]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/c/c-parser.c
Update copyright years.
[thirdparty/gcc.git] / gcc / c / c-parser.c
1 /* Parser for C and Objective-C.
2 Copyright (C) 1987-2019 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 #define INCLUDE_UNIQUE_PTR
40 #include "system.h"
41 #include "coretypes.h"
42 #include "target.h"
43 #include "function.h"
44 #include "c-tree.h"
45 #include "timevar.h"
46 #include "stringpool.h"
47 #include "cgraph.h"
48 #include "attribs.h"
49 #include "stor-layout.h"
50 #include "varasm.h"
51 #include "trans-mem.h"
52 #include "c-family/c-pragma.h"
53 #include "c-lang.h"
54 #include "c-family/c-objc.h"
55 #include "plugin.h"
56 #include "omp-general.h"
57 #include "omp-offload.h"
58 #include "builtins.h"
59 #include "gomp-constants.h"
60 #include "c-family/c-indentation.h"
61 #include "gimple-expr.h"
62 #include "context.h"
63 #include "gcc-rich-location.h"
64 #include "c-parser.h"
65 #include "gimple-parser.h"
66 #include "read-rtl-function.h"
67 #include "run-rtl-passes.h"
68 #include "intl.h"
69 #include "c-family/name-hint.h"
70 #include "tree-iterator.h"
71 #include "memmodel.h"
72
73 /* We need to walk over decls with incomplete struct/union/enum types
74 after parsing the whole translation unit.
75 In finish_decl(), if the decl is static, has incomplete
76 struct/union/enum type, it is appeneded to incomplete_record_decls.
77 In c_parser_translation_unit(), we iterate over incomplete_record_decls
78 and report error if any of the decls are still incomplete. */
79
80 vec<tree> incomplete_record_decls;
81
82 void
83 set_c_expr_source_range (c_expr *expr,
84 location_t start, location_t finish)
85 {
86 expr->src_range.m_start = start;
87 expr->src_range.m_finish = finish;
88 if (expr->value)
89 set_source_range (expr->value, start, finish);
90 }
91
92 void
93 set_c_expr_source_range (c_expr *expr,
94 source_range src_range)
95 {
96 expr->src_range = src_range;
97 if (expr->value)
98 set_source_range (expr->value, src_range);
99 }
100
101 \f
102 /* Initialization routine for this file. */
103
104 void
105 c_parse_init (void)
106 {
107 /* The only initialization required is of the reserved word
108 identifiers. */
109 unsigned int i;
110 tree id;
111 int mask = 0;
112
113 /* Make sure RID_MAX hasn't grown past the 8 bits used to hold the keyword in
114 the c_token structure. */
115 gcc_assert (RID_MAX <= 255);
116
117 mask |= D_CXXONLY;
118 if (!flag_isoc99)
119 mask |= D_C99;
120 if (flag_no_asm)
121 {
122 mask |= D_ASM | D_EXT;
123 if (!flag_isoc99)
124 mask |= D_EXT89;
125 }
126 if (!c_dialect_objc ())
127 mask |= D_OBJC | D_CXX_OBJC;
128
129 ridpointers = ggc_cleared_vec_alloc<tree> ((int) RID_MAX);
130 for (i = 0; i < num_c_common_reswords; i++)
131 {
132 /* If a keyword is disabled, do not enter it into the table
133 and so create a canonical spelling that isn't a keyword. */
134 if (c_common_reswords[i].disable & mask)
135 {
136 if (warn_cxx_compat
137 && (c_common_reswords[i].disable & D_CXXWARN))
138 {
139 id = get_identifier (c_common_reswords[i].word);
140 C_SET_RID_CODE (id, RID_CXX_COMPAT_WARN);
141 C_IS_RESERVED_WORD (id) = 1;
142 }
143 continue;
144 }
145
146 id = get_identifier (c_common_reswords[i].word);
147 C_SET_RID_CODE (id, c_common_reswords[i].rid);
148 C_IS_RESERVED_WORD (id) = 1;
149 ridpointers [(int) c_common_reswords[i].rid] = id;
150 }
151
152 for (i = 0; i < NUM_INT_N_ENTS; i++)
153 {
154 /* We always create the symbols but they aren't always supported. */
155 char name[50];
156 sprintf (name, "__int%d", int_n_data[i].bitsize);
157 id = get_identifier (name);
158 C_SET_RID_CODE (id, RID_FIRST_INT_N + i);
159 C_IS_RESERVED_WORD (id) = 1;
160 }
161 }
162 \f
163 /* A parser structure recording information about the state and
164 context of parsing. Includes lexer information with up to two
165 tokens of look-ahead; more are not needed for C. */
166 struct GTY(()) c_parser {
167 /* The look-ahead tokens. */
168 c_token * GTY((skip)) tokens;
169 /* Buffer for look-ahead tokens. */
170 c_token tokens_buf[4];
171 /* How many look-ahead tokens are available (0 - 4, or
172 more if parsing from pre-lexed tokens). */
173 unsigned int tokens_avail;
174 /* True if a syntax error is being recovered from; false otherwise.
175 c_parser_error sets this flag. It should clear this flag when
176 enough tokens have been consumed to recover from the error. */
177 BOOL_BITFIELD error : 1;
178 /* True if we're processing a pragma, and shouldn't automatically
179 consume CPP_PRAGMA_EOL. */
180 BOOL_BITFIELD in_pragma : 1;
181 /* True if we're parsing the outermost block of an if statement. */
182 BOOL_BITFIELD in_if_block : 1;
183 /* True if we want to lex an untranslated string. */
184 BOOL_BITFIELD lex_untranslated_string : 1;
185
186 /* Objective-C specific parser/lexer information. */
187
188 /* True if we are in a context where the Objective-C "PQ" keywords
189 are considered keywords. */
190 BOOL_BITFIELD objc_pq_context : 1;
191 /* True if we are parsing a (potential) Objective-C foreach
192 statement. This is set to true after we parsed 'for (' and while
193 we wait for 'in' or ';' to decide if it's a standard C for loop or an
194 Objective-C foreach loop. */
195 BOOL_BITFIELD objc_could_be_foreach_context : 1;
196 /* The following flag is needed to contextualize Objective-C lexical
197 analysis. In some cases (e.g., 'int NSObject;'), it is
198 undesirable to bind an identifier to an Objective-C class, even
199 if a class with that name exists. */
200 BOOL_BITFIELD objc_need_raw_identifier : 1;
201 /* Nonzero if we're processing a __transaction statement. The value
202 is 1 | TM_STMT_ATTR_*. */
203 unsigned int in_transaction : 4;
204 /* True if we are in a context where the Objective-C "Property attribute"
205 keywords are valid. */
206 BOOL_BITFIELD objc_property_attr_context : 1;
207
208 /* Location of the last consumed token. */
209 location_t last_token_location;
210 };
211
212 /* Return a pointer to the Nth token in PARSERs tokens_buf. */
213
214 c_token *
215 c_parser_tokens_buf (c_parser *parser, unsigned n)
216 {
217 return &parser->tokens_buf[n];
218 }
219
220 /* Return the error state of PARSER. */
221
222 bool
223 c_parser_error (c_parser *parser)
224 {
225 return parser->error;
226 }
227
228 /* Set the error state of PARSER to ERR. */
229
230 void
231 c_parser_set_error (c_parser *parser, bool err)
232 {
233 parser->error = err;
234 }
235
236
237 /* The actual parser and external interface. ??? Does this need to be
238 garbage-collected? */
239
240 static GTY (()) c_parser *the_parser;
241
242 /* Read in and lex a single token, storing it in *TOKEN. */
243
244 static void
245 c_lex_one_token (c_parser *parser, c_token *token)
246 {
247 timevar_push (TV_LEX);
248
249 token->type = c_lex_with_flags (&token->value, &token->location,
250 &token->flags,
251 (parser->lex_untranslated_string
252 ? C_LEX_STRING_NO_TRANSLATE : 0));
253 token->id_kind = C_ID_NONE;
254 token->keyword = RID_MAX;
255 token->pragma_kind = PRAGMA_NONE;
256
257 switch (token->type)
258 {
259 case CPP_NAME:
260 {
261 tree decl;
262
263 bool objc_force_identifier = parser->objc_need_raw_identifier;
264 if (c_dialect_objc ())
265 parser->objc_need_raw_identifier = false;
266
267 if (C_IS_RESERVED_WORD (token->value))
268 {
269 enum rid rid_code = C_RID_CODE (token->value);
270
271 if (rid_code == RID_CXX_COMPAT_WARN)
272 {
273 warning_at (token->location,
274 OPT_Wc___compat,
275 "identifier %qE conflicts with C++ keyword",
276 token->value);
277 }
278 else if (rid_code >= RID_FIRST_ADDR_SPACE
279 && rid_code <= RID_LAST_ADDR_SPACE)
280 {
281 addr_space_t as;
282 as = (addr_space_t) (rid_code - RID_FIRST_ADDR_SPACE);
283 targetm.addr_space.diagnose_usage (as, token->location);
284 token->id_kind = C_ID_ADDRSPACE;
285 token->keyword = rid_code;
286 break;
287 }
288 else if (c_dialect_objc () && OBJC_IS_PQ_KEYWORD (rid_code))
289 {
290 /* We found an Objective-C "pq" keyword (in, out,
291 inout, bycopy, byref, oneway). They need special
292 care because the interpretation depends on the
293 context. */
294 if (parser->objc_pq_context)
295 {
296 token->type = CPP_KEYWORD;
297 token->keyword = rid_code;
298 break;
299 }
300 else if (parser->objc_could_be_foreach_context
301 && rid_code == RID_IN)
302 {
303 /* We are in Objective-C, inside a (potential)
304 foreach context (which means after having
305 parsed 'for (', but before having parsed ';'),
306 and we found 'in'. We consider it the keyword
307 which terminates the declaration at the
308 beginning of a foreach-statement. Note that
309 this means you can't use 'in' for anything else
310 in that context; in particular, in Objective-C
311 you can't use 'in' as the name of the running
312 variable in a C for loop. We could potentially
313 try to add code here to disambiguate, but it
314 seems a reasonable limitation. */
315 token->type = CPP_KEYWORD;
316 token->keyword = rid_code;
317 break;
318 }
319 /* Else, "pq" keywords outside of the "pq" context are
320 not keywords, and we fall through to the code for
321 normal tokens. */
322 }
323 else if (c_dialect_objc () && OBJC_IS_PATTR_KEYWORD (rid_code))
324 {
325 /* We found an Objective-C "property attribute"
326 keyword (getter, setter, readonly, etc). These are
327 only valid in the property context. */
328 if (parser->objc_property_attr_context)
329 {
330 token->type = CPP_KEYWORD;
331 token->keyword = rid_code;
332 break;
333 }
334 /* Else they are not special keywords.
335 */
336 }
337 else if (c_dialect_objc ()
338 && (OBJC_IS_AT_KEYWORD (rid_code)
339 || OBJC_IS_CXX_KEYWORD (rid_code)))
340 {
341 /* We found one of the Objective-C "@" keywords (defs,
342 selector, synchronized, etc) or one of the
343 Objective-C "cxx" keywords (class, private,
344 protected, public, try, catch, throw) without a
345 preceding '@' sign. Do nothing and fall through to
346 the code for normal tokens (in C++ we would still
347 consider the CXX ones keywords, but not in C). */
348 ;
349 }
350 else
351 {
352 token->type = CPP_KEYWORD;
353 token->keyword = rid_code;
354 break;
355 }
356 }
357
358 decl = lookup_name (token->value);
359 if (decl)
360 {
361 if (TREE_CODE (decl) == TYPE_DECL)
362 {
363 token->id_kind = C_ID_TYPENAME;
364 break;
365 }
366 }
367 else if (c_dialect_objc ())
368 {
369 tree objc_interface_decl = objc_is_class_name (token->value);
370 /* Objective-C class names are in the same namespace as
371 variables and typedefs, and hence are shadowed by local
372 declarations. */
373 if (objc_interface_decl
374 && (!objc_force_identifier || global_bindings_p ()))
375 {
376 token->value = objc_interface_decl;
377 token->id_kind = C_ID_CLASSNAME;
378 break;
379 }
380 }
381 token->id_kind = C_ID_ID;
382 }
383 break;
384 case CPP_AT_NAME:
385 /* This only happens in Objective-C; it must be a keyword. */
386 token->type = CPP_KEYWORD;
387 switch (C_RID_CODE (token->value))
388 {
389 /* Replace 'class' with '@class', 'private' with '@private',
390 etc. This prevents confusion with the C++ keyword
391 'class', and makes the tokens consistent with other
392 Objective-C 'AT' keywords. For example '@class' is
393 reported as RID_AT_CLASS which is consistent with
394 '@synchronized', which is reported as
395 RID_AT_SYNCHRONIZED.
396 */
397 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
398 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
399 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
400 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
401 case RID_THROW: token->keyword = RID_AT_THROW; break;
402 case RID_TRY: token->keyword = RID_AT_TRY; break;
403 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
404 case RID_SYNCHRONIZED: token->keyword = RID_AT_SYNCHRONIZED; break;
405 default: token->keyword = C_RID_CODE (token->value);
406 }
407 break;
408 case CPP_COLON:
409 case CPP_COMMA:
410 case CPP_CLOSE_PAREN:
411 case CPP_SEMICOLON:
412 /* These tokens may affect the interpretation of any identifiers
413 following, if doing Objective-C. */
414 if (c_dialect_objc ())
415 parser->objc_need_raw_identifier = false;
416 break;
417 case CPP_PRAGMA:
418 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
419 token->pragma_kind = (enum pragma_kind) TREE_INT_CST_LOW (token->value);
420 token->value = NULL;
421 break;
422 default:
423 break;
424 }
425 timevar_pop (TV_LEX);
426 }
427
428 /* Return a pointer to the next token from PARSER, reading it in if
429 necessary. */
430
431 c_token *
432 c_parser_peek_token (c_parser *parser)
433 {
434 if (parser->tokens_avail == 0)
435 {
436 c_lex_one_token (parser, &parser->tokens[0]);
437 parser->tokens_avail = 1;
438 }
439 return &parser->tokens[0];
440 }
441
442 /* Return a pointer to the next-but-one token from PARSER, reading it
443 in if necessary. The next token is already read in. */
444
445 c_token *
446 c_parser_peek_2nd_token (c_parser *parser)
447 {
448 if (parser->tokens_avail >= 2)
449 return &parser->tokens[1];
450 gcc_assert (parser->tokens_avail == 1);
451 gcc_assert (parser->tokens[0].type != CPP_EOF);
452 gcc_assert (parser->tokens[0].type != CPP_PRAGMA_EOL);
453 c_lex_one_token (parser, &parser->tokens[1]);
454 parser->tokens_avail = 2;
455 return &parser->tokens[1];
456 }
457
458 /* Return a pointer to the Nth token from PARSER, reading it
459 in if necessary. The N-1th token is already read in. */
460
461 c_token *
462 c_parser_peek_nth_token (c_parser *parser, unsigned int n)
463 {
464 /* N is 1-based, not zero-based. */
465 gcc_assert (n > 0);
466
467 if (parser->tokens_avail >= n)
468 return &parser->tokens[n - 1];
469 gcc_assert (parser->tokens_avail == n - 1);
470 c_lex_one_token (parser, &parser->tokens[n - 1]);
471 parser->tokens_avail = n;
472 return &parser->tokens[n - 1];
473 }
474
475 bool
476 c_keyword_starts_typename (enum rid keyword)
477 {
478 switch (keyword)
479 {
480 case RID_UNSIGNED:
481 case RID_LONG:
482 case RID_SHORT:
483 case RID_SIGNED:
484 case RID_COMPLEX:
485 case RID_INT:
486 case RID_CHAR:
487 case RID_FLOAT:
488 case RID_DOUBLE:
489 case RID_VOID:
490 case RID_DFLOAT32:
491 case RID_DFLOAT64:
492 case RID_DFLOAT128:
493 CASE_RID_FLOATN_NX:
494 case RID_BOOL:
495 case RID_ENUM:
496 case RID_STRUCT:
497 case RID_UNION:
498 case RID_TYPEOF:
499 case RID_CONST:
500 case RID_ATOMIC:
501 case RID_VOLATILE:
502 case RID_RESTRICT:
503 case RID_ATTRIBUTE:
504 case RID_FRACT:
505 case RID_ACCUM:
506 case RID_SAT:
507 case RID_AUTO_TYPE:
508 case RID_ALIGNAS:
509 return true;
510 default:
511 if (keyword >= RID_FIRST_INT_N
512 && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
513 && int_n_enabled_p[keyword - RID_FIRST_INT_N])
514 return true;
515 return false;
516 }
517 }
518
519 /* Return true if TOKEN can start a type name,
520 false otherwise. */
521 bool
522 c_token_starts_typename (c_token *token)
523 {
524 switch (token->type)
525 {
526 case CPP_NAME:
527 switch (token->id_kind)
528 {
529 case C_ID_ID:
530 return false;
531 case C_ID_ADDRSPACE:
532 return true;
533 case C_ID_TYPENAME:
534 return true;
535 case C_ID_CLASSNAME:
536 gcc_assert (c_dialect_objc ());
537 return true;
538 default:
539 gcc_unreachable ();
540 }
541 case CPP_KEYWORD:
542 return c_keyword_starts_typename (token->keyword);
543 case CPP_LESS:
544 if (c_dialect_objc ())
545 return true;
546 return false;
547 default:
548 return false;
549 }
550 }
551
552 /* Return true if the next token from PARSER can start a type name,
553 false otherwise. LA specifies how to do lookahead in order to
554 detect unknown type names. If unsure, pick CLA_PREFER_ID. */
555
556 static inline bool
557 c_parser_next_tokens_start_typename (c_parser *parser, enum c_lookahead_kind la)
558 {
559 c_token *token = c_parser_peek_token (parser);
560 if (c_token_starts_typename (token))
561 return true;
562
563 /* Try a bit harder to detect an unknown typename. */
564 if (la != cla_prefer_id
565 && token->type == CPP_NAME
566 && token->id_kind == C_ID_ID
567
568 /* Do not try too hard when we could have "object in array". */
569 && !parser->objc_could_be_foreach_context
570
571 && (la == cla_prefer_type
572 || c_parser_peek_2nd_token (parser)->type == CPP_NAME
573 || c_parser_peek_2nd_token (parser)->type == CPP_MULT)
574
575 /* Only unknown identifiers. */
576 && !lookup_name (token->value))
577 return true;
578
579 return false;
580 }
581
582 /* Return true if TOKEN is a type qualifier, false otherwise. */
583 static bool
584 c_token_is_qualifier (c_token *token)
585 {
586 switch (token->type)
587 {
588 case CPP_NAME:
589 switch (token->id_kind)
590 {
591 case C_ID_ADDRSPACE:
592 return true;
593 default:
594 return false;
595 }
596 case CPP_KEYWORD:
597 switch (token->keyword)
598 {
599 case RID_CONST:
600 case RID_VOLATILE:
601 case RID_RESTRICT:
602 case RID_ATTRIBUTE:
603 case RID_ATOMIC:
604 return true;
605 default:
606 return false;
607 }
608 case CPP_LESS:
609 return false;
610 default:
611 gcc_unreachable ();
612 }
613 }
614
615 /* Return true if the next token from PARSER is a type qualifier,
616 false otherwise. */
617 static inline bool
618 c_parser_next_token_is_qualifier (c_parser *parser)
619 {
620 c_token *token = c_parser_peek_token (parser);
621 return c_token_is_qualifier (token);
622 }
623
624 /* Return true if TOKEN can start declaration specifiers, false
625 otherwise. */
626 static bool
627 c_token_starts_declspecs (c_token *token)
628 {
629 switch (token->type)
630 {
631 case CPP_NAME:
632 switch (token->id_kind)
633 {
634 case C_ID_ID:
635 return false;
636 case C_ID_ADDRSPACE:
637 return true;
638 case C_ID_TYPENAME:
639 return true;
640 case C_ID_CLASSNAME:
641 gcc_assert (c_dialect_objc ());
642 return true;
643 default:
644 gcc_unreachable ();
645 }
646 case CPP_KEYWORD:
647 switch (token->keyword)
648 {
649 case RID_STATIC:
650 case RID_EXTERN:
651 case RID_REGISTER:
652 case RID_TYPEDEF:
653 case RID_INLINE:
654 case RID_NORETURN:
655 case RID_AUTO:
656 case RID_THREAD:
657 case RID_UNSIGNED:
658 case RID_LONG:
659 case RID_SHORT:
660 case RID_SIGNED:
661 case RID_COMPLEX:
662 case RID_INT:
663 case RID_CHAR:
664 case RID_FLOAT:
665 case RID_DOUBLE:
666 case RID_VOID:
667 case RID_DFLOAT32:
668 case RID_DFLOAT64:
669 case RID_DFLOAT128:
670 CASE_RID_FLOATN_NX:
671 case RID_BOOL:
672 case RID_ENUM:
673 case RID_STRUCT:
674 case RID_UNION:
675 case RID_TYPEOF:
676 case RID_CONST:
677 case RID_VOLATILE:
678 case RID_RESTRICT:
679 case RID_ATTRIBUTE:
680 case RID_FRACT:
681 case RID_ACCUM:
682 case RID_SAT:
683 case RID_ALIGNAS:
684 case RID_ATOMIC:
685 case RID_AUTO_TYPE:
686 return true;
687 default:
688 if (token->keyword >= RID_FIRST_INT_N
689 && token->keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
690 && int_n_enabled_p[token->keyword - RID_FIRST_INT_N])
691 return true;
692 return false;
693 }
694 case CPP_LESS:
695 if (c_dialect_objc ())
696 return true;
697 return false;
698 default:
699 return false;
700 }
701 }
702
703
704 /* Return true if TOKEN can start declaration specifiers or a static
705 assertion, false otherwise. */
706 static bool
707 c_token_starts_declaration (c_token *token)
708 {
709 if (c_token_starts_declspecs (token)
710 || token->keyword == RID_STATIC_ASSERT)
711 return true;
712 else
713 return false;
714 }
715
716 /* Return true if the next token from PARSER can start declaration
717 specifiers, false otherwise. */
718 bool
719 c_parser_next_token_starts_declspecs (c_parser *parser)
720 {
721 c_token *token = c_parser_peek_token (parser);
722
723 /* In Objective-C, a classname normally starts a declspecs unless it
724 is immediately followed by a dot. In that case, it is the
725 Objective-C 2.0 "dot-syntax" for class objects, ie, calls the
726 setter/getter on the class. c_token_starts_declspecs() can't
727 differentiate between the two cases because it only checks the
728 current token, so we have a special check here. */
729 if (c_dialect_objc ()
730 && token->type == CPP_NAME
731 && token->id_kind == C_ID_CLASSNAME
732 && c_parser_peek_2nd_token (parser)->type == CPP_DOT)
733 return false;
734
735 return c_token_starts_declspecs (token);
736 }
737
738 /* Return true if the next tokens from PARSER can start declaration
739 specifiers or a static assertion, false otherwise. */
740 bool
741 c_parser_next_tokens_start_declaration (c_parser *parser)
742 {
743 c_token *token = c_parser_peek_token (parser);
744
745 /* Same as above. */
746 if (c_dialect_objc ()
747 && token->type == CPP_NAME
748 && token->id_kind == C_ID_CLASSNAME
749 && c_parser_peek_2nd_token (parser)->type == CPP_DOT)
750 return false;
751
752 /* Labels do not start declarations. */
753 if (token->type == CPP_NAME
754 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
755 return false;
756
757 if (c_token_starts_declaration (token))
758 return true;
759
760 if (c_parser_next_tokens_start_typename (parser, cla_nonabstract_decl))
761 return true;
762
763 return false;
764 }
765
766 /* Consume the next token from PARSER. */
767
768 void
769 c_parser_consume_token (c_parser *parser)
770 {
771 gcc_assert (parser->tokens_avail >= 1);
772 gcc_assert (parser->tokens[0].type != CPP_EOF);
773 gcc_assert (!parser->in_pragma || parser->tokens[0].type != CPP_PRAGMA_EOL);
774 gcc_assert (parser->error || parser->tokens[0].type != CPP_PRAGMA);
775 parser->last_token_location = parser->tokens[0].location;
776 if (parser->tokens != &parser->tokens_buf[0])
777 parser->tokens++;
778 else if (parser->tokens_avail == 2)
779 parser->tokens[0] = parser->tokens[1];
780 parser->tokens_avail--;
781 }
782
783 /* Expect the current token to be a #pragma. Consume it and remember
784 that we've begun parsing a pragma. */
785
786 static void
787 c_parser_consume_pragma (c_parser *parser)
788 {
789 gcc_assert (!parser->in_pragma);
790 gcc_assert (parser->tokens_avail >= 1);
791 gcc_assert (parser->tokens[0].type == CPP_PRAGMA);
792 if (parser->tokens != &parser->tokens_buf[0])
793 parser->tokens++;
794 else if (parser->tokens_avail == 2)
795 parser->tokens[0] = parser->tokens[1];
796 parser->tokens_avail--;
797 parser->in_pragma = true;
798 }
799
800 /* Update the global input_location from TOKEN. */
801 static inline void
802 c_parser_set_source_position_from_token (c_token *token)
803 {
804 if (token->type != CPP_EOF)
805 {
806 input_location = token->location;
807 }
808 }
809
810 /* Helper function for c_parser_error.
811 Having peeked a token of kind TOK1_KIND that might signify
812 a conflict marker, peek successor tokens to determine
813 if we actually do have a conflict marker.
814 Specifically, we consider a run of 7 '<', '=' or '>' characters
815 at the start of a line as a conflict marker.
816 These come through the lexer as three pairs and a single,
817 e.g. three CPP_LSHIFT ("<<") and a CPP_LESS ('<').
818 If it returns true, *OUT_LOC is written to with the location/range
819 of the marker. */
820
821 static bool
822 c_parser_peek_conflict_marker (c_parser *parser, enum cpp_ttype tok1_kind,
823 location_t *out_loc)
824 {
825 c_token *token2 = c_parser_peek_2nd_token (parser);
826 if (token2->type != tok1_kind)
827 return false;
828 c_token *token3 = c_parser_peek_nth_token (parser, 3);
829 if (token3->type != tok1_kind)
830 return false;
831 c_token *token4 = c_parser_peek_nth_token (parser, 4);
832 if (token4->type != conflict_marker_get_final_tok_kind (tok1_kind))
833 return false;
834
835 /* It must be at the start of the line. */
836 location_t start_loc = c_parser_peek_token (parser)->location;
837 if (LOCATION_COLUMN (start_loc) != 1)
838 return false;
839
840 /* We have a conflict marker. Construct a location of the form:
841 <<<<<<<
842 ^~~~~~~
843 with start == caret, finishing at the end of the marker. */
844 location_t finish_loc = get_finish (token4->location);
845 *out_loc = make_location (start_loc, start_loc, finish_loc);
846
847 return true;
848 }
849
850 /* Issue a diagnostic of the form
851 FILE:LINE: MESSAGE before TOKEN
852 where TOKEN is the next token in the input stream of PARSER.
853 MESSAGE (specified by the caller) is usually of the form "expected
854 OTHER-TOKEN".
855
856 Use RICHLOC as the location of the diagnostic.
857
858 Do not issue a diagnostic if still recovering from an error.
859
860 Return true iff an error was actually emitted.
861
862 ??? This is taken from the C++ parser, but building up messages in
863 this way is not i18n-friendly and some other approach should be
864 used. */
865
866 static bool
867 c_parser_error_richloc (c_parser *parser, const char *gmsgid,
868 rich_location *richloc)
869 {
870 c_token *token = c_parser_peek_token (parser);
871 if (parser->error)
872 return false;
873 parser->error = true;
874 if (!gmsgid)
875 return false;
876
877 /* If this is actually a conflict marker, report it as such. */
878 if (token->type == CPP_LSHIFT
879 || token->type == CPP_RSHIFT
880 || token->type == CPP_EQ_EQ)
881 {
882 location_t loc;
883 if (c_parser_peek_conflict_marker (parser, token->type, &loc))
884 {
885 error_at (loc, "version control conflict marker in file");
886 return true;
887 }
888 }
889
890 c_parse_error (gmsgid,
891 /* Because c_parse_error does not understand
892 CPP_KEYWORD, keywords are treated like
893 identifiers. */
894 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
895 /* ??? The C parser does not save the cpp flags of a
896 token, we need to pass 0 here and we will not get
897 the source spelling of some tokens but rather the
898 canonical spelling. */
899 token->value, /*flags=*/0, richloc);
900 return true;
901 }
902
903 /* As c_parser_error_richloc, but issue the message at the
904 location of PARSER's next token, or at input_location
905 if the next token is EOF. */
906
907 bool
908 c_parser_error (c_parser *parser, const char *gmsgid)
909 {
910 c_token *token = c_parser_peek_token (parser);
911 c_parser_set_source_position_from_token (token);
912 rich_location richloc (line_table, input_location);
913 return c_parser_error_richloc (parser, gmsgid, &richloc);
914 }
915
916 /* Some tokens naturally come in pairs e.g.'(' and ')'.
917 This class is for tracking such a matching pair of symbols.
918 In particular, it tracks the location of the first token,
919 so that if the second token is missing, we can highlight the
920 location of the first token when notifying the user about the
921 problem. */
922
923 template <typename traits_t>
924 class token_pair
925 {
926 public:
927 /* token_pair's ctor. */
928 token_pair () : m_open_loc (UNKNOWN_LOCATION) {}
929
930 /* If the next token is the opening symbol for this pair, consume it and
931 return true.
932 Otherwise, issue an error and return false.
933 In either case, record the location of the opening token. */
934
935 bool require_open (c_parser *parser)
936 {
937 c_token *token = c_parser_peek_token (parser);
938 if (token)
939 m_open_loc = token->location;
940
941 return c_parser_require (parser, traits_t::open_token_type,
942 traits_t::open_gmsgid);
943 }
944
945 /* Consume the next token from PARSER, recording its location as
946 that of the opening token within the pair. */
947
948 void consume_open (c_parser *parser)
949 {
950 c_token *token = c_parser_peek_token (parser);
951 gcc_assert (token->type == traits_t::open_token_type);
952 m_open_loc = token->location;
953 c_parser_consume_token (parser);
954 }
955
956 /* If the next token is the closing symbol for this pair, consume it
957 and return true.
958 Otherwise, issue an error, highlighting the location of the
959 corresponding opening token, and return false. */
960
961 bool require_close (c_parser *parser) const
962 {
963 return c_parser_require (parser, traits_t::close_token_type,
964 traits_t::close_gmsgid, m_open_loc);
965 }
966
967 /* Like token_pair::require_close, except that tokens will be skipped
968 until the desired token is found. An error message is still produced
969 if the next token is not as expected. */
970
971 void skip_until_found_close (c_parser *parser) const
972 {
973 c_parser_skip_until_found (parser, traits_t::close_token_type,
974 traits_t::close_gmsgid, m_open_loc);
975 }
976
977 private:
978 location_t m_open_loc;
979 };
980
981 /* Traits for token_pair<T> for tracking matching pairs of parentheses. */
982
983 struct matching_paren_traits
984 {
985 static const enum cpp_ttype open_token_type = CPP_OPEN_PAREN;
986 static const char * const open_gmsgid;
987 static const enum cpp_ttype close_token_type = CPP_CLOSE_PAREN;
988 static const char * const close_gmsgid;
989 };
990
991 const char * const matching_paren_traits::open_gmsgid = "expected %<(%>";
992 const char * const matching_paren_traits::close_gmsgid = "expected %<)%>";
993
994 /* "matching_parens" is a token_pair<T> class for tracking matching
995 pairs of parentheses. */
996
997 typedef token_pair<matching_paren_traits> matching_parens;
998
999 /* Traits for token_pair<T> for tracking matching pairs of braces. */
1000
1001 struct matching_brace_traits
1002 {
1003 static const enum cpp_ttype open_token_type = CPP_OPEN_BRACE;
1004 static const char * const open_gmsgid;
1005 static const enum cpp_ttype close_token_type = CPP_CLOSE_BRACE;
1006 static const char * const close_gmsgid;
1007 };
1008
1009 const char * const matching_brace_traits::open_gmsgid = "expected %<{%>";
1010 const char * const matching_brace_traits::close_gmsgid = "expected %<}%>";
1011
1012 /* "matching_braces" is a token_pair<T> class for tracking matching
1013 pairs of braces. */
1014
1015 typedef token_pair<matching_brace_traits> matching_braces;
1016
1017 /* Get a description of the matching symbol to TYPE e.g. "(" for
1018 CPP_CLOSE_PAREN. */
1019
1020 static const char *
1021 get_matching_symbol (enum cpp_ttype type)
1022 {
1023 switch (type)
1024 {
1025 default:
1026 gcc_unreachable ();
1027 return "";
1028 case CPP_CLOSE_PAREN:
1029 return "(";
1030 case CPP_CLOSE_BRACE:
1031 return "{";
1032 }
1033 }
1034
1035 /* If the next token is of the indicated TYPE, consume it. Otherwise,
1036 issue the error MSGID. If MSGID is NULL then a message has already
1037 been produced and no message will be produced this time. Returns
1038 true if found, false otherwise.
1039
1040 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
1041 within any error as the location of an "opening" token matching
1042 the close token TYPE (e.g. the location of the '(' when TYPE is
1043 CPP_CLOSE_PAREN).
1044
1045 If TYPE_IS_UNIQUE is true (the default) then msgid describes exactly
1046 one type (e.g. "expected %<)%>") and thus it may be reasonable to
1047 attempt to generate a fix-it hint for the problem.
1048 Otherwise msgid describes multiple token types (e.g.
1049 "expected %<;%>, %<,%> or %<)%>"), and thus we shouldn't attempt to
1050 generate a fix-it hint. */
1051
1052 bool
1053 c_parser_require (c_parser *parser,
1054 enum cpp_ttype type,
1055 const char *msgid,
1056 location_t matching_location,
1057 bool type_is_unique)
1058 {
1059 if (c_parser_next_token_is (parser, type))
1060 {
1061 c_parser_consume_token (parser);
1062 return true;
1063 }
1064 else
1065 {
1066 location_t next_token_loc = c_parser_peek_token (parser)->location;
1067 gcc_rich_location richloc (next_token_loc);
1068
1069 /* Potentially supply a fix-it hint, suggesting to add the
1070 missing token immediately after the *previous* token.
1071 This may move the primary location within richloc. */
1072 if (!parser->error && type_is_unique)
1073 maybe_suggest_missing_token_insertion (&richloc, type,
1074 parser->last_token_location);
1075
1076 /* If matching_location != UNKNOWN_LOCATION, highlight it.
1077 Attempt to consolidate diagnostics by printing it as a
1078 secondary range within the main diagnostic. */
1079 bool added_matching_location = false;
1080 if (matching_location != UNKNOWN_LOCATION)
1081 added_matching_location
1082 = richloc.add_location_if_nearby (matching_location);
1083
1084 if (c_parser_error_richloc (parser, msgid, &richloc))
1085 /* If we weren't able to consolidate matching_location, then
1086 print it as a secondary diagnostic. */
1087 if (matching_location != UNKNOWN_LOCATION && !added_matching_location)
1088 inform (matching_location, "to match this %qs",
1089 get_matching_symbol (type));
1090
1091 return false;
1092 }
1093 }
1094
1095 /* If the next token is the indicated keyword, consume it. Otherwise,
1096 issue the error MSGID. Returns true if found, false otherwise. */
1097
1098 static bool
1099 c_parser_require_keyword (c_parser *parser,
1100 enum rid keyword,
1101 const char *msgid)
1102 {
1103 if (c_parser_next_token_is_keyword (parser, keyword))
1104 {
1105 c_parser_consume_token (parser);
1106 return true;
1107 }
1108 else
1109 {
1110 c_parser_error (parser, msgid);
1111 return false;
1112 }
1113 }
1114
1115 /* Like c_parser_require, except that tokens will be skipped until the
1116 desired token is found. An error message is still produced if the
1117 next token is not as expected. If MSGID is NULL then a message has
1118 already been produced and no message will be produced this
1119 time.
1120
1121 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
1122 within any error as the location of an "opening" token matching
1123 the close token TYPE (e.g. the location of the '(' when TYPE is
1124 CPP_CLOSE_PAREN). */
1125
1126 void
1127 c_parser_skip_until_found (c_parser *parser,
1128 enum cpp_ttype type,
1129 const char *msgid,
1130 location_t matching_location)
1131 {
1132 unsigned nesting_depth = 0;
1133
1134 if (c_parser_require (parser, type, msgid, matching_location))
1135 return;
1136
1137 /* Skip tokens until the desired token is found. */
1138 while (true)
1139 {
1140 /* Peek at the next token. */
1141 c_token *token = c_parser_peek_token (parser);
1142 /* If we've reached the token we want, consume it and stop. */
1143 if (token->type == type && !nesting_depth)
1144 {
1145 c_parser_consume_token (parser);
1146 break;
1147 }
1148
1149 /* If we've run out of tokens, stop. */
1150 if (token->type == CPP_EOF)
1151 return;
1152 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
1153 return;
1154 if (token->type == CPP_OPEN_BRACE
1155 || token->type == CPP_OPEN_PAREN
1156 || token->type == CPP_OPEN_SQUARE)
1157 ++nesting_depth;
1158 else if (token->type == CPP_CLOSE_BRACE
1159 || token->type == CPP_CLOSE_PAREN
1160 || token->type == CPP_CLOSE_SQUARE)
1161 {
1162 if (nesting_depth-- == 0)
1163 break;
1164 }
1165 /* Consume this token. */
1166 c_parser_consume_token (parser);
1167 }
1168 parser->error = false;
1169 }
1170
1171 /* Skip tokens until the end of a parameter is found, but do not
1172 consume the comma, semicolon or closing delimiter. */
1173
1174 static void
1175 c_parser_skip_to_end_of_parameter (c_parser *parser)
1176 {
1177 unsigned nesting_depth = 0;
1178
1179 while (true)
1180 {
1181 c_token *token = c_parser_peek_token (parser);
1182 if ((token->type == CPP_COMMA || token->type == CPP_SEMICOLON)
1183 && !nesting_depth)
1184 break;
1185 /* If we've run out of tokens, stop. */
1186 if (token->type == CPP_EOF)
1187 return;
1188 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
1189 return;
1190 if (token->type == CPP_OPEN_BRACE
1191 || token->type == CPP_OPEN_PAREN
1192 || token->type == CPP_OPEN_SQUARE)
1193 ++nesting_depth;
1194 else if (token->type == CPP_CLOSE_BRACE
1195 || token->type == CPP_CLOSE_PAREN
1196 || token->type == CPP_CLOSE_SQUARE)
1197 {
1198 if (nesting_depth-- == 0)
1199 break;
1200 }
1201 /* Consume this token. */
1202 c_parser_consume_token (parser);
1203 }
1204 parser->error = false;
1205 }
1206
1207 /* Expect to be at the end of the pragma directive and consume an
1208 end of line marker. */
1209
1210 static void
1211 c_parser_skip_to_pragma_eol (c_parser *parser, bool error_if_not_eol = true)
1212 {
1213 gcc_assert (parser->in_pragma);
1214 parser->in_pragma = false;
1215
1216 if (error_if_not_eol && c_parser_peek_token (parser)->type != CPP_PRAGMA_EOL)
1217 c_parser_error (parser, "expected end of line");
1218
1219 cpp_ttype token_type;
1220 do
1221 {
1222 c_token *token = c_parser_peek_token (parser);
1223 token_type = token->type;
1224 if (token_type == CPP_EOF)
1225 break;
1226 c_parser_consume_token (parser);
1227 }
1228 while (token_type != CPP_PRAGMA_EOL);
1229
1230 parser->error = false;
1231 }
1232
1233 /* Skip tokens until we have consumed an entire block, or until we
1234 have consumed a non-nested ';'. */
1235
1236 static void
1237 c_parser_skip_to_end_of_block_or_statement (c_parser *parser)
1238 {
1239 unsigned nesting_depth = 0;
1240 bool save_error = parser->error;
1241
1242 while (true)
1243 {
1244 c_token *token;
1245
1246 /* Peek at the next token. */
1247 token = c_parser_peek_token (parser);
1248
1249 switch (token->type)
1250 {
1251 case CPP_EOF:
1252 return;
1253
1254 case CPP_PRAGMA_EOL:
1255 if (parser->in_pragma)
1256 return;
1257 break;
1258
1259 case CPP_SEMICOLON:
1260 /* If the next token is a ';', we have reached the
1261 end of the statement. */
1262 if (!nesting_depth)
1263 {
1264 /* Consume the ';'. */
1265 c_parser_consume_token (parser);
1266 goto finished;
1267 }
1268 break;
1269
1270 case CPP_CLOSE_BRACE:
1271 /* If the next token is a non-nested '}', then we have
1272 reached the end of the current block. */
1273 if (nesting_depth == 0 || --nesting_depth == 0)
1274 {
1275 c_parser_consume_token (parser);
1276 goto finished;
1277 }
1278 break;
1279
1280 case CPP_OPEN_BRACE:
1281 /* If it the next token is a '{', then we are entering a new
1282 block. Consume the entire block. */
1283 ++nesting_depth;
1284 break;
1285
1286 case CPP_PRAGMA:
1287 /* If we see a pragma, consume the whole thing at once. We
1288 have some safeguards against consuming pragmas willy-nilly.
1289 Normally, we'd expect to be here with parser->error set,
1290 which disables these safeguards. But it's possible to get
1291 here for secondary error recovery, after parser->error has
1292 been cleared. */
1293 c_parser_consume_pragma (parser);
1294 c_parser_skip_to_pragma_eol (parser);
1295 parser->error = save_error;
1296 continue;
1297
1298 default:
1299 break;
1300 }
1301
1302 c_parser_consume_token (parser);
1303 }
1304
1305 finished:
1306 parser->error = false;
1307 }
1308
1309 /* CPP's options (initialized by c-opts.c). */
1310 extern cpp_options *cpp_opts;
1311
1312 /* Save the warning flags which are controlled by __extension__. */
1313
1314 static inline int
1315 disable_extension_diagnostics (void)
1316 {
1317 int ret = (pedantic
1318 | (warn_pointer_arith << 1)
1319 | (warn_traditional << 2)
1320 | (flag_iso << 3)
1321 | (warn_long_long << 4)
1322 | (warn_cxx_compat << 5)
1323 | (warn_overlength_strings << 6)
1324 /* warn_c90_c99_compat has three states: -1/0/1, so we must
1325 play tricks to properly restore it. */
1326 | ((warn_c90_c99_compat == 1) << 7)
1327 | ((warn_c90_c99_compat == -1) << 8)
1328 /* Similarly for warn_c99_c11_compat. */
1329 | ((warn_c99_c11_compat == 1) << 9)
1330 | ((warn_c99_c11_compat == -1) << 10)
1331 /* Similarly for warn_c11_c2x_compat. */
1332 | ((warn_c11_c2x_compat == 1) << 11)
1333 | ((warn_c11_c2x_compat == -1) << 12)
1334 );
1335 cpp_opts->cpp_pedantic = pedantic = 0;
1336 warn_pointer_arith = 0;
1337 cpp_opts->cpp_warn_traditional = warn_traditional = 0;
1338 flag_iso = 0;
1339 cpp_opts->cpp_warn_long_long = warn_long_long = 0;
1340 warn_cxx_compat = 0;
1341 warn_overlength_strings = 0;
1342 warn_c90_c99_compat = 0;
1343 warn_c99_c11_compat = 0;
1344 warn_c11_c2x_compat = 0;
1345 return ret;
1346 }
1347
1348 /* Restore the warning flags which are controlled by __extension__.
1349 FLAGS is the return value from disable_extension_diagnostics. */
1350
1351 static inline void
1352 restore_extension_diagnostics (int flags)
1353 {
1354 cpp_opts->cpp_pedantic = pedantic = flags & 1;
1355 warn_pointer_arith = (flags >> 1) & 1;
1356 cpp_opts->cpp_warn_traditional = warn_traditional = (flags >> 2) & 1;
1357 flag_iso = (flags >> 3) & 1;
1358 cpp_opts->cpp_warn_long_long = warn_long_long = (flags >> 4) & 1;
1359 warn_cxx_compat = (flags >> 5) & 1;
1360 warn_overlength_strings = (flags >> 6) & 1;
1361 /* See above for why is this needed. */
1362 warn_c90_c99_compat = (flags >> 7) & 1 ? 1 : ((flags >> 8) & 1 ? -1 : 0);
1363 warn_c99_c11_compat = (flags >> 9) & 1 ? 1 : ((flags >> 10) & 1 ? -1 : 0);
1364 warn_c11_c2x_compat = (flags >> 11) & 1 ? 1 : ((flags >> 12) & 1 ? -1 : 0);
1365 }
1366
1367 /* Helper data structure for parsing #pragma acc routine. */
1368 struct oacc_routine_data {
1369 bool error_seen; /* Set if error has been reported. */
1370 bool fndecl_seen; /* Set if one fn decl/definition has been seen already. */
1371 tree clauses;
1372 location_t loc;
1373 };
1374
1375 static void c_parser_external_declaration (c_parser *);
1376 static void c_parser_asm_definition (c_parser *);
1377 static void c_parser_declaration_or_fndef (c_parser *, bool, bool, bool,
1378 bool, bool, tree *, vec<c_token>,
1379 struct oacc_routine_data * = NULL,
1380 bool * = NULL);
1381 static void c_parser_static_assert_declaration_no_semi (c_parser *);
1382 static void c_parser_static_assert_declaration (c_parser *);
1383 static struct c_typespec c_parser_enum_specifier (c_parser *);
1384 static struct c_typespec c_parser_struct_or_union_specifier (c_parser *);
1385 static tree c_parser_struct_declaration (c_parser *);
1386 static struct c_typespec c_parser_typeof_specifier (c_parser *);
1387 static tree c_parser_alignas_specifier (c_parser *);
1388 static struct c_declarator *c_parser_direct_declarator (c_parser *, bool,
1389 c_dtr_syn, bool *);
1390 static struct c_declarator *c_parser_direct_declarator_inner (c_parser *,
1391 bool,
1392 struct c_declarator *);
1393 static struct c_arg_info *c_parser_parms_declarator (c_parser *, bool, tree);
1394 static struct c_arg_info *c_parser_parms_list_declarator (c_parser *, tree,
1395 tree);
1396 static struct c_parm *c_parser_parameter_declaration (c_parser *, tree);
1397 static tree c_parser_simple_asm_expr (c_parser *);
1398 static tree c_parser_attributes (c_parser *);
1399 static struct c_expr c_parser_initializer (c_parser *);
1400 static struct c_expr c_parser_braced_init (c_parser *, tree, bool,
1401 struct obstack *);
1402 static void c_parser_initelt (c_parser *, struct obstack *);
1403 static void c_parser_initval (c_parser *, struct c_expr *,
1404 struct obstack *);
1405 static tree c_parser_compound_statement (c_parser *);
1406 static void c_parser_compound_statement_nostart (c_parser *);
1407 static void c_parser_label (c_parser *);
1408 static void c_parser_statement (c_parser *, bool *, location_t * = NULL);
1409 static void c_parser_statement_after_labels (c_parser *, bool *,
1410 vec<tree> * = NULL);
1411 static tree c_parser_c99_block_statement (c_parser *, bool *,
1412 location_t * = NULL);
1413 static void c_parser_if_statement (c_parser *, bool *, vec<tree> *);
1414 static void c_parser_switch_statement (c_parser *, bool *);
1415 static void c_parser_while_statement (c_parser *, bool, unsigned short, bool *);
1416 static void c_parser_do_statement (c_parser *, bool, unsigned short);
1417 static void c_parser_for_statement (c_parser *, bool, unsigned short, bool *);
1418 static tree c_parser_asm_statement (c_parser *);
1419 static tree c_parser_asm_operands (c_parser *);
1420 static tree c_parser_asm_goto_operands (c_parser *);
1421 static tree c_parser_asm_clobbers (c_parser *);
1422 static struct c_expr c_parser_expr_no_commas (c_parser *, struct c_expr *,
1423 tree = NULL_TREE);
1424 static struct c_expr c_parser_conditional_expression (c_parser *,
1425 struct c_expr *, tree);
1426 static struct c_expr c_parser_binary_expression (c_parser *, struct c_expr *,
1427 tree);
1428 static struct c_expr c_parser_cast_expression (c_parser *, struct c_expr *);
1429 static struct c_expr c_parser_unary_expression (c_parser *);
1430 static struct c_expr c_parser_sizeof_expression (c_parser *);
1431 static struct c_expr c_parser_alignof_expression (c_parser *);
1432 static struct c_expr c_parser_postfix_expression (c_parser *);
1433 static struct c_expr c_parser_postfix_expression_after_paren_type (c_parser *,
1434 struct c_type_name *,
1435 location_t);
1436 static struct c_expr c_parser_postfix_expression_after_primary (c_parser *,
1437 location_t loc,
1438 struct c_expr);
1439 static tree c_parser_transaction (c_parser *, enum rid);
1440 static struct c_expr c_parser_transaction_expression (c_parser *, enum rid);
1441 static tree c_parser_transaction_cancel (c_parser *);
1442 static struct c_expr c_parser_expression (c_parser *);
1443 static struct c_expr c_parser_expression_conv (c_parser *);
1444 static vec<tree, va_gc> *c_parser_expr_list (c_parser *, bool, bool,
1445 vec<tree, va_gc> **, location_t *,
1446 tree *, vec<location_t> *,
1447 unsigned int * = NULL);
1448 static struct c_expr c_parser_has_attribute_expression (c_parser *);
1449
1450 static void c_parser_oacc_declare (c_parser *);
1451 static void c_parser_oacc_enter_exit_data (c_parser *, bool);
1452 static void c_parser_oacc_update (c_parser *);
1453 static void c_parser_omp_construct (c_parser *, bool *);
1454 static void c_parser_omp_threadprivate (c_parser *);
1455 static void c_parser_omp_barrier (c_parser *);
1456 static void c_parser_omp_depobj (c_parser *);
1457 static void c_parser_omp_flush (c_parser *);
1458 static tree c_parser_omp_for_loop (location_t, c_parser *, enum tree_code,
1459 tree, tree *, bool *);
1460 static void c_parser_omp_taskwait (c_parser *);
1461 static void c_parser_omp_taskyield (c_parser *);
1462 static void c_parser_omp_cancel (c_parser *);
1463
1464 enum pragma_context { pragma_external, pragma_struct, pragma_param,
1465 pragma_stmt, pragma_compound };
1466 static bool c_parser_pragma (c_parser *, enum pragma_context, bool *);
1467 static void c_parser_omp_cancellation_point (c_parser *, enum pragma_context);
1468 static bool c_parser_omp_target (c_parser *, enum pragma_context, bool *);
1469 static void c_parser_omp_end_declare_target (c_parser *);
1470 static void c_parser_omp_declare (c_parser *, enum pragma_context);
1471 static void c_parser_omp_requires (c_parser *);
1472 static bool c_parser_omp_ordered (c_parser *, enum pragma_context, bool *);
1473 static void c_parser_oacc_routine (c_parser *, enum pragma_context);
1474
1475 /* These Objective-C parser functions are only ever called when
1476 compiling Objective-C. */
1477 static void c_parser_objc_class_definition (c_parser *, tree);
1478 static void c_parser_objc_class_instance_variables (c_parser *);
1479 static void c_parser_objc_class_declaration (c_parser *);
1480 static void c_parser_objc_alias_declaration (c_parser *);
1481 static void c_parser_objc_protocol_definition (c_parser *, tree);
1482 static bool c_parser_objc_method_type (c_parser *);
1483 static void c_parser_objc_method_definition (c_parser *);
1484 static void c_parser_objc_methodprotolist (c_parser *);
1485 static void c_parser_objc_methodproto (c_parser *);
1486 static tree c_parser_objc_method_decl (c_parser *, bool, tree *, tree *);
1487 static tree c_parser_objc_type_name (c_parser *);
1488 static tree c_parser_objc_protocol_refs (c_parser *);
1489 static void c_parser_objc_try_catch_finally_statement (c_parser *);
1490 static void c_parser_objc_synchronized_statement (c_parser *);
1491 static tree c_parser_objc_selector (c_parser *);
1492 static tree c_parser_objc_selector_arg (c_parser *);
1493 static tree c_parser_objc_receiver (c_parser *);
1494 static tree c_parser_objc_message_args (c_parser *);
1495 static tree c_parser_objc_keywordexpr (c_parser *);
1496 static void c_parser_objc_at_property_declaration (c_parser *);
1497 static void c_parser_objc_at_synthesize_declaration (c_parser *);
1498 static void c_parser_objc_at_dynamic_declaration (c_parser *);
1499 static bool c_parser_objc_diagnose_bad_element_prefix
1500 (c_parser *, struct c_declspecs *);
1501
1502 static void c_parser_parse_rtl_body (c_parser *parser, char *start_with_pass);
1503
1504 /* Parse a translation unit (C90 6.7, C99 6.9, C11 6.9).
1505
1506 translation-unit:
1507 external-declarations
1508
1509 external-declarations:
1510 external-declaration
1511 external-declarations external-declaration
1512
1513 GNU extensions:
1514
1515 translation-unit:
1516 empty
1517 */
1518
1519 static void
1520 c_parser_translation_unit (c_parser *parser)
1521 {
1522 if (c_parser_next_token_is (parser, CPP_EOF))
1523 {
1524 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
1525 "ISO C forbids an empty translation unit");
1526 }
1527 else
1528 {
1529 void *obstack_position = obstack_alloc (&parser_obstack, 0);
1530 mark_valid_location_for_stdc_pragma (false);
1531 do
1532 {
1533 ggc_collect ();
1534 c_parser_external_declaration (parser);
1535 obstack_free (&parser_obstack, obstack_position);
1536 }
1537 while (c_parser_next_token_is_not (parser, CPP_EOF));
1538 }
1539
1540 unsigned int i;
1541 tree decl;
1542 FOR_EACH_VEC_ELT (incomplete_record_decls, i, decl)
1543 if (DECL_SIZE (decl) == NULL_TREE && TREE_TYPE (decl) != error_mark_node)
1544 error ("storage size of %q+D isn%'t known", decl);
1545 }
1546
1547 /* Parse an external declaration (C90 6.7, C99 6.9, C11 6.9).
1548
1549 external-declaration:
1550 function-definition
1551 declaration
1552
1553 GNU extensions:
1554
1555 external-declaration:
1556 asm-definition
1557 ;
1558 __extension__ external-declaration
1559
1560 Objective-C:
1561
1562 external-declaration:
1563 objc-class-definition
1564 objc-class-declaration
1565 objc-alias-declaration
1566 objc-protocol-definition
1567 objc-method-definition
1568 @end
1569 */
1570
1571 static void
1572 c_parser_external_declaration (c_parser *parser)
1573 {
1574 int ext;
1575 switch (c_parser_peek_token (parser)->type)
1576 {
1577 case CPP_KEYWORD:
1578 switch (c_parser_peek_token (parser)->keyword)
1579 {
1580 case RID_EXTENSION:
1581 ext = disable_extension_diagnostics ();
1582 c_parser_consume_token (parser);
1583 c_parser_external_declaration (parser);
1584 restore_extension_diagnostics (ext);
1585 break;
1586 case RID_ASM:
1587 c_parser_asm_definition (parser);
1588 break;
1589 case RID_AT_INTERFACE:
1590 case RID_AT_IMPLEMENTATION:
1591 gcc_assert (c_dialect_objc ());
1592 c_parser_objc_class_definition (parser, NULL_TREE);
1593 break;
1594 case RID_AT_CLASS:
1595 gcc_assert (c_dialect_objc ());
1596 c_parser_objc_class_declaration (parser);
1597 break;
1598 case RID_AT_ALIAS:
1599 gcc_assert (c_dialect_objc ());
1600 c_parser_objc_alias_declaration (parser);
1601 break;
1602 case RID_AT_PROTOCOL:
1603 gcc_assert (c_dialect_objc ());
1604 c_parser_objc_protocol_definition (parser, NULL_TREE);
1605 break;
1606 case RID_AT_PROPERTY:
1607 gcc_assert (c_dialect_objc ());
1608 c_parser_objc_at_property_declaration (parser);
1609 break;
1610 case RID_AT_SYNTHESIZE:
1611 gcc_assert (c_dialect_objc ());
1612 c_parser_objc_at_synthesize_declaration (parser);
1613 break;
1614 case RID_AT_DYNAMIC:
1615 gcc_assert (c_dialect_objc ());
1616 c_parser_objc_at_dynamic_declaration (parser);
1617 break;
1618 case RID_AT_END:
1619 gcc_assert (c_dialect_objc ());
1620 c_parser_consume_token (parser);
1621 objc_finish_implementation ();
1622 break;
1623 default:
1624 goto decl_or_fndef;
1625 }
1626 break;
1627 case CPP_SEMICOLON:
1628 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
1629 "ISO C does not allow extra %<;%> outside of a function");
1630 c_parser_consume_token (parser);
1631 break;
1632 case CPP_PRAGMA:
1633 mark_valid_location_for_stdc_pragma (true);
1634 c_parser_pragma (parser, pragma_external, NULL);
1635 mark_valid_location_for_stdc_pragma (false);
1636 break;
1637 case CPP_PLUS:
1638 case CPP_MINUS:
1639 if (c_dialect_objc ())
1640 {
1641 c_parser_objc_method_definition (parser);
1642 break;
1643 }
1644 /* Else fall through, and yield a syntax error trying to parse
1645 as a declaration or function definition. */
1646 /* FALLTHRU */
1647 default:
1648 decl_or_fndef:
1649 /* A declaration or a function definition (or, in Objective-C,
1650 an @interface or @protocol with prefix attributes). We can
1651 only tell which after parsing the declaration specifiers, if
1652 any, and the first declarator. */
1653 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
1654 NULL, vNULL);
1655 break;
1656 }
1657 }
1658
1659 static void c_finish_omp_declare_simd (c_parser *, tree, tree, vec<c_token>);
1660 static void c_finish_oacc_routine (struct oacc_routine_data *, tree, bool);
1661
1662 /* Build and add a DEBUG_BEGIN_STMT statement with location LOC. */
1663
1664 static void
1665 add_debug_begin_stmt (location_t loc)
1666 {
1667 /* Don't add DEBUG_BEGIN_STMTs outside of functions, see PR84721. */
1668 if (!MAY_HAVE_DEBUG_MARKER_STMTS || !building_stmt_list_p ())
1669 return;
1670
1671 tree stmt = build0 (DEBUG_BEGIN_STMT, void_type_node);
1672 SET_EXPR_LOCATION (stmt, loc);
1673 add_stmt (stmt);
1674 }
1675
1676 /* Parse a declaration or function definition (C90 6.5, 6.7.1, C99
1677 6.7, 6.9.1, C11 6.7, 6.9.1). If FNDEF_OK is true, a function definition
1678 is accepted; otherwise (old-style parameter declarations) only other
1679 declarations are accepted. If STATIC_ASSERT_OK is true, a static
1680 assertion is accepted; otherwise (old-style parameter declarations)
1681 it is not. If NESTED is true, we are inside a function or parsing
1682 old-style parameter declarations; any functions encountered are
1683 nested functions and declaration specifiers are required; otherwise
1684 we are at top level and functions are normal functions and
1685 declaration specifiers may be optional. If EMPTY_OK is true, empty
1686 declarations are OK (subject to all other constraints); otherwise
1687 (old-style parameter declarations) they are diagnosed. If
1688 START_ATTR_OK is true, the declaration specifiers may start with
1689 attributes; otherwise they may not.
1690 OBJC_FOREACH_OBJECT_DECLARATION can be used to get back the parsed
1691 declaration when parsing an Objective-C foreach statement.
1692 FALLTHRU_ATTR_P is used to signal whether this function parsed
1693 "__attribute__((fallthrough));".
1694
1695 declaration:
1696 declaration-specifiers init-declarator-list[opt] ;
1697 static_assert-declaration
1698
1699 function-definition:
1700 declaration-specifiers[opt] declarator declaration-list[opt]
1701 compound-statement
1702
1703 declaration-list:
1704 declaration
1705 declaration-list declaration
1706
1707 init-declarator-list:
1708 init-declarator
1709 init-declarator-list , init-declarator
1710
1711 init-declarator:
1712 declarator simple-asm-expr[opt] attributes[opt]
1713 declarator simple-asm-expr[opt] attributes[opt] = initializer
1714
1715 GNU extensions:
1716
1717 nested-function-definition:
1718 declaration-specifiers declarator declaration-list[opt]
1719 compound-statement
1720
1721 attribute ;
1722
1723 Objective-C:
1724 attributes objc-class-definition
1725 attributes objc-category-definition
1726 attributes objc-protocol-definition
1727
1728 The simple-asm-expr and attributes are GNU extensions.
1729
1730 This function does not handle __extension__; that is handled in its
1731 callers. ??? Following the old parser, __extension__ may start
1732 external declarations, declarations in functions and declarations
1733 at the start of "for" loops, but not old-style parameter
1734 declarations.
1735
1736 C99 requires declaration specifiers in a function definition; the
1737 absence is diagnosed through the diagnosis of implicit int. In GNU
1738 C we also allow but diagnose declarations without declaration
1739 specifiers, but only at top level (elsewhere they conflict with
1740 other syntax).
1741
1742 In Objective-C, declarations of the looping variable in a foreach
1743 statement are exceptionally terminated by 'in' (for example, 'for
1744 (NSObject *object in array) { ... }').
1745
1746 OpenMP:
1747
1748 declaration:
1749 threadprivate-directive
1750
1751 GIMPLE:
1752
1753 gimple-function-definition:
1754 declaration-specifiers[opt] __GIMPLE (gimple-or-rtl-pass-list) declarator
1755 declaration-list[opt] compound-statement
1756
1757 rtl-function-definition:
1758 declaration-specifiers[opt] __RTL (gimple-or-rtl-pass-list) declarator
1759 declaration-list[opt] compound-statement */
1760
1761 static void
1762 c_parser_declaration_or_fndef (c_parser *parser, bool fndef_ok,
1763 bool static_assert_ok, bool empty_ok,
1764 bool nested, bool start_attr_ok,
1765 tree *objc_foreach_object_declaration,
1766 vec<c_token> omp_declare_simd_clauses,
1767 struct oacc_routine_data *oacc_routine_data,
1768 bool *fallthru_attr_p)
1769 {
1770 struct c_declspecs *specs;
1771 tree prefix_attrs;
1772 tree all_prefix_attrs;
1773 bool diagnosed_no_specs = false;
1774 location_t here = c_parser_peek_token (parser)->location;
1775
1776 add_debug_begin_stmt (c_parser_peek_token (parser)->location);
1777
1778 if (static_assert_ok
1779 && c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
1780 {
1781 c_parser_static_assert_declaration (parser);
1782 return;
1783 }
1784 specs = build_null_declspecs ();
1785
1786 /* Try to detect an unknown type name when we have "A B" or "A *B". */
1787 if (c_parser_peek_token (parser)->type == CPP_NAME
1788 && c_parser_peek_token (parser)->id_kind == C_ID_ID
1789 && (c_parser_peek_2nd_token (parser)->type == CPP_NAME
1790 || c_parser_peek_2nd_token (parser)->type == CPP_MULT)
1791 && (!nested || !lookup_name (c_parser_peek_token (parser)->value)))
1792 {
1793 tree name = c_parser_peek_token (parser)->value;
1794
1795 /* Issue a warning about NAME being an unknown type name, perhaps
1796 with some kind of hint.
1797 If the user forgot a "struct" etc, suggest inserting
1798 it. Otherwise, attempt to look for misspellings. */
1799 gcc_rich_location richloc (here);
1800 if (tag_exists_p (RECORD_TYPE, name))
1801 {
1802 /* This is not C++ with its implicit typedef. */
1803 richloc.add_fixit_insert_before ("struct ");
1804 error_at (&richloc,
1805 "unknown type name %qE;"
1806 " use %<struct%> keyword to refer to the type",
1807 name);
1808 }
1809 else if (tag_exists_p (UNION_TYPE, name))
1810 {
1811 richloc.add_fixit_insert_before ("union ");
1812 error_at (&richloc,
1813 "unknown type name %qE;"
1814 " use %<union%> keyword to refer to the type",
1815 name);
1816 }
1817 else if (tag_exists_p (ENUMERAL_TYPE, name))
1818 {
1819 richloc.add_fixit_insert_before ("enum ");
1820 error_at (&richloc,
1821 "unknown type name %qE;"
1822 " use %<enum%> keyword to refer to the type",
1823 name);
1824 }
1825 else
1826 {
1827 auto_diagnostic_group d;
1828 name_hint hint = lookup_name_fuzzy (name, FUZZY_LOOKUP_TYPENAME,
1829 here);
1830 if (const char *suggestion = hint.suggestion ())
1831 {
1832 richloc.add_fixit_replace (suggestion);
1833 error_at (&richloc,
1834 "unknown type name %qE; did you mean %qs?",
1835 name, suggestion);
1836 }
1837 else
1838 error_at (here, "unknown type name %qE", name);
1839 }
1840
1841 /* Parse declspecs normally to get a correct pointer type, but avoid
1842 a further "fails to be a type name" error. Refuse nested functions
1843 since it is not how the user likely wants us to recover. */
1844 c_parser_peek_token (parser)->type = CPP_KEYWORD;
1845 c_parser_peek_token (parser)->keyword = RID_VOID;
1846 c_parser_peek_token (parser)->value = error_mark_node;
1847 fndef_ok = !nested;
1848 }
1849
1850 c_parser_declspecs (parser, specs, true, true, start_attr_ok,
1851 true, true, cla_nonabstract_decl);
1852 if (parser->error)
1853 {
1854 c_parser_skip_to_end_of_block_or_statement (parser);
1855 return;
1856 }
1857 if (nested && !specs->declspecs_seen_p)
1858 {
1859 c_parser_error (parser, "expected declaration specifiers");
1860 c_parser_skip_to_end_of_block_or_statement (parser);
1861 return;
1862 }
1863
1864 finish_declspecs (specs);
1865 bool auto_type_p = specs->typespec_word == cts_auto_type;
1866 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1867 {
1868 if (auto_type_p)
1869 error_at (here, "%<__auto_type%> in empty declaration");
1870 else if (specs->typespec_kind == ctsk_none
1871 && attribute_fallthrough_p (specs->attrs))
1872 {
1873 if (fallthru_attr_p != NULL)
1874 *fallthru_attr_p = true;
1875 tree fn = build_call_expr_internal_loc (here, IFN_FALLTHROUGH,
1876 void_type_node, 0);
1877 add_stmt (fn);
1878 }
1879 else if (empty_ok)
1880 shadow_tag (specs);
1881 else
1882 {
1883 shadow_tag_warned (specs, 1);
1884 pedwarn (here, 0, "empty declaration");
1885 }
1886 c_parser_consume_token (parser);
1887 if (oacc_routine_data)
1888 c_finish_oacc_routine (oacc_routine_data, NULL_TREE, false);
1889 return;
1890 }
1891
1892 /* Provide better error recovery. Note that a type name here is usually
1893 better diagnosed as a redeclaration. */
1894 if (empty_ok
1895 && specs->typespec_kind == ctsk_tagdef
1896 && c_parser_next_token_starts_declspecs (parser)
1897 && !c_parser_next_token_is (parser, CPP_NAME))
1898 {
1899 c_parser_error (parser, "expected %<;%>, identifier or %<(%>");
1900 parser->error = false;
1901 shadow_tag_warned (specs, 1);
1902 return;
1903 }
1904 else if (c_dialect_objc () && !auto_type_p)
1905 {
1906 /* Prefix attributes are an error on method decls. */
1907 switch (c_parser_peek_token (parser)->type)
1908 {
1909 case CPP_PLUS:
1910 case CPP_MINUS:
1911 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1912 return;
1913 if (specs->attrs)
1914 {
1915 warning_at (c_parser_peek_token (parser)->location,
1916 OPT_Wattributes,
1917 "prefix attributes are ignored for methods");
1918 specs->attrs = NULL_TREE;
1919 }
1920 if (fndef_ok)
1921 c_parser_objc_method_definition (parser);
1922 else
1923 c_parser_objc_methodproto (parser);
1924 return;
1925 break;
1926 default:
1927 break;
1928 }
1929 /* This is where we parse 'attributes @interface ...',
1930 'attributes @implementation ...', 'attributes @protocol ...'
1931 (where attributes could be, for example, __attribute__
1932 ((deprecated)).
1933 */
1934 switch (c_parser_peek_token (parser)->keyword)
1935 {
1936 case RID_AT_INTERFACE:
1937 {
1938 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1939 return;
1940 c_parser_objc_class_definition (parser, specs->attrs);
1941 return;
1942 }
1943 break;
1944 case RID_AT_IMPLEMENTATION:
1945 {
1946 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1947 return;
1948 if (specs->attrs)
1949 {
1950 warning_at (c_parser_peek_token (parser)->location,
1951 OPT_Wattributes,
1952 "prefix attributes are ignored for implementations");
1953 specs->attrs = NULL_TREE;
1954 }
1955 c_parser_objc_class_definition (parser, NULL_TREE);
1956 return;
1957 }
1958 break;
1959 case RID_AT_PROTOCOL:
1960 {
1961 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1962 return;
1963 c_parser_objc_protocol_definition (parser, specs->attrs);
1964 return;
1965 }
1966 break;
1967 case RID_AT_ALIAS:
1968 case RID_AT_CLASS:
1969 case RID_AT_END:
1970 case RID_AT_PROPERTY:
1971 if (specs->attrs)
1972 {
1973 c_parser_error (parser, "unexpected attribute");
1974 specs->attrs = NULL;
1975 }
1976 break;
1977 default:
1978 break;
1979 }
1980 }
1981 else if (attribute_fallthrough_p (specs->attrs))
1982 warning_at (here, OPT_Wattributes,
1983 "%<fallthrough%> attribute not followed by %<;%>");
1984
1985 pending_xref_error ();
1986 prefix_attrs = specs->attrs;
1987 all_prefix_attrs = prefix_attrs;
1988 specs->attrs = NULL_TREE;
1989 while (true)
1990 {
1991 struct c_declarator *declarator;
1992 bool dummy = false;
1993 timevar_id_t tv;
1994 tree fnbody = NULL_TREE;
1995 /* Declaring either one or more declarators (in which case we
1996 should diagnose if there were no declaration specifiers) or a
1997 function definition (in which case the diagnostic for
1998 implicit int suffices). */
1999 declarator = c_parser_declarator (parser,
2000 specs->typespec_kind != ctsk_none,
2001 C_DTR_NORMAL, &dummy);
2002 if (declarator == NULL)
2003 {
2004 if (omp_declare_simd_clauses.exists ())
2005 c_finish_omp_declare_simd (parser, NULL_TREE, NULL_TREE,
2006 omp_declare_simd_clauses);
2007 if (oacc_routine_data)
2008 c_finish_oacc_routine (oacc_routine_data, NULL_TREE, false);
2009 c_parser_skip_to_end_of_block_or_statement (parser);
2010 return;
2011 }
2012 if (auto_type_p && declarator->kind != cdk_id)
2013 {
2014 error_at (here,
2015 "%<__auto_type%> requires a plain identifier"
2016 " as declarator");
2017 c_parser_skip_to_end_of_block_or_statement (parser);
2018 return;
2019 }
2020 if (c_parser_next_token_is (parser, CPP_EQ)
2021 || c_parser_next_token_is (parser, CPP_COMMA)
2022 || c_parser_next_token_is (parser, CPP_SEMICOLON)
2023 || c_parser_next_token_is_keyword (parser, RID_ASM)
2024 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE)
2025 || c_parser_next_token_is_keyword (parser, RID_IN))
2026 {
2027 tree asm_name = NULL_TREE;
2028 tree postfix_attrs = NULL_TREE;
2029 if (!diagnosed_no_specs && !specs->declspecs_seen_p)
2030 {
2031 diagnosed_no_specs = true;
2032 pedwarn (here, 0, "data definition has no type or storage class");
2033 }
2034 /* Having seen a data definition, there cannot now be a
2035 function definition. */
2036 fndef_ok = false;
2037 if (c_parser_next_token_is_keyword (parser, RID_ASM))
2038 asm_name = c_parser_simple_asm_expr (parser);
2039 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2040 {
2041 postfix_attrs = c_parser_attributes (parser);
2042 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2043 {
2044 /* This means there is an attribute specifier after
2045 the declarator in a function definition. Provide
2046 some more information for the user. */
2047 error_at (here, "attributes should be specified before the "
2048 "declarator in a function definition");
2049 c_parser_skip_to_end_of_block_or_statement (parser);
2050 return;
2051 }
2052 }
2053 if (c_parser_next_token_is (parser, CPP_EQ))
2054 {
2055 tree d;
2056 struct c_expr init;
2057 location_t init_loc;
2058 c_parser_consume_token (parser);
2059 if (auto_type_p)
2060 {
2061 init_loc = c_parser_peek_token (parser)->location;
2062 rich_location richloc (line_table, init_loc);
2063 start_init (NULL_TREE, asm_name, global_bindings_p (), &richloc);
2064 /* A parameter is initialized, which is invalid. Don't
2065 attempt to instrument the initializer. */
2066 int flag_sanitize_save = flag_sanitize;
2067 if (nested && !empty_ok)
2068 flag_sanitize = 0;
2069 init = c_parser_expr_no_commas (parser, NULL);
2070 flag_sanitize = flag_sanitize_save;
2071 if (TREE_CODE (init.value) == COMPONENT_REF
2072 && DECL_C_BIT_FIELD (TREE_OPERAND (init.value, 1)))
2073 error_at (here,
2074 "%<__auto_type%> used with a bit-field"
2075 " initializer");
2076 init = convert_lvalue_to_rvalue (init_loc, init, true, true);
2077 tree init_type = TREE_TYPE (init.value);
2078 /* As with typeof, remove all qualifiers from atomic types. */
2079 if (init_type != error_mark_node && TYPE_ATOMIC (init_type))
2080 init_type
2081 = c_build_qualified_type (init_type, TYPE_UNQUALIFIED);
2082 bool vm_type = variably_modified_type_p (init_type,
2083 NULL_TREE);
2084 if (vm_type)
2085 init.value = save_expr (init.value);
2086 finish_init ();
2087 specs->typespec_kind = ctsk_typeof;
2088 specs->locations[cdw_typedef] = init_loc;
2089 specs->typedef_p = true;
2090 specs->type = init_type;
2091 if (vm_type)
2092 {
2093 bool maybe_const = true;
2094 tree type_expr = c_fully_fold (init.value, false,
2095 &maybe_const);
2096 specs->expr_const_operands &= maybe_const;
2097 if (specs->expr)
2098 specs->expr = build2 (COMPOUND_EXPR,
2099 TREE_TYPE (type_expr),
2100 specs->expr, type_expr);
2101 else
2102 specs->expr = type_expr;
2103 }
2104 d = start_decl (declarator, specs, true,
2105 chainon (postfix_attrs, all_prefix_attrs));
2106 if (!d)
2107 d = error_mark_node;
2108 if (omp_declare_simd_clauses.exists ())
2109 c_finish_omp_declare_simd (parser, d, NULL_TREE,
2110 omp_declare_simd_clauses);
2111 }
2112 else
2113 {
2114 /* The declaration of the variable is in effect while
2115 its initializer is parsed. */
2116 d = start_decl (declarator, specs, true,
2117 chainon (postfix_attrs, all_prefix_attrs));
2118 if (!d)
2119 d = error_mark_node;
2120 if (omp_declare_simd_clauses.exists ())
2121 c_finish_omp_declare_simd (parser, d, NULL_TREE,
2122 omp_declare_simd_clauses);
2123 init_loc = c_parser_peek_token (parser)->location;
2124 rich_location richloc (line_table, init_loc);
2125 start_init (d, asm_name, global_bindings_p (), &richloc);
2126 /* A parameter is initialized, which is invalid. Don't
2127 attempt to instrument the initializer. */
2128 int flag_sanitize_save = flag_sanitize;
2129 if (TREE_CODE (d) == PARM_DECL)
2130 flag_sanitize = 0;
2131 init = c_parser_initializer (parser);
2132 flag_sanitize = flag_sanitize_save;
2133 finish_init ();
2134 }
2135 if (oacc_routine_data)
2136 c_finish_oacc_routine (oacc_routine_data, d, false);
2137 if (d != error_mark_node)
2138 {
2139 maybe_warn_string_init (init_loc, TREE_TYPE (d), init);
2140 finish_decl (d, init_loc, init.value,
2141 init.original_type, asm_name);
2142 }
2143 }
2144 else
2145 {
2146 if (auto_type_p)
2147 {
2148 error_at (here,
2149 "%<__auto_type%> requires an initialized "
2150 "data declaration");
2151 c_parser_skip_to_end_of_block_or_statement (parser);
2152 return;
2153 }
2154 tree d = start_decl (declarator, specs, false,
2155 chainon (postfix_attrs,
2156 all_prefix_attrs));
2157 if (d && TREE_CODE (d) == FUNCTION_DECL)
2158 if (declarator->kind == cdk_function)
2159 if (DECL_ARGUMENTS (d) == NULL_TREE)
2160 DECL_ARGUMENTS (d) = declarator->u.arg_info->parms;
2161 if (omp_declare_simd_clauses.exists ())
2162 {
2163 tree parms = NULL_TREE;
2164 if (d && TREE_CODE (d) == FUNCTION_DECL)
2165 {
2166 struct c_declarator *ce = declarator;
2167 while (ce != NULL)
2168 if (ce->kind == cdk_function)
2169 {
2170 parms = ce->u.arg_info->parms;
2171 break;
2172 }
2173 else
2174 ce = ce->declarator;
2175 }
2176 if (parms)
2177 temp_store_parm_decls (d, parms);
2178 c_finish_omp_declare_simd (parser, d, parms,
2179 omp_declare_simd_clauses);
2180 if (parms)
2181 temp_pop_parm_decls ();
2182 }
2183 if (oacc_routine_data)
2184 c_finish_oacc_routine (oacc_routine_data, d, false);
2185 if (d)
2186 finish_decl (d, UNKNOWN_LOCATION, NULL_TREE,
2187 NULL_TREE, asm_name);
2188
2189 if (c_parser_next_token_is_keyword (parser, RID_IN))
2190 {
2191 if (d)
2192 *objc_foreach_object_declaration = d;
2193 else
2194 *objc_foreach_object_declaration = error_mark_node;
2195 }
2196 }
2197 if (c_parser_next_token_is (parser, CPP_COMMA))
2198 {
2199 if (auto_type_p)
2200 {
2201 error_at (here,
2202 "%<__auto_type%> may only be used with"
2203 " a single declarator");
2204 c_parser_skip_to_end_of_block_or_statement (parser);
2205 return;
2206 }
2207 c_parser_consume_token (parser);
2208 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2209 all_prefix_attrs = chainon (c_parser_attributes (parser),
2210 prefix_attrs);
2211 else
2212 all_prefix_attrs = prefix_attrs;
2213 continue;
2214 }
2215 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2216 {
2217 c_parser_consume_token (parser);
2218 return;
2219 }
2220 else if (c_parser_next_token_is_keyword (parser, RID_IN))
2221 {
2222 /* This can only happen in Objective-C: we found the
2223 'in' that terminates the declaration inside an
2224 Objective-C foreach statement. Do not consume the
2225 token, so that the caller can use it to determine
2226 that this indeed is a foreach context. */
2227 return;
2228 }
2229 else
2230 {
2231 c_parser_error (parser, "expected %<,%> or %<;%>");
2232 c_parser_skip_to_end_of_block_or_statement (parser);
2233 return;
2234 }
2235 }
2236 else if (auto_type_p)
2237 {
2238 error_at (here,
2239 "%<__auto_type%> requires an initialized data declaration");
2240 c_parser_skip_to_end_of_block_or_statement (parser);
2241 return;
2242 }
2243 else if (!fndef_ok)
2244 {
2245 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, "
2246 "%<asm%> or %<__attribute__%>");
2247 c_parser_skip_to_end_of_block_or_statement (parser);
2248 return;
2249 }
2250 /* Function definition (nested or otherwise). */
2251 if (nested)
2252 {
2253 pedwarn (here, OPT_Wpedantic, "ISO C forbids nested functions");
2254 c_push_function_context ();
2255 }
2256 if (!start_function (specs, declarator, all_prefix_attrs))
2257 {
2258 /* At this point we've consumed:
2259 declaration-specifiers declarator
2260 and the next token isn't CPP_EQ, CPP_COMMA, CPP_SEMICOLON,
2261 RID_ASM, RID_ATTRIBUTE, or RID_IN,
2262 but the
2263 declaration-specifiers declarator
2264 aren't grokkable as a function definition, so we have
2265 an error. */
2266 gcc_assert (!c_parser_next_token_is (parser, CPP_SEMICOLON));
2267 if (c_parser_next_token_starts_declspecs (parser))
2268 {
2269 /* If we have
2270 declaration-specifiers declarator decl-specs
2271 then assume we have a missing semicolon, which would
2272 give us:
2273 declaration-specifiers declarator decl-specs
2274 ^
2275 ;
2276 <~~~~~~~~~ declaration ~~~~~~~~~~>
2277 Use c_parser_require to get an error with a fix-it hint. */
2278 c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>");
2279 parser->error = false;
2280 }
2281 else
2282 {
2283 /* This can appear in many cases looking nothing like a
2284 function definition, so we don't give a more specific
2285 error suggesting there was one. */
2286 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, %<asm%> "
2287 "or %<__attribute__%>");
2288 }
2289 if (nested)
2290 c_pop_function_context ();
2291 break;
2292 }
2293
2294 if (DECL_DECLARED_INLINE_P (current_function_decl))
2295 tv = TV_PARSE_INLINE;
2296 else
2297 tv = TV_PARSE_FUNC;
2298 auto_timevar at (g_timer, tv);
2299
2300 /* Parse old-style parameter declarations. ??? Attributes are
2301 not allowed to start declaration specifiers here because of a
2302 syntax conflict between a function declaration with attribute
2303 suffix and a function definition with an attribute prefix on
2304 first old-style parameter declaration. Following the old
2305 parser, they are not accepted on subsequent old-style
2306 parameter declarations either. However, there is no
2307 ambiguity after the first declaration, nor indeed on the
2308 first as long as we don't allow postfix attributes after a
2309 declarator with a nonempty identifier list in a definition;
2310 and postfix attributes have never been accepted here in
2311 function definitions either. */
2312 while (c_parser_next_token_is_not (parser, CPP_EOF)
2313 && c_parser_next_token_is_not (parser, CPP_OPEN_BRACE))
2314 c_parser_declaration_or_fndef (parser, false, false, false,
2315 true, false, NULL, vNULL);
2316 store_parm_decls ();
2317 if (omp_declare_simd_clauses.exists ())
2318 c_finish_omp_declare_simd (parser, current_function_decl, NULL_TREE,
2319 omp_declare_simd_clauses);
2320 if (oacc_routine_data)
2321 c_finish_oacc_routine (oacc_routine_data, current_function_decl, true);
2322 DECL_STRUCT_FUNCTION (current_function_decl)->function_start_locus
2323 = c_parser_peek_token (parser)->location;
2324
2325 /* If the definition was marked with __GIMPLE then parse the
2326 function body as GIMPLE. */
2327 if (specs->gimple_p)
2328 {
2329 cfun->pass_startwith = specs->gimple_or_rtl_pass;
2330 bool saved = in_late_binary_op;
2331 in_late_binary_op = true;
2332 c_parser_parse_gimple_body (parser);
2333 in_late_binary_op = saved;
2334 }
2335 /* Similarly, if it was marked with __RTL, use the RTL parser now,
2336 consuming the function body. */
2337 else if (specs->rtl_p)
2338 {
2339 c_parser_parse_rtl_body (parser, specs->gimple_or_rtl_pass);
2340
2341 /* Normally, store_parm_decls sets next_is_function_body,
2342 anticipating a function body. We need a push_scope/pop_scope
2343 pair to flush out this state, or subsequent function parsing
2344 will go wrong. */
2345 push_scope ();
2346 pop_scope ();
2347
2348 finish_function ();
2349 return;
2350 }
2351 else
2352 fnbody = c_parser_compound_statement (parser);
2353 tree fndecl = current_function_decl;
2354 if (nested)
2355 {
2356 tree decl = current_function_decl;
2357 /* Mark nested functions as needing static-chain initially.
2358 lower_nested_functions will recompute it but the
2359 DECL_STATIC_CHAIN flag is also used before that happens,
2360 by initializer_constant_valid_p. See gcc.dg/nested-fn-2.c. */
2361 DECL_STATIC_CHAIN (decl) = 1;
2362 add_stmt (fnbody);
2363 finish_function ();
2364 c_pop_function_context ();
2365 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
2366 }
2367 else
2368 {
2369 if (fnbody)
2370 add_stmt (fnbody);
2371 finish_function ();
2372 }
2373 /* Get rid of the empty stmt list for GIMPLE. */
2374 if (specs->gimple_p)
2375 DECL_SAVED_TREE (fndecl) = NULL_TREE;
2376
2377 break;
2378 }
2379 }
2380
2381 /* Parse an asm-definition (asm() outside a function body). This is a
2382 GNU extension.
2383
2384 asm-definition:
2385 simple-asm-expr ;
2386 */
2387
2388 static void
2389 c_parser_asm_definition (c_parser *parser)
2390 {
2391 tree asm_str = c_parser_simple_asm_expr (parser);
2392 if (asm_str)
2393 symtab->finalize_toplevel_asm (asm_str);
2394 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
2395 }
2396
2397 /* Parse a static assertion (C11 6.7.10).
2398
2399 static_assert-declaration:
2400 static_assert-declaration-no-semi ;
2401 */
2402
2403 static void
2404 c_parser_static_assert_declaration (c_parser *parser)
2405 {
2406 c_parser_static_assert_declaration_no_semi (parser);
2407 if (parser->error
2408 || !c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
2409 c_parser_skip_to_end_of_block_or_statement (parser);
2410 }
2411
2412 /* Parse a static assertion (C11 6.7.10), without the trailing
2413 semicolon.
2414
2415 static_assert-declaration-no-semi:
2416 _Static_assert ( constant-expression , string-literal )
2417
2418 C2X:
2419 static_assert-declaration-no-semi:
2420 _Static_assert ( constant-expression )
2421 */
2422
2423 static void
2424 c_parser_static_assert_declaration_no_semi (c_parser *parser)
2425 {
2426 location_t assert_loc, value_loc;
2427 tree value;
2428 tree string = NULL_TREE;
2429
2430 gcc_assert (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT));
2431 assert_loc = c_parser_peek_token (parser)->location;
2432 if (flag_isoc99)
2433 pedwarn_c99 (assert_loc, OPT_Wpedantic,
2434 "ISO C99 does not support %<_Static_assert%>");
2435 else
2436 pedwarn_c99 (assert_loc, OPT_Wpedantic,
2437 "ISO C90 does not support %<_Static_assert%>");
2438 c_parser_consume_token (parser);
2439 matching_parens parens;
2440 if (!parens.require_open (parser))
2441 return;
2442 location_t value_tok_loc = c_parser_peek_token (parser)->location;
2443 value = c_parser_expr_no_commas (parser, NULL).value;
2444 value_loc = EXPR_LOC_OR_LOC (value, value_tok_loc);
2445 parser->lex_untranslated_string = true;
2446 if (c_parser_next_token_is (parser, CPP_COMMA))
2447 {
2448 c_parser_consume_token (parser);
2449 switch (c_parser_peek_token (parser)->type)
2450 {
2451 case CPP_STRING:
2452 case CPP_STRING16:
2453 case CPP_STRING32:
2454 case CPP_WSTRING:
2455 case CPP_UTF8STRING:
2456 string = c_parser_peek_token (parser)->value;
2457 c_parser_consume_token (parser);
2458 parser->lex_untranslated_string = false;
2459 break;
2460 default:
2461 c_parser_error (parser, "expected string literal");
2462 parser->lex_untranslated_string = false;
2463 return;
2464 }
2465 }
2466 else if (flag_isoc11)
2467 /* If pedantic for pre-C11, the use of _Static_assert itself will
2468 have been diagnosed, so do not also diagnose the use of this
2469 new C2X feature of _Static_assert. */
2470 pedwarn_c11 (assert_loc, OPT_Wpedantic,
2471 "ISO C11 does not support omitting the string in "
2472 "%<_Static_assert%>");
2473 parens.require_close (parser);
2474
2475 if (!INTEGRAL_TYPE_P (TREE_TYPE (value)))
2476 {
2477 error_at (value_loc, "expression in static assertion is not an integer");
2478 return;
2479 }
2480 if (TREE_CODE (value) != INTEGER_CST)
2481 {
2482 value = c_fully_fold (value, false, NULL);
2483 /* Strip no-op conversions. */
2484 STRIP_TYPE_NOPS (value);
2485 if (TREE_CODE (value) == INTEGER_CST)
2486 pedwarn (value_loc, OPT_Wpedantic, "expression in static assertion "
2487 "is not an integer constant expression");
2488 }
2489 if (TREE_CODE (value) != INTEGER_CST)
2490 {
2491 error_at (value_loc, "expression in static assertion is not constant");
2492 return;
2493 }
2494 constant_expression_warning (value);
2495 if (integer_zerop (value))
2496 {
2497 if (string)
2498 error_at (assert_loc, "static assertion failed: %E", string);
2499 else
2500 error_at (assert_loc, "static assertion failed");
2501 }
2502 }
2503
2504 /* Parse some declaration specifiers (possibly none) (C90 6.5, C99
2505 6.7, C11 6.7), adding them to SPECS (which may already include some).
2506 Storage class specifiers are accepted iff SCSPEC_OK; type
2507 specifiers are accepted iff TYPESPEC_OK; alignment specifiers are
2508 accepted iff ALIGNSPEC_OK; attributes are accepted at the start
2509 iff START_ATTR_OK; __auto_type is accepted iff AUTO_TYPE_OK.
2510
2511 declaration-specifiers:
2512 storage-class-specifier declaration-specifiers[opt]
2513 type-specifier declaration-specifiers[opt]
2514 type-qualifier declaration-specifiers[opt]
2515 function-specifier declaration-specifiers[opt]
2516 alignment-specifier declaration-specifiers[opt]
2517
2518 Function specifiers (inline) are from C99, and are currently
2519 handled as storage class specifiers, as is __thread. Alignment
2520 specifiers are from C11.
2521
2522 C90 6.5.1, C99 6.7.1, C11 6.7.1:
2523 storage-class-specifier:
2524 typedef
2525 extern
2526 static
2527 auto
2528 register
2529 _Thread_local
2530
2531 (_Thread_local is new in C11.)
2532
2533 C99 6.7.4, C11 6.7.4:
2534 function-specifier:
2535 inline
2536 _Noreturn
2537
2538 (_Noreturn is new in C11.)
2539
2540 C90 6.5.2, C99 6.7.2, C11 6.7.2:
2541 type-specifier:
2542 void
2543 char
2544 short
2545 int
2546 long
2547 float
2548 double
2549 signed
2550 unsigned
2551 _Bool
2552 _Complex
2553 [_Imaginary removed in C99 TC2]
2554 struct-or-union-specifier
2555 enum-specifier
2556 typedef-name
2557 atomic-type-specifier
2558
2559 (_Bool and _Complex are new in C99.)
2560 (atomic-type-specifier is new in C11.)
2561
2562 C90 6.5.3, C99 6.7.3, C11 6.7.3:
2563
2564 type-qualifier:
2565 const
2566 restrict
2567 volatile
2568 address-space-qualifier
2569 _Atomic
2570
2571 (restrict is new in C99.)
2572 (_Atomic is new in C11.)
2573
2574 GNU extensions:
2575
2576 declaration-specifiers:
2577 attributes declaration-specifiers[opt]
2578
2579 type-qualifier:
2580 address-space
2581
2582 address-space:
2583 identifier recognized by the target
2584
2585 storage-class-specifier:
2586 __thread
2587
2588 type-specifier:
2589 typeof-specifier
2590 __auto_type
2591 __intN
2592 _Decimal32
2593 _Decimal64
2594 _Decimal128
2595 _Fract
2596 _Accum
2597 _Sat
2598
2599 (_Fract, _Accum, and _Sat are new from ISO/IEC DTR 18037:
2600 http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1169.pdf)
2601
2602 atomic-type-specifier
2603 _Atomic ( type-name )
2604
2605 Objective-C:
2606
2607 type-specifier:
2608 class-name objc-protocol-refs[opt]
2609 typedef-name objc-protocol-refs
2610 objc-protocol-refs
2611 */
2612
2613 void
2614 c_parser_declspecs (c_parser *parser, struct c_declspecs *specs,
2615 bool scspec_ok, bool typespec_ok, bool start_attr_ok,
2616 bool alignspec_ok, bool auto_type_ok,
2617 enum c_lookahead_kind la)
2618 {
2619 bool attrs_ok = start_attr_ok;
2620 bool seen_type = specs->typespec_kind != ctsk_none;
2621
2622 if (!typespec_ok)
2623 gcc_assert (la == cla_prefer_id);
2624
2625 while (c_parser_next_token_is (parser, CPP_NAME)
2626 || c_parser_next_token_is (parser, CPP_KEYWORD)
2627 || (c_dialect_objc () && c_parser_next_token_is (parser, CPP_LESS)))
2628 {
2629 struct c_typespec t;
2630 tree attrs;
2631 tree align;
2632 location_t loc = c_parser_peek_token (parser)->location;
2633
2634 /* If we cannot accept a type, exit if the next token must start
2635 one. Also, if we already have seen a tagged definition,
2636 a typename would be an error anyway and likely the user
2637 has simply forgotten a semicolon, so we exit. */
2638 if ((!typespec_ok || specs->typespec_kind == ctsk_tagdef)
2639 && c_parser_next_tokens_start_typename (parser, la)
2640 && !c_parser_next_token_is_qualifier (parser)
2641 && !c_parser_next_token_is_keyword (parser, RID_ALIGNAS))
2642 break;
2643
2644 if (c_parser_next_token_is (parser, CPP_NAME))
2645 {
2646 c_token *name_token = c_parser_peek_token (parser);
2647 tree value = name_token->value;
2648 c_id_kind kind = name_token->id_kind;
2649
2650 if (kind == C_ID_ADDRSPACE)
2651 {
2652 addr_space_t as
2653 = name_token->keyword - RID_FIRST_ADDR_SPACE;
2654 declspecs_add_addrspace (name_token->location, specs, as);
2655 c_parser_consume_token (parser);
2656 attrs_ok = true;
2657 continue;
2658 }
2659
2660 gcc_assert (!c_parser_next_token_is_qualifier (parser));
2661
2662 /* If we cannot accept a type, and the next token must start one,
2663 exit. Do the same if we already have seen a tagged definition,
2664 since it would be an error anyway and likely the user has simply
2665 forgotten a semicolon. */
2666 if (seen_type || !c_parser_next_tokens_start_typename (parser, la))
2667 break;
2668
2669 /* Now at an unknown typename (C_ID_ID), a C_ID_TYPENAME or
2670 a C_ID_CLASSNAME. */
2671 c_parser_consume_token (parser);
2672 seen_type = true;
2673 attrs_ok = true;
2674 if (kind == C_ID_ID)
2675 {
2676 error_at (loc, "unknown type name %qE", value);
2677 t.kind = ctsk_typedef;
2678 t.spec = error_mark_node;
2679 }
2680 else if (kind == C_ID_TYPENAME
2681 && (!c_dialect_objc ()
2682 || c_parser_next_token_is_not (parser, CPP_LESS)))
2683 {
2684 t.kind = ctsk_typedef;
2685 /* For a typedef name, record the meaning, not the name.
2686 In case of 'foo foo, bar;'. */
2687 t.spec = lookup_name (value);
2688 }
2689 else
2690 {
2691 tree proto = NULL_TREE;
2692 gcc_assert (c_dialect_objc ());
2693 t.kind = ctsk_objc;
2694 if (c_parser_next_token_is (parser, CPP_LESS))
2695 proto = c_parser_objc_protocol_refs (parser);
2696 t.spec = objc_get_protocol_qualified_type (value, proto);
2697 }
2698 t.expr = NULL_TREE;
2699 t.expr_const_operands = true;
2700 declspecs_add_type (name_token->location, specs, t);
2701 continue;
2702 }
2703 if (c_parser_next_token_is (parser, CPP_LESS))
2704 {
2705 /* Make "<SomeProtocol>" equivalent to "id <SomeProtocol>" -
2706 nisse@lysator.liu.se. */
2707 tree proto;
2708 gcc_assert (c_dialect_objc ());
2709 if (!typespec_ok || seen_type)
2710 break;
2711 proto = c_parser_objc_protocol_refs (parser);
2712 t.kind = ctsk_objc;
2713 t.spec = objc_get_protocol_qualified_type (NULL_TREE, proto);
2714 t.expr = NULL_TREE;
2715 t.expr_const_operands = true;
2716 declspecs_add_type (loc, specs, t);
2717 continue;
2718 }
2719 gcc_assert (c_parser_next_token_is (parser, CPP_KEYWORD));
2720 switch (c_parser_peek_token (parser)->keyword)
2721 {
2722 case RID_STATIC:
2723 case RID_EXTERN:
2724 case RID_REGISTER:
2725 case RID_TYPEDEF:
2726 case RID_INLINE:
2727 case RID_NORETURN:
2728 case RID_AUTO:
2729 case RID_THREAD:
2730 if (!scspec_ok)
2731 goto out;
2732 attrs_ok = true;
2733 /* TODO: Distinguish between function specifiers (inline, noreturn)
2734 and storage class specifiers, either here or in
2735 declspecs_add_scspec. */
2736 declspecs_add_scspec (loc, specs,
2737 c_parser_peek_token (parser)->value);
2738 c_parser_consume_token (parser);
2739 break;
2740 case RID_AUTO_TYPE:
2741 if (!auto_type_ok)
2742 goto out;
2743 /* Fall through. */
2744 case RID_UNSIGNED:
2745 case RID_LONG:
2746 case RID_SHORT:
2747 case RID_SIGNED:
2748 case RID_COMPLEX:
2749 case RID_INT:
2750 case RID_CHAR:
2751 case RID_FLOAT:
2752 case RID_DOUBLE:
2753 case RID_VOID:
2754 case RID_DFLOAT32:
2755 case RID_DFLOAT64:
2756 case RID_DFLOAT128:
2757 CASE_RID_FLOATN_NX:
2758 case RID_BOOL:
2759 case RID_FRACT:
2760 case RID_ACCUM:
2761 case RID_SAT:
2762 case RID_INT_N_0:
2763 case RID_INT_N_1:
2764 case RID_INT_N_2:
2765 case RID_INT_N_3:
2766 if (!typespec_ok)
2767 goto out;
2768 attrs_ok = true;
2769 seen_type = true;
2770 if (c_dialect_objc ())
2771 parser->objc_need_raw_identifier = true;
2772 t.kind = ctsk_resword;
2773 t.spec = c_parser_peek_token (parser)->value;
2774 t.expr = NULL_TREE;
2775 t.expr_const_operands = true;
2776 declspecs_add_type (loc, specs, t);
2777 c_parser_consume_token (parser);
2778 break;
2779 case RID_ENUM:
2780 if (!typespec_ok)
2781 goto out;
2782 attrs_ok = true;
2783 seen_type = true;
2784 t = c_parser_enum_specifier (parser);
2785 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, t.spec);
2786 declspecs_add_type (loc, specs, t);
2787 break;
2788 case RID_STRUCT:
2789 case RID_UNION:
2790 if (!typespec_ok)
2791 goto out;
2792 attrs_ok = true;
2793 seen_type = true;
2794 t = c_parser_struct_or_union_specifier (parser);
2795 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, t.spec);
2796 declspecs_add_type (loc, specs, t);
2797 break;
2798 case RID_TYPEOF:
2799 /* ??? The old parser rejected typeof after other type
2800 specifiers, but is a syntax error the best way of
2801 handling this? */
2802 if (!typespec_ok || seen_type)
2803 goto out;
2804 attrs_ok = true;
2805 seen_type = true;
2806 t = c_parser_typeof_specifier (parser);
2807 declspecs_add_type (loc, specs, t);
2808 break;
2809 case RID_ATOMIC:
2810 /* C parser handling of Objective-C constructs needs
2811 checking for correct lvalue-to-rvalue conversions, and
2812 the code in build_modify_expr handling various
2813 Objective-C cases, and that in build_unary_op handling
2814 Objective-C cases for increment / decrement, also needs
2815 updating; uses of TYPE_MAIN_VARIANT in objc_compare_types
2816 and objc_types_are_equivalent may also need updates. */
2817 if (c_dialect_objc ())
2818 sorry ("%<_Atomic%> in Objective-C");
2819 if (flag_isoc99)
2820 pedwarn_c99 (loc, OPT_Wpedantic,
2821 "ISO C99 does not support the %<_Atomic%> qualifier");
2822 else
2823 pedwarn_c99 (loc, OPT_Wpedantic,
2824 "ISO C90 does not support the %<_Atomic%> qualifier");
2825 attrs_ok = true;
2826 tree value;
2827 value = c_parser_peek_token (parser)->value;
2828 c_parser_consume_token (parser);
2829 if (typespec_ok && c_parser_next_token_is (parser, CPP_OPEN_PAREN))
2830 {
2831 /* _Atomic ( type-name ). */
2832 seen_type = true;
2833 c_parser_consume_token (parser);
2834 struct c_type_name *type = c_parser_type_name (parser);
2835 t.kind = ctsk_typeof;
2836 t.spec = error_mark_node;
2837 t.expr = NULL_TREE;
2838 t.expr_const_operands = true;
2839 if (type != NULL)
2840 t.spec = groktypename (type, &t.expr,
2841 &t.expr_const_operands);
2842 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2843 "expected %<)%>");
2844 if (t.spec != error_mark_node)
2845 {
2846 if (TREE_CODE (t.spec) == ARRAY_TYPE)
2847 error_at (loc, "%<_Atomic%>-qualified array type");
2848 else if (TREE_CODE (t.spec) == FUNCTION_TYPE)
2849 error_at (loc, "%<_Atomic%>-qualified function type");
2850 else if (TYPE_QUALS (t.spec) != TYPE_UNQUALIFIED)
2851 error_at (loc, "%<_Atomic%> applied to a qualified type");
2852 else
2853 t.spec = c_build_qualified_type (t.spec, TYPE_QUAL_ATOMIC);
2854 }
2855 declspecs_add_type (loc, specs, t);
2856 }
2857 else
2858 declspecs_add_qual (loc, specs, value);
2859 break;
2860 case RID_CONST:
2861 case RID_VOLATILE:
2862 case RID_RESTRICT:
2863 attrs_ok = true;
2864 declspecs_add_qual (loc, specs, c_parser_peek_token (parser)->value);
2865 c_parser_consume_token (parser);
2866 break;
2867 case RID_ATTRIBUTE:
2868 if (!attrs_ok)
2869 goto out;
2870 attrs = c_parser_attributes (parser);
2871 declspecs_add_attrs (loc, specs, attrs);
2872 break;
2873 case RID_ALIGNAS:
2874 if (!alignspec_ok)
2875 goto out;
2876 align = c_parser_alignas_specifier (parser);
2877 declspecs_add_alignas (loc, specs, align);
2878 break;
2879 case RID_GIMPLE:
2880 if (! flag_gimple)
2881 error_at (loc, "%<__GIMPLE%> only valid with -fgimple");
2882 c_parser_consume_token (parser);
2883 specs->gimple_p = true;
2884 specs->locations[cdw_gimple] = loc;
2885 specs->gimple_or_rtl_pass = c_parser_gimple_or_rtl_pass_list (parser);
2886 break;
2887 case RID_RTL:
2888 c_parser_consume_token (parser);
2889 specs->rtl_p = true;
2890 specs->locations[cdw_rtl] = loc;
2891 specs->gimple_or_rtl_pass = c_parser_gimple_or_rtl_pass_list (parser);
2892 break;
2893 default:
2894 goto out;
2895 }
2896 }
2897 out: ;
2898 }
2899
2900 /* Parse an enum specifier (C90 6.5.2.2, C99 6.7.2.2, C11 6.7.2.2).
2901
2902 enum-specifier:
2903 enum attributes[opt] identifier[opt] { enumerator-list } attributes[opt]
2904 enum attributes[opt] identifier[opt] { enumerator-list , } attributes[opt]
2905 enum attributes[opt] identifier
2906
2907 The form with trailing comma is new in C99. The forms with
2908 attributes are GNU extensions. In GNU C, we accept any expression
2909 without commas in the syntax (assignment expressions, not just
2910 conditional expressions); assignment expressions will be diagnosed
2911 as non-constant.
2912
2913 enumerator-list:
2914 enumerator
2915 enumerator-list , enumerator
2916
2917 enumerator:
2918 enumeration-constant
2919 enumeration-constant = constant-expression
2920
2921 GNU Extensions:
2922
2923 enumerator:
2924 enumeration-constant attributes[opt]
2925 enumeration-constant attributes[opt] = constant-expression
2926
2927 */
2928
2929 static struct c_typespec
2930 c_parser_enum_specifier (c_parser *parser)
2931 {
2932 struct c_typespec ret;
2933 tree attrs;
2934 tree ident = NULL_TREE;
2935 location_t enum_loc;
2936 location_t ident_loc = UNKNOWN_LOCATION; /* Quiet warning. */
2937 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ENUM));
2938 c_parser_consume_token (parser);
2939 attrs = c_parser_attributes (parser);
2940 enum_loc = c_parser_peek_token (parser)->location;
2941 /* Set the location in case we create a decl now. */
2942 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
2943 if (c_parser_next_token_is (parser, CPP_NAME))
2944 {
2945 ident = c_parser_peek_token (parser)->value;
2946 ident_loc = c_parser_peek_token (parser)->location;
2947 enum_loc = ident_loc;
2948 c_parser_consume_token (parser);
2949 }
2950 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2951 {
2952 /* Parse an enum definition. */
2953 struct c_enum_contents the_enum;
2954 tree type;
2955 tree postfix_attrs;
2956 /* We chain the enumerators in reverse order, then put them in
2957 forward order at the end. */
2958 tree values;
2959 timevar_push (TV_PARSE_ENUM);
2960 type = start_enum (enum_loc, &the_enum, ident);
2961 values = NULL_TREE;
2962 c_parser_consume_token (parser);
2963 while (true)
2964 {
2965 tree enum_id;
2966 tree enum_value;
2967 tree enum_decl;
2968 bool seen_comma;
2969 c_token *token;
2970 location_t comma_loc = UNKNOWN_LOCATION; /* Quiet warning. */
2971 location_t decl_loc, value_loc;
2972 if (c_parser_next_token_is_not (parser, CPP_NAME))
2973 {
2974 /* Give a nicer error for "enum {}". */
2975 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE)
2976 && !parser->error)
2977 {
2978 error_at (c_parser_peek_token (parser)->location,
2979 "empty enum is invalid");
2980 parser->error = true;
2981 }
2982 else
2983 c_parser_error (parser, "expected identifier");
2984 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2985 values = error_mark_node;
2986 break;
2987 }
2988 token = c_parser_peek_token (parser);
2989 enum_id = token->value;
2990 /* Set the location in case we create a decl now. */
2991 c_parser_set_source_position_from_token (token);
2992 decl_loc = value_loc = token->location;
2993 c_parser_consume_token (parser);
2994 /* Parse any specified attributes. */
2995 tree enum_attrs = c_parser_attributes (parser);
2996 if (c_parser_next_token_is (parser, CPP_EQ))
2997 {
2998 c_parser_consume_token (parser);
2999 value_loc = c_parser_peek_token (parser)->location;
3000 enum_value = c_parser_expr_no_commas (parser, NULL).value;
3001 }
3002 else
3003 enum_value = NULL_TREE;
3004 enum_decl = build_enumerator (decl_loc, value_loc,
3005 &the_enum, enum_id, enum_value);
3006 if (enum_attrs)
3007 decl_attributes (&TREE_PURPOSE (enum_decl), enum_attrs, 0);
3008 TREE_CHAIN (enum_decl) = values;
3009 values = enum_decl;
3010 seen_comma = false;
3011 if (c_parser_next_token_is (parser, CPP_COMMA))
3012 {
3013 comma_loc = c_parser_peek_token (parser)->location;
3014 seen_comma = true;
3015 c_parser_consume_token (parser);
3016 }
3017 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3018 {
3019 if (seen_comma)
3020 pedwarn_c90 (comma_loc, OPT_Wpedantic,
3021 "comma at end of enumerator list");
3022 c_parser_consume_token (parser);
3023 break;
3024 }
3025 if (!seen_comma)
3026 {
3027 c_parser_error (parser, "expected %<,%> or %<}%>");
3028 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
3029 values = error_mark_node;
3030 break;
3031 }
3032 }
3033 postfix_attrs = c_parser_attributes (parser);
3034 ret.spec = finish_enum (type, nreverse (values),
3035 chainon (attrs, postfix_attrs));
3036 ret.kind = ctsk_tagdef;
3037 ret.expr = NULL_TREE;
3038 ret.expr_const_operands = true;
3039 timevar_pop (TV_PARSE_ENUM);
3040 return ret;
3041 }
3042 else if (!ident)
3043 {
3044 c_parser_error (parser, "expected %<{%>");
3045 ret.spec = error_mark_node;
3046 ret.kind = ctsk_tagref;
3047 ret.expr = NULL_TREE;
3048 ret.expr_const_operands = true;
3049 return ret;
3050 }
3051 ret = parser_xref_tag (ident_loc, ENUMERAL_TYPE, ident);
3052 /* In ISO C, enumerated types can be referred to only if already
3053 defined. */
3054 if (pedantic && !COMPLETE_TYPE_P (ret.spec))
3055 {
3056 gcc_assert (ident);
3057 pedwarn (enum_loc, OPT_Wpedantic,
3058 "ISO C forbids forward references to %<enum%> types");
3059 }
3060 return ret;
3061 }
3062
3063 /* Parse a struct or union specifier (C90 6.5.2.1, C99 6.7.2.1, C11 6.7.2.1).
3064
3065 struct-or-union-specifier:
3066 struct-or-union attributes[opt] identifier[opt]
3067 { struct-contents } attributes[opt]
3068 struct-or-union attributes[opt] identifier
3069
3070 struct-contents:
3071 struct-declaration-list
3072
3073 struct-declaration-list:
3074 struct-declaration ;
3075 struct-declaration-list struct-declaration ;
3076
3077 GNU extensions:
3078
3079 struct-contents:
3080 empty
3081 struct-declaration
3082 struct-declaration-list struct-declaration
3083
3084 struct-declaration-list:
3085 struct-declaration-list ;
3086 ;
3087
3088 (Note that in the syntax here, unlike that in ISO C, the semicolons
3089 are included here rather than in struct-declaration, in order to
3090 describe the syntax with extra semicolons and missing semicolon at
3091 end.)
3092
3093 Objective-C:
3094
3095 struct-declaration-list:
3096 @defs ( class-name )
3097
3098 (Note this does not include a trailing semicolon, but can be
3099 followed by further declarations, and gets a pedwarn-if-pedantic
3100 when followed by a semicolon.) */
3101
3102 static struct c_typespec
3103 c_parser_struct_or_union_specifier (c_parser *parser)
3104 {
3105 struct c_typespec ret;
3106 tree attrs;
3107 tree ident = NULL_TREE;
3108 location_t struct_loc;
3109 location_t ident_loc = UNKNOWN_LOCATION;
3110 enum tree_code code;
3111 switch (c_parser_peek_token (parser)->keyword)
3112 {
3113 case RID_STRUCT:
3114 code = RECORD_TYPE;
3115 break;
3116 case RID_UNION:
3117 code = UNION_TYPE;
3118 break;
3119 default:
3120 gcc_unreachable ();
3121 }
3122 struct_loc = c_parser_peek_token (parser)->location;
3123 c_parser_consume_token (parser);
3124 attrs = c_parser_attributes (parser);
3125
3126 /* Set the location in case we create a decl now. */
3127 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
3128
3129 if (c_parser_next_token_is (parser, CPP_NAME))
3130 {
3131 ident = c_parser_peek_token (parser)->value;
3132 ident_loc = c_parser_peek_token (parser)->location;
3133 struct_loc = ident_loc;
3134 c_parser_consume_token (parser);
3135 }
3136 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
3137 {
3138 /* Parse a struct or union definition. Start the scope of the
3139 tag before parsing components. */
3140 struct c_struct_parse_info *struct_info;
3141 tree type = start_struct (struct_loc, code, ident, &struct_info);
3142 tree postfix_attrs;
3143 /* We chain the components in reverse order, then put them in
3144 forward order at the end. Each struct-declaration may
3145 declare multiple components (comma-separated), so we must use
3146 chainon to join them, although when parsing each
3147 struct-declaration we can use TREE_CHAIN directly.
3148
3149 The theory behind all this is that there will be more
3150 semicolon separated fields than comma separated fields, and
3151 so we'll be minimizing the number of node traversals required
3152 by chainon. */
3153 tree contents;
3154 timevar_push (TV_PARSE_STRUCT);
3155 contents = NULL_TREE;
3156 c_parser_consume_token (parser);
3157 /* Handle the Objective-C @defs construct,
3158 e.g. foo(sizeof(struct{ @defs(ClassName) }));. */
3159 if (c_parser_next_token_is_keyword (parser, RID_AT_DEFS))
3160 {
3161 tree name;
3162 gcc_assert (c_dialect_objc ());
3163 c_parser_consume_token (parser);
3164 matching_parens parens;
3165 if (!parens.require_open (parser))
3166 goto end_at_defs;
3167 if (c_parser_next_token_is (parser, CPP_NAME)
3168 && c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME)
3169 {
3170 name = c_parser_peek_token (parser)->value;
3171 c_parser_consume_token (parser);
3172 }
3173 else
3174 {
3175 c_parser_error (parser, "expected class name");
3176 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3177 goto end_at_defs;
3178 }
3179 parens.skip_until_found_close (parser);
3180 contents = nreverse (objc_get_class_ivars (name));
3181 }
3182 end_at_defs:
3183 /* Parse the struct-declarations and semicolons. Problems with
3184 semicolons are diagnosed here; empty structures are diagnosed
3185 elsewhere. */
3186 while (true)
3187 {
3188 tree decls;
3189 /* Parse any stray semicolon. */
3190 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3191 {
3192 location_t semicolon_loc
3193 = c_parser_peek_token (parser)->location;
3194 gcc_rich_location richloc (semicolon_loc);
3195 richloc.add_fixit_remove ();
3196 pedwarn (&richloc, OPT_Wpedantic,
3197 "extra semicolon in struct or union specified");
3198 c_parser_consume_token (parser);
3199 continue;
3200 }
3201 /* Stop if at the end of the struct or union contents. */
3202 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3203 {
3204 c_parser_consume_token (parser);
3205 break;
3206 }
3207 /* Accept #pragmas at struct scope. */
3208 if (c_parser_next_token_is (parser, CPP_PRAGMA))
3209 {
3210 c_parser_pragma (parser, pragma_struct, NULL);
3211 continue;
3212 }
3213 /* Parse some comma-separated declarations, but not the
3214 trailing semicolon if any. */
3215 decls = c_parser_struct_declaration (parser);
3216 contents = chainon (decls, contents);
3217 /* If no semicolon follows, either we have a parse error or
3218 are at the end of the struct or union and should
3219 pedwarn. */
3220 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3221 c_parser_consume_token (parser);
3222 else
3223 {
3224 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3225 pedwarn (c_parser_peek_token (parser)->location, 0,
3226 "no semicolon at end of struct or union");
3227 else if (parser->error
3228 || !c_parser_next_token_starts_declspecs (parser))
3229 {
3230 c_parser_error (parser, "expected %<;%>");
3231 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
3232 break;
3233 }
3234
3235 /* If we come here, we have already emitted an error
3236 for an expected `;', identifier or `(', and we also
3237 recovered already. Go on with the next field. */
3238 }
3239 }
3240 postfix_attrs = c_parser_attributes (parser);
3241 ret.spec = finish_struct (struct_loc, type, nreverse (contents),
3242 chainon (attrs, postfix_attrs), struct_info);
3243 ret.kind = ctsk_tagdef;
3244 ret.expr = NULL_TREE;
3245 ret.expr_const_operands = true;
3246 timevar_pop (TV_PARSE_STRUCT);
3247 return ret;
3248 }
3249 else if (!ident)
3250 {
3251 c_parser_error (parser, "expected %<{%>");
3252 ret.spec = error_mark_node;
3253 ret.kind = ctsk_tagref;
3254 ret.expr = NULL_TREE;
3255 ret.expr_const_operands = true;
3256 return ret;
3257 }
3258 ret = parser_xref_tag (ident_loc, code, ident);
3259 return ret;
3260 }
3261
3262 /* Parse a struct-declaration (C90 6.5.2.1, C99 6.7.2.1, C11 6.7.2.1),
3263 *without* the trailing semicolon.
3264
3265 struct-declaration:
3266 specifier-qualifier-list struct-declarator-list
3267 static_assert-declaration-no-semi
3268
3269 specifier-qualifier-list:
3270 type-specifier specifier-qualifier-list[opt]
3271 type-qualifier specifier-qualifier-list[opt]
3272 alignment-specifier specifier-qualifier-list[opt]
3273 attributes specifier-qualifier-list[opt]
3274
3275 struct-declarator-list:
3276 struct-declarator
3277 struct-declarator-list , attributes[opt] struct-declarator
3278
3279 struct-declarator:
3280 declarator attributes[opt]
3281 declarator[opt] : constant-expression attributes[opt]
3282
3283 GNU extensions:
3284
3285 struct-declaration:
3286 __extension__ struct-declaration
3287 specifier-qualifier-list
3288
3289 Unlike the ISO C syntax, semicolons are handled elsewhere. The use
3290 of attributes where shown is a GNU extension. In GNU C, we accept
3291 any expression without commas in the syntax (assignment
3292 expressions, not just conditional expressions); assignment
3293 expressions will be diagnosed as non-constant. */
3294
3295 static tree
3296 c_parser_struct_declaration (c_parser *parser)
3297 {
3298 struct c_declspecs *specs;
3299 tree prefix_attrs;
3300 tree all_prefix_attrs;
3301 tree decls;
3302 location_t decl_loc;
3303 if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
3304 {
3305 int ext;
3306 tree decl;
3307 ext = disable_extension_diagnostics ();
3308 c_parser_consume_token (parser);
3309 decl = c_parser_struct_declaration (parser);
3310 restore_extension_diagnostics (ext);
3311 return decl;
3312 }
3313 if (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
3314 {
3315 c_parser_static_assert_declaration_no_semi (parser);
3316 return NULL_TREE;
3317 }
3318 specs = build_null_declspecs ();
3319 decl_loc = c_parser_peek_token (parser)->location;
3320 /* Strictly by the standard, we shouldn't allow _Alignas here,
3321 but it appears to have been intended to allow it there, so
3322 we're keeping it as it is until WG14 reaches a conclusion
3323 of N1731.
3324 <http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1731.pdf> */
3325 c_parser_declspecs (parser, specs, false, true, true,
3326 true, false, cla_nonabstract_decl);
3327 if (parser->error)
3328 return NULL_TREE;
3329 if (!specs->declspecs_seen_p)
3330 {
3331 c_parser_error (parser, "expected specifier-qualifier-list");
3332 return NULL_TREE;
3333 }
3334 finish_declspecs (specs);
3335 if (c_parser_next_token_is (parser, CPP_SEMICOLON)
3336 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3337 {
3338 tree ret;
3339 if (specs->typespec_kind == ctsk_none)
3340 {
3341 pedwarn (decl_loc, OPT_Wpedantic,
3342 "ISO C forbids member declarations with no members");
3343 shadow_tag_warned (specs, pedantic);
3344 ret = NULL_TREE;
3345 }
3346 else
3347 {
3348 /* Support for unnamed structs or unions as members of
3349 structs or unions (which is [a] useful and [b] supports
3350 MS P-SDK). */
3351 tree attrs = NULL;
3352
3353 ret = grokfield (c_parser_peek_token (parser)->location,
3354 build_id_declarator (NULL_TREE), specs,
3355 NULL_TREE, &attrs);
3356 if (ret)
3357 decl_attributes (&ret, attrs, 0);
3358 }
3359 return ret;
3360 }
3361
3362 /* Provide better error recovery. Note that a type name here is valid,
3363 and will be treated as a field name. */
3364 if (specs->typespec_kind == ctsk_tagdef
3365 && TREE_CODE (specs->type) != ENUMERAL_TYPE
3366 && c_parser_next_token_starts_declspecs (parser)
3367 && !c_parser_next_token_is (parser, CPP_NAME))
3368 {
3369 c_parser_error (parser, "expected %<;%>, identifier or %<(%>");
3370 parser->error = false;
3371 return NULL_TREE;
3372 }
3373
3374 pending_xref_error ();
3375 prefix_attrs = specs->attrs;
3376 all_prefix_attrs = prefix_attrs;
3377 specs->attrs = NULL_TREE;
3378 decls = NULL_TREE;
3379 while (true)
3380 {
3381 /* Declaring one or more declarators or un-named bit-fields. */
3382 struct c_declarator *declarator;
3383 bool dummy = false;
3384 if (c_parser_next_token_is (parser, CPP_COLON))
3385 declarator = build_id_declarator (NULL_TREE);
3386 else
3387 declarator = c_parser_declarator (parser,
3388 specs->typespec_kind != ctsk_none,
3389 C_DTR_NORMAL, &dummy);
3390 if (declarator == NULL)
3391 {
3392 c_parser_skip_to_end_of_block_or_statement (parser);
3393 break;
3394 }
3395 if (c_parser_next_token_is (parser, CPP_COLON)
3396 || c_parser_next_token_is (parser, CPP_COMMA)
3397 || c_parser_next_token_is (parser, CPP_SEMICOLON)
3398 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE)
3399 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3400 {
3401 tree postfix_attrs = NULL_TREE;
3402 tree width = NULL_TREE;
3403 tree d;
3404 if (c_parser_next_token_is (parser, CPP_COLON))
3405 {
3406 c_parser_consume_token (parser);
3407 width = c_parser_expr_no_commas (parser, NULL).value;
3408 }
3409 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3410 postfix_attrs = c_parser_attributes (parser);
3411 d = grokfield (c_parser_peek_token (parser)->location,
3412 declarator, specs, width, &all_prefix_attrs);
3413 decl_attributes (&d, chainon (postfix_attrs,
3414 all_prefix_attrs), 0);
3415 DECL_CHAIN (d) = decls;
3416 decls = d;
3417 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3418 all_prefix_attrs = chainon (c_parser_attributes (parser),
3419 prefix_attrs);
3420 else
3421 all_prefix_attrs = prefix_attrs;
3422 if (c_parser_next_token_is (parser, CPP_COMMA))
3423 c_parser_consume_token (parser);
3424 else if (c_parser_next_token_is (parser, CPP_SEMICOLON)
3425 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3426 {
3427 /* Semicolon consumed in caller. */
3428 break;
3429 }
3430 else
3431 {
3432 c_parser_error (parser, "expected %<,%>, %<;%> or %<}%>");
3433 break;
3434 }
3435 }
3436 else
3437 {
3438 c_parser_error (parser,
3439 "expected %<:%>, %<,%>, %<;%>, %<}%> or "
3440 "%<__attribute__%>");
3441 break;
3442 }
3443 }
3444 return decls;
3445 }
3446
3447 /* Parse a typeof specifier (a GNU extension).
3448
3449 typeof-specifier:
3450 typeof ( expression )
3451 typeof ( type-name )
3452 */
3453
3454 static struct c_typespec
3455 c_parser_typeof_specifier (c_parser *parser)
3456 {
3457 struct c_typespec ret;
3458 ret.kind = ctsk_typeof;
3459 ret.spec = error_mark_node;
3460 ret.expr = NULL_TREE;
3461 ret.expr_const_operands = true;
3462 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TYPEOF));
3463 c_parser_consume_token (parser);
3464 c_inhibit_evaluation_warnings++;
3465 in_typeof++;
3466 matching_parens parens;
3467 if (!parens.require_open (parser))
3468 {
3469 c_inhibit_evaluation_warnings--;
3470 in_typeof--;
3471 return ret;
3472 }
3473 if (c_parser_next_tokens_start_typename (parser, cla_prefer_id))
3474 {
3475 struct c_type_name *type = c_parser_type_name (parser);
3476 c_inhibit_evaluation_warnings--;
3477 in_typeof--;
3478 if (type != NULL)
3479 {
3480 ret.spec = groktypename (type, &ret.expr, &ret.expr_const_operands);
3481 pop_maybe_used (variably_modified_type_p (ret.spec, NULL_TREE));
3482 }
3483 }
3484 else
3485 {
3486 bool was_vm;
3487 location_t here = c_parser_peek_token (parser)->location;
3488 struct c_expr expr = c_parser_expression (parser);
3489 c_inhibit_evaluation_warnings--;
3490 in_typeof--;
3491 if (TREE_CODE (expr.value) == COMPONENT_REF
3492 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
3493 error_at (here, "%<typeof%> applied to a bit-field");
3494 mark_exp_read (expr.value);
3495 ret.spec = TREE_TYPE (expr.value);
3496 was_vm = variably_modified_type_p (ret.spec, NULL_TREE);
3497 /* This is returned with the type so that when the type is
3498 evaluated, this can be evaluated. */
3499 if (was_vm)
3500 ret.expr = c_fully_fold (expr.value, false, &ret.expr_const_operands);
3501 pop_maybe_used (was_vm);
3502 /* For use in macros such as those in <stdatomic.h>, remove all
3503 qualifiers from atomic types. (const can be an issue for more macros
3504 using typeof than just the <stdatomic.h> ones.) */
3505 if (ret.spec != error_mark_node && TYPE_ATOMIC (ret.spec))
3506 ret.spec = c_build_qualified_type (ret.spec, TYPE_UNQUALIFIED);
3507 }
3508 parens.skip_until_found_close (parser);
3509 return ret;
3510 }
3511
3512 /* Parse an alignment-specifier.
3513
3514 C11 6.7.5:
3515
3516 alignment-specifier:
3517 _Alignas ( type-name )
3518 _Alignas ( constant-expression )
3519 */
3520
3521 static tree
3522 c_parser_alignas_specifier (c_parser * parser)
3523 {
3524 tree ret = error_mark_node;
3525 location_t loc = c_parser_peek_token (parser)->location;
3526 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNAS));
3527 c_parser_consume_token (parser);
3528 if (flag_isoc99)
3529 pedwarn_c99 (loc, OPT_Wpedantic,
3530 "ISO C99 does not support %<_Alignas%>");
3531 else
3532 pedwarn_c99 (loc, OPT_Wpedantic,
3533 "ISO C90 does not support %<_Alignas%>");
3534 matching_parens parens;
3535 if (!parens.require_open (parser))
3536 return ret;
3537 if (c_parser_next_tokens_start_typename (parser, cla_prefer_id))
3538 {
3539 struct c_type_name *type = c_parser_type_name (parser);
3540 if (type != NULL)
3541 ret = c_sizeof_or_alignof_type (loc, groktypename (type, NULL, NULL),
3542 false, true, 1);
3543 }
3544 else
3545 ret = c_parser_expr_no_commas (parser, NULL).value;
3546 parens.skip_until_found_close (parser);
3547 return ret;
3548 }
3549
3550 /* Parse a declarator, possibly an abstract declarator (C90 6.5.4,
3551 6.5.5, C99 6.7.5, 6.7.6, C11 6.7.6, 6.7.7). If TYPE_SEEN_P then
3552 a typedef name may be redeclared; otherwise it may not. KIND
3553 indicates which kind of declarator is wanted. Returns a valid
3554 declarator except in the case of a syntax error in which case NULL is
3555 returned. *SEEN_ID is set to true if an identifier being declared is
3556 seen; this is used to diagnose bad forms of abstract array declarators
3557 and to determine whether an identifier list is syntactically permitted.
3558
3559 declarator:
3560 pointer[opt] direct-declarator
3561
3562 direct-declarator:
3563 identifier
3564 ( attributes[opt] declarator )
3565 direct-declarator array-declarator
3566 direct-declarator ( parameter-type-list )
3567 direct-declarator ( identifier-list[opt] )
3568
3569 pointer:
3570 * type-qualifier-list[opt]
3571 * type-qualifier-list[opt] pointer
3572
3573 type-qualifier-list:
3574 type-qualifier
3575 attributes
3576 type-qualifier-list type-qualifier
3577 type-qualifier-list attributes
3578
3579 array-declarator:
3580 [ type-qualifier-list[opt] assignment-expression[opt] ]
3581 [ static type-qualifier-list[opt] assignment-expression ]
3582 [ type-qualifier-list static assignment-expression ]
3583 [ type-qualifier-list[opt] * ]
3584
3585 parameter-type-list:
3586 parameter-list
3587 parameter-list , ...
3588
3589 parameter-list:
3590 parameter-declaration
3591 parameter-list , parameter-declaration
3592
3593 parameter-declaration:
3594 declaration-specifiers declarator attributes[opt]
3595 declaration-specifiers abstract-declarator[opt] attributes[opt]
3596
3597 identifier-list:
3598 identifier
3599 identifier-list , identifier
3600
3601 abstract-declarator:
3602 pointer
3603 pointer[opt] direct-abstract-declarator
3604
3605 direct-abstract-declarator:
3606 ( attributes[opt] abstract-declarator )
3607 direct-abstract-declarator[opt] array-declarator
3608 direct-abstract-declarator[opt] ( parameter-type-list[opt] )
3609
3610 GNU extensions:
3611
3612 direct-declarator:
3613 direct-declarator ( parameter-forward-declarations
3614 parameter-type-list[opt] )
3615
3616 direct-abstract-declarator:
3617 direct-abstract-declarator[opt] ( parameter-forward-declarations
3618 parameter-type-list[opt] )
3619
3620 parameter-forward-declarations:
3621 parameter-list ;
3622 parameter-forward-declarations parameter-list ;
3623
3624 The uses of attributes shown above are GNU extensions.
3625
3626 Some forms of array declarator are not included in C99 in the
3627 syntax for abstract declarators; these are disallowed elsewhere.
3628 This may be a defect (DR#289).
3629
3630 This function also accepts an omitted abstract declarator as being
3631 an abstract declarator, although not part of the formal syntax. */
3632
3633 struct c_declarator *
3634 c_parser_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
3635 bool *seen_id)
3636 {
3637 /* Parse any initial pointer part. */
3638 if (c_parser_next_token_is (parser, CPP_MULT))
3639 {
3640 struct c_declspecs *quals_attrs = build_null_declspecs ();
3641 struct c_declarator *inner;
3642 c_parser_consume_token (parser);
3643 c_parser_declspecs (parser, quals_attrs, false, false, true,
3644 false, false, cla_prefer_id);
3645 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
3646 if (inner == NULL)
3647 return NULL;
3648 else
3649 return make_pointer_declarator (quals_attrs, inner);
3650 }
3651 /* Now we have a direct declarator, direct abstract declarator or
3652 nothing (which counts as a direct abstract declarator here). */
3653 return c_parser_direct_declarator (parser, type_seen_p, kind, seen_id);
3654 }
3655
3656 /* Parse a direct declarator or direct abstract declarator; arguments
3657 as c_parser_declarator. */
3658
3659 static struct c_declarator *
3660 c_parser_direct_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
3661 bool *seen_id)
3662 {
3663 /* The direct declarator must start with an identifier (possibly
3664 omitted) or a parenthesized declarator (possibly abstract). In
3665 an ordinary declarator, initial parentheses must start a
3666 parenthesized declarator. In an abstract declarator or parameter
3667 declarator, they could start a parenthesized declarator or a
3668 parameter list. To tell which, the open parenthesis and any
3669 following attributes must be read. If a declaration specifier
3670 follows, then it is a parameter list; if the specifier is a
3671 typedef name, there might be an ambiguity about redeclaring it,
3672 which is resolved in the direction of treating it as a typedef
3673 name. If a close parenthesis follows, it is also an empty
3674 parameter list, as the syntax does not permit empty abstract
3675 declarators. Otherwise, it is a parenthesized declarator (in
3676 which case the analysis may be repeated inside it, recursively).
3677
3678 ??? There is an ambiguity in a parameter declaration "int
3679 (__attribute__((foo)) x)", where x is not a typedef name: it
3680 could be an abstract declarator for a function, or declare x with
3681 parentheses. The proper resolution of this ambiguity needs
3682 documenting. At present we follow an accident of the old
3683 parser's implementation, whereby the first parameter must have
3684 some declaration specifiers other than just attributes. Thus as
3685 a parameter declaration it is treated as a parenthesized
3686 parameter named x, and as an abstract declarator it is
3687 rejected.
3688
3689 ??? Also following the old parser, attributes inside an empty
3690 parameter list are ignored, making it a list not yielding a
3691 prototype, rather than giving an error or making it have one
3692 parameter with implicit type int.
3693
3694 ??? Also following the old parser, typedef names may be
3695 redeclared in declarators, but not Objective-C class names. */
3696
3697 if (kind != C_DTR_ABSTRACT
3698 && c_parser_next_token_is (parser, CPP_NAME)
3699 && ((type_seen_p
3700 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
3701 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
3702 || c_parser_peek_token (parser)->id_kind == C_ID_ID))
3703 {
3704 struct c_declarator *inner
3705 = build_id_declarator (c_parser_peek_token (parser)->value);
3706 *seen_id = true;
3707 inner->id_loc = c_parser_peek_token (parser)->location;
3708 c_parser_consume_token (parser);
3709 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3710 }
3711
3712 if (kind != C_DTR_NORMAL
3713 && c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
3714 {
3715 struct c_declarator *inner = build_id_declarator (NULL_TREE);
3716 inner->id_loc = c_parser_peek_token (parser)->location;
3717 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3718 }
3719
3720 /* Either we are at the end of an abstract declarator, or we have
3721 parentheses. */
3722
3723 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
3724 {
3725 tree attrs;
3726 struct c_declarator *inner;
3727 c_parser_consume_token (parser);
3728 attrs = c_parser_attributes (parser);
3729 if (kind != C_DTR_NORMAL
3730 && (c_parser_next_token_starts_declspecs (parser)
3731 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN)))
3732 {
3733 struct c_arg_info *args
3734 = c_parser_parms_declarator (parser, kind == C_DTR_NORMAL,
3735 attrs);
3736 if (args == NULL)
3737 return NULL;
3738 else
3739 {
3740 inner
3741 = build_function_declarator (args,
3742 build_id_declarator (NULL_TREE));
3743 return c_parser_direct_declarator_inner (parser, *seen_id,
3744 inner);
3745 }
3746 }
3747 /* A parenthesized declarator. */
3748 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
3749 if (inner != NULL && attrs != NULL)
3750 inner = build_attrs_declarator (attrs, inner);
3751 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3752 {
3753 c_parser_consume_token (parser);
3754 if (inner == NULL)
3755 return NULL;
3756 else
3757 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3758 }
3759 else
3760 {
3761 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3762 "expected %<)%>");
3763 return NULL;
3764 }
3765 }
3766 else
3767 {
3768 if (kind == C_DTR_NORMAL)
3769 {
3770 c_parser_error (parser, "expected identifier or %<(%>");
3771 return NULL;
3772 }
3773 else
3774 return build_id_declarator (NULL_TREE);
3775 }
3776 }
3777
3778 /* Parse part of a direct declarator or direct abstract declarator,
3779 given that some (in INNER) has already been parsed; ID_PRESENT is
3780 true if an identifier is present, false for an abstract
3781 declarator. */
3782
3783 static struct c_declarator *
3784 c_parser_direct_declarator_inner (c_parser *parser, bool id_present,
3785 struct c_declarator *inner)
3786 {
3787 /* Parse a sequence of array declarators and parameter lists. */
3788 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
3789 {
3790 location_t brace_loc = c_parser_peek_token (parser)->location;
3791 struct c_declarator *declarator;
3792 struct c_declspecs *quals_attrs = build_null_declspecs ();
3793 bool static_seen;
3794 bool star_seen;
3795 struct c_expr dimen;
3796 dimen.value = NULL_TREE;
3797 dimen.original_code = ERROR_MARK;
3798 dimen.original_type = NULL_TREE;
3799 c_parser_consume_token (parser);
3800 c_parser_declspecs (parser, quals_attrs, false, false, true,
3801 false, false, cla_prefer_id);
3802 static_seen = c_parser_next_token_is_keyword (parser, RID_STATIC);
3803 if (static_seen)
3804 c_parser_consume_token (parser);
3805 if (static_seen && !quals_attrs->declspecs_seen_p)
3806 c_parser_declspecs (parser, quals_attrs, false, false, true,
3807 false, false, cla_prefer_id);
3808 if (!quals_attrs->declspecs_seen_p)
3809 quals_attrs = NULL;
3810 /* If "static" is present, there must be an array dimension.
3811 Otherwise, there may be a dimension, "*", or no
3812 dimension. */
3813 if (static_seen)
3814 {
3815 star_seen = false;
3816 dimen = c_parser_expr_no_commas (parser, NULL);
3817 }
3818 else
3819 {
3820 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3821 {
3822 dimen.value = NULL_TREE;
3823 star_seen = false;
3824 }
3825 else if (c_parser_next_token_is (parser, CPP_MULT))
3826 {
3827 if (c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_SQUARE)
3828 {
3829 dimen.value = NULL_TREE;
3830 star_seen = true;
3831 c_parser_consume_token (parser);
3832 }
3833 else
3834 {
3835 star_seen = false;
3836 dimen = c_parser_expr_no_commas (parser, NULL);
3837 }
3838 }
3839 else
3840 {
3841 star_seen = false;
3842 dimen = c_parser_expr_no_commas (parser, NULL);
3843 }
3844 }
3845 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3846 c_parser_consume_token (parser);
3847 else
3848 {
3849 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3850 "expected %<]%>");
3851 return NULL;
3852 }
3853 if (dimen.value)
3854 dimen = convert_lvalue_to_rvalue (brace_loc, dimen, true, true);
3855 declarator = build_array_declarator (brace_loc, dimen.value, quals_attrs,
3856 static_seen, star_seen);
3857 if (declarator == NULL)
3858 return NULL;
3859 inner = set_array_declarator_inner (declarator, inner);
3860 return c_parser_direct_declarator_inner (parser, id_present, inner);
3861 }
3862 else if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
3863 {
3864 tree attrs;
3865 struct c_arg_info *args;
3866 c_parser_consume_token (parser);
3867 attrs = c_parser_attributes (parser);
3868 args = c_parser_parms_declarator (parser, id_present, attrs);
3869 if (args == NULL)
3870 return NULL;
3871 else
3872 {
3873 inner = build_function_declarator (args, inner);
3874 return c_parser_direct_declarator_inner (parser, id_present, inner);
3875 }
3876 }
3877 return inner;
3878 }
3879
3880 /* Parse a parameter list or identifier list, including the closing
3881 parenthesis but not the opening one. ATTRS are the attributes at
3882 the start of the list. ID_LIST_OK is true if an identifier list is
3883 acceptable; such a list must not have attributes at the start. */
3884
3885 static struct c_arg_info *
3886 c_parser_parms_declarator (c_parser *parser, bool id_list_ok, tree attrs)
3887 {
3888 push_scope ();
3889 declare_parm_level ();
3890 /* If the list starts with an identifier, it is an identifier list.
3891 Otherwise, it is either a prototype list or an empty list. */
3892 if (id_list_ok
3893 && !attrs
3894 && c_parser_next_token_is (parser, CPP_NAME)
3895 && c_parser_peek_token (parser)->id_kind == C_ID_ID
3896
3897 /* Look ahead to detect typos in type names. */
3898 && c_parser_peek_2nd_token (parser)->type != CPP_NAME
3899 && c_parser_peek_2nd_token (parser)->type != CPP_MULT
3900 && c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN
3901 && c_parser_peek_2nd_token (parser)->type != CPP_OPEN_SQUARE
3902 && c_parser_peek_2nd_token (parser)->type != CPP_KEYWORD)
3903 {
3904 tree list = NULL_TREE, *nextp = &list;
3905 while (c_parser_next_token_is (parser, CPP_NAME)
3906 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
3907 {
3908 *nextp = build_tree_list (NULL_TREE,
3909 c_parser_peek_token (parser)->value);
3910 nextp = & TREE_CHAIN (*nextp);
3911 c_parser_consume_token (parser);
3912 if (c_parser_next_token_is_not (parser, CPP_COMMA))
3913 break;
3914 c_parser_consume_token (parser);
3915 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3916 {
3917 c_parser_error (parser, "expected identifier");
3918 break;
3919 }
3920 }
3921 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3922 {
3923 struct c_arg_info *ret = build_arg_info ();
3924 ret->types = list;
3925 c_parser_consume_token (parser);
3926 pop_scope ();
3927 return ret;
3928 }
3929 else
3930 {
3931 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3932 "expected %<)%>");
3933 pop_scope ();
3934 return NULL;
3935 }
3936 }
3937 else
3938 {
3939 struct c_arg_info *ret = c_parser_parms_list_declarator (parser, attrs,
3940 NULL);
3941 pop_scope ();
3942 return ret;
3943 }
3944 }
3945
3946 /* Parse a parameter list (possibly empty), including the closing
3947 parenthesis but not the opening one. ATTRS are the attributes at
3948 the start of the list. EXPR is NULL or an expression that needs to
3949 be evaluated for the side effects of array size expressions in the
3950 parameters. */
3951
3952 static struct c_arg_info *
3953 c_parser_parms_list_declarator (c_parser *parser, tree attrs, tree expr)
3954 {
3955 bool bad_parm = false;
3956
3957 /* ??? Following the old parser, forward parameter declarations may
3958 use abstract declarators, and if no real parameter declarations
3959 follow the forward declarations then this is not diagnosed. Also
3960 note as above that attributes are ignored as the only contents of
3961 the parentheses, or as the only contents after forward
3962 declarations. */
3963 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3964 {
3965 struct c_arg_info *ret = build_arg_info ();
3966 c_parser_consume_token (parser);
3967 return ret;
3968 }
3969 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3970 {
3971 struct c_arg_info *ret = build_arg_info ();
3972
3973 if (flag_allow_parameterless_variadic_functions)
3974 {
3975 /* F (...) is allowed. */
3976 ret->types = NULL_TREE;
3977 }
3978 else
3979 {
3980 /* Suppress -Wold-style-definition for this case. */
3981 ret->types = error_mark_node;
3982 error_at (c_parser_peek_token (parser)->location,
3983 "ISO C requires a named argument before %<...%>");
3984 }
3985 c_parser_consume_token (parser);
3986 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3987 {
3988 c_parser_consume_token (parser);
3989 return ret;
3990 }
3991 else
3992 {
3993 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3994 "expected %<)%>");
3995 return NULL;
3996 }
3997 }
3998 /* Nonempty list of parameters, either terminated with semicolon
3999 (forward declarations; recurse) or with close parenthesis (normal
4000 function) or with ", ... )" (variadic function). */
4001 while (true)
4002 {
4003 /* Parse a parameter. */
4004 struct c_parm *parm = c_parser_parameter_declaration (parser, attrs);
4005 attrs = NULL_TREE;
4006 if (parm == NULL)
4007 bad_parm = true;
4008 else
4009 push_parm_decl (parm, &expr);
4010 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4011 {
4012 tree new_attrs;
4013 c_parser_consume_token (parser);
4014 mark_forward_parm_decls ();
4015 new_attrs = c_parser_attributes (parser);
4016 return c_parser_parms_list_declarator (parser, new_attrs, expr);
4017 }
4018 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4019 {
4020 c_parser_consume_token (parser);
4021 if (bad_parm)
4022 return NULL;
4023 else
4024 return get_parm_info (false, expr);
4025 }
4026 if (!c_parser_require (parser, CPP_COMMA,
4027 "expected %<;%>, %<,%> or %<)%>",
4028 UNKNOWN_LOCATION, false))
4029 {
4030 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4031 return NULL;
4032 }
4033 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
4034 {
4035 c_parser_consume_token (parser);
4036 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4037 {
4038 c_parser_consume_token (parser);
4039 if (bad_parm)
4040 return NULL;
4041 else
4042 return get_parm_info (true, expr);
4043 }
4044 else
4045 {
4046 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4047 "expected %<)%>");
4048 return NULL;
4049 }
4050 }
4051 }
4052 }
4053
4054 /* Parse a parameter declaration. ATTRS are the attributes at the
4055 start of the declaration if it is the first parameter. */
4056
4057 static struct c_parm *
4058 c_parser_parameter_declaration (c_parser *parser, tree attrs)
4059 {
4060 struct c_declspecs *specs;
4061 struct c_declarator *declarator;
4062 tree prefix_attrs;
4063 tree postfix_attrs = NULL_TREE;
4064 bool dummy = false;
4065
4066 /* Accept #pragmas between parameter declarations. */
4067 while (c_parser_next_token_is (parser, CPP_PRAGMA))
4068 c_parser_pragma (parser, pragma_param, NULL);
4069
4070 if (!c_parser_next_token_starts_declspecs (parser))
4071 {
4072 c_token *token = c_parser_peek_token (parser);
4073 if (parser->error)
4074 return NULL;
4075 c_parser_set_source_position_from_token (token);
4076 if (c_parser_next_tokens_start_typename (parser, cla_prefer_type))
4077 {
4078 auto_diagnostic_group d;
4079 name_hint hint = lookup_name_fuzzy (token->value,
4080 FUZZY_LOOKUP_TYPENAME,
4081 token->location);
4082 if (const char *suggestion = hint.suggestion ())
4083 {
4084 gcc_rich_location richloc (token->location);
4085 richloc.add_fixit_replace (suggestion);
4086 error_at (&richloc,
4087 "unknown type name %qE; did you mean %qs?",
4088 token->value, suggestion);
4089 }
4090 else
4091 error_at (token->location, "unknown type name %qE", token->value);
4092 parser->error = true;
4093 }
4094 /* ??? In some Objective-C cases '...' isn't applicable so there
4095 should be a different message. */
4096 else
4097 c_parser_error (parser,
4098 "expected declaration specifiers or %<...%>");
4099 c_parser_skip_to_end_of_parameter (parser);
4100 return NULL;
4101 }
4102
4103 location_t start_loc = c_parser_peek_token (parser)->location;
4104
4105 specs = build_null_declspecs ();
4106 if (attrs)
4107 {
4108 declspecs_add_attrs (input_location, specs, attrs);
4109 attrs = NULL_TREE;
4110 }
4111 c_parser_declspecs (parser, specs, true, true, true, true, false,
4112 cla_nonabstract_decl);
4113 finish_declspecs (specs);
4114 pending_xref_error ();
4115 prefix_attrs = specs->attrs;
4116 specs->attrs = NULL_TREE;
4117 declarator = c_parser_declarator (parser,
4118 specs->typespec_kind != ctsk_none,
4119 C_DTR_PARM, &dummy);
4120 if (declarator == NULL)
4121 {
4122 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
4123 return NULL;
4124 }
4125 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
4126 postfix_attrs = c_parser_attributes (parser);
4127
4128 /* Generate a location for the parameter, ranging from the start of the
4129 initial token to the end of the final token.
4130
4131 If we have a identifier, then use it for the caret location, e.g.
4132
4133 extern int callee (int one, int (*two)(int, int), float three);
4134 ~~~~~~^~~~~~~~~~~~~~
4135
4136 otherwise, reuse the start location for the caret location e.g.:
4137
4138 extern int callee (int one, int (*)(int, int), float three);
4139 ^~~~~~~~~~~~~~~~~
4140 */
4141 location_t end_loc = parser->last_token_location;
4142
4143 /* Find any cdk_id declarator; determine if we have an identifier. */
4144 c_declarator *id_declarator = declarator;
4145 while (id_declarator && id_declarator->kind != cdk_id)
4146 id_declarator = id_declarator->declarator;
4147 location_t caret_loc = (id_declarator->u.id
4148 ? id_declarator->id_loc
4149 : start_loc);
4150 location_t param_loc = make_location (caret_loc, start_loc, end_loc);
4151
4152 return build_c_parm (specs, chainon (postfix_attrs, prefix_attrs),
4153 declarator, param_loc);
4154 }
4155
4156 /* Parse a string literal in an asm expression. It should not be
4157 translated, and wide string literals are an error although
4158 permitted by the syntax. This is a GNU extension.
4159
4160 asm-string-literal:
4161 string-literal
4162
4163 ??? At present, following the old parser, the caller needs to have
4164 set lex_untranslated_string to 1. It would be better to follow the
4165 C++ parser rather than using this kludge. */
4166
4167 static tree
4168 c_parser_asm_string_literal (c_parser *parser)
4169 {
4170 tree str;
4171 int save_flag = warn_overlength_strings;
4172 warn_overlength_strings = 0;
4173 if (c_parser_next_token_is (parser, CPP_STRING))
4174 {
4175 str = c_parser_peek_token (parser)->value;
4176 c_parser_consume_token (parser);
4177 }
4178 else if (c_parser_next_token_is (parser, CPP_WSTRING))
4179 {
4180 error_at (c_parser_peek_token (parser)->location,
4181 "wide string literal in %<asm%>");
4182 str = build_string (1, "");
4183 c_parser_consume_token (parser);
4184 }
4185 else
4186 {
4187 c_parser_error (parser, "expected string literal");
4188 str = NULL_TREE;
4189 }
4190 warn_overlength_strings = save_flag;
4191 return str;
4192 }
4193
4194 /* Parse a simple asm expression. This is used in restricted
4195 contexts, where a full expression with inputs and outputs does not
4196 make sense. This is a GNU extension.
4197
4198 simple-asm-expr:
4199 asm ( asm-string-literal )
4200 */
4201
4202 static tree
4203 c_parser_simple_asm_expr (c_parser *parser)
4204 {
4205 tree str;
4206 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
4207 /* ??? Follow the C++ parser rather than using the
4208 lex_untranslated_string kludge. */
4209 parser->lex_untranslated_string = true;
4210 c_parser_consume_token (parser);
4211 matching_parens parens;
4212 if (!parens.require_open (parser))
4213 {
4214 parser->lex_untranslated_string = false;
4215 return NULL_TREE;
4216 }
4217 str = c_parser_asm_string_literal (parser);
4218 parser->lex_untranslated_string = false;
4219 if (!parens.require_close (parser))
4220 {
4221 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4222 return NULL_TREE;
4223 }
4224 return str;
4225 }
4226
4227 static tree
4228 c_parser_attribute_any_word (c_parser *parser)
4229 {
4230 tree attr_name = NULL_TREE;
4231
4232 if (c_parser_next_token_is (parser, CPP_KEYWORD))
4233 {
4234 /* ??? See comment above about what keywords are accepted here. */
4235 bool ok;
4236 switch (c_parser_peek_token (parser)->keyword)
4237 {
4238 case RID_STATIC:
4239 case RID_UNSIGNED:
4240 case RID_LONG:
4241 case RID_CONST:
4242 case RID_EXTERN:
4243 case RID_REGISTER:
4244 case RID_TYPEDEF:
4245 case RID_SHORT:
4246 case RID_INLINE:
4247 case RID_NORETURN:
4248 case RID_VOLATILE:
4249 case RID_SIGNED:
4250 case RID_AUTO:
4251 case RID_RESTRICT:
4252 case RID_COMPLEX:
4253 case RID_THREAD:
4254 case RID_INT:
4255 case RID_CHAR:
4256 case RID_FLOAT:
4257 case RID_DOUBLE:
4258 case RID_VOID:
4259 case RID_DFLOAT32:
4260 case RID_DFLOAT64:
4261 case RID_DFLOAT128:
4262 CASE_RID_FLOATN_NX:
4263 case RID_BOOL:
4264 case RID_FRACT:
4265 case RID_ACCUM:
4266 case RID_SAT:
4267 case RID_TRANSACTION_ATOMIC:
4268 case RID_TRANSACTION_CANCEL:
4269 case RID_ATOMIC:
4270 case RID_AUTO_TYPE:
4271 case RID_INT_N_0:
4272 case RID_INT_N_1:
4273 case RID_INT_N_2:
4274 case RID_INT_N_3:
4275 ok = true;
4276 break;
4277 default:
4278 ok = false;
4279 break;
4280 }
4281 if (!ok)
4282 return NULL_TREE;
4283
4284 /* Accept __attribute__((__const)) as __attribute__((const)) etc. */
4285 attr_name = ridpointers[(int) c_parser_peek_token (parser)->keyword];
4286 }
4287 else if (c_parser_next_token_is (parser, CPP_NAME))
4288 attr_name = c_parser_peek_token (parser)->value;
4289
4290 return attr_name;
4291 }
4292
4293 /* Parse (possibly empty) attributes. This is a GNU extension.
4294
4295 attributes:
4296 empty
4297 attributes attribute
4298
4299 attribute:
4300 __attribute__ ( ( attribute-list ) )
4301
4302 attribute-list:
4303 attrib
4304 attribute_list , attrib
4305
4306 attrib:
4307 empty
4308 any-word
4309 any-word ( identifier )
4310 any-word ( identifier , nonempty-expr-list )
4311 any-word ( expr-list )
4312
4313 where the "identifier" must not be declared as a type, and
4314 "any-word" may be any identifier (including one declared as a
4315 type), a reserved word storage class specifier, type specifier or
4316 type qualifier. ??? This still leaves out most reserved keywords
4317 (following the old parser), shouldn't we include them, and why not
4318 allow identifiers declared as types to start the arguments?
4319 When EXPECT_COMMA is true, expect the attribute to be preceded
4320 by a comma and fail if it isn't.
4321 When EMPTY_OK is true, allow and consume any number of consecutive
4322 commas with no attributes in between. */
4323
4324 static tree
4325 c_parser_attribute (c_parser *parser, tree attrs,
4326 bool expect_comma = false, bool empty_ok = true)
4327 {
4328 bool comma_first = c_parser_next_token_is (parser, CPP_COMMA);
4329 if (!comma_first
4330 && !c_parser_next_token_is (parser, CPP_NAME)
4331 && !c_parser_next_token_is (parser, CPP_KEYWORD))
4332 return NULL_TREE;
4333
4334 while (c_parser_next_token_is (parser, CPP_COMMA))
4335 {
4336 c_parser_consume_token (parser);
4337 if (!empty_ok)
4338 return attrs;
4339 }
4340
4341 tree attr_name = c_parser_attribute_any_word (parser);
4342 if (attr_name == NULL_TREE)
4343 return NULL_TREE;
4344
4345 attr_name = canonicalize_attr_name (attr_name);
4346 c_parser_consume_token (parser);
4347
4348 tree attr;
4349 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
4350 {
4351 if (expect_comma && !comma_first)
4352 {
4353 /* A comma is missing between the last attribute on the chain
4354 and this one. */
4355 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4356 "expected %<)%>");
4357 return error_mark_node;
4358 }
4359 attr = build_tree_list (attr_name, NULL_TREE);
4360 /* Add this attribute to the list. */
4361 attrs = chainon (attrs, attr);
4362 return attrs;
4363 }
4364 c_parser_consume_token (parser);
4365
4366 vec<tree, va_gc> *expr_list;
4367 tree attr_args;
4368 /* Parse the attribute contents. If they start with an
4369 identifier which is followed by a comma or close
4370 parenthesis, then the arguments start with that
4371 identifier; otherwise they are an expression list.
4372 In objective-c the identifier may be a classname. */
4373 if (c_parser_next_token_is (parser, CPP_NAME)
4374 && (c_parser_peek_token (parser)->id_kind == C_ID_ID
4375 || (c_dialect_objc ()
4376 && c_parser_peek_token (parser)->id_kind
4377 == C_ID_CLASSNAME))
4378 && ((c_parser_peek_2nd_token (parser)->type == CPP_COMMA)
4379 || (c_parser_peek_2nd_token (parser)->type
4380 == CPP_CLOSE_PAREN))
4381 && (attribute_takes_identifier_p (attr_name)
4382 || (c_dialect_objc ()
4383 && c_parser_peek_token (parser)->id_kind
4384 == C_ID_CLASSNAME)))
4385 {
4386 tree arg1 = c_parser_peek_token (parser)->value;
4387 c_parser_consume_token (parser);
4388 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4389 attr_args = build_tree_list (NULL_TREE, arg1);
4390 else
4391 {
4392 tree tree_list;
4393 c_parser_consume_token (parser);
4394 expr_list = c_parser_expr_list (parser, false, true,
4395 NULL, NULL, NULL, NULL);
4396 tree_list = build_tree_list_vec (expr_list);
4397 attr_args = tree_cons (NULL_TREE, arg1, tree_list);
4398 release_tree_vector (expr_list);
4399 }
4400 }
4401 else
4402 {
4403 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4404 attr_args = NULL_TREE;
4405 else
4406 {
4407 expr_list = c_parser_expr_list (parser, false, true,
4408 NULL, NULL, NULL, NULL);
4409 attr_args = build_tree_list_vec (expr_list);
4410 release_tree_vector (expr_list);
4411 }
4412 }
4413
4414 attr = build_tree_list (attr_name, attr_args);
4415 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4416 c_parser_consume_token (parser);
4417 else
4418 {
4419 parser->lex_untranslated_string = false;
4420 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4421 "expected %<)%>");
4422 return error_mark_node;
4423 }
4424
4425 if (expect_comma && !comma_first)
4426 {
4427 /* A comma is missing between the last attribute on the chain
4428 and this one. */
4429 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4430 "expected %<)%>");
4431 return error_mark_node;
4432 }
4433
4434 /* Add this attribute to the list. */
4435 attrs = chainon (attrs, attr);
4436 return attrs;
4437 }
4438
4439 static tree
4440 c_parser_attributes (c_parser *parser)
4441 {
4442 tree attrs = NULL_TREE;
4443 while (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
4444 {
4445 /* ??? Follow the C++ parser rather than using the
4446 lex_untranslated_string kludge. */
4447 parser->lex_untranslated_string = true;
4448 /* Consume the `__attribute__' keyword. */
4449 c_parser_consume_token (parser);
4450 /* Look for the two `(' tokens. */
4451 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4452 {
4453 parser->lex_untranslated_string = false;
4454 return attrs;
4455 }
4456 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4457 {
4458 parser->lex_untranslated_string = false;
4459 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4460 return attrs;
4461 }
4462 /* Parse the attribute list. Require a comma between successive
4463 (possibly empty) attributes. */
4464 for (bool expect_comma = false; ; expect_comma = true)
4465 {
4466 /* Parse a single attribute. */
4467 tree attr = c_parser_attribute (parser, attrs, expect_comma);
4468 if (attr == error_mark_node)
4469 return attrs;
4470 if (!attr)
4471 break;
4472 attrs = attr;
4473 }
4474
4475 /* Look for the two `)' tokens. */
4476 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4477 c_parser_consume_token (parser);
4478 else
4479 {
4480 parser->lex_untranslated_string = false;
4481 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4482 "expected %<)%>");
4483 return attrs;
4484 }
4485 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4486 c_parser_consume_token (parser);
4487 else
4488 {
4489 parser->lex_untranslated_string = false;
4490 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4491 "expected %<)%>");
4492 return attrs;
4493 }
4494 parser->lex_untranslated_string = false;
4495 }
4496
4497 return attrs;
4498 }
4499
4500 /* Parse a type name (C90 6.5.5, C99 6.7.6, C11 6.7.7). ALIGNAS_OK
4501 says whether alignment specifiers are OK (only in cases that might
4502 be the type name of a compound literal).
4503
4504 type-name:
4505 specifier-qualifier-list abstract-declarator[opt]
4506 */
4507
4508 struct c_type_name *
4509 c_parser_type_name (c_parser *parser, bool alignas_ok)
4510 {
4511 struct c_declspecs *specs = build_null_declspecs ();
4512 struct c_declarator *declarator;
4513 struct c_type_name *ret;
4514 bool dummy = false;
4515 c_parser_declspecs (parser, specs, false, true, true, alignas_ok, false,
4516 cla_prefer_type);
4517 if (!specs->declspecs_seen_p)
4518 {
4519 c_parser_error (parser, "expected specifier-qualifier-list");
4520 return NULL;
4521 }
4522 if (specs->type != error_mark_node)
4523 {
4524 pending_xref_error ();
4525 finish_declspecs (specs);
4526 }
4527 declarator = c_parser_declarator (parser,
4528 specs->typespec_kind != ctsk_none,
4529 C_DTR_ABSTRACT, &dummy);
4530 if (declarator == NULL)
4531 return NULL;
4532 ret = XOBNEW (&parser_obstack, struct c_type_name);
4533 ret->specs = specs;
4534 ret->declarator = declarator;
4535 return ret;
4536 }
4537
4538 /* Parse an initializer (C90 6.5.7, C99 6.7.8, C11 6.7.9).
4539
4540 initializer:
4541 assignment-expression
4542 { initializer-list }
4543 { initializer-list , }
4544
4545 initializer-list:
4546 designation[opt] initializer
4547 initializer-list , designation[opt] initializer
4548
4549 designation:
4550 designator-list =
4551
4552 designator-list:
4553 designator
4554 designator-list designator
4555
4556 designator:
4557 array-designator
4558 . identifier
4559
4560 array-designator:
4561 [ constant-expression ]
4562
4563 GNU extensions:
4564
4565 initializer:
4566 { }
4567
4568 designation:
4569 array-designator
4570 identifier :
4571
4572 array-designator:
4573 [ constant-expression ... constant-expression ]
4574
4575 Any expression without commas is accepted in the syntax for the
4576 constant-expressions, with non-constant expressions rejected later.
4577
4578 This function is only used for top-level initializers; for nested
4579 ones, see c_parser_initval. */
4580
4581 static struct c_expr
4582 c_parser_initializer (c_parser *parser)
4583 {
4584 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
4585 return c_parser_braced_init (parser, NULL_TREE, false, NULL);
4586 else
4587 {
4588 struct c_expr ret;
4589 location_t loc = c_parser_peek_token (parser)->location;
4590 ret = c_parser_expr_no_commas (parser, NULL);
4591 if (TREE_CODE (ret.value) != STRING_CST
4592 && TREE_CODE (ret.value) != COMPOUND_LITERAL_EXPR)
4593 ret = convert_lvalue_to_rvalue (loc, ret, true, true);
4594 return ret;
4595 }
4596 }
4597
4598 /* The location of the last comma within the current initializer list,
4599 or UNKNOWN_LOCATION if not within one. */
4600
4601 location_t last_init_list_comma;
4602
4603 /* Parse a braced initializer list. TYPE is the type specified for a
4604 compound literal, and NULL_TREE for other initializers and for
4605 nested braced lists. NESTED_P is true for nested braced lists,
4606 false for the list of a compound literal or the list that is the
4607 top-level initializer in a declaration. */
4608
4609 static struct c_expr
4610 c_parser_braced_init (c_parser *parser, tree type, bool nested_p,
4611 struct obstack *outer_obstack)
4612 {
4613 struct c_expr ret;
4614 struct obstack braced_init_obstack;
4615 location_t brace_loc = c_parser_peek_token (parser)->location;
4616 gcc_obstack_init (&braced_init_obstack);
4617 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
4618 matching_braces braces;
4619 braces.consume_open (parser);
4620 if (nested_p)
4621 {
4622 finish_implicit_inits (brace_loc, outer_obstack);
4623 push_init_level (brace_loc, 0, &braced_init_obstack);
4624 }
4625 else
4626 really_start_incremental_init (type);
4627 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4628 {
4629 pedwarn (brace_loc, OPT_Wpedantic, "ISO C forbids empty initializer braces");
4630 }
4631 else
4632 {
4633 /* Parse a non-empty initializer list, possibly with a trailing
4634 comma. */
4635 while (true)
4636 {
4637 c_parser_initelt (parser, &braced_init_obstack);
4638 if (parser->error)
4639 break;
4640 if (c_parser_next_token_is (parser, CPP_COMMA))
4641 {
4642 last_init_list_comma = c_parser_peek_token (parser)->location;
4643 c_parser_consume_token (parser);
4644 }
4645 else
4646 break;
4647 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4648 break;
4649 }
4650 }
4651 c_token *next_tok = c_parser_peek_token (parser);
4652 if (next_tok->type != CPP_CLOSE_BRACE)
4653 {
4654 ret.set_error ();
4655 ret.original_code = ERROR_MARK;
4656 ret.original_type = NULL;
4657 braces.skip_until_found_close (parser);
4658 pop_init_level (brace_loc, 0, &braced_init_obstack, last_init_list_comma);
4659 obstack_free (&braced_init_obstack, NULL);
4660 return ret;
4661 }
4662 location_t close_loc = next_tok->location;
4663 c_parser_consume_token (parser);
4664 ret = pop_init_level (brace_loc, 0, &braced_init_obstack, close_loc);
4665 obstack_free (&braced_init_obstack, NULL);
4666 set_c_expr_source_range (&ret, brace_loc, close_loc);
4667 return ret;
4668 }
4669
4670 /* Parse a nested initializer, including designators. */
4671
4672 static void
4673 c_parser_initelt (c_parser *parser, struct obstack * braced_init_obstack)
4674 {
4675 /* Parse any designator or designator list. A single array
4676 designator may have the subsequent "=" omitted in GNU C, but a
4677 longer list or a structure member designator may not. */
4678 if (c_parser_next_token_is (parser, CPP_NAME)
4679 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
4680 {
4681 /* Old-style structure member designator. */
4682 set_init_label (c_parser_peek_token (parser)->location,
4683 c_parser_peek_token (parser)->value,
4684 c_parser_peek_token (parser)->location,
4685 braced_init_obstack);
4686 /* Use the colon as the error location. */
4687 pedwarn (c_parser_peek_2nd_token (parser)->location, OPT_Wpedantic,
4688 "obsolete use of designated initializer with %<:%>");
4689 c_parser_consume_token (parser);
4690 c_parser_consume_token (parser);
4691 }
4692 else
4693 {
4694 /* des_seen is 0 if there have been no designators, 1 if there
4695 has been a single array designator and 2 otherwise. */
4696 int des_seen = 0;
4697 /* Location of a designator. */
4698 location_t des_loc = UNKNOWN_LOCATION; /* Quiet warning. */
4699 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE)
4700 || c_parser_next_token_is (parser, CPP_DOT))
4701 {
4702 int des_prev = des_seen;
4703 if (!des_seen)
4704 des_loc = c_parser_peek_token (parser)->location;
4705 if (des_seen < 2)
4706 des_seen++;
4707 if (c_parser_next_token_is (parser, CPP_DOT))
4708 {
4709 des_seen = 2;
4710 c_parser_consume_token (parser);
4711 if (c_parser_next_token_is (parser, CPP_NAME))
4712 {
4713 set_init_label (des_loc, c_parser_peek_token (parser)->value,
4714 c_parser_peek_token (parser)->location,
4715 braced_init_obstack);
4716 c_parser_consume_token (parser);
4717 }
4718 else
4719 {
4720 struct c_expr init;
4721 init.set_error ();
4722 init.original_code = ERROR_MARK;
4723 init.original_type = NULL;
4724 c_parser_error (parser, "expected identifier");
4725 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
4726 process_init_element (input_location, init, false,
4727 braced_init_obstack);
4728 return;
4729 }
4730 }
4731 else
4732 {
4733 tree first, second;
4734 location_t ellipsis_loc = UNKNOWN_LOCATION; /* Quiet warning. */
4735 location_t array_index_loc = UNKNOWN_LOCATION;
4736 /* ??? Following the old parser, [ objc-receiver
4737 objc-message-args ] is accepted as an initializer,
4738 being distinguished from a designator by what follows
4739 the first assignment expression inside the square
4740 brackets, but after a first array designator a
4741 subsequent square bracket is for Objective-C taken to
4742 start an expression, using the obsolete form of
4743 designated initializer without '=', rather than
4744 possibly being a second level of designation: in LALR
4745 terms, the '[' is shifted rather than reducing
4746 designator to designator-list. */
4747 if (des_prev == 1 && c_dialect_objc ())
4748 {
4749 des_seen = des_prev;
4750 break;
4751 }
4752 if (des_prev == 0 && c_dialect_objc ())
4753 {
4754 /* This might be an array designator or an
4755 Objective-C message expression. If the former,
4756 continue parsing here; if the latter, parse the
4757 remainder of the initializer given the starting
4758 primary-expression. ??? It might make sense to
4759 distinguish when des_prev == 1 as well; see
4760 previous comment. */
4761 tree rec, args;
4762 struct c_expr mexpr;
4763 c_parser_consume_token (parser);
4764 if (c_parser_peek_token (parser)->type == CPP_NAME
4765 && ((c_parser_peek_token (parser)->id_kind
4766 == C_ID_TYPENAME)
4767 || (c_parser_peek_token (parser)->id_kind
4768 == C_ID_CLASSNAME)))
4769 {
4770 /* Type name receiver. */
4771 tree id = c_parser_peek_token (parser)->value;
4772 c_parser_consume_token (parser);
4773 rec = objc_get_class_reference (id);
4774 goto parse_message_args;
4775 }
4776 first = c_parser_expr_no_commas (parser, NULL).value;
4777 mark_exp_read (first);
4778 if (c_parser_next_token_is (parser, CPP_ELLIPSIS)
4779 || c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
4780 goto array_desig_after_first;
4781 /* Expression receiver. So far only one part
4782 without commas has been parsed; there might be
4783 more of the expression. */
4784 rec = first;
4785 while (c_parser_next_token_is (parser, CPP_COMMA))
4786 {
4787 struct c_expr next;
4788 location_t comma_loc, exp_loc;
4789 comma_loc = c_parser_peek_token (parser)->location;
4790 c_parser_consume_token (parser);
4791 exp_loc = c_parser_peek_token (parser)->location;
4792 next = c_parser_expr_no_commas (parser, NULL);
4793 next = convert_lvalue_to_rvalue (exp_loc, next,
4794 true, true);
4795 rec = build_compound_expr (comma_loc, rec, next.value);
4796 }
4797 parse_message_args:
4798 /* Now parse the objc-message-args. */
4799 args = c_parser_objc_message_args (parser);
4800 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
4801 "expected %<]%>");
4802 mexpr.value
4803 = objc_build_message_expr (rec, args);
4804 mexpr.original_code = ERROR_MARK;
4805 mexpr.original_type = NULL;
4806 /* Now parse and process the remainder of the
4807 initializer, starting with this message
4808 expression as a primary-expression. */
4809 c_parser_initval (parser, &mexpr, braced_init_obstack);
4810 return;
4811 }
4812 c_parser_consume_token (parser);
4813 array_index_loc = c_parser_peek_token (parser)->location;
4814 first = c_parser_expr_no_commas (parser, NULL).value;
4815 mark_exp_read (first);
4816 array_desig_after_first:
4817 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
4818 {
4819 ellipsis_loc = c_parser_peek_token (parser)->location;
4820 c_parser_consume_token (parser);
4821 second = c_parser_expr_no_commas (parser, NULL).value;
4822 mark_exp_read (second);
4823 }
4824 else
4825 second = NULL_TREE;
4826 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
4827 {
4828 c_parser_consume_token (parser);
4829 set_init_index (array_index_loc, first, second,
4830 braced_init_obstack);
4831 if (second)
4832 pedwarn (ellipsis_loc, OPT_Wpedantic,
4833 "ISO C forbids specifying range of elements to initialize");
4834 }
4835 else
4836 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
4837 "expected %<]%>");
4838 }
4839 }
4840 if (des_seen >= 1)
4841 {
4842 if (c_parser_next_token_is (parser, CPP_EQ))
4843 {
4844 pedwarn_c90 (des_loc, OPT_Wpedantic,
4845 "ISO C90 forbids specifying subobject "
4846 "to initialize");
4847 c_parser_consume_token (parser);
4848 }
4849 else
4850 {
4851 if (des_seen == 1)
4852 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
4853 "obsolete use of designated initializer without %<=%>");
4854 else
4855 {
4856 struct c_expr init;
4857 init.set_error ();
4858 init.original_code = ERROR_MARK;
4859 init.original_type = NULL;
4860 c_parser_error (parser, "expected %<=%>");
4861 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
4862 process_init_element (input_location, init, false,
4863 braced_init_obstack);
4864 return;
4865 }
4866 }
4867 }
4868 }
4869 c_parser_initval (parser, NULL, braced_init_obstack);
4870 }
4871
4872 /* Parse a nested initializer; as c_parser_initializer but parses
4873 initializers within braced lists, after any designators have been
4874 applied. If AFTER is not NULL then it is an Objective-C message
4875 expression which is the primary-expression starting the
4876 initializer. */
4877
4878 static void
4879 c_parser_initval (c_parser *parser, struct c_expr *after,
4880 struct obstack * braced_init_obstack)
4881 {
4882 struct c_expr init;
4883 gcc_assert (!after || c_dialect_objc ());
4884 location_t loc = c_parser_peek_token (parser)->location;
4885
4886 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE) && !after)
4887 init = c_parser_braced_init (parser, NULL_TREE, true,
4888 braced_init_obstack);
4889 else
4890 {
4891 init = c_parser_expr_no_commas (parser, after);
4892 if (init.value != NULL_TREE
4893 && TREE_CODE (init.value) != STRING_CST
4894 && TREE_CODE (init.value) != COMPOUND_LITERAL_EXPR)
4895 init = convert_lvalue_to_rvalue (loc, init, true, true);
4896 }
4897 process_init_element (loc, init, false, braced_init_obstack);
4898 }
4899
4900 /* Parse a compound statement (possibly a function body) (C90 6.6.2,
4901 C99 6.8.2, C11 6.8.2).
4902
4903 compound-statement:
4904 { block-item-list[opt] }
4905 { label-declarations block-item-list }
4906
4907 block-item-list:
4908 block-item
4909 block-item-list block-item
4910
4911 block-item:
4912 nested-declaration
4913 statement
4914
4915 nested-declaration:
4916 declaration
4917
4918 GNU extensions:
4919
4920 compound-statement:
4921 { label-declarations block-item-list }
4922
4923 nested-declaration:
4924 __extension__ nested-declaration
4925 nested-function-definition
4926
4927 label-declarations:
4928 label-declaration
4929 label-declarations label-declaration
4930
4931 label-declaration:
4932 __label__ identifier-list ;
4933
4934 Allowing the mixing of declarations and code is new in C99. The
4935 GNU syntax also permits (not shown above) labels at the end of
4936 compound statements, which yield an error. We don't allow labels
4937 on declarations; this might seem like a natural extension, but
4938 there would be a conflict between attributes on the label and
4939 prefix attributes on the declaration. ??? The syntax follows the
4940 old parser in requiring something after label declarations.
4941 Although they are erroneous if the labels declared aren't defined,
4942 is it useful for the syntax to be this way?
4943
4944 OpenACC:
4945
4946 block-item:
4947 openacc-directive
4948
4949 openacc-directive:
4950 update-directive
4951
4952 OpenMP:
4953
4954 block-item:
4955 openmp-directive
4956
4957 openmp-directive:
4958 barrier-directive
4959 flush-directive
4960 taskwait-directive
4961 taskyield-directive
4962 cancel-directive
4963 cancellation-point-directive */
4964
4965 static tree
4966 c_parser_compound_statement (c_parser *parser)
4967 {
4968 tree stmt;
4969 location_t brace_loc;
4970 brace_loc = c_parser_peek_token (parser)->location;
4971 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
4972 {
4973 /* Ensure a scope is entered and left anyway to avoid confusion
4974 if we have just prepared to enter a function body. */
4975 stmt = c_begin_compound_stmt (true);
4976 c_end_compound_stmt (brace_loc, stmt, true);
4977 return error_mark_node;
4978 }
4979 stmt = c_begin_compound_stmt (true);
4980 c_parser_compound_statement_nostart (parser);
4981
4982 return c_end_compound_stmt (brace_loc, stmt, true);
4983 }
4984
4985 /* Parse a compound statement except for the opening brace. This is
4986 used for parsing both compound statements and statement expressions
4987 (which follow different paths to handling the opening). */
4988
4989 static void
4990 c_parser_compound_statement_nostart (c_parser *parser)
4991 {
4992 bool last_stmt = false;
4993 bool last_label = false;
4994 bool save_valid_for_pragma = valid_location_for_stdc_pragma_p ();
4995 location_t label_loc = UNKNOWN_LOCATION; /* Quiet warning. */
4996 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4997 {
4998 add_debug_begin_stmt (c_parser_peek_token (parser)->location);
4999 c_parser_consume_token (parser);
5000 return;
5001 }
5002 mark_valid_location_for_stdc_pragma (true);
5003 if (c_parser_next_token_is_keyword (parser, RID_LABEL))
5004 {
5005 /* Read zero or more forward-declarations for labels that nested
5006 functions can jump to. */
5007 mark_valid_location_for_stdc_pragma (false);
5008 while (c_parser_next_token_is_keyword (parser, RID_LABEL))
5009 {
5010 label_loc = c_parser_peek_token (parser)->location;
5011 c_parser_consume_token (parser);
5012 /* Any identifiers, including those declared as type names,
5013 are OK here. */
5014 while (true)
5015 {
5016 tree label;
5017 if (c_parser_next_token_is_not (parser, CPP_NAME))
5018 {
5019 c_parser_error (parser, "expected identifier");
5020 break;
5021 }
5022 label
5023 = declare_label (c_parser_peek_token (parser)->value);
5024 C_DECLARED_LABEL_FLAG (label) = 1;
5025 add_stmt (build_stmt (label_loc, DECL_EXPR, label));
5026 c_parser_consume_token (parser);
5027 if (c_parser_next_token_is (parser, CPP_COMMA))
5028 c_parser_consume_token (parser);
5029 else
5030 break;
5031 }
5032 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5033 }
5034 pedwarn (label_loc, OPT_Wpedantic, "ISO C forbids label declarations");
5035 }
5036 /* We must now have at least one statement, label or declaration. */
5037 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
5038 {
5039 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
5040 c_parser_error (parser, "expected declaration or statement");
5041 c_parser_consume_token (parser);
5042 return;
5043 }
5044 while (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
5045 {
5046 location_t loc = c_parser_peek_token (parser)->location;
5047 loc = expansion_point_location_if_in_system_header (loc);
5048 if (c_parser_next_token_is_keyword (parser, RID_CASE)
5049 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
5050 || (c_parser_next_token_is (parser, CPP_NAME)
5051 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
5052 {
5053 if (c_parser_next_token_is_keyword (parser, RID_CASE))
5054 label_loc = c_parser_peek_2nd_token (parser)->location;
5055 else
5056 label_loc = c_parser_peek_token (parser)->location;
5057 last_label = true;
5058 last_stmt = false;
5059 mark_valid_location_for_stdc_pragma (false);
5060 c_parser_label (parser);
5061 }
5062 else if (!last_label
5063 && c_parser_next_tokens_start_declaration (parser))
5064 {
5065 last_label = false;
5066 mark_valid_location_for_stdc_pragma (false);
5067 bool fallthru_attr_p = false;
5068 c_parser_declaration_or_fndef (parser, true, true, true, true,
5069 true, NULL, vNULL, NULL,
5070 &fallthru_attr_p);
5071 if (last_stmt && !fallthru_attr_p)
5072 pedwarn_c90 (loc, OPT_Wdeclaration_after_statement,
5073 "ISO C90 forbids mixed declarations and code");
5074 last_stmt = fallthru_attr_p;
5075 }
5076 else if (!last_label
5077 && c_parser_next_token_is_keyword (parser, RID_EXTENSION))
5078 {
5079 /* __extension__ can start a declaration, but is also an
5080 unary operator that can start an expression. Consume all
5081 but the last of a possible series of __extension__ to
5082 determine which. */
5083 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
5084 && (c_parser_peek_2nd_token (parser)->keyword
5085 == RID_EXTENSION))
5086 c_parser_consume_token (parser);
5087 if (c_token_starts_declaration (c_parser_peek_2nd_token (parser)))
5088 {
5089 int ext;
5090 ext = disable_extension_diagnostics ();
5091 c_parser_consume_token (parser);
5092 last_label = false;
5093 mark_valid_location_for_stdc_pragma (false);
5094 c_parser_declaration_or_fndef (parser, true, true, true, true,
5095 true, NULL, vNULL);
5096 /* Following the old parser, __extension__ does not
5097 disable this diagnostic. */
5098 restore_extension_diagnostics (ext);
5099 if (last_stmt)
5100 pedwarn_c90 (loc, OPT_Wdeclaration_after_statement,
5101 "ISO C90 forbids mixed declarations and code");
5102 last_stmt = false;
5103 }
5104 else
5105 goto statement;
5106 }
5107 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
5108 {
5109 /* External pragmas, and some omp pragmas, are not associated
5110 with regular c code, and so are not to be considered statements
5111 syntactically. This ensures that the user doesn't put them
5112 places that would turn into syntax errors if the directive
5113 were ignored. */
5114 if (c_parser_pragma (parser,
5115 last_label ? pragma_stmt : pragma_compound,
5116 NULL))
5117 last_label = false, last_stmt = true;
5118 }
5119 else if (c_parser_next_token_is (parser, CPP_EOF))
5120 {
5121 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
5122 c_parser_error (parser, "expected declaration or statement");
5123 return;
5124 }
5125 else if (c_parser_next_token_is_keyword (parser, RID_ELSE))
5126 {
5127 if (parser->in_if_block)
5128 {
5129 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
5130 error_at (loc, "expected %<}%> before %<else%>");
5131 return;
5132 }
5133 else
5134 {
5135 error_at (loc, "%<else%> without a previous %<if%>");
5136 c_parser_consume_token (parser);
5137 continue;
5138 }
5139 }
5140 else
5141 {
5142 statement:
5143 last_label = false;
5144 last_stmt = true;
5145 mark_valid_location_for_stdc_pragma (false);
5146 c_parser_statement_after_labels (parser, NULL);
5147 }
5148
5149 parser->error = false;
5150 }
5151 if (last_label)
5152 error_at (label_loc, "label at end of compound statement");
5153 c_parser_consume_token (parser);
5154 /* Restore the value we started with. */
5155 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
5156 }
5157
5158 /* Parse all consecutive labels. */
5159
5160 static void
5161 c_parser_all_labels (c_parser *parser)
5162 {
5163 while (c_parser_next_token_is_keyword (parser, RID_CASE)
5164 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
5165 || (c_parser_next_token_is (parser, CPP_NAME)
5166 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
5167 c_parser_label (parser);
5168 }
5169
5170 /* Parse a label (C90 6.6.1, C99 6.8.1, C11 6.8.1).
5171
5172 label:
5173 identifier : attributes[opt]
5174 case constant-expression :
5175 default :
5176
5177 GNU extensions:
5178
5179 label:
5180 case constant-expression ... constant-expression :
5181
5182 The use of attributes on labels is a GNU extension. The syntax in
5183 GNU C accepts any expressions without commas, non-constant
5184 expressions being rejected later. */
5185
5186 static void
5187 c_parser_label (c_parser *parser)
5188 {
5189 location_t loc1 = c_parser_peek_token (parser)->location;
5190 tree label = NULL_TREE;
5191
5192 /* Remember whether this case or a user-defined label is allowed to fall
5193 through to. */
5194 bool fallthrough_p = c_parser_peek_token (parser)->flags & PREV_FALLTHROUGH;
5195
5196 if (c_parser_next_token_is_keyword (parser, RID_CASE))
5197 {
5198 tree exp1, exp2;
5199 c_parser_consume_token (parser);
5200 exp1 = c_parser_expr_no_commas (parser, NULL).value;
5201 if (c_parser_next_token_is (parser, CPP_COLON))
5202 {
5203 c_parser_consume_token (parser);
5204 label = do_case (loc1, exp1, NULL_TREE);
5205 }
5206 else if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
5207 {
5208 c_parser_consume_token (parser);
5209 exp2 = c_parser_expr_no_commas (parser, NULL).value;
5210 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
5211 label = do_case (loc1, exp1, exp2);
5212 }
5213 else
5214 c_parser_error (parser, "expected %<:%> or %<...%>");
5215 }
5216 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
5217 {
5218 c_parser_consume_token (parser);
5219 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
5220 label = do_case (loc1, NULL_TREE, NULL_TREE);
5221 }
5222 else
5223 {
5224 tree name = c_parser_peek_token (parser)->value;
5225 tree tlab;
5226 tree attrs;
5227 location_t loc2 = c_parser_peek_token (parser)->location;
5228 gcc_assert (c_parser_next_token_is (parser, CPP_NAME));
5229 c_parser_consume_token (parser);
5230 gcc_assert (c_parser_next_token_is (parser, CPP_COLON));
5231 c_parser_consume_token (parser);
5232 attrs = c_parser_attributes (parser);
5233 tlab = define_label (loc2, name);
5234 if (tlab)
5235 {
5236 decl_attributes (&tlab, attrs, 0);
5237 label = add_stmt (build_stmt (loc1, LABEL_EXPR, tlab));
5238 }
5239 }
5240 if (label)
5241 {
5242 if (TREE_CODE (label) == LABEL_EXPR)
5243 FALLTHROUGH_LABEL_P (LABEL_EXPR_LABEL (label)) = fallthrough_p;
5244 else
5245 FALLTHROUGH_LABEL_P (CASE_LABEL (label)) = fallthrough_p;
5246
5247 /* Allow '__attribute__((fallthrough));'. */
5248 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
5249 {
5250 location_t loc = c_parser_peek_token (parser)->location;
5251 tree attrs = c_parser_attributes (parser);
5252 if (attribute_fallthrough_p (attrs))
5253 {
5254 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5255 {
5256 tree fn = build_call_expr_internal_loc (loc,
5257 IFN_FALLTHROUGH,
5258 void_type_node, 0);
5259 add_stmt (fn);
5260 }
5261 else
5262 warning_at (loc, OPT_Wattributes, "%<fallthrough%> attribute "
5263 "not followed by %<;%>");
5264 }
5265 else if (attrs != NULL_TREE)
5266 warning_at (loc, OPT_Wattributes, "only attribute %<fallthrough%>"
5267 " can be applied to a null statement");
5268 }
5269 if (c_parser_next_tokens_start_declaration (parser))
5270 {
5271 error_at (c_parser_peek_token (parser)->location,
5272 "a label can only be part of a statement and "
5273 "a declaration is not a statement");
5274 c_parser_declaration_or_fndef (parser, /*fndef_ok*/ false,
5275 /*static_assert_ok*/ true,
5276 /*empty_ok*/ true, /*nested*/ true,
5277 /*start_attr_ok*/ true, NULL,
5278 vNULL);
5279 }
5280 }
5281 }
5282
5283 /* Parse a statement (C90 6.6, C99 6.8, C11 6.8).
5284
5285 statement:
5286 labeled-statement
5287 compound-statement
5288 expression-statement
5289 selection-statement
5290 iteration-statement
5291 jump-statement
5292
5293 labeled-statement:
5294 label statement
5295
5296 expression-statement:
5297 expression[opt] ;
5298
5299 selection-statement:
5300 if-statement
5301 switch-statement
5302
5303 iteration-statement:
5304 while-statement
5305 do-statement
5306 for-statement
5307
5308 jump-statement:
5309 goto identifier ;
5310 continue ;
5311 break ;
5312 return expression[opt] ;
5313
5314 GNU extensions:
5315
5316 statement:
5317 asm-statement
5318
5319 jump-statement:
5320 goto * expression ;
5321
5322 expression-statement:
5323 attributes ;
5324
5325 Objective-C:
5326
5327 statement:
5328 objc-throw-statement
5329 objc-try-catch-statement
5330 objc-synchronized-statement
5331
5332 objc-throw-statement:
5333 @throw expression ;
5334 @throw ;
5335
5336 OpenACC:
5337
5338 statement:
5339 openacc-construct
5340
5341 openacc-construct:
5342 parallel-construct
5343 kernels-construct
5344 data-construct
5345 loop-construct
5346
5347 parallel-construct:
5348 parallel-directive structured-block
5349
5350 kernels-construct:
5351 kernels-directive structured-block
5352
5353 data-construct:
5354 data-directive structured-block
5355
5356 loop-construct:
5357 loop-directive structured-block
5358
5359 OpenMP:
5360
5361 statement:
5362 openmp-construct
5363
5364 openmp-construct:
5365 parallel-construct
5366 for-construct
5367 simd-construct
5368 for-simd-construct
5369 sections-construct
5370 single-construct
5371 parallel-for-construct
5372 parallel-for-simd-construct
5373 parallel-sections-construct
5374 master-construct
5375 critical-construct
5376 atomic-construct
5377 ordered-construct
5378
5379 parallel-construct:
5380 parallel-directive structured-block
5381
5382 for-construct:
5383 for-directive iteration-statement
5384
5385 simd-construct:
5386 simd-directive iteration-statements
5387
5388 for-simd-construct:
5389 for-simd-directive iteration-statements
5390
5391 sections-construct:
5392 sections-directive section-scope
5393
5394 single-construct:
5395 single-directive structured-block
5396
5397 parallel-for-construct:
5398 parallel-for-directive iteration-statement
5399
5400 parallel-for-simd-construct:
5401 parallel-for-simd-directive iteration-statement
5402
5403 parallel-sections-construct:
5404 parallel-sections-directive section-scope
5405
5406 master-construct:
5407 master-directive structured-block
5408
5409 critical-construct:
5410 critical-directive structured-block
5411
5412 atomic-construct:
5413 atomic-directive expression-statement
5414
5415 ordered-construct:
5416 ordered-directive structured-block
5417
5418 Transactional Memory:
5419
5420 statement:
5421 transaction-statement
5422 transaction-cancel-statement
5423
5424 IF_P is used to track whether there's a (possibly labeled) if statement
5425 which is not enclosed in braces and has an else clause. This is used to
5426 implement -Wparentheses. */
5427
5428 static void
5429 c_parser_statement (c_parser *parser, bool *if_p, location_t *loc_after_labels)
5430 {
5431 c_parser_all_labels (parser);
5432 if (loc_after_labels)
5433 *loc_after_labels = c_parser_peek_token (parser)->location;
5434 c_parser_statement_after_labels (parser, if_p, NULL);
5435 }
5436
5437 /* Parse a statement, other than a labeled statement. CHAIN is a vector
5438 of if-else-if conditions.
5439
5440 IF_P is used to track whether there's a (possibly labeled) if statement
5441 which is not enclosed in braces and has an else clause. This is used to
5442 implement -Wparentheses. */
5443
5444 static void
5445 c_parser_statement_after_labels (c_parser *parser, bool *if_p,
5446 vec<tree> *chain)
5447 {
5448 location_t loc = c_parser_peek_token (parser)->location;
5449 tree stmt = NULL_TREE;
5450 bool in_if_block = parser->in_if_block;
5451 parser->in_if_block = false;
5452 if (if_p != NULL)
5453 *if_p = false;
5454
5455 if (c_parser_peek_token (parser)->type != CPP_OPEN_BRACE)
5456 add_debug_begin_stmt (loc);
5457
5458 switch (c_parser_peek_token (parser)->type)
5459 {
5460 case CPP_OPEN_BRACE:
5461 add_stmt (c_parser_compound_statement (parser));
5462 break;
5463 case CPP_KEYWORD:
5464 switch (c_parser_peek_token (parser)->keyword)
5465 {
5466 case RID_IF:
5467 c_parser_if_statement (parser, if_p, chain);
5468 break;
5469 case RID_SWITCH:
5470 c_parser_switch_statement (parser, if_p);
5471 break;
5472 case RID_WHILE:
5473 c_parser_while_statement (parser, false, 0, if_p);
5474 break;
5475 case RID_DO:
5476 c_parser_do_statement (parser, 0, false);
5477 break;
5478 case RID_FOR:
5479 c_parser_for_statement (parser, false, 0, if_p);
5480 break;
5481 case RID_GOTO:
5482 c_parser_consume_token (parser);
5483 if (c_parser_next_token_is (parser, CPP_NAME))
5484 {
5485 stmt = c_finish_goto_label (loc,
5486 c_parser_peek_token (parser)->value);
5487 c_parser_consume_token (parser);
5488 }
5489 else if (c_parser_next_token_is (parser, CPP_MULT))
5490 {
5491 struct c_expr val;
5492
5493 c_parser_consume_token (parser);
5494 val = c_parser_expression (parser);
5495 val = convert_lvalue_to_rvalue (loc, val, false, true);
5496 stmt = c_finish_goto_ptr (loc, val.value);
5497 }
5498 else
5499 c_parser_error (parser, "expected identifier or %<*%>");
5500 goto expect_semicolon;
5501 case RID_CONTINUE:
5502 c_parser_consume_token (parser);
5503 stmt = c_finish_bc_stmt (loc, &c_cont_label, false);
5504 goto expect_semicolon;
5505 case RID_BREAK:
5506 c_parser_consume_token (parser);
5507 stmt = c_finish_bc_stmt (loc, &c_break_label, true);
5508 goto expect_semicolon;
5509 case RID_RETURN:
5510 c_parser_consume_token (parser);
5511 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5512 {
5513 stmt = c_finish_return (loc, NULL_TREE, NULL_TREE);
5514 c_parser_consume_token (parser);
5515 }
5516 else
5517 {
5518 location_t xloc = c_parser_peek_token (parser)->location;
5519 struct c_expr expr = c_parser_expression_conv (parser);
5520 mark_exp_read (expr.value);
5521 stmt = c_finish_return (EXPR_LOC_OR_LOC (expr.value, xloc),
5522 expr.value, expr.original_type);
5523 goto expect_semicolon;
5524 }
5525 break;
5526 case RID_ASM:
5527 stmt = c_parser_asm_statement (parser);
5528 break;
5529 case RID_TRANSACTION_ATOMIC:
5530 case RID_TRANSACTION_RELAXED:
5531 stmt = c_parser_transaction (parser,
5532 c_parser_peek_token (parser)->keyword);
5533 break;
5534 case RID_TRANSACTION_CANCEL:
5535 stmt = c_parser_transaction_cancel (parser);
5536 goto expect_semicolon;
5537 case RID_AT_THROW:
5538 gcc_assert (c_dialect_objc ());
5539 c_parser_consume_token (parser);
5540 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5541 {
5542 stmt = objc_build_throw_stmt (loc, NULL_TREE);
5543 c_parser_consume_token (parser);
5544 }
5545 else
5546 {
5547 struct c_expr expr = c_parser_expression (parser);
5548 expr = convert_lvalue_to_rvalue (loc, expr, false, false);
5549 expr.value = c_fully_fold (expr.value, false, NULL);
5550 stmt = objc_build_throw_stmt (loc, expr.value);
5551 goto expect_semicolon;
5552 }
5553 break;
5554 case RID_AT_TRY:
5555 gcc_assert (c_dialect_objc ());
5556 c_parser_objc_try_catch_finally_statement (parser);
5557 break;
5558 case RID_AT_SYNCHRONIZED:
5559 gcc_assert (c_dialect_objc ());
5560 c_parser_objc_synchronized_statement (parser);
5561 break;
5562 case RID_ATTRIBUTE:
5563 {
5564 /* Allow '__attribute__((fallthrough));'. */
5565 tree attrs = c_parser_attributes (parser);
5566 if (attribute_fallthrough_p (attrs))
5567 {
5568 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5569 {
5570 tree fn = build_call_expr_internal_loc (loc,
5571 IFN_FALLTHROUGH,
5572 void_type_node, 0);
5573 add_stmt (fn);
5574 /* Eat the ';'. */
5575 c_parser_consume_token (parser);
5576 }
5577 else
5578 warning_at (loc, OPT_Wattributes,
5579 "%<fallthrough%> attribute not followed "
5580 "by %<;%>");
5581 }
5582 else if (attrs != NULL_TREE)
5583 warning_at (loc, OPT_Wattributes, "only attribute %<fallthrough%>"
5584 " can be applied to a null statement");
5585 break;
5586 }
5587 default:
5588 goto expr_stmt;
5589 }
5590 break;
5591 case CPP_SEMICOLON:
5592 c_parser_consume_token (parser);
5593 break;
5594 case CPP_CLOSE_PAREN:
5595 case CPP_CLOSE_SQUARE:
5596 /* Avoid infinite loop in error recovery:
5597 c_parser_skip_until_found stops at a closing nesting
5598 delimiter without consuming it, but here we need to consume
5599 it to proceed further. */
5600 c_parser_error (parser, "expected statement");
5601 c_parser_consume_token (parser);
5602 break;
5603 case CPP_PRAGMA:
5604 c_parser_pragma (parser, pragma_stmt, if_p);
5605 break;
5606 default:
5607 expr_stmt:
5608 stmt = c_finish_expr_stmt (loc, c_parser_expression_conv (parser).value);
5609 expect_semicolon:
5610 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5611 break;
5612 }
5613 /* Two cases cannot and do not have line numbers associated: If stmt
5614 is degenerate, such as "2;", then stmt is an INTEGER_CST, which
5615 cannot hold line numbers. But that's OK because the statement
5616 will either be changed to a MODIFY_EXPR during gimplification of
5617 the statement expr, or discarded. If stmt was compound, but
5618 without new variables, we will have skipped the creation of a
5619 BIND and will have a bare STATEMENT_LIST. But that's OK because
5620 (recursively) all of the component statements should already have
5621 line numbers assigned. ??? Can we discard no-op statements
5622 earlier? */
5623 if (EXPR_LOCATION (stmt) == UNKNOWN_LOCATION)
5624 protected_set_expr_location (stmt, loc);
5625
5626 parser->in_if_block = in_if_block;
5627 }
5628
5629 /* Parse the condition from an if, do, while or for statements. */
5630
5631 static tree
5632 c_parser_condition (c_parser *parser)
5633 {
5634 location_t loc = c_parser_peek_token (parser)->location;
5635 tree cond;
5636 cond = c_parser_expression_conv (parser).value;
5637 cond = c_objc_common_truthvalue_conversion (loc, cond);
5638 cond = c_fully_fold (cond, false, NULL);
5639 if (warn_sequence_point)
5640 verify_sequence_points (cond);
5641 return cond;
5642 }
5643
5644 /* Parse a parenthesized condition from an if, do or while statement.
5645
5646 condition:
5647 ( expression )
5648 */
5649 static tree
5650 c_parser_paren_condition (c_parser *parser)
5651 {
5652 tree cond;
5653 matching_parens parens;
5654 if (!parens.require_open (parser))
5655 return error_mark_node;
5656 cond = c_parser_condition (parser);
5657 parens.skip_until_found_close (parser);
5658 return cond;
5659 }
5660
5661 /* Parse a statement which is a block in C99.
5662
5663 IF_P is used to track whether there's a (possibly labeled) if statement
5664 which is not enclosed in braces and has an else clause. This is used to
5665 implement -Wparentheses. */
5666
5667 static tree
5668 c_parser_c99_block_statement (c_parser *parser, bool *if_p,
5669 location_t *loc_after_labels)
5670 {
5671 tree block = c_begin_compound_stmt (flag_isoc99);
5672 location_t loc = c_parser_peek_token (parser)->location;
5673 c_parser_statement (parser, if_p, loc_after_labels);
5674 return c_end_compound_stmt (loc, block, flag_isoc99);
5675 }
5676
5677 /* Parse the body of an if statement. This is just parsing a
5678 statement but (a) it is a block in C99, (b) we track whether the
5679 body is an if statement for the sake of -Wparentheses warnings, (c)
5680 we handle an empty body specially for the sake of -Wempty-body
5681 warnings, and (d) we call parser_compound_statement directly
5682 because c_parser_statement_after_labels resets
5683 parser->in_if_block.
5684
5685 IF_P is used to track whether there's a (possibly labeled) if statement
5686 which is not enclosed in braces and has an else clause. This is used to
5687 implement -Wparentheses. */
5688
5689 static tree
5690 c_parser_if_body (c_parser *parser, bool *if_p,
5691 const token_indent_info &if_tinfo)
5692 {
5693 tree block = c_begin_compound_stmt (flag_isoc99);
5694 location_t body_loc = c_parser_peek_token (parser)->location;
5695 location_t body_loc_after_labels = UNKNOWN_LOCATION;
5696 token_indent_info body_tinfo
5697 = get_token_indent_info (c_parser_peek_token (parser));
5698
5699 c_parser_all_labels (parser);
5700 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5701 {
5702 location_t loc = c_parser_peek_token (parser)->location;
5703 add_stmt (build_empty_stmt (loc));
5704 c_parser_consume_token (parser);
5705 if (!c_parser_next_token_is_keyword (parser, RID_ELSE))
5706 warning_at (loc, OPT_Wempty_body,
5707 "suggest braces around empty body in an %<if%> statement");
5708 }
5709 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5710 add_stmt (c_parser_compound_statement (parser));
5711 else
5712 {
5713 body_loc_after_labels = c_parser_peek_token (parser)->location;
5714 c_parser_statement_after_labels (parser, if_p);
5715 }
5716
5717 token_indent_info next_tinfo
5718 = get_token_indent_info (c_parser_peek_token (parser));
5719 warn_for_misleading_indentation (if_tinfo, body_tinfo, next_tinfo);
5720 if (body_loc_after_labels != UNKNOWN_LOCATION
5721 && next_tinfo.type != CPP_SEMICOLON)
5722 warn_for_multistatement_macros (body_loc_after_labels, next_tinfo.location,
5723 if_tinfo.location, RID_IF);
5724
5725 return c_end_compound_stmt (body_loc, block, flag_isoc99);
5726 }
5727
5728 /* Parse the else body of an if statement. This is just parsing a
5729 statement but (a) it is a block in C99, (b) we handle an empty body
5730 specially for the sake of -Wempty-body warnings. CHAIN is a vector
5731 of if-else-if conditions. */
5732
5733 static tree
5734 c_parser_else_body (c_parser *parser, const token_indent_info &else_tinfo,
5735 vec<tree> *chain)
5736 {
5737 location_t body_loc = c_parser_peek_token (parser)->location;
5738 tree block = c_begin_compound_stmt (flag_isoc99);
5739 token_indent_info body_tinfo
5740 = get_token_indent_info (c_parser_peek_token (parser));
5741 location_t body_loc_after_labels = UNKNOWN_LOCATION;
5742
5743 c_parser_all_labels (parser);
5744 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5745 {
5746 location_t loc = c_parser_peek_token (parser)->location;
5747 warning_at (loc,
5748 OPT_Wempty_body,
5749 "suggest braces around empty body in an %<else%> statement");
5750 add_stmt (build_empty_stmt (loc));
5751 c_parser_consume_token (parser);
5752 }
5753 else
5754 {
5755 if (!c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5756 body_loc_after_labels = c_parser_peek_token (parser)->location;
5757 c_parser_statement_after_labels (parser, NULL, chain);
5758 }
5759
5760 token_indent_info next_tinfo
5761 = get_token_indent_info (c_parser_peek_token (parser));
5762 warn_for_misleading_indentation (else_tinfo, body_tinfo, next_tinfo);
5763 if (body_loc_after_labels != UNKNOWN_LOCATION
5764 && next_tinfo.type != CPP_SEMICOLON)
5765 warn_for_multistatement_macros (body_loc_after_labels, next_tinfo.location,
5766 else_tinfo.location, RID_ELSE);
5767
5768 return c_end_compound_stmt (body_loc, block, flag_isoc99);
5769 }
5770
5771 /* We might need to reclassify any previously-lexed identifier, e.g.
5772 when we've left a for loop with an if-statement without else in the
5773 body - we might have used a wrong scope for the token. See PR67784. */
5774
5775 static void
5776 c_parser_maybe_reclassify_token (c_parser *parser)
5777 {
5778 if (c_parser_next_token_is (parser, CPP_NAME))
5779 {
5780 c_token *token = c_parser_peek_token (parser);
5781
5782 if (token->id_kind != C_ID_CLASSNAME)
5783 {
5784 tree decl = lookup_name (token->value);
5785
5786 token->id_kind = C_ID_ID;
5787 if (decl)
5788 {
5789 if (TREE_CODE (decl) == TYPE_DECL)
5790 token->id_kind = C_ID_TYPENAME;
5791 }
5792 else if (c_dialect_objc ())
5793 {
5794 tree objc_interface_decl = objc_is_class_name (token->value);
5795 /* Objective-C class names are in the same namespace as
5796 variables and typedefs, and hence are shadowed by local
5797 declarations. */
5798 if (objc_interface_decl)
5799 {
5800 token->value = objc_interface_decl;
5801 token->id_kind = C_ID_CLASSNAME;
5802 }
5803 }
5804 }
5805 }
5806 }
5807
5808 /* Parse an if statement (C90 6.6.4, C99 6.8.4, C11 6.8.4).
5809
5810 if-statement:
5811 if ( expression ) statement
5812 if ( expression ) statement else statement
5813
5814 CHAIN is a vector of if-else-if conditions.
5815 IF_P is used to track whether there's a (possibly labeled) if statement
5816 which is not enclosed in braces and has an else clause. This is used to
5817 implement -Wparentheses. */
5818
5819 static void
5820 c_parser_if_statement (c_parser *parser, bool *if_p, vec<tree> *chain)
5821 {
5822 tree block;
5823 location_t loc;
5824 tree cond;
5825 bool nested_if = false;
5826 tree first_body, second_body;
5827 bool in_if_block;
5828
5829 gcc_assert (c_parser_next_token_is_keyword (parser, RID_IF));
5830 token_indent_info if_tinfo
5831 = get_token_indent_info (c_parser_peek_token (parser));
5832 c_parser_consume_token (parser);
5833 block = c_begin_compound_stmt (flag_isoc99);
5834 loc = c_parser_peek_token (parser)->location;
5835 cond = c_parser_paren_condition (parser);
5836 in_if_block = parser->in_if_block;
5837 parser->in_if_block = true;
5838 first_body = c_parser_if_body (parser, &nested_if, if_tinfo);
5839 parser->in_if_block = in_if_block;
5840
5841 if (warn_duplicated_cond)
5842 warn_duplicated_cond_add_or_warn (EXPR_LOCATION (cond), cond, &chain);
5843
5844 if (c_parser_next_token_is_keyword (parser, RID_ELSE))
5845 {
5846 token_indent_info else_tinfo
5847 = get_token_indent_info (c_parser_peek_token (parser));
5848 c_parser_consume_token (parser);
5849 if (warn_duplicated_cond)
5850 {
5851 if (c_parser_next_token_is_keyword (parser, RID_IF)
5852 && chain == NULL)
5853 {
5854 /* We've got "if (COND) else if (COND2)". Start the
5855 condition chain and add COND as the first element. */
5856 chain = new vec<tree> ();
5857 if (!CONSTANT_CLASS_P (cond) && !TREE_SIDE_EFFECTS (cond))
5858 chain->safe_push (cond);
5859 }
5860 else if (!c_parser_next_token_is_keyword (parser, RID_IF))
5861 {
5862 /* This is if-else without subsequent if. Zap the condition
5863 chain; we would have already warned at this point. */
5864 delete chain;
5865 chain = NULL;
5866 }
5867 }
5868 second_body = c_parser_else_body (parser, else_tinfo, chain);
5869 /* Set IF_P to true to indicate that this if statement has an
5870 else clause. This may trigger the Wparentheses warning
5871 below when we get back up to the parent if statement. */
5872 if (if_p != NULL)
5873 *if_p = true;
5874 }
5875 else
5876 {
5877 second_body = NULL_TREE;
5878
5879 /* Diagnose an ambiguous else if if-then-else is nested inside
5880 if-then. */
5881 if (nested_if)
5882 warning_at (loc, OPT_Wdangling_else,
5883 "suggest explicit braces to avoid ambiguous %<else%>");
5884
5885 if (warn_duplicated_cond)
5886 {
5887 /* This if statement does not have an else clause. We don't
5888 need the condition chain anymore. */
5889 delete chain;
5890 chain = NULL;
5891 }
5892 }
5893 c_finish_if_stmt (loc, cond, first_body, second_body);
5894 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
5895
5896 c_parser_maybe_reclassify_token (parser);
5897 }
5898
5899 /* Parse a switch statement (C90 6.6.4, C99 6.8.4, C11 6.8.4).
5900
5901 switch-statement:
5902 switch (expression) statement
5903 */
5904
5905 static void
5906 c_parser_switch_statement (c_parser *parser, bool *if_p)
5907 {
5908 struct c_expr ce;
5909 tree block, expr, body, save_break;
5910 location_t switch_loc = c_parser_peek_token (parser)->location;
5911 location_t switch_cond_loc;
5912 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SWITCH));
5913 c_parser_consume_token (parser);
5914 block = c_begin_compound_stmt (flag_isoc99);
5915 bool explicit_cast_p = false;
5916 matching_parens parens;
5917 if (parens.require_open (parser))
5918 {
5919 switch_cond_loc = c_parser_peek_token (parser)->location;
5920 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
5921 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
5922 explicit_cast_p = true;
5923 ce = c_parser_expression (parser);
5924 ce = convert_lvalue_to_rvalue (switch_cond_loc, ce, true, false);
5925 expr = ce.value;
5926 /* ??? expr has no valid location? */
5927 parens.skip_until_found_close (parser);
5928 }
5929 else
5930 {
5931 switch_cond_loc = UNKNOWN_LOCATION;
5932 expr = error_mark_node;
5933 ce.original_type = error_mark_node;
5934 }
5935 c_start_case (switch_loc, switch_cond_loc, expr, explicit_cast_p);
5936 save_break = c_break_label;
5937 c_break_label = NULL_TREE;
5938 location_t loc_after_labels;
5939 bool open_brace_p = c_parser_peek_token (parser)->type == CPP_OPEN_BRACE;
5940 body = c_parser_c99_block_statement (parser, if_p, &loc_after_labels);
5941 location_t next_loc = c_parser_peek_token (parser)->location;
5942 if (!open_brace_p && c_parser_peek_token (parser)->type != CPP_SEMICOLON)
5943 warn_for_multistatement_macros (loc_after_labels, next_loc, switch_loc,
5944 RID_SWITCH);
5945 if (c_break_label)
5946 {
5947 location_t here = c_parser_peek_token (parser)->location;
5948 tree t = build1 (LABEL_EXPR, void_type_node, c_break_label);
5949 SET_EXPR_LOCATION (t, here);
5950 SWITCH_BREAK_LABEL_P (c_break_label) = 1;
5951 append_to_statement_list_force (t, &body);
5952 }
5953 c_finish_case (body, ce.original_type);
5954 c_break_label = save_break;
5955 add_stmt (c_end_compound_stmt (switch_loc, block, flag_isoc99));
5956 c_parser_maybe_reclassify_token (parser);
5957 }
5958
5959 /* Parse a while statement (C90 6.6.5, C99 6.8.5, C11 6.8.5).
5960
5961 while-statement:
5962 while (expression) statement
5963
5964 IF_P is used to track whether there's a (possibly labeled) if statement
5965 which is not enclosed in braces and has an else clause. This is used to
5966 implement -Wparentheses. */
5967
5968 static void
5969 c_parser_while_statement (c_parser *parser, bool ivdep, unsigned short unroll,
5970 bool *if_p)
5971 {
5972 tree block, cond, body, save_break, save_cont;
5973 location_t loc;
5974 gcc_assert (c_parser_next_token_is_keyword (parser, RID_WHILE));
5975 token_indent_info while_tinfo
5976 = get_token_indent_info (c_parser_peek_token (parser));
5977 c_parser_consume_token (parser);
5978 block = c_begin_compound_stmt (flag_isoc99);
5979 loc = c_parser_peek_token (parser)->location;
5980 cond = c_parser_paren_condition (parser);
5981 if (ivdep && cond != error_mark_node)
5982 cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
5983 build_int_cst (integer_type_node,
5984 annot_expr_ivdep_kind),
5985 integer_zero_node);
5986 if (unroll && cond != error_mark_node)
5987 cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
5988 build_int_cst (integer_type_node,
5989 annot_expr_unroll_kind),
5990 build_int_cst (integer_type_node, unroll));
5991 save_break = c_break_label;
5992 c_break_label = NULL_TREE;
5993 save_cont = c_cont_label;
5994 c_cont_label = NULL_TREE;
5995
5996 token_indent_info body_tinfo
5997 = get_token_indent_info (c_parser_peek_token (parser));
5998
5999 location_t loc_after_labels;
6000 bool open_brace = c_parser_next_token_is (parser, CPP_OPEN_BRACE);
6001 body = c_parser_c99_block_statement (parser, if_p, &loc_after_labels);
6002 c_finish_loop (loc, cond, NULL, body, c_break_label, c_cont_label, true);
6003 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
6004 c_parser_maybe_reclassify_token (parser);
6005
6006 token_indent_info next_tinfo
6007 = get_token_indent_info (c_parser_peek_token (parser));
6008 warn_for_misleading_indentation (while_tinfo, body_tinfo, next_tinfo);
6009
6010 if (next_tinfo.type != CPP_SEMICOLON && !open_brace)
6011 warn_for_multistatement_macros (loc_after_labels, next_tinfo.location,
6012 while_tinfo.location, RID_WHILE);
6013
6014 c_break_label = save_break;
6015 c_cont_label = save_cont;
6016 }
6017
6018 /* Parse a do statement (C90 6.6.5, C99 6.8.5, C11 6.8.5).
6019
6020 do-statement:
6021 do statement while ( expression ) ;
6022 */
6023
6024 static void
6025 c_parser_do_statement (c_parser *parser, bool ivdep, unsigned short unroll)
6026 {
6027 tree block, cond, body, save_break, save_cont, new_break, new_cont;
6028 location_t loc;
6029 gcc_assert (c_parser_next_token_is_keyword (parser, RID_DO));
6030 c_parser_consume_token (parser);
6031 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
6032 warning_at (c_parser_peek_token (parser)->location,
6033 OPT_Wempty_body,
6034 "suggest braces around empty body in %<do%> statement");
6035 block = c_begin_compound_stmt (flag_isoc99);
6036 loc = c_parser_peek_token (parser)->location;
6037 save_break = c_break_label;
6038 c_break_label = NULL_TREE;
6039 save_cont = c_cont_label;
6040 c_cont_label = NULL_TREE;
6041 body = c_parser_c99_block_statement (parser, NULL);
6042 c_parser_require_keyword (parser, RID_WHILE, "expected %<while%>");
6043 new_break = c_break_label;
6044 c_break_label = save_break;
6045 new_cont = c_cont_label;
6046 c_cont_label = save_cont;
6047 cond = c_parser_paren_condition (parser);
6048 if (ivdep && cond != error_mark_node)
6049 cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
6050 build_int_cst (integer_type_node,
6051 annot_expr_ivdep_kind),
6052 integer_zero_node);
6053 if (unroll && cond != error_mark_node)
6054 cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
6055 build_int_cst (integer_type_node,
6056 annot_expr_unroll_kind),
6057 build_int_cst (integer_type_node, unroll));
6058 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
6059 c_parser_skip_to_end_of_block_or_statement (parser);
6060 c_finish_loop (loc, cond, NULL, body, new_break, new_cont, false);
6061 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
6062 }
6063
6064 /* Parse a for statement (C90 6.6.5, C99 6.8.5, C11 6.8.5).
6065
6066 for-statement:
6067 for ( expression[opt] ; expression[opt] ; expression[opt] ) statement
6068 for ( nested-declaration expression[opt] ; expression[opt] ) statement
6069
6070 The form with a declaration is new in C99.
6071
6072 ??? In accordance with the old parser, the declaration may be a
6073 nested function, which is then rejected in check_for_loop_decls,
6074 but does it make any sense for this to be included in the grammar?
6075 Note in particular that the nested function does not include a
6076 trailing ';', whereas the "declaration" production includes one.
6077 Also, can we reject bad declarations earlier and cheaper than
6078 check_for_loop_decls?
6079
6080 In Objective-C, there are two additional variants:
6081
6082 foreach-statement:
6083 for ( expression in expresssion ) statement
6084 for ( declaration in expression ) statement
6085
6086 This is inconsistent with C, because the second variant is allowed
6087 even if c99 is not enabled.
6088
6089 The rest of the comment documents these Objective-C foreach-statement.
6090
6091 Here is the canonical example of the first variant:
6092 for (object in array) { do something with object }
6093 we call the first expression ("object") the "object_expression" and
6094 the second expression ("array") the "collection_expression".
6095 object_expression must be an lvalue of type "id" (a generic Objective-C
6096 object) because the loop works by assigning to object_expression the
6097 various objects from the collection_expression. collection_expression
6098 must evaluate to something of type "id" which responds to the method
6099 countByEnumeratingWithState:objects:count:.
6100
6101 The canonical example of the second variant is:
6102 for (id object in array) { do something with object }
6103 which is completely equivalent to
6104 {
6105 id object;
6106 for (object in array) { do something with object }
6107 }
6108 Note that initizializing 'object' in some way (eg, "for ((object =
6109 xxx) in array) { do something with object }") is possibly
6110 technically valid, but completely pointless as 'object' will be
6111 assigned to something else as soon as the loop starts. We should
6112 most likely reject it (TODO).
6113
6114 The beginning of the Objective-C foreach-statement looks exactly
6115 like the beginning of the for-statement, and we can tell it is a
6116 foreach-statement only because the initial declaration or
6117 expression is terminated by 'in' instead of ';'.
6118
6119 IF_P is used to track whether there's a (possibly labeled) if statement
6120 which is not enclosed in braces and has an else clause. This is used to
6121 implement -Wparentheses. */
6122
6123 static void
6124 c_parser_for_statement (c_parser *parser, bool ivdep, unsigned short unroll,
6125 bool *if_p)
6126 {
6127 tree block, cond, incr, save_break, save_cont, body;
6128 /* The following are only used when parsing an ObjC foreach statement. */
6129 tree object_expression;
6130 /* Silence the bogus uninitialized warning. */
6131 tree collection_expression = NULL;
6132 location_t loc = c_parser_peek_token (parser)->location;
6133 location_t for_loc = c_parser_peek_token (parser)->location;
6134 bool is_foreach_statement = false;
6135 gcc_assert (c_parser_next_token_is_keyword (parser, RID_FOR));
6136 token_indent_info for_tinfo
6137 = get_token_indent_info (c_parser_peek_token (parser));
6138 c_parser_consume_token (parser);
6139 /* Open a compound statement in Objective-C as well, just in case this is
6140 as foreach expression. */
6141 block = c_begin_compound_stmt (flag_isoc99 || c_dialect_objc ());
6142 cond = error_mark_node;
6143 incr = error_mark_node;
6144 matching_parens parens;
6145 if (parens.require_open (parser))
6146 {
6147 /* Parse the initialization declaration or expression. */
6148 object_expression = error_mark_node;
6149 parser->objc_could_be_foreach_context = c_dialect_objc ();
6150 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
6151 {
6152 parser->objc_could_be_foreach_context = false;
6153 c_parser_consume_token (parser);
6154 c_finish_expr_stmt (loc, NULL_TREE);
6155 }
6156 else if (c_parser_next_tokens_start_declaration (parser))
6157 {
6158 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
6159 &object_expression, vNULL);
6160 parser->objc_could_be_foreach_context = false;
6161
6162 if (c_parser_next_token_is_keyword (parser, RID_IN))
6163 {
6164 c_parser_consume_token (parser);
6165 is_foreach_statement = true;
6166 if (check_for_loop_decls (for_loc, true) == NULL_TREE)
6167 c_parser_error (parser, "multiple iterating variables in fast enumeration");
6168 }
6169 else
6170 check_for_loop_decls (for_loc, flag_isoc99);
6171 }
6172 else if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
6173 {
6174 /* __extension__ can start a declaration, but is also an
6175 unary operator that can start an expression. Consume all
6176 but the last of a possible series of __extension__ to
6177 determine which. */
6178 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
6179 && (c_parser_peek_2nd_token (parser)->keyword
6180 == RID_EXTENSION))
6181 c_parser_consume_token (parser);
6182 if (c_token_starts_declaration (c_parser_peek_2nd_token (parser)))
6183 {
6184 int ext;
6185 ext = disable_extension_diagnostics ();
6186 c_parser_consume_token (parser);
6187 c_parser_declaration_or_fndef (parser, true, true, true, true,
6188 true, &object_expression, vNULL);
6189 parser->objc_could_be_foreach_context = false;
6190
6191 restore_extension_diagnostics (ext);
6192 if (c_parser_next_token_is_keyword (parser, RID_IN))
6193 {
6194 c_parser_consume_token (parser);
6195 is_foreach_statement = true;
6196 if (check_for_loop_decls (for_loc, true) == NULL_TREE)
6197 c_parser_error (parser, "multiple iterating variables in fast enumeration");
6198 }
6199 else
6200 check_for_loop_decls (for_loc, flag_isoc99);
6201 }
6202 else
6203 goto init_expr;
6204 }
6205 else
6206 {
6207 init_expr:
6208 {
6209 struct c_expr ce;
6210 tree init_expression;
6211 ce = c_parser_expression (parser);
6212 init_expression = ce.value;
6213 parser->objc_could_be_foreach_context = false;
6214 if (c_parser_next_token_is_keyword (parser, RID_IN))
6215 {
6216 c_parser_consume_token (parser);
6217 is_foreach_statement = true;
6218 if (! lvalue_p (init_expression))
6219 c_parser_error (parser, "invalid iterating variable in fast enumeration");
6220 object_expression = c_fully_fold (init_expression, false, NULL);
6221 }
6222 else
6223 {
6224 ce = convert_lvalue_to_rvalue (loc, ce, true, false);
6225 init_expression = ce.value;
6226 c_finish_expr_stmt (loc, init_expression);
6227 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
6228 }
6229 }
6230 }
6231 /* Parse the loop condition. In the case of a foreach
6232 statement, there is no loop condition. */
6233 gcc_assert (!parser->objc_could_be_foreach_context);
6234 if (!is_foreach_statement)
6235 {
6236 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
6237 {
6238 if (ivdep)
6239 {
6240 c_parser_error (parser, "missing loop condition in loop with "
6241 "%<GCC ivdep%> pragma");
6242 cond = error_mark_node;
6243 }
6244 else if (unroll)
6245 {
6246 c_parser_error (parser, "missing loop condition in loop with "
6247 "%<GCC unroll%> pragma");
6248 cond = error_mark_node;
6249 }
6250 else
6251 {
6252 c_parser_consume_token (parser);
6253 cond = NULL_TREE;
6254 }
6255 }
6256 else
6257 {
6258 cond = c_parser_condition (parser);
6259 c_parser_skip_until_found (parser, CPP_SEMICOLON,
6260 "expected %<;%>");
6261 }
6262 if (ivdep && cond != error_mark_node)
6263 cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
6264 build_int_cst (integer_type_node,
6265 annot_expr_ivdep_kind),
6266 integer_zero_node);
6267 if (unroll && cond != error_mark_node)
6268 cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
6269 build_int_cst (integer_type_node,
6270 annot_expr_unroll_kind),
6271 build_int_cst (integer_type_node, unroll));
6272 }
6273 /* Parse the increment expression (the third expression in a
6274 for-statement). In the case of a foreach-statement, this is
6275 the expression that follows the 'in'. */
6276 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
6277 {
6278 if (is_foreach_statement)
6279 {
6280 c_parser_error (parser, "missing collection in fast enumeration");
6281 collection_expression = error_mark_node;
6282 }
6283 else
6284 incr = c_process_expr_stmt (loc, NULL_TREE);
6285 }
6286 else
6287 {
6288 if (is_foreach_statement)
6289 collection_expression = c_fully_fold (c_parser_expression (parser).value,
6290 false, NULL);
6291 else
6292 {
6293 struct c_expr ce = c_parser_expression (parser);
6294 ce = convert_lvalue_to_rvalue (loc, ce, true, false);
6295 incr = c_process_expr_stmt (loc, ce.value);
6296 }
6297 }
6298 parens.skip_until_found_close (parser);
6299 }
6300 save_break = c_break_label;
6301 c_break_label = NULL_TREE;
6302 save_cont = c_cont_label;
6303 c_cont_label = NULL_TREE;
6304
6305 token_indent_info body_tinfo
6306 = get_token_indent_info (c_parser_peek_token (parser));
6307
6308 location_t loc_after_labels;
6309 bool open_brace = c_parser_next_token_is (parser, CPP_OPEN_BRACE);
6310 body = c_parser_c99_block_statement (parser, if_p, &loc_after_labels);
6311
6312 if (is_foreach_statement)
6313 objc_finish_foreach_loop (loc, object_expression, collection_expression, body, c_break_label, c_cont_label);
6314 else
6315 c_finish_loop (loc, cond, incr, body, c_break_label, c_cont_label, true);
6316 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99 || c_dialect_objc ()));
6317 c_parser_maybe_reclassify_token (parser);
6318
6319 token_indent_info next_tinfo
6320 = get_token_indent_info (c_parser_peek_token (parser));
6321 warn_for_misleading_indentation (for_tinfo, body_tinfo, next_tinfo);
6322
6323 if (next_tinfo.type != CPP_SEMICOLON && !open_brace)
6324 warn_for_multistatement_macros (loc_after_labels, next_tinfo.location,
6325 for_tinfo.location, RID_FOR);
6326
6327 c_break_label = save_break;
6328 c_cont_label = save_cont;
6329 }
6330
6331 /* Parse an asm statement, a GNU extension. This is a full-blown asm
6332 statement with inputs, outputs, clobbers, and volatile, inline, and goto
6333 tags allowed.
6334
6335 asm-qualifier:
6336 volatile
6337 inline
6338 goto
6339
6340 asm-qualifier-list:
6341 asm-qualifier-list asm-qualifier
6342 asm-qualifier
6343
6344 asm-statement:
6345 asm asm-qualifier-list[opt] ( asm-argument ) ;
6346
6347 asm-argument:
6348 asm-string-literal
6349 asm-string-literal : asm-operands[opt]
6350 asm-string-literal : asm-operands[opt] : asm-operands[opt]
6351 asm-string-literal : asm-operands[opt] : asm-operands[opt] \
6352 : asm-clobbers[opt]
6353 asm-string-literal : : asm-operands[opt] : asm-clobbers[opt] \
6354 : asm-goto-operands
6355
6356 The form with asm-goto-operands is valid if and only if the
6357 asm-qualifier-list contains goto, and is the only allowed form in that case.
6358 Duplicate asm-qualifiers are not allowed. */
6359
6360 static tree
6361 c_parser_asm_statement (c_parser *parser)
6362 {
6363 tree str, outputs, inputs, clobbers, labels, ret;
6364 bool simple;
6365 location_t asm_loc = c_parser_peek_token (parser)->location;
6366 int section, nsections;
6367
6368 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
6369 c_parser_consume_token (parser);
6370
6371 /* Handle the asm-qualifier-list. */
6372 location_t volatile_loc = UNKNOWN_LOCATION;
6373 location_t inline_loc = UNKNOWN_LOCATION;
6374 location_t goto_loc = UNKNOWN_LOCATION;
6375 for (;;)
6376 {
6377 c_token *token = c_parser_peek_token (parser);
6378 location_t loc = token->location;
6379 switch (token->keyword)
6380 {
6381 case RID_VOLATILE:
6382 if (volatile_loc)
6383 {
6384 error_at (loc, "duplicate asm qualifier %qE", token->value);
6385 inform (volatile_loc, "first seen here");
6386 }
6387 else
6388 volatile_loc = loc;
6389 c_parser_consume_token (parser);
6390 continue;
6391
6392 case RID_INLINE:
6393 if (inline_loc)
6394 {
6395 error_at (loc, "duplicate asm qualifier %qE", token->value);
6396 inform (inline_loc, "first seen here");
6397 }
6398 else
6399 inline_loc = loc;
6400 c_parser_consume_token (parser);
6401 continue;
6402
6403 case RID_GOTO:
6404 if (goto_loc)
6405 {
6406 error_at (loc, "duplicate asm qualifier %qE", token->value);
6407 inform (goto_loc, "first seen here");
6408 }
6409 else
6410 goto_loc = loc;
6411 c_parser_consume_token (parser);
6412 continue;
6413
6414 case RID_CONST:
6415 case RID_RESTRICT:
6416 error_at (loc, "%qE is not an asm qualifier", token->value);
6417 c_parser_consume_token (parser);
6418 continue;
6419
6420 default:
6421 break;
6422 }
6423 break;
6424 }
6425
6426 bool is_volatile = (volatile_loc != UNKNOWN_LOCATION);
6427 bool is_inline = (inline_loc != UNKNOWN_LOCATION);
6428 bool is_goto = (goto_loc != UNKNOWN_LOCATION);
6429
6430 /* ??? Follow the C++ parser rather than using the
6431 lex_untranslated_string kludge. */
6432 parser->lex_untranslated_string = true;
6433 ret = NULL;
6434
6435 matching_parens parens;
6436 if (!parens.require_open (parser))
6437 goto error;
6438
6439 str = c_parser_asm_string_literal (parser);
6440 if (str == NULL_TREE)
6441 goto error_close_paren;
6442
6443 simple = true;
6444 outputs = NULL_TREE;
6445 inputs = NULL_TREE;
6446 clobbers = NULL_TREE;
6447 labels = NULL_TREE;
6448
6449 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
6450 goto done_asm;
6451
6452 /* Parse each colon-delimited section of operands. */
6453 nsections = 3 + is_goto;
6454 for (section = 0; section < nsections; ++section)
6455 {
6456 if (!c_parser_require (parser, CPP_COLON,
6457 is_goto
6458 ? G_("expected %<:%>")
6459 : G_("expected %<:%> or %<)%>"),
6460 UNKNOWN_LOCATION, is_goto))
6461 goto error_close_paren;
6462
6463 /* Once past any colon, we're no longer a simple asm. */
6464 simple = false;
6465
6466 if ((!c_parser_next_token_is (parser, CPP_COLON)
6467 && !c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
6468 || section == 3)
6469 switch (section)
6470 {
6471 case 0:
6472 /* For asm goto, we don't allow output operands, but reserve
6473 the slot for a future extension that does allow them. */
6474 if (!is_goto)
6475 outputs = c_parser_asm_operands (parser);
6476 break;
6477 case 1:
6478 inputs = c_parser_asm_operands (parser);
6479 break;
6480 case 2:
6481 clobbers = c_parser_asm_clobbers (parser);
6482 break;
6483 case 3:
6484 labels = c_parser_asm_goto_operands (parser);
6485 break;
6486 default:
6487 gcc_unreachable ();
6488 }
6489
6490 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
6491 goto done_asm;
6492 }
6493
6494 done_asm:
6495 if (!parens.require_close (parser))
6496 {
6497 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6498 goto error;
6499 }
6500
6501 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
6502 c_parser_skip_to_end_of_block_or_statement (parser);
6503
6504 ret = build_asm_stmt (is_volatile,
6505 build_asm_expr (asm_loc, str, outputs, inputs,
6506 clobbers, labels, simple, is_inline));
6507
6508 error:
6509 parser->lex_untranslated_string = false;
6510 return ret;
6511
6512 error_close_paren:
6513 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6514 goto error;
6515 }
6516
6517 /* Parse asm operands, a GNU extension.
6518
6519 asm-operands:
6520 asm-operand
6521 asm-operands , asm-operand
6522
6523 asm-operand:
6524 asm-string-literal ( expression )
6525 [ identifier ] asm-string-literal ( expression )
6526 */
6527
6528 static tree
6529 c_parser_asm_operands (c_parser *parser)
6530 {
6531 tree list = NULL_TREE;
6532 while (true)
6533 {
6534 tree name, str;
6535 struct c_expr expr;
6536 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
6537 {
6538 c_parser_consume_token (parser);
6539 if (c_parser_next_token_is (parser, CPP_NAME))
6540 {
6541 tree id = c_parser_peek_token (parser)->value;
6542 c_parser_consume_token (parser);
6543 name = build_string (IDENTIFIER_LENGTH (id),
6544 IDENTIFIER_POINTER (id));
6545 }
6546 else
6547 {
6548 c_parser_error (parser, "expected identifier");
6549 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
6550 return NULL_TREE;
6551 }
6552 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
6553 "expected %<]%>");
6554 }
6555 else
6556 name = NULL_TREE;
6557 str = c_parser_asm_string_literal (parser);
6558 if (str == NULL_TREE)
6559 return NULL_TREE;
6560 parser->lex_untranslated_string = false;
6561 matching_parens parens;
6562 if (!parens.require_open (parser))
6563 {
6564 parser->lex_untranslated_string = true;
6565 return NULL_TREE;
6566 }
6567 expr = c_parser_expression (parser);
6568 mark_exp_read (expr.value);
6569 parser->lex_untranslated_string = true;
6570 if (!parens.require_close (parser))
6571 {
6572 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6573 return NULL_TREE;
6574 }
6575 list = chainon (list, build_tree_list (build_tree_list (name, str),
6576 expr.value));
6577 if (c_parser_next_token_is (parser, CPP_COMMA))
6578 c_parser_consume_token (parser);
6579 else
6580 break;
6581 }
6582 return list;
6583 }
6584
6585 /* Parse asm clobbers, a GNU extension.
6586
6587 asm-clobbers:
6588 asm-string-literal
6589 asm-clobbers , asm-string-literal
6590 */
6591
6592 static tree
6593 c_parser_asm_clobbers (c_parser *parser)
6594 {
6595 tree list = NULL_TREE;
6596 while (true)
6597 {
6598 tree str = c_parser_asm_string_literal (parser);
6599 if (str)
6600 list = tree_cons (NULL_TREE, str, list);
6601 else
6602 return NULL_TREE;
6603 if (c_parser_next_token_is (parser, CPP_COMMA))
6604 c_parser_consume_token (parser);
6605 else
6606 break;
6607 }
6608 return list;
6609 }
6610
6611 /* Parse asm goto labels, a GNU extension.
6612
6613 asm-goto-operands:
6614 identifier
6615 asm-goto-operands , identifier
6616 */
6617
6618 static tree
6619 c_parser_asm_goto_operands (c_parser *parser)
6620 {
6621 tree list = NULL_TREE;
6622 while (true)
6623 {
6624 tree name, label;
6625
6626 if (c_parser_next_token_is (parser, CPP_NAME))
6627 {
6628 c_token *tok = c_parser_peek_token (parser);
6629 name = tok->value;
6630 label = lookup_label_for_goto (tok->location, name);
6631 c_parser_consume_token (parser);
6632 TREE_USED (label) = 1;
6633 }
6634 else
6635 {
6636 c_parser_error (parser, "expected identifier");
6637 return NULL_TREE;
6638 }
6639
6640 name = build_string (IDENTIFIER_LENGTH (name),
6641 IDENTIFIER_POINTER (name));
6642 list = tree_cons (name, label, list);
6643 if (c_parser_next_token_is (parser, CPP_COMMA))
6644 c_parser_consume_token (parser);
6645 else
6646 return nreverse (list);
6647 }
6648 }
6649
6650 /* Parse an expression other than a compound expression; that is, an
6651 assignment expression (C90 6.3.16, C99 6.5.16, C11 6.5.16). If
6652 AFTER is not NULL then it is an Objective-C message expression which
6653 is the primary-expression starting the expression as an initializer.
6654
6655 assignment-expression:
6656 conditional-expression
6657 unary-expression assignment-operator assignment-expression
6658
6659 assignment-operator: one of
6660 = *= /= %= += -= <<= >>= &= ^= |=
6661
6662 In GNU C we accept any conditional expression on the LHS and
6663 diagnose the invalid lvalue rather than producing a syntax
6664 error. */
6665
6666 static struct c_expr
6667 c_parser_expr_no_commas (c_parser *parser, struct c_expr *after,
6668 tree omp_atomic_lhs)
6669 {
6670 struct c_expr lhs, rhs, ret;
6671 enum tree_code code;
6672 location_t op_location, exp_location;
6673 gcc_assert (!after || c_dialect_objc ());
6674 lhs = c_parser_conditional_expression (parser, after, omp_atomic_lhs);
6675 op_location = c_parser_peek_token (parser)->location;
6676 switch (c_parser_peek_token (parser)->type)
6677 {
6678 case CPP_EQ:
6679 code = NOP_EXPR;
6680 break;
6681 case CPP_MULT_EQ:
6682 code = MULT_EXPR;
6683 break;
6684 case CPP_DIV_EQ:
6685 code = TRUNC_DIV_EXPR;
6686 break;
6687 case CPP_MOD_EQ:
6688 code = TRUNC_MOD_EXPR;
6689 break;
6690 case CPP_PLUS_EQ:
6691 code = PLUS_EXPR;
6692 break;
6693 case CPP_MINUS_EQ:
6694 code = MINUS_EXPR;
6695 break;
6696 case CPP_LSHIFT_EQ:
6697 code = LSHIFT_EXPR;
6698 break;
6699 case CPP_RSHIFT_EQ:
6700 code = RSHIFT_EXPR;
6701 break;
6702 case CPP_AND_EQ:
6703 code = BIT_AND_EXPR;
6704 break;
6705 case CPP_XOR_EQ:
6706 code = BIT_XOR_EXPR;
6707 break;
6708 case CPP_OR_EQ:
6709 code = BIT_IOR_EXPR;
6710 break;
6711 default:
6712 return lhs;
6713 }
6714 c_parser_consume_token (parser);
6715 exp_location = c_parser_peek_token (parser)->location;
6716 rhs = c_parser_expr_no_commas (parser, NULL);
6717 rhs = convert_lvalue_to_rvalue (exp_location, rhs, true, true);
6718
6719 ret.value = build_modify_expr (op_location, lhs.value, lhs.original_type,
6720 code, exp_location, rhs.value,
6721 rhs.original_type);
6722 set_c_expr_source_range (&ret, lhs.get_start (), rhs.get_finish ());
6723 if (code == NOP_EXPR)
6724 ret.original_code = MODIFY_EXPR;
6725 else
6726 {
6727 TREE_NO_WARNING (ret.value) = 1;
6728 ret.original_code = ERROR_MARK;
6729 }
6730 ret.original_type = NULL;
6731 return ret;
6732 }
6733
6734 /* Parse a conditional expression (C90 6.3.15, C99 6.5.15, C11 6.5.15). If
6735 AFTER is not NULL then it is an Objective-C message expression which is
6736 the primary-expression starting the expression as an initializer.
6737
6738 conditional-expression:
6739 logical-OR-expression
6740 logical-OR-expression ? expression : conditional-expression
6741
6742 GNU extensions:
6743
6744 conditional-expression:
6745 logical-OR-expression ? : conditional-expression
6746 */
6747
6748 static struct c_expr
6749 c_parser_conditional_expression (c_parser *parser, struct c_expr *after,
6750 tree omp_atomic_lhs)
6751 {
6752 struct c_expr cond, exp1, exp2, ret;
6753 location_t start, cond_loc, colon_loc;
6754
6755 gcc_assert (!after || c_dialect_objc ());
6756
6757 cond = c_parser_binary_expression (parser, after, omp_atomic_lhs);
6758
6759 if (c_parser_next_token_is_not (parser, CPP_QUERY))
6760 return cond;
6761 if (cond.value != error_mark_node)
6762 start = cond.get_start ();
6763 else
6764 start = UNKNOWN_LOCATION;
6765 cond_loc = c_parser_peek_token (parser)->location;
6766 cond = convert_lvalue_to_rvalue (cond_loc, cond, true, true);
6767 c_parser_consume_token (parser);
6768 if (c_parser_next_token_is (parser, CPP_COLON))
6769 {
6770 tree eptype = NULL_TREE;
6771
6772 location_t middle_loc = c_parser_peek_token (parser)->location;
6773 pedwarn (middle_loc, OPT_Wpedantic,
6774 "ISO C forbids omitting the middle term of a ?: expression");
6775 if (TREE_CODE (cond.value) == EXCESS_PRECISION_EXPR)
6776 {
6777 eptype = TREE_TYPE (cond.value);
6778 cond.value = TREE_OPERAND (cond.value, 0);
6779 }
6780 tree e = cond.value;
6781 while (TREE_CODE (e) == COMPOUND_EXPR)
6782 e = TREE_OPERAND (e, 1);
6783 warn_for_omitted_condop (middle_loc, e);
6784 /* Make sure first operand is calculated only once. */
6785 exp1.value = save_expr (default_conversion (cond.value));
6786 if (eptype)
6787 exp1.value = build1 (EXCESS_PRECISION_EXPR, eptype, exp1.value);
6788 exp1.original_type = NULL;
6789 exp1.src_range = cond.src_range;
6790 cond.value = c_objc_common_truthvalue_conversion (cond_loc, exp1.value);
6791 c_inhibit_evaluation_warnings += cond.value == truthvalue_true_node;
6792 }
6793 else
6794 {
6795 cond.value
6796 = c_objc_common_truthvalue_conversion
6797 (cond_loc, default_conversion (cond.value));
6798 c_inhibit_evaluation_warnings += cond.value == truthvalue_false_node;
6799 exp1 = c_parser_expression_conv (parser);
6800 mark_exp_read (exp1.value);
6801 c_inhibit_evaluation_warnings +=
6802 ((cond.value == truthvalue_true_node)
6803 - (cond.value == truthvalue_false_node));
6804 }
6805
6806 colon_loc = c_parser_peek_token (parser)->location;
6807 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
6808 {
6809 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
6810 ret.set_error ();
6811 ret.original_code = ERROR_MARK;
6812 ret.original_type = NULL;
6813 return ret;
6814 }
6815 {
6816 location_t exp2_loc = c_parser_peek_token (parser)->location;
6817 exp2 = c_parser_conditional_expression (parser, NULL, NULL_TREE);
6818 exp2 = convert_lvalue_to_rvalue (exp2_loc, exp2, true, true);
6819 }
6820 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
6821 location_t loc1 = make_location (exp1.get_start (), exp1.src_range);
6822 location_t loc2 = make_location (exp2.get_start (), exp2.src_range);
6823 ret.value = build_conditional_expr (colon_loc, cond.value,
6824 cond.original_code == C_MAYBE_CONST_EXPR,
6825 exp1.value, exp1.original_type, loc1,
6826 exp2.value, exp2.original_type, loc2);
6827 ret.original_code = ERROR_MARK;
6828 if (exp1.value == error_mark_node || exp2.value == error_mark_node)
6829 ret.original_type = NULL;
6830 else
6831 {
6832 tree t1, t2;
6833
6834 /* If both sides are enum type, the default conversion will have
6835 made the type of the result be an integer type. We want to
6836 remember the enum types we started with. */
6837 t1 = exp1.original_type ? exp1.original_type : TREE_TYPE (exp1.value);
6838 t2 = exp2.original_type ? exp2.original_type : TREE_TYPE (exp2.value);
6839 ret.original_type = ((t1 != error_mark_node
6840 && t2 != error_mark_node
6841 && (TYPE_MAIN_VARIANT (t1)
6842 == TYPE_MAIN_VARIANT (t2)))
6843 ? t1
6844 : NULL);
6845 }
6846 set_c_expr_source_range (&ret, start, exp2.get_finish ());
6847 return ret;
6848 }
6849
6850 /* Parse a binary expression; that is, a logical-OR-expression (C90
6851 6.3.5-6.3.14, C99 6.5.5-6.5.14, C11 6.5.5-6.5.14). If AFTER is not
6852 NULL then it is an Objective-C message expression which is the
6853 primary-expression starting the expression as an initializer.
6854
6855 OMP_ATOMIC_LHS is NULL, unless parsing OpenMP #pragma omp atomic,
6856 when it should be the unfolded lhs. In a valid OpenMP source,
6857 one of the operands of the toplevel binary expression must be equal
6858 to it. In that case, just return a build2 created binary operation
6859 rather than result of parser_build_binary_op.
6860
6861 multiplicative-expression:
6862 cast-expression
6863 multiplicative-expression * cast-expression
6864 multiplicative-expression / cast-expression
6865 multiplicative-expression % cast-expression
6866
6867 additive-expression:
6868 multiplicative-expression
6869 additive-expression + multiplicative-expression
6870 additive-expression - multiplicative-expression
6871
6872 shift-expression:
6873 additive-expression
6874 shift-expression << additive-expression
6875 shift-expression >> additive-expression
6876
6877 relational-expression:
6878 shift-expression
6879 relational-expression < shift-expression
6880 relational-expression > shift-expression
6881 relational-expression <= shift-expression
6882 relational-expression >= shift-expression
6883
6884 equality-expression:
6885 relational-expression
6886 equality-expression == relational-expression
6887 equality-expression != relational-expression
6888
6889 AND-expression:
6890 equality-expression
6891 AND-expression & equality-expression
6892
6893 exclusive-OR-expression:
6894 AND-expression
6895 exclusive-OR-expression ^ AND-expression
6896
6897 inclusive-OR-expression:
6898 exclusive-OR-expression
6899 inclusive-OR-expression | exclusive-OR-expression
6900
6901 logical-AND-expression:
6902 inclusive-OR-expression
6903 logical-AND-expression && inclusive-OR-expression
6904
6905 logical-OR-expression:
6906 logical-AND-expression
6907 logical-OR-expression || logical-AND-expression
6908 */
6909
6910 static struct c_expr
6911 c_parser_binary_expression (c_parser *parser, struct c_expr *after,
6912 tree omp_atomic_lhs)
6913 {
6914 /* A binary expression is parsed using operator-precedence parsing,
6915 with the operands being cast expressions. All the binary
6916 operators are left-associative. Thus a binary expression is of
6917 form:
6918
6919 E0 op1 E1 op2 E2 ...
6920
6921 which we represent on a stack. On the stack, the precedence
6922 levels are strictly increasing. When a new operator is
6923 encountered of higher precedence than that at the top of the
6924 stack, it is pushed; its LHS is the top expression, and its RHS
6925 is everything parsed until it is popped. When a new operator is
6926 encountered with precedence less than or equal to that at the top
6927 of the stack, triples E[i-1] op[i] E[i] are popped and replaced
6928 by the result of the operation until the operator at the top of
6929 the stack has lower precedence than the new operator or there is
6930 only one element on the stack; then the top expression is the LHS
6931 of the new operator. In the case of logical AND and OR
6932 expressions, we also need to adjust c_inhibit_evaluation_warnings
6933 as appropriate when the operators are pushed and popped. */
6934
6935 struct {
6936 /* The expression at this stack level. */
6937 struct c_expr expr;
6938 /* The precedence of the operator on its left, PREC_NONE at the
6939 bottom of the stack. */
6940 enum c_parser_prec prec;
6941 /* The operation on its left. */
6942 enum tree_code op;
6943 /* The source location of this operation. */
6944 location_t loc;
6945 /* The sizeof argument if expr.original_code == SIZEOF_EXPR. */
6946 tree sizeof_arg;
6947 } stack[NUM_PRECS];
6948 int sp;
6949 /* Location of the binary operator. */
6950 location_t binary_loc = UNKNOWN_LOCATION; /* Quiet warning. */
6951 #define POP \
6952 do { \
6953 switch (stack[sp].op) \
6954 { \
6955 case TRUTH_ANDIF_EXPR: \
6956 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
6957 == truthvalue_false_node); \
6958 break; \
6959 case TRUTH_ORIF_EXPR: \
6960 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
6961 == truthvalue_true_node); \
6962 break; \
6963 case TRUNC_DIV_EXPR: \
6964 if (stack[sp - 1].expr.original_code == SIZEOF_EXPR \
6965 && stack[sp].expr.original_code == SIZEOF_EXPR) \
6966 { \
6967 tree type0 = stack[sp - 1].sizeof_arg; \
6968 tree type1 = stack[sp].sizeof_arg; \
6969 tree first_arg = type0; \
6970 if (!TYPE_P (type0)) \
6971 type0 = TREE_TYPE (type0); \
6972 if (!TYPE_P (type1)) \
6973 type1 = TREE_TYPE (type1); \
6974 if (POINTER_TYPE_P (type0) \
6975 && comptypes (TREE_TYPE (type0), type1) \
6976 && !(TREE_CODE (first_arg) == PARM_DECL \
6977 && C_ARRAY_PARAMETER (first_arg) \
6978 && warn_sizeof_array_argument)) \
6979 { \
6980 auto_diagnostic_group d; \
6981 if (warning_at (stack[sp].loc, OPT_Wsizeof_pointer_div, \
6982 "division %<sizeof (%T) / sizeof (%T)%> " \
6983 "does not compute the number of array " \
6984 "elements", \
6985 type0, type1)) \
6986 if (DECL_P (first_arg)) \
6987 inform (DECL_SOURCE_LOCATION (first_arg), \
6988 "first %<sizeof%> operand was declared here"); \
6989 } \
6990 } \
6991 break; \
6992 default: \
6993 break; \
6994 } \
6995 stack[sp - 1].expr \
6996 = convert_lvalue_to_rvalue (stack[sp - 1].loc, \
6997 stack[sp - 1].expr, true, true); \
6998 stack[sp].expr \
6999 = convert_lvalue_to_rvalue (stack[sp].loc, \
7000 stack[sp].expr, true, true); \
7001 if (__builtin_expect (omp_atomic_lhs != NULL_TREE, 0) && sp == 1 \
7002 && c_parser_peek_token (parser)->type == CPP_SEMICOLON \
7003 && ((1 << stack[sp].prec) \
7004 & ((1 << PREC_BITOR) | (1 << PREC_BITXOR) | (1 << PREC_BITAND) \
7005 | (1 << PREC_SHIFT) | (1 << PREC_ADD) | (1 << PREC_MULT))) \
7006 && stack[sp].op != TRUNC_MOD_EXPR \
7007 && stack[0].expr.value != error_mark_node \
7008 && stack[1].expr.value != error_mark_node \
7009 && (c_tree_equal (stack[0].expr.value, omp_atomic_lhs) \
7010 || c_tree_equal (stack[1].expr.value, omp_atomic_lhs))) \
7011 stack[0].expr.value \
7012 = build2 (stack[1].op, TREE_TYPE (stack[0].expr.value), \
7013 stack[0].expr.value, stack[1].expr.value); \
7014 else \
7015 stack[sp - 1].expr = parser_build_binary_op (stack[sp].loc, \
7016 stack[sp].op, \
7017 stack[sp - 1].expr, \
7018 stack[sp].expr); \
7019 sp--; \
7020 } while (0)
7021 gcc_assert (!after || c_dialect_objc ());
7022 stack[0].loc = c_parser_peek_token (parser)->location;
7023 stack[0].expr = c_parser_cast_expression (parser, after);
7024 stack[0].prec = PREC_NONE;
7025 stack[0].sizeof_arg = c_last_sizeof_arg;
7026 sp = 0;
7027 while (true)
7028 {
7029 enum c_parser_prec oprec;
7030 enum tree_code ocode;
7031 source_range src_range;
7032 if (parser->error)
7033 goto out;
7034 switch (c_parser_peek_token (parser)->type)
7035 {
7036 case CPP_MULT:
7037 oprec = PREC_MULT;
7038 ocode = MULT_EXPR;
7039 break;
7040 case CPP_DIV:
7041 oprec = PREC_MULT;
7042 ocode = TRUNC_DIV_EXPR;
7043 break;
7044 case CPP_MOD:
7045 oprec = PREC_MULT;
7046 ocode = TRUNC_MOD_EXPR;
7047 break;
7048 case CPP_PLUS:
7049 oprec = PREC_ADD;
7050 ocode = PLUS_EXPR;
7051 break;
7052 case CPP_MINUS:
7053 oprec = PREC_ADD;
7054 ocode = MINUS_EXPR;
7055 break;
7056 case CPP_LSHIFT:
7057 oprec = PREC_SHIFT;
7058 ocode = LSHIFT_EXPR;
7059 break;
7060 case CPP_RSHIFT:
7061 oprec = PREC_SHIFT;
7062 ocode = RSHIFT_EXPR;
7063 break;
7064 case CPP_LESS:
7065 oprec = PREC_REL;
7066 ocode = LT_EXPR;
7067 break;
7068 case CPP_GREATER:
7069 oprec = PREC_REL;
7070 ocode = GT_EXPR;
7071 break;
7072 case CPP_LESS_EQ:
7073 oprec = PREC_REL;
7074 ocode = LE_EXPR;
7075 break;
7076 case CPP_GREATER_EQ:
7077 oprec = PREC_REL;
7078 ocode = GE_EXPR;
7079 break;
7080 case CPP_EQ_EQ:
7081 oprec = PREC_EQ;
7082 ocode = EQ_EXPR;
7083 break;
7084 case CPP_NOT_EQ:
7085 oprec = PREC_EQ;
7086 ocode = NE_EXPR;
7087 break;
7088 case CPP_AND:
7089 oprec = PREC_BITAND;
7090 ocode = BIT_AND_EXPR;
7091 break;
7092 case CPP_XOR:
7093 oprec = PREC_BITXOR;
7094 ocode = BIT_XOR_EXPR;
7095 break;
7096 case CPP_OR:
7097 oprec = PREC_BITOR;
7098 ocode = BIT_IOR_EXPR;
7099 break;
7100 case CPP_AND_AND:
7101 oprec = PREC_LOGAND;
7102 ocode = TRUTH_ANDIF_EXPR;
7103 break;
7104 case CPP_OR_OR:
7105 oprec = PREC_LOGOR;
7106 ocode = TRUTH_ORIF_EXPR;
7107 break;
7108 default:
7109 /* Not a binary operator, so end of the binary
7110 expression. */
7111 goto out;
7112 }
7113 binary_loc = c_parser_peek_token (parser)->location;
7114 while (oprec <= stack[sp].prec)
7115 POP;
7116 c_parser_consume_token (parser);
7117 switch (ocode)
7118 {
7119 case TRUTH_ANDIF_EXPR:
7120 src_range = stack[sp].expr.src_range;
7121 stack[sp].expr
7122 = convert_lvalue_to_rvalue (stack[sp].loc,
7123 stack[sp].expr, true, true);
7124 stack[sp].expr.value = c_objc_common_truthvalue_conversion
7125 (stack[sp].loc, default_conversion (stack[sp].expr.value));
7126 c_inhibit_evaluation_warnings += (stack[sp].expr.value
7127 == truthvalue_false_node);
7128 set_c_expr_source_range (&stack[sp].expr, src_range);
7129 break;
7130 case TRUTH_ORIF_EXPR:
7131 src_range = stack[sp].expr.src_range;
7132 stack[sp].expr
7133 = convert_lvalue_to_rvalue (stack[sp].loc,
7134 stack[sp].expr, true, true);
7135 stack[sp].expr.value = c_objc_common_truthvalue_conversion
7136 (stack[sp].loc, default_conversion (stack[sp].expr.value));
7137 c_inhibit_evaluation_warnings += (stack[sp].expr.value
7138 == truthvalue_true_node);
7139 set_c_expr_source_range (&stack[sp].expr, src_range);
7140 break;
7141 default:
7142 break;
7143 }
7144 sp++;
7145 stack[sp].loc = binary_loc;
7146 stack[sp].expr = c_parser_cast_expression (parser, NULL);
7147 stack[sp].prec = oprec;
7148 stack[sp].op = ocode;
7149 stack[sp].sizeof_arg = c_last_sizeof_arg;
7150 }
7151 out:
7152 while (sp > 0)
7153 POP;
7154 return stack[0].expr;
7155 #undef POP
7156 }
7157
7158 /* Parse a cast expression (C90 6.3.4, C99 6.5.4, C11 6.5.4). If AFTER
7159 is not NULL then it is an Objective-C message expression which is the
7160 primary-expression starting the expression as an initializer.
7161
7162 cast-expression:
7163 unary-expression
7164 ( type-name ) unary-expression
7165 */
7166
7167 static struct c_expr
7168 c_parser_cast_expression (c_parser *parser, struct c_expr *after)
7169 {
7170 location_t cast_loc = c_parser_peek_token (parser)->location;
7171 gcc_assert (!after || c_dialect_objc ());
7172 if (after)
7173 return c_parser_postfix_expression_after_primary (parser,
7174 cast_loc, *after);
7175 /* If the expression begins with a parenthesized type name, it may
7176 be either a cast or a compound literal; we need to see whether
7177 the next character is '{' to tell the difference. If not, it is
7178 an unary expression. Full detection of unknown typenames here
7179 would require a 3-token lookahead. */
7180 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
7181 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
7182 {
7183 struct c_type_name *type_name;
7184 struct c_expr ret;
7185 struct c_expr expr;
7186 matching_parens parens;
7187 parens.consume_open (parser);
7188 type_name = c_parser_type_name (parser, true);
7189 parens.skip_until_found_close (parser);
7190 if (type_name == NULL)
7191 {
7192 ret.set_error ();
7193 ret.original_code = ERROR_MARK;
7194 ret.original_type = NULL;
7195 return ret;
7196 }
7197
7198 /* Save casted types in the function's used types hash table. */
7199 used_types_insert (type_name->specs->type);
7200
7201 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
7202 return c_parser_postfix_expression_after_paren_type (parser, type_name,
7203 cast_loc);
7204 if (type_name->specs->alignas_p)
7205 error_at (type_name->specs->locations[cdw_alignas],
7206 "alignment specified for type name in cast");
7207 {
7208 location_t expr_loc = c_parser_peek_token (parser)->location;
7209 expr = c_parser_cast_expression (parser, NULL);
7210 expr = convert_lvalue_to_rvalue (expr_loc, expr, true, true);
7211 }
7212 ret.value = c_cast_expr (cast_loc, type_name, expr.value);
7213 if (ret.value && expr.value)
7214 set_c_expr_source_range (&ret, cast_loc, expr.get_finish ());
7215 ret.original_code = ERROR_MARK;
7216 ret.original_type = NULL;
7217 return ret;
7218 }
7219 else
7220 return c_parser_unary_expression (parser);
7221 }
7222
7223 /* Parse an unary expression (C90 6.3.3, C99 6.5.3, C11 6.5.3).
7224
7225 unary-expression:
7226 postfix-expression
7227 ++ unary-expression
7228 -- unary-expression
7229 unary-operator cast-expression
7230 sizeof unary-expression
7231 sizeof ( type-name )
7232
7233 unary-operator: one of
7234 & * + - ~ !
7235
7236 GNU extensions:
7237
7238 unary-expression:
7239 __alignof__ unary-expression
7240 __alignof__ ( type-name )
7241 && identifier
7242
7243 (C11 permits _Alignof with type names only.)
7244
7245 unary-operator: one of
7246 __extension__ __real__ __imag__
7247
7248 Transactional Memory:
7249
7250 unary-expression:
7251 transaction-expression
7252
7253 In addition, the GNU syntax treats ++ and -- as unary operators, so
7254 they may be applied to cast expressions with errors for non-lvalues
7255 given later. */
7256
7257 static struct c_expr
7258 c_parser_unary_expression (c_parser *parser)
7259 {
7260 int ext;
7261 struct c_expr ret, op;
7262 location_t op_loc = c_parser_peek_token (parser)->location;
7263 location_t exp_loc;
7264 location_t finish;
7265 ret.original_code = ERROR_MARK;
7266 ret.original_type = NULL;
7267 switch (c_parser_peek_token (parser)->type)
7268 {
7269 case CPP_PLUS_PLUS:
7270 c_parser_consume_token (parser);
7271 exp_loc = c_parser_peek_token (parser)->location;
7272 op = c_parser_cast_expression (parser, NULL);
7273
7274 op = default_function_array_read_conversion (exp_loc, op);
7275 return parser_build_unary_op (op_loc, PREINCREMENT_EXPR, op);
7276 case CPP_MINUS_MINUS:
7277 c_parser_consume_token (parser);
7278 exp_loc = c_parser_peek_token (parser)->location;
7279 op = c_parser_cast_expression (parser, NULL);
7280
7281 op = default_function_array_read_conversion (exp_loc, op);
7282 return parser_build_unary_op (op_loc, PREDECREMENT_EXPR, op);
7283 case CPP_AND:
7284 c_parser_consume_token (parser);
7285 op = c_parser_cast_expression (parser, NULL);
7286 mark_exp_read (op.value);
7287 return parser_build_unary_op (op_loc, ADDR_EXPR, op);
7288 case CPP_MULT:
7289 {
7290 c_parser_consume_token (parser);
7291 exp_loc = c_parser_peek_token (parser)->location;
7292 op = c_parser_cast_expression (parser, NULL);
7293 finish = op.get_finish ();
7294 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
7295 location_t combined_loc = make_location (op_loc, op_loc, finish);
7296 ret.value = build_indirect_ref (combined_loc, op.value, RO_UNARY_STAR);
7297 ret.src_range.m_start = op_loc;
7298 ret.src_range.m_finish = finish;
7299 return ret;
7300 }
7301 case CPP_PLUS:
7302 if (!c_dialect_objc () && !in_system_header_at (input_location))
7303 warning_at (op_loc,
7304 OPT_Wtraditional,
7305 "traditional C rejects the unary plus operator");
7306 c_parser_consume_token (parser);
7307 exp_loc = c_parser_peek_token (parser)->location;
7308 op = c_parser_cast_expression (parser, NULL);
7309 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
7310 return parser_build_unary_op (op_loc, CONVERT_EXPR, op);
7311 case CPP_MINUS:
7312 c_parser_consume_token (parser);
7313 exp_loc = c_parser_peek_token (parser)->location;
7314 op = c_parser_cast_expression (parser, NULL);
7315 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
7316 return parser_build_unary_op (op_loc, NEGATE_EXPR, op);
7317 case CPP_COMPL:
7318 c_parser_consume_token (parser);
7319 exp_loc = c_parser_peek_token (parser)->location;
7320 op = c_parser_cast_expression (parser, NULL);
7321 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
7322 return parser_build_unary_op (op_loc, BIT_NOT_EXPR, op);
7323 case CPP_NOT:
7324 c_parser_consume_token (parser);
7325 exp_loc = c_parser_peek_token (parser)->location;
7326 op = c_parser_cast_expression (parser, NULL);
7327 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
7328 return parser_build_unary_op (op_loc, TRUTH_NOT_EXPR, op);
7329 case CPP_AND_AND:
7330 /* Refer to the address of a label as a pointer. */
7331 c_parser_consume_token (parser);
7332 if (c_parser_next_token_is (parser, CPP_NAME))
7333 {
7334 ret.value = finish_label_address_expr
7335 (c_parser_peek_token (parser)->value, op_loc);
7336 set_c_expr_source_range (&ret, op_loc,
7337 c_parser_peek_token (parser)->get_finish ());
7338 c_parser_consume_token (parser);
7339 }
7340 else
7341 {
7342 c_parser_error (parser, "expected identifier");
7343 ret.set_error ();
7344 }
7345 return ret;
7346 case CPP_KEYWORD:
7347 switch (c_parser_peek_token (parser)->keyword)
7348 {
7349 case RID_SIZEOF:
7350 return c_parser_sizeof_expression (parser);
7351 case RID_ALIGNOF:
7352 return c_parser_alignof_expression (parser);
7353 case RID_BUILTIN_HAS_ATTRIBUTE:
7354 return c_parser_has_attribute_expression (parser);
7355 case RID_EXTENSION:
7356 c_parser_consume_token (parser);
7357 ext = disable_extension_diagnostics ();
7358 ret = c_parser_cast_expression (parser, NULL);
7359 restore_extension_diagnostics (ext);
7360 return ret;
7361 case RID_REALPART:
7362 c_parser_consume_token (parser);
7363 exp_loc = c_parser_peek_token (parser)->location;
7364 op = c_parser_cast_expression (parser, NULL);
7365 op = default_function_array_conversion (exp_loc, op);
7366 return parser_build_unary_op (op_loc, REALPART_EXPR, op);
7367 case RID_IMAGPART:
7368 c_parser_consume_token (parser);
7369 exp_loc = c_parser_peek_token (parser)->location;
7370 op = c_parser_cast_expression (parser, NULL);
7371 op = default_function_array_conversion (exp_loc, op);
7372 return parser_build_unary_op (op_loc, IMAGPART_EXPR, op);
7373 case RID_TRANSACTION_ATOMIC:
7374 case RID_TRANSACTION_RELAXED:
7375 return c_parser_transaction_expression (parser,
7376 c_parser_peek_token (parser)->keyword);
7377 default:
7378 return c_parser_postfix_expression (parser);
7379 }
7380 default:
7381 return c_parser_postfix_expression (parser);
7382 }
7383 }
7384
7385 /* Parse a sizeof expression. */
7386
7387 static struct c_expr
7388 c_parser_sizeof_expression (c_parser *parser)
7389 {
7390 struct c_expr expr;
7391 struct c_expr result;
7392 location_t expr_loc;
7393 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SIZEOF));
7394
7395 location_t start;
7396 location_t finish = UNKNOWN_LOCATION;
7397
7398 start = c_parser_peek_token (parser)->location;
7399
7400 c_parser_consume_token (parser);
7401 c_inhibit_evaluation_warnings++;
7402 in_sizeof++;
7403 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
7404 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
7405 {
7406 /* Either sizeof ( type-name ) or sizeof unary-expression
7407 starting with a compound literal. */
7408 struct c_type_name *type_name;
7409 matching_parens parens;
7410 parens.consume_open (parser);
7411 expr_loc = c_parser_peek_token (parser)->location;
7412 type_name = c_parser_type_name (parser, true);
7413 parens.skip_until_found_close (parser);
7414 finish = parser->tokens_buf[0].location;
7415 if (type_name == NULL)
7416 {
7417 struct c_expr ret;
7418 c_inhibit_evaluation_warnings--;
7419 in_sizeof--;
7420 ret.set_error ();
7421 ret.original_code = ERROR_MARK;
7422 ret.original_type = NULL;
7423 return ret;
7424 }
7425 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
7426 {
7427 expr = c_parser_postfix_expression_after_paren_type (parser,
7428 type_name,
7429 expr_loc);
7430 finish = expr.get_finish ();
7431 goto sizeof_expr;
7432 }
7433 /* sizeof ( type-name ). */
7434 if (type_name->specs->alignas_p)
7435 error_at (type_name->specs->locations[cdw_alignas],
7436 "alignment specified for type name in %<sizeof%>");
7437 c_inhibit_evaluation_warnings--;
7438 in_sizeof--;
7439 result = c_expr_sizeof_type (expr_loc, type_name);
7440 }
7441 else
7442 {
7443 expr_loc = c_parser_peek_token (parser)->location;
7444 expr = c_parser_unary_expression (parser);
7445 finish = expr.get_finish ();
7446 sizeof_expr:
7447 c_inhibit_evaluation_warnings--;
7448 in_sizeof--;
7449 mark_exp_read (expr.value);
7450 if (TREE_CODE (expr.value) == COMPONENT_REF
7451 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
7452 error_at (expr_loc, "%<sizeof%> applied to a bit-field");
7453 result = c_expr_sizeof_expr (expr_loc, expr);
7454 }
7455 if (finish != UNKNOWN_LOCATION)
7456 set_c_expr_source_range (&result, start, finish);
7457 return result;
7458 }
7459
7460 /* Parse an alignof expression. */
7461
7462 static struct c_expr
7463 c_parser_alignof_expression (c_parser *parser)
7464 {
7465 struct c_expr expr;
7466 location_t start_loc = c_parser_peek_token (parser)->location;
7467 location_t end_loc;
7468 tree alignof_spelling = c_parser_peek_token (parser)->value;
7469 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNOF));
7470 bool is_c11_alignof = strcmp (IDENTIFIER_POINTER (alignof_spelling),
7471 "_Alignof") == 0;
7472 /* A diagnostic is not required for the use of this identifier in
7473 the implementation namespace; only diagnose it for the C11
7474 spelling because of existing code using the other spellings. */
7475 if (is_c11_alignof)
7476 {
7477 if (flag_isoc99)
7478 pedwarn_c99 (start_loc, OPT_Wpedantic, "ISO C99 does not support %qE",
7479 alignof_spelling);
7480 else
7481 pedwarn_c99 (start_loc, OPT_Wpedantic, "ISO C90 does not support %qE",
7482 alignof_spelling);
7483 }
7484 c_parser_consume_token (parser);
7485 c_inhibit_evaluation_warnings++;
7486 in_alignof++;
7487 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
7488 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
7489 {
7490 /* Either __alignof__ ( type-name ) or __alignof__
7491 unary-expression starting with a compound literal. */
7492 location_t loc;
7493 struct c_type_name *type_name;
7494 struct c_expr ret;
7495 matching_parens parens;
7496 parens.consume_open (parser);
7497 loc = c_parser_peek_token (parser)->location;
7498 type_name = c_parser_type_name (parser, true);
7499 end_loc = c_parser_peek_token (parser)->location;
7500 parens.skip_until_found_close (parser);
7501 if (type_name == NULL)
7502 {
7503 struct c_expr ret;
7504 c_inhibit_evaluation_warnings--;
7505 in_alignof--;
7506 ret.set_error ();
7507 ret.original_code = ERROR_MARK;
7508 ret.original_type = NULL;
7509 return ret;
7510 }
7511 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
7512 {
7513 expr = c_parser_postfix_expression_after_paren_type (parser,
7514 type_name,
7515 loc);
7516 goto alignof_expr;
7517 }
7518 /* alignof ( type-name ). */
7519 if (type_name->specs->alignas_p)
7520 error_at (type_name->specs->locations[cdw_alignas],
7521 "alignment specified for type name in %qE",
7522 alignof_spelling);
7523 c_inhibit_evaluation_warnings--;
7524 in_alignof--;
7525 ret.value = c_sizeof_or_alignof_type (loc, groktypename (type_name,
7526 NULL, NULL),
7527 false, is_c11_alignof, 1);
7528 ret.original_code = ERROR_MARK;
7529 ret.original_type = NULL;
7530 set_c_expr_source_range (&ret, start_loc, end_loc);
7531 return ret;
7532 }
7533 else
7534 {
7535 struct c_expr ret;
7536 expr = c_parser_unary_expression (parser);
7537 end_loc = expr.src_range.m_finish;
7538 alignof_expr:
7539 mark_exp_read (expr.value);
7540 c_inhibit_evaluation_warnings--;
7541 in_alignof--;
7542 if (is_c11_alignof)
7543 pedwarn (start_loc,
7544 OPT_Wpedantic, "ISO C does not allow %<%E (expression)%>",
7545 alignof_spelling);
7546 ret.value = c_alignof_expr (start_loc, expr.value);
7547 ret.original_code = ERROR_MARK;
7548 ret.original_type = NULL;
7549 set_c_expr_source_range (&ret, start_loc, end_loc);
7550 return ret;
7551 }
7552 }
7553
7554 /* Parse the __builtin_has_attribute ([expr|type], attribute-spec)
7555 expression. */
7556
7557 static struct c_expr
7558 c_parser_has_attribute_expression (c_parser *parser)
7559 {
7560 gcc_assert (c_parser_next_token_is_keyword (parser,
7561 RID_BUILTIN_HAS_ATTRIBUTE));
7562 c_parser_consume_token (parser);
7563
7564 c_inhibit_evaluation_warnings++;
7565
7566 matching_parens parens;
7567 if (!parens.require_open (parser))
7568 {
7569 c_inhibit_evaluation_warnings--;
7570 in_typeof--;
7571
7572 struct c_expr result;
7573 result.set_error ();
7574 result.original_code = ERROR_MARK;
7575 result.original_type = NULL;
7576 return result;
7577 }
7578
7579 /* Treat the type argument the same way as in typeof for the purposes
7580 of warnings. FIXME: Generalize this so the warning refers to
7581 __builtin_has_attribute rather than typeof. */
7582 in_typeof++;
7583
7584 /* The first operand: one of DECL, EXPR, or TYPE. */
7585 tree oper = NULL_TREE;
7586 if (c_parser_next_tokens_start_typename (parser, cla_prefer_id))
7587 {
7588 struct c_type_name *tname = c_parser_type_name (parser);
7589 in_typeof--;
7590 if (tname)
7591 {
7592 oper = groktypename (tname, NULL, NULL);
7593 pop_maybe_used (variably_modified_type_p (oper, NULL_TREE));
7594 }
7595 }
7596 else
7597 {
7598 struct c_expr cexpr = c_parser_expr_no_commas (parser, NULL);
7599 c_inhibit_evaluation_warnings--;
7600 in_typeof--;
7601 if (cexpr.value != error_mark_node)
7602 {
7603 mark_exp_read (cexpr.value);
7604 oper = cexpr.value;
7605 tree etype = TREE_TYPE (oper);
7606 bool was_vm = variably_modified_type_p (etype, NULL_TREE);
7607 /* This is returned with the type so that when the type is
7608 evaluated, this can be evaluated. */
7609 if (was_vm)
7610 oper = c_fully_fold (oper, false, NULL);
7611 pop_maybe_used (was_vm);
7612 }
7613 }
7614
7615 struct c_expr result;
7616 result.original_code = ERROR_MARK;
7617 result.original_type = NULL;
7618
7619 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
7620 {
7621 /* Consume the closing parenthesis if that's the next token
7622 in the likely case the built-in was invoked with fewer
7623 than two arguments. */
7624 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
7625 c_parser_consume_token (parser);
7626 c_inhibit_evaluation_warnings--;
7627 result.set_error ();
7628 return result;
7629 }
7630
7631 parser->lex_untranslated_string = true;
7632
7633 location_t atloc = c_parser_peek_token (parser)->location;
7634 /* Parse a single attribute. Require no leading comma and do not
7635 allow empty attributes. */
7636 tree attr = c_parser_attribute (parser, NULL_TREE, false, false);
7637
7638 parser->lex_untranslated_string = false;
7639
7640 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
7641 c_parser_consume_token (parser);
7642 else
7643 {
7644 c_parser_error (parser, "expected identifier");
7645 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7646
7647 result.set_error ();
7648 return result;
7649 }
7650
7651 if (!attr)
7652 {
7653 error_at (atloc, "expected identifier");
7654 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7655 "expected %<)%>");
7656 result.set_error ();
7657 return result;
7658 }
7659
7660 result.original_code = INTEGER_CST;
7661 result.original_type = boolean_type_node;
7662
7663 if (has_attribute (atloc, oper, attr, default_conversion))
7664 result.value = boolean_true_node;
7665 else
7666 result.value = boolean_false_node;
7667
7668 return result;
7669 }
7670
7671 /* Helper function to read arguments of builtins which are interfaces
7672 for the middle-end nodes like COMPLEX_EXPR, VEC_PERM_EXPR and
7673 others. The name of the builtin is passed using BNAME parameter.
7674 Function returns true if there were no errors while parsing and
7675 stores the arguments in CEXPR_LIST. If it returns true,
7676 *OUT_CLOSE_PAREN_LOC is written to with the location of the closing
7677 parenthesis. */
7678 static bool
7679 c_parser_get_builtin_args (c_parser *parser, const char *bname,
7680 vec<c_expr_t, va_gc> **ret_cexpr_list,
7681 bool choose_expr_p,
7682 location_t *out_close_paren_loc)
7683 {
7684 location_t loc = c_parser_peek_token (parser)->location;
7685 vec<c_expr_t, va_gc> *cexpr_list;
7686 c_expr_t expr;
7687 bool saved_force_folding_builtin_constant_p;
7688
7689 *ret_cexpr_list = NULL;
7690 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
7691 {
7692 error_at (loc, "cannot take address of %qs", bname);
7693 return false;
7694 }
7695
7696 c_parser_consume_token (parser);
7697
7698 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
7699 {
7700 *out_close_paren_loc = c_parser_peek_token (parser)->location;
7701 c_parser_consume_token (parser);
7702 return true;
7703 }
7704
7705 saved_force_folding_builtin_constant_p
7706 = force_folding_builtin_constant_p;
7707 force_folding_builtin_constant_p |= choose_expr_p;
7708 expr = c_parser_expr_no_commas (parser, NULL);
7709 force_folding_builtin_constant_p
7710 = saved_force_folding_builtin_constant_p;
7711 vec_alloc (cexpr_list, 1);
7712 vec_safe_push (cexpr_list, expr);
7713 while (c_parser_next_token_is (parser, CPP_COMMA))
7714 {
7715 c_parser_consume_token (parser);
7716 expr = c_parser_expr_no_commas (parser, NULL);
7717 vec_safe_push (cexpr_list, expr);
7718 }
7719
7720 *out_close_paren_loc = c_parser_peek_token (parser)->location;
7721 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
7722 return false;
7723
7724 *ret_cexpr_list = cexpr_list;
7725 return true;
7726 }
7727
7728 /* This represents a single generic-association. */
7729
7730 struct c_generic_association
7731 {
7732 /* The location of the starting token of the type. */
7733 location_t type_location;
7734 /* The association's type, or NULL_TREE for 'default'. */
7735 tree type;
7736 /* The association's expression. */
7737 struct c_expr expression;
7738 };
7739
7740 /* Parse a generic-selection. (C11 6.5.1.1).
7741
7742 generic-selection:
7743 _Generic ( assignment-expression , generic-assoc-list )
7744
7745 generic-assoc-list:
7746 generic-association
7747 generic-assoc-list , generic-association
7748
7749 generic-association:
7750 type-name : assignment-expression
7751 default : assignment-expression
7752 */
7753
7754 static struct c_expr
7755 c_parser_generic_selection (c_parser *parser)
7756 {
7757 struct c_expr selector, error_expr;
7758 tree selector_type;
7759 struct c_generic_association matched_assoc;
7760 bool match_found = false;
7761 location_t generic_loc, selector_loc;
7762
7763 error_expr.original_code = ERROR_MARK;
7764 error_expr.original_type = NULL;
7765 error_expr.set_error ();
7766 matched_assoc.type_location = UNKNOWN_LOCATION;
7767 matched_assoc.type = NULL_TREE;
7768 matched_assoc.expression = error_expr;
7769
7770 gcc_assert (c_parser_next_token_is_keyword (parser, RID_GENERIC));
7771 generic_loc = c_parser_peek_token (parser)->location;
7772 c_parser_consume_token (parser);
7773 if (flag_isoc99)
7774 pedwarn_c99 (generic_loc, OPT_Wpedantic,
7775 "ISO C99 does not support %<_Generic%>");
7776 else
7777 pedwarn_c99 (generic_loc, OPT_Wpedantic,
7778 "ISO C90 does not support %<_Generic%>");
7779
7780 matching_parens parens;
7781 if (!parens.require_open (parser))
7782 return error_expr;
7783
7784 c_inhibit_evaluation_warnings++;
7785 selector_loc = c_parser_peek_token (parser)->location;
7786 selector = c_parser_expr_no_commas (parser, NULL);
7787 selector = default_function_array_conversion (selector_loc, selector);
7788 c_inhibit_evaluation_warnings--;
7789
7790 if (selector.value == error_mark_node)
7791 {
7792 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7793 return selector;
7794 }
7795 selector_type = TREE_TYPE (selector.value);
7796 /* In ISO C terms, rvalues (including the controlling expression of
7797 _Generic) do not have qualified types. */
7798 if (TREE_CODE (selector_type) != ARRAY_TYPE)
7799 selector_type = TYPE_MAIN_VARIANT (selector_type);
7800 /* In ISO C terms, _Noreturn is not part of the type of expressions
7801 such as &abort, but in GCC it is represented internally as a type
7802 qualifier. */
7803 if (FUNCTION_POINTER_TYPE_P (selector_type)
7804 && TYPE_QUALS (TREE_TYPE (selector_type)) != TYPE_UNQUALIFIED)
7805 selector_type
7806 = build_pointer_type (TYPE_MAIN_VARIANT (TREE_TYPE (selector_type)));
7807
7808 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
7809 {
7810 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7811 return error_expr;
7812 }
7813
7814 auto_vec<c_generic_association> associations;
7815 while (1)
7816 {
7817 struct c_generic_association assoc, *iter;
7818 unsigned int ix;
7819 c_token *token = c_parser_peek_token (parser);
7820
7821 assoc.type_location = token->location;
7822 if (token->type == CPP_KEYWORD && token->keyword == RID_DEFAULT)
7823 {
7824 c_parser_consume_token (parser);
7825 assoc.type = NULL_TREE;
7826 }
7827 else
7828 {
7829 struct c_type_name *type_name;
7830
7831 type_name = c_parser_type_name (parser);
7832 if (type_name == NULL)
7833 {
7834 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7835 return error_expr;
7836 }
7837 assoc.type = groktypename (type_name, NULL, NULL);
7838 if (assoc.type == error_mark_node)
7839 {
7840 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7841 return error_expr;
7842 }
7843
7844 if (TREE_CODE (assoc.type) == FUNCTION_TYPE)
7845 error_at (assoc.type_location,
7846 "%<_Generic%> association has function type");
7847 else if (!COMPLETE_TYPE_P (assoc.type))
7848 error_at (assoc.type_location,
7849 "%<_Generic%> association has incomplete type");
7850
7851 if (variably_modified_type_p (assoc.type, NULL_TREE))
7852 error_at (assoc.type_location,
7853 "%<_Generic%> association has "
7854 "variable length type");
7855 }
7856
7857 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
7858 {
7859 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7860 return error_expr;
7861 }
7862
7863 assoc.expression = c_parser_expr_no_commas (parser, NULL);
7864 if (assoc.expression.value == error_mark_node)
7865 {
7866 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7867 return error_expr;
7868 }
7869
7870 for (ix = 0; associations.iterate (ix, &iter); ++ix)
7871 {
7872 if (assoc.type == NULL_TREE)
7873 {
7874 if (iter->type == NULL_TREE)
7875 {
7876 error_at (assoc.type_location,
7877 "duplicate %<default%> case in %<_Generic%>");
7878 inform (iter->type_location, "original %<default%> is here");
7879 }
7880 }
7881 else if (iter->type != NULL_TREE)
7882 {
7883 if (comptypes (assoc.type, iter->type))
7884 {
7885 error_at (assoc.type_location,
7886 "%<_Generic%> specifies two compatible types");
7887 inform (iter->type_location, "compatible type is here");
7888 }
7889 }
7890 }
7891
7892 if (assoc.type == NULL_TREE)
7893 {
7894 if (!match_found)
7895 {
7896 matched_assoc = assoc;
7897 match_found = true;
7898 }
7899 }
7900 else if (comptypes (assoc.type, selector_type))
7901 {
7902 if (!match_found || matched_assoc.type == NULL_TREE)
7903 {
7904 matched_assoc = assoc;
7905 match_found = true;
7906 }
7907 else
7908 {
7909 error_at (assoc.type_location,
7910 "%<_Generic%> selector matches multiple associations");
7911 inform (matched_assoc.type_location,
7912 "other match is here");
7913 }
7914 }
7915
7916 associations.safe_push (assoc);
7917
7918 if (c_parser_peek_token (parser)->type != CPP_COMMA)
7919 break;
7920 c_parser_consume_token (parser);
7921 }
7922
7923 if (!parens.require_close (parser))
7924 {
7925 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7926 return error_expr;
7927 }
7928
7929 if (!match_found)
7930 {
7931 error_at (selector_loc, "%<_Generic%> selector of type %qT is not "
7932 "compatible with any association",
7933 selector_type);
7934 return error_expr;
7935 }
7936
7937 return matched_assoc.expression;
7938 }
7939
7940 /* Check the validity of a function pointer argument *EXPR (argument
7941 position POS) to __builtin_tgmath. Return the number of function
7942 arguments if possibly valid; return 0 having reported an error if
7943 not valid. */
7944
7945 static unsigned int
7946 check_tgmath_function (c_expr *expr, unsigned int pos)
7947 {
7948 tree type = TREE_TYPE (expr->value);
7949 if (!FUNCTION_POINTER_TYPE_P (type))
7950 {
7951 error_at (expr->get_location (),
7952 "argument %u of %<__builtin_tgmath%> is not a function pointer",
7953 pos);
7954 return 0;
7955 }
7956 type = TREE_TYPE (type);
7957 if (!prototype_p (type))
7958 {
7959 error_at (expr->get_location (),
7960 "argument %u of %<__builtin_tgmath%> is unprototyped", pos);
7961 return 0;
7962 }
7963 if (stdarg_p (type))
7964 {
7965 error_at (expr->get_location (),
7966 "argument %u of %<__builtin_tgmath%> has variable arguments",
7967 pos);
7968 return 0;
7969 }
7970 unsigned int nargs = 0;
7971 function_args_iterator iter;
7972 tree t;
7973 FOREACH_FUNCTION_ARGS (type, t, iter)
7974 {
7975 if (t == void_type_node)
7976 break;
7977 nargs++;
7978 }
7979 if (nargs == 0)
7980 {
7981 error_at (expr->get_location (),
7982 "argument %u of %<__builtin_tgmath%> has no arguments", pos);
7983 return 0;
7984 }
7985 return nargs;
7986 }
7987
7988 /* Ways in which a parameter or return value of a type-generic macro
7989 may vary between the different functions the macro may call. */
7990 enum tgmath_parm_kind
7991 {
7992 tgmath_fixed, tgmath_real, tgmath_complex
7993 };
7994
7995 /* Parse a postfix expression (C90 6.3.1-6.3.2, C99 6.5.1-6.5.2,
7996 C11 6.5.1-6.5.2). Compound literals aren't handled here; callers have to
7997 call c_parser_postfix_expression_after_paren_type on encountering them.
7998
7999 postfix-expression:
8000 primary-expression
8001 postfix-expression [ expression ]
8002 postfix-expression ( argument-expression-list[opt] )
8003 postfix-expression . identifier
8004 postfix-expression -> identifier
8005 postfix-expression ++
8006 postfix-expression --
8007 ( type-name ) { initializer-list }
8008 ( type-name ) { initializer-list , }
8009
8010 argument-expression-list:
8011 argument-expression
8012 argument-expression-list , argument-expression
8013
8014 primary-expression:
8015 identifier
8016 constant
8017 string-literal
8018 ( expression )
8019 generic-selection
8020
8021 GNU extensions:
8022
8023 primary-expression:
8024 __func__
8025 (treated as a keyword in GNU C)
8026 __FUNCTION__
8027 __PRETTY_FUNCTION__
8028 ( compound-statement )
8029 __builtin_va_arg ( assignment-expression , type-name )
8030 __builtin_offsetof ( type-name , offsetof-member-designator )
8031 __builtin_choose_expr ( assignment-expression ,
8032 assignment-expression ,
8033 assignment-expression )
8034 __builtin_types_compatible_p ( type-name , type-name )
8035 __builtin_tgmath ( expr-list )
8036 __builtin_complex ( assignment-expression , assignment-expression )
8037 __builtin_shuffle ( assignment-expression , assignment-expression )
8038 __builtin_shuffle ( assignment-expression ,
8039 assignment-expression ,
8040 assignment-expression, )
8041
8042 offsetof-member-designator:
8043 identifier
8044 offsetof-member-designator . identifier
8045 offsetof-member-designator [ expression ]
8046
8047 Objective-C:
8048
8049 primary-expression:
8050 [ objc-receiver objc-message-args ]
8051 @selector ( objc-selector-arg )
8052 @protocol ( identifier )
8053 @encode ( type-name )
8054 objc-string-literal
8055 Classname . identifier
8056 */
8057
8058 static struct c_expr
8059 c_parser_postfix_expression (c_parser *parser)
8060 {
8061 struct c_expr expr, e1;
8062 struct c_type_name *t1, *t2;
8063 location_t loc = c_parser_peek_token (parser)->location;
8064 source_range tok_range = c_parser_peek_token (parser)->get_range ();
8065 expr.original_code = ERROR_MARK;
8066 expr.original_type = NULL;
8067 switch (c_parser_peek_token (parser)->type)
8068 {
8069 case CPP_NUMBER:
8070 expr.value = c_parser_peek_token (parser)->value;
8071 set_c_expr_source_range (&expr, tok_range);
8072 loc = c_parser_peek_token (parser)->location;
8073 c_parser_consume_token (parser);
8074 if (TREE_CODE (expr.value) == FIXED_CST
8075 && !targetm.fixed_point_supported_p ())
8076 {
8077 error_at (loc, "fixed-point types not supported for this target");
8078 expr.set_error ();
8079 }
8080 break;
8081 case CPP_CHAR:
8082 case CPP_CHAR16:
8083 case CPP_CHAR32:
8084 case CPP_WCHAR:
8085 expr.value = c_parser_peek_token (parser)->value;
8086 /* For the purpose of warning when a pointer is compared with
8087 a zero character constant. */
8088 expr.original_type = char_type_node;
8089 set_c_expr_source_range (&expr, tok_range);
8090 c_parser_consume_token (parser);
8091 break;
8092 case CPP_STRING:
8093 case CPP_STRING16:
8094 case CPP_STRING32:
8095 case CPP_WSTRING:
8096 case CPP_UTF8STRING:
8097 expr.value = c_parser_peek_token (parser)->value;
8098 set_c_expr_source_range (&expr, tok_range);
8099 expr.original_code = STRING_CST;
8100 c_parser_consume_token (parser);
8101 break;
8102 case CPP_OBJC_STRING:
8103 gcc_assert (c_dialect_objc ());
8104 expr.value
8105 = objc_build_string_object (c_parser_peek_token (parser)->value);
8106 set_c_expr_source_range (&expr, tok_range);
8107 c_parser_consume_token (parser);
8108 break;
8109 case CPP_NAME:
8110 switch (c_parser_peek_token (parser)->id_kind)
8111 {
8112 case C_ID_ID:
8113 {
8114 tree id = c_parser_peek_token (parser)->value;
8115 c_parser_consume_token (parser);
8116 expr.value = build_external_ref (loc, id,
8117 (c_parser_peek_token (parser)->type
8118 == CPP_OPEN_PAREN),
8119 &expr.original_type);
8120 set_c_expr_source_range (&expr, tok_range);
8121 break;
8122 }
8123 case C_ID_CLASSNAME:
8124 {
8125 /* Here we parse the Objective-C 2.0 Class.name dot
8126 syntax. */
8127 tree class_name = c_parser_peek_token (parser)->value;
8128 tree component;
8129 c_parser_consume_token (parser);
8130 gcc_assert (c_dialect_objc ());
8131 if (!c_parser_require (parser, CPP_DOT, "expected %<.%>"))
8132 {
8133 expr.set_error ();
8134 break;
8135 }
8136 if (c_parser_next_token_is_not (parser, CPP_NAME))
8137 {
8138 c_parser_error (parser, "expected identifier");
8139 expr.set_error ();
8140 break;
8141 }
8142 c_token *component_tok = c_parser_peek_token (parser);
8143 component = component_tok->value;
8144 location_t end_loc = component_tok->get_finish ();
8145 c_parser_consume_token (parser);
8146 expr.value = objc_build_class_component_ref (class_name,
8147 component);
8148 set_c_expr_source_range (&expr, loc, end_loc);
8149 break;
8150 }
8151 default:
8152 c_parser_error (parser, "expected expression");
8153 expr.set_error ();
8154 break;
8155 }
8156 break;
8157 case CPP_OPEN_PAREN:
8158 /* A parenthesized expression, statement expression or compound
8159 literal. */
8160 if (c_parser_peek_2nd_token (parser)->type == CPP_OPEN_BRACE)
8161 {
8162 /* A statement expression. */
8163 tree stmt;
8164 location_t brace_loc;
8165 c_parser_consume_token (parser);
8166 brace_loc = c_parser_peek_token (parser)->location;
8167 c_parser_consume_token (parser);
8168 /* If we've not yet started the current function's statement list,
8169 or we're in the parameter scope of an old-style function
8170 declaration, statement expressions are not allowed. */
8171 if (!building_stmt_list_p () || old_style_parameter_scope ())
8172 {
8173 error_at (loc, "braced-group within expression allowed "
8174 "only inside a function");
8175 parser->error = true;
8176 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
8177 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
8178 expr.set_error ();
8179 break;
8180 }
8181 stmt = c_begin_stmt_expr ();
8182 c_parser_compound_statement_nostart (parser);
8183 location_t close_loc = c_parser_peek_token (parser)->location;
8184 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
8185 "expected %<)%>");
8186 pedwarn (loc, OPT_Wpedantic,
8187 "ISO C forbids braced-groups within expressions");
8188 expr.value = c_finish_stmt_expr (brace_loc, stmt);
8189 set_c_expr_source_range (&expr, loc, close_loc);
8190 mark_exp_read (expr.value);
8191 }
8192 else
8193 {
8194 /* A parenthesized expression. */
8195 location_t loc_open_paren = c_parser_peek_token (parser)->location;
8196 c_parser_consume_token (parser);
8197 expr = c_parser_expression (parser);
8198 if (TREE_CODE (expr.value) == MODIFY_EXPR)
8199 TREE_NO_WARNING (expr.value) = 1;
8200 if (expr.original_code != C_MAYBE_CONST_EXPR
8201 && expr.original_code != SIZEOF_EXPR)
8202 expr.original_code = ERROR_MARK;
8203 /* Don't change EXPR.ORIGINAL_TYPE. */
8204 location_t loc_close_paren = c_parser_peek_token (parser)->location;
8205 set_c_expr_source_range (&expr, loc_open_paren, loc_close_paren);
8206 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
8207 "expected %<)%>", loc_open_paren);
8208 }
8209 break;
8210 case CPP_KEYWORD:
8211 switch (c_parser_peek_token (parser)->keyword)
8212 {
8213 case RID_FUNCTION_NAME:
8214 pedwarn (loc, OPT_Wpedantic, "ISO C does not support "
8215 "%<__FUNCTION__%> predefined identifier");
8216 expr.value = fname_decl (loc,
8217 c_parser_peek_token (parser)->keyword,
8218 c_parser_peek_token (parser)->value);
8219 set_c_expr_source_range (&expr, loc, loc);
8220 c_parser_consume_token (parser);
8221 break;
8222 case RID_PRETTY_FUNCTION_NAME:
8223 pedwarn (loc, OPT_Wpedantic, "ISO C does not support "
8224 "%<__PRETTY_FUNCTION__%> predefined identifier");
8225 expr.value = fname_decl (loc,
8226 c_parser_peek_token (parser)->keyword,
8227 c_parser_peek_token (parser)->value);
8228 set_c_expr_source_range (&expr, loc, loc);
8229 c_parser_consume_token (parser);
8230 break;
8231 case RID_C99_FUNCTION_NAME:
8232 pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not support "
8233 "%<__func__%> predefined identifier");
8234 expr.value = fname_decl (loc,
8235 c_parser_peek_token (parser)->keyword,
8236 c_parser_peek_token (parser)->value);
8237 set_c_expr_source_range (&expr, loc, loc);
8238 c_parser_consume_token (parser);
8239 break;
8240 case RID_VA_ARG:
8241 {
8242 location_t start_loc = loc;
8243 c_parser_consume_token (parser);
8244 matching_parens parens;
8245 if (!parens.require_open (parser))
8246 {
8247 expr.set_error ();
8248 break;
8249 }
8250 e1 = c_parser_expr_no_commas (parser, NULL);
8251 mark_exp_read (e1.value);
8252 e1.value = c_fully_fold (e1.value, false, NULL);
8253 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
8254 {
8255 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
8256 expr.set_error ();
8257 break;
8258 }
8259 loc = c_parser_peek_token (parser)->location;
8260 t1 = c_parser_type_name (parser);
8261 location_t end_loc = c_parser_peek_token (parser)->get_finish ();
8262 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
8263 "expected %<)%>");
8264 if (t1 == NULL)
8265 {
8266 expr.set_error ();
8267 }
8268 else
8269 {
8270 tree type_expr = NULL_TREE;
8271 expr.value = c_build_va_arg (start_loc, e1.value, loc,
8272 groktypename (t1, &type_expr, NULL));
8273 if (type_expr)
8274 {
8275 expr.value = build2 (C_MAYBE_CONST_EXPR,
8276 TREE_TYPE (expr.value), type_expr,
8277 expr.value);
8278 C_MAYBE_CONST_EXPR_NON_CONST (expr.value) = true;
8279 }
8280 set_c_expr_source_range (&expr, start_loc, end_loc);
8281 }
8282 }
8283 break;
8284 case RID_OFFSETOF:
8285 {
8286 c_parser_consume_token (parser);
8287 matching_parens parens;
8288 if (!parens.require_open (parser))
8289 {
8290 expr.set_error ();
8291 break;
8292 }
8293 t1 = c_parser_type_name (parser);
8294 if (t1 == NULL)
8295 parser->error = true;
8296 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
8297 gcc_assert (parser->error);
8298 if (parser->error)
8299 {
8300 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
8301 expr.set_error ();
8302 break;
8303 }
8304 tree type = groktypename (t1, NULL, NULL);
8305 tree offsetof_ref;
8306 if (type == error_mark_node)
8307 offsetof_ref = error_mark_node;
8308 else
8309 {
8310 offsetof_ref = build1 (INDIRECT_REF, type, null_pointer_node);
8311 SET_EXPR_LOCATION (offsetof_ref, loc);
8312 }
8313 /* Parse the second argument to __builtin_offsetof. We
8314 must have one identifier, and beyond that we want to
8315 accept sub structure and sub array references. */
8316 if (c_parser_next_token_is (parser, CPP_NAME))
8317 {
8318 c_token *comp_tok = c_parser_peek_token (parser);
8319 offsetof_ref = build_component_ref
8320 (loc, offsetof_ref, comp_tok->value, comp_tok->location);
8321 c_parser_consume_token (parser);
8322 while (c_parser_next_token_is (parser, CPP_DOT)
8323 || c_parser_next_token_is (parser,
8324 CPP_OPEN_SQUARE)
8325 || c_parser_next_token_is (parser,
8326 CPP_DEREF))
8327 {
8328 if (c_parser_next_token_is (parser, CPP_DEREF))
8329 {
8330 loc = c_parser_peek_token (parser)->location;
8331 offsetof_ref = build_array_ref (loc,
8332 offsetof_ref,
8333 integer_zero_node);
8334 goto do_dot;
8335 }
8336 else if (c_parser_next_token_is (parser, CPP_DOT))
8337 {
8338 do_dot:
8339 c_parser_consume_token (parser);
8340 if (c_parser_next_token_is_not (parser,
8341 CPP_NAME))
8342 {
8343 c_parser_error (parser, "expected identifier");
8344 break;
8345 }
8346 c_token *comp_tok = c_parser_peek_token (parser);
8347 offsetof_ref = build_component_ref
8348 (loc, offsetof_ref, comp_tok->value,
8349 comp_tok->location);
8350 c_parser_consume_token (parser);
8351 }
8352 else
8353 {
8354 struct c_expr ce;
8355 tree idx;
8356 loc = c_parser_peek_token (parser)->location;
8357 c_parser_consume_token (parser);
8358 ce = c_parser_expression (parser);
8359 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
8360 idx = ce.value;
8361 idx = c_fully_fold (idx, false, NULL);
8362 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
8363 "expected %<]%>");
8364 offsetof_ref = build_array_ref (loc, offsetof_ref, idx);
8365 }
8366 }
8367 }
8368 else
8369 c_parser_error (parser, "expected identifier");
8370 location_t end_loc = c_parser_peek_token (parser)->get_finish ();
8371 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
8372 "expected %<)%>");
8373 expr.value = fold_offsetof (offsetof_ref);
8374 set_c_expr_source_range (&expr, loc, end_loc);
8375 }
8376 break;
8377 case RID_CHOOSE_EXPR:
8378 {
8379 vec<c_expr_t, va_gc> *cexpr_list;
8380 c_expr_t *e1_p, *e2_p, *e3_p;
8381 tree c;
8382 location_t close_paren_loc;
8383
8384 c_parser_consume_token (parser);
8385 if (!c_parser_get_builtin_args (parser,
8386 "__builtin_choose_expr",
8387 &cexpr_list, true,
8388 &close_paren_loc))
8389 {
8390 expr.set_error ();
8391 break;
8392 }
8393
8394 if (vec_safe_length (cexpr_list) != 3)
8395 {
8396 error_at (loc, "wrong number of arguments to "
8397 "%<__builtin_choose_expr%>");
8398 expr.set_error ();
8399 break;
8400 }
8401
8402 e1_p = &(*cexpr_list)[0];
8403 e2_p = &(*cexpr_list)[1];
8404 e3_p = &(*cexpr_list)[2];
8405
8406 c = e1_p->value;
8407 mark_exp_read (e2_p->value);
8408 mark_exp_read (e3_p->value);
8409 if (TREE_CODE (c) != INTEGER_CST
8410 || !INTEGRAL_TYPE_P (TREE_TYPE (c)))
8411 error_at (loc,
8412 "first argument to %<__builtin_choose_expr%> not"
8413 " a constant");
8414 constant_expression_warning (c);
8415 expr = integer_zerop (c) ? *e3_p : *e2_p;
8416 set_c_expr_source_range (&expr, loc, close_paren_loc);
8417 break;
8418 }
8419 case RID_TYPES_COMPATIBLE_P:
8420 {
8421 c_parser_consume_token (parser);
8422 matching_parens parens;
8423 if (!parens.require_open (parser))
8424 {
8425 expr.set_error ();
8426 break;
8427 }
8428 t1 = c_parser_type_name (parser);
8429 if (t1 == NULL)
8430 {
8431 expr.set_error ();
8432 break;
8433 }
8434 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
8435 {
8436 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
8437 expr.set_error ();
8438 break;
8439 }
8440 t2 = c_parser_type_name (parser);
8441 if (t2 == NULL)
8442 {
8443 expr.set_error ();
8444 break;
8445 }
8446 location_t close_paren_loc = c_parser_peek_token (parser)->location;
8447 parens.skip_until_found_close (parser);
8448 tree e1, e2;
8449 e1 = groktypename (t1, NULL, NULL);
8450 e2 = groktypename (t2, NULL, NULL);
8451 if (e1 == error_mark_node || e2 == error_mark_node)
8452 {
8453 expr.set_error ();
8454 break;
8455 }
8456
8457 e1 = TYPE_MAIN_VARIANT (e1);
8458 e2 = TYPE_MAIN_VARIANT (e2);
8459
8460 expr.value
8461 = comptypes (e1, e2) ? integer_one_node : integer_zero_node;
8462 set_c_expr_source_range (&expr, loc, close_paren_loc);
8463 }
8464 break;
8465 case RID_BUILTIN_TGMATH:
8466 {
8467 vec<c_expr_t, va_gc> *cexpr_list;
8468 location_t close_paren_loc;
8469
8470 c_parser_consume_token (parser);
8471 if (!c_parser_get_builtin_args (parser,
8472 "__builtin_tgmath",
8473 &cexpr_list, false,
8474 &close_paren_loc))
8475 {
8476 expr.set_error ();
8477 break;
8478 }
8479
8480 if (vec_safe_length (cexpr_list) < 3)
8481 {
8482 error_at (loc, "too few arguments to %<__builtin_tgmath%>");
8483 expr.set_error ();
8484 break;
8485 }
8486
8487 unsigned int i;
8488 c_expr_t *p;
8489 FOR_EACH_VEC_ELT (*cexpr_list, i, p)
8490 *p = convert_lvalue_to_rvalue (loc, *p, true, true);
8491 unsigned int nargs = check_tgmath_function (&(*cexpr_list)[0], 1);
8492 if (nargs == 0)
8493 {
8494 expr.set_error ();
8495 break;
8496 }
8497 if (vec_safe_length (cexpr_list) < nargs)
8498 {
8499 error_at (loc, "too few arguments to %<__builtin_tgmath%>");
8500 expr.set_error ();
8501 break;
8502 }
8503 unsigned int num_functions = vec_safe_length (cexpr_list) - nargs;
8504 if (num_functions < 2)
8505 {
8506 error_at (loc, "too few arguments to %<__builtin_tgmath%>");
8507 expr.set_error ();
8508 break;
8509 }
8510
8511 /* The first NUM_FUNCTIONS expressions are the function
8512 pointers. The remaining NARGS expressions are the
8513 arguments that are to be passed to one of those
8514 functions, chosen following <tgmath.h> rules. */
8515 for (unsigned int j = 1; j < num_functions; j++)
8516 {
8517 unsigned int this_nargs
8518 = check_tgmath_function (&(*cexpr_list)[j], j + 1);
8519 if (this_nargs == 0)
8520 {
8521 expr.set_error ();
8522 goto out;
8523 }
8524 if (this_nargs != nargs)
8525 {
8526 error_at ((*cexpr_list)[j].get_location (),
8527 "argument %u of %<__builtin_tgmath%> has "
8528 "wrong number of arguments", j + 1);
8529 expr.set_error ();
8530 goto out;
8531 }
8532 }
8533
8534 /* The functions all have the same number of arguments.
8535 Determine whether arguments and return types vary in
8536 ways permitted for <tgmath.h> functions. */
8537 /* The first entry in each of these vectors is for the
8538 return type, subsequent entries for parameter
8539 types. */
8540 auto_vec<enum tgmath_parm_kind> parm_kind (nargs + 1);
8541 auto_vec<tree> parm_first (nargs + 1);
8542 auto_vec<bool> parm_complex (nargs + 1);
8543 auto_vec<bool> parm_varies (nargs + 1);
8544 tree first_type = TREE_TYPE (TREE_TYPE ((*cexpr_list)[0].value));
8545 tree first_ret = TYPE_MAIN_VARIANT (TREE_TYPE (first_type));
8546 parm_first.quick_push (first_ret);
8547 parm_complex.quick_push (TREE_CODE (first_ret) == COMPLEX_TYPE);
8548 parm_varies.quick_push (false);
8549 function_args_iterator iter;
8550 tree t;
8551 unsigned int argpos;
8552 FOREACH_FUNCTION_ARGS (first_type, t, iter)
8553 {
8554 if (t == void_type_node)
8555 break;
8556 parm_first.quick_push (TYPE_MAIN_VARIANT (t));
8557 parm_complex.quick_push (TREE_CODE (t) == COMPLEX_TYPE);
8558 parm_varies.quick_push (false);
8559 }
8560 for (unsigned int j = 1; j < num_functions; j++)
8561 {
8562 tree type = TREE_TYPE (TREE_TYPE ((*cexpr_list)[j].value));
8563 tree ret = TYPE_MAIN_VARIANT (TREE_TYPE (type));
8564 if (ret != parm_first[0])
8565 {
8566 parm_varies[0] = true;
8567 if (!SCALAR_FLOAT_TYPE_P (parm_first[0])
8568 && !COMPLEX_FLOAT_TYPE_P (parm_first[0]))
8569 {
8570 error_at ((*cexpr_list)[0].get_location (),
8571 "invalid type-generic return type for "
8572 "argument %u of %<__builtin_tgmath%>",
8573 1);
8574 expr.set_error ();
8575 goto out;
8576 }
8577 if (!SCALAR_FLOAT_TYPE_P (ret)
8578 && !COMPLEX_FLOAT_TYPE_P (ret))
8579 {
8580 error_at ((*cexpr_list)[j].get_location (),
8581 "invalid type-generic return type for "
8582 "argument %u of %<__builtin_tgmath%>",
8583 j + 1);
8584 expr.set_error ();
8585 goto out;
8586 }
8587 }
8588 if (TREE_CODE (ret) == COMPLEX_TYPE)
8589 parm_complex[0] = true;
8590 argpos = 1;
8591 FOREACH_FUNCTION_ARGS (type, t, iter)
8592 {
8593 if (t == void_type_node)
8594 break;
8595 t = TYPE_MAIN_VARIANT (t);
8596 if (t != parm_first[argpos])
8597 {
8598 parm_varies[argpos] = true;
8599 if (!SCALAR_FLOAT_TYPE_P (parm_first[argpos])
8600 && !COMPLEX_FLOAT_TYPE_P (parm_first[argpos]))
8601 {
8602 error_at ((*cexpr_list)[0].get_location (),
8603 "invalid type-generic type for "
8604 "argument %u of argument %u of "
8605 "%<__builtin_tgmath%>", argpos, 1);
8606 expr.set_error ();
8607 goto out;
8608 }
8609 if (!SCALAR_FLOAT_TYPE_P (t)
8610 && !COMPLEX_FLOAT_TYPE_P (t))
8611 {
8612 error_at ((*cexpr_list)[j].get_location (),
8613 "invalid type-generic type for "
8614 "argument %u of argument %u of "
8615 "%<__builtin_tgmath%>", argpos, j + 1);
8616 expr.set_error ();
8617 goto out;
8618 }
8619 }
8620 if (TREE_CODE (t) == COMPLEX_TYPE)
8621 parm_complex[argpos] = true;
8622 argpos++;
8623 }
8624 }
8625 enum tgmath_parm_kind max_variation = tgmath_fixed;
8626 for (unsigned int j = 0; j <= nargs; j++)
8627 {
8628 enum tgmath_parm_kind this_kind;
8629 if (parm_varies[j])
8630 {
8631 if (parm_complex[j])
8632 max_variation = this_kind = tgmath_complex;
8633 else
8634 {
8635 this_kind = tgmath_real;
8636 if (max_variation != tgmath_complex)
8637 max_variation = tgmath_real;
8638 }
8639 }
8640 else
8641 this_kind = tgmath_fixed;
8642 parm_kind.quick_push (this_kind);
8643 }
8644 if (max_variation == tgmath_fixed)
8645 {
8646 error_at (loc, "function arguments of %<__builtin_tgmath%> "
8647 "all have the same type");
8648 expr.set_error ();
8649 break;
8650 }
8651
8652 /* Identify a parameter (not the return type) that varies,
8653 including with complex types if any variation includes
8654 complex types; there must be at least one such
8655 parameter. */
8656 unsigned int tgarg = 0;
8657 for (unsigned int j = 1; j <= nargs; j++)
8658 if (parm_kind[j] == max_variation)
8659 {
8660 tgarg = j;
8661 break;
8662 }
8663 if (tgarg == 0)
8664 {
8665 error_at (loc, "function arguments of %<__builtin_tgmath%> "
8666 "lack type-generic parameter");
8667 expr.set_error ();
8668 break;
8669 }
8670
8671 /* Determine the type of the relevant parameter for each
8672 function. */
8673 auto_vec<tree> tg_type (num_functions);
8674 for (unsigned int j = 0; j < num_functions; j++)
8675 {
8676 tree type = TREE_TYPE (TREE_TYPE ((*cexpr_list)[j].value));
8677 argpos = 1;
8678 FOREACH_FUNCTION_ARGS (type, t, iter)
8679 {
8680 if (argpos == tgarg)
8681 {
8682 tg_type.quick_push (TYPE_MAIN_VARIANT (t));
8683 break;
8684 }
8685 argpos++;
8686 }
8687 }
8688
8689 /* Verify that the corresponding types are different for
8690 all the listed functions. Also determine whether all
8691 the types are complex, whether all the types are
8692 standard or binary, and whether all the types are
8693 decimal. */
8694 bool all_complex = true;
8695 bool all_binary = true;
8696 bool all_decimal = true;
8697 hash_set<tree> tg_types;
8698 FOR_EACH_VEC_ELT (tg_type, i, t)
8699 {
8700 if (TREE_CODE (t) == COMPLEX_TYPE)
8701 all_decimal = false;
8702 else
8703 {
8704 all_complex = false;
8705 if (DECIMAL_FLOAT_TYPE_P (t))
8706 all_binary = false;
8707 else
8708 all_decimal = false;
8709 }
8710 if (tg_types.add (t))
8711 {
8712 error_at ((*cexpr_list)[i].get_location (),
8713 "duplicate type-generic parameter type for "
8714 "function argument %u of %<__builtin_tgmath%>",
8715 i + 1);
8716 expr.set_error ();
8717 goto out;
8718 }
8719 }
8720
8721 /* Verify that other parameters and the return type whose
8722 types vary have their types varying in the correct
8723 way. */
8724 for (unsigned int j = 0; j < num_functions; j++)
8725 {
8726 tree exp_type = tg_type[j];
8727 tree exp_real_type = exp_type;
8728 if (TREE_CODE (exp_type) == COMPLEX_TYPE)
8729 exp_real_type = TREE_TYPE (exp_type);
8730 tree type = TREE_TYPE (TREE_TYPE ((*cexpr_list)[j].value));
8731 tree ret = TYPE_MAIN_VARIANT (TREE_TYPE (type));
8732 if ((parm_kind[0] == tgmath_complex && ret != exp_type)
8733 || (parm_kind[0] == tgmath_real && ret != exp_real_type))
8734 {
8735 error_at ((*cexpr_list)[j].get_location (),
8736 "bad return type for function argument %u "
8737 "of %<__builtin_tgmath%>", j + 1);
8738 expr.set_error ();
8739 goto out;
8740 }
8741 argpos = 1;
8742 FOREACH_FUNCTION_ARGS (type, t, iter)
8743 {
8744 if (t == void_type_node)
8745 break;
8746 t = TYPE_MAIN_VARIANT (t);
8747 if ((parm_kind[argpos] == tgmath_complex
8748 && t != exp_type)
8749 || (parm_kind[argpos] == tgmath_real
8750 && t != exp_real_type))
8751 {
8752 error_at ((*cexpr_list)[j].get_location (),
8753 "bad type for argument %u of "
8754 "function argument %u of "
8755 "%<__builtin_tgmath%>", argpos, j + 1);
8756 expr.set_error ();
8757 goto out;
8758 }
8759 argpos++;
8760 }
8761 }
8762
8763 /* The functions listed are a valid set of functions for a
8764 <tgmath.h> macro to select between. Identify the
8765 matching function, if any. First, the argument types
8766 must be combined following <tgmath.h> rules. Integer
8767 types are treated as _Decimal64 if any type-generic
8768 argument is decimal, or if the only alternatives for
8769 type-generic arguments are of decimal types, and are
8770 otherwise treated as double (or _Complex double for
8771 complex integer types, or _Float64 or _Complex _Float64
8772 if all the return types are the same _FloatN or
8773 _FloatNx type). After that adjustment, types are
8774 combined following the usual arithmetic conversions.
8775 If the function only accepts complex arguments, a
8776 complex type is produced. */
8777 bool arg_complex = all_complex;
8778 bool arg_binary = all_binary;
8779 bool arg_int_decimal = all_decimal;
8780 for (unsigned int j = 1; j <= nargs; j++)
8781 {
8782 if (parm_kind[j] == tgmath_fixed)
8783 continue;
8784 c_expr_t *ce = &(*cexpr_list)[num_functions + j - 1];
8785 tree type = TREE_TYPE (ce->value);
8786 if (!INTEGRAL_TYPE_P (type)
8787 && !SCALAR_FLOAT_TYPE_P (type)
8788 && TREE_CODE (type) != COMPLEX_TYPE)
8789 {
8790 error_at (ce->get_location (),
8791 "invalid type of argument %u of type-generic "
8792 "function", j);
8793 expr.set_error ();
8794 goto out;
8795 }
8796 if (DECIMAL_FLOAT_TYPE_P (type))
8797 {
8798 arg_int_decimal = true;
8799 if (all_complex)
8800 {
8801 error_at (ce->get_location (),
8802 "decimal floating-point argument %u to "
8803 "complex-only type-generic function", j);
8804 expr.set_error ();
8805 goto out;
8806 }
8807 else if (all_binary)
8808 {
8809 error_at (ce->get_location (),
8810 "decimal floating-point argument %u to "
8811 "binary-only type-generic function", j);
8812 expr.set_error ();
8813 goto out;
8814 }
8815 else if (arg_complex)
8816 {
8817 error_at (ce->get_location (),
8818 "both complex and decimal floating-point "
8819 "arguments to type-generic function");
8820 expr.set_error ();
8821 goto out;
8822 }
8823 else if (arg_binary)
8824 {
8825 error_at (ce->get_location (),
8826 "both binary and decimal floating-point "
8827 "arguments to type-generic function");
8828 expr.set_error ();
8829 goto out;
8830 }
8831 }
8832 else if (TREE_CODE (type) == COMPLEX_TYPE)
8833 {
8834 arg_complex = true;
8835 if (COMPLEX_FLOAT_TYPE_P (type))
8836 arg_binary = true;
8837 if (all_decimal)
8838 {
8839 error_at (ce->get_location (),
8840 "complex argument %u to "
8841 "decimal-only type-generic function", j);
8842 expr.set_error ();
8843 goto out;
8844 }
8845 else if (arg_int_decimal)
8846 {
8847 error_at (ce->get_location (),
8848 "both complex and decimal floating-point "
8849 "arguments to type-generic function");
8850 expr.set_error ();
8851 goto out;
8852 }
8853 }
8854 else if (SCALAR_FLOAT_TYPE_P (type))
8855 {
8856 arg_binary = true;
8857 if (all_decimal)
8858 {
8859 error_at (ce->get_location (),
8860 "binary argument %u to "
8861 "decimal-only type-generic function", j);
8862 expr.set_error ();
8863 goto out;
8864 }
8865 else if (arg_int_decimal)
8866 {
8867 error_at (ce->get_location (),
8868 "both binary and decimal floating-point "
8869 "arguments to type-generic function");
8870 expr.set_error ();
8871 goto out;
8872 }
8873 }
8874 }
8875 /* For a macro rounding its result to a narrower type, map
8876 integer types to _Float64 not double if the return type
8877 is a _FloatN or _FloatNx type. */
8878 bool arg_int_float64 = false;
8879 if (parm_kind[0] == tgmath_fixed
8880 && SCALAR_FLOAT_TYPE_P (parm_first[0])
8881 && float64_type_node != NULL_TREE)
8882 for (unsigned int j = 0; j < NUM_FLOATN_NX_TYPES; j++)
8883 if (parm_first[0] == FLOATN_TYPE_NODE (j))
8884 {
8885 arg_int_float64 = true;
8886 break;
8887 }
8888 tree arg_real = NULL_TREE;
8889 for (unsigned int j = 1; j <= nargs; j++)
8890 {
8891 if (parm_kind[j] == tgmath_fixed)
8892 continue;
8893 c_expr_t *ce = &(*cexpr_list)[num_functions + j - 1];
8894 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (ce->value));
8895 if (TREE_CODE (type) == COMPLEX_TYPE)
8896 type = TREE_TYPE (type);
8897 if (INTEGRAL_TYPE_P (type))
8898 type = (arg_int_decimal
8899 ? dfloat64_type_node
8900 : arg_int_float64
8901 ? float64_type_node
8902 : double_type_node);
8903 if (arg_real == NULL_TREE)
8904 arg_real = type;
8905 else
8906 arg_real = common_type (arg_real, type);
8907 if (arg_real == error_mark_node)
8908 {
8909 expr.set_error ();
8910 goto out;
8911 }
8912 }
8913 tree arg_type = (arg_complex
8914 ? build_complex_type (arg_real)
8915 : arg_real);
8916
8917 /* Look for a function to call with type-generic parameter
8918 type ARG_TYPE. */
8919 c_expr_t *fn = NULL;
8920 for (unsigned int j = 0; j < num_functions; j++)
8921 {
8922 if (tg_type[j] == arg_type)
8923 {
8924 fn = &(*cexpr_list)[j];
8925 break;
8926 }
8927 }
8928 if (fn == NULL
8929 && parm_kind[0] == tgmath_fixed
8930 && SCALAR_FLOAT_TYPE_P (parm_first[0]))
8931 {
8932 /* Presume this is a macro that rounds its result to a
8933 narrower type, and look for the first function with
8934 at least the range and precision of the argument
8935 type. */
8936 for (unsigned int j = 0; j < num_functions; j++)
8937 {
8938 if (arg_complex
8939 != (TREE_CODE (tg_type[j]) == COMPLEX_TYPE))
8940 continue;
8941 tree real_tg_type = (arg_complex
8942 ? TREE_TYPE (tg_type[j])
8943 : tg_type[j]);
8944 if (DECIMAL_FLOAT_TYPE_P (arg_real)
8945 != DECIMAL_FLOAT_TYPE_P (real_tg_type))
8946 continue;
8947 scalar_float_mode arg_mode
8948 = SCALAR_FLOAT_TYPE_MODE (arg_real);
8949 scalar_float_mode tg_mode
8950 = SCALAR_FLOAT_TYPE_MODE (real_tg_type);
8951 const real_format *arg_fmt = REAL_MODE_FORMAT (arg_mode);
8952 const real_format *tg_fmt = REAL_MODE_FORMAT (tg_mode);
8953 if (arg_fmt->b == tg_fmt->b
8954 && arg_fmt->p <= tg_fmt->p
8955 && arg_fmt->emax <= tg_fmt->emax
8956 && (arg_fmt->emin - arg_fmt->p
8957 >= tg_fmt->emin - tg_fmt->p))
8958 {
8959 fn = &(*cexpr_list)[j];
8960 break;
8961 }
8962 }
8963 }
8964 if (fn == NULL)
8965 {
8966 error_at (loc, "no matching function for type-generic call");
8967 expr.set_error ();
8968 break;
8969 }
8970
8971 /* Construct a call to FN. */
8972 vec<tree, va_gc> *args;
8973 vec_alloc (args, nargs);
8974 vec<tree, va_gc> *origtypes;
8975 vec_alloc (origtypes, nargs);
8976 auto_vec<location_t> arg_loc (nargs);
8977 for (unsigned int j = 0; j < nargs; j++)
8978 {
8979 c_expr_t *ce = &(*cexpr_list)[num_functions + j];
8980 args->quick_push (ce->value);
8981 arg_loc.quick_push (ce->get_location ());
8982 origtypes->quick_push (ce->original_type);
8983 }
8984 expr.value = c_build_function_call_vec (loc, arg_loc, fn->value,
8985 args, origtypes);
8986 set_c_expr_source_range (&expr, loc, close_paren_loc);
8987 break;
8988 }
8989 case RID_BUILTIN_CALL_WITH_STATIC_CHAIN:
8990 {
8991 vec<c_expr_t, va_gc> *cexpr_list;
8992 c_expr_t *e2_p;
8993 tree chain_value;
8994 location_t close_paren_loc;
8995
8996 c_parser_consume_token (parser);
8997 if (!c_parser_get_builtin_args (parser,
8998 "__builtin_call_with_static_chain",
8999 &cexpr_list, false,
9000 &close_paren_loc))
9001 {
9002 expr.set_error ();
9003 break;
9004 }
9005 if (vec_safe_length (cexpr_list) != 2)
9006 {
9007 error_at (loc, "wrong number of arguments to "
9008 "%<__builtin_call_with_static_chain%>");
9009 expr.set_error ();
9010 break;
9011 }
9012
9013 expr = (*cexpr_list)[0];
9014 e2_p = &(*cexpr_list)[1];
9015 *e2_p = convert_lvalue_to_rvalue (loc, *e2_p, true, true);
9016 chain_value = e2_p->value;
9017 mark_exp_read (chain_value);
9018
9019 if (TREE_CODE (expr.value) != CALL_EXPR)
9020 error_at (loc, "first argument to "
9021 "%<__builtin_call_with_static_chain%> "
9022 "must be a call expression");
9023 else if (TREE_CODE (TREE_TYPE (chain_value)) != POINTER_TYPE)
9024 error_at (loc, "second argument to "
9025 "%<__builtin_call_with_static_chain%> "
9026 "must be a pointer type");
9027 else
9028 CALL_EXPR_STATIC_CHAIN (expr.value) = chain_value;
9029 set_c_expr_source_range (&expr, loc, close_paren_loc);
9030 break;
9031 }
9032 case RID_BUILTIN_COMPLEX:
9033 {
9034 vec<c_expr_t, va_gc> *cexpr_list;
9035 c_expr_t *e1_p, *e2_p;
9036 location_t close_paren_loc;
9037
9038 c_parser_consume_token (parser);
9039 if (!c_parser_get_builtin_args (parser,
9040 "__builtin_complex",
9041 &cexpr_list, false,
9042 &close_paren_loc))
9043 {
9044 expr.set_error ();
9045 break;
9046 }
9047
9048 if (vec_safe_length (cexpr_list) != 2)
9049 {
9050 error_at (loc, "wrong number of arguments to "
9051 "%<__builtin_complex%>");
9052 expr.set_error ();
9053 break;
9054 }
9055
9056 e1_p = &(*cexpr_list)[0];
9057 e2_p = &(*cexpr_list)[1];
9058
9059 *e1_p = convert_lvalue_to_rvalue (loc, *e1_p, true, true);
9060 if (TREE_CODE (e1_p->value) == EXCESS_PRECISION_EXPR)
9061 e1_p->value = convert (TREE_TYPE (e1_p->value),
9062 TREE_OPERAND (e1_p->value, 0));
9063 *e2_p = convert_lvalue_to_rvalue (loc, *e2_p, true, true);
9064 if (TREE_CODE (e2_p->value) == EXCESS_PRECISION_EXPR)
9065 e2_p->value = convert (TREE_TYPE (e2_p->value),
9066 TREE_OPERAND (e2_p->value, 0));
9067 if (!SCALAR_FLOAT_TYPE_P (TREE_TYPE (e1_p->value))
9068 || DECIMAL_FLOAT_TYPE_P (TREE_TYPE (e1_p->value))
9069 || !SCALAR_FLOAT_TYPE_P (TREE_TYPE (e2_p->value))
9070 || DECIMAL_FLOAT_TYPE_P (TREE_TYPE (e2_p->value)))
9071 {
9072 error_at (loc, "%<__builtin_complex%> operand "
9073 "not of real binary floating-point type");
9074 expr.set_error ();
9075 break;
9076 }
9077 if (TYPE_MAIN_VARIANT (TREE_TYPE (e1_p->value))
9078 != TYPE_MAIN_VARIANT (TREE_TYPE (e2_p->value)))
9079 {
9080 error_at (loc,
9081 "%<__builtin_complex%> operands of different types");
9082 expr.set_error ();
9083 break;
9084 }
9085 pedwarn_c90 (loc, OPT_Wpedantic,
9086 "ISO C90 does not support complex types");
9087 expr.value = build2_loc (loc, COMPLEX_EXPR,
9088 build_complex_type
9089 (TYPE_MAIN_VARIANT
9090 (TREE_TYPE (e1_p->value))),
9091 e1_p->value, e2_p->value);
9092 set_c_expr_source_range (&expr, loc, close_paren_loc);
9093 break;
9094 }
9095 case RID_BUILTIN_SHUFFLE:
9096 {
9097 vec<c_expr_t, va_gc> *cexpr_list;
9098 unsigned int i;
9099 c_expr_t *p;
9100 location_t close_paren_loc;
9101
9102 c_parser_consume_token (parser);
9103 if (!c_parser_get_builtin_args (parser,
9104 "__builtin_shuffle",
9105 &cexpr_list, false,
9106 &close_paren_loc))
9107 {
9108 expr.set_error ();
9109 break;
9110 }
9111
9112 FOR_EACH_VEC_SAFE_ELT (cexpr_list, i, p)
9113 *p = convert_lvalue_to_rvalue (loc, *p, true, true);
9114
9115 if (vec_safe_length (cexpr_list) == 2)
9116 expr.value =
9117 c_build_vec_perm_expr
9118 (loc, (*cexpr_list)[0].value,
9119 NULL_TREE, (*cexpr_list)[1].value);
9120
9121 else if (vec_safe_length (cexpr_list) == 3)
9122 expr.value =
9123 c_build_vec_perm_expr
9124 (loc, (*cexpr_list)[0].value,
9125 (*cexpr_list)[1].value,
9126 (*cexpr_list)[2].value);
9127 else
9128 {
9129 error_at (loc, "wrong number of arguments to "
9130 "%<__builtin_shuffle%>");
9131 expr.set_error ();
9132 }
9133 set_c_expr_source_range (&expr, loc, close_paren_loc);
9134 break;
9135 }
9136 case RID_AT_SELECTOR:
9137 {
9138 gcc_assert (c_dialect_objc ());
9139 c_parser_consume_token (parser);
9140 matching_parens parens;
9141 if (!parens.require_open (parser))
9142 {
9143 expr.set_error ();
9144 break;
9145 }
9146 tree sel = c_parser_objc_selector_arg (parser);
9147 location_t close_loc = c_parser_peek_token (parser)->location;
9148 parens.skip_until_found_close (parser);
9149 expr.value = objc_build_selector_expr (loc, sel);
9150 set_c_expr_source_range (&expr, loc, close_loc);
9151 }
9152 break;
9153 case RID_AT_PROTOCOL:
9154 {
9155 gcc_assert (c_dialect_objc ());
9156 c_parser_consume_token (parser);
9157 matching_parens parens;
9158 if (!parens.require_open (parser))
9159 {
9160 expr.set_error ();
9161 break;
9162 }
9163 if (c_parser_next_token_is_not (parser, CPP_NAME))
9164 {
9165 c_parser_error (parser, "expected identifier");
9166 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
9167 expr.set_error ();
9168 break;
9169 }
9170 tree id = c_parser_peek_token (parser)->value;
9171 c_parser_consume_token (parser);
9172 location_t close_loc = c_parser_peek_token (parser)->location;
9173 parens.skip_until_found_close (parser);
9174 expr.value = objc_build_protocol_expr (id);
9175 set_c_expr_source_range (&expr, loc, close_loc);
9176 }
9177 break;
9178 case RID_AT_ENCODE:
9179 {
9180 /* Extension to support C-structures in the archiver. */
9181 gcc_assert (c_dialect_objc ());
9182 c_parser_consume_token (parser);
9183 matching_parens parens;
9184 if (!parens.require_open (parser))
9185 {
9186 expr.set_error ();
9187 break;
9188 }
9189 t1 = c_parser_type_name (parser);
9190 if (t1 == NULL)
9191 {
9192 expr.set_error ();
9193 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
9194 break;
9195 }
9196 location_t close_loc = c_parser_peek_token (parser)->location;
9197 parens.skip_until_found_close (parser);
9198 tree type = groktypename (t1, NULL, NULL);
9199 expr.value = objc_build_encode_expr (type);
9200 set_c_expr_source_range (&expr, loc, close_loc);
9201 }
9202 break;
9203 case RID_GENERIC:
9204 expr = c_parser_generic_selection (parser);
9205 break;
9206 default:
9207 c_parser_error (parser, "expected expression");
9208 expr.set_error ();
9209 break;
9210 }
9211 break;
9212 case CPP_OPEN_SQUARE:
9213 if (c_dialect_objc ())
9214 {
9215 tree receiver, args;
9216 c_parser_consume_token (parser);
9217 receiver = c_parser_objc_receiver (parser);
9218 args = c_parser_objc_message_args (parser);
9219 location_t close_loc = c_parser_peek_token (parser)->location;
9220 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
9221 "expected %<]%>");
9222 expr.value = objc_build_message_expr (receiver, args);
9223 set_c_expr_source_range (&expr, loc, close_loc);
9224 break;
9225 }
9226 /* Else fall through to report error. */
9227 /* FALLTHRU */
9228 default:
9229 c_parser_error (parser, "expected expression");
9230 expr.set_error ();
9231 break;
9232 }
9233 out:
9234 return c_parser_postfix_expression_after_primary
9235 (parser, EXPR_LOC_OR_LOC (expr.value, loc), expr);
9236 }
9237
9238 /* Parse a postfix expression after a parenthesized type name: the
9239 brace-enclosed initializer of a compound literal, possibly followed
9240 by some postfix operators. This is separate because it is not
9241 possible to tell until after the type name whether a cast
9242 expression has a cast or a compound literal, or whether the operand
9243 of sizeof is a parenthesized type name or starts with a compound
9244 literal. TYPE_LOC is the location where TYPE_NAME starts--the
9245 location of the first token after the parentheses around the type
9246 name. */
9247
9248 static struct c_expr
9249 c_parser_postfix_expression_after_paren_type (c_parser *parser,
9250 struct c_type_name *type_name,
9251 location_t type_loc)
9252 {
9253 tree type;
9254 struct c_expr init;
9255 bool non_const;
9256 struct c_expr expr;
9257 location_t start_loc;
9258 tree type_expr = NULL_TREE;
9259 bool type_expr_const = true;
9260 check_compound_literal_type (type_loc, type_name);
9261 rich_location richloc (line_table, type_loc);
9262 start_init (NULL_TREE, NULL, 0, &richloc);
9263 type = groktypename (type_name, &type_expr, &type_expr_const);
9264 start_loc = c_parser_peek_token (parser)->location;
9265 if (type != error_mark_node && C_TYPE_VARIABLE_SIZE (type))
9266 {
9267 error_at (type_loc, "compound literal has variable size");
9268 type = error_mark_node;
9269 }
9270 init = c_parser_braced_init (parser, type, false, NULL);
9271 finish_init ();
9272 maybe_warn_string_init (type_loc, type, init);
9273
9274 if (type != error_mark_node
9275 && !ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (type))
9276 && current_function_decl)
9277 {
9278 error ("compound literal qualified by address-space qualifier");
9279 type = error_mark_node;
9280 }
9281
9282 pedwarn_c90 (start_loc, OPT_Wpedantic, "ISO C90 forbids compound literals");
9283 non_const = ((init.value && TREE_CODE (init.value) == CONSTRUCTOR)
9284 ? CONSTRUCTOR_NON_CONST (init.value)
9285 : init.original_code == C_MAYBE_CONST_EXPR);
9286 non_const |= !type_expr_const;
9287 unsigned int alignas_align = 0;
9288 if (type != error_mark_node
9289 && type_name->specs->align_log != -1)
9290 {
9291 alignas_align = 1U << type_name->specs->align_log;
9292 if (alignas_align < min_align_of_type (type))
9293 {
9294 error_at (type_name->specs->locations[cdw_alignas],
9295 "%<_Alignas%> specifiers cannot reduce "
9296 "alignment of compound literal");
9297 alignas_align = 0;
9298 }
9299 }
9300 expr.value = build_compound_literal (start_loc, type, init.value, non_const,
9301 alignas_align);
9302 set_c_expr_source_range (&expr, init.src_range);
9303 expr.original_code = ERROR_MARK;
9304 expr.original_type = NULL;
9305 if (type != error_mark_node
9306 && expr.value != error_mark_node
9307 && type_expr)
9308 {
9309 if (TREE_CODE (expr.value) == C_MAYBE_CONST_EXPR)
9310 {
9311 gcc_assert (C_MAYBE_CONST_EXPR_PRE (expr.value) == NULL_TREE);
9312 C_MAYBE_CONST_EXPR_PRE (expr.value) = type_expr;
9313 }
9314 else
9315 {
9316 gcc_assert (!non_const);
9317 expr.value = build2 (C_MAYBE_CONST_EXPR, type,
9318 type_expr, expr.value);
9319 }
9320 }
9321 return c_parser_postfix_expression_after_primary (parser, start_loc, expr);
9322 }
9323
9324 /* Callback function for sizeof_pointer_memaccess_warning to compare
9325 types. */
9326
9327 static bool
9328 sizeof_ptr_memacc_comptypes (tree type1, tree type2)
9329 {
9330 return comptypes (type1, type2) == 1;
9331 }
9332
9333 /* Warn for patterns where abs-like function appears to be used incorrectly,
9334 gracefully ignore any non-abs-like function. The warning location should
9335 be LOC. FNDECL is the declaration of called function, it must be a
9336 BUILT_IN_NORMAL function. ARG is the first and only argument of the
9337 call. */
9338
9339 static void
9340 warn_for_abs (location_t loc, tree fndecl, tree arg)
9341 {
9342 tree atype = TREE_TYPE (arg);
9343
9344 /* Casts from pointers (and thus arrays and fndecls) will generate
9345 -Wint-conversion warnings. Most other wrong types hopefully lead to type
9346 mismatch errors. TODO: Think about what to do with FIXED_POINT_TYPE_P
9347 types and possibly other exotic types. */
9348 if (!INTEGRAL_TYPE_P (atype)
9349 && !SCALAR_FLOAT_TYPE_P (atype)
9350 && TREE_CODE (atype) != COMPLEX_TYPE)
9351 return;
9352
9353 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
9354
9355 switch (fcode)
9356 {
9357 case BUILT_IN_ABS:
9358 case BUILT_IN_LABS:
9359 case BUILT_IN_LLABS:
9360 case BUILT_IN_IMAXABS:
9361 if (!INTEGRAL_TYPE_P (atype))
9362 {
9363 if (SCALAR_FLOAT_TYPE_P (atype))
9364 warning_at (loc, OPT_Wabsolute_value,
9365 "using integer absolute value function %qD when "
9366 "argument is of floating point type %qT",
9367 fndecl, atype);
9368 else if (TREE_CODE (atype) == COMPLEX_TYPE)
9369 warning_at (loc, OPT_Wabsolute_value,
9370 "using integer absolute value function %qD when "
9371 "argument is of complex type %qT", fndecl, atype);
9372 else
9373 gcc_unreachable ();
9374 return;
9375 }
9376 if (TYPE_UNSIGNED (atype))
9377 warning_at (loc, OPT_Wabsolute_value,
9378 "taking the absolute value of unsigned type %qT "
9379 "has no effect", atype);
9380 break;
9381
9382 CASE_FLT_FN (BUILT_IN_FABS):
9383 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FABS):
9384 if (!SCALAR_FLOAT_TYPE_P (atype)
9385 || DECIMAL_FLOAT_MODE_P (TYPE_MODE (atype)))
9386 {
9387 if (INTEGRAL_TYPE_P (atype))
9388 warning_at (loc, OPT_Wabsolute_value,
9389 "using floating point absolute value function %qD "
9390 "when argument is of integer type %qT", fndecl, atype);
9391 else if (DECIMAL_FLOAT_TYPE_P (atype))
9392 warning_at (loc, OPT_Wabsolute_value,
9393 "using floating point absolute value function %qD "
9394 "when argument is of decimal floating point type %qT",
9395 fndecl, atype);
9396 else if (TREE_CODE (atype) == COMPLEX_TYPE)
9397 warning_at (loc, OPT_Wabsolute_value,
9398 "using floating point absolute value function %qD when "
9399 "argument is of complex type %qT", fndecl, atype);
9400 else
9401 gcc_unreachable ();
9402 return;
9403 }
9404 break;
9405
9406 CASE_FLT_FN (BUILT_IN_CABS):
9407 if (TREE_CODE (atype) != COMPLEX_TYPE)
9408 {
9409 if (INTEGRAL_TYPE_P (atype))
9410 warning_at (loc, OPT_Wabsolute_value,
9411 "using complex absolute value function %qD when "
9412 "argument is of integer type %qT", fndecl, atype);
9413 else if (SCALAR_FLOAT_TYPE_P (atype))
9414 warning_at (loc, OPT_Wabsolute_value,
9415 "using complex absolute value function %qD when "
9416 "argument is of floating point type %qT",
9417 fndecl, atype);
9418 else
9419 gcc_unreachable ();
9420
9421 return;
9422 }
9423 break;
9424
9425 case BUILT_IN_FABSD32:
9426 case BUILT_IN_FABSD64:
9427 case BUILT_IN_FABSD128:
9428 if (!DECIMAL_FLOAT_TYPE_P (atype))
9429 {
9430 if (INTEGRAL_TYPE_P (atype))
9431 warning_at (loc, OPT_Wabsolute_value,
9432 "using decimal floating point absolute value "
9433 "function %qD when argument is of integer type %qT",
9434 fndecl, atype);
9435 else if (SCALAR_FLOAT_TYPE_P (atype))
9436 warning_at (loc, OPT_Wabsolute_value,
9437 "using decimal floating point absolute value "
9438 "function %qD when argument is of floating point "
9439 "type %qT", fndecl, atype);
9440 else if (TREE_CODE (atype) == COMPLEX_TYPE)
9441 warning_at (loc, OPT_Wabsolute_value,
9442 "using decimal floating point absolute value "
9443 "function %qD when argument is of complex type %qT",
9444 fndecl, atype);
9445 else
9446 gcc_unreachable ();
9447 return;
9448 }
9449 break;
9450
9451 default:
9452 return;
9453 }
9454
9455 if (!TYPE_ARG_TYPES (TREE_TYPE (fndecl)))
9456 return;
9457
9458 tree ftype = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
9459 if (TREE_CODE (atype) == COMPLEX_TYPE)
9460 {
9461 gcc_assert (TREE_CODE (ftype) == COMPLEX_TYPE);
9462 atype = TREE_TYPE (atype);
9463 ftype = TREE_TYPE (ftype);
9464 }
9465
9466 if (TYPE_PRECISION (ftype) < TYPE_PRECISION (atype))
9467 warning_at (loc, OPT_Wabsolute_value,
9468 "absolute value function %qD given an argument of type %qT "
9469 "but has parameter of type %qT which may cause truncation "
9470 "of value", fndecl, atype, ftype);
9471 }
9472
9473
9474 /* Parse a postfix expression after the initial primary or compound
9475 literal; that is, parse a series of postfix operators.
9476
9477 EXPR_LOC is the location of the primary expression. */
9478
9479 static struct c_expr
9480 c_parser_postfix_expression_after_primary (c_parser *parser,
9481 location_t expr_loc,
9482 struct c_expr expr)
9483 {
9484 struct c_expr orig_expr;
9485 tree ident, idx;
9486 location_t sizeof_arg_loc[3], comp_loc;
9487 tree sizeof_arg[3];
9488 unsigned int literal_zero_mask;
9489 unsigned int i;
9490 vec<tree, va_gc> *exprlist;
9491 vec<tree, va_gc> *origtypes = NULL;
9492 vec<location_t> arg_loc = vNULL;
9493 location_t start;
9494 location_t finish;
9495
9496 while (true)
9497 {
9498 location_t op_loc = c_parser_peek_token (parser)->location;
9499 switch (c_parser_peek_token (parser)->type)
9500 {
9501 case CPP_OPEN_SQUARE:
9502 /* Array reference. */
9503 c_parser_consume_token (parser);
9504 idx = c_parser_expression (parser).value;
9505 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
9506 "expected %<]%>");
9507 start = expr.get_start ();
9508 finish = parser->tokens_buf[0].location;
9509 expr.value = build_array_ref (op_loc, expr.value, idx);
9510 set_c_expr_source_range (&expr, start, finish);
9511 expr.original_code = ERROR_MARK;
9512 expr.original_type = NULL;
9513 break;
9514 case CPP_OPEN_PAREN:
9515 /* Function call. */
9516 c_parser_consume_token (parser);
9517 for (i = 0; i < 3; i++)
9518 {
9519 sizeof_arg[i] = NULL_TREE;
9520 sizeof_arg_loc[i] = UNKNOWN_LOCATION;
9521 }
9522 literal_zero_mask = 0;
9523 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
9524 exprlist = NULL;
9525 else
9526 exprlist = c_parser_expr_list (parser, true, false, &origtypes,
9527 sizeof_arg_loc, sizeof_arg,
9528 &arg_loc, &literal_zero_mask);
9529 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
9530 "expected %<)%>");
9531 orig_expr = expr;
9532 mark_exp_read (expr.value);
9533 if (warn_sizeof_pointer_memaccess)
9534 sizeof_pointer_memaccess_warning (sizeof_arg_loc,
9535 expr.value, exprlist,
9536 sizeof_arg,
9537 sizeof_ptr_memacc_comptypes);
9538 if (TREE_CODE (expr.value) == FUNCTION_DECL)
9539 {
9540 if (fndecl_built_in_p (expr.value, BUILT_IN_MEMSET)
9541 && vec_safe_length (exprlist) == 3)
9542 {
9543 tree arg0 = (*exprlist)[0];
9544 tree arg2 = (*exprlist)[2];
9545 warn_for_memset (expr_loc, arg0, arg2, literal_zero_mask);
9546 }
9547 if (warn_absolute_value
9548 && fndecl_built_in_p (expr.value, BUILT_IN_NORMAL)
9549 && vec_safe_length (exprlist) == 1)
9550 warn_for_abs (expr_loc, expr.value, (*exprlist)[0]);
9551 }
9552
9553 start = expr.get_start ();
9554 finish = parser->tokens_buf[0].get_finish ();
9555 expr.value
9556 = c_build_function_call_vec (expr_loc, arg_loc, expr.value,
9557 exprlist, origtypes);
9558 set_c_expr_source_range (&expr, start, finish);
9559
9560 expr.original_code = ERROR_MARK;
9561 if (TREE_CODE (expr.value) == INTEGER_CST
9562 && TREE_CODE (orig_expr.value) == FUNCTION_DECL
9563 && fndecl_built_in_p (orig_expr.value, BUILT_IN_CONSTANT_P))
9564 expr.original_code = C_MAYBE_CONST_EXPR;
9565 expr.original_type = NULL;
9566 if (exprlist)
9567 {
9568 release_tree_vector (exprlist);
9569 release_tree_vector (origtypes);
9570 }
9571 arg_loc.release ();
9572 break;
9573 case CPP_DOT:
9574 /* Structure element reference. */
9575 c_parser_consume_token (parser);
9576 expr = default_function_array_conversion (expr_loc, expr);
9577 if (c_parser_next_token_is (parser, CPP_NAME))
9578 {
9579 c_token *comp_tok = c_parser_peek_token (parser);
9580 ident = comp_tok->value;
9581 comp_loc = comp_tok->location;
9582 }
9583 else
9584 {
9585 c_parser_error (parser, "expected identifier");
9586 expr.set_error ();
9587 expr.original_code = ERROR_MARK;
9588 expr.original_type = NULL;
9589 return expr;
9590 }
9591 start = expr.get_start ();
9592 finish = c_parser_peek_token (parser)->get_finish ();
9593 c_parser_consume_token (parser);
9594 expr.value = build_component_ref (op_loc, expr.value, ident,
9595 comp_loc);
9596 set_c_expr_source_range (&expr, start, finish);
9597 expr.original_code = ERROR_MARK;
9598 if (TREE_CODE (expr.value) != COMPONENT_REF)
9599 expr.original_type = NULL;
9600 else
9601 {
9602 /* Remember the original type of a bitfield. */
9603 tree field = TREE_OPERAND (expr.value, 1);
9604 if (TREE_CODE (field) != FIELD_DECL)
9605 expr.original_type = NULL;
9606 else
9607 expr.original_type = DECL_BIT_FIELD_TYPE (field);
9608 }
9609 break;
9610 case CPP_DEREF:
9611 /* Structure element reference. */
9612 c_parser_consume_token (parser);
9613 expr = convert_lvalue_to_rvalue (expr_loc, expr, true, false);
9614 if (c_parser_next_token_is (parser, CPP_NAME))
9615 {
9616 c_token *comp_tok = c_parser_peek_token (parser);
9617 ident = comp_tok->value;
9618 comp_loc = comp_tok->location;
9619 }
9620 else
9621 {
9622 c_parser_error (parser, "expected identifier");
9623 expr.set_error ();
9624 expr.original_code = ERROR_MARK;
9625 expr.original_type = NULL;
9626 return expr;
9627 }
9628 start = expr.get_start ();
9629 finish = c_parser_peek_token (parser)->get_finish ();
9630 c_parser_consume_token (parser);
9631 expr.value = build_component_ref (op_loc,
9632 build_indirect_ref (op_loc,
9633 expr.value,
9634 RO_ARROW),
9635 ident, comp_loc);
9636 set_c_expr_source_range (&expr, start, finish);
9637 expr.original_code = ERROR_MARK;
9638 if (TREE_CODE (expr.value) != COMPONENT_REF)
9639 expr.original_type = NULL;
9640 else
9641 {
9642 /* Remember the original type of a bitfield. */
9643 tree field = TREE_OPERAND (expr.value, 1);
9644 if (TREE_CODE (field) != FIELD_DECL)
9645 expr.original_type = NULL;
9646 else
9647 expr.original_type = DECL_BIT_FIELD_TYPE (field);
9648 }
9649 break;
9650 case CPP_PLUS_PLUS:
9651 /* Postincrement. */
9652 start = expr.get_start ();
9653 finish = c_parser_peek_token (parser)->get_finish ();
9654 c_parser_consume_token (parser);
9655 expr = default_function_array_read_conversion (expr_loc, expr);
9656 expr.value = build_unary_op (op_loc, POSTINCREMENT_EXPR,
9657 expr.value, false);
9658 set_c_expr_source_range (&expr, start, finish);
9659 expr.original_code = ERROR_MARK;
9660 expr.original_type = NULL;
9661 break;
9662 case CPP_MINUS_MINUS:
9663 /* Postdecrement. */
9664 start = expr.get_start ();
9665 finish = c_parser_peek_token (parser)->get_finish ();
9666 c_parser_consume_token (parser);
9667 expr = default_function_array_read_conversion (expr_loc, expr);
9668 expr.value = build_unary_op (op_loc, POSTDECREMENT_EXPR,
9669 expr.value, false);
9670 set_c_expr_source_range (&expr, start, finish);
9671 expr.original_code = ERROR_MARK;
9672 expr.original_type = NULL;
9673 break;
9674 default:
9675 return expr;
9676 }
9677 }
9678 }
9679
9680 /* Parse an expression (C90 6.3.17, C99 6.5.17, C11 6.5.17).
9681
9682 expression:
9683 assignment-expression
9684 expression , assignment-expression
9685 */
9686
9687 static struct c_expr
9688 c_parser_expression (c_parser *parser)
9689 {
9690 location_t tloc = c_parser_peek_token (parser)->location;
9691 struct c_expr expr;
9692 expr = c_parser_expr_no_commas (parser, NULL);
9693 if (c_parser_next_token_is (parser, CPP_COMMA))
9694 expr = convert_lvalue_to_rvalue (tloc, expr, true, false);
9695 while (c_parser_next_token_is (parser, CPP_COMMA))
9696 {
9697 struct c_expr next;
9698 tree lhsval;
9699 location_t loc = c_parser_peek_token (parser)->location;
9700 location_t expr_loc;
9701 c_parser_consume_token (parser);
9702 expr_loc = c_parser_peek_token (parser)->location;
9703 lhsval = expr.value;
9704 while (TREE_CODE (lhsval) == COMPOUND_EXPR)
9705 lhsval = TREE_OPERAND (lhsval, 1);
9706 if (DECL_P (lhsval) || handled_component_p (lhsval))
9707 mark_exp_read (lhsval);
9708 next = c_parser_expr_no_commas (parser, NULL);
9709 next = convert_lvalue_to_rvalue (expr_loc, next, true, false);
9710 expr.value = build_compound_expr (loc, expr.value, next.value);
9711 expr.original_code = COMPOUND_EXPR;
9712 expr.original_type = next.original_type;
9713 }
9714 return expr;
9715 }
9716
9717 /* Parse an expression and convert functions or arrays to pointers and
9718 lvalues to rvalues. */
9719
9720 static struct c_expr
9721 c_parser_expression_conv (c_parser *parser)
9722 {
9723 struct c_expr expr;
9724 location_t loc = c_parser_peek_token (parser)->location;
9725 expr = c_parser_expression (parser);
9726 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
9727 return expr;
9728 }
9729
9730 /* Helper function of c_parser_expr_list. Check if IDXth (0 based)
9731 argument is a literal zero alone and if so, set it in literal_zero_mask. */
9732
9733 static inline void
9734 c_parser_check_literal_zero (c_parser *parser, unsigned *literal_zero_mask,
9735 unsigned int idx)
9736 {
9737 if (idx >= HOST_BITS_PER_INT)
9738 return;
9739
9740 c_token *tok = c_parser_peek_token (parser);
9741 switch (tok->type)
9742 {
9743 case CPP_NUMBER:
9744 case CPP_CHAR:
9745 case CPP_WCHAR:
9746 case CPP_CHAR16:
9747 case CPP_CHAR32:
9748 /* If a parameter is literal zero alone, remember it
9749 for -Wmemset-transposed-args warning. */
9750 if (integer_zerop (tok->value)
9751 && !TREE_OVERFLOW (tok->value)
9752 && (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
9753 || c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_PAREN))
9754 *literal_zero_mask |= 1U << idx;
9755 default:
9756 break;
9757 }
9758 }
9759
9760 /* Parse a non-empty list of expressions. If CONVERT_P, convert
9761 functions and arrays to pointers and lvalues to rvalues. If
9762 FOLD_P, fold the expressions. If LOCATIONS is non-NULL, save the
9763 locations of function arguments into this vector.
9764
9765 nonempty-expr-list:
9766 assignment-expression
9767 nonempty-expr-list , assignment-expression
9768 */
9769
9770 static vec<tree, va_gc> *
9771 c_parser_expr_list (c_parser *parser, bool convert_p, bool fold_p,
9772 vec<tree, va_gc> **p_orig_types,
9773 location_t *sizeof_arg_loc, tree *sizeof_arg,
9774 vec<location_t> *locations,
9775 unsigned int *literal_zero_mask)
9776 {
9777 vec<tree, va_gc> *ret;
9778 vec<tree, va_gc> *orig_types;
9779 struct c_expr expr;
9780 unsigned int idx = 0;
9781
9782 ret = make_tree_vector ();
9783 if (p_orig_types == NULL)
9784 orig_types = NULL;
9785 else
9786 orig_types = make_tree_vector ();
9787
9788 if (literal_zero_mask)
9789 c_parser_check_literal_zero (parser, literal_zero_mask, 0);
9790 expr = c_parser_expr_no_commas (parser, NULL);
9791 if (convert_p)
9792 expr = convert_lvalue_to_rvalue (expr.get_location (), expr, true, true);
9793 if (fold_p)
9794 expr.value = c_fully_fold (expr.value, false, NULL);
9795 ret->quick_push (expr.value);
9796 if (orig_types)
9797 orig_types->quick_push (expr.original_type);
9798 if (locations)
9799 locations->safe_push (expr.get_location ());
9800 if (sizeof_arg != NULL
9801 && expr.original_code == SIZEOF_EXPR)
9802 {
9803 sizeof_arg[0] = c_last_sizeof_arg;
9804 sizeof_arg_loc[0] = c_last_sizeof_loc;
9805 }
9806 while (c_parser_next_token_is (parser, CPP_COMMA))
9807 {
9808 c_parser_consume_token (parser);
9809 if (literal_zero_mask)
9810 c_parser_check_literal_zero (parser, literal_zero_mask, idx + 1);
9811 expr = c_parser_expr_no_commas (parser, NULL);
9812 if (convert_p)
9813 expr = convert_lvalue_to_rvalue (expr.get_location (), expr, true,
9814 true);
9815 if (fold_p)
9816 expr.value = c_fully_fold (expr.value, false, NULL);
9817 vec_safe_push (ret, expr.value);
9818 if (orig_types)
9819 vec_safe_push (orig_types, expr.original_type);
9820 if (locations)
9821 locations->safe_push (expr.get_location ());
9822 if (++idx < 3
9823 && sizeof_arg != NULL
9824 && expr.original_code == SIZEOF_EXPR)
9825 {
9826 sizeof_arg[idx] = c_last_sizeof_arg;
9827 sizeof_arg_loc[idx] = c_last_sizeof_loc;
9828 }
9829 }
9830 if (orig_types)
9831 *p_orig_types = orig_types;
9832 return ret;
9833 }
9834 \f
9835 /* Parse Objective-C-specific constructs. */
9836
9837 /* Parse an objc-class-definition.
9838
9839 objc-class-definition:
9840 @interface identifier objc-superclass[opt] objc-protocol-refs[opt]
9841 objc-class-instance-variables[opt] objc-methodprotolist @end
9842 @implementation identifier objc-superclass[opt]
9843 objc-class-instance-variables[opt]
9844 @interface identifier ( identifier ) objc-protocol-refs[opt]
9845 objc-methodprotolist @end
9846 @interface identifier ( ) objc-protocol-refs[opt]
9847 objc-methodprotolist @end
9848 @implementation identifier ( identifier )
9849
9850 objc-superclass:
9851 : identifier
9852
9853 "@interface identifier (" must start "@interface identifier (
9854 identifier ) ...": objc-methodprotolist in the first production may
9855 not start with a parenthesized identifier as a declarator of a data
9856 definition with no declaration specifiers if the objc-superclass,
9857 objc-protocol-refs and objc-class-instance-variables are omitted. */
9858
9859 static void
9860 c_parser_objc_class_definition (c_parser *parser, tree attributes)
9861 {
9862 bool iface_p;
9863 tree id1;
9864 tree superclass;
9865 if (c_parser_next_token_is_keyword (parser, RID_AT_INTERFACE))
9866 iface_p = true;
9867 else if (c_parser_next_token_is_keyword (parser, RID_AT_IMPLEMENTATION))
9868 iface_p = false;
9869 else
9870 gcc_unreachable ();
9871
9872 c_parser_consume_token (parser);
9873 if (c_parser_next_token_is_not (parser, CPP_NAME))
9874 {
9875 c_parser_error (parser, "expected identifier");
9876 return;
9877 }
9878 id1 = c_parser_peek_token (parser)->value;
9879 c_parser_consume_token (parser);
9880 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
9881 {
9882 /* We have a category or class extension. */
9883 tree id2;
9884 tree proto = NULL_TREE;
9885 matching_parens parens;
9886 parens.consume_open (parser);
9887 if (c_parser_next_token_is_not (parser, CPP_NAME))
9888 {
9889 if (iface_p && c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
9890 {
9891 /* We have a class extension. */
9892 id2 = NULL_TREE;
9893 }
9894 else
9895 {
9896 c_parser_error (parser, "expected identifier or %<)%>");
9897 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
9898 return;
9899 }
9900 }
9901 else
9902 {
9903 id2 = c_parser_peek_token (parser)->value;
9904 c_parser_consume_token (parser);
9905 }
9906 parens.skip_until_found_close (parser);
9907 if (!iface_p)
9908 {
9909 objc_start_category_implementation (id1, id2);
9910 return;
9911 }
9912 if (c_parser_next_token_is (parser, CPP_LESS))
9913 proto = c_parser_objc_protocol_refs (parser);
9914 objc_start_category_interface (id1, id2, proto, attributes);
9915 c_parser_objc_methodprotolist (parser);
9916 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
9917 objc_finish_interface ();
9918 return;
9919 }
9920 if (c_parser_next_token_is (parser, CPP_COLON))
9921 {
9922 c_parser_consume_token (parser);
9923 if (c_parser_next_token_is_not (parser, CPP_NAME))
9924 {
9925 c_parser_error (parser, "expected identifier");
9926 return;
9927 }
9928 superclass = c_parser_peek_token (parser)->value;
9929 c_parser_consume_token (parser);
9930 }
9931 else
9932 superclass = NULL_TREE;
9933 if (iface_p)
9934 {
9935 tree proto = NULL_TREE;
9936 if (c_parser_next_token_is (parser, CPP_LESS))
9937 proto = c_parser_objc_protocol_refs (parser);
9938 objc_start_class_interface (id1, superclass, proto, attributes);
9939 }
9940 else
9941 objc_start_class_implementation (id1, superclass);
9942 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
9943 c_parser_objc_class_instance_variables (parser);
9944 if (iface_p)
9945 {
9946 objc_continue_interface ();
9947 c_parser_objc_methodprotolist (parser);
9948 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
9949 objc_finish_interface ();
9950 }
9951 else
9952 {
9953 objc_continue_implementation ();
9954 return;
9955 }
9956 }
9957
9958 /* Parse objc-class-instance-variables.
9959
9960 objc-class-instance-variables:
9961 { objc-instance-variable-decl-list[opt] }
9962
9963 objc-instance-variable-decl-list:
9964 objc-visibility-spec
9965 objc-instance-variable-decl ;
9966 ;
9967 objc-instance-variable-decl-list objc-visibility-spec
9968 objc-instance-variable-decl-list objc-instance-variable-decl ;
9969 objc-instance-variable-decl-list ;
9970
9971 objc-visibility-spec:
9972 @private
9973 @protected
9974 @public
9975
9976 objc-instance-variable-decl:
9977 struct-declaration
9978 */
9979
9980 static void
9981 c_parser_objc_class_instance_variables (c_parser *parser)
9982 {
9983 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
9984 c_parser_consume_token (parser);
9985 while (c_parser_next_token_is_not (parser, CPP_EOF))
9986 {
9987 tree decls;
9988 /* Parse any stray semicolon. */
9989 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
9990 {
9991 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
9992 "extra semicolon");
9993 c_parser_consume_token (parser);
9994 continue;
9995 }
9996 /* Stop if at the end of the instance variables. */
9997 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
9998 {
9999 c_parser_consume_token (parser);
10000 break;
10001 }
10002 /* Parse any objc-visibility-spec. */
10003 if (c_parser_next_token_is_keyword (parser, RID_AT_PRIVATE))
10004 {
10005 c_parser_consume_token (parser);
10006 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
10007 continue;
10008 }
10009 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROTECTED))
10010 {
10011 c_parser_consume_token (parser);
10012 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
10013 continue;
10014 }
10015 else if (c_parser_next_token_is_keyword (parser, RID_AT_PUBLIC))
10016 {
10017 c_parser_consume_token (parser);
10018 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
10019 continue;
10020 }
10021 else if (c_parser_next_token_is_keyword (parser, RID_AT_PACKAGE))
10022 {
10023 c_parser_consume_token (parser);
10024 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
10025 continue;
10026 }
10027 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
10028 {
10029 c_parser_pragma (parser, pragma_external, NULL);
10030 continue;
10031 }
10032
10033 /* Parse some comma-separated declarations. */
10034 decls = c_parser_struct_declaration (parser);
10035 if (decls == NULL)
10036 {
10037 /* There is a syntax error. We want to skip the offending
10038 tokens up to the next ';' (included) or '}'
10039 (excluded). */
10040
10041 /* First, skip manually a ')' or ']'. This is because they
10042 reduce the nesting level, so c_parser_skip_until_found()
10043 wouldn't be able to skip past them. */
10044 c_token *token = c_parser_peek_token (parser);
10045 if (token->type == CPP_CLOSE_PAREN || token->type == CPP_CLOSE_SQUARE)
10046 c_parser_consume_token (parser);
10047
10048 /* Then, do the standard skipping. */
10049 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
10050
10051 /* We hopefully recovered. Start normal parsing again. */
10052 parser->error = false;
10053 continue;
10054 }
10055 else
10056 {
10057 /* Comma-separated instance variables are chained together
10058 in reverse order; add them one by one. */
10059 tree ivar = nreverse (decls);
10060 for (; ivar; ivar = DECL_CHAIN (ivar))
10061 objc_add_instance_variable (copy_node (ivar));
10062 }
10063 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
10064 }
10065 }
10066
10067 /* Parse an objc-class-declaration.
10068
10069 objc-class-declaration:
10070 @class identifier-list ;
10071 */
10072
10073 static void
10074 c_parser_objc_class_declaration (c_parser *parser)
10075 {
10076 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_CLASS));
10077 c_parser_consume_token (parser);
10078 /* Any identifiers, including those declared as type names, are OK
10079 here. */
10080 while (true)
10081 {
10082 tree id;
10083 if (c_parser_next_token_is_not (parser, CPP_NAME))
10084 {
10085 c_parser_error (parser, "expected identifier");
10086 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
10087 parser->error = false;
10088 return;
10089 }
10090 id = c_parser_peek_token (parser)->value;
10091 objc_declare_class (id);
10092 c_parser_consume_token (parser);
10093 if (c_parser_next_token_is (parser, CPP_COMMA))
10094 c_parser_consume_token (parser);
10095 else
10096 break;
10097 }
10098 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
10099 }
10100
10101 /* Parse an objc-alias-declaration.
10102
10103 objc-alias-declaration:
10104 @compatibility_alias identifier identifier ;
10105 */
10106
10107 static void
10108 c_parser_objc_alias_declaration (c_parser *parser)
10109 {
10110 tree id1, id2;
10111 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_ALIAS));
10112 c_parser_consume_token (parser);
10113 if (c_parser_next_token_is_not (parser, CPP_NAME))
10114 {
10115 c_parser_error (parser, "expected identifier");
10116 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
10117 return;
10118 }
10119 id1 = c_parser_peek_token (parser)->value;
10120 c_parser_consume_token (parser);
10121 if (c_parser_next_token_is_not (parser, CPP_NAME))
10122 {
10123 c_parser_error (parser, "expected identifier");
10124 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
10125 return;
10126 }
10127 id2 = c_parser_peek_token (parser)->value;
10128 c_parser_consume_token (parser);
10129 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
10130 objc_declare_alias (id1, id2);
10131 }
10132
10133 /* Parse an objc-protocol-definition.
10134
10135 objc-protocol-definition:
10136 @protocol identifier objc-protocol-refs[opt] objc-methodprotolist @end
10137 @protocol identifier-list ;
10138
10139 "@protocol identifier ;" should be resolved as "@protocol
10140 identifier-list ;": objc-methodprotolist may not start with a
10141 semicolon in the first alternative if objc-protocol-refs are
10142 omitted. */
10143
10144 static void
10145 c_parser_objc_protocol_definition (c_parser *parser, tree attributes)
10146 {
10147 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROTOCOL));
10148
10149 c_parser_consume_token (parser);
10150 if (c_parser_next_token_is_not (parser, CPP_NAME))
10151 {
10152 c_parser_error (parser, "expected identifier");
10153 return;
10154 }
10155 if (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
10156 || c_parser_peek_2nd_token (parser)->type == CPP_SEMICOLON)
10157 {
10158 /* Any identifiers, including those declared as type names, are
10159 OK here. */
10160 while (true)
10161 {
10162 tree id;
10163 if (c_parser_next_token_is_not (parser, CPP_NAME))
10164 {
10165 c_parser_error (parser, "expected identifier");
10166 break;
10167 }
10168 id = c_parser_peek_token (parser)->value;
10169 objc_declare_protocol (id, attributes);
10170 c_parser_consume_token (parser);
10171 if (c_parser_next_token_is (parser, CPP_COMMA))
10172 c_parser_consume_token (parser);
10173 else
10174 break;
10175 }
10176 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
10177 }
10178 else
10179 {
10180 tree id = c_parser_peek_token (parser)->value;
10181 tree proto = NULL_TREE;
10182 c_parser_consume_token (parser);
10183 if (c_parser_next_token_is (parser, CPP_LESS))
10184 proto = c_parser_objc_protocol_refs (parser);
10185 parser->objc_pq_context = true;
10186 objc_start_protocol (id, proto, attributes);
10187 c_parser_objc_methodprotolist (parser);
10188 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
10189 parser->objc_pq_context = false;
10190 objc_finish_interface ();
10191 }
10192 }
10193
10194 /* Parse an objc-method-type.
10195
10196 objc-method-type:
10197 +
10198 -
10199
10200 Return true if it is a class method (+) and false if it is
10201 an instance method (-).
10202 */
10203 static inline bool
10204 c_parser_objc_method_type (c_parser *parser)
10205 {
10206 switch (c_parser_peek_token (parser)->type)
10207 {
10208 case CPP_PLUS:
10209 c_parser_consume_token (parser);
10210 return true;
10211 case CPP_MINUS:
10212 c_parser_consume_token (parser);
10213 return false;
10214 default:
10215 gcc_unreachable ();
10216 }
10217 }
10218
10219 /* Parse an objc-method-definition.
10220
10221 objc-method-definition:
10222 objc-method-type objc-method-decl ;[opt] compound-statement
10223 */
10224
10225 static void
10226 c_parser_objc_method_definition (c_parser *parser)
10227 {
10228 bool is_class_method = c_parser_objc_method_type (parser);
10229 tree decl, attributes = NULL_TREE, expr = NULL_TREE;
10230 parser->objc_pq_context = true;
10231 decl = c_parser_objc_method_decl (parser, is_class_method, &attributes,
10232 &expr);
10233 if (decl == error_mark_node)
10234 return; /* Bail here. */
10235
10236 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
10237 {
10238 c_parser_consume_token (parser);
10239 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
10240 "extra semicolon in method definition specified");
10241 }
10242
10243 if (!c_parser_next_token_is (parser, CPP_OPEN_BRACE))
10244 {
10245 c_parser_error (parser, "expected %<{%>");
10246 return;
10247 }
10248
10249 parser->objc_pq_context = false;
10250 if (objc_start_method_definition (is_class_method, decl, attributes, expr))
10251 {
10252 add_stmt (c_parser_compound_statement (parser));
10253 objc_finish_method_definition (current_function_decl);
10254 }
10255 else
10256 {
10257 /* This code is executed when we find a method definition
10258 outside of an @implementation context (or invalid for other
10259 reasons). Parse the method (to keep going) but do not emit
10260 any code.
10261 */
10262 c_parser_compound_statement (parser);
10263 }
10264 }
10265
10266 /* Parse an objc-methodprotolist.
10267
10268 objc-methodprotolist:
10269 empty
10270 objc-methodprotolist objc-methodproto
10271 objc-methodprotolist declaration
10272 objc-methodprotolist ;
10273 @optional
10274 @required
10275
10276 The declaration is a data definition, which may be missing
10277 declaration specifiers under the same rules and diagnostics as
10278 other data definitions outside functions, and the stray semicolon
10279 is diagnosed the same way as a stray semicolon outside a
10280 function. */
10281
10282 static void
10283 c_parser_objc_methodprotolist (c_parser *parser)
10284 {
10285 while (true)
10286 {
10287 /* The list is terminated by @end. */
10288 switch (c_parser_peek_token (parser)->type)
10289 {
10290 case CPP_SEMICOLON:
10291 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
10292 "ISO C does not allow extra %<;%> outside of a function");
10293 c_parser_consume_token (parser);
10294 break;
10295 case CPP_PLUS:
10296 case CPP_MINUS:
10297 c_parser_objc_methodproto (parser);
10298 break;
10299 case CPP_PRAGMA:
10300 c_parser_pragma (parser, pragma_external, NULL);
10301 break;
10302 case CPP_EOF:
10303 return;
10304 default:
10305 if (c_parser_next_token_is_keyword (parser, RID_AT_END))
10306 return;
10307 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROPERTY))
10308 c_parser_objc_at_property_declaration (parser);
10309 else if (c_parser_next_token_is_keyword (parser, RID_AT_OPTIONAL))
10310 {
10311 objc_set_method_opt (true);
10312 c_parser_consume_token (parser);
10313 }
10314 else if (c_parser_next_token_is_keyword (parser, RID_AT_REQUIRED))
10315 {
10316 objc_set_method_opt (false);
10317 c_parser_consume_token (parser);
10318 }
10319 else
10320 c_parser_declaration_or_fndef (parser, false, false, true,
10321 false, true, NULL, vNULL);
10322 break;
10323 }
10324 }
10325 }
10326
10327 /* Parse an objc-methodproto.
10328
10329 objc-methodproto:
10330 objc-method-type objc-method-decl ;
10331 */
10332
10333 static void
10334 c_parser_objc_methodproto (c_parser *parser)
10335 {
10336 bool is_class_method = c_parser_objc_method_type (parser);
10337 tree decl, attributes = NULL_TREE;
10338
10339 /* Remember protocol qualifiers in prototypes. */
10340 parser->objc_pq_context = true;
10341 decl = c_parser_objc_method_decl (parser, is_class_method, &attributes,
10342 NULL);
10343 /* Forget protocol qualifiers now. */
10344 parser->objc_pq_context = false;
10345
10346 /* Do not allow the presence of attributes to hide an erroneous
10347 method implementation in the interface section. */
10348 if (!c_parser_next_token_is (parser, CPP_SEMICOLON))
10349 {
10350 c_parser_error (parser, "expected %<;%>");
10351 return;
10352 }
10353
10354 if (decl != error_mark_node)
10355 objc_add_method_declaration (is_class_method, decl, attributes);
10356
10357 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
10358 }
10359
10360 /* If we are at a position that method attributes may be present, check that
10361 there are not any parsed already (a syntax error) and then collect any
10362 specified at the current location. Finally, if new attributes were present,
10363 check that the next token is legal ( ';' for decls and '{' for defs). */
10364
10365 static bool
10366 c_parser_objc_maybe_method_attributes (c_parser* parser, tree* attributes)
10367 {
10368 bool bad = false;
10369 if (*attributes)
10370 {
10371 c_parser_error (parser,
10372 "method attributes must be specified at the end only");
10373 *attributes = NULL_TREE;
10374 bad = true;
10375 }
10376
10377 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
10378 *attributes = c_parser_attributes (parser);
10379
10380 /* If there were no attributes here, just report any earlier error. */
10381 if (*attributes == NULL_TREE || bad)
10382 return bad;
10383
10384 /* If the attributes are followed by a ; or {, then just report any earlier
10385 error. */
10386 if (c_parser_next_token_is (parser, CPP_SEMICOLON)
10387 || c_parser_next_token_is (parser, CPP_OPEN_BRACE))
10388 return bad;
10389
10390 /* We've got attributes, but not at the end. */
10391 c_parser_error (parser,
10392 "expected %<;%> or %<{%> after method attribute definition");
10393 return true;
10394 }
10395
10396 /* Parse an objc-method-decl.
10397
10398 objc-method-decl:
10399 ( objc-type-name ) objc-selector
10400 objc-selector
10401 ( objc-type-name ) objc-keyword-selector objc-optparmlist
10402 objc-keyword-selector objc-optparmlist
10403 attributes
10404
10405 objc-keyword-selector:
10406 objc-keyword-decl
10407 objc-keyword-selector objc-keyword-decl
10408
10409 objc-keyword-decl:
10410 objc-selector : ( objc-type-name ) identifier
10411 objc-selector : identifier
10412 : ( objc-type-name ) identifier
10413 : identifier
10414
10415 objc-optparmlist:
10416 objc-optparms objc-optellipsis
10417
10418 objc-optparms:
10419 empty
10420 objc-opt-parms , parameter-declaration
10421
10422 objc-optellipsis:
10423 empty
10424 , ...
10425 */
10426
10427 static tree
10428 c_parser_objc_method_decl (c_parser *parser, bool is_class_method,
10429 tree *attributes, tree *expr)
10430 {
10431 tree type = NULL_TREE;
10432 tree sel;
10433 tree parms = NULL_TREE;
10434 bool ellipsis = false;
10435 bool attr_err = false;
10436
10437 *attributes = NULL_TREE;
10438 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
10439 {
10440 matching_parens parens;
10441 parens.consume_open (parser);
10442 type = c_parser_objc_type_name (parser);
10443 parens.skip_until_found_close (parser);
10444 }
10445 sel = c_parser_objc_selector (parser);
10446 /* If there is no selector, or a colon follows, we have an
10447 objc-keyword-selector. If there is a selector, and a colon does
10448 not follow, that selector ends the objc-method-decl. */
10449 if (!sel || c_parser_next_token_is (parser, CPP_COLON))
10450 {
10451 tree tsel = sel;
10452 tree list = NULL_TREE;
10453 while (true)
10454 {
10455 tree atype = NULL_TREE, id, keyworddecl;
10456 tree param_attr = NULL_TREE;
10457 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
10458 break;
10459 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
10460 {
10461 c_parser_consume_token (parser);
10462 atype = c_parser_objc_type_name (parser);
10463 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
10464 "expected %<)%>");
10465 }
10466 /* New ObjC allows attributes on method parameters. */
10467 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
10468 param_attr = c_parser_attributes (parser);
10469 if (c_parser_next_token_is_not (parser, CPP_NAME))
10470 {
10471 c_parser_error (parser, "expected identifier");
10472 return error_mark_node;
10473 }
10474 id = c_parser_peek_token (parser)->value;
10475 c_parser_consume_token (parser);
10476 keyworddecl = objc_build_keyword_decl (tsel, atype, id, param_attr);
10477 list = chainon (list, keyworddecl);
10478 tsel = c_parser_objc_selector (parser);
10479 if (!tsel && c_parser_next_token_is_not (parser, CPP_COLON))
10480 break;
10481 }
10482
10483 attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
10484
10485 /* Parse the optional parameter list. Optional Objective-C
10486 method parameters follow the C syntax, and may include '...'
10487 to denote a variable number of arguments. */
10488 parms = make_node (TREE_LIST);
10489 while (c_parser_next_token_is (parser, CPP_COMMA))
10490 {
10491 struct c_parm *parm;
10492 c_parser_consume_token (parser);
10493 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
10494 {
10495 ellipsis = true;
10496 c_parser_consume_token (parser);
10497 attr_err |= c_parser_objc_maybe_method_attributes
10498 (parser, attributes) ;
10499 break;
10500 }
10501 parm = c_parser_parameter_declaration (parser, NULL_TREE);
10502 if (parm == NULL)
10503 break;
10504 parms = chainon (parms,
10505 build_tree_list (NULL_TREE, grokparm (parm, expr)));
10506 }
10507 sel = list;
10508 }
10509 else
10510 attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
10511
10512 if (sel == NULL)
10513 {
10514 c_parser_error (parser, "objective-c method declaration is expected");
10515 return error_mark_node;
10516 }
10517
10518 if (attr_err)
10519 return error_mark_node;
10520
10521 return objc_build_method_signature (is_class_method, type, sel, parms, ellipsis);
10522 }
10523
10524 /* Parse an objc-type-name.
10525
10526 objc-type-name:
10527 objc-type-qualifiers[opt] type-name
10528 objc-type-qualifiers[opt]
10529
10530 objc-type-qualifiers:
10531 objc-type-qualifier
10532 objc-type-qualifiers objc-type-qualifier
10533
10534 objc-type-qualifier: one of
10535 in out inout bycopy byref oneway
10536 */
10537
10538 static tree
10539 c_parser_objc_type_name (c_parser *parser)
10540 {
10541 tree quals = NULL_TREE;
10542 struct c_type_name *type_name = NULL;
10543 tree type = NULL_TREE;
10544 while (true)
10545 {
10546 c_token *token = c_parser_peek_token (parser);
10547 if (token->type == CPP_KEYWORD
10548 && (token->keyword == RID_IN
10549 || token->keyword == RID_OUT
10550 || token->keyword == RID_INOUT
10551 || token->keyword == RID_BYCOPY
10552 || token->keyword == RID_BYREF
10553 || token->keyword == RID_ONEWAY))
10554 {
10555 quals = chainon (build_tree_list (NULL_TREE, token->value), quals);
10556 c_parser_consume_token (parser);
10557 }
10558 else
10559 break;
10560 }
10561 if (c_parser_next_tokens_start_typename (parser, cla_prefer_type))
10562 type_name = c_parser_type_name (parser);
10563 if (type_name)
10564 type = groktypename (type_name, NULL, NULL);
10565
10566 /* If the type is unknown, and error has already been produced and
10567 we need to recover from the error. In that case, use NULL_TREE
10568 for the type, as if no type had been specified; this will use the
10569 default type ('id') which is good for error recovery. */
10570 if (type == error_mark_node)
10571 type = NULL_TREE;
10572
10573 return build_tree_list (quals, type);
10574 }
10575
10576 /* Parse objc-protocol-refs.
10577
10578 objc-protocol-refs:
10579 < identifier-list >
10580 */
10581
10582 static tree
10583 c_parser_objc_protocol_refs (c_parser *parser)
10584 {
10585 tree list = NULL_TREE;
10586 gcc_assert (c_parser_next_token_is (parser, CPP_LESS));
10587 c_parser_consume_token (parser);
10588 /* Any identifiers, including those declared as type names, are OK
10589 here. */
10590 while (true)
10591 {
10592 tree id;
10593 if (c_parser_next_token_is_not (parser, CPP_NAME))
10594 {
10595 c_parser_error (parser, "expected identifier");
10596 break;
10597 }
10598 id = c_parser_peek_token (parser)->value;
10599 list = chainon (list, build_tree_list (NULL_TREE, id));
10600 c_parser_consume_token (parser);
10601 if (c_parser_next_token_is (parser, CPP_COMMA))
10602 c_parser_consume_token (parser);
10603 else
10604 break;
10605 }
10606 c_parser_require (parser, CPP_GREATER, "expected %<>%>");
10607 return list;
10608 }
10609
10610 /* Parse an objc-try-catch-finally-statement.
10611
10612 objc-try-catch-finally-statement:
10613 @try compound-statement objc-catch-list[opt]
10614 @try compound-statement objc-catch-list[opt] @finally compound-statement
10615
10616 objc-catch-list:
10617 @catch ( objc-catch-parameter-declaration ) compound-statement
10618 objc-catch-list @catch ( objc-catch-parameter-declaration ) compound-statement
10619
10620 objc-catch-parameter-declaration:
10621 parameter-declaration
10622 '...'
10623
10624 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
10625
10626 PS: This function is identical to cp_parser_objc_try_catch_finally_statement
10627 for C++. Keep them in sync. */
10628
10629 static void
10630 c_parser_objc_try_catch_finally_statement (c_parser *parser)
10631 {
10632 location_t location;
10633 tree stmt;
10634
10635 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_TRY));
10636 c_parser_consume_token (parser);
10637 location = c_parser_peek_token (parser)->location;
10638 objc_maybe_warn_exceptions (location);
10639 stmt = c_parser_compound_statement (parser);
10640 objc_begin_try_stmt (location, stmt);
10641
10642 while (c_parser_next_token_is_keyword (parser, RID_AT_CATCH))
10643 {
10644 struct c_parm *parm;
10645 tree parameter_declaration = error_mark_node;
10646 bool seen_open_paren = false;
10647
10648 c_parser_consume_token (parser);
10649 matching_parens parens;
10650 if (!parens.require_open (parser))
10651 seen_open_paren = true;
10652 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
10653 {
10654 /* We have "@catch (...)" (where the '...' are literally
10655 what is in the code). Skip the '...'.
10656 parameter_declaration is set to NULL_TREE, and
10657 objc_being_catch_clauses() knows that that means
10658 '...'. */
10659 c_parser_consume_token (parser);
10660 parameter_declaration = NULL_TREE;
10661 }
10662 else
10663 {
10664 /* We have "@catch (NSException *exception)" or something
10665 like that. Parse the parameter declaration. */
10666 parm = c_parser_parameter_declaration (parser, NULL_TREE);
10667 if (parm == NULL)
10668 parameter_declaration = error_mark_node;
10669 else
10670 parameter_declaration = grokparm (parm, NULL);
10671 }
10672 if (seen_open_paren)
10673 parens.require_close (parser);
10674 else
10675 {
10676 /* If there was no open parenthesis, we are recovering from
10677 an error, and we are trying to figure out what mistake
10678 the user has made. */
10679
10680 /* If there is an immediate closing parenthesis, the user
10681 probably forgot the opening one (ie, they typed "@catch
10682 NSException *e)". Parse the closing parenthesis and keep
10683 going. */
10684 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
10685 c_parser_consume_token (parser);
10686
10687 /* If these is no immediate closing parenthesis, the user
10688 probably doesn't know that parenthesis are required at
10689 all (ie, they typed "@catch NSException *e"). So, just
10690 forget about the closing parenthesis and keep going. */
10691 }
10692 objc_begin_catch_clause (parameter_declaration);
10693 if (c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
10694 c_parser_compound_statement_nostart (parser);
10695 objc_finish_catch_clause ();
10696 }
10697 if (c_parser_next_token_is_keyword (parser, RID_AT_FINALLY))
10698 {
10699 c_parser_consume_token (parser);
10700 location = c_parser_peek_token (parser)->location;
10701 stmt = c_parser_compound_statement (parser);
10702 objc_build_finally_clause (location, stmt);
10703 }
10704 objc_finish_try_stmt ();
10705 }
10706
10707 /* Parse an objc-synchronized-statement.
10708
10709 objc-synchronized-statement:
10710 @synchronized ( expression ) compound-statement
10711 */
10712
10713 static void
10714 c_parser_objc_synchronized_statement (c_parser *parser)
10715 {
10716 location_t loc;
10717 tree expr, stmt;
10718 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNCHRONIZED));
10719 c_parser_consume_token (parser);
10720 loc = c_parser_peek_token (parser)->location;
10721 objc_maybe_warn_exceptions (loc);
10722 matching_parens parens;
10723 if (parens.require_open (parser))
10724 {
10725 struct c_expr ce = c_parser_expression (parser);
10726 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
10727 expr = ce.value;
10728 expr = c_fully_fold (expr, false, NULL);
10729 parens.skip_until_found_close (parser);
10730 }
10731 else
10732 expr = error_mark_node;
10733 stmt = c_parser_compound_statement (parser);
10734 objc_build_synchronized (loc, expr, stmt);
10735 }
10736
10737 /* Parse an objc-selector; return NULL_TREE without an error if the
10738 next token is not an objc-selector.
10739
10740 objc-selector:
10741 identifier
10742 one of
10743 enum struct union if else while do for switch case default
10744 break continue return goto asm sizeof typeof __alignof
10745 unsigned long const short volatile signed restrict _Complex
10746 in out inout bycopy byref oneway int char float double void _Bool
10747 _Atomic
10748
10749 ??? Why this selection of keywords but not, for example, storage
10750 class specifiers? */
10751
10752 static tree
10753 c_parser_objc_selector (c_parser *parser)
10754 {
10755 c_token *token = c_parser_peek_token (parser);
10756 tree value = token->value;
10757 if (token->type == CPP_NAME)
10758 {
10759 c_parser_consume_token (parser);
10760 return value;
10761 }
10762 if (token->type != CPP_KEYWORD)
10763 return NULL_TREE;
10764 switch (token->keyword)
10765 {
10766 case RID_ENUM:
10767 case RID_STRUCT:
10768 case RID_UNION:
10769 case RID_IF:
10770 case RID_ELSE:
10771 case RID_WHILE:
10772 case RID_DO:
10773 case RID_FOR:
10774 case RID_SWITCH:
10775 case RID_CASE:
10776 case RID_DEFAULT:
10777 case RID_BREAK:
10778 case RID_CONTINUE:
10779 case RID_RETURN:
10780 case RID_GOTO:
10781 case RID_ASM:
10782 case RID_SIZEOF:
10783 case RID_TYPEOF:
10784 case RID_ALIGNOF:
10785 case RID_UNSIGNED:
10786 case RID_LONG:
10787 case RID_CONST:
10788 case RID_SHORT:
10789 case RID_VOLATILE:
10790 case RID_SIGNED:
10791 case RID_RESTRICT:
10792 case RID_COMPLEX:
10793 case RID_IN:
10794 case RID_OUT:
10795 case RID_INOUT:
10796 case RID_BYCOPY:
10797 case RID_BYREF:
10798 case RID_ONEWAY:
10799 case RID_INT:
10800 case RID_CHAR:
10801 case RID_FLOAT:
10802 case RID_DOUBLE:
10803 CASE_RID_FLOATN_NX:
10804 case RID_VOID:
10805 case RID_BOOL:
10806 case RID_ATOMIC:
10807 case RID_AUTO_TYPE:
10808 case RID_INT_N_0:
10809 case RID_INT_N_1:
10810 case RID_INT_N_2:
10811 case RID_INT_N_3:
10812 c_parser_consume_token (parser);
10813 return value;
10814 default:
10815 return NULL_TREE;
10816 }
10817 }
10818
10819 /* Parse an objc-selector-arg.
10820
10821 objc-selector-arg:
10822 objc-selector
10823 objc-keywordname-list
10824
10825 objc-keywordname-list:
10826 objc-keywordname
10827 objc-keywordname-list objc-keywordname
10828
10829 objc-keywordname:
10830 objc-selector :
10831 :
10832 */
10833
10834 static tree
10835 c_parser_objc_selector_arg (c_parser *parser)
10836 {
10837 tree sel = c_parser_objc_selector (parser);
10838 tree list = NULL_TREE;
10839 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
10840 return sel;
10841 while (true)
10842 {
10843 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
10844 return list;
10845 list = chainon (list, build_tree_list (sel, NULL_TREE));
10846 sel = c_parser_objc_selector (parser);
10847 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
10848 break;
10849 }
10850 return list;
10851 }
10852
10853 /* Parse an objc-receiver.
10854
10855 objc-receiver:
10856 expression
10857 class-name
10858 type-name
10859 */
10860
10861 static tree
10862 c_parser_objc_receiver (c_parser *parser)
10863 {
10864 location_t loc = c_parser_peek_token (parser)->location;
10865
10866 if (c_parser_peek_token (parser)->type == CPP_NAME
10867 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
10868 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
10869 {
10870 tree id = c_parser_peek_token (parser)->value;
10871 c_parser_consume_token (parser);
10872 return objc_get_class_reference (id);
10873 }
10874 struct c_expr ce = c_parser_expression (parser);
10875 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
10876 return c_fully_fold (ce.value, false, NULL);
10877 }
10878
10879 /* Parse objc-message-args.
10880
10881 objc-message-args:
10882 objc-selector
10883 objc-keywordarg-list
10884
10885 objc-keywordarg-list:
10886 objc-keywordarg
10887 objc-keywordarg-list objc-keywordarg
10888
10889 objc-keywordarg:
10890 objc-selector : objc-keywordexpr
10891 : objc-keywordexpr
10892 */
10893
10894 static tree
10895 c_parser_objc_message_args (c_parser *parser)
10896 {
10897 tree sel = c_parser_objc_selector (parser);
10898 tree list = NULL_TREE;
10899 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
10900 return sel;
10901 while (true)
10902 {
10903 tree keywordexpr;
10904 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
10905 return error_mark_node;
10906 keywordexpr = c_parser_objc_keywordexpr (parser);
10907 list = chainon (list, build_tree_list (sel, keywordexpr));
10908 sel = c_parser_objc_selector (parser);
10909 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
10910 break;
10911 }
10912 return list;
10913 }
10914
10915 /* Parse an objc-keywordexpr.
10916
10917 objc-keywordexpr:
10918 nonempty-expr-list
10919 */
10920
10921 static tree
10922 c_parser_objc_keywordexpr (c_parser *parser)
10923 {
10924 tree ret;
10925 vec<tree, va_gc> *expr_list = c_parser_expr_list (parser, true, true,
10926 NULL, NULL, NULL, NULL);
10927 if (vec_safe_length (expr_list) == 1)
10928 {
10929 /* Just return the expression, remove a level of
10930 indirection. */
10931 ret = (*expr_list)[0];
10932 }
10933 else
10934 {
10935 /* We have a comma expression, we will collapse later. */
10936 ret = build_tree_list_vec (expr_list);
10937 }
10938 release_tree_vector (expr_list);
10939 return ret;
10940 }
10941
10942 /* A check, needed in several places, that ObjC interface, implementation or
10943 method definitions are not prefixed by incorrect items. */
10944 static bool
10945 c_parser_objc_diagnose_bad_element_prefix (c_parser *parser,
10946 struct c_declspecs *specs)
10947 {
10948 if (!specs->declspecs_seen_p || specs->non_sc_seen_p
10949 || specs->typespec_kind != ctsk_none)
10950 {
10951 c_parser_error (parser,
10952 "no type or storage class may be specified here,");
10953 c_parser_skip_to_end_of_block_or_statement (parser);
10954 return true;
10955 }
10956 return false;
10957 }
10958
10959 /* Parse an Objective-C @property declaration. The syntax is:
10960
10961 objc-property-declaration:
10962 '@property' objc-property-attributes[opt] struct-declaration ;
10963
10964 objc-property-attributes:
10965 '(' objc-property-attribute-list ')'
10966
10967 objc-property-attribute-list:
10968 objc-property-attribute
10969 objc-property-attribute-list, objc-property-attribute
10970
10971 objc-property-attribute
10972 'getter' = identifier
10973 'setter' = identifier
10974 'readonly'
10975 'readwrite'
10976 'assign'
10977 'retain'
10978 'copy'
10979 'nonatomic'
10980
10981 For example:
10982 @property NSString *name;
10983 @property (readonly) id object;
10984 @property (retain, nonatomic, getter=getTheName) id name;
10985 @property int a, b, c;
10986
10987 PS: This function is identical to cp_parser_objc_at_propery_declaration
10988 for C++. Keep them in sync. */
10989 static void
10990 c_parser_objc_at_property_declaration (c_parser *parser)
10991 {
10992 /* The following variables hold the attributes of the properties as
10993 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
10994 seen. When we see an attribute, we set them to 'true' (if they
10995 are boolean properties) or to the identifier (if they have an
10996 argument, ie, for getter and setter). Note that here we only
10997 parse the list of attributes, check the syntax and accumulate the
10998 attributes that we find. objc_add_property_declaration() will
10999 then process the information. */
11000 bool property_assign = false;
11001 bool property_copy = false;
11002 tree property_getter_ident = NULL_TREE;
11003 bool property_nonatomic = false;
11004 bool property_readonly = false;
11005 bool property_readwrite = false;
11006 bool property_retain = false;
11007 tree property_setter_ident = NULL_TREE;
11008
11009 /* 'properties' is the list of properties that we read. Usually a
11010 single one, but maybe more (eg, in "@property int a, b, c;" there
11011 are three). */
11012 tree properties;
11013 location_t loc;
11014
11015 loc = c_parser_peek_token (parser)->location;
11016 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROPERTY));
11017
11018 c_parser_consume_token (parser); /* Eat '@property'. */
11019
11020 /* Parse the optional attribute list... */
11021 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
11022 {
11023 matching_parens parens;
11024
11025 /* Eat the '(' */
11026 parens.consume_open (parser);
11027
11028 /* Property attribute keywords are valid now. */
11029 parser->objc_property_attr_context = true;
11030
11031 while (true)
11032 {
11033 bool syntax_error = false;
11034 c_token *token = c_parser_peek_token (parser);
11035 enum rid keyword;
11036
11037 if (token->type != CPP_KEYWORD)
11038 {
11039 if (token->type == CPP_CLOSE_PAREN)
11040 c_parser_error (parser, "expected identifier");
11041 else
11042 {
11043 c_parser_consume_token (parser);
11044 c_parser_error (parser, "unknown property attribute");
11045 }
11046 break;
11047 }
11048 keyword = token->keyword;
11049 c_parser_consume_token (parser);
11050 switch (keyword)
11051 {
11052 case RID_ASSIGN: property_assign = true; break;
11053 case RID_COPY: property_copy = true; break;
11054 case RID_NONATOMIC: property_nonatomic = true; break;
11055 case RID_READONLY: property_readonly = true; break;
11056 case RID_READWRITE: property_readwrite = true; break;
11057 case RID_RETAIN: property_retain = true; break;
11058
11059 case RID_GETTER:
11060 case RID_SETTER:
11061 if (c_parser_next_token_is_not (parser, CPP_EQ))
11062 {
11063 if (keyword == RID_GETTER)
11064 c_parser_error (parser,
11065 "missing %<=%> (after %<getter%> attribute)");
11066 else
11067 c_parser_error (parser,
11068 "missing %<=%> (after %<setter%> attribute)");
11069 syntax_error = true;
11070 break;
11071 }
11072 c_parser_consume_token (parser); /* eat the = */
11073 if (c_parser_next_token_is_not (parser, CPP_NAME))
11074 {
11075 c_parser_error (parser, "expected identifier");
11076 syntax_error = true;
11077 break;
11078 }
11079 if (keyword == RID_SETTER)
11080 {
11081 if (property_setter_ident != NULL_TREE)
11082 c_parser_error (parser, "the %<setter%> attribute may only be specified once");
11083 else
11084 property_setter_ident = c_parser_peek_token (parser)->value;
11085 c_parser_consume_token (parser);
11086 if (c_parser_next_token_is_not (parser, CPP_COLON))
11087 c_parser_error (parser, "setter name must terminate with %<:%>");
11088 else
11089 c_parser_consume_token (parser);
11090 }
11091 else
11092 {
11093 if (property_getter_ident != NULL_TREE)
11094 c_parser_error (parser, "the %<getter%> attribute may only be specified once");
11095 else
11096 property_getter_ident = c_parser_peek_token (parser)->value;
11097 c_parser_consume_token (parser);
11098 }
11099 break;
11100 default:
11101 c_parser_error (parser, "unknown property attribute");
11102 syntax_error = true;
11103 break;
11104 }
11105
11106 if (syntax_error)
11107 break;
11108
11109 if (c_parser_next_token_is (parser, CPP_COMMA))
11110 c_parser_consume_token (parser);
11111 else
11112 break;
11113 }
11114 parser->objc_property_attr_context = false;
11115 parens.skip_until_found_close (parser);
11116 }
11117 /* ... and the property declaration(s). */
11118 properties = c_parser_struct_declaration (parser);
11119
11120 if (properties == error_mark_node)
11121 {
11122 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
11123 parser->error = false;
11124 return;
11125 }
11126
11127 if (properties == NULL_TREE)
11128 c_parser_error (parser, "expected identifier");
11129 else
11130 {
11131 /* Comma-separated properties are chained together in
11132 reverse order; add them one by one. */
11133 properties = nreverse (properties);
11134
11135 for (; properties; properties = TREE_CHAIN (properties))
11136 objc_add_property_declaration (loc, copy_node (properties),
11137 property_readonly, property_readwrite,
11138 property_assign, property_retain,
11139 property_copy, property_nonatomic,
11140 property_getter_ident, property_setter_ident);
11141 }
11142
11143 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
11144 parser->error = false;
11145 }
11146
11147 /* Parse an Objective-C @synthesize declaration. The syntax is:
11148
11149 objc-synthesize-declaration:
11150 @synthesize objc-synthesize-identifier-list ;
11151
11152 objc-synthesize-identifier-list:
11153 objc-synthesize-identifier
11154 objc-synthesize-identifier-list, objc-synthesize-identifier
11155
11156 objc-synthesize-identifier
11157 identifier
11158 identifier = identifier
11159
11160 For example:
11161 @synthesize MyProperty;
11162 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
11163
11164 PS: This function is identical to cp_parser_objc_at_synthesize_declaration
11165 for C++. Keep them in sync.
11166 */
11167 static void
11168 c_parser_objc_at_synthesize_declaration (c_parser *parser)
11169 {
11170 tree list = NULL_TREE;
11171 location_t loc;
11172 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNTHESIZE));
11173 loc = c_parser_peek_token (parser)->location;
11174
11175 c_parser_consume_token (parser);
11176 while (true)
11177 {
11178 tree property, ivar;
11179 if (c_parser_next_token_is_not (parser, CPP_NAME))
11180 {
11181 c_parser_error (parser, "expected identifier");
11182 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
11183 /* Once we find the semicolon, we can resume normal parsing.
11184 We have to reset parser->error manually because
11185 c_parser_skip_until_found() won't reset it for us if the
11186 next token is precisely a semicolon. */
11187 parser->error = false;
11188 return;
11189 }
11190 property = c_parser_peek_token (parser)->value;
11191 c_parser_consume_token (parser);
11192 if (c_parser_next_token_is (parser, CPP_EQ))
11193 {
11194 c_parser_consume_token (parser);
11195 if (c_parser_next_token_is_not (parser, CPP_NAME))
11196 {
11197 c_parser_error (parser, "expected identifier");
11198 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
11199 parser->error = false;
11200 return;
11201 }
11202 ivar = c_parser_peek_token (parser)->value;
11203 c_parser_consume_token (parser);
11204 }
11205 else
11206 ivar = NULL_TREE;
11207 list = chainon (list, build_tree_list (ivar, property));
11208 if (c_parser_next_token_is (parser, CPP_COMMA))
11209 c_parser_consume_token (parser);
11210 else
11211 break;
11212 }
11213 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
11214 objc_add_synthesize_declaration (loc, list);
11215 }
11216
11217 /* Parse an Objective-C @dynamic declaration. The syntax is:
11218
11219 objc-dynamic-declaration:
11220 @dynamic identifier-list ;
11221
11222 For example:
11223 @dynamic MyProperty;
11224 @dynamic MyProperty, AnotherProperty;
11225
11226 PS: This function is identical to cp_parser_objc_at_dynamic_declaration
11227 for C++. Keep them in sync.
11228 */
11229 static void
11230 c_parser_objc_at_dynamic_declaration (c_parser *parser)
11231 {
11232 tree list = NULL_TREE;
11233 location_t loc;
11234 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_DYNAMIC));
11235 loc = c_parser_peek_token (parser)->location;
11236
11237 c_parser_consume_token (parser);
11238 while (true)
11239 {
11240 tree property;
11241 if (c_parser_next_token_is_not (parser, CPP_NAME))
11242 {
11243 c_parser_error (parser, "expected identifier");
11244 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
11245 parser->error = false;
11246 return;
11247 }
11248 property = c_parser_peek_token (parser)->value;
11249 list = chainon (list, build_tree_list (NULL_TREE, property));
11250 c_parser_consume_token (parser);
11251 if (c_parser_next_token_is (parser, CPP_COMMA))
11252 c_parser_consume_token (parser);
11253 else
11254 break;
11255 }
11256 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
11257 objc_add_dynamic_declaration (loc, list);
11258 }
11259
11260 \f
11261 /* Parse a pragma GCC ivdep. */
11262
11263 static bool
11264 c_parse_pragma_ivdep (c_parser *parser)
11265 {
11266 c_parser_consume_pragma (parser);
11267 c_parser_skip_to_pragma_eol (parser);
11268 return true;
11269 }
11270
11271 /* Parse a pragma GCC unroll. */
11272
11273 static unsigned short
11274 c_parser_pragma_unroll (c_parser *parser)
11275 {
11276 unsigned short unroll;
11277 c_parser_consume_pragma (parser);
11278 location_t location = c_parser_peek_token (parser)->location;
11279 tree expr = c_parser_expr_no_commas (parser, NULL).value;
11280 mark_exp_read (expr);
11281 expr = c_fully_fold (expr, false, NULL);
11282 HOST_WIDE_INT lunroll = 0;
11283 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr))
11284 || TREE_CODE (expr) != INTEGER_CST
11285 || (lunroll = tree_to_shwi (expr)) < 0
11286 || lunroll >= USHRT_MAX)
11287 {
11288 error_at (location, "%<#pragma GCC unroll%> requires an"
11289 " assignment-expression that evaluates to a non-negative"
11290 " integral constant less than %u", USHRT_MAX);
11291 unroll = 0;
11292 }
11293 else
11294 {
11295 unroll = (unsigned short)lunroll;
11296 if (unroll == 0)
11297 unroll = 1;
11298 }
11299
11300 c_parser_skip_to_pragma_eol (parser);
11301 return unroll;
11302 }
11303
11304 /* Handle pragmas. Some OpenMP pragmas are associated with, and therefore
11305 should be considered, statements. ALLOW_STMT is true if we're within
11306 the context of a function and such pragmas are to be allowed. Returns
11307 true if we actually parsed such a pragma. */
11308
11309 static bool
11310 c_parser_pragma (c_parser *parser, enum pragma_context context, bool *if_p)
11311 {
11312 unsigned int id;
11313 const char *construct = NULL;
11314
11315 id = c_parser_peek_token (parser)->pragma_kind;
11316 gcc_assert (id != PRAGMA_NONE);
11317
11318 switch (id)
11319 {
11320 case PRAGMA_OACC_DECLARE:
11321 c_parser_oacc_declare (parser);
11322 return false;
11323
11324 case PRAGMA_OACC_ENTER_DATA:
11325 if (context != pragma_compound)
11326 {
11327 construct = "acc enter data";
11328 in_compound:
11329 if (context == pragma_stmt)
11330 {
11331 error_at (c_parser_peek_token (parser)->location,
11332 "%<#pragma %s%> may only be used in compound "
11333 "statements", construct);
11334 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
11335 return false;
11336 }
11337 goto bad_stmt;
11338 }
11339 c_parser_oacc_enter_exit_data (parser, true);
11340 return false;
11341
11342 case PRAGMA_OACC_EXIT_DATA:
11343 if (context != pragma_compound)
11344 {
11345 construct = "acc exit data";
11346 goto in_compound;
11347 }
11348 c_parser_oacc_enter_exit_data (parser, false);
11349 return false;
11350
11351 case PRAGMA_OACC_ROUTINE:
11352 if (context != pragma_external)
11353 {
11354 error_at (c_parser_peek_token (parser)->location,
11355 "%<#pragma acc routine%> must be at file scope");
11356 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
11357 return false;
11358 }
11359 c_parser_oacc_routine (parser, context);
11360 return false;
11361
11362 case PRAGMA_OACC_UPDATE:
11363 if (context != pragma_compound)
11364 {
11365 construct = "acc update";
11366 goto in_compound;
11367 }
11368 c_parser_oacc_update (parser);
11369 return false;
11370
11371 case PRAGMA_OMP_BARRIER:
11372 if (context != pragma_compound)
11373 {
11374 construct = "omp barrier";
11375 goto in_compound;
11376 }
11377 c_parser_omp_barrier (parser);
11378 return false;
11379
11380 case PRAGMA_OMP_DEPOBJ:
11381 if (context != pragma_compound)
11382 {
11383 construct = "omp depobj";
11384 goto in_compound;
11385 }
11386 c_parser_omp_depobj (parser);
11387 return false;
11388
11389 case PRAGMA_OMP_FLUSH:
11390 if (context != pragma_compound)
11391 {
11392 construct = "omp flush";
11393 goto in_compound;
11394 }
11395 c_parser_omp_flush (parser);
11396 return false;
11397
11398 case PRAGMA_OMP_TASKWAIT:
11399 if (context != pragma_compound)
11400 {
11401 construct = "omp taskwait";
11402 goto in_compound;
11403 }
11404 c_parser_omp_taskwait (parser);
11405 return false;
11406
11407 case PRAGMA_OMP_TASKYIELD:
11408 if (context != pragma_compound)
11409 {
11410 construct = "omp taskyield";
11411 goto in_compound;
11412 }
11413 c_parser_omp_taskyield (parser);
11414 return false;
11415
11416 case PRAGMA_OMP_CANCEL:
11417 if (context != pragma_compound)
11418 {
11419 construct = "omp cancel";
11420 goto in_compound;
11421 }
11422 c_parser_omp_cancel (parser);
11423 return false;
11424
11425 case PRAGMA_OMP_CANCELLATION_POINT:
11426 c_parser_omp_cancellation_point (parser, context);
11427 return false;
11428
11429 case PRAGMA_OMP_THREADPRIVATE:
11430 c_parser_omp_threadprivate (parser);
11431 return false;
11432
11433 case PRAGMA_OMP_TARGET:
11434 return c_parser_omp_target (parser, context, if_p);
11435
11436 case PRAGMA_OMP_END_DECLARE_TARGET:
11437 c_parser_omp_end_declare_target (parser);
11438 return false;
11439
11440 case PRAGMA_OMP_SECTION:
11441 error_at (c_parser_peek_token (parser)->location,
11442 "%<#pragma omp section%> may only be used in "
11443 "%<#pragma omp sections%> construct");
11444 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
11445 return false;
11446
11447 case PRAGMA_OMP_DECLARE:
11448 c_parser_omp_declare (parser, context);
11449 return false;
11450
11451 case PRAGMA_OMP_REQUIRES:
11452 c_parser_omp_requires (parser);
11453 return false;
11454
11455 case PRAGMA_OMP_ORDERED:
11456 return c_parser_omp_ordered (parser, context, if_p);
11457
11458 case PRAGMA_IVDEP:
11459 {
11460 const bool ivdep = c_parse_pragma_ivdep (parser);
11461 unsigned short unroll;
11462 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_UNROLL)
11463 unroll = c_parser_pragma_unroll (parser);
11464 else
11465 unroll = 0;
11466 if (!c_parser_next_token_is_keyword (parser, RID_FOR)
11467 && !c_parser_next_token_is_keyword (parser, RID_WHILE)
11468 && !c_parser_next_token_is_keyword (parser, RID_DO))
11469 {
11470 c_parser_error (parser, "for, while or do statement expected");
11471 return false;
11472 }
11473 if (c_parser_next_token_is_keyword (parser, RID_FOR))
11474 c_parser_for_statement (parser, ivdep, unroll, if_p);
11475 else if (c_parser_next_token_is_keyword (parser, RID_WHILE))
11476 c_parser_while_statement (parser, ivdep, unroll, if_p);
11477 else
11478 c_parser_do_statement (parser, ivdep, unroll);
11479 }
11480 return false;
11481
11482 case PRAGMA_UNROLL:
11483 {
11484 unsigned short unroll = c_parser_pragma_unroll (parser);
11485 bool ivdep;
11486 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_IVDEP)
11487 ivdep = c_parse_pragma_ivdep (parser);
11488 else
11489 ivdep = false;
11490 if (!c_parser_next_token_is_keyword (parser, RID_FOR)
11491 && !c_parser_next_token_is_keyword (parser, RID_WHILE)
11492 && !c_parser_next_token_is_keyword (parser, RID_DO))
11493 {
11494 c_parser_error (parser, "for, while or do statement expected");
11495 return false;
11496 }
11497 if (c_parser_next_token_is_keyword (parser, RID_FOR))
11498 c_parser_for_statement (parser, ivdep, unroll, if_p);
11499 else if (c_parser_next_token_is_keyword (parser, RID_WHILE))
11500 c_parser_while_statement (parser, ivdep, unroll, if_p);
11501 else
11502 c_parser_do_statement (parser, ivdep, unroll);
11503 }
11504 return false;
11505
11506 case PRAGMA_GCC_PCH_PREPROCESS:
11507 c_parser_error (parser, "%<#pragma GCC pch_preprocess%> must be first");
11508 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
11509 return false;
11510
11511 case PRAGMA_OACC_WAIT:
11512 if (context != pragma_compound)
11513 {
11514 construct = "acc wait";
11515 goto in_compound;
11516 }
11517 /* FALL THROUGH. */
11518
11519 default:
11520 if (id < PRAGMA_FIRST_EXTERNAL)
11521 {
11522 if (context != pragma_stmt && context != pragma_compound)
11523 {
11524 bad_stmt:
11525 c_parser_error (parser, "expected declaration specifiers");
11526 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
11527 return false;
11528 }
11529 c_parser_omp_construct (parser, if_p);
11530 return true;
11531 }
11532 break;
11533 }
11534
11535 c_parser_consume_pragma (parser);
11536 c_invoke_pragma_handler (id);
11537
11538 /* Skip to EOL, but suppress any error message. Those will have been
11539 generated by the handler routine through calling error, as opposed
11540 to calling c_parser_error. */
11541 parser->error = true;
11542 c_parser_skip_to_pragma_eol (parser);
11543
11544 return false;
11545 }
11546
11547 /* The interface the pragma parsers have to the lexer. */
11548
11549 enum cpp_ttype
11550 pragma_lex (tree *value, location_t *loc)
11551 {
11552 c_token *tok = c_parser_peek_token (the_parser);
11553 enum cpp_ttype ret = tok->type;
11554
11555 *value = tok->value;
11556 if (loc)
11557 *loc = tok->location;
11558
11559 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
11560 ret = CPP_EOF;
11561 else
11562 {
11563 if (ret == CPP_KEYWORD)
11564 ret = CPP_NAME;
11565 c_parser_consume_token (the_parser);
11566 }
11567
11568 return ret;
11569 }
11570
11571 static void
11572 c_parser_pragma_pch_preprocess (c_parser *parser)
11573 {
11574 tree name = NULL;
11575
11576 c_parser_consume_pragma (parser);
11577 if (c_parser_next_token_is (parser, CPP_STRING))
11578 {
11579 name = c_parser_peek_token (parser)->value;
11580 c_parser_consume_token (parser);
11581 }
11582 else
11583 c_parser_error (parser, "expected string literal");
11584 c_parser_skip_to_pragma_eol (parser);
11585
11586 if (name)
11587 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
11588 }
11589 \f
11590 /* OpenACC and OpenMP parsing routines. */
11591
11592 /* Returns name of the next clause.
11593 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
11594 the token is not consumed. Otherwise appropriate pragma_omp_clause is
11595 returned and the token is consumed. */
11596
11597 static pragma_omp_clause
11598 c_parser_omp_clause_name (c_parser *parser)
11599 {
11600 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
11601
11602 if (c_parser_next_token_is_keyword (parser, RID_AUTO))
11603 result = PRAGMA_OACC_CLAUSE_AUTO;
11604 else if (c_parser_next_token_is_keyword (parser, RID_IF))
11605 result = PRAGMA_OMP_CLAUSE_IF;
11606 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
11607 result = PRAGMA_OMP_CLAUSE_DEFAULT;
11608 else if (c_parser_next_token_is_keyword (parser, RID_FOR))
11609 result = PRAGMA_OMP_CLAUSE_FOR;
11610 else if (c_parser_next_token_is (parser, CPP_NAME))
11611 {
11612 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11613
11614 switch (p[0])
11615 {
11616 case 'a':
11617 if (!strcmp ("aligned", p))
11618 result = PRAGMA_OMP_CLAUSE_ALIGNED;
11619 else if (!strcmp ("async", p))
11620 result = PRAGMA_OACC_CLAUSE_ASYNC;
11621 break;
11622 case 'c':
11623 if (!strcmp ("collapse", p))
11624 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
11625 else if (!strcmp ("copy", p))
11626 result = PRAGMA_OACC_CLAUSE_COPY;
11627 else if (!strcmp ("copyin", p))
11628 result = PRAGMA_OMP_CLAUSE_COPYIN;
11629 else if (!strcmp ("copyout", p))
11630 result = PRAGMA_OACC_CLAUSE_COPYOUT;
11631 else if (!strcmp ("copyprivate", p))
11632 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
11633 else if (!strcmp ("create", p))
11634 result = PRAGMA_OACC_CLAUSE_CREATE;
11635 break;
11636 case 'd':
11637 if (!strcmp ("defaultmap", p))
11638 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
11639 else if (!strcmp ("delete", p))
11640 result = PRAGMA_OACC_CLAUSE_DELETE;
11641 else if (!strcmp ("depend", p))
11642 result = PRAGMA_OMP_CLAUSE_DEPEND;
11643 else if (!strcmp ("device", p))
11644 result = PRAGMA_OMP_CLAUSE_DEVICE;
11645 else if (!strcmp ("deviceptr", p))
11646 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
11647 else if (!strcmp ("device_resident", p))
11648 result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
11649 else if (!strcmp ("dist_schedule", p))
11650 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
11651 break;
11652 case 'f':
11653 if (!strcmp ("final", p))
11654 result = PRAGMA_OMP_CLAUSE_FINAL;
11655 else if (!strcmp ("finalize", p))
11656 result = PRAGMA_OACC_CLAUSE_FINALIZE;
11657 else if (!strcmp ("firstprivate", p))
11658 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
11659 else if (!strcmp ("from", p))
11660 result = PRAGMA_OMP_CLAUSE_FROM;
11661 break;
11662 case 'g':
11663 if (!strcmp ("gang", p))
11664 result = PRAGMA_OACC_CLAUSE_GANG;
11665 else if (!strcmp ("grainsize", p))
11666 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
11667 break;
11668 case 'h':
11669 if (!strcmp ("hint", p))
11670 result = PRAGMA_OMP_CLAUSE_HINT;
11671 else if (!strcmp ("host", p))
11672 result = PRAGMA_OACC_CLAUSE_HOST;
11673 break;
11674 case 'i':
11675 if (!strcmp ("if_present", p))
11676 result = PRAGMA_OACC_CLAUSE_IF_PRESENT;
11677 else if (!strcmp ("in_reduction", p))
11678 result = PRAGMA_OMP_CLAUSE_IN_REDUCTION;
11679 else if (!strcmp ("inbranch", p))
11680 result = PRAGMA_OMP_CLAUSE_INBRANCH;
11681 else if (!strcmp ("independent", p))
11682 result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
11683 else if (!strcmp ("is_device_ptr", p))
11684 result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
11685 break;
11686 case 'l':
11687 if (!strcmp ("lastprivate", p))
11688 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
11689 else if (!strcmp ("linear", p))
11690 result = PRAGMA_OMP_CLAUSE_LINEAR;
11691 else if (!strcmp ("link", p))
11692 result = PRAGMA_OMP_CLAUSE_LINK;
11693 break;
11694 case 'm':
11695 if (!strcmp ("map", p))
11696 result = PRAGMA_OMP_CLAUSE_MAP;
11697 else if (!strcmp ("mergeable", p))
11698 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
11699 break;
11700 case 'n':
11701 if (!strcmp ("nogroup", p))
11702 result = PRAGMA_OMP_CLAUSE_NOGROUP;
11703 else if (!strcmp ("nontemporal", p))
11704 result = PRAGMA_OMP_CLAUSE_NONTEMPORAL;
11705 else if (!strcmp ("notinbranch", p))
11706 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
11707 else if (!strcmp ("nowait", p))
11708 result = PRAGMA_OMP_CLAUSE_NOWAIT;
11709 else if (!strcmp ("num_gangs", p))
11710 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
11711 else if (!strcmp ("num_tasks", p))
11712 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
11713 else if (!strcmp ("num_teams", p))
11714 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
11715 else if (!strcmp ("num_threads", p))
11716 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
11717 else if (!strcmp ("num_workers", p))
11718 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
11719 break;
11720 case 'o':
11721 if (!strcmp ("ordered", p))
11722 result = PRAGMA_OMP_CLAUSE_ORDERED;
11723 break;
11724 case 'p':
11725 if (!strcmp ("parallel", p))
11726 result = PRAGMA_OMP_CLAUSE_PARALLEL;
11727 else if (!strcmp ("present", p))
11728 result = PRAGMA_OACC_CLAUSE_PRESENT;
11729 /* As of OpenACC 2.5, these are now aliases of the non-present_or
11730 clauses. */
11731 else if (!strcmp ("present_or_copy", p)
11732 || !strcmp ("pcopy", p))
11733 result = PRAGMA_OACC_CLAUSE_COPY;
11734 else if (!strcmp ("present_or_copyin", p)
11735 || !strcmp ("pcopyin", p))
11736 result = PRAGMA_OACC_CLAUSE_COPYIN;
11737 else if (!strcmp ("present_or_copyout", p)
11738 || !strcmp ("pcopyout", p))
11739 result = PRAGMA_OACC_CLAUSE_COPYOUT;
11740 else if (!strcmp ("present_or_create", p)
11741 || !strcmp ("pcreate", p))
11742 result = PRAGMA_OACC_CLAUSE_CREATE;
11743 else if (!strcmp ("priority", p))
11744 result = PRAGMA_OMP_CLAUSE_PRIORITY;
11745 else if (!strcmp ("private", p))
11746 result = PRAGMA_OMP_CLAUSE_PRIVATE;
11747 else if (!strcmp ("proc_bind", p))
11748 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
11749 break;
11750 case 'r':
11751 if (!strcmp ("reduction", p))
11752 result = PRAGMA_OMP_CLAUSE_REDUCTION;
11753 break;
11754 case 's':
11755 if (!strcmp ("safelen", p))
11756 result = PRAGMA_OMP_CLAUSE_SAFELEN;
11757 else if (!strcmp ("schedule", p))
11758 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
11759 else if (!strcmp ("sections", p))
11760 result = PRAGMA_OMP_CLAUSE_SECTIONS;
11761 else if (!strcmp ("self", p)) /* "self" is a synonym for "host". */
11762 result = PRAGMA_OACC_CLAUSE_HOST;
11763 else if (!strcmp ("seq", p))
11764 result = PRAGMA_OACC_CLAUSE_SEQ;
11765 else if (!strcmp ("shared", p))
11766 result = PRAGMA_OMP_CLAUSE_SHARED;
11767 else if (!strcmp ("simd", p))
11768 result = PRAGMA_OMP_CLAUSE_SIMD;
11769 else if (!strcmp ("simdlen", p))
11770 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
11771 break;
11772 case 't':
11773 if (!strcmp ("task_reduction", p))
11774 result = PRAGMA_OMP_CLAUSE_TASK_REDUCTION;
11775 else if (!strcmp ("taskgroup", p))
11776 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
11777 else if (!strcmp ("thread_limit", p))
11778 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
11779 else if (!strcmp ("threads", p))
11780 result = PRAGMA_OMP_CLAUSE_THREADS;
11781 else if (!strcmp ("tile", p))
11782 result = PRAGMA_OACC_CLAUSE_TILE;
11783 else if (!strcmp ("to", p))
11784 result = PRAGMA_OMP_CLAUSE_TO;
11785 break;
11786 case 'u':
11787 if (!strcmp ("uniform", p))
11788 result = PRAGMA_OMP_CLAUSE_UNIFORM;
11789 else if (!strcmp ("untied", p))
11790 result = PRAGMA_OMP_CLAUSE_UNTIED;
11791 else if (!strcmp ("use_device", p))
11792 result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
11793 else if (!strcmp ("use_device_ptr", p))
11794 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
11795 break;
11796 case 'v':
11797 if (!strcmp ("vector", p))
11798 result = PRAGMA_OACC_CLAUSE_VECTOR;
11799 else if (!strcmp ("vector_length", p))
11800 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
11801 break;
11802 case 'w':
11803 if (!strcmp ("wait", p))
11804 result = PRAGMA_OACC_CLAUSE_WAIT;
11805 else if (!strcmp ("worker", p))
11806 result = PRAGMA_OACC_CLAUSE_WORKER;
11807 break;
11808 }
11809 }
11810
11811 if (result != PRAGMA_OMP_CLAUSE_NONE)
11812 c_parser_consume_token (parser);
11813
11814 return result;
11815 }
11816
11817 /* Validate that a clause of the given type does not already exist. */
11818
11819 static void
11820 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
11821 const char *name)
11822 {
11823 tree c;
11824
11825 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
11826 if (OMP_CLAUSE_CODE (c) == code)
11827 {
11828 location_t loc = OMP_CLAUSE_LOCATION (c);
11829 error_at (loc, "too many %qs clauses", name);
11830 break;
11831 }
11832 }
11833
11834 /* OpenACC 2.0
11835 Parse wait clause or wait directive parameters. */
11836
11837 static tree
11838 c_parser_oacc_wait_list (c_parser *parser, location_t clause_loc, tree list)
11839 {
11840 vec<tree, va_gc> *args;
11841 tree t, args_tree;
11842
11843 matching_parens parens;
11844 if (!parens.require_open (parser))
11845 return list;
11846
11847 args = c_parser_expr_list (parser, false, true, NULL, NULL, NULL, NULL);
11848 args_tree = build_tree_list_vec (args);
11849
11850 for (t = args_tree; t; t = TREE_CHAIN (t))
11851 {
11852 tree targ = TREE_VALUE (t);
11853
11854 if (targ != error_mark_node)
11855 {
11856 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
11857 {
11858 c_parser_error (parser, "expression must be integral");
11859 targ = error_mark_node;
11860 }
11861 else
11862 {
11863 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
11864
11865 OMP_CLAUSE_DECL (c) = targ;
11866 OMP_CLAUSE_CHAIN (c) = list;
11867 list = c;
11868 }
11869 }
11870 }
11871
11872 release_tree_vector (args);
11873 parens.require_close (parser);
11874 return list;
11875 }
11876
11877 /* OpenACC 2.0, OpenMP 2.5:
11878 variable-list:
11879 identifier
11880 variable-list , identifier
11881
11882 If KIND is nonzero, create the appropriate node and install the
11883 decl in OMP_CLAUSE_DECL and add the node to the head of the list.
11884 If KIND is nonzero, CLAUSE_LOC is the location of the clause.
11885
11886 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
11887 return the list created. */
11888
11889 static tree
11890 c_parser_omp_variable_list (c_parser *parser,
11891 location_t clause_loc,
11892 enum omp_clause_code kind, tree list)
11893 {
11894 auto_vec<c_token> tokens;
11895 unsigned int tokens_avail = 0;
11896
11897 if (kind != OMP_CLAUSE_DEPEND
11898 && (c_parser_next_token_is_not (parser, CPP_NAME)
11899 || c_parser_peek_token (parser)->id_kind != C_ID_ID))
11900 c_parser_error (parser, "expected identifier");
11901
11902 while (kind == OMP_CLAUSE_DEPEND
11903 || (c_parser_next_token_is (parser, CPP_NAME)
11904 && c_parser_peek_token (parser)->id_kind == C_ID_ID))
11905 {
11906 bool array_section_p = false;
11907 if (kind == OMP_CLAUSE_DEPEND)
11908 {
11909 if (c_parser_next_token_is_not (parser, CPP_NAME)
11910 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
11911 {
11912 struct c_expr expr = c_parser_expr_no_commas (parser, NULL);
11913 if (expr.value != error_mark_node)
11914 {
11915 tree u = build_omp_clause (clause_loc, kind);
11916 OMP_CLAUSE_DECL (u) = expr.value;
11917 OMP_CLAUSE_CHAIN (u) = list;
11918 list = u;
11919 }
11920
11921 if (c_parser_next_token_is_not (parser, CPP_COMMA))
11922 break;
11923
11924 c_parser_consume_token (parser);
11925 continue;
11926 }
11927
11928 tokens.truncate (0);
11929 unsigned int nesting_depth = 0;
11930 while (1)
11931 {
11932 c_token *token = c_parser_peek_token (parser);
11933 switch (token->type)
11934 {
11935 case CPP_EOF:
11936 case CPP_PRAGMA_EOL:
11937 break;
11938 case CPP_OPEN_BRACE:
11939 case CPP_OPEN_PAREN:
11940 case CPP_OPEN_SQUARE:
11941 ++nesting_depth;
11942 goto add;
11943 case CPP_CLOSE_BRACE:
11944 case CPP_CLOSE_PAREN:
11945 case CPP_CLOSE_SQUARE:
11946 if (nesting_depth-- == 0)
11947 break;
11948 goto add;
11949 case CPP_COMMA:
11950 if (nesting_depth == 0)
11951 break;
11952 goto add;
11953 default:
11954 add:
11955 tokens.safe_push (*token);
11956 c_parser_consume_token (parser);
11957 continue;
11958 }
11959 break;
11960 }
11961
11962 /* Make sure nothing tries to read past the end of the tokens. */
11963 c_token eof_token;
11964 memset (&eof_token, 0, sizeof (eof_token));
11965 eof_token.type = CPP_EOF;
11966 tokens.safe_push (eof_token);
11967 tokens.safe_push (eof_token);
11968
11969 tokens_avail = parser->tokens_avail;
11970 gcc_assert (parser->tokens == &parser->tokens_buf[0]);
11971 parser->tokens = tokens.address ();
11972 parser->tokens_avail = tokens.length ();
11973 }
11974
11975 tree t = lookup_name (c_parser_peek_token (parser)->value);
11976
11977 if (t == NULL_TREE)
11978 {
11979 undeclared_variable (c_parser_peek_token (parser)->location,
11980 c_parser_peek_token (parser)->value);
11981 t = error_mark_node;
11982 }
11983
11984 c_parser_consume_token (parser);
11985
11986 if (t == error_mark_node)
11987 ;
11988 else if (kind != 0)
11989 {
11990 switch (kind)
11991 {
11992 case OMP_CLAUSE__CACHE_:
11993 /* The OpenACC cache directive explicitly only allows "array
11994 elements or subarrays". */
11995 if (c_parser_peek_token (parser)->type != CPP_OPEN_SQUARE)
11996 {
11997 c_parser_error (parser, "expected %<[%>");
11998 t = error_mark_node;
11999 break;
12000 }
12001 /* FALLTHROUGH */
12002 case OMP_CLAUSE_MAP:
12003 case OMP_CLAUSE_FROM:
12004 case OMP_CLAUSE_TO:
12005 while (c_parser_next_token_is (parser, CPP_DOT))
12006 {
12007 location_t op_loc = c_parser_peek_token (parser)->location;
12008 c_parser_consume_token (parser);
12009 if (!c_parser_next_token_is (parser, CPP_NAME))
12010 {
12011 c_parser_error (parser, "expected identifier");
12012 t = error_mark_node;
12013 break;
12014 }
12015
12016 c_token *comp_tok = c_parser_peek_token (parser);
12017 tree ident = comp_tok->value;
12018 location_t comp_loc = comp_tok->location;
12019 c_parser_consume_token (parser);
12020 t = build_component_ref (op_loc, t, ident, comp_loc);
12021 }
12022 /* FALLTHROUGH */
12023 case OMP_CLAUSE_DEPEND:
12024 case OMP_CLAUSE_REDUCTION:
12025 case OMP_CLAUSE_IN_REDUCTION:
12026 case OMP_CLAUSE_TASK_REDUCTION:
12027 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
12028 {
12029 tree low_bound = NULL_TREE, length = NULL_TREE;
12030
12031 c_parser_consume_token (parser);
12032 if (!c_parser_next_token_is (parser, CPP_COLON))
12033 {
12034 location_t expr_loc
12035 = c_parser_peek_token (parser)->location;
12036 c_expr expr = c_parser_expression (parser);
12037 expr = convert_lvalue_to_rvalue (expr_loc, expr,
12038 false, true);
12039 low_bound = expr.value;
12040 }
12041 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
12042 length = integer_one_node;
12043 else
12044 {
12045 /* Look for `:'. */
12046 if (!c_parser_require (parser, CPP_COLON,
12047 "expected %<:%>"))
12048 {
12049 t = error_mark_node;
12050 break;
12051 }
12052 array_section_p = true;
12053 if (!c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
12054 {
12055 location_t expr_loc
12056 = c_parser_peek_token (parser)->location;
12057 c_expr expr = c_parser_expression (parser);
12058 expr = convert_lvalue_to_rvalue (expr_loc, expr,
12059 false, true);
12060 length = expr.value;
12061 }
12062 }
12063 /* Look for the closing `]'. */
12064 if (!c_parser_require (parser, CPP_CLOSE_SQUARE,
12065 "expected %<]%>"))
12066 {
12067 t = error_mark_node;
12068 break;
12069 }
12070
12071 t = tree_cons (low_bound, length, t);
12072 }
12073 if (kind == OMP_CLAUSE_DEPEND
12074 && t != error_mark_node
12075 && parser->tokens_avail != 2)
12076 {
12077 if (array_section_p)
12078 {
12079 error_at (c_parser_peek_token (parser)->location,
12080 "expected %<)%> or %<,%>");
12081 t = error_mark_node;
12082 }
12083 else
12084 {
12085 parser->tokens = tokens.address ();
12086 parser->tokens_avail = tokens.length ();
12087
12088 t = c_parser_expr_no_commas (parser, NULL).value;
12089 if (t != error_mark_node && parser->tokens_avail != 2)
12090 {
12091 error_at (c_parser_peek_token (parser)->location,
12092 "expected %<)%> or %<,%>");
12093 t = error_mark_node;
12094 }
12095 }
12096 }
12097 break;
12098 default:
12099 break;
12100 }
12101
12102 if (t != error_mark_node)
12103 {
12104 tree u = build_omp_clause (clause_loc, kind);
12105 OMP_CLAUSE_DECL (u) = t;
12106 OMP_CLAUSE_CHAIN (u) = list;
12107 list = u;
12108 }
12109 }
12110 else
12111 list = tree_cons (t, NULL_TREE, list);
12112
12113 if (kind == OMP_CLAUSE_DEPEND)
12114 {
12115 parser->tokens = &parser->tokens_buf[0];
12116 parser->tokens_avail = tokens_avail;
12117 }
12118 if (c_parser_next_token_is_not (parser, CPP_COMMA))
12119 break;
12120
12121 c_parser_consume_token (parser);
12122 }
12123
12124 return list;
12125 }
12126
12127 /* Similarly, but expect leading and trailing parenthesis. This is a very
12128 common case for OpenACC and OpenMP clauses. */
12129
12130 static tree
12131 c_parser_omp_var_list_parens (c_parser *parser, enum omp_clause_code kind,
12132 tree list)
12133 {
12134 /* The clauses location. */
12135 location_t loc = c_parser_peek_token (parser)->location;
12136
12137 matching_parens parens;
12138 if (parens.require_open (parser))
12139 {
12140 list = c_parser_omp_variable_list (parser, loc, kind, list);
12141 parens.skip_until_found_close (parser);
12142 }
12143 return list;
12144 }
12145
12146 /* OpenACC 2.0:
12147 copy ( variable-list )
12148 copyin ( variable-list )
12149 copyout ( variable-list )
12150 create ( variable-list )
12151 delete ( variable-list )
12152 present ( variable-list ) */
12153
12154 static tree
12155 c_parser_oacc_data_clause (c_parser *parser, pragma_omp_clause c_kind,
12156 tree list)
12157 {
12158 enum gomp_map_kind kind;
12159 switch (c_kind)
12160 {
12161 case PRAGMA_OACC_CLAUSE_COPY:
12162 kind = GOMP_MAP_TOFROM;
12163 break;
12164 case PRAGMA_OACC_CLAUSE_COPYIN:
12165 kind = GOMP_MAP_TO;
12166 break;
12167 case PRAGMA_OACC_CLAUSE_COPYOUT:
12168 kind = GOMP_MAP_FROM;
12169 break;
12170 case PRAGMA_OACC_CLAUSE_CREATE:
12171 kind = GOMP_MAP_ALLOC;
12172 break;
12173 case PRAGMA_OACC_CLAUSE_DELETE:
12174 kind = GOMP_MAP_RELEASE;
12175 break;
12176 case PRAGMA_OACC_CLAUSE_DEVICE:
12177 kind = GOMP_MAP_FORCE_TO;
12178 break;
12179 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
12180 kind = GOMP_MAP_DEVICE_RESIDENT;
12181 break;
12182 case PRAGMA_OACC_CLAUSE_HOST:
12183 kind = GOMP_MAP_FORCE_FROM;
12184 break;
12185 case PRAGMA_OACC_CLAUSE_LINK:
12186 kind = GOMP_MAP_LINK;
12187 break;
12188 case PRAGMA_OACC_CLAUSE_PRESENT:
12189 kind = GOMP_MAP_FORCE_PRESENT;
12190 break;
12191 default:
12192 gcc_unreachable ();
12193 }
12194 tree nl, c;
12195 nl = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_MAP, list);
12196
12197 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
12198 OMP_CLAUSE_SET_MAP_KIND (c, kind);
12199
12200 return nl;
12201 }
12202
12203 /* OpenACC 2.0:
12204 deviceptr ( variable-list ) */
12205
12206 static tree
12207 c_parser_oacc_data_clause_deviceptr (c_parser *parser, tree list)
12208 {
12209 location_t loc = c_parser_peek_token (parser)->location;
12210 tree vars, t;
12211
12212 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
12213 c_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
12214 variable-list must only allow for pointer variables. */
12215 vars = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
12216 for (t = vars; t && t; t = TREE_CHAIN (t))
12217 {
12218 tree v = TREE_PURPOSE (t);
12219
12220 /* FIXME diagnostics: Ideally we should keep individual
12221 locations for all the variables in the var list to make the
12222 following errors more precise. Perhaps
12223 c_parser_omp_var_list_parens() should construct a list of
12224 locations to go along with the var list. */
12225
12226 if (!VAR_P (v) && TREE_CODE (v) != PARM_DECL)
12227 error_at (loc, "%qD is not a variable", v);
12228 else if (TREE_TYPE (v) == error_mark_node)
12229 ;
12230 else if (!POINTER_TYPE_P (TREE_TYPE (v)))
12231 error_at (loc, "%qD is not a pointer variable", v);
12232
12233 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
12234 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
12235 OMP_CLAUSE_DECL (u) = v;
12236 OMP_CLAUSE_CHAIN (u) = list;
12237 list = u;
12238 }
12239
12240 return list;
12241 }
12242
12243 /* OpenACC 2.0, OpenMP 3.0:
12244 collapse ( constant-expression ) */
12245
12246 static tree
12247 c_parser_omp_clause_collapse (c_parser *parser, tree list)
12248 {
12249 tree c, num = error_mark_node;
12250 HOST_WIDE_INT n;
12251 location_t loc;
12252
12253 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse");
12254 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile");
12255
12256 loc = c_parser_peek_token (parser)->location;
12257 matching_parens parens;
12258 if (parens.require_open (parser))
12259 {
12260 num = c_parser_expr_no_commas (parser, NULL).value;
12261 parens.skip_until_found_close (parser);
12262 }
12263 if (num == error_mark_node)
12264 return list;
12265 mark_exp_read (num);
12266 num = c_fully_fold (num, false, NULL);
12267 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
12268 || !tree_fits_shwi_p (num)
12269 || (n = tree_to_shwi (num)) <= 0
12270 || (int) n != n)
12271 {
12272 error_at (loc,
12273 "collapse argument needs positive constant integer expression");
12274 return list;
12275 }
12276 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
12277 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
12278 OMP_CLAUSE_CHAIN (c) = list;
12279 return c;
12280 }
12281
12282 /* OpenMP 2.5:
12283 copyin ( variable-list ) */
12284
12285 static tree
12286 c_parser_omp_clause_copyin (c_parser *parser, tree list)
12287 {
12288 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYIN, list);
12289 }
12290
12291 /* OpenMP 2.5:
12292 copyprivate ( variable-list ) */
12293
12294 static tree
12295 c_parser_omp_clause_copyprivate (c_parser *parser, tree list)
12296 {
12297 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYPRIVATE, list);
12298 }
12299
12300 /* OpenMP 2.5:
12301 default ( none | shared )
12302
12303 OpenACC:
12304 default ( none | present ) */
12305
12306 static tree
12307 c_parser_omp_clause_default (c_parser *parser, tree list, bool is_oacc)
12308 {
12309 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
12310 location_t loc = c_parser_peek_token (parser)->location;
12311 tree c;
12312
12313 matching_parens parens;
12314 if (!parens.require_open (parser))
12315 return list;
12316 if (c_parser_next_token_is (parser, CPP_NAME))
12317 {
12318 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12319
12320 switch (p[0])
12321 {
12322 case 'n':
12323 if (strcmp ("none", p) != 0)
12324 goto invalid_kind;
12325 kind = OMP_CLAUSE_DEFAULT_NONE;
12326 break;
12327
12328 case 'p':
12329 if (strcmp ("present", p) != 0 || !is_oacc)
12330 goto invalid_kind;
12331 kind = OMP_CLAUSE_DEFAULT_PRESENT;
12332 break;
12333
12334 case 's':
12335 if (strcmp ("shared", p) != 0 || is_oacc)
12336 goto invalid_kind;
12337 kind = OMP_CLAUSE_DEFAULT_SHARED;
12338 break;
12339
12340 default:
12341 goto invalid_kind;
12342 }
12343
12344 c_parser_consume_token (parser);
12345 }
12346 else
12347 {
12348 invalid_kind:
12349 if (is_oacc)
12350 c_parser_error (parser, "expected %<none%> or %<present%>");
12351 else
12352 c_parser_error (parser, "expected %<none%> or %<shared%>");
12353 }
12354 parens.skip_until_found_close (parser);
12355
12356 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
12357 return list;
12358
12359 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
12360 c = build_omp_clause (loc, OMP_CLAUSE_DEFAULT);
12361 OMP_CLAUSE_CHAIN (c) = list;
12362 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
12363
12364 return c;
12365 }
12366
12367 /* OpenMP 2.5:
12368 firstprivate ( variable-list ) */
12369
12370 static tree
12371 c_parser_omp_clause_firstprivate (c_parser *parser, tree list)
12372 {
12373 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_FIRSTPRIVATE, list);
12374 }
12375
12376 /* OpenMP 3.1:
12377 final ( expression ) */
12378
12379 static tree
12380 c_parser_omp_clause_final (c_parser *parser, tree list)
12381 {
12382 location_t loc = c_parser_peek_token (parser)->location;
12383 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
12384 {
12385 matching_parens parens;
12386 tree t, c;
12387 if (!parens.require_open (parser))
12388 t = error_mark_node;
12389 else
12390 {
12391 location_t eloc = c_parser_peek_token (parser)->location;
12392 c_expr expr = c_parser_expr_no_commas (parser, NULL);
12393 t = convert_lvalue_to_rvalue (eloc, expr, true, true).value;
12394 t = c_objc_common_truthvalue_conversion (eloc, t);
12395 t = c_fully_fold (t, false, NULL);
12396 parens.skip_until_found_close (parser);
12397 }
12398
12399 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final");
12400
12401 c = build_omp_clause (loc, OMP_CLAUSE_FINAL);
12402 OMP_CLAUSE_FINAL_EXPR (c) = t;
12403 OMP_CLAUSE_CHAIN (c) = list;
12404 list = c;
12405 }
12406 else
12407 c_parser_error (parser, "expected %<(%>");
12408
12409 return list;
12410 }
12411
12412 /* OpenACC, OpenMP 2.5:
12413 if ( expression )
12414
12415 OpenMP 4.5:
12416 if ( directive-name-modifier : expression )
12417
12418 directive-name-modifier:
12419 parallel | task | taskloop | target data | target | target update
12420 | target enter data | target exit data
12421
12422 OpenMP 5.0:
12423 directive-name-modifier:
12424 ... | simd | cancel */
12425
12426 static tree
12427 c_parser_omp_clause_if (c_parser *parser, tree list, bool is_omp)
12428 {
12429 location_t location = c_parser_peek_token (parser)->location;
12430 enum tree_code if_modifier = ERROR_MARK;
12431
12432 matching_parens parens;
12433 if (!parens.require_open (parser))
12434 return list;
12435
12436 if (is_omp && c_parser_next_token_is (parser, CPP_NAME))
12437 {
12438 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12439 int n = 2;
12440 if (strcmp (p, "cancel") == 0)
12441 if_modifier = VOID_CST;
12442 else if (strcmp (p, "parallel") == 0)
12443 if_modifier = OMP_PARALLEL;
12444 else if (strcmp (p, "simd") == 0)
12445 if_modifier = OMP_SIMD;
12446 else if (strcmp (p, "task") == 0)
12447 if_modifier = OMP_TASK;
12448 else if (strcmp (p, "taskloop") == 0)
12449 if_modifier = OMP_TASKLOOP;
12450 else if (strcmp (p, "target") == 0)
12451 {
12452 if_modifier = OMP_TARGET;
12453 if (c_parser_peek_2nd_token (parser)->type == CPP_NAME)
12454 {
12455 p = IDENTIFIER_POINTER (c_parser_peek_2nd_token (parser)->value);
12456 if (strcmp ("data", p) == 0)
12457 if_modifier = OMP_TARGET_DATA;
12458 else if (strcmp ("update", p) == 0)
12459 if_modifier = OMP_TARGET_UPDATE;
12460 else if (strcmp ("enter", p) == 0)
12461 if_modifier = OMP_TARGET_ENTER_DATA;
12462 else if (strcmp ("exit", p) == 0)
12463 if_modifier = OMP_TARGET_EXIT_DATA;
12464 if (if_modifier != OMP_TARGET)
12465 {
12466 n = 3;
12467 c_parser_consume_token (parser);
12468 }
12469 else
12470 {
12471 location_t loc = c_parser_peek_2nd_token (parser)->location;
12472 error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
12473 "or %<exit%>");
12474 if_modifier = ERROR_MARK;
12475 }
12476 if (if_modifier == OMP_TARGET_ENTER_DATA
12477 || if_modifier == OMP_TARGET_EXIT_DATA)
12478 {
12479 if (c_parser_peek_2nd_token (parser)->type == CPP_NAME)
12480 {
12481 p = IDENTIFIER_POINTER
12482 (c_parser_peek_2nd_token (parser)->value);
12483 if (strcmp ("data", p) == 0)
12484 n = 4;
12485 }
12486 if (n == 4)
12487 c_parser_consume_token (parser);
12488 else
12489 {
12490 location_t loc
12491 = c_parser_peek_2nd_token (parser)->location;
12492 error_at (loc, "expected %<data%>");
12493 if_modifier = ERROR_MARK;
12494 }
12495 }
12496 }
12497 }
12498 if (if_modifier != ERROR_MARK)
12499 {
12500 if (c_parser_peek_2nd_token (parser)->type == CPP_COLON)
12501 {
12502 c_parser_consume_token (parser);
12503 c_parser_consume_token (parser);
12504 }
12505 else
12506 {
12507 if (n > 2)
12508 {
12509 location_t loc = c_parser_peek_2nd_token (parser)->location;
12510 error_at (loc, "expected %<:%>");
12511 }
12512 if_modifier = ERROR_MARK;
12513 }
12514 }
12515 }
12516
12517 location_t loc = c_parser_peek_token (parser)->location;
12518 c_expr expr = c_parser_expr_no_commas (parser, NULL);
12519 expr = convert_lvalue_to_rvalue (loc, expr, true, true);
12520 tree t = c_objc_common_truthvalue_conversion (loc, expr.value), c;
12521 t = c_fully_fold (t, false, NULL);
12522 parens.skip_until_found_close (parser);
12523
12524 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
12525 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
12526 {
12527 if (if_modifier != ERROR_MARK
12528 && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
12529 {
12530 const char *p = NULL;
12531 switch (if_modifier)
12532 {
12533 case VOID_CST: p = "cancel"; break;
12534 case OMP_PARALLEL: p = "parallel"; break;
12535 case OMP_SIMD: p = "simd"; break;
12536 case OMP_TASK: p = "task"; break;
12537 case OMP_TASKLOOP: p = "taskloop"; break;
12538 case OMP_TARGET_DATA: p = "target data"; break;
12539 case OMP_TARGET: p = "target"; break;
12540 case OMP_TARGET_UPDATE: p = "target update"; break;
12541 case OMP_TARGET_ENTER_DATA: p = "enter data"; break;
12542 case OMP_TARGET_EXIT_DATA: p = "exit data"; break;
12543 default: gcc_unreachable ();
12544 }
12545 error_at (location, "too many %<if%> clauses with %qs modifier",
12546 p);
12547 return list;
12548 }
12549 else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
12550 {
12551 if (!is_omp)
12552 error_at (location, "too many %<if%> clauses");
12553 else
12554 error_at (location, "too many %<if%> clauses without modifier");
12555 return list;
12556 }
12557 else if (if_modifier == ERROR_MARK
12558 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
12559 {
12560 error_at (location, "if any %<if%> clause has modifier, then all "
12561 "%<if%> clauses have to use modifier");
12562 return list;
12563 }
12564 }
12565
12566 c = build_omp_clause (location, OMP_CLAUSE_IF);
12567 OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
12568 OMP_CLAUSE_IF_EXPR (c) = t;
12569 OMP_CLAUSE_CHAIN (c) = list;
12570 return c;
12571 }
12572
12573 /* OpenMP 2.5:
12574 lastprivate ( variable-list )
12575
12576 OpenMP 5.0:
12577 lastprivate ( [ lastprivate-modifier : ] variable-list ) */
12578
12579 static tree
12580 c_parser_omp_clause_lastprivate (c_parser *parser, tree list)
12581 {
12582 /* The clauses location. */
12583 location_t loc = c_parser_peek_token (parser)->location;
12584
12585 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12586 {
12587 bool conditional = false;
12588 if (c_parser_next_token_is (parser, CPP_NAME)
12589 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
12590 {
12591 const char *p
12592 = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12593 if (strcmp (p, "conditional") == 0)
12594 {
12595 conditional = true;
12596 c_parser_consume_token (parser);
12597 c_parser_consume_token (parser);
12598 }
12599 }
12600 tree nlist = c_parser_omp_variable_list (parser, loc,
12601 OMP_CLAUSE_LASTPRIVATE, list);
12602 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12603 if (conditional)
12604 for (tree c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
12605 OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c) = 1;
12606 return nlist;
12607 }
12608 return list;
12609 }
12610
12611 /* OpenMP 3.1:
12612 mergeable */
12613
12614 static tree
12615 c_parser_omp_clause_mergeable (c_parser *parser ATTRIBUTE_UNUSED, tree list)
12616 {
12617 tree c;
12618
12619 /* FIXME: Should we allow duplicates? */
12620 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable");
12621
12622 c = build_omp_clause (c_parser_peek_token (parser)->location,
12623 OMP_CLAUSE_MERGEABLE);
12624 OMP_CLAUSE_CHAIN (c) = list;
12625
12626 return c;
12627 }
12628
12629 /* OpenMP 2.5:
12630 nowait */
12631
12632 static tree
12633 c_parser_omp_clause_nowait (c_parser *parser ATTRIBUTE_UNUSED, tree list)
12634 {
12635 tree c;
12636 location_t loc = c_parser_peek_token (parser)->location;
12637
12638 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
12639
12640 c = build_omp_clause (loc, OMP_CLAUSE_NOWAIT);
12641 OMP_CLAUSE_CHAIN (c) = list;
12642 return c;
12643 }
12644
12645 /* OpenMP 2.5:
12646 num_threads ( expression ) */
12647
12648 static tree
12649 c_parser_omp_clause_num_threads (c_parser *parser, tree list)
12650 {
12651 location_t num_threads_loc = c_parser_peek_token (parser)->location;
12652 matching_parens parens;
12653 if (parens.require_open (parser))
12654 {
12655 location_t expr_loc = c_parser_peek_token (parser)->location;
12656 c_expr expr = c_parser_expr_no_commas (parser, NULL);
12657 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
12658 tree c, t = expr.value;
12659 t = c_fully_fold (t, false, NULL);
12660
12661 parens.skip_until_found_close (parser);
12662
12663 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
12664 {
12665 c_parser_error (parser, "expected integer expression");
12666 return list;
12667 }
12668
12669 /* Attempt to statically determine when the number isn't positive. */
12670 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
12671 build_int_cst (TREE_TYPE (t), 0));
12672 protected_set_expr_location (c, expr_loc);
12673 if (c == boolean_true_node)
12674 {
12675 warning_at (expr_loc, 0,
12676 "%<num_threads%> value must be positive");
12677 t = integer_one_node;
12678 }
12679
12680 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
12681
12682 c = build_omp_clause (num_threads_loc, OMP_CLAUSE_NUM_THREADS);
12683 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
12684 OMP_CLAUSE_CHAIN (c) = list;
12685 list = c;
12686 }
12687
12688 return list;
12689 }
12690
12691 /* OpenMP 4.5:
12692 num_tasks ( expression ) */
12693
12694 static tree
12695 c_parser_omp_clause_num_tasks (c_parser *parser, tree list)
12696 {
12697 location_t num_tasks_loc = c_parser_peek_token (parser)->location;
12698 matching_parens parens;
12699 if (parens.require_open (parser))
12700 {
12701 location_t expr_loc = c_parser_peek_token (parser)->location;
12702 c_expr expr = c_parser_expr_no_commas (parser, NULL);
12703 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
12704 tree c, t = expr.value;
12705 t = c_fully_fold (t, false, NULL);
12706
12707 parens.skip_until_found_close (parser);
12708
12709 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
12710 {
12711 c_parser_error (parser, "expected integer expression");
12712 return list;
12713 }
12714
12715 /* Attempt to statically determine when the number isn't positive. */
12716 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
12717 build_int_cst (TREE_TYPE (t), 0));
12718 if (CAN_HAVE_LOCATION_P (c))
12719 SET_EXPR_LOCATION (c, expr_loc);
12720 if (c == boolean_true_node)
12721 {
12722 warning_at (expr_loc, 0, "%<num_tasks%> value must be positive");
12723 t = integer_one_node;
12724 }
12725
12726 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS, "num_tasks");
12727
12728 c = build_omp_clause (num_tasks_loc, OMP_CLAUSE_NUM_TASKS);
12729 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
12730 OMP_CLAUSE_CHAIN (c) = list;
12731 list = c;
12732 }
12733
12734 return list;
12735 }
12736
12737 /* OpenMP 4.5:
12738 grainsize ( expression ) */
12739
12740 static tree
12741 c_parser_omp_clause_grainsize (c_parser *parser, tree list)
12742 {
12743 location_t grainsize_loc = c_parser_peek_token (parser)->location;
12744 matching_parens parens;
12745 if (parens.require_open (parser))
12746 {
12747 location_t expr_loc = c_parser_peek_token (parser)->location;
12748 c_expr expr = c_parser_expr_no_commas (parser, NULL);
12749 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
12750 tree c, t = expr.value;
12751 t = c_fully_fold (t, false, NULL);
12752
12753 parens.skip_until_found_close (parser);
12754
12755 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
12756 {
12757 c_parser_error (parser, "expected integer expression");
12758 return list;
12759 }
12760
12761 /* Attempt to statically determine when the number isn't positive. */
12762 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
12763 build_int_cst (TREE_TYPE (t), 0));
12764 if (CAN_HAVE_LOCATION_P (c))
12765 SET_EXPR_LOCATION (c, expr_loc);
12766 if (c == boolean_true_node)
12767 {
12768 warning_at (expr_loc, 0, "%<grainsize%> value must be positive");
12769 t = integer_one_node;
12770 }
12771
12772 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE, "grainsize");
12773
12774 c = build_omp_clause (grainsize_loc, OMP_CLAUSE_GRAINSIZE);
12775 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
12776 OMP_CLAUSE_CHAIN (c) = list;
12777 list = c;
12778 }
12779
12780 return list;
12781 }
12782
12783 /* OpenMP 4.5:
12784 priority ( expression ) */
12785
12786 static tree
12787 c_parser_omp_clause_priority (c_parser *parser, tree list)
12788 {
12789 location_t priority_loc = c_parser_peek_token (parser)->location;
12790 matching_parens parens;
12791 if (parens.require_open (parser))
12792 {
12793 location_t expr_loc = c_parser_peek_token (parser)->location;
12794 c_expr expr = c_parser_expr_no_commas (parser, NULL);
12795 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
12796 tree c, t = expr.value;
12797 t = c_fully_fold (t, false, NULL);
12798
12799 parens.skip_until_found_close (parser);
12800
12801 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
12802 {
12803 c_parser_error (parser, "expected integer expression");
12804 return list;
12805 }
12806
12807 /* Attempt to statically determine when the number isn't
12808 non-negative. */
12809 c = fold_build2_loc (expr_loc, LT_EXPR, boolean_type_node, t,
12810 build_int_cst (TREE_TYPE (t), 0));
12811 if (CAN_HAVE_LOCATION_P (c))
12812 SET_EXPR_LOCATION (c, expr_loc);
12813 if (c == boolean_true_node)
12814 {
12815 warning_at (expr_loc, 0, "%<priority%> value must be non-negative");
12816 t = integer_one_node;
12817 }
12818
12819 check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY, "priority");
12820
12821 c = build_omp_clause (priority_loc, OMP_CLAUSE_PRIORITY);
12822 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
12823 OMP_CLAUSE_CHAIN (c) = list;
12824 list = c;
12825 }
12826
12827 return list;
12828 }
12829
12830 /* OpenMP 4.5:
12831 hint ( expression ) */
12832
12833 static tree
12834 c_parser_omp_clause_hint (c_parser *parser, tree list)
12835 {
12836 location_t hint_loc = c_parser_peek_token (parser)->location;
12837 matching_parens parens;
12838 if (parens.require_open (parser))
12839 {
12840 location_t expr_loc = c_parser_peek_token (parser)->location;
12841 c_expr expr = c_parser_expr_no_commas (parser, NULL);
12842 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
12843 tree c, t = expr.value;
12844 t = c_fully_fold (t, false, NULL);
12845
12846 parens.skip_until_found_close (parser);
12847
12848 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
12849 || TREE_CODE (t) != INTEGER_CST)
12850 {
12851 c_parser_error (parser, "expected constant integer expression");
12852 return list;
12853 }
12854
12855 check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint");
12856
12857 c = build_omp_clause (hint_loc, OMP_CLAUSE_HINT);
12858 OMP_CLAUSE_HINT_EXPR (c) = t;
12859 OMP_CLAUSE_CHAIN (c) = list;
12860 list = c;
12861 }
12862
12863 return list;
12864 }
12865
12866 /* OpenMP 4.5:
12867 defaultmap ( tofrom : scalar )
12868
12869 OpenMP 5.0:
12870 defaultmap ( implicit-behavior [ : variable-category ] ) */
12871
12872 static tree
12873 c_parser_omp_clause_defaultmap (c_parser *parser, tree list)
12874 {
12875 location_t loc = c_parser_peek_token (parser)->location;
12876 tree c;
12877 const char *p;
12878 enum omp_clause_defaultmap_kind behavior = OMP_CLAUSE_DEFAULTMAP_DEFAULT;
12879 enum omp_clause_defaultmap_kind category
12880 = OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED;
12881
12882 matching_parens parens;
12883 if (!parens.require_open (parser))
12884 return list;
12885 if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
12886 p = "default";
12887 else if (!c_parser_next_token_is (parser, CPP_NAME))
12888 {
12889 invalid_behavior:
12890 c_parser_error (parser, "expected %<alloc%>, %<to%>, %<from%>, "
12891 "%<tofrom%>, %<firstprivate%>, %<none%> "
12892 "or %<default%>");
12893 goto out_err;
12894 }
12895 else
12896 p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12897
12898 switch (p[0])
12899 {
12900 case 'a':
12901 if (strcmp ("alloc", p) == 0)
12902 behavior = OMP_CLAUSE_DEFAULTMAP_ALLOC;
12903 else
12904 goto invalid_behavior;
12905 break;
12906
12907 case 'd':
12908 if (strcmp ("default", p) == 0)
12909 behavior = OMP_CLAUSE_DEFAULTMAP_DEFAULT;
12910 else
12911 goto invalid_behavior;
12912 break;
12913
12914 case 'f':
12915 if (strcmp ("firstprivate", p) == 0)
12916 behavior = OMP_CLAUSE_DEFAULTMAP_FIRSTPRIVATE;
12917 else if (strcmp ("from", p) == 0)
12918 behavior = OMP_CLAUSE_DEFAULTMAP_FROM;
12919 else
12920 goto invalid_behavior;
12921 break;
12922
12923 case 'n':
12924 if (strcmp ("none", p) == 0)
12925 behavior = OMP_CLAUSE_DEFAULTMAP_NONE;
12926 else
12927 goto invalid_behavior;
12928 break;
12929
12930 case 't':
12931 if (strcmp ("tofrom", p) == 0)
12932 behavior = OMP_CLAUSE_DEFAULTMAP_TOFROM;
12933 else if (strcmp ("to", p) == 0)
12934 behavior = OMP_CLAUSE_DEFAULTMAP_TO;
12935 else
12936 goto invalid_behavior;
12937 break;
12938
12939 default:
12940 goto invalid_behavior;
12941 }
12942 c_parser_consume_token (parser);
12943
12944 if (!c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
12945 {
12946 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
12947 goto out_err;
12948 if (!c_parser_next_token_is (parser, CPP_NAME))
12949 {
12950 invalid_category:
12951 c_parser_error (parser, "expected %<scalar%>, %<aggregate%> or "
12952 "%<pointer%>");
12953 goto out_err;
12954 }
12955 p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12956 switch (p[0])
12957 {
12958 case 'a':
12959 if (strcmp ("aggregate", p) == 0)
12960 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE;
12961 else
12962 goto invalid_category;
12963 break;
12964
12965 case 'p':
12966 if (strcmp ("pointer", p) == 0)
12967 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER;
12968 else
12969 goto invalid_category;
12970 break;
12971
12972 case 's':
12973 if (strcmp ("scalar", p) == 0)
12974 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR;
12975 else
12976 goto invalid_category;
12977 break;
12978
12979 default:
12980 goto invalid_category;
12981 }
12982
12983 c_parser_consume_token (parser);
12984 }
12985 parens.skip_until_found_close (parser);
12986
12987 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
12988 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEFAULTMAP
12989 && (category == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
12990 || OMP_CLAUSE_DEFAULTMAP_CATEGORY (c) == category
12991 || (OMP_CLAUSE_DEFAULTMAP_CATEGORY (c)
12992 == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED)))
12993 {
12994 enum omp_clause_defaultmap_kind cat = category;
12995 location_t loc = OMP_CLAUSE_LOCATION (c);
12996 if (cat == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED)
12997 cat = OMP_CLAUSE_DEFAULTMAP_CATEGORY (c);
12998 p = NULL;
12999 switch (cat)
13000 {
13001 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED:
13002 p = NULL;
13003 break;
13004 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE:
13005 p = "aggregate";
13006 break;
13007 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER:
13008 p = "pointer";
13009 break;
13010 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR:
13011 p = "scalar";
13012 break;
13013 default:
13014 gcc_unreachable ();
13015 }
13016 if (p)
13017 error_at (loc, "too many %<defaultmap%> clauses with %qs category",
13018 p);
13019 else
13020 error_at (loc, "too many %<defaultmap%> clauses with unspecified "
13021 "category");
13022 break;
13023 }
13024
13025 c = build_omp_clause (loc, OMP_CLAUSE_DEFAULTMAP);
13026 OMP_CLAUSE_DEFAULTMAP_SET_KIND (c, behavior, category);
13027 OMP_CLAUSE_CHAIN (c) = list;
13028 return c;
13029
13030 out_err:
13031 parens.skip_until_found_close (parser);
13032 return list;
13033 }
13034
13035 /* OpenACC 2.0:
13036 use_device ( variable-list )
13037
13038 OpenMP 4.5:
13039 use_device_ptr ( variable-list ) */
13040
13041 static tree
13042 c_parser_omp_clause_use_device_ptr (c_parser *parser, tree list)
13043 {
13044 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_USE_DEVICE_PTR,
13045 list);
13046 }
13047
13048 /* OpenMP 4.5:
13049 is_device_ptr ( variable-list ) */
13050
13051 static tree
13052 c_parser_omp_clause_is_device_ptr (c_parser *parser, tree list)
13053 {
13054 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_IS_DEVICE_PTR, list);
13055 }
13056
13057 /* OpenACC:
13058 num_gangs ( expression )
13059 num_workers ( expression )
13060 vector_length ( expression ) */
13061
13062 static tree
13063 c_parser_oacc_single_int_clause (c_parser *parser, omp_clause_code code,
13064 tree list)
13065 {
13066 location_t loc = c_parser_peek_token (parser)->location;
13067
13068 matching_parens parens;
13069 if (!parens.require_open (parser))
13070 return list;
13071
13072 location_t expr_loc = c_parser_peek_token (parser)->location;
13073 c_expr expr = c_parser_expression (parser);
13074 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
13075 tree c, t = expr.value;
13076 t = c_fully_fold (t, false, NULL);
13077
13078 parens.skip_until_found_close (parser);
13079
13080 if (t == error_mark_node)
13081 return list;
13082 else if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
13083 {
13084 error_at (expr_loc, "%qs expression must be integral",
13085 omp_clause_code_name[code]);
13086 return list;
13087 }
13088
13089 /* Attempt to statically determine when the number isn't positive. */
13090 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
13091 build_int_cst (TREE_TYPE (t), 0));
13092 protected_set_expr_location (c, expr_loc);
13093 if (c == boolean_true_node)
13094 {
13095 warning_at (expr_loc, 0,
13096 "%qs value must be positive",
13097 omp_clause_code_name[code]);
13098 t = integer_one_node;
13099 }
13100
13101 check_no_duplicate_clause (list, code, omp_clause_code_name[code]);
13102
13103 c = build_omp_clause (loc, code);
13104 OMP_CLAUSE_OPERAND (c, 0) = t;
13105 OMP_CLAUSE_CHAIN (c) = list;
13106 return c;
13107 }
13108
13109 /* OpenACC:
13110
13111 gang [( gang-arg-list )]
13112 worker [( [num:] int-expr )]
13113 vector [( [length:] int-expr )]
13114
13115 where gang-arg is one of:
13116
13117 [num:] int-expr
13118 static: size-expr
13119
13120 and size-expr may be:
13121
13122 *
13123 int-expr
13124 */
13125
13126 static tree
13127 c_parser_oacc_shape_clause (c_parser *parser, omp_clause_code kind,
13128 const char *str, tree list)
13129 {
13130 const char *id = "num";
13131 tree ops[2] = { NULL_TREE, NULL_TREE }, c;
13132 location_t loc = c_parser_peek_token (parser)->location;
13133
13134 if (kind == OMP_CLAUSE_VECTOR)
13135 id = "length";
13136
13137 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
13138 {
13139 c_parser_consume_token (parser);
13140
13141 do
13142 {
13143 c_token *next = c_parser_peek_token (parser);
13144 int idx = 0;
13145
13146 /* Gang static argument. */
13147 if (kind == OMP_CLAUSE_GANG
13148 && c_parser_next_token_is_keyword (parser, RID_STATIC))
13149 {
13150 c_parser_consume_token (parser);
13151
13152 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
13153 goto cleanup_error;
13154
13155 idx = 1;
13156 if (ops[idx] != NULL_TREE)
13157 {
13158 c_parser_error (parser, "too many %<static%> arguments");
13159 goto cleanup_error;
13160 }
13161
13162 /* Check for the '*' argument. */
13163 if (c_parser_next_token_is (parser, CPP_MULT)
13164 && (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
13165 || c_parser_peek_2nd_token (parser)->type
13166 == CPP_CLOSE_PAREN))
13167 {
13168 c_parser_consume_token (parser);
13169 ops[idx] = integer_minus_one_node;
13170
13171 if (c_parser_next_token_is (parser, CPP_COMMA))
13172 {
13173 c_parser_consume_token (parser);
13174 continue;
13175 }
13176 else
13177 break;
13178 }
13179 }
13180 /* Worker num: argument and vector length: arguments. */
13181 else if (c_parser_next_token_is (parser, CPP_NAME)
13182 && strcmp (id, IDENTIFIER_POINTER (next->value)) == 0
13183 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
13184 {
13185 c_parser_consume_token (parser); /* id */
13186 c_parser_consume_token (parser); /* ':' */
13187 }
13188
13189 /* Now collect the actual argument. */
13190 if (ops[idx] != NULL_TREE)
13191 {
13192 c_parser_error (parser, "unexpected argument");
13193 goto cleanup_error;
13194 }
13195
13196 location_t expr_loc = c_parser_peek_token (parser)->location;
13197 c_expr cexpr = c_parser_expr_no_commas (parser, NULL);
13198 cexpr = convert_lvalue_to_rvalue (expr_loc, cexpr, false, true);
13199 tree expr = cexpr.value;
13200 if (expr == error_mark_node)
13201 goto cleanup_error;
13202
13203 expr = c_fully_fold (expr, false, NULL);
13204
13205 /* Attempt to statically determine when the number isn't a
13206 positive integer. */
13207
13208 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr)))
13209 {
13210 c_parser_error (parser, "expected integer expression");
13211 return list;
13212 }
13213
13214 tree c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, expr,
13215 build_int_cst (TREE_TYPE (expr), 0));
13216 if (c == boolean_true_node)
13217 {
13218 warning_at (loc, 0,
13219 "%qs value must be positive", str);
13220 expr = integer_one_node;
13221 }
13222
13223 ops[idx] = expr;
13224
13225 if (kind == OMP_CLAUSE_GANG
13226 && c_parser_next_token_is (parser, CPP_COMMA))
13227 {
13228 c_parser_consume_token (parser);
13229 continue;
13230 }
13231 break;
13232 }
13233 while (1);
13234
13235 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
13236 goto cleanup_error;
13237 }
13238
13239 check_no_duplicate_clause (list, kind, str);
13240
13241 c = build_omp_clause (loc, kind);
13242
13243 if (ops[1])
13244 OMP_CLAUSE_OPERAND (c, 1) = ops[1];
13245
13246 OMP_CLAUSE_OPERAND (c, 0) = ops[0];
13247 OMP_CLAUSE_CHAIN (c) = list;
13248
13249 return c;
13250
13251 cleanup_error:
13252 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
13253 return list;
13254 }
13255
13256 /* OpenACC 2.5:
13257 auto
13258 finalize
13259 independent
13260 nohost
13261 seq */
13262
13263 static tree
13264 c_parser_oacc_simple_clause (c_parser *parser, enum omp_clause_code code,
13265 tree list)
13266 {
13267 check_no_duplicate_clause (list, code, omp_clause_code_name[code]);
13268
13269 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
13270 OMP_CLAUSE_CHAIN (c) = list;
13271
13272 return c;
13273 }
13274
13275 /* OpenACC:
13276 async [( int-expr )] */
13277
13278 static tree
13279 c_parser_oacc_clause_async (c_parser *parser, tree list)
13280 {
13281 tree c, t;
13282 location_t loc = c_parser_peek_token (parser)->location;
13283
13284 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
13285
13286 if (c_parser_peek_token (parser)->type == CPP_OPEN_PAREN)
13287 {
13288 c_parser_consume_token (parser);
13289
13290 t = c_parser_expression (parser).value;
13291 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
13292 c_parser_error (parser, "expected integer expression");
13293 else if (t == error_mark_node
13294 || !c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
13295 return list;
13296 }
13297 else
13298 t = c_fully_fold (t, false, NULL);
13299
13300 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async");
13301
13302 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
13303 OMP_CLAUSE_ASYNC_EXPR (c) = t;
13304 OMP_CLAUSE_CHAIN (c) = list;
13305 list = c;
13306
13307 return list;
13308 }
13309
13310 /* OpenACC 2.0:
13311 tile ( size-expr-list ) */
13312
13313 static tree
13314 c_parser_oacc_clause_tile (c_parser *parser, tree list)
13315 {
13316 tree c, expr = error_mark_node;
13317 location_t loc;
13318 tree tile = NULL_TREE;
13319
13320 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile");
13321 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse");
13322
13323 loc = c_parser_peek_token (parser)->location;
13324 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
13325 return list;
13326
13327 do
13328 {
13329 if (tile && !c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
13330 return list;
13331
13332 if (c_parser_next_token_is (parser, CPP_MULT)
13333 && (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
13334 || c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_PAREN))
13335 {
13336 c_parser_consume_token (parser);
13337 expr = integer_zero_node;
13338 }
13339 else
13340 {
13341 location_t expr_loc = c_parser_peek_token (parser)->location;
13342 c_expr cexpr = c_parser_expr_no_commas (parser, NULL);
13343 cexpr = convert_lvalue_to_rvalue (expr_loc, cexpr, false, true);
13344 expr = cexpr.value;
13345
13346 if (expr == error_mark_node)
13347 {
13348 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
13349 "expected %<)%>");
13350 return list;
13351 }
13352
13353 expr = c_fully_fold (expr, false, NULL);
13354
13355 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr))
13356 || !tree_fits_shwi_p (expr)
13357 || tree_to_shwi (expr) <= 0)
13358 {
13359 error_at (expr_loc, "%<tile%> argument needs positive"
13360 " integral constant");
13361 expr = integer_zero_node;
13362 }
13363 }
13364
13365 tile = tree_cons (NULL_TREE, expr, tile);
13366 }
13367 while (c_parser_next_token_is_not (parser, CPP_CLOSE_PAREN));
13368
13369 /* Consume the trailing ')'. */
13370 c_parser_consume_token (parser);
13371
13372 c = build_omp_clause (loc, OMP_CLAUSE_TILE);
13373 tile = nreverse (tile);
13374 OMP_CLAUSE_TILE_LIST (c) = tile;
13375 OMP_CLAUSE_CHAIN (c) = list;
13376 return c;
13377 }
13378
13379 /* OpenACC:
13380 wait ( int-expr-list ) */
13381
13382 static tree
13383 c_parser_oacc_clause_wait (c_parser *parser, tree list)
13384 {
13385 location_t clause_loc = c_parser_peek_token (parser)->location;
13386
13387 if (c_parser_peek_token (parser)->type == CPP_OPEN_PAREN)
13388 list = c_parser_oacc_wait_list (parser, clause_loc, list);
13389
13390 return list;
13391 }
13392
13393 /* OpenMP 2.5:
13394 ordered
13395
13396 OpenMP 4.5:
13397 ordered ( constant-expression ) */
13398
13399 static tree
13400 c_parser_omp_clause_ordered (c_parser *parser, tree list)
13401 {
13402 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
13403
13404 tree c, num = NULL_TREE;
13405 HOST_WIDE_INT n;
13406 location_t loc = c_parser_peek_token (parser)->location;
13407 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
13408 {
13409 matching_parens parens;
13410 parens.consume_open (parser);
13411 num = c_parser_expr_no_commas (parser, NULL).value;
13412 parens.skip_until_found_close (parser);
13413 }
13414 if (num == error_mark_node)
13415 return list;
13416 if (num)
13417 {
13418 mark_exp_read (num);
13419 num = c_fully_fold (num, false, NULL);
13420 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
13421 || !tree_fits_shwi_p (num)
13422 || (n = tree_to_shwi (num)) <= 0
13423 || (int) n != n)
13424 {
13425 error_at (loc, "ordered argument needs positive "
13426 "constant integer expression");
13427 return list;
13428 }
13429 }
13430 c = build_omp_clause (loc, OMP_CLAUSE_ORDERED);
13431 OMP_CLAUSE_ORDERED_EXPR (c) = num;
13432 OMP_CLAUSE_CHAIN (c) = list;
13433 return c;
13434 }
13435
13436 /* OpenMP 2.5:
13437 private ( variable-list ) */
13438
13439 static tree
13440 c_parser_omp_clause_private (c_parser *parser, tree list)
13441 {
13442 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_PRIVATE, list);
13443 }
13444
13445 /* OpenMP 2.5:
13446 reduction ( reduction-operator : variable-list )
13447
13448 reduction-operator:
13449 One of: + * - & ^ | && ||
13450
13451 OpenMP 3.1:
13452
13453 reduction-operator:
13454 One of: + * - & ^ | && || max min
13455
13456 OpenMP 4.0:
13457
13458 reduction-operator:
13459 One of: + * - & ^ | && ||
13460 identifier
13461
13462 OpenMP 5.0:
13463 reduction ( reduction-modifier, reduction-operator : variable-list )
13464 in_reduction ( reduction-operator : variable-list )
13465 task_reduction ( reduction-operator : variable-list ) */
13466
13467 static tree
13468 c_parser_omp_clause_reduction (c_parser *parser, enum omp_clause_code kind,
13469 bool is_omp, tree list)
13470 {
13471 location_t clause_loc = c_parser_peek_token (parser)->location;
13472 matching_parens parens;
13473 if (parens.require_open (parser))
13474 {
13475 bool task = false;
13476 bool inscan = false;
13477 enum tree_code code = ERROR_MARK;
13478 tree reduc_id = NULL_TREE;
13479
13480 if (kind == OMP_CLAUSE_REDUCTION && is_omp)
13481 {
13482 if (c_parser_next_token_is_keyword (parser, RID_DEFAULT)
13483 && c_parser_peek_2nd_token (parser)->type == CPP_COMMA)
13484 {
13485 c_parser_consume_token (parser);
13486 c_parser_consume_token (parser);
13487 }
13488 else if (c_parser_next_token_is (parser, CPP_NAME)
13489 && c_parser_peek_2nd_token (parser)->type == CPP_COMMA)
13490 {
13491 const char *p
13492 = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13493 if (strcmp (p, "task") == 0)
13494 task = true;
13495 else if (strcmp (p, "inscan") == 0)
13496 {
13497 inscan = true;
13498 sorry ("%<inscan%> modifier on %<reduction%> clause "
13499 "not supported yet");
13500 }
13501 if (task || inscan)
13502 {
13503 c_parser_consume_token (parser);
13504 c_parser_consume_token (parser);
13505 }
13506 }
13507 }
13508
13509 switch (c_parser_peek_token (parser)->type)
13510 {
13511 case CPP_PLUS:
13512 code = PLUS_EXPR;
13513 break;
13514 case CPP_MULT:
13515 code = MULT_EXPR;
13516 break;
13517 case CPP_MINUS:
13518 code = MINUS_EXPR;
13519 break;
13520 case CPP_AND:
13521 code = BIT_AND_EXPR;
13522 break;
13523 case CPP_XOR:
13524 code = BIT_XOR_EXPR;
13525 break;
13526 case CPP_OR:
13527 code = BIT_IOR_EXPR;
13528 break;
13529 case CPP_AND_AND:
13530 code = TRUTH_ANDIF_EXPR;
13531 break;
13532 case CPP_OR_OR:
13533 code = TRUTH_ORIF_EXPR;
13534 break;
13535 case CPP_NAME:
13536 {
13537 const char *p
13538 = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13539 if (strcmp (p, "min") == 0)
13540 {
13541 code = MIN_EXPR;
13542 break;
13543 }
13544 if (strcmp (p, "max") == 0)
13545 {
13546 code = MAX_EXPR;
13547 break;
13548 }
13549 reduc_id = c_parser_peek_token (parser)->value;
13550 break;
13551 }
13552 default:
13553 c_parser_error (parser,
13554 "expected %<+%>, %<*%>, %<-%>, %<&%>, "
13555 "%<^%>, %<|%>, %<&&%>, %<||%> or identifier");
13556 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
13557 return list;
13558 }
13559 c_parser_consume_token (parser);
13560 reduc_id = c_omp_reduction_id (code, reduc_id);
13561 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
13562 {
13563 tree nl, c;
13564
13565 nl = c_parser_omp_variable_list (parser, clause_loc, kind, list);
13566 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
13567 {
13568 tree d = OMP_CLAUSE_DECL (c), type;
13569 if (TREE_CODE (d) != TREE_LIST)
13570 type = TREE_TYPE (d);
13571 else
13572 {
13573 int cnt = 0;
13574 tree t;
13575 for (t = d; TREE_CODE (t) == TREE_LIST; t = TREE_CHAIN (t))
13576 cnt++;
13577 type = TREE_TYPE (t);
13578 while (cnt > 0)
13579 {
13580 if (TREE_CODE (type) != POINTER_TYPE
13581 && TREE_CODE (type) != ARRAY_TYPE)
13582 break;
13583 type = TREE_TYPE (type);
13584 cnt--;
13585 }
13586 }
13587 while (TREE_CODE (type) == ARRAY_TYPE)
13588 type = TREE_TYPE (type);
13589 OMP_CLAUSE_REDUCTION_CODE (c) = code;
13590 if (task)
13591 OMP_CLAUSE_REDUCTION_TASK (c) = 1;
13592 else if (inscan)
13593 OMP_CLAUSE_REDUCTION_INSCAN (c) = 1;
13594 if (code == ERROR_MARK
13595 || !(INTEGRAL_TYPE_P (type)
13596 || TREE_CODE (type) == REAL_TYPE
13597 || TREE_CODE (type) == COMPLEX_TYPE))
13598 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c)
13599 = c_omp_reduction_lookup (reduc_id,
13600 TYPE_MAIN_VARIANT (type));
13601 }
13602
13603 list = nl;
13604 }
13605 parens.skip_until_found_close (parser);
13606 }
13607 return list;
13608 }
13609
13610 /* OpenMP 2.5:
13611 schedule ( schedule-kind )
13612 schedule ( schedule-kind , expression )
13613
13614 schedule-kind:
13615 static | dynamic | guided | runtime | auto
13616
13617 OpenMP 4.5:
13618 schedule ( schedule-modifier : schedule-kind )
13619 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
13620
13621 schedule-modifier:
13622 simd
13623 monotonic
13624 nonmonotonic */
13625
13626 static tree
13627 c_parser_omp_clause_schedule (c_parser *parser, tree list)
13628 {
13629 tree c, t;
13630 location_t loc = c_parser_peek_token (parser)->location;
13631 int modifiers = 0, nmodifiers = 0;
13632
13633 matching_parens parens;
13634 if (!parens.require_open (parser))
13635 return list;
13636
13637 c = build_omp_clause (loc, OMP_CLAUSE_SCHEDULE);
13638
13639 while (c_parser_next_token_is (parser, CPP_NAME))
13640 {
13641 tree kind = c_parser_peek_token (parser)->value;
13642 const char *p = IDENTIFIER_POINTER (kind);
13643 if (strcmp ("simd", p) == 0)
13644 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
13645 else if (strcmp ("monotonic", p) == 0)
13646 modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
13647 else if (strcmp ("nonmonotonic", p) == 0)
13648 modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
13649 else
13650 break;
13651 c_parser_consume_token (parser);
13652 if (nmodifiers++ == 0
13653 && c_parser_next_token_is (parser, CPP_COMMA))
13654 c_parser_consume_token (parser);
13655 else
13656 {
13657 c_parser_require (parser, CPP_COLON, "expected %<:%>");
13658 break;
13659 }
13660 }
13661
13662 if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
13663 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
13664 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
13665 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
13666 {
13667 error_at (loc, "both %<monotonic%> and %<nonmonotonic%> modifiers "
13668 "specified");
13669 modifiers = 0;
13670 }
13671
13672 if (c_parser_next_token_is (parser, CPP_NAME))
13673 {
13674 tree kind = c_parser_peek_token (parser)->value;
13675 const char *p = IDENTIFIER_POINTER (kind);
13676
13677 switch (p[0])
13678 {
13679 case 'd':
13680 if (strcmp ("dynamic", p) != 0)
13681 goto invalid_kind;
13682 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
13683 break;
13684
13685 case 'g':
13686 if (strcmp ("guided", p) != 0)
13687 goto invalid_kind;
13688 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
13689 break;
13690
13691 case 'r':
13692 if (strcmp ("runtime", p) != 0)
13693 goto invalid_kind;
13694 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
13695 break;
13696
13697 default:
13698 goto invalid_kind;
13699 }
13700 }
13701 else if (c_parser_next_token_is_keyword (parser, RID_STATIC))
13702 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
13703 else if (c_parser_next_token_is_keyword (parser, RID_AUTO))
13704 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
13705 else
13706 goto invalid_kind;
13707
13708 c_parser_consume_token (parser);
13709 if (c_parser_next_token_is (parser, CPP_COMMA))
13710 {
13711 location_t here;
13712 c_parser_consume_token (parser);
13713
13714 here = c_parser_peek_token (parser)->location;
13715 c_expr expr = c_parser_expr_no_commas (parser, NULL);
13716 expr = convert_lvalue_to_rvalue (here, expr, false, true);
13717 t = expr.value;
13718 t = c_fully_fold (t, false, NULL);
13719
13720 if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
13721 error_at (here, "schedule %<runtime%> does not take "
13722 "a %<chunk_size%> parameter");
13723 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
13724 error_at (here,
13725 "schedule %<auto%> does not take "
13726 "a %<chunk_size%> parameter");
13727 else if (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE)
13728 {
13729 /* Attempt to statically determine when the number isn't
13730 positive. */
13731 tree s = fold_build2_loc (loc, LE_EXPR, boolean_type_node, t,
13732 build_int_cst (TREE_TYPE (t), 0));
13733 protected_set_expr_location (s, loc);
13734 if (s == boolean_true_node)
13735 {
13736 warning_at (loc, 0,
13737 "chunk size value must be positive");
13738 t = integer_one_node;
13739 }
13740 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
13741 }
13742 else
13743 c_parser_error (parser, "expected integer expression");
13744
13745 parens.skip_until_found_close (parser);
13746 }
13747 else
13748 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
13749 "expected %<,%> or %<)%>");
13750
13751 OMP_CLAUSE_SCHEDULE_KIND (c)
13752 = (enum omp_clause_schedule_kind)
13753 (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
13754
13755 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
13756 OMP_CLAUSE_CHAIN (c) = list;
13757 return c;
13758
13759 invalid_kind:
13760 c_parser_error (parser, "invalid schedule kind");
13761 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
13762 return list;
13763 }
13764
13765 /* OpenMP 2.5:
13766 shared ( variable-list ) */
13767
13768 static tree
13769 c_parser_omp_clause_shared (c_parser *parser, tree list)
13770 {
13771 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_SHARED, list);
13772 }
13773
13774 /* OpenMP 3.0:
13775 untied */
13776
13777 static tree
13778 c_parser_omp_clause_untied (c_parser *parser ATTRIBUTE_UNUSED, tree list)
13779 {
13780 tree c;
13781
13782 /* FIXME: Should we allow duplicates? */
13783 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied");
13784
13785 c = build_omp_clause (c_parser_peek_token (parser)->location,
13786 OMP_CLAUSE_UNTIED);
13787 OMP_CLAUSE_CHAIN (c) = list;
13788
13789 return c;
13790 }
13791
13792 /* OpenMP 4.0:
13793 inbranch
13794 notinbranch */
13795
13796 static tree
13797 c_parser_omp_clause_branch (c_parser *parser ATTRIBUTE_UNUSED,
13798 enum omp_clause_code code, tree list)
13799 {
13800 check_no_duplicate_clause (list, code, omp_clause_code_name[code]);
13801
13802 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
13803 OMP_CLAUSE_CHAIN (c) = list;
13804
13805 return c;
13806 }
13807
13808 /* OpenMP 4.0:
13809 parallel
13810 for
13811 sections
13812 taskgroup */
13813
13814 static tree
13815 c_parser_omp_clause_cancelkind (c_parser *parser ATTRIBUTE_UNUSED,
13816 enum omp_clause_code code, tree list)
13817 {
13818 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
13819 OMP_CLAUSE_CHAIN (c) = list;
13820
13821 return c;
13822 }
13823
13824 /* OpenMP 4.5:
13825 nogroup */
13826
13827 static tree
13828 c_parser_omp_clause_nogroup (c_parser *parser ATTRIBUTE_UNUSED, tree list)
13829 {
13830 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup");
13831 tree c = build_omp_clause (c_parser_peek_token (parser)->location,
13832 OMP_CLAUSE_NOGROUP);
13833 OMP_CLAUSE_CHAIN (c) = list;
13834 return c;
13835 }
13836
13837 /* OpenMP 4.5:
13838 simd
13839 threads */
13840
13841 static tree
13842 c_parser_omp_clause_orderedkind (c_parser *parser ATTRIBUTE_UNUSED,
13843 enum omp_clause_code code, tree list)
13844 {
13845 check_no_duplicate_clause (list, code, omp_clause_code_name[code]);
13846 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
13847 OMP_CLAUSE_CHAIN (c) = list;
13848 return c;
13849 }
13850
13851 /* OpenMP 4.0:
13852 num_teams ( expression ) */
13853
13854 static tree
13855 c_parser_omp_clause_num_teams (c_parser *parser, tree list)
13856 {
13857 location_t num_teams_loc = c_parser_peek_token (parser)->location;
13858 matching_parens parens;
13859 if (parens.require_open (parser))
13860 {
13861 location_t expr_loc = c_parser_peek_token (parser)->location;
13862 c_expr expr = c_parser_expr_no_commas (parser, NULL);
13863 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
13864 tree c, t = expr.value;
13865 t = c_fully_fold (t, false, NULL);
13866
13867 parens.skip_until_found_close (parser);
13868
13869 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
13870 {
13871 c_parser_error (parser, "expected integer expression");
13872 return list;
13873 }
13874
13875 /* Attempt to statically determine when the number isn't positive. */
13876 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
13877 build_int_cst (TREE_TYPE (t), 0));
13878 protected_set_expr_location (c, expr_loc);
13879 if (c == boolean_true_node)
13880 {
13881 warning_at (expr_loc, 0, "%<num_teams%> value must be positive");
13882 t = integer_one_node;
13883 }
13884
13885 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS, "num_teams");
13886
13887 c = build_omp_clause (num_teams_loc, OMP_CLAUSE_NUM_TEAMS);
13888 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
13889 OMP_CLAUSE_CHAIN (c) = list;
13890 list = c;
13891 }
13892
13893 return list;
13894 }
13895
13896 /* OpenMP 4.0:
13897 thread_limit ( expression ) */
13898
13899 static tree
13900 c_parser_omp_clause_thread_limit (c_parser *parser, tree list)
13901 {
13902 location_t num_thread_limit_loc = c_parser_peek_token (parser)->location;
13903 matching_parens parens;
13904 if (parens.require_open (parser))
13905 {
13906 location_t expr_loc = c_parser_peek_token (parser)->location;
13907 c_expr expr = c_parser_expr_no_commas (parser, NULL);
13908 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
13909 tree c, t = expr.value;
13910 t = c_fully_fold (t, false, NULL);
13911
13912 parens.skip_until_found_close (parser);
13913
13914 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
13915 {
13916 c_parser_error (parser, "expected integer expression");
13917 return list;
13918 }
13919
13920 /* Attempt to statically determine when the number isn't positive. */
13921 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
13922 build_int_cst (TREE_TYPE (t), 0));
13923 protected_set_expr_location (c, expr_loc);
13924 if (c == boolean_true_node)
13925 {
13926 warning_at (expr_loc, 0, "%<thread_limit%> value must be positive");
13927 t = integer_one_node;
13928 }
13929
13930 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
13931 "thread_limit");
13932
13933 c = build_omp_clause (num_thread_limit_loc, OMP_CLAUSE_THREAD_LIMIT);
13934 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
13935 OMP_CLAUSE_CHAIN (c) = list;
13936 list = c;
13937 }
13938
13939 return list;
13940 }
13941
13942 /* OpenMP 4.0:
13943 aligned ( variable-list )
13944 aligned ( variable-list : constant-expression ) */
13945
13946 static tree
13947 c_parser_omp_clause_aligned (c_parser *parser, tree list)
13948 {
13949 location_t clause_loc = c_parser_peek_token (parser)->location;
13950 tree nl, c;
13951
13952 matching_parens parens;
13953 if (!parens.require_open (parser))
13954 return list;
13955
13956 nl = c_parser_omp_variable_list (parser, clause_loc,
13957 OMP_CLAUSE_ALIGNED, list);
13958
13959 if (c_parser_next_token_is (parser, CPP_COLON))
13960 {
13961 c_parser_consume_token (parser);
13962 location_t expr_loc = c_parser_peek_token (parser)->location;
13963 c_expr expr = c_parser_expr_no_commas (parser, NULL);
13964 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
13965 tree alignment = expr.value;
13966 alignment = c_fully_fold (alignment, false, NULL);
13967 if (TREE_CODE (alignment) != INTEGER_CST
13968 || !INTEGRAL_TYPE_P (TREE_TYPE (alignment))
13969 || tree_int_cst_sgn (alignment) != 1)
13970 {
13971 error_at (clause_loc, "%<aligned%> clause alignment expression must "
13972 "be positive constant integer expression");
13973 alignment = NULL_TREE;
13974 }
13975
13976 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
13977 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
13978 }
13979
13980 parens.skip_until_found_close (parser);
13981 return nl;
13982 }
13983
13984 /* OpenMP 4.0:
13985 linear ( variable-list )
13986 linear ( variable-list : expression )
13987
13988 OpenMP 4.5:
13989 linear ( modifier ( variable-list ) )
13990 linear ( modifier ( variable-list ) : expression ) */
13991
13992 static tree
13993 c_parser_omp_clause_linear (c_parser *parser, tree list)
13994 {
13995 location_t clause_loc = c_parser_peek_token (parser)->location;
13996 tree nl, c, step;
13997 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
13998
13999 matching_parens parens;
14000 if (!parens.require_open (parser))
14001 return list;
14002
14003 if (c_parser_next_token_is (parser, CPP_NAME))
14004 {
14005 c_token *tok = c_parser_peek_token (parser);
14006 const char *p = IDENTIFIER_POINTER (tok->value);
14007 if (strcmp ("val", p) == 0)
14008 kind = OMP_CLAUSE_LINEAR_VAL;
14009 if (c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN)
14010 kind = OMP_CLAUSE_LINEAR_DEFAULT;
14011 if (kind != OMP_CLAUSE_LINEAR_DEFAULT)
14012 {
14013 c_parser_consume_token (parser);
14014 c_parser_consume_token (parser);
14015 }
14016 }
14017
14018 nl = c_parser_omp_variable_list (parser, clause_loc,
14019 OMP_CLAUSE_LINEAR, list);
14020
14021 if (kind != OMP_CLAUSE_LINEAR_DEFAULT)
14022 parens.skip_until_found_close (parser);
14023
14024 if (c_parser_next_token_is (parser, CPP_COLON))
14025 {
14026 c_parser_consume_token (parser);
14027 location_t expr_loc = c_parser_peek_token (parser)->location;
14028 c_expr expr = c_parser_expr_no_commas (parser, NULL);
14029 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
14030 step = expr.value;
14031 step = c_fully_fold (step, false, NULL);
14032 if (!INTEGRAL_TYPE_P (TREE_TYPE (step)))
14033 {
14034 error_at (clause_loc, "%<linear%> clause step expression must "
14035 "be integral");
14036 step = integer_one_node;
14037 }
14038
14039 }
14040 else
14041 step = integer_one_node;
14042
14043 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
14044 {
14045 OMP_CLAUSE_LINEAR_STEP (c) = step;
14046 OMP_CLAUSE_LINEAR_KIND (c) = kind;
14047 }
14048
14049 parens.skip_until_found_close (parser);
14050 return nl;
14051 }
14052
14053 /* OpenMP 5.0:
14054 nontemporal ( variable-list ) */
14055
14056 static tree
14057 c_parser_omp_clause_nontemporal (c_parser *parser, tree list)
14058 {
14059 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_NONTEMPORAL, list);
14060 }
14061
14062 /* OpenMP 4.0:
14063 safelen ( constant-expression ) */
14064
14065 static tree
14066 c_parser_omp_clause_safelen (c_parser *parser, tree list)
14067 {
14068 location_t clause_loc = c_parser_peek_token (parser)->location;
14069 tree c, t;
14070
14071 matching_parens parens;
14072 if (!parens.require_open (parser))
14073 return list;
14074
14075 location_t expr_loc = c_parser_peek_token (parser)->location;
14076 c_expr expr = c_parser_expr_no_commas (parser, NULL);
14077 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
14078 t = expr.value;
14079 t = c_fully_fold (t, false, NULL);
14080 if (TREE_CODE (t) != INTEGER_CST
14081 || !INTEGRAL_TYPE_P (TREE_TYPE (t))
14082 || tree_int_cst_sgn (t) != 1)
14083 {
14084 error_at (clause_loc, "%<safelen%> clause expression must "
14085 "be positive constant integer expression");
14086 t = NULL_TREE;
14087 }
14088
14089 parens.skip_until_found_close (parser);
14090 if (t == NULL_TREE || t == error_mark_node)
14091 return list;
14092
14093 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen");
14094
14095 c = build_omp_clause (clause_loc, OMP_CLAUSE_SAFELEN);
14096 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
14097 OMP_CLAUSE_CHAIN (c) = list;
14098 return c;
14099 }
14100
14101 /* OpenMP 4.0:
14102 simdlen ( constant-expression ) */
14103
14104 static tree
14105 c_parser_omp_clause_simdlen (c_parser *parser, tree list)
14106 {
14107 location_t clause_loc = c_parser_peek_token (parser)->location;
14108 tree c, t;
14109
14110 matching_parens parens;
14111 if (!parens.require_open (parser))
14112 return list;
14113
14114 location_t expr_loc = c_parser_peek_token (parser)->location;
14115 c_expr expr = c_parser_expr_no_commas (parser, NULL);
14116 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
14117 t = expr.value;
14118 t = c_fully_fold (t, false, NULL);
14119 if (TREE_CODE (t) != INTEGER_CST
14120 || !INTEGRAL_TYPE_P (TREE_TYPE (t))
14121 || tree_int_cst_sgn (t) != 1)
14122 {
14123 error_at (clause_loc, "%<simdlen%> clause expression must "
14124 "be positive constant integer expression");
14125 t = NULL_TREE;
14126 }
14127
14128 parens.skip_until_found_close (parser);
14129 if (t == NULL_TREE || t == error_mark_node)
14130 return list;
14131
14132 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen");
14133
14134 c = build_omp_clause (clause_loc, OMP_CLAUSE_SIMDLEN);
14135 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
14136 OMP_CLAUSE_CHAIN (c) = list;
14137 return c;
14138 }
14139
14140 /* OpenMP 4.5:
14141 vec:
14142 identifier [+/- integer]
14143 vec , identifier [+/- integer]
14144 */
14145
14146 static tree
14147 c_parser_omp_clause_depend_sink (c_parser *parser, location_t clause_loc,
14148 tree list)
14149 {
14150 tree vec = NULL;
14151 if (c_parser_next_token_is_not (parser, CPP_NAME)
14152 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
14153 {
14154 c_parser_error (parser, "expected identifier");
14155 return list;
14156 }
14157
14158 while (c_parser_next_token_is (parser, CPP_NAME)
14159 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
14160 {
14161 tree t = lookup_name (c_parser_peek_token (parser)->value);
14162 tree addend = NULL;
14163
14164 if (t == NULL_TREE)
14165 {
14166 undeclared_variable (c_parser_peek_token (parser)->location,
14167 c_parser_peek_token (parser)->value);
14168 t = error_mark_node;
14169 }
14170
14171 c_parser_consume_token (parser);
14172
14173 bool neg = false;
14174 if (c_parser_next_token_is (parser, CPP_MINUS))
14175 neg = true;
14176 else if (!c_parser_next_token_is (parser, CPP_PLUS))
14177 {
14178 addend = integer_zero_node;
14179 neg = false;
14180 goto add_to_vector;
14181 }
14182 c_parser_consume_token (parser);
14183
14184 if (c_parser_next_token_is_not (parser, CPP_NUMBER))
14185 {
14186 c_parser_error (parser, "expected integer");
14187 return list;
14188 }
14189
14190 addend = c_parser_peek_token (parser)->value;
14191 if (TREE_CODE (addend) != INTEGER_CST)
14192 {
14193 c_parser_error (parser, "expected integer");
14194 return list;
14195 }
14196 c_parser_consume_token (parser);
14197
14198 add_to_vector:
14199 if (t != error_mark_node)
14200 {
14201 vec = tree_cons (addend, t, vec);
14202 if (neg)
14203 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec) = 1;
14204 }
14205
14206 if (c_parser_next_token_is_not (parser, CPP_COMMA))
14207 break;
14208
14209 c_parser_consume_token (parser);
14210 }
14211
14212 if (vec == NULL_TREE)
14213 return list;
14214
14215 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
14216 OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
14217 OMP_CLAUSE_DECL (u) = nreverse (vec);
14218 OMP_CLAUSE_CHAIN (u) = list;
14219 return u;
14220 }
14221
14222 /* OpenMP 5.0:
14223 iterators ( iterators-definition )
14224
14225 iterators-definition:
14226 iterator-specifier
14227 iterator-specifier , iterators-definition
14228
14229 iterator-specifier:
14230 identifier = range-specification
14231 iterator-type identifier = range-specification
14232
14233 range-specification:
14234 begin : end
14235 begin : end : step */
14236
14237 static tree
14238 c_parser_omp_iterators (c_parser *parser)
14239 {
14240 tree ret = NULL_TREE, *last = &ret;
14241 c_parser_consume_token (parser);
14242
14243 push_scope ();
14244
14245 matching_parens parens;
14246 if (!parens.require_open (parser))
14247 return error_mark_node;
14248
14249 do
14250 {
14251 tree iter_type = NULL_TREE, type_expr = NULL_TREE;
14252 if (c_parser_next_tokens_start_typename (parser, cla_prefer_id))
14253 {
14254 struct c_type_name *type = c_parser_type_name (parser);
14255 if (type != NULL)
14256 iter_type = groktypename (type, &type_expr, NULL);
14257 }
14258 if (iter_type == NULL_TREE)
14259 iter_type = integer_type_node;
14260
14261 location_t loc = c_parser_peek_token (parser)->location;
14262 if (!c_parser_next_token_is (parser, CPP_NAME))
14263 {
14264 c_parser_error (parser, "expected identifier");
14265 break;
14266 }
14267
14268 tree id = c_parser_peek_token (parser)->value;
14269 c_parser_consume_token (parser);
14270
14271 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
14272 break;
14273
14274 location_t eloc = c_parser_peek_token (parser)->location;
14275 c_expr expr = c_parser_expr_no_commas (parser, NULL);
14276 expr = convert_lvalue_to_rvalue (eloc, expr, true, false);
14277 tree begin = expr.value;
14278
14279 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
14280 break;
14281
14282 eloc = c_parser_peek_token (parser)->location;
14283 expr = c_parser_expr_no_commas (parser, NULL);
14284 expr = convert_lvalue_to_rvalue (eloc, expr, true, false);
14285 tree end = expr.value;
14286
14287 tree step = integer_one_node;
14288 if (c_parser_next_token_is (parser, CPP_COLON))
14289 {
14290 c_parser_consume_token (parser);
14291 eloc = c_parser_peek_token (parser)->location;
14292 expr = c_parser_expr_no_commas (parser, NULL);
14293 expr = convert_lvalue_to_rvalue (eloc, expr, true, false);
14294 step = expr.value;
14295 }
14296
14297 tree iter_var = build_decl (loc, VAR_DECL, id, iter_type);
14298 DECL_ARTIFICIAL (iter_var) = 1;
14299 DECL_CONTEXT (iter_var) = current_function_decl;
14300 pushdecl (iter_var);
14301
14302 *last = make_tree_vec (6);
14303 TREE_VEC_ELT (*last, 0) = iter_var;
14304 TREE_VEC_ELT (*last, 1) = begin;
14305 TREE_VEC_ELT (*last, 2) = end;
14306 TREE_VEC_ELT (*last, 3) = step;
14307 last = &TREE_CHAIN (*last);
14308
14309 if (c_parser_next_token_is (parser, CPP_COMMA))
14310 {
14311 c_parser_consume_token (parser);
14312 continue;
14313 }
14314 break;
14315 }
14316 while (1);
14317
14318 parens.skip_until_found_close (parser);
14319 return ret ? ret : error_mark_node;
14320 }
14321
14322 /* OpenMP 4.0:
14323 depend ( depend-kind: variable-list )
14324
14325 depend-kind:
14326 in | out | inout
14327
14328 OpenMP 4.5:
14329 depend ( source )
14330
14331 depend ( sink : vec )
14332
14333 OpenMP 5.0:
14334 depend ( depend-modifier , depend-kind: variable-list )
14335
14336 depend-kind:
14337 in | out | inout | mutexinoutset | depobj
14338
14339 depend-modifier:
14340 iterator ( iterators-definition ) */
14341
14342 static tree
14343 c_parser_omp_clause_depend (c_parser *parser, tree list)
14344 {
14345 location_t clause_loc = c_parser_peek_token (parser)->location;
14346 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_LAST;
14347 tree nl, c, iterators = NULL_TREE;
14348
14349 matching_parens parens;
14350 if (!parens.require_open (parser))
14351 return list;
14352
14353 do
14354 {
14355 if (c_parser_next_token_is_not (parser, CPP_NAME))
14356 goto invalid_kind;
14357
14358 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
14359 if (strcmp ("iterator", p) == 0 && iterators == NULL_TREE)
14360 {
14361 iterators = c_parser_omp_iterators (parser);
14362 c_parser_require (parser, CPP_COMMA, "expected %<,%>");
14363 continue;
14364 }
14365 if (strcmp ("in", p) == 0)
14366 kind = OMP_CLAUSE_DEPEND_IN;
14367 else if (strcmp ("inout", p) == 0)
14368 kind = OMP_CLAUSE_DEPEND_INOUT;
14369 else if (strcmp ("mutexinoutset", p) == 0)
14370 kind = OMP_CLAUSE_DEPEND_MUTEXINOUTSET;
14371 else if (strcmp ("out", p) == 0)
14372 kind = OMP_CLAUSE_DEPEND_OUT;
14373 else if (strcmp ("depobj", p) == 0)
14374 kind = OMP_CLAUSE_DEPEND_DEPOBJ;
14375 else if (strcmp ("sink", p) == 0)
14376 kind = OMP_CLAUSE_DEPEND_SINK;
14377 else if (strcmp ("source", p) == 0)
14378 kind = OMP_CLAUSE_DEPEND_SOURCE;
14379 else
14380 goto invalid_kind;
14381 break;
14382 }
14383 while (1);
14384
14385 c_parser_consume_token (parser);
14386
14387 if (iterators
14388 && (kind == OMP_CLAUSE_DEPEND_SOURCE || kind == OMP_CLAUSE_DEPEND_SINK))
14389 {
14390 pop_scope ();
14391 error_at (clause_loc, "%<iterator%> modifier incompatible with %qs",
14392 kind == OMP_CLAUSE_DEPEND_SOURCE ? "source" : "sink");
14393 iterators = NULL_TREE;
14394 }
14395
14396 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
14397 {
14398 c = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
14399 OMP_CLAUSE_DEPEND_KIND (c) = kind;
14400 OMP_CLAUSE_DECL (c) = NULL_TREE;
14401 OMP_CLAUSE_CHAIN (c) = list;
14402 parens.skip_until_found_close (parser);
14403 return c;
14404 }
14405
14406 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
14407 goto resync_fail;
14408
14409 if (kind == OMP_CLAUSE_DEPEND_SINK)
14410 nl = c_parser_omp_clause_depend_sink (parser, clause_loc, list);
14411 else
14412 {
14413 nl = c_parser_omp_variable_list (parser, clause_loc,
14414 OMP_CLAUSE_DEPEND, list);
14415
14416 if (iterators)
14417 {
14418 tree block = pop_scope ();
14419 if (iterators == error_mark_node)
14420 iterators = NULL_TREE;
14421 else
14422 TREE_VEC_ELT (iterators, 5) = block;
14423 }
14424
14425 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
14426 {
14427 OMP_CLAUSE_DEPEND_KIND (c) = kind;
14428 if (iterators)
14429 OMP_CLAUSE_DECL (c)
14430 = build_tree_list (iterators, OMP_CLAUSE_DECL (c));
14431 }
14432 }
14433
14434 parens.skip_until_found_close (parser);
14435 return nl;
14436
14437 invalid_kind:
14438 c_parser_error (parser, "invalid depend kind");
14439 resync_fail:
14440 parens.skip_until_found_close (parser);
14441 if (iterators)
14442 pop_scope ();
14443 return list;
14444 }
14445
14446 /* OpenMP 4.0:
14447 map ( map-kind: variable-list )
14448 map ( variable-list )
14449
14450 map-kind:
14451 alloc | to | from | tofrom
14452
14453 OpenMP 4.5:
14454 map-kind:
14455 alloc | to | from | tofrom | release | delete
14456
14457 map ( always [,] map-kind: variable-list ) */
14458
14459 static tree
14460 c_parser_omp_clause_map (c_parser *parser, tree list)
14461 {
14462 location_t clause_loc = c_parser_peek_token (parser)->location;
14463 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
14464 int always = 0;
14465 enum c_id_kind always_id_kind = C_ID_NONE;
14466 location_t always_loc = UNKNOWN_LOCATION;
14467 tree always_id = NULL_TREE;
14468 tree nl, c;
14469
14470 matching_parens parens;
14471 if (!parens.require_open (parser))
14472 return list;
14473
14474 if (c_parser_next_token_is (parser, CPP_NAME))
14475 {
14476 c_token *tok = c_parser_peek_token (parser);
14477 const char *p = IDENTIFIER_POINTER (tok->value);
14478 always_id_kind = tok->id_kind;
14479 always_loc = tok->location;
14480 always_id = tok->value;
14481 if (strcmp ("always", p) == 0)
14482 {
14483 c_token *sectok = c_parser_peek_2nd_token (parser);
14484 if (sectok->type == CPP_COMMA)
14485 {
14486 c_parser_consume_token (parser);
14487 c_parser_consume_token (parser);
14488 always = 2;
14489 }
14490 else if (sectok->type == CPP_NAME)
14491 {
14492 p = IDENTIFIER_POINTER (sectok->value);
14493 if (strcmp ("alloc", p) == 0
14494 || strcmp ("to", p) == 0
14495 || strcmp ("from", p) == 0
14496 || strcmp ("tofrom", p) == 0
14497 || strcmp ("release", p) == 0
14498 || strcmp ("delete", p) == 0)
14499 {
14500 c_parser_consume_token (parser);
14501 always = 1;
14502 }
14503 }
14504 }
14505 }
14506
14507 if (c_parser_next_token_is (parser, CPP_NAME)
14508 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
14509 {
14510 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
14511 if (strcmp ("alloc", p) == 0)
14512 kind = GOMP_MAP_ALLOC;
14513 else if (strcmp ("to", p) == 0)
14514 kind = always ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
14515 else if (strcmp ("from", p) == 0)
14516 kind = always ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
14517 else if (strcmp ("tofrom", p) == 0)
14518 kind = always ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
14519 else if (strcmp ("release", p) == 0)
14520 kind = GOMP_MAP_RELEASE;
14521 else if (strcmp ("delete", p) == 0)
14522 kind = GOMP_MAP_DELETE;
14523 else
14524 {
14525 c_parser_error (parser, "invalid map kind");
14526 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
14527 "expected %<)%>");
14528 return list;
14529 }
14530 c_parser_consume_token (parser);
14531 c_parser_consume_token (parser);
14532 }
14533 else if (always)
14534 {
14535 if (always_id_kind != C_ID_ID)
14536 {
14537 c_parser_error (parser, "expected identifier");
14538 parens.skip_until_found_close (parser);
14539 return list;
14540 }
14541
14542 tree t = lookup_name (always_id);
14543 if (t == NULL_TREE)
14544 {
14545 undeclared_variable (always_loc, always_id);
14546 t = error_mark_node;
14547 }
14548 if (t != error_mark_node)
14549 {
14550 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_MAP);
14551 OMP_CLAUSE_DECL (u) = t;
14552 OMP_CLAUSE_CHAIN (u) = list;
14553 OMP_CLAUSE_SET_MAP_KIND (u, kind);
14554 list = u;
14555 }
14556 if (always == 1)
14557 {
14558 parens.skip_until_found_close (parser);
14559 return list;
14560 }
14561 }
14562
14563 nl = c_parser_omp_variable_list (parser, clause_loc, OMP_CLAUSE_MAP, list);
14564
14565 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
14566 OMP_CLAUSE_SET_MAP_KIND (c, kind);
14567
14568 parens.skip_until_found_close (parser);
14569 return nl;
14570 }
14571
14572 /* OpenMP 4.0:
14573 device ( expression ) */
14574
14575 static tree
14576 c_parser_omp_clause_device (c_parser *parser, tree list)
14577 {
14578 location_t clause_loc = c_parser_peek_token (parser)->location;
14579 matching_parens parens;
14580 if (parens.require_open (parser))
14581 {
14582 location_t expr_loc = c_parser_peek_token (parser)->location;
14583 c_expr expr = c_parser_expr_no_commas (parser, NULL);
14584 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
14585 tree c, t = expr.value;
14586 t = c_fully_fold (t, false, NULL);
14587
14588 parens.skip_until_found_close (parser);
14589
14590 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
14591 {
14592 c_parser_error (parser, "expected integer expression");
14593 return list;
14594 }
14595
14596 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE, "device");
14597
14598 c = build_omp_clause (clause_loc, OMP_CLAUSE_DEVICE);
14599 OMP_CLAUSE_DEVICE_ID (c) = t;
14600 OMP_CLAUSE_CHAIN (c) = list;
14601 list = c;
14602 }
14603
14604 return list;
14605 }
14606
14607 /* OpenMP 4.0:
14608 dist_schedule ( static )
14609 dist_schedule ( static , expression ) */
14610
14611 static tree
14612 c_parser_omp_clause_dist_schedule (c_parser *parser, tree list)
14613 {
14614 tree c, t = NULL_TREE;
14615 location_t loc = c_parser_peek_token (parser)->location;
14616
14617 matching_parens parens;
14618 if (!parens.require_open (parser))
14619 return list;
14620
14621 if (!c_parser_next_token_is_keyword (parser, RID_STATIC))
14622 {
14623 c_parser_error (parser, "invalid dist_schedule kind");
14624 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
14625 "expected %<)%>");
14626 return list;
14627 }
14628
14629 c_parser_consume_token (parser);
14630 if (c_parser_next_token_is (parser, CPP_COMMA))
14631 {
14632 c_parser_consume_token (parser);
14633
14634 location_t expr_loc = c_parser_peek_token (parser)->location;
14635 c_expr expr = c_parser_expr_no_commas (parser, NULL);
14636 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
14637 t = expr.value;
14638 t = c_fully_fold (t, false, NULL);
14639 parens.skip_until_found_close (parser);
14640 }
14641 else
14642 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
14643 "expected %<,%> or %<)%>");
14644
14645 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
14646 if (t == error_mark_node)
14647 return list;
14648
14649 c = build_omp_clause (loc, OMP_CLAUSE_DIST_SCHEDULE);
14650 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
14651 OMP_CLAUSE_CHAIN (c) = list;
14652 return c;
14653 }
14654
14655 /* OpenMP 4.0:
14656 proc_bind ( proc-bind-kind )
14657
14658 proc-bind-kind:
14659 master | close | spread */
14660
14661 static tree
14662 c_parser_omp_clause_proc_bind (c_parser *parser, tree list)
14663 {
14664 location_t clause_loc = c_parser_peek_token (parser)->location;
14665 enum omp_clause_proc_bind_kind kind;
14666 tree c;
14667
14668 matching_parens parens;
14669 if (!parens.require_open (parser))
14670 return list;
14671
14672 if (c_parser_next_token_is (parser, CPP_NAME))
14673 {
14674 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
14675 if (strcmp ("master", p) == 0)
14676 kind = OMP_CLAUSE_PROC_BIND_MASTER;
14677 else if (strcmp ("close", p) == 0)
14678 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
14679 else if (strcmp ("spread", p) == 0)
14680 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
14681 else
14682 goto invalid_kind;
14683 }
14684 else
14685 goto invalid_kind;
14686
14687 c_parser_consume_token (parser);
14688 parens.skip_until_found_close (parser);
14689 c = build_omp_clause (clause_loc, OMP_CLAUSE_PROC_BIND);
14690 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
14691 OMP_CLAUSE_CHAIN (c) = list;
14692 return c;
14693
14694 invalid_kind:
14695 c_parser_error (parser, "invalid proc_bind kind");
14696 parens.skip_until_found_close (parser);
14697 return list;
14698 }
14699
14700 /* OpenMP 4.0:
14701 to ( variable-list ) */
14702
14703 static tree
14704 c_parser_omp_clause_to (c_parser *parser, tree list)
14705 {
14706 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_TO, list);
14707 }
14708
14709 /* OpenMP 4.0:
14710 from ( variable-list ) */
14711
14712 static tree
14713 c_parser_omp_clause_from (c_parser *parser, tree list)
14714 {
14715 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_FROM, list);
14716 }
14717
14718 /* OpenMP 4.0:
14719 uniform ( variable-list ) */
14720
14721 static tree
14722 c_parser_omp_clause_uniform (c_parser *parser, tree list)
14723 {
14724 /* The clauses location. */
14725 location_t loc = c_parser_peek_token (parser)->location;
14726
14727 matching_parens parens;
14728 if (parens.require_open (parser))
14729 {
14730 list = c_parser_omp_variable_list (parser, loc, OMP_CLAUSE_UNIFORM,
14731 list);
14732 parens.skip_until_found_close (parser);
14733 }
14734 return list;
14735 }
14736
14737 /* Parse all OpenACC clauses. The set clauses allowed by the directive
14738 is a bitmask in MASK. Return the list of clauses found. */
14739
14740 static tree
14741 c_parser_oacc_all_clauses (c_parser *parser, omp_clause_mask mask,
14742 const char *where, bool finish_p = true)
14743 {
14744 tree clauses = NULL;
14745 bool first = true;
14746
14747 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
14748 {
14749 location_t here;
14750 pragma_omp_clause c_kind;
14751 const char *c_name;
14752 tree prev = clauses;
14753
14754 if (!first && c_parser_next_token_is (parser, CPP_COMMA))
14755 c_parser_consume_token (parser);
14756
14757 here = c_parser_peek_token (parser)->location;
14758 c_kind = c_parser_omp_clause_name (parser);
14759
14760 switch (c_kind)
14761 {
14762 case PRAGMA_OACC_CLAUSE_ASYNC:
14763 clauses = c_parser_oacc_clause_async (parser, clauses);
14764 c_name = "async";
14765 break;
14766 case PRAGMA_OACC_CLAUSE_AUTO:
14767 clauses = c_parser_oacc_simple_clause (parser, OMP_CLAUSE_AUTO,
14768 clauses);
14769 c_name = "auto";
14770 break;
14771 case PRAGMA_OACC_CLAUSE_COLLAPSE:
14772 clauses = c_parser_omp_clause_collapse (parser, clauses);
14773 c_name = "collapse";
14774 break;
14775 case PRAGMA_OACC_CLAUSE_COPY:
14776 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
14777 c_name = "copy";
14778 break;
14779 case PRAGMA_OACC_CLAUSE_COPYIN:
14780 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
14781 c_name = "copyin";
14782 break;
14783 case PRAGMA_OACC_CLAUSE_COPYOUT:
14784 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
14785 c_name = "copyout";
14786 break;
14787 case PRAGMA_OACC_CLAUSE_CREATE:
14788 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
14789 c_name = "create";
14790 break;
14791 case PRAGMA_OACC_CLAUSE_DELETE:
14792 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
14793 c_name = "delete";
14794 break;
14795 case PRAGMA_OMP_CLAUSE_DEFAULT:
14796 clauses = c_parser_omp_clause_default (parser, clauses, true);
14797 c_name = "default";
14798 break;
14799 case PRAGMA_OACC_CLAUSE_DEVICE:
14800 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
14801 c_name = "device";
14802 break;
14803 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
14804 clauses = c_parser_oacc_data_clause_deviceptr (parser, clauses);
14805 c_name = "deviceptr";
14806 break;
14807 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
14808 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
14809 c_name = "device_resident";
14810 break;
14811 case PRAGMA_OACC_CLAUSE_FINALIZE:
14812 clauses = c_parser_oacc_simple_clause (parser, OMP_CLAUSE_FINALIZE,
14813 clauses);
14814 c_name = "finalize";
14815 break;
14816 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
14817 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
14818 c_name = "firstprivate";
14819 break;
14820 case PRAGMA_OACC_CLAUSE_GANG:
14821 c_name = "gang";
14822 clauses = c_parser_oacc_shape_clause (parser, OMP_CLAUSE_GANG,
14823 c_name, clauses);
14824 break;
14825 case PRAGMA_OACC_CLAUSE_HOST:
14826 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
14827 c_name = "host";
14828 break;
14829 case PRAGMA_OACC_CLAUSE_IF:
14830 clauses = c_parser_omp_clause_if (parser, clauses, false);
14831 c_name = "if";
14832 break;
14833 case PRAGMA_OACC_CLAUSE_IF_PRESENT:
14834 clauses = c_parser_oacc_simple_clause (parser, OMP_CLAUSE_IF_PRESENT,
14835 clauses);
14836 c_name = "if_present";
14837 break;
14838 case PRAGMA_OACC_CLAUSE_INDEPENDENT:
14839 clauses = c_parser_oacc_simple_clause (parser, OMP_CLAUSE_INDEPENDENT,
14840 clauses);
14841 c_name = "independent";
14842 break;
14843 case PRAGMA_OACC_CLAUSE_LINK:
14844 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
14845 c_name = "link";
14846 break;
14847 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
14848 clauses = c_parser_oacc_single_int_clause (parser,
14849 OMP_CLAUSE_NUM_GANGS,
14850 clauses);
14851 c_name = "num_gangs";
14852 break;
14853 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
14854 clauses = c_parser_oacc_single_int_clause (parser,
14855 OMP_CLAUSE_NUM_WORKERS,
14856 clauses);
14857 c_name = "num_workers";
14858 break;
14859 case PRAGMA_OACC_CLAUSE_PRESENT:
14860 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
14861 c_name = "present";
14862 break;
14863 case PRAGMA_OACC_CLAUSE_PRIVATE:
14864 clauses = c_parser_omp_clause_private (parser, clauses);
14865 c_name = "private";
14866 break;
14867 case PRAGMA_OACC_CLAUSE_REDUCTION:
14868 clauses
14869 = c_parser_omp_clause_reduction (parser, OMP_CLAUSE_REDUCTION,
14870 false, clauses);
14871 c_name = "reduction";
14872 break;
14873 case PRAGMA_OACC_CLAUSE_SEQ:
14874 clauses = c_parser_oacc_simple_clause (parser, OMP_CLAUSE_SEQ,
14875 clauses);
14876 c_name = "seq";
14877 break;
14878 case PRAGMA_OACC_CLAUSE_TILE:
14879 clauses = c_parser_oacc_clause_tile (parser, clauses);
14880 c_name = "tile";
14881 break;
14882 case PRAGMA_OACC_CLAUSE_USE_DEVICE:
14883 clauses = c_parser_omp_clause_use_device_ptr (parser, clauses);
14884 c_name = "use_device";
14885 break;
14886 case PRAGMA_OACC_CLAUSE_VECTOR:
14887 c_name = "vector";
14888 clauses = c_parser_oacc_shape_clause (parser, OMP_CLAUSE_VECTOR,
14889 c_name, clauses);
14890 break;
14891 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
14892 clauses = c_parser_oacc_single_int_clause (parser,
14893 OMP_CLAUSE_VECTOR_LENGTH,
14894 clauses);
14895 c_name = "vector_length";
14896 break;
14897 case PRAGMA_OACC_CLAUSE_WAIT:
14898 clauses = c_parser_oacc_clause_wait (parser, clauses);
14899 c_name = "wait";
14900 break;
14901 case PRAGMA_OACC_CLAUSE_WORKER:
14902 c_name = "worker";
14903 clauses = c_parser_oacc_shape_clause (parser, OMP_CLAUSE_WORKER,
14904 c_name, clauses);
14905 break;
14906 default:
14907 c_parser_error (parser, "expected %<#pragma acc%> clause");
14908 goto saw_error;
14909 }
14910
14911 first = false;
14912
14913 if (((mask >> c_kind) & 1) == 0)
14914 {
14915 /* Remove the invalid clause(s) from the list to avoid
14916 confusing the rest of the compiler. */
14917 clauses = prev;
14918 error_at (here, "%qs is not valid for %qs", c_name, where);
14919 }
14920 }
14921
14922 saw_error:
14923 c_parser_skip_to_pragma_eol (parser);
14924
14925 if (finish_p)
14926 return c_finish_omp_clauses (clauses, C_ORT_ACC);
14927
14928 return clauses;
14929 }
14930
14931 /* Parse all OpenMP clauses. The set clauses allowed by the directive
14932 is a bitmask in MASK. Return the list of clauses found. */
14933
14934 static tree
14935 c_parser_omp_all_clauses (c_parser *parser, omp_clause_mask mask,
14936 const char *where, bool finish_p = true)
14937 {
14938 tree clauses = NULL;
14939 bool first = true;
14940
14941 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
14942 {
14943 location_t here;
14944 pragma_omp_clause c_kind;
14945 const char *c_name;
14946 tree prev = clauses;
14947
14948 if (!first && c_parser_next_token_is (parser, CPP_COMMA))
14949 c_parser_consume_token (parser);
14950
14951 here = c_parser_peek_token (parser)->location;
14952 c_kind = c_parser_omp_clause_name (parser);
14953
14954 switch (c_kind)
14955 {
14956 case PRAGMA_OMP_CLAUSE_COLLAPSE:
14957 clauses = c_parser_omp_clause_collapse (parser, clauses);
14958 c_name = "collapse";
14959 break;
14960 case PRAGMA_OMP_CLAUSE_COPYIN:
14961 clauses = c_parser_omp_clause_copyin (parser, clauses);
14962 c_name = "copyin";
14963 break;
14964 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
14965 clauses = c_parser_omp_clause_copyprivate (parser, clauses);
14966 c_name = "copyprivate";
14967 break;
14968 case PRAGMA_OMP_CLAUSE_DEFAULT:
14969 clauses = c_parser_omp_clause_default (parser, clauses, false);
14970 c_name = "default";
14971 break;
14972 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
14973 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
14974 c_name = "firstprivate";
14975 break;
14976 case PRAGMA_OMP_CLAUSE_FINAL:
14977 clauses = c_parser_omp_clause_final (parser, clauses);
14978 c_name = "final";
14979 break;
14980 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
14981 clauses = c_parser_omp_clause_grainsize (parser, clauses);
14982 c_name = "grainsize";
14983 break;
14984 case PRAGMA_OMP_CLAUSE_HINT:
14985 clauses = c_parser_omp_clause_hint (parser, clauses);
14986 c_name = "hint";
14987 break;
14988 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
14989 clauses = c_parser_omp_clause_defaultmap (parser, clauses);
14990 c_name = "defaultmap";
14991 break;
14992 case PRAGMA_OMP_CLAUSE_IF:
14993 clauses = c_parser_omp_clause_if (parser, clauses, true);
14994 c_name = "if";
14995 break;
14996 case PRAGMA_OMP_CLAUSE_IN_REDUCTION:
14997 clauses
14998 = c_parser_omp_clause_reduction (parser, OMP_CLAUSE_IN_REDUCTION,
14999 true, clauses);
15000 c_name = "in_reduction";
15001 break;
15002 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
15003 clauses = c_parser_omp_clause_lastprivate (parser, clauses);
15004 c_name = "lastprivate";
15005 break;
15006 case PRAGMA_OMP_CLAUSE_MERGEABLE:
15007 clauses = c_parser_omp_clause_mergeable (parser, clauses);
15008 c_name = "mergeable";
15009 break;
15010 case PRAGMA_OMP_CLAUSE_NOWAIT:
15011 clauses = c_parser_omp_clause_nowait (parser, clauses);
15012 c_name = "nowait";
15013 break;
15014 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
15015 clauses = c_parser_omp_clause_num_tasks (parser, clauses);
15016 c_name = "num_tasks";
15017 break;
15018 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
15019 clauses = c_parser_omp_clause_num_threads (parser, clauses);
15020 c_name = "num_threads";
15021 break;
15022 case PRAGMA_OMP_CLAUSE_ORDERED:
15023 clauses = c_parser_omp_clause_ordered (parser, clauses);
15024 c_name = "ordered";
15025 break;
15026 case PRAGMA_OMP_CLAUSE_PRIORITY:
15027 clauses = c_parser_omp_clause_priority (parser, clauses);
15028 c_name = "priority";
15029 break;
15030 case PRAGMA_OMP_CLAUSE_PRIVATE:
15031 clauses = c_parser_omp_clause_private (parser, clauses);
15032 c_name = "private";
15033 break;
15034 case PRAGMA_OMP_CLAUSE_REDUCTION:
15035 clauses
15036 = c_parser_omp_clause_reduction (parser, OMP_CLAUSE_REDUCTION,
15037 true, clauses);
15038 c_name = "reduction";
15039 break;
15040 case PRAGMA_OMP_CLAUSE_SCHEDULE:
15041 clauses = c_parser_omp_clause_schedule (parser, clauses);
15042 c_name = "schedule";
15043 break;
15044 case PRAGMA_OMP_CLAUSE_SHARED:
15045 clauses = c_parser_omp_clause_shared (parser, clauses);
15046 c_name = "shared";
15047 break;
15048 case PRAGMA_OMP_CLAUSE_TASK_REDUCTION:
15049 clauses
15050 = c_parser_omp_clause_reduction (parser, OMP_CLAUSE_TASK_REDUCTION,
15051 true, clauses);
15052 c_name = "task_reduction";
15053 break;
15054 case PRAGMA_OMP_CLAUSE_UNTIED:
15055 clauses = c_parser_omp_clause_untied (parser, clauses);
15056 c_name = "untied";
15057 break;
15058 case PRAGMA_OMP_CLAUSE_INBRANCH:
15059 clauses = c_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
15060 clauses);
15061 c_name = "inbranch";
15062 break;
15063 case PRAGMA_OMP_CLAUSE_NONTEMPORAL:
15064 clauses = c_parser_omp_clause_nontemporal (parser, clauses);
15065 c_name = "nontemporal";
15066 break;
15067 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
15068 clauses = c_parser_omp_clause_branch (parser, OMP_CLAUSE_NOTINBRANCH,
15069 clauses);
15070 c_name = "notinbranch";
15071 break;
15072 case PRAGMA_OMP_CLAUSE_PARALLEL:
15073 clauses
15074 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
15075 clauses);
15076 c_name = "parallel";
15077 if (!first)
15078 {
15079 clause_not_first:
15080 error_at (here, "%qs must be the first clause of %qs",
15081 c_name, where);
15082 clauses = prev;
15083 }
15084 break;
15085 case PRAGMA_OMP_CLAUSE_FOR:
15086 clauses
15087 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
15088 clauses);
15089 c_name = "for";
15090 if (!first)
15091 goto clause_not_first;
15092 break;
15093 case PRAGMA_OMP_CLAUSE_SECTIONS:
15094 clauses
15095 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
15096 clauses);
15097 c_name = "sections";
15098 if (!first)
15099 goto clause_not_first;
15100 break;
15101 case PRAGMA_OMP_CLAUSE_TASKGROUP:
15102 clauses
15103 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
15104 clauses);
15105 c_name = "taskgroup";
15106 if (!first)
15107 goto clause_not_first;
15108 break;
15109 case PRAGMA_OMP_CLAUSE_LINK:
15110 clauses
15111 = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_LINK, clauses);
15112 c_name = "link";
15113 break;
15114 case PRAGMA_OMP_CLAUSE_TO:
15115 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
15116 clauses
15117 = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_TO_DECLARE,
15118 clauses);
15119 else
15120 clauses = c_parser_omp_clause_to (parser, clauses);
15121 c_name = "to";
15122 break;
15123 case PRAGMA_OMP_CLAUSE_FROM:
15124 clauses = c_parser_omp_clause_from (parser, clauses);
15125 c_name = "from";
15126 break;
15127 case PRAGMA_OMP_CLAUSE_UNIFORM:
15128 clauses = c_parser_omp_clause_uniform (parser, clauses);
15129 c_name = "uniform";
15130 break;
15131 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
15132 clauses = c_parser_omp_clause_num_teams (parser, clauses);
15133 c_name = "num_teams";
15134 break;
15135 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
15136 clauses = c_parser_omp_clause_thread_limit (parser, clauses);
15137 c_name = "thread_limit";
15138 break;
15139 case PRAGMA_OMP_CLAUSE_ALIGNED:
15140 clauses = c_parser_omp_clause_aligned (parser, clauses);
15141 c_name = "aligned";
15142 break;
15143 case PRAGMA_OMP_CLAUSE_LINEAR:
15144 clauses = c_parser_omp_clause_linear (parser, clauses);
15145 c_name = "linear";
15146 break;
15147 case PRAGMA_OMP_CLAUSE_DEPEND:
15148 clauses = c_parser_omp_clause_depend (parser, clauses);
15149 c_name = "depend";
15150 break;
15151 case PRAGMA_OMP_CLAUSE_MAP:
15152 clauses = c_parser_omp_clause_map (parser, clauses);
15153 c_name = "map";
15154 break;
15155 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
15156 clauses = c_parser_omp_clause_use_device_ptr (parser, clauses);
15157 c_name = "use_device_ptr";
15158 break;
15159 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
15160 clauses = c_parser_omp_clause_is_device_ptr (parser, clauses);
15161 c_name = "is_device_ptr";
15162 break;
15163 case PRAGMA_OMP_CLAUSE_DEVICE:
15164 clauses = c_parser_omp_clause_device (parser, clauses);
15165 c_name = "device";
15166 break;
15167 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
15168 clauses = c_parser_omp_clause_dist_schedule (parser, clauses);
15169 c_name = "dist_schedule";
15170 break;
15171 case PRAGMA_OMP_CLAUSE_PROC_BIND:
15172 clauses = c_parser_omp_clause_proc_bind (parser, clauses);
15173 c_name = "proc_bind";
15174 break;
15175 case PRAGMA_OMP_CLAUSE_SAFELEN:
15176 clauses = c_parser_omp_clause_safelen (parser, clauses);
15177 c_name = "safelen";
15178 break;
15179 case PRAGMA_OMP_CLAUSE_SIMDLEN:
15180 clauses = c_parser_omp_clause_simdlen (parser, clauses);
15181 c_name = "simdlen";
15182 break;
15183 case PRAGMA_OMP_CLAUSE_NOGROUP:
15184 clauses = c_parser_omp_clause_nogroup (parser, clauses);
15185 c_name = "nogroup";
15186 break;
15187 case PRAGMA_OMP_CLAUSE_THREADS:
15188 clauses
15189 = c_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
15190 clauses);
15191 c_name = "threads";
15192 break;
15193 case PRAGMA_OMP_CLAUSE_SIMD:
15194 clauses
15195 = c_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
15196 clauses);
15197 c_name = "simd";
15198 break;
15199 default:
15200 c_parser_error (parser, "expected %<#pragma omp%> clause");
15201 goto saw_error;
15202 }
15203
15204 first = false;
15205
15206 if (((mask >> c_kind) & 1) == 0)
15207 {
15208 /* Remove the invalid clause(s) from the list to avoid
15209 confusing the rest of the compiler. */
15210 clauses = prev;
15211 error_at (here, "%qs is not valid for %qs", c_name, where);
15212 }
15213 }
15214
15215 saw_error:
15216 c_parser_skip_to_pragma_eol (parser);
15217
15218 if (finish_p)
15219 {
15220 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
15221 return c_finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
15222 return c_finish_omp_clauses (clauses, C_ORT_OMP);
15223 }
15224
15225 return clauses;
15226 }
15227
15228 /* OpenACC 2.0, OpenMP 2.5:
15229 structured-block:
15230 statement
15231
15232 In practice, we're also interested in adding the statement to an
15233 outer node. So it is convenient if we work around the fact that
15234 c_parser_statement calls add_stmt. */
15235
15236 static tree
15237 c_parser_omp_structured_block (c_parser *parser, bool *if_p)
15238 {
15239 tree stmt = push_stmt_list ();
15240 c_parser_statement (parser, if_p);
15241 return pop_stmt_list (stmt);
15242 }
15243
15244 /* OpenACC 2.0:
15245 # pragma acc cache (variable-list) new-line
15246
15247 LOC is the location of the #pragma token.
15248 */
15249
15250 static tree
15251 c_parser_oacc_cache (location_t loc, c_parser *parser)
15252 {
15253 tree stmt, clauses;
15254
15255 clauses = c_parser_omp_var_list_parens (parser, OMP_CLAUSE__CACHE_, NULL);
15256 clauses = c_finish_omp_clauses (clauses, C_ORT_ACC);
15257
15258 c_parser_skip_to_pragma_eol (parser);
15259
15260 stmt = make_node (OACC_CACHE);
15261 TREE_TYPE (stmt) = void_type_node;
15262 OACC_CACHE_CLAUSES (stmt) = clauses;
15263 SET_EXPR_LOCATION (stmt, loc);
15264 add_stmt (stmt);
15265
15266 return stmt;
15267 }
15268
15269 /* OpenACC 2.0:
15270 # pragma acc data oacc-data-clause[optseq] new-line
15271 structured-block
15272
15273 LOC is the location of the #pragma token.
15274 */
15275
15276 #define OACC_DATA_CLAUSE_MASK \
15277 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
15278 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
15279 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
15280 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
15281 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
15282 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
15283 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT))
15284
15285 static tree
15286 c_parser_oacc_data (location_t loc, c_parser *parser, bool *if_p)
15287 {
15288 tree stmt, clauses, block;
15289
15290 clauses = c_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
15291 "#pragma acc data");
15292
15293 block = c_begin_omp_parallel ();
15294 add_stmt (c_parser_omp_structured_block (parser, if_p));
15295
15296 stmt = c_finish_oacc_data (loc, clauses, block);
15297
15298 return stmt;
15299 }
15300
15301 /* OpenACC 2.0:
15302 # pragma acc declare oacc-data-clause[optseq] new-line
15303 */
15304
15305 #define OACC_DECLARE_CLAUSE_MASK \
15306 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
15307 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
15308 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
15309 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
15310 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
15311 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
15312 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
15313 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT))
15314
15315 static void
15316 c_parser_oacc_declare (c_parser *parser)
15317 {
15318 location_t pragma_loc = c_parser_peek_token (parser)->location;
15319 tree clauses, stmt, t, decl;
15320
15321 bool error = false;
15322
15323 c_parser_consume_pragma (parser);
15324
15325 clauses = c_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
15326 "#pragma acc declare");
15327 if (!clauses)
15328 {
15329 error_at (pragma_loc,
15330 "no valid clauses specified in %<#pragma acc declare%>");
15331 return;
15332 }
15333
15334 for (t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
15335 {
15336 location_t loc = OMP_CLAUSE_LOCATION (t);
15337 decl = OMP_CLAUSE_DECL (t);
15338 if (!DECL_P (decl))
15339 {
15340 error_at (loc, "array section in %<#pragma acc declare%>");
15341 error = true;
15342 continue;
15343 }
15344
15345 switch (OMP_CLAUSE_MAP_KIND (t))
15346 {
15347 case GOMP_MAP_FIRSTPRIVATE_POINTER:
15348 case GOMP_MAP_ALLOC:
15349 case GOMP_MAP_TO:
15350 case GOMP_MAP_FORCE_DEVICEPTR:
15351 case GOMP_MAP_DEVICE_RESIDENT:
15352 break;
15353
15354 case GOMP_MAP_LINK:
15355 if (!global_bindings_p ()
15356 && (TREE_STATIC (decl)
15357 || !DECL_EXTERNAL (decl)))
15358 {
15359 error_at (loc,
15360 "%qD must be a global variable in "
15361 "%<#pragma acc declare link%>",
15362 decl);
15363 error = true;
15364 continue;
15365 }
15366 break;
15367
15368 default:
15369 if (global_bindings_p ())
15370 {
15371 error_at (loc, "invalid OpenACC clause at file scope");
15372 error = true;
15373 continue;
15374 }
15375 if (DECL_EXTERNAL (decl))
15376 {
15377 error_at (loc,
15378 "invalid use of %<extern%> variable %qD "
15379 "in %<#pragma acc declare%>", decl);
15380 error = true;
15381 continue;
15382 }
15383 else if (TREE_PUBLIC (decl))
15384 {
15385 error_at (loc,
15386 "invalid use of %<global%> variable %qD "
15387 "in %<#pragma acc declare%>", decl);
15388 error = true;
15389 continue;
15390 }
15391 break;
15392 }
15393
15394 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
15395 || lookup_attribute ("omp declare target link",
15396 DECL_ATTRIBUTES (decl)))
15397 {
15398 error_at (loc, "variable %qD used more than once with "
15399 "%<#pragma acc declare%>", decl);
15400 error = true;
15401 continue;
15402 }
15403
15404 if (!error)
15405 {
15406 tree id;
15407
15408 if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
15409 id = get_identifier ("omp declare target link");
15410 else
15411 id = get_identifier ("omp declare target");
15412
15413 DECL_ATTRIBUTES (decl)
15414 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
15415
15416 if (global_bindings_p ())
15417 {
15418 symtab_node *node = symtab_node::get (decl);
15419 if (node != NULL)
15420 {
15421 node->offloadable = 1;
15422 if (ENABLE_OFFLOADING)
15423 {
15424 g->have_offload = true;
15425 if (is_a <varpool_node *> (node))
15426 vec_safe_push (offload_vars, decl);
15427 }
15428 }
15429 }
15430 }
15431 }
15432
15433 if (error || global_bindings_p ())
15434 return;
15435
15436 stmt = make_node (OACC_DECLARE);
15437 TREE_TYPE (stmt) = void_type_node;
15438 OACC_DECLARE_CLAUSES (stmt) = clauses;
15439 SET_EXPR_LOCATION (stmt, pragma_loc);
15440
15441 add_stmt (stmt);
15442
15443 return;
15444 }
15445
15446 /* OpenACC 2.0:
15447 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
15448
15449 or
15450
15451 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
15452
15453
15454 LOC is the location of the #pragma token.
15455 */
15456
15457 #define OACC_ENTER_DATA_CLAUSE_MASK \
15458 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
15459 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
15460 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
15461 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
15462 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
15463
15464 #define OACC_EXIT_DATA_CLAUSE_MASK \
15465 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
15466 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
15467 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
15468 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
15469 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FINALIZE) \
15470 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
15471
15472 static void
15473 c_parser_oacc_enter_exit_data (c_parser *parser, bool enter)
15474 {
15475 location_t loc = c_parser_peek_token (parser)->location;
15476 tree clauses, stmt;
15477 const char *p = "";
15478
15479 c_parser_consume_pragma (parser);
15480
15481 if (c_parser_next_token_is (parser, CPP_NAME))
15482 {
15483 p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
15484 c_parser_consume_token (parser);
15485 }
15486
15487 if (strcmp (p, "data") != 0)
15488 {
15489 error_at (loc, "expected %<data%> after %<#pragma acc %s%>",
15490 enter ? "enter" : "exit");
15491 parser->error = true;
15492 c_parser_skip_to_pragma_eol (parser);
15493 return;
15494 }
15495
15496 if (enter)
15497 clauses = c_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
15498 "#pragma acc enter data");
15499 else
15500 clauses = c_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
15501 "#pragma acc exit data");
15502
15503 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
15504 {
15505 error_at (loc, "%<#pragma acc %s data%> has no data movement clause",
15506 enter ? "enter" : "exit");
15507 return;
15508 }
15509
15510 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
15511 TREE_TYPE (stmt) = void_type_node;
15512 OMP_STANDALONE_CLAUSES (stmt) = clauses;
15513 SET_EXPR_LOCATION (stmt, loc);
15514 add_stmt (stmt);
15515 }
15516
15517
15518 /* OpenACC 2.0:
15519 # pragma acc host_data oacc-data-clause[optseq] new-line
15520 structured-block
15521 */
15522
15523 #define OACC_HOST_DATA_CLAUSE_MASK \
15524 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
15525
15526 static tree
15527 c_parser_oacc_host_data (location_t loc, c_parser *parser, bool *if_p)
15528 {
15529 tree stmt, clauses, block;
15530
15531 clauses = c_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
15532 "#pragma acc host_data");
15533
15534 block = c_begin_omp_parallel ();
15535 add_stmt (c_parser_omp_structured_block (parser, if_p));
15536 stmt = c_finish_oacc_host_data (loc, clauses, block);
15537 return stmt;
15538 }
15539
15540
15541 /* OpenACC 2.0:
15542
15543 # pragma acc loop oacc-loop-clause[optseq] new-line
15544 structured-block
15545
15546 LOC is the location of the #pragma token.
15547 */
15548
15549 #define OACC_LOOP_CLAUSE_MASK \
15550 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
15551 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
15552 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
15553 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
15554 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
15555 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
15556 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
15557 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
15558 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
15559 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE) )
15560 static tree
15561 c_parser_oacc_loop (location_t loc, c_parser *parser, char *p_name,
15562 omp_clause_mask mask, tree *cclauses, bool *if_p)
15563 {
15564 bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
15565
15566 strcat (p_name, " loop");
15567 mask |= OACC_LOOP_CLAUSE_MASK;
15568
15569 tree clauses = c_parser_oacc_all_clauses (parser, mask, p_name,
15570 cclauses == NULL);
15571 if (cclauses)
15572 {
15573 clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
15574 if (*cclauses)
15575 *cclauses = c_finish_omp_clauses (*cclauses, C_ORT_ACC);
15576 if (clauses)
15577 clauses = c_finish_omp_clauses (clauses, C_ORT_ACC);
15578 }
15579
15580 tree block = c_begin_compound_stmt (true);
15581 tree stmt = c_parser_omp_for_loop (loc, parser, OACC_LOOP, clauses, NULL,
15582 if_p);
15583 block = c_end_compound_stmt (loc, block, true);
15584 add_stmt (block);
15585
15586 return stmt;
15587 }
15588
15589 /* OpenACC 2.0:
15590 # pragma acc kernels oacc-kernels-clause[optseq] new-line
15591 structured-block
15592
15593 or
15594
15595 # pragma acc parallel oacc-parallel-clause[optseq] new-line
15596 structured-block
15597
15598 LOC is the location of the #pragma token.
15599 */
15600
15601 #define OACC_KERNELS_CLAUSE_MASK \
15602 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
15603 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
15604 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
15605 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
15606 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
15607 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
15608 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
15609 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
15610 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
15611 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
15612 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
15613 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
15614 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
15615
15616 #define OACC_PARALLEL_CLAUSE_MASK \
15617 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
15618 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
15619 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
15620 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
15621 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
15622 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
15623 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
15624 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
15625 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
15626 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
15627 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
15628 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
15629 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
15630 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
15631 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
15632 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
15633
15634 static tree
15635 c_parser_oacc_kernels_parallel (location_t loc, c_parser *parser,
15636 enum pragma_kind p_kind, char *p_name,
15637 bool *if_p)
15638 {
15639 omp_clause_mask mask;
15640 enum tree_code code;
15641 switch (p_kind)
15642 {
15643 case PRAGMA_OACC_KERNELS:
15644 strcat (p_name, " kernels");
15645 mask = OACC_KERNELS_CLAUSE_MASK;
15646 code = OACC_KERNELS;
15647 break;
15648 case PRAGMA_OACC_PARALLEL:
15649 strcat (p_name, " parallel");
15650 mask = OACC_PARALLEL_CLAUSE_MASK;
15651 code = OACC_PARALLEL;
15652 break;
15653 default:
15654 gcc_unreachable ();
15655 }
15656
15657 if (c_parser_next_token_is (parser, CPP_NAME))
15658 {
15659 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
15660 if (strcmp (p, "loop") == 0)
15661 {
15662 c_parser_consume_token (parser);
15663 tree block = c_begin_omp_parallel ();
15664 tree clauses;
15665 c_parser_oacc_loop (loc, parser, p_name, mask, &clauses, if_p);
15666 return c_finish_omp_construct (loc, code, block, clauses);
15667 }
15668 }
15669
15670 tree clauses = c_parser_oacc_all_clauses (parser, mask, p_name);
15671
15672 tree block = c_begin_omp_parallel ();
15673 add_stmt (c_parser_omp_structured_block (parser, if_p));
15674
15675 return c_finish_omp_construct (loc, code, block, clauses);
15676 }
15677
15678 /* OpenACC 2.0:
15679 # pragma acc routine oacc-routine-clause[optseq] new-line
15680 function-definition
15681
15682 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
15683 */
15684
15685 #define OACC_ROUTINE_CLAUSE_MASK \
15686 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
15687 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
15688 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
15689 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) )
15690
15691 /* Parse an OpenACC routine directive. For named directives, we apply
15692 immediately to the named function. For unnamed ones we then parse
15693 a declaration or definition, which must be for a function. */
15694
15695 static void
15696 c_parser_oacc_routine (c_parser *parser, enum pragma_context context)
15697 {
15698 gcc_checking_assert (context == pragma_external);
15699
15700 oacc_routine_data data;
15701 data.error_seen = false;
15702 data.fndecl_seen = false;
15703 data.clauses = NULL_TREE;
15704 data.loc = c_parser_peek_token (parser)->location;
15705
15706 c_parser_consume_pragma (parser);
15707
15708 /* Look for optional '( name )'. */
15709 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
15710 {
15711 c_parser_consume_token (parser); /* '(' */
15712
15713 tree decl = NULL_TREE;
15714 c_token *name_token = c_parser_peek_token (parser);
15715 location_t name_loc = name_token->location;
15716 if (name_token->type == CPP_NAME
15717 && (name_token->id_kind == C_ID_ID
15718 || name_token->id_kind == C_ID_TYPENAME))
15719 {
15720 decl = lookup_name (name_token->value);
15721 if (!decl)
15722 error_at (name_loc,
15723 "%qE has not been declared", name_token->value);
15724 c_parser_consume_token (parser);
15725 }
15726 else
15727 c_parser_error (parser, "expected function name");
15728
15729 if (!decl
15730 || !c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
15731 {
15732 c_parser_skip_to_pragma_eol (parser, false);
15733 return;
15734 }
15735
15736 data.clauses
15737 = c_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
15738 "#pragma acc routine");
15739
15740 if (TREE_CODE (decl) != FUNCTION_DECL)
15741 {
15742 error_at (name_loc, "%qD does not refer to a function", decl);
15743 return;
15744 }
15745
15746 c_finish_oacc_routine (&data, decl, false);
15747 }
15748 else /* No optional '( name )'. */
15749 {
15750 data.clauses
15751 = c_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
15752 "#pragma acc routine");
15753
15754 /* Emit a helpful diagnostic if there's another pragma following this
15755 one. Also don't allow a static assertion declaration, as in the
15756 following we'll just parse a *single* "declaration or function
15757 definition", and the static assertion counts an one. */
15758 if (c_parser_next_token_is (parser, CPP_PRAGMA)
15759 || c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
15760 {
15761 error_at (data.loc,
15762 "%<#pragma acc routine%> not immediately followed by"
15763 " function declaration or definition");
15764 /* ..., and then just keep going. */
15765 return;
15766 }
15767
15768 /* We only have to consider the pragma_external case here. */
15769 if (c_parser_next_token_is (parser, CPP_KEYWORD)
15770 && c_parser_peek_token (parser)->keyword == RID_EXTENSION)
15771 {
15772 int ext = disable_extension_diagnostics ();
15773 do
15774 c_parser_consume_token (parser);
15775 while (c_parser_next_token_is (parser, CPP_KEYWORD)
15776 && c_parser_peek_token (parser)->keyword == RID_EXTENSION);
15777 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
15778 NULL, vNULL, &data);
15779 restore_extension_diagnostics (ext);
15780 }
15781 else
15782 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
15783 NULL, vNULL, &data);
15784 }
15785 }
15786
15787 /* Finalize an OpenACC routine pragma, applying it to FNDECL.
15788 IS_DEFN is true if we're applying it to the definition. */
15789
15790 static void
15791 c_finish_oacc_routine (struct oacc_routine_data *data, tree fndecl,
15792 bool is_defn)
15793 {
15794 /* Keep going if we're in error reporting mode. */
15795 if (data->error_seen
15796 || fndecl == error_mark_node)
15797 return;
15798
15799 if (data->fndecl_seen)
15800 {
15801 error_at (data->loc,
15802 "%<#pragma acc routine%> not immediately followed by"
15803 " a single function declaration or definition");
15804 data->error_seen = true;
15805 return;
15806 }
15807 if (fndecl == NULL_TREE || TREE_CODE (fndecl) != FUNCTION_DECL)
15808 {
15809 error_at (data->loc,
15810 "%<#pragma acc routine%> not immediately followed by"
15811 " function declaration or definition");
15812 data->error_seen = true;
15813 return;
15814 }
15815
15816 if (oacc_get_fn_attrib (fndecl))
15817 {
15818 error_at (data->loc,
15819 "%<#pragma acc routine%> already applied to %qD", fndecl);
15820 data->error_seen = true;
15821 return;
15822 }
15823
15824 if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
15825 {
15826 error_at (data->loc,
15827 TREE_USED (fndecl)
15828 ? G_("%<#pragma acc routine%> must be applied before use")
15829 : G_("%<#pragma acc routine%> must be applied before "
15830 "definition"));
15831 data->error_seen = true;
15832 return;
15833 }
15834
15835 /* Process the routine's dimension clauses. */
15836 tree dims = oacc_build_routine_dims (data->clauses);
15837 oacc_replace_fn_attrib (fndecl, dims);
15838
15839 /* Add an "omp declare target" attribute. */
15840 DECL_ATTRIBUTES (fndecl)
15841 = tree_cons (get_identifier ("omp declare target"),
15842 NULL_TREE, DECL_ATTRIBUTES (fndecl));
15843
15844 /* Remember that we've used this "#pragma acc routine". */
15845 data->fndecl_seen = true;
15846 }
15847
15848 /* OpenACC 2.0:
15849 # pragma acc update oacc-update-clause[optseq] new-line
15850 */
15851
15852 #define OACC_UPDATE_CLAUSE_MASK \
15853 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
15854 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
15855 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
15856 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
15857 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF_PRESENT) \
15858 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
15859
15860 static void
15861 c_parser_oacc_update (c_parser *parser)
15862 {
15863 location_t loc = c_parser_peek_token (parser)->location;
15864
15865 c_parser_consume_pragma (parser);
15866
15867 tree clauses = c_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
15868 "#pragma acc update");
15869 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
15870 {
15871 error_at (loc,
15872 "%<#pragma acc update%> must contain at least one "
15873 "%<device%> or %<host%> or %<self%> clause");
15874 return;
15875 }
15876
15877 if (parser->error)
15878 return;
15879
15880 tree stmt = make_node (OACC_UPDATE);
15881 TREE_TYPE (stmt) = void_type_node;
15882 OACC_UPDATE_CLAUSES (stmt) = clauses;
15883 SET_EXPR_LOCATION (stmt, loc);
15884 add_stmt (stmt);
15885 }
15886
15887 /* OpenACC 2.0:
15888 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
15889
15890 LOC is the location of the #pragma token.
15891 */
15892
15893 #define OACC_WAIT_CLAUSE_MASK \
15894 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) )
15895
15896 static tree
15897 c_parser_oacc_wait (location_t loc, c_parser *parser, char *p_name)
15898 {
15899 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
15900
15901 if (c_parser_peek_token (parser)->type == CPP_OPEN_PAREN)
15902 list = c_parser_oacc_wait_list (parser, loc, list);
15903
15904 strcpy (p_name, " wait");
15905 clauses = c_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK, p_name);
15906 stmt = c_finish_oacc_wait (loc, list, clauses);
15907 add_stmt (stmt);
15908
15909 return stmt;
15910 }
15911
15912 /* OpenMP 2.5:
15913 # pragma omp atomic new-line
15914 expression-stmt
15915
15916 expression-stmt:
15917 x binop= expr | x++ | ++x | x-- | --x
15918 binop:
15919 +, *, -, /, &, ^, |, <<, >>
15920
15921 where x is an lvalue expression with scalar type.
15922
15923 OpenMP 3.1:
15924 # pragma omp atomic new-line
15925 update-stmt
15926
15927 # pragma omp atomic read new-line
15928 read-stmt
15929
15930 # pragma omp atomic write new-line
15931 write-stmt
15932
15933 # pragma omp atomic update new-line
15934 update-stmt
15935
15936 # pragma omp atomic capture new-line
15937 capture-stmt
15938
15939 # pragma omp atomic capture new-line
15940 capture-block
15941
15942 read-stmt:
15943 v = x
15944 write-stmt:
15945 x = expr
15946 update-stmt:
15947 expression-stmt | x = x binop expr
15948 capture-stmt:
15949 v = expression-stmt
15950 capture-block:
15951 { v = x; update-stmt; } | { update-stmt; v = x; }
15952
15953 OpenMP 4.0:
15954 update-stmt:
15955 expression-stmt | x = x binop expr | x = expr binop x
15956 capture-stmt:
15957 v = update-stmt
15958 capture-block:
15959 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
15960
15961 where x and v are lvalue expressions with scalar type.
15962
15963 LOC is the location of the #pragma token. */
15964
15965 static void
15966 c_parser_omp_atomic (location_t loc, c_parser *parser)
15967 {
15968 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE;
15969 tree lhs1 = NULL_TREE, rhs1 = NULL_TREE;
15970 tree stmt, orig_lhs, unfolded_lhs = NULL_TREE, unfolded_lhs1 = NULL_TREE;
15971 enum tree_code code = ERROR_MARK, opcode = NOP_EXPR;
15972 enum omp_memory_order memory_order = OMP_MEMORY_ORDER_UNSPECIFIED;
15973 struct c_expr expr;
15974 location_t eloc;
15975 bool structured_block = false;
15976 bool swapped = false;
15977 bool non_lvalue_p;
15978 bool first = true;
15979 tree clauses = NULL_TREE;
15980
15981 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
15982 {
15983 if (!first && c_parser_next_token_is (parser, CPP_COMMA))
15984 c_parser_consume_token (parser);
15985
15986 first = false;
15987
15988 if (c_parser_next_token_is (parser, CPP_NAME))
15989 {
15990 const char *p
15991 = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
15992 location_t cloc = c_parser_peek_token (parser)->location;
15993 enum tree_code new_code = ERROR_MARK;
15994 enum omp_memory_order new_memory_order
15995 = OMP_MEMORY_ORDER_UNSPECIFIED;
15996
15997 if (!strcmp (p, "read"))
15998 new_code = OMP_ATOMIC_READ;
15999 else if (!strcmp (p, "write"))
16000 new_code = NOP_EXPR;
16001 else if (!strcmp (p, "update"))
16002 new_code = OMP_ATOMIC;
16003 else if (!strcmp (p, "capture"))
16004 new_code = OMP_ATOMIC_CAPTURE_NEW;
16005 else if (!strcmp (p, "seq_cst"))
16006 new_memory_order = OMP_MEMORY_ORDER_SEQ_CST;
16007 else if (!strcmp (p, "acq_rel"))
16008 new_memory_order = OMP_MEMORY_ORDER_ACQ_REL;
16009 else if (!strcmp (p, "release"))
16010 new_memory_order = OMP_MEMORY_ORDER_RELEASE;
16011 else if (!strcmp (p, "acquire"))
16012 new_memory_order = OMP_MEMORY_ORDER_ACQUIRE;
16013 else if (!strcmp (p, "relaxed"))
16014 new_memory_order = OMP_MEMORY_ORDER_RELAXED;
16015 else if (!strcmp (p, "hint"))
16016 {
16017 c_parser_consume_token (parser);
16018 clauses = c_parser_omp_clause_hint (parser, clauses);
16019 continue;
16020 }
16021 else
16022 {
16023 p = NULL;
16024 error_at (cloc, "expected %<read%>, %<write%>, %<update%>, "
16025 "%<capture%>, %<seq_cst%>, %<acq_rel%>, "
16026 "%<release%>, %<relaxed%> or %<hint%> clause");
16027 }
16028 if (p)
16029 {
16030 if (new_code != ERROR_MARK)
16031 {
16032 if (code != ERROR_MARK)
16033 error_at (cloc, "too many atomic clauses");
16034 else
16035 code = new_code;
16036 }
16037 else if (new_memory_order != OMP_MEMORY_ORDER_UNSPECIFIED)
16038 {
16039 if (memory_order != OMP_MEMORY_ORDER_UNSPECIFIED)
16040 error_at (cloc, "too many memory order clauses");
16041 else
16042 memory_order = new_memory_order;
16043 }
16044 c_parser_consume_token (parser);
16045 continue;
16046 }
16047 }
16048 break;
16049 }
16050 c_parser_skip_to_pragma_eol (parser);
16051
16052 if (code == ERROR_MARK)
16053 code = OMP_ATOMIC;
16054 if (memory_order == OMP_MEMORY_ORDER_UNSPECIFIED)
16055 {
16056 omp_requires_mask
16057 = (enum omp_requires) (omp_requires_mask
16058 | OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER_USED);
16059 switch ((enum omp_memory_order)
16060 (omp_requires_mask & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER))
16061 {
16062 case OMP_MEMORY_ORDER_UNSPECIFIED:
16063 case OMP_MEMORY_ORDER_RELAXED:
16064 memory_order = OMP_MEMORY_ORDER_RELAXED;
16065 break;
16066 case OMP_MEMORY_ORDER_SEQ_CST:
16067 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
16068 break;
16069 case OMP_MEMORY_ORDER_ACQ_REL:
16070 switch (code)
16071 {
16072 case OMP_ATOMIC_READ:
16073 memory_order = OMP_MEMORY_ORDER_ACQUIRE;
16074 break;
16075 case NOP_EXPR: /* atomic write */
16076 case OMP_ATOMIC:
16077 memory_order = OMP_MEMORY_ORDER_RELEASE;
16078 break;
16079 default:
16080 memory_order = OMP_MEMORY_ORDER_ACQ_REL;
16081 break;
16082 }
16083 break;
16084 default:
16085 gcc_unreachable ();
16086 }
16087 }
16088 else
16089 switch (code)
16090 {
16091 case OMP_ATOMIC_READ:
16092 if (memory_order == OMP_MEMORY_ORDER_ACQ_REL
16093 || memory_order == OMP_MEMORY_ORDER_RELEASE)
16094 {
16095 error_at (loc, "%<#pragma omp atomic read%> incompatible with "
16096 "%<acq_rel%> or %<release%> clauses");
16097 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
16098 }
16099 break;
16100 case NOP_EXPR: /* atomic write */
16101 if (memory_order == OMP_MEMORY_ORDER_ACQ_REL
16102 || memory_order == OMP_MEMORY_ORDER_ACQUIRE)
16103 {
16104 error_at (loc, "%<#pragma omp atomic write%> incompatible with "
16105 "%<acq_rel%> or %<acquire%> clauses");
16106 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
16107 }
16108 break;
16109 case OMP_ATOMIC:
16110 if (memory_order == OMP_MEMORY_ORDER_ACQ_REL
16111 || memory_order == OMP_MEMORY_ORDER_ACQUIRE)
16112 {
16113 error_at (loc, "%<#pragma omp atomic update%> incompatible with "
16114 "%<acq_rel%> or %<acquire%> clauses");
16115 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
16116 }
16117 break;
16118 default:
16119 break;
16120 }
16121
16122 switch (code)
16123 {
16124 case OMP_ATOMIC_READ:
16125 case NOP_EXPR: /* atomic write */
16126 v = c_parser_cast_expression (parser, NULL).value;
16127 non_lvalue_p = !lvalue_p (v);
16128 v = c_fully_fold (v, false, NULL, true);
16129 if (v == error_mark_node)
16130 goto saw_error;
16131 if (non_lvalue_p)
16132 v = non_lvalue (v);
16133 loc = c_parser_peek_token (parser)->location;
16134 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
16135 goto saw_error;
16136 if (code == NOP_EXPR)
16137 {
16138 lhs = c_parser_expression (parser).value;
16139 lhs = c_fully_fold (lhs, false, NULL);
16140 if (lhs == error_mark_node)
16141 goto saw_error;
16142 }
16143 else
16144 {
16145 lhs = c_parser_cast_expression (parser, NULL).value;
16146 non_lvalue_p = !lvalue_p (lhs);
16147 lhs = c_fully_fold (lhs, false, NULL, true);
16148 if (lhs == error_mark_node)
16149 goto saw_error;
16150 if (non_lvalue_p)
16151 lhs = non_lvalue (lhs);
16152 }
16153 if (code == NOP_EXPR)
16154 {
16155 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
16156 opcode. */
16157 code = OMP_ATOMIC;
16158 rhs = lhs;
16159 lhs = v;
16160 v = NULL_TREE;
16161 }
16162 goto done;
16163 case OMP_ATOMIC_CAPTURE_NEW:
16164 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
16165 {
16166 c_parser_consume_token (parser);
16167 structured_block = true;
16168 }
16169 else
16170 {
16171 v = c_parser_cast_expression (parser, NULL).value;
16172 non_lvalue_p = !lvalue_p (v);
16173 v = c_fully_fold (v, false, NULL, true);
16174 if (v == error_mark_node)
16175 goto saw_error;
16176 if (non_lvalue_p)
16177 v = non_lvalue (v);
16178 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
16179 goto saw_error;
16180 }
16181 break;
16182 default:
16183 break;
16184 }
16185
16186 /* For structured_block case we don't know yet whether
16187 old or new x should be captured. */
16188 restart:
16189 eloc = c_parser_peek_token (parser)->location;
16190 expr = c_parser_cast_expression (parser, NULL);
16191 lhs = expr.value;
16192 expr = default_function_array_conversion (eloc, expr);
16193 unfolded_lhs = expr.value;
16194 lhs = c_fully_fold (lhs, false, NULL, true);
16195 orig_lhs = lhs;
16196 switch (TREE_CODE (lhs))
16197 {
16198 case ERROR_MARK:
16199 saw_error:
16200 c_parser_skip_to_end_of_block_or_statement (parser);
16201 if (structured_block)
16202 {
16203 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
16204 c_parser_consume_token (parser);
16205 else if (code == OMP_ATOMIC_CAPTURE_NEW)
16206 {
16207 c_parser_skip_to_end_of_block_or_statement (parser);
16208 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
16209 c_parser_consume_token (parser);
16210 }
16211 }
16212 return;
16213
16214 case POSTINCREMENT_EXPR:
16215 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
16216 code = OMP_ATOMIC_CAPTURE_OLD;
16217 /* FALLTHROUGH */
16218 case PREINCREMENT_EXPR:
16219 lhs = TREE_OPERAND (lhs, 0);
16220 unfolded_lhs = NULL_TREE;
16221 opcode = PLUS_EXPR;
16222 rhs = integer_one_node;
16223 break;
16224
16225 case POSTDECREMENT_EXPR:
16226 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
16227 code = OMP_ATOMIC_CAPTURE_OLD;
16228 /* FALLTHROUGH */
16229 case PREDECREMENT_EXPR:
16230 lhs = TREE_OPERAND (lhs, 0);
16231 unfolded_lhs = NULL_TREE;
16232 opcode = MINUS_EXPR;
16233 rhs = integer_one_node;
16234 break;
16235
16236 case COMPOUND_EXPR:
16237 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
16238 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
16239 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
16240 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
16241 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
16242 (TREE_OPERAND (lhs, 1), 0), 0)))
16243 == BOOLEAN_TYPE)
16244 /* Undo effects of boolean_increment for post {in,de}crement. */
16245 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
16246 /* FALLTHRU */
16247 case MODIFY_EXPR:
16248 if (TREE_CODE (lhs) == MODIFY_EXPR
16249 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
16250 {
16251 /* Undo effects of boolean_increment. */
16252 if (integer_onep (TREE_OPERAND (lhs, 1)))
16253 {
16254 /* This is pre or post increment. */
16255 rhs = TREE_OPERAND (lhs, 1);
16256 lhs = TREE_OPERAND (lhs, 0);
16257 unfolded_lhs = NULL_TREE;
16258 opcode = NOP_EXPR;
16259 if (code == OMP_ATOMIC_CAPTURE_NEW
16260 && !structured_block
16261 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
16262 code = OMP_ATOMIC_CAPTURE_OLD;
16263 break;
16264 }
16265 if (TREE_CODE (TREE_OPERAND (lhs, 1)) == TRUTH_NOT_EXPR
16266 && TREE_OPERAND (lhs, 0)
16267 == TREE_OPERAND (TREE_OPERAND (lhs, 1), 0))
16268 {
16269 /* This is pre or post decrement. */
16270 rhs = TREE_OPERAND (lhs, 1);
16271 lhs = TREE_OPERAND (lhs, 0);
16272 unfolded_lhs = NULL_TREE;
16273 opcode = NOP_EXPR;
16274 if (code == OMP_ATOMIC_CAPTURE_NEW
16275 && !structured_block
16276 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
16277 code = OMP_ATOMIC_CAPTURE_OLD;
16278 break;
16279 }
16280 }
16281 /* FALLTHRU */
16282 default:
16283 if (!lvalue_p (unfolded_lhs))
16284 lhs = non_lvalue (lhs);
16285 switch (c_parser_peek_token (parser)->type)
16286 {
16287 case CPP_MULT_EQ:
16288 opcode = MULT_EXPR;
16289 break;
16290 case CPP_DIV_EQ:
16291 opcode = TRUNC_DIV_EXPR;
16292 break;
16293 case CPP_PLUS_EQ:
16294 opcode = PLUS_EXPR;
16295 break;
16296 case CPP_MINUS_EQ:
16297 opcode = MINUS_EXPR;
16298 break;
16299 case CPP_LSHIFT_EQ:
16300 opcode = LSHIFT_EXPR;
16301 break;
16302 case CPP_RSHIFT_EQ:
16303 opcode = RSHIFT_EXPR;
16304 break;
16305 case CPP_AND_EQ:
16306 opcode = BIT_AND_EXPR;
16307 break;
16308 case CPP_OR_EQ:
16309 opcode = BIT_IOR_EXPR;
16310 break;
16311 case CPP_XOR_EQ:
16312 opcode = BIT_XOR_EXPR;
16313 break;
16314 case CPP_EQ:
16315 c_parser_consume_token (parser);
16316 eloc = c_parser_peek_token (parser)->location;
16317 expr = c_parser_expr_no_commas (parser, NULL, unfolded_lhs);
16318 rhs1 = expr.value;
16319 switch (TREE_CODE (rhs1))
16320 {
16321 case MULT_EXPR:
16322 case TRUNC_DIV_EXPR:
16323 case RDIV_EXPR:
16324 case PLUS_EXPR:
16325 case MINUS_EXPR:
16326 case LSHIFT_EXPR:
16327 case RSHIFT_EXPR:
16328 case BIT_AND_EXPR:
16329 case BIT_IOR_EXPR:
16330 case BIT_XOR_EXPR:
16331 if (c_tree_equal (TREE_OPERAND (rhs1, 0), unfolded_lhs))
16332 {
16333 opcode = TREE_CODE (rhs1);
16334 rhs = c_fully_fold (TREE_OPERAND (rhs1, 1), false, NULL,
16335 true);
16336 rhs1 = c_fully_fold (TREE_OPERAND (rhs1, 0), false, NULL,
16337 true);
16338 goto stmt_done;
16339 }
16340 if (c_tree_equal (TREE_OPERAND (rhs1, 1), unfolded_lhs))
16341 {
16342 opcode = TREE_CODE (rhs1);
16343 rhs = c_fully_fold (TREE_OPERAND (rhs1, 0), false, NULL,
16344 true);
16345 rhs1 = c_fully_fold (TREE_OPERAND (rhs1, 1), false, NULL,
16346 true);
16347 swapped = !commutative_tree_code (opcode);
16348 goto stmt_done;
16349 }
16350 break;
16351 case ERROR_MARK:
16352 goto saw_error;
16353 default:
16354 break;
16355 }
16356 if (c_parser_peek_token (parser)->type == CPP_SEMICOLON)
16357 {
16358 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
16359 {
16360 code = OMP_ATOMIC_CAPTURE_OLD;
16361 v = lhs;
16362 lhs = NULL_TREE;
16363 expr = default_function_array_read_conversion (eloc, expr);
16364 unfolded_lhs1 = expr.value;
16365 lhs1 = c_fully_fold (unfolded_lhs1, false, NULL, true);
16366 rhs1 = NULL_TREE;
16367 c_parser_consume_token (parser);
16368 goto restart;
16369 }
16370 if (structured_block)
16371 {
16372 opcode = NOP_EXPR;
16373 expr = default_function_array_read_conversion (eloc, expr);
16374 rhs = c_fully_fold (expr.value, false, NULL, true);
16375 rhs1 = NULL_TREE;
16376 goto stmt_done;
16377 }
16378 }
16379 c_parser_error (parser, "invalid form of %<#pragma omp atomic%>");
16380 goto saw_error;
16381 default:
16382 c_parser_error (parser,
16383 "invalid operator for %<#pragma omp atomic%>");
16384 goto saw_error;
16385 }
16386
16387 /* Arrange to pass the location of the assignment operator to
16388 c_finish_omp_atomic. */
16389 loc = c_parser_peek_token (parser)->location;
16390 c_parser_consume_token (parser);
16391 eloc = c_parser_peek_token (parser)->location;
16392 expr = c_parser_expression (parser);
16393 expr = default_function_array_read_conversion (eloc, expr);
16394 rhs = expr.value;
16395 rhs = c_fully_fold (rhs, false, NULL, true);
16396 break;
16397 }
16398 stmt_done:
16399 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
16400 {
16401 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
16402 goto saw_error;
16403 v = c_parser_cast_expression (parser, NULL).value;
16404 non_lvalue_p = !lvalue_p (v);
16405 v = c_fully_fold (v, false, NULL, true);
16406 if (v == error_mark_node)
16407 goto saw_error;
16408 if (non_lvalue_p)
16409 v = non_lvalue (v);
16410 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
16411 goto saw_error;
16412 eloc = c_parser_peek_token (parser)->location;
16413 expr = c_parser_cast_expression (parser, NULL);
16414 lhs1 = expr.value;
16415 expr = default_function_array_read_conversion (eloc, expr);
16416 unfolded_lhs1 = expr.value;
16417 lhs1 = c_fully_fold (lhs1, false, NULL, true);
16418 if (lhs1 == error_mark_node)
16419 goto saw_error;
16420 if (!lvalue_p (unfolded_lhs1))
16421 lhs1 = non_lvalue (lhs1);
16422 }
16423 if (structured_block)
16424 {
16425 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
16426 c_parser_require (parser, CPP_CLOSE_BRACE, "expected %<}%>");
16427 }
16428 done:
16429 if (unfolded_lhs && unfolded_lhs1
16430 && !c_tree_equal (unfolded_lhs, unfolded_lhs1))
16431 {
16432 error ("%<#pragma omp atomic capture%> uses two different "
16433 "expressions for memory");
16434 stmt = error_mark_node;
16435 }
16436 else
16437 stmt = c_finish_omp_atomic (loc, code, opcode, lhs, rhs, v, lhs1, rhs1,
16438 swapped, memory_order);
16439 if (stmt != error_mark_node)
16440 add_stmt (stmt);
16441
16442 if (!structured_block)
16443 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
16444 }
16445
16446
16447 /* OpenMP 2.5:
16448 # pragma omp barrier new-line
16449 */
16450
16451 static void
16452 c_parser_omp_barrier (c_parser *parser)
16453 {
16454 location_t loc = c_parser_peek_token (parser)->location;
16455 c_parser_consume_pragma (parser);
16456 c_parser_skip_to_pragma_eol (parser);
16457
16458 c_finish_omp_barrier (loc);
16459 }
16460
16461 /* OpenMP 2.5:
16462 # pragma omp critical [(name)] new-line
16463 structured-block
16464
16465 OpenMP 4.5:
16466 # pragma omp critical [(name) [hint(expression)]] new-line
16467
16468 LOC is the location of the #pragma itself. */
16469
16470 #define OMP_CRITICAL_CLAUSE_MASK \
16471 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
16472
16473 static tree
16474 c_parser_omp_critical (location_t loc, c_parser *parser, bool *if_p)
16475 {
16476 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
16477
16478 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
16479 {
16480 c_parser_consume_token (parser);
16481 if (c_parser_next_token_is (parser, CPP_NAME))
16482 {
16483 name = c_parser_peek_token (parser)->value;
16484 c_parser_consume_token (parser);
16485 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
16486 }
16487 else
16488 c_parser_error (parser, "expected identifier");
16489
16490 if (c_parser_next_token_is (parser, CPP_COMMA)
16491 && c_parser_peek_2nd_token (parser)->type == CPP_NAME)
16492 c_parser_consume_token (parser);
16493
16494 clauses = c_parser_omp_all_clauses (parser,
16495 OMP_CRITICAL_CLAUSE_MASK,
16496 "#pragma omp critical");
16497 }
16498 else
16499 {
16500 if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
16501 c_parser_error (parser, "expected %<(%> or end of line");
16502 c_parser_skip_to_pragma_eol (parser);
16503 }
16504
16505 stmt = c_parser_omp_structured_block (parser, if_p);
16506 return c_finish_omp_critical (loc, stmt, name, clauses);
16507 }
16508
16509 /* OpenMP 5.0:
16510 # pragma omp depobj ( depobj ) depobj-clause new-line
16511
16512 depobj-clause:
16513 depend (dependence-type : locator)
16514 destroy
16515 update (dependence-type)
16516
16517 dependence-type:
16518 in
16519 out
16520 inout
16521 mutexinout */
16522
16523 static void
16524 c_parser_omp_depobj (c_parser *parser)
16525 {
16526 location_t loc = c_parser_peek_token (parser)->location;
16527 c_parser_consume_pragma (parser);
16528 matching_parens parens;
16529 if (!parens.require_open (parser))
16530 {
16531 c_parser_skip_to_pragma_eol (parser);
16532 return;
16533 }
16534
16535 tree depobj = c_parser_expr_no_commas (parser, NULL).value;
16536 if (depobj != error_mark_node)
16537 {
16538 if (!lvalue_p (depobj))
16539 {
16540 error_at (EXPR_LOC_OR_LOC (depobj, loc),
16541 "%<depobj%> expression is not lvalue expression");
16542 depobj = error_mark_node;
16543 }
16544 else
16545 {
16546 tree addr = build_unary_op (EXPR_LOC_OR_LOC (depobj, loc), ADDR_EXPR,
16547 depobj, false);
16548 if (addr == error_mark_node)
16549 depobj = error_mark_node;
16550 else
16551 depobj = build_indirect_ref (EXPR_LOC_OR_LOC (depobj, loc),
16552 addr, RO_UNARY_STAR);
16553 }
16554 }
16555
16556 parens.skip_until_found_close (parser);
16557 tree clause = NULL_TREE;
16558 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_SOURCE;
16559 location_t c_loc = c_parser_peek_token (parser)->location;
16560 if (c_parser_next_token_is (parser, CPP_NAME))
16561 {
16562 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16563
16564 c_parser_consume_token (parser);
16565 if (!strcmp ("depend", p))
16566 {
16567 clause = c_parser_omp_clause_depend (parser, NULL_TREE);
16568 clause = c_finish_omp_clauses (clause, C_ORT_OMP);
16569 if (!clause)
16570 clause = error_mark_node;
16571 }
16572 else if (!strcmp ("destroy", p))
16573 kind = OMP_CLAUSE_DEPEND_LAST;
16574 else if (!strcmp ("update", p))
16575 {
16576 matching_parens c_parens;
16577 if (c_parens.require_open (parser))
16578 {
16579 location_t c2_loc = c_parser_peek_token (parser)->location;
16580 if (c_parser_next_token_is (parser, CPP_NAME))
16581 {
16582 const char *p2
16583 = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16584
16585 c_parser_consume_token (parser);
16586 if (!strcmp ("in", p2))
16587 kind = OMP_CLAUSE_DEPEND_IN;
16588 else if (!strcmp ("out", p2))
16589 kind = OMP_CLAUSE_DEPEND_OUT;
16590 else if (!strcmp ("inout", p2))
16591 kind = OMP_CLAUSE_DEPEND_INOUT;
16592 else if (!strcmp ("mutexinoutset", p2))
16593 kind = OMP_CLAUSE_DEPEND_MUTEXINOUTSET;
16594 }
16595 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
16596 {
16597 clause = error_mark_node;
16598 error_at (c2_loc, "expected %<in%>, %<out%>, %<inout%> or "
16599 "%<mutexinoutset%>");
16600 }
16601 c_parens.skip_until_found_close (parser);
16602 }
16603 else
16604 clause = error_mark_node;
16605 }
16606 }
16607 if (!clause && kind == OMP_CLAUSE_DEPEND_SOURCE)
16608 {
16609 clause = error_mark_node;
16610 error_at (c_loc, "expected %<depend%>, %<destroy%> or %<update%> clause");
16611 }
16612 c_parser_skip_to_pragma_eol (parser);
16613
16614 c_finish_omp_depobj (loc, depobj, kind, clause);
16615 }
16616
16617
16618 /* OpenMP 2.5:
16619 # pragma omp flush flush-vars[opt] new-line
16620
16621 flush-vars:
16622 ( variable-list )
16623
16624 OpenMP 5.0:
16625 # pragma omp flush memory-order-clause new-line */
16626
16627 static void
16628 c_parser_omp_flush (c_parser *parser)
16629 {
16630 location_t loc = c_parser_peek_token (parser)->location;
16631 c_parser_consume_pragma (parser);
16632 enum memmodel mo = MEMMODEL_LAST;
16633 if (c_parser_next_token_is (parser, CPP_NAME))
16634 {
16635 const char *p
16636 = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16637
16638 if (!strcmp (p, "acq_rel"))
16639 mo = MEMMODEL_ACQ_REL;
16640 else if (!strcmp (p, "release"))
16641 mo = MEMMODEL_RELEASE;
16642 else if (!strcmp (p, "acquire"))
16643 mo = MEMMODEL_ACQUIRE;
16644 else
16645 error_at (c_parser_peek_token (parser)->location,
16646 "expected %<acq_rel%>, %<release%> or %<acquire%>");
16647 c_parser_consume_token (parser);
16648 }
16649 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
16650 {
16651 if (mo != MEMMODEL_LAST)
16652 error_at (c_parser_peek_token (parser)->location,
16653 "%<flush%> list specified together with memory order "
16654 "clause");
16655 c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
16656 }
16657 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
16658 c_parser_error (parser, "expected %<(%> or end of line");
16659 c_parser_skip_to_pragma_eol (parser);
16660
16661 c_finish_omp_flush (loc, mo);
16662 }
16663
16664 /* Parse the restricted form of loop statements allowed by OpenACC and OpenMP.
16665 The real trick here is to determine the loop control variable early
16666 so that we can push a new decl if necessary to make it private.
16667 LOC is the location of the "acc" or "omp" in "#pragma acc" or "#pragma omp",
16668 respectively. */
16669
16670 static tree
16671 c_parser_omp_for_loop (location_t loc, c_parser *parser, enum tree_code code,
16672 tree clauses, tree *cclauses, bool *if_p)
16673 {
16674 tree decl, cond, incr, save_break, save_cont, body, init, stmt, cl;
16675 tree declv, condv, incrv, initv, ret = NULL_TREE;
16676 tree pre_body = NULL_TREE, this_pre_body;
16677 tree ordered_cl = NULL_TREE;
16678 bool fail = false, open_brace_parsed = false;
16679 int i, collapse = 1, ordered = 0, count, nbraces = 0;
16680 location_t for_loc;
16681 bool tiling = false;
16682 vec<tree, va_gc> *for_block = make_tree_vector ();
16683
16684 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
16685 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
16686 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
16687 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_TILE)
16688 {
16689 tiling = true;
16690 collapse = list_length (OMP_CLAUSE_TILE_LIST (cl));
16691 }
16692 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
16693 && OMP_CLAUSE_ORDERED_EXPR (cl))
16694 {
16695 ordered_cl = cl;
16696 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
16697 }
16698
16699 if (ordered && ordered < collapse)
16700 {
16701 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
16702 "%<ordered%> clause parameter is less than %<collapse%>");
16703 OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
16704 = build_int_cst (NULL_TREE, collapse);
16705 ordered = collapse;
16706 }
16707 if (ordered)
16708 {
16709 for (tree *pc = &clauses; *pc; )
16710 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
16711 {
16712 error_at (OMP_CLAUSE_LOCATION (*pc),
16713 "%<linear%> clause may not be specified together "
16714 "with %<ordered%> clause with a parameter");
16715 *pc = OMP_CLAUSE_CHAIN (*pc);
16716 }
16717 else
16718 pc = &OMP_CLAUSE_CHAIN (*pc);
16719 }
16720
16721 gcc_assert (tiling || (collapse >= 1 && ordered >= 0));
16722 count = ordered ? ordered : collapse;
16723
16724 declv = make_tree_vec (count);
16725 initv = make_tree_vec (count);
16726 condv = make_tree_vec (count);
16727 incrv = make_tree_vec (count);
16728
16729 if (!c_parser_next_token_is_keyword (parser, RID_FOR))
16730 {
16731 c_parser_error (parser, "for statement expected");
16732 return NULL;
16733 }
16734 for_loc = c_parser_peek_token (parser)->location;
16735 c_parser_consume_token (parser);
16736
16737 for (i = 0; i < count; i++)
16738 {
16739 int bracecount = 0;
16740
16741 matching_parens parens;
16742 if (!parens.require_open (parser))
16743 goto pop_scopes;
16744
16745 /* Parse the initialization declaration or expression. */
16746 if (c_parser_next_tokens_start_declaration (parser))
16747 {
16748 if (i > 0)
16749 vec_safe_push (for_block, c_begin_compound_stmt (true));
16750 this_pre_body = push_stmt_list ();
16751 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
16752 NULL, vNULL);
16753 if (this_pre_body)
16754 {
16755 this_pre_body = pop_stmt_list (this_pre_body);
16756 if (pre_body)
16757 {
16758 tree t = pre_body;
16759 pre_body = push_stmt_list ();
16760 add_stmt (t);
16761 add_stmt (this_pre_body);
16762 pre_body = pop_stmt_list (pre_body);
16763 }
16764 else
16765 pre_body = this_pre_body;
16766 }
16767 decl = check_for_loop_decls (for_loc, flag_isoc99);
16768 if (decl == NULL)
16769 goto error_init;
16770 if (DECL_INITIAL (decl) == error_mark_node)
16771 decl = error_mark_node;
16772 init = decl;
16773 }
16774 else if (c_parser_next_token_is (parser, CPP_NAME)
16775 && c_parser_peek_2nd_token (parser)->type == CPP_EQ)
16776 {
16777 struct c_expr decl_exp;
16778 struct c_expr init_exp;
16779 location_t init_loc;
16780
16781 decl_exp = c_parser_postfix_expression (parser);
16782 decl = decl_exp.value;
16783
16784 c_parser_require (parser, CPP_EQ, "expected %<=%>");
16785
16786 init_loc = c_parser_peek_token (parser)->location;
16787 init_exp = c_parser_expr_no_commas (parser, NULL);
16788 init_exp = default_function_array_read_conversion (init_loc,
16789 init_exp);
16790 init = build_modify_expr (init_loc, decl, decl_exp.original_type,
16791 NOP_EXPR, init_loc, init_exp.value,
16792 init_exp.original_type);
16793 init = c_process_expr_stmt (init_loc, init);
16794
16795 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
16796 }
16797 else
16798 {
16799 error_init:
16800 c_parser_error (parser,
16801 "expected iteration declaration or initialization");
16802 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
16803 "expected %<)%>");
16804 fail = true;
16805 goto parse_next;
16806 }
16807
16808 /* Parse the loop condition. */
16809 cond = NULL_TREE;
16810 if (c_parser_next_token_is_not (parser, CPP_SEMICOLON))
16811 {
16812 location_t cond_loc = c_parser_peek_token (parser)->location;
16813 struct c_expr cond_expr
16814 = c_parser_binary_expression (parser, NULL, NULL_TREE);
16815
16816 cond = cond_expr.value;
16817 cond = c_objc_common_truthvalue_conversion (cond_loc, cond);
16818 if (COMPARISON_CLASS_P (cond))
16819 {
16820 tree op0 = TREE_OPERAND (cond, 0), op1 = TREE_OPERAND (cond, 1);
16821 op0 = c_fully_fold (op0, false, NULL);
16822 op1 = c_fully_fold (op1, false, NULL);
16823 TREE_OPERAND (cond, 0) = op0;
16824 TREE_OPERAND (cond, 1) = op1;
16825 }
16826 switch (cond_expr.original_code)
16827 {
16828 case GT_EXPR:
16829 case GE_EXPR:
16830 case LT_EXPR:
16831 case LE_EXPR:
16832 break;
16833 case NE_EXPR:
16834 if (code != OACC_LOOP)
16835 break;
16836 /* FALLTHRU. */
16837 default:
16838 /* Can't be cond = error_mark_node, because we want to preserve
16839 the location until c_finish_omp_for. */
16840 cond = build1 (NOP_EXPR, boolean_type_node, error_mark_node);
16841 break;
16842 }
16843 protected_set_expr_location (cond, cond_loc);
16844 }
16845 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
16846
16847 /* Parse the increment expression. */
16848 incr = NULL_TREE;
16849 if (c_parser_next_token_is_not (parser, CPP_CLOSE_PAREN))
16850 {
16851 location_t incr_loc = c_parser_peek_token (parser)->location;
16852
16853 incr = c_process_expr_stmt (incr_loc,
16854 c_parser_expression (parser).value);
16855 }
16856 parens.skip_until_found_close (parser);
16857
16858 if (decl == NULL || decl == error_mark_node || init == error_mark_node)
16859 fail = true;
16860 else
16861 {
16862 TREE_VEC_ELT (declv, i) = decl;
16863 TREE_VEC_ELT (initv, i) = init;
16864 TREE_VEC_ELT (condv, i) = cond;
16865 TREE_VEC_ELT (incrv, i) = incr;
16866 }
16867
16868 parse_next:
16869 if (i == count - 1)
16870 break;
16871
16872 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
16873 in between the collapsed for loops to be still considered perfectly
16874 nested. Hopefully the final version clarifies this.
16875 For now handle (multiple) {'s and empty statements. */
16876 do
16877 {
16878 if (c_parser_next_token_is_keyword (parser, RID_FOR))
16879 {
16880 c_parser_consume_token (parser);
16881 break;
16882 }
16883 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
16884 {
16885 c_parser_consume_token (parser);
16886 bracecount++;
16887 }
16888 else if (bracecount
16889 && c_parser_next_token_is (parser, CPP_SEMICOLON))
16890 c_parser_consume_token (parser);
16891 else
16892 {
16893 c_parser_error (parser, "not enough perfectly nested loops");
16894 if (bracecount)
16895 {
16896 open_brace_parsed = true;
16897 bracecount--;
16898 }
16899 fail = true;
16900 count = 0;
16901 break;
16902 }
16903 }
16904 while (1);
16905
16906 nbraces += bracecount;
16907 }
16908
16909 if (nbraces)
16910 if_p = NULL;
16911
16912 save_break = c_break_label;
16913 c_break_label = size_one_node;
16914 save_cont = c_cont_label;
16915 c_cont_label = NULL_TREE;
16916 body = push_stmt_list ();
16917
16918 if (open_brace_parsed)
16919 {
16920 location_t here = c_parser_peek_token (parser)->location;
16921 stmt = c_begin_compound_stmt (true);
16922 c_parser_compound_statement_nostart (parser);
16923 add_stmt (c_end_compound_stmt (here, stmt, true));
16924 }
16925 else
16926 add_stmt (c_parser_c99_block_statement (parser, if_p));
16927 if (c_cont_label)
16928 {
16929 tree t = build1 (LABEL_EXPR, void_type_node, c_cont_label);
16930 SET_EXPR_LOCATION (t, loc);
16931 add_stmt (t);
16932 }
16933
16934 body = pop_stmt_list (body);
16935 c_break_label = save_break;
16936 c_cont_label = save_cont;
16937
16938 while (nbraces)
16939 {
16940 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
16941 {
16942 c_parser_consume_token (parser);
16943 nbraces--;
16944 }
16945 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
16946 c_parser_consume_token (parser);
16947 else
16948 {
16949 c_parser_error (parser, "collapsed loops not perfectly nested");
16950 while (nbraces)
16951 {
16952 location_t here = c_parser_peek_token (parser)->location;
16953 stmt = c_begin_compound_stmt (true);
16954 add_stmt (body);
16955 c_parser_compound_statement_nostart (parser);
16956 body = c_end_compound_stmt (here, stmt, true);
16957 nbraces--;
16958 }
16959 goto pop_scopes;
16960 }
16961 }
16962
16963 /* Only bother calling c_finish_omp_for if we haven't already generated
16964 an error from the initialization parsing. */
16965 if (!fail)
16966 {
16967 stmt = c_finish_omp_for (loc, code, declv, NULL, initv, condv,
16968 incrv, body, pre_body, true);
16969
16970 /* Check for iterators appearing in lb, b or incr expressions. */
16971 if (stmt && !c_omp_check_loop_iv (stmt, declv, NULL))
16972 stmt = NULL_TREE;
16973
16974 if (stmt)
16975 {
16976 add_stmt (stmt);
16977
16978 if (cclauses != NULL
16979 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL)
16980 {
16981 tree *c;
16982 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
16983 if (OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_FIRSTPRIVATE
16984 && OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_LASTPRIVATE)
16985 c = &OMP_CLAUSE_CHAIN (*c);
16986 else
16987 {
16988 for (i = 0; i < count; i++)
16989 if (TREE_VEC_ELT (declv, i) == OMP_CLAUSE_DECL (*c))
16990 break;
16991 if (i == count)
16992 c = &OMP_CLAUSE_CHAIN (*c);
16993 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE)
16994 {
16995 error_at (loc,
16996 "iteration variable %qD should not be firstprivate",
16997 OMP_CLAUSE_DECL (*c));
16998 *c = OMP_CLAUSE_CHAIN (*c);
16999 }
17000 else
17001 {
17002 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
17003 tree l = *c;
17004 *c = OMP_CLAUSE_CHAIN (*c);
17005 if (code == OMP_SIMD)
17006 {
17007 OMP_CLAUSE_CHAIN (l)
17008 = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
17009 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
17010 }
17011 else
17012 {
17013 OMP_CLAUSE_CHAIN (l) = clauses;
17014 clauses = l;
17015 }
17016 }
17017 }
17018 }
17019 OMP_FOR_CLAUSES (stmt) = clauses;
17020 }
17021 ret = stmt;
17022 }
17023 pop_scopes:
17024 while (!for_block->is_empty ())
17025 {
17026 /* FIXME diagnostics: LOC below should be the actual location of
17027 this particular for block. We need to build a list of
17028 locations to go along with FOR_BLOCK. */
17029 stmt = c_end_compound_stmt (loc, for_block->pop (), true);
17030 add_stmt (stmt);
17031 }
17032 release_tree_vector (for_block);
17033 return ret;
17034 }
17035
17036 /* Helper function for OpenMP parsing, split clauses and call
17037 finish_omp_clauses on each of the set of clauses afterwards. */
17038
17039 static void
17040 omp_split_clauses (location_t loc, enum tree_code code,
17041 omp_clause_mask mask, tree clauses, tree *cclauses)
17042 {
17043 int i;
17044 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
17045 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
17046 if (cclauses[i])
17047 cclauses[i] = c_finish_omp_clauses (cclauses[i], C_ORT_OMP);
17048 }
17049
17050 /* OpenMP 4.0:
17051 #pragma omp simd simd-clause[optseq] new-line
17052 for-loop
17053
17054 LOC is the location of the #pragma token.
17055 */
17056
17057 #define OMP_SIMD_CLAUSE_MASK \
17058 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
17059 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
17060 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
17061 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
17062 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
17063 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
17064 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
17065 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
17066 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
17067 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NONTEMPORAL))
17068
17069 static tree
17070 c_parser_omp_simd (location_t loc, c_parser *parser,
17071 char *p_name, omp_clause_mask mask, tree *cclauses,
17072 bool *if_p)
17073 {
17074 tree block, clauses, ret;
17075
17076 strcat (p_name, " simd");
17077 mask |= OMP_SIMD_CLAUSE_MASK;
17078
17079 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
17080 if (cclauses)
17081 {
17082 omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
17083 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
17084 tree c = omp_find_clause (cclauses[C_OMP_CLAUSE_SPLIT_FOR],
17085 OMP_CLAUSE_ORDERED);
17086 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
17087 {
17088 error_at (OMP_CLAUSE_LOCATION (c),
17089 "%<ordered%> clause with parameter may not be specified "
17090 "on %qs construct", p_name);
17091 OMP_CLAUSE_ORDERED_EXPR (c) = NULL_TREE;
17092 }
17093 }
17094
17095 block = c_begin_compound_stmt (true);
17096 ret = c_parser_omp_for_loop (loc, parser, OMP_SIMD, clauses, cclauses, if_p);
17097 block = c_end_compound_stmt (loc, block, true);
17098 add_stmt (block);
17099
17100 return ret;
17101 }
17102
17103 /* OpenMP 2.5:
17104 #pragma omp for for-clause[optseq] new-line
17105 for-loop
17106
17107 OpenMP 4.0:
17108 #pragma omp for simd for-simd-clause[optseq] new-line
17109 for-loop
17110
17111 LOC is the location of the #pragma token.
17112 */
17113
17114 #define OMP_FOR_CLAUSE_MASK \
17115 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
17116 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
17117 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
17118 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
17119 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
17120 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
17121 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
17122 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
17123 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
17124
17125 static tree
17126 c_parser_omp_for (location_t loc, c_parser *parser,
17127 char *p_name, omp_clause_mask mask, tree *cclauses,
17128 bool *if_p)
17129 {
17130 tree block, clauses, ret;
17131
17132 strcat (p_name, " for");
17133 mask |= OMP_FOR_CLAUSE_MASK;
17134 /* parallel for{, simd} disallows nowait clause, but for
17135 target {teams distribute ,}parallel for{, simd} it should be accepted. */
17136 if (cclauses && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
17137 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
17138 /* Composite distribute parallel for{, simd} disallows ordered clause. */
17139 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
17140 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
17141
17142 if (c_parser_next_token_is (parser, CPP_NAME))
17143 {
17144 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
17145
17146 if (strcmp (p, "simd") == 0)
17147 {
17148 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
17149 if (cclauses == NULL)
17150 cclauses = cclauses_buf;
17151
17152 c_parser_consume_token (parser);
17153 if (!flag_openmp) /* flag_openmp_simd */
17154 return c_parser_omp_simd (loc, parser, p_name, mask, cclauses,
17155 if_p);
17156 block = c_begin_compound_stmt (true);
17157 ret = c_parser_omp_simd (loc, parser, p_name, mask, cclauses, if_p);
17158 block = c_end_compound_stmt (loc, block, true);
17159 if (ret == NULL_TREE)
17160 return ret;
17161 ret = make_node (OMP_FOR);
17162 TREE_TYPE (ret) = void_type_node;
17163 OMP_FOR_BODY (ret) = block;
17164 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
17165 SET_EXPR_LOCATION (ret, loc);
17166 add_stmt (ret);
17167 return ret;
17168 }
17169 }
17170 if (!flag_openmp) /* flag_openmp_simd */
17171 {
17172 c_parser_skip_to_pragma_eol (parser, false);
17173 return NULL_TREE;
17174 }
17175
17176 /* Composite distribute parallel for disallows linear clause. */
17177 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
17178 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
17179
17180 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
17181 if (cclauses)
17182 {
17183 omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
17184 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
17185 }
17186
17187 block = c_begin_compound_stmt (true);
17188 ret = c_parser_omp_for_loop (loc, parser, OMP_FOR, clauses, cclauses, if_p);
17189 block = c_end_compound_stmt (loc, block, true);
17190 add_stmt (block);
17191
17192 return ret;
17193 }
17194
17195 static tree c_parser_omp_taskloop (location_t, c_parser *, char *,
17196 omp_clause_mask, tree *, bool *);
17197
17198 /* OpenMP 2.5:
17199 # pragma omp master new-line
17200 structured-block
17201
17202 LOC is the location of the #pragma token.
17203 */
17204
17205 static tree
17206 c_parser_omp_master (location_t loc, c_parser *parser,
17207 char *p_name, omp_clause_mask mask, tree *cclauses,
17208 bool *if_p)
17209 {
17210 tree block, clauses, ret;
17211
17212 strcat (p_name, " master");
17213
17214 if (c_parser_next_token_is (parser, CPP_NAME))
17215 {
17216 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
17217
17218 if (strcmp (p, "taskloop") == 0)
17219 {
17220 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
17221 if (cclauses == NULL)
17222 cclauses = cclauses_buf;
17223
17224 c_parser_consume_token (parser);
17225 if (!flag_openmp) /* flag_openmp_simd */
17226 return c_parser_omp_taskloop (loc, parser, p_name, mask, cclauses,
17227 if_p);
17228 block = c_begin_compound_stmt (true);
17229 ret = c_parser_omp_taskloop (loc, parser, p_name, mask, cclauses,
17230 if_p);
17231 block = c_end_compound_stmt (loc, block, true);
17232 if (ret == NULL_TREE)
17233 return ret;
17234 ret = c_finish_omp_master (loc, block);
17235 return ret;
17236 }
17237 }
17238 if (!flag_openmp) /* flag_openmp_simd */
17239 {
17240 c_parser_skip_to_pragma_eol (parser, false);
17241 return NULL_TREE;
17242 }
17243
17244 if (cclauses)
17245 {
17246 clauses = c_parser_omp_all_clauses (parser, mask, p_name, false);
17247 omp_split_clauses (loc, OMP_MASTER, mask, clauses, cclauses);
17248 }
17249 else
17250 c_parser_skip_to_pragma_eol (parser);
17251
17252 return c_finish_omp_master (loc, c_parser_omp_structured_block (parser,
17253 if_p));
17254 }
17255
17256 /* OpenMP 2.5:
17257 # pragma omp ordered new-line
17258 structured-block
17259
17260 OpenMP 4.5:
17261 # pragma omp ordered ordered-clauses new-line
17262 structured-block
17263
17264 # pragma omp ordered depend-clauses new-line */
17265
17266 #define OMP_ORDERED_CLAUSE_MASK \
17267 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
17268 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
17269
17270 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
17271 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
17272
17273 static bool
17274 c_parser_omp_ordered (c_parser *parser, enum pragma_context context,
17275 bool *if_p)
17276 {
17277 location_t loc = c_parser_peek_token (parser)->location;
17278 c_parser_consume_pragma (parser);
17279
17280 if (context != pragma_stmt && context != pragma_compound)
17281 {
17282 c_parser_error (parser, "expected declaration specifiers");
17283 c_parser_skip_to_pragma_eol (parser, false);
17284 return false;
17285 }
17286
17287 if (c_parser_next_token_is (parser, CPP_NAME))
17288 {
17289 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
17290
17291 if (!strcmp ("depend", p))
17292 {
17293 if (!flag_openmp) /* flag_openmp_simd */
17294 {
17295 c_parser_skip_to_pragma_eol (parser, false);
17296 return false;
17297 }
17298 if (context == pragma_stmt)
17299 {
17300 error_at (loc,
17301 "%<#pragma omp ordered%> with %<depend%> clause may "
17302 "only be used in compound statements");
17303 c_parser_skip_to_pragma_eol (parser, false);
17304 return false;
17305 }
17306
17307 tree clauses
17308 = c_parser_omp_all_clauses (parser,
17309 OMP_ORDERED_DEPEND_CLAUSE_MASK,
17310 "#pragma omp ordered");
17311 c_finish_omp_ordered (loc, clauses, NULL_TREE);
17312 return false;
17313 }
17314 }
17315
17316 tree clauses = c_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
17317 "#pragma omp ordered");
17318
17319 if (!flag_openmp /* flag_openmp_simd */
17320 && omp_find_clause (clauses, OMP_CLAUSE_SIMD) == NULL_TREE)
17321 return false;
17322
17323 c_finish_omp_ordered (loc, clauses,
17324 c_parser_omp_structured_block (parser, if_p));
17325 return true;
17326 }
17327
17328 /* OpenMP 2.5:
17329
17330 section-scope:
17331 { section-sequence }
17332
17333 section-sequence:
17334 section-directive[opt] structured-block
17335 section-sequence section-directive structured-block
17336
17337 SECTIONS_LOC is the location of the #pragma omp sections. */
17338
17339 static tree
17340 c_parser_omp_sections_scope (location_t sections_loc, c_parser *parser)
17341 {
17342 tree stmt, substmt;
17343 bool error_suppress = false;
17344 location_t loc;
17345
17346 loc = c_parser_peek_token (parser)->location;
17347 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
17348 {
17349 /* Avoid skipping until the end of the block. */
17350 parser->error = false;
17351 return NULL_TREE;
17352 }
17353
17354 stmt = push_stmt_list ();
17355
17356 if (c_parser_peek_token (parser)->pragma_kind != PRAGMA_OMP_SECTION)
17357 {
17358 substmt = c_parser_omp_structured_block (parser, NULL);
17359 substmt = build1 (OMP_SECTION, void_type_node, substmt);
17360 SET_EXPR_LOCATION (substmt, loc);
17361 add_stmt (substmt);
17362 }
17363
17364 while (1)
17365 {
17366 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
17367 break;
17368 if (c_parser_next_token_is (parser, CPP_EOF))
17369 break;
17370
17371 loc = c_parser_peek_token (parser)->location;
17372 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_OMP_SECTION)
17373 {
17374 c_parser_consume_pragma (parser);
17375 c_parser_skip_to_pragma_eol (parser);
17376 error_suppress = false;
17377 }
17378 else if (!error_suppress)
17379 {
17380 error_at (loc, "expected %<#pragma omp section%> or %<}%>");
17381 error_suppress = true;
17382 }
17383
17384 substmt = c_parser_omp_structured_block (parser, NULL);
17385 substmt = build1 (OMP_SECTION, void_type_node, substmt);
17386 SET_EXPR_LOCATION (substmt, loc);
17387 add_stmt (substmt);
17388 }
17389 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE,
17390 "expected %<#pragma omp section%> or %<}%>");
17391
17392 substmt = pop_stmt_list (stmt);
17393
17394 stmt = make_node (OMP_SECTIONS);
17395 SET_EXPR_LOCATION (stmt, sections_loc);
17396 TREE_TYPE (stmt) = void_type_node;
17397 OMP_SECTIONS_BODY (stmt) = substmt;
17398
17399 return add_stmt (stmt);
17400 }
17401
17402 /* OpenMP 2.5:
17403 # pragma omp sections sections-clause[optseq] newline
17404 sections-scope
17405
17406 LOC is the location of the #pragma token.
17407 */
17408
17409 #define OMP_SECTIONS_CLAUSE_MASK \
17410 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
17411 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
17412 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
17413 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
17414 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
17415
17416 static tree
17417 c_parser_omp_sections (location_t loc, c_parser *parser,
17418 char *p_name, omp_clause_mask mask, tree *cclauses)
17419 {
17420 tree block, clauses, ret;
17421
17422 strcat (p_name, " sections");
17423 mask |= OMP_SECTIONS_CLAUSE_MASK;
17424 if (cclauses)
17425 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
17426
17427 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
17428 if (cclauses)
17429 {
17430 omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
17431 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
17432 }
17433
17434 block = c_begin_compound_stmt (true);
17435 ret = c_parser_omp_sections_scope (loc, parser);
17436 if (ret)
17437 OMP_SECTIONS_CLAUSES (ret) = clauses;
17438 block = c_end_compound_stmt (loc, block, true);
17439 add_stmt (block);
17440
17441 return ret;
17442 }
17443
17444 /* OpenMP 2.5:
17445 # pragma omp parallel parallel-clause[optseq] new-line
17446 structured-block
17447 # pragma omp parallel for parallel-for-clause[optseq] new-line
17448 structured-block
17449 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
17450 structured-block
17451
17452 OpenMP 4.0:
17453 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
17454 structured-block
17455
17456 LOC is the location of the #pragma token.
17457 */
17458
17459 #define OMP_PARALLEL_CLAUSE_MASK \
17460 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
17461 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
17462 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
17463 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
17464 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
17465 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
17466 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
17467 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
17468 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
17469
17470 static tree
17471 c_parser_omp_parallel (location_t loc, c_parser *parser,
17472 char *p_name, omp_clause_mask mask, tree *cclauses,
17473 bool *if_p)
17474 {
17475 tree stmt, clauses, block;
17476
17477 strcat (p_name, " parallel");
17478 mask |= OMP_PARALLEL_CLAUSE_MASK;
17479 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
17480 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
17481 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
17482 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
17483
17484 if (c_parser_next_token_is_keyword (parser, RID_FOR))
17485 {
17486 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
17487 if (cclauses == NULL)
17488 cclauses = cclauses_buf;
17489
17490 c_parser_consume_token (parser);
17491 if (!flag_openmp) /* flag_openmp_simd */
17492 return c_parser_omp_for (loc, parser, p_name, mask, cclauses, if_p);
17493 block = c_begin_omp_parallel ();
17494 tree ret = c_parser_omp_for (loc, parser, p_name, mask, cclauses, if_p);
17495 stmt
17496 = c_finish_omp_parallel (loc, cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
17497 block);
17498 if (ret == NULL_TREE)
17499 return ret;
17500 OMP_PARALLEL_COMBINED (stmt) = 1;
17501 return stmt;
17502 }
17503 /* When combined with distribute, parallel has to be followed by for.
17504 #pragma omp target parallel is allowed though. */
17505 else if (cclauses
17506 && (mask & (OMP_CLAUSE_MASK_1
17507 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
17508 {
17509 error_at (loc, "expected %<for%> after %qs", p_name);
17510 c_parser_skip_to_pragma_eol (parser);
17511 return NULL_TREE;
17512 }
17513 else if (cclauses == NULL && c_parser_next_token_is (parser, CPP_NAME))
17514 {
17515 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
17516 if (strcmp (p, "master") == 0)
17517 {
17518 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
17519 cclauses = cclauses_buf;
17520
17521 c_parser_consume_token (parser);
17522 if (!flag_openmp) /* flag_openmp_simd */
17523 return c_parser_omp_master (loc, parser, p_name, mask, cclauses,
17524 if_p);
17525 block = c_begin_omp_parallel ();
17526 tree ret = c_parser_omp_master (loc, parser, p_name, mask, cclauses,
17527 if_p);
17528 stmt = c_finish_omp_parallel (loc,
17529 cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
17530 block);
17531 OMP_PARALLEL_COMBINED (stmt) = 1;
17532 if (ret == NULL)
17533 return ret;
17534 return stmt;
17535 }
17536 else if (!flag_openmp) /* flag_openmp_simd */
17537 {
17538 c_parser_skip_to_pragma_eol (parser, false);
17539 return NULL_TREE;
17540 }
17541 else if (strcmp (p, "sections") == 0)
17542 {
17543 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
17544 cclauses = cclauses_buf;
17545
17546 c_parser_consume_token (parser);
17547 block = c_begin_omp_parallel ();
17548 c_parser_omp_sections (loc, parser, p_name, mask, cclauses);
17549 stmt = c_finish_omp_parallel (loc,
17550 cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
17551 block);
17552 OMP_PARALLEL_COMBINED (stmt) = 1;
17553 return stmt;
17554 }
17555 }
17556 else if (!flag_openmp) /* flag_openmp_simd */
17557 {
17558 c_parser_skip_to_pragma_eol (parser, false);
17559 return NULL_TREE;
17560 }
17561
17562 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
17563 if (cclauses)
17564 {
17565 omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
17566 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
17567 }
17568
17569 block = c_begin_omp_parallel ();
17570 c_parser_statement (parser, if_p);
17571 stmt = c_finish_omp_parallel (loc, clauses, block);
17572
17573 return stmt;
17574 }
17575
17576 /* OpenMP 2.5:
17577 # pragma omp single single-clause[optseq] new-line
17578 structured-block
17579
17580 LOC is the location of the #pragma.
17581 */
17582
17583 #define OMP_SINGLE_CLAUSE_MASK \
17584 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
17585 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
17586 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
17587 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
17588
17589 static tree
17590 c_parser_omp_single (location_t loc, c_parser *parser, bool *if_p)
17591 {
17592 tree stmt = make_node (OMP_SINGLE);
17593 SET_EXPR_LOCATION (stmt, loc);
17594 TREE_TYPE (stmt) = void_type_node;
17595
17596 OMP_SINGLE_CLAUSES (stmt)
17597 = c_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
17598 "#pragma omp single");
17599 OMP_SINGLE_BODY (stmt) = c_parser_omp_structured_block (parser, if_p);
17600
17601 return add_stmt (stmt);
17602 }
17603
17604 /* OpenMP 3.0:
17605 # pragma omp task task-clause[optseq] new-line
17606
17607 LOC is the location of the #pragma.
17608 */
17609
17610 #define OMP_TASK_CLAUSE_MASK \
17611 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
17612 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
17613 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
17614 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
17615 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
17616 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
17617 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
17618 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
17619 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
17620 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY) \
17621 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION))
17622
17623 static tree
17624 c_parser_omp_task (location_t loc, c_parser *parser, bool *if_p)
17625 {
17626 tree clauses, block;
17627
17628 clauses = c_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
17629 "#pragma omp task");
17630
17631 block = c_begin_omp_task ();
17632 c_parser_statement (parser, if_p);
17633 return c_finish_omp_task (loc, clauses, block);
17634 }
17635
17636 /* OpenMP 3.0:
17637 # pragma omp taskwait new-line
17638
17639 OpenMP 5.0:
17640 # pragma omp taskwait taskwait-clause[optseq] new-line
17641 */
17642
17643 #define OMP_TASKWAIT_CLAUSE_MASK \
17644 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
17645
17646 static void
17647 c_parser_omp_taskwait (c_parser *parser)
17648 {
17649 location_t loc = c_parser_peek_token (parser)->location;
17650 c_parser_consume_pragma (parser);
17651
17652 tree clauses
17653 = c_parser_omp_all_clauses (parser, OMP_TASKWAIT_CLAUSE_MASK,
17654 "#pragma omp taskwait");
17655
17656 if (clauses)
17657 {
17658 tree stmt = make_node (OMP_TASK);
17659 TREE_TYPE (stmt) = void_node;
17660 OMP_TASK_CLAUSES (stmt) = clauses;
17661 OMP_TASK_BODY (stmt) = NULL_TREE;
17662 SET_EXPR_LOCATION (stmt, loc);
17663 add_stmt (stmt);
17664 }
17665 else
17666 c_finish_omp_taskwait (loc);
17667 }
17668
17669 /* OpenMP 3.1:
17670 # pragma omp taskyield new-line
17671 */
17672
17673 static void
17674 c_parser_omp_taskyield (c_parser *parser)
17675 {
17676 location_t loc = c_parser_peek_token (parser)->location;
17677 c_parser_consume_pragma (parser);
17678 c_parser_skip_to_pragma_eol (parser);
17679
17680 c_finish_omp_taskyield (loc);
17681 }
17682
17683 /* OpenMP 4.0:
17684 # pragma omp taskgroup new-line
17685
17686 OpenMP 5.0:
17687 # pragma omp taskgroup taskgroup-clause[optseq] new-line
17688 */
17689
17690 #define OMP_TASKGROUP_CLAUSE_MASK \
17691 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASK_REDUCTION))
17692
17693 static tree
17694 c_parser_omp_taskgroup (location_t loc, c_parser *parser, bool *if_p)
17695 {
17696 tree clauses = c_parser_omp_all_clauses (parser, OMP_TASKGROUP_CLAUSE_MASK,
17697 "#pragma omp taskgroup");
17698
17699 tree body = c_parser_omp_structured_block (parser, if_p);
17700 return c_finish_omp_taskgroup (loc, body, clauses);
17701 }
17702
17703 /* OpenMP 4.0:
17704 # pragma omp cancel cancel-clause[optseq] new-line
17705
17706 LOC is the location of the #pragma.
17707 */
17708
17709 #define OMP_CANCEL_CLAUSE_MASK \
17710 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
17711 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
17712 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
17713 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
17714 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
17715
17716 static void
17717 c_parser_omp_cancel (c_parser *parser)
17718 {
17719 location_t loc = c_parser_peek_token (parser)->location;
17720
17721 c_parser_consume_pragma (parser);
17722 tree clauses = c_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
17723 "#pragma omp cancel");
17724
17725 c_finish_omp_cancel (loc, clauses);
17726 }
17727
17728 /* OpenMP 4.0:
17729 # pragma omp cancellation point cancelpt-clause[optseq] new-line
17730
17731 LOC is the location of the #pragma.
17732 */
17733
17734 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
17735 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
17736 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
17737 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
17738 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
17739
17740 static void
17741 c_parser_omp_cancellation_point (c_parser *parser, enum pragma_context context)
17742 {
17743 location_t loc = c_parser_peek_token (parser)->location;
17744 tree clauses;
17745 bool point_seen = false;
17746
17747 c_parser_consume_pragma (parser);
17748 if (c_parser_next_token_is (parser, CPP_NAME))
17749 {
17750 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
17751 if (strcmp (p, "point") == 0)
17752 {
17753 c_parser_consume_token (parser);
17754 point_seen = true;
17755 }
17756 }
17757 if (!point_seen)
17758 {
17759 c_parser_error (parser, "expected %<point%>");
17760 c_parser_skip_to_pragma_eol (parser);
17761 return;
17762 }
17763
17764 if (context != pragma_compound)
17765 {
17766 if (context == pragma_stmt)
17767 error_at (loc,
17768 "%<#pragma %s%> may only be used in compound statements",
17769 "omp cancellation point");
17770 else
17771 c_parser_error (parser, "expected declaration specifiers");
17772 c_parser_skip_to_pragma_eol (parser, false);
17773 return;
17774 }
17775
17776 clauses
17777 = c_parser_omp_all_clauses (parser, OMP_CANCELLATION_POINT_CLAUSE_MASK,
17778 "#pragma omp cancellation point");
17779
17780 c_finish_omp_cancellation_point (loc, clauses);
17781 }
17782
17783 /* OpenMP 4.0:
17784 #pragma omp distribute distribute-clause[optseq] new-line
17785 for-loop */
17786
17787 #define OMP_DISTRIBUTE_CLAUSE_MASK \
17788 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
17789 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
17790 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
17791 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
17792 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
17793
17794 static tree
17795 c_parser_omp_distribute (location_t loc, c_parser *parser,
17796 char *p_name, omp_clause_mask mask, tree *cclauses,
17797 bool *if_p)
17798 {
17799 tree clauses, block, ret;
17800
17801 strcat (p_name, " distribute");
17802 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
17803
17804 if (c_parser_next_token_is (parser, CPP_NAME))
17805 {
17806 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
17807 bool simd = false;
17808 bool parallel = false;
17809
17810 if (strcmp (p, "simd") == 0)
17811 simd = true;
17812 else
17813 parallel = strcmp (p, "parallel") == 0;
17814 if (parallel || simd)
17815 {
17816 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
17817 if (cclauses == NULL)
17818 cclauses = cclauses_buf;
17819 c_parser_consume_token (parser);
17820 if (!flag_openmp) /* flag_openmp_simd */
17821 {
17822 if (simd)
17823 return c_parser_omp_simd (loc, parser, p_name, mask, cclauses,
17824 if_p);
17825 else
17826 return c_parser_omp_parallel (loc, parser, p_name, mask,
17827 cclauses, if_p);
17828 }
17829 block = c_begin_compound_stmt (true);
17830 if (simd)
17831 ret = c_parser_omp_simd (loc, parser, p_name, mask, cclauses,
17832 if_p);
17833 else
17834 ret = c_parser_omp_parallel (loc, parser, p_name, mask, cclauses,
17835 if_p);
17836 block = c_end_compound_stmt (loc, block, true);
17837 if (ret == NULL)
17838 return ret;
17839 ret = make_node (OMP_DISTRIBUTE);
17840 TREE_TYPE (ret) = void_type_node;
17841 OMP_FOR_BODY (ret) = block;
17842 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
17843 SET_EXPR_LOCATION (ret, loc);
17844 add_stmt (ret);
17845 return ret;
17846 }
17847 }
17848 if (!flag_openmp) /* flag_openmp_simd */
17849 {
17850 c_parser_skip_to_pragma_eol (parser, false);
17851 return NULL_TREE;
17852 }
17853
17854 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
17855 if (cclauses)
17856 {
17857 omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
17858 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
17859 }
17860
17861 block = c_begin_compound_stmt (true);
17862 ret = c_parser_omp_for_loop (loc, parser, OMP_DISTRIBUTE, clauses, NULL,
17863 if_p);
17864 block = c_end_compound_stmt (loc, block, true);
17865 add_stmt (block);
17866
17867 return ret;
17868 }
17869
17870 /* OpenMP 4.0:
17871 # pragma omp teams teams-clause[optseq] new-line
17872 structured-block */
17873
17874 #define OMP_TEAMS_CLAUSE_MASK \
17875 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
17876 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
17877 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
17878 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
17879 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
17880 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
17881 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
17882
17883 static tree
17884 c_parser_omp_teams (location_t loc, c_parser *parser,
17885 char *p_name, omp_clause_mask mask, tree *cclauses,
17886 bool *if_p)
17887 {
17888 tree clauses, block, ret;
17889
17890 strcat (p_name, " teams");
17891 mask |= OMP_TEAMS_CLAUSE_MASK;
17892
17893 if (c_parser_next_token_is (parser, CPP_NAME))
17894 {
17895 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
17896 if (strcmp (p, "distribute") == 0)
17897 {
17898 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
17899 if (cclauses == NULL)
17900 cclauses = cclauses_buf;
17901
17902 c_parser_consume_token (parser);
17903 if (!flag_openmp) /* flag_openmp_simd */
17904 return c_parser_omp_distribute (loc, parser, p_name, mask,
17905 cclauses, if_p);
17906 block = c_begin_omp_parallel ();
17907 ret = c_parser_omp_distribute (loc, parser, p_name, mask, cclauses,
17908 if_p);
17909 block = c_end_compound_stmt (loc, block, true);
17910 if (ret == NULL)
17911 return ret;
17912 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
17913 ret = make_node (OMP_TEAMS);
17914 TREE_TYPE (ret) = void_type_node;
17915 OMP_TEAMS_CLAUSES (ret) = clauses;
17916 OMP_TEAMS_BODY (ret) = block;
17917 OMP_TEAMS_COMBINED (ret) = 1;
17918 SET_EXPR_LOCATION (ret, loc);
17919 return add_stmt (ret);
17920 }
17921 }
17922 if (!flag_openmp) /* flag_openmp_simd */
17923 {
17924 c_parser_skip_to_pragma_eol (parser, false);
17925 return NULL_TREE;
17926 }
17927
17928 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
17929 if (cclauses)
17930 {
17931 omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
17932 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
17933 }
17934
17935 tree stmt = make_node (OMP_TEAMS);
17936 TREE_TYPE (stmt) = void_type_node;
17937 OMP_TEAMS_CLAUSES (stmt) = clauses;
17938 block = c_begin_omp_parallel ();
17939 add_stmt (c_parser_omp_structured_block (parser, if_p));
17940 OMP_TEAMS_BODY (stmt) = c_end_compound_stmt (loc, block, true);
17941 SET_EXPR_LOCATION (stmt, loc);
17942
17943 return add_stmt (stmt);
17944 }
17945
17946 /* OpenMP 4.0:
17947 # pragma omp target data target-data-clause[optseq] new-line
17948 structured-block */
17949
17950 #define OMP_TARGET_DATA_CLAUSE_MASK \
17951 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
17952 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
17953 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
17954 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
17955
17956 static tree
17957 c_parser_omp_target_data (location_t loc, c_parser *parser, bool *if_p)
17958 {
17959 tree clauses
17960 = c_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
17961 "#pragma omp target data");
17962 int map_seen = 0;
17963 for (tree *pc = &clauses; *pc;)
17964 {
17965 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
17966 switch (OMP_CLAUSE_MAP_KIND (*pc))
17967 {
17968 case GOMP_MAP_TO:
17969 case GOMP_MAP_ALWAYS_TO:
17970 case GOMP_MAP_FROM:
17971 case GOMP_MAP_ALWAYS_FROM:
17972 case GOMP_MAP_TOFROM:
17973 case GOMP_MAP_ALWAYS_TOFROM:
17974 case GOMP_MAP_ALLOC:
17975 map_seen = 3;
17976 break;
17977 case GOMP_MAP_FIRSTPRIVATE_POINTER:
17978 case GOMP_MAP_ALWAYS_POINTER:
17979 break;
17980 default:
17981 map_seen |= 1;
17982 error_at (OMP_CLAUSE_LOCATION (*pc),
17983 "%<#pragma omp target data%> with map-type other "
17984 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
17985 "on %<map%> clause");
17986 *pc = OMP_CLAUSE_CHAIN (*pc);
17987 continue;
17988 }
17989 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_USE_DEVICE_PTR)
17990 map_seen = 3;
17991 pc = &OMP_CLAUSE_CHAIN (*pc);
17992 }
17993
17994 if (map_seen != 3)
17995 {
17996 if (map_seen == 0)
17997 error_at (loc,
17998 "%<#pragma omp target data%> must contain at least "
17999 "one %<map%> or %<use_device_ptr%> clause");
18000 return NULL_TREE;
18001 }
18002
18003 tree stmt = make_node (OMP_TARGET_DATA);
18004 TREE_TYPE (stmt) = void_type_node;
18005 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
18006 keep_next_level ();
18007 tree block = c_begin_compound_stmt (true);
18008 add_stmt (c_parser_omp_structured_block (parser, if_p));
18009 OMP_TARGET_DATA_BODY (stmt) = c_end_compound_stmt (loc, block, true);
18010
18011 SET_EXPR_LOCATION (stmt, loc);
18012 return add_stmt (stmt);
18013 }
18014
18015 /* OpenMP 4.0:
18016 # pragma omp target update target-update-clause[optseq] new-line */
18017
18018 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
18019 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
18020 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
18021 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
18022 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
18023 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
18024 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
18025
18026 static bool
18027 c_parser_omp_target_update (location_t loc, c_parser *parser,
18028 enum pragma_context context)
18029 {
18030 if (context == pragma_stmt)
18031 {
18032 error_at (loc, "%<#pragma %s%> may only be used in compound statements",
18033 "omp target update");
18034 c_parser_skip_to_pragma_eol (parser, false);
18035 return false;
18036 }
18037
18038 tree clauses
18039 = c_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
18040 "#pragma omp target update");
18041 if (omp_find_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
18042 && omp_find_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
18043 {
18044 error_at (loc,
18045 "%<#pragma omp target update%> must contain at least one "
18046 "%<from%> or %<to%> clauses");
18047 return false;
18048 }
18049
18050 tree stmt = make_node (OMP_TARGET_UPDATE);
18051 TREE_TYPE (stmt) = void_type_node;
18052 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
18053 SET_EXPR_LOCATION (stmt, loc);
18054 add_stmt (stmt);
18055 return false;
18056 }
18057
18058 /* OpenMP 4.5:
18059 # pragma omp target enter data target-data-clause[optseq] new-line */
18060
18061 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
18062 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
18063 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
18064 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
18065 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
18066 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
18067
18068 static tree
18069 c_parser_omp_target_enter_data (location_t loc, c_parser *parser,
18070 enum pragma_context context)
18071 {
18072 bool data_seen = false;
18073 if (c_parser_next_token_is (parser, CPP_NAME))
18074 {
18075 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
18076 if (strcmp (p, "data") == 0)
18077 {
18078 c_parser_consume_token (parser);
18079 data_seen = true;
18080 }
18081 }
18082 if (!data_seen)
18083 {
18084 c_parser_error (parser, "expected %<data%>");
18085 c_parser_skip_to_pragma_eol (parser);
18086 return NULL_TREE;
18087 }
18088
18089 if (context == pragma_stmt)
18090 {
18091 error_at (loc, "%<#pragma %s%> may only be used in compound statements",
18092 "omp target enter data");
18093 c_parser_skip_to_pragma_eol (parser, false);
18094 return NULL_TREE;
18095 }
18096
18097 tree clauses
18098 = c_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
18099 "#pragma omp target enter data");
18100 int map_seen = 0;
18101 for (tree *pc = &clauses; *pc;)
18102 {
18103 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
18104 switch (OMP_CLAUSE_MAP_KIND (*pc))
18105 {
18106 case GOMP_MAP_TO:
18107 case GOMP_MAP_ALWAYS_TO:
18108 case GOMP_MAP_ALLOC:
18109 map_seen = 3;
18110 break;
18111 case GOMP_MAP_FIRSTPRIVATE_POINTER:
18112 case GOMP_MAP_ALWAYS_POINTER:
18113 break;
18114 default:
18115 map_seen |= 1;
18116 error_at (OMP_CLAUSE_LOCATION (*pc),
18117 "%<#pragma omp target enter data%> with map-type other "
18118 "than %<to%> or %<alloc%> on %<map%> clause");
18119 *pc = OMP_CLAUSE_CHAIN (*pc);
18120 continue;
18121 }
18122 pc = &OMP_CLAUSE_CHAIN (*pc);
18123 }
18124
18125 if (map_seen != 3)
18126 {
18127 if (map_seen == 0)
18128 error_at (loc,
18129 "%<#pragma omp target enter data%> must contain at least "
18130 "one %<map%> clause");
18131 return NULL_TREE;
18132 }
18133
18134 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
18135 TREE_TYPE (stmt) = void_type_node;
18136 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
18137 SET_EXPR_LOCATION (stmt, loc);
18138 add_stmt (stmt);
18139 return stmt;
18140 }
18141
18142 /* OpenMP 4.5:
18143 # pragma omp target exit data target-data-clause[optseq] new-line */
18144
18145 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
18146 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
18147 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
18148 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
18149 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
18150 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
18151
18152 static tree
18153 c_parser_omp_target_exit_data (location_t loc, c_parser *parser,
18154 enum pragma_context context)
18155 {
18156 bool data_seen = false;
18157 if (c_parser_next_token_is (parser, CPP_NAME))
18158 {
18159 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
18160 if (strcmp (p, "data") == 0)
18161 {
18162 c_parser_consume_token (parser);
18163 data_seen = true;
18164 }
18165 }
18166 if (!data_seen)
18167 {
18168 c_parser_error (parser, "expected %<data%>");
18169 c_parser_skip_to_pragma_eol (parser);
18170 return NULL_TREE;
18171 }
18172
18173 if (context == pragma_stmt)
18174 {
18175 error_at (loc, "%<#pragma %s%> may only be used in compound statements",
18176 "omp target exit data");
18177 c_parser_skip_to_pragma_eol (parser, false);
18178 return NULL_TREE;
18179 }
18180
18181 tree clauses
18182 = c_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
18183 "#pragma omp target exit data");
18184
18185 int map_seen = 0;
18186 for (tree *pc = &clauses; *pc;)
18187 {
18188 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
18189 switch (OMP_CLAUSE_MAP_KIND (*pc))
18190 {
18191 case GOMP_MAP_FROM:
18192 case GOMP_MAP_ALWAYS_FROM:
18193 case GOMP_MAP_RELEASE:
18194 case GOMP_MAP_DELETE:
18195 map_seen = 3;
18196 break;
18197 case GOMP_MAP_FIRSTPRIVATE_POINTER:
18198 case GOMP_MAP_ALWAYS_POINTER:
18199 break;
18200 default:
18201 map_seen |= 1;
18202 error_at (OMP_CLAUSE_LOCATION (*pc),
18203 "%<#pragma omp target exit data%> with map-type other "
18204 "than %<from%>, %<release%> or %<delete%> on %<map%>"
18205 " clause");
18206 *pc = OMP_CLAUSE_CHAIN (*pc);
18207 continue;
18208 }
18209 pc = &OMP_CLAUSE_CHAIN (*pc);
18210 }
18211
18212 if (map_seen != 3)
18213 {
18214 if (map_seen == 0)
18215 error_at (loc,
18216 "%<#pragma omp target exit data%> must contain at least one "
18217 "%<map%> clause");
18218 return NULL_TREE;
18219 }
18220
18221 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
18222 TREE_TYPE (stmt) = void_type_node;
18223 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
18224 SET_EXPR_LOCATION (stmt, loc);
18225 add_stmt (stmt);
18226 return stmt;
18227 }
18228
18229 /* OpenMP 4.0:
18230 # pragma omp target target-clause[optseq] new-line
18231 structured-block */
18232
18233 #define OMP_TARGET_CLAUSE_MASK \
18234 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
18235 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
18236 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
18237 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
18238 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
18239 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
18240 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
18241 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
18242 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
18243
18244 static bool
18245 c_parser_omp_target (c_parser *parser, enum pragma_context context, bool *if_p)
18246 {
18247 location_t loc = c_parser_peek_token (parser)->location;
18248 c_parser_consume_pragma (parser);
18249 tree *pc = NULL, stmt, block;
18250
18251 if (context != pragma_stmt && context != pragma_compound)
18252 {
18253 c_parser_error (parser, "expected declaration specifiers");
18254 c_parser_skip_to_pragma_eol (parser);
18255 return false;
18256 }
18257
18258 if (flag_openmp)
18259 omp_requires_mask
18260 = (enum omp_requires) (omp_requires_mask | OMP_REQUIRES_TARGET_USED);
18261
18262 if (c_parser_next_token_is (parser, CPP_NAME))
18263 {
18264 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
18265 enum tree_code ccode = ERROR_MARK;
18266
18267 if (strcmp (p, "teams") == 0)
18268 ccode = OMP_TEAMS;
18269 else if (strcmp (p, "parallel") == 0)
18270 ccode = OMP_PARALLEL;
18271 else if (strcmp (p, "simd") == 0)
18272 ccode = OMP_SIMD;
18273 if (ccode != ERROR_MARK)
18274 {
18275 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
18276 char p_name[sizeof ("#pragma omp target teams distribute "
18277 "parallel for simd")];
18278
18279 c_parser_consume_token (parser);
18280 strcpy (p_name, "#pragma omp target");
18281 if (!flag_openmp) /* flag_openmp_simd */
18282 {
18283 tree stmt;
18284 switch (ccode)
18285 {
18286 case OMP_TEAMS:
18287 stmt = c_parser_omp_teams (loc, parser, p_name,
18288 OMP_TARGET_CLAUSE_MASK,
18289 cclauses, if_p);
18290 break;
18291 case OMP_PARALLEL:
18292 stmt = c_parser_omp_parallel (loc, parser, p_name,
18293 OMP_TARGET_CLAUSE_MASK,
18294 cclauses, if_p);
18295 break;
18296 case OMP_SIMD:
18297 stmt = c_parser_omp_simd (loc, parser, p_name,
18298 OMP_TARGET_CLAUSE_MASK,
18299 cclauses, if_p);
18300 break;
18301 default:
18302 gcc_unreachable ();
18303 }
18304 return stmt != NULL_TREE;
18305 }
18306 keep_next_level ();
18307 tree block = c_begin_compound_stmt (true), ret;
18308 switch (ccode)
18309 {
18310 case OMP_TEAMS:
18311 ret = c_parser_omp_teams (loc, parser, p_name,
18312 OMP_TARGET_CLAUSE_MASK, cclauses,
18313 if_p);
18314 break;
18315 case OMP_PARALLEL:
18316 ret = c_parser_omp_parallel (loc, parser, p_name,
18317 OMP_TARGET_CLAUSE_MASK, cclauses,
18318 if_p);
18319 break;
18320 case OMP_SIMD:
18321 ret = c_parser_omp_simd (loc, parser, p_name,
18322 OMP_TARGET_CLAUSE_MASK, cclauses,
18323 if_p);
18324 break;
18325 default:
18326 gcc_unreachable ();
18327 }
18328 block = c_end_compound_stmt (loc, block, true);
18329 if (ret == NULL_TREE)
18330 return false;
18331 if (ccode == OMP_TEAMS)
18332 {
18333 /* For combined target teams, ensure the num_teams and
18334 thread_limit clause expressions are evaluated on the host,
18335 before entering the target construct. */
18336 tree c;
18337 for (c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
18338 c; c = OMP_CLAUSE_CHAIN (c))
18339 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
18340 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
18341 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
18342 {
18343 tree expr = OMP_CLAUSE_OPERAND (c, 0);
18344 tree tmp = create_tmp_var_raw (TREE_TYPE (expr));
18345 expr = build4 (TARGET_EXPR, TREE_TYPE (expr), tmp,
18346 expr, NULL_TREE, NULL_TREE);
18347 add_stmt (expr);
18348 OMP_CLAUSE_OPERAND (c, 0) = expr;
18349 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
18350 OMP_CLAUSE_FIRSTPRIVATE);
18351 OMP_CLAUSE_DECL (tc) = tmp;
18352 OMP_CLAUSE_CHAIN (tc)
18353 = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
18354 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
18355 }
18356 }
18357 tree stmt = make_node (OMP_TARGET);
18358 TREE_TYPE (stmt) = void_type_node;
18359 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
18360 OMP_TARGET_BODY (stmt) = block;
18361 OMP_TARGET_COMBINED (stmt) = 1;
18362 SET_EXPR_LOCATION (stmt, loc);
18363 add_stmt (stmt);
18364 pc = &OMP_TARGET_CLAUSES (stmt);
18365 goto check_clauses;
18366 }
18367 else if (!flag_openmp) /* flag_openmp_simd */
18368 {
18369 c_parser_skip_to_pragma_eol (parser, false);
18370 return false;
18371 }
18372 else if (strcmp (p, "data") == 0)
18373 {
18374 c_parser_consume_token (parser);
18375 c_parser_omp_target_data (loc, parser, if_p);
18376 return true;
18377 }
18378 else if (strcmp (p, "enter") == 0)
18379 {
18380 c_parser_consume_token (parser);
18381 c_parser_omp_target_enter_data (loc, parser, context);
18382 return false;
18383 }
18384 else if (strcmp (p, "exit") == 0)
18385 {
18386 c_parser_consume_token (parser);
18387 c_parser_omp_target_exit_data (loc, parser, context);
18388 return false;
18389 }
18390 else if (strcmp (p, "update") == 0)
18391 {
18392 c_parser_consume_token (parser);
18393 return c_parser_omp_target_update (loc, parser, context);
18394 }
18395 }
18396 if (!flag_openmp) /* flag_openmp_simd */
18397 {
18398 c_parser_skip_to_pragma_eol (parser, false);
18399 return false;
18400 }
18401
18402 stmt = make_node (OMP_TARGET);
18403 TREE_TYPE (stmt) = void_type_node;
18404
18405 OMP_TARGET_CLAUSES (stmt)
18406 = c_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
18407 "#pragma omp target");
18408 pc = &OMP_TARGET_CLAUSES (stmt);
18409 keep_next_level ();
18410 block = c_begin_compound_stmt (true);
18411 add_stmt (c_parser_omp_structured_block (parser, if_p));
18412 OMP_TARGET_BODY (stmt) = c_end_compound_stmt (loc, block, true);
18413
18414 SET_EXPR_LOCATION (stmt, loc);
18415 add_stmt (stmt);
18416
18417 check_clauses:
18418 while (*pc)
18419 {
18420 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
18421 switch (OMP_CLAUSE_MAP_KIND (*pc))
18422 {
18423 case GOMP_MAP_TO:
18424 case GOMP_MAP_ALWAYS_TO:
18425 case GOMP_MAP_FROM:
18426 case GOMP_MAP_ALWAYS_FROM:
18427 case GOMP_MAP_TOFROM:
18428 case GOMP_MAP_ALWAYS_TOFROM:
18429 case GOMP_MAP_ALLOC:
18430 case GOMP_MAP_FIRSTPRIVATE_POINTER:
18431 case GOMP_MAP_ALWAYS_POINTER:
18432 break;
18433 default:
18434 error_at (OMP_CLAUSE_LOCATION (*pc),
18435 "%<#pragma omp target%> with map-type other "
18436 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
18437 "on %<map%> clause");
18438 *pc = OMP_CLAUSE_CHAIN (*pc);
18439 continue;
18440 }
18441 pc = &OMP_CLAUSE_CHAIN (*pc);
18442 }
18443 return true;
18444 }
18445
18446 /* OpenMP 4.0:
18447 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
18448
18449 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
18450 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
18451 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
18452 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
18453 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
18454 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
18455 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
18456
18457 static void
18458 c_parser_omp_declare_simd (c_parser *parser, enum pragma_context context)
18459 {
18460 auto_vec<c_token> clauses;
18461 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
18462 {
18463 c_token *token = c_parser_peek_token (parser);
18464 if (token->type == CPP_EOF)
18465 {
18466 c_parser_skip_to_pragma_eol (parser);
18467 return;
18468 }
18469 clauses.safe_push (*token);
18470 c_parser_consume_token (parser);
18471 }
18472 clauses.safe_push (*c_parser_peek_token (parser));
18473 c_parser_skip_to_pragma_eol (parser);
18474
18475 while (c_parser_next_token_is (parser, CPP_PRAGMA))
18476 {
18477 if (c_parser_peek_token (parser)->pragma_kind
18478 != PRAGMA_OMP_DECLARE
18479 || c_parser_peek_2nd_token (parser)->type != CPP_NAME
18480 || strcmp (IDENTIFIER_POINTER
18481 (c_parser_peek_2nd_token (parser)->value),
18482 "simd") != 0)
18483 {
18484 c_parser_error (parser,
18485 "%<#pragma omp declare simd%> must be followed by "
18486 "function declaration or definition or another "
18487 "%<#pragma omp declare simd%>");
18488 return;
18489 }
18490 c_parser_consume_pragma (parser);
18491 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
18492 {
18493 c_token *token = c_parser_peek_token (parser);
18494 if (token->type == CPP_EOF)
18495 {
18496 c_parser_skip_to_pragma_eol (parser);
18497 return;
18498 }
18499 clauses.safe_push (*token);
18500 c_parser_consume_token (parser);
18501 }
18502 clauses.safe_push (*c_parser_peek_token (parser));
18503 c_parser_skip_to_pragma_eol (parser);
18504 }
18505
18506 /* Make sure nothing tries to read past the end of the tokens. */
18507 c_token eof_token;
18508 memset (&eof_token, 0, sizeof (eof_token));
18509 eof_token.type = CPP_EOF;
18510 clauses.safe_push (eof_token);
18511 clauses.safe_push (eof_token);
18512
18513 switch (context)
18514 {
18515 case pragma_external:
18516 if (c_parser_next_token_is (parser, CPP_KEYWORD)
18517 && c_parser_peek_token (parser)->keyword == RID_EXTENSION)
18518 {
18519 int ext = disable_extension_diagnostics ();
18520 do
18521 c_parser_consume_token (parser);
18522 while (c_parser_next_token_is (parser, CPP_KEYWORD)
18523 && c_parser_peek_token (parser)->keyword == RID_EXTENSION);
18524 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
18525 NULL, clauses);
18526 restore_extension_diagnostics (ext);
18527 }
18528 else
18529 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
18530 NULL, clauses);
18531 break;
18532 case pragma_struct:
18533 case pragma_param:
18534 case pragma_stmt:
18535 c_parser_error (parser, "%<#pragma omp declare simd%> must be followed by "
18536 "function declaration or definition");
18537 break;
18538 case pragma_compound:
18539 if (c_parser_next_token_is (parser, CPP_KEYWORD)
18540 && c_parser_peek_token (parser)->keyword == RID_EXTENSION)
18541 {
18542 int ext = disable_extension_diagnostics ();
18543 do
18544 c_parser_consume_token (parser);
18545 while (c_parser_next_token_is (parser, CPP_KEYWORD)
18546 && c_parser_peek_token (parser)->keyword == RID_EXTENSION);
18547 if (c_parser_next_tokens_start_declaration (parser))
18548 {
18549 c_parser_declaration_or_fndef (parser, true, true, true, true,
18550 true, NULL, clauses);
18551 restore_extension_diagnostics (ext);
18552 break;
18553 }
18554 restore_extension_diagnostics (ext);
18555 }
18556 else if (c_parser_next_tokens_start_declaration (parser))
18557 {
18558 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
18559 NULL, clauses);
18560 break;
18561 }
18562 c_parser_error (parser, "%<#pragma omp declare simd%> must be followed by "
18563 "function declaration or definition");
18564 break;
18565 default:
18566 gcc_unreachable ();
18567 }
18568 }
18569
18570 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
18571 and put that into "omp declare simd" attribute. */
18572
18573 static void
18574 c_finish_omp_declare_simd (c_parser *parser, tree fndecl, tree parms,
18575 vec<c_token> clauses)
18576 {
18577 /* Normally first token is CPP_NAME "simd". CPP_EOF there indicates
18578 error has been reported and CPP_PRAGMA that c_finish_omp_declare_simd
18579 has already processed the tokens. */
18580 if (clauses.exists () && clauses[0].type == CPP_EOF)
18581 return;
18582 if (fndecl == NULL_TREE || TREE_CODE (fndecl) != FUNCTION_DECL)
18583 {
18584 error ("%<#pragma omp declare simd%> not immediately followed by "
18585 "a function declaration or definition");
18586 clauses[0].type = CPP_EOF;
18587 return;
18588 }
18589 if (clauses.exists () && clauses[0].type != CPP_NAME)
18590 {
18591 error_at (DECL_SOURCE_LOCATION (fndecl),
18592 "%<#pragma omp declare simd%> not immediately followed by "
18593 "a single function declaration or definition");
18594 clauses[0].type = CPP_EOF;
18595 return;
18596 }
18597
18598 if (parms == NULL_TREE)
18599 parms = DECL_ARGUMENTS (fndecl);
18600
18601 unsigned int tokens_avail = parser->tokens_avail;
18602 gcc_assert (parser->tokens == &parser->tokens_buf[0]);
18603
18604
18605 parser->tokens = clauses.address ();
18606 parser->tokens_avail = clauses.length ();
18607
18608 /* c_parser_omp_declare_simd pushed 2 extra CPP_EOF tokens at the end. */
18609 while (parser->tokens_avail > 3)
18610 {
18611 c_token *token = c_parser_peek_token (parser);
18612 gcc_assert (token->type == CPP_NAME
18613 && strcmp (IDENTIFIER_POINTER (token->value), "simd") == 0);
18614 c_parser_consume_token (parser);
18615 parser->in_pragma = true;
18616
18617 tree c = NULL_TREE;
18618 c = c_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
18619 "#pragma omp declare simd");
18620 c = c_omp_declare_simd_clauses_to_numbers (parms, c);
18621 if (c != NULL_TREE)
18622 c = tree_cons (NULL_TREE, c, NULL_TREE);
18623 c = build_tree_list (get_identifier ("omp declare simd"), c);
18624 TREE_CHAIN (c) = DECL_ATTRIBUTES (fndecl);
18625 DECL_ATTRIBUTES (fndecl) = c;
18626 }
18627
18628 parser->tokens = &parser->tokens_buf[0];
18629 parser->tokens_avail = tokens_avail;
18630 if (clauses.exists ())
18631 clauses[0].type = CPP_PRAGMA;
18632 }
18633
18634
18635 /* OpenMP 4.0:
18636 # pragma omp declare target new-line
18637 declarations and definitions
18638 # pragma omp end declare target new-line
18639
18640 OpenMP 4.5:
18641 # pragma omp declare target ( extended-list ) new-line
18642
18643 # pragma omp declare target declare-target-clauses[seq] new-line */
18644
18645 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
18646 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
18647 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
18648
18649 static void
18650 c_parser_omp_declare_target (c_parser *parser)
18651 {
18652 location_t loc = c_parser_peek_token (parser)->location;
18653 tree clauses = NULL_TREE;
18654 if (c_parser_next_token_is (parser, CPP_NAME))
18655 clauses = c_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
18656 "#pragma omp declare target");
18657 else if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
18658 {
18659 clauses = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_TO_DECLARE,
18660 clauses);
18661 clauses = c_finish_omp_clauses (clauses, C_ORT_OMP);
18662 c_parser_skip_to_pragma_eol (parser);
18663 }
18664 else
18665 {
18666 c_parser_skip_to_pragma_eol (parser);
18667 current_omp_declare_target_attribute++;
18668 return;
18669 }
18670 if (current_omp_declare_target_attribute)
18671 error_at (loc, "%<#pragma omp declare target%> with clauses in between "
18672 "%<#pragma omp declare target%> without clauses and "
18673 "%<#pragma omp end declare target%>");
18674 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
18675 {
18676 tree t = OMP_CLAUSE_DECL (c), id;
18677 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
18678 tree at2 = lookup_attribute ("omp declare target link",
18679 DECL_ATTRIBUTES (t));
18680 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
18681 {
18682 id = get_identifier ("omp declare target link");
18683 std::swap (at1, at2);
18684 }
18685 else
18686 id = get_identifier ("omp declare target");
18687 if (at2)
18688 {
18689 error_at (OMP_CLAUSE_LOCATION (c),
18690 "%qD specified both in declare target %<link%> and %<to%>"
18691 " clauses", t);
18692 continue;
18693 }
18694 if (!at1)
18695 {
18696 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
18697 if (TREE_CODE (t) != FUNCTION_DECL && !is_global_var (t))
18698 continue;
18699
18700 symtab_node *node = symtab_node::get (t);
18701 if (node != NULL)
18702 {
18703 node->offloadable = 1;
18704 if (ENABLE_OFFLOADING)
18705 {
18706 g->have_offload = true;
18707 if (is_a <varpool_node *> (node))
18708 vec_safe_push (offload_vars, t);
18709 }
18710 }
18711 }
18712 }
18713 }
18714
18715 static void
18716 c_parser_omp_end_declare_target (c_parser *parser)
18717 {
18718 location_t loc = c_parser_peek_token (parser)->location;
18719 c_parser_consume_pragma (parser);
18720 if (c_parser_next_token_is (parser, CPP_NAME)
18721 && strcmp (IDENTIFIER_POINTER (c_parser_peek_token (parser)->value),
18722 "declare") == 0)
18723 {
18724 c_parser_consume_token (parser);
18725 if (c_parser_next_token_is (parser, CPP_NAME)
18726 && strcmp (IDENTIFIER_POINTER (c_parser_peek_token (parser)->value),
18727 "target") == 0)
18728 c_parser_consume_token (parser);
18729 else
18730 {
18731 c_parser_error (parser, "expected %<target%>");
18732 c_parser_skip_to_pragma_eol (parser);
18733 return;
18734 }
18735 }
18736 else
18737 {
18738 c_parser_error (parser, "expected %<declare%>");
18739 c_parser_skip_to_pragma_eol (parser);
18740 return;
18741 }
18742 c_parser_skip_to_pragma_eol (parser);
18743 if (!current_omp_declare_target_attribute)
18744 error_at (loc, "%<#pragma omp end declare target%> without corresponding "
18745 "%<#pragma omp declare target%>");
18746 else
18747 current_omp_declare_target_attribute--;
18748 }
18749
18750
18751 /* OpenMP 4.0
18752 #pragma omp declare reduction (reduction-id : typename-list : expression) \
18753 initializer-clause[opt] new-line
18754
18755 initializer-clause:
18756 initializer (omp_priv = initializer)
18757 initializer (function-name (argument-list)) */
18758
18759 static void
18760 c_parser_omp_declare_reduction (c_parser *parser, enum pragma_context context)
18761 {
18762 unsigned int tokens_avail = 0, i;
18763 vec<tree> types = vNULL;
18764 vec<c_token> clauses = vNULL;
18765 enum tree_code reduc_code = ERROR_MARK;
18766 tree reduc_id = NULL_TREE;
18767 tree type;
18768 location_t rloc = c_parser_peek_token (parser)->location;
18769
18770 if (context == pragma_struct || context == pragma_param)
18771 {
18772 error ("%<#pragma omp declare reduction%> not at file or block scope");
18773 goto fail;
18774 }
18775
18776 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
18777 goto fail;
18778
18779 switch (c_parser_peek_token (parser)->type)
18780 {
18781 case CPP_PLUS:
18782 reduc_code = PLUS_EXPR;
18783 break;
18784 case CPP_MULT:
18785 reduc_code = MULT_EXPR;
18786 break;
18787 case CPP_MINUS:
18788 reduc_code = MINUS_EXPR;
18789 break;
18790 case CPP_AND:
18791 reduc_code = BIT_AND_EXPR;
18792 break;
18793 case CPP_XOR:
18794 reduc_code = BIT_XOR_EXPR;
18795 break;
18796 case CPP_OR:
18797 reduc_code = BIT_IOR_EXPR;
18798 break;
18799 case CPP_AND_AND:
18800 reduc_code = TRUTH_ANDIF_EXPR;
18801 break;
18802 case CPP_OR_OR:
18803 reduc_code = TRUTH_ORIF_EXPR;
18804 break;
18805 case CPP_NAME:
18806 const char *p;
18807 p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
18808 if (strcmp (p, "min") == 0)
18809 {
18810 reduc_code = MIN_EXPR;
18811 break;
18812 }
18813 if (strcmp (p, "max") == 0)
18814 {
18815 reduc_code = MAX_EXPR;
18816 break;
18817 }
18818 reduc_id = c_parser_peek_token (parser)->value;
18819 break;
18820 default:
18821 c_parser_error (parser,
18822 "expected %<+%>, %<*%>, %<-%>, %<&%>, "
18823 "%<^%>, %<|%>, %<&&%>, %<||%> or identifier");
18824 goto fail;
18825 }
18826
18827 tree orig_reduc_id, reduc_decl;
18828 orig_reduc_id = reduc_id;
18829 reduc_id = c_omp_reduction_id (reduc_code, reduc_id);
18830 reduc_decl = c_omp_reduction_decl (reduc_id);
18831 c_parser_consume_token (parser);
18832
18833 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
18834 goto fail;
18835
18836 while (true)
18837 {
18838 location_t loc = c_parser_peek_token (parser)->location;
18839 struct c_type_name *ctype = c_parser_type_name (parser);
18840 if (ctype != NULL)
18841 {
18842 type = groktypename (ctype, NULL, NULL);
18843 if (type == error_mark_node)
18844 ;
18845 else if ((INTEGRAL_TYPE_P (type)
18846 || TREE_CODE (type) == REAL_TYPE
18847 || TREE_CODE (type) == COMPLEX_TYPE)
18848 && orig_reduc_id == NULL_TREE)
18849 error_at (loc, "predeclared arithmetic type in "
18850 "%<#pragma omp declare reduction%>");
18851 else if (TREE_CODE (type) == FUNCTION_TYPE
18852 || TREE_CODE (type) == ARRAY_TYPE)
18853 error_at (loc, "function or array type in "
18854 "%<#pragma omp declare reduction%>");
18855 else if (TYPE_ATOMIC (type))
18856 error_at (loc, "%<_Atomic%> qualified type in "
18857 "%<#pragma omp declare reduction%>");
18858 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
18859 error_at (loc, "const, volatile or restrict qualified type in "
18860 "%<#pragma omp declare reduction%>");
18861 else
18862 {
18863 tree t;
18864 for (t = DECL_INITIAL (reduc_decl); t; t = TREE_CHAIN (t))
18865 if (comptypes (TREE_PURPOSE (t), type))
18866 {
18867 error_at (loc, "redeclaration of %qs "
18868 "%<#pragma omp declare reduction%> for "
18869 "type %qT",
18870 IDENTIFIER_POINTER (reduc_id)
18871 + sizeof ("omp declare reduction ") - 1,
18872 type);
18873 location_t ploc
18874 = DECL_SOURCE_LOCATION (TREE_VEC_ELT (TREE_VALUE (t),
18875 0));
18876 error_at (ploc, "previous %<#pragma omp declare "
18877 "reduction%>");
18878 break;
18879 }
18880 if (t == NULL_TREE)
18881 types.safe_push (type);
18882 }
18883 if (c_parser_next_token_is (parser, CPP_COMMA))
18884 c_parser_consume_token (parser);
18885 else
18886 break;
18887 }
18888 else
18889 break;
18890 }
18891
18892 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>")
18893 || types.is_empty ())
18894 {
18895 fail:
18896 clauses.release ();
18897 types.release ();
18898 while (true)
18899 {
18900 c_token *token = c_parser_peek_token (parser);
18901 if (token->type == CPP_EOF || token->type == CPP_PRAGMA_EOL)
18902 break;
18903 c_parser_consume_token (parser);
18904 }
18905 c_parser_skip_to_pragma_eol (parser);
18906 return;
18907 }
18908
18909 if (types.length () > 1)
18910 {
18911 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
18912 {
18913 c_token *token = c_parser_peek_token (parser);
18914 if (token->type == CPP_EOF)
18915 goto fail;
18916 clauses.safe_push (*token);
18917 c_parser_consume_token (parser);
18918 }
18919 clauses.safe_push (*c_parser_peek_token (parser));
18920 c_parser_skip_to_pragma_eol (parser);
18921
18922 /* Make sure nothing tries to read past the end of the tokens. */
18923 c_token eof_token;
18924 memset (&eof_token, 0, sizeof (eof_token));
18925 eof_token.type = CPP_EOF;
18926 clauses.safe_push (eof_token);
18927 clauses.safe_push (eof_token);
18928 }
18929
18930 int errs = errorcount;
18931 FOR_EACH_VEC_ELT (types, i, type)
18932 {
18933 tokens_avail = parser->tokens_avail;
18934 gcc_assert (parser->tokens == &parser->tokens_buf[0]);
18935 if (!clauses.is_empty ())
18936 {
18937 parser->tokens = clauses.address ();
18938 parser->tokens_avail = clauses.length ();
18939 parser->in_pragma = true;
18940 }
18941
18942 bool nested = current_function_decl != NULL_TREE;
18943 if (nested)
18944 c_push_function_context ();
18945 tree fndecl = build_decl (BUILTINS_LOCATION, FUNCTION_DECL,
18946 reduc_id, default_function_type);
18947 current_function_decl = fndecl;
18948 allocate_struct_function (fndecl, true);
18949 push_scope ();
18950 tree stmt = push_stmt_list ();
18951 /* Intentionally BUILTINS_LOCATION, so that -Wshadow doesn't
18952 warn about these. */
18953 tree omp_out = build_decl (BUILTINS_LOCATION, VAR_DECL,
18954 get_identifier ("omp_out"), type);
18955 DECL_ARTIFICIAL (omp_out) = 1;
18956 DECL_CONTEXT (omp_out) = fndecl;
18957 pushdecl (omp_out);
18958 tree omp_in = build_decl (BUILTINS_LOCATION, VAR_DECL,
18959 get_identifier ("omp_in"), type);
18960 DECL_ARTIFICIAL (omp_in) = 1;
18961 DECL_CONTEXT (omp_in) = fndecl;
18962 pushdecl (omp_in);
18963 struct c_expr combiner = c_parser_expression (parser);
18964 struct c_expr initializer;
18965 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE;
18966 bool bad = false;
18967 initializer.set_error ();
18968 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
18969 bad = true;
18970 else if (c_parser_next_token_is (parser, CPP_NAME)
18971 && strcmp (IDENTIFIER_POINTER
18972 (c_parser_peek_token (parser)->value),
18973 "initializer") == 0)
18974 {
18975 c_parser_consume_token (parser);
18976 pop_scope ();
18977 push_scope ();
18978 omp_priv = build_decl (BUILTINS_LOCATION, VAR_DECL,
18979 get_identifier ("omp_priv"), type);
18980 DECL_ARTIFICIAL (omp_priv) = 1;
18981 DECL_INITIAL (omp_priv) = error_mark_node;
18982 DECL_CONTEXT (omp_priv) = fndecl;
18983 pushdecl (omp_priv);
18984 omp_orig = build_decl (BUILTINS_LOCATION, VAR_DECL,
18985 get_identifier ("omp_orig"), type);
18986 DECL_ARTIFICIAL (omp_orig) = 1;
18987 DECL_CONTEXT (omp_orig) = fndecl;
18988 pushdecl (omp_orig);
18989 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
18990 bad = true;
18991 else if (!c_parser_next_token_is (parser, CPP_NAME))
18992 {
18993 c_parser_error (parser, "expected %<omp_priv%> or "
18994 "function-name");
18995 bad = true;
18996 }
18997 else if (strcmp (IDENTIFIER_POINTER
18998 (c_parser_peek_token (parser)->value),
18999 "omp_priv") != 0)
19000 {
19001 if (c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN
19002 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
19003 {
19004 c_parser_error (parser, "expected function-name %<(%>");
19005 bad = true;
19006 }
19007 else
19008 initializer = c_parser_postfix_expression (parser);
19009 if (initializer.value
19010 && TREE_CODE (initializer.value) == CALL_EXPR)
19011 {
19012 int j;
19013 tree c = initializer.value;
19014 for (j = 0; j < call_expr_nargs (c); j++)
19015 {
19016 tree a = CALL_EXPR_ARG (c, j);
19017 STRIP_NOPS (a);
19018 if (TREE_CODE (a) == ADDR_EXPR
19019 && TREE_OPERAND (a, 0) == omp_priv)
19020 break;
19021 }
19022 if (j == call_expr_nargs (c))
19023 error ("one of the initializer call arguments should be "
19024 "%<&omp_priv%>");
19025 }
19026 }
19027 else
19028 {
19029 c_parser_consume_token (parser);
19030 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
19031 bad = true;
19032 else
19033 {
19034 tree st = push_stmt_list ();
19035 location_t loc = c_parser_peek_token (parser)->location;
19036 rich_location richloc (line_table, loc);
19037 start_init (omp_priv, NULL_TREE, 0, &richloc);
19038 struct c_expr init = c_parser_initializer (parser);
19039 finish_init ();
19040 finish_decl (omp_priv, loc, init.value,
19041 init.original_type, NULL_TREE);
19042 pop_stmt_list (st);
19043 }
19044 }
19045 if (!bad
19046 && !c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
19047 bad = true;
19048 }
19049
19050 if (!bad)
19051 {
19052 c_parser_skip_to_pragma_eol (parser);
19053
19054 tree t = tree_cons (type, make_tree_vec (omp_priv ? 6 : 3),
19055 DECL_INITIAL (reduc_decl));
19056 DECL_INITIAL (reduc_decl) = t;
19057 DECL_SOURCE_LOCATION (omp_out) = rloc;
19058 TREE_VEC_ELT (TREE_VALUE (t), 0) = omp_out;
19059 TREE_VEC_ELT (TREE_VALUE (t), 1) = omp_in;
19060 TREE_VEC_ELT (TREE_VALUE (t), 2) = combiner.value;
19061 walk_tree (&combiner.value, c_check_omp_declare_reduction_r,
19062 &TREE_VEC_ELT (TREE_VALUE (t), 0), NULL);
19063 if (omp_priv)
19064 {
19065 DECL_SOURCE_LOCATION (omp_priv) = rloc;
19066 TREE_VEC_ELT (TREE_VALUE (t), 3) = omp_priv;
19067 TREE_VEC_ELT (TREE_VALUE (t), 4) = omp_orig;
19068 TREE_VEC_ELT (TREE_VALUE (t), 5) = initializer.value;
19069 walk_tree (&initializer.value, c_check_omp_declare_reduction_r,
19070 &TREE_VEC_ELT (TREE_VALUE (t), 3), NULL);
19071 walk_tree (&DECL_INITIAL (omp_priv),
19072 c_check_omp_declare_reduction_r,
19073 &TREE_VEC_ELT (TREE_VALUE (t), 3), NULL);
19074 }
19075 }
19076
19077 pop_stmt_list (stmt);
19078 pop_scope ();
19079 if (cfun->language != NULL)
19080 {
19081 ggc_free (cfun->language);
19082 cfun->language = NULL;
19083 }
19084 set_cfun (NULL);
19085 current_function_decl = NULL_TREE;
19086 if (nested)
19087 c_pop_function_context ();
19088
19089 if (!clauses.is_empty ())
19090 {
19091 parser->tokens = &parser->tokens_buf[0];
19092 parser->tokens_avail = tokens_avail;
19093 }
19094 if (bad)
19095 goto fail;
19096 if (errs != errorcount)
19097 break;
19098 }
19099
19100 clauses.release ();
19101 types.release ();
19102 }
19103
19104
19105 /* OpenMP 4.0
19106 #pragma omp declare simd declare-simd-clauses[optseq] new-line
19107 #pragma omp declare reduction (reduction-id : typename-list : expression) \
19108 initializer-clause[opt] new-line
19109 #pragma omp declare target new-line */
19110
19111 static void
19112 c_parser_omp_declare (c_parser *parser, enum pragma_context context)
19113 {
19114 c_parser_consume_pragma (parser);
19115 if (c_parser_next_token_is (parser, CPP_NAME))
19116 {
19117 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
19118 if (strcmp (p, "simd") == 0)
19119 {
19120 /* c_parser_consume_token (parser); done in
19121 c_parser_omp_declare_simd. */
19122 c_parser_omp_declare_simd (parser, context);
19123 return;
19124 }
19125 if (strcmp (p, "reduction") == 0)
19126 {
19127 c_parser_consume_token (parser);
19128 c_parser_omp_declare_reduction (parser, context);
19129 return;
19130 }
19131 if (!flag_openmp) /* flag_openmp_simd */
19132 {
19133 c_parser_skip_to_pragma_eol (parser, false);
19134 return;
19135 }
19136 if (strcmp (p, "target") == 0)
19137 {
19138 c_parser_consume_token (parser);
19139 c_parser_omp_declare_target (parser);
19140 return;
19141 }
19142 }
19143
19144 c_parser_error (parser, "expected %<simd%> or %<reduction%> "
19145 "or %<target%>");
19146 c_parser_skip_to_pragma_eol (parser);
19147 }
19148
19149 /* OpenMP 5.0
19150 #pragma omp requires clauses[optseq] new-line */
19151
19152 static void
19153 c_parser_omp_requires (c_parser *parser)
19154 {
19155 bool first = true;
19156 enum omp_requires new_req = (enum omp_requires) 0;
19157
19158 c_parser_consume_pragma (parser);
19159
19160 location_t loc = c_parser_peek_token (parser)->location;
19161 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
19162 {
19163 if (!first && c_parser_next_token_is (parser, CPP_COMMA))
19164 c_parser_consume_token (parser);
19165
19166 first = false;
19167
19168 if (c_parser_next_token_is (parser, CPP_NAME))
19169 {
19170 const char *p
19171 = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
19172 location_t cloc = c_parser_peek_token (parser)->location;
19173 enum omp_requires this_req = (enum omp_requires) 0;
19174
19175 if (!strcmp (p, "unified_address"))
19176 this_req = OMP_REQUIRES_UNIFIED_ADDRESS;
19177 else if (!strcmp (p, "unified_shared_memory"))
19178 this_req = OMP_REQUIRES_UNIFIED_SHARED_MEMORY;
19179 else if (!strcmp (p, "dynamic_allocators"))
19180 this_req = OMP_REQUIRES_DYNAMIC_ALLOCATORS;
19181 else if (!strcmp (p, "reverse_offload"))
19182 this_req = OMP_REQUIRES_REVERSE_OFFLOAD;
19183 else if (!strcmp (p, "atomic_default_mem_order"))
19184 {
19185 c_parser_consume_token (parser);
19186
19187 matching_parens parens;
19188 if (parens.require_open (parser))
19189 {
19190 if (c_parser_next_token_is (parser, CPP_NAME))
19191 {
19192 tree v = c_parser_peek_token (parser)->value;
19193 p = IDENTIFIER_POINTER (v);
19194
19195 if (!strcmp (p, "seq_cst"))
19196 this_req
19197 = (enum omp_requires) OMP_MEMORY_ORDER_SEQ_CST;
19198 else if (!strcmp (p, "relaxed"))
19199 this_req
19200 = (enum omp_requires) OMP_MEMORY_ORDER_RELAXED;
19201 else if (!strcmp (p, "acq_rel"))
19202 this_req
19203 = (enum omp_requires) OMP_MEMORY_ORDER_ACQ_REL;
19204 }
19205 if (this_req == 0)
19206 {
19207 error_at (c_parser_peek_token (parser)->location,
19208 "expected %<seq_cst%>, %<relaxed%> or "
19209 "%<acq_rel%>");
19210 if (c_parser_peek_2nd_token (parser)->type
19211 == CPP_CLOSE_PAREN)
19212 c_parser_consume_token (parser);
19213 }
19214 else
19215 c_parser_consume_token (parser);
19216
19217 parens.skip_until_found_close (parser);
19218 if (this_req == 0)
19219 {
19220 c_parser_skip_to_pragma_eol (parser, false);
19221 return;
19222 }
19223 }
19224 p = NULL;
19225 }
19226 else
19227 {
19228 error_at (cloc, "expected %<unified_address%>, "
19229 "%<unified_shared_memory%>, "
19230 "%<dynamic_allocators%>, "
19231 "%<reverse_offload%> "
19232 "or %<atomic_default_mem_order%> clause");
19233 c_parser_skip_to_pragma_eol (parser, false);
19234 return;
19235 }
19236 if (p)
19237 sorry_at (cloc, "%qs clause on %<requires%> directive not "
19238 "supported yet", p);
19239 if (p)
19240 c_parser_consume_token (parser);
19241 if (this_req)
19242 {
19243 if ((this_req & ~OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
19244 {
19245 if ((this_req & new_req) != 0)
19246 error_at (cloc, "too many %qs clauses", p);
19247 if (this_req != OMP_REQUIRES_DYNAMIC_ALLOCATORS
19248 && (omp_requires_mask & OMP_REQUIRES_TARGET_USED) != 0)
19249 error_at (cloc, "%qs clause used lexically after first "
19250 "target construct or offloading API", p);
19251 }
19252 else if ((new_req & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
19253 {
19254 error_at (cloc, "too many %qs clauses",
19255 "atomic_default_mem_order");
19256 this_req = (enum omp_requires) 0;
19257 }
19258 else if ((omp_requires_mask
19259 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
19260 {
19261 error_at (cloc, "more than one %<atomic_default_mem_order%>"
19262 " clause in a single compilation unit");
19263 this_req
19264 = (enum omp_requires)
19265 (omp_requires_mask
19266 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER);
19267 }
19268 else if ((omp_requires_mask
19269 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER_USED) != 0)
19270 error_at (cloc, "%<atomic_default_mem_order%> clause used "
19271 "lexically after first %<atomic%> construct "
19272 "without memory order clause");
19273 new_req = (enum omp_requires) (new_req | this_req);
19274 omp_requires_mask
19275 = (enum omp_requires) (omp_requires_mask | this_req);
19276 continue;
19277 }
19278 }
19279 break;
19280 }
19281 c_parser_skip_to_pragma_eol (parser);
19282
19283 if (new_req == 0)
19284 error_at (loc, "%<pragma omp requires%> requires at least one clause");
19285 }
19286
19287 /* Helper function for c_parser_omp_taskloop.
19288 Disallow zero sized or potentially zero sized task reductions. */
19289
19290 static tree
19291 c_finish_taskloop_clauses (tree clauses)
19292 {
19293 tree *pc = &clauses;
19294 for (tree c = clauses; c; c = *pc)
19295 {
19296 bool remove = false;
19297 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
19298 {
19299 tree type = strip_array_types (TREE_TYPE (OMP_CLAUSE_DECL (c)));
19300 if (integer_zerop (TYPE_SIZE_UNIT (type)))
19301 {
19302 error_at (OMP_CLAUSE_LOCATION (c),
19303 "zero sized type %qT in %<reduction%> clause", type);
19304 remove = true;
19305 }
19306 else if (TREE_CODE (TYPE_SIZE_UNIT (type)) != INTEGER_CST)
19307 {
19308 error_at (OMP_CLAUSE_LOCATION (c),
19309 "variable sized type %qT in %<reduction%> clause",
19310 type);
19311 remove = true;
19312 }
19313 }
19314 if (remove)
19315 *pc = OMP_CLAUSE_CHAIN (c);
19316 else
19317 pc = &OMP_CLAUSE_CHAIN (c);
19318 }
19319 return clauses;
19320 }
19321
19322 /* OpenMP 4.5:
19323 #pragma omp taskloop taskloop-clause[optseq] new-line
19324 for-loop
19325
19326 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
19327 for-loop */
19328
19329 #define OMP_TASKLOOP_CLAUSE_MASK \
19330 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
19331 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
19332 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
19333 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
19334 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
19335 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
19336 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
19337 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
19338 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
19339 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
19340 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
19341 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
19342 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
19343 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY) \
19344 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
19345 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION))
19346
19347 static tree
19348 c_parser_omp_taskloop (location_t loc, c_parser *parser,
19349 char *p_name, omp_clause_mask mask, tree *cclauses,
19350 bool *if_p)
19351 {
19352 tree clauses, block, ret;
19353
19354 strcat (p_name, " taskloop");
19355 mask |= OMP_TASKLOOP_CLAUSE_MASK;
19356 /* #pragma omp parallel master taskloop{, simd} disallow in_reduction
19357 clause. */
19358 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS)) != 0)
19359 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION);
19360
19361 if (c_parser_next_token_is (parser, CPP_NAME))
19362 {
19363 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
19364
19365 if (strcmp (p, "simd") == 0)
19366 {
19367 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
19368 if (cclauses == NULL)
19369 cclauses = cclauses_buf;
19370 c_parser_consume_token (parser);
19371 if (!flag_openmp) /* flag_openmp_simd */
19372 return c_parser_omp_simd (loc, parser, p_name, mask, cclauses,
19373 if_p);
19374 block = c_begin_compound_stmt (true);
19375 ret = c_parser_omp_simd (loc, parser, p_name, mask, cclauses, if_p);
19376 block = c_end_compound_stmt (loc, block, true);
19377 if (ret == NULL)
19378 return ret;
19379 ret = make_node (OMP_TASKLOOP);
19380 TREE_TYPE (ret) = void_type_node;
19381 OMP_FOR_BODY (ret) = block;
19382 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
19383 OMP_FOR_CLAUSES (ret)
19384 = c_finish_taskloop_clauses (OMP_FOR_CLAUSES (ret));
19385 SET_EXPR_LOCATION (ret, loc);
19386 add_stmt (ret);
19387 return ret;
19388 }
19389 }
19390 if (!flag_openmp) /* flag_openmp_simd */
19391 {
19392 c_parser_skip_to_pragma_eol (parser, false);
19393 return NULL_TREE;
19394 }
19395
19396 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
19397 if (cclauses)
19398 {
19399 omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
19400 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
19401 }
19402
19403 clauses = c_finish_taskloop_clauses (clauses);
19404 block = c_begin_compound_stmt (true);
19405 ret = c_parser_omp_for_loop (loc, parser, OMP_TASKLOOP, clauses, NULL, if_p);
19406 block = c_end_compound_stmt (loc, block, true);
19407 add_stmt (block);
19408
19409 return ret;
19410 }
19411
19412 /* Main entry point to parsing most OpenMP pragmas. */
19413
19414 static void
19415 c_parser_omp_construct (c_parser *parser, bool *if_p)
19416 {
19417 enum pragma_kind p_kind;
19418 location_t loc;
19419 tree stmt;
19420 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
19421 omp_clause_mask mask (0);
19422
19423 loc = c_parser_peek_token (parser)->location;
19424 p_kind = c_parser_peek_token (parser)->pragma_kind;
19425 c_parser_consume_pragma (parser);
19426
19427 switch (p_kind)
19428 {
19429 case PRAGMA_OACC_ATOMIC:
19430 c_parser_omp_atomic (loc, parser);
19431 return;
19432 case PRAGMA_OACC_CACHE:
19433 strcpy (p_name, "#pragma acc");
19434 stmt = c_parser_oacc_cache (loc, parser);
19435 break;
19436 case PRAGMA_OACC_DATA:
19437 stmt = c_parser_oacc_data (loc, parser, if_p);
19438 break;
19439 case PRAGMA_OACC_HOST_DATA:
19440 stmt = c_parser_oacc_host_data (loc, parser, if_p);
19441 break;
19442 case PRAGMA_OACC_KERNELS:
19443 case PRAGMA_OACC_PARALLEL:
19444 strcpy (p_name, "#pragma acc");
19445 stmt = c_parser_oacc_kernels_parallel (loc, parser, p_kind, p_name,
19446 if_p);
19447 break;
19448 case PRAGMA_OACC_LOOP:
19449 strcpy (p_name, "#pragma acc");
19450 stmt = c_parser_oacc_loop (loc, parser, p_name, mask, NULL, if_p);
19451 break;
19452 case PRAGMA_OACC_WAIT:
19453 strcpy (p_name, "#pragma wait");
19454 stmt = c_parser_oacc_wait (loc, parser, p_name);
19455 break;
19456 case PRAGMA_OMP_ATOMIC:
19457 c_parser_omp_atomic (loc, parser);
19458 return;
19459 case PRAGMA_OMP_CRITICAL:
19460 stmt = c_parser_omp_critical (loc, parser, if_p);
19461 break;
19462 case PRAGMA_OMP_DISTRIBUTE:
19463 strcpy (p_name, "#pragma omp");
19464 stmt = c_parser_omp_distribute (loc, parser, p_name, mask, NULL, if_p);
19465 break;
19466 case PRAGMA_OMP_FOR:
19467 strcpy (p_name, "#pragma omp");
19468 stmt = c_parser_omp_for (loc, parser, p_name, mask, NULL, if_p);
19469 break;
19470 case PRAGMA_OMP_MASTER:
19471 strcpy (p_name, "#pragma omp");
19472 stmt = c_parser_omp_master (loc, parser, p_name, mask, NULL, if_p);
19473 break;
19474 case PRAGMA_OMP_PARALLEL:
19475 strcpy (p_name, "#pragma omp");
19476 stmt = c_parser_omp_parallel (loc, parser, p_name, mask, NULL, if_p);
19477 break;
19478 case PRAGMA_OMP_SECTIONS:
19479 strcpy (p_name, "#pragma omp");
19480 stmt = c_parser_omp_sections (loc, parser, p_name, mask, NULL);
19481 break;
19482 case PRAGMA_OMP_SIMD:
19483 strcpy (p_name, "#pragma omp");
19484 stmt = c_parser_omp_simd (loc, parser, p_name, mask, NULL, if_p);
19485 break;
19486 case PRAGMA_OMP_SINGLE:
19487 stmt = c_parser_omp_single (loc, parser, if_p);
19488 break;
19489 case PRAGMA_OMP_TASK:
19490 stmt = c_parser_omp_task (loc, parser, if_p);
19491 break;
19492 case PRAGMA_OMP_TASKGROUP:
19493 stmt = c_parser_omp_taskgroup (loc, parser, if_p);
19494 break;
19495 case PRAGMA_OMP_TASKLOOP:
19496 strcpy (p_name, "#pragma omp");
19497 stmt = c_parser_omp_taskloop (loc, parser, p_name, mask, NULL, if_p);
19498 break;
19499 case PRAGMA_OMP_TEAMS:
19500 strcpy (p_name, "#pragma omp");
19501 stmt = c_parser_omp_teams (loc, parser, p_name, mask, NULL, if_p);
19502 break;
19503 default:
19504 gcc_unreachable ();
19505 }
19506
19507 if (stmt)
19508 gcc_assert (EXPR_LOCATION (stmt) != UNKNOWN_LOCATION);
19509 }
19510
19511
19512 /* OpenMP 2.5:
19513 # pragma omp threadprivate (variable-list) */
19514
19515 static void
19516 c_parser_omp_threadprivate (c_parser *parser)
19517 {
19518 tree vars, t;
19519 location_t loc;
19520
19521 c_parser_consume_pragma (parser);
19522 loc = c_parser_peek_token (parser)->location;
19523 vars = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
19524
19525 /* Mark every variable in VARS to be assigned thread local storage. */
19526 for (t = vars; t; t = TREE_CHAIN (t))
19527 {
19528 tree v = TREE_PURPOSE (t);
19529
19530 /* FIXME diagnostics: Ideally we should keep individual
19531 locations for all the variables in the var list to make the
19532 following errors more precise. Perhaps
19533 c_parser_omp_var_list_parens() should construct a list of
19534 locations to go along with the var list. */
19535
19536 /* If V had already been marked threadprivate, it doesn't matter
19537 whether it had been used prior to this point. */
19538 if (!VAR_P (v))
19539 error_at (loc, "%qD is not a variable", v);
19540 else if (TREE_USED (v) && !C_DECL_THREADPRIVATE_P (v))
19541 error_at (loc, "%qE declared %<threadprivate%> after first use", v);
19542 else if (! is_global_var (v))
19543 error_at (loc, "automatic variable %qE cannot be %<threadprivate%>", v);
19544 else if (TREE_TYPE (v) == error_mark_node)
19545 ;
19546 else if (! COMPLETE_TYPE_P (TREE_TYPE (v)))
19547 error_at (loc, "%<threadprivate%> %qE has incomplete type", v);
19548 else
19549 {
19550 if (! DECL_THREAD_LOCAL_P (v))
19551 {
19552 set_decl_tls_model (v, decl_default_tls_model (v));
19553 /* If rtl has been already set for this var, call
19554 make_decl_rtl once again, so that encode_section_info
19555 has a chance to look at the new decl flags. */
19556 if (DECL_RTL_SET_P (v))
19557 make_decl_rtl (v);
19558 }
19559 C_DECL_THREADPRIVATE_P (v) = 1;
19560 }
19561 }
19562
19563 c_parser_skip_to_pragma_eol (parser);
19564 }
19565
19566 /* Parse a transaction attribute (GCC Extension).
19567
19568 transaction-attribute:
19569 attributes
19570 [ [ any-word ] ]
19571
19572 The transactional memory language description is written for C++,
19573 and uses the C++0x attribute syntax. For compatibility, allow the
19574 bracket style for transactions in C as well. */
19575
19576 static tree
19577 c_parser_transaction_attributes (c_parser *parser)
19578 {
19579 tree attr_name, attr = NULL;
19580
19581 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
19582 return c_parser_attributes (parser);
19583
19584 if (!c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
19585 return NULL_TREE;
19586 c_parser_consume_token (parser);
19587 if (!c_parser_require (parser, CPP_OPEN_SQUARE, "expected %<[%>"))
19588 goto error1;
19589
19590 attr_name = c_parser_attribute_any_word (parser);
19591 if (attr_name)
19592 {
19593 c_parser_consume_token (parser);
19594 attr = build_tree_list (attr_name, NULL_TREE);
19595 }
19596 else
19597 c_parser_error (parser, "expected identifier");
19598
19599 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
19600 error1:
19601 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
19602 return attr;
19603 }
19604
19605 /* Parse a __transaction_atomic or __transaction_relaxed statement
19606 (GCC Extension).
19607
19608 transaction-statement:
19609 __transaction_atomic transaction-attribute[opt] compound-statement
19610 __transaction_relaxed compound-statement
19611
19612 Note that the only valid attribute is: "outer".
19613 */
19614
19615 static tree
19616 c_parser_transaction (c_parser *parser, enum rid keyword)
19617 {
19618 unsigned int old_in = parser->in_transaction;
19619 unsigned int this_in = 1, new_in;
19620 location_t loc = c_parser_peek_token (parser)->location;
19621 tree stmt, attrs;
19622
19623 gcc_assert ((keyword == RID_TRANSACTION_ATOMIC
19624 || keyword == RID_TRANSACTION_RELAXED)
19625 && c_parser_next_token_is_keyword (parser, keyword));
19626 c_parser_consume_token (parser);
19627
19628 if (keyword == RID_TRANSACTION_RELAXED)
19629 this_in |= TM_STMT_ATTR_RELAXED;
19630 else
19631 {
19632 attrs = c_parser_transaction_attributes (parser);
19633 if (attrs)
19634 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
19635 }
19636
19637 /* Keep track if we're in the lexical scope of an outer transaction. */
19638 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
19639
19640 parser->in_transaction = new_in;
19641 stmt = c_parser_compound_statement (parser);
19642 parser->in_transaction = old_in;
19643
19644 if (flag_tm)
19645 stmt = c_finish_transaction (loc, stmt, this_in);
19646 else
19647 error_at (loc, (keyword == RID_TRANSACTION_ATOMIC ?
19648 "%<__transaction_atomic%> without transactional memory support enabled"
19649 : "%<__transaction_relaxed %> "
19650 "without transactional memory support enabled"));
19651
19652 return stmt;
19653 }
19654
19655 /* Parse a __transaction_atomic or __transaction_relaxed expression
19656 (GCC Extension).
19657
19658 transaction-expression:
19659 __transaction_atomic ( expression )
19660 __transaction_relaxed ( expression )
19661 */
19662
19663 static struct c_expr
19664 c_parser_transaction_expression (c_parser *parser, enum rid keyword)
19665 {
19666 struct c_expr ret;
19667 unsigned int old_in = parser->in_transaction;
19668 unsigned int this_in = 1;
19669 location_t loc = c_parser_peek_token (parser)->location;
19670 tree attrs;
19671
19672 gcc_assert ((keyword == RID_TRANSACTION_ATOMIC
19673 || keyword == RID_TRANSACTION_RELAXED)
19674 && c_parser_next_token_is_keyword (parser, keyword));
19675 c_parser_consume_token (parser);
19676
19677 if (keyword == RID_TRANSACTION_RELAXED)
19678 this_in |= TM_STMT_ATTR_RELAXED;
19679 else
19680 {
19681 attrs = c_parser_transaction_attributes (parser);
19682 if (attrs)
19683 this_in |= parse_tm_stmt_attr (attrs, 0);
19684 }
19685
19686 parser->in_transaction = this_in;
19687 matching_parens parens;
19688 if (parens.require_open (parser))
19689 {
19690 tree expr = c_parser_expression (parser).value;
19691 ret.original_type = TREE_TYPE (expr);
19692 ret.value = build1 (TRANSACTION_EXPR, ret.original_type, expr);
19693 if (this_in & TM_STMT_ATTR_RELAXED)
19694 TRANSACTION_EXPR_RELAXED (ret.value) = 1;
19695 SET_EXPR_LOCATION (ret.value, loc);
19696 ret.original_code = TRANSACTION_EXPR;
19697 if (!parens.require_close (parser))
19698 {
19699 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
19700 goto error;
19701 }
19702 }
19703 else
19704 {
19705 error:
19706 ret.set_error ();
19707 ret.original_code = ERROR_MARK;
19708 ret.original_type = NULL;
19709 }
19710 parser->in_transaction = old_in;
19711
19712 if (!flag_tm)
19713 error_at (loc, (keyword == RID_TRANSACTION_ATOMIC ?
19714 "%<__transaction_atomic%> without transactional memory support enabled"
19715 : "%<__transaction_relaxed %> "
19716 "without transactional memory support enabled"));
19717
19718 set_c_expr_source_range (&ret, loc, loc);
19719
19720 return ret;
19721 }
19722
19723 /* Parse a __transaction_cancel statement (GCC Extension).
19724
19725 transaction-cancel-statement:
19726 __transaction_cancel transaction-attribute[opt] ;
19727
19728 Note that the only valid attribute is "outer".
19729 */
19730
19731 static tree
19732 c_parser_transaction_cancel (c_parser *parser)
19733 {
19734 location_t loc = c_parser_peek_token (parser)->location;
19735 tree attrs;
19736 bool is_outer = false;
19737
19738 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TRANSACTION_CANCEL));
19739 c_parser_consume_token (parser);
19740
19741 attrs = c_parser_transaction_attributes (parser);
19742 if (attrs)
19743 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
19744
19745 if (!flag_tm)
19746 {
19747 error_at (loc, "%<__transaction_cancel%> without "
19748 "transactional memory support enabled");
19749 goto ret_error;
19750 }
19751 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
19752 {
19753 error_at (loc, "%<__transaction_cancel%> within a "
19754 "%<__transaction_relaxed%>");
19755 goto ret_error;
19756 }
19757 else if (is_outer)
19758 {
19759 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
19760 && !is_tm_may_cancel_outer (current_function_decl))
19761 {
19762 error_at (loc, "outer %<__transaction_cancel%> not "
19763 "within outer %<__transaction_atomic%>");
19764 error_at (loc, " or a %<transaction_may_cancel_outer%> function");
19765 goto ret_error;
19766 }
19767 }
19768 else if (parser->in_transaction == 0)
19769 {
19770 error_at (loc, "%<__transaction_cancel%> not within "
19771 "%<__transaction_atomic%>");
19772 goto ret_error;
19773 }
19774
19775 return add_stmt (build_tm_abort_call (loc, is_outer));
19776
19777 ret_error:
19778 return build1 (NOP_EXPR, void_type_node, error_mark_node);
19779 }
19780 \f
19781 /* Parse a single source file. */
19782
19783 void
19784 c_parse_file (void)
19785 {
19786 /* Use local storage to begin. If the first token is a pragma, parse it.
19787 If it is #pragma GCC pch_preprocess, then this will load a PCH file
19788 which will cause garbage collection. */
19789 c_parser tparser;
19790
19791 memset (&tparser, 0, sizeof tparser);
19792 tparser.tokens = &tparser.tokens_buf[0];
19793 the_parser = &tparser;
19794
19795 if (c_parser_peek_token (&tparser)->pragma_kind == PRAGMA_GCC_PCH_PREPROCESS)
19796 c_parser_pragma_pch_preprocess (&tparser);
19797
19798 the_parser = ggc_alloc<c_parser> ();
19799 *the_parser = tparser;
19800 if (tparser.tokens == &tparser.tokens_buf[0])
19801 the_parser->tokens = &the_parser->tokens_buf[0];
19802
19803 /* Initialize EH, if we've been told to do so. */
19804 if (flag_exceptions)
19805 using_eh_for_cleanups ();
19806
19807 c_parser_translation_unit (the_parser);
19808 the_parser = NULL;
19809 }
19810
19811 /* Parse the body of a function declaration marked with "__RTL".
19812
19813 The RTL parser works on the level of characters read from a
19814 FILE *, whereas c_parser works at the level of tokens.
19815 Square this circle by consuming all of the tokens up to and
19816 including the closing brace, recording the start/end of the RTL
19817 fragment, and reopening the file and re-reading the relevant
19818 lines within the RTL parser.
19819
19820 This requires the opening and closing braces of the C function
19821 to be on separate lines from the RTL they wrap.
19822
19823 Take ownership of START_WITH_PASS, if non-NULL. */
19824
19825 void
19826 c_parser_parse_rtl_body (c_parser *parser, char *start_with_pass)
19827 {
19828 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
19829 {
19830 free (start_with_pass);
19831 return;
19832 }
19833
19834 location_t start_loc = c_parser_peek_token (parser)->location;
19835
19836 /* Consume all tokens, up to the closing brace, handling
19837 matching pairs of braces in the rtl dump. */
19838 int num_open_braces = 1;
19839 while (1)
19840 {
19841 switch (c_parser_peek_token (parser)->type)
19842 {
19843 case CPP_OPEN_BRACE:
19844 num_open_braces++;
19845 break;
19846 case CPP_CLOSE_BRACE:
19847 if (--num_open_braces == 0)
19848 goto found_closing_brace;
19849 break;
19850 case CPP_EOF:
19851 error_at (start_loc, "no closing brace");
19852 free (start_with_pass);
19853 return;
19854 default:
19855 break;
19856 }
19857 c_parser_consume_token (parser);
19858 }
19859
19860 found_closing_brace:
19861 /* At the closing brace; record its location. */
19862 location_t end_loc = c_parser_peek_token (parser)->location;
19863
19864 /* Consume the closing brace. */
19865 c_parser_consume_token (parser);
19866
19867 /* Invoke the RTL parser. */
19868 if (!read_rtl_function_body_from_file_range (start_loc, end_loc))
19869 {
19870 free (start_with_pass);
19871 return;
19872 }
19873
19874 /* If a pass name was provided for START_WITH_PASS, run the backend
19875 accordingly now, on the cfun created above, transferring
19876 ownership of START_WITH_PASS. */
19877 if (start_with_pass)
19878 run_rtl_passes (start_with_pass);
19879 }
19880
19881 #include "gt-c-c-parser.h"