2015-09-21 Steven G. Kargl <kargl@gcc.gnu.org>
[official-gcc.git] / gcc / c / c-parser.c
blob2fab3f0ebe404f207f6d55b14a0d0d7dc110a2f4
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 "tree.h"
43 #include "alias.h"
44 #include "flags.h"
45 #include "fold-const.h"
46 #include "stringpool.h"
47 #include "attribs.h"
48 #include "stor-layout.h"
49 #include "varasm.h"
50 #include "trans-mem.h"
51 #include "langhooks.h"
52 #include "cpplib.h"
53 #include "timevar.h"
54 #include "c-family/c-pragma.h"
55 #include "c-tree.h"
56 #include "c-lang.h"
57 #include "flags.h"
58 #include "c-family/c-common.h"
59 #include "c-family/c-objc.h"
60 #include "target.h"
61 #include "hard-reg-set.h"
62 #include "function.h"
63 #include "cgraph.h"
64 #include "plugin.h"
65 #include "omp-low.h"
66 #include "builtins.h"
67 #include "gomp-constants.h"
68 #include "c-family/c-indentation.h"
71 /* Initialization routine for this file. */
73 void
74 c_parse_init (void)
76 /* The only initialization required is of the reserved word
77 identifiers. */
78 unsigned int i;
79 tree id;
80 int mask = 0;
82 /* Make sure RID_MAX hasn't grown past the 8 bits used to hold the keyword in
83 the c_token structure. */
84 gcc_assert (RID_MAX <= 255);
86 mask |= D_CXXONLY;
87 if (!flag_isoc99)
88 mask |= D_C99;
89 if (flag_no_asm)
91 mask |= D_ASM | D_EXT;
92 if (!flag_isoc99)
93 mask |= D_EXT89;
95 if (!c_dialect_objc ())
96 mask |= D_OBJC | D_CXX_OBJC;
98 ridpointers = ggc_cleared_vec_alloc<tree> ((int) RID_MAX);
99 for (i = 0; i < num_c_common_reswords; i++)
101 /* If a keyword is disabled, do not enter it into the table
102 and so create a canonical spelling that isn't a keyword. */
103 if (c_common_reswords[i].disable & mask)
105 if (warn_cxx_compat
106 && (c_common_reswords[i].disable & D_CXXWARN))
108 id = get_identifier (c_common_reswords[i].word);
109 C_SET_RID_CODE (id, RID_CXX_COMPAT_WARN);
110 C_IS_RESERVED_WORD (id) = 1;
112 continue;
115 id = get_identifier (c_common_reswords[i].word);
116 C_SET_RID_CODE (id, c_common_reswords[i].rid);
117 C_IS_RESERVED_WORD (id) = 1;
118 ridpointers [(int) c_common_reswords[i].rid] = id;
121 for (i = 0; i < NUM_INT_N_ENTS; i++)
123 /* We always create the symbols but they aren't always supported. */
124 char name[50];
125 sprintf (name, "__int%d", int_n_data[i].bitsize);
126 id = get_identifier (name);
127 C_SET_RID_CODE (id, RID_FIRST_INT_N + i);
128 C_IS_RESERVED_WORD (id) = 1;
132 /* The C lexer intermediates between the lexer in cpplib and c-lex.c
133 and the C parser. Unlike the C++ lexer, the parser structure
134 stores the lexer information instead of using a separate structure.
135 Identifiers are separated into ordinary identifiers, type names,
136 keywords and some other Objective-C types of identifiers, and some
137 look-ahead is maintained.
139 ??? It might be a good idea to lex the whole file up front (as for
140 C++). It would then be possible to share more of the C and C++
141 lexer code, if desired. */
143 /* More information about the type of a CPP_NAME token. */
144 enum c_id_kind {
145 /* An ordinary identifier. */
146 C_ID_ID,
147 /* An identifier declared as a typedef name. */
148 C_ID_TYPENAME,
149 /* An identifier declared as an Objective-C class name. */
150 C_ID_CLASSNAME,
151 /* An address space identifier. */
152 C_ID_ADDRSPACE,
153 /* Not an identifier. */
154 C_ID_NONE
157 /* A single C token after string literal concatenation and conversion
158 of preprocessing tokens to tokens. */
159 struct GTY (()) c_token {
160 /* The kind of token. */
161 ENUM_BITFIELD (cpp_ttype) type : 8;
162 /* If this token is a CPP_NAME, this value indicates whether also
163 declared as some kind of type. Otherwise, it is C_ID_NONE. */
164 ENUM_BITFIELD (c_id_kind) id_kind : 8;
165 /* If this token is a keyword, this value indicates which keyword.
166 Otherwise, this value is RID_MAX. */
167 ENUM_BITFIELD (rid) keyword : 8;
168 /* If this token is a CPP_PRAGMA, this indicates the pragma that
169 was seen. Otherwise it is PRAGMA_NONE. */
170 ENUM_BITFIELD (pragma_kind) pragma_kind : 8;
171 /* The location at which this token was found. */
172 location_t location;
173 /* The value associated with this token, if any. */
174 tree value;
177 /* A parser structure recording information about the state and
178 context of parsing. Includes lexer information with up to two
179 tokens of look-ahead; more are not needed for C. */
180 struct GTY(()) c_parser {
181 /* The look-ahead tokens. */
182 c_token * GTY((skip)) tokens;
183 /* Buffer for look-ahead tokens. */
184 c_token tokens_buf[2];
185 /* How many look-ahead tokens are available (0, 1 or 2, or
186 more if parsing from pre-lexed tokens). */
187 unsigned int tokens_avail;
188 /* True if a syntax error is being recovered from; false otherwise.
189 c_parser_error sets this flag. It should clear this flag when
190 enough tokens have been consumed to recover from the error. */
191 BOOL_BITFIELD error : 1;
192 /* True if we're processing a pragma, and shouldn't automatically
193 consume CPP_PRAGMA_EOL. */
194 BOOL_BITFIELD in_pragma : 1;
195 /* True if we're parsing the outermost block of an if statement. */
196 BOOL_BITFIELD in_if_block : 1;
197 /* True if we want to lex an untranslated string. */
198 BOOL_BITFIELD lex_untranslated_string : 1;
200 /* Objective-C specific parser/lexer information. */
202 /* True if we are in a context where the Objective-C "PQ" keywords
203 are considered keywords. */
204 BOOL_BITFIELD objc_pq_context : 1;
205 /* True if we are parsing a (potential) Objective-C foreach
206 statement. This is set to true after we parsed 'for (' and while
207 we wait for 'in' or ';' to decide if it's a standard C for loop or an
208 Objective-C foreach loop. */
209 BOOL_BITFIELD objc_could_be_foreach_context : 1;
210 /* The following flag is needed to contextualize Objective-C lexical
211 analysis. In some cases (e.g., 'int NSObject;'), it is
212 undesirable to bind an identifier to an Objective-C class, even
213 if a class with that name exists. */
214 BOOL_BITFIELD objc_need_raw_identifier : 1;
215 /* Nonzero if we're processing a __transaction statement. The value
216 is 1 | TM_STMT_ATTR_*. */
217 unsigned int in_transaction : 4;
218 /* True if we are in a context where the Objective-C "Property attribute"
219 keywords are valid. */
220 BOOL_BITFIELD objc_property_attr_context : 1;
222 /* Cilk Plus specific parser/lexer information. */
224 /* Buffer to hold all the tokens from parsing the vector attribute for the
225 SIMD-enabled functions (formerly known as elemental functions). */
226 vec <c_token, va_gc> *cilk_simd_fn_tokens;
230 /* The actual parser and external interface. ??? Does this need to be
231 garbage-collected? */
233 static GTY (()) c_parser *the_parser;
235 /* Read in and lex a single token, storing it in *TOKEN. */
237 static void
238 c_lex_one_token (c_parser *parser, c_token *token)
240 timevar_push (TV_LEX);
242 token->type = c_lex_with_flags (&token->value, &token->location, NULL,
243 (parser->lex_untranslated_string
244 ? C_LEX_STRING_NO_TRANSLATE : 0));
245 token->id_kind = C_ID_NONE;
246 token->keyword = RID_MAX;
247 token->pragma_kind = PRAGMA_NONE;
249 switch (token->type)
251 case CPP_NAME:
253 tree decl;
255 bool objc_force_identifier = parser->objc_need_raw_identifier;
256 if (c_dialect_objc ())
257 parser->objc_need_raw_identifier = false;
259 if (C_IS_RESERVED_WORD (token->value))
261 enum rid rid_code = C_RID_CODE (token->value);
263 if (rid_code == RID_CXX_COMPAT_WARN)
265 warning_at (token->location,
266 OPT_Wc___compat,
267 "identifier %qE conflicts with C++ keyword",
268 token->value);
270 else if (rid_code >= RID_FIRST_ADDR_SPACE
271 && rid_code <= RID_LAST_ADDR_SPACE)
273 token->id_kind = C_ID_ADDRSPACE;
274 token->keyword = rid_code;
275 break;
277 else if (c_dialect_objc () && OBJC_IS_PQ_KEYWORD (rid_code))
279 /* We found an Objective-C "pq" keyword (in, out,
280 inout, bycopy, byref, oneway). They need special
281 care because the interpretation depends on the
282 context. */
283 if (parser->objc_pq_context)
285 token->type = CPP_KEYWORD;
286 token->keyword = rid_code;
287 break;
289 else if (parser->objc_could_be_foreach_context
290 && rid_code == RID_IN)
292 /* We are in Objective-C, inside a (potential)
293 foreach context (which means after having
294 parsed 'for (', but before having parsed ';'),
295 and we found 'in'. We consider it the keyword
296 which terminates the declaration at the
297 beginning of a foreach-statement. Note that
298 this means you can't use 'in' for anything else
299 in that context; in particular, in Objective-C
300 you can't use 'in' as the name of the running
301 variable in a C for loop. We could potentially
302 try to add code here to disambiguate, but it
303 seems a reasonable limitation. */
304 token->type = CPP_KEYWORD;
305 token->keyword = rid_code;
306 break;
308 /* Else, "pq" keywords outside of the "pq" context are
309 not keywords, and we fall through to the code for
310 normal tokens. */
312 else if (c_dialect_objc () && OBJC_IS_PATTR_KEYWORD (rid_code))
314 /* We found an Objective-C "property attribute"
315 keyword (getter, setter, readonly, etc). These are
316 only valid in the property context. */
317 if (parser->objc_property_attr_context)
319 token->type = CPP_KEYWORD;
320 token->keyword = rid_code;
321 break;
323 /* Else they are not special keywords.
326 else if (c_dialect_objc ()
327 && (OBJC_IS_AT_KEYWORD (rid_code)
328 || OBJC_IS_CXX_KEYWORD (rid_code)))
330 /* We found one of the Objective-C "@" keywords (defs,
331 selector, synchronized, etc) or one of the
332 Objective-C "cxx" keywords (class, private,
333 protected, public, try, catch, throw) without a
334 preceding '@' sign. Do nothing and fall through to
335 the code for normal tokens (in C++ we would still
336 consider the CXX ones keywords, but not in C). */
339 else
341 token->type = CPP_KEYWORD;
342 token->keyword = rid_code;
343 break;
347 decl = lookup_name (token->value);
348 if (decl)
350 if (TREE_CODE (decl) == TYPE_DECL)
352 token->id_kind = C_ID_TYPENAME;
353 break;
356 else if (c_dialect_objc ())
358 tree objc_interface_decl = objc_is_class_name (token->value);
359 /* Objective-C class names are in the same namespace as
360 variables and typedefs, and hence are shadowed by local
361 declarations. */
362 if (objc_interface_decl
363 && (!objc_force_identifier || global_bindings_p ()))
365 token->value = objc_interface_decl;
366 token->id_kind = C_ID_CLASSNAME;
367 break;
370 token->id_kind = C_ID_ID;
372 break;
373 case CPP_AT_NAME:
374 /* This only happens in Objective-C; it must be a keyword. */
375 token->type = CPP_KEYWORD;
376 switch (C_RID_CODE (token->value))
378 /* Replace 'class' with '@class', 'private' with '@private',
379 etc. This prevents confusion with the C++ keyword
380 'class', and makes the tokens consistent with other
381 Objective-C 'AT' keywords. For example '@class' is
382 reported as RID_AT_CLASS which is consistent with
383 '@synchronized', which is reported as
384 RID_AT_SYNCHRONIZED.
386 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
387 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
388 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
389 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
390 case RID_THROW: token->keyword = RID_AT_THROW; break;
391 case RID_TRY: token->keyword = RID_AT_TRY; break;
392 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
393 default: token->keyword = C_RID_CODE (token->value);
395 break;
396 case CPP_COLON:
397 case CPP_COMMA:
398 case CPP_CLOSE_PAREN:
399 case CPP_SEMICOLON:
400 /* These tokens may affect the interpretation of any identifiers
401 following, if doing Objective-C. */
402 if (c_dialect_objc ())
403 parser->objc_need_raw_identifier = false;
404 break;
405 case CPP_PRAGMA:
406 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
407 token->pragma_kind = (enum pragma_kind) TREE_INT_CST_LOW (token->value);
408 token->value = NULL;
409 break;
410 default:
411 break;
413 timevar_pop (TV_LEX);
416 /* Return a pointer to the next token from PARSER, reading it in if
417 necessary. */
419 static inline c_token *
420 c_parser_peek_token (c_parser *parser)
422 if (parser->tokens_avail == 0)
424 c_lex_one_token (parser, &parser->tokens[0]);
425 parser->tokens_avail = 1;
427 return &parser->tokens[0];
430 /* Return true if the next token from PARSER has the indicated
431 TYPE. */
433 static inline bool
434 c_parser_next_token_is (c_parser *parser, enum cpp_ttype type)
436 return c_parser_peek_token (parser)->type == type;
439 /* Return true if the next token from PARSER does not have the
440 indicated TYPE. */
442 static inline bool
443 c_parser_next_token_is_not (c_parser *parser, enum cpp_ttype type)
445 return !c_parser_next_token_is (parser, type);
448 /* Return true if the next token from PARSER is the indicated
449 KEYWORD. */
451 static inline bool
452 c_parser_next_token_is_keyword (c_parser *parser, enum rid keyword)
454 return c_parser_peek_token (parser)->keyword == keyword;
457 /* Return a pointer to the next-but-one token from PARSER, reading it
458 in if necessary. The next token is already read in. */
460 static c_token *
461 c_parser_peek_2nd_token (c_parser *parser)
463 if (parser->tokens_avail >= 2)
464 return &parser->tokens[1];
465 gcc_assert (parser->tokens_avail == 1);
466 gcc_assert (parser->tokens[0].type != CPP_EOF);
467 gcc_assert (parser->tokens[0].type != CPP_PRAGMA_EOL);
468 c_lex_one_token (parser, &parser->tokens[1]);
469 parser->tokens_avail = 2;
470 return &parser->tokens[1];
473 /* Return true if TOKEN can start a type name,
474 false otherwise. */
475 static bool
476 c_token_starts_typename (c_token *token)
478 switch (token->type)
480 case CPP_NAME:
481 switch (token->id_kind)
483 case C_ID_ID:
484 return false;
485 case C_ID_ADDRSPACE:
486 return true;
487 case C_ID_TYPENAME:
488 return true;
489 case C_ID_CLASSNAME:
490 gcc_assert (c_dialect_objc ());
491 return true;
492 default:
493 gcc_unreachable ();
495 case CPP_KEYWORD:
496 switch (token->keyword)
498 case RID_UNSIGNED:
499 case RID_LONG:
500 case RID_SHORT:
501 case RID_SIGNED:
502 case RID_COMPLEX:
503 case RID_INT:
504 case RID_CHAR:
505 case RID_FLOAT:
506 case RID_DOUBLE:
507 case RID_VOID:
508 case RID_DFLOAT32:
509 case RID_DFLOAT64:
510 case RID_DFLOAT128:
511 case RID_BOOL:
512 case RID_ENUM:
513 case RID_STRUCT:
514 case RID_UNION:
515 case RID_TYPEOF:
516 case RID_CONST:
517 case RID_ATOMIC:
518 case RID_VOLATILE:
519 case RID_RESTRICT:
520 case RID_ATTRIBUTE:
521 case RID_FRACT:
522 case RID_ACCUM:
523 case RID_SAT:
524 case RID_AUTO_TYPE:
525 return true;
526 default:
527 if (token->keyword >= RID_FIRST_INT_N
528 && token->keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
529 && int_n_enabled_p[token->keyword - RID_FIRST_INT_N])
530 return true;
531 return false;
533 case CPP_LESS:
534 if (c_dialect_objc ())
535 return true;
536 return false;
537 default:
538 return false;
542 enum c_lookahead_kind {
543 /* Always treat unknown identifiers as typenames. */
544 cla_prefer_type,
546 /* Could be parsing a nonabstract declarator. Only treat an identifier
547 as a typename if followed by another identifier or a star. */
548 cla_nonabstract_decl,
550 /* Never treat identifiers as typenames. */
551 cla_prefer_id
554 /* Return true if the next token from PARSER can start a type name,
555 false otherwise. LA specifies how to do lookahead in order to
556 detect unknown type names. If unsure, pick CLA_PREFER_ID. */
558 static inline bool
559 c_parser_next_tokens_start_typename (c_parser *parser, enum c_lookahead_kind la)
561 c_token *token = c_parser_peek_token (parser);
562 if (c_token_starts_typename (token))
563 return true;
565 /* Try a bit harder to detect an unknown typename. */
566 if (la != cla_prefer_id
567 && token->type == CPP_NAME
568 && token->id_kind == C_ID_ID
570 /* Do not try too hard when we could have "object in array". */
571 && !parser->objc_could_be_foreach_context
573 && (la == cla_prefer_type
574 || c_parser_peek_2nd_token (parser)->type == CPP_NAME
575 || c_parser_peek_2nd_token (parser)->type == CPP_MULT)
577 /* Only unknown identifiers. */
578 && !lookup_name (token->value))
579 return true;
581 return false;
584 /* Return true if TOKEN is a type qualifier, false otherwise. */
585 static bool
586 c_token_is_qualifier (c_token *token)
588 switch (token->type)
590 case CPP_NAME:
591 switch (token->id_kind)
593 case C_ID_ADDRSPACE:
594 return true;
595 default:
596 return false;
598 case CPP_KEYWORD:
599 switch (token->keyword)
601 case RID_CONST:
602 case RID_VOLATILE:
603 case RID_RESTRICT:
604 case RID_ATTRIBUTE:
605 case RID_ATOMIC:
606 return true;
607 default:
608 return false;
610 case CPP_LESS:
611 return false;
612 default:
613 gcc_unreachable ();
617 /* Return true if the next token from PARSER is a type qualifier,
618 false otherwise. */
619 static inline bool
620 c_parser_next_token_is_qualifier (c_parser *parser)
622 c_token *token = c_parser_peek_token (parser);
623 return c_token_is_qualifier (token);
626 /* Return true if TOKEN can start declaration specifiers, false
627 otherwise. */
628 static bool
629 c_token_starts_declspecs (c_token *token)
631 switch (token->type)
633 case CPP_NAME:
634 switch (token->id_kind)
636 case C_ID_ID:
637 return false;
638 case C_ID_ADDRSPACE:
639 return true;
640 case C_ID_TYPENAME:
641 return true;
642 case C_ID_CLASSNAME:
643 gcc_assert (c_dialect_objc ());
644 return true;
645 default:
646 gcc_unreachable ();
648 case CPP_KEYWORD:
649 switch (token->keyword)
651 case RID_STATIC:
652 case RID_EXTERN:
653 case RID_REGISTER:
654 case RID_TYPEDEF:
655 case RID_INLINE:
656 case RID_NORETURN:
657 case RID_AUTO:
658 case RID_THREAD:
659 case RID_UNSIGNED:
660 case RID_LONG:
661 case RID_SHORT:
662 case RID_SIGNED:
663 case RID_COMPLEX:
664 case RID_INT:
665 case RID_CHAR:
666 case RID_FLOAT:
667 case RID_DOUBLE:
668 case RID_VOID:
669 case RID_DFLOAT32:
670 case RID_DFLOAT64:
671 case RID_DFLOAT128:
672 case RID_BOOL:
673 case RID_ENUM:
674 case RID_STRUCT:
675 case RID_UNION:
676 case RID_TYPEOF:
677 case RID_CONST:
678 case RID_VOLATILE:
679 case RID_RESTRICT:
680 case RID_ATTRIBUTE:
681 case RID_FRACT:
682 case RID_ACCUM:
683 case RID_SAT:
684 case RID_ALIGNAS:
685 case RID_ATOMIC:
686 case RID_AUTO_TYPE:
687 return true;
688 default:
689 if (token->keyword >= RID_FIRST_INT_N
690 && token->keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
691 && int_n_enabled_p[token->keyword - RID_FIRST_INT_N])
692 return true;
693 return false;
695 case CPP_LESS:
696 if (c_dialect_objc ())
697 return true;
698 return false;
699 default:
700 return false;
705 /* Return true if TOKEN can start declaration specifiers or a static
706 assertion, false otherwise. */
707 static bool
708 c_token_starts_declaration (c_token *token)
710 if (c_token_starts_declspecs (token)
711 || token->keyword == RID_STATIC_ASSERT)
712 return true;
713 else
714 return false;
717 /* Return true if the next token from PARSER can start declaration
718 specifiers, false otherwise. */
719 static inline bool
720 c_parser_next_token_starts_declspecs (c_parser *parser)
722 c_token *token = c_parser_peek_token (parser);
724 /* In Objective-C, a classname normally starts a declspecs unless it
725 is immediately followed by a dot. In that case, it is the
726 Objective-C 2.0 "dot-syntax" for class objects, ie, calls the
727 setter/getter on the class. c_token_starts_declspecs() can't
728 differentiate between the two cases because it only checks the
729 current token, so we have a special check here. */
730 if (c_dialect_objc ()
731 && token->type == CPP_NAME
732 && token->id_kind == C_ID_CLASSNAME
733 && c_parser_peek_2nd_token (parser)->type == CPP_DOT)
734 return false;
736 return c_token_starts_declspecs (token);
739 /* Return true if the next tokens from PARSER can start declaration
740 specifiers or a static assertion, false otherwise. */
741 static inline bool
742 c_parser_next_tokens_start_declaration (c_parser *parser)
744 c_token *token = c_parser_peek_token (parser);
746 /* Same as above. */
747 if (c_dialect_objc ()
748 && token->type == CPP_NAME
749 && token->id_kind == C_ID_CLASSNAME
750 && c_parser_peek_2nd_token (parser)->type == CPP_DOT)
751 return false;
753 /* Labels do not start declarations. */
754 if (token->type == CPP_NAME
755 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
756 return false;
758 if (c_token_starts_declaration (token))
759 return true;
761 if (c_parser_next_tokens_start_typename (parser, cla_nonabstract_decl))
762 return true;
764 return false;
767 /* Consume the next token from PARSER. */
769 static void
770 c_parser_consume_token (c_parser *parser)
772 gcc_assert (parser->tokens_avail >= 1);
773 gcc_assert (parser->tokens[0].type != CPP_EOF);
774 gcc_assert (!parser->in_pragma || parser->tokens[0].type != CPP_PRAGMA_EOL);
775 gcc_assert (parser->error || parser->tokens[0].type != CPP_PRAGMA);
776 if (parser->tokens != &parser->tokens_buf[0])
777 parser->tokens++;
778 else if (parser->tokens_avail == 2)
779 parser->tokens[0] = parser->tokens[1];
780 parser->tokens_avail--;
783 /* Expect the current token to be a #pragma. Consume it and remember
784 that we've begun parsing a pragma. */
786 static void
787 c_parser_consume_pragma (c_parser *parser)
789 gcc_assert (!parser->in_pragma);
790 gcc_assert (parser->tokens_avail >= 1);
791 gcc_assert (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--;
797 parser->in_pragma = true;
800 /* Update the global input_location from TOKEN. */
801 static inline void
802 c_parser_set_source_position_from_token (c_token *token)
804 if (token->type != CPP_EOF)
806 input_location = token->location;
810 /* Issue a diagnostic of the form
811 FILE:LINE: MESSAGE before TOKEN
812 where TOKEN is the next token in the input stream of PARSER.
813 MESSAGE (specified by the caller) is usually of the form "expected
814 OTHER-TOKEN".
816 Do not issue a diagnostic if still recovering from an error.
818 ??? This is taken from the C++ parser, but building up messages in
819 this way is not i18n-friendly and some other approach should be
820 used. */
822 static void
823 c_parser_error (c_parser *parser, const char *gmsgid)
825 c_token *token = c_parser_peek_token (parser);
826 if (parser->error)
827 return;
828 parser->error = true;
829 if (!gmsgid)
830 return;
831 /* This diagnostic makes more sense if it is tagged to the line of
832 the token we just peeked at. */
833 c_parser_set_source_position_from_token (token);
834 c_parse_error (gmsgid,
835 /* Because c_parse_error does not understand
836 CPP_KEYWORD, keywords are treated like
837 identifiers. */
838 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
839 /* ??? The C parser does not save the cpp flags of a
840 token, we need to pass 0 here and we will not get
841 the source spelling of some tokens but rather the
842 canonical spelling. */
843 token->value, /*flags=*/0);
846 /* If the next token is of the indicated TYPE, consume it. Otherwise,
847 issue the error MSGID. If MSGID is NULL then a message has already
848 been produced and no message will be produced this time. Returns
849 true if found, false otherwise. */
851 static bool
852 c_parser_require (c_parser *parser,
853 enum cpp_ttype type,
854 const char *msgid)
856 if (c_parser_next_token_is (parser, type))
858 c_parser_consume_token (parser);
859 return true;
861 else
863 c_parser_error (parser, msgid);
864 return false;
868 /* If the next token is the indicated keyword, consume it. Otherwise,
869 issue the error MSGID. Returns true if found, false otherwise. */
871 static bool
872 c_parser_require_keyword (c_parser *parser,
873 enum rid keyword,
874 const char *msgid)
876 if (c_parser_next_token_is_keyword (parser, keyword))
878 c_parser_consume_token (parser);
879 return true;
881 else
883 c_parser_error (parser, msgid);
884 return false;
888 /* Like c_parser_require, except that tokens will be skipped until the
889 desired token is found. An error message is still produced if the
890 next token is not as expected. If MSGID is NULL then a message has
891 already been produced and no message will be produced this
892 time. */
894 static void
895 c_parser_skip_until_found (c_parser *parser,
896 enum cpp_ttype type,
897 const char *msgid)
899 unsigned nesting_depth = 0;
901 if (c_parser_require (parser, type, msgid))
902 return;
904 /* Skip tokens until the desired token is found. */
905 while (true)
907 /* Peek at the next token. */
908 c_token *token = c_parser_peek_token (parser);
909 /* If we've reached the token we want, consume it and stop. */
910 if (token->type == type && !nesting_depth)
912 c_parser_consume_token (parser);
913 break;
916 /* If we've run out of tokens, stop. */
917 if (token->type == CPP_EOF)
918 return;
919 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
920 return;
921 if (token->type == CPP_OPEN_BRACE
922 || token->type == CPP_OPEN_PAREN
923 || token->type == CPP_OPEN_SQUARE)
924 ++nesting_depth;
925 else if (token->type == CPP_CLOSE_BRACE
926 || token->type == CPP_CLOSE_PAREN
927 || token->type == CPP_CLOSE_SQUARE)
929 if (nesting_depth-- == 0)
930 break;
932 /* Consume this token. */
933 c_parser_consume_token (parser);
935 parser->error = false;
938 /* Skip tokens until the end of a parameter is found, but do not
939 consume the comma, semicolon or closing delimiter. */
941 static void
942 c_parser_skip_to_end_of_parameter (c_parser *parser)
944 unsigned nesting_depth = 0;
946 while (true)
948 c_token *token = c_parser_peek_token (parser);
949 if ((token->type == CPP_COMMA || token->type == CPP_SEMICOLON)
950 && !nesting_depth)
951 break;
952 /* If we've run out of tokens, stop. */
953 if (token->type == CPP_EOF)
954 return;
955 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
956 return;
957 if (token->type == CPP_OPEN_BRACE
958 || token->type == CPP_OPEN_PAREN
959 || token->type == CPP_OPEN_SQUARE)
960 ++nesting_depth;
961 else if (token->type == CPP_CLOSE_BRACE
962 || token->type == CPP_CLOSE_PAREN
963 || token->type == CPP_CLOSE_SQUARE)
965 if (nesting_depth-- == 0)
966 break;
968 /* Consume this token. */
969 c_parser_consume_token (parser);
971 parser->error = false;
974 /* Expect to be at the end of the pragma directive and consume an
975 end of line marker. */
977 static void
978 c_parser_skip_to_pragma_eol (c_parser *parser, bool error_if_not_eol = true)
980 gcc_assert (parser->in_pragma);
981 parser->in_pragma = false;
983 if (error_if_not_eol && c_parser_peek_token (parser)->type != CPP_PRAGMA_EOL)
984 c_parser_error (parser, "expected end of line");
986 cpp_ttype token_type;
989 c_token *token = c_parser_peek_token (parser);
990 token_type = token->type;
991 if (token_type == CPP_EOF)
992 break;
993 c_parser_consume_token (parser);
995 while (token_type != CPP_PRAGMA_EOL);
997 parser->error = false;
1000 /* Skip tokens until we have consumed an entire block, or until we
1001 have consumed a non-nested ';'. */
1003 static void
1004 c_parser_skip_to_end_of_block_or_statement (c_parser *parser)
1006 unsigned nesting_depth = 0;
1007 bool save_error = parser->error;
1009 while (true)
1011 c_token *token;
1013 /* Peek at the next token. */
1014 token = c_parser_peek_token (parser);
1016 switch (token->type)
1018 case CPP_EOF:
1019 return;
1021 case CPP_PRAGMA_EOL:
1022 if (parser->in_pragma)
1023 return;
1024 break;
1026 case CPP_SEMICOLON:
1027 /* If the next token is a ';', we have reached the
1028 end of the statement. */
1029 if (!nesting_depth)
1031 /* Consume the ';'. */
1032 c_parser_consume_token (parser);
1033 goto finished;
1035 break;
1037 case CPP_CLOSE_BRACE:
1038 /* If the next token is a non-nested '}', then we have
1039 reached the end of the current block. */
1040 if (nesting_depth == 0 || --nesting_depth == 0)
1042 c_parser_consume_token (parser);
1043 goto finished;
1045 break;
1047 case CPP_OPEN_BRACE:
1048 /* If it the next token is a '{', then we are entering a new
1049 block. Consume the entire block. */
1050 ++nesting_depth;
1051 break;
1053 case CPP_PRAGMA:
1054 /* If we see a pragma, consume the whole thing at once. We
1055 have some safeguards against consuming pragmas willy-nilly.
1056 Normally, we'd expect to be here with parser->error set,
1057 which disables these safeguards. But it's possible to get
1058 here for secondary error recovery, after parser->error has
1059 been cleared. */
1060 c_parser_consume_pragma (parser);
1061 c_parser_skip_to_pragma_eol (parser);
1062 parser->error = save_error;
1063 continue;
1065 default:
1066 break;
1069 c_parser_consume_token (parser);
1072 finished:
1073 parser->error = false;
1076 /* CPP's options (initialized by c-opts.c). */
1077 extern cpp_options *cpp_opts;
1079 /* Save the warning flags which are controlled by __extension__. */
1081 static inline int
1082 disable_extension_diagnostics (void)
1084 int ret = (pedantic
1085 | (warn_pointer_arith << 1)
1086 | (warn_traditional << 2)
1087 | (flag_iso << 3)
1088 | (warn_long_long << 4)
1089 | (warn_cxx_compat << 5)
1090 | (warn_overlength_strings << 6)
1091 /* warn_c90_c99_compat has three states: -1/0/1, so we must
1092 play tricks to properly restore it. */
1093 | ((warn_c90_c99_compat == 1) << 7)
1094 | ((warn_c90_c99_compat == -1) << 8)
1095 /* Similarly for warn_c99_c11_compat. */
1096 | ((warn_c99_c11_compat == 1) << 9)
1097 | ((warn_c99_c11_compat == -1) << 10)
1099 cpp_opts->cpp_pedantic = pedantic = 0;
1100 warn_pointer_arith = 0;
1101 cpp_opts->cpp_warn_traditional = warn_traditional = 0;
1102 flag_iso = 0;
1103 cpp_opts->cpp_warn_long_long = warn_long_long = 0;
1104 warn_cxx_compat = 0;
1105 warn_overlength_strings = 0;
1106 warn_c90_c99_compat = 0;
1107 warn_c99_c11_compat = 0;
1108 return ret;
1111 /* Restore the warning flags which are controlled by __extension__.
1112 FLAGS is the return value from disable_extension_diagnostics. */
1114 static inline void
1115 restore_extension_diagnostics (int flags)
1117 cpp_opts->cpp_pedantic = pedantic = flags & 1;
1118 warn_pointer_arith = (flags >> 1) & 1;
1119 cpp_opts->cpp_warn_traditional = warn_traditional = (flags >> 2) & 1;
1120 flag_iso = (flags >> 3) & 1;
1121 cpp_opts->cpp_warn_long_long = warn_long_long = (flags >> 4) & 1;
1122 warn_cxx_compat = (flags >> 5) & 1;
1123 warn_overlength_strings = (flags >> 6) & 1;
1124 /* See above for why is this needed. */
1125 warn_c90_c99_compat = (flags >> 7) & 1 ? 1 : ((flags >> 8) & 1 ? -1 : 0);
1126 warn_c99_c11_compat = (flags >> 9) & 1 ? 1 : ((flags >> 10) & 1 ? -1 : 0);
1129 /* Possibly kinds of declarator to parse. */
1130 enum c_dtr_syn {
1131 /* A normal declarator with an identifier. */
1132 C_DTR_NORMAL,
1133 /* An abstract declarator (maybe empty). */
1134 C_DTR_ABSTRACT,
1135 /* A parameter declarator: may be either, but after a type name does
1136 not redeclare a typedef name as an identifier if it can
1137 alternatively be interpreted as a typedef name; see DR#009,
1138 applied in C90 TC1, omitted from C99 and reapplied in C99 TC2
1139 following DR#249. For example, given a typedef T, "int T" and
1140 "int *T" are valid parameter declarations redeclaring T, while
1141 "int (T)" and "int * (T)" and "int (T[])" and "int (T (int))" are
1142 abstract declarators rather than involving redundant parentheses;
1143 the same applies with attributes inside the parentheses before
1144 "T". */
1145 C_DTR_PARM
1148 /* The binary operation precedence levels, where 0 is a dummy lowest level
1149 used for the bottom of the stack. */
1150 enum c_parser_prec {
1151 PREC_NONE,
1152 PREC_LOGOR,
1153 PREC_LOGAND,
1154 PREC_BITOR,
1155 PREC_BITXOR,
1156 PREC_BITAND,
1157 PREC_EQ,
1158 PREC_REL,
1159 PREC_SHIFT,
1160 PREC_ADD,
1161 PREC_MULT,
1162 NUM_PRECS
1165 static void c_parser_external_declaration (c_parser *);
1166 static void c_parser_asm_definition (c_parser *);
1167 static void c_parser_declaration_or_fndef (c_parser *, bool, bool, bool,
1168 bool, bool, tree *, vec<c_token>);
1169 static void c_parser_static_assert_declaration_no_semi (c_parser *);
1170 static void c_parser_static_assert_declaration (c_parser *);
1171 static void c_parser_declspecs (c_parser *, struct c_declspecs *, bool, bool,
1172 bool, bool, bool, enum c_lookahead_kind);
1173 static struct c_typespec c_parser_enum_specifier (c_parser *);
1174 static struct c_typespec c_parser_struct_or_union_specifier (c_parser *);
1175 static tree c_parser_struct_declaration (c_parser *);
1176 static struct c_typespec c_parser_typeof_specifier (c_parser *);
1177 static tree c_parser_alignas_specifier (c_parser *);
1178 static struct c_declarator *c_parser_declarator (c_parser *, bool, c_dtr_syn,
1179 bool *);
1180 static struct c_declarator *c_parser_direct_declarator (c_parser *, bool,
1181 c_dtr_syn, bool *);
1182 static struct c_declarator *c_parser_direct_declarator_inner (c_parser *,
1183 bool,
1184 struct c_declarator *);
1185 static struct c_arg_info *c_parser_parms_declarator (c_parser *, bool, tree);
1186 static struct c_arg_info *c_parser_parms_list_declarator (c_parser *, tree,
1187 tree);
1188 static struct c_parm *c_parser_parameter_declaration (c_parser *, tree);
1189 static tree c_parser_simple_asm_expr (c_parser *);
1190 static tree c_parser_attributes (c_parser *);
1191 static struct c_type_name *c_parser_type_name (c_parser *);
1192 static struct c_expr c_parser_initializer (c_parser *);
1193 static struct c_expr c_parser_braced_init (c_parser *, tree, bool);
1194 static void c_parser_initelt (c_parser *, struct obstack *);
1195 static void c_parser_initval (c_parser *, struct c_expr *,
1196 struct obstack *);
1197 static tree c_parser_compound_statement (c_parser *);
1198 static void c_parser_compound_statement_nostart (c_parser *);
1199 static void c_parser_label (c_parser *);
1200 static void c_parser_statement (c_parser *);
1201 static void c_parser_statement_after_labels (c_parser *);
1202 static void c_parser_if_statement (c_parser *);
1203 static void c_parser_switch_statement (c_parser *);
1204 static void c_parser_while_statement (c_parser *, bool);
1205 static void c_parser_do_statement (c_parser *, bool);
1206 static void c_parser_for_statement (c_parser *, bool);
1207 static tree c_parser_asm_statement (c_parser *);
1208 static tree c_parser_asm_operands (c_parser *);
1209 static tree c_parser_asm_goto_operands (c_parser *);
1210 static tree c_parser_asm_clobbers (c_parser *);
1211 static struct c_expr c_parser_expr_no_commas (c_parser *, struct c_expr *,
1212 tree = NULL_TREE);
1213 static struct c_expr c_parser_conditional_expression (c_parser *,
1214 struct c_expr *, tree);
1215 static struct c_expr c_parser_binary_expression (c_parser *, struct c_expr *,
1216 tree);
1217 static struct c_expr c_parser_cast_expression (c_parser *, struct c_expr *);
1218 static struct c_expr c_parser_unary_expression (c_parser *);
1219 static struct c_expr c_parser_sizeof_expression (c_parser *);
1220 static struct c_expr c_parser_alignof_expression (c_parser *);
1221 static struct c_expr c_parser_postfix_expression (c_parser *);
1222 static struct c_expr c_parser_postfix_expression_after_paren_type (c_parser *,
1223 struct c_type_name *,
1224 location_t);
1225 static struct c_expr c_parser_postfix_expression_after_primary (c_parser *,
1226 location_t loc,
1227 struct c_expr);
1228 static tree c_parser_transaction (c_parser *, enum rid);
1229 static struct c_expr c_parser_transaction_expression (c_parser *, enum rid);
1230 static tree c_parser_transaction_cancel (c_parser *);
1231 static struct c_expr c_parser_expression (c_parser *);
1232 static struct c_expr c_parser_expression_conv (c_parser *);
1233 static vec<tree, va_gc> *c_parser_expr_list (c_parser *, bool, bool,
1234 vec<tree, va_gc> **, location_t *,
1235 tree *, vec<location_t> *,
1236 unsigned int * = NULL);
1237 static void c_parser_oacc_enter_exit_data (c_parser *, bool);
1238 static void c_parser_oacc_update (c_parser *);
1239 static tree c_parser_oacc_loop (location_t, c_parser *, char *);
1240 static void c_parser_omp_construct (c_parser *);
1241 static void c_parser_omp_threadprivate (c_parser *);
1242 static void c_parser_omp_barrier (c_parser *);
1243 static void c_parser_omp_flush (c_parser *);
1244 static tree c_parser_omp_for_loop (location_t, c_parser *, enum tree_code,
1245 tree, tree *);
1246 static void c_parser_omp_taskwait (c_parser *);
1247 static void c_parser_omp_taskyield (c_parser *);
1248 static void c_parser_omp_cancel (c_parser *);
1249 static void c_parser_omp_cancellation_point (c_parser *);
1251 enum pragma_context { pragma_external, pragma_struct, pragma_param,
1252 pragma_stmt, pragma_compound };
1253 static bool c_parser_pragma (c_parser *, enum pragma_context);
1254 static bool c_parser_omp_target (c_parser *, enum pragma_context);
1255 static void c_parser_omp_end_declare_target (c_parser *);
1256 static void c_parser_omp_declare (c_parser *, enum pragma_context);
1258 /* These Objective-C parser functions are only ever called when
1259 compiling Objective-C. */
1260 static void c_parser_objc_class_definition (c_parser *, tree);
1261 static void c_parser_objc_class_instance_variables (c_parser *);
1262 static void c_parser_objc_class_declaration (c_parser *);
1263 static void c_parser_objc_alias_declaration (c_parser *);
1264 static void c_parser_objc_protocol_definition (c_parser *, tree);
1265 static bool c_parser_objc_method_type (c_parser *);
1266 static void c_parser_objc_method_definition (c_parser *);
1267 static void c_parser_objc_methodprotolist (c_parser *);
1268 static void c_parser_objc_methodproto (c_parser *);
1269 static tree c_parser_objc_method_decl (c_parser *, bool, tree *, tree *);
1270 static tree c_parser_objc_type_name (c_parser *);
1271 static tree c_parser_objc_protocol_refs (c_parser *);
1272 static void c_parser_objc_try_catch_finally_statement (c_parser *);
1273 static void c_parser_objc_synchronized_statement (c_parser *);
1274 static tree c_parser_objc_selector (c_parser *);
1275 static tree c_parser_objc_selector_arg (c_parser *);
1276 static tree c_parser_objc_receiver (c_parser *);
1277 static tree c_parser_objc_message_args (c_parser *);
1278 static tree c_parser_objc_keywordexpr (c_parser *);
1279 static void c_parser_objc_at_property_declaration (c_parser *);
1280 static void c_parser_objc_at_synthesize_declaration (c_parser *);
1281 static void c_parser_objc_at_dynamic_declaration (c_parser *);
1282 static bool c_parser_objc_diagnose_bad_element_prefix
1283 (c_parser *, struct c_declspecs *);
1285 /* Cilk Plus supporting routines. */
1286 static void c_parser_cilk_simd (c_parser *);
1287 static void c_parser_cilk_for (c_parser *, tree);
1288 static bool c_parser_cilk_verify_simd (c_parser *, enum pragma_context);
1289 static tree c_parser_array_notation (location_t, c_parser *, tree, tree);
1290 static tree c_parser_cilk_clause_vectorlength (c_parser *, tree, bool);
1291 static void c_parser_cilk_grainsize (c_parser *);
1293 /* Parse a translation unit (C90 6.7, C99 6.9).
1295 translation-unit:
1296 external-declarations
1298 external-declarations:
1299 external-declaration
1300 external-declarations external-declaration
1302 GNU extensions:
1304 translation-unit:
1305 empty
1308 static void
1309 c_parser_translation_unit (c_parser *parser)
1311 if (c_parser_next_token_is (parser, CPP_EOF))
1313 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
1314 "ISO C forbids an empty translation unit");
1316 else
1318 void *obstack_position = obstack_alloc (&parser_obstack, 0);
1319 mark_valid_location_for_stdc_pragma (false);
1322 ggc_collect ();
1323 c_parser_external_declaration (parser);
1324 obstack_free (&parser_obstack, obstack_position);
1326 while (c_parser_next_token_is_not (parser, CPP_EOF));
1330 /* Parse an external declaration (C90 6.7, C99 6.9).
1332 external-declaration:
1333 function-definition
1334 declaration
1336 GNU extensions:
1338 external-declaration:
1339 asm-definition
1341 __extension__ external-declaration
1343 Objective-C:
1345 external-declaration:
1346 objc-class-definition
1347 objc-class-declaration
1348 objc-alias-declaration
1349 objc-protocol-definition
1350 objc-method-definition
1351 @end
1354 static void
1355 c_parser_external_declaration (c_parser *parser)
1357 int ext;
1358 switch (c_parser_peek_token (parser)->type)
1360 case CPP_KEYWORD:
1361 switch (c_parser_peek_token (parser)->keyword)
1363 case RID_EXTENSION:
1364 ext = disable_extension_diagnostics ();
1365 c_parser_consume_token (parser);
1366 c_parser_external_declaration (parser);
1367 restore_extension_diagnostics (ext);
1368 break;
1369 case RID_ASM:
1370 c_parser_asm_definition (parser);
1371 break;
1372 case RID_AT_INTERFACE:
1373 case RID_AT_IMPLEMENTATION:
1374 gcc_assert (c_dialect_objc ());
1375 c_parser_objc_class_definition (parser, NULL_TREE);
1376 break;
1377 case RID_AT_CLASS:
1378 gcc_assert (c_dialect_objc ());
1379 c_parser_objc_class_declaration (parser);
1380 break;
1381 case RID_AT_ALIAS:
1382 gcc_assert (c_dialect_objc ());
1383 c_parser_objc_alias_declaration (parser);
1384 break;
1385 case RID_AT_PROTOCOL:
1386 gcc_assert (c_dialect_objc ());
1387 c_parser_objc_protocol_definition (parser, NULL_TREE);
1388 break;
1389 case RID_AT_PROPERTY:
1390 gcc_assert (c_dialect_objc ());
1391 c_parser_objc_at_property_declaration (parser);
1392 break;
1393 case RID_AT_SYNTHESIZE:
1394 gcc_assert (c_dialect_objc ());
1395 c_parser_objc_at_synthesize_declaration (parser);
1396 break;
1397 case RID_AT_DYNAMIC:
1398 gcc_assert (c_dialect_objc ());
1399 c_parser_objc_at_dynamic_declaration (parser);
1400 break;
1401 case RID_AT_END:
1402 gcc_assert (c_dialect_objc ());
1403 c_parser_consume_token (parser);
1404 objc_finish_implementation ();
1405 break;
1406 default:
1407 goto decl_or_fndef;
1409 break;
1410 case CPP_SEMICOLON:
1411 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
1412 "ISO C does not allow extra %<;%> outside of a function");
1413 c_parser_consume_token (parser);
1414 break;
1415 case CPP_PRAGMA:
1416 mark_valid_location_for_stdc_pragma (true);
1417 c_parser_pragma (parser, pragma_external);
1418 mark_valid_location_for_stdc_pragma (false);
1419 break;
1420 case CPP_PLUS:
1421 case CPP_MINUS:
1422 if (c_dialect_objc ())
1424 c_parser_objc_method_definition (parser);
1425 break;
1427 /* Else fall through, and yield a syntax error trying to parse
1428 as a declaration or function definition. */
1429 default:
1430 decl_or_fndef:
1431 /* A declaration or a function definition (or, in Objective-C,
1432 an @interface or @protocol with prefix attributes). We can
1433 only tell which after parsing the declaration specifiers, if
1434 any, and the first declarator. */
1435 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
1436 NULL, vNULL);
1437 break;
1441 static void c_finish_omp_declare_simd (c_parser *, tree, tree, vec<c_token>);
1443 /* Parse a declaration or function definition (C90 6.5, 6.7.1, C99
1444 6.7, 6.9.1). If FNDEF_OK is true, a function definition is
1445 accepted; otherwise (old-style parameter declarations) only other
1446 declarations are accepted. If STATIC_ASSERT_OK is true, a static
1447 assertion is accepted; otherwise (old-style parameter declarations)
1448 it is not. If NESTED is true, we are inside a function or parsing
1449 old-style parameter declarations; any functions encountered are
1450 nested functions and declaration specifiers are required; otherwise
1451 we are at top level and functions are normal functions and
1452 declaration specifiers may be optional. If EMPTY_OK is true, empty
1453 declarations are OK (subject to all other constraints); otherwise
1454 (old-style parameter declarations) they are diagnosed. If
1455 START_ATTR_OK is true, the declaration specifiers may start with
1456 attributes; otherwise they may not.
1457 OBJC_FOREACH_OBJECT_DECLARATION can be used to get back the parsed
1458 declaration when parsing an Objective-C foreach statement.
1460 declaration:
1461 declaration-specifiers init-declarator-list[opt] ;
1462 static_assert-declaration
1464 function-definition:
1465 declaration-specifiers[opt] declarator declaration-list[opt]
1466 compound-statement
1468 declaration-list:
1469 declaration
1470 declaration-list declaration
1472 init-declarator-list:
1473 init-declarator
1474 init-declarator-list , init-declarator
1476 init-declarator:
1477 declarator simple-asm-expr[opt] attributes[opt]
1478 declarator simple-asm-expr[opt] attributes[opt] = initializer
1480 GNU extensions:
1482 nested-function-definition:
1483 declaration-specifiers declarator declaration-list[opt]
1484 compound-statement
1486 Objective-C:
1487 attributes objc-class-definition
1488 attributes objc-category-definition
1489 attributes objc-protocol-definition
1491 The simple-asm-expr and attributes are GNU extensions.
1493 This function does not handle __extension__; that is handled in its
1494 callers. ??? Following the old parser, __extension__ may start
1495 external declarations, declarations in functions and declarations
1496 at the start of "for" loops, but not old-style parameter
1497 declarations.
1499 C99 requires declaration specifiers in a function definition; the
1500 absence is diagnosed through the diagnosis of implicit int. In GNU
1501 C we also allow but diagnose declarations without declaration
1502 specifiers, but only at top level (elsewhere they conflict with
1503 other syntax).
1505 In Objective-C, declarations of the looping variable in a foreach
1506 statement are exceptionally terminated by 'in' (for example, 'for
1507 (NSObject *object in array) { ... }').
1509 OpenMP:
1511 declaration:
1512 threadprivate-directive */
1514 static void
1515 c_parser_declaration_or_fndef (c_parser *parser, bool fndef_ok,
1516 bool static_assert_ok, bool empty_ok,
1517 bool nested, bool start_attr_ok,
1518 tree *objc_foreach_object_declaration,
1519 vec<c_token> omp_declare_simd_clauses)
1521 struct c_declspecs *specs;
1522 tree prefix_attrs;
1523 tree all_prefix_attrs;
1524 bool diagnosed_no_specs = false;
1525 location_t here = c_parser_peek_token (parser)->location;
1527 if (static_assert_ok
1528 && c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
1530 c_parser_static_assert_declaration (parser);
1531 return;
1533 specs = build_null_declspecs ();
1535 /* Try to detect an unknown type name when we have "A B" or "A *B". */
1536 if (c_parser_peek_token (parser)->type == CPP_NAME
1537 && c_parser_peek_token (parser)->id_kind == C_ID_ID
1538 && (c_parser_peek_2nd_token (parser)->type == CPP_NAME
1539 || c_parser_peek_2nd_token (parser)->type == CPP_MULT)
1540 && (!nested || !lookup_name (c_parser_peek_token (parser)->value)))
1542 tree name = c_parser_peek_token (parser)->value;
1543 error_at (here, "unknown type name %qE", name);
1544 /* Give a hint to the user. This is not C++ with its implicit
1545 typedef. */
1546 if (tag_exists_p (RECORD_TYPE, name))
1547 inform (here, "use %<struct%> keyword to refer to the type");
1548 else if (tag_exists_p (UNION_TYPE, name))
1549 inform (here, "use %<union%> keyword to refer to the type");
1550 else if (tag_exists_p (ENUMERAL_TYPE, name))
1551 inform (here, "use %<enum%> keyword to refer to the type");
1553 /* Parse declspecs normally to get a correct pointer type, but avoid
1554 a further "fails to be a type name" error. Refuse nested functions
1555 since it is not how the user likely wants us to recover. */
1556 c_parser_peek_token (parser)->type = CPP_KEYWORD;
1557 c_parser_peek_token (parser)->keyword = RID_VOID;
1558 c_parser_peek_token (parser)->value = error_mark_node;
1559 fndef_ok = !nested;
1562 c_parser_declspecs (parser, specs, true, true, start_attr_ok,
1563 true, true, cla_nonabstract_decl);
1564 if (parser->error)
1566 c_parser_skip_to_end_of_block_or_statement (parser);
1567 return;
1569 if (nested && !specs->declspecs_seen_p)
1571 c_parser_error (parser, "expected declaration specifiers");
1572 c_parser_skip_to_end_of_block_or_statement (parser);
1573 return;
1575 finish_declspecs (specs);
1576 bool auto_type_p = specs->typespec_word == cts_auto_type;
1577 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1579 if (auto_type_p)
1580 error_at (here, "%<__auto_type%> in empty declaration");
1581 else if (empty_ok)
1582 shadow_tag (specs);
1583 else
1585 shadow_tag_warned (specs, 1);
1586 pedwarn (here, 0, "empty declaration");
1588 c_parser_consume_token (parser);
1589 return;
1592 /* Provide better error recovery. Note that a type name here is usually
1593 better diagnosed as a redeclaration. */
1594 if (empty_ok
1595 && specs->typespec_kind == ctsk_tagdef
1596 && c_parser_next_token_starts_declspecs (parser)
1597 && !c_parser_next_token_is (parser, CPP_NAME))
1599 c_parser_error (parser, "expected %<;%>, identifier or %<(%>");
1600 parser->error = false;
1601 shadow_tag_warned (specs, 1);
1602 return;
1604 else if (c_dialect_objc () && !auto_type_p)
1606 /* Prefix attributes are an error on method decls. */
1607 switch (c_parser_peek_token (parser)->type)
1609 case CPP_PLUS:
1610 case CPP_MINUS:
1611 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1612 return;
1613 if (specs->attrs)
1615 warning_at (c_parser_peek_token (parser)->location,
1616 OPT_Wattributes,
1617 "prefix attributes are ignored for methods");
1618 specs->attrs = NULL_TREE;
1620 if (fndef_ok)
1621 c_parser_objc_method_definition (parser);
1622 else
1623 c_parser_objc_methodproto (parser);
1624 return;
1625 break;
1626 default:
1627 break;
1629 /* This is where we parse 'attributes @interface ...',
1630 'attributes @implementation ...', 'attributes @protocol ...'
1631 (where attributes could be, for example, __attribute__
1632 ((deprecated)).
1634 switch (c_parser_peek_token (parser)->keyword)
1636 case RID_AT_INTERFACE:
1638 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1639 return;
1640 c_parser_objc_class_definition (parser, specs->attrs);
1641 return;
1643 break;
1644 case RID_AT_IMPLEMENTATION:
1646 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1647 return;
1648 if (specs->attrs)
1650 warning_at (c_parser_peek_token (parser)->location,
1651 OPT_Wattributes,
1652 "prefix attributes are ignored for implementations");
1653 specs->attrs = NULL_TREE;
1655 c_parser_objc_class_definition (parser, NULL_TREE);
1656 return;
1658 break;
1659 case RID_AT_PROTOCOL:
1661 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1662 return;
1663 c_parser_objc_protocol_definition (parser, specs->attrs);
1664 return;
1666 break;
1667 case RID_AT_ALIAS:
1668 case RID_AT_CLASS:
1669 case RID_AT_END:
1670 case RID_AT_PROPERTY:
1671 if (specs->attrs)
1673 c_parser_error (parser, "unexpected attribute");
1674 specs->attrs = NULL;
1676 break;
1677 default:
1678 break;
1682 pending_xref_error ();
1683 prefix_attrs = specs->attrs;
1684 all_prefix_attrs = prefix_attrs;
1685 specs->attrs = NULL_TREE;
1686 while (true)
1688 struct c_declarator *declarator;
1689 bool dummy = false;
1690 timevar_id_t tv;
1691 tree fnbody;
1692 /* Declaring either one or more declarators (in which case we
1693 should diagnose if there were no declaration specifiers) or a
1694 function definition (in which case the diagnostic for
1695 implicit int suffices). */
1696 declarator = c_parser_declarator (parser,
1697 specs->typespec_kind != ctsk_none,
1698 C_DTR_NORMAL, &dummy);
1699 if (declarator == NULL)
1701 if (omp_declare_simd_clauses.exists ()
1702 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
1703 c_finish_omp_declare_simd (parser, NULL_TREE, NULL_TREE,
1704 omp_declare_simd_clauses);
1705 c_parser_skip_to_end_of_block_or_statement (parser);
1706 return;
1708 if (auto_type_p && declarator->kind != cdk_id)
1710 error_at (here,
1711 "%<__auto_type%> requires a plain identifier"
1712 " as declarator");
1713 c_parser_skip_to_end_of_block_or_statement (parser);
1714 return;
1716 if (c_parser_next_token_is (parser, CPP_EQ)
1717 || c_parser_next_token_is (parser, CPP_COMMA)
1718 || c_parser_next_token_is (parser, CPP_SEMICOLON)
1719 || c_parser_next_token_is_keyword (parser, RID_ASM)
1720 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE)
1721 || c_parser_next_token_is_keyword (parser, RID_IN))
1723 tree asm_name = NULL_TREE;
1724 tree postfix_attrs = NULL_TREE;
1725 if (!diagnosed_no_specs && !specs->declspecs_seen_p)
1727 diagnosed_no_specs = true;
1728 pedwarn (here, 0, "data definition has no type or storage class");
1730 /* Having seen a data definition, there cannot now be a
1731 function definition. */
1732 fndef_ok = false;
1733 if (c_parser_next_token_is_keyword (parser, RID_ASM))
1734 asm_name = c_parser_simple_asm_expr (parser);
1735 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1737 postfix_attrs = c_parser_attributes (parser);
1738 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
1740 /* This means there is an attribute specifier after
1741 the declarator in a function definition. Provide
1742 some more information for the user. */
1743 error_at (here, "attributes should be specified before the "
1744 "declarator in a function definition");
1745 c_parser_skip_to_end_of_block_or_statement (parser);
1746 return;
1749 if (c_parser_next_token_is (parser, CPP_EQ))
1751 tree d;
1752 struct c_expr init;
1753 location_t init_loc;
1754 c_parser_consume_token (parser);
1755 if (auto_type_p)
1757 start_init (NULL_TREE, asm_name, global_bindings_p ());
1758 init_loc = c_parser_peek_token (parser)->location;
1759 init = c_parser_expr_no_commas (parser, NULL);
1760 if (TREE_CODE (init.value) == COMPONENT_REF
1761 && DECL_C_BIT_FIELD (TREE_OPERAND (init.value, 1)))
1762 error_at (here,
1763 "%<__auto_type%> used with a bit-field"
1764 " initializer");
1765 init = convert_lvalue_to_rvalue (init_loc, init, true, true);
1766 tree init_type = TREE_TYPE (init.value);
1767 /* As with typeof, remove all qualifiers from atomic types. */
1768 if (init_type != error_mark_node && TYPE_ATOMIC (init_type))
1769 init_type
1770 = c_build_qualified_type (init_type, TYPE_UNQUALIFIED);
1771 bool vm_type = variably_modified_type_p (init_type,
1772 NULL_TREE);
1773 if (vm_type)
1774 init.value = c_save_expr (init.value);
1775 finish_init ();
1776 specs->typespec_kind = ctsk_typeof;
1777 specs->locations[cdw_typedef] = init_loc;
1778 specs->typedef_p = true;
1779 specs->type = init_type;
1780 if (vm_type)
1782 bool maybe_const = true;
1783 tree type_expr = c_fully_fold (init.value, false,
1784 &maybe_const);
1785 specs->expr_const_operands &= maybe_const;
1786 if (specs->expr)
1787 specs->expr = build2 (COMPOUND_EXPR,
1788 TREE_TYPE (type_expr),
1789 specs->expr, type_expr);
1790 else
1791 specs->expr = type_expr;
1793 d = start_decl (declarator, specs, true,
1794 chainon (postfix_attrs, all_prefix_attrs));
1795 if (!d)
1796 d = error_mark_node;
1797 if (omp_declare_simd_clauses.exists ()
1798 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
1799 c_finish_omp_declare_simd (parser, d, NULL_TREE,
1800 omp_declare_simd_clauses);
1802 else
1804 /* The declaration of the variable is in effect while
1805 its initializer is parsed. */
1806 d = start_decl (declarator, specs, true,
1807 chainon (postfix_attrs, all_prefix_attrs));
1808 if (!d)
1809 d = error_mark_node;
1810 if (omp_declare_simd_clauses.exists ()
1811 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
1812 c_finish_omp_declare_simd (parser, d, NULL_TREE,
1813 omp_declare_simd_clauses);
1814 start_init (d, asm_name, global_bindings_p ());
1815 init_loc = c_parser_peek_token (parser)->location;
1816 init = c_parser_initializer (parser);
1817 finish_init ();
1819 if (d != error_mark_node)
1821 maybe_warn_string_init (init_loc, TREE_TYPE (d), init);
1822 finish_decl (d, init_loc, init.value,
1823 init.original_type, asm_name);
1826 else
1828 if (auto_type_p)
1830 error_at (here,
1831 "%<__auto_type%> requires an initialized "
1832 "data declaration");
1833 c_parser_skip_to_end_of_block_or_statement (parser);
1834 return;
1836 tree d = start_decl (declarator, specs, false,
1837 chainon (postfix_attrs,
1838 all_prefix_attrs));
1839 if (omp_declare_simd_clauses.exists ()
1840 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
1842 tree parms = NULL_TREE;
1843 if (d && TREE_CODE (d) == FUNCTION_DECL)
1845 struct c_declarator *ce = declarator;
1846 while (ce != NULL)
1847 if (ce->kind == cdk_function)
1849 parms = ce->u.arg_info->parms;
1850 break;
1852 else
1853 ce = ce->declarator;
1855 if (parms)
1856 temp_store_parm_decls (d, parms);
1857 c_finish_omp_declare_simd (parser, d, parms,
1858 omp_declare_simd_clauses);
1859 if (parms)
1860 temp_pop_parm_decls ();
1862 if (d)
1863 finish_decl (d, UNKNOWN_LOCATION, NULL_TREE,
1864 NULL_TREE, asm_name);
1866 if (c_parser_next_token_is_keyword (parser, RID_IN))
1868 if (d)
1869 *objc_foreach_object_declaration = d;
1870 else
1871 *objc_foreach_object_declaration = error_mark_node;
1874 if (c_parser_next_token_is (parser, CPP_COMMA))
1876 if (auto_type_p)
1878 error_at (here,
1879 "%<__auto_type%> may only be used with"
1880 " a single declarator");
1881 c_parser_skip_to_end_of_block_or_statement (parser);
1882 return;
1884 c_parser_consume_token (parser);
1885 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1886 all_prefix_attrs = chainon (c_parser_attributes (parser),
1887 prefix_attrs);
1888 else
1889 all_prefix_attrs = prefix_attrs;
1890 continue;
1892 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1894 c_parser_consume_token (parser);
1895 return;
1897 else if (c_parser_next_token_is_keyword (parser, RID_IN))
1899 /* This can only happen in Objective-C: we found the
1900 'in' that terminates the declaration inside an
1901 Objective-C foreach statement. Do not consume the
1902 token, so that the caller can use it to determine
1903 that this indeed is a foreach context. */
1904 return;
1906 else
1908 c_parser_error (parser, "expected %<,%> or %<;%>");
1909 c_parser_skip_to_end_of_block_or_statement (parser);
1910 return;
1913 else if (auto_type_p)
1915 error_at (here,
1916 "%<__auto_type%> requires an initialized data declaration");
1917 c_parser_skip_to_end_of_block_or_statement (parser);
1918 return;
1920 else if (!fndef_ok)
1922 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, "
1923 "%<asm%> or %<__attribute__%>");
1924 c_parser_skip_to_end_of_block_or_statement (parser);
1925 return;
1927 /* Function definition (nested or otherwise). */
1928 if (nested)
1930 pedwarn (here, OPT_Wpedantic, "ISO C forbids nested functions");
1931 c_push_function_context ();
1933 if (!start_function (specs, declarator, all_prefix_attrs))
1935 /* This can appear in many cases looking nothing like a
1936 function definition, so we don't give a more specific
1937 error suggesting there was one. */
1938 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, %<asm%> "
1939 "or %<__attribute__%>");
1940 if (nested)
1941 c_pop_function_context ();
1942 break;
1945 if (DECL_DECLARED_INLINE_P (current_function_decl))
1946 tv = TV_PARSE_INLINE;
1947 else
1948 tv = TV_PARSE_FUNC;
1949 timevar_push (tv);
1951 /* Parse old-style parameter declarations. ??? Attributes are
1952 not allowed to start declaration specifiers here because of a
1953 syntax conflict between a function declaration with attribute
1954 suffix and a function definition with an attribute prefix on
1955 first old-style parameter declaration. Following the old
1956 parser, they are not accepted on subsequent old-style
1957 parameter declarations either. However, there is no
1958 ambiguity after the first declaration, nor indeed on the
1959 first as long as we don't allow postfix attributes after a
1960 declarator with a nonempty identifier list in a definition;
1961 and postfix attributes have never been accepted here in
1962 function definitions either. */
1963 while (c_parser_next_token_is_not (parser, CPP_EOF)
1964 && c_parser_next_token_is_not (parser, CPP_OPEN_BRACE))
1965 c_parser_declaration_or_fndef (parser, false, false, false,
1966 true, false, NULL, vNULL);
1967 store_parm_decls ();
1968 if (omp_declare_simd_clauses.exists ()
1969 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
1970 c_finish_omp_declare_simd (parser, current_function_decl, NULL_TREE,
1971 omp_declare_simd_clauses);
1972 DECL_STRUCT_FUNCTION (current_function_decl)->function_start_locus
1973 = c_parser_peek_token (parser)->location;
1974 fnbody = c_parser_compound_statement (parser);
1975 if (flag_cilkplus && contains_array_notation_expr (fnbody))
1976 fnbody = expand_array_notation_exprs (fnbody);
1977 if (nested)
1979 tree decl = current_function_decl;
1980 /* Mark nested functions as needing static-chain initially.
1981 lower_nested_functions will recompute it but the
1982 DECL_STATIC_CHAIN flag is also used before that happens,
1983 by initializer_constant_valid_p. See gcc.dg/nested-fn-2.c. */
1984 DECL_STATIC_CHAIN (decl) = 1;
1985 add_stmt (fnbody);
1986 finish_function ();
1987 c_pop_function_context ();
1988 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
1990 else
1992 add_stmt (fnbody);
1993 finish_function ();
1996 timevar_pop (tv);
1997 break;
2001 /* Parse an asm-definition (asm() outside a function body). This is a
2002 GNU extension.
2004 asm-definition:
2005 simple-asm-expr ;
2008 static void
2009 c_parser_asm_definition (c_parser *parser)
2011 tree asm_str = c_parser_simple_asm_expr (parser);
2012 if (asm_str)
2013 symtab->finalize_toplevel_asm (asm_str);
2014 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
2017 /* Parse a static assertion (C11 6.7.10).
2019 static_assert-declaration:
2020 static_assert-declaration-no-semi ;
2023 static void
2024 c_parser_static_assert_declaration (c_parser *parser)
2026 c_parser_static_assert_declaration_no_semi (parser);
2027 if (parser->error
2028 || !c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
2029 c_parser_skip_to_end_of_block_or_statement (parser);
2032 /* Parse a static assertion (C11 6.7.10), without the trailing
2033 semicolon.
2035 static_assert-declaration-no-semi:
2036 _Static_assert ( constant-expression , string-literal )
2039 static void
2040 c_parser_static_assert_declaration_no_semi (c_parser *parser)
2042 location_t assert_loc, value_loc;
2043 tree value;
2044 tree string;
2046 gcc_assert (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT));
2047 assert_loc = c_parser_peek_token (parser)->location;
2048 if (flag_isoc99)
2049 pedwarn_c99 (assert_loc, OPT_Wpedantic,
2050 "ISO C99 does not support %<_Static_assert%>");
2051 else
2052 pedwarn_c99 (assert_loc, OPT_Wpedantic,
2053 "ISO C90 does not support %<_Static_assert%>");
2054 c_parser_consume_token (parser);
2055 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2056 return;
2057 value_loc = c_parser_peek_token (parser)->location;
2058 value = c_parser_expr_no_commas (parser, NULL).value;
2059 parser->lex_untranslated_string = true;
2060 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
2062 parser->lex_untranslated_string = false;
2063 return;
2065 switch (c_parser_peek_token (parser)->type)
2067 case CPP_STRING:
2068 case CPP_STRING16:
2069 case CPP_STRING32:
2070 case CPP_WSTRING:
2071 case CPP_UTF8STRING:
2072 string = c_parser_peek_token (parser)->value;
2073 c_parser_consume_token (parser);
2074 parser->lex_untranslated_string = false;
2075 break;
2076 default:
2077 c_parser_error (parser, "expected string literal");
2078 parser->lex_untranslated_string = false;
2079 return;
2081 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
2083 if (!INTEGRAL_TYPE_P (TREE_TYPE (value)))
2085 error_at (value_loc, "expression in static assertion is not an integer");
2086 return;
2088 if (TREE_CODE (value) != INTEGER_CST)
2090 value = c_fully_fold (value, false, NULL);
2091 /* Strip no-op conversions. */
2092 STRIP_TYPE_NOPS (value);
2093 if (TREE_CODE (value) == INTEGER_CST)
2094 pedwarn (value_loc, OPT_Wpedantic, "expression in static assertion "
2095 "is not an integer constant expression");
2097 if (TREE_CODE (value) != INTEGER_CST)
2099 error_at (value_loc, "expression in static assertion is not constant");
2100 return;
2102 constant_expression_warning (value);
2103 if (integer_zerop (value))
2104 error_at (assert_loc, "static assertion failed: %E", string);
2107 /* Parse some declaration specifiers (possibly none) (C90 6.5, C99
2108 6.7), adding them to SPECS (which may already include some).
2109 Storage class specifiers are accepted iff SCSPEC_OK; type
2110 specifiers are accepted iff TYPESPEC_OK; alignment specifiers are
2111 accepted iff ALIGNSPEC_OK; attributes are accepted at the start
2112 iff START_ATTR_OK; __auto_type is accepted iff AUTO_TYPE_OK.
2114 declaration-specifiers:
2115 storage-class-specifier declaration-specifiers[opt]
2116 type-specifier declaration-specifiers[opt]
2117 type-qualifier declaration-specifiers[opt]
2118 function-specifier declaration-specifiers[opt]
2119 alignment-specifier declaration-specifiers[opt]
2121 Function specifiers (inline) are from C99, and are currently
2122 handled as storage class specifiers, as is __thread. Alignment
2123 specifiers are from C11.
2125 C90 6.5.1, C99 6.7.1:
2126 storage-class-specifier:
2127 typedef
2128 extern
2129 static
2130 auto
2131 register
2132 _Thread_local
2134 (_Thread_local is new in C11.)
2136 C99 6.7.4:
2137 function-specifier:
2138 inline
2139 _Noreturn
2141 (_Noreturn is new in C11.)
2143 C90 6.5.2, C99 6.7.2:
2144 type-specifier:
2145 void
2146 char
2147 short
2149 long
2150 float
2151 double
2152 signed
2153 unsigned
2154 _Bool
2155 _Complex
2156 [_Imaginary removed in C99 TC2]
2157 struct-or-union-specifier
2158 enum-specifier
2159 typedef-name
2160 atomic-type-specifier
2162 (_Bool and _Complex are new in C99.)
2163 (atomic-type-specifier is new in C11.)
2165 C90 6.5.3, C99 6.7.3:
2167 type-qualifier:
2168 const
2169 restrict
2170 volatile
2171 address-space-qualifier
2172 _Atomic
2174 (restrict is new in C99.)
2175 (_Atomic is new in C11.)
2177 GNU extensions:
2179 declaration-specifiers:
2180 attributes declaration-specifiers[opt]
2182 type-qualifier:
2183 address-space
2185 address-space:
2186 identifier recognized by the target
2188 storage-class-specifier:
2189 __thread
2191 type-specifier:
2192 typeof-specifier
2193 __auto_type
2194 __intN
2195 _Decimal32
2196 _Decimal64
2197 _Decimal128
2198 _Fract
2199 _Accum
2200 _Sat
2202 (_Fract, _Accum, and _Sat are new from ISO/IEC DTR 18037:
2203 http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1169.pdf)
2205 atomic-type-specifier
2206 _Atomic ( type-name )
2208 Objective-C:
2210 type-specifier:
2211 class-name objc-protocol-refs[opt]
2212 typedef-name objc-protocol-refs
2213 objc-protocol-refs
2216 static void
2217 c_parser_declspecs (c_parser *parser, struct c_declspecs *specs,
2218 bool scspec_ok, bool typespec_ok, bool start_attr_ok,
2219 bool alignspec_ok, bool auto_type_ok,
2220 enum c_lookahead_kind la)
2222 bool attrs_ok = start_attr_ok;
2223 bool seen_type = specs->typespec_kind != ctsk_none;
2225 if (!typespec_ok)
2226 gcc_assert (la == cla_prefer_id);
2228 while (c_parser_next_token_is (parser, CPP_NAME)
2229 || c_parser_next_token_is (parser, CPP_KEYWORD)
2230 || (c_dialect_objc () && c_parser_next_token_is (parser, CPP_LESS)))
2232 struct c_typespec t;
2233 tree attrs;
2234 tree align;
2235 location_t loc = c_parser_peek_token (parser)->location;
2237 /* If we cannot accept a type, exit if the next token must start
2238 one. Also, if we already have seen a tagged definition,
2239 a typename would be an error anyway and likely the user
2240 has simply forgotten a semicolon, so we exit. */
2241 if ((!typespec_ok || specs->typespec_kind == ctsk_tagdef)
2242 && c_parser_next_tokens_start_typename (parser, la)
2243 && !c_parser_next_token_is_qualifier (parser))
2244 break;
2246 if (c_parser_next_token_is (parser, CPP_NAME))
2248 c_token *name_token = c_parser_peek_token (parser);
2249 tree value = name_token->value;
2250 c_id_kind kind = name_token->id_kind;
2252 if (kind == C_ID_ADDRSPACE)
2254 addr_space_t as
2255 = name_token->keyword - RID_FIRST_ADDR_SPACE;
2256 declspecs_add_addrspace (name_token->location, specs, as);
2257 c_parser_consume_token (parser);
2258 attrs_ok = true;
2259 continue;
2262 gcc_assert (!c_parser_next_token_is_qualifier (parser));
2264 /* If we cannot accept a type, and the next token must start one,
2265 exit. Do the same if we already have seen a tagged definition,
2266 since it would be an error anyway and likely the user has simply
2267 forgotten a semicolon. */
2268 if (seen_type || !c_parser_next_tokens_start_typename (parser, la))
2269 break;
2271 /* Now at an unknown typename (C_ID_ID), a C_ID_TYPENAME or
2272 a C_ID_CLASSNAME. */
2273 c_parser_consume_token (parser);
2274 seen_type = true;
2275 attrs_ok = true;
2276 if (kind == C_ID_ID)
2278 error_at (loc, "unknown type name %qE", value);
2279 t.kind = ctsk_typedef;
2280 t.spec = error_mark_node;
2282 else if (kind == C_ID_TYPENAME
2283 && (!c_dialect_objc ()
2284 || c_parser_next_token_is_not (parser, CPP_LESS)))
2286 t.kind = ctsk_typedef;
2287 /* For a typedef name, record the meaning, not the name.
2288 In case of 'foo foo, bar;'. */
2289 t.spec = lookup_name (value);
2291 else
2293 tree proto = NULL_TREE;
2294 gcc_assert (c_dialect_objc ());
2295 t.kind = ctsk_objc;
2296 if (c_parser_next_token_is (parser, CPP_LESS))
2297 proto = c_parser_objc_protocol_refs (parser);
2298 t.spec = objc_get_protocol_qualified_type (value, proto);
2300 t.expr = NULL_TREE;
2301 t.expr_const_operands = true;
2302 declspecs_add_type (name_token->location, specs, t);
2303 continue;
2305 if (c_parser_next_token_is (parser, CPP_LESS))
2307 /* Make "<SomeProtocol>" equivalent to "id <SomeProtocol>" -
2308 nisse@lysator.liu.se. */
2309 tree proto;
2310 gcc_assert (c_dialect_objc ());
2311 if (!typespec_ok || seen_type)
2312 break;
2313 proto = c_parser_objc_protocol_refs (parser);
2314 t.kind = ctsk_objc;
2315 t.spec = objc_get_protocol_qualified_type (NULL_TREE, proto);
2316 t.expr = NULL_TREE;
2317 t.expr_const_operands = true;
2318 declspecs_add_type (loc, specs, t);
2319 continue;
2321 gcc_assert (c_parser_next_token_is (parser, CPP_KEYWORD));
2322 switch (c_parser_peek_token (parser)->keyword)
2324 case RID_STATIC:
2325 case RID_EXTERN:
2326 case RID_REGISTER:
2327 case RID_TYPEDEF:
2328 case RID_INLINE:
2329 case RID_NORETURN:
2330 case RID_AUTO:
2331 case RID_THREAD:
2332 if (!scspec_ok)
2333 goto out;
2334 attrs_ok = true;
2335 /* TODO: Distinguish between function specifiers (inline, noreturn)
2336 and storage class specifiers, either here or in
2337 declspecs_add_scspec. */
2338 declspecs_add_scspec (loc, specs,
2339 c_parser_peek_token (parser)->value);
2340 c_parser_consume_token (parser);
2341 break;
2342 case RID_AUTO_TYPE:
2343 if (!auto_type_ok)
2344 goto out;
2345 /* Fall through. */
2346 case RID_UNSIGNED:
2347 case RID_LONG:
2348 case RID_SHORT:
2349 case RID_SIGNED:
2350 case RID_COMPLEX:
2351 case RID_INT:
2352 case RID_CHAR:
2353 case RID_FLOAT:
2354 case RID_DOUBLE:
2355 case RID_VOID:
2356 case RID_DFLOAT32:
2357 case RID_DFLOAT64:
2358 case RID_DFLOAT128:
2359 case RID_BOOL:
2360 case RID_FRACT:
2361 case RID_ACCUM:
2362 case RID_SAT:
2363 case RID_INT_N_0:
2364 case RID_INT_N_1:
2365 case RID_INT_N_2:
2366 case RID_INT_N_3:
2367 if (!typespec_ok)
2368 goto out;
2369 attrs_ok = true;
2370 seen_type = true;
2371 if (c_dialect_objc ())
2372 parser->objc_need_raw_identifier = true;
2373 t.kind = ctsk_resword;
2374 t.spec = c_parser_peek_token (parser)->value;
2375 t.expr = NULL_TREE;
2376 t.expr_const_operands = true;
2377 declspecs_add_type (loc, specs, t);
2378 c_parser_consume_token (parser);
2379 break;
2380 case RID_ENUM:
2381 if (!typespec_ok)
2382 goto out;
2383 attrs_ok = true;
2384 seen_type = true;
2385 t = c_parser_enum_specifier (parser);
2386 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, t.spec);
2387 declspecs_add_type (loc, specs, t);
2388 break;
2389 case RID_STRUCT:
2390 case RID_UNION:
2391 if (!typespec_ok)
2392 goto out;
2393 attrs_ok = true;
2394 seen_type = true;
2395 t = c_parser_struct_or_union_specifier (parser);
2396 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, t.spec);
2397 declspecs_add_type (loc, specs, t);
2398 break;
2399 case RID_TYPEOF:
2400 /* ??? The old parser rejected typeof after other type
2401 specifiers, but is a syntax error the best way of
2402 handling this? */
2403 if (!typespec_ok || seen_type)
2404 goto out;
2405 attrs_ok = true;
2406 seen_type = true;
2407 t = c_parser_typeof_specifier (parser);
2408 declspecs_add_type (loc, specs, t);
2409 break;
2410 case RID_ATOMIC:
2411 /* C parser handling of Objective-C constructs needs
2412 checking for correct lvalue-to-rvalue conversions, and
2413 the code in build_modify_expr handling various
2414 Objective-C cases, and that in build_unary_op handling
2415 Objective-C cases for increment / decrement, also needs
2416 updating; uses of TYPE_MAIN_VARIANT in objc_compare_types
2417 and objc_types_are_equivalent may also need updates. */
2418 if (c_dialect_objc ())
2419 sorry ("%<_Atomic%> in Objective-C");
2420 /* C parser handling of OpenMP constructs needs checking for
2421 correct lvalue-to-rvalue conversions. */
2422 if (flag_openmp)
2423 sorry ("%<_Atomic%> with OpenMP");
2424 if (flag_isoc99)
2425 pedwarn_c99 (loc, OPT_Wpedantic,
2426 "ISO C99 does not support the %<_Atomic%> qualifier");
2427 else
2428 pedwarn_c99 (loc, OPT_Wpedantic,
2429 "ISO C90 does not support the %<_Atomic%> qualifier");
2430 attrs_ok = true;
2431 tree value;
2432 value = c_parser_peek_token (parser)->value;
2433 c_parser_consume_token (parser);
2434 if (typespec_ok && c_parser_next_token_is (parser, CPP_OPEN_PAREN))
2436 /* _Atomic ( type-name ). */
2437 seen_type = true;
2438 c_parser_consume_token (parser);
2439 struct c_type_name *type = c_parser_type_name (parser);
2440 t.kind = ctsk_typeof;
2441 t.spec = error_mark_node;
2442 t.expr = NULL_TREE;
2443 t.expr_const_operands = true;
2444 if (type != NULL)
2445 t.spec = groktypename (type, &t.expr,
2446 &t.expr_const_operands);
2447 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2448 "expected %<)%>");
2449 if (t.spec != error_mark_node)
2451 if (TREE_CODE (t.spec) == ARRAY_TYPE)
2452 error_at (loc, "%<_Atomic%>-qualified array type");
2453 else if (TREE_CODE (t.spec) == FUNCTION_TYPE)
2454 error_at (loc, "%<_Atomic%>-qualified function type");
2455 else if (TYPE_QUALS (t.spec) != TYPE_UNQUALIFIED)
2456 error_at (loc, "%<_Atomic%> applied to a qualified type");
2457 else
2458 t.spec = c_build_qualified_type (t.spec, TYPE_QUAL_ATOMIC);
2460 declspecs_add_type (loc, specs, t);
2462 else
2463 declspecs_add_qual (loc, specs, value);
2464 break;
2465 case RID_CONST:
2466 case RID_VOLATILE:
2467 case RID_RESTRICT:
2468 attrs_ok = true;
2469 declspecs_add_qual (loc, specs, c_parser_peek_token (parser)->value);
2470 c_parser_consume_token (parser);
2471 break;
2472 case RID_ATTRIBUTE:
2473 if (!attrs_ok)
2474 goto out;
2475 attrs = c_parser_attributes (parser);
2476 declspecs_add_attrs (loc, specs, attrs);
2477 break;
2478 case RID_ALIGNAS:
2479 if (!alignspec_ok)
2480 goto out;
2481 align = c_parser_alignas_specifier (parser);
2482 declspecs_add_alignas (loc, specs, align);
2483 break;
2484 default:
2485 goto out;
2488 out: ;
2491 /* Parse an enum specifier (C90 6.5.2.2, C99 6.7.2.2).
2493 enum-specifier:
2494 enum attributes[opt] identifier[opt] { enumerator-list } attributes[opt]
2495 enum attributes[opt] identifier[opt] { enumerator-list , } attributes[opt]
2496 enum attributes[opt] identifier
2498 The form with trailing comma is new in C99. The forms with
2499 attributes are GNU extensions. In GNU C, we accept any expression
2500 without commas in the syntax (assignment expressions, not just
2501 conditional expressions); assignment expressions will be diagnosed
2502 as non-constant.
2504 enumerator-list:
2505 enumerator
2506 enumerator-list , enumerator
2508 enumerator:
2509 enumeration-constant
2510 enumeration-constant = constant-expression
2512 GNU Extensions:
2514 enumerator:
2515 enumeration-constant attributes[opt]
2516 enumeration-constant attributes[opt] = constant-expression
2520 static struct c_typespec
2521 c_parser_enum_specifier (c_parser *parser)
2523 struct c_typespec ret;
2524 tree attrs;
2525 tree ident = NULL_TREE;
2526 location_t enum_loc;
2527 location_t ident_loc = UNKNOWN_LOCATION; /* Quiet warning. */
2528 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ENUM));
2529 enum_loc = c_parser_peek_token (parser)->location;
2530 c_parser_consume_token (parser);
2531 attrs = c_parser_attributes (parser);
2532 enum_loc = c_parser_peek_token (parser)->location;
2533 /* Set the location in case we create a decl now. */
2534 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
2535 if (c_parser_next_token_is (parser, CPP_NAME))
2537 ident = c_parser_peek_token (parser)->value;
2538 ident_loc = c_parser_peek_token (parser)->location;
2539 enum_loc = ident_loc;
2540 c_parser_consume_token (parser);
2542 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2544 /* Parse an enum definition. */
2545 struct c_enum_contents the_enum;
2546 tree type;
2547 tree postfix_attrs;
2548 /* We chain the enumerators in reverse order, then put them in
2549 forward order at the end. */
2550 tree values;
2551 timevar_push (TV_PARSE_ENUM);
2552 type = start_enum (enum_loc, &the_enum, ident);
2553 values = NULL_TREE;
2554 c_parser_consume_token (parser);
2555 while (true)
2557 tree enum_id;
2558 tree enum_value;
2559 tree enum_decl;
2560 bool seen_comma;
2561 c_token *token;
2562 location_t comma_loc = UNKNOWN_LOCATION; /* Quiet warning. */
2563 location_t decl_loc, value_loc;
2564 if (c_parser_next_token_is_not (parser, CPP_NAME))
2566 /* Give a nicer error for "enum {}". */
2567 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE)
2568 && !parser->error)
2570 error_at (c_parser_peek_token (parser)->location,
2571 "empty enum is invalid");
2572 parser->error = true;
2574 else
2575 c_parser_error (parser, "expected identifier");
2576 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2577 values = error_mark_node;
2578 break;
2580 token = c_parser_peek_token (parser);
2581 enum_id = token->value;
2582 /* Set the location in case we create a decl now. */
2583 c_parser_set_source_position_from_token (token);
2584 decl_loc = value_loc = token->location;
2585 c_parser_consume_token (parser);
2586 /* Parse any specified attributes. */
2587 tree enum_attrs = c_parser_attributes (parser);
2588 if (c_parser_next_token_is (parser, CPP_EQ))
2590 c_parser_consume_token (parser);
2591 value_loc = c_parser_peek_token (parser)->location;
2592 enum_value = c_parser_expr_no_commas (parser, NULL).value;
2594 else
2595 enum_value = NULL_TREE;
2596 enum_decl = build_enumerator (decl_loc, value_loc,
2597 &the_enum, enum_id, enum_value);
2598 if (enum_attrs)
2599 decl_attributes (&TREE_PURPOSE (enum_decl), enum_attrs, 0);
2600 TREE_CHAIN (enum_decl) = values;
2601 values = enum_decl;
2602 seen_comma = false;
2603 if (c_parser_next_token_is (parser, CPP_COMMA))
2605 comma_loc = c_parser_peek_token (parser)->location;
2606 seen_comma = true;
2607 c_parser_consume_token (parser);
2609 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2611 if (seen_comma)
2612 pedwarn_c90 (comma_loc, OPT_Wpedantic,
2613 "comma at end of enumerator list");
2614 c_parser_consume_token (parser);
2615 break;
2617 if (!seen_comma)
2619 c_parser_error (parser, "expected %<,%> or %<}%>");
2620 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2621 values = error_mark_node;
2622 break;
2625 postfix_attrs = c_parser_attributes (parser);
2626 ret.spec = finish_enum (type, nreverse (values),
2627 chainon (attrs, postfix_attrs));
2628 ret.kind = ctsk_tagdef;
2629 ret.expr = NULL_TREE;
2630 ret.expr_const_operands = true;
2631 timevar_pop (TV_PARSE_ENUM);
2632 return ret;
2634 else if (!ident)
2636 c_parser_error (parser, "expected %<{%>");
2637 ret.spec = error_mark_node;
2638 ret.kind = ctsk_tagref;
2639 ret.expr = NULL_TREE;
2640 ret.expr_const_operands = true;
2641 return ret;
2643 ret = parser_xref_tag (ident_loc, ENUMERAL_TYPE, ident);
2644 /* In ISO C, enumerated types can be referred to only if already
2645 defined. */
2646 if (pedantic && !COMPLETE_TYPE_P (ret.spec))
2648 gcc_assert (ident);
2649 pedwarn (enum_loc, OPT_Wpedantic,
2650 "ISO C forbids forward references to %<enum%> types");
2652 return ret;
2655 /* Parse a struct or union specifier (C90 6.5.2.1, C99 6.7.2.1).
2657 struct-or-union-specifier:
2658 struct-or-union attributes[opt] identifier[opt]
2659 { struct-contents } attributes[opt]
2660 struct-or-union attributes[opt] identifier
2662 struct-contents:
2663 struct-declaration-list
2665 struct-declaration-list:
2666 struct-declaration ;
2667 struct-declaration-list struct-declaration ;
2669 GNU extensions:
2671 struct-contents:
2672 empty
2673 struct-declaration
2674 struct-declaration-list struct-declaration
2676 struct-declaration-list:
2677 struct-declaration-list ;
2680 (Note that in the syntax here, unlike that in ISO C, the semicolons
2681 are included here rather than in struct-declaration, in order to
2682 describe the syntax with extra semicolons and missing semicolon at
2683 end.)
2685 Objective-C:
2687 struct-declaration-list:
2688 @defs ( class-name )
2690 (Note this does not include a trailing semicolon, but can be
2691 followed by further declarations, and gets a pedwarn-if-pedantic
2692 when followed by a semicolon.) */
2694 static struct c_typespec
2695 c_parser_struct_or_union_specifier (c_parser *parser)
2697 struct c_typespec ret;
2698 tree attrs;
2699 tree ident = NULL_TREE;
2700 location_t struct_loc;
2701 location_t ident_loc = UNKNOWN_LOCATION;
2702 enum tree_code code;
2703 switch (c_parser_peek_token (parser)->keyword)
2705 case RID_STRUCT:
2706 code = RECORD_TYPE;
2707 break;
2708 case RID_UNION:
2709 code = UNION_TYPE;
2710 break;
2711 default:
2712 gcc_unreachable ();
2714 struct_loc = c_parser_peek_token (parser)->location;
2715 c_parser_consume_token (parser);
2716 attrs = c_parser_attributes (parser);
2718 /* Set the location in case we create a decl now. */
2719 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
2721 if (c_parser_next_token_is (parser, CPP_NAME))
2723 ident = c_parser_peek_token (parser)->value;
2724 ident_loc = c_parser_peek_token (parser)->location;
2725 struct_loc = ident_loc;
2726 c_parser_consume_token (parser);
2728 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2730 /* Parse a struct or union definition. Start the scope of the
2731 tag before parsing components. */
2732 struct c_struct_parse_info *struct_info;
2733 tree type = start_struct (struct_loc, code, ident, &struct_info);
2734 tree postfix_attrs;
2735 /* We chain the components in reverse order, then put them in
2736 forward order at the end. Each struct-declaration may
2737 declare multiple components (comma-separated), so we must use
2738 chainon to join them, although when parsing each
2739 struct-declaration we can use TREE_CHAIN directly.
2741 The theory behind all this is that there will be more
2742 semicolon separated fields than comma separated fields, and
2743 so we'll be minimizing the number of node traversals required
2744 by chainon. */
2745 tree contents;
2746 timevar_push (TV_PARSE_STRUCT);
2747 contents = NULL_TREE;
2748 c_parser_consume_token (parser);
2749 /* Handle the Objective-C @defs construct,
2750 e.g. foo(sizeof(struct{ @defs(ClassName) }));. */
2751 if (c_parser_next_token_is_keyword (parser, RID_AT_DEFS))
2753 tree name;
2754 gcc_assert (c_dialect_objc ());
2755 c_parser_consume_token (parser);
2756 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2757 goto end_at_defs;
2758 if (c_parser_next_token_is (parser, CPP_NAME)
2759 && c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME)
2761 name = c_parser_peek_token (parser)->value;
2762 c_parser_consume_token (parser);
2764 else
2766 c_parser_error (parser, "expected class name");
2767 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2768 goto end_at_defs;
2770 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2771 "expected %<)%>");
2772 contents = nreverse (objc_get_class_ivars (name));
2774 end_at_defs:
2775 /* Parse the struct-declarations and semicolons. Problems with
2776 semicolons are diagnosed here; empty structures are diagnosed
2777 elsewhere. */
2778 while (true)
2780 tree decls;
2781 /* Parse any stray semicolon. */
2782 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2784 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
2785 "extra semicolon in struct or union specified");
2786 c_parser_consume_token (parser);
2787 continue;
2789 /* Stop if at the end of the struct or union contents. */
2790 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2792 c_parser_consume_token (parser);
2793 break;
2795 /* Accept #pragmas at struct scope. */
2796 if (c_parser_next_token_is (parser, CPP_PRAGMA))
2798 c_parser_pragma (parser, pragma_struct);
2799 continue;
2801 /* Parse some comma-separated declarations, but not the
2802 trailing semicolon if any. */
2803 decls = c_parser_struct_declaration (parser);
2804 contents = chainon (decls, contents);
2805 /* If no semicolon follows, either we have a parse error or
2806 are at the end of the struct or union and should
2807 pedwarn. */
2808 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2809 c_parser_consume_token (parser);
2810 else
2812 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2813 pedwarn (c_parser_peek_token (parser)->location, 0,
2814 "no semicolon at end of struct or union");
2815 else if (parser->error
2816 || !c_parser_next_token_starts_declspecs (parser))
2818 c_parser_error (parser, "expected %<;%>");
2819 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2820 break;
2823 /* If we come here, we have already emitted an error
2824 for an expected `;', identifier or `(', and we also
2825 recovered already. Go on with the next field. */
2828 postfix_attrs = c_parser_attributes (parser);
2829 ret.spec = finish_struct (struct_loc, type, nreverse (contents),
2830 chainon (attrs, postfix_attrs), struct_info);
2831 ret.kind = ctsk_tagdef;
2832 ret.expr = NULL_TREE;
2833 ret.expr_const_operands = true;
2834 timevar_pop (TV_PARSE_STRUCT);
2835 return ret;
2837 else if (!ident)
2839 c_parser_error (parser, "expected %<{%>");
2840 ret.spec = error_mark_node;
2841 ret.kind = ctsk_tagref;
2842 ret.expr = NULL_TREE;
2843 ret.expr_const_operands = true;
2844 return ret;
2846 ret = parser_xref_tag (ident_loc, code, ident);
2847 return ret;
2850 /* Parse a struct-declaration (C90 6.5.2.1, C99 6.7.2.1), *without*
2851 the trailing semicolon.
2853 struct-declaration:
2854 specifier-qualifier-list struct-declarator-list
2855 static_assert-declaration-no-semi
2857 specifier-qualifier-list:
2858 type-specifier specifier-qualifier-list[opt]
2859 type-qualifier specifier-qualifier-list[opt]
2860 attributes specifier-qualifier-list[opt]
2862 struct-declarator-list:
2863 struct-declarator
2864 struct-declarator-list , attributes[opt] struct-declarator
2866 struct-declarator:
2867 declarator attributes[opt]
2868 declarator[opt] : constant-expression attributes[opt]
2870 GNU extensions:
2872 struct-declaration:
2873 __extension__ struct-declaration
2874 specifier-qualifier-list
2876 Unlike the ISO C syntax, semicolons are handled elsewhere. The use
2877 of attributes where shown is a GNU extension. In GNU C, we accept
2878 any expression without commas in the syntax (assignment
2879 expressions, not just conditional expressions); assignment
2880 expressions will be diagnosed as non-constant. */
2882 static tree
2883 c_parser_struct_declaration (c_parser *parser)
2885 struct c_declspecs *specs;
2886 tree prefix_attrs;
2887 tree all_prefix_attrs;
2888 tree decls;
2889 location_t decl_loc;
2890 if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
2892 int ext;
2893 tree decl;
2894 ext = disable_extension_diagnostics ();
2895 c_parser_consume_token (parser);
2896 decl = c_parser_struct_declaration (parser);
2897 restore_extension_diagnostics (ext);
2898 return decl;
2900 if (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
2902 c_parser_static_assert_declaration_no_semi (parser);
2903 return NULL_TREE;
2905 specs = build_null_declspecs ();
2906 decl_loc = c_parser_peek_token (parser)->location;
2907 /* Strictly by the standard, we shouldn't allow _Alignas here,
2908 but it appears to have been intended to allow it there, so
2909 we're keeping it as it is until WG14 reaches a conclusion
2910 of N1731.
2911 <http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1731.pdf> */
2912 c_parser_declspecs (parser, specs, false, true, true,
2913 true, false, cla_nonabstract_decl);
2914 if (parser->error)
2915 return NULL_TREE;
2916 if (!specs->declspecs_seen_p)
2918 c_parser_error (parser, "expected specifier-qualifier-list");
2919 return NULL_TREE;
2921 finish_declspecs (specs);
2922 if (c_parser_next_token_is (parser, CPP_SEMICOLON)
2923 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2925 tree ret;
2926 if (specs->typespec_kind == ctsk_none)
2928 pedwarn (decl_loc, OPT_Wpedantic,
2929 "ISO C forbids member declarations with no members");
2930 shadow_tag_warned (specs, pedantic);
2931 ret = NULL_TREE;
2933 else
2935 /* Support for unnamed structs or unions as members of
2936 structs or unions (which is [a] useful and [b] supports
2937 MS P-SDK). */
2938 tree attrs = NULL;
2940 ret = grokfield (c_parser_peek_token (parser)->location,
2941 build_id_declarator (NULL_TREE), specs,
2942 NULL_TREE, &attrs);
2943 if (ret)
2944 decl_attributes (&ret, attrs, 0);
2946 return ret;
2949 /* Provide better error recovery. Note that a type name here is valid,
2950 and will be treated as a field name. */
2951 if (specs->typespec_kind == ctsk_tagdef
2952 && TREE_CODE (specs->type) != ENUMERAL_TYPE
2953 && c_parser_next_token_starts_declspecs (parser)
2954 && !c_parser_next_token_is (parser, CPP_NAME))
2956 c_parser_error (parser, "expected %<;%>, identifier or %<(%>");
2957 parser->error = false;
2958 return NULL_TREE;
2961 pending_xref_error ();
2962 prefix_attrs = specs->attrs;
2963 all_prefix_attrs = prefix_attrs;
2964 specs->attrs = NULL_TREE;
2965 decls = NULL_TREE;
2966 while (true)
2968 /* Declaring one or more declarators or un-named bit-fields. */
2969 struct c_declarator *declarator;
2970 bool dummy = false;
2971 if (c_parser_next_token_is (parser, CPP_COLON))
2972 declarator = build_id_declarator (NULL_TREE);
2973 else
2974 declarator = c_parser_declarator (parser,
2975 specs->typespec_kind != ctsk_none,
2976 C_DTR_NORMAL, &dummy);
2977 if (declarator == NULL)
2979 c_parser_skip_to_end_of_block_or_statement (parser);
2980 break;
2982 if (c_parser_next_token_is (parser, CPP_COLON)
2983 || c_parser_next_token_is (parser, CPP_COMMA)
2984 || c_parser_next_token_is (parser, CPP_SEMICOLON)
2985 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE)
2986 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2988 tree postfix_attrs = NULL_TREE;
2989 tree width = NULL_TREE;
2990 tree d;
2991 if (c_parser_next_token_is (parser, CPP_COLON))
2993 c_parser_consume_token (parser);
2994 width = c_parser_expr_no_commas (parser, NULL).value;
2996 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2997 postfix_attrs = c_parser_attributes (parser);
2998 d = grokfield (c_parser_peek_token (parser)->location,
2999 declarator, specs, width, &all_prefix_attrs);
3000 decl_attributes (&d, chainon (postfix_attrs,
3001 all_prefix_attrs), 0);
3002 DECL_CHAIN (d) = decls;
3003 decls = d;
3004 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3005 all_prefix_attrs = chainon (c_parser_attributes (parser),
3006 prefix_attrs);
3007 else
3008 all_prefix_attrs = prefix_attrs;
3009 if (c_parser_next_token_is (parser, CPP_COMMA))
3010 c_parser_consume_token (parser);
3011 else if (c_parser_next_token_is (parser, CPP_SEMICOLON)
3012 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3014 /* Semicolon consumed in caller. */
3015 break;
3017 else
3019 c_parser_error (parser, "expected %<,%>, %<;%> or %<}%>");
3020 break;
3023 else
3025 c_parser_error (parser,
3026 "expected %<:%>, %<,%>, %<;%>, %<}%> or "
3027 "%<__attribute__%>");
3028 break;
3031 return decls;
3034 /* Parse a typeof specifier (a GNU extension).
3036 typeof-specifier:
3037 typeof ( expression )
3038 typeof ( type-name )
3041 static struct c_typespec
3042 c_parser_typeof_specifier (c_parser *parser)
3044 struct c_typespec ret;
3045 ret.kind = ctsk_typeof;
3046 ret.spec = error_mark_node;
3047 ret.expr = NULL_TREE;
3048 ret.expr_const_operands = true;
3049 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TYPEOF));
3050 c_parser_consume_token (parser);
3051 c_inhibit_evaluation_warnings++;
3052 in_typeof++;
3053 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3055 c_inhibit_evaluation_warnings--;
3056 in_typeof--;
3057 return ret;
3059 if (c_parser_next_tokens_start_typename (parser, cla_prefer_id))
3061 struct c_type_name *type = c_parser_type_name (parser);
3062 c_inhibit_evaluation_warnings--;
3063 in_typeof--;
3064 if (type != NULL)
3066 ret.spec = groktypename (type, &ret.expr, &ret.expr_const_operands);
3067 pop_maybe_used (variably_modified_type_p (ret.spec, NULL_TREE));
3070 else
3072 bool was_vm;
3073 location_t here = c_parser_peek_token (parser)->location;
3074 struct c_expr expr = c_parser_expression (parser);
3075 c_inhibit_evaluation_warnings--;
3076 in_typeof--;
3077 if (TREE_CODE (expr.value) == COMPONENT_REF
3078 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
3079 error_at (here, "%<typeof%> applied to a bit-field");
3080 mark_exp_read (expr.value);
3081 ret.spec = TREE_TYPE (expr.value);
3082 was_vm = variably_modified_type_p (ret.spec, NULL_TREE);
3083 /* This is returned with the type so that when the type is
3084 evaluated, this can be evaluated. */
3085 if (was_vm)
3086 ret.expr = c_fully_fold (expr.value, false, &ret.expr_const_operands);
3087 pop_maybe_used (was_vm);
3088 /* For use in macros such as those in <stdatomic.h>, remove all
3089 qualifiers from atomic types. (const can be an issue for more macros
3090 using typeof than just the <stdatomic.h> ones.) */
3091 if (ret.spec != error_mark_node && TYPE_ATOMIC (ret.spec))
3092 ret.spec = c_build_qualified_type (ret.spec, TYPE_UNQUALIFIED);
3094 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
3095 return ret;
3098 /* Parse an alignment-specifier.
3100 C11 6.7.5:
3102 alignment-specifier:
3103 _Alignas ( type-name )
3104 _Alignas ( constant-expression )
3107 static tree
3108 c_parser_alignas_specifier (c_parser * parser)
3110 tree ret = error_mark_node;
3111 location_t loc = c_parser_peek_token (parser)->location;
3112 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNAS));
3113 c_parser_consume_token (parser);
3114 if (flag_isoc99)
3115 pedwarn_c99 (loc, OPT_Wpedantic,
3116 "ISO C99 does not support %<_Alignas%>");
3117 else
3118 pedwarn_c99 (loc, OPT_Wpedantic,
3119 "ISO C90 does not support %<_Alignas%>");
3120 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3121 return ret;
3122 if (c_parser_next_tokens_start_typename (parser, cla_prefer_id))
3124 struct c_type_name *type = c_parser_type_name (parser);
3125 if (type != NULL)
3126 ret = c_sizeof_or_alignof_type (loc, groktypename (type, NULL, NULL),
3127 false, true, 1);
3129 else
3130 ret = c_parser_expr_no_commas (parser, NULL).value;
3131 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
3132 return ret;
3135 /* Parse a declarator, possibly an abstract declarator (C90 6.5.4,
3136 6.5.5, C99 6.7.5, 6.7.6). If TYPE_SEEN_P then a typedef name may
3137 be redeclared; otherwise it may not. KIND indicates which kind of
3138 declarator is wanted. Returns a valid declarator except in the
3139 case of a syntax error in which case NULL is returned. *SEEN_ID is
3140 set to true if an identifier being declared is seen; this is used
3141 to diagnose bad forms of abstract array declarators and to
3142 determine whether an identifier list is syntactically permitted.
3144 declarator:
3145 pointer[opt] direct-declarator
3147 direct-declarator:
3148 identifier
3149 ( attributes[opt] declarator )
3150 direct-declarator array-declarator
3151 direct-declarator ( parameter-type-list )
3152 direct-declarator ( identifier-list[opt] )
3154 pointer:
3155 * type-qualifier-list[opt]
3156 * type-qualifier-list[opt] pointer
3158 type-qualifier-list:
3159 type-qualifier
3160 attributes
3161 type-qualifier-list type-qualifier
3162 type-qualifier-list attributes
3164 array-declarator:
3165 [ type-qualifier-list[opt] assignment-expression[opt] ]
3166 [ static type-qualifier-list[opt] assignment-expression ]
3167 [ type-qualifier-list static assignment-expression ]
3168 [ type-qualifier-list[opt] * ]
3170 parameter-type-list:
3171 parameter-list
3172 parameter-list , ...
3174 parameter-list:
3175 parameter-declaration
3176 parameter-list , parameter-declaration
3178 parameter-declaration:
3179 declaration-specifiers declarator attributes[opt]
3180 declaration-specifiers abstract-declarator[opt] attributes[opt]
3182 identifier-list:
3183 identifier
3184 identifier-list , identifier
3186 abstract-declarator:
3187 pointer
3188 pointer[opt] direct-abstract-declarator
3190 direct-abstract-declarator:
3191 ( attributes[opt] abstract-declarator )
3192 direct-abstract-declarator[opt] array-declarator
3193 direct-abstract-declarator[opt] ( parameter-type-list[opt] )
3195 GNU extensions:
3197 direct-declarator:
3198 direct-declarator ( parameter-forward-declarations
3199 parameter-type-list[opt] )
3201 direct-abstract-declarator:
3202 direct-abstract-declarator[opt] ( parameter-forward-declarations
3203 parameter-type-list[opt] )
3205 parameter-forward-declarations:
3206 parameter-list ;
3207 parameter-forward-declarations parameter-list ;
3209 The uses of attributes shown above are GNU extensions.
3211 Some forms of array declarator are not included in C99 in the
3212 syntax for abstract declarators; these are disallowed elsewhere.
3213 This may be a defect (DR#289).
3215 This function also accepts an omitted abstract declarator as being
3216 an abstract declarator, although not part of the formal syntax. */
3218 static struct c_declarator *
3219 c_parser_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
3220 bool *seen_id)
3222 /* Parse any initial pointer part. */
3223 if (c_parser_next_token_is (parser, CPP_MULT))
3225 struct c_declspecs *quals_attrs = build_null_declspecs ();
3226 struct c_declarator *inner;
3227 c_parser_consume_token (parser);
3228 c_parser_declspecs (parser, quals_attrs, false, false, true,
3229 false, false, cla_prefer_id);
3230 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
3231 if (inner == NULL)
3232 return NULL;
3233 else
3234 return make_pointer_declarator (quals_attrs, inner);
3236 /* Now we have a direct declarator, direct abstract declarator or
3237 nothing (which counts as a direct abstract declarator here). */
3238 return c_parser_direct_declarator (parser, type_seen_p, kind, seen_id);
3241 /* Parse a direct declarator or direct abstract declarator; arguments
3242 as c_parser_declarator. */
3244 static struct c_declarator *
3245 c_parser_direct_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
3246 bool *seen_id)
3248 /* The direct declarator must start with an identifier (possibly
3249 omitted) or a parenthesized declarator (possibly abstract). In
3250 an ordinary declarator, initial parentheses must start a
3251 parenthesized declarator. In an abstract declarator or parameter
3252 declarator, they could start a parenthesized declarator or a
3253 parameter list. To tell which, the open parenthesis and any
3254 following attributes must be read. If a declaration specifier
3255 follows, then it is a parameter list; if the specifier is a
3256 typedef name, there might be an ambiguity about redeclaring it,
3257 which is resolved in the direction of treating it as a typedef
3258 name. If a close parenthesis follows, it is also an empty
3259 parameter list, as the syntax does not permit empty abstract
3260 declarators. Otherwise, it is a parenthesized declarator (in
3261 which case the analysis may be repeated inside it, recursively).
3263 ??? There is an ambiguity in a parameter declaration "int
3264 (__attribute__((foo)) x)", where x is not a typedef name: it
3265 could be an abstract declarator for a function, or declare x with
3266 parentheses. The proper resolution of this ambiguity needs
3267 documenting. At present we follow an accident of the old
3268 parser's implementation, whereby the first parameter must have
3269 some declaration specifiers other than just attributes. Thus as
3270 a parameter declaration it is treated as a parenthesized
3271 parameter named x, and as an abstract declarator it is
3272 rejected.
3274 ??? Also following the old parser, attributes inside an empty
3275 parameter list are ignored, making it a list not yielding a
3276 prototype, rather than giving an error or making it have one
3277 parameter with implicit type int.
3279 ??? Also following the old parser, typedef names may be
3280 redeclared in declarators, but not Objective-C class names. */
3282 if (kind != C_DTR_ABSTRACT
3283 && c_parser_next_token_is (parser, CPP_NAME)
3284 && ((type_seen_p
3285 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
3286 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
3287 || c_parser_peek_token (parser)->id_kind == C_ID_ID))
3289 struct c_declarator *inner
3290 = build_id_declarator (c_parser_peek_token (parser)->value);
3291 *seen_id = true;
3292 inner->id_loc = c_parser_peek_token (parser)->location;
3293 c_parser_consume_token (parser);
3294 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3297 if (kind != C_DTR_NORMAL
3298 && c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
3300 struct c_declarator *inner = build_id_declarator (NULL_TREE);
3301 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3304 /* Either we are at the end of an abstract declarator, or we have
3305 parentheses. */
3307 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
3309 tree attrs;
3310 struct c_declarator *inner;
3311 c_parser_consume_token (parser);
3312 attrs = c_parser_attributes (parser);
3313 if (kind != C_DTR_NORMAL
3314 && (c_parser_next_token_starts_declspecs (parser)
3315 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN)))
3317 struct c_arg_info *args
3318 = c_parser_parms_declarator (parser, kind == C_DTR_NORMAL,
3319 attrs);
3320 if (args == NULL)
3321 return NULL;
3322 else
3324 inner
3325 = build_function_declarator (args,
3326 build_id_declarator (NULL_TREE));
3327 return c_parser_direct_declarator_inner (parser, *seen_id,
3328 inner);
3331 /* A parenthesized declarator. */
3332 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
3333 if (inner != NULL && attrs != NULL)
3334 inner = build_attrs_declarator (attrs, inner);
3335 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3337 c_parser_consume_token (parser);
3338 if (inner == NULL)
3339 return NULL;
3340 else
3341 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3343 else
3345 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3346 "expected %<)%>");
3347 return NULL;
3350 else
3352 if (kind == C_DTR_NORMAL)
3354 c_parser_error (parser, "expected identifier or %<(%>");
3355 return NULL;
3357 else
3358 return build_id_declarator (NULL_TREE);
3362 /* Parse part of a direct declarator or direct abstract declarator,
3363 given that some (in INNER) has already been parsed; ID_PRESENT is
3364 true if an identifier is present, false for an abstract
3365 declarator. */
3367 static struct c_declarator *
3368 c_parser_direct_declarator_inner (c_parser *parser, bool id_present,
3369 struct c_declarator *inner)
3371 /* Parse a sequence of array declarators and parameter lists. */
3372 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
3374 location_t brace_loc = c_parser_peek_token (parser)->location;
3375 struct c_declarator *declarator;
3376 struct c_declspecs *quals_attrs = build_null_declspecs ();
3377 bool static_seen;
3378 bool star_seen;
3379 struct c_expr dimen;
3380 dimen.value = NULL_TREE;
3381 dimen.original_code = ERROR_MARK;
3382 dimen.original_type = NULL_TREE;
3383 c_parser_consume_token (parser);
3384 c_parser_declspecs (parser, quals_attrs, false, false, true,
3385 false, false, cla_prefer_id);
3386 static_seen = c_parser_next_token_is_keyword (parser, RID_STATIC);
3387 if (static_seen)
3388 c_parser_consume_token (parser);
3389 if (static_seen && !quals_attrs->declspecs_seen_p)
3390 c_parser_declspecs (parser, quals_attrs, false, false, true,
3391 false, false, cla_prefer_id);
3392 if (!quals_attrs->declspecs_seen_p)
3393 quals_attrs = NULL;
3394 /* If "static" is present, there must be an array dimension.
3395 Otherwise, there may be a dimension, "*", or no
3396 dimension. */
3397 if (static_seen)
3399 star_seen = false;
3400 dimen = c_parser_expr_no_commas (parser, NULL);
3402 else
3404 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3406 dimen.value = NULL_TREE;
3407 star_seen = false;
3409 else if (flag_cilkplus
3410 && c_parser_next_token_is (parser, CPP_COLON))
3412 dimen.value = error_mark_node;
3413 star_seen = false;
3414 error_at (c_parser_peek_token (parser)->location,
3415 "array notations cannot be used in declaration");
3416 c_parser_consume_token (parser);
3418 else if (c_parser_next_token_is (parser, CPP_MULT))
3420 if (c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_SQUARE)
3422 dimen.value = NULL_TREE;
3423 star_seen = true;
3424 c_parser_consume_token (parser);
3426 else
3428 star_seen = false;
3429 dimen = c_parser_expr_no_commas (parser, NULL);
3432 else
3434 star_seen = false;
3435 dimen = c_parser_expr_no_commas (parser, NULL);
3438 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3439 c_parser_consume_token (parser);
3440 else if (flag_cilkplus
3441 && c_parser_next_token_is (parser, CPP_COLON))
3443 error_at (c_parser_peek_token (parser)->location,
3444 "array notations cannot be used in declaration");
3445 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
3446 return NULL;
3448 else
3450 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3451 "expected %<]%>");
3452 return NULL;
3454 if (dimen.value)
3455 dimen = convert_lvalue_to_rvalue (brace_loc, dimen, true, true);
3456 declarator = build_array_declarator (brace_loc, dimen.value, quals_attrs,
3457 static_seen, star_seen);
3458 if (declarator == NULL)
3459 return NULL;
3460 inner = set_array_declarator_inner (declarator, inner);
3461 return c_parser_direct_declarator_inner (parser, id_present, inner);
3463 else if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
3465 tree attrs;
3466 struct c_arg_info *args;
3467 c_parser_consume_token (parser);
3468 attrs = c_parser_attributes (parser);
3469 args = c_parser_parms_declarator (parser, id_present, attrs);
3470 if (args == NULL)
3471 return NULL;
3472 else
3474 inner = build_function_declarator (args, inner);
3475 return c_parser_direct_declarator_inner (parser, id_present, inner);
3478 return inner;
3481 /* Parse a parameter list or identifier list, including the closing
3482 parenthesis but not the opening one. ATTRS are the attributes at
3483 the start of the list. ID_LIST_OK is true if an identifier list is
3484 acceptable; such a list must not have attributes at the start. */
3486 static struct c_arg_info *
3487 c_parser_parms_declarator (c_parser *parser, bool id_list_ok, tree attrs)
3489 push_scope ();
3490 declare_parm_level ();
3491 /* If the list starts with an identifier, it is an identifier list.
3492 Otherwise, it is either a prototype list or an empty list. */
3493 if (id_list_ok
3494 && !attrs
3495 && c_parser_next_token_is (parser, CPP_NAME)
3496 && c_parser_peek_token (parser)->id_kind == C_ID_ID
3498 /* Look ahead to detect typos in type names. */
3499 && c_parser_peek_2nd_token (parser)->type != CPP_NAME
3500 && c_parser_peek_2nd_token (parser)->type != CPP_MULT
3501 && c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN
3502 && c_parser_peek_2nd_token (parser)->type != CPP_OPEN_SQUARE)
3504 tree list = NULL_TREE, *nextp = &list;
3505 while (c_parser_next_token_is (parser, CPP_NAME)
3506 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
3508 *nextp = build_tree_list (NULL_TREE,
3509 c_parser_peek_token (parser)->value);
3510 nextp = & TREE_CHAIN (*nextp);
3511 c_parser_consume_token (parser);
3512 if (c_parser_next_token_is_not (parser, CPP_COMMA))
3513 break;
3514 c_parser_consume_token (parser);
3515 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3517 c_parser_error (parser, "expected identifier");
3518 break;
3521 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3523 struct c_arg_info *ret = build_arg_info ();
3524 ret->types = list;
3525 c_parser_consume_token (parser);
3526 pop_scope ();
3527 return ret;
3529 else
3531 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3532 "expected %<)%>");
3533 pop_scope ();
3534 return NULL;
3537 else
3539 struct c_arg_info *ret = c_parser_parms_list_declarator (parser, attrs,
3540 NULL);
3541 pop_scope ();
3542 return ret;
3546 /* Parse a parameter list (possibly empty), including the closing
3547 parenthesis but not the opening one. ATTRS are the attributes at
3548 the start of the list. EXPR is NULL or an expression that needs to
3549 be evaluated for the side effects of array size expressions in the
3550 parameters. */
3552 static struct c_arg_info *
3553 c_parser_parms_list_declarator (c_parser *parser, tree attrs, tree expr)
3555 bool bad_parm = false;
3557 /* ??? Following the old parser, forward parameter declarations may
3558 use abstract declarators, and if no real parameter declarations
3559 follow the forward declarations then this is not diagnosed. Also
3560 note as above that attributes are ignored as the only contents of
3561 the parentheses, or as the only contents after forward
3562 declarations. */
3563 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3565 struct c_arg_info *ret = build_arg_info ();
3566 c_parser_consume_token (parser);
3567 return ret;
3569 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3571 struct c_arg_info *ret = build_arg_info ();
3573 if (flag_allow_parameterless_variadic_functions)
3575 /* F (...) is allowed. */
3576 ret->types = NULL_TREE;
3578 else
3580 /* Suppress -Wold-style-definition for this case. */
3581 ret->types = error_mark_node;
3582 error_at (c_parser_peek_token (parser)->location,
3583 "ISO C requires a named argument before %<...%>");
3585 c_parser_consume_token (parser);
3586 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3588 c_parser_consume_token (parser);
3589 return ret;
3591 else
3593 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3594 "expected %<)%>");
3595 return NULL;
3598 /* Nonempty list of parameters, either terminated with semicolon
3599 (forward declarations; recurse) or with close parenthesis (normal
3600 function) or with ", ... )" (variadic function). */
3601 while (true)
3603 /* Parse a parameter. */
3604 struct c_parm *parm = c_parser_parameter_declaration (parser, attrs);
3605 attrs = NULL_TREE;
3606 if (parm == NULL)
3607 bad_parm = true;
3608 else
3609 push_parm_decl (parm, &expr);
3610 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3612 tree new_attrs;
3613 c_parser_consume_token (parser);
3614 mark_forward_parm_decls ();
3615 new_attrs = c_parser_attributes (parser);
3616 return c_parser_parms_list_declarator (parser, new_attrs, expr);
3618 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3620 c_parser_consume_token (parser);
3621 if (bad_parm)
3622 return NULL;
3623 else
3624 return get_parm_info (false, expr);
3626 if (!c_parser_require (parser, CPP_COMMA,
3627 "expected %<;%>, %<,%> or %<)%>"))
3629 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3630 return NULL;
3632 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3634 c_parser_consume_token (parser);
3635 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3637 c_parser_consume_token (parser);
3638 if (bad_parm)
3639 return NULL;
3640 else
3641 return get_parm_info (true, expr);
3643 else
3645 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3646 "expected %<)%>");
3647 return NULL;
3653 /* Parse a parameter declaration. ATTRS are the attributes at the
3654 start of the declaration if it is the first parameter. */
3656 static struct c_parm *
3657 c_parser_parameter_declaration (c_parser *parser, tree attrs)
3659 struct c_declspecs *specs;
3660 struct c_declarator *declarator;
3661 tree prefix_attrs;
3662 tree postfix_attrs = NULL_TREE;
3663 bool dummy = false;
3665 /* Accept #pragmas between parameter declarations. */
3666 while (c_parser_next_token_is (parser, CPP_PRAGMA))
3667 c_parser_pragma (parser, pragma_param);
3669 if (!c_parser_next_token_starts_declspecs (parser))
3671 c_token *token = c_parser_peek_token (parser);
3672 if (parser->error)
3673 return NULL;
3674 c_parser_set_source_position_from_token (token);
3675 if (c_parser_next_tokens_start_typename (parser, cla_prefer_type))
3677 error_at (token->location, "unknown type name %qE", token->value);
3678 parser->error = true;
3680 /* ??? In some Objective-C cases '...' isn't applicable so there
3681 should be a different message. */
3682 else
3683 c_parser_error (parser,
3684 "expected declaration specifiers or %<...%>");
3685 c_parser_skip_to_end_of_parameter (parser);
3686 return NULL;
3688 specs = build_null_declspecs ();
3689 if (attrs)
3691 declspecs_add_attrs (input_location, specs, attrs);
3692 attrs = NULL_TREE;
3694 c_parser_declspecs (parser, specs, true, true, true, true, false,
3695 cla_nonabstract_decl);
3696 finish_declspecs (specs);
3697 pending_xref_error ();
3698 prefix_attrs = specs->attrs;
3699 specs->attrs = NULL_TREE;
3700 declarator = c_parser_declarator (parser,
3701 specs->typespec_kind != ctsk_none,
3702 C_DTR_PARM, &dummy);
3703 if (declarator == NULL)
3705 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
3706 return NULL;
3708 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3709 postfix_attrs = c_parser_attributes (parser);
3710 return build_c_parm (specs, chainon (postfix_attrs, prefix_attrs),
3711 declarator);
3714 /* Parse a string literal in an asm expression. It should not be
3715 translated, and wide string literals are an error although
3716 permitted by the syntax. This is a GNU extension.
3718 asm-string-literal:
3719 string-literal
3721 ??? At present, following the old parser, the caller needs to have
3722 set lex_untranslated_string to 1. It would be better to follow the
3723 C++ parser rather than using this kludge. */
3725 static tree
3726 c_parser_asm_string_literal (c_parser *parser)
3728 tree str;
3729 int save_flag = warn_overlength_strings;
3730 warn_overlength_strings = 0;
3731 if (c_parser_next_token_is (parser, CPP_STRING))
3733 str = c_parser_peek_token (parser)->value;
3734 c_parser_consume_token (parser);
3736 else if (c_parser_next_token_is (parser, CPP_WSTRING))
3738 error_at (c_parser_peek_token (parser)->location,
3739 "wide string literal in %<asm%>");
3740 str = build_string (1, "");
3741 c_parser_consume_token (parser);
3743 else
3745 c_parser_error (parser, "expected string literal");
3746 str = NULL_TREE;
3748 warn_overlength_strings = save_flag;
3749 return str;
3752 /* Parse a simple asm expression. This is used in restricted
3753 contexts, where a full expression with inputs and outputs does not
3754 make sense. This is a GNU extension.
3756 simple-asm-expr:
3757 asm ( asm-string-literal )
3760 static tree
3761 c_parser_simple_asm_expr (c_parser *parser)
3763 tree str;
3764 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
3765 /* ??? Follow the C++ parser rather than using the
3766 lex_untranslated_string kludge. */
3767 parser->lex_untranslated_string = true;
3768 c_parser_consume_token (parser);
3769 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3771 parser->lex_untranslated_string = false;
3772 return NULL_TREE;
3774 str = c_parser_asm_string_literal (parser);
3775 parser->lex_untranslated_string = false;
3776 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
3778 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3779 return NULL_TREE;
3781 return str;
3784 static tree
3785 c_parser_attribute_any_word (c_parser *parser)
3787 tree attr_name = NULL_TREE;
3789 if (c_parser_next_token_is (parser, CPP_KEYWORD))
3791 /* ??? See comment above about what keywords are accepted here. */
3792 bool ok;
3793 switch (c_parser_peek_token (parser)->keyword)
3795 case RID_STATIC:
3796 case RID_UNSIGNED:
3797 case RID_LONG:
3798 case RID_CONST:
3799 case RID_EXTERN:
3800 case RID_REGISTER:
3801 case RID_TYPEDEF:
3802 case RID_SHORT:
3803 case RID_INLINE:
3804 case RID_NORETURN:
3805 case RID_VOLATILE:
3806 case RID_SIGNED:
3807 case RID_AUTO:
3808 case RID_RESTRICT:
3809 case RID_COMPLEX:
3810 case RID_THREAD:
3811 case RID_INT:
3812 case RID_CHAR:
3813 case RID_FLOAT:
3814 case RID_DOUBLE:
3815 case RID_VOID:
3816 case RID_DFLOAT32:
3817 case RID_DFLOAT64:
3818 case RID_DFLOAT128:
3819 case RID_BOOL:
3820 case RID_FRACT:
3821 case RID_ACCUM:
3822 case RID_SAT:
3823 case RID_TRANSACTION_ATOMIC:
3824 case RID_TRANSACTION_CANCEL:
3825 case RID_ATOMIC:
3826 case RID_AUTO_TYPE:
3827 case RID_INT_N_0:
3828 case RID_INT_N_1:
3829 case RID_INT_N_2:
3830 case RID_INT_N_3:
3831 ok = true;
3832 break;
3833 default:
3834 ok = false;
3835 break;
3837 if (!ok)
3838 return NULL_TREE;
3840 /* Accept __attribute__((__const)) as __attribute__((const)) etc. */
3841 attr_name = ridpointers[(int) c_parser_peek_token (parser)->keyword];
3843 else if (c_parser_next_token_is (parser, CPP_NAME))
3844 attr_name = c_parser_peek_token (parser)->value;
3846 return attr_name;
3849 /* Returns true of NAME is an IDENTIFIER_NODE with identiifer "vector,"
3850 "__vector" or "__vector__." */
3852 static inline bool
3853 is_cilkplus_vector_p (tree name)
3855 if (flag_cilkplus && is_attribute_p ("vector", name))
3856 return true;
3857 return false;
3860 #define CILK_SIMD_FN_CLAUSE_MASK \
3861 ((OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_VECTORLENGTH) \
3862 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_LINEAR) \
3863 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_UNIFORM) \
3864 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_MASK) \
3865 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_NOMASK))
3867 /* Parses the vector attribute of SIMD enabled functions in Cilk Plus.
3868 VEC_TOKEN is the "vector" token that is replaced with "simd" and
3869 pushed into the token list.
3870 Syntax:
3871 vector
3872 vector (<vector attributes>). */
3874 static void
3875 c_parser_cilk_simd_fn_vector_attrs (c_parser *parser, c_token vec_token)
3877 gcc_assert (is_cilkplus_vector_p (vec_token.value));
3879 int paren_scope = 0;
3880 vec_safe_push (parser->cilk_simd_fn_tokens, vec_token);
3881 /* Consume the "vector" token. */
3882 c_parser_consume_token (parser);
3884 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
3886 c_parser_consume_token (parser);
3887 paren_scope++;
3889 while (paren_scope > 0)
3891 c_token *token = c_parser_peek_token (parser);
3892 if (token->type == CPP_OPEN_PAREN)
3893 paren_scope++;
3894 else if (token->type == CPP_CLOSE_PAREN)
3895 paren_scope--;
3896 /* Do not push the last ')' since we are not pushing the '('. */
3897 if (!(token->type == CPP_CLOSE_PAREN && paren_scope == 0))
3898 vec_safe_push (parser->cilk_simd_fn_tokens, *token);
3899 c_parser_consume_token (parser);
3902 /* Since we are converting an attribute to a pragma, we need to end the
3903 attribute with PRAGMA_EOL. */
3904 c_token eol_token;
3905 memset (&eol_token, 0, sizeof (eol_token));
3906 eol_token.type = CPP_PRAGMA_EOL;
3907 vec_safe_push (parser->cilk_simd_fn_tokens, eol_token);
3910 /* Add 2 CPP_EOF at the end of PARSER->ELEM_FN_TOKENS vector. */
3912 static void
3913 c_finish_cilk_simd_fn_tokens (c_parser *parser)
3915 c_token last_token = parser->cilk_simd_fn_tokens->last ();
3917 /* c_parser_attributes is called in several places, so if these EOF
3918 tokens are already inserted, then don't do them again. */
3919 if (last_token.type == CPP_EOF)
3920 return;
3922 /* Two CPP_EOF token are added as a safety net since the normal C
3923 front-end has two token look-ahead. */
3924 c_token eof_token;
3925 eof_token.type = CPP_EOF;
3926 vec_safe_push (parser->cilk_simd_fn_tokens, eof_token);
3927 vec_safe_push (parser->cilk_simd_fn_tokens, eof_token);
3930 /* Parse (possibly empty) attributes. This is a GNU extension.
3932 attributes:
3933 empty
3934 attributes attribute
3936 attribute:
3937 __attribute__ ( ( attribute-list ) )
3939 attribute-list:
3940 attrib
3941 attribute_list , attrib
3943 attrib:
3944 empty
3945 any-word
3946 any-word ( identifier )
3947 any-word ( identifier , nonempty-expr-list )
3948 any-word ( expr-list )
3950 where the "identifier" must not be declared as a type, and
3951 "any-word" may be any identifier (including one declared as a
3952 type), a reserved word storage class specifier, type specifier or
3953 type qualifier. ??? This still leaves out most reserved keywords
3954 (following the old parser), shouldn't we include them, and why not
3955 allow identifiers declared as types to start the arguments? */
3957 static tree
3958 c_parser_attributes (c_parser *parser)
3960 tree attrs = NULL_TREE;
3961 while (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3963 /* ??? Follow the C++ parser rather than using the
3964 lex_untranslated_string kludge. */
3965 parser->lex_untranslated_string = true;
3966 c_parser_consume_token (parser);
3967 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3969 parser->lex_untranslated_string = false;
3970 return attrs;
3972 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3974 parser->lex_untranslated_string = false;
3975 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3976 return attrs;
3978 /* Parse the attribute list. */
3979 while (c_parser_next_token_is (parser, CPP_COMMA)
3980 || c_parser_next_token_is (parser, CPP_NAME)
3981 || c_parser_next_token_is (parser, CPP_KEYWORD))
3983 tree attr, attr_name, attr_args;
3984 vec<tree, va_gc> *expr_list;
3985 if (c_parser_next_token_is (parser, CPP_COMMA))
3987 c_parser_consume_token (parser);
3988 continue;
3991 attr_name = c_parser_attribute_any_word (parser);
3992 if (attr_name == NULL)
3993 break;
3994 if (is_cilkplus_vector_p (attr_name))
3996 c_token *v_token = c_parser_peek_token (parser);
3997 c_parser_cilk_simd_fn_vector_attrs (parser, *v_token);
3998 continue;
4000 c_parser_consume_token (parser);
4001 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
4003 attr = build_tree_list (attr_name, NULL_TREE);
4004 attrs = chainon (attrs, attr);
4005 continue;
4007 c_parser_consume_token (parser);
4008 /* Parse the attribute contents. If they start with an
4009 identifier which is followed by a comma or close
4010 parenthesis, then the arguments start with that
4011 identifier; otherwise they are an expression list.
4012 In objective-c the identifier may be a classname. */
4013 if (c_parser_next_token_is (parser, CPP_NAME)
4014 && (c_parser_peek_token (parser)->id_kind == C_ID_ID
4015 || (c_dialect_objc ()
4016 && c_parser_peek_token (parser)->id_kind
4017 == C_ID_CLASSNAME))
4018 && ((c_parser_peek_2nd_token (parser)->type == CPP_COMMA)
4019 || (c_parser_peek_2nd_token (parser)->type
4020 == CPP_CLOSE_PAREN))
4021 && (attribute_takes_identifier_p (attr_name)
4022 || (c_dialect_objc ()
4023 && c_parser_peek_token (parser)->id_kind
4024 == C_ID_CLASSNAME)))
4026 tree arg1 = c_parser_peek_token (parser)->value;
4027 c_parser_consume_token (parser);
4028 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4029 attr_args = build_tree_list (NULL_TREE, arg1);
4030 else
4032 tree tree_list;
4033 c_parser_consume_token (parser);
4034 expr_list = c_parser_expr_list (parser, false, true,
4035 NULL, NULL, NULL, NULL);
4036 tree_list = build_tree_list_vec (expr_list);
4037 attr_args = tree_cons (NULL_TREE, arg1, tree_list);
4038 release_tree_vector (expr_list);
4041 else
4043 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4044 attr_args = NULL_TREE;
4045 else
4047 expr_list = c_parser_expr_list (parser, false, true,
4048 NULL, NULL, NULL, NULL);
4049 attr_args = build_tree_list_vec (expr_list);
4050 release_tree_vector (expr_list);
4053 attr = build_tree_list (attr_name, attr_args);
4054 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4055 c_parser_consume_token (parser);
4056 else
4058 parser->lex_untranslated_string = false;
4059 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4060 "expected %<)%>");
4061 return attrs;
4063 attrs = chainon (attrs, attr);
4065 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4066 c_parser_consume_token (parser);
4067 else
4069 parser->lex_untranslated_string = false;
4070 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4071 "expected %<)%>");
4072 return attrs;
4074 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4075 c_parser_consume_token (parser);
4076 else
4078 parser->lex_untranslated_string = false;
4079 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4080 "expected %<)%>");
4081 return attrs;
4083 parser->lex_untranslated_string = false;
4086 if (flag_cilkplus && !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
4087 c_finish_cilk_simd_fn_tokens (parser);
4088 return attrs;
4091 /* Parse a type name (C90 6.5.5, C99 6.7.6).
4093 type-name:
4094 specifier-qualifier-list abstract-declarator[opt]
4097 static struct c_type_name *
4098 c_parser_type_name (c_parser *parser)
4100 struct c_declspecs *specs = build_null_declspecs ();
4101 struct c_declarator *declarator;
4102 struct c_type_name *ret;
4103 bool dummy = false;
4104 c_parser_declspecs (parser, specs, false, true, true, false, false,
4105 cla_prefer_type);
4106 if (!specs->declspecs_seen_p)
4108 c_parser_error (parser, "expected specifier-qualifier-list");
4109 return NULL;
4111 if (specs->type != error_mark_node)
4113 pending_xref_error ();
4114 finish_declspecs (specs);
4116 declarator = c_parser_declarator (parser,
4117 specs->typespec_kind != ctsk_none,
4118 C_DTR_ABSTRACT, &dummy);
4119 if (declarator == NULL)
4120 return NULL;
4121 ret = XOBNEW (&parser_obstack, struct c_type_name);
4122 ret->specs = specs;
4123 ret->declarator = declarator;
4124 return ret;
4127 /* Parse an initializer (C90 6.5.7, C99 6.7.8).
4129 initializer:
4130 assignment-expression
4131 { initializer-list }
4132 { initializer-list , }
4134 initializer-list:
4135 designation[opt] initializer
4136 initializer-list , designation[opt] initializer
4138 designation:
4139 designator-list =
4141 designator-list:
4142 designator
4143 designator-list designator
4145 designator:
4146 array-designator
4147 . identifier
4149 array-designator:
4150 [ constant-expression ]
4152 GNU extensions:
4154 initializer:
4157 designation:
4158 array-designator
4159 identifier :
4161 array-designator:
4162 [ constant-expression ... constant-expression ]
4164 Any expression without commas is accepted in the syntax for the
4165 constant-expressions, with non-constant expressions rejected later.
4167 This function is only used for top-level initializers; for nested
4168 ones, see c_parser_initval. */
4170 static struct c_expr
4171 c_parser_initializer (c_parser *parser)
4173 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
4174 return c_parser_braced_init (parser, NULL_TREE, false);
4175 else
4177 struct c_expr ret;
4178 location_t loc = c_parser_peek_token (parser)->location;
4179 ret = c_parser_expr_no_commas (parser, NULL);
4180 if (TREE_CODE (ret.value) != STRING_CST
4181 && TREE_CODE (ret.value) != COMPOUND_LITERAL_EXPR)
4182 ret = convert_lvalue_to_rvalue (loc, ret, true, true);
4183 return ret;
4187 /* Parse a braced initializer list. TYPE is the type specified for a
4188 compound literal, and NULL_TREE for other initializers and for
4189 nested braced lists. NESTED_P is true for nested braced lists,
4190 false for the list of a compound literal or the list that is the
4191 top-level initializer in a declaration. */
4193 static struct c_expr
4194 c_parser_braced_init (c_parser *parser, tree type, bool nested_p)
4196 struct c_expr ret;
4197 struct obstack braced_init_obstack;
4198 location_t brace_loc = c_parser_peek_token (parser)->location;
4199 gcc_obstack_init (&braced_init_obstack);
4200 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
4201 c_parser_consume_token (parser);
4202 if (nested_p)
4203 push_init_level (brace_loc, 0, &braced_init_obstack);
4204 else
4205 really_start_incremental_init (type);
4206 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4208 pedwarn (brace_loc, OPT_Wpedantic, "ISO C forbids empty initializer braces");
4210 else
4212 /* Parse a non-empty initializer list, possibly with a trailing
4213 comma. */
4214 while (true)
4216 c_parser_initelt (parser, &braced_init_obstack);
4217 if (parser->error)
4218 break;
4219 if (c_parser_next_token_is (parser, CPP_COMMA))
4220 c_parser_consume_token (parser);
4221 else
4222 break;
4223 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4224 break;
4227 if (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
4229 ret.value = error_mark_node;
4230 ret.original_code = ERROR_MARK;
4231 ret.original_type = NULL;
4232 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, "expected %<}%>");
4233 pop_init_level (brace_loc, 0, &braced_init_obstack);
4234 obstack_free (&braced_init_obstack, NULL);
4235 return ret;
4237 c_parser_consume_token (parser);
4238 ret = pop_init_level (brace_loc, 0, &braced_init_obstack);
4239 obstack_free (&braced_init_obstack, NULL);
4240 return ret;
4243 /* Parse a nested initializer, including designators. */
4245 static void
4246 c_parser_initelt (c_parser *parser, struct obstack * braced_init_obstack)
4248 /* Parse any designator or designator list. A single array
4249 designator may have the subsequent "=" omitted in GNU C, but a
4250 longer list or a structure member designator may not. */
4251 if (c_parser_next_token_is (parser, CPP_NAME)
4252 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
4254 /* Old-style structure member designator. */
4255 set_init_label (c_parser_peek_token (parser)->location,
4256 c_parser_peek_token (parser)->value,
4257 braced_init_obstack);
4258 /* Use the colon as the error location. */
4259 pedwarn (c_parser_peek_2nd_token (parser)->location, OPT_Wpedantic,
4260 "obsolete use of designated initializer with %<:%>");
4261 c_parser_consume_token (parser);
4262 c_parser_consume_token (parser);
4264 else
4266 /* des_seen is 0 if there have been no designators, 1 if there
4267 has been a single array designator and 2 otherwise. */
4268 int des_seen = 0;
4269 /* Location of a designator. */
4270 location_t des_loc = UNKNOWN_LOCATION; /* Quiet warning. */
4271 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE)
4272 || c_parser_next_token_is (parser, CPP_DOT))
4274 int des_prev = des_seen;
4275 if (!des_seen)
4276 des_loc = c_parser_peek_token (parser)->location;
4277 if (des_seen < 2)
4278 des_seen++;
4279 if (c_parser_next_token_is (parser, CPP_DOT))
4281 des_seen = 2;
4282 c_parser_consume_token (parser);
4283 if (c_parser_next_token_is (parser, CPP_NAME))
4285 set_init_label (des_loc, c_parser_peek_token (parser)->value,
4286 braced_init_obstack);
4287 c_parser_consume_token (parser);
4289 else
4291 struct c_expr init;
4292 init.value = error_mark_node;
4293 init.original_code = ERROR_MARK;
4294 init.original_type = NULL;
4295 c_parser_error (parser, "expected identifier");
4296 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
4297 process_init_element (input_location, init, false,
4298 braced_init_obstack);
4299 return;
4302 else
4304 tree first, second;
4305 location_t ellipsis_loc = UNKNOWN_LOCATION; /* Quiet warning. */
4306 location_t array_index_loc = UNKNOWN_LOCATION;
4307 /* ??? Following the old parser, [ objc-receiver
4308 objc-message-args ] is accepted as an initializer,
4309 being distinguished from a designator by what follows
4310 the first assignment expression inside the square
4311 brackets, but after a first array designator a
4312 subsequent square bracket is for Objective-C taken to
4313 start an expression, using the obsolete form of
4314 designated initializer without '=', rather than
4315 possibly being a second level of designation: in LALR
4316 terms, the '[' is shifted rather than reducing
4317 designator to designator-list. */
4318 if (des_prev == 1 && c_dialect_objc ())
4320 des_seen = des_prev;
4321 break;
4323 if (des_prev == 0 && c_dialect_objc ())
4325 /* This might be an array designator or an
4326 Objective-C message expression. If the former,
4327 continue parsing here; if the latter, parse the
4328 remainder of the initializer given the starting
4329 primary-expression. ??? It might make sense to
4330 distinguish when des_prev == 1 as well; see
4331 previous comment. */
4332 tree rec, args;
4333 struct c_expr mexpr;
4334 c_parser_consume_token (parser);
4335 if (c_parser_peek_token (parser)->type == CPP_NAME
4336 && ((c_parser_peek_token (parser)->id_kind
4337 == C_ID_TYPENAME)
4338 || (c_parser_peek_token (parser)->id_kind
4339 == C_ID_CLASSNAME)))
4341 /* Type name receiver. */
4342 tree id = c_parser_peek_token (parser)->value;
4343 c_parser_consume_token (parser);
4344 rec = objc_get_class_reference (id);
4345 goto parse_message_args;
4347 first = c_parser_expr_no_commas (parser, NULL).value;
4348 mark_exp_read (first);
4349 if (c_parser_next_token_is (parser, CPP_ELLIPSIS)
4350 || c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
4351 goto array_desig_after_first;
4352 /* Expression receiver. So far only one part
4353 without commas has been parsed; there might be
4354 more of the expression. */
4355 rec = first;
4356 while (c_parser_next_token_is (parser, CPP_COMMA))
4358 struct c_expr next;
4359 location_t comma_loc, exp_loc;
4360 comma_loc = c_parser_peek_token (parser)->location;
4361 c_parser_consume_token (parser);
4362 exp_loc = c_parser_peek_token (parser)->location;
4363 next = c_parser_expr_no_commas (parser, NULL);
4364 next = convert_lvalue_to_rvalue (exp_loc, next,
4365 true, true);
4366 rec = build_compound_expr (comma_loc, rec, next.value);
4368 parse_message_args:
4369 /* Now parse the objc-message-args. */
4370 args = c_parser_objc_message_args (parser);
4371 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
4372 "expected %<]%>");
4373 mexpr.value
4374 = objc_build_message_expr (rec, args);
4375 mexpr.original_code = ERROR_MARK;
4376 mexpr.original_type = NULL;
4377 /* Now parse and process the remainder of the
4378 initializer, starting with this message
4379 expression as a primary-expression. */
4380 c_parser_initval (parser, &mexpr, braced_init_obstack);
4381 return;
4383 c_parser_consume_token (parser);
4384 array_index_loc = c_parser_peek_token (parser)->location;
4385 first = c_parser_expr_no_commas (parser, NULL).value;
4386 mark_exp_read (first);
4387 array_desig_after_first:
4388 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
4390 ellipsis_loc = c_parser_peek_token (parser)->location;
4391 c_parser_consume_token (parser);
4392 second = c_parser_expr_no_commas (parser, NULL).value;
4393 mark_exp_read (second);
4395 else
4396 second = NULL_TREE;
4397 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
4399 c_parser_consume_token (parser);
4400 set_init_index (array_index_loc, first, second,
4401 braced_init_obstack);
4402 if (second)
4403 pedwarn (ellipsis_loc, OPT_Wpedantic,
4404 "ISO C forbids specifying range of elements to initialize");
4406 else
4407 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
4408 "expected %<]%>");
4411 if (des_seen >= 1)
4413 if (c_parser_next_token_is (parser, CPP_EQ))
4415 pedwarn_c90 (des_loc, OPT_Wpedantic,
4416 "ISO C90 forbids specifying subobject "
4417 "to initialize");
4418 c_parser_consume_token (parser);
4420 else
4422 if (des_seen == 1)
4423 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
4424 "obsolete use of designated initializer without %<=%>");
4425 else
4427 struct c_expr init;
4428 init.value = error_mark_node;
4429 init.original_code = ERROR_MARK;
4430 init.original_type = NULL;
4431 c_parser_error (parser, "expected %<=%>");
4432 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
4433 process_init_element (input_location, init, false,
4434 braced_init_obstack);
4435 return;
4440 c_parser_initval (parser, NULL, braced_init_obstack);
4443 /* Parse a nested initializer; as c_parser_initializer but parses
4444 initializers within braced lists, after any designators have been
4445 applied. If AFTER is not NULL then it is an Objective-C message
4446 expression which is the primary-expression starting the
4447 initializer. */
4449 static void
4450 c_parser_initval (c_parser *parser, struct c_expr *after,
4451 struct obstack * braced_init_obstack)
4453 struct c_expr init;
4454 gcc_assert (!after || c_dialect_objc ());
4455 location_t loc = c_parser_peek_token (parser)->location;
4457 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE) && !after)
4458 init = c_parser_braced_init (parser, NULL_TREE, true);
4459 else
4461 init = c_parser_expr_no_commas (parser, after);
4462 if (init.value != NULL_TREE
4463 && TREE_CODE (init.value) != STRING_CST
4464 && TREE_CODE (init.value) != COMPOUND_LITERAL_EXPR)
4465 init = convert_lvalue_to_rvalue (loc, init, true, true);
4467 process_init_element (loc, init, false, braced_init_obstack);
4470 /* Parse a compound statement (possibly a function body) (C90 6.6.2,
4471 C99 6.8.2).
4473 compound-statement:
4474 { block-item-list[opt] }
4475 { label-declarations block-item-list }
4477 block-item-list:
4478 block-item
4479 block-item-list block-item
4481 block-item:
4482 nested-declaration
4483 statement
4485 nested-declaration:
4486 declaration
4488 GNU extensions:
4490 compound-statement:
4491 { label-declarations block-item-list }
4493 nested-declaration:
4494 __extension__ nested-declaration
4495 nested-function-definition
4497 label-declarations:
4498 label-declaration
4499 label-declarations label-declaration
4501 label-declaration:
4502 __label__ identifier-list ;
4504 Allowing the mixing of declarations and code is new in C99. The
4505 GNU syntax also permits (not shown above) labels at the end of
4506 compound statements, which yield an error. We don't allow labels
4507 on declarations; this might seem like a natural extension, but
4508 there would be a conflict between attributes on the label and
4509 prefix attributes on the declaration. ??? The syntax follows the
4510 old parser in requiring something after label declarations.
4511 Although they are erroneous if the labels declared aren't defined,
4512 is it useful for the syntax to be this way?
4514 OpenACC:
4516 block-item:
4517 openacc-directive
4519 openacc-directive:
4520 update-directive
4522 OpenMP:
4524 block-item:
4525 openmp-directive
4527 openmp-directive:
4528 barrier-directive
4529 flush-directive
4530 taskwait-directive
4531 taskyield-directive
4532 cancel-directive
4533 cancellation-point-directive */
4535 static tree
4536 c_parser_compound_statement (c_parser *parser)
4538 tree stmt;
4539 location_t brace_loc;
4540 brace_loc = c_parser_peek_token (parser)->location;
4541 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
4543 /* Ensure a scope is entered and left anyway to avoid confusion
4544 if we have just prepared to enter a function body. */
4545 stmt = c_begin_compound_stmt (true);
4546 c_end_compound_stmt (brace_loc, stmt, true);
4547 return error_mark_node;
4549 stmt = c_begin_compound_stmt (true);
4550 c_parser_compound_statement_nostart (parser);
4552 /* If the compound stmt contains array notations, then we expand them. */
4553 if (flag_cilkplus && contains_array_notation_expr (stmt))
4554 stmt = expand_array_notation_exprs (stmt);
4555 return c_end_compound_stmt (brace_loc, stmt, true);
4558 /* Parse a compound statement except for the opening brace. This is
4559 used for parsing both compound statements and statement expressions
4560 (which follow different paths to handling the opening). */
4562 static void
4563 c_parser_compound_statement_nostart (c_parser *parser)
4565 bool last_stmt = false;
4566 bool last_label = false;
4567 bool save_valid_for_pragma = valid_location_for_stdc_pragma_p ();
4568 location_t label_loc = UNKNOWN_LOCATION; /* Quiet warning. */
4569 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4571 c_parser_consume_token (parser);
4572 return;
4574 mark_valid_location_for_stdc_pragma (true);
4575 if (c_parser_next_token_is_keyword (parser, RID_LABEL))
4577 /* Read zero or more forward-declarations for labels that nested
4578 functions can jump to. */
4579 mark_valid_location_for_stdc_pragma (false);
4580 while (c_parser_next_token_is_keyword (parser, RID_LABEL))
4582 label_loc = c_parser_peek_token (parser)->location;
4583 c_parser_consume_token (parser);
4584 /* Any identifiers, including those declared as type names,
4585 are OK here. */
4586 while (true)
4588 tree label;
4589 if (c_parser_next_token_is_not (parser, CPP_NAME))
4591 c_parser_error (parser, "expected identifier");
4592 break;
4594 label
4595 = declare_label (c_parser_peek_token (parser)->value);
4596 C_DECLARED_LABEL_FLAG (label) = 1;
4597 add_stmt (build_stmt (label_loc, DECL_EXPR, label));
4598 c_parser_consume_token (parser);
4599 if (c_parser_next_token_is (parser, CPP_COMMA))
4600 c_parser_consume_token (parser);
4601 else
4602 break;
4604 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4606 pedwarn (label_loc, OPT_Wpedantic, "ISO C forbids label declarations");
4608 /* We must now have at least one statement, label or declaration. */
4609 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4611 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4612 c_parser_error (parser, "expected declaration or statement");
4613 c_parser_consume_token (parser);
4614 return;
4616 while (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
4618 location_t loc = c_parser_peek_token (parser)->location;
4619 if (c_parser_next_token_is_keyword (parser, RID_CASE)
4620 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4621 || (c_parser_next_token_is (parser, CPP_NAME)
4622 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4624 if (c_parser_next_token_is_keyword (parser, RID_CASE))
4625 label_loc = c_parser_peek_2nd_token (parser)->location;
4626 else
4627 label_loc = c_parser_peek_token (parser)->location;
4628 last_label = true;
4629 last_stmt = false;
4630 mark_valid_location_for_stdc_pragma (false);
4631 c_parser_label (parser);
4633 else if (!last_label
4634 && c_parser_next_tokens_start_declaration (parser))
4636 last_label = false;
4637 mark_valid_location_for_stdc_pragma (false);
4638 c_parser_declaration_or_fndef (parser, true, true, true, true,
4639 true, NULL, vNULL);
4640 if (last_stmt)
4641 pedwarn_c90 (loc, OPT_Wdeclaration_after_statement,
4642 "ISO C90 forbids mixed declarations and code");
4643 last_stmt = false;
4645 else if (!last_label
4646 && c_parser_next_token_is_keyword (parser, RID_EXTENSION))
4648 /* __extension__ can start a declaration, but is also an
4649 unary operator that can start an expression. Consume all
4650 but the last of a possible series of __extension__ to
4651 determine which. */
4652 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
4653 && (c_parser_peek_2nd_token (parser)->keyword
4654 == RID_EXTENSION))
4655 c_parser_consume_token (parser);
4656 if (c_token_starts_declaration (c_parser_peek_2nd_token (parser)))
4658 int ext;
4659 ext = disable_extension_diagnostics ();
4660 c_parser_consume_token (parser);
4661 last_label = false;
4662 mark_valid_location_for_stdc_pragma (false);
4663 c_parser_declaration_or_fndef (parser, true, true, true, true,
4664 true, NULL, vNULL);
4665 /* Following the old parser, __extension__ does not
4666 disable this diagnostic. */
4667 restore_extension_diagnostics (ext);
4668 if (last_stmt)
4669 pedwarn_c90 (loc, OPT_Wdeclaration_after_statement,
4670 "ISO C90 forbids mixed declarations and code");
4671 last_stmt = false;
4673 else
4674 goto statement;
4676 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
4678 /* External pragmas, and some omp pragmas, are not associated
4679 with regular c code, and so are not to be considered statements
4680 syntactically. This ensures that the user doesn't put them
4681 places that would turn into syntax errors if the directive
4682 were ignored. */
4683 if (c_parser_pragma (parser, pragma_compound))
4684 last_label = false, last_stmt = true;
4686 else if (c_parser_next_token_is (parser, CPP_EOF))
4688 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4689 c_parser_error (parser, "expected declaration or statement");
4690 return;
4692 else if (c_parser_next_token_is_keyword (parser, RID_ELSE))
4694 if (parser->in_if_block)
4696 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4697 error_at (loc, """expected %<}%> before %<else%>");
4698 return;
4700 else
4702 error_at (loc, "%<else%> without a previous %<if%>");
4703 c_parser_consume_token (parser);
4704 continue;
4707 else
4709 statement:
4710 last_label = false;
4711 last_stmt = true;
4712 mark_valid_location_for_stdc_pragma (false);
4713 c_parser_statement_after_labels (parser);
4716 parser->error = false;
4718 if (last_label)
4719 error_at (label_loc, "label at end of compound statement");
4720 c_parser_consume_token (parser);
4721 /* Restore the value we started with. */
4722 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4725 /* Parse all consecutive labels. */
4727 static void
4728 c_parser_all_labels (c_parser *parser)
4730 while (c_parser_next_token_is_keyword (parser, RID_CASE)
4731 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4732 || (c_parser_next_token_is (parser, CPP_NAME)
4733 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4734 c_parser_label (parser);
4737 /* Parse a label (C90 6.6.1, C99 6.8.1).
4739 label:
4740 identifier : attributes[opt]
4741 case constant-expression :
4742 default :
4744 GNU extensions:
4746 label:
4747 case constant-expression ... constant-expression :
4749 The use of attributes on labels is a GNU extension. The syntax in
4750 GNU C accepts any expressions without commas, non-constant
4751 expressions being rejected later. */
4753 static void
4754 c_parser_label (c_parser *parser)
4756 location_t loc1 = c_parser_peek_token (parser)->location;
4757 tree label = NULL_TREE;
4758 if (c_parser_next_token_is_keyword (parser, RID_CASE))
4760 tree exp1, exp2;
4761 c_parser_consume_token (parser);
4762 exp1 = c_parser_expr_no_commas (parser, NULL).value;
4763 if (c_parser_next_token_is (parser, CPP_COLON))
4765 c_parser_consume_token (parser);
4766 label = do_case (loc1, exp1, NULL_TREE);
4768 else if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
4770 c_parser_consume_token (parser);
4771 exp2 = c_parser_expr_no_commas (parser, NULL).value;
4772 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
4773 label = do_case (loc1, exp1, exp2);
4775 else
4776 c_parser_error (parser, "expected %<:%> or %<...%>");
4778 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
4780 c_parser_consume_token (parser);
4781 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
4782 label = do_case (loc1, NULL_TREE, NULL_TREE);
4784 else
4786 tree name = c_parser_peek_token (parser)->value;
4787 tree tlab;
4788 tree attrs;
4789 location_t loc2 = c_parser_peek_token (parser)->location;
4790 gcc_assert (c_parser_next_token_is (parser, CPP_NAME));
4791 c_parser_consume_token (parser);
4792 gcc_assert (c_parser_next_token_is (parser, CPP_COLON));
4793 c_parser_consume_token (parser);
4794 attrs = c_parser_attributes (parser);
4795 tlab = define_label (loc2, name);
4796 if (tlab)
4798 decl_attributes (&tlab, attrs, 0);
4799 label = add_stmt (build_stmt (loc1, LABEL_EXPR, tlab));
4802 if (label)
4804 if (c_parser_next_tokens_start_declaration (parser))
4806 error_at (c_parser_peek_token (parser)->location,
4807 "a label can only be part of a statement and "
4808 "a declaration is not a statement");
4809 c_parser_declaration_or_fndef (parser, /*fndef_ok*/ false,
4810 /*static_assert_ok*/ true,
4811 /*empty_ok*/ true, /*nested*/ true,
4812 /*start_attr_ok*/ true, NULL,
4813 vNULL);
4818 /* Parse a statement (C90 6.6, C99 6.8).
4820 statement:
4821 labeled-statement
4822 compound-statement
4823 expression-statement
4824 selection-statement
4825 iteration-statement
4826 jump-statement
4828 labeled-statement:
4829 label statement
4831 expression-statement:
4832 expression[opt] ;
4834 selection-statement:
4835 if-statement
4836 switch-statement
4838 iteration-statement:
4839 while-statement
4840 do-statement
4841 for-statement
4843 jump-statement:
4844 goto identifier ;
4845 continue ;
4846 break ;
4847 return expression[opt] ;
4849 GNU extensions:
4851 statement:
4852 asm-statement
4854 jump-statement:
4855 goto * expression ;
4857 Objective-C:
4859 statement:
4860 objc-throw-statement
4861 objc-try-catch-statement
4862 objc-synchronized-statement
4864 objc-throw-statement:
4865 @throw expression ;
4866 @throw ;
4868 OpenACC:
4870 statement:
4871 openacc-construct
4873 openacc-construct:
4874 parallel-construct
4875 kernels-construct
4876 data-construct
4877 loop-construct
4879 parallel-construct:
4880 parallel-directive structured-block
4882 kernels-construct:
4883 kernels-directive structured-block
4885 data-construct:
4886 data-directive structured-block
4888 loop-construct:
4889 loop-directive structured-block
4891 OpenMP:
4893 statement:
4894 openmp-construct
4896 openmp-construct:
4897 parallel-construct
4898 for-construct
4899 simd-construct
4900 for-simd-construct
4901 sections-construct
4902 single-construct
4903 parallel-for-construct
4904 parallel-for-simd-construct
4905 parallel-sections-construct
4906 master-construct
4907 critical-construct
4908 atomic-construct
4909 ordered-construct
4911 parallel-construct:
4912 parallel-directive structured-block
4914 for-construct:
4915 for-directive iteration-statement
4917 simd-construct:
4918 simd-directive iteration-statements
4920 for-simd-construct:
4921 for-simd-directive iteration-statements
4923 sections-construct:
4924 sections-directive section-scope
4926 single-construct:
4927 single-directive structured-block
4929 parallel-for-construct:
4930 parallel-for-directive iteration-statement
4932 parallel-for-simd-construct:
4933 parallel-for-simd-directive iteration-statement
4935 parallel-sections-construct:
4936 parallel-sections-directive section-scope
4938 master-construct:
4939 master-directive structured-block
4941 critical-construct:
4942 critical-directive structured-block
4944 atomic-construct:
4945 atomic-directive expression-statement
4947 ordered-construct:
4948 ordered-directive structured-block
4950 Transactional Memory:
4952 statement:
4953 transaction-statement
4954 transaction-cancel-statement
4957 static void
4958 c_parser_statement (c_parser *parser)
4960 c_parser_all_labels (parser);
4961 c_parser_statement_after_labels (parser);
4964 /* Parse a statement, other than a labeled statement. */
4966 static void
4967 c_parser_statement_after_labels (c_parser *parser)
4969 location_t loc = c_parser_peek_token (parser)->location;
4970 tree stmt = NULL_TREE;
4971 bool in_if_block = parser->in_if_block;
4972 parser->in_if_block = false;
4973 switch (c_parser_peek_token (parser)->type)
4975 case CPP_OPEN_BRACE:
4976 add_stmt (c_parser_compound_statement (parser));
4977 break;
4978 case CPP_KEYWORD:
4979 switch (c_parser_peek_token (parser)->keyword)
4981 case RID_IF:
4982 c_parser_if_statement (parser);
4983 break;
4984 case RID_SWITCH:
4985 c_parser_switch_statement (parser);
4986 break;
4987 case RID_WHILE:
4988 c_parser_while_statement (parser, false);
4989 break;
4990 case RID_DO:
4991 c_parser_do_statement (parser, false);
4992 break;
4993 case RID_FOR:
4994 c_parser_for_statement (parser, false);
4995 break;
4996 case RID_CILK_FOR:
4997 if (!flag_cilkplus)
4999 error_at (c_parser_peek_token (parser)->location,
5000 "-fcilkplus must be enabled to use %<_Cilk_for%>");
5001 c_parser_skip_to_end_of_block_or_statement (parser);
5003 else
5004 c_parser_cilk_for (parser, integer_zero_node);
5005 break;
5006 case RID_CILK_SYNC:
5007 c_parser_consume_token (parser);
5008 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5009 if (!flag_cilkplus)
5010 error_at (loc, "-fcilkplus must be enabled to use %<_Cilk_sync%>");
5011 else
5012 add_stmt (build_cilk_sync ());
5013 break;
5014 case RID_GOTO:
5015 c_parser_consume_token (parser);
5016 if (c_parser_next_token_is (parser, CPP_NAME))
5018 stmt = c_finish_goto_label (loc,
5019 c_parser_peek_token (parser)->value);
5020 c_parser_consume_token (parser);
5022 else if (c_parser_next_token_is (parser, CPP_MULT))
5024 struct c_expr val;
5026 c_parser_consume_token (parser);
5027 val = c_parser_expression (parser);
5028 if (check_no_cilk (val.value,
5029 "Cilk array notation cannot be used as a computed goto expression",
5030 "%<_Cilk_spawn%> statement cannot be used as a computed goto expression",
5031 loc))
5032 val.value = error_mark_node;
5033 val = convert_lvalue_to_rvalue (loc, val, false, true);
5034 stmt = c_finish_goto_ptr (loc, val.value);
5036 else
5037 c_parser_error (parser, "expected identifier or %<*%>");
5038 goto expect_semicolon;
5039 case RID_CONTINUE:
5040 c_parser_consume_token (parser);
5041 stmt = c_finish_bc_stmt (loc, &c_cont_label, false);
5042 goto expect_semicolon;
5043 case RID_BREAK:
5044 c_parser_consume_token (parser);
5045 stmt = c_finish_bc_stmt (loc, &c_break_label, true);
5046 goto expect_semicolon;
5047 case RID_RETURN:
5048 c_parser_consume_token (parser);
5049 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5051 stmt = c_finish_return (loc, NULL_TREE, NULL_TREE);
5052 c_parser_consume_token (parser);
5054 else
5056 location_t xloc = c_parser_peek_token (parser)->location;
5057 struct c_expr expr = c_parser_expression_conv (parser);
5058 mark_exp_read (expr.value);
5059 stmt = c_finish_return (xloc, expr.value, expr.original_type);
5060 goto expect_semicolon;
5062 break;
5063 case RID_ASM:
5064 stmt = c_parser_asm_statement (parser);
5065 break;
5066 case RID_TRANSACTION_ATOMIC:
5067 case RID_TRANSACTION_RELAXED:
5068 stmt = c_parser_transaction (parser,
5069 c_parser_peek_token (parser)->keyword);
5070 break;
5071 case RID_TRANSACTION_CANCEL:
5072 stmt = c_parser_transaction_cancel (parser);
5073 goto expect_semicolon;
5074 case RID_AT_THROW:
5075 gcc_assert (c_dialect_objc ());
5076 c_parser_consume_token (parser);
5077 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5079 stmt = objc_build_throw_stmt (loc, NULL_TREE);
5080 c_parser_consume_token (parser);
5082 else
5084 struct c_expr expr = c_parser_expression (parser);
5085 expr = convert_lvalue_to_rvalue (loc, expr, false, false);
5086 if (check_no_cilk (expr.value,
5087 "Cilk array notation cannot be used for a throw expression",
5088 "%<_Cilk_spawn%> statement cannot be used for a throw expression"))
5089 expr.value = error_mark_node;
5090 else
5092 expr.value = c_fully_fold (expr.value, false, NULL);
5093 stmt = objc_build_throw_stmt (loc, expr.value);
5095 goto expect_semicolon;
5097 break;
5098 case RID_AT_TRY:
5099 gcc_assert (c_dialect_objc ());
5100 c_parser_objc_try_catch_finally_statement (parser);
5101 break;
5102 case RID_AT_SYNCHRONIZED:
5103 gcc_assert (c_dialect_objc ());
5104 c_parser_objc_synchronized_statement (parser);
5105 break;
5106 default:
5107 goto expr_stmt;
5109 break;
5110 case CPP_SEMICOLON:
5111 c_parser_consume_token (parser);
5112 break;
5113 case CPP_CLOSE_PAREN:
5114 case CPP_CLOSE_SQUARE:
5115 /* Avoid infinite loop in error recovery:
5116 c_parser_skip_until_found stops at a closing nesting
5117 delimiter without consuming it, but here we need to consume
5118 it to proceed further. */
5119 c_parser_error (parser, "expected statement");
5120 c_parser_consume_token (parser);
5121 break;
5122 case CPP_PRAGMA:
5123 c_parser_pragma (parser, pragma_stmt);
5124 break;
5125 default:
5126 expr_stmt:
5127 stmt = c_finish_expr_stmt (loc, c_parser_expression_conv (parser).value);
5128 expect_semicolon:
5129 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5130 break;
5132 /* Two cases cannot and do not have line numbers associated: If stmt
5133 is degenerate, such as "2;", then stmt is an INTEGER_CST, which
5134 cannot hold line numbers. But that's OK because the statement
5135 will either be changed to a MODIFY_EXPR during gimplification of
5136 the statement expr, or discarded. If stmt was compound, but
5137 without new variables, we will have skipped the creation of a
5138 BIND and will have a bare STATEMENT_LIST. But that's OK because
5139 (recursively) all of the component statements should already have
5140 line numbers assigned. ??? Can we discard no-op statements
5141 earlier? */
5142 if (CAN_HAVE_LOCATION_P (stmt)
5143 && EXPR_LOCATION (stmt) == UNKNOWN_LOCATION)
5144 SET_EXPR_LOCATION (stmt, loc);
5146 parser->in_if_block = in_if_block;
5149 /* Parse the condition from an if, do, while or for statements. */
5151 static tree
5152 c_parser_condition (c_parser *parser)
5154 location_t loc = c_parser_peek_token (parser)->location;
5155 tree cond;
5156 cond = c_parser_expression_conv (parser).value;
5157 cond = c_objc_common_truthvalue_conversion (loc, cond);
5158 cond = c_fully_fold (cond, false, NULL);
5159 if (warn_sequence_point)
5160 verify_sequence_points (cond);
5161 return cond;
5164 /* Parse a parenthesized condition from an if, do or while statement.
5166 condition:
5167 ( expression )
5169 static tree
5170 c_parser_paren_condition (c_parser *parser)
5172 tree cond;
5173 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5174 return error_mark_node;
5175 cond = c_parser_condition (parser);
5176 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5177 return cond;
5180 /* Parse a statement which is a block in C99. */
5182 static tree
5183 c_parser_c99_block_statement (c_parser *parser)
5185 tree block = c_begin_compound_stmt (flag_isoc99);
5186 location_t loc = c_parser_peek_token (parser)->location;
5187 c_parser_statement (parser);
5188 return c_end_compound_stmt (loc, block, flag_isoc99);
5191 /* Parse the body of an if statement. This is just parsing a
5192 statement but (a) it is a block in C99, (b) we track whether the
5193 body is an if statement for the sake of -Wparentheses warnings, (c)
5194 we handle an empty body specially for the sake of -Wempty-body
5195 warnings, and (d) we call parser_compound_statement directly
5196 because c_parser_statement_after_labels resets
5197 parser->in_if_block. */
5199 static tree
5200 c_parser_if_body (c_parser *parser, bool *if_p,
5201 const token_indent_info &if_tinfo)
5203 tree block = c_begin_compound_stmt (flag_isoc99);
5204 location_t body_loc = c_parser_peek_token (parser)->location;
5205 token_indent_info body_tinfo
5206 = get_token_indent_info (c_parser_peek_token (parser));
5208 c_parser_all_labels (parser);
5209 *if_p = c_parser_next_token_is_keyword (parser, RID_IF);
5210 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5212 location_t loc = c_parser_peek_token (parser)->location;
5213 add_stmt (build_empty_stmt (loc));
5214 c_parser_consume_token (parser);
5215 if (!c_parser_next_token_is_keyword (parser, RID_ELSE))
5216 warning_at (loc, OPT_Wempty_body,
5217 "suggest braces around empty body in an %<if%> statement");
5219 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5220 add_stmt (c_parser_compound_statement (parser));
5221 else
5222 c_parser_statement_after_labels (parser);
5224 token_indent_info next_tinfo
5225 = get_token_indent_info (c_parser_peek_token (parser));
5226 warn_for_misleading_indentation (if_tinfo, body_tinfo, next_tinfo);
5228 return c_end_compound_stmt (body_loc, block, flag_isoc99);
5231 /* Parse the else body of an if statement. This is just parsing a
5232 statement but (a) it is a block in C99, (b) we handle an empty body
5233 specially for the sake of -Wempty-body warnings. */
5235 static tree
5236 c_parser_else_body (c_parser *parser, const token_indent_info &else_tinfo)
5238 location_t body_loc = c_parser_peek_token (parser)->location;
5239 tree block = c_begin_compound_stmt (flag_isoc99);
5240 token_indent_info body_tinfo
5241 = get_token_indent_info (c_parser_peek_token (parser));
5243 c_parser_all_labels (parser);
5244 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5246 location_t loc = c_parser_peek_token (parser)->location;
5247 warning_at (loc,
5248 OPT_Wempty_body,
5249 "suggest braces around empty body in an %<else%> statement");
5250 add_stmt (build_empty_stmt (loc));
5251 c_parser_consume_token (parser);
5253 else
5254 c_parser_statement_after_labels (parser);
5256 token_indent_info next_tinfo
5257 = get_token_indent_info (c_parser_peek_token (parser));
5258 warn_for_misleading_indentation (else_tinfo, body_tinfo, next_tinfo);
5260 return c_end_compound_stmt (body_loc, block, flag_isoc99);
5263 /* Parse an if statement (C90 6.6.4, C99 6.8.4).
5265 if-statement:
5266 if ( expression ) statement
5267 if ( expression ) statement else statement
5270 static void
5271 c_parser_if_statement (c_parser *parser)
5273 tree block;
5274 location_t loc;
5275 tree cond;
5276 bool first_if = false;
5277 tree first_body, second_body;
5278 bool in_if_block;
5279 tree if_stmt;
5281 gcc_assert (c_parser_next_token_is_keyword (parser, RID_IF));
5282 token_indent_info if_tinfo
5283 = get_token_indent_info (c_parser_peek_token (parser));
5284 c_parser_consume_token (parser);
5285 block = c_begin_compound_stmt (flag_isoc99);
5286 loc = c_parser_peek_token (parser)->location;
5287 cond = c_parser_paren_condition (parser);
5288 if (flag_cilkplus && contains_cilk_spawn_stmt (cond))
5290 error_at (loc, "if statement cannot contain %<Cilk_spawn%>");
5291 cond = error_mark_node;
5293 in_if_block = parser->in_if_block;
5294 parser->in_if_block = true;
5295 first_body = c_parser_if_body (parser, &first_if, if_tinfo);
5296 parser->in_if_block = in_if_block;
5297 if (c_parser_next_token_is_keyword (parser, RID_ELSE))
5299 token_indent_info else_tinfo
5300 = get_token_indent_info (c_parser_peek_token (parser));
5301 c_parser_consume_token (parser);
5302 second_body = c_parser_else_body (parser, else_tinfo);
5304 else
5305 second_body = NULL_TREE;
5306 c_finish_if_stmt (loc, cond, first_body, second_body, first_if);
5307 if_stmt = c_end_compound_stmt (loc, block, flag_isoc99);
5309 /* If the if statement contains array notations, then we expand them. */
5310 if (flag_cilkplus && contains_array_notation_expr (if_stmt))
5311 if_stmt = fix_conditional_array_notations (if_stmt);
5312 add_stmt (if_stmt);
5315 /* Parse a switch statement (C90 6.6.4, C99 6.8.4).
5317 switch-statement:
5318 switch (expression) statement
5321 static void
5322 c_parser_switch_statement (c_parser *parser)
5324 struct c_expr ce;
5325 tree block, expr, body, save_break;
5326 location_t switch_loc = c_parser_peek_token (parser)->location;
5327 location_t switch_cond_loc;
5328 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SWITCH));
5329 c_parser_consume_token (parser);
5330 block = c_begin_compound_stmt (flag_isoc99);
5331 bool explicit_cast_p = false;
5332 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5334 switch_cond_loc = c_parser_peek_token (parser)->location;
5335 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
5336 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
5337 explicit_cast_p = true;
5338 ce = c_parser_expression (parser);
5339 ce = convert_lvalue_to_rvalue (switch_cond_loc, ce, true, false);
5340 expr = ce.value;
5341 /* ??? expr has no valid location? */
5342 if (check_no_cilk (expr,
5343 "Cilk array notation cannot be used as a condition for switch statement",
5344 "%<_Cilk_spawn%> statement cannot be used as a condition for switch statement",
5345 switch_cond_loc))
5346 expr = error_mark_node;
5347 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5349 else
5351 switch_cond_loc = UNKNOWN_LOCATION;
5352 expr = error_mark_node;
5354 c_start_case (switch_loc, switch_cond_loc, expr, explicit_cast_p);
5355 save_break = c_break_label;
5356 c_break_label = NULL_TREE;
5357 body = c_parser_c99_block_statement (parser);
5358 c_finish_case (body, ce.original_type);
5359 if (c_break_label)
5361 location_t here = c_parser_peek_token (parser)->location;
5362 tree t = build1 (LABEL_EXPR, void_type_node, c_break_label);
5363 SET_EXPR_LOCATION (t, here);
5364 add_stmt (t);
5366 c_break_label = save_break;
5367 add_stmt (c_end_compound_stmt (switch_loc, block, flag_isoc99));
5370 /* Parse a while statement (C90 6.6.5, C99 6.8.5).
5372 while-statement:
5373 while (expression) statement
5376 static void
5377 c_parser_while_statement (c_parser *parser, bool ivdep)
5379 tree block, cond, body, save_break, save_cont;
5380 location_t loc;
5381 gcc_assert (c_parser_next_token_is_keyword (parser, RID_WHILE));
5382 token_indent_info while_tinfo
5383 = get_token_indent_info (c_parser_peek_token (parser));
5384 c_parser_consume_token (parser);
5385 block = c_begin_compound_stmt (flag_isoc99);
5386 loc = c_parser_peek_token (parser)->location;
5387 cond = c_parser_paren_condition (parser);
5388 if (check_no_cilk (cond,
5389 "Cilk array notation cannot be used as a condition for while statement",
5390 "%<_Cilk_spawn%> statement cannot be used as a condition for while statement"))
5391 cond = error_mark_node;
5392 if (ivdep && cond != error_mark_node)
5393 cond = build2 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
5394 build_int_cst (integer_type_node,
5395 annot_expr_ivdep_kind));
5396 save_break = c_break_label;
5397 c_break_label = NULL_TREE;
5398 save_cont = c_cont_label;
5399 c_cont_label = NULL_TREE;
5401 token_indent_info body_tinfo
5402 = get_token_indent_info (c_parser_peek_token (parser));
5404 body = c_parser_c99_block_statement (parser);
5406 token_indent_info next_tinfo
5407 = get_token_indent_info (c_parser_peek_token (parser));
5408 warn_for_misleading_indentation (while_tinfo, body_tinfo, next_tinfo);
5410 c_finish_loop (loc, cond, NULL, body, c_break_label, c_cont_label, true);
5411 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
5412 c_break_label = save_break;
5413 c_cont_label = save_cont;
5416 /* Parse a do statement (C90 6.6.5, C99 6.8.5).
5418 do-statement:
5419 do statement while ( expression ) ;
5422 static void
5423 c_parser_do_statement (c_parser *parser, bool ivdep)
5425 tree block, cond, body, save_break, save_cont, new_break, new_cont;
5426 location_t loc;
5427 gcc_assert (c_parser_next_token_is_keyword (parser, RID_DO));
5428 c_parser_consume_token (parser);
5429 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5430 warning_at (c_parser_peek_token (parser)->location,
5431 OPT_Wempty_body,
5432 "suggest braces around empty body in %<do%> statement");
5433 block = c_begin_compound_stmt (flag_isoc99);
5434 loc = c_parser_peek_token (parser)->location;
5435 save_break = c_break_label;
5436 c_break_label = NULL_TREE;
5437 save_cont = c_cont_label;
5438 c_cont_label = NULL_TREE;
5439 body = c_parser_c99_block_statement (parser);
5440 c_parser_require_keyword (parser, RID_WHILE, "expected %<while%>");
5441 new_break = c_break_label;
5442 c_break_label = save_break;
5443 new_cont = c_cont_label;
5444 c_cont_label = save_cont;
5445 cond = c_parser_paren_condition (parser);
5446 if (check_no_cilk (cond,
5447 "Cilk array notation cannot be used as a condition for a do-while statement",
5448 "%<_Cilk_spawn%> statement cannot be used as a condition for a do-while statement"))
5449 cond = error_mark_node;
5450 if (ivdep && cond != error_mark_node)
5451 cond = build2 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
5452 build_int_cst (integer_type_node,
5453 annot_expr_ivdep_kind));
5454 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
5455 c_parser_skip_to_end_of_block_or_statement (parser);
5456 c_finish_loop (loc, cond, NULL, body, new_break, new_cont, false);
5457 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
5460 /* Parse a for statement (C90 6.6.5, C99 6.8.5).
5462 for-statement:
5463 for ( expression[opt] ; expression[opt] ; expression[opt] ) statement
5464 for ( nested-declaration expression[opt] ; expression[opt] ) statement
5466 The form with a declaration is new in C99.
5468 ??? In accordance with the old parser, the declaration may be a
5469 nested function, which is then rejected in check_for_loop_decls,
5470 but does it make any sense for this to be included in the grammar?
5471 Note in particular that the nested function does not include a
5472 trailing ';', whereas the "declaration" production includes one.
5473 Also, can we reject bad declarations earlier and cheaper than
5474 check_for_loop_decls?
5476 In Objective-C, there are two additional variants:
5478 foreach-statement:
5479 for ( expression in expresssion ) statement
5480 for ( declaration in expression ) statement
5482 This is inconsistent with C, because the second variant is allowed
5483 even if c99 is not enabled.
5485 The rest of the comment documents these Objective-C foreach-statement.
5487 Here is the canonical example of the first variant:
5488 for (object in array) { do something with object }
5489 we call the first expression ("object") the "object_expression" and
5490 the second expression ("array") the "collection_expression".
5491 object_expression must be an lvalue of type "id" (a generic Objective-C
5492 object) because the loop works by assigning to object_expression the
5493 various objects from the collection_expression. collection_expression
5494 must evaluate to something of type "id" which responds to the method
5495 countByEnumeratingWithState:objects:count:.
5497 The canonical example of the second variant is:
5498 for (id object in array) { do something with object }
5499 which is completely equivalent to
5501 id object;
5502 for (object in array) { do something with object }
5504 Note that initizializing 'object' in some way (eg, "for ((object =
5505 xxx) in array) { do something with object }") is possibly
5506 technically valid, but completely pointless as 'object' will be
5507 assigned to something else as soon as the loop starts. We should
5508 most likely reject it (TODO).
5510 The beginning of the Objective-C foreach-statement looks exactly
5511 like the beginning of the for-statement, and we can tell it is a
5512 foreach-statement only because the initial declaration or
5513 expression is terminated by 'in' instead of ';'.
5516 static void
5517 c_parser_for_statement (c_parser *parser, bool ivdep)
5519 tree block, cond, incr, save_break, save_cont, body;
5520 /* The following are only used when parsing an ObjC foreach statement. */
5521 tree object_expression;
5522 /* Silence the bogus uninitialized warning. */
5523 tree collection_expression = NULL;
5524 location_t loc = c_parser_peek_token (parser)->location;
5525 location_t for_loc = c_parser_peek_token (parser)->location;
5526 bool is_foreach_statement = false;
5527 gcc_assert (c_parser_next_token_is_keyword (parser, RID_FOR));
5528 token_indent_info for_tinfo
5529 = get_token_indent_info (c_parser_peek_token (parser));
5530 c_parser_consume_token (parser);
5531 /* Open a compound statement in Objective-C as well, just in case this is
5532 as foreach expression. */
5533 block = c_begin_compound_stmt (flag_isoc99 || c_dialect_objc ());
5534 cond = error_mark_node;
5535 incr = error_mark_node;
5536 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5538 /* Parse the initialization declaration or expression. */
5539 object_expression = error_mark_node;
5540 parser->objc_could_be_foreach_context = c_dialect_objc ();
5541 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5543 parser->objc_could_be_foreach_context = false;
5544 c_parser_consume_token (parser);
5545 c_finish_expr_stmt (loc, NULL_TREE);
5547 else if (c_parser_next_tokens_start_declaration (parser))
5549 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
5550 &object_expression, vNULL);
5551 parser->objc_could_be_foreach_context = false;
5553 if (c_parser_next_token_is_keyword (parser, RID_IN))
5555 c_parser_consume_token (parser);
5556 is_foreach_statement = true;
5557 if (check_for_loop_decls (for_loc, true) == NULL_TREE)
5558 c_parser_error (parser, "multiple iterating variables in fast enumeration");
5560 else
5561 check_for_loop_decls (for_loc, flag_isoc99);
5563 else if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
5565 /* __extension__ can start a declaration, but is also an
5566 unary operator that can start an expression. Consume all
5567 but the last of a possible series of __extension__ to
5568 determine which. */
5569 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
5570 && (c_parser_peek_2nd_token (parser)->keyword
5571 == RID_EXTENSION))
5572 c_parser_consume_token (parser);
5573 if (c_token_starts_declaration (c_parser_peek_2nd_token (parser)))
5575 int ext;
5576 ext = disable_extension_diagnostics ();
5577 c_parser_consume_token (parser);
5578 c_parser_declaration_or_fndef (parser, true, true, true, true,
5579 true, &object_expression, vNULL);
5580 parser->objc_could_be_foreach_context = false;
5582 restore_extension_diagnostics (ext);
5583 if (c_parser_next_token_is_keyword (parser, RID_IN))
5585 c_parser_consume_token (parser);
5586 is_foreach_statement = true;
5587 if (check_for_loop_decls (for_loc, true) == NULL_TREE)
5588 c_parser_error (parser, "multiple iterating variables in fast enumeration");
5590 else
5591 check_for_loop_decls (for_loc, flag_isoc99);
5593 else
5594 goto init_expr;
5596 else
5598 init_expr:
5600 struct c_expr ce;
5601 tree init_expression;
5602 ce = c_parser_expression (parser);
5603 /* In theory we could forbid _Cilk_spawn here, as the spec says "only in top
5604 level statement", but it works just fine, so allow it. */
5605 init_expression = ce.value;
5606 parser->objc_could_be_foreach_context = false;
5607 if (c_parser_next_token_is_keyword (parser, RID_IN))
5609 c_parser_consume_token (parser);
5610 is_foreach_statement = true;
5611 if (! lvalue_p (init_expression))
5612 c_parser_error (parser, "invalid iterating variable in fast enumeration");
5613 object_expression = c_fully_fold (init_expression, false, NULL);
5615 else
5617 ce = convert_lvalue_to_rvalue (loc, ce, true, false);
5618 init_expression = ce.value;
5619 c_finish_expr_stmt (loc, init_expression);
5620 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5624 /* Parse the loop condition. In the case of a foreach
5625 statement, there is no loop condition. */
5626 gcc_assert (!parser->objc_could_be_foreach_context);
5627 if (!is_foreach_statement)
5629 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5631 if (ivdep)
5633 c_parser_error (parser, "missing loop condition in loop with "
5634 "%<GCC ivdep%> pragma");
5635 cond = error_mark_node;
5637 else
5639 c_parser_consume_token (parser);
5640 cond = NULL_TREE;
5643 else
5645 cond = c_parser_condition (parser);
5646 if (check_no_cilk (cond,
5647 "Cilk array notation cannot be used in a condition for a for-loop",
5648 "%<_Cilk_spawn%> statement cannot be used in a condition for a for-loop"))
5649 cond = error_mark_node;
5650 c_parser_skip_until_found (parser, CPP_SEMICOLON,
5651 "expected %<;%>");
5653 if (ivdep && cond != error_mark_node)
5654 cond = build2 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
5655 build_int_cst (integer_type_node,
5656 annot_expr_ivdep_kind));
5658 /* Parse the increment expression (the third expression in a
5659 for-statement). In the case of a foreach-statement, this is
5660 the expression that follows the 'in'. */
5661 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
5663 if (is_foreach_statement)
5665 c_parser_error (parser, "missing collection in fast enumeration");
5666 collection_expression = error_mark_node;
5668 else
5669 incr = c_process_expr_stmt (loc, NULL_TREE);
5671 else
5673 if (is_foreach_statement)
5674 collection_expression = c_fully_fold (c_parser_expression (parser).value,
5675 false, NULL);
5676 else
5678 struct c_expr ce = c_parser_expression (parser);
5679 ce = convert_lvalue_to_rvalue (loc, ce, true, false);
5680 incr = c_process_expr_stmt (loc, ce.value);
5683 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5685 save_break = c_break_label;
5686 c_break_label = NULL_TREE;
5687 save_cont = c_cont_label;
5688 c_cont_label = NULL_TREE;
5690 token_indent_info body_tinfo
5691 = get_token_indent_info (c_parser_peek_token (parser));
5693 body = c_parser_c99_block_statement (parser);
5695 token_indent_info next_tinfo
5696 = get_token_indent_info (c_parser_peek_token (parser));
5697 warn_for_misleading_indentation (for_tinfo, body_tinfo, next_tinfo);
5699 if (is_foreach_statement)
5700 objc_finish_foreach_loop (loc, object_expression, collection_expression, body, c_break_label, c_cont_label);
5701 else
5702 c_finish_loop (loc, cond, incr, body, c_break_label, c_cont_label, true);
5703 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99 || c_dialect_objc ()));
5704 c_break_label = save_break;
5705 c_cont_label = save_cont;
5708 /* Parse an asm statement, a GNU extension. This is a full-blown asm
5709 statement with inputs, outputs, clobbers, and volatile tag
5710 allowed.
5712 asm-statement:
5713 asm type-qualifier[opt] ( asm-argument ) ;
5714 asm type-qualifier[opt] goto ( asm-goto-argument ) ;
5716 asm-argument:
5717 asm-string-literal
5718 asm-string-literal : asm-operands[opt]
5719 asm-string-literal : asm-operands[opt] : asm-operands[opt]
5720 asm-string-literal : asm-operands[opt] : asm-operands[opt] : asm-clobbers[opt]
5722 asm-goto-argument:
5723 asm-string-literal : : asm-operands[opt] : asm-clobbers[opt] \
5724 : asm-goto-operands
5726 Qualifiers other than volatile are accepted in the syntax but
5727 warned for. */
5729 static tree
5730 c_parser_asm_statement (c_parser *parser)
5732 tree quals, str, outputs, inputs, clobbers, labels, ret;
5733 bool simple, is_goto;
5734 location_t asm_loc = c_parser_peek_token (parser)->location;
5735 int section, nsections;
5737 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
5738 c_parser_consume_token (parser);
5739 if (c_parser_next_token_is_keyword (parser, RID_VOLATILE))
5741 quals = c_parser_peek_token (parser)->value;
5742 c_parser_consume_token (parser);
5744 else if (c_parser_next_token_is_keyword (parser, RID_CONST)
5745 || c_parser_next_token_is_keyword (parser, RID_RESTRICT))
5747 warning_at (c_parser_peek_token (parser)->location,
5749 "%E qualifier ignored on asm",
5750 c_parser_peek_token (parser)->value);
5751 quals = NULL_TREE;
5752 c_parser_consume_token (parser);
5754 else
5755 quals = NULL_TREE;
5757 is_goto = false;
5758 if (c_parser_next_token_is_keyword (parser, RID_GOTO))
5760 c_parser_consume_token (parser);
5761 is_goto = true;
5764 /* ??? Follow the C++ parser rather than using the
5765 lex_untranslated_string kludge. */
5766 parser->lex_untranslated_string = true;
5767 ret = NULL;
5769 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5770 goto error;
5772 str = c_parser_asm_string_literal (parser);
5773 if (str == NULL_TREE)
5774 goto error_close_paren;
5776 simple = true;
5777 outputs = NULL_TREE;
5778 inputs = NULL_TREE;
5779 clobbers = NULL_TREE;
5780 labels = NULL_TREE;
5782 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
5783 goto done_asm;
5785 /* Parse each colon-delimited section of operands. */
5786 nsections = 3 + is_goto;
5787 for (section = 0; section < nsections; ++section)
5789 if (!c_parser_require (parser, CPP_COLON,
5790 is_goto
5791 ? "expected %<:%>"
5792 : "expected %<:%> or %<)%>"))
5793 goto error_close_paren;
5795 /* Once past any colon, we're no longer a simple asm. */
5796 simple = false;
5798 if ((!c_parser_next_token_is (parser, CPP_COLON)
5799 && !c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
5800 || section == 3)
5801 switch (section)
5803 case 0:
5804 /* For asm goto, we don't allow output operands, but reserve
5805 the slot for a future extension that does allow them. */
5806 if (!is_goto)
5807 outputs = c_parser_asm_operands (parser);
5808 break;
5809 case 1:
5810 inputs = c_parser_asm_operands (parser);
5811 break;
5812 case 2:
5813 clobbers = c_parser_asm_clobbers (parser);
5814 break;
5815 case 3:
5816 labels = c_parser_asm_goto_operands (parser);
5817 break;
5818 default:
5819 gcc_unreachable ();
5822 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
5823 goto done_asm;
5826 done_asm:
5827 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
5829 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5830 goto error;
5833 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
5834 c_parser_skip_to_end_of_block_or_statement (parser);
5836 ret = build_asm_stmt (quals, build_asm_expr (asm_loc, str, outputs, inputs,
5837 clobbers, labels, simple));
5839 error:
5840 parser->lex_untranslated_string = false;
5841 return ret;
5843 error_close_paren:
5844 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5845 goto error;
5848 /* Parse asm operands, a GNU extension.
5850 asm-operands:
5851 asm-operand
5852 asm-operands , asm-operand
5854 asm-operand:
5855 asm-string-literal ( expression )
5856 [ identifier ] asm-string-literal ( expression )
5859 static tree
5860 c_parser_asm_operands (c_parser *parser)
5862 tree list = NULL_TREE;
5863 while (true)
5865 tree name, str;
5866 struct c_expr expr;
5867 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
5869 c_parser_consume_token (parser);
5870 if (c_parser_next_token_is (parser, CPP_NAME))
5872 tree id = c_parser_peek_token (parser)->value;
5873 c_parser_consume_token (parser);
5874 name = build_string (IDENTIFIER_LENGTH (id),
5875 IDENTIFIER_POINTER (id));
5877 else
5879 c_parser_error (parser, "expected identifier");
5880 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
5881 return NULL_TREE;
5883 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
5884 "expected %<]%>");
5886 else
5887 name = NULL_TREE;
5888 str = c_parser_asm_string_literal (parser);
5889 if (str == NULL_TREE)
5890 return NULL_TREE;
5891 parser->lex_untranslated_string = false;
5892 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5894 parser->lex_untranslated_string = true;
5895 return NULL_TREE;
5897 expr = c_parser_expression (parser);
5898 mark_exp_read (expr.value);
5899 parser->lex_untranslated_string = true;
5900 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
5902 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5903 return NULL_TREE;
5905 list = chainon (list, build_tree_list (build_tree_list (name, str),
5906 expr.value));
5907 if (c_parser_next_token_is (parser, CPP_COMMA))
5908 c_parser_consume_token (parser);
5909 else
5910 break;
5912 return list;
5915 /* Parse asm clobbers, a GNU extension.
5917 asm-clobbers:
5918 asm-string-literal
5919 asm-clobbers , asm-string-literal
5922 static tree
5923 c_parser_asm_clobbers (c_parser *parser)
5925 tree list = NULL_TREE;
5926 while (true)
5928 tree str = c_parser_asm_string_literal (parser);
5929 if (str)
5930 list = tree_cons (NULL_TREE, str, list);
5931 else
5932 return NULL_TREE;
5933 if (c_parser_next_token_is (parser, CPP_COMMA))
5934 c_parser_consume_token (parser);
5935 else
5936 break;
5938 return list;
5941 /* Parse asm goto labels, a GNU extension.
5943 asm-goto-operands:
5944 identifier
5945 asm-goto-operands , identifier
5948 static tree
5949 c_parser_asm_goto_operands (c_parser *parser)
5951 tree list = NULL_TREE;
5952 while (true)
5954 tree name, label;
5956 if (c_parser_next_token_is (parser, CPP_NAME))
5958 c_token *tok = c_parser_peek_token (parser);
5959 name = tok->value;
5960 label = lookup_label_for_goto (tok->location, name);
5961 c_parser_consume_token (parser);
5962 TREE_USED (label) = 1;
5964 else
5966 c_parser_error (parser, "expected identifier");
5967 return NULL_TREE;
5970 name = build_string (IDENTIFIER_LENGTH (name),
5971 IDENTIFIER_POINTER (name));
5972 list = tree_cons (name, label, list);
5973 if (c_parser_next_token_is (parser, CPP_COMMA))
5974 c_parser_consume_token (parser);
5975 else
5976 return nreverse (list);
5980 /* Parse an expression other than a compound expression; that is, an
5981 assignment expression (C90 6.3.16, C99 6.5.16). If AFTER is not
5982 NULL then it is an Objective-C message expression which is the
5983 primary-expression starting the expression as an initializer.
5985 assignment-expression:
5986 conditional-expression
5987 unary-expression assignment-operator assignment-expression
5989 assignment-operator: one of
5990 = *= /= %= += -= <<= >>= &= ^= |=
5992 In GNU C we accept any conditional expression on the LHS and
5993 diagnose the invalid lvalue rather than producing a syntax
5994 error. */
5996 static struct c_expr
5997 c_parser_expr_no_commas (c_parser *parser, struct c_expr *after,
5998 tree omp_atomic_lhs)
6000 struct c_expr lhs, rhs, ret;
6001 enum tree_code code;
6002 location_t op_location, exp_location;
6003 gcc_assert (!after || c_dialect_objc ());
6004 lhs = c_parser_conditional_expression (parser, after, omp_atomic_lhs);
6005 op_location = c_parser_peek_token (parser)->location;
6006 switch (c_parser_peek_token (parser)->type)
6008 case CPP_EQ:
6009 code = NOP_EXPR;
6010 break;
6011 case CPP_MULT_EQ:
6012 code = MULT_EXPR;
6013 break;
6014 case CPP_DIV_EQ:
6015 code = TRUNC_DIV_EXPR;
6016 break;
6017 case CPP_MOD_EQ:
6018 code = TRUNC_MOD_EXPR;
6019 break;
6020 case CPP_PLUS_EQ:
6021 code = PLUS_EXPR;
6022 break;
6023 case CPP_MINUS_EQ:
6024 code = MINUS_EXPR;
6025 break;
6026 case CPP_LSHIFT_EQ:
6027 code = LSHIFT_EXPR;
6028 break;
6029 case CPP_RSHIFT_EQ:
6030 code = RSHIFT_EXPR;
6031 break;
6032 case CPP_AND_EQ:
6033 code = BIT_AND_EXPR;
6034 break;
6035 case CPP_XOR_EQ:
6036 code = BIT_XOR_EXPR;
6037 break;
6038 case CPP_OR_EQ:
6039 code = BIT_IOR_EXPR;
6040 break;
6041 default:
6042 return lhs;
6044 c_parser_consume_token (parser);
6045 exp_location = c_parser_peek_token (parser)->location;
6046 rhs = c_parser_expr_no_commas (parser, NULL);
6047 rhs = convert_lvalue_to_rvalue (exp_location, rhs, true, true);
6049 ret.value = build_modify_expr (op_location, lhs.value, lhs.original_type,
6050 code, exp_location, rhs.value,
6051 rhs.original_type);
6052 if (code == NOP_EXPR)
6053 ret.original_code = MODIFY_EXPR;
6054 else
6056 TREE_NO_WARNING (ret.value) = 1;
6057 ret.original_code = ERROR_MARK;
6059 ret.original_type = NULL;
6060 return ret;
6063 /* Parse a conditional expression (C90 6.3.15, C99 6.5.15). If AFTER
6064 is not NULL then it is an Objective-C message expression which is
6065 the primary-expression starting the expression as an initializer.
6067 conditional-expression:
6068 logical-OR-expression
6069 logical-OR-expression ? expression : conditional-expression
6071 GNU extensions:
6073 conditional-expression:
6074 logical-OR-expression ? : conditional-expression
6077 static struct c_expr
6078 c_parser_conditional_expression (c_parser *parser, struct c_expr *after,
6079 tree omp_atomic_lhs)
6081 struct c_expr cond, exp1, exp2, ret;
6082 location_t cond_loc, colon_loc, middle_loc;
6084 gcc_assert (!after || c_dialect_objc ());
6086 cond = c_parser_binary_expression (parser, after, omp_atomic_lhs);
6088 if (c_parser_next_token_is_not (parser, CPP_QUERY))
6089 return cond;
6090 cond_loc = c_parser_peek_token (parser)->location;
6091 cond = convert_lvalue_to_rvalue (cond_loc, cond, true, true);
6092 c_parser_consume_token (parser);
6093 if (c_parser_next_token_is (parser, CPP_COLON))
6095 tree eptype = NULL_TREE;
6097 middle_loc = c_parser_peek_token (parser)->location;
6098 pedwarn (middle_loc, OPT_Wpedantic,
6099 "ISO C forbids omitting the middle term of a ?: expression");
6100 warn_for_omitted_condop (middle_loc, cond.value);
6101 if (TREE_CODE (cond.value) == EXCESS_PRECISION_EXPR)
6103 eptype = TREE_TYPE (cond.value);
6104 cond.value = TREE_OPERAND (cond.value, 0);
6106 /* Make sure first operand is calculated only once. */
6107 exp1.value = c_save_expr (default_conversion (cond.value));
6108 if (eptype)
6109 exp1.value = build1 (EXCESS_PRECISION_EXPR, eptype, exp1.value);
6110 exp1.original_type = NULL;
6111 cond.value = c_objc_common_truthvalue_conversion (cond_loc, exp1.value);
6112 c_inhibit_evaluation_warnings += cond.value == truthvalue_true_node;
6114 else
6116 cond.value
6117 = c_objc_common_truthvalue_conversion
6118 (cond_loc, default_conversion (cond.value));
6119 c_inhibit_evaluation_warnings += cond.value == truthvalue_false_node;
6120 exp1 = c_parser_expression_conv (parser);
6121 mark_exp_read (exp1.value);
6122 c_inhibit_evaluation_warnings +=
6123 ((cond.value == truthvalue_true_node)
6124 - (cond.value == truthvalue_false_node));
6127 colon_loc = c_parser_peek_token (parser)->location;
6128 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
6130 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
6131 ret.value = error_mark_node;
6132 ret.original_code = ERROR_MARK;
6133 ret.original_type = NULL;
6134 return ret;
6137 location_t exp2_loc = c_parser_peek_token (parser)->location;
6138 exp2 = c_parser_conditional_expression (parser, NULL, NULL_TREE);
6139 exp2 = convert_lvalue_to_rvalue (exp2_loc, exp2, true, true);
6141 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
6142 ret.value = build_conditional_expr (colon_loc, cond.value,
6143 cond.original_code == C_MAYBE_CONST_EXPR,
6144 exp1.value, exp1.original_type,
6145 exp2.value, exp2.original_type);
6146 ret.original_code = ERROR_MARK;
6147 if (exp1.value == error_mark_node || exp2.value == error_mark_node)
6148 ret.original_type = NULL;
6149 else
6151 tree t1, t2;
6153 /* If both sides are enum type, the default conversion will have
6154 made the type of the result be an integer type. We want to
6155 remember the enum types we started with. */
6156 t1 = exp1.original_type ? exp1.original_type : TREE_TYPE (exp1.value);
6157 t2 = exp2.original_type ? exp2.original_type : TREE_TYPE (exp2.value);
6158 ret.original_type = ((t1 != error_mark_node
6159 && t2 != error_mark_node
6160 && (TYPE_MAIN_VARIANT (t1)
6161 == TYPE_MAIN_VARIANT (t2)))
6162 ? t1
6163 : NULL);
6165 return ret;
6168 /* Parse a binary expression; that is, a logical-OR-expression (C90
6169 6.3.5-6.3.14, C99 6.5.5-6.5.14). If AFTER is not NULL then it is
6170 an Objective-C message expression which is the primary-expression
6171 starting the expression as an initializer.
6173 OMP_ATOMIC_LHS is NULL, unless parsing OpenMP #pragma omp atomic,
6174 when it should be the unfolded lhs. In a valid OpenMP source,
6175 one of the operands of the toplevel binary expression must be equal
6176 to it. In that case, just return a build2 created binary operation
6177 rather than result of parser_build_binary_op.
6179 multiplicative-expression:
6180 cast-expression
6181 multiplicative-expression * cast-expression
6182 multiplicative-expression / cast-expression
6183 multiplicative-expression % cast-expression
6185 additive-expression:
6186 multiplicative-expression
6187 additive-expression + multiplicative-expression
6188 additive-expression - multiplicative-expression
6190 shift-expression:
6191 additive-expression
6192 shift-expression << additive-expression
6193 shift-expression >> additive-expression
6195 relational-expression:
6196 shift-expression
6197 relational-expression < shift-expression
6198 relational-expression > shift-expression
6199 relational-expression <= shift-expression
6200 relational-expression >= shift-expression
6202 equality-expression:
6203 relational-expression
6204 equality-expression == relational-expression
6205 equality-expression != relational-expression
6207 AND-expression:
6208 equality-expression
6209 AND-expression & equality-expression
6211 exclusive-OR-expression:
6212 AND-expression
6213 exclusive-OR-expression ^ AND-expression
6215 inclusive-OR-expression:
6216 exclusive-OR-expression
6217 inclusive-OR-expression | exclusive-OR-expression
6219 logical-AND-expression:
6220 inclusive-OR-expression
6221 logical-AND-expression && inclusive-OR-expression
6223 logical-OR-expression:
6224 logical-AND-expression
6225 logical-OR-expression || logical-AND-expression
6228 static struct c_expr
6229 c_parser_binary_expression (c_parser *parser, struct c_expr *after,
6230 tree omp_atomic_lhs)
6232 /* A binary expression is parsed using operator-precedence parsing,
6233 with the operands being cast expressions. All the binary
6234 operators are left-associative. Thus a binary expression is of
6235 form:
6237 E0 op1 E1 op2 E2 ...
6239 which we represent on a stack. On the stack, the precedence
6240 levels are strictly increasing. When a new operator is
6241 encountered of higher precedence than that at the top of the
6242 stack, it is pushed; its LHS is the top expression, and its RHS
6243 is everything parsed until it is popped. When a new operator is
6244 encountered with precedence less than or equal to that at the top
6245 of the stack, triples E[i-1] op[i] E[i] are popped and replaced
6246 by the result of the operation until the operator at the top of
6247 the stack has lower precedence than the new operator or there is
6248 only one element on the stack; then the top expression is the LHS
6249 of the new operator. In the case of logical AND and OR
6250 expressions, we also need to adjust c_inhibit_evaluation_warnings
6251 as appropriate when the operators are pushed and popped. */
6253 struct {
6254 /* The expression at this stack level. */
6255 struct c_expr expr;
6256 /* The precedence of the operator on its left, PREC_NONE at the
6257 bottom of the stack. */
6258 enum c_parser_prec prec;
6259 /* The operation on its left. */
6260 enum tree_code op;
6261 /* The source location of this operation. */
6262 location_t loc;
6263 } stack[NUM_PRECS];
6264 int sp;
6265 /* Location of the binary operator. */
6266 location_t binary_loc = UNKNOWN_LOCATION; /* Quiet warning. */
6267 #define POP \
6268 do { \
6269 switch (stack[sp].op) \
6271 case TRUTH_ANDIF_EXPR: \
6272 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
6273 == truthvalue_false_node); \
6274 break; \
6275 case TRUTH_ORIF_EXPR: \
6276 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
6277 == truthvalue_true_node); \
6278 break; \
6279 default: \
6280 break; \
6282 stack[sp - 1].expr \
6283 = convert_lvalue_to_rvalue (stack[sp - 1].loc, \
6284 stack[sp - 1].expr, true, true); \
6285 stack[sp].expr \
6286 = convert_lvalue_to_rvalue (stack[sp].loc, \
6287 stack[sp].expr, true, true); \
6288 if (__builtin_expect (omp_atomic_lhs != NULL_TREE, 0) && sp == 1 \
6289 && c_parser_peek_token (parser)->type == CPP_SEMICOLON \
6290 && ((1 << stack[sp].prec) \
6291 & ((1 << PREC_BITOR) | (1 << PREC_BITXOR) | (1 << PREC_BITAND) \
6292 | (1 << PREC_SHIFT) | (1 << PREC_ADD) | (1 << PREC_MULT))) \
6293 && stack[sp].op != TRUNC_MOD_EXPR \
6294 && stack[0].expr.value != error_mark_node \
6295 && stack[1].expr.value != error_mark_node \
6296 && (c_tree_equal (stack[0].expr.value, omp_atomic_lhs) \
6297 || c_tree_equal (stack[1].expr.value, omp_atomic_lhs))) \
6298 stack[0].expr.value \
6299 = build2 (stack[1].op, TREE_TYPE (stack[0].expr.value), \
6300 stack[0].expr.value, stack[1].expr.value); \
6301 else \
6302 stack[sp - 1].expr = parser_build_binary_op (stack[sp].loc, \
6303 stack[sp].op, \
6304 stack[sp - 1].expr, \
6305 stack[sp].expr); \
6306 sp--; \
6307 } while (0)
6308 gcc_assert (!after || c_dialect_objc ());
6309 stack[0].loc = c_parser_peek_token (parser)->location;
6310 stack[0].expr = c_parser_cast_expression (parser, after);
6311 stack[0].prec = PREC_NONE;
6312 sp = 0;
6313 while (true)
6315 enum c_parser_prec oprec;
6316 enum tree_code ocode;
6317 if (parser->error)
6318 goto out;
6319 switch (c_parser_peek_token (parser)->type)
6321 case CPP_MULT:
6322 oprec = PREC_MULT;
6323 ocode = MULT_EXPR;
6324 break;
6325 case CPP_DIV:
6326 oprec = PREC_MULT;
6327 ocode = TRUNC_DIV_EXPR;
6328 break;
6329 case CPP_MOD:
6330 oprec = PREC_MULT;
6331 ocode = TRUNC_MOD_EXPR;
6332 break;
6333 case CPP_PLUS:
6334 oprec = PREC_ADD;
6335 ocode = PLUS_EXPR;
6336 break;
6337 case CPP_MINUS:
6338 oprec = PREC_ADD;
6339 ocode = MINUS_EXPR;
6340 break;
6341 case CPP_LSHIFT:
6342 oprec = PREC_SHIFT;
6343 ocode = LSHIFT_EXPR;
6344 break;
6345 case CPP_RSHIFT:
6346 oprec = PREC_SHIFT;
6347 ocode = RSHIFT_EXPR;
6348 break;
6349 case CPP_LESS:
6350 oprec = PREC_REL;
6351 ocode = LT_EXPR;
6352 break;
6353 case CPP_GREATER:
6354 oprec = PREC_REL;
6355 ocode = GT_EXPR;
6356 break;
6357 case CPP_LESS_EQ:
6358 oprec = PREC_REL;
6359 ocode = LE_EXPR;
6360 break;
6361 case CPP_GREATER_EQ:
6362 oprec = PREC_REL;
6363 ocode = GE_EXPR;
6364 break;
6365 case CPP_EQ_EQ:
6366 oprec = PREC_EQ;
6367 ocode = EQ_EXPR;
6368 break;
6369 case CPP_NOT_EQ:
6370 oprec = PREC_EQ;
6371 ocode = NE_EXPR;
6372 break;
6373 case CPP_AND:
6374 oprec = PREC_BITAND;
6375 ocode = BIT_AND_EXPR;
6376 break;
6377 case CPP_XOR:
6378 oprec = PREC_BITXOR;
6379 ocode = BIT_XOR_EXPR;
6380 break;
6381 case CPP_OR:
6382 oprec = PREC_BITOR;
6383 ocode = BIT_IOR_EXPR;
6384 break;
6385 case CPP_AND_AND:
6386 oprec = PREC_LOGAND;
6387 ocode = TRUTH_ANDIF_EXPR;
6388 break;
6389 case CPP_OR_OR:
6390 oprec = PREC_LOGOR;
6391 ocode = TRUTH_ORIF_EXPR;
6392 break;
6393 default:
6394 /* Not a binary operator, so end of the binary
6395 expression. */
6396 goto out;
6398 binary_loc = c_parser_peek_token (parser)->location;
6399 while (oprec <= stack[sp].prec)
6400 POP;
6401 c_parser_consume_token (parser);
6402 switch (ocode)
6404 case TRUTH_ANDIF_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_false_node);
6412 break;
6413 case TRUTH_ORIF_EXPR:
6414 stack[sp].expr
6415 = convert_lvalue_to_rvalue (stack[sp].loc,
6416 stack[sp].expr, true, true);
6417 stack[sp].expr.value = c_objc_common_truthvalue_conversion
6418 (stack[sp].loc, default_conversion (stack[sp].expr.value));
6419 c_inhibit_evaluation_warnings += (stack[sp].expr.value
6420 == truthvalue_true_node);
6421 break;
6422 default:
6423 break;
6425 sp++;
6426 stack[sp].loc = binary_loc;
6427 stack[sp].expr = c_parser_cast_expression (parser, NULL);
6428 stack[sp].prec = oprec;
6429 stack[sp].op = ocode;
6431 out:
6432 while (sp > 0)
6433 POP;
6434 return stack[0].expr;
6435 #undef POP
6438 /* Parse a cast expression (C90 6.3.4, C99 6.5.4). If AFTER is not
6439 NULL then it is an Objective-C message expression which is the
6440 primary-expression starting the expression as an initializer.
6442 cast-expression:
6443 unary-expression
6444 ( type-name ) unary-expression
6447 static struct c_expr
6448 c_parser_cast_expression (c_parser *parser, struct c_expr *after)
6450 location_t cast_loc = c_parser_peek_token (parser)->location;
6451 gcc_assert (!after || c_dialect_objc ());
6452 if (after)
6453 return c_parser_postfix_expression_after_primary (parser,
6454 cast_loc, *after);
6455 /* If the expression begins with a parenthesized type name, it may
6456 be either a cast or a compound literal; we need to see whether
6457 the next character is '{' to tell the difference. If not, it is
6458 an unary expression. Full detection of unknown typenames here
6459 would require a 3-token lookahead. */
6460 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
6461 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
6463 struct c_type_name *type_name;
6464 struct c_expr ret;
6465 struct c_expr expr;
6466 c_parser_consume_token (parser);
6467 type_name = c_parser_type_name (parser);
6468 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6469 if (type_name == NULL)
6471 ret.value = error_mark_node;
6472 ret.original_code = ERROR_MARK;
6473 ret.original_type = NULL;
6474 return ret;
6477 /* Save casted types in the function's used types hash table. */
6478 used_types_insert (type_name->specs->type);
6480 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
6481 return c_parser_postfix_expression_after_paren_type (parser, type_name,
6482 cast_loc);
6484 location_t expr_loc = c_parser_peek_token (parser)->location;
6485 expr = c_parser_cast_expression (parser, NULL);
6486 expr = convert_lvalue_to_rvalue (expr_loc, expr, true, true);
6488 ret.value = c_cast_expr (cast_loc, type_name, expr.value);
6489 ret.original_code = ERROR_MARK;
6490 ret.original_type = NULL;
6491 return ret;
6493 else
6494 return c_parser_unary_expression (parser);
6497 /* Parse an unary expression (C90 6.3.3, C99 6.5.3).
6499 unary-expression:
6500 postfix-expression
6501 ++ unary-expression
6502 -- unary-expression
6503 unary-operator cast-expression
6504 sizeof unary-expression
6505 sizeof ( type-name )
6507 unary-operator: one of
6508 & * + - ~ !
6510 GNU extensions:
6512 unary-expression:
6513 __alignof__ unary-expression
6514 __alignof__ ( type-name )
6515 && identifier
6517 (C11 permits _Alignof with type names only.)
6519 unary-operator: one of
6520 __extension__ __real__ __imag__
6522 Transactional Memory:
6524 unary-expression:
6525 transaction-expression
6527 In addition, the GNU syntax treats ++ and -- as unary operators, so
6528 they may be applied to cast expressions with errors for non-lvalues
6529 given later. */
6531 static struct c_expr
6532 c_parser_unary_expression (c_parser *parser)
6534 int ext;
6535 struct c_expr ret, op;
6536 location_t op_loc = c_parser_peek_token (parser)->location;
6537 location_t exp_loc;
6538 ret.original_code = ERROR_MARK;
6539 ret.original_type = NULL;
6540 switch (c_parser_peek_token (parser)->type)
6542 case CPP_PLUS_PLUS:
6543 c_parser_consume_token (parser);
6544 exp_loc = c_parser_peek_token (parser)->location;
6545 op = c_parser_cast_expression (parser, NULL);
6547 /* If there is array notations in op, we expand them. */
6548 if (flag_cilkplus && TREE_CODE (op.value) == ARRAY_NOTATION_REF)
6549 return fix_array_notation_expr (exp_loc, PREINCREMENT_EXPR, op);
6550 else
6552 op = default_function_array_read_conversion (exp_loc, op);
6553 return parser_build_unary_op (op_loc, PREINCREMENT_EXPR, op);
6555 case CPP_MINUS_MINUS:
6556 c_parser_consume_token (parser);
6557 exp_loc = c_parser_peek_token (parser)->location;
6558 op = c_parser_cast_expression (parser, NULL);
6560 /* If there is array notations in op, we expand them. */
6561 if (flag_cilkplus && TREE_CODE (op.value) == ARRAY_NOTATION_REF)
6562 return fix_array_notation_expr (exp_loc, PREDECREMENT_EXPR, op);
6563 else
6565 op = default_function_array_read_conversion (exp_loc, op);
6566 return parser_build_unary_op (op_loc, PREDECREMENT_EXPR, op);
6568 case CPP_AND:
6569 c_parser_consume_token (parser);
6570 op = c_parser_cast_expression (parser, NULL);
6571 mark_exp_read (op.value);
6572 return parser_build_unary_op (op_loc, ADDR_EXPR, op);
6573 case CPP_MULT:
6574 c_parser_consume_token (parser);
6575 exp_loc = c_parser_peek_token (parser)->location;
6576 op = c_parser_cast_expression (parser, NULL);
6577 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
6578 ret.value = build_indirect_ref (op_loc, op.value, RO_UNARY_STAR);
6579 return ret;
6580 case CPP_PLUS:
6581 if (!c_dialect_objc () && !in_system_header_at (input_location))
6582 warning_at (op_loc,
6583 OPT_Wtraditional,
6584 "traditional C rejects the unary plus operator");
6585 c_parser_consume_token (parser);
6586 exp_loc = c_parser_peek_token (parser)->location;
6587 op = c_parser_cast_expression (parser, NULL);
6588 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
6589 return parser_build_unary_op (op_loc, CONVERT_EXPR, op);
6590 case CPP_MINUS:
6591 c_parser_consume_token (parser);
6592 exp_loc = c_parser_peek_token (parser)->location;
6593 op = c_parser_cast_expression (parser, NULL);
6594 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
6595 return parser_build_unary_op (op_loc, NEGATE_EXPR, op);
6596 case CPP_COMPL:
6597 c_parser_consume_token (parser);
6598 exp_loc = c_parser_peek_token (parser)->location;
6599 op = c_parser_cast_expression (parser, NULL);
6600 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
6601 return parser_build_unary_op (op_loc, BIT_NOT_EXPR, op);
6602 case CPP_NOT:
6603 c_parser_consume_token (parser);
6604 exp_loc = c_parser_peek_token (parser)->location;
6605 op = c_parser_cast_expression (parser, NULL);
6606 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
6607 return parser_build_unary_op (op_loc, TRUTH_NOT_EXPR, op);
6608 case CPP_AND_AND:
6609 /* Refer to the address of a label as a pointer. */
6610 c_parser_consume_token (parser);
6611 if (c_parser_next_token_is (parser, CPP_NAME))
6613 ret.value = finish_label_address_expr
6614 (c_parser_peek_token (parser)->value, op_loc);
6615 c_parser_consume_token (parser);
6617 else
6619 c_parser_error (parser, "expected identifier");
6620 ret.value = error_mark_node;
6622 return ret;
6623 case CPP_KEYWORD:
6624 switch (c_parser_peek_token (parser)->keyword)
6626 case RID_SIZEOF:
6627 return c_parser_sizeof_expression (parser);
6628 case RID_ALIGNOF:
6629 return c_parser_alignof_expression (parser);
6630 case RID_EXTENSION:
6631 c_parser_consume_token (parser);
6632 ext = disable_extension_diagnostics ();
6633 ret = c_parser_cast_expression (parser, NULL);
6634 restore_extension_diagnostics (ext);
6635 return ret;
6636 case RID_REALPART:
6637 c_parser_consume_token (parser);
6638 exp_loc = c_parser_peek_token (parser)->location;
6639 op = c_parser_cast_expression (parser, NULL);
6640 op = default_function_array_conversion (exp_loc, op);
6641 return parser_build_unary_op (op_loc, REALPART_EXPR, op);
6642 case RID_IMAGPART:
6643 c_parser_consume_token (parser);
6644 exp_loc = c_parser_peek_token (parser)->location;
6645 op = c_parser_cast_expression (parser, NULL);
6646 op = default_function_array_conversion (exp_loc, op);
6647 return parser_build_unary_op (op_loc, IMAGPART_EXPR, op);
6648 case RID_TRANSACTION_ATOMIC:
6649 case RID_TRANSACTION_RELAXED:
6650 return c_parser_transaction_expression (parser,
6651 c_parser_peek_token (parser)->keyword);
6652 default:
6653 return c_parser_postfix_expression (parser);
6655 default:
6656 return c_parser_postfix_expression (parser);
6660 /* Parse a sizeof expression. */
6662 static struct c_expr
6663 c_parser_sizeof_expression (c_parser *parser)
6665 struct c_expr expr;
6666 location_t expr_loc;
6667 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SIZEOF));
6668 c_parser_consume_token (parser);
6669 c_inhibit_evaluation_warnings++;
6670 in_sizeof++;
6671 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
6672 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
6674 /* Either sizeof ( type-name ) or sizeof unary-expression
6675 starting with a compound literal. */
6676 struct c_type_name *type_name;
6677 c_parser_consume_token (parser);
6678 expr_loc = c_parser_peek_token (parser)->location;
6679 type_name = c_parser_type_name (parser);
6680 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6681 if (type_name == NULL)
6683 struct c_expr ret;
6684 c_inhibit_evaluation_warnings--;
6685 in_sizeof--;
6686 ret.value = error_mark_node;
6687 ret.original_code = ERROR_MARK;
6688 ret.original_type = NULL;
6689 return ret;
6691 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
6693 expr = c_parser_postfix_expression_after_paren_type (parser,
6694 type_name,
6695 expr_loc);
6696 goto sizeof_expr;
6698 /* sizeof ( type-name ). */
6699 c_inhibit_evaluation_warnings--;
6700 in_sizeof--;
6701 return c_expr_sizeof_type (expr_loc, type_name);
6703 else
6705 expr_loc = c_parser_peek_token (parser)->location;
6706 expr = c_parser_unary_expression (parser);
6707 sizeof_expr:
6708 c_inhibit_evaluation_warnings--;
6709 in_sizeof--;
6710 mark_exp_read (expr.value);
6711 if (TREE_CODE (expr.value) == COMPONENT_REF
6712 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
6713 error_at (expr_loc, "%<sizeof%> applied to a bit-field");
6714 return c_expr_sizeof_expr (expr_loc, expr);
6718 /* Parse an alignof expression. */
6720 static struct c_expr
6721 c_parser_alignof_expression (c_parser *parser)
6723 struct c_expr expr;
6724 location_t loc = c_parser_peek_token (parser)->location;
6725 tree alignof_spelling = c_parser_peek_token (parser)->value;
6726 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNOF));
6727 bool is_c11_alignof = strcmp (IDENTIFIER_POINTER (alignof_spelling),
6728 "_Alignof") == 0;
6729 /* A diagnostic is not required for the use of this identifier in
6730 the implementation namespace; only diagnose it for the C11
6731 spelling because of existing code using the other spellings. */
6732 if (is_c11_alignof)
6734 if (flag_isoc99)
6735 pedwarn_c99 (loc, OPT_Wpedantic, "ISO C99 does not support %qE",
6736 alignof_spelling);
6737 else
6738 pedwarn_c99 (loc, OPT_Wpedantic, "ISO C90 does not support %qE",
6739 alignof_spelling);
6741 c_parser_consume_token (parser);
6742 c_inhibit_evaluation_warnings++;
6743 in_alignof++;
6744 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
6745 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
6747 /* Either __alignof__ ( type-name ) or __alignof__
6748 unary-expression starting with a compound literal. */
6749 location_t loc;
6750 struct c_type_name *type_name;
6751 struct c_expr ret;
6752 c_parser_consume_token (parser);
6753 loc = c_parser_peek_token (parser)->location;
6754 type_name = c_parser_type_name (parser);
6755 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6756 if (type_name == NULL)
6758 struct c_expr ret;
6759 c_inhibit_evaluation_warnings--;
6760 in_alignof--;
6761 ret.value = error_mark_node;
6762 ret.original_code = ERROR_MARK;
6763 ret.original_type = NULL;
6764 return ret;
6766 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
6768 expr = c_parser_postfix_expression_after_paren_type (parser,
6769 type_name,
6770 loc);
6771 goto alignof_expr;
6773 /* alignof ( type-name ). */
6774 c_inhibit_evaluation_warnings--;
6775 in_alignof--;
6776 ret.value = c_sizeof_or_alignof_type (loc, groktypename (type_name,
6777 NULL, NULL),
6778 false, is_c11_alignof, 1);
6779 ret.original_code = ERROR_MARK;
6780 ret.original_type = NULL;
6781 return ret;
6783 else
6785 struct c_expr ret;
6786 expr = c_parser_unary_expression (parser);
6787 alignof_expr:
6788 mark_exp_read (expr.value);
6789 c_inhibit_evaluation_warnings--;
6790 in_alignof--;
6791 pedwarn (loc, OPT_Wpedantic, "ISO C does not allow %<%E (expression)%>",
6792 alignof_spelling);
6793 ret.value = c_alignof_expr (loc, expr.value);
6794 ret.original_code = ERROR_MARK;
6795 ret.original_type = NULL;
6796 return ret;
6800 /* Helper function to read arguments of builtins which are interfaces
6801 for the middle-end nodes like COMPLEX_EXPR, VEC_PERM_EXPR and
6802 others. The name of the builtin is passed using BNAME parameter.
6803 Function returns true if there were no errors while parsing and
6804 stores the arguments in CEXPR_LIST. */
6805 static bool
6806 c_parser_get_builtin_args (c_parser *parser, const char *bname,
6807 vec<c_expr_t, va_gc> **ret_cexpr_list,
6808 bool choose_expr_p)
6810 location_t loc = c_parser_peek_token (parser)->location;
6811 vec<c_expr_t, va_gc> *cexpr_list;
6812 c_expr_t expr;
6813 bool saved_force_folding_builtin_constant_p;
6815 *ret_cexpr_list = NULL;
6816 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
6818 error_at (loc, "cannot take address of %qs", bname);
6819 return false;
6822 c_parser_consume_token (parser);
6824 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
6826 c_parser_consume_token (parser);
6827 return true;
6830 saved_force_folding_builtin_constant_p
6831 = force_folding_builtin_constant_p;
6832 force_folding_builtin_constant_p |= choose_expr_p;
6833 expr = c_parser_expr_no_commas (parser, NULL);
6834 force_folding_builtin_constant_p
6835 = saved_force_folding_builtin_constant_p;
6836 vec_alloc (cexpr_list, 1);
6837 vec_safe_push (cexpr_list, expr);
6838 while (c_parser_next_token_is (parser, CPP_COMMA))
6840 c_parser_consume_token (parser);
6841 expr = c_parser_expr_no_commas (parser, NULL);
6842 vec_safe_push (cexpr_list, expr);
6845 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
6846 return false;
6848 *ret_cexpr_list = cexpr_list;
6849 return true;
6852 /* This represents a single generic-association. */
6854 struct c_generic_association
6856 /* The location of the starting token of the type. */
6857 location_t type_location;
6858 /* The association's type, or NULL_TREE for 'default'. */
6859 tree type;
6860 /* The association's expression. */
6861 struct c_expr expression;
6864 /* Parse a generic-selection. (C11 6.5.1.1).
6866 generic-selection:
6867 _Generic ( assignment-expression , generic-assoc-list )
6869 generic-assoc-list:
6870 generic-association
6871 generic-assoc-list , generic-association
6873 generic-association:
6874 type-name : assignment-expression
6875 default : assignment-expression
6878 static struct c_expr
6879 c_parser_generic_selection (c_parser *parser)
6881 vec<c_generic_association> associations = vNULL;
6882 struct c_expr selector, error_expr;
6883 tree selector_type;
6884 struct c_generic_association matched_assoc;
6885 bool match_found = false;
6886 location_t generic_loc, selector_loc;
6888 error_expr.original_code = ERROR_MARK;
6889 error_expr.original_type = NULL;
6890 error_expr.value = error_mark_node;
6891 matched_assoc.type_location = UNKNOWN_LOCATION;
6892 matched_assoc.type = NULL_TREE;
6893 matched_assoc.expression = error_expr;
6895 gcc_assert (c_parser_next_token_is_keyword (parser, RID_GENERIC));
6896 generic_loc = c_parser_peek_token (parser)->location;
6897 c_parser_consume_token (parser);
6898 if (flag_isoc99)
6899 pedwarn_c99 (generic_loc, OPT_Wpedantic,
6900 "ISO C99 does not support %<_Generic%>");
6901 else
6902 pedwarn_c99 (generic_loc, OPT_Wpedantic,
6903 "ISO C90 does not support %<_Generic%>");
6905 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6906 return error_expr;
6908 c_inhibit_evaluation_warnings++;
6909 selector_loc = c_parser_peek_token (parser)->location;
6910 selector = c_parser_expr_no_commas (parser, NULL);
6911 selector = default_function_array_conversion (selector_loc, selector);
6912 c_inhibit_evaluation_warnings--;
6914 if (selector.value == error_mark_node)
6916 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6917 return selector;
6919 selector_type = TREE_TYPE (selector.value);
6920 /* In ISO C terms, rvalues (including the controlling expression of
6921 _Generic) do not have qualified types. */
6922 if (TREE_CODE (selector_type) != ARRAY_TYPE)
6923 selector_type = TYPE_MAIN_VARIANT (selector_type);
6924 /* In ISO C terms, _Noreturn is not part of the type of expressions
6925 such as &abort, but in GCC it is represented internally as a type
6926 qualifier. */
6927 if (FUNCTION_POINTER_TYPE_P (selector_type)
6928 && TYPE_QUALS (TREE_TYPE (selector_type)) != TYPE_UNQUALIFIED)
6929 selector_type
6930 = build_pointer_type (TYPE_MAIN_VARIANT (TREE_TYPE (selector_type)));
6932 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
6934 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6935 return error_expr;
6938 while (1)
6940 struct c_generic_association assoc, *iter;
6941 unsigned int ix;
6942 c_token *token = c_parser_peek_token (parser);
6944 assoc.type_location = token->location;
6945 if (token->type == CPP_KEYWORD && token->keyword == RID_DEFAULT)
6947 c_parser_consume_token (parser);
6948 assoc.type = NULL_TREE;
6950 else
6952 struct c_type_name *type_name;
6954 type_name = c_parser_type_name (parser);
6955 if (type_name == NULL)
6957 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6958 goto error_exit;
6960 assoc.type = groktypename (type_name, NULL, NULL);
6961 if (assoc.type == error_mark_node)
6963 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6964 goto error_exit;
6967 if (TREE_CODE (assoc.type) == FUNCTION_TYPE)
6968 error_at (assoc.type_location,
6969 "%<_Generic%> association has function type");
6970 else if (!COMPLETE_TYPE_P (assoc.type))
6971 error_at (assoc.type_location,
6972 "%<_Generic%> association has incomplete type");
6974 if (variably_modified_type_p (assoc.type, NULL_TREE))
6975 error_at (assoc.type_location,
6976 "%<_Generic%> association has "
6977 "variable length type");
6980 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
6982 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6983 goto error_exit;
6986 assoc.expression = c_parser_expr_no_commas (parser, NULL);
6987 if (assoc.expression.value == error_mark_node)
6989 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6990 goto error_exit;
6993 for (ix = 0; associations.iterate (ix, &iter); ++ix)
6995 if (assoc.type == NULL_TREE)
6997 if (iter->type == NULL_TREE)
6999 error_at (assoc.type_location,
7000 "duplicate %<default%> case in %<_Generic%>");
7001 inform (iter->type_location, "original %<default%> is here");
7004 else if (iter->type != NULL_TREE)
7006 if (comptypes (assoc.type, iter->type))
7008 error_at (assoc.type_location,
7009 "%<_Generic%> specifies two compatible types");
7010 inform (iter->type_location, "compatible type is here");
7015 if (assoc.type == NULL_TREE)
7017 if (!match_found)
7019 matched_assoc = assoc;
7020 match_found = true;
7023 else if (comptypes (assoc.type, selector_type))
7025 if (!match_found || matched_assoc.type == NULL_TREE)
7027 matched_assoc = assoc;
7028 match_found = true;
7030 else
7032 error_at (assoc.type_location,
7033 "%<_Generic> selector matches multiple associations");
7034 inform (matched_assoc.type_location,
7035 "other match is here");
7039 associations.safe_push (assoc);
7041 if (c_parser_peek_token (parser)->type != CPP_COMMA)
7042 break;
7043 c_parser_consume_token (parser);
7046 associations.release ();
7048 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
7050 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7051 return error_expr;
7054 if (!match_found)
7056 error_at (selector_loc, "%<_Generic%> selector of type %qT is not "
7057 "compatible with any association",
7058 selector_type);
7059 return error_expr;
7062 return matched_assoc.expression;
7064 error_exit:
7065 associations.release ();
7066 return error_expr;
7069 /* Parse a postfix expression (C90 6.3.1-6.3.2, C99 6.5.1-6.5.2).
7071 postfix-expression:
7072 primary-expression
7073 postfix-expression [ expression ]
7074 postfix-expression ( argument-expression-list[opt] )
7075 postfix-expression . identifier
7076 postfix-expression -> identifier
7077 postfix-expression ++
7078 postfix-expression --
7079 ( type-name ) { initializer-list }
7080 ( type-name ) { initializer-list , }
7082 argument-expression-list:
7083 argument-expression
7084 argument-expression-list , argument-expression
7086 primary-expression:
7087 identifier
7088 constant
7089 string-literal
7090 ( expression )
7091 generic-selection
7093 GNU extensions:
7095 primary-expression:
7096 __func__
7097 (treated as a keyword in GNU C)
7098 __FUNCTION__
7099 __PRETTY_FUNCTION__
7100 ( compound-statement )
7101 __builtin_va_arg ( assignment-expression , type-name )
7102 __builtin_offsetof ( type-name , offsetof-member-designator )
7103 __builtin_choose_expr ( assignment-expression ,
7104 assignment-expression ,
7105 assignment-expression )
7106 __builtin_types_compatible_p ( type-name , type-name )
7107 __builtin_complex ( assignment-expression , assignment-expression )
7108 __builtin_shuffle ( assignment-expression , assignment-expression )
7109 __builtin_shuffle ( assignment-expression ,
7110 assignment-expression ,
7111 assignment-expression, )
7113 offsetof-member-designator:
7114 identifier
7115 offsetof-member-designator . identifier
7116 offsetof-member-designator [ expression ]
7118 Objective-C:
7120 primary-expression:
7121 [ objc-receiver objc-message-args ]
7122 @selector ( objc-selector-arg )
7123 @protocol ( identifier )
7124 @encode ( type-name )
7125 objc-string-literal
7126 Classname . identifier
7129 static struct c_expr
7130 c_parser_postfix_expression (c_parser *parser)
7132 struct c_expr expr, e1;
7133 struct c_type_name *t1, *t2;
7134 location_t loc = c_parser_peek_token (parser)->location;;
7135 expr.original_code = ERROR_MARK;
7136 expr.original_type = NULL;
7137 switch (c_parser_peek_token (parser)->type)
7139 case CPP_NUMBER:
7140 expr.value = c_parser_peek_token (parser)->value;
7141 loc = c_parser_peek_token (parser)->location;
7142 c_parser_consume_token (parser);
7143 if (TREE_CODE (expr.value) == FIXED_CST
7144 && !targetm.fixed_point_supported_p ())
7146 error_at (loc, "fixed-point types not supported for this target");
7147 expr.value = error_mark_node;
7149 break;
7150 case CPP_CHAR:
7151 case CPP_CHAR16:
7152 case CPP_CHAR32:
7153 case CPP_WCHAR:
7154 expr.value = c_parser_peek_token (parser)->value;
7155 c_parser_consume_token (parser);
7156 break;
7157 case CPP_STRING:
7158 case CPP_STRING16:
7159 case CPP_STRING32:
7160 case CPP_WSTRING:
7161 case CPP_UTF8STRING:
7162 expr.value = c_parser_peek_token (parser)->value;
7163 expr.original_code = STRING_CST;
7164 c_parser_consume_token (parser);
7165 break;
7166 case CPP_OBJC_STRING:
7167 gcc_assert (c_dialect_objc ());
7168 expr.value
7169 = objc_build_string_object (c_parser_peek_token (parser)->value);
7170 c_parser_consume_token (parser);
7171 break;
7172 case CPP_NAME:
7173 switch (c_parser_peek_token (parser)->id_kind)
7175 case C_ID_ID:
7177 tree id = c_parser_peek_token (parser)->value;
7178 c_parser_consume_token (parser);
7179 expr.value = build_external_ref (loc, id,
7180 (c_parser_peek_token (parser)->type
7181 == CPP_OPEN_PAREN),
7182 &expr.original_type);
7183 break;
7185 case C_ID_CLASSNAME:
7187 /* Here we parse the Objective-C 2.0 Class.name dot
7188 syntax. */
7189 tree class_name = c_parser_peek_token (parser)->value;
7190 tree component;
7191 c_parser_consume_token (parser);
7192 gcc_assert (c_dialect_objc ());
7193 if (!c_parser_require (parser, CPP_DOT, "expected %<.%>"))
7195 expr.value = error_mark_node;
7196 break;
7198 if (c_parser_next_token_is_not (parser, CPP_NAME))
7200 c_parser_error (parser, "expected identifier");
7201 expr.value = error_mark_node;
7202 break;
7204 component = c_parser_peek_token (parser)->value;
7205 c_parser_consume_token (parser);
7206 expr.value = objc_build_class_component_ref (class_name,
7207 component);
7208 break;
7210 default:
7211 c_parser_error (parser, "expected expression");
7212 expr.value = error_mark_node;
7213 break;
7215 break;
7216 case CPP_OPEN_PAREN:
7217 /* A parenthesized expression, statement expression or compound
7218 literal. */
7219 if (c_parser_peek_2nd_token (parser)->type == CPP_OPEN_BRACE)
7221 /* A statement expression. */
7222 tree stmt;
7223 location_t brace_loc;
7224 c_parser_consume_token (parser);
7225 brace_loc = c_parser_peek_token (parser)->location;
7226 c_parser_consume_token (parser);
7227 if (!building_stmt_list_p ())
7229 error_at (loc, "braced-group within expression allowed "
7230 "only inside a function");
7231 parser->error = true;
7232 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
7233 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7234 expr.value = error_mark_node;
7235 break;
7237 stmt = c_begin_stmt_expr ();
7238 c_parser_compound_statement_nostart (parser);
7239 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7240 "expected %<)%>");
7241 pedwarn (loc, OPT_Wpedantic,
7242 "ISO C forbids braced-groups within expressions");
7243 expr.value = c_finish_stmt_expr (brace_loc, stmt);
7244 mark_exp_read (expr.value);
7246 else if (c_token_starts_typename (c_parser_peek_2nd_token (parser)))
7248 /* A compound literal. ??? Can we actually get here rather
7249 than going directly to
7250 c_parser_postfix_expression_after_paren_type from
7251 elsewhere? */
7252 location_t loc;
7253 struct c_type_name *type_name;
7254 c_parser_consume_token (parser);
7255 loc = c_parser_peek_token (parser)->location;
7256 type_name = c_parser_type_name (parser);
7257 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7258 "expected %<)%>");
7259 if (type_name == NULL)
7261 expr.value = error_mark_node;
7263 else
7264 expr = c_parser_postfix_expression_after_paren_type (parser,
7265 type_name,
7266 loc);
7268 else
7270 /* A parenthesized expression. */
7271 c_parser_consume_token (parser);
7272 expr = c_parser_expression (parser);
7273 if (TREE_CODE (expr.value) == MODIFY_EXPR)
7274 TREE_NO_WARNING (expr.value) = 1;
7275 if (expr.original_code != C_MAYBE_CONST_EXPR)
7276 expr.original_code = ERROR_MARK;
7277 /* Don't change EXPR.ORIGINAL_TYPE. */
7278 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7279 "expected %<)%>");
7281 break;
7282 case CPP_KEYWORD:
7283 switch (c_parser_peek_token (parser)->keyword)
7285 case RID_FUNCTION_NAME:
7286 pedwarn (loc, OPT_Wpedantic, "ISO C does not support "
7287 "%<__FUNCTION__%> predefined identifier");
7288 expr.value = fname_decl (loc,
7289 c_parser_peek_token (parser)->keyword,
7290 c_parser_peek_token (parser)->value);
7291 c_parser_consume_token (parser);
7292 break;
7293 case RID_PRETTY_FUNCTION_NAME:
7294 pedwarn (loc, OPT_Wpedantic, "ISO C does not support "
7295 "%<__PRETTY_FUNCTION__%> predefined identifier");
7296 expr.value = fname_decl (loc,
7297 c_parser_peek_token (parser)->keyword,
7298 c_parser_peek_token (parser)->value);
7299 c_parser_consume_token (parser);
7300 break;
7301 case RID_C99_FUNCTION_NAME:
7302 pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not support "
7303 "%<__func__%> predefined identifier");
7304 expr.value = fname_decl (loc,
7305 c_parser_peek_token (parser)->keyword,
7306 c_parser_peek_token (parser)->value);
7307 c_parser_consume_token (parser);
7308 break;
7309 case RID_VA_ARG:
7310 c_parser_consume_token (parser);
7311 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7313 expr.value = error_mark_node;
7314 break;
7316 e1 = c_parser_expr_no_commas (parser, NULL);
7317 mark_exp_read (e1.value);
7318 e1.value = c_fully_fold (e1.value, false, NULL);
7319 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
7321 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7322 expr.value = error_mark_node;
7323 break;
7325 loc = c_parser_peek_token (parser)->location;
7326 t1 = c_parser_type_name (parser);
7327 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7328 "expected %<)%>");
7329 if (t1 == NULL)
7331 expr.value = error_mark_node;
7333 else
7335 tree type_expr = NULL_TREE;
7336 expr.value = c_build_va_arg (loc, e1.value,
7337 groktypename (t1, &type_expr, NULL));
7338 if (type_expr)
7340 expr.value = build2 (C_MAYBE_CONST_EXPR,
7341 TREE_TYPE (expr.value), type_expr,
7342 expr.value);
7343 C_MAYBE_CONST_EXPR_NON_CONST (expr.value) = true;
7346 break;
7347 case RID_OFFSETOF:
7348 c_parser_consume_token (parser);
7349 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7351 expr.value = error_mark_node;
7352 break;
7354 t1 = c_parser_type_name (parser);
7355 if (t1 == NULL)
7356 parser->error = true;
7357 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
7358 gcc_assert (parser->error);
7359 if (parser->error)
7361 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7362 expr.value = error_mark_node;
7363 break;
7367 tree type = groktypename (t1, NULL, NULL);
7368 tree offsetof_ref;
7369 if (type == error_mark_node)
7370 offsetof_ref = error_mark_node;
7371 else
7373 offsetof_ref = build1 (INDIRECT_REF, type, null_pointer_node);
7374 SET_EXPR_LOCATION (offsetof_ref, loc);
7376 /* Parse the second argument to __builtin_offsetof. We
7377 must have one identifier, and beyond that we want to
7378 accept sub structure and sub array references. */
7379 if (c_parser_next_token_is (parser, CPP_NAME))
7381 offsetof_ref = build_component_ref
7382 (loc, offsetof_ref, c_parser_peek_token (parser)->value);
7383 c_parser_consume_token (parser);
7384 while (c_parser_next_token_is (parser, CPP_DOT)
7385 || c_parser_next_token_is (parser,
7386 CPP_OPEN_SQUARE)
7387 || c_parser_next_token_is (parser,
7388 CPP_DEREF))
7390 if (c_parser_next_token_is (parser, CPP_DEREF))
7392 loc = c_parser_peek_token (parser)->location;
7393 offsetof_ref = build_array_ref (loc,
7394 offsetof_ref,
7395 integer_zero_node);
7396 goto do_dot;
7398 else if (c_parser_next_token_is (parser, CPP_DOT))
7400 do_dot:
7401 c_parser_consume_token (parser);
7402 if (c_parser_next_token_is_not (parser,
7403 CPP_NAME))
7405 c_parser_error (parser, "expected identifier");
7406 break;
7408 offsetof_ref = build_component_ref
7409 (loc, offsetof_ref,
7410 c_parser_peek_token (parser)->value);
7411 c_parser_consume_token (parser);
7413 else
7415 struct c_expr ce;
7416 tree idx;
7417 loc = c_parser_peek_token (parser)->location;
7418 c_parser_consume_token (parser);
7419 ce = c_parser_expression (parser);
7420 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
7421 idx = ce.value;
7422 idx = c_fully_fold (idx, false, NULL);
7423 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
7424 "expected %<]%>");
7425 offsetof_ref = build_array_ref (loc, offsetof_ref, idx);
7429 else
7430 c_parser_error (parser, "expected identifier");
7431 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7432 "expected %<)%>");
7433 expr.value = fold_offsetof (offsetof_ref);
7435 break;
7436 case RID_CHOOSE_EXPR:
7438 vec<c_expr_t, va_gc> *cexpr_list;
7439 c_expr_t *e1_p, *e2_p, *e3_p;
7440 tree c;
7442 c_parser_consume_token (parser);
7443 if (!c_parser_get_builtin_args (parser,
7444 "__builtin_choose_expr",
7445 &cexpr_list, true))
7447 expr.value = error_mark_node;
7448 break;
7451 if (vec_safe_length (cexpr_list) != 3)
7453 error_at (loc, "wrong number of arguments to "
7454 "%<__builtin_choose_expr%>");
7455 expr.value = error_mark_node;
7456 break;
7459 e1_p = &(*cexpr_list)[0];
7460 e2_p = &(*cexpr_list)[1];
7461 e3_p = &(*cexpr_list)[2];
7463 c = e1_p->value;
7464 mark_exp_read (e2_p->value);
7465 mark_exp_read (e3_p->value);
7466 if (TREE_CODE (c) != INTEGER_CST
7467 || !INTEGRAL_TYPE_P (TREE_TYPE (c)))
7468 error_at (loc,
7469 "first argument to %<__builtin_choose_expr%> not"
7470 " a constant");
7471 constant_expression_warning (c);
7472 expr = integer_zerop (c) ? *e3_p : *e2_p;
7473 break;
7475 case RID_TYPES_COMPATIBLE_P:
7476 c_parser_consume_token (parser);
7477 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7479 expr.value = error_mark_node;
7480 break;
7482 t1 = c_parser_type_name (parser);
7483 if (t1 == NULL)
7485 expr.value = error_mark_node;
7486 break;
7488 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
7490 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7491 expr.value = error_mark_node;
7492 break;
7494 t2 = c_parser_type_name (parser);
7495 if (t2 == NULL)
7497 expr.value = error_mark_node;
7498 break;
7500 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7501 "expected %<)%>");
7503 tree e1, e2;
7504 e1 = groktypename (t1, NULL, NULL);
7505 e2 = groktypename (t2, NULL, NULL);
7506 if (e1 == error_mark_node || e2 == error_mark_node)
7508 expr.value = error_mark_node;
7509 break;
7512 e1 = TYPE_MAIN_VARIANT (e1);
7513 e2 = TYPE_MAIN_VARIANT (e2);
7515 expr.value
7516 = comptypes (e1, e2) ? integer_one_node : integer_zero_node;
7518 break;
7519 case RID_BUILTIN_CALL_WITH_STATIC_CHAIN:
7521 vec<c_expr_t, va_gc> *cexpr_list;
7522 c_expr_t *e2_p;
7523 tree chain_value;
7525 c_parser_consume_token (parser);
7526 if (!c_parser_get_builtin_args (parser,
7527 "__builtin_call_with_static_chain",
7528 &cexpr_list, false))
7530 expr.value = error_mark_node;
7531 break;
7533 if (vec_safe_length (cexpr_list) != 2)
7535 error_at (loc, "wrong number of arguments to "
7536 "%<__builtin_call_with_static_chain%>");
7537 expr.value = error_mark_node;
7538 break;
7541 expr = (*cexpr_list)[0];
7542 e2_p = &(*cexpr_list)[1];
7543 *e2_p = convert_lvalue_to_rvalue (loc, *e2_p, true, true);
7544 chain_value = e2_p->value;
7545 mark_exp_read (chain_value);
7547 if (TREE_CODE (expr.value) != CALL_EXPR)
7548 error_at (loc, "first argument to "
7549 "%<__builtin_call_with_static_chain%> "
7550 "must be a call expression");
7551 else if (TREE_CODE (TREE_TYPE (chain_value)) != POINTER_TYPE)
7552 error_at (loc, "second argument to "
7553 "%<__builtin_call_with_static_chain%> "
7554 "must be a pointer type");
7555 else
7556 CALL_EXPR_STATIC_CHAIN (expr.value) = chain_value;
7557 break;
7559 case RID_BUILTIN_COMPLEX:
7561 vec<c_expr_t, va_gc> *cexpr_list;
7562 c_expr_t *e1_p, *e2_p;
7564 c_parser_consume_token (parser);
7565 if (!c_parser_get_builtin_args (parser,
7566 "__builtin_complex",
7567 &cexpr_list, false))
7569 expr.value = error_mark_node;
7570 break;
7573 if (vec_safe_length (cexpr_list) != 2)
7575 error_at (loc, "wrong number of arguments to "
7576 "%<__builtin_complex%>");
7577 expr.value = error_mark_node;
7578 break;
7581 e1_p = &(*cexpr_list)[0];
7582 e2_p = &(*cexpr_list)[1];
7584 *e1_p = convert_lvalue_to_rvalue (loc, *e1_p, true, true);
7585 if (TREE_CODE (e1_p->value) == EXCESS_PRECISION_EXPR)
7586 e1_p->value = convert (TREE_TYPE (e1_p->value),
7587 TREE_OPERAND (e1_p->value, 0));
7588 *e2_p = convert_lvalue_to_rvalue (loc, *e2_p, true, true);
7589 if (TREE_CODE (e2_p->value) == EXCESS_PRECISION_EXPR)
7590 e2_p->value = convert (TREE_TYPE (e2_p->value),
7591 TREE_OPERAND (e2_p->value, 0));
7592 if (!SCALAR_FLOAT_TYPE_P (TREE_TYPE (e1_p->value))
7593 || DECIMAL_FLOAT_TYPE_P (TREE_TYPE (e1_p->value))
7594 || !SCALAR_FLOAT_TYPE_P (TREE_TYPE (e2_p->value))
7595 || DECIMAL_FLOAT_TYPE_P (TREE_TYPE (e2_p->value)))
7597 error_at (loc, "%<__builtin_complex%> operand "
7598 "not of real binary floating-point type");
7599 expr.value = error_mark_node;
7600 break;
7602 if (TYPE_MAIN_VARIANT (TREE_TYPE (e1_p->value))
7603 != TYPE_MAIN_VARIANT (TREE_TYPE (e2_p->value)))
7605 error_at (loc,
7606 "%<__builtin_complex%> operands of different types");
7607 expr.value = error_mark_node;
7608 break;
7610 pedwarn_c90 (loc, OPT_Wpedantic,
7611 "ISO C90 does not support complex types");
7612 expr.value = build2 (COMPLEX_EXPR,
7613 build_complex_type
7614 (TYPE_MAIN_VARIANT
7615 (TREE_TYPE (e1_p->value))),
7616 e1_p->value, e2_p->value);
7617 break;
7619 case RID_BUILTIN_SHUFFLE:
7621 vec<c_expr_t, va_gc> *cexpr_list;
7622 unsigned int i;
7623 c_expr_t *p;
7625 c_parser_consume_token (parser);
7626 if (!c_parser_get_builtin_args (parser,
7627 "__builtin_shuffle",
7628 &cexpr_list, false))
7630 expr.value = error_mark_node;
7631 break;
7634 FOR_EACH_VEC_SAFE_ELT (cexpr_list, i, p)
7635 *p = convert_lvalue_to_rvalue (loc, *p, true, true);
7637 if (vec_safe_length (cexpr_list) == 2)
7638 expr.value =
7639 c_build_vec_perm_expr
7640 (loc, (*cexpr_list)[0].value,
7641 NULL_TREE, (*cexpr_list)[1].value);
7643 else if (vec_safe_length (cexpr_list) == 3)
7644 expr.value =
7645 c_build_vec_perm_expr
7646 (loc, (*cexpr_list)[0].value,
7647 (*cexpr_list)[1].value,
7648 (*cexpr_list)[2].value);
7649 else
7651 error_at (loc, "wrong number of arguments to "
7652 "%<__builtin_shuffle%>");
7653 expr.value = error_mark_node;
7655 break;
7657 case RID_AT_SELECTOR:
7658 gcc_assert (c_dialect_objc ());
7659 c_parser_consume_token (parser);
7660 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7662 expr.value = error_mark_node;
7663 break;
7666 tree sel = c_parser_objc_selector_arg (parser);
7667 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7668 "expected %<)%>");
7669 expr.value = objc_build_selector_expr (loc, sel);
7671 break;
7672 case RID_AT_PROTOCOL:
7673 gcc_assert (c_dialect_objc ());
7674 c_parser_consume_token (parser);
7675 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7677 expr.value = error_mark_node;
7678 break;
7680 if (c_parser_next_token_is_not (parser, CPP_NAME))
7682 c_parser_error (parser, "expected identifier");
7683 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7684 expr.value = error_mark_node;
7685 break;
7688 tree id = c_parser_peek_token (parser)->value;
7689 c_parser_consume_token (parser);
7690 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7691 "expected %<)%>");
7692 expr.value = objc_build_protocol_expr (id);
7694 break;
7695 case RID_AT_ENCODE:
7696 /* Extension to support C-structures in the archiver. */
7697 gcc_assert (c_dialect_objc ());
7698 c_parser_consume_token (parser);
7699 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7701 expr.value = error_mark_node;
7702 break;
7704 t1 = c_parser_type_name (parser);
7705 if (t1 == NULL)
7707 expr.value = error_mark_node;
7708 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7709 break;
7711 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7712 "expected %<)%>");
7714 tree type = groktypename (t1, NULL, NULL);
7715 expr.value = objc_build_encode_expr (type);
7717 break;
7718 case RID_GENERIC:
7719 expr = c_parser_generic_selection (parser);
7720 break;
7721 case RID_CILK_SPAWN:
7722 c_parser_consume_token (parser);
7723 if (!flag_cilkplus)
7725 error_at (loc, "-fcilkplus must be enabled to use "
7726 "%<_Cilk_spawn%>");
7727 expr = c_parser_postfix_expression (parser);
7728 expr.value = error_mark_node;
7730 else if (c_parser_peek_token (parser)->keyword == RID_CILK_SPAWN)
7732 error_at (loc, "consecutive %<_Cilk_spawn%> keywords "
7733 "are not permitted");
7734 /* Now flush out all the _Cilk_spawns. */
7735 while (c_parser_peek_token (parser)->keyword == RID_CILK_SPAWN)
7736 c_parser_consume_token (parser);
7737 expr = c_parser_postfix_expression (parser);
7739 else
7741 expr = c_parser_postfix_expression (parser);
7742 expr.value = build_cilk_spawn (loc, expr.value);
7744 break;
7745 default:
7746 c_parser_error (parser, "expected expression");
7747 expr.value = error_mark_node;
7748 break;
7750 break;
7751 case CPP_OPEN_SQUARE:
7752 if (c_dialect_objc ())
7754 tree receiver, args;
7755 c_parser_consume_token (parser);
7756 receiver = c_parser_objc_receiver (parser);
7757 args = c_parser_objc_message_args (parser);
7758 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
7759 "expected %<]%>");
7760 expr.value = objc_build_message_expr (receiver, args);
7761 break;
7763 /* Else fall through to report error. */
7764 default:
7765 c_parser_error (parser, "expected expression");
7766 expr.value = error_mark_node;
7767 break;
7769 return c_parser_postfix_expression_after_primary (parser, loc, expr);
7772 /* Parse a postfix expression after a parenthesized type name: the
7773 brace-enclosed initializer of a compound literal, possibly followed
7774 by some postfix operators. This is separate because it is not
7775 possible to tell until after the type name whether a cast
7776 expression has a cast or a compound literal, or whether the operand
7777 of sizeof is a parenthesized type name or starts with a compound
7778 literal. TYPE_LOC is the location where TYPE_NAME starts--the
7779 location of the first token after the parentheses around the type
7780 name. */
7782 static struct c_expr
7783 c_parser_postfix_expression_after_paren_type (c_parser *parser,
7784 struct c_type_name *type_name,
7785 location_t type_loc)
7787 tree type;
7788 struct c_expr init;
7789 bool non_const;
7790 struct c_expr expr;
7791 location_t start_loc;
7792 tree type_expr = NULL_TREE;
7793 bool type_expr_const = true;
7794 check_compound_literal_type (type_loc, type_name);
7795 start_init (NULL_TREE, NULL, 0);
7796 type = groktypename (type_name, &type_expr, &type_expr_const);
7797 start_loc = c_parser_peek_token (parser)->location;
7798 if (type != error_mark_node && C_TYPE_VARIABLE_SIZE (type))
7800 error_at (type_loc, "compound literal has variable size");
7801 type = error_mark_node;
7803 init = c_parser_braced_init (parser, type, false);
7804 finish_init ();
7805 maybe_warn_string_init (type_loc, type, init);
7807 if (type != error_mark_node
7808 && !ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (type))
7809 && current_function_decl)
7811 error ("compound literal qualified by address-space qualifier");
7812 type = error_mark_node;
7815 pedwarn_c90 (start_loc, OPT_Wpedantic, "ISO C90 forbids compound literals");
7816 non_const = ((init.value && TREE_CODE (init.value) == CONSTRUCTOR)
7817 ? CONSTRUCTOR_NON_CONST (init.value)
7818 : init.original_code == C_MAYBE_CONST_EXPR);
7819 non_const |= !type_expr_const;
7820 expr.value = build_compound_literal (start_loc, type, init.value, non_const);
7821 expr.original_code = ERROR_MARK;
7822 expr.original_type = NULL;
7823 if (type_expr)
7825 if (TREE_CODE (expr.value) == C_MAYBE_CONST_EXPR)
7827 gcc_assert (C_MAYBE_CONST_EXPR_PRE (expr.value) == NULL_TREE);
7828 C_MAYBE_CONST_EXPR_PRE (expr.value) = type_expr;
7830 else
7832 gcc_assert (!non_const);
7833 expr.value = build2 (C_MAYBE_CONST_EXPR, type,
7834 type_expr, expr.value);
7837 return c_parser_postfix_expression_after_primary (parser, start_loc, expr);
7840 /* Callback function for sizeof_pointer_memaccess_warning to compare
7841 types. */
7843 static bool
7844 sizeof_ptr_memacc_comptypes (tree type1, tree type2)
7846 return comptypes (type1, type2) == 1;
7849 /* Parse a postfix expression after the initial primary or compound
7850 literal; that is, parse a series of postfix operators.
7852 EXPR_LOC is the location of the primary expression. */
7854 static struct c_expr
7855 c_parser_postfix_expression_after_primary (c_parser *parser,
7856 location_t expr_loc,
7857 struct c_expr expr)
7859 struct c_expr orig_expr;
7860 tree ident, idx;
7861 location_t sizeof_arg_loc[3];
7862 tree sizeof_arg[3];
7863 unsigned int literal_zero_mask;
7864 unsigned int i;
7865 vec<tree, va_gc> *exprlist;
7866 vec<tree, va_gc> *origtypes = NULL;
7867 vec<location_t> arg_loc = vNULL;
7869 while (true)
7871 location_t op_loc = c_parser_peek_token (parser)->location;
7872 switch (c_parser_peek_token (parser)->type)
7874 case CPP_OPEN_SQUARE:
7875 /* Array reference. */
7876 c_parser_consume_token (parser);
7877 if (flag_cilkplus
7878 && c_parser_peek_token (parser)->type == CPP_COLON)
7879 /* If we are here, then we have something like this:
7880 Array [ : ]
7882 expr.value = c_parser_array_notation (expr_loc, parser, NULL_TREE,
7883 expr.value);
7884 else
7886 idx = c_parser_expression (parser).value;
7887 /* Here we have 3 options:
7888 1. Array [EXPR] -- Normal Array call.
7889 2. Array [EXPR : EXPR] -- Array notation without stride.
7890 3. Array [EXPR : EXPR : EXPR] -- Array notation with stride.
7892 For 1, we just handle it just like a normal array expression.
7893 For 2 and 3 we handle it like we handle array notations. The
7894 idx value we have above becomes the initial/start index.
7896 if (flag_cilkplus
7897 && c_parser_peek_token (parser)->type == CPP_COLON)
7898 expr.value = c_parser_array_notation (expr_loc, parser, idx,
7899 expr.value);
7900 else
7902 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
7903 "expected %<]%>");
7904 expr.value = build_array_ref (op_loc, expr.value, idx);
7907 expr.original_code = ERROR_MARK;
7908 expr.original_type = NULL;
7909 break;
7910 case CPP_OPEN_PAREN:
7911 /* Function call. */
7912 c_parser_consume_token (parser);
7913 for (i = 0; i < 3; i++)
7915 sizeof_arg[i] = NULL_TREE;
7916 sizeof_arg_loc[i] = UNKNOWN_LOCATION;
7918 literal_zero_mask = 0;
7919 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
7920 exprlist = NULL;
7921 else
7922 exprlist = c_parser_expr_list (parser, true, false, &origtypes,
7923 sizeof_arg_loc, sizeof_arg,
7924 &arg_loc, &literal_zero_mask);
7925 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7926 "expected %<)%>");
7927 orig_expr = expr;
7928 mark_exp_read (expr.value);
7929 if (warn_sizeof_pointer_memaccess)
7930 sizeof_pointer_memaccess_warning (sizeof_arg_loc,
7931 expr.value, exprlist,
7932 sizeof_arg,
7933 sizeof_ptr_memacc_comptypes);
7934 if (warn_memset_transposed_args
7935 && TREE_CODE (expr.value) == FUNCTION_DECL
7936 && DECL_BUILT_IN_CLASS (expr.value) == BUILT_IN_NORMAL
7937 && DECL_FUNCTION_CODE (expr.value) == BUILT_IN_MEMSET
7938 && vec_safe_length (exprlist) == 3
7939 && integer_zerop ((*exprlist)[2])
7940 && (literal_zero_mask & (1 << 2)) != 0
7941 && (!integer_zerop ((*exprlist)[1])
7942 || (literal_zero_mask & (1 << 1)) == 0))
7943 warning_at (expr_loc, OPT_Wmemset_transposed_args,
7944 "%<memset%> used with constant zero length parameter; "
7945 "this could be due to transposed parameters");
7947 expr.value
7948 = c_build_function_call_vec (expr_loc, arg_loc, expr.value,
7949 exprlist, origtypes);
7950 expr.original_code = ERROR_MARK;
7951 if (TREE_CODE (expr.value) == INTEGER_CST
7952 && TREE_CODE (orig_expr.value) == FUNCTION_DECL
7953 && DECL_BUILT_IN_CLASS (orig_expr.value) == BUILT_IN_NORMAL
7954 && DECL_FUNCTION_CODE (orig_expr.value) == BUILT_IN_CONSTANT_P)
7955 expr.original_code = C_MAYBE_CONST_EXPR;
7956 expr.original_type = NULL;
7957 if (exprlist)
7959 release_tree_vector (exprlist);
7960 release_tree_vector (origtypes);
7962 arg_loc.release ();
7963 break;
7964 case CPP_DOT:
7965 /* Structure element reference. */
7966 c_parser_consume_token (parser);
7967 expr = default_function_array_conversion (expr_loc, expr);
7968 if (c_parser_next_token_is (parser, CPP_NAME))
7969 ident = c_parser_peek_token (parser)->value;
7970 else
7972 c_parser_error (parser, "expected identifier");
7973 expr.value = error_mark_node;
7974 expr.original_code = ERROR_MARK;
7975 expr.original_type = NULL;
7976 return expr;
7978 c_parser_consume_token (parser);
7979 expr.value = build_component_ref (op_loc, expr.value, ident);
7980 expr.original_code = ERROR_MARK;
7981 if (TREE_CODE (expr.value) != COMPONENT_REF)
7982 expr.original_type = NULL;
7983 else
7985 /* Remember the original type of a bitfield. */
7986 tree field = TREE_OPERAND (expr.value, 1);
7987 if (TREE_CODE (field) != FIELD_DECL)
7988 expr.original_type = NULL;
7989 else
7990 expr.original_type = DECL_BIT_FIELD_TYPE (field);
7992 break;
7993 case CPP_DEREF:
7994 /* Structure element reference. */
7995 c_parser_consume_token (parser);
7996 expr = convert_lvalue_to_rvalue (expr_loc, expr, true, false);
7997 if (c_parser_next_token_is (parser, CPP_NAME))
7998 ident = c_parser_peek_token (parser)->value;
7999 else
8001 c_parser_error (parser, "expected identifier");
8002 expr.value = error_mark_node;
8003 expr.original_code = ERROR_MARK;
8004 expr.original_type = NULL;
8005 return expr;
8007 c_parser_consume_token (parser);
8008 expr.value = build_component_ref (op_loc,
8009 build_indirect_ref (op_loc,
8010 expr.value,
8011 RO_ARROW),
8012 ident);
8013 expr.original_code = ERROR_MARK;
8014 if (TREE_CODE (expr.value) != COMPONENT_REF)
8015 expr.original_type = NULL;
8016 else
8018 /* Remember the original type of a bitfield. */
8019 tree field = TREE_OPERAND (expr.value, 1);
8020 if (TREE_CODE (field) != FIELD_DECL)
8021 expr.original_type = NULL;
8022 else
8023 expr.original_type = DECL_BIT_FIELD_TYPE (field);
8025 break;
8026 case CPP_PLUS_PLUS:
8027 /* Postincrement. */
8028 c_parser_consume_token (parser);
8029 /* If the expressions have array notations, we expand them. */
8030 if (flag_cilkplus
8031 && TREE_CODE (expr.value) == ARRAY_NOTATION_REF)
8032 expr = fix_array_notation_expr (expr_loc, POSTINCREMENT_EXPR, expr);
8033 else
8035 expr = default_function_array_read_conversion (expr_loc, expr);
8036 expr.value = build_unary_op (op_loc,
8037 POSTINCREMENT_EXPR, expr.value, 0);
8039 expr.original_code = ERROR_MARK;
8040 expr.original_type = NULL;
8041 break;
8042 case CPP_MINUS_MINUS:
8043 /* Postdecrement. */
8044 c_parser_consume_token (parser);
8045 /* If the expressions have array notations, we expand them. */
8046 if (flag_cilkplus
8047 && TREE_CODE (expr.value) == ARRAY_NOTATION_REF)
8048 expr = fix_array_notation_expr (expr_loc, POSTDECREMENT_EXPR, expr);
8049 else
8051 expr = default_function_array_read_conversion (expr_loc, expr);
8052 expr.value = build_unary_op (op_loc,
8053 POSTDECREMENT_EXPR, expr.value, 0);
8055 expr.original_code = ERROR_MARK;
8056 expr.original_type = NULL;
8057 break;
8058 default:
8059 return expr;
8064 /* Parse an expression (C90 6.3.17, C99 6.5.17).
8066 expression:
8067 assignment-expression
8068 expression , assignment-expression
8071 static struct c_expr
8072 c_parser_expression (c_parser *parser)
8074 location_t tloc = c_parser_peek_token (parser)->location;
8075 struct c_expr expr;
8076 expr = c_parser_expr_no_commas (parser, NULL);
8077 if (c_parser_next_token_is (parser, CPP_COMMA))
8078 expr = convert_lvalue_to_rvalue (tloc, expr, true, false);
8079 while (c_parser_next_token_is (parser, CPP_COMMA))
8081 struct c_expr next;
8082 tree lhsval;
8083 location_t loc = c_parser_peek_token (parser)->location;
8084 location_t expr_loc;
8085 c_parser_consume_token (parser);
8086 expr_loc = c_parser_peek_token (parser)->location;
8087 lhsval = expr.value;
8088 while (TREE_CODE (lhsval) == COMPOUND_EXPR)
8089 lhsval = TREE_OPERAND (lhsval, 1);
8090 if (DECL_P (lhsval) || handled_component_p (lhsval))
8091 mark_exp_read (lhsval);
8092 next = c_parser_expr_no_commas (parser, NULL);
8093 next = convert_lvalue_to_rvalue (expr_loc, next, true, false);
8094 expr.value = build_compound_expr (loc, expr.value, next.value);
8095 expr.original_code = COMPOUND_EXPR;
8096 expr.original_type = next.original_type;
8098 return expr;
8101 /* Parse an expression and convert functions or arrays to pointers and
8102 lvalues to rvalues. */
8104 static struct c_expr
8105 c_parser_expression_conv (c_parser *parser)
8107 struct c_expr expr;
8108 location_t loc = c_parser_peek_token (parser)->location;
8109 expr = c_parser_expression (parser);
8110 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
8111 return expr;
8114 /* Helper function of c_parser_expr_list. Check if IDXth (0 based)
8115 argument is a literal zero alone and if so, set it in literal_zero_mask. */
8117 static inline void
8118 c_parser_check_literal_zero (c_parser *parser, unsigned *literal_zero_mask,
8119 unsigned int idx)
8121 if (idx >= HOST_BITS_PER_INT)
8122 return;
8124 c_token *tok = c_parser_peek_token (parser);
8125 switch (tok->type)
8127 case CPP_NUMBER:
8128 case CPP_CHAR:
8129 case CPP_WCHAR:
8130 case CPP_CHAR16:
8131 case CPP_CHAR32:
8132 /* If a parameter is literal zero alone, remember it
8133 for -Wmemset-transposed-args warning. */
8134 if (integer_zerop (tok->value)
8135 && !TREE_OVERFLOW (tok->value)
8136 && (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
8137 || c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_PAREN))
8138 *literal_zero_mask |= 1U << idx;
8139 default:
8140 break;
8144 /* Parse a non-empty list of expressions. If CONVERT_P, convert
8145 functions and arrays to pointers and lvalues to rvalues. If
8146 FOLD_P, fold the expressions. If LOCATIONS is non-NULL, save the
8147 locations of function arguments into this vector.
8149 nonempty-expr-list:
8150 assignment-expression
8151 nonempty-expr-list , assignment-expression
8154 static vec<tree, va_gc> *
8155 c_parser_expr_list (c_parser *parser, bool convert_p, bool fold_p,
8156 vec<tree, va_gc> **p_orig_types,
8157 location_t *sizeof_arg_loc, tree *sizeof_arg,
8158 vec<location_t> *locations,
8159 unsigned int *literal_zero_mask)
8161 vec<tree, va_gc> *ret;
8162 vec<tree, va_gc> *orig_types;
8163 struct c_expr expr;
8164 location_t loc = c_parser_peek_token (parser)->location;
8165 location_t cur_sizeof_arg_loc = UNKNOWN_LOCATION;
8166 unsigned int idx = 0;
8168 ret = make_tree_vector ();
8169 if (p_orig_types == NULL)
8170 orig_types = NULL;
8171 else
8172 orig_types = make_tree_vector ();
8174 if (sizeof_arg != NULL
8175 && c_parser_next_token_is_keyword (parser, RID_SIZEOF))
8176 cur_sizeof_arg_loc = c_parser_peek_2nd_token (parser)->location;
8177 if (literal_zero_mask)
8178 c_parser_check_literal_zero (parser, literal_zero_mask, 0);
8179 expr = c_parser_expr_no_commas (parser, NULL);
8180 if (convert_p)
8181 expr = convert_lvalue_to_rvalue (loc, expr, true, true);
8182 if (fold_p)
8183 expr.value = c_fully_fold (expr.value, false, NULL);
8184 ret->quick_push (expr.value);
8185 if (orig_types)
8186 orig_types->quick_push (expr.original_type);
8187 if (locations)
8188 locations->safe_push (loc);
8189 if (sizeof_arg != NULL
8190 && cur_sizeof_arg_loc != UNKNOWN_LOCATION
8191 && expr.original_code == SIZEOF_EXPR)
8193 sizeof_arg[0] = c_last_sizeof_arg;
8194 sizeof_arg_loc[0] = cur_sizeof_arg_loc;
8196 while (c_parser_next_token_is (parser, CPP_COMMA))
8198 c_parser_consume_token (parser);
8199 loc = c_parser_peek_token (parser)->location;
8200 if (sizeof_arg != NULL
8201 && c_parser_next_token_is_keyword (parser, RID_SIZEOF))
8202 cur_sizeof_arg_loc = c_parser_peek_2nd_token (parser)->location;
8203 else
8204 cur_sizeof_arg_loc = UNKNOWN_LOCATION;
8205 if (literal_zero_mask)
8206 c_parser_check_literal_zero (parser, literal_zero_mask, idx + 1);
8207 expr = c_parser_expr_no_commas (parser, NULL);
8208 if (convert_p)
8209 expr = convert_lvalue_to_rvalue (loc, expr, true, true);
8210 if (fold_p)
8211 expr.value = c_fully_fold (expr.value, false, NULL);
8212 vec_safe_push (ret, expr.value);
8213 if (orig_types)
8214 vec_safe_push (orig_types, expr.original_type);
8215 if (locations)
8216 locations->safe_push (loc);
8217 if (++idx < 3
8218 && sizeof_arg != NULL
8219 && cur_sizeof_arg_loc != UNKNOWN_LOCATION
8220 && expr.original_code == SIZEOF_EXPR)
8222 sizeof_arg[idx] = c_last_sizeof_arg;
8223 sizeof_arg_loc[idx] = cur_sizeof_arg_loc;
8226 if (orig_types)
8227 *p_orig_types = orig_types;
8228 return ret;
8231 /* Parse Objective-C-specific constructs. */
8233 /* Parse an objc-class-definition.
8235 objc-class-definition:
8236 @interface identifier objc-superclass[opt] objc-protocol-refs[opt]
8237 objc-class-instance-variables[opt] objc-methodprotolist @end
8238 @implementation identifier objc-superclass[opt]
8239 objc-class-instance-variables[opt]
8240 @interface identifier ( identifier ) objc-protocol-refs[opt]
8241 objc-methodprotolist @end
8242 @interface identifier ( ) objc-protocol-refs[opt]
8243 objc-methodprotolist @end
8244 @implementation identifier ( identifier )
8246 objc-superclass:
8247 : identifier
8249 "@interface identifier (" must start "@interface identifier (
8250 identifier ) ...": objc-methodprotolist in the first production may
8251 not start with a parenthesized identifier as a declarator of a data
8252 definition with no declaration specifiers if the objc-superclass,
8253 objc-protocol-refs and objc-class-instance-variables are omitted. */
8255 static void
8256 c_parser_objc_class_definition (c_parser *parser, tree attributes)
8258 bool iface_p;
8259 tree id1;
8260 tree superclass;
8261 if (c_parser_next_token_is_keyword (parser, RID_AT_INTERFACE))
8262 iface_p = true;
8263 else if (c_parser_next_token_is_keyword (parser, RID_AT_IMPLEMENTATION))
8264 iface_p = false;
8265 else
8266 gcc_unreachable ();
8268 c_parser_consume_token (parser);
8269 if (c_parser_next_token_is_not (parser, CPP_NAME))
8271 c_parser_error (parser, "expected identifier");
8272 return;
8274 id1 = c_parser_peek_token (parser)->value;
8275 c_parser_consume_token (parser);
8276 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
8278 /* We have a category or class extension. */
8279 tree id2;
8280 tree proto = NULL_TREE;
8281 c_parser_consume_token (parser);
8282 if (c_parser_next_token_is_not (parser, CPP_NAME))
8284 if (iface_p && c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
8286 /* We have a class extension. */
8287 id2 = NULL_TREE;
8289 else
8291 c_parser_error (parser, "expected identifier or %<)%>");
8292 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
8293 return;
8296 else
8298 id2 = c_parser_peek_token (parser)->value;
8299 c_parser_consume_token (parser);
8301 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8302 if (!iface_p)
8304 objc_start_category_implementation (id1, id2);
8305 return;
8307 if (c_parser_next_token_is (parser, CPP_LESS))
8308 proto = c_parser_objc_protocol_refs (parser);
8309 objc_start_category_interface (id1, id2, proto, attributes);
8310 c_parser_objc_methodprotolist (parser);
8311 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
8312 objc_finish_interface ();
8313 return;
8315 if (c_parser_next_token_is (parser, CPP_COLON))
8317 c_parser_consume_token (parser);
8318 if (c_parser_next_token_is_not (parser, CPP_NAME))
8320 c_parser_error (parser, "expected identifier");
8321 return;
8323 superclass = c_parser_peek_token (parser)->value;
8324 c_parser_consume_token (parser);
8326 else
8327 superclass = NULL_TREE;
8328 if (iface_p)
8330 tree proto = NULL_TREE;
8331 if (c_parser_next_token_is (parser, CPP_LESS))
8332 proto = c_parser_objc_protocol_refs (parser);
8333 objc_start_class_interface (id1, superclass, proto, attributes);
8335 else
8336 objc_start_class_implementation (id1, superclass);
8337 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
8338 c_parser_objc_class_instance_variables (parser);
8339 if (iface_p)
8341 objc_continue_interface ();
8342 c_parser_objc_methodprotolist (parser);
8343 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
8344 objc_finish_interface ();
8346 else
8348 objc_continue_implementation ();
8349 return;
8353 /* Parse objc-class-instance-variables.
8355 objc-class-instance-variables:
8356 { objc-instance-variable-decl-list[opt] }
8358 objc-instance-variable-decl-list:
8359 objc-visibility-spec
8360 objc-instance-variable-decl ;
8362 objc-instance-variable-decl-list objc-visibility-spec
8363 objc-instance-variable-decl-list objc-instance-variable-decl ;
8364 objc-instance-variable-decl-list ;
8366 objc-visibility-spec:
8367 @private
8368 @protected
8369 @public
8371 objc-instance-variable-decl:
8372 struct-declaration
8375 static void
8376 c_parser_objc_class_instance_variables (c_parser *parser)
8378 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
8379 c_parser_consume_token (parser);
8380 while (c_parser_next_token_is_not (parser, CPP_EOF))
8382 tree decls;
8383 /* Parse any stray semicolon. */
8384 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
8386 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
8387 "extra semicolon");
8388 c_parser_consume_token (parser);
8389 continue;
8391 /* Stop if at the end of the instance variables. */
8392 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
8394 c_parser_consume_token (parser);
8395 break;
8397 /* Parse any objc-visibility-spec. */
8398 if (c_parser_next_token_is_keyword (parser, RID_AT_PRIVATE))
8400 c_parser_consume_token (parser);
8401 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
8402 continue;
8404 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROTECTED))
8406 c_parser_consume_token (parser);
8407 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
8408 continue;
8410 else if (c_parser_next_token_is_keyword (parser, RID_AT_PUBLIC))
8412 c_parser_consume_token (parser);
8413 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
8414 continue;
8416 else if (c_parser_next_token_is_keyword (parser, RID_AT_PACKAGE))
8418 c_parser_consume_token (parser);
8419 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
8420 continue;
8422 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
8424 c_parser_pragma (parser, pragma_external);
8425 continue;
8428 /* Parse some comma-separated declarations. */
8429 decls = c_parser_struct_declaration (parser);
8430 if (decls == NULL)
8432 /* There is a syntax error. We want to skip the offending
8433 tokens up to the next ';' (included) or '}'
8434 (excluded). */
8436 /* First, skip manually a ')' or ']'. This is because they
8437 reduce the nesting level, so c_parser_skip_until_found()
8438 wouldn't be able to skip past them. */
8439 c_token *token = c_parser_peek_token (parser);
8440 if (token->type == CPP_CLOSE_PAREN || token->type == CPP_CLOSE_SQUARE)
8441 c_parser_consume_token (parser);
8443 /* Then, do the standard skipping. */
8444 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8446 /* We hopefully recovered. Start normal parsing again. */
8447 parser->error = false;
8448 continue;
8450 else
8452 /* Comma-separated instance variables are chained together
8453 in reverse order; add them one by one. */
8454 tree ivar = nreverse (decls);
8455 for (; ivar; ivar = DECL_CHAIN (ivar))
8456 objc_add_instance_variable (copy_node (ivar));
8458 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8462 /* Parse an objc-class-declaration.
8464 objc-class-declaration:
8465 @class identifier-list ;
8468 static void
8469 c_parser_objc_class_declaration (c_parser *parser)
8471 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_CLASS));
8472 c_parser_consume_token (parser);
8473 /* Any identifiers, including those declared as type names, are OK
8474 here. */
8475 while (true)
8477 tree id;
8478 if (c_parser_next_token_is_not (parser, CPP_NAME))
8480 c_parser_error (parser, "expected identifier");
8481 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8482 parser->error = false;
8483 return;
8485 id = c_parser_peek_token (parser)->value;
8486 objc_declare_class (id);
8487 c_parser_consume_token (parser);
8488 if (c_parser_next_token_is (parser, CPP_COMMA))
8489 c_parser_consume_token (parser);
8490 else
8491 break;
8493 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8496 /* Parse an objc-alias-declaration.
8498 objc-alias-declaration:
8499 @compatibility_alias identifier identifier ;
8502 static void
8503 c_parser_objc_alias_declaration (c_parser *parser)
8505 tree id1, id2;
8506 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_ALIAS));
8507 c_parser_consume_token (parser);
8508 if (c_parser_next_token_is_not (parser, CPP_NAME))
8510 c_parser_error (parser, "expected identifier");
8511 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8512 return;
8514 id1 = c_parser_peek_token (parser)->value;
8515 c_parser_consume_token (parser);
8516 if (c_parser_next_token_is_not (parser, CPP_NAME))
8518 c_parser_error (parser, "expected identifier");
8519 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8520 return;
8522 id2 = c_parser_peek_token (parser)->value;
8523 c_parser_consume_token (parser);
8524 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8525 objc_declare_alias (id1, id2);
8528 /* Parse an objc-protocol-definition.
8530 objc-protocol-definition:
8531 @protocol identifier objc-protocol-refs[opt] objc-methodprotolist @end
8532 @protocol identifier-list ;
8534 "@protocol identifier ;" should be resolved as "@protocol
8535 identifier-list ;": objc-methodprotolist may not start with a
8536 semicolon in the first alternative if objc-protocol-refs are
8537 omitted. */
8539 static void
8540 c_parser_objc_protocol_definition (c_parser *parser, tree attributes)
8542 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROTOCOL));
8544 c_parser_consume_token (parser);
8545 if (c_parser_next_token_is_not (parser, CPP_NAME))
8547 c_parser_error (parser, "expected identifier");
8548 return;
8550 if (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
8551 || c_parser_peek_2nd_token (parser)->type == CPP_SEMICOLON)
8553 /* Any identifiers, including those declared as type names, are
8554 OK here. */
8555 while (true)
8557 tree id;
8558 if (c_parser_next_token_is_not (parser, CPP_NAME))
8560 c_parser_error (parser, "expected identifier");
8561 break;
8563 id = c_parser_peek_token (parser)->value;
8564 objc_declare_protocol (id, attributes);
8565 c_parser_consume_token (parser);
8566 if (c_parser_next_token_is (parser, CPP_COMMA))
8567 c_parser_consume_token (parser);
8568 else
8569 break;
8571 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8573 else
8575 tree id = c_parser_peek_token (parser)->value;
8576 tree proto = NULL_TREE;
8577 c_parser_consume_token (parser);
8578 if (c_parser_next_token_is (parser, CPP_LESS))
8579 proto = c_parser_objc_protocol_refs (parser);
8580 parser->objc_pq_context = true;
8581 objc_start_protocol (id, proto, attributes);
8582 c_parser_objc_methodprotolist (parser);
8583 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
8584 parser->objc_pq_context = false;
8585 objc_finish_interface ();
8589 /* Parse an objc-method-type.
8591 objc-method-type:
8595 Return true if it is a class method (+) and false if it is
8596 an instance method (-).
8598 static inline bool
8599 c_parser_objc_method_type (c_parser *parser)
8601 switch (c_parser_peek_token (parser)->type)
8603 case CPP_PLUS:
8604 c_parser_consume_token (parser);
8605 return true;
8606 case CPP_MINUS:
8607 c_parser_consume_token (parser);
8608 return false;
8609 default:
8610 gcc_unreachable ();
8614 /* Parse an objc-method-definition.
8616 objc-method-definition:
8617 objc-method-type objc-method-decl ;[opt] compound-statement
8620 static void
8621 c_parser_objc_method_definition (c_parser *parser)
8623 bool is_class_method = c_parser_objc_method_type (parser);
8624 tree decl, attributes = NULL_TREE, expr = NULL_TREE;
8625 parser->objc_pq_context = true;
8626 decl = c_parser_objc_method_decl (parser, is_class_method, &attributes,
8627 &expr);
8628 if (decl == error_mark_node)
8629 return; /* Bail here. */
8631 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
8633 c_parser_consume_token (parser);
8634 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
8635 "extra semicolon in method definition specified");
8638 if (!c_parser_next_token_is (parser, CPP_OPEN_BRACE))
8640 c_parser_error (parser, "expected %<{%>");
8641 return;
8644 parser->objc_pq_context = false;
8645 if (objc_start_method_definition (is_class_method, decl, attributes, expr))
8647 add_stmt (c_parser_compound_statement (parser));
8648 objc_finish_method_definition (current_function_decl);
8650 else
8652 /* This code is executed when we find a method definition
8653 outside of an @implementation context (or invalid for other
8654 reasons). Parse the method (to keep going) but do not emit
8655 any code.
8657 c_parser_compound_statement (parser);
8661 /* Parse an objc-methodprotolist.
8663 objc-methodprotolist:
8664 empty
8665 objc-methodprotolist objc-methodproto
8666 objc-methodprotolist declaration
8667 objc-methodprotolist ;
8668 @optional
8669 @required
8671 The declaration is a data definition, which may be missing
8672 declaration specifiers under the same rules and diagnostics as
8673 other data definitions outside functions, and the stray semicolon
8674 is diagnosed the same way as a stray semicolon outside a
8675 function. */
8677 static void
8678 c_parser_objc_methodprotolist (c_parser *parser)
8680 while (true)
8682 /* The list is terminated by @end. */
8683 switch (c_parser_peek_token (parser)->type)
8685 case CPP_SEMICOLON:
8686 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
8687 "ISO C does not allow extra %<;%> outside of a function");
8688 c_parser_consume_token (parser);
8689 break;
8690 case CPP_PLUS:
8691 case CPP_MINUS:
8692 c_parser_objc_methodproto (parser);
8693 break;
8694 case CPP_PRAGMA:
8695 c_parser_pragma (parser, pragma_external);
8696 break;
8697 case CPP_EOF:
8698 return;
8699 default:
8700 if (c_parser_next_token_is_keyword (parser, RID_AT_END))
8701 return;
8702 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROPERTY))
8703 c_parser_objc_at_property_declaration (parser);
8704 else if (c_parser_next_token_is_keyword (parser, RID_AT_OPTIONAL))
8706 objc_set_method_opt (true);
8707 c_parser_consume_token (parser);
8709 else if (c_parser_next_token_is_keyword (parser, RID_AT_REQUIRED))
8711 objc_set_method_opt (false);
8712 c_parser_consume_token (parser);
8714 else
8715 c_parser_declaration_or_fndef (parser, false, false, true,
8716 false, true, NULL, vNULL);
8717 break;
8722 /* Parse an objc-methodproto.
8724 objc-methodproto:
8725 objc-method-type objc-method-decl ;
8728 static void
8729 c_parser_objc_methodproto (c_parser *parser)
8731 bool is_class_method = c_parser_objc_method_type (parser);
8732 tree decl, attributes = NULL_TREE;
8734 /* Remember protocol qualifiers in prototypes. */
8735 parser->objc_pq_context = true;
8736 decl = c_parser_objc_method_decl (parser, is_class_method, &attributes,
8737 NULL);
8738 /* Forget protocol qualifiers now. */
8739 parser->objc_pq_context = false;
8741 /* Do not allow the presence of attributes to hide an erroneous
8742 method implementation in the interface section. */
8743 if (!c_parser_next_token_is (parser, CPP_SEMICOLON))
8745 c_parser_error (parser, "expected %<;%>");
8746 return;
8749 if (decl != error_mark_node)
8750 objc_add_method_declaration (is_class_method, decl, attributes);
8752 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8755 /* If we are at a position that method attributes may be present, check that
8756 there are not any parsed already (a syntax error) and then collect any
8757 specified at the current location. Finally, if new attributes were present,
8758 check that the next token is legal ( ';' for decls and '{' for defs). */
8760 static bool
8761 c_parser_objc_maybe_method_attributes (c_parser* parser, tree* attributes)
8763 bool bad = false;
8764 if (*attributes)
8766 c_parser_error (parser,
8767 "method attributes must be specified at the end only");
8768 *attributes = NULL_TREE;
8769 bad = true;
8772 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
8773 *attributes = c_parser_attributes (parser);
8775 /* If there were no attributes here, just report any earlier error. */
8776 if (*attributes == NULL_TREE || bad)
8777 return bad;
8779 /* If the attributes are followed by a ; or {, then just report any earlier
8780 error. */
8781 if (c_parser_next_token_is (parser, CPP_SEMICOLON)
8782 || c_parser_next_token_is (parser, CPP_OPEN_BRACE))
8783 return bad;
8785 /* We've got attributes, but not at the end. */
8786 c_parser_error (parser,
8787 "expected %<;%> or %<{%> after method attribute definition");
8788 return true;
8791 /* Parse an objc-method-decl.
8793 objc-method-decl:
8794 ( objc-type-name ) objc-selector
8795 objc-selector
8796 ( objc-type-name ) objc-keyword-selector objc-optparmlist
8797 objc-keyword-selector objc-optparmlist
8798 attributes
8800 objc-keyword-selector:
8801 objc-keyword-decl
8802 objc-keyword-selector objc-keyword-decl
8804 objc-keyword-decl:
8805 objc-selector : ( objc-type-name ) identifier
8806 objc-selector : identifier
8807 : ( objc-type-name ) identifier
8808 : identifier
8810 objc-optparmlist:
8811 objc-optparms objc-optellipsis
8813 objc-optparms:
8814 empty
8815 objc-opt-parms , parameter-declaration
8817 objc-optellipsis:
8818 empty
8819 , ...
8822 static tree
8823 c_parser_objc_method_decl (c_parser *parser, bool is_class_method,
8824 tree *attributes, tree *expr)
8826 tree type = NULL_TREE;
8827 tree sel;
8828 tree parms = NULL_TREE;
8829 bool ellipsis = false;
8830 bool attr_err = false;
8832 *attributes = NULL_TREE;
8833 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
8835 c_parser_consume_token (parser);
8836 type = c_parser_objc_type_name (parser);
8837 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8839 sel = c_parser_objc_selector (parser);
8840 /* If there is no selector, or a colon follows, we have an
8841 objc-keyword-selector. If there is a selector, and a colon does
8842 not follow, that selector ends the objc-method-decl. */
8843 if (!sel || c_parser_next_token_is (parser, CPP_COLON))
8845 tree tsel = sel;
8846 tree list = NULL_TREE;
8847 while (true)
8849 tree atype = NULL_TREE, id, keyworddecl;
8850 tree param_attr = NULL_TREE;
8851 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
8852 break;
8853 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
8855 c_parser_consume_token (parser);
8856 atype = c_parser_objc_type_name (parser);
8857 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
8858 "expected %<)%>");
8860 /* New ObjC allows attributes on method parameters. */
8861 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
8862 param_attr = c_parser_attributes (parser);
8863 if (c_parser_next_token_is_not (parser, CPP_NAME))
8865 c_parser_error (parser, "expected identifier");
8866 return error_mark_node;
8868 id = c_parser_peek_token (parser)->value;
8869 c_parser_consume_token (parser);
8870 keyworddecl = objc_build_keyword_decl (tsel, atype, id, param_attr);
8871 list = chainon (list, keyworddecl);
8872 tsel = c_parser_objc_selector (parser);
8873 if (!tsel && c_parser_next_token_is_not (parser, CPP_COLON))
8874 break;
8877 attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
8879 /* Parse the optional parameter list. Optional Objective-C
8880 method parameters follow the C syntax, and may include '...'
8881 to denote a variable number of arguments. */
8882 parms = make_node (TREE_LIST);
8883 while (c_parser_next_token_is (parser, CPP_COMMA))
8885 struct c_parm *parm;
8886 c_parser_consume_token (parser);
8887 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
8889 ellipsis = true;
8890 c_parser_consume_token (parser);
8891 attr_err |= c_parser_objc_maybe_method_attributes
8892 (parser, attributes) ;
8893 break;
8895 parm = c_parser_parameter_declaration (parser, NULL_TREE);
8896 if (parm == NULL)
8897 break;
8898 parms = chainon (parms,
8899 build_tree_list (NULL_TREE, grokparm (parm, expr)));
8901 sel = list;
8903 else
8904 attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
8906 if (sel == NULL)
8908 c_parser_error (parser, "objective-c method declaration is expected");
8909 return error_mark_node;
8912 if (attr_err)
8913 return error_mark_node;
8915 return objc_build_method_signature (is_class_method, type, sel, parms, ellipsis);
8918 /* Parse an objc-type-name.
8920 objc-type-name:
8921 objc-type-qualifiers[opt] type-name
8922 objc-type-qualifiers[opt]
8924 objc-type-qualifiers:
8925 objc-type-qualifier
8926 objc-type-qualifiers objc-type-qualifier
8928 objc-type-qualifier: one of
8929 in out inout bycopy byref oneway
8932 static tree
8933 c_parser_objc_type_name (c_parser *parser)
8935 tree quals = NULL_TREE;
8936 struct c_type_name *type_name = NULL;
8937 tree type = NULL_TREE;
8938 while (true)
8940 c_token *token = c_parser_peek_token (parser);
8941 if (token->type == CPP_KEYWORD
8942 && (token->keyword == RID_IN
8943 || token->keyword == RID_OUT
8944 || token->keyword == RID_INOUT
8945 || token->keyword == RID_BYCOPY
8946 || token->keyword == RID_BYREF
8947 || token->keyword == RID_ONEWAY))
8949 quals = chainon (build_tree_list (NULL_TREE, token->value), quals);
8950 c_parser_consume_token (parser);
8952 else
8953 break;
8955 if (c_parser_next_tokens_start_typename (parser, cla_prefer_type))
8956 type_name = c_parser_type_name (parser);
8957 if (type_name)
8958 type = groktypename (type_name, NULL, NULL);
8960 /* If the type is unknown, and error has already been produced and
8961 we need to recover from the error. In that case, use NULL_TREE
8962 for the type, as if no type had been specified; this will use the
8963 default type ('id') which is good for error recovery. */
8964 if (type == error_mark_node)
8965 type = NULL_TREE;
8967 return build_tree_list (quals, type);
8970 /* Parse objc-protocol-refs.
8972 objc-protocol-refs:
8973 < identifier-list >
8976 static tree
8977 c_parser_objc_protocol_refs (c_parser *parser)
8979 tree list = NULL_TREE;
8980 gcc_assert (c_parser_next_token_is (parser, CPP_LESS));
8981 c_parser_consume_token (parser);
8982 /* Any identifiers, including those declared as type names, are OK
8983 here. */
8984 while (true)
8986 tree id;
8987 if (c_parser_next_token_is_not (parser, CPP_NAME))
8989 c_parser_error (parser, "expected identifier");
8990 break;
8992 id = c_parser_peek_token (parser)->value;
8993 list = chainon (list, build_tree_list (NULL_TREE, id));
8994 c_parser_consume_token (parser);
8995 if (c_parser_next_token_is (parser, CPP_COMMA))
8996 c_parser_consume_token (parser);
8997 else
8998 break;
9000 c_parser_require (parser, CPP_GREATER, "expected %<>%>");
9001 return list;
9004 /* Parse an objc-try-catch-finally-statement.
9006 objc-try-catch-finally-statement:
9007 @try compound-statement objc-catch-list[opt]
9008 @try compound-statement objc-catch-list[opt] @finally compound-statement
9010 objc-catch-list:
9011 @catch ( objc-catch-parameter-declaration ) compound-statement
9012 objc-catch-list @catch ( objc-catch-parameter-declaration ) compound-statement
9014 objc-catch-parameter-declaration:
9015 parameter-declaration
9016 '...'
9018 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
9020 PS: This function is identical to cp_parser_objc_try_catch_finally_statement
9021 for C++. Keep them in sync. */
9023 static void
9024 c_parser_objc_try_catch_finally_statement (c_parser *parser)
9026 location_t location;
9027 tree stmt;
9029 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_TRY));
9030 c_parser_consume_token (parser);
9031 location = c_parser_peek_token (parser)->location;
9032 objc_maybe_warn_exceptions (location);
9033 stmt = c_parser_compound_statement (parser);
9034 objc_begin_try_stmt (location, stmt);
9036 while (c_parser_next_token_is_keyword (parser, RID_AT_CATCH))
9038 struct c_parm *parm;
9039 tree parameter_declaration = error_mark_node;
9040 bool seen_open_paren = false;
9042 c_parser_consume_token (parser);
9043 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
9044 seen_open_paren = true;
9045 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
9047 /* We have "@catch (...)" (where the '...' are literally
9048 what is in the code). Skip the '...'.
9049 parameter_declaration is set to NULL_TREE, and
9050 objc_being_catch_clauses() knows that that means
9051 '...'. */
9052 c_parser_consume_token (parser);
9053 parameter_declaration = NULL_TREE;
9055 else
9057 /* We have "@catch (NSException *exception)" or something
9058 like that. Parse the parameter declaration. */
9059 parm = c_parser_parameter_declaration (parser, NULL_TREE);
9060 if (parm == NULL)
9061 parameter_declaration = error_mark_node;
9062 else
9063 parameter_declaration = grokparm (parm, NULL);
9065 if (seen_open_paren)
9066 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9067 else
9069 /* If there was no open parenthesis, we are recovering from
9070 an error, and we are trying to figure out what mistake
9071 the user has made. */
9073 /* If there is an immediate closing parenthesis, the user
9074 probably forgot the opening one (ie, they typed "@catch
9075 NSException *e)". Parse the closing parenthesis and keep
9076 going. */
9077 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
9078 c_parser_consume_token (parser);
9080 /* If these is no immediate closing parenthesis, the user
9081 probably doesn't know that parenthesis are required at
9082 all (ie, they typed "@catch NSException *e"). So, just
9083 forget about the closing parenthesis and keep going. */
9085 objc_begin_catch_clause (parameter_declaration);
9086 if (c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
9087 c_parser_compound_statement_nostart (parser);
9088 objc_finish_catch_clause ();
9090 if (c_parser_next_token_is_keyword (parser, RID_AT_FINALLY))
9092 c_parser_consume_token (parser);
9093 location = c_parser_peek_token (parser)->location;
9094 stmt = c_parser_compound_statement (parser);
9095 objc_build_finally_clause (location, stmt);
9097 objc_finish_try_stmt ();
9100 /* Parse an objc-synchronized-statement.
9102 objc-synchronized-statement:
9103 @synchronized ( expression ) compound-statement
9106 static void
9107 c_parser_objc_synchronized_statement (c_parser *parser)
9109 location_t loc;
9110 tree expr, stmt;
9111 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNCHRONIZED));
9112 c_parser_consume_token (parser);
9113 loc = c_parser_peek_token (parser)->location;
9114 objc_maybe_warn_exceptions (loc);
9115 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
9117 struct c_expr ce = c_parser_expression (parser);
9118 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
9119 expr = ce.value;
9120 expr = c_fully_fold (expr, false, NULL);
9121 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9123 else
9124 expr = error_mark_node;
9125 stmt = c_parser_compound_statement (parser);
9126 objc_build_synchronized (loc, expr, stmt);
9129 /* Parse an objc-selector; return NULL_TREE without an error if the
9130 next token is not an objc-selector.
9132 objc-selector:
9133 identifier
9134 one of
9135 enum struct union if else while do for switch case default
9136 break continue return goto asm sizeof typeof __alignof
9137 unsigned long const short volatile signed restrict _Complex
9138 in out inout bycopy byref oneway int char float double void _Bool
9139 _Atomic
9141 ??? Why this selection of keywords but not, for example, storage
9142 class specifiers? */
9144 static tree
9145 c_parser_objc_selector (c_parser *parser)
9147 c_token *token = c_parser_peek_token (parser);
9148 tree value = token->value;
9149 if (token->type == CPP_NAME)
9151 c_parser_consume_token (parser);
9152 return value;
9154 if (token->type != CPP_KEYWORD)
9155 return NULL_TREE;
9156 switch (token->keyword)
9158 case RID_ENUM:
9159 case RID_STRUCT:
9160 case RID_UNION:
9161 case RID_IF:
9162 case RID_ELSE:
9163 case RID_WHILE:
9164 case RID_DO:
9165 case RID_FOR:
9166 case RID_SWITCH:
9167 case RID_CASE:
9168 case RID_DEFAULT:
9169 case RID_BREAK:
9170 case RID_CONTINUE:
9171 case RID_RETURN:
9172 case RID_GOTO:
9173 case RID_ASM:
9174 case RID_SIZEOF:
9175 case RID_TYPEOF:
9176 case RID_ALIGNOF:
9177 case RID_UNSIGNED:
9178 case RID_LONG:
9179 case RID_CONST:
9180 case RID_SHORT:
9181 case RID_VOLATILE:
9182 case RID_SIGNED:
9183 case RID_RESTRICT:
9184 case RID_COMPLEX:
9185 case RID_IN:
9186 case RID_OUT:
9187 case RID_INOUT:
9188 case RID_BYCOPY:
9189 case RID_BYREF:
9190 case RID_ONEWAY:
9191 case RID_INT:
9192 case RID_CHAR:
9193 case RID_FLOAT:
9194 case RID_DOUBLE:
9195 case RID_VOID:
9196 case RID_BOOL:
9197 case RID_ATOMIC:
9198 case RID_AUTO_TYPE:
9199 case RID_INT_N_0:
9200 case RID_INT_N_1:
9201 case RID_INT_N_2:
9202 case RID_INT_N_3:
9203 c_parser_consume_token (parser);
9204 return value;
9205 default:
9206 return NULL_TREE;
9210 /* Parse an objc-selector-arg.
9212 objc-selector-arg:
9213 objc-selector
9214 objc-keywordname-list
9216 objc-keywordname-list:
9217 objc-keywordname
9218 objc-keywordname-list objc-keywordname
9220 objc-keywordname:
9221 objc-selector :
9225 static tree
9226 c_parser_objc_selector_arg (c_parser *parser)
9228 tree sel = c_parser_objc_selector (parser);
9229 tree list = NULL_TREE;
9230 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
9231 return sel;
9232 while (true)
9234 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
9235 return list;
9236 list = chainon (list, build_tree_list (sel, NULL_TREE));
9237 sel = c_parser_objc_selector (parser);
9238 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
9239 break;
9241 return list;
9244 /* Parse an objc-receiver.
9246 objc-receiver:
9247 expression
9248 class-name
9249 type-name
9252 static tree
9253 c_parser_objc_receiver (c_parser *parser)
9255 location_t loc = c_parser_peek_token (parser)->location;
9257 if (c_parser_peek_token (parser)->type == CPP_NAME
9258 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
9259 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
9261 tree id = c_parser_peek_token (parser)->value;
9262 c_parser_consume_token (parser);
9263 return objc_get_class_reference (id);
9265 struct c_expr ce = c_parser_expression (parser);
9266 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
9267 return c_fully_fold (ce.value, false, NULL);
9270 /* Parse objc-message-args.
9272 objc-message-args:
9273 objc-selector
9274 objc-keywordarg-list
9276 objc-keywordarg-list:
9277 objc-keywordarg
9278 objc-keywordarg-list objc-keywordarg
9280 objc-keywordarg:
9281 objc-selector : objc-keywordexpr
9282 : objc-keywordexpr
9285 static tree
9286 c_parser_objc_message_args (c_parser *parser)
9288 tree sel = c_parser_objc_selector (parser);
9289 tree list = NULL_TREE;
9290 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
9291 return sel;
9292 while (true)
9294 tree keywordexpr;
9295 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
9296 return error_mark_node;
9297 keywordexpr = c_parser_objc_keywordexpr (parser);
9298 list = chainon (list, build_tree_list (sel, keywordexpr));
9299 sel = c_parser_objc_selector (parser);
9300 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
9301 break;
9303 return list;
9306 /* Parse an objc-keywordexpr.
9308 objc-keywordexpr:
9309 nonempty-expr-list
9312 static tree
9313 c_parser_objc_keywordexpr (c_parser *parser)
9315 tree ret;
9316 vec<tree, va_gc> *expr_list = c_parser_expr_list (parser, true, true,
9317 NULL, NULL, NULL, NULL);
9318 if (vec_safe_length (expr_list) == 1)
9320 /* Just return the expression, remove a level of
9321 indirection. */
9322 ret = (*expr_list)[0];
9324 else
9326 /* We have a comma expression, we will collapse later. */
9327 ret = build_tree_list_vec (expr_list);
9329 release_tree_vector (expr_list);
9330 return ret;
9333 /* A check, needed in several places, that ObjC interface, implementation or
9334 method definitions are not prefixed by incorrect items. */
9335 static bool
9336 c_parser_objc_diagnose_bad_element_prefix (c_parser *parser,
9337 struct c_declspecs *specs)
9339 if (!specs->declspecs_seen_p || specs->non_sc_seen_p
9340 || specs->typespec_kind != ctsk_none)
9342 c_parser_error (parser,
9343 "no type or storage class may be specified here,");
9344 c_parser_skip_to_end_of_block_or_statement (parser);
9345 return true;
9347 return false;
9350 /* Parse an Objective-C @property declaration. The syntax is:
9352 objc-property-declaration:
9353 '@property' objc-property-attributes[opt] struct-declaration ;
9355 objc-property-attributes:
9356 '(' objc-property-attribute-list ')'
9358 objc-property-attribute-list:
9359 objc-property-attribute
9360 objc-property-attribute-list, objc-property-attribute
9362 objc-property-attribute
9363 'getter' = identifier
9364 'setter' = identifier
9365 'readonly'
9366 'readwrite'
9367 'assign'
9368 'retain'
9369 'copy'
9370 'nonatomic'
9372 For example:
9373 @property NSString *name;
9374 @property (readonly) id object;
9375 @property (retain, nonatomic, getter=getTheName) id name;
9376 @property int a, b, c;
9378 PS: This function is identical to cp_parser_objc_at_propery_declaration
9379 for C++. Keep them in sync. */
9380 static void
9381 c_parser_objc_at_property_declaration (c_parser *parser)
9383 /* The following variables hold the attributes of the properties as
9384 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
9385 seen. When we see an attribute, we set them to 'true' (if they
9386 are boolean properties) or to the identifier (if they have an
9387 argument, ie, for getter and setter). Note that here we only
9388 parse the list of attributes, check the syntax and accumulate the
9389 attributes that we find. objc_add_property_declaration() will
9390 then process the information. */
9391 bool property_assign = false;
9392 bool property_copy = false;
9393 tree property_getter_ident = NULL_TREE;
9394 bool property_nonatomic = false;
9395 bool property_readonly = false;
9396 bool property_readwrite = false;
9397 bool property_retain = false;
9398 tree property_setter_ident = NULL_TREE;
9400 /* 'properties' is the list of properties that we read. Usually a
9401 single one, but maybe more (eg, in "@property int a, b, c;" there
9402 are three). */
9403 tree properties;
9404 location_t loc;
9406 loc = c_parser_peek_token (parser)->location;
9407 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROPERTY));
9409 c_parser_consume_token (parser); /* Eat '@property'. */
9411 /* Parse the optional attribute list... */
9412 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
9414 /* Eat the '(' */
9415 c_parser_consume_token (parser);
9417 /* Property attribute keywords are valid now. */
9418 parser->objc_property_attr_context = true;
9420 while (true)
9422 bool syntax_error = false;
9423 c_token *token = c_parser_peek_token (parser);
9424 enum rid keyword;
9426 if (token->type != CPP_KEYWORD)
9428 if (token->type == CPP_CLOSE_PAREN)
9429 c_parser_error (parser, "expected identifier");
9430 else
9432 c_parser_consume_token (parser);
9433 c_parser_error (parser, "unknown property attribute");
9435 break;
9437 keyword = token->keyword;
9438 c_parser_consume_token (parser);
9439 switch (keyword)
9441 case RID_ASSIGN: property_assign = true; break;
9442 case RID_COPY: property_copy = true; break;
9443 case RID_NONATOMIC: property_nonatomic = true; break;
9444 case RID_READONLY: property_readonly = true; break;
9445 case RID_READWRITE: property_readwrite = true; break;
9446 case RID_RETAIN: property_retain = true; break;
9448 case RID_GETTER:
9449 case RID_SETTER:
9450 if (c_parser_next_token_is_not (parser, CPP_EQ))
9452 if (keyword == RID_GETTER)
9453 c_parser_error (parser,
9454 "missing %<=%> (after %<getter%> attribute)");
9455 else
9456 c_parser_error (parser,
9457 "missing %<=%> (after %<setter%> attribute)");
9458 syntax_error = true;
9459 break;
9461 c_parser_consume_token (parser); /* eat the = */
9462 if (c_parser_next_token_is_not (parser, CPP_NAME))
9464 c_parser_error (parser, "expected identifier");
9465 syntax_error = true;
9466 break;
9468 if (keyword == RID_SETTER)
9470 if (property_setter_ident != NULL_TREE)
9471 c_parser_error (parser, "the %<setter%> attribute may only be specified once");
9472 else
9473 property_setter_ident = c_parser_peek_token (parser)->value;
9474 c_parser_consume_token (parser);
9475 if (c_parser_next_token_is_not (parser, CPP_COLON))
9476 c_parser_error (parser, "setter name must terminate with %<:%>");
9477 else
9478 c_parser_consume_token (parser);
9480 else
9482 if (property_getter_ident != NULL_TREE)
9483 c_parser_error (parser, "the %<getter%> attribute may only be specified once");
9484 else
9485 property_getter_ident = c_parser_peek_token (parser)->value;
9486 c_parser_consume_token (parser);
9488 break;
9489 default:
9490 c_parser_error (parser, "unknown property attribute");
9491 syntax_error = true;
9492 break;
9495 if (syntax_error)
9496 break;
9498 if (c_parser_next_token_is (parser, CPP_COMMA))
9499 c_parser_consume_token (parser);
9500 else
9501 break;
9503 parser->objc_property_attr_context = false;
9504 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9506 /* ... and the property declaration(s). */
9507 properties = c_parser_struct_declaration (parser);
9509 if (properties == error_mark_node)
9511 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9512 parser->error = false;
9513 return;
9516 if (properties == NULL_TREE)
9517 c_parser_error (parser, "expected identifier");
9518 else
9520 /* Comma-separated properties are chained together in
9521 reverse order; add them one by one. */
9522 properties = nreverse (properties);
9524 for (; properties; properties = TREE_CHAIN (properties))
9525 objc_add_property_declaration (loc, copy_node (properties),
9526 property_readonly, property_readwrite,
9527 property_assign, property_retain,
9528 property_copy, property_nonatomic,
9529 property_getter_ident, property_setter_ident);
9532 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9533 parser->error = false;
9536 /* Parse an Objective-C @synthesize declaration. The syntax is:
9538 objc-synthesize-declaration:
9539 @synthesize objc-synthesize-identifier-list ;
9541 objc-synthesize-identifier-list:
9542 objc-synthesize-identifier
9543 objc-synthesize-identifier-list, objc-synthesize-identifier
9545 objc-synthesize-identifier
9546 identifier
9547 identifier = identifier
9549 For example:
9550 @synthesize MyProperty;
9551 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
9553 PS: This function is identical to cp_parser_objc_at_synthesize_declaration
9554 for C++. Keep them in sync.
9556 static void
9557 c_parser_objc_at_synthesize_declaration (c_parser *parser)
9559 tree list = NULL_TREE;
9560 location_t loc;
9561 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNTHESIZE));
9562 loc = c_parser_peek_token (parser)->location;
9564 c_parser_consume_token (parser);
9565 while (true)
9567 tree property, ivar;
9568 if (c_parser_next_token_is_not (parser, CPP_NAME))
9570 c_parser_error (parser, "expected identifier");
9571 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9572 /* Once we find the semicolon, we can resume normal parsing.
9573 We have to reset parser->error manually because
9574 c_parser_skip_until_found() won't reset it for us if the
9575 next token is precisely a semicolon. */
9576 parser->error = false;
9577 return;
9579 property = c_parser_peek_token (parser)->value;
9580 c_parser_consume_token (parser);
9581 if (c_parser_next_token_is (parser, CPP_EQ))
9583 c_parser_consume_token (parser);
9584 if (c_parser_next_token_is_not (parser, CPP_NAME))
9586 c_parser_error (parser, "expected identifier");
9587 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9588 parser->error = false;
9589 return;
9591 ivar = c_parser_peek_token (parser)->value;
9592 c_parser_consume_token (parser);
9594 else
9595 ivar = NULL_TREE;
9596 list = chainon (list, build_tree_list (ivar, property));
9597 if (c_parser_next_token_is (parser, CPP_COMMA))
9598 c_parser_consume_token (parser);
9599 else
9600 break;
9602 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9603 objc_add_synthesize_declaration (loc, list);
9606 /* Parse an Objective-C @dynamic declaration. The syntax is:
9608 objc-dynamic-declaration:
9609 @dynamic identifier-list ;
9611 For example:
9612 @dynamic MyProperty;
9613 @dynamic MyProperty, AnotherProperty;
9615 PS: This function is identical to cp_parser_objc_at_dynamic_declaration
9616 for C++. Keep them in sync.
9618 static void
9619 c_parser_objc_at_dynamic_declaration (c_parser *parser)
9621 tree list = NULL_TREE;
9622 location_t loc;
9623 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_DYNAMIC));
9624 loc = c_parser_peek_token (parser)->location;
9626 c_parser_consume_token (parser);
9627 while (true)
9629 tree property;
9630 if (c_parser_next_token_is_not (parser, CPP_NAME))
9632 c_parser_error (parser, "expected identifier");
9633 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9634 parser->error = false;
9635 return;
9637 property = c_parser_peek_token (parser)->value;
9638 list = chainon (list, build_tree_list (NULL_TREE, property));
9639 c_parser_consume_token (parser);
9640 if (c_parser_next_token_is (parser, CPP_COMMA))
9641 c_parser_consume_token (parser);
9642 else
9643 break;
9645 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9646 objc_add_dynamic_declaration (loc, list);
9650 /* Handle pragmas. Some OpenMP pragmas are associated with, and therefore
9651 should be considered, statements. ALLOW_STMT is true if we're within
9652 the context of a function and such pragmas are to be allowed. Returns
9653 true if we actually parsed such a pragma. */
9655 static bool
9656 c_parser_pragma (c_parser *parser, enum pragma_context context)
9658 unsigned int id;
9660 id = c_parser_peek_token (parser)->pragma_kind;
9661 gcc_assert (id != PRAGMA_NONE);
9663 switch (id)
9665 case PRAGMA_OACC_ENTER_DATA:
9666 c_parser_oacc_enter_exit_data (parser, true);
9667 return false;
9669 case PRAGMA_OACC_EXIT_DATA:
9670 c_parser_oacc_enter_exit_data (parser, false);
9671 return false;
9673 case PRAGMA_OACC_UPDATE:
9674 if (context != pragma_compound)
9676 if (context == pragma_stmt)
9677 c_parser_error (parser, "%<#pragma acc update%> may only be "
9678 "used in compound statements");
9679 goto bad_stmt;
9681 c_parser_oacc_update (parser);
9682 return false;
9684 case PRAGMA_OMP_BARRIER:
9685 if (context != pragma_compound)
9687 if (context == pragma_stmt)
9688 c_parser_error (parser, "%<#pragma omp barrier%> may only be "
9689 "used in compound statements");
9690 goto bad_stmt;
9692 c_parser_omp_barrier (parser);
9693 return false;
9695 case PRAGMA_OMP_FLUSH:
9696 if (context != pragma_compound)
9698 if (context == pragma_stmt)
9699 c_parser_error (parser, "%<#pragma omp flush%> may only be "
9700 "used in compound statements");
9701 goto bad_stmt;
9703 c_parser_omp_flush (parser);
9704 return false;
9706 case PRAGMA_OMP_TASKWAIT:
9707 if (context != pragma_compound)
9709 if (context == pragma_stmt)
9710 c_parser_error (parser, "%<#pragma omp taskwait%> may only be "
9711 "used in compound statements");
9712 goto bad_stmt;
9714 c_parser_omp_taskwait (parser);
9715 return false;
9717 case PRAGMA_OMP_TASKYIELD:
9718 if (context != pragma_compound)
9720 if (context == pragma_stmt)
9721 c_parser_error (parser, "%<#pragma omp taskyield%> may only be "
9722 "used in compound statements");
9723 goto bad_stmt;
9725 c_parser_omp_taskyield (parser);
9726 return false;
9728 case PRAGMA_OMP_CANCEL:
9729 if (context != pragma_compound)
9731 if (context == pragma_stmt)
9732 c_parser_error (parser, "%<#pragma omp cancel%> may only be "
9733 "used in compound statements");
9734 goto bad_stmt;
9736 c_parser_omp_cancel (parser);
9737 return false;
9739 case PRAGMA_OMP_CANCELLATION_POINT:
9740 if (context != pragma_compound)
9742 if (context == pragma_stmt)
9743 c_parser_error (parser, "%<#pragma omp cancellation point%> may "
9744 "only be used in compound statements");
9745 goto bad_stmt;
9747 c_parser_omp_cancellation_point (parser);
9748 return false;
9750 case PRAGMA_OMP_THREADPRIVATE:
9751 c_parser_omp_threadprivate (parser);
9752 return false;
9754 case PRAGMA_OMP_TARGET:
9755 return c_parser_omp_target (parser, context);
9757 case PRAGMA_OMP_END_DECLARE_TARGET:
9758 c_parser_omp_end_declare_target (parser);
9759 return false;
9761 case PRAGMA_OMP_SECTION:
9762 error_at (c_parser_peek_token (parser)->location,
9763 "%<#pragma omp section%> may only be used in "
9764 "%<#pragma omp sections%> construct");
9765 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
9766 return false;
9768 case PRAGMA_OMP_DECLARE_REDUCTION:
9769 c_parser_omp_declare (parser, context);
9770 return false;
9771 case PRAGMA_IVDEP:
9772 c_parser_consume_pragma (parser);
9773 c_parser_skip_to_pragma_eol (parser);
9774 if (!c_parser_next_token_is_keyword (parser, RID_FOR)
9775 && !c_parser_next_token_is_keyword (parser, RID_WHILE)
9776 && !c_parser_next_token_is_keyword (parser, RID_DO))
9778 c_parser_error (parser, "for, while or do statement expected");
9779 return false;
9781 if (c_parser_next_token_is_keyword (parser, RID_FOR))
9782 c_parser_for_statement (parser, true);
9783 else if (c_parser_next_token_is_keyword (parser, RID_WHILE))
9784 c_parser_while_statement (parser, true);
9785 else
9786 c_parser_do_statement (parser, true);
9787 return false;
9789 case PRAGMA_GCC_PCH_PREPROCESS:
9790 c_parser_error (parser, "%<#pragma GCC pch_preprocess%> must be first");
9791 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
9792 return false;
9794 case PRAGMA_CILK_SIMD:
9795 if (!c_parser_cilk_verify_simd (parser, context))
9796 return false;
9797 c_parser_consume_pragma (parser);
9798 c_parser_cilk_simd (parser);
9799 return false;
9800 case PRAGMA_CILK_GRAINSIZE:
9801 if (!flag_cilkplus)
9803 warning (0, "%<#pragma grainsize%> ignored because -fcilkplus is not"
9804 " enabled");
9805 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
9806 return false;
9808 if (context == pragma_external)
9810 error_at (c_parser_peek_token (parser)->location,
9811 "%<#pragma grainsize%> must be inside a function");
9812 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
9813 return false;
9815 c_parser_cilk_grainsize (parser);
9816 return false;
9818 default:
9819 if (id < PRAGMA_FIRST_EXTERNAL)
9821 if (context != pragma_stmt && context != pragma_compound)
9823 bad_stmt:
9824 c_parser_error (parser, "expected declaration specifiers");
9825 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
9826 return false;
9828 c_parser_omp_construct (parser);
9829 return true;
9831 break;
9834 c_parser_consume_pragma (parser);
9835 c_invoke_pragma_handler (id);
9837 /* Skip to EOL, but suppress any error message. Those will have been
9838 generated by the handler routine through calling error, as opposed
9839 to calling c_parser_error. */
9840 parser->error = true;
9841 c_parser_skip_to_pragma_eol (parser);
9843 return false;
9846 /* The interface the pragma parsers have to the lexer. */
9848 enum cpp_ttype
9849 pragma_lex (tree *value, location_t *loc)
9851 c_token *tok = c_parser_peek_token (the_parser);
9852 enum cpp_ttype ret = tok->type;
9854 *value = tok->value;
9855 if (loc)
9856 *loc = tok->location;
9858 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
9859 ret = CPP_EOF;
9860 else
9862 if (ret == CPP_KEYWORD)
9863 ret = CPP_NAME;
9864 c_parser_consume_token (the_parser);
9867 return ret;
9870 static void
9871 c_parser_pragma_pch_preprocess (c_parser *parser)
9873 tree name = NULL;
9875 c_parser_consume_pragma (parser);
9876 if (c_parser_next_token_is (parser, CPP_STRING))
9878 name = c_parser_peek_token (parser)->value;
9879 c_parser_consume_token (parser);
9881 else
9882 c_parser_error (parser, "expected string literal");
9883 c_parser_skip_to_pragma_eol (parser);
9885 if (name)
9886 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
9889 /* OpenACC and OpenMP parsing routines. */
9891 /* Returns name of the next clause.
9892 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
9893 the token is not consumed. Otherwise appropriate pragma_omp_clause is
9894 returned and the token is consumed. */
9896 static pragma_omp_clause
9897 c_parser_omp_clause_name (c_parser *parser)
9899 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
9901 if (c_parser_next_token_is_keyword (parser, RID_AUTO))
9902 result = PRAGMA_OACC_CLAUSE_AUTO;
9903 else if (c_parser_next_token_is_keyword (parser, RID_IF))
9904 result = PRAGMA_OMP_CLAUSE_IF;
9905 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
9906 result = PRAGMA_OMP_CLAUSE_DEFAULT;
9907 else if (c_parser_next_token_is_keyword (parser, RID_FOR))
9908 result = PRAGMA_OMP_CLAUSE_FOR;
9909 else if (c_parser_next_token_is (parser, CPP_NAME))
9911 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
9913 switch (p[0])
9915 case 'a':
9916 if (!strcmp ("aligned", p))
9917 result = PRAGMA_OMP_CLAUSE_ALIGNED;
9918 else if (!strcmp ("async", p))
9919 result = PRAGMA_OACC_CLAUSE_ASYNC;
9920 break;
9921 case 'c':
9922 if (!strcmp ("collapse", p))
9923 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
9924 else if (!strcmp ("copy", p))
9925 result = PRAGMA_OACC_CLAUSE_COPY;
9926 else if (!strcmp ("copyin", p))
9927 result = PRAGMA_OMP_CLAUSE_COPYIN;
9928 else if (!strcmp ("copyout", p))
9929 result = PRAGMA_OACC_CLAUSE_COPYOUT;
9930 else if (!strcmp ("copyprivate", p))
9931 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
9932 else if (!strcmp ("create", p))
9933 result = PRAGMA_OACC_CLAUSE_CREATE;
9934 break;
9935 case 'd':
9936 if (!strcmp ("delete", p))
9937 result = PRAGMA_OACC_CLAUSE_DELETE;
9938 else if (!strcmp ("depend", p))
9939 result = PRAGMA_OMP_CLAUSE_DEPEND;
9940 else if (!strcmp ("device", p))
9941 result = PRAGMA_OMP_CLAUSE_DEVICE;
9942 else if (!strcmp ("deviceptr", p))
9943 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
9944 else if (!strcmp ("dist_schedule", p))
9945 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
9946 break;
9947 case 'f':
9948 if (!strcmp ("final", p))
9949 result = PRAGMA_OMP_CLAUSE_FINAL;
9950 else if (!strcmp ("firstprivate", p))
9951 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
9952 else if (!strcmp ("from", p))
9953 result = PRAGMA_OMP_CLAUSE_FROM;
9954 break;
9955 case 'g':
9956 if (!strcmp ("gang", p))
9957 result = PRAGMA_OACC_CLAUSE_GANG;
9958 break;
9959 case 'h':
9960 if (!strcmp ("host", p))
9961 result = PRAGMA_OACC_CLAUSE_HOST;
9962 break;
9963 case 'i':
9964 if (!strcmp ("inbranch", p))
9965 result = PRAGMA_OMP_CLAUSE_INBRANCH;
9966 break;
9967 case 'l':
9968 if (!strcmp ("lastprivate", p))
9969 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
9970 else if (!strcmp ("linear", p))
9971 result = PRAGMA_OMP_CLAUSE_LINEAR;
9972 break;
9973 case 'm':
9974 if (!strcmp ("map", p))
9975 result = PRAGMA_OMP_CLAUSE_MAP;
9976 else if (!strcmp ("mergeable", p))
9977 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
9978 else if (flag_cilkplus && !strcmp ("mask", p))
9979 result = PRAGMA_CILK_CLAUSE_MASK;
9980 break;
9981 case 'n':
9982 if (!strcmp ("notinbranch", p))
9983 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
9984 else if (!strcmp ("nowait", p))
9985 result = PRAGMA_OMP_CLAUSE_NOWAIT;
9986 else if (!strcmp ("num_gangs", p))
9987 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
9988 else if (!strcmp ("num_teams", p))
9989 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
9990 else if (!strcmp ("num_threads", p))
9991 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
9992 else if (!strcmp ("num_workers", p))
9993 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
9994 else if (flag_cilkplus && !strcmp ("nomask", p))
9995 result = PRAGMA_CILK_CLAUSE_NOMASK;
9996 break;
9997 case 'o':
9998 if (!strcmp ("ordered", p))
9999 result = PRAGMA_OMP_CLAUSE_ORDERED;
10000 break;
10001 case 'p':
10002 if (!strcmp ("parallel", p))
10003 result = PRAGMA_OMP_CLAUSE_PARALLEL;
10004 else if (!strcmp ("present", p))
10005 result = PRAGMA_OACC_CLAUSE_PRESENT;
10006 else if (!strcmp ("present_or_copy", p)
10007 || !strcmp ("pcopy", p))
10008 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY;
10009 else if (!strcmp ("present_or_copyin", p)
10010 || !strcmp ("pcopyin", p))
10011 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN;
10012 else if (!strcmp ("present_or_copyout", p)
10013 || !strcmp ("pcopyout", p))
10014 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT;
10015 else if (!strcmp ("present_or_create", p)
10016 || !strcmp ("pcreate", p))
10017 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE;
10018 else if (!strcmp ("private", p))
10019 result = PRAGMA_OMP_CLAUSE_PRIVATE;
10020 else if (!strcmp ("proc_bind", p))
10021 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
10022 break;
10023 case 'r':
10024 if (!strcmp ("reduction", p))
10025 result = PRAGMA_OMP_CLAUSE_REDUCTION;
10026 break;
10027 case 's':
10028 if (!strcmp ("safelen", p))
10029 result = PRAGMA_OMP_CLAUSE_SAFELEN;
10030 else if (!strcmp ("schedule", p))
10031 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
10032 else if (!strcmp ("sections", p))
10033 result = PRAGMA_OMP_CLAUSE_SECTIONS;
10034 else if (!strcmp ("seq", p))
10035 result = PRAGMA_OACC_CLAUSE_SEQ;
10036 else if (!strcmp ("shared", p))
10037 result = PRAGMA_OMP_CLAUSE_SHARED;
10038 else if (!strcmp ("simdlen", p))
10039 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
10040 else if (!strcmp ("self", p))
10041 result = PRAGMA_OACC_CLAUSE_SELF;
10042 break;
10043 case 't':
10044 if (!strcmp ("taskgroup", p))
10045 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
10046 else if (!strcmp ("thread_limit", p))
10047 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
10048 else if (!strcmp ("to", p))
10049 result = PRAGMA_OMP_CLAUSE_TO;
10050 break;
10051 case 'u':
10052 if (!strcmp ("uniform", p))
10053 result = PRAGMA_OMP_CLAUSE_UNIFORM;
10054 else if (!strcmp ("untied", p))
10055 result = PRAGMA_OMP_CLAUSE_UNTIED;
10056 break;
10057 case 'v':
10058 if (!strcmp ("vector", p))
10059 result = PRAGMA_OACC_CLAUSE_VECTOR;
10060 else if (!strcmp ("vector_length", p))
10061 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
10062 else if (flag_cilkplus && !strcmp ("vectorlength", p))
10063 result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
10064 break;
10065 case 'w':
10066 if (!strcmp ("wait", p))
10067 result = PRAGMA_OACC_CLAUSE_WAIT;
10068 else if (!strcmp ("worker", p))
10069 result = PRAGMA_OACC_CLAUSE_WORKER;
10070 break;
10074 if (result != PRAGMA_OMP_CLAUSE_NONE)
10075 c_parser_consume_token (parser);
10077 return result;
10080 /* Validate that a clause of the given type does not already exist. */
10082 static void
10083 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
10084 const char *name)
10086 tree c;
10088 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
10089 if (OMP_CLAUSE_CODE (c) == code)
10091 location_t loc = OMP_CLAUSE_LOCATION (c);
10092 error_at (loc, "too many %qs clauses", name);
10093 break;
10097 /* OpenACC 2.0
10098 Parse wait clause or wait directive parameters. */
10100 static tree
10101 c_parser_oacc_wait_list (c_parser *parser, location_t clause_loc, tree list)
10103 vec<tree, va_gc> *args;
10104 tree t, args_tree;
10106 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10107 return list;
10109 args = c_parser_expr_list (parser, false, true, NULL, NULL, NULL, NULL);
10111 if (args->length () == 0)
10113 c_parser_error (parser, "expected integer expression before ')'");
10114 release_tree_vector (args);
10115 return list;
10118 args_tree = build_tree_list_vec (args);
10120 for (t = args_tree; t; t = TREE_CHAIN (t))
10122 tree targ = TREE_VALUE (t);
10124 if (targ != error_mark_node)
10126 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
10128 c_parser_error (parser, "expression must be integral");
10129 targ = error_mark_node;
10131 else
10133 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
10135 OMP_CLAUSE_DECL (c) = targ;
10136 OMP_CLAUSE_CHAIN (c) = list;
10137 list = c;
10142 release_tree_vector (args);
10143 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10144 return list;
10147 /* OpenACC 2.0, OpenMP 2.5:
10148 variable-list:
10149 identifier
10150 variable-list , identifier
10152 If KIND is nonzero, create the appropriate node and install the
10153 decl in OMP_CLAUSE_DECL and add the node to the head of the list.
10154 If KIND is nonzero, CLAUSE_LOC is the location of the clause.
10156 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
10157 return the list created. */
10159 static tree
10160 c_parser_omp_variable_list (c_parser *parser,
10161 location_t clause_loc,
10162 enum omp_clause_code kind, tree list)
10164 if (c_parser_next_token_is_not (parser, CPP_NAME)
10165 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
10166 c_parser_error (parser, "expected identifier");
10168 while (c_parser_next_token_is (parser, CPP_NAME)
10169 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
10171 tree t = lookup_name (c_parser_peek_token (parser)->value);
10173 if (t == NULL_TREE)
10175 undeclared_variable (c_parser_peek_token (parser)->location,
10176 c_parser_peek_token (parser)->value);
10177 t = error_mark_node;
10180 c_parser_consume_token (parser);
10182 if (t == error_mark_node)
10184 else if (kind != 0)
10186 switch (kind)
10188 case OMP_CLAUSE__CACHE_:
10189 if (c_parser_peek_token (parser)->type != CPP_OPEN_SQUARE)
10191 c_parser_error (parser, "expected %<[%>");
10192 t = error_mark_node;
10193 break;
10195 /* FALL THROUGH. */
10196 case OMP_CLAUSE_MAP:
10197 case OMP_CLAUSE_FROM:
10198 case OMP_CLAUSE_TO:
10199 case OMP_CLAUSE_DEPEND:
10200 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
10202 tree low_bound = NULL_TREE, length = NULL_TREE;
10204 c_parser_consume_token (parser);
10205 if (!c_parser_next_token_is (parser, CPP_COLON))
10207 low_bound = c_parser_expression (parser).value;
10208 mark_exp_read (low_bound);
10210 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
10211 length = integer_one_node;
10212 else
10214 /* Look for `:'. */
10215 if (!c_parser_require (parser, CPP_COLON,
10216 "expected %<:%>"))
10218 t = error_mark_node;
10219 break;
10221 if (!c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
10223 length = c_parser_expression (parser).value;
10224 mark_exp_read (length);
10227 /* Look for the closing `]'. */
10228 if (!c_parser_require (parser, CPP_CLOSE_SQUARE,
10229 "expected %<]%>"))
10231 t = error_mark_node;
10232 break;
10235 if (kind == OMP_CLAUSE__CACHE_)
10237 if (TREE_CODE (low_bound) != INTEGER_CST
10238 && !TREE_READONLY (low_bound))
10240 error_at (clause_loc,
10241 "%qD is not a constant", low_bound);
10242 t = error_mark_node;
10245 if (TREE_CODE (length) != INTEGER_CST
10246 && !TREE_READONLY (length))
10248 error_at (clause_loc,
10249 "%qD is not a constant", length);
10250 t = error_mark_node;
10254 t = tree_cons (low_bound, length, t);
10256 break;
10257 default:
10258 break;
10261 if (t != error_mark_node)
10263 tree u = build_omp_clause (clause_loc, kind);
10264 OMP_CLAUSE_DECL (u) = t;
10265 OMP_CLAUSE_CHAIN (u) = list;
10266 list = u;
10269 else
10270 list = tree_cons (t, NULL_TREE, list);
10272 if (c_parser_next_token_is_not (parser, CPP_COMMA))
10273 break;
10275 c_parser_consume_token (parser);
10278 return list;
10281 /* Similarly, but expect leading and trailing parenthesis. This is a very
10282 common case for OpenACC and OpenMP clauses. */
10284 static tree
10285 c_parser_omp_var_list_parens (c_parser *parser, enum omp_clause_code kind,
10286 tree list)
10288 /* The clauses location. */
10289 location_t loc = c_parser_peek_token (parser)->location;
10291 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10293 list = c_parser_omp_variable_list (parser, loc, kind, list);
10294 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10296 return list;
10299 /* OpenACC 2.0:
10300 copy ( variable-list )
10301 copyin ( variable-list )
10302 copyout ( variable-list )
10303 create ( variable-list )
10304 delete ( variable-list )
10305 present ( variable-list )
10306 present_or_copy ( variable-list )
10307 pcopy ( variable-list )
10308 present_or_copyin ( variable-list )
10309 pcopyin ( variable-list )
10310 present_or_copyout ( variable-list )
10311 pcopyout ( variable-list )
10312 present_or_create ( variable-list )
10313 pcreate ( variable-list ) */
10315 static tree
10316 c_parser_oacc_data_clause (c_parser *parser, pragma_omp_clause c_kind,
10317 tree list)
10319 enum gomp_map_kind kind;
10320 switch (c_kind)
10322 case PRAGMA_OACC_CLAUSE_COPY:
10323 kind = GOMP_MAP_FORCE_TOFROM;
10324 break;
10325 case PRAGMA_OACC_CLAUSE_COPYIN:
10326 kind = GOMP_MAP_FORCE_TO;
10327 break;
10328 case PRAGMA_OACC_CLAUSE_COPYOUT:
10329 kind = GOMP_MAP_FORCE_FROM;
10330 break;
10331 case PRAGMA_OACC_CLAUSE_CREATE:
10332 kind = GOMP_MAP_FORCE_ALLOC;
10333 break;
10334 case PRAGMA_OACC_CLAUSE_DELETE:
10335 kind = GOMP_MAP_FORCE_DEALLOC;
10336 break;
10337 case PRAGMA_OACC_CLAUSE_DEVICE:
10338 kind = GOMP_MAP_FORCE_TO;
10339 break;
10340 case PRAGMA_OACC_CLAUSE_HOST:
10341 case PRAGMA_OACC_CLAUSE_SELF:
10342 kind = GOMP_MAP_FORCE_FROM;
10343 break;
10344 case PRAGMA_OACC_CLAUSE_PRESENT:
10345 kind = GOMP_MAP_FORCE_PRESENT;
10346 break;
10347 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
10348 kind = GOMP_MAP_TOFROM;
10349 break;
10350 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
10351 kind = GOMP_MAP_TO;
10352 break;
10353 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
10354 kind = GOMP_MAP_FROM;
10355 break;
10356 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
10357 kind = GOMP_MAP_ALLOC;
10358 break;
10359 default:
10360 gcc_unreachable ();
10362 tree nl, c;
10363 nl = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_MAP, list);
10365 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
10366 OMP_CLAUSE_SET_MAP_KIND (c, kind);
10368 return nl;
10371 /* OpenACC 2.0:
10372 deviceptr ( variable-list ) */
10374 static tree
10375 c_parser_oacc_data_clause_deviceptr (c_parser *parser, tree list)
10377 location_t loc = c_parser_peek_token (parser)->location;
10378 tree vars, t;
10380 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
10381 c_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
10382 variable-list must only allow for pointer variables. */
10383 vars = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
10384 for (t = vars; t && t; t = TREE_CHAIN (t))
10386 tree v = TREE_PURPOSE (t);
10388 /* FIXME diagnostics: Ideally we should keep individual
10389 locations for all the variables in the var list to make the
10390 following errors more precise. Perhaps
10391 c_parser_omp_var_list_parens() should construct a list of
10392 locations to go along with the var list. */
10394 if (!VAR_P (v))
10395 error_at (loc, "%qD is not a variable", v);
10396 else if (TREE_TYPE (v) == error_mark_node)
10398 else if (!POINTER_TYPE_P (TREE_TYPE (v)))
10399 error_at (loc, "%qD is not a pointer variable", v);
10401 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
10402 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
10403 OMP_CLAUSE_DECL (u) = v;
10404 OMP_CLAUSE_CHAIN (u) = list;
10405 list = u;
10408 return list;
10411 /* OpenACC 2.0, OpenMP 3.0:
10412 collapse ( constant-expression ) */
10414 static tree
10415 c_parser_omp_clause_collapse (c_parser *parser, tree list)
10417 tree c, num = error_mark_node;
10418 HOST_WIDE_INT n;
10419 location_t loc;
10421 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse");
10423 loc = c_parser_peek_token (parser)->location;
10424 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10426 num = c_parser_expr_no_commas (parser, NULL).value;
10427 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10429 if (num == error_mark_node)
10430 return list;
10431 mark_exp_read (num);
10432 num = c_fully_fold (num, false, NULL);
10433 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
10434 || !tree_fits_shwi_p (num)
10435 || (n = tree_to_shwi (num)) <= 0
10436 || (int) n != n)
10438 error_at (loc,
10439 "collapse argument needs positive constant integer expression");
10440 return list;
10442 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
10443 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
10444 OMP_CLAUSE_CHAIN (c) = list;
10445 return c;
10448 /* OpenMP 2.5:
10449 copyin ( variable-list ) */
10451 static tree
10452 c_parser_omp_clause_copyin (c_parser *parser, tree list)
10454 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYIN, list);
10457 /* OpenMP 2.5:
10458 copyprivate ( variable-list ) */
10460 static tree
10461 c_parser_omp_clause_copyprivate (c_parser *parser, tree list)
10463 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYPRIVATE, list);
10466 /* OpenMP 2.5:
10467 default ( shared | none ) */
10469 static tree
10470 c_parser_omp_clause_default (c_parser *parser, tree list)
10472 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
10473 location_t loc = c_parser_peek_token (parser)->location;
10474 tree c;
10476 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10477 return list;
10478 if (c_parser_next_token_is (parser, CPP_NAME))
10480 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
10482 switch (p[0])
10484 case 'n':
10485 if (strcmp ("none", p) != 0)
10486 goto invalid_kind;
10487 kind = OMP_CLAUSE_DEFAULT_NONE;
10488 break;
10490 case 's':
10491 if (strcmp ("shared", p) != 0)
10492 goto invalid_kind;
10493 kind = OMP_CLAUSE_DEFAULT_SHARED;
10494 break;
10496 default:
10497 goto invalid_kind;
10500 c_parser_consume_token (parser);
10502 else
10504 invalid_kind:
10505 c_parser_error (parser, "expected %<none%> or %<shared%>");
10507 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10509 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
10510 return list;
10512 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
10513 c = build_omp_clause (loc, OMP_CLAUSE_DEFAULT);
10514 OMP_CLAUSE_CHAIN (c) = list;
10515 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
10517 return c;
10520 /* OpenMP 2.5:
10521 firstprivate ( variable-list ) */
10523 static tree
10524 c_parser_omp_clause_firstprivate (c_parser *parser, tree list)
10526 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_FIRSTPRIVATE, list);
10529 /* OpenMP 3.1:
10530 final ( expression ) */
10532 static tree
10533 c_parser_omp_clause_final (c_parser *parser, tree list)
10535 location_t loc = c_parser_peek_token (parser)->location;
10536 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
10538 tree t = c_parser_paren_condition (parser);
10539 tree c;
10541 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final");
10543 c = build_omp_clause (loc, OMP_CLAUSE_FINAL);
10544 OMP_CLAUSE_FINAL_EXPR (c) = t;
10545 OMP_CLAUSE_CHAIN (c) = list;
10546 list = c;
10548 else
10549 c_parser_error (parser, "expected %<(%>");
10551 return list;
10554 /* OpenACC, OpenMP 2.5:
10555 if ( expression ) */
10557 static tree
10558 c_parser_omp_clause_if (c_parser *parser, tree list)
10560 location_t loc = c_parser_peek_token (parser)->location;
10561 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
10563 tree t = c_parser_paren_condition (parser);
10564 tree c;
10566 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if");
10568 c = build_omp_clause (loc, OMP_CLAUSE_IF);
10569 OMP_CLAUSE_IF_EXPR (c) = t;
10570 OMP_CLAUSE_CHAIN (c) = list;
10571 list = c;
10573 else
10574 c_parser_error (parser, "expected %<(%>");
10576 return list;
10579 /* OpenMP 2.5:
10580 lastprivate ( variable-list ) */
10582 static tree
10583 c_parser_omp_clause_lastprivate (c_parser *parser, tree list)
10585 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_LASTPRIVATE, list);
10588 /* OpenMP 3.1:
10589 mergeable */
10591 static tree
10592 c_parser_omp_clause_mergeable (c_parser *parser ATTRIBUTE_UNUSED, tree list)
10594 tree c;
10596 /* FIXME: Should we allow duplicates? */
10597 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable");
10599 c = build_omp_clause (c_parser_peek_token (parser)->location,
10600 OMP_CLAUSE_MERGEABLE);
10601 OMP_CLAUSE_CHAIN (c) = list;
10603 return c;
10606 /* OpenMP 2.5:
10607 nowait */
10609 static tree
10610 c_parser_omp_clause_nowait (c_parser *parser ATTRIBUTE_UNUSED, tree list)
10612 tree c;
10613 location_t loc = c_parser_peek_token (parser)->location;
10615 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
10617 c = build_omp_clause (loc, OMP_CLAUSE_NOWAIT);
10618 OMP_CLAUSE_CHAIN (c) = list;
10619 return c;
10622 /* OpenACC:
10623 num_gangs ( expression ) */
10625 static tree
10626 c_parser_omp_clause_num_gangs (c_parser *parser, tree list)
10628 location_t num_gangs_loc = c_parser_peek_token (parser)->location;
10629 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10631 location_t expr_loc = c_parser_peek_token (parser)->location;
10632 tree c, t = c_parser_expression (parser).value;
10633 mark_exp_read (t);
10634 t = c_fully_fold (t, false, NULL);
10636 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10638 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
10640 c_parser_error (parser, "expected integer expression");
10641 return list;
10644 /* Attempt to statically determine when the number isn't positive. */
10645 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
10646 build_int_cst (TREE_TYPE (t), 0));
10647 if (CAN_HAVE_LOCATION_P (c))
10648 SET_EXPR_LOCATION (c, expr_loc);
10649 if (c == boolean_true_node)
10651 warning_at (expr_loc, 0,
10652 "%<num_gangs%> value must be positive");
10653 t = integer_one_node;
10656 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_GANGS, "num_gangs");
10658 c = build_omp_clause (num_gangs_loc, OMP_CLAUSE_NUM_GANGS);
10659 OMP_CLAUSE_NUM_GANGS_EXPR (c) = t;
10660 OMP_CLAUSE_CHAIN (c) = list;
10661 list = c;
10664 return list;
10667 /* OpenMP 2.5:
10668 num_threads ( expression ) */
10670 static tree
10671 c_parser_omp_clause_num_threads (c_parser *parser, tree list)
10673 location_t num_threads_loc = c_parser_peek_token (parser)->location;
10674 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10676 location_t expr_loc = c_parser_peek_token (parser)->location;
10677 tree c, t = c_parser_expression (parser).value;
10678 mark_exp_read (t);
10679 t = c_fully_fold (t, false, NULL);
10681 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10683 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
10685 c_parser_error (parser, "expected integer expression");
10686 return list;
10689 /* Attempt to statically determine when the number isn't positive. */
10690 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
10691 build_int_cst (TREE_TYPE (t), 0));
10692 if (CAN_HAVE_LOCATION_P (c))
10693 SET_EXPR_LOCATION (c, expr_loc);
10694 if (c == boolean_true_node)
10696 warning_at (expr_loc, 0,
10697 "%<num_threads%> value must be positive");
10698 t = integer_one_node;
10701 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
10703 c = build_omp_clause (num_threads_loc, OMP_CLAUSE_NUM_THREADS);
10704 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
10705 OMP_CLAUSE_CHAIN (c) = list;
10706 list = c;
10709 return list;
10712 /* OpenACC:
10713 num_workers ( expression ) */
10715 static tree
10716 c_parser_omp_clause_num_workers (c_parser *parser, tree list)
10718 location_t num_workers_loc = c_parser_peek_token (parser)->location;
10719 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10721 location_t expr_loc = c_parser_peek_token (parser)->location;
10722 tree c, t = c_parser_expression (parser).value;
10723 mark_exp_read (t);
10724 t = c_fully_fold (t, false, NULL);
10726 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10728 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
10730 c_parser_error (parser, "expected integer expression");
10731 return list;
10734 /* Attempt to statically determine when the number isn't positive. */
10735 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
10736 build_int_cst (TREE_TYPE (t), 0));
10737 if (CAN_HAVE_LOCATION_P (c))
10738 SET_EXPR_LOCATION (c, expr_loc);
10739 if (c == boolean_true_node)
10741 warning_at (expr_loc, 0,
10742 "%<num_workers%> value must be positive");
10743 t = integer_one_node;
10746 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_WORKERS, "num_workers");
10748 c = build_omp_clause (num_workers_loc, OMP_CLAUSE_NUM_WORKERS);
10749 OMP_CLAUSE_NUM_WORKERS_EXPR (c) = t;
10750 OMP_CLAUSE_CHAIN (c) = list;
10751 list = c;
10754 return list;
10757 /* OpenACC:
10758 async [( int-expr )] */
10760 static tree
10761 c_parser_oacc_clause_async (c_parser *parser, tree list)
10763 tree c, t;
10764 location_t loc = c_parser_peek_token (parser)->location;
10766 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
10768 if (c_parser_peek_token (parser)->type == CPP_OPEN_PAREN)
10770 c_parser_consume_token (parser);
10772 t = c_parser_expression (parser).value;
10773 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
10774 c_parser_error (parser, "expected integer expression");
10775 else if (t == error_mark_node
10776 || !c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
10777 return list;
10779 else
10780 t = c_fully_fold (t, false, NULL);
10782 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async");
10784 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
10785 OMP_CLAUSE_ASYNC_EXPR (c) = t;
10786 OMP_CLAUSE_CHAIN (c) = list;
10787 list = c;
10789 return list;
10792 /* OpenACC:
10793 wait ( int-expr-list ) */
10795 static tree
10796 c_parser_oacc_clause_wait (c_parser *parser, tree list)
10798 location_t clause_loc = c_parser_peek_token (parser)->location;
10800 if (c_parser_peek_token (parser)->type == CPP_OPEN_PAREN)
10801 list = c_parser_oacc_wait_list (parser, clause_loc, list);
10803 return list;
10806 /* OpenMP 2.5:
10807 ordered */
10809 static tree
10810 c_parser_omp_clause_ordered (c_parser *parser, tree list)
10812 tree c;
10814 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
10816 c = build_omp_clause (c_parser_peek_token (parser)->location,
10817 OMP_CLAUSE_ORDERED);
10818 OMP_CLAUSE_CHAIN (c) = list;
10820 return c;
10823 /* OpenMP 2.5:
10824 private ( variable-list ) */
10826 static tree
10827 c_parser_omp_clause_private (c_parser *parser, tree list)
10829 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_PRIVATE, list);
10832 /* OpenMP 2.5:
10833 reduction ( reduction-operator : variable-list )
10835 reduction-operator:
10836 One of: + * - & ^ | && ||
10838 OpenMP 3.1:
10840 reduction-operator:
10841 One of: + * - & ^ | && || max min
10843 OpenMP 4.0:
10845 reduction-operator:
10846 One of: + * - & ^ | && ||
10847 identifier */
10849 static tree
10850 c_parser_omp_clause_reduction (c_parser *parser, tree list)
10852 location_t clause_loc = c_parser_peek_token (parser)->location;
10853 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10855 enum tree_code code = ERROR_MARK;
10856 tree reduc_id = NULL_TREE;
10858 switch (c_parser_peek_token (parser)->type)
10860 case CPP_PLUS:
10861 code = PLUS_EXPR;
10862 break;
10863 case CPP_MULT:
10864 code = MULT_EXPR;
10865 break;
10866 case CPP_MINUS:
10867 code = MINUS_EXPR;
10868 break;
10869 case CPP_AND:
10870 code = BIT_AND_EXPR;
10871 break;
10872 case CPP_XOR:
10873 code = BIT_XOR_EXPR;
10874 break;
10875 case CPP_OR:
10876 code = BIT_IOR_EXPR;
10877 break;
10878 case CPP_AND_AND:
10879 code = TRUTH_ANDIF_EXPR;
10880 break;
10881 case CPP_OR_OR:
10882 code = TRUTH_ORIF_EXPR;
10883 break;
10884 case CPP_NAME:
10886 const char *p
10887 = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
10888 if (strcmp (p, "min") == 0)
10890 code = MIN_EXPR;
10891 break;
10893 if (strcmp (p, "max") == 0)
10895 code = MAX_EXPR;
10896 break;
10898 reduc_id = c_parser_peek_token (parser)->value;
10899 break;
10901 default:
10902 c_parser_error (parser,
10903 "expected %<+%>, %<*%>, %<-%>, %<&%>, "
10904 "%<^%>, %<|%>, %<&&%>, %<||%>, %<min%> or %<max%>");
10905 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
10906 return list;
10908 c_parser_consume_token (parser);
10909 reduc_id = c_omp_reduction_id (code, reduc_id);
10910 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
10912 tree nl, c;
10914 nl = c_parser_omp_variable_list (parser, clause_loc,
10915 OMP_CLAUSE_REDUCTION, list);
10916 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
10918 tree type = TREE_TYPE (OMP_CLAUSE_DECL (c));
10919 OMP_CLAUSE_REDUCTION_CODE (c) = code;
10920 if (code == ERROR_MARK
10921 || !(INTEGRAL_TYPE_P (type)
10922 || TREE_CODE (type) == REAL_TYPE
10923 || TREE_CODE (type) == COMPLEX_TYPE))
10924 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c)
10925 = c_omp_reduction_lookup (reduc_id,
10926 TYPE_MAIN_VARIANT (type));
10929 list = nl;
10931 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10933 return list;
10936 /* OpenMP 2.5:
10937 schedule ( schedule-kind )
10938 schedule ( schedule-kind , expression )
10940 schedule-kind:
10941 static | dynamic | guided | runtime | auto
10944 static tree
10945 c_parser_omp_clause_schedule (c_parser *parser, tree list)
10947 tree c, t;
10948 location_t loc = c_parser_peek_token (parser)->location;
10950 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10951 return list;
10953 c = build_omp_clause (loc, OMP_CLAUSE_SCHEDULE);
10955 if (c_parser_next_token_is (parser, CPP_NAME))
10957 tree kind = c_parser_peek_token (parser)->value;
10958 const char *p = IDENTIFIER_POINTER (kind);
10960 switch (p[0])
10962 case 'd':
10963 if (strcmp ("dynamic", p) != 0)
10964 goto invalid_kind;
10965 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
10966 break;
10968 case 'g':
10969 if (strcmp ("guided", p) != 0)
10970 goto invalid_kind;
10971 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
10972 break;
10974 case 'r':
10975 if (strcmp ("runtime", p) != 0)
10976 goto invalid_kind;
10977 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
10978 break;
10980 default:
10981 goto invalid_kind;
10984 else if (c_parser_next_token_is_keyword (parser, RID_STATIC))
10985 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
10986 else if (c_parser_next_token_is_keyword (parser, RID_AUTO))
10987 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
10988 else
10989 goto invalid_kind;
10991 c_parser_consume_token (parser);
10992 if (c_parser_next_token_is (parser, CPP_COMMA))
10994 location_t here;
10995 c_parser_consume_token (parser);
10997 here = c_parser_peek_token (parser)->location;
10998 t = c_parser_expr_no_commas (parser, NULL).value;
10999 mark_exp_read (t);
11000 t = c_fully_fold (t, false, NULL);
11002 if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
11003 error_at (here, "schedule %<runtime%> does not take "
11004 "a %<chunk_size%> parameter");
11005 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
11006 error_at (here,
11007 "schedule %<auto%> does not take "
11008 "a %<chunk_size%> parameter");
11009 else if (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE)
11010 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
11011 else
11012 c_parser_error (parser, "expected integer expression");
11014 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11016 else
11017 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
11018 "expected %<,%> or %<)%>");
11020 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
11021 OMP_CLAUSE_CHAIN (c) = list;
11022 return c;
11024 invalid_kind:
11025 c_parser_error (parser, "invalid schedule kind");
11026 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
11027 return list;
11030 /* OpenMP 2.5:
11031 shared ( variable-list ) */
11033 static tree
11034 c_parser_omp_clause_shared (c_parser *parser, tree list)
11036 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_SHARED, list);
11039 /* OpenMP 3.0:
11040 untied */
11042 static tree
11043 c_parser_omp_clause_untied (c_parser *parser ATTRIBUTE_UNUSED, tree list)
11045 tree c;
11047 /* FIXME: Should we allow duplicates? */
11048 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied");
11050 c = build_omp_clause (c_parser_peek_token (parser)->location,
11051 OMP_CLAUSE_UNTIED);
11052 OMP_CLAUSE_CHAIN (c) = list;
11054 return c;
11057 /* OpenACC:
11058 vector_length ( expression ) */
11060 static tree
11061 c_parser_omp_clause_vector_length (c_parser *parser, tree list)
11063 location_t vector_length_loc = c_parser_peek_token (parser)->location;
11064 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11066 location_t expr_loc = c_parser_peek_token (parser)->location;
11067 tree c, t = c_parser_expression (parser).value;
11068 mark_exp_read (t);
11069 t = c_fully_fold (t, false, NULL);
11071 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11073 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
11075 c_parser_error (parser, "expected integer expression");
11076 return list;
11079 /* Attempt to statically determine when the number isn't positive. */
11080 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
11081 build_int_cst (TREE_TYPE (t), 0));
11082 if (CAN_HAVE_LOCATION_P (c))
11083 SET_EXPR_LOCATION (c, expr_loc);
11084 if (c == boolean_true_node)
11086 warning_at (expr_loc, 0,
11087 "%<vector_length%> value must be positive");
11088 t = integer_one_node;
11091 check_no_duplicate_clause (list, OMP_CLAUSE_VECTOR_LENGTH, "vector_length");
11093 c = build_omp_clause (vector_length_loc, OMP_CLAUSE_VECTOR_LENGTH);
11094 OMP_CLAUSE_VECTOR_LENGTH_EXPR (c) = t;
11095 OMP_CLAUSE_CHAIN (c) = list;
11096 list = c;
11099 return list;
11102 /* OpenMP 4.0:
11103 inbranch
11104 notinbranch */
11106 static tree
11107 c_parser_omp_clause_branch (c_parser *parser ATTRIBUTE_UNUSED,
11108 enum omp_clause_code code, tree list)
11110 check_no_duplicate_clause (list, code, omp_clause_code_name[code]);
11112 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
11113 OMP_CLAUSE_CHAIN (c) = list;
11115 return c;
11118 /* OpenMP 4.0:
11119 parallel
11121 sections
11122 taskgroup */
11124 static tree
11125 c_parser_omp_clause_cancelkind (c_parser *parser ATTRIBUTE_UNUSED,
11126 enum omp_clause_code code, tree list)
11128 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
11129 OMP_CLAUSE_CHAIN (c) = list;
11131 return c;
11134 /* OpenMP 4.0:
11135 num_teams ( expression ) */
11137 static tree
11138 c_parser_omp_clause_num_teams (c_parser *parser, tree list)
11140 location_t num_teams_loc = c_parser_peek_token (parser)->location;
11141 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11143 location_t expr_loc = c_parser_peek_token (parser)->location;
11144 tree c, t = c_parser_expression (parser).value;
11145 mark_exp_read (t);
11146 t = c_fully_fold (t, false, NULL);
11148 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11150 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
11152 c_parser_error (parser, "expected integer expression");
11153 return list;
11156 /* Attempt to statically determine when the number isn't positive. */
11157 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
11158 build_int_cst (TREE_TYPE (t), 0));
11159 if (CAN_HAVE_LOCATION_P (c))
11160 SET_EXPR_LOCATION (c, expr_loc);
11161 if (c == boolean_true_node)
11163 warning_at (expr_loc, 0, "%<num_teams%> value must be positive");
11164 t = integer_one_node;
11167 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS, "num_teams");
11169 c = build_omp_clause (num_teams_loc, OMP_CLAUSE_NUM_TEAMS);
11170 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
11171 OMP_CLAUSE_CHAIN (c) = list;
11172 list = c;
11175 return list;
11178 /* OpenMP 4.0:
11179 thread_limit ( expression ) */
11181 static tree
11182 c_parser_omp_clause_thread_limit (c_parser *parser, tree list)
11184 location_t num_thread_limit_loc = c_parser_peek_token (parser)->location;
11185 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11187 location_t expr_loc = c_parser_peek_token (parser)->location;
11188 tree c, t = c_parser_expression (parser).value;
11189 mark_exp_read (t);
11190 t = c_fully_fold (t, false, NULL);
11192 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11194 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
11196 c_parser_error (parser, "expected integer expression");
11197 return list;
11200 /* Attempt to statically determine when the number isn't positive. */
11201 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
11202 build_int_cst (TREE_TYPE (t), 0));
11203 if (CAN_HAVE_LOCATION_P (c))
11204 SET_EXPR_LOCATION (c, expr_loc);
11205 if (c == boolean_true_node)
11207 warning_at (expr_loc, 0, "%<thread_limit%> value must be positive");
11208 t = integer_one_node;
11211 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
11212 "thread_limit");
11214 c = build_omp_clause (num_thread_limit_loc, OMP_CLAUSE_THREAD_LIMIT);
11215 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
11216 OMP_CLAUSE_CHAIN (c) = list;
11217 list = c;
11220 return list;
11223 /* OpenMP 4.0:
11224 aligned ( variable-list )
11225 aligned ( variable-list : constant-expression ) */
11227 static tree
11228 c_parser_omp_clause_aligned (c_parser *parser, tree list)
11230 location_t clause_loc = c_parser_peek_token (parser)->location;
11231 tree nl, c;
11233 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11234 return list;
11236 nl = c_parser_omp_variable_list (parser, clause_loc,
11237 OMP_CLAUSE_ALIGNED, list);
11239 if (c_parser_next_token_is (parser, CPP_COLON))
11241 c_parser_consume_token (parser);
11242 tree alignment = c_parser_expr_no_commas (parser, NULL).value;
11243 mark_exp_read (alignment);
11244 alignment = c_fully_fold (alignment, false, NULL);
11245 if (TREE_CODE (alignment) != INTEGER_CST
11246 || !INTEGRAL_TYPE_P (TREE_TYPE (alignment))
11247 || tree_int_cst_sgn (alignment) != 1)
11249 error_at (clause_loc, "%<aligned%> clause alignment expression must "
11250 "be positive constant integer expression");
11251 alignment = NULL_TREE;
11254 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
11255 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
11258 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11259 return nl;
11262 /* OpenMP 4.0:
11263 linear ( variable-list )
11264 linear ( variable-list : expression ) */
11266 static tree
11267 c_parser_omp_clause_linear (c_parser *parser, tree list, bool is_cilk_simd_fn)
11269 location_t clause_loc = c_parser_peek_token (parser)->location;
11270 tree nl, c, step;
11272 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11273 return list;
11275 nl = c_parser_omp_variable_list (parser, clause_loc,
11276 OMP_CLAUSE_LINEAR, list);
11278 if (c_parser_next_token_is (parser, CPP_COLON))
11280 c_parser_consume_token (parser);
11281 step = c_parser_expression (parser).value;
11282 mark_exp_read (step);
11283 step = c_fully_fold (step, false, NULL);
11284 if (is_cilk_simd_fn && TREE_CODE (step) == PARM_DECL)
11286 sorry ("using parameters for %<linear%> step is not supported yet");
11287 step = integer_one_node;
11289 if (!INTEGRAL_TYPE_P (TREE_TYPE (step)))
11291 error_at (clause_loc, "%<linear%> clause step expression must "
11292 "be integral");
11293 step = integer_one_node;
11297 else
11298 step = integer_one_node;
11300 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
11302 OMP_CLAUSE_LINEAR_STEP (c) = step;
11305 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11306 return nl;
11309 /* OpenMP 4.0:
11310 safelen ( constant-expression ) */
11312 static tree
11313 c_parser_omp_clause_safelen (c_parser *parser, tree list)
11315 location_t clause_loc = c_parser_peek_token (parser)->location;
11316 tree c, t;
11318 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11319 return list;
11321 t = c_parser_expr_no_commas (parser, NULL).value;
11322 mark_exp_read (t);
11323 t = c_fully_fold (t, false, NULL);
11324 if (TREE_CODE (t) != INTEGER_CST
11325 || !INTEGRAL_TYPE_P (TREE_TYPE (t))
11326 || tree_int_cst_sgn (t) != 1)
11328 error_at (clause_loc, "%<safelen%> clause expression must "
11329 "be positive constant integer expression");
11330 t = NULL_TREE;
11333 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11334 if (t == NULL_TREE || t == error_mark_node)
11335 return list;
11337 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen");
11339 c = build_omp_clause (clause_loc, OMP_CLAUSE_SAFELEN);
11340 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
11341 OMP_CLAUSE_CHAIN (c) = list;
11342 return c;
11345 /* OpenMP 4.0:
11346 simdlen ( constant-expression ) */
11348 static tree
11349 c_parser_omp_clause_simdlen (c_parser *parser, tree list)
11351 location_t clause_loc = c_parser_peek_token (parser)->location;
11352 tree c, t;
11354 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11355 return list;
11357 t = c_parser_expr_no_commas (parser, NULL).value;
11358 mark_exp_read (t);
11359 t = c_fully_fold (t, false, NULL);
11360 if (TREE_CODE (t) != INTEGER_CST
11361 || !INTEGRAL_TYPE_P (TREE_TYPE (t))
11362 || tree_int_cst_sgn (t) != 1)
11364 error_at (clause_loc, "%<simdlen%> clause expression must "
11365 "be positive constant integer expression");
11366 t = NULL_TREE;
11369 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11370 if (t == NULL_TREE || t == error_mark_node)
11371 return list;
11373 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen");
11375 c = build_omp_clause (clause_loc, OMP_CLAUSE_SIMDLEN);
11376 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
11377 OMP_CLAUSE_CHAIN (c) = list;
11378 return c;
11381 /* OpenMP 4.0:
11382 depend ( depend-kind: variable-list )
11384 depend-kind:
11385 in | out | inout */
11387 static tree
11388 c_parser_omp_clause_depend (c_parser *parser, tree list)
11390 location_t clause_loc = c_parser_peek_token (parser)->location;
11391 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
11392 tree nl, c;
11394 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11395 return list;
11397 if (c_parser_next_token_is (parser, CPP_NAME))
11399 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11400 if (strcmp ("in", p) == 0)
11401 kind = OMP_CLAUSE_DEPEND_IN;
11402 else if (strcmp ("inout", p) == 0)
11403 kind = OMP_CLAUSE_DEPEND_INOUT;
11404 else if (strcmp ("out", p) == 0)
11405 kind = OMP_CLAUSE_DEPEND_OUT;
11406 else
11407 goto invalid_kind;
11409 else
11410 goto invalid_kind;
11412 c_parser_consume_token (parser);
11413 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
11414 goto resync_fail;
11416 nl = c_parser_omp_variable_list (parser, clause_loc,
11417 OMP_CLAUSE_DEPEND, list);
11419 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
11420 OMP_CLAUSE_DEPEND_KIND (c) = kind;
11422 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11423 return nl;
11425 invalid_kind:
11426 c_parser_error (parser, "invalid depend kind");
11427 resync_fail:
11428 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11429 return list;
11432 /* OpenMP 4.0:
11433 map ( map-kind: variable-list )
11434 map ( variable-list )
11436 map-kind:
11437 alloc | to | from | tofrom */
11439 static tree
11440 c_parser_omp_clause_map (c_parser *parser, tree list)
11442 location_t clause_loc = c_parser_peek_token (parser)->location;
11443 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
11444 tree nl, c;
11446 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11447 return list;
11449 if (c_parser_next_token_is (parser, CPP_NAME)
11450 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
11452 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11453 if (strcmp ("alloc", p) == 0)
11454 kind = GOMP_MAP_ALLOC;
11455 else if (strcmp ("to", p) == 0)
11456 kind = GOMP_MAP_TO;
11457 else if (strcmp ("from", p) == 0)
11458 kind = GOMP_MAP_FROM;
11459 else if (strcmp ("tofrom", p) == 0)
11460 kind = GOMP_MAP_TOFROM;
11461 else
11463 c_parser_error (parser, "invalid map kind");
11464 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
11465 "expected %<)%>");
11466 return list;
11468 c_parser_consume_token (parser);
11469 c_parser_consume_token (parser);
11472 nl = c_parser_omp_variable_list (parser, clause_loc, OMP_CLAUSE_MAP, list);
11474 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
11475 OMP_CLAUSE_SET_MAP_KIND (c, kind);
11477 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11478 return nl;
11481 /* OpenMP 4.0:
11482 device ( expression ) */
11484 static tree
11485 c_parser_omp_clause_device (c_parser *parser, tree list)
11487 location_t clause_loc = c_parser_peek_token (parser)->location;
11488 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11490 tree c, t = c_parser_expr_no_commas (parser, NULL).value;
11491 mark_exp_read (t);
11492 t = c_fully_fold (t, false, NULL);
11494 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11496 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
11498 c_parser_error (parser, "expected integer expression");
11499 return list;
11502 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE, "device");
11504 c = build_omp_clause (clause_loc, OMP_CLAUSE_DEVICE);
11505 OMP_CLAUSE_DEVICE_ID (c) = t;
11506 OMP_CLAUSE_CHAIN (c) = list;
11507 list = c;
11510 return list;
11513 /* OpenMP 4.0:
11514 dist_schedule ( static )
11515 dist_schedule ( static , expression ) */
11517 static tree
11518 c_parser_omp_clause_dist_schedule (c_parser *parser, tree list)
11520 tree c, t = NULL_TREE;
11521 location_t loc = c_parser_peek_token (parser)->location;
11523 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11524 return list;
11526 if (!c_parser_next_token_is_keyword (parser, RID_STATIC))
11528 c_parser_error (parser, "invalid dist_schedule kind");
11529 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
11530 "expected %<)%>");
11531 return list;
11534 c_parser_consume_token (parser);
11535 if (c_parser_next_token_is (parser, CPP_COMMA))
11537 c_parser_consume_token (parser);
11539 t = c_parser_expr_no_commas (parser, NULL).value;
11540 mark_exp_read (t);
11541 t = c_fully_fold (t, false, NULL);
11542 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11544 else
11545 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
11546 "expected %<,%> or %<)%>");
11548 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
11549 if (t == error_mark_node)
11550 return list;
11552 c = build_omp_clause (loc, OMP_CLAUSE_DIST_SCHEDULE);
11553 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
11554 OMP_CLAUSE_CHAIN (c) = list;
11555 return c;
11558 /* OpenMP 4.0:
11559 proc_bind ( proc-bind-kind )
11561 proc-bind-kind:
11562 master | close | spread */
11564 static tree
11565 c_parser_omp_clause_proc_bind (c_parser *parser, tree list)
11567 location_t clause_loc = c_parser_peek_token (parser)->location;
11568 enum omp_clause_proc_bind_kind kind;
11569 tree c;
11571 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11572 return list;
11574 if (c_parser_next_token_is (parser, CPP_NAME))
11576 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11577 if (strcmp ("master", p) == 0)
11578 kind = OMP_CLAUSE_PROC_BIND_MASTER;
11579 else if (strcmp ("close", p) == 0)
11580 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
11581 else if (strcmp ("spread", p) == 0)
11582 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
11583 else
11584 goto invalid_kind;
11586 else
11587 goto invalid_kind;
11589 c_parser_consume_token (parser);
11590 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11591 c = build_omp_clause (clause_loc, OMP_CLAUSE_PROC_BIND);
11592 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
11593 OMP_CLAUSE_CHAIN (c) = list;
11594 return c;
11596 invalid_kind:
11597 c_parser_error (parser, "invalid proc_bind kind");
11598 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11599 return list;
11602 /* OpenMP 4.0:
11603 to ( variable-list ) */
11605 static tree
11606 c_parser_omp_clause_to (c_parser *parser, tree list)
11608 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_TO, list);
11611 /* OpenMP 4.0:
11612 from ( variable-list ) */
11614 static tree
11615 c_parser_omp_clause_from (c_parser *parser, tree list)
11617 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_FROM, list);
11620 /* OpenMP 4.0:
11621 uniform ( variable-list ) */
11623 static tree
11624 c_parser_omp_clause_uniform (c_parser *parser, tree list)
11626 /* The clauses location. */
11627 location_t loc = c_parser_peek_token (parser)->location;
11629 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11631 list = c_parser_omp_variable_list (parser, loc, OMP_CLAUSE_UNIFORM,
11632 list);
11633 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11635 return list;
11638 /* Parse all OpenACC clauses. The set clauses allowed by the directive
11639 is a bitmask in MASK. Return the list of clauses found. */
11641 static tree
11642 c_parser_oacc_all_clauses (c_parser *parser, omp_clause_mask mask,
11643 const char *where, bool finish_p = true)
11645 tree clauses = NULL;
11646 bool first = true;
11648 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
11650 location_t here;
11651 pragma_omp_clause c_kind;
11652 const char *c_name;
11653 tree prev = clauses;
11655 if (!first && c_parser_next_token_is (parser, CPP_COMMA))
11656 c_parser_consume_token (parser);
11658 here = c_parser_peek_token (parser)->location;
11659 c_kind = c_parser_omp_clause_name (parser);
11661 switch (c_kind)
11663 case PRAGMA_OACC_CLAUSE_ASYNC:
11664 clauses = c_parser_oacc_clause_async (parser, clauses);
11665 c_name = "async";
11666 break;
11667 case PRAGMA_OACC_CLAUSE_COLLAPSE:
11668 clauses = c_parser_omp_clause_collapse (parser, clauses);
11669 c_name = "collapse";
11670 break;
11671 case PRAGMA_OACC_CLAUSE_COPY:
11672 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
11673 c_name = "copy";
11674 break;
11675 case PRAGMA_OACC_CLAUSE_COPYIN:
11676 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
11677 c_name = "copyin";
11678 break;
11679 case PRAGMA_OACC_CLAUSE_COPYOUT:
11680 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
11681 c_name = "copyout";
11682 break;
11683 case PRAGMA_OACC_CLAUSE_CREATE:
11684 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
11685 c_name = "create";
11686 break;
11687 case PRAGMA_OACC_CLAUSE_DELETE:
11688 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
11689 c_name = "delete";
11690 break;
11691 case PRAGMA_OACC_CLAUSE_DEVICE:
11692 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
11693 c_name = "device";
11694 break;
11695 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
11696 clauses = c_parser_oacc_data_clause_deviceptr (parser, clauses);
11697 c_name = "deviceptr";
11698 break;
11699 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
11700 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
11701 c_name = "firstprivate";
11702 break;
11703 case PRAGMA_OACC_CLAUSE_HOST:
11704 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
11705 c_name = "host";
11706 break;
11707 case PRAGMA_OACC_CLAUSE_IF:
11708 clauses = c_parser_omp_clause_if (parser, clauses);
11709 c_name = "if";
11710 break;
11711 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
11712 clauses = c_parser_omp_clause_num_gangs (parser, clauses);
11713 c_name = "num_gangs";
11714 break;
11715 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
11716 clauses = c_parser_omp_clause_num_workers (parser, clauses);
11717 c_name = "num_workers";
11718 break;
11719 case PRAGMA_OACC_CLAUSE_PRESENT:
11720 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
11721 c_name = "present";
11722 break;
11723 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
11724 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
11725 c_name = "present_or_copy";
11726 break;
11727 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
11728 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
11729 c_name = "present_or_copyin";
11730 break;
11731 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
11732 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
11733 c_name = "present_or_copyout";
11734 break;
11735 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
11736 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
11737 c_name = "present_or_create";
11738 break;
11739 case PRAGMA_OACC_CLAUSE_PRIVATE:
11740 clauses = c_parser_omp_clause_private (parser, clauses);
11741 c_name = "private";
11742 break;
11743 case PRAGMA_OACC_CLAUSE_REDUCTION:
11744 clauses = c_parser_omp_clause_reduction (parser, clauses);
11745 c_name = "reduction";
11746 break;
11747 case PRAGMA_OACC_CLAUSE_SELF:
11748 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
11749 c_name = "self";
11750 break;
11751 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
11752 clauses = c_parser_omp_clause_vector_length (parser, clauses);
11753 c_name = "vector_length";
11754 break;
11755 case PRAGMA_OACC_CLAUSE_WAIT:
11756 clauses = c_parser_oacc_clause_wait (parser, clauses);
11757 c_name = "wait";
11758 break;
11759 default:
11760 c_parser_error (parser, "expected %<#pragma acc%> clause");
11761 goto saw_error;
11764 first = false;
11766 if (((mask >> c_kind) & 1) == 0)
11768 /* Remove the invalid clause(s) from the list to avoid
11769 confusing the rest of the compiler. */
11770 clauses = prev;
11771 error_at (here, "%qs is not valid for %qs", c_name, where);
11775 saw_error:
11776 c_parser_skip_to_pragma_eol (parser);
11778 if (finish_p)
11779 return c_finish_omp_clauses (clauses);
11781 return clauses;
11784 /* Parse all OpenMP clauses. The set clauses allowed by the directive
11785 is a bitmask in MASK. Return the list of clauses found. */
11787 static tree
11788 c_parser_omp_all_clauses (c_parser *parser, omp_clause_mask mask,
11789 const char *where, bool finish_p = true)
11791 tree clauses = NULL;
11792 bool first = true, cilk_simd_fn = false;
11794 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
11796 location_t here;
11797 pragma_omp_clause c_kind;
11798 const char *c_name;
11799 tree prev = clauses;
11801 if (!first && c_parser_next_token_is (parser, CPP_COMMA))
11802 c_parser_consume_token (parser);
11804 here = c_parser_peek_token (parser)->location;
11805 c_kind = c_parser_omp_clause_name (parser);
11807 switch (c_kind)
11809 case PRAGMA_OMP_CLAUSE_COLLAPSE:
11810 clauses = c_parser_omp_clause_collapse (parser, clauses);
11811 c_name = "collapse";
11812 break;
11813 case PRAGMA_OMP_CLAUSE_COPYIN:
11814 clauses = c_parser_omp_clause_copyin (parser, clauses);
11815 c_name = "copyin";
11816 break;
11817 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
11818 clauses = c_parser_omp_clause_copyprivate (parser, clauses);
11819 c_name = "copyprivate";
11820 break;
11821 case PRAGMA_OMP_CLAUSE_DEFAULT:
11822 clauses = c_parser_omp_clause_default (parser, clauses);
11823 c_name = "default";
11824 break;
11825 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
11826 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
11827 c_name = "firstprivate";
11828 break;
11829 case PRAGMA_OMP_CLAUSE_FINAL:
11830 clauses = c_parser_omp_clause_final (parser, clauses);
11831 c_name = "final";
11832 break;
11833 case PRAGMA_OMP_CLAUSE_IF:
11834 clauses = c_parser_omp_clause_if (parser, clauses);
11835 c_name = "if";
11836 break;
11837 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
11838 clauses = c_parser_omp_clause_lastprivate (parser, clauses);
11839 c_name = "lastprivate";
11840 break;
11841 case PRAGMA_OMP_CLAUSE_MERGEABLE:
11842 clauses = c_parser_omp_clause_mergeable (parser, clauses);
11843 c_name = "mergeable";
11844 break;
11845 case PRAGMA_OMP_CLAUSE_NOWAIT:
11846 clauses = c_parser_omp_clause_nowait (parser, clauses);
11847 c_name = "nowait";
11848 break;
11849 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
11850 clauses = c_parser_omp_clause_num_threads (parser, clauses);
11851 c_name = "num_threads";
11852 break;
11853 case PRAGMA_OMP_CLAUSE_ORDERED:
11854 clauses = c_parser_omp_clause_ordered (parser, clauses);
11855 c_name = "ordered";
11856 break;
11857 case PRAGMA_OMP_CLAUSE_PRIVATE:
11858 clauses = c_parser_omp_clause_private (parser, clauses);
11859 c_name = "private";
11860 break;
11861 case PRAGMA_OMP_CLAUSE_REDUCTION:
11862 clauses = c_parser_omp_clause_reduction (parser, clauses);
11863 c_name = "reduction";
11864 break;
11865 case PRAGMA_OMP_CLAUSE_SCHEDULE:
11866 clauses = c_parser_omp_clause_schedule (parser, clauses);
11867 c_name = "schedule";
11868 break;
11869 case PRAGMA_OMP_CLAUSE_SHARED:
11870 clauses = c_parser_omp_clause_shared (parser, clauses);
11871 c_name = "shared";
11872 break;
11873 case PRAGMA_OMP_CLAUSE_UNTIED:
11874 clauses = c_parser_omp_clause_untied (parser, clauses);
11875 c_name = "untied";
11876 break;
11877 case PRAGMA_OMP_CLAUSE_INBRANCH:
11878 case PRAGMA_CILK_CLAUSE_MASK:
11879 clauses = c_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
11880 clauses);
11881 c_name = "inbranch";
11882 break;
11883 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
11884 case PRAGMA_CILK_CLAUSE_NOMASK:
11885 clauses = c_parser_omp_clause_branch (parser, OMP_CLAUSE_NOTINBRANCH,
11886 clauses);
11887 c_name = "notinbranch";
11888 break;
11889 case PRAGMA_OMP_CLAUSE_PARALLEL:
11890 clauses
11891 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
11892 clauses);
11893 c_name = "parallel";
11894 if (!first)
11896 clause_not_first:
11897 error_at (here, "%qs must be the first clause of %qs",
11898 c_name, where);
11899 clauses = prev;
11901 break;
11902 case PRAGMA_OMP_CLAUSE_FOR:
11903 clauses
11904 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
11905 clauses);
11906 c_name = "for";
11907 if (!first)
11908 goto clause_not_first;
11909 break;
11910 case PRAGMA_OMP_CLAUSE_SECTIONS:
11911 clauses
11912 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
11913 clauses);
11914 c_name = "sections";
11915 if (!first)
11916 goto clause_not_first;
11917 break;
11918 case PRAGMA_OMP_CLAUSE_TASKGROUP:
11919 clauses
11920 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
11921 clauses);
11922 c_name = "taskgroup";
11923 if (!first)
11924 goto clause_not_first;
11925 break;
11926 case PRAGMA_OMP_CLAUSE_TO:
11927 clauses = c_parser_omp_clause_to (parser, clauses);
11928 c_name = "to";
11929 break;
11930 case PRAGMA_OMP_CLAUSE_FROM:
11931 clauses = c_parser_omp_clause_from (parser, clauses);
11932 c_name = "from";
11933 break;
11934 case PRAGMA_OMP_CLAUSE_UNIFORM:
11935 clauses = c_parser_omp_clause_uniform (parser, clauses);
11936 c_name = "uniform";
11937 break;
11938 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
11939 clauses = c_parser_omp_clause_num_teams (parser, clauses);
11940 c_name = "num_teams";
11941 break;
11942 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
11943 clauses = c_parser_omp_clause_thread_limit (parser, clauses);
11944 c_name = "thread_limit";
11945 break;
11946 case PRAGMA_OMP_CLAUSE_ALIGNED:
11947 clauses = c_parser_omp_clause_aligned (parser, clauses);
11948 c_name = "aligned";
11949 break;
11950 case PRAGMA_OMP_CLAUSE_LINEAR:
11951 if (((mask >> PRAGMA_CILK_CLAUSE_VECTORLENGTH) & 1) != 0)
11952 cilk_simd_fn = true;
11953 clauses = c_parser_omp_clause_linear (parser, clauses, cilk_simd_fn);
11954 c_name = "linear";
11955 break;
11956 case PRAGMA_OMP_CLAUSE_DEPEND:
11957 clauses = c_parser_omp_clause_depend (parser, clauses);
11958 c_name = "depend";
11959 break;
11960 case PRAGMA_OMP_CLAUSE_MAP:
11961 clauses = c_parser_omp_clause_map (parser, clauses);
11962 c_name = "map";
11963 break;
11964 case PRAGMA_OMP_CLAUSE_DEVICE:
11965 clauses = c_parser_omp_clause_device (parser, clauses);
11966 c_name = "device";
11967 break;
11968 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
11969 clauses = c_parser_omp_clause_dist_schedule (parser, clauses);
11970 c_name = "dist_schedule";
11971 break;
11972 case PRAGMA_OMP_CLAUSE_PROC_BIND:
11973 clauses = c_parser_omp_clause_proc_bind (parser, clauses);
11974 c_name = "proc_bind";
11975 break;
11976 case PRAGMA_OMP_CLAUSE_SAFELEN:
11977 clauses = c_parser_omp_clause_safelen (parser, clauses);
11978 c_name = "safelen";
11979 break;
11980 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
11981 clauses = c_parser_cilk_clause_vectorlength (parser, clauses, true);
11982 c_name = "simdlen";
11983 break;
11984 case PRAGMA_OMP_CLAUSE_SIMDLEN:
11985 clauses = c_parser_omp_clause_simdlen (parser, clauses);
11986 c_name = "simdlen";
11987 break;
11988 default:
11989 c_parser_error (parser, "expected %<#pragma omp%> clause");
11990 goto saw_error;
11993 first = false;
11995 if (((mask >> c_kind) & 1) == 0)
11997 /* Remove the invalid clause(s) from the list to avoid
11998 confusing the rest of the compiler. */
11999 clauses = prev;
12000 error_at (here, "%qs is not valid for %qs", c_name, where);
12004 saw_error:
12005 c_parser_skip_to_pragma_eol (parser);
12007 if (finish_p)
12008 return c_finish_omp_clauses (clauses);
12010 return clauses;
12013 /* OpenACC 2.0, OpenMP 2.5:
12014 structured-block:
12015 statement
12017 In practice, we're also interested in adding the statement to an
12018 outer node. So it is convenient if we work around the fact that
12019 c_parser_statement calls add_stmt. */
12021 static tree
12022 c_parser_omp_structured_block (c_parser *parser)
12024 tree stmt = push_stmt_list ();
12025 c_parser_statement (parser);
12026 return pop_stmt_list (stmt);
12029 /* OpenACC 2.0:
12030 # pragma acc cache (variable-list) new-line
12032 LOC is the location of the #pragma token.
12035 static tree
12036 c_parser_oacc_cache (location_t loc, c_parser *parser)
12038 tree stmt, clauses;
12040 clauses = c_parser_omp_var_list_parens (parser, OMP_CLAUSE__CACHE_, NULL);
12041 clauses = c_finish_omp_clauses (clauses);
12043 c_parser_skip_to_pragma_eol (parser);
12045 stmt = make_node (OACC_CACHE);
12046 TREE_TYPE (stmt) = void_type_node;
12047 OACC_CACHE_CLAUSES (stmt) = clauses;
12048 SET_EXPR_LOCATION (stmt, loc);
12049 add_stmt (stmt);
12051 return stmt;
12054 /* OpenACC 2.0:
12055 # pragma acc data oacc-data-clause[optseq] new-line
12056 structured-block
12058 LOC is the location of the #pragma token.
12061 #define OACC_DATA_CLAUSE_MASK \
12062 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
12063 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
12064 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
12065 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
12066 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
12067 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
12068 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
12069 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
12070 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
12071 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
12072 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) )
12074 static tree
12075 c_parser_oacc_data (location_t loc, c_parser *parser)
12077 tree stmt, clauses, block;
12079 clauses = c_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
12080 "#pragma acc data");
12082 block = c_begin_omp_parallel ();
12083 add_stmt (c_parser_omp_structured_block (parser));
12085 stmt = c_finish_oacc_data (loc, clauses, block);
12087 return stmt;
12090 /* OpenACC 2.0:
12091 # pragma acc kernels oacc-kernels-clause[optseq] new-line
12092 structured-block
12094 LOC is the location of the #pragma token.
12097 #define OACC_KERNELS_CLAUSE_MASK \
12098 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
12099 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
12100 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
12101 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
12102 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
12103 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
12104 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
12105 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
12106 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
12107 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
12108 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
12109 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
12110 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
12112 static tree
12113 c_parser_oacc_kernels (location_t loc, c_parser *parser, char *p_name)
12115 tree stmt, clauses = NULL_TREE, block;
12117 strcat (p_name, " kernels");
12119 if (c_parser_next_token_is (parser, CPP_NAME))
12121 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12122 if (strcmp (p, "loop") == 0)
12124 c_parser_consume_token (parser);
12125 block = c_begin_omp_parallel ();
12126 c_parser_oacc_loop (loc, parser, p_name);
12127 stmt = c_finish_oacc_kernels (loc, clauses, block);
12128 OACC_KERNELS_COMBINED (stmt) = 1;
12129 return stmt;
12133 clauses = c_parser_oacc_all_clauses (parser, OACC_KERNELS_CLAUSE_MASK,
12134 p_name);
12136 block = c_begin_omp_parallel ();
12137 add_stmt (c_parser_omp_structured_block (parser));
12139 stmt = c_finish_oacc_kernels (loc, clauses, block);
12141 return stmt;
12144 /* OpenACC 2.0:
12145 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
12149 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
12152 LOC is the location of the #pragma token.
12155 #define OACC_ENTER_DATA_CLAUSE_MASK \
12156 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
12157 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
12158 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
12159 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
12160 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
12161 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
12162 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
12164 #define OACC_EXIT_DATA_CLAUSE_MASK \
12165 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
12166 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
12167 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
12168 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
12169 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
12171 static void
12172 c_parser_oacc_enter_exit_data (c_parser *parser, bool enter)
12174 location_t loc = c_parser_peek_token (parser)->location;
12175 tree clauses, stmt;
12177 c_parser_consume_pragma (parser);
12179 if (!c_parser_next_token_is (parser, CPP_NAME))
12181 c_parser_error (parser, enter
12182 ? "expected %<data%> in %<#pragma acc enter data%>"
12183 : "expected %<data%> in %<#pragma acc exit data%>");
12184 c_parser_skip_to_pragma_eol (parser);
12185 return;
12188 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12189 if (strcmp (p, "data") != 0)
12191 c_parser_error (parser, "invalid pragma");
12192 c_parser_skip_to_pragma_eol (parser);
12193 return;
12196 c_parser_consume_token (parser);
12198 if (enter)
12199 clauses = c_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
12200 "#pragma acc enter data");
12201 else
12202 clauses = c_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
12203 "#pragma acc exit data");
12205 if (find_omp_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
12207 error_at (loc, enter
12208 ? "%<#pragma acc enter data%> has no data movement clause"
12209 : "%<#pragma acc exit data%> has no data movement clause");
12210 return;
12213 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
12214 TREE_TYPE (stmt) = void_type_node;
12215 OMP_STANDALONE_CLAUSES (stmt) = clauses;
12216 SET_EXPR_LOCATION (stmt, loc);
12217 add_stmt (stmt);
12221 /* OpenACC 2.0:
12223 # pragma acc loop oacc-loop-clause[optseq] new-line
12224 structured-block
12226 LOC is the location of the #pragma token.
12229 #define OACC_LOOP_CLAUSE_MASK \
12230 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
12231 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) )
12233 static tree
12234 c_parser_oacc_loop (location_t loc, c_parser *parser, char *p_name)
12236 tree stmt, clauses, block;
12238 strcat (p_name, " loop");
12240 clauses = c_parser_oacc_all_clauses (parser, OACC_LOOP_CLAUSE_MASK, p_name);
12242 block = c_begin_compound_stmt (true);
12243 stmt = c_parser_omp_for_loop (loc, parser, OACC_LOOP, clauses, NULL);
12244 block = c_end_compound_stmt (loc, block, true);
12245 add_stmt (block);
12247 return stmt;
12250 /* OpenACC 2.0:
12251 # pragma acc parallel oacc-parallel-clause[optseq] new-line
12252 structured-block
12254 LOC is the location of the #pragma token.
12257 #define OACC_PARALLEL_CLAUSE_MASK \
12258 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
12259 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
12260 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
12261 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
12262 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
12263 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
12264 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
12265 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
12266 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
12267 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
12268 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
12269 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
12270 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
12271 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
12272 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
12273 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
12274 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
12276 static tree
12277 c_parser_oacc_parallel (location_t loc, c_parser *parser, char *p_name)
12279 tree stmt, clauses = NULL_TREE, block;
12281 strcat (p_name, " parallel");
12283 if (c_parser_next_token_is (parser, CPP_NAME))
12285 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12286 if (strcmp (p, "loop") == 0)
12288 c_parser_consume_token (parser);
12289 block = c_begin_omp_parallel ();
12290 c_parser_oacc_loop (loc, parser, p_name);
12291 stmt = c_finish_oacc_parallel (loc, clauses, block);
12292 OACC_PARALLEL_COMBINED (stmt) = 1;
12293 return stmt;
12297 clauses = c_parser_oacc_all_clauses (parser, OACC_PARALLEL_CLAUSE_MASK,
12298 p_name);
12300 block = c_begin_omp_parallel ();
12301 add_stmt (c_parser_omp_structured_block (parser));
12303 stmt = c_finish_oacc_parallel (loc, clauses, block);
12305 return stmt;
12308 /* OpenACC 2.0:
12309 # pragma acc update oacc-update-clause[optseq] new-line
12312 #define OACC_UPDATE_CLAUSE_MASK \
12313 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
12314 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
12315 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
12316 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
12317 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
12318 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
12320 static void
12321 c_parser_oacc_update (c_parser *parser)
12323 location_t loc = c_parser_peek_token (parser)->location;
12325 c_parser_consume_pragma (parser);
12327 tree clauses = c_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
12328 "#pragma acc update");
12329 if (find_omp_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
12331 error_at (loc,
12332 "%<#pragma acc update%> must contain at least one "
12333 "%<device%> or %<host/self%> clause");
12334 return;
12337 if (parser->error)
12338 return;
12340 tree stmt = make_node (OACC_UPDATE);
12341 TREE_TYPE (stmt) = void_type_node;
12342 OACC_UPDATE_CLAUSES (stmt) = clauses;
12343 SET_EXPR_LOCATION (stmt, loc);
12344 add_stmt (stmt);
12347 /* OpenACC 2.0:
12348 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
12350 LOC is the location of the #pragma token.
12353 #define OACC_WAIT_CLAUSE_MASK \
12354 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) )
12356 static tree
12357 c_parser_oacc_wait (location_t loc, c_parser *parser, char *p_name)
12359 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
12361 if (c_parser_peek_token (parser)->type == CPP_OPEN_PAREN)
12362 list = c_parser_oacc_wait_list (parser, loc, list);
12364 strcpy (p_name, " wait");
12365 clauses = c_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK, p_name);
12366 stmt = c_finish_oacc_wait (loc, list, clauses);
12368 return stmt;
12371 /* OpenMP 2.5:
12372 # pragma omp atomic new-line
12373 expression-stmt
12375 expression-stmt:
12376 x binop= expr | x++ | ++x | x-- | --x
12377 binop:
12378 +, *, -, /, &, ^, |, <<, >>
12380 where x is an lvalue expression with scalar type.
12382 OpenMP 3.1:
12383 # pragma omp atomic new-line
12384 update-stmt
12386 # pragma omp atomic read new-line
12387 read-stmt
12389 # pragma omp atomic write new-line
12390 write-stmt
12392 # pragma omp atomic update new-line
12393 update-stmt
12395 # pragma omp atomic capture new-line
12396 capture-stmt
12398 # pragma omp atomic capture new-line
12399 capture-block
12401 read-stmt:
12402 v = x
12403 write-stmt:
12404 x = expr
12405 update-stmt:
12406 expression-stmt | x = x binop expr
12407 capture-stmt:
12408 v = expression-stmt
12409 capture-block:
12410 { v = x; update-stmt; } | { update-stmt; v = x; }
12412 OpenMP 4.0:
12413 update-stmt:
12414 expression-stmt | x = x binop expr | x = expr binop x
12415 capture-stmt:
12416 v = update-stmt
12417 capture-block:
12418 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
12420 where x and v are lvalue expressions with scalar type.
12422 LOC is the location of the #pragma token. */
12424 static void
12425 c_parser_omp_atomic (location_t loc, c_parser *parser)
12427 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE;
12428 tree lhs1 = NULL_TREE, rhs1 = NULL_TREE;
12429 tree stmt, orig_lhs, unfolded_lhs = NULL_TREE, unfolded_lhs1 = NULL_TREE;
12430 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
12431 struct c_expr expr;
12432 location_t eloc;
12433 bool structured_block = false;
12434 bool swapped = false;
12435 bool seq_cst = false;
12436 bool non_lvalue_p;
12438 if (c_parser_next_token_is (parser, CPP_NAME))
12440 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12441 if (!strcmp (p, "seq_cst"))
12443 seq_cst = true;
12444 c_parser_consume_token (parser);
12445 if (c_parser_next_token_is (parser, CPP_COMMA)
12446 && c_parser_peek_2nd_token (parser)->type == CPP_NAME)
12447 c_parser_consume_token (parser);
12450 if (c_parser_next_token_is (parser, CPP_NAME))
12452 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12454 if (!strcmp (p, "read"))
12455 code = OMP_ATOMIC_READ;
12456 else if (!strcmp (p, "write"))
12457 code = NOP_EXPR;
12458 else if (!strcmp (p, "update"))
12459 code = OMP_ATOMIC;
12460 else if (!strcmp (p, "capture"))
12461 code = OMP_ATOMIC_CAPTURE_NEW;
12462 else
12463 p = NULL;
12464 if (p)
12465 c_parser_consume_token (parser);
12467 if (!seq_cst)
12469 if (c_parser_next_token_is (parser, CPP_COMMA)
12470 && c_parser_peek_2nd_token (parser)->type == CPP_NAME)
12471 c_parser_consume_token (parser);
12473 if (c_parser_next_token_is (parser, CPP_NAME))
12475 const char *p
12476 = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12477 if (!strcmp (p, "seq_cst"))
12479 seq_cst = true;
12480 c_parser_consume_token (parser);
12484 c_parser_skip_to_pragma_eol (parser);
12486 switch (code)
12488 case OMP_ATOMIC_READ:
12489 case NOP_EXPR: /* atomic write */
12490 v = c_parser_cast_expression (parser, NULL).value;
12491 non_lvalue_p = !lvalue_p (v);
12492 v = c_fully_fold (v, false, NULL);
12493 if (v == error_mark_node)
12494 goto saw_error;
12495 if (non_lvalue_p)
12496 v = non_lvalue (v);
12497 loc = c_parser_peek_token (parser)->location;
12498 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
12499 goto saw_error;
12500 if (code == NOP_EXPR)
12502 lhs = c_parser_expression (parser).value;
12503 lhs = c_fully_fold (lhs, false, NULL);
12504 if (lhs == error_mark_node)
12505 goto saw_error;
12507 else
12509 lhs = c_parser_cast_expression (parser, NULL).value;
12510 non_lvalue_p = !lvalue_p (lhs);
12511 lhs = c_fully_fold (lhs, false, NULL);
12512 if (lhs == error_mark_node)
12513 goto saw_error;
12514 if (non_lvalue_p)
12515 lhs = non_lvalue (lhs);
12517 if (code == NOP_EXPR)
12519 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
12520 opcode. */
12521 code = OMP_ATOMIC;
12522 rhs = lhs;
12523 lhs = v;
12524 v = NULL_TREE;
12526 goto done;
12527 case OMP_ATOMIC_CAPTURE_NEW:
12528 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
12530 c_parser_consume_token (parser);
12531 structured_block = true;
12533 else
12535 v = c_parser_cast_expression (parser, NULL).value;
12536 non_lvalue_p = !lvalue_p (v);
12537 v = c_fully_fold (v, false, NULL);
12538 if (v == error_mark_node)
12539 goto saw_error;
12540 if (non_lvalue_p)
12541 v = non_lvalue (v);
12542 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
12543 goto saw_error;
12545 break;
12546 default:
12547 break;
12550 /* For structured_block case we don't know yet whether
12551 old or new x should be captured. */
12552 restart:
12553 eloc = c_parser_peek_token (parser)->location;
12554 expr = c_parser_cast_expression (parser, NULL);
12555 lhs = expr.value;
12556 expr = default_function_array_conversion (eloc, expr);
12557 unfolded_lhs = expr.value;
12558 lhs = c_fully_fold (lhs, false, NULL);
12559 orig_lhs = lhs;
12560 switch (TREE_CODE (lhs))
12562 case ERROR_MARK:
12563 saw_error:
12564 c_parser_skip_to_end_of_block_or_statement (parser);
12565 if (structured_block)
12567 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
12568 c_parser_consume_token (parser);
12569 else if (code == OMP_ATOMIC_CAPTURE_NEW)
12571 c_parser_skip_to_end_of_block_or_statement (parser);
12572 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
12573 c_parser_consume_token (parser);
12576 return;
12578 case POSTINCREMENT_EXPR:
12579 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
12580 code = OMP_ATOMIC_CAPTURE_OLD;
12581 /* FALLTHROUGH */
12582 case PREINCREMENT_EXPR:
12583 lhs = TREE_OPERAND (lhs, 0);
12584 unfolded_lhs = NULL_TREE;
12585 opcode = PLUS_EXPR;
12586 rhs = integer_one_node;
12587 break;
12589 case POSTDECREMENT_EXPR:
12590 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
12591 code = OMP_ATOMIC_CAPTURE_OLD;
12592 /* FALLTHROUGH */
12593 case PREDECREMENT_EXPR:
12594 lhs = TREE_OPERAND (lhs, 0);
12595 unfolded_lhs = NULL_TREE;
12596 opcode = MINUS_EXPR;
12597 rhs = integer_one_node;
12598 break;
12600 case COMPOUND_EXPR:
12601 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
12602 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
12603 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
12604 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
12605 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
12606 (TREE_OPERAND (lhs, 1), 0), 0)))
12607 == BOOLEAN_TYPE)
12608 /* Undo effects of boolean_increment for post {in,de}crement. */
12609 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
12610 /* FALLTHRU */
12611 case MODIFY_EXPR:
12612 if (TREE_CODE (lhs) == MODIFY_EXPR
12613 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
12615 /* Undo effects of boolean_increment. */
12616 if (integer_onep (TREE_OPERAND (lhs, 1)))
12618 /* This is pre or post increment. */
12619 rhs = TREE_OPERAND (lhs, 1);
12620 lhs = TREE_OPERAND (lhs, 0);
12621 unfolded_lhs = NULL_TREE;
12622 opcode = NOP_EXPR;
12623 if (code == OMP_ATOMIC_CAPTURE_NEW
12624 && !structured_block
12625 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
12626 code = OMP_ATOMIC_CAPTURE_OLD;
12627 break;
12629 if (TREE_CODE (TREE_OPERAND (lhs, 1)) == TRUTH_NOT_EXPR
12630 && TREE_OPERAND (lhs, 0)
12631 == TREE_OPERAND (TREE_OPERAND (lhs, 1), 0))
12633 /* This is pre or post decrement. */
12634 rhs = TREE_OPERAND (lhs, 1);
12635 lhs = TREE_OPERAND (lhs, 0);
12636 unfolded_lhs = NULL_TREE;
12637 opcode = NOP_EXPR;
12638 if (code == OMP_ATOMIC_CAPTURE_NEW
12639 && !structured_block
12640 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
12641 code = OMP_ATOMIC_CAPTURE_OLD;
12642 break;
12645 /* FALLTHRU */
12646 default:
12647 if (!lvalue_p (unfolded_lhs))
12648 lhs = non_lvalue (lhs);
12649 switch (c_parser_peek_token (parser)->type)
12651 case CPP_MULT_EQ:
12652 opcode = MULT_EXPR;
12653 break;
12654 case CPP_DIV_EQ:
12655 opcode = TRUNC_DIV_EXPR;
12656 break;
12657 case CPP_PLUS_EQ:
12658 opcode = PLUS_EXPR;
12659 break;
12660 case CPP_MINUS_EQ:
12661 opcode = MINUS_EXPR;
12662 break;
12663 case CPP_LSHIFT_EQ:
12664 opcode = LSHIFT_EXPR;
12665 break;
12666 case CPP_RSHIFT_EQ:
12667 opcode = RSHIFT_EXPR;
12668 break;
12669 case CPP_AND_EQ:
12670 opcode = BIT_AND_EXPR;
12671 break;
12672 case CPP_OR_EQ:
12673 opcode = BIT_IOR_EXPR;
12674 break;
12675 case CPP_XOR_EQ:
12676 opcode = BIT_XOR_EXPR;
12677 break;
12678 case CPP_EQ:
12679 c_parser_consume_token (parser);
12680 eloc = c_parser_peek_token (parser)->location;
12681 expr = c_parser_expr_no_commas (parser, NULL, unfolded_lhs);
12682 rhs1 = expr.value;
12683 switch (TREE_CODE (rhs1))
12685 case MULT_EXPR:
12686 case TRUNC_DIV_EXPR:
12687 case RDIV_EXPR:
12688 case PLUS_EXPR:
12689 case MINUS_EXPR:
12690 case LSHIFT_EXPR:
12691 case RSHIFT_EXPR:
12692 case BIT_AND_EXPR:
12693 case BIT_IOR_EXPR:
12694 case BIT_XOR_EXPR:
12695 if (c_tree_equal (TREE_OPERAND (rhs1, 0), unfolded_lhs))
12697 opcode = TREE_CODE (rhs1);
12698 rhs = c_fully_fold (TREE_OPERAND (rhs1, 1), false, NULL);
12699 rhs1 = c_fully_fold (TREE_OPERAND (rhs1, 0), false, NULL);
12700 goto stmt_done;
12702 if (c_tree_equal (TREE_OPERAND (rhs1, 1), unfolded_lhs))
12704 opcode = TREE_CODE (rhs1);
12705 rhs = c_fully_fold (TREE_OPERAND (rhs1, 0), false, NULL);
12706 rhs1 = c_fully_fold (TREE_OPERAND (rhs1, 1), false, NULL);
12707 swapped = !commutative_tree_code (opcode);
12708 goto stmt_done;
12710 break;
12711 case ERROR_MARK:
12712 goto saw_error;
12713 default:
12714 break;
12716 if (c_parser_peek_token (parser)->type == CPP_SEMICOLON)
12718 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
12720 code = OMP_ATOMIC_CAPTURE_OLD;
12721 v = lhs;
12722 lhs = NULL_TREE;
12723 expr = default_function_array_read_conversion (eloc, expr);
12724 unfolded_lhs1 = expr.value;
12725 lhs1 = c_fully_fold (unfolded_lhs1, false, NULL);
12726 rhs1 = NULL_TREE;
12727 c_parser_consume_token (parser);
12728 goto restart;
12730 if (structured_block)
12732 opcode = NOP_EXPR;
12733 expr = default_function_array_read_conversion (eloc, expr);
12734 rhs = c_fully_fold (expr.value, false, NULL);
12735 rhs1 = NULL_TREE;
12736 goto stmt_done;
12739 c_parser_error (parser, "invalid form of %<#pragma omp atomic%>");
12740 goto saw_error;
12741 default:
12742 c_parser_error (parser,
12743 "invalid operator for %<#pragma omp atomic%>");
12744 goto saw_error;
12747 /* Arrange to pass the location of the assignment operator to
12748 c_finish_omp_atomic. */
12749 loc = c_parser_peek_token (parser)->location;
12750 c_parser_consume_token (parser);
12751 eloc = c_parser_peek_token (parser)->location;
12752 expr = c_parser_expression (parser);
12753 expr = default_function_array_read_conversion (eloc, expr);
12754 rhs = expr.value;
12755 rhs = c_fully_fold (rhs, false, NULL);
12756 break;
12758 stmt_done:
12759 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
12761 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
12762 goto saw_error;
12763 v = c_parser_cast_expression (parser, NULL).value;
12764 non_lvalue_p = !lvalue_p (v);
12765 v = c_fully_fold (v, false, NULL);
12766 if (v == error_mark_node)
12767 goto saw_error;
12768 if (non_lvalue_p)
12769 v = non_lvalue (v);
12770 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
12771 goto saw_error;
12772 eloc = c_parser_peek_token (parser)->location;
12773 expr = c_parser_cast_expression (parser, NULL);
12774 lhs1 = expr.value;
12775 expr = default_function_array_read_conversion (eloc, expr);
12776 unfolded_lhs1 = expr.value;
12777 lhs1 = c_fully_fold (lhs1, false, NULL);
12778 if (lhs1 == error_mark_node)
12779 goto saw_error;
12780 if (!lvalue_p (unfolded_lhs1))
12781 lhs1 = non_lvalue (lhs1);
12783 if (structured_block)
12785 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
12786 c_parser_require (parser, CPP_CLOSE_BRACE, "expected %<}%>");
12788 done:
12789 if (unfolded_lhs && unfolded_lhs1
12790 && !c_tree_equal (unfolded_lhs, unfolded_lhs1))
12792 error ("%<#pragma omp atomic capture%> uses two different "
12793 "expressions for memory");
12794 stmt = error_mark_node;
12796 else
12797 stmt = c_finish_omp_atomic (loc, code, opcode, lhs, rhs, v, lhs1, rhs1,
12798 swapped, seq_cst);
12799 if (stmt != error_mark_node)
12800 add_stmt (stmt);
12802 if (!structured_block)
12803 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
12807 /* OpenMP 2.5:
12808 # pragma omp barrier new-line
12811 static void
12812 c_parser_omp_barrier (c_parser *parser)
12814 location_t loc = c_parser_peek_token (parser)->location;
12815 c_parser_consume_pragma (parser);
12816 c_parser_skip_to_pragma_eol (parser);
12818 c_finish_omp_barrier (loc);
12821 /* OpenMP 2.5:
12822 # pragma omp critical [(name)] new-line
12823 structured-block
12825 LOC is the location of the #pragma itself. */
12827 static tree
12828 c_parser_omp_critical (location_t loc, c_parser *parser)
12830 tree stmt, name = NULL;
12832 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
12834 c_parser_consume_token (parser);
12835 if (c_parser_next_token_is (parser, CPP_NAME))
12837 name = c_parser_peek_token (parser)->value;
12838 c_parser_consume_token (parser);
12839 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12841 else
12842 c_parser_error (parser, "expected identifier");
12844 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
12845 c_parser_error (parser, "expected %<(%> or end of line");
12846 c_parser_skip_to_pragma_eol (parser);
12848 stmt = c_parser_omp_structured_block (parser);
12849 return c_finish_omp_critical (loc, stmt, name);
12852 /* OpenMP 2.5:
12853 # pragma omp flush flush-vars[opt] new-line
12855 flush-vars:
12856 ( variable-list ) */
12858 static void
12859 c_parser_omp_flush (c_parser *parser)
12861 location_t loc = c_parser_peek_token (parser)->location;
12862 c_parser_consume_pragma (parser);
12863 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
12864 c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
12865 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
12866 c_parser_error (parser, "expected %<(%> or end of line");
12867 c_parser_skip_to_pragma_eol (parser);
12869 c_finish_omp_flush (loc);
12872 /* Parse the restricted form of loop statements allowed by OpenACC and OpenMP.
12873 The real trick here is to determine the loop control variable early
12874 so that we can push a new decl if necessary to make it private.
12875 LOC is the location of the "acc" or "omp" in "#pragma acc" or "#pragma omp",
12876 respectively. */
12878 static tree
12879 c_parser_omp_for_loop (location_t loc, c_parser *parser, enum tree_code code,
12880 tree clauses, tree *cclauses)
12882 tree decl, cond, incr, save_break, save_cont, body, init, stmt, cl;
12883 tree declv, condv, incrv, initv, ret = NULL_TREE;
12884 tree pre_body = NULL_TREE, this_pre_body;
12885 bool fail = false, open_brace_parsed = false;
12886 int i, collapse = 1, nbraces = 0;
12887 location_t for_loc;
12888 vec<tree, va_gc> *for_block = make_tree_vector ();
12890 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
12891 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
12892 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
12894 gcc_assert (collapse >= 1);
12896 declv = make_tree_vec (collapse);
12897 initv = make_tree_vec (collapse);
12898 condv = make_tree_vec (collapse);
12899 incrv = make_tree_vec (collapse);
12901 if (code != CILK_FOR
12902 && !c_parser_next_token_is_keyword (parser, RID_FOR))
12904 c_parser_error (parser, "for statement expected");
12905 return NULL;
12907 if (code == CILK_FOR
12908 && !c_parser_next_token_is_keyword (parser, RID_CILK_FOR))
12910 c_parser_error (parser, "_Cilk_for statement expected");
12911 return NULL;
12913 for_loc = c_parser_peek_token (parser)->location;
12914 c_parser_consume_token (parser);
12916 for (i = 0; i < collapse; i++)
12918 int bracecount = 0;
12920 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12921 goto pop_scopes;
12923 /* Parse the initialization declaration or expression. */
12924 if (c_parser_next_tokens_start_declaration (parser))
12926 if (i > 0)
12927 vec_safe_push (for_block, c_begin_compound_stmt (true));
12928 this_pre_body = push_stmt_list ();
12929 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
12930 NULL, vNULL);
12931 if (this_pre_body)
12933 this_pre_body = pop_stmt_list (this_pre_body);
12934 if (pre_body)
12936 tree t = pre_body;
12937 pre_body = push_stmt_list ();
12938 add_stmt (t);
12939 add_stmt (this_pre_body);
12940 pre_body = pop_stmt_list (pre_body);
12942 else
12943 pre_body = this_pre_body;
12945 decl = check_for_loop_decls (for_loc, flag_isoc99);
12946 if (decl == NULL)
12947 goto error_init;
12948 if (DECL_INITIAL (decl) == error_mark_node)
12949 decl = error_mark_node;
12950 init = decl;
12952 else if (c_parser_next_token_is (parser, CPP_NAME)
12953 && c_parser_peek_2nd_token (parser)->type == CPP_EQ)
12955 struct c_expr decl_exp;
12956 struct c_expr init_exp;
12957 location_t init_loc;
12959 decl_exp = c_parser_postfix_expression (parser);
12960 decl = decl_exp.value;
12962 c_parser_require (parser, CPP_EQ, "expected %<=%>");
12964 init_loc = c_parser_peek_token (parser)->location;
12965 init_exp = c_parser_expr_no_commas (parser, NULL);
12966 init_exp = default_function_array_read_conversion (init_loc,
12967 init_exp);
12968 init = build_modify_expr (init_loc, decl, decl_exp.original_type,
12969 NOP_EXPR, init_loc, init_exp.value,
12970 init_exp.original_type);
12971 init = c_process_expr_stmt (init_loc, init);
12973 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
12975 else
12977 error_init:
12978 c_parser_error (parser,
12979 "expected iteration declaration or initialization");
12980 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
12981 "expected %<)%>");
12982 fail = true;
12983 goto parse_next;
12986 /* Parse the loop condition. */
12987 cond = NULL_TREE;
12988 if (c_parser_next_token_is_not (parser, CPP_SEMICOLON))
12990 location_t cond_loc = c_parser_peek_token (parser)->location;
12991 struct c_expr cond_expr
12992 = c_parser_binary_expression (parser, NULL, NULL_TREE);
12994 cond = cond_expr.value;
12995 cond = c_objc_common_truthvalue_conversion (cond_loc, cond);
12996 cond = c_fully_fold (cond, false, NULL);
12997 switch (cond_expr.original_code)
12999 case GT_EXPR:
13000 case GE_EXPR:
13001 case LT_EXPR:
13002 case LE_EXPR:
13003 break;
13004 case NE_EXPR:
13005 if (code == CILK_SIMD || code == CILK_FOR)
13006 break;
13007 /* FALLTHRU. */
13008 default:
13009 /* Can't be cond = error_mark_node, because we want to preserve
13010 the location until c_finish_omp_for. */
13011 cond = build1 (NOP_EXPR, boolean_type_node, error_mark_node);
13012 break;
13014 protected_set_expr_location (cond, cond_loc);
13016 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
13018 /* Parse the increment expression. */
13019 incr = NULL_TREE;
13020 if (c_parser_next_token_is_not (parser, CPP_CLOSE_PAREN))
13022 location_t incr_loc = c_parser_peek_token (parser)->location;
13024 incr = c_process_expr_stmt (incr_loc,
13025 c_parser_expression (parser).value);
13027 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
13029 if (decl == NULL || decl == error_mark_node || init == error_mark_node)
13030 fail = true;
13031 else
13033 TREE_VEC_ELT (declv, i) = decl;
13034 TREE_VEC_ELT (initv, i) = init;
13035 TREE_VEC_ELT (condv, i) = cond;
13036 TREE_VEC_ELT (incrv, i) = incr;
13039 parse_next:
13040 if (i == collapse - 1)
13041 break;
13043 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
13044 in between the collapsed for loops to be still considered perfectly
13045 nested. Hopefully the final version clarifies this.
13046 For now handle (multiple) {'s and empty statements. */
13049 if (c_parser_next_token_is_keyword (parser, RID_FOR))
13051 c_parser_consume_token (parser);
13052 break;
13054 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
13056 c_parser_consume_token (parser);
13057 bracecount++;
13059 else if (bracecount
13060 && c_parser_next_token_is (parser, CPP_SEMICOLON))
13061 c_parser_consume_token (parser);
13062 else
13064 c_parser_error (parser, "not enough perfectly nested loops");
13065 if (bracecount)
13067 open_brace_parsed = true;
13068 bracecount--;
13070 fail = true;
13071 collapse = 0;
13072 break;
13075 while (1);
13077 nbraces += bracecount;
13080 save_break = c_break_label;
13081 if (code == CILK_SIMD)
13082 c_break_label = build_int_cst (size_type_node, 2);
13083 else
13084 c_break_label = size_one_node;
13085 save_cont = c_cont_label;
13086 c_cont_label = NULL_TREE;
13087 body = push_stmt_list ();
13089 if (open_brace_parsed)
13091 location_t here = c_parser_peek_token (parser)->location;
13092 stmt = c_begin_compound_stmt (true);
13093 c_parser_compound_statement_nostart (parser);
13094 add_stmt (c_end_compound_stmt (here, stmt, true));
13096 else
13097 add_stmt (c_parser_c99_block_statement (parser));
13098 if (c_cont_label)
13100 tree t = build1 (LABEL_EXPR, void_type_node, c_cont_label);
13101 SET_EXPR_LOCATION (t, loc);
13102 add_stmt (t);
13105 body = pop_stmt_list (body);
13106 c_break_label = save_break;
13107 c_cont_label = save_cont;
13109 while (nbraces)
13111 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
13113 c_parser_consume_token (parser);
13114 nbraces--;
13116 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
13117 c_parser_consume_token (parser);
13118 else
13120 c_parser_error (parser, "collapsed loops not perfectly nested");
13121 while (nbraces)
13123 location_t here = c_parser_peek_token (parser)->location;
13124 stmt = c_begin_compound_stmt (true);
13125 add_stmt (body);
13126 c_parser_compound_statement_nostart (parser);
13127 body = c_end_compound_stmt (here, stmt, true);
13128 nbraces--;
13130 goto pop_scopes;
13134 /* Only bother calling c_finish_omp_for if we haven't already generated
13135 an error from the initialization parsing. */
13136 if (!fail)
13138 stmt = c_finish_omp_for (loc, code, declv, initv, condv,
13139 incrv, body, pre_body);
13140 if (stmt)
13142 if (cclauses != NULL
13143 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL)
13145 tree *c;
13146 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
13147 if (OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_FIRSTPRIVATE
13148 && OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_LASTPRIVATE)
13149 c = &OMP_CLAUSE_CHAIN (*c);
13150 else
13152 for (i = 0; i < collapse; i++)
13153 if (TREE_VEC_ELT (declv, i) == OMP_CLAUSE_DECL (*c))
13154 break;
13155 if (i == collapse)
13156 c = &OMP_CLAUSE_CHAIN (*c);
13157 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE)
13159 error_at (loc,
13160 "iteration variable %qD should not be firstprivate",
13161 OMP_CLAUSE_DECL (*c));
13162 *c = OMP_CLAUSE_CHAIN (*c);
13164 else
13166 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
13167 tree l = *c;
13168 *c = OMP_CLAUSE_CHAIN (*c);
13169 if (code == OMP_SIMD)
13171 OMP_CLAUSE_CHAIN (l)
13172 = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
13173 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
13175 else
13177 OMP_CLAUSE_CHAIN (l) = clauses;
13178 clauses = l;
13183 OMP_FOR_CLAUSES (stmt) = clauses;
13185 ret = stmt;
13187 pop_scopes:
13188 while (!for_block->is_empty ())
13190 /* FIXME diagnostics: LOC below should be the actual location of
13191 this particular for block. We need to build a list of
13192 locations to go along with FOR_BLOCK. */
13193 stmt = c_end_compound_stmt (loc, for_block->pop (), true);
13194 add_stmt (stmt);
13196 release_tree_vector (for_block);
13197 return ret;
13200 /* Helper function for OpenMP parsing, split clauses and call
13201 finish_omp_clauses on each of the set of clauses afterwards. */
13203 static void
13204 omp_split_clauses (location_t loc, enum tree_code code,
13205 omp_clause_mask mask, tree clauses, tree *cclauses)
13207 int i;
13208 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
13209 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
13210 if (cclauses[i])
13211 cclauses[i] = c_finish_omp_clauses (cclauses[i]);
13214 /* OpenMP 4.0:
13215 #pragma omp simd simd-clause[optseq] new-line
13216 for-loop
13218 LOC is the location of the #pragma token.
13221 #define OMP_SIMD_CLAUSE_MASK \
13222 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
13223 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
13224 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
13225 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
13226 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
13227 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
13228 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
13230 static tree
13231 c_parser_omp_simd (location_t loc, c_parser *parser,
13232 char *p_name, omp_clause_mask mask, tree *cclauses)
13234 tree block, clauses, ret;
13236 strcat (p_name, " simd");
13237 mask |= OMP_SIMD_CLAUSE_MASK;
13238 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
13240 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
13241 if (cclauses)
13243 omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
13244 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
13247 block = c_begin_compound_stmt (true);
13248 ret = c_parser_omp_for_loop (loc, parser, OMP_SIMD, clauses, cclauses);
13249 block = c_end_compound_stmt (loc, block, true);
13250 add_stmt (block);
13252 return ret;
13255 /* OpenMP 2.5:
13256 #pragma omp for for-clause[optseq] new-line
13257 for-loop
13259 OpenMP 4.0:
13260 #pragma omp for simd for-simd-clause[optseq] new-line
13261 for-loop
13263 LOC is the location of the #pragma token.
13266 #define OMP_FOR_CLAUSE_MASK \
13267 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
13268 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
13269 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
13270 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
13271 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
13272 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
13273 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
13274 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
13276 static tree
13277 c_parser_omp_for (location_t loc, c_parser *parser,
13278 char *p_name, omp_clause_mask mask, tree *cclauses)
13280 tree block, clauses, ret;
13282 strcat (p_name, " for");
13283 mask |= OMP_FOR_CLAUSE_MASK;
13284 if (cclauses)
13285 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
13287 if (c_parser_next_token_is (parser, CPP_NAME))
13289 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13291 if (strcmp (p, "simd") == 0)
13293 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
13294 if (cclauses == NULL)
13295 cclauses = cclauses_buf;
13297 c_parser_consume_token (parser);
13298 if (!flag_openmp) /* flag_openmp_simd */
13299 return c_parser_omp_simd (loc, parser, p_name, mask, cclauses);
13300 block = c_begin_compound_stmt (true);
13301 ret = c_parser_omp_simd (loc, parser, p_name, mask, cclauses);
13302 block = c_end_compound_stmt (loc, block, true);
13303 if (ret == NULL_TREE)
13304 return ret;
13305 ret = make_node (OMP_FOR);
13306 TREE_TYPE (ret) = void_type_node;
13307 OMP_FOR_BODY (ret) = block;
13308 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
13309 SET_EXPR_LOCATION (ret, loc);
13310 add_stmt (ret);
13311 return ret;
13314 if (!flag_openmp) /* flag_openmp_simd */
13316 c_parser_skip_to_pragma_eol (parser, false);
13317 return NULL_TREE;
13320 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
13321 if (cclauses)
13323 omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
13324 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
13327 block = c_begin_compound_stmt (true);
13328 ret = c_parser_omp_for_loop (loc, parser, OMP_FOR, clauses, cclauses);
13329 block = c_end_compound_stmt (loc, block, true);
13330 add_stmt (block);
13332 return ret;
13335 /* OpenMP 2.5:
13336 # pragma omp master new-line
13337 structured-block
13339 LOC is the location of the #pragma token.
13342 static tree
13343 c_parser_omp_master (location_t loc, c_parser *parser)
13345 c_parser_skip_to_pragma_eol (parser);
13346 return c_finish_omp_master (loc, c_parser_omp_structured_block (parser));
13349 /* OpenMP 2.5:
13350 # pragma omp ordered new-line
13351 structured-block
13353 LOC is the location of the #pragma itself.
13356 static tree
13357 c_parser_omp_ordered (location_t loc, c_parser *parser)
13359 c_parser_skip_to_pragma_eol (parser);
13360 return c_finish_omp_ordered (loc, c_parser_omp_structured_block (parser));
13363 /* OpenMP 2.5:
13365 section-scope:
13366 { section-sequence }
13368 section-sequence:
13369 section-directive[opt] structured-block
13370 section-sequence section-directive structured-block
13372 SECTIONS_LOC is the location of the #pragma omp sections. */
13374 static tree
13375 c_parser_omp_sections_scope (location_t sections_loc, c_parser *parser)
13377 tree stmt, substmt;
13378 bool error_suppress = false;
13379 location_t loc;
13381 loc = c_parser_peek_token (parser)->location;
13382 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
13384 /* Avoid skipping until the end of the block. */
13385 parser->error = false;
13386 return NULL_TREE;
13389 stmt = push_stmt_list ();
13391 if (c_parser_peek_token (parser)->pragma_kind != PRAGMA_OMP_SECTION)
13393 substmt = c_parser_omp_structured_block (parser);
13394 substmt = build1 (OMP_SECTION, void_type_node, substmt);
13395 SET_EXPR_LOCATION (substmt, loc);
13396 add_stmt (substmt);
13399 while (1)
13401 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
13402 break;
13403 if (c_parser_next_token_is (parser, CPP_EOF))
13404 break;
13406 loc = c_parser_peek_token (parser)->location;
13407 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_OMP_SECTION)
13409 c_parser_consume_pragma (parser);
13410 c_parser_skip_to_pragma_eol (parser);
13411 error_suppress = false;
13413 else if (!error_suppress)
13415 error_at (loc, "expected %<#pragma omp section%> or %<}%>");
13416 error_suppress = true;
13419 substmt = c_parser_omp_structured_block (parser);
13420 substmt = build1 (OMP_SECTION, void_type_node, substmt);
13421 SET_EXPR_LOCATION (substmt, loc);
13422 add_stmt (substmt);
13424 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE,
13425 "expected %<#pragma omp section%> or %<}%>");
13427 substmt = pop_stmt_list (stmt);
13429 stmt = make_node (OMP_SECTIONS);
13430 SET_EXPR_LOCATION (stmt, sections_loc);
13431 TREE_TYPE (stmt) = void_type_node;
13432 OMP_SECTIONS_BODY (stmt) = substmt;
13434 return add_stmt (stmt);
13437 /* OpenMP 2.5:
13438 # pragma omp sections sections-clause[optseq] newline
13439 sections-scope
13441 LOC is the location of the #pragma token.
13444 #define OMP_SECTIONS_CLAUSE_MASK \
13445 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
13446 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
13447 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
13448 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
13449 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
13451 static tree
13452 c_parser_omp_sections (location_t loc, c_parser *parser,
13453 char *p_name, omp_clause_mask mask, tree *cclauses)
13455 tree block, clauses, ret;
13457 strcat (p_name, " sections");
13458 mask |= OMP_SECTIONS_CLAUSE_MASK;
13459 if (cclauses)
13460 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
13462 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
13463 if (cclauses)
13465 omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
13466 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
13469 block = c_begin_compound_stmt (true);
13470 ret = c_parser_omp_sections_scope (loc, parser);
13471 if (ret)
13472 OMP_SECTIONS_CLAUSES (ret) = clauses;
13473 block = c_end_compound_stmt (loc, block, true);
13474 add_stmt (block);
13476 return ret;
13479 /* OpenMP 2.5:
13480 # pragma omp parallel parallel-clause[optseq] new-line
13481 structured-block
13482 # pragma omp parallel for parallel-for-clause[optseq] new-line
13483 structured-block
13484 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
13485 structured-block
13487 OpenMP 4.0:
13488 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
13489 structured-block
13491 LOC is the location of the #pragma token.
13494 #define OMP_PARALLEL_CLAUSE_MASK \
13495 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
13496 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
13497 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
13498 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
13499 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
13500 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
13501 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
13502 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
13503 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
13505 static tree
13506 c_parser_omp_parallel (location_t loc, c_parser *parser,
13507 char *p_name, omp_clause_mask mask, tree *cclauses)
13509 tree stmt, clauses, block;
13511 strcat (p_name, " parallel");
13512 mask |= OMP_PARALLEL_CLAUSE_MASK;
13514 if (c_parser_next_token_is_keyword (parser, RID_FOR))
13516 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
13517 if (cclauses == NULL)
13518 cclauses = cclauses_buf;
13520 c_parser_consume_token (parser);
13521 if (!flag_openmp) /* flag_openmp_simd */
13522 return c_parser_omp_for (loc, parser, p_name, mask, cclauses);
13523 block = c_begin_omp_parallel ();
13524 tree ret = c_parser_omp_for (loc, parser, p_name, mask, cclauses);
13525 stmt
13526 = c_finish_omp_parallel (loc, cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
13527 block);
13528 if (ret == NULL_TREE)
13529 return ret;
13530 OMP_PARALLEL_COMBINED (stmt) = 1;
13531 return stmt;
13533 else if (cclauses)
13535 error_at (loc, "expected %<for%> after %qs", p_name);
13536 c_parser_skip_to_pragma_eol (parser);
13537 return NULL_TREE;
13539 else if (!flag_openmp) /* flag_openmp_simd */
13541 c_parser_skip_to_pragma_eol (parser, false);
13542 return NULL_TREE;
13544 else if (c_parser_next_token_is (parser, CPP_NAME))
13546 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13547 if (strcmp (p, "sections") == 0)
13549 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
13550 if (cclauses == NULL)
13551 cclauses = cclauses_buf;
13553 c_parser_consume_token (parser);
13554 block = c_begin_omp_parallel ();
13555 c_parser_omp_sections (loc, parser, p_name, mask, cclauses);
13556 stmt = c_finish_omp_parallel (loc,
13557 cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
13558 block);
13559 OMP_PARALLEL_COMBINED (stmt) = 1;
13560 return stmt;
13564 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
13566 block = c_begin_omp_parallel ();
13567 c_parser_statement (parser);
13568 stmt = c_finish_omp_parallel (loc, clauses, block);
13570 return stmt;
13573 /* OpenMP 2.5:
13574 # pragma omp single single-clause[optseq] new-line
13575 structured-block
13577 LOC is the location of the #pragma.
13580 #define OMP_SINGLE_CLAUSE_MASK \
13581 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
13582 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
13583 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
13584 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
13586 static tree
13587 c_parser_omp_single (location_t loc, c_parser *parser)
13589 tree stmt = make_node (OMP_SINGLE);
13590 SET_EXPR_LOCATION (stmt, loc);
13591 TREE_TYPE (stmt) = void_type_node;
13593 OMP_SINGLE_CLAUSES (stmt)
13594 = c_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
13595 "#pragma omp single");
13596 OMP_SINGLE_BODY (stmt) = c_parser_omp_structured_block (parser);
13598 return add_stmt (stmt);
13601 /* OpenMP 3.0:
13602 # pragma omp task task-clause[optseq] new-line
13604 LOC is the location of the #pragma.
13607 #define OMP_TASK_CLAUSE_MASK \
13608 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
13609 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
13610 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
13611 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
13612 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
13613 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
13614 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
13615 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
13616 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND))
13618 static tree
13619 c_parser_omp_task (location_t loc, c_parser *parser)
13621 tree clauses, block;
13623 clauses = c_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
13624 "#pragma omp task");
13626 block = c_begin_omp_task ();
13627 c_parser_statement (parser);
13628 return c_finish_omp_task (loc, clauses, block);
13631 /* OpenMP 3.0:
13632 # pragma omp taskwait new-line
13635 static void
13636 c_parser_omp_taskwait (c_parser *parser)
13638 location_t loc = c_parser_peek_token (parser)->location;
13639 c_parser_consume_pragma (parser);
13640 c_parser_skip_to_pragma_eol (parser);
13642 c_finish_omp_taskwait (loc);
13645 /* OpenMP 3.1:
13646 # pragma omp taskyield new-line
13649 static void
13650 c_parser_omp_taskyield (c_parser *parser)
13652 location_t loc = c_parser_peek_token (parser)->location;
13653 c_parser_consume_pragma (parser);
13654 c_parser_skip_to_pragma_eol (parser);
13656 c_finish_omp_taskyield (loc);
13659 /* OpenMP 4.0:
13660 # pragma omp taskgroup new-line
13663 static tree
13664 c_parser_omp_taskgroup (c_parser *parser)
13666 location_t loc = c_parser_peek_token (parser)->location;
13667 c_parser_skip_to_pragma_eol (parser);
13668 return c_finish_omp_taskgroup (loc, c_parser_omp_structured_block (parser));
13671 /* OpenMP 4.0:
13672 # pragma omp cancel cancel-clause[optseq] new-line
13674 LOC is the location of the #pragma.
13677 #define OMP_CANCEL_CLAUSE_MASK \
13678 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
13679 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
13680 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
13681 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
13682 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
13684 static void
13685 c_parser_omp_cancel (c_parser *parser)
13687 location_t loc = c_parser_peek_token (parser)->location;
13689 c_parser_consume_pragma (parser);
13690 tree clauses = c_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
13691 "#pragma omp cancel");
13693 c_finish_omp_cancel (loc, clauses);
13696 /* OpenMP 4.0:
13697 # pragma omp cancellation point cancelpt-clause[optseq] new-line
13699 LOC is the location of the #pragma.
13702 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
13703 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
13704 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
13705 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
13706 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
13708 static void
13709 c_parser_omp_cancellation_point (c_parser *parser)
13711 location_t loc = c_parser_peek_token (parser)->location;
13712 tree clauses;
13713 bool point_seen = false;
13715 c_parser_consume_pragma (parser);
13716 if (c_parser_next_token_is (parser, CPP_NAME))
13718 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13719 if (strcmp (p, "point") == 0)
13721 c_parser_consume_token (parser);
13722 point_seen = true;
13725 if (!point_seen)
13727 c_parser_error (parser, "expected %<point%>");
13728 c_parser_skip_to_pragma_eol (parser);
13729 return;
13732 clauses
13733 = c_parser_omp_all_clauses (parser, OMP_CANCELLATION_POINT_CLAUSE_MASK,
13734 "#pragma omp cancellation point");
13736 c_finish_omp_cancellation_point (loc, clauses);
13739 /* OpenMP 4.0:
13740 #pragma omp distribute distribute-clause[optseq] new-line
13741 for-loop */
13743 #define OMP_DISTRIBUTE_CLAUSE_MASK \
13744 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
13745 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
13746 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
13747 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
13749 static tree
13750 c_parser_omp_distribute (location_t loc, c_parser *parser,
13751 char *p_name, omp_clause_mask mask, tree *cclauses)
13753 tree clauses, block, ret;
13755 strcat (p_name, " distribute");
13756 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
13758 if (c_parser_next_token_is (parser, CPP_NAME))
13760 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13761 bool simd = false;
13762 bool parallel = false;
13764 if (strcmp (p, "simd") == 0)
13765 simd = true;
13766 else
13767 parallel = strcmp (p, "parallel") == 0;
13768 if (parallel || simd)
13770 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
13771 if (cclauses == NULL)
13772 cclauses = cclauses_buf;
13773 c_parser_consume_token (parser);
13774 if (!flag_openmp) /* flag_openmp_simd */
13776 if (simd)
13777 return c_parser_omp_simd (loc, parser, p_name, mask, cclauses);
13778 else
13779 return c_parser_omp_parallel (loc, parser, p_name, mask,
13780 cclauses);
13782 block = c_begin_compound_stmt (true);
13783 if (simd)
13784 ret = c_parser_omp_simd (loc, parser, p_name, mask, cclauses);
13785 else
13786 ret = c_parser_omp_parallel (loc, parser, p_name, mask, cclauses);
13787 block = c_end_compound_stmt (loc, block, true);
13788 if (ret == NULL)
13789 return ret;
13790 ret = make_node (OMP_DISTRIBUTE);
13791 TREE_TYPE (ret) = void_type_node;
13792 OMP_FOR_BODY (ret) = block;
13793 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
13794 SET_EXPR_LOCATION (ret, loc);
13795 add_stmt (ret);
13796 return ret;
13799 if (!flag_openmp) /* flag_openmp_simd */
13801 c_parser_skip_to_pragma_eol (parser, false);
13802 return NULL_TREE;
13805 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
13806 if (cclauses)
13808 omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
13809 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
13812 block = c_begin_compound_stmt (true);
13813 ret = c_parser_omp_for_loop (loc, parser, OMP_DISTRIBUTE, clauses, NULL);
13814 block = c_end_compound_stmt (loc, block, true);
13815 add_stmt (block);
13817 return ret;
13820 /* OpenMP 4.0:
13821 # pragma omp teams teams-clause[optseq] new-line
13822 structured-block */
13824 #define OMP_TEAMS_CLAUSE_MASK \
13825 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
13826 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
13827 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
13828 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
13829 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
13830 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
13831 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
13833 static tree
13834 c_parser_omp_teams (location_t loc, c_parser *parser,
13835 char *p_name, omp_clause_mask mask, tree *cclauses)
13837 tree clauses, block, ret;
13839 strcat (p_name, " teams");
13840 mask |= OMP_TEAMS_CLAUSE_MASK;
13842 if (c_parser_next_token_is (parser, CPP_NAME))
13844 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13845 if (strcmp (p, "distribute") == 0)
13847 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
13848 if (cclauses == NULL)
13849 cclauses = cclauses_buf;
13851 c_parser_consume_token (parser);
13852 if (!flag_openmp) /* flag_openmp_simd */
13853 return c_parser_omp_distribute (loc, parser, p_name, mask, cclauses);
13854 block = c_begin_compound_stmt (true);
13855 ret = c_parser_omp_distribute (loc, parser, p_name, mask, cclauses);
13856 block = c_end_compound_stmt (loc, block, true);
13857 if (ret == NULL)
13858 return ret;
13859 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
13860 ret = make_node (OMP_TEAMS);
13861 TREE_TYPE (ret) = void_type_node;
13862 OMP_TEAMS_CLAUSES (ret) = clauses;
13863 OMP_TEAMS_BODY (ret) = block;
13864 OMP_TEAMS_COMBINED (ret) = 1;
13865 return add_stmt (ret);
13868 if (!flag_openmp) /* flag_openmp_simd */
13870 c_parser_skip_to_pragma_eol (parser, false);
13871 return NULL_TREE;
13874 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
13875 if (cclauses)
13877 omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
13878 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
13881 tree stmt = make_node (OMP_TEAMS);
13882 TREE_TYPE (stmt) = void_type_node;
13883 OMP_TEAMS_CLAUSES (stmt) = clauses;
13884 OMP_TEAMS_BODY (stmt) = c_parser_omp_structured_block (parser);
13886 return add_stmt (stmt);
13889 /* OpenMP 4.0:
13890 # pragma omp target data target-data-clause[optseq] new-line
13891 structured-block */
13893 #define OMP_TARGET_DATA_CLAUSE_MASK \
13894 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
13895 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
13896 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
13898 static tree
13899 c_parser_omp_target_data (location_t loc, c_parser *parser)
13901 tree stmt = make_node (OMP_TARGET_DATA);
13902 TREE_TYPE (stmt) = void_type_node;
13904 OMP_TARGET_DATA_CLAUSES (stmt)
13905 = c_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
13906 "#pragma omp target data");
13907 keep_next_level ();
13908 tree block = c_begin_compound_stmt (true);
13909 add_stmt (c_parser_omp_structured_block (parser));
13910 OMP_TARGET_DATA_BODY (stmt) = c_end_compound_stmt (loc, block, true);
13912 SET_EXPR_LOCATION (stmt, loc);
13913 return add_stmt (stmt);
13916 /* OpenMP 4.0:
13917 # pragma omp target update target-update-clause[optseq] new-line */
13919 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
13920 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
13921 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
13922 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
13923 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
13925 static bool
13926 c_parser_omp_target_update (location_t loc, c_parser *parser,
13927 enum pragma_context context)
13929 if (context == pragma_stmt)
13931 error_at (loc,
13932 "%<#pragma omp target update%> may only be "
13933 "used in compound statements");
13934 c_parser_skip_to_pragma_eol (parser);
13935 return false;
13938 tree clauses
13939 = c_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
13940 "#pragma omp target update");
13941 if (find_omp_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
13942 && find_omp_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
13944 error_at (loc,
13945 "%<#pragma omp target update%> must contain at least one "
13946 "%<from%> or %<to%> clauses");
13947 return false;
13950 tree stmt = make_node (OMP_TARGET_UPDATE);
13951 TREE_TYPE (stmt) = void_type_node;
13952 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
13953 SET_EXPR_LOCATION (stmt, loc);
13954 add_stmt (stmt);
13955 return false;
13958 /* OpenMP 4.0:
13959 # pragma omp target target-clause[optseq] new-line
13960 structured-block */
13962 #define OMP_TARGET_CLAUSE_MASK \
13963 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
13964 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
13965 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
13967 static bool
13968 c_parser_omp_target (c_parser *parser, enum pragma_context context)
13970 location_t loc = c_parser_peek_token (parser)->location;
13971 c_parser_consume_pragma (parser);
13973 if (context != pragma_stmt && context != pragma_compound)
13975 c_parser_error (parser, "expected declaration specifiers");
13976 c_parser_skip_to_pragma_eol (parser);
13977 return false;
13980 if (c_parser_next_token_is (parser, CPP_NAME))
13982 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13984 if (strcmp (p, "teams") == 0)
13986 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
13987 char p_name[sizeof ("#pragma omp target teams distribute "
13988 "parallel for simd")];
13990 c_parser_consume_token (parser);
13991 strcpy (p_name, "#pragma omp target");
13992 if (!flag_openmp) /* flag_openmp_simd */
13994 tree stmt = c_parser_omp_teams (loc, parser, p_name,
13995 OMP_TARGET_CLAUSE_MASK,
13996 cclauses);
13997 return stmt != NULL_TREE;
13999 keep_next_level ();
14000 tree block = c_begin_compound_stmt (true);
14001 tree ret = c_parser_omp_teams (loc, parser, p_name,
14002 OMP_TARGET_CLAUSE_MASK, cclauses);
14003 block = c_end_compound_stmt (loc, block, true);
14004 if (ret == NULL_TREE)
14005 return false;
14006 tree stmt = make_node (OMP_TARGET);
14007 TREE_TYPE (stmt) = void_type_node;
14008 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
14009 OMP_TARGET_BODY (stmt) = block;
14010 add_stmt (stmt);
14011 return true;
14013 else if (!flag_openmp) /* flag_openmp_simd */
14015 c_parser_skip_to_pragma_eol (parser, false);
14016 return false;
14018 else if (strcmp (p, "data") == 0)
14020 c_parser_consume_token (parser);
14021 c_parser_omp_target_data (loc, parser);
14022 return true;
14024 else if (strcmp (p, "update") == 0)
14026 c_parser_consume_token (parser);
14027 return c_parser_omp_target_update (loc, parser, context);
14031 tree stmt = make_node (OMP_TARGET);
14032 TREE_TYPE (stmt) = void_type_node;
14034 OMP_TARGET_CLAUSES (stmt)
14035 = c_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
14036 "#pragma omp target");
14037 keep_next_level ();
14038 tree block = c_begin_compound_stmt (true);
14039 add_stmt (c_parser_omp_structured_block (parser));
14040 OMP_TARGET_BODY (stmt) = c_end_compound_stmt (loc, block, true);
14042 SET_EXPR_LOCATION (stmt, loc);
14043 add_stmt (stmt);
14044 return true;
14047 /* OpenMP 4.0:
14048 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
14050 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
14051 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
14052 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
14053 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
14054 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
14055 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
14056 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
14058 static void
14059 c_parser_omp_declare_simd (c_parser *parser, enum pragma_context context)
14061 vec<c_token> clauses = vNULL;
14062 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
14064 c_token *token = c_parser_peek_token (parser);
14065 if (token->type == CPP_EOF)
14067 c_parser_skip_to_pragma_eol (parser);
14068 clauses.release ();
14069 return;
14071 clauses.safe_push (*token);
14072 c_parser_consume_token (parser);
14074 clauses.safe_push (*c_parser_peek_token (parser));
14075 c_parser_skip_to_pragma_eol (parser);
14077 while (c_parser_next_token_is (parser, CPP_PRAGMA))
14079 if (c_parser_peek_token (parser)->pragma_kind
14080 != PRAGMA_OMP_DECLARE_REDUCTION
14081 || c_parser_peek_2nd_token (parser)->type != CPP_NAME
14082 || strcmp (IDENTIFIER_POINTER
14083 (c_parser_peek_2nd_token (parser)->value),
14084 "simd") != 0)
14086 c_parser_error (parser,
14087 "%<#pragma omp declare simd%> must be followed by "
14088 "function declaration or definition or another "
14089 "%<#pragma omp declare simd%>");
14090 clauses.release ();
14091 return;
14093 c_parser_consume_pragma (parser);
14094 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
14096 c_token *token = c_parser_peek_token (parser);
14097 if (token->type == CPP_EOF)
14099 c_parser_skip_to_pragma_eol (parser);
14100 clauses.release ();
14101 return;
14103 clauses.safe_push (*token);
14104 c_parser_consume_token (parser);
14106 clauses.safe_push (*c_parser_peek_token (parser));
14107 c_parser_skip_to_pragma_eol (parser);
14110 /* Make sure nothing tries to read past the end of the tokens. */
14111 c_token eof_token;
14112 memset (&eof_token, 0, sizeof (eof_token));
14113 eof_token.type = CPP_EOF;
14114 clauses.safe_push (eof_token);
14115 clauses.safe_push (eof_token);
14117 switch (context)
14119 case pragma_external:
14120 if (c_parser_next_token_is (parser, CPP_KEYWORD)
14121 && c_parser_peek_token (parser)->keyword == RID_EXTENSION)
14123 int ext = disable_extension_diagnostics ();
14125 c_parser_consume_token (parser);
14126 while (c_parser_next_token_is (parser, CPP_KEYWORD)
14127 && c_parser_peek_token (parser)->keyword == RID_EXTENSION);
14128 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
14129 NULL, clauses);
14130 restore_extension_diagnostics (ext);
14132 else
14133 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
14134 NULL, clauses);
14135 break;
14136 case pragma_struct:
14137 case pragma_param:
14138 c_parser_error (parser, "%<#pragma omp declare simd%> must be followed by "
14139 "function declaration or definition");
14140 break;
14141 case pragma_compound:
14142 case pragma_stmt:
14143 if (c_parser_next_token_is (parser, CPP_KEYWORD)
14144 && c_parser_peek_token (parser)->keyword == RID_EXTENSION)
14146 int ext = disable_extension_diagnostics ();
14148 c_parser_consume_token (parser);
14149 while (c_parser_next_token_is (parser, CPP_KEYWORD)
14150 && c_parser_peek_token (parser)->keyword == RID_EXTENSION);
14151 if (c_parser_next_tokens_start_declaration (parser))
14153 c_parser_declaration_or_fndef (parser, true, true, true, true,
14154 true, NULL, clauses);
14155 restore_extension_diagnostics (ext);
14156 break;
14158 restore_extension_diagnostics (ext);
14160 else if (c_parser_next_tokens_start_declaration (parser))
14162 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
14163 NULL, clauses);
14164 break;
14166 c_parser_error (parser, "%<#pragma omp declare simd%> must be followed by "
14167 "function declaration or definition");
14168 break;
14169 default:
14170 gcc_unreachable ();
14172 clauses.release ();
14175 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
14176 and put that into "omp declare simd" attribute. */
14178 static void
14179 c_finish_omp_declare_simd (c_parser *parser, tree fndecl, tree parms,
14180 vec<c_token> clauses)
14182 if (flag_cilkplus
14183 && clauses.exists () && !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
14185 error ("%<#pragma omp declare simd%> cannot be used in the same "
14186 "function marked as a Cilk Plus SIMD-enabled function");
14187 vec_free (parser->cilk_simd_fn_tokens);
14188 return;
14191 /* Normally first token is CPP_NAME "simd". CPP_EOF there indicates
14192 error has been reported and CPP_PRAGMA that c_finish_omp_declare_simd
14193 has already processed the tokens. */
14194 if (clauses.exists () && clauses[0].type == CPP_EOF)
14195 return;
14196 if (fndecl == NULL_TREE || TREE_CODE (fndecl) != FUNCTION_DECL)
14198 error ("%<#pragma omp declare simd%> not immediately followed by "
14199 "a function declaration or definition");
14200 clauses[0].type = CPP_EOF;
14201 return;
14203 if (clauses.exists () && clauses[0].type != CPP_NAME)
14205 error_at (DECL_SOURCE_LOCATION (fndecl),
14206 "%<#pragma omp declare simd%> not immediately followed by "
14207 "a single function declaration or definition");
14208 clauses[0].type = CPP_EOF;
14209 return;
14212 if (parms == NULL_TREE)
14213 parms = DECL_ARGUMENTS (fndecl);
14215 unsigned int tokens_avail = parser->tokens_avail;
14216 gcc_assert (parser->tokens == &parser->tokens_buf[0]);
14217 bool is_cilkplus_cilk_simd_fn = false;
14219 if (flag_cilkplus && !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
14221 parser->tokens = parser->cilk_simd_fn_tokens->address ();
14222 parser->tokens_avail = vec_safe_length (parser->cilk_simd_fn_tokens);
14223 is_cilkplus_cilk_simd_fn = true;
14225 else
14227 parser->tokens = clauses.address ();
14228 parser->tokens_avail = clauses.length ();
14231 /* c_parser_omp_declare_simd pushed 2 extra CPP_EOF tokens at the end. */
14232 while (parser->tokens_avail > 3)
14234 c_token *token = c_parser_peek_token (parser);
14235 if (!is_cilkplus_cilk_simd_fn)
14236 gcc_assert (token->type == CPP_NAME
14237 && strcmp (IDENTIFIER_POINTER (token->value), "simd") == 0);
14238 else
14239 gcc_assert (token->type == CPP_NAME
14240 && is_cilkplus_vector_p (token->value));
14241 c_parser_consume_token (parser);
14242 parser->in_pragma = true;
14244 tree c = NULL_TREE;
14245 if (is_cilkplus_cilk_simd_fn)
14246 c = c_parser_omp_all_clauses (parser, CILK_SIMD_FN_CLAUSE_MASK,
14247 "SIMD-enabled functions attribute");
14248 else
14249 c = c_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
14250 "#pragma omp declare simd");
14251 c = c_omp_declare_simd_clauses_to_numbers (parms, c);
14252 if (c != NULL_TREE)
14253 c = tree_cons (NULL_TREE, c, NULL_TREE);
14254 if (is_cilkplus_cilk_simd_fn)
14256 tree k = build_tree_list (get_identifier ("cilk simd function"),
14257 NULL_TREE);
14258 TREE_CHAIN (k) = DECL_ATTRIBUTES (fndecl);
14259 DECL_ATTRIBUTES (fndecl) = k;
14261 c = build_tree_list (get_identifier ("omp declare simd"), c);
14262 TREE_CHAIN (c) = DECL_ATTRIBUTES (fndecl);
14263 DECL_ATTRIBUTES (fndecl) = c;
14266 parser->tokens = &parser->tokens_buf[0];
14267 parser->tokens_avail = tokens_avail;
14268 if (clauses.exists ())
14269 clauses[0].type = CPP_PRAGMA;
14271 if (!vec_safe_is_empty (parser->cilk_simd_fn_tokens))
14272 vec_free (parser->cilk_simd_fn_tokens);
14276 /* OpenMP 4.0:
14277 # pragma omp declare target new-line
14278 declarations and definitions
14279 # pragma omp end declare target new-line */
14281 static void
14282 c_parser_omp_declare_target (c_parser *parser)
14284 c_parser_skip_to_pragma_eol (parser);
14285 current_omp_declare_target_attribute++;
14288 static void
14289 c_parser_omp_end_declare_target (c_parser *parser)
14291 location_t loc = c_parser_peek_token (parser)->location;
14292 c_parser_consume_pragma (parser);
14293 if (c_parser_next_token_is (parser, CPP_NAME)
14294 && strcmp (IDENTIFIER_POINTER (c_parser_peek_token (parser)->value),
14295 "declare") == 0)
14297 c_parser_consume_token (parser);
14298 if (c_parser_next_token_is (parser, CPP_NAME)
14299 && strcmp (IDENTIFIER_POINTER (c_parser_peek_token (parser)->value),
14300 "target") == 0)
14301 c_parser_consume_token (parser);
14302 else
14304 c_parser_error (parser, "expected %<target%>");
14305 c_parser_skip_to_pragma_eol (parser);
14306 return;
14309 else
14311 c_parser_error (parser, "expected %<declare%>");
14312 c_parser_skip_to_pragma_eol (parser);
14313 return;
14315 c_parser_skip_to_pragma_eol (parser);
14316 if (!current_omp_declare_target_attribute)
14317 error_at (loc, "%<#pragma omp end declare target%> without corresponding "
14318 "%<#pragma omp declare target%>");
14319 else
14320 current_omp_declare_target_attribute--;
14324 /* OpenMP 4.0
14325 #pragma omp declare reduction (reduction-id : typename-list : expression) \
14326 initializer-clause[opt] new-line
14328 initializer-clause:
14329 initializer (omp_priv = initializer)
14330 initializer (function-name (argument-list)) */
14332 static void
14333 c_parser_omp_declare_reduction (c_parser *parser, enum pragma_context context)
14335 unsigned int tokens_avail = 0, i;
14336 vec<tree> types = vNULL;
14337 vec<c_token> clauses = vNULL;
14338 enum tree_code reduc_code = ERROR_MARK;
14339 tree reduc_id = NULL_TREE;
14340 tree type;
14341 location_t rloc = c_parser_peek_token (parser)->location;
14343 if (context == pragma_struct || context == pragma_param)
14345 error ("%<#pragma omp declare reduction%> not at file or block scope");
14346 goto fail;
14349 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
14350 goto fail;
14352 switch (c_parser_peek_token (parser)->type)
14354 case CPP_PLUS:
14355 reduc_code = PLUS_EXPR;
14356 break;
14357 case CPP_MULT:
14358 reduc_code = MULT_EXPR;
14359 break;
14360 case CPP_MINUS:
14361 reduc_code = MINUS_EXPR;
14362 break;
14363 case CPP_AND:
14364 reduc_code = BIT_AND_EXPR;
14365 break;
14366 case CPP_XOR:
14367 reduc_code = BIT_XOR_EXPR;
14368 break;
14369 case CPP_OR:
14370 reduc_code = BIT_IOR_EXPR;
14371 break;
14372 case CPP_AND_AND:
14373 reduc_code = TRUTH_ANDIF_EXPR;
14374 break;
14375 case CPP_OR_OR:
14376 reduc_code = TRUTH_ORIF_EXPR;
14377 break;
14378 case CPP_NAME:
14379 const char *p;
14380 p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
14381 if (strcmp (p, "min") == 0)
14383 reduc_code = MIN_EXPR;
14384 break;
14386 if (strcmp (p, "max") == 0)
14388 reduc_code = MAX_EXPR;
14389 break;
14391 reduc_id = c_parser_peek_token (parser)->value;
14392 break;
14393 default:
14394 c_parser_error (parser,
14395 "expected %<+%>, %<*%>, %<-%>, %<&%>, "
14396 "%<^%>, %<|%>, %<&&%>, %<||%>, %<min%> or identifier");
14397 goto fail;
14400 tree orig_reduc_id, reduc_decl;
14401 orig_reduc_id = reduc_id;
14402 reduc_id = c_omp_reduction_id (reduc_code, reduc_id);
14403 reduc_decl = c_omp_reduction_decl (reduc_id);
14404 c_parser_consume_token (parser);
14406 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
14407 goto fail;
14409 while (true)
14411 location_t loc = c_parser_peek_token (parser)->location;
14412 struct c_type_name *ctype = c_parser_type_name (parser);
14413 if (ctype != NULL)
14415 type = groktypename (ctype, NULL, NULL);
14416 if (type == error_mark_node)
14418 else if ((INTEGRAL_TYPE_P (type)
14419 || TREE_CODE (type) == REAL_TYPE
14420 || TREE_CODE (type) == COMPLEX_TYPE)
14421 && orig_reduc_id == NULL_TREE)
14422 error_at (loc, "predeclared arithmetic type in "
14423 "%<#pragma omp declare reduction%>");
14424 else if (TREE_CODE (type) == FUNCTION_TYPE
14425 || TREE_CODE (type) == ARRAY_TYPE)
14426 error_at (loc, "function or array type in "
14427 "%<#pragma omp declare reduction%>");
14428 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
14429 error_at (loc, "const, volatile or restrict qualified type in "
14430 "%<#pragma omp declare reduction%>");
14431 else
14433 tree t;
14434 for (t = DECL_INITIAL (reduc_decl); t; t = TREE_CHAIN (t))
14435 if (comptypes (TREE_PURPOSE (t), type))
14437 error_at (loc, "redeclaration of %qs "
14438 "%<#pragma omp declare reduction%> for "
14439 "type %qT",
14440 IDENTIFIER_POINTER (reduc_id)
14441 + sizeof ("omp declare reduction ") - 1,
14442 type);
14443 location_t ploc
14444 = DECL_SOURCE_LOCATION (TREE_VEC_ELT (TREE_VALUE (t),
14445 0));
14446 error_at (ploc, "previous %<#pragma omp declare "
14447 "reduction%>");
14448 break;
14450 if (t == NULL_TREE)
14451 types.safe_push (type);
14453 if (c_parser_next_token_is (parser, CPP_COMMA))
14454 c_parser_consume_token (parser);
14455 else
14456 break;
14458 else
14459 break;
14462 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>")
14463 || types.is_empty ())
14465 fail:
14466 clauses.release ();
14467 types.release ();
14468 while (true)
14470 c_token *token = c_parser_peek_token (parser);
14471 if (token->type == CPP_EOF || token->type == CPP_PRAGMA_EOL)
14472 break;
14473 c_parser_consume_token (parser);
14475 c_parser_skip_to_pragma_eol (parser);
14476 return;
14479 if (types.length () > 1)
14481 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
14483 c_token *token = c_parser_peek_token (parser);
14484 if (token->type == CPP_EOF)
14485 goto fail;
14486 clauses.safe_push (*token);
14487 c_parser_consume_token (parser);
14489 clauses.safe_push (*c_parser_peek_token (parser));
14490 c_parser_skip_to_pragma_eol (parser);
14492 /* Make sure nothing tries to read past the end of the tokens. */
14493 c_token eof_token;
14494 memset (&eof_token, 0, sizeof (eof_token));
14495 eof_token.type = CPP_EOF;
14496 clauses.safe_push (eof_token);
14497 clauses.safe_push (eof_token);
14500 int errs = errorcount;
14501 FOR_EACH_VEC_ELT (types, i, type)
14503 tokens_avail = parser->tokens_avail;
14504 gcc_assert (parser->tokens == &parser->tokens_buf[0]);
14505 if (!clauses.is_empty ())
14507 parser->tokens = clauses.address ();
14508 parser->tokens_avail = clauses.length ();
14509 parser->in_pragma = true;
14512 bool nested = current_function_decl != NULL_TREE;
14513 if (nested)
14514 c_push_function_context ();
14515 tree fndecl = build_decl (BUILTINS_LOCATION, FUNCTION_DECL,
14516 reduc_id, default_function_type);
14517 current_function_decl = fndecl;
14518 allocate_struct_function (fndecl, true);
14519 push_scope ();
14520 tree stmt = push_stmt_list ();
14521 /* Intentionally BUILTINS_LOCATION, so that -Wshadow doesn't
14522 warn about these. */
14523 tree omp_out = build_decl (BUILTINS_LOCATION, VAR_DECL,
14524 get_identifier ("omp_out"), type);
14525 DECL_ARTIFICIAL (omp_out) = 1;
14526 DECL_CONTEXT (omp_out) = fndecl;
14527 pushdecl (omp_out);
14528 tree omp_in = build_decl (BUILTINS_LOCATION, VAR_DECL,
14529 get_identifier ("omp_in"), type);
14530 DECL_ARTIFICIAL (omp_in) = 1;
14531 DECL_CONTEXT (omp_in) = fndecl;
14532 pushdecl (omp_in);
14533 struct c_expr combiner = c_parser_expression (parser);
14534 struct c_expr initializer;
14535 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE;
14536 bool bad = false;
14537 initializer.value = error_mark_node;
14538 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
14539 bad = true;
14540 else if (c_parser_next_token_is (parser, CPP_NAME)
14541 && strcmp (IDENTIFIER_POINTER
14542 (c_parser_peek_token (parser)->value),
14543 "initializer") == 0)
14545 c_parser_consume_token (parser);
14546 pop_scope ();
14547 push_scope ();
14548 omp_priv = build_decl (BUILTINS_LOCATION, VAR_DECL,
14549 get_identifier ("omp_priv"), type);
14550 DECL_ARTIFICIAL (omp_priv) = 1;
14551 DECL_INITIAL (omp_priv) = error_mark_node;
14552 DECL_CONTEXT (omp_priv) = fndecl;
14553 pushdecl (omp_priv);
14554 omp_orig = build_decl (BUILTINS_LOCATION, VAR_DECL,
14555 get_identifier ("omp_orig"), type);
14556 DECL_ARTIFICIAL (omp_orig) = 1;
14557 DECL_CONTEXT (omp_orig) = fndecl;
14558 pushdecl (omp_orig);
14559 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
14560 bad = true;
14561 else if (!c_parser_next_token_is (parser, CPP_NAME))
14563 c_parser_error (parser, "expected %<omp_priv%> or "
14564 "function-name");
14565 bad = true;
14567 else if (strcmp (IDENTIFIER_POINTER
14568 (c_parser_peek_token (parser)->value),
14569 "omp_priv") != 0)
14571 if (c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN
14572 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
14574 c_parser_error (parser, "expected function-name %<(%>");
14575 bad = true;
14577 else
14578 initializer = c_parser_postfix_expression (parser);
14579 if (initializer.value
14580 && TREE_CODE (initializer.value) == CALL_EXPR)
14582 int j;
14583 tree c = initializer.value;
14584 for (j = 0; j < call_expr_nargs (c); j++)
14585 if (TREE_CODE (CALL_EXPR_ARG (c, j)) == ADDR_EXPR
14586 && TREE_OPERAND (CALL_EXPR_ARG (c, j), 0) == omp_priv)
14587 break;
14588 if (j == call_expr_nargs (c))
14589 error ("one of the initializer call arguments should be "
14590 "%<&omp_priv%>");
14593 else
14595 c_parser_consume_token (parser);
14596 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
14597 bad = true;
14598 else
14600 tree st = push_stmt_list ();
14601 start_init (omp_priv, NULL_TREE, 0);
14602 location_t loc = c_parser_peek_token (parser)->location;
14603 struct c_expr init = c_parser_initializer (parser);
14604 finish_init ();
14605 finish_decl (omp_priv, loc, init.value,
14606 init.original_type, NULL_TREE);
14607 pop_stmt_list (st);
14610 if (!bad
14611 && !c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
14612 bad = true;
14615 if (!bad)
14617 c_parser_skip_to_pragma_eol (parser);
14619 tree t = tree_cons (type, make_tree_vec (omp_priv ? 6 : 3),
14620 DECL_INITIAL (reduc_decl));
14621 DECL_INITIAL (reduc_decl) = t;
14622 DECL_SOURCE_LOCATION (omp_out) = rloc;
14623 TREE_VEC_ELT (TREE_VALUE (t), 0) = omp_out;
14624 TREE_VEC_ELT (TREE_VALUE (t), 1) = omp_in;
14625 TREE_VEC_ELT (TREE_VALUE (t), 2) = combiner.value;
14626 walk_tree (&combiner.value, c_check_omp_declare_reduction_r,
14627 &TREE_VEC_ELT (TREE_VALUE (t), 0), NULL);
14628 if (omp_priv)
14630 DECL_SOURCE_LOCATION (omp_priv) = rloc;
14631 TREE_VEC_ELT (TREE_VALUE (t), 3) = omp_priv;
14632 TREE_VEC_ELT (TREE_VALUE (t), 4) = omp_orig;
14633 TREE_VEC_ELT (TREE_VALUE (t), 5) = initializer.value;
14634 walk_tree (&initializer.value, c_check_omp_declare_reduction_r,
14635 &TREE_VEC_ELT (TREE_VALUE (t), 3), NULL);
14636 walk_tree (&DECL_INITIAL (omp_priv),
14637 c_check_omp_declare_reduction_r,
14638 &TREE_VEC_ELT (TREE_VALUE (t), 3), NULL);
14642 pop_stmt_list (stmt);
14643 pop_scope ();
14644 if (cfun->language != NULL)
14646 ggc_free (cfun->language);
14647 cfun->language = NULL;
14649 set_cfun (NULL);
14650 current_function_decl = NULL_TREE;
14651 if (nested)
14652 c_pop_function_context ();
14654 if (!clauses.is_empty ())
14656 parser->tokens = &parser->tokens_buf[0];
14657 parser->tokens_avail = tokens_avail;
14659 if (bad)
14660 goto fail;
14661 if (errs != errorcount)
14662 break;
14665 clauses.release ();
14666 types.release ();
14670 /* OpenMP 4.0
14671 #pragma omp declare simd declare-simd-clauses[optseq] new-line
14672 #pragma omp declare reduction (reduction-id : typename-list : expression) \
14673 initializer-clause[opt] new-line
14674 #pragma omp declare target new-line */
14676 static void
14677 c_parser_omp_declare (c_parser *parser, enum pragma_context context)
14679 c_parser_consume_pragma (parser);
14680 if (c_parser_next_token_is (parser, CPP_NAME))
14682 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
14683 if (strcmp (p, "simd") == 0)
14685 /* c_parser_consume_token (parser); done in
14686 c_parser_omp_declare_simd. */
14687 c_parser_omp_declare_simd (parser, context);
14688 return;
14690 if (strcmp (p, "reduction") == 0)
14692 c_parser_consume_token (parser);
14693 c_parser_omp_declare_reduction (parser, context);
14694 return;
14696 if (!flag_openmp) /* flag_openmp_simd */
14698 c_parser_skip_to_pragma_eol (parser, false);
14699 return;
14701 if (strcmp (p, "target") == 0)
14703 c_parser_consume_token (parser);
14704 c_parser_omp_declare_target (parser);
14705 return;
14709 c_parser_error (parser, "expected %<simd%> or %<reduction%> "
14710 "or %<target%>");
14711 c_parser_skip_to_pragma_eol (parser);
14714 /* Main entry point to parsing most OpenMP pragmas. */
14716 static void
14717 c_parser_omp_construct (c_parser *parser)
14719 enum pragma_kind p_kind;
14720 location_t loc;
14721 tree stmt;
14722 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
14723 omp_clause_mask mask (0);
14725 loc = c_parser_peek_token (parser)->location;
14726 p_kind = c_parser_peek_token (parser)->pragma_kind;
14727 c_parser_consume_pragma (parser);
14729 switch (p_kind)
14731 case PRAGMA_OACC_CACHE:
14732 strcpy (p_name, "#pragma acc");
14733 stmt = c_parser_oacc_cache (loc, parser);
14734 break;
14735 case PRAGMA_OACC_DATA:
14736 stmt = c_parser_oacc_data (loc, parser);
14737 break;
14738 case PRAGMA_OACC_KERNELS:
14739 strcpy (p_name, "#pragma acc");
14740 stmt = c_parser_oacc_kernels (loc, parser, p_name);
14741 break;
14742 case PRAGMA_OACC_LOOP:
14743 strcpy (p_name, "#pragma acc");
14744 stmt = c_parser_oacc_loop (loc, parser, p_name);
14745 break;
14746 case PRAGMA_OACC_PARALLEL:
14747 strcpy (p_name, "#pragma acc");
14748 stmt = c_parser_oacc_parallel (loc, parser, p_name);
14749 break;
14750 case PRAGMA_OACC_WAIT:
14751 strcpy (p_name, "#pragma wait");
14752 stmt = c_parser_oacc_wait (loc, parser, p_name);
14753 break;
14754 case PRAGMA_OMP_ATOMIC:
14755 c_parser_omp_atomic (loc, parser);
14756 return;
14757 case PRAGMA_OMP_CRITICAL:
14758 stmt = c_parser_omp_critical (loc, parser);
14759 break;
14760 case PRAGMA_OMP_DISTRIBUTE:
14761 strcpy (p_name, "#pragma omp");
14762 stmt = c_parser_omp_distribute (loc, parser, p_name, mask, NULL);
14763 break;
14764 case PRAGMA_OMP_FOR:
14765 strcpy (p_name, "#pragma omp");
14766 stmt = c_parser_omp_for (loc, parser, p_name, mask, NULL);
14767 break;
14768 case PRAGMA_OMP_MASTER:
14769 stmt = c_parser_omp_master (loc, parser);
14770 break;
14771 case PRAGMA_OMP_ORDERED:
14772 stmt = c_parser_omp_ordered (loc, parser);
14773 break;
14774 case PRAGMA_OMP_PARALLEL:
14775 strcpy (p_name, "#pragma omp");
14776 stmt = c_parser_omp_parallel (loc, parser, p_name, mask, NULL);
14777 break;
14778 case PRAGMA_OMP_SECTIONS:
14779 strcpy (p_name, "#pragma omp");
14780 stmt = c_parser_omp_sections (loc, parser, p_name, mask, NULL);
14781 break;
14782 case PRAGMA_OMP_SIMD:
14783 strcpy (p_name, "#pragma omp");
14784 stmt = c_parser_omp_simd (loc, parser, p_name, mask, NULL);
14785 break;
14786 case PRAGMA_OMP_SINGLE:
14787 stmt = c_parser_omp_single (loc, parser);
14788 break;
14789 case PRAGMA_OMP_TASK:
14790 stmt = c_parser_omp_task (loc, parser);
14791 break;
14792 case PRAGMA_OMP_TASKGROUP:
14793 stmt = c_parser_omp_taskgroup (parser);
14794 break;
14795 case PRAGMA_OMP_TEAMS:
14796 strcpy (p_name, "#pragma omp");
14797 stmt = c_parser_omp_teams (loc, parser, p_name, mask, NULL);
14798 break;
14799 default:
14800 gcc_unreachable ();
14803 if (stmt)
14804 gcc_assert (EXPR_LOCATION (stmt) != UNKNOWN_LOCATION);
14808 /* OpenMP 2.5:
14809 # pragma omp threadprivate (variable-list) */
14811 static void
14812 c_parser_omp_threadprivate (c_parser *parser)
14814 tree vars, t;
14815 location_t loc;
14817 c_parser_consume_pragma (parser);
14818 loc = c_parser_peek_token (parser)->location;
14819 vars = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
14821 /* Mark every variable in VARS to be assigned thread local storage. */
14822 for (t = vars; t; t = TREE_CHAIN (t))
14824 tree v = TREE_PURPOSE (t);
14826 /* FIXME diagnostics: Ideally we should keep individual
14827 locations for all the variables in the var list to make the
14828 following errors more precise. Perhaps
14829 c_parser_omp_var_list_parens() should construct a list of
14830 locations to go along with the var list. */
14832 /* If V had already been marked threadprivate, it doesn't matter
14833 whether it had been used prior to this point. */
14834 if (!VAR_P (v))
14835 error_at (loc, "%qD is not a variable", v);
14836 else if (TREE_USED (v) && !C_DECL_THREADPRIVATE_P (v))
14837 error_at (loc, "%qE declared %<threadprivate%> after first use", v);
14838 else if (! is_global_var (v))
14839 error_at (loc, "automatic variable %qE cannot be %<threadprivate%>", v);
14840 else if (TREE_TYPE (v) == error_mark_node)
14842 else if (! COMPLETE_TYPE_P (TREE_TYPE (v)))
14843 error_at (loc, "%<threadprivate%> %qE has incomplete type", v);
14844 else
14846 if (! DECL_THREAD_LOCAL_P (v))
14848 set_decl_tls_model (v, decl_default_tls_model (v));
14849 /* If rtl has been already set for this var, call
14850 make_decl_rtl once again, so that encode_section_info
14851 has a chance to look at the new decl flags. */
14852 if (DECL_RTL_SET_P (v))
14853 make_decl_rtl (v);
14855 C_DECL_THREADPRIVATE_P (v) = 1;
14859 c_parser_skip_to_pragma_eol (parser);
14862 /* Cilk Plus <#pragma simd> parsing routines. */
14864 /* Helper function for c_parser_pragma. Perform some sanity checking
14865 for <#pragma simd> constructs. Returns FALSE if there was a
14866 problem. */
14868 static bool
14869 c_parser_cilk_verify_simd (c_parser *parser,
14870 enum pragma_context context)
14872 if (!flag_cilkplus)
14874 warning (0, "pragma simd ignored because -fcilkplus is not enabled");
14875 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
14876 return false;
14878 if (context == pragma_external)
14880 c_parser_error (parser,"pragma simd must be inside a function");
14881 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
14882 return false;
14884 return true;
14887 /* Cilk Plus:
14888 This function is shared by SIMD-enabled functions and #pragma simd.
14889 If IS_SIMD_FN is true then it is parsing a SIMD-enabled function and
14890 CLAUSES is unused. The main purpose of this function is to parse a
14891 vectorlength attribute or clause and check for parse errors.
14892 When IS_SIMD_FN is true then the function is merely caching the tokens
14893 in PARSER->CILK_SIMD_FN_TOKENS. If errors are found then the token
14894 cache is cleared since there is no reason to continue.
14895 Syntax:
14896 vectorlength ( constant-expression ) */
14898 static tree
14899 c_parser_cilk_clause_vectorlength (c_parser *parser, tree clauses,
14900 bool is_simd_fn)
14902 if (is_simd_fn)
14903 check_no_duplicate_clause (clauses, OMP_CLAUSE_SIMDLEN, "vectorlength");
14904 else
14905 /* The vectorlength clause behaves exactly like OpenMP's safelen
14906 clause. Represent it in OpenMP terms. */
14907 check_no_duplicate_clause (clauses, OMP_CLAUSE_SAFELEN, "vectorlength");
14909 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
14910 return clauses;
14912 location_t loc = c_parser_peek_token (parser)->location;
14913 tree expr = c_parser_expr_no_commas (parser, NULL).value;
14914 expr = c_fully_fold (expr, false, NULL);
14916 /* If expr is an error_mark_node then the above function would have
14917 emitted an error. No reason to do it twice. */
14918 if (expr == error_mark_node)
14920 else if (!TREE_TYPE (expr)
14921 || !TREE_CONSTANT (expr)
14922 || !INTEGRAL_TYPE_P (TREE_TYPE (expr)))
14924 error_at (loc, "vectorlength must be an integer constant");
14925 else if (wi::exact_log2 (expr) == -1)
14926 error_at (loc, "vectorlength must be a power of 2");
14927 else
14929 if (is_simd_fn)
14931 tree u = build_omp_clause (loc, OMP_CLAUSE_SIMDLEN);
14932 OMP_CLAUSE_SIMDLEN_EXPR (u) = expr;
14933 OMP_CLAUSE_CHAIN (u) = clauses;
14934 clauses = u;
14936 else
14938 tree u = build_omp_clause (loc, OMP_CLAUSE_SAFELEN);
14939 OMP_CLAUSE_SAFELEN_EXPR (u) = expr;
14940 OMP_CLAUSE_CHAIN (u) = clauses;
14941 clauses = u;
14945 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
14947 return clauses;
14950 /* Cilk Plus:
14951 linear ( simd-linear-variable-list )
14953 simd-linear-variable-list:
14954 simd-linear-variable
14955 simd-linear-variable-list , simd-linear-variable
14957 simd-linear-variable:
14958 id-expression
14959 id-expression : simd-linear-step
14961 simd-linear-step:
14962 conditional-expression */
14964 static tree
14965 c_parser_cilk_clause_linear (c_parser *parser, tree clauses)
14967 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
14968 return clauses;
14970 location_t loc = c_parser_peek_token (parser)->location;
14972 if (c_parser_next_token_is_not (parser, CPP_NAME)
14973 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
14974 c_parser_error (parser, "expected identifier");
14976 while (c_parser_next_token_is (parser, CPP_NAME)
14977 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
14979 tree var = lookup_name (c_parser_peek_token (parser)->value);
14981 if (var == NULL)
14983 undeclared_variable (c_parser_peek_token (parser)->location,
14984 c_parser_peek_token (parser)->value);
14985 c_parser_consume_token (parser);
14987 else if (var == error_mark_node)
14988 c_parser_consume_token (parser);
14989 else
14991 tree step = integer_one_node;
14993 /* Parse the linear step if present. */
14994 if (c_parser_peek_2nd_token (parser)->type == CPP_COLON)
14996 c_parser_consume_token (parser);
14997 c_parser_consume_token (parser);
14999 tree expr = c_parser_expr_no_commas (parser, NULL).value;
15000 expr = c_fully_fold (expr, false, NULL);
15002 if (TREE_TYPE (expr)
15003 && INTEGRAL_TYPE_P (TREE_TYPE (expr))
15004 && (TREE_CONSTANT (expr)
15005 || DECL_P (expr)))
15006 step = expr;
15007 else
15008 c_parser_error (parser,
15009 "step size must be an integer constant "
15010 "expression or an integer variable");
15012 else
15013 c_parser_consume_token (parser);
15015 /* Use OMP_CLAUSE_LINEAR, which has the same semantics. */
15016 tree u = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
15017 OMP_CLAUSE_DECL (u) = var;
15018 OMP_CLAUSE_LINEAR_STEP (u) = step;
15019 OMP_CLAUSE_CHAIN (u) = clauses;
15020 clauses = u;
15023 if (c_parser_next_token_is_not (parser, CPP_COMMA))
15024 break;
15026 c_parser_consume_token (parser);
15029 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
15031 return clauses;
15034 /* Returns the name of the next clause. If the clause is not
15035 recognized SIMD_OMP_CLAUSE_NONE is returned and the next token is
15036 not consumed. Otherwise, the appropriate pragma_simd_clause is
15037 returned and the token is consumed. */
15039 static pragma_omp_clause
15040 c_parser_cilk_clause_name (c_parser *parser)
15042 pragma_omp_clause result;
15043 c_token *token = c_parser_peek_token (parser);
15045 if (!token->value || token->type != CPP_NAME)
15046 return PRAGMA_CILK_CLAUSE_NONE;
15048 const char *p = IDENTIFIER_POINTER (token->value);
15050 if (!strcmp (p, "vectorlength"))
15051 result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
15052 else if (!strcmp (p, "linear"))
15053 result = PRAGMA_CILK_CLAUSE_LINEAR;
15054 else if (!strcmp (p, "private"))
15055 result = PRAGMA_CILK_CLAUSE_PRIVATE;
15056 else if (!strcmp (p, "firstprivate"))
15057 result = PRAGMA_CILK_CLAUSE_FIRSTPRIVATE;
15058 else if (!strcmp (p, "lastprivate"))
15059 result = PRAGMA_CILK_CLAUSE_LASTPRIVATE;
15060 else if (!strcmp (p, "reduction"))
15061 result = PRAGMA_CILK_CLAUSE_REDUCTION;
15062 else
15063 return PRAGMA_CILK_CLAUSE_NONE;
15065 c_parser_consume_token (parser);
15066 return result;
15069 /* Parse all #<pragma simd> clauses. Return the list of clauses
15070 found. */
15072 static tree
15073 c_parser_cilk_all_clauses (c_parser *parser)
15075 tree clauses = NULL;
15077 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
15079 pragma_omp_clause c_kind;
15081 c_kind = c_parser_cilk_clause_name (parser);
15083 switch (c_kind)
15085 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
15086 clauses = c_parser_cilk_clause_vectorlength (parser, clauses, false);
15087 break;
15088 case PRAGMA_CILK_CLAUSE_LINEAR:
15089 clauses = c_parser_cilk_clause_linear (parser, clauses);
15090 break;
15091 case PRAGMA_CILK_CLAUSE_PRIVATE:
15092 /* Use the OpenMP counterpart. */
15093 clauses = c_parser_omp_clause_private (parser, clauses);
15094 break;
15095 case PRAGMA_CILK_CLAUSE_FIRSTPRIVATE:
15096 /* Use the OpenMP counterpart. */
15097 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
15098 break;
15099 case PRAGMA_CILK_CLAUSE_LASTPRIVATE:
15100 /* Use the OpenMP counterpart. */
15101 clauses = c_parser_omp_clause_lastprivate (parser, clauses);
15102 break;
15103 case PRAGMA_CILK_CLAUSE_REDUCTION:
15104 /* Use the OpenMP counterpart. */
15105 clauses = c_parser_omp_clause_reduction (parser, clauses);
15106 break;
15107 default:
15108 c_parser_error (parser, "expected %<#pragma simd%> clause");
15109 goto saw_error;
15113 saw_error:
15114 c_parser_skip_to_pragma_eol (parser);
15115 return c_finish_cilk_clauses (clauses);
15118 /* This function helps parse the grainsize pragma for a _Cilk_for statement.
15119 Here is the correct syntax of this pragma:
15120 #pragma cilk grainsize = <EXP>
15123 static void
15124 c_parser_cilk_grainsize (c_parser *parser)
15126 extern tree convert_to_integer (tree, tree);
15128 /* consume the 'grainsize' keyword. */
15129 c_parser_consume_pragma (parser);
15131 if (c_parser_require (parser, CPP_EQ, "expected %<=%>") != 0)
15133 struct c_expr g_expr = c_parser_binary_expression (parser, NULL, NULL);
15134 if (g_expr.value == error_mark_node)
15136 c_parser_skip_to_pragma_eol (parser);
15137 return;
15139 tree grain = convert_to_integer (long_integer_type_node,
15140 c_fully_fold (g_expr.value, false,
15141 NULL));
15142 c_parser_skip_to_pragma_eol (parser);
15143 c_token *token = c_parser_peek_token (parser);
15144 if (token && token->type == CPP_KEYWORD
15145 && token->keyword == RID_CILK_FOR)
15147 if (grain == NULL_TREE || grain == error_mark_node)
15148 grain = integer_zero_node;
15149 c_parser_cilk_for (parser, grain);
15151 else
15152 warning (0, "%<#pragma cilk grainsize%> is not followed by "
15153 "%<_Cilk_for%>");
15155 else
15156 c_parser_skip_to_pragma_eol (parser);
15159 /* Main entry point for parsing Cilk Plus <#pragma simd> for loops. */
15161 static void
15162 c_parser_cilk_simd (c_parser *parser)
15164 tree clauses = c_parser_cilk_all_clauses (parser);
15165 tree block = c_begin_compound_stmt (true);
15166 location_t loc = c_parser_peek_token (parser)->location;
15167 c_parser_omp_for_loop (loc, parser, CILK_SIMD, clauses, NULL);
15168 block = c_end_compound_stmt (loc, block, true);
15169 add_stmt (block);
15172 /* Create an artificial decl with TYPE and emit initialization of it with
15173 INIT. */
15175 static tree
15176 c_get_temp_regvar (tree type, tree init)
15178 location_t loc = EXPR_LOCATION (init);
15179 tree decl = build_decl (loc, VAR_DECL, NULL_TREE, type);
15180 DECL_ARTIFICIAL (decl) = 1;
15181 DECL_IGNORED_P (decl) = 1;
15182 pushdecl (decl);
15183 tree t = build2 (INIT_EXPR, type, decl, init);
15184 add_stmt (t);
15185 return decl;
15188 /* Main entry point for parsing Cilk Plus _Cilk_for loops.
15189 GRAIN is the grain value passed in through pragma or 0. */
15191 static void
15192 c_parser_cilk_for (c_parser *parser, tree grain)
15194 tree clauses = build_omp_clause (EXPR_LOCATION (grain), OMP_CLAUSE_SCHEDULE);
15195 OMP_CLAUSE_SCHEDULE_KIND (clauses) = OMP_CLAUSE_SCHEDULE_CILKFOR;
15196 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clauses) = grain;
15197 clauses = c_finish_omp_clauses (clauses);
15199 tree block = c_begin_compound_stmt (true);
15200 tree sb = push_stmt_list ();
15201 location_t loc = c_parser_peek_token (parser)->location;
15202 tree omp_for = c_parser_omp_for_loop (loc, parser, CILK_FOR, clauses, NULL);
15203 sb = pop_stmt_list (sb);
15205 if (omp_for)
15207 tree omp_par = make_node (OMP_PARALLEL);
15208 TREE_TYPE (omp_par) = void_type_node;
15209 OMP_PARALLEL_CLAUSES (omp_par) = NULL_TREE;
15210 tree bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
15211 TREE_SIDE_EFFECTS (bind) = 1;
15212 BIND_EXPR_BODY (bind) = sb;
15213 OMP_PARALLEL_BODY (omp_par) = bind;
15214 if (OMP_FOR_PRE_BODY (omp_for))
15216 add_stmt (OMP_FOR_PRE_BODY (omp_for));
15217 OMP_FOR_PRE_BODY (omp_for) = NULL_TREE;
15219 tree init = TREE_VEC_ELT (OMP_FOR_INIT (omp_for), 0);
15220 tree decl = TREE_OPERAND (init, 0);
15221 tree cond = TREE_VEC_ELT (OMP_FOR_COND (omp_for), 0);
15222 tree incr = TREE_VEC_ELT (OMP_FOR_INCR (omp_for), 0);
15223 tree t = TREE_OPERAND (cond, 1), c, clauses = NULL_TREE;
15224 if (TREE_CODE (t) != INTEGER_CST)
15226 TREE_OPERAND (cond, 1) = c_get_temp_regvar (TREE_TYPE (t), t);
15227 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
15228 OMP_CLAUSE_DECL (c) = TREE_OPERAND (cond, 1);
15229 OMP_CLAUSE_CHAIN (c) = clauses;
15230 clauses = c;
15232 if (TREE_CODE (incr) == MODIFY_EXPR)
15234 t = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
15235 if (TREE_CODE (t) != INTEGER_CST)
15237 TREE_OPERAND (TREE_OPERAND (incr, 1), 1)
15238 = c_get_temp_regvar (TREE_TYPE (t), t);
15239 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
15240 OMP_CLAUSE_DECL (c) = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
15241 OMP_CLAUSE_CHAIN (c) = clauses;
15242 clauses = c;
15245 t = TREE_OPERAND (init, 1);
15246 if (TREE_CODE (t) != INTEGER_CST)
15248 TREE_OPERAND (init, 1) = c_get_temp_regvar (TREE_TYPE (t), t);
15249 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
15250 OMP_CLAUSE_DECL (c) = TREE_OPERAND (init, 1);
15251 OMP_CLAUSE_CHAIN (c) = clauses;
15252 clauses = c;
15254 c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
15255 OMP_CLAUSE_DECL (c) = decl;
15256 OMP_CLAUSE_CHAIN (c) = clauses;
15257 clauses = c;
15258 c = build_omp_clause (input_location, OMP_CLAUSE__CILK_FOR_COUNT_);
15259 OMP_CLAUSE_OPERAND (c, 0)
15260 = cilk_for_number_of_iterations (omp_for);
15261 OMP_CLAUSE_CHAIN (c) = clauses;
15262 OMP_PARALLEL_CLAUSES (omp_par) = c_finish_omp_clauses (c);
15263 add_stmt (omp_par);
15266 block = c_end_compound_stmt (loc, block, true);
15267 add_stmt (block);
15271 /* Parse a transaction attribute (GCC Extension).
15273 transaction-attribute:
15274 attributes
15275 [ [ any-word ] ]
15277 The transactional memory language description is written for C++,
15278 and uses the C++0x attribute syntax. For compatibility, allow the
15279 bracket style for transactions in C as well. */
15281 static tree
15282 c_parser_transaction_attributes (c_parser *parser)
15284 tree attr_name, attr = NULL;
15286 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
15287 return c_parser_attributes (parser);
15289 if (!c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
15290 return NULL_TREE;
15291 c_parser_consume_token (parser);
15292 if (!c_parser_require (parser, CPP_OPEN_SQUARE, "expected %<[%>"))
15293 goto error1;
15295 attr_name = c_parser_attribute_any_word (parser);
15296 if (attr_name)
15298 c_parser_consume_token (parser);
15299 attr = build_tree_list (attr_name, NULL_TREE);
15301 else
15302 c_parser_error (parser, "expected identifier");
15304 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
15305 error1:
15306 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
15307 return attr;
15310 /* Parse a __transaction_atomic or __transaction_relaxed statement
15311 (GCC Extension).
15313 transaction-statement:
15314 __transaction_atomic transaction-attribute[opt] compound-statement
15315 __transaction_relaxed compound-statement
15317 Note that the only valid attribute is: "outer".
15320 static tree
15321 c_parser_transaction (c_parser *parser, enum rid keyword)
15323 unsigned int old_in = parser->in_transaction;
15324 unsigned int this_in = 1, new_in;
15325 location_t loc = c_parser_peek_token (parser)->location;
15326 tree stmt, attrs;
15328 gcc_assert ((keyword == RID_TRANSACTION_ATOMIC
15329 || keyword == RID_TRANSACTION_RELAXED)
15330 && c_parser_next_token_is_keyword (parser, keyword));
15331 c_parser_consume_token (parser);
15333 if (keyword == RID_TRANSACTION_RELAXED)
15334 this_in |= TM_STMT_ATTR_RELAXED;
15335 else
15337 attrs = c_parser_transaction_attributes (parser);
15338 if (attrs)
15339 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
15342 /* Keep track if we're in the lexical scope of an outer transaction. */
15343 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
15345 parser->in_transaction = new_in;
15346 stmt = c_parser_compound_statement (parser);
15347 parser->in_transaction = old_in;
15349 if (flag_tm)
15350 stmt = c_finish_transaction (loc, stmt, this_in);
15351 else
15352 error_at (loc, (keyword == RID_TRANSACTION_ATOMIC ?
15353 "%<__transaction_atomic%> without transactional memory support enabled"
15354 : "%<__transaction_relaxed %> "
15355 "without transactional memory support enabled"));
15357 return stmt;
15360 /* Parse a __transaction_atomic or __transaction_relaxed expression
15361 (GCC Extension).
15363 transaction-expression:
15364 __transaction_atomic ( expression )
15365 __transaction_relaxed ( expression )
15368 static struct c_expr
15369 c_parser_transaction_expression (c_parser *parser, enum rid keyword)
15371 struct c_expr ret;
15372 unsigned int old_in = parser->in_transaction;
15373 unsigned int this_in = 1;
15374 location_t loc = c_parser_peek_token (parser)->location;
15375 tree attrs;
15377 gcc_assert ((keyword == RID_TRANSACTION_ATOMIC
15378 || keyword == RID_TRANSACTION_RELAXED)
15379 && c_parser_next_token_is_keyword (parser, keyword));
15380 c_parser_consume_token (parser);
15382 if (keyword == RID_TRANSACTION_RELAXED)
15383 this_in |= TM_STMT_ATTR_RELAXED;
15384 else
15386 attrs = c_parser_transaction_attributes (parser);
15387 if (attrs)
15388 this_in |= parse_tm_stmt_attr (attrs, 0);
15391 parser->in_transaction = this_in;
15392 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
15394 tree expr = c_parser_expression (parser).value;
15395 ret.original_type = TREE_TYPE (expr);
15396 ret.value = build1 (TRANSACTION_EXPR, ret.original_type, expr);
15397 if (this_in & TM_STMT_ATTR_RELAXED)
15398 TRANSACTION_EXPR_RELAXED (ret.value) = 1;
15399 SET_EXPR_LOCATION (ret.value, loc);
15400 ret.original_code = TRANSACTION_EXPR;
15401 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
15403 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
15404 goto error;
15407 else
15409 error:
15410 ret.value = error_mark_node;
15411 ret.original_code = ERROR_MARK;
15412 ret.original_type = NULL;
15414 parser->in_transaction = old_in;
15416 if (!flag_tm)
15417 error_at (loc, (keyword == RID_TRANSACTION_ATOMIC ?
15418 "%<__transaction_atomic%> without transactional memory support enabled"
15419 : "%<__transaction_relaxed %> "
15420 "without transactional memory support enabled"));
15422 return ret;
15425 /* Parse a __transaction_cancel statement (GCC Extension).
15427 transaction-cancel-statement:
15428 __transaction_cancel transaction-attribute[opt] ;
15430 Note that the only valid attribute is "outer".
15433 static tree
15434 c_parser_transaction_cancel (c_parser *parser)
15436 location_t loc = c_parser_peek_token (parser)->location;
15437 tree attrs;
15438 bool is_outer = false;
15440 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TRANSACTION_CANCEL));
15441 c_parser_consume_token (parser);
15443 attrs = c_parser_transaction_attributes (parser);
15444 if (attrs)
15445 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
15447 if (!flag_tm)
15449 error_at (loc, "%<__transaction_cancel%> without "
15450 "transactional memory support enabled");
15451 goto ret_error;
15453 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
15455 error_at (loc, "%<__transaction_cancel%> within a "
15456 "%<__transaction_relaxed%>");
15457 goto ret_error;
15459 else if (is_outer)
15461 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
15462 && !is_tm_may_cancel_outer (current_function_decl))
15464 error_at (loc, "outer %<__transaction_cancel%> not "
15465 "within outer %<__transaction_atomic%>");
15466 error_at (loc, " or a %<transaction_may_cancel_outer%> function");
15467 goto ret_error;
15470 else if (parser->in_transaction == 0)
15472 error_at (loc, "%<__transaction_cancel%> not within "
15473 "%<__transaction_atomic%>");
15474 goto ret_error;
15477 return add_stmt (build_tm_abort_call (loc, is_outer));
15479 ret_error:
15480 return build1 (NOP_EXPR, void_type_node, error_mark_node);
15483 /* Parse a single source file. */
15485 void
15486 c_parse_file (void)
15488 /* Use local storage to begin. If the first token is a pragma, parse it.
15489 If it is #pragma GCC pch_preprocess, then this will load a PCH file
15490 which will cause garbage collection. */
15491 c_parser tparser;
15493 memset (&tparser, 0, sizeof tparser);
15494 tparser.tokens = &tparser.tokens_buf[0];
15495 the_parser = &tparser;
15497 if (c_parser_peek_token (&tparser)->pragma_kind == PRAGMA_GCC_PCH_PREPROCESS)
15498 c_parser_pragma_pch_preprocess (&tparser);
15500 the_parser = ggc_alloc<c_parser> ();
15501 *the_parser = tparser;
15502 if (tparser.tokens == &tparser.tokens_buf[0])
15503 the_parser->tokens = &the_parser->tokens_buf[0];
15505 /* Initialize EH, if we've been told to do so. */
15506 if (flag_exceptions)
15507 using_eh_for_cleanups ();
15509 c_parser_translation_unit (the_parser);
15510 the_parser = NULL;
15513 /* This function parses Cilk Plus array notation. The starting index is
15514 passed in INITIAL_INDEX and the array name is passes in ARRAY_VALUE. The
15515 return value of this function is a tree_node called VALUE_TREE of type
15516 ARRAY_NOTATION_REF. */
15518 static tree
15519 c_parser_array_notation (location_t loc, c_parser *parser, tree initial_index,
15520 tree array_value)
15522 c_token *token = NULL;
15523 tree start_index = NULL_TREE, end_index = NULL_TREE, stride = NULL_TREE;
15524 tree value_tree = NULL_TREE, type = NULL_TREE, array_type = NULL_TREE;
15525 tree array_type_domain = NULL_TREE;
15527 if (array_value == error_mark_node || initial_index == error_mark_node)
15529 /* No need to continue. If either of these 2 were true, then an error
15530 must be emitted already. Thus, no need to emit them twice. */
15531 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
15532 return error_mark_node;
15535 array_type = TREE_TYPE (array_value);
15536 gcc_assert (array_type);
15537 if (TREE_CODE (array_type) != ARRAY_TYPE
15538 && TREE_CODE (array_type) != POINTER_TYPE)
15540 error_at (loc, "base of array section must be pointer or array type");
15541 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
15542 return error_mark_node;
15544 type = TREE_TYPE (array_type);
15545 token = c_parser_peek_token (parser);
15547 if (token->type == CPP_EOF)
15549 c_parser_error (parser, "expected %<:%> or numeral");
15550 return value_tree;
15552 else if (token->type == CPP_COLON)
15554 if (!initial_index)
15556 /* If we are here, then we have a case like this A[:]. */
15557 c_parser_consume_token (parser);
15558 if (TREE_CODE (array_type) == POINTER_TYPE)
15560 error_at (loc, "start-index and length fields necessary for "
15561 "using array notations in pointers");
15562 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
15563 return error_mark_node;
15565 if (TREE_CODE (array_type) == FUNCTION_TYPE)
15567 error_at (loc, "array notations cannot be used with function "
15568 "type");
15569 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
15570 return error_mark_node;
15572 array_type_domain = TYPE_DOMAIN (array_type);
15574 if (!array_type_domain)
15576 error_at (loc, "start-index and length fields necessary for "
15577 "using array notations in dimensionless arrays");
15578 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
15579 return error_mark_node;
15582 start_index = TYPE_MINVAL (array_type_domain);
15583 start_index = fold_build1 (CONVERT_EXPR, ptrdiff_type_node,
15584 start_index);
15585 if (!TYPE_MAXVAL (array_type_domain)
15586 || !TREE_CONSTANT (TYPE_MAXVAL (array_type_domain)))
15588 error_at (loc, "start-index and length fields necessary for "
15589 "using array notations in variable-length arrays");
15590 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
15591 return error_mark_node;
15593 end_index = TYPE_MAXVAL (array_type_domain);
15594 end_index = fold_build2 (PLUS_EXPR, TREE_TYPE (end_index),
15595 end_index, integer_one_node);
15596 end_index = fold_build1 (CONVERT_EXPR, ptrdiff_type_node, end_index);
15597 stride = build_int_cst (integer_type_node, 1);
15598 stride = fold_build1 (CONVERT_EXPR, ptrdiff_type_node, stride);
15600 else if (initial_index != error_mark_node)
15602 /* If we are here, then there should be 2 possibilities:
15603 1. Array [EXPR : EXPR]
15604 2. Array [EXPR : EXPR : EXPR]
15606 start_index = initial_index;
15608 if (TREE_CODE (array_type) == FUNCTION_TYPE)
15610 error_at (loc, "array notations cannot be used with function "
15611 "type");
15612 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
15613 return error_mark_node;
15615 c_parser_consume_token (parser); /* consume the ':' */
15616 struct c_expr ce = c_parser_expression (parser);
15617 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
15618 end_index = ce.value;
15619 if (!end_index || end_index == error_mark_node)
15621 c_parser_skip_to_end_of_block_or_statement (parser);
15622 return error_mark_node;
15624 if (c_parser_peek_token (parser)->type == CPP_COLON)
15626 c_parser_consume_token (parser);
15627 ce = c_parser_expression (parser);
15628 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
15629 stride = ce.value;
15630 if (!stride || stride == error_mark_node)
15632 c_parser_skip_to_end_of_block_or_statement (parser);
15633 return error_mark_node;
15637 else
15638 c_parser_error (parser, "expected array notation expression");
15640 else
15641 c_parser_error (parser, "expected array notation expression");
15643 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
15645 value_tree = build_array_notation_ref (loc, array_value, start_index,
15646 end_index, stride, type);
15647 if (value_tree != error_mark_node)
15648 SET_EXPR_LOCATION (value_tree, loc);
15649 return value_tree;
15652 #include "gt-c-c-parser.h"