Split omp-low into multiple files
[gcc.git] / gcc / c / c-parser.c
1 /* Parser for C and Objective-C.
2 Copyright (C) 1987-2016 Free Software Foundation, Inc.
3
4 Parser actions based on the old Bison parser; structure somewhat
5 influenced by and fragments based on the C++ parser.
6
7 This file is part of GCC.
8
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.
13
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.
18
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/>. */
22
23 /* TODO:
24
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.
28
29 Add testcases covering every input symbol in every state in old and
30 new parsers.
31
32 Include full syntax for GNU C, including erroneous cases accepted
33 with error messages, in syntax productions in comments.
34
35 Make more diagnostics in the front end generally take an explicit
36 location rather than implicitly using input_location. */
37
38 #include "config.h"
39 #include "system.h"
40 #include "coretypes.h"
41 #include "target.h"
42 #include "function.h"
43 #include "c-tree.h"
44 #include "timevar.h"
45 #include "stringpool.h"
46 #include "cgraph.h"
47 #include "attribs.h"
48 #include "stor-layout.h"
49 #include "varasm.h"
50 #include "trans-mem.h"
51 #include "c-family/c-pragma.h"
52 #include "c-lang.h"
53 #include "c-family/c-objc.h"
54 #include "plugin.h"
55 #include "omp-general.h"
56 #include "omp-offload.h"
57 #include "builtins.h"
58 #include "gomp-constants.h"
59 #include "c-family/c-indentation.h"
60 #include "gimple-expr.h"
61 #include "context.h"
62 #include "gcc-rich-location.h"
63 #include "c-parser.h"
64 #include "gimple-parser.h"
65
66 /* We need to walk over decls with incomplete struct/union/enum types
67 after parsing the whole translation unit.
68 In finish_decl(), if the decl is static, has incomplete
69 struct/union/enum type, it is appeneded to incomplete_record_decls.
70 In c_parser_translation_unit(), we iterate over incomplete_record_decls
71 and report error if any of the decls are still incomplete. */
72
73 vec<tree> incomplete_record_decls;
74
75 void
76 set_c_expr_source_range (c_expr *expr,
77 location_t start, location_t finish)
78 {
79 expr->src_range.m_start = start;
80 expr->src_range.m_finish = finish;
81 if (expr->value)
82 set_source_range (expr->value, start, finish);
83 }
84
85 void
86 set_c_expr_source_range (c_expr *expr,
87 source_range src_range)
88 {
89 expr->src_range = src_range;
90 if (expr->value)
91 set_source_range (expr->value, src_range);
92 }
93
94 \f
95 /* Initialization routine for this file. */
96
97 void
98 c_parse_init (void)
99 {
100 /* The only initialization required is of the reserved word
101 identifiers. */
102 unsigned int i;
103 tree id;
104 int mask = 0;
105
106 /* Make sure RID_MAX hasn't grown past the 8 bits used to hold the keyword in
107 the c_token structure. */
108 gcc_assert (RID_MAX <= 255);
109
110 mask |= D_CXXONLY;
111 if (!flag_isoc99)
112 mask |= D_C99;
113 if (flag_no_asm)
114 {
115 mask |= D_ASM | D_EXT;
116 if (!flag_isoc99)
117 mask |= D_EXT89;
118 }
119 if (!c_dialect_objc ())
120 mask |= D_OBJC | D_CXX_OBJC;
121
122 ridpointers = ggc_cleared_vec_alloc<tree> ((int) RID_MAX);
123 for (i = 0; i < num_c_common_reswords; i++)
124 {
125 /* If a keyword is disabled, do not enter it into the table
126 and so create a canonical spelling that isn't a keyword. */
127 if (c_common_reswords[i].disable & mask)
128 {
129 if (warn_cxx_compat
130 && (c_common_reswords[i].disable & D_CXXWARN))
131 {
132 id = get_identifier (c_common_reswords[i].word);
133 C_SET_RID_CODE (id, RID_CXX_COMPAT_WARN);
134 C_IS_RESERVED_WORD (id) = 1;
135 }
136 continue;
137 }
138
139 id = get_identifier (c_common_reswords[i].word);
140 C_SET_RID_CODE (id, c_common_reswords[i].rid);
141 C_IS_RESERVED_WORD (id) = 1;
142 ridpointers [(int) c_common_reswords[i].rid] = id;
143 }
144
145 for (i = 0; i < NUM_INT_N_ENTS; i++)
146 {
147 /* We always create the symbols but they aren't always supported. */
148 char name[50];
149 sprintf (name, "__int%d", int_n_data[i].bitsize);
150 id = get_identifier (name);
151 C_SET_RID_CODE (id, RID_FIRST_INT_N + i);
152 C_IS_RESERVED_WORD (id) = 1;
153 }
154 }
155 \f
156 /* A parser structure recording information about the state and
157 context of parsing. Includes lexer information with up to two
158 tokens of look-ahead; more are not needed for C. */
159 struct GTY(()) c_parser {
160 /* The look-ahead tokens. */
161 c_token * GTY((skip)) tokens;
162 /* Buffer for look-ahead tokens. */
163 c_token tokens_buf[4];
164 /* How many look-ahead tokens are available (0 - 4, or
165 more if parsing from pre-lexed tokens). */
166 unsigned int tokens_avail;
167 /* True if a syntax error is being recovered from; false otherwise.
168 c_parser_error sets this flag. It should clear this flag when
169 enough tokens have been consumed to recover from the error. */
170 BOOL_BITFIELD error : 1;
171 /* True if we're processing a pragma, and shouldn't automatically
172 consume CPP_PRAGMA_EOL. */
173 BOOL_BITFIELD in_pragma : 1;
174 /* True if we're parsing the outermost block of an if statement. */
175 BOOL_BITFIELD in_if_block : 1;
176 /* True if we want to lex an untranslated string. */
177 BOOL_BITFIELD lex_untranslated_string : 1;
178
179 /* Objective-C specific parser/lexer information. */
180
181 /* True if we are in a context where the Objective-C "PQ" keywords
182 are considered keywords. */
183 BOOL_BITFIELD objc_pq_context : 1;
184 /* True if we are parsing a (potential) Objective-C foreach
185 statement. This is set to true after we parsed 'for (' and while
186 we wait for 'in' or ';' to decide if it's a standard C for loop or an
187 Objective-C foreach loop. */
188 BOOL_BITFIELD objc_could_be_foreach_context : 1;
189 /* The following flag is needed to contextualize Objective-C lexical
190 analysis. In some cases (e.g., 'int NSObject;'), it is
191 undesirable to bind an identifier to an Objective-C class, even
192 if a class with that name exists. */
193 BOOL_BITFIELD objc_need_raw_identifier : 1;
194 /* Nonzero if we're processing a __transaction statement. The value
195 is 1 | TM_STMT_ATTR_*. */
196 unsigned int in_transaction : 4;
197 /* True if we are in a context where the Objective-C "Property attribute"
198 keywords are valid. */
199 BOOL_BITFIELD objc_property_attr_context : 1;
200
201 /* Cilk Plus specific parser/lexer information. */
202
203 /* Buffer to hold all the tokens from parsing the vector attribute for the
204 SIMD-enabled functions (formerly known as elemental functions). */
205 vec <c_token, va_gc> *cilk_simd_fn_tokens;
206 };
207
208 /* Return a pointer to the Nth token in PARSERs tokens_buf. */
209
210 c_token *
211 c_parser_tokens_buf (c_parser *parser, unsigned n)
212 {
213 return &parser->tokens_buf[n];
214 }
215
216 /* Return the error state of PARSER. */
217
218 bool
219 c_parser_error (c_parser *parser)
220 {
221 return parser->error;
222 }
223
224 /* Set the error state of PARSER to ERR. */
225
226 void
227 c_parser_set_error (c_parser *parser, bool err)
228 {
229 parser->error = err;
230 }
231
232
233 /* The actual parser and external interface. ??? Does this need to be
234 garbage-collected? */
235
236 static GTY (()) c_parser *the_parser;
237
238 /* Read in and lex a single token, storing it in *TOKEN. */
239
240 static void
241 c_lex_one_token (c_parser *parser, c_token *token)
242 {
243 timevar_push (TV_LEX);
244
245 token->type = c_lex_with_flags (&token->value, &token->location,
246 &token->flags,
247 (parser->lex_untranslated_string
248 ? C_LEX_STRING_NO_TRANSLATE : 0));
249 token->id_kind = C_ID_NONE;
250 token->keyword = RID_MAX;
251 token->pragma_kind = PRAGMA_NONE;
252
253 switch (token->type)
254 {
255 case CPP_NAME:
256 {
257 tree decl;
258
259 bool objc_force_identifier = parser->objc_need_raw_identifier;
260 if (c_dialect_objc ())
261 parser->objc_need_raw_identifier = false;
262
263 if (C_IS_RESERVED_WORD (token->value))
264 {
265 enum rid rid_code = C_RID_CODE (token->value);
266
267 if (rid_code == RID_CXX_COMPAT_WARN)
268 {
269 warning_at (token->location,
270 OPT_Wc___compat,
271 "identifier %qE conflicts with C++ keyword",
272 token->value);
273 }
274 else if (rid_code >= RID_FIRST_ADDR_SPACE
275 && rid_code <= RID_LAST_ADDR_SPACE)
276 {
277 addr_space_t as;
278 as = (addr_space_t) (rid_code - RID_FIRST_ADDR_SPACE);
279 targetm.addr_space.diagnose_usage (as, token->location);
280 token->id_kind = C_ID_ADDRSPACE;
281 token->keyword = rid_code;
282 break;
283 }
284 else if (c_dialect_objc () && OBJC_IS_PQ_KEYWORD (rid_code))
285 {
286 /* We found an Objective-C "pq" keyword (in, out,
287 inout, bycopy, byref, oneway). They need special
288 care because the interpretation depends on the
289 context. */
290 if (parser->objc_pq_context)
291 {
292 token->type = CPP_KEYWORD;
293 token->keyword = rid_code;
294 break;
295 }
296 else if (parser->objc_could_be_foreach_context
297 && rid_code == RID_IN)
298 {
299 /* We are in Objective-C, inside a (potential)
300 foreach context (which means after having
301 parsed 'for (', but before having parsed ';'),
302 and we found 'in'. We consider it the keyword
303 which terminates the declaration at the
304 beginning of a foreach-statement. Note that
305 this means you can't use 'in' for anything else
306 in that context; in particular, in Objective-C
307 you can't use 'in' as the name of the running
308 variable in a C for loop. We could potentially
309 try to add code here to disambiguate, but it
310 seems a reasonable limitation. */
311 token->type = CPP_KEYWORD;
312 token->keyword = rid_code;
313 break;
314 }
315 /* Else, "pq" keywords outside of the "pq" context are
316 not keywords, and we fall through to the code for
317 normal tokens. */
318 }
319 else if (c_dialect_objc () && OBJC_IS_PATTR_KEYWORD (rid_code))
320 {
321 /* We found an Objective-C "property attribute"
322 keyword (getter, setter, readonly, etc). These are
323 only valid in the property context. */
324 if (parser->objc_property_attr_context)
325 {
326 token->type = CPP_KEYWORD;
327 token->keyword = rid_code;
328 break;
329 }
330 /* Else they are not special keywords.
331 */
332 }
333 else if (c_dialect_objc ()
334 && (OBJC_IS_AT_KEYWORD (rid_code)
335 || OBJC_IS_CXX_KEYWORD (rid_code)))
336 {
337 /* We found one of the Objective-C "@" keywords (defs,
338 selector, synchronized, etc) or one of the
339 Objective-C "cxx" keywords (class, private,
340 protected, public, try, catch, throw) without a
341 preceding '@' sign. Do nothing and fall through to
342 the code for normal tokens (in C++ we would still
343 consider the CXX ones keywords, but not in C). */
344 ;
345 }
346 else
347 {
348 token->type = CPP_KEYWORD;
349 token->keyword = rid_code;
350 break;
351 }
352 }
353
354 decl = lookup_name (token->value);
355 if (decl)
356 {
357 if (TREE_CODE (decl) == TYPE_DECL)
358 {
359 token->id_kind = C_ID_TYPENAME;
360 break;
361 }
362 }
363 else if (c_dialect_objc ())
364 {
365 tree objc_interface_decl = objc_is_class_name (token->value);
366 /* Objective-C class names are in the same namespace as
367 variables and typedefs, and hence are shadowed by local
368 declarations. */
369 if (objc_interface_decl
370 && (!objc_force_identifier || global_bindings_p ()))
371 {
372 token->value = objc_interface_decl;
373 token->id_kind = C_ID_CLASSNAME;
374 break;
375 }
376 }
377 token->id_kind = C_ID_ID;
378 }
379 break;
380 case CPP_AT_NAME:
381 /* This only happens in Objective-C; it must be a keyword. */
382 token->type = CPP_KEYWORD;
383 switch (C_RID_CODE (token->value))
384 {
385 /* Replace 'class' with '@class', 'private' with '@private',
386 etc. This prevents confusion with the C++ keyword
387 'class', and makes the tokens consistent with other
388 Objective-C 'AT' keywords. For example '@class' is
389 reported as RID_AT_CLASS which is consistent with
390 '@synchronized', which is reported as
391 RID_AT_SYNCHRONIZED.
392 */
393 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
394 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
395 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
396 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
397 case RID_THROW: token->keyword = RID_AT_THROW; break;
398 case RID_TRY: token->keyword = RID_AT_TRY; break;
399 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
400 case RID_SYNCHRONIZED: token->keyword = RID_AT_SYNCHRONIZED; break;
401 default: token->keyword = C_RID_CODE (token->value);
402 }
403 break;
404 case CPP_COLON:
405 case CPP_COMMA:
406 case CPP_CLOSE_PAREN:
407 case CPP_SEMICOLON:
408 /* These tokens may affect the interpretation of any identifiers
409 following, if doing Objective-C. */
410 if (c_dialect_objc ())
411 parser->objc_need_raw_identifier = false;
412 break;
413 case CPP_PRAGMA:
414 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
415 token->pragma_kind = (enum pragma_kind) TREE_INT_CST_LOW (token->value);
416 token->value = NULL;
417 break;
418 default:
419 break;
420 }
421 timevar_pop (TV_LEX);
422 }
423
424 /* Return a pointer to the next token from PARSER, reading it in if
425 necessary. */
426
427 c_token *
428 c_parser_peek_token (c_parser *parser)
429 {
430 if (parser->tokens_avail == 0)
431 {
432 c_lex_one_token (parser, &parser->tokens[0]);
433 parser->tokens_avail = 1;
434 }
435 return &parser->tokens[0];
436 }
437
438 /* Return a pointer to the next-but-one token from PARSER, reading it
439 in if necessary. The next token is already read in. */
440
441 c_token *
442 c_parser_peek_2nd_token (c_parser *parser)
443 {
444 if (parser->tokens_avail >= 2)
445 return &parser->tokens[1];
446 gcc_assert (parser->tokens_avail == 1);
447 gcc_assert (parser->tokens[0].type != CPP_EOF);
448 gcc_assert (parser->tokens[0].type != CPP_PRAGMA_EOL);
449 c_lex_one_token (parser, &parser->tokens[1]);
450 parser->tokens_avail = 2;
451 return &parser->tokens[1];
452 }
453
454 /* Return a pointer to the Nth token from PARSER, reading it
455 in if necessary. The N-1th token is already read in. */
456
457 c_token *
458 c_parser_peek_nth_token (c_parser *parser, unsigned int n)
459 {
460 /* N is 1-based, not zero-based. */
461 gcc_assert (n > 0);
462
463 if (parser->tokens_avail >= n)
464 return &parser->tokens[n - 1];
465 gcc_assert (parser->tokens_avail == n - 1);
466 c_lex_one_token (parser, &parser->tokens[n - 1]);
467 parser->tokens_avail = n;
468 return &parser->tokens[n - 1];
469 }
470
471 bool
472 c_keyword_starts_typename (enum rid keyword)
473 {
474 switch (keyword)
475 {
476 case RID_UNSIGNED:
477 case RID_LONG:
478 case RID_SHORT:
479 case RID_SIGNED:
480 case RID_COMPLEX:
481 case RID_INT:
482 case RID_CHAR:
483 case RID_FLOAT:
484 case RID_DOUBLE:
485 case RID_VOID:
486 case RID_DFLOAT32:
487 case RID_DFLOAT64:
488 case RID_DFLOAT128:
489 CASE_RID_FLOATN_NX:
490 case RID_BOOL:
491 case RID_ENUM:
492 case RID_STRUCT:
493 case RID_UNION:
494 case RID_TYPEOF:
495 case RID_CONST:
496 case RID_ATOMIC:
497 case RID_VOLATILE:
498 case RID_RESTRICT:
499 case RID_ATTRIBUTE:
500 case RID_FRACT:
501 case RID_ACCUM:
502 case RID_SAT:
503 case RID_AUTO_TYPE:
504 return true;
505 default:
506 if (keyword >= RID_FIRST_INT_N
507 && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
508 && int_n_enabled_p[keyword - RID_FIRST_INT_N])
509 return true;
510 return false;
511 }
512 }
513
514 /* Return true if TOKEN can start a type name,
515 false otherwise. */
516 bool
517 c_token_starts_typename (c_token *token)
518 {
519 switch (token->type)
520 {
521 case CPP_NAME:
522 switch (token->id_kind)
523 {
524 case C_ID_ID:
525 return false;
526 case C_ID_ADDRSPACE:
527 return true;
528 case C_ID_TYPENAME:
529 return true;
530 case C_ID_CLASSNAME:
531 gcc_assert (c_dialect_objc ());
532 return true;
533 default:
534 gcc_unreachable ();
535 }
536 case CPP_KEYWORD:
537 return c_keyword_starts_typename (token->keyword);
538 case CPP_LESS:
539 if (c_dialect_objc ())
540 return true;
541 return false;
542 default:
543 return false;
544 }
545 }
546
547 /* Return true if the next token from PARSER can start a type name,
548 false otherwise. LA specifies how to do lookahead in order to
549 detect unknown type names. If unsure, pick CLA_PREFER_ID. */
550
551 static inline bool
552 c_parser_next_tokens_start_typename (c_parser *parser, enum c_lookahead_kind la)
553 {
554 c_token *token = c_parser_peek_token (parser);
555 if (c_token_starts_typename (token))
556 return true;
557
558 /* Try a bit harder to detect an unknown typename. */
559 if (la != cla_prefer_id
560 && token->type == CPP_NAME
561 && token->id_kind == C_ID_ID
562
563 /* Do not try too hard when we could have "object in array". */
564 && !parser->objc_could_be_foreach_context
565
566 && (la == cla_prefer_type
567 || c_parser_peek_2nd_token (parser)->type == CPP_NAME
568 || c_parser_peek_2nd_token (parser)->type == CPP_MULT)
569
570 /* Only unknown identifiers. */
571 && !lookup_name (token->value))
572 return true;
573
574 return false;
575 }
576
577 /* Return true if TOKEN is a type qualifier, false otherwise. */
578 static bool
579 c_token_is_qualifier (c_token *token)
580 {
581 switch (token->type)
582 {
583 case CPP_NAME:
584 switch (token->id_kind)
585 {
586 case C_ID_ADDRSPACE:
587 return true;
588 default:
589 return false;
590 }
591 case CPP_KEYWORD:
592 switch (token->keyword)
593 {
594 case RID_CONST:
595 case RID_VOLATILE:
596 case RID_RESTRICT:
597 case RID_ATTRIBUTE:
598 case RID_ATOMIC:
599 return true;
600 default:
601 return false;
602 }
603 case CPP_LESS:
604 return false;
605 default:
606 gcc_unreachable ();
607 }
608 }
609
610 /* Return true if the next token from PARSER is a type qualifier,
611 false otherwise. */
612 static inline bool
613 c_parser_next_token_is_qualifier (c_parser *parser)
614 {
615 c_token *token = c_parser_peek_token (parser);
616 return c_token_is_qualifier (token);
617 }
618
619 /* Return true if TOKEN can start declaration specifiers, false
620 otherwise. */
621 static bool
622 c_token_starts_declspecs (c_token *token)
623 {
624 switch (token->type)
625 {
626 case CPP_NAME:
627 switch (token->id_kind)
628 {
629 case C_ID_ID:
630 return false;
631 case C_ID_ADDRSPACE:
632 return true;
633 case C_ID_TYPENAME:
634 return true;
635 case C_ID_CLASSNAME:
636 gcc_assert (c_dialect_objc ());
637 return true;
638 default:
639 gcc_unreachable ();
640 }
641 case CPP_KEYWORD:
642 switch (token->keyword)
643 {
644 case RID_STATIC:
645 case RID_EXTERN:
646 case RID_REGISTER:
647 case RID_TYPEDEF:
648 case RID_INLINE:
649 case RID_NORETURN:
650 case RID_AUTO:
651 case RID_THREAD:
652 case RID_UNSIGNED:
653 case RID_LONG:
654 case RID_SHORT:
655 case RID_SIGNED:
656 case RID_COMPLEX:
657 case RID_INT:
658 case RID_CHAR:
659 case RID_FLOAT:
660 case RID_DOUBLE:
661 case RID_VOID:
662 case RID_DFLOAT32:
663 case RID_DFLOAT64:
664 case RID_DFLOAT128:
665 CASE_RID_FLOATN_NX:
666 case RID_BOOL:
667 case RID_ENUM:
668 case RID_STRUCT:
669 case RID_UNION:
670 case RID_TYPEOF:
671 case RID_CONST:
672 case RID_VOLATILE:
673 case RID_RESTRICT:
674 case RID_ATTRIBUTE:
675 case RID_FRACT:
676 case RID_ACCUM:
677 case RID_SAT:
678 case RID_ALIGNAS:
679 case RID_ATOMIC:
680 case RID_AUTO_TYPE:
681 return true;
682 default:
683 if (token->keyword >= RID_FIRST_INT_N
684 && token->keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
685 && int_n_enabled_p[token->keyword - RID_FIRST_INT_N])
686 return true;
687 return false;
688 }
689 case CPP_LESS:
690 if (c_dialect_objc ())
691 return true;
692 return false;
693 default:
694 return false;
695 }
696 }
697
698
699 /* Return true if TOKEN can start declaration specifiers or a static
700 assertion, false otherwise. */
701 static bool
702 c_token_starts_declaration (c_token *token)
703 {
704 if (c_token_starts_declspecs (token)
705 || token->keyword == RID_STATIC_ASSERT)
706 return true;
707 else
708 return false;
709 }
710
711 /* Return true if the next token from PARSER can start declaration
712 specifiers, false otherwise. */
713 bool
714 c_parser_next_token_starts_declspecs (c_parser *parser)
715 {
716 c_token *token = c_parser_peek_token (parser);
717
718 /* In Objective-C, a classname normally starts a declspecs unless it
719 is immediately followed by a dot. In that case, it is the
720 Objective-C 2.0 "dot-syntax" for class objects, ie, calls the
721 setter/getter on the class. c_token_starts_declspecs() can't
722 differentiate between the two cases because it only checks the
723 current token, so we have a special check here. */
724 if (c_dialect_objc ()
725 && token->type == CPP_NAME
726 && token->id_kind == C_ID_CLASSNAME
727 && c_parser_peek_2nd_token (parser)->type == CPP_DOT)
728 return false;
729
730 return c_token_starts_declspecs (token);
731 }
732
733 /* Return true if the next tokens from PARSER can start declaration
734 specifiers or a static assertion, false otherwise. */
735 bool
736 c_parser_next_tokens_start_declaration (c_parser *parser)
737 {
738 c_token *token = c_parser_peek_token (parser);
739
740 /* Same as above. */
741 if (c_dialect_objc ()
742 && token->type == CPP_NAME
743 && token->id_kind == C_ID_CLASSNAME
744 && c_parser_peek_2nd_token (parser)->type == CPP_DOT)
745 return false;
746
747 /* Labels do not start declarations. */
748 if (token->type == CPP_NAME
749 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
750 return false;
751
752 if (c_token_starts_declaration (token))
753 return true;
754
755 if (c_parser_next_tokens_start_typename (parser, cla_nonabstract_decl))
756 return true;
757
758 return false;
759 }
760
761 /* Consume the next token from PARSER. */
762
763 void
764 c_parser_consume_token (c_parser *parser)
765 {
766 gcc_assert (parser->tokens_avail >= 1);
767 gcc_assert (parser->tokens[0].type != CPP_EOF);
768 gcc_assert (!parser->in_pragma || parser->tokens[0].type != CPP_PRAGMA_EOL);
769 gcc_assert (parser->error || parser->tokens[0].type != CPP_PRAGMA);
770 if (parser->tokens != &parser->tokens_buf[0])
771 parser->tokens++;
772 else if (parser->tokens_avail == 2)
773 parser->tokens[0] = parser->tokens[1];
774 parser->tokens_avail--;
775 }
776
777 /* Expect the current token to be a #pragma. Consume it and remember
778 that we've begun parsing a pragma. */
779
780 static void
781 c_parser_consume_pragma (c_parser *parser)
782 {
783 gcc_assert (!parser->in_pragma);
784 gcc_assert (parser->tokens_avail >= 1);
785 gcc_assert (parser->tokens[0].type == CPP_PRAGMA);
786 if (parser->tokens != &parser->tokens_buf[0])
787 parser->tokens++;
788 else if (parser->tokens_avail == 2)
789 parser->tokens[0] = parser->tokens[1];
790 parser->tokens_avail--;
791 parser->in_pragma = true;
792 }
793
794 /* Update the global input_location from TOKEN. */
795 static inline void
796 c_parser_set_source_position_from_token (c_token *token)
797 {
798 if (token->type != CPP_EOF)
799 {
800 input_location = token->location;
801 }
802 }
803
804 /* Helper function for c_parser_error.
805 Having peeked a token of kind TOK1_KIND that might signify
806 a conflict marker, peek successor tokens to determine
807 if we actually do have a conflict marker.
808 Specifically, we consider a run of 7 '<', '=' or '>' characters
809 at the start of a line as a conflict marker.
810 These come through the lexer as three pairs and a single,
811 e.g. three CPP_LSHIFT ("<<") and a CPP_LESS ('<').
812 If it returns true, *OUT_LOC is written to with the location/range
813 of the marker. */
814
815 static bool
816 c_parser_peek_conflict_marker (c_parser *parser, enum cpp_ttype tok1_kind,
817 location_t *out_loc)
818 {
819 c_token *token2 = c_parser_peek_2nd_token (parser);
820 if (token2->type != tok1_kind)
821 return false;
822 c_token *token3 = c_parser_peek_nth_token (parser, 3);
823 if (token3->type != tok1_kind)
824 return false;
825 c_token *token4 = c_parser_peek_nth_token (parser, 4);
826 if (token4->type != conflict_marker_get_final_tok_kind (tok1_kind))
827 return false;
828
829 /* It must be at the start of the line. */
830 location_t start_loc = c_parser_peek_token (parser)->location;
831 if (LOCATION_COLUMN (start_loc) != 1)
832 return false;
833
834 /* We have a conflict marker. Construct a location of the form:
835 <<<<<<<
836 ^~~~~~~
837 with start == caret, finishing at the end of the marker. */
838 location_t finish_loc = get_finish (token4->location);
839 *out_loc = make_location (start_loc, start_loc, finish_loc);
840
841 return true;
842 }
843
844 /* Issue a diagnostic of the form
845 FILE:LINE: MESSAGE before TOKEN
846 where TOKEN is the next token in the input stream of PARSER.
847 MESSAGE (specified by the caller) is usually of the form "expected
848 OTHER-TOKEN".
849
850 Do not issue a diagnostic if still recovering from an error.
851
852 ??? This is taken from the C++ parser, but building up messages in
853 this way is not i18n-friendly and some other approach should be
854 used. */
855
856 void
857 c_parser_error (c_parser *parser, const char *gmsgid)
858 {
859 c_token *token = c_parser_peek_token (parser);
860 if (parser->error)
861 return;
862 parser->error = true;
863 if (!gmsgid)
864 return;
865
866 /* If this is actually a conflict marker, report it as such. */
867 if (token->type == CPP_LSHIFT
868 || token->type == CPP_RSHIFT
869 || token->type == CPP_EQ_EQ)
870 {
871 location_t loc;
872 if (c_parser_peek_conflict_marker (parser, token->type, &loc))
873 {
874 error_at (loc, "version control conflict marker in file");
875 return;
876 }
877 }
878
879 /* This diagnostic makes more sense if it is tagged to the line of
880 the token we just peeked at. */
881 c_parser_set_source_position_from_token (token);
882 c_parse_error (gmsgid,
883 /* Because c_parse_error does not understand
884 CPP_KEYWORD, keywords are treated like
885 identifiers. */
886 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
887 /* ??? The C parser does not save the cpp flags of a
888 token, we need to pass 0 here and we will not get
889 the source spelling of some tokens but rather the
890 canonical spelling. */
891 token->value, /*flags=*/0);
892 }
893
894 /* If the next token is of the indicated TYPE, consume it. Otherwise,
895 issue the error MSGID. If MSGID is NULL then a message has already
896 been produced and no message will be produced this time. Returns
897 true if found, false otherwise. */
898
899 bool
900 c_parser_require (c_parser *parser,
901 enum cpp_ttype type,
902 const char *msgid)
903 {
904 if (c_parser_next_token_is (parser, type))
905 {
906 c_parser_consume_token (parser);
907 return true;
908 }
909 else
910 {
911 c_parser_error (parser, msgid);
912 return false;
913 }
914 }
915
916 /* If the next token is the indicated keyword, consume it. Otherwise,
917 issue the error MSGID. Returns true if found, false otherwise. */
918
919 static bool
920 c_parser_require_keyword (c_parser *parser,
921 enum rid keyword,
922 const char *msgid)
923 {
924 if (c_parser_next_token_is_keyword (parser, keyword))
925 {
926 c_parser_consume_token (parser);
927 return true;
928 }
929 else
930 {
931 c_parser_error (parser, msgid);
932 return false;
933 }
934 }
935
936 /* Like c_parser_require, except that tokens will be skipped until the
937 desired token is found. An error message is still produced if the
938 next token is not as expected. If MSGID is NULL then a message has
939 already been produced and no message will be produced this
940 time. */
941
942 void
943 c_parser_skip_until_found (c_parser *parser,
944 enum cpp_ttype type,
945 const char *msgid)
946 {
947 unsigned nesting_depth = 0;
948
949 if (c_parser_require (parser, type, msgid))
950 return;
951
952 /* Skip tokens until the desired token is found. */
953 while (true)
954 {
955 /* Peek at the next token. */
956 c_token *token = c_parser_peek_token (parser);
957 /* If we've reached the token we want, consume it and stop. */
958 if (token->type == type && !nesting_depth)
959 {
960 c_parser_consume_token (parser);
961 break;
962 }
963
964 /* If we've run out of tokens, stop. */
965 if (token->type == CPP_EOF)
966 return;
967 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
968 return;
969 if (token->type == CPP_OPEN_BRACE
970 || token->type == CPP_OPEN_PAREN
971 || token->type == CPP_OPEN_SQUARE)
972 ++nesting_depth;
973 else if (token->type == CPP_CLOSE_BRACE
974 || token->type == CPP_CLOSE_PAREN
975 || token->type == CPP_CLOSE_SQUARE)
976 {
977 if (nesting_depth-- == 0)
978 break;
979 }
980 /* Consume this token. */
981 c_parser_consume_token (parser);
982 }
983 parser->error = false;
984 }
985
986 /* Skip tokens until the end of a parameter is found, but do not
987 consume the comma, semicolon or closing delimiter. */
988
989 static void
990 c_parser_skip_to_end_of_parameter (c_parser *parser)
991 {
992 unsigned nesting_depth = 0;
993
994 while (true)
995 {
996 c_token *token = c_parser_peek_token (parser);
997 if ((token->type == CPP_COMMA || token->type == CPP_SEMICOLON)
998 && !nesting_depth)
999 break;
1000 /* If we've run out of tokens, stop. */
1001 if (token->type == CPP_EOF)
1002 return;
1003 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
1004 return;
1005 if (token->type == CPP_OPEN_BRACE
1006 || token->type == CPP_OPEN_PAREN
1007 || token->type == CPP_OPEN_SQUARE)
1008 ++nesting_depth;
1009 else if (token->type == CPP_CLOSE_BRACE
1010 || token->type == CPP_CLOSE_PAREN
1011 || token->type == CPP_CLOSE_SQUARE)
1012 {
1013 if (nesting_depth-- == 0)
1014 break;
1015 }
1016 /* Consume this token. */
1017 c_parser_consume_token (parser);
1018 }
1019 parser->error = false;
1020 }
1021
1022 /* Expect to be at the end of the pragma directive and consume an
1023 end of line marker. */
1024
1025 static void
1026 c_parser_skip_to_pragma_eol (c_parser *parser, bool error_if_not_eol = true)
1027 {
1028 gcc_assert (parser->in_pragma);
1029 parser->in_pragma = false;
1030
1031 if (error_if_not_eol && c_parser_peek_token (parser)->type != CPP_PRAGMA_EOL)
1032 c_parser_error (parser, "expected end of line");
1033
1034 cpp_ttype token_type;
1035 do
1036 {
1037 c_token *token = c_parser_peek_token (parser);
1038 token_type = token->type;
1039 if (token_type == CPP_EOF)
1040 break;
1041 c_parser_consume_token (parser);
1042 }
1043 while (token_type != CPP_PRAGMA_EOL);
1044
1045 parser->error = false;
1046 }
1047
1048 /* Skip tokens until we have consumed an entire block, or until we
1049 have consumed a non-nested ';'. */
1050
1051 static void
1052 c_parser_skip_to_end_of_block_or_statement (c_parser *parser)
1053 {
1054 unsigned nesting_depth = 0;
1055 bool save_error = parser->error;
1056
1057 while (true)
1058 {
1059 c_token *token;
1060
1061 /* Peek at the next token. */
1062 token = c_parser_peek_token (parser);
1063
1064 switch (token->type)
1065 {
1066 case CPP_EOF:
1067 return;
1068
1069 case CPP_PRAGMA_EOL:
1070 if (parser->in_pragma)
1071 return;
1072 break;
1073
1074 case CPP_SEMICOLON:
1075 /* If the next token is a ';', we have reached the
1076 end of the statement. */
1077 if (!nesting_depth)
1078 {
1079 /* Consume the ';'. */
1080 c_parser_consume_token (parser);
1081 goto finished;
1082 }
1083 break;
1084
1085 case CPP_CLOSE_BRACE:
1086 /* If the next token is a non-nested '}', then we have
1087 reached the end of the current block. */
1088 if (nesting_depth == 0 || --nesting_depth == 0)
1089 {
1090 c_parser_consume_token (parser);
1091 goto finished;
1092 }
1093 break;
1094
1095 case CPP_OPEN_BRACE:
1096 /* If it the next token is a '{', then we are entering a new
1097 block. Consume the entire block. */
1098 ++nesting_depth;
1099 break;
1100
1101 case CPP_PRAGMA:
1102 /* If we see a pragma, consume the whole thing at once. We
1103 have some safeguards against consuming pragmas willy-nilly.
1104 Normally, we'd expect to be here with parser->error set,
1105 which disables these safeguards. But it's possible to get
1106 here for secondary error recovery, after parser->error has
1107 been cleared. */
1108 c_parser_consume_pragma (parser);
1109 c_parser_skip_to_pragma_eol (parser);
1110 parser->error = save_error;
1111 continue;
1112
1113 default:
1114 break;
1115 }
1116
1117 c_parser_consume_token (parser);
1118 }
1119
1120 finished:
1121 parser->error = false;
1122 }
1123
1124 /* CPP's options (initialized by c-opts.c). */
1125 extern cpp_options *cpp_opts;
1126
1127 /* Save the warning flags which are controlled by __extension__. */
1128
1129 static inline int
1130 disable_extension_diagnostics (void)
1131 {
1132 int ret = (pedantic
1133 | (warn_pointer_arith << 1)
1134 | (warn_traditional << 2)
1135 | (flag_iso << 3)
1136 | (warn_long_long << 4)
1137 | (warn_cxx_compat << 5)
1138 | (warn_overlength_strings << 6)
1139 /* warn_c90_c99_compat has three states: -1/0/1, so we must
1140 play tricks to properly restore it. */
1141 | ((warn_c90_c99_compat == 1) << 7)
1142 | ((warn_c90_c99_compat == -1) << 8)
1143 /* Similarly for warn_c99_c11_compat. */
1144 | ((warn_c99_c11_compat == 1) << 9)
1145 | ((warn_c99_c11_compat == -1) << 10)
1146 );
1147 cpp_opts->cpp_pedantic = pedantic = 0;
1148 warn_pointer_arith = 0;
1149 cpp_opts->cpp_warn_traditional = warn_traditional = 0;
1150 flag_iso = 0;
1151 cpp_opts->cpp_warn_long_long = warn_long_long = 0;
1152 warn_cxx_compat = 0;
1153 warn_overlength_strings = 0;
1154 warn_c90_c99_compat = 0;
1155 warn_c99_c11_compat = 0;
1156 return ret;
1157 }
1158
1159 /* Restore the warning flags which are controlled by __extension__.
1160 FLAGS is the return value from disable_extension_diagnostics. */
1161
1162 static inline void
1163 restore_extension_diagnostics (int flags)
1164 {
1165 cpp_opts->cpp_pedantic = pedantic = flags & 1;
1166 warn_pointer_arith = (flags >> 1) & 1;
1167 cpp_opts->cpp_warn_traditional = warn_traditional = (flags >> 2) & 1;
1168 flag_iso = (flags >> 3) & 1;
1169 cpp_opts->cpp_warn_long_long = warn_long_long = (flags >> 4) & 1;
1170 warn_cxx_compat = (flags >> 5) & 1;
1171 warn_overlength_strings = (flags >> 6) & 1;
1172 /* See above for why is this needed. */
1173 warn_c90_c99_compat = (flags >> 7) & 1 ? 1 : ((flags >> 8) & 1 ? -1 : 0);
1174 warn_c99_c11_compat = (flags >> 9) & 1 ? 1 : ((flags >> 10) & 1 ? -1 : 0);
1175 }
1176
1177 /* Helper data structure for parsing #pragma acc routine. */
1178 struct oacc_routine_data {
1179 bool error_seen; /* Set if error has been reported. */
1180 bool fndecl_seen; /* Set if one fn decl/definition has been seen already. */
1181 tree clauses;
1182 location_t loc;
1183 };
1184
1185 static void c_parser_external_declaration (c_parser *);
1186 static void c_parser_asm_definition (c_parser *);
1187 static void c_parser_declaration_or_fndef (c_parser *, bool, bool, bool,
1188 bool, bool, tree *, vec<c_token>,
1189 struct oacc_routine_data * = NULL,
1190 bool * = NULL);
1191 static void c_parser_static_assert_declaration_no_semi (c_parser *);
1192 static void c_parser_static_assert_declaration (c_parser *);
1193 static struct c_typespec c_parser_enum_specifier (c_parser *);
1194 static struct c_typespec c_parser_struct_or_union_specifier (c_parser *);
1195 static tree c_parser_struct_declaration (c_parser *);
1196 static struct c_typespec c_parser_typeof_specifier (c_parser *);
1197 static tree c_parser_alignas_specifier (c_parser *);
1198 static struct c_declarator *c_parser_direct_declarator (c_parser *, bool,
1199 c_dtr_syn, bool *);
1200 static struct c_declarator *c_parser_direct_declarator_inner (c_parser *,
1201 bool,
1202 struct c_declarator *);
1203 static struct c_arg_info *c_parser_parms_declarator (c_parser *, bool, tree);
1204 static struct c_arg_info *c_parser_parms_list_declarator (c_parser *, tree,
1205 tree);
1206 static struct c_parm *c_parser_parameter_declaration (c_parser *, tree);
1207 static tree c_parser_simple_asm_expr (c_parser *);
1208 static tree c_parser_attributes (c_parser *);
1209 static struct c_expr c_parser_initializer (c_parser *);
1210 static struct c_expr c_parser_braced_init (c_parser *, tree, bool,
1211 struct obstack *);
1212 static void c_parser_initelt (c_parser *, struct obstack *);
1213 static void c_parser_initval (c_parser *, struct c_expr *,
1214 struct obstack *);
1215 static tree c_parser_compound_statement (c_parser *);
1216 static void c_parser_compound_statement_nostart (c_parser *);
1217 static void c_parser_label (c_parser *);
1218 static void c_parser_statement (c_parser *, bool *);
1219 static void c_parser_statement_after_labels (c_parser *, bool *,
1220 vec<tree> * = NULL);
1221 static void c_parser_if_statement (c_parser *, bool *, vec<tree> *);
1222 static void c_parser_switch_statement (c_parser *, bool *);
1223 static void c_parser_while_statement (c_parser *, bool, bool *);
1224 static void c_parser_do_statement (c_parser *, bool);
1225 static void c_parser_for_statement (c_parser *, bool, bool *);
1226 static tree c_parser_asm_statement (c_parser *);
1227 static tree c_parser_asm_operands (c_parser *);
1228 static tree c_parser_asm_goto_operands (c_parser *);
1229 static tree c_parser_asm_clobbers (c_parser *);
1230 static struct c_expr c_parser_expr_no_commas (c_parser *, struct c_expr *,
1231 tree = NULL_TREE);
1232 static struct c_expr c_parser_conditional_expression (c_parser *,
1233 struct c_expr *, tree);
1234 static struct c_expr c_parser_binary_expression (c_parser *, struct c_expr *,
1235 tree);
1236 static struct c_expr c_parser_cast_expression (c_parser *, struct c_expr *);
1237 static struct c_expr c_parser_unary_expression (c_parser *);
1238 static struct c_expr c_parser_sizeof_expression (c_parser *);
1239 static struct c_expr c_parser_alignof_expression (c_parser *);
1240 static struct c_expr c_parser_postfix_expression (c_parser *);
1241 static struct c_expr c_parser_postfix_expression_after_paren_type (c_parser *,
1242 struct c_type_name *,
1243 location_t);
1244 static struct c_expr c_parser_postfix_expression_after_primary (c_parser *,
1245 location_t loc,
1246 struct c_expr);
1247 static tree c_parser_transaction (c_parser *, enum rid);
1248 static struct c_expr c_parser_transaction_expression (c_parser *, enum rid);
1249 static tree c_parser_transaction_cancel (c_parser *);
1250 static struct c_expr c_parser_expression (c_parser *);
1251 static struct c_expr c_parser_expression_conv (c_parser *);
1252 static vec<tree, va_gc> *c_parser_expr_list (c_parser *, bool, bool,
1253 vec<tree, va_gc> **, location_t *,
1254 tree *, vec<location_t> *,
1255 unsigned int * = NULL);
1256 static void c_parser_oacc_declare (c_parser *);
1257 static void c_parser_oacc_enter_exit_data (c_parser *, bool);
1258 static void c_parser_oacc_update (c_parser *);
1259 static void c_parser_omp_construct (c_parser *, bool *);
1260 static void c_parser_omp_threadprivate (c_parser *);
1261 static void c_parser_omp_barrier (c_parser *);
1262 static void c_parser_omp_flush (c_parser *);
1263 static tree c_parser_omp_for_loop (location_t, c_parser *, enum tree_code,
1264 tree, tree *, bool *);
1265 static void c_parser_omp_taskwait (c_parser *);
1266 static void c_parser_omp_taskyield (c_parser *);
1267 static void c_parser_omp_cancel (c_parser *);
1268
1269 enum pragma_context { pragma_external, pragma_struct, pragma_param,
1270 pragma_stmt, pragma_compound };
1271 static bool c_parser_pragma (c_parser *, enum pragma_context, bool *);
1272 static void c_parser_omp_cancellation_point (c_parser *, enum pragma_context);
1273 static bool c_parser_omp_target (c_parser *, enum pragma_context, bool *);
1274 static void c_parser_omp_end_declare_target (c_parser *);
1275 static void c_parser_omp_declare (c_parser *, enum pragma_context);
1276 static bool c_parser_omp_ordered (c_parser *, enum pragma_context, bool *);
1277 static void c_parser_oacc_routine (c_parser *, enum pragma_context);
1278
1279 /* These Objective-C parser functions are only ever called when
1280 compiling Objective-C. */
1281 static void c_parser_objc_class_definition (c_parser *, tree);
1282 static void c_parser_objc_class_instance_variables (c_parser *);
1283 static void c_parser_objc_class_declaration (c_parser *);
1284 static void c_parser_objc_alias_declaration (c_parser *);
1285 static void c_parser_objc_protocol_definition (c_parser *, tree);
1286 static bool c_parser_objc_method_type (c_parser *);
1287 static void c_parser_objc_method_definition (c_parser *);
1288 static void c_parser_objc_methodprotolist (c_parser *);
1289 static void c_parser_objc_methodproto (c_parser *);
1290 static tree c_parser_objc_method_decl (c_parser *, bool, tree *, tree *);
1291 static tree c_parser_objc_type_name (c_parser *);
1292 static tree c_parser_objc_protocol_refs (c_parser *);
1293 static void c_parser_objc_try_catch_finally_statement (c_parser *);
1294 static void c_parser_objc_synchronized_statement (c_parser *);
1295 static tree c_parser_objc_selector (c_parser *);
1296 static tree c_parser_objc_selector_arg (c_parser *);
1297 static tree c_parser_objc_receiver (c_parser *);
1298 static tree c_parser_objc_message_args (c_parser *);
1299 static tree c_parser_objc_keywordexpr (c_parser *);
1300 static void c_parser_objc_at_property_declaration (c_parser *);
1301 static void c_parser_objc_at_synthesize_declaration (c_parser *);
1302 static void c_parser_objc_at_dynamic_declaration (c_parser *);
1303 static bool c_parser_objc_diagnose_bad_element_prefix
1304 (c_parser *, struct c_declspecs *);
1305
1306 /* Cilk Plus supporting routines. */
1307 static void c_parser_cilk_simd (c_parser *, bool *);
1308 static void c_parser_cilk_for (c_parser *, tree, bool *);
1309 static bool c_parser_cilk_verify_simd (c_parser *, enum pragma_context);
1310 static tree c_parser_array_notation (location_t, c_parser *, tree, tree);
1311 static tree c_parser_cilk_clause_vectorlength (c_parser *, tree, bool);
1312 static void c_parser_cilk_grainsize (c_parser *, bool *);
1313
1314 /* Parse a translation unit (C90 6.7, C99 6.9).
1315
1316 translation-unit:
1317 external-declarations
1318
1319 external-declarations:
1320 external-declaration
1321 external-declarations external-declaration
1322
1323 GNU extensions:
1324
1325 translation-unit:
1326 empty
1327 */
1328
1329 static void
1330 c_parser_translation_unit (c_parser *parser)
1331 {
1332 if (c_parser_next_token_is (parser, CPP_EOF))
1333 {
1334 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
1335 "ISO C forbids an empty translation unit");
1336 }
1337 else
1338 {
1339 void *obstack_position = obstack_alloc (&parser_obstack, 0);
1340 mark_valid_location_for_stdc_pragma (false);
1341 do
1342 {
1343 ggc_collect ();
1344 c_parser_external_declaration (parser);
1345 obstack_free (&parser_obstack, obstack_position);
1346 }
1347 while (c_parser_next_token_is_not (parser, CPP_EOF));
1348 }
1349
1350 unsigned int i;
1351 tree decl;
1352 FOR_EACH_VEC_ELT (incomplete_record_decls, i, decl)
1353 if (DECL_SIZE (decl) == NULL_TREE && TREE_TYPE (decl) != error_mark_node)
1354 error ("storage size of %q+D isn%'t known", decl);
1355 }
1356
1357 /* Parse an external declaration (C90 6.7, C99 6.9).
1358
1359 external-declaration:
1360 function-definition
1361 declaration
1362
1363 GNU extensions:
1364
1365 external-declaration:
1366 asm-definition
1367 ;
1368 __extension__ external-declaration
1369
1370 Objective-C:
1371
1372 external-declaration:
1373 objc-class-definition
1374 objc-class-declaration
1375 objc-alias-declaration
1376 objc-protocol-definition
1377 objc-method-definition
1378 @end
1379 */
1380
1381 static void
1382 c_parser_external_declaration (c_parser *parser)
1383 {
1384 int ext;
1385 switch (c_parser_peek_token (parser)->type)
1386 {
1387 case CPP_KEYWORD:
1388 switch (c_parser_peek_token (parser)->keyword)
1389 {
1390 case RID_EXTENSION:
1391 ext = disable_extension_diagnostics ();
1392 c_parser_consume_token (parser);
1393 c_parser_external_declaration (parser);
1394 restore_extension_diagnostics (ext);
1395 break;
1396 case RID_ASM:
1397 c_parser_asm_definition (parser);
1398 break;
1399 case RID_AT_INTERFACE:
1400 case RID_AT_IMPLEMENTATION:
1401 gcc_assert (c_dialect_objc ());
1402 c_parser_objc_class_definition (parser, NULL_TREE);
1403 break;
1404 case RID_AT_CLASS:
1405 gcc_assert (c_dialect_objc ());
1406 c_parser_objc_class_declaration (parser);
1407 break;
1408 case RID_AT_ALIAS:
1409 gcc_assert (c_dialect_objc ());
1410 c_parser_objc_alias_declaration (parser);
1411 break;
1412 case RID_AT_PROTOCOL:
1413 gcc_assert (c_dialect_objc ());
1414 c_parser_objc_protocol_definition (parser, NULL_TREE);
1415 break;
1416 case RID_AT_PROPERTY:
1417 gcc_assert (c_dialect_objc ());
1418 c_parser_objc_at_property_declaration (parser);
1419 break;
1420 case RID_AT_SYNTHESIZE:
1421 gcc_assert (c_dialect_objc ());
1422 c_parser_objc_at_synthesize_declaration (parser);
1423 break;
1424 case RID_AT_DYNAMIC:
1425 gcc_assert (c_dialect_objc ());
1426 c_parser_objc_at_dynamic_declaration (parser);
1427 break;
1428 case RID_AT_END:
1429 gcc_assert (c_dialect_objc ());
1430 c_parser_consume_token (parser);
1431 objc_finish_implementation ();
1432 break;
1433 default:
1434 goto decl_or_fndef;
1435 }
1436 break;
1437 case CPP_SEMICOLON:
1438 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
1439 "ISO C does not allow extra %<;%> outside of a function");
1440 c_parser_consume_token (parser);
1441 break;
1442 case CPP_PRAGMA:
1443 mark_valid_location_for_stdc_pragma (true);
1444 c_parser_pragma (parser, pragma_external, NULL);
1445 mark_valid_location_for_stdc_pragma (false);
1446 break;
1447 case CPP_PLUS:
1448 case CPP_MINUS:
1449 if (c_dialect_objc ())
1450 {
1451 c_parser_objc_method_definition (parser);
1452 break;
1453 }
1454 /* Else fall through, and yield a syntax error trying to parse
1455 as a declaration or function definition. */
1456 /* FALLTHRU */
1457 default:
1458 decl_or_fndef:
1459 /* A declaration or a function definition (or, in Objective-C,
1460 an @interface or @protocol with prefix attributes). We can
1461 only tell which after parsing the declaration specifiers, if
1462 any, and the first declarator. */
1463 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
1464 NULL, vNULL);
1465 break;
1466 }
1467 }
1468
1469 static void c_finish_omp_declare_simd (c_parser *, tree, tree, vec<c_token>);
1470 static void c_finish_oacc_routine (struct oacc_routine_data *, tree, bool);
1471
1472 /* Parse a declaration or function definition (C90 6.5, 6.7.1, C99
1473 6.7, 6.9.1). If FNDEF_OK is true, a function definition is
1474 accepted; otherwise (old-style parameter declarations) only other
1475 declarations are accepted. If STATIC_ASSERT_OK is true, a static
1476 assertion is accepted; otherwise (old-style parameter declarations)
1477 it is not. If NESTED is true, we are inside a function or parsing
1478 old-style parameter declarations; any functions encountered are
1479 nested functions and declaration specifiers are required; otherwise
1480 we are at top level and functions are normal functions and
1481 declaration specifiers may be optional. If EMPTY_OK is true, empty
1482 declarations are OK (subject to all other constraints); otherwise
1483 (old-style parameter declarations) they are diagnosed. If
1484 START_ATTR_OK is true, the declaration specifiers may start with
1485 attributes; otherwise they may not.
1486 OBJC_FOREACH_OBJECT_DECLARATION can be used to get back the parsed
1487 declaration when parsing an Objective-C foreach statement.
1488 FALLTHRU_ATTR_P is used to signal whether this function parsed
1489 "__attribute__((fallthrough));".
1490
1491 declaration:
1492 declaration-specifiers init-declarator-list[opt] ;
1493 static_assert-declaration
1494
1495 function-definition:
1496 declaration-specifiers[opt] declarator declaration-list[opt]
1497 compound-statement
1498
1499 declaration-list:
1500 declaration
1501 declaration-list declaration
1502
1503 init-declarator-list:
1504 init-declarator
1505 init-declarator-list , init-declarator
1506
1507 init-declarator:
1508 declarator simple-asm-expr[opt] attributes[opt]
1509 declarator simple-asm-expr[opt] attributes[opt] = initializer
1510
1511 GNU extensions:
1512
1513 nested-function-definition:
1514 declaration-specifiers declarator declaration-list[opt]
1515 compound-statement
1516
1517 attribute ;
1518
1519 Objective-C:
1520 attributes objc-class-definition
1521 attributes objc-category-definition
1522 attributes objc-protocol-definition
1523
1524 The simple-asm-expr and attributes are GNU extensions.
1525
1526 This function does not handle __extension__; that is handled in its
1527 callers. ??? Following the old parser, __extension__ may start
1528 external declarations, declarations in functions and declarations
1529 at the start of "for" loops, but not old-style parameter
1530 declarations.
1531
1532 C99 requires declaration specifiers in a function definition; the
1533 absence is diagnosed through the diagnosis of implicit int. In GNU
1534 C we also allow but diagnose declarations without declaration
1535 specifiers, but only at top level (elsewhere they conflict with
1536 other syntax).
1537
1538 In Objective-C, declarations of the looping variable in a foreach
1539 statement are exceptionally terminated by 'in' (for example, 'for
1540 (NSObject *object in array) { ... }').
1541
1542 OpenMP:
1543
1544 declaration:
1545 threadprivate-directive
1546
1547 GIMPLE:
1548
1549 gimple-function-definition:
1550 declaration-specifiers[opt] __GIMPLE (gimple-pass-list) declarator
1551 declaration-list[opt] compound-statement */
1552
1553 static void
1554 c_parser_declaration_or_fndef (c_parser *parser, bool fndef_ok,
1555 bool static_assert_ok, bool empty_ok,
1556 bool nested, bool start_attr_ok,
1557 tree *objc_foreach_object_declaration,
1558 vec<c_token> omp_declare_simd_clauses,
1559 struct oacc_routine_data *oacc_routine_data,
1560 bool *fallthru_attr_p)
1561 {
1562 struct c_declspecs *specs;
1563 tree prefix_attrs;
1564 tree all_prefix_attrs;
1565 bool diagnosed_no_specs = false;
1566 location_t here = c_parser_peek_token (parser)->location;
1567
1568 if (static_assert_ok
1569 && c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
1570 {
1571 c_parser_static_assert_declaration (parser);
1572 return;
1573 }
1574 specs = build_null_declspecs ();
1575
1576 /* Try to detect an unknown type name when we have "A B" or "A *B". */
1577 if (c_parser_peek_token (parser)->type == CPP_NAME
1578 && c_parser_peek_token (parser)->id_kind == C_ID_ID
1579 && (c_parser_peek_2nd_token (parser)->type == CPP_NAME
1580 || c_parser_peek_2nd_token (parser)->type == CPP_MULT)
1581 && (!nested || !lookup_name (c_parser_peek_token (parser)->value)))
1582 {
1583 tree name = c_parser_peek_token (parser)->value;
1584
1585 /* Issue a warning about NAME being an unknown type name, perhaps
1586 with some kind of hint.
1587 If the user forgot a "struct" etc, suggest inserting
1588 it. Otherwise, attempt to look for misspellings. */
1589 gcc_rich_location richloc (here);
1590 if (tag_exists_p (RECORD_TYPE, name))
1591 {
1592 /* This is not C++ with its implicit typedef. */
1593 richloc.add_fixit_insert_before ("struct ");
1594 error_at_rich_loc (&richloc,
1595 "unknown type name %qE;"
1596 " use %<struct%> keyword to refer to the type",
1597 name);
1598 }
1599 else if (tag_exists_p (UNION_TYPE, name))
1600 {
1601 richloc.add_fixit_insert_before ("union ");
1602 error_at_rich_loc (&richloc,
1603 "unknown type name %qE;"
1604 " use %<union%> keyword to refer to the type",
1605 name);
1606 }
1607 else if (tag_exists_p (ENUMERAL_TYPE, name))
1608 {
1609 richloc.add_fixit_insert_before ("enum ");
1610 error_at_rich_loc (&richloc,
1611 "unknown type name %qE;"
1612 " use %<enum%> keyword to refer to the type",
1613 name);
1614 }
1615 else
1616 {
1617 const char *hint = lookup_name_fuzzy (name, FUZZY_LOOKUP_TYPENAME);
1618 if (hint)
1619 {
1620 richloc.add_fixit_replace (hint);
1621 error_at_rich_loc (&richloc,
1622 "unknown type name %qE; did you mean %qs?",
1623 name, hint);
1624 }
1625 else
1626 error_at (here, "unknown type name %qE", name);
1627 }
1628
1629 /* Parse declspecs normally to get a correct pointer type, but avoid
1630 a further "fails to be a type name" error. Refuse nested functions
1631 since it is not how the user likely wants us to recover. */
1632 c_parser_peek_token (parser)->type = CPP_KEYWORD;
1633 c_parser_peek_token (parser)->keyword = RID_VOID;
1634 c_parser_peek_token (parser)->value = error_mark_node;
1635 fndef_ok = !nested;
1636 }
1637
1638 c_parser_declspecs (parser, specs, true, true, start_attr_ok,
1639 true, true, cla_nonabstract_decl);
1640 if (parser->error)
1641 {
1642 c_parser_skip_to_end_of_block_or_statement (parser);
1643 return;
1644 }
1645 if (nested && !specs->declspecs_seen_p)
1646 {
1647 c_parser_error (parser, "expected declaration specifiers");
1648 c_parser_skip_to_end_of_block_or_statement (parser);
1649 return;
1650 }
1651
1652 finish_declspecs (specs);
1653 bool auto_type_p = specs->typespec_word == cts_auto_type;
1654 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1655 {
1656 if (auto_type_p)
1657 error_at (here, "%<__auto_type%> in empty declaration");
1658 else if (specs->typespec_kind == ctsk_none
1659 && attribute_fallthrough_p (specs->attrs))
1660 {
1661 if (fallthru_attr_p != NULL)
1662 *fallthru_attr_p = true;
1663 tree fn = build_call_expr_internal_loc (here, IFN_FALLTHROUGH,
1664 void_type_node, 0);
1665 add_stmt (fn);
1666 }
1667 else if (empty_ok)
1668 shadow_tag (specs);
1669 else
1670 {
1671 shadow_tag_warned (specs, 1);
1672 pedwarn (here, 0, "empty declaration");
1673 }
1674 c_parser_consume_token (parser);
1675 if (oacc_routine_data)
1676 c_finish_oacc_routine (oacc_routine_data, NULL_TREE, false);
1677 return;
1678 }
1679
1680 /* Provide better error recovery. Note that a type name here is usually
1681 better diagnosed as a redeclaration. */
1682 if (empty_ok
1683 && specs->typespec_kind == ctsk_tagdef
1684 && c_parser_next_token_starts_declspecs (parser)
1685 && !c_parser_next_token_is (parser, CPP_NAME))
1686 {
1687 c_parser_error (parser, "expected %<;%>, identifier or %<(%>");
1688 parser->error = false;
1689 shadow_tag_warned (specs, 1);
1690 return;
1691 }
1692 else if (c_dialect_objc () && !auto_type_p)
1693 {
1694 /* Prefix attributes are an error on method decls. */
1695 switch (c_parser_peek_token (parser)->type)
1696 {
1697 case CPP_PLUS:
1698 case CPP_MINUS:
1699 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1700 return;
1701 if (specs->attrs)
1702 {
1703 warning_at (c_parser_peek_token (parser)->location,
1704 OPT_Wattributes,
1705 "prefix attributes are ignored for methods");
1706 specs->attrs = NULL_TREE;
1707 }
1708 if (fndef_ok)
1709 c_parser_objc_method_definition (parser);
1710 else
1711 c_parser_objc_methodproto (parser);
1712 return;
1713 break;
1714 default:
1715 break;
1716 }
1717 /* This is where we parse 'attributes @interface ...',
1718 'attributes @implementation ...', 'attributes @protocol ...'
1719 (where attributes could be, for example, __attribute__
1720 ((deprecated)).
1721 */
1722 switch (c_parser_peek_token (parser)->keyword)
1723 {
1724 case RID_AT_INTERFACE:
1725 {
1726 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1727 return;
1728 c_parser_objc_class_definition (parser, specs->attrs);
1729 return;
1730 }
1731 break;
1732 case RID_AT_IMPLEMENTATION:
1733 {
1734 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1735 return;
1736 if (specs->attrs)
1737 {
1738 warning_at (c_parser_peek_token (parser)->location,
1739 OPT_Wattributes,
1740 "prefix attributes are ignored for implementations");
1741 specs->attrs = NULL_TREE;
1742 }
1743 c_parser_objc_class_definition (parser, NULL_TREE);
1744 return;
1745 }
1746 break;
1747 case RID_AT_PROTOCOL:
1748 {
1749 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1750 return;
1751 c_parser_objc_protocol_definition (parser, specs->attrs);
1752 return;
1753 }
1754 break;
1755 case RID_AT_ALIAS:
1756 case RID_AT_CLASS:
1757 case RID_AT_END:
1758 case RID_AT_PROPERTY:
1759 if (specs->attrs)
1760 {
1761 c_parser_error (parser, "unexpected attribute");
1762 specs->attrs = NULL;
1763 }
1764 break;
1765 default:
1766 break;
1767 }
1768 }
1769 else if (attribute_fallthrough_p (specs->attrs))
1770 warning_at (here, OPT_Wattributes,
1771 "%<fallthrough%> attribute not followed by %<;%>");
1772
1773 pending_xref_error ();
1774 prefix_attrs = specs->attrs;
1775 all_prefix_attrs = prefix_attrs;
1776 specs->attrs = NULL_TREE;
1777 while (true)
1778 {
1779 struct c_declarator *declarator;
1780 bool dummy = false;
1781 timevar_id_t tv;
1782 tree fnbody = NULL_TREE;
1783 /* Declaring either one or more declarators (in which case we
1784 should diagnose if there were no declaration specifiers) or a
1785 function definition (in which case the diagnostic for
1786 implicit int suffices). */
1787 declarator = c_parser_declarator (parser,
1788 specs->typespec_kind != ctsk_none,
1789 C_DTR_NORMAL, &dummy);
1790 if (declarator == NULL)
1791 {
1792 if (omp_declare_simd_clauses.exists ()
1793 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
1794 c_finish_omp_declare_simd (parser, NULL_TREE, NULL_TREE,
1795 omp_declare_simd_clauses);
1796 if (oacc_routine_data)
1797 c_finish_oacc_routine (oacc_routine_data, NULL_TREE, false);
1798 c_parser_skip_to_end_of_block_or_statement (parser);
1799 return;
1800 }
1801 if (auto_type_p && declarator->kind != cdk_id)
1802 {
1803 error_at (here,
1804 "%<__auto_type%> requires a plain identifier"
1805 " as declarator");
1806 c_parser_skip_to_end_of_block_or_statement (parser);
1807 return;
1808 }
1809 if (c_parser_next_token_is (parser, CPP_EQ)
1810 || c_parser_next_token_is (parser, CPP_COMMA)
1811 || c_parser_next_token_is (parser, CPP_SEMICOLON)
1812 || c_parser_next_token_is_keyword (parser, RID_ASM)
1813 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE)
1814 || c_parser_next_token_is_keyword (parser, RID_IN))
1815 {
1816 tree asm_name = NULL_TREE;
1817 tree postfix_attrs = NULL_TREE;
1818 if (!diagnosed_no_specs && !specs->declspecs_seen_p)
1819 {
1820 diagnosed_no_specs = true;
1821 pedwarn (here, 0, "data definition has no type or storage class");
1822 }
1823 /* Having seen a data definition, there cannot now be a
1824 function definition. */
1825 fndef_ok = false;
1826 if (c_parser_next_token_is_keyword (parser, RID_ASM))
1827 asm_name = c_parser_simple_asm_expr (parser);
1828 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1829 {
1830 postfix_attrs = c_parser_attributes (parser);
1831 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
1832 {
1833 /* This means there is an attribute specifier after
1834 the declarator in a function definition. Provide
1835 some more information for the user. */
1836 error_at (here, "attributes should be specified before the "
1837 "declarator in a function definition");
1838 c_parser_skip_to_end_of_block_or_statement (parser);
1839 return;
1840 }
1841 }
1842 if (c_parser_next_token_is (parser, CPP_EQ))
1843 {
1844 tree d;
1845 struct c_expr init;
1846 location_t init_loc;
1847 c_parser_consume_token (parser);
1848 if (auto_type_p)
1849 {
1850 start_init (NULL_TREE, asm_name, global_bindings_p ());
1851 init_loc = c_parser_peek_token (parser)->location;
1852 init = c_parser_expr_no_commas (parser, NULL);
1853 if (TREE_CODE (init.value) == COMPONENT_REF
1854 && DECL_C_BIT_FIELD (TREE_OPERAND (init.value, 1)))
1855 error_at (here,
1856 "%<__auto_type%> used with a bit-field"
1857 " initializer");
1858 init = convert_lvalue_to_rvalue (init_loc, init, true, true);
1859 tree init_type = TREE_TYPE (init.value);
1860 /* As with typeof, remove all qualifiers from atomic types. */
1861 if (init_type != error_mark_node && TYPE_ATOMIC (init_type))
1862 init_type
1863 = c_build_qualified_type (init_type, TYPE_UNQUALIFIED);
1864 bool vm_type = variably_modified_type_p (init_type,
1865 NULL_TREE);
1866 if (vm_type)
1867 init.value = c_save_expr (init.value);
1868 finish_init ();
1869 specs->typespec_kind = ctsk_typeof;
1870 specs->locations[cdw_typedef] = init_loc;
1871 specs->typedef_p = true;
1872 specs->type = init_type;
1873 if (vm_type)
1874 {
1875 bool maybe_const = true;
1876 tree type_expr = c_fully_fold (init.value, false,
1877 &maybe_const);
1878 specs->expr_const_operands &= maybe_const;
1879 if (specs->expr)
1880 specs->expr = build2 (COMPOUND_EXPR,
1881 TREE_TYPE (type_expr),
1882 specs->expr, type_expr);
1883 else
1884 specs->expr = type_expr;
1885 }
1886 d = start_decl (declarator, specs, true,
1887 chainon (postfix_attrs, all_prefix_attrs));
1888 if (!d)
1889 d = error_mark_node;
1890 if (omp_declare_simd_clauses.exists ()
1891 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
1892 c_finish_omp_declare_simd (parser, d, NULL_TREE,
1893 omp_declare_simd_clauses);
1894 }
1895 else
1896 {
1897 /* The declaration of the variable is in effect while
1898 its initializer is parsed. */
1899 d = start_decl (declarator, specs, true,
1900 chainon (postfix_attrs, all_prefix_attrs));
1901 if (!d)
1902 d = error_mark_node;
1903 if (omp_declare_simd_clauses.exists ()
1904 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
1905 c_finish_omp_declare_simd (parser, d, NULL_TREE,
1906 omp_declare_simd_clauses);
1907 start_init (d, asm_name, global_bindings_p ());
1908 init_loc = c_parser_peek_token (parser)->location;
1909 init = c_parser_initializer (parser);
1910 finish_init ();
1911 }
1912 if (oacc_routine_data)
1913 c_finish_oacc_routine (oacc_routine_data, d, false);
1914 if (d != error_mark_node)
1915 {
1916 maybe_warn_string_init (init_loc, TREE_TYPE (d), init);
1917 finish_decl (d, init_loc, init.value,
1918 init.original_type, asm_name);
1919 }
1920 }
1921 else
1922 {
1923 if (auto_type_p)
1924 {
1925 error_at (here,
1926 "%<__auto_type%> requires an initialized "
1927 "data declaration");
1928 c_parser_skip_to_end_of_block_or_statement (parser);
1929 return;
1930 }
1931 tree d = start_decl (declarator, specs, false,
1932 chainon (postfix_attrs,
1933 all_prefix_attrs));
1934 if (omp_declare_simd_clauses.exists ()
1935 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
1936 {
1937 tree parms = NULL_TREE;
1938 if (d && TREE_CODE (d) == FUNCTION_DECL)
1939 {
1940 struct c_declarator *ce = declarator;
1941 while (ce != NULL)
1942 if (ce->kind == cdk_function)
1943 {
1944 parms = ce->u.arg_info->parms;
1945 break;
1946 }
1947 else
1948 ce = ce->declarator;
1949 }
1950 if (parms)
1951 temp_store_parm_decls (d, parms);
1952 c_finish_omp_declare_simd (parser, d, parms,
1953 omp_declare_simd_clauses);
1954 if (parms)
1955 temp_pop_parm_decls ();
1956 }
1957 if (oacc_routine_data)
1958 c_finish_oacc_routine (oacc_routine_data, d, false);
1959 if (d)
1960 finish_decl (d, UNKNOWN_LOCATION, NULL_TREE,
1961 NULL_TREE, asm_name);
1962
1963 if (c_parser_next_token_is_keyword (parser, RID_IN))
1964 {
1965 if (d)
1966 *objc_foreach_object_declaration = d;
1967 else
1968 *objc_foreach_object_declaration = error_mark_node;
1969 }
1970 }
1971 if (c_parser_next_token_is (parser, CPP_COMMA))
1972 {
1973 if (auto_type_p)
1974 {
1975 error_at (here,
1976 "%<__auto_type%> may only be used with"
1977 " a single declarator");
1978 c_parser_skip_to_end_of_block_or_statement (parser);
1979 return;
1980 }
1981 c_parser_consume_token (parser);
1982 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1983 all_prefix_attrs = chainon (c_parser_attributes (parser),
1984 prefix_attrs);
1985 else
1986 all_prefix_attrs = prefix_attrs;
1987 continue;
1988 }
1989 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1990 {
1991 c_parser_consume_token (parser);
1992 return;
1993 }
1994 else if (c_parser_next_token_is_keyword (parser, RID_IN))
1995 {
1996 /* This can only happen in Objective-C: we found the
1997 'in' that terminates the declaration inside an
1998 Objective-C foreach statement. Do not consume the
1999 token, so that the caller can use it to determine
2000 that this indeed is a foreach context. */
2001 return;
2002 }
2003 else
2004 {
2005 c_parser_error (parser, "expected %<,%> or %<;%>");
2006 c_parser_skip_to_end_of_block_or_statement (parser);
2007 return;
2008 }
2009 }
2010 else if (auto_type_p)
2011 {
2012 error_at (here,
2013 "%<__auto_type%> requires an initialized data declaration");
2014 c_parser_skip_to_end_of_block_or_statement (parser);
2015 return;
2016 }
2017 else if (!fndef_ok)
2018 {
2019 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, "
2020 "%<asm%> or %<__attribute__%>");
2021 c_parser_skip_to_end_of_block_or_statement (parser);
2022 return;
2023 }
2024 /* Function definition (nested or otherwise). */
2025 if (nested)
2026 {
2027 pedwarn (here, OPT_Wpedantic, "ISO C forbids nested functions");
2028 c_push_function_context ();
2029 }
2030 if (!start_function (specs, declarator, all_prefix_attrs))
2031 {
2032 /* This can appear in many cases looking nothing like a
2033 function definition, so we don't give a more specific
2034 error suggesting there was one. */
2035 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, %<asm%> "
2036 "or %<__attribute__%>");
2037 if (nested)
2038 c_pop_function_context ();
2039 break;
2040 }
2041
2042 if (DECL_DECLARED_INLINE_P (current_function_decl))
2043 tv = TV_PARSE_INLINE;
2044 else
2045 tv = TV_PARSE_FUNC;
2046 timevar_push (tv);
2047
2048 /* Parse old-style parameter declarations. ??? Attributes are
2049 not allowed to start declaration specifiers here because of a
2050 syntax conflict between a function declaration with attribute
2051 suffix and a function definition with an attribute prefix on
2052 first old-style parameter declaration. Following the old
2053 parser, they are not accepted on subsequent old-style
2054 parameter declarations either. However, there is no
2055 ambiguity after the first declaration, nor indeed on the
2056 first as long as we don't allow postfix attributes after a
2057 declarator with a nonempty identifier list in a definition;
2058 and postfix attributes have never been accepted here in
2059 function definitions either. */
2060 while (c_parser_next_token_is_not (parser, CPP_EOF)
2061 && c_parser_next_token_is_not (parser, CPP_OPEN_BRACE))
2062 c_parser_declaration_or_fndef (parser, false, false, false,
2063 true, false, NULL, vNULL);
2064 store_parm_decls ();
2065 if (omp_declare_simd_clauses.exists ()
2066 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
2067 c_finish_omp_declare_simd (parser, current_function_decl, NULL_TREE,
2068 omp_declare_simd_clauses);
2069 if (oacc_routine_data)
2070 c_finish_oacc_routine (oacc_routine_data, current_function_decl, true);
2071 DECL_STRUCT_FUNCTION (current_function_decl)->function_start_locus
2072 = c_parser_peek_token (parser)->location;
2073
2074 /* If the definition was marked with __GIMPLE then parse the
2075 function body as GIMPLE. */
2076 if (specs->gimple_p)
2077 {
2078 cfun->pass_startwith = specs->gimple_pass;
2079 bool saved = in_late_binary_op;
2080 in_late_binary_op = true;
2081 c_parser_parse_gimple_body (parser);
2082 in_late_binary_op = saved;
2083 }
2084 else
2085 {
2086 fnbody = c_parser_compound_statement (parser);
2087 if (flag_cilkplus && contains_array_notation_expr (fnbody))
2088 fnbody = expand_array_notation_exprs (fnbody);
2089 }
2090 tree fndecl = current_function_decl;
2091 if (nested)
2092 {
2093 tree decl = current_function_decl;
2094 /* Mark nested functions as needing static-chain initially.
2095 lower_nested_functions will recompute it but the
2096 DECL_STATIC_CHAIN flag is also used before that happens,
2097 by initializer_constant_valid_p. See gcc.dg/nested-fn-2.c. */
2098 DECL_STATIC_CHAIN (decl) = 1;
2099 add_stmt (fnbody);
2100 finish_function ();
2101 c_pop_function_context ();
2102 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
2103 }
2104 else
2105 {
2106 if (fnbody)
2107 add_stmt (fnbody);
2108 finish_function ();
2109 }
2110 /* Get rid of the empty stmt list for GIMPLE. */
2111 if (specs->gimple_p)
2112 DECL_SAVED_TREE (fndecl) = NULL_TREE;
2113
2114 timevar_pop (tv);
2115 break;
2116 }
2117 }
2118
2119 /* Parse an asm-definition (asm() outside a function body). This is a
2120 GNU extension.
2121
2122 asm-definition:
2123 simple-asm-expr ;
2124 */
2125
2126 static void
2127 c_parser_asm_definition (c_parser *parser)
2128 {
2129 tree asm_str = c_parser_simple_asm_expr (parser);
2130 if (asm_str)
2131 symtab->finalize_toplevel_asm (asm_str);
2132 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
2133 }
2134
2135 /* Parse a static assertion (C11 6.7.10).
2136
2137 static_assert-declaration:
2138 static_assert-declaration-no-semi ;
2139 */
2140
2141 static void
2142 c_parser_static_assert_declaration (c_parser *parser)
2143 {
2144 c_parser_static_assert_declaration_no_semi (parser);
2145 if (parser->error
2146 || !c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
2147 c_parser_skip_to_end_of_block_or_statement (parser);
2148 }
2149
2150 /* Parse a static assertion (C11 6.7.10), without the trailing
2151 semicolon.
2152
2153 static_assert-declaration-no-semi:
2154 _Static_assert ( constant-expression , string-literal )
2155 */
2156
2157 static void
2158 c_parser_static_assert_declaration_no_semi (c_parser *parser)
2159 {
2160 location_t assert_loc, value_loc;
2161 tree value;
2162 tree string;
2163
2164 gcc_assert (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT));
2165 assert_loc = c_parser_peek_token (parser)->location;
2166 if (flag_isoc99)
2167 pedwarn_c99 (assert_loc, OPT_Wpedantic,
2168 "ISO C99 does not support %<_Static_assert%>");
2169 else
2170 pedwarn_c99 (assert_loc, OPT_Wpedantic,
2171 "ISO C90 does not support %<_Static_assert%>");
2172 c_parser_consume_token (parser);
2173 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2174 return;
2175 location_t value_tok_loc = c_parser_peek_token (parser)->location;
2176 value = c_parser_expr_no_commas (parser, NULL).value;
2177 value_loc = EXPR_LOC_OR_LOC (value, value_tok_loc);
2178 parser->lex_untranslated_string = true;
2179 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
2180 {
2181 parser->lex_untranslated_string = false;
2182 return;
2183 }
2184 switch (c_parser_peek_token (parser)->type)
2185 {
2186 case CPP_STRING:
2187 case CPP_STRING16:
2188 case CPP_STRING32:
2189 case CPP_WSTRING:
2190 case CPP_UTF8STRING:
2191 string = c_parser_peek_token (parser)->value;
2192 c_parser_consume_token (parser);
2193 parser->lex_untranslated_string = false;
2194 break;
2195 default:
2196 c_parser_error (parser, "expected string literal");
2197 parser->lex_untranslated_string = false;
2198 return;
2199 }
2200 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
2201
2202 if (!INTEGRAL_TYPE_P (TREE_TYPE (value)))
2203 {
2204 error_at (value_loc, "expression in static assertion is not an integer");
2205 return;
2206 }
2207 if (TREE_CODE (value) != INTEGER_CST)
2208 {
2209 value = c_fully_fold (value, false, NULL);
2210 /* Strip no-op conversions. */
2211 STRIP_TYPE_NOPS (value);
2212 if (TREE_CODE (value) == INTEGER_CST)
2213 pedwarn (value_loc, OPT_Wpedantic, "expression in static assertion "
2214 "is not an integer constant expression");
2215 }
2216 if (TREE_CODE (value) != INTEGER_CST)
2217 {
2218 error_at (value_loc, "expression in static assertion is not constant");
2219 return;
2220 }
2221 constant_expression_warning (value);
2222 if (integer_zerop (value))
2223 error_at (assert_loc, "static assertion failed: %E", string);
2224 }
2225
2226 /* Parse some declaration specifiers (possibly none) (C90 6.5, C99
2227 6.7), adding them to SPECS (which may already include some).
2228 Storage class specifiers are accepted iff SCSPEC_OK; type
2229 specifiers are accepted iff TYPESPEC_OK; alignment specifiers are
2230 accepted iff ALIGNSPEC_OK; attributes are accepted at the start
2231 iff START_ATTR_OK; __auto_type is accepted iff AUTO_TYPE_OK.
2232
2233 declaration-specifiers:
2234 storage-class-specifier declaration-specifiers[opt]
2235 type-specifier declaration-specifiers[opt]
2236 type-qualifier declaration-specifiers[opt]
2237 function-specifier declaration-specifiers[opt]
2238 alignment-specifier declaration-specifiers[opt]
2239
2240 Function specifiers (inline) are from C99, and are currently
2241 handled as storage class specifiers, as is __thread. Alignment
2242 specifiers are from C11.
2243
2244 C90 6.5.1, C99 6.7.1:
2245 storage-class-specifier:
2246 typedef
2247 extern
2248 static
2249 auto
2250 register
2251 _Thread_local
2252
2253 (_Thread_local is new in C11.)
2254
2255 C99 6.7.4:
2256 function-specifier:
2257 inline
2258 _Noreturn
2259
2260 (_Noreturn is new in C11.)
2261
2262 C90 6.5.2, C99 6.7.2:
2263 type-specifier:
2264 void
2265 char
2266 short
2267 int
2268 long
2269 float
2270 double
2271 signed
2272 unsigned
2273 _Bool
2274 _Complex
2275 [_Imaginary removed in C99 TC2]
2276 struct-or-union-specifier
2277 enum-specifier
2278 typedef-name
2279 atomic-type-specifier
2280
2281 (_Bool and _Complex are new in C99.)
2282 (atomic-type-specifier is new in C11.)
2283
2284 C90 6.5.3, C99 6.7.3:
2285
2286 type-qualifier:
2287 const
2288 restrict
2289 volatile
2290 address-space-qualifier
2291 _Atomic
2292
2293 (restrict is new in C99.)
2294 (_Atomic is new in C11.)
2295
2296 GNU extensions:
2297
2298 declaration-specifiers:
2299 attributes declaration-specifiers[opt]
2300
2301 type-qualifier:
2302 address-space
2303
2304 address-space:
2305 identifier recognized by the target
2306
2307 storage-class-specifier:
2308 __thread
2309
2310 type-specifier:
2311 typeof-specifier
2312 __auto_type
2313 __intN
2314 _Decimal32
2315 _Decimal64
2316 _Decimal128
2317 _Fract
2318 _Accum
2319 _Sat
2320
2321 (_Fract, _Accum, and _Sat are new from ISO/IEC DTR 18037:
2322 http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1169.pdf)
2323
2324 atomic-type-specifier
2325 _Atomic ( type-name )
2326
2327 Objective-C:
2328
2329 type-specifier:
2330 class-name objc-protocol-refs[opt]
2331 typedef-name objc-protocol-refs
2332 objc-protocol-refs
2333 */
2334
2335 void
2336 c_parser_declspecs (c_parser *parser, struct c_declspecs *specs,
2337 bool scspec_ok, bool typespec_ok, bool start_attr_ok,
2338 bool alignspec_ok, bool auto_type_ok,
2339 enum c_lookahead_kind la)
2340 {
2341 bool attrs_ok = start_attr_ok;
2342 bool seen_type = specs->typespec_kind != ctsk_none;
2343
2344 if (!typespec_ok)
2345 gcc_assert (la == cla_prefer_id);
2346
2347 while (c_parser_next_token_is (parser, CPP_NAME)
2348 || c_parser_next_token_is (parser, CPP_KEYWORD)
2349 || (c_dialect_objc () && c_parser_next_token_is (parser, CPP_LESS)))
2350 {
2351 struct c_typespec t;
2352 tree attrs;
2353 tree align;
2354 location_t loc = c_parser_peek_token (parser)->location;
2355
2356 /* If we cannot accept a type, exit if the next token must start
2357 one. Also, if we already have seen a tagged definition,
2358 a typename would be an error anyway and likely the user
2359 has simply forgotten a semicolon, so we exit. */
2360 if ((!typespec_ok || specs->typespec_kind == ctsk_tagdef)
2361 && c_parser_next_tokens_start_typename (parser, la)
2362 && !c_parser_next_token_is_qualifier (parser))
2363 break;
2364
2365 if (c_parser_next_token_is (parser, CPP_NAME))
2366 {
2367 c_token *name_token = c_parser_peek_token (parser);
2368 tree value = name_token->value;
2369 c_id_kind kind = name_token->id_kind;
2370
2371 if (kind == C_ID_ADDRSPACE)
2372 {
2373 addr_space_t as
2374 = name_token->keyword - RID_FIRST_ADDR_SPACE;
2375 declspecs_add_addrspace (name_token->location, specs, as);
2376 c_parser_consume_token (parser);
2377 attrs_ok = true;
2378 continue;
2379 }
2380
2381 gcc_assert (!c_parser_next_token_is_qualifier (parser));
2382
2383 /* If we cannot accept a type, and the next token must start one,
2384 exit. Do the same if we already have seen a tagged definition,
2385 since it would be an error anyway and likely the user has simply
2386 forgotten a semicolon. */
2387 if (seen_type || !c_parser_next_tokens_start_typename (parser, la))
2388 break;
2389
2390 /* Now at an unknown typename (C_ID_ID), a C_ID_TYPENAME or
2391 a C_ID_CLASSNAME. */
2392 c_parser_consume_token (parser);
2393 seen_type = true;
2394 attrs_ok = true;
2395 if (kind == C_ID_ID)
2396 {
2397 error_at (loc, "unknown type name %qE", value);
2398 t.kind = ctsk_typedef;
2399 t.spec = error_mark_node;
2400 }
2401 else if (kind == C_ID_TYPENAME
2402 && (!c_dialect_objc ()
2403 || c_parser_next_token_is_not (parser, CPP_LESS)))
2404 {
2405 t.kind = ctsk_typedef;
2406 /* For a typedef name, record the meaning, not the name.
2407 In case of 'foo foo, bar;'. */
2408 t.spec = lookup_name (value);
2409 }
2410 else
2411 {
2412 tree proto = NULL_TREE;
2413 gcc_assert (c_dialect_objc ());
2414 t.kind = ctsk_objc;
2415 if (c_parser_next_token_is (parser, CPP_LESS))
2416 proto = c_parser_objc_protocol_refs (parser);
2417 t.spec = objc_get_protocol_qualified_type (value, proto);
2418 }
2419 t.expr = NULL_TREE;
2420 t.expr_const_operands = true;
2421 declspecs_add_type (name_token->location, specs, t);
2422 continue;
2423 }
2424 if (c_parser_next_token_is (parser, CPP_LESS))
2425 {
2426 /* Make "<SomeProtocol>" equivalent to "id <SomeProtocol>" -
2427 nisse@lysator.liu.se. */
2428 tree proto;
2429 gcc_assert (c_dialect_objc ());
2430 if (!typespec_ok || seen_type)
2431 break;
2432 proto = c_parser_objc_protocol_refs (parser);
2433 t.kind = ctsk_objc;
2434 t.spec = objc_get_protocol_qualified_type (NULL_TREE, proto);
2435 t.expr = NULL_TREE;
2436 t.expr_const_operands = true;
2437 declspecs_add_type (loc, specs, t);
2438 continue;
2439 }
2440 gcc_assert (c_parser_next_token_is (parser, CPP_KEYWORD));
2441 switch (c_parser_peek_token (parser)->keyword)
2442 {
2443 case RID_STATIC:
2444 case RID_EXTERN:
2445 case RID_REGISTER:
2446 case RID_TYPEDEF:
2447 case RID_INLINE:
2448 case RID_NORETURN:
2449 case RID_AUTO:
2450 case RID_THREAD:
2451 if (!scspec_ok)
2452 goto out;
2453 attrs_ok = true;
2454 /* TODO: Distinguish between function specifiers (inline, noreturn)
2455 and storage class specifiers, either here or in
2456 declspecs_add_scspec. */
2457 declspecs_add_scspec (loc, specs,
2458 c_parser_peek_token (parser)->value);
2459 c_parser_consume_token (parser);
2460 break;
2461 case RID_AUTO_TYPE:
2462 if (!auto_type_ok)
2463 goto out;
2464 /* Fall through. */
2465 case RID_UNSIGNED:
2466 case RID_LONG:
2467 case RID_SHORT:
2468 case RID_SIGNED:
2469 case RID_COMPLEX:
2470 case RID_INT:
2471 case RID_CHAR:
2472 case RID_FLOAT:
2473 case RID_DOUBLE:
2474 case RID_VOID:
2475 case RID_DFLOAT32:
2476 case RID_DFLOAT64:
2477 case RID_DFLOAT128:
2478 CASE_RID_FLOATN_NX:
2479 case RID_BOOL:
2480 case RID_FRACT:
2481 case RID_ACCUM:
2482 case RID_SAT:
2483 case RID_INT_N_0:
2484 case RID_INT_N_1:
2485 case RID_INT_N_2:
2486 case RID_INT_N_3:
2487 if (!typespec_ok)
2488 goto out;
2489 attrs_ok = true;
2490 seen_type = true;
2491 if (c_dialect_objc ())
2492 parser->objc_need_raw_identifier = true;
2493 t.kind = ctsk_resword;
2494 t.spec = c_parser_peek_token (parser)->value;
2495 t.expr = NULL_TREE;
2496 t.expr_const_operands = true;
2497 declspecs_add_type (loc, specs, t);
2498 c_parser_consume_token (parser);
2499 break;
2500 case RID_ENUM:
2501 if (!typespec_ok)
2502 goto out;
2503 attrs_ok = true;
2504 seen_type = true;
2505 t = c_parser_enum_specifier (parser);
2506 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, t.spec);
2507 declspecs_add_type (loc, specs, t);
2508 break;
2509 case RID_STRUCT:
2510 case RID_UNION:
2511 if (!typespec_ok)
2512 goto out;
2513 attrs_ok = true;
2514 seen_type = true;
2515 t = c_parser_struct_or_union_specifier (parser);
2516 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, t.spec);
2517 declspecs_add_type (loc, specs, t);
2518 break;
2519 case RID_TYPEOF:
2520 /* ??? The old parser rejected typeof after other type
2521 specifiers, but is a syntax error the best way of
2522 handling this? */
2523 if (!typespec_ok || seen_type)
2524 goto out;
2525 attrs_ok = true;
2526 seen_type = true;
2527 t = c_parser_typeof_specifier (parser);
2528 declspecs_add_type (loc, specs, t);
2529 break;
2530 case RID_ATOMIC:
2531 /* C parser handling of Objective-C constructs needs
2532 checking for correct lvalue-to-rvalue conversions, and
2533 the code in build_modify_expr handling various
2534 Objective-C cases, and that in build_unary_op handling
2535 Objective-C cases for increment / decrement, also needs
2536 updating; uses of TYPE_MAIN_VARIANT in objc_compare_types
2537 and objc_types_are_equivalent may also need updates. */
2538 if (c_dialect_objc ())
2539 sorry ("%<_Atomic%> in Objective-C");
2540 if (flag_isoc99)
2541 pedwarn_c99 (loc, OPT_Wpedantic,
2542 "ISO C99 does not support the %<_Atomic%> qualifier");
2543 else
2544 pedwarn_c99 (loc, OPT_Wpedantic,
2545 "ISO C90 does not support the %<_Atomic%> qualifier");
2546 attrs_ok = true;
2547 tree value;
2548 value = c_parser_peek_token (parser)->value;
2549 c_parser_consume_token (parser);
2550 if (typespec_ok && c_parser_next_token_is (parser, CPP_OPEN_PAREN))
2551 {
2552 /* _Atomic ( type-name ). */
2553 seen_type = true;
2554 c_parser_consume_token (parser);
2555 struct c_type_name *type = c_parser_type_name (parser);
2556 t.kind = ctsk_typeof;
2557 t.spec = error_mark_node;
2558 t.expr = NULL_TREE;
2559 t.expr_const_operands = true;
2560 if (type != NULL)
2561 t.spec = groktypename (type, &t.expr,
2562 &t.expr_const_operands);
2563 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2564 "expected %<)%>");
2565 if (t.spec != error_mark_node)
2566 {
2567 if (TREE_CODE (t.spec) == ARRAY_TYPE)
2568 error_at (loc, "%<_Atomic%>-qualified array type");
2569 else if (TREE_CODE (t.spec) == FUNCTION_TYPE)
2570 error_at (loc, "%<_Atomic%>-qualified function type");
2571 else if (TYPE_QUALS (t.spec) != TYPE_UNQUALIFIED)
2572 error_at (loc, "%<_Atomic%> applied to a qualified type");
2573 else
2574 t.spec = c_build_qualified_type (t.spec, TYPE_QUAL_ATOMIC);
2575 }
2576 declspecs_add_type (loc, specs, t);
2577 }
2578 else
2579 declspecs_add_qual (loc, specs, value);
2580 break;
2581 case RID_CONST:
2582 case RID_VOLATILE:
2583 case RID_RESTRICT:
2584 attrs_ok = true;
2585 declspecs_add_qual (loc, specs, c_parser_peek_token (parser)->value);
2586 c_parser_consume_token (parser);
2587 break;
2588 case RID_ATTRIBUTE:
2589 if (!attrs_ok)
2590 goto out;
2591 attrs = c_parser_attributes (parser);
2592 declspecs_add_attrs (loc, specs, attrs);
2593 break;
2594 case RID_ALIGNAS:
2595 if (!alignspec_ok)
2596 goto out;
2597 align = c_parser_alignas_specifier (parser);
2598 declspecs_add_alignas (loc, specs, align);
2599 break;
2600 case RID_GIMPLE:
2601 if (! flag_gimple)
2602 error_at (loc, "%<__GIMPLE%> only valid with -fgimple");
2603 c_parser_consume_token (parser);
2604 specs->gimple_p = true;
2605 specs->locations[cdw_gimple] = loc;
2606 specs->gimple_pass = c_parser_gimple_pass_list (parser);
2607 break;
2608 default:
2609 goto out;
2610 }
2611 }
2612 out: ;
2613 }
2614
2615 /* Parse an enum specifier (C90 6.5.2.2, C99 6.7.2.2).
2616
2617 enum-specifier:
2618 enum attributes[opt] identifier[opt] { enumerator-list } attributes[opt]
2619 enum attributes[opt] identifier[opt] { enumerator-list , } attributes[opt]
2620 enum attributes[opt] identifier
2621
2622 The form with trailing comma is new in C99. The forms with
2623 attributes are GNU extensions. In GNU C, we accept any expression
2624 without commas in the syntax (assignment expressions, not just
2625 conditional expressions); assignment expressions will be diagnosed
2626 as non-constant.
2627
2628 enumerator-list:
2629 enumerator
2630 enumerator-list , enumerator
2631
2632 enumerator:
2633 enumeration-constant
2634 enumeration-constant = constant-expression
2635
2636 GNU Extensions:
2637
2638 enumerator:
2639 enumeration-constant attributes[opt]
2640 enumeration-constant attributes[opt] = constant-expression
2641
2642 */
2643
2644 static struct c_typespec
2645 c_parser_enum_specifier (c_parser *parser)
2646 {
2647 struct c_typespec ret;
2648 tree attrs;
2649 tree ident = NULL_TREE;
2650 location_t enum_loc;
2651 location_t ident_loc = UNKNOWN_LOCATION; /* Quiet warning. */
2652 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ENUM));
2653 enum_loc = c_parser_peek_token (parser)->location;
2654 c_parser_consume_token (parser);
2655 attrs = c_parser_attributes (parser);
2656 enum_loc = c_parser_peek_token (parser)->location;
2657 /* Set the location in case we create a decl now. */
2658 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
2659 if (c_parser_next_token_is (parser, CPP_NAME))
2660 {
2661 ident = c_parser_peek_token (parser)->value;
2662 ident_loc = c_parser_peek_token (parser)->location;
2663 enum_loc = ident_loc;
2664 c_parser_consume_token (parser);
2665 }
2666 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2667 {
2668 /* Parse an enum definition. */
2669 struct c_enum_contents the_enum;
2670 tree type;
2671 tree postfix_attrs;
2672 /* We chain the enumerators in reverse order, then put them in
2673 forward order at the end. */
2674 tree values;
2675 timevar_push (TV_PARSE_ENUM);
2676 type = start_enum (enum_loc, &the_enum, ident);
2677 values = NULL_TREE;
2678 c_parser_consume_token (parser);
2679 while (true)
2680 {
2681 tree enum_id;
2682 tree enum_value;
2683 tree enum_decl;
2684 bool seen_comma;
2685 c_token *token;
2686 location_t comma_loc = UNKNOWN_LOCATION; /* Quiet warning. */
2687 location_t decl_loc, value_loc;
2688 if (c_parser_next_token_is_not (parser, CPP_NAME))
2689 {
2690 /* Give a nicer error for "enum {}". */
2691 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE)
2692 && !parser->error)
2693 {
2694 error_at (c_parser_peek_token (parser)->location,
2695 "empty enum is invalid");
2696 parser->error = true;
2697 }
2698 else
2699 c_parser_error (parser, "expected identifier");
2700 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2701 values = error_mark_node;
2702 break;
2703 }
2704 token = c_parser_peek_token (parser);
2705 enum_id = token->value;
2706 /* Set the location in case we create a decl now. */
2707 c_parser_set_source_position_from_token (token);
2708 decl_loc = value_loc = token->location;
2709 c_parser_consume_token (parser);
2710 /* Parse any specified attributes. */
2711 tree enum_attrs = c_parser_attributes (parser);
2712 if (c_parser_next_token_is (parser, CPP_EQ))
2713 {
2714 c_parser_consume_token (parser);
2715 value_loc = c_parser_peek_token (parser)->location;
2716 enum_value = c_parser_expr_no_commas (parser, NULL).value;
2717 }
2718 else
2719 enum_value = NULL_TREE;
2720 enum_decl = build_enumerator (decl_loc, value_loc,
2721 &the_enum, enum_id, enum_value);
2722 if (enum_attrs)
2723 decl_attributes (&TREE_PURPOSE (enum_decl), enum_attrs, 0);
2724 TREE_CHAIN (enum_decl) = values;
2725 values = enum_decl;
2726 seen_comma = false;
2727 if (c_parser_next_token_is (parser, CPP_COMMA))
2728 {
2729 comma_loc = c_parser_peek_token (parser)->location;
2730 seen_comma = true;
2731 c_parser_consume_token (parser);
2732 }
2733 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2734 {
2735 if (seen_comma)
2736 pedwarn_c90 (comma_loc, OPT_Wpedantic,
2737 "comma at end of enumerator list");
2738 c_parser_consume_token (parser);
2739 break;
2740 }
2741 if (!seen_comma)
2742 {
2743 c_parser_error (parser, "expected %<,%> or %<}%>");
2744 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2745 values = error_mark_node;
2746 break;
2747 }
2748 }
2749 postfix_attrs = c_parser_attributes (parser);
2750 ret.spec = finish_enum (type, nreverse (values),
2751 chainon (attrs, postfix_attrs));
2752 ret.kind = ctsk_tagdef;
2753 ret.expr = NULL_TREE;
2754 ret.expr_const_operands = true;
2755 timevar_pop (TV_PARSE_ENUM);
2756 return ret;
2757 }
2758 else if (!ident)
2759 {
2760 c_parser_error (parser, "expected %<{%>");
2761 ret.spec = error_mark_node;
2762 ret.kind = ctsk_tagref;
2763 ret.expr = NULL_TREE;
2764 ret.expr_const_operands = true;
2765 return ret;
2766 }
2767 ret = parser_xref_tag (ident_loc, ENUMERAL_TYPE, ident);
2768 /* In ISO C, enumerated types can be referred to only if already
2769 defined. */
2770 if (pedantic && !COMPLETE_TYPE_P (ret.spec))
2771 {
2772 gcc_assert (ident);
2773 pedwarn (enum_loc, OPT_Wpedantic,
2774 "ISO C forbids forward references to %<enum%> types");
2775 }
2776 return ret;
2777 }
2778
2779 /* Parse a struct or union specifier (C90 6.5.2.1, C99 6.7.2.1).
2780
2781 struct-or-union-specifier:
2782 struct-or-union attributes[opt] identifier[opt]
2783 { struct-contents } attributes[opt]
2784 struct-or-union attributes[opt] identifier
2785
2786 struct-contents:
2787 struct-declaration-list
2788
2789 struct-declaration-list:
2790 struct-declaration ;
2791 struct-declaration-list struct-declaration ;
2792
2793 GNU extensions:
2794
2795 struct-contents:
2796 empty
2797 struct-declaration
2798 struct-declaration-list struct-declaration
2799
2800 struct-declaration-list:
2801 struct-declaration-list ;
2802 ;
2803
2804 (Note that in the syntax here, unlike that in ISO C, the semicolons
2805 are included here rather than in struct-declaration, in order to
2806 describe the syntax with extra semicolons and missing semicolon at
2807 end.)
2808
2809 Objective-C:
2810
2811 struct-declaration-list:
2812 @defs ( class-name )
2813
2814 (Note this does not include a trailing semicolon, but can be
2815 followed by further declarations, and gets a pedwarn-if-pedantic
2816 when followed by a semicolon.) */
2817
2818 static struct c_typespec
2819 c_parser_struct_or_union_specifier (c_parser *parser)
2820 {
2821 struct c_typespec ret;
2822 tree attrs;
2823 tree ident = NULL_TREE;
2824 location_t struct_loc;
2825 location_t ident_loc = UNKNOWN_LOCATION;
2826 enum tree_code code;
2827 switch (c_parser_peek_token (parser)->keyword)
2828 {
2829 case RID_STRUCT:
2830 code = RECORD_TYPE;
2831 break;
2832 case RID_UNION:
2833 code = UNION_TYPE;
2834 break;
2835 default:
2836 gcc_unreachable ();
2837 }
2838 struct_loc = c_parser_peek_token (parser)->location;
2839 c_parser_consume_token (parser);
2840 attrs = c_parser_attributes (parser);
2841
2842 /* Set the location in case we create a decl now. */
2843 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
2844
2845 if (c_parser_next_token_is (parser, CPP_NAME))
2846 {
2847 ident = c_parser_peek_token (parser)->value;
2848 ident_loc = c_parser_peek_token (parser)->location;
2849 struct_loc = ident_loc;
2850 c_parser_consume_token (parser);
2851 }
2852 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2853 {
2854 /* Parse a struct or union definition. Start the scope of the
2855 tag before parsing components. */
2856 struct c_struct_parse_info *struct_info;
2857 tree type = start_struct (struct_loc, code, ident, &struct_info);
2858 tree postfix_attrs;
2859 /* We chain the components in reverse order, then put them in
2860 forward order at the end. Each struct-declaration may
2861 declare multiple components (comma-separated), so we must use
2862 chainon to join them, although when parsing each
2863 struct-declaration we can use TREE_CHAIN directly.
2864
2865 The theory behind all this is that there will be more
2866 semicolon separated fields than comma separated fields, and
2867 so we'll be minimizing the number of node traversals required
2868 by chainon. */
2869 tree contents;
2870 timevar_push (TV_PARSE_STRUCT);
2871 contents = NULL_TREE;
2872 c_parser_consume_token (parser);
2873 /* Handle the Objective-C @defs construct,
2874 e.g. foo(sizeof(struct{ @defs(ClassName) }));. */
2875 if (c_parser_next_token_is_keyword (parser, RID_AT_DEFS))
2876 {
2877 tree name;
2878 gcc_assert (c_dialect_objc ());
2879 c_parser_consume_token (parser);
2880 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2881 goto end_at_defs;
2882 if (c_parser_next_token_is (parser, CPP_NAME)
2883 && c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME)
2884 {
2885 name = c_parser_peek_token (parser)->value;
2886 c_parser_consume_token (parser);
2887 }
2888 else
2889 {
2890 c_parser_error (parser, "expected class name");
2891 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2892 goto end_at_defs;
2893 }
2894 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2895 "expected %<)%>");
2896 contents = nreverse (objc_get_class_ivars (name));
2897 }
2898 end_at_defs:
2899 /* Parse the struct-declarations and semicolons. Problems with
2900 semicolons are diagnosed here; empty structures are diagnosed
2901 elsewhere. */
2902 while (true)
2903 {
2904 tree decls;
2905 /* Parse any stray semicolon. */
2906 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2907 {
2908 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
2909 "extra semicolon in struct or union specified");
2910 c_parser_consume_token (parser);
2911 continue;
2912 }
2913 /* Stop if at the end of the struct or union contents. */
2914 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2915 {
2916 c_parser_consume_token (parser);
2917 break;
2918 }
2919 /* Accept #pragmas at struct scope. */
2920 if (c_parser_next_token_is (parser, CPP_PRAGMA))
2921 {
2922 c_parser_pragma (parser, pragma_struct, NULL);
2923 continue;
2924 }
2925 /* Parse some comma-separated declarations, but not the
2926 trailing semicolon if any. */
2927 decls = c_parser_struct_declaration (parser);
2928 contents = chainon (decls, contents);
2929 /* If no semicolon follows, either we have a parse error or
2930 are at the end of the struct or union and should
2931 pedwarn. */
2932 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2933 c_parser_consume_token (parser);
2934 else
2935 {
2936 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2937 pedwarn (c_parser_peek_token (parser)->location, 0,
2938 "no semicolon at end of struct or union");
2939 else if (parser->error
2940 || !c_parser_next_token_starts_declspecs (parser))
2941 {
2942 c_parser_error (parser, "expected %<;%>");
2943 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2944 break;
2945 }
2946
2947 /* If we come here, we have already emitted an error
2948 for an expected `;', identifier or `(', and we also
2949 recovered already. Go on with the next field. */
2950 }
2951 }
2952 postfix_attrs = c_parser_attributes (parser);
2953 ret.spec = finish_struct (struct_loc, type, nreverse (contents),
2954 chainon (attrs, postfix_attrs), struct_info);
2955 ret.kind = ctsk_tagdef;
2956 ret.expr = NULL_TREE;
2957 ret.expr_const_operands = true;
2958 timevar_pop (TV_PARSE_STRUCT);
2959 return ret;
2960 }
2961 else if (!ident)
2962 {
2963 c_parser_error (parser, "expected %<{%>");
2964 ret.spec = error_mark_node;
2965 ret.kind = ctsk_tagref;
2966 ret.expr = NULL_TREE;
2967 ret.expr_const_operands = true;
2968 return ret;
2969 }
2970 ret = parser_xref_tag (ident_loc, code, ident);
2971 return ret;
2972 }
2973
2974 /* Parse a struct-declaration (C90 6.5.2.1, C99 6.7.2.1), *without*
2975 the trailing semicolon.
2976
2977 struct-declaration:
2978 specifier-qualifier-list struct-declarator-list
2979 static_assert-declaration-no-semi
2980
2981 specifier-qualifier-list:
2982 type-specifier specifier-qualifier-list[opt]
2983 type-qualifier specifier-qualifier-list[opt]
2984 attributes specifier-qualifier-list[opt]
2985
2986 struct-declarator-list:
2987 struct-declarator
2988 struct-declarator-list , attributes[opt] struct-declarator
2989
2990 struct-declarator:
2991 declarator attributes[opt]
2992 declarator[opt] : constant-expression attributes[opt]
2993
2994 GNU extensions:
2995
2996 struct-declaration:
2997 __extension__ struct-declaration
2998 specifier-qualifier-list
2999
3000 Unlike the ISO C syntax, semicolons are handled elsewhere. The use
3001 of attributes where shown is a GNU extension. In GNU C, we accept
3002 any expression without commas in the syntax (assignment
3003 expressions, not just conditional expressions); assignment
3004 expressions will be diagnosed as non-constant. */
3005
3006 static tree
3007 c_parser_struct_declaration (c_parser *parser)
3008 {
3009 struct c_declspecs *specs;
3010 tree prefix_attrs;
3011 tree all_prefix_attrs;
3012 tree decls;
3013 location_t decl_loc;
3014 if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
3015 {
3016 int ext;
3017 tree decl;
3018 ext = disable_extension_diagnostics ();
3019 c_parser_consume_token (parser);
3020 decl = c_parser_struct_declaration (parser);
3021 restore_extension_diagnostics (ext);
3022 return decl;
3023 }
3024 if (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
3025 {
3026 c_parser_static_assert_declaration_no_semi (parser);
3027 return NULL_TREE;
3028 }
3029 specs = build_null_declspecs ();
3030 decl_loc = c_parser_peek_token (parser)->location;
3031 /* Strictly by the standard, we shouldn't allow _Alignas here,
3032 but it appears to have been intended to allow it there, so
3033 we're keeping it as it is until WG14 reaches a conclusion
3034 of N1731.
3035 <http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1731.pdf> */
3036 c_parser_declspecs (parser, specs, false, true, true,
3037 true, false, cla_nonabstract_decl);
3038 if (parser->error)
3039 return NULL_TREE;
3040 if (!specs->declspecs_seen_p)
3041 {
3042 c_parser_error (parser, "expected specifier-qualifier-list");
3043 return NULL_TREE;
3044 }
3045 finish_declspecs (specs);
3046 if (c_parser_next_token_is (parser, CPP_SEMICOLON)
3047 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3048 {
3049 tree ret;
3050 if (specs->typespec_kind == ctsk_none)
3051 {
3052 pedwarn (decl_loc, OPT_Wpedantic,
3053 "ISO C forbids member declarations with no members");
3054 shadow_tag_warned (specs, pedantic);
3055 ret = NULL_TREE;
3056 }
3057 else
3058 {
3059 /* Support for unnamed structs or unions as members of
3060 structs or unions (which is [a] useful and [b] supports
3061 MS P-SDK). */
3062 tree attrs = NULL;
3063
3064 ret = grokfield (c_parser_peek_token (parser)->location,
3065 build_id_declarator (NULL_TREE), specs,
3066 NULL_TREE, &attrs);
3067 if (ret)
3068 decl_attributes (&ret, attrs, 0);
3069 }
3070 return ret;
3071 }
3072
3073 /* Provide better error recovery. Note that a type name here is valid,
3074 and will be treated as a field name. */
3075 if (specs->typespec_kind == ctsk_tagdef
3076 && TREE_CODE (specs->type) != ENUMERAL_TYPE
3077 && c_parser_next_token_starts_declspecs (parser)
3078 && !c_parser_next_token_is (parser, CPP_NAME))
3079 {
3080 c_parser_error (parser, "expected %<;%>, identifier or %<(%>");
3081 parser->error = false;
3082 return NULL_TREE;
3083 }
3084
3085 pending_xref_error ();
3086 prefix_attrs = specs->attrs;
3087 all_prefix_attrs = prefix_attrs;
3088 specs->attrs = NULL_TREE;
3089 decls = NULL_TREE;
3090 while (true)
3091 {
3092 /* Declaring one or more declarators or un-named bit-fields. */
3093 struct c_declarator *declarator;
3094 bool dummy = false;
3095 if (c_parser_next_token_is (parser, CPP_COLON))
3096 declarator = build_id_declarator (NULL_TREE);
3097 else
3098 declarator = c_parser_declarator (parser,
3099 specs->typespec_kind != ctsk_none,
3100 C_DTR_NORMAL, &dummy);
3101 if (declarator == NULL)
3102 {
3103 c_parser_skip_to_end_of_block_or_statement (parser);
3104 break;
3105 }
3106 if (c_parser_next_token_is (parser, CPP_COLON)
3107 || c_parser_next_token_is (parser, CPP_COMMA)
3108 || c_parser_next_token_is (parser, CPP_SEMICOLON)
3109 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE)
3110 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3111 {
3112 tree postfix_attrs = NULL_TREE;
3113 tree width = NULL_TREE;
3114 tree d;
3115 if (c_parser_next_token_is (parser, CPP_COLON))
3116 {
3117 c_parser_consume_token (parser);
3118 width = c_parser_expr_no_commas (parser, NULL).value;
3119 }
3120 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3121 postfix_attrs = c_parser_attributes (parser);
3122 d = grokfield (c_parser_peek_token (parser)->location,
3123 declarator, specs, width, &all_prefix_attrs);
3124 decl_attributes (&d, chainon (postfix_attrs,
3125 all_prefix_attrs), 0);
3126 DECL_CHAIN (d) = decls;
3127 decls = d;
3128 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3129 all_prefix_attrs = chainon (c_parser_attributes (parser),
3130 prefix_attrs);
3131 else
3132 all_prefix_attrs = prefix_attrs;
3133 if (c_parser_next_token_is (parser, CPP_COMMA))
3134 c_parser_consume_token (parser);
3135 else if (c_parser_next_token_is (parser, CPP_SEMICOLON)
3136 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3137 {
3138 /* Semicolon consumed in caller. */
3139 break;
3140 }
3141 else
3142 {
3143 c_parser_error (parser, "expected %<,%>, %<;%> or %<}%>");
3144 break;
3145 }
3146 }
3147 else
3148 {
3149 c_parser_error (parser,
3150 "expected %<:%>, %<,%>, %<;%>, %<}%> or "
3151 "%<__attribute__%>");
3152 break;
3153 }
3154 }
3155 return decls;
3156 }
3157
3158 /* Parse a typeof specifier (a GNU extension).
3159
3160 typeof-specifier:
3161 typeof ( expression )
3162 typeof ( type-name )
3163 */
3164
3165 static struct c_typespec
3166 c_parser_typeof_specifier (c_parser *parser)
3167 {
3168 struct c_typespec ret;
3169 ret.kind = ctsk_typeof;
3170 ret.spec = error_mark_node;
3171 ret.expr = NULL_TREE;
3172 ret.expr_const_operands = true;
3173 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TYPEOF));
3174 c_parser_consume_token (parser);
3175 c_inhibit_evaluation_warnings++;
3176 in_typeof++;
3177 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3178 {
3179 c_inhibit_evaluation_warnings--;
3180 in_typeof--;
3181 return ret;
3182 }
3183 if (c_parser_next_tokens_start_typename (parser, cla_prefer_id))
3184 {
3185 struct c_type_name *type = c_parser_type_name (parser);
3186 c_inhibit_evaluation_warnings--;
3187 in_typeof--;
3188 if (type != NULL)
3189 {
3190 ret.spec = groktypename (type, &ret.expr, &ret.expr_const_operands);
3191 pop_maybe_used (variably_modified_type_p (ret.spec, NULL_TREE));
3192 }
3193 }
3194 else
3195 {
3196 bool was_vm;
3197 location_t here = c_parser_peek_token (parser)->location;
3198 struct c_expr expr = c_parser_expression (parser);
3199 c_inhibit_evaluation_warnings--;
3200 in_typeof--;
3201 if (TREE_CODE (expr.value) == COMPONENT_REF
3202 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
3203 error_at (here, "%<typeof%> applied to a bit-field");
3204 mark_exp_read (expr.value);
3205 ret.spec = TREE_TYPE (expr.value);
3206 was_vm = variably_modified_type_p (ret.spec, NULL_TREE);
3207 /* This is returned with the type so that when the type is
3208 evaluated, this can be evaluated. */
3209 if (was_vm)
3210 ret.expr = c_fully_fold (expr.value, false, &ret.expr_const_operands);
3211 pop_maybe_used (was_vm);
3212 /* For use in macros such as those in <stdatomic.h>, remove all
3213 qualifiers from atomic types. (const can be an issue for more macros
3214 using typeof than just the <stdatomic.h> ones.) */
3215 if (ret.spec != error_mark_node && TYPE_ATOMIC (ret.spec))
3216 ret.spec = c_build_qualified_type (ret.spec, TYPE_UNQUALIFIED);
3217 }
3218 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
3219 return ret;
3220 }
3221
3222 /* Parse an alignment-specifier.
3223
3224 C11 6.7.5:
3225
3226 alignment-specifier:
3227 _Alignas ( type-name )
3228 _Alignas ( constant-expression )
3229 */
3230
3231 static tree
3232 c_parser_alignas_specifier (c_parser * parser)
3233 {
3234 tree ret = error_mark_node;
3235 location_t loc = c_parser_peek_token (parser)->location;
3236 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNAS));
3237 c_parser_consume_token (parser);
3238 if (flag_isoc99)
3239 pedwarn_c99 (loc, OPT_Wpedantic,
3240 "ISO C99 does not support %<_Alignas%>");
3241 else
3242 pedwarn_c99 (loc, OPT_Wpedantic,
3243 "ISO C90 does not support %<_Alignas%>");
3244 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3245 return ret;
3246 if (c_parser_next_tokens_start_typename (parser, cla_prefer_id))
3247 {
3248 struct c_type_name *type = c_parser_type_name (parser);
3249 if (type != NULL)
3250 ret = c_sizeof_or_alignof_type (loc, groktypename (type, NULL, NULL),
3251 false, true, 1);
3252 }
3253 else
3254 ret = c_parser_expr_no_commas (parser, NULL).value;
3255 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
3256 return ret;
3257 }
3258
3259 /* Parse a declarator, possibly an abstract declarator (C90 6.5.4,
3260 6.5.5, C99 6.7.5, 6.7.6). If TYPE_SEEN_P then a typedef name may
3261 be redeclared; otherwise it may not. KIND indicates which kind of
3262 declarator is wanted. Returns a valid declarator except in the
3263 case of a syntax error in which case NULL is returned. *SEEN_ID is
3264 set to true if an identifier being declared is seen; this is used
3265 to diagnose bad forms of abstract array declarators and to
3266 determine whether an identifier list is syntactically permitted.
3267
3268 declarator:
3269 pointer[opt] direct-declarator
3270
3271 direct-declarator:
3272 identifier
3273 ( attributes[opt] declarator )
3274 direct-declarator array-declarator
3275 direct-declarator ( parameter-type-list )
3276 direct-declarator ( identifier-list[opt] )
3277
3278 pointer:
3279 * type-qualifier-list[opt]
3280 * type-qualifier-list[opt] pointer
3281
3282 type-qualifier-list:
3283 type-qualifier
3284 attributes
3285 type-qualifier-list type-qualifier
3286 type-qualifier-list attributes
3287
3288 array-declarator:
3289 [ type-qualifier-list[opt] assignment-expression[opt] ]
3290 [ static type-qualifier-list[opt] assignment-expression ]
3291 [ type-qualifier-list static assignment-expression ]
3292 [ type-qualifier-list[opt] * ]
3293
3294 parameter-type-list:
3295 parameter-list
3296 parameter-list , ...
3297
3298 parameter-list:
3299 parameter-declaration
3300 parameter-list , parameter-declaration
3301
3302 parameter-declaration:
3303 declaration-specifiers declarator attributes[opt]
3304 declaration-specifiers abstract-declarator[opt] attributes[opt]
3305
3306 identifier-list:
3307 identifier
3308 identifier-list , identifier
3309
3310 abstract-declarator:
3311 pointer
3312 pointer[opt] direct-abstract-declarator
3313
3314 direct-abstract-declarator:
3315 ( attributes[opt] abstract-declarator )
3316 direct-abstract-declarator[opt] array-declarator
3317 direct-abstract-declarator[opt] ( parameter-type-list[opt] )
3318
3319 GNU extensions:
3320
3321 direct-declarator:
3322 direct-declarator ( parameter-forward-declarations
3323 parameter-type-list[opt] )
3324
3325 direct-abstract-declarator:
3326 direct-abstract-declarator[opt] ( parameter-forward-declarations
3327 parameter-type-list[opt] )
3328
3329 parameter-forward-declarations:
3330 parameter-list ;
3331 parameter-forward-declarations parameter-list ;
3332
3333 The uses of attributes shown above are GNU extensions.
3334
3335 Some forms of array declarator are not included in C99 in the
3336 syntax for abstract declarators; these are disallowed elsewhere.
3337 This may be a defect (DR#289).
3338
3339 This function also accepts an omitted abstract declarator as being
3340 an abstract declarator, although not part of the formal syntax. */
3341
3342 struct c_declarator *
3343 c_parser_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
3344 bool *seen_id)
3345 {
3346 /* Parse any initial pointer part. */
3347 if (c_parser_next_token_is (parser, CPP_MULT))
3348 {
3349 struct c_declspecs *quals_attrs = build_null_declspecs ();
3350 struct c_declarator *inner;
3351 c_parser_consume_token (parser);
3352 c_parser_declspecs (parser, quals_attrs, false, false, true,
3353 false, false, cla_prefer_id);
3354 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
3355 if (inner == NULL)
3356 return NULL;
3357 else
3358 return make_pointer_declarator (quals_attrs, inner);
3359 }
3360 /* Now we have a direct declarator, direct abstract declarator or
3361 nothing (which counts as a direct abstract declarator here). */
3362 return c_parser_direct_declarator (parser, type_seen_p, kind, seen_id);
3363 }
3364
3365 /* Parse a direct declarator or direct abstract declarator; arguments
3366 as c_parser_declarator. */
3367
3368 static struct c_declarator *
3369 c_parser_direct_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
3370 bool *seen_id)
3371 {
3372 /* The direct declarator must start with an identifier (possibly
3373 omitted) or a parenthesized declarator (possibly abstract). In
3374 an ordinary declarator, initial parentheses must start a
3375 parenthesized declarator. In an abstract declarator or parameter
3376 declarator, they could start a parenthesized declarator or a
3377 parameter list. To tell which, the open parenthesis and any
3378 following attributes must be read. If a declaration specifier
3379 follows, then it is a parameter list; if the specifier is a
3380 typedef name, there might be an ambiguity about redeclaring it,
3381 which is resolved in the direction of treating it as a typedef
3382 name. If a close parenthesis follows, it is also an empty
3383 parameter list, as the syntax does not permit empty abstract
3384 declarators. Otherwise, it is a parenthesized declarator (in
3385 which case the analysis may be repeated inside it, recursively).
3386
3387 ??? There is an ambiguity in a parameter declaration "int
3388 (__attribute__((foo)) x)", where x is not a typedef name: it
3389 could be an abstract declarator for a function, or declare x with
3390 parentheses. The proper resolution of this ambiguity needs
3391 documenting. At present we follow an accident of the old
3392 parser's implementation, whereby the first parameter must have
3393 some declaration specifiers other than just attributes. Thus as
3394 a parameter declaration it is treated as a parenthesized
3395 parameter named x, and as an abstract declarator it is
3396 rejected.
3397
3398 ??? Also following the old parser, attributes inside an empty
3399 parameter list are ignored, making it a list not yielding a
3400 prototype, rather than giving an error or making it have one
3401 parameter with implicit type int.
3402
3403 ??? Also following the old parser, typedef names may be
3404 redeclared in declarators, but not Objective-C class names. */
3405
3406 if (kind != C_DTR_ABSTRACT
3407 && c_parser_next_token_is (parser, CPP_NAME)
3408 && ((type_seen_p
3409 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
3410 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
3411 || c_parser_peek_token (parser)->id_kind == C_ID_ID))
3412 {
3413 struct c_declarator *inner
3414 = build_id_declarator (c_parser_peek_token (parser)->value);
3415 *seen_id = true;
3416 inner->id_loc = c_parser_peek_token (parser)->location;
3417 c_parser_consume_token (parser);
3418 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3419 }
3420
3421 if (kind != C_DTR_NORMAL
3422 && c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
3423 {
3424 struct c_declarator *inner = build_id_declarator (NULL_TREE);
3425 inner->id_loc = c_parser_peek_token (parser)->location;
3426 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3427 }
3428
3429 /* Either we are at the end of an abstract declarator, or we have
3430 parentheses. */
3431
3432 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
3433 {
3434 tree attrs;
3435 struct c_declarator *inner;
3436 c_parser_consume_token (parser);
3437 attrs = c_parser_attributes (parser);
3438 if (kind != C_DTR_NORMAL
3439 && (c_parser_next_token_starts_declspecs (parser)
3440 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN)))
3441 {
3442 struct c_arg_info *args
3443 = c_parser_parms_declarator (parser, kind == C_DTR_NORMAL,
3444 attrs);
3445 if (args == NULL)
3446 return NULL;
3447 else
3448 {
3449 inner
3450 = build_function_declarator (args,
3451 build_id_declarator (NULL_TREE));
3452 return c_parser_direct_declarator_inner (parser, *seen_id,
3453 inner);
3454 }
3455 }
3456 /* A parenthesized declarator. */
3457 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
3458 if (inner != NULL && attrs != NULL)
3459 inner = build_attrs_declarator (attrs, inner);
3460 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3461 {
3462 c_parser_consume_token (parser);
3463 if (inner == NULL)
3464 return NULL;
3465 else
3466 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3467 }
3468 else
3469 {
3470 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3471 "expected %<)%>");
3472 return NULL;
3473 }
3474 }
3475 else
3476 {
3477 if (kind == C_DTR_NORMAL)
3478 {
3479 c_parser_error (parser, "expected identifier or %<(%>");
3480 return NULL;
3481 }
3482 else
3483 return build_id_declarator (NULL_TREE);
3484 }
3485 }
3486
3487 /* Parse part of a direct declarator or direct abstract declarator,
3488 given that some (in INNER) has already been parsed; ID_PRESENT is
3489 true if an identifier is present, false for an abstract
3490 declarator. */
3491
3492 static struct c_declarator *
3493 c_parser_direct_declarator_inner (c_parser *parser, bool id_present,
3494 struct c_declarator *inner)
3495 {
3496 /* Parse a sequence of array declarators and parameter lists. */
3497 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
3498 {
3499 location_t brace_loc = c_parser_peek_token (parser)->location;
3500 struct c_declarator *declarator;
3501 struct c_declspecs *quals_attrs = build_null_declspecs ();
3502 bool static_seen;
3503 bool star_seen;
3504 struct c_expr dimen;
3505 dimen.value = NULL_TREE;
3506 dimen.original_code = ERROR_MARK;
3507 dimen.original_type = NULL_TREE;
3508 c_parser_consume_token (parser);
3509 c_parser_declspecs (parser, quals_attrs, false, false, true,
3510 false, false, cla_prefer_id);
3511 static_seen = c_parser_next_token_is_keyword (parser, RID_STATIC);
3512 if (static_seen)
3513 c_parser_consume_token (parser);
3514 if (static_seen && !quals_attrs->declspecs_seen_p)
3515 c_parser_declspecs (parser, quals_attrs, false, false, true,
3516 false, false, cla_prefer_id);
3517 if (!quals_attrs->declspecs_seen_p)
3518 quals_attrs = NULL;
3519 /* If "static" is present, there must be an array dimension.
3520 Otherwise, there may be a dimension, "*", or no
3521 dimension. */
3522 if (static_seen)
3523 {
3524 star_seen = false;
3525 dimen = c_parser_expr_no_commas (parser, NULL);
3526 }
3527 else
3528 {
3529 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3530 {
3531 dimen.value = NULL_TREE;
3532 star_seen = false;
3533 }
3534 else if (flag_cilkplus
3535 && c_parser_next_token_is (parser, CPP_COLON))
3536 {
3537 dimen.value = error_mark_node;
3538 star_seen = false;
3539 error_at (c_parser_peek_token (parser)->location,
3540 "array notations cannot be used in declaration");
3541 c_parser_consume_token (parser);
3542 }
3543 else if (c_parser_next_token_is (parser, CPP_MULT))
3544 {
3545 if (c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_SQUARE)
3546 {
3547 dimen.value = NULL_TREE;
3548 star_seen = true;
3549 c_parser_consume_token (parser);
3550 }
3551 else
3552 {
3553 star_seen = false;
3554 dimen = c_parser_expr_no_commas (parser, NULL);
3555 }
3556 }
3557 else
3558 {
3559 star_seen = false;
3560 dimen = c_parser_expr_no_commas (parser, NULL);
3561 }
3562 }
3563 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3564 c_parser_consume_token (parser);
3565 else if (flag_cilkplus
3566 && c_parser_next_token_is (parser, CPP_COLON))
3567 {
3568 error_at (c_parser_peek_token (parser)->location,
3569 "array notations cannot be used in declaration");
3570 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
3571 return NULL;
3572 }
3573 else
3574 {
3575 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3576 "expected %<]%>");
3577 return NULL;
3578 }
3579 if (dimen.value)
3580 dimen = convert_lvalue_to_rvalue (brace_loc, dimen, true, true);
3581 declarator = build_array_declarator (brace_loc, dimen.value, quals_attrs,
3582 static_seen, star_seen);
3583 if (declarator == NULL)
3584 return NULL;
3585 inner = set_array_declarator_inner (declarator, inner);
3586 return c_parser_direct_declarator_inner (parser, id_present, inner);
3587 }
3588 else if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
3589 {
3590 tree attrs;
3591 struct c_arg_info *args;
3592 c_parser_consume_token (parser);
3593 attrs = c_parser_attributes (parser);
3594 args = c_parser_parms_declarator (parser, id_present, attrs);
3595 if (args == NULL)
3596 return NULL;
3597 else
3598 {
3599 inner = build_function_declarator (args, inner);
3600 return c_parser_direct_declarator_inner (parser, id_present, inner);
3601 }
3602 }
3603 return inner;
3604 }
3605
3606 /* Parse a parameter list or identifier list, including the closing
3607 parenthesis but not the opening one. ATTRS are the attributes at
3608 the start of the list. ID_LIST_OK is true if an identifier list is
3609 acceptable; such a list must not have attributes at the start. */
3610
3611 static struct c_arg_info *
3612 c_parser_parms_declarator (c_parser *parser, bool id_list_ok, tree attrs)
3613 {
3614 push_scope ();
3615 declare_parm_level ();
3616 /* If the list starts with an identifier, it is an identifier list.
3617 Otherwise, it is either a prototype list or an empty list. */
3618 if (id_list_ok
3619 && !attrs
3620 && c_parser_next_token_is (parser, CPP_NAME)
3621 && c_parser_peek_token (parser)->id_kind == C_ID_ID
3622
3623 /* Look ahead to detect typos in type names. */
3624 && c_parser_peek_2nd_token (parser)->type != CPP_NAME
3625 && c_parser_peek_2nd_token (parser)->type != CPP_MULT
3626 && c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN
3627 && c_parser_peek_2nd_token (parser)->type != CPP_OPEN_SQUARE
3628 && c_parser_peek_2nd_token (parser)->type != CPP_KEYWORD)
3629 {
3630 tree list = NULL_TREE, *nextp = &list;
3631 while (c_parser_next_token_is (parser, CPP_NAME)
3632 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
3633 {
3634 *nextp = build_tree_list (NULL_TREE,
3635 c_parser_peek_token (parser)->value);
3636 nextp = & TREE_CHAIN (*nextp);
3637 c_parser_consume_token (parser);
3638 if (c_parser_next_token_is_not (parser, CPP_COMMA))
3639 break;
3640 c_parser_consume_token (parser);
3641 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3642 {
3643 c_parser_error (parser, "expected identifier");
3644 break;
3645 }
3646 }
3647 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3648 {
3649 struct c_arg_info *ret = build_arg_info ();
3650 ret->types = list;
3651 c_parser_consume_token (parser);
3652 pop_scope ();
3653 return ret;
3654 }
3655 else
3656 {
3657 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3658 "expected %<)%>");
3659 pop_scope ();
3660 return NULL;
3661 }
3662 }
3663 else
3664 {
3665 struct c_arg_info *ret = c_parser_parms_list_declarator (parser, attrs,
3666 NULL);
3667 pop_scope ();
3668 return ret;
3669 }
3670 }
3671
3672 /* Parse a parameter list (possibly empty), including the closing
3673 parenthesis but not the opening one. ATTRS are the attributes at
3674 the start of the list. EXPR is NULL or an expression that needs to
3675 be evaluated for the side effects of array size expressions in the
3676 parameters. */
3677
3678 static struct c_arg_info *
3679 c_parser_parms_list_declarator (c_parser *parser, tree attrs, tree expr)
3680 {
3681 bool bad_parm = false;
3682
3683 /* ??? Following the old parser, forward parameter declarations may
3684 use abstract declarators, and if no real parameter declarations
3685 follow the forward declarations then this is not diagnosed. Also
3686 note as above that attributes are ignored as the only contents of
3687 the parentheses, or as the only contents after forward
3688 declarations. */
3689 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3690 {
3691 struct c_arg_info *ret = build_arg_info ();
3692 c_parser_consume_token (parser);
3693 return ret;
3694 }
3695 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3696 {
3697 struct c_arg_info *ret = build_arg_info ();
3698
3699 if (flag_allow_parameterless_variadic_functions)
3700 {
3701 /* F (...) is allowed. */
3702 ret->types = NULL_TREE;
3703 }
3704 else
3705 {
3706 /* Suppress -Wold-style-definition for this case. */
3707 ret->types = error_mark_node;
3708 error_at (c_parser_peek_token (parser)->location,
3709 "ISO C requires a named argument before %<...%>");
3710 }
3711 c_parser_consume_token (parser);
3712 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3713 {
3714 c_parser_consume_token (parser);
3715 return ret;
3716 }
3717 else
3718 {
3719 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3720 "expected %<)%>");
3721 return NULL;
3722 }
3723 }
3724 /* Nonempty list of parameters, either terminated with semicolon
3725 (forward declarations; recurse) or with close parenthesis (normal
3726 function) or with ", ... )" (variadic function). */
3727 while (true)
3728 {
3729 /* Parse a parameter. */
3730 struct c_parm *parm = c_parser_parameter_declaration (parser, attrs);
3731 attrs = NULL_TREE;
3732 if (parm == NULL)
3733 bad_parm = true;
3734 else
3735 push_parm_decl (parm, &expr);
3736 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3737 {
3738 tree new_attrs;
3739 c_parser_consume_token (parser);
3740 mark_forward_parm_decls ();
3741 new_attrs = c_parser_attributes (parser);
3742 return c_parser_parms_list_declarator (parser, new_attrs, expr);
3743 }
3744 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3745 {
3746 c_parser_consume_token (parser);
3747 if (bad_parm)
3748 return NULL;
3749 else
3750 return get_parm_info (false, expr);
3751 }
3752 if (!c_parser_require (parser, CPP_COMMA,
3753 "expected %<;%>, %<,%> or %<)%>"))
3754 {
3755 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3756 return NULL;
3757 }
3758 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3759 {
3760 c_parser_consume_token (parser);
3761 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3762 {
3763 c_parser_consume_token (parser);
3764 if (bad_parm)
3765 return NULL;
3766 else
3767 return get_parm_info (true, expr);
3768 }
3769 else
3770 {
3771 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3772 "expected %<)%>");
3773 return NULL;
3774 }
3775 }
3776 }
3777 }
3778
3779 /* Parse a parameter declaration. ATTRS are the attributes at the
3780 start of the declaration if it is the first parameter. */
3781
3782 static struct c_parm *
3783 c_parser_parameter_declaration (c_parser *parser, tree attrs)
3784 {
3785 struct c_declspecs *specs;
3786 struct c_declarator *declarator;
3787 tree prefix_attrs;
3788 tree postfix_attrs = NULL_TREE;
3789 bool dummy = false;
3790
3791 /* Accept #pragmas between parameter declarations. */
3792 while (c_parser_next_token_is (parser, CPP_PRAGMA))
3793 c_parser_pragma (parser, pragma_param, NULL);
3794
3795 if (!c_parser_next_token_starts_declspecs (parser))
3796 {
3797 c_token *token = c_parser_peek_token (parser);
3798 if (parser->error)
3799 return NULL;
3800 c_parser_set_source_position_from_token (token);
3801 if (c_parser_next_tokens_start_typename (parser, cla_prefer_type))
3802 {
3803 const char *hint = lookup_name_fuzzy (token->value,
3804 FUZZY_LOOKUP_TYPENAME);
3805 if (hint)
3806 {
3807 gcc_rich_location richloc (token->location);
3808 richloc.add_fixit_replace (hint);
3809 error_at_rich_loc (&richloc,
3810 "unknown type name %qE; did you mean %qs?",
3811 token->value, hint);
3812 }
3813 else
3814 error_at (token->location, "unknown type name %qE", token->value);
3815 parser->error = true;
3816 }
3817 /* ??? In some Objective-C cases '...' isn't applicable so there
3818 should be a different message. */
3819 else
3820 c_parser_error (parser,
3821 "expected declaration specifiers or %<...%>");
3822 c_parser_skip_to_end_of_parameter (parser);
3823 return NULL;
3824 }
3825 specs = build_null_declspecs ();
3826 if (attrs)
3827 {
3828 declspecs_add_attrs (input_location, specs, attrs);
3829 attrs = NULL_TREE;
3830 }
3831 c_parser_declspecs (parser, specs, true, true, true, true, false,
3832 cla_nonabstract_decl);
3833 finish_declspecs (specs);
3834 pending_xref_error ();
3835 prefix_attrs = specs->attrs;
3836 specs->attrs = NULL_TREE;
3837 declarator = c_parser_declarator (parser,
3838 specs->typespec_kind != ctsk_none,
3839 C_DTR_PARM, &dummy);
3840 if (declarator == NULL)
3841 {
3842 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
3843 return NULL;
3844 }
3845 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3846 postfix_attrs = c_parser_attributes (parser);
3847 return build_c_parm (specs, chainon (postfix_attrs, prefix_attrs),
3848 declarator);
3849 }
3850
3851 /* Parse a string literal in an asm expression. It should not be
3852 translated, and wide string literals are an error although
3853 permitted by the syntax. This is a GNU extension.
3854
3855 asm-string-literal:
3856 string-literal
3857
3858 ??? At present, following the old parser, the caller needs to have
3859 set lex_untranslated_string to 1. It would be better to follow the
3860 C++ parser rather than using this kludge. */
3861
3862 static tree
3863 c_parser_asm_string_literal (c_parser *parser)
3864 {
3865 tree str;
3866 int save_flag = warn_overlength_strings;
3867 warn_overlength_strings = 0;
3868 if (c_parser_next_token_is (parser, CPP_STRING))
3869 {
3870 str = c_parser_peek_token (parser)->value;
3871 c_parser_consume_token (parser);
3872 }
3873 else if (c_parser_next_token_is (parser, CPP_WSTRING))
3874 {
3875 error_at (c_parser_peek_token (parser)->location,
3876 "wide string literal in %<asm%>");
3877 str = build_string (1, "");
3878 c_parser_consume_token (parser);
3879 }
3880 else
3881 {
3882 c_parser_error (parser, "expected string literal");
3883 str = NULL_TREE;
3884 }
3885 warn_overlength_strings = save_flag;
3886 return str;
3887 }
3888
3889 /* Parse a simple asm expression. This is used in restricted
3890 contexts, where a full expression with inputs and outputs does not
3891 make sense. This is a GNU extension.
3892
3893 simple-asm-expr:
3894 asm ( asm-string-literal )
3895 */
3896
3897 static tree
3898 c_parser_simple_asm_expr (c_parser *parser)
3899 {
3900 tree str;
3901 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
3902 /* ??? Follow the C++ parser rather than using the
3903 lex_untranslated_string kludge. */
3904 parser->lex_untranslated_string = true;
3905 c_parser_consume_token (parser);
3906 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3907 {
3908 parser->lex_untranslated_string = false;
3909 return NULL_TREE;
3910 }
3911 str = c_parser_asm_string_literal (parser);
3912 parser->lex_untranslated_string = false;
3913 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
3914 {
3915 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3916 return NULL_TREE;
3917 }
3918 return str;
3919 }
3920
3921 static tree
3922 c_parser_attribute_any_word (c_parser *parser)
3923 {
3924 tree attr_name = NULL_TREE;
3925
3926 if (c_parser_next_token_is (parser, CPP_KEYWORD))
3927 {
3928 /* ??? See comment above about what keywords are accepted here. */
3929 bool ok;
3930 switch (c_parser_peek_token (parser)->keyword)
3931 {
3932 case RID_STATIC:
3933 case RID_UNSIGNED:
3934 case RID_LONG:
3935 case RID_CONST:
3936 case RID_EXTERN:
3937 case RID_REGISTER:
3938 case RID_TYPEDEF:
3939 case RID_SHORT:
3940 case RID_INLINE:
3941 case RID_NORETURN:
3942 case RID_VOLATILE:
3943 case RID_SIGNED:
3944 case RID_AUTO:
3945 case RID_RESTRICT:
3946 case RID_COMPLEX:
3947 case RID_THREAD:
3948 case RID_INT:
3949 case RID_CHAR:
3950 case RID_FLOAT:
3951 case RID_DOUBLE:
3952 case RID_VOID:
3953 case RID_DFLOAT32:
3954 case RID_DFLOAT64:
3955 case RID_DFLOAT128:
3956 CASE_RID_FLOATN_NX:
3957 case RID_BOOL:
3958 case RID_FRACT:
3959 case RID_ACCUM:
3960 case RID_SAT:
3961 case RID_TRANSACTION_ATOMIC:
3962 case RID_TRANSACTION_CANCEL:
3963 case RID_ATOMIC:
3964 case RID_AUTO_TYPE:
3965 case RID_INT_N_0:
3966 case RID_INT_N_1:
3967 case RID_INT_N_2:
3968 case RID_INT_N_3:
3969 ok = true;
3970 break;
3971 default:
3972 ok = false;
3973 break;
3974 }
3975 if (!ok)
3976 return NULL_TREE;
3977
3978 /* Accept __attribute__((__const)) as __attribute__((const)) etc. */
3979 attr_name = ridpointers[(int) c_parser_peek_token (parser)->keyword];
3980 }
3981 else if (c_parser_next_token_is (parser, CPP_NAME))
3982 attr_name = c_parser_peek_token (parser)->value;
3983
3984 return attr_name;
3985 }
3986
3987 #define CILK_SIMD_FN_CLAUSE_MASK \
3988 ((OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_VECTORLENGTH) \
3989 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_LINEAR) \
3990 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_UNIFORM) \
3991 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_MASK) \
3992 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_NOMASK))
3993
3994 /* Parses the vector attribute of SIMD enabled functions in Cilk Plus.
3995 VEC_TOKEN is the "vector" token that is replaced with "simd" and
3996 pushed into the token list.
3997 Syntax:
3998 vector
3999 vector (<vector attributes>). */
4000
4001 static void
4002 c_parser_cilk_simd_fn_vector_attrs (c_parser *parser, c_token vec_token)
4003 {
4004 gcc_assert (is_cilkplus_vector_p (vec_token.value));
4005
4006 int paren_scope = 0;
4007 vec_safe_push (parser->cilk_simd_fn_tokens, vec_token);
4008 /* Consume the "vector" token. */
4009 c_parser_consume_token (parser);
4010
4011 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
4012 {
4013 c_parser_consume_token (parser);
4014 paren_scope++;
4015 }
4016 while (paren_scope > 0)
4017 {
4018 c_token *token = c_parser_peek_token (parser);
4019 if (token->type == CPP_OPEN_PAREN)
4020 paren_scope++;
4021 else if (token->type == CPP_CLOSE_PAREN)
4022 paren_scope--;
4023 /* Do not push the last ')' since we are not pushing the '('. */
4024 if (!(token->type == CPP_CLOSE_PAREN && paren_scope == 0))
4025 vec_safe_push (parser->cilk_simd_fn_tokens, *token);
4026 c_parser_consume_token (parser);
4027 }
4028
4029 /* Since we are converting an attribute to a pragma, we need to end the
4030 attribute with PRAGMA_EOL. */
4031 c_token eol_token;
4032 memset (&eol_token, 0, sizeof (eol_token));
4033 eol_token.type = CPP_PRAGMA_EOL;
4034 vec_safe_push (parser->cilk_simd_fn_tokens, eol_token);
4035 }
4036
4037 /* Add 2 CPP_EOF at the end of PARSER->ELEM_FN_TOKENS vector. */
4038
4039 static void
4040 c_finish_cilk_simd_fn_tokens (c_parser *parser)
4041 {
4042 c_token last_token = parser->cilk_simd_fn_tokens->last ();
4043
4044 /* c_parser_attributes is called in several places, so if these EOF
4045 tokens are already inserted, then don't do them again. */
4046 if (last_token.type == CPP_EOF)
4047 return;
4048
4049 /* Two CPP_EOF token are added as a safety net since the normal C
4050 front-end has two token look-ahead. */
4051 c_token eof_token;
4052 eof_token.type = CPP_EOF;
4053 vec_safe_push (parser->cilk_simd_fn_tokens, eof_token);
4054 vec_safe_push (parser->cilk_simd_fn_tokens, eof_token);
4055 }
4056
4057 /* Parse (possibly empty) attributes. This is a GNU extension.
4058
4059 attributes:
4060 empty
4061 attributes attribute
4062
4063 attribute:
4064 __attribute__ ( ( attribute-list ) )
4065
4066 attribute-list:
4067 attrib
4068 attribute_list , attrib
4069
4070 attrib:
4071 empty
4072 any-word
4073 any-word ( identifier )
4074 any-word ( identifier , nonempty-expr-list )
4075 any-word ( expr-list )
4076
4077 where the "identifier" must not be declared as a type, and
4078 "any-word" may be any identifier (including one declared as a
4079 type), a reserved word storage class specifier, type specifier or
4080 type qualifier. ??? This still leaves out most reserved keywords
4081 (following the old parser), shouldn't we include them, and why not
4082 allow identifiers declared as types to start the arguments? */
4083
4084 static tree
4085 c_parser_attributes (c_parser *parser)
4086 {
4087 tree attrs = NULL_TREE;
4088 while (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
4089 {
4090 /* ??? Follow the C++ parser rather than using the
4091 lex_untranslated_string kludge. */
4092 parser->lex_untranslated_string = true;
4093 /* Consume the `__attribute__' keyword. */
4094 c_parser_consume_token (parser);
4095 /* Look for the two `(' tokens. */
4096 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4097 {
4098 parser->lex_untranslated_string = false;
4099 return attrs;
4100 }
4101 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4102 {
4103 parser->lex_untranslated_string = false;
4104 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4105 return attrs;
4106 }
4107 /* Parse the attribute list. */
4108 while (c_parser_next_token_is (parser, CPP_COMMA)
4109 || c_parser_next_token_is (parser, CPP_NAME)
4110 || c_parser_next_token_is (parser, CPP_KEYWORD))
4111 {
4112 tree attr, attr_name, attr_args;
4113 vec<tree, va_gc> *expr_list;
4114 if (c_parser_next_token_is (parser, CPP_COMMA))
4115 {
4116 c_parser_consume_token (parser);
4117 continue;
4118 }
4119
4120 attr_name = c_parser_attribute_any_word (parser);
4121 if (attr_name == NULL)
4122 break;
4123 if (is_cilkplus_vector_p (attr_name))
4124 {
4125 c_token *v_token = c_parser_peek_token (parser);
4126 c_parser_cilk_simd_fn_vector_attrs (parser, *v_token);
4127 /* If the next token isn't a comma, we're done. */
4128 if (!c_parser_next_token_is (parser, CPP_COMMA))
4129 break;
4130 continue;
4131 }
4132 c_parser_consume_token (parser);
4133 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
4134 {
4135 attr = build_tree_list (attr_name, NULL_TREE);
4136 /* Add this attribute to the list. */
4137 attrs = chainon (attrs, attr);
4138 /* If the next token isn't a comma, we're done. */
4139 if (!c_parser_next_token_is (parser, CPP_COMMA))
4140 break;
4141 continue;
4142 }
4143 c_parser_consume_token (parser);
4144 /* Parse the attribute contents. If they start with an
4145 identifier which is followed by a comma or close
4146 parenthesis, then the arguments start with that
4147 identifier; otherwise they are an expression list.
4148 In objective-c the identifier may be a classname. */
4149 if (c_parser_next_token_is (parser, CPP_NAME)
4150 && (c_parser_peek_token (parser)->id_kind == C_ID_ID
4151 || (c_dialect_objc ()
4152 && c_parser_peek_token (parser)->id_kind
4153 == C_ID_CLASSNAME))
4154 && ((c_parser_peek_2nd_token (parser)->type == CPP_COMMA)
4155 || (c_parser_peek_2nd_token (parser)->type
4156 == CPP_CLOSE_PAREN))
4157 && (attribute_takes_identifier_p (attr_name)
4158 || (c_dialect_objc ()
4159 && c_parser_peek_token (parser)->id_kind
4160 == C_ID_CLASSNAME)))
4161 {
4162 tree arg1 = c_parser_peek_token (parser)->value;
4163 c_parser_consume_token (parser);
4164 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4165 attr_args = build_tree_list (NULL_TREE, arg1);
4166 else
4167 {
4168 tree tree_list;
4169 c_parser_consume_token (parser);
4170 expr_list = c_parser_expr_list (parser, false, true,
4171 NULL, NULL, NULL, NULL);
4172 tree_list = build_tree_list_vec (expr_list);
4173 attr_args = tree_cons (NULL_TREE, arg1, tree_list);
4174 release_tree_vector (expr_list);
4175 }
4176 }
4177 else
4178 {
4179 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4180 attr_args = NULL_TREE;
4181 else
4182 {
4183 expr_list = c_parser_expr_list (parser, false, true,
4184 NULL, NULL, NULL, NULL);
4185 attr_args = build_tree_list_vec (expr_list);
4186 release_tree_vector (expr_list);
4187 }
4188 }
4189 attr = build_tree_list (attr_name, attr_args);
4190 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4191 c_parser_consume_token (parser);
4192 else
4193 {
4194 parser->lex_untranslated_string = false;
4195 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4196 "expected %<)%>");
4197 return attrs;
4198 }
4199 /* Add this attribute to the list. */
4200 attrs = chainon (attrs, attr);
4201 /* If the next token isn't a comma, we're done. */
4202 if (!c_parser_next_token_is (parser, CPP_COMMA))
4203 break;
4204 }
4205 /* Look for the two `)' tokens. */
4206 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4207 c_parser_consume_token (parser);
4208 else
4209 {
4210 parser->lex_untranslated_string = false;
4211 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4212 "expected %<)%>");
4213 return attrs;
4214 }
4215 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4216 c_parser_consume_token (parser);
4217 else
4218 {
4219 parser->lex_untranslated_string = false;
4220 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4221 "expected %<)%>");
4222 return attrs;
4223 }
4224 parser->lex_untranslated_string = false;
4225 }
4226
4227 if (flag_cilkplus && !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
4228 c_finish_cilk_simd_fn_tokens (parser);
4229 return attrs;
4230 }
4231
4232 /* Parse a type name (C90 6.5.5, C99 6.7.6).
4233
4234 type-name:
4235 specifier-qualifier-list abstract-declarator[opt]
4236 */
4237
4238 struct c_type_name *
4239 c_parser_type_name (c_parser *parser)
4240 {
4241 struct c_declspecs *specs = build_null_declspecs ();
4242 struct c_declarator *declarator;
4243 struct c_type_name *ret;
4244 bool dummy = false;
4245 c_parser_declspecs (parser, specs, false, true, true, false, false,
4246 cla_prefer_type);
4247 if (!specs->declspecs_seen_p)
4248 {
4249 c_parser_error (parser, "expected specifier-qualifier-list");
4250 return NULL;
4251 }
4252 if (specs->type != error_mark_node)
4253 {
4254 pending_xref_error ();
4255 finish_declspecs (specs);
4256 }
4257 declarator = c_parser_declarator (parser,
4258 specs->typespec_kind != ctsk_none,
4259 C_DTR_ABSTRACT, &dummy);
4260 if (declarator == NULL)
4261 return NULL;
4262 ret = XOBNEW (&parser_obstack, struct c_type_name);
4263 ret->specs = specs;
4264 ret->declarator = declarator;
4265 return ret;
4266 }
4267
4268 /* Parse an initializer (C90 6.5.7, C99 6.7.8).
4269
4270 initializer:
4271 assignment-expression
4272 { initializer-list }
4273 { initializer-list , }
4274
4275 initializer-list:
4276 designation[opt] initializer
4277 initializer-list , designation[opt] initializer
4278
4279 designation:
4280 designator-list =
4281
4282 designator-list:
4283 designator
4284 designator-list designator
4285
4286 designator:
4287 array-designator
4288 . identifier
4289
4290 array-designator:
4291 [ constant-expression ]
4292
4293 GNU extensions:
4294
4295 initializer:
4296 { }
4297
4298 designation:
4299 array-designator
4300 identifier :
4301
4302 array-designator:
4303 [ constant-expression ... constant-expression ]
4304
4305 Any expression without commas is accepted in the syntax for the
4306 constant-expressions, with non-constant expressions rejected later.
4307
4308 This function is only used for top-level initializers; for nested
4309 ones, see c_parser_initval. */
4310
4311 static struct c_expr
4312 c_parser_initializer (c_parser *parser)
4313 {
4314 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
4315 return c_parser_braced_init (parser, NULL_TREE, false, NULL);
4316 else
4317 {
4318 struct c_expr ret;
4319 location_t loc = c_parser_peek_token (parser)->location;
4320 ret = c_parser_expr_no_commas (parser, NULL);
4321 if (TREE_CODE (ret.value) != STRING_CST
4322 && TREE_CODE (ret.value) != COMPOUND_LITERAL_EXPR)
4323 ret = convert_lvalue_to_rvalue (loc, ret, true, true);
4324 return ret;
4325 }
4326 }
4327
4328 /* Parse a braced initializer list. TYPE is the type specified for a
4329 compound literal, and NULL_TREE for other initializers and for
4330 nested braced lists. NESTED_P is true for nested braced lists,
4331 false for the list of a compound literal or the list that is the
4332 top-level initializer in a declaration. */
4333
4334 static struct c_expr
4335 c_parser_braced_init (c_parser *parser, tree type, bool nested_p,
4336 struct obstack *outer_obstack)
4337 {
4338 struct c_expr ret;
4339 struct obstack braced_init_obstack;
4340 location_t brace_loc = c_parser_peek_token (parser)->location;
4341 gcc_obstack_init (&braced_init_obstack);
4342 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
4343 c_parser_consume_token (parser);
4344 if (nested_p)
4345 {
4346 finish_implicit_inits (brace_loc, outer_obstack);
4347 push_init_level (brace_loc, 0, &braced_init_obstack);
4348 }
4349 else
4350 really_start_incremental_init (type);
4351 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4352 {
4353 pedwarn (brace_loc, OPT_Wpedantic, "ISO C forbids empty initializer braces");
4354 }
4355 else
4356 {
4357 /* Parse a non-empty initializer list, possibly with a trailing
4358 comma. */
4359 while (true)
4360 {
4361 c_parser_initelt (parser, &braced_init_obstack);
4362 if (parser->error)
4363 break;
4364 if (c_parser_next_token_is (parser, CPP_COMMA))
4365 c_parser_consume_token (parser);
4366 else
4367 break;
4368 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4369 break;
4370 }
4371 }
4372 c_token *next_tok = c_parser_peek_token (parser);
4373 if (next_tok->type != CPP_CLOSE_BRACE)
4374 {
4375 ret.value = error_mark_node;
4376 ret.original_code = ERROR_MARK;
4377 ret.original_type = NULL;
4378 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, "expected %<}%>");
4379 pop_init_level (brace_loc, 0, &braced_init_obstack);
4380 obstack_free (&braced_init_obstack, NULL);
4381 return ret;
4382 }
4383 location_t close_loc = next_tok->location;
4384 c_parser_consume_token (parser);
4385 ret = pop_init_level (brace_loc, 0, &braced_init_obstack);
4386 obstack_free (&braced_init_obstack, NULL);
4387 set_c_expr_source_range (&ret, brace_loc, close_loc);
4388 return ret;
4389 }
4390
4391 /* Parse a nested initializer, including designators. */
4392
4393 static void
4394 c_parser_initelt (c_parser *parser, struct obstack * braced_init_obstack)
4395 {
4396 /* Parse any designator or designator list. A single array
4397 designator may have the subsequent "=" omitted in GNU C, but a
4398 longer list or a structure member designator may not. */
4399 if (c_parser_next_token_is (parser, CPP_NAME)
4400 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
4401 {
4402 /* Old-style structure member designator. */
4403 set_init_label (c_parser_peek_token (parser)->location,
4404 c_parser_peek_token (parser)->value,
4405 c_parser_peek_token (parser)->location,
4406 braced_init_obstack);
4407 /* Use the colon as the error location. */
4408 pedwarn (c_parser_peek_2nd_token (parser)->location, OPT_Wpedantic,
4409 "obsolete use of designated initializer with %<:%>");
4410 c_parser_consume_token (parser);
4411 c_parser_consume_token (parser);
4412 }
4413 else
4414 {
4415 /* des_seen is 0 if there have been no designators, 1 if there
4416 has been a single array designator and 2 otherwise. */
4417 int des_seen = 0;
4418 /* Location of a designator. */
4419 location_t des_loc = UNKNOWN_LOCATION; /* Quiet warning. */
4420 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE)
4421 || c_parser_next_token_is (parser, CPP_DOT))
4422 {
4423 int des_prev = des_seen;
4424 if (!des_seen)
4425 des_loc = c_parser_peek_token (parser)->location;
4426 if (des_seen < 2)
4427 des_seen++;
4428 if (c_parser_next_token_is (parser, CPP_DOT))
4429 {
4430 des_seen = 2;
4431 c_parser_consume_token (parser);
4432 if (c_parser_next_token_is (parser, CPP_NAME))
4433 {
4434 set_init_label (des_loc, c_parser_peek_token (parser)->value,
4435 c_parser_peek_token (parser)->location,
4436 braced_init_obstack);
4437 c_parser_consume_token (parser);
4438 }
4439 else
4440 {
4441 struct c_expr init;
4442 init.value = error_mark_node;
4443 init.original_code = ERROR_MARK;
4444 init.original_type = NULL;
4445 c_parser_error (parser, "expected identifier");
4446 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
4447 process_init_element (input_location, init, false,
4448 braced_init_obstack);
4449 return;
4450 }
4451 }
4452 else
4453 {
4454 tree first, second;
4455 location_t ellipsis_loc = UNKNOWN_LOCATION; /* Quiet warning. */
4456 location_t array_index_loc = UNKNOWN_LOCATION;
4457 /* ??? Following the old parser, [ objc-receiver
4458 objc-message-args ] is accepted as an initializer,
4459 being distinguished from a designator by what follows
4460 the first assignment expression inside the square
4461 brackets, but after a first array designator a
4462 subsequent square bracket is for Objective-C taken to
4463 start an expression, using the obsolete form of
4464 designated initializer without '=', rather than
4465 possibly being a second level of designation: in LALR
4466 terms, the '[' is shifted rather than reducing
4467 designator to designator-list. */
4468 if (des_prev == 1 && c_dialect_objc ())
4469 {
4470 des_seen = des_prev;
4471 break;
4472 }
4473 if (des_prev == 0 && c_dialect_objc ())
4474 {
4475 /* This might be an array designator or an
4476 Objective-C message expression. If the former,
4477 continue parsing here; if the latter, parse the
4478 remainder of the initializer given the starting
4479 primary-expression. ??? It might make sense to
4480 distinguish when des_prev == 1 as well; see
4481 previous comment. */
4482 tree rec, args;
4483 struct c_expr mexpr;
4484 c_parser_consume_token (parser);
4485 if (c_parser_peek_token (parser)->type == CPP_NAME
4486 && ((c_parser_peek_token (parser)->id_kind
4487 == C_ID_TYPENAME)
4488 || (c_parser_peek_token (parser)->id_kind
4489 == C_ID_CLASSNAME)))
4490 {
4491 /* Type name receiver. */
4492 tree id = c_parser_peek_token (parser)->value;
4493 c_parser_consume_token (parser);
4494 rec = objc_get_class_reference (id);
4495 goto parse_message_args;
4496 }
4497 first = c_parser_expr_no_commas (parser, NULL).value;
4498 mark_exp_read (first);
4499 if (c_parser_next_token_is (parser, CPP_ELLIPSIS)
4500 || c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
4501 goto array_desig_after_first;
4502 /* Expression receiver. So far only one part
4503 without commas has been parsed; there might be
4504 more of the expression. */
4505 rec = first;
4506 while (c_parser_next_token_is (parser, CPP_COMMA))
4507 {
4508 struct c_expr next;
4509 location_t comma_loc, exp_loc;
4510 comma_loc = c_parser_peek_token (parser)->location;
4511 c_parser_consume_token (parser);
4512 exp_loc = c_parser_peek_token (parser)->location;
4513 next = c_parser_expr_no_commas (parser, NULL);
4514 next = convert_lvalue_to_rvalue (exp_loc, next,
4515 true, true);
4516 rec = build_compound_expr (comma_loc, rec, next.value);
4517 }
4518 parse_message_args:
4519 /* Now parse the objc-message-args. */
4520 args = c_parser_objc_message_args (parser);
4521 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
4522 "expected %<]%>");
4523 mexpr.value
4524 = objc_build_message_expr (rec, args);
4525 mexpr.original_code = ERROR_MARK;
4526 mexpr.original_type = NULL;
4527 /* Now parse and process the remainder of the
4528 initializer, starting with this message
4529 expression as a primary-expression. */
4530 c_parser_initval (parser, &mexpr, braced_init_obstack);
4531 return;
4532 }
4533 c_parser_consume_token (parser);
4534 array_index_loc = c_parser_peek_token (parser)->location;
4535 first = c_parser_expr_no_commas (parser, NULL).value;
4536 mark_exp_read (first);
4537 array_desig_after_first:
4538 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
4539 {
4540 ellipsis_loc = c_parser_peek_token (parser)->location;
4541 c_parser_consume_token (parser);
4542 second = c_parser_expr_no_commas (parser, NULL).value;
4543 mark_exp_read (second);
4544 }
4545 else
4546 second = NULL_TREE;
4547 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
4548 {
4549 c_parser_consume_token (parser);
4550 set_init_index (array_index_loc, first, second,
4551 braced_init_obstack);
4552 if (second)
4553 pedwarn (ellipsis_loc, OPT_Wpedantic,
4554 "ISO C forbids specifying range of elements to initialize");
4555 }
4556 else
4557 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
4558 "expected %<]%>");
4559 }
4560 }
4561 if (des_seen >= 1)
4562 {
4563 if (c_parser_next_token_is (parser, CPP_EQ))
4564 {
4565 pedwarn_c90 (des_loc, OPT_Wpedantic,
4566 "ISO C90 forbids specifying subobject "
4567 "to initialize");
4568 c_parser_consume_token (parser);
4569 }
4570 else
4571 {
4572 if (des_seen == 1)
4573 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
4574 "obsolete use of designated initializer without %<=%>");
4575 else
4576 {
4577 struct c_expr init;
4578 init.value = error_mark_node;
4579 init.original_code = ERROR_MARK;
4580 init.original_type = NULL;
4581 c_parser_error (parser, "expected %<=%>");
4582 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
4583 process_init_element (input_location, init, false,
4584 braced_init_obstack);
4585 return;
4586 }
4587 }
4588 }
4589 }
4590 c_parser_initval (parser, NULL, braced_init_obstack);
4591 }
4592
4593 /* Parse a nested initializer; as c_parser_initializer but parses
4594 initializers within braced lists, after any designators have been
4595 applied. If AFTER is not NULL then it is an Objective-C message
4596 expression which is the primary-expression starting the
4597 initializer. */
4598
4599 static void
4600 c_parser_initval (c_parser *parser, struct c_expr *after,
4601 struct obstack * braced_init_obstack)
4602 {
4603 struct c_expr init;
4604 gcc_assert (!after || c_dialect_objc ());
4605 location_t loc = c_parser_peek_token (parser)->location;
4606
4607 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE) && !after)
4608 init = c_parser_braced_init (parser, NULL_TREE, true,
4609 braced_init_obstack);
4610 else
4611 {
4612 init = c_parser_expr_no_commas (parser, after);
4613 if (init.value != NULL_TREE
4614 && TREE_CODE (init.value) != STRING_CST
4615 && TREE_CODE (init.value) != COMPOUND_LITERAL_EXPR)
4616 init = convert_lvalue_to_rvalue (loc, init, true, true);
4617 }
4618 process_init_element (loc, init, false, braced_init_obstack);
4619 }
4620
4621 /* Parse a compound statement (possibly a function body) (C90 6.6.2,
4622 C99 6.8.2).
4623
4624 compound-statement:
4625 { block-item-list[opt] }
4626 { label-declarations block-item-list }
4627
4628 block-item-list:
4629 block-item
4630 block-item-list block-item
4631
4632 block-item:
4633 nested-declaration
4634 statement
4635
4636 nested-declaration:
4637 declaration
4638
4639 GNU extensions:
4640
4641 compound-statement:
4642 { label-declarations block-item-list }
4643
4644 nested-declaration:
4645 __extension__ nested-declaration
4646 nested-function-definition
4647
4648 label-declarations:
4649 label-declaration
4650 label-declarations label-declaration
4651
4652 label-declaration:
4653 __label__ identifier-list ;
4654
4655 Allowing the mixing of declarations and code is new in C99. The
4656 GNU syntax also permits (not shown above) labels at the end of
4657 compound statements, which yield an error. We don't allow labels
4658 on declarations; this might seem like a natural extension, but
4659 there would be a conflict between attributes on the label and
4660 prefix attributes on the declaration. ??? The syntax follows the
4661 old parser in requiring something after label declarations.
4662 Although they are erroneous if the labels declared aren't defined,
4663 is it useful for the syntax to be this way?
4664
4665 OpenACC:
4666
4667 block-item:
4668 openacc-directive
4669
4670 openacc-directive:
4671 update-directive
4672
4673 OpenMP:
4674
4675 block-item:
4676 openmp-directive
4677
4678 openmp-directive:
4679 barrier-directive
4680 flush-directive
4681 taskwait-directive
4682 taskyield-directive
4683 cancel-directive
4684 cancellation-point-directive */
4685
4686 static tree
4687 c_parser_compound_statement (c_parser *parser)
4688 {
4689 tree stmt;
4690 location_t brace_loc;
4691 brace_loc = c_parser_peek_token (parser)->location;
4692 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
4693 {
4694 /* Ensure a scope is entered and left anyway to avoid confusion
4695 if we have just prepared to enter a function body. */
4696 stmt = c_begin_compound_stmt (true);
4697 c_end_compound_stmt (brace_loc, stmt, true);
4698 return error_mark_node;
4699 }
4700 stmt = c_begin_compound_stmt (true);
4701 c_parser_compound_statement_nostart (parser);
4702
4703 /* If the compound stmt contains array notations, then we expand them. */
4704 if (flag_cilkplus && contains_array_notation_expr (stmt))
4705 stmt = expand_array_notation_exprs (stmt);
4706 return c_end_compound_stmt (brace_loc, stmt, true);
4707 }
4708
4709 /* Parse a compound statement except for the opening brace. This is
4710 used for parsing both compound statements and statement expressions
4711 (which follow different paths to handling the opening). */
4712
4713 static void
4714 c_parser_compound_statement_nostart (c_parser *parser)
4715 {
4716 bool last_stmt = false;
4717 bool last_label = false;
4718 bool save_valid_for_pragma = valid_location_for_stdc_pragma_p ();
4719 location_t label_loc = UNKNOWN_LOCATION; /* Quiet warning. */
4720 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4721 {
4722 c_parser_consume_token (parser);
4723 return;
4724 }
4725 mark_valid_location_for_stdc_pragma (true);
4726 if (c_parser_next_token_is_keyword (parser, RID_LABEL))
4727 {
4728 /* Read zero or more forward-declarations for labels that nested
4729 functions can jump to. */
4730 mark_valid_location_for_stdc_pragma (false);
4731 while (c_parser_next_token_is_keyword (parser, RID_LABEL))
4732 {
4733 label_loc = c_parser_peek_token (parser)->location;
4734 c_parser_consume_token (parser);
4735 /* Any identifiers, including those declared as type names,
4736 are OK here. */
4737 while (true)
4738 {
4739 tree label;
4740 if (c_parser_next_token_is_not (parser, CPP_NAME))
4741 {
4742 c_parser_error (parser, "expected identifier");
4743 break;
4744 }
4745 label
4746 = declare_label (c_parser_peek_token (parser)->value);
4747 C_DECLARED_LABEL_FLAG (label) = 1;
4748 add_stmt (build_stmt (label_loc, DECL_EXPR, label));
4749 c_parser_consume_token (parser);
4750 if (c_parser_next_token_is (parser, CPP_COMMA))
4751 c_parser_consume_token (parser);
4752 else
4753 break;
4754 }
4755 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4756 }
4757 pedwarn (label_loc, OPT_Wpedantic, "ISO C forbids label declarations");
4758 }
4759 /* We must now have at least one statement, label or declaration. */
4760 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4761 {
4762 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4763 c_parser_error (parser, "expected declaration or statement");
4764 c_parser_consume_token (parser);
4765 return;
4766 }
4767 while (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
4768 {
4769 location_t loc = c_parser_peek_token (parser)->location;
4770 if (c_parser_next_token_is_keyword (parser, RID_CASE)
4771 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4772 || (c_parser_next_token_is (parser, CPP_NAME)
4773 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4774 {
4775 if (c_parser_next_token_is_keyword (parser, RID_CASE))
4776 label_loc = c_parser_peek_2nd_token (parser)->location;
4777 else
4778 label_loc = c_parser_peek_token (parser)->location;
4779 last_label = true;
4780 last_stmt = false;
4781 mark_valid_location_for_stdc_pragma (false);
4782 c_parser_label (parser);
4783 }
4784 else if (!last_label
4785 && c_parser_next_tokens_start_declaration (parser))
4786 {
4787 last_label = false;
4788 mark_valid_location_for_stdc_pragma (false);
4789 bool fallthru_attr_p = false;
4790 c_parser_declaration_or_fndef (parser, true, true, true, true,
4791 true, NULL, vNULL, NULL,
4792 &fallthru_attr_p);
4793 if (last_stmt && !fallthru_attr_p)
4794 pedwarn_c90 (loc, OPT_Wdeclaration_after_statement,
4795 "ISO C90 forbids mixed declarations and code");
4796 last_stmt = fallthru_attr_p;
4797 }
4798 else if (!last_label
4799 && c_parser_next_token_is_keyword (parser, RID_EXTENSION))
4800 {
4801 /* __extension__ can start a declaration, but is also an
4802 unary operator that can start an expression. Consume all
4803 but the last of a possible series of __extension__ to
4804 determine which. */
4805 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
4806 && (c_parser_peek_2nd_token (parser)->keyword
4807 == RID_EXTENSION))
4808 c_parser_consume_token (parser);
4809 if (c_token_starts_declaration (c_parser_peek_2nd_token (parser)))
4810 {
4811 int ext;
4812 ext = disable_extension_diagnostics ();
4813 c_parser_consume_token (parser);
4814 last_label = false;
4815 mark_valid_location_for_stdc_pragma (false);
4816 c_parser_declaration_or_fndef (parser, true, true, true, true,
4817 true, NULL, vNULL);
4818 /* Following the old parser, __extension__ does not
4819 disable this diagnostic. */
4820 restore_extension_diagnostics (ext);
4821 if (last_stmt)
4822 pedwarn_c90 (loc, OPT_Wdeclaration_after_statement,
4823 "ISO C90 forbids mixed declarations and code");
4824 last_stmt = false;
4825 }
4826 else
4827 goto statement;
4828 }
4829 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
4830 {
4831 /* External pragmas, and some omp pragmas, are not associated
4832 with regular c code, and so are not to be considered statements
4833 syntactically. This ensures that the user doesn't put them
4834 places that would turn into syntax errors if the directive
4835 were ignored. */
4836 if (c_parser_pragma (parser,
4837 last_label ? pragma_stmt : pragma_compound,
4838 NULL))
4839 last_label = false, last_stmt = true;
4840 }
4841 else if (c_parser_next_token_is (parser, CPP_EOF))
4842 {
4843 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4844 c_parser_error (parser, "expected declaration or statement");
4845 return;
4846 }
4847 else if (c_parser_next_token_is_keyword (parser, RID_ELSE))
4848 {
4849 if (parser->in_if_block)
4850 {
4851 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4852 error_at (loc, """expected %<}%> before %<else%>");
4853 return;
4854 }
4855 else
4856 {
4857 error_at (loc, "%<else%> without a previous %<if%>");
4858 c_parser_consume_token (parser);
4859 continue;
4860 }
4861 }
4862 else
4863 {
4864 statement:
4865 last_label = false;
4866 last_stmt = true;
4867 mark_valid_location_for_stdc_pragma (false);
4868 c_parser_statement_after_labels (parser, NULL);
4869 }
4870
4871 parser->error = false;
4872 }
4873 if (last_label)
4874 error_at (label_loc, "label at end of compound statement");
4875 c_parser_consume_token (parser);
4876 /* Restore the value we started with. */
4877 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4878 }
4879
4880 /* Parse all consecutive labels. */
4881
4882 static void
4883 c_parser_all_labels (c_parser *parser)
4884 {
4885 while (c_parser_next_token_is_keyword (parser, RID_CASE)
4886 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4887 || (c_parser_next_token_is (parser, CPP_NAME)
4888 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4889 c_parser_label (parser);
4890 }
4891
4892 /* Parse a label (C90 6.6.1, C99 6.8.1).
4893
4894 label:
4895 identifier : attributes[opt]
4896 case constant-expression :
4897 default :
4898
4899 GNU extensions:
4900
4901 label:
4902 case constant-expression ... constant-expression :
4903
4904 The use of attributes on labels is a GNU extension. The syntax in
4905 GNU C accepts any expressions without commas, non-constant
4906 expressions being rejected later. */
4907
4908 static void
4909 c_parser_label (c_parser *parser)
4910 {
4911 location_t loc1 = c_parser_peek_token (parser)->location;
4912 tree label = NULL_TREE;
4913
4914 /* Remember whether this case or a user-defined label is allowed to fall
4915 through to. */
4916 bool fallthrough_p = c_parser_peek_token (parser)->flags & PREV_FALLTHROUGH;
4917
4918 if (c_parser_next_token_is_keyword (parser, RID_CASE))
4919 {
4920 tree exp1, exp2;
4921 c_parser_consume_token (parser);
4922 exp1 = c_parser_expr_no_commas (parser, NULL).value;
4923 if (c_parser_next_token_is (parser, CPP_COLON))
4924 {
4925 c_parser_consume_token (parser);
4926 label = do_case (loc1, exp1, NULL_TREE);
4927 }
4928 else if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
4929 {
4930 c_parser_consume_token (parser);
4931 exp2 = c_parser_expr_no_commas (parser, NULL).value;
4932 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
4933 label = do_case (loc1, exp1, exp2);
4934 }
4935 else
4936 c_parser_error (parser, "expected %<:%> or %<...%>");
4937 }
4938 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
4939 {
4940 c_parser_consume_token (parser);
4941 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
4942 label = do_case (loc1, NULL_TREE, NULL_TREE);
4943 }
4944 else
4945 {
4946 tree name = c_parser_peek_token (parser)->value;
4947 tree tlab;
4948 tree attrs;
4949 location_t loc2 = c_parser_peek_token (parser)->location;
4950 gcc_assert (c_parser_next_token_is (parser, CPP_NAME));
4951 c_parser_consume_token (parser);
4952 gcc_assert (c_parser_next_token_is (parser, CPP_COLON));
4953 c_parser_consume_token (parser);
4954 attrs = c_parser_attributes (parser);
4955 tlab = define_label (loc2, name);
4956 if (tlab)
4957 {
4958 decl_attributes (&tlab, attrs, 0);
4959 label = add_stmt (build_stmt (loc1, LABEL_EXPR, tlab));
4960 }
4961 }
4962 if (label)
4963 {
4964 if (TREE_CODE (label) == LABEL_EXPR)
4965 FALLTHROUGH_LABEL_P (LABEL_EXPR_LABEL (label)) = fallthrough_p;
4966 else
4967 FALLTHROUGH_LABEL_P (CASE_LABEL (label)) = fallthrough_p;
4968
4969 /* Allow '__attribute__((fallthrough));'. */
4970 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
4971 {
4972 location_t loc = c_parser_peek_token (parser)->location;
4973 tree attrs = c_parser_attributes (parser);
4974 if (attribute_fallthrough_p (attrs))
4975 {
4976 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4977 {
4978 tree fn = build_call_expr_internal_loc (loc,
4979 IFN_FALLTHROUGH,
4980 void_type_node, 0);
4981 add_stmt (fn);
4982 }
4983 else
4984 warning_at (loc, OPT_Wattributes, "%<fallthrough%> attribute "
4985 "not followed by %<;%>");
4986 }
4987 else if (attrs != NULL_TREE)
4988 warning_at (loc, OPT_Wattributes, "only attribute %<fallthrough%>"
4989 " can be applied to a null statement");
4990 }
4991 if (c_parser_next_tokens_start_declaration (parser))
4992 {
4993 error_at (c_parser_peek_token (parser)->location,
4994 "a label can only be part of a statement and "
4995 "a declaration is not a statement");
4996 c_parser_declaration_or_fndef (parser, /*fndef_ok*/ false,
4997 /*static_assert_ok*/ true,
4998 /*empty_ok*/ true, /*nested*/ true,
4999 /*start_attr_ok*/ true, NULL,
5000 vNULL);
5001 }
5002 }
5003 }
5004
5005 /* Parse a statement (C90 6.6, C99 6.8).
5006
5007 statement:
5008 labeled-statement
5009 compound-statement
5010 expression-statement
5011 selection-statement
5012 iteration-statement
5013 jump-statement
5014
5015 labeled-statement:
5016 label statement
5017
5018 expression-statement:
5019 expression[opt] ;
5020
5021 selection-statement:
5022 if-statement
5023 switch-statement
5024
5025 iteration-statement:
5026 while-statement
5027 do-statement
5028 for-statement
5029
5030 jump-statement:
5031 goto identifier ;
5032 continue ;
5033 break ;
5034 return expression[opt] ;
5035
5036 GNU extensions:
5037
5038 statement:
5039 asm-statement
5040
5041 jump-statement:
5042 goto * expression ;
5043
5044 expression-statement:
5045 attributes ;
5046
5047 Objective-C:
5048
5049 statement:
5050 objc-throw-statement
5051 objc-try-catch-statement
5052 objc-synchronized-statement
5053
5054 objc-throw-statement:
5055 @throw expression ;
5056 @throw ;
5057
5058 OpenACC:
5059
5060 statement:
5061 openacc-construct
5062
5063 openacc-construct:
5064 parallel-construct
5065 kernels-construct
5066 data-construct
5067 loop-construct
5068
5069 parallel-construct:
5070 parallel-directive structured-block
5071
5072 kernels-construct:
5073 kernels-directive structured-block
5074
5075 data-construct:
5076 data-directive structured-block
5077
5078 loop-construct:
5079 loop-directive structured-block
5080
5081 OpenMP:
5082
5083 statement:
5084 openmp-construct
5085
5086 openmp-construct:
5087 parallel-construct
5088 for-construct
5089 simd-construct
5090 for-simd-construct
5091 sections-construct
5092 single-construct
5093 parallel-for-construct
5094 parallel-for-simd-construct
5095 parallel-sections-construct
5096 master-construct
5097 critical-construct
5098 atomic-construct
5099 ordered-construct
5100
5101 parallel-construct:
5102 parallel-directive structured-block
5103
5104 for-construct:
5105 for-directive iteration-statement
5106
5107 simd-construct:
5108 simd-directive iteration-statements
5109
5110 for-simd-construct:
5111 for-simd-directive iteration-statements
5112
5113 sections-construct:
5114 sections-directive section-scope
5115
5116 single-construct:
5117 single-directive structured-block
5118
5119 parallel-for-construct:
5120 parallel-for-directive iteration-statement
5121
5122 parallel-for-simd-construct:
5123 parallel-for-simd-directive iteration-statement
5124
5125 parallel-sections-construct:
5126 parallel-sections-directive section-scope
5127
5128 master-construct:
5129 master-directive structured-block
5130
5131 critical-construct:
5132 critical-directive structured-block
5133
5134 atomic-construct:
5135 atomic-directive expression-statement
5136
5137 ordered-construct:
5138 ordered-directive structured-block
5139
5140 Transactional Memory:
5141
5142 statement:
5143 transaction-statement
5144 transaction-cancel-statement
5145
5146 IF_P is used to track whether there's a (possibly labeled) if statement
5147 which is not enclosed in braces and has an else clause. This is used to
5148 implement -Wparentheses. */
5149
5150 static void
5151 c_parser_statement (c_parser *parser, bool *if_p)
5152 {
5153 c_parser_all_labels (parser);
5154 c_parser_statement_after_labels (parser, if_p, NULL);
5155 }
5156
5157 /* Parse a statement, other than a labeled statement. CHAIN is a vector
5158 of if-else-if conditions.
5159
5160 IF_P is used to track whether there's a (possibly labeled) if statement
5161 which is not enclosed in braces and has an else clause. This is used to
5162 implement -Wparentheses. */
5163
5164 static void
5165 c_parser_statement_after_labels (c_parser *parser, bool *if_p,
5166 vec<tree> *chain)
5167 {
5168 location_t loc = c_parser_peek_token (parser)->location;
5169 tree stmt = NULL_TREE;
5170 bool in_if_block = parser->in_if_block;
5171 parser->in_if_block = false;
5172 if (if_p != NULL)
5173 *if_p = false;
5174 switch (c_parser_peek_token (parser)->type)
5175 {
5176 case CPP_OPEN_BRACE:
5177 add_stmt (c_parser_compound_statement (parser));
5178 break;
5179 case CPP_KEYWORD:
5180 switch (c_parser_peek_token (parser)->keyword)
5181 {
5182 case RID_IF:
5183 c_parser_if_statement (parser, if_p, chain);
5184 break;
5185 case RID_SWITCH:
5186 c_parser_switch_statement (parser, if_p);
5187 break;
5188 case RID_WHILE:
5189 c_parser_while_statement (parser, false, if_p);
5190 break;
5191 case RID_DO:
5192 c_parser_do_statement (parser, false);
5193 break;
5194 case RID_FOR:
5195 c_parser_for_statement (parser, false, if_p);
5196 break;
5197 case RID_CILK_FOR:
5198 if (!flag_cilkplus)
5199 {
5200 error_at (c_parser_peek_token (parser)->location,
5201 "-fcilkplus must be enabled to use %<_Cilk_for%>");
5202 c_parser_skip_to_end_of_block_or_statement (parser);
5203 }
5204 else
5205 c_parser_cilk_for (parser, integer_zero_node, if_p);
5206 break;
5207 case RID_CILK_SYNC:
5208 c_parser_consume_token (parser);
5209 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5210 if (!flag_cilkplus)
5211 error_at (loc, "-fcilkplus must be enabled to use %<_Cilk_sync%>");
5212 else
5213 add_stmt (build_cilk_sync ());
5214 break;
5215 case RID_GOTO:
5216 c_parser_consume_token (parser);
5217 if (c_parser_next_token_is (parser, CPP_NAME))
5218 {
5219 stmt = c_finish_goto_label (loc,
5220 c_parser_peek_token (parser)->value);
5221 c_parser_consume_token (parser);
5222 }
5223 else if (c_parser_next_token_is (parser, CPP_MULT))
5224 {
5225 struct c_expr val;
5226
5227 c_parser_consume_token (parser);
5228 val = c_parser_expression (parser);
5229 if (check_no_cilk (val.value,
5230 "Cilk array notation cannot be used as a computed goto expression",
5231 "%<_Cilk_spawn%> statement cannot be used as a computed goto expression",
5232 loc))
5233 val.value = error_mark_node;
5234 val = convert_lvalue_to_rvalue (loc, val, false, true);
5235 stmt = c_finish_goto_ptr (loc, val.value);
5236 }
5237 else
5238 c_parser_error (parser, "expected identifier or %<*%>");
5239 goto expect_semicolon;
5240 case RID_CONTINUE:
5241 c_parser_consume_token (parser);
5242 stmt = c_finish_bc_stmt (loc, &c_cont_label, false);
5243 goto expect_semicolon;
5244 case RID_BREAK:
5245 c_parser_consume_token (parser);
5246 stmt = c_finish_bc_stmt (loc, &c_break_label, true);
5247 goto expect_semicolon;
5248 case RID_RETURN:
5249 c_parser_consume_token (parser);
5250 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5251 {
5252 stmt = c_finish_return (loc, NULL_TREE, NULL_TREE);
5253 c_parser_consume_token (parser);
5254 }
5255 else
5256 {
5257 location_t xloc = c_parser_peek_token (parser)->location;
5258 struct c_expr expr = c_parser_expression_conv (parser);
5259 mark_exp_read (expr.value);
5260 stmt = c_finish_return (EXPR_LOC_OR_LOC (expr.value, xloc),
5261 expr.value, expr.original_type);
5262 goto expect_semicolon;
5263 }
5264 break;
5265 case RID_ASM:
5266 stmt = c_parser_asm_statement (parser);
5267 break;
5268 case RID_TRANSACTION_ATOMIC:
5269 case RID_TRANSACTION_RELAXED:
5270 stmt = c_parser_transaction (parser,
5271 c_parser_peek_token (parser)->keyword);
5272 break;
5273 case RID_TRANSACTION_CANCEL:
5274 stmt = c_parser_transaction_cancel (parser);
5275 goto expect_semicolon;
5276 case RID_AT_THROW:
5277 gcc_assert (c_dialect_objc ());
5278 c_parser_consume_token (parser);
5279 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5280 {
5281 stmt = objc_build_throw_stmt (loc, NULL_TREE);
5282 c_parser_consume_token (parser);
5283 }
5284 else
5285 {
5286 struct c_expr expr = c_parser_expression (parser);
5287 expr = convert_lvalue_to_rvalue (loc, expr, false, false);
5288 if (check_no_cilk (expr.value,
5289 "Cilk array notation cannot be used for a throw expression",
5290 "%<_Cilk_spawn%> statement cannot be used for a throw expression"))
5291 expr.value = error_mark_node;
5292 else
5293 {
5294 expr.value = c_fully_fold (expr.value, false, NULL);
5295 stmt = objc_build_throw_stmt (loc, expr.value);
5296 }
5297 goto expect_semicolon;
5298 }
5299 break;
5300 case RID_AT_TRY:
5301 gcc_assert (c_dialect_objc ());
5302 c_parser_objc_try_catch_finally_statement (parser);
5303 break;
5304 case RID_AT_SYNCHRONIZED:
5305 gcc_assert (c_dialect_objc ());
5306 c_parser_objc_synchronized_statement (parser);
5307 break;
5308 case RID_ATTRIBUTE:
5309 {
5310 /* Allow '__attribute__((fallthrough));'. */
5311 tree attrs = c_parser_attributes (parser);
5312 if (attribute_fallthrough_p (attrs))
5313 {
5314 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5315 {
5316 tree fn = build_call_expr_internal_loc (loc,
5317 IFN_FALLTHROUGH,
5318 void_type_node, 0);
5319 add_stmt (fn);
5320 /* Eat the ';'. */
5321 c_parser_consume_token (parser);
5322 }
5323 else
5324 warning_at (loc, OPT_Wattributes,
5325 "%<fallthrough%> attribute not followed "
5326 "by %<;%>");
5327 }
5328 else if (attrs != NULL_TREE)
5329 warning_at (loc, OPT_Wattributes, "only attribute %<fallthrough%>"
5330 " can be applied to a null statement");
5331 break;
5332 }
5333 default:
5334 goto expr_stmt;
5335 }
5336 break;
5337 case CPP_SEMICOLON:
5338 c_parser_consume_token (parser);
5339 break;
5340 case CPP_CLOSE_PAREN:
5341 case CPP_CLOSE_SQUARE:
5342 /* Avoid infinite loop in error recovery:
5343 c_parser_skip_until_found stops at a closing nesting
5344 delimiter without consuming it, but here we need to consume
5345 it to proceed further. */
5346 c_parser_error (parser, "expected statement");
5347 c_parser_consume_token (parser);
5348 break;
5349 case CPP_PRAGMA:
5350 c_parser_pragma (parser, pragma_stmt, if_p);
5351 break;
5352 default:
5353 expr_stmt:
5354 stmt = c_finish_expr_stmt (loc, c_parser_expression_conv (parser).value);
5355 expect_semicolon:
5356 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5357 break;
5358 }
5359 /* Two cases cannot and do not have line numbers associated: If stmt
5360 is degenerate, such as "2;", then stmt is an INTEGER_CST, which
5361 cannot hold line numbers. But that's OK because the statement
5362 will either be changed to a MODIFY_EXPR during gimplification of
5363 the statement expr, or discarded. If stmt was compound, but
5364 without new variables, we will have skipped the creation of a
5365 BIND and will have a bare STATEMENT_LIST. But that's OK because
5366 (recursively) all of the component statements should already have
5367 line numbers assigned. ??? Can we discard no-op statements
5368 earlier? */
5369 if (EXPR_LOCATION (stmt) == UNKNOWN_LOCATION)
5370 protected_set_expr_location (stmt, loc);
5371
5372 parser->in_if_block = in_if_block;
5373 }
5374
5375 /* Parse the condition from an if, do, while or for statements. */
5376
5377 static tree
5378 c_parser_condition (c_parser *parser)
5379 {
5380 location_t loc = c_parser_peek_token (parser)->location;
5381 tree cond;
5382 cond = c_parser_expression_conv (parser).value;
5383 cond = c_objc_common_truthvalue_conversion (loc, cond);
5384 cond = c_fully_fold (cond, false, NULL);
5385 if (warn_sequence_point)
5386 verify_sequence_points (cond);
5387 return cond;
5388 }
5389
5390 /* Parse a parenthesized condition from an if, do or while statement.
5391
5392 condition:
5393 ( expression )
5394 */
5395 static tree
5396 c_parser_paren_condition (c_parser *parser)
5397 {
5398 tree cond;
5399 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5400 return error_mark_node;
5401 cond = c_parser_condition (parser);
5402 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5403 return cond;
5404 }
5405
5406 /* Parse a statement which is a block in C99.
5407
5408 IF_P is used to track whether there's a (possibly labeled) if statement
5409 which is not enclosed in braces and has an else clause. This is used to
5410 implement -Wparentheses. */
5411
5412 static tree
5413 c_parser_c99_block_statement (c_parser *parser, bool *if_p)
5414 {
5415 tree block = c_begin_compound_stmt (flag_isoc99);
5416 location_t loc = c_parser_peek_token (parser)->location;
5417 c_parser_statement (parser, if_p);
5418 return c_end_compound_stmt (loc, block, flag_isoc99);
5419 }
5420
5421 /* Parse the body of an if statement. This is just parsing a
5422 statement but (a) it is a block in C99, (b) we track whether the
5423 body is an if statement for the sake of -Wparentheses warnings, (c)
5424 we handle an empty body specially for the sake of -Wempty-body
5425 warnings, and (d) we call parser_compound_statement directly
5426 because c_parser_statement_after_labels resets
5427 parser->in_if_block.
5428
5429 IF_P is used to track whether there's a (possibly labeled) if statement
5430 which is not enclosed in braces and has an else clause. This is used to
5431 implement -Wparentheses. */
5432
5433 static tree
5434 c_parser_if_body (c_parser *parser, bool *if_p,
5435 const token_indent_info &if_tinfo)
5436 {
5437 tree block = c_begin_compound_stmt (flag_isoc99);
5438 location_t body_loc = c_parser_peek_token (parser)->location;
5439 token_indent_info body_tinfo
5440 = get_token_indent_info (c_parser_peek_token (parser));
5441
5442 c_parser_all_labels (parser);
5443 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5444 {
5445 location_t loc = c_parser_peek_token (parser)->location;
5446 add_stmt (build_empty_stmt (loc));
5447 c_parser_consume_token (parser);
5448 if (!c_parser_next_token_is_keyword (parser, RID_ELSE))
5449 warning_at (loc, OPT_Wempty_body,
5450 "suggest braces around empty body in an %<if%> statement");
5451 }
5452 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5453 add_stmt (c_parser_compound_statement (parser));
5454 else
5455 c_parser_statement_after_labels (parser, if_p);
5456
5457 token_indent_info next_tinfo
5458 = get_token_indent_info (c_parser_peek_token (parser));
5459 warn_for_misleading_indentation (if_tinfo, body_tinfo, next_tinfo);
5460
5461 return c_end_compound_stmt (body_loc, block, flag_isoc99);
5462 }
5463
5464 /* Parse the else body of an if statement. This is just parsing a
5465 statement but (a) it is a block in C99, (b) we handle an empty body
5466 specially for the sake of -Wempty-body warnings. CHAIN is a vector
5467 of if-else-if conditions. */
5468
5469 static tree
5470 c_parser_else_body (c_parser *parser, const token_indent_info &else_tinfo,
5471 vec<tree> *chain)
5472 {
5473 location_t body_loc = c_parser_peek_token (parser)->location;
5474 tree block = c_begin_compound_stmt (flag_isoc99);
5475 token_indent_info body_tinfo
5476 = get_token_indent_info (c_parser_peek_token (parser));
5477
5478 c_parser_all_labels (parser);
5479 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5480 {
5481 location_t loc = c_parser_peek_token (parser)->location;
5482 warning_at (loc,
5483 OPT_Wempty_body,
5484 "suggest braces around empty body in an %<else%> statement");
5485 add_stmt (build_empty_stmt (loc));
5486 c_parser_consume_token (parser);
5487 }
5488 else
5489 c_parser_statement_after_labels (parser, NULL, chain);
5490
5491 token_indent_info next_tinfo
5492 = get_token_indent_info (c_parser_peek_token (parser));
5493 warn_for_misleading_indentation (else_tinfo, body_tinfo, next_tinfo);
5494
5495 return c_end_compound_stmt (body_loc, block, flag_isoc99);
5496 }
5497
5498 /* We might need to reclassify any previously-lexed identifier, e.g.
5499 when we've left a for loop with an if-statement without else in the
5500 body - we might have used a wrong scope for the token. See PR67784. */
5501
5502 static void
5503 c_parser_maybe_reclassify_token (c_parser *parser)
5504 {
5505 if (c_parser_next_token_is (parser, CPP_NAME))
5506 {
5507 c_token *token = c_parser_peek_token (parser);
5508
5509 if (token->id_kind != C_ID_CLASSNAME)
5510 {
5511 tree decl = lookup_name (token->value);
5512
5513 token->id_kind = C_ID_ID;
5514 if (decl)
5515 {
5516 if (TREE_CODE (decl) == TYPE_DECL)
5517 token->id_kind = C_ID_TYPENAME;
5518 }
5519 else if (c_dialect_objc ())
5520 {
5521 tree objc_interface_decl = objc_is_class_name (token->value);
5522 /* Objective-C class names are in the same namespace as
5523 variables and typedefs, and hence are shadowed by local
5524 declarations. */
5525 if (objc_interface_decl)
5526 {
5527 token->value = objc_interface_decl;
5528 token->id_kind = C_ID_CLASSNAME;
5529 }
5530 }
5531 }
5532 }
5533 }
5534
5535 /* Parse an if statement (C90 6.6.4, C99 6.8.4).
5536
5537 if-statement:
5538 if ( expression ) statement
5539 if ( expression ) statement else statement
5540
5541 CHAIN is a vector of if-else-if conditions.
5542 IF_P is used to track whether there's a (possibly labeled) if statement
5543 which is not enclosed in braces and has an else clause. This is used to
5544 implement -Wparentheses. */
5545
5546 static void
5547 c_parser_if_statement (c_parser *parser, bool *if_p, vec<tree> *chain)
5548 {
5549 tree block;
5550 location_t loc;
5551 tree cond;
5552 bool nested_if = false;
5553 tree first_body, second_body;
5554 bool in_if_block;
5555 tree if_stmt;
5556
5557 gcc_assert (c_parser_next_token_is_keyword (parser, RID_IF));
5558 token_indent_info if_tinfo
5559 = get_token_indent_info (c_parser_peek_token (parser));
5560 c_parser_consume_token (parser);
5561 block = c_begin_compound_stmt (flag_isoc99);
5562 loc = c_parser_peek_token (parser)->location;
5563 cond = c_parser_paren_condition (parser);
5564 if (flag_cilkplus && contains_cilk_spawn_stmt (cond))
5565 {
5566 error_at (loc, "if statement cannot contain %<Cilk_spawn%>");
5567 cond = error_mark_node;
5568 }
5569 in_if_block = parser->in_if_block;
5570 parser->in_if_block = true;
5571 first_body = c_parser_if_body (parser, &nested_if, if_tinfo);
5572 parser->in_if_block = in_if_block;
5573
5574 if (warn_duplicated_cond)
5575 warn_duplicated_cond_add_or_warn (EXPR_LOCATION (cond), cond, &chain);
5576
5577 if (c_parser_next_token_is_keyword (parser, RID_ELSE))
5578 {
5579 token_indent_info else_tinfo
5580 = get_token_indent_info (c_parser_peek_token (parser));
5581 c_parser_consume_token (parser);
5582 if (warn_duplicated_cond)
5583 {
5584 if (c_parser_next_token_is_keyword (parser, RID_IF)
5585 && chain == NULL)
5586 {
5587 /* We've got "if (COND) else if (COND2)". Start the
5588 condition chain and add COND as the first element. */
5589 chain = new vec<tree> ();
5590 if (!CONSTANT_CLASS_P (cond) && !TREE_SIDE_EFFECTS (cond))
5591 chain->safe_push (cond);
5592 }
5593 else if (!c_parser_next_token_is_keyword (parser, RID_IF))
5594 {
5595 /* This is if-else without subsequent if. Zap the condition
5596 chain; we would have already warned at this point. */
5597 delete chain;
5598 chain = NULL;
5599 }
5600 }
5601 second_body = c_parser_else_body (parser, else_tinfo, chain);
5602 /* Set IF_P to true to indicate that this if statement has an
5603 else clause. This may trigger the Wparentheses warning
5604 below when we get back up to the parent if statement. */
5605 if (if_p != NULL)
5606 *if_p = true;
5607 }
5608 else
5609 {
5610 second_body = NULL_TREE;
5611
5612 /* Diagnose an ambiguous else if if-then-else is nested inside
5613 if-then. */
5614 if (nested_if)
5615 warning_at (loc, OPT_Wdangling_else,
5616 "suggest explicit braces to avoid ambiguous %<else%>");
5617
5618 if (warn_duplicated_cond)
5619 {
5620 /* This if statement does not have an else clause. We don't
5621 need the condition chain anymore. */
5622 delete chain;
5623 chain = NULL;
5624 }
5625 }
5626 c_finish_if_stmt (loc, cond, first_body, second_body);
5627 if_stmt = c_end_compound_stmt (loc, block, flag_isoc99);
5628
5629 /* If the if statement contains array notations, then we expand them. */
5630 if (flag_cilkplus && contains_array_notation_expr (if_stmt))
5631 if_stmt = fix_conditional_array_notations (if_stmt);
5632 add_stmt (if_stmt);
5633 c_parser_maybe_reclassify_token (parser);
5634 }
5635
5636 /* Parse a switch statement (C90 6.6.4, C99 6.8.4).
5637
5638 switch-statement:
5639 switch (expression) statement
5640 */
5641
5642 static void
5643 c_parser_switch_statement (c_parser *parser, bool *if_p)
5644 {
5645 struct c_expr ce;
5646 tree block, expr, body, save_break;
5647 location_t switch_loc = c_parser_peek_token (parser)->location;
5648 location_t switch_cond_loc;
5649 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SWITCH));
5650 c_parser_consume_token (parser);
5651 block = c_begin_compound_stmt (flag_isoc99);
5652 bool explicit_cast_p = false;
5653 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5654 {
5655 switch_cond_loc = c_parser_peek_token (parser)->location;
5656 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
5657 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
5658 explicit_cast_p = true;
5659 ce = c_parser_expression (parser);
5660 ce = convert_lvalue_to_rvalue (switch_cond_loc, ce, true, false);
5661 expr = ce.value;
5662 /* ??? expr has no valid location? */
5663 if (check_no_cilk (expr,
5664 "Cilk array notation cannot be used as a condition for switch statement",
5665 "%<_Cilk_spawn%> statement cannot be used as a condition for switch statement",
5666 switch_cond_loc))
5667 expr = error_mark_node;
5668 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5669 }
5670 else
5671 {
5672 switch_cond_loc = UNKNOWN_LOCATION;
5673 expr = error_mark_node;
5674 ce.original_type = error_mark_node;
5675 }
5676 c_start_case (switch_loc, switch_cond_loc, expr, explicit_cast_p);
5677 save_break = c_break_label;
5678 c_break_label = NULL_TREE;
5679 body = c_parser_c99_block_statement (parser, if_p);
5680 c_finish_case (body, ce.original_type);
5681 if (c_break_label)
5682 {
5683 location_t here = c_parser_peek_token (parser)->location;
5684 tree t = build1 (LABEL_EXPR, void_type_node, c_break_label);
5685 SET_EXPR_LOCATION (t, here);
5686 add_stmt (t);
5687 }
5688 c_break_label = save_break;
5689 add_stmt (c_end_compound_stmt (switch_loc, block, flag_isoc99));
5690 c_parser_maybe_reclassify_token (parser);
5691 }
5692
5693 /* Parse a while statement (C90 6.6.5, C99 6.8.5).
5694
5695 while-statement:
5696 while (expression) statement
5697
5698 IF_P is used to track whether there's a (possibly labeled) if statement
5699 which is not enclosed in braces and has an else clause. This is used to
5700 implement -Wparentheses. */
5701
5702 static void
5703 c_parser_while_statement (c_parser *parser, bool ivdep, bool *if_p)
5704 {
5705 tree block, cond, body, save_break, save_cont;
5706 location_t loc;
5707 gcc_assert (c_parser_next_token_is_keyword (parser, RID_WHILE));
5708 token_indent_info while_tinfo
5709 = get_token_indent_info (c_parser_peek_token (parser));
5710 c_parser_consume_token (parser);
5711 block = c_begin_compound_stmt (flag_isoc99);
5712 loc = c_parser_peek_token (parser)->location;
5713 cond = c_parser_paren_condition (parser);
5714 if (check_no_cilk (cond,
5715 "Cilk array notation cannot be used as a condition for while statement",
5716 "%<_Cilk_spawn%> statement cannot be used as a condition for while statement"))
5717 cond = error_mark_node;
5718 if (ivdep && cond != error_mark_node)
5719 cond = build2 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
5720 build_int_cst (integer_type_node,
5721 annot_expr_ivdep_kind));
5722 save_break = c_break_label;
5723 c_break_label = NULL_TREE;
5724 save_cont = c_cont_label;
5725 c_cont_label = NULL_TREE;
5726
5727 token_indent_info body_tinfo
5728 = get_token_indent_info (c_parser_peek_token (parser));
5729
5730 body = c_parser_c99_block_statement (parser, if_p);
5731 c_finish_loop (loc, cond, NULL, body, c_break_label, c_cont_label, true);
5732 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
5733 c_parser_maybe_reclassify_token (parser);
5734
5735 token_indent_info next_tinfo
5736 = get_token_indent_info (c_parser_peek_token (parser));
5737 warn_for_misleading_indentation (while_tinfo, body_tinfo, next_tinfo);
5738
5739 c_break_label = save_break;
5740 c_cont_label = save_cont;
5741 }
5742
5743 /* Parse a do statement (C90 6.6.5, C99 6.8.5).
5744
5745 do-statement:
5746 do statement while ( expression ) ;
5747 */
5748
5749 static void
5750 c_parser_do_statement (c_parser *parser, bool ivdep)
5751 {
5752 tree block, cond, body, save_break, save_cont, new_break, new_cont;
5753 location_t loc;
5754 gcc_assert (c_parser_next_token_is_keyword (parser, RID_DO));
5755 c_parser_consume_token (parser);
5756 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5757 warning_at (c_parser_peek_token (parser)->location,
5758 OPT_Wempty_body,
5759 "suggest braces around empty body in %<do%> statement");
5760 block = c_begin_compound_stmt (flag_isoc99);
5761 loc = c_parser_peek_token (parser)->location;
5762 save_break = c_break_label;
5763 c_break_label = NULL_TREE;
5764 save_cont = c_cont_label;
5765 c_cont_label = NULL_TREE;
5766 body = c_parser_c99_block_statement (parser, NULL);
5767 c_parser_require_keyword (parser, RID_WHILE, "expected %<while%>");
5768 new_break = c_break_label;
5769 c_break_label = save_break;
5770 new_cont = c_cont_label;
5771 c_cont_label = save_cont;
5772 cond = c_parser_paren_condition (parser);
5773 if (check_no_cilk (cond,
5774 "Cilk array notation cannot be used as a condition for a do-while statement",
5775 "%<_Cilk_spawn%> statement cannot be used as a condition for a do-while statement"))
5776 cond = error_mark_node;
5777 if (ivdep && cond != error_mark_node)
5778 cond = build2 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
5779 build_int_cst (integer_type_node,
5780 annot_expr_ivdep_kind));
5781 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
5782 c_parser_skip_to_end_of_block_or_statement (parser);
5783 c_finish_loop (loc, cond, NULL, body, new_break, new_cont, false);
5784 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
5785 }
5786
5787 /* Parse a for statement (C90 6.6.5, C99 6.8.5).
5788
5789 for-statement:
5790 for ( expression[opt] ; expression[opt] ; expression[opt] ) statement
5791 for ( nested-declaration expression[opt] ; expression[opt] ) statement
5792
5793 The form with a declaration is new in C99.
5794
5795 ??? In accordance with the old parser, the declaration may be a
5796 nested function, which is then rejected in check_for_loop_decls,
5797 but does it make any sense for this to be included in the grammar?
5798 Note in particular that the nested function does not include a
5799 trailing ';', whereas the "declaration" production includes one.
5800 Also, can we reject bad declarations earlier and cheaper than
5801 check_for_loop_decls?
5802
5803 In Objective-C, there are two additional variants:
5804
5805 foreach-statement:
5806 for ( expression in expresssion ) statement
5807 for ( declaration in expression ) statement
5808
5809 This is inconsistent with C, because the second variant is allowed
5810 even if c99 is not enabled.
5811
5812 The rest of the comment documents these Objective-C foreach-statement.
5813
5814 Here is the canonical example of the first variant:
5815 for (object in array) { do something with object }
5816 we call the first expression ("object") the "object_expression" and
5817 the second expression ("array") the "collection_expression".
5818 object_expression must be an lvalue of type "id" (a generic Objective-C
5819 object) because the loop works by assigning to object_expression the
5820 various objects from the collection_expression. collection_expression
5821 must evaluate to something of type "id" which responds to the method
5822 countByEnumeratingWithState:objects:count:.
5823
5824 The canonical example of the second variant is:
5825 for (id object in array) { do something with object }
5826 which is completely equivalent to
5827 {
5828 id object;
5829 for (object in array) { do something with object }
5830 }
5831 Note that initizializing 'object' in some way (eg, "for ((object =
5832 xxx) in array) { do something with object }") is possibly
5833 technically valid, but completely pointless as 'object' will be
5834 assigned to something else as soon as the loop starts. We should
5835 most likely reject it (TODO).
5836
5837 The beginning of the Objective-C foreach-statement looks exactly
5838 like the beginning of the for-statement, and we can tell it is a
5839 foreach-statement only because the initial declaration or
5840 expression is terminated by 'in' instead of ';'.
5841
5842 IF_P is used to track whether there's a (possibly labeled) if statement
5843 which is not enclosed in braces and has an else clause. This is used to
5844 implement -Wparentheses. */
5845
5846 static void
5847 c_parser_for_statement (c_parser *parser, bool ivdep, bool *if_p)
5848 {
5849 tree block, cond, incr, save_break, save_cont, body;
5850 /* The following are only used when parsing an ObjC foreach statement. */
5851 tree object_expression;
5852 /* Silence the bogus uninitialized warning. */
5853 tree collection_expression = NULL;
5854 location_t loc = c_parser_peek_token (parser)->location;
5855 location_t for_loc = c_parser_peek_token (parser)->location;
5856 bool is_foreach_statement = false;
5857 gcc_assert (c_parser_next_token_is_keyword (parser, RID_FOR));
5858 token_indent_info for_tinfo
5859 = get_token_indent_info (c_parser_peek_token (parser));
5860 c_parser_consume_token (parser);
5861 /* Open a compound statement in Objective-C as well, just in case this is
5862 as foreach expression. */
5863 block = c_begin_compound_stmt (flag_isoc99 || c_dialect_objc ());
5864 cond = error_mark_node;
5865 incr = error_mark_node;
5866 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5867 {
5868 /* Parse the initialization declaration or expression. */
5869 object_expression = error_mark_node;
5870 parser->objc_could_be_foreach_context = c_dialect_objc ();
5871 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5872 {
5873 parser->objc_could_be_foreach_context = false;
5874 c_parser_consume_token (parser);
5875 c_finish_expr_stmt (loc, NULL_TREE);
5876 }
5877 else if (c_parser_next_tokens_start_declaration (parser))
5878 {
5879 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
5880 &object_expression, vNULL);
5881 parser->objc_could_be_foreach_context = false;
5882
5883 if (c_parser_next_token_is_keyword (parser, RID_IN))
5884 {
5885 c_parser_consume_token (parser);
5886 is_foreach_statement = true;
5887 if (check_for_loop_decls (for_loc, true) == NULL_TREE)
5888 c_parser_error (parser, "multiple iterating variables in fast enumeration");
5889 }
5890 else
5891 check_for_loop_decls (for_loc, flag_isoc99);
5892 }
5893 else if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
5894 {
5895 /* __extension__ can start a declaration, but is also an
5896 unary operator that can start an expression. Consume all
5897 but the last of a possible series of __extension__ to
5898 determine which. */
5899 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
5900 && (c_parser_peek_2nd_token (parser)->keyword
5901 == RID_EXTENSION))
5902 c_parser_consume_token (parser);
5903 if (c_token_starts_declaration (c_parser_peek_2nd_token (parser)))
5904 {
5905 int ext;
5906 ext = disable_extension_diagnostics ();
5907 c_parser_consume_token (parser);
5908 c_parser_declaration_or_fndef (parser, true, true, true, true,
5909 true, &object_expression, vNULL);
5910 parser->objc_could_be_foreach_context = false;
5911
5912 restore_extension_diagnostics (ext);
5913 if (c_parser_next_token_is_keyword (parser, RID_IN))
5914 {
5915 c_parser_consume_token (parser);
5916 is_foreach_statement = true;
5917 if (check_for_loop_decls (for_loc, true) == NULL_TREE)
5918 c_parser_error (parser, "multiple iterating variables in fast enumeration");
5919 }
5920 else
5921 check_for_loop_decls (for_loc, flag_isoc99);
5922 }
5923 else
5924 goto init_expr;
5925 }
5926 else
5927 {
5928 init_expr:
5929 {
5930 struct c_expr ce;
5931 tree init_expression;
5932 ce = c_parser_expression (parser);
5933 /* In theory we could forbid _Cilk_spawn here, as the spec says "only in top
5934 level statement", but it works just fine, so allow it. */
5935 init_expression = ce.value;
5936 parser->objc_could_be_foreach_context = false;
5937 if (c_parser_next_token_is_keyword (parser, RID_IN))
5938 {
5939 c_parser_consume_token (parser);
5940 is_foreach_statement = true;
5941 if (! lvalue_p (init_expression))
5942 c_parser_error (parser, "invalid iterating variable in fast enumeration");
5943 object_expression = c_fully_fold (init_expression, false, NULL);
5944 }
5945 else
5946 {
5947 ce = convert_lvalue_to_rvalue (loc, ce, true, false);
5948 init_expression = ce.value;
5949 c_finish_expr_stmt (loc, init_expression);
5950 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5951 }
5952 }
5953 }
5954 /* Parse the loop condition. In the case of a foreach
5955 statement, there is no loop condition. */
5956 gcc_assert (!parser->objc_could_be_foreach_context);
5957 if (!is_foreach_statement)
5958 {
5959 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5960 {
5961 if (ivdep)
5962 {
5963 c_parser_error (parser, "missing loop condition in loop with "
5964 "%<GCC ivdep%> pragma");
5965 cond = error_mark_node;
5966 }
5967 else
5968 {
5969 c_parser_consume_token (parser);
5970 cond = NULL_TREE;
5971 }
5972 }
5973 else
5974 {
5975 cond = c_parser_condition (parser);
5976 if (check_no_cilk (cond,
5977 "Cilk array notation cannot be used in a condition for a for-loop",
5978 "%<_Cilk_spawn%> statement cannot be used in a condition for a for-loop"))
5979 cond = error_mark_node;
5980 c_parser_skip_until_found (parser, CPP_SEMICOLON,
5981 "expected %<;%>");
5982 }
5983 if (ivdep && cond != error_mark_node)
5984 cond = build2 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
5985 build_int_cst (integer_type_node,
5986 annot_expr_ivdep_kind));
5987 }
5988 /* Parse the increment expression (the third expression in a
5989 for-statement). In the case of a foreach-statement, this is
5990 the expression that follows the 'in'. */
5991 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
5992 {
5993 if (is_foreach_statement)
5994 {
5995 c_parser_error (parser, "missing collection in fast enumeration");
5996 collection_expression = error_mark_node;
5997 }
5998 else
5999 incr = c_process_expr_stmt (loc, NULL_TREE);
6000 }
6001 else
6002 {
6003 if (is_foreach_statement)
6004 collection_expression = c_fully_fold (c_parser_expression (parser).value,
6005 false, NULL);
6006 else
6007 {
6008 struct c_expr ce = c_parser_expression (parser);
6009 ce = convert_lvalue_to_rvalue (loc, ce, true, false);
6010 incr = c_process_expr_stmt (loc, ce.value);
6011 }
6012 }
6013 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6014 }
6015 save_break = c_break_label;
6016 c_break_label = NULL_TREE;
6017 save_cont = c_cont_label;
6018 c_cont_label = NULL_TREE;
6019
6020 token_indent_info body_tinfo
6021 = get_token_indent_info (c_parser_peek_token (parser));
6022
6023 body = c_parser_c99_block_statement (parser, if_p);
6024
6025 if (is_foreach_statement)
6026 objc_finish_foreach_loop (loc, object_expression, collection_expression, body, c_break_label, c_cont_label);
6027 else
6028 c_finish_loop (loc, cond, incr, body, c_break_label, c_cont_label, true);
6029 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99 || c_dialect_objc ()));
6030 c_parser_maybe_reclassify_token (parser);
6031
6032 token_indent_info next_tinfo
6033 = get_token_indent_info (c_parser_peek_token (parser));
6034 warn_for_misleading_indentation (for_tinfo, body_tinfo, next_tinfo);
6035
6036 c_break_label = save_break;
6037 c_cont_label = save_cont;
6038 }
6039
6040 /* Parse an asm statement, a GNU extension. This is a full-blown asm
6041 statement with inputs, outputs, clobbers, and volatile tag
6042 allowed.
6043
6044 asm-statement:
6045 asm type-qualifier[opt] ( asm-argument ) ;
6046 asm type-qualifier[opt] goto ( asm-goto-argument ) ;
6047
6048 asm-argument:
6049 asm-string-literal
6050 asm-string-literal : asm-operands[opt]
6051 asm-string-literal : asm-operands[opt] : asm-operands[opt]
6052 asm-string-literal : asm-operands[opt] : asm-operands[opt] : asm-clobbers[opt]
6053
6054 asm-goto-argument:
6055 asm-string-literal : : asm-operands[opt] : asm-clobbers[opt] \
6056 : asm-goto-operands
6057
6058 Qualifiers other than volatile are accepted in the syntax but
6059 warned for. */
6060
6061 static tree
6062 c_parser_asm_statement (c_parser *parser)
6063 {
6064 tree quals, str, outputs, inputs, clobbers, labels, ret;
6065 bool simple, is_goto;
6066 location_t asm_loc = c_parser_peek_token (parser)->location;
6067 int section, nsections;
6068
6069 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
6070 c_parser_consume_token (parser);
6071 if (c_parser_next_token_is_keyword (parser, RID_VOLATILE))
6072 {
6073 quals = c_parser_peek_token (parser)->value;
6074 c_parser_consume_token (parser);
6075 }
6076 else if (c_parser_next_token_is_keyword (parser, RID_CONST)
6077 || c_parser_next_token_is_keyword (parser, RID_RESTRICT))
6078 {
6079 warning_at (c_parser_peek_token (parser)->location,
6080 0,
6081 "%E qualifier ignored on asm",
6082 c_parser_peek_token (parser)->value);
6083 quals = NULL_TREE;
6084 c_parser_consume_token (parser);
6085 }
6086 else
6087 quals = NULL_TREE;
6088
6089 is_goto = false;
6090 if (c_parser_next_token_is_keyword (parser, RID_GOTO))
6091 {
6092 c_parser_consume_token (parser);
6093 is_goto = true;
6094 }
6095
6096 /* ??? Follow the C++ parser rather than using the
6097 lex_untranslated_string kludge. */
6098 parser->lex_untranslated_string = true;
6099 ret = NULL;
6100
6101 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6102 goto error;
6103
6104 str = c_parser_asm_string_literal (parser);
6105 if (str == NULL_TREE)
6106 goto error_close_paren;
6107
6108 simple = true;
6109 outputs = NULL_TREE;
6110 inputs = NULL_TREE;
6111 clobbers = NULL_TREE;
6112 labels = NULL_TREE;
6113
6114 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
6115 goto done_asm;
6116
6117 /* Parse each colon-delimited section of operands. */
6118 nsections = 3 + is_goto;
6119 for (section = 0; section < nsections; ++section)
6120 {
6121 if (!c_parser_require (parser, CPP_COLON,
6122 is_goto
6123 ? "expected %<:%>"
6124 : "expected %<:%> or %<)%>"))
6125 goto error_close_paren;
6126
6127 /* Once past any colon, we're no longer a simple asm. */
6128 simple = false;
6129
6130 if ((!c_parser_next_token_is (parser, CPP_COLON)
6131 && !c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
6132 || section == 3)
6133 switch (section)
6134 {
6135 case 0:
6136 /* For asm goto, we don't allow output operands, but reserve
6137 the slot for a future extension that does allow them. */
6138 if (!is_goto)
6139 outputs = c_parser_asm_operands (parser);
6140 break;
6141 case 1:
6142 inputs = c_parser_asm_operands (parser);
6143 break;
6144 case 2:
6145 clobbers = c_parser_asm_clobbers (parser);
6146 break;
6147 case 3:
6148 labels = c_parser_asm_goto_operands (parser);
6149 break;
6150 default:
6151 gcc_unreachable ();
6152 }
6153
6154 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
6155 goto done_asm;
6156 }
6157
6158 done_asm:
6159 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
6160 {
6161 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6162 goto error;
6163 }
6164
6165 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
6166 c_parser_skip_to_end_of_block_or_statement (parser);
6167
6168 ret = build_asm_stmt (quals, build_asm_expr (asm_loc, str, outputs, inputs,
6169 clobbers, labels, simple));
6170
6171 error:
6172 parser->lex_untranslated_string = false;
6173 return ret;
6174
6175 error_close_paren:
6176 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6177 goto error;
6178 }
6179
6180 /* Parse asm operands, a GNU extension.
6181
6182 asm-operands:
6183 asm-operand
6184 asm-operands , asm-operand
6185
6186 asm-operand:
6187 asm-string-literal ( expression )
6188 [ identifier ] asm-string-literal ( expression )
6189 */
6190
6191 static tree
6192 c_parser_asm_operands (c_parser *parser)
6193 {
6194 tree list = NULL_TREE;
6195 while (true)
6196 {
6197 tree name, str;
6198 struct c_expr expr;
6199 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
6200 {
6201 c_parser_consume_token (parser);
6202 if (c_parser_next_token_is (parser, CPP_NAME))
6203 {
6204 tree id = c_parser_peek_token (parser)->value;
6205 c_parser_consume_token (parser);
6206 name = build_string (IDENTIFIER_LENGTH (id),
6207 IDENTIFIER_POINTER (id));
6208 }
6209 else
6210 {
6211 c_parser_error (parser, "expected identifier");
6212 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
6213 return NULL_TREE;
6214 }
6215 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
6216 "expected %<]%>");
6217 }
6218 else
6219 name = NULL_TREE;
6220 str = c_parser_asm_string_literal (parser);
6221 if (str == NULL_TREE)
6222 return NULL_TREE;
6223 parser->lex_untranslated_string = false;
6224 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6225 {
6226 parser->lex_untranslated_string = true;
6227 return NULL_TREE;
6228 }
6229 expr = c_parser_expression (parser);
6230 mark_exp_read (expr.value);
6231 parser->lex_untranslated_string = true;
6232 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
6233 {
6234 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6235 return NULL_TREE;
6236 }
6237 list = chainon (list, build_tree_list (build_tree_list (name, str),
6238 expr.value));
6239 if (c_parser_next_token_is (parser, CPP_COMMA))
6240 c_parser_consume_token (parser);
6241 else
6242 break;
6243 }
6244 return list;
6245 }
6246
6247 /* Parse asm clobbers, a GNU extension.
6248
6249 asm-clobbers:
6250 asm-string-literal
6251 asm-clobbers , asm-string-literal
6252 */
6253
6254 static tree
6255 c_parser_asm_clobbers (c_parser *parser)
6256 {
6257 tree list = NULL_TREE;
6258 while (true)
6259 {
6260 tree str = c_parser_asm_string_literal (parser);
6261 if (str)
6262 list = tree_cons (NULL_TREE, str, list);
6263 else
6264 return NULL_TREE;
6265 if (c_parser_next_token_is (parser, CPP_COMMA))
6266 c_parser_consume_token (parser);
6267 else
6268 break;
6269 }
6270 return list;
6271 }
6272
6273 /* Parse asm goto labels, a GNU extension.
6274
6275 asm-goto-operands:
6276 identifier
6277 asm-goto-operands , identifier
6278 */
6279
6280 static tree
6281 c_parser_asm_goto_operands (c_parser *parser)
6282 {
6283 tree list = NULL_TREE;
6284 while (true)
6285 {
6286 tree name, label;
6287
6288 if (c_parser_next_token_is (parser, CPP_NAME))
6289 {
6290 c_token *tok = c_parser_peek_token (parser);
6291 name = tok->value;
6292 label = lookup_label_for_goto (tok->location, name);
6293 c_parser_consume_token (parser);
6294 TREE_USED (label) = 1;
6295 }
6296 else
6297 {
6298 c_parser_error (parser, "expected identifier");
6299 return NULL_TREE;
6300 }
6301
6302 name = build_string (IDENTIFIER_LENGTH (name),
6303 IDENTIFIER_POINTER (name));
6304 list = tree_cons (name, label, list);
6305 if (c_parser_next_token_is (parser, CPP_COMMA))
6306 c_parser_consume_token (parser);
6307 else
6308 return nreverse (list);
6309 }
6310 }
6311
6312 /* Parse an expression other than a compound expression; that is, an
6313 assignment expression (C90 6.3.16, C99 6.5.16). If AFTER is not
6314 NULL then it is an Objective-C message expression which is the
6315 primary-expression starting the expression as an initializer.
6316
6317 assignment-expression:
6318 conditional-expression
6319 unary-expression assignment-operator assignment-expression
6320
6321 assignment-operator: one of
6322 = *= /= %= += -= <<= >>= &= ^= |=
6323
6324 In GNU C we accept any conditional expression on the LHS and
6325 diagnose the invalid lvalue rather than producing a syntax
6326 error. */
6327
6328 static struct c_expr
6329 c_parser_expr_no_commas (c_parser *parser, struct c_expr *after,
6330 tree omp_atomic_lhs)
6331 {
6332 struct c_expr lhs, rhs, ret;
6333 enum tree_code code;
6334 location_t op_location, exp_location;
6335 gcc_assert (!after || c_dialect_objc ());
6336 lhs = c_parser_conditional_expression (parser, after, omp_atomic_lhs);
6337 op_location = c_parser_peek_token (parser)->location;
6338 switch (c_parser_peek_token (parser)->type)
6339 {
6340 case CPP_EQ:
6341 code = NOP_EXPR;
6342 break;
6343 case CPP_MULT_EQ:
6344 code = MULT_EXPR;
6345 break;
6346 case CPP_DIV_EQ:
6347 code = TRUNC_DIV_EXPR;
6348 break;
6349 case CPP_MOD_EQ:
6350 code = TRUNC_MOD_EXPR;
6351 break;
6352 case CPP_PLUS_EQ:
6353 code = PLUS_EXPR;
6354 break;
6355 case CPP_MINUS_EQ:
6356 code = MINUS_EXPR;
6357 break;
6358 case CPP_LSHIFT_EQ:
6359 code = LSHIFT_EXPR;
6360 break;
6361 case CPP_RSHIFT_EQ:
6362 code = RSHIFT_EXPR;
6363 break;
6364 case CPP_AND_EQ:
6365 code = BIT_AND_EXPR;
6366 break;
6367 case CPP_XOR_EQ:
6368 code = BIT_XOR_EXPR;
6369 break;
6370 case CPP_OR_EQ:
6371 code = BIT_IOR_EXPR;
6372 break;
6373 default:
6374 return lhs;
6375 }
6376 c_parser_consume_token (parser);
6377 exp_location = c_parser_peek_token (parser)->location;
6378 rhs = c_parser_expr_no_commas (parser, NULL);
6379 rhs = convert_lvalue_to_rvalue (exp_location, rhs, true, true);
6380
6381 ret.value = build_modify_expr (op_location, lhs.value, lhs.original_type,
6382 code, exp_location, rhs.value,
6383 rhs.original_type);
6384 set_c_expr_source_range (&ret, lhs.get_start (), rhs.get_finish ());
6385 if (code == NOP_EXPR)
6386 ret.original_code = MODIFY_EXPR;
6387 else
6388 {
6389 TREE_NO_WARNING (ret.value) = 1;
6390 ret.original_code = ERROR_MARK;
6391 }
6392 ret.original_type = NULL;
6393 return ret;
6394 }
6395
6396 /* Parse a conditional expression (C90 6.3.15, C99 6.5.15). If AFTER
6397 is not NULL then it is an Objective-C message expression which is
6398 the primary-expression starting the expression as an initializer.
6399
6400 conditional-expression:
6401 logical-OR-expression
6402 logical-OR-expression ? expression : conditional-expression
6403
6404 GNU extensions:
6405
6406 conditional-expression:
6407 logical-OR-expression ? : conditional-expression
6408 */
6409
6410 static struct c_expr
6411 c_parser_conditional_expression (c_parser *parser, struct c_expr *after,
6412 tree omp_atomic_lhs)
6413 {
6414 struct c_expr cond, exp1, exp2, ret;
6415 location_t start, cond_loc, colon_loc, middle_loc;
6416
6417 gcc_assert (!after || c_dialect_objc ());
6418
6419 cond = c_parser_binary_expression (parser, after, omp_atomic_lhs);
6420
6421 if (c_parser_next_token_is_not (parser, CPP_QUERY))
6422 return cond;
6423 if (cond.value != error_mark_node)
6424 start = cond.get_start ();
6425 else
6426 start = UNKNOWN_LOCATION;
6427 cond_loc = c_parser_peek_token (parser)->location;
6428 cond = convert_lvalue_to_rvalue (cond_loc, cond, true, true);
6429 c_parser_consume_token (parser);
6430 if (c_parser_next_token_is (parser, CPP_COLON))
6431 {
6432 tree eptype = NULL_TREE;
6433
6434 middle_loc = c_parser_peek_token (parser)->location;
6435 pedwarn (middle_loc, OPT_Wpedantic,
6436 "ISO C forbids omitting the middle term of a ?: expression");
6437 if (TREE_CODE (cond.value) == EXCESS_PRECISION_EXPR)
6438 {
6439 eptype = TREE_TYPE (cond.value);
6440 cond.value = TREE_OPERAND (cond.value, 0);
6441 }
6442 tree e = cond.value;
6443 while (TREE_CODE (e) == COMPOUND_EXPR)
6444 e = TREE_OPERAND (e, 1);
6445 warn_for_omitted_condop (middle_loc, e);
6446 /* Make sure first operand is calculated only once. */
6447 exp1.value = c_save_expr (default_conversion (cond.value));
6448 if (eptype)
6449 exp1.value = build1 (EXCESS_PRECISION_EXPR, eptype, exp1.value);
6450 exp1.original_type = NULL;
6451 cond.value = c_objc_common_truthvalue_conversion (cond_loc, exp1.value);
6452 c_inhibit_evaluation_warnings += cond.value == truthvalue_true_node;
6453 }
6454 else
6455 {
6456 cond.value
6457 = c_objc_common_truthvalue_conversion
6458 (cond_loc, default_conversion (cond.value));
6459 c_inhibit_evaluation_warnings += cond.value == truthvalue_false_node;
6460 exp1 = c_parser_expression_conv (parser);
6461 mark_exp_read (exp1.value);
6462 c_inhibit_evaluation_warnings +=
6463 ((cond.value == truthvalue_true_node)
6464 - (cond.value == truthvalue_false_node));
6465 }
6466
6467 colon_loc = c_parser_peek_token (parser)->location;
6468 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
6469 {
6470 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
6471 ret.value = error_mark_node;
6472 ret.original_code = ERROR_MARK;
6473 ret.original_type = NULL;
6474 return ret;
6475 }
6476 {
6477 location_t exp2_loc = c_parser_peek_token (parser)->location;
6478 exp2 = c_parser_conditional_expression (parser, NULL, NULL_TREE);
6479 exp2 = convert_lvalue_to_rvalue (exp2_loc, exp2, true, true);
6480 }
6481 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
6482 ret.value = build_conditional_expr (colon_loc, cond.value,
6483 cond.original_code == C_MAYBE_CONST_EXPR,
6484 exp1.value, exp1.original_type,
6485 exp2.value, exp2.original_type);
6486 ret.original_code = ERROR_MARK;
6487 if (exp1.value == error_mark_node || exp2.value == error_mark_node)
6488 ret.original_type = NULL;
6489 else
6490 {
6491 tree t1, t2;
6492
6493 /* If both sides are enum type, the default conversion will have
6494 made the type of the result be an integer type. We want to
6495 remember the enum types we started with. */
6496 t1 = exp1.original_type ? exp1.original_type : TREE_TYPE (exp1.value);
6497 t2 = exp2.original_type ? exp2.original_type : TREE_TYPE (exp2.value);
6498 ret.original_type = ((t1 != error_mark_node
6499 && t2 != error_mark_node
6500 && (TYPE_MAIN_VARIANT (t1)
6501 == TYPE_MAIN_VARIANT (t2)))
6502 ? t1
6503 : NULL);
6504 }
6505 set_c_expr_source_range (&ret, start, exp2.get_finish ());
6506 return ret;
6507 }
6508
6509 /* Parse a binary expression; that is, a logical-OR-expression (C90
6510 6.3.5-6.3.14, C99 6.5.5-6.5.14). If AFTER is not NULL then it is
6511 an Objective-C message expression which is the primary-expression
6512 starting the expression as an initializer.
6513
6514 OMP_ATOMIC_LHS is NULL, unless parsing OpenMP #pragma omp atomic,
6515 when it should be the unfolded lhs. In a valid OpenMP source,
6516 one of the operands of the toplevel binary expression must be equal
6517 to it. In that case, just return a build2 created binary operation
6518 rather than result of parser_build_binary_op.
6519
6520 multiplicative-expression:
6521 cast-expression
6522 multiplicative-expression * cast-expression
6523 multiplicative-expression / cast-expression
6524 multiplicative-expression % cast-expression
6525
6526 additive-expression:
6527 multiplicative-expression
6528 additive-expression + multiplicative-expression
6529 additive-expression - multiplicative-expression
6530
6531 shift-expression:
6532 additive-expression
6533 shift-expression << additive-expression
6534 shift-expression >> additive-expression
6535
6536 relational-expression:
6537 shift-expression
6538 relational-expression < shift-expression
6539 relational-expression > shift-expression
6540 relational-expression <= shift-expression
6541 relational-expression >= shift-expression
6542
6543 equality-expression:
6544 relational-expression
6545 equality-expression == relational-expression
6546 equality-expression != relational-expression
6547
6548 AND-expression:
6549 equality-expression
6550 AND-expression & equality-expression
6551
6552 exclusive-OR-expression:
6553 AND-expression
6554 exclusive-OR-expression ^ AND-expression
6555
6556 inclusive-OR-expression:
6557 exclusive-OR-expression
6558 inclusive-OR-expression | exclusive-OR-expression
6559
6560 logical-AND-expression:
6561 inclusive-OR-expression
6562 logical-AND-expression && inclusive-OR-expression
6563
6564 logical-OR-expression:
6565 logical-AND-expression
6566 logical-OR-expression || logical-AND-expression
6567 */
6568
6569 static struct c_expr
6570 c_parser_binary_expression (c_parser *parser, struct c_expr *after,
6571 tree omp_atomic_lhs)
6572 {
6573 /* A binary expression is parsed using operator-precedence parsing,
6574 with the operands being cast expressions. All the binary
6575 operators are left-associative. Thus a binary expression is of
6576 form:
6577
6578 E0 op1 E1 op2 E2 ...
6579
6580 which we represent on a stack. On the stack, the precedence
6581 levels are strictly increasing. When a new operator is
6582 encountered of higher precedence than that at the top of the
6583 stack, it is pushed; its LHS is the top expression, and its RHS
6584 is everything parsed until it is popped. When a new operator is
6585 encountered with precedence less than or equal to that at the top
6586 of the stack, triples E[i-1] op[i] E[i] are popped and replaced
6587 by the result of the operation until the operator at the top of
6588 the stack has lower precedence than the new operator or there is
6589 only one element on the stack; then the top expression is the LHS
6590 of the new operator. In the case of logical AND and OR
6591 expressions, we also need to adjust c_inhibit_evaluation_warnings
6592 as appropriate when the operators are pushed and popped. */
6593
6594 struct {
6595 /* The expression at this stack level. */
6596 struct c_expr expr;
6597 /* The precedence of the operator on its left, PREC_NONE at the
6598 bottom of the stack. */
6599 enum c_parser_prec prec;
6600 /* The operation on its left. */
6601 enum tree_code op;
6602 /* The source location of this operation. */
6603 location_t loc;
6604 } stack[NUM_PRECS];
6605 int sp;
6606 /* Location of the binary operator. */
6607 location_t binary_loc = UNKNOWN_LOCATION; /* Quiet warning. */
6608 #define POP \
6609 do { \
6610 switch (stack[sp].op) \
6611 { \
6612 case TRUTH_ANDIF_EXPR: \
6613 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
6614 == truthvalue_false_node); \
6615 break; \
6616 case TRUTH_ORIF_EXPR: \
6617 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
6618 == truthvalue_true_node); \
6619 break; \
6620 default: \
6621 break; \
6622 } \
6623 stack[sp - 1].expr \
6624 = convert_lvalue_to_rvalue (stack[sp - 1].loc, \
6625 stack[sp - 1].expr, true, true); \
6626 stack[sp].expr \
6627 = convert_lvalue_to_rvalue (stack[sp].loc, \
6628 stack[sp].expr, true, true); \
6629 if (__builtin_expect (omp_atomic_lhs != NULL_TREE, 0) && sp == 1 \
6630 && c_parser_peek_token (parser)->type == CPP_SEMICOLON \
6631 && ((1 << stack[sp].prec) \
6632 & ((1 << PREC_BITOR) | (1 << PREC_BITXOR) | (1 << PREC_BITAND) \
6633 | (1 << PREC_SHIFT) | (1 << PREC_ADD) | (1 << PREC_MULT))) \
6634 && stack[sp].op != TRUNC_MOD_EXPR \
6635 && stack[0].expr.value != error_mark_node \
6636 && stack[1].expr.value != error_mark_node \
6637 && (c_tree_equal (stack[0].expr.value, omp_atomic_lhs) \
6638 || c_tree_equal (stack[1].expr.value, omp_atomic_lhs))) \
6639 stack[0].expr.value \
6640 = build2 (stack[1].op, TREE_TYPE (stack[0].expr.value), \
6641 stack[0].expr.value, stack[1].expr.value); \
6642 else \
6643 stack[sp - 1].expr = parser_build_binary_op (stack[sp].loc, \
6644 stack[sp].op, \
6645 stack[sp - 1].expr, \
6646 stack[sp].expr); \
6647 sp--; \
6648 } while (0)
6649 gcc_assert (!after || c_dialect_objc ());
6650 stack[0].loc = c_parser_peek_token (parser)->location;
6651 stack[0].expr = c_parser_cast_expression (parser, after);
6652 stack[0].prec = PREC_NONE;
6653 sp = 0;
6654 while (true)
6655 {
6656 enum c_parser_prec oprec;
6657 enum tree_code ocode;
6658 source_range src_range;
6659 if (parser->error)
6660 goto out;
6661 switch (c_parser_peek_token (parser)->type)
6662 {
6663 case CPP_MULT:
6664 oprec = PREC_MULT;
6665 ocode = MULT_EXPR;
6666 break;
6667 case CPP_DIV:
6668 oprec = PREC_MULT;
6669 ocode = TRUNC_DIV_EXPR;
6670 break;
6671 case CPP_MOD:
6672 oprec = PREC_MULT;
6673 ocode = TRUNC_MOD_EXPR;
6674 break;
6675 case CPP_PLUS:
6676 oprec = PREC_ADD;
6677 ocode = PLUS_EXPR;
6678 break;
6679 case CPP_MINUS:
6680 oprec = PREC_ADD;
6681 ocode = MINUS_EXPR;
6682 break;
6683 case CPP_LSHIFT:
6684 oprec = PREC_SHIFT;
6685 ocode = LSHIFT_EXPR;
6686 break;
6687 case CPP_RSHIFT:
6688 oprec = PREC_SHIFT;
6689 ocode = RSHIFT_EXPR;
6690 break;
6691 case CPP_LESS:
6692 oprec = PREC_REL;
6693 ocode = LT_EXPR;
6694 break;
6695 case CPP_GREATER:
6696 oprec = PREC_REL;
6697 ocode = GT_EXPR;
6698 break;
6699 case CPP_LESS_EQ:
6700 oprec = PREC_REL;
6701 ocode = LE_EXPR;
6702 break;
6703 case CPP_GREATER_EQ:
6704 oprec = PREC_REL;
6705 ocode = GE_EXPR;
6706 break;
6707 case CPP_EQ_EQ:
6708 oprec = PREC_EQ;
6709 ocode = EQ_EXPR;
6710 break;
6711 case CPP_NOT_EQ:
6712 oprec = PREC_EQ;
6713 ocode = NE_EXPR;
6714 break;
6715 case CPP_AND:
6716 oprec = PREC_BITAND;
6717 ocode = BIT_AND_EXPR;
6718 break;
6719 case CPP_XOR:
6720 oprec = PREC_BITXOR;
6721 ocode = BIT_XOR_EXPR;
6722 break;
6723 case CPP_OR:
6724 oprec = PREC_BITOR;
6725 ocode = BIT_IOR_EXPR;
6726 break;
6727 case CPP_AND_AND:
6728 oprec = PREC_LOGAND;
6729 ocode = TRUTH_ANDIF_EXPR;
6730 break;
6731 case CPP_OR_OR:
6732 oprec = PREC_LOGOR;
6733 ocode = TRUTH_ORIF_EXPR;
6734 break;
6735 default:
6736 /* Not a binary operator, so end of the binary
6737 expression. */
6738 goto out;
6739 }
6740 binary_loc = c_parser_peek_token (parser)->location;
6741 while (oprec <= stack[sp].prec)
6742 POP;
6743 c_parser_consume_token (parser);
6744 switch (ocode)
6745 {
6746 case TRUTH_ANDIF_EXPR:
6747 src_range = stack[sp].expr.src_range;
6748 stack[sp].expr
6749 = convert_lvalue_to_rvalue (stack[sp].loc,
6750 stack[sp].expr, true, true);
6751 stack[sp].expr.value = c_objc_common_truthvalue_conversion
6752 (stack[sp].loc, default_conversion (stack[sp].expr.value));
6753 c_inhibit_evaluation_warnings += (stack[sp].expr.value
6754 == truthvalue_false_node);
6755 set_c_expr_source_range (&stack[sp].expr, src_range);
6756 break;
6757 case TRUTH_ORIF_EXPR:
6758 src_range = stack[sp].expr.src_range;
6759 stack[sp].expr
6760 = convert_lvalue_to_rvalue (stack[sp].loc,
6761 stack[sp].expr, true, true);
6762 stack[sp].expr.value = c_objc_common_truthvalue_conversion
6763 (stack[sp].loc, default_conversion (stack[sp].expr.value));
6764 c_inhibit_evaluation_warnings += (stack[sp].expr.value
6765 == truthvalue_true_node);
6766 set_c_expr_source_range (&stack[sp].expr, src_range);
6767 break;
6768 default:
6769 break;
6770 }
6771 sp++;
6772 stack[sp].loc = binary_loc;
6773 stack[sp].expr = c_parser_cast_expression (parser, NULL);
6774 stack[sp].prec = oprec;
6775 stack[sp].op = ocode;
6776 }
6777 out:
6778 while (sp > 0)
6779 POP;
6780 return stack[0].expr;
6781 #undef POP
6782 }
6783
6784 /* Parse a cast expression (C90 6.3.4, C99 6.5.4). If AFTER is not
6785 NULL then it is an Objective-C message expression which is the
6786 primary-expression starting the expression as an initializer.
6787
6788 cast-expression:
6789 unary-expression
6790 ( type-name ) unary-expression
6791 */
6792
6793 static struct c_expr
6794 c_parser_cast_expression (c_parser *parser, struct c_expr *after)
6795 {
6796 location_t cast_loc = c_parser_peek_token (parser)->location;
6797 gcc_assert (!after || c_dialect_objc ());
6798 if (after)
6799 return c_parser_postfix_expression_after_primary (parser,
6800 cast_loc, *after);
6801 /* If the expression begins with a parenthesized type name, it may
6802 be either a cast or a compound literal; we need to see whether
6803 the next character is '{' to tell the difference. If not, it is
6804 an unary expression. Full detection of unknown typenames here
6805 would require a 3-token lookahead. */
6806 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
6807 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
6808 {
6809 struct c_type_name *type_name;
6810 struct c_expr ret;
6811 struct c_expr expr;
6812 c_parser_consume_token (parser);
6813 type_name = c_parser_type_name (parser);
6814 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6815 if (type_name == NULL)
6816 {
6817 ret.value = error_mark_node;
6818 ret.original_code = ERROR_MARK;
6819 ret.original_type = NULL;
6820 return ret;
6821 }
6822
6823 /* Save casted types in the function's used types hash table. */
6824 used_types_insert (type_name->specs->type);
6825
6826 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
6827 return c_parser_postfix_expression_after_paren_type (parser, type_name,
6828 cast_loc);
6829 {
6830 location_t expr_loc = c_parser_peek_token (parser)->location;
6831 expr = c_parser_cast_expression (parser, NULL);
6832 expr = convert_lvalue_to_rvalue (expr_loc, expr, true, true);
6833 }
6834 ret.value = c_cast_expr (cast_loc, type_name, expr.value);
6835 if (ret.value && expr.value)
6836 set_c_expr_source_range (&ret, cast_loc, expr.get_finish ());
6837 ret.original_code = ERROR_MARK;
6838 ret.original_type = NULL;
6839 return ret;
6840 }
6841 else
6842 return c_parser_unary_expression (parser);
6843 }
6844
6845 /* Parse an unary expression (C90 6.3.3, C99 6.5.3).
6846
6847 unary-expression:
6848 postfix-expression
6849 ++ unary-expression
6850 -- unary-expression
6851 unary-operator cast-expression
6852 sizeof unary-expression
6853 sizeof ( type-name )
6854
6855 unary-operator: one of
6856 & * + - ~ !
6857
6858 GNU extensions:
6859
6860 unary-expression:
6861 __alignof__ unary-expression
6862 __alignof__ ( type-name )
6863 && identifier
6864
6865 (C11 permits _Alignof with type names only.)
6866
6867 unary-operator: one of
6868 __extension__ __real__ __imag__
6869
6870 Transactional Memory:
6871
6872 unary-expression:
6873 transaction-expression
6874
6875 In addition, the GNU syntax treats ++ and -- as unary operators, so
6876 they may be applied to cast expressions with errors for non-lvalues
6877 given later. */
6878
6879 static struct c_expr
6880 c_parser_unary_expression (c_parser *parser)
6881 {
6882 int ext;
6883 struct c_expr ret, op;
6884 location_t op_loc = c_parser_peek_token (parser)->location;
6885 location_t exp_loc;
6886 location_t finish;
6887 ret.original_code = ERROR_MARK;
6888 ret.original_type = NULL;
6889 switch (c_parser_peek_token (parser)->type)
6890 {
6891 case CPP_PLUS_PLUS:
6892 c_parser_consume_token (parser);
6893 exp_loc = c_parser_peek_token (parser)->location;
6894 op = c_parser_cast_expression (parser, NULL);
6895
6896 /* If there is array notations in op, we expand them. */
6897 if (flag_cilkplus && TREE_CODE (op.value) == ARRAY_NOTATION_REF)
6898 return fix_array_notation_expr (exp_loc, PREINCREMENT_EXPR, op);
6899 else
6900 {
6901 op = default_function_array_read_conversion (exp_loc, op);
6902 return parser_build_unary_op (op_loc, PREINCREMENT_EXPR, op);
6903 }
6904 case CPP_MINUS_MINUS:
6905 c_parser_consume_token (parser);
6906 exp_loc = c_parser_peek_token (parser)->location;
6907 op = c_parser_cast_expression (parser, NULL);
6908
6909 /* If there is array notations in op, we expand them. */
6910 if (flag_cilkplus && TREE_CODE (op.value) == ARRAY_NOTATION_REF)
6911 return fix_array_notation_expr (exp_loc, PREDECREMENT_EXPR, op);
6912 else
6913 {
6914 op = default_function_array_read_conversion (exp_loc, op);
6915 return parser_build_unary_op (op_loc, PREDECREMENT_EXPR, op);
6916 }
6917 case CPP_AND:
6918 c_parser_consume_token (parser);
6919 op = c_parser_cast_expression (parser, NULL);
6920 mark_exp_read (op.value);
6921 return parser_build_unary_op (op_loc, ADDR_EXPR, op);
6922 case CPP_MULT:
6923 {
6924 c_parser_consume_token (parser);
6925 exp_loc = c_parser_peek_token (parser)->location;
6926 op = c_parser_cast_expression (parser, NULL);
6927 finish = op.get_finish ();
6928 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
6929 location_t combined_loc = make_location (op_loc, op_loc, finish);
6930 ret.value = build_indirect_ref (combined_loc, op.value, RO_UNARY_STAR);
6931 ret.src_range.m_start = op_loc;
6932 ret.src_range.m_finish = finish;
6933 return ret;
6934 }
6935 case CPP_PLUS:
6936 if (!c_dialect_objc () && !in_system_header_at (input_location))
6937 warning_at (op_loc,
6938 OPT_Wtraditional,
6939 "traditional C rejects the unary plus operator");
6940 c_parser_consume_token (parser);
6941 exp_loc = c_parser_peek_token (parser)->location;
6942 op = c_parser_cast_expression (parser, NULL);
6943 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
6944 return parser_build_unary_op (op_loc, CONVERT_EXPR, op);
6945 case CPP_MINUS:
6946 c_parser_consume_token (parser);
6947 exp_loc = c_parser_peek_token (parser)->location;
6948 op = c_parser_cast_expression (parser, NULL);
6949 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
6950 return parser_build_unary_op (op_loc, NEGATE_EXPR, op);
6951 case CPP_COMPL:
6952 c_parser_consume_token (parser);
6953 exp_loc = c_parser_peek_token (parser)->location;
6954 op = c_parser_cast_expression (parser, NULL);
6955 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
6956 return parser_build_unary_op (op_loc, BIT_NOT_EXPR, op);
6957 case CPP_NOT:
6958 c_parser_consume_token (parser);
6959 exp_loc = c_parser_peek_token (parser)->location;
6960 op = c_parser_cast_expression (parser, NULL);
6961 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
6962 return parser_build_unary_op (op_loc, TRUTH_NOT_EXPR, op);
6963 case CPP_AND_AND:
6964 /* Refer to the address of a label as a pointer. */
6965 c_parser_consume_token (parser);
6966 if (c_parser_next_token_is (parser, CPP_NAME))
6967 {
6968 ret.value = finish_label_address_expr
6969 (c_parser_peek_token (parser)->value, op_loc);
6970 set_c_expr_source_range (&ret, op_loc,
6971 c_parser_peek_token (parser)->get_finish ());
6972 c_parser_consume_token (parser);
6973 }
6974 else
6975 {
6976 c_parser_error (parser, "expected identifier");
6977 ret.value = error_mark_node;
6978 }
6979 return ret;
6980 case CPP_KEYWORD:
6981 switch (c_parser_peek_token (parser)->keyword)
6982 {
6983 case RID_SIZEOF:
6984 return c_parser_sizeof_expression (parser);
6985 case RID_ALIGNOF:
6986 return c_parser_alignof_expression (parser);
6987 case RID_EXTENSION:
6988 c_parser_consume_token (parser);
6989 ext = disable_extension_diagnostics ();
6990 ret = c_parser_cast_expression (parser, NULL);
6991 restore_extension_diagnostics (ext);
6992 return ret;
6993 case RID_REALPART:
6994 c_parser_consume_token (parser);
6995 exp_loc = c_parser_peek_token (parser)->location;
6996 op = c_parser_cast_expression (parser, NULL);
6997 op = default_function_array_conversion (exp_loc, op);
6998 return parser_build_unary_op (op_loc, REALPART_EXPR, op);
6999 case RID_IMAGPART:
7000 c_parser_consume_token (parser);
7001 exp_loc = c_parser_peek_token (parser)->location;
7002 op = c_parser_cast_expression (parser, NULL);
7003 op = default_function_array_conversion (exp_loc, op);
7004 return parser_build_unary_op (op_loc, IMAGPART_EXPR, op);
7005 case RID_TRANSACTION_ATOMIC:
7006 case RID_TRANSACTION_RELAXED:
7007 return c_parser_transaction_expression (parser,
7008 c_parser_peek_token (parser)->keyword);
7009 default:
7010 return c_parser_postfix_expression (parser);
7011 }
7012 default:
7013 return c_parser_postfix_expression (parser);
7014 }
7015 }
7016
7017 /* Parse a sizeof expression. */
7018
7019 static struct c_expr
7020 c_parser_sizeof_expression (c_parser *parser)
7021 {
7022 struct c_expr expr;
7023 struct c_expr result;
7024 location_t expr_loc;
7025 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SIZEOF));
7026
7027 location_t start;
7028 location_t finish = UNKNOWN_LOCATION;
7029
7030 start = c_parser_peek_token (parser)->location;
7031
7032 c_parser_consume_token (parser);
7033 c_inhibit_evaluation_warnings++;
7034 in_sizeof++;
7035 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
7036 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
7037 {
7038 /* Either sizeof ( type-name ) or sizeof unary-expression
7039 starting with a compound literal. */
7040 struct c_type_name *type_name;
7041 c_parser_consume_token (parser);
7042 expr_loc = c_parser_peek_token (parser)->location;
7043 type_name = c_parser_type_name (parser);
7044 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7045 finish = parser->tokens_buf[0].location;
7046 if (type_name == NULL)
7047 {
7048 struct c_expr ret;
7049 c_inhibit_evaluation_warnings--;
7050 in_sizeof--;
7051 ret.value = error_mark_node;
7052 ret.original_code = ERROR_MARK;
7053 ret.original_type = NULL;
7054 return ret;
7055 }
7056 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
7057 {
7058 expr = c_parser_postfix_expression_after_paren_type (parser,
7059 type_name,
7060 expr_loc);
7061 finish = expr.get_finish ();
7062 goto sizeof_expr;
7063 }
7064 /* sizeof ( type-name ). */
7065 c_inhibit_evaluation_warnings--;
7066 in_sizeof--;
7067 result = c_expr_sizeof_type (expr_loc, type_name);
7068 }
7069 else
7070 {
7071 expr_loc = c_parser_peek_token (parser)->location;
7072 expr = c_parser_unary_expression (parser);
7073 finish = expr.get_finish ();
7074 sizeof_expr:
7075 c_inhibit_evaluation_warnings--;
7076 in_sizeof--;
7077 mark_exp_read (expr.value);
7078 if (TREE_CODE (expr.value) == COMPONENT_REF
7079 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
7080 error_at (expr_loc, "%<sizeof%> applied to a bit-field");
7081 result = c_expr_sizeof_expr (expr_loc, expr);
7082 }
7083 if (finish != UNKNOWN_LOCATION)
7084 set_c_expr_source_range (&result, start, finish);
7085 return result;
7086 }
7087
7088 /* Parse an alignof expression. */
7089
7090 static struct c_expr
7091 c_parser_alignof_expression (c_parser *parser)
7092 {
7093 struct c_expr expr;
7094 location_t start_loc = c_parser_peek_token (parser)->location;
7095 location_t end_loc;
7096 tree alignof_spelling = c_parser_peek_token (parser)->value;
7097 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNOF));
7098 bool is_c11_alignof = strcmp (IDENTIFIER_POINTER (alignof_spelling),
7099 "_Alignof") == 0;
7100 /* A diagnostic is not required for the use of this identifier in
7101 the implementation namespace; only diagnose it for the C11
7102 spelling because of existing code using the other spellings. */
7103 if (is_c11_alignof)
7104 {
7105 if (flag_isoc99)
7106 pedwarn_c99 (start_loc, OPT_Wpedantic, "ISO C99 does not support %qE",
7107 alignof_spelling);
7108 else
7109 pedwarn_c99 (start_loc, OPT_Wpedantic, "ISO C90 does not support %qE",
7110 alignof_spelling);
7111 }
7112 c_parser_consume_token (parser);
7113 c_inhibit_evaluation_warnings++;
7114 in_alignof++;
7115 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
7116 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
7117 {
7118 /* Either __alignof__ ( type-name ) or __alignof__
7119 unary-expression starting with a compound literal. */
7120 location_t loc;
7121 struct c_type_name *type_name;
7122 struct c_expr ret;
7123 c_parser_consume_token (parser);
7124 loc = c_parser_peek_token (parser)->location;
7125 type_name = c_parser_type_name (parser);
7126 end_loc = c_parser_peek_token (parser)->location;
7127 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7128 if (type_name == NULL)
7129 {
7130 struct c_expr ret;
7131 c_inhibit_evaluation_warnings--;
7132 in_alignof--;
7133 ret.value = error_mark_node;
7134 ret.original_code = ERROR_MARK;
7135 ret.original_type = NULL;
7136 return ret;
7137 }
7138 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
7139 {
7140 expr = c_parser_postfix_expression_after_paren_type (parser,
7141 type_name,
7142 loc);
7143 goto alignof_expr;
7144 }
7145 /* alignof ( type-name ). */
7146 c_inhibit_evaluation_warnings--;
7147 in_alignof--;
7148 ret.value = c_sizeof_or_alignof_type (loc, groktypename (type_name,
7149 NULL, NULL),
7150 false, is_c11_alignof, 1);
7151 ret.original_code = ERROR_MARK;
7152 ret.original_type = NULL;
7153 set_c_expr_source_range (&ret, start_loc, end_loc);
7154 return ret;
7155 }
7156 else
7157 {
7158 struct c_expr ret;
7159 expr = c_parser_unary_expression (parser);
7160 end_loc = expr.src_range.m_finish;
7161 alignof_expr:
7162 mark_exp_read (expr.value);
7163 c_inhibit_evaluation_warnings--;
7164 in_alignof--;
7165 if (is_c11_alignof)
7166 pedwarn (start_loc,
7167 OPT_Wpedantic, "ISO C does not allow %<%E (expression)%>",
7168 alignof_spelling);
7169 ret.value = c_alignof_expr (start_loc, expr.value);
7170 ret.original_code = ERROR_MARK;
7171 ret.original_type = NULL;
7172 set_c_expr_source_range (&ret, start_loc, end_loc);
7173 return ret;
7174 }
7175 }
7176
7177 /* Helper function to read arguments of builtins which are interfaces
7178 for the middle-end nodes like COMPLEX_EXPR, VEC_PERM_EXPR and
7179 others. The name of the builtin is passed using BNAME parameter.
7180 Function returns true if there were no errors while parsing and
7181 stores the arguments in CEXPR_LIST. If it returns true,
7182 *OUT_CLOSE_PAREN_LOC is written to with the location of the closing
7183 parenthesis. */
7184 static bool
7185 c_parser_get_builtin_args (c_parser *parser, const char *bname,
7186 vec<c_expr_t, va_gc> **ret_cexpr_list,
7187 bool choose_expr_p,
7188 location_t *out_close_paren_loc)
7189 {
7190 location_t loc = c_parser_peek_token (parser)->location;
7191 vec<c_expr_t, va_gc> *cexpr_list;
7192 c_expr_t expr;
7193 bool saved_force_folding_builtin_constant_p;
7194
7195 *ret_cexpr_list = NULL;
7196 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
7197 {
7198 error_at (loc, "cannot take address of %qs", bname);
7199 return false;
7200 }
7201
7202 c_parser_consume_token (parser);
7203
7204 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
7205 {
7206 *out_close_paren_loc = c_parser_peek_token (parser)->location;
7207 c_parser_consume_token (parser);
7208 return true;
7209 }
7210
7211 saved_force_folding_builtin_constant_p
7212 = force_folding_builtin_constant_p;
7213 force_folding_builtin_constant_p |= choose_expr_p;
7214 expr = c_parser_expr_no_commas (parser, NULL);
7215 force_folding_builtin_constant_p
7216 = saved_force_folding_builtin_constant_p;
7217 vec_alloc (cexpr_list, 1);
7218 vec_safe_push (cexpr_list, expr);
7219 while (c_parser_next_token_is (parser, CPP_COMMA))
7220 {
7221 c_parser_consume_token (parser);
7222 expr = c_parser_expr_no_commas (parser, NULL);
7223 vec_safe_push (cexpr_list, expr);
7224 }
7225
7226 *out_close_paren_loc = c_parser_peek_token (parser)->location;
7227 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
7228 return false;
7229
7230 *ret_cexpr_list = cexpr_list;
7231 return true;
7232 }
7233
7234 /* This represents a single generic-association. */
7235
7236 struct c_generic_association
7237 {
7238 /* The location of the starting token of the type. */
7239 location_t type_location;
7240 /* The association's type, or NULL_TREE for 'default'. */
7241 tree type;
7242 /* The association's expression. */
7243 struct c_expr expression;
7244 };
7245
7246 /* Parse a generic-selection. (C11 6.5.1.1).
7247
7248 generic-selection:
7249 _Generic ( assignment-expression , generic-assoc-list )
7250
7251 generic-assoc-list:
7252 generic-association
7253 generic-assoc-list , generic-association
7254
7255 generic-association:
7256 type-name : assignment-expression
7257 default : assignment-expression
7258 */
7259
7260 static struct c_expr
7261 c_parser_generic_selection (c_parser *parser)
7262 {
7263 struct c_expr selector, error_expr;
7264 tree selector_type;
7265 struct c_generic_association matched_assoc;
7266 bool match_found = false;
7267 location_t generic_loc, selector_loc;
7268
7269 error_expr.original_code = ERROR_MARK;
7270 error_expr.original_type = NULL;
7271 error_expr.set_error ();
7272 matched_assoc.type_location = UNKNOWN_LOCATION;
7273 matched_assoc.type = NULL_TREE;
7274 matched_assoc.expression = error_expr;
7275
7276 gcc_assert (c_parser_next_token_is_keyword (parser, RID_GENERIC));
7277 generic_loc = c_parser_peek_token (parser)->location;
7278 c_parser_consume_token (parser);
7279 if (flag_isoc99)
7280 pedwarn_c99 (generic_loc, OPT_Wpedantic,
7281 "ISO C99 does not support %<_Generic%>");
7282 else
7283 pedwarn_c99 (generic_loc, OPT_Wpedantic,
7284 "ISO C90 does not support %<_Generic%>");
7285
7286 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7287 return error_expr;
7288
7289 c_inhibit_evaluation_warnings++;
7290 selector_loc = c_parser_peek_token (parser)->location;
7291 selector = c_parser_expr_no_commas (parser, NULL);
7292 selector = default_function_array_conversion (selector_loc, selector);
7293 c_inhibit_evaluation_warnings--;
7294
7295 if (selector.value == error_mark_node)
7296 {
7297 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7298 return selector;
7299 }
7300 selector_type = TREE_TYPE (selector.value);
7301 /* In ISO C terms, rvalues (including the controlling expression of
7302 _Generic) do not have qualified types. */
7303 if (TREE_CODE (selector_type) != ARRAY_TYPE)
7304 selector_type = TYPE_MAIN_VARIANT (selector_type);
7305 /* In ISO C terms, _Noreturn is not part of the type of expressions
7306 such as &abort, but in GCC it is represented internally as a type
7307 qualifier. */
7308 if (FUNCTION_POINTER_TYPE_P (selector_type)
7309 && TYPE_QUALS (TREE_TYPE (selector_type)) != TYPE_UNQUALIFIED)
7310 selector_type
7311 = build_pointer_type (TYPE_MAIN_VARIANT (TREE_TYPE (selector_type)));
7312
7313 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
7314 {
7315 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7316 return error_expr;
7317 }
7318
7319 auto_vec<c_generic_association> associations;
7320 while (1)
7321 {
7322 struct c_generic_association assoc, *iter;
7323 unsigned int ix;
7324 c_token *token = c_parser_peek_token (parser);
7325
7326 assoc.type_location = token->location;
7327 if (token->type == CPP_KEYWORD && token->keyword == RID_DEFAULT)
7328 {
7329 c_parser_consume_token (parser);
7330 assoc.type = NULL_TREE;
7331 }
7332 else
7333 {
7334 struct c_type_name *type_name;
7335
7336 type_name = c_parser_type_name (parser);
7337 if (type_name == NULL)
7338 {
7339 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7340 return error_expr;
7341 }
7342 assoc.type = groktypename (type_name, NULL, NULL);
7343 if (assoc.type == error_mark_node)
7344 {
7345 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7346 return error_expr;
7347 }
7348
7349 if (TREE_CODE (assoc.type) == FUNCTION_TYPE)
7350 error_at (assoc.type_location,
7351 "%<_Generic%> association has function type");
7352 else if (!COMPLETE_TYPE_P (assoc.type))
7353 error_at (assoc.type_location,
7354 "%<_Generic%> association has incomplete type");
7355
7356 if (variably_modified_type_p (assoc.type, NULL_TREE))
7357 error_at (assoc.type_location,
7358 "%<_Generic%> association has "
7359 "variable length type");
7360 }
7361
7362 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
7363 {
7364 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7365 return error_expr;
7366 }
7367
7368 assoc.expression = c_parser_expr_no_commas (parser, NULL);
7369 if (assoc.expression.value == error_mark_node)
7370 {
7371 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7372 return error_expr;
7373 }
7374
7375 for (ix = 0; associations.iterate (ix, &iter); ++ix)
7376 {
7377 if (assoc.type == NULL_TREE)
7378 {
7379 if (iter->type == NULL_TREE)
7380 {
7381 error_at (assoc.type_location,
7382 "duplicate %<default%> case in %<_Generic%>");
7383 inform (iter->type_location, "original %<default%> is here");
7384 }
7385 }
7386 else if (iter->type != NULL_TREE)
7387 {
7388 if (comptypes (assoc.type, iter->type))
7389 {
7390 error_at (assoc.type_location,
7391 "%<_Generic%> specifies two compatible types");
7392 inform (iter->type_location, "compatible type is here");
7393 }
7394 }
7395 }
7396
7397 if (assoc.type == NULL_TREE)
7398 {
7399 if (!match_found)
7400 {
7401 matched_assoc = assoc;
7402 match_found = true;
7403 }
7404 }
7405 else if (comptypes (assoc.type, selector_type))
7406 {
7407 if (!match_found || matched_assoc.type == NULL_TREE)
7408 {
7409 matched_assoc = assoc;
7410 match_found = true;
7411 }
7412 else
7413 {
7414 error_at (assoc.type_location,
7415 "%<_Generic> selector matches multiple associations");
7416 inform (matched_assoc.type_location,
7417 "other match is here");
7418 }
7419 }
7420
7421 associations.safe_push (assoc);
7422
7423 if (c_parser_peek_token (parser)->type != CPP_COMMA)
7424 break;
7425 c_parser_consume_token (parser);
7426 }
7427
7428 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
7429 {
7430 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7431 return error_expr;
7432 }
7433
7434 if (!match_found)
7435 {
7436 error_at (selector_loc, "%<_Generic%> selector of type %qT is not "
7437 "compatible with any association",
7438 selector_type);
7439 return error_expr;
7440 }
7441
7442 return matched_assoc.expression;
7443 }
7444
7445 /* Parse a postfix expression (C90 6.3.1-6.3.2, C99 6.5.1-6.5.2).
7446
7447 postfix-expression:
7448 primary-expression
7449 postfix-expression [ expression ]
7450 postfix-expression ( argument-expression-list[opt] )
7451 postfix-expression . identifier
7452 postfix-expression -> identifier
7453 postfix-expression ++
7454 postfix-expression --
7455 ( type-name ) { initializer-list }
7456 ( type-name ) { initializer-list , }
7457
7458 argument-expression-list:
7459 argument-expression
7460 argument-expression-list , argument-expression
7461
7462 primary-expression:
7463 identifier
7464 constant
7465 string-literal
7466 ( expression )
7467 generic-selection
7468
7469 GNU extensions:
7470
7471 primary-expression:
7472 __func__
7473 (treated as a keyword in GNU C)
7474 __FUNCTION__
7475 __PRETTY_FUNCTION__
7476 ( compound-statement )
7477 __builtin_va_arg ( assignment-expression , type-name )
7478 __builtin_offsetof ( type-name , offsetof-member-designator )
7479 __builtin_choose_expr ( assignment-expression ,
7480 assignment-expression ,
7481 assignment-expression )
7482 __builtin_types_compatible_p ( type-name , type-name )
7483 __builtin_complex ( assignment-expression , assignment-expression )
7484 __builtin_shuffle ( assignment-expression , assignment-expression )
7485 __builtin_shuffle ( assignment-expression ,
7486 assignment-expression ,
7487 assignment-expression, )
7488
7489 offsetof-member-designator:
7490 identifier
7491 offsetof-member-designator . identifier
7492 offsetof-member-designator [ expression ]
7493
7494 Objective-C:
7495
7496 primary-expression:
7497 [ objc-receiver objc-message-args ]
7498 @selector ( objc-selector-arg )
7499 @protocol ( identifier )
7500 @encode ( type-name )
7501 objc-string-literal
7502 Classname . identifier
7503 */
7504
7505 static struct c_expr
7506 c_parser_postfix_expression (c_parser *parser)
7507 {
7508 struct c_expr expr, e1;
7509 struct c_type_name *t1, *t2;
7510 location_t loc = c_parser_peek_token (parser)->location;;
7511 source_range tok_range = c_parser_peek_token (parser)->get_range ();
7512 expr.original_code = ERROR_MARK;
7513 expr.original_type = NULL;
7514 switch (c_parser_peek_token (parser)->type)
7515 {
7516 case CPP_NUMBER:
7517 expr.value = c_parser_peek_token (parser)->value;
7518 set_c_expr_source_range (&expr, tok_range);
7519 loc = c_parser_peek_token (parser)->location;
7520 c_parser_consume_token (parser);
7521 if (TREE_CODE (expr.value) == FIXED_CST
7522 && !targetm.fixed_point_supported_p ())
7523 {
7524 error_at (loc, "fixed-point types not supported for this target");
7525 expr.value = error_mark_node;
7526 }
7527 break;
7528 case CPP_CHAR:
7529 case CPP_CHAR16:
7530 case CPP_CHAR32:
7531 case CPP_WCHAR:
7532 expr.value = c_parser_peek_token (parser)->value;
7533 set_c_expr_source_range (&expr, tok_range);
7534 c_parser_consume_token (parser);
7535 break;
7536 case CPP_STRING:
7537 case CPP_STRING16:
7538 case CPP_STRING32:
7539 case CPP_WSTRING:
7540 case CPP_UTF8STRING:
7541 expr.value = c_parser_peek_token (parser)->value;
7542 set_c_expr_source_range (&expr, tok_range);
7543 expr.original_code = STRING_CST;
7544 c_parser_consume_token (parser);
7545 break;
7546 case CPP_OBJC_STRING:
7547 gcc_assert (c_dialect_objc ());
7548 expr.value
7549 = objc_build_string_object (c_parser_peek_token (parser)->value);
7550 set_c_expr_source_range (&expr, tok_range);
7551 c_parser_consume_token (parser);
7552 break;
7553 case CPP_NAME:
7554 switch (c_parser_peek_token (parser)->id_kind)
7555 {
7556 case C_ID_ID:
7557 {
7558 tree id = c_parser_peek_token (parser)->value;
7559 c_parser_consume_token (parser);
7560 expr.value = build_external_ref (loc, id,
7561 (c_parser_peek_token (parser)->type
7562 == CPP_OPEN_PAREN),
7563 &expr.original_type);
7564 set_c_expr_source_range (&expr, tok_range);
7565 break;
7566 }
7567 case C_ID_CLASSNAME:
7568 {
7569 /* Here we parse the Objective-C 2.0 Class.name dot
7570 syntax. */
7571 tree class_name = c_parser_peek_token (parser)->value;
7572 tree component;
7573 c_parser_consume_token (parser);
7574 gcc_assert (c_dialect_objc ());
7575 if (!c_parser_require (parser, CPP_DOT, "expected %<.%>"))
7576 {
7577 expr.set_error ();
7578 break;
7579 }
7580 if (c_parser_next_token_is_not (parser, CPP_NAME))
7581 {
7582 c_parser_error (parser, "expected identifier");
7583 expr.set_error ();
7584 break;
7585 }
7586 c_token *component_tok = c_parser_peek_token (parser);
7587 component = component_tok->value;
7588 location_t end_loc = component_tok->get_finish ();
7589 c_parser_consume_token (parser);
7590 expr.value = objc_build_class_component_ref (class_name,
7591 component);
7592 set_c_expr_source_range (&expr, loc, end_loc);
7593 break;
7594 }
7595 default:
7596 c_parser_error (parser, "expected expression");
7597 expr.set_error ();
7598 break;
7599 }
7600 break;
7601 case CPP_OPEN_PAREN:
7602 /* A parenthesized expression, statement expression or compound
7603 literal. */
7604 if (c_parser_peek_2nd_token (parser)->type == CPP_OPEN_BRACE)
7605 {
7606 /* A statement expression. */
7607 tree stmt;
7608 location_t brace_loc;
7609 c_parser_consume_token (parser);
7610 brace_loc = c_parser_peek_token (parser)->location;
7611 c_parser_consume_token (parser);
7612 if (!building_stmt_list_p ())
7613 {
7614 error_at (loc, "braced-group within expression allowed "
7615 "only inside a function");
7616 parser->error = true;
7617 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
7618 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7619 expr.set_error ();
7620 break;
7621 }
7622 stmt = c_begin_stmt_expr ();
7623 c_parser_compound_statement_nostart (parser);
7624 location_t close_loc = c_parser_peek_token (parser)->location;
7625 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7626 "expected %<)%>");
7627 pedwarn (loc, OPT_Wpedantic,
7628 "ISO C forbids braced-groups within expressions");
7629 expr.value = c_finish_stmt_expr (brace_loc, stmt);
7630 set_c_expr_source_range (&expr, loc, close_loc);
7631 mark_exp_read (expr.value);
7632 }
7633 else if (c_token_starts_typename (c_parser_peek_2nd_token (parser)))
7634 {
7635 /* A compound literal. ??? Can we actually get here rather
7636 than going directly to
7637 c_parser_postfix_expression_after_paren_type from
7638 elsewhere? */
7639 location_t loc;
7640 struct c_type_name *type_name;
7641 c_parser_consume_token (parser);
7642 loc = c_parser_peek_token (parser)->location;
7643 type_name = c_parser_type_name (parser);
7644 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7645 "expected %<)%>");
7646 if (type_name == NULL)
7647 {
7648 expr.set_error ();
7649 }
7650 else
7651 expr = c_parser_postfix_expression_after_paren_type (parser,
7652 type_name,
7653 loc);
7654 }
7655 else
7656 {
7657 /* A parenthesized expression. */
7658 location_t loc_open_paren = c_parser_peek_token (parser)->location;
7659 c_parser_consume_token (parser);
7660 expr = c_parser_expression (parser);
7661 if (TREE_CODE (expr.value) == MODIFY_EXPR)
7662 TREE_NO_WARNING (expr.value) = 1;
7663 if (expr.original_code != C_MAYBE_CONST_EXPR)
7664 expr.original_code = ERROR_MARK;
7665 /* Don't change EXPR.ORIGINAL_TYPE. */
7666 location_t loc_close_paren = c_parser_peek_token (parser)->location;
7667 set_c_expr_source_range (&expr, loc_open_paren, loc_close_paren);
7668 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7669 "expected %<)%>");
7670 }
7671 break;
7672 case CPP_KEYWORD:
7673 switch (c_parser_peek_token (parser)->keyword)
7674 {
7675 case RID_FUNCTION_NAME:
7676 pedwarn (loc, OPT_Wpedantic, "ISO C does not support "
7677 "%<__FUNCTION__%> predefined identifier");
7678 expr.value = fname_decl (loc,
7679 c_parser_peek_token (parser)->keyword,
7680 c_parser_peek_token (parser)->value);
7681 set_c_expr_source_range (&expr, loc, loc);
7682 c_parser_consume_token (parser);
7683 break;
7684 case RID_PRETTY_FUNCTION_NAME:
7685 pedwarn (loc, OPT_Wpedantic, "ISO C does not support "
7686 "%<__PRETTY_FUNCTION__%> predefined identifier");
7687 expr.value = fname_decl (loc,
7688 c_parser_peek_token (parser)->keyword,
7689 c_parser_peek_token (parser)->value);
7690 set_c_expr_source_range (&expr, loc, loc);
7691 c_parser_consume_token (parser);
7692 break;
7693 case RID_C99_FUNCTION_NAME:
7694 pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not support "
7695 "%<__func__%> predefined identifier");
7696 expr.value = fname_decl (loc,
7697 c_parser_peek_token (parser)->keyword,
7698 c_parser_peek_token (parser)->value);
7699 set_c_expr_source_range (&expr, loc, loc);
7700 c_parser_consume_token (parser);
7701 break;
7702 case RID_VA_ARG:
7703 {
7704 location_t start_loc = loc;
7705 c_parser_consume_token (parser);
7706 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7707 {
7708 expr.set_error ();
7709 break;
7710 }
7711 e1 = c_parser_expr_no_commas (parser, NULL);
7712 mark_exp_read (e1.value);
7713 e1.value = c_fully_fold (e1.value, false, NULL);
7714 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
7715 {
7716 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7717 expr.set_error ();
7718 break;
7719 }
7720 loc = c_parser_peek_token (parser)->location;
7721 t1 = c_parser_type_name (parser);
7722 location_t end_loc = c_parser_peek_token (parser)->get_finish ();
7723 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7724 "expected %<)%>");
7725 if (t1 == NULL)
7726 {
7727 expr.set_error ();
7728 }
7729 else
7730 {
7731 tree type_expr = NULL_TREE;
7732 expr.value = c_build_va_arg (start_loc, e1.value, loc,
7733 groktypename (t1, &type_expr, NULL));
7734 if (type_expr)
7735 {
7736 expr.value = build2 (C_MAYBE_CONST_EXPR,
7737 TREE_TYPE (expr.value), type_expr,
7738 expr.value);
7739 C_MAYBE_CONST_EXPR_NON_CONST (expr.value) = true;
7740 }
7741 set_c_expr_source_range (&expr, start_loc, end_loc);
7742 }
7743 }
7744 break;
7745 case RID_OFFSETOF:
7746 c_parser_consume_token (parser);
7747 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7748 {
7749 expr.set_error ();
7750 break;
7751 }
7752 t1 = c_parser_type_name (parser);
7753 if (t1 == NULL)
7754 parser->error = true;
7755 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
7756 gcc_assert (parser->error);
7757 if (parser->error)
7758 {
7759 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7760 expr.set_error ();
7761 break;
7762 }
7763
7764 {
7765 tree type = groktypename (t1, NULL, NULL);
7766 tree offsetof_ref;
7767 if (type == error_mark_node)
7768 offsetof_ref = error_mark_node;
7769 else
7770 {
7771 offsetof_ref = build1 (INDIRECT_REF, type, null_pointer_node);
7772 SET_EXPR_LOCATION (offsetof_ref, loc);
7773 }
7774 /* Parse the second argument to __builtin_offsetof. We
7775 must have one identifier, and beyond that we want to
7776 accept sub structure and sub array references. */
7777 if (c_parser_next_token_is (parser, CPP_NAME))
7778 {
7779 c_token *comp_tok = c_parser_peek_token (parser);
7780 offsetof_ref = build_component_ref
7781 (loc, offsetof_ref, comp_tok->value, comp_tok->location);
7782 c_parser_consume_token (parser);
7783 while (c_parser_next_token_is (parser, CPP_DOT)
7784 || c_parser_next_token_is (parser,
7785 CPP_OPEN_SQUARE)
7786 || c_parser_next_token_is (parser,
7787 CPP_DEREF))
7788 {
7789 if (c_parser_next_token_is (parser, CPP_DEREF))
7790 {
7791 loc = c_parser_peek_token (parser)->location;
7792 offsetof_ref = build_array_ref (loc,
7793 offsetof_ref,
7794 integer_zero_node);
7795 goto do_dot;
7796 }
7797 else if (c_parser_next_token_is (parser, CPP_DOT))
7798 {
7799 do_dot:
7800 c_parser_consume_token (parser);
7801 if (c_parser_next_token_is_not (parser,
7802 CPP_NAME))
7803 {
7804 c_parser_error (parser, "expected identifier");
7805 break;
7806 }
7807 c_token *comp_tok = c_parser_peek_token (parser);
7808 offsetof_ref = build_component_ref
7809 (loc, offsetof_ref, comp_tok->value,
7810 comp_tok->location);
7811 c_parser_consume_token (parser);
7812 }
7813 else
7814 {
7815 struct c_expr ce;
7816 tree idx;
7817 loc = c_parser_peek_token (parser)->location;
7818 c_parser_consume_token (parser);
7819 ce = c_parser_expression (parser);
7820 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
7821 idx = ce.value;
7822 idx = c_fully_fold (idx, false, NULL);
7823 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
7824 "expected %<]%>");
7825 offsetof_ref = build_array_ref (loc, offsetof_ref, idx);
7826 }
7827 }
7828 }
7829 else
7830 c_parser_error (parser, "expected identifier");
7831 location_t end_loc = c_parser_peek_token (parser)->get_finish ();
7832 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7833 "expected %<)%>");
7834 expr.value = fold_offsetof (offsetof_ref);
7835 set_c_expr_source_range (&expr, loc, end_loc);
7836 }
7837 break;
7838 case RID_CHOOSE_EXPR:
7839 {
7840 vec<c_expr_t, va_gc> *cexpr_list;
7841 c_expr_t *e1_p, *e2_p, *e3_p;
7842 tree c;
7843 location_t close_paren_loc;
7844
7845 c_parser_consume_token (parser);
7846 if (!c_parser_get_builtin_args (parser,
7847 "__builtin_choose_expr",
7848 &cexpr_list, true,
7849 &close_paren_loc))
7850 {
7851 expr.set_error ();
7852 break;
7853 }
7854
7855 if (vec_safe_length (cexpr_list) != 3)
7856 {
7857 error_at (loc, "wrong number of arguments to "
7858 "%<__builtin_choose_expr%>");
7859 expr.set_error ();
7860 break;
7861 }
7862
7863 e1_p = &(*cexpr_list)[0];
7864 e2_p = &(*cexpr_list)[1];
7865 e3_p = &(*cexpr_list)[2];
7866
7867 c = e1_p->value;
7868 mark_exp_read (e2_p->value);
7869 mark_exp_read (e3_p->value);
7870 if (TREE_CODE (c) != INTEGER_CST
7871 || !INTEGRAL_TYPE_P (TREE_TYPE (c)))
7872 error_at (loc,
7873 "first argument to %<__builtin_choose_expr%> not"
7874 " a constant");
7875 constant_expression_warning (c);
7876 expr = integer_zerop (c) ? *e3_p : *e2_p;
7877 set_c_expr_source_range (&expr, loc, close_paren_loc);
7878 break;
7879 }
7880 case RID_TYPES_COMPATIBLE_P:
7881 c_parser_consume_token (parser);
7882 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7883 {
7884 expr.set_error ();
7885 break;
7886 }
7887 t1 = c_parser_type_name (parser);
7888 if (t1 == NULL)
7889 {
7890 expr.set_error ();
7891 break;
7892 }
7893 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
7894 {
7895 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7896 expr.set_error ();
7897 break;
7898 }
7899 t2 = c_parser_type_name (parser);
7900 if (t2 == NULL)
7901 {
7902 expr.set_error ();
7903 break;
7904 }
7905 {
7906 location_t close_paren_loc = c_parser_peek_token (parser)->location;
7907 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7908 "expected %<)%>");
7909 tree e1, e2;
7910 e1 = groktypename (t1, NULL, NULL);
7911 e2 = groktypename (t2, NULL, NULL);
7912 if (e1 == error_mark_node || e2 == error_mark_node)
7913 {
7914 expr.set_error ();
7915 break;
7916 }
7917
7918 e1 = TYPE_MAIN_VARIANT (e1);
7919 e2 = TYPE_MAIN_VARIANT (e2);
7920
7921 expr.value
7922 = comptypes (e1, e2) ? integer_one_node : integer_zero_node;
7923 set_c_expr_source_range (&expr, loc, close_paren_loc);
7924 }
7925 break;
7926 case RID_BUILTIN_CALL_WITH_STATIC_CHAIN:
7927 {
7928 vec<c_expr_t, va_gc> *cexpr_list;
7929 c_expr_t *e2_p;
7930 tree chain_value;
7931 location_t close_paren_loc;
7932
7933 c_parser_consume_token (parser);
7934 if (!c_parser_get_builtin_args (parser,
7935 "__builtin_call_with_static_chain",
7936 &cexpr_list, false,
7937 &close_paren_loc))
7938 {
7939 expr.set_error ();
7940 break;
7941 }
7942 if (vec_safe_length (cexpr_list) != 2)
7943 {
7944 error_at (loc, "wrong number of arguments to "
7945 "%<__builtin_call_with_static_chain%>");
7946 expr.set_error ();
7947 break;
7948 }
7949
7950 expr = (*cexpr_list)[0];
7951 e2_p = &(*cexpr_list)[1];
7952 *e2_p = convert_lvalue_to_rvalue (loc, *e2_p, true, true);
7953 chain_value = e2_p->value;
7954 mark_exp_read (chain_value);
7955
7956 if (TREE_CODE (expr.value) != CALL_EXPR)
7957 error_at (loc, "first argument to "
7958 "%<__builtin_call_with_static_chain%> "
7959 "must be a call expression");
7960 else if (TREE_CODE (TREE_TYPE (chain_value)) != POINTER_TYPE)
7961 error_at (loc, "second argument to "
7962 "%<__builtin_call_with_static_chain%> "
7963 "must be a pointer type");
7964 else
7965 CALL_EXPR_STATIC_CHAIN (expr.value) = chain_value;
7966 set_c_expr_source_range (&expr, loc, close_paren_loc);
7967 break;
7968 }
7969 case RID_BUILTIN_COMPLEX:
7970 {
7971 vec<c_expr_t, va_gc> *cexpr_list;
7972 c_expr_t *e1_p, *e2_p;
7973 location_t close_paren_loc;
7974
7975 c_parser_consume_token (parser);
7976 if (!c_parser_get_builtin_args (parser,
7977 "__builtin_complex",
7978 &cexpr_list, false,
7979 &close_paren_loc))
7980 {
7981 expr.set_error ();
7982 break;
7983 }
7984
7985 if (vec_safe_length (cexpr_list) != 2)
7986 {
7987 error_at (loc, "wrong number of arguments to "
7988 "%<__builtin_complex%>");
7989 expr.set_error ();
7990 break;
7991 }
7992
7993 e1_p = &(*cexpr_list)[0];
7994 e2_p = &(*cexpr_list)[1];
7995
7996 *e1_p = convert_lvalue_to_rvalue (loc, *e1_p, true, true);
7997 if (TREE_CODE (e1_p->value) == EXCESS_PRECISION_EXPR)
7998 e1_p->value = convert (TREE_TYPE (e1_p->value),
7999 TREE_OPERAND (e1_p->value, 0));
8000 *e2_p = convert_lvalue_to_rvalue (loc, *e2_p, true, true);
8001 if (TREE_CODE (e2_p->value) == EXCESS_PRECISION_EXPR)
8002 e2_p->value = convert (TREE_TYPE (e2_p->value),
8003 TREE_OPERAND (e2_p->value, 0));
8004 if (!SCALAR_FLOAT_TYPE_P (TREE_TYPE (e1_p->value))
8005 || DECIMAL_FLOAT_TYPE_P (TREE_TYPE (e1_p->value))
8006 || !SCALAR_FLOAT_TYPE_P (TREE_TYPE (e2_p->value))
8007 || DECIMAL_FLOAT_TYPE_P (TREE_TYPE (e2_p->value)))
8008 {
8009 error_at (loc, "%<__builtin_complex%> operand "
8010 "not of real binary floating-point type");
8011 expr.set_error ();
8012 break;
8013 }
8014 if (TYPE_MAIN_VARIANT (TREE_TYPE (e1_p->value))
8015 != TYPE_MAIN_VARIANT (TREE_TYPE (e2_p->value)))
8016 {
8017 error_at (loc,
8018 "%<__builtin_complex%> operands of different types");
8019 expr.set_error ();
8020 break;
8021 }
8022 pedwarn_c90 (loc, OPT_Wpedantic,
8023 "ISO C90 does not support complex types");
8024 expr.value = build2_loc (loc, COMPLEX_EXPR,
8025 build_complex_type
8026 (TYPE_MAIN_VARIANT
8027 (TREE_TYPE (e1_p->value))),
8028 e1_p->value, e2_p->value);
8029 set_c_expr_source_range (&expr, loc, close_paren_loc);
8030 break;
8031 }
8032 case RID_BUILTIN_SHUFFLE:
8033 {
8034 vec<c_expr_t, va_gc> *cexpr_list;
8035 unsigned int i;
8036 c_expr_t *p;
8037 location_t close_paren_loc;
8038
8039 c_parser_consume_token (parser);
8040 if (!c_parser_get_builtin_args (parser,
8041 "__builtin_shuffle",
8042 &cexpr_list, false,
8043 &close_paren_loc))
8044 {
8045 expr.set_error ();
8046 break;
8047 }
8048
8049 FOR_EACH_VEC_SAFE_ELT (cexpr_list, i, p)
8050 *p = convert_lvalue_to_rvalue (loc, *p, true, true);
8051
8052 if (vec_safe_length (cexpr_list) == 2)
8053 expr.value =
8054 c_build_vec_perm_expr
8055 (loc, (*cexpr_list)[0].value,
8056 NULL_TREE, (*cexpr_list)[1].value);
8057
8058 else if (vec_safe_length (cexpr_list) == 3)
8059 expr.value =
8060 c_build_vec_perm_expr
8061 (loc, (*cexpr_list)[0].value,
8062 (*cexpr_list)[1].value,
8063 (*cexpr_list)[2].value);
8064 else
8065 {
8066 error_at (loc, "wrong number of arguments to "
8067 "%<__builtin_shuffle%>");
8068 expr.set_error ();
8069 }
8070 set_c_expr_source_range (&expr, loc, close_paren_loc);
8071 break;
8072 }
8073 case RID_AT_SELECTOR:
8074 gcc_assert (c_dialect_objc ());
8075 c_parser_consume_token (parser);
8076 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8077 {
8078 expr.set_error ();
8079 break;
8080 }
8081 {
8082 tree sel = c_parser_objc_selector_arg (parser);
8083 location_t close_loc = c_parser_peek_token (parser)->location;
8084 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
8085 "expected %<)%>");
8086 expr.value = objc_build_selector_expr (loc, sel);
8087 set_c_expr_source_range (&expr, loc, close_loc);
8088 }
8089 break;
8090 case RID_AT_PROTOCOL:
8091 gcc_assert (c_dialect_objc ());
8092 c_parser_consume_token (parser);
8093 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8094 {
8095 expr.set_error ();
8096 break;
8097 }
8098 if (c_parser_next_token_is_not (parser, CPP_NAME))
8099 {
8100 c_parser_error (parser, "expected identifier");
8101 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
8102 expr.set_error ();
8103 break;
8104 }
8105 {
8106 tree id = c_parser_peek_token (parser)->value;
8107 c_parser_consume_token (parser);
8108 location_t close_loc = c_parser_peek_token (parser)->location;
8109 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
8110 "expected %<)%>");
8111 expr.value = objc_build_protocol_expr (id);
8112 set_c_expr_source_range (&expr, loc, close_loc);
8113 }
8114 break;
8115 case RID_AT_ENCODE:
8116 /* Extension to support C-structures in the archiver. */
8117 gcc_assert (c_dialect_objc ());
8118 c_parser_consume_token (parser);
8119 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8120 {
8121 expr.set_error ();
8122 break;
8123 }
8124 t1 = c_parser_type_name (parser);
8125 if (t1 == NULL)
8126 {
8127 expr.set_error ();
8128 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
8129 break;
8130 }
8131 {
8132 location_t close_loc = c_parser_peek_token (parser)->location;
8133 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
8134 "expected %<)%>");
8135 tree type = groktypename (t1, NULL, NULL);
8136 expr.value = objc_build_encode_expr (type);
8137 set_c_expr_source_range (&expr, loc, close_loc);
8138 }
8139 break;
8140 case RID_GENERIC:
8141 expr = c_parser_generic_selection (parser);
8142 break;
8143 case RID_CILK_SPAWN:
8144 c_parser_consume_token (parser);
8145 if (!flag_cilkplus)
8146 {
8147 error_at (loc, "-fcilkplus must be enabled to use "
8148 "%<_Cilk_spawn%>");
8149 expr = c_parser_cast_expression (parser, NULL);
8150 expr.set_error ();
8151 }
8152 else if (c_parser_peek_token (parser)->keyword == RID_CILK_SPAWN)
8153 {
8154 error_at (loc, "consecutive %<_Cilk_spawn%> keywords "
8155 "are not permitted");
8156 /* Now flush out all the _Cilk_spawns. */
8157 while (c_parser_peek_token (parser)->keyword == RID_CILK_SPAWN)
8158 c_parser_consume_token (parser);
8159 expr = c_parser_cast_expression (parser, NULL);
8160 }
8161 else
8162 {
8163 expr = c_parser_cast_expression (parser, NULL);
8164 expr.value = build_cilk_spawn (loc, expr.value);
8165 }
8166 break;
8167 default:
8168 c_parser_error (parser, "expected expression");
8169 expr.set_error ();
8170 break;
8171 }
8172 break;
8173 case CPP_OPEN_SQUARE:
8174 if (c_dialect_objc ())
8175 {
8176 tree receiver, args;
8177 c_parser_consume_token (parser);
8178 receiver = c_parser_objc_receiver (parser);
8179 args = c_parser_objc_message_args (parser);
8180 location_t close_loc = c_parser_peek_token (parser)->location;
8181 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
8182 "expected %<]%>");
8183 expr.value = objc_build_message_expr (receiver, args);
8184 set_c_expr_source_range (&expr, loc, close_loc);
8185 break;
8186 }
8187 /* Else fall through to report error. */
8188 /* FALLTHRU */
8189 default:
8190 c_parser_error (parser, "expected expression");
8191 expr.set_error ();
8192 break;
8193 }
8194 return c_parser_postfix_expression_after_primary
8195 (parser, EXPR_LOC_OR_LOC (expr.value, loc), expr);
8196 }
8197
8198 /* Parse a postfix expression after a parenthesized type name: the
8199 brace-enclosed initializer of a compound literal, possibly followed
8200 by some postfix operators. This is separate because it is not
8201 possible to tell until after the type name whether a cast
8202 expression has a cast or a compound literal, or whether the operand
8203 of sizeof is a parenthesized type name or starts with a compound
8204 literal. TYPE_LOC is the location where TYPE_NAME starts--the
8205 location of the first token after the parentheses around the type
8206 name. */
8207
8208 static struct c_expr
8209 c_parser_postfix_expression_after_paren_type (c_parser *parser,
8210 struct c_type_name *type_name,
8211 location_t type_loc)
8212 {
8213 tree type;
8214 struct c_expr init;
8215 bool non_const;
8216 struct c_expr expr;
8217 location_t start_loc;
8218 tree type_expr = NULL_TREE;
8219 bool type_expr_const = true;
8220 check_compound_literal_type (type_loc, type_name);
8221 start_init (NULL_TREE, NULL, 0);
8222 type = groktypename (type_name, &type_expr, &type_expr_const);
8223 start_loc = c_parser_peek_token (parser)->location;
8224 if (type != error_mark_node && C_TYPE_VARIABLE_SIZE (type))
8225 {
8226 error_at (type_loc, "compound literal has variable size");
8227 type = error_mark_node;
8228 }
8229 init = c_parser_braced_init (parser, type, false, NULL);
8230 finish_init ();
8231 maybe_warn_string_init (type_loc, type, init);
8232
8233 if (type != error_mark_node
8234 && !ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (type))
8235 && current_function_decl)
8236 {
8237 error ("compound literal qualified by address-space qualifier");
8238 type = error_mark_node;
8239 }
8240
8241 pedwarn_c90 (start_loc, OPT_Wpedantic, "ISO C90 forbids compound literals");
8242 non_const = ((init.value && TREE_CODE (init.value) == CONSTRUCTOR)
8243 ? CONSTRUCTOR_NON_CONST (init.value)
8244 : init.original_code == C_MAYBE_CONST_EXPR);
8245 non_const |= !type_expr_const;
8246 expr.value = build_compound_literal (start_loc, type, init.value, non_const);
8247 set_c_expr_source_range (&expr, init.src_range);
8248 expr.original_code = ERROR_MARK;
8249 expr.original_type = NULL;
8250 if (type != error_mark_node
8251 && expr.value != error_mark_node
8252 && type_expr)
8253 {
8254 if (TREE_CODE (expr.value) == C_MAYBE_CONST_EXPR)
8255 {
8256 gcc_assert (C_MAYBE_CONST_EXPR_PRE (expr.value) == NULL_TREE);
8257 C_MAYBE_CONST_EXPR_PRE (expr.value) = type_expr;
8258 }
8259 else
8260 {
8261 gcc_assert (!non_const);
8262 expr.value = build2 (C_MAYBE_CONST_EXPR, type,
8263 type_expr, expr.value);
8264 }
8265 }
8266 return c_parser_postfix_expression_after_primary (parser, start_loc, expr);
8267 }
8268
8269 /* Callback function for sizeof_pointer_memaccess_warning to compare
8270 types. */
8271
8272 static bool
8273 sizeof_ptr_memacc_comptypes (tree type1, tree type2)
8274 {
8275 return comptypes (type1, type2) == 1;
8276 }
8277
8278 /* Parse a postfix expression after the initial primary or compound
8279 literal; that is, parse a series of postfix operators.
8280
8281 EXPR_LOC is the location of the primary expression. */
8282
8283 static struct c_expr
8284 c_parser_postfix_expression_after_primary (c_parser *parser,
8285 location_t expr_loc,
8286 struct c_expr expr)
8287 {
8288 struct c_expr orig_expr;
8289 tree ident, idx;
8290 location_t sizeof_arg_loc[3], comp_loc;
8291 tree sizeof_arg[3];
8292 unsigned int literal_zero_mask;
8293 unsigned int i;
8294 vec<tree, va_gc> *exprlist;
8295 vec<tree, va_gc> *origtypes = NULL;
8296 vec<location_t> arg_loc = vNULL;
8297 location_t start;
8298 location_t finish;
8299
8300 while (true)
8301 {
8302 location_t op_loc = c_parser_peek_token (parser)->location;
8303 switch (c_parser_peek_token (parser)->type)
8304 {
8305 case CPP_OPEN_SQUARE:
8306 /* Array reference. */
8307 c_parser_consume_token (parser);
8308 if (flag_cilkplus
8309 && c_parser_peek_token (parser)->type == CPP_COLON)
8310 /* If we are here, then we have something like this:
8311 Array [ : ]
8312 */
8313 expr.value = c_parser_array_notation (expr_loc, parser, NULL_TREE,
8314 expr.value);
8315 else
8316 {
8317 idx = c_parser_expression (parser).value;
8318 /* Here we have 3 options:
8319 1. Array [EXPR] -- Normal Array call.
8320 2. Array [EXPR : EXPR] -- Array notation without stride.
8321 3. Array [EXPR : EXPR : EXPR] -- Array notation with stride.
8322
8323 For 1, we just handle it just like a normal array expression.
8324 For 2 and 3 we handle it like we handle array notations. The
8325 idx value we have above becomes the initial/start index.
8326 */
8327 if (flag_cilkplus
8328 && c_parser_peek_token (parser)->type == CPP_COLON)
8329 expr.value = c_parser_array_notation (expr_loc, parser, idx,
8330 expr.value);
8331 else
8332 {
8333 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
8334 "expected %<]%>");
8335 start = expr.get_start ();
8336 finish = parser->tokens_buf[0].location;
8337 expr.value = build_array_ref (op_loc, expr.value, idx);
8338 set_c_expr_source_range (&expr, start, finish);
8339 }
8340 }
8341 expr.original_code = ERROR_MARK;
8342 expr.original_type = NULL;
8343 break;
8344 case CPP_OPEN_PAREN:
8345 /* Function call. */
8346 c_parser_consume_token (parser);
8347 for (i = 0; i < 3; i++)
8348 {
8349 sizeof_arg[i] = NULL_TREE;
8350 sizeof_arg_loc[i] = UNKNOWN_LOCATION;
8351 }
8352 literal_zero_mask = 0;
8353 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
8354 exprlist = NULL;
8355 else
8356 exprlist = c_parser_expr_list (parser, true, false, &origtypes,
8357 sizeof_arg_loc, sizeof_arg,
8358 &arg_loc, &literal_zero_mask);
8359 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
8360 "expected %<)%>");
8361 orig_expr = expr;
8362 mark_exp_read (expr.value);
8363 if (warn_sizeof_pointer_memaccess)
8364 sizeof_pointer_memaccess_warning (sizeof_arg_loc,
8365 expr.value, exprlist,
8366 sizeof_arg,
8367 sizeof_ptr_memacc_comptypes);
8368 if (TREE_CODE (expr.value) == FUNCTION_DECL
8369 && DECL_BUILT_IN_CLASS (expr.value) == BUILT_IN_NORMAL
8370 && DECL_FUNCTION_CODE (expr.value) == BUILT_IN_MEMSET
8371 && vec_safe_length (exprlist) == 3)
8372 {
8373 tree arg0 = (*exprlist)[0];
8374 tree arg2 = (*exprlist)[2];
8375 warn_for_memset (expr_loc, arg0, arg2, literal_zero_mask);
8376 }
8377
8378 if (TREE_CODE (expr.value) == FUNCTION_DECL && warn_restrict)
8379 {
8380 unsigned i;
8381 tree arg;
8382 FOR_EACH_VEC_SAFE_ELT (exprlist, i, arg)
8383 TREE_VISITED (arg) = 0;
8384
8385 unsigned param_pos = 0;
8386 function_args_iterator iter;
8387 tree t;
8388 FOREACH_FUNCTION_ARGS (TREE_TYPE (expr.value), t, iter)
8389 {
8390 if (POINTER_TYPE_P (t) && TYPE_RESTRICT (t)
8391 && !TYPE_READONLY (TREE_TYPE (t)))
8392 warn_for_restrict (param_pos, exprlist);
8393 param_pos++;
8394 }
8395
8396 FOR_EACH_VEC_SAFE_ELT (exprlist, i, arg)
8397 TREE_VISITED (arg) = 0;
8398 }
8399
8400 start = expr.get_start ();
8401 finish = parser->tokens_buf[0].get_finish ();
8402 expr.value
8403 = c_build_function_call_vec (expr_loc, arg_loc, expr.value,
8404 exprlist, origtypes);
8405 set_c_expr_source_range (&expr, start, finish);
8406
8407 expr.original_code = ERROR_MARK;
8408 if (TREE_CODE (expr.value) == INTEGER_CST
8409 && TREE_CODE (orig_expr.value) == FUNCTION_DECL
8410 && DECL_BUILT_IN_CLASS (orig_expr.value) == BUILT_IN_NORMAL
8411 && DECL_FUNCTION_CODE (orig_expr.value) == BUILT_IN_CONSTANT_P)
8412 expr.original_code = C_MAYBE_CONST_EXPR;
8413 expr.original_type = NULL;
8414 if (exprlist)
8415 {
8416 release_tree_vector (exprlist);
8417 release_tree_vector (origtypes);
8418 }
8419 arg_loc.release ();
8420 break;
8421 case CPP_DOT:
8422 /* Structure element reference. */
8423 c_parser_consume_token (parser);
8424 expr = default_function_array_conversion (expr_loc, expr);
8425 if (c_parser_next_token_is (parser, CPP_NAME))
8426 {
8427 c_token *comp_tok = c_parser_peek_token (parser);
8428 ident = comp_tok->value;
8429 comp_loc = comp_tok->location;
8430 }
8431 else
8432 {
8433 c_parser_error (parser, "expected identifier");
8434 expr.set_error ();
8435 expr.original_code = ERROR_MARK;
8436 expr.original_type = NULL;
8437 return expr;
8438 }
8439 start = expr.get_start ();
8440 finish = c_parser_peek_token (parser)->get_finish ();
8441 c_parser_consume_token (parser);
8442 expr.value = build_component_ref (op_loc, expr.value, ident,
8443 comp_loc);
8444 set_c_expr_source_range (&expr, start, finish);
8445 expr.original_code = ERROR_MARK;
8446 if (TREE_CODE (expr.value) != COMPONENT_REF)
8447 expr.original_type = NULL;
8448 else
8449 {
8450 /* Remember the original type of a bitfield. */
8451 tree field = TREE_OPERAND (expr.value, 1);
8452 if (TREE_CODE (field) != FIELD_DECL)
8453 expr.original_type = NULL;
8454 else
8455 expr.original_type = DECL_BIT_FIELD_TYPE (field);
8456 }
8457 break;
8458 case CPP_DEREF:
8459 /* Structure element reference. */
8460 c_parser_consume_token (parser);
8461 expr = convert_lvalue_to_rvalue (expr_loc, expr, true, false);
8462 if (c_parser_next_token_is (parser, CPP_NAME))
8463 {
8464 c_token *comp_tok = c_parser_peek_token (parser);
8465 ident = comp_tok->value;
8466 comp_loc = comp_tok->location;
8467 }
8468 else
8469 {
8470 c_parser_error (parser, "expected identifier");
8471 expr.set_error ();
8472 expr.original_code = ERROR_MARK;
8473 expr.original_type = NULL;
8474 return expr;
8475 }
8476 start = expr.get_start ();
8477 finish = c_parser_peek_token (parser)->get_finish ();
8478 c_parser_consume_token (parser);
8479 expr.value = build_component_ref (op_loc,
8480 build_indirect_ref (op_loc,
8481 expr.value,
8482 RO_ARROW),
8483 ident, comp_loc);
8484 set_c_expr_source_range (&expr, start, finish);
8485 expr.original_code = ERROR_MARK;
8486 if (TREE_CODE (expr.value) != COMPONENT_REF)
8487 expr.original_type = NULL;
8488 else
8489 {
8490 /* Remember the original type of a bitfield. */
8491 tree field = TREE_OPERAND (expr.value, 1);
8492 if (TREE_CODE (field) != FIELD_DECL)
8493 expr.original_type = NULL;
8494 else
8495 expr.original_type = DECL_BIT_FIELD_TYPE (field);
8496 }
8497 break;
8498 case CPP_PLUS_PLUS:
8499 /* Postincrement. */
8500 start = expr.get_start ();
8501 finish = c_parser_peek_token (parser)->get_finish ();
8502 c_parser_consume_token (parser);
8503 /* If the expressions have array notations, we expand them. */
8504 if (flag_cilkplus
8505 && TREE_CODE (expr.value) == ARRAY_NOTATION_REF)
8506 expr = fix_array_notation_expr (expr_loc, POSTINCREMENT_EXPR, expr);
8507 else
8508 {
8509 expr = default_function_array_read_conversion (expr_loc, expr);
8510 expr.value = build_unary_op (op_loc, POSTINCREMENT_EXPR,
8511 expr.value, false);
8512 }
8513 set_c_expr_source_range (&expr, start, finish);
8514 expr.original_code = ERROR_MARK;
8515 expr.original_type = NULL;
8516 break;
8517 case CPP_MINUS_MINUS:
8518 /* Postdecrement. */
8519 start = expr.get_start ();
8520 finish = c_parser_peek_token (parser)->get_finish ();
8521 c_parser_consume_token (parser);
8522 /* If the expressions have array notations, we expand them. */
8523 if (flag_cilkplus
8524 && TREE_CODE (expr.value) == ARRAY_NOTATION_REF)
8525 expr = fix_array_notation_expr (expr_loc, POSTDECREMENT_EXPR, expr);
8526 else
8527 {
8528 expr = default_function_array_read_conversion (expr_loc, expr);
8529 expr.value = build_unary_op (op_loc, POSTDECREMENT_EXPR,
8530 expr.value, false);
8531 }
8532 set_c_expr_source_range (&expr, start, finish);
8533 expr.original_code = ERROR_MARK;
8534 expr.original_type = NULL;
8535 break;
8536 default:
8537 return expr;
8538 }
8539 }
8540 }
8541
8542 /* Parse an expression (C90 6.3.17, C99 6.5.17).
8543
8544 expression:
8545 assignment-expression
8546 expression , assignment-expression
8547 */
8548
8549 static struct c_expr
8550 c_parser_expression (c_parser *parser)
8551 {
8552 location_t tloc = c_parser_peek_token (parser)->location;
8553 struct c_expr expr;
8554 expr = c_parser_expr_no_commas (parser, NULL);
8555 if (c_parser_next_token_is (parser, CPP_COMMA))
8556 expr = convert_lvalue_to_rvalue (tloc, expr, true, false);
8557 while (c_parser_next_token_is (parser, CPP_COMMA))
8558 {
8559 struct c_expr next;
8560 tree lhsval;
8561 location_t loc = c_parser_peek_token (parser)->location;
8562 location_t expr_loc;
8563 c_parser_consume_token (parser);
8564 expr_loc = c_parser_peek_token (parser)->location;
8565 lhsval = expr.value;
8566 while (TREE_CODE (lhsval) == COMPOUND_EXPR)
8567 lhsval = TREE_OPERAND (lhsval, 1);
8568 if (DECL_P (lhsval) || handled_component_p (lhsval))
8569 mark_exp_read (lhsval);
8570 next = c_parser_expr_no_commas (parser, NULL);
8571 next = convert_lvalue_to_rvalue (expr_loc, next, true, false);
8572 expr.value = build_compound_expr (loc, expr.value, next.value);
8573 expr.original_code = COMPOUND_EXPR;
8574 expr.original_type = next.original_type;
8575 }
8576 return expr;
8577 }
8578
8579 /* Parse an expression and convert functions or arrays to pointers and
8580 lvalues to rvalues. */
8581
8582 static struct c_expr
8583 c_parser_expression_conv (c_parser *parser)
8584 {
8585 struct c_expr expr;
8586 location_t loc = c_parser_peek_token (parser)->location;
8587 expr = c_parser_expression (parser);
8588 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
8589 return expr;
8590 }
8591
8592 /* Helper function of c_parser_expr_list. Check if IDXth (0 based)
8593 argument is a literal zero alone and if so, set it in literal_zero_mask. */
8594
8595 static inline void
8596 c_parser_check_literal_zero (c_parser *parser, unsigned *literal_zero_mask,
8597 unsigned int idx)
8598 {
8599 if (idx >= HOST_BITS_PER_INT)
8600 return;
8601
8602 c_token *tok = c_parser_peek_token (parser);
8603 switch (tok->type)
8604 {
8605 case CPP_NUMBER:
8606 case CPP_CHAR:
8607 case CPP_WCHAR:
8608 case CPP_CHAR16:
8609 case CPP_CHAR32:
8610 /* If a parameter is literal zero alone, remember it
8611 for -Wmemset-transposed-args warning. */
8612 if (integer_zerop (tok->value)
8613 && !TREE_OVERFLOW (tok->value)
8614 && (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
8615 || c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_PAREN))
8616 *literal_zero_mask |= 1U << idx;
8617 default:
8618 break;
8619 }
8620 }
8621
8622 /* Parse a non-empty list of expressions. If CONVERT_P, convert
8623 functions and arrays to pointers and lvalues to rvalues. If
8624 FOLD_P, fold the expressions. If LOCATIONS is non-NULL, save the
8625 locations of function arguments into this vector.
8626
8627 nonempty-expr-list:
8628 assignment-expression
8629 nonempty-expr-list , assignment-expression
8630 */
8631
8632 static vec<tree, va_gc> *
8633 c_parser_expr_list (c_parser *parser, bool convert_p, bool fold_p,
8634 vec<tree, va_gc> **p_orig_types,
8635 location_t *sizeof_arg_loc, tree *sizeof_arg,
8636 vec<location_t> *locations,
8637 unsigned int *literal_zero_mask)
8638 {
8639 vec<tree, va_gc> *ret;
8640 vec<tree, va_gc> *orig_types;
8641 struct c_expr expr;
8642 location_t loc = c_parser_peek_token (parser)->location;
8643 location_t cur_sizeof_arg_loc = UNKNOWN_LOCATION;
8644 unsigned int idx = 0;
8645
8646 ret = make_tree_vector ();
8647 if (p_orig_types == NULL)
8648 orig_types = NULL;
8649 else
8650 orig_types = make_tree_vector ();
8651
8652 if (sizeof_arg != NULL
8653 && c_parser_next_token_is_keyword (parser, RID_SIZEOF))
8654 cur_sizeof_arg_loc = c_parser_peek_2nd_token (parser)->location;
8655 if (literal_zero_mask)
8656 c_parser_check_literal_zero (parser, literal_zero_mask, 0);
8657 expr = c_parser_expr_no_commas (parser, NULL);
8658 if (convert_p)
8659 expr = convert_lvalue_to_rvalue (loc, expr, true, true);
8660 if (fold_p)
8661 expr.value = c_fully_fold (expr.value, false, NULL);
8662 ret->quick_push (expr.value);
8663 if (orig_types)
8664 orig_types->quick_push (expr.original_type);
8665 if (locations)
8666 locations->safe_push (loc);
8667 if (sizeof_arg != NULL
8668 && cur_sizeof_arg_loc != UNKNOWN_LOCATION
8669 && expr.original_code == SIZEOF_EXPR)
8670 {
8671 sizeof_arg[0] = c_last_sizeof_arg;
8672 sizeof_arg_loc[0] = cur_sizeof_arg_loc;
8673 }
8674 while (c_parser_next_token_is (parser, CPP_COMMA))
8675 {
8676 c_parser_consume_token (parser);
8677 loc = c_parser_peek_token (parser)->location;
8678 if (sizeof_arg != NULL
8679 && c_parser_next_token_is_keyword (parser, RID_SIZEOF))
8680 cur_sizeof_arg_loc = c_parser_peek_2nd_token (parser)->location;
8681 else
8682 cur_sizeof_arg_loc = UNKNOWN_LOCATION;
8683 if (literal_zero_mask)
8684 c_parser_check_literal_zero (parser, literal_zero_mask, idx + 1);
8685 expr = c_parser_expr_no_commas (parser, NULL);
8686 if (convert_p)
8687 expr = convert_lvalue_to_rvalue (loc, expr, true, true);
8688 if (fold_p)
8689 expr.value = c_fully_fold (expr.value, false, NULL);
8690 vec_safe_push (ret, expr.value);
8691 if (orig_types)
8692 vec_safe_push (orig_types, expr.original_type);
8693 if (locations)
8694 locations->safe_push (loc);
8695 if (++idx < 3
8696 && sizeof_arg != NULL
8697 && cur_sizeof_arg_loc != UNKNOWN_LOCATION
8698 && expr.original_code == SIZEOF_EXPR)
8699 {
8700 sizeof_arg[idx] = c_last_sizeof_arg;
8701 sizeof_arg_loc[idx] = cur_sizeof_arg_loc;
8702 }
8703 }
8704 if (orig_types)
8705 *p_orig_types = orig_types;
8706 return ret;
8707 }
8708 \f
8709 /* Parse Objective-C-specific constructs. */
8710
8711 /* Parse an objc-class-definition.
8712
8713 objc-class-definition:
8714 @interface identifier objc-superclass[opt] objc-protocol-refs[opt]
8715 objc-class-instance-variables[opt] objc-methodprotolist @end
8716 @implementation identifier objc-superclass[opt]
8717 objc-class-instance-variables[opt]
8718 @interface identifier ( identifier ) objc-protocol-refs[opt]
8719 objc-methodprotolist @end
8720 @interface identifier ( ) objc-protocol-refs[opt]
8721 objc-methodprotolist @end
8722 @implementation identifier ( identifier )
8723
8724 objc-superclass:
8725 : identifier
8726
8727 "@interface identifier (" must start "@interface identifier (
8728 identifier ) ...": objc-methodprotolist in the first production may
8729 not start with a parenthesized identifier as a declarator of a data
8730 definition with no declaration specifiers if the objc-superclass,
8731 objc-protocol-refs and objc-class-instance-variables are omitted. */
8732
8733 static void
8734 c_parser_objc_class_definition (c_parser *parser, tree attributes)
8735 {
8736 bool iface_p;
8737 tree id1;
8738 tree superclass;
8739 if (c_parser_next_token_is_keyword (parser, RID_AT_INTERFACE))
8740 iface_p = true;
8741 else if (c_parser_next_token_is_keyword (parser, RID_AT_IMPLEMENTATION))
8742 iface_p = false;
8743 else
8744 gcc_unreachable ();
8745
8746 c_parser_consume_token (parser);
8747 if (c_parser_next_token_is_not (parser, CPP_NAME))
8748 {
8749 c_parser_error (parser, "expected identifier");
8750 return;
8751 }
8752 id1 = c_parser_peek_token (parser)->value;
8753 c_parser_consume_token (parser);
8754 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
8755 {
8756 /* We have a category or class extension. */
8757 tree id2;
8758 tree proto = NULL_TREE;
8759 c_parser_consume_token (parser);
8760 if (c_parser_next_token_is_not (parser, CPP_NAME))
8761 {
8762 if (iface_p && c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
8763 {
8764 /* We have a class extension. */
8765 id2 = NULL_TREE;
8766 }
8767 else
8768 {
8769 c_parser_error (parser, "expected identifier or %<)%>");
8770 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
8771 return;
8772 }
8773 }
8774 else
8775 {
8776 id2 = c_parser_peek_token (parser)->value;
8777 c_parser_consume_token (parser);
8778 }
8779 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8780 if (!iface_p)
8781 {
8782 objc_start_category_implementation (id1, id2);
8783 return;
8784 }
8785 if (c_parser_next_token_is (parser, CPP_LESS))
8786 proto = c_parser_objc_protocol_refs (parser);
8787 objc_start_category_interface (id1, id2, proto, attributes);
8788 c_parser_objc_methodprotolist (parser);
8789 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
8790 objc_finish_interface ();
8791 return;
8792 }
8793 if (c_parser_next_token_is (parser, CPP_COLON))
8794 {
8795 c_parser_consume_token (parser);
8796 if (c_parser_next_token_is_not (parser, CPP_NAME))
8797 {
8798 c_parser_error (parser, "expected identifier");
8799 return;
8800 }
8801 superclass = c_parser_peek_token (parser)->value;
8802 c_parser_consume_token (parser);
8803 }
8804 else
8805 superclass = NULL_TREE;
8806 if (iface_p)
8807 {
8808 tree proto = NULL_TREE;
8809 if (c_parser_next_token_is (parser, CPP_LESS))
8810 proto = c_parser_objc_protocol_refs (parser);
8811 objc_start_class_interface (id1, superclass, proto, attributes);
8812 }
8813 else
8814 objc_start_class_implementation (id1, superclass);
8815 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
8816 c_parser_objc_class_instance_variables (parser);
8817 if (iface_p)
8818 {
8819 objc_continue_interface ();
8820 c_parser_objc_methodprotolist (parser);
8821 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
8822 objc_finish_interface ();
8823 }
8824 else
8825 {
8826 objc_continue_implementation ();
8827 return;
8828 }
8829 }
8830
8831 /* Parse objc-class-instance-variables.
8832
8833 objc-class-instance-variables:
8834 { objc-instance-variable-decl-list[opt] }
8835
8836 objc-instance-variable-decl-list:
8837 objc-visibility-spec
8838 objc-instance-variable-decl ;
8839 ;
8840 objc-instance-variable-decl-list objc-visibility-spec
8841 objc-instance-variable-decl-list objc-instance-variable-decl ;
8842 objc-instance-variable-decl-list ;
8843
8844 objc-visibility-spec:
8845 @private
8846 @protected
8847 @public
8848
8849 objc-instance-variable-decl:
8850 struct-declaration
8851 */
8852
8853 static void
8854 c_parser_objc_class_instance_variables (c_parser *parser)
8855 {
8856 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
8857 c_parser_consume_token (parser);
8858 while (c_parser_next_token_is_not (parser, CPP_EOF))
8859 {
8860 tree decls;
8861 /* Parse any stray semicolon. */
8862 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
8863 {
8864 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
8865 "extra semicolon");
8866 c_parser_consume_token (parser);
8867 continue;
8868 }
8869 /* Stop if at the end of the instance variables. */
8870 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
8871 {
8872 c_parser_consume_token (parser);
8873 break;
8874 }
8875 /* Parse any objc-visibility-spec. */
8876 if (c_parser_next_token_is_keyword (parser, RID_AT_PRIVATE))
8877 {
8878 c_parser_consume_token (parser);
8879 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
8880 continue;
8881 }
8882 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROTECTED))
8883 {
8884 c_parser_consume_token (parser);
8885 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
8886 continue;
8887 }
8888 else if (c_parser_next_token_is_keyword (parser, RID_AT_PUBLIC))
8889 {
8890 c_parser_consume_token (parser);
8891 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
8892 continue;
8893 }
8894 else if (c_parser_next_token_is_keyword (parser, RID_AT_PACKAGE))
8895 {
8896 c_parser_consume_token (parser);
8897 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
8898 continue;
8899 }
8900 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
8901 {
8902 c_parser_pragma (parser, pragma_external, NULL);
8903 continue;
8904 }
8905
8906 /* Parse some comma-separated declarations. */
8907 decls = c_parser_struct_declaration (parser);
8908 if (decls == NULL)
8909 {
8910 /* There is a syntax error. We want to skip the offending
8911 tokens up to the next ';' (included) or '}'
8912 (excluded). */
8913
8914 /* First, skip manually a ')' or ']'. This is because they
8915 reduce the nesting level, so c_parser_skip_until_found()
8916 wouldn't be able to skip past them. */
8917 c_token *token = c_parser_peek_token (parser);
8918 if (token->type == CPP_CLOSE_PAREN || token->type == CPP_CLOSE_SQUARE)
8919 c_parser_consume_token (parser);
8920
8921 /* Then, do the standard skipping. */
8922 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8923
8924 /* We hopefully recovered. Start normal parsing again. */
8925 parser->error = false;
8926 continue;
8927 }
8928 else
8929 {
8930 /* Comma-separated instance variables are chained together
8931 in reverse order; add them one by one. */
8932 tree ivar = nreverse (decls);
8933 for (; ivar; ivar = DECL_CHAIN (ivar))
8934 objc_add_instance_variable (copy_node (ivar));
8935 }
8936 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8937 }
8938 }
8939
8940 /* Parse an objc-class-declaration.
8941
8942 objc-class-declaration:
8943 @class identifier-list ;
8944 */
8945
8946 static void
8947 c_parser_objc_class_declaration (c_parser *parser)
8948 {
8949 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_CLASS));
8950 c_parser_consume_token (parser);
8951 /* Any identifiers, including those declared as type names, are OK
8952 here. */
8953 while (true)
8954 {
8955 tree id;
8956 if (c_parser_next_token_is_not (parser, CPP_NAME))
8957 {
8958 c_parser_error (parser, "expected identifier");
8959 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8960 parser->error = false;
8961 return;
8962 }
8963 id = c_parser_peek_token (parser)->value;
8964 objc_declare_class (id);
8965 c_parser_consume_token (parser);
8966 if (c_parser_next_token_is (parser, CPP_COMMA))
8967 c_parser_consume_token (parser);
8968 else
8969 break;
8970 }
8971 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8972 }
8973
8974 /* Parse an objc-alias-declaration.
8975
8976 objc-alias-declaration:
8977 @compatibility_alias identifier identifier ;
8978 */
8979
8980 static void
8981 c_parser_objc_alias_declaration (c_parser *parser)
8982 {
8983 tree id1, id2;
8984 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_ALIAS));
8985 c_parser_consume_token (parser);
8986 if (c_parser_next_token_is_not (parser, CPP_NAME))
8987 {
8988 c_parser_error (parser, "expected identifier");
8989 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8990 return;
8991 }
8992 id1 = c_parser_peek_token (parser)->value;
8993 c_parser_consume_token (parser);
8994 if (c_parser_next_token_is_not (parser, CPP_NAME))
8995 {
8996 c_parser_error (parser, "expected identifier");
8997 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8998 return;
8999 }
9000 id2 = c_parser_peek_token (parser)->value;
9001 c_parser_consume_token (parser);
9002 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9003 objc_declare_alias (id1, id2);
9004 }
9005
9006 /* Parse an objc-protocol-definition.
9007
9008 objc-protocol-definition:
9009 @protocol identifier objc-protocol-refs[opt] objc-methodprotolist @end
9010 @protocol identifier-list ;
9011
9012 "@protocol identifier ;" should be resolved as "@protocol
9013 identifier-list ;": objc-methodprotolist may not start with a
9014 semicolon in the first alternative if objc-protocol-refs are
9015 omitted. */
9016
9017 static void
9018 c_parser_objc_protocol_definition (c_parser *parser, tree attributes)
9019 {
9020 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROTOCOL));
9021
9022 c_parser_consume_token (parser);
9023 if (c_parser_next_token_is_not (parser, CPP_NAME))
9024 {
9025 c_parser_error (parser, "expected identifier");
9026 return;
9027 }
9028 if (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
9029 || c_parser_peek_2nd_token (parser)->type == CPP_SEMICOLON)
9030 {
9031 /* Any identifiers, including those declared as type names, are
9032 OK here. */
9033 while (true)
9034 {
9035 tree id;
9036 if (c_parser_next_token_is_not (parser, CPP_NAME))
9037 {
9038 c_parser_error (parser, "expected identifier");
9039 break;
9040 }
9041 id = c_parser_peek_token (parser)->value;
9042 objc_declare_protocol (id, attributes);
9043 c_parser_consume_token (parser);
9044 if (c_parser_next_token_is (parser, CPP_COMMA))
9045 c_parser_consume_token (parser);
9046 else
9047 break;
9048 }
9049 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9050 }
9051 else
9052 {
9053 tree id = c_parser_peek_token (parser)->value;
9054 tree proto = NULL_TREE;
9055 c_parser_consume_token (parser);
9056 if (c_parser_next_token_is (parser, CPP_LESS))
9057 proto = c_parser_objc_protocol_refs (parser);
9058 parser->objc_pq_context = true;
9059 objc_start_protocol (id, proto, attributes);
9060 c_parser_objc_methodprotolist (parser);
9061 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
9062 parser->objc_pq_context = false;
9063 objc_finish_interface ();
9064 }
9065 }
9066
9067 /* Parse an objc-method-type.
9068
9069 objc-method-type:
9070 +
9071 -
9072
9073 Return true if it is a class method (+) and false if it is
9074 an instance method (-).
9075 */
9076 static inline bool
9077 c_parser_objc_method_type (c_parser *parser)
9078 {
9079 switch (c_parser_peek_token (parser)->type)
9080 {
9081 case CPP_PLUS:
9082 c_parser_consume_token (parser);
9083 return true;
9084 case CPP_MINUS:
9085 c_parser_consume_token (parser);
9086 return false;
9087 default:
9088 gcc_unreachable ();
9089 }
9090 }
9091
9092 /* Parse an objc-method-definition.
9093
9094 objc-method-definition:
9095 objc-method-type objc-method-decl ;[opt] compound-statement
9096 */
9097
9098 static void
9099 c_parser_objc_method_definition (c_parser *parser)
9100 {
9101 bool is_class_method = c_parser_objc_method_type (parser);
9102 tree decl, attributes = NULL_TREE, expr = NULL_TREE;
9103 parser->objc_pq_context = true;
9104 decl = c_parser_objc_method_decl (parser, is_class_method, &attributes,
9105 &expr);
9106 if (decl == error_mark_node)
9107 return; /* Bail here. */
9108
9109 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
9110 {
9111 c_parser_consume_token (parser);
9112 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
9113 "extra semicolon in method definition specified");
9114 }
9115
9116 if (!c_parser_next_token_is (parser, CPP_OPEN_BRACE))
9117 {
9118 c_parser_error (parser, "expected %<{%>");
9119 return;
9120 }
9121
9122 parser->objc_pq_context = false;
9123 if (objc_start_method_definition (is_class_method, decl, attributes, expr))
9124 {
9125 add_stmt (c_parser_compound_statement (parser));
9126 objc_finish_method_definition (current_function_decl);
9127 }
9128 else
9129 {
9130 /* This code is executed when we find a method definition
9131 outside of an @implementation context (or invalid for other
9132 reasons). Parse the method (to keep going) but do not emit
9133 any code.
9134 */
9135 c_parser_compound_statement (parser);
9136 }
9137 }
9138
9139 /* Parse an objc-methodprotolist.
9140
9141 objc-methodprotolist:
9142 empty
9143 objc-methodprotolist objc-methodproto
9144 objc-methodprotolist declaration
9145 objc-methodprotolist ;
9146 @optional
9147 @required
9148
9149 The declaration is a data definition, which may be missing
9150 declaration specifiers under the same rules and diagnostics as
9151 other data definitions outside functions, and the stray semicolon
9152 is diagnosed the same way as a stray semicolon outside a
9153 function. */
9154
9155 static void
9156 c_parser_objc_methodprotolist (c_parser *parser)
9157 {
9158 while (true)
9159 {
9160 /* The list is terminated by @end. */
9161 switch (c_parser_peek_token (parser)->type)
9162 {
9163 case CPP_SEMICOLON:
9164 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
9165 "ISO C does not allow extra %<;%> outside of a function");
9166 c_parser_consume_token (parser);
9167 break;
9168 case CPP_PLUS:
9169 case CPP_MINUS:
9170 c_parser_objc_methodproto (parser);
9171 break;
9172 case CPP_PRAGMA:
9173 c_parser_pragma (parser, pragma_external, NULL);
9174 break;
9175 case CPP_EOF:
9176 return;
9177 default:
9178 if (c_parser_next_token_is_keyword (parser, RID_AT_END))
9179 return;
9180 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROPERTY))
9181 c_parser_objc_at_property_declaration (parser);
9182 else if (c_parser_next_token_is_keyword (parser, RID_AT_OPTIONAL))
9183 {
9184 objc_set_method_opt (true);
9185 c_parser_consume_token (parser);
9186 }
9187 else if (c_parser_next_token_is_keyword (parser, RID_AT_REQUIRED))
9188 {
9189 objc_set_method_opt (false);
9190 c_parser_consume_token (parser);
9191 }
9192 else
9193 c_parser_declaration_or_fndef (parser, false, false, true,
9194 false, true, NULL, vNULL);
9195 break;
9196 }
9197 }
9198 }
9199
9200 /* Parse an objc-methodproto.
9201
9202 objc-methodproto:
9203 objc-method-type objc-method-decl ;
9204 */
9205
9206 static void
9207 c_parser_objc_methodproto (c_parser *parser)
9208 {
9209 bool is_class_method = c_parser_objc_method_type (parser);
9210 tree decl, attributes = NULL_TREE;
9211
9212 /* Remember protocol qualifiers in prototypes. */
9213 parser->objc_pq_context = true;
9214 decl = c_parser_objc_method_decl (parser, is_class_method, &attributes,
9215 NULL);
9216 /* Forget protocol qualifiers now. */
9217 parser->objc_pq_context = false;
9218
9219 /* Do not allow the presence of attributes to hide an erroneous
9220 method implementation in the interface section. */
9221 if (!c_parser_next_token_is (parser, CPP_SEMICOLON))
9222 {
9223 c_parser_error (parser, "expected %<;%>");
9224 return;
9225 }
9226
9227 if (decl != error_mark_node)
9228 objc_add_method_declaration (is_class_method, decl, attributes);
9229
9230 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9231 }
9232
9233 /* If we are at a position that method attributes may be present, check that
9234 there are not any parsed already (a syntax error) and then collect any
9235 specified at the current location. Finally, if new attributes were present,
9236 check that the next token is legal ( ';' for decls and '{' for defs). */
9237
9238 static bool
9239 c_parser_objc_maybe_method_attributes (c_parser* parser, tree* attributes)
9240 {
9241 bool bad = false;
9242 if (*attributes)
9243 {
9244 c_parser_error (parser,
9245 "method attributes must be specified at the end only");
9246 *attributes = NULL_TREE;
9247 bad = true;
9248 }
9249
9250 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
9251 *attributes = c_parser_attributes (parser);
9252
9253 /* If there were no attributes here, just report any earlier error. */
9254 if (*attributes == NULL_TREE || bad)
9255 return bad;
9256
9257 /* If the attributes are followed by a ; or {, then just report any earlier
9258 error. */
9259 if (c_parser_next_token_is (parser, CPP_SEMICOLON)
9260 || c_parser_next_token_is (parser, CPP_OPEN_BRACE))
9261 return bad;
9262
9263 /* We've got attributes, but not at the end. */
9264 c_parser_error (parser,
9265 "expected %<;%> or %<{%> after method attribute definition");
9266 return true;
9267 }
9268
9269 /* Parse an objc-method-decl.
9270
9271 objc-method-decl:
9272 ( objc-type-name ) objc-selector
9273 objc-selector
9274 ( objc-type-name ) objc-keyword-selector objc-optparmlist
9275 objc-keyword-selector objc-optparmlist
9276 attributes
9277
9278 objc-keyword-selector:
9279 objc-keyword-decl
9280 objc-keyword-selector objc-keyword-decl
9281
9282 objc-keyword-decl:
9283 objc-selector : ( objc-type-name ) identifier
9284 objc-selector : identifier
9285 : ( objc-type-name ) identifier
9286 : identifier
9287
9288 objc-optparmlist:
9289 objc-optparms objc-optellipsis
9290
9291 objc-optparms:
9292 empty
9293 objc-opt-parms , parameter-declaration
9294
9295 objc-optellipsis:
9296 empty
9297 , ...
9298 */
9299
9300 static tree
9301 c_parser_objc_method_decl (c_parser *parser, bool is_class_method,
9302 tree *attributes, tree *expr)
9303 {
9304 tree type = NULL_TREE;
9305 tree sel;
9306 tree parms = NULL_TREE;
9307 bool ellipsis = false;
9308 bool attr_err = false;
9309
9310 *attributes = NULL_TREE;
9311 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
9312 {
9313 c_parser_consume_token (parser);
9314 type = c_parser_objc_type_name (parser);
9315 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9316 }
9317 sel = c_parser_objc_selector (parser);
9318 /* If there is no selector, or a colon follows, we have an
9319 objc-keyword-selector. If there is a selector, and a colon does
9320 not follow, that selector ends the objc-method-decl. */
9321 if (!sel || c_parser_next_token_is (parser, CPP_COLON))
9322 {
9323 tree tsel = sel;
9324 tree list = NULL_TREE;
9325 while (true)
9326 {
9327 tree atype = NULL_TREE, id, keyworddecl;
9328 tree param_attr = NULL_TREE;
9329 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
9330 break;
9331 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
9332 {
9333 c_parser_consume_token (parser);
9334 atype = c_parser_objc_type_name (parser);
9335 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
9336 "expected %<)%>");
9337 }
9338 /* New ObjC allows attributes on method parameters. */
9339 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
9340 param_attr = c_parser_attributes (parser);
9341 if (c_parser_next_token_is_not (parser, CPP_NAME))
9342 {
9343 c_parser_error (parser, "expected identifier");
9344 return error_mark_node;
9345 }
9346 id = c_parser_peek_token (parser)->value;
9347 c_parser_consume_token (parser);
9348 keyworddecl = objc_build_keyword_decl (tsel, atype, id, param_attr);
9349 list = chainon (list, keyworddecl);
9350 tsel = c_parser_objc_selector (parser);
9351 if (!tsel && c_parser_next_token_is_not (parser, CPP_COLON))
9352 break;
9353 }
9354
9355 attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
9356
9357 /* Parse the optional parameter list. Optional Objective-C
9358 method parameters follow the C syntax, and may include '...'
9359 to denote a variable number of arguments. */
9360 parms = make_node (TREE_LIST);
9361 while (c_parser_next_token_is (parser, CPP_COMMA))
9362 {
9363 struct c_parm *parm;
9364 c_parser_consume_token (parser);
9365 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
9366 {
9367 ellipsis = true;
9368 c_parser_consume_token (parser);
9369 attr_err |= c_parser_objc_maybe_method_attributes
9370 (parser, attributes) ;
9371 break;
9372 }
9373 parm = c_parser_parameter_declaration (parser, NULL_TREE);
9374 if (parm == NULL)
9375 break;
9376 parms = chainon (parms,
9377 build_tree_list (NULL_TREE, grokparm (parm, expr)));
9378 }
9379 sel = list;
9380 }
9381 else
9382 attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
9383
9384 if (sel == NULL)
9385 {
9386 c_parser_error (parser, "objective-c method declaration is expected");
9387 return error_mark_node;
9388 }
9389
9390 if (attr_err)
9391 return error_mark_node;
9392
9393 return objc_build_method_signature (is_class_method, type, sel, parms, ellipsis);
9394 }
9395
9396 /* Parse an objc-type-name.
9397
9398 objc-type-name:
9399 objc-type-qualifiers[opt] type-name
9400 objc-type-qualifiers[opt]
9401
9402 objc-type-qualifiers:
9403 objc-type-qualifier
9404 objc-type-qualifiers objc-type-qualifier
9405
9406 objc-type-qualifier: one of
9407 in out inout bycopy byref oneway
9408 */
9409
9410 static tree
9411 c_parser_objc_type_name (c_parser *parser)
9412 {
9413 tree quals = NULL_TREE;
9414 struct c_type_name *type_name = NULL;
9415 tree type = NULL_TREE;
9416 while (true)
9417 {
9418 c_token *token = c_parser_peek_token (parser);
9419 if (token->type == CPP_KEYWORD
9420 && (token->keyword == RID_IN
9421 || token->keyword == RID_OUT
9422 || token->keyword == RID_INOUT
9423 || token->keyword == RID_BYCOPY
9424 || token->keyword == RID_BYREF
9425 || token->keyword == RID_ONEWAY))
9426 {
9427 quals = chainon (build_tree_list (NULL_TREE, token->value), quals);
9428 c_parser_consume_token (parser);
9429 }
9430 else
9431 break;
9432 }
9433 if (c_parser_next_tokens_start_typename (parser, cla_prefer_type))
9434 type_name = c_parser_type_name (parser);
9435 if (type_name)
9436 type = groktypename (type_name, NULL, NULL);
9437
9438 /* If the type is unknown, and error has already been produced and
9439 we need to recover from the error. In that case, use NULL_TREE
9440 for the type, as if no type had been specified; this will use the
9441 default type ('id') which is good for error recovery. */
9442 if (type == error_mark_node)
9443 type = NULL_TREE;
9444
9445 return build_tree_list (quals, type);
9446 }
9447
9448 /* Parse objc-protocol-refs.
9449
9450 objc-protocol-refs:
9451 < identifier-list >
9452 */
9453
9454 static tree
9455 c_parser_objc_protocol_refs (c_parser *parser)
9456 {
9457 tree list = NULL_TREE;
9458 gcc_assert (c_parser_next_token_is (parser, CPP_LESS));
9459 c_parser_consume_token (parser);
9460 /* Any identifiers, including those declared as type names, are OK
9461 here. */
9462 while (true)
9463 {
9464 tree id;
9465 if (c_parser_next_token_is_not (parser, CPP_NAME))
9466 {
9467 c_parser_error (parser, "expected identifier");
9468 break;
9469 }
9470 id = c_parser_peek_token (parser)->value;
9471 list = chainon (list, build_tree_list (NULL_TREE, id));
9472 c_parser_consume_token (parser);
9473 if (c_parser_next_token_is (parser, CPP_COMMA))
9474 c_parser_consume_token (parser);
9475 else
9476 break;
9477 }
9478 c_parser_require (parser, CPP_GREATER, "expected %<>%>");
9479 return list;
9480 }
9481
9482 /* Parse an objc-try-catch-finally-statement.
9483
9484 objc-try-catch-finally-statement:
9485 @try compound-statement objc-catch-list[opt]
9486 @try compound-statement objc-catch-list[opt] @finally compound-statement
9487
9488 objc-catch-list:
9489 @catch ( objc-catch-parameter-declaration ) compound-statement
9490 objc-catch-list @catch ( objc-catch-parameter-declaration ) compound-statement
9491
9492 objc-catch-parameter-declaration:
9493 parameter-declaration
9494 '...'
9495
9496 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
9497
9498 PS: This function is identical to cp_parser_objc_try_catch_finally_statement
9499 for C++. Keep them in sync. */
9500
9501 static void
9502 c_parser_objc_try_catch_finally_statement (c_parser *parser)
9503 {
9504 location_t location;
9505 tree stmt;
9506
9507 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_TRY));
9508 c_parser_consume_token (parser);
9509 location = c_parser_peek_token (parser)->location;
9510 objc_maybe_warn_exceptions (location);
9511 stmt = c_parser_compound_statement (parser);
9512 objc_begin_try_stmt (location, stmt);
9513
9514 while (c_parser_next_token_is_keyword (parser, RID_AT_CATCH))
9515 {
9516 struct c_parm *parm;
9517 tree parameter_declaration = error_mark_node;
9518 bool seen_open_paren = false;
9519
9520 c_parser_consume_token (parser);
9521 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
9522 seen_open_paren = true;
9523 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
9524 {
9525 /* We have "@catch (...)" (where the '...' are literally
9526 what is in the code). Skip the '...'.
9527 parameter_declaration is set to NULL_TREE, and
9528 objc_being_catch_clauses() knows that that means
9529 '...'. */
9530 c_parser_consume_token (parser);
9531 parameter_declaration = NULL_TREE;
9532 }
9533 else
9534 {
9535 /* We have "@catch (NSException *exception)" or something
9536 like that. Parse the parameter declaration. */
9537 parm = c_parser_parameter_declaration (parser, NULL_TREE);
9538 if (parm == NULL)
9539 parameter_declaration = error_mark_node;
9540 else
9541 parameter_declaration = grokparm (parm, NULL);
9542 }
9543 if (seen_open_paren)
9544 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9545 else
9546 {
9547 /* If there was no open parenthesis, we are recovering from
9548 an error, and we are trying to figure out what mistake
9549 the user has made. */
9550
9551 /* If there is an immediate closing parenthesis, the user
9552 probably forgot the opening one (ie, they typed "@catch
9553 NSException *e)". Parse the closing parenthesis and keep
9554 going. */
9555 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
9556 c_parser_consume_token (parser);
9557
9558 /* If these is no immediate closing parenthesis, the user
9559 probably doesn't know that parenthesis are required at
9560 all (ie, they typed "@catch NSException *e"). So, just
9561 forget about the closing parenthesis and keep going. */
9562 }
9563 objc_begin_catch_clause (parameter_declaration);
9564 if (c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
9565 c_parser_compound_statement_nostart (parser);
9566 objc_finish_catch_clause ();
9567 }
9568 if (c_parser_next_token_is_keyword (parser, RID_AT_FINALLY))
9569 {
9570 c_parser_consume_token (parser);
9571 location = c_parser_peek_token (parser)->location;
9572 stmt = c_parser_compound_statement (parser);
9573 objc_build_finally_clause (location, stmt);
9574 }
9575 objc_finish_try_stmt ();
9576 }
9577
9578 /* Parse an objc-synchronized-statement.
9579
9580 objc-synchronized-statement:
9581 @synchronized ( expression ) compound-statement
9582 */
9583
9584 static void
9585 c_parser_objc_synchronized_statement (c_parser *parser)
9586 {
9587 location_t loc;
9588 tree expr, stmt;
9589 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNCHRONIZED));
9590 c_parser_consume_token (parser);
9591 loc = c_parser_peek_token (parser)->location;
9592 objc_maybe_warn_exceptions (loc);
9593 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
9594 {
9595 struct c_expr ce = c_parser_expression (parser);
9596 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
9597 expr = ce.value;
9598 expr = c_fully_fold (expr, false, NULL);
9599 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9600 }
9601 else
9602 expr = error_mark_node;
9603 stmt = c_parser_compound_statement (parser);
9604 objc_build_synchronized (loc, expr, stmt);
9605 }
9606
9607 /* Parse an objc-selector; return NULL_TREE without an error if the
9608 next token is not an objc-selector.
9609
9610 objc-selector:
9611 identifier
9612 one of
9613 enum struct union if else while do for switch case default
9614 break continue return goto asm sizeof typeof __alignof
9615 unsigned long const short volatile signed restrict _Complex
9616 in out inout bycopy byref oneway int char float double void _Bool
9617 _Atomic
9618
9619 ??? Why this selection of keywords but not, for example, storage
9620 class specifiers? */
9621
9622 static tree
9623 c_parser_objc_selector (c_parser *parser)
9624 {
9625 c_token *token = c_parser_peek_token (parser);
9626 tree value = token->value;
9627 if (token->type == CPP_NAME)
9628 {
9629 c_parser_consume_token (parser);
9630 return value;
9631 }
9632 if (token->type != CPP_KEYWORD)
9633 return NULL_TREE;
9634 switch (token->keyword)
9635 {
9636 case RID_ENUM:
9637 case RID_STRUCT:
9638 case RID_UNION:
9639 case RID_IF:
9640 case RID_ELSE:
9641 case RID_WHILE:
9642 case RID_DO:
9643 case RID_FOR:
9644 case RID_SWITCH:
9645 case RID_CASE:
9646 case RID_DEFAULT:
9647 case RID_BREAK:
9648 case RID_CONTINUE:
9649 case RID_RETURN:
9650 case RID_GOTO:
9651 case RID_ASM:
9652 case RID_SIZEOF:
9653 case RID_TYPEOF:
9654 case RID_ALIGNOF:
9655 case RID_UNSIGNED:
9656 case RID_LONG:
9657 case RID_CONST:
9658 case RID_SHORT:
9659 case RID_VOLATILE:
9660 case RID_SIGNED:
9661 case RID_RESTRICT:
9662 case RID_COMPLEX:
9663 case RID_IN:
9664 case RID_OUT:
9665 case RID_INOUT:
9666 case RID_BYCOPY:
9667 case RID_BYREF:
9668 case RID_ONEWAY:
9669 case RID_INT:
9670 case RID_CHAR:
9671 case RID_FLOAT:
9672 case RID_DOUBLE:
9673 CASE_RID_FLOATN_NX:
9674 case RID_VOID:
9675 case RID_BOOL:
9676 case RID_ATOMIC:
9677 case RID_AUTO_TYPE:
9678 case RID_INT_N_0:
9679 case RID_INT_N_1:
9680 case RID_INT_N_2:
9681 case RID_INT_N_3:
9682 c_parser_consume_token (parser);
9683 return value;
9684 default:
9685 return NULL_TREE;
9686 }
9687 }
9688
9689 /* Parse an objc-selector-arg.
9690
9691 objc-selector-arg:
9692 objc-selector
9693 objc-keywordname-list
9694
9695 objc-keywordname-list:
9696 objc-keywordname
9697 objc-keywordname-list objc-keywordname
9698
9699 objc-keywordname:
9700 objc-selector :
9701 :
9702 */
9703
9704 static tree
9705 c_parser_objc_selector_arg (c_parser *parser)
9706 {
9707 tree sel = c_parser_objc_selector (parser);
9708 tree list = NULL_TREE;
9709 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
9710 return sel;
9711 while (true)
9712 {
9713 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
9714 return list;
9715 list = chainon (list, build_tree_list (sel, NULL_TREE));
9716 sel = c_parser_objc_selector (parser);
9717 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
9718 break;
9719 }
9720 return list;
9721 }
9722
9723 /* Parse an objc-receiver.
9724
9725 objc-receiver:
9726 expression
9727 class-name
9728 type-name
9729 */
9730
9731 static tree
9732 c_parser_objc_receiver (c_parser *parser)
9733 {
9734 location_t loc = c_parser_peek_token (parser)->location;
9735
9736 if (c_parser_peek_token (parser)->type == CPP_NAME
9737 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
9738 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
9739 {
9740 tree id = c_parser_peek_token (parser)->value;
9741 c_parser_consume_token (parser);
9742 return objc_get_class_reference (id);
9743 }
9744 struct c_expr ce = c_parser_expression (parser);
9745 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
9746 return c_fully_fold (ce.value, false, NULL);
9747 }
9748
9749 /* Parse objc-message-args.
9750
9751 objc-message-args:
9752 objc-selector
9753 objc-keywordarg-list
9754
9755 objc-keywordarg-list:
9756 objc-keywordarg
9757 objc-keywordarg-list objc-keywordarg
9758
9759 objc-keywordarg:
9760 objc-selector : objc-keywordexpr
9761 : objc-keywordexpr
9762 */
9763
9764 static tree
9765 c_parser_objc_message_args (c_parser *parser)
9766 {
9767 tree sel = c_parser_objc_selector (parser);
9768 tree list = NULL_TREE;
9769 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
9770 return sel;
9771 while (true)
9772 {
9773 tree keywordexpr;
9774 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
9775 return error_mark_node;
9776 keywordexpr = c_parser_objc_keywordexpr (parser);
9777 list = chainon (list, build_tree_list (sel, keywordexpr));
9778 sel = c_parser_objc_selector (parser);
9779 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
9780 break;
9781 }
9782 return list;
9783 }
9784
9785 /* Parse an objc-keywordexpr.
9786
9787 objc-keywordexpr:
9788 nonempty-expr-list
9789 */
9790
9791 static tree
9792 c_parser_objc_keywordexpr (c_parser *parser)
9793 {
9794 tree ret;
9795 vec<tree, va_gc> *expr_list = c_parser_expr_list (parser, true, true,
9796 NULL, NULL, NULL, NULL);
9797 if (vec_safe_length (expr_list) == 1)
9798 {
9799 /* Just return the expression, remove a level of
9800 indirection. */
9801 ret = (*expr_list)[0];
9802 }
9803 else
9804 {
9805 /* We have a comma expression, we will collapse later. */
9806 ret = build_tree_list_vec (expr_list);
9807 }
9808 release_tree_vector (expr_list);
9809 return ret;
9810 }
9811
9812 /* A check, needed in several places, that ObjC interface, implementation or
9813 method definitions are not prefixed by incorrect items. */
9814 static bool
9815 c_parser_objc_diagnose_bad_element_prefix (c_parser *parser,
9816 struct c_declspecs *specs)
9817 {
9818 if (!specs->declspecs_seen_p || specs->non_sc_seen_p
9819 || specs->typespec_kind != ctsk_none)
9820 {
9821 c_parser_error (parser,
9822 "no type or storage class may be specified here,");
9823 c_parser_skip_to_end_of_block_or_statement (parser);
9824 return true;
9825 }
9826 return false;
9827 }
9828
9829 /* Parse an Objective-C @property declaration. The syntax is:
9830
9831 objc-property-declaration:
9832 '@property' objc-property-attributes[opt] struct-declaration ;
9833
9834 objc-property-attributes:
9835 '(' objc-property-attribute-list ')'
9836
9837 objc-property-attribute-list:
9838 objc-property-attribute
9839 objc-property-attribute-list, objc-property-attribute
9840
9841 objc-property-attribute
9842 'getter' = identifier
9843 'setter' = identifier
9844 'readonly'
9845 'readwrite'
9846 'assign'
9847 'retain'
9848 'copy'
9849 'nonatomic'
9850
9851 For example:
9852 @property NSString *name;
9853 @property (readonly) id object;
9854 @property (retain, nonatomic, getter=getTheName) id name;
9855 @property int a, b, c;
9856
9857 PS: This function is identical to cp_parser_objc_at_propery_declaration
9858 for C++. Keep them in sync. */
9859 static void
9860 c_parser_objc_at_property_declaration (c_parser *parser)
9861 {
9862 /* The following variables hold the attributes of the properties as
9863 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
9864 seen. When we see an attribute, we set them to 'true' (if they
9865 are boolean properties) or to the identifier (if they have an
9866 argument, ie, for getter and setter). Note that here we only
9867 parse the list of attributes, check the syntax and accumulate the
9868 attributes that we find. objc_add_property_declaration() will
9869 then process the information. */
9870 bool property_assign = false;
9871 bool property_copy = false;
9872 tree property_getter_ident = NULL_TREE;
9873 bool property_nonatomic = false;
9874 bool property_readonly = false;
9875 bool property_readwrite = false;
9876 bool property_retain = false;
9877 tree property_setter_ident = NULL_TREE;
9878
9879 /* 'properties' is the list of properties that we read. Usually a
9880 single one, but maybe more (eg, in "@property int a, b, c;" there
9881 are three). */
9882 tree properties;
9883 location_t loc;
9884
9885 loc = c_parser_peek_token (parser)->location;
9886 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROPERTY));
9887
9888 c_parser_consume_token (parser); /* Eat '@property'. */
9889
9890 /* Parse the optional attribute list... */
9891 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
9892 {
9893 /* Eat the '(' */
9894 c_parser_consume_token (parser);
9895
9896 /* Property attribute keywords are valid now. */
9897 parser->objc_property_attr_context = true;
9898
9899 while (true)
9900 {
9901 bool syntax_error = false;
9902 c_token *token = c_parser_peek_token (parser);
9903 enum rid keyword;
9904
9905 if (token->type != CPP_KEYWORD)
9906 {
9907 if (token->type == CPP_CLOSE_PAREN)
9908 c_parser_error (parser, "expected identifier");
9909 else
9910 {
9911 c_parser_consume_token (parser);
9912 c_parser_error (parser, "unknown property attribute");
9913 }
9914 break;
9915 }
9916 keyword = token->keyword;
9917 c_parser_consume_token (parser);
9918 switch (keyword)
9919 {
9920 case RID_ASSIGN: property_assign = true; break;
9921 case RID_COPY: property_copy = true; break;
9922 case RID_NONATOMIC: property_nonatomic = true; break;
9923 case RID_READONLY: property_readonly = true; break;
9924 case RID_READWRITE: property_readwrite = true; break;
9925 case RID_RETAIN: property_retain = true; break;
9926
9927 case RID_GETTER:
9928 case RID_SETTER:
9929 if (c_parser_next_token_is_not (parser, CPP_EQ))
9930 {
9931 if (keyword == RID_GETTER)
9932 c_parser_error (parser,
9933 "missing %<=%> (after %<getter%> attribute)");
9934 else
9935 c_parser_error (parser,
9936 "missing %<=%> (after %<setter%> attribute)");
9937 syntax_error = true;
9938 break;
9939 }
9940 c_parser_consume_token (parser); /* eat the = */
9941 if (c_parser_next_token_is_not (parser, CPP_NAME))
9942 {
9943 c_parser_error (parser, "expected identifier");
9944 syntax_error = true;
9945 break;
9946 }
9947 if (keyword == RID_SETTER)
9948 {
9949 if (property_setter_ident != NULL_TREE)
9950 c_parser_error (parser, "the %<setter%> attribute may only be specified once");
9951 else
9952 property_setter_ident = c_parser_peek_token (parser)->value;
9953 c_parser_consume_token (parser);
9954 if (c_parser_next_token_is_not (parser, CPP_COLON))
9955 c_parser_error (parser, "setter name must terminate with %<:%>");
9956 else
9957 c_parser_consume_token (parser);
9958 }
9959 else
9960 {
9961 if (property_getter_ident != NULL_TREE)
9962 c_parser_error (parser, "the %<getter%> attribute may only be specified once");
9963 else
9964 property_getter_ident = c_parser_peek_token (parser)->value;
9965 c_parser_consume_token (parser);
9966 }
9967 break;
9968 default:
9969 c_parser_error (parser, "unknown property attribute");
9970 syntax_error = true;
9971 break;
9972 }
9973
9974 if (syntax_error)
9975 break;
9976
9977 if (c_parser_next_token_is (parser, CPP_COMMA))
9978 c_parser_consume_token (parser);
9979 else
9980 break;
9981 }
9982 parser->objc_property_attr_context = false;
9983 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9984 }
9985 /* ... and the property declaration(s). */
9986 properties = c_parser_struct_declaration (parser);
9987
9988 if (properties == error_mark_node)
9989 {
9990 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9991 parser->error = false;
9992 return;
9993 }
9994
9995 if (properties == NULL_TREE)
9996 c_parser_error (parser, "expected identifier");
9997 else
9998 {
9999 /* Comma-separated properties are chained together in
10000 reverse order; add them one by one. */
10001 properties = nreverse (properties);
10002
10003 for (; properties; properties = TREE_CHAIN (properties))
10004 objc_add_property_declaration (loc, copy_node (properties),
10005 property_readonly, property_readwrite,
10006 property_assign, property_retain,
10007 property_copy, property_nonatomic,
10008 property_getter_ident, property_setter_ident);
10009 }
10010
10011 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
10012 parser->error = false;
10013 }
10014
10015 /* Parse an Objective-C @synthesize declaration. The syntax is:
10016
10017 objc-synthesize-declaration:
10018 @synthesize objc-synthesize-identifier-list ;
10019
10020 objc-synthesize-identifier-list:
10021 objc-synthesize-identifier
10022 objc-synthesize-identifier-list, objc-synthesize-identifier
10023
10024 objc-synthesize-identifier
10025 identifier
10026 identifier = identifier
10027
10028 For example:
10029 @synthesize MyProperty;
10030 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
10031
10032 PS: This function is identical to cp_parser_objc_at_synthesize_declaration
10033 for C++. Keep them in sync.
10034 */
10035 static void
10036 c_parser_objc_at_synthesize_declaration (c_parser *parser)
10037 {
10038 tree list = NULL_TREE;
10039 location_t loc;
10040 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNTHESIZE));
10041 loc = c_parser_peek_token (parser)->location;
10042
10043 c_parser_consume_token (parser);
10044 while (true)
10045 {
10046 tree property, ivar;
10047 if (c_parser_next_token_is_not (parser, CPP_NAME))
10048 {
10049 c_parser_error (parser, "expected identifier");
10050 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
10051 /* Once we find the semicolon, we can resume normal parsing.
10052 We have to reset parser->error manually because
10053 c_parser_skip_until_found() won't reset it for us if the
10054 next token is precisely a semicolon. */
10055 parser->error = false;
10056 return;
10057 }
10058 property = c_parser_peek_token (parser)->value;
10059 c_parser_consume_token (parser);
10060 if (c_parser_next_token_is (parser, CPP_EQ))
10061 {
10062 c_parser_consume_token (parser);
10063 if (c_parser_next_token_is_not (parser, CPP_NAME))
10064 {
10065 c_parser_error (parser, "expected identifier");
10066 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
10067 parser->error = false;
10068 return;
10069 }
10070 ivar = c_parser_peek_token (parser)->value;
10071 c_parser_consume_token (parser);
10072 }
10073 else
10074 ivar = NULL_TREE;
10075 list = chainon (list, build_tree_list (ivar, property));
10076 if (c_parser_next_token_is (parser, CPP_COMMA))
10077 c_parser_consume_token (parser);
10078 else
10079 break;
10080 }
10081 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
10082 objc_add_synthesize_declaration (loc, list);
10083 }
10084
10085 /* Parse an Objective-C @dynamic declaration. The syntax is:
10086
10087 objc-dynamic-declaration:
10088 @dynamic identifier-list ;
10089
10090 For example:
10091 @dynamic MyProperty;
10092 @dynamic MyProperty, AnotherProperty;
10093
10094 PS: This function is identical to cp_parser_objc_at_dynamic_declaration
10095 for C++. Keep them in sync.
10096 */
10097 static void
10098 c_parser_objc_at_dynamic_declaration (c_parser *parser)
10099 {
10100 tree list = NULL_TREE;
10101 location_t loc;
10102 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_DYNAMIC));
10103 loc = c_parser_peek_token (parser)->location;
10104
10105 c_parser_consume_token (parser);
10106 while (true)
10107 {
10108 tree property;
10109 if (c_parser_next_token_is_not (parser, CPP_NAME))
10110 {
10111 c_parser_error (parser, "expected identifier");
10112 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
10113 parser->error = false;
10114 return;
10115 }
10116 property = c_parser_peek_token (parser)->value;
10117 list = chainon (list, build_tree_list (NULL_TREE, property));
10118 c_parser_consume_token (parser);
10119 if (c_parser_next_token_is (parser, CPP_COMMA))
10120 c_parser_consume_token (parser);
10121 else
10122 break;
10123 }
10124 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
10125 objc_add_dynamic_declaration (loc, list);
10126 }
10127
10128 \f
10129 /* Handle pragmas. Some OpenMP pragmas are associated with, and therefore
10130 should be considered, statements. ALLOW_STMT is true if we're within
10131 the context of a function and such pragmas are to be allowed. Returns
10132 true if we actually parsed such a pragma. */
10133
10134 static bool
10135 c_parser_pragma (c_parser *parser, enum pragma_context context, bool *if_p)
10136 {
10137 unsigned int id;
10138
10139 id = c_parser_peek_token (parser)->pragma_kind;
10140 gcc_assert (id != PRAGMA_NONE);
10141
10142 switch (id)
10143 {
10144 case PRAGMA_OACC_DECLARE:
10145 c_parser_oacc_declare (parser);
10146 return false;
10147
10148 case PRAGMA_OACC_ENTER_DATA:
10149 if (context != pragma_compound)
10150 {
10151 if (context == pragma_stmt)
10152 c_parser_error (parser, "%<#pragma acc enter data%> may only be "
10153 "used in compound statements");
10154 goto bad_stmt;
10155 }
10156 c_parser_oacc_enter_exit_data (parser, true);
10157 return false;
10158
10159 case PRAGMA_OACC_EXIT_DATA:
10160 if (context != pragma_compound)
10161 {
10162 if (context == pragma_stmt)
10163 c_parser_error (parser, "%<#pragma acc exit data%> may only be "
10164 "used in compound statements");
10165 goto bad_stmt;
10166 }
10167 c_parser_oacc_enter_exit_data (parser, false);
10168 return false;
10169
10170 case PRAGMA_OACC_ROUTINE:
10171 if (context != pragma_external)
10172 {
10173 error_at (c_parser_peek_token (parser)->location,
10174 "%<#pragma acc routine%> must be at file scope");
10175 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
10176 return false;
10177 }
10178 c_parser_oacc_routine (parser, context);
10179 return false;
10180
10181 case PRAGMA_OACC_UPDATE:
10182 if (context != pragma_compound)
10183 {
10184 if (context == pragma_stmt)
10185 c_parser_error (parser, "%<#pragma acc update%> may only be "
10186 "used in compound statements");
10187 goto bad_stmt;
10188 }
10189 c_parser_oacc_update (parser);
10190 return false;
10191
10192 case PRAGMA_OMP_BARRIER:
10193 if (context != pragma_compound)
10194 {
10195 if (context == pragma_stmt)
10196 c_parser_error (parser, "%<#pragma omp barrier%> may only be "
10197 "used in compound statements");
10198 goto bad_stmt;
10199 }
10200 c_parser_omp_barrier (parser);
10201 return false;
10202
10203 case PRAGMA_OMP_FLUSH:
10204 if (context != pragma_compound)
10205 {
10206 if (context == pragma_stmt)
10207 c_parser_error (parser, "%<#pragma omp flush%> may only be "
10208 "used in compound statements");
10209 goto bad_stmt;
10210 }
10211 c_parser_omp_flush (parser);
10212 return false;
10213
10214 case PRAGMA_OMP_TASKWAIT:
10215 if (context != pragma_compound)
10216 {
10217 if (context == pragma_stmt)
10218 c_parser_error (parser, "%<#pragma omp taskwait%> may only be "
10219 "used in compound statements");
10220 goto bad_stmt;
10221 }
10222 c_parser_omp_taskwait (parser);
10223 return false;
10224
10225 case PRAGMA_OMP_TASKYIELD:
10226 if (context != pragma_compound)
10227 {
10228 if (context == pragma_stmt)
10229 c_parser_error (parser, "%<#pragma omp taskyield%> may only be "
10230 "used in compound statements");
10231 goto bad_stmt;
10232 }
10233 c_parser_omp_taskyield (parser);
10234 return false;
10235
10236 case PRAGMA_OMP_CANCEL:
10237 if (context != pragma_compound)
10238 {
10239 if (context == pragma_stmt)
10240 c_parser_error (parser, "%<#pragma omp cancel%> may only be "
10241 "used in compound statements");
10242 goto bad_stmt;
10243 }
10244 c_parser_omp_cancel (parser);
10245 return false;
10246
10247 case PRAGMA_OMP_CANCELLATION_POINT:
10248 c_parser_omp_cancellation_point (parser, context);
10249 return false;
10250
10251 case PRAGMA_OMP_THREADPRIVATE:
10252 c_parser_omp_threadprivate (parser);
10253 return false;
10254
10255 case PRAGMA_OMP_TARGET:
10256 return c_parser_omp_target (parser, context, if_p);
10257
10258 case PRAGMA_OMP_END_DECLARE_TARGET:
10259 c_parser_omp_end_declare_target (parser);
10260 return false;
10261
10262 case PRAGMA_OMP_SECTION:
10263 error_at (c_parser_peek_token (parser)->location,
10264 "%<#pragma omp section%> may only be used in "
10265 "%<#pragma omp sections%> construct");
10266 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
10267 return false;
10268
10269 case PRAGMA_OMP_DECLARE:
10270 c_parser_omp_declare (parser, context);
10271 return false;
10272
10273 case PRAGMA_OMP_ORDERED:
10274 return c_parser_omp_ordered (parser, context, if_p);
10275
10276 case PRAGMA_IVDEP:
10277 c_parser_consume_pragma (parser);
10278 c_parser_skip_to_pragma_eol (parser);
10279 if (!c_parser_next_token_is_keyword (parser, RID_FOR)
10280 && !c_parser_next_token_is_keyword (parser, RID_WHILE)
10281 && !c_parser_next_token_is_keyword (parser, RID_DO))
10282 {
10283 c_parser_error (parser, "for, while or do statement expected");
10284 return false;
10285 }
10286 if (c_parser_next_token_is_keyword (parser, RID_FOR))
10287 c_parser_for_statement (parser, true, if_p);
10288 else if (c_parser_next_token_is_keyword (parser, RID_WHILE))
10289 c_parser_while_statement (parser, true, if_p);
10290 else
10291 c_parser_do_statement (parser, true);
10292 return false;
10293
10294 case PRAGMA_GCC_PCH_PREPROCESS:
10295 c_parser_error (parser, "%<#pragma GCC pch_preprocess%> must be first");
10296 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
10297 return false;
10298
10299 case PRAGMA_CILK_SIMD:
10300 if (!c_parser_cilk_verify_simd (parser, context))
10301 return false;
10302 c_parser_consume_pragma (parser);
10303 c_parser_cilk_simd (parser, if_p);
10304 return false;
10305 case PRAGMA_CILK_GRAINSIZE:
10306 if (!flag_cilkplus)
10307 {
10308 warning (0, "%<#pragma grainsize%> ignored because -fcilkplus is not"
10309 " enabled");
10310 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
10311 return false;
10312 }
10313 if (context == pragma_external)
10314 {
10315 error_at (c_parser_peek_token (parser)->location,
10316 "%<#pragma grainsize%> must be inside a function");
10317 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
10318 return false;
10319 }
10320 c_parser_cilk_grainsize (parser, if_p);
10321 return false;
10322
10323 case PRAGMA_OACC_WAIT:
10324 if (context != pragma_compound)
10325 {
10326 if (context == pragma_stmt)
10327 c_parser_error (parser, "%<#pragma acc enter data%> may only be "
10328 "used in compound statements");
10329 goto bad_stmt;
10330 }
10331 /* FALL THROUGH. */
10332
10333 default:
10334 if (id < PRAGMA_FIRST_EXTERNAL)
10335 {
10336 if (context != pragma_stmt && context != pragma_compound)
10337 {
10338 bad_stmt:
10339 c_parser_error (parser, "expected declaration specifiers");
10340 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
10341 return false;
10342 }
10343 c_parser_omp_construct (parser, if_p);
10344 return true;
10345 }
10346 break;
10347 }
10348
10349 c_parser_consume_pragma (parser);
10350 c_invoke_pragma_handler (id);
10351
10352 /* Skip to EOL, but suppress any error message. Those will have been
10353 generated by the handler routine through calling error, as opposed
10354 to calling c_parser_error. */
10355 parser->error = true;
10356 c_parser_skip_to_pragma_eol (parser);
10357
10358 return false;
10359 }
10360
10361 /* The interface the pragma parsers have to the lexer. */
10362
10363 enum cpp_ttype
10364 pragma_lex (tree *value, location_t *loc)
10365 {
10366 c_token *tok = c_parser_peek_token (the_parser);
10367 enum cpp_ttype ret = tok->type;
10368
10369 *value = tok->value;
10370 if (loc)
10371 *loc = tok->location;
10372
10373 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
10374 ret = CPP_EOF;
10375 else
10376 {
10377 if (ret == CPP_KEYWORD)
10378 ret = CPP_NAME;
10379 c_parser_consume_token (the_parser);
10380 }
10381
10382 return ret;
10383 }
10384
10385 static void
10386 c_parser_pragma_pch_preprocess (c_parser *parser)
10387 {
10388 tree name = NULL;
10389
10390 c_parser_consume_pragma (parser);
10391 if (c_parser_next_token_is (parser, CPP_STRING))
10392 {
10393 name = c_parser_peek_token (parser)->value;
10394 c_parser_consume_token (parser);
10395 }
10396 else
10397 c_parser_error (parser, "expected string literal");
10398 c_parser_skip_to_pragma_eol (parser);
10399
10400 if (name)
10401 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
10402 }
10403 \f
10404 /* OpenACC and OpenMP parsing routines. */
10405
10406 /* Returns name of the next clause.
10407 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
10408 the token is not consumed. Otherwise appropriate pragma_omp_clause is
10409 returned and the token is consumed. */
10410
10411 static pragma_omp_clause
10412 c_parser_omp_clause_name (c_parser *parser)
10413 {
10414 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
10415
10416 if (c_parser_next_token_is_keyword (parser, RID_AUTO))
10417 result = PRAGMA_OACC_CLAUSE_AUTO;
10418 else if (c_parser_next_token_is_keyword (parser, RID_IF))
10419 result = PRAGMA_OMP_CLAUSE_IF;
10420 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
10421 result = PRAGMA_OMP_CLAUSE_DEFAULT;
10422 else if (c_parser_next_token_is_keyword (parser, RID_FOR))
10423 result = PRAGMA_OMP_CLAUSE_FOR;
10424 else if (c_parser_next_token_is (parser, CPP_NAME))
10425 {
10426 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
10427
10428 switch (p[0])
10429 {
10430 case 'a':
10431 if (!strcmp ("aligned", p))
10432 result = PRAGMA_OMP_CLAUSE_ALIGNED;
10433 else if (!strcmp ("async", p))
10434 result = PRAGMA_OACC_CLAUSE_ASYNC;
10435 break;
10436 case 'c':
10437 if (!strcmp ("collapse", p))
10438 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
10439 else if (!strcmp ("copy", p))
10440 result = PRAGMA_OACC_CLAUSE_COPY;
10441 else if (!strcmp ("copyin", p))
10442 result = PRAGMA_OMP_CLAUSE_COPYIN;
10443 else if (!strcmp ("copyout", p))
10444 result = PRAGMA_OACC_CLAUSE_COPYOUT;
10445 else if (!strcmp ("copyprivate", p))
10446 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
10447 else if (!strcmp ("create", p))
10448 result = PRAGMA_OACC_CLAUSE_CREATE;
10449 break;
10450 case 'd':
10451 if (!strcmp ("defaultmap", p))
10452 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
10453 else if (!strcmp ("delete", p))
10454 result = PRAGMA_OACC_CLAUSE_DELETE;
10455 else if (!strcmp ("depend", p))
10456 result = PRAGMA_OMP_CLAUSE_DEPEND;
10457 else if (!strcmp ("device", p))
10458 result = PRAGMA_OMP_CLAUSE_DEVICE;
10459 else if (!strcmp ("deviceptr", p))
10460 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
10461 else if (!strcmp ("device_resident", p))
10462 result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
10463 else if (!strcmp ("dist_schedule", p))
10464 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
10465 break;
10466 case 'f':
10467 if (!strcmp ("final", p))
10468 result = PRAGMA_OMP_CLAUSE_FINAL;
10469 else if (!strcmp ("firstprivate", p))
10470 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
10471 else if (!strcmp ("from", p))
10472 result = PRAGMA_OMP_CLAUSE_FROM;
10473 break;
10474 case 'g':
10475 if (!strcmp ("gang", p))
10476 result = PRAGMA_OACC_CLAUSE_GANG;
10477 else if (!strcmp ("grainsize", p))
10478 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
10479 break;
10480 case 'h':
10481 if (!strcmp ("hint", p))
10482 result = PRAGMA_OMP_CLAUSE_HINT;
10483 else if (!strcmp ("host", p))
10484 result = PRAGMA_OACC_CLAUSE_HOST;
10485 break;
10486 case 'i':
10487 if (!strcmp ("inbranch", p))
10488 result = PRAGMA_OMP_CLAUSE_INBRANCH;
10489 else if (!strcmp ("independent", p))
10490 result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
10491 else if (!strcmp ("is_device_ptr", p))
10492 result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
10493 break;
10494 case 'l':
10495 if (!strcmp ("lastprivate", p))
10496 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
10497 else if (!strcmp ("linear", p))
10498 result = PRAGMA_OMP_CLAUSE_LINEAR;
10499 else if (!strcmp ("link", p))
10500 result = PRAGMA_OMP_CLAUSE_LINK;
10501 break;
10502 case 'm':
10503 if (!strcmp ("map", p))
10504 result = PRAGMA_OMP_CLAUSE_MAP;
10505 else if (!strcmp ("mergeable", p))
10506 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
10507 else if (flag_cilkplus && !strcmp ("mask", p))
10508 result = PRAGMA_CILK_CLAUSE_MASK;
10509 break;
10510 case 'n':
10511 if (!strcmp ("nogroup", p))
10512 result = PRAGMA_OMP_CLAUSE_NOGROUP;
10513 else if (!strcmp ("notinbranch", p))
10514 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
10515 else if (!strcmp ("nowait", p))
10516 result = PRAGMA_OMP_CLAUSE_NOWAIT;
10517 else if (!strcmp ("num_gangs", p))
10518 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
10519 else if (!strcmp ("num_tasks", p))
10520 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
10521 else if (!strcmp ("num_teams", p))
10522 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
10523 else if (!strcmp ("num_threads", p))
10524 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
10525 else if (!strcmp ("num_workers", p))
10526 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
10527 else if (flag_cilkplus && !strcmp ("nomask", p))
10528 result = PRAGMA_CILK_CLAUSE_NOMASK;
10529 break;
10530 case 'o':
10531 if (!strcmp ("ordered", p))
10532 result = PRAGMA_OMP_CLAUSE_ORDERED;
10533 break;
10534 case 'p':
10535 if (!strcmp ("parallel", p))
10536 result = PRAGMA_OMP_CLAUSE_PARALLEL;
10537 else if (!strcmp ("present", p))
10538 result = PRAGMA_OACC_CLAUSE_PRESENT;
10539 else if (!strcmp ("present_or_copy", p)
10540 || !strcmp ("pcopy", p))
10541 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY;
10542 else if (!strcmp ("present_or_copyin", p)
10543 || !strcmp ("pcopyin", p))
10544 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN;
10545 else if (!strcmp ("present_or_copyout", p)
10546 || !strcmp ("pcopyout", p))
10547 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT;
10548 else if (!strcmp ("present_or_create", p)
10549 || !strcmp ("pcreate", p))
10550 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE;
10551 else if (!strcmp ("priority", p))
10552 result = PRAGMA_OMP_CLAUSE_PRIORITY;
10553 else if (!strcmp ("private", p))
10554 result = PRAGMA_OMP_CLAUSE_PRIVATE;
10555 else if (!strcmp ("proc_bind", p))
10556 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
10557 break;
10558 case 'r':
10559 if (!strcmp ("reduction", p))
10560 result = PRAGMA_OMP_CLAUSE_REDUCTION;
10561 break;
10562 case 's':
10563 if (!strcmp ("safelen", p))
10564 result = PRAGMA_OMP_CLAUSE_SAFELEN;
10565 else if (!strcmp ("schedule", p))
10566 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
10567 else if (!strcmp ("sections", p))
10568 result = PRAGMA_OMP_CLAUSE_SECTIONS;
10569 else if (!strcmp ("seq", p))
10570 result = PRAGMA_OACC_CLAUSE_SEQ;
10571 else if (!strcmp ("shared", p))
10572 result = PRAGMA_OMP_CLAUSE_SHARED;
10573 else if (!strcmp ("simd", p))
10574 result = PRAGMA_OMP_CLAUSE_SIMD;
10575 else if (!strcmp ("simdlen", p))
10576 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
10577 else if (!strcmp ("self", p))
10578 result = PRAGMA_OACC_CLAUSE_SELF;
10579 break;
10580 case 't':
10581 if (!strcmp ("taskgroup", p))
10582 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
10583 else if (!strcmp ("thread_limit", p))
10584 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
10585 else if (!strcmp ("threads", p))
10586 result = PRAGMA_OMP_CLAUSE_THREADS;
10587 else if (!strcmp ("tile", p))
10588 result = PRAGMA_OACC_CLAUSE_TILE;
10589 else if (!strcmp ("to", p))
10590 result = PRAGMA_OMP_CLAUSE_TO;
10591 break;
10592 case 'u':
10593 if (!strcmp ("uniform", p))
10594 result = PRAGMA_OMP_CLAUSE_UNIFORM;
10595 else if (!strcmp ("untied", p))
10596 result = PRAGMA_OMP_CLAUSE_UNTIED;
10597 else if (!strcmp ("use_device", p))
10598 result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
10599 else if (!strcmp ("use_device_ptr", p))
10600 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
10601 break;
10602 case 'v':
10603 if (!strcmp ("vector", p))
10604 result = PRAGMA_OACC_CLAUSE_VECTOR;
10605 else if (!strcmp ("vector_length", p))
10606 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
10607 else if (flag_cilkplus && !strcmp ("vectorlength", p))
10608 result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
10609 break;
10610 case 'w':
10611 if (!strcmp ("wait", p))
10612 result = PRAGMA_OACC_CLAUSE_WAIT;
10613 else if (!strcmp ("worker", p))
10614 result = PRAGMA_OACC_CLAUSE_WORKER;
10615 break;
10616 }
10617 }
10618
10619 if (result != PRAGMA_OMP_CLAUSE_NONE)
10620 c_parser_consume_token (parser);
10621
10622 return result;
10623 }
10624
10625 /* Validate that a clause of the given type does not already exist. */
10626
10627 static void
10628 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
10629 const char *name)
10630 {
10631 tree c;
10632
10633 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
10634 if (OMP_CLAUSE_CODE (c) == code)
10635 {
10636 location_t loc = OMP_CLAUSE_LOCATION (c);
10637 error_at (loc, "too many %qs clauses", name);
10638 break;
10639 }
10640 }
10641
10642 /* OpenACC 2.0
10643 Parse wait clause or wait directive parameters. */
10644
10645 static tree
10646 c_parser_oacc_wait_list (c_parser *parser, location_t clause_loc, tree list)
10647 {
10648 vec<tree, va_gc> *args;
10649 tree t, args_tree;
10650
10651 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10652 return list;
10653
10654 args = c_parser_expr_list (parser, false, true, NULL, NULL, NULL, NULL);
10655
10656 if (args->length () == 0)
10657 {
10658 c_parser_error (parser, "expected integer expression before ')'");
10659 release_tree_vector (args);
10660 return list;
10661 }
10662
10663 args_tree = build_tree_list_vec (args);
10664
10665 for (t = args_tree; t; t = TREE_CHAIN (t))
10666 {
10667 tree targ = TREE_VALUE (t);
10668
10669 if (targ != error_mark_node)
10670 {
10671 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
10672 {
10673 c_parser_error (parser, "expression must be integral");
10674 targ = error_mark_node;
10675 }
10676 else
10677 {
10678 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
10679
10680 OMP_CLAUSE_DECL (c) = targ;
10681 OMP_CLAUSE_CHAIN (c) = list;
10682 list = c;
10683 }
10684 }
10685 }
10686
10687 release_tree_vector (args);
10688 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10689 return list;
10690 }
10691
10692 /* OpenACC 2.0, OpenMP 2.5:
10693 variable-list:
10694 identifier
10695 variable-list , identifier
10696
10697 If KIND is nonzero, create the appropriate node and install the
10698 decl in OMP_CLAUSE_DECL and add the node to the head of the list.
10699 If KIND is nonzero, CLAUSE_LOC is the location of the clause.
10700
10701 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
10702 return the list created. */
10703
10704 static tree
10705 c_parser_omp_variable_list (c_parser *parser,
10706 location_t clause_loc,
10707 enum omp_clause_code kind, tree list)
10708 {
10709 if (c_parser_next_token_is_not (parser, CPP_NAME)
10710 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
10711 c_parser_error (parser, "expected identifier");
10712
10713 while (c_parser_next_token_is (parser, CPP_NAME)
10714 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
10715 {
10716 tree t = lookup_name (c_parser_peek_token (parser)->value);
10717
10718 if (t == NULL_TREE)
10719 {
10720 undeclared_variable (c_parser_peek_token (parser)->location,
10721 c_parser_peek_token (parser)->value);
10722 t = error_mark_node;
10723 }
10724
10725 c_parser_consume_token (parser);
10726
10727 if (t == error_mark_node)
10728 ;
10729 else if (kind != 0)
10730 {
10731 switch (kind)
10732 {
10733 case OMP_CLAUSE__CACHE_:
10734 /* The OpenACC cache directive explicitly only allows "array
10735 elements or subarrays". */
10736 if (c_parser_peek_token (parser)->type != CPP_OPEN_SQUARE)
10737 {
10738 c_parser_error (parser, "expected %<[%>");
10739 t = error_mark_node;
10740 break;
10741 }
10742 /* FALLTHROUGH */
10743 case OMP_CLAUSE_MAP:
10744 case OMP_CLAUSE_FROM:
10745 case OMP_CLAUSE_TO:
10746 while (c_parser_next_token_is (parser, CPP_DOT))
10747 {
10748 location_t op_loc = c_parser_peek_token (parser)->location;
10749 c_parser_consume_token (parser);
10750 if (!c_parser_next_token_is (parser, CPP_NAME))
10751 {
10752 c_parser_error (parser, "expected identifier");
10753 t = error_mark_node;
10754 break;
10755 }
10756
10757 c_token *comp_tok = c_parser_peek_token (parser);
10758 tree ident = comp_tok->value;
10759 location_t comp_loc = comp_tok->location;
10760 c_parser_consume_token (parser);
10761 t = build_component_ref (op_loc, t, ident, comp_loc);
10762 }
10763 /* FALLTHROUGH */
10764 case OMP_CLAUSE_DEPEND:
10765 case OMP_CLAUSE_REDUCTION:
10766 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
10767 {
10768 tree low_bound = NULL_TREE, length = NULL_TREE;
10769
10770 c_parser_consume_token (parser);
10771 if (!c_parser_next_token_is (parser, CPP_COLON))
10772 {
10773 location_t expr_loc
10774 = c_parser_peek_token (parser)->location;
10775 c_expr expr = c_parser_expression (parser);
10776 expr = convert_lvalue_to_rvalue (expr_loc, expr,
10777 false, true);
10778 low_bound = expr.value;
10779 }
10780 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
10781 length = integer_one_node;
10782 else
10783 {
10784 /* Look for `:'. */
10785 if (!c_parser_require (parser, CPP_COLON,
10786 "expected %<:%>"))
10787 {
10788 t = error_mark_node;
10789 break;
10790 }
10791 if (!c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
10792 {
10793 location_t expr_loc
10794 = c_parser_peek_token (parser)->location;
10795 c_expr expr = c_parser_expression (parser);
10796 expr = convert_lvalue_to_rvalue (expr_loc, expr,
10797 false, true);
10798 length = expr.value;
10799 }
10800 }
10801 /* Look for the closing `]'. */
10802 if (!c_parser_require (parser, CPP_CLOSE_SQUARE,
10803 "expected %<]%>"))
10804 {
10805 t = error_mark_node;
10806 break;
10807 }
10808
10809 t = tree_cons (low_bound, length, t);
10810 }
10811 break;
10812 default:
10813 break;
10814 }
10815
10816 if (t != error_mark_node)
10817 {
10818 tree u = build_omp_clause (clause_loc, kind);
10819 OMP_CLAUSE_DECL (u) = t;
10820 OMP_CLAUSE_CHAIN (u) = list;
10821 list = u;
10822 }
10823 }
10824 else
10825 list = tree_cons (t, NULL_TREE, list);
10826
10827 if (c_parser_next_token_is_not (parser, CPP_COMMA))
10828 break;
10829
10830 c_parser_consume_token (parser);
10831 }
10832
10833 return list;
10834 }
10835
10836 /* Similarly, but expect leading and trailing parenthesis. This is a very
10837 common case for OpenACC and OpenMP clauses. */
10838
10839 static tree
10840 c_parser_omp_var_list_parens (c_parser *parser, enum omp_clause_code kind,
10841 tree list)
10842 {
10843 /* The clauses location. */
10844 location_t loc = c_parser_peek_token (parser)->location;
10845
10846 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10847 {
10848 list = c_parser_omp_variable_list (parser, loc, kind, list);
10849 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10850 }
10851 return list;
10852 }
10853
10854 /* OpenACC 2.0:
10855 copy ( variable-list )
10856 copyin ( variable-list )
10857 copyout ( variable-list )
10858 create ( variable-list )
10859 delete ( variable-list )
10860 present ( variable-list )
10861 present_or_copy ( variable-list )
10862 pcopy ( variable-list )
10863 present_or_copyin ( variable-list )
10864 pcopyin ( variable-list )
10865 present_or_copyout ( variable-list )
10866 pcopyout ( variable-list )
10867 present_or_create ( variable-list )
10868 pcreate ( variable-list ) */
10869
10870 static tree
10871 c_parser_oacc_data_clause (c_parser *parser, pragma_omp_clause c_kind,
10872 tree list)
10873 {
10874 enum gomp_map_kind kind;
10875 switch (c_kind)
10876 {
10877 case PRAGMA_OACC_CLAUSE_COPY:
10878 kind = GOMP_MAP_FORCE_TOFROM;
10879 break;
10880 case PRAGMA_OACC_CLAUSE_COPYIN:
10881 kind = GOMP_MAP_FORCE_TO;
10882 break;
10883 case PRAGMA_OACC_CLAUSE_COPYOUT:
10884 kind = GOMP_MAP_FORCE_FROM;
10885 break;
10886 case PRAGMA_OACC_CLAUSE_CREATE:
10887 kind = GOMP_MAP_FORCE_ALLOC;
10888 break;
10889 case PRAGMA_OACC_CLAUSE_DELETE:
10890 kind = GOMP_MAP_DELETE;
10891 break;
10892 case PRAGMA_OACC_CLAUSE_DEVICE:
10893 kind = GOMP_MAP_FORCE_TO;
10894 break;
10895 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
10896 kind = GOMP_MAP_DEVICE_RESIDENT;
10897 break;
10898 case PRAGMA_OACC_CLAUSE_HOST:
10899 case PRAGMA_OACC_CLAUSE_SELF:
10900 kind = GOMP_MAP_FORCE_FROM;
10901 break;
10902 case PRAGMA_OACC_CLAUSE_LINK:
10903 kind = GOMP_MAP_LINK;
10904 break;
10905 case PRAGMA_OACC_CLAUSE_PRESENT:
10906 kind = GOMP_MAP_FORCE_PRESENT;
10907 break;
10908 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
10909 kind = GOMP_MAP_TOFROM;
10910 break;
10911 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
10912 kind = GOMP_MAP_TO;
10913 break;
10914 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
10915 kind = GOMP_MAP_FROM;
10916 break;
10917 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
10918 kind = GOMP_MAP_ALLOC;
10919 break;
10920 default:
10921 gcc_unreachable ();
10922 }
10923 tree nl, c;
10924 nl = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_MAP, list);
10925
10926 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
10927 OMP_CLAUSE_SET_MAP_KIND (c, kind);
10928
10929 return nl;
10930 }
10931
10932 /* OpenACC 2.0:
10933 deviceptr ( variable-list ) */
10934
10935 static tree
10936 c_parser_oacc_data_clause_deviceptr (c_parser *parser, tree list)
10937 {
10938 location_t loc = c_parser_peek_token (parser)->location;
10939 tree vars, t;
10940
10941 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
10942 c_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
10943 variable-list must only allow for pointer variables. */
10944 vars = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
10945 for (t = vars; t && t; t = TREE_CHAIN (t))
10946 {
10947 tree v = TREE_PURPOSE (t);
10948
10949 /* FIXME diagnostics: Ideally we should keep individual
10950 locations for all the variables in the var list to make the
10951 following errors more precise. Perhaps
10952 c_parser_omp_var_list_parens() should construct a list of
10953 locations to go along with the var list. */
10954
10955 if (!VAR_P (v) && TREE_CODE (v) != PARM_DECL)
10956 error_at (loc, "%qD is not a variable", v);
10957 else if (TREE_TYPE (v) == error_mark_node)
10958 ;
10959 else if (!POINTER_TYPE_P (TREE_TYPE (v)))
10960 error_at (loc, "%qD is not a pointer variable", v);
10961
10962 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
10963 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
10964 OMP_CLAUSE_DECL (u) = v;
10965 OMP_CLAUSE_CHAIN (u) = list;
10966 list = u;
10967 }
10968
10969 return list;
10970 }
10971
10972 /* OpenACC 2.0, OpenMP 3.0:
10973 collapse ( constant-expression ) */
10974
10975 static tree
10976 c_parser_omp_clause_collapse (c_parser *parser, tree list)
10977 {
10978 tree c, num = error_mark_node;
10979 HOST_WIDE_INT n;
10980 location_t loc;
10981
10982 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse");
10983
10984 loc = c_parser_peek_token (parser)->location;
10985 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10986 {
10987 num = c_parser_expr_no_commas (parser, NULL).value;
10988 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10989 }
10990 if (num == error_mark_node)
10991 return list;
10992 mark_exp_read (num);
10993 num = c_fully_fold (num, false, NULL);
10994 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
10995 || !tree_fits_shwi_p (num)
10996 || (n = tree_to_shwi (num)) <= 0
10997 || (int) n != n)
10998 {
10999 error_at (loc,
11000 "collapse argument needs positive constant integer expression");
11001 return list;
11002 }
11003 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
11004 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
11005 OMP_CLAUSE_CHAIN (c) = list;
11006 return c;
11007 }
11008
11009 /* OpenMP 2.5:
11010 copyin ( variable-list ) */
11011
11012 static tree
11013 c_parser_omp_clause_copyin (c_parser *parser, tree list)
11014 {
11015 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYIN, list);
11016 }
11017
11018 /* OpenMP 2.5:
11019 copyprivate ( variable-list ) */
11020
11021 static tree
11022 c_parser_omp_clause_copyprivate (c_parser *parser, tree list)
11023 {
11024 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYPRIVATE, list);
11025 }
11026
11027 /* OpenMP 2.5:
11028 default ( shared | none )
11029
11030 OpenACC 2.0:
11031 default (none) */
11032
11033 static tree
11034 c_parser_omp_clause_default (c_parser *parser, tree list, bool is_oacc)
11035 {
11036 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
11037 location_t loc = c_parser_peek_token (parser)->location;
11038 tree c;
11039
11040 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11041 return list;
11042 if (c_parser_next_token_is (parser, CPP_NAME))
11043 {
11044 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11045
11046 switch (p[0])
11047 {
11048 case 'n':
11049 if (strcmp ("none", p) != 0)
11050 goto invalid_kind;
11051 kind = OMP_CLAUSE_DEFAULT_NONE;
11052 break;
11053
11054 case 's':
11055 if (strcmp ("shared", p) != 0 || is_oacc)
11056 goto invalid_kind;
11057 kind = OMP_CLAUSE_DEFAULT_SHARED;
11058 break;
11059
11060 default:
11061 goto invalid_kind;
11062 }
11063
11064 c_parser_consume_token (parser);
11065 }
11066 else
11067 {
11068 invalid_kind:
11069 if (is_oacc)
11070 c_parser_error (parser, "expected %<none%>");
11071 else
11072 c_parser_error (parser, "expected %<none%> or %<shared%>");
11073 }
11074 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11075
11076 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
11077 return list;
11078
11079 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
11080 c = build_omp_clause (loc, OMP_CLAUSE_DEFAULT);
11081 OMP_CLAUSE_CHAIN (c) = list;
11082 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
11083
11084 return c;
11085 }
11086
11087 /* OpenMP 2.5:
11088 firstprivate ( variable-list ) */
11089
11090 static tree
11091 c_parser_omp_clause_firstprivate (c_parser *parser, tree list)
11092 {
11093 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_FIRSTPRIVATE, list);
11094 }
11095
11096 /* OpenMP 3.1:
11097 final ( expression ) */
11098
11099 static tree
11100 c_parser_omp_clause_final (c_parser *parser, tree list)
11101 {
11102 location_t loc = c_parser_peek_token (parser)->location;
11103 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
11104 {
11105 tree t = c_parser_paren_condition (parser);
11106 tree c;
11107
11108 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final");
11109
11110 c = build_omp_clause (loc, OMP_CLAUSE_FINAL);
11111 OMP_CLAUSE_FINAL_EXPR (c) = t;
11112 OMP_CLAUSE_CHAIN (c) = list;
11113 list = c;
11114 }
11115 else
11116 c_parser_error (parser, "expected %<(%>");
11117
11118 return list;
11119 }
11120
11121 /* OpenACC, OpenMP 2.5:
11122 if ( expression )
11123
11124 OpenMP 4.5:
11125 if ( directive-name-modifier : expression )
11126
11127 directive-name-modifier:
11128 parallel | task | taskloop | target data | target | target update
11129 | target enter data | target exit data */
11130
11131 static tree
11132 c_parser_omp_clause_if (c_parser *parser, tree list, bool is_omp)
11133 {
11134 location_t location = c_parser_peek_token (parser)->location;
11135 enum tree_code if_modifier = ERROR_MARK;
11136
11137 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11138 return list;
11139
11140 if (is_omp && c_parser_next_token_is (parser, CPP_NAME))
11141 {
11142 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11143 int n = 2;
11144 if (strcmp (p, "parallel") == 0)
11145 if_modifier = OMP_PARALLEL;
11146 else if (strcmp (p, "task") == 0)
11147 if_modifier = OMP_TASK;
11148 else if (strcmp (p, "taskloop") == 0)
11149 if_modifier = OMP_TASKLOOP;
11150 else if (strcmp (p, "target") == 0)
11151 {
11152 if_modifier = OMP_TARGET;
11153 if (c_parser_peek_2nd_token (parser)->type == CPP_NAME)
11154 {
11155 p = IDENTIFIER_POINTER (c_parser_peek_2nd_token (parser)->value);
11156 if (strcmp ("data", p) == 0)
11157 if_modifier = OMP_TARGET_DATA;
11158 else if (strcmp ("update", p) == 0)
11159 if_modifier = OMP_TARGET_UPDATE;
11160 else if (strcmp ("enter", p) == 0)
11161 if_modifier = OMP_TARGET_ENTER_DATA;
11162 else if (strcmp ("exit", p) == 0)
11163 if_modifier = OMP_TARGET_EXIT_DATA;
11164 if (if_modifier != OMP_TARGET)
11165 {
11166 n = 3;
11167 c_parser_consume_token (parser);
11168 }
11169 else
11170 {
11171 location_t loc = c_parser_peek_2nd_token (parser)->location;
11172 error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
11173 "or %<exit%>");
11174 if_modifier = ERROR_MARK;
11175 }
11176 if (if_modifier == OMP_TARGET_ENTER_DATA
11177 || if_modifier == OMP_TARGET_EXIT_DATA)
11178 {
11179 if (c_parser_peek_2nd_token (parser)->type == CPP_NAME)
11180 {
11181 p = IDENTIFIER_POINTER
11182 (c_parser_peek_2nd_token (parser)->value);
11183 if (strcmp ("data", p) == 0)
11184 n = 4;
11185 }
11186 if (n == 4)
11187 c_parser_consume_token (parser);
11188 else
11189 {
11190 location_t loc
11191 = c_parser_peek_2nd_token (parser)->location;
11192 error_at (loc, "expected %<data%>");
11193 if_modifier = ERROR_MARK;
11194 }
11195 }
11196 }
11197 }
11198 if (if_modifier != ERROR_MARK)
11199 {
11200 if (c_parser_peek_2nd_token (parser)->type == CPP_COLON)
11201 {
11202 c_parser_consume_token (parser);
11203 c_parser_consume_token (parser);
11204 }
11205 else
11206 {
11207 if (n > 2)
11208 {
11209 location_t loc = c_parser_peek_2nd_token (parser)->location;
11210 error_at (loc, "expected %<:%>");
11211 }
11212 if_modifier = ERROR_MARK;
11213 }
11214 }
11215 }
11216
11217 tree t = c_parser_condition (parser), c;
11218 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11219
11220 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
11221 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
11222 {
11223 if (if_modifier != ERROR_MARK
11224 && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
11225 {
11226 const char *p = NULL;
11227 switch (if_modifier)
11228 {
11229 case OMP_PARALLEL: p = "parallel"; break;
11230 case OMP_TASK: p = "task"; break;
11231 case OMP_TASKLOOP: p = "taskloop"; break;
11232 case OMP_TARGET_DATA: p = "target data"; break;
11233 case OMP_TARGET: p = "target"; break;
11234 case OMP_TARGET_UPDATE: p = "target update"; break;
11235 case OMP_TARGET_ENTER_DATA: p = "enter data"; break;
11236 case OMP_TARGET_EXIT_DATA: p = "exit data"; break;
11237 default: gcc_unreachable ();
11238 }
11239 error_at (location, "too many %<if%> clauses with %qs modifier",
11240 p);
11241 return list;
11242 }
11243 else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
11244 {
11245 if (!is_omp)
11246 error_at (location, "too many %<if%> clauses");
11247 else
11248 error_at (location, "too many %<if%> clauses without modifier");
11249 return list;
11250 }
11251 else if (if_modifier == ERROR_MARK
11252 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
11253 {
11254 error_at (location, "if any %<if%> clause has modifier, then all "
11255 "%<if%> clauses have to use modifier");
11256 return list;
11257 }
11258 }
11259
11260 c = build_omp_clause (location, OMP_CLAUSE_IF);
11261 OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
11262 OMP_CLAUSE_IF_EXPR (c) = t;
11263 OMP_CLAUSE_CHAIN (c) = list;
11264 return c;
11265 }
11266
11267 /* OpenMP 2.5:
11268 lastprivate ( variable-list ) */
11269
11270 static tree
11271 c_parser_omp_clause_lastprivate (c_parser *parser, tree list)
11272 {
11273 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_LASTPRIVATE, list);
11274 }
11275
11276 /* OpenMP 3.1:
11277 mergeable */
11278
11279 static tree
11280 c_parser_omp_clause_mergeable (c_parser *parser ATTRIBUTE_UNUSED, tree list)
11281 {
11282 tree c;
11283
11284 /* FIXME: Should we allow duplicates? */
11285 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable");
11286
11287 c = build_omp_clause (c_parser_peek_token (parser)->location,
11288 OMP_CLAUSE_MERGEABLE);
11289 OMP_CLAUSE_CHAIN (c) = list;
11290
11291 return c;
11292 }
11293
11294 /* OpenMP 2.5:
11295 nowait */
11296
11297 static tree
11298 c_parser_omp_clause_nowait (c_parser *parser ATTRIBUTE_UNUSED, tree list)
11299 {
11300 tree c;
11301 location_t loc = c_parser_peek_token (parser)->location;
11302
11303 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
11304
11305 c = build_omp_clause (loc, OMP_CLAUSE_NOWAIT);
11306 OMP_CLAUSE_CHAIN (c) = list;
11307 return c;
11308 }
11309
11310 /* OpenACC:
11311 num_gangs ( expression ) */
11312
11313 static tree
11314 c_parser_omp_clause_num_gangs (c_parser *parser, tree list)
11315 {
11316 location_t num_gangs_loc = c_parser_peek_token (parser)->location;
11317 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11318 {
11319 location_t expr_loc = c_parser_peek_token (parser)->location;
11320 c_expr expr = c_parser_expression (parser);
11321 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
11322 tree c, t = expr.value;
11323 t = c_fully_fold (t, false, NULL);
11324
11325 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11326
11327 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
11328 {
11329 c_parser_error (parser, "expected integer expression");
11330 return list;
11331 }
11332
11333 /* Attempt to statically determine when the number isn't positive. */
11334 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
11335 build_int_cst (TREE_TYPE (t), 0));
11336 protected_set_expr_location (c, expr_loc);
11337 if (c == boolean_true_node)
11338 {
11339 warning_at (expr_loc, 0,
11340 "%<num_gangs%> value must be positive");
11341 t = integer_one_node;
11342 }
11343
11344 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_GANGS, "num_gangs");
11345
11346 c = build_omp_clause (num_gangs_loc, OMP_CLAUSE_NUM_GANGS);
11347 OMP_CLAUSE_NUM_GANGS_EXPR (c) = t;
11348 OMP_CLAUSE_CHAIN (c) = list;
11349 list = c;
11350 }
11351
11352 return list;
11353 }
11354
11355 /* OpenMP 2.5:
11356 num_threads ( expression ) */
11357
11358 static tree
11359 c_parser_omp_clause_num_threads (c_parser *parser, tree list)
11360 {
11361 location_t num_threads_loc = c_parser_peek_token (parser)->location;
11362 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11363 {
11364 location_t expr_loc = c_parser_peek_token (parser)->location;
11365 c_expr expr = c_parser_expression (parser);
11366 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
11367 tree c, t = expr.value;
11368 t = c_fully_fold (t, false, NULL);
11369
11370 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11371
11372 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
11373 {
11374 c_parser_error (parser, "expected integer expression");
11375 return list;
11376 }
11377
11378 /* Attempt to statically determine when the number isn't positive. */
11379 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
11380 build_int_cst (TREE_TYPE (t), 0));
11381 protected_set_expr_location (c, expr_loc);
11382 if (c == boolean_true_node)
11383 {
11384 warning_at (expr_loc, 0,
11385 "%<num_threads%> value must be positive");
11386 t = integer_one_node;
11387 }
11388
11389 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
11390
11391 c = build_omp_clause (num_threads_loc, OMP_CLAUSE_NUM_THREADS);
11392 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
11393 OMP_CLAUSE_CHAIN (c) = list;
11394 list = c;
11395 }
11396
11397 return list;
11398 }
11399
11400 /* OpenMP 4.5:
11401 num_tasks ( expression ) */
11402
11403 static tree
11404 c_parser_omp_clause_num_tasks (c_parser *parser, tree list)
11405 {
11406 location_t num_tasks_loc = c_parser_peek_token (parser)->location;
11407 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11408 {
11409 location_t expr_loc = c_parser_peek_token (parser)->location;
11410 c_expr expr = c_parser_expression (parser);
11411 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
11412 tree c, t = expr.value;
11413 t = c_fully_fold (t, false, NULL);
11414
11415 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11416
11417 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
11418 {
11419 c_parser_error (parser, "expected integer expression");
11420 return list;
11421 }
11422
11423 /* Attempt to statically determine when the number isn't positive. */
11424 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
11425 build_int_cst (TREE_TYPE (t), 0));
11426 if (CAN_HAVE_LOCATION_P (c))
11427 SET_EXPR_LOCATION (c, expr_loc);
11428 if (c == boolean_true_node)
11429 {
11430 warning_at (expr_loc, 0, "%<num_tasks%> value must be positive");
11431 t = integer_one_node;
11432 }
11433
11434 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS, "num_tasks");
11435
11436 c = build_omp_clause (num_tasks_loc, OMP_CLAUSE_NUM_TASKS);
11437 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
11438 OMP_CLAUSE_CHAIN (c) = list;
11439 list = c;
11440 }
11441
11442 return list;
11443 }
11444
11445 /* OpenMP 4.5:
11446 grainsize ( expression ) */
11447
11448 static tree
11449 c_parser_omp_clause_grainsize (c_parser *parser, tree list)
11450 {
11451 location_t grainsize_loc = c_parser_peek_token (parser)->location;
11452 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11453 {
11454 location_t expr_loc = c_parser_peek_token (parser)->location;
11455 c_expr expr = c_parser_expression (parser);
11456 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
11457 tree c, t = expr.value;
11458 t = c_fully_fold (t, false, NULL);
11459
11460 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11461
11462 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
11463 {
11464 c_parser_error (parser, "expected integer expression");
11465 return list;
11466 }
11467
11468 /* Attempt to statically determine when the number isn't positive. */
11469 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
11470 build_int_cst (TREE_TYPE (t), 0));
11471 if (CAN_HAVE_LOCATION_P (c))
11472 SET_EXPR_LOCATION (c, expr_loc);
11473 if (c == boolean_true_node)
11474 {
11475 warning_at (expr_loc, 0, "%<grainsize%> value must be positive");
11476 t = integer_one_node;
11477 }
11478
11479 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE, "grainsize");
11480
11481 c = build_omp_clause (grainsize_loc, OMP_CLAUSE_GRAINSIZE);
11482 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
11483 OMP_CLAUSE_CHAIN (c) = list;
11484 list = c;
11485 }
11486
11487 return list;
11488 }
11489
11490 /* OpenMP 4.5:
11491 priority ( expression ) */
11492
11493 static tree
11494 c_parser_omp_clause_priority (c_parser *parser, tree list)
11495 {
11496 location_t priority_loc = c_parser_peek_token (parser)->location;
11497 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11498 {
11499 location_t expr_loc = c_parser_peek_token (parser)->location;
11500 c_expr expr = c_parser_expression (parser);
11501 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
11502 tree c, t = expr.value;
11503 t = c_fully_fold (t, false, NULL);
11504
11505 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11506
11507 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
11508 {
11509 c_parser_error (parser, "expected integer expression");
11510 return list;
11511 }
11512
11513 /* Attempt to statically determine when the number isn't
11514 non-negative. */
11515 c = fold_build2_loc (expr_loc, LT_EXPR, boolean_type_node, t,
11516 build_int_cst (TREE_TYPE (t), 0));
11517 if (CAN_HAVE_LOCATION_P (c))
11518 SET_EXPR_LOCATION (c, expr_loc);
11519 if (c == boolean_true_node)
11520 {
11521 warning_at (expr_loc, 0, "%<priority%> value must be non-negative");
11522 t = integer_one_node;
11523 }
11524
11525 check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY, "priority");
11526
11527 c = build_omp_clause (priority_loc, OMP_CLAUSE_PRIORITY);
11528 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
11529 OMP_CLAUSE_CHAIN (c) = list;
11530 list = c;
11531 }
11532
11533 return list;
11534 }
11535
11536 /* OpenMP 4.5:
11537 hint ( expression ) */
11538
11539 static tree
11540 c_parser_omp_clause_hint (c_parser *parser, tree list)
11541 {
11542 location_t hint_loc = c_parser_peek_token (parser)->location;
11543 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11544 {
11545 location_t expr_loc = c_parser_peek_token (parser)->location;
11546 c_expr expr = c_parser_expression (parser);
11547 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
11548 tree c, t = expr.value;
11549 t = c_fully_fold (t, false, NULL);
11550
11551 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11552
11553 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
11554 {
11555 c_parser_error (parser, "expected integer expression");
11556 return list;
11557 }
11558
11559 check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint");
11560
11561 c = build_omp_clause (hint_loc, OMP_CLAUSE_HINT);
11562 OMP_CLAUSE_HINT_EXPR (c) = t;
11563 OMP_CLAUSE_CHAIN (c) = list;
11564 list = c;
11565 }
11566
11567 return list;
11568 }
11569
11570 /* OpenMP 4.5:
11571 defaultmap ( tofrom : scalar ) */
11572
11573 static tree
11574 c_parser_omp_clause_defaultmap (c_parser *parser, tree list)
11575 {
11576 location_t loc = c_parser_peek_token (parser)->location;
11577 tree c;
11578 const char *p;
11579
11580 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11581 return list;
11582 if (!c_parser_next_token_is (parser, CPP_NAME))
11583 {
11584 c_parser_error (parser, "expected %<tofrom%>");
11585 goto out_err;
11586 }
11587 p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11588 if (strcmp (p, "tofrom") != 0)
11589 {
11590 c_parser_error (parser, "expected %<tofrom%>");
11591 goto out_err;
11592 }
11593 c_parser_consume_token (parser);
11594 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
11595 goto out_err;
11596 if (!c_parser_next_token_is (parser, CPP_NAME))
11597 {
11598 c_parser_error (parser, "expected %<scalar%>");
11599 goto out_err;
11600 }
11601 p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11602 if (strcmp (p, "scalar") != 0)
11603 {
11604 c_parser_error (parser, "expected %<scalar%>");
11605 goto out_err;
11606 }
11607 c_parser_consume_token (parser);
11608 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11609 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULTMAP, "defaultmap");
11610 c = build_omp_clause (loc, OMP_CLAUSE_DEFAULTMAP);
11611 OMP_CLAUSE_CHAIN (c) = list;
11612 return c;
11613
11614 out_err:
11615 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11616 return list;
11617 }
11618
11619 /* OpenACC 2.0:
11620 use_device ( variable-list )
11621
11622 OpenMP 4.5:
11623 use_device_ptr ( variable-list ) */
11624
11625 static tree
11626 c_parser_omp_clause_use_device_ptr (c_parser *parser, tree list)
11627 {
11628 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_USE_DEVICE_PTR,
11629 list);
11630 }
11631
11632 /* OpenMP 4.5:
11633 is_device_ptr ( variable-list ) */
11634
11635 static tree
11636 c_parser_omp_clause_is_device_ptr (c_parser *parser, tree list)
11637 {
11638 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_IS_DEVICE_PTR, list);
11639 }
11640
11641 /* OpenACC:
11642 num_workers ( expression ) */
11643
11644 static tree
11645 c_parser_omp_clause_num_workers (c_parser *parser, tree list)
11646 {
11647 location_t num_workers_loc = c_parser_peek_token (parser)->location;
11648 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11649 {
11650 location_t expr_loc = c_parser_peek_token (parser)->location;
11651 c_expr expr = c_parser_expression (parser);
11652 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
11653 tree c, t = expr.value;
11654 t = c_fully_fold (t, false, NULL);
11655
11656 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11657
11658 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
11659 {
11660 c_parser_error (parser, "expected integer expression");
11661 return list;
11662 }
11663
11664 /* Attempt to statically determine when the number isn't positive. */
11665 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
11666 build_int_cst (TREE_TYPE (t), 0));
11667 protected_set_expr_location (c, expr_loc);
11668 if (c == boolean_true_node)
11669 {
11670 warning_at (expr_loc, 0,
11671 "%<num_workers%> value must be positive");
11672 t = integer_one_node;
11673 }
11674
11675 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_WORKERS, "num_workers");
11676
11677 c = build_omp_clause (num_workers_loc, OMP_CLAUSE_NUM_WORKERS);
11678 OMP_CLAUSE_NUM_WORKERS_EXPR (c) = t;
11679 OMP_CLAUSE_CHAIN (c) = list;
11680 list = c;
11681 }
11682
11683 return list;
11684 }
11685
11686 /* OpenACC:
11687
11688 gang [( gang-arg-list )]
11689 worker [( [num:] int-expr )]
11690 vector [( [length:] int-expr )]
11691
11692 where gang-arg is one of:
11693
11694 [num:] int-expr
11695 static: size-expr
11696
11697 and size-expr may be:
11698
11699 *
11700 int-expr
11701 */
11702
11703 static tree
11704 c_parser_oacc_shape_clause (c_parser *parser, omp_clause_code kind,
11705 const char *str, tree list)
11706 {
11707 const char *id = "num";
11708 tree ops[2] = { NULL_TREE, NULL_TREE }, c;
11709 location_t loc = c_parser_peek_token (parser)->location;
11710
11711 if (kind == OMP_CLAUSE_VECTOR)
11712 id = "length";
11713
11714 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
11715 {
11716 c_parser_consume_token (parser);
11717
11718 do
11719 {
11720 c_token *next = c_parser_peek_token (parser);
11721 int idx = 0;
11722
11723 /* Gang static argument. */
11724 if (kind == OMP_CLAUSE_GANG
11725 && c_parser_next_token_is_keyword (parser, RID_STATIC))
11726 {
11727 c_parser_consume_token (parser);
11728
11729 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
11730 goto cleanup_error;
11731
11732 idx = 1;
11733 if (ops[idx] != NULL_TREE)
11734 {
11735 c_parser_error (parser, "too many %<static%> arguments");
11736 goto cleanup_error;
11737 }
11738
11739 /* Check for the '*' argument. */
11740 if (c_parser_next_token_is (parser, CPP_MULT)
11741 && (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
11742 || c_parser_peek_2nd_token (parser)->type
11743 == CPP_CLOSE_PAREN))
11744 {
11745 c_parser_consume_token (parser);
11746 ops[idx] = integer_minus_one_node;
11747
11748 if (c_parser_next_token_is (parser, CPP_COMMA))
11749 {
11750 c_parser_consume_token (parser);
11751 continue;
11752 }
11753 else
11754 break;
11755 }
11756 }
11757 /* Worker num: argument and vector length: arguments. */
11758 else if (c_parser_next_token_is (parser, CPP_NAME)
11759 && strcmp (id, IDENTIFIER_POINTER (next->value)) == 0
11760 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
11761 {
11762 c_parser_consume_token (parser); /* id */
11763 c_parser_consume_token (parser); /* ':' */
11764 }
11765
11766 /* Now collect the actual argument. */
11767 if (ops[idx] != NULL_TREE)
11768 {
11769 c_parser_error (parser, "unexpected argument");
11770 goto cleanup_error;
11771 }
11772
11773 location_t expr_loc = c_parser_peek_token (parser)->location;
11774 c_expr cexpr = c_parser_expr_no_commas (parser, NULL);
11775 cexpr = convert_lvalue_to_rvalue (expr_loc, cexpr, false, true);
11776 tree expr = cexpr.value;
11777 if (expr == error_mark_node)
11778 goto cleanup_error;
11779
11780 expr = c_fully_fold (expr, false, NULL);
11781
11782 /* Attempt to statically determine when the number isn't a
11783 positive integer. */
11784
11785 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr)))
11786 {
11787 c_parser_error (parser, "expected integer expression");
11788 return list;
11789 }
11790
11791 tree c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, expr,
11792 build_int_cst (TREE_TYPE (expr), 0));
11793 if (c == boolean_true_node)
11794 {
11795 warning_at (loc, 0,
11796 "%<%s%> value must be positive", str);
11797 expr = integer_one_node;
11798 }
11799
11800 ops[idx] = expr;
11801
11802 if (kind == OMP_CLAUSE_GANG
11803 && c_parser_next_token_is (parser, CPP_COMMA))
11804 {
11805 c_parser_consume_token (parser);
11806 continue;
11807 }
11808 break;
11809 }
11810 while (1);
11811
11812 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
11813 goto cleanup_error;
11814 }
11815
11816 check_no_duplicate_clause (list, kind, str);
11817
11818 c = build_omp_clause (loc, kind);
11819
11820 if (ops[1])
11821 OMP_CLAUSE_OPERAND (c, 1) = ops[1];
11822
11823 OMP_CLAUSE_OPERAND (c, 0) = ops[0];
11824 OMP_CLAUSE_CHAIN (c) = list;
11825
11826 return c;
11827
11828 cleanup_error:
11829 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
11830 return list;
11831 }
11832
11833 /* OpenACC:
11834 auto
11835 independent
11836 nohost
11837 seq */
11838
11839 static tree
11840 c_parser_oacc_simple_clause (c_parser *parser, enum omp_clause_code code,
11841 tree list)
11842 {
11843 check_no_duplicate_clause (list, code, omp_clause_code_name[code]);
11844
11845 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
11846 OMP_CLAUSE_CHAIN (c) = list;
11847
11848 return c;
11849 }
11850
11851 /* OpenACC:
11852 async [( int-expr )] */
11853
11854 static tree
11855 c_parser_oacc_clause_async (c_parser *parser, tree list)
11856 {
11857 tree c, t;
11858 location_t loc = c_parser_peek_token (parser)->location;
11859
11860 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
11861
11862 if (c_parser_peek_token (parser)->type == CPP_OPEN_PAREN)
11863 {
11864 c_parser_consume_token (parser);
11865
11866 t = c_parser_expression (parser).value;
11867 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
11868 c_parser_error (parser, "expected integer expression");
11869 else if (t == error_mark_node
11870 || !c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
11871 return list;
11872 }
11873 else
11874 t = c_fully_fold (t, false, NULL);
11875
11876 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async");
11877
11878 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
11879 OMP_CLAUSE_ASYNC_EXPR (c) = t;
11880 OMP_CLAUSE_CHAIN (c) = list;
11881 list = c;
11882
11883 return list;
11884 }
11885
11886 /* OpenACC 2.0:
11887 tile ( size-expr-list ) */
11888
11889 static tree
11890 c_parser_oacc_clause_tile (c_parser *parser, tree list)
11891 {
11892 tree c, expr = error_mark_node;
11893 location_t loc, expr_loc;
11894 tree tile = NULL_TREE;
11895
11896 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile");
11897
11898 loc = c_parser_peek_token (parser)->location;
11899 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11900 return list;
11901
11902 do
11903 {
11904 if (c_parser_next_token_is (parser, CPP_MULT)
11905 && (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
11906 || c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_PAREN))
11907 {
11908 c_parser_consume_token (parser);
11909 expr = integer_minus_one_node;
11910 }
11911 else
11912 {
11913 expr_loc = c_parser_peek_token (parser)->location;
11914 c_expr cexpr = c_parser_expr_no_commas (parser, NULL);
11915 cexpr = convert_lvalue_to_rvalue (expr_loc, cexpr, false, true);
11916 expr = cexpr.value;
11917
11918 if (expr == error_mark_node)
11919 {
11920 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
11921 "expected %<)%>");
11922 return list;
11923 }
11924
11925 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr)))
11926 {
11927 c_parser_error (parser, "%<tile%> value must be integral");
11928 return list;
11929 }
11930
11931 expr = c_fully_fold (expr, false, NULL);
11932
11933 /* Attempt to statically determine when expr isn't positive. */
11934 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, expr,
11935 build_int_cst (TREE_TYPE (expr), 0));
11936 protected_set_expr_location (c, expr_loc);
11937 if (c == boolean_true_node)
11938 {
11939 warning_at (expr_loc, 0,"%<tile%> value must be positive");
11940 expr = integer_one_node;
11941 }
11942 }
11943
11944 tile = tree_cons (NULL_TREE, expr, tile);
11945 if (c_parser_next_token_is (parser, CPP_COMMA))
11946 c_parser_consume_token (parser);
11947 }
11948 while (c_parser_next_token_is_not (parser, CPP_CLOSE_PAREN));
11949
11950 /* Consume the trailing ')'. */
11951 c_parser_consume_token (parser);
11952
11953 c = build_omp_clause (loc, OMP_CLAUSE_TILE);
11954 tile = nreverse (tile);
11955 OMP_CLAUSE_TILE_LIST (c) = tile;
11956 OMP_CLAUSE_CHAIN (c) = list;
11957 return c;
11958 }
11959
11960 /* OpenACC:
11961 wait ( int-expr-list ) */
11962
11963 static tree
11964 c_parser_oacc_clause_wait (c_parser *parser, tree list)
11965 {
11966 location_t clause_loc = c_parser_peek_token (parser)->location;
11967
11968 if (c_parser_peek_token (parser)->type == CPP_OPEN_PAREN)
11969 list = c_parser_oacc_wait_list (parser, clause_loc, list);
11970
11971 return list;
11972 }
11973
11974 /* OpenMP 2.5:
11975 ordered
11976
11977 OpenMP 4.5:
11978 ordered ( constant-expression ) */
11979
11980 static tree
11981 c_parser_omp_clause_ordered (c_parser *parser, tree list)
11982 {
11983 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
11984
11985 tree c, num = NULL_TREE;
11986 HOST_WIDE_INT n;
11987 location_t loc = c_parser_peek_token (parser)->location;
11988 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
11989 {
11990 c_parser_consume_token (parser);
11991 num = c_parser_expr_no_commas (parser, NULL).value;
11992 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11993 }
11994 if (num == error_mark_node)
11995 return list;
11996 if (num)
11997 {
11998 mark_exp_read (num);
11999 num = c_fully_fold (num, false, NULL);
12000 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
12001 || !tree_fits_shwi_p (num)
12002 || (n = tree_to_shwi (num)) <= 0
12003 || (int) n != n)
12004 {
12005 error_at (loc, "ordered argument needs positive "
12006 "constant integer expression");
12007 return list;
12008 }
12009 }
12010 c = build_omp_clause (loc, OMP_CLAUSE_ORDERED);
12011 OMP_CLAUSE_ORDERED_EXPR (c) = num;
12012 OMP_CLAUSE_CHAIN (c) = list;
12013 return c;
12014 }
12015
12016 /* OpenMP 2.5:
12017 private ( variable-list ) */
12018
12019 static tree
12020 c_parser_omp_clause_private (c_parser *parser, tree list)
12021 {
12022 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_PRIVATE, list);
12023 }
12024
12025 /* OpenMP 2.5:
12026 reduction ( reduction-operator : variable-list )
12027
12028 reduction-operator:
12029 One of: + * - & ^ | && ||
12030
12031 OpenMP 3.1:
12032
12033 reduction-operator:
12034 One of: + * - & ^ | && || max min
12035
12036 OpenMP 4.0:
12037
12038 reduction-operator:
12039 One of: + * - & ^ | && ||
12040 identifier */
12041
12042 static tree
12043 c_parser_omp_clause_reduction (c_parser *parser, tree list)
12044 {
12045 location_t clause_loc = c_parser_peek_token (parser)->location;
12046 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12047 {
12048 enum tree_code code = ERROR_MARK;
12049 tree reduc_id = NULL_TREE;
12050
12051 switch (c_parser_peek_token (parser)->type)
12052 {
12053 case CPP_PLUS:
12054 code = PLUS_EXPR;
12055 break;
12056 case CPP_MULT:
12057 code = MULT_EXPR;
12058 break;
12059 case CPP_MINUS:
12060 code = MINUS_EXPR;
12061 break;
12062 case CPP_AND:
12063 code = BIT_AND_EXPR;
12064 break;
12065 case CPP_XOR:
12066 code = BIT_XOR_EXPR;
12067 break;
12068 case CPP_OR:
12069 code = BIT_IOR_EXPR;
12070 break;
12071 case CPP_AND_AND:
12072 code = TRUTH_ANDIF_EXPR;
12073 break;
12074 case CPP_OR_OR:
12075 code = TRUTH_ORIF_EXPR;
12076 break;
12077 case CPP_NAME:
12078 {
12079 const char *p
12080 = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12081 if (strcmp (p, "min") == 0)
12082 {
12083 code = MIN_EXPR;
12084 break;
12085 }
12086 if (strcmp (p, "max") == 0)
12087 {
12088 code = MAX_EXPR;
12089 break;
12090 }
12091 reduc_id = c_parser_peek_token (parser)->value;
12092 break;
12093 }
12094 default:
12095 c_parser_error (parser,
12096 "expected %<+%>, %<*%>, %<-%>, %<&%>, "
12097 "%<^%>, %<|%>, %<&&%>, %<||%>, %<min%> or %<max%>");
12098 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
12099 return list;
12100 }
12101 c_parser_consume_token (parser);
12102 reduc_id = c_omp_reduction_id (code, reduc_id);
12103 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
12104 {
12105 tree nl, c;
12106
12107 nl = c_parser_omp_variable_list (parser, clause_loc,
12108 OMP_CLAUSE_REDUCTION, list);
12109 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
12110 {
12111 tree d = OMP_CLAUSE_DECL (c), type;
12112 if (TREE_CODE (d) != TREE_LIST)
12113 type = TREE_TYPE (d);
12114 else
12115 {
12116 int cnt = 0;
12117 tree t;
12118 for (t = d; TREE_CODE (t) == TREE_LIST; t = TREE_CHAIN (t))
12119 cnt++;
12120 type = TREE_TYPE (t);
12121 while (cnt > 0)
12122 {
12123 if (TREE_CODE (type) != POINTER_TYPE
12124 && TREE_CODE (type) != ARRAY_TYPE)
12125 break;
12126 type = TREE_TYPE (type);
12127 cnt--;
12128 }
12129 }
12130 while (TREE_CODE (type) == ARRAY_TYPE)
12131 type = TREE_TYPE (type);
12132 OMP_CLAUSE_REDUCTION_CODE (c) = code;
12133 if (code == ERROR_MARK
12134 || !(INTEGRAL_TYPE_P (type)
12135 || TREE_CODE (type) == REAL_TYPE
12136 || TREE_CODE (type) == COMPLEX_TYPE))
12137 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c)
12138 = c_omp_reduction_lookup (reduc_id,
12139 TYPE_MAIN_VARIANT (type));
12140 }
12141
12142 list = nl;
12143 }
12144 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12145 }
12146 return list;
12147 }
12148
12149 /* OpenMP 2.5:
12150 schedule ( schedule-kind )
12151 schedule ( schedule-kind , expression )
12152
12153 schedule-kind:
12154 static | dynamic | guided | runtime | auto
12155
12156 OpenMP 4.5:
12157 schedule ( schedule-modifier : schedule-kind )
12158 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
12159
12160 schedule-modifier:
12161 simd
12162 monotonic
12163 nonmonotonic */
12164
12165 static tree
12166 c_parser_omp_clause_schedule (c_parser *parser, tree list)
12167 {
12168 tree c, t;
12169 location_t loc = c_parser_peek_token (parser)->location;
12170 int modifiers = 0, nmodifiers = 0;
12171
12172 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12173 return list;
12174
12175 c = build_omp_clause (loc, OMP_CLAUSE_SCHEDULE);
12176
12177 while (c_parser_next_token_is (parser, CPP_NAME))
12178 {
12179 tree kind = c_parser_peek_token (parser)->value;
12180 const char *p = IDENTIFIER_POINTER (kind);
12181 if (strcmp ("simd", p) == 0)
12182 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
12183 else if (strcmp ("monotonic", p) == 0)
12184 modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
12185 else if (strcmp ("nonmonotonic", p) == 0)
12186 modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
12187 else
12188 break;
12189 c_parser_consume_token (parser);
12190 if (nmodifiers++ == 0
12191 && c_parser_next_token_is (parser, CPP_COMMA))
12192 c_parser_consume_token (parser);
12193 else
12194 {
12195 c_parser_require (parser, CPP_COLON, "expected %<:%>");
12196 break;
12197 }
12198 }
12199
12200 if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
12201 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
12202 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
12203 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
12204 {
12205 error_at (loc, "both %<monotonic%> and %<nonmonotonic%> modifiers "
12206 "specified");
12207 modifiers = 0;
12208 }
12209
12210 if (c_parser_next_token_is (parser, CPP_NAME))
12211 {
12212 tree kind = c_parser_peek_token (parser)->value;
12213 const char *p = IDENTIFIER_POINTER (kind);
12214
12215 switch (p[0])
12216 {
12217 case 'd':
12218 if (strcmp ("dynamic", p) != 0)
12219 goto invalid_kind;
12220 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
12221 break;
12222
12223 case 'g':
12224 if (strcmp ("guided", p) != 0)
12225 goto invalid_kind;
12226 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
12227 break;
12228
12229 case 'r':
12230 if (strcmp ("runtime", p) != 0)
12231 goto invalid_kind;
12232 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
12233 break;
12234
12235 default:
12236 goto invalid_kind;
12237 }
12238 }
12239 else if (c_parser_next_token_is_keyword (parser, RID_STATIC))
12240 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
12241 else if (c_parser_next_token_is_keyword (parser, RID_AUTO))
12242 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
12243 else
12244 goto invalid_kind;
12245
12246 c_parser_consume_token (parser);
12247 if (c_parser_next_token_is (parser, CPP_COMMA))
12248 {
12249 location_t here;
12250 c_parser_consume_token (parser);
12251
12252 here = c_parser_peek_token (parser)->location;
12253 c_expr expr = c_parser_expr_no_commas (parser, NULL);
12254 expr = convert_lvalue_to_rvalue (here, expr, false, true);
12255 t = expr.value;
12256 t = c_fully_fold (t, false, NULL);
12257
12258 if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
12259 error_at (here, "schedule %<runtime%> does not take "
12260 "a %<chunk_size%> parameter");
12261 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
12262 error_at (here,
12263 "schedule %<auto%> does not take "
12264 "a %<chunk_size%> parameter");
12265 else if (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE)
12266 {
12267 /* Attempt to statically determine when the number isn't
12268 positive. */
12269 tree s = fold_build2_loc (loc, LE_EXPR, boolean_type_node, t,
12270 build_int_cst (TREE_TYPE (t), 0));
12271 protected_set_expr_location (s, loc);
12272 if (s == boolean_true_node)
12273 {
12274 warning_at (loc, 0,
12275 "chunk size value must be positive");
12276 t = integer_one_node;
12277 }
12278 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
12279 }
12280 else
12281 c_parser_error (parser, "expected integer expression");
12282
12283 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12284 }
12285 else
12286 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
12287 "expected %<,%> or %<)%>");
12288
12289 OMP_CLAUSE_SCHEDULE_KIND (c)
12290 = (enum omp_clause_schedule_kind)
12291 (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
12292
12293 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
12294 OMP_CLAUSE_CHAIN (c) = list;
12295 return c;
12296
12297 invalid_kind:
12298 c_parser_error (parser, "invalid schedule kind");
12299 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
12300 return list;
12301 }
12302
12303 /* OpenMP 2.5:
12304 shared ( variable-list ) */
12305
12306 static tree
12307 c_parser_omp_clause_shared (c_parser *parser, tree list)
12308 {
12309 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_SHARED, list);
12310 }
12311
12312 /* OpenMP 3.0:
12313 untied */
12314
12315 static tree
12316 c_parser_omp_clause_untied (c_parser *parser ATTRIBUTE_UNUSED, tree list)
12317 {
12318 tree c;
12319
12320 /* FIXME: Should we allow duplicates? */
12321 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied");
12322
12323 c = build_omp_clause (c_parser_peek_token (parser)->location,
12324 OMP_CLAUSE_UNTIED);
12325 OMP_CLAUSE_CHAIN (c) = list;
12326
12327 return c;
12328 }
12329
12330 /* OpenACC:
12331 vector_length ( expression ) */
12332
12333 static tree
12334 c_parser_omp_clause_vector_length (c_parser *parser, tree list)
12335 {
12336 location_t vector_length_loc = c_parser_peek_token (parser)->location;
12337 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12338 {
12339 location_t expr_loc = c_parser_peek_token (parser)->location;
12340 c_expr expr = c_parser_expression (parser);
12341 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
12342 tree c, t = expr.value;
12343 t = c_fully_fold (t, false, NULL);
12344
12345 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12346
12347 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
12348 {
12349 c_parser_error (parser, "expected integer expression");
12350 return list;
12351 }
12352
12353 /* Attempt to statically determine when the number isn't positive. */
12354 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
12355 build_int_cst (TREE_TYPE (t), 0));
12356 protected_set_expr_location (c, expr_loc);
12357 if (c == boolean_true_node)
12358 {
12359 warning_at (expr_loc, 0,
12360 "%<vector_length%> value must be positive");
12361 t = integer_one_node;
12362 }
12363
12364 check_no_duplicate_clause (list, OMP_CLAUSE_VECTOR_LENGTH, "vector_length");
12365
12366 c = build_omp_clause (vector_length_loc, OMP_CLAUSE_VECTOR_LENGTH);
12367 OMP_CLAUSE_VECTOR_LENGTH_EXPR (c) = t;
12368 OMP_CLAUSE_CHAIN (c) = list;
12369 list = c;
12370 }
12371
12372 return list;
12373 }
12374
12375 /* OpenMP 4.0:
12376 inbranch
12377 notinbranch */
12378
12379 static tree
12380 c_parser_omp_clause_branch (c_parser *parser ATTRIBUTE_UNUSED,
12381 enum omp_clause_code code, tree list)
12382 {
12383 check_no_duplicate_clause (list, code, omp_clause_code_name[code]);
12384
12385 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
12386 OMP_CLAUSE_CHAIN (c) = list;
12387
12388 return c;
12389 }
12390
12391 /* OpenMP 4.0:
12392 parallel
12393 for
12394 sections
12395 taskgroup */
12396
12397 static tree
12398 c_parser_omp_clause_cancelkind (c_parser *parser ATTRIBUTE_UNUSED,
12399 enum omp_clause_code code, tree list)
12400 {
12401 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
12402 OMP_CLAUSE_CHAIN (c) = list;
12403
12404 return c;
12405 }
12406
12407 /* OpenMP 4.5:
12408 nogroup */
12409
12410 static tree
12411 c_parser_omp_clause_nogroup (c_parser *parser ATTRIBUTE_UNUSED, tree list)
12412 {
12413 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup");
12414 tree c = build_omp_clause (c_parser_peek_token (parser)->location,
12415 OMP_CLAUSE_NOGROUP);
12416 OMP_CLAUSE_CHAIN (c) = list;
12417 return c;
12418 }
12419
12420 /* OpenMP 4.5:
12421 simd
12422 threads */
12423
12424 static tree
12425 c_parser_omp_clause_orderedkind (c_parser *parser ATTRIBUTE_UNUSED,
12426 enum omp_clause_code code, tree list)
12427 {
12428 check_no_duplicate_clause (list, code, omp_clause_code_name[code]);
12429 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
12430 OMP_CLAUSE_CHAIN (c) = list;
12431 return c;
12432 }
12433
12434 /* OpenMP 4.0:
12435 num_teams ( expression ) */
12436
12437 static tree
12438 c_parser_omp_clause_num_teams (c_parser *parser, tree list)
12439 {
12440 location_t num_teams_loc = c_parser_peek_token (parser)->location;
12441 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12442 {
12443 location_t expr_loc = c_parser_peek_token (parser)->location;
12444 c_expr expr = c_parser_expression (parser);
12445 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
12446 tree c, t = expr.value;
12447 t = c_fully_fold (t, false, NULL);
12448
12449 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12450
12451 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
12452 {
12453 c_parser_error (parser, "expected integer expression");
12454 return list;
12455 }
12456
12457 /* Attempt to statically determine when the number isn't positive. */
12458 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
12459 build_int_cst (TREE_TYPE (t), 0));
12460 protected_set_expr_location (c, expr_loc);
12461 if (c == boolean_true_node)
12462 {
12463 warning_at (expr_loc, 0, "%<num_teams%> value must be positive");
12464 t = integer_one_node;
12465 }
12466
12467 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS, "num_teams");
12468
12469 c = build_omp_clause (num_teams_loc, OMP_CLAUSE_NUM_TEAMS);
12470 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
12471 OMP_CLAUSE_CHAIN (c) = list;
12472 list = c;
12473 }
12474
12475 return list;
12476 }
12477
12478 /* OpenMP 4.0:
12479 thread_limit ( expression ) */
12480
12481 static tree
12482 c_parser_omp_clause_thread_limit (c_parser *parser, tree list)
12483 {
12484 location_t num_thread_limit_loc = c_parser_peek_token (parser)->location;
12485 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12486 {
12487 location_t expr_loc = c_parser_peek_token (parser)->location;
12488 c_expr expr = c_parser_expression (parser);
12489 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
12490 tree c, t = expr.value;
12491 t = c_fully_fold (t, false, NULL);
12492
12493 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12494
12495 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
12496 {
12497 c_parser_error (parser, "expected integer expression");
12498 return list;
12499 }
12500
12501 /* Attempt to statically determine when the number isn't positive. */
12502 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
12503 build_int_cst (TREE_TYPE (t), 0));
12504 protected_set_expr_location (c, expr_loc);
12505 if (c == boolean_true_node)
12506 {
12507 warning_at (expr_loc, 0, "%<thread_limit%> value must be positive");
12508 t = integer_one_node;
12509 }
12510
12511 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
12512 "thread_limit");
12513
12514 c = build_omp_clause (num_thread_limit_loc, OMP_CLAUSE_THREAD_LIMIT);
12515 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
12516 OMP_CLAUSE_CHAIN (c) = list;
12517 list = c;
12518 }
12519
12520 return list;
12521 }
12522
12523 /* OpenMP 4.0:
12524 aligned ( variable-list )
12525 aligned ( variable-list : constant-expression ) */
12526
12527 static tree
12528 c_parser_omp_clause_aligned (c_parser *parser, tree list)
12529 {
12530 location_t clause_loc = c_parser_peek_token (parser)->location;
12531 tree nl, c;
12532
12533 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12534 return list;
12535
12536 nl = c_parser_omp_variable_list (parser, clause_loc,
12537 OMP_CLAUSE_ALIGNED, list);
12538
12539 if (c_parser_next_token_is (parser, CPP_COLON))
12540 {
12541 c_parser_consume_token (parser);
12542 location_t expr_loc = c_parser_peek_token (parser)->location;
12543 c_expr expr = c_parser_expr_no_commas (parser, NULL);
12544 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
12545 tree alignment = expr.value;
12546 alignment = c_fully_fold (alignment, false, NULL);
12547 if (TREE_CODE (alignment) != INTEGER_CST
12548 || !INTEGRAL_TYPE_P (TREE_TYPE (alignment))
12549 || tree_int_cst_sgn (alignment) != 1)
12550 {
12551 error_at (clause_loc, "%<aligned%> clause alignment expression must "
12552 "be positive constant integer expression");
12553 alignment = NULL_TREE;
12554 }
12555
12556 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
12557 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
12558 }
12559
12560 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12561 return nl;
12562 }
12563
12564 /* OpenMP 4.0:
12565 linear ( variable-list )
12566 linear ( variable-list : expression )
12567
12568 OpenMP 4.5:
12569 linear ( modifier ( variable-list ) )
12570 linear ( modifier ( variable-list ) : expression ) */
12571
12572 static tree
12573 c_parser_omp_clause_linear (c_parser *parser, tree list, bool is_cilk_simd_fn)
12574 {
12575 location_t clause_loc = c_parser_peek_token (parser)->location;
12576 tree nl, c, step;
12577 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
12578
12579 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12580 return list;
12581
12582 if (!is_cilk_simd_fn
12583 && c_parser_next_token_is (parser, CPP_NAME))
12584 {
12585 c_token *tok = c_parser_peek_token (parser);
12586 const char *p = IDENTIFIER_POINTER (tok->value);
12587 if (strcmp ("val", p) == 0)
12588 kind = OMP_CLAUSE_LINEAR_VAL;
12589 if (c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN)
12590 kind = OMP_CLAUSE_LINEAR_DEFAULT;
12591 if (kind != OMP_CLAUSE_LINEAR_DEFAULT)
12592 {
12593 c_parser_consume_token (parser);
12594 c_parser_consume_token (parser);
12595 }
12596 }
12597
12598 nl = c_parser_omp_variable_list (parser, clause_loc,
12599 OMP_CLAUSE_LINEAR, list);
12600
12601 if (kind != OMP_CLAUSE_LINEAR_DEFAULT)
12602 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12603
12604 if (c_parser_next_token_is (parser, CPP_COLON))
12605 {
12606 c_parser_consume_token (parser);
12607 location_t expr_loc = c_parser_peek_token (parser)->location;
12608 c_expr expr = c_parser_expression (parser);
12609 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
12610 step = expr.value;
12611 step = c_fully_fold (step, false, NULL);
12612 if (is_cilk_simd_fn && TREE_CODE (step) == PARM_DECL)
12613 {
12614 sorry ("using parameters for %<linear%> step is not supported yet");
12615 step = integer_one_node;
12616 }
12617 if (!INTEGRAL_TYPE_P (TREE_TYPE (step)))
12618 {
12619 error_at (clause_loc, "%<linear%> clause step expression must "
12620 "be integral");
12621 step = integer_one_node;
12622 }
12623
12624 }
12625 else
12626 step = integer_one_node;
12627
12628 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
12629 {
12630 OMP_CLAUSE_LINEAR_STEP (c) = step;
12631 OMP_CLAUSE_LINEAR_KIND (c) = kind;
12632 }
12633
12634 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12635 return nl;
12636 }
12637
12638 /* OpenMP 4.0:
12639 safelen ( constant-expression ) */
12640
12641 static tree
12642 c_parser_omp_clause_safelen (c_parser *parser, tree list)
12643 {
12644 location_t clause_loc = c_parser_peek_token (parser)->location;
12645 tree c, t;
12646
12647 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12648 return list;
12649
12650 location_t expr_loc = c_parser_peek_token (parser)->location;
12651 c_expr expr = c_parser_expr_no_commas (parser, NULL);
12652 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
12653 t = expr.value;
12654 t = c_fully_fold (t, false, NULL);
12655 if (TREE_CODE (t) != INTEGER_CST
12656 || !INTEGRAL_TYPE_P (TREE_TYPE (t))
12657 || tree_int_cst_sgn (t) != 1)
12658 {
12659 error_at (clause_loc, "%<safelen%> clause expression must "
12660 "be positive constant integer expression");
12661 t = NULL_TREE;
12662 }
12663
12664 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12665 if (t == NULL_TREE || t == error_mark_node)
12666 return list;
12667
12668 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen");
12669
12670 c = build_omp_clause (clause_loc, OMP_CLAUSE_SAFELEN);
12671 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
12672 OMP_CLAUSE_CHAIN (c) = list;
12673 return c;
12674 }
12675
12676 /* OpenMP 4.0:
12677 simdlen ( constant-expression ) */
12678
12679 static tree
12680 c_parser_omp_clause_simdlen (c_parser *parser, tree list)
12681 {
12682 location_t clause_loc = c_parser_peek_token (parser)->location;
12683 tree c, t;
12684
12685 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12686 return list;
12687
12688 location_t expr_loc = c_parser_peek_token (parser)->location;
12689 c_expr expr = c_parser_expr_no_commas (parser, NULL);
12690 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
12691 t = expr.value;
12692 t = c_fully_fold (t, false, NULL);
12693 if (TREE_CODE (t) != INTEGER_CST
12694 || !INTEGRAL_TYPE_P (TREE_TYPE (t))
12695 || tree_int_cst_sgn (t) != 1)
12696 {
12697 error_at (clause_loc, "%<simdlen%> clause expression must "
12698 "be positive constant integer expression");
12699 t = NULL_TREE;
12700 }
12701
12702 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12703 if (t == NULL_TREE || t == error_mark_node)
12704 return list;
12705
12706 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen");
12707
12708 c = build_omp_clause (clause_loc, OMP_CLAUSE_SIMDLEN);
12709 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
12710 OMP_CLAUSE_CHAIN (c) = list;
12711 return c;
12712 }
12713
12714 /* OpenMP 4.5:
12715 vec:
12716 identifier [+/- integer]
12717 vec , identifier [+/- integer]
12718 */
12719
12720 static tree
12721 c_parser_omp_clause_depend_sink (c_parser *parser, location_t clause_loc,
12722 tree list)
12723 {
12724 tree vec = NULL;
12725 if (c_parser_next_token_is_not (parser, CPP_NAME)
12726 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
12727 {
12728 c_parser_error (parser, "expected identifier");
12729 return list;
12730 }
12731
12732 while (c_parser_next_token_is (parser, CPP_NAME)
12733 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
12734 {
12735 tree t = lookup_name (c_parser_peek_token (parser)->value);
12736 tree addend = NULL;
12737
12738 if (t == NULL_TREE)
12739 {
12740 undeclared_variable (c_parser_peek_token (parser)->location,
12741 c_parser_peek_token (parser)->value);
12742 t = error_mark_node;
12743 }
12744
12745 c_parser_consume_token (parser);
12746
12747 bool neg = false;
12748 if (c_parser_next_token_is (parser, CPP_MINUS))
12749 neg = true;
12750 else if (!c_parser_next_token_is (parser, CPP_PLUS))
12751 {
12752 addend = integer_zero_node;
12753 neg = false;
12754 goto add_to_vector;
12755 }
12756 c_parser_consume_token (parser);
12757
12758 if (c_parser_next_token_is_not (parser, CPP_NUMBER))
12759 {
12760 c_parser_error (parser, "expected integer");
12761 return list;
12762 }
12763
12764 addend = c_parser_peek_token (parser)->value;
12765 if (TREE_CODE (addend) != INTEGER_CST)
12766 {
12767 c_parser_error (parser, "expected integer");
12768 return list;
12769 }
12770 c_parser_consume_token (parser);
12771
12772 add_to_vector:
12773 if (t != error_mark_node)
12774 {
12775 vec = tree_cons (addend, t, vec);
12776 if (neg)
12777 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec) = 1;
12778 }
12779
12780 if (c_parser_next_token_is_not (parser, CPP_COMMA))
12781 break;
12782
12783 c_parser_consume_token (parser);
12784 }
12785
12786 if (vec == NULL_TREE)
12787 return list;
12788
12789 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
12790 OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
12791 OMP_CLAUSE_DECL (u) = nreverse (vec);
12792 OMP_CLAUSE_CHAIN (u) = list;
12793 return u;
12794 }
12795
12796 /* OpenMP 4.0:
12797 depend ( depend-kind: variable-list )
12798
12799 depend-kind:
12800 in | out | inout
12801
12802 OpenMP 4.5:
12803 depend ( source )
12804
12805 depend ( sink : vec ) */
12806
12807 static tree
12808 c_parser_omp_clause_depend (c_parser *parser, tree list)
12809 {
12810 location_t clause_loc = c_parser_peek_token (parser)->location;
12811 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
12812 tree nl, c;
12813
12814 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12815 return list;
12816
12817 if (c_parser_next_token_is (parser, CPP_NAME))
12818 {
12819 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12820 if (strcmp ("in", p) == 0)
12821 kind = OMP_CLAUSE_DEPEND_IN;
12822 else if (strcmp ("inout", p) == 0)
12823 kind = OMP_CLAUSE_DEPEND_INOUT;
12824 else if (strcmp ("out", p) == 0)
12825 kind = OMP_CLAUSE_DEPEND_OUT;
12826 else if (strcmp ("source", p) == 0)
12827 kind = OMP_CLAUSE_DEPEND_SOURCE;
12828 else if (strcmp ("sink", p) == 0)
12829 kind = OMP_CLAUSE_DEPEND_SINK;
12830 else
12831 goto invalid_kind;
12832 }
12833 else
12834 goto invalid_kind;
12835
12836 c_parser_consume_token (parser);
12837
12838 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
12839 {
12840 c = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
12841 OMP_CLAUSE_DEPEND_KIND (c) = kind;
12842 OMP_CLAUSE_DECL (c) = NULL_TREE;
12843 OMP_CLAUSE_CHAIN (c) = list;
12844 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12845 return c;
12846 }
12847
12848 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
12849 goto resync_fail;
12850
12851 if (kind == OMP_CLAUSE_DEPEND_SINK)
12852 nl = c_parser_omp_clause_depend_sink (parser, clause_loc, list);
12853 else
12854 {
12855 nl = c_parser_omp_variable_list (parser, clause_loc,
12856 OMP_CLAUSE_DEPEND, list);
12857
12858 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
12859 OMP_CLAUSE_DEPEND_KIND (c) = kind;
12860 }
12861
12862 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12863 return nl;
12864
12865 invalid_kind:
12866 c_parser_error (parser, "invalid depend kind");
12867 resync_fail:
12868 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12869 return list;
12870 }
12871
12872 /* OpenMP 4.0:
12873 map ( map-kind: variable-list )
12874 map ( variable-list )
12875
12876 map-kind:
12877 alloc | to | from | tofrom
12878
12879 OpenMP 4.5:
12880 map-kind:
12881 alloc | to | from | tofrom | release | delete
12882
12883 map ( always [,] map-kind: variable-list ) */
12884
12885 static tree
12886 c_parser_omp_clause_map (c_parser *parser, tree list)
12887 {
12888 location_t clause_loc = c_parser_peek_token (parser)->location;
12889 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
12890 int always = 0;
12891 enum c_id_kind always_id_kind = C_ID_NONE;
12892 location_t always_loc = UNKNOWN_LOCATION;
12893 tree always_id = NULL_TREE;
12894 tree nl, c;
12895
12896 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12897 return list;
12898
12899 if (c_parser_next_token_is (parser, CPP_NAME))
12900 {
12901 c_token *tok = c_parser_peek_token (parser);
12902 const char *p = IDENTIFIER_POINTER (tok->value);
12903 always_id_kind = tok->id_kind;
12904 always_loc = tok->location;
12905 always_id = tok->value;
12906 if (strcmp ("always", p) == 0)
12907 {
12908 c_token *sectok = c_parser_peek_2nd_token (parser);
12909 if (sectok->type == CPP_COMMA)
12910 {
12911 c_parser_consume_token (parser);
12912 c_parser_consume_token (parser);
12913 always = 2;
12914 }
12915 else if (sectok->type == CPP_NAME)
12916 {
12917 p = IDENTIFIER_POINTER (sectok->value);
12918 if (strcmp ("alloc", p) == 0
12919 || strcmp ("to", p) == 0
12920 || strcmp ("from", p) == 0
12921 || strcmp ("tofrom", p) == 0
12922 || strcmp ("release", p) == 0
12923 || strcmp ("delete", p) == 0)
12924 {
12925 c_parser_consume_token (parser);
12926 always = 1;
12927 }
12928 }
12929 }
12930 }
12931
12932 if (c_parser_next_token_is (parser, CPP_NAME)
12933 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
12934 {
12935 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12936 if (strcmp ("alloc", p) == 0)
12937 kind = GOMP_MAP_ALLOC;
12938 else if (strcmp ("to", p) == 0)
12939 kind = always ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
12940 else if (strcmp ("from", p) == 0)
12941 kind = always ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
12942 else if (strcmp ("tofrom", p) == 0)
12943 kind = always ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
12944 else if (strcmp ("release", p) == 0)
12945 kind = GOMP_MAP_RELEASE;
12946 else if (strcmp ("delete", p) == 0)
12947 kind = GOMP_MAP_DELETE;
12948 else
12949 {
12950 c_parser_error (parser, "invalid map kind");
12951 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
12952 "expected %<)%>");
12953 return list;
12954 }
12955 c_parser_consume_token (parser);
12956 c_parser_consume_token (parser);
12957 }
12958 else if (always)
12959 {
12960 if (always_id_kind != C_ID_ID)
12961 {
12962 c_parser_error (parser, "expected identifier");
12963 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12964 return list;
12965 }
12966
12967 tree t = lookup_name (always_id);
12968 if (t == NULL_TREE)
12969 {
12970 undeclared_variable (always_loc, always_id);
12971 t = error_mark_node;
12972 }
12973 if (t != error_mark_node)
12974 {
12975 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_MAP);
12976 OMP_CLAUSE_DECL (u) = t;
12977 OMP_CLAUSE_CHAIN (u) = list;
12978 OMP_CLAUSE_SET_MAP_KIND (u, kind);
12979 list = u;
12980 }
12981 if (always == 1)
12982 {
12983 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12984 return list;
12985 }
12986 }
12987
12988 nl = c_parser_omp_variable_list (parser, clause_loc, OMP_CLAUSE_MAP, list);
12989
12990 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
12991 OMP_CLAUSE_SET_MAP_KIND (c, kind);
12992
12993 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12994 return nl;
12995 }
12996
12997 /* OpenMP 4.0:
12998 device ( expression ) */
12999
13000 static tree
13001 c_parser_omp_clause_device (c_parser *parser, tree list)
13002 {
13003 location_t clause_loc = c_parser_peek_token (parser)->location;
13004 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
13005 {
13006 location_t expr_loc = c_parser_peek_token (parser)->location;
13007 c_expr expr = c_parser_expr_no_commas (parser, NULL);
13008 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
13009 tree c, t = expr.value;
13010 t = c_fully_fold (t, false, NULL);
13011
13012 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
13013
13014 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
13015 {
13016 c_parser_error (parser, "expected integer expression");
13017 return list;
13018 }
13019
13020 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE, "device");
13021
13022 c = build_omp_clause (clause_loc, OMP_CLAUSE_DEVICE);
13023 OMP_CLAUSE_DEVICE_ID (c) = t;
13024 OMP_CLAUSE_CHAIN (c) = list;
13025 list = c;
13026 }
13027
13028 return list;
13029 }
13030
13031 /* OpenMP 4.0:
13032 dist_schedule ( static )
13033 dist_schedule ( static , expression ) */
13034
13035 static tree
13036 c_parser_omp_clause_dist_schedule (c_parser *parser, tree list)
13037 {
13038 tree c, t = NULL_TREE;
13039 location_t loc = c_parser_peek_token (parser)->location;
13040
13041 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
13042 return list;
13043
13044 if (!c_parser_next_token_is_keyword (parser, RID_STATIC))
13045 {
13046 c_parser_error (parser, "invalid dist_schedule kind");
13047 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
13048 "expected %<)%>");
13049 return list;
13050 }
13051
13052 c_parser_consume_token (parser);
13053 if (c_parser_next_token_is (parser, CPP_COMMA))
13054 {
13055 c_parser_consume_token (parser);
13056
13057 location_t expr_loc = c_parser_peek_token (parser)->location;
13058 c_expr expr = c_parser_expr_no_commas (parser, NULL);
13059 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
13060 t = expr.value;
13061 t = c_fully_fold (t, false, NULL);
13062 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
13063 }
13064 else
13065 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
13066 "expected %<,%> or %<)%>");
13067
13068 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
13069 if (t == error_mark_node)
13070 return list;
13071
13072 c = build_omp_clause (loc, OMP_CLAUSE_DIST_SCHEDULE);
13073 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
13074 OMP_CLAUSE_CHAIN (c) = list;
13075 return c;
13076 }
13077
13078 /* OpenMP 4.0:
13079 proc_bind ( proc-bind-kind )
13080
13081 proc-bind-kind:
13082 master | close | spread */
13083
13084 static tree
13085 c_parser_omp_clause_proc_bind (c_parser *parser, tree list)
13086 {
13087 location_t clause_loc = c_parser_peek_token (parser)->location;
13088 enum omp_clause_proc_bind_kind kind;
13089 tree c;
13090
13091 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
13092 return list;
13093
13094 if (c_parser_next_token_is (parser, CPP_NAME))
13095 {
13096 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13097 if (strcmp ("master", p) == 0)
13098 kind = OMP_CLAUSE_PROC_BIND_MASTER;
13099 else if (strcmp ("close", p) == 0)
13100 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
13101 else if (strcmp ("spread", p) == 0)
13102 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
13103 else
13104 goto invalid_kind;
13105 }
13106 else
13107 goto invalid_kind;
13108
13109 c_parser_consume_token (parser);
13110 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
13111 c = build_omp_clause (clause_loc, OMP_CLAUSE_PROC_BIND);
13112 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
13113 OMP_CLAUSE_CHAIN (c) = list;
13114 return c;
13115
13116 invalid_kind:
13117 c_parser_error (parser, "invalid proc_bind kind");
13118 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
13119 return list;
13120 }
13121
13122 /* OpenMP 4.0:
13123 to ( variable-list ) */
13124
13125 static tree
13126 c_parser_omp_clause_to (c_parser *parser, tree list)
13127 {
13128 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_TO, list);
13129 }
13130
13131 /* OpenMP 4.0:
13132 from ( variable-list ) */
13133
13134 static tree
13135 c_parser_omp_clause_from (c_parser *parser, tree list)
13136 {
13137 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_FROM, list);
13138 }
13139
13140 /* OpenMP 4.0:
13141 uniform ( variable-list ) */
13142
13143 static tree
13144 c_parser_omp_clause_uniform (c_parser *parser, tree list)
13145 {
13146 /* The clauses location. */
13147 location_t loc = c_parser_peek_token (parser)->location;
13148
13149 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
13150 {
13151 list = c_parser_omp_variable_list (parser, loc, OMP_CLAUSE_UNIFORM,
13152 list);
13153 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
13154 }
13155 return list;
13156 }
13157
13158 /* Parse all OpenACC clauses. The set clauses allowed by the directive
13159 is a bitmask in MASK. Return the list of clauses found. */
13160
13161 static tree
13162 c_parser_oacc_all_clauses (c_parser *parser, omp_clause_mask mask,
13163 const char *where, bool finish_p = true)
13164 {
13165 tree clauses = NULL;
13166 bool first = true;
13167
13168 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
13169 {
13170 location_t here;
13171 pragma_omp_clause c_kind;
13172 const char *c_name;
13173 tree prev = clauses;
13174
13175 if (!first && c_parser_next_token_is (parser, CPP_COMMA))
13176 c_parser_consume_token (parser);
13177
13178 here = c_parser_peek_token (parser)->location;
13179 c_kind = c_parser_omp_clause_name (parser);
13180
13181 switch (c_kind)
13182 {
13183 case PRAGMA_OACC_CLAUSE_ASYNC:
13184 clauses = c_parser_oacc_clause_async (parser, clauses);
13185 c_name = "async";
13186 break;
13187 case PRAGMA_OACC_CLAUSE_AUTO:
13188 clauses = c_parser_oacc_simple_clause (parser, OMP_CLAUSE_AUTO,
13189 clauses);
13190 c_name = "auto";
13191 break;
13192 case PRAGMA_OACC_CLAUSE_COLLAPSE:
13193 clauses = c_parser_omp_clause_collapse (parser, clauses);
13194 c_name = "collapse";
13195 break;
13196 case PRAGMA_OACC_CLAUSE_COPY:
13197 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13198 c_name = "copy";
13199 break;
13200 case PRAGMA_OACC_CLAUSE_COPYIN:
13201 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13202 c_name = "copyin";
13203 break;
13204 case PRAGMA_OACC_CLAUSE_COPYOUT:
13205 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13206 c_name = "copyout";
13207 break;
13208 case PRAGMA_OACC_CLAUSE_CREATE:
13209 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13210 c_name = "create";
13211 break;
13212 case PRAGMA_OACC_CLAUSE_DELETE:
13213 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13214 c_name = "delete";
13215 break;
13216 case PRAGMA_OMP_CLAUSE_DEFAULT:
13217 clauses = c_parser_omp_clause_default (parser, clauses, true);
13218 c_name = "default";
13219 break;
13220 case PRAGMA_OACC_CLAUSE_DEVICE:
13221 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13222 c_name = "device";
13223 break;
13224 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
13225 clauses = c_parser_oacc_data_clause_deviceptr (parser, clauses);
13226 c_name = "deviceptr";
13227 break;
13228 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
13229 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13230 c_name = "device_resident";
13231 break;
13232 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
13233 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
13234 c_name = "firstprivate";
13235 break;
13236 case PRAGMA_OACC_CLAUSE_GANG:
13237 c_name = "gang";
13238 clauses = c_parser_oacc_shape_clause (parser, OMP_CLAUSE_GANG,
13239 c_name, clauses);
13240 break;
13241 case PRAGMA_OACC_CLAUSE_HOST:
13242 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13243 c_name = "host";
13244 break;
13245 case PRAGMA_OACC_CLAUSE_IF:
13246 clauses = c_parser_omp_clause_if (parser, clauses, false);
13247 c_name = "if";
13248 break;
13249 case PRAGMA_OACC_CLAUSE_INDEPENDENT:
13250 clauses = c_parser_oacc_simple_clause (parser, OMP_CLAUSE_INDEPENDENT,
13251 clauses);
13252 c_name = "independent";
13253 break;
13254 case PRAGMA_OACC_CLAUSE_LINK:
13255 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13256 c_name = "link";
13257 break;
13258 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
13259 clauses = c_parser_omp_clause_num_gangs (parser, clauses);
13260 c_name = "num_gangs";
13261 break;
13262 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
13263 clauses = c_parser_omp_clause_num_workers (parser, clauses);
13264 c_name = "num_workers";
13265 break;
13266 case PRAGMA_OACC_CLAUSE_PRESENT:
13267 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13268 c_name = "present";
13269 break;
13270 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
13271 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13272 c_name = "present_or_copy";
13273 break;
13274 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
13275 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13276 c_name = "present_or_copyin";
13277 break;
13278 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
13279 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13280 c_name = "present_or_copyout";
13281 break;
13282 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
13283 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13284 c_name = "present_or_create";
13285 break;
13286 case PRAGMA_OACC_CLAUSE_PRIVATE:
13287 clauses = c_parser_omp_clause_private (parser, clauses);
13288 c_name = "private";
13289 break;
13290 case PRAGMA_OACC_CLAUSE_REDUCTION:
13291 clauses = c_parser_omp_clause_reduction (parser, clauses);
13292 c_name = "reduction";
13293 break;
13294 case PRAGMA_OACC_CLAUSE_SELF:
13295 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13296 c_name = "self";
13297 break;
13298 case PRAGMA_OACC_CLAUSE_SEQ:
13299 clauses = c_parser_oacc_simple_clause (parser, OMP_CLAUSE_SEQ,
13300 clauses);
13301 c_name = "seq";
13302 break;
13303 case PRAGMA_OACC_CLAUSE_TILE:
13304 clauses = c_parser_oacc_clause_tile (parser, clauses);
13305 c_name = "tile";
13306 break;
13307 case PRAGMA_OACC_CLAUSE_USE_DEVICE:
13308 clauses = c_parser_omp_clause_use_device_ptr (parser, clauses);
13309 c_name = "use_device";
13310 break;
13311 case PRAGMA_OACC_CLAUSE_VECTOR:
13312 c_name = "vector";
13313 clauses = c_parser_oacc_shape_clause (parser, OMP_CLAUSE_VECTOR,
13314 c_name, clauses);
13315 break;
13316 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
13317 clauses = c_parser_omp_clause_vector_length (parser, clauses);
13318 c_name = "vector_length";
13319 break;
13320 case PRAGMA_OACC_CLAUSE_WAIT:
13321 clauses = c_parser_oacc_clause_wait (parser, clauses);
13322 c_name = "wait";
13323 break;
13324 case PRAGMA_OACC_CLAUSE_WORKER:
13325 c_name = "worker";
13326 clauses = c_parser_oacc_shape_clause (parser, OMP_CLAUSE_WORKER,
13327 c_name, clauses);
13328 break;
13329 default:
13330 c_parser_error (parser, "expected %<#pragma acc%> clause");
13331 goto saw_error;
13332 }
13333
13334 first = false;
13335
13336 if (((mask >> c_kind) & 1) == 0)
13337 {
13338 /* Remove the invalid clause(s) from the list to avoid
13339 confusing the rest of the compiler. */
13340 clauses = prev;
13341 error_at (here, "%qs is not valid for %qs", c_name, where);
13342 }
13343 }
13344
13345 saw_error:
13346 c_parser_skip_to_pragma_eol (parser);
13347
13348 if (finish_p)
13349 return c_finish_omp_clauses (clauses, C_ORT_ACC);
13350
13351 return clauses;
13352 }
13353
13354 /* Parse all OpenMP clauses. The set clauses allowed by the directive
13355 is a bitmask in MASK. Return the list of clauses found. */
13356
13357 static tree
13358 c_parser_omp_all_clauses (c_parser *parser, omp_clause_mask mask,
13359 const char *where, bool finish_p = true)
13360 {
13361 tree clauses = NULL;
13362 bool first = true, cilk_simd_fn = false;
13363
13364 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
13365 {
13366 location_t here;
13367 pragma_omp_clause c_kind;
13368 const char *c_name;
13369 tree prev = clauses;
13370
13371 if (!first && c_parser_next_token_is (parser, CPP_COMMA))
13372 c_parser_consume_token (parser);
13373
13374 here = c_parser_peek_token (parser)->location;
13375 c_kind = c_parser_omp_clause_name (parser);
13376
13377 switch (c_kind)
13378 {
13379 case PRAGMA_OMP_CLAUSE_COLLAPSE:
13380 clauses = c_parser_omp_clause_collapse (parser, clauses);
13381 c_name = "collapse";
13382 break;
13383 case PRAGMA_OMP_CLAUSE_COPYIN:
13384 clauses = c_parser_omp_clause_copyin (parser, clauses);
13385 c_name = "copyin";
13386 break;
13387 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
13388 clauses = c_parser_omp_clause_copyprivate (parser, clauses);
13389 c_name = "copyprivate";
13390 break;
13391 case PRAGMA_OMP_CLAUSE_DEFAULT:
13392 clauses = c_parser_omp_clause_default (parser, clauses, false);
13393 c_name = "default";
13394 break;
13395 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
13396 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
13397 c_name = "firstprivate";
13398 break;
13399 case PRAGMA_OMP_CLAUSE_FINAL:
13400 clauses = c_parser_omp_clause_final (parser, clauses);
13401 c_name = "final";
13402 break;
13403 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
13404 clauses = c_parser_omp_clause_grainsize (parser, clauses);
13405 c_name = "grainsize";
13406 break;
13407 case PRAGMA_OMP_CLAUSE_HINT:
13408 clauses = c_parser_omp_clause_hint (parser, clauses);
13409 c_name = "hint";
13410 break;
13411 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
13412 clauses = c_parser_omp_clause_defaultmap (parser, clauses);
13413 c_name = "defaultmap";
13414 break;
13415 case PRAGMA_OMP_CLAUSE_IF:
13416 clauses = c_parser_omp_clause_if (parser, clauses, true);
13417 c_name = "if";
13418 break;
13419 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
13420 clauses = c_parser_omp_clause_lastprivate (parser, clauses);
13421 c_name = "lastprivate";
13422 break;
13423 case PRAGMA_OMP_CLAUSE_MERGEABLE:
13424 clauses = c_parser_omp_clause_mergeable (parser, clauses);
13425 c_name = "mergeable";
13426 break;
13427 case PRAGMA_OMP_CLAUSE_NOWAIT:
13428 clauses = c_parser_omp_clause_nowait (parser, clauses);
13429 c_name = "nowait";
13430 break;
13431 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
13432 clauses = c_parser_omp_clause_num_tasks (parser, clauses);
13433 c_name = "num_tasks";
13434 break;
13435 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
13436 clauses = c_parser_omp_clause_num_threads (parser, clauses);
13437 c_name = "num_threads";
13438 break;
13439 case PRAGMA_OMP_CLAUSE_ORDERED:
13440 clauses = c_parser_omp_clause_ordered (parser, clauses);
13441 c_name = "ordered";
13442 break;
13443 case PRAGMA_OMP_CLAUSE_PRIORITY:
13444 clauses = c_parser_omp_clause_priority (parser, clauses);
13445 c_name = "priority";
13446 break;
13447 case PRAGMA_OMP_CLAUSE_PRIVATE:
13448 clauses = c_parser_omp_clause_private (parser, clauses);
13449 c_name = "private";
13450 break;
13451 case PRAGMA_OMP_CLAUSE_REDUCTION:
13452 clauses = c_parser_omp_clause_reduction (parser, clauses);
13453 c_name = "reduction";
13454 break;
13455 case PRAGMA_OMP_CLAUSE_SCHEDULE:
13456 clauses = c_parser_omp_clause_schedule (parser, clauses);
13457 c_name = "schedule";
13458 break;
13459 case PRAGMA_OMP_CLAUSE_SHARED:
13460 clauses = c_parser_omp_clause_shared (parser, clauses);
13461 c_name = "shared";
13462 break;
13463 case PRAGMA_OMP_CLAUSE_UNTIED:
13464 clauses = c_parser_omp_clause_untied (parser, clauses);
13465 c_name = "untied";
13466 break;
13467 case PRAGMA_OMP_CLAUSE_INBRANCH:
13468 case PRAGMA_CILK_CLAUSE_MASK:
13469 clauses = c_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
13470 clauses);
13471 c_name = "inbranch";
13472 break;
13473 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
13474 case PRAGMA_CILK_CLAUSE_NOMASK:
13475 clauses = c_parser_omp_clause_branch (parser, OMP_CLAUSE_NOTINBRANCH,
13476 clauses);
13477 c_name = "notinbranch";
13478 break;
13479 case PRAGMA_OMP_CLAUSE_PARALLEL:
13480 clauses
13481 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
13482 clauses);
13483 c_name = "parallel";
13484 if (!first)
13485 {
13486 clause_not_first:
13487 error_at (here, "%qs must be the first clause of %qs",
13488 c_name, where);
13489 clauses = prev;
13490 }
13491 break;
13492 case PRAGMA_OMP_CLAUSE_FOR:
13493 clauses
13494 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
13495 clauses);
13496 c_name = "for";
13497 if (!first)
13498 goto clause_not_first;
13499 break;
13500 case PRAGMA_OMP_CLAUSE_SECTIONS:
13501 clauses
13502 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
13503 clauses);
13504 c_name = "sections";
13505 if (!first)
13506 goto clause_not_first;
13507 break;
13508 case PRAGMA_OMP_CLAUSE_TASKGROUP:
13509 clauses
13510 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
13511 clauses);
13512 c_name = "taskgroup";
13513 if (!first)
13514 goto clause_not_first;
13515 break;
13516 case PRAGMA_OMP_CLAUSE_LINK:
13517 clauses
13518 = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_LINK, clauses);
13519 c_name = "link";
13520 break;
13521 case PRAGMA_OMP_CLAUSE_TO:
13522 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
13523 clauses
13524 = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_TO_DECLARE,
13525 clauses);
13526 else
13527 clauses = c_parser_omp_clause_to (parser, clauses);
13528 c_name = "to";
13529 break;
13530 case PRAGMA_OMP_CLAUSE_FROM:
13531 clauses = c_parser_omp_clause_from (parser, clauses);
13532 c_name = "from";
13533 break;
13534 case PRAGMA_OMP_CLAUSE_UNIFORM:
13535 clauses = c_parser_omp_clause_uniform (parser, clauses);
13536 c_name = "uniform";
13537 break;
13538 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
13539 clauses = c_parser_omp_clause_num_teams (parser, clauses);
13540 c_name = "num_teams";
13541 break;
13542 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
13543 clauses = c_parser_omp_clause_thread_limit (parser, clauses);
13544 c_name = "thread_limit";
13545 break;
13546 case PRAGMA_OMP_CLAUSE_ALIGNED:
13547 clauses = c_parser_omp_clause_aligned (parser, clauses);
13548 c_name = "aligned";
13549 break;
13550 case PRAGMA_OMP_CLAUSE_LINEAR:
13551 if (((mask >> PRAGMA_CILK_CLAUSE_VECTORLENGTH) & 1) != 0)
13552 cilk_simd_fn = true;
13553 clauses = c_parser_omp_clause_linear (parser, clauses, cilk_simd_fn);
13554 c_name = "linear";
13555 break;
13556 case PRAGMA_OMP_CLAUSE_DEPEND:
13557 clauses = c_parser_omp_clause_depend (parser, clauses);
13558 c_name = "depend";
13559 break;
13560 case PRAGMA_OMP_CLAUSE_MAP:
13561 clauses = c_parser_omp_clause_map (parser, clauses);
13562 c_name = "map";
13563 break;
13564 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
13565 clauses = c_parser_omp_clause_use_device_ptr (parser, clauses);
13566 c_name = "use_device_ptr";
13567 break;
13568 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
13569 clauses = c_parser_omp_clause_is_device_ptr (parser, clauses);
13570 c_name = "is_device_ptr";
13571 break;
13572 case PRAGMA_OMP_CLAUSE_DEVICE:
13573 clauses = c_parser_omp_clause_device (parser, clauses);
13574 c_name = "device";
13575 break;
13576 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
13577 clauses = c_parser_omp_clause_dist_schedule (parser, clauses);
13578 c_name = "dist_schedule";
13579 break;
13580 case PRAGMA_OMP_CLAUSE_PROC_BIND:
13581 clauses = c_parser_omp_clause_proc_bind (parser, clauses);
13582 c_name = "proc_bind";
13583 break;
13584 case PRAGMA_OMP_CLAUSE_SAFELEN:
13585 clauses = c_parser_omp_clause_safelen (parser, clauses);
13586 c_name = "safelen";
13587 break;
13588 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
13589 clauses = c_parser_cilk_clause_vectorlength (parser, clauses, true);
13590 c_name = "simdlen";
13591 break;
13592 case PRAGMA_OMP_CLAUSE_SIMDLEN:
13593 clauses = c_parser_omp_clause_simdlen (parser, clauses);
13594 c_name = "simdlen";
13595 break;
13596 case PRAGMA_OMP_CLAUSE_NOGROUP:
13597 clauses = c_parser_omp_clause_nogroup (parser, clauses);
13598 c_name = "nogroup";
13599 break;
13600 case PRAGMA_OMP_CLAUSE_THREADS:
13601 clauses
13602 = c_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
13603 clauses);
13604 c_name = "threads";
13605 break;
13606 case PRAGMA_OMP_CLAUSE_SIMD:
13607 clauses
13608 = c_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
13609 clauses);
13610 c_name = "simd";
13611 break;
13612 default:
13613 c_parser_error (parser, "expected %<#pragma omp%> clause");
13614 goto saw_error;
13615 }
13616
13617 first = false;
13618
13619 if (((mask >> c_kind) & 1) == 0)
13620 {
13621 /* Remove the invalid clause(s) from the list to avoid
13622 confusing the rest of the compiler. */
13623 clauses = prev;
13624 error_at (here, "%qs is not valid for %qs", c_name, where);
13625 }
13626 }
13627
13628 saw_error:
13629 c_parser_skip_to_pragma_eol (parser);
13630
13631 if (finish_p)
13632 {
13633 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
13634 return c_finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
13635 return c_finish_omp_clauses (clauses, C_ORT_OMP);
13636 }
13637
13638 return clauses;
13639 }
13640
13641 /* OpenACC 2.0, OpenMP 2.5:
13642 structured-block:
13643 statement
13644
13645 In practice, we're also interested in adding the statement to an
13646 outer node. So it is convenient if we work around the fact that
13647 c_parser_statement calls add_stmt. */
13648
13649 static tree
13650 c_parser_omp_structured_block (c_parser *parser, bool *if_p)
13651 {
13652 tree stmt = push_stmt_list ();
13653 c_parser_statement (parser, if_p);
13654 return pop_stmt_list (stmt);
13655 }
13656
13657 /* OpenACC 2.0:
13658 # pragma acc cache (variable-list) new-line
13659
13660 LOC is the location of the #pragma token.
13661 */
13662
13663 static tree
13664 c_parser_oacc_cache (location_t loc, c_parser *parser)
13665 {
13666 tree stmt, clauses;
13667
13668 clauses = c_parser_omp_var_list_parens (parser, OMP_CLAUSE__CACHE_, NULL);
13669 clauses = c_finish_omp_clauses (clauses, C_ORT_ACC);
13670
13671 c_parser_skip_to_pragma_eol (parser);
13672
13673 stmt = make_node (OACC_CACHE);
13674 TREE_TYPE (stmt) = void_type_node;
13675 OACC_CACHE_CLAUSES (stmt) = clauses;
13676 SET_EXPR_LOCATION (stmt, loc);
13677 add_stmt (stmt);
13678
13679 return stmt;
13680 }
13681
13682 /* OpenACC 2.0:
13683 # pragma acc data oacc-data-clause[optseq] new-line
13684 structured-block
13685
13686 LOC is the location of the #pragma token.
13687 */
13688
13689 #define OACC_DATA_CLAUSE_MASK \
13690 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
13691 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
13692 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
13693 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
13694 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
13695 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
13696 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
13697 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
13698 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
13699 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
13700 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) )
13701
13702 static tree
13703 c_parser_oacc_data (location_t loc, c_parser *parser, bool *if_p)
13704 {
13705 tree stmt, clauses, block;
13706
13707 clauses = c_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
13708 "#pragma acc data");
13709
13710 block = c_begin_omp_parallel ();
13711 add_stmt (c_parser_omp_structured_block (parser, if_p));
13712
13713 stmt = c_finish_oacc_data (loc, clauses, block);
13714
13715 return stmt;
13716 }
13717
13718 /* OpenACC 2.0:
13719 # pragma acc declare oacc-data-clause[optseq] new-line
13720 */
13721
13722 #define OACC_DECLARE_CLAUSE_MASK \
13723 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
13724 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
13725 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
13726 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
13727 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
13728 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
13729 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
13730 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
13731 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
13732 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
13733 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
13734 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) )
13735
13736 static void
13737 c_parser_oacc_declare (c_parser *parser)
13738 {
13739 location_t pragma_loc = c_parser_peek_token (parser)->location;
13740 tree clauses, stmt, t, decl;
13741
13742 bool error = false;
13743
13744 c_parser_consume_pragma (parser);
13745
13746 clauses = c_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
13747 "#pragma acc declare");
13748 if (!clauses)
13749 {
13750 error_at (pragma_loc,
13751 "no valid clauses specified in %<#pragma acc declare%>");
13752 return;
13753 }
13754
13755 for (t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
13756 {
13757 location_t loc = OMP_CLAUSE_LOCATION (t);
13758 decl = OMP_CLAUSE_DECL (t);
13759 if (!DECL_P (decl))
13760 {
13761 error_at (loc, "array section in %<#pragma acc declare%>");
13762 error = true;
13763 continue;
13764 }
13765
13766 switch (OMP_CLAUSE_MAP_KIND (t))
13767 {
13768 case GOMP_MAP_FIRSTPRIVATE_POINTER:
13769 case GOMP_MAP_FORCE_ALLOC:
13770 case GOMP_MAP_FORCE_TO:
13771 case GOMP_MAP_FORCE_DEVICEPTR:
13772 case GOMP_MAP_DEVICE_RESIDENT:
13773 break;
13774
13775 case GOMP_MAP_LINK:
13776 if (!global_bindings_p ()
13777 && (TREE_STATIC (decl)
13778 || !DECL_EXTERNAL (decl)))
13779 {
13780 error_at (loc,
13781 "%qD must be a global variable in"
13782 "%<#pragma acc declare link%>",
13783 decl);
13784 error = true;
13785 continue;
13786 }
13787 break;
13788
13789 default:
13790 if (global_bindings_p ())
13791 {
13792 error_at (loc, "invalid OpenACC clause at file scope");
13793 error = true;
13794 continue;
13795 }
13796 if (DECL_EXTERNAL (decl))
13797 {
13798 error_at (loc,
13799 "invalid use of %<extern%> variable %qD "
13800 "in %<#pragma acc declare%>", decl);
13801 error = true;
13802 continue;
13803 }
13804 else if (TREE_PUBLIC (decl))
13805 {
13806 error_at (loc,
13807 "invalid use of %<global%> variable %qD "
13808 "in %<#pragma acc declare%>", decl);
13809 error = true;
13810 continue;
13811 }
13812 break;
13813 }
13814
13815 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
13816 || lookup_attribute ("omp declare target link",
13817 DECL_ATTRIBUTES (decl)))
13818 {
13819 error_at (loc, "variable %qD used more than once with "
13820 "%<#pragma acc declare%>", decl);
13821 error = true;
13822 continue;
13823 }
13824
13825 if (!error)
13826 {
13827 tree id;
13828
13829 if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
13830 id = get_identifier ("omp declare target link");
13831 else
13832 id = get_identifier ("omp declare target");
13833
13834 DECL_ATTRIBUTES (decl)
13835 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
13836
13837 if (global_bindings_p ())
13838 {
13839 symtab_node *node = symtab_node::get (decl);
13840 if (node != NULL)
13841 {
13842 node->offloadable = 1;
13843 if (ENABLE_OFFLOADING)
13844 {
13845 g->have_offload = true;
13846 if (is_a <varpool_node *> (node))
13847 vec_safe_push (offload_vars, decl);
13848 }
13849 }
13850 }
13851 }
13852 }
13853
13854 if (error || global_bindings_p ())
13855 return;
13856
13857 stmt = make_node (OACC_DECLARE);
13858 TREE_TYPE (stmt) = void_type_node;
13859 OACC_DECLARE_CLAUSES (stmt) = clauses;
13860 SET_EXPR_LOCATION (stmt, pragma_loc);
13861
13862 add_stmt (stmt);
13863
13864 return;
13865 }
13866
13867 /* OpenACC 2.0:
13868 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
13869
13870 or
13871
13872 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
13873
13874
13875 LOC is the location of the #pragma token.
13876 */
13877
13878 #define OACC_ENTER_DATA_CLAUSE_MASK \
13879 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
13880 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
13881 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
13882 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
13883 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
13884 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
13885 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
13886
13887 #define OACC_EXIT_DATA_CLAUSE_MASK \
13888 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
13889 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
13890 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
13891 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
13892 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
13893
13894 static void
13895 c_parser_oacc_enter_exit_data (c_parser *parser, bool enter)
13896 {
13897 location_t loc = c_parser_peek_token (parser)->location;
13898 tree clauses, stmt;
13899 const char *p = "";
13900
13901 c_parser_consume_pragma (parser);
13902
13903 if (c_parser_next_token_is (parser, CPP_NAME))
13904 {
13905 p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13906 c_parser_consume_token (parser);
13907 }
13908
13909 if (strcmp (p, "data") != 0)
13910 {
13911 error_at (loc, enter
13912 ? "expected %<data%> after %<#pragma acc enter%>"
13913 : "expected %<data%> after %<#pragma acc exit%>");
13914 parser->error = true;
13915 c_parser_skip_to_pragma_eol (parser);
13916 return;
13917 }
13918
13919 if (enter)
13920 clauses = c_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
13921 "#pragma acc enter data");
13922 else
13923 clauses = c_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
13924 "#pragma acc exit data");
13925
13926 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
13927 {
13928 error_at (loc, enter
13929 ? "%<#pragma acc enter data%> has no data movement clause"
13930 : "%<#pragma acc exit data%> has no data movement clause");
13931 return;
13932 }
13933
13934 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
13935 TREE_TYPE (stmt) = void_type_node;
13936 OMP_STANDALONE_CLAUSES (stmt) = clauses;
13937 SET_EXPR_LOCATION (stmt, loc);
13938 add_stmt (stmt);
13939 }
13940
13941
13942 /* OpenACC 2.0:
13943 # pragma acc host_data oacc-data-clause[optseq] new-line
13944 structured-block
13945 */
13946
13947 #define OACC_HOST_DATA_CLAUSE_MASK \
13948 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
13949
13950 static tree
13951 c_parser_oacc_host_data (location_t loc, c_parser *parser, bool *if_p)
13952 {
13953 tree stmt, clauses, block;
13954
13955 clauses = c_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
13956 "#pragma acc host_data");
13957
13958 block = c_begin_omp_parallel ();
13959 add_stmt (c_parser_omp_structured_block (parser, if_p));
13960 stmt = c_finish_oacc_host_data (loc, clauses, block);
13961 return stmt;
13962 }
13963
13964
13965 /* OpenACC 2.0:
13966
13967 # pragma acc loop oacc-loop-clause[optseq] new-line
13968 structured-block
13969
13970 LOC is the location of the #pragma token.
13971 */
13972
13973 #define OACC_LOOP_CLAUSE_MASK \
13974 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
13975 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
13976 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
13977 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
13978 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
13979 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
13980 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
13981 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
13982 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
13983 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE) )
13984 static tree
13985 c_parser_oacc_loop (location_t loc, c_parser *parser, char *p_name,
13986 omp_clause_mask mask, tree *cclauses, bool *if_p)
13987 {
13988 bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
13989
13990 strcat (p_name, " loop");
13991 mask |= OACC_LOOP_CLAUSE_MASK;
13992
13993 tree clauses = c_parser_oacc_all_clauses (parser, mask, p_name,
13994 cclauses == NULL);
13995 if (cclauses)
13996 {
13997 clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
13998 if (*cclauses)
13999 *cclauses = c_finish_omp_clauses (*cclauses, C_ORT_ACC);
14000 if (clauses)
14001 clauses = c_finish_omp_clauses (clauses, C_ORT_ACC);
14002 }
14003
14004 tree block = c_begin_compound_stmt (true);
14005 tree stmt = c_parser_omp_for_loop (loc, parser, OACC_LOOP, clauses, NULL,
14006 if_p);
14007 block = c_end_compound_stmt (loc, block, true);
14008 add_stmt (block);
14009
14010 return stmt;
14011 }
14012
14013 /* OpenACC 2.0:
14014 # pragma acc kernels oacc-kernels-clause[optseq] new-line
14015 structured-block
14016
14017 or
14018
14019 # pragma acc parallel oacc-parallel-clause[optseq] new-line
14020 structured-block
14021
14022 LOC is the location of the #pragma token.
14023 */
14024
14025 #define OACC_KERNELS_CLAUSE_MASK \
14026 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
14027 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
14028 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
14029 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
14030 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
14031 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
14032 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
14033 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
14034 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
14035 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
14036 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
14037 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
14038 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
14039 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
14040
14041 #define OACC_PARALLEL_CLAUSE_MASK \
14042 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
14043 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
14044 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
14045 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
14046 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
14047 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
14048 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
14049 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
14050 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
14051 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
14052 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
14053 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
14054 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
14055 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
14056 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
14057 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
14058 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
14059 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
14060 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
14061 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
14062
14063 static tree
14064 c_parser_oacc_kernels_parallel (location_t loc, c_parser *parser,
14065 enum pragma_kind p_kind, char *p_name,
14066 bool *if_p)
14067 {
14068 omp_clause_mask mask;
14069 enum tree_code code;
14070 switch (p_kind)
14071 {
14072 case PRAGMA_OACC_KERNELS:
14073 strcat (p_name, " kernels");
14074 mask = OACC_KERNELS_CLAUSE_MASK;
14075 code = OACC_KERNELS;
14076 break;
14077 case PRAGMA_OACC_PARALLEL:
14078 strcat (p_name, " parallel");
14079 mask = OACC_PARALLEL_CLAUSE_MASK;
14080 code = OACC_PARALLEL;
14081 break;
14082 default:
14083 gcc_unreachable ();
14084 }
14085
14086 if (c_parser_next_token_is (parser, CPP_NAME))
14087 {
14088 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
14089 if (strcmp (p, "loop") == 0)
14090 {
14091 c_parser_consume_token (parser);
14092 tree block = c_begin_omp_parallel ();
14093 tree clauses;
14094 c_parser_oacc_loop (loc, parser, p_name, mask, &clauses, if_p);
14095 return c_finish_omp_construct (loc, code, block, clauses);
14096 }
14097 }
14098
14099 tree clauses = c_parser_oacc_all_clauses (parser, mask, p_name);
14100
14101 tree block = c_begin_omp_parallel ();
14102 add_stmt (c_parser_omp_structured_block (parser, if_p));
14103
14104 return c_finish_omp_construct (loc, code, block, clauses);
14105 }
14106
14107 /* OpenACC 2.0:
14108 # pragma acc routine oacc-routine-clause[optseq] new-line
14109 function-definition
14110
14111 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
14112 */
14113
14114 #define OACC_ROUTINE_CLAUSE_MASK \
14115 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
14116 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
14117 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
14118 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) )
14119
14120 /* Parse an OpenACC routine directive. For named directives, we apply
14121 immediately to the named function. For unnamed ones we then parse
14122 a declaration or definition, which must be for a function. */
14123
14124 static void
14125 c_parser_oacc_routine (c_parser *parser, enum pragma_context context)
14126 {
14127 gcc_checking_assert (context == pragma_external);
14128
14129 oacc_routine_data data;
14130 data.error_seen = false;
14131 data.fndecl_seen = false;
14132 data.clauses = NULL_TREE;
14133 data.loc = c_parser_peek_token (parser)->location;
14134
14135 c_parser_consume_pragma (parser);
14136
14137 /* Look for optional '( name )'. */
14138 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
14139 {
14140 c_parser_consume_token (parser); /* '(' */
14141
14142 tree decl = NULL_TREE;
14143 c_token *name_token = c_parser_peek_token (parser);
14144 location_t name_loc = name_token->location;
14145 if (name_token->type == CPP_NAME
14146 && (name_token->id_kind == C_ID_ID
14147 || name_token->id_kind == C_ID_TYPENAME))
14148 {
14149 decl = lookup_name (name_token->value);
14150 if (!decl)
14151 error_at (name_loc,
14152 "%qE has not been declared", name_token->value);
14153 c_parser_consume_token (parser);
14154 }
14155 else
14156 c_parser_error (parser, "expected function name");
14157
14158 if (!decl
14159 || !c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
14160 {
14161 c_parser_skip_to_pragma_eol (parser, false);
14162 return;
14163 }
14164
14165 data.clauses
14166 = c_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
14167 "#pragma acc routine");
14168
14169 if (TREE_CODE (decl) != FUNCTION_DECL)
14170 {
14171 error_at (name_loc, "%qD does not refer to a function", decl);
14172 return;
14173 }
14174
14175 c_finish_oacc_routine (&data, decl, false);
14176 }
14177 else /* No optional '( name )'. */
14178 {
14179 data.clauses
14180 = c_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
14181 "#pragma acc routine");
14182
14183 /* Emit a helpful diagnostic if there's another pragma following this
14184 one. Also don't allow a static assertion declaration, as in the
14185 following we'll just parse a *single* "declaration or function
14186 definition", and the static assertion counts an one. */
14187 if (c_parser_next_token_is (parser, CPP_PRAGMA)
14188 || c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
14189 {
14190 error_at (data.loc,
14191 "%<#pragma acc routine%> not immediately followed by"
14192 " function declaration or definition");
14193 /* ..., and then just keep going. */
14194 return;
14195 }
14196
14197 /* We only have to consider the pragma_external case here. */
14198 if (c_parser_next_token_is (parser, CPP_KEYWORD)
14199 && c_parser_peek_token (parser)->keyword == RID_EXTENSION)
14200 {
14201 int ext = disable_extension_diagnostics ();
14202 do
14203 c_parser_consume_token (parser);
14204 while (c_parser_next_token_is (parser, CPP_KEYWORD)
14205 && c_parser_peek_token (parser)->keyword == RID_EXTENSION);
14206 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
14207 NULL, vNULL, &data);
14208 restore_extension_diagnostics (ext);
14209 }
14210 else
14211 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
14212 NULL, vNULL, &data);
14213 }
14214 }
14215
14216 /* Finalize an OpenACC routine pragma, applying it to FNDECL.
14217 IS_DEFN is true if we're applying it to the definition. */
14218
14219 static void
14220 c_finish_oacc_routine (struct oacc_routine_data *data, tree fndecl,
14221 bool is_defn)
14222 {
14223 /* Keep going if we're in error reporting mode. */
14224 if (data->error_seen
14225 || fndecl == error_mark_node)
14226 return;
14227
14228 if (data->fndecl_seen)
14229 {
14230 error_at (data->loc,
14231 "%<#pragma acc routine%> not immediately followed by"
14232 " a single function declaration or definition");
14233 data->error_seen = true;
14234 return;
14235 }
14236 if (fndecl == NULL_TREE || TREE_CODE (fndecl) != FUNCTION_DECL)
14237 {
14238 error_at (data->loc,
14239 "%<#pragma acc routine%> not immediately followed by"
14240 " function declaration or definition");
14241 data->error_seen = true;
14242 return;
14243 }
14244
14245 if (oacc_get_fn_attrib (fndecl))
14246 {
14247 error_at (data->loc,
14248 "%<#pragma acc routine%> already applied to %qD", fndecl);
14249 data->error_seen = true;
14250 return;
14251 }
14252
14253 if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
14254 {
14255 error_at (data->loc,
14256 "%<#pragma acc routine%> must be applied before %s",
14257 TREE_USED (fndecl) ? "use" : "definition");
14258 data->error_seen = true;
14259 return;
14260 }
14261
14262 /* Process the routine's dimension clauses. */
14263 tree dims = oacc_build_routine_dims (data->clauses);
14264 oacc_replace_fn_attrib (fndecl, dims);
14265
14266 /* Add an "omp declare target" attribute. */
14267 DECL_ATTRIBUTES (fndecl)
14268 = tree_cons (get_identifier ("omp declare target"),
14269 NULL_TREE, DECL_ATTRIBUTES (fndecl));
14270
14271 /* Remember that we've used this "#pragma acc routine". */
14272 data->fndecl_seen = true;
14273 }
14274
14275 /* OpenACC 2.0:
14276 # pragma acc update oacc-update-clause[optseq] new-line
14277 */
14278
14279 #define OACC_UPDATE_CLAUSE_MASK \
14280 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
14281 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
14282 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
14283 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
14284 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
14285 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
14286
14287 static void
14288 c_parser_oacc_update (c_parser *parser)
14289 {
14290 location_t loc = c_parser_peek_token (parser)->location;
14291
14292 c_parser_consume_pragma (parser);
14293
14294 tree clauses = c_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
14295 "#pragma acc update");
14296 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
14297 {
14298 error_at (loc,
14299 "%<#pragma acc update%> must contain at least one "
14300 "%<device%> or %<host%> or %<self%> clause");
14301 return;
14302 }
14303
14304 if (parser->error)
14305 return;
14306
14307 tree stmt = make_node (OACC_UPDATE);
14308 TREE_TYPE (stmt) = void_type_node;
14309 OACC_UPDATE_CLAUSES (stmt) = clauses;
14310 SET_EXPR_LOCATION (stmt, loc);
14311 add_stmt (stmt);
14312 }
14313
14314 /* OpenACC 2.0:
14315 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
14316
14317 LOC is the location of the #pragma token.
14318 */
14319
14320 #define OACC_WAIT_CLAUSE_MASK \
14321 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) )
14322
14323 static tree
14324 c_parser_oacc_wait (location_t loc, c_parser *parser, char *p_name)
14325 {
14326 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
14327
14328 if (c_parser_peek_token (parser)->type == CPP_OPEN_PAREN)
14329 list = c_parser_oacc_wait_list (parser, loc, list);
14330
14331 strcpy (p_name, " wait");
14332 clauses = c_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK, p_name);
14333 stmt = c_finish_oacc_wait (loc, list, clauses);
14334 add_stmt (stmt);
14335
14336 return stmt;
14337 }
14338
14339 /* OpenMP 2.5:
14340 # pragma omp atomic new-line
14341 expression-stmt
14342
14343 expression-stmt:
14344 x binop= expr | x++ | ++x | x-- | --x
14345 binop:
14346 +, *, -, /, &, ^, |, <<, >>
14347
14348 where x is an lvalue expression with scalar type.
14349
14350 OpenMP 3.1:
14351 # pragma omp atomic new-line
14352 update-stmt
14353
14354 # pragma omp atomic read new-line
14355 read-stmt
14356
14357 # pragma omp atomic write new-line
14358 write-stmt
14359
14360 # pragma omp atomic update new-line
14361 update-stmt
14362
14363 # pragma omp atomic capture new-line
14364 capture-stmt
14365
14366 # pragma omp atomic capture new-line
14367 capture-block
14368
14369 read-stmt:
14370 v = x
14371 write-stmt:
14372 x = expr
14373 update-stmt:
14374 expression-stmt | x = x binop expr
14375 capture-stmt:
14376 v = expression-stmt
14377 capture-block:
14378 { v = x; update-stmt; } | { update-stmt; v = x; }
14379
14380 OpenMP 4.0:
14381 update-stmt:
14382 expression-stmt | x = x binop expr | x = expr binop x
14383 capture-stmt:
14384 v = update-stmt
14385 capture-block:
14386 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
14387
14388 where x and v are lvalue expressions with scalar type.
14389
14390 LOC is the location of the #pragma token. */
14391
14392 static void
14393 c_parser_omp_atomic (location_t loc, c_parser *parser)
14394 {
14395 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE;
14396 tree lhs1 = NULL_TREE, rhs1 = NULL_TREE;
14397 tree stmt, orig_lhs, unfolded_lhs = NULL_TREE, unfolded_lhs1 = NULL_TREE;
14398 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
14399 struct c_expr expr;
14400 location_t eloc;
14401 bool structured_block = false;
14402 bool swapped = false;
14403 bool seq_cst = false;
14404 bool non_lvalue_p;
14405
14406 if (c_parser_next_token_is (parser, CPP_NAME))
14407 {
14408 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
14409 if (!strcmp (p, "seq_cst"))
14410 {
14411 seq_cst = true;
14412 c_parser_consume_token (parser);
14413 if (c_parser_next_token_is (parser, CPP_COMMA)
14414 && c_parser_peek_2nd_token (parser)->type == CPP_NAME)
14415 c_parser_consume_token (parser);
14416 }
14417 }
14418 if (c_parser_next_token_is (parser, CPP_NAME))
14419 {
14420 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
14421
14422 if (!strcmp (p, "read"))
14423 code = OMP_ATOMIC_READ;
14424 else if (!strcmp (p, "write"))
14425 code = NOP_EXPR;
14426 else if (!strcmp (p, "update"))
14427 code = OMP_ATOMIC;
14428 else if (!strcmp (p, "capture"))
14429 code = OMP_ATOMIC_CAPTURE_NEW;
14430 else
14431 p = NULL;
14432 if (p)
14433 c_parser_consume_token (parser);
14434 }
14435 if (!seq_cst)
14436 {
14437 if (c_parser_next_token_is (parser, CPP_COMMA)
14438 && c_parser_peek_2nd_token (parser)->type == CPP_NAME)
14439 c_parser_consume_token (parser);
14440
14441 if (c_parser_next_token_is (parser, CPP_NAME))
14442 {
14443 const char *p
14444 = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
14445 if (!strcmp (p, "seq_cst"))
14446 {
14447 seq_cst = true;
14448 c_parser_consume_token (parser);
14449 }
14450 }
14451 }
14452 c_parser_skip_to_pragma_eol (parser);
14453
14454 switch (code)
14455 {
14456 case OMP_ATOMIC_READ:
14457 case NOP_EXPR: /* atomic write */
14458 v = c_parser_cast_expression (parser, NULL).value;
14459 non_lvalue_p = !lvalue_p (v);
14460 v = c_fully_fold (v, false, NULL);
14461 if (v == error_mark_node)
14462 goto saw_error;
14463 if (non_lvalue_p)
14464 v = non_lvalue (v);
14465 loc = c_parser_peek_token (parser)->location;
14466 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
14467 goto saw_error;
14468 if (code == NOP_EXPR)
14469 {
14470 lhs = c_parser_expression (parser).value;
14471 lhs = c_fully_fold (lhs, false, NULL);
14472 if (lhs == error_mark_node)
14473 goto saw_error;
14474 }
14475 else
14476 {
14477 lhs = c_parser_cast_expression (parser, NULL).value;
14478 non_lvalue_p = !lvalue_p (lhs);
14479 lhs = c_fully_fold (lhs, false, NULL);
14480 if (lhs == error_mark_node)
14481 goto saw_error;
14482 if (non_lvalue_p)
14483 lhs = non_lvalue (lhs);
14484 }
14485 if (code == NOP_EXPR)
14486 {
14487 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
14488 opcode. */
14489 code = OMP_ATOMIC;
14490 rhs = lhs;
14491 lhs = v;
14492 v = NULL_TREE;
14493 }
14494 goto done;
14495 case OMP_ATOMIC_CAPTURE_NEW:
14496 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
14497 {
14498 c_parser_consume_token (parser);
14499 structured_block = true;
14500 }
14501 else
14502 {
14503 v = c_parser_cast_expression (parser, NULL).value;
14504 non_lvalue_p = !lvalue_p (v);
14505 v = c_fully_fold (v, false, NULL);
14506 if (v == error_mark_node)
14507 goto saw_error;
14508 if (non_lvalue_p)
14509 v = non_lvalue (v);
14510 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
14511 goto saw_error;
14512 }
14513 break;
14514 default:
14515 break;
14516 }
14517
14518 /* For structured_block case we don't know yet whether
14519 old or new x should be captured. */
14520 restart:
14521 eloc = c_parser_peek_token (parser)->location;
14522 expr = c_parser_cast_expression (parser, NULL);
14523 lhs = expr.value;
14524 expr = default_function_array_conversion (eloc, expr);
14525 unfolded_lhs = expr.value;
14526 lhs = c_fully_fold (lhs, false, NULL);
14527 orig_lhs = lhs;
14528 switch (TREE_CODE (lhs))
14529 {
14530 case ERROR_MARK:
14531 saw_error:
14532 c_parser_skip_to_end_of_block_or_statement (parser);
14533 if (structured_block)
14534 {
14535 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
14536 c_parser_consume_token (parser);
14537 else if (code == OMP_ATOMIC_CAPTURE_NEW)
14538 {
14539 c_parser_skip_to_end_of_block_or_statement (parser);
14540 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
14541 c_parser_consume_token (parser);
14542 }
14543 }
14544 return;
14545
14546 case POSTINCREMENT_EXPR:
14547 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
14548 code = OMP_ATOMIC_CAPTURE_OLD;
14549 /* FALLTHROUGH */
14550 case PREINCREMENT_EXPR:
14551 lhs = TREE_OPERAND (lhs, 0);
14552 unfolded_lhs = NULL_TREE;
14553 opcode = PLUS_EXPR;
14554 rhs = integer_one_node;
14555 break;
14556
14557 case POSTDECREMENT_EXPR:
14558 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
14559 code = OMP_ATOMIC_CAPTURE_OLD;
14560 /* FALLTHROUGH */
14561 case PREDECREMENT_EXPR:
14562 lhs = TREE_OPERAND (lhs, 0);
14563 unfolded_lhs = NULL_TREE;
14564 opcode = MINUS_EXPR;
14565 rhs = integer_one_node;
14566 break;
14567
14568 case COMPOUND_EXPR:
14569 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
14570 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
14571 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
14572 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
14573 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
14574 (TREE_OPERAND (lhs, 1), 0), 0)))
14575 == BOOLEAN_TYPE)
14576 /* Undo effects of boolean_increment for post {in,de}crement. */
14577 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
14578 /* FALLTHRU */
14579 case MODIFY_EXPR:
14580 if (TREE_CODE (lhs) == MODIFY_EXPR
14581 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
14582 {
14583 /* Undo effects of boolean_increment. */
14584 if (integer_onep (TREE_OPERAND (lhs, 1)))
14585 {
14586 /* This is pre or post increment. */
14587 rhs = TREE_OPERAND (lhs, 1);
14588 lhs = TREE_OPERAND (lhs, 0);
14589 unfolded_lhs = NULL_TREE;
14590 opcode = NOP_EXPR;
14591 if (code == OMP_ATOMIC_CAPTURE_NEW
14592 && !structured_block
14593 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
14594 code = OMP_ATOMIC_CAPTURE_OLD;
14595 break;
14596 }
14597 if (TREE_CODE (TREE_OPERAND (lhs, 1)) == TRUTH_NOT_EXPR
14598 && TREE_OPERAND (lhs, 0)
14599 == TREE_OPERAND (TREE_OPERAND (lhs, 1), 0))
14600 {
14601 /* This is pre or post decrement. */
14602 rhs = TREE_OPERAND (lhs, 1);
14603 lhs = TREE_OPERAND (lhs, 0);
14604 unfolded_lhs = NULL_TREE;
14605 opcode = NOP_EXPR;
14606 if (code == OMP_ATOMIC_CAPTURE_NEW
14607 && !structured_block
14608 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
14609 code = OMP_ATOMIC_CAPTURE_OLD;
14610 break;
14611 }
14612 }
14613 /* FALLTHRU */
14614 default:
14615 if (!lvalue_p (unfolded_lhs))
14616 lhs = non_lvalue (lhs);
14617 switch (c_parser_peek_token (parser)->type)
14618 {
14619 case CPP_MULT_EQ:
14620 opcode = MULT_EXPR;
14621 break;
14622 case CPP_DIV_EQ:
14623 opcode = TRUNC_DIV_EXPR;
14624 break;
14625 case CPP_PLUS_EQ:
14626 opcode = PLUS_EXPR;
14627 break;
14628 case CPP_MINUS_EQ:
14629 opcode = MINUS_EXPR;
14630 break;
14631 case CPP_LSHIFT_EQ:
14632 opcode = LSHIFT_EXPR;
14633 break;
14634 case CPP_RSHIFT_EQ:
14635 opcode = RSHIFT_EXPR;
14636 break;
14637 case CPP_AND_EQ:
14638 opcode = BIT_AND_EXPR;
14639 break;
14640 case CPP_OR_EQ:
14641 opcode = BIT_IOR_EXPR;
14642 break;
14643 case CPP_XOR_EQ:
14644 opcode = BIT_XOR_EXPR;
14645 break;
14646 case CPP_EQ:
14647 c_parser_consume_token (parser);
14648 eloc = c_parser_peek_token (parser)->location;
14649 expr = c_parser_expr_no_commas (parser, NULL, unfolded_lhs);
14650 rhs1 = expr.value;
14651 switch (TREE_CODE (rhs1))
14652 {
14653 case MULT_EXPR:
14654 case TRUNC_DIV_EXPR:
14655 case RDIV_EXPR:
14656 case PLUS_EXPR:
14657 case MINUS_EXPR:
14658 case LSHIFT_EXPR:
14659 case RSHIFT_EXPR:
14660 case BIT_AND_EXPR:
14661 case BIT_IOR_EXPR:
14662 case BIT_XOR_EXPR:
14663 if (c_tree_equal (TREE_OPERAND (rhs1, 0), unfolded_lhs))
14664 {
14665 opcode = TREE_CODE (rhs1);
14666 rhs = c_fully_fold (TREE_OPERAND (rhs1, 1), false, NULL);
14667 rhs1 = c_fully_fold (TREE_OPERAND (rhs1, 0), false, NULL);
14668 goto stmt_done;
14669 }
14670 if (c_tree_equal (TREE_OPERAND (rhs1, 1), unfolded_lhs))
14671 {
14672 opcode = TREE_CODE (rhs1);
14673 rhs = c_fully_fold (TREE_OPERAND (rhs1, 0), false, NULL);
14674 rhs1 = c_fully_fold (TREE_OPERAND (rhs1, 1), false, NULL);
14675 swapped = !commutative_tree_code (opcode);
14676 goto stmt_done;
14677 }
14678 break;
14679 case ERROR_MARK:
14680 goto saw_error;
14681 default:
14682 break;
14683 }
14684 if (c_parser_peek_token (parser)->type == CPP_SEMICOLON)
14685 {
14686 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
14687 {
14688 code = OMP_ATOMIC_CAPTURE_OLD;
14689 v = lhs;
14690 lhs = NULL_TREE;
14691 expr = default_function_array_read_conversion (eloc, expr);
14692 unfolded_lhs1 = expr.value;
14693 lhs1 = c_fully_fold (unfolded_lhs1, false, NULL);
14694 rhs1 = NULL_TREE;
14695 c_parser_consume_token (parser);
14696 goto restart;
14697 }
14698 if (structured_block)
14699 {
14700 opcode = NOP_EXPR;
14701 expr = default_function_array_read_conversion (eloc, expr);
14702 rhs = c_fully_fold (expr.value, false, NULL);
14703 rhs1 = NULL_TREE;
14704 goto stmt_done;
14705 }
14706 }
14707 c_parser_error (parser, "invalid form of %<#pragma omp atomic%>");
14708 goto saw_error;
14709 default:
14710 c_parser_error (parser,
14711 "invalid operator for %<#pragma omp atomic%>");
14712 goto saw_error;
14713 }
14714
14715 /* Arrange to pass the location of the assignment operator to
14716 c_finish_omp_atomic. */
14717 loc = c_parser_peek_token (parser)->location;
14718 c_parser_consume_token (parser);
14719 eloc = c_parser_peek_token (parser)->location;
14720 expr = c_parser_expression (parser);
14721 expr = default_function_array_read_conversion (eloc, expr);
14722 rhs = expr.value;
14723 rhs = c_fully_fold (rhs, false, NULL);
14724 break;
14725 }
14726 stmt_done:
14727 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
14728 {
14729 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
14730 goto saw_error;
14731 v = c_parser_cast_expression (parser, NULL).value;
14732 non_lvalue_p = !lvalue_p (v);
14733 v = c_fully_fold (v, false, NULL);
14734 if (v == error_mark_node)
14735 goto saw_error;
14736 if (non_lvalue_p)
14737 v = non_lvalue (v);
14738 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
14739 goto saw_error;
14740 eloc = c_parser_peek_token (parser)->location;
14741 expr = c_parser_cast_expression (parser, NULL);
14742 lhs1 = expr.value;
14743 expr = default_function_array_read_conversion (eloc, expr);
14744 unfolded_lhs1 = expr.value;
14745 lhs1 = c_fully_fold (lhs1, false, NULL);
14746 if (lhs1 == error_mark_node)
14747 goto saw_error;
14748 if (!lvalue_p (unfolded_lhs1))
14749 lhs1 = non_lvalue (lhs1);
14750 }
14751 if (structured_block)
14752 {
14753 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
14754 c_parser_require (parser, CPP_CLOSE_BRACE, "expected %<}%>");
14755 }
14756 done:
14757 if (unfolded_lhs && unfolded_lhs1
14758 && !c_tree_equal (unfolded_lhs, unfolded_lhs1))
14759 {
14760 error ("%<#pragma omp atomic capture%> uses two different "
14761 "expressions for memory");
14762 stmt = error_mark_node;
14763 }
14764 else
14765 stmt = c_finish_omp_atomic (loc, code, opcode, lhs, rhs, v, lhs1, rhs1,
14766 swapped, seq_cst);
14767 if (stmt != error_mark_node)
14768 add_stmt (stmt);
14769
14770 if (!structured_block)
14771 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
14772 }
14773
14774
14775 /* OpenMP 2.5:
14776 # pragma omp barrier new-line
14777 */
14778
14779 static void
14780 c_parser_omp_barrier (c_parser *parser)
14781 {
14782 location_t loc = c_parser_peek_token (parser)->location;
14783 c_parser_consume_pragma (parser);
14784 c_parser_skip_to_pragma_eol (parser);
14785
14786 c_finish_omp_barrier (loc);
14787 }
14788
14789 /* OpenMP 2.5:
14790 # pragma omp critical [(name)] new-line
14791 structured-block
14792
14793 OpenMP 4.5:
14794 # pragma omp critical [(name) [hint(expression)]] new-line
14795
14796 LOC is the location of the #pragma itself. */
14797
14798 #define OMP_CRITICAL_CLAUSE_MASK \
14799 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
14800
14801 static tree
14802 c_parser_omp_critical (location_t loc, c_parser *parser, bool *if_p)
14803 {
14804 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
14805
14806 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
14807 {
14808 c_parser_consume_token (parser);
14809 if (c_parser_next_token_is (parser, CPP_NAME))
14810 {
14811 name = c_parser_peek_token (parser)->value;
14812 c_parser_consume_token (parser);
14813 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
14814 }
14815 else
14816 c_parser_error (parser, "expected identifier");
14817
14818 clauses = c_parser_omp_all_clauses (parser,
14819 OMP_CRITICAL_CLAUSE_MASK,
14820 "#pragma omp critical");
14821 }
14822 else
14823 {
14824 if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
14825 c_parser_error (parser, "expected %<(%> or end of line");
14826 c_parser_skip_to_pragma_eol (parser);
14827 }
14828
14829 stmt = c_parser_omp_structured_block (parser, if_p);
14830 return c_finish_omp_critical (loc, stmt, name, clauses);
14831 }
14832
14833 /* OpenMP 2.5:
14834 # pragma omp flush flush-vars[opt] new-line
14835
14836 flush-vars:
14837 ( variable-list ) */
14838
14839 static void
14840 c_parser_omp_flush (c_parser *parser)
14841 {
14842 location_t loc = c_parser_peek_token (parser)->location;
14843 c_parser_consume_pragma (parser);
14844 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
14845 c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
14846 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
14847 c_parser_error (parser, "expected %<(%> or end of line");
14848 c_parser_skip_to_pragma_eol (parser);
14849
14850 c_finish_omp_flush (loc);
14851 }
14852
14853 /* Parse the restricted form of loop statements allowed by OpenACC and OpenMP.
14854 The real trick here is to determine the loop control variable early
14855 so that we can push a new decl if necessary to make it private.
14856 LOC is the location of the "acc" or "omp" in "#pragma acc" or "#pragma omp",
14857 respectively. */
14858
14859 static tree
14860 c_parser_omp_for_loop (location_t loc, c_parser *parser, enum tree_code code,
14861 tree clauses, tree *cclauses, bool *if_p)
14862 {
14863 tree decl, cond, incr, save_break, save_cont, body, init, stmt, cl;
14864 tree declv, condv, incrv, initv, ret = NULL_TREE;
14865 tree pre_body = NULL_TREE, this_pre_body;
14866 tree ordered_cl = NULL_TREE;
14867 bool fail = false, open_brace_parsed = false;
14868 int i, collapse = 1, ordered = 0, count, nbraces = 0;
14869 location_t for_loc;
14870 vec<tree, va_gc> *for_block = make_tree_vector ();
14871
14872 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
14873 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
14874 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
14875 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
14876 && OMP_CLAUSE_ORDERED_EXPR (cl))
14877 {
14878 ordered_cl = cl;
14879 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
14880 }
14881
14882 if (ordered && ordered < collapse)
14883 {
14884 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
14885 "%<ordered%> clause parameter is less than %<collapse%>");
14886 OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
14887 = build_int_cst (NULL_TREE, collapse);
14888 ordered = collapse;
14889 }
14890 if (ordered)
14891 {
14892 for (tree *pc = &clauses; *pc; )
14893 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
14894 {
14895 error_at (OMP_CLAUSE_LOCATION (*pc),
14896 "%<linear%> clause may not be specified together "
14897 "with %<ordered%> clause with a parameter");
14898 *pc = OMP_CLAUSE_CHAIN (*pc);
14899 }
14900 else
14901 pc = &OMP_CLAUSE_CHAIN (*pc);
14902 }
14903
14904 gcc_assert (collapse >= 1 && ordered >= 0);
14905 count = ordered ? ordered : collapse;
14906
14907 declv = make_tree_vec (count);
14908 initv = make_tree_vec (count);
14909 condv = make_tree_vec (count);
14910 incrv = make_tree_vec (count);
14911
14912 if (code != CILK_FOR
14913 && !c_parser_next_token_is_keyword (parser, RID_FOR))
14914 {
14915 c_parser_error (parser, "for statement expected");
14916 return NULL;
14917 }
14918 if (code == CILK_FOR
14919 && !c_parser_next_token_is_keyword (parser, RID_CILK_FOR))
14920 {
14921 c_parser_error (parser, "_Cilk_for statement expected");
14922 return NULL;
14923 }
14924 for_loc = c_parser_peek_token (parser)->location;
14925 c_parser_consume_token (parser);
14926
14927 for (i = 0; i < count; i++)
14928 {
14929 int bracecount = 0;
14930
14931 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
14932 goto pop_scopes;
14933
14934 /* Parse the initialization declaration or expression. */
14935 if (c_parser_next_tokens_start_declaration (parser))
14936 {
14937 if (i > 0)
14938 vec_safe_push (for_block, c_begin_compound_stmt (true));
14939 this_pre_body = push_stmt_list ();
14940 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
14941 NULL, vNULL);
14942 if (this_pre_body)
14943 {
14944 this_pre_body = pop_stmt_list (this_pre_body);
14945 if (pre_body)
14946 {
14947 tree t = pre_body;
14948 pre_body = push_stmt_list ();
14949 add_stmt (t);
14950 add_stmt (this_pre_body);
14951 pre_body = pop_stmt_list (pre_body);
14952 }
14953 else
14954 pre_body = this_pre_body;
14955 }
14956 decl = check_for_loop_decls (for_loc, flag_isoc99);
14957 if (decl == NULL)
14958 goto error_init;
14959 if (DECL_INITIAL (decl) == error_mark_node)
14960 decl = error_mark_node;
14961 init = decl;
14962 }
14963 else if (c_parser_next_token_is (parser, CPP_NAME)
14964 && c_parser_peek_2nd_token (parser)->type == CPP_EQ)
14965 {
14966 struct c_expr decl_exp;
14967 struct c_expr init_exp;
14968 location_t init_loc;
14969
14970 decl_exp = c_parser_postfix_expression (parser);
14971 decl = decl_exp.value;
14972
14973 c_parser_require (parser, CPP_EQ, "expected %<=%>");
14974
14975 init_loc = c_parser_peek_token (parser)->location;
14976 init_exp = c_parser_expr_no_commas (parser, NULL);
14977 init_exp = default_function_array_read_conversion (init_loc,
14978 init_exp);
14979 init = build_modify_expr (init_loc, decl, decl_exp.original_type,
14980 NOP_EXPR, init_loc, init_exp.value,
14981 init_exp.original_type);
14982 init = c_process_expr_stmt (init_loc, init);
14983
14984 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
14985 }
14986 else
14987 {
14988 error_init:
14989 c_parser_error (parser,
14990 "expected iteration declaration or initialization");
14991 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
14992 "expected %<)%>");
14993 fail = true;
14994 goto parse_next;
14995 }
14996
14997 /* Parse the loop condition. */
14998 cond = NULL_TREE;
14999 if (c_parser_next_token_is_not (parser, CPP_SEMICOLON))
15000 {
15001 location_t cond_loc = c_parser_peek_token (parser)->location;
15002 struct c_expr cond_expr
15003 = c_parser_binary_expression (parser, NULL, NULL_TREE);
15004
15005 cond = cond_expr.value;
15006 cond = c_objc_common_truthvalue_conversion (cond_loc, cond);
15007 cond = c_fully_fold (cond, false, NULL);
15008 switch (cond_expr.original_code)
15009 {
15010 case GT_EXPR:
15011 case GE_EXPR:
15012 case LT_EXPR:
15013 case LE_EXPR:
15014 break;
15015 case NE_EXPR:
15016 if (code == CILK_SIMD || code == CILK_FOR)
15017 break;
15018 /* FALLTHRU. */
15019 default:
15020 /* Can't be cond = error_mark_node, because we want to preserve
15021 the location until c_finish_omp_for. */
15022 cond = build1 (NOP_EXPR, boolean_type_node, error_mark_node);
15023 break;
15024 }
15025 protected_set_expr_location (cond, cond_loc);
15026 }
15027 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
15028
15029 /* Parse the increment expression. */
15030 incr = NULL_TREE;
15031 if (c_parser_next_token_is_not (parser, CPP_CLOSE_PAREN))
15032 {
15033 location_t incr_loc = c_parser_peek_token (parser)->location;
15034
15035 incr = c_process_expr_stmt (incr_loc,
15036 c_parser_expression (parser).value);
15037 }
15038 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
15039
15040 if (decl == NULL || decl == error_mark_node || init == error_mark_node)
15041 fail = true;
15042 else
15043 {
15044 TREE_VEC_ELT (declv, i) = decl;
15045 TREE_VEC_ELT (initv, i) = init;
15046 TREE_VEC_ELT (condv, i) = cond;
15047 TREE_VEC_ELT (incrv, i) = incr;
15048 }
15049
15050 parse_next:
15051 if (i == count - 1)
15052 break;
15053
15054 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
15055 in between the collapsed for loops to be still considered perfectly
15056 nested. Hopefully the final version clarifies this.
15057 For now handle (multiple) {'s and empty statements. */
15058 do
15059 {
15060 if (c_parser_next_token_is_keyword (parser, RID_FOR))
15061 {
15062 c_parser_consume_token (parser);
15063 break;
15064 }
15065 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
15066 {
15067 c_parser_consume_token (parser);
15068 bracecount++;
15069 }
15070 else if (bracecount
15071 && c_parser_next_token_is (parser, CPP_SEMICOLON))
15072 c_parser_consume_token (parser);
15073 else
15074 {
15075 c_parser_error (parser, "not enough perfectly nested loops");
15076 if (bracecount)
15077 {
15078 open_brace_parsed = true;
15079 bracecount--;
15080 }
15081 fail = true;
15082 count = 0;
15083 break;
15084 }
15085 }
15086 while (1);
15087
15088 nbraces += bracecount;
15089 }
15090
15091 if (nbraces)
15092 if_p = NULL;
15093
15094 save_break = c_break_label;
15095 if (code == CILK_SIMD)
15096 c_break_label = build_int_cst (size_type_node, 2);
15097 else
15098 c_break_label = size_one_node;
15099 save_cont = c_cont_label;
15100 c_cont_label = NULL_TREE;
15101 body = push_stmt_list ();
15102
15103 if (open_brace_parsed)
15104 {
15105 location_t here = c_parser_peek_token (parser)->location;
15106 stmt = c_begin_compound_stmt (true);
15107 c_parser_compound_statement_nostart (parser);
15108 add_stmt (c_end_compound_stmt (here, stmt, true));
15109 }
15110 else
15111 add_stmt (c_parser_c99_block_statement (parser, if_p));
15112 if (c_cont_label)
15113 {
15114 tree t = build1 (LABEL_EXPR, void_type_node, c_cont_label);
15115 SET_EXPR_LOCATION (t, loc);
15116 add_stmt (t);
15117 }
15118
15119 body = pop_stmt_list (body);
15120 c_break_label = save_break;
15121 c_cont_label = save_cont;
15122
15123 while (nbraces)
15124 {
15125 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
15126 {
15127 c_parser_consume_token (parser);
15128 nbraces--;
15129 }
15130 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
15131 c_parser_consume_token (parser);
15132 else
15133 {
15134 c_parser_error (parser, "collapsed loops not perfectly nested");
15135 while (nbraces)
15136 {
15137 location_t here = c_parser_peek_token (parser)->location;
15138 stmt = c_begin_compound_stmt (true);
15139 add_stmt (body);
15140 c_parser_compound_statement_nostart (parser);
15141 body = c_end_compound_stmt (here, stmt, true);
15142 nbraces--;
15143 }
15144 goto pop_scopes;
15145 }
15146 }
15147
15148 /* Only bother calling c_finish_omp_for if we haven't already generated
15149 an error from the initialization parsing. */
15150 if (!fail)
15151 {
15152 stmt = c_finish_omp_for (loc, code, declv, NULL, initv, condv,
15153 incrv, body, pre_body);
15154
15155 /* Check for iterators appearing in lb, b or incr expressions. */
15156 if (stmt && !c_omp_check_loop_iv (stmt, declv, NULL))
15157 stmt = NULL_TREE;
15158
15159 if (stmt)
15160 {
15161 add_stmt (stmt);
15162
15163 if (cclauses != NULL
15164 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL)
15165 {
15166 tree *c;
15167 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
15168 if (OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_FIRSTPRIVATE
15169 && OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_LASTPRIVATE)
15170 c = &OMP_CLAUSE_CHAIN (*c);
15171 else
15172 {
15173 for (i = 0; i < count; i++)
15174 if (TREE_VEC_ELT (declv, i) == OMP_CLAUSE_DECL (*c))
15175 break;
15176 if (i == count)
15177 c = &OMP_CLAUSE_CHAIN (*c);
15178 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE)
15179 {
15180 error_at (loc,
15181 "iteration variable %qD should not be firstprivate",
15182 OMP_CLAUSE_DECL (*c));
15183 *c = OMP_CLAUSE_CHAIN (*c);
15184 }
15185 else
15186 {
15187 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
15188 tree l = *c;
15189 *c = OMP_CLAUSE_CHAIN (*c);
15190 if (code == OMP_SIMD)
15191 {
15192 OMP_CLAUSE_CHAIN (l)
15193 = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
15194 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
15195 }
15196 else
15197 {
15198 OMP_CLAUSE_CHAIN (l) = clauses;
15199 clauses = l;
15200 }
15201 }
15202 }
15203 }
15204 OMP_FOR_CLAUSES (stmt) = clauses;
15205 }
15206 ret = stmt;
15207 }
15208 pop_scopes:
15209 while (!for_block->is_empty ())
15210 {
15211 /* FIXME diagnostics: LOC below should be the actual location of
15212 this particular for block. We need to build a list of
15213 locations to go along with FOR_BLOCK. */
15214 stmt = c_end_compound_stmt (loc, for_block->pop (), true);
15215 add_stmt (stmt);
15216 }
15217 release_tree_vector (for_block);
15218 return ret;
15219 }
15220
15221 /* Helper function for OpenMP parsing, split clauses and call
15222 finish_omp_clauses on each of the set of clauses afterwards. */
15223
15224 static void
15225 omp_split_clauses (location_t loc, enum tree_code code,
15226 omp_clause_mask mask, tree clauses, tree *cclauses)
15227 {
15228 int i;
15229 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
15230 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
15231 if (cclauses[i])
15232 cclauses[i] = c_finish_omp_clauses (cclauses[i], C_ORT_OMP);
15233 }
15234
15235 /* OpenMP 4.0:
15236 #pragma omp simd simd-clause[optseq] new-line
15237 for-loop
15238
15239 LOC is the location of the #pragma token.
15240 */
15241
15242 #define OMP_SIMD_CLAUSE_MASK \
15243 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
15244 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
15245 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
15246 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
15247 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
15248 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
15249 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
15250 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
15251
15252 static tree
15253 c_parser_omp_simd (location_t loc, c_parser *parser,
15254 char *p_name, omp_clause_mask mask, tree *cclauses,
15255 bool *if_p)
15256 {
15257 tree block, clauses, ret;
15258
15259 strcat (p_name, " simd");
15260 mask |= OMP_SIMD_CLAUSE_MASK;
15261
15262 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
15263 if (cclauses)
15264 {
15265 omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
15266 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
15267 tree c = omp_find_clause (cclauses[C_OMP_CLAUSE_SPLIT_FOR],
15268 OMP_CLAUSE_ORDERED);
15269 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
15270 {
15271 error_at (OMP_CLAUSE_LOCATION (c),
15272 "%<ordered%> clause with parameter may not be specified "
15273 "on %qs construct", p_name);
15274 OMP_CLAUSE_ORDERED_EXPR (c) = NULL_TREE;
15275 }
15276 }
15277
15278 block = c_begin_compound_stmt (true);
15279 ret = c_parser_omp_for_loop (loc, parser, OMP_SIMD, clauses, cclauses, if_p);
15280 block = c_end_compound_stmt (loc, block, true);
15281 add_stmt (block);
15282
15283 return ret;
15284 }
15285
15286 /* OpenMP 2.5:
15287 #pragma omp for for-clause[optseq] new-line
15288 for-loop
15289
15290 OpenMP 4.0:
15291 #pragma omp for simd for-simd-clause[optseq] new-line
15292 for-loop
15293
15294 LOC is the location of the #pragma token.
15295 */
15296
15297 #define OMP_FOR_CLAUSE_MASK \
15298 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
15299 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
15300 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
15301 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
15302 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
15303 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
15304 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
15305 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
15306 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
15307
15308 static tree
15309 c_parser_omp_for (location_t loc, c_parser *parser,
15310 char *p_name, omp_clause_mask mask, tree *cclauses,
15311 bool *if_p)
15312 {
15313 tree block, clauses, ret;
15314
15315 strcat (p_name, " for");
15316 mask |= OMP_FOR_CLAUSE_MASK;
15317 /* parallel for{, simd} disallows nowait clause, but for
15318 target {teams distribute ,}parallel for{, simd} it should be accepted. */
15319 if (cclauses && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
15320 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
15321 /* Composite distribute parallel for{, simd} disallows ordered clause. */
15322 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
15323 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
15324
15325 if (c_parser_next_token_is (parser, CPP_NAME))
15326 {
15327 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
15328
15329 if (strcmp (p, "simd") == 0)
15330 {
15331 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
15332 if (cclauses == NULL)
15333 cclauses = cclauses_buf;
15334
15335 c_parser_consume_token (parser);
15336 if (!flag_openmp) /* flag_openmp_simd */
15337 return c_parser_omp_simd (loc, parser, p_name, mask, cclauses,
15338 if_p);
15339 block = c_begin_compound_stmt (true);
15340 ret = c_parser_omp_simd (loc, parser, p_name, mask, cclauses, if_p);
15341 block = c_end_compound_stmt (loc, block, true);
15342 if (ret == NULL_TREE)
15343 return ret;
15344 ret = make_node (OMP_FOR);
15345 TREE_TYPE (ret) = void_type_node;
15346 OMP_FOR_BODY (ret) = block;
15347 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
15348 SET_EXPR_LOCATION (ret, loc);
15349 add_stmt (ret);
15350 return ret;
15351 }
15352 }
15353 if (!flag_openmp) /* flag_openmp_simd */
15354 {
15355 c_parser_skip_to_pragma_eol (parser, false);
15356 return NULL_TREE;
15357 }
15358
15359 /* Composite distribute parallel for disallows linear clause. */
15360 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
15361 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
15362
15363 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
15364 if (cclauses)
15365 {
15366 omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
15367 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
15368 }
15369
15370 block = c_begin_compound_stmt (true);
15371 ret = c_parser_omp_for_loop (loc, parser, OMP_FOR, clauses, cclauses, if_p);
15372 block = c_end_compound_stmt (loc, block, true);
15373 add_stmt (block);
15374
15375 return ret;
15376 }
15377
15378 /* OpenMP 2.5:
15379 # pragma omp master new-line
15380 structured-block
15381
15382 LOC is the location of the #pragma token.
15383 */
15384
15385 static tree
15386 c_parser_omp_master (location_t loc, c_parser *parser, bool *if_p)
15387 {
15388 c_parser_skip_to_pragma_eol (parser);
15389 return c_finish_omp_master (loc, c_parser_omp_structured_block (parser,
15390 if_p));
15391 }
15392
15393 /* OpenMP 2.5:
15394 # pragma omp ordered new-line
15395 structured-block
15396
15397 OpenMP 4.5:
15398 # pragma omp ordered ordered-clauses new-line
15399 structured-block
15400
15401 # pragma omp ordered depend-clauses new-line */
15402
15403 #define OMP_ORDERED_CLAUSE_MASK \
15404 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
15405 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
15406
15407 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
15408 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
15409
15410 static bool
15411 c_parser_omp_ordered (c_parser *parser, enum pragma_context context,
15412 bool *if_p)
15413 {
15414 location_t loc = c_parser_peek_token (parser)->location;
15415 c_parser_consume_pragma (parser);
15416
15417 if (context != pragma_stmt && context != pragma_compound)
15418 {
15419 c_parser_error (parser, "expected declaration specifiers");
15420 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
15421 return false;
15422 }
15423
15424 if (c_parser_next_token_is (parser, CPP_NAME))
15425 {
15426 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
15427
15428 if (!strcmp ("depend", p))
15429 {
15430 if (context == pragma_stmt)
15431 {
15432 error_at (loc,
15433 "%<#pragma omp ordered%> with %<depend> clause may "
15434 "only be used in compound statements");
15435 c_parser_skip_to_pragma_eol (parser, false);
15436 return false;
15437 }
15438
15439 tree clauses
15440 = c_parser_omp_all_clauses (parser,
15441 OMP_ORDERED_DEPEND_CLAUSE_MASK,
15442 "#pragma omp ordered");
15443 c_finish_omp_ordered (loc, clauses, NULL_TREE);
15444 return false;
15445 }
15446 }
15447
15448 tree clauses = c_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
15449 "#pragma omp ordered");
15450 c_finish_omp_ordered (loc, clauses,
15451 c_parser_omp_structured_block (parser, if_p));
15452 return true;
15453 }
15454
15455 /* OpenMP 2.5:
15456
15457 section-scope:
15458 { section-sequence }
15459
15460 section-sequence:
15461 section-directive[opt] structured-block
15462 section-sequence section-directive structured-block
15463
15464 SECTIONS_LOC is the location of the #pragma omp sections. */
15465
15466 static tree
15467 c_parser_omp_sections_scope (location_t sections_loc, c_parser *parser)
15468 {
15469 tree stmt, substmt;
15470 bool error_suppress = false;
15471 location_t loc;
15472
15473 loc = c_parser_peek_token (parser)->location;
15474 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
15475 {
15476 /* Avoid skipping until the end of the block. */
15477 parser->error = false;
15478 return NULL_TREE;
15479 }
15480
15481 stmt = push_stmt_list ();
15482
15483 if (c_parser_peek_token (parser)->pragma_kind != PRAGMA_OMP_SECTION)
15484 {
15485 substmt = c_parser_omp_structured_block (parser, NULL);
15486 substmt = build1 (OMP_SECTION, void_type_node, substmt);
15487 SET_EXPR_LOCATION (substmt, loc);
15488 add_stmt (substmt);
15489 }
15490
15491 while (1)
15492 {
15493 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
15494 break;
15495 if (c_parser_next_token_is (parser, CPP_EOF))
15496 break;
15497
15498 loc = c_parser_peek_token (parser)->location;
15499 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_OMP_SECTION)
15500 {
15501 c_parser_consume_pragma (parser);
15502 c_parser_skip_to_pragma_eol (parser);
15503 error_suppress = false;
15504 }
15505 else if (!error_suppress)
15506 {
15507 error_at (loc, "expected %<#pragma omp section%> or %<}%>");
15508 error_suppress = true;
15509 }
15510
15511 substmt = c_parser_omp_structured_block (parser, NULL);
15512 substmt = build1 (OMP_SECTION, void_type_node, substmt);
15513 SET_EXPR_LOCATION (substmt, loc);
15514 add_stmt (substmt);
15515 }
15516 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE,
15517 "expected %<#pragma omp section%> or %<}%>");
15518
15519 substmt = pop_stmt_list (stmt);
15520
15521 stmt = make_node (OMP_SECTIONS);
15522 SET_EXPR_LOCATION (stmt, sections_loc);
15523 TREE_TYPE (stmt) = void_type_node;
15524 OMP_SECTIONS_BODY (stmt) = substmt;
15525
15526 return add_stmt (stmt);
15527 }
15528
15529 /* OpenMP 2.5:
15530 # pragma omp sections sections-clause[optseq] newline
15531 sections-scope
15532
15533 LOC is the location of the #pragma token.
15534 */
15535
15536 #define OMP_SECTIONS_CLAUSE_MASK \
15537 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
15538 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
15539 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
15540 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
15541 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
15542
15543 static tree
15544 c_parser_omp_sections (location_t loc, c_parser *parser,
15545 char *p_name, omp_clause_mask mask, tree *cclauses)
15546 {
15547 tree block, clauses, ret;
15548
15549 strcat (p_name, " sections");
15550 mask |= OMP_SECTIONS_CLAUSE_MASK;
15551 if (cclauses)
15552 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
15553
15554 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
15555 if (cclauses)
15556 {
15557 omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
15558 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
15559 }
15560
15561 block = c_begin_compound_stmt (true);
15562 ret = c_parser_omp_sections_scope (loc, parser);
15563 if (ret)
15564 OMP_SECTIONS_CLAUSES (ret) = clauses;
15565 block = c_end_compound_stmt (loc, block, true);
15566 add_stmt (block);
15567
15568 return ret;
15569 }
15570
15571 /* OpenMP 2.5:
15572 # pragma omp parallel parallel-clause[optseq] new-line
15573 structured-block
15574 # pragma omp parallel for parallel-for-clause[optseq] new-line
15575 structured-block
15576 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
15577 structured-block
15578
15579 OpenMP 4.0:
15580 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
15581 structured-block
15582
15583 LOC is the location of the #pragma token.
15584 */
15585
15586 #define OMP_PARALLEL_CLAUSE_MASK \
15587 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
15588 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
15589 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
15590 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
15591 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
15592 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
15593 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
15594 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
15595 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
15596
15597 static tree
15598 c_parser_omp_parallel (location_t loc, c_parser *parser,
15599 char *p_name, omp_clause_mask mask, tree *cclauses,
15600 bool *if_p)
15601 {
15602 tree stmt, clauses, block;
15603
15604 strcat (p_name, " parallel");
15605 mask |= OMP_PARALLEL_CLAUSE_MASK;
15606 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
15607 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
15608 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
15609 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
15610
15611 if (c_parser_next_token_is_keyword (parser, RID_FOR))
15612 {
15613 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
15614 if (cclauses == NULL)
15615 cclauses = cclauses_buf;
15616
15617 c_parser_consume_token (parser);
15618 if (!flag_openmp) /* flag_openmp_simd */
15619 return c_parser_omp_for (loc, parser, p_name, mask, cclauses, if_p);
15620 block = c_begin_omp_parallel ();
15621 tree ret = c_parser_omp_for (loc, parser, p_name, mask, cclauses, if_p);
15622 stmt
15623 = c_finish_omp_parallel (loc, cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
15624 block);
15625 if (ret == NULL_TREE)
15626 return ret;
15627 OMP_PARALLEL_COMBINED (stmt) = 1;
15628 return stmt;
15629 }
15630 /* When combined with distribute, parallel has to be followed by for.
15631 #pragma omp target parallel is allowed though. */
15632 else if (cclauses
15633 && (mask & (OMP_CLAUSE_MASK_1
15634 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
15635 {
15636 error_at (loc, "expected %<for%> after %qs", p_name);
15637 c_parser_skip_to_pragma_eol (parser);
15638 return NULL_TREE;
15639 }
15640 else if (!flag_openmp) /* flag_openmp_simd */
15641 {
15642 c_parser_skip_to_pragma_eol (parser, false);
15643 return NULL_TREE;
15644 }
15645 else if (cclauses == NULL && c_parser_next_token_is (parser, CPP_NAME))
15646 {
15647 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
15648 if (strcmp (p, "sections") == 0)
15649 {
15650 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
15651 if (cclauses == NULL)
15652 cclauses = cclauses_buf;
15653
15654 c_parser_consume_token (parser);
15655 block = c_begin_omp_parallel ();
15656 c_parser_omp_sections (loc, parser, p_name, mask, cclauses);
15657 stmt = c_finish_omp_parallel (loc,
15658 cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
15659 block);
15660 OMP_PARALLEL_COMBINED (stmt) = 1;
15661 return stmt;
15662 }
15663 }
15664
15665 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
15666 if (cclauses)
15667 {
15668 omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
15669 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
15670 }
15671
15672 block = c_begin_omp_parallel ();
15673 c_parser_statement (parser, if_p);
15674 stmt = c_finish_omp_parallel (loc, clauses, block);
15675
15676 return stmt;
15677 }
15678
15679 /* OpenMP 2.5:
15680 # pragma omp single single-clause[optseq] new-line
15681 structured-block
15682
15683 LOC is the location of the #pragma.
15684 */
15685
15686 #define OMP_SINGLE_CLAUSE_MASK \
15687 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
15688 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
15689 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
15690 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
15691
15692 static tree
15693 c_parser_omp_single (location_t loc, c_parser *parser, bool *if_p)
15694 {
15695 tree stmt = make_node (OMP_SINGLE);
15696 SET_EXPR_LOCATION (stmt, loc);
15697 TREE_TYPE (stmt) = void_type_node;
15698
15699 OMP_SINGLE_CLAUSES (stmt)
15700 = c_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
15701 "#pragma omp single");
15702 OMP_SINGLE_BODY (stmt) = c_parser_omp_structured_block (parser, if_p);
15703
15704 return add_stmt (stmt);
15705 }
15706
15707 /* OpenMP 3.0:
15708 # pragma omp task task-clause[optseq] new-line
15709
15710 LOC is the location of the #pragma.
15711 */
15712
15713 #define OMP_TASK_CLAUSE_MASK \
15714 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
15715 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
15716 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
15717 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
15718 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
15719 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
15720 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
15721 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
15722 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
15723 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
15724
15725 static tree
15726 c_parser_omp_task (location_t loc, c_parser *parser, bool *if_p)
15727 {
15728 tree clauses, block;
15729
15730 clauses = c_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
15731 "#pragma omp task");
15732
15733 block = c_begin_omp_task ();
15734 c_parser_statement (parser, if_p);
15735 return c_finish_omp_task (loc, clauses, block);
15736 }
15737
15738 /* OpenMP 3.0:
15739 # pragma omp taskwait new-line
15740 */
15741
15742 static void
15743 c_parser_omp_taskwait (c_parser *parser)
15744 {
15745 location_t loc = c_parser_peek_token (parser)->location;
15746 c_parser_consume_pragma (parser);
15747 c_parser_skip_to_pragma_eol (parser);
15748
15749 c_finish_omp_taskwait (loc);
15750 }
15751
15752 /* OpenMP 3.1:
15753 # pragma omp taskyield new-line
15754 */
15755
15756 static void
15757 c_parser_omp_taskyield (c_parser *parser)
15758 {
15759 location_t loc = c_parser_peek_token (parser)->location;
15760 c_parser_consume_pragma (parser);
15761 c_parser_skip_to_pragma_eol (parser);
15762
15763 c_finish_omp_taskyield (loc);
15764 }
15765
15766 /* OpenMP 4.0:
15767 # pragma omp taskgroup new-line
15768 */
15769
15770 static tree
15771 c_parser_omp_taskgroup (c_parser *parser, bool *if_p)
15772 {
15773 location_t loc = c_parser_peek_token (parser)->location;
15774 c_parser_skip_to_pragma_eol (parser);
15775 return c_finish_omp_taskgroup (loc, c_parser_omp_structured_block (parser,
15776 if_p));
15777 }
15778
15779 /* OpenMP 4.0:
15780 # pragma omp cancel cancel-clause[optseq] new-line
15781
15782 LOC is the location of the #pragma.
15783 */
15784
15785 #define OMP_CANCEL_CLAUSE_MASK \
15786 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
15787 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
15788 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
15789 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
15790 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
15791
15792 static void
15793 c_parser_omp_cancel (c_parser *parser)
15794 {
15795 location_t loc = c_parser_peek_token (parser)->location;
15796
15797 c_parser_consume_pragma (parser);
15798 tree clauses = c_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
15799 "#pragma omp cancel");
15800
15801 c_finish_omp_cancel (loc, clauses);
15802 }
15803
15804 /* OpenMP 4.0:
15805 # pragma omp cancellation point cancelpt-clause[optseq] new-line
15806
15807 LOC is the location of the #pragma.
15808 */
15809
15810 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
15811 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
15812 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
15813 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
15814 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
15815
15816 static void
15817 c_parser_omp_cancellation_point (c_parser *parser, enum pragma_context context)
15818 {
15819 location_t loc = c_parser_peek_token (parser)->location;
15820 tree clauses;
15821 bool point_seen = false;
15822
15823 c_parser_consume_pragma (parser);
15824 if (c_parser_next_token_is (parser, CPP_NAME))
15825 {
15826 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
15827 if (strcmp (p, "point") == 0)
15828 {
15829 c_parser_consume_token (parser);
15830 point_seen = true;
15831 }
15832 }
15833 if (!point_seen)
15834 {
15835 c_parser_error (parser, "expected %<point%>");
15836 c_parser_skip_to_pragma_eol (parser);
15837 return;
15838 }
15839
15840 if (context != pragma_compound)
15841 {
15842 if (context == pragma_stmt)
15843 error_at (loc, "%<#pragma omp cancellation point%> may only be used in"
15844 " compound statements");
15845 else
15846 c_parser_error (parser, "expected declaration specifiers");
15847 c_parser_skip_to_pragma_eol (parser, false);
15848 return;
15849 }
15850
15851 clauses
15852 = c_parser_omp_all_clauses (parser, OMP_CANCELLATION_POINT_CLAUSE_MASK,
15853 "#pragma omp cancellation point");
15854
15855 c_finish_omp_cancellation_point (loc, clauses);
15856 }
15857
15858 /* OpenMP 4.0:
15859 #pragma omp distribute distribute-clause[optseq] new-line
15860 for-loop */
15861
15862 #define OMP_DISTRIBUTE_CLAUSE_MASK \
15863 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
15864 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
15865 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
15866 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
15867 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
15868
15869 static tree
15870 c_parser_omp_distribute (location_t loc, c_parser *parser,
15871 char *p_name, omp_clause_mask mask, tree *cclauses,
15872 bool *if_p)
15873 {
15874 tree clauses, block, ret;
15875
15876 strcat (p_name, " distribute");
15877 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
15878
15879 if (c_parser_next_token_is (parser, CPP_NAME))
15880 {
15881 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
15882 bool simd = false;
15883 bool parallel = false;
15884
15885 if (strcmp (p, "simd") == 0)
15886 simd = true;
15887 else
15888 parallel = strcmp (p, "parallel") == 0;
15889 if (parallel || simd)
15890 {
15891 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
15892 if (cclauses == NULL)
15893 cclauses = cclauses_buf;
15894 c_parser_consume_token (parser);
15895 if (!flag_openmp) /* flag_openmp_simd */
15896 {
15897 if (simd)
15898 return c_parser_omp_simd (loc, parser, p_name, mask, cclauses,
15899 if_p);
15900 else
15901 return c_parser_omp_parallel (loc, parser, p_name, mask,
15902 cclauses, if_p);
15903 }
15904 block = c_begin_compound_stmt (true);
15905 if (simd)
15906 ret = c_parser_omp_simd (loc, parser, p_name, mask, cclauses,
15907 if_p);
15908 else
15909 ret = c_parser_omp_parallel (loc, parser, p_name, mask, cclauses,
15910 if_p);
15911 block = c_end_compound_stmt (loc, block, true);
15912 if (ret == NULL)
15913 return ret;
15914 ret = make_node (OMP_DISTRIBUTE);
15915 TREE_TYPE (ret) = void_type_node;
15916 OMP_FOR_BODY (ret) = block;
15917 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
15918 SET_EXPR_LOCATION (ret, loc);
15919 add_stmt (ret);
15920 return ret;
15921 }
15922 }
15923 if (!flag_openmp) /* flag_openmp_simd */
15924 {
15925 c_parser_skip_to_pragma_eol (parser, false);
15926 return NULL_TREE;
15927 }
15928
15929 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
15930 if (cclauses)
15931 {
15932 omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
15933 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
15934 }
15935
15936 block = c_begin_compound_stmt (true);
15937 ret = c_parser_omp_for_loop (loc, parser, OMP_DISTRIBUTE, clauses, NULL,
15938 if_p);
15939 block = c_end_compound_stmt (loc, block, true);
15940 add_stmt (block);
15941
15942 return ret;
15943 }
15944
15945 /* OpenMP 4.0:
15946 # pragma omp teams teams-clause[optseq] new-line
15947 structured-block */
15948
15949 #define OMP_TEAMS_CLAUSE_MASK \
15950 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
15951 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
15952 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
15953 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
15954 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
15955 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
15956 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
15957
15958 static tree
15959 c_parser_omp_teams (location_t loc, c_parser *parser,
15960 char *p_name, omp_clause_mask mask, tree *cclauses,
15961 bool *if_p)
15962 {
15963 tree clauses, block, ret;
15964
15965 strcat (p_name, " teams");
15966 mask |= OMP_TEAMS_CLAUSE_MASK;
15967
15968 if (c_parser_next_token_is (parser, CPP_NAME))
15969 {
15970 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
15971 if (strcmp (p, "distribute") == 0)
15972 {
15973 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
15974 if (cclauses == NULL)
15975 cclauses = cclauses_buf;
15976
15977 c_parser_consume_token (parser);
15978 if (!flag_openmp) /* flag_openmp_simd */
15979 return c_parser_omp_distribute (loc, parser, p_name, mask,
15980 cclauses, if_p);
15981 block = c_begin_compound_stmt (true);
15982 ret = c_parser_omp_distribute (loc, parser, p_name, mask, cclauses,
15983 if_p);
15984 block = c_end_compound_stmt (loc, block, true);
15985 if (ret == NULL)
15986 return ret;
15987 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
15988 ret = make_node (OMP_TEAMS);
15989 TREE_TYPE (ret) = void_type_node;
15990 OMP_TEAMS_CLAUSES (ret) = clauses;
15991 OMP_TEAMS_BODY (ret) = block;
15992 OMP_TEAMS_COMBINED (ret) = 1;
15993 return add_stmt (ret);
15994 }
15995 }
15996 if (!flag_openmp) /* flag_openmp_simd */
15997 {
15998 c_parser_skip_to_pragma_eol (parser, false);
15999 return NULL_TREE;
16000 }
16001
16002 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
16003 if (cclauses)
16004 {
16005 omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
16006 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
16007 }
16008
16009 tree stmt = make_node (OMP_TEAMS);
16010 TREE_TYPE (stmt) = void_type_node;
16011 OMP_TEAMS_CLAUSES (stmt) = clauses;
16012 OMP_TEAMS_BODY (stmt) = c_parser_omp_structured_block (parser, if_p);
16013
16014 return add_stmt (stmt);
16015 }
16016
16017 /* OpenMP 4.0:
16018 # pragma omp target data target-data-clause[optseq] new-line
16019 structured-block */
16020
16021 #define OMP_TARGET_DATA_CLAUSE_MASK \
16022 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
16023 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
16024 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
16025 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
16026
16027 static tree
16028 c_parser_omp_target_data (location_t loc, c_parser *parser, bool *if_p)
16029 {
16030 tree clauses
16031 = c_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
16032 "#pragma omp target data");
16033 int map_seen = 0;
16034 for (tree *pc = &clauses; *pc;)
16035 {
16036 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
16037 switch (OMP_CLAUSE_MAP_KIND (*pc))
16038 {
16039 case GOMP_MAP_TO:
16040 case GOMP_MAP_ALWAYS_TO:
16041 case GOMP_MAP_FROM:
16042 case GOMP_MAP_ALWAYS_FROM:
16043 case GOMP_MAP_TOFROM:
16044 case GOMP_MAP_ALWAYS_TOFROM:
16045 case GOMP_MAP_ALLOC:
16046 map_seen = 3;
16047 break;
16048 case GOMP_MAP_FIRSTPRIVATE_POINTER:
16049 case GOMP_MAP_ALWAYS_POINTER:
16050 break;
16051 default:
16052 map_seen |= 1;
16053 error_at (OMP_CLAUSE_LOCATION (*pc),
16054 "%<#pragma omp target data%> with map-type other "
16055 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
16056 "on %<map%> clause");
16057 *pc = OMP_CLAUSE_CHAIN (*pc);
16058 continue;
16059 }
16060 pc = &OMP_CLAUSE_CHAIN (*pc);
16061 }
16062
16063 if (map_seen != 3)
16064 {
16065 if (map_seen == 0)
16066 error_at (loc,
16067 "%<#pragma omp target data%> must contain at least "
16068 "one %<map%> clause");
16069 return NULL_TREE;
16070 }
16071
16072 tree stmt = make_node (OMP_TARGET_DATA);
16073 TREE_TYPE (stmt) = void_type_node;
16074 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
16075 keep_next_level ();
16076 tree block = c_begin_compound_stmt (true);
16077 add_stmt (c_parser_omp_structured_block (parser, if_p));
16078 OMP_TARGET_DATA_BODY (stmt) = c_end_compound_stmt (loc, block, true);
16079
16080 SET_EXPR_LOCATION (stmt, loc);
16081 return add_stmt (stmt);
16082 }
16083
16084 /* OpenMP 4.0:
16085 # pragma omp target update target-update-clause[optseq] new-line */
16086
16087 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
16088 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
16089 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
16090 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
16091 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
16092 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
16093 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
16094
16095 static bool
16096 c_parser_omp_target_update (location_t loc, c_parser *parser,
16097 enum pragma_context context)
16098 {
16099 if (context == pragma_stmt)
16100 {
16101 error_at (loc,
16102 "%<#pragma omp target update%> may only be "
16103 "used in compound statements");
16104 c_parser_skip_to_pragma_eol (parser, false);
16105 return false;
16106 }
16107
16108 tree clauses
16109 = c_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
16110 "#pragma omp target update");
16111 if (omp_find_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
16112 && omp_find_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
16113 {
16114 error_at (loc,
16115 "%<#pragma omp target update%> must contain at least one "
16116 "%<from%> or %<to%> clauses");
16117 return false;
16118 }
16119
16120 tree stmt = make_node (OMP_TARGET_UPDATE);
16121 TREE_TYPE (stmt) = void_type_node;
16122 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
16123 SET_EXPR_LOCATION (stmt, loc);
16124 add_stmt (stmt);
16125 return false;
16126 }
16127
16128 /* OpenMP 4.5:
16129 # pragma omp target enter data target-data-clause[optseq] new-line */
16130
16131 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
16132 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
16133 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
16134 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
16135 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
16136 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
16137
16138 static tree
16139 c_parser_omp_target_enter_data (location_t loc, c_parser *parser,
16140 enum pragma_context context)
16141 {
16142 bool data_seen = false;
16143 if (c_parser_next_token_is (parser, CPP_NAME))
16144 {
16145 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16146 if (strcmp (p, "data") == 0)
16147 {
16148 c_parser_consume_token (parser);
16149 data_seen = true;
16150 }
16151 }
16152 if (!data_seen)
16153 {
16154 c_parser_error (parser, "expected %<data%>");
16155 c_parser_skip_to_pragma_eol (parser);
16156 return NULL_TREE;
16157 }
16158
16159 if (context == pragma_stmt)
16160 {
16161 error_at (loc,
16162 "%<#pragma omp target enter data%> may only be "
16163 "used in compound statements");
16164 c_parser_skip_to_pragma_eol (parser, false);
16165 return NULL_TREE;
16166 }
16167
16168 tree clauses
16169 = c_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
16170 "#pragma omp target enter data");
16171 int map_seen = 0;
16172 for (tree *pc = &clauses; *pc;)
16173 {
16174 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
16175 switch (OMP_CLAUSE_MAP_KIND (*pc))
16176 {
16177 case GOMP_MAP_TO:
16178 case GOMP_MAP_ALWAYS_TO:
16179 case GOMP_MAP_ALLOC:
16180 map_seen = 3;
16181 break;
16182 case GOMP_MAP_FIRSTPRIVATE_POINTER:
16183 case GOMP_MAP_ALWAYS_POINTER:
16184 break;
16185 default:
16186 map_seen |= 1;
16187 error_at (OMP_CLAUSE_LOCATION (*pc),
16188 "%<#pragma omp target enter data%> with map-type other "
16189 "than %<to%> or %<alloc%> on %<map%> clause");
16190 *pc = OMP_CLAUSE_CHAIN (*pc);
16191 continue;
16192 }
16193 pc = &OMP_CLAUSE_CHAIN (*pc);
16194 }
16195
16196 if (map_seen != 3)
16197 {
16198 if (map_seen == 0)
16199 error_at (loc,
16200 "%<#pragma omp target enter data%> must contain at least "
16201 "one %<map%> clause");
16202 return NULL_TREE;
16203 }
16204
16205 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
16206 TREE_TYPE (stmt) = void_type_node;
16207 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
16208 SET_EXPR_LOCATION (stmt, loc);
16209 add_stmt (stmt);
16210 return stmt;
16211 }
16212
16213 /* OpenMP 4.5:
16214 # pragma omp target exit data target-data-clause[optseq] new-line */
16215
16216 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
16217 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
16218 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
16219 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
16220 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
16221 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
16222
16223 static tree
16224 c_parser_omp_target_exit_data (location_t loc, c_parser *parser,
16225 enum pragma_context context)
16226 {
16227 bool data_seen = false;
16228 if (c_parser_next_token_is (parser, CPP_NAME))
16229 {
16230 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16231 if (strcmp (p, "data") == 0)
16232 {
16233 c_parser_consume_token (parser);
16234 data_seen = true;
16235 }
16236 }
16237 if (!data_seen)
16238 {
16239 c_parser_error (parser, "expected %<data%>");
16240 c_parser_skip_to_pragma_eol (parser);
16241 return NULL_TREE;
16242 }
16243
16244 if (context == pragma_stmt)
16245 {
16246 error_at (loc,
16247 "%<#pragma omp target exit data%> may only be "
16248 "used in compound statements");
16249 c_parser_skip_to_pragma_eol (parser, false);
16250 return NULL_TREE;
16251 }
16252
16253 tree clauses
16254 = c_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
16255 "#pragma omp target exit data");
16256
16257 int map_seen = 0;
16258 for (tree *pc = &clauses; *pc;)
16259 {
16260 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
16261 switch (OMP_CLAUSE_MAP_KIND (*pc))
16262 {
16263 case GOMP_MAP_FROM:
16264 case GOMP_MAP_ALWAYS_FROM:
16265 case GOMP_MAP_RELEASE:
16266 case GOMP_MAP_DELETE:
16267 map_seen = 3;
16268 break;
16269 case GOMP_MAP_FIRSTPRIVATE_POINTER:
16270 case GOMP_MAP_ALWAYS_POINTER:
16271 break;
16272 default:
16273 map_seen |= 1;
16274 error_at (OMP_CLAUSE_LOCATION (*pc),
16275 "%<#pragma omp target exit data%> with map-type other "
16276 "than %<from%>, %<release> or %<delete%> on %<map%>"
16277 " clause");
16278 *pc = OMP_CLAUSE_CHAIN (*pc);
16279 continue;
16280 }
16281 pc = &OMP_CLAUSE_CHAIN (*pc);
16282 }
16283
16284 if (map_seen != 3)
16285 {
16286 if (map_seen == 0)
16287 error_at (loc,
16288 "%<#pragma omp target exit data%> must contain at least one "
16289 "%<map%> clause");
16290 return NULL_TREE;
16291 }
16292
16293 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
16294 TREE_TYPE (stmt) = void_type_node;
16295 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
16296 SET_EXPR_LOCATION (stmt, loc);
16297 add_stmt (stmt);
16298 return stmt;
16299 }
16300
16301 /* OpenMP 4.0:
16302 # pragma omp target target-clause[optseq] new-line
16303 structured-block */
16304
16305 #define OMP_TARGET_CLAUSE_MASK \
16306 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
16307 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
16308 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
16309 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
16310 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
16311 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
16312 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
16313 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
16314 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
16315
16316 static bool
16317 c_parser_omp_target (c_parser *parser, enum pragma_context context, bool *if_p)
16318 {
16319 location_t loc = c_parser_peek_token (parser)->location;
16320 c_parser_consume_pragma (parser);
16321 tree *pc = NULL, stmt, block;
16322
16323 if (context != pragma_stmt && context != pragma_compound)
16324 {
16325 c_parser_error (parser, "expected declaration specifiers");
16326 c_parser_skip_to_pragma_eol (parser);
16327 return false;
16328 }
16329
16330 if (c_parser_next_token_is (parser, CPP_NAME))
16331 {
16332 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16333 enum tree_code ccode = ERROR_MARK;
16334
16335 if (strcmp (p, "teams") == 0)
16336 ccode = OMP_TEAMS;
16337 else if (strcmp (p, "parallel") == 0)
16338 ccode = OMP_PARALLEL;
16339 else if (strcmp (p, "simd") == 0)
16340 ccode = OMP_SIMD;
16341 if (ccode != ERROR_MARK)
16342 {
16343 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
16344 char p_name[sizeof ("#pragma omp target teams distribute "
16345 "parallel for simd")];
16346
16347 c_parser_consume_token (parser);
16348 strcpy (p_name, "#pragma omp target");
16349 if (!flag_openmp) /* flag_openmp_simd */
16350 {
16351 tree stmt;
16352 switch (ccode)
16353 {
16354 case OMP_TEAMS:
16355 stmt = c_parser_omp_teams (loc, parser, p_name,
16356 OMP_TARGET_CLAUSE_MASK,
16357 cclauses, if_p);
16358 break;
16359 case OMP_PARALLEL:
16360 stmt = c_parser_omp_parallel (loc, parser, p_name,
16361 OMP_TARGET_CLAUSE_MASK,
16362 cclauses, if_p);
16363 break;
16364 case OMP_SIMD:
16365 stmt = c_parser_omp_simd (loc, parser, p_name,
16366 OMP_TARGET_CLAUSE_MASK,
16367 cclauses, if_p);
16368 break;
16369 default:
16370 gcc_unreachable ();
16371 }
16372 return stmt != NULL_TREE;
16373 }
16374 keep_next_level ();
16375 tree block = c_begin_compound_stmt (true), ret;
16376 switch (ccode)
16377 {
16378 case OMP_TEAMS:
16379 ret = c_parser_omp_teams (loc, parser, p_name,
16380 OMP_TARGET_CLAUSE_MASK, cclauses,
16381 if_p);
16382 break;
16383 case OMP_PARALLEL:
16384 ret = c_parser_omp_parallel (loc, parser, p_name,
16385 OMP_TARGET_CLAUSE_MASK, cclauses,
16386 if_p);
16387 break;
16388 case OMP_SIMD:
16389 ret = c_parser_omp_simd (loc, parser, p_name,
16390 OMP_TARGET_CLAUSE_MASK, cclauses,
16391 if_p);
16392 break;
16393 default:
16394 gcc_unreachable ();
16395 }
16396 block = c_end_compound_stmt (loc, block, true);
16397 if (ret == NULL_TREE)
16398 return false;
16399 if (ccode == OMP_TEAMS)
16400 {
16401 /* For combined target teams, ensure the num_teams and
16402 thread_limit clause expressions are evaluated on the host,
16403 before entering the target construct. */
16404 tree c;
16405 for (c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
16406 c; c = OMP_CLAUSE_CHAIN (c))
16407 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
16408 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
16409 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
16410 {
16411 tree expr = OMP_CLAUSE_OPERAND (c, 0);
16412 tree tmp = create_tmp_var_raw (TREE_TYPE (expr));
16413 expr = build4 (TARGET_EXPR, TREE_TYPE (expr), tmp,
16414 expr, NULL_TREE, NULL_TREE);
16415 add_stmt (expr);
16416 OMP_CLAUSE_OPERAND (c, 0) = expr;
16417 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
16418 OMP_CLAUSE_FIRSTPRIVATE);
16419 OMP_CLAUSE_DECL (tc) = tmp;
16420 OMP_CLAUSE_CHAIN (tc)
16421 = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
16422 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
16423 }
16424 }
16425 tree stmt = make_node (OMP_TARGET);
16426 TREE_TYPE (stmt) = void_type_node;
16427 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
16428 OMP_TARGET_BODY (stmt) = block;
16429 OMP_TARGET_COMBINED (stmt) = 1;
16430 add_stmt (stmt);
16431 pc = &OMP_TARGET_CLAUSES (stmt);
16432 goto check_clauses;
16433 }
16434 else if (!flag_openmp) /* flag_openmp_simd */
16435 {
16436 c_parser_skip_to_pragma_eol (parser, false);
16437 return false;
16438 }
16439 else if (strcmp (p, "data") == 0)
16440 {
16441 c_parser_consume_token (parser);
16442 c_parser_omp_target_data (loc, parser, if_p);
16443 return true;
16444 }
16445 else if (strcmp (p, "enter") == 0)
16446 {
16447 c_parser_consume_token (parser);
16448 c_parser_omp_target_enter_data (loc, parser, context);
16449 return false;
16450 }
16451 else if (strcmp (p, "exit") == 0)
16452 {
16453 c_parser_consume_token (parser);
16454 c_parser_omp_target_exit_data (loc, parser, context);
16455 return false;
16456 }
16457 else if (strcmp (p, "update") == 0)
16458 {
16459 c_parser_consume_token (parser);
16460 return c_parser_omp_target_update (loc, parser, context);
16461 }
16462 }
16463
16464 stmt = make_node (OMP_TARGET);
16465 TREE_TYPE (stmt) = void_type_node;
16466
16467 OMP_TARGET_CLAUSES (stmt)
16468 = c_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
16469 "#pragma omp target");
16470 pc = &OMP_TARGET_CLAUSES (stmt);
16471 keep_next_level ();
16472 block = c_begin_compound_stmt (true);
16473 add_stmt (c_parser_omp_structured_block (parser, if_p));
16474 OMP_TARGET_BODY (stmt) = c_end_compound_stmt (loc, block, true);
16475
16476 SET_EXPR_LOCATION (stmt, loc);
16477 add_stmt (stmt);
16478
16479 check_clauses:
16480 while (*pc)
16481 {
16482 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
16483 switch (OMP_CLAUSE_MAP_KIND (*pc))
16484 {
16485 case GOMP_MAP_TO:
16486 case GOMP_MAP_ALWAYS_TO:
16487 case GOMP_MAP_FROM:
16488 case GOMP_MAP_ALWAYS_FROM:
16489 case GOMP_MAP_TOFROM:
16490 case GOMP_MAP_ALWAYS_TOFROM:
16491 case GOMP_MAP_ALLOC:
16492 case GOMP_MAP_FIRSTPRIVATE_POINTER:
16493 case GOMP_MAP_ALWAYS_POINTER:
16494 break;
16495 default:
16496 error_at (OMP_CLAUSE_LOCATION (*pc),
16497 "%<#pragma omp target%> with map-type other "
16498 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
16499 "on %<map%> clause");
16500 *pc = OMP_CLAUSE_CHAIN (*pc);
16501 continue;
16502 }
16503 pc = &OMP_CLAUSE_CHAIN (*pc);
16504 }
16505 return true;
16506 }
16507
16508 /* OpenMP 4.0:
16509 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
16510
16511 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
16512 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
16513 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
16514 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
16515 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
16516 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
16517 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
16518
16519 static void
16520 c_parser_omp_declare_simd (c_parser *parser, enum pragma_context context)
16521 {
16522 auto_vec<c_token> clauses;
16523 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
16524 {
16525 c_token *token = c_parser_peek_token (parser);
16526 if (token->type == CPP_EOF)
16527 {
16528 c_parser_skip_to_pragma_eol (parser);
16529 return;
16530 }
16531 clauses.safe_push (*token);
16532 c_parser_consume_token (parser);
16533 }
16534 clauses.safe_push (*c_parser_peek_token (parser));
16535 c_parser_skip_to_pragma_eol (parser);
16536
16537 while (c_parser_next_token_is (parser, CPP_PRAGMA))
16538 {
16539 if (c_parser_peek_token (parser)->pragma_kind
16540 != PRAGMA_OMP_DECLARE
16541 || c_parser_peek_2nd_token (parser)->type != CPP_NAME
16542 || strcmp (IDENTIFIER_POINTER
16543 (c_parser_peek_2nd_token (parser)->value),
16544 "simd") != 0)
16545 {
16546 c_parser_error (parser,
16547 "%<#pragma omp declare simd%> must be followed by "
16548 "function declaration or definition or another "
16549 "%<#pragma omp declare simd%>");
16550 return;
16551 }
16552 c_parser_consume_pragma (parser);
16553 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
16554 {
16555 c_token *token = c_parser_peek_token (parser);
16556 if (token->type == CPP_EOF)
16557 {
16558 c_parser_skip_to_pragma_eol (parser);
16559 return;
16560 }
16561 clauses.safe_push (*token);
16562 c_parser_consume_token (parser);
16563 }
16564 clauses.safe_push (*c_parser_peek_token (parser));
16565 c_parser_skip_to_pragma_eol (parser);
16566 }
16567
16568 /* Make sure nothing tries to read past the end of the tokens. */
16569 c_token eof_token;
16570 memset (&eof_token, 0, sizeof (eof_token));
16571 eof_token.type = CPP_EOF;
16572 clauses.safe_push (eof_token);
16573 clauses.safe_push (eof_token);
16574
16575 switch (context)
16576 {
16577 case pragma_external:
16578 if (c_parser_next_token_is (parser, CPP_KEYWORD)
16579 && c_parser_peek_token (parser)->keyword == RID_EXTENSION)
16580 {
16581 int ext = disable_extension_diagnostics ();
16582 do
16583 c_parser_consume_token (parser);
16584 while (c_parser_next_token_is (parser, CPP_KEYWORD)
16585 && c_parser_peek_token (parser)->keyword == RID_EXTENSION);
16586 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
16587 NULL, clauses);
16588 restore_extension_diagnostics (ext);
16589 }
16590 else
16591 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
16592 NULL, clauses);
16593 break;
16594 case pragma_struct:
16595 case pragma_param:
16596 c_parser_error (parser, "%<#pragma omp declare simd%> must be followed by "
16597 "function declaration or definition");
16598 break;
16599 case pragma_compound:
16600 case pragma_stmt:
16601 if (c_parser_next_token_is (parser, CPP_KEYWORD)
16602 && c_parser_peek_token (parser)->keyword == RID_EXTENSION)
16603 {
16604 int ext = disable_extension_diagnostics ();
16605 do
16606 c_parser_consume_token (parser);
16607 while (c_parser_next_token_is (parser, CPP_KEYWORD)
16608 && c_parser_peek_token (parser)->keyword == RID_EXTENSION);
16609 if (c_parser_next_tokens_start_declaration (parser))
16610 {
16611 c_parser_declaration_or_fndef (parser, true, true, true, true,
16612 true, NULL, clauses);
16613 restore_extension_diagnostics (ext);
16614 break;
16615 }
16616 restore_extension_diagnostics (ext);
16617 }
16618 else if (c_parser_next_tokens_start_declaration (parser))
16619 {
16620 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
16621 NULL, clauses);
16622 break;
16623 }
16624 c_parser_error (parser, "%<#pragma omp declare simd%> must be followed by "
16625 "function declaration or definition");
16626 break;
16627 default:
16628 gcc_unreachable ();
16629 }
16630 }
16631
16632 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
16633 and put that into "omp declare simd" attribute. */
16634
16635 static void
16636 c_finish_omp_declare_simd (c_parser *parser, tree fndecl, tree parms,
16637 vec<c_token> clauses)
16638 {
16639 if (flag_cilkplus
16640 && (clauses.exists ()
16641 || lookup_attribute ("simd", DECL_ATTRIBUTES (fndecl)))
16642 && !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
16643 {
16644 error ("%<#pragma omp declare simd%> or %<simd%> attribute cannot be "
16645 "used in the same function marked as a Cilk Plus SIMD-enabled "
16646 "function");
16647 vec_free (parser->cilk_simd_fn_tokens);
16648 return;
16649 }
16650
16651 /* Normally first token is CPP_NAME "simd". CPP_EOF there indicates
16652 error has been reported and CPP_PRAGMA that c_finish_omp_declare_simd
16653 has already processed the tokens. */
16654 if (clauses.exists () && clauses[0].type == CPP_EOF)
16655 return;
16656 if (fndecl == NULL_TREE || TREE_CODE (fndecl) != FUNCTION_DECL)
16657 {
16658 error ("%<#pragma omp declare simd%> not immediately followed by "
16659 "a function declaration or definition");
16660 clauses[0].type = CPP_EOF;
16661 return;
16662 }
16663 if (clauses.exists () && clauses[0].type != CPP_NAME)
16664 {
16665 error_at (DECL_SOURCE_LOCATION (fndecl),
16666 "%<#pragma omp declare simd%> not immediately followed by "
16667 "a single function declaration or definition");
16668 clauses[0].type = CPP_EOF;
16669 return;
16670 }
16671
16672 if (parms == NULL_TREE)
16673 parms = DECL_ARGUMENTS (fndecl);
16674
16675 unsigned int tokens_avail = parser->tokens_avail;
16676 gcc_assert (parser->tokens == &parser->tokens_buf[0]);
16677 bool is_cilkplus_cilk_simd_fn = false;
16678
16679 if (flag_cilkplus && !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
16680 {
16681 parser->tokens = parser->cilk_simd_fn_tokens->address ();
16682 parser->tokens_avail = vec_safe_length (parser->cilk_simd_fn_tokens);
16683 is_cilkplus_cilk_simd_fn = true;
16684
16685 if (lookup_attribute ("simd", DECL_ATTRIBUTES (fndecl)) != NULL)
16686 {
16687 error_at (DECL_SOURCE_LOCATION (fndecl),
16688 "%<__simd__%> attribute cannot be used in the same "
16689 "function marked as a Cilk Plus SIMD-enabled function");
16690 vec_free (parser->cilk_simd_fn_tokens);
16691 return;
16692 }
16693
16694 }
16695 else
16696 {
16697 parser->tokens = clauses.address ();
16698 parser->tokens_avail = clauses.length ();
16699 }
16700
16701 /* c_parser_omp_declare_simd pushed 2 extra CPP_EOF tokens at the end. */
16702 while (parser->tokens_avail > 3)
16703 {
16704 c_token *token = c_parser_peek_token (parser);
16705 if (!is_cilkplus_cilk_simd_fn)
16706 gcc_assert (token->type == CPP_NAME
16707 && strcmp (IDENTIFIER_POINTER (token->value), "simd") == 0);
16708 else
16709 gcc_assert (token->type == CPP_NAME
16710 && is_cilkplus_vector_p (token->value));
16711 c_parser_consume_token (parser);
16712 parser->in_pragma = true;
16713
16714 tree c = NULL_TREE;
16715 if (is_cilkplus_cilk_simd_fn)
16716 c = c_parser_omp_all_clauses (parser, CILK_SIMD_FN_CLAUSE_MASK,
16717 "SIMD-enabled functions attribute");
16718 else
16719 c = c_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
16720 "#pragma omp declare simd");
16721 c = c_omp_declare_simd_clauses_to_numbers (parms, c);
16722 if (c != NULL_TREE)
16723 c = tree_cons (NULL_TREE, c, NULL_TREE);
16724 if (is_cilkplus_cilk_simd_fn)
16725 {
16726 tree k = build_tree_list (get_identifier ("cilk simd function"),
16727 NULL_TREE);
16728 TREE_CHAIN (k) = DECL_ATTRIBUTES (fndecl);
16729 DECL_ATTRIBUTES (fndecl) = k;
16730 }
16731 c = build_tree_list (get_identifier ("omp declare simd"), c);
16732 TREE_CHAIN (c) = DECL_ATTRIBUTES (fndecl);
16733 DECL_ATTRIBUTES (fndecl) = c;
16734 }
16735
16736 parser->tokens = &parser->tokens_buf[0];
16737 parser->tokens_avail = tokens_avail;
16738 if (clauses.exists ())
16739 clauses[0].type = CPP_PRAGMA;
16740
16741 if (!vec_safe_is_empty (parser->cilk_simd_fn_tokens))
16742 vec_free (parser->cilk_simd_fn_tokens);
16743 }
16744
16745
16746 /* OpenMP 4.0:
16747 # pragma omp declare target new-line
16748 declarations and definitions
16749 # pragma omp end declare target new-line
16750
16751 OpenMP 4.5:
16752 # pragma omp declare target ( extended-list ) new-line
16753
16754 # pragma omp declare target declare-target-clauses[seq] new-line */
16755
16756 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
16757 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
16758 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
16759
16760 static void
16761 c_parser_omp_declare_target (c_parser *parser)
16762 {
16763 location_t loc = c_parser_peek_token (parser)->location;
16764 tree clauses = NULL_TREE;
16765 if (c_parser_next_token_is (parser, CPP_NAME))
16766 clauses = c_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
16767 "#pragma omp declare target");
16768 else if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
16769 {
16770 clauses = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_TO_DECLARE,
16771 clauses);
16772 clauses = c_finish_omp_clauses (clauses, C_ORT_OMP);
16773 c_parser_skip_to_pragma_eol (parser);
16774 }
16775 else
16776 {
16777 c_parser_skip_to_pragma_eol (parser);
16778 current_omp_declare_target_attribute++;
16779 return;
16780 }
16781 if (current_omp_declare_target_attribute)
16782 error_at (loc, "%<#pragma omp declare target%> with clauses in between "
16783 "%<#pragma omp declare target%> without clauses and "
16784 "%<#pragma omp end declare target%>");
16785 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
16786 {
16787 tree t = OMP_CLAUSE_DECL (c), id;
16788 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
16789 tree at2 = lookup_attribute ("omp declare target link",
16790 DECL_ATTRIBUTES (t));
16791 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
16792 {
16793 id = get_identifier ("omp declare target link");
16794 std::swap (at1, at2);
16795 }
16796 else
16797 id = get_identifier ("omp declare target");
16798 if (at2)
16799 {
16800 error_at (OMP_CLAUSE_LOCATION (c),
16801 "%qD specified both in declare target %<link%> and %<to%>"
16802 " clauses", t);
16803 continue;
16804 }
16805 if (!at1)
16806 {
16807 symtab_node *node = symtab_node::get (t);
16808 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
16809 if (node != NULL)
16810 {
16811 node->offloadable = 1;
16812 if (ENABLE_OFFLOADING)
16813 {
16814 g->have_offload = true;
16815 if (is_a <varpool_node *> (node))
16816 vec_safe_push (offload_vars, t);
16817 }
16818 }
16819 }
16820 }
16821 }
16822
16823 static void
16824 c_parser_omp_end_declare_target (c_parser *parser)
16825 {
16826 location_t loc = c_parser_peek_token (parser)->location;
16827 c_parser_consume_pragma (parser);
16828 if (c_parser_next_token_is (parser, CPP_NAME)
16829 && strcmp (IDENTIFIER_POINTER (c_parser_peek_token (parser)->value),
16830 "declare") == 0)
16831 {
16832 c_parser_consume_token (parser);
16833 if (c_parser_next_token_is (parser, CPP_NAME)
16834 && strcmp (IDENTIFIER_POINTER (c_parser_peek_token (parser)->value),
16835 "target") == 0)
16836 c_parser_consume_token (parser);
16837 else
16838 {
16839 c_parser_error (parser, "expected %<target%>");
16840 c_parser_skip_to_pragma_eol (parser);
16841 return;
16842 }
16843 }
16844 else
16845 {
16846 c_parser_error (parser, "expected %<declare%>");
16847 c_parser_skip_to_pragma_eol (parser);
16848 return;
16849 }
16850 c_parser_skip_to_pragma_eol (parser);
16851 if (!current_omp_declare_target_attribute)
16852 error_at (loc, "%<#pragma omp end declare target%> without corresponding "
16853 "%<#pragma omp declare target%>");
16854 else
16855 current_omp_declare_target_attribute--;
16856 }
16857
16858
16859 /* OpenMP 4.0
16860 #pragma omp declare reduction (reduction-id : typename-list : expression) \
16861 initializer-clause[opt] new-line
16862
16863 initializer-clause:
16864 initializer (omp_priv = initializer)
16865 initializer (function-name (argument-list)) */
16866
16867 static void
16868 c_parser_omp_declare_reduction (c_parser *parser, enum pragma_context context)
16869 {
16870 unsigned int tokens_avail = 0, i;
16871 vec<tree> types = vNULL;
16872 vec<c_token> clauses = vNULL;
16873 enum tree_code reduc_code = ERROR_MARK;
16874 tree reduc_id = NULL_TREE;
16875 tree type;
16876 location_t rloc = c_parser_peek_token (parser)->location;
16877
16878 if (context == pragma_struct || context == pragma_param)
16879 {
16880 error ("%<#pragma omp declare reduction%> not at file or block scope");
16881 goto fail;
16882 }
16883
16884 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
16885 goto fail;
16886
16887 switch (c_parser_peek_token (parser)->type)
16888 {
16889 case CPP_PLUS:
16890 reduc_code = PLUS_EXPR;
16891 break;
16892 case CPP_MULT:
16893 reduc_code = MULT_EXPR;
16894 break;
16895 case CPP_MINUS:
16896 reduc_code = MINUS_EXPR;
16897 break;
16898 case CPP_AND:
16899 reduc_code = BIT_AND_EXPR;
16900 break;
16901 case CPP_XOR:
16902 reduc_code = BIT_XOR_EXPR;
16903 break;
16904 case CPP_OR:
16905 reduc_code = BIT_IOR_EXPR;
16906 break;
16907 case CPP_AND_AND:
16908 reduc_code = TRUTH_ANDIF_EXPR;
16909 break;
16910 case CPP_OR_OR:
16911 reduc_code = TRUTH_ORIF_EXPR;
16912 break;
16913 case CPP_NAME:
16914 const char *p;
16915 p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16916 if (strcmp (p, "min") == 0)
16917 {
16918 reduc_code = MIN_EXPR;
16919 break;
16920 }
16921 if (strcmp (p, "max") == 0)
16922 {
16923 reduc_code = MAX_EXPR;
16924 break;
16925 }
16926 reduc_id = c_parser_peek_token (parser)->value;
16927 break;
16928 default:
16929 c_parser_error (parser,
16930 "expected %<+%>, %<*%>, %<-%>, %<&%>, "
16931 "%<^%>, %<|%>, %<&&%>, %<||%>, %<min%> or identifier");
16932 goto fail;
16933 }
16934
16935 tree orig_reduc_id, reduc_decl;
16936 orig_reduc_id = reduc_id;
16937 reduc_id = c_omp_reduction_id (reduc_code, reduc_id);
16938 reduc_decl = c_omp_reduction_decl (reduc_id);
16939 c_parser_consume_token (parser);
16940
16941 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
16942 goto fail;
16943
16944 while (true)
16945 {
16946 location_t loc = c_parser_peek_token (parser)->location;
16947 struct c_type_name *ctype = c_parser_type_name (parser);
16948 if (ctype != NULL)
16949 {
16950 type = groktypename (ctype, NULL, NULL);
16951 if (type == error_mark_node)
16952 ;
16953 else if ((INTEGRAL_TYPE_P (type)
16954 || TREE_CODE (type) == REAL_TYPE
16955 || TREE_CODE (type) == COMPLEX_TYPE)
16956 && orig_reduc_id == NULL_TREE)
16957 error_at (loc, "predeclared arithmetic type in "
16958 "%<#pragma omp declare reduction%>");
16959 else if (TREE_CODE (type) == FUNCTION_TYPE
16960 || TREE_CODE (type) == ARRAY_TYPE)
16961 error_at (loc, "function or array type in "
16962 "%<#pragma omp declare reduction%>");
16963 else if (TYPE_ATOMIC (type))
16964 error_at (loc, "%<_Atomic%> qualified type in "
16965 "%<#pragma omp declare reduction%>");
16966 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
16967 error_at (loc, "const, volatile or restrict qualified type in "
16968 "%<#pragma omp declare reduction%>");
16969 else
16970 {
16971 tree t;
16972 for (t = DECL_INITIAL (reduc_decl); t; t = TREE_CHAIN (t))
16973 if (comptypes (TREE_PURPOSE (t), type))
16974 {
16975 error_at (loc, "redeclaration of %qs "
16976 "%<#pragma omp declare reduction%> for "
16977 "type %qT",
16978 IDENTIFIER_POINTER (reduc_id)
16979 + sizeof ("omp declare reduction ") - 1,
16980 type);
16981 location_t ploc
16982 = DECL_SOURCE_LOCATION (TREE_VEC_ELT (TREE_VALUE (t),
16983 0));
16984 error_at (ploc, "previous %<#pragma omp declare "
16985 "reduction%>");
16986 break;
16987 }
16988 if (t == NULL_TREE)
16989 types.safe_push (type);
16990 }
16991 if (c_parser_next_token_is (parser, CPP_COMMA))
16992 c_parser_consume_token (parser);
16993 else
16994 break;
16995 }
16996 else
16997 break;
16998 }
16999
17000 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>")
17001 || types.is_empty ())
17002 {
17003 fail:
17004 clauses.release ();
17005 types.release ();
17006 while (true)
17007 {
17008 c_token *token = c_parser_peek_token (parser);
17009 if (token->type == CPP_EOF || token->type == CPP_PRAGMA_EOL)
17010 break;
17011 c_parser_consume_token (parser);
17012 }
17013 c_parser_skip_to_pragma_eol (parser);
17014 return;
17015 }
17016
17017 if (types.length () > 1)
17018 {
17019 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
17020 {
17021 c_token *token = c_parser_peek_token (parser);
17022 if (token->type == CPP_EOF)
17023 goto fail;
17024 clauses.safe_push (*token);
17025 c_parser_consume_token (parser);
17026 }
17027 clauses.safe_push (*c_parser_peek_token (parser));
17028 c_parser_skip_to_pragma_eol (parser);
17029
17030 /* Make sure nothing tries to read past the end of the tokens. */
17031 c_token eof_token;
17032 memset (&eof_token, 0, sizeof (eof_token));
17033 eof_token.type = CPP_EOF;
17034 clauses.safe_push (eof_token);
17035 clauses.safe_push (eof_token);
17036 }
17037
17038 int errs = errorcount;
17039 FOR_EACH_VEC_ELT (types, i, type)
17040 {
17041 tokens_avail = parser->tokens_avail;
17042 gcc_assert (parser->tokens == &parser->tokens_buf[0]);
17043 if (!clauses.is_empty ())
17044 {
17045 parser->tokens = clauses.address ();
17046 parser->tokens_avail = clauses.length ();
17047 parser->in_pragma = true;
17048 }
17049
17050 bool nested = current_function_decl != NULL_TREE;
17051 if (nested)
17052 c_push_function_context ();
17053 tree fndecl = build_decl (BUILTINS_LOCATION, FUNCTION_DECL,
17054 reduc_id, default_function_type);
17055 current_function_decl = fndecl;
17056 allocate_struct_function (fndecl, true);
17057 push_scope ();
17058 tree stmt = push_stmt_list ();
17059 /* Intentionally BUILTINS_LOCATION, so that -Wshadow doesn't
17060 warn about these. */
17061 tree omp_out = build_decl (BUILTINS_LOCATION, VAR_DECL,
17062 get_identifier ("omp_out"), type);
17063 DECL_ARTIFICIAL (omp_out) = 1;
17064 DECL_CONTEXT (omp_out) = fndecl;
17065 pushdecl (omp_out);
17066 tree omp_in = build_decl (BUILTINS_LOCATION, VAR_DECL,
17067 get_identifier ("omp_in"), type);
17068 DECL_ARTIFICIAL (omp_in) = 1;
17069 DECL_CONTEXT (omp_in) = fndecl;
17070 pushdecl (omp_in);
17071 struct c_expr combiner = c_parser_expression (parser);
17072 struct c_expr initializer;
17073 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE;
17074 bool bad = false;
17075 initializer.value = error_mark_node;
17076 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
17077 bad = true;
17078 else if (c_parser_next_token_is (parser, CPP_NAME)
17079 && strcmp (IDENTIFIER_POINTER
17080 (c_parser_peek_token (parser)->value),
17081 "initializer") == 0)
17082 {
17083 c_parser_consume_token (parser);
17084 pop_scope ();
17085 push_scope ();
17086 omp_priv = build_decl (BUILTINS_LOCATION, VAR_DECL,
17087 get_identifier ("omp_priv"), type);
17088 DECL_ARTIFICIAL (omp_priv) = 1;
17089 DECL_INITIAL (omp_priv) = error_mark_node;
17090 DECL_CONTEXT (omp_priv) = fndecl;
17091 pushdecl (omp_priv);
17092 omp_orig = build_decl (BUILTINS_LOCATION, VAR_DECL,
17093 get_identifier ("omp_orig"), type);
17094 DECL_ARTIFICIAL (omp_orig) = 1;
17095 DECL_CONTEXT (omp_orig) = fndecl;
17096 pushdecl (omp_orig);
17097 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
17098 bad = true;
17099 else if (!c_parser_next_token_is (parser, CPP_NAME))
17100 {
17101 c_parser_error (parser, "expected %<omp_priv%> or "
17102 "function-name");
17103 bad = true;
17104 }
17105 else if (strcmp (IDENTIFIER_POINTER
17106 (c_parser_peek_token (parser)->value),
17107 "omp_priv") != 0)
17108 {
17109 if (c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN
17110 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
17111 {
17112 c_parser_error (parser, "expected function-name %<(%>");
17113 bad = true;
17114 }
17115 else
17116 initializer = c_parser_postfix_expression (parser);
17117 if (initializer.value
17118 && TREE_CODE (initializer.value) == CALL_EXPR)
17119 {
17120 int j;
17121 tree c = initializer.value;
17122 for (j = 0; j < call_expr_nargs (c); j++)
17123 {
17124 tree a = CALL_EXPR_ARG (c, j);
17125 STRIP_NOPS (a);
17126 if (TREE_CODE (a) == ADDR_EXPR
17127 && TREE_OPERAND (a, 0) == omp_priv)
17128 break;
17129 }
17130 if (j == call_expr_nargs (c))
17131 error ("one of the initializer call arguments should be "
17132 "%<&omp_priv%>");
17133 }
17134 }
17135 else
17136 {
17137 c_parser_consume_token (parser);
17138 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
17139 bad = true;
17140 else
17141 {
17142 tree st = push_stmt_list ();
17143 start_init (omp_priv, NULL_TREE, 0);
17144 location_t loc = c_parser_peek_token (parser)->location;
17145 struct c_expr init = c_parser_initializer (parser);
17146 finish_init ();
17147 finish_decl (omp_priv, loc, init.value,
17148 init.original_type, NULL_TREE);
17149 pop_stmt_list (st);
17150 }
17151 }
17152 if (!bad
17153 && !c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
17154 bad = true;
17155 }
17156
17157 if (!bad)
17158 {
17159 c_parser_skip_to_pragma_eol (parser);
17160
17161 tree t = tree_cons (type, make_tree_vec (omp_priv ? 6 : 3),
17162 DECL_INITIAL (reduc_decl));
17163 DECL_INITIAL (reduc_decl) = t;
17164 DECL_SOURCE_LOCATION (omp_out) = rloc;
17165 TREE_VEC_ELT (TREE_VALUE (t), 0) = omp_out;
17166 TREE_VEC_ELT (TREE_VALUE (t), 1) = omp_in;
17167 TREE_VEC_ELT (TREE_VALUE (t), 2) = combiner.value;
17168 walk_tree (&combiner.value, c_check_omp_declare_reduction_r,
17169 &TREE_VEC_ELT (TREE_VALUE (t), 0), NULL);
17170 if (omp_priv)
17171 {
17172 DECL_SOURCE_LOCATION (omp_priv) = rloc;
17173 TREE_VEC_ELT (TREE_VALUE (t), 3) = omp_priv;
17174 TREE_VEC_ELT (TREE_VALUE (t), 4) = omp_orig;
17175 TREE_VEC_ELT (TREE_VALUE (t), 5) = initializer.value;
17176 walk_tree (&initializer.value, c_check_omp_declare_reduction_r,
17177 &TREE_VEC_ELT (TREE_VALUE (t), 3), NULL);
17178 walk_tree (&DECL_INITIAL (omp_priv),
17179 c_check_omp_declare_reduction_r,
17180 &TREE_VEC_ELT (TREE_VALUE (t), 3), NULL);
17181 }
17182 }
17183
17184 pop_stmt_list (stmt);
17185 pop_scope ();
17186 if (cfun->language != NULL)
17187 {
17188 ggc_free (cfun->language);
17189 cfun->language = NULL;
17190 }
17191 set_cfun (NULL);
17192 current_function_decl = NULL_TREE;
17193 if (nested)
17194 c_pop_function_context ();
17195
17196 if (!clauses.is_empty ())
17197 {
17198 parser->tokens = &parser->tokens_buf[0];
17199 parser->tokens_avail = tokens_avail;
17200 }
17201 if (bad)
17202 goto fail;
17203 if (errs != errorcount)
17204 break;
17205 }
17206
17207 clauses.release ();
17208 types.release ();
17209 }
17210
17211
17212 /* OpenMP 4.0
17213 #pragma omp declare simd declare-simd-clauses[optseq] new-line
17214 #pragma omp declare reduction (reduction-id : typename-list : expression) \
17215 initializer-clause[opt] new-line
17216 #pragma omp declare target new-line */
17217
17218 static void
17219 c_parser_omp_declare (c_parser *parser, enum pragma_context context)
17220 {
17221 c_parser_consume_pragma (parser);
17222 if (c_parser_next_token_is (parser, CPP_NAME))
17223 {
17224 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
17225 if (strcmp (p, "simd") == 0)
17226 {
17227 /* c_parser_consume_token (parser); done in
17228 c_parser_omp_declare_simd. */
17229 c_parser_omp_declare_simd (parser, context);
17230 return;
17231 }
17232 if (strcmp (p, "reduction") == 0)
17233 {
17234 c_parser_consume_token (parser);
17235 c_parser_omp_declare_reduction (parser, context);
17236 return;
17237 }
17238 if (!flag_openmp) /* flag_openmp_simd */
17239 {
17240 c_parser_skip_to_pragma_eol (parser, false);
17241 return;
17242 }
17243 if (strcmp (p, "target") == 0)
17244 {
17245 c_parser_consume_token (parser);
17246 c_parser_omp_declare_target (parser);
17247 return;
17248 }
17249 }
17250
17251 c_parser_error (parser, "expected %<simd%> or %<reduction%> "
17252 "or %<target%>");
17253 c_parser_skip_to_pragma_eol (parser);
17254 }
17255
17256 /* OpenMP 4.5:
17257 #pragma omp taskloop taskloop-clause[optseq] new-line
17258 for-loop
17259
17260 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
17261 for-loop */
17262
17263 #define OMP_TASKLOOP_CLAUSE_MASK \
17264 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
17265 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
17266 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
17267 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
17268 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
17269 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
17270 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
17271 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
17272 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
17273 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
17274 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
17275 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
17276 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
17277 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
17278
17279 static tree
17280 c_parser_omp_taskloop (location_t loc, c_parser *parser,
17281 char *p_name, omp_clause_mask mask, tree *cclauses,
17282 bool *if_p)
17283 {
17284 tree clauses, block, ret;
17285
17286 strcat (p_name, " taskloop");
17287 mask |= OMP_TASKLOOP_CLAUSE_MASK;
17288
17289 if (c_parser_next_token_is (parser, CPP_NAME))
17290 {
17291 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
17292
17293 if (strcmp (p, "simd") == 0)
17294 {
17295 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
17296 if (cclauses == NULL)
17297 cclauses = cclauses_buf;
17298 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION);
17299 c_parser_consume_token (parser);
17300 if (!flag_openmp) /* flag_openmp_simd */
17301 return c_parser_omp_simd (loc, parser, p_name, mask, cclauses,
17302 if_p);
17303 block = c_begin_compound_stmt (true);
17304 ret = c_parser_omp_simd (loc, parser, p_name, mask, cclauses, if_p);
17305 block = c_end_compound_stmt (loc, block, true);
17306 if (ret == NULL)
17307 return ret;
17308 ret = make_node (OMP_TASKLOOP);
17309 TREE_TYPE (ret) = void_type_node;
17310 OMP_FOR_BODY (ret) = block;
17311 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
17312 SET_EXPR_LOCATION (ret, loc);
17313 add_stmt (ret);
17314 return ret;
17315 }
17316 }
17317 if (!flag_openmp) /* flag_openmp_simd */
17318 {
17319 c_parser_skip_to_pragma_eol (parser, false);
17320 return NULL_TREE;
17321 }
17322
17323 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
17324 if (cclauses)
17325 {
17326 omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
17327 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
17328 }
17329
17330 block = c_begin_compound_stmt (true);
17331 ret = c_parser_omp_for_loop (loc, parser, OMP_TASKLOOP, clauses, NULL, if_p);
17332 block = c_end_compound_stmt (loc, block, true);
17333 add_stmt (block);
17334
17335 return ret;
17336 }
17337
17338 /* Main entry point to parsing most OpenMP pragmas. */
17339
17340 static void
17341 c_parser_omp_construct (c_parser *parser, bool *if_p)
17342 {
17343 enum pragma_kind p_kind;
17344 location_t loc;
17345 tree stmt;
17346 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
17347 omp_clause_mask mask (0);
17348
17349 loc = c_parser_peek_token (parser)->location;
17350 p_kind = c_parser_peek_token (parser)->pragma_kind;
17351 c_parser_consume_pragma (parser);
17352
17353 switch (p_kind)
17354 {
17355 case PRAGMA_OACC_ATOMIC:
17356 c_parser_omp_atomic (loc, parser);
17357 return;
17358 case PRAGMA_OACC_CACHE:
17359 strcpy (p_name, "#pragma acc");
17360 stmt = c_parser_oacc_cache (loc, parser);
17361 break;
17362 case PRAGMA_OACC_DATA:
17363 stmt = c_parser_oacc_data (loc, parser, if_p);
17364 break;
17365 case PRAGMA_OACC_HOST_DATA:
17366 stmt = c_parser_oacc_host_data (loc, parser, if_p);
17367 break;
17368 case PRAGMA_OACC_KERNELS:
17369 case PRAGMA_OACC_PARALLEL:
17370 strcpy (p_name, "#pragma acc");
17371 stmt = c_parser_oacc_kernels_parallel (loc, parser, p_kind, p_name,
17372 if_p);
17373 break;
17374 case PRAGMA_OACC_LOOP:
17375 strcpy (p_name, "#pragma acc");
17376 stmt = c_parser_oacc_loop (loc, parser, p_name, mask, NULL, if_p);
17377 break;
17378 case PRAGMA_OACC_WAIT:
17379 strcpy (p_name, "#pragma wait");
17380 stmt = c_parser_oacc_wait (loc, parser, p_name);
17381 break;
17382 case PRAGMA_OMP_ATOMIC:
17383 c_parser_omp_atomic (loc, parser);
17384 return;
17385 case PRAGMA_OMP_CRITICAL:
17386 stmt = c_parser_omp_critical (loc, parser, if_p);
17387 break;
17388 case PRAGMA_OMP_DISTRIBUTE:
17389 strcpy (p_name, "#pragma omp");
17390 stmt = c_parser_omp_distribute (loc, parser, p_name, mask, NULL, if_p);
17391 break;
17392 case PRAGMA_OMP_FOR:
17393 strcpy (p_name, "#pragma omp");
17394 stmt = c_parser_omp_for (loc, parser, p_name, mask, NULL, if_p);
17395 break;
17396 case PRAGMA_OMP_MASTER:
17397 stmt = c_parser_omp_master (loc, parser, if_p);
17398 break;
17399 case PRAGMA_OMP_PARALLEL:
17400 strcpy (p_name, "#pragma omp");
17401 stmt = c_parser_omp_parallel (loc, parser, p_name, mask, NULL, if_p);
17402 break;
17403 case PRAGMA_OMP_SECTIONS:
17404 strcpy (p_name, "#pragma omp");
17405 stmt = c_parser_omp_sections (loc, parser, p_name, mask, NULL);
17406 break;
17407 case PRAGMA_OMP_SIMD:
17408 strcpy (p_name, "#pragma omp");
17409 stmt = c_parser_omp_simd (loc, parser, p_name, mask, NULL, if_p);
17410 break;
17411 case PRAGMA_OMP_SINGLE:
17412 stmt = c_parser_omp_single (loc, parser, if_p);
17413 break;
17414 case PRAGMA_OMP_TASK:
17415 stmt = c_parser_omp_task (loc, parser, if_p);
17416 break;
17417 case PRAGMA_OMP_TASKGROUP:
17418 stmt = c_parser_omp_taskgroup (parser, if_p);
17419 break;
17420 case PRAGMA_OMP_TASKLOOP:
17421 strcpy (p_name, "#pragma omp");
17422 stmt = c_parser_omp_taskloop (loc, parser, p_name, mask, NULL, if_p);
17423 break;
17424 case PRAGMA_OMP_TEAMS:
17425 strcpy (p_name, "#pragma omp");
17426 stmt = c_parser_omp_teams (loc, parser, p_name, mask, NULL, if_p);
17427 break;
17428 default:
17429 gcc_unreachable ();
17430 }
17431
17432 if (stmt)
17433 gcc_assert (EXPR_LOCATION (stmt) != UNKNOWN_LOCATION);
17434 }
17435
17436
17437 /* OpenMP 2.5:
17438 # pragma omp threadprivate (variable-list) */
17439
17440 static void
17441 c_parser_omp_threadprivate (c_parser *parser)
17442 {
17443 tree vars, t;
17444 location_t loc;
17445
17446 c_parser_consume_pragma (parser);
17447 loc = c_parser_peek_token (parser)->location;
17448 vars = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
17449
17450 /* Mark every variable in VARS to be assigned thread local storage. */
17451 for (t = vars; t; t = TREE_CHAIN (t))
17452 {
17453 tree v = TREE_PURPOSE (t);
17454
17455 /* FIXME diagnostics: Ideally we should keep individual
17456 locations for all the variables in the var list to make the
17457 following errors more precise. Perhaps
17458 c_parser_omp_var_list_parens() should construct a list of
17459 locations to go along with the var list. */
17460
17461 /* If V had already been marked threadprivate, it doesn't matter
17462 whether it had been used prior to this point. */
17463 if (!VAR_P (v))
17464 error_at (loc, "%qD is not a variable", v);
17465 else if (TREE_USED (v) && !C_DECL_THREADPRIVATE_P (v))
17466 error_at (loc, "%qE declared %<threadprivate%> after first use", v);
17467 else if (! is_global_var (v))
17468 error_at (loc, "automatic variable %qE cannot be %<threadprivate%>", v);
17469 else if (TREE_TYPE (v) == error_mark_node)
17470 ;
17471 else if (! COMPLETE_TYPE_P (TREE_TYPE (v)))
17472 error_at (loc, "%<threadprivate%> %qE has incomplete type", v);
17473 else
17474 {
17475 if (! DECL_THREAD_LOCAL_P (v))
17476 {
17477 set_decl_tls_model (v, decl_default_tls_model (v));
17478 /* If rtl has been already set for this var, call
17479 make_decl_rtl once again, so that encode_section_info
17480 has a chance to look at the new decl flags. */
17481 if (DECL_RTL_SET_P (v))
17482 make_decl_rtl (v);
17483 }
17484 C_DECL_THREADPRIVATE_P (v) = 1;
17485 }
17486 }
17487
17488 c_parser_skip_to_pragma_eol (parser);
17489 }
17490 \f
17491 /* Cilk Plus <#pragma simd> parsing routines. */
17492
17493 /* Helper function for c_parser_pragma. Perform some sanity checking
17494 for <#pragma simd> constructs. Returns FALSE if there was a
17495 problem. */
17496
17497 static bool
17498 c_parser_cilk_verify_simd (c_parser *parser,
17499 enum pragma_context context)
17500 {
17501 if (!flag_cilkplus)
17502 {
17503 warning (0, "pragma simd ignored because -fcilkplus is not enabled");
17504 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
17505 return false;
17506 }
17507 if (context == pragma_external)
17508 {
17509 c_parser_error (parser,"pragma simd must be inside a function");
17510 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
17511 return false;
17512 }
17513 return true;
17514 }
17515
17516 /* Cilk Plus:
17517 This function is shared by SIMD-enabled functions and #pragma simd.
17518 If IS_SIMD_FN is true then it is parsing a SIMD-enabled function and
17519 CLAUSES is unused. The main purpose of this function is to parse a
17520 vectorlength attribute or clause and check for parse errors.
17521 When IS_SIMD_FN is true then the function is merely caching the tokens
17522 in PARSER->CILK_SIMD_FN_TOKENS. If errors are found then the token
17523 cache is cleared since there is no reason to continue.
17524 Syntax:
17525 vectorlength ( constant-expression ) */
17526
17527 static tree
17528 c_parser_cilk_clause_vectorlength (c_parser *parser, tree clauses,
17529 bool is_simd_fn)
17530 {
17531 if (is_simd_fn)
17532 check_no_duplicate_clause (clauses, OMP_CLAUSE_SIMDLEN, "vectorlength");
17533 else
17534 /* The vectorlength clause behaves exactly like OpenMP's safelen
17535 clause. Represent it in OpenMP terms. */
17536 check_no_duplicate_clause (clauses, OMP_CLAUSE_SAFELEN, "vectorlength");
17537
17538 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
17539 return clauses;
17540
17541 location_t loc = c_parser_peek_token (parser)->location;
17542 tree expr = c_parser_expr_no_commas (parser, NULL).value;
17543 expr = c_fully_fold (expr, false, NULL);
17544
17545 /* If expr is an error_mark_node then the above function would have
17546 emitted an error. No reason to do it twice. */
17547 if (expr == error_mark_node)
17548 ;
17549 else if (!TREE_TYPE (expr)
17550 || !TREE_CONSTANT (expr)
17551 || !INTEGRAL_TYPE_P (TREE_TYPE (expr)))
17552
17553 error_at (loc, "vectorlength must be an integer constant");
17554 else if (wi::exact_log2 (expr) == -1)
17555 error_at (loc, "vectorlength must be a power of 2");
17556 else
17557 {
17558 if (is_simd_fn)
17559 {
17560 tree u = build_omp_clause (loc, OMP_CLAUSE_SIMDLEN);
17561 OMP_CLAUSE_SIMDLEN_EXPR (u) = expr;
17562 OMP_CLAUSE_CHAIN (u) = clauses;
17563 clauses = u;
17564 }
17565 else
17566 {
17567 tree u = build_omp_clause (loc, OMP_CLAUSE_SAFELEN);
17568 OMP_CLAUSE_SAFELEN_EXPR (u) = expr;
17569 OMP_CLAUSE_CHAIN (u) = clauses;
17570 clauses = u;
17571 }
17572 }
17573
17574 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
17575
17576 return clauses;
17577 }
17578
17579 /* Cilk Plus:
17580 linear ( simd-linear-variable-list )
17581
17582 simd-linear-variable-list:
17583 simd-linear-variable
17584 simd-linear-variable-list , simd-linear-variable
17585
17586 simd-linear-variable:
17587 id-expression
17588 id-expression : simd-linear-step
17589
17590 simd-linear-step:
17591 conditional-expression */
17592
17593 static tree
17594 c_parser_cilk_clause_linear (c_parser *parser, tree clauses)
17595 {
17596 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
17597 return clauses;
17598
17599 location_t loc = c_parser_peek_token (parser)->location;
17600
17601 if (c_parser_next_token_is_not (parser, CPP_NAME)
17602 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
17603 c_parser_error (parser, "expected identifier");
17604
17605 while (c_parser_next_token_is (parser, CPP_NAME)
17606 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
17607 {
17608 tree var = lookup_name (c_parser_peek_token (parser)->value);
17609
17610 if (var == NULL)
17611 {
17612 undeclared_variable (c_parser_peek_token (parser)->location,
17613 c_parser_peek_token (parser)->value);
17614 c_parser_consume_token (parser);
17615 }
17616 else if (var == error_mark_node)
17617 c_parser_consume_token (parser);
17618 else
17619 {
17620 tree step = integer_one_node;
17621
17622 /* Parse the linear step if present. */
17623 if (c_parser_peek_2nd_token (parser)->type == CPP_COLON)
17624 {
17625 c_parser_consume_token (parser);
17626 c_parser_consume_token (parser);
17627
17628 tree expr = c_parser_expr_no_commas (parser, NULL).value;
17629 expr = c_fully_fold (expr, false, NULL);
17630
17631 if (TREE_TYPE (expr)
17632 && INTEGRAL_TYPE_P (TREE_TYPE (expr))
17633 && (TREE_CONSTANT (expr)
17634 || DECL_P (expr)))
17635 step = expr;
17636 else
17637 c_parser_error (parser,
17638 "step size must be an integer constant "
17639 "expression or an integer variable");
17640 }
17641 else
17642 c_parser_consume_token (parser);
17643
17644 /* Use OMP_CLAUSE_LINEAR, which has the same semantics. */
17645 tree u = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
17646 OMP_CLAUSE_DECL (u) = var;
17647 OMP_CLAUSE_LINEAR_STEP (u) = step;
17648 OMP_CLAUSE_CHAIN (u) = clauses;
17649 clauses = u;
17650 }
17651
17652 if (c_parser_next_token_is_not (parser, CPP_COMMA))
17653 break;
17654
17655 c_parser_consume_token (parser);
17656 }
17657
17658 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
17659
17660 return clauses;
17661 }
17662
17663 /* Returns the name of the next clause. If the clause is not
17664 recognized SIMD_OMP_CLAUSE_NONE is returned and the next token is
17665 not consumed. Otherwise, the appropriate pragma_simd_clause is
17666 returned and the token is consumed. */
17667
17668 static pragma_omp_clause
17669 c_parser_cilk_clause_name (c_parser *parser)
17670 {
17671 pragma_omp_clause result;
17672 c_token *token = c_parser_peek_token (parser);
17673
17674 if (!token->value || token->type != CPP_NAME)
17675 return PRAGMA_CILK_CLAUSE_NONE;
17676
17677 const char *p = IDENTIFIER_POINTER (token->value);
17678
17679 if (!strcmp (p, "vectorlength"))
17680 result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
17681 else if (!strcmp (p, "linear"))
17682 result = PRAGMA_CILK_CLAUSE_LINEAR;
17683 else if (!strcmp (p, "private"))
17684 result = PRAGMA_CILK_CLAUSE_PRIVATE;
17685 else if (!strcmp (p, "firstprivate"))
17686 result = PRAGMA_CILK_CLAUSE_FIRSTPRIVATE;
17687 else if (!strcmp (p, "lastprivate"))
17688 result = PRAGMA_CILK_CLAUSE_LASTPRIVATE;
17689 else if (!strcmp (p, "reduction"))
17690 result = PRAGMA_CILK_CLAUSE_REDUCTION;
17691 else
17692 return PRAGMA_CILK_CLAUSE_NONE;
17693
17694 c_parser_consume_token (parser);
17695 return result;
17696 }
17697
17698 /* Parse all #<pragma simd> clauses. Return the list of clauses
17699 found. */
17700
17701 static tree
17702 c_parser_cilk_all_clauses (c_parser *parser)
17703 {
17704 tree clauses = NULL;
17705
17706 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
17707 {
17708 pragma_omp_clause c_kind;
17709
17710 c_kind = c_parser_cilk_clause_name (parser);
17711
17712 switch (c_kind)
17713 {
17714 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
17715 clauses = c_parser_cilk_clause_vectorlength (parser, clauses, false);
17716 break;
17717 case PRAGMA_CILK_CLAUSE_LINEAR:
17718 clauses = c_parser_cilk_clause_linear (parser, clauses);
17719 break;
17720 case PRAGMA_CILK_CLAUSE_PRIVATE:
17721 /* Use the OpenMP counterpart. */
17722 clauses = c_parser_omp_clause_private (parser, clauses);
17723 break;
17724 case PRAGMA_CILK_CLAUSE_FIRSTPRIVATE:
17725 /* Use the OpenMP counterpart. */
17726 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
17727 break;
17728 case PRAGMA_CILK_CLAUSE_LASTPRIVATE:
17729 /* Use the OpenMP counterpart. */
17730 clauses = c_parser_omp_clause_lastprivate (parser, clauses);
17731 break;
17732 case PRAGMA_CILK_CLAUSE_REDUCTION:
17733 /* Use the OpenMP counterpart. */
17734 clauses = c_parser_omp_clause_reduction (parser, clauses);
17735 break;
17736 default:
17737 c_parser_error (parser, "expected %<#pragma simd%> clause");
17738 goto saw_error;
17739 }
17740 }
17741
17742 saw_error:
17743 c_parser_skip_to_pragma_eol (parser);
17744 return c_finish_omp_clauses (clauses, C_ORT_CILK);
17745 }
17746
17747 /* This function helps parse the grainsize pragma for a _Cilk_for statement.
17748 Here is the correct syntax of this pragma:
17749 #pragma cilk grainsize = <EXP>
17750 */
17751
17752 static void
17753 c_parser_cilk_grainsize (c_parser *parser, bool *if_p)
17754 {
17755 extern tree convert_to_integer (tree, tree);
17756
17757 /* consume the 'grainsize' keyword. */
17758 c_parser_consume_pragma (parser);
17759
17760 if (c_parser_require (parser, CPP_EQ, "expected %<=%>") != 0)
17761 {
17762 struct c_expr g_expr = c_parser_binary_expression (parser, NULL, NULL);
17763 if (g_expr.value == error_mark_node)
17764 {
17765 c_parser_skip_to_pragma_eol (parser);
17766 return;
17767 }
17768 tree grain = convert_to_integer (long_integer_type_node,
17769 c_fully_fold (g_expr.value, false,
17770 NULL));
17771 c_parser_skip_to_pragma_eol (parser);
17772 c_token *token = c_parser_peek_token (parser);
17773 if (token && token->type == CPP_KEYWORD
17774 && token->keyword == RID_CILK_FOR)
17775 {
17776 if (grain == NULL_TREE || grain == error_mark_node)
17777 grain = integer_zero_node;
17778 c_parser_cilk_for (parser, grain, if_p);
17779 }
17780 else
17781 warning (0, "%<#pragma cilk grainsize%> is not followed by "
17782 "%<_Cilk_for%>");
17783 }
17784 else
17785 c_parser_skip_to_pragma_eol (parser);
17786 }
17787
17788 /* Main entry point for parsing Cilk Plus <#pragma simd> for loops. */
17789
17790 static void
17791 c_parser_cilk_simd (c_parser *parser, bool *if_p)
17792 {
17793 tree clauses = c_parser_cilk_all_clauses (parser);
17794 tree block = c_begin_compound_stmt (true);
17795 location_t loc = c_parser_peek_token (parser)->location;
17796 c_parser_omp_for_loop (loc, parser, CILK_SIMD, clauses, NULL, if_p);
17797 block = c_end_compound_stmt (loc, block, true);
17798 add_stmt (block);
17799 }
17800
17801 /* Create an artificial decl with TYPE and emit initialization of it with
17802 INIT. */
17803
17804 static tree
17805 c_get_temp_regvar (tree type, tree init)
17806 {
17807 location_t loc = EXPR_LOCATION (init);
17808 tree decl = build_decl (loc, VAR_DECL, NULL_TREE, type);
17809 DECL_ARTIFICIAL (decl) = 1;
17810 DECL_IGNORED_P (decl) = 1;
17811 pushdecl (decl);
17812 tree t = build2 (INIT_EXPR, type, decl, init);
17813 add_stmt (t);
17814 return decl;
17815 }
17816
17817 /* Main entry point for parsing Cilk Plus _Cilk_for loops.
17818 GRAIN is the grain value passed in through pragma or 0. */
17819
17820 static void
17821 c_parser_cilk_for (c_parser *parser, tree grain, bool *if_p)
17822 {
17823 tree clauses = build_omp_clause (EXPR_LOCATION (grain), OMP_CLAUSE_SCHEDULE);
17824 OMP_CLAUSE_SCHEDULE_KIND (clauses) = OMP_CLAUSE_SCHEDULE_CILKFOR;
17825 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clauses) = grain;
17826 clauses = c_finish_omp_clauses (clauses, C_ORT_CILK);
17827
17828 tree block = c_begin_compound_stmt (true);
17829 tree sb = push_stmt_list ();
17830 location_t loc = c_parser_peek_token (parser)->location;
17831 tree omp_for = c_parser_omp_for_loop (loc, parser, CILK_FOR, clauses, NULL,
17832 if_p);
17833 sb = pop_stmt_list (sb);
17834
17835 if (omp_for)
17836 {
17837 tree omp_par = make_node (OMP_PARALLEL);
17838 TREE_TYPE (omp_par) = void_type_node;
17839 OMP_PARALLEL_CLAUSES (omp_par) = NULL_TREE;
17840 tree bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
17841 TREE_SIDE_EFFECTS (bind) = 1;
17842 BIND_EXPR_BODY (bind) = sb;
17843 OMP_PARALLEL_BODY (omp_par) = bind;
17844 if (OMP_FOR_PRE_BODY (omp_for))
17845 {
17846 add_stmt (OMP_FOR_PRE_BODY (omp_for));
17847 OMP_FOR_PRE_BODY (omp_for) = NULL_TREE;
17848 }
17849 tree init = TREE_VEC_ELT (OMP_FOR_INIT (omp_for), 0);
17850 tree decl = TREE_OPERAND (init, 0);
17851 tree cond = TREE_VEC_ELT (OMP_FOR_COND (omp_for), 0);
17852 tree incr = TREE_VEC_ELT (OMP_FOR_INCR (omp_for), 0);
17853 tree t = TREE_OPERAND (cond, 1), c, clauses = NULL_TREE;
17854 if (TREE_CODE (t) != INTEGER_CST)
17855 {
17856 TREE_OPERAND (cond, 1) = c_get_temp_regvar (TREE_TYPE (t), t);
17857 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
17858 OMP_CLAUSE_DECL (c) = TREE_OPERAND (cond, 1);
17859 OMP_CLAUSE_CHAIN (c) = clauses;
17860 clauses = c;
17861 }
17862 if (TREE_CODE (incr) == MODIFY_EXPR)
17863 {
17864 t = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
17865 if (TREE_CODE (t) != INTEGER_CST)
17866 {
17867 TREE_OPERAND (TREE_OPERAND (incr, 1), 1)
17868 = c_get_temp_regvar (TREE_TYPE (t), t);
17869 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
17870 OMP_CLAUSE_DECL (c) = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
17871 OMP_CLAUSE_CHAIN (c) = clauses;
17872 clauses = c;
17873 }
17874 }
17875 t = TREE_OPERAND (init, 1);
17876 if (TREE_CODE (t) != INTEGER_CST)
17877 {
17878 TREE_OPERAND (init, 1) = c_get_temp_regvar (TREE_TYPE (t), t);
17879 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
17880 OMP_CLAUSE_DECL (c) = TREE_OPERAND (init, 1);
17881 OMP_CLAUSE_CHAIN (c) = clauses;
17882 clauses = c;
17883 }
17884 c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
17885 OMP_CLAUSE_DECL (c) = decl;
17886 OMP_CLAUSE_CHAIN (c) = clauses;
17887 clauses = c;
17888 c = build_omp_clause (input_location, OMP_CLAUSE__CILK_FOR_COUNT_);
17889 OMP_CLAUSE_OPERAND (c, 0)
17890 = cilk_for_number_of_iterations (omp_for);
17891 OMP_CLAUSE_CHAIN (c) = clauses;
17892 OMP_PARALLEL_CLAUSES (omp_par) = c_finish_omp_clauses (c, C_ORT_CILK);
17893 add_stmt (omp_par);
17894 }
17895
17896 block = c_end_compound_stmt (loc, block, true);
17897 add_stmt (block);
17898 }
17899
17900 \f
17901 /* Parse a transaction attribute (GCC Extension).
17902
17903 transaction-attribute:
17904 attributes
17905 [ [ any-word ] ]
17906
17907 The transactional memory language description is written for C++,
17908 and uses the C++0x attribute syntax. For compatibility, allow the
17909 bracket style for transactions in C as well. */
17910
17911 static tree
17912 c_parser_transaction_attributes (c_parser *parser)
17913 {
17914 tree attr_name, attr = NULL;
17915
17916 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
17917 return c_parser_attributes (parser);
17918
17919 if (!c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
17920 return NULL_TREE;
17921 c_parser_consume_token (parser);
17922 if (!c_parser_require (parser, CPP_OPEN_SQUARE, "expected %<[%>"))
17923 goto error1;
17924
17925 attr_name = c_parser_attribute_any_word (parser);
17926 if (attr_name)
17927 {
17928 c_parser_consume_token (parser);
17929 attr = build_tree_list (attr_name, NULL_TREE);
17930 }
17931 else
17932 c_parser_error (parser, "expected identifier");
17933
17934 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
17935 error1:
17936 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
17937 return attr;
17938 }
17939
17940 /* Parse a __transaction_atomic or __transaction_relaxed statement
17941 (GCC Extension).
17942
17943 transaction-statement:
17944 __transaction_atomic transaction-attribute[opt] compound-statement
17945 __transaction_relaxed compound-statement
17946
17947 Note that the only valid attribute is: "outer".
17948 */
17949
17950 static tree
17951 c_parser_transaction (c_parser *parser, enum rid keyword)
17952 {
17953 unsigned int old_in = parser->in_transaction;
17954 unsigned int this_in = 1, new_in;
17955 location_t loc = c_parser_peek_token (parser)->location;
17956 tree stmt, attrs;
17957
17958 gcc_assert ((keyword == RID_TRANSACTION_ATOMIC
17959 || keyword == RID_TRANSACTION_RELAXED)
17960 && c_parser_next_token_is_keyword (parser, keyword));
17961 c_parser_consume_token (parser);
17962
17963 if (keyword == RID_TRANSACTION_RELAXED)
17964 this_in |= TM_STMT_ATTR_RELAXED;
17965 else
17966 {
17967 attrs = c_parser_transaction_attributes (parser);
17968 if (attrs)
17969 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
17970 }
17971
17972 /* Keep track if we're in the lexical scope of an outer transaction. */
17973 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
17974
17975 parser->in_transaction = new_in;
17976 stmt = c_parser_compound_statement (parser);
17977 parser->in_transaction = old_in;
17978
17979 if (flag_tm)
17980 stmt = c_finish_transaction (loc, stmt, this_in);
17981 else
17982 error_at (loc, (keyword == RID_TRANSACTION_ATOMIC ?
17983 "%<__transaction_atomic%> without transactional memory support enabled"
17984 : "%<__transaction_relaxed %> "
17985 "without transactional memory support enabled"));
17986
17987 return stmt;
17988 }
17989
17990 /* Parse a __transaction_atomic or __transaction_relaxed expression
17991 (GCC Extension).
17992
17993 transaction-expression:
17994 __transaction_atomic ( expression )
17995 __transaction_relaxed ( expression )
17996 */
17997
17998 static struct c_expr
17999 c_parser_transaction_expression (c_parser *parser, enum rid keyword)
18000 {
18001 struct c_expr ret;
18002 unsigned int old_in = parser->in_transaction;
18003 unsigned int this_in = 1;
18004 location_t loc = c_parser_peek_token (parser)->location;
18005 tree attrs;
18006
18007 gcc_assert ((keyword == RID_TRANSACTION_ATOMIC
18008 || keyword == RID_TRANSACTION_RELAXED)
18009 && c_parser_next_token_is_keyword (parser, keyword));
18010 c_parser_consume_token (parser);
18011
18012 if (keyword == RID_TRANSACTION_RELAXED)
18013 this_in |= TM_STMT_ATTR_RELAXED;
18014 else
18015 {
18016 attrs = c_parser_transaction_attributes (parser);
18017 if (attrs)
18018 this_in |= parse_tm_stmt_attr (attrs, 0);
18019 }
18020
18021 parser->in_transaction = this_in;
18022 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
18023 {
18024 tree expr = c_parser_expression (parser).value;
18025 ret.original_type = TREE_TYPE (expr);
18026 ret.value = build1 (TRANSACTION_EXPR, ret.original_type, expr);
18027 if (this_in & TM_STMT_ATTR_RELAXED)
18028 TRANSACTION_EXPR_RELAXED (ret.value) = 1;
18029 SET_EXPR_LOCATION (ret.value, loc);
18030 ret.original_code = TRANSACTION_EXPR;
18031 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
18032 {
18033 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
18034 goto error;
18035 }
18036 }
18037 else
18038 {
18039 error:
18040 ret.value = error_mark_node;
18041 ret.original_code = ERROR_MARK;
18042 ret.original_type = NULL;
18043 }
18044 parser->in_transaction = old_in;
18045
18046 if (!flag_tm)
18047 error_at (loc, (keyword == RID_TRANSACTION_ATOMIC ?
18048 "%<__transaction_atomic%> without transactional memory support enabled"
18049 : "%<__transaction_relaxed %> "
18050 "without transactional memory support enabled"));
18051
18052 set_c_expr_source_range (&ret, loc, loc);
18053
18054 return ret;
18055 }
18056
18057 /* Parse a __transaction_cancel statement (GCC Extension).
18058
18059 transaction-cancel-statement:
18060 __transaction_cancel transaction-attribute[opt] ;
18061
18062 Note that the only valid attribute is "outer".
18063 */
18064
18065 static tree
18066 c_parser_transaction_cancel (c_parser *parser)
18067 {
18068 location_t loc = c_parser_peek_token (parser)->location;
18069 tree attrs;
18070 bool is_outer = false;
18071
18072 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TRANSACTION_CANCEL));
18073 c_parser_consume_token (parser);
18074
18075 attrs = c_parser_transaction_attributes (parser);
18076 if (attrs)
18077 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
18078
18079 if (!flag_tm)
18080 {
18081 error_at (loc, "%<__transaction_cancel%> without "
18082 "transactional memory support enabled");
18083 goto ret_error;
18084 }
18085 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
18086 {
18087 error_at (loc, "%<__transaction_cancel%> within a "
18088 "%<__transaction_relaxed%>");
18089 goto ret_error;
18090 }
18091 else if (is_outer)
18092 {
18093 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
18094 && !is_tm_may_cancel_outer (current_function_decl))
18095 {
18096 error_at (loc, "outer %<__transaction_cancel%> not "
18097 "within outer %<__transaction_atomic%>");
18098 error_at (loc, " or a %<transaction_may_cancel_outer%> function");
18099 goto ret_error;
18100 }
18101 }
18102 else if (parser->in_transaction == 0)
18103 {
18104 error_at (loc, "%<__transaction_cancel%> not within "
18105 "%<__transaction_atomic%>");
18106 goto ret_error;
18107 }
18108
18109 return add_stmt (build_tm_abort_call (loc, is_outer));
18110
18111 ret_error:
18112 return build1 (NOP_EXPR, void_type_node, error_mark_node);
18113 }
18114 \f
18115 /* Parse a single source file. */
18116
18117 void
18118 c_parse_file (void)
18119 {
18120 /* Use local storage to begin. If the first token is a pragma, parse it.
18121 If it is #pragma GCC pch_preprocess, then this will load a PCH file
18122 which will cause garbage collection. */
18123 c_parser tparser;
18124
18125 memset (&tparser, 0, sizeof tparser);
18126 tparser.tokens = &tparser.tokens_buf[0];
18127 the_parser = &tparser;
18128
18129 if (c_parser_peek_token (&tparser)->pragma_kind == PRAGMA_GCC_PCH_PREPROCESS)
18130 c_parser_pragma_pch_preprocess (&tparser);
18131
18132 the_parser = ggc_alloc<c_parser> ();
18133 *the_parser = tparser;
18134 if (tparser.tokens == &tparser.tokens_buf[0])
18135 the_parser->tokens = &the_parser->tokens_buf[0];
18136
18137 /* Initialize EH, if we've been told to do so. */
18138 if (flag_exceptions)
18139 using_eh_for_cleanups ();
18140
18141 c_parser_translation_unit (the_parser);
18142 the_parser = NULL;
18143 }
18144
18145 /* This function parses Cilk Plus array notation. The starting index is
18146 passed in INITIAL_INDEX and the array name is passes in ARRAY_VALUE. The
18147 return value of this function is a tree_node called VALUE_TREE of type
18148 ARRAY_NOTATION_REF. */
18149
18150 static tree
18151 c_parser_array_notation (location_t loc, c_parser *parser, tree initial_index,
18152 tree array_value)
18153 {
18154 c_token *token = NULL;
18155 tree start_index = NULL_TREE, end_index = NULL_TREE, stride = NULL_TREE;
18156 tree value_tree = NULL_TREE, type = NULL_TREE, array_type = NULL_TREE;
18157 tree array_type_domain = NULL_TREE;
18158
18159 if (array_value == error_mark_node || initial_index == error_mark_node)
18160 {
18161 /* No need to continue. If either of these 2 were true, then an error
18162 must be emitted already. Thus, no need to emit them twice. */
18163 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
18164 return error_mark_node;
18165 }
18166
18167 array_type = TREE_TYPE (array_value);
18168 gcc_assert (array_type);
18169 if (TREE_CODE (array_type) != ARRAY_TYPE
18170 && TREE_CODE (array_type) != POINTER_TYPE)
18171 {
18172 error_at (loc, "base of array section must be pointer or array type");
18173 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
18174 return error_mark_node;
18175 }
18176 type = TREE_TYPE (array_type);
18177 token = c_parser_peek_token (parser);
18178
18179 if (token->type == CPP_EOF)
18180 {
18181 c_parser_error (parser, "expected %<:%> or numeral");
18182 return value_tree;
18183 }
18184 else if (token->type == CPP_COLON)
18185 {
18186 if (!initial_index)
18187 {
18188 /* If we are here, then we have a case like this A[:]. */
18189 c_parser_consume_token (parser);
18190 if (TREE_CODE (array_type) == POINTER_TYPE)
18191 {
18192 error_at (loc, "start-index and length fields necessary for "
18193 "using array notations in pointers");
18194 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
18195 return error_mark_node;
18196 }
18197 if (TREE_CODE (array_type) == FUNCTION_TYPE)
18198 {
18199 error_at (loc, "array notations cannot be used with function "
18200 "type");
18201 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
18202 return error_mark_node;
18203 }
18204 array_type_domain = TYPE_DOMAIN (array_type);
18205
18206 if (!array_type_domain)
18207 {
18208 error_at (loc, "start-index and length fields necessary for "
18209 "using array notations in dimensionless arrays");
18210 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
18211 return error_mark_node;
18212 }
18213
18214 start_index = TYPE_MINVAL (array_type_domain);
18215 start_index = fold_build1 (CONVERT_EXPR, ptrdiff_type_node,
18216 start_index);
18217 if (!TYPE_MAXVAL (array_type_domain)
18218 || !TREE_CONSTANT (TYPE_MAXVAL (array_type_domain)))
18219 {
18220 error_at (loc, "start-index and length fields necessary for "
18221 "using array notations in variable-length arrays");
18222 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
18223 return error_mark_node;
18224 }
18225 end_index = TYPE_MAXVAL (array_type_domain);
18226 end_index = fold_build2 (PLUS_EXPR, TREE_TYPE (end_index),
18227 end_index, integer_one_node);
18228 end_index = fold_build1 (CONVERT_EXPR, ptrdiff_type_node, end_index);
18229 stride = build_int_cst (integer_type_node, 1);
18230 stride = fold_build1 (CONVERT_EXPR, ptrdiff_type_node, stride);
18231 }
18232 else if (initial_index != error_mark_node)
18233 {
18234 /* If we are here, then there should be 2 possibilities:
18235 1. Array [EXPR : EXPR]
18236 2. Array [EXPR : EXPR : EXPR]
18237 */
18238 start_index = initial_index;
18239
18240 if (TREE_CODE (array_type) == FUNCTION_TYPE)
18241 {
18242 error_at (loc, "array notations cannot be used with function "
18243 "type");
18244 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
18245 return error_mark_node;
18246 }
18247 c_parser_consume_token (parser); /* consume the ':' */
18248 struct c_expr ce = c_parser_expression (parser);
18249 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
18250 end_index = ce.value;
18251 if (!end_index || end_index == error_mark_node)
18252 {
18253 c_parser_skip_to_end_of_block_or_statement (parser);
18254 return error_mark_node;
18255 }
18256 if (c_parser_peek_token (parser)->type == CPP_COLON)
18257 {
18258 c_parser_consume_token (parser);
18259 ce = c_parser_expression (parser);
18260 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
18261 stride = ce.value;
18262 if (!stride || stride == error_mark_node)
18263 {
18264 c_parser_skip_to_end_of_block_or_statement (parser);
18265 return error_mark_node;
18266 }
18267 }
18268 }
18269 else
18270 c_parser_error (parser, "expected array notation expression");
18271 }
18272 else
18273 c_parser_error (parser, "expected array notation expression");
18274
18275 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
18276
18277 value_tree = build_array_notation_ref (loc, array_value, start_index,
18278 end_index, stride, type);
18279 if (value_tree != error_mark_node)
18280 SET_EXPR_LOCATION (value_tree, loc);
18281 return value_tree;
18282 }
18283
18284 #include "gt-c-c-parser.h"