[Fortran] gfortran.texi - minor style cleanup
[gcc.git] / gcc / c / c-parser.c
1 /* Parser for C and Objective-C.
2 Copyright (C) 1987-2020 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 sprintf (name, "__int%d__", int_n_data[i].bitsize);
162 id = get_identifier (name);
163 C_SET_RID_CODE (id, RID_FIRST_INT_N + i);
164 C_IS_RESERVED_WORD (id) = 1;
165 }
166 }
167 \f
168 /* A parser structure recording information about the state and
169 context of parsing. Includes lexer information with up to two
170 tokens of look-ahead; more are not needed for C. */
171 struct GTY(()) c_parser {
172 /* The look-ahead tokens. */
173 c_token * GTY((skip)) tokens;
174 /* Buffer for look-ahead tokens. */
175 c_token tokens_buf[4];
176 /* How many look-ahead tokens are available (0 - 4, or
177 more if parsing from pre-lexed tokens). */
178 unsigned int tokens_avail;
179 /* Raw look-ahead tokens, used only for checking in Objective-C
180 whether '[[' starts attributes. */
181 vec<c_token, va_gc> *raw_tokens;
182 /* The number of raw look-ahead tokens that have since been fully
183 lexed. */
184 unsigned int raw_tokens_used;
185 /* True if a syntax error is being recovered from; false otherwise.
186 c_parser_error sets this flag. It should clear this flag when
187 enough tokens have been consumed to recover from the error. */
188 BOOL_BITFIELD error : 1;
189 /* True if we're processing a pragma, and shouldn't automatically
190 consume CPP_PRAGMA_EOL. */
191 BOOL_BITFIELD in_pragma : 1;
192 /* True if we're parsing the outermost block of an if statement. */
193 BOOL_BITFIELD in_if_block : 1;
194 /* True if we want to lex a translated, joined string (for an
195 initial #pragma pch_preprocess). Otherwise the parser is
196 responsible for concatenating strings and translating to the
197 execution character set as needed. */
198 BOOL_BITFIELD lex_joined_string : 1;
199 /* True if, when the parser is concatenating string literals, it
200 should translate them to the execution character set (false
201 inside attributes). */
202 BOOL_BITFIELD translate_strings_p : 1;
203
204 /* Objective-C specific parser/lexer information. */
205
206 /* True if we are in a context where the Objective-C "PQ" keywords
207 are considered keywords. */
208 BOOL_BITFIELD objc_pq_context : 1;
209 /* True if we are parsing a (potential) Objective-C foreach
210 statement. This is set to true after we parsed 'for (' and while
211 we wait for 'in' or ';' to decide if it's a standard C for loop or an
212 Objective-C foreach loop. */
213 BOOL_BITFIELD objc_could_be_foreach_context : 1;
214 /* The following flag is needed to contextualize Objective-C lexical
215 analysis. In some cases (e.g., 'int NSObject;'), it is
216 undesirable to bind an identifier to an Objective-C class, even
217 if a class with that name exists. */
218 BOOL_BITFIELD objc_need_raw_identifier : 1;
219 /* Nonzero if we're processing a __transaction statement. The value
220 is 1 | TM_STMT_ATTR_*. */
221 unsigned int in_transaction : 4;
222 /* True if we are in a context where the Objective-C "Property attribute"
223 keywords are valid. */
224 BOOL_BITFIELD objc_property_attr_context : 1;
225
226 /* Location of the last consumed token. */
227 location_t last_token_location;
228 };
229
230 /* Return a pointer to the Nth token in PARSERs tokens_buf. */
231
232 c_token *
233 c_parser_tokens_buf (c_parser *parser, unsigned n)
234 {
235 return &parser->tokens_buf[n];
236 }
237
238 /* Return the error state of PARSER. */
239
240 bool
241 c_parser_error (c_parser *parser)
242 {
243 return parser->error;
244 }
245
246 /* Set the error state of PARSER to ERR. */
247
248 void
249 c_parser_set_error (c_parser *parser, bool err)
250 {
251 parser->error = err;
252 }
253
254
255 /* The actual parser and external interface. ??? Does this need to be
256 garbage-collected? */
257
258 static GTY (()) c_parser *the_parser;
259
260 /* Read in and lex a single token, storing it in *TOKEN. If RAW,
261 context-sensitive postprocessing of the token is not done. */
262
263 static void
264 c_lex_one_token (c_parser *parser, c_token *token, bool raw = false)
265 {
266 timevar_push (TV_LEX);
267
268 if (raw || vec_safe_length (parser->raw_tokens) == 0)
269 {
270 token->type = c_lex_with_flags (&token->value, &token->location,
271 &token->flags,
272 (parser->lex_joined_string
273 ? 0 : C_LEX_STRING_NO_JOIN));
274 token->id_kind = C_ID_NONE;
275 token->keyword = RID_MAX;
276 token->pragma_kind = PRAGMA_NONE;
277 }
278 else
279 {
280 /* Use a token previously lexed as a raw look-ahead token, and
281 complete the processing on it. */
282 *token = (*parser->raw_tokens)[parser->raw_tokens_used];
283 ++parser->raw_tokens_used;
284 if (parser->raw_tokens_used == vec_safe_length (parser->raw_tokens))
285 {
286 vec_free (parser->raw_tokens);
287 parser->raw_tokens_used = 0;
288 }
289 }
290
291 if (raw)
292 goto out;
293
294 switch (token->type)
295 {
296 case CPP_NAME:
297 {
298 tree decl;
299
300 bool objc_force_identifier = parser->objc_need_raw_identifier;
301 if (c_dialect_objc ())
302 parser->objc_need_raw_identifier = false;
303
304 if (C_IS_RESERVED_WORD (token->value))
305 {
306 enum rid rid_code = C_RID_CODE (token->value);
307
308 if (rid_code == RID_CXX_COMPAT_WARN)
309 {
310 warning_at (token->location,
311 OPT_Wc___compat,
312 "identifier %qE conflicts with C++ keyword",
313 token->value);
314 }
315 else if (rid_code >= RID_FIRST_ADDR_SPACE
316 && rid_code <= RID_LAST_ADDR_SPACE)
317 {
318 addr_space_t as;
319 as = (addr_space_t) (rid_code - RID_FIRST_ADDR_SPACE);
320 targetm.addr_space.diagnose_usage (as, token->location);
321 token->id_kind = C_ID_ADDRSPACE;
322 token->keyword = rid_code;
323 break;
324 }
325 else if (c_dialect_objc () && OBJC_IS_PQ_KEYWORD (rid_code))
326 {
327 /* We found an Objective-C "pq" keyword (in, out,
328 inout, bycopy, byref, oneway). They need special
329 care because the interpretation depends on the
330 context. */
331 if (parser->objc_pq_context)
332 {
333 token->type = CPP_KEYWORD;
334 token->keyword = rid_code;
335 break;
336 }
337 else if (parser->objc_could_be_foreach_context
338 && rid_code == RID_IN)
339 {
340 /* We are in Objective-C, inside a (potential)
341 foreach context (which means after having
342 parsed 'for (', but before having parsed ';'),
343 and we found 'in'. We consider it the keyword
344 which terminates the declaration at the
345 beginning of a foreach-statement. Note that
346 this means you can't use 'in' for anything else
347 in that context; in particular, in Objective-C
348 you can't use 'in' as the name of the running
349 variable in a C for loop. We could potentially
350 try to add code here to disambiguate, but it
351 seems a reasonable limitation. */
352 token->type = CPP_KEYWORD;
353 token->keyword = rid_code;
354 break;
355 }
356 /* Else, "pq" keywords outside of the "pq" context are
357 not keywords, and we fall through to the code for
358 normal tokens. */
359 }
360 else if (c_dialect_objc () && OBJC_IS_PATTR_KEYWORD (rid_code))
361 {
362 /* We found an Objective-C "property attribute"
363 keyword (getter, setter, readonly, etc). These are
364 only valid in the property context. */
365 if (parser->objc_property_attr_context)
366 {
367 token->type = CPP_KEYWORD;
368 token->keyword = rid_code;
369 break;
370 }
371 /* Else they are not special keywords.
372 */
373 }
374 else if (c_dialect_objc ()
375 && (OBJC_IS_AT_KEYWORD (rid_code)
376 || OBJC_IS_CXX_KEYWORD (rid_code)))
377 {
378 /* We found one of the Objective-C "@" keywords (defs,
379 selector, synchronized, etc) or one of the
380 Objective-C "cxx" keywords (class, private,
381 protected, public, try, catch, throw) without a
382 preceding '@' sign. Do nothing and fall through to
383 the code for normal tokens (in C++ we would still
384 consider the CXX ones keywords, but not in C). */
385 ;
386 }
387 else
388 {
389 token->type = CPP_KEYWORD;
390 token->keyword = rid_code;
391 break;
392 }
393 }
394
395 decl = lookup_name (token->value);
396 if (decl)
397 {
398 if (TREE_CODE (decl) == TYPE_DECL)
399 {
400 token->id_kind = C_ID_TYPENAME;
401 break;
402 }
403 }
404 else if (c_dialect_objc ())
405 {
406 tree objc_interface_decl = objc_is_class_name (token->value);
407 /* Objective-C class names are in the same namespace as
408 variables and typedefs, and hence are shadowed by local
409 declarations. */
410 if (objc_interface_decl
411 && (!objc_force_identifier || global_bindings_p ()))
412 {
413 token->value = objc_interface_decl;
414 token->id_kind = C_ID_CLASSNAME;
415 break;
416 }
417 }
418 token->id_kind = C_ID_ID;
419 }
420 break;
421 case CPP_AT_NAME:
422 /* This only happens in Objective-C; it must be a keyword. */
423 token->type = CPP_KEYWORD;
424 switch (C_RID_CODE (token->value))
425 {
426 /* Replace 'class' with '@class', 'private' with '@private',
427 etc. This prevents confusion with the C++ keyword
428 'class', and makes the tokens consistent with other
429 Objective-C 'AT' keywords. For example '@class' is
430 reported as RID_AT_CLASS which is consistent with
431 '@synchronized', which is reported as
432 RID_AT_SYNCHRONIZED.
433 */
434 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
435 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
436 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
437 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
438 case RID_THROW: token->keyword = RID_AT_THROW; break;
439 case RID_TRY: token->keyword = RID_AT_TRY; break;
440 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
441 case RID_SYNCHRONIZED: token->keyword = RID_AT_SYNCHRONIZED; break;
442 default: token->keyword = C_RID_CODE (token->value);
443 }
444 break;
445 case CPP_COLON:
446 case CPP_COMMA:
447 case CPP_CLOSE_PAREN:
448 case CPP_SEMICOLON:
449 /* These tokens may affect the interpretation of any identifiers
450 following, if doing Objective-C. */
451 if (c_dialect_objc ())
452 parser->objc_need_raw_identifier = false;
453 break;
454 case CPP_PRAGMA:
455 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
456 token->pragma_kind = (enum pragma_kind) TREE_INT_CST_LOW (token->value);
457 token->value = NULL;
458 break;
459 default:
460 break;
461 }
462 out:
463 timevar_pop (TV_LEX);
464 }
465
466 /* Return a pointer to the next token from PARSER, reading it in if
467 necessary. */
468
469 c_token *
470 c_parser_peek_token (c_parser *parser)
471 {
472 if (parser->tokens_avail == 0)
473 {
474 c_lex_one_token (parser, &parser->tokens[0]);
475 parser->tokens_avail = 1;
476 }
477 return &parser->tokens[0];
478 }
479
480 /* Return a pointer to the next-but-one token from PARSER, reading it
481 in if necessary. The next token is already read in. */
482
483 c_token *
484 c_parser_peek_2nd_token (c_parser *parser)
485 {
486 if (parser->tokens_avail >= 2)
487 return &parser->tokens[1];
488 gcc_assert (parser->tokens_avail == 1);
489 gcc_assert (parser->tokens[0].type != CPP_EOF);
490 gcc_assert (parser->tokens[0].type != CPP_PRAGMA_EOL);
491 c_lex_one_token (parser, &parser->tokens[1]);
492 parser->tokens_avail = 2;
493 return &parser->tokens[1];
494 }
495
496 /* Return a pointer to the Nth token from PARSER, reading it
497 in if necessary. The N-1th token is already read in. */
498
499 c_token *
500 c_parser_peek_nth_token (c_parser *parser, unsigned int n)
501 {
502 /* N is 1-based, not zero-based. */
503 gcc_assert (n > 0);
504
505 if (parser->tokens_avail >= n)
506 return &parser->tokens[n - 1];
507 gcc_assert (parser->tokens_avail == n - 1);
508 c_lex_one_token (parser, &parser->tokens[n - 1]);
509 parser->tokens_avail = n;
510 return &parser->tokens[n - 1];
511 }
512
513 /* Return a pointer to the Nth token from PARSER, reading it in as a
514 raw look-ahead token if necessary. The N-1th token is already read
515 in. Raw look-ahead tokens remain available for when the non-raw
516 functions above are called. */
517
518 c_token *
519 c_parser_peek_nth_token_raw (c_parser *parser, unsigned int n)
520 {
521 /* N is 1-based, not zero-based. */
522 gcc_assert (n > 0);
523
524 if (parser->tokens_avail >= n)
525 return &parser->tokens[n - 1];
526 unsigned int raw_len = vec_safe_length (parser->raw_tokens);
527 unsigned int raw_avail
528 = parser->tokens_avail + raw_len - parser->raw_tokens_used;
529 gcc_assert (raw_avail >= n - 1);
530 if (raw_avail >= n)
531 return &(*parser->raw_tokens)[parser->raw_tokens_used
532 + n - 1 - parser->tokens_avail];
533 vec_safe_reserve (parser->raw_tokens, 1);
534 parser->raw_tokens->quick_grow (raw_len + 1);
535 c_lex_one_token (parser, &(*parser->raw_tokens)[raw_len], true);
536 return &(*parser->raw_tokens)[raw_len];
537 }
538
539 bool
540 c_keyword_starts_typename (enum rid keyword)
541 {
542 switch (keyword)
543 {
544 case RID_UNSIGNED:
545 case RID_LONG:
546 case RID_SHORT:
547 case RID_SIGNED:
548 case RID_COMPLEX:
549 case RID_INT:
550 case RID_CHAR:
551 case RID_FLOAT:
552 case RID_DOUBLE:
553 case RID_VOID:
554 case RID_DFLOAT32:
555 case RID_DFLOAT64:
556 case RID_DFLOAT128:
557 CASE_RID_FLOATN_NX:
558 case RID_BOOL:
559 case RID_ENUM:
560 case RID_STRUCT:
561 case RID_UNION:
562 case RID_TYPEOF:
563 case RID_CONST:
564 case RID_ATOMIC:
565 case RID_VOLATILE:
566 case RID_RESTRICT:
567 case RID_ATTRIBUTE:
568 case RID_FRACT:
569 case RID_ACCUM:
570 case RID_SAT:
571 case RID_AUTO_TYPE:
572 case RID_ALIGNAS:
573 return true;
574 default:
575 if (keyword >= RID_FIRST_INT_N
576 && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
577 && int_n_enabled_p[keyword - RID_FIRST_INT_N])
578 return true;
579 return false;
580 }
581 }
582
583 /* Return true if TOKEN can start a type name,
584 false otherwise. */
585 bool
586 c_token_starts_typename (c_token *token)
587 {
588 switch (token->type)
589 {
590 case CPP_NAME:
591 switch (token->id_kind)
592 {
593 case C_ID_ID:
594 return false;
595 case C_ID_ADDRSPACE:
596 return true;
597 case C_ID_TYPENAME:
598 return true;
599 case C_ID_CLASSNAME:
600 gcc_assert (c_dialect_objc ());
601 return true;
602 default:
603 gcc_unreachable ();
604 }
605 case CPP_KEYWORD:
606 return c_keyword_starts_typename (token->keyword);
607 case CPP_LESS:
608 if (c_dialect_objc ())
609 return true;
610 return false;
611 default:
612 return false;
613 }
614 }
615
616 /* Return true if the next token from PARSER can start a type name,
617 false otherwise. LA specifies how to do lookahead in order to
618 detect unknown type names. If unsure, pick CLA_PREFER_ID. */
619
620 static inline bool
621 c_parser_next_tokens_start_typename (c_parser *parser, enum c_lookahead_kind la)
622 {
623 c_token *token = c_parser_peek_token (parser);
624 if (c_token_starts_typename (token))
625 return true;
626
627 /* Try a bit harder to detect an unknown typename. */
628 if (la != cla_prefer_id
629 && token->type == CPP_NAME
630 && token->id_kind == C_ID_ID
631
632 /* Do not try too hard when we could have "object in array". */
633 && !parser->objc_could_be_foreach_context
634
635 && (la == cla_prefer_type
636 || c_parser_peek_2nd_token (parser)->type == CPP_NAME
637 || c_parser_peek_2nd_token (parser)->type == CPP_MULT)
638
639 /* Only unknown identifiers. */
640 && !lookup_name (token->value))
641 return true;
642
643 return false;
644 }
645
646 /* Return true if TOKEN is a type qualifier, false otherwise. */
647 static bool
648 c_token_is_qualifier (c_token *token)
649 {
650 switch (token->type)
651 {
652 case CPP_NAME:
653 switch (token->id_kind)
654 {
655 case C_ID_ADDRSPACE:
656 return true;
657 default:
658 return false;
659 }
660 case CPP_KEYWORD:
661 switch (token->keyword)
662 {
663 case RID_CONST:
664 case RID_VOLATILE:
665 case RID_RESTRICT:
666 case RID_ATTRIBUTE:
667 case RID_ATOMIC:
668 return true;
669 default:
670 return false;
671 }
672 case CPP_LESS:
673 return false;
674 default:
675 gcc_unreachable ();
676 }
677 }
678
679 /* Return true if the next token from PARSER is a type qualifier,
680 false otherwise. */
681 static inline bool
682 c_parser_next_token_is_qualifier (c_parser *parser)
683 {
684 c_token *token = c_parser_peek_token (parser);
685 return c_token_is_qualifier (token);
686 }
687
688 /* Return true if TOKEN can start declaration specifiers (not
689 including standard attributes), false otherwise. */
690 static bool
691 c_token_starts_declspecs (c_token *token)
692 {
693 switch (token->type)
694 {
695 case CPP_NAME:
696 switch (token->id_kind)
697 {
698 case C_ID_ID:
699 return false;
700 case C_ID_ADDRSPACE:
701 return true;
702 case C_ID_TYPENAME:
703 return true;
704 case C_ID_CLASSNAME:
705 gcc_assert (c_dialect_objc ());
706 return true;
707 default:
708 gcc_unreachable ();
709 }
710 case CPP_KEYWORD:
711 switch (token->keyword)
712 {
713 case RID_STATIC:
714 case RID_EXTERN:
715 case RID_REGISTER:
716 case RID_TYPEDEF:
717 case RID_INLINE:
718 case RID_NORETURN:
719 case RID_AUTO:
720 case RID_THREAD:
721 case RID_UNSIGNED:
722 case RID_LONG:
723 case RID_SHORT:
724 case RID_SIGNED:
725 case RID_COMPLEX:
726 case RID_INT:
727 case RID_CHAR:
728 case RID_FLOAT:
729 case RID_DOUBLE:
730 case RID_VOID:
731 case RID_DFLOAT32:
732 case RID_DFLOAT64:
733 case RID_DFLOAT128:
734 CASE_RID_FLOATN_NX:
735 case RID_BOOL:
736 case RID_ENUM:
737 case RID_STRUCT:
738 case RID_UNION:
739 case RID_TYPEOF:
740 case RID_CONST:
741 case RID_VOLATILE:
742 case RID_RESTRICT:
743 case RID_ATTRIBUTE:
744 case RID_FRACT:
745 case RID_ACCUM:
746 case RID_SAT:
747 case RID_ALIGNAS:
748 case RID_ATOMIC:
749 case RID_AUTO_TYPE:
750 return true;
751 default:
752 if (token->keyword >= RID_FIRST_INT_N
753 && token->keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
754 && int_n_enabled_p[token->keyword - RID_FIRST_INT_N])
755 return true;
756 return false;
757 }
758 case CPP_LESS:
759 if (c_dialect_objc ())
760 return true;
761 return false;
762 default:
763 return false;
764 }
765 }
766
767
768 /* Return true if TOKEN can start declaration specifiers (not
769 including standard attributes) or a static assertion, false
770 otherwise. */
771 static bool
772 c_token_starts_declaration (c_token *token)
773 {
774 if (c_token_starts_declspecs (token)
775 || token->keyword == RID_STATIC_ASSERT)
776 return true;
777 else
778 return false;
779 }
780
781 /* Return true if the next token from PARSER can start declaration
782 specifiers (not including standard attributes), false
783 otherwise. */
784 bool
785 c_parser_next_token_starts_declspecs (c_parser *parser)
786 {
787 c_token *token = c_parser_peek_token (parser);
788
789 /* In Objective-C, a classname normally starts a declspecs unless it
790 is immediately followed by a dot. In that case, it is the
791 Objective-C 2.0 "dot-syntax" for class objects, ie, calls the
792 setter/getter on the class. c_token_starts_declspecs() can't
793 differentiate between the two cases because it only checks the
794 current token, so we have a special check here. */
795 if (c_dialect_objc ()
796 && token->type == CPP_NAME
797 && token->id_kind == C_ID_CLASSNAME
798 && c_parser_peek_2nd_token (parser)->type == CPP_DOT)
799 return false;
800
801 return c_token_starts_declspecs (token);
802 }
803
804 /* Return true if the next tokens from PARSER can start declaration
805 specifiers (not including standard attributes) or a static
806 assertion, false otherwise. */
807 bool
808 c_parser_next_tokens_start_declaration (c_parser *parser)
809 {
810 c_token *token = c_parser_peek_token (parser);
811
812 /* Same as above. */
813 if (c_dialect_objc ()
814 && token->type == CPP_NAME
815 && token->id_kind == C_ID_CLASSNAME
816 && c_parser_peek_2nd_token (parser)->type == CPP_DOT)
817 return false;
818
819 /* Labels do not start declarations. */
820 if (token->type == CPP_NAME
821 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
822 return false;
823
824 if (c_token_starts_declaration (token))
825 return true;
826
827 if (c_parser_next_tokens_start_typename (parser, cla_nonabstract_decl))
828 return true;
829
830 return false;
831 }
832
833 /* Consume the next token from PARSER. */
834
835 void
836 c_parser_consume_token (c_parser *parser)
837 {
838 gcc_assert (parser->tokens_avail >= 1);
839 gcc_assert (parser->tokens[0].type != CPP_EOF);
840 gcc_assert (!parser->in_pragma || parser->tokens[0].type != CPP_PRAGMA_EOL);
841 gcc_assert (parser->error || parser->tokens[0].type != CPP_PRAGMA);
842 parser->last_token_location = parser->tokens[0].location;
843 if (parser->tokens != &parser->tokens_buf[0])
844 parser->tokens++;
845 else if (parser->tokens_avail >= 2)
846 {
847 parser->tokens[0] = parser->tokens[1];
848 if (parser->tokens_avail >= 3)
849 {
850 parser->tokens[1] = parser->tokens[2];
851 if (parser->tokens_avail >= 4)
852 parser->tokens[2] = parser->tokens[3];
853 }
854 }
855 parser->tokens_avail--;
856 }
857
858 /* Expect the current token to be a #pragma. Consume it and remember
859 that we've begun parsing a pragma. */
860
861 static void
862 c_parser_consume_pragma (c_parser *parser)
863 {
864 gcc_assert (!parser->in_pragma);
865 gcc_assert (parser->tokens_avail >= 1);
866 gcc_assert (parser->tokens[0].type == CPP_PRAGMA);
867 if (parser->tokens != &parser->tokens_buf[0])
868 parser->tokens++;
869 else if (parser->tokens_avail >= 2)
870 {
871 parser->tokens[0] = parser->tokens[1];
872 if (parser->tokens_avail >= 3)
873 parser->tokens[1] = parser->tokens[2];
874 }
875 parser->tokens_avail--;
876 parser->in_pragma = true;
877 }
878
879 /* Update the global input_location from TOKEN. */
880 static inline void
881 c_parser_set_source_position_from_token (c_token *token)
882 {
883 if (token->type != CPP_EOF)
884 {
885 input_location = token->location;
886 }
887 }
888
889 /* Helper function for c_parser_error.
890 Having peeked a token of kind TOK1_KIND that might signify
891 a conflict marker, peek successor tokens to determine
892 if we actually do have a conflict marker.
893 Specifically, we consider a run of 7 '<', '=' or '>' characters
894 at the start of a line as a conflict marker.
895 These come through the lexer as three pairs and a single,
896 e.g. three CPP_LSHIFT ("<<") and a CPP_LESS ('<').
897 If it returns true, *OUT_LOC is written to with the location/range
898 of the marker. */
899
900 static bool
901 c_parser_peek_conflict_marker (c_parser *parser, enum cpp_ttype tok1_kind,
902 location_t *out_loc)
903 {
904 c_token *token2 = c_parser_peek_2nd_token (parser);
905 if (token2->type != tok1_kind)
906 return false;
907 c_token *token3 = c_parser_peek_nth_token (parser, 3);
908 if (token3->type != tok1_kind)
909 return false;
910 c_token *token4 = c_parser_peek_nth_token (parser, 4);
911 if (token4->type != conflict_marker_get_final_tok_kind (tok1_kind))
912 return false;
913
914 /* It must be at the start of the line. */
915 location_t start_loc = c_parser_peek_token (parser)->location;
916 if (LOCATION_COLUMN (start_loc) != 1)
917 return false;
918
919 /* We have a conflict marker. Construct a location of the form:
920 <<<<<<<
921 ^~~~~~~
922 with start == caret, finishing at the end of the marker. */
923 location_t finish_loc = get_finish (token4->location);
924 *out_loc = make_location (start_loc, start_loc, finish_loc);
925
926 return true;
927 }
928
929 /* Issue a diagnostic of the form
930 FILE:LINE: MESSAGE before TOKEN
931 where TOKEN is the next token in the input stream of PARSER.
932 MESSAGE (specified by the caller) is usually of the form "expected
933 OTHER-TOKEN".
934
935 Use RICHLOC as the location of the diagnostic.
936
937 Do not issue a diagnostic if still recovering from an error.
938
939 Return true iff an error was actually emitted.
940
941 ??? This is taken from the C++ parser, but building up messages in
942 this way is not i18n-friendly and some other approach should be
943 used. */
944
945 static bool
946 c_parser_error_richloc (c_parser *parser, const char *gmsgid,
947 rich_location *richloc)
948 {
949 c_token *token = c_parser_peek_token (parser);
950 if (parser->error)
951 return false;
952 parser->error = true;
953 if (!gmsgid)
954 return false;
955
956 /* If this is actually a conflict marker, report it as such. */
957 if (token->type == CPP_LSHIFT
958 || token->type == CPP_RSHIFT
959 || token->type == CPP_EQ_EQ)
960 {
961 location_t loc;
962 if (c_parser_peek_conflict_marker (parser, token->type, &loc))
963 {
964 error_at (loc, "version control conflict marker in file");
965 return true;
966 }
967 }
968
969 c_parse_error (gmsgid,
970 /* Because c_parse_error does not understand
971 CPP_KEYWORD, keywords are treated like
972 identifiers. */
973 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
974 /* ??? The C parser does not save the cpp flags of a
975 token, we need to pass 0 here and we will not get
976 the source spelling of some tokens but rather the
977 canonical spelling. */
978 token->value, /*flags=*/0, richloc);
979 return true;
980 }
981
982 /* As c_parser_error_richloc, but issue the message at the
983 location of PARSER's next token, or at input_location
984 if the next token is EOF. */
985
986 bool
987 c_parser_error (c_parser *parser, const char *gmsgid)
988 {
989 c_token *token = c_parser_peek_token (parser);
990 c_parser_set_source_position_from_token (token);
991 rich_location richloc (line_table, input_location);
992 return c_parser_error_richloc (parser, gmsgid, &richloc);
993 }
994
995 /* Some tokens naturally come in pairs e.g.'(' and ')'.
996 This class is for tracking such a matching pair of symbols.
997 In particular, it tracks the location of the first token,
998 so that if the second token is missing, we can highlight the
999 location of the first token when notifying the user about the
1000 problem. */
1001
1002 template <typename traits_t>
1003 class token_pair
1004 {
1005 public:
1006 /* token_pair's ctor. */
1007 token_pair () : m_open_loc (UNKNOWN_LOCATION) {}
1008
1009 /* If the next token is the opening symbol for this pair, consume it and
1010 return true.
1011 Otherwise, issue an error and return false.
1012 In either case, record the location of the opening token. */
1013
1014 bool require_open (c_parser *parser)
1015 {
1016 c_token *token = c_parser_peek_token (parser);
1017 if (token)
1018 m_open_loc = token->location;
1019
1020 return c_parser_require (parser, traits_t::open_token_type,
1021 traits_t::open_gmsgid);
1022 }
1023
1024 /* Consume the next token from PARSER, recording its location as
1025 that of the opening token within the pair. */
1026
1027 void consume_open (c_parser *parser)
1028 {
1029 c_token *token = c_parser_peek_token (parser);
1030 gcc_assert (token->type == traits_t::open_token_type);
1031 m_open_loc = token->location;
1032 c_parser_consume_token (parser);
1033 }
1034
1035 /* If the next token is the closing symbol for this pair, consume it
1036 and return true.
1037 Otherwise, issue an error, highlighting the location of the
1038 corresponding opening token, and return false. */
1039
1040 bool require_close (c_parser *parser) const
1041 {
1042 return c_parser_require (parser, traits_t::close_token_type,
1043 traits_t::close_gmsgid, m_open_loc);
1044 }
1045
1046 /* Like token_pair::require_close, except that tokens will be skipped
1047 until the desired token is found. An error message is still produced
1048 if the next token is not as expected. */
1049
1050 void skip_until_found_close (c_parser *parser) const
1051 {
1052 c_parser_skip_until_found (parser, traits_t::close_token_type,
1053 traits_t::close_gmsgid, m_open_loc);
1054 }
1055
1056 private:
1057 location_t m_open_loc;
1058 };
1059
1060 /* Traits for token_pair<T> for tracking matching pairs of parentheses. */
1061
1062 struct matching_paren_traits
1063 {
1064 static const enum cpp_ttype open_token_type = CPP_OPEN_PAREN;
1065 static const char * const open_gmsgid;
1066 static const enum cpp_ttype close_token_type = CPP_CLOSE_PAREN;
1067 static const char * const close_gmsgid;
1068 };
1069
1070 const char * const matching_paren_traits::open_gmsgid = "expected %<(%>";
1071 const char * const matching_paren_traits::close_gmsgid = "expected %<)%>";
1072
1073 /* "matching_parens" is a token_pair<T> class for tracking matching
1074 pairs of parentheses. */
1075
1076 typedef token_pair<matching_paren_traits> matching_parens;
1077
1078 /* Traits for token_pair<T> for tracking matching pairs of braces. */
1079
1080 struct matching_brace_traits
1081 {
1082 static const enum cpp_ttype open_token_type = CPP_OPEN_BRACE;
1083 static const char * const open_gmsgid;
1084 static const enum cpp_ttype close_token_type = CPP_CLOSE_BRACE;
1085 static const char * const close_gmsgid;
1086 };
1087
1088 const char * const matching_brace_traits::open_gmsgid = "expected %<{%>";
1089 const char * const matching_brace_traits::close_gmsgid = "expected %<}%>";
1090
1091 /* "matching_braces" is a token_pair<T> class for tracking matching
1092 pairs of braces. */
1093
1094 typedef token_pair<matching_brace_traits> matching_braces;
1095
1096 /* Get a description of the matching symbol to TYPE e.g. "(" for
1097 CPP_CLOSE_PAREN. */
1098
1099 static const char *
1100 get_matching_symbol (enum cpp_ttype type)
1101 {
1102 switch (type)
1103 {
1104 default:
1105 gcc_unreachable ();
1106 return "";
1107 case CPP_CLOSE_PAREN:
1108 return "(";
1109 case CPP_CLOSE_BRACE:
1110 return "{";
1111 }
1112 }
1113
1114 /* If the next token is of the indicated TYPE, consume it. Otherwise,
1115 issue the error MSGID. If MSGID is NULL then a message has already
1116 been produced and no message will be produced this time. Returns
1117 true if found, false otherwise.
1118
1119 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
1120 within any error as the location of an "opening" token matching
1121 the close token TYPE (e.g. the location of the '(' when TYPE is
1122 CPP_CLOSE_PAREN).
1123
1124 If TYPE_IS_UNIQUE is true (the default) then msgid describes exactly
1125 one type (e.g. "expected %<)%>") and thus it may be reasonable to
1126 attempt to generate a fix-it hint for the problem.
1127 Otherwise msgid describes multiple token types (e.g.
1128 "expected %<;%>, %<,%> or %<)%>"), and thus we shouldn't attempt to
1129 generate a fix-it hint. */
1130
1131 bool
1132 c_parser_require (c_parser *parser,
1133 enum cpp_ttype type,
1134 const char *msgid,
1135 location_t matching_location,
1136 bool type_is_unique)
1137 {
1138 if (c_parser_next_token_is (parser, type))
1139 {
1140 c_parser_consume_token (parser);
1141 return true;
1142 }
1143 else
1144 {
1145 location_t next_token_loc = c_parser_peek_token (parser)->location;
1146 gcc_rich_location richloc (next_token_loc);
1147
1148 /* Potentially supply a fix-it hint, suggesting to add the
1149 missing token immediately after the *previous* token.
1150 This may move the primary location within richloc. */
1151 if (!parser->error && type_is_unique)
1152 maybe_suggest_missing_token_insertion (&richloc, type,
1153 parser->last_token_location);
1154
1155 /* If matching_location != UNKNOWN_LOCATION, highlight it.
1156 Attempt to consolidate diagnostics by printing it as a
1157 secondary range within the main diagnostic. */
1158 bool added_matching_location = false;
1159 if (matching_location != UNKNOWN_LOCATION)
1160 added_matching_location
1161 = richloc.add_location_if_nearby (matching_location);
1162
1163 if (c_parser_error_richloc (parser, msgid, &richloc))
1164 /* If we weren't able to consolidate matching_location, then
1165 print it as a secondary diagnostic. */
1166 if (matching_location != UNKNOWN_LOCATION && !added_matching_location)
1167 inform (matching_location, "to match this %qs",
1168 get_matching_symbol (type));
1169
1170 return false;
1171 }
1172 }
1173
1174 /* If the next token is the indicated keyword, consume it. Otherwise,
1175 issue the error MSGID. Returns true if found, false otherwise. */
1176
1177 static bool
1178 c_parser_require_keyword (c_parser *parser,
1179 enum rid keyword,
1180 const char *msgid)
1181 {
1182 if (c_parser_next_token_is_keyword (parser, keyword))
1183 {
1184 c_parser_consume_token (parser);
1185 return true;
1186 }
1187 else
1188 {
1189 c_parser_error (parser, msgid);
1190 return false;
1191 }
1192 }
1193
1194 /* Like c_parser_require, except that tokens will be skipped until the
1195 desired token is found. An error message is still produced if the
1196 next token is not as expected. If MSGID is NULL then a message has
1197 already been produced and no message will be produced this
1198 time.
1199
1200 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
1201 within any error as the location of an "opening" token matching
1202 the close token TYPE (e.g. the location of the '(' when TYPE is
1203 CPP_CLOSE_PAREN). */
1204
1205 void
1206 c_parser_skip_until_found (c_parser *parser,
1207 enum cpp_ttype type,
1208 const char *msgid,
1209 location_t matching_location)
1210 {
1211 unsigned nesting_depth = 0;
1212
1213 if (c_parser_require (parser, type, msgid, matching_location))
1214 return;
1215
1216 /* Skip tokens until the desired token is found. */
1217 while (true)
1218 {
1219 /* Peek at the next token. */
1220 c_token *token = c_parser_peek_token (parser);
1221 /* If we've reached the token we want, consume it and stop. */
1222 if (token->type == type && !nesting_depth)
1223 {
1224 c_parser_consume_token (parser);
1225 break;
1226 }
1227
1228 /* If we've run out of tokens, stop. */
1229 if (token->type == CPP_EOF)
1230 return;
1231 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
1232 return;
1233 if (token->type == CPP_OPEN_BRACE
1234 || token->type == CPP_OPEN_PAREN
1235 || token->type == CPP_OPEN_SQUARE)
1236 ++nesting_depth;
1237 else if (token->type == CPP_CLOSE_BRACE
1238 || token->type == CPP_CLOSE_PAREN
1239 || token->type == CPP_CLOSE_SQUARE)
1240 {
1241 if (nesting_depth-- == 0)
1242 break;
1243 }
1244 /* Consume this token. */
1245 c_parser_consume_token (parser);
1246 }
1247 parser->error = false;
1248 }
1249
1250 /* Skip tokens until the end of a parameter is found, but do not
1251 consume the comma, semicolon or closing delimiter. */
1252
1253 static void
1254 c_parser_skip_to_end_of_parameter (c_parser *parser)
1255 {
1256 unsigned nesting_depth = 0;
1257
1258 while (true)
1259 {
1260 c_token *token = c_parser_peek_token (parser);
1261 if ((token->type == CPP_COMMA || token->type == CPP_SEMICOLON)
1262 && !nesting_depth)
1263 break;
1264 /* If we've run out of tokens, stop. */
1265 if (token->type == CPP_EOF)
1266 return;
1267 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
1268 return;
1269 if (token->type == CPP_OPEN_BRACE
1270 || token->type == CPP_OPEN_PAREN
1271 || token->type == CPP_OPEN_SQUARE)
1272 ++nesting_depth;
1273 else if (token->type == CPP_CLOSE_BRACE
1274 || token->type == CPP_CLOSE_PAREN
1275 || token->type == CPP_CLOSE_SQUARE)
1276 {
1277 if (nesting_depth-- == 0)
1278 break;
1279 }
1280 /* Consume this token. */
1281 c_parser_consume_token (parser);
1282 }
1283 parser->error = false;
1284 }
1285
1286 /* Expect to be at the end of the pragma directive and consume an
1287 end of line marker. */
1288
1289 static void
1290 c_parser_skip_to_pragma_eol (c_parser *parser, bool error_if_not_eol = true)
1291 {
1292 gcc_assert (parser->in_pragma);
1293 parser->in_pragma = false;
1294
1295 if (error_if_not_eol && c_parser_peek_token (parser)->type != CPP_PRAGMA_EOL)
1296 c_parser_error (parser, "expected end of line");
1297
1298 cpp_ttype token_type;
1299 do
1300 {
1301 c_token *token = c_parser_peek_token (parser);
1302 token_type = token->type;
1303 if (token_type == CPP_EOF)
1304 break;
1305 c_parser_consume_token (parser);
1306 }
1307 while (token_type != CPP_PRAGMA_EOL);
1308
1309 parser->error = false;
1310 }
1311
1312 /* Skip tokens until we have consumed an entire block, or until we
1313 have consumed a non-nested ';'. */
1314
1315 static void
1316 c_parser_skip_to_end_of_block_or_statement (c_parser *parser)
1317 {
1318 unsigned nesting_depth = 0;
1319 bool save_error = parser->error;
1320
1321 while (true)
1322 {
1323 c_token *token;
1324
1325 /* Peek at the next token. */
1326 token = c_parser_peek_token (parser);
1327
1328 switch (token->type)
1329 {
1330 case CPP_EOF:
1331 return;
1332
1333 case CPP_PRAGMA_EOL:
1334 if (parser->in_pragma)
1335 return;
1336 break;
1337
1338 case CPP_SEMICOLON:
1339 /* If the next token is a ';', we have reached the
1340 end of the statement. */
1341 if (!nesting_depth)
1342 {
1343 /* Consume the ';'. */
1344 c_parser_consume_token (parser);
1345 goto finished;
1346 }
1347 break;
1348
1349 case CPP_CLOSE_BRACE:
1350 /* If the next token is a non-nested '}', then we have
1351 reached the end of the current block. */
1352 if (nesting_depth == 0 || --nesting_depth == 0)
1353 {
1354 c_parser_consume_token (parser);
1355 goto finished;
1356 }
1357 break;
1358
1359 case CPP_OPEN_BRACE:
1360 /* If it the next token is a '{', then we are entering a new
1361 block. Consume the entire block. */
1362 ++nesting_depth;
1363 break;
1364
1365 case CPP_PRAGMA:
1366 /* If we see a pragma, consume the whole thing at once. We
1367 have some safeguards against consuming pragmas willy-nilly.
1368 Normally, we'd expect to be here with parser->error set,
1369 which disables these safeguards. But it's possible to get
1370 here for secondary error recovery, after parser->error has
1371 been cleared. */
1372 c_parser_consume_pragma (parser);
1373 c_parser_skip_to_pragma_eol (parser);
1374 parser->error = save_error;
1375 continue;
1376
1377 default:
1378 break;
1379 }
1380
1381 c_parser_consume_token (parser);
1382 }
1383
1384 finished:
1385 parser->error = false;
1386 }
1387
1388 /* CPP's options (initialized by c-opts.c). */
1389 extern cpp_options *cpp_opts;
1390
1391 /* Save the warning flags which are controlled by __extension__. */
1392
1393 static inline int
1394 disable_extension_diagnostics (void)
1395 {
1396 int ret = (pedantic
1397 | (warn_pointer_arith << 1)
1398 | (warn_traditional << 2)
1399 | (flag_iso << 3)
1400 | (warn_long_long << 4)
1401 | (warn_cxx_compat << 5)
1402 | (warn_overlength_strings << 6)
1403 /* warn_c90_c99_compat has three states: -1/0/1, so we must
1404 play tricks to properly restore it. */
1405 | ((warn_c90_c99_compat == 1) << 7)
1406 | ((warn_c90_c99_compat == -1) << 8)
1407 /* Similarly for warn_c99_c11_compat. */
1408 | ((warn_c99_c11_compat == 1) << 9)
1409 | ((warn_c99_c11_compat == -1) << 10)
1410 /* Similarly for warn_c11_c2x_compat. */
1411 | ((warn_c11_c2x_compat == 1) << 11)
1412 | ((warn_c11_c2x_compat == -1) << 12)
1413 );
1414 cpp_opts->cpp_pedantic = pedantic = 0;
1415 warn_pointer_arith = 0;
1416 cpp_opts->cpp_warn_traditional = warn_traditional = 0;
1417 flag_iso = 0;
1418 cpp_opts->cpp_warn_long_long = warn_long_long = 0;
1419 warn_cxx_compat = 0;
1420 warn_overlength_strings = 0;
1421 warn_c90_c99_compat = 0;
1422 warn_c99_c11_compat = 0;
1423 warn_c11_c2x_compat = 0;
1424 return ret;
1425 }
1426
1427 /* Restore the warning flags which are controlled by __extension__.
1428 FLAGS is the return value from disable_extension_diagnostics. */
1429
1430 static inline void
1431 restore_extension_diagnostics (int flags)
1432 {
1433 cpp_opts->cpp_pedantic = pedantic = flags & 1;
1434 warn_pointer_arith = (flags >> 1) & 1;
1435 cpp_opts->cpp_warn_traditional = warn_traditional = (flags >> 2) & 1;
1436 flag_iso = (flags >> 3) & 1;
1437 cpp_opts->cpp_warn_long_long = warn_long_long = (flags >> 4) & 1;
1438 warn_cxx_compat = (flags >> 5) & 1;
1439 warn_overlength_strings = (flags >> 6) & 1;
1440 /* See above for why is this needed. */
1441 warn_c90_c99_compat = (flags >> 7) & 1 ? 1 : ((flags >> 8) & 1 ? -1 : 0);
1442 warn_c99_c11_compat = (flags >> 9) & 1 ? 1 : ((flags >> 10) & 1 ? -1 : 0);
1443 warn_c11_c2x_compat = (flags >> 11) & 1 ? 1 : ((flags >> 12) & 1 ? -1 : 0);
1444 }
1445
1446 /* Helper data structure for parsing #pragma acc routine. */
1447 struct oacc_routine_data {
1448 bool error_seen; /* Set if error has been reported. */
1449 bool fndecl_seen; /* Set if one fn decl/definition has been seen already. */
1450 tree clauses;
1451 location_t loc;
1452 };
1453
1454 static bool c_parser_nth_token_starts_std_attributes (c_parser *,
1455 unsigned int);
1456 static tree c_parser_std_attribute_specifier_sequence (c_parser *);
1457 static void c_parser_external_declaration (c_parser *);
1458 static void c_parser_asm_definition (c_parser *);
1459 static void c_parser_declaration_or_fndef (c_parser *, bool, bool, bool,
1460 bool, bool, tree *, vec<c_token>,
1461 bool have_attrs = false,
1462 tree attrs = NULL,
1463 struct oacc_routine_data * = NULL,
1464 bool * = NULL);
1465 static void c_parser_static_assert_declaration_no_semi (c_parser *);
1466 static void c_parser_static_assert_declaration (c_parser *);
1467 static struct c_typespec c_parser_enum_specifier (c_parser *);
1468 static struct c_typespec c_parser_struct_or_union_specifier (c_parser *);
1469 static tree c_parser_struct_declaration (c_parser *);
1470 static struct c_typespec c_parser_typeof_specifier (c_parser *);
1471 static tree c_parser_alignas_specifier (c_parser *);
1472 static struct c_declarator *c_parser_direct_declarator (c_parser *, bool,
1473 c_dtr_syn, bool *);
1474 static struct c_declarator *c_parser_direct_declarator_inner (c_parser *,
1475 bool,
1476 struct c_declarator *);
1477 static struct c_arg_info *c_parser_parms_declarator (c_parser *, bool, tree,
1478 bool);
1479 static struct c_arg_info *c_parser_parms_list_declarator (c_parser *, tree,
1480 tree, bool);
1481 static struct c_parm *c_parser_parameter_declaration (c_parser *, tree, bool);
1482 static tree c_parser_simple_asm_expr (c_parser *);
1483 static tree c_parser_gnu_attributes (c_parser *);
1484 static struct c_expr c_parser_initializer (c_parser *);
1485 static struct c_expr c_parser_braced_init (c_parser *, tree, bool,
1486 struct obstack *);
1487 static void c_parser_initelt (c_parser *, struct obstack *);
1488 static void c_parser_initval (c_parser *, struct c_expr *,
1489 struct obstack *);
1490 static tree c_parser_compound_statement (c_parser *);
1491 static void c_parser_compound_statement_nostart (c_parser *);
1492 static void c_parser_label (c_parser *);
1493 static void c_parser_statement (c_parser *, bool *, location_t * = NULL);
1494 static void c_parser_statement_after_labels (c_parser *, bool *,
1495 vec<tree> * = NULL);
1496 static tree c_parser_c99_block_statement (c_parser *, bool *,
1497 location_t * = NULL);
1498 static void c_parser_if_statement (c_parser *, bool *, vec<tree> *);
1499 static void c_parser_switch_statement (c_parser *, bool *);
1500 static void c_parser_while_statement (c_parser *, bool, unsigned short, bool *);
1501 static void c_parser_do_statement (c_parser *, bool, unsigned short);
1502 static void c_parser_for_statement (c_parser *, bool, unsigned short, bool *);
1503 static tree c_parser_asm_statement (c_parser *);
1504 static tree c_parser_asm_operands (c_parser *);
1505 static tree c_parser_asm_goto_operands (c_parser *);
1506 static tree c_parser_asm_clobbers (c_parser *);
1507 static struct c_expr c_parser_expr_no_commas (c_parser *, struct c_expr *,
1508 tree = NULL_TREE);
1509 static struct c_expr c_parser_conditional_expression (c_parser *,
1510 struct c_expr *, tree);
1511 static struct c_expr c_parser_binary_expression (c_parser *, struct c_expr *,
1512 tree);
1513 static struct c_expr c_parser_cast_expression (c_parser *, struct c_expr *);
1514 static struct c_expr c_parser_unary_expression (c_parser *);
1515 static struct c_expr c_parser_sizeof_expression (c_parser *);
1516 static struct c_expr c_parser_alignof_expression (c_parser *);
1517 static struct c_expr c_parser_postfix_expression (c_parser *);
1518 static struct c_expr c_parser_postfix_expression_after_paren_type (c_parser *,
1519 struct c_type_name *,
1520 location_t);
1521 static struct c_expr c_parser_postfix_expression_after_primary (c_parser *,
1522 location_t loc,
1523 struct c_expr);
1524 static tree c_parser_transaction (c_parser *, enum rid);
1525 static struct c_expr c_parser_transaction_expression (c_parser *, enum rid);
1526 static tree c_parser_transaction_cancel (c_parser *);
1527 static struct c_expr c_parser_expression (c_parser *);
1528 static struct c_expr c_parser_expression_conv (c_parser *);
1529 static vec<tree, va_gc> *c_parser_expr_list (c_parser *, bool, bool,
1530 vec<tree, va_gc> **, location_t *,
1531 tree *, vec<location_t> *,
1532 unsigned int * = NULL);
1533 static struct c_expr c_parser_has_attribute_expression (c_parser *);
1534
1535 static void c_parser_oacc_declare (c_parser *);
1536 static void c_parser_oacc_enter_exit_data (c_parser *, bool);
1537 static void c_parser_oacc_update (c_parser *);
1538 static void c_parser_omp_construct (c_parser *, bool *);
1539 static void c_parser_omp_threadprivate (c_parser *);
1540 static void c_parser_omp_barrier (c_parser *);
1541 static void c_parser_omp_depobj (c_parser *);
1542 static void c_parser_omp_flush (c_parser *);
1543 static tree c_parser_omp_for_loop (location_t, c_parser *, enum tree_code,
1544 tree, tree *, bool *);
1545 static void c_parser_omp_taskwait (c_parser *);
1546 static void c_parser_omp_taskyield (c_parser *);
1547 static void c_parser_omp_cancel (c_parser *);
1548
1549 enum pragma_context { pragma_external, pragma_struct, pragma_param,
1550 pragma_stmt, pragma_compound };
1551 static bool c_parser_pragma (c_parser *, enum pragma_context, bool *);
1552 static void c_parser_omp_cancellation_point (c_parser *, enum pragma_context);
1553 static bool c_parser_omp_target (c_parser *, enum pragma_context, bool *);
1554 static void c_parser_omp_end_declare_target (c_parser *);
1555 static void c_parser_omp_declare (c_parser *, enum pragma_context);
1556 static void c_parser_omp_requires (c_parser *);
1557 static bool c_parser_omp_ordered (c_parser *, enum pragma_context, bool *);
1558 static void c_parser_oacc_routine (c_parser *, enum pragma_context);
1559
1560 /* These Objective-C parser functions are only ever called when
1561 compiling Objective-C. */
1562 static void c_parser_objc_class_definition (c_parser *, tree);
1563 static void c_parser_objc_class_instance_variables (c_parser *);
1564 static void c_parser_objc_class_declaration (c_parser *);
1565 static void c_parser_objc_alias_declaration (c_parser *);
1566 static void c_parser_objc_protocol_definition (c_parser *, tree);
1567 static bool c_parser_objc_method_type (c_parser *);
1568 static void c_parser_objc_method_definition (c_parser *);
1569 static void c_parser_objc_methodprotolist (c_parser *);
1570 static void c_parser_objc_methodproto (c_parser *);
1571 static tree c_parser_objc_method_decl (c_parser *, bool, tree *, tree *);
1572 static tree c_parser_objc_type_name (c_parser *);
1573 static tree c_parser_objc_protocol_refs (c_parser *);
1574 static void c_parser_objc_try_catch_finally_statement (c_parser *);
1575 static void c_parser_objc_synchronized_statement (c_parser *);
1576 static tree c_parser_objc_selector (c_parser *);
1577 static tree c_parser_objc_selector_arg (c_parser *);
1578 static tree c_parser_objc_receiver (c_parser *);
1579 static tree c_parser_objc_message_args (c_parser *);
1580 static tree c_parser_objc_keywordexpr (c_parser *);
1581 static void c_parser_objc_at_property_declaration (c_parser *);
1582 static void c_parser_objc_at_synthesize_declaration (c_parser *);
1583 static void c_parser_objc_at_dynamic_declaration (c_parser *);
1584 static bool c_parser_objc_diagnose_bad_element_prefix
1585 (c_parser *, struct c_declspecs *);
1586
1587 static void c_parser_parse_rtl_body (c_parser *parser, char *start_with_pass);
1588
1589 /* Parse a translation unit (C90 6.7, C99 6.9, C11 6.9).
1590
1591 translation-unit:
1592 external-declarations
1593
1594 external-declarations:
1595 external-declaration
1596 external-declarations external-declaration
1597
1598 GNU extensions:
1599
1600 translation-unit:
1601 empty
1602 */
1603
1604 static void
1605 c_parser_translation_unit (c_parser *parser)
1606 {
1607 if (c_parser_next_token_is (parser, CPP_EOF))
1608 {
1609 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
1610 "ISO C forbids an empty translation unit");
1611 }
1612 else
1613 {
1614 void *obstack_position = obstack_alloc (&parser_obstack, 0);
1615 mark_valid_location_for_stdc_pragma (false);
1616 do
1617 {
1618 ggc_collect ();
1619 c_parser_external_declaration (parser);
1620 obstack_free (&parser_obstack, obstack_position);
1621 }
1622 while (c_parser_next_token_is_not (parser, CPP_EOF));
1623 }
1624
1625 unsigned int i;
1626 tree decl;
1627 FOR_EACH_VEC_ELT (incomplete_record_decls, i, decl)
1628 if (DECL_SIZE (decl) == NULL_TREE && TREE_TYPE (decl) != error_mark_node)
1629 error ("storage size of %q+D isn%'t known", decl);
1630
1631 if (current_omp_declare_target_attribute)
1632 {
1633 if (!errorcount)
1634 error ("%<#pragma omp declare target%> without corresponding "
1635 "%<#pragma omp end declare target%>");
1636 current_omp_declare_target_attribute = 0;
1637 }
1638 }
1639
1640 /* Parse an external declaration (C90 6.7, C99 6.9, C11 6.9).
1641
1642 external-declaration:
1643 function-definition
1644 declaration
1645
1646 GNU extensions:
1647
1648 external-declaration:
1649 asm-definition
1650 ;
1651 __extension__ external-declaration
1652
1653 Objective-C:
1654
1655 external-declaration:
1656 objc-class-definition
1657 objc-class-declaration
1658 objc-alias-declaration
1659 objc-protocol-definition
1660 objc-method-definition
1661 @end
1662 */
1663
1664 static void
1665 c_parser_external_declaration (c_parser *parser)
1666 {
1667 int ext;
1668 switch (c_parser_peek_token (parser)->type)
1669 {
1670 case CPP_KEYWORD:
1671 switch (c_parser_peek_token (parser)->keyword)
1672 {
1673 case RID_EXTENSION:
1674 ext = disable_extension_diagnostics ();
1675 c_parser_consume_token (parser);
1676 c_parser_external_declaration (parser);
1677 restore_extension_diagnostics (ext);
1678 break;
1679 case RID_ASM:
1680 c_parser_asm_definition (parser);
1681 break;
1682 case RID_AT_INTERFACE:
1683 case RID_AT_IMPLEMENTATION:
1684 gcc_assert (c_dialect_objc ());
1685 c_parser_objc_class_definition (parser, NULL_TREE);
1686 break;
1687 case RID_AT_CLASS:
1688 gcc_assert (c_dialect_objc ());
1689 c_parser_objc_class_declaration (parser);
1690 break;
1691 case RID_AT_ALIAS:
1692 gcc_assert (c_dialect_objc ());
1693 c_parser_objc_alias_declaration (parser);
1694 break;
1695 case RID_AT_PROTOCOL:
1696 gcc_assert (c_dialect_objc ());
1697 c_parser_objc_protocol_definition (parser, NULL_TREE);
1698 break;
1699 case RID_AT_PROPERTY:
1700 gcc_assert (c_dialect_objc ());
1701 c_parser_objc_at_property_declaration (parser);
1702 break;
1703 case RID_AT_SYNTHESIZE:
1704 gcc_assert (c_dialect_objc ());
1705 c_parser_objc_at_synthesize_declaration (parser);
1706 break;
1707 case RID_AT_DYNAMIC:
1708 gcc_assert (c_dialect_objc ());
1709 c_parser_objc_at_dynamic_declaration (parser);
1710 break;
1711 case RID_AT_END:
1712 gcc_assert (c_dialect_objc ());
1713 c_parser_consume_token (parser);
1714 objc_finish_implementation ();
1715 break;
1716 default:
1717 goto decl_or_fndef;
1718 }
1719 break;
1720 case CPP_SEMICOLON:
1721 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
1722 "ISO C does not allow extra %<;%> outside of a function");
1723 c_parser_consume_token (parser);
1724 break;
1725 case CPP_PRAGMA:
1726 mark_valid_location_for_stdc_pragma (true);
1727 c_parser_pragma (parser, pragma_external, NULL);
1728 mark_valid_location_for_stdc_pragma (false);
1729 break;
1730 case CPP_PLUS:
1731 case CPP_MINUS:
1732 if (c_dialect_objc ())
1733 {
1734 c_parser_objc_method_definition (parser);
1735 break;
1736 }
1737 /* Else fall through, and yield a syntax error trying to parse
1738 as a declaration or function definition. */
1739 /* FALLTHRU */
1740 default:
1741 decl_or_fndef:
1742 /* A declaration or a function definition (or, in Objective-C,
1743 an @interface or @protocol with prefix attributes). We can
1744 only tell which after parsing the declaration specifiers, if
1745 any, and the first declarator. */
1746 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
1747 NULL, vNULL);
1748 break;
1749 }
1750 }
1751
1752 static void c_finish_omp_declare_simd (c_parser *, tree, tree, vec<c_token>);
1753 static void c_finish_oacc_routine (struct oacc_routine_data *, tree, bool);
1754
1755 /* Build and add a DEBUG_BEGIN_STMT statement with location LOC. */
1756
1757 static void
1758 add_debug_begin_stmt (location_t loc)
1759 {
1760 /* Don't add DEBUG_BEGIN_STMTs outside of functions, see PR84721. */
1761 if (!MAY_HAVE_DEBUG_MARKER_STMTS || !building_stmt_list_p ())
1762 return;
1763
1764 tree stmt = build0 (DEBUG_BEGIN_STMT, void_type_node);
1765 SET_EXPR_LOCATION (stmt, loc);
1766 add_stmt (stmt);
1767 }
1768
1769 /* Parse a declaration or function definition (C90 6.5, 6.7.1, C99
1770 6.7, 6.9.1, C11 6.7, 6.9.1). If FNDEF_OK is true, a function definition
1771 is accepted; otherwise (old-style parameter declarations) only other
1772 declarations are accepted. If STATIC_ASSERT_OK is true, a static
1773 assertion is accepted; otherwise (old-style parameter declarations)
1774 it is not. If NESTED is true, we are inside a function or parsing
1775 old-style parameter declarations; any functions encountered are
1776 nested functions and declaration specifiers are required; otherwise
1777 we are at top level and functions are normal functions and
1778 declaration specifiers may be optional. If EMPTY_OK is true, empty
1779 declarations are OK (subject to all other constraints); otherwise
1780 (old-style parameter declarations) they are diagnosed. If
1781 START_ATTR_OK is true, the declaration specifiers may start with
1782 attributes (GNU or standard); otherwise they may not.
1783 OBJC_FOREACH_OBJECT_DECLARATION can be used to get back the parsed
1784 declaration when parsing an Objective-C foreach statement.
1785 FALLTHRU_ATTR_P is used to signal whether this function parsed
1786 "__attribute__((fallthrough));". ATTRS are any standard attributes
1787 parsed in the caller (in contexts where such attributes had to be
1788 parsed to determine whether what follows is a declaration or a
1789 statement); HAVE_ATTRS says whether there were any such attributes
1790 (even empty).
1791
1792 declaration:
1793 declaration-specifiers init-declarator-list[opt] ;
1794 static_assert-declaration
1795
1796 function-definition:
1797 declaration-specifiers[opt] declarator declaration-list[opt]
1798 compound-statement
1799
1800 declaration-list:
1801 declaration
1802 declaration-list declaration
1803
1804 init-declarator-list:
1805 init-declarator
1806 init-declarator-list , init-declarator
1807
1808 init-declarator:
1809 declarator simple-asm-expr[opt] gnu-attributes[opt]
1810 declarator simple-asm-expr[opt] gnu-attributes[opt] = initializer
1811
1812 GNU extensions:
1813
1814 nested-function-definition:
1815 declaration-specifiers declarator declaration-list[opt]
1816 compound-statement
1817
1818 attribute ;
1819
1820 Objective-C:
1821 gnu-attributes objc-class-definition
1822 gnu-attributes objc-category-definition
1823 gnu-attributes objc-protocol-definition
1824
1825 The simple-asm-expr and gnu-attributes are GNU extensions.
1826
1827 This function does not handle __extension__; that is handled in its
1828 callers. ??? Following the old parser, __extension__ may start
1829 external declarations, declarations in functions and declarations
1830 at the start of "for" loops, but not old-style parameter
1831 declarations.
1832
1833 C99 requires declaration specifiers in a function definition; the
1834 absence is diagnosed through the diagnosis of implicit int. In GNU
1835 C we also allow but diagnose declarations without declaration
1836 specifiers, but only at top level (elsewhere they conflict with
1837 other syntax).
1838
1839 In Objective-C, declarations of the looping variable in a foreach
1840 statement are exceptionally terminated by 'in' (for example, 'for
1841 (NSObject *object in array) { ... }').
1842
1843 OpenMP:
1844
1845 declaration:
1846 threadprivate-directive
1847
1848 GIMPLE:
1849
1850 gimple-function-definition:
1851 declaration-specifiers[opt] __GIMPLE (gimple-or-rtl-pass-list) declarator
1852 declaration-list[opt] compound-statement
1853
1854 rtl-function-definition:
1855 declaration-specifiers[opt] __RTL (gimple-or-rtl-pass-list) declarator
1856 declaration-list[opt] compound-statement */
1857
1858 static void
1859 c_parser_declaration_or_fndef (c_parser *parser, bool fndef_ok,
1860 bool static_assert_ok, bool empty_ok,
1861 bool nested, bool start_attr_ok,
1862 tree *objc_foreach_object_declaration,
1863 vec<c_token> omp_declare_simd_clauses,
1864 bool have_attrs, tree attrs,
1865 struct oacc_routine_data *oacc_routine_data,
1866 bool *fallthru_attr_p)
1867 {
1868 struct c_declspecs *specs;
1869 tree prefix_attrs;
1870 tree all_prefix_attrs;
1871 bool diagnosed_no_specs = false;
1872 location_t here = c_parser_peek_token (parser)->location;
1873
1874 add_debug_begin_stmt (c_parser_peek_token (parser)->location);
1875
1876 if (static_assert_ok
1877 && c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
1878 {
1879 c_parser_static_assert_declaration (parser);
1880 return;
1881 }
1882 specs = build_null_declspecs ();
1883
1884 /* Handle any standard attributes parsed in the caller. */
1885 if (have_attrs)
1886 {
1887 declspecs_add_attrs (here, specs, attrs);
1888 specs->non_std_attrs_seen_p = false;
1889 }
1890
1891 /* Try to detect an unknown type name when we have "A B" or "A *B". */
1892 if (c_parser_peek_token (parser)->type == CPP_NAME
1893 && c_parser_peek_token (parser)->id_kind == C_ID_ID
1894 && (c_parser_peek_2nd_token (parser)->type == CPP_NAME
1895 || c_parser_peek_2nd_token (parser)->type == CPP_MULT)
1896 && (!nested || !lookup_name (c_parser_peek_token (parser)->value)))
1897 {
1898 tree name = c_parser_peek_token (parser)->value;
1899
1900 /* Issue a warning about NAME being an unknown type name, perhaps
1901 with some kind of hint.
1902 If the user forgot a "struct" etc, suggest inserting
1903 it. Otherwise, attempt to look for misspellings. */
1904 gcc_rich_location richloc (here);
1905 if (tag_exists_p (RECORD_TYPE, name))
1906 {
1907 /* This is not C++ with its implicit typedef. */
1908 richloc.add_fixit_insert_before ("struct ");
1909 error_at (&richloc,
1910 "unknown type name %qE;"
1911 " use %<struct%> keyword to refer to the type",
1912 name);
1913 }
1914 else if (tag_exists_p (UNION_TYPE, name))
1915 {
1916 richloc.add_fixit_insert_before ("union ");
1917 error_at (&richloc,
1918 "unknown type name %qE;"
1919 " use %<union%> keyword to refer to the type",
1920 name);
1921 }
1922 else if (tag_exists_p (ENUMERAL_TYPE, name))
1923 {
1924 richloc.add_fixit_insert_before ("enum ");
1925 error_at (&richloc,
1926 "unknown type name %qE;"
1927 " use %<enum%> keyword to refer to the type",
1928 name);
1929 }
1930 else
1931 {
1932 auto_diagnostic_group d;
1933 name_hint hint = lookup_name_fuzzy (name, FUZZY_LOOKUP_TYPENAME,
1934 here);
1935 if (const char *suggestion = hint.suggestion ())
1936 {
1937 richloc.add_fixit_replace (suggestion);
1938 error_at (&richloc,
1939 "unknown type name %qE; did you mean %qs?",
1940 name, suggestion);
1941 }
1942 else
1943 error_at (here, "unknown type name %qE", name);
1944 }
1945
1946 /* Parse declspecs normally to get a correct pointer type, but avoid
1947 a further "fails to be a type name" error. Refuse nested functions
1948 since it is not how the user likely wants us to recover. */
1949 c_parser_peek_token (parser)->type = CPP_KEYWORD;
1950 c_parser_peek_token (parser)->keyword = RID_VOID;
1951 c_parser_peek_token (parser)->value = error_mark_node;
1952 fndef_ok = !nested;
1953 }
1954
1955 /* When there are standard attributes at the start of the
1956 declaration (to apply to the entity being declared), an
1957 init-declarator-list or function definition must be present. */
1958 if (c_parser_nth_token_starts_std_attributes (parser, 1))
1959 have_attrs = true;
1960
1961 c_parser_declspecs (parser, specs, true, true, start_attr_ok,
1962 true, true, start_attr_ok, true, cla_nonabstract_decl);
1963 if (parser->error)
1964 {
1965 c_parser_skip_to_end_of_block_or_statement (parser);
1966 return;
1967 }
1968 if (nested && !specs->declspecs_seen_p)
1969 {
1970 c_parser_error (parser, "expected declaration specifiers");
1971 c_parser_skip_to_end_of_block_or_statement (parser);
1972 return;
1973 }
1974
1975 finish_declspecs (specs);
1976 bool auto_type_p = specs->typespec_word == cts_auto_type;
1977 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1978 {
1979 if (auto_type_p)
1980 error_at (here, "%<__auto_type%> in empty declaration");
1981 else if (specs->typespec_kind == ctsk_none
1982 && attribute_fallthrough_p (specs->attrs))
1983 {
1984 if (fallthru_attr_p != NULL)
1985 *fallthru_attr_p = true;
1986 if (nested)
1987 {
1988 tree fn = build_call_expr_internal_loc (here, IFN_FALLTHROUGH,
1989 void_type_node, 0);
1990 add_stmt (fn);
1991 }
1992 else
1993 pedwarn (here, OPT_Wattributes,
1994 "%<fallthrough%> attribute at top level");
1995 }
1996 else if (empty_ok && !(have_attrs
1997 && specs->non_std_attrs_seen_p))
1998 shadow_tag (specs);
1999 else
2000 {
2001 shadow_tag_warned (specs, 1);
2002 pedwarn (here, 0, "empty declaration");
2003 }
2004 c_parser_consume_token (parser);
2005 if (oacc_routine_data)
2006 c_finish_oacc_routine (oacc_routine_data, NULL_TREE, false);
2007 return;
2008 }
2009
2010 /* Provide better error recovery. Note that a type name here is usually
2011 better diagnosed as a redeclaration. */
2012 if (empty_ok
2013 && specs->typespec_kind == ctsk_tagdef
2014 && c_parser_next_token_starts_declspecs (parser)
2015 && !c_parser_next_token_is (parser, CPP_NAME))
2016 {
2017 c_parser_error (parser, "expected %<;%>, identifier or %<(%>");
2018 parser->error = false;
2019 shadow_tag_warned (specs, 1);
2020 return;
2021 }
2022 else if (c_dialect_objc () && !auto_type_p)
2023 {
2024 /* Prefix attributes are an error on method decls. */
2025 switch (c_parser_peek_token (parser)->type)
2026 {
2027 case CPP_PLUS:
2028 case CPP_MINUS:
2029 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
2030 return;
2031 if (specs->attrs)
2032 {
2033 warning_at (c_parser_peek_token (parser)->location,
2034 OPT_Wattributes,
2035 "prefix attributes are ignored for methods");
2036 specs->attrs = NULL_TREE;
2037 }
2038 if (fndef_ok)
2039 c_parser_objc_method_definition (parser);
2040 else
2041 c_parser_objc_methodproto (parser);
2042 return;
2043 break;
2044 default:
2045 break;
2046 }
2047 /* This is where we parse 'attributes @interface ...',
2048 'attributes @implementation ...', 'attributes @protocol ...'
2049 (where attributes could be, for example, __attribute__
2050 ((deprecated)).
2051 */
2052 switch (c_parser_peek_token (parser)->keyword)
2053 {
2054 case RID_AT_INTERFACE:
2055 {
2056 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
2057 return;
2058 c_parser_objc_class_definition (parser, specs->attrs);
2059 return;
2060 }
2061 break;
2062 case RID_AT_IMPLEMENTATION:
2063 {
2064 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
2065 return;
2066 if (specs->attrs)
2067 {
2068 warning_at (c_parser_peek_token (parser)->location,
2069 OPT_Wattributes,
2070 "prefix attributes are ignored for implementations");
2071 specs->attrs = NULL_TREE;
2072 }
2073 c_parser_objc_class_definition (parser, NULL_TREE);
2074 return;
2075 }
2076 break;
2077 case RID_AT_PROTOCOL:
2078 {
2079 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
2080 return;
2081 c_parser_objc_protocol_definition (parser, specs->attrs);
2082 return;
2083 }
2084 break;
2085 case RID_AT_ALIAS:
2086 case RID_AT_CLASS:
2087 case RID_AT_END:
2088 case RID_AT_PROPERTY:
2089 if (specs->attrs)
2090 {
2091 c_parser_error (parser, "unexpected attribute");
2092 specs->attrs = NULL;
2093 }
2094 break;
2095 default:
2096 break;
2097 }
2098 }
2099 else if (attribute_fallthrough_p (specs->attrs))
2100 warning_at (here, OPT_Wattributes,
2101 "%<fallthrough%> attribute not followed by %<;%>");
2102
2103 pending_xref_error ();
2104 prefix_attrs = specs->attrs;
2105 all_prefix_attrs = prefix_attrs;
2106 specs->attrs = NULL_TREE;
2107 while (true)
2108 {
2109 struct c_declarator *declarator;
2110 bool dummy = false;
2111 timevar_id_t tv;
2112 tree fnbody = NULL_TREE;
2113 /* Declaring either one or more declarators (in which case we
2114 should diagnose if there were no declaration specifiers) or a
2115 function definition (in which case the diagnostic for
2116 implicit int suffices). */
2117 declarator = c_parser_declarator (parser,
2118 specs->typespec_kind != ctsk_none,
2119 C_DTR_NORMAL, &dummy);
2120 if (declarator == NULL)
2121 {
2122 if (omp_declare_simd_clauses.exists ())
2123 c_finish_omp_declare_simd (parser, NULL_TREE, NULL_TREE,
2124 omp_declare_simd_clauses);
2125 if (oacc_routine_data)
2126 c_finish_oacc_routine (oacc_routine_data, NULL_TREE, false);
2127 c_parser_skip_to_end_of_block_or_statement (parser);
2128 return;
2129 }
2130 if (auto_type_p && declarator->kind != cdk_id)
2131 {
2132 error_at (here,
2133 "%<__auto_type%> requires a plain identifier"
2134 " as declarator");
2135 c_parser_skip_to_end_of_block_or_statement (parser);
2136 return;
2137 }
2138 if (c_parser_next_token_is (parser, CPP_EQ)
2139 || c_parser_next_token_is (parser, CPP_COMMA)
2140 || c_parser_next_token_is (parser, CPP_SEMICOLON)
2141 || c_parser_next_token_is_keyword (parser, RID_ASM)
2142 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE)
2143 || c_parser_next_token_is_keyword (parser, RID_IN))
2144 {
2145 tree asm_name = NULL_TREE;
2146 tree postfix_attrs = NULL_TREE;
2147 if (!diagnosed_no_specs && !specs->declspecs_seen_p)
2148 {
2149 diagnosed_no_specs = true;
2150 pedwarn (here, 0, "data definition has no type or storage class");
2151 }
2152 /* Having seen a data definition, there cannot now be a
2153 function definition. */
2154 fndef_ok = false;
2155 if (c_parser_next_token_is_keyword (parser, RID_ASM))
2156 asm_name = c_parser_simple_asm_expr (parser);
2157 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2158 {
2159 postfix_attrs = c_parser_gnu_attributes (parser);
2160 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2161 {
2162 /* This means there is an attribute specifier after
2163 the declarator in a function definition. Provide
2164 some more information for the user. */
2165 error_at (here, "attributes should be specified before the "
2166 "declarator in a function definition");
2167 c_parser_skip_to_end_of_block_or_statement (parser);
2168 return;
2169 }
2170 }
2171 if (c_parser_next_token_is (parser, CPP_EQ))
2172 {
2173 tree d;
2174 struct c_expr init;
2175 location_t init_loc;
2176 c_parser_consume_token (parser);
2177 if (auto_type_p)
2178 {
2179 init_loc = c_parser_peek_token (parser)->location;
2180 rich_location richloc (line_table, init_loc);
2181 start_init (NULL_TREE, asm_name, global_bindings_p (), &richloc);
2182 /* A parameter is initialized, which is invalid. Don't
2183 attempt to instrument the initializer. */
2184 int flag_sanitize_save = flag_sanitize;
2185 if (nested && !empty_ok)
2186 flag_sanitize = 0;
2187 init = c_parser_expr_no_commas (parser, NULL);
2188 flag_sanitize = flag_sanitize_save;
2189 if (TREE_CODE (init.value) == COMPONENT_REF
2190 && DECL_C_BIT_FIELD (TREE_OPERAND (init.value, 1)))
2191 error_at (here,
2192 "%<__auto_type%> used with a bit-field"
2193 " initializer");
2194 init = convert_lvalue_to_rvalue (init_loc, init, true, true);
2195 tree init_type = TREE_TYPE (init.value);
2196 /* As with typeof, remove all qualifiers from atomic types. */
2197 if (init_type != error_mark_node && TYPE_ATOMIC (init_type))
2198 init_type
2199 = c_build_qualified_type (init_type, TYPE_UNQUALIFIED);
2200 bool vm_type = variably_modified_type_p (init_type,
2201 NULL_TREE);
2202 if (vm_type)
2203 init.value = save_expr (init.value);
2204 finish_init ();
2205 specs->typespec_kind = ctsk_typeof;
2206 specs->locations[cdw_typedef] = init_loc;
2207 specs->typedef_p = true;
2208 specs->type = init_type;
2209 if (vm_type)
2210 {
2211 bool maybe_const = true;
2212 tree type_expr = c_fully_fold (init.value, false,
2213 &maybe_const);
2214 specs->expr_const_operands &= maybe_const;
2215 if (specs->expr)
2216 specs->expr = build2 (COMPOUND_EXPR,
2217 TREE_TYPE (type_expr),
2218 specs->expr, type_expr);
2219 else
2220 specs->expr = type_expr;
2221 }
2222 d = start_decl (declarator, specs, true,
2223 chainon (postfix_attrs, all_prefix_attrs));
2224 if (!d)
2225 d = error_mark_node;
2226 if (omp_declare_simd_clauses.exists ())
2227 c_finish_omp_declare_simd (parser, d, NULL_TREE,
2228 omp_declare_simd_clauses);
2229 }
2230 else
2231 {
2232 /* The declaration of the variable is in effect while
2233 its initializer is parsed. */
2234 d = start_decl (declarator, specs, true,
2235 chainon (postfix_attrs, all_prefix_attrs));
2236 if (!d)
2237 d = error_mark_node;
2238 if (omp_declare_simd_clauses.exists ())
2239 c_finish_omp_declare_simd (parser, d, NULL_TREE,
2240 omp_declare_simd_clauses);
2241 init_loc = c_parser_peek_token (parser)->location;
2242 rich_location richloc (line_table, init_loc);
2243 start_init (d, asm_name, global_bindings_p (), &richloc);
2244 /* A parameter is initialized, which is invalid. Don't
2245 attempt to instrument the initializer. */
2246 int flag_sanitize_save = flag_sanitize;
2247 if (TREE_CODE (d) == PARM_DECL)
2248 flag_sanitize = 0;
2249 init = c_parser_initializer (parser);
2250 flag_sanitize = flag_sanitize_save;
2251 finish_init ();
2252 }
2253 if (oacc_routine_data)
2254 c_finish_oacc_routine (oacc_routine_data, d, false);
2255 if (d != error_mark_node)
2256 {
2257 maybe_warn_string_init (init_loc, TREE_TYPE (d), init);
2258 finish_decl (d, init_loc, init.value,
2259 init.original_type, asm_name);
2260 }
2261 }
2262 else
2263 {
2264 if (auto_type_p)
2265 {
2266 error_at (here,
2267 "%<__auto_type%> requires an initialized "
2268 "data declaration");
2269 c_parser_skip_to_end_of_block_or_statement (parser);
2270 return;
2271 }
2272 tree d = start_decl (declarator, specs, false,
2273 chainon (postfix_attrs,
2274 all_prefix_attrs));
2275 if (d
2276 && TREE_CODE (d) == FUNCTION_DECL
2277 && DECL_ARGUMENTS (d) == NULL_TREE
2278 && DECL_INITIAL (d) == NULL_TREE)
2279 {
2280 /* Find the innermost declarator that is neither cdk_id
2281 nor cdk_attrs. */
2282 const struct c_declarator *decl = declarator;
2283 const struct c_declarator *last_non_id_attrs = NULL;
2284
2285 while (decl)
2286 switch (decl->kind)
2287 {
2288 case cdk_array:
2289 case cdk_function:
2290 case cdk_pointer:
2291 last_non_id_attrs = decl;
2292 decl = decl->declarator;
2293 break;
2294
2295 case cdk_attrs:
2296 decl = decl->declarator;
2297 break;
2298
2299 case cdk_id:
2300 decl = 0;
2301 break;
2302
2303 default:
2304 gcc_unreachable ();
2305 }
2306
2307 /* If it exists and is cdk_function, use its parameters. */
2308 if (last_non_id_attrs
2309 && last_non_id_attrs->kind == cdk_function)
2310 DECL_ARGUMENTS (d) = last_non_id_attrs->u.arg_info->parms;
2311 }
2312 if (omp_declare_simd_clauses.exists ())
2313 {
2314 tree parms = NULL_TREE;
2315 if (d && TREE_CODE (d) == FUNCTION_DECL)
2316 {
2317 struct c_declarator *ce = declarator;
2318 while (ce != NULL)
2319 if (ce->kind == cdk_function)
2320 {
2321 parms = ce->u.arg_info->parms;
2322 break;
2323 }
2324 else
2325 ce = ce->declarator;
2326 }
2327 if (parms)
2328 temp_store_parm_decls (d, parms);
2329 c_finish_omp_declare_simd (parser, d, parms,
2330 omp_declare_simd_clauses);
2331 if (parms)
2332 temp_pop_parm_decls ();
2333 }
2334 if (oacc_routine_data)
2335 c_finish_oacc_routine (oacc_routine_data, d, false);
2336 if (d)
2337 finish_decl (d, UNKNOWN_LOCATION, NULL_TREE,
2338 NULL_TREE, asm_name);
2339
2340 if (c_parser_next_token_is_keyword (parser, RID_IN))
2341 {
2342 if (d)
2343 *objc_foreach_object_declaration = d;
2344 else
2345 *objc_foreach_object_declaration = error_mark_node;
2346 }
2347 }
2348 if (c_parser_next_token_is (parser, CPP_COMMA))
2349 {
2350 if (auto_type_p)
2351 {
2352 error_at (here,
2353 "%<__auto_type%> may only be used with"
2354 " a single declarator");
2355 c_parser_skip_to_end_of_block_or_statement (parser);
2356 return;
2357 }
2358 c_parser_consume_token (parser);
2359 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2360 all_prefix_attrs = chainon (c_parser_gnu_attributes (parser),
2361 prefix_attrs);
2362 else
2363 all_prefix_attrs = prefix_attrs;
2364 continue;
2365 }
2366 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2367 {
2368 c_parser_consume_token (parser);
2369 return;
2370 }
2371 else if (c_parser_next_token_is_keyword (parser, RID_IN))
2372 {
2373 /* This can only happen in Objective-C: we found the
2374 'in' that terminates the declaration inside an
2375 Objective-C foreach statement. Do not consume the
2376 token, so that the caller can use it to determine
2377 that this indeed is a foreach context. */
2378 return;
2379 }
2380 else
2381 {
2382 c_parser_error (parser, "expected %<,%> or %<;%>");
2383 c_parser_skip_to_end_of_block_or_statement (parser);
2384 return;
2385 }
2386 }
2387 else if (auto_type_p)
2388 {
2389 error_at (here,
2390 "%<__auto_type%> requires an initialized data declaration");
2391 c_parser_skip_to_end_of_block_or_statement (parser);
2392 return;
2393 }
2394 else if (!fndef_ok)
2395 {
2396 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, "
2397 "%<asm%> or %<__attribute__%>");
2398 c_parser_skip_to_end_of_block_or_statement (parser);
2399 return;
2400 }
2401 /* Function definition (nested or otherwise). */
2402 if (nested)
2403 {
2404 pedwarn (here, OPT_Wpedantic, "ISO C forbids nested functions");
2405 c_push_function_context ();
2406 }
2407 if (!start_function (specs, declarator, all_prefix_attrs))
2408 {
2409 /* At this point we've consumed:
2410 declaration-specifiers declarator
2411 and the next token isn't CPP_EQ, CPP_COMMA, CPP_SEMICOLON,
2412 RID_ASM, RID_ATTRIBUTE, or RID_IN,
2413 but the
2414 declaration-specifiers declarator
2415 aren't grokkable as a function definition, so we have
2416 an error. */
2417 gcc_assert (!c_parser_next_token_is (parser, CPP_SEMICOLON));
2418 if (c_parser_next_token_starts_declspecs (parser))
2419 {
2420 /* If we have
2421 declaration-specifiers declarator decl-specs
2422 then assume we have a missing semicolon, which would
2423 give us:
2424 declaration-specifiers declarator decl-specs
2425 ^
2426 ;
2427 <~~~~~~~~~ declaration ~~~~~~~~~~>
2428 Use c_parser_require to get an error with a fix-it hint. */
2429 c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>");
2430 parser->error = false;
2431 }
2432 else
2433 {
2434 /* This can appear in many cases looking nothing like a
2435 function definition, so we don't give a more specific
2436 error suggesting there was one. */
2437 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, %<asm%> "
2438 "or %<__attribute__%>");
2439 }
2440 if (nested)
2441 c_pop_function_context ();
2442 break;
2443 }
2444
2445 if (DECL_DECLARED_INLINE_P (current_function_decl))
2446 tv = TV_PARSE_INLINE;
2447 else
2448 tv = TV_PARSE_FUNC;
2449 auto_timevar at (g_timer, tv);
2450
2451 /* Parse old-style parameter declarations. ??? Attributes are
2452 not allowed to start declaration specifiers here because of a
2453 syntax conflict between a function declaration with attribute
2454 suffix and a function definition with an attribute prefix on
2455 first old-style parameter declaration. Following the old
2456 parser, they are not accepted on subsequent old-style
2457 parameter declarations either. However, there is no
2458 ambiguity after the first declaration, nor indeed on the
2459 first as long as we don't allow postfix attributes after a
2460 declarator with a nonempty identifier list in a definition;
2461 and postfix attributes have never been accepted here in
2462 function definitions either. */
2463 while (c_parser_next_token_is_not (parser, CPP_EOF)
2464 && c_parser_next_token_is_not (parser, CPP_OPEN_BRACE))
2465 c_parser_declaration_or_fndef (parser, false, false, false,
2466 true, false, NULL, vNULL);
2467 store_parm_decls ();
2468 if (omp_declare_simd_clauses.exists ())
2469 c_finish_omp_declare_simd (parser, current_function_decl, NULL_TREE,
2470 omp_declare_simd_clauses);
2471 if (oacc_routine_data)
2472 c_finish_oacc_routine (oacc_routine_data, current_function_decl, true);
2473 DECL_STRUCT_FUNCTION (current_function_decl)->function_start_locus
2474 = c_parser_peek_token (parser)->location;
2475
2476 /* If the definition was marked with __RTL, use the RTL parser now,
2477 consuming the function body. */
2478 if (specs->declspec_il == cdil_rtl)
2479 {
2480 c_parser_parse_rtl_body (parser, specs->gimple_or_rtl_pass);
2481
2482 /* Normally, store_parm_decls sets next_is_function_body,
2483 anticipating a function body. We need a push_scope/pop_scope
2484 pair to flush out this state, or subsequent function parsing
2485 will go wrong. */
2486 push_scope ();
2487 pop_scope ();
2488
2489 finish_function ();
2490 return;
2491 }
2492 /* If the definition was marked with __GIMPLE then parse the
2493 function body as GIMPLE. */
2494 else if (specs->declspec_il != cdil_none)
2495 {
2496 bool saved = in_late_binary_op;
2497 in_late_binary_op = true;
2498 c_parser_parse_gimple_body (parser, specs->gimple_or_rtl_pass,
2499 specs->declspec_il,
2500 specs->entry_bb_count);
2501 in_late_binary_op = saved;
2502 }
2503 else
2504 fnbody = c_parser_compound_statement (parser);
2505 tree fndecl = current_function_decl;
2506 if (nested)
2507 {
2508 tree decl = current_function_decl;
2509 /* Mark nested functions as needing static-chain initially.
2510 lower_nested_functions will recompute it but the
2511 DECL_STATIC_CHAIN flag is also used before that happens,
2512 by initializer_constant_valid_p. See gcc.dg/nested-fn-2.c. */
2513 DECL_STATIC_CHAIN (decl) = 1;
2514 add_stmt (fnbody);
2515 finish_function ();
2516 c_pop_function_context ();
2517 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
2518 }
2519 else
2520 {
2521 if (fnbody)
2522 add_stmt (fnbody);
2523 finish_function ();
2524 }
2525 /* Get rid of the empty stmt list for GIMPLE/RTL. */
2526 if (specs->declspec_il != cdil_none)
2527 DECL_SAVED_TREE (fndecl) = NULL_TREE;
2528
2529 break;
2530 }
2531 }
2532
2533 /* Parse an asm-definition (asm() outside a function body). This is a
2534 GNU extension.
2535
2536 asm-definition:
2537 simple-asm-expr ;
2538 */
2539
2540 static void
2541 c_parser_asm_definition (c_parser *parser)
2542 {
2543 tree asm_str = c_parser_simple_asm_expr (parser);
2544 if (asm_str)
2545 symtab->finalize_toplevel_asm (asm_str);
2546 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
2547 }
2548
2549 /* Parse a static assertion (C11 6.7.10).
2550
2551 static_assert-declaration:
2552 static_assert-declaration-no-semi ;
2553 */
2554
2555 static void
2556 c_parser_static_assert_declaration (c_parser *parser)
2557 {
2558 c_parser_static_assert_declaration_no_semi (parser);
2559 if (parser->error
2560 || !c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
2561 c_parser_skip_to_end_of_block_or_statement (parser);
2562 }
2563
2564 /* Parse a static assertion (C11 6.7.10), without the trailing
2565 semicolon.
2566
2567 static_assert-declaration-no-semi:
2568 _Static_assert ( constant-expression , string-literal )
2569
2570 C2X:
2571 static_assert-declaration-no-semi:
2572 _Static_assert ( constant-expression )
2573 */
2574
2575 static void
2576 c_parser_static_assert_declaration_no_semi (c_parser *parser)
2577 {
2578 location_t assert_loc, value_loc;
2579 tree value;
2580 tree string = NULL_TREE;
2581
2582 gcc_assert (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT));
2583 assert_loc = c_parser_peek_token (parser)->location;
2584 if (flag_isoc99)
2585 pedwarn_c99 (assert_loc, OPT_Wpedantic,
2586 "ISO C99 does not support %<_Static_assert%>");
2587 else
2588 pedwarn_c99 (assert_loc, OPT_Wpedantic,
2589 "ISO C90 does not support %<_Static_assert%>");
2590 c_parser_consume_token (parser);
2591 matching_parens parens;
2592 if (!parens.require_open (parser))
2593 return;
2594 location_t value_tok_loc = c_parser_peek_token (parser)->location;
2595 value = c_parser_expr_no_commas (parser, NULL).value;
2596 value_loc = EXPR_LOC_OR_LOC (value, value_tok_loc);
2597 if (c_parser_next_token_is (parser, CPP_COMMA))
2598 {
2599 c_parser_consume_token (parser);
2600 switch (c_parser_peek_token (parser)->type)
2601 {
2602 case CPP_STRING:
2603 case CPP_STRING16:
2604 case CPP_STRING32:
2605 case CPP_WSTRING:
2606 case CPP_UTF8STRING:
2607 string = c_parser_string_literal (parser, false, true).value;
2608 break;
2609 default:
2610 c_parser_error (parser, "expected string literal");
2611 return;
2612 }
2613 }
2614 else if (flag_isoc11)
2615 /* If pedantic for pre-C11, the use of _Static_assert itself will
2616 have been diagnosed, so do not also diagnose the use of this
2617 new C2X feature of _Static_assert. */
2618 pedwarn_c11 (assert_loc, OPT_Wpedantic,
2619 "ISO C11 does not support omitting the string in "
2620 "%<_Static_assert%>");
2621 parens.require_close (parser);
2622
2623 if (!INTEGRAL_TYPE_P (TREE_TYPE (value)))
2624 {
2625 error_at (value_loc, "expression in static assertion is not an integer");
2626 return;
2627 }
2628 if (TREE_CODE (value) != INTEGER_CST)
2629 {
2630 value = c_fully_fold (value, false, NULL);
2631 /* Strip no-op conversions. */
2632 STRIP_TYPE_NOPS (value);
2633 if (TREE_CODE (value) == INTEGER_CST)
2634 pedwarn (value_loc, OPT_Wpedantic, "expression in static assertion "
2635 "is not an integer constant expression");
2636 }
2637 if (TREE_CODE (value) != INTEGER_CST)
2638 {
2639 error_at (value_loc, "expression in static assertion is not constant");
2640 return;
2641 }
2642 constant_expression_warning (value);
2643 if (integer_zerop (value))
2644 {
2645 if (string)
2646 error_at (assert_loc, "static assertion failed: %E", string);
2647 else
2648 error_at (assert_loc, "static assertion failed");
2649 }
2650 }
2651
2652 /* Parse some declaration specifiers (possibly none) (C90 6.5, C99
2653 6.7, C11 6.7), adding them to SPECS (which may already include some).
2654 Storage class specifiers are accepted iff SCSPEC_OK; type
2655 specifiers are accepted iff TYPESPEC_OK; alignment specifiers are
2656 accepted iff ALIGNSPEC_OK; gnu-attributes are accepted at the start
2657 iff START_ATTR_OK; __auto_type is accepted iff AUTO_TYPE_OK. In
2658 addition to the syntax shown, standard attributes are accepted at
2659 the start iff START_STD_ATTR_OK and at the end iff END_STD_ATTR_OK;
2660 unlike gnu-attributes, they are not accepted in the middle of the
2661 list. (This combines various different syntax productions in the C
2662 standard, and in some cases gnu-attributes and standard attributes
2663 at the start may already have been parsed before this function is
2664 called.)
2665
2666 declaration-specifiers:
2667 storage-class-specifier declaration-specifiers[opt]
2668 type-specifier declaration-specifiers[opt]
2669 type-qualifier declaration-specifiers[opt]
2670 function-specifier declaration-specifiers[opt]
2671 alignment-specifier declaration-specifiers[opt]
2672
2673 Function specifiers (inline) are from C99, and are currently
2674 handled as storage class specifiers, as is __thread. Alignment
2675 specifiers are from C11.
2676
2677 C90 6.5.1, C99 6.7.1, C11 6.7.1:
2678 storage-class-specifier:
2679 typedef
2680 extern
2681 static
2682 auto
2683 register
2684 _Thread_local
2685
2686 (_Thread_local is new in C11.)
2687
2688 C99 6.7.4, C11 6.7.4:
2689 function-specifier:
2690 inline
2691 _Noreturn
2692
2693 (_Noreturn is new in C11.)
2694
2695 C90 6.5.2, C99 6.7.2, C11 6.7.2:
2696 type-specifier:
2697 void
2698 char
2699 short
2700 int
2701 long
2702 float
2703 double
2704 signed
2705 unsigned
2706 _Bool
2707 _Complex
2708 [_Imaginary removed in C99 TC2]
2709 struct-or-union-specifier
2710 enum-specifier
2711 typedef-name
2712 atomic-type-specifier
2713
2714 (_Bool and _Complex are new in C99.)
2715 (atomic-type-specifier is new in C11.)
2716
2717 C90 6.5.3, C99 6.7.3, C11 6.7.3:
2718
2719 type-qualifier:
2720 const
2721 restrict
2722 volatile
2723 address-space-qualifier
2724 _Atomic
2725
2726 (restrict is new in C99.)
2727 (_Atomic is new in C11.)
2728
2729 GNU extensions:
2730
2731 declaration-specifiers:
2732 gnu-attributes declaration-specifiers[opt]
2733
2734 type-qualifier:
2735 address-space
2736
2737 address-space:
2738 identifier recognized by the target
2739
2740 storage-class-specifier:
2741 __thread
2742
2743 type-specifier:
2744 typeof-specifier
2745 __auto_type
2746 __intN
2747 _Decimal32
2748 _Decimal64
2749 _Decimal128
2750 _Fract
2751 _Accum
2752 _Sat
2753
2754 (_Fract, _Accum, and _Sat are new from ISO/IEC DTR 18037:
2755 http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1169.pdf)
2756
2757 atomic-type-specifier
2758 _Atomic ( type-name )
2759
2760 Objective-C:
2761
2762 type-specifier:
2763 class-name objc-protocol-refs[opt]
2764 typedef-name objc-protocol-refs
2765 objc-protocol-refs
2766 */
2767
2768 void
2769 c_parser_declspecs (c_parser *parser, struct c_declspecs *specs,
2770 bool scspec_ok, bool typespec_ok, bool start_attr_ok,
2771 bool alignspec_ok, bool auto_type_ok,
2772 bool start_std_attr_ok, bool end_std_attr_ok,
2773 enum c_lookahead_kind la)
2774 {
2775 bool attrs_ok = start_attr_ok;
2776 bool seen_type = specs->typespec_kind != ctsk_none;
2777
2778 if (!typespec_ok)
2779 gcc_assert (la == cla_prefer_id);
2780
2781 if (start_std_attr_ok
2782 && c_parser_nth_token_starts_std_attributes (parser, 1))
2783 {
2784 gcc_assert (!specs->non_std_attrs_seen_p);
2785 location_t loc = c_parser_peek_token (parser)->location;
2786 tree attrs = c_parser_std_attribute_specifier_sequence (parser);
2787 declspecs_add_attrs (loc, specs, attrs);
2788 specs->non_std_attrs_seen_p = false;
2789 }
2790
2791 while (c_parser_next_token_is (parser, CPP_NAME)
2792 || c_parser_next_token_is (parser, CPP_KEYWORD)
2793 || (c_dialect_objc () && c_parser_next_token_is (parser, CPP_LESS)))
2794 {
2795 struct c_typespec t;
2796 tree attrs;
2797 tree align;
2798 location_t loc = c_parser_peek_token (parser)->location;
2799
2800 /* If we cannot accept a type, exit if the next token must start
2801 one. Also, if we already have seen a tagged definition,
2802 a typename would be an error anyway and likely the user
2803 has simply forgotten a semicolon, so we exit. */
2804 if ((!typespec_ok || specs->typespec_kind == ctsk_tagdef)
2805 && c_parser_next_tokens_start_typename (parser, la)
2806 && !c_parser_next_token_is_qualifier (parser)
2807 && !c_parser_next_token_is_keyword (parser, RID_ALIGNAS))
2808 break;
2809
2810 if (c_parser_next_token_is (parser, CPP_NAME))
2811 {
2812 c_token *name_token = c_parser_peek_token (parser);
2813 tree value = name_token->value;
2814 c_id_kind kind = name_token->id_kind;
2815
2816 if (kind == C_ID_ADDRSPACE)
2817 {
2818 addr_space_t as
2819 = name_token->keyword - RID_FIRST_ADDR_SPACE;
2820 declspecs_add_addrspace (name_token->location, specs, as);
2821 c_parser_consume_token (parser);
2822 attrs_ok = true;
2823 continue;
2824 }
2825
2826 gcc_assert (!c_parser_next_token_is_qualifier (parser));
2827
2828 /* If we cannot accept a type, and the next token must start one,
2829 exit. Do the same if we already have seen a tagged definition,
2830 since it would be an error anyway and likely the user has simply
2831 forgotten a semicolon. */
2832 if (seen_type || !c_parser_next_tokens_start_typename (parser, la))
2833 break;
2834
2835 /* Now at an unknown typename (C_ID_ID), a C_ID_TYPENAME or
2836 a C_ID_CLASSNAME. */
2837 c_parser_consume_token (parser);
2838 seen_type = true;
2839 attrs_ok = true;
2840 if (kind == C_ID_ID)
2841 {
2842 error_at (loc, "unknown type name %qE", value);
2843 t.kind = ctsk_typedef;
2844 t.spec = error_mark_node;
2845 }
2846 else if (kind == C_ID_TYPENAME
2847 && (!c_dialect_objc ()
2848 || c_parser_next_token_is_not (parser, CPP_LESS)))
2849 {
2850 t.kind = ctsk_typedef;
2851 /* For a typedef name, record the meaning, not the name.
2852 In case of 'foo foo, bar;'. */
2853 t.spec = lookup_name (value);
2854 }
2855 else
2856 {
2857 tree proto = NULL_TREE;
2858 gcc_assert (c_dialect_objc ());
2859 t.kind = ctsk_objc;
2860 if (c_parser_next_token_is (parser, CPP_LESS))
2861 proto = c_parser_objc_protocol_refs (parser);
2862 t.spec = objc_get_protocol_qualified_type (value, proto);
2863 }
2864 t.expr = NULL_TREE;
2865 t.expr_const_operands = true;
2866 declspecs_add_type (name_token->location, specs, t);
2867 continue;
2868 }
2869 if (c_parser_next_token_is (parser, CPP_LESS))
2870 {
2871 /* Make "<SomeProtocol>" equivalent to "id <SomeProtocol>" -
2872 nisse@lysator.liu.se. */
2873 tree proto;
2874 gcc_assert (c_dialect_objc ());
2875 if (!typespec_ok || seen_type)
2876 break;
2877 proto = c_parser_objc_protocol_refs (parser);
2878 t.kind = ctsk_objc;
2879 t.spec = objc_get_protocol_qualified_type (NULL_TREE, proto);
2880 t.expr = NULL_TREE;
2881 t.expr_const_operands = true;
2882 declspecs_add_type (loc, specs, t);
2883 continue;
2884 }
2885 gcc_assert (c_parser_next_token_is (parser, CPP_KEYWORD));
2886 switch (c_parser_peek_token (parser)->keyword)
2887 {
2888 case RID_STATIC:
2889 case RID_EXTERN:
2890 case RID_REGISTER:
2891 case RID_TYPEDEF:
2892 case RID_INLINE:
2893 case RID_NORETURN:
2894 case RID_AUTO:
2895 case RID_THREAD:
2896 if (!scspec_ok)
2897 goto out;
2898 attrs_ok = true;
2899 /* TODO: Distinguish between function specifiers (inline, noreturn)
2900 and storage class specifiers, either here or in
2901 declspecs_add_scspec. */
2902 declspecs_add_scspec (loc, specs,
2903 c_parser_peek_token (parser)->value);
2904 c_parser_consume_token (parser);
2905 break;
2906 case RID_AUTO_TYPE:
2907 if (!auto_type_ok)
2908 goto out;
2909 /* Fall through. */
2910 case RID_UNSIGNED:
2911 case RID_LONG:
2912 case RID_SHORT:
2913 case RID_SIGNED:
2914 case RID_COMPLEX:
2915 case RID_INT:
2916 case RID_CHAR:
2917 case RID_FLOAT:
2918 case RID_DOUBLE:
2919 case RID_VOID:
2920 case RID_DFLOAT32:
2921 case RID_DFLOAT64:
2922 case RID_DFLOAT128:
2923 CASE_RID_FLOATN_NX:
2924 case RID_BOOL:
2925 case RID_FRACT:
2926 case RID_ACCUM:
2927 case RID_SAT:
2928 case RID_INT_N_0:
2929 case RID_INT_N_1:
2930 case RID_INT_N_2:
2931 case RID_INT_N_3:
2932 if (!typespec_ok)
2933 goto out;
2934 attrs_ok = true;
2935 seen_type = true;
2936 if (c_dialect_objc ())
2937 parser->objc_need_raw_identifier = true;
2938 t.kind = ctsk_resword;
2939 t.spec = c_parser_peek_token (parser)->value;
2940 t.expr = NULL_TREE;
2941 t.expr_const_operands = true;
2942 declspecs_add_type (loc, specs, t);
2943 c_parser_consume_token (parser);
2944 break;
2945 case RID_ENUM:
2946 if (!typespec_ok)
2947 goto out;
2948 attrs_ok = true;
2949 seen_type = true;
2950 t = c_parser_enum_specifier (parser);
2951 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, t.spec);
2952 declspecs_add_type (loc, specs, t);
2953 break;
2954 case RID_STRUCT:
2955 case RID_UNION:
2956 if (!typespec_ok)
2957 goto out;
2958 attrs_ok = true;
2959 seen_type = true;
2960 t = c_parser_struct_or_union_specifier (parser);
2961 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, t.spec);
2962 declspecs_add_type (loc, specs, t);
2963 break;
2964 case RID_TYPEOF:
2965 /* ??? The old parser rejected typeof after other type
2966 specifiers, but is a syntax error the best way of
2967 handling this? */
2968 if (!typespec_ok || seen_type)
2969 goto out;
2970 attrs_ok = true;
2971 seen_type = true;
2972 t = c_parser_typeof_specifier (parser);
2973 declspecs_add_type (loc, specs, t);
2974 break;
2975 case RID_ATOMIC:
2976 /* C parser handling of Objective-C constructs needs
2977 checking for correct lvalue-to-rvalue conversions, and
2978 the code in build_modify_expr handling various
2979 Objective-C cases, and that in build_unary_op handling
2980 Objective-C cases for increment / decrement, also needs
2981 updating; uses of TYPE_MAIN_VARIANT in objc_compare_types
2982 and objc_types_are_equivalent may also need updates. */
2983 if (c_dialect_objc ())
2984 sorry ("%<_Atomic%> in Objective-C");
2985 if (flag_isoc99)
2986 pedwarn_c99 (loc, OPT_Wpedantic,
2987 "ISO C99 does not support the %<_Atomic%> qualifier");
2988 else
2989 pedwarn_c99 (loc, OPT_Wpedantic,
2990 "ISO C90 does not support the %<_Atomic%> qualifier");
2991 attrs_ok = true;
2992 tree value;
2993 value = c_parser_peek_token (parser)->value;
2994 c_parser_consume_token (parser);
2995 if (typespec_ok && c_parser_next_token_is (parser, CPP_OPEN_PAREN))
2996 {
2997 /* _Atomic ( type-name ). */
2998 seen_type = true;
2999 c_parser_consume_token (parser);
3000 struct c_type_name *type = c_parser_type_name (parser);
3001 t.kind = ctsk_typeof;
3002 t.spec = error_mark_node;
3003 t.expr = NULL_TREE;
3004 t.expr_const_operands = true;
3005 if (type != NULL)
3006 t.spec = groktypename (type, &t.expr,
3007 &t.expr_const_operands);
3008 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3009 "expected %<)%>");
3010 if (t.spec != error_mark_node)
3011 {
3012 if (TREE_CODE (t.spec) == ARRAY_TYPE)
3013 error_at (loc, "%<_Atomic%>-qualified array type");
3014 else if (TREE_CODE (t.spec) == FUNCTION_TYPE)
3015 error_at (loc, "%<_Atomic%>-qualified function type");
3016 else if (TYPE_QUALS (t.spec) != TYPE_UNQUALIFIED)
3017 error_at (loc, "%<_Atomic%> applied to a qualified type");
3018 else
3019 t.spec = c_build_qualified_type (t.spec, TYPE_QUAL_ATOMIC);
3020 }
3021 declspecs_add_type (loc, specs, t);
3022 }
3023 else
3024 declspecs_add_qual (loc, specs, value);
3025 break;
3026 case RID_CONST:
3027 case RID_VOLATILE:
3028 case RID_RESTRICT:
3029 attrs_ok = true;
3030 declspecs_add_qual (loc, specs, c_parser_peek_token (parser)->value);
3031 c_parser_consume_token (parser);
3032 break;
3033 case RID_ATTRIBUTE:
3034 if (!attrs_ok)
3035 goto out;
3036 attrs = c_parser_gnu_attributes (parser);
3037 declspecs_add_attrs (loc, specs, attrs);
3038 break;
3039 case RID_ALIGNAS:
3040 if (!alignspec_ok)
3041 goto out;
3042 align = c_parser_alignas_specifier (parser);
3043 declspecs_add_alignas (loc, specs, align);
3044 break;
3045 case RID_GIMPLE:
3046 if (! flag_gimple)
3047 error_at (loc, "%<__GIMPLE%> only valid with %<-fgimple%>");
3048 c_parser_consume_token (parser);
3049 specs->declspec_il = cdil_gimple;
3050 specs->locations[cdw_gimple] = loc;
3051 c_parser_gimple_or_rtl_pass_list (parser, specs);
3052 break;
3053 case RID_RTL:
3054 c_parser_consume_token (parser);
3055 specs->declspec_il = cdil_rtl;
3056 specs->locations[cdw_rtl] = loc;
3057 c_parser_gimple_or_rtl_pass_list (parser, specs);
3058 break;
3059 default:
3060 goto out;
3061 }
3062 }
3063 out:
3064 if (end_std_attr_ok
3065 && c_parser_nth_token_starts_std_attributes (parser, 1))
3066 specs->postfix_attrs = c_parser_std_attribute_specifier_sequence (parser);
3067 }
3068
3069 /* Parse an enum specifier (C90 6.5.2.2, C99 6.7.2.2, C11 6.7.2.2).
3070
3071 enum-specifier:
3072 enum gnu-attributes[opt] identifier[opt] { enumerator-list }
3073 gnu-attributes[opt]
3074 enum gnu-attributes[opt] identifier[opt] { enumerator-list , }
3075 gnu-attributes[opt]
3076 enum gnu-attributes[opt] identifier
3077
3078 The form with trailing comma is new in C99. The forms with
3079 gnu-attributes are GNU extensions. In GNU C, we accept any expression
3080 without commas in the syntax (assignment expressions, not just
3081 conditional expressions); assignment expressions will be diagnosed
3082 as non-constant.
3083
3084 enumerator-list:
3085 enumerator
3086 enumerator-list , enumerator
3087
3088 enumerator:
3089 enumeration-constant attribute-specifier-sequence[opt]
3090 enumeration-constant attribute-specifier-sequence[opt]
3091 = constant-expression
3092
3093 GNU Extensions:
3094
3095 enumerator:
3096 enumeration-constant attribute-specifier-sequence[opt] gnu-attributes[opt]
3097 enumeration-constant attribute-specifier-sequence[opt] gnu-attributes[opt]
3098 = constant-expression
3099
3100 */
3101
3102 static struct c_typespec
3103 c_parser_enum_specifier (c_parser *parser)
3104 {
3105 struct c_typespec ret;
3106 bool have_std_attrs;
3107 tree std_attrs = NULL_TREE;
3108 tree attrs;
3109 tree ident = NULL_TREE;
3110 location_t enum_loc;
3111 location_t ident_loc = UNKNOWN_LOCATION; /* Quiet warning. */
3112 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ENUM));
3113 c_parser_consume_token (parser);
3114 have_std_attrs = c_parser_nth_token_starts_std_attributes (parser, 1);
3115 if (have_std_attrs)
3116 std_attrs = c_parser_std_attribute_specifier_sequence (parser);
3117 attrs = c_parser_gnu_attributes (parser);
3118 enum_loc = c_parser_peek_token (parser)->location;
3119 /* Set the location in case we create a decl now. */
3120 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
3121 if (c_parser_next_token_is (parser, CPP_NAME))
3122 {
3123 ident = c_parser_peek_token (parser)->value;
3124 ident_loc = c_parser_peek_token (parser)->location;
3125 enum_loc = ident_loc;
3126 c_parser_consume_token (parser);
3127 }
3128 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
3129 {
3130 /* Parse an enum definition. */
3131 struct c_enum_contents the_enum;
3132 tree type;
3133 tree postfix_attrs;
3134 /* We chain the enumerators in reverse order, then put them in
3135 forward order at the end. */
3136 tree values;
3137 timevar_push (TV_PARSE_ENUM);
3138 type = start_enum (enum_loc, &the_enum, ident);
3139 values = NULL_TREE;
3140 c_parser_consume_token (parser);
3141 while (true)
3142 {
3143 tree enum_id;
3144 tree enum_value;
3145 tree enum_decl;
3146 bool seen_comma;
3147 c_token *token;
3148 location_t comma_loc = UNKNOWN_LOCATION; /* Quiet warning. */
3149 location_t decl_loc, value_loc;
3150 if (c_parser_next_token_is_not (parser, CPP_NAME))
3151 {
3152 /* Give a nicer error for "enum {}". */
3153 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE)
3154 && !parser->error)
3155 {
3156 error_at (c_parser_peek_token (parser)->location,
3157 "empty enum is invalid");
3158 parser->error = true;
3159 }
3160 else
3161 c_parser_error (parser, "expected identifier");
3162 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
3163 values = error_mark_node;
3164 break;
3165 }
3166 token = c_parser_peek_token (parser);
3167 enum_id = token->value;
3168 /* Set the location in case we create a decl now. */
3169 c_parser_set_source_position_from_token (token);
3170 decl_loc = value_loc = token->location;
3171 c_parser_consume_token (parser);
3172 /* Parse any specified attributes. */
3173 tree std_attrs = NULL_TREE;
3174 if (c_parser_nth_token_starts_std_attributes (parser, 1))
3175 std_attrs = c_parser_std_attribute_specifier_sequence (parser);
3176 tree enum_attrs = chainon (std_attrs,
3177 c_parser_gnu_attributes (parser));
3178 if (c_parser_next_token_is (parser, CPP_EQ))
3179 {
3180 c_parser_consume_token (parser);
3181 value_loc = c_parser_peek_token (parser)->location;
3182 enum_value = c_parser_expr_no_commas (parser, NULL).value;
3183 }
3184 else
3185 enum_value = NULL_TREE;
3186 enum_decl = build_enumerator (decl_loc, value_loc,
3187 &the_enum, enum_id, enum_value);
3188 if (enum_attrs)
3189 decl_attributes (&TREE_PURPOSE (enum_decl), enum_attrs, 0);
3190 TREE_CHAIN (enum_decl) = values;
3191 values = enum_decl;
3192 seen_comma = false;
3193 if (c_parser_next_token_is (parser, CPP_COMMA))
3194 {
3195 comma_loc = c_parser_peek_token (parser)->location;
3196 seen_comma = true;
3197 c_parser_consume_token (parser);
3198 }
3199 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3200 {
3201 if (seen_comma)
3202 pedwarn_c90 (comma_loc, OPT_Wpedantic,
3203 "comma at end of enumerator list");
3204 c_parser_consume_token (parser);
3205 break;
3206 }
3207 if (!seen_comma)
3208 {
3209 c_parser_error (parser, "expected %<,%> or %<}%>");
3210 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
3211 values = error_mark_node;
3212 break;
3213 }
3214 }
3215 postfix_attrs = c_parser_gnu_attributes (parser);
3216 ret.spec = finish_enum (type, nreverse (values),
3217 chainon (std_attrs,
3218 chainon (attrs, postfix_attrs)));
3219 ret.kind = ctsk_tagdef;
3220 ret.expr = NULL_TREE;
3221 ret.expr_const_operands = true;
3222 timevar_pop (TV_PARSE_ENUM);
3223 return ret;
3224 }
3225 else if (!ident)
3226 {
3227 c_parser_error (parser, "expected %<{%>");
3228 ret.spec = error_mark_node;
3229 ret.kind = ctsk_tagref;
3230 ret.expr = NULL_TREE;
3231 ret.expr_const_operands = true;
3232 return ret;
3233 }
3234 /* Attributes may only appear when the members are defined or in
3235 certain forward declarations (treat enum forward declarations in
3236 GNU C analogously to struct and union forward declarations in
3237 standard C). */
3238 if (have_std_attrs && c_parser_next_token_is_not (parser, CPP_SEMICOLON))
3239 c_parser_error (parser, "expected %<;%>");
3240 ret = parser_xref_tag (ident_loc, ENUMERAL_TYPE, ident, have_std_attrs,
3241 std_attrs);
3242 /* In ISO C, enumerated types can be referred to only if already
3243 defined. */
3244 if (pedantic && !COMPLETE_TYPE_P (ret.spec))
3245 {
3246 gcc_assert (ident);
3247 pedwarn (enum_loc, OPT_Wpedantic,
3248 "ISO C forbids forward references to %<enum%> types");
3249 }
3250 return ret;
3251 }
3252
3253 /* Parse a struct or union specifier (C90 6.5.2.1, C99 6.7.2.1, C11 6.7.2.1).
3254
3255 struct-or-union-specifier:
3256 struct-or-union attribute-specifier-sequence[opt] gnu-attributes[opt]
3257 identifier[opt] { struct-contents } gnu-attributes[opt]
3258 struct-or-union attribute-specifier-sequence[opt] gnu-attributes[opt]
3259 identifier
3260
3261 struct-contents:
3262 struct-declaration-list
3263
3264 struct-declaration-list:
3265 struct-declaration ;
3266 struct-declaration-list struct-declaration ;
3267
3268 GNU extensions:
3269
3270 struct-contents:
3271 empty
3272 struct-declaration
3273 struct-declaration-list struct-declaration
3274
3275 struct-declaration-list:
3276 struct-declaration-list ;
3277 ;
3278
3279 (Note that in the syntax here, unlike that in ISO C, the semicolons
3280 are included here rather than in struct-declaration, in order to
3281 describe the syntax with extra semicolons and missing semicolon at
3282 end.)
3283
3284 Objective-C:
3285
3286 struct-declaration-list:
3287 @defs ( class-name )
3288
3289 (Note this does not include a trailing semicolon, but can be
3290 followed by further declarations, and gets a pedwarn-if-pedantic
3291 when followed by a semicolon.) */
3292
3293 static struct c_typespec
3294 c_parser_struct_or_union_specifier (c_parser *parser)
3295 {
3296 struct c_typespec ret;
3297 bool have_std_attrs;
3298 tree std_attrs = NULL_TREE;
3299 tree attrs;
3300 tree ident = NULL_TREE;
3301 location_t struct_loc;
3302 location_t ident_loc = UNKNOWN_LOCATION;
3303 enum tree_code code;
3304 switch (c_parser_peek_token (parser)->keyword)
3305 {
3306 case RID_STRUCT:
3307 code = RECORD_TYPE;
3308 break;
3309 case RID_UNION:
3310 code = UNION_TYPE;
3311 break;
3312 default:
3313 gcc_unreachable ();
3314 }
3315 struct_loc = c_parser_peek_token (parser)->location;
3316 c_parser_consume_token (parser);
3317 have_std_attrs = c_parser_nth_token_starts_std_attributes (parser, 1);
3318 if (have_std_attrs)
3319 std_attrs = c_parser_std_attribute_specifier_sequence (parser);
3320 attrs = c_parser_gnu_attributes (parser);
3321
3322 /* Set the location in case we create a decl now. */
3323 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
3324
3325 if (c_parser_next_token_is (parser, CPP_NAME))
3326 {
3327 ident = c_parser_peek_token (parser)->value;
3328 ident_loc = c_parser_peek_token (parser)->location;
3329 struct_loc = ident_loc;
3330 c_parser_consume_token (parser);
3331 }
3332 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
3333 {
3334 /* Parse a struct or union definition. Start the scope of the
3335 tag before parsing components. */
3336 class c_struct_parse_info *struct_info;
3337 tree type = start_struct (struct_loc, code, ident, &struct_info);
3338 tree postfix_attrs;
3339 /* We chain the components in reverse order, then put them in
3340 forward order at the end. Each struct-declaration may
3341 declare multiple components (comma-separated), so we must use
3342 chainon to join them, although when parsing each
3343 struct-declaration we can use TREE_CHAIN directly.
3344
3345 The theory behind all this is that there will be more
3346 semicolon separated fields than comma separated fields, and
3347 so we'll be minimizing the number of node traversals required
3348 by chainon. */
3349 tree contents;
3350 timevar_push (TV_PARSE_STRUCT);
3351 contents = NULL_TREE;
3352 c_parser_consume_token (parser);
3353 /* Handle the Objective-C @defs construct,
3354 e.g. foo(sizeof(struct{ @defs(ClassName) }));. */
3355 if (c_parser_next_token_is_keyword (parser, RID_AT_DEFS))
3356 {
3357 tree name;
3358 gcc_assert (c_dialect_objc ());
3359 c_parser_consume_token (parser);
3360 matching_parens parens;
3361 if (!parens.require_open (parser))
3362 goto end_at_defs;
3363 if (c_parser_next_token_is (parser, CPP_NAME)
3364 && c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME)
3365 {
3366 name = c_parser_peek_token (parser)->value;
3367 c_parser_consume_token (parser);
3368 }
3369 else
3370 {
3371 c_parser_error (parser, "expected class name");
3372 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3373 goto end_at_defs;
3374 }
3375 parens.skip_until_found_close (parser);
3376 contents = nreverse (objc_get_class_ivars (name));
3377 }
3378 end_at_defs:
3379 /* Parse the struct-declarations and semicolons. Problems with
3380 semicolons are diagnosed here; empty structures are diagnosed
3381 elsewhere. */
3382 while (true)
3383 {
3384 tree decls;
3385 /* Parse any stray semicolon. */
3386 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3387 {
3388 location_t semicolon_loc
3389 = c_parser_peek_token (parser)->location;
3390 gcc_rich_location richloc (semicolon_loc);
3391 richloc.add_fixit_remove ();
3392 pedwarn (&richloc, OPT_Wpedantic,
3393 "extra semicolon in struct or union specified");
3394 c_parser_consume_token (parser);
3395 continue;
3396 }
3397 /* Stop if at the end of the struct or union contents. */
3398 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3399 {
3400 c_parser_consume_token (parser);
3401 break;
3402 }
3403 /* Accept #pragmas at struct scope. */
3404 if (c_parser_next_token_is (parser, CPP_PRAGMA))
3405 {
3406 c_parser_pragma (parser, pragma_struct, NULL);
3407 continue;
3408 }
3409 /* Parse some comma-separated declarations, but not the
3410 trailing semicolon if any. */
3411 decls = c_parser_struct_declaration (parser);
3412 contents = chainon (decls, contents);
3413 /* If no semicolon follows, either we have a parse error or
3414 are at the end of the struct or union and should
3415 pedwarn. */
3416 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3417 c_parser_consume_token (parser);
3418 else
3419 {
3420 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3421 pedwarn (c_parser_peek_token (parser)->location, 0,
3422 "no semicolon at end of struct or union");
3423 else if (parser->error
3424 || !c_parser_next_token_starts_declspecs (parser))
3425 {
3426 c_parser_error (parser, "expected %<;%>");
3427 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
3428 break;
3429 }
3430
3431 /* If we come here, we have already emitted an error
3432 for an expected `;', identifier or `(', and we also
3433 recovered already. Go on with the next field. */
3434 }
3435 }
3436 postfix_attrs = c_parser_gnu_attributes (parser);
3437 ret.spec = finish_struct (struct_loc, type, nreverse (contents),
3438 chainon (std_attrs,
3439 chainon (attrs, postfix_attrs)),
3440 struct_info);
3441 ret.kind = ctsk_tagdef;
3442 ret.expr = NULL_TREE;
3443 ret.expr_const_operands = true;
3444 timevar_pop (TV_PARSE_STRUCT);
3445 return ret;
3446 }
3447 else if (!ident)
3448 {
3449 c_parser_error (parser, "expected %<{%>");
3450 ret.spec = error_mark_node;
3451 ret.kind = ctsk_tagref;
3452 ret.expr = NULL_TREE;
3453 ret.expr_const_operands = true;
3454 return ret;
3455 }
3456 /* Attributes may only appear when the members are defined or in
3457 certain forward declarations. */
3458 if (have_std_attrs && c_parser_next_token_is_not (parser, CPP_SEMICOLON))
3459 c_parser_error (parser, "expected %<;%>");
3460 /* ??? Existing practice is that GNU attributes are ignored after
3461 the struct or union keyword when not defining the members. */
3462 ret = parser_xref_tag (ident_loc, code, ident, have_std_attrs, std_attrs);
3463 return ret;
3464 }
3465
3466 /* Parse a struct-declaration (C90 6.5.2.1, C99 6.7.2.1, C11 6.7.2.1),
3467 *without* the trailing semicolon.
3468
3469 struct-declaration:
3470 attribute-specifier-sequence[opt] specifier-qualifier-list
3471 attribute-specifier-sequence[opt] struct-declarator-list
3472 static_assert-declaration-no-semi
3473
3474 specifier-qualifier-list:
3475 type-specifier specifier-qualifier-list[opt]
3476 type-qualifier specifier-qualifier-list[opt]
3477 alignment-specifier specifier-qualifier-list[opt]
3478 gnu-attributes specifier-qualifier-list[opt]
3479
3480 struct-declarator-list:
3481 struct-declarator
3482 struct-declarator-list , gnu-attributes[opt] struct-declarator
3483
3484 struct-declarator:
3485 declarator gnu-attributes[opt]
3486 declarator[opt] : constant-expression gnu-attributes[opt]
3487
3488 GNU extensions:
3489
3490 struct-declaration:
3491 __extension__ struct-declaration
3492 specifier-qualifier-list
3493
3494 Unlike the ISO C syntax, semicolons are handled elsewhere. The use
3495 of gnu-attributes where shown is a GNU extension. In GNU C, we accept
3496 any expression without commas in the syntax (assignment
3497 expressions, not just conditional expressions); assignment
3498 expressions will be diagnosed as non-constant. */
3499
3500 static tree
3501 c_parser_struct_declaration (c_parser *parser)
3502 {
3503 struct c_declspecs *specs;
3504 tree prefix_attrs;
3505 tree all_prefix_attrs;
3506 tree decls;
3507 location_t decl_loc;
3508 if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
3509 {
3510 int ext;
3511 tree decl;
3512 ext = disable_extension_diagnostics ();
3513 c_parser_consume_token (parser);
3514 decl = c_parser_struct_declaration (parser);
3515 restore_extension_diagnostics (ext);
3516 return decl;
3517 }
3518 if (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
3519 {
3520 c_parser_static_assert_declaration_no_semi (parser);
3521 return NULL_TREE;
3522 }
3523 specs = build_null_declspecs ();
3524 decl_loc = c_parser_peek_token (parser)->location;
3525 /* Strictly by the standard, we shouldn't allow _Alignas here,
3526 but it appears to have been intended to allow it there, so
3527 we're keeping it as it is until WG14 reaches a conclusion
3528 of N1731.
3529 <http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1731.pdf> */
3530 c_parser_declspecs (parser, specs, false, true, true,
3531 true, false, true, true, cla_nonabstract_decl);
3532 if (parser->error)
3533 return NULL_TREE;
3534 if (!specs->declspecs_seen_p)
3535 {
3536 c_parser_error (parser, "expected specifier-qualifier-list");
3537 return NULL_TREE;
3538 }
3539 finish_declspecs (specs);
3540 if (c_parser_next_token_is (parser, CPP_SEMICOLON)
3541 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3542 {
3543 tree ret;
3544 if (specs->typespec_kind == ctsk_none)
3545 {
3546 pedwarn (decl_loc, OPT_Wpedantic,
3547 "ISO C forbids member declarations with no members");
3548 shadow_tag_warned (specs, pedantic);
3549 ret = NULL_TREE;
3550 }
3551 else
3552 {
3553 /* Support for unnamed structs or unions as members of
3554 structs or unions (which is [a] useful and [b] supports
3555 MS P-SDK). */
3556 tree attrs = NULL;
3557
3558 ret = grokfield (c_parser_peek_token (parser)->location,
3559 build_id_declarator (NULL_TREE), specs,
3560 NULL_TREE, &attrs);
3561 if (ret)
3562 decl_attributes (&ret, attrs, 0);
3563 }
3564 return ret;
3565 }
3566
3567 /* Provide better error recovery. Note that a type name here is valid,
3568 and will be treated as a field name. */
3569 if (specs->typespec_kind == ctsk_tagdef
3570 && TREE_CODE (specs->type) != ENUMERAL_TYPE
3571 && c_parser_next_token_starts_declspecs (parser)
3572 && !c_parser_next_token_is (parser, CPP_NAME))
3573 {
3574 c_parser_error (parser, "expected %<;%>, identifier or %<(%>");
3575 parser->error = false;
3576 return NULL_TREE;
3577 }
3578
3579 pending_xref_error ();
3580 prefix_attrs = specs->attrs;
3581 all_prefix_attrs = prefix_attrs;
3582 specs->attrs = NULL_TREE;
3583 decls = NULL_TREE;
3584 while (true)
3585 {
3586 /* Declaring one or more declarators or un-named bit-fields. */
3587 struct c_declarator *declarator;
3588 bool dummy = false;
3589 if (c_parser_next_token_is (parser, CPP_COLON))
3590 declarator = build_id_declarator (NULL_TREE);
3591 else
3592 declarator = c_parser_declarator (parser,
3593 specs->typespec_kind != ctsk_none,
3594 C_DTR_NORMAL, &dummy);
3595 if (declarator == NULL)
3596 {
3597 c_parser_skip_to_end_of_block_or_statement (parser);
3598 break;
3599 }
3600 if (c_parser_next_token_is (parser, CPP_COLON)
3601 || c_parser_next_token_is (parser, CPP_COMMA)
3602 || c_parser_next_token_is (parser, CPP_SEMICOLON)
3603 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE)
3604 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3605 {
3606 tree postfix_attrs = NULL_TREE;
3607 tree width = NULL_TREE;
3608 tree d;
3609 if (c_parser_next_token_is (parser, CPP_COLON))
3610 {
3611 c_parser_consume_token (parser);
3612 width = c_parser_expr_no_commas (parser, NULL).value;
3613 }
3614 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3615 postfix_attrs = c_parser_gnu_attributes (parser);
3616 d = grokfield (c_parser_peek_token (parser)->location,
3617 declarator, specs, width, &all_prefix_attrs);
3618 decl_attributes (&d, chainon (postfix_attrs,
3619 all_prefix_attrs), 0);
3620 DECL_CHAIN (d) = decls;
3621 decls = d;
3622 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3623 all_prefix_attrs = chainon (c_parser_gnu_attributes (parser),
3624 prefix_attrs);
3625 else
3626 all_prefix_attrs = prefix_attrs;
3627 if (c_parser_next_token_is (parser, CPP_COMMA))
3628 c_parser_consume_token (parser);
3629 else if (c_parser_next_token_is (parser, CPP_SEMICOLON)
3630 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3631 {
3632 /* Semicolon consumed in caller. */
3633 break;
3634 }
3635 else
3636 {
3637 c_parser_error (parser, "expected %<,%>, %<;%> or %<}%>");
3638 break;
3639 }
3640 }
3641 else
3642 {
3643 c_parser_error (parser,
3644 "expected %<:%>, %<,%>, %<;%>, %<}%> or "
3645 "%<__attribute__%>");
3646 break;
3647 }
3648 }
3649 return decls;
3650 }
3651
3652 /* Parse a typeof specifier (a GNU extension).
3653
3654 typeof-specifier:
3655 typeof ( expression )
3656 typeof ( type-name )
3657 */
3658
3659 static struct c_typespec
3660 c_parser_typeof_specifier (c_parser *parser)
3661 {
3662 struct c_typespec ret;
3663 ret.kind = ctsk_typeof;
3664 ret.spec = error_mark_node;
3665 ret.expr = NULL_TREE;
3666 ret.expr_const_operands = true;
3667 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TYPEOF));
3668 c_parser_consume_token (parser);
3669 c_inhibit_evaluation_warnings++;
3670 in_typeof++;
3671 matching_parens parens;
3672 if (!parens.require_open (parser))
3673 {
3674 c_inhibit_evaluation_warnings--;
3675 in_typeof--;
3676 return ret;
3677 }
3678 if (c_parser_next_tokens_start_typename (parser, cla_prefer_id))
3679 {
3680 struct c_type_name *type = c_parser_type_name (parser);
3681 c_inhibit_evaluation_warnings--;
3682 in_typeof--;
3683 if (type != NULL)
3684 {
3685 ret.spec = groktypename (type, &ret.expr, &ret.expr_const_operands);
3686 pop_maybe_used (variably_modified_type_p (ret.spec, NULL_TREE));
3687 }
3688 }
3689 else
3690 {
3691 bool was_vm;
3692 location_t here = c_parser_peek_token (parser)->location;
3693 struct c_expr expr = c_parser_expression (parser);
3694 c_inhibit_evaluation_warnings--;
3695 in_typeof--;
3696 if (TREE_CODE (expr.value) == COMPONENT_REF
3697 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
3698 error_at (here, "%<typeof%> applied to a bit-field");
3699 mark_exp_read (expr.value);
3700 ret.spec = TREE_TYPE (expr.value);
3701 was_vm = variably_modified_type_p (ret.spec, NULL_TREE);
3702 /* This is returned with the type so that when the type is
3703 evaluated, this can be evaluated. */
3704 if (was_vm)
3705 ret.expr = c_fully_fold (expr.value, false, &ret.expr_const_operands);
3706 pop_maybe_used (was_vm);
3707 /* For use in macros such as those in <stdatomic.h>, remove all
3708 qualifiers from atomic types. (const can be an issue for more macros
3709 using typeof than just the <stdatomic.h> ones.) */
3710 if (ret.spec != error_mark_node && TYPE_ATOMIC (ret.spec))
3711 ret.spec = c_build_qualified_type (ret.spec, TYPE_UNQUALIFIED);
3712 }
3713 parens.skip_until_found_close (parser);
3714 return ret;
3715 }
3716
3717 /* Parse an alignment-specifier.
3718
3719 C11 6.7.5:
3720
3721 alignment-specifier:
3722 _Alignas ( type-name )
3723 _Alignas ( constant-expression )
3724 */
3725
3726 static tree
3727 c_parser_alignas_specifier (c_parser * parser)
3728 {
3729 tree ret = error_mark_node;
3730 location_t loc = c_parser_peek_token (parser)->location;
3731 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNAS));
3732 c_parser_consume_token (parser);
3733 if (flag_isoc99)
3734 pedwarn_c99 (loc, OPT_Wpedantic,
3735 "ISO C99 does not support %<_Alignas%>");
3736 else
3737 pedwarn_c99 (loc, OPT_Wpedantic,
3738 "ISO C90 does not support %<_Alignas%>");
3739 matching_parens parens;
3740 if (!parens.require_open (parser))
3741 return ret;
3742 if (c_parser_next_tokens_start_typename (parser, cla_prefer_id))
3743 {
3744 struct c_type_name *type = c_parser_type_name (parser);
3745 if (type != NULL)
3746 ret = c_sizeof_or_alignof_type (loc, groktypename (type, NULL, NULL),
3747 false, true, 1);
3748 }
3749 else
3750 ret = c_parser_expr_no_commas (parser, NULL).value;
3751 parens.skip_until_found_close (parser);
3752 return ret;
3753 }
3754
3755 /* Parse a declarator, possibly an abstract declarator (C90 6.5.4,
3756 6.5.5, C99 6.7.5, 6.7.6, C11 6.7.6, 6.7.7). If TYPE_SEEN_P then
3757 a typedef name may be redeclared; otherwise it may not. KIND
3758 indicates which kind of declarator is wanted. Returns a valid
3759 declarator except in the case of a syntax error in which case NULL is
3760 returned. *SEEN_ID is set to true if an identifier being declared is
3761 seen; this is used to diagnose bad forms of abstract array declarators
3762 and to determine whether an identifier list is syntactically permitted.
3763
3764 declarator:
3765 pointer[opt] direct-declarator
3766
3767 direct-declarator:
3768 identifier
3769 ( gnu-attributes[opt] declarator )
3770 direct-declarator array-declarator
3771 direct-declarator ( parameter-type-list )
3772 direct-declarator ( identifier-list[opt] )
3773
3774 pointer:
3775 * type-qualifier-list[opt]
3776 * type-qualifier-list[opt] pointer
3777
3778 type-qualifier-list:
3779 type-qualifier
3780 gnu-attributes
3781 type-qualifier-list type-qualifier
3782 type-qualifier-list gnu-attributes
3783
3784 array-declarator:
3785 [ type-qualifier-list[opt] assignment-expression[opt] ]
3786 [ static type-qualifier-list[opt] assignment-expression ]
3787 [ type-qualifier-list static assignment-expression ]
3788 [ type-qualifier-list[opt] * ]
3789
3790 parameter-type-list:
3791 parameter-list
3792 parameter-list , ...
3793
3794 parameter-list:
3795 parameter-declaration
3796 parameter-list , parameter-declaration
3797
3798 parameter-declaration:
3799 declaration-specifiers declarator gnu-attributes[opt]
3800 declaration-specifiers abstract-declarator[opt] gnu-attributes[opt]
3801
3802 identifier-list:
3803 identifier
3804 identifier-list , identifier
3805
3806 abstract-declarator:
3807 pointer
3808 pointer[opt] direct-abstract-declarator
3809
3810 direct-abstract-declarator:
3811 ( gnu-attributes[opt] abstract-declarator )
3812 direct-abstract-declarator[opt] array-declarator
3813 direct-abstract-declarator[opt] ( parameter-type-list[opt] )
3814
3815 GNU extensions:
3816
3817 direct-declarator:
3818 direct-declarator ( parameter-forward-declarations
3819 parameter-type-list[opt] )
3820
3821 direct-abstract-declarator:
3822 direct-abstract-declarator[opt] ( parameter-forward-declarations
3823 parameter-type-list[opt] )
3824
3825 parameter-forward-declarations:
3826 parameter-list ;
3827 parameter-forward-declarations parameter-list ;
3828
3829 The uses of gnu-attributes shown above are GNU extensions.
3830
3831 Some forms of array declarator are not included in C99 in the
3832 syntax for abstract declarators; these are disallowed elsewhere.
3833 This may be a defect (DR#289).
3834
3835 This function also accepts an omitted abstract declarator as being
3836 an abstract declarator, although not part of the formal syntax. */
3837
3838 struct c_declarator *
3839 c_parser_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
3840 bool *seen_id)
3841 {
3842 /* Parse any initial pointer part. */
3843 if (c_parser_next_token_is (parser, CPP_MULT))
3844 {
3845 struct c_declspecs *quals_attrs = build_null_declspecs ();
3846 struct c_declarator *inner;
3847 c_parser_consume_token (parser);
3848 c_parser_declspecs (parser, quals_attrs, false, false, true,
3849 false, false, true, false, cla_prefer_id);
3850 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
3851 if (inner == NULL)
3852 return NULL;
3853 else
3854 return make_pointer_declarator (quals_attrs, inner);
3855 }
3856 /* Now we have a direct declarator, direct abstract declarator or
3857 nothing (which counts as a direct abstract declarator here). */
3858 return c_parser_direct_declarator (parser, type_seen_p, kind, seen_id);
3859 }
3860
3861 /* Parse a direct declarator or direct abstract declarator; arguments
3862 as c_parser_declarator. */
3863
3864 static struct c_declarator *
3865 c_parser_direct_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
3866 bool *seen_id)
3867 {
3868 /* The direct declarator must start with an identifier (possibly
3869 omitted) or a parenthesized declarator (possibly abstract). In
3870 an ordinary declarator, initial parentheses must start a
3871 parenthesized declarator. In an abstract declarator or parameter
3872 declarator, they could start a parenthesized declarator or a
3873 parameter list. To tell which, the open parenthesis and any
3874 following gnu-attributes must be read. If a declaration
3875 specifier or standard attributes follow, then it is a parameter
3876 list; if the specifier is a typedef name, there might be an
3877 ambiguity about redeclaring it, which is resolved in the
3878 direction of treating it as a typedef name. If a close
3879 parenthesis follows, it is also an empty parameter list, as the
3880 syntax does not permit empty abstract declarators. Otherwise, it
3881 is a parenthesized declarator (in which case the analysis may be
3882 repeated inside it, recursively).
3883
3884 ??? There is an ambiguity in a parameter declaration "int
3885 (__attribute__((foo)) x)", where x is not a typedef name: it
3886 could be an abstract declarator for a function, or declare x with
3887 parentheses. The proper resolution of this ambiguity needs
3888 documenting. At present we follow an accident of the old
3889 parser's implementation, whereby the first parameter must have
3890 some declaration specifiers other than just gnu-attributes. Thus as
3891 a parameter declaration it is treated as a parenthesized
3892 parameter named x, and as an abstract declarator it is
3893 rejected.
3894
3895 ??? Also following the old parser, gnu-attributes inside an empty
3896 parameter list are ignored, making it a list not yielding a
3897 prototype, rather than giving an error or making it have one
3898 parameter with implicit type int.
3899
3900 ??? Also following the old parser, typedef names may be
3901 redeclared in declarators, but not Objective-C class names. */
3902
3903 if (kind != C_DTR_ABSTRACT
3904 && c_parser_next_token_is (parser, CPP_NAME)
3905 && ((type_seen_p
3906 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
3907 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
3908 || c_parser_peek_token (parser)->id_kind == C_ID_ID))
3909 {
3910 struct c_declarator *inner
3911 = build_id_declarator (c_parser_peek_token (parser)->value);
3912 *seen_id = true;
3913 inner->id_loc = c_parser_peek_token (parser)->location;
3914 c_parser_consume_token (parser);
3915 if (c_parser_nth_token_starts_std_attributes (parser, 1))
3916 inner->u.id.attrs = c_parser_std_attribute_specifier_sequence (parser);
3917 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3918 }
3919
3920 if (kind != C_DTR_NORMAL
3921 && c_parser_next_token_is (parser, CPP_OPEN_SQUARE)
3922 && !c_parser_nth_token_starts_std_attributes (parser, 1))
3923 {
3924 struct c_declarator *inner = build_id_declarator (NULL_TREE);
3925 inner->id_loc = c_parser_peek_token (parser)->location;
3926 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3927 }
3928
3929 /* Either we are at the end of an abstract declarator, or we have
3930 parentheses. */
3931
3932 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
3933 {
3934 tree attrs;
3935 struct c_declarator *inner;
3936 c_parser_consume_token (parser);
3937 bool have_gnu_attrs = c_parser_next_token_is_keyword (parser,
3938 RID_ATTRIBUTE);
3939 attrs = c_parser_gnu_attributes (parser);
3940 if (kind != C_DTR_NORMAL
3941 && (c_parser_next_token_starts_declspecs (parser)
3942 || (!have_gnu_attrs
3943 && c_parser_nth_token_starts_std_attributes (parser, 1))
3944 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN)))
3945 {
3946 struct c_arg_info *args
3947 = c_parser_parms_declarator (parser, kind == C_DTR_NORMAL,
3948 attrs, have_gnu_attrs);
3949 if (args == NULL)
3950 return NULL;
3951 else
3952 {
3953 inner = build_id_declarator (NULL_TREE);
3954 if (!(args->types
3955 && args->types != error_mark_node
3956 && TREE_CODE (TREE_VALUE (args->types)) == IDENTIFIER_NODE)
3957 && c_parser_nth_token_starts_std_attributes (parser, 1))
3958 {
3959 tree std_attrs
3960 = c_parser_std_attribute_specifier_sequence (parser);
3961 if (std_attrs)
3962 inner = build_attrs_declarator (std_attrs, inner);
3963 }
3964 inner = build_function_declarator (args, inner);
3965 return c_parser_direct_declarator_inner (parser, *seen_id,
3966 inner);
3967 }
3968 }
3969 /* A parenthesized declarator. */
3970 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
3971 if (inner != NULL && attrs != NULL)
3972 inner = build_attrs_declarator (attrs, inner);
3973 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3974 {
3975 c_parser_consume_token (parser);
3976 if (inner == NULL)
3977 return NULL;
3978 else
3979 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3980 }
3981 else
3982 {
3983 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3984 "expected %<)%>");
3985 return NULL;
3986 }
3987 }
3988 else
3989 {
3990 if (kind == C_DTR_NORMAL)
3991 {
3992 c_parser_error (parser, "expected identifier or %<(%>");
3993 return NULL;
3994 }
3995 else
3996 return build_id_declarator (NULL_TREE);
3997 }
3998 }
3999
4000 /* Parse part of a direct declarator or direct abstract declarator,
4001 given that some (in INNER) has already been parsed; ID_PRESENT is
4002 true if an identifier is present, false for an abstract
4003 declarator. */
4004
4005 static struct c_declarator *
4006 c_parser_direct_declarator_inner (c_parser *parser, bool id_present,
4007 struct c_declarator *inner)
4008 {
4009 /* Parse a sequence of array declarators and parameter lists. */
4010 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE)
4011 && !c_parser_nth_token_starts_std_attributes (parser, 1))
4012 {
4013 location_t brace_loc = c_parser_peek_token (parser)->location;
4014 struct c_declarator *declarator;
4015 struct c_declspecs *quals_attrs = build_null_declspecs ();
4016 bool static_seen;
4017 bool star_seen;
4018 struct c_expr dimen;
4019 dimen.value = NULL_TREE;
4020 dimen.original_code = ERROR_MARK;
4021 dimen.original_type = NULL_TREE;
4022 c_parser_consume_token (parser);
4023 c_parser_declspecs (parser, quals_attrs, false, false, true,
4024 false, false, false, false, cla_prefer_id);
4025 static_seen = c_parser_next_token_is_keyword (parser, RID_STATIC);
4026 if (static_seen)
4027 c_parser_consume_token (parser);
4028 if (static_seen && !quals_attrs->declspecs_seen_p)
4029 c_parser_declspecs (parser, quals_attrs, false, false, true,
4030 false, false, false, false, cla_prefer_id);
4031 if (!quals_attrs->declspecs_seen_p)
4032 quals_attrs = NULL;
4033 /* If "static" is present, there must be an array dimension.
4034 Otherwise, there may be a dimension, "*", or no
4035 dimension. */
4036 if (static_seen)
4037 {
4038 star_seen = false;
4039 dimen = c_parser_expr_no_commas (parser, NULL);
4040 }
4041 else
4042 {
4043 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
4044 {
4045 dimen.value = NULL_TREE;
4046 star_seen = false;
4047 }
4048 else if (c_parser_next_token_is (parser, CPP_MULT))
4049 {
4050 if (c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_SQUARE)
4051 {
4052 dimen.value = NULL_TREE;
4053 star_seen = true;
4054 c_parser_consume_token (parser);
4055 }
4056 else
4057 {
4058 star_seen = false;
4059 dimen = c_parser_expr_no_commas (parser, NULL);
4060 }
4061 }
4062 else
4063 {
4064 star_seen = false;
4065 dimen = c_parser_expr_no_commas (parser, NULL);
4066 }
4067 }
4068 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
4069 c_parser_consume_token (parser);
4070 else
4071 {
4072 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
4073 "expected %<]%>");
4074 return NULL;
4075 }
4076 if (dimen.value)
4077 dimen = convert_lvalue_to_rvalue (brace_loc, dimen, true, true);
4078 declarator = build_array_declarator (brace_loc, dimen.value, quals_attrs,
4079 static_seen, star_seen);
4080 if (declarator == NULL)
4081 return NULL;
4082 if (c_parser_nth_token_starts_std_attributes (parser, 1))
4083 {
4084 tree std_attrs
4085 = c_parser_std_attribute_specifier_sequence (parser);
4086 if (std_attrs)
4087 inner = build_attrs_declarator (std_attrs, inner);
4088 }
4089 inner = set_array_declarator_inner (declarator, inner);
4090 return c_parser_direct_declarator_inner (parser, id_present, inner);
4091 }
4092 else if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
4093 {
4094 tree attrs;
4095 struct c_arg_info *args;
4096 c_parser_consume_token (parser);
4097 bool have_gnu_attrs = c_parser_next_token_is_keyword (parser,
4098 RID_ATTRIBUTE);
4099 attrs = c_parser_gnu_attributes (parser);
4100 args = c_parser_parms_declarator (parser, id_present, attrs,
4101 have_gnu_attrs);
4102 if (args == NULL)
4103 return NULL;
4104 else
4105 {
4106 if (!(args->types
4107 && args->types != error_mark_node
4108 && TREE_CODE (TREE_VALUE (args->types)) == IDENTIFIER_NODE)
4109 && c_parser_nth_token_starts_std_attributes (parser, 1))
4110 {
4111 tree std_attrs
4112 = c_parser_std_attribute_specifier_sequence (parser);
4113 if (std_attrs)
4114 inner = build_attrs_declarator (std_attrs, inner);
4115 }
4116 inner = build_function_declarator (args, inner);
4117 return c_parser_direct_declarator_inner (parser, id_present, inner);
4118 }
4119 }
4120 return inner;
4121 }
4122
4123 /* Parse a parameter list or identifier list, including the closing
4124 parenthesis but not the opening one. ATTRS are the gnu-attributes
4125 at the start of the list. ID_LIST_OK is true if an identifier list
4126 is acceptable; such a list must not have attributes at the start.
4127 HAVE_GNU_ATTRS says whether any gnu-attributes (including empty
4128 attributes) were present (in which case standard attributes cannot
4129 occur). */
4130
4131 static struct c_arg_info *
4132 c_parser_parms_declarator (c_parser *parser, bool id_list_ok, tree attrs,
4133 bool have_gnu_attrs)
4134 {
4135 push_scope ();
4136 declare_parm_level ();
4137 /* If the list starts with an identifier, it is an identifier list.
4138 Otherwise, it is either a prototype list or an empty list. */
4139 if (id_list_ok
4140 && !attrs
4141 && c_parser_next_token_is (parser, CPP_NAME)
4142 && c_parser_peek_token (parser)->id_kind == C_ID_ID
4143
4144 /* Look ahead to detect typos in type names. */
4145 && c_parser_peek_2nd_token (parser)->type != CPP_NAME
4146 && c_parser_peek_2nd_token (parser)->type != CPP_MULT
4147 && c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN
4148 && c_parser_peek_2nd_token (parser)->type != CPP_OPEN_SQUARE
4149 && c_parser_peek_2nd_token (parser)->type != CPP_KEYWORD)
4150 {
4151 tree list = NULL_TREE, *nextp = &list;
4152 while (c_parser_next_token_is (parser, CPP_NAME)
4153 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
4154 {
4155 *nextp = build_tree_list (NULL_TREE,
4156 c_parser_peek_token (parser)->value);
4157 nextp = & TREE_CHAIN (*nextp);
4158 c_parser_consume_token (parser);
4159 if (c_parser_next_token_is_not (parser, CPP_COMMA))
4160 break;
4161 c_parser_consume_token (parser);
4162 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4163 {
4164 c_parser_error (parser, "expected identifier");
4165 break;
4166 }
4167 }
4168 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4169 {
4170 struct c_arg_info *ret = build_arg_info ();
4171 ret->types = list;
4172 c_parser_consume_token (parser);
4173 pop_scope ();
4174 return ret;
4175 }
4176 else
4177 {
4178 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4179 "expected %<)%>");
4180 pop_scope ();
4181 return NULL;
4182 }
4183 }
4184 else
4185 {
4186 struct c_arg_info *ret
4187 = c_parser_parms_list_declarator (parser, attrs, NULL, have_gnu_attrs);
4188 pop_scope ();
4189 return ret;
4190 }
4191 }
4192
4193 /* Parse a parameter list (possibly empty), including the closing
4194 parenthesis but not the opening one. ATTRS are the gnu-attributes
4195 at the start of the list; if HAVE_GNU_ATTRS, there were some such
4196 attributes (possibly empty, in which case ATTRS is NULL_TREE),
4197 which means standard attributes cannot start the list. EXPR is
4198 NULL or an expression that needs to be evaluated for the side
4199 effects of array size expressions in the parameters. */
4200
4201 static struct c_arg_info *
4202 c_parser_parms_list_declarator (c_parser *parser, tree attrs, tree expr,
4203 bool have_gnu_attrs)
4204 {
4205 bool bad_parm = false;
4206
4207 /* ??? Following the old parser, forward parameter declarations may
4208 use abstract declarators, and if no real parameter declarations
4209 follow the forward declarations then this is not diagnosed. Also
4210 note as above that gnu-attributes are ignored as the only contents of
4211 the parentheses, or as the only contents after forward
4212 declarations. */
4213 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4214 {
4215 struct c_arg_info *ret = build_arg_info ();
4216 c_parser_consume_token (parser);
4217 return ret;
4218 }
4219 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
4220 {
4221 struct c_arg_info *ret = build_arg_info ();
4222
4223 if (flag_allow_parameterless_variadic_functions)
4224 {
4225 /* F (...) is allowed. */
4226 ret->types = NULL_TREE;
4227 }
4228 else
4229 {
4230 /* Suppress -Wold-style-definition for this case. */
4231 ret->types = error_mark_node;
4232 error_at (c_parser_peek_token (parser)->location,
4233 "ISO C requires a named argument before %<...%>");
4234 }
4235 c_parser_consume_token (parser);
4236 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4237 {
4238 c_parser_consume_token (parser);
4239 return ret;
4240 }
4241 else
4242 {
4243 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4244 "expected %<)%>");
4245 return NULL;
4246 }
4247 }
4248 /* Nonempty list of parameters, either terminated with semicolon
4249 (forward declarations; recurse) or with close parenthesis (normal
4250 function) or with ", ... )" (variadic function). */
4251 while (true)
4252 {
4253 /* Parse a parameter. */
4254 struct c_parm *parm = c_parser_parameter_declaration (parser, attrs,
4255 have_gnu_attrs);
4256 attrs = NULL_TREE;
4257 have_gnu_attrs = false;
4258 if (parm == NULL)
4259 bad_parm = true;
4260 else
4261 push_parm_decl (parm, &expr);
4262 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4263 {
4264 tree new_attrs;
4265 c_parser_consume_token (parser);
4266 mark_forward_parm_decls ();
4267 bool new_have_gnu_attrs
4268 = c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE);
4269 new_attrs = c_parser_gnu_attributes (parser);
4270 return c_parser_parms_list_declarator (parser, new_attrs, expr,
4271 new_have_gnu_attrs);
4272 }
4273 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4274 {
4275 c_parser_consume_token (parser);
4276 if (bad_parm)
4277 return NULL;
4278 else
4279 return get_parm_info (false, expr);
4280 }
4281 if (!c_parser_require (parser, CPP_COMMA,
4282 "expected %<;%>, %<,%> or %<)%>",
4283 UNKNOWN_LOCATION, false))
4284 {
4285 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4286 return NULL;
4287 }
4288 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
4289 {
4290 c_parser_consume_token (parser);
4291 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4292 {
4293 c_parser_consume_token (parser);
4294 if (bad_parm)
4295 return NULL;
4296 else
4297 return get_parm_info (true, expr);
4298 }
4299 else
4300 {
4301 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4302 "expected %<)%>");
4303 return NULL;
4304 }
4305 }
4306 }
4307 }
4308
4309 /* Parse a parameter declaration. ATTRS are the gnu-attributes at the
4310 start of the declaration if it is the first parameter;
4311 HAVE_GNU_ATTRS is true if there were any gnu-attributes there (even
4312 empty) there. */
4313
4314 static struct c_parm *
4315 c_parser_parameter_declaration (c_parser *parser, tree attrs,
4316 bool have_gnu_attrs)
4317 {
4318 struct c_declspecs *specs;
4319 struct c_declarator *declarator;
4320 tree prefix_attrs;
4321 tree postfix_attrs = NULL_TREE;
4322 bool dummy = false;
4323
4324 /* Accept #pragmas between parameter declarations. */
4325 while (c_parser_next_token_is (parser, CPP_PRAGMA))
4326 c_parser_pragma (parser, pragma_param, NULL);
4327
4328 if (!c_parser_next_token_starts_declspecs (parser)
4329 && !c_parser_nth_token_starts_std_attributes (parser, 1))
4330 {
4331 c_token *token = c_parser_peek_token (parser);
4332 if (parser->error)
4333 return NULL;
4334 c_parser_set_source_position_from_token (token);
4335 if (c_parser_next_tokens_start_typename (parser, cla_prefer_type))
4336 {
4337 auto_diagnostic_group d;
4338 name_hint hint = lookup_name_fuzzy (token->value,
4339 FUZZY_LOOKUP_TYPENAME,
4340 token->location);
4341 if (const char *suggestion = hint.suggestion ())
4342 {
4343 gcc_rich_location richloc (token->location);
4344 richloc.add_fixit_replace (suggestion);
4345 error_at (&richloc,
4346 "unknown type name %qE; did you mean %qs?",
4347 token->value, suggestion);
4348 }
4349 else
4350 error_at (token->location, "unknown type name %qE", token->value);
4351 parser->error = true;
4352 }
4353 /* ??? In some Objective-C cases '...' isn't applicable so there
4354 should be a different message. */
4355 else
4356 c_parser_error (parser,
4357 "expected declaration specifiers or %<...%>");
4358 c_parser_skip_to_end_of_parameter (parser);
4359 return NULL;
4360 }
4361
4362 location_t start_loc = c_parser_peek_token (parser)->location;
4363
4364 specs = build_null_declspecs ();
4365 if (attrs)
4366 {
4367 declspecs_add_attrs (input_location, specs, attrs);
4368 attrs = NULL_TREE;
4369 }
4370 c_parser_declspecs (parser, specs, true, true, true, true, false,
4371 !have_gnu_attrs, true, cla_nonabstract_decl);
4372 finish_declspecs (specs);
4373 pending_xref_error ();
4374 prefix_attrs = specs->attrs;
4375 specs->attrs = NULL_TREE;
4376 declarator = c_parser_declarator (parser,
4377 specs->typespec_kind != ctsk_none,
4378 C_DTR_PARM, &dummy);
4379 if (declarator == NULL)
4380 {
4381 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
4382 return NULL;
4383 }
4384 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
4385 postfix_attrs = c_parser_gnu_attributes (parser);
4386
4387 /* Generate a location for the parameter, ranging from the start of the
4388 initial token to the end of the final token.
4389
4390 If we have a identifier, then use it for the caret location, e.g.
4391
4392 extern int callee (int one, int (*two)(int, int), float three);
4393 ~~~~~~^~~~~~~~~~~~~~
4394
4395 otherwise, reuse the start location for the caret location e.g.:
4396
4397 extern int callee (int one, int (*)(int, int), float three);
4398 ^~~~~~~~~~~~~~~~~
4399 */
4400 location_t end_loc = parser->last_token_location;
4401
4402 /* Find any cdk_id declarator; determine if we have an identifier. */
4403 c_declarator *id_declarator = declarator;
4404 while (id_declarator && id_declarator->kind != cdk_id)
4405 id_declarator = id_declarator->declarator;
4406 location_t caret_loc = (id_declarator->u.id.id
4407 ? id_declarator->id_loc
4408 : start_loc);
4409 location_t param_loc = make_location (caret_loc, start_loc, end_loc);
4410
4411 return build_c_parm (specs, chainon (postfix_attrs, prefix_attrs),
4412 declarator, param_loc);
4413 }
4414
4415 /* Parse a string literal in an asm expression. It should not be
4416 translated, and wide string literals are an error although
4417 permitted by the syntax. This is a GNU extension.
4418
4419 asm-string-literal:
4420 string-literal
4421 */
4422
4423 static tree
4424 c_parser_asm_string_literal (c_parser *parser)
4425 {
4426 tree str;
4427 int save_flag = warn_overlength_strings;
4428 warn_overlength_strings = 0;
4429 str = c_parser_string_literal (parser, false, false).value;
4430 warn_overlength_strings = save_flag;
4431 return str;
4432 }
4433
4434 /* Parse a simple asm expression. This is used in restricted
4435 contexts, where a full expression with inputs and outputs does not
4436 make sense. This is a GNU extension.
4437
4438 simple-asm-expr:
4439 asm ( asm-string-literal )
4440 */
4441
4442 static tree
4443 c_parser_simple_asm_expr (c_parser *parser)
4444 {
4445 tree str;
4446 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
4447 c_parser_consume_token (parser);
4448 matching_parens parens;
4449 if (!parens.require_open (parser))
4450 return NULL_TREE;
4451 str = c_parser_asm_string_literal (parser);
4452 if (!parens.require_close (parser))
4453 {
4454 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4455 return NULL_TREE;
4456 }
4457 return str;
4458 }
4459
4460 static tree
4461 c_parser_gnu_attribute_any_word (c_parser *parser)
4462 {
4463 tree attr_name = NULL_TREE;
4464
4465 if (c_parser_next_token_is (parser, CPP_KEYWORD))
4466 {
4467 /* ??? See comment above about what keywords are accepted here. */
4468 bool ok;
4469 switch (c_parser_peek_token (parser)->keyword)
4470 {
4471 case RID_STATIC:
4472 case RID_UNSIGNED:
4473 case RID_LONG:
4474 case RID_CONST:
4475 case RID_EXTERN:
4476 case RID_REGISTER:
4477 case RID_TYPEDEF:
4478 case RID_SHORT:
4479 case RID_INLINE:
4480 case RID_NORETURN:
4481 case RID_VOLATILE:
4482 case RID_SIGNED:
4483 case RID_AUTO:
4484 case RID_RESTRICT:
4485 case RID_COMPLEX:
4486 case RID_THREAD:
4487 case RID_INT:
4488 case RID_CHAR:
4489 case RID_FLOAT:
4490 case RID_DOUBLE:
4491 case RID_VOID:
4492 case RID_DFLOAT32:
4493 case RID_DFLOAT64:
4494 case RID_DFLOAT128:
4495 CASE_RID_FLOATN_NX:
4496 case RID_BOOL:
4497 case RID_FRACT:
4498 case RID_ACCUM:
4499 case RID_SAT:
4500 case RID_TRANSACTION_ATOMIC:
4501 case RID_TRANSACTION_CANCEL:
4502 case RID_ATOMIC:
4503 case RID_AUTO_TYPE:
4504 case RID_INT_N_0:
4505 case RID_INT_N_1:
4506 case RID_INT_N_2:
4507 case RID_INT_N_3:
4508 ok = true;
4509 break;
4510 default:
4511 ok = false;
4512 break;
4513 }
4514 if (!ok)
4515 return NULL_TREE;
4516
4517 /* Accept __attribute__((__const)) as __attribute__((const)) etc. */
4518 attr_name = ridpointers[(int) c_parser_peek_token (parser)->keyword];
4519 }
4520 else if (c_parser_next_token_is (parser, CPP_NAME))
4521 attr_name = c_parser_peek_token (parser)->value;
4522
4523 return attr_name;
4524 }
4525
4526 /* Parse attribute arguments. This is a common form of syntax
4527 covering all currently valid GNU and standard attributes.
4528
4529 gnu-attribute-arguments:
4530 identifier
4531 identifier , nonempty-expr-list
4532 expr-list
4533
4534 where the "identifier" must not be declared as a type. ??? Why not
4535 allow identifiers declared as types to start the arguments? */
4536
4537 static tree
4538 c_parser_attribute_arguments (c_parser *parser, bool takes_identifier,
4539 bool require_string, bool allow_empty_args)
4540 {
4541 vec<tree, va_gc> *expr_list;
4542 tree attr_args;
4543 /* Parse the attribute contents. If they start with an
4544 identifier which is followed by a comma or close
4545 parenthesis, then the arguments start with that
4546 identifier; otherwise they are an expression list.
4547 In objective-c the identifier may be a classname. */
4548 if (c_parser_next_token_is (parser, CPP_NAME)
4549 && (c_parser_peek_token (parser)->id_kind == C_ID_ID
4550 || (c_dialect_objc ()
4551 && c_parser_peek_token (parser)->id_kind
4552 == C_ID_CLASSNAME))
4553 && ((c_parser_peek_2nd_token (parser)->type == CPP_COMMA)
4554 || (c_parser_peek_2nd_token (parser)->type
4555 == CPP_CLOSE_PAREN))
4556 && (takes_identifier
4557 || (c_dialect_objc ()
4558 && c_parser_peek_token (parser)->id_kind
4559 == C_ID_CLASSNAME)))
4560 {
4561 tree arg1 = c_parser_peek_token (parser)->value;
4562 c_parser_consume_token (parser);
4563 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4564 attr_args = build_tree_list (NULL_TREE, arg1);
4565 else
4566 {
4567 tree tree_list;
4568 c_parser_consume_token (parser);
4569 expr_list = c_parser_expr_list (parser, false, true,
4570 NULL, NULL, NULL, NULL);
4571 tree_list = build_tree_list_vec (expr_list);
4572 attr_args = tree_cons (NULL_TREE, arg1, tree_list);
4573 release_tree_vector (expr_list);
4574 }
4575 }
4576 else
4577 {
4578 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4579 {
4580 if (!allow_empty_args)
4581 error_at (c_parser_peek_token (parser)->location,
4582 "parentheses must be omitted if "
4583 "attribute argument list is empty");
4584 attr_args = NULL_TREE;
4585 }
4586 else if (require_string)
4587 {
4588 /* The only valid argument for this attribute is a string
4589 literal. Handle this specially here to avoid accepting
4590 string literals with excess parentheses. */
4591 tree string = c_parser_string_literal (parser, false, true).value;
4592 attr_args = build_tree_list (NULL_TREE, string);
4593 }
4594 else
4595 {
4596 expr_list = c_parser_expr_list (parser, false, true,
4597 NULL, NULL, NULL, NULL);
4598 attr_args = build_tree_list_vec (expr_list);
4599 release_tree_vector (expr_list);
4600 }
4601 }
4602 return attr_args;
4603 }
4604
4605 /* Parse (possibly empty) gnu-attributes. This is a GNU extension.
4606
4607 gnu-attributes:
4608 empty
4609 gnu-attributes gnu-attribute
4610
4611 gnu-attribute:
4612 __attribute__ ( ( gnu-attribute-list ) )
4613
4614 gnu-attribute-list:
4615 gnu-attrib
4616 gnu-attribute_list , gnu-attrib
4617
4618 gnu-attrib:
4619 empty
4620 any-word
4621 any-word ( gnu-attribute-arguments )
4622
4623 where "any-word" may be any identifier (including one declared as a
4624 type), a reserved word storage class specifier, type specifier or
4625 type qualifier. ??? This still leaves out most reserved keywords
4626 (following the old parser), shouldn't we include them?
4627 When EXPECT_COMMA is true, expect the attribute to be preceded
4628 by a comma and fail if it isn't.
4629 When EMPTY_OK is true, allow and consume any number of consecutive
4630 commas with no attributes in between. */
4631
4632 static tree
4633 c_parser_gnu_attribute (c_parser *parser, tree attrs,
4634 bool expect_comma = false, bool empty_ok = true)
4635 {
4636 bool comma_first = c_parser_next_token_is (parser, CPP_COMMA);
4637 if (!comma_first
4638 && !c_parser_next_token_is (parser, CPP_NAME)
4639 && !c_parser_next_token_is (parser, CPP_KEYWORD))
4640 return NULL_TREE;
4641
4642 while (c_parser_next_token_is (parser, CPP_COMMA))
4643 {
4644 c_parser_consume_token (parser);
4645 if (!empty_ok)
4646 return attrs;
4647 }
4648
4649 tree attr_name = c_parser_gnu_attribute_any_word (parser);
4650 if (attr_name == NULL_TREE)
4651 return NULL_TREE;
4652
4653 attr_name = canonicalize_attr_name (attr_name);
4654 c_parser_consume_token (parser);
4655
4656 tree attr;
4657 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
4658 {
4659 if (expect_comma && !comma_first)
4660 {
4661 /* A comma is missing between the last attribute on the chain
4662 and this one. */
4663 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4664 "expected %<)%>");
4665 return error_mark_node;
4666 }
4667 attr = build_tree_list (attr_name, NULL_TREE);
4668 /* Add this attribute to the list. */
4669 attrs = chainon (attrs, attr);
4670 return attrs;
4671 }
4672 c_parser_consume_token (parser);
4673
4674 tree attr_args
4675 = c_parser_attribute_arguments (parser,
4676 attribute_takes_identifier_p (attr_name),
4677 false, true);
4678
4679 attr = build_tree_list (attr_name, attr_args);
4680 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4681 c_parser_consume_token (parser);
4682 else
4683 {
4684 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4685 "expected %<)%>");
4686 return error_mark_node;
4687 }
4688
4689 if (expect_comma && !comma_first)
4690 {
4691 /* A comma is missing between the last attribute on the chain
4692 and this one. */
4693 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4694 "expected %<)%>");
4695 return error_mark_node;
4696 }
4697
4698 /* Add this attribute to the list. */
4699 attrs = chainon (attrs, attr);
4700 return attrs;
4701 }
4702
4703 static tree
4704 c_parser_gnu_attributes (c_parser *parser)
4705 {
4706 tree attrs = NULL_TREE;
4707 while (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
4708 {
4709 bool save_translate_strings_p = parser->translate_strings_p;
4710 parser->translate_strings_p = false;
4711 /* Consume the `__attribute__' keyword. */
4712 c_parser_consume_token (parser);
4713 /* Look for the two `(' tokens. */
4714 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4715 {
4716 parser->translate_strings_p = save_translate_strings_p;
4717 return attrs;
4718 }
4719 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4720 {
4721 parser->translate_strings_p = save_translate_strings_p;
4722 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4723 return attrs;
4724 }
4725 /* Parse the attribute list. Require a comma between successive
4726 (possibly empty) attributes. */
4727 for (bool expect_comma = false; ; expect_comma = true)
4728 {
4729 /* Parse a single attribute. */
4730 tree attr = c_parser_gnu_attribute (parser, attrs, expect_comma);
4731 if (attr == error_mark_node)
4732 return attrs;
4733 if (!attr)
4734 break;
4735 attrs = attr;
4736 }
4737
4738 /* Look for the two `)' tokens. */
4739 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4740 c_parser_consume_token (parser);
4741 else
4742 {
4743 parser->translate_strings_p = save_translate_strings_p;
4744 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4745 "expected %<)%>");
4746 return attrs;
4747 }
4748 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4749 c_parser_consume_token (parser);
4750 else
4751 {
4752 parser->translate_strings_p = save_translate_strings_p;
4753 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4754 "expected %<)%>");
4755 return attrs;
4756 }
4757 parser->translate_strings_p = save_translate_strings_p;
4758 }
4759
4760 return attrs;
4761 }
4762
4763 /* Parse an optional balanced token sequence.
4764
4765 balanced-token-sequence:
4766 balanced-token
4767 balanced-token-sequence balanced-token
4768
4769 balanced-token:
4770 ( balanced-token-sequence[opt] )
4771 [ balanced-token-sequence[opt] ]
4772 { balanced-token-sequence[opt] }
4773 any token other than ()[]{}
4774 */
4775
4776 static void
4777 c_parser_balanced_token_sequence (c_parser *parser)
4778 {
4779 while (true)
4780 {
4781 c_token *token = c_parser_peek_token (parser);
4782 switch (token->type)
4783 {
4784 case CPP_OPEN_BRACE:
4785 {
4786 matching_braces braces;
4787 braces.consume_open (parser);
4788 c_parser_balanced_token_sequence (parser);
4789 braces.require_close (parser);
4790 break;
4791 }
4792
4793 case CPP_OPEN_PAREN:
4794 {
4795 matching_parens parens;
4796 parens.consume_open (parser);
4797 c_parser_balanced_token_sequence (parser);
4798 parens.require_close (parser);
4799 break;
4800 }
4801
4802 case CPP_OPEN_SQUARE:
4803 c_parser_consume_token (parser);
4804 c_parser_balanced_token_sequence (parser);
4805 c_parser_require (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
4806 break;
4807
4808 case CPP_CLOSE_BRACE:
4809 case CPP_CLOSE_PAREN:
4810 case CPP_CLOSE_SQUARE:
4811 case CPP_EOF:
4812 return;
4813
4814 default:
4815 c_parser_consume_token (parser);
4816 break;
4817 }
4818 }
4819 }
4820
4821 /* Parse standard (C2X) attributes (including GNU attributes in the
4822 gnu:: namespace).
4823
4824 attribute-specifier-sequence:
4825 attribute-specifier-sequence[opt] attribute-specifier
4826
4827 attribute-specifier:
4828 [ [ attribute-list ] ]
4829
4830 attribute-list:
4831 attribute[opt]
4832 attribute-list, attribute[opt]
4833
4834 attribute:
4835 attribute-token attribute-argument-clause[opt]
4836
4837 attribute-token:
4838 standard-attribute
4839 attribute-prefixed-token
4840
4841 standard-attribute:
4842 identifier
4843
4844 attribute-prefixed-token:
4845 attribute-prefix :: identifier
4846
4847 attribute-prefix:
4848 identifier
4849
4850 attribute-argument-clause:
4851 ( balanced-token-sequence[opt] )
4852
4853 Keywords are accepted as identifiers for this purpose.
4854 */
4855
4856 static tree
4857 c_parser_std_attribute (c_parser *parser, bool for_tm)
4858 {
4859 c_token *token = c_parser_peek_token (parser);
4860 tree ns, name, attribute;
4861
4862 /* Parse the attribute-token. */
4863 if (token->type != CPP_NAME && token->type != CPP_KEYWORD)
4864 {
4865 c_parser_error (parser, "expected identifier");
4866 return error_mark_node;
4867 }
4868 name = canonicalize_attr_name (token->value);
4869 c_parser_consume_token (parser);
4870 if (c_parser_next_token_is (parser, CPP_SCOPE))
4871 {
4872 ns = name;
4873 c_parser_consume_token (parser);
4874 token = c_parser_peek_token (parser);
4875 if (token->type != CPP_NAME && token->type != CPP_KEYWORD)
4876 {
4877 c_parser_error (parser, "expected identifier");
4878 return error_mark_node;
4879 }
4880 name = canonicalize_attr_name (token->value);
4881 c_parser_consume_token (parser);
4882 }
4883 else
4884 ns = NULL_TREE;
4885 attribute = build_tree_list (build_tree_list (ns, name), NULL_TREE);
4886
4887 /* Parse the arguments, if any. */
4888 const attribute_spec *as = lookup_attribute_spec (TREE_PURPOSE (attribute));
4889 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
4890 goto out;
4891 {
4892 location_t open_loc = c_parser_peek_token (parser)->location;
4893 matching_parens parens;
4894 parens.consume_open (parser);
4895 if ((as && as->max_length == 0)
4896 /* Special-case the transactional-memory attribute "outer",
4897 which is specially handled but not registered as an
4898 attribute, to avoid allowing arbitrary balanced token
4899 sequences as arguments. */
4900 || is_attribute_p ("outer", name))
4901 {
4902 error_at (open_loc, "%qE attribute does not take any arguments", name);
4903 parens.skip_until_found_close (parser);
4904 return error_mark_node;
4905 }
4906 if (as)
4907 {
4908 bool takes_identifier
4909 = (ns != NULL_TREE
4910 && strcmp (IDENTIFIER_POINTER (ns), "gnu") == 0
4911 && attribute_takes_identifier_p (name));
4912 bool require_string
4913 = (ns == NULL_TREE
4914 && strcmp (IDENTIFIER_POINTER (name), "deprecated") == 0);
4915 TREE_VALUE (attribute)
4916 = c_parser_attribute_arguments (parser, takes_identifier,
4917 require_string, false);
4918 }
4919 else
4920 c_parser_balanced_token_sequence (parser);
4921 parens.require_close (parser);
4922 }
4923 out:
4924 if (ns == NULL_TREE && !for_tm && !as && !is_attribute_p ("nodiscard", name))
4925 {
4926 /* An attribute with standard syntax and no namespace specified
4927 is a constraint violation if it is not one of the known
4928 standard attributes (of which nodiscard is the only one
4929 without a handler in GCC). Diagnose it here with a pedwarn
4930 and then discard it to prevent a duplicate warning later. */
4931 pedwarn (input_location, OPT_Wattributes, "%qE attribute ignored",
4932 name);
4933 return error_mark_node;
4934 }
4935 return attribute;
4936 }
4937
4938 static tree
4939 c_parser_std_attribute_specifier (c_parser *parser, bool for_tm)
4940 {
4941 bool seen_deprecated = false;
4942 bool seen_fallthrough = false;
4943 bool seen_maybe_unused = false;
4944 location_t loc = c_parser_peek_token (parser)->location;
4945 if (!c_parser_require (parser, CPP_OPEN_SQUARE, "expected %<[%>"))
4946 return NULL_TREE;
4947 if (!c_parser_require (parser, CPP_OPEN_SQUARE, "expected %<[%>"))
4948 {
4949 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
4950 return NULL_TREE;
4951 }
4952 if (!for_tm)
4953 pedwarn_c11 (loc, OPT_Wpedantic,
4954 "ISO C does not support %<[[]]%> attributes before C2X");
4955 tree attributes = NULL_TREE;
4956 while (true)
4957 {
4958 c_token *token = c_parser_peek_token (parser);
4959 if (token->type == CPP_CLOSE_SQUARE)
4960 break;
4961 if (token->type == CPP_COMMA)
4962 {
4963 c_parser_consume_token (parser);
4964 continue;
4965 }
4966 tree attribute = c_parser_std_attribute (parser, for_tm);
4967 if (attribute != error_mark_node)
4968 {
4969 bool duplicate = false;
4970 tree name = get_attribute_name (attribute);
4971 tree ns = get_attribute_namespace (attribute);
4972 if (ns == NULL_TREE)
4973 {
4974 /* Some standard attributes may appear at most once in
4975 each attribute list. Diagnose duplicates and remove
4976 them from the list to avoid subsequent diagnostics
4977 such as the more general one for multiple
4978 "fallthrough" attributes in the same place (including
4979 in separate attribute lists in the same attribute
4980 specifier sequence, which is not a constraint
4981 violation). */
4982 if (is_attribute_p ("deprecated", name))
4983 {
4984 if (seen_deprecated)
4985 {
4986 error ("attribute %<deprecated%> can appear at most "
4987 "once in an attribute-list");
4988 duplicate = true;
4989 }
4990 seen_deprecated = true;
4991 }
4992 else if (is_attribute_p ("fallthrough", name))
4993 {
4994 if (seen_fallthrough)
4995 {
4996 error ("attribute %<fallthrough%> can appear at most "
4997 "once in an attribute-list");
4998 duplicate = true;
4999 }
5000 seen_fallthrough = true;
5001 }
5002 else if (is_attribute_p ("maybe_unused", name))
5003 {
5004 if (seen_maybe_unused)
5005 {
5006 error ("attribute %<maybe_unused%> can appear at most "
5007 "once in an attribute-list");
5008 duplicate = true;
5009 }
5010 seen_maybe_unused = true;
5011 }
5012 }
5013 if (!duplicate)
5014 {
5015 TREE_CHAIN (attribute) = attributes;
5016 attributes = attribute;
5017 }
5018 }
5019 if (c_parser_next_token_is_not (parser, CPP_COMMA))
5020 break;
5021 }
5022 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
5023 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
5024 return nreverse (attributes);
5025 }
5026
5027 /* Look past an optional balanced token sequence of raw look-ahead
5028 tokens starting with the *Nth token. *N is updated to point to the
5029 following token. Return true if such a sequence was found, false
5030 if the tokens parsed were not balanced. */
5031
5032 static bool
5033 c_parser_check_balanced_raw_token_sequence (c_parser *parser, unsigned int *n)
5034 {
5035 while (true)
5036 {
5037 c_token *token = c_parser_peek_nth_token_raw (parser, *n);
5038 switch (token->type)
5039 {
5040 case CPP_OPEN_BRACE:
5041 {
5042 ++*n;
5043 if (c_parser_check_balanced_raw_token_sequence (parser, n))
5044 {
5045 token = c_parser_peek_nth_token_raw (parser, *n);
5046 if (token->type == CPP_CLOSE_BRACE)
5047 ++*n;
5048 else
5049 return false;
5050 }
5051 else
5052 return false;
5053 break;
5054 }
5055
5056 case CPP_OPEN_PAREN:
5057 {
5058 ++*n;
5059 if (c_parser_check_balanced_raw_token_sequence (parser, n))
5060 {
5061 token = c_parser_peek_nth_token_raw (parser, *n);
5062 if (token->type == CPP_CLOSE_PAREN)
5063 ++*n;
5064 else
5065 return false;
5066 }
5067 else
5068 return false;
5069 break;
5070 }
5071
5072 case CPP_OPEN_SQUARE:
5073 {
5074 ++*n;
5075 if (c_parser_check_balanced_raw_token_sequence (parser, n))
5076 {
5077 token = c_parser_peek_nth_token_raw (parser, *n);
5078 if (token->type == CPP_CLOSE_SQUARE)
5079 ++*n;
5080 else
5081 return false;
5082 }
5083 else
5084 return false;
5085 break;
5086 }
5087
5088 case CPP_CLOSE_BRACE:
5089 case CPP_CLOSE_PAREN:
5090 case CPP_CLOSE_SQUARE:
5091 case CPP_EOF:
5092 return true;
5093
5094 default:
5095 ++*n;
5096 break;
5097 }
5098 }
5099 }
5100
5101 /* Return whether standard attributes start with the Nth token. */
5102
5103 static bool
5104 c_parser_nth_token_starts_std_attributes (c_parser *parser, unsigned int n)
5105 {
5106 if (!(c_parser_peek_nth_token (parser, n)->type == CPP_OPEN_SQUARE
5107 && c_parser_peek_nth_token (parser, n + 1)->type == CPP_OPEN_SQUARE))
5108 return false;
5109 /* In C, '[[' must start attributes. In Objective-C, we need to
5110 check whether '[[' is matched by ']]'. */
5111 if (!c_dialect_objc ())
5112 return true;
5113 n += 2;
5114 if (!c_parser_check_balanced_raw_token_sequence (parser, &n))
5115 return false;
5116 c_token *token = c_parser_peek_nth_token_raw (parser, n);
5117 if (token->type != CPP_CLOSE_SQUARE)
5118 return false;
5119 token = c_parser_peek_nth_token_raw (parser, n + 1);
5120 return token->type == CPP_CLOSE_SQUARE;
5121 }
5122
5123 static tree
5124 c_parser_std_attribute_specifier_sequence (c_parser *parser)
5125 {
5126 tree attributes = NULL_TREE;
5127 do
5128 {
5129 tree attrs = c_parser_std_attribute_specifier (parser, false);
5130 attributes = chainon (attributes, attrs);
5131 }
5132 while (c_parser_nth_token_starts_std_attributes (parser, 1));
5133 return attributes;
5134 }
5135
5136 /* Parse a type name (C90 6.5.5, C99 6.7.6, C11 6.7.7). ALIGNAS_OK
5137 says whether alignment specifiers are OK (only in cases that might
5138 be the type name of a compound literal).
5139
5140 type-name:
5141 specifier-qualifier-list abstract-declarator[opt]
5142 */
5143
5144 struct c_type_name *
5145 c_parser_type_name (c_parser *parser, bool alignas_ok)
5146 {
5147 struct c_declspecs *specs = build_null_declspecs ();
5148 struct c_declarator *declarator;
5149 struct c_type_name *ret;
5150 bool dummy = false;
5151 c_parser_declspecs (parser, specs, false, true, true, alignas_ok, false,
5152 false, true, cla_prefer_type);
5153 if (!specs->declspecs_seen_p)
5154 {
5155 c_parser_error (parser, "expected specifier-qualifier-list");
5156 return NULL;
5157 }
5158 if (specs->type != error_mark_node)
5159 {
5160 pending_xref_error ();
5161 finish_declspecs (specs);
5162 }
5163 declarator = c_parser_declarator (parser,
5164 specs->typespec_kind != ctsk_none,
5165 C_DTR_ABSTRACT, &dummy);
5166 if (declarator == NULL)
5167 return NULL;
5168 ret = XOBNEW (&parser_obstack, struct c_type_name);
5169 ret->specs = specs;
5170 ret->declarator = declarator;
5171 return ret;
5172 }
5173
5174 /* Parse an initializer (C90 6.5.7, C99 6.7.8, C11 6.7.9).
5175
5176 initializer:
5177 assignment-expression
5178 { initializer-list }
5179 { initializer-list , }
5180
5181 initializer-list:
5182 designation[opt] initializer
5183 initializer-list , designation[opt] initializer
5184
5185 designation:
5186 designator-list =
5187
5188 designator-list:
5189 designator
5190 designator-list designator
5191
5192 designator:
5193 array-designator
5194 . identifier
5195
5196 array-designator:
5197 [ constant-expression ]
5198
5199 GNU extensions:
5200
5201 initializer:
5202 { }
5203
5204 designation:
5205 array-designator
5206 identifier :
5207
5208 array-designator:
5209 [ constant-expression ... constant-expression ]
5210
5211 Any expression without commas is accepted in the syntax for the
5212 constant-expressions, with non-constant expressions rejected later.
5213
5214 This function is only used for top-level initializers; for nested
5215 ones, see c_parser_initval. */
5216
5217 static struct c_expr
5218 c_parser_initializer (c_parser *parser)
5219 {
5220 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5221 return c_parser_braced_init (parser, NULL_TREE, false, NULL);
5222 else
5223 {
5224 struct c_expr ret;
5225 location_t loc = c_parser_peek_token (parser)->location;
5226 ret = c_parser_expr_no_commas (parser, NULL);
5227 if (TREE_CODE (ret.value) != STRING_CST
5228 && TREE_CODE (ret.value) != COMPOUND_LITERAL_EXPR)
5229 ret = convert_lvalue_to_rvalue (loc, ret, true, true);
5230 return ret;
5231 }
5232 }
5233
5234 /* The location of the last comma within the current initializer list,
5235 or UNKNOWN_LOCATION if not within one. */
5236
5237 location_t last_init_list_comma;
5238
5239 /* Parse a braced initializer list. TYPE is the type specified for a
5240 compound literal, and NULL_TREE for other initializers and for
5241 nested braced lists. NESTED_P is true for nested braced lists,
5242 false for the list of a compound literal or the list that is the
5243 top-level initializer in a declaration. */
5244
5245 static struct c_expr
5246 c_parser_braced_init (c_parser *parser, tree type, bool nested_p,
5247 struct obstack *outer_obstack)
5248 {
5249 struct c_expr ret;
5250 struct obstack braced_init_obstack;
5251 location_t brace_loc = c_parser_peek_token (parser)->location;
5252 gcc_obstack_init (&braced_init_obstack);
5253 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
5254 matching_braces braces;
5255 braces.consume_open (parser);
5256 if (nested_p)
5257 {
5258 finish_implicit_inits (brace_loc, outer_obstack);
5259 push_init_level (brace_loc, 0, &braced_init_obstack);
5260 }
5261 else
5262 really_start_incremental_init (type);
5263 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
5264 {
5265 pedwarn (brace_loc, OPT_Wpedantic, "ISO C forbids empty initializer braces");
5266 }
5267 else
5268 {
5269 /* Parse a non-empty initializer list, possibly with a trailing
5270 comma. */
5271 while (true)
5272 {
5273 c_parser_initelt (parser, &braced_init_obstack);
5274 if (parser->error)
5275 break;
5276 if (c_parser_next_token_is (parser, CPP_COMMA))
5277 {
5278 last_init_list_comma = c_parser_peek_token (parser)->location;
5279 c_parser_consume_token (parser);
5280 }
5281 else
5282 break;
5283 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
5284 break;
5285 }
5286 }
5287 c_token *next_tok = c_parser_peek_token (parser);
5288 if (next_tok->type != CPP_CLOSE_BRACE)
5289 {
5290 ret.set_error ();
5291 ret.original_code = ERROR_MARK;
5292 ret.original_type = NULL;
5293 braces.skip_until_found_close (parser);
5294 pop_init_level (brace_loc, 0, &braced_init_obstack, last_init_list_comma);
5295 obstack_free (&braced_init_obstack, NULL);
5296 return ret;
5297 }
5298 location_t close_loc = next_tok->location;
5299 c_parser_consume_token (parser);
5300 ret = pop_init_level (brace_loc, 0, &braced_init_obstack, close_loc);
5301 obstack_free (&braced_init_obstack, NULL);
5302 set_c_expr_source_range (&ret, brace_loc, close_loc);
5303 return ret;
5304 }
5305
5306 /* Parse a nested initializer, including designators. */
5307
5308 static void
5309 c_parser_initelt (c_parser *parser, struct obstack * braced_init_obstack)
5310 {
5311 /* Parse any designator or designator list. A single array
5312 designator may have the subsequent "=" omitted in GNU C, but a
5313 longer list or a structure member designator may not. */
5314 if (c_parser_next_token_is (parser, CPP_NAME)
5315 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
5316 {
5317 /* Old-style structure member designator. */
5318 set_init_label (c_parser_peek_token (parser)->location,
5319 c_parser_peek_token (parser)->value,
5320 c_parser_peek_token (parser)->location,
5321 braced_init_obstack);
5322 /* Use the colon as the error location. */
5323 pedwarn (c_parser_peek_2nd_token (parser)->location, OPT_Wpedantic,
5324 "obsolete use of designated initializer with %<:%>");
5325 c_parser_consume_token (parser);
5326 c_parser_consume_token (parser);
5327 }
5328 else
5329 {
5330 /* des_seen is 0 if there have been no designators, 1 if there
5331 has been a single array designator and 2 otherwise. */
5332 int des_seen = 0;
5333 /* Location of a designator. */
5334 location_t des_loc = UNKNOWN_LOCATION; /* Quiet warning. */
5335 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE)
5336 || c_parser_next_token_is (parser, CPP_DOT))
5337 {
5338 int des_prev = des_seen;
5339 if (!des_seen)
5340 des_loc = c_parser_peek_token (parser)->location;
5341 if (des_seen < 2)
5342 des_seen++;
5343 if (c_parser_next_token_is (parser, CPP_DOT))
5344 {
5345 des_seen = 2;
5346 c_parser_consume_token (parser);
5347 if (c_parser_next_token_is (parser, CPP_NAME))
5348 {
5349 set_init_label (des_loc, c_parser_peek_token (parser)->value,
5350 c_parser_peek_token (parser)->location,
5351 braced_init_obstack);
5352 c_parser_consume_token (parser);
5353 }
5354 else
5355 {
5356 struct c_expr init;
5357 init.set_error ();
5358 init.original_code = ERROR_MARK;
5359 init.original_type = NULL;
5360 c_parser_error (parser, "expected identifier");
5361 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
5362 process_init_element (input_location, init, false,
5363 braced_init_obstack);
5364 return;
5365 }
5366 }
5367 else
5368 {
5369 tree first, second;
5370 location_t ellipsis_loc = UNKNOWN_LOCATION; /* Quiet warning. */
5371 location_t array_index_loc = UNKNOWN_LOCATION;
5372 /* ??? Following the old parser, [ objc-receiver
5373 objc-message-args ] is accepted as an initializer,
5374 being distinguished from a designator by what follows
5375 the first assignment expression inside the square
5376 brackets, but after a first array designator a
5377 subsequent square bracket is for Objective-C taken to
5378 start an expression, using the obsolete form of
5379 designated initializer without '=', rather than
5380 possibly being a second level of designation: in LALR
5381 terms, the '[' is shifted rather than reducing
5382 designator to designator-list. */
5383 if (des_prev == 1 && c_dialect_objc ())
5384 {
5385 des_seen = des_prev;
5386 break;
5387 }
5388 if (des_prev == 0 && c_dialect_objc ())
5389 {
5390 /* This might be an array designator or an
5391 Objective-C message expression. If the former,
5392 continue parsing here; if the latter, parse the
5393 remainder of the initializer given the starting
5394 primary-expression. ??? It might make sense to
5395 distinguish when des_prev == 1 as well; see
5396 previous comment. */
5397 tree rec, args;
5398 struct c_expr mexpr;
5399 c_parser_consume_token (parser);
5400 if (c_parser_peek_token (parser)->type == CPP_NAME
5401 && ((c_parser_peek_token (parser)->id_kind
5402 == C_ID_TYPENAME)
5403 || (c_parser_peek_token (parser)->id_kind
5404 == C_ID_CLASSNAME)))
5405 {
5406 /* Type name receiver. */
5407 tree id = c_parser_peek_token (parser)->value;
5408 c_parser_consume_token (parser);
5409 rec = objc_get_class_reference (id);
5410 goto parse_message_args;
5411 }
5412 first = c_parser_expr_no_commas (parser, NULL).value;
5413 mark_exp_read (first);
5414 if (c_parser_next_token_is (parser, CPP_ELLIPSIS)
5415 || c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
5416 goto array_desig_after_first;
5417 /* Expression receiver. So far only one part
5418 without commas has been parsed; there might be
5419 more of the expression. */
5420 rec = first;
5421 while (c_parser_next_token_is (parser, CPP_COMMA))
5422 {
5423 struct c_expr next;
5424 location_t comma_loc, exp_loc;
5425 comma_loc = c_parser_peek_token (parser)->location;
5426 c_parser_consume_token (parser);
5427 exp_loc = c_parser_peek_token (parser)->location;
5428 next = c_parser_expr_no_commas (parser, NULL);
5429 next = convert_lvalue_to_rvalue (exp_loc, next,
5430 true, true);
5431 rec = build_compound_expr (comma_loc, rec, next.value);
5432 }
5433 parse_message_args:
5434 /* Now parse the objc-message-args. */
5435 args = c_parser_objc_message_args (parser);
5436 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
5437 "expected %<]%>");
5438 mexpr.value
5439 = objc_build_message_expr (rec, args);
5440 mexpr.original_code = ERROR_MARK;
5441 mexpr.original_type = NULL;
5442 /* Now parse and process the remainder of the
5443 initializer, starting with this message
5444 expression as a primary-expression. */
5445 c_parser_initval (parser, &mexpr, braced_init_obstack);
5446 return;
5447 }
5448 c_parser_consume_token (parser);
5449 array_index_loc = c_parser_peek_token (parser)->location;
5450 first = c_parser_expr_no_commas (parser, NULL).value;
5451 mark_exp_read (first);
5452 array_desig_after_first:
5453 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
5454 {
5455 ellipsis_loc = c_parser_peek_token (parser)->location;
5456 c_parser_consume_token (parser);
5457 second = c_parser_expr_no_commas (parser, NULL).value;
5458 mark_exp_read (second);
5459 }
5460 else
5461 second = NULL_TREE;
5462 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
5463 {
5464 c_parser_consume_token (parser);
5465 set_init_index (array_index_loc, first, second,
5466 braced_init_obstack);
5467 if (second)
5468 pedwarn (ellipsis_loc, OPT_Wpedantic,
5469 "ISO C forbids specifying range of elements to initialize");
5470 }
5471 else
5472 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
5473 "expected %<]%>");
5474 }
5475 }
5476 if (des_seen >= 1)
5477 {
5478 if (c_parser_next_token_is (parser, CPP_EQ))
5479 {
5480 pedwarn_c90 (des_loc, OPT_Wpedantic,
5481 "ISO C90 forbids specifying subobject "
5482 "to initialize");
5483 c_parser_consume_token (parser);
5484 }
5485 else
5486 {
5487 if (des_seen == 1)
5488 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
5489 "obsolete use of designated initializer without %<=%>");
5490 else
5491 {
5492 struct c_expr init;
5493 init.set_error ();
5494 init.original_code = ERROR_MARK;
5495 init.original_type = NULL;
5496 c_parser_error (parser, "expected %<=%>");
5497 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
5498 process_init_element (input_location, init, false,
5499 braced_init_obstack);
5500 return;
5501 }
5502 }
5503 }
5504 }
5505 c_parser_initval (parser, NULL, braced_init_obstack);
5506 }
5507
5508 /* Parse a nested initializer; as c_parser_initializer but parses
5509 initializers within braced lists, after any designators have been
5510 applied. If AFTER is not NULL then it is an Objective-C message
5511 expression which is the primary-expression starting the
5512 initializer. */
5513
5514 static void
5515 c_parser_initval (c_parser *parser, struct c_expr *after,
5516 struct obstack * braced_init_obstack)
5517 {
5518 struct c_expr init;
5519 gcc_assert (!after || c_dialect_objc ());
5520 location_t loc = c_parser_peek_token (parser)->location;
5521
5522 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE) && !after)
5523 init = c_parser_braced_init (parser, NULL_TREE, true,
5524 braced_init_obstack);
5525 else
5526 {
5527 init = c_parser_expr_no_commas (parser, after);
5528 if (init.value != NULL_TREE
5529 && TREE_CODE (init.value) != STRING_CST
5530 && TREE_CODE (init.value) != COMPOUND_LITERAL_EXPR)
5531 init = convert_lvalue_to_rvalue (loc, init, true, true);
5532 }
5533 process_init_element (loc, init, false, braced_init_obstack);
5534 }
5535
5536 /* Parse a compound statement (possibly a function body) (C90 6.6.2,
5537 C99 6.8.2, C11 6.8.2).
5538
5539 compound-statement:
5540 { block-item-list[opt] }
5541 { label-declarations block-item-list }
5542
5543 block-item-list:
5544 block-item
5545 block-item-list block-item
5546
5547 block-item:
5548 nested-declaration
5549 statement
5550
5551 nested-declaration:
5552 declaration
5553
5554 GNU extensions:
5555
5556 compound-statement:
5557 { label-declarations block-item-list }
5558
5559 nested-declaration:
5560 __extension__ nested-declaration
5561 nested-function-definition
5562
5563 label-declarations:
5564 label-declaration
5565 label-declarations label-declaration
5566
5567 label-declaration:
5568 __label__ identifier-list ;
5569
5570 Allowing the mixing of declarations and code is new in C99. The
5571 GNU syntax also permits (not shown above) labels at the end of
5572 compound statements, which yield an error. We don't allow labels
5573 on declarations; this might seem like a natural extension, but
5574 there would be a conflict between gnu-attributes on the label and
5575 prefix gnu-attributes on the declaration. ??? The syntax follows the
5576 old parser in requiring something after label declarations.
5577 Although they are erroneous if the labels declared aren't defined,
5578 is it useful for the syntax to be this way?
5579
5580 OpenACC:
5581
5582 block-item:
5583 openacc-directive
5584
5585 openacc-directive:
5586 update-directive
5587
5588 OpenMP:
5589
5590 block-item:
5591 openmp-directive
5592
5593 openmp-directive:
5594 barrier-directive
5595 flush-directive
5596 taskwait-directive
5597 taskyield-directive
5598 cancel-directive
5599 cancellation-point-directive */
5600
5601 static tree
5602 c_parser_compound_statement (c_parser *parser)
5603 {
5604 tree stmt;
5605 location_t brace_loc;
5606 brace_loc = c_parser_peek_token (parser)->location;
5607 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
5608 {
5609 /* Ensure a scope is entered and left anyway to avoid confusion
5610 if we have just prepared to enter a function body. */
5611 stmt = c_begin_compound_stmt (true);
5612 c_end_compound_stmt (brace_loc, stmt, true);
5613 return error_mark_node;
5614 }
5615 stmt = c_begin_compound_stmt (true);
5616 c_parser_compound_statement_nostart (parser);
5617
5618 return c_end_compound_stmt (brace_loc, stmt, true);
5619 }
5620
5621 /* Parse a compound statement except for the opening brace. This is
5622 used for parsing both compound statements and statement expressions
5623 (which follow different paths to handling the opening). */
5624
5625 static void
5626 c_parser_compound_statement_nostart (c_parser *parser)
5627 {
5628 bool last_stmt = false;
5629 bool last_label = false;
5630 bool save_valid_for_pragma = valid_location_for_stdc_pragma_p ();
5631 location_t label_loc = UNKNOWN_LOCATION; /* Quiet warning. */
5632 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
5633 {
5634 add_debug_begin_stmt (c_parser_peek_token (parser)->location);
5635 c_parser_consume_token (parser);
5636 return;
5637 }
5638 mark_valid_location_for_stdc_pragma (true);
5639 if (c_parser_next_token_is_keyword (parser, RID_LABEL))
5640 {
5641 /* Read zero or more forward-declarations for labels that nested
5642 functions can jump to. */
5643 mark_valid_location_for_stdc_pragma (false);
5644 while (c_parser_next_token_is_keyword (parser, RID_LABEL))
5645 {
5646 label_loc = c_parser_peek_token (parser)->location;
5647 c_parser_consume_token (parser);
5648 /* Any identifiers, including those declared as type names,
5649 are OK here. */
5650 while (true)
5651 {
5652 tree label;
5653 if (c_parser_next_token_is_not (parser, CPP_NAME))
5654 {
5655 c_parser_error (parser, "expected identifier");
5656 break;
5657 }
5658 label
5659 = declare_label (c_parser_peek_token (parser)->value);
5660 C_DECLARED_LABEL_FLAG (label) = 1;
5661 add_stmt (build_stmt (label_loc, DECL_EXPR, label));
5662 c_parser_consume_token (parser);
5663 if (c_parser_next_token_is (parser, CPP_COMMA))
5664 c_parser_consume_token (parser);
5665 else
5666 break;
5667 }
5668 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5669 }
5670 pedwarn (label_loc, OPT_Wpedantic, "ISO C forbids label declarations");
5671 }
5672 /* We must now have at least one statement, label or declaration. */
5673 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
5674 {
5675 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
5676 c_parser_error (parser, "expected declaration or statement");
5677 c_parser_consume_token (parser);
5678 return;
5679 }
5680 while (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
5681 {
5682 location_t loc = c_parser_peek_token (parser)->location;
5683 loc = expansion_point_location_if_in_system_header (loc);
5684 /* Standard attributes may start a statement or a declaration. */
5685 bool have_std_attrs
5686 = c_parser_nth_token_starts_std_attributes (parser, 1);
5687 tree std_attrs = NULL_TREE;
5688 if (have_std_attrs)
5689 std_attrs = c_parser_std_attribute_specifier_sequence (parser);
5690 if (c_parser_next_token_is_keyword (parser, RID_CASE)
5691 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
5692 || (c_parser_next_token_is (parser, CPP_NAME)
5693 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
5694 {
5695 c_warn_unused_attributes (std_attrs);
5696 if (c_parser_next_token_is_keyword (parser, RID_CASE))
5697 label_loc = c_parser_peek_2nd_token (parser)->location;
5698 else
5699 label_loc = c_parser_peek_token (parser)->location;
5700 last_label = true;
5701 last_stmt = false;
5702 mark_valid_location_for_stdc_pragma (false);
5703 c_parser_label (parser);
5704 }
5705 else if (!last_label
5706 && (c_parser_next_tokens_start_declaration (parser)
5707 || (have_std_attrs
5708 && c_parser_next_token_is (parser, CPP_SEMICOLON))))
5709 {
5710 last_label = false;
5711 mark_valid_location_for_stdc_pragma (false);
5712 bool fallthru_attr_p = false;
5713 c_parser_declaration_or_fndef (parser, true, !have_std_attrs,
5714 true, true, true, NULL,
5715 vNULL, have_std_attrs, std_attrs,
5716 NULL, &fallthru_attr_p);
5717 if (last_stmt && !fallthru_attr_p)
5718 pedwarn_c90 (loc, OPT_Wdeclaration_after_statement,
5719 "ISO C90 forbids mixed declarations and code");
5720 last_stmt = fallthru_attr_p;
5721 }
5722 else if (!last_label
5723 && c_parser_next_token_is_keyword (parser, RID_EXTENSION))
5724 {
5725 /* __extension__ can start a declaration, but is also an
5726 unary operator that can start an expression. Consume all
5727 but the last of a possible series of __extension__ to
5728 determine which. If standard attributes have already
5729 been seen, it must start a statement, not a declaration,
5730 but standard attributes starting a declaration may appear
5731 after __extension__. */
5732 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
5733 && (c_parser_peek_2nd_token (parser)->keyword
5734 == RID_EXTENSION))
5735 c_parser_consume_token (parser);
5736 if (!have_std_attrs
5737 && (c_token_starts_declaration (c_parser_peek_2nd_token (parser))
5738 || c_parser_nth_token_starts_std_attributes (parser, 2)))
5739 {
5740 int ext;
5741 ext = disable_extension_diagnostics ();
5742 c_parser_consume_token (parser);
5743 last_label = false;
5744 mark_valid_location_for_stdc_pragma (false);
5745 c_parser_declaration_or_fndef (parser, true, true, true, true,
5746 true, NULL, vNULL);
5747 /* Following the old parser, __extension__ does not
5748 disable this diagnostic. */
5749 restore_extension_diagnostics (ext);
5750 if (last_stmt)
5751 pedwarn_c90 (loc, OPT_Wdeclaration_after_statement,
5752 "ISO C90 forbids mixed declarations and code");
5753 last_stmt = false;
5754 }
5755 else
5756 goto statement;
5757 }
5758 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
5759 {
5760 if (have_std_attrs)
5761 c_parser_error (parser, "expected declaration or statement");
5762 /* External pragmas, and some omp pragmas, are not associated
5763 with regular c code, and so are not to be considered statements
5764 syntactically. This ensures that the user doesn't put them
5765 places that would turn into syntax errors if the directive
5766 were ignored. */
5767 if (c_parser_pragma (parser,
5768 last_label ? pragma_stmt : pragma_compound,
5769 NULL))
5770 last_label = false, last_stmt = true;
5771 }
5772 else if (c_parser_next_token_is (parser, CPP_EOF))
5773 {
5774 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
5775 c_parser_error (parser, "expected declaration or statement");
5776 return;
5777 }
5778 else if (c_parser_next_token_is_keyword (parser, RID_ELSE))
5779 {
5780 if (parser->in_if_block)
5781 {
5782 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
5783 error_at (loc, "expected %<}%> before %<else%>");
5784 return;
5785 }
5786 else
5787 {
5788 error_at (loc, "%<else%> without a previous %<if%>");
5789 c_parser_consume_token (parser);
5790 continue;
5791 }
5792 }
5793 else
5794 {
5795 statement:
5796 c_warn_unused_attributes (std_attrs);
5797 last_label = false;
5798 last_stmt = true;
5799 mark_valid_location_for_stdc_pragma (false);
5800 c_parser_statement_after_labels (parser, NULL);
5801 }
5802
5803 parser->error = false;
5804 }
5805 if (last_label)
5806 error_at (label_loc, "label at end of compound statement");
5807 c_parser_consume_token (parser);
5808 /* Restore the value we started with. */
5809 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
5810 }
5811
5812 /* Parse all consecutive labels, possibly preceded by standard
5813 attributes. In this context, a statement is required, not a
5814 declaration, so attributes must be followed by a statement that is
5815 not just a semicolon. */
5816
5817 static void
5818 c_parser_all_labels (c_parser *parser)
5819 {
5820 if (c_parser_nth_token_starts_std_attributes (parser, 1))
5821 {
5822 tree std_attrs = c_parser_std_attribute_specifier_sequence (parser);
5823 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5824 c_parser_error (parser, "expected statement");
5825 else
5826 c_warn_unused_attributes (std_attrs);
5827 }
5828 while (c_parser_next_token_is_keyword (parser, RID_CASE)
5829 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
5830 || (c_parser_next_token_is (parser, CPP_NAME)
5831 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
5832 c_parser_label (parser);
5833 }
5834
5835 /* Parse a label (C90 6.6.1, C99 6.8.1, C11 6.8.1).
5836
5837 label:
5838 identifier : gnu-attributes[opt]
5839 case constant-expression :
5840 default :
5841
5842 GNU extensions:
5843
5844 label:
5845 case constant-expression ... constant-expression :
5846
5847 The use of gnu-attributes on labels is a GNU extension. The syntax in
5848 GNU C accepts any expressions without commas, non-constant
5849 expressions being rejected later. Any standard
5850 attribute-specifier-sequence before the first label has been parsed
5851 in the caller, to distinguish statements from declarations. Any
5852 attribute-specifier-sequence after the label is parsed in this
5853 function. */
5854
5855 static void
5856 c_parser_label (c_parser *parser)
5857 {
5858 location_t loc1 = c_parser_peek_token (parser)->location;
5859 tree label = NULL_TREE;
5860
5861 /* Remember whether this case or a user-defined label is allowed to fall
5862 through to. */
5863 bool fallthrough_p = c_parser_peek_token (parser)->flags & PREV_FALLTHROUGH;
5864
5865 if (c_parser_next_token_is_keyword (parser, RID_CASE))
5866 {
5867 tree exp1, exp2;
5868 c_parser_consume_token (parser);
5869 exp1 = c_parser_expr_no_commas (parser, NULL).value;
5870 if (c_parser_next_token_is (parser, CPP_COLON))
5871 {
5872 c_parser_consume_token (parser);
5873 label = do_case (loc1, exp1, NULL_TREE);
5874 }
5875 else if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
5876 {
5877 c_parser_consume_token (parser);
5878 exp2 = c_parser_expr_no_commas (parser, NULL).value;
5879 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
5880 label = do_case (loc1, exp1, exp2);
5881 }
5882 else
5883 c_parser_error (parser, "expected %<:%> or %<...%>");
5884 }
5885 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
5886 {
5887 c_parser_consume_token (parser);
5888 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
5889 label = do_case (loc1, NULL_TREE, NULL_TREE);
5890 }
5891 else
5892 {
5893 tree name = c_parser_peek_token (parser)->value;
5894 tree tlab;
5895 tree attrs;
5896 location_t loc2 = c_parser_peek_token (parser)->location;
5897 gcc_assert (c_parser_next_token_is (parser, CPP_NAME));
5898 c_parser_consume_token (parser);
5899 gcc_assert (c_parser_next_token_is (parser, CPP_COLON));
5900 c_parser_consume_token (parser);
5901 attrs = c_parser_gnu_attributes (parser);
5902 tlab = define_label (loc2, name);
5903 if (tlab)
5904 {
5905 decl_attributes (&tlab, attrs, 0);
5906 label = add_stmt (build_stmt (loc1, LABEL_EXPR, tlab));
5907 }
5908 }
5909 if (label)
5910 {
5911 if (TREE_CODE (label) == LABEL_EXPR)
5912 FALLTHROUGH_LABEL_P (LABEL_EXPR_LABEL (label)) = fallthrough_p;
5913 else
5914 FALLTHROUGH_LABEL_P (CASE_LABEL (label)) = fallthrough_p;
5915
5916 /* Standard attributes are only allowed here if they start a
5917 statement, not a declaration (including the case of an
5918 attribute-declaration with only attributes). */
5919 bool have_std_attrs
5920 = c_parser_nth_token_starts_std_attributes (parser, 1);
5921 tree std_attrs = NULL_TREE;
5922 if (have_std_attrs)
5923 std_attrs = c_parser_std_attribute_specifier_sequence (parser);
5924
5925 /* Allow '__attribute__((fallthrough));'. */
5926 if (!have_std_attrs
5927 && c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
5928 {
5929 location_t loc = c_parser_peek_token (parser)->location;
5930 tree attrs = c_parser_gnu_attributes (parser);
5931 if (attribute_fallthrough_p (attrs))
5932 {
5933 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5934 {
5935 tree fn = build_call_expr_internal_loc (loc,
5936 IFN_FALLTHROUGH,
5937 void_type_node, 0);
5938 add_stmt (fn);
5939 }
5940 else
5941 warning_at (loc, OPT_Wattributes, "%<fallthrough%> attribute "
5942 "not followed by %<;%>");
5943 }
5944 else if (attrs != NULL_TREE)
5945 warning_at (loc, OPT_Wattributes, "only attribute %<fallthrough%>"
5946 " can be applied to a null statement");
5947 }
5948 if (c_parser_next_tokens_start_declaration (parser)
5949 || (have_std_attrs
5950 && c_parser_next_token_is (parser, CPP_SEMICOLON)))
5951 {
5952 error_at (c_parser_peek_token (parser)->location,
5953 "a label can only be part of a statement and "
5954 "a declaration is not a statement");
5955 c_parser_declaration_or_fndef (parser, /*fndef_ok*/ false,
5956 /*static_assert_ok*/ true,
5957 /*empty_ok*/ true, /*nested*/ true,
5958 /*start_attr_ok*/ true, NULL,
5959 vNULL, have_std_attrs, std_attrs);
5960 }
5961 else if (std_attrs)
5962 /* Nonempty attributes on the following statement are ignored. */
5963 c_warn_unused_attributes (std_attrs);
5964 }
5965 }
5966
5967 /* Parse a statement (C90 6.6, C99 6.8, C11 6.8).
5968
5969 statement:
5970 labeled-statement
5971 attribute-specifier-sequence[opt] compound-statement
5972 expression-statement
5973 attribute-specifier-sequence[opt] selection-statement
5974 attribute-specifier-sequence[opt] iteration-statement
5975 attribute-specifier-sequence[opt] jump-statement
5976
5977 labeled-statement:
5978 attribute-specifier-sequence[opt] label statement
5979
5980 expression-statement:
5981 expression[opt] ;
5982 attribute-specifier-sequence expression ;
5983
5984 selection-statement:
5985 if-statement
5986 switch-statement
5987
5988 iteration-statement:
5989 while-statement
5990 do-statement
5991 for-statement
5992
5993 jump-statement:
5994 goto identifier ;
5995 continue ;
5996 break ;
5997 return expression[opt] ;
5998
5999 GNU extensions:
6000
6001 statement:
6002 attribute-specifier-sequence[opt] asm-statement
6003
6004 jump-statement:
6005 goto * expression ;
6006
6007 expression-statement:
6008 gnu-attributes ;
6009
6010 Objective-C:
6011
6012 statement:
6013 attribute-specifier-sequence[opt] objc-throw-statement
6014 attribute-specifier-sequence[opt] objc-try-catch-statement
6015 attribute-specifier-sequence[opt] objc-synchronized-statement
6016
6017 objc-throw-statement:
6018 @throw expression ;
6019 @throw ;
6020
6021 OpenACC:
6022
6023 statement:
6024 attribute-specifier-sequence[opt] openacc-construct
6025
6026 openacc-construct:
6027 parallel-construct
6028 kernels-construct
6029 data-construct
6030 loop-construct
6031
6032 parallel-construct:
6033 parallel-directive structured-block
6034
6035 kernels-construct:
6036 kernels-directive structured-block
6037
6038 data-construct:
6039 data-directive structured-block
6040
6041 loop-construct:
6042 loop-directive structured-block
6043
6044 OpenMP:
6045
6046 statement:
6047 attribute-specifier-sequence[opt] openmp-construct
6048
6049 openmp-construct:
6050 parallel-construct
6051 for-construct
6052 simd-construct
6053 for-simd-construct
6054 sections-construct
6055 single-construct
6056 parallel-for-construct
6057 parallel-for-simd-construct
6058 parallel-sections-construct
6059 master-construct
6060 critical-construct
6061 atomic-construct
6062 ordered-construct
6063
6064 parallel-construct:
6065 parallel-directive structured-block
6066
6067 for-construct:
6068 for-directive iteration-statement
6069
6070 simd-construct:
6071 simd-directive iteration-statements
6072
6073 for-simd-construct:
6074 for-simd-directive iteration-statements
6075
6076 sections-construct:
6077 sections-directive section-scope
6078
6079 single-construct:
6080 single-directive structured-block
6081
6082 parallel-for-construct:
6083 parallel-for-directive iteration-statement
6084
6085 parallel-for-simd-construct:
6086 parallel-for-simd-directive iteration-statement
6087
6088 parallel-sections-construct:
6089 parallel-sections-directive section-scope
6090
6091 master-construct:
6092 master-directive structured-block
6093
6094 critical-construct:
6095 critical-directive structured-block
6096
6097 atomic-construct:
6098 atomic-directive expression-statement
6099
6100 ordered-construct:
6101 ordered-directive structured-block
6102
6103 Transactional Memory:
6104
6105 statement:
6106 attribute-specifier-sequence[opt] transaction-statement
6107 attribute-specifier-sequence[opt] transaction-cancel-statement
6108
6109 IF_P is used to track whether there's a (possibly labeled) if statement
6110 which is not enclosed in braces and has an else clause. This is used to
6111 implement -Wparentheses. */
6112
6113 static void
6114 c_parser_statement (c_parser *parser, bool *if_p, location_t *loc_after_labels)
6115 {
6116 c_parser_all_labels (parser);
6117 if (loc_after_labels)
6118 *loc_after_labels = c_parser_peek_token (parser)->location;
6119 c_parser_statement_after_labels (parser, if_p, NULL);
6120 }
6121
6122 /* Parse a statement, other than a labeled statement. CHAIN is a vector
6123 of if-else-if conditions. All labels and standard attributes have
6124 been parsed in the caller.
6125
6126 IF_P is used to track whether there's a (possibly labeled) if statement
6127 which is not enclosed in braces and has an else clause. This is used to
6128 implement -Wparentheses. */
6129
6130 static void
6131 c_parser_statement_after_labels (c_parser *parser, bool *if_p,
6132 vec<tree> *chain)
6133 {
6134 location_t loc = c_parser_peek_token (parser)->location;
6135 tree stmt = NULL_TREE;
6136 bool in_if_block = parser->in_if_block;
6137 parser->in_if_block = false;
6138 if (if_p != NULL)
6139 *if_p = false;
6140
6141 if (c_parser_peek_token (parser)->type != CPP_OPEN_BRACE)
6142 add_debug_begin_stmt (loc);
6143
6144 switch (c_parser_peek_token (parser)->type)
6145 {
6146 case CPP_OPEN_BRACE:
6147 add_stmt (c_parser_compound_statement (parser));
6148 break;
6149 case CPP_KEYWORD:
6150 switch (c_parser_peek_token (parser)->keyword)
6151 {
6152 case RID_IF:
6153 c_parser_if_statement (parser, if_p, chain);
6154 break;
6155 case RID_SWITCH:
6156 c_parser_switch_statement (parser, if_p);
6157 break;
6158 case RID_WHILE:
6159 c_parser_while_statement (parser, false, 0, if_p);
6160 break;
6161 case RID_DO:
6162 c_parser_do_statement (parser, 0, false);
6163 break;
6164 case RID_FOR:
6165 c_parser_for_statement (parser, false, 0, if_p);
6166 break;
6167 case RID_GOTO:
6168 c_parser_consume_token (parser);
6169 if (c_parser_next_token_is (parser, CPP_NAME))
6170 {
6171 stmt = c_finish_goto_label (loc,
6172 c_parser_peek_token (parser)->value);
6173 c_parser_consume_token (parser);
6174 }
6175 else if (c_parser_next_token_is (parser, CPP_MULT))
6176 {
6177 struct c_expr val;
6178
6179 c_parser_consume_token (parser);
6180 val = c_parser_expression (parser);
6181 val = convert_lvalue_to_rvalue (loc, val, false, true);
6182 stmt = c_finish_goto_ptr (loc, val.value);
6183 }
6184 else
6185 c_parser_error (parser, "expected identifier or %<*%>");
6186 goto expect_semicolon;
6187 case RID_CONTINUE:
6188 c_parser_consume_token (parser);
6189 stmt = c_finish_bc_stmt (loc, &c_cont_label, false);
6190 goto expect_semicolon;
6191 case RID_BREAK:
6192 c_parser_consume_token (parser);
6193 stmt = c_finish_bc_stmt (loc, &c_break_label, true);
6194 goto expect_semicolon;
6195 case RID_RETURN:
6196 c_parser_consume_token (parser);
6197 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
6198 {
6199 stmt = c_finish_return (loc, NULL_TREE, NULL_TREE);
6200 c_parser_consume_token (parser);
6201 }
6202 else
6203 {
6204 location_t xloc = c_parser_peek_token (parser)->location;
6205 struct c_expr expr = c_parser_expression_conv (parser);
6206 mark_exp_read (expr.value);
6207 stmt = c_finish_return (EXPR_LOC_OR_LOC (expr.value, xloc),
6208 expr.value, expr.original_type);
6209 goto expect_semicolon;
6210 }
6211 break;
6212 case RID_ASM:
6213 stmt = c_parser_asm_statement (parser);
6214 break;
6215 case RID_TRANSACTION_ATOMIC:
6216 case RID_TRANSACTION_RELAXED:
6217 stmt = c_parser_transaction (parser,
6218 c_parser_peek_token (parser)->keyword);
6219 break;
6220 case RID_TRANSACTION_CANCEL:
6221 stmt = c_parser_transaction_cancel (parser);
6222 goto expect_semicolon;
6223 case RID_AT_THROW:
6224 gcc_assert (c_dialect_objc ());
6225 c_parser_consume_token (parser);
6226 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
6227 {
6228 stmt = objc_build_throw_stmt (loc, NULL_TREE);
6229 c_parser_consume_token (parser);
6230 }
6231 else
6232 {
6233 struct c_expr expr = c_parser_expression (parser);
6234 expr = convert_lvalue_to_rvalue (loc, expr, false, false);
6235 expr.value = c_fully_fold (expr.value, false, NULL);
6236 stmt = objc_build_throw_stmt (loc, expr.value);
6237 goto expect_semicolon;
6238 }
6239 break;
6240 case RID_AT_TRY:
6241 gcc_assert (c_dialect_objc ());
6242 c_parser_objc_try_catch_finally_statement (parser);
6243 break;
6244 case RID_AT_SYNCHRONIZED:
6245 gcc_assert (c_dialect_objc ());
6246 c_parser_objc_synchronized_statement (parser);
6247 break;
6248 case RID_ATTRIBUTE:
6249 {
6250 /* Allow '__attribute__((fallthrough));'. */
6251 tree attrs = c_parser_gnu_attributes (parser);
6252 if (attribute_fallthrough_p (attrs))
6253 {
6254 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
6255 {
6256 tree fn = build_call_expr_internal_loc (loc,
6257 IFN_FALLTHROUGH,
6258 void_type_node, 0);
6259 add_stmt (fn);
6260 /* Eat the ';'. */
6261 c_parser_consume_token (parser);
6262 }
6263 else
6264 warning_at (loc, OPT_Wattributes,
6265 "%<fallthrough%> attribute not followed "
6266 "by %<;%>");
6267 }
6268 else if (attrs != NULL_TREE)
6269 warning_at (loc, OPT_Wattributes, "only attribute %<fallthrough%>"
6270 " can be applied to a null statement");
6271 break;
6272 }
6273 default:
6274 goto expr_stmt;
6275 }
6276 break;
6277 case CPP_SEMICOLON:
6278 c_parser_consume_token (parser);
6279 break;
6280 case CPP_CLOSE_PAREN:
6281 case CPP_CLOSE_SQUARE:
6282 /* Avoid infinite loop in error recovery:
6283 c_parser_skip_until_found stops at a closing nesting
6284 delimiter without consuming it, but here we need to consume
6285 it to proceed further. */
6286 c_parser_error (parser, "expected statement");
6287 c_parser_consume_token (parser);
6288 break;
6289 case CPP_PRAGMA:
6290 c_parser_pragma (parser, pragma_stmt, if_p);
6291 break;
6292 default:
6293 expr_stmt:
6294 stmt = c_finish_expr_stmt (loc, c_parser_expression_conv (parser).value);
6295 expect_semicolon:
6296 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
6297 break;
6298 }
6299 /* Two cases cannot and do not have line numbers associated: If stmt
6300 is degenerate, such as "2;", then stmt is an INTEGER_CST, which
6301 cannot hold line numbers. But that's OK because the statement
6302 will either be changed to a MODIFY_EXPR during gimplification of
6303 the statement expr, or discarded. If stmt was compound, but
6304 without new variables, we will have skipped the creation of a
6305 BIND and will have a bare STATEMENT_LIST. But that's OK because
6306 (recursively) all of the component statements should already have
6307 line numbers assigned. ??? Can we discard no-op statements
6308 earlier? */
6309 if (EXPR_LOCATION (stmt) == UNKNOWN_LOCATION)
6310 protected_set_expr_location (stmt, loc);
6311
6312 parser->in_if_block = in_if_block;
6313 }
6314
6315 /* Parse the condition from an if, do, while or for statements. */
6316
6317 static tree
6318 c_parser_condition (c_parser *parser)
6319 {
6320 location_t loc = c_parser_peek_token (parser)->location;
6321 tree cond;
6322 cond = c_parser_expression_conv (parser).value;
6323 cond = c_objc_common_truthvalue_conversion (loc, cond);
6324 cond = c_fully_fold (cond, false, NULL);
6325 if (warn_sequence_point)
6326 verify_sequence_points (cond);
6327 return cond;
6328 }
6329
6330 /* Parse a parenthesized condition from an if, do or while statement.
6331
6332 condition:
6333 ( expression )
6334 */
6335 static tree
6336 c_parser_paren_condition (c_parser *parser)
6337 {
6338 tree cond;
6339 matching_parens parens;
6340 if (!parens.require_open (parser))
6341 return error_mark_node;
6342 cond = c_parser_condition (parser);
6343 parens.skip_until_found_close (parser);
6344 return cond;
6345 }
6346
6347 /* Parse a statement which is a block in C99.
6348
6349 IF_P is used to track whether there's a (possibly labeled) if statement
6350 which is not enclosed in braces and has an else clause. This is used to
6351 implement -Wparentheses. */
6352
6353 static tree
6354 c_parser_c99_block_statement (c_parser *parser, bool *if_p,
6355 location_t *loc_after_labels)
6356 {
6357 tree block = c_begin_compound_stmt (flag_isoc99);
6358 location_t loc = c_parser_peek_token (parser)->location;
6359 c_parser_statement (parser, if_p, loc_after_labels);
6360 return c_end_compound_stmt (loc, block, flag_isoc99);
6361 }
6362
6363 /* Parse the body of an if statement. This is just parsing a
6364 statement but (a) it is a block in C99, (b) we track whether the
6365 body is an if statement for the sake of -Wparentheses warnings, (c)
6366 we handle an empty body specially for the sake of -Wempty-body
6367 warnings, and (d) we call parser_compound_statement directly
6368 because c_parser_statement_after_labels resets
6369 parser->in_if_block.
6370
6371 IF_P is used to track whether there's a (possibly labeled) if statement
6372 which is not enclosed in braces and has an else clause. This is used to
6373 implement -Wparentheses. */
6374
6375 static tree
6376 c_parser_if_body (c_parser *parser, bool *if_p,
6377 const token_indent_info &if_tinfo)
6378 {
6379 tree block = c_begin_compound_stmt (flag_isoc99);
6380 location_t body_loc = c_parser_peek_token (parser)->location;
6381 location_t body_loc_after_labels = UNKNOWN_LOCATION;
6382 token_indent_info body_tinfo
6383 = get_token_indent_info (c_parser_peek_token (parser));
6384
6385 c_parser_all_labels (parser);
6386 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
6387 {
6388 location_t loc = c_parser_peek_token (parser)->location;
6389 add_stmt (build_empty_stmt (loc));
6390 c_parser_consume_token (parser);
6391 if (!c_parser_next_token_is_keyword (parser, RID_ELSE))
6392 warning_at (loc, OPT_Wempty_body,
6393 "suggest braces around empty body in an %<if%> statement");
6394 }
6395 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
6396 add_stmt (c_parser_compound_statement (parser));
6397 else
6398 {
6399 body_loc_after_labels = c_parser_peek_token (parser)->location;
6400 c_parser_statement_after_labels (parser, if_p);
6401 }
6402
6403 token_indent_info next_tinfo
6404 = get_token_indent_info (c_parser_peek_token (parser));
6405 warn_for_misleading_indentation (if_tinfo, body_tinfo, next_tinfo);
6406 if (body_loc_after_labels != UNKNOWN_LOCATION
6407 && next_tinfo.type != CPP_SEMICOLON)
6408 warn_for_multistatement_macros (body_loc_after_labels, next_tinfo.location,
6409 if_tinfo.location, RID_IF);
6410
6411 return c_end_compound_stmt (body_loc, block, flag_isoc99);
6412 }
6413
6414 /* Parse the else body of an if statement. This is just parsing a
6415 statement but (a) it is a block in C99, (b) we handle an empty body
6416 specially for the sake of -Wempty-body warnings. CHAIN is a vector
6417 of if-else-if conditions. */
6418
6419 static tree
6420 c_parser_else_body (c_parser *parser, const token_indent_info &else_tinfo,
6421 vec<tree> *chain)
6422 {
6423 location_t body_loc = c_parser_peek_token (parser)->location;
6424 tree block = c_begin_compound_stmt (flag_isoc99);
6425 token_indent_info body_tinfo
6426 = get_token_indent_info (c_parser_peek_token (parser));
6427 location_t body_loc_after_labels = UNKNOWN_LOCATION;
6428
6429 c_parser_all_labels (parser);
6430 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
6431 {
6432 location_t loc = c_parser_peek_token (parser)->location;
6433 warning_at (loc,
6434 OPT_Wempty_body,
6435 "suggest braces around empty body in an %<else%> statement");
6436 add_stmt (build_empty_stmt (loc));
6437 c_parser_consume_token (parser);
6438 }
6439 else
6440 {
6441 if (!c_parser_next_token_is (parser, CPP_OPEN_BRACE))
6442 body_loc_after_labels = c_parser_peek_token (parser)->location;
6443 c_parser_statement_after_labels (parser, NULL, chain);
6444 }
6445
6446 token_indent_info next_tinfo
6447 = get_token_indent_info (c_parser_peek_token (parser));
6448 warn_for_misleading_indentation (else_tinfo, body_tinfo, next_tinfo);
6449 if (body_loc_after_labels != UNKNOWN_LOCATION
6450 && next_tinfo.type != CPP_SEMICOLON)
6451 warn_for_multistatement_macros (body_loc_after_labels, next_tinfo.location,
6452 else_tinfo.location, RID_ELSE);
6453
6454 return c_end_compound_stmt (body_loc, block, flag_isoc99);
6455 }
6456
6457 /* We might need to reclassify any previously-lexed identifier, e.g.
6458 when we've left a for loop with an if-statement without else in the
6459 body - we might have used a wrong scope for the token. See PR67784. */
6460
6461 static void
6462 c_parser_maybe_reclassify_token (c_parser *parser)
6463 {
6464 if (c_parser_next_token_is (parser, CPP_NAME))
6465 {
6466 c_token *token = c_parser_peek_token (parser);
6467
6468 if (token->id_kind != C_ID_CLASSNAME)
6469 {
6470 tree decl = lookup_name (token->value);
6471
6472 token->id_kind = C_ID_ID;
6473 if (decl)
6474 {
6475 if (TREE_CODE (decl) == TYPE_DECL)
6476 token->id_kind = C_ID_TYPENAME;
6477 }
6478 else if (c_dialect_objc ())
6479 {
6480 tree objc_interface_decl = objc_is_class_name (token->value);
6481 /* Objective-C class names are in the same namespace as
6482 variables and typedefs, and hence are shadowed by local
6483 declarations. */
6484 if (objc_interface_decl)
6485 {
6486 token->value = objc_interface_decl;
6487 token->id_kind = C_ID_CLASSNAME;
6488 }
6489 }
6490 }
6491 }
6492 }
6493
6494 /* Parse an if statement (C90 6.6.4, C99 6.8.4, C11 6.8.4).
6495
6496 if-statement:
6497 if ( expression ) statement
6498 if ( expression ) statement else statement
6499
6500 CHAIN is a vector of if-else-if conditions.
6501 IF_P is used to track whether there's a (possibly labeled) if statement
6502 which is not enclosed in braces and has an else clause. This is used to
6503 implement -Wparentheses. */
6504
6505 static void
6506 c_parser_if_statement (c_parser *parser, bool *if_p, vec<tree> *chain)
6507 {
6508 tree block;
6509 location_t loc;
6510 tree cond;
6511 bool nested_if = false;
6512 tree first_body, second_body;
6513 bool in_if_block;
6514
6515 gcc_assert (c_parser_next_token_is_keyword (parser, RID_IF));
6516 token_indent_info if_tinfo
6517 = get_token_indent_info (c_parser_peek_token (parser));
6518 c_parser_consume_token (parser);
6519 block = c_begin_compound_stmt (flag_isoc99);
6520 loc = c_parser_peek_token (parser)->location;
6521 cond = c_parser_paren_condition (parser);
6522 in_if_block = parser->in_if_block;
6523 parser->in_if_block = true;
6524 first_body = c_parser_if_body (parser, &nested_if, if_tinfo);
6525 parser->in_if_block = in_if_block;
6526
6527 if (warn_duplicated_cond)
6528 warn_duplicated_cond_add_or_warn (EXPR_LOCATION (cond), cond, &chain);
6529
6530 if (c_parser_next_token_is_keyword (parser, RID_ELSE))
6531 {
6532 token_indent_info else_tinfo
6533 = get_token_indent_info (c_parser_peek_token (parser));
6534 c_parser_consume_token (parser);
6535 if (warn_duplicated_cond)
6536 {
6537 if (c_parser_next_token_is_keyword (parser, RID_IF)
6538 && chain == NULL)
6539 {
6540 /* We've got "if (COND) else if (COND2)". Start the
6541 condition chain and add COND as the first element. */
6542 chain = new vec<tree> ();
6543 if (!CONSTANT_CLASS_P (cond) && !TREE_SIDE_EFFECTS (cond))
6544 chain->safe_push (cond);
6545 }
6546 else if (!c_parser_next_token_is_keyword (parser, RID_IF))
6547 {
6548 /* This is if-else without subsequent if. Zap the condition
6549 chain; we would have already warned at this point. */
6550 delete chain;
6551 chain = NULL;
6552 }
6553 }
6554 second_body = c_parser_else_body (parser, else_tinfo, chain);
6555 /* Set IF_P to true to indicate that this if statement has an
6556 else clause. This may trigger the Wparentheses warning
6557 below when we get back up to the parent if statement. */
6558 if (if_p != NULL)
6559 *if_p = true;
6560 }
6561 else
6562 {
6563 second_body = NULL_TREE;
6564
6565 /* Diagnose an ambiguous else if if-then-else is nested inside
6566 if-then. */
6567 if (nested_if)
6568 warning_at (loc, OPT_Wdangling_else,
6569 "suggest explicit braces to avoid ambiguous %<else%>");
6570
6571 if (warn_duplicated_cond)
6572 {
6573 /* This if statement does not have an else clause. We don't
6574 need the condition chain anymore. */
6575 delete chain;
6576 chain = NULL;
6577 }
6578 }
6579 c_finish_if_stmt (loc, cond, first_body, second_body);
6580 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
6581
6582 c_parser_maybe_reclassify_token (parser);
6583 }
6584
6585 /* Parse a switch statement (C90 6.6.4, C99 6.8.4, C11 6.8.4).
6586
6587 switch-statement:
6588 switch (expression) statement
6589 */
6590
6591 static void
6592 c_parser_switch_statement (c_parser *parser, bool *if_p)
6593 {
6594 struct c_expr ce;
6595 tree block, expr, body, save_break;
6596 location_t switch_loc = c_parser_peek_token (parser)->location;
6597 location_t switch_cond_loc;
6598 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SWITCH));
6599 c_parser_consume_token (parser);
6600 block = c_begin_compound_stmt (flag_isoc99);
6601 bool explicit_cast_p = false;
6602 matching_parens parens;
6603 if (parens.require_open (parser))
6604 {
6605 switch_cond_loc = c_parser_peek_token (parser)->location;
6606 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
6607 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
6608 explicit_cast_p = true;
6609 ce = c_parser_expression (parser);
6610 ce = convert_lvalue_to_rvalue (switch_cond_loc, ce, true, false);
6611 expr = ce.value;
6612 /* ??? expr has no valid location? */
6613 parens.skip_until_found_close (parser);
6614 }
6615 else
6616 {
6617 switch_cond_loc = UNKNOWN_LOCATION;
6618 expr = error_mark_node;
6619 ce.original_type = error_mark_node;
6620 }
6621 c_start_case (switch_loc, switch_cond_loc, expr, explicit_cast_p);
6622 save_break = c_break_label;
6623 c_break_label = NULL_TREE;
6624 location_t loc_after_labels;
6625 bool open_brace_p = c_parser_peek_token (parser)->type == CPP_OPEN_BRACE;
6626 body = c_parser_c99_block_statement (parser, if_p, &loc_after_labels);
6627 location_t next_loc = c_parser_peek_token (parser)->location;
6628 if (!open_brace_p && c_parser_peek_token (parser)->type != CPP_SEMICOLON)
6629 warn_for_multistatement_macros (loc_after_labels, next_loc, switch_loc,
6630 RID_SWITCH);
6631 if (c_break_label)
6632 {
6633 location_t here = c_parser_peek_token (parser)->location;
6634 tree t = build1 (LABEL_EXPR, void_type_node, c_break_label);
6635 SET_EXPR_LOCATION (t, here);
6636 SWITCH_BREAK_LABEL_P (c_break_label) = 1;
6637 append_to_statement_list_force (t, &body);
6638 }
6639 c_finish_case (body, ce.original_type);
6640 c_break_label = save_break;
6641 add_stmt (c_end_compound_stmt (switch_loc, block, flag_isoc99));
6642 c_parser_maybe_reclassify_token (parser);
6643 }
6644
6645 /* Parse a while statement (C90 6.6.5, C99 6.8.5, C11 6.8.5).
6646
6647 while-statement:
6648 while (expression) statement
6649
6650 IF_P is used to track whether there's a (possibly labeled) if statement
6651 which is not enclosed in braces and has an else clause. This is used to
6652 implement -Wparentheses. */
6653
6654 static void
6655 c_parser_while_statement (c_parser *parser, bool ivdep, unsigned short unroll,
6656 bool *if_p)
6657 {
6658 tree block, cond, body, save_break, save_cont;
6659 location_t loc;
6660 gcc_assert (c_parser_next_token_is_keyword (parser, RID_WHILE));
6661 token_indent_info while_tinfo
6662 = get_token_indent_info (c_parser_peek_token (parser));
6663 c_parser_consume_token (parser);
6664 block = c_begin_compound_stmt (flag_isoc99);
6665 loc = c_parser_peek_token (parser)->location;
6666 cond = c_parser_paren_condition (parser);
6667 if (ivdep && cond != error_mark_node)
6668 cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
6669 build_int_cst (integer_type_node,
6670 annot_expr_ivdep_kind),
6671 integer_zero_node);
6672 if (unroll && cond != error_mark_node)
6673 cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
6674 build_int_cst (integer_type_node,
6675 annot_expr_unroll_kind),
6676 build_int_cst (integer_type_node, unroll));
6677 save_break = c_break_label;
6678 c_break_label = NULL_TREE;
6679 save_cont = c_cont_label;
6680 c_cont_label = NULL_TREE;
6681
6682 token_indent_info body_tinfo
6683 = get_token_indent_info (c_parser_peek_token (parser));
6684
6685 location_t loc_after_labels;
6686 bool open_brace = c_parser_next_token_is (parser, CPP_OPEN_BRACE);
6687 body = c_parser_c99_block_statement (parser, if_p, &loc_after_labels);
6688 c_finish_loop (loc, loc, cond, UNKNOWN_LOCATION, NULL, body,
6689 c_break_label, c_cont_label, true);
6690 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
6691 c_parser_maybe_reclassify_token (parser);
6692
6693 token_indent_info next_tinfo
6694 = get_token_indent_info (c_parser_peek_token (parser));
6695 warn_for_misleading_indentation (while_tinfo, body_tinfo, next_tinfo);
6696
6697 if (next_tinfo.type != CPP_SEMICOLON && !open_brace)
6698 warn_for_multistatement_macros (loc_after_labels, next_tinfo.location,
6699 while_tinfo.location, RID_WHILE);
6700
6701 c_break_label = save_break;
6702 c_cont_label = save_cont;
6703 }
6704
6705 /* Parse a do statement (C90 6.6.5, C99 6.8.5, C11 6.8.5).
6706
6707 do-statement:
6708 do statement while ( expression ) ;
6709 */
6710
6711 static void
6712 c_parser_do_statement (c_parser *parser, bool ivdep, unsigned short unroll)
6713 {
6714 tree block, cond, body, save_break, save_cont, new_break, new_cont;
6715 location_t loc;
6716 gcc_assert (c_parser_next_token_is_keyword (parser, RID_DO));
6717 c_parser_consume_token (parser);
6718 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
6719 warning_at (c_parser_peek_token (parser)->location,
6720 OPT_Wempty_body,
6721 "suggest braces around empty body in %<do%> statement");
6722 block = c_begin_compound_stmt (flag_isoc99);
6723 loc = c_parser_peek_token (parser)->location;
6724 save_break = c_break_label;
6725 c_break_label = NULL_TREE;
6726 save_cont = c_cont_label;
6727 c_cont_label = NULL_TREE;
6728 body = c_parser_c99_block_statement (parser, NULL);
6729 c_parser_require_keyword (parser, RID_WHILE, "expected %<while%>");
6730 new_break = c_break_label;
6731 c_break_label = save_break;
6732 new_cont = c_cont_label;
6733 c_cont_label = save_cont;
6734 location_t cond_loc = c_parser_peek_token (parser)->location;
6735 cond = c_parser_paren_condition (parser);
6736 if (ivdep && cond != error_mark_node)
6737 cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
6738 build_int_cst (integer_type_node,
6739 annot_expr_ivdep_kind),
6740 integer_zero_node);
6741 if (unroll && cond != error_mark_node)
6742 cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
6743 build_int_cst (integer_type_node,
6744 annot_expr_unroll_kind),
6745 build_int_cst (integer_type_node, unroll));
6746 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
6747 c_parser_skip_to_end_of_block_or_statement (parser);
6748 c_finish_loop (loc, cond_loc, cond, UNKNOWN_LOCATION, NULL, body,
6749 new_break, new_cont, false);
6750 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
6751 }
6752
6753 /* Parse a for statement (C90 6.6.5, C99 6.8.5, C11 6.8.5).
6754
6755 for-statement:
6756 for ( expression[opt] ; expression[opt] ; expression[opt] ) statement
6757 for ( nested-declaration expression[opt] ; expression[opt] ) statement
6758
6759 The form with a declaration is new in C99.
6760
6761 ??? In accordance with the old parser, the declaration may be a
6762 nested function, which is then rejected in check_for_loop_decls,
6763 but does it make any sense for this to be included in the grammar?
6764 Note in particular that the nested function does not include a
6765 trailing ';', whereas the "declaration" production includes one.
6766 Also, can we reject bad declarations earlier and cheaper than
6767 check_for_loop_decls?
6768
6769 In Objective-C, there are two additional variants:
6770
6771 foreach-statement:
6772 for ( expression in expresssion ) statement
6773 for ( declaration in expression ) statement
6774
6775 This is inconsistent with C, because the second variant is allowed
6776 even if c99 is not enabled.
6777
6778 The rest of the comment documents these Objective-C foreach-statement.
6779
6780 Here is the canonical example of the first variant:
6781 for (object in array) { do something with object }
6782 we call the first expression ("object") the "object_expression" and
6783 the second expression ("array") the "collection_expression".
6784 object_expression must be an lvalue of type "id" (a generic Objective-C
6785 object) because the loop works by assigning to object_expression the
6786 various objects from the collection_expression. collection_expression
6787 must evaluate to something of type "id" which responds to the method
6788 countByEnumeratingWithState:objects:count:.
6789
6790 The canonical example of the second variant is:
6791 for (id object in array) { do something with object }
6792 which is completely equivalent to
6793 {
6794 id object;
6795 for (object in array) { do something with object }
6796 }
6797 Note that initizializing 'object' in some way (eg, "for ((object =
6798 xxx) in array) { do something with object }") is possibly
6799 technically valid, but completely pointless as 'object' will be
6800 assigned to something else as soon as the loop starts. We should
6801 most likely reject it (TODO).
6802
6803 The beginning of the Objective-C foreach-statement looks exactly
6804 like the beginning of the for-statement, and we can tell it is a
6805 foreach-statement only because the initial declaration or
6806 expression is terminated by 'in' instead of ';'.
6807
6808 IF_P is used to track whether there's a (possibly labeled) if statement
6809 which is not enclosed in braces and has an else clause. This is used to
6810 implement -Wparentheses. */
6811
6812 static void
6813 c_parser_for_statement (c_parser *parser, bool ivdep, unsigned short unroll,
6814 bool *if_p)
6815 {
6816 tree block, cond, incr, save_break, save_cont, body;
6817 /* The following are only used when parsing an ObjC foreach statement. */
6818 tree object_expression;
6819 /* Silence the bogus uninitialized warning. */
6820 tree collection_expression = NULL;
6821 location_t loc = c_parser_peek_token (parser)->location;
6822 location_t for_loc = loc;
6823 location_t cond_loc = UNKNOWN_LOCATION;
6824 location_t incr_loc = UNKNOWN_LOCATION;
6825 bool is_foreach_statement = false;
6826 gcc_assert (c_parser_next_token_is_keyword (parser, RID_FOR));
6827 token_indent_info for_tinfo
6828 = get_token_indent_info (c_parser_peek_token (parser));
6829 c_parser_consume_token (parser);
6830 /* Open a compound statement in Objective-C as well, just in case this is
6831 as foreach expression. */
6832 block = c_begin_compound_stmt (flag_isoc99 || c_dialect_objc ());
6833 cond = error_mark_node;
6834 incr = error_mark_node;
6835 matching_parens parens;
6836 if (parens.require_open (parser))
6837 {
6838 /* Parse the initialization declaration or expression. */
6839 object_expression = error_mark_node;
6840 parser->objc_could_be_foreach_context = c_dialect_objc ();
6841 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
6842 {
6843 parser->objc_could_be_foreach_context = false;
6844 c_parser_consume_token (parser);
6845 c_finish_expr_stmt (loc, NULL_TREE);
6846 }
6847 else if (c_parser_next_tokens_start_declaration (parser)
6848 || c_parser_nth_token_starts_std_attributes (parser, 1))
6849 {
6850 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
6851 &object_expression, vNULL);
6852 parser->objc_could_be_foreach_context = false;
6853
6854 if (c_parser_next_token_is_keyword (parser, RID_IN))
6855 {
6856 c_parser_consume_token (parser);
6857 is_foreach_statement = true;
6858 if (check_for_loop_decls (for_loc, true) == NULL_TREE)
6859 c_parser_error (parser, "multiple iterating variables in "
6860 "fast enumeration");
6861 }
6862 else
6863 check_for_loop_decls (for_loc, flag_isoc99);
6864 }
6865 else if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
6866 {
6867 /* __extension__ can start a declaration, but is also an
6868 unary operator that can start an expression. Consume all
6869 but the last of a possible series of __extension__ to
6870 determine which. */
6871 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
6872 && (c_parser_peek_2nd_token (parser)->keyword
6873 == RID_EXTENSION))
6874 c_parser_consume_token (parser);
6875 if (c_token_starts_declaration (c_parser_peek_2nd_token (parser))
6876 || c_parser_nth_token_starts_std_attributes (parser, 2))
6877 {
6878 int ext;
6879 ext = disable_extension_diagnostics ();
6880 c_parser_consume_token (parser);
6881 c_parser_declaration_or_fndef (parser, true, true, true, true,
6882 true, &object_expression, vNULL);
6883 parser->objc_could_be_foreach_context = false;
6884
6885 restore_extension_diagnostics (ext);
6886 if (c_parser_next_token_is_keyword (parser, RID_IN))
6887 {
6888 c_parser_consume_token (parser);
6889 is_foreach_statement = true;
6890 if (check_for_loop_decls (for_loc, true) == NULL_TREE)
6891 c_parser_error (parser, "multiple iterating variables in "
6892 "fast enumeration");
6893 }
6894 else
6895 check_for_loop_decls (for_loc, flag_isoc99);
6896 }
6897 else
6898 goto init_expr;
6899 }
6900 else
6901 {
6902 init_expr:
6903 {
6904 struct c_expr ce;
6905 tree init_expression;
6906 ce = c_parser_expression (parser);
6907 init_expression = ce.value;
6908 parser->objc_could_be_foreach_context = false;
6909 if (c_parser_next_token_is_keyword (parser, RID_IN))
6910 {
6911 c_parser_consume_token (parser);
6912 is_foreach_statement = true;
6913 if (! lvalue_p (init_expression))
6914 c_parser_error (parser, "invalid iterating variable in "
6915 "fast enumeration");
6916 object_expression
6917 = c_fully_fold (init_expression, false, NULL);
6918 }
6919 else
6920 {
6921 ce = convert_lvalue_to_rvalue (loc, ce, true, false);
6922 init_expression = ce.value;
6923 c_finish_expr_stmt (loc, init_expression);
6924 c_parser_skip_until_found (parser, CPP_SEMICOLON,
6925 "expected %<;%>");
6926 }
6927 }
6928 }
6929 /* Parse the loop condition. In the case of a foreach
6930 statement, there is no loop condition. */
6931 gcc_assert (!parser->objc_could_be_foreach_context);
6932 if (!is_foreach_statement)
6933 {
6934 cond_loc = c_parser_peek_token (parser)->location;
6935 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
6936 {
6937 if (ivdep)
6938 {
6939 c_parser_error (parser, "missing loop condition in loop "
6940 "with %<GCC ivdep%> pragma");
6941 cond = error_mark_node;
6942 }
6943 else if (unroll)
6944 {
6945 c_parser_error (parser, "missing loop condition in loop "
6946 "with %<GCC unroll%> pragma");
6947 cond = error_mark_node;
6948 }
6949 else
6950 {
6951 c_parser_consume_token (parser);
6952 cond = NULL_TREE;
6953 }
6954 }
6955 else
6956 {
6957 cond = c_parser_condition (parser);
6958 c_parser_skip_until_found (parser, CPP_SEMICOLON,
6959 "expected %<;%>");
6960 }
6961 if (ivdep && cond != error_mark_node)
6962 cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
6963 build_int_cst (integer_type_node,
6964 annot_expr_ivdep_kind),
6965 integer_zero_node);
6966 if (unroll && cond != error_mark_node)
6967 cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
6968 build_int_cst (integer_type_node,
6969 annot_expr_unroll_kind),
6970 build_int_cst (integer_type_node, unroll));
6971 }
6972 /* Parse the increment expression (the third expression in a
6973 for-statement). In the case of a foreach-statement, this is
6974 the expression that follows the 'in'. */
6975 loc = incr_loc = c_parser_peek_token (parser)->location;
6976 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
6977 {
6978 if (is_foreach_statement)
6979 {
6980 c_parser_error (parser,
6981 "missing collection in fast enumeration");
6982 collection_expression = error_mark_node;
6983 }
6984 else
6985 incr = c_process_expr_stmt (loc, NULL_TREE);
6986 }
6987 else
6988 {
6989 if (is_foreach_statement)
6990 collection_expression
6991 = c_fully_fold (c_parser_expression (parser).value, false, NULL);
6992 else
6993 {
6994 struct c_expr ce = c_parser_expression (parser);
6995 ce = convert_lvalue_to_rvalue (loc, ce, true, false);
6996 incr = c_process_expr_stmt (loc, ce.value);
6997 }
6998 }
6999 parens.skip_until_found_close (parser);
7000 }
7001 save_break = c_break_label;
7002 c_break_label = NULL_TREE;
7003 save_cont = c_cont_label;
7004 c_cont_label = NULL_TREE;
7005
7006 token_indent_info body_tinfo
7007 = get_token_indent_info (c_parser_peek_token (parser));
7008
7009 location_t loc_after_labels;
7010 bool open_brace = c_parser_next_token_is (parser, CPP_OPEN_BRACE);
7011 body = c_parser_c99_block_statement (parser, if_p, &loc_after_labels);
7012
7013 if (is_foreach_statement)
7014 objc_finish_foreach_loop (for_loc, object_expression,
7015 collection_expression, body, c_break_label,
7016 c_cont_label);
7017 else
7018 c_finish_loop (for_loc, cond_loc, cond, incr_loc, incr, body,
7019 c_break_label, c_cont_label, true);
7020 add_stmt (c_end_compound_stmt (for_loc, block,
7021 flag_isoc99 || c_dialect_objc ()));
7022 c_parser_maybe_reclassify_token (parser);
7023
7024 token_indent_info next_tinfo
7025 = get_token_indent_info (c_parser_peek_token (parser));
7026 warn_for_misleading_indentation (for_tinfo, body_tinfo, next_tinfo);
7027
7028 if (next_tinfo.type != CPP_SEMICOLON && !open_brace)
7029 warn_for_multistatement_macros (loc_after_labels, next_tinfo.location,
7030 for_tinfo.location, RID_FOR);
7031
7032 c_break_label = save_break;
7033 c_cont_label = save_cont;
7034 }
7035
7036 /* Parse an asm statement, a GNU extension. This is a full-blown asm
7037 statement with inputs, outputs, clobbers, and volatile, inline, and goto
7038 tags allowed.
7039
7040 asm-qualifier:
7041 volatile
7042 inline
7043 goto
7044
7045 asm-qualifier-list:
7046 asm-qualifier-list asm-qualifier
7047 asm-qualifier
7048
7049 asm-statement:
7050 asm asm-qualifier-list[opt] ( asm-argument ) ;
7051
7052 asm-argument:
7053 asm-string-literal
7054 asm-string-literal : asm-operands[opt]
7055 asm-string-literal : asm-operands[opt] : asm-operands[opt]
7056 asm-string-literal : asm-operands[opt] : asm-operands[opt] \
7057 : asm-clobbers[opt]
7058 asm-string-literal : : asm-operands[opt] : asm-clobbers[opt] \
7059 : asm-goto-operands
7060
7061 The form with asm-goto-operands is valid if and only if the
7062 asm-qualifier-list contains goto, and is the only allowed form in that case.
7063 Duplicate asm-qualifiers are not allowed.
7064
7065 The :: token is considered equivalent to two consecutive : tokens. */
7066
7067 static tree
7068 c_parser_asm_statement (c_parser *parser)
7069 {
7070 tree str, outputs, inputs, clobbers, labels, ret;
7071 bool simple;
7072 location_t asm_loc = c_parser_peek_token (parser)->location;
7073 int section, nsections;
7074
7075 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
7076 c_parser_consume_token (parser);
7077
7078 /* Handle the asm-qualifier-list. */
7079 location_t volatile_loc = UNKNOWN_LOCATION;
7080 location_t inline_loc = UNKNOWN_LOCATION;
7081 location_t goto_loc = UNKNOWN_LOCATION;
7082 for (;;)
7083 {
7084 c_token *token = c_parser_peek_token (parser);
7085 location_t loc = token->location;
7086 switch (token->keyword)
7087 {
7088 case RID_VOLATILE:
7089 if (volatile_loc)
7090 {
7091 error_at (loc, "duplicate %<asm%> qualifier %qE", token->value);
7092 inform (volatile_loc, "first seen here");
7093 }
7094 else
7095 volatile_loc = loc;
7096 c_parser_consume_token (parser);
7097 continue;
7098
7099 case RID_INLINE:
7100 if (inline_loc)
7101 {
7102 error_at (loc, "duplicate %<asm%> qualifier %qE", token->value);
7103 inform (inline_loc, "first seen here");
7104 }
7105 else
7106 inline_loc = loc;
7107 c_parser_consume_token (parser);
7108 continue;
7109
7110 case RID_GOTO:
7111 if (goto_loc)
7112 {
7113 error_at (loc, "duplicate %<asm%> qualifier %qE", token->value);
7114 inform (goto_loc, "first seen here");
7115 }
7116 else
7117 goto_loc = loc;
7118 c_parser_consume_token (parser);
7119 continue;
7120
7121 case RID_CONST:
7122 case RID_RESTRICT:
7123 error_at (loc, "%qE is not a valid %<asm%> qualifier", token->value);
7124 c_parser_consume_token (parser);
7125 continue;
7126
7127 default:
7128 break;
7129 }
7130 break;
7131 }
7132
7133 bool is_volatile = (volatile_loc != UNKNOWN_LOCATION);
7134 bool is_inline = (inline_loc != UNKNOWN_LOCATION);
7135 bool is_goto = (goto_loc != UNKNOWN_LOCATION);
7136
7137 ret = NULL;
7138
7139 matching_parens parens;
7140 if (!parens.require_open (parser))
7141 goto error;
7142
7143 str = c_parser_asm_string_literal (parser);
7144 if (str == NULL_TREE)
7145 goto error_close_paren;
7146
7147 simple = true;
7148 outputs = NULL_TREE;
7149 inputs = NULL_TREE;
7150 clobbers = NULL_TREE;
7151 labels = NULL_TREE;
7152
7153 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
7154 goto done_asm;
7155
7156 /* Parse each colon-delimited section of operands. */
7157 nsections = 3 + is_goto;
7158 for (section = 0; section < nsections; ++section)
7159 {
7160 if (c_parser_next_token_is (parser, CPP_SCOPE))
7161 {
7162 ++section;
7163 if (section == nsections)
7164 {
7165 c_parser_error (parser, "expected %<)%>");
7166 goto error_close_paren;
7167 }
7168 c_parser_consume_token (parser);
7169 }
7170 else if (!c_parser_require (parser, CPP_COLON,
7171 is_goto
7172 ? G_("expected %<:%>")
7173 : G_("expected %<:%> or %<)%>"),
7174 UNKNOWN_LOCATION, is_goto))
7175 goto error_close_paren;
7176
7177 /* Once past any colon, we're no longer a simple asm. */
7178 simple = false;
7179
7180 if ((!c_parser_next_token_is (parser, CPP_COLON)
7181 && !c_parser_next_token_is (parser, CPP_SCOPE)
7182 && !c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
7183 || section == 3)
7184 switch (section)
7185 {
7186 case 0:
7187 /* For asm goto, we don't allow output operands, but reserve
7188 the slot for a future extension that does allow them. */
7189 if (!is_goto)
7190 outputs = c_parser_asm_operands (parser);
7191 break;
7192 case 1:
7193 inputs = c_parser_asm_operands (parser);
7194 break;
7195 case 2:
7196 clobbers = c_parser_asm_clobbers (parser);
7197 break;
7198 case 3:
7199 labels = c_parser_asm_goto_operands (parser);
7200 break;
7201 default:
7202 gcc_unreachable ();
7203 }
7204
7205 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
7206 goto done_asm;
7207 }
7208
7209 done_asm:
7210 if (!parens.require_close (parser))
7211 {
7212 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7213 goto error;
7214 }
7215
7216 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
7217 c_parser_skip_to_end_of_block_or_statement (parser);
7218
7219 ret = build_asm_stmt (is_volatile,
7220 build_asm_expr (asm_loc, str, outputs, inputs,
7221 clobbers, labels, simple, is_inline));
7222
7223 error:
7224 return ret;
7225
7226 error_close_paren:
7227 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7228 goto error;
7229 }
7230
7231 /* Parse asm operands, a GNU extension.
7232
7233 asm-operands:
7234 asm-operand
7235 asm-operands , asm-operand
7236
7237 asm-operand:
7238 asm-string-literal ( expression )
7239 [ identifier ] asm-string-literal ( expression )
7240 */
7241
7242 static tree
7243 c_parser_asm_operands (c_parser *parser)
7244 {
7245 tree list = NULL_TREE;
7246 while (true)
7247 {
7248 tree name, str;
7249 struct c_expr expr;
7250 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
7251 {
7252 c_parser_consume_token (parser);
7253 if (c_parser_next_token_is (parser, CPP_NAME))
7254 {
7255 tree id = c_parser_peek_token (parser)->value;
7256 c_parser_consume_token (parser);
7257 name = build_string (IDENTIFIER_LENGTH (id),
7258 IDENTIFIER_POINTER (id));
7259 }
7260 else
7261 {
7262 c_parser_error (parser, "expected identifier");
7263 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
7264 return NULL_TREE;
7265 }
7266 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
7267 "expected %<]%>");
7268 }
7269 else
7270 name = NULL_TREE;
7271 str = c_parser_asm_string_literal (parser);
7272 if (str == NULL_TREE)
7273 return NULL_TREE;
7274 matching_parens parens;
7275 if (!parens.require_open (parser))
7276 return NULL_TREE;
7277 expr = c_parser_expression (parser);
7278 mark_exp_read (expr.value);
7279 if (!parens.require_close (parser))
7280 {
7281 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7282 return NULL_TREE;
7283 }
7284 list = chainon (list, build_tree_list (build_tree_list (name, str),
7285 expr.value));
7286 if (c_parser_next_token_is (parser, CPP_COMMA))
7287 c_parser_consume_token (parser);
7288 else
7289 break;
7290 }
7291 return list;
7292 }
7293
7294 /* Parse asm clobbers, a GNU extension.
7295
7296 asm-clobbers:
7297 asm-string-literal
7298 asm-clobbers , asm-string-literal
7299 */
7300
7301 static tree
7302 c_parser_asm_clobbers (c_parser *parser)
7303 {
7304 tree list = NULL_TREE;
7305 while (true)
7306 {
7307 tree str = c_parser_asm_string_literal (parser);
7308 if (str)
7309 list = tree_cons (NULL_TREE, str, list);
7310 else
7311 return NULL_TREE;
7312 if (c_parser_next_token_is (parser, CPP_COMMA))
7313 c_parser_consume_token (parser);
7314 else
7315 break;
7316 }
7317 return list;
7318 }
7319
7320 /* Parse asm goto labels, a GNU extension.
7321
7322 asm-goto-operands:
7323 identifier
7324 asm-goto-operands , identifier
7325 */
7326
7327 static tree
7328 c_parser_asm_goto_operands (c_parser *parser)
7329 {
7330 tree list = NULL_TREE;
7331 while (true)
7332 {
7333 tree name, label;
7334
7335 if (c_parser_next_token_is (parser, CPP_NAME))
7336 {
7337 c_token *tok = c_parser_peek_token (parser);
7338 name = tok->value;
7339 label = lookup_label_for_goto (tok->location, name);
7340 c_parser_consume_token (parser);
7341 TREE_USED (label) = 1;
7342 }
7343 else
7344 {
7345 c_parser_error (parser, "expected identifier");
7346 return NULL_TREE;
7347 }
7348
7349 name = build_string (IDENTIFIER_LENGTH (name),
7350 IDENTIFIER_POINTER (name));
7351 list = tree_cons (name, label, list);
7352 if (c_parser_next_token_is (parser, CPP_COMMA))
7353 c_parser_consume_token (parser);
7354 else
7355 return nreverse (list);
7356 }
7357 }
7358
7359 /* Parse a possibly concatenated sequence of string literals.
7360 TRANSLATE says whether to translate them to the execution character
7361 set; WIDE_OK says whether any kind of prefixed string literal is
7362 permitted in this context. This code is based on that in
7363 lex_string. */
7364
7365 struct c_expr
7366 c_parser_string_literal (c_parser *parser, bool translate, bool wide_ok)
7367 {
7368 struct c_expr ret;
7369 size_t count;
7370 struct obstack str_ob;
7371 struct obstack loc_ob;
7372 cpp_string str, istr, *strs;
7373 c_token *tok;
7374 location_t loc, last_tok_loc;
7375 enum cpp_ttype type;
7376 tree value, string_tree;
7377
7378 tok = c_parser_peek_token (parser);
7379 loc = tok->location;
7380 last_tok_loc = linemap_resolve_location (line_table, loc,
7381 LRK_MACRO_DEFINITION_LOCATION,
7382 NULL);
7383 type = tok->type;
7384 switch (type)
7385 {
7386 case CPP_STRING:
7387 case CPP_WSTRING:
7388 case CPP_STRING16:
7389 case CPP_STRING32:
7390 case CPP_UTF8STRING:
7391 string_tree = tok->value;
7392 break;
7393
7394 default:
7395 c_parser_error (parser, "expected string literal");
7396 ret.set_error ();
7397 ret.value = NULL_TREE;
7398 ret.original_code = ERROR_MARK;
7399 ret.original_type = NULL_TREE;
7400 return ret;
7401 }
7402
7403 /* Try to avoid the overhead of creating and destroying an obstack
7404 for the common case of just one string. */
7405 switch (c_parser_peek_2nd_token (parser)->type)
7406 {
7407 default:
7408 c_parser_consume_token (parser);
7409 str.text = (const unsigned char *) TREE_STRING_POINTER (string_tree);
7410 str.len = TREE_STRING_LENGTH (string_tree);
7411 count = 1;
7412 strs = &str;
7413 break;
7414
7415 case CPP_STRING:
7416 case CPP_WSTRING:
7417 case CPP_STRING16:
7418 case CPP_STRING32:
7419 case CPP_UTF8STRING:
7420 gcc_obstack_init (&str_ob);
7421 gcc_obstack_init (&loc_ob);
7422 count = 0;
7423 do
7424 {
7425 c_parser_consume_token (parser);
7426 count++;
7427 str.text = (const unsigned char *) TREE_STRING_POINTER (string_tree);
7428 str.len = TREE_STRING_LENGTH (string_tree);
7429 if (type != tok->type)
7430 {
7431 if (type == CPP_STRING)
7432 type = tok->type;
7433 else if (tok->type != CPP_STRING)
7434 error ("unsupported non-standard concatenation "
7435 "of string literals");
7436 }
7437 obstack_grow (&str_ob, &str, sizeof (cpp_string));
7438 obstack_grow (&loc_ob, &last_tok_loc, sizeof (location_t));
7439 tok = c_parser_peek_token (parser);
7440 string_tree = tok->value;
7441 last_tok_loc
7442 = linemap_resolve_location (line_table, tok->location,
7443 LRK_MACRO_DEFINITION_LOCATION, NULL);
7444 }
7445 while (tok->type == CPP_STRING
7446 || tok->type == CPP_WSTRING
7447 || tok->type == CPP_STRING16
7448 || tok->type == CPP_STRING32
7449 || tok->type == CPP_UTF8STRING);
7450 strs = (cpp_string *) obstack_finish (&str_ob);
7451 }
7452
7453 if (count > 1 && !in_system_header_at (input_location))
7454 warning (OPT_Wtraditional,
7455 "traditional C rejects string constant concatenation");
7456
7457 if ((type == CPP_STRING || wide_ok)
7458 && ((translate
7459 ? cpp_interpret_string : cpp_interpret_string_notranslate)
7460 (parse_in, strs, count, &istr, type)))
7461 {
7462 value = build_string (istr.len, (const char *) istr.text);
7463 free (CONST_CAST (unsigned char *, istr.text));
7464 if (count > 1)
7465 {
7466 location_t *locs = (location_t *) obstack_finish (&loc_ob);
7467 gcc_assert (g_string_concat_db);
7468 g_string_concat_db->record_string_concatenation (count, locs);
7469 }
7470 }
7471 else
7472 {
7473 if (type != CPP_STRING && !wide_ok)
7474 {
7475 error_at (loc, "a wide string is invalid in this context");
7476 type = CPP_STRING;
7477 }
7478 /* Callers cannot generally handle error_mark_node in this
7479 context, so return the empty string instead. An error has
7480 been issued, either above or from cpp_interpret_string. */
7481 switch (type)
7482 {
7483 default:
7484 case CPP_STRING:
7485 case CPP_UTF8STRING:
7486 value = build_string (1, "");
7487 break;
7488 case CPP_STRING16:
7489 value = build_string (TYPE_PRECISION (char16_type_node)
7490 / TYPE_PRECISION (char_type_node),
7491 "\0"); /* char16_t is 16 bits */
7492 break;
7493 case CPP_STRING32:
7494 value = build_string (TYPE_PRECISION (char32_type_node)
7495 / TYPE_PRECISION (char_type_node),
7496 "\0\0\0"); /* char32_t is 32 bits */
7497 break;
7498 case CPP_WSTRING:
7499 value = build_string (TYPE_PRECISION (wchar_type_node)
7500 / TYPE_PRECISION (char_type_node),
7501 "\0\0\0"); /* widest supported wchar_t
7502 is 32 bits */
7503 break;
7504 }
7505 }
7506
7507 switch (type)
7508 {
7509 default:
7510 case CPP_STRING:
7511 case CPP_UTF8STRING:
7512 TREE_TYPE (value) = char_array_type_node;
7513 break;
7514 case CPP_STRING16:
7515 TREE_TYPE (value) = char16_array_type_node;
7516 break;
7517 case CPP_STRING32:
7518 TREE_TYPE (value) = char32_array_type_node;
7519 break;
7520 case CPP_WSTRING:
7521 TREE_TYPE (value) = wchar_array_type_node;
7522 }
7523 value = fix_string_type (value);
7524
7525 if (count > 1)
7526 {
7527 obstack_free (&str_ob, 0);
7528 obstack_free (&loc_ob, 0);
7529 }
7530
7531 ret.value = value;
7532 ret.original_code = STRING_CST;
7533 ret.original_type = NULL_TREE;
7534 set_c_expr_source_range (&ret, get_range_from_loc (line_table, loc));
7535 return ret;
7536 }
7537
7538 /* Parse an expression other than a compound expression; that is, an
7539 assignment expression (C90 6.3.16, C99 6.5.16, C11 6.5.16). If
7540 AFTER is not NULL then it is an Objective-C message expression which
7541 is the primary-expression starting the expression as an initializer.
7542
7543 assignment-expression:
7544 conditional-expression
7545 unary-expression assignment-operator assignment-expression
7546
7547 assignment-operator: one of
7548 = *= /= %= += -= <<= >>= &= ^= |=
7549
7550 In GNU C we accept any conditional expression on the LHS and
7551 diagnose the invalid lvalue rather than producing a syntax
7552 error. */
7553
7554 static struct c_expr
7555 c_parser_expr_no_commas (c_parser *parser, struct c_expr *after,
7556 tree omp_atomic_lhs)
7557 {
7558 struct c_expr lhs, rhs, ret;
7559 enum tree_code code;
7560 location_t op_location, exp_location;
7561 gcc_assert (!after || c_dialect_objc ());
7562 lhs = c_parser_conditional_expression (parser, after, omp_atomic_lhs);
7563 op_location = c_parser_peek_token (parser)->location;
7564 switch (c_parser_peek_token (parser)->type)
7565 {
7566 case CPP_EQ:
7567 code = NOP_EXPR;
7568 break;
7569 case CPP_MULT_EQ:
7570 code = MULT_EXPR;
7571 break;
7572 case CPP_DIV_EQ:
7573 code = TRUNC_DIV_EXPR;
7574 break;
7575 case CPP_MOD_EQ:
7576 code = TRUNC_MOD_EXPR;
7577 break;
7578 case CPP_PLUS_EQ:
7579 code = PLUS_EXPR;
7580 break;
7581 case CPP_MINUS_EQ:
7582 code = MINUS_EXPR;
7583 break;
7584 case CPP_LSHIFT_EQ:
7585 code = LSHIFT_EXPR;
7586 break;
7587 case CPP_RSHIFT_EQ:
7588 code = RSHIFT_EXPR;
7589 break;
7590 case CPP_AND_EQ:
7591 code = BIT_AND_EXPR;
7592 break;
7593 case CPP_XOR_EQ:
7594 code = BIT_XOR_EXPR;
7595 break;
7596 case CPP_OR_EQ:
7597 code = BIT_IOR_EXPR;
7598 break;
7599 default:
7600 return lhs;
7601 }
7602 c_parser_consume_token (parser);
7603 exp_location = c_parser_peek_token (parser)->location;
7604 rhs = c_parser_expr_no_commas (parser, NULL);
7605 rhs = convert_lvalue_to_rvalue (exp_location, rhs, true, true);
7606
7607 ret.value = build_modify_expr (op_location, lhs.value, lhs.original_type,
7608 code, exp_location, rhs.value,
7609 rhs.original_type);
7610 set_c_expr_source_range (&ret, lhs.get_start (), rhs.get_finish ());
7611 if (code == NOP_EXPR)
7612 ret.original_code = MODIFY_EXPR;
7613 else
7614 {
7615 TREE_NO_WARNING (ret.value) = 1;
7616 ret.original_code = ERROR_MARK;
7617 }
7618 ret.original_type = NULL;
7619 return ret;
7620 }
7621
7622 /* Parse a conditional expression (C90 6.3.15, C99 6.5.15, C11 6.5.15). If
7623 AFTER is not NULL then it is an Objective-C message expression which is
7624 the primary-expression starting the expression as an initializer.
7625
7626 conditional-expression:
7627 logical-OR-expression
7628 logical-OR-expression ? expression : conditional-expression
7629
7630 GNU extensions:
7631
7632 conditional-expression:
7633 logical-OR-expression ? : conditional-expression
7634 */
7635
7636 static struct c_expr
7637 c_parser_conditional_expression (c_parser *parser, struct c_expr *after,
7638 tree omp_atomic_lhs)
7639 {
7640 struct c_expr cond, exp1, exp2, ret;
7641 location_t start, cond_loc, colon_loc;
7642
7643 gcc_assert (!after || c_dialect_objc ());
7644
7645 cond = c_parser_binary_expression (parser, after, omp_atomic_lhs);
7646
7647 if (c_parser_next_token_is_not (parser, CPP_QUERY))
7648 return cond;
7649 if (cond.value != error_mark_node)
7650 start = cond.get_start ();
7651 else
7652 start = UNKNOWN_LOCATION;
7653 cond_loc = c_parser_peek_token (parser)->location;
7654 cond = convert_lvalue_to_rvalue (cond_loc, cond, true, true);
7655 c_parser_consume_token (parser);
7656 if (c_parser_next_token_is (parser, CPP_COLON))
7657 {
7658 tree eptype = NULL_TREE;
7659
7660 location_t middle_loc = c_parser_peek_token (parser)->location;
7661 pedwarn (middle_loc, OPT_Wpedantic,
7662 "ISO C forbids omitting the middle term of a %<?:%> expression");
7663 if (TREE_CODE (cond.value) == EXCESS_PRECISION_EXPR)
7664 {
7665 eptype = TREE_TYPE (cond.value);
7666 cond.value = TREE_OPERAND (cond.value, 0);
7667 }
7668 tree e = cond.value;
7669 while (TREE_CODE (e) == COMPOUND_EXPR)
7670 e = TREE_OPERAND (e, 1);
7671 warn_for_omitted_condop (middle_loc, e);
7672 /* Make sure first operand is calculated only once. */
7673 exp1.value = save_expr (default_conversion (cond.value));
7674 if (eptype)
7675 exp1.value = build1 (EXCESS_PRECISION_EXPR, eptype, exp1.value);
7676 exp1.original_type = NULL;
7677 exp1.src_range = cond.src_range;
7678 cond.value = c_objc_common_truthvalue_conversion (cond_loc, exp1.value);
7679 c_inhibit_evaluation_warnings += cond.value == truthvalue_true_node;
7680 }
7681 else
7682 {
7683 cond.value
7684 = c_objc_common_truthvalue_conversion
7685 (cond_loc, default_conversion (cond.value));
7686 c_inhibit_evaluation_warnings += cond.value == truthvalue_false_node;
7687 exp1 = c_parser_expression_conv (parser);
7688 mark_exp_read (exp1.value);
7689 c_inhibit_evaluation_warnings +=
7690 ((cond.value == truthvalue_true_node)
7691 - (cond.value == truthvalue_false_node));
7692 }
7693
7694 colon_loc = c_parser_peek_token (parser)->location;
7695 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
7696 {
7697 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
7698 ret.set_error ();
7699 ret.original_code = ERROR_MARK;
7700 ret.original_type = NULL;
7701 return ret;
7702 }
7703 {
7704 location_t exp2_loc = c_parser_peek_token (parser)->location;
7705 exp2 = c_parser_conditional_expression (parser, NULL, NULL_TREE);
7706 exp2 = convert_lvalue_to_rvalue (exp2_loc, exp2, true, true);
7707 }
7708 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
7709 location_t loc1 = make_location (exp1.get_start (), exp1.src_range);
7710 location_t loc2 = make_location (exp2.get_start (), exp2.src_range);
7711 ret.value = build_conditional_expr (colon_loc, cond.value,
7712 cond.original_code == C_MAYBE_CONST_EXPR,
7713 exp1.value, exp1.original_type, loc1,
7714 exp2.value, exp2.original_type, loc2);
7715 ret.original_code = ERROR_MARK;
7716 if (exp1.value == error_mark_node || exp2.value == error_mark_node)
7717 ret.original_type = NULL;
7718 else
7719 {
7720 tree t1, t2;
7721
7722 /* If both sides are enum type, the default conversion will have
7723 made the type of the result be an integer type. We want to
7724 remember the enum types we started with. */
7725 t1 = exp1.original_type ? exp1.original_type : TREE_TYPE (exp1.value);
7726 t2 = exp2.original_type ? exp2.original_type : TREE_TYPE (exp2.value);
7727 ret.original_type = ((t1 != error_mark_node
7728 && t2 != error_mark_node
7729 && (TYPE_MAIN_VARIANT (t1)
7730 == TYPE_MAIN_VARIANT (t2)))
7731 ? t1
7732 : NULL);
7733 }
7734 set_c_expr_source_range (&ret, start, exp2.get_finish ());
7735 return ret;
7736 }
7737
7738 /* Parse a binary expression; that is, a logical-OR-expression (C90
7739 6.3.5-6.3.14, C99 6.5.5-6.5.14, C11 6.5.5-6.5.14). If AFTER is not
7740 NULL then it is an Objective-C message expression which is the
7741 primary-expression starting the expression as an initializer.
7742
7743 OMP_ATOMIC_LHS is NULL, unless parsing OpenMP #pragma omp atomic,
7744 when it should be the unfolded lhs. In a valid OpenMP source,
7745 one of the operands of the toplevel binary expression must be equal
7746 to it. In that case, just return a build2 created binary operation
7747 rather than result of parser_build_binary_op.
7748
7749 multiplicative-expression:
7750 cast-expression
7751 multiplicative-expression * cast-expression
7752 multiplicative-expression / cast-expression
7753 multiplicative-expression % cast-expression
7754
7755 additive-expression:
7756 multiplicative-expression
7757 additive-expression + multiplicative-expression
7758 additive-expression - multiplicative-expression
7759
7760 shift-expression:
7761 additive-expression
7762 shift-expression << additive-expression
7763 shift-expression >> additive-expression
7764
7765 relational-expression:
7766 shift-expression
7767 relational-expression < shift-expression
7768 relational-expression > shift-expression
7769 relational-expression <= shift-expression
7770 relational-expression >= shift-expression
7771
7772 equality-expression:
7773 relational-expression
7774 equality-expression == relational-expression
7775 equality-expression != relational-expression
7776
7777 AND-expression:
7778 equality-expression
7779 AND-expression & equality-expression
7780
7781 exclusive-OR-expression:
7782 AND-expression
7783 exclusive-OR-expression ^ AND-expression
7784
7785 inclusive-OR-expression:
7786 exclusive-OR-expression
7787 inclusive-OR-expression | exclusive-OR-expression
7788
7789 logical-AND-expression:
7790 inclusive-OR-expression
7791 logical-AND-expression && inclusive-OR-expression
7792
7793 logical-OR-expression:
7794 logical-AND-expression
7795 logical-OR-expression || logical-AND-expression
7796 */
7797
7798 static struct c_expr
7799 c_parser_binary_expression (c_parser *parser, struct c_expr *after,
7800 tree omp_atomic_lhs)
7801 {
7802 /* A binary expression is parsed using operator-precedence parsing,
7803 with the operands being cast expressions. All the binary
7804 operators are left-associative. Thus a binary expression is of
7805 form:
7806
7807 E0 op1 E1 op2 E2 ...
7808
7809 which we represent on a stack. On the stack, the precedence
7810 levels are strictly increasing. When a new operator is
7811 encountered of higher precedence than that at the top of the
7812 stack, it is pushed; its LHS is the top expression, and its RHS
7813 is everything parsed until it is popped. When a new operator is
7814 encountered with precedence less than or equal to that at the top
7815 of the stack, triples E[i-1] op[i] E[i] are popped and replaced
7816 by the result of the operation until the operator at the top of
7817 the stack has lower precedence than the new operator or there is
7818 only one element on the stack; then the top expression is the LHS
7819 of the new operator. In the case of logical AND and OR
7820 expressions, we also need to adjust c_inhibit_evaluation_warnings
7821 as appropriate when the operators are pushed and popped. */
7822
7823 struct {
7824 /* The expression at this stack level. */
7825 struct c_expr expr;
7826 /* The precedence of the operator on its left, PREC_NONE at the
7827 bottom of the stack. */
7828 enum c_parser_prec prec;
7829 /* The operation on its left. */
7830 enum tree_code op;
7831 /* The source location of this operation. */
7832 location_t loc;
7833 /* The sizeof argument if expr.original_code == SIZEOF_EXPR. */
7834 tree sizeof_arg;
7835 } stack[NUM_PRECS];
7836 int sp;
7837 /* Location of the binary operator. */
7838 location_t binary_loc = UNKNOWN_LOCATION; /* Quiet warning. */
7839 #define POP \
7840 do { \
7841 switch (stack[sp].op) \
7842 { \
7843 case TRUTH_ANDIF_EXPR: \
7844 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
7845 == truthvalue_false_node); \
7846 break; \
7847 case TRUTH_ORIF_EXPR: \
7848 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
7849 == truthvalue_true_node); \
7850 break; \
7851 case TRUNC_DIV_EXPR: \
7852 if (stack[sp - 1].expr.original_code == SIZEOF_EXPR \
7853 && stack[sp].expr.original_code == SIZEOF_EXPR) \
7854 { \
7855 tree type0 = stack[sp - 1].sizeof_arg; \
7856 tree type1 = stack[sp].sizeof_arg; \
7857 tree first_arg = type0; \
7858 if (!TYPE_P (type0)) \
7859 type0 = TREE_TYPE (type0); \
7860 if (!TYPE_P (type1)) \
7861 type1 = TREE_TYPE (type1); \
7862 if (POINTER_TYPE_P (type0) \
7863 && comptypes (TREE_TYPE (type0), type1) \
7864 && !(TREE_CODE (first_arg) == PARM_DECL \
7865 && C_ARRAY_PARAMETER (first_arg) \
7866 && warn_sizeof_array_argument)) \
7867 { \
7868 auto_diagnostic_group d; \
7869 if (warning_at (stack[sp].loc, OPT_Wsizeof_pointer_div, \
7870 "division %<sizeof (%T) / sizeof (%T)%> " \
7871 "does not compute the number of array " \
7872 "elements", \
7873 type0, type1)) \
7874 if (DECL_P (first_arg)) \
7875 inform (DECL_SOURCE_LOCATION (first_arg), \
7876 "first %<sizeof%> operand was declared here"); \
7877 } \
7878 } \
7879 break; \
7880 default: \
7881 break; \
7882 } \
7883 stack[sp - 1].expr \
7884 = convert_lvalue_to_rvalue (stack[sp - 1].loc, \
7885 stack[sp - 1].expr, true, true); \
7886 stack[sp].expr \
7887 = convert_lvalue_to_rvalue (stack[sp].loc, \
7888 stack[sp].expr, true, true); \
7889 if (__builtin_expect (omp_atomic_lhs != NULL_TREE, 0) && sp == 1 \
7890 && c_parser_peek_token (parser)->type == CPP_SEMICOLON \
7891 && ((1 << stack[sp].prec) \
7892 & ((1 << PREC_BITOR) | (1 << PREC_BITXOR) | (1 << PREC_BITAND) \
7893 | (1 << PREC_SHIFT) | (1 << PREC_ADD) | (1 << PREC_MULT))) \
7894 && stack[sp].op != TRUNC_MOD_EXPR \
7895 && stack[0].expr.value != error_mark_node \
7896 && stack[1].expr.value != error_mark_node \
7897 && (c_tree_equal (stack[0].expr.value, omp_atomic_lhs) \
7898 || c_tree_equal (stack[1].expr.value, omp_atomic_lhs))) \
7899 stack[0].expr.value \
7900 = build2 (stack[1].op, TREE_TYPE (stack[0].expr.value), \
7901 stack[0].expr.value, stack[1].expr.value); \
7902 else \
7903 stack[sp - 1].expr = parser_build_binary_op (stack[sp].loc, \
7904 stack[sp].op, \
7905 stack[sp - 1].expr, \
7906 stack[sp].expr); \
7907 sp--; \
7908 } while (0)
7909 gcc_assert (!after || c_dialect_objc ());
7910 stack[0].loc = c_parser_peek_token (parser)->location;
7911 stack[0].expr = c_parser_cast_expression (parser, after);
7912 stack[0].prec = PREC_NONE;
7913 stack[0].sizeof_arg = c_last_sizeof_arg;
7914 sp = 0;
7915 while (true)
7916 {
7917 enum c_parser_prec oprec;
7918 enum tree_code ocode;
7919 source_range src_range;
7920 if (parser->error)
7921 goto out;
7922 switch (c_parser_peek_token (parser)->type)
7923 {
7924 case CPP_MULT:
7925 oprec = PREC_MULT;
7926 ocode = MULT_EXPR;
7927 break;
7928 case CPP_DIV:
7929 oprec = PREC_MULT;
7930 ocode = TRUNC_DIV_EXPR;
7931 break;
7932 case CPP_MOD:
7933 oprec = PREC_MULT;
7934 ocode = TRUNC_MOD_EXPR;
7935 break;
7936 case CPP_PLUS:
7937 oprec = PREC_ADD;
7938 ocode = PLUS_EXPR;
7939 break;
7940 case CPP_MINUS:
7941 oprec = PREC_ADD;
7942 ocode = MINUS_EXPR;
7943 break;
7944 case CPP_LSHIFT:
7945 oprec = PREC_SHIFT;
7946 ocode = LSHIFT_EXPR;
7947 break;
7948 case CPP_RSHIFT:
7949 oprec = PREC_SHIFT;
7950 ocode = RSHIFT_EXPR;
7951 break;
7952 case CPP_LESS:
7953 oprec = PREC_REL;
7954 ocode = LT_EXPR;
7955 break;
7956 case CPP_GREATER:
7957 oprec = PREC_REL;
7958 ocode = GT_EXPR;
7959 break;
7960 case CPP_LESS_EQ:
7961 oprec = PREC_REL;
7962 ocode = LE_EXPR;
7963 break;
7964 case CPP_GREATER_EQ:
7965 oprec = PREC_REL;
7966 ocode = GE_EXPR;
7967 break;
7968 case CPP_EQ_EQ:
7969 oprec = PREC_EQ;
7970 ocode = EQ_EXPR;
7971 break;
7972 case CPP_NOT_EQ:
7973 oprec = PREC_EQ;
7974 ocode = NE_EXPR;
7975 break;
7976 case CPP_AND:
7977 oprec = PREC_BITAND;
7978 ocode = BIT_AND_EXPR;
7979 break;
7980 case CPP_XOR:
7981 oprec = PREC_BITXOR;
7982 ocode = BIT_XOR_EXPR;
7983 break;
7984 case CPP_OR:
7985 oprec = PREC_BITOR;
7986 ocode = BIT_IOR_EXPR;
7987 break;
7988 case CPP_AND_AND:
7989 oprec = PREC_LOGAND;
7990 ocode = TRUTH_ANDIF_EXPR;
7991 break;
7992 case CPP_OR_OR:
7993 oprec = PREC_LOGOR;
7994 ocode = TRUTH_ORIF_EXPR;
7995 break;
7996 default:
7997 /* Not a binary operator, so end of the binary
7998 expression. */
7999 goto out;
8000 }
8001 binary_loc = c_parser_peek_token (parser)->location;
8002 while (oprec <= stack[sp].prec)
8003 POP;
8004 c_parser_consume_token (parser);
8005 switch (ocode)
8006 {
8007 case TRUTH_ANDIF_EXPR:
8008 src_range = stack[sp].expr.src_range;
8009 stack[sp].expr
8010 = convert_lvalue_to_rvalue (stack[sp].loc,
8011 stack[sp].expr, true, true);
8012 stack[sp].expr.value = c_objc_common_truthvalue_conversion
8013 (stack[sp].loc, default_conversion (stack[sp].expr.value));
8014 c_inhibit_evaluation_warnings += (stack[sp].expr.value
8015 == truthvalue_false_node);
8016 set_c_expr_source_range (&stack[sp].expr, src_range);
8017 break;
8018 case TRUTH_ORIF_EXPR:
8019 src_range = stack[sp].expr.src_range;
8020 stack[sp].expr
8021 = convert_lvalue_to_rvalue (stack[sp].loc,
8022 stack[sp].expr, true, true);
8023 stack[sp].expr.value = c_objc_common_truthvalue_conversion
8024 (stack[sp].loc, default_conversion (stack[sp].expr.value));
8025 c_inhibit_evaluation_warnings += (stack[sp].expr.value
8026 == truthvalue_true_node);
8027 set_c_expr_source_range (&stack[sp].expr, src_range);
8028 break;
8029 default:
8030 break;
8031 }
8032 sp++;
8033 stack[sp].loc = binary_loc;
8034 stack[sp].expr = c_parser_cast_expression (parser, NULL);
8035 stack[sp].prec = oprec;
8036 stack[sp].op = ocode;
8037 stack[sp].sizeof_arg = c_last_sizeof_arg;
8038 }
8039 out:
8040 while (sp > 0)
8041 POP;
8042 return stack[0].expr;
8043 #undef POP
8044 }
8045
8046 /* Parse a cast expression (C90 6.3.4, C99 6.5.4, C11 6.5.4). If AFTER
8047 is not NULL then it is an Objective-C message expression which is the
8048 primary-expression starting the expression as an initializer.
8049
8050 cast-expression:
8051 unary-expression
8052 ( type-name ) unary-expression
8053 */
8054
8055 static struct c_expr
8056 c_parser_cast_expression (c_parser *parser, struct c_expr *after)
8057 {
8058 location_t cast_loc = c_parser_peek_token (parser)->location;
8059 gcc_assert (!after || c_dialect_objc ());
8060 if (after)
8061 return c_parser_postfix_expression_after_primary (parser,
8062 cast_loc, *after);
8063 /* If the expression begins with a parenthesized type name, it may
8064 be either a cast or a compound literal; we need to see whether
8065 the next character is '{' to tell the difference. If not, it is
8066 an unary expression. Full detection of unknown typenames here
8067 would require a 3-token lookahead. */
8068 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
8069 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
8070 {
8071 struct c_type_name *type_name;
8072 struct c_expr ret;
8073 struct c_expr expr;
8074 matching_parens parens;
8075 parens.consume_open (parser);
8076 type_name = c_parser_type_name (parser, true);
8077 parens.skip_until_found_close (parser);
8078 if (type_name == NULL)
8079 {
8080 ret.set_error ();
8081 ret.original_code = ERROR_MARK;
8082 ret.original_type = NULL;
8083 return ret;
8084 }
8085
8086 /* Save casted types in the function's used types hash table. */
8087 used_types_insert (type_name->specs->type);
8088
8089 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
8090 return c_parser_postfix_expression_after_paren_type (parser, type_name,
8091 cast_loc);
8092 if (type_name->specs->alignas_p)
8093 error_at (type_name->specs->locations[cdw_alignas],
8094 "alignment specified for type name in cast");
8095 {
8096 location_t expr_loc = c_parser_peek_token (parser)->location;
8097 expr = c_parser_cast_expression (parser, NULL);
8098 expr = convert_lvalue_to_rvalue (expr_loc, expr, true, true);
8099 }
8100 ret.value = c_cast_expr (cast_loc, type_name, expr.value);
8101 if (ret.value && expr.value)
8102 set_c_expr_source_range (&ret, cast_loc, expr.get_finish ());
8103 ret.original_code = ERROR_MARK;
8104 ret.original_type = NULL;
8105 return ret;
8106 }
8107 else
8108 return c_parser_unary_expression (parser);
8109 }
8110
8111 /* Parse an unary expression (C90 6.3.3, C99 6.5.3, C11 6.5.3).
8112
8113 unary-expression:
8114 postfix-expression
8115 ++ unary-expression
8116 -- unary-expression
8117 unary-operator cast-expression
8118 sizeof unary-expression
8119 sizeof ( type-name )
8120
8121 unary-operator: one of
8122 & * + - ~ !
8123
8124 GNU extensions:
8125
8126 unary-expression:
8127 __alignof__ unary-expression
8128 __alignof__ ( type-name )
8129 && identifier
8130
8131 (C11 permits _Alignof with type names only.)
8132
8133 unary-operator: one of
8134 __extension__ __real__ __imag__
8135
8136 Transactional Memory:
8137
8138 unary-expression:
8139 transaction-expression
8140
8141 In addition, the GNU syntax treats ++ and -- as unary operators, so
8142 they may be applied to cast expressions with errors for non-lvalues
8143 given later. */
8144
8145 static struct c_expr
8146 c_parser_unary_expression (c_parser *parser)
8147 {
8148 int ext;
8149 struct c_expr ret, op;
8150 location_t op_loc = c_parser_peek_token (parser)->location;
8151 location_t exp_loc;
8152 location_t finish;
8153 ret.original_code = ERROR_MARK;
8154 ret.original_type = NULL;
8155 switch (c_parser_peek_token (parser)->type)
8156 {
8157 case CPP_PLUS_PLUS:
8158 c_parser_consume_token (parser);
8159 exp_loc = c_parser_peek_token (parser)->location;
8160 op = c_parser_cast_expression (parser, NULL);
8161
8162 op = default_function_array_read_conversion (exp_loc, op);
8163 return parser_build_unary_op (op_loc, PREINCREMENT_EXPR, op);
8164 case CPP_MINUS_MINUS:
8165 c_parser_consume_token (parser);
8166 exp_loc = c_parser_peek_token (parser)->location;
8167 op = c_parser_cast_expression (parser, NULL);
8168
8169 op = default_function_array_read_conversion (exp_loc, op);
8170 return parser_build_unary_op (op_loc, PREDECREMENT_EXPR, op);
8171 case CPP_AND:
8172 c_parser_consume_token (parser);
8173 op = c_parser_cast_expression (parser, NULL);
8174 mark_exp_read (op.value);
8175 return parser_build_unary_op (op_loc, ADDR_EXPR, op);
8176 case CPP_MULT:
8177 {
8178 c_parser_consume_token (parser);
8179 exp_loc = c_parser_peek_token (parser)->location;
8180 op = c_parser_cast_expression (parser, NULL);
8181 finish = op.get_finish ();
8182 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
8183 location_t combined_loc = make_location (op_loc, op_loc, finish);
8184 ret.value = build_indirect_ref (combined_loc, op.value, RO_UNARY_STAR);
8185 ret.src_range.m_start = op_loc;
8186 ret.src_range.m_finish = finish;
8187 return ret;
8188 }
8189 case CPP_PLUS:
8190 if (!c_dialect_objc () && !in_system_header_at (input_location))
8191 warning_at (op_loc,
8192 OPT_Wtraditional,
8193 "traditional C rejects the unary plus operator");
8194 c_parser_consume_token (parser);
8195 exp_loc = c_parser_peek_token (parser)->location;
8196 op = c_parser_cast_expression (parser, NULL);
8197 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
8198 return parser_build_unary_op (op_loc, CONVERT_EXPR, op);
8199 case CPP_MINUS:
8200 c_parser_consume_token (parser);
8201 exp_loc = c_parser_peek_token (parser)->location;
8202 op = c_parser_cast_expression (parser, NULL);
8203 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
8204 return parser_build_unary_op (op_loc, NEGATE_EXPR, op);
8205 case CPP_COMPL:
8206 c_parser_consume_token (parser);
8207 exp_loc = c_parser_peek_token (parser)->location;
8208 op = c_parser_cast_expression (parser, NULL);
8209 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
8210 return parser_build_unary_op (op_loc, BIT_NOT_EXPR, op);
8211 case CPP_NOT:
8212 c_parser_consume_token (parser);
8213 exp_loc = c_parser_peek_token (parser)->location;
8214 op = c_parser_cast_expression (parser, NULL);
8215 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
8216 return parser_build_unary_op (op_loc, TRUTH_NOT_EXPR, op);
8217 case CPP_AND_AND:
8218 /* Refer to the address of a label as a pointer. */
8219 c_parser_consume_token (parser);
8220 if (c_parser_next_token_is (parser, CPP_NAME))
8221 {
8222 ret.value = finish_label_address_expr
8223 (c_parser_peek_token (parser)->value, op_loc);
8224 set_c_expr_source_range (&ret, op_loc,
8225 c_parser_peek_token (parser)->get_finish ());
8226 c_parser_consume_token (parser);
8227 }
8228 else
8229 {
8230 c_parser_error (parser, "expected identifier");
8231 ret.set_error ();
8232 }
8233 return ret;
8234 case CPP_KEYWORD:
8235 switch (c_parser_peek_token (parser)->keyword)
8236 {
8237 case RID_SIZEOF:
8238 return c_parser_sizeof_expression (parser);
8239 case RID_ALIGNOF:
8240 return c_parser_alignof_expression (parser);
8241 case RID_BUILTIN_HAS_ATTRIBUTE:
8242 return c_parser_has_attribute_expression (parser);
8243 case RID_EXTENSION:
8244 c_parser_consume_token (parser);
8245 ext = disable_extension_diagnostics ();
8246 ret = c_parser_cast_expression (parser, NULL);
8247 restore_extension_diagnostics (ext);
8248 return ret;
8249 case RID_REALPART:
8250 c_parser_consume_token (parser);
8251 exp_loc = c_parser_peek_token (parser)->location;
8252 op = c_parser_cast_expression (parser, NULL);
8253 op = default_function_array_conversion (exp_loc, op);
8254 return parser_build_unary_op (op_loc, REALPART_EXPR, op);
8255 case RID_IMAGPART:
8256 c_parser_consume_token (parser);
8257 exp_loc = c_parser_peek_token (parser)->location;
8258 op = c_parser_cast_expression (parser, NULL);
8259 op = default_function_array_conversion (exp_loc, op);
8260 return parser_build_unary_op (op_loc, IMAGPART_EXPR, op);
8261 case RID_TRANSACTION_ATOMIC:
8262 case RID_TRANSACTION_RELAXED:
8263 return c_parser_transaction_expression (parser,
8264 c_parser_peek_token (parser)->keyword);
8265 default:
8266 return c_parser_postfix_expression (parser);
8267 }
8268 default:
8269 return c_parser_postfix_expression (parser);
8270 }
8271 }
8272
8273 /* Parse a sizeof expression. */
8274
8275 static struct c_expr
8276 c_parser_sizeof_expression (c_parser *parser)
8277 {
8278 struct c_expr expr;
8279 struct c_expr result;
8280 location_t expr_loc;
8281 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SIZEOF));
8282
8283 location_t start;
8284 location_t finish = UNKNOWN_LOCATION;
8285
8286 start = c_parser_peek_token (parser)->location;
8287
8288 c_parser_consume_token (parser);
8289 c_inhibit_evaluation_warnings++;
8290 in_sizeof++;
8291 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
8292 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
8293 {
8294 /* Either sizeof ( type-name ) or sizeof unary-expression
8295 starting with a compound literal. */
8296 struct c_type_name *type_name;
8297 matching_parens parens;
8298 parens.consume_open (parser);
8299 expr_loc = c_parser_peek_token (parser)->location;
8300 type_name = c_parser_type_name (parser, true);
8301 parens.skip_until_found_close (parser);
8302 finish = parser->tokens_buf[0].location;
8303 if (type_name == NULL)
8304 {
8305 struct c_expr ret;
8306 c_inhibit_evaluation_warnings--;
8307 in_sizeof--;
8308 ret.set_error ();
8309 ret.original_code = ERROR_MARK;
8310 ret.original_type = NULL;
8311 return ret;
8312 }
8313 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
8314 {
8315 expr = c_parser_postfix_expression_after_paren_type (parser,
8316 type_name,
8317 expr_loc);
8318 finish = expr.get_finish ();
8319 goto sizeof_expr;
8320 }
8321 /* sizeof ( type-name ). */
8322 if (type_name->specs->alignas_p)
8323 error_at (type_name->specs->locations[cdw_alignas],
8324 "alignment specified for type name in %<sizeof%>");
8325 c_inhibit_evaluation_warnings--;
8326 in_sizeof--;
8327 result = c_expr_sizeof_type (expr_loc, type_name);
8328 }
8329 else
8330 {
8331 expr_loc = c_parser_peek_token (parser)->location;
8332 expr = c_parser_unary_expression (parser);
8333 finish = expr.get_finish ();
8334 sizeof_expr:
8335 c_inhibit_evaluation_warnings--;
8336 in_sizeof--;
8337 mark_exp_read (expr.value);
8338 if (TREE_CODE (expr.value) == COMPONENT_REF
8339 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
8340 error_at (expr_loc, "%<sizeof%> applied to a bit-field");
8341 result = c_expr_sizeof_expr (expr_loc, expr);
8342 }
8343 if (finish == UNKNOWN_LOCATION)
8344 finish = start;
8345 set_c_expr_source_range (&result, start, finish);
8346 return result;
8347 }
8348
8349 /* Parse an alignof expression. */
8350
8351 static struct c_expr
8352 c_parser_alignof_expression (c_parser *parser)
8353 {
8354 struct c_expr expr;
8355 location_t start_loc = c_parser_peek_token (parser)->location;
8356 location_t end_loc;
8357 tree alignof_spelling = c_parser_peek_token (parser)->value;
8358 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNOF));
8359 bool is_c11_alignof = strcmp (IDENTIFIER_POINTER (alignof_spelling),
8360 "_Alignof") == 0;
8361 /* A diagnostic is not required for the use of this identifier in
8362 the implementation namespace; only diagnose it for the C11
8363 spelling because of existing code using the other spellings. */
8364 if (is_c11_alignof)
8365 {
8366 if (flag_isoc99)
8367 pedwarn_c99 (start_loc, OPT_Wpedantic, "ISO C99 does not support %qE",
8368 alignof_spelling);
8369 else
8370 pedwarn_c99 (start_loc, OPT_Wpedantic, "ISO C90 does not support %qE",
8371 alignof_spelling);
8372 }
8373 c_parser_consume_token (parser);
8374 c_inhibit_evaluation_warnings++;
8375 in_alignof++;
8376 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
8377 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
8378 {
8379 /* Either __alignof__ ( type-name ) or __alignof__
8380 unary-expression starting with a compound literal. */
8381 location_t loc;
8382 struct c_type_name *type_name;
8383 struct c_expr ret;
8384 matching_parens parens;
8385 parens.consume_open (parser);
8386 loc = c_parser_peek_token (parser)->location;
8387 type_name = c_parser_type_name (parser, true);
8388 end_loc = c_parser_peek_token (parser)->location;
8389 parens.skip_until_found_close (parser);
8390 if (type_name == NULL)
8391 {
8392 struct c_expr ret;
8393 c_inhibit_evaluation_warnings--;
8394 in_alignof--;
8395 ret.set_error ();
8396 ret.original_code = ERROR_MARK;
8397 ret.original_type = NULL;
8398 return ret;
8399 }
8400 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
8401 {
8402 expr = c_parser_postfix_expression_after_paren_type (parser,
8403 type_name,
8404 loc);
8405 goto alignof_expr;
8406 }
8407 /* alignof ( type-name ). */
8408 if (type_name->specs->alignas_p)
8409 error_at (type_name->specs->locations[cdw_alignas],
8410 "alignment specified for type name in %qE",
8411 alignof_spelling);
8412 c_inhibit_evaluation_warnings--;
8413 in_alignof--;
8414 ret.value = c_sizeof_or_alignof_type (loc, groktypename (type_name,
8415 NULL, NULL),
8416 false, is_c11_alignof, 1);
8417 ret.original_code = ERROR_MARK;
8418 ret.original_type = NULL;
8419 set_c_expr_source_range (&ret, start_loc, end_loc);
8420 return ret;
8421 }
8422 else
8423 {
8424 struct c_expr ret;
8425 expr = c_parser_unary_expression (parser);
8426 end_loc = expr.src_range.m_finish;
8427 alignof_expr:
8428 mark_exp_read (expr.value);
8429 c_inhibit_evaluation_warnings--;
8430 in_alignof--;
8431 if (is_c11_alignof)
8432 pedwarn (start_loc,
8433 OPT_Wpedantic, "ISO C does not allow %<%E (expression)%>",
8434 alignof_spelling);
8435 ret.value = c_alignof_expr (start_loc, expr.value);
8436 ret.original_code = ERROR_MARK;
8437 ret.original_type = NULL;
8438 set_c_expr_source_range (&ret, start_loc, end_loc);
8439 return ret;
8440 }
8441 }
8442
8443 /* Parse the __builtin_has_attribute ([expr|type], attribute-spec)
8444 expression. */
8445
8446 static struct c_expr
8447 c_parser_has_attribute_expression (c_parser *parser)
8448 {
8449 gcc_assert (c_parser_next_token_is_keyword (parser,
8450 RID_BUILTIN_HAS_ATTRIBUTE));
8451 c_parser_consume_token (parser);
8452
8453 c_inhibit_evaluation_warnings++;
8454
8455 matching_parens parens;
8456 if (!parens.require_open (parser))
8457 {
8458 c_inhibit_evaluation_warnings--;
8459 in_typeof--;
8460
8461 struct c_expr result;
8462 result.set_error ();
8463 result.original_code = ERROR_MARK;
8464 result.original_type = NULL;
8465 return result;
8466 }
8467
8468 /* Treat the type argument the same way as in typeof for the purposes
8469 of warnings. FIXME: Generalize this so the warning refers to
8470 __builtin_has_attribute rather than typeof. */
8471 in_typeof++;
8472
8473 /* The first operand: one of DECL, EXPR, or TYPE. */
8474 tree oper = NULL_TREE;
8475 if (c_parser_next_tokens_start_typename (parser, cla_prefer_id))
8476 {
8477 struct c_type_name *tname = c_parser_type_name (parser);
8478 in_typeof--;
8479 if (tname)
8480 {
8481 oper = groktypename (tname, NULL, NULL);
8482 pop_maybe_used (variably_modified_type_p (oper, NULL_TREE));
8483 }
8484 }
8485 else
8486 {
8487 struct c_expr cexpr = c_parser_expr_no_commas (parser, NULL);
8488 c_inhibit_evaluation_warnings--;
8489 in_typeof--;
8490 if (cexpr.value != error_mark_node)
8491 {
8492 mark_exp_read (cexpr.value);
8493 oper = cexpr.value;
8494 tree etype = TREE_TYPE (oper);
8495 bool was_vm = variably_modified_type_p (etype, NULL_TREE);
8496 /* This is returned with the type so that when the type is
8497 evaluated, this can be evaluated. */
8498 if (was_vm)
8499 oper = c_fully_fold (oper, false, NULL);
8500 pop_maybe_used (was_vm);
8501 }
8502 }
8503
8504 struct c_expr result;
8505 result.original_code = ERROR_MARK;
8506 result.original_type = NULL;
8507
8508 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
8509 {
8510 /* Consume the closing parenthesis if that's the next token
8511 in the likely case the built-in was invoked with fewer
8512 than two arguments. */
8513 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
8514 c_parser_consume_token (parser);
8515 c_inhibit_evaluation_warnings--;
8516 result.set_error ();
8517 return result;
8518 }
8519
8520 bool save_translate_strings_p = parser->translate_strings_p;
8521
8522 location_t atloc = c_parser_peek_token (parser)->location;
8523 /* Parse a single attribute. Require no leading comma and do not
8524 allow empty attributes. */
8525 tree attr = c_parser_gnu_attribute (parser, NULL_TREE, false, false);
8526
8527 parser->translate_strings_p = save_translate_strings_p;
8528
8529 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
8530 c_parser_consume_token (parser);
8531 else
8532 {
8533 c_parser_error (parser, "expected identifier");
8534 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
8535
8536 result.set_error ();
8537 return result;
8538 }
8539
8540 if (!attr)
8541 {
8542 error_at (atloc, "expected identifier");
8543 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
8544 "expected %<)%>");
8545 result.set_error ();
8546 return result;
8547 }
8548
8549 result.original_code = INTEGER_CST;
8550 result.original_type = boolean_type_node;
8551
8552 if (has_attribute (atloc, oper, attr, default_conversion))
8553 result.value = boolean_true_node;
8554 else
8555 result.value = boolean_false_node;
8556
8557 return result;
8558 }
8559
8560 /* Helper function to read arguments of builtins which are interfaces
8561 for the middle-end nodes like COMPLEX_EXPR, VEC_PERM_EXPR and
8562 others. The name of the builtin is passed using BNAME parameter.
8563 Function returns true if there were no errors while parsing and
8564 stores the arguments in CEXPR_LIST. If it returns true,
8565 *OUT_CLOSE_PAREN_LOC is written to with the location of the closing
8566 parenthesis. */
8567 static bool
8568 c_parser_get_builtin_args (c_parser *parser, const char *bname,
8569 vec<c_expr_t, va_gc> **ret_cexpr_list,
8570 bool choose_expr_p,
8571 location_t *out_close_paren_loc)
8572 {
8573 location_t loc = c_parser_peek_token (parser)->location;
8574 vec<c_expr_t, va_gc> *cexpr_list;
8575 c_expr_t expr;
8576 bool saved_force_folding_builtin_constant_p;
8577
8578 *ret_cexpr_list = NULL;
8579 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
8580 {
8581 error_at (loc, "cannot take address of %qs", bname);
8582 return false;
8583 }
8584
8585 c_parser_consume_token (parser);
8586
8587 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
8588 {
8589 *out_close_paren_loc = c_parser_peek_token (parser)->location;
8590 c_parser_consume_token (parser);
8591 return true;
8592 }
8593
8594 saved_force_folding_builtin_constant_p
8595 = force_folding_builtin_constant_p;
8596 force_folding_builtin_constant_p |= choose_expr_p;
8597 expr = c_parser_expr_no_commas (parser, NULL);
8598 force_folding_builtin_constant_p
8599 = saved_force_folding_builtin_constant_p;
8600 vec_alloc (cexpr_list, 1);
8601 vec_safe_push (cexpr_list, expr);
8602 while (c_parser_next_token_is (parser, CPP_COMMA))
8603 {
8604 c_parser_consume_token (parser);
8605 expr = c_parser_expr_no_commas (parser, NULL);
8606 vec_safe_push (cexpr_list, expr);
8607 }
8608
8609 *out_close_paren_loc = c_parser_peek_token (parser)->location;
8610 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
8611 return false;
8612
8613 *ret_cexpr_list = cexpr_list;
8614 return true;
8615 }
8616
8617 /* This represents a single generic-association. */
8618
8619 struct c_generic_association
8620 {
8621 /* The location of the starting token of the type. */
8622 location_t type_location;
8623 /* The association's type, or NULL_TREE for 'default'. */
8624 tree type;
8625 /* The association's expression. */
8626 struct c_expr expression;
8627 };
8628
8629 /* Parse a generic-selection. (C11 6.5.1.1).
8630
8631 generic-selection:
8632 _Generic ( assignment-expression , generic-assoc-list )
8633
8634 generic-assoc-list:
8635 generic-association
8636 generic-assoc-list , generic-association
8637
8638 generic-association:
8639 type-name : assignment-expression
8640 default : assignment-expression
8641 */
8642
8643 static struct c_expr
8644 c_parser_generic_selection (c_parser *parser)
8645 {
8646 struct c_expr selector, error_expr;
8647 tree selector_type;
8648 struct c_generic_association matched_assoc;
8649 bool match_found = false;
8650 location_t generic_loc, selector_loc;
8651
8652 error_expr.original_code = ERROR_MARK;
8653 error_expr.original_type = NULL;
8654 error_expr.set_error ();
8655 matched_assoc.type_location = UNKNOWN_LOCATION;
8656 matched_assoc.type = NULL_TREE;
8657 matched_assoc.expression = error_expr;
8658
8659 gcc_assert (c_parser_next_token_is_keyword (parser, RID_GENERIC));
8660 generic_loc = c_parser_peek_token (parser)->location;
8661 c_parser_consume_token (parser);
8662 if (flag_isoc99)
8663 pedwarn_c99 (generic_loc, OPT_Wpedantic,
8664 "ISO C99 does not support %<_Generic%>");
8665 else
8666 pedwarn_c99 (generic_loc, OPT_Wpedantic,
8667 "ISO C90 does not support %<_Generic%>");
8668
8669 matching_parens parens;
8670 if (!parens.require_open (parser))
8671 return error_expr;
8672
8673 c_inhibit_evaluation_warnings++;
8674 selector_loc = c_parser_peek_token (parser)->location;
8675 selector = c_parser_expr_no_commas (parser, NULL);
8676 selector = default_function_array_conversion (selector_loc, selector);
8677 c_inhibit_evaluation_warnings--;
8678
8679 if (selector.value == error_mark_node)
8680 {
8681 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
8682 return selector;
8683 }
8684 selector_type = TREE_TYPE (selector.value);
8685 /* In ISO C terms, rvalues (including the controlling expression of
8686 _Generic) do not have qualified types. */
8687 if (TREE_CODE (selector_type) != ARRAY_TYPE)
8688 selector_type = TYPE_MAIN_VARIANT (selector_type);
8689 /* In ISO C terms, _Noreturn is not part of the type of expressions
8690 such as &abort, but in GCC it is represented internally as a type
8691 qualifier. */
8692 if (FUNCTION_POINTER_TYPE_P (selector_type)
8693 && TYPE_QUALS (TREE_TYPE (selector_type)) != TYPE_UNQUALIFIED)
8694 selector_type
8695 = build_pointer_type (TYPE_MAIN_VARIANT (TREE_TYPE (selector_type)));
8696
8697 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
8698 {
8699 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
8700 return error_expr;
8701 }
8702
8703 auto_vec<c_generic_association> associations;
8704 while (1)
8705 {
8706 struct c_generic_association assoc, *iter;
8707 unsigned int ix;
8708 c_token *token = c_parser_peek_token (parser);
8709
8710 assoc.type_location = token->location;
8711 if (token->type == CPP_KEYWORD && token->keyword == RID_DEFAULT)
8712 {
8713 c_parser_consume_token (parser);
8714 assoc.type = NULL_TREE;
8715 }
8716 else
8717 {
8718 struct c_type_name *type_name;
8719
8720 type_name = c_parser_type_name (parser);
8721 if (type_name == NULL)
8722 {
8723 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
8724 return error_expr;
8725 }
8726 assoc.type = groktypename (type_name, NULL, NULL);
8727 if (assoc.type == error_mark_node)
8728 {
8729 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
8730 return error_expr;
8731 }
8732
8733 if (TREE_CODE (assoc.type) == FUNCTION_TYPE)
8734 error_at (assoc.type_location,
8735 "%<_Generic%> association has function type");
8736 else if (!COMPLETE_TYPE_P (assoc.type))
8737 error_at (assoc.type_location,
8738 "%<_Generic%> association has incomplete type");
8739
8740 if (variably_modified_type_p (assoc.type, NULL_TREE))
8741 error_at (assoc.type_location,
8742 "%<_Generic%> association has "
8743 "variable length type");
8744 }
8745
8746 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
8747 {
8748 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
8749 return error_expr;
8750 }
8751
8752 assoc.expression = c_parser_expr_no_commas (parser, NULL);
8753 if (assoc.expression.value == error_mark_node)
8754 {
8755 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
8756 return error_expr;
8757 }
8758
8759 for (ix = 0; associations.iterate (ix, &iter); ++ix)
8760 {
8761 if (assoc.type == NULL_TREE)
8762 {
8763 if (iter->type == NULL_TREE)
8764 {
8765 error_at (assoc.type_location,
8766 "duplicate %<default%> case in %<_Generic%>");
8767 inform (iter->type_location, "original %<default%> is here");
8768 }
8769 }
8770 else if (iter->type != NULL_TREE)
8771 {
8772 if (comptypes (assoc.type, iter->type))
8773 {
8774 error_at (assoc.type_location,
8775 "%<_Generic%> specifies two compatible types");
8776 inform (iter->type_location, "compatible type is here");
8777 }
8778 }
8779 }
8780
8781 if (assoc.type == NULL_TREE)
8782 {
8783 if (!match_found)
8784 {
8785 matched_assoc = assoc;
8786 match_found = true;
8787 }
8788 }
8789 else if (comptypes (assoc.type, selector_type))
8790 {
8791 if (!match_found || matched_assoc.type == NULL_TREE)
8792 {
8793 matched_assoc = assoc;
8794 match_found = true;
8795 }
8796 else
8797 {
8798 error_at (assoc.type_location,
8799 "%<_Generic%> selector matches multiple associations");
8800 inform (matched_assoc.type_location,
8801 "other match is here");
8802 }
8803 }
8804
8805 associations.safe_push (assoc);
8806
8807 if (c_parser_peek_token (parser)->type != CPP_COMMA)
8808 break;
8809 c_parser_consume_token (parser);
8810 }
8811
8812 if (!parens.require_close (parser))
8813 {
8814 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
8815 return error_expr;
8816 }
8817
8818 if (!match_found)
8819 {
8820 error_at (selector_loc, "%<_Generic%> selector of type %qT is not "
8821 "compatible with any association",
8822 selector_type);
8823 return error_expr;
8824 }
8825
8826 return matched_assoc.expression;
8827 }
8828
8829 /* Check the validity of a function pointer argument *EXPR (argument
8830 position POS) to __builtin_tgmath. Return the number of function
8831 arguments if possibly valid; return 0 having reported an error if
8832 not valid. */
8833
8834 static unsigned int
8835 check_tgmath_function (c_expr *expr, unsigned int pos)
8836 {
8837 tree type = TREE_TYPE (expr->value);
8838 if (!FUNCTION_POINTER_TYPE_P (type))
8839 {
8840 error_at (expr->get_location (),
8841 "argument %u of %<__builtin_tgmath%> is not a function pointer",
8842 pos);
8843 return 0;
8844 }
8845 type = TREE_TYPE (type);
8846 if (!prototype_p (type))
8847 {
8848 error_at (expr->get_location (),
8849 "argument %u of %<__builtin_tgmath%> is unprototyped", pos);
8850 return 0;
8851 }
8852 if (stdarg_p (type))
8853 {
8854 error_at (expr->get_location (),
8855 "argument %u of %<__builtin_tgmath%> has variable arguments",
8856 pos);
8857 return 0;
8858 }
8859 unsigned int nargs = 0;
8860 function_args_iterator iter;
8861 tree t;
8862 FOREACH_FUNCTION_ARGS (type, t, iter)
8863 {
8864 if (t == void_type_node)
8865 break;
8866 nargs++;
8867 }
8868 if (nargs == 0)
8869 {
8870 error_at (expr->get_location (),
8871 "argument %u of %<__builtin_tgmath%> has no arguments", pos);
8872 return 0;
8873 }
8874 return nargs;
8875 }
8876
8877 /* Ways in which a parameter or return value of a type-generic macro
8878 may vary between the different functions the macro may call. */
8879 enum tgmath_parm_kind
8880 {
8881 tgmath_fixed, tgmath_real, tgmath_complex
8882 };
8883
8884 /* Helper function for c_parser_postfix_expression. Parse predefined
8885 identifiers. */
8886
8887 static struct c_expr
8888 c_parser_predefined_identifier (c_parser *parser)
8889 {
8890 location_t loc = c_parser_peek_token (parser)->location;
8891 switch (c_parser_peek_token (parser)->keyword)
8892 {
8893 case RID_FUNCTION_NAME:
8894 pedwarn (loc, OPT_Wpedantic, "ISO C does not support %qs predefined "
8895 "identifier", "__FUNCTION__");
8896 break;
8897 case RID_PRETTY_FUNCTION_NAME:
8898 pedwarn (loc, OPT_Wpedantic, "ISO C does not support %qs predefined "
8899 "identifier", "__PRETTY_FUNCTION__");
8900 break;
8901 case RID_C99_FUNCTION_NAME:
8902 pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not support "
8903 "%<__func__%> predefined identifier");
8904 break;
8905 default:
8906 gcc_unreachable ();
8907 }
8908
8909 struct c_expr expr;
8910 expr.original_code = ERROR_MARK;
8911 expr.original_type = NULL;
8912 expr.value = fname_decl (loc, c_parser_peek_token (parser)->keyword,
8913 c_parser_peek_token (parser)->value);
8914 set_c_expr_source_range (&expr, loc, loc);
8915 c_parser_consume_token (parser);
8916 return expr;
8917 }
8918
8919 /* Parse a postfix expression (C90 6.3.1-6.3.2, C99 6.5.1-6.5.2,
8920 C11 6.5.1-6.5.2). Compound literals aren't handled here; callers have to
8921 call c_parser_postfix_expression_after_paren_type on encountering them.
8922
8923 postfix-expression:
8924 primary-expression
8925 postfix-expression [ expression ]
8926 postfix-expression ( argument-expression-list[opt] )
8927 postfix-expression . identifier
8928 postfix-expression -> identifier
8929 postfix-expression ++
8930 postfix-expression --
8931 ( type-name ) { initializer-list }
8932 ( type-name ) { initializer-list , }
8933
8934 argument-expression-list:
8935 argument-expression
8936 argument-expression-list , argument-expression
8937
8938 primary-expression:
8939 identifier
8940 constant
8941 string-literal
8942 ( expression )
8943 generic-selection
8944
8945 GNU extensions:
8946
8947 primary-expression:
8948 __func__
8949 (treated as a keyword in GNU C)
8950 __FUNCTION__
8951 __PRETTY_FUNCTION__
8952 ( compound-statement )
8953 __builtin_va_arg ( assignment-expression , type-name )
8954 __builtin_offsetof ( type-name , offsetof-member-designator )
8955 __builtin_choose_expr ( assignment-expression ,
8956 assignment-expression ,
8957 assignment-expression )
8958 __builtin_types_compatible_p ( type-name , type-name )
8959 __builtin_tgmath ( expr-list )
8960 __builtin_complex ( assignment-expression , assignment-expression )
8961 __builtin_shuffle ( assignment-expression , assignment-expression )
8962 __builtin_shuffle ( assignment-expression ,
8963 assignment-expression ,
8964 assignment-expression, )
8965 __builtin_convertvector ( assignment-expression , type-name )
8966
8967 offsetof-member-designator:
8968 identifier
8969 offsetof-member-designator . identifier
8970 offsetof-member-designator [ expression ]
8971
8972 Objective-C:
8973
8974 primary-expression:
8975 [ objc-receiver objc-message-args ]
8976 @selector ( objc-selector-arg )
8977 @protocol ( identifier )
8978 @encode ( type-name )
8979 objc-string-literal
8980 Classname . identifier
8981 */
8982
8983 static struct c_expr
8984 c_parser_postfix_expression (c_parser *parser)
8985 {
8986 struct c_expr expr, e1;
8987 struct c_type_name *t1, *t2;
8988 location_t loc = c_parser_peek_token (parser)->location;
8989 source_range tok_range = c_parser_peek_token (parser)->get_range ();
8990 expr.original_code = ERROR_MARK;
8991 expr.original_type = NULL;
8992 switch (c_parser_peek_token (parser)->type)
8993 {
8994 case CPP_NUMBER:
8995 expr.value = c_parser_peek_token (parser)->value;
8996 set_c_expr_source_range (&expr, tok_range);
8997 loc = c_parser_peek_token (parser)->location;
8998 c_parser_consume_token (parser);
8999 if (TREE_CODE (expr.value) == FIXED_CST
9000 && !targetm.fixed_point_supported_p ())
9001 {
9002 error_at (loc, "fixed-point types not supported for this target");
9003 expr.set_error ();
9004 }
9005 break;
9006 case CPP_CHAR:
9007 case CPP_CHAR16:
9008 case CPP_CHAR32:
9009 case CPP_UTF8CHAR:
9010 case CPP_WCHAR:
9011 expr.value = c_parser_peek_token (parser)->value;
9012 /* For the purpose of warning when a pointer is compared with
9013 a zero character constant. */
9014 expr.original_type = char_type_node;
9015 set_c_expr_source_range (&expr, tok_range);
9016 c_parser_consume_token (parser);
9017 break;
9018 case CPP_STRING:
9019 case CPP_STRING16:
9020 case CPP_STRING32:
9021 case CPP_WSTRING:
9022 case CPP_UTF8STRING:
9023 expr = c_parser_string_literal (parser, parser->translate_strings_p,
9024 true);
9025 break;
9026 case CPP_OBJC_STRING:
9027 gcc_assert (c_dialect_objc ());
9028 expr.value
9029 = objc_build_string_object (c_parser_peek_token (parser)->value);
9030 set_c_expr_source_range (&expr, tok_range);
9031 c_parser_consume_token (parser);
9032 break;
9033 case CPP_NAME:
9034 switch (c_parser_peek_token (parser)->id_kind)
9035 {
9036 case C_ID_ID:
9037 {
9038 tree id = c_parser_peek_token (parser)->value;
9039 c_parser_consume_token (parser);
9040 expr.value = build_external_ref (loc, id,
9041 (c_parser_peek_token (parser)->type
9042 == CPP_OPEN_PAREN),
9043 &expr.original_type);
9044 set_c_expr_source_range (&expr, tok_range);
9045 break;
9046 }
9047 case C_ID_CLASSNAME:
9048 {
9049 /* Here we parse the Objective-C 2.0 Class.name dot
9050 syntax. */
9051 tree class_name = c_parser_peek_token (parser)->value;
9052 tree component;
9053 c_parser_consume_token (parser);
9054 gcc_assert (c_dialect_objc ());
9055 if (!c_parser_require (parser, CPP_DOT, "expected %<.%>"))
9056 {
9057 expr.set_error ();
9058 break;
9059 }
9060 if (c_parser_next_token_is_not (parser, CPP_NAME))
9061 {
9062 c_parser_error (parser, "expected identifier");
9063 expr.set_error ();
9064 break;
9065 }
9066 c_token *component_tok = c_parser_peek_token (parser);
9067 component = component_tok->value;
9068 location_t end_loc = component_tok->get_finish ();
9069 c_parser_consume_token (parser);
9070 expr.value = objc_build_class_component_ref (class_name,
9071 component);
9072 set_c_expr_source_range (&expr, loc, end_loc);
9073 break;
9074 }
9075 default:
9076 c_parser_error (parser, "expected expression");
9077 expr.set_error ();
9078 break;
9079 }
9080 break;
9081 case CPP_OPEN_PAREN:
9082 /* A parenthesized expression, statement expression or compound
9083 literal. */
9084 if (c_parser_peek_2nd_token (parser)->type == CPP_OPEN_BRACE)
9085 {
9086 /* A statement expression. */
9087 tree stmt;
9088 location_t brace_loc;
9089 c_parser_consume_token (parser);
9090 brace_loc = c_parser_peek_token (parser)->location;
9091 c_parser_consume_token (parser);
9092 /* If we've not yet started the current function's statement list,
9093 or we're in the parameter scope of an old-style function
9094 declaration, statement expressions are not allowed. */
9095 if (!building_stmt_list_p () || old_style_parameter_scope ())
9096 {
9097 error_at (loc, "braced-group within expression allowed "
9098 "only inside a function");
9099 parser->error = true;
9100 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
9101 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
9102 expr.set_error ();
9103 break;
9104 }
9105 stmt = c_begin_stmt_expr ();
9106 c_parser_compound_statement_nostart (parser);
9107 location_t close_loc = c_parser_peek_token (parser)->location;
9108 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
9109 "expected %<)%>");
9110 pedwarn (loc, OPT_Wpedantic,
9111 "ISO C forbids braced-groups within expressions");
9112 expr.value = c_finish_stmt_expr (brace_loc, stmt);
9113 set_c_expr_source_range (&expr, loc, close_loc);
9114 mark_exp_read (expr.value);
9115 }
9116 else
9117 {
9118 /* A parenthesized expression. */
9119 location_t loc_open_paren = c_parser_peek_token (parser)->location;
9120 c_parser_consume_token (parser);
9121 expr = c_parser_expression (parser);
9122 if (TREE_CODE (expr.value) == MODIFY_EXPR)
9123 TREE_NO_WARNING (expr.value) = 1;
9124 if (expr.original_code != C_MAYBE_CONST_EXPR
9125 && expr.original_code != SIZEOF_EXPR)
9126 expr.original_code = ERROR_MARK;
9127 /* Don't change EXPR.ORIGINAL_TYPE. */
9128 location_t loc_close_paren = c_parser_peek_token (parser)->location;
9129 set_c_expr_source_range (&expr, loc_open_paren, loc_close_paren);
9130 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
9131 "expected %<)%>", loc_open_paren);
9132 }
9133 break;
9134 case CPP_KEYWORD:
9135 switch (c_parser_peek_token (parser)->keyword)
9136 {
9137 case RID_FUNCTION_NAME:
9138 case RID_PRETTY_FUNCTION_NAME:
9139 case RID_C99_FUNCTION_NAME:
9140 expr = c_parser_predefined_identifier (parser);
9141 break;
9142 case RID_VA_ARG:
9143 {
9144 location_t start_loc = loc;
9145 c_parser_consume_token (parser);
9146 matching_parens parens;
9147 if (!parens.require_open (parser))
9148 {
9149 expr.set_error ();
9150 break;
9151 }
9152 e1 = c_parser_expr_no_commas (parser, NULL);
9153 mark_exp_read (e1.value);
9154 e1.value = c_fully_fold (e1.value, false, NULL);
9155 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
9156 {
9157 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
9158 expr.set_error ();
9159 break;
9160 }
9161 loc = c_parser_peek_token (parser)->location;
9162 t1 = c_parser_type_name (parser);
9163 location_t end_loc = c_parser_peek_token (parser)->get_finish ();
9164 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
9165 "expected %<)%>");
9166 if (t1 == NULL)
9167 {
9168 expr.set_error ();
9169 }
9170 else
9171 {
9172 tree type_expr = NULL_TREE;
9173 expr.value = c_build_va_arg (start_loc, e1.value, loc,
9174 groktypename (t1, &type_expr, NULL));
9175 if (type_expr)
9176 {
9177 expr.value = build2 (C_MAYBE_CONST_EXPR,
9178 TREE_TYPE (expr.value), type_expr,
9179 expr.value);
9180 C_MAYBE_CONST_EXPR_NON_CONST (expr.value) = true;
9181 }
9182 set_c_expr_source_range (&expr, start_loc, end_loc);
9183 }
9184 }
9185 break;
9186 case RID_OFFSETOF:
9187 {
9188 c_parser_consume_token (parser);
9189 matching_parens parens;
9190 if (!parens.require_open (parser))
9191 {
9192 expr.set_error ();
9193 break;
9194 }
9195 t1 = c_parser_type_name (parser);
9196 if (t1 == NULL)
9197 parser->error = true;
9198 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
9199 gcc_assert (parser->error);
9200 if (parser->error)
9201 {
9202 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
9203 expr.set_error ();
9204 break;
9205 }
9206 tree type = groktypename (t1, NULL, NULL);
9207 tree offsetof_ref;
9208 if (type == error_mark_node)
9209 offsetof_ref = error_mark_node;
9210 else
9211 {
9212 offsetof_ref = build1 (INDIRECT_REF, type, null_pointer_node);
9213 SET_EXPR_LOCATION (offsetof_ref, loc);
9214 }
9215 /* Parse the second argument to __builtin_offsetof. We
9216 must have one identifier, and beyond that we want to
9217 accept sub structure and sub array references. */
9218 if (c_parser_next_token_is (parser, CPP_NAME))
9219 {
9220 c_token *comp_tok = c_parser_peek_token (parser);
9221 offsetof_ref = build_component_ref
9222 (loc, offsetof_ref, comp_tok->value, comp_tok->location);
9223 c_parser_consume_token (parser);
9224 while (c_parser_next_token_is (parser, CPP_DOT)
9225 || c_parser_next_token_is (parser,
9226 CPP_OPEN_SQUARE)
9227 || c_parser_next_token_is (parser,
9228 CPP_DEREF))
9229 {
9230 if (c_parser_next_token_is (parser, CPP_DEREF))
9231 {
9232 loc = c_parser_peek_token (parser)->location;
9233 offsetof_ref = build_array_ref (loc,
9234 offsetof_ref,
9235 integer_zero_node);
9236 goto do_dot;
9237 }
9238 else if (c_parser_next_token_is (parser, CPP_DOT))
9239 {
9240 do_dot:
9241 c_parser_consume_token (parser);
9242 if (c_parser_next_token_is_not (parser,
9243 CPP_NAME))
9244 {
9245 c_parser_error (parser, "expected identifier");
9246 break;
9247 }
9248 c_token *comp_tok = c_parser_peek_token (parser);
9249 offsetof_ref = build_component_ref
9250 (loc, offsetof_ref, comp_tok->value,
9251 comp_tok->location);
9252 c_parser_consume_token (parser);
9253 }
9254 else
9255 {
9256 struct c_expr ce;
9257 tree idx;
9258 loc = c_parser_peek_token (parser)->location;
9259 c_parser_consume_token (parser);
9260 ce = c_parser_expression (parser);
9261 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
9262 idx = ce.value;
9263 idx = c_fully_fold (idx, false, NULL);
9264 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
9265 "expected %<]%>");
9266 offsetof_ref = build_array_ref (loc, offsetof_ref, idx);
9267 }
9268 }
9269 }
9270 else
9271 c_parser_error (parser, "expected identifier");
9272 location_t end_loc = c_parser_peek_token (parser)->get_finish ();
9273 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
9274 "expected %<)%>");
9275 expr.value = fold_offsetof (offsetof_ref);
9276 set_c_expr_source_range (&expr, loc, end_loc);
9277 }
9278 break;
9279 case RID_CHOOSE_EXPR:
9280 {
9281 vec<c_expr_t, va_gc> *cexpr_list;
9282 c_expr_t *e1_p, *e2_p, *e3_p;
9283 tree c;
9284 location_t close_paren_loc;
9285
9286 c_parser_consume_token (parser);
9287 if (!c_parser_get_builtin_args (parser,
9288 "__builtin_choose_expr",
9289 &cexpr_list, true,
9290 &close_paren_loc))
9291 {
9292 expr.set_error ();
9293 break;
9294 }
9295
9296 if (vec_safe_length (cexpr_list) != 3)
9297 {
9298 error_at (loc, "wrong number of arguments to "
9299 "%<__builtin_choose_expr%>");
9300 expr.set_error ();
9301 break;
9302 }
9303
9304 e1_p = &(*cexpr_list)[0];
9305 e2_p = &(*cexpr_list)[1];
9306 e3_p = &(*cexpr_list)[2];
9307
9308 c = e1_p->value;
9309 mark_exp_read (e2_p->value);
9310 mark_exp_read (e3_p->value);
9311 if (TREE_CODE (c) != INTEGER_CST
9312 || !INTEGRAL_TYPE_P (TREE_TYPE (c)))
9313 error_at (loc,
9314 "first argument to %<__builtin_choose_expr%> not"
9315 " a constant");
9316 constant_expression_warning (c);
9317 expr = integer_zerop (c) ? *e3_p : *e2_p;
9318 set_c_expr_source_range (&expr, loc, close_paren_loc);
9319 break;
9320 }
9321 case RID_TYPES_COMPATIBLE_P:
9322 {
9323 c_parser_consume_token (parser);
9324 matching_parens parens;
9325 if (!parens.require_open (parser))
9326 {
9327 expr.set_error ();
9328 break;
9329 }
9330 t1 = c_parser_type_name (parser);
9331 if (t1 == NULL)
9332 {
9333 expr.set_error ();
9334 break;
9335 }
9336 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
9337 {
9338 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
9339 expr.set_error ();
9340 break;
9341 }
9342 t2 = c_parser_type_name (parser);
9343 if (t2 == NULL)
9344 {
9345 expr.set_error ();
9346 break;
9347 }
9348 location_t close_paren_loc = c_parser_peek_token (parser)->location;
9349 parens.skip_until_found_close (parser);
9350 tree e1, e2;
9351 e1 = groktypename (t1, NULL, NULL);
9352 e2 = groktypename (t2, NULL, NULL);
9353 if (e1 == error_mark_node || e2 == error_mark_node)
9354 {
9355 expr.set_error ();
9356 break;
9357 }
9358
9359 e1 = TYPE_MAIN_VARIANT (e1);
9360 e2 = TYPE_MAIN_VARIANT (e2);
9361
9362 expr.value
9363 = comptypes (e1, e2) ? integer_one_node : integer_zero_node;
9364 set_c_expr_source_range (&expr, loc, close_paren_loc);
9365 }
9366 break;
9367 case RID_BUILTIN_TGMATH:
9368 {
9369 vec<c_expr_t, va_gc> *cexpr_list;
9370 location_t close_paren_loc;
9371
9372 c_parser_consume_token (parser);
9373 if (!c_parser_get_builtin_args (parser,
9374 "__builtin_tgmath",
9375 &cexpr_list, false,
9376 &close_paren_loc))
9377 {
9378 expr.set_error ();
9379 break;
9380 }
9381
9382 if (vec_safe_length (cexpr_list) < 3)
9383 {
9384 error_at (loc, "too few arguments to %<__builtin_tgmath%>");
9385 expr.set_error ();
9386 break;
9387 }
9388
9389 unsigned int i;
9390 c_expr_t *p;
9391 FOR_EACH_VEC_ELT (*cexpr_list, i, p)
9392 *p = convert_lvalue_to_rvalue (loc, *p, true, true);
9393 unsigned int nargs = check_tgmath_function (&(*cexpr_list)[0], 1);
9394 if (nargs == 0)
9395 {
9396 expr.set_error ();
9397 break;
9398 }
9399 if (vec_safe_length (cexpr_list) < nargs)
9400 {
9401 error_at (loc, "too few arguments to %<__builtin_tgmath%>");
9402 expr.set_error ();
9403 break;
9404 }
9405 unsigned int num_functions = vec_safe_length (cexpr_list) - nargs;
9406 if (num_functions < 2)
9407 {
9408 error_at (loc, "too few arguments to %<__builtin_tgmath%>");
9409 expr.set_error ();
9410 break;
9411 }
9412
9413 /* The first NUM_FUNCTIONS expressions are the function
9414 pointers. The remaining NARGS expressions are the
9415 arguments that are to be passed to one of those
9416 functions, chosen following <tgmath.h> rules. */
9417 for (unsigned int j = 1; j < num_functions; j++)
9418 {
9419 unsigned int this_nargs
9420 = check_tgmath_function (&(*cexpr_list)[j], j + 1);
9421 if (this_nargs == 0)
9422 {
9423 expr.set_error ();
9424 goto out;
9425 }
9426 if (this_nargs != nargs)
9427 {
9428 error_at ((*cexpr_list)[j].get_location (),
9429 "argument %u of %<__builtin_tgmath%> has "
9430 "wrong number of arguments", j + 1);
9431 expr.set_error ();
9432 goto out;
9433 }
9434 }
9435
9436 /* The functions all have the same number of arguments.
9437 Determine whether arguments and return types vary in
9438 ways permitted for <tgmath.h> functions. */
9439 /* The first entry in each of these vectors is for the
9440 return type, subsequent entries for parameter
9441 types. */
9442 auto_vec<enum tgmath_parm_kind> parm_kind (nargs + 1);
9443 auto_vec<tree> parm_first (nargs + 1);
9444 auto_vec<bool> parm_complex (nargs + 1);
9445 auto_vec<bool> parm_varies (nargs + 1);
9446 tree first_type = TREE_TYPE (TREE_TYPE ((*cexpr_list)[0].value));
9447 tree first_ret = TYPE_MAIN_VARIANT (TREE_TYPE (first_type));
9448 parm_first.quick_push (first_ret);
9449 parm_complex.quick_push (TREE_CODE (first_ret) == COMPLEX_TYPE);
9450 parm_varies.quick_push (false);
9451 function_args_iterator iter;
9452 tree t;
9453 unsigned int argpos;
9454 FOREACH_FUNCTION_ARGS (first_type, t, iter)
9455 {
9456 if (t == void_type_node)
9457 break;
9458 parm_first.quick_push (TYPE_MAIN_VARIANT (t));
9459 parm_complex.quick_push (TREE_CODE (t) == COMPLEX_TYPE);
9460 parm_varies.quick_push (false);
9461 }
9462 for (unsigned int j = 1; j < num_functions; j++)
9463 {
9464 tree type = TREE_TYPE (TREE_TYPE ((*cexpr_list)[j].value));
9465 tree ret = TYPE_MAIN_VARIANT (TREE_TYPE (type));
9466 if (ret != parm_first[0])
9467 {
9468 parm_varies[0] = true;
9469 if (!SCALAR_FLOAT_TYPE_P (parm_first[0])
9470 && !COMPLEX_FLOAT_TYPE_P (parm_first[0]))
9471 {
9472 error_at ((*cexpr_list)[0].get_location (),
9473 "invalid type-generic return type for "
9474 "argument %u of %<__builtin_tgmath%>",
9475 1);
9476 expr.set_error ();
9477 goto out;
9478 }
9479 if (!SCALAR_FLOAT_TYPE_P (ret)
9480 && !COMPLEX_FLOAT_TYPE_P (ret))
9481 {
9482 error_at ((*cexpr_list)[j].get_location (),
9483 "invalid type-generic return type for "
9484 "argument %u of %<__builtin_tgmath%>",
9485 j + 1);
9486 expr.set_error ();
9487 goto out;
9488 }
9489 }
9490 if (TREE_CODE (ret) == COMPLEX_TYPE)
9491 parm_complex[0] = true;
9492 argpos = 1;
9493 FOREACH_FUNCTION_ARGS (type, t, iter)
9494 {
9495 if (t == void_type_node)
9496 break;
9497 t = TYPE_MAIN_VARIANT (t);
9498 if (t != parm_first[argpos])
9499 {
9500 parm_varies[argpos] = true;
9501 if (!SCALAR_FLOAT_TYPE_P (parm_first[argpos])
9502 && !COMPLEX_FLOAT_TYPE_P (parm_first[argpos]))
9503 {
9504 error_at ((*cexpr_list)[0].get_location (),
9505 "invalid type-generic type for "
9506 "argument %u of argument %u of "
9507 "%<__builtin_tgmath%>", argpos, 1);
9508 expr.set_error ();
9509 goto out;
9510 }
9511 if (!SCALAR_FLOAT_TYPE_P (t)
9512 && !COMPLEX_FLOAT_TYPE_P (t))
9513 {
9514 error_at ((*cexpr_list)[j].get_location (),
9515 "invalid type-generic type for "
9516 "argument %u of argument %u of "
9517 "%<__builtin_tgmath%>", argpos, j + 1);
9518 expr.set_error ();
9519 goto out;
9520 }
9521 }
9522 if (TREE_CODE (t) == COMPLEX_TYPE)
9523 parm_complex[argpos] = true;
9524 argpos++;
9525 }
9526 }
9527 enum tgmath_parm_kind max_variation = tgmath_fixed;
9528 for (unsigned int j = 0; j <= nargs; j++)
9529 {
9530 enum tgmath_parm_kind this_kind;
9531 if (parm_varies[j])
9532 {
9533 if (parm_complex[j])
9534 max_variation = this_kind = tgmath_complex;
9535 else
9536 {
9537 this_kind = tgmath_real;
9538 if (max_variation != tgmath_complex)
9539 max_variation = tgmath_real;
9540 }
9541 }
9542 else
9543 this_kind = tgmath_fixed;
9544 parm_kind.quick_push (this_kind);
9545 }
9546 if (max_variation == tgmath_fixed)
9547 {
9548 error_at (loc, "function arguments of %<__builtin_tgmath%> "
9549 "all have the same type");
9550 expr.set_error ();
9551 break;
9552 }
9553
9554 /* Identify a parameter (not the return type) that varies,
9555 including with complex types if any variation includes
9556 complex types; there must be at least one such
9557 parameter. */
9558 unsigned int tgarg = 0;
9559 for (unsigned int j = 1; j <= nargs; j++)
9560 if (parm_kind[j] == max_variation)
9561 {
9562 tgarg = j;
9563 break;
9564 }
9565 if (tgarg == 0)
9566 {
9567 error_at (loc, "function arguments of %<__builtin_tgmath%> "
9568 "lack type-generic parameter");
9569 expr.set_error ();
9570 break;
9571 }
9572
9573 /* Determine the type of the relevant parameter for each
9574 function. */
9575 auto_vec<tree> tg_type (num_functions);
9576 for (unsigned int j = 0; j < num_functions; j++)
9577 {
9578 tree type = TREE_TYPE (TREE_TYPE ((*cexpr_list)[j].value));
9579 argpos = 1;
9580 FOREACH_FUNCTION_ARGS (type, t, iter)
9581 {
9582 if (argpos == tgarg)
9583 {
9584 tg_type.quick_push (TYPE_MAIN_VARIANT (t));
9585 break;
9586 }
9587 argpos++;
9588 }
9589 }
9590
9591 /* Verify that the corresponding types are different for
9592 all the listed functions. Also determine whether all
9593 the types are complex, whether all the types are
9594 standard or binary, and whether all the types are
9595 decimal. */
9596 bool all_complex = true;
9597 bool all_binary = true;
9598 bool all_decimal = true;
9599 hash_set<tree> tg_types;
9600 FOR_EACH_VEC_ELT (tg_type, i, t)
9601 {
9602 if (TREE_CODE (t) == COMPLEX_TYPE)
9603 all_decimal = false;
9604 else
9605 {
9606 all_complex = false;
9607 if (DECIMAL_FLOAT_TYPE_P (t))
9608 all_binary = false;
9609 else
9610 all_decimal = false;
9611 }
9612 if (tg_types.add (t))
9613 {
9614 error_at ((*cexpr_list)[i].get_location (),
9615 "duplicate type-generic parameter type for "
9616 "function argument %u of %<__builtin_tgmath%>",
9617 i + 1);
9618 expr.set_error ();
9619 goto out;
9620 }
9621 }
9622
9623 /* Verify that other parameters and the return type whose
9624 types vary have their types varying in the correct
9625 way. */
9626 for (unsigned int j = 0; j < num_functions; j++)
9627 {
9628 tree exp_type = tg_type[j];
9629 tree exp_real_type = exp_type;
9630 if (TREE_CODE (exp_type) == COMPLEX_TYPE)
9631 exp_real_type = TREE_TYPE (exp_type);
9632 tree type = TREE_TYPE (TREE_TYPE ((*cexpr_list)[j].value));
9633 tree ret = TYPE_MAIN_VARIANT (TREE_TYPE (type));
9634 if ((parm_kind[0] == tgmath_complex && ret != exp_type)
9635 || (parm_kind[0] == tgmath_real && ret != exp_real_type))
9636 {
9637 error_at ((*cexpr_list)[j].get_location (),
9638 "bad return type for function argument %u "
9639 "of %<__builtin_tgmath%>", j + 1);
9640 expr.set_error ();
9641 goto out;
9642 }
9643 argpos = 1;
9644 FOREACH_FUNCTION_ARGS (type, t, iter)
9645 {
9646 if (t == void_type_node)
9647 break;
9648 t = TYPE_MAIN_VARIANT (t);
9649 if ((parm_kind[argpos] == tgmath_complex
9650 && t != exp_type)
9651 || (parm_kind[argpos] == tgmath_real
9652 && t != exp_real_type))
9653 {
9654 error_at ((*cexpr_list)[j].get_location (),
9655 "bad type for argument %u of "
9656 "function argument %u of "
9657 "%<__builtin_tgmath%>", argpos, j + 1);
9658 expr.set_error ();
9659 goto out;
9660 }
9661 argpos++;
9662 }
9663 }
9664
9665 /* The functions listed are a valid set of functions for a
9666 <tgmath.h> macro to select between. Identify the
9667 matching function, if any. First, the argument types
9668 must be combined following <tgmath.h> rules. Integer
9669 types are treated as _Decimal64 if any type-generic
9670 argument is decimal, or if the only alternatives for
9671 type-generic arguments are of decimal types, and are
9672 otherwise treated as double (or _Complex double for
9673 complex integer types, or _Float64 or _Complex _Float64
9674 if all the return types are the same _FloatN or
9675 _FloatNx type). After that adjustment, types are
9676 combined following the usual arithmetic conversions.
9677 If the function only accepts complex arguments, a
9678 complex type is produced. */
9679 bool arg_complex = all_complex;
9680 bool arg_binary = all_binary;
9681 bool arg_int_decimal = all_decimal;
9682 for (unsigned int j = 1; j <= nargs; j++)
9683 {
9684 if (parm_kind[j] == tgmath_fixed)
9685 continue;
9686 c_expr_t *ce = &(*cexpr_list)[num_functions + j - 1];
9687 tree type = TREE_TYPE (ce->value);
9688 if (!INTEGRAL_TYPE_P (type)
9689 && !SCALAR_FLOAT_TYPE_P (type)
9690 && TREE_CODE (type) != COMPLEX_TYPE)
9691 {
9692 error_at (ce->get_location (),
9693 "invalid type of argument %u of type-generic "
9694 "function", j);
9695 expr.set_error ();
9696 goto out;
9697 }
9698 if (DECIMAL_FLOAT_TYPE_P (type))
9699 {
9700 arg_int_decimal = true;
9701 if (all_complex)
9702 {
9703 error_at (ce->get_location (),
9704 "decimal floating-point argument %u to "
9705 "complex-only type-generic function", j);
9706 expr.set_error ();
9707 goto out;
9708 }
9709 else if (all_binary)
9710 {
9711 error_at (ce->get_location (),
9712 "decimal floating-point argument %u to "
9713 "binary-only type-generic function", j);
9714 expr.set_error ();
9715 goto out;
9716 }
9717 else if (arg_complex)
9718 {
9719 error_at (ce->get_location (),
9720 "both complex and decimal floating-point "
9721 "arguments to type-generic function");
9722 expr.set_error ();
9723 goto out;
9724 }
9725 else if (arg_binary)
9726 {
9727 error_at (ce->get_location (),
9728 "both binary and decimal floating-point "
9729 "arguments to type-generic function");
9730 expr.set_error ();
9731 goto out;
9732 }
9733 }
9734 else if (TREE_CODE (type) == COMPLEX_TYPE)
9735 {
9736 arg_complex = true;
9737 if (COMPLEX_FLOAT_TYPE_P (type))
9738 arg_binary = true;
9739 if (all_decimal)
9740 {
9741 error_at (ce->get_location (),
9742 "complex argument %u to "
9743 "decimal-only type-generic function", j);
9744 expr.set_error ();
9745 goto out;
9746 }
9747 else if (arg_int_decimal)
9748 {
9749 error_at (ce->get_location (),
9750 "both complex and decimal floating-point "
9751 "arguments to type-generic function");
9752 expr.set_error ();
9753 goto out;
9754 }
9755 }
9756 else if (SCALAR_FLOAT_TYPE_P (type))
9757 {
9758 arg_binary = true;
9759 if (all_decimal)
9760 {
9761 error_at (ce->get_location (),
9762 "binary argument %u to "
9763 "decimal-only type-generic function", j);
9764 expr.set_error ();
9765 goto out;
9766 }
9767 else if (arg_int_decimal)
9768 {
9769 error_at (ce->get_location (),
9770 "both binary and decimal floating-point "
9771 "arguments to type-generic function");
9772 expr.set_error ();
9773 goto out;
9774 }
9775 }
9776 }
9777 /* For a macro rounding its result to a narrower type, map
9778 integer types to _Float64 not double if the return type
9779 is a _FloatN or _FloatNx type. */
9780 bool arg_int_float64 = false;
9781 if (parm_kind[0] == tgmath_fixed
9782 && SCALAR_FLOAT_TYPE_P (parm_first[0])
9783 && float64_type_node != NULL_TREE)
9784 for (unsigned int j = 0; j < NUM_FLOATN_NX_TYPES; j++)
9785 if (parm_first[0] == FLOATN_TYPE_NODE (j))
9786 {
9787 arg_int_float64 = true;
9788 break;
9789 }
9790 tree arg_real = NULL_TREE;
9791 for (unsigned int j = 1; j <= nargs; j++)
9792 {
9793 if (parm_kind[j] == tgmath_fixed)
9794 continue;
9795 c_expr_t *ce = &(*cexpr_list)[num_functions + j - 1];
9796 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (ce->value));
9797 if (TREE_CODE (type) == COMPLEX_TYPE)
9798 type = TREE_TYPE (type);
9799 if (INTEGRAL_TYPE_P (type))
9800 type = (arg_int_decimal
9801 ? dfloat64_type_node
9802 : arg_int_float64
9803 ? float64_type_node
9804 : double_type_node);
9805 if (arg_real == NULL_TREE)
9806 arg_real = type;
9807 else
9808 arg_real = common_type (arg_real, type);
9809 if (arg_real == error_mark_node)
9810 {
9811 expr.set_error ();
9812 goto out;
9813 }
9814 }
9815 tree arg_type = (arg_complex
9816 ? build_complex_type (arg_real)
9817 : arg_real);
9818
9819 /* Look for a function to call with type-generic parameter
9820 type ARG_TYPE. */
9821 c_expr_t *fn = NULL;
9822 for (unsigned int j = 0; j < num_functions; j++)
9823 {
9824 if (tg_type[j] == arg_type)
9825 {
9826 fn = &(*cexpr_list)[j];
9827 break;
9828 }
9829 }
9830 if (fn == NULL
9831 && parm_kind[0] == tgmath_fixed
9832 && SCALAR_FLOAT_TYPE_P (parm_first[0]))
9833 {
9834 /* Presume this is a macro that rounds its result to a
9835 narrower type, and look for the first function with
9836 at least the range and precision of the argument
9837 type. */
9838 for (unsigned int j = 0; j < num_functions; j++)
9839 {
9840 if (arg_complex
9841 != (TREE_CODE (tg_type[j]) == COMPLEX_TYPE))
9842 continue;
9843 tree real_tg_type = (arg_complex
9844 ? TREE_TYPE (tg_type[j])
9845 : tg_type[j]);
9846 if (DECIMAL_FLOAT_TYPE_P (arg_real)
9847 != DECIMAL_FLOAT_TYPE_P (real_tg_type))
9848 continue;
9849 scalar_float_mode arg_mode
9850 = SCALAR_FLOAT_TYPE_MODE (arg_real);
9851 scalar_float_mode tg_mode
9852 = SCALAR_FLOAT_TYPE_MODE (real_tg_type);
9853 const real_format *arg_fmt = REAL_MODE_FORMAT (arg_mode);
9854 const real_format *tg_fmt = REAL_MODE_FORMAT (tg_mode);
9855 if (arg_fmt->b == tg_fmt->b
9856 && arg_fmt->p <= tg_fmt->p
9857 && arg_fmt->emax <= tg_fmt->emax
9858 && (arg_fmt->emin - arg_fmt->p
9859 >= tg_fmt->emin - tg_fmt->p))
9860 {
9861 fn = &(*cexpr_list)[j];
9862 break;
9863 }
9864 }
9865 }
9866 if (fn == NULL)
9867 {
9868 error_at (loc, "no matching function for type-generic call");
9869 expr.set_error ();
9870 break;
9871 }
9872
9873 /* Construct a call to FN. */
9874 vec<tree, va_gc> *args;
9875 vec_alloc (args, nargs);
9876 vec<tree, va_gc> *origtypes;
9877 vec_alloc (origtypes, nargs);
9878 auto_vec<location_t> arg_loc (nargs);
9879 for (unsigned int j = 0; j < nargs; j++)
9880 {
9881 c_expr_t *ce = &(*cexpr_list)[num_functions + j];
9882 args->quick_push (ce->value);
9883 arg_loc.quick_push (ce->get_location ());
9884 origtypes->quick_push (ce->original_type);
9885 }
9886 expr.value = c_build_function_call_vec (loc, arg_loc, fn->value,
9887 args, origtypes);
9888 set_c_expr_source_range (&expr, loc, close_paren_loc);
9889 break;
9890 }
9891 case RID_BUILTIN_CALL_WITH_STATIC_CHAIN:
9892 {
9893 vec<c_expr_t, va_gc> *cexpr_list;
9894 c_expr_t *e2_p;
9895 tree chain_value;
9896 location_t close_paren_loc;
9897
9898 c_parser_consume_token (parser);
9899 if (!c_parser_get_builtin_args (parser,
9900 "__builtin_call_with_static_chain",
9901 &cexpr_list, false,
9902 &close_paren_loc))
9903 {
9904 expr.set_error ();
9905 break;
9906 }
9907 if (vec_safe_length (cexpr_list) != 2)
9908 {
9909 error_at (loc, "wrong number of arguments to "
9910 "%<__builtin_call_with_static_chain%>");
9911 expr.set_error ();
9912 break;
9913 }
9914
9915 expr = (*cexpr_list)[0];
9916 e2_p = &(*cexpr_list)[1];
9917 *e2_p = convert_lvalue_to_rvalue (loc, *e2_p, true, true);
9918 chain_value = e2_p->value;
9919 mark_exp_read (chain_value);
9920
9921 if (TREE_CODE (expr.value) != CALL_EXPR)
9922 error_at (loc, "first argument to "
9923 "%<__builtin_call_with_static_chain%> "
9924 "must be a call expression");
9925 else if (TREE_CODE (TREE_TYPE (chain_value)) != POINTER_TYPE)
9926 error_at (loc, "second argument to "
9927 "%<__builtin_call_with_static_chain%> "
9928 "must be a pointer type");
9929 else
9930 CALL_EXPR_STATIC_CHAIN (expr.value) = chain_value;
9931 set_c_expr_source_range (&expr, loc, close_paren_loc);
9932 break;
9933 }
9934 case RID_BUILTIN_COMPLEX:
9935 {
9936 vec<c_expr_t, va_gc> *cexpr_list;
9937 c_expr_t *e1_p, *e2_p;
9938 location_t close_paren_loc;
9939
9940 c_parser_consume_token (parser);
9941 if (!c_parser_get_builtin_args (parser,
9942 "__builtin_complex",
9943 &cexpr_list, false,
9944 &close_paren_loc))
9945 {
9946 expr.set_error ();
9947 break;
9948 }
9949
9950 if (vec_safe_length (cexpr_list) != 2)
9951 {
9952 error_at (loc, "wrong number of arguments to "
9953 "%<__builtin_complex%>");
9954 expr.set_error ();
9955 break;
9956 }
9957
9958 e1_p = &(*cexpr_list)[0];
9959 e2_p = &(*cexpr_list)[1];
9960
9961 *e1_p = convert_lvalue_to_rvalue (loc, *e1_p, true, true);
9962 if (TREE_CODE (e1_p->value) == EXCESS_PRECISION_EXPR)
9963 e1_p->value = convert (TREE_TYPE (e1_p->value),
9964 TREE_OPERAND (e1_p->value, 0));
9965 *e2_p = convert_lvalue_to_rvalue (loc, *e2_p, true, true);
9966 if (TREE_CODE (e2_p->value) == EXCESS_PRECISION_EXPR)
9967 e2_p->value = convert (TREE_TYPE (e2_p->value),
9968 TREE_OPERAND (e2_p->value, 0));
9969 if (!SCALAR_FLOAT_TYPE_P (TREE_TYPE (e1_p->value))
9970 || DECIMAL_FLOAT_TYPE_P (TREE_TYPE (e1_p->value))
9971 || !SCALAR_FLOAT_TYPE_P (TREE_TYPE (e2_p->value))
9972 || DECIMAL_FLOAT_TYPE_P (TREE_TYPE (e2_p->value)))
9973 {
9974 error_at (loc, "%<__builtin_complex%> operand "
9975 "not of real binary floating-point type");
9976 expr.set_error ();
9977 break;
9978 }
9979 if (TYPE_MAIN_VARIANT (TREE_TYPE (e1_p->value))
9980 != TYPE_MAIN_VARIANT (TREE_TYPE (e2_p->value)))
9981 {
9982 error_at (loc,
9983 "%<__builtin_complex%> operands of different types");
9984 expr.set_error ();
9985 break;
9986 }
9987 pedwarn_c90 (loc, OPT_Wpedantic,
9988 "ISO C90 does not support complex types");
9989 expr.value = build2_loc (loc, COMPLEX_EXPR,
9990 build_complex_type
9991 (TYPE_MAIN_VARIANT
9992 (TREE_TYPE (e1_p->value))),
9993 e1_p->value, e2_p->value);
9994 set_c_expr_source_range (&expr, loc, close_paren_loc);
9995 break;
9996 }
9997 case RID_BUILTIN_SHUFFLE:
9998 {
9999 vec<c_expr_t, va_gc> *cexpr_list;
10000 unsigned int i;
10001 c_expr_t *p;
10002 location_t close_paren_loc;
10003
10004 c_parser_consume_token (parser);
10005 if (!c_parser_get_builtin_args (parser,
10006 "__builtin_shuffle",
10007 &cexpr_list, false,
10008 &close_paren_loc))
10009 {
10010 expr.set_error ();
10011 break;
10012 }
10013
10014 FOR_EACH_VEC_SAFE_ELT (cexpr_list, i, p)
10015 *p = convert_lvalue_to_rvalue (loc, *p, true, true);
10016
10017 if (vec_safe_length (cexpr_list) == 2)
10018 expr.value = c_build_vec_perm_expr (loc, (*cexpr_list)[0].value,
10019 NULL_TREE,
10020 (*cexpr_list)[1].value);
10021
10022 else if (vec_safe_length (cexpr_list) == 3)
10023 expr.value = c_build_vec_perm_expr (loc, (*cexpr_list)[0].value,
10024 (*cexpr_list)[1].value,
10025 (*cexpr_list)[2].value);
10026 else
10027 {
10028 error_at (loc, "wrong number of arguments to "
10029 "%<__builtin_shuffle%>");
10030 expr.set_error ();
10031 }
10032 set_c_expr_source_range (&expr, loc, close_paren_loc);
10033 break;
10034 }
10035 case RID_BUILTIN_CONVERTVECTOR:
10036 {
10037 location_t start_loc = loc;
10038 c_parser_consume_token (parser);
10039 matching_parens parens;
10040 if (!parens.require_open (parser))
10041 {
10042 expr.set_error ();
10043 break;
10044 }
10045 e1 = c_parser_expr_no_commas (parser, NULL);
10046 mark_exp_read (e1.value);
10047 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
10048 {
10049 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
10050 expr.set_error ();
10051 break;
10052 }
10053 loc = c_parser_peek_token (parser)->location;
10054 t1 = c_parser_type_name (parser);
10055 location_t end_loc = c_parser_peek_token (parser)->get_finish ();
10056 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
10057 "expected %<)%>");
10058 if (t1 == NULL)
10059 expr.set_error ();
10060 else
10061 {
10062 tree type_expr = NULL_TREE;
10063 expr.value = c_build_vec_convert (start_loc, e1.value, loc,
10064 groktypename (t1, &type_expr,
10065 NULL));
10066 set_c_expr_source_range (&expr, start_loc, end_loc);
10067 }
10068 }
10069 break;
10070 case RID_AT_SELECTOR:
10071 {
10072 gcc_assert (c_dialect_objc ());
10073 c_parser_consume_token (parser);
10074 matching_parens parens;
10075 if (!parens.require_open (parser))
10076 {
10077 expr.set_error ();
10078 break;
10079 }
10080 tree sel = c_parser_objc_selector_arg (parser);
10081 location_t close_loc = c_parser_peek_token (parser)->location;
10082 parens.skip_until_found_close (parser);
10083 expr.value = objc_build_selector_expr (loc, sel);
10084 set_c_expr_source_range (&expr, loc, close_loc);
10085 }
10086 break;
10087 case RID_AT_PROTOCOL:
10088 {
10089 gcc_assert (c_dialect_objc ());
10090 c_parser_consume_token (parser);
10091 matching_parens parens;
10092 if (!parens.require_open (parser))
10093 {
10094 expr.set_error ();
10095 break;
10096 }
10097 if (c_parser_next_token_is_not (parser, CPP_NAME))
10098 {
10099 c_parser_error (parser, "expected identifier");
10100 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
10101 expr.set_error ();
10102 break;
10103 }
10104 tree id = c_parser_peek_token (parser)->value;
10105 c_parser_consume_token (parser);
10106 location_t close_loc = c_parser_peek_token (parser)->location;
10107 parens.skip_until_found_close (parser);
10108 expr.value = objc_build_protocol_expr (id);
10109 set_c_expr_source_range (&expr, loc, close_loc);
10110 }
10111 break;
10112 case RID_AT_ENCODE:
10113 {
10114 /* Extension to support C-structures in the archiver. */
10115 gcc_assert (c_dialect_objc ());
10116 c_parser_consume_token (parser);
10117 matching_parens parens;
10118 if (!parens.require_open (parser))
10119 {
10120 expr.set_error ();
10121 break;
10122 }
10123 t1 = c_parser_type_name (parser);
10124 if (t1 == NULL)
10125 {
10126 expr.set_error ();
10127 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
10128 break;
10129 }
10130 location_t close_loc = c_parser_peek_token (parser)->location;
10131 parens.skip_until_found_close (parser);
10132 tree type = groktypename (t1, NULL, NULL);
10133 expr.value = objc_build_encode_expr (type);
10134 set_c_expr_source_range (&expr, loc, close_loc);
10135 }
10136 break;
10137 case RID_GENERIC:
10138 expr = c_parser_generic_selection (parser);
10139 break;
10140 default:
10141 c_parser_error (parser, "expected expression");
10142 expr.set_error ();
10143 break;
10144 }
10145 break;
10146 case CPP_OPEN_SQUARE:
10147 if (c_dialect_objc ())
10148 {
10149 tree receiver, args;
10150 c_parser_consume_token (parser);
10151 receiver = c_parser_objc_receiver (parser);
10152 args = c_parser_objc_message_args (parser);
10153 location_t close_loc = c_parser_peek_token (parser)->location;
10154 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
10155 "expected %<]%>");
10156 expr.value = objc_build_message_expr (receiver, args);
10157 set_c_expr_source_range (&expr, loc, close_loc);
10158 break;
10159 }
10160 /* Else fall through to report error. */
10161 /* FALLTHRU */
10162 default:
10163 c_parser_error (parser, "expected expression");
10164 expr.set_error ();
10165 break;
10166 }
10167 out:
10168 return c_parser_postfix_expression_after_primary
10169 (parser, EXPR_LOC_OR_LOC (expr.value, loc), expr);
10170 }
10171
10172 /* Parse a postfix expression after a parenthesized type name: the
10173 brace-enclosed initializer of a compound literal, possibly followed
10174 by some postfix operators. This is separate because it is not
10175 possible to tell until after the type name whether a cast
10176 expression has a cast or a compound literal, or whether the operand
10177 of sizeof is a parenthesized type name or starts with a compound
10178 literal. TYPE_LOC is the location where TYPE_NAME starts--the
10179 location of the first token after the parentheses around the type
10180 name. */
10181
10182 static struct c_expr
10183 c_parser_postfix_expression_after_paren_type (c_parser *parser,
10184 struct c_type_name *type_name,
10185 location_t type_loc)
10186 {
10187 tree type;
10188 struct c_expr init;
10189 bool non_const;
10190 struct c_expr expr;
10191 location_t start_loc;
10192 tree type_expr = NULL_TREE;
10193 bool type_expr_const = true;
10194 check_compound_literal_type (type_loc, type_name);
10195 rich_location richloc (line_table, type_loc);
10196 start_init (NULL_TREE, NULL, 0, &richloc);
10197 type = groktypename (type_name, &type_expr, &type_expr_const);
10198 start_loc = c_parser_peek_token (parser)->location;
10199 if (type != error_mark_node && C_TYPE_VARIABLE_SIZE (type))
10200 {
10201 error_at (type_loc, "compound literal has variable size");
10202 type = error_mark_node;
10203 }
10204 init = c_parser_braced_init (parser, type, false, NULL);
10205 finish_init ();
10206 maybe_warn_string_init (type_loc, type, init);
10207
10208 if (type != error_mark_node
10209 && !ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (type))
10210 && current_function_decl)
10211 {
10212 error ("compound literal qualified by address-space qualifier");
10213 type = error_mark_node;
10214 }
10215
10216 pedwarn_c90 (start_loc, OPT_Wpedantic, "ISO C90 forbids compound literals");
10217 non_const = ((init.value && TREE_CODE (init.value) == CONSTRUCTOR)
10218 ? CONSTRUCTOR_NON_CONST (init.value)
10219 : init.original_code == C_MAYBE_CONST_EXPR);
10220 non_const |= !type_expr_const;
10221 unsigned int alignas_align = 0;
10222 if (type != error_mark_node
10223 && type_name->specs->align_log != -1)
10224 {
10225 alignas_align = 1U << type_name->specs->align_log;
10226 if (alignas_align < min_align_of_type (type))
10227 {
10228 error_at (type_name->specs->locations[cdw_alignas],
10229 "%<_Alignas%> specifiers cannot reduce "
10230 "alignment of compound literal");
10231 alignas_align = 0;
10232 }
10233 }
10234 expr.value = build_compound_literal (start_loc, type, init.value, non_const,
10235 alignas_align);
10236 set_c_expr_source_range (&expr, init.src_range);
10237 expr.original_code = ERROR_MARK;
10238 expr.original_type = NULL;
10239 if (type != error_mark_node
10240 && expr.value != error_mark_node
10241 && type_expr)
10242 {
10243 if (TREE_CODE (expr.value) == C_MAYBE_CONST_EXPR)
10244 {
10245 gcc_assert (C_MAYBE_CONST_EXPR_PRE (expr.value) == NULL_TREE);
10246 C_MAYBE_CONST_EXPR_PRE (expr.value) = type_expr;
10247 }
10248 else
10249 {
10250 gcc_assert (!non_const);
10251 expr.value = build2 (C_MAYBE_CONST_EXPR, type,
10252 type_expr, expr.value);
10253 }
10254 }
10255 return c_parser_postfix_expression_after_primary (parser, start_loc, expr);
10256 }
10257
10258 /* Callback function for sizeof_pointer_memaccess_warning to compare
10259 types. */
10260
10261 static bool
10262 sizeof_ptr_memacc_comptypes (tree type1, tree type2)
10263 {
10264 return comptypes (type1, type2) == 1;
10265 }
10266
10267 /* Warn for patterns where abs-like function appears to be used incorrectly,
10268 gracefully ignore any non-abs-like function. The warning location should
10269 be LOC. FNDECL is the declaration of called function, it must be a
10270 BUILT_IN_NORMAL function. ARG is the first and only argument of the
10271 call. */
10272
10273 static void
10274 warn_for_abs (location_t loc, tree fndecl, tree arg)
10275 {
10276 /* Avoid warning in unreachable subexpressions. */
10277 if (c_inhibit_evaluation_warnings)
10278 return;
10279
10280 tree atype = TREE_TYPE (arg);
10281
10282 /* Casts from pointers (and thus arrays and fndecls) will generate
10283 -Wint-conversion warnings. Most other wrong types hopefully lead to type
10284 mismatch errors. TODO: Think about what to do with FIXED_POINT_TYPE_P
10285 types and possibly other exotic types. */
10286 if (!INTEGRAL_TYPE_P (atype)
10287 && !SCALAR_FLOAT_TYPE_P (atype)
10288 && TREE_CODE (atype) != COMPLEX_TYPE)
10289 return;
10290
10291 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10292
10293 switch (fcode)
10294 {
10295 case BUILT_IN_ABS:
10296 case BUILT_IN_LABS:
10297 case BUILT_IN_LLABS:
10298 case BUILT_IN_IMAXABS:
10299 if (!INTEGRAL_TYPE_P (atype))
10300 {
10301 if (SCALAR_FLOAT_TYPE_P (atype))
10302 warning_at (loc, OPT_Wabsolute_value,
10303 "using integer absolute value function %qD when "
10304 "argument is of floating-point type %qT",
10305 fndecl, atype);
10306 else if (TREE_CODE (atype) == COMPLEX_TYPE)
10307 warning_at (loc, OPT_Wabsolute_value,
10308 "using integer absolute value function %qD when "
10309 "argument is of complex type %qT", fndecl, atype);
10310 else
10311 gcc_unreachable ();
10312 return;
10313 }
10314 if (TYPE_UNSIGNED (atype))
10315 warning_at (loc, OPT_Wabsolute_value,
10316 "taking the absolute value of unsigned type %qT "
10317 "has no effect", atype);
10318 break;
10319
10320 CASE_FLT_FN (BUILT_IN_FABS):
10321 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FABS):
10322 if (!SCALAR_FLOAT_TYPE_P (atype)
10323 || DECIMAL_FLOAT_MODE_P (TYPE_MODE (atype)))
10324 {
10325 if (INTEGRAL_TYPE_P (atype))
10326 warning_at (loc, OPT_Wabsolute_value,
10327 "using floating-point absolute value function %qD "
10328 "when argument is of integer type %qT", fndecl, atype);
10329 else if (DECIMAL_FLOAT_TYPE_P (atype))
10330 warning_at (loc, OPT_Wabsolute_value,
10331 "using floating-point absolute value function %qD "
10332 "when argument is of decimal floating-point type %qT",
10333 fndecl, atype);
10334 else if (TREE_CODE (atype) == COMPLEX_TYPE)
10335 warning_at (loc, OPT_Wabsolute_value,
10336 "using floating-point absolute value function %qD when "
10337 "argument is of complex type %qT", fndecl, atype);
10338 else
10339 gcc_unreachable ();
10340 return;
10341 }
10342 break;
10343
10344 CASE_FLT_FN (BUILT_IN_CABS):
10345 if (TREE_CODE (atype) != COMPLEX_TYPE)
10346 {
10347 if (INTEGRAL_TYPE_P (atype))
10348 warning_at (loc, OPT_Wabsolute_value,
10349 "using complex absolute value function %qD when "
10350 "argument is of integer type %qT", fndecl, atype);
10351 else if (SCALAR_FLOAT_TYPE_P (atype))
10352 warning_at (loc, OPT_Wabsolute_value,
10353 "using complex absolute value function %qD when "
10354 "argument is of floating-point type %qT",
10355 fndecl, atype);
10356 else
10357 gcc_unreachable ();
10358
10359 return;
10360 }
10361 break;
10362
10363 case BUILT_IN_FABSD32:
10364 case BUILT_IN_FABSD64:
10365 case BUILT_IN_FABSD128:
10366 if (!DECIMAL_FLOAT_TYPE_P (atype))
10367 {
10368 if (INTEGRAL_TYPE_P (atype))
10369 warning_at (loc, OPT_Wabsolute_value,
10370 "using decimal floating-point absolute value "
10371 "function %qD when argument is of integer type %qT",
10372 fndecl, atype);
10373 else if (SCALAR_FLOAT_TYPE_P (atype))
10374 warning_at (loc, OPT_Wabsolute_value,
10375 "using decimal floating-point absolute value "
10376 "function %qD when argument is of floating-point "
10377 "type %qT", fndecl, atype);
10378 else if (TREE_CODE (atype) == COMPLEX_TYPE)
10379 warning_at (loc, OPT_Wabsolute_value,
10380 "using decimal floating-point absolute value "
10381 "function %qD when argument is of complex type %qT",
10382 fndecl, atype);
10383 else
10384 gcc_unreachable ();
10385 return;
10386 }
10387 break;
10388
10389 default:
10390 return;
10391 }
10392
10393 if (!TYPE_ARG_TYPES (TREE_TYPE (fndecl)))
10394 return;
10395
10396 tree ftype = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
10397 if (TREE_CODE (atype) == COMPLEX_TYPE)
10398 {
10399 gcc_assert (TREE_CODE (ftype) == COMPLEX_TYPE);
10400 atype = TREE_TYPE (atype);
10401 ftype = TREE_TYPE (ftype);
10402 }
10403
10404 if (TYPE_PRECISION (ftype) < TYPE_PRECISION (atype))
10405 warning_at (loc, OPT_Wabsolute_value,
10406 "absolute value function %qD given an argument of type %qT "
10407 "but has parameter of type %qT which may cause truncation "
10408 "of value", fndecl, atype, ftype);
10409 }
10410
10411
10412 /* Parse a postfix expression after the initial primary or compound
10413 literal; that is, parse a series of postfix operators.
10414
10415 EXPR_LOC is the location of the primary expression. */
10416
10417 static struct c_expr
10418 c_parser_postfix_expression_after_primary (c_parser *parser,
10419 location_t expr_loc,
10420 struct c_expr expr)
10421 {
10422 struct c_expr orig_expr;
10423 tree ident, idx;
10424 location_t sizeof_arg_loc[3], comp_loc;
10425 tree sizeof_arg[3];
10426 unsigned int literal_zero_mask;
10427 unsigned int i;
10428 vec<tree, va_gc> *exprlist;
10429 vec<tree, va_gc> *origtypes = NULL;
10430 vec<location_t> arg_loc = vNULL;
10431 location_t start;
10432 location_t finish;
10433
10434 while (true)
10435 {
10436 location_t op_loc = c_parser_peek_token (parser)->location;
10437 switch (c_parser_peek_token (parser)->type)
10438 {
10439 case CPP_OPEN_SQUARE:
10440 /* Array reference. */
10441 c_parser_consume_token (parser);
10442 idx = c_parser_expression (parser).value;
10443 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
10444 "expected %<]%>");
10445 start = expr.get_start ();
10446 finish = parser->tokens_buf[0].location;
10447 expr.value = build_array_ref (op_loc, expr.value, idx);
10448 set_c_expr_source_range (&expr, start, finish);
10449 expr.original_code = ERROR_MARK;
10450 expr.original_type = NULL;
10451 break;
10452 case CPP_OPEN_PAREN:
10453 /* Function call. */
10454 c_parser_consume_token (parser);
10455 for (i = 0; i < 3; i++)
10456 {
10457 sizeof_arg[i] = NULL_TREE;
10458 sizeof_arg_loc[i] = UNKNOWN_LOCATION;
10459 }
10460 literal_zero_mask = 0;
10461 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
10462 exprlist = NULL;
10463 else
10464 exprlist = c_parser_expr_list (parser, true, false, &origtypes,
10465 sizeof_arg_loc, sizeof_arg,
10466 &arg_loc, &literal_zero_mask);
10467 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
10468 "expected %<)%>");
10469 orig_expr = expr;
10470 mark_exp_read (expr.value);
10471 if (warn_sizeof_pointer_memaccess)
10472 sizeof_pointer_memaccess_warning (sizeof_arg_loc,
10473 expr.value, exprlist,
10474 sizeof_arg,
10475 sizeof_ptr_memacc_comptypes);
10476 if (TREE_CODE (expr.value) == FUNCTION_DECL)
10477 {
10478 if (fndecl_built_in_p (expr.value, BUILT_IN_MEMSET)
10479 && vec_safe_length (exprlist) == 3)
10480 {
10481 tree arg0 = (*exprlist)[0];
10482 tree arg2 = (*exprlist)[2];
10483 warn_for_memset (expr_loc, arg0, arg2, literal_zero_mask);
10484 }
10485 if (warn_absolute_value
10486 && fndecl_built_in_p (expr.value, BUILT_IN_NORMAL)
10487 && vec_safe_length (exprlist) == 1)
10488 warn_for_abs (expr_loc, expr.value, (*exprlist)[0]);
10489 }
10490
10491 start = expr.get_start ();
10492 finish = parser->tokens_buf[0].get_finish ();
10493 expr.value
10494 = c_build_function_call_vec (expr_loc, arg_loc, expr.value,
10495 exprlist, origtypes);
10496 set_c_expr_source_range (&expr, start, finish);
10497
10498 expr.original_code = ERROR_MARK;
10499 if (TREE_CODE (expr.value) == INTEGER_CST
10500 && TREE_CODE (orig_expr.value) == FUNCTION_DECL
10501 && fndecl_built_in_p (orig_expr.value, BUILT_IN_CONSTANT_P))
10502 expr.original_code = C_MAYBE_CONST_EXPR;
10503 expr.original_type = NULL;
10504 if (exprlist)
10505 {
10506 release_tree_vector (exprlist);
10507 release_tree_vector (origtypes);
10508 }
10509 arg_loc.release ();
10510 break;
10511 case CPP_DOT:
10512 /* Structure element reference. */
10513 c_parser_consume_token (parser);
10514 expr = default_function_array_conversion (expr_loc, expr);
10515 if (c_parser_next_token_is (parser, CPP_NAME))
10516 {
10517 c_token *comp_tok = c_parser_peek_token (parser);
10518 ident = comp_tok->value;
10519 comp_loc = comp_tok->location;
10520 }
10521 else
10522 {
10523 c_parser_error (parser, "expected identifier");
10524 expr.set_error ();
10525 expr.original_code = ERROR_MARK;
10526 expr.original_type = NULL;
10527 return expr;
10528 }
10529 start = expr.get_start ();
10530 finish = c_parser_peek_token (parser)->get_finish ();
10531 c_parser_consume_token (parser);
10532 expr.value = build_component_ref (op_loc, expr.value, ident,
10533 comp_loc);
10534 set_c_expr_source_range (&expr, start, finish);
10535 expr.original_code = ERROR_MARK;
10536 if (TREE_CODE (expr.value) != COMPONENT_REF)
10537 expr.original_type = NULL;
10538 else
10539 {
10540 /* Remember the original type of a bitfield. */
10541 tree field = TREE_OPERAND (expr.value, 1);
10542 if (TREE_CODE (field) != FIELD_DECL)
10543 expr.original_type = NULL;
10544 else
10545 expr.original_type = DECL_BIT_FIELD_TYPE (field);
10546 }
10547 break;
10548 case CPP_DEREF:
10549 /* Structure element reference. */
10550 c_parser_consume_token (parser);
10551 expr = convert_lvalue_to_rvalue (expr_loc, expr, true, false);
10552 if (c_parser_next_token_is (parser, CPP_NAME))
10553 {
10554 c_token *comp_tok = c_parser_peek_token (parser);
10555 ident = comp_tok->value;
10556 comp_loc = comp_tok->location;
10557 }
10558 else
10559 {
10560 c_parser_error (parser, "expected identifier");
10561 expr.set_error ();
10562 expr.original_code = ERROR_MARK;
10563 expr.original_type = NULL;
10564 return expr;
10565 }
10566 start = expr.get_start ();
10567 finish = c_parser_peek_token (parser)->get_finish ();
10568 c_parser_consume_token (parser);
10569 expr.value = build_component_ref (op_loc,
10570 build_indirect_ref (op_loc,
10571 expr.value,
10572 RO_ARROW),
10573 ident, comp_loc);
10574 set_c_expr_source_range (&expr, start, finish);
10575 expr.original_code = ERROR_MARK;
10576 if (TREE_CODE (expr.value) != COMPONENT_REF)
10577 expr.original_type = NULL;
10578 else
10579 {
10580 /* Remember the original type of a bitfield. */
10581 tree field = TREE_OPERAND (expr.value, 1);
10582 if (TREE_CODE (field) != FIELD_DECL)
10583 expr.original_type = NULL;
10584 else
10585 expr.original_type = DECL_BIT_FIELD_TYPE (field);
10586 }
10587 break;
10588 case CPP_PLUS_PLUS:
10589 /* Postincrement. */
10590 start = expr.get_start ();
10591 finish = c_parser_peek_token (parser)->get_finish ();
10592 c_parser_consume_token (parser);
10593 expr = default_function_array_read_conversion (expr_loc, expr);
10594 expr.value = build_unary_op (op_loc, POSTINCREMENT_EXPR,
10595 expr.value, false);
10596 set_c_expr_source_range (&expr, start, finish);
10597 expr.original_code = ERROR_MARK;
10598 expr.original_type = NULL;
10599 break;
10600 case CPP_MINUS_MINUS:
10601 /* Postdecrement. */
10602 start = expr.get_start ();
10603 finish = c_parser_peek_token (parser)->get_finish ();
10604 c_parser_consume_token (parser);
10605 expr = default_function_array_read_conversion (expr_loc, expr);
10606 expr.value = build_unary_op (op_loc, POSTDECREMENT_EXPR,
10607 expr.value, false);
10608 set_c_expr_source_range (&expr, start, finish);
10609 expr.original_code = ERROR_MARK;
10610 expr.original_type = NULL;
10611 break;
10612 default:
10613 return expr;
10614 }
10615 }
10616 }
10617
10618 /* Parse an expression (C90 6.3.17, C99 6.5.17, C11 6.5.17).
10619
10620 expression:
10621 assignment-expression
10622 expression , assignment-expression
10623 */
10624
10625 static struct c_expr
10626 c_parser_expression (c_parser *parser)
10627 {
10628 location_t tloc = c_parser_peek_token (parser)->location;
10629 struct c_expr expr;
10630 expr = c_parser_expr_no_commas (parser, NULL);
10631 if (c_parser_next_token_is (parser, CPP_COMMA))
10632 expr = convert_lvalue_to_rvalue (tloc, expr, true, false);
10633 while (c_parser_next_token_is (parser, CPP_COMMA))
10634 {
10635 struct c_expr next;
10636 tree lhsval;
10637 location_t loc = c_parser_peek_token (parser)->location;
10638 location_t expr_loc;
10639 c_parser_consume_token (parser);
10640 expr_loc = c_parser_peek_token (parser)->location;
10641 lhsval = expr.value;
10642 while (TREE_CODE (lhsval) == COMPOUND_EXPR)
10643 lhsval = TREE_OPERAND (lhsval, 1);
10644 if (DECL_P (lhsval) || handled_component_p (lhsval))
10645 mark_exp_read (lhsval);
10646 next = c_parser_expr_no_commas (parser, NULL);
10647 next = convert_lvalue_to_rvalue (expr_loc, next, true, false);
10648 expr.value = build_compound_expr (loc, expr.value, next.value);
10649 expr.original_code = COMPOUND_EXPR;
10650 expr.original_type = next.original_type;
10651 }
10652 return expr;
10653 }
10654
10655 /* Parse an expression and convert functions or arrays to pointers and
10656 lvalues to rvalues. */
10657
10658 static struct c_expr
10659 c_parser_expression_conv (c_parser *parser)
10660 {
10661 struct c_expr expr;
10662 location_t loc = c_parser_peek_token (parser)->location;
10663 expr = c_parser_expression (parser);
10664 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
10665 return expr;
10666 }
10667
10668 /* Helper function of c_parser_expr_list. Check if IDXth (0 based)
10669 argument is a literal zero alone and if so, set it in literal_zero_mask. */
10670
10671 static inline void
10672 c_parser_check_literal_zero (c_parser *parser, unsigned *literal_zero_mask,
10673 unsigned int idx)
10674 {
10675 if (idx >= HOST_BITS_PER_INT)
10676 return;
10677
10678 c_token *tok = c_parser_peek_token (parser);
10679 switch (tok->type)
10680 {
10681 case CPP_NUMBER:
10682 case CPP_CHAR:
10683 case CPP_WCHAR:
10684 case CPP_CHAR16:
10685 case CPP_CHAR32:
10686 case CPP_UTF8CHAR:
10687 /* If a parameter is literal zero alone, remember it
10688 for -Wmemset-transposed-args warning. */
10689 if (integer_zerop (tok->value)
10690 && !TREE_OVERFLOW (tok->value)
10691 && (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
10692 || c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_PAREN))
10693 *literal_zero_mask |= 1U << idx;
10694 default:
10695 break;
10696 }
10697 }
10698
10699 /* Parse a non-empty list of expressions. If CONVERT_P, convert
10700 functions and arrays to pointers and lvalues to rvalues. If
10701 FOLD_P, fold the expressions. If LOCATIONS is non-NULL, save the
10702 locations of function arguments into this vector.
10703
10704 nonempty-expr-list:
10705 assignment-expression
10706 nonempty-expr-list , assignment-expression
10707 */
10708
10709 static vec<tree, va_gc> *
10710 c_parser_expr_list (c_parser *parser, bool convert_p, bool fold_p,
10711 vec<tree, va_gc> **p_orig_types,
10712 location_t *sizeof_arg_loc, tree *sizeof_arg,
10713 vec<location_t> *locations,
10714 unsigned int *literal_zero_mask)
10715 {
10716 vec<tree, va_gc> *ret;
10717 vec<tree, va_gc> *orig_types;
10718 struct c_expr expr;
10719 unsigned int idx = 0;
10720
10721 ret = make_tree_vector ();
10722 if (p_orig_types == NULL)
10723 orig_types = NULL;
10724 else
10725 orig_types = make_tree_vector ();
10726
10727 if (literal_zero_mask)
10728 c_parser_check_literal_zero (parser, literal_zero_mask, 0);
10729 expr = c_parser_expr_no_commas (parser, NULL);
10730 if (convert_p)
10731 expr = convert_lvalue_to_rvalue (expr.get_location (), expr, true, true);
10732 if (fold_p)
10733 expr.value = c_fully_fold (expr.value, false, NULL);
10734 ret->quick_push (expr.value);
10735 if (orig_types)
10736 orig_types->quick_push (expr.original_type);
10737 if (locations)
10738 locations->safe_push (expr.get_location ());
10739 if (sizeof_arg != NULL
10740 && expr.original_code == SIZEOF_EXPR)
10741 {
10742 sizeof_arg[0] = c_last_sizeof_arg;
10743 sizeof_arg_loc[0] = c_last_sizeof_loc;
10744 }
10745 while (c_parser_next_token_is (parser, CPP_COMMA))
10746 {
10747 c_parser_consume_token (parser);
10748 if (literal_zero_mask)
10749 c_parser_check_literal_zero (parser, literal_zero_mask, idx + 1);
10750 expr = c_parser_expr_no_commas (parser, NULL);
10751 if (convert_p)
10752 expr = convert_lvalue_to_rvalue (expr.get_location (), expr, true,
10753 true);
10754 if (fold_p)
10755 expr.value = c_fully_fold (expr.value, false, NULL);
10756 vec_safe_push (ret, expr.value);
10757 if (orig_types)
10758 vec_safe_push (orig_types, expr.original_type);
10759 if (locations)
10760 locations->safe_push (expr.get_location ());
10761 if (++idx < 3
10762 && sizeof_arg != NULL
10763 && expr.original_code == SIZEOF_EXPR)
10764 {
10765 sizeof_arg[idx] = c_last_sizeof_arg;
10766 sizeof_arg_loc[idx] = c_last_sizeof_loc;
10767 }
10768 }
10769 if (orig_types)
10770 *p_orig_types = orig_types;
10771 return ret;
10772 }
10773 \f
10774 /* Parse Objective-C-specific constructs. */
10775
10776 /* Parse an objc-class-definition.
10777
10778 objc-class-definition:
10779 @interface identifier objc-superclass[opt] objc-protocol-refs[opt]
10780 objc-class-instance-variables[opt] objc-methodprotolist @end
10781 @implementation identifier objc-superclass[opt]
10782 objc-class-instance-variables[opt]
10783 @interface identifier ( identifier ) objc-protocol-refs[opt]
10784 objc-methodprotolist @end
10785 @interface identifier ( ) objc-protocol-refs[opt]
10786 objc-methodprotolist @end
10787 @implementation identifier ( identifier )
10788
10789 objc-superclass:
10790 : identifier
10791
10792 "@interface identifier (" must start "@interface identifier (
10793 identifier ) ...": objc-methodprotolist in the first production may
10794 not start with a parenthesized identifier as a declarator of a data
10795 definition with no declaration specifiers if the objc-superclass,
10796 objc-protocol-refs and objc-class-instance-variables are omitted. */
10797
10798 static void
10799 c_parser_objc_class_definition (c_parser *parser, tree attributes)
10800 {
10801 bool iface_p;
10802 tree id1;
10803 tree superclass;
10804 if (c_parser_next_token_is_keyword (parser, RID_AT_INTERFACE))
10805 iface_p = true;
10806 else if (c_parser_next_token_is_keyword (parser, RID_AT_IMPLEMENTATION))
10807 iface_p = false;
10808 else
10809 gcc_unreachable ();
10810
10811 c_parser_consume_token (parser);
10812 if (c_parser_next_token_is_not (parser, CPP_NAME))
10813 {
10814 c_parser_error (parser, "expected identifier");
10815 return;
10816 }
10817 id1 = c_parser_peek_token (parser)->value;
10818 c_parser_consume_token (parser);
10819 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
10820 {
10821 /* We have a category or class extension. */
10822 tree id2;
10823 tree proto = NULL_TREE;
10824 matching_parens parens;
10825 parens.consume_open (parser);
10826 if (c_parser_next_token_is_not (parser, CPP_NAME))
10827 {
10828 if (iface_p && c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
10829 {
10830 /* We have a class extension. */
10831 id2 = NULL_TREE;
10832 }
10833 else
10834 {
10835 c_parser_error (parser, "expected identifier or %<)%>");
10836 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
10837 return;
10838 }
10839 }
10840 else
10841 {
10842 id2 = c_parser_peek_token (parser)->value;
10843 c_parser_consume_token (parser);
10844 }
10845 parens.skip_until_found_close (parser);
10846 if (!iface_p)
10847 {
10848 objc_start_category_implementation (id1, id2);
10849 return;
10850 }
10851 if (c_parser_next_token_is (parser, CPP_LESS))
10852 proto = c_parser_objc_protocol_refs (parser);
10853 objc_start_category_interface (id1, id2, proto, attributes);
10854 c_parser_objc_methodprotolist (parser);
10855 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
10856 objc_finish_interface ();
10857 return;
10858 }
10859 if (c_parser_next_token_is (parser, CPP_COLON))
10860 {
10861 c_parser_consume_token (parser);
10862 if (c_parser_next_token_is_not (parser, CPP_NAME))
10863 {
10864 c_parser_error (parser, "expected identifier");
10865 return;
10866 }
10867 superclass = c_parser_peek_token (parser)->value;
10868 c_parser_consume_token (parser);
10869 }
10870 else
10871 superclass = NULL_TREE;
10872 if (iface_p)
10873 {
10874 tree proto = NULL_TREE;
10875 if (c_parser_next_token_is (parser, CPP_LESS))
10876 proto = c_parser_objc_protocol_refs (parser);
10877 objc_start_class_interface (id1, superclass, proto, attributes);
10878 }
10879 else
10880 objc_start_class_implementation (id1, superclass);
10881 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
10882 c_parser_objc_class_instance_variables (parser);
10883 if (iface_p)
10884 {
10885 objc_continue_interface ();
10886 c_parser_objc_methodprotolist (parser);
10887 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
10888 objc_finish_interface ();
10889 }
10890 else
10891 {
10892 objc_continue_implementation ();
10893 return;
10894 }
10895 }
10896
10897 /* Parse objc-class-instance-variables.
10898
10899 objc-class-instance-variables:
10900 { objc-instance-variable-decl-list[opt] }
10901
10902 objc-instance-variable-decl-list:
10903 objc-visibility-spec
10904 objc-instance-variable-decl ;
10905 ;
10906 objc-instance-variable-decl-list objc-visibility-spec
10907 objc-instance-variable-decl-list objc-instance-variable-decl ;
10908 objc-instance-variable-decl-list ;
10909
10910 objc-visibility-spec:
10911 @private
10912 @protected
10913 @public
10914
10915 objc-instance-variable-decl:
10916 struct-declaration
10917 */
10918
10919 static void
10920 c_parser_objc_class_instance_variables (c_parser *parser)
10921 {
10922 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
10923 c_parser_consume_token (parser);
10924 while (c_parser_next_token_is_not (parser, CPP_EOF))
10925 {
10926 tree decls;
10927 /* Parse any stray semicolon. */
10928 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
10929 {
10930 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
10931 "extra semicolon");
10932 c_parser_consume_token (parser);
10933 continue;
10934 }
10935 /* Stop if at the end of the instance variables. */
10936 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
10937 {
10938 c_parser_consume_token (parser);
10939 break;
10940 }
10941 /* Parse any objc-visibility-spec. */
10942 if (c_parser_next_token_is_keyword (parser, RID_AT_PRIVATE))
10943 {
10944 c_parser_consume_token (parser);
10945 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
10946 continue;
10947 }
10948 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROTECTED))
10949 {
10950 c_parser_consume_token (parser);
10951 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
10952 continue;
10953 }
10954 else if (c_parser_next_token_is_keyword (parser, RID_AT_PUBLIC))
10955 {
10956 c_parser_consume_token (parser);
10957 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
10958 continue;
10959 }
10960 else if (c_parser_next_token_is_keyword (parser, RID_AT_PACKAGE))
10961 {
10962 c_parser_consume_token (parser);
10963 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
10964 continue;
10965 }
10966 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
10967 {
10968 c_parser_pragma (parser, pragma_external, NULL);
10969 continue;
10970 }
10971
10972 /* Parse some comma-separated declarations. */
10973 decls = c_parser_struct_declaration (parser);
10974 if (decls == NULL)
10975 {
10976 /* There is a syntax error. We want to skip the offending
10977 tokens up to the next ';' (included) or '}'
10978 (excluded). */
10979
10980 /* First, skip manually a ')' or ']'. This is because they
10981 reduce the nesting level, so c_parser_skip_until_found()
10982 wouldn't be able to skip past them. */
10983 c_token *token = c_parser_peek_token (parser);
10984 if (token->type == CPP_CLOSE_PAREN || token->type == CPP_CLOSE_SQUARE)
10985 c_parser_consume_token (parser);
10986
10987 /* Then, do the standard skipping. */
10988 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
10989
10990 /* We hopefully recovered. Start normal parsing again. */
10991 parser->error = false;
10992 continue;
10993 }
10994 else
10995 {
10996 /* Comma-separated instance variables are chained together
10997 in reverse order; add them one by one. */
10998 tree ivar = nreverse (decls);
10999 for (; ivar; ivar = DECL_CHAIN (ivar))
11000 objc_add_instance_variable (copy_node (ivar));
11001 }
11002 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
11003 }
11004 }
11005
11006 /* Parse an objc-class-declaration.
11007
11008 objc-class-declaration:
11009 @class identifier-list ;
11010 */
11011
11012 static void
11013 c_parser_objc_class_declaration (c_parser *parser)
11014 {
11015 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_CLASS));
11016 c_parser_consume_token (parser);
11017 /* Any identifiers, including those declared as type names, are OK
11018 here. */
11019 while (true)
11020 {
11021 tree id;
11022 if (c_parser_next_token_is_not (parser, CPP_NAME))
11023 {
11024 c_parser_error (parser, "expected identifier");
11025 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
11026 parser->error = false;
11027 return;
11028 }
11029 id = c_parser_peek_token (parser)->value;
11030 objc_declare_class (id);
11031 c_parser_consume_token (parser);
11032 if (c_parser_next_token_is (parser, CPP_COMMA))
11033 c_parser_consume_token (parser);
11034 else
11035 break;
11036 }
11037 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
11038 }
11039
11040 /* Parse an objc-alias-declaration.
11041
11042 objc-alias-declaration:
11043 @compatibility_alias identifier identifier ;
11044 */
11045
11046 static void
11047 c_parser_objc_alias_declaration (c_parser *parser)
11048 {
11049 tree id1, id2;
11050 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_ALIAS));
11051 c_parser_consume_token (parser);
11052 if (c_parser_next_token_is_not (parser, CPP_NAME))
11053 {
11054 c_parser_error (parser, "expected identifier");
11055 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
11056 return;
11057 }
11058 id1 = c_parser_peek_token (parser)->value;
11059 c_parser_consume_token (parser);
11060 if (c_parser_next_token_is_not (parser, CPP_NAME))
11061 {
11062 c_parser_error (parser, "expected identifier");
11063 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
11064 return;
11065 }
11066 id2 = c_parser_peek_token (parser)->value;
11067 c_parser_consume_token (parser);
11068 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
11069 objc_declare_alias (id1, id2);
11070 }
11071
11072 /* Parse an objc-protocol-definition.
11073
11074 objc-protocol-definition:
11075 @protocol identifier objc-protocol-refs[opt] objc-methodprotolist @end
11076 @protocol identifier-list ;
11077
11078 "@protocol identifier ;" should be resolved as "@protocol
11079 identifier-list ;": objc-methodprotolist may not start with a
11080 semicolon in the first alternative if objc-protocol-refs are
11081 omitted. */
11082
11083 static void
11084 c_parser_objc_protocol_definition (c_parser *parser, tree attributes)
11085 {
11086 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROTOCOL));
11087
11088 c_parser_consume_token (parser);
11089 if (c_parser_next_token_is_not (parser, CPP_NAME))
11090 {
11091 c_parser_error (parser, "expected identifier");
11092 return;
11093 }
11094 if (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
11095 || c_parser_peek_2nd_token (parser)->type == CPP_SEMICOLON)
11096 {
11097 /* Any identifiers, including those declared as type names, are
11098 OK here. */
11099 while (true)
11100 {
11101 tree id;
11102 if (c_parser_next_token_is_not (parser, CPP_NAME))
11103 {
11104 c_parser_error (parser, "expected identifier");
11105 break;
11106 }
11107 id = c_parser_peek_token (parser)->value;
11108 objc_declare_protocol (id, attributes);
11109 c_parser_consume_token (parser);
11110 if (c_parser_next_token_is (parser, CPP_COMMA))
11111 c_parser_consume_token (parser);
11112 else
11113 break;
11114 }
11115 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
11116 }
11117 else
11118 {
11119 tree id = c_parser_peek_token (parser)->value;
11120 tree proto = NULL_TREE;
11121 c_parser_consume_token (parser);
11122 if (c_parser_next_token_is (parser, CPP_LESS))
11123 proto = c_parser_objc_protocol_refs (parser);
11124 parser->objc_pq_context = true;
11125 objc_start_protocol (id, proto, attributes);
11126 c_parser_objc_methodprotolist (parser);
11127 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
11128 parser->objc_pq_context = false;
11129 objc_finish_interface ();
11130 }
11131 }
11132
11133 /* Parse an objc-method-type.
11134
11135 objc-method-type:
11136 +
11137 -
11138
11139 Return true if it is a class method (+) and false if it is
11140 an instance method (-).
11141 */
11142 static inline bool
11143 c_parser_objc_method_type (c_parser *parser)
11144 {
11145 switch (c_parser_peek_token (parser)->type)
11146 {
11147 case CPP_PLUS:
11148 c_parser_consume_token (parser);
11149 return true;
11150 case CPP_MINUS:
11151 c_parser_consume_token (parser);
11152 return false;
11153 default:
11154 gcc_unreachable ();
11155 }
11156 }
11157
11158 /* Parse an objc-method-definition.
11159
11160 objc-method-definition:
11161 objc-method-type objc-method-decl ;[opt] compound-statement
11162 */
11163
11164 static void
11165 c_parser_objc_method_definition (c_parser *parser)
11166 {
11167 bool is_class_method = c_parser_objc_method_type (parser);
11168 tree decl, attributes = NULL_TREE, expr = NULL_TREE;
11169 parser->objc_pq_context = true;
11170 decl = c_parser_objc_method_decl (parser, is_class_method, &attributes,
11171 &expr);
11172 if (decl == error_mark_node)
11173 return; /* Bail here. */
11174
11175 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
11176 {
11177 c_parser_consume_token (parser);
11178 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
11179 "extra semicolon in method definition specified");
11180 }
11181
11182 if (!c_parser_next_token_is (parser, CPP_OPEN_BRACE))
11183 {
11184 c_parser_error (parser, "expected %<{%>");
11185 return;
11186 }
11187
11188 parser->objc_pq_context = false;
11189 if (objc_start_method_definition (is_class_method, decl, attributes, expr))
11190 {
11191 add_stmt (c_parser_compound_statement (parser));
11192 objc_finish_method_definition (current_function_decl);
11193 }
11194 else
11195 {
11196 /* This code is executed when we find a method definition
11197 outside of an @implementation context (or invalid for other
11198 reasons). Parse the method (to keep going) but do not emit
11199 any code.
11200 */
11201 c_parser_compound_statement (parser);
11202 }
11203 }
11204
11205 /* Parse an objc-methodprotolist.
11206
11207 objc-methodprotolist:
11208 empty
11209 objc-methodprotolist objc-methodproto
11210 objc-methodprotolist declaration
11211 objc-methodprotolist ;
11212 @optional
11213 @required
11214
11215 The declaration is a data definition, which may be missing
11216 declaration specifiers under the same rules and diagnostics as
11217 other data definitions outside functions, and the stray semicolon
11218 is diagnosed the same way as a stray semicolon outside a
11219 function. */
11220
11221 static void
11222 c_parser_objc_methodprotolist (c_parser *parser)
11223 {
11224 while (true)
11225 {
11226 /* The list is terminated by @end. */
11227 switch (c_parser_peek_token (parser)->type)
11228 {
11229 case CPP_SEMICOLON:
11230 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
11231 "ISO C does not allow extra %<;%> outside of a function");
11232 c_parser_consume_token (parser);
11233 break;
11234 case CPP_PLUS:
11235 case CPP_MINUS:
11236 c_parser_objc_methodproto (parser);
11237 break;
11238 case CPP_PRAGMA:
11239 c_parser_pragma (parser, pragma_external, NULL);
11240 break;
11241 case CPP_EOF:
11242 return;
11243 default:
11244 if (c_parser_next_token_is_keyword (parser, RID_AT_END))
11245 return;
11246 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROPERTY))
11247 c_parser_objc_at_property_declaration (parser);
11248 else if (c_parser_next_token_is_keyword (parser, RID_AT_OPTIONAL))
11249 {
11250 objc_set_method_opt (true);
11251 c_parser_consume_token (parser);
11252 }
11253 else if (c_parser_next_token_is_keyword (parser, RID_AT_REQUIRED))
11254 {
11255 objc_set_method_opt (false);
11256 c_parser_consume_token (parser);
11257 }
11258 else
11259 c_parser_declaration_or_fndef (parser, false, false, true,
11260 false, true, NULL, vNULL);
11261 break;
11262 }
11263 }
11264 }
11265
11266 /* Parse an objc-methodproto.
11267
11268 objc-methodproto:
11269 objc-method-type objc-method-decl ;
11270 */
11271
11272 static void
11273 c_parser_objc_methodproto (c_parser *parser)
11274 {
11275 bool is_class_method = c_parser_objc_method_type (parser);
11276 tree decl, attributes = NULL_TREE;
11277
11278 /* Remember protocol qualifiers in prototypes. */
11279 parser->objc_pq_context = true;
11280 decl = c_parser_objc_method_decl (parser, is_class_method, &attributes,
11281 NULL);
11282 /* Forget protocol qualifiers now. */
11283 parser->objc_pq_context = false;
11284
11285 /* Do not allow the presence of attributes to hide an erroneous
11286 method implementation in the interface section. */
11287 if (!c_parser_next_token_is (parser, CPP_SEMICOLON))
11288 {
11289 c_parser_error (parser, "expected %<;%>");
11290 return;
11291 }
11292
11293 if (decl != error_mark_node)
11294 objc_add_method_declaration (is_class_method, decl, attributes);
11295
11296 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
11297 }
11298
11299 /* If we are at a position that method attributes may be present, check that
11300 there are not any parsed already (a syntax error) and then collect any
11301 specified at the current location. Finally, if new attributes were present,
11302 check that the next token is legal ( ';' for decls and '{' for defs). */
11303
11304 static bool
11305 c_parser_objc_maybe_method_attributes (c_parser* parser, tree* attributes)
11306 {
11307 bool bad = false;
11308 if (*attributes)
11309 {
11310 c_parser_error (parser,
11311 "method attributes must be specified at the end only");
11312 *attributes = NULL_TREE;
11313 bad = true;
11314 }
11315
11316 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
11317 *attributes = c_parser_gnu_attributes (parser);
11318
11319 /* If there were no attributes here, just report any earlier error. */
11320 if (*attributes == NULL_TREE || bad)
11321 return bad;
11322
11323 /* If the attributes are followed by a ; or {, then just report any earlier
11324 error. */
11325 if (c_parser_next_token_is (parser, CPP_SEMICOLON)
11326 || c_parser_next_token_is (parser, CPP_OPEN_BRACE))
11327 return bad;
11328
11329 /* We've got attributes, but not at the end. */
11330 c_parser_error (parser,
11331 "expected %<;%> or %<{%> after method attribute definition");
11332 return true;
11333 }
11334
11335 /* Parse an objc-method-decl.
11336
11337 objc-method-decl:
11338 ( objc-type-name ) objc-selector
11339 objc-selector
11340 ( objc-type-name ) objc-keyword-selector objc-optparmlist
11341 objc-keyword-selector objc-optparmlist
11342 gnu-attributes
11343
11344 objc-keyword-selector:
11345 objc-keyword-decl
11346 objc-keyword-selector objc-keyword-decl
11347
11348 objc-keyword-decl:
11349 objc-selector : ( objc-type-name ) identifier
11350 objc-selector : identifier
11351 : ( objc-type-name ) identifier
11352 : identifier
11353
11354 objc-optparmlist:
11355 objc-optparms objc-optellipsis
11356
11357 objc-optparms:
11358 empty
11359 objc-opt-parms , parameter-declaration
11360
11361 objc-optellipsis:
11362 empty
11363 , ...
11364 */
11365
11366 static tree
11367 c_parser_objc_method_decl (c_parser *parser, bool is_class_method,
11368 tree *attributes, tree *expr)
11369 {
11370 tree type = NULL_TREE;
11371 tree sel;
11372 tree parms = NULL_TREE;
11373 bool ellipsis = false;
11374 bool attr_err = false;
11375
11376 *attributes = NULL_TREE;
11377 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
11378 {
11379 matching_parens parens;
11380 parens.consume_open (parser);
11381 type = c_parser_objc_type_name (parser);
11382 parens.skip_until_found_close (parser);
11383 }
11384 sel = c_parser_objc_selector (parser);
11385 /* If there is no selector, or a colon follows, we have an
11386 objc-keyword-selector. If there is a selector, and a colon does
11387 not follow, that selector ends the objc-method-decl. */
11388 if (!sel || c_parser_next_token_is (parser, CPP_COLON))
11389 {
11390 tree tsel = sel;
11391 tree list = NULL_TREE;
11392 while (true)
11393 {
11394 tree atype = NULL_TREE, id, keyworddecl;
11395 tree param_attr = NULL_TREE;
11396 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
11397 break;
11398 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
11399 {
11400 c_parser_consume_token (parser);
11401 atype = c_parser_objc_type_name (parser);
11402 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
11403 "expected %<)%>");
11404 }
11405 /* New ObjC allows attributes on method parameters. */
11406 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
11407 param_attr = c_parser_gnu_attributes (parser);
11408 if (c_parser_next_token_is_not (parser, CPP_NAME))
11409 {
11410 c_parser_error (parser, "expected identifier");
11411 return error_mark_node;
11412 }
11413 id = c_parser_peek_token (parser)->value;
11414 c_parser_consume_token (parser);
11415 keyworddecl = objc_build_keyword_decl (tsel, atype, id, param_attr);
11416 list = chainon (list, keyworddecl);
11417 tsel = c_parser_objc_selector (parser);
11418 if (!tsel && c_parser_next_token_is_not (parser, CPP_COLON))
11419 break;
11420 }
11421
11422 attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
11423
11424 /* Parse the optional parameter list. Optional Objective-C
11425 method parameters follow the C syntax, and may include '...'
11426 to denote a variable number of arguments. */
11427 parms = make_node (TREE_LIST);
11428 while (c_parser_next_token_is (parser, CPP_COMMA))
11429 {
11430 struct c_parm *parm;
11431 c_parser_consume_token (parser);
11432 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
11433 {
11434 ellipsis = true;
11435 c_parser_consume_token (parser);
11436 attr_err |= c_parser_objc_maybe_method_attributes
11437 (parser, attributes) ;
11438 break;
11439 }
11440 parm = c_parser_parameter_declaration (parser, NULL_TREE, false);
11441 if (parm == NULL)
11442 break;
11443 parms = chainon (parms,
11444 build_tree_list (NULL_TREE, grokparm (parm, expr)));
11445 }
11446 sel = list;
11447 }
11448 else
11449 attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
11450
11451 if (sel == NULL)
11452 {
11453 c_parser_error (parser, "objective-c method declaration is expected");
11454 return error_mark_node;
11455 }
11456
11457 if (attr_err)
11458 return error_mark_node;
11459
11460 return objc_build_method_signature (is_class_method, type, sel, parms, ellipsis);
11461 }
11462
11463 /* Parse an objc-type-name.
11464
11465 objc-type-name:
11466 objc-type-qualifiers[opt] type-name
11467 objc-type-qualifiers[opt]
11468
11469 objc-type-qualifiers:
11470 objc-type-qualifier
11471 objc-type-qualifiers objc-type-qualifier
11472
11473 objc-type-qualifier: one of
11474 in out inout bycopy byref oneway
11475 */
11476
11477 static tree
11478 c_parser_objc_type_name (c_parser *parser)
11479 {
11480 tree quals = NULL_TREE;
11481 struct c_type_name *type_name = NULL;
11482 tree type = NULL_TREE;
11483 while (true)
11484 {
11485 c_token *token = c_parser_peek_token (parser);
11486 if (token->type == CPP_KEYWORD
11487 && (token->keyword == RID_IN
11488 || token->keyword == RID_OUT
11489 || token->keyword == RID_INOUT
11490 || token->keyword == RID_BYCOPY
11491 || token->keyword == RID_BYREF
11492 || token->keyword == RID_ONEWAY))
11493 {
11494 quals = chainon (build_tree_list (NULL_TREE, token->value), quals);
11495 c_parser_consume_token (parser);
11496 }
11497 else
11498 break;
11499 }
11500 if (c_parser_next_tokens_start_typename (parser, cla_prefer_type))
11501 type_name = c_parser_type_name (parser);
11502 if (type_name)
11503 type = groktypename (type_name, NULL, NULL);
11504
11505 /* If the type is unknown, and error has already been produced and
11506 we need to recover from the error. In that case, use NULL_TREE
11507 for the type, as if no type had been specified; this will use the
11508 default type ('id') which is good for error recovery. */
11509 if (type == error_mark_node)
11510 type = NULL_TREE;
11511
11512 return build_tree_list (quals, type);
11513 }
11514
11515 /* Parse objc-protocol-refs.
11516
11517 objc-protocol-refs:
11518 < identifier-list >
11519 */
11520
11521 static tree
11522 c_parser_objc_protocol_refs (c_parser *parser)
11523 {
11524 tree list = NULL_TREE;
11525 gcc_assert (c_parser_next_token_is (parser, CPP_LESS));
11526 c_parser_consume_token (parser);
11527 /* Any identifiers, including those declared as type names, are OK
11528 here. */
11529 while (true)
11530 {
11531 tree id;
11532 if (c_parser_next_token_is_not (parser, CPP_NAME))
11533 {
11534 c_parser_error (parser, "expected identifier");
11535 break;
11536 }
11537 id = c_parser_peek_token (parser)->value;
11538 list = chainon (list, build_tree_list (NULL_TREE, id));
11539 c_parser_consume_token (parser);
11540 if (c_parser_next_token_is (parser, CPP_COMMA))
11541 c_parser_consume_token (parser);
11542 else
11543 break;
11544 }
11545 c_parser_require (parser, CPP_GREATER, "expected %<>%>");
11546 return list;
11547 }
11548
11549 /* Parse an objc-try-catch-finally-statement.
11550
11551 objc-try-catch-finally-statement:
11552 @try compound-statement objc-catch-list[opt]
11553 @try compound-statement objc-catch-list[opt] @finally compound-statement
11554
11555 objc-catch-list:
11556 @catch ( objc-catch-parameter-declaration ) compound-statement
11557 objc-catch-list @catch ( objc-catch-parameter-declaration ) compound-statement
11558
11559 objc-catch-parameter-declaration:
11560 parameter-declaration
11561 '...'
11562
11563 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
11564
11565 PS: This function is identical to cp_parser_objc_try_catch_finally_statement
11566 for C++. Keep them in sync. */
11567
11568 static void
11569 c_parser_objc_try_catch_finally_statement (c_parser *parser)
11570 {
11571 location_t location;
11572 tree stmt;
11573
11574 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_TRY));
11575 c_parser_consume_token (parser);
11576 location = c_parser_peek_token (parser)->location;
11577 objc_maybe_warn_exceptions (location);
11578 stmt = c_parser_compound_statement (parser);
11579 objc_begin_try_stmt (location, stmt);
11580
11581 while (c_parser_next_token_is_keyword (parser, RID_AT_CATCH))
11582 {
11583 struct c_parm *parm;
11584 tree parameter_declaration = error_mark_node;
11585 bool seen_open_paren = false;
11586
11587 c_parser_consume_token (parser);
11588 matching_parens parens;
11589 if (!parens.require_open (parser))
11590 seen_open_paren = true;
11591 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
11592 {
11593 /* We have "@catch (...)" (where the '...' are literally
11594 what is in the code). Skip the '...'.
11595 parameter_declaration is set to NULL_TREE, and
11596 objc_being_catch_clauses() knows that that means
11597 '...'. */
11598 c_parser_consume_token (parser);
11599 parameter_declaration = NULL_TREE;
11600 }
11601 else
11602 {
11603 /* We have "@catch (NSException *exception)" or something
11604 like that. Parse the parameter declaration. */
11605 parm = c_parser_parameter_declaration (parser, NULL_TREE, false);
11606 if (parm == NULL)
11607 parameter_declaration = error_mark_node;
11608 else
11609 parameter_declaration = grokparm (parm, NULL);
11610 }
11611 if (seen_open_paren)
11612 parens.require_close (parser);
11613 else
11614 {
11615 /* If there was no open parenthesis, we are recovering from
11616 an error, and we are trying to figure out what mistake
11617 the user has made. */
11618
11619 /* If there is an immediate closing parenthesis, the user
11620 probably forgot the opening one (ie, they typed "@catch
11621 NSException *e)". Parse the closing parenthesis and keep
11622 going. */
11623 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
11624 c_parser_consume_token (parser);
11625
11626 /* If these is no immediate closing parenthesis, the user
11627 probably doesn't know that parenthesis are required at
11628 all (ie, they typed "@catch NSException *e"). So, just
11629 forget about the closing parenthesis and keep going. */
11630 }
11631 objc_begin_catch_clause (parameter_declaration);
11632 if (c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
11633 c_parser_compound_statement_nostart (parser);
11634 objc_finish_catch_clause ();
11635 }
11636 if (c_parser_next_token_is_keyword (parser, RID_AT_FINALLY))
11637 {
11638 c_parser_consume_token (parser);
11639 location = c_parser_peek_token (parser)->location;
11640 stmt = c_parser_compound_statement (parser);
11641 objc_build_finally_clause (location, stmt);
11642 }
11643 objc_finish_try_stmt ();
11644 }
11645
11646 /* Parse an objc-synchronized-statement.
11647
11648 objc-synchronized-statement:
11649 @synchronized ( expression ) compound-statement
11650 */
11651
11652 static void
11653 c_parser_objc_synchronized_statement (c_parser *parser)
11654 {
11655 location_t loc;
11656 tree expr, stmt;
11657 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNCHRONIZED));
11658 c_parser_consume_token (parser);
11659 loc = c_parser_peek_token (parser)->location;
11660 objc_maybe_warn_exceptions (loc);
11661 matching_parens parens;
11662 if (parens.require_open (parser))
11663 {
11664 struct c_expr ce = c_parser_expression (parser);
11665 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
11666 expr = ce.value;
11667 expr = c_fully_fold (expr, false, NULL);
11668 parens.skip_until_found_close (parser);
11669 }
11670 else
11671 expr = error_mark_node;
11672 stmt = c_parser_compound_statement (parser);
11673 objc_build_synchronized (loc, expr, stmt);
11674 }
11675
11676 /* Parse an objc-selector; return NULL_TREE without an error if the
11677 next token is not an objc-selector.
11678
11679 objc-selector:
11680 identifier
11681 one of
11682 enum struct union if else while do for switch case default
11683 break continue return goto asm sizeof typeof __alignof
11684 unsigned long const short volatile signed restrict _Complex
11685 in out inout bycopy byref oneway int char float double void _Bool
11686 _Atomic
11687
11688 ??? Why this selection of keywords but not, for example, storage
11689 class specifiers? */
11690
11691 static tree
11692 c_parser_objc_selector (c_parser *parser)
11693 {
11694 c_token *token = c_parser_peek_token (parser);
11695 tree value = token->value;
11696 if (token->type == CPP_NAME)
11697 {
11698 c_parser_consume_token (parser);
11699 return value;
11700 }
11701 if (token->type != CPP_KEYWORD)
11702 return NULL_TREE;
11703 switch (token->keyword)
11704 {
11705 case RID_ENUM:
11706 case RID_STRUCT:
11707 case RID_UNION:
11708 case RID_IF:
11709 case RID_ELSE:
11710 case RID_WHILE:
11711 case RID_DO:
11712 case RID_FOR:
11713 case RID_SWITCH:
11714 case RID_CASE:
11715 case RID_DEFAULT:
11716 case RID_BREAK:
11717 case RID_CONTINUE:
11718 case RID_RETURN:
11719 case RID_GOTO:
11720 case RID_ASM:
11721 case RID_SIZEOF:
11722 case RID_TYPEOF:
11723 case RID_ALIGNOF:
11724 case RID_UNSIGNED:
11725 case RID_LONG:
11726 case RID_CONST:
11727 case RID_SHORT:
11728 case RID_VOLATILE:
11729 case RID_SIGNED:
11730 case RID_RESTRICT:
11731 case RID_COMPLEX:
11732 case RID_IN:
11733 case RID_OUT:
11734 case RID_INOUT:
11735 case RID_BYCOPY:
11736 case RID_BYREF:
11737 case RID_ONEWAY:
11738 case RID_INT:
11739 case RID_CHAR:
11740 case RID_FLOAT:
11741 case RID_DOUBLE:
11742 CASE_RID_FLOATN_NX:
11743 case RID_VOID:
11744 case RID_BOOL:
11745 case RID_ATOMIC:
11746 case RID_AUTO_TYPE:
11747 case RID_INT_N_0:
11748 case RID_INT_N_1:
11749 case RID_INT_N_2:
11750 case RID_INT_N_3:
11751 c_parser_consume_token (parser);
11752 return value;
11753 default:
11754 return NULL_TREE;
11755 }
11756 }
11757
11758 /* Parse an objc-selector-arg.
11759
11760 objc-selector-arg:
11761 objc-selector
11762 objc-keywordname-list
11763
11764 objc-keywordname-list:
11765 objc-keywordname
11766 objc-keywordname-list objc-keywordname
11767
11768 objc-keywordname:
11769 objc-selector :
11770 :
11771 */
11772
11773 static tree
11774 c_parser_objc_selector_arg (c_parser *parser)
11775 {
11776 tree sel = c_parser_objc_selector (parser);
11777 tree list = NULL_TREE;
11778 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
11779 return sel;
11780 while (true)
11781 {
11782 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
11783 return list;
11784 list = chainon (list, build_tree_list (sel, NULL_TREE));
11785 sel = c_parser_objc_selector (parser);
11786 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
11787 break;
11788 }
11789 return list;
11790 }
11791
11792 /* Parse an objc-receiver.
11793
11794 objc-receiver:
11795 expression
11796 class-name
11797 type-name
11798 */
11799
11800 static tree
11801 c_parser_objc_receiver (c_parser *parser)
11802 {
11803 location_t loc = c_parser_peek_token (parser)->location;
11804
11805 if (c_parser_peek_token (parser)->type == CPP_NAME
11806 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
11807 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
11808 {
11809 tree id = c_parser_peek_token (parser)->value;
11810 c_parser_consume_token (parser);
11811 return objc_get_class_reference (id);
11812 }
11813 struct c_expr ce = c_parser_expression (parser);
11814 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
11815 return c_fully_fold (ce.value, false, NULL);
11816 }
11817
11818 /* Parse objc-message-args.
11819
11820 objc-message-args:
11821 objc-selector
11822 objc-keywordarg-list
11823
11824 objc-keywordarg-list:
11825 objc-keywordarg
11826 objc-keywordarg-list objc-keywordarg
11827
11828 objc-keywordarg:
11829 objc-selector : objc-keywordexpr
11830 : objc-keywordexpr
11831 */
11832
11833 static tree
11834 c_parser_objc_message_args (c_parser *parser)
11835 {
11836 tree sel = c_parser_objc_selector (parser);
11837 tree list = NULL_TREE;
11838 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
11839 return sel;
11840 while (true)
11841 {
11842 tree keywordexpr;
11843 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
11844 return error_mark_node;
11845 keywordexpr = c_parser_objc_keywordexpr (parser);
11846 list = chainon (list, build_tree_list (sel, keywordexpr));
11847 sel = c_parser_objc_selector (parser);
11848 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
11849 break;
11850 }
11851 return list;
11852 }
11853
11854 /* Parse an objc-keywordexpr.
11855
11856 objc-keywordexpr:
11857 nonempty-expr-list
11858 */
11859
11860 static tree
11861 c_parser_objc_keywordexpr (c_parser *parser)
11862 {
11863 tree ret;
11864 vec<tree, va_gc> *expr_list = c_parser_expr_list (parser, true, true,
11865 NULL, NULL, NULL, NULL);
11866 if (vec_safe_length (expr_list) == 1)
11867 {
11868 /* Just return the expression, remove a level of
11869 indirection. */
11870 ret = (*expr_list)[0];
11871 }
11872 else
11873 {
11874 /* We have a comma expression, we will collapse later. */
11875 ret = build_tree_list_vec (expr_list);
11876 }
11877 release_tree_vector (expr_list);
11878 return ret;
11879 }
11880
11881 /* A check, needed in several places, that ObjC interface, implementation or
11882 method definitions are not prefixed by incorrect items. */
11883 static bool
11884 c_parser_objc_diagnose_bad_element_prefix (c_parser *parser,
11885 struct c_declspecs *specs)
11886 {
11887 if (!specs->declspecs_seen_p || specs->non_sc_seen_p
11888 || specs->typespec_kind != ctsk_none)
11889 {
11890 c_parser_error (parser,
11891 "no type or storage class may be specified here,");
11892 c_parser_skip_to_end_of_block_or_statement (parser);
11893 return true;
11894 }
11895 return false;
11896 }
11897
11898 /* Parse an Objective-C @property declaration. The syntax is:
11899
11900 objc-property-declaration:
11901 '@property' objc-property-attributes[opt] struct-declaration ;
11902
11903 objc-property-attributes:
11904 '(' objc-property-attribute-list ')'
11905
11906 objc-property-attribute-list:
11907 objc-property-attribute
11908 objc-property-attribute-list, objc-property-attribute
11909
11910 objc-property-attribute
11911 'getter' = identifier
11912 'setter' = identifier
11913 'readonly'
11914 'readwrite'
11915 'assign'
11916 'retain'
11917 'copy'
11918 'nonatomic'
11919
11920 For example:
11921 @property NSString *name;
11922 @property (readonly) id object;
11923 @property (retain, nonatomic, getter=getTheName) id name;
11924 @property int a, b, c;
11925
11926 PS: This function is identical to cp_parser_objc_at_propery_declaration
11927 for C++. Keep them in sync. */
11928 static void
11929 c_parser_objc_at_property_declaration (c_parser *parser)
11930 {
11931 /* The following variables hold the attributes of the properties as
11932 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
11933 seen. When we see an attribute, we set them to 'true' (if they
11934 are boolean properties) or to the identifier (if they have an
11935 argument, ie, for getter and setter). Note that here we only
11936 parse the list of attributes, check the syntax and accumulate the
11937 attributes that we find. objc_add_property_declaration() will
11938 then process the information. */
11939 bool property_assign = false;
11940 bool property_copy = false;
11941 tree property_getter_ident = NULL_TREE;
11942 bool property_nonatomic = false;
11943 bool property_readonly = false;
11944 bool property_readwrite = false;
11945 bool property_retain = false;
11946 tree property_setter_ident = NULL_TREE;
11947
11948 /* 'properties' is the list of properties that we read. Usually a
11949 single one, but maybe more (eg, in "@property int a, b, c;" there
11950 are three). */
11951 tree properties;
11952 location_t loc;
11953
11954 loc = c_parser_peek_token (parser)->location;
11955 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROPERTY));
11956
11957 c_parser_consume_token (parser); /* Eat '@property'. */
11958
11959 /* Parse the optional attribute list... */
11960 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
11961 {
11962 matching_parens parens;
11963
11964 /* Eat the '(' */
11965 parens.consume_open (parser);
11966
11967 /* Property attribute keywords are valid now. */
11968 parser->objc_property_attr_context = true;
11969
11970 while (true)
11971 {
11972 bool syntax_error = false;
11973 c_token *token = c_parser_peek_token (parser);
11974 enum rid keyword;
11975
11976 if (token->type != CPP_KEYWORD)
11977 {
11978 if (token->type == CPP_CLOSE_PAREN)
11979 c_parser_error (parser, "expected identifier");
11980 else
11981 {
11982 c_parser_consume_token (parser);
11983 c_parser_error (parser, "unknown property attribute");
11984 }
11985 break;
11986 }
11987 keyword = token->keyword;
11988 c_parser_consume_token (parser);
11989 switch (keyword)
11990 {
11991 case RID_ASSIGN: property_assign = true; break;
11992 case RID_COPY: property_copy = true; break;
11993 case RID_NONATOMIC: property_nonatomic = true; break;
11994 case RID_READONLY: property_readonly = true; break;
11995 case RID_READWRITE: property_readwrite = true; break;
11996 case RID_RETAIN: property_retain = true; break;
11997
11998 case RID_GETTER:
11999 case RID_SETTER:
12000 if (c_parser_next_token_is_not (parser, CPP_EQ))
12001 {
12002 if (keyword == RID_GETTER)
12003 c_parser_error (parser,
12004 "missing %<=%> (after %<getter%> attribute)");
12005 else
12006 c_parser_error (parser,
12007 "missing %<=%> (after %<setter%> attribute)");
12008 syntax_error = true;
12009 break;
12010 }
12011 c_parser_consume_token (parser); /* eat the = */
12012 if (c_parser_next_token_is_not (parser, CPP_NAME))
12013 {
12014 c_parser_error (parser, "expected identifier");
12015 syntax_error = true;
12016 break;
12017 }
12018 if (keyword == RID_SETTER)
12019 {
12020 if (property_setter_ident != NULL_TREE)
12021 c_parser_error (parser, "the %<setter%> attribute may only be specified once");
12022 else
12023 property_setter_ident = c_parser_peek_token (parser)->value;
12024 c_parser_consume_token (parser);
12025 if (c_parser_next_token_is_not (parser, CPP_COLON))
12026 c_parser_error (parser, "setter name must terminate with %<:%>");
12027 else
12028 c_parser_consume_token (parser);
12029 }
12030 else
12031 {
12032 if (property_getter_ident != NULL_TREE)
12033 c_parser_error (parser, "the %<getter%> attribute may only be specified once");
12034 else
12035 property_getter_ident = c_parser_peek_token (parser)->value;
12036 c_parser_consume_token (parser);
12037 }
12038 break;
12039 default:
12040 c_parser_error (parser, "unknown property attribute");
12041 syntax_error = true;
12042 break;
12043 }
12044
12045 if (syntax_error)
12046 break;
12047
12048 if (c_parser_next_token_is (parser, CPP_COMMA))
12049 c_parser_consume_token (parser);
12050 else
12051 break;
12052 }
12053 parser->objc_property_attr_context = false;
12054 parens.skip_until_found_close (parser);
12055 }
12056 /* ... and the property declaration(s). */
12057 properties = c_parser_struct_declaration (parser);
12058
12059 if (properties == error_mark_node)
12060 {
12061 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
12062 parser->error = false;
12063 return;
12064 }
12065
12066 if (properties == NULL_TREE)
12067 c_parser_error (parser, "expected identifier");
12068 else
12069 {
12070 /* Comma-separated properties are chained together in
12071 reverse order; add them one by one. */
12072 properties = nreverse (properties);
12073
12074 for (; properties; properties = TREE_CHAIN (properties))
12075 objc_add_property_declaration (loc, copy_node (properties),
12076 property_readonly, property_readwrite,
12077 property_assign, property_retain,
12078 property_copy, property_nonatomic,
12079 property_getter_ident, property_setter_ident);
12080 }
12081
12082 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
12083 parser->error = false;
12084 }
12085
12086 /* Parse an Objective-C @synthesize declaration. The syntax is:
12087
12088 objc-synthesize-declaration:
12089 @synthesize objc-synthesize-identifier-list ;
12090
12091 objc-synthesize-identifier-list:
12092 objc-synthesize-identifier
12093 objc-synthesize-identifier-list, objc-synthesize-identifier
12094
12095 objc-synthesize-identifier
12096 identifier
12097 identifier = identifier
12098
12099 For example:
12100 @synthesize MyProperty;
12101 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
12102
12103 PS: This function is identical to cp_parser_objc_at_synthesize_declaration
12104 for C++. Keep them in sync.
12105 */
12106 static void
12107 c_parser_objc_at_synthesize_declaration (c_parser *parser)
12108 {
12109 tree list = NULL_TREE;
12110 location_t loc;
12111 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNTHESIZE));
12112 loc = c_parser_peek_token (parser)->location;
12113
12114 c_parser_consume_token (parser);
12115 while (true)
12116 {
12117 tree property, ivar;
12118 if (c_parser_next_token_is_not (parser, CPP_NAME))
12119 {
12120 c_parser_error (parser, "expected identifier");
12121 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
12122 /* Once we find the semicolon, we can resume normal parsing.
12123 We have to reset parser->error manually because
12124 c_parser_skip_until_found() won't reset it for us if the
12125 next token is precisely a semicolon. */
12126 parser->error = false;
12127 return;
12128 }
12129 property = c_parser_peek_token (parser)->value;
12130 c_parser_consume_token (parser);
12131 if (c_parser_next_token_is (parser, CPP_EQ))
12132 {
12133 c_parser_consume_token (parser);
12134 if (c_parser_next_token_is_not (parser, CPP_NAME))
12135 {
12136 c_parser_error (parser, "expected identifier");
12137 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
12138 parser->error = false;
12139 return;
12140 }
12141 ivar = c_parser_peek_token (parser)->value;
12142 c_parser_consume_token (parser);
12143 }
12144 else
12145 ivar = NULL_TREE;
12146 list = chainon (list, build_tree_list (ivar, property));
12147 if (c_parser_next_token_is (parser, CPP_COMMA))
12148 c_parser_consume_token (parser);
12149 else
12150 break;
12151 }
12152 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
12153 objc_add_synthesize_declaration (loc, list);
12154 }
12155
12156 /* Parse an Objective-C @dynamic declaration. The syntax is:
12157
12158 objc-dynamic-declaration:
12159 @dynamic identifier-list ;
12160
12161 For example:
12162 @dynamic MyProperty;
12163 @dynamic MyProperty, AnotherProperty;
12164
12165 PS: This function is identical to cp_parser_objc_at_dynamic_declaration
12166 for C++. Keep them in sync.
12167 */
12168 static void
12169 c_parser_objc_at_dynamic_declaration (c_parser *parser)
12170 {
12171 tree list = NULL_TREE;
12172 location_t loc;
12173 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_DYNAMIC));
12174 loc = c_parser_peek_token (parser)->location;
12175
12176 c_parser_consume_token (parser);
12177 while (true)
12178 {
12179 tree property;
12180 if (c_parser_next_token_is_not (parser, CPP_NAME))
12181 {
12182 c_parser_error (parser, "expected identifier");
12183 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
12184 parser->error = false;
12185 return;
12186 }
12187 property = c_parser_peek_token (parser)->value;
12188 list = chainon (list, build_tree_list (NULL_TREE, property));
12189 c_parser_consume_token (parser);
12190 if (c_parser_next_token_is (parser, CPP_COMMA))
12191 c_parser_consume_token (parser);
12192 else
12193 break;
12194 }
12195 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
12196 objc_add_dynamic_declaration (loc, list);
12197 }
12198
12199 \f
12200 /* Parse a pragma GCC ivdep. */
12201
12202 static bool
12203 c_parse_pragma_ivdep (c_parser *parser)
12204 {
12205 c_parser_consume_pragma (parser);
12206 c_parser_skip_to_pragma_eol (parser);
12207 return true;
12208 }
12209
12210 /* Parse a pragma GCC unroll. */
12211
12212 static unsigned short
12213 c_parser_pragma_unroll (c_parser *parser)
12214 {
12215 unsigned short unroll;
12216 c_parser_consume_pragma (parser);
12217 location_t location = c_parser_peek_token (parser)->location;
12218 tree expr = c_parser_expr_no_commas (parser, NULL).value;
12219 mark_exp_read (expr);
12220 expr = c_fully_fold (expr, false, NULL);
12221 HOST_WIDE_INT lunroll = 0;
12222 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr))
12223 || TREE_CODE (expr) != INTEGER_CST
12224 || (lunroll = tree_to_shwi (expr)) < 0
12225 || lunroll >= USHRT_MAX)
12226 {
12227 error_at (location, "%<#pragma GCC unroll%> requires an"
12228 " assignment-expression that evaluates to a non-negative"
12229 " integral constant less than %u", USHRT_MAX);
12230 unroll = 0;
12231 }
12232 else
12233 {
12234 unroll = (unsigned short)lunroll;
12235 if (unroll == 0)
12236 unroll = 1;
12237 }
12238
12239 c_parser_skip_to_pragma_eol (parser);
12240 return unroll;
12241 }
12242
12243 /* Handle pragmas. Some OpenMP pragmas are associated with, and therefore
12244 should be considered, statements. ALLOW_STMT is true if we're within
12245 the context of a function and such pragmas are to be allowed. Returns
12246 true if we actually parsed such a pragma. */
12247
12248 static bool
12249 c_parser_pragma (c_parser *parser, enum pragma_context context, bool *if_p)
12250 {
12251 unsigned int id;
12252 const char *construct = NULL;
12253
12254 id = c_parser_peek_token (parser)->pragma_kind;
12255 gcc_assert (id != PRAGMA_NONE);
12256
12257 switch (id)
12258 {
12259 case PRAGMA_OACC_DECLARE:
12260 c_parser_oacc_declare (parser);
12261 return false;
12262
12263 case PRAGMA_OACC_ENTER_DATA:
12264 if (context != pragma_compound)
12265 {
12266 construct = "acc enter data";
12267 in_compound:
12268 if (context == pragma_stmt)
12269 {
12270 error_at (c_parser_peek_token (parser)->location,
12271 "%<#pragma %s%> may only be used in compound "
12272 "statements", construct);
12273 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
12274 return false;
12275 }
12276 goto bad_stmt;
12277 }
12278 c_parser_oacc_enter_exit_data (parser, true);
12279 return false;
12280
12281 case PRAGMA_OACC_EXIT_DATA:
12282 if (context != pragma_compound)
12283 {
12284 construct = "acc exit data";
12285 goto in_compound;
12286 }
12287 c_parser_oacc_enter_exit_data (parser, false);
12288 return false;
12289
12290 case PRAGMA_OACC_ROUTINE:
12291 if (context != pragma_external)
12292 {
12293 error_at (c_parser_peek_token (parser)->location,
12294 "%<#pragma acc routine%> must be at file scope");
12295 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
12296 return false;
12297 }
12298 c_parser_oacc_routine (parser, context);
12299 return false;
12300
12301 case PRAGMA_OACC_UPDATE:
12302 if (context != pragma_compound)
12303 {
12304 construct = "acc update";
12305 goto in_compound;
12306 }
12307 c_parser_oacc_update (parser);
12308 return false;
12309
12310 case PRAGMA_OMP_BARRIER:
12311 if (context != pragma_compound)
12312 {
12313 construct = "omp barrier";
12314 goto in_compound;
12315 }
12316 c_parser_omp_barrier (parser);
12317 return false;
12318
12319 case PRAGMA_OMP_DEPOBJ:
12320 if (context != pragma_compound)
12321 {
12322 construct = "omp depobj";
12323 goto in_compound;
12324 }
12325 c_parser_omp_depobj (parser);
12326 return false;
12327
12328 case PRAGMA_OMP_FLUSH:
12329 if (context != pragma_compound)
12330 {
12331 construct = "omp flush";
12332 goto in_compound;
12333 }
12334 c_parser_omp_flush (parser);
12335 return false;
12336
12337 case PRAGMA_OMP_TASKWAIT:
12338 if (context != pragma_compound)
12339 {
12340 construct = "omp taskwait";
12341 goto in_compound;
12342 }
12343 c_parser_omp_taskwait (parser);
12344 return false;
12345
12346 case PRAGMA_OMP_TASKYIELD:
12347 if (context != pragma_compound)
12348 {
12349 construct = "omp taskyield";
12350 goto in_compound;
12351 }
12352 c_parser_omp_taskyield (parser);
12353 return false;
12354
12355 case PRAGMA_OMP_CANCEL:
12356 if (context != pragma_compound)
12357 {
12358 construct = "omp cancel";
12359 goto in_compound;
12360 }
12361 c_parser_omp_cancel (parser);
12362 return false;
12363
12364 case PRAGMA_OMP_CANCELLATION_POINT:
12365 c_parser_omp_cancellation_point (parser, context);
12366 return false;
12367
12368 case PRAGMA_OMP_THREADPRIVATE:
12369 c_parser_omp_threadprivate (parser);
12370 return false;
12371
12372 case PRAGMA_OMP_TARGET:
12373 return c_parser_omp_target (parser, context, if_p);
12374
12375 case PRAGMA_OMP_END_DECLARE_TARGET:
12376 c_parser_omp_end_declare_target (parser);
12377 return false;
12378
12379 case PRAGMA_OMP_SCAN:
12380 error_at (c_parser_peek_token (parser)->location,
12381 "%<#pragma omp scan%> may only be used in "
12382 "a loop construct with %<inscan%> %<reduction%> clause");
12383 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
12384 return false;
12385
12386 case PRAGMA_OMP_SECTION:
12387 error_at (c_parser_peek_token (parser)->location,
12388 "%<#pragma omp section%> may only be used in "
12389 "%<#pragma omp sections%> construct");
12390 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
12391 return false;
12392
12393 case PRAGMA_OMP_DECLARE:
12394 c_parser_omp_declare (parser, context);
12395 return false;
12396
12397 case PRAGMA_OMP_REQUIRES:
12398 c_parser_omp_requires (parser);
12399 return false;
12400
12401 case PRAGMA_OMP_ORDERED:
12402 return c_parser_omp_ordered (parser, context, if_p);
12403
12404 case PRAGMA_IVDEP:
12405 {
12406 const bool ivdep = c_parse_pragma_ivdep (parser);
12407 unsigned short unroll;
12408 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_UNROLL)
12409 unroll = c_parser_pragma_unroll (parser);
12410 else
12411 unroll = 0;
12412 if (!c_parser_next_token_is_keyword (parser, RID_FOR)
12413 && !c_parser_next_token_is_keyword (parser, RID_WHILE)
12414 && !c_parser_next_token_is_keyword (parser, RID_DO))
12415 {
12416 c_parser_error (parser, "for, while or do statement expected");
12417 return false;
12418 }
12419 if (c_parser_next_token_is_keyword (parser, RID_FOR))
12420 c_parser_for_statement (parser, ivdep, unroll, if_p);
12421 else if (c_parser_next_token_is_keyword (parser, RID_WHILE))
12422 c_parser_while_statement (parser, ivdep, unroll, if_p);
12423 else
12424 c_parser_do_statement (parser, ivdep, unroll);
12425 }
12426 return false;
12427
12428 case PRAGMA_UNROLL:
12429 {
12430 unsigned short unroll = c_parser_pragma_unroll (parser);
12431 bool ivdep;
12432 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_IVDEP)
12433 ivdep = c_parse_pragma_ivdep (parser);
12434 else
12435 ivdep = false;
12436 if (!c_parser_next_token_is_keyword (parser, RID_FOR)
12437 && !c_parser_next_token_is_keyword (parser, RID_WHILE)
12438 && !c_parser_next_token_is_keyword (parser, RID_DO))
12439 {
12440 c_parser_error (parser, "for, while or do statement expected");
12441 return false;
12442 }
12443 if (c_parser_next_token_is_keyword (parser, RID_FOR))
12444 c_parser_for_statement (parser, ivdep, unroll, if_p);
12445 else if (c_parser_next_token_is_keyword (parser, RID_WHILE))
12446 c_parser_while_statement (parser, ivdep, unroll, if_p);
12447 else
12448 c_parser_do_statement (parser, ivdep, unroll);
12449 }
12450 return false;
12451
12452 case PRAGMA_GCC_PCH_PREPROCESS:
12453 c_parser_error (parser, "%<#pragma GCC pch_preprocess%> must be first");
12454 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
12455 return false;
12456
12457 case PRAGMA_OACC_WAIT:
12458 if (context != pragma_compound)
12459 {
12460 construct = "acc wait";
12461 goto in_compound;
12462 }
12463 /* FALL THROUGH. */
12464
12465 default:
12466 if (id < PRAGMA_FIRST_EXTERNAL)
12467 {
12468 if (context != pragma_stmt && context != pragma_compound)
12469 {
12470 bad_stmt:
12471 c_parser_error (parser, "expected declaration specifiers");
12472 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
12473 return false;
12474 }
12475 c_parser_omp_construct (parser, if_p);
12476 return true;
12477 }
12478 break;
12479 }
12480
12481 c_parser_consume_pragma (parser);
12482 c_invoke_pragma_handler (id);
12483
12484 /* Skip to EOL, but suppress any error message. Those will have been
12485 generated by the handler routine through calling error, as opposed
12486 to calling c_parser_error. */
12487 parser->error = true;
12488 c_parser_skip_to_pragma_eol (parser);
12489
12490 return false;
12491 }
12492
12493 /* The interface the pragma parsers have to the lexer. */
12494
12495 enum cpp_ttype
12496 pragma_lex (tree *value, location_t *loc)
12497 {
12498 c_token *tok = c_parser_peek_token (the_parser);
12499 enum cpp_ttype ret = tok->type;
12500
12501 *value = tok->value;
12502 if (loc)
12503 *loc = tok->location;
12504
12505 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
12506 ret = CPP_EOF;
12507 else if (ret == CPP_STRING)
12508 *value = c_parser_string_literal (the_parser, false, false).value;
12509 else
12510 {
12511 if (ret == CPP_KEYWORD)
12512 ret = CPP_NAME;
12513 c_parser_consume_token (the_parser);
12514 }
12515
12516 return ret;
12517 }
12518
12519 static void
12520 c_parser_pragma_pch_preprocess (c_parser *parser)
12521 {
12522 tree name = NULL;
12523
12524 parser->lex_joined_string = true;
12525 c_parser_consume_pragma (parser);
12526 if (c_parser_next_token_is (parser, CPP_STRING))
12527 {
12528 name = c_parser_peek_token (parser)->value;
12529 c_parser_consume_token (parser);
12530 }
12531 else
12532 c_parser_error (parser, "expected string literal");
12533 c_parser_skip_to_pragma_eol (parser);
12534 parser->lex_joined_string = false;
12535
12536 if (name)
12537 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
12538 }
12539 \f
12540 /* OpenACC and OpenMP parsing routines. */
12541
12542 /* Returns name of the next clause.
12543 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
12544 the token is not consumed. Otherwise appropriate pragma_omp_clause is
12545 returned and the token is consumed. */
12546
12547 static pragma_omp_clause
12548 c_parser_omp_clause_name (c_parser *parser)
12549 {
12550 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
12551
12552 if (c_parser_next_token_is_keyword (parser, RID_AUTO))
12553 result = PRAGMA_OACC_CLAUSE_AUTO;
12554 else if (c_parser_next_token_is_keyword (parser, RID_IF))
12555 result = PRAGMA_OMP_CLAUSE_IF;
12556 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
12557 result = PRAGMA_OMP_CLAUSE_DEFAULT;
12558 else if (c_parser_next_token_is_keyword (parser, RID_FOR))
12559 result = PRAGMA_OMP_CLAUSE_FOR;
12560 else if (c_parser_next_token_is (parser, CPP_NAME))
12561 {
12562 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12563
12564 switch (p[0])
12565 {
12566 case 'a':
12567 if (!strcmp ("aligned", p))
12568 result = PRAGMA_OMP_CLAUSE_ALIGNED;
12569 else if (!strcmp ("async", p))
12570 result = PRAGMA_OACC_CLAUSE_ASYNC;
12571 else if (!strcmp ("attach", p))
12572 result = PRAGMA_OACC_CLAUSE_ATTACH;
12573 break;
12574 case 'b':
12575 if (!strcmp ("bind", p))
12576 result = PRAGMA_OMP_CLAUSE_BIND;
12577 break;
12578 case 'c':
12579 if (!strcmp ("collapse", p))
12580 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
12581 else if (!strcmp ("copy", p))
12582 result = PRAGMA_OACC_CLAUSE_COPY;
12583 else if (!strcmp ("copyin", p))
12584 result = PRAGMA_OMP_CLAUSE_COPYIN;
12585 else if (!strcmp ("copyout", p))
12586 result = PRAGMA_OACC_CLAUSE_COPYOUT;
12587 else if (!strcmp ("copyprivate", p))
12588 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
12589 else if (!strcmp ("create", p))
12590 result = PRAGMA_OACC_CLAUSE_CREATE;
12591 break;
12592 case 'd':
12593 if (!strcmp ("defaultmap", p))
12594 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
12595 else if (!strcmp ("delete", p))
12596 result = PRAGMA_OACC_CLAUSE_DELETE;
12597 else if (!strcmp ("depend", p))
12598 result = PRAGMA_OMP_CLAUSE_DEPEND;
12599 else if (!strcmp ("detach", p))
12600 result = PRAGMA_OACC_CLAUSE_DETACH;
12601 else if (!strcmp ("device", p))
12602 result = PRAGMA_OMP_CLAUSE_DEVICE;
12603 else if (!strcmp ("deviceptr", p))
12604 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
12605 else if (!strcmp ("device_resident", p))
12606 result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
12607 else if (!strcmp ("device_type", p))
12608 result = PRAGMA_OMP_CLAUSE_DEVICE_TYPE;
12609 else if (!strcmp ("dist_schedule", p))
12610 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
12611 break;
12612 case 'f':
12613 if (!strcmp ("final", p))
12614 result = PRAGMA_OMP_CLAUSE_FINAL;
12615 else if (!strcmp ("finalize", p))
12616 result = PRAGMA_OACC_CLAUSE_FINALIZE;
12617 else if (!strcmp ("firstprivate", p))
12618 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
12619 else if (!strcmp ("from", p))
12620 result = PRAGMA_OMP_CLAUSE_FROM;
12621 break;
12622 case 'g':
12623 if (!strcmp ("gang", p))
12624 result = PRAGMA_OACC_CLAUSE_GANG;
12625 else if (!strcmp ("grainsize", p))
12626 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
12627 break;
12628 case 'h':
12629 if (!strcmp ("hint", p))
12630 result = PRAGMA_OMP_CLAUSE_HINT;
12631 else if (!strcmp ("host", p))
12632 result = PRAGMA_OACC_CLAUSE_HOST;
12633 break;
12634 case 'i':
12635 if (!strcmp ("if_present", p))
12636 result = PRAGMA_OACC_CLAUSE_IF_PRESENT;
12637 else if (!strcmp ("in_reduction", p))
12638 result = PRAGMA_OMP_CLAUSE_IN_REDUCTION;
12639 else if (!strcmp ("inbranch", p))
12640 result = PRAGMA_OMP_CLAUSE_INBRANCH;
12641 else if (!strcmp ("independent", p))
12642 result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
12643 else if (!strcmp ("is_device_ptr", p))
12644 result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
12645 break;
12646 case 'l':
12647 if (!strcmp ("lastprivate", p))
12648 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
12649 else if (!strcmp ("linear", p))
12650 result = PRAGMA_OMP_CLAUSE_LINEAR;
12651 else if (!strcmp ("link", p))
12652 result = PRAGMA_OMP_CLAUSE_LINK;
12653 break;
12654 case 'm':
12655 if (!strcmp ("map", p))
12656 result = PRAGMA_OMP_CLAUSE_MAP;
12657 else if (!strcmp ("mergeable", p))
12658 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
12659 break;
12660 case 'n':
12661 if (!strcmp ("no_create", p))
12662 result = PRAGMA_OACC_CLAUSE_NO_CREATE;
12663 else if (!strcmp ("nogroup", p))
12664 result = PRAGMA_OMP_CLAUSE_NOGROUP;
12665 else if (!strcmp ("nontemporal", p))
12666 result = PRAGMA_OMP_CLAUSE_NONTEMPORAL;
12667 else if (!strcmp ("notinbranch", p))
12668 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
12669 else if (!strcmp ("nowait", p))
12670 result = PRAGMA_OMP_CLAUSE_NOWAIT;
12671 else if (!strcmp ("num_gangs", p))
12672 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
12673 else if (!strcmp ("num_tasks", p))
12674 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
12675 else if (!strcmp ("num_teams", p))
12676 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
12677 else if (!strcmp ("num_threads", p))
12678 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
12679 else if (!strcmp ("num_workers", p))
12680 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
12681 break;
12682 case 'o':
12683 if (!strcmp ("ordered", p))
12684 result = PRAGMA_OMP_CLAUSE_ORDERED;
12685 else if (!strcmp ("order", p))
12686 result = PRAGMA_OMP_CLAUSE_ORDER;
12687 break;
12688 case 'p':
12689 if (!strcmp ("parallel", p))
12690 result = PRAGMA_OMP_CLAUSE_PARALLEL;
12691 else if (!strcmp ("present", p))
12692 result = PRAGMA_OACC_CLAUSE_PRESENT;
12693 /* As of OpenACC 2.5, these are now aliases of the non-present_or
12694 clauses. */
12695 else if (!strcmp ("present_or_copy", p)
12696 || !strcmp ("pcopy", p))
12697 result = PRAGMA_OACC_CLAUSE_COPY;
12698 else if (!strcmp ("present_or_copyin", p)
12699 || !strcmp ("pcopyin", p))
12700 result = PRAGMA_OACC_CLAUSE_COPYIN;
12701 else if (!strcmp ("present_or_copyout", p)
12702 || !strcmp ("pcopyout", p))
12703 result = PRAGMA_OACC_CLAUSE_COPYOUT;
12704 else if (!strcmp ("present_or_create", p)
12705 || !strcmp ("pcreate", p))
12706 result = PRAGMA_OACC_CLAUSE_CREATE;
12707 else if (!strcmp ("priority", p))
12708 result = PRAGMA_OMP_CLAUSE_PRIORITY;
12709 else if (!strcmp ("private", p))
12710 result = PRAGMA_OMP_CLAUSE_PRIVATE;
12711 else if (!strcmp ("proc_bind", p))
12712 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
12713 break;
12714 case 'r':
12715 if (!strcmp ("reduction", p))
12716 result = PRAGMA_OMP_CLAUSE_REDUCTION;
12717 break;
12718 case 's':
12719 if (!strcmp ("safelen", p))
12720 result = PRAGMA_OMP_CLAUSE_SAFELEN;
12721 else if (!strcmp ("schedule", p))
12722 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
12723 else if (!strcmp ("sections", p))
12724 result = PRAGMA_OMP_CLAUSE_SECTIONS;
12725 else if (!strcmp ("self", p)) /* "self" is a synonym for "host". */
12726 result = PRAGMA_OACC_CLAUSE_HOST;
12727 else if (!strcmp ("seq", p))
12728 result = PRAGMA_OACC_CLAUSE_SEQ;
12729 else if (!strcmp ("shared", p))
12730 result = PRAGMA_OMP_CLAUSE_SHARED;
12731 else if (!strcmp ("simd", p))
12732 result = PRAGMA_OMP_CLAUSE_SIMD;
12733 else if (!strcmp ("simdlen", p))
12734 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
12735 break;
12736 case 't':
12737 if (!strcmp ("task_reduction", p))
12738 result = PRAGMA_OMP_CLAUSE_TASK_REDUCTION;
12739 else if (!strcmp ("taskgroup", p))
12740 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
12741 else if (!strcmp ("thread_limit", p))
12742 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
12743 else if (!strcmp ("threads", p))
12744 result = PRAGMA_OMP_CLAUSE_THREADS;
12745 else if (!strcmp ("tile", p))
12746 result = PRAGMA_OACC_CLAUSE_TILE;
12747 else if (!strcmp ("to", p))
12748 result = PRAGMA_OMP_CLAUSE_TO;
12749 break;
12750 case 'u':
12751 if (!strcmp ("uniform", p))
12752 result = PRAGMA_OMP_CLAUSE_UNIFORM;
12753 else if (!strcmp ("untied", p))
12754 result = PRAGMA_OMP_CLAUSE_UNTIED;
12755 else if (!strcmp ("use_device", p))
12756 result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
12757 else if (!strcmp ("use_device_addr", p))
12758 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_ADDR;
12759 else if (!strcmp ("use_device_ptr", p))
12760 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
12761 break;
12762 case 'v':
12763 if (!strcmp ("vector", p))
12764 result = PRAGMA_OACC_CLAUSE_VECTOR;
12765 else if (!strcmp ("vector_length", p))
12766 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
12767 break;
12768 case 'w':
12769 if (!strcmp ("wait", p))
12770 result = PRAGMA_OACC_CLAUSE_WAIT;
12771 else if (!strcmp ("worker", p))
12772 result = PRAGMA_OACC_CLAUSE_WORKER;
12773 break;
12774 }
12775 }
12776
12777 if (result != PRAGMA_OMP_CLAUSE_NONE)
12778 c_parser_consume_token (parser);
12779
12780 return result;
12781 }
12782
12783 /* Validate that a clause of the given type does not already exist. */
12784
12785 static void
12786 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
12787 const char *name)
12788 {
12789 if (tree c = omp_find_clause (clauses, code))
12790 error_at (OMP_CLAUSE_LOCATION (c), "too many %qs clauses", name);
12791 }
12792
12793 /* OpenACC 2.0
12794 Parse wait clause or wait directive parameters. */
12795
12796 static tree
12797 c_parser_oacc_wait_list (c_parser *parser, location_t clause_loc, tree list)
12798 {
12799 vec<tree, va_gc> *args;
12800 tree t, args_tree;
12801
12802 matching_parens parens;
12803 if (!parens.require_open (parser))
12804 return list;
12805
12806 args = c_parser_expr_list (parser, false, true, NULL, NULL, NULL, NULL);
12807 args_tree = build_tree_list_vec (args);
12808
12809 for (t = args_tree; t; t = TREE_CHAIN (t))
12810 {
12811 tree targ = TREE_VALUE (t);
12812
12813 if (targ != error_mark_node)
12814 {
12815 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
12816 {
12817 c_parser_error (parser, "expression must be integral");
12818 targ = error_mark_node;
12819 }
12820 else
12821 {
12822 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
12823
12824 OMP_CLAUSE_DECL (c) = targ;
12825 OMP_CLAUSE_CHAIN (c) = list;
12826 list = c;
12827 }
12828 }
12829 }
12830
12831 release_tree_vector (args);
12832 parens.require_close (parser);
12833 return list;
12834 }
12835
12836 /* OpenACC 2.0, OpenMP 2.5:
12837 variable-list:
12838 identifier
12839 variable-list , identifier
12840
12841 If KIND is nonzero, create the appropriate node and install the
12842 decl in OMP_CLAUSE_DECL and add the node to the head of the list.
12843 If KIND is nonzero, CLAUSE_LOC is the location of the clause.
12844
12845 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
12846 return the list created.
12847
12848 The optional ALLOW_DEREF argument is true if list items can use the deref
12849 (->) operator. */
12850
12851 static tree
12852 c_parser_omp_variable_list (c_parser *parser,
12853 location_t clause_loc,
12854 enum omp_clause_code kind, tree list,
12855 bool allow_deref = false)
12856 {
12857 auto_vec<c_token> tokens;
12858 unsigned int tokens_avail = 0;
12859 bool first = true;
12860
12861 while (1)
12862 {
12863 bool array_section_p = false;
12864 if (kind == OMP_CLAUSE_DEPEND)
12865 {
12866 if (c_parser_next_token_is_not (parser, CPP_NAME)
12867 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
12868 {
12869 struct c_expr expr = c_parser_expr_no_commas (parser, NULL);
12870 if (expr.value != error_mark_node)
12871 {
12872 tree u = build_omp_clause (clause_loc, kind);
12873 OMP_CLAUSE_DECL (u) = expr.value;
12874 OMP_CLAUSE_CHAIN (u) = list;
12875 list = u;
12876 }
12877
12878 if (c_parser_next_token_is_not (parser, CPP_COMMA))
12879 break;
12880
12881 c_parser_consume_token (parser);
12882 first = false;
12883 continue;
12884 }
12885
12886 tokens.truncate (0);
12887 unsigned int nesting_depth = 0;
12888 while (1)
12889 {
12890 c_token *token = c_parser_peek_token (parser);
12891 switch (token->type)
12892 {
12893 case CPP_EOF:
12894 case CPP_PRAGMA_EOL:
12895 break;
12896 case CPP_OPEN_BRACE:
12897 case CPP_OPEN_PAREN:
12898 case CPP_OPEN_SQUARE:
12899 ++nesting_depth;
12900 goto add;
12901 case CPP_CLOSE_BRACE:
12902 case CPP_CLOSE_PAREN:
12903 case CPP_CLOSE_SQUARE:
12904 if (nesting_depth-- == 0)
12905 break;
12906 goto add;
12907 case CPP_COMMA:
12908 if (nesting_depth == 0)
12909 break;
12910 goto add;
12911 default:
12912 add:
12913 tokens.safe_push (*token);
12914 c_parser_consume_token (parser);
12915 continue;
12916 }
12917 break;
12918 }
12919
12920 /* Make sure nothing tries to read past the end of the tokens. */
12921 c_token eof_token;
12922 memset (&eof_token, 0, sizeof (eof_token));
12923 eof_token.type = CPP_EOF;
12924 tokens.safe_push (eof_token);
12925 tokens.safe_push (eof_token);
12926
12927 tokens_avail = parser->tokens_avail;
12928 gcc_assert (parser->tokens == &parser->tokens_buf[0]);
12929 parser->tokens = tokens.address ();
12930 parser->tokens_avail = tokens.length ();
12931 }
12932
12933 tree t = NULL_TREE;
12934
12935 if (c_parser_next_token_is (parser, CPP_NAME)
12936 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
12937 {
12938 t = lookup_name (c_parser_peek_token (parser)->value);
12939
12940 if (t == NULL_TREE)
12941 {
12942 undeclared_variable (c_parser_peek_token (parser)->location,
12943 c_parser_peek_token (parser)->value);
12944 t = error_mark_node;
12945 }
12946
12947 c_parser_consume_token (parser);
12948 }
12949 else if (c_parser_next_token_is (parser, CPP_KEYWORD)
12950 && (c_parser_peek_token (parser)->keyword == RID_FUNCTION_NAME
12951 || (c_parser_peek_token (parser)->keyword
12952 == RID_PRETTY_FUNCTION_NAME)
12953 || (c_parser_peek_token (parser)->keyword
12954 == RID_C99_FUNCTION_NAME)))
12955 t = c_parser_predefined_identifier (parser).value;
12956 else
12957 {
12958 if (first)
12959 c_parser_error (parser, "expected identifier");
12960 break;
12961 }
12962
12963 if (t == error_mark_node)
12964 ;
12965 else if (kind != 0)
12966 {
12967 switch (kind)
12968 {
12969 case OMP_CLAUSE__CACHE_:
12970 /* The OpenACC cache directive explicitly only allows "array
12971 elements or subarrays". */
12972 if (c_parser_peek_token (parser)->type != CPP_OPEN_SQUARE)
12973 {
12974 c_parser_error (parser, "expected %<[%>");
12975 t = error_mark_node;
12976 break;
12977 }
12978 /* FALLTHROUGH */
12979 case OMP_CLAUSE_MAP:
12980 case OMP_CLAUSE_FROM:
12981 case OMP_CLAUSE_TO:
12982 while (c_parser_next_token_is (parser, CPP_DOT)
12983 || (allow_deref
12984 && c_parser_next_token_is (parser, CPP_DEREF)))
12985 {
12986 location_t op_loc = c_parser_peek_token (parser)->location;
12987 if (c_parser_next_token_is (parser, CPP_DEREF))
12988 t = build_simple_mem_ref (t);
12989 c_parser_consume_token (parser);
12990 if (!c_parser_next_token_is (parser, CPP_NAME))
12991 {
12992 c_parser_error (parser, "expected identifier");
12993 t = error_mark_node;
12994 break;
12995 }
12996
12997 c_token *comp_tok = c_parser_peek_token (parser);
12998 tree ident = comp_tok->value;
12999 location_t comp_loc = comp_tok->location;
13000 c_parser_consume_token (parser);
13001 t = build_component_ref (op_loc, t, ident, comp_loc);
13002 }
13003 /* FALLTHROUGH */
13004 case OMP_CLAUSE_DEPEND:
13005 case OMP_CLAUSE_REDUCTION:
13006 case OMP_CLAUSE_IN_REDUCTION:
13007 case OMP_CLAUSE_TASK_REDUCTION:
13008 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
13009 {
13010 tree low_bound = NULL_TREE, length = NULL_TREE;
13011
13012 c_parser_consume_token (parser);
13013 if (!c_parser_next_token_is (parser, CPP_COLON))
13014 {
13015 location_t expr_loc
13016 = c_parser_peek_token (parser)->location;
13017 c_expr expr = c_parser_expression (parser);
13018 expr = convert_lvalue_to_rvalue (expr_loc, expr,
13019 false, true);
13020 low_bound = expr.value;
13021 }
13022 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
13023 length = integer_one_node;
13024 else
13025 {
13026 /* Look for `:'. */
13027 if (!c_parser_require (parser, CPP_COLON,
13028 "expected %<:%>"))
13029 {
13030 t = error_mark_node;
13031 break;
13032 }
13033 array_section_p = true;
13034 if (!c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
13035 {
13036 location_t expr_loc
13037 = c_parser_peek_token (parser)->location;
13038 c_expr expr = c_parser_expression (parser);
13039 expr = convert_lvalue_to_rvalue (expr_loc, expr,
13040 false, true);
13041 length = expr.value;
13042 }
13043 }
13044 /* Look for the closing `]'. */
13045 if (!c_parser_require (parser, CPP_CLOSE_SQUARE,
13046 "expected %<]%>"))
13047 {
13048 t = error_mark_node;
13049 break;
13050 }
13051
13052 t = tree_cons (low_bound, length, t);
13053 }
13054 if (kind == OMP_CLAUSE_DEPEND
13055 && t != error_mark_node
13056 && parser->tokens_avail != 2)
13057 {
13058 if (array_section_p)
13059 {
13060 error_at (c_parser_peek_token (parser)->location,
13061 "expected %<)%> or %<,%>");
13062 t = error_mark_node;
13063 }
13064 else
13065 {
13066 parser->tokens = tokens.address ();
13067 parser->tokens_avail = tokens.length ();
13068
13069 t = c_parser_expr_no_commas (parser, NULL).value;
13070 if (t != error_mark_node && parser->tokens_avail != 2)
13071 {
13072 error_at (c_parser_peek_token (parser)->location,
13073 "expected %<)%> or %<,%>");
13074 t = error_mark_node;
13075 }
13076 }
13077 }
13078 break;
13079 default:
13080 break;
13081 }
13082
13083 if (t != error_mark_node)
13084 {
13085 tree u = build_omp_clause (clause_loc, kind);
13086 OMP_CLAUSE_DECL (u) = t;
13087 OMP_CLAUSE_CHAIN (u) = list;
13088 list = u;
13089 }
13090 }
13091 else
13092 list = tree_cons (t, NULL_TREE, list);
13093
13094 if (kind == OMP_CLAUSE_DEPEND)
13095 {
13096 parser->tokens = &parser->tokens_buf[0];
13097 parser->tokens_avail = tokens_avail;
13098 }
13099 if (c_parser_next_token_is_not (parser, CPP_COMMA))
13100 break;
13101
13102 c_parser_consume_token (parser);
13103 first = false;
13104 }
13105
13106 return list;
13107 }
13108
13109 /* Similarly, but expect leading and trailing parenthesis. This is a very
13110 common case for OpenACC and OpenMP clauses. The optional ALLOW_DEREF
13111 argument is true if list items can use the deref (->) operator. */
13112
13113 static tree
13114 c_parser_omp_var_list_parens (c_parser *parser, enum omp_clause_code kind,
13115 tree list, bool allow_deref = false)
13116 {
13117 /* The clauses location. */
13118 location_t loc = c_parser_peek_token (parser)->location;
13119
13120 matching_parens parens;
13121 if (parens.require_open (parser))
13122 {
13123 list = c_parser_omp_variable_list (parser, loc, kind, list, allow_deref);
13124 parens.skip_until_found_close (parser);
13125 }
13126 return list;
13127 }
13128
13129 /* OpenACC 2.0:
13130 copy ( variable-list )
13131 copyin ( variable-list )
13132 copyout ( variable-list )
13133 create ( variable-list )
13134 delete ( variable-list )
13135 present ( variable-list )
13136
13137 OpenACC 2.6:
13138 no_create ( variable-list )
13139 attach ( variable-list )
13140 detach ( variable-list ) */
13141
13142 static tree
13143 c_parser_oacc_data_clause (c_parser *parser, pragma_omp_clause c_kind,
13144 tree list)
13145 {
13146 enum gomp_map_kind kind;
13147 switch (c_kind)
13148 {
13149 case PRAGMA_OACC_CLAUSE_ATTACH:
13150 kind = GOMP_MAP_ATTACH;
13151 break;
13152 case PRAGMA_OACC_CLAUSE_COPY:
13153 kind = GOMP_MAP_TOFROM;
13154 break;
13155 case PRAGMA_OACC_CLAUSE_COPYIN:
13156 kind = GOMP_MAP_TO;
13157 break;
13158 case PRAGMA_OACC_CLAUSE_COPYOUT:
13159 kind = GOMP_MAP_FROM;
13160 break;
13161 case PRAGMA_OACC_CLAUSE_CREATE:
13162 kind = GOMP_MAP_ALLOC;
13163 break;
13164 case PRAGMA_OACC_CLAUSE_DELETE:
13165 kind = GOMP_MAP_RELEASE;
13166 break;
13167 case PRAGMA_OACC_CLAUSE_DETACH:
13168 kind = GOMP_MAP_DETACH;
13169 break;
13170 case PRAGMA_OACC_CLAUSE_DEVICE:
13171 kind = GOMP_MAP_FORCE_TO;
13172 break;
13173 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
13174 kind = GOMP_MAP_DEVICE_RESIDENT;
13175 break;
13176 case PRAGMA_OACC_CLAUSE_HOST:
13177 kind = GOMP_MAP_FORCE_FROM;
13178 break;
13179 case PRAGMA_OACC_CLAUSE_LINK:
13180 kind = GOMP_MAP_LINK;
13181 break;
13182 case PRAGMA_OACC_CLAUSE_NO_CREATE:
13183 kind = GOMP_MAP_IF_PRESENT;
13184 break;
13185 case PRAGMA_OACC_CLAUSE_PRESENT:
13186 kind = GOMP_MAP_FORCE_PRESENT;
13187 break;
13188 default:
13189 gcc_unreachable ();
13190 }
13191 tree nl, c;
13192 nl = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_MAP, list, true);
13193
13194 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
13195 OMP_CLAUSE_SET_MAP_KIND (c, kind);
13196
13197 return nl;
13198 }
13199
13200 /* OpenACC 2.0:
13201 deviceptr ( variable-list ) */
13202
13203 static tree
13204 c_parser_oacc_data_clause_deviceptr (c_parser *parser, tree list)
13205 {
13206 location_t loc = c_parser_peek_token (parser)->location;
13207 tree vars, t;
13208
13209 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
13210 c_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
13211 variable-list must only allow for pointer variables. */
13212 vars = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
13213 for (t = vars; t && t; t = TREE_CHAIN (t))
13214 {
13215 tree v = TREE_PURPOSE (t);
13216
13217 /* FIXME diagnostics: Ideally we should keep individual
13218 locations for all the variables in the var list to make the
13219 following errors more precise. Perhaps
13220 c_parser_omp_var_list_parens() should construct a list of
13221 locations to go along with the var list. */
13222
13223 if (!VAR_P (v) && TREE_CODE (v) != PARM_DECL)
13224 error_at (loc, "%qD is not a variable", v);
13225 else if (TREE_TYPE (v) == error_mark_node)
13226 ;
13227 else if (!POINTER_TYPE_P (TREE_TYPE (v)))
13228 error_at (loc, "%qD is not a pointer variable", v);
13229
13230 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
13231 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
13232 OMP_CLAUSE_DECL (u) = v;
13233 OMP_CLAUSE_CHAIN (u) = list;
13234 list = u;
13235 }
13236
13237 return list;
13238 }
13239
13240 /* OpenACC 2.0, OpenMP 3.0:
13241 collapse ( constant-expression ) */
13242
13243 static tree
13244 c_parser_omp_clause_collapse (c_parser *parser, tree list)
13245 {
13246 tree c, num = error_mark_node;
13247 HOST_WIDE_INT n;
13248 location_t loc;
13249
13250 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse");
13251 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile");
13252
13253 loc = c_parser_peek_token (parser)->location;
13254 matching_parens parens;
13255 if (parens.require_open (parser))
13256 {
13257 num = c_parser_expr_no_commas (parser, NULL).value;
13258 parens.skip_until_found_close (parser);
13259 }
13260 if (num == error_mark_node)
13261 return list;
13262 mark_exp_read (num);
13263 num = c_fully_fold (num, false, NULL);
13264 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
13265 || !tree_fits_shwi_p (num)
13266 || (n = tree_to_shwi (num)) <= 0
13267 || (int) n != n)
13268 {
13269 error_at (loc,
13270 "collapse argument needs positive constant integer expression");
13271 return list;
13272 }
13273 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
13274 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
13275 OMP_CLAUSE_CHAIN (c) = list;
13276 return c;
13277 }
13278
13279 /* OpenMP 2.5:
13280 copyin ( variable-list ) */
13281
13282 static tree
13283 c_parser_omp_clause_copyin (c_parser *parser, tree list)
13284 {
13285 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYIN, list);
13286 }
13287
13288 /* OpenMP 2.5:
13289 copyprivate ( variable-list ) */
13290
13291 static tree
13292 c_parser_omp_clause_copyprivate (c_parser *parser, tree list)
13293 {
13294 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYPRIVATE, list);
13295 }
13296
13297 /* OpenMP 2.5:
13298 default ( none | shared )
13299
13300 OpenACC:
13301 default ( none | present ) */
13302
13303 static tree
13304 c_parser_omp_clause_default (c_parser *parser, tree list, bool is_oacc)
13305 {
13306 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
13307 location_t loc = c_parser_peek_token (parser)->location;
13308 tree c;
13309
13310 matching_parens parens;
13311 if (!parens.require_open (parser))
13312 return list;
13313 if (c_parser_next_token_is (parser, CPP_NAME))
13314 {
13315 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13316
13317 switch (p[0])
13318 {
13319 case 'n':
13320 if (strcmp ("none", p) != 0)
13321 goto invalid_kind;
13322 kind = OMP_CLAUSE_DEFAULT_NONE;
13323 break;
13324
13325 case 'p':
13326 if (strcmp ("present", p) != 0 || !is_oacc)
13327 goto invalid_kind;
13328 kind = OMP_CLAUSE_DEFAULT_PRESENT;
13329 break;
13330
13331 case 's':
13332 if (strcmp ("shared", p) != 0 || is_oacc)
13333 goto invalid_kind;
13334 kind = OMP_CLAUSE_DEFAULT_SHARED;
13335 break;
13336
13337 default:
13338 goto invalid_kind;
13339 }
13340
13341 c_parser_consume_token (parser);
13342 }
13343 else
13344 {
13345 invalid_kind:
13346 if (is_oacc)
13347 c_parser_error (parser, "expected %<none%> or %<present%>");
13348 else
13349 c_parser_error (parser, "expected %<none%> or %<shared%>");
13350 }
13351 parens.skip_until_found_close (parser);
13352
13353 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
13354 return list;
13355
13356 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
13357 c = build_omp_clause (loc, OMP_CLAUSE_DEFAULT);
13358 OMP_CLAUSE_CHAIN (c) = list;
13359 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
13360
13361 return c;
13362 }
13363
13364 /* OpenMP 2.5:
13365 firstprivate ( variable-list ) */
13366
13367 static tree
13368 c_parser_omp_clause_firstprivate (c_parser *parser, tree list)
13369 {
13370 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_FIRSTPRIVATE, list);
13371 }
13372
13373 /* OpenMP 3.1:
13374 final ( expression ) */
13375
13376 static tree
13377 c_parser_omp_clause_final (c_parser *parser, tree list)
13378 {
13379 location_t loc = c_parser_peek_token (parser)->location;
13380 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
13381 {
13382 matching_parens parens;
13383 tree t, c;
13384 if (!parens.require_open (parser))
13385 t = error_mark_node;
13386 else
13387 {
13388 location_t eloc = c_parser_peek_token (parser)->location;
13389 c_expr expr = c_parser_expr_no_commas (parser, NULL);
13390 t = convert_lvalue_to_rvalue (eloc, expr, true, true).value;
13391 t = c_objc_common_truthvalue_conversion (eloc, t);
13392 t = c_fully_fold (t, false, NULL);
13393 parens.skip_until_found_close (parser);
13394 }
13395
13396 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final");
13397
13398 c = build_omp_clause (loc, OMP_CLAUSE_FINAL);
13399 OMP_CLAUSE_FINAL_EXPR (c) = t;
13400 OMP_CLAUSE_CHAIN (c) = list;
13401 list = c;
13402 }
13403 else
13404 c_parser_error (parser, "expected %<(%>");
13405
13406 return list;
13407 }
13408
13409 /* OpenACC, OpenMP 2.5:
13410 if ( expression )
13411
13412 OpenMP 4.5:
13413 if ( directive-name-modifier : expression )
13414
13415 directive-name-modifier:
13416 parallel | task | taskloop | target data | target | target update
13417 | target enter data | target exit data
13418
13419 OpenMP 5.0:
13420 directive-name-modifier:
13421 ... | simd | cancel */
13422
13423 static tree
13424 c_parser_omp_clause_if (c_parser *parser, tree list, bool is_omp)
13425 {
13426 location_t location = c_parser_peek_token (parser)->location;
13427 enum tree_code if_modifier = ERROR_MARK;
13428
13429 matching_parens parens;
13430 if (!parens.require_open (parser))
13431 return list;
13432
13433 if (is_omp && c_parser_next_token_is (parser, CPP_NAME))
13434 {
13435 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13436 int n = 2;
13437 if (strcmp (p, "cancel") == 0)
13438 if_modifier = VOID_CST;
13439 else if (strcmp (p, "parallel") == 0)
13440 if_modifier = OMP_PARALLEL;
13441 else if (strcmp (p, "simd") == 0)
13442 if_modifier = OMP_SIMD;
13443 else if (strcmp (p, "task") == 0)
13444 if_modifier = OMP_TASK;
13445 else if (strcmp (p, "taskloop") == 0)
13446 if_modifier = OMP_TASKLOOP;
13447 else if (strcmp (p, "target") == 0)
13448 {
13449 if_modifier = OMP_TARGET;
13450 if (c_parser_peek_2nd_token (parser)->type == CPP_NAME)
13451 {
13452 p = IDENTIFIER_POINTER (c_parser_peek_2nd_token (parser)->value);
13453 if (strcmp ("data", p) == 0)
13454 if_modifier = OMP_TARGET_DATA;
13455 else if (strcmp ("update", p) == 0)
13456 if_modifier = OMP_TARGET_UPDATE;
13457 else if (strcmp ("enter", p) == 0)
13458 if_modifier = OMP_TARGET_ENTER_DATA;
13459 else if (strcmp ("exit", p) == 0)
13460 if_modifier = OMP_TARGET_EXIT_DATA;
13461 if (if_modifier != OMP_TARGET)
13462 {
13463 n = 3;
13464 c_parser_consume_token (parser);
13465 }
13466 else
13467 {
13468 location_t loc = c_parser_peek_2nd_token (parser)->location;
13469 error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
13470 "or %<exit%>");
13471 if_modifier = ERROR_MARK;
13472 }
13473 if (if_modifier == OMP_TARGET_ENTER_DATA
13474 || if_modifier == OMP_TARGET_EXIT_DATA)
13475 {
13476 if (c_parser_peek_2nd_token (parser)->type == CPP_NAME)
13477 {
13478 p = IDENTIFIER_POINTER
13479 (c_parser_peek_2nd_token (parser)->value);
13480 if (strcmp ("data", p) == 0)
13481 n = 4;
13482 }
13483 if (n == 4)
13484 c_parser_consume_token (parser);
13485 else
13486 {
13487 location_t loc
13488 = c_parser_peek_2nd_token (parser)->location;
13489 error_at (loc, "expected %<data%>");
13490 if_modifier = ERROR_MARK;
13491 }
13492 }
13493 }
13494 }
13495 if (if_modifier != ERROR_MARK)
13496 {
13497 if (c_parser_peek_2nd_token (parser)->type == CPP_COLON)
13498 {
13499 c_parser_consume_token (parser);
13500 c_parser_consume_token (parser);
13501 }
13502 else
13503 {
13504 if (n > 2)
13505 {
13506 location_t loc = c_parser_peek_2nd_token (parser)->location;
13507 error_at (loc, "expected %<:%>");
13508 }
13509 if_modifier = ERROR_MARK;
13510 }
13511 }
13512 }
13513
13514 location_t loc = c_parser_peek_token (parser)->location;
13515 c_expr expr = c_parser_expr_no_commas (parser, NULL);
13516 expr = convert_lvalue_to_rvalue (loc, expr, true, true);
13517 tree t = c_objc_common_truthvalue_conversion (loc, expr.value), c;
13518 t = c_fully_fold (t, false, NULL);
13519 parens.skip_until_found_close (parser);
13520
13521 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
13522 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
13523 {
13524 if (if_modifier != ERROR_MARK
13525 && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
13526 {
13527 const char *p = NULL;
13528 switch (if_modifier)
13529 {
13530 case VOID_CST: p = "cancel"; break;
13531 case OMP_PARALLEL: p = "parallel"; break;
13532 case OMP_SIMD: p = "simd"; break;
13533 case OMP_TASK: p = "task"; break;
13534 case OMP_TASKLOOP: p = "taskloop"; break;
13535 case OMP_TARGET_DATA: p = "target data"; break;
13536 case OMP_TARGET: p = "target"; break;
13537 case OMP_TARGET_UPDATE: p = "target update"; break;
13538 case OMP_TARGET_ENTER_DATA: p = "target enter data"; break;
13539 case OMP_TARGET_EXIT_DATA: p = "target exit data"; break;
13540 default: gcc_unreachable ();
13541 }
13542 error_at (location, "too many %<if%> clauses with %qs modifier",
13543 p);
13544 return list;
13545 }
13546 else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
13547 {
13548 if (!is_omp)
13549 error_at (location, "too many %<if%> clauses");
13550 else
13551 error_at (location, "too many %<if%> clauses without modifier");
13552 return list;
13553 }
13554 else if (if_modifier == ERROR_MARK
13555 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
13556 {
13557 error_at (location, "if any %<if%> clause has modifier, then all "
13558 "%<if%> clauses have to use modifier");
13559 return list;
13560 }
13561 }
13562
13563 c = build_omp_clause (location, OMP_CLAUSE_IF);
13564 OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
13565 OMP_CLAUSE_IF_EXPR (c) = t;
13566 OMP_CLAUSE_CHAIN (c) = list;
13567 return c;
13568 }
13569
13570 /* OpenMP 2.5:
13571 lastprivate ( variable-list )
13572
13573 OpenMP 5.0:
13574 lastprivate ( [ lastprivate-modifier : ] variable-list ) */
13575
13576 static tree
13577 c_parser_omp_clause_lastprivate (c_parser *parser, tree list)
13578 {
13579 /* The clauses location. */
13580 location_t loc = c_parser_peek_token (parser)->location;
13581
13582 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
13583 {
13584 bool conditional = false;
13585 if (c_parser_next_token_is (parser, CPP_NAME)
13586 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
13587 {
13588 const char *p
13589 = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13590 if (strcmp (p, "conditional") == 0)
13591 {
13592 conditional = true;
13593 c_parser_consume_token (parser);
13594 c_parser_consume_token (parser);
13595 }
13596 }
13597 tree nlist = c_parser_omp_variable_list (parser, loc,
13598 OMP_CLAUSE_LASTPRIVATE, list);
13599 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
13600 if (conditional)
13601 for (tree c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
13602 OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c) = 1;
13603 return nlist;
13604 }
13605 return list;
13606 }
13607
13608 /* OpenMP 3.1:
13609 mergeable */
13610
13611 static tree
13612 c_parser_omp_clause_mergeable (c_parser *parser ATTRIBUTE_UNUSED, tree list)
13613 {
13614 tree c;
13615
13616 /* FIXME: Should we allow duplicates? */
13617 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable");
13618
13619 c = build_omp_clause (c_parser_peek_token (parser)->location,
13620 OMP_CLAUSE_MERGEABLE);
13621 OMP_CLAUSE_CHAIN (c) = list;
13622
13623 return c;
13624 }
13625
13626 /* OpenMP 2.5:
13627 nowait */
13628
13629 static tree
13630 c_parser_omp_clause_nowait (c_parser *parser ATTRIBUTE_UNUSED, tree list)
13631 {
13632 tree c;
13633 location_t loc = c_parser_peek_token (parser)->location;
13634
13635 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
13636
13637 c = build_omp_clause (loc, OMP_CLAUSE_NOWAIT);
13638 OMP_CLAUSE_CHAIN (c) = list;
13639 return c;
13640 }
13641
13642 /* OpenMP 2.5:
13643 num_threads ( expression ) */
13644
13645 static tree
13646 c_parser_omp_clause_num_threads (c_parser *parser, tree list)
13647 {
13648 location_t num_threads_loc = c_parser_peek_token (parser)->location;
13649 matching_parens parens;
13650 if (parens.require_open (parser))
13651 {
13652 location_t expr_loc = c_parser_peek_token (parser)->location;
13653 c_expr expr = c_parser_expr_no_commas (parser, NULL);
13654 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
13655 tree c, t = expr.value;
13656 t = c_fully_fold (t, false, NULL);
13657
13658 parens.skip_until_found_close (parser);
13659
13660 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
13661 {
13662 c_parser_error (parser, "expected integer expression");
13663 return list;
13664 }
13665
13666 /* Attempt to statically determine when the number isn't positive. */
13667 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
13668 build_int_cst (TREE_TYPE (t), 0));
13669 protected_set_expr_location (c, expr_loc);
13670 if (c == boolean_true_node)
13671 {
13672 warning_at (expr_loc, 0,
13673 "%<num_threads%> value must be positive");
13674 t = integer_one_node;
13675 }
13676
13677 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
13678
13679 c = build_omp_clause (num_threads_loc, OMP_CLAUSE_NUM_THREADS);
13680 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
13681 OMP_CLAUSE_CHAIN (c) = list;
13682 list = c;
13683 }
13684
13685 return list;
13686 }
13687
13688 /* OpenMP 4.5:
13689 num_tasks ( expression ) */
13690
13691 static tree
13692 c_parser_omp_clause_num_tasks (c_parser *parser, tree list)
13693 {
13694 location_t num_tasks_loc = c_parser_peek_token (parser)->location;
13695 matching_parens parens;
13696 if (parens.require_open (parser))
13697 {
13698 location_t expr_loc = c_parser_peek_token (parser)->location;
13699 c_expr expr = c_parser_expr_no_commas (parser, NULL);
13700 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
13701 tree c, t = expr.value;
13702 t = c_fully_fold (t, false, NULL);
13703
13704 parens.skip_until_found_close (parser);
13705
13706 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
13707 {
13708 c_parser_error (parser, "expected integer expression");
13709 return list;
13710 }
13711
13712 /* Attempt to statically determine when the number isn't positive. */
13713 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
13714 build_int_cst (TREE_TYPE (t), 0));
13715 if (CAN_HAVE_LOCATION_P (c))
13716 SET_EXPR_LOCATION (c, expr_loc);
13717 if (c == boolean_true_node)
13718 {
13719 warning_at (expr_loc, 0, "%<num_tasks%> value must be positive");
13720 t = integer_one_node;
13721 }
13722
13723 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS, "num_tasks");
13724
13725 c = build_omp_clause (num_tasks_loc, OMP_CLAUSE_NUM_TASKS);
13726 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
13727 OMP_CLAUSE_CHAIN (c) = list;
13728 list = c;
13729 }
13730
13731 return list;
13732 }
13733
13734 /* OpenMP 4.5:
13735 grainsize ( expression ) */
13736
13737 static tree
13738 c_parser_omp_clause_grainsize (c_parser *parser, tree list)
13739 {
13740 location_t grainsize_loc = c_parser_peek_token (parser)->location;
13741 matching_parens parens;
13742 if (parens.require_open (parser))
13743 {
13744 location_t expr_loc = c_parser_peek_token (parser)->location;
13745 c_expr expr = c_parser_expr_no_commas (parser, NULL);
13746 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
13747 tree c, t = expr.value;
13748 t = c_fully_fold (t, false, NULL);
13749
13750 parens.skip_until_found_close (parser);
13751
13752 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
13753 {
13754 c_parser_error (parser, "expected integer expression");
13755 return list;
13756 }
13757
13758 /* Attempt to statically determine when the number isn't positive. */
13759 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
13760 build_int_cst (TREE_TYPE (t), 0));
13761 if (CAN_HAVE_LOCATION_P (c))
13762 SET_EXPR_LOCATION (c, expr_loc);
13763 if (c == boolean_true_node)
13764 {
13765 warning_at (expr_loc, 0, "%<grainsize%> value must be positive");
13766 t = integer_one_node;
13767 }
13768
13769 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE, "grainsize");
13770
13771 c = build_omp_clause (grainsize_loc, OMP_CLAUSE_GRAINSIZE);
13772 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
13773 OMP_CLAUSE_CHAIN (c) = list;
13774 list = c;
13775 }
13776
13777 return list;
13778 }
13779
13780 /* OpenMP 4.5:
13781 priority ( expression ) */
13782
13783 static tree
13784 c_parser_omp_clause_priority (c_parser *parser, tree list)
13785 {
13786 location_t priority_loc = c_parser_peek_token (parser)->location;
13787 matching_parens parens;
13788 if (parens.require_open (parser))
13789 {
13790 location_t expr_loc = c_parser_peek_token (parser)->location;
13791 c_expr expr = c_parser_expr_no_commas (parser, NULL);
13792 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
13793 tree c, t = expr.value;
13794 t = c_fully_fold (t, false, NULL);
13795
13796 parens.skip_until_found_close (parser);
13797
13798 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
13799 {
13800 c_parser_error (parser, "expected integer expression");
13801 return list;
13802 }
13803
13804 /* Attempt to statically determine when the number isn't
13805 non-negative. */
13806 c = fold_build2_loc (expr_loc, LT_EXPR, boolean_type_node, t,
13807 build_int_cst (TREE_TYPE (t), 0));
13808 if (CAN_HAVE_LOCATION_P (c))
13809 SET_EXPR_LOCATION (c, expr_loc);
13810 if (c == boolean_true_node)
13811 {
13812 warning_at (expr_loc, 0, "%<priority%> value must be non-negative");
13813 t = integer_one_node;
13814 }
13815
13816 check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY, "priority");
13817
13818 c = build_omp_clause (priority_loc, OMP_CLAUSE_PRIORITY);
13819 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
13820 OMP_CLAUSE_CHAIN (c) = list;
13821 list = c;
13822 }
13823
13824 return list;
13825 }
13826
13827 /* OpenMP 4.5:
13828 hint ( expression ) */
13829
13830 static tree
13831 c_parser_omp_clause_hint (c_parser *parser, tree list)
13832 {
13833 location_t hint_loc = c_parser_peek_token (parser)->location;
13834 matching_parens parens;
13835 if (parens.require_open (parser))
13836 {
13837 location_t expr_loc = c_parser_peek_token (parser)->location;
13838 c_expr expr = c_parser_expr_no_commas (parser, NULL);
13839 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
13840 tree c, t = expr.value;
13841 t = c_fully_fold (t, false, NULL);
13842
13843 parens.skip_until_found_close (parser);
13844
13845 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
13846 || TREE_CODE (t) != INTEGER_CST)
13847 {
13848 c_parser_error (parser, "expected constant integer expression");
13849 return list;
13850 }
13851
13852 check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint");
13853
13854 c = build_omp_clause (hint_loc, OMP_CLAUSE_HINT);
13855 OMP_CLAUSE_HINT_EXPR (c) = t;
13856 OMP_CLAUSE_CHAIN (c) = list;
13857 list = c;
13858 }
13859
13860 return list;
13861 }
13862
13863 /* OpenMP 4.5:
13864 defaultmap ( tofrom : scalar )
13865
13866 OpenMP 5.0:
13867 defaultmap ( implicit-behavior [ : variable-category ] ) */
13868
13869 static tree
13870 c_parser_omp_clause_defaultmap (c_parser *parser, tree list)
13871 {
13872 location_t loc = c_parser_peek_token (parser)->location;
13873 tree c;
13874 const char *p;
13875 enum omp_clause_defaultmap_kind behavior = OMP_CLAUSE_DEFAULTMAP_DEFAULT;
13876 enum omp_clause_defaultmap_kind category
13877 = OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED;
13878
13879 matching_parens parens;
13880 if (!parens.require_open (parser))
13881 return list;
13882 if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
13883 p = "default";
13884 else if (!c_parser_next_token_is (parser, CPP_NAME))
13885 {
13886 invalid_behavior:
13887 c_parser_error (parser, "expected %<alloc%>, %<to%>, %<from%>, "
13888 "%<tofrom%>, %<firstprivate%>, %<none%> "
13889 "or %<default%>");
13890 goto out_err;
13891 }
13892 else
13893 p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13894
13895 switch (p[0])
13896 {
13897 case 'a':
13898 if (strcmp ("alloc", p) == 0)
13899 behavior = OMP_CLAUSE_DEFAULTMAP_ALLOC;
13900 else
13901 goto invalid_behavior;
13902 break;
13903
13904 case 'd':
13905 if (strcmp ("default", p) == 0)
13906 behavior = OMP_CLAUSE_DEFAULTMAP_DEFAULT;
13907 else
13908 goto invalid_behavior;
13909 break;
13910
13911 case 'f':
13912 if (strcmp ("firstprivate", p) == 0)
13913 behavior = OMP_CLAUSE_DEFAULTMAP_FIRSTPRIVATE;
13914 else if (strcmp ("from", p) == 0)
13915 behavior = OMP_CLAUSE_DEFAULTMAP_FROM;
13916 else
13917 goto invalid_behavior;
13918 break;
13919
13920 case 'n':
13921 if (strcmp ("none", p) == 0)
13922 behavior = OMP_CLAUSE_DEFAULTMAP_NONE;
13923 else
13924 goto invalid_behavior;
13925 break;
13926
13927 case 't':
13928 if (strcmp ("tofrom", p) == 0)
13929 behavior = OMP_CLAUSE_DEFAULTMAP_TOFROM;
13930 else if (strcmp ("to", p) == 0)
13931 behavior = OMP_CLAUSE_DEFAULTMAP_TO;
13932 else
13933 goto invalid_behavior;
13934 break;
13935
13936 default:
13937 goto invalid_behavior;
13938 }
13939 c_parser_consume_token (parser);
13940
13941 if (!c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
13942 {
13943 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
13944 goto out_err;
13945 if (!c_parser_next_token_is (parser, CPP_NAME))
13946 {
13947 invalid_category:
13948 c_parser_error (parser, "expected %<scalar%>, %<aggregate%> or "
13949 "%<pointer%>");
13950 goto out_err;
13951 }
13952 p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13953 switch (p[0])
13954 {
13955 case 'a':
13956 if (strcmp ("aggregate", p) == 0)
13957 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE;
13958 else
13959 goto invalid_category;
13960 break;
13961
13962 case 'p':
13963 if (strcmp ("pointer", p) == 0)
13964 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER;
13965 else
13966 goto invalid_category;
13967 break;
13968
13969 case 's':
13970 if (strcmp ("scalar", p) == 0)
13971 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR;
13972 else
13973 goto invalid_category;
13974 break;
13975
13976 default:
13977 goto invalid_category;
13978 }
13979
13980 c_parser_consume_token (parser);
13981 }
13982 parens.skip_until_found_close (parser);
13983
13984 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
13985 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEFAULTMAP
13986 && (category == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
13987 || OMP_CLAUSE_DEFAULTMAP_CATEGORY (c) == category
13988 || (OMP_CLAUSE_DEFAULTMAP_CATEGORY (c)
13989 == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED)))
13990 {
13991 enum omp_clause_defaultmap_kind cat = category;
13992 location_t loc = OMP_CLAUSE_LOCATION (c);
13993 if (cat == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED)
13994 cat = OMP_CLAUSE_DEFAULTMAP_CATEGORY (c);
13995 p = NULL;
13996 switch (cat)
13997 {
13998 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED:
13999 p = NULL;
14000 break;
14001 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE:
14002 p = "aggregate";
14003 break;
14004 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER:
14005 p = "pointer";
14006 break;
14007 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR:
14008 p = "scalar";
14009 break;
14010 default:
14011 gcc_unreachable ();
14012 }
14013 if (p)
14014 error_at (loc, "too many %<defaultmap%> clauses with %qs category",
14015 p);
14016 else
14017 error_at (loc, "too many %<defaultmap%> clauses with unspecified "
14018 "category");
14019 break;
14020 }
14021
14022 c = build_omp_clause (loc, OMP_CLAUSE_DEFAULTMAP);
14023 OMP_CLAUSE_DEFAULTMAP_SET_KIND (c, behavior, category);
14024 OMP_CLAUSE_CHAIN (c) = list;
14025 return c;
14026
14027 out_err:
14028 parens.skip_until_found_close (parser);
14029 return list;
14030 }
14031
14032 /* OpenACC 2.0:
14033 use_device ( variable-list )
14034
14035 OpenMP 4.5:
14036 use_device_ptr ( variable-list ) */
14037
14038 static tree
14039 c_parser_omp_clause_use_device_ptr (c_parser *parser, tree list)
14040 {
14041 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_USE_DEVICE_PTR,
14042 list);
14043 }
14044
14045 /* OpenMP 5.0:
14046 use_device_addr ( variable-list ) */
14047
14048 static tree
14049 c_parser_omp_clause_use_device_addr (c_parser *parser, tree list)
14050 {
14051 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_USE_DEVICE_ADDR,
14052 list);
14053 }
14054
14055 /* OpenMP 4.5:
14056 is_device_ptr ( variable-list ) */
14057
14058 static tree
14059 c_parser_omp_clause_is_device_ptr (c_parser *parser, tree list)
14060 {
14061 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_IS_DEVICE_PTR, list);
14062 }
14063
14064 /* OpenACC:
14065 num_gangs ( expression )
14066 num_workers ( expression )
14067 vector_length ( expression ) */
14068
14069 static tree
14070 c_parser_oacc_single_int_clause (c_parser *parser, omp_clause_code code,
14071 tree list)
14072 {
14073 location_t loc = c_parser_peek_token (parser)->location;
14074
14075 matching_parens parens;
14076 if (!parens.require_open (parser))
14077 return list;
14078
14079 location_t expr_loc = c_parser_peek_token (parser)->location;
14080 c_expr expr = c_parser_expression (parser);
14081 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
14082 tree c, t = expr.value;
14083 t = c_fully_fold (t, false, NULL);
14084
14085 parens.skip_until_found_close (parser);
14086
14087 if (t == error_mark_node)
14088 return list;
14089 else if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
14090 {
14091 error_at (expr_loc, "%qs expression must be integral",
14092 omp_clause_code_name[code]);
14093 return list;
14094 }
14095
14096 /* Attempt to statically determine when the number isn't positive. */
14097 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
14098 build_int_cst (TREE_TYPE (t), 0));
14099 protected_set_expr_location (c, expr_loc);
14100 if (c == boolean_true_node)
14101 {
14102 warning_at (expr_loc, 0,
14103 "%qs value must be positive",
14104 omp_clause_code_name[code]);
14105 t = integer_one_node;
14106 }
14107
14108 check_no_duplicate_clause (list, code, omp_clause_code_name[code]);
14109
14110 c = build_omp_clause (loc, code);
14111 OMP_CLAUSE_OPERAND (c, 0) = t;
14112 OMP_CLAUSE_CHAIN (c) = list;
14113 return c;
14114 }
14115
14116 /* OpenACC:
14117
14118 gang [( gang-arg-list )]
14119 worker [( [num:] int-expr )]
14120 vector [( [length:] int-expr )]
14121
14122 where gang-arg is one of:
14123
14124 [num:] int-expr
14125 static: size-expr
14126
14127 and size-expr may be:
14128
14129 *
14130 int-expr
14131 */
14132
14133 static tree
14134 c_parser_oacc_shape_clause (c_parser *parser, location_t loc,
14135 omp_clause_code kind,
14136 const char *str, tree list)
14137 {
14138 const char *id = "num";
14139 tree ops[2] = { NULL_TREE, NULL_TREE }, c;
14140
14141 if (kind == OMP_CLAUSE_VECTOR)
14142 id = "length";
14143
14144 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
14145 {
14146 c_parser_consume_token (parser);
14147
14148 do
14149 {
14150 c_token *next = c_parser_peek_token (parser);
14151 int idx = 0;
14152
14153 /* Gang static argument. */
14154 if (kind == OMP_CLAUSE_GANG
14155 && c_parser_next_token_is_keyword (parser, RID_STATIC))
14156 {
14157 c_parser_consume_token (parser);
14158
14159 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
14160 goto cleanup_error;
14161
14162 idx = 1;
14163 if (ops[idx] != NULL_TREE)
14164 {
14165 c_parser_error (parser, "too many %<static%> arguments");
14166 goto cleanup_error;
14167 }
14168
14169 /* Check for the '*' argument. */
14170 if (c_parser_next_token_is (parser, CPP_MULT)
14171 && (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
14172 || c_parser_peek_2nd_token (parser)->type
14173 == CPP_CLOSE_PAREN))
14174 {
14175 c_parser_consume_token (parser);
14176 ops[idx] = integer_minus_one_node;
14177
14178 if (c_parser_next_token_is (parser, CPP_COMMA))
14179 {
14180 c_parser_consume_token (parser);
14181 continue;
14182 }
14183 else
14184 break;
14185 }
14186 }
14187 /* Worker num: argument and vector length: arguments. */
14188 else if (c_parser_next_token_is (parser, CPP_NAME)
14189 && strcmp (id, IDENTIFIER_POINTER (next->value)) == 0
14190 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
14191 {
14192 c_parser_consume_token (parser); /* id */
14193 c_parser_consume_token (parser); /* ':' */
14194 }
14195
14196 /* Now collect the actual argument. */
14197 if (ops[idx] != NULL_TREE)
14198 {
14199 c_parser_error (parser, "unexpected argument");
14200 goto cleanup_error;
14201 }
14202
14203 location_t expr_loc = c_parser_peek_token (parser)->location;
14204 c_expr cexpr = c_parser_expr_no_commas (parser, NULL);
14205 cexpr = convert_lvalue_to_rvalue (expr_loc, cexpr, false, true);
14206 tree expr = cexpr.value;
14207 if (expr == error_mark_node)
14208 goto cleanup_error;
14209
14210 expr = c_fully_fold (expr, false, NULL);
14211
14212 /* Attempt to statically determine when the number isn't a
14213 positive integer. */
14214
14215 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr)))
14216 {
14217 c_parser_error (parser, "expected integer expression");
14218 return list;
14219 }
14220
14221 tree c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, expr,
14222 build_int_cst (TREE_TYPE (expr), 0));
14223 if (c == boolean_true_node)
14224 {
14225 warning_at (loc, 0,
14226 "%qs value must be positive", str);
14227 expr = integer_one_node;
14228 }
14229
14230 ops[idx] = expr;
14231
14232 if (kind == OMP_CLAUSE_GANG
14233 && c_parser_next_token_is (parser, CPP_COMMA))
14234 {
14235 c_parser_consume_token (parser);
14236 continue;
14237 }
14238 break;
14239 }
14240 while (1);
14241
14242 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
14243 goto cleanup_error;
14244 }
14245
14246 check_no_duplicate_clause (list, kind, str);
14247
14248 c = build_omp_clause (loc, kind);
14249
14250 if (ops[1])
14251 OMP_CLAUSE_OPERAND (c, 1) = ops[1];
14252
14253 OMP_CLAUSE_OPERAND (c, 0) = ops[0];
14254 OMP_CLAUSE_CHAIN (c) = list;
14255
14256 return c;
14257
14258 cleanup_error:
14259 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
14260 return list;
14261 }
14262
14263 /* OpenACC 2.5:
14264 auto
14265 finalize
14266 independent
14267 nohost
14268 seq */
14269
14270 static tree
14271 c_parser_oacc_simple_clause (location_t loc, enum omp_clause_code code,
14272 tree list)
14273 {
14274 check_no_duplicate_clause (list, code, omp_clause_code_name[code]);
14275
14276 tree c = build_omp_clause (loc, code);
14277 OMP_CLAUSE_CHAIN (c) = list;
14278
14279 return c;
14280 }
14281
14282 /* OpenACC:
14283 async [( int-expr )] */
14284
14285 static tree
14286 c_parser_oacc_clause_async (c_parser *parser, tree list)
14287 {
14288 tree c, t;
14289 location_t loc = c_parser_peek_token (parser)->location;
14290
14291 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
14292
14293 if (c_parser_peek_token (parser)->type == CPP_OPEN_PAREN)
14294 {
14295 c_parser_consume_token (parser);
14296
14297 t = c_parser_expression (parser).value;
14298 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
14299 c_parser_error (parser, "expected integer expression");
14300 else if (t == error_mark_node
14301 || !c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
14302 return list;
14303 }
14304 else
14305 t = c_fully_fold (t, false, NULL);
14306
14307 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async");
14308
14309 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
14310 OMP_CLAUSE_ASYNC_EXPR (c) = t;
14311 OMP_CLAUSE_CHAIN (c) = list;
14312 list = c;
14313
14314 return list;
14315 }
14316
14317 /* OpenACC 2.0:
14318 tile ( size-expr-list ) */
14319
14320 static tree
14321 c_parser_oacc_clause_tile (c_parser *parser, tree list)
14322 {
14323 tree c, expr = error_mark_node;
14324 location_t loc;
14325 tree tile = NULL_TREE;
14326
14327 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile");
14328 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse");
14329
14330 loc = c_parser_peek_token (parser)->location;
14331 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
14332 return list;
14333
14334 do
14335 {
14336 if (tile && !c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
14337 return list;
14338
14339 if (c_parser_next_token_is (parser, CPP_MULT)
14340 && (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
14341 || c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_PAREN))
14342 {
14343 c_parser_consume_token (parser);
14344 expr = integer_zero_node;
14345 }
14346 else
14347 {
14348 location_t expr_loc = c_parser_peek_token (parser)->location;
14349 c_expr cexpr = c_parser_expr_no_commas (parser, NULL);
14350 cexpr = convert_lvalue_to_rvalue (expr_loc, cexpr, false, true);
14351 expr = cexpr.value;
14352
14353 if (expr == error_mark_node)
14354 {
14355 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
14356 "expected %<)%>");
14357 return list;
14358 }
14359
14360 expr = c_fully_fold (expr, false, NULL);
14361
14362 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr))
14363 || !tree_fits_shwi_p (expr)
14364 || tree_to_shwi (expr) <= 0)
14365 {
14366 error_at (expr_loc, "%<tile%> argument needs positive"
14367 " integral constant");
14368 expr = integer_zero_node;
14369 }
14370 }
14371
14372 tile = tree_cons (NULL_TREE, expr, tile);
14373 }
14374 while (c_parser_next_token_is_not (parser, CPP_CLOSE_PAREN));
14375
14376 /* Consume the trailing ')'. */
14377 c_parser_consume_token (parser);
14378
14379 c = build_omp_clause (loc, OMP_CLAUSE_TILE);
14380 tile = nreverse (tile);
14381 OMP_CLAUSE_TILE_LIST (c) = tile;
14382 OMP_CLAUSE_CHAIN (c) = list;
14383 return c;
14384 }
14385
14386 /* OpenACC:
14387 wait [( int-expr-list )] */
14388
14389 static tree
14390 c_parser_oacc_clause_wait (c_parser *parser, tree list)
14391 {
14392 location_t clause_loc = c_parser_peek_token (parser)->location;
14393
14394 if (c_parser_peek_token (parser)->type == CPP_OPEN_PAREN)
14395 list = c_parser_oacc_wait_list (parser, clause_loc, list);
14396 else
14397 {
14398 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
14399
14400 OMP_CLAUSE_DECL (c) = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
14401 OMP_CLAUSE_CHAIN (c) = list;
14402 list = c;
14403 }
14404
14405 return list;
14406 }
14407
14408
14409 /* OpenMP 5.0:
14410 order ( concurrent ) */
14411
14412 static tree
14413 c_parser_omp_clause_order (c_parser *parser, tree list)
14414 {
14415 location_t loc = c_parser_peek_token (parser)->location;
14416 tree c;
14417 const char *p;
14418
14419 matching_parens parens;
14420 if (!parens.require_open (parser))
14421 return list;
14422 if (!c_parser_next_token_is (parser, CPP_NAME))
14423 {
14424 c_parser_error (parser, "expected %<concurrent%>");
14425 goto out_err;
14426 }
14427 p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
14428 if (strcmp (p, "concurrent") != 0)
14429 {
14430 c_parser_error (parser, "expected %<concurrent%>");
14431 goto out_err;
14432 }
14433 c_parser_consume_token (parser);
14434 parens.skip_until_found_close (parser);
14435 /* check_no_duplicate_clause (list, OMP_CLAUSE_ORDER, "order"); */
14436 c = build_omp_clause (loc, OMP_CLAUSE_ORDER);
14437 OMP_CLAUSE_CHAIN (c) = list;
14438 return c;
14439
14440 out_err:
14441 parens.skip_until_found_close (parser);
14442 return list;
14443 }
14444
14445
14446 /* OpenMP 5.0:
14447 bind ( teams | parallel | thread ) */
14448
14449 static tree
14450 c_parser_omp_clause_bind (c_parser *parser, tree list)
14451 {
14452 location_t loc = c_parser_peek_token (parser)->location;
14453 tree c;
14454 const char *p;
14455 enum omp_clause_bind_kind kind = OMP_CLAUSE_BIND_THREAD;
14456
14457 matching_parens parens;
14458 if (!parens.require_open (parser))
14459 return list;
14460 if (!c_parser_next_token_is (parser, CPP_NAME))
14461 {
14462 invalid:
14463 c_parser_error (parser,
14464 "expected %<teams%>, %<parallel%> or %<thread%>");
14465 parens.skip_until_found_close (parser);
14466 return list;
14467 }
14468 p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
14469 if (strcmp (p, "teams") == 0)
14470 kind = OMP_CLAUSE_BIND_TEAMS;
14471 else if (strcmp (p, "parallel") == 0)
14472 kind = OMP_CLAUSE_BIND_PARALLEL;
14473 else if (strcmp (p, "thread") != 0)
14474 goto invalid;
14475 c_parser_consume_token (parser);
14476 parens.skip_until_found_close (parser);
14477 /* check_no_duplicate_clause (list, OMP_CLAUSE_BIND, "bind"); */
14478 c = build_omp_clause (loc, OMP_CLAUSE_BIND);
14479 OMP_CLAUSE_BIND_KIND (c) = kind;
14480 OMP_CLAUSE_CHAIN (c) = list;
14481 return c;
14482 }
14483
14484
14485 /* OpenMP 2.5:
14486 ordered
14487
14488 OpenMP 4.5:
14489 ordered ( constant-expression ) */
14490
14491 static tree
14492 c_parser_omp_clause_ordered (c_parser *parser, tree list)
14493 {
14494 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
14495
14496 tree c, num = NULL_TREE;
14497 HOST_WIDE_INT n;
14498 location_t loc = c_parser_peek_token (parser)->location;
14499 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
14500 {
14501 matching_parens parens;
14502 parens.consume_open (parser);
14503 num = c_parser_expr_no_commas (parser, NULL).value;
14504 parens.skip_until_found_close (parser);
14505 }
14506 if (num == error_mark_node)
14507 return list;
14508 if (num)
14509 {
14510 mark_exp_read (num);
14511 num = c_fully_fold (num, false, NULL);
14512 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
14513 || !tree_fits_shwi_p (num)
14514 || (n = tree_to_shwi (num)) <= 0
14515 || (int) n != n)
14516 {
14517 error_at (loc, "ordered argument needs positive "
14518 "constant integer expression");
14519 return list;
14520 }
14521 }
14522 c = build_omp_clause (loc, OMP_CLAUSE_ORDERED);
14523 OMP_CLAUSE_ORDERED_EXPR (c) = num;
14524 OMP_CLAUSE_CHAIN (c) = list;
14525 return c;
14526 }
14527
14528 /* OpenMP 2.5:
14529 private ( variable-list ) */
14530
14531 static tree
14532 c_parser_omp_clause_private (c_parser *parser, tree list)
14533 {
14534 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_PRIVATE, list);
14535 }
14536
14537 /* OpenMP 2.5:
14538 reduction ( reduction-operator : variable-list )
14539
14540 reduction-operator:
14541 One of: + * - & ^ | && ||
14542
14543 OpenMP 3.1:
14544
14545 reduction-operator:
14546 One of: + * - & ^ | && || max min
14547
14548 OpenMP 4.0:
14549
14550 reduction-operator:
14551 One of: + * - & ^ | && ||
14552 identifier
14553
14554 OpenMP 5.0:
14555 reduction ( reduction-modifier, reduction-operator : variable-list )
14556 in_reduction ( reduction-operator : variable-list )
14557 task_reduction ( reduction-operator : variable-list ) */
14558
14559 static tree
14560 c_parser_omp_clause_reduction (c_parser *parser, enum omp_clause_code kind,
14561 bool is_omp, tree list)
14562 {
14563 location_t clause_loc = c_parser_peek_token (parser)->location;
14564 matching_parens parens;
14565 if (parens.require_open (parser))
14566 {
14567 bool task = false;
14568 bool inscan = false;
14569 enum tree_code code = ERROR_MARK;
14570 tree reduc_id = NULL_TREE;
14571
14572 if (kind == OMP_CLAUSE_REDUCTION && is_omp)
14573 {
14574 if (c_parser_next_token_is_keyword (parser, RID_DEFAULT)
14575 && c_parser_peek_2nd_token (parser)->type == CPP_COMMA)
14576 {
14577 c_parser_consume_token (parser);
14578 c_parser_consume_token (parser);
14579 }
14580 else if (c_parser_next_token_is (parser, CPP_NAME)
14581 && c_parser_peek_2nd_token (parser)->type == CPP_COMMA)
14582 {
14583 const char *p
14584 = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
14585 if (strcmp (p, "task") == 0)
14586 task = true;
14587 else if (strcmp (p, "inscan") == 0)
14588 inscan = true;
14589 if (task || inscan)
14590 {
14591 c_parser_consume_token (parser);
14592 c_parser_consume_token (parser);
14593 }
14594 }
14595 }
14596
14597 switch (c_parser_peek_token (parser)->type)
14598 {
14599 case CPP_PLUS:
14600 code = PLUS_EXPR;
14601 break;
14602 case CPP_MULT:
14603 code = MULT_EXPR;
14604 break;
14605 case CPP_MINUS:
14606 code = MINUS_EXPR;
14607 break;
14608 case CPP_AND:
14609 code = BIT_AND_EXPR;
14610 break;
14611 case CPP_XOR:
14612 code = BIT_XOR_EXPR;
14613 break;
14614 case CPP_OR:
14615 code = BIT_IOR_EXPR;
14616 break;
14617 case CPP_AND_AND:
14618 code = TRUTH_ANDIF_EXPR;
14619 break;
14620 case CPP_OR_OR:
14621 code = TRUTH_ORIF_EXPR;
14622 break;
14623 case CPP_NAME:
14624 {
14625 const char *p
14626 = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
14627 if (strcmp (p, "min") == 0)
14628 {
14629 code = MIN_EXPR;
14630 break;
14631 }
14632 if (strcmp (p, "max") == 0)
14633 {
14634 code = MAX_EXPR;
14635 break;
14636 }
14637 reduc_id = c_parser_peek_token (parser)->value;
14638 break;
14639 }
14640 default:
14641 c_parser_error (parser,
14642 "expected %<+%>, %<*%>, %<-%>, %<&%>, "
14643 "%<^%>, %<|%>, %<&&%>, %<||%> or identifier");
14644 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
14645 return list;
14646 }
14647 c_parser_consume_token (parser);
14648 reduc_id = c_omp_reduction_id (code, reduc_id);
14649 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
14650 {
14651 tree nl, c;
14652
14653 nl = c_parser_omp_variable_list (parser, clause_loc, kind, list);
14654 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
14655 {
14656 tree d = OMP_CLAUSE_DECL (c), type;
14657 if (TREE_CODE (d) != TREE_LIST)
14658 type = TREE_TYPE (d);
14659 else
14660 {
14661 int cnt = 0;
14662 tree t;
14663 for (t = d; TREE_CODE (t) == TREE_LIST; t = TREE_CHAIN (t))
14664 cnt++;
14665 type = TREE_TYPE (t);
14666 while (cnt > 0)
14667 {
14668 if (TREE_CODE (type) != POINTER_TYPE
14669 && TREE_CODE (type) != ARRAY_TYPE)
14670 break;
14671 type = TREE_TYPE (type);
14672 cnt--;
14673 }
14674 }
14675 while (TREE_CODE (type) == ARRAY_TYPE)
14676 type = TREE_TYPE (type);
14677 OMP_CLAUSE_REDUCTION_CODE (c) = code;
14678 if (task)
14679 OMP_CLAUSE_REDUCTION_TASK (c) = 1;
14680 else if (inscan)
14681 OMP_CLAUSE_REDUCTION_INSCAN (c) = 1;
14682 if (code == ERROR_MARK
14683 || !(INTEGRAL_TYPE_P (type)
14684 || TREE_CODE (type) == REAL_TYPE
14685 || TREE_CODE (type) == COMPLEX_TYPE))
14686 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c)
14687 = c_omp_reduction_lookup (reduc_id,
14688 TYPE_MAIN_VARIANT (type));
14689 }
14690
14691 list = nl;
14692 }
14693 parens.skip_until_found_close (parser);
14694 }
14695 return list;
14696 }
14697
14698 /* OpenMP 2.5:
14699 schedule ( schedule-kind )
14700 schedule ( schedule-kind , expression )
14701
14702 schedule-kind:
14703 static | dynamic | guided | runtime | auto
14704
14705 OpenMP 4.5:
14706 schedule ( schedule-modifier : schedule-kind )
14707 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
14708
14709 schedule-modifier:
14710 simd
14711 monotonic
14712 nonmonotonic */
14713
14714 static tree
14715 c_parser_omp_clause_schedule (c_parser *parser, tree list)
14716 {
14717 tree c, t;
14718 location_t loc = c_parser_peek_token (parser)->location;
14719 int modifiers = 0, nmodifiers = 0;
14720
14721 matching_parens parens;
14722 if (!parens.require_open (parser))
14723 return list;
14724
14725 c = build_omp_clause (loc, OMP_CLAUSE_SCHEDULE);
14726
14727 while (c_parser_next_token_is (parser, CPP_NAME))
14728 {
14729 tree kind = c_parser_peek_token (parser)->value;
14730 const char *p = IDENTIFIER_POINTER (kind);
14731 if (strcmp ("simd", p) == 0)
14732 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
14733 else if (strcmp ("monotonic", p) == 0)
14734 modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
14735 else if (strcmp ("nonmonotonic", p) == 0)
14736 modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
14737 else
14738 break;
14739 c_parser_consume_token (parser);
14740 if (nmodifiers++ == 0
14741 && c_parser_next_token_is (parser, CPP_COMMA))
14742 c_parser_consume_token (parser);
14743 else
14744 {
14745 c_parser_require (parser, CPP_COLON, "expected %<:%>");
14746 break;
14747 }
14748 }
14749
14750 if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
14751 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
14752 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
14753 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
14754 {
14755 error_at (loc, "both %<monotonic%> and %<nonmonotonic%> modifiers "
14756 "specified");
14757 modifiers = 0;
14758 }
14759
14760 if (c_parser_next_token_is (parser, CPP_NAME))
14761 {
14762 tree kind = c_parser_peek_token (parser)->value;
14763 const char *p = IDENTIFIER_POINTER (kind);
14764
14765 switch (p[0])
14766 {
14767 case 'd':
14768 if (strcmp ("dynamic", p) != 0)
14769 goto invalid_kind;
14770 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
14771 break;
14772
14773 case 'g':
14774 if (strcmp ("guided", p) != 0)
14775 goto invalid_kind;
14776 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
14777 break;
14778
14779 case 'r':
14780 if (strcmp ("runtime", p) != 0)
14781 goto invalid_kind;
14782 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
14783 break;
14784
14785 default:
14786 goto invalid_kind;
14787 }
14788 }
14789 else if (c_parser_next_token_is_keyword (parser, RID_STATIC))
14790 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
14791 else if (c_parser_next_token_is_keyword (parser, RID_AUTO))
14792 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
14793 else
14794 goto invalid_kind;
14795
14796 c_parser_consume_token (parser);
14797 if (c_parser_next_token_is (parser, CPP_COMMA))
14798 {
14799 location_t here;
14800 c_parser_consume_token (parser);
14801
14802 here = c_parser_peek_token (parser)->location;
14803 c_expr expr = c_parser_expr_no_commas (parser, NULL);
14804 expr = convert_lvalue_to_rvalue (here, expr, false, true);
14805 t = expr.value;
14806 t = c_fully_fold (t, false, NULL);
14807
14808 if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
14809 error_at (here, "schedule %<runtime%> does not take "
14810 "a %<chunk_size%> parameter");
14811 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
14812 error_at (here,
14813 "schedule %<auto%> does not take "
14814 "a %<chunk_size%> parameter");
14815 else if (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE)
14816 {
14817 /* Attempt to statically determine when the number isn't
14818 positive. */
14819 tree s = fold_build2_loc (loc, LE_EXPR, boolean_type_node, t,
14820 build_int_cst (TREE_TYPE (t), 0));
14821 protected_set_expr_location (s, loc);
14822 if (s == boolean_true_node)
14823 {
14824 warning_at (loc, 0,
14825 "chunk size value must be positive");
14826 t = integer_one_node;
14827 }
14828 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
14829 }
14830 else
14831 c_parser_error (parser, "expected integer expression");
14832
14833 parens.skip_until_found_close (parser);
14834 }
14835 else
14836 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
14837 "expected %<,%> or %<)%>");
14838
14839 OMP_CLAUSE_SCHEDULE_KIND (c)
14840 = (enum omp_clause_schedule_kind)
14841 (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
14842
14843 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
14844 OMP_CLAUSE_CHAIN (c) = list;
14845 return c;
14846
14847 invalid_kind:
14848 c_parser_error (parser, "invalid schedule kind");
14849 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
14850 return list;
14851 }
14852
14853 /* OpenMP 2.5:
14854 shared ( variable-list ) */
14855
14856 static tree
14857 c_parser_omp_clause_shared (c_parser *parser, tree list)
14858 {
14859 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_SHARED, list);
14860 }
14861
14862 /* OpenMP 3.0:
14863 untied */
14864
14865 static tree
14866 c_parser_omp_clause_untied (c_parser *parser ATTRIBUTE_UNUSED, tree list)
14867 {
14868 tree c;
14869
14870 /* FIXME: Should we allow duplicates? */
14871 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied");
14872
14873 c = build_omp_clause (c_parser_peek_token (parser)->location,
14874 OMP_CLAUSE_UNTIED);
14875 OMP_CLAUSE_CHAIN (c) = list;
14876
14877 return c;
14878 }
14879
14880 /* OpenMP 4.0:
14881 inbranch
14882 notinbranch */
14883
14884 static tree
14885 c_parser_omp_clause_branch (c_parser *parser ATTRIBUTE_UNUSED,
14886 enum omp_clause_code code, tree list)
14887 {
14888 check_no_duplicate_clause (list, code, omp_clause_code_name[code]);
14889
14890 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
14891 OMP_CLAUSE_CHAIN (c) = list;
14892
14893 return c;
14894 }
14895
14896 /* OpenMP 4.0:
14897 parallel
14898 for
14899 sections
14900 taskgroup */
14901
14902 static tree
14903 c_parser_omp_clause_cancelkind (c_parser *parser ATTRIBUTE_UNUSED,
14904 enum omp_clause_code code, tree list)
14905 {
14906 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
14907 OMP_CLAUSE_CHAIN (c) = list;
14908
14909 return c;
14910 }
14911
14912 /* OpenMP 4.5:
14913 nogroup */
14914
14915 static tree
14916 c_parser_omp_clause_nogroup (c_parser *parser ATTRIBUTE_UNUSED, tree list)
14917 {
14918 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup");
14919 tree c = build_omp_clause (c_parser_peek_token (parser)->location,
14920 OMP_CLAUSE_NOGROUP);
14921 OMP_CLAUSE_CHAIN (c) = list;
14922 return c;
14923 }
14924
14925 /* OpenMP 4.5:
14926 simd
14927 threads */
14928
14929 static tree
14930 c_parser_omp_clause_orderedkind (c_parser *parser ATTRIBUTE_UNUSED,
14931 enum omp_clause_code code, tree list)
14932 {
14933 check_no_duplicate_clause (list, code, omp_clause_code_name[code]);
14934 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
14935 OMP_CLAUSE_CHAIN (c) = list;
14936 return c;
14937 }
14938
14939 /* OpenMP 4.0:
14940 num_teams ( expression ) */
14941
14942 static tree
14943 c_parser_omp_clause_num_teams (c_parser *parser, tree list)
14944 {
14945 location_t num_teams_loc = c_parser_peek_token (parser)->location;
14946 matching_parens parens;
14947 if (parens.require_open (parser))
14948 {
14949 location_t expr_loc = c_parser_peek_token (parser)->location;
14950 c_expr expr = c_parser_expr_no_commas (parser, NULL);
14951 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
14952 tree c, t = expr.value;
14953 t = c_fully_fold (t, false, NULL);
14954
14955 parens.skip_until_found_close (parser);
14956
14957 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
14958 {
14959 c_parser_error (parser, "expected integer expression");
14960 return list;
14961 }
14962
14963 /* Attempt to statically determine when the number isn't positive. */
14964 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
14965 build_int_cst (TREE_TYPE (t), 0));
14966 protected_set_expr_location (c, expr_loc);
14967 if (c == boolean_true_node)
14968 {
14969 warning_at (expr_loc, 0, "%<num_teams%> value must be positive");
14970 t = integer_one_node;
14971 }
14972
14973 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS, "num_teams");
14974
14975 c = build_omp_clause (num_teams_loc, OMP_CLAUSE_NUM_TEAMS);
14976 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
14977 OMP_CLAUSE_CHAIN (c) = list;
14978 list = c;
14979 }
14980
14981 return list;
14982 }
14983
14984 /* OpenMP 4.0:
14985 thread_limit ( expression ) */
14986
14987 static tree
14988 c_parser_omp_clause_thread_limit (c_parser *parser, tree list)
14989 {
14990 location_t num_thread_limit_loc = c_parser_peek_token (parser)->location;
14991 matching_parens parens;
14992 if (parens.require_open (parser))
14993 {
14994 location_t expr_loc = c_parser_peek_token (parser)->location;
14995 c_expr expr = c_parser_expr_no_commas (parser, NULL);
14996 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
14997 tree c, t = expr.value;
14998 t = c_fully_fold (t, false, NULL);
14999
15000 parens.skip_until_found_close (parser);
15001
15002 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
15003 {
15004 c_parser_error (parser, "expected integer expression");
15005 return list;
15006 }
15007
15008 /* Attempt to statically determine when the number isn't positive. */
15009 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
15010 build_int_cst (TREE_TYPE (t), 0));
15011 protected_set_expr_location (c, expr_loc);
15012 if (c == boolean_true_node)
15013 {
15014 warning_at (expr_loc, 0, "%<thread_limit%> value must be positive");
15015 t = integer_one_node;
15016 }
15017
15018 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
15019 "thread_limit");
15020
15021 c = build_omp_clause (num_thread_limit_loc, OMP_CLAUSE_THREAD_LIMIT);
15022 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
15023 OMP_CLAUSE_CHAIN (c) = list;
15024 list = c;
15025 }
15026
15027 return list;
15028 }
15029
15030 /* OpenMP 4.0:
15031 aligned ( variable-list )
15032 aligned ( variable-list : constant-expression ) */
15033
15034 static tree
15035 c_parser_omp_clause_aligned (c_parser *parser, tree list)
15036 {
15037 location_t clause_loc = c_parser_peek_token (parser)->location;
15038 tree nl, c;
15039
15040 matching_parens parens;
15041 if (!parens.require_open (parser))
15042 return list;
15043
15044 nl = c_parser_omp_variable_list (parser, clause_loc,
15045 OMP_CLAUSE_ALIGNED, list);
15046
15047 if (c_parser_next_token_is (parser, CPP_COLON))
15048 {
15049 c_parser_consume_token (parser);
15050 location_t expr_loc = c_parser_peek_token (parser)->location;
15051 c_expr expr = c_parser_expr_no_commas (parser, NULL);
15052 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
15053 tree alignment = expr.value;
15054 alignment = c_fully_fold (alignment, false, NULL);
15055 if (TREE_CODE (alignment) != INTEGER_CST
15056 || !INTEGRAL_TYPE_P (TREE_TYPE (alignment))
15057 || tree_int_cst_sgn (alignment) != 1)
15058 {
15059 error_at (clause_loc, "%<aligned%> clause alignment expression must "
15060 "be positive constant integer expression");
15061 alignment = NULL_TREE;
15062 }
15063
15064 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
15065 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
15066 }
15067
15068 parens.skip_until_found_close (parser);
15069 return nl;
15070 }
15071
15072 /* OpenMP 4.0:
15073 linear ( variable-list )
15074 linear ( variable-list : expression )
15075
15076 OpenMP 4.5:
15077 linear ( modifier ( variable-list ) )
15078 linear ( modifier ( variable-list ) : expression ) */
15079
15080 static tree
15081 c_parser_omp_clause_linear (c_parser *parser, tree list)
15082 {
15083 location_t clause_loc = c_parser_peek_token (parser)->location;
15084 tree nl, c, step;
15085 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
15086
15087 matching_parens parens;
15088 if (!parens.require_open (parser))
15089 return list;
15090
15091 if (c_parser_next_token_is (parser, CPP_NAME))
15092 {
15093 c_token *tok = c_parser_peek_token (parser);
15094 const char *p = IDENTIFIER_POINTER (tok->value);
15095 if (strcmp ("val", p) == 0)
15096 kind = OMP_CLAUSE_LINEAR_VAL;
15097 if (c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN)
15098 kind = OMP_CLAUSE_LINEAR_DEFAULT;
15099 if (kind != OMP_CLAUSE_LINEAR_DEFAULT)
15100 {
15101 c_parser_consume_token (parser);
15102 c_parser_consume_token (parser);
15103 }
15104 }
15105
15106 nl = c_parser_omp_variable_list (parser, clause_loc,
15107 OMP_CLAUSE_LINEAR, list);
15108
15109 if (kind != OMP_CLAUSE_LINEAR_DEFAULT)
15110 parens.skip_until_found_close (parser);
15111
15112 if (c_parser_next_token_is (parser, CPP_COLON))
15113 {
15114 c_parser_consume_token (parser);
15115 location_t expr_loc = c_parser_peek_token (parser)->location;
15116 c_expr expr = c_parser_expr_no_commas (parser, NULL);
15117 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
15118 step = expr.value;
15119 step = c_fully_fold (step, false, NULL);
15120 if (!INTEGRAL_TYPE_P (TREE_TYPE (step)))
15121 {
15122 error_at (clause_loc, "%<linear%> clause step expression must "
15123 "be integral");
15124 step = integer_one_node;
15125 }
15126
15127 }
15128 else
15129 step = integer_one_node;
15130
15131 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
15132 {
15133 OMP_CLAUSE_LINEAR_STEP (c) = step;
15134 OMP_CLAUSE_LINEAR_KIND (c) = kind;
15135 }
15136
15137 parens.skip_until_found_close (parser);
15138 return nl;
15139 }
15140
15141 /* OpenMP 5.0:
15142 nontemporal ( variable-list ) */
15143
15144 static tree
15145 c_parser_omp_clause_nontemporal (c_parser *parser, tree list)
15146 {
15147 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_NONTEMPORAL, list);
15148 }
15149
15150 /* OpenMP 4.0:
15151 safelen ( constant-expression ) */
15152
15153 static tree
15154 c_parser_omp_clause_safelen (c_parser *parser, tree list)
15155 {
15156 location_t clause_loc = c_parser_peek_token (parser)->location;
15157 tree c, t;
15158
15159 matching_parens parens;
15160 if (!parens.require_open (parser))
15161 return list;
15162
15163 location_t expr_loc = c_parser_peek_token (parser)->location;
15164 c_expr expr = c_parser_expr_no_commas (parser, NULL);
15165 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
15166 t = expr.value;
15167 t = c_fully_fold (t, false, NULL);
15168 if (TREE_CODE (t) != INTEGER_CST
15169 || !INTEGRAL_TYPE_P (TREE_TYPE (t))
15170 || tree_int_cst_sgn (t) != 1)
15171 {
15172 error_at (clause_loc, "%<safelen%> clause expression must "
15173 "be positive constant integer expression");
15174 t = NULL_TREE;
15175 }
15176
15177 parens.skip_until_found_close (parser);
15178 if (t == NULL_TREE || t == error_mark_node)
15179 return list;
15180
15181 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen");
15182
15183 c = build_omp_clause (clause_loc, OMP_CLAUSE_SAFELEN);
15184 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
15185 OMP_CLAUSE_CHAIN (c) = list;
15186 return c;
15187 }
15188
15189 /* OpenMP 4.0:
15190 simdlen ( constant-expression ) */
15191
15192 static tree
15193 c_parser_omp_clause_simdlen (c_parser *parser, tree list)
15194 {
15195 location_t clause_loc = c_parser_peek_token (parser)->location;
15196 tree c, t;
15197
15198 matching_parens parens;
15199 if (!parens.require_open (parser))
15200 return list;
15201
15202 location_t expr_loc = c_parser_peek_token (parser)->location;
15203 c_expr expr = c_parser_expr_no_commas (parser, NULL);
15204 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
15205 t = expr.value;
15206 t = c_fully_fold (t, false, NULL);
15207 if (TREE_CODE (t) != INTEGER_CST
15208 || !INTEGRAL_TYPE_P (TREE_TYPE (t))
15209 || tree_int_cst_sgn (t) != 1)
15210 {
15211 error_at (clause_loc, "%<simdlen%> clause expression must "
15212 "be positive constant integer expression");
15213 t = NULL_TREE;
15214 }
15215
15216 parens.skip_until_found_close (parser);
15217 if (t == NULL_TREE || t == error_mark_node)
15218 return list;
15219
15220 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen");
15221
15222 c = build_omp_clause (clause_loc, OMP_CLAUSE_SIMDLEN);
15223 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
15224 OMP_CLAUSE_CHAIN (c) = list;
15225 return c;
15226 }
15227
15228 /* OpenMP 4.5:
15229 vec:
15230 identifier [+/- integer]
15231 vec , identifier [+/- integer]
15232 */
15233
15234 static tree
15235 c_parser_omp_clause_depend_sink (c_parser *parser, location_t clause_loc,
15236 tree list)
15237 {
15238 tree vec = NULL;
15239 if (c_parser_next_token_is_not (parser, CPP_NAME)
15240 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
15241 {
15242 c_parser_error (parser, "expected identifier");
15243 return list;
15244 }
15245
15246 while (c_parser_next_token_is (parser, CPP_NAME)
15247 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
15248 {
15249 tree t = lookup_name (c_parser_peek_token (parser)->value);
15250 tree addend = NULL;
15251
15252 if (t == NULL_TREE)
15253 {
15254 undeclared_variable (c_parser_peek_token (parser)->location,
15255 c_parser_peek_token (parser)->value);
15256 t = error_mark_node;
15257 }
15258
15259 c_parser_consume_token (parser);
15260
15261 bool neg = false;
15262 if (c_parser_next_token_is (parser, CPP_MINUS))
15263 neg = true;
15264 else if (!c_parser_next_token_is (parser, CPP_PLUS))
15265 {
15266 addend = integer_zero_node;
15267 neg = false;
15268 goto add_to_vector;
15269 }
15270 c_parser_consume_token (parser);
15271
15272 if (c_parser_next_token_is_not (parser, CPP_NUMBER))
15273 {
15274 c_parser_error (parser, "expected integer");
15275 return list;
15276 }
15277
15278 addend = c_parser_peek_token (parser)->value;
15279 if (TREE_CODE (addend) != INTEGER_CST)
15280 {
15281 c_parser_error (parser, "expected integer");
15282 return list;
15283 }
15284 c_parser_consume_token (parser);
15285
15286 add_to_vector:
15287 if (t != error_mark_node)
15288 {
15289 vec = tree_cons (addend, t, vec);
15290 if (neg)
15291 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec) = 1;
15292 }
15293
15294 if (c_parser_next_token_is_not (parser, CPP_COMMA))
15295 break;
15296
15297 c_parser_consume_token (parser);
15298 }
15299
15300 if (vec == NULL_TREE)
15301 return list;
15302
15303 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
15304 OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
15305 OMP_CLAUSE_DECL (u) = nreverse (vec);
15306 OMP_CLAUSE_CHAIN (u) = list;
15307 return u;
15308 }
15309
15310 /* OpenMP 5.0:
15311 iterators ( iterators-definition )
15312
15313 iterators-definition:
15314 iterator-specifier
15315 iterator-specifier , iterators-definition
15316
15317 iterator-specifier:
15318 identifier = range-specification
15319 iterator-type identifier = range-specification
15320
15321 range-specification:
15322 begin : end
15323 begin : end : step */
15324
15325 static tree
15326 c_parser_omp_iterators (c_parser *parser)
15327 {
15328 tree ret = NULL_TREE, *last = &ret;
15329 c_parser_consume_token (parser);
15330
15331 push_scope ();
15332
15333 matching_parens parens;
15334 if (!parens.require_open (parser))
15335 return error_mark_node;
15336
15337 do
15338 {
15339 tree iter_type = NULL_TREE, type_expr = NULL_TREE;
15340 if (c_parser_next_tokens_start_typename (parser, cla_prefer_id))
15341 {
15342 struct c_type_name *type = c_parser_type_name (parser);
15343 if (type != NULL)
15344 iter_type = groktypename (type, &type_expr, NULL);
15345 }
15346 if (iter_type == NULL_TREE)
15347 iter_type = integer_type_node;
15348
15349 location_t loc = c_parser_peek_token (parser)->location;
15350 if (!c_parser_next_token_is (parser, CPP_NAME))
15351 {
15352 c_parser_error (parser, "expected identifier");
15353 break;
15354 }
15355
15356 tree id = c_parser_peek_token (parser)->value;
15357 c_parser_consume_token (parser);
15358
15359 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
15360 break;
15361
15362 location_t eloc = c_parser_peek_token (parser)->location;
15363 c_expr expr = c_parser_expr_no_commas (parser, NULL);
15364 expr = convert_lvalue_to_rvalue (eloc, expr, true, false);
15365 tree begin = expr.value;
15366
15367 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
15368 break;
15369
15370 eloc = c_parser_peek_token (parser)->location;
15371 expr = c_parser_expr_no_commas (parser, NULL);
15372 expr = convert_lvalue_to_rvalue (eloc, expr, true, false);
15373 tree end = expr.value;
15374
15375 tree step = integer_one_node;
15376 if (c_parser_next_token_is (parser, CPP_COLON))
15377 {
15378 c_parser_consume_token (parser);
15379 eloc = c_parser_peek_token (parser)->location;
15380 expr = c_parser_expr_no_commas (parser, NULL);
15381 expr = convert_lvalue_to_rvalue (eloc, expr, true, false);
15382 step = expr.value;
15383 }
15384
15385 tree iter_var = build_decl (loc, VAR_DECL, id, iter_type);
15386 DECL_ARTIFICIAL (iter_var) = 1;
15387 DECL_CONTEXT (iter_var) = current_function_decl;
15388 pushdecl (iter_var);
15389
15390 *last = make_tree_vec (6);
15391 TREE_VEC_ELT (*last, 0) = iter_var;
15392 TREE_VEC_ELT (*last, 1) = begin;
15393 TREE_VEC_ELT (*last, 2) = end;
15394 TREE_VEC_ELT (*last, 3) = step;
15395 last = &TREE_CHAIN (*last);
15396
15397 if (c_parser_next_token_is (parser, CPP_COMMA))
15398 {
15399 c_parser_consume_token (parser);
15400 continue;
15401 }
15402 break;
15403 }
15404 while (1);
15405
15406 parens.skip_until_found_close (parser);
15407 return ret ? ret : error_mark_node;
15408 }
15409
15410 /* OpenMP 4.0:
15411 depend ( depend-kind: variable-list )
15412
15413 depend-kind:
15414 in | out | inout
15415
15416 OpenMP 4.5:
15417 depend ( source )
15418
15419 depend ( sink : vec )
15420
15421 OpenMP 5.0:
15422 depend ( depend-modifier , depend-kind: variable-list )
15423
15424 depend-kind:
15425 in | out | inout | mutexinoutset | depobj
15426
15427 depend-modifier:
15428 iterator ( iterators-definition ) */
15429
15430 static tree
15431 c_parser_omp_clause_depend (c_parser *parser, tree list)
15432 {
15433 location_t clause_loc = c_parser_peek_token (parser)->location;
15434 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_LAST;
15435 tree nl, c, iterators = NULL_TREE;
15436
15437 matching_parens parens;
15438 if (!parens.require_open (parser))
15439 return list;
15440
15441 do
15442 {
15443 if (c_parser_next_token_is_not (parser, CPP_NAME))
15444 goto invalid_kind;
15445
15446 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
15447 if (strcmp ("iterator", p) == 0 && iterators == NULL_TREE)
15448 {
15449 iterators = c_parser_omp_iterators (parser);
15450 c_parser_require (parser, CPP_COMMA, "expected %<,%>");
15451 continue;
15452 }
15453 if (strcmp ("in", p) == 0)
15454 kind = OMP_CLAUSE_DEPEND_IN;
15455 else if (strcmp ("inout", p) == 0)
15456 kind = OMP_CLAUSE_DEPEND_INOUT;
15457 else if (strcmp ("mutexinoutset", p) == 0)
15458 kind = OMP_CLAUSE_DEPEND_MUTEXINOUTSET;
15459 else if (strcmp ("out", p) == 0)
15460 kind = OMP_CLAUSE_DEPEND_OUT;
15461 else if (strcmp ("depobj", p) == 0)
15462 kind = OMP_CLAUSE_DEPEND_DEPOBJ;
15463 else if (strcmp ("sink", p) == 0)
15464 kind = OMP_CLAUSE_DEPEND_SINK;
15465 else if (strcmp ("source", p) == 0)
15466 kind = OMP_CLAUSE_DEPEND_SOURCE;
15467 else
15468 goto invalid_kind;
15469 break;
15470 }
15471 while (1);
15472
15473 c_parser_consume_token (parser);
15474
15475 if (iterators
15476 && (kind == OMP_CLAUSE_DEPEND_SOURCE || kind == OMP_CLAUSE_DEPEND_SINK))
15477 {
15478 pop_scope ();
15479 error_at (clause_loc, "%<iterator%> modifier incompatible with %qs",
15480 kind == OMP_CLAUSE_DEPEND_SOURCE ? "source" : "sink");
15481 iterators = NULL_TREE;
15482 }
15483
15484 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
15485 {
15486 c = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
15487 OMP_CLAUSE_DEPEND_KIND (c) = kind;
15488 OMP_CLAUSE_DECL (c) = NULL_TREE;
15489 OMP_CLAUSE_CHAIN (c) = list;
15490 parens.skip_until_found_close (parser);
15491 return c;
15492 }
15493
15494 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
15495 goto resync_fail;
15496
15497 if (kind == OMP_CLAUSE_DEPEND_SINK)
15498 nl = c_parser_omp_clause_depend_sink (parser, clause_loc, list);
15499 else
15500 {
15501 nl = c_parser_omp_variable_list (parser, clause_loc,
15502 OMP_CLAUSE_DEPEND, list);
15503
15504 if (iterators)
15505 {
15506 tree block = pop_scope ();
15507 if (iterators == error_mark_node)
15508 iterators = NULL_TREE;
15509 else
15510 TREE_VEC_ELT (iterators, 5) = block;
15511 }
15512
15513 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
15514 {
15515 OMP_CLAUSE_DEPEND_KIND (c) = kind;
15516 if (iterators)
15517 OMP_CLAUSE_DECL (c)
15518 = build_tree_list (iterators, OMP_CLAUSE_DECL (c));
15519 }
15520 }
15521
15522 parens.skip_until_found_close (parser);
15523 return nl;
15524
15525 invalid_kind:
15526 c_parser_error (parser, "invalid depend kind");
15527 resync_fail:
15528 parens.skip_until_found_close (parser);
15529 if (iterators)
15530 pop_scope ();
15531 return list;
15532 }
15533
15534 /* OpenMP 4.0:
15535 map ( map-kind: variable-list )
15536 map ( variable-list )
15537
15538 map-kind:
15539 alloc | to | from | tofrom
15540
15541 OpenMP 4.5:
15542 map-kind:
15543 alloc | to | from | tofrom | release | delete
15544
15545 map ( always [,] map-kind: variable-list ) */
15546
15547 static tree
15548 c_parser_omp_clause_map (c_parser *parser, tree list)
15549 {
15550 location_t clause_loc = c_parser_peek_token (parser)->location;
15551 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
15552 int always = 0;
15553 enum c_id_kind always_id_kind = C_ID_NONE;
15554 location_t always_loc = UNKNOWN_LOCATION;
15555 tree always_id = NULL_TREE;
15556 tree nl, c;
15557
15558 matching_parens parens;
15559 if (!parens.require_open (parser))
15560 return list;
15561
15562 if (c_parser_next_token_is (parser, CPP_NAME))
15563 {
15564 c_token *tok = c_parser_peek_token (parser);
15565 const char *p = IDENTIFIER_POINTER (tok->value);
15566 always_id_kind = tok->id_kind;
15567 always_loc = tok->location;
15568 always_id = tok->value;
15569 if (strcmp ("always", p) == 0)
15570 {
15571 c_token *sectok = c_parser_peek_2nd_token (parser);
15572 if (sectok->type == CPP_COMMA)
15573 {
15574 c_parser_consume_token (parser);
15575 c_parser_consume_token (parser);
15576 always = 2;
15577 }
15578 else if (sectok->type == CPP_NAME)
15579 {
15580 p = IDENTIFIER_POINTER (sectok->value);
15581 if (strcmp ("alloc", p) == 0
15582 || strcmp ("to", p) == 0
15583 || strcmp ("from", p) == 0
15584 || strcmp ("tofrom", p) == 0
15585 || strcmp ("release", p) == 0
15586 || strcmp ("delete", p) == 0)
15587 {
15588 c_parser_consume_token (parser);
15589 always = 1;
15590 }
15591 }
15592 }
15593 }
15594
15595 if (c_parser_next_token_is (parser, CPP_NAME)
15596 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
15597 {
15598 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
15599 if (strcmp ("alloc", p) == 0)
15600 kind = GOMP_MAP_ALLOC;
15601 else if (strcmp ("to", p) == 0)
15602 kind = always ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
15603 else if (strcmp ("from", p) == 0)
15604 kind = always ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
15605 else if (strcmp ("tofrom", p) == 0)
15606 kind = always ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
15607 else if (strcmp ("release", p) == 0)
15608 kind = GOMP_MAP_RELEASE;
15609 else if (strcmp ("delete", p) == 0)
15610 kind = GOMP_MAP_DELETE;
15611 else
15612 {
15613 c_parser_error (parser, "invalid map kind");
15614 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
15615 "expected %<)%>");
15616 return list;
15617 }
15618 c_parser_consume_token (parser);
15619 c_parser_consume_token (parser);
15620 }
15621 else if (always)
15622 {
15623 if (always_id_kind != C_ID_ID)
15624 {
15625 c_parser_error (parser, "expected identifier");
15626 parens.skip_until_found_close (parser);
15627 return list;
15628 }
15629
15630 tree t = lookup_name (always_id);
15631 if (t == NULL_TREE)
15632 {
15633 undeclared_variable (always_loc, always_id);
15634 t = error_mark_node;
15635 }
15636 if (t != error_mark_node)
15637 {
15638 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_MAP);
15639 OMP_CLAUSE_DECL (u) = t;
15640 OMP_CLAUSE_CHAIN (u) = list;
15641 OMP_CLAUSE_SET_MAP_KIND (u, kind);
15642 list = u;
15643 }
15644 if (always == 1)
15645 {
15646 parens.skip_until_found_close (parser);
15647 return list;
15648 }
15649 }
15650
15651 nl = c_parser_omp_variable_list (parser, clause_loc, OMP_CLAUSE_MAP, list);
15652
15653 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
15654 OMP_CLAUSE_SET_MAP_KIND (c, kind);
15655
15656 parens.skip_until_found_close (parser);
15657 return nl;
15658 }
15659
15660 /* OpenMP 4.0:
15661 device ( expression ) */
15662
15663 static tree
15664 c_parser_omp_clause_device (c_parser *parser, tree list)
15665 {
15666 location_t clause_loc = c_parser_peek_token (parser)->location;
15667 matching_parens parens;
15668 if (parens.require_open (parser))
15669 {
15670 location_t expr_loc = c_parser_peek_token (parser)->location;
15671 c_expr expr = c_parser_expr_no_commas (parser, NULL);
15672 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
15673 tree c, t = expr.value;
15674 t = c_fully_fold (t, false, NULL);
15675
15676 parens.skip_until_found_close (parser);
15677
15678 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
15679 {
15680 c_parser_error (parser, "expected integer expression");
15681 return list;
15682 }
15683
15684 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE, "device");
15685
15686 c = build_omp_clause (clause_loc, OMP_CLAUSE_DEVICE);
15687 OMP_CLAUSE_DEVICE_ID (c) = t;
15688 OMP_CLAUSE_CHAIN (c) = list;
15689 list = c;
15690 }
15691
15692 return list;
15693 }
15694
15695 /* OpenMP 4.0:
15696 dist_schedule ( static )
15697 dist_schedule ( static , expression ) */
15698
15699 static tree
15700 c_parser_omp_clause_dist_schedule (c_parser *parser, tree list)
15701 {
15702 tree c, t = NULL_TREE;
15703 location_t loc = c_parser_peek_token (parser)->location;
15704
15705 matching_parens parens;
15706 if (!parens.require_open (parser))
15707 return list;
15708
15709 if (!c_parser_next_token_is_keyword (parser, RID_STATIC))
15710 {
15711 c_parser_error (parser, "invalid dist_schedule kind");
15712 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
15713 "expected %<)%>");
15714 return list;
15715 }
15716
15717 c_parser_consume_token (parser);
15718 if (c_parser_next_token_is (parser, CPP_COMMA))
15719 {
15720 c_parser_consume_token (parser);
15721
15722 location_t expr_loc = c_parser_peek_token (parser)->location;
15723 c_expr expr = c_parser_expr_no_commas (parser, NULL);
15724 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
15725 t = expr.value;
15726 t = c_fully_fold (t, false, NULL);
15727 parens.skip_until_found_close (parser);
15728 }
15729 else
15730 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
15731 "expected %<,%> or %<)%>");
15732
15733 /* check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE,
15734 "dist_schedule"); */
15735 if (omp_find_clause (list, OMP_CLAUSE_DIST_SCHEDULE))
15736 warning_at (loc, 0, "too many %qs clauses", "dist_schedule");
15737 if (t == error_mark_node)
15738 return list;
15739
15740 c = build_omp_clause (loc, OMP_CLAUSE_DIST_SCHEDULE);
15741 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
15742 OMP_CLAUSE_CHAIN (c) = list;
15743 return c;
15744 }
15745
15746 /* OpenMP 4.0:
15747 proc_bind ( proc-bind-kind )
15748
15749 proc-bind-kind:
15750 master | close | spread */
15751
15752 static tree
15753 c_parser_omp_clause_proc_bind (c_parser *parser, tree list)
15754 {
15755 location_t clause_loc = c_parser_peek_token (parser)->location;
15756 enum omp_clause_proc_bind_kind kind;
15757 tree c;
15758
15759 matching_parens parens;
15760 if (!parens.require_open (parser))
15761 return list;
15762
15763 if (c_parser_next_token_is (parser, CPP_NAME))
15764 {
15765 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
15766 if (strcmp ("master", p) == 0)
15767 kind = OMP_CLAUSE_PROC_BIND_MASTER;
15768 else if (strcmp ("close", p) == 0)
15769 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
15770 else if (strcmp ("spread", p) == 0)
15771 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
15772 else
15773 goto invalid_kind;
15774 }
15775 else
15776 goto invalid_kind;
15777
15778 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind");
15779 c_parser_consume_token (parser);
15780 parens.skip_until_found_close (parser);
15781 c = build_omp_clause (clause_loc, OMP_CLAUSE_PROC_BIND);
15782 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
15783 OMP_CLAUSE_CHAIN (c) = list;
15784 return c;
15785
15786 invalid_kind:
15787 c_parser_error (parser, "invalid proc_bind kind");
15788 parens.skip_until_found_close (parser);
15789 return list;
15790 }
15791
15792 /* OpenMP 5.0:
15793 device_type ( host | nohost | any ) */
15794
15795 static tree
15796 c_parser_omp_clause_device_type (c_parser *parser, tree list)
15797 {
15798 location_t clause_loc = c_parser_peek_token (parser)->location;
15799 enum omp_clause_device_type_kind kind;
15800 tree c;
15801
15802 matching_parens parens;
15803 if (!parens.require_open (parser))
15804 return list;
15805
15806 if (c_parser_next_token_is (parser, CPP_NAME))
15807 {
15808 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
15809 if (strcmp ("host", p) == 0)
15810 kind = OMP_CLAUSE_DEVICE_TYPE_HOST;
15811 else if (strcmp ("nohost", p) == 0)
15812 kind = OMP_CLAUSE_DEVICE_TYPE_NOHOST;
15813 else if (strcmp ("any", p) == 0)
15814 kind = OMP_CLAUSE_DEVICE_TYPE_ANY;
15815 else
15816 goto invalid_kind;
15817 }
15818 else
15819 goto invalid_kind;
15820
15821 /* check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE_TYPE,
15822 "device_type"); */
15823 c_parser_consume_token (parser);
15824 parens.skip_until_found_close (parser);
15825 c = build_omp_clause (clause_loc, OMP_CLAUSE_DEVICE_TYPE);
15826 OMP_CLAUSE_DEVICE_TYPE_KIND (c) = kind;
15827 OMP_CLAUSE_CHAIN (c) = list;
15828 return c;
15829
15830 invalid_kind:
15831 c_parser_error (parser, "expected %<host%>, %<nohost%> or %<any%>");
15832 parens.skip_until_found_close (parser);
15833 return list;
15834 }
15835
15836 /* OpenMP 4.0:
15837 to ( variable-list ) */
15838
15839 static tree
15840 c_parser_omp_clause_to (c_parser *parser, tree list)
15841 {
15842 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_TO, list);
15843 }
15844
15845 /* OpenMP 4.0:
15846 from ( variable-list ) */
15847
15848 static tree
15849 c_parser_omp_clause_from (c_parser *parser, tree list)
15850 {
15851 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_FROM, list);
15852 }
15853
15854 /* OpenMP 4.0:
15855 uniform ( variable-list ) */
15856
15857 static tree
15858 c_parser_omp_clause_uniform (c_parser *parser, tree list)
15859 {
15860 /* The clauses location. */
15861 location_t loc = c_parser_peek_token (parser)->location;
15862
15863 matching_parens parens;
15864 if (parens.require_open (parser))
15865 {
15866 list = c_parser_omp_variable_list (parser, loc, OMP_CLAUSE_UNIFORM,
15867 list);
15868 parens.skip_until_found_close (parser);
15869 }
15870 return list;
15871 }
15872
15873 /* Parse all OpenACC clauses. The set clauses allowed by the directive
15874 is a bitmask in MASK. Return the list of clauses found. */
15875
15876 static tree
15877 c_parser_oacc_all_clauses (c_parser *parser, omp_clause_mask mask,
15878 const char *where, bool finish_p = true)
15879 {
15880 tree clauses = NULL;
15881 bool first = true;
15882
15883 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
15884 {
15885 location_t here;
15886 pragma_omp_clause c_kind;
15887 const char *c_name;
15888 tree prev = clauses;
15889
15890 if (!first && c_parser_next_token_is (parser, CPP_COMMA))
15891 c_parser_consume_token (parser);
15892
15893 here = c_parser_peek_token (parser)->location;
15894 c_kind = c_parser_omp_clause_name (parser);
15895
15896 switch (c_kind)
15897 {
15898 case PRAGMA_OACC_CLAUSE_ASYNC:
15899 clauses = c_parser_oacc_clause_async (parser, clauses);
15900 c_name = "async";
15901 break;
15902 case PRAGMA_OACC_CLAUSE_AUTO:
15903 clauses = c_parser_oacc_simple_clause (here, OMP_CLAUSE_AUTO,
15904 clauses);
15905 c_name = "auto";
15906 break;
15907 case PRAGMA_OACC_CLAUSE_ATTACH:
15908 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
15909 c_name = "attach";
15910 break;
15911 case PRAGMA_OACC_CLAUSE_COLLAPSE:
15912 clauses = c_parser_omp_clause_collapse (parser, clauses);
15913 c_name = "collapse";
15914 break;
15915 case PRAGMA_OACC_CLAUSE_COPY:
15916 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
15917 c_name = "copy";
15918 break;
15919 case PRAGMA_OACC_CLAUSE_COPYIN:
15920 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
15921 c_name = "copyin";
15922 break;
15923 case PRAGMA_OACC_CLAUSE_COPYOUT:
15924 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
15925 c_name = "copyout";
15926 break;
15927 case PRAGMA_OACC_CLAUSE_CREATE:
15928 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
15929 c_name = "create";
15930 break;
15931 case PRAGMA_OACC_CLAUSE_DELETE:
15932 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
15933 c_name = "delete";
15934 break;
15935 case PRAGMA_OMP_CLAUSE_DEFAULT:
15936 clauses = c_parser_omp_clause_default (parser, clauses, true);
15937 c_name = "default";
15938 break;
15939 case PRAGMA_OACC_CLAUSE_DETACH:
15940 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
15941 c_name = "detach";
15942 break;
15943 case PRAGMA_OACC_CLAUSE_DEVICE:
15944 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
15945 c_name = "device";
15946 break;
15947 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
15948 clauses = c_parser_oacc_data_clause_deviceptr (parser, clauses);
15949 c_name = "deviceptr";
15950 break;
15951 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
15952 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
15953 c_name = "device_resident";
15954 break;
15955 case PRAGMA_OACC_CLAUSE_FINALIZE:
15956 clauses = c_parser_oacc_simple_clause (here, OMP_CLAUSE_FINALIZE,
15957 clauses);
15958 c_name = "finalize";
15959 break;
15960 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
15961 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
15962 c_name = "firstprivate";
15963 break;
15964 case PRAGMA_OACC_CLAUSE_GANG:
15965 c_name = "gang";
15966 clauses = c_parser_oacc_shape_clause (parser, here, OMP_CLAUSE_GANG,
15967 c_name, clauses);
15968 break;
15969 case PRAGMA_OACC_CLAUSE_HOST:
15970 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
15971 c_name = "host";
15972 break;
15973 case PRAGMA_OACC_CLAUSE_IF:
15974 clauses = c_parser_omp_clause_if (parser, clauses, false);
15975 c_name = "if";
15976 break;
15977 case PRAGMA_OACC_CLAUSE_IF_PRESENT:
15978 clauses = c_parser_oacc_simple_clause (here, OMP_CLAUSE_IF_PRESENT,
15979 clauses);
15980 c_name = "if_present";
15981 break;
15982 case PRAGMA_OACC_CLAUSE_INDEPENDENT:
15983 clauses = c_parser_oacc_simple_clause (here, OMP_CLAUSE_INDEPENDENT,
15984 clauses);
15985 c_name = "independent";
15986 break;
15987 case PRAGMA_OACC_CLAUSE_LINK:
15988 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
15989 c_name = "link";
15990 break;
15991 case PRAGMA_OACC_CLAUSE_NO_CREATE:
15992 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
15993 c_name = "no_create";
15994 break;
15995 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
15996 clauses = c_parser_oacc_single_int_clause (parser,
15997 OMP_CLAUSE_NUM_GANGS,
15998 clauses);
15999 c_name = "num_gangs";
16000 break;
16001 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
16002 clauses = c_parser_oacc_single_int_clause (parser,
16003 OMP_CLAUSE_NUM_WORKERS,
16004 clauses);
16005 c_name = "num_workers";
16006 break;
16007 case PRAGMA_OACC_CLAUSE_PRESENT:
16008 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
16009 c_name = "present";
16010 break;
16011 case PRAGMA_OACC_CLAUSE_PRIVATE:
16012 clauses = c_parser_omp_clause_private (parser, clauses);
16013 c_name = "private";
16014 break;
16015 case PRAGMA_OACC_CLAUSE_REDUCTION:
16016 clauses
16017 = c_parser_omp_clause_reduction (parser, OMP_CLAUSE_REDUCTION,
16018 false, clauses);
16019 c_name = "reduction";
16020 break;
16021 case PRAGMA_OACC_CLAUSE_SEQ:
16022 clauses = c_parser_oacc_simple_clause (here, OMP_CLAUSE_SEQ,
16023 clauses);
16024 c_name = "seq";
16025 break;
16026 case PRAGMA_OACC_CLAUSE_TILE:
16027 clauses = c_parser_oacc_clause_tile (parser, clauses);
16028 c_name = "tile";
16029 break;
16030 case PRAGMA_OACC_CLAUSE_USE_DEVICE:
16031 clauses = c_parser_omp_clause_use_device_ptr (parser, clauses);
16032 c_name = "use_device";
16033 break;
16034 case PRAGMA_OACC_CLAUSE_VECTOR:
16035 c_name = "vector";
16036 clauses = c_parser_oacc_shape_clause (parser, here, OMP_CLAUSE_VECTOR,
16037 c_name, clauses);
16038 break;
16039 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
16040 clauses = c_parser_oacc_single_int_clause (parser,
16041 OMP_CLAUSE_VECTOR_LENGTH,
16042 clauses);
16043 c_name = "vector_length";
16044 break;
16045 case PRAGMA_OACC_CLAUSE_WAIT:
16046 clauses = c_parser_oacc_clause_wait (parser, clauses);
16047 c_name = "wait";
16048 break;
16049 case PRAGMA_OACC_CLAUSE_WORKER:
16050 c_name = "worker";
16051 clauses = c_parser_oacc_shape_clause (parser, here, OMP_CLAUSE_WORKER,
16052 c_name, clauses);
16053 break;
16054 default:
16055 c_parser_error (parser, "expected %<#pragma acc%> clause");
16056 goto saw_error;
16057 }
16058
16059 first = false;
16060
16061 if (((mask >> c_kind) & 1) == 0)
16062 {
16063 /* Remove the invalid clause(s) from the list to avoid
16064 confusing the rest of the compiler. */
16065 clauses = prev;
16066 error_at (here, "%qs is not valid for %qs", c_name, where);
16067 }
16068 }
16069
16070 saw_error:
16071 c_parser_skip_to_pragma_eol (parser);
16072
16073 if (finish_p)
16074 return c_finish_omp_clauses (clauses, C_ORT_ACC);
16075
16076 return clauses;
16077 }
16078
16079 /* Parse all OpenMP clauses. The set clauses allowed by the directive
16080 is a bitmask in MASK. Return the list of clauses found.
16081 FINISH_P set if c_finish_omp_clauses should be called.
16082 NESTED non-zero if clauses should be terminated by closing paren instead
16083 of end of pragma. If it is 2, additionally commas are required in between
16084 the clauses. */
16085
16086 static tree
16087 c_parser_omp_all_clauses (c_parser *parser, omp_clause_mask mask,
16088 const char *where, bool finish_p = true,
16089 int nested = 0)
16090 {
16091 tree clauses = NULL;
16092 bool first = true;
16093
16094 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
16095 {
16096 location_t here;
16097 pragma_omp_clause c_kind;
16098 const char *c_name;
16099 tree prev = clauses;
16100
16101 if (nested && c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
16102 break;
16103
16104 if (!first)
16105 {
16106 if (c_parser_next_token_is (parser, CPP_COMMA))
16107 c_parser_consume_token (parser);
16108 else if (nested == 2)
16109 error_at (c_parser_peek_token (parser)->location,
16110 "clauses in %<simd%> trait should be separated "
16111 "by %<,%>");
16112 }
16113
16114 here = c_parser_peek_token (parser)->location;
16115 c_kind = c_parser_omp_clause_name (parser);
16116
16117 switch (c_kind)
16118 {
16119 case PRAGMA_OMP_CLAUSE_BIND:
16120 clauses = c_parser_omp_clause_bind (parser, clauses);
16121 c_name = "bind";
16122 break;
16123 case PRAGMA_OMP_CLAUSE_COLLAPSE:
16124 clauses = c_parser_omp_clause_collapse (parser, clauses);
16125 c_name = "collapse";
16126 break;
16127 case PRAGMA_OMP_CLAUSE_COPYIN:
16128 clauses = c_parser_omp_clause_copyin (parser, clauses);
16129 c_name = "copyin";
16130 break;
16131 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
16132 clauses = c_parser_omp_clause_copyprivate (parser, clauses);
16133 c_name = "copyprivate";
16134 break;
16135 case PRAGMA_OMP_CLAUSE_DEFAULT:
16136 clauses = c_parser_omp_clause_default (parser, clauses, false);
16137 c_name = "default";
16138 break;
16139 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
16140 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
16141 c_name = "firstprivate";
16142 break;
16143 case PRAGMA_OMP_CLAUSE_FINAL:
16144 clauses = c_parser_omp_clause_final (parser, clauses);
16145 c_name = "final";
16146 break;
16147 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
16148 clauses = c_parser_omp_clause_grainsize (parser, clauses);
16149 c_name = "grainsize";
16150 break;
16151 case PRAGMA_OMP_CLAUSE_HINT:
16152 clauses = c_parser_omp_clause_hint (parser, clauses);
16153 c_name = "hint";
16154 break;
16155 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
16156 clauses = c_parser_omp_clause_defaultmap (parser, clauses);
16157 c_name = "defaultmap";
16158 break;
16159 case PRAGMA_OMP_CLAUSE_IF:
16160 clauses = c_parser_omp_clause_if (parser, clauses, true);
16161 c_name = "if";
16162 break;
16163 case PRAGMA_OMP_CLAUSE_IN_REDUCTION:
16164 clauses
16165 = c_parser_omp_clause_reduction (parser, OMP_CLAUSE_IN_REDUCTION,
16166 true, clauses);
16167 c_name = "in_reduction";
16168 break;
16169 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
16170 clauses = c_parser_omp_clause_lastprivate (parser, clauses);
16171 c_name = "lastprivate";
16172 break;
16173 case PRAGMA_OMP_CLAUSE_MERGEABLE:
16174 clauses = c_parser_omp_clause_mergeable (parser, clauses);
16175 c_name = "mergeable";
16176 break;
16177 case PRAGMA_OMP_CLAUSE_NOWAIT:
16178 clauses = c_parser_omp_clause_nowait (parser, clauses);
16179 c_name = "nowait";
16180 break;
16181 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
16182 clauses = c_parser_omp_clause_num_tasks (parser, clauses);
16183 c_name = "num_tasks";
16184 break;
16185 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
16186 clauses = c_parser_omp_clause_num_threads (parser, clauses);
16187 c_name = "num_threads";
16188 break;
16189 case PRAGMA_OMP_CLAUSE_ORDER:
16190 clauses = c_parser_omp_clause_order (parser, clauses);
16191 c_name = "order";
16192 break;
16193 case PRAGMA_OMP_CLAUSE_ORDERED:
16194 clauses = c_parser_omp_clause_ordered (parser, clauses);
16195 c_name = "ordered";
16196 break;
16197 case PRAGMA_OMP_CLAUSE_PRIORITY:
16198 clauses = c_parser_omp_clause_priority (parser, clauses);
16199 c_name = "priority";
16200 break;
16201 case PRAGMA_OMP_CLAUSE_PRIVATE:
16202 clauses = c_parser_omp_clause_private (parser, clauses);
16203 c_name = "private";
16204 break;
16205 case PRAGMA_OMP_CLAUSE_REDUCTION:
16206 clauses
16207 = c_parser_omp_clause_reduction (parser, OMP_CLAUSE_REDUCTION,
16208 true, clauses);
16209 c_name = "reduction";
16210 break;
16211 case PRAGMA_OMP_CLAUSE_SCHEDULE:
16212 clauses = c_parser_omp_clause_schedule (parser, clauses);
16213 c_name = "schedule";
16214 break;
16215 case PRAGMA_OMP_CLAUSE_SHARED:
16216 clauses = c_parser_omp_clause_shared (parser, clauses);
16217 c_name = "shared";
16218 break;
16219 case PRAGMA_OMP_CLAUSE_TASK_REDUCTION:
16220 clauses
16221 = c_parser_omp_clause_reduction (parser, OMP_CLAUSE_TASK_REDUCTION,
16222 true, clauses);
16223 c_name = "task_reduction";
16224 break;
16225 case PRAGMA_OMP_CLAUSE_UNTIED:
16226 clauses = c_parser_omp_clause_untied (parser, clauses);
16227 c_name = "untied";
16228 break;
16229 case PRAGMA_OMP_CLAUSE_INBRANCH:
16230 clauses = c_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
16231 clauses);
16232 c_name = "inbranch";
16233 break;
16234 case PRAGMA_OMP_CLAUSE_NONTEMPORAL:
16235 clauses = c_parser_omp_clause_nontemporal (parser, clauses);
16236 c_name = "nontemporal";
16237 break;
16238 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
16239 clauses = c_parser_omp_clause_branch (parser, OMP_CLAUSE_NOTINBRANCH,
16240 clauses);
16241 c_name = "notinbranch";
16242 break;
16243 case PRAGMA_OMP_CLAUSE_PARALLEL:
16244 clauses
16245 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
16246 clauses);
16247 c_name = "parallel";
16248 if (!first)
16249 {
16250 clause_not_first:
16251 error_at (here, "%qs must be the first clause of %qs",
16252 c_name, where);
16253 clauses = prev;
16254 }
16255 break;
16256 case PRAGMA_OMP_CLAUSE_FOR:
16257 clauses
16258 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
16259 clauses);
16260 c_name = "for";
16261 if (!first)
16262 goto clause_not_first;
16263 break;
16264 case PRAGMA_OMP_CLAUSE_SECTIONS:
16265 clauses
16266 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
16267 clauses);
16268 c_name = "sections";
16269 if (!first)
16270 goto clause_not_first;
16271 break;
16272 case PRAGMA_OMP_CLAUSE_TASKGROUP:
16273 clauses
16274 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
16275 clauses);
16276 c_name = "taskgroup";
16277 if (!first)
16278 goto clause_not_first;
16279 break;
16280 case PRAGMA_OMP_CLAUSE_LINK:
16281 clauses
16282 = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_LINK, clauses);
16283 c_name = "link";
16284 break;
16285 case PRAGMA_OMP_CLAUSE_TO:
16286 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
16287 clauses
16288 = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_TO_DECLARE,
16289 clauses);
16290 else
16291 clauses = c_parser_omp_clause_to (parser, clauses);
16292 c_name = "to";
16293 break;
16294 case PRAGMA_OMP_CLAUSE_FROM:
16295 clauses = c_parser_omp_clause_from (parser, clauses);
16296 c_name = "from";
16297 break;
16298 case PRAGMA_OMP_CLAUSE_UNIFORM:
16299 clauses = c_parser_omp_clause_uniform (parser, clauses);
16300 c_name = "uniform";
16301 break;
16302 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
16303 clauses = c_parser_omp_clause_num_teams (parser, clauses);
16304 c_name = "num_teams";
16305 break;
16306 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
16307 clauses = c_parser_omp_clause_thread_limit (parser, clauses);
16308 c_name = "thread_limit";
16309 break;
16310 case PRAGMA_OMP_CLAUSE_ALIGNED:
16311 clauses = c_parser_omp_clause_aligned (parser, clauses);
16312 c_name = "aligned";
16313 break;
16314 case PRAGMA_OMP_CLAUSE_LINEAR:
16315 clauses = c_parser_omp_clause_linear (parser, clauses);
16316 c_name = "linear";
16317 break;
16318 case PRAGMA_OMP_CLAUSE_DEPEND:
16319 clauses = c_parser_omp_clause_depend (parser, clauses);
16320 c_name = "depend";
16321 break;
16322 case PRAGMA_OMP_CLAUSE_MAP:
16323 clauses = c_parser_omp_clause_map (parser, clauses);
16324 c_name = "map";
16325 break;
16326 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
16327 clauses = c_parser_omp_clause_use_device_ptr (parser, clauses);
16328 c_name = "use_device_ptr";
16329 break;
16330 case PRAGMA_OMP_CLAUSE_USE_DEVICE_ADDR:
16331 clauses = c_parser_omp_clause_use_device_addr (parser, clauses);
16332 c_name = "use_device_addr";
16333 break;
16334 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
16335 clauses = c_parser_omp_clause_is_device_ptr (parser, clauses);
16336 c_name = "is_device_ptr";
16337 break;
16338 case PRAGMA_OMP_CLAUSE_DEVICE:
16339 clauses = c_parser_omp_clause_device (parser, clauses);
16340 c_name = "device";
16341 break;
16342 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
16343 clauses = c_parser_omp_clause_dist_schedule (parser, clauses);
16344 c_name = "dist_schedule";
16345 break;
16346 case PRAGMA_OMP_CLAUSE_PROC_BIND:
16347 clauses = c_parser_omp_clause_proc_bind (parser, clauses);
16348 c_name = "proc_bind";
16349 break;
16350 case PRAGMA_OMP_CLAUSE_DEVICE_TYPE:
16351 clauses = c_parser_omp_clause_device_type (parser, clauses);
16352 c_name = "device_type";
16353 break;
16354 case PRAGMA_OMP_CLAUSE_SAFELEN:
16355 clauses = c_parser_omp_clause_safelen (parser, clauses);
16356 c_name = "safelen";
16357 break;
16358 case PRAGMA_OMP_CLAUSE_SIMDLEN:
16359 clauses = c_parser_omp_clause_simdlen (parser, clauses);
16360 c_name = "simdlen";
16361 break;
16362 case PRAGMA_OMP_CLAUSE_NOGROUP:
16363 clauses = c_parser_omp_clause_nogroup (parser, clauses);
16364 c_name = "nogroup";
16365 break;
16366 case PRAGMA_OMP_CLAUSE_THREADS:
16367 clauses
16368 = c_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
16369 clauses);
16370 c_name = "threads";
16371 break;
16372 case PRAGMA_OMP_CLAUSE_SIMD:
16373 clauses
16374 = c_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
16375 clauses);
16376 c_name = "simd";
16377 break;
16378 default:
16379 c_parser_error (parser, "expected %<#pragma omp%> clause");
16380 goto saw_error;
16381 }
16382
16383 first = false;
16384
16385 if (((mask >> c_kind) & 1) == 0)
16386 {
16387 /* Remove the invalid clause(s) from the list to avoid
16388 confusing the rest of the compiler. */
16389 clauses = prev;
16390 error_at (here, "%qs is not valid for %qs", c_name, where);
16391 }
16392 }
16393
16394 saw_error:
16395 if (!nested)
16396 c_parser_skip_to_pragma_eol (parser);
16397
16398 if (finish_p)
16399 {
16400 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
16401 return c_finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
16402 return c_finish_omp_clauses (clauses, C_ORT_OMP);
16403 }
16404
16405 return clauses;
16406 }
16407
16408 /* OpenACC 2.0, OpenMP 2.5:
16409 structured-block:
16410 statement
16411
16412 In practice, we're also interested in adding the statement to an
16413 outer node. So it is convenient if we work around the fact that
16414 c_parser_statement calls add_stmt. */
16415
16416 static tree
16417 c_parser_omp_structured_block (c_parser *parser, bool *if_p)
16418 {
16419 tree stmt = push_stmt_list ();
16420 c_parser_statement (parser, if_p);
16421 return pop_stmt_list (stmt);
16422 }
16423
16424 /* OpenACC 2.0:
16425 # pragma acc cache (variable-list) new-line
16426
16427 LOC is the location of the #pragma token.
16428 */
16429
16430 static tree
16431 c_parser_oacc_cache (location_t loc, c_parser *parser)
16432 {
16433 tree stmt, clauses;
16434
16435 clauses = c_parser_omp_var_list_parens (parser, OMP_CLAUSE__CACHE_, NULL);
16436 clauses = c_finish_omp_clauses (clauses, C_ORT_ACC);
16437
16438 c_parser_skip_to_pragma_eol (parser);
16439
16440 stmt = make_node (OACC_CACHE);
16441 TREE_TYPE (stmt) = void_type_node;
16442 OACC_CACHE_CLAUSES (stmt) = clauses;
16443 SET_EXPR_LOCATION (stmt, loc);
16444 add_stmt (stmt);
16445
16446 return stmt;
16447 }
16448
16449 /* OpenACC 2.0:
16450 # pragma acc data oacc-data-clause[optseq] new-line
16451 structured-block
16452
16453 LOC is the location of the #pragma token.
16454 */
16455
16456 #define OACC_DATA_CLAUSE_MASK \
16457 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
16458 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
16459 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
16460 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
16461 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
16462 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
16463 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
16464 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NO_CREATE) \
16465 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT))
16466
16467 static tree
16468 c_parser_oacc_data (location_t loc, c_parser *parser, bool *if_p)
16469 {
16470 tree stmt, clauses, block;
16471
16472 clauses = c_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
16473 "#pragma acc data");
16474
16475 block = c_begin_omp_parallel ();
16476 add_stmt (c_parser_omp_structured_block (parser, if_p));
16477
16478 stmt = c_finish_oacc_data (loc, clauses, block);
16479
16480 return stmt;
16481 }
16482
16483 /* OpenACC 2.0:
16484 # pragma acc declare oacc-data-clause[optseq] new-line
16485 */
16486
16487 #define OACC_DECLARE_CLAUSE_MASK \
16488 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
16489 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
16490 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
16491 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
16492 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
16493 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
16494 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
16495 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT))
16496
16497 static void
16498 c_parser_oacc_declare (c_parser *parser)
16499 {
16500 location_t pragma_loc = c_parser_peek_token (parser)->location;
16501 tree clauses, stmt, t, decl;
16502
16503 bool error = false;
16504
16505 c_parser_consume_pragma (parser);
16506
16507 clauses = c_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
16508 "#pragma acc declare");
16509 if (!clauses)
16510 {
16511 error_at (pragma_loc,
16512 "no valid clauses specified in %<#pragma acc declare%>");
16513 return;
16514 }
16515
16516 for (t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
16517 {
16518 location_t loc = OMP_CLAUSE_LOCATION (t);
16519 decl = OMP_CLAUSE_DECL (t);
16520 if (!DECL_P (decl))
16521 {
16522 error_at (loc, "array section in %<#pragma acc declare%>");
16523 error = true;
16524 continue;
16525 }
16526
16527 switch (OMP_CLAUSE_MAP_KIND (t))
16528 {
16529 case GOMP_MAP_FIRSTPRIVATE_POINTER:
16530 case GOMP_MAP_ALLOC:
16531 case GOMP_MAP_TO:
16532 case GOMP_MAP_FORCE_DEVICEPTR:
16533 case GOMP_MAP_DEVICE_RESIDENT:
16534 break;
16535
16536 case GOMP_MAP_LINK:
16537 if (!global_bindings_p ()
16538 && (TREE_STATIC (decl)
16539 || !DECL_EXTERNAL (decl)))
16540 {
16541 error_at (loc,
16542 "%qD must be a global variable in "
16543 "%<#pragma acc declare link%>",
16544 decl);
16545 error = true;
16546 continue;
16547 }
16548 break;
16549
16550 default:
16551 if (global_bindings_p ())
16552 {
16553 error_at (loc, "invalid OpenACC clause at file scope");
16554 error = true;
16555 continue;
16556 }
16557 if (DECL_EXTERNAL (decl))
16558 {
16559 error_at (loc,
16560 "invalid use of %<extern%> variable %qD "
16561 "in %<#pragma acc declare%>", decl);
16562 error = true;
16563 continue;
16564 }
16565 else if (TREE_PUBLIC (decl))
16566 {
16567 error_at (loc,
16568 "invalid use of %<global%> variable %qD "
16569 "in %<#pragma acc declare%>", decl);
16570 error = true;
16571 continue;
16572 }
16573 break;
16574 }
16575
16576 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
16577 || lookup_attribute ("omp declare target link",
16578 DECL_ATTRIBUTES (decl)))
16579 {
16580 error_at (loc, "variable %qD used more than once with "
16581 "%<#pragma acc declare%>", decl);
16582 error = true;
16583 continue;
16584 }
16585
16586 if (!error)
16587 {
16588 tree id;
16589
16590 if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
16591 id = get_identifier ("omp declare target link");
16592 else
16593 id = get_identifier ("omp declare target");
16594
16595 DECL_ATTRIBUTES (decl)
16596 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
16597
16598 if (global_bindings_p ())
16599 {
16600 symtab_node *node = symtab_node::get (decl);
16601 if (node != NULL)
16602 {
16603 node->offloadable = 1;
16604 if (ENABLE_OFFLOADING)
16605 {
16606 g->have_offload = true;
16607 if (is_a <varpool_node *> (node))
16608 vec_safe_push (offload_vars, decl);
16609 }
16610 }
16611 }
16612 }
16613 }
16614
16615 if (error || global_bindings_p ())
16616 return;
16617
16618 stmt = make_node (OACC_DECLARE);
16619 TREE_TYPE (stmt) = void_type_node;
16620 OACC_DECLARE_CLAUSES (stmt) = clauses;
16621 SET_EXPR_LOCATION (stmt, pragma_loc);
16622
16623 add_stmt (stmt);
16624
16625 return;
16626 }
16627
16628 /* OpenACC 2.0:
16629 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
16630
16631 or
16632
16633 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
16634
16635
16636 LOC is the location of the #pragma token.
16637 */
16638
16639 #define OACC_ENTER_DATA_CLAUSE_MASK \
16640 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
16641 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
16642 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
16643 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
16644 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
16645 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
16646
16647 #define OACC_EXIT_DATA_CLAUSE_MASK \
16648 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
16649 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
16650 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
16651 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
16652 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DETACH) \
16653 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FINALIZE) \
16654 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
16655
16656 static void
16657 c_parser_oacc_enter_exit_data (c_parser *parser, bool enter)
16658 {
16659 location_t loc = c_parser_peek_token (parser)->location;
16660 tree clauses, stmt;
16661 const char *p = "";
16662
16663 c_parser_consume_pragma (parser);
16664
16665 if (c_parser_next_token_is (parser, CPP_NAME))
16666 {
16667 p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16668 c_parser_consume_token (parser);
16669 }
16670
16671 if (strcmp (p, "data") != 0)
16672 {
16673 error_at (loc, "expected %<data%> after %<#pragma acc %s%>",
16674 enter ? "enter" : "exit");
16675 parser->error = true;
16676 c_parser_skip_to_pragma_eol (parser);
16677 return;
16678 }
16679
16680 if (enter)
16681 clauses = c_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
16682 "#pragma acc enter data");
16683 else
16684 clauses = c_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
16685 "#pragma acc exit data");
16686
16687 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
16688 {
16689 error_at (loc, "%<#pragma acc %s data%> has no data movement clause",
16690 enter ? "enter" : "exit");
16691 return;
16692 }
16693
16694 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
16695 TREE_TYPE (stmt) = void_type_node;
16696 OMP_STANDALONE_CLAUSES (stmt) = clauses;
16697 SET_EXPR_LOCATION (stmt, loc);
16698 add_stmt (stmt);
16699 }
16700
16701
16702 /* OpenACC 2.0:
16703 # pragma acc host_data oacc-data-clause[optseq] new-line
16704 structured-block
16705 */
16706
16707 #define OACC_HOST_DATA_CLAUSE_MASK \
16708 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) \
16709 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
16710 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF_PRESENT) )
16711
16712 static tree
16713 c_parser_oacc_host_data (location_t loc, c_parser *parser, bool *if_p)
16714 {
16715 tree stmt, clauses, block;
16716
16717 clauses = c_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
16718 "#pragma acc host_data");
16719
16720 block = c_begin_omp_parallel ();
16721 add_stmt (c_parser_omp_structured_block (parser, if_p));
16722 stmt = c_finish_oacc_host_data (loc, clauses, block);
16723 return stmt;
16724 }
16725
16726
16727 /* OpenACC 2.0:
16728
16729 # pragma acc loop oacc-loop-clause[optseq] new-line
16730 structured-block
16731
16732 LOC is the location of the #pragma token.
16733 */
16734
16735 #define OACC_LOOP_CLAUSE_MASK \
16736 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
16737 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
16738 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
16739 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
16740 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
16741 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
16742 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
16743 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
16744 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
16745 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE) )
16746 static tree
16747 c_parser_oacc_loop (location_t loc, c_parser *parser, char *p_name,
16748 omp_clause_mask mask, tree *cclauses, bool *if_p)
16749 {
16750 bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
16751
16752 strcat (p_name, " loop");
16753 mask |= OACC_LOOP_CLAUSE_MASK;
16754
16755 tree clauses = c_parser_oacc_all_clauses (parser, mask, p_name,
16756 cclauses == NULL);
16757 if (cclauses)
16758 {
16759 clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
16760 if (*cclauses)
16761 *cclauses = c_finish_omp_clauses (*cclauses, C_ORT_ACC);
16762 if (clauses)
16763 clauses = c_finish_omp_clauses (clauses, C_ORT_ACC);
16764 }
16765
16766 tree block = c_begin_compound_stmt (true);
16767 tree stmt = c_parser_omp_for_loop (loc, parser, OACC_LOOP, clauses, NULL,
16768 if_p);
16769 block = c_end_compound_stmt (loc, block, true);
16770 add_stmt (block);
16771
16772 return stmt;
16773 }
16774
16775 /* OpenACC 2.0:
16776 # pragma acc kernels oacc-kernels-clause[optseq] new-line
16777 structured-block
16778
16779 or
16780
16781 # pragma acc parallel oacc-parallel-clause[optseq] new-line
16782 structured-block
16783
16784 OpenACC 2.6:
16785
16786 # pragma acc serial oacc-serial-clause[optseq] new-line
16787 structured-block
16788
16789 LOC is the location of the #pragma token.
16790 */
16791
16792 #define OACC_KERNELS_CLAUSE_MASK \
16793 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
16794 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
16795 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
16796 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
16797 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
16798 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
16799 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
16800 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
16801 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
16802 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NO_CREATE) \
16803 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
16804 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
16805 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
16806 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
16807 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
16808
16809 #define OACC_PARALLEL_CLAUSE_MASK \
16810 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
16811 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
16812 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
16813 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
16814 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
16815 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
16816 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
16817 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
16818 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
16819 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NO_CREATE) \
16820 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
16821 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
16822 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
16823 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
16824 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
16825 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
16826 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
16827 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
16828
16829 #define OACC_SERIAL_CLAUSE_MASK \
16830 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
16831 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
16832 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
16833 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
16834 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
16835 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
16836 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
16837 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
16838 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
16839 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NO_CREATE) \
16840 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
16841 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
16842 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
16843 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
16844 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
16845
16846 static tree
16847 c_parser_oacc_compute (location_t loc, c_parser *parser,
16848 enum pragma_kind p_kind, char *p_name, bool *if_p)
16849 {
16850 omp_clause_mask mask;
16851 enum tree_code code;
16852 switch (p_kind)
16853 {
16854 case PRAGMA_OACC_KERNELS:
16855 strcat (p_name, " kernels");
16856 mask = OACC_KERNELS_CLAUSE_MASK;
16857 code = OACC_KERNELS;
16858 break;
16859 case PRAGMA_OACC_PARALLEL:
16860 strcat (p_name, " parallel");
16861 mask = OACC_PARALLEL_CLAUSE_MASK;
16862 code = OACC_PARALLEL;
16863 break;
16864 case PRAGMA_OACC_SERIAL:
16865 strcat (p_name, " serial");
16866 mask = OACC_SERIAL_CLAUSE_MASK;
16867 code = OACC_SERIAL;
16868 break;
16869 default:
16870 gcc_unreachable ();
16871 }
16872
16873 if (c_parser_next_token_is (parser, CPP_NAME))
16874 {
16875 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16876 if (strcmp (p, "loop") == 0)
16877 {
16878 c_parser_consume_token (parser);
16879 tree block = c_begin_omp_parallel ();
16880 tree clauses;
16881 c_parser_oacc_loop (loc, parser, p_name, mask, &clauses, if_p);
16882 return c_finish_omp_construct (loc, code, block, clauses);
16883 }
16884 }
16885
16886 tree clauses = c_parser_oacc_all_clauses (parser, mask, p_name);
16887
16888 tree block = c_begin_omp_parallel ();
16889 add_stmt (c_parser_omp_structured_block (parser, if_p));
16890
16891 return c_finish_omp_construct (loc, code, block, clauses);
16892 }
16893
16894 /* OpenACC 2.0:
16895 # pragma acc routine oacc-routine-clause[optseq] new-line
16896 function-definition
16897
16898 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
16899 */
16900
16901 #define OACC_ROUTINE_CLAUSE_MASK \
16902 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
16903 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
16904 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
16905 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) )
16906
16907 /* Parse an OpenACC routine directive. For named directives, we apply
16908 immediately to the named function. For unnamed ones we then parse
16909 a declaration or definition, which must be for a function. */
16910
16911 static void
16912 c_parser_oacc_routine (c_parser *parser, enum pragma_context context)
16913 {
16914 gcc_checking_assert (context == pragma_external);
16915
16916 oacc_routine_data data;
16917 data.error_seen = false;
16918 data.fndecl_seen = false;
16919 data.clauses = NULL_TREE;
16920 data.loc = c_parser_peek_token (parser)->location;
16921
16922 c_parser_consume_pragma (parser);
16923
16924 /* Look for optional '( name )'. */
16925 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
16926 {
16927 c_parser_consume_token (parser); /* '(' */
16928
16929 tree decl = NULL_TREE;
16930 c_token *name_token = c_parser_peek_token (parser);
16931 location_t name_loc = name_token->location;
16932 if (name_token->type == CPP_NAME
16933 && (name_token->id_kind == C_ID_ID
16934 || name_token->id_kind == C_ID_TYPENAME))
16935 {
16936 decl = lookup_name (name_token->value);
16937 if (!decl)
16938 error_at (name_loc,
16939 "%qE has not been declared", name_token->value);
16940 c_parser_consume_token (parser);
16941 }
16942 else
16943 c_parser_error (parser, "expected function name");
16944
16945 if (!decl
16946 || !c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
16947 {
16948 c_parser_skip_to_pragma_eol (parser, false);
16949 return;
16950 }
16951
16952 data.clauses
16953 = c_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
16954 "#pragma acc routine");
16955 /* The clauses are in reverse order; fix that to make later diagnostic
16956 emission easier. */
16957 data.clauses = nreverse (data.clauses);
16958
16959 if (TREE_CODE (decl) != FUNCTION_DECL)
16960 {
16961 error_at (name_loc, "%qD does not refer to a function", decl);
16962 return;
16963 }
16964
16965 c_finish_oacc_routine (&data, decl, false);
16966 }
16967 else /* No optional '( name )'. */
16968 {
16969 data.clauses
16970 = c_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
16971 "#pragma acc routine");
16972 /* The clauses are in reverse order; fix that to make later diagnostic
16973 emission easier. */
16974 data.clauses = nreverse (data.clauses);
16975
16976 /* Emit a helpful diagnostic if there's another pragma following this
16977 one. Also don't allow a static assertion declaration, as in the
16978 following we'll just parse a *single* "declaration or function
16979 definition", and the static assertion counts an one. */
16980 if (c_parser_next_token_is (parser, CPP_PRAGMA)
16981 || c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
16982 {
16983 error_at (data.loc,
16984 "%<#pragma acc routine%> not immediately followed by"
16985 " function declaration or definition");
16986 /* ..., and then just keep going. */
16987 return;
16988 }
16989
16990 /* We only have to consider the pragma_external case here. */
16991 if (c_parser_next_token_is (parser, CPP_KEYWORD)
16992 && c_parser_peek_token (parser)->keyword == RID_EXTENSION)
16993 {
16994 int ext = disable_extension_diagnostics ();
16995 do
16996 c_parser_consume_token (parser);
16997 while (c_parser_next_token_is (parser, CPP_KEYWORD)
16998 && c_parser_peek_token (parser)->keyword == RID_EXTENSION);
16999 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
17000 NULL, vNULL, false, NULL, &data);
17001 restore_extension_diagnostics (ext);
17002 }
17003 else
17004 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
17005 NULL, vNULL, false, NULL, &data);
17006 }
17007 }
17008
17009 /* Finalize an OpenACC routine pragma, applying it to FNDECL.
17010 IS_DEFN is true if we're applying it to the definition. */
17011
17012 static void
17013 c_finish_oacc_routine (struct oacc_routine_data *data, tree fndecl,
17014 bool is_defn)
17015 {
17016 /* Keep going if we're in error reporting mode. */
17017 if (data->error_seen
17018 || fndecl == error_mark_node)
17019 return;
17020
17021 if (data->fndecl_seen)
17022 {
17023 error_at (data->loc,
17024 "%<#pragma acc routine%> not immediately followed by"
17025 " a single function declaration or definition");
17026 data->error_seen = true;
17027 return;
17028 }
17029 if (fndecl == NULL_TREE || TREE_CODE (fndecl) != FUNCTION_DECL)
17030 {
17031 error_at (data->loc,
17032 "%<#pragma acc routine%> not immediately followed by"
17033 " function declaration or definition");
17034 data->error_seen = true;
17035 return;
17036 }
17037
17038 int compatible
17039 = oacc_verify_routine_clauses (fndecl, &data->clauses, data->loc,
17040 "#pragma acc routine");
17041 if (compatible < 0)
17042 {
17043 data->error_seen = true;
17044 return;
17045 }
17046 if (compatible > 0)
17047 {
17048 }
17049 else
17050 {
17051 if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
17052 {
17053 error_at (data->loc,
17054 TREE_USED (fndecl)
17055 ? G_("%<#pragma acc routine%> must be applied before use")
17056 : G_("%<#pragma acc routine%> must be applied before"
17057 " definition"));
17058 data->error_seen = true;
17059 return;
17060 }
17061
17062 /* Set the routine's level of parallelism. */
17063 tree dims = oacc_build_routine_dims (data->clauses);
17064 oacc_replace_fn_attrib (fndecl, dims);
17065
17066 /* Add an "omp declare target" attribute. */
17067 DECL_ATTRIBUTES (fndecl)
17068 = tree_cons (get_identifier ("omp declare target"),
17069 data->clauses, DECL_ATTRIBUTES (fndecl));
17070 }
17071
17072 /* Remember that we've used this "#pragma acc routine". */
17073 data->fndecl_seen = true;
17074 }
17075
17076 /* OpenACC 2.0:
17077 # pragma acc update oacc-update-clause[optseq] new-line
17078 */
17079
17080 #define OACC_UPDATE_CLAUSE_MASK \
17081 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
17082 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
17083 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
17084 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
17085 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF_PRESENT) \
17086 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
17087
17088 static void
17089 c_parser_oacc_update (c_parser *parser)
17090 {
17091 location_t loc = c_parser_peek_token (parser)->location;
17092
17093 c_parser_consume_pragma (parser);
17094
17095 tree clauses = c_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
17096 "#pragma acc update");
17097 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
17098 {
17099 error_at (loc,
17100 "%<#pragma acc update%> must contain at least one "
17101 "%<device%> or %<host%> or %<self%> clause");
17102 return;
17103 }
17104
17105 if (parser->error)
17106 return;
17107
17108 tree stmt = make_node (OACC_UPDATE);
17109 TREE_TYPE (stmt) = void_type_node;
17110 OACC_UPDATE_CLAUSES (stmt) = clauses;
17111 SET_EXPR_LOCATION (stmt, loc);
17112 add_stmt (stmt);
17113 }
17114
17115 /* OpenACC 2.0:
17116 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
17117
17118 LOC is the location of the #pragma token.
17119 */
17120
17121 #define OACC_WAIT_CLAUSE_MASK \
17122 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) )
17123
17124 static tree
17125 c_parser_oacc_wait (location_t loc, c_parser *parser, char *p_name)
17126 {
17127 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
17128
17129 if (c_parser_peek_token (parser)->type == CPP_OPEN_PAREN)
17130 list = c_parser_oacc_wait_list (parser, loc, list);
17131
17132 strcpy (p_name, " wait");
17133 clauses = c_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK, p_name);
17134 stmt = c_finish_oacc_wait (loc, list, clauses);
17135 add_stmt (stmt);
17136
17137 return stmt;
17138 }
17139
17140 /* OpenMP 2.5:
17141 # pragma omp atomic new-line
17142 expression-stmt
17143
17144 expression-stmt:
17145 x binop= expr | x++ | ++x | x-- | --x
17146 binop:
17147 +, *, -, /, &, ^, |, <<, >>
17148
17149 where x is an lvalue expression with scalar type.
17150
17151 OpenMP 3.1:
17152 # pragma omp atomic new-line
17153 update-stmt
17154
17155 # pragma omp atomic read new-line
17156 read-stmt
17157
17158 # pragma omp atomic write new-line
17159 write-stmt
17160
17161 # pragma omp atomic update new-line
17162 update-stmt
17163
17164 # pragma omp atomic capture new-line
17165 capture-stmt
17166
17167 # pragma omp atomic capture new-line
17168 capture-block
17169
17170 read-stmt:
17171 v = x
17172 write-stmt:
17173 x = expr
17174 update-stmt:
17175 expression-stmt | x = x binop expr
17176 capture-stmt:
17177 v = expression-stmt
17178 capture-block:
17179 { v = x; update-stmt; } | { update-stmt; v = x; }
17180
17181 OpenMP 4.0:
17182 update-stmt:
17183 expression-stmt | x = x binop expr | x = expr binop x
17184 capture-stmt:
17185 v = update-stmt
17186 capture-block:
17187 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
17188
17189 where x and v are lvalue expressions with scalar type.
17190
17191 LOC is the location of the #pragma token. */
17192
17193 static void
17194 c_parser_omp_atomic (location_t loc, c_parser *parser)
17195 {
17196 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE;
17197 tree lhs1 = NULL_TREE, rhs1 = NULL_TREE;
17198 tree stmt, orig_lhs, unfolded_lhs = NULL_TREE, unfolded_lhs1 = NULL_TREE;
17199 enum tree_code code = ERROR_MARK, opcode = NOP_EXPR;
17200 enum omp_memory_order memory_order = OMP_MEMORY_ORDER_UNSPECIFIED;
17201 struct c_expr expr;
17202 location_t eloc;
17203 bool structured_block = false;
17204 bool swapped = false;
17205 bool non_lvalue_p;
17206 bool first = true;
17207 tree clauses = NULL_TREE;
17208
17209 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
17210 {
17211 if (!first && c_parser_next_token_is (parser, CPP_COMMA))
17212 c_parser_consume_token (parser);
17213
17214 first = false;
17215
17216 if (c_parser_next_token_is (parser, CPP_NAME))
17217 {
17218 const char *p
17219 = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
17220 location_t cloc = c_parser_peek_token (parser)->location;
17221 enum tree_code new_code = ERROR_MARK;
17222 enum omp_memory_order new_memory_order
17223 = OMP_MEMORY_ORDER_UNSPECIFIED;
17224
17225 if (!strcmp (p, "read"))
17226 new_code = OMP_ATOMIC_READ;
17227 else if (!strcmp (p, "write"))
17228 new_code = NOP_EXPR;
17229 else if (!strcmp (p, "update"))
17230 new_code = OMP_ATOMIC;
17231 else if (!strcmp (p, "capture"))
17232 new_code = OMP_ATOMIC_CAPTURE_NEW;
17233 else if (!strcmp (p, "seq_cst"))
17234 new_memory_order = OMP_MEMORY_ORDER_SEQ_CST;
17235 else if (!strcmp (p, "acq_rel"))
17236 new_memory_order = OMP_MEMORY_ORDER_ACQ_REL;
17237 else if (!strcmp (p, "release"))
17238 new_memory_order = OMP_MEMORY_ORDER_RELEASE;
17239 else if (!strcmp (p, "acquire"))
17240 new_memory_order = OMP_MEMORY_ORDER_ACQUIRE;
17241 else if (!strcmp (p, "relaxed"))
17242 new_memory_order = OMP_MEMORY_ORDER_RELAXED;
17243 else if (!strcmp (p, "hint"))
17244 {
17245 c_parser_consume_token (parser);
17246 clauses = c_parser_omp_clause_hint (parser, clauses);
17247 continue;
17248 }
17249 else
17250 {
17251 p = NULL;
17252 error_at (cloc, "expected %<read%>, %<write%>, %<update%>, "
17253 "%<capture%>, %<seq_cst%>, %<acq_rel%>, "
17254 "%<release%>, %<relaxed%> or %<hint%> clause");
17255 }
17256 if (p)
17257 {
17258 if (new_code != ERROR_MARK)
17259 {
17260 if (code != ERROR_MARK)
17261 error_at (cloc, "too many atomic clauses");
17262 else
17263 code = new_code;
17264 }
17265 else if (new_memory_order != OMP_MEMORY_ORDER_UNSPECIFIED)
17266 {
17267 if (memory_order != OMP_MEMORY_ORDER_UNSPECIFIED)
17268 error_at (cloc, "too many memory order clauses");
17269 else
17270 memory_order = new_memory_order;
17271 }
17272 c_parser_consume_token (parser);
17273 continue;
17274 }
17275 }
17276 break;
17277 }
17278 c_parser_skip_to_pragma_eol (parser);
17279
17280 if (code == ERROR_MARK)
17281 code = OMP_ATOMIC;
17282 if (memory_order == OMP_MEMORY_ORDER_UNSPECIFIED)
17283 {
17284 omp_requires_mask
17285 = (enum omp_requires) (omp_requires_mask
17286 | OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER_USED);
17287 switch ((enum omp_memory_order)
17288 (omp_requires_mask & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER))
17289 {
17290 case OMP_MEMORY_ORDER_UNSPECIFIED:
17291 case OMP_MEMORY_ORDER_RELAXED:
17292 memory_order = OMP_MEMORY_ORDER_RELAXED;
17293 break;
17294 case OMP_MEMORY_ORDER_SEQ_CST:
17295 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
17296 break;
17297 case OMP_MEMORY_ORDER_ACQ_REL:
17298 switch (code)
17299 {
17300 case OMP_ATOMIC_READ:
17301 memory_order = OMP_MEMORY_ORDER_ACQUIRE;
17302 break;
17303 case NOP_EXPR: /* atomic write */
17304 case OMP_ATOMIC:
17305 memory_order = OMP_MEMORY_ORDER_RELEASE;
17306 break;
17307 default:
17308 memory_order = OMP_MEMORY_ORDER_ACQ_REL;
17309 break;
17310 }
17311 break;
17312 default:
17313 gcc_unreachable ();
17314 }
17315 }
17316 else
17317 switch (code)
17318 {
17319 case OMP_ATOMIC_READ:
17320 if (memory_order == OMP_MEMORY_ORDER_ACQ_REL
17321 || memory_order == OMP_MEMORY_ORDER_RELEASE)
17322 {
17323 error_at (loc, "%<#pragma omp atomic read%> incompatible with "
17324 "%<acq_rel%> or %<release%> clauses");
17325 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
17326 }
17327 break;
17328 case NOP_EXPR: /* atomic write */
17329 if (memory_order == OMP_MEMORY_ORDER_ACQ_REL
17330 || memory_order == OMP_MEMORY_ORDER_ACQUIRE)
17331 {
17332 error_at (loc, "%<#pragma omp atomic write%> incompatible with "
17333 "%<acq_rel%> or %<acquire%> clauses");
17334 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
17335 }
17336 break;
17337 case OMP_ATOMIC:
17338 if (memory_order == OMP_MEMORY_ORDER_ACQ_REL
17339 || memory_order == OMP_MEMORY_ORDER_ACQUIRE)
17340 {
17341 error_at (loc, "%<#pragma omp atomic update%> incompatible with "
17342 "%<acq_rel%> or %<acquire%> clauses");
17343 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
17344 }
17345 break;
17346 default:
17347 break;
17348 }
17349
17350 switch (code)
17351 {
17352 case OMP_ATOMIC_READ:
17353 case NOP_EXPR: /* atomic write */
17354 v = c_parser_cast_expression (parser, NULL).value;
17355 non_lvalue_p = !lvalue_p (v);
17356 v = c_fully_fold (v, false, NULL, true);
17357 if (v == error_mark_node)
17358 goto saw_error;
17359 if (non_lvalue_p)
17360 v = non_lvalue (v);
17361 loc = c_parser_peek_token (parser)->location;
17362 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
17363 goto saw_error;
17364 if (code == NOP_EXPR)
17365 {
17366 lhs = c_parser_expression (parser).value;
17367 lhs = c_fully_fold (lhs, false, NULL);
17368 if (lhs == error_mark_node)
17369 goto saw_error;
17370 }
17371 else
17372 {
17373 lhs = c_parser_cast_expression (parser, NULL).value;
17374 non_lvalue_p = !lvalue_p (lhs);
17375 lhs = c_fully_fold (lhs, false, NULL, true);
17376 if (lhs == error_mark_node)
17377 goto saw_error;
17378 if (non_lvalue_p)
17379 lhs = non_lvalue (lhs);
17380 }
17381 if (code == NOP_EXPR)
17382 {
17383 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
17384 opcode. */
17385 code = OMP_ATOMIC;
17386 rhs = lhs;
17387 lhs = v;
17388 v = NULL_TREE;
17389 }
17390 goto done;
17391 case OMP_ATOMIC_CAPTURE_NEW:
17392 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
17393 {
17394 c_parser_consume_token (parser);
17395 structured_block = true;
17396 }
17397 else
17398 {
17399 v = c_parser_cast_expression (parser, NULL).value;
17400 non_lvalue_p = !lvalue_p (v);
17401 v = c_fully_fold (v, false, NULL, true);
17402 if (v == error_mark_node)
17403 goto saw_error;
17404 if (non_lvalue_p)
17405 v = non_lvalue (v);
17406 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
17407 goto saw_error;
17408 }
17409 break;
17410 default:
17411 break;
17412 }
17413
17414 /* For structured_block case we don't know yet whether
17415 old or new x should be captured. */
17416 restart:
17417 eloc = c_parser_peek_token (parser)->location;
17418 expr = c_parser_cast_expression (parser, NULL);
17419 lhs = expr.value;
17420 expr = default_function_array_conversion (eloc, expr);
17421 unfolded_lhs = expr.value;
17422 lhs = c_fully_fold (lhs, false, NULL, true);
17423 orig_lhs = lhs;
17424 switch (TREE_CODE (lhs))
17425 {
17426 case ERROR_MARK:
17427 saw_error:
17428 c_parser_skip_to_end_of_block_or_statement (parser);
17429 if (structured_block)
17430 {
17431 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
17432 c_parser_consume_token (parser);
17433 else if (code == OMP_ATOMIC_CAPTURE_NEW)
17434 {
17435 c_parser_skip_to_end_of_block_or_statement (parser);
17436 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
17437 c_parser_consume_token (parser);
17438 }
17439 }
17440 return;
17441
17442 case POSTINCREMENT_EXPR:
17443 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
17444 code = OMP_ATOMIC_CAPTURE_OLD;
17445 /* FALLTHROUGH */
17446 case PREINCREMENT_EXPR:
17447 lhs = TREE_OPERAND (lhs, 0);
17448 unfolded_lhs = NULL_TREE;
17449 opcode = PLUS_EXPR;
17450 rhs = integer_one_node;
17451 break;
17452
17453 case POSTDECREMENT_EXPR:
17454 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
17455 code = OMP_ATOMIC_CAPTURE_OLD;
17456 /* FALLTHROUGH */
17457 case PREDECREMENT_EXPR:
17458 lhs = TREE_OPERAND (lhs, 0);
17459 unfolded_lhs = NULL_TREE;
17460 opcode = MINUS_EXPR;
17461 rhs = integer_one_node;
17462 break;
17463
17464 case COMPOUND_EXPR:
17465 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
17466 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
17467 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
17468 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
17469 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
17470 (TREE_OPERAND (lhs, 1), 0), 0)))
17471 == BOOLEAN_TYPE)
17472 /* Undo effects of boolean_increment for post {in,de}crement. */
17473 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
17474 /* FALLTHRU */
17475 case MODIFY_EXPR:
17476 if (TREE_CODE (lhs) == MODIFY_EXPR
17477 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
17478 {
17479 /* Undo effects of boolean_increment. */
17480 if (integer_onep (TREE_OPERAND (lhs, 1)))
17481 {
17482 /* This is pre or post increment. */
17483 rhs = TREE_OPERAND (lhs, 1);
17484 lhs = TREE_OPERAND (lhs, 0);
17485 unfolded_lhs = NULL_TREE;
17486 opcode = NOP_EXPR;
17487 if (code == OMP_ATOMIC_CAPTURE_NEW
17488 && !structured_block
17489 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
17490 code = OMP_ATOMIC_CAPTURE_OLD;
17491 break;
17492 }
17493 if (TREE_CODE (TREE_OPERAND (lhs, 1)) == TRUTH_NOT_EXPR
17494 && TREE_OPERAND (lhs, 0)
17495 == TREE_OPERAND (TREE_OPERAND (lhs, 1), 0))
17496 {
17497 /* This is pre or post decrement. */
17498 rhs = TREE_OPERAND (lhs, 1);
17499 lhs = TREE_OPERAND (lhs, 0);
17500 unfolded_lhs = NULL_TREE;
17501 opcode = NOP_EXPR;
17502 if (code == OMP_ATOMIC_CAPTURE_NEW
17503 && !structured_block
17504 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
17505 code = OMP_ATOMIC_CAPTURE_OLD;
17506 break;
17507 }
17508 }
17509 /* FALLTHRU */
17510 default:
17511 if (!lvalue_p (unfolded_lhs))
17512 lhs = non_lvalue (lhs);
17513 switch (c_parser_peek_token (parser)->type)
17514 {
17515 case CPP_MULT_EQ:
17516 opcode = MULT_EXPR;
17517 break;
17518 case CPP_DIV_EQ:
17519 opcode = TRUNC_DIV_EXPR;
17520 break;
17521 case CPP_PLUS_EQ:
17522 opcode = PLUS_EXPR;
17523 break;
17524 case CPP_MINUS_EQ:
17525 opcode = MINUS_EXPR;
17526 break;
17527 case CPP_LSHIFT_EQ:
17528 opcode = LSHIFT_EXPR;
17529 break;
17530 case CPP_RSHIFT_EQ:
17531 opcode = RSHIFT_EXPR;
17532 break;
17533 case CPP_AND_EQ:
17534 opcode = BIT_AND_EXPR;
17535 break;
17536 case CPP_OR_EQ:
17537 opcode = BIT_IOR_EXPR;
17538 break;
17539 case CPP_XOR_EQ:
17540 opcode = BIT_XOR_EXPR;
17541 break;
17542 case CPP_EQ:
17543 c_parser_consume_token (parser);
17544 eloc = c_parser_peek_token (parser)->location;
17545 expr = c_parser_expr_no_commas (parser, NULL, unfolded_lhs);
17546 rhs1 = expr.value;
17547 switch (TREE_CODE (rhs1))
17548 {
17549 case MULT_EXPR:
17550 case TRUNC_DIV_EXPR:
17551 case RDIV_EXPR:
17552 case PLUS_EXPR:
17553 case MINUS_EXPR:
17554 case LSHIFT_EXPR:
17555 case RSHIFT_EXPR:
17556 case BIT_AND_EXPR:
17557 case BIT_IOR_EXPR:
17558 case BIT_XOR_EXPR:
17559 if (c_tree_equal (TREE_OPERAND (rhs1, 0), unfolded_lhs))
17560 {
17561 opcode = TREE_CODE (rhs1);
17562 rhs = c_fully_fold (TREE_OPERAND (rhs1, 1), false, NULL,
17563 true);
17564 rhs1 = c_fully_fold (TREE_OPERAND (rhs1, 0), false, NULL,
17565 true);
17566 goto stmt_done;
17567 }
17568 if (c_tree_equal (TREE_OPERAND (rhs1, 1), unfolded_lhs))
17569 {
17570 opcode = TREE_CODE (rhs1);
17571 rhs = c_fully_fold (TREE_OPERAND (rhs1, 0), false, NULL,
17572 true);
17573 rhs1 = c_fully_fold (TREE_OPERAND (rhs1, 1), false, NULL,
17574 true);
17575 swapped = !commutative_tree_code (opcode);
17576 goto stmt_done;
17577 }
17578 break;
17579 case ERROR_MARK:
17580 goto saw_error;
17581 default:
17582 break;
17583 }
17584 if (c_parser_peek_token (parser)->type == CPP_SEMICOLON)
17585 {
17586 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
17587 {
17588 code = OMP_ATOMIC_CAPTURE_OLD;
17589 v = lhs;
17590 lhs = NULL_TREE;
17591 expr = default_function_array_read_conversion (eloc, expr);
17592 unfolded_lhs1 = expr.value;
17593 lhs1 = c_fully_fold (unfolded_lhs1, false, NULL, true);
17594 rhs1 = NULL_TREE;
17595 c_parser_consume_token (parser);
17596 goto restart;
17597 }
17598 if (structured_block)
17599 {
17600 opcode = NOP_EXPR;
17601 expr = default_function_array_read_conversion (eloc, expr);
17602 rhs = c_fully_fold (expr.value, false, NULL, true);
17603 rhs1 = NULL_TREE;
17604 goto stmt_done;
17605 }
17606 }
17607 c_parser_error (parser, "invalid form of %<#pragma omp atomic%>");
17608 goto saw_error;
17609 default:
17610 c_parser_error (parser,
17611 "invalid operator for %<#pragma omp atomic%>");
17612 goto saw_error;
17613 }
17614
17615 /* Arrange to pass the location of the assignment operator to
17616 c_finish_omp_atomic. */
17617 loc = c_parser_peek_token (parser)->location;
17618 c_parser_consume_token (parser);
17619 eloc = c_parser_peek_token (parser)->location;
17620 expr = c_parser_expression (parser);
17621 expr = default_function_array_read_conversion (eloc, expr);
17622 rhs = expr.value;
17623 rhs = c_fully_fold (rhs, false, NULL, true);
17624 break;
17625 }
17626 stmt_done:
17627 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
17628 {
17629 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
17630 goto saw_error;
17631 v = c_parser_cast_expression (parser, NULL).value;
17632 non_lvalue_p = !lvalue_p (v);
17633 v = c_fully_fold (v, false, NULL, true);
17634 if (v == error_mark_node)
17635 goto saw_error;
17636 if (non_lvalue_p)
17637 v = non_lvalue (v);
17638 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
17639 goto saw_error;
17640 eloc = c_parser_peek_token (parser)->location;
17641 expr = c_parser_cast_expression (parser, NULL);
17642 lhs1 = expr.value;
17643 expr = default_function_array_read_conversion (eloc, expr);
17644 unfolded_lhs1 = expr.value;
17645 lhs1 = c_fully_fold (lhs1, false, NULL, true);
17646 if (lhs1 == error_mark_node)
17647 goto saw_error;
17648 if (!lvalue_p (unfolded_lhs1))
17649 lhs1 = non_lvalue (lhs1);
17650 }
17651 if (structured_block)
17652 {
17653 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
17654 c_parser_require (parser, CPP_CLOSE_BRACE, "expected %<}%>");
17655 }
17656 done:
17657 if (unfolded_lhs && unfolded_lhs1
17658 && !c_tree_equal (unfolded_lhs, unfolded_lhs1))
17659 {
17660 error ("%<#pragma omp atomic capture%> uses two different "
17661 "expressions for memory");
17662 stmt = error_mark_node;
17663 }
17664 else
17665 stmt = c_finish_omp_atomic (loc, code, opcode, lhs, rhs, v, lhs1, rhs1,
17666 swapped, memory_order);
17667 if (stmt != error_mark_node)
17668 add_stmt (stmt);
17669
17670 if (!structured_block)
17671 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
17672 }
17673
17674
17675 /* OpenMP 2.5:
17676 # pragma omp barrier new-line
17677 */
17678
17679 static void
17680 c_parser_omp_barrier (c_parser *parser)
17681 {
17682 location_t loc = c_parser_peek_token (parser)->location;
17683 c_parser_consume_pragma (parser);
17684 c_parser_skip_to_pragma_eol (parser);
17685
17686 c_finish_omp_barrier (loc);
17687 }
17688
17689 /* OpenMP 2.5:
17690 # pragma omp critical [(name)] new-line
17691 structured-block
17692
17693 OpenMP 4.5:
17694 # pragma omp critical [(name) [hint(expression)]] new-line
17695
17696 LOC is the location of the #pragma itself. */
17697
17698 #define OMP_CRITICAL_CLAUSE_MASK \
17699 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
17700
17701 static tree
17702 c_parser_omp_critical (location_t loc, c_parser *parser, bool *if_p)
17703 {
17704 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
17705
17706 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
17707 {
17708 c_parser_consume_token (parser);
17709 if (c_parser_next_token_is (parser, CPP_NAME))
17710 {
17711 name = c_parser_peek_token (parser)->value;
17712 c_parser_consume_token (parser);
17713 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
17714 }
17715 else
17716 c_parser_error (parser, "expected identifier");
17717
17718 if (c_parser_next_token_is (parser, CPP_COMMA)
17719 && c_parser_peek_2nd_token (parser)->type == CPP_NAME)
17720 c_parser_consume_token (parser);
17721
17722 clauses = c_parser_omp_all_clauses (parser,
17723 OMP_CRITICAL_CLAUSE_MASK,
17724 "#pragma omp critical");
17725 }
17726 else
17727 {
17728 if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
17729 c_parser_error (parser, "expected %<(%> or end of line");
17730 c_parser_skip_to_pragma_eol (parser);
17731 }
17732
17733 stmt = c_parser_omp_structured_block (parser, if_p);
17734 return c_finish_omp_critical (loc, stmt, name, clauses);
17735 }
17736
17737 /* OpenMP 5.0:
17738 # pragma omp depobj ( depobj ) depobj-clause new-line
17739
17740 depobj-clause:
17741 depend (dependence-type : locator)
17742 destroy
17743 update (dependence-type)
17744
17745 dependence-type:
17746 in
17747 out
17748 inout
17749 mutexinout */
17750
17751 static void
17752 c_parser_omp_depobj (c_parser *parser)
17753 {
17754 location_t loc = c_parser_peek_token (parser)->location;
17755 c_parser_consume_pragma (parser);
17756 matching_parens parens;
17757 if (!parens.require_open (parser))
17758 {
17759 c_parser_skip_to_pragma_eol (parser);
17760 return;
17761 }
17762
17763 tree depobj = c_parser_expr_no_commas (parser, NULL).value;
17764 if (depobj != error_mark_node)
17765 {
17766 if (!lvalue_p (depobj))
17767 {
17768 error_at (EXPR_LOC_OR_LOC (depobj, loc),
17769 "%<depobj%> expression is not lvalue expression");
17770 depobj = error_mark_node;
17771 }
17772 else
17773 {
17774 tree addr = build_unary_op (EXPR_LOC_OR_LOC (depobj, loc), ADDR_EXPR,
17775 depobj, false);
17776 if (addr == error_mark_node)
17777 depobj = error_mark_node;
17778 else
17779 depobj = build_indirect_ref (EXPR_LOC_OR_LOC (depobj, loc),
17780 addr, RO_UNARY_STAR);
17781 }
17782 }
17783
17784 parens.skip_until_found_close (parser);
17785 tree clause = NULL_TREE;
17786 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_SOURCE;
17787 location_t c_loc = c_parser_peek_token (parser)->location;
17788 if (c_parser_next_token_is (parser, CPP_NAME))
17789 {
17790 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
17791
17792 c_parser_consume_token (parser);
17793 if (!strcmp ("depend", p))
17794 {
17795 clause = c_parser_omp_clause_depend (parser, NULL_TREE);
17796 clause = c_finish_omp_clauses (clause, C_ORT_OMP);
17797 if (!clause)
17798 clause = error_mark_node;
17799 }
17800 else if (!strcmp ("destroy", p))
17801 kind = OMP_CLAUSE_DEPEND_LAST;
17802 else if (!strcmp ("update", p))
17803 {
17804 matching_parens c_parens;
17805 if (c_parens.require_open (parser))
17806 {
17807 location_t c2_loc = c_parser_peek_token (parser)->location;
17808 if (c_parser_next_token_is (parser, CPP_NAME))
17809 {
17810 const char *p2
17811 = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
17812
17813 c_parser_consume_token (parser);
17814 if (!strcmp ("in", p2))
17815 kind = OMP_CLAUSE_DEPEND_IN;
17816 else if (!strcmp ("out", p2))
17817 kind = OMP_CLAUSE_DEPEND_OUT;
17818 else if (!strcmp ("inout", p2))
17819 kind = OMP_CLAUSE_DEPEND_INOUT;
17820 else if (!strcmp ("mutexinoutset", p2))
17821 kind = OMP_CLAUSE_DEPEND_MUTEXINOUTSET;
17822 }
17823 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
17824 {
17825 clause = error_mark_node;
17826 error_at (c2_loc, "expected %<in%>, %<out%>, %<inout%> or "
17827 "%<mutexinoutset%>");
17828 }
17829 c_parens.skip_until_found_close (parser);
17830 }
17831 else
17832 clause = error_mark_node;
17833 }
17834 }
17835 if (!clause && kind == OMP_CLAUSE_DEPEND_SOURCE)
17836 {
17837 clause = error_mark_node;
17838 error_at (c_loc, "expected %<depend%>, %<destroy%> or %<update%> clause");
17839 }
17840 c_parser_skip_to_pragma_eol (parser);
17841
17842 c_finish_omp_depobj (loc, depobj, kind, clause);
17843 }
17844
17845
17846 /* OpenMP 2.5:
17847 # pragma omp flush flush-vars[opt] new-line
17848
17849 flush-vars:
17850 ( variable-list )
17851
17852 OpenMP 5.0:
17853 # pragma omp flush memory-order-clause new-line */
17854
17855 static void
17856 c_parser_omp_flush (c_parser *parser)
17857 {
17858 location_t loc = c_parser_peek_token (parser)->location;
17859 c_parser_consume_pragma (parser);
17860 enum memmodel mo = MEMMODEL_LAST;
17861 if (c_parser_next_token_is (parser, CPP_NAME))
17862 {
17863 const char *p
17864 = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
17865
17866 if (!strcmp (p, "acq_rel"))
17867 mo = MEMMODEL_ACQ_REL;
17868 else if (!strcmp (p, "release"))
17869 mo = MEMMODEL_RELEASE;
17870 else if (!strcmp (p, "acquire"))
17871 mo = MEMMODEL_ACQUIRE;
17872 else
17873 error_at (c_parser_peek_token (parser)->location,
17874 "expected %<acq_rel%>, %<release%> or %<acquire%>");
17875 c_parser_consume_token (parser);
17876 }
17877 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
17878 {
17879 if (mo != MEMMODEL_LAST)
17880 error_at (c_parser_peek_token (parser)->location,
17881 "%<flush%> list specified together with memory order "
17882 "clause");
17883 c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
17884 }
17885 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
17886 c_parser_error (parser, "expected %<(%> or end of line");
17887 c_parser_skip_to_pragma_eol (parser);
17888
17889 c_finish_omp_flush (loc, mo);
17890 }
17891
17892 /* OpenMP 5.0:
17893
17894 scan-loop-body:
17895 { structured-block scan-directive structured-block } */
17896
17897 static void
17898 c_parser_omp_scan_loop_body (c_parser *parser, bool open_brace_parsed)
17899 {
17900 tree substmt;
17901 location_t loc;
17902 tree clauses = NULL_TREE;
17903
17904 loc = c_parser_peek_token (parser)->location;
17905 if (!open_brace_parsed
17906 && !c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
17907 {
17908 /* Avoid skipping until the end of the block. */
17909 parser->error = false;
17910 return;
17911 }
17912
17913 substmt = c_parser_omp_structured_block (parser, NULL);
17914 substmt = build2 (OMP_SCAN, void_type_node, substmt, NULL_TREE);
17915 SET_EXPR_LOCATION (substmt, loc);
17916 add_stmt (substmt);
17917
17918 loc = c_parser_peek_token (parser)->location;
17919 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_OMP_SCAN)
17920 {
17921 enum omp_clause_code clause = OMP_CLAUSE_ERROR;
17922
17923 c_parser_consume_pragma (parser);
17924
17925 if (c_parser_next_token_is (parser, CPP_NAME))
17926 {
17927 const char *p
17928 = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
17929 if (strcmp (p, "inclusive") == 0)
17930 clause = OMP_CLAUSE_INCLUSIVE;
17931 else if (strcmp (p, "exclusive") == 0)
17932 clause = OMP_CLAUSE_EXCLUSIVE;
17933 }
17934 if (clause != OMP_CLAUSE_ERROR)
17935 {
17936 c_parser_consume_token (parser);
17937 clauses = c_parser_omp_var_list_parens (parser, clause, NULL_TREE);
17938 }
17939 else
17940 c_parser_error (parser, "expected %<inclusive%> or "
17941 "%<exclusive%> clause");
17942 c_parser_skip_to_pragma_eol (parser);
17943 }
17944 else
17945 error ("expected %<#pragma omp scan%>");
17946
17947 clauses = c_finish_omp_clauses (clauses, C_ORT_OMP);
17948 substmt = c_parser_omp_structured_block (parser, NULL);
17949 substmt = build2 (OMP_SCAN, void_type_node, substmt, clauses);
17950 SET_EXPR_LOCATION (substmt, loc);
17951 add_stmt (substmt);
17952
17953 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE,
17954 "expected %<}%>");
17955 }
17956
17957 /* Parse the restricted form of loop statements allowed by OpenACC and OpenMP.
17958 The real trick here is to determine the loop control variable early
17959 so that we can push a new decl if necessary to make it private.
17960 LOC is the location of the "acc" or "omp" in "#pragma acc" or "#pragma omp",
17961 respectively. */
17962
17963 static tree
17964 c_parser_omp_for_loop (location_t loc, c_parser *parser, enum tree_code code,
17965 tree clauses, tree *cclauses, bool *if_p)
17966 {
17967 tree decl, cond, incr, save_break, save_cont, body, init, stmt, cl;
17968 tree declv, condv, incrv, initv, ret = NULL_TREE;
17969 tree pre_body = NULL_TREE, this_pre_body;
17970 tree ordered_cl = NULL_TREE;
17971 bool fail = false, open_brace_parsed = false;
17972 int i, collapse = 1, ordered = 0, count, nbraces = 0;
17973 location_t for_loc;
17974 bool tiling = false;
17975 bool inscan = false;
17976 vec<tree, va_gc> *for_block = make_tree_vector ();
17977
17978 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
17979 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
17980 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
17981 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_TILE)
17982 {
17983 tiling = true;
17984 collapse = list_length (OMP_CLAUSE_TILE_LIST (cl));
17985 }
17986 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
17987 && OMP_CLAUSE_ORDERED_EXPR (cl))
17988 {
17989 ordered_cl = cl;
17990 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
17991 }
17992 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_REDUCTION
17993 && OMP_CLAUSE_REDUCTION_INSCAN (cl)
17994 && (code == OMP_SIMD || code == OMP_FOR))
17995 inscan = true;
17996
17997 if (ordered && ordered < collapse)
17998 {
17999 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
18000 "%<ordered%> clause parameter is less than %<collapse%>");
18001 OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
18002 = build_int_cst (NULL_TREE, collapse);
18003 ordered = collapse;
18004 }
18005 if (ordered)
18006 {
18007 for (tree *pc = &clauses; *pc; )
18008 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
18009 {
18010 error_at (OMP_CLAUSE_LOCATION (*pc),
18011 "%<linear%> clause may not be specified together "
18012 "with %<ordered%> clause with a parameter");
18013 *pc = OMP_CLAUSE_CHAIN (*pc);
18014 }
18015 else
18016 pc = &OMP_CLAUSE_CHAIN (*pc);
18017 }
18018
18019 gcc_assert (tiling || (collapse >= 1 && ordered >= 0));
18020 count = ordered ? ordered : collapse;
18021
18022 declv = make_tree_vec (count);
18023 initv = make_tree_vec (count);
18024 condv = make_tree_vec (count);
18025 incrv = make_tree_vec (count);
18026
18027 if (!c_parser_next_token_is_keyword (parser, RID_FOR))
18028 {
18029 c_parser_error (parser, "for statement expected");
18030 return NULL;
18031 }
18032 for_loc = c_parser_peek_token (parser)->location;
18033 c_parser_consume_token (parser);
18034
18035 for (i = 0; i < count; i++)
18036 {
18037 int bracecount = 0;
18038
18039 matching_parens parens;
18040 if (!parens.require_open (parser))
18041 goto pop_scopes;
18042
18043 /* Parse the initialization declaration or expression. */
18044 if (c_parser_next_tokens_start_declaration (parser))
18045 {
18046 if (i > 0)
18047 vec_safe_push (for_block, c_begin_compound_stmt (true));
18048 this_pre_body = push_stmt_list ();
18049 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
18050 NULL, vNULL);
18051 if (this_pre_body)
18052 {
18053 this_pre_body = pop_stmt_list (this_pre_body);
18054 if (pre_body)
18055 {
18056 tree t = pre_body;
18057 pre_body = push_stmt_list ();
18058 add_stmt (t);
18059 add_stmt (this_pre_body);
18060 pre_body = pop_stmt_list (pre_body);
18061 }
18062 else
18063 pre_body = this_pre_body;
18064 }
18065 decl = check_for_loop_decls (for_loc, flag_isoc99);
18066 if (decl == NULL)
18067 goto error_init;
18068 if (DECL_INITIAL (decl) == error_mark_node)
18069 decl = error_mark_node;
18070 init = decl;
18071 }
18072 else if (c_parser_next_token_is (parser, CPP_NAME)
18073 && c_parser_peek_2nd_token (parser)->type == CPP_EQ)
18074 {
18075 struct c_expr decl_exp;
18076 struct c_expr init_exp;
18077 location_t init_loc;
18078
18079 decl_exp = c_parser_postfix_expression (parser);
18080 decl = decl_exp.value;
18081
18082 c_parser_require (parser, CPP_EQ, "expected %<=%>");
18083
18084 init_loc = c_parser_peek_token (parser)->location;
18085 init_exp = c_parser_expr_no_commas (parser, NULL);
18086 init_exp = default_function_array_read_conversion (init_loc,
18087 init_exp);
18088 init = build_modify_expr (init_loc, decl, decl_exp.original_type,
18089 NOP_EXPR, init_loc, init_exp.value,
18090 init_exp.original_type);
18091 init = c_process_expr_stmt (init_loc, init);
18092
18093 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
18094 }
18095 else
18096 {
18097 error_init:
18098 c_parser_error (parser,
18099 "expected iteration declaration or initialization");
18100 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
18101 "expected %<)%>");
18102 fail = true;
18103 goto parse_next;
18104 }
18105
18106 /* Parse the loop condition. */
18107 cond = NULL_TREE;
18108 if (c_parser_next_token_is_not (parser, CPP_SEMICOLON))
18109 {
18110 location_t cond_loc = c_parser_peek_token (parser)->location;
18111 struct c_expr cond_expr
18112 = c_parser_binary_expression (parser, NULL, NULL_TREE);
18113
18114 cond = cond_expr.value;
18115 cond = c_objc_common_truthvalue_conversion (cond_loc, cond);
18116 if (COMPARISON_CLASS_P (cond))
18117 {
18118 tree op0 = TREE_OPERAND (cond, 0), op1 = TREE_OPERAND (cond, 1);
18119 op0 = c_fully_fold (op0, false, NULL);
18120 op1 = c_fully_fold (op1, false, NULL);
18121 TREE_OPERAND (cond, 0) = op0;
18122 TREE_OPERAND (cond, 1) = op1;
18123 }
18124 switch (cond_expr.original_code)
18125 {
18126 case GT_EXPR:
18127 case GE_EXPR:
18128 case LT_EXPR:
18129 case LE_EXPR:
18130 break;
18131 case NE_EXPR:
18132 if (code != OACC_LOOP)
18133 break;
18134 /* FALLTHRU. */
18135 default:
18136 /* Can't be cond = error_mark_node, because we want to preserve
18137 the location until c_finish_omp_for. */
18138 cond = build1 (NOP_EXPR, boolean_type_node, error_mark_node);
18139 break;
18140 }
18141 protected_set_expr_location (cond, cond_loc);
18142 }
18143 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
18144
18145 /* Parse the increment expression. */
18146 incr = NULL_TREE;
18147 if (c_parser_next_token_is_not (parser, CPP_CLOSE_PAREN))
18148 {
18149 location_t incr_loc = c_parser_peek_token (parser)->location;
18150
18151 incr = c_process_expr_stmt (incr_loc,
18152 c_parser_expression (parser).value);
18153 }
18154 parens.skip_until_found_close (parser);
18155
18156 if (decl == NULL || decl == error_mark_node || init == error_mark_node)
18157 fail = true;
18158 else
18159 {
18160 TREE_VEC_ELT (declv, i) = decl;
18161 TREE_VEC_ELT (initv, i) = init;
18162 TREE_VEC_ELT (condv, i) = cond;
18163 TREE_VEC_ELT (incrv, i) = incr;
18164 }
18165
18166 parse_next:
18167 if (i == count - 1)
18168 break;
18169
18170 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
18171 in between the collapsed for loops to be still considered perfectly
18172 nested. Hopefully the final version clarifies this.
18173 For now handle (multiple) {'s and empty statements. */
18174 do
18175 {
18176 if (c_parser_next_token_is_keyword (parser, RID_FOR))
18177 {
18178 c_parser_consume_token (parser);
18179 break;
18180 }
18181 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
18182 {
18183 c_parser_consume_token (parser);
18184 bracecount++;
18185 }
18186 else if (bracecount
18187 && c_parser_next_token_is (parser, CPP_SEMICOLON))
18188 c_parser_consume_token (parser);
18189 else
18190 {
18191 c_parser_error (parser, "not enough perfectly nested loops");
18192 if (bracecount)
18193 {
18194 open_brace_parsed = true;
18195 bracecount--;
18196 }
18197 fail = true;
18198 count = 0;
18199 break;
18200 }
18201 }
18202 while (1);
18203
18204 nbraces += bracecount;
18205 }
18206
18207 if (nbraces)
18208 if_p = NULL;
18209
18210 save_break = c_break_label;
18211 c_break_label = size_one_node;
18212 save_cont = c_cont_label;
18213 c_cont_label = NULL_TREE;
18214 body = push_stmt_list ();
18215
18216 if (inscan)
18217 c_parser_omp_scan_loop_body (parser, open_brace_parsed);
18218 else if (open_brace_parsed)
18219 {
18220 location_t here = c_parser_peek_token (parser)->location;
18221 stmt = c_begin_compound_stmt (true);
18222 c_parser_compound_statement_nostart (parser);
18223 add_stmt (c_end_compound_stmt (here, stmt, true));
18224 }
18225 else
18226 add_stmt (c_parser_c99_block_statement (parser, if_p));
18227 if (c_cont_label)
18228 {
18229 tree t = build1 (LABEL_EXPR, void_type_node, c_cont_label);
18230 SET_EXPR_LOCATION (t, loc);
18231 add_stmt (t);
18232 }
18233
18234 body = pop_stmt_list (body);
18235 c_break_label = save_break;
18236 c_cont_label = save_cont;
18237
18238 while (nbraces)
18239 {
18240 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
18241 {
18242 c_parser_consume_token (parser);
18243 nbraces--;
18244 }
18245 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
18246 c_parser_consume_token (parser);
18247 else
18248 {
18249 c_parser_error (parser, "collapsed loops not perfectly nested");
18250 while (nbraces)
18251 {
18252 location_t here = c_parser_peek_token (parser)->location;
18253 stmt = c_begin_compound_stmt (true);
18254 add_stmt (body);
18255 c_parser_compound_statement_nostart (parser);
18256 body = c_end_compound_stmt (here, stmt, true);
18257 nbraces--;
18258 }
18259 goto pop_scopes;
18260 }
18261 }
18262
18263 /* Only bother calling c_finish_omp_for if we haven't already generated
18264 an error from the initialization parsing. */
18265 if (!fail)
18266 {
18267 stmt = c_finish_omp_for (loc, code, declv, NULL, initv, condv,
18268 incrv, body, pre_body, true);
18269
18270 /* Check for iterators appearing in lb, b or incr expressions. */
18271 if (stmt && !c_omp_check_loop_iv (stmt, declv, NULL))
18272 stmt = NULL_TREE;
18273
18274 if (stmt)
18275 {
18276 add_stmt (stmt);
18277
18278 if (cclauses != NULL
18279 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL)
18280 {
18281 tree *c;
18282 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
18283 if (OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_FIRSTPRIVATE
18284 && OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_LASTPRIVATE)
18285 c = &OMP_CLAUSE_CHAIN (*c);
18286 else
18287 {
18288 for (i = 0; i < count; i++)
18289 if (TREE_VEC_ELT (declv, i) == OMP_CLAUSE_DECL (*c))
18290 break;
18291 if (i == count)
18292 c = &OMP_CLAUSE_CHAIN (*c);
18293 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE)
18294 {
18295 error_at (loc,
18296 "iteration variable %qD should not be firstprivate",
18297 OMP_CLAUSE_DECL (*c));
18298 *c = OMP_CLAUSE_CHAIN (*c);
18299 }
18300 else
18301 {
18302 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
18303 tree l = *c;
18304 *c = OMP_CLAUSE_CHAIN (*c);
18305 if (code == OMP_SIMD)
18306 {
18307 OMP_CLAUSE_CHAIN (l)
18308 = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
18309 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
18310 }
18311 else
18312 {
18313 OMP_CLAUSE_CHAIN (l) = clauses;
18314 clauses = l;
18315 }
18316 }
18317 }
18318 }
18319 OMP_FOR_CLAUSES (stmt) = clauses;
18320 }
18321 ret = stmt;
18322 }
18323 pop_scopes:
18324 while (!for_block->is_empty ())
18325 {
18326 /* FIXME diagnostics: LOC below should be the actual location of
18327 this particular for block. We need to build a list of
18328 locations to go along with FOR_BLOCK. */
18329 stmt = c_end_compound_stmt (loc, for_block->pop (), true);
18330 add_stmt (stmt);
18331 }
18332 release_tree_vector (for_block);
18333 return ret;
18334 }
18335
18336 /* Helper function for OpenMP parsing, split clauses and call
18337 finish_omp_clauses on each of the set of clauses afterwards. */
18338
18339 static void
18340 omp_split_clauses (location_t loc, enum tree_code code,
18341 omp_clause_mask mask, tree clauses, tree *cclauses)
18342 {
18343 int i;
18344 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
18345 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
18346 if (cclauses[i])
18347 cclauses[i] = c_finish_omp_clauses (cclauses[i], C_ORT_OMP);
18348 }
18349
18350 /* OpenMP 5.0:
18351 #pragma omp loop loop-clause[optseq] new-line
18352 for-loop
18353
18354 LOC is the location of the #pragma token.
18355 */
18356
18357 #define OMP_LOOP_CLAUSE_MASK \
18358 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
18359 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
18360 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
18361 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
18362 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_BIND) \
18363 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDER))
18364
18365 static tree
18366 c_parser_omp_loop (location_t loc, c_parser *parser,
18367 char *p_name, omp_clause_mask mask, tree *cclauses,
18368 bool *if_p)
18369 {
18370 tree block, clauses, ret;
18371
18372 strcat (p_name, " loop");
18373 mask |= OMP_LOOP_CLAUSE_MASK;
18374
18375 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
18376 if (cclauses)
18377 {
18378 omp_split_clauses (loc, OMP_LOOP, mask, clauses, cclauses);
18379 clauses = cclauses[C_OMP_CLAUSE_SPLIT_LOOP];
18380 }
18381
18382 block = c_begin_compound_stmt (true);
18383 ret = c_parser_omp_for_loop (loc, parser, OMP_LOOP, clauses, cclauses, if_p);
18384 block = c_end_compound_stmt (loc, block, true);
18385 add_stmt (block);
18386
18387 return ret;
18388 }
18389
18390 /* OpenMP 4.0:
18391 #pragma omp simd simd-clause[optseq] new-line
18392 for-loop
18393
18394 LOC is the location of the #pragma token.
18395 */
18396
18397 #define OMP_SIMD_CLAUSE_MASK \
18398 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
18399 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
18400 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
18401 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
18402 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
18403 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
18404 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
18405 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
18406 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
18407 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NONTEMPORAL) \
18408 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDER))
18409
18410 static tree
18411 c_parser_omp_simd (location_t loc, c_parser *parser,
18412 char *p_name, omp_clause_mask mask, tree *cclauses,
18413 bool *if_p)
18414 {
18415 tree block, clauses, ret;
18416
18417 strcat (p_name, " simd");
18418 mask |= OMP_SIMD_CLAUSE_MASK;
18419
18420 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
18421 if (cclauses)
18422 {
18423 omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
18424 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
18425 tree c = omp_find_clause (cclauses[C_OMP_CLAUSE_SPLIT_FOR],
18426 OMP_CLAUSE_ORDERED);
18427 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
18428 {
18429 error_at (OMP_CLAUSE_LOCATION (c),
18430 "%<ordered%> clause with parameter may not be specified "
18431 "on %qs construct", p_name);
18432 OMP_CLAUSE_ORDERED_EXPR (c) = NULL_TREE;
18433 }
18434 }
18435
18436 block = c_begin_compound_stmt (true);
18437 ret = c_parser_omp_for_loop (loc, parser, OMP_SIMD, clauses, cclauses, if_p);
18438 block = c_end_compound_stmt (loc, block, true);
18439 add_stmt (block);
18440
18441 return ret;
18442 }
18443
18444 /* OpenMP 2.5:
18445 #pragma omp for for-clause[optseq] new-line
18446 for-loop
18447
18448 OpenMP 4.0:
18449 #pragma omp for simd for-simd-clause[optseq] new-line
18450 for-loop
18451
18452 LOC is the location of the #pragma token.
18453 */
18454
18455 #define OMP_FOR_CLAUSE_MASK \
18456 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
18457 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
18458 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
18459 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
18460 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
18461 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
18462 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
18463 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
18464 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
18465 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDER))
18466
18467 static tree
18468 c_parser_omp_for (location_t loc, c_parser *parser,
18469 char *p_name, omp_clause_mask mask, tree *cclauses,
18470 bool *if_p)
18471 {
18472 tree block, clauses, ret;
18473
18474 strcat (p_name, " for");
18475 mask |= OMP_FOR_CLAUSE_MASK;
18476 /* parallel for{, simd} disallows nowait clause, but for
18477 target {teams distribute ,}parallel for{, simd} it should be accepted. */
18478 if (cclauses && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
18479 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
18480 /* Composite distribute parallel for{, simd} disallows ordered clause. */
18481 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
18482 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
18483
18484 if (c_parser_next_token_is (parser, CPP_NAME))
18485 {
18486 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
18487
18488 if (strcmp (p, "simd") == 0)
18489 {
18490 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
18491 if (cclauses == NULL)
18492 cclauses = cclauses_buf;
18493
18494 c_parser_consume_token (parser);
18495 if (!flag_openmp) /* flag_openmp_simd */
18496 return c_parser_omp_simd (loc, parser, p_name, mask, cclauses,
18497 if_p);
18498 block = c_begin_compound_stmt (true);
18499 ret = c_parser_omp_simd (loc, parser, p_name, mask, cclauses, if_p);
18500 block = c_end_compound_stmt (loc, block, true);
18501 if (ret == NULL_TREE)
18502 return ret;
18503 ret = make_node (OMP_FOR);
18504 TREE_TYPE (ret) = void_type_node;
18505 OMP_FOR_BODY (ret) = block;
18506 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
18507 SET_EXPR_LOCATION (ret, loc);
18508 add_stmt (ret);
18509 return ret;
18510 }
18511 }
18512 if (!flag_openmp) /* flag_openmp_simd */
18513 {
18514 c_parser_skip_to_pragma_eol (parser, false);
18515 return NULL_TREE;
18516 }
18517
18518 /* Composite distribute parallel for disallows linear clause. */
18519 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
18520 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
18521
18522 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
18523 if (cclauses)
18524 {
18525 omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
18526 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
18527 }
18528
18529 block = c_begin_compound_stmt (true);
18530 ret = c_parser_omp_for_loop (loc, parser, OMP_FOR, clauses, cclauses, if_p);
18531 block = c_end_compound_stmt (loc, block, true);
18532 add_stmt (block);
18533
18534 return ret;
18535 }
18536
18537 static tree c_parser_omp_taskloop (location_t, c_parser *, char *,
18538 omp_clause_mask, tree *, bool *);
18539
18540 /* OpenMP 2.5:
18541 # pragma omp master new-line
18542 structured-block
18543
18544 LOC is the location of the #pragma token.
18545 */
18546
18547 static tree
18548 c_parser_omp_master (location_t loc, c_parser *parser,
18549 char *p_name, omp_clause_mask mask, tree *cclauses,
18550 bool *if_p)
18551 {
18552 tree block, clauses, ret;
18553
18554 strcat (p_name, " master");
18555
18556 if (c_parser_next_token_is (parser, CPP_NAME))
18557 {
18558 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
18559
18560 if (strcmp (p, "taskloop") == 0)
18561 {
18562 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
18563 if (cclauses == NULL)
18564 cclauses = cclauses_buf;
18565
18566 c_parser_consume_token (parser);
18567 if (!flag_openmp) /* flag_openmp_simd */
18568 return c_parser_omp_taskloop (loc, parser, p_name, mask, cclauses,
18569 if_p);
18570 block = c_begin_compound_stmt (true);
18571 ret = c_parser_omp_taskloop (loc, parser, p_name, mask, cclauses,
18572 if_p);
18573 block = c_end_compound_stmt (loc, block, true);
18574 if (ret == NULL_TREE)
18575 return ret;
18576 ret = c_finish_omp_master (loc, block);
18577 return ret;
18578 }
18579 }
18580 if (!flag_openmp) /* flag_openmp_simd */
18581 {
18582 c_parser_skip_to_pragma_eol (parser, false);
18583 return NULL_TREE;
18584 }
18585
18586 if (cclauses)
18587 {
18588 clauses = c_parser_omp_all_clauses (parser, mask, p_name, false);
18589 omp_split_clauses (loc, OMP_MASTER, mask, clauses, cclauses);
18590 }
18591 else
18592 c_parser_skip_to_pragma_eol (parser);
18593
18594 return c_finish_omp_master (loc, c_parser_omp_structured_block (parser,
18595 if_p));
18596 }
18597
18598 /* OpenMP 2.5:
18599 # pragma omp ordered new-line
18600 structured-block
18601
18602 OpenMP 4.5:
18603 # pragma omp ordered ordered-clauses new-line
18604 structured-block
18605
18606 # pragma omp ordered depend-clauses new-line */
18607
18608 #define OMP_ORDERED_CLAUSE_MASK \
18609 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
18610 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
18611
18612 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
18613 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
18614
18615 static bool
18616 c_parser_omp_ordered (c_parser *parser, enum pragma_context context,
18617 bool *if_p)
18618 {
18619 location_t loc = c_parser_peek_token (parser)->location;
18620 c_parser_consume_pragma (parser);
18621
18622 if (context != pragma_stmt && context != pragma_compound)
18623 {
18624 c_parser_error (parser, "expected declaration specifiers");
18625 c_parser_skip_to_pragma_eol (parser, false);
18626 return false;
18627 }
18628
18629 if (c_parser_next_token_is (parser, CPP_NAME))
18630 {
18631 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
18632
18633 if (!strcmp ("depend", p))
18634 {
18635 if (!flag_openmp) /* flag_openmp_simd */
18636 {
18637 c_parser_skip_to_pragma_eol (parser, false);
18638 return false;
18639 }
18640 if (context == pragma_stmt)
18641 {
18642 error_at (loc,
18643 "%<#pragma omp ordered%> with %<depend%> clause may "
18644 "only be used in compound statements");
18645 c_parser_skip_to_pragma_eol (parser, false);
18646 return false;
18647 }
18648
18649 tree clauses
18650 = c_parser_omp_all_clauses (parser,
18651 OMP_ORDERED_DEPEND_CLAUSE_MASK,
18652 "#pragma omp ordered");
18653 c_finish_omp_ordered (loc, clauses, NULL_TREE);
18654 return false;
18655 }
18656 }
18657
18658 tree clauses = c_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
18659 "#pragma omp ordered");
18660
18661 if (!flag_openmp /* flag_openmp_simd */
18662 && omp_find_clause (clauses, OMP_CLAUSE_SIMD) == NULL_TREE)
18663 return false;
18664
18665 c_finish_omp_ordered (loc, clauses,
18666 c_parser_omp_structured_block (parser, if_p));
18667 return true;
18668 }
18669
18670 /* OpenMP 2.5:
18671
18672 section-scope:
18673 { section-sequence }
18674
18675 section-sequence:
18676 section-directive[opt] structured-block
18677 section-sequence section-directive structured-block
18678
18679 SECTIONS_LOC is the location of the #pragma omp sections. */
18680
18681 static tree
18682 c_parser_omp_sections_scope (location_t sections_loc, c_parser *parser)
18683 {
18684 tree stmt, substmt;
18685 bool error_suppress = false;
18686 location_t loc;
18687
18688 loc = c_parser_peek_token (parser)->location;
18689 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
18690 {
18691 /* Avoid skipping until the end of the block. */
18692 parser->error = false;
18693 return NULL_TREE;
18694 }
18695
18696 stmt = push_stmt_list ();
18697
18698 if (c_parser_peek_token (parser)->pragma_kind != PRAGMA_OMP_SECTION)
18699 {
18700 substmt = c_parser_omp_structured_block (parser, NULL);
18701 substmt = build1 (OMP_SECTION, void_type_node, substmt);
18702 SET_EXPR_LOCATION (substmt, loc);
18703 add_stmt (substmt);
18704 }
18705
18706 while (1)
18707 {
18708 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
18709 break;
18710 if (c_parser_next_token_is (parser, CPP_EOF))
18711 break;
18712
18713 loc = c_parser_peek_token (parser)->location;
18714 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_OMP_SECTION)
18715 {
18716 c_parser_consume_pragma (parser);
18717 c_parser_skip_to_pragma_eol (parser);
18718 error_suppress = false;
18719 }
18720 else if (!error_suppress)
18721 {
18722 error_at (loc, "expected %<#pragma omp section%> or %<}%>");
18723 error_suppress = true;
18724 }
18725
18726 substmt = c_parser_omp_structured_block (parser, NULL);
18727 substmt = build1 (OMP_SECTION, void_type_node, substmt);
18728 SET_EXPR_LOCATION (substmt, loc);
18729 add_stmt (substmt);
18730 }
18731 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE,
18732 "expected %<#pragma omp section%> or %<}%>");
18733
18734 substmt = pop_stmt_list (stmt);
18735
18736 stmt = make_node (OMP_SECTIONS);
18737 SET_EXPR_LOCATION (stmt, sections_loc);
18738 TREE_TYPE (stmt) = void_type_node;
18739 OMP_SECTIONS_BODY (stmt) = substmt;
18740
18741 return add_stmt (stmt);
18742 }
18743
18744 /* OpenMP 2.5:
18745 # pragma omp sections sections-clause[optseq] newline
18746 sections-scope
18747
18748 LOC is the location of the #pragma token.
18749 */
18750
18751 #define OMP_SECTIONS_CLAUSE_MASK \
18752 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
18753 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
18754 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
18755 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
18756 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
18757
18758 static tree
18759 c_parser_omp_sections (location_t loc, c_parser *parser,
18760 char *p_name, omp_clause_mask mask, tree *cclauses)
18761 {
18762 tree block, clauses, ret;
18763
18764 strcat (p_name, " sections");
18765 mask |= OMP_SECTIONS_CLAUSE_MASK;
18766 if (cclauses)
18767 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
18768
18769 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
18770 if (cclauses)
18771 {
18772 omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
18773 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
18774 }
18775
18776 block = c_begin_compound_stmt (true);
18777 ret = c_parser_omp_sections_scope (loc, parser);
18778 if (ret)
18779 OMP_SECTIONS_CLAUSES (ret) = clauses;
18780 block = c_end_compound_stmt (loc, block, true);
18781 add_stmt (block);
18782
18783 return ret;
18784 }
18785
18786 /* OpenMP 2.5:
18787 # pragma omp parallel parallel-clause[optseq] new-line
18788 structured-block
18789 # pragma omp parallel for parallel-for-clause[optseq] new-line
18790 structured-block
18791 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
18792 structured-block
18793
18794 OpenMP 4.0:
18795 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
18796 structured-block
18797
18798 LOC is the location of the #pragma token.
18799 */
18800
18801 #define OMP_PARALLEL_CLAUSE_MASK \
18802 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
18803 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
18804 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
18805 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
18806 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
18807 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
18808 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
18809 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
18810 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
18811
18812 static tree
18813 c_parser_omp_parallel (location_t loc, c_parser *parser,
18814 char *p_name, omp_clause_mask mask, tree *cclauses,
18815 bool *if_p)
18816 {
18817 tree stmt, clauses, block;
18818
18819 strcat (p_name, " parallel");
18820 mask |= OMP_PARALLEL_CLAUSE_MASK;
18821 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
18822 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
18823 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
18824 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
18825
18826 if (c_parser_next_token_is_keyword (parser, RID_FOR))
18827 {
18828 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
18829 if (cclauses == NULL)
18830 cclauses = cclauses_buf;
18831
18832 c_parser_consume_token (parser);
18833 if (!flag_openmp) /* flag_openmp_simd */
18834 return c_parser_omp_for (loc, parser, p_name, mask, cclauses, if_p);
18835 block = c_begin_omp_parallel ();
18836 tree ret = c_parser_omp_for (loc, parser, p_name, mask, cclauses, if_p);
18837 stmt
18838 = c_finish_omp_parallel (loc, cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
18839 block);
18840 if (ret == NULL_TREE)
18841 return ret;
18842 OMP_PARALLEL_COMBINED (stmt) = 1;
18843 return stmt;
18844 }
18845 /* When combined with distribute, parallel has to be followed by for.
18846 #pragma omp target parallel is allowed though. */
18847 else if (cclauses
18848 && (mask & (OMP_CLAUSE_MASK_1
18849 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
18850 {
18851 error_at (loc, "expected %<for%> after %qs", p_name);
18852 c_parser_skip_to_pragma_eol (parser);
18853 return NULL_TREE;
18854 }
18855 else if (c_parser_next_token_is (parser, CPP_NAME))
18856 {
18857 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
18858 if (cclauses == NULL && strcmp (p, "master") == 0)
18859 {
18860 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
18861 cclauses = cclauses_buf;
18862
18863 c_parser_consume_token (parser);
18864 if (!flag_openmp) /* flag_openmp_simd */
18865 return c_parser_omp_master (loc, parser, p_name, mask, cclauses,
18866 if_p);
18867 block = c_begin_omp_parallel ();
18868 tree ret = c_parser_omp_master (loc, parser, p_name, mask, cclauses,
18869 if_p);
18870 stmt = c_finish_omp_parallel (loc,
18871 cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
18872 block);
18873 OMP_PARALLEL_COMBINED (stmt) = 1;
18874 if (ret == NULL)
18875 return ret;
18876 return stmt;
18877 }
18878 else if (strcmp (p, "loop") == 0)
18879 {
18880 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
18881 if (cclauses == NULL)
18882 cclauses = cclauses_buf;
18883
18884 c_parser_consume_token (parser);
18885 if (!flag_openmp) /* flag_openmp_simd */
18886 return c_parser_omp_loop (loc, parser, p_name, mask, cclauses,
18887 if_p);
18888 block = c_begin_omp_parallel ();
18889 tree ret = c_parser_omp_loop (loc, parser, p_name, mask, cclauses,
18890 if_p);
18891 stmt
18892 = c_finish_omp_parallel (loc,
18893 cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
18894 block);
18895 if (ret == NULL_TREE)
18896 return ret;
18897 OMP_PARALLEL_COMBINED (stmt) = 1;
18898 return stmt;
18899 }
18900 else if (!flag_openmp) /* flag_openmp_simd */
18901 {
18902 c_parser_skip_to_pragma_eol (parser, false);
18903 return NULL_TREE;
18904 }
18905 else if (cclauses == NULL && strcmp (p, "sections") == 0)
18906 {
18907 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
18908 cclauses = cclauses_buf;
18909
18910 c_parser_consume_token (parser);
18911 block = c_begin_omp_parallel ();
18912 c_parser_omp_sections (loc, parser, p_name, mask, cclauses);
18913 stmt = c_finish_omp_parallel (loc,
18914 cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
18915 block);
18916 OMP_PARALLEL_COMBINED (stmt) = 1;
18917 return stmt;
18918 }
18919 }
18920 else if (!flag_openmp) /* flag_openmp_simd */
18921 {
18922 c_parser_skip_to_pragma_eol (parser, false);
18923 return NULL_TREE;
18924 }
18925
18926 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
18927 if (cclauses)
18928 {
18929 omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
18930 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
18931 }
18932
18933 block = c_begin_omp_parallel ();
18934 c_parser_statement (parser, if_p);
18935 stmt = c_finish_omp_parallel (loc, clauses, block);
18936
18937 return stmt;
18938 }
18939
18940 /* OpenMP 2.5:
18941 # pragma omp single single-clause[optseq] new-line
18942 structured-block
18943
18944 LOC is the location of the #pragma.
18945 */
18946
18947 #define OMP_SINGLE_CLAUSE_MASK \
18948 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
18949 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
18950 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
18951 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
18952
18953 static tree
18954 c_parser_omp_single (location_t loc, c_parser *parser, bool *if_p)
18955 {
18956 tree stmt = make_node (OMP_SINGLE);
18957 SET_EXPR_LOCATION (stmt, loc);
18958 TREE_TYPE (stmt) = void_type_node;
18959
18960 OMP_SINGLE_CLAUSES (stmt)
18961 = c_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
18962 "#pragma omp single");
18963 OMP_SINGLE_BODY (stmt) = c_parser_omp_structured_block (parser, if_p);
18964
18965 return add_stmt (stmt);
18966 }
18967
18968 /* OpenMP 3.0:
18969 # pragma omp task task-clause[optseq] new-line
18970
18971 LOC is the location of the #pragma.
18972 */
18973
18974 #define OMP_TASK_CLAUSE_MASK \
18975 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
18976 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
18977 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
18978 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
18979 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
18980 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
18981 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
18982 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
18983 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
18984 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY) \
18985 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION))
18986
18987 static tree
18988 c_parser_omp_task (location_t loc, c_parser *parser, bool *if_p)
18989 {
18990 tree clauses, block;
18991
18992 clauses = c_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
18993 "#pragma omp task");
18994
18995 block = c_begin_omp_task ();
18996 c_parser_statement (parser, if_p);
18997 return c_finish_omp_task (loc, clauses, block);
18998 }
18999
19000 /* OpenMP 3.0:
19001 # pragma omp taskwait new-line
19002
19003 OpenMP 5.0:
19004 # pragma omp taskwait taskwait-clause[optseq] new-line
19005 */
19006
19007 #define OMP_TASKWAIT_CLAUSE_MASK \
19008 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
19009
19010 static void
19011 c_parser_omp_taskwait (c_parser *parser)
19012 {
19013 location_t loc = c_parser_peek_token (parser)->location;
19014 c_parser_consume_pragma (parser);
19015
19016 tree clauses
19017 = c_parser_omp_all_clauses (parser, OMP_TASKWAIT_CLAUSE_MASK,
19018 "#pragma omp taskwait");
19019
19020 if (clauses)
19021 {
19022 tree stmt = make_node (OMP_TASK);
19023 TREE_TYPE (stmt) = void_node;
19024 OMP_TASK_CLAUSES (stmt) = clauses;
19025 OMP_TASK_BODY (stmt) = NULL_TREE;
19026 SET_EXPR_LOCATION (stmt, loc);
19027 add_stmt (stmt);
19028 }
19029 else
19030 c_finish_omp_taskwait (loc);
19031 }
19032
19033 /* OpenMP 3.1:
19034 # pragma omp taskyield new-line
19035 */
19036
19037 static void
19038 c_parser_omp_taskyield (c_parser *parser)
19039 {
19040 location_t loc = c_parser_peek_token (parser)->location;
19041 c_parser_consume_pragma (parser);
19042 c_parser_skip_to_pragma_eol (parser);
19043
19044 c_finish_omp_taskyield (loc);
19045 }
19046
19047 /* OpenMP 4.0:
19048 # pragma omp taskgroup new-line
19049
19050 OpenMP 5.0:
19051 # pragma omp taskgroup taskgroup-clause[optseq] new-line
19052 */
19053
19054 #define OMP_TASKGROUP_CLAUSE_MASK \
19055 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASK_REDUCTION))
19056
19057 static tree
19058 c_parser_omp_taskgroup (location_t loc, c_parser *parser, bool *if_p)
19059 {
19060 tree clauses = c_parser_omp_all_clauses (parser, OMP_TASKGROUP_CLAUSE_MASK,
19061 "#pragma omp taskgroup");
19062
19063 tree body = c_parser_omp_structured_block (parser, if_p);
19064 return c_finish_omp_taskgroup (loc, body, clauses);
19065 }
19066
19067 /* OpenMP 4.0:
19068 # pragma omp cancel cancel-clause[optseq] new-line
19069
19070 LOC is the location of the #pragma.
19071 */
19072
19073 #define OMP_CANCEL_CLAUSE_MASK \
19074 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
19075 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
19076 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
19077 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
19078 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
19079
19080 static void
19081 c_parser_omp_cancel (c_parser *parser)
19082 {
19083 location_t loc = c_parser_peek_token (parser)->location;
19084
19085 c_parser_consume_pragma (parser);
19086 tree clauses = c_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
19087 "#pragma omp cancel");
19088
19089 c_finish_omp_cancel (loc, clauses);
19090 }
19091
19092 /* OpenMP 4.0:
19093 # pragma omp cancellation point cancelpt-clause[optseq] new-line
19094
19095 LOC is the location of the #pragma.
19096 */
19097
19098 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
19099 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
19100 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
19101 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
19102 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
19103
19104 static void
19105 c_parser_omp_cancellation_point (c_parser *parser, enum pragma_context context)
19106 {
19107 location_t loc = c_parser_peek_token (parser)->location;
19108 tree clauses;
19109 bool point_seen = false;
19110
19111 c_parser_consume_pragma (parser);
19112 if (c_parser_next_token_is (parser, CPP_NAME))
19113 {
19114 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
19115 if (strcmp (p, "point") == 0)
19116 {
19117 c_parser_consume_token (parser);
19118 point_seen = true;
19119 }
19120 }
19121 if (!point_seen)
19122 {
19123 c_parser_error (parser, "expected %<point%>");
19124 c_parser_skip_to_pragma_eol (parser);
19125 return;
19126 }
19127
19128 if (context != pragma_compound)
19129 {
19130 if (context == pragma_stmt)
19131 error_at (loc,
19132 "%<#pragma %s%> may only be used in compound statements",
19133 "omp cancellation point");
19134 else
19135 c_parser_error (parser, "expected declaration specifiers");
19136 c_parser_skip_to_pragma_eol (parser, false);
19137 return;
19138 }
19139
19140 clauses
19141 = c_parser_omp_all_clauses (parser, OMP_CANCELLATION_POINT_CLAUSE_MASK,
19142 "#pragma omp cancellation point");
19143
19144 c_finish_omp_cancellation_point (loc, clauses);
19145 }
19146
19147 /* OpenMP 4.0:
19148 #pragma omp distribute distribute-clause[optseq] new-line
19149 for-loop */
19150
19151 #define OMP_DISTRIBUTE_CLAUSE_MASK \
19152 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
19153 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
19154 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
19155 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
19156 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
19157
19158 static tree
19159 c_parser_omp_distribute (location_t loc, c_parser *parser,
19160 char *p_name, omp_clause_mask mask, tree *cclauses,
19161 bool *if_p)
19162 {
19163 tree clauses, block, ret;
19164
19165 strcat (p_name, " distribute");
19166 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
19167
19168 if (c_parser_next_token_is (parser, CPP_NAME))
19169 {
19170 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
19171 bool simd = false;
19172 bool parallel = false;
19173
19174 if (strcmp (p, "simd") == 0)
19175 simd = true;
19176 else
19177 parallel = strcmp (p, "parallel") == 0;
19178 if (parallel || simd)
19179 {
19180 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
19181 if (cclauses == NULL)
19182 cclauses = cclauses_buf;
19183 c_parser_consume_token (parser);
19184 if (!flag_openmp) /* flag_openmp_simd */
19185 {
19186 if (simd)
19187 return c_parser_omp_simd (loc, parser, p_name, mask, cclauses,
19188 if_p);
19189 else
19190 return c_parser_omp_parallel (loc, parser, p_name, mask,
19191 cclauses, if_p);
19192 }
19193 block = c_begin_compound_stmt (true);
19194 if (simd)
19195 ret = c_parser_omp_simd (loc, parser, p_name, mask, cclauses,
19196 if_p);
19197 else
19198 ret = c_parser_omp_parallel (loc, parser, p_name, mask, cclauses,
19199 if_p);
19200 block = c_end_compound_stmt (loc, block, true);
19201 if (ret == NULL)
19202 return ret;
19203 ret = make_node (OMP_DISTRIBUTE);
19204 TREE_TYPE (ret) = void_type_node;
19205 OMP_FOR_BODY (ret) = block;
19206 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
19207 SET_EXPR_LOCATION (ret, loc);
19208 add_stmt (ret);
19209 return ret;
19210 }
19211 }
19212 if (!flag_openmp) /* flag_openmp_simd */
19213 {
19214 c_parser_skip_to_pragma_eol (parser, false);
19215 return NULL_TREE;
19216 }
19217
19218 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
19219 if (cclauses)
19220 {
19221 omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
19222 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
19223 }
19224
19225 block = c_begin_compound_stmt (true);
19226 ret = c_parser_omp_for_loop (loc, parser, OMP_DISTRIBUTE, clauses, NULL,
19227 if_p);
19228 block = c_end_compound_stmt (loc, block, true);
19229 add_stmt (block);
19230
19231 return ret;
19232 }
19233
19234 /* OpenMP 4.0:
19235 # pragma omp teams teams-clause[optseq] new-line
19236 structured-block */
19237
19238 #define OMP_TEAMS_CLAUSE_MASK \
19239 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
19240 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
19241 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
19242 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
19243 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
19244 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
19245 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
19246
19247 static tree
19248 c_parser_omp_teams (location_t loc, c_parser *parser,
19249 char *p_name, omp_clause_mask mask, tree *cclauses,
19250 bool *if_p)
19251 {
19252 tree clauses, block, ret;
19253
19254 strcat (p_name, " teams");
19255 mask |= OMP_TEAMS_CLAUSE_MASK;
19256
19257 if (c_parser_next_token_is (parser, CPP_NAME))
19258 {
19259 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
19260 if (strcmp (p, "distribute") == 0)
19261 {
19262 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
19263 if (cclauses == NULL)
19264 cclauses = cclauses_buf;
19265
19266 c_parser_consume_token (parser);
19267 if (!flag_openmp) /* flag_openmp_simd */
19268 return c_parser_omp_distribute (loc, parser, p_name, mask,
19269 cclauses, if_p);
19270 block = c_begin_omp_parallel ();
19271 ret = c_parser_omp_distribute (loc, parser, p_name, mask, cclauses,
19272 if_p);
19273 block = c_end_compound_stmt (loc, block, true);
19274 if (ret == NULL)
19275 return ret;
19276 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
19277 ret = make_node (OMP_TEAMS);
19278 TREE_TYPE (ret) = void_type_node;
19279 OMP_TEAMS_CLAUSES (ret) = clauses;
19280 OMP_TEAMS_BODY (ret) = block;
19281 OMP_TEAMS_COMBINED (ret) = 1;
19282 SET_EXPR_LOCATION (ret, loc);
19283 return add_stmt (ret);
19284 }
19285 else if (strcmp (p, "loop") == 0)
19286 {
19287 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
19288 if (cclauses == NULL)
19289 cclauses = cclauses_buf;
19290
19291 c_parser_consume_token (parser);
19292 if (!flag_openmp) /* flag_openmp_simd */
19293 return c_parser_omp_loop (loc, parser, p_name, mask, cclauses,
19294 if_p);
19295 block = c_begin_omp_parallel ();
19296 ret = c_parser_omp_loop (loc, parser, p_name, mask, cclauses, if_p);
19297 block = c_end_compound_stmt (loc, block, true);
19298 if (ret == NULL)
19299 return ret;
19300 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
19301 ret = make_node (OMP_TEAMS);
19302 TREE_TYPE (ret) = void_type_node;
19303 OMP_TEAMS_CLAUSES (ret) = clauses;
19304 OMP_TEAMS_BODY (ret) = block;
19305 OMP_TEAMS_COMBINED (ret) = 1;
19306 SET_EXPR_LOCATION (ret, loc);
19307 return add_stmt (ret);
19308 }
19309 }
19310 if (!flag_openmp) /* flag_openmp_simd */
19311 {
19312 c_parser_skip_to_pragma_eol (parser, false);
19313 return NULL_TREE;
19314 }
19315
19316 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
19317 if (cclauses)
19318 {
19319 omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
19320 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
19321 }
19322
19323 tree stmt = make_node (OMP_TEAMS);
19324 TREE_TYPE (stmt) = void_type_node;
19325 OMP_TEAMS_CLAUSES (stmt) = clauses;
19326 block = c_begin_omp_parallel ();
19327 add_stmt (c_parser_omp_structured_block (parser, if_p));
19328 OMP_TEAMS_BODY (stmt) = c_end_compound_stmt (loc, block, true);
19329 SET_EXPR_LOCATION (stmt, loc);
19330
19331 return add_stmt (stmt);
19332 }
19333
19334 /* OpenMP 4.0:
19335 # pragma omp target data target-data-clause[optseq] new-line
19336 structured-block */
19337
19338 #define OMP_TARGET_DATA_CLAUSE_MASK \
19339 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
19340 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
19341 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
19342 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR) \
19343 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_ADDR))
19344
19345 static tree
19346 c_parser_omp_target_data (location_t loc, c_parser *parser, bool *if_p)
19347 {
19348 tree clauses
19349 = c_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
19350 "#pragma omp target data");
19351 int map_seen = 0;
19352 for (tree *pc = &clauses; *pc;)
19353 {
19354 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
19355 switch (OMP_CLAUSE_MAP_KIND (*pc))
19356 {
19357 case GOMP_MAP_TO:
19358 case GOMP_MAP_ALWAYS_TO:
19359 case GOMP_MAP_FROM:
19360 case GOMP_MAP_ALWAYS_FROM:
19361 case GOMP_MAP_TOFROM:
19362 case GOMP_MAP_ALWAYS_TOFROM:
19363 case GOMP_MAP_ALLOC:
19364 map_seen = 3;
19365 break;
19366 case GOMP_MAP_FIRSTPRIVATE_POINTER:
19367 case GOMP_MAP_ALWAYS_POINTER:
19368 break;
19369 default:
19370 map_seen |= 1;
19371 error_at (OMP_CLAUSE_LOCATION (*pc),
19372 "%<#pragma omp target data%> with map-type other "
19373 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
19374 "on %<map%> clause");
19375 *pc = OMP_CLAUSE_CHAIN (*pc);
19376 continue;
19377 }
19378 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_USE_DEVICE_PTR
19379 || OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_USE_DEVICE_ADDR)
19380 map_seen = 3;
19381 pc = &OMP_CLAUSE_CHAIN (*pc);
19382 }
19383
19384 if (map_seen != 3)
19385 {
19386 if (map_seen == 0)
19387 error_at (loc,
19388 "%<#pragma omp target data%> must contain at least "
19389 "one %<map%>, %<use_device_ptr%> or %<use_device_addr%> "
19390 "clause");
19391 return NULL_TREE;
19392 }
19393
19394 tree stmt = make_node (OMP_TARGET_DATA);
19395 TREE_TYPE (stmt) = void_type_node;
19396 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
19397 keep_next_level ();
19398 tree block = c_begin_compound_stmt (true);
19399 add_stmt (c_parser_omp_structured_block (parser, if_p));
19400 OMP_TARGET_DATA_BODY (stmt) = c_end_compound_stmt (loc, block, true);
19401
19402 SET_EXPR_LOCATION (stmt, loc);
19403 return add_stmt (stmt);
19404 }
19405
19406 /* OpenMP 4.0:
19407 # pragma omp target update target-update-clause[optseq] new-line */
19408
19409 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
19410 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
19411 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
19412 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
19413 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
19414 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
19415 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
19416
19417 static bool
19418 c_parser_omp_target_update (location_t loc, c_parser *parser,
19419 enum pragma_context context)
19420 {
19421 if (context == pragma_stmt)
19422 {
19423 error_at (loc, "%<#pragma %s%> may only be used in compound statements",
19424 "omp target update");
19425 c_parser_skip_to_pragma_eol (parser, false);
19426 return false;
19427 }
19428
19429 tree clauses
19430 = c_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
19431 "#pragma omp target update");
19432 if (omp_find_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
19433 && omp_find_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
19434 {
19435 error_at (loc,
19436 "%<#pragma omp target update%> must contain at least one "
19437 "%<from%> or %<to%> clauses");
19438 return false;
19439 }
19440
19441 tree stmt = make_node (OMP_TARGET_UPDATE);
19442 TREE_TYPE (stmt) = void_type_node;
19443 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
19444 SET_EXPR_LOCATION (stmt, loc);
19445 add_stmt (stmt);
19446 return false;
19447 }
19448
19449 /* OpenMP 4.5:
19450 # pragma omp target enter data target-data-clause[optseq] new-line */
19451
19452 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
19453 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
19454 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
19455 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
19456 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
19457 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
19458
19459 static tree
19460 c_parser_omp_target_enter_data (location_t loc, c_parser *parser,
19461 enum pragma_context context)
19462 {
19463 bool data_seen = false;
19464 if (c_parser_next_token_is (parser, CPP_NAME))
19465 {
19466 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
19467 if (strcmp (p, "data") == 0)
19468 {
19469 c_parser_consume_token (parser);
19470 data_seen = true;
19471 }
19472 }
19473 if (!data_seen)
19474 {
19475 c_parser_error (parser, "expected %<data%>");
19476 c_parser_skip_to_pragma_eol (parser);
19477 return NULL_TREE;
19478 }
19479
19480 if (context == pragma_stmt)
19481 {
19482 error_at (loc, "%<#pragma %s%> may only be used in compound statements",
19483 "omp target enter data");
19484 c_parser_skip_to_pragma_eol (parser, false);
19485 return NULL_TREE;
19486 }
19487
19488 tree clauses
19489 = c_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
19490 "#pragma omp target enter data");
19491 int map_seen = 0;
19492 for (tree *pc = &clauses; *pc;)
19493 {
19494 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
19495 switch (OMP_CLAUSE_MAP_KIND (*pc))
19496 {
19497 case GOMP_MAP_TO:
19498 case GOMP_MAP_ALWAYS_TO:
19499 case GOMP_MAP_ALLOC:
19500 map_seen = 3;
19501 break;
19502 case GOMP_MAP_FIRSTPRIVATE_POINTER:
19503 case GOMP_MAP_ALWAYS_POINTER:
19504 break;
19505 default:
19506 map_seen |= 1;
19507 error_at (OMP_CLAUSE_LOCATION (*pc),
19508 "%<#pragma omp target enter data%> with map-type other "
19509 "than %<to%> or %<alloc%> on %<map%> clause");
19510 *pc = OMP_CLAUSE_CHAIN (*pc);
19511 continue;
19512 }
19513 pc = &OMP_CLAUSE_CHAIN (*pc);
19514 }
19515
19516 if (map_seen != 3)
19517 {
19518 if (map_seen == 0)
19519 error_at (loc,
19520 "%<#pragma omp target enter data%> must contain at least "
19521 "one %<map%> clause");
19522 return NULL_TREE;
19523 }
19524
19525 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
19526 TREE_TYPE (stmt) = void_type_node;
19527 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
19528 SET_EXPR_LOCATION (stmt, loc);
19529 add_stmt (stmt);
19530 return stmt;
19531 }
19532
19533 /* OpenMP 4.5:
19534 # pragma omp target exit data target-data-clause[optseq] new-line */
19535
19536 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
19537 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
19538 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
19539 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
19540 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
19541 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
19542
19543 static tree
19544 c_parser_omp_target_exit_data (location_t loc, c_parser *parser,
19545 enum pragma_context context)
19546 {
19547 bool data_seen = false;
19548 if (c_parser_next_token_is (parser, CPP_NAME))
19549 {
19550 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
19551 if (strcmp (p, "data") == 0)
19552 {
19553 c_parser_consume_token (parser);
19554 data_seen = true;
19555 }
19556 }
19557 if (!data_seen)
19558 {
19559 c_parser_error (parser, "expected %<data%>");
19560 c_parser_skip_to_pragma_eol (parser);
19561 return NULL_TREE;
19562 }
19563
19564 if (context == pragma_stmt)
19565 {
19566 error_at (loc, "%<#pragma %s%> may only be used in compound statements",
19567 "omp target exit data");
19568 c_parser_skip_to_pragma_eol (parser, false);
19569 return NULL_TREE;
19570 }
19571
19572 tree clauses
19573 = c_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
19574 "#pragma omp target exit data");
19575
19576 int map_seen = 0;
19577 for (tree *pc = &clauses; *pc;)
19578 {
19579 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
19580 switch (OMP_CLAUSE_MAP_KIND (*pc))
19581 {
19582 case GOMP_MAP_FROM:
19583 case GOMP_MAP_ALWAYS_FROM:
19584 case GOMP_MAP_RELEASE:
19585 case GOMP_MAP_DELETE:
19586 map_seen = 3;
19587 break;
19588 case GOMP_MAP_FIRSTPRIVATE_POINTER:
19589 case GOMP_MAP_ALWAYS_POINTER:
19590 break;
19591 default:
19592 map_seen |= 1;
19593 error_at (OMP_CLAUSE_LOCATION (*pc),
19594 "%<#pragma omp target exit data%> with map-type other "
19595 "than %<from%>, %<release%> or %<delete%> on %<map%>"
19596 " clause");
19597 *pc = OMP_CLAUSE_CHAIN (*pc);
19598 continue;
19599 }
19600 pc = &OMP_CLAUSE_CHAIN (*pc);
19601 }
19602
19603 if (map_seen != 3)
19604 {
19605 if (map_seen == 0)
19606 error_at (loc,
19607 "%<#pragma omp target exit data%> must contain at least one "
19608 "%<map%> clause");
19609 return NULL_TREE;
19610 }
19611
19612 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
19613 TREE_TYPE (stmt) = void_type_node;
19614 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
19615 SET_EXPR_LOCATION (stmt, loc);
19616 add_stmt (stmt);
19617 return stmt;
19618 }
19619
19620 /* OpenMP 4.0:
19621 # pragma omp target target-clause[optseq] new-line
19622 structured-block */
19623
19624 #define OMP_TARGET_CLAUSE_MASK \
19625 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
19626 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
19627 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
19628 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
19629 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
19630 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
19631 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
19632 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
19633 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
19634
19635 static bool
19636 c_parser_omp_target (c_parser *parser, enum pragma_context context, bool *if_p)
19637 {
19638 location_t loc = c_parser_peek_token (parser)->location;
19639 c_parser_consume_pragma (parser);
19640 tree *pc = NULL, stmt, block;
19641
19642 if (context != pragma_stmt && context != pragma_compound)
19643 {
19644 c_parser_error (parser, "expected declaration specifiers");
19645 c_parser_skip_to_pragma_eol (parser);
19646 return false;
19647 }
19648
19649 if (flag_openmp)
19650 omp_requires_mask
19651 = (enum omp_requires) (omp_requires_mask | OMP_REQUIRES_TARGET_USED);
19652
19653 if (c_parser_next_token_is (parser, CPP_NAME))
19654 {
19655 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
19656 enum tree_code ccode = ERROR_MARK;
19657
19658 if (strcmp (p, "teams") == 0)
19659 ccode = OMP_TEAMS;
19660 else if (strcmp (p, "parallel") == 0)
19661 ccode = OMP_PARALLEL;
19662 else if (strcmp (p, "simd") == 0)
19663 ccode = OMP_SIMD;
19664 if (ccode != ERROR_MARK)
19665 {
19666 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
19667 char p_name[sizeof ("#pragma omp target teams distribute "
19668 "parallel for simd")];
19669
19670 c_parser_consume_token (parser);
19671 strcpy (p_name, "#pragma omp target");
19672 if (!flag_openmp) /* flag_openmp_simd */
19673 {
19674 tree stmt;
19675 switch (ccode)
19676 {
19677 case OMP_TEAMS:
19678 stmt = c_parser_omp_teams (loc, parser, p_name,
19679 OMP_TARGET_CLAUSE_MASK,
19680 cclauses, if_p);
19681 break;
19682 case OMP_PARALLEL:
19683 stmt = c_parser_omp_parallel (loc, parser, p_name,
19684 OMP_TARGET_CLAUSE_MASK,
19685 cclauses, if_p);
19686 break;
19687 case OMP_SIMD:
19688 stmt = c_parser_omp_simd (loc, parser, p_name,
19689 OMP_TARGET_CLAUSE_MASK,
19690 cclauses, if_p);
19691 break;
19692 default:
19693 gcc_unreachable ();
19694 }
19695 return stmt != NULL_TREE;
19696 }
19697 keep_next_level ();
19698 tree block = c_begin_compound_stmt (true), ret;
19699 switch (ccode)
19700 {
19701 case OMP_TEAMS:
19702 ret = c_parser_omp_teams (loc, parser, p_name,
19703 OMP_TARGET_CLAUSE_MASK, cclauses,
19704 if_p);
19705 break;
19706 case OMP_PARALLEL:
19707 ret = c_parser_omp_parallel (loc, parser, p_name,
19708 OMP_TARGET_CLAUSE_MASK, cclauses,
19709 if_p);
19710 break;
19711 case OMP_SIMD:
19712 ret = c_parser_omp_simd (loc, parser, p_name,
19713 OMP_TARGET_CLAUSE_MASK, cclauses,
19714 if_p);
19715 break;
19716 default:
19717 gcc_unreachable ();
19718 }
19719 block = c_end_compound_stmt (loc, block, true);
19720 if (ret == NULL_TREE)
19721 return false;
19722 if (ccode == OMP_TEAMS)
19723 {
19724 /* For combined target teams, ensure the num_teams and
19725 thread_limit clause expressions are evaluated on the host,
19726 before entering the target construct. */
19727 tree c;
19728 for (c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
19729 c; c = OMP_CLAUSE_CHAIN (c))
19730 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
19731 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
19732 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
19733 {
19734 tree expr = OMP_CLAUSE_OPERAND (c, 0);
19735 tree tmp = create_tmp_var_raw (TREE_TYPE (expr));
19736 expr = build4 (TARGET_EXPR, TREE_TYPE (expr), tmp,
19737 expr, NULL_TREE, NULL_TREE);
19738 add_stmt (expr);
19739 OMP_CLAUSE_OPERAND (c, 0) = expr;
19740 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
19741 OMP_CLAUSE_FIRSTPRIVATE);
19742 OMP_CLAUSE_DECL (tc) = tmp;
19743 OMP_CLAUSE_CHAIN (tc)
19744 = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
19745 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
19746 }
19747 }
19748 tree stmt = make_node (OMP_TARGET);
19749 TREE_TYPE (stmt) = void_type_node;
19750 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
19751 OMP_TARGET_BODY (stmt) = block;
19752 OMP_TARGET_COMBINED (stmt) = 1;
19753 SET_EXPR_LOCATION (stmt, loc);
19754 add_stmt (stmt);
19755 pc = &OMP_TARGET_CLAUSES (stmt);
19756 goto check_clauses;
19757 }
19758 else if (!flag_openmp) /* flag_openmp_simd */
19759 {
19760 c_parser_skip_to_pragma_eol (parser, false);
19761 return false;
19762 }
19763 else if (strcmp (p, "data") == 0)
19764 {
19765 c_parser_consume_token (parser);
19766 c_parser_omp_target_data (loc, parser, if_p);
19767 return true;
19768 }
19769 else if (strcmp (p, "enter") == 0)
19770 {
19771 c_parser_consume_token (parser);
19772 c_parser_omp_target_enter_data (loc, parser, context);
19773 return false;
19774 }
19775 else if (strcmp (p, "exit") == 0)
19776 {
19777 c_parser_consume_token (parser);
19778 c_parser_omp_target_exit_data (loc, parser, context);
19779 return false;
19780 }
19781 else if (strcmp (p, "update") == 0)
19782 {
19783 c_parser_consume_token (parser);
19784 return c_parser_omp_target_update (loc, parser, context);
19785 }
19786 }
19787 if (!flag_openmp) /* flag_openmp_simd */
19788 {
19789 c_parser_skip_to_pragma_eol (parser, false);
19790 return false;
19791 }
19792
19793 stmt = make_node (OMP_TARGET);
19794 TREE_TYPE (stmt) = void_type_node;
19795
19796 OMP_TARGET_CLAUSES (stmt)
19797 = c_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
19798 "#pragma omp target");
19799 pc = &OMP_TARGET_CLAUSES (stmt);
19800 keep_next_level ();
19801 block = c_begin_compound_stmt (true);
19802 add_stmt (c_parser_omp_structured_block (parser, if_p));
19803 OMP_TARGET_BODY (stmt) = c_end_compound_stmt (loc, block, true);
19804
19805 SET_EXPR_LOCATION (stmt, loc);
19806 add_stmt (stmt);
19807
19808 check_clauses:
19809 while (*pc)
19810 {
19811 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
19812 switch (OMP_CLAUSE_MAP_KIND (*pc))
19813 {
19814 case GOMP_MAP_TO:
19815 case GOMP_MAP_ALWAYS_TO:
19816 case GOMP_MAP_FROM:
19817 case GOMP_MAP_ALWAYS_FROM:
19818 case GOMP_MAP_TOFROM:
19819 case GOMP_MAP_ALWAYS_TOFROM:
19820 case GOMP_MAP_ALLOC:
19821 case GOMP_MAP_FIRSTPRIVATE_POINTER:
19822 case GOMP_MAP_ALWAYS_POINTER:
19823 break;
19824 default:
19825 error_at (OMP_CLAUSE_LOCATION (*pc),
19826 "%<#pragma omp target%> with map-type other "
19827 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
19828 "on %<map%> clause");
19829 *pc = OMP_CLAUSE_CHAIN (*pc);
19830 continue;
19831 }
19832 pc = &OMP_CLAUSE_CHAIN (*pc);
19833 }
19834 return true;
19835 }
19836
19837 /* OpenMP 4.0:
19838 # pragma omp declare simd declare-simd-clauses[optseq] new-line
19839
19840 OpenMP 5.0:
19841 # pragma omp declare variant (identifier) match(context-selector) new-line
19842 */
19843
19844 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
19845 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
19846 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
19847 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
19848 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
19849 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
19850 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
19851
19852 static void
19853 c_parser_omp_declare_simd (c_parser *parser, enum pragma_context context)
19854 {
19855 c_token *token = c_parser_peek_token (parser);
19856 gcc_assert (token->type == CPP_NAME);
19857 tree kind = token->value;
19858 gcc_assert (strcmp (IDENTIFIER_POINTER (kind), "simd") == 0
19859 || strcmp (IDENTIFIER_POINTER (kind), "variant") == 0);
19860
19861 auto_vec<c_token> clauses;
19862 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
19863 {
19864 c_token *token = c_parser_peek_token (parser);
19865 if (token->type == CPP_EOF)
19866 {
19867 c_parser_skip_to_pragma_eol (parser);
19868 return;
19869 }
19870 clauses.safe_push (*token);
19871 c_parser_consume_token (parser);
19872 }
19873 clauses.safe_push (*c_parser_peek_token (parser));
19874 c_parser_skip_to_pragma_eol (parser);
19875
19876 while (c_parser_next_token_is (parser, CPP_PRAGMA))
19877 {
19878 if (c_parser_peek_token (parser)->pragma_kind != PRAGMA_OMP_DECLARE
19879 || c_parser_peek_2nd_token (parser)->type != CPP_NAME
19880 || c_parser_peek_2nd_token (parser)->value != kind)
19881 {
19882 error ("%<#pragma omp declare %s%> must be followed by "
19883 "function declaration or definition or another "
19884 "%<#pragma omp declare %s%>",
19885 IDENTIFIER_POINTER (kind), IDENTIFIER_POINTER (kind));
19886 return;
19887 }
19888 c_parser_consume_pragma (parser);
19889 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
19890 {
19891 c_token *token = c_parser_peek_token (parser);
19892 if (token->type == CPP_EOF)
19893 {
19894 c_parser_skip_to_pragma_eol (parser);
19895 return;
19896 }
19897 clauses.safe_push (*token);
19898 c_parser_consume_token (parser);
19899 }
19900 clauses.safe_push (*c_parser_peek_token (parser));
19901 c_parser_skip_to_pragma_eol (parser);
19902 }
19903
19904 /* Make sure nothing tries to read past the end of the tokens. */
19905 c_token eof_token;
19906 memset (&eof_token, 0, sizeof (eof_token));
19907 eof_token.type = CPP_EOF;
19908 clauses.safe_push (eof_token);
19909 clauses.safe_push (eof_token);
19910
19911 switch (context)
19912 {
19913 case pragma_external:
19914 if (c_parser_next_token_is (parser, CPP_KEYWORD)
19915 && c_parser_peek_token (parser)->keyword == RID_EXTENSION)
19916 {
19917 int ext = disable_extension_diagnostics ();
19918 do
19919 c_parser_consume_token (parser);
19920 while (c_parser_next_token_is (parser, CPP_KEYWORD)
19921 && c_parser_peek_token (parser)->keyword == RID_EXTENSION);
19922 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
19923 NULL, clauses);
19924 restore_extension_diagnostics (ext);
19925 }
19926 else
19927 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
19928 NULL, clauses);
19929 break;
19930 case pragma_struct:
19931 case pragma_param:
19932 case pragma_stmt:
19933 error ("%<#pragma omp declare %s%> must be followed by "
19934 "function declaration or definition",
19935 IDENTIFIER_POINTER (kind));
19936 break;
19937 case pragma_compound:
19938 if (c_parser_next_token_is (parser, CPP_KEYWORD)
19939 && c_parser_peek_token (parser)->keyword == RID_EXTENSION)
19940 {
19941 int ext = disable_extension_diagnostics ();
19942 do
19943 c_parser_consume_token (parser);
19944 while (c_parser_next_token_is (parser, CPP_KEYWORD)
19945 && c_parser_peek_token (parser)->keyword == RID_EXTENSION);
19946 if (c_parser_next_tokens_start_declaration (parser))
19947 {
19948 c_parser_declaration_or_fndef (parser, true, true, true, true,
19949 true, NULL, clauses);
19950 restore_extension_diagnostics (ext);
19951 break;
19952 }
19953 restore_extension_diagnostics (ext);
19954 }
19955 else if (c_parser_next_tokens_start_declaration (parser))
19956 {
19957 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
19958 NULL, clauses);
19959 break;
19960 }
19961 error ("%<#pragma omp declare %s%> must be followed by "
19962 "function declaration or definition",
19963 IDENTIFIER_POINTER (kind));
19964 break;
19965 default:
19966 gcc_unreachable ();
19967 }
19968 }
19969
19970 static const char *const omp_construct_selectors[] = {
19971 "simd", "target", "teams", "parallel", "for", NULL };
19972 static const char *const omp_device_selectors[] = {
19973 "kind", "isa", "arch", NULL };
19974 static const char *const omp_implementation_selectors[] = {
19975 "vendor", "extension", "atomic_default_mem_order", "unified_address",
19976 "unified_shared_memory", "dynamic_allocators", "reverse_offload", NULL };
19977 static const char *const omp_user_selectors[] = {
19978 "condition", NULL };
19979
19980 /* OpenMP 5.0:
19981
19982 trait-selector:
19983 trait-selector-name[([trait-score:]trait-property[,trait-property[,...]])]
19984
19985 trait-score:
19986 score(score-expression) */
19987
19988 static tree
19989 c_parser_omp_context_selector (c_parser *parser, tree set, tree parms)
19990 {
19991 tree ret = NULL_TREE;
19992 do
19993 {
19994 tree selector;
19995 if (c_parser_next_token_is (parser, CPP_KEYWORD)
19996 || c_parser_next_token_is (parser, CPP_NAME))
19997 selector = c_parser_peek_token (parser)->value;
19998 else
19999 {
20000 c_parser_error (parser, "expected trait selector name");
20001 return error_mark_node;
20002 }
20003
20004 tree properties = NULL_TREE;
20005 const char *const *selectors = NULL;
20006 bool allow_score = true;
20007 bool allow_user = false;
20008 int property_limit = 0;
20009 enum { CTX_PROPERTY_NONE, CTX_PROPERTY_USER, CTX_PROPERTY_NAME_LIST,
20010 CTX_PROPERTY_ID, CTX_PROPERTY_EXPR,
20011 CTX_PROPERTY_SIMD } property_kind = CTX_PROPERTY_NONE;
20012 switch (IDENTIFIER_POINTER (set)[0])
20013 {
20014 case 'c': /* construct */
20015 selectors = omp_construct_selectors;
20016 allow_score = false;
20017 property_limit = 1;
20018 property_kind = CTX_PROPERTY_SIMD;
20019 break;
20020 case 'd': /* device */
20021 selectors = omp_device_selectors;
20022 allow_score = false;
20023 allow_user = true;
20024 property_limit = 3;
20025 property_kind = CTX_PROPERTY_NAME_LIST;
20026 break;
20027 case 'i': /* implementation */
20028 selectors = omp_implementation_selectors;
20029 allow_user = true;
20030 property_limit = 3;
20031 property_kind = CTX_PROPERTY_NAME_LIST;
20032 break;
20033 case 'u': /* user */
20034 selectors = omp_user_selectors;
20035 property_limit = 1;
20036 property_kind = CTX_PROPERTY_EXPR;
20037 break;
20038 default:
20039 gcc_unreachable ();
20040 }
20041 for (int i = 0; ; i++)
20042 {
20043 if (selectors[i] == NULL)
20044 {
20045 if (allow_user)
20046 {
20047 property_kind = CTX_PROPERTY_USER;
20048 break;
20049 }
20050 else
20051 {
20052 error_at (c_parser_peek_token (parser)->location,
20053 "selector %qs not allowed for context selector "
20054 "set %qs", IDENTIFIER_POINTER (selector),
20055 IDENTIFIER_POINTER (set));
20056 c_parser_consume_token (parser);
20057 return error_mark_node;
20058 }
20059 }
20060 if (i == property_limit)
20061 property_kind = CTX_PROPERTY_NONE;
20062 if (strcmp (selectors[i], IDENTIFIER_POINTER (selector)) == 0)
20063 break;
20064 }
20065 if (property_kind == CTX_PROPERTY_NAME_LIST
20066 && IDENTIFIER_POINTER (set)[0] == 'i'
20067 && strcmp (IDENTIFIER_POINTER (selector),
20068 "atomic_default_mem_order") == 0)
20069 property_kind = CTX_PROPERTY_ID;
20070
20071 c_parser_consume_token (parser);
20072
20073 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
20074 {
20075 if (property_kind == CTX_PROPERTY_NONE)
20076 {
20077 error_at (c_parser_peek_token (parser)->location,
20078 "selector %qs does not accept any properties",
20079 IDENTIFIER_POINTER (selector));
20080 return error_mark_node;
20081 }
20082
20083 matching_parens parens;
20084 parens.require_open (parser);
20085
20086 c_token *token = c_parser_peek_token (parser);
20087 if (allow_score
20088 && c_parser_next_token_is (parser, CPP_NAME)
20089 && strcmp (IDENTIFIER_POINTER (token->value), "score") == 0
20090 && c_parser_peek_2nd_token (parser)->type == CPP_OPEN_PAREN)
20091 {
20092 c_parser_consume_token (parser);
20093
20094 matching_parens parens2;
20095 parens2.require_open (parser);
20096 tree score = c_parser_expr_no_commas (parser, NULL).value;
20097 parens2.skip_until_found_close (parser);
20098 c_parser_require (parser, CPP_COLON, "expected %<:%>");
20099 if (score != error_mark_node)
20100 {
20101 mark_exp_read (score);
20102 score = c_fully_fold (score, false, NULL);
20103 if (!INTEGRAL_TYPE_P (TREE_TYPE (score))
20104 || TREE_CODE (score) != INTEGER_CST)
20105 error_at (token->location, "score argument must be "
20106 "constant integer expression");
20107 else if (tree_int_cst_sgn (score) < 0)
20108 error_at (token->location, "score argument must be "
20109 "non-negative");
20110 else
20111 properties = tree_cons (get_identifier (" score"),
20112 score, properties);
20113 }
20114 token = c_parser_peek_token (parser);
20115 }
20116
20117 switch (property_kind)
20118 {
20119 tree t;
20120 case CTX_PROPERTY_USER:
20121 do
20122 {
20123 t = c_parser_expr_no_commas (parser, NULL).value;
20124 if (TREE_CODE (t) == STRING_CST)
20125 properties = tree_cons (NULL_TREE, t, properties);
20126 else if (t != error_mark_node)
20127 {
20128 mark_exp_read (t);
20129 t = c_fully_fold (t, false, NULL);
20130 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
20131 || !tree_fits_shwi_p (t))
20132 error_at (token->location, "property must be "
20133 "constant integer expression or string "
20134 "literal");
20135 else
20136 properties = tree_cons (NULL_TREE, t, properties);
20137 }
20138 else
20139 return error_mark_node;
20140
20141 if (c_parser_next_token_is (parser, CPP_COMMA))
20142 c_parser_consume_token (parser);
20143 else
20144 break;
20145 }
20146 while (1);
20147 break;
20148 case CTX_PROPERTY_ID:
20149 if (c_parser_next_token_is (parser, CPP_KEYWORD)
20150 || c_parser_next_token_is (parser, CPP_NAME))
20151 {
20152 tree prop = c_parser_peek_token (parser)->value;
20153 c_parser_consume_token (parser);
20154 properties = tree_cons (prop, NULL_TREE, properties);
20155 }
20156 else
20157 {
20158 c_parser_error (parser, "expected identifier");
20159 return error_mark_node;
20160 }
20161 break;
20162 case CTX_PROPERTY_NAME_LIST:
20163 do
20164 {
20165 tree prop = NULL_TREE, value = NULL_TREE;
20166 if (c_parser_next_token_is (parser, CPP_KEYWORD)
20167 || c_parser_next_token_is (parser, CPP_NAME))
20168 {
20169 prop = c_parser_peek_token (parser)->value;
20170 c_parser_consume_token (parser);
20171 }
20172 else if (c_parser_next_token_is (parser, CPP_STRING))
20173 value = c_parser_string_literal (parser, false,
20174 false).value;
20175 else
20176 {
20177 c_parser_error (parser, "expected identifier or "
20178 "string literal");
20179 return error_mark_node;
20180 }
20181
20182 properties = tree_cons (prop, value, properties);
20183
20184 if (c_parser_next_token_is (parser, CPP_COMMA))
20185 c_parser_consume_token (parser);
20186 else
20187 break;
20188 }
20189 while (1);
20190 break;
20191 case CTX_PROPERTY_EXPR:
20192 t = c_parser_expr_no_commas (parser, NULL).value;
20193 if (t != error_mark_node)
20194 {
20195 mark_exp_read (t);
20196 t = c_fully_fold (t, false, NULL);
20197 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
20198 || !tree_fits_shwi_p (t))
20199 error_at (token->location, "property must be "
20200 "constant integer expression");
20201 else
20202 properties = tree_cons (NULL_TREE, t, properties);
20203 }
20204 else
20205 return error_mark_node;
20206 break;
20207 case CTX_PROPERTY_SIMD:
20208 if (parms == NULL_TREE)
20209 {
20210 error_at (token->location, "properties for %<simd%> "
20211 "selector may not be specified in "
20212 "%<metadirective%>");
20213 return error_mark_node;
20214 }
20215 tree c;
20216 c = c_parser_omp_all_clauses (parser,
20217 OMP_DECLARE_SIMD_CLAUSE_MASK,
20218 "simd", true, 2);
20219 c = c_omp_declare_simd_clauses_to_numbers (parms
20220 == error_mark_node
20221 ? NULL_TREE : parms,
20222 c);
20223 properties = c;
20224 break;
20225 default:
20226 gcc_unreachable ();
20227 }
20228
20229 parens.skip_until_found_close (parser);
20230 properties = nreverse (properties);
20231 }
20232 else if (property_kind == CTX_PROPERTY_NAME_LIST
20233 || property_kind == CTX_PROPERTY_ID
20234 || property_kind == CTX_PROPERTY_EXPR)
20235 {
20236 c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>");
20237 return error_mark_node;
20238 }
20239
20240 ret = tree_cons (selector, properties, ret);
20241
20242 if (c_parser_next_token_is (parser, CPP_COMMA))
20243 c_parser_consume_token (parser);
20244 else
20245 break;
20246 }
20247 while (1);
20248
20249 return nreverse (ret);
20250 }
20251
20252 /* OpenMP 5.0:
20253
20254 trait-set-selector[,trait-set-selector[,...]]
20255
20256 trait-set-selector:
20257 trait-set-selector-name = { trait-selector[, trait-selector[, ...]] }
20258
20259 trait-set-selector-name:
20260 constructor
20261 device
20262 implementation
20263 user */
20264
20265 static tree
20266 c_parser_omp_context_selector_specification (c_parser *parser, tree parms)
20267 {
20268 tree ret = NULL_TREE;
20269 do
20270 {
20271 const char *setp = "";
20272 if (c_parser_next_token_is (parser, CPP_NAME))
20273 setp = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
20274 switch (setp[0])
20275 {
20276 case 'c':
20277 if (strcmp (setp, "construct") == 0)
20278 setp = NULL;
20279 break;
20280 case 'd':
20281 if (strcmp (setp, "device") == 0)
20282 setp = NULL;
20283 break;
20284 case 'i':
20285 if (strcmp (setp, "implementation") == 0)
20286 setp = NULL;
20287 break;
20288 case 'u':
20289 if (strcmp (setp, "user") == 0)
20290 setp = NULL;
20291 break;
20292 default:
20293 break;
20294 }
20295 if (setp)
20296 {
20297 c_parser_error (parser, "expected %<construct%>, %<device%>, "
20298 "%<implementation%> or %<user%>");
20299 return error_mark_node;
20300 }
20301
20302 tree set = c_parser_peek_token (parser)->value;
20303 c_parser_consume_token (parser);
20304
20305 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
20306 return error_mark_node;
20307
20308 matching_braces braces;
20309 if (!braces.require_open (parser))
20310 return error_mark_node;
20311
20312 tree selectors = c_parser_omp_context_selector (parser, set, parms);
20313 if (selectors == error_mark_node)
20314 ret = error_mark_node;
20315 else if (ret != error_mark_node)
20316 ret = tree_cons (set, selectors, ret);
20317
20318 braces.skip_until_found_close (parser);
20319
20320 if (c_parser_next_token_is (parser, CPP_COMMA))
20321 c_parser_consume_token (parser);
20322 else
20323 break;
20324 }
20325 while (1);
20326
20327 if (ret == error_mark_node)
20328 return ret;
20329 return nreverse (ret);
20330 }
20331
20332 /* Finalize #pragma omp declare variant after FNDECL has been parsed, and put
20333 that into "omp declare variant base" attribute. */
20334
20335 static void
20336 c_finish_omp_declare_variant (c_parser *parser, tree fndecl, tree parms)
20337 {
20338 matching_parens parens;
20339 if (!parens.require_open (parser))
20340 {
20341 fail:
20342 c_parser_skip_to_pragma_eol (parser, false);
20343 return;
20344 }
20345
20346 if (c_parser_next_token_is_not (parser, CPP_NAME)
20347 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
20348 {
20349 c_parser_error (parser, "expected identifier");
20350 goto fail;
20351 }
20352
20353 c_token *token = c_parser_peek_token (parser);
20354 tree variant = lookup_name (token->value);
20355
20356 if (variant == NULL_TREE)
20357 {
20358 undeclared_variable (token->location, token->value);
20359 variant = error_mark_node;
20360 }
20361
20362 c_parser_consume_token (parser);
20363
20364 parens.require_close (parser);
20365
20366 const char *clause = "";
20367 location_t match_loc = c_parser_peek_token (parser)->location;
20368 if (c_parser_next_token_is (parser, CPP_NAME))
20369 clause = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
20370 if (strcmp (clause, "match"))
20371 {
20372 c_parser_error (parser, "expected %<match%>");
20373 goto fail;
20374 }
20375
20376 c_parser_consume_token (parser);
20377
20378 if (!parens.require_open (parser))
20379 goto fail;
20380
20381 if (parms == NULL_TREE)
20382 parms = error_mark_node;
20383
20384 tree ctx = c_parser_omp_context_selector_specification (parser, parms);
20385 if (ctx == error_mark_node)
20386 goto fail;
20387 ctx = c_omp_check_context_selector (match_loc, ctx);
20388 if (ctx != error_mark_node && variant != error_mark_node)
20389 {
20390 if (TREE_CODE (variant) != FUNCTION_DECL)
20391 {
20392 error_at (token->location, "variant %qD is not a function", variant);
20393 variant = error_mark_node;
20394 }
20395 else if (omp_get_context_selector (ctx, "construct", "simd") == NULL_TREE
20396 && !comptypes (TREE_TYPE (fndecl), TREE_TYPE (variant)))
20397 {
20398 error_at (token->location, "variant %qD and base %qD have "
20399 "incompatible types", variant, fndecl);
20400 variant = error_mark_node;
20401 }
20402 else if (fndecl_built_in_p (variant)
20403 && (strncmp (IDENTIFIER_POINTER (DECL_NAME (variant)),
20404 "__builtin_", strlen ("__builtin_")) == 0
20405 || strncmp (IDENTIFIER_POINTER (DECL_NAME (variant)),
20406 "__sync_", strlen ("__sync_")) == 0
20407 || strncmp (IDENTIFIER_POINTER (DECL_NAME (variant)),
20408 "__atomic_", strlen ("__atomic_")) == 0))
20409 {
20410 error_at (token->location, "variant %qD is a built-in", variant);
20411 variant = error_mark_node;
20412 }
20413 if (variant != error_mark_node)
20414 {
20415 C_DECL_USED (variant) = 1;
20416 tree construct = omp_get_context_selector (ctx, "construct", NULL);
20417 c_omp_mark_declare_variant (match_loc, variant, construct);
20418 if (omp_context_selector_matches (ctx))
20419 {
20420 tree attr
20421 = tree_cons (get_identifier ("omp declare variant base"),
20422 build_tree_list (variant, ctx),
20423 DECL_ATTRIBUTES (fndecl));
20424 DECL_ATTRIBUTES (fndecl) = attr;
20425 }
20426 }
20427 }
20428
20429 parens.require_close (parser);
20430 c_parser_skip_to_pragma_eol (parser);
20431 }
20432
20433 /* Finalize #pragma omp declare simd or #pragma omp declare variant
20434 clauses after FNDECL has been parsed, and put that into "omp declare simd"
20435 or "omp declare variant base" attribute. */
20436
20437 static void
20438 c_finish_omp_declare_simd (c_parser *parser, tree fndecl, tree parms,
20439 vec<c_token> clauses)
20440 {
20441 /* Normally first token is CPP_NAME "simd" or "variant". CPP_EOF there
20442 indicates error has been reported and CPP_PRAGMA that
20443 c_finish_omp_declare_simd has already processed the tokens. */
20444 if (clauses.exists () && clauses[0].type == CPP_EOF)
20445 return;
20446 const char *kind = "simd";
20447 if (clauses.exists ()
20448 && (clauses[0].type == CPP_NAME || clauses[0].type == CPP_PRAGMA))
20449 kind = IDENTIFIER_POINTER (clauses[0].value);
20450 gcc_assert (strcmp (kind, "simd") == 0 || strcmp (kind, "variant") == 0);
20451 if (fndecl == NULL_TREE || TREE_CODE (fndecl) != FUNCTION_DECL)
20452 {
20453 error ("%<#pragma omp declare %s%> not immediately followed by "
20454 "a function declaration or definition", kind);
20455 clauses[0].type = CPP_EOF;
20456 return;
20457 }
20458 if (clauses.exists () && clauses[0].type != CPP_NAME)
20459 {
20460 error_at (DECL_SOURCE_LOCATION (fndecl),
20461 "%<#pragma omp declare %s%> not immediately followed by "
20462 "a single function declaration or definition", kind);
20463 clauses[0].type = CPP_EOF;
20464 return;
20465 }
20466
20467 if (parms == NULL_TREE)
20468 parms = DECL_ARGUMENTS (fndecl);
20469
20470 unsigned int tokens_avail = parser->tokens_avail;
20471 gcc_assert (parser->tokens == &parser->tokens_buf[0]);
20472
20473 parser->tokens = clauses.address ();
20474 parser->tokens_avail = clauses.length ();
20475
20476 /* c_parser_omp_declare_simd pushed 2 extra CPP_EOF tokens at the end. */
20477 while (parser->tokens_avail > 3)
20478 {
20479 c_token *token = c_parser_peek_token (parser);
20480 gcc_assert (token->type == CPP_NAME
20481 && strcmp (IDENTIFIER_POINTER (token->value), kind) == 0);
20482 c_parser_consume_token (parser);
20483 parser->in_pragma = true;
20484
20485 if (strcmp (kind, "simd") == 0)
20486 {
20487 tree c;
20488 c = c_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
20489 "#pragma omp declare simd");
20490 c = c_omp_declare_simd_clauses_to_numbers (parms, c);
20491 if (c != NULL_TREE)
20492 c = tree_cons (NULL_TREE, c, NULL_TREE);
20493 c = build_tree_list (get_identifier ("omp declare simd"), c);
20494 TREE_CHAIN (c) = DECL_ATTRIBUTES (fndecl);
20495 DECL_ATTRIBUTES (fndecl) = c;
20496 }
20497 else
20498 {
20499 gcc_assert (strcmp (kind, "variant") == 0);
20500 c_finish_omp_declare_variant (parser, fndecl, parms);
20501 }
20502 }
20503
20504 parser->tokens = &parser->tokens_buf[0];
20505 parser->tokens_avail = tokens_avail;
20506 if (clauses.exists ())
20507 clauses[0].type = CPP_PRAGMA;
20508 }
20509
20510
20511 /* OpenMP 4.0:
20512 # pragma omp declare target new-line
20513 declarations and definitions
20514 # pragma omp end declare target new-line
20515
20516 OpenMP 4.5:
20517 # pragma omp declare target ( extended-list ) new-line
20518
20519 # pragma omp declare target declare-target-clauses[seq] new-line */
20520
20521 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
20522 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
20523 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK) \
20524 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE_TYPE))
20525
20526 static void
20527 c_parser_omp_declare_target (c_parser *parser)
20528 {
20529 tree clauses = NULL_TREE;
20530 int device_type = 0;
20531 bool only_device_type = true;
20532 if (c_parser_next_token_is (parser, CPP_NAME))
20533 clauses = c_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
20534 "#pragma omp declare target");
20535 else if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
20536 {
20537 clauses = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_TO_DECLARE,
20538 clauses);
20539 clauses = c_finish_omp_clauses (clauses, C_ORT_OMP);
20540 c_parser_skip_to_pragma_eol (parser);
20541 }
20542 else
20543 {
20544 c_parser_skip_to_pragma_eol (parser);
20545 current_omp_declare_target_attribute++;
20546 return;
20547 }
20548 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
20549 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEVICE_TYPE)
20550 device_type |= OMP_CLAUSE_DEVICE_TYPE_KIND (c);
20551 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
20552 {
20553 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEVICE_TYPE)
20554 continue;
20555 tree t = OMP_CLAUSE_DECL (c), id;
20556 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
20557 tree at2 = lookup_attribute ("omp declare target link",
20558 DECL_ATTRIBUTES (t));
20559 only_device_type = false;
20560 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
20561 {
20562 id = get_identifier ("omp declare target link");
20563 std::swap (at1, at2);
20564 }
20565 else
20566 id = get_identifier ("omp declare target");
20567 if (at2)
20568 {
20569 error_at (OMP_CLAUSE_LOCATION (c),
20570 "%qD specified both in declare target %<link%> and %<to%>"
20571 " clauses", t);
20572 continue;
20573 }
20574 if (!at1)
20575 {
20576 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
20577 if (TREE_CODE (t) != FUNCTION_DECL && !is_global_var (t))
20578 continue;
20579
20580 symtab_node *node = symtab_node::get (t);
20581 if (node != NULL)
20582 {
20583 node->offloadable = 1;
20584 if (ENABLE_OFFLOADING)
20585 {
20586 g->have_offload = true;
20587 if (is_a <varpool_node *> (node))
20588 vec_safe_push (offload_vars, t);
20589 }
20590 }
20591 }
20592 if (TREE_CODE (t) != FUNCTION_DECL)
20593 continue;
20594 if ((device_type & OMP_CLAUSE_DEVICE_TYPE_HOST) != 0)
20595 {
20596 tree at3 = lookup_attribute ("omp declare target host",
20597 DECL_ATTRIBUTES (t));
20598 if (at3 == NULL_TREE)
20599 {
20600 id = get_identifier ("omp declare target host");
20601 DECL_ATTRIBUTES (t)
20602 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
20603 }
20604 }
20605 if ((device_type & OMP_CLAUSE_DEVICE_TYPE_NOHOST) != 0)
20606 {
20607 tree at3 = lookup_attribute ("omp declare target nohost",
20608 DECL_ATTRIBUTES (t));
20609 if (at3 == NULL_TREE)
20610 {
20611 id = get_identifier ("omp declare target nohost");
20612 DECL_ATTRIBUTES (t)
20613 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
20614 }
20615 }
20616 }
20617 if (device_type && only_device_type)
20618 warning_at (OMP_CLAUSE_LOCATION (clauses), 0,
20619 "directive with only %<device_type%> clauses ignored");
20620 }
20621
20622 static void
20623 c_parser_omp_end_declare_target (c_parser *parser)
20624 {
20625 location_t loc = c_parser_peek_token (parser)->location;
20626 c_parser_consume_pragma (parser);
20627 if (c_parser_next_token_is (parser, CPP_NAME)
20628 && strcmp (IDENTIFIER_POINTER (c_parser_peek_token (parser)->value),
20629 "declare") == 0)
20630 {
20631 c_parser_consume_token (parser);
20632 if (c_parser_next_token_is (parser, CPP_NAME)
20633 && strcmp (IDENTIFIER_POINTER (c_parser_peek_token (parser)->value),
20634 "target") == 0)
20635 c_parser_consume_token (parser);
20636 else
20637 {
20638 c_parser_error (parser, "expected %<target%>");
20639 c_parser_skip_to_pragma_eol (parser);
20640 return;
20641 }
20642 }
20643 else
20644 {
20645 c_parser_error (parser, "expected %<declare%>");
20646 c_parser_skip_to_pragma_eol (parser);
20647 return;
20648 }
20649 c_parser_skip_to_pragma_eol (parser);
20650 if (!current_omp_declare_target_attribute)
20651 error_at (loc, "%<#pragma omp end declare target%> without corresponding "
20652 "%<#pragma omp declare target%>");
20653 else
20654 current_omp_declare_target_attribute--;
20655 }
20656
20657
20658 /* OpenMP 4.0
20659 #pragma omp declare reduction (reduction-id : typename-list : expression) \
20660 initializer-clause[opt] new-line
20661
20662 initializer-clause:
20663 initializer (omp_priv = initializer)
20664 initializer (function-name (argument-list)) */
20665
20666 static void
20667 c_parser_omp_declare_reduction (c_parser *parser, enum pragma_context context)
20668 {
20669 unsigned int tokens_avail = 0, i;
20670 vec<tree> types = vNULL;
20671 vec<c_token> clauses = vNULL;
20672 enum tree_code reduc_code = ERROR_MARK;
20673 tree reduc_id = NULL_TREE;
20674 tree type;
20675 location_t rloc = c_parser_peek_token (parser)->location;
20676
20677 if (context == pragma_struct || context == pragma_param)
20678 {
20679 error ("%<#pragma omp declare reduction%> not at file or block scope");
20680 goto fail;
20681 }
20682
20683 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
20684 goto fail;
20685
20686 switch (c_parser_peek_token (parser)->type)
20687 {
20688 case CPP_PLUS:
20689 reduc_code = PLUS_EXPR;
20690 break;
20691 case CPP_MULT:
20692 reduc_code = MULT_EXPR;
20693 break;
20694 case CPP_MINUS:
20695 reduc_code = MINUS_EXPR;
20696 break;
20697 case CPP_AND:
20698 reduc_code = BIT_AND_EXPR;
20699 break;
20700 case CPP_XOR:
20701 reduc_code = BIT_XOR_EXPR;
20702 break;
20703 case CPP_OR:
20704 reduc_code = BIT_IOR_EXPR;
20705 break;
20706 case CPP_AND_AND:
20707 reduc_code = TRUTH_ANDIF_EXPR;
20708 break;
20709 case CPP_OR_OR:
20710 reduc_code = TRUTH_ORIF_EXPR;
20711 break;
20712 case CPP_NAME:
20713 const char *p;
20714 p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
20715 if (strcmp (p, "min") == 0)
20716 {
20717 reduc_code = MIN_EXPR;
20718 break;
20719 }
20720 if (strcmp (p, "max") == 0)
20721 {
20722 reduc_code = MAX_EXPR;
20723 break;
20724 }
20725 reduc_id = c_parser_peek_token (parser)->value;
20726 break;
20727 default:
20728 c_parser_error (parser,
20729 "expected %<+%>, %<*%>, %<-%>, %<&%>, "
20730 "%<^%>, %<|%>, %<&&%>, %<||%> or identifier");
20731 goto fail;
20732 }
20733
20734 tree orig_reduc_id, reduc_decl;
20735 orig_reduc_id = reduc_id;
20736 reduc_id = c_omp_reduction_id (reduc_code, reduc_id);
20737 reduc_decl = c_omp_reduction_decl (reduc_id);
20738 c_parser_consume_token (parser);
20739
20740 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
20741 goto fail;
20742
20743 while (true)
20744 {
20745 location_t loc = c_parser_peek_token (parser)->location;
20746 struct c_type_name *ctype = c_parser_type_name (parser);
20747 if (ctype != NULL)
20748 {
20749 type = groktypename (ctype, NULL, NULL);
20750 if (type == error_mark_node)
20751 ;
20752 else if ((INTEGRAL_TYPE_P (type)
20753 || TREE_CODE (type) == REAL_TYPE
20754 || TREE_CODE (type) == COMPLEX_TYPE)
20755 && orig_reduc_id == NULL_TREE)
20756 error_at (loc, "predeclared arithmetic type in "
20757 "%<#pragma omp declare reduction%>");
20758 else if (TREE_CODE (type) == FUNCTION_TYPE
20759 || TREE_CODE (type) == ARRAY_TYPE)
20760 error_at (loc, "function or array type in "
20761 "%<#pragma omp declare reduction%>");
20762 else if (TYPE_ATOMIC (type))
20763 error_at (loc, "%<_Atomic%> qualified type in "
20764 "%<#pragma omp declare reduction%>");
20765 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
20766 error_at (loc, "const, volatile or restrict qualified type in "
20767 "%<#pragma omp declare reduction%>");
20768 else
20769 {
20770 tree t;
20771 for (t = DECL_INITIAL (reduc_decl); t; t = TREE_CHAIN (t))
20772 if (comptypes (TREE_PURPOSE (t), type))
20773 {
20774 error_at (loc, "redeclaration of %qs "
20775 "%<#pragma omp declare reduction%> for "
20776 "type %qT",
20777 IDENTIFIER_POINTER (reduc_id)
20778 + sizeof ("omp declare reduction ") - 1,
20779 type);
20780 location_t ploc
20781 = DECL_SOURCE_LOCATION (TREE_VEC_ELT (TREE_VALUE (t),
20782 0));
20783 error_at (ploc, "previous %<#pragma omp declare "
20784 "reduction%>");
20785 break;
20786 }
20787 if (t == NULL_TREE)
20788 types.safe_push (type);
20789 }
20790 if (c_parser_next_token_is (parser, CPP_COMMA))
20791 c_parser_consume_token (parser);
20792 else
20793 break;
20794 }
20795 else
20796 break;
20797 }
20798
20799 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>")
20800 || types.is_empty ())
20801 {
20802 fail:
20803 clauses.release ();
20804 types.release ();
20805 while (true)
20806 {
20807 c_token *token = c_parser_peek_token (parser);
20808 if (token->type == CPP_EOF || token->type == CPP_PRAGMA_EOL)
20809 break;
20810 c_parser_consume_token (parser);
20811 }
20812 c_parser_skip_to_pragma_eol (parser);
20813 return;
20814 }
20815
20816 if (types.length () > 1)
20817 {
20818 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
20819 {
20820 c_token *token = c_parser_peek_token (parser);
20821 if (token->type == CPP_EOF)
20822 goto fail;
20823 clauses.safe_push (*token);
20824 c_parser_consume_token (parser);
20825 }
20826 clauses.safe_push (*c_parser_peek_token (parser));
20827 c_parser_skip_to_pragma_eol (parser);
20828
20829 /* Make sure nothing tries to read past the end of the tokens. */
20830 c_token eof_token;
20831 memset (&eof_token, 0, sizeof (eof_token));
20832 eof_token.type = CPP_EOF;
20833 clauses.safe_push (eof_token);
20834 clauses.safe_push (eof_token);
20835 }
20836
20837 int errs = errorcount;
20838 FOR_EACH_VEC_ELT (types, i, type)
20839 {
20840 tokens_avail = parser->tokens_avail;
20841 gcc_assert (parser->tokens == &parser->tokens_buf[0]);
20842 if (!clauses.is_empty ())
20843 {
20844 parser->tokens = clauses.address ();
20845 parser->tokens_avail = clauses.length ();
20846 parser->in_pragma = true;
20847 }
20848
20849 bool nested = current_function_decl != NULL_TREE;
20850 if (nested)
20851 c_push_function_context ();
20852 tree fndecl = build_decl (BUILTINS_LOCATION, FUNCTION_DECL,
20853 reduc_id, default_function_type);
20854 current_function_decl = fndecl;
20855 allocate_struct_function (fndecl, true);
20856 push_scope ();
20857 tree stmt = push_stmt_list ();
20858 /* Intentionally BUILTINS_LOCATION, so that -Wshadow doesn't
20859 warn about these. */
20860 tree omp_out = build_decl (BUILTINS_LOCATION, VAR_DECL,
20861 get_identifier ("omp_out"), type);
20862 DECL_ARTIFICIAL (omp_out) = 1;
20863 DECL_CONTEXT (omp_out) = fndecl;
20864 pushdecl (omp_out);
20865 tree omp_in = build_decl (BUILTINS_LOCATION, VAR_DECL,
20866 get_identifier ("omp_in"), type);
20867 DECL_ARTIFICIAL (omp_in) = 1;
20868 DECL_CONTEXT (omp_in) = fndecl;
20869 pushdecl (omp_in);
20870 struct c_expr combiner = c_parser_expression (parser);
20871 struct c_expr initializer;
20872 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE;
20873 bool bad = false;
20874 initializer.set_error ();
20875 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
20876 bad = true;
20877 else if (c_parser_next_token_is (parser, CPP_NAME)
20878 && strcmp (IDENTIFIER_POINTER
20879 (c_parser_peek_token (parser)->value),
20880 "initializer") == 0)
20881 {
20882 c_parser_consume_token (parser);
20883 pop_scope ();
20884 push_scope ();
20885 omp_priv = build_decl (BUILTINS_LOCATION, VAR_DECL,
20886 get_identifier ("omp_priv"), type);
20887 DECL_ARTIFICIAL (omp_priv) = 1;
20888 DECL_INITIAL (omp_priv) = error_mark_node;
20889 DECL_CONTEXT (omp_priv) = fndecl;
20890 pushdecl (omp_priv);
20891 omp_orig = build_decl (BUILTINS_LOCATION, VAR_DECL,
20892 get_identifier ("omp_orig"), type);
20893 DECL_ARTIFICIAL (omp_orig) = 1;
20894 DECL_CONTEXT (omp_orig) = fndecl;
20895 pushdecl (omp_orig);
20896 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
20897 bad = true;
20898 else if (!c_parser_next_token_is (parser, CPP_NAME))
20899 {
20900 c_parser_error (parser, "expected %<omp_priv%> or "
20901 "function-name");
20902 bad = true;
20903 }
20904 else if (strcmp (IDENTIFIER_POINTER
20905 (c_parser_peek_token (parser)->value),
20906 "omp_priv") != 0)
20907 {
20908 if (c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN
20909 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
20910 {
20911 c_parser_error (parser, "expected function-name %<(%>");
20912 bad = true;
20913 }
20914 else
20915 initializer = c_parser_postfix_expression (parser);
20916 if (initializer.value
20917 && TREE_CODE (initializer.value) == CALL_EXPR)
20918 {
20919 int j;
20920 tree c = initializer.value;
20921 for (j = 0; j < call_expr_nargs (c); j++)
20922 {
20923 tree a = CALL_EXPR_ARG (c, j);
20924 STRIP_NOPS (a);
20925 if (TREE_CODE (a) == ADDR_EXPR
20926 && TREE_OPERAND (a, 0) == omp_priv)
20927 break;
20928 }
20929 if (j == call_expr_nargs (c))
20930 error ("one of the initializer call arguments should be "
20931 "%<&omp_priv%>");
20932 }
20933 }
20934 else
20935 {
20936 c_parser_consume_token (parser);
20937 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
20938 bad = true;
20939 else
20940 {
20941 tree st = push_stmt_list ();
20942 location_t loc = c_parser_peek_token (parser)->location;
20943 rich_location richloc (line_table, loc);
20944 start_init (omp_priv, NULL_TREE, 0, &richloc);
20945 struct c_expr init = c_parser_initializer (parser);
20946 finish_init ();
20947 finish_decl (omp_priv, loc, init.value,
20948 init.original_type, NULL_TREE);
20949 pop_stmt_list (st);
20950 }
20951 }
20952 if (!bad
20953 && !c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
20954 bad = true;
20955 }
20956
20957 if (!bad)
20958 {
20959 c_parser_skip_to_pragma_eol (parser);
20960
20961 tree t = tree_cons (type, make_tree_vec (omp_priv ? 6 : 3),
20962 DECL_INITIAL (reduc_decl));
20963 DECL_INITIAL (reduc_decl) = t;
20964 DECL_SOURCE_LOCATION (omp_out) = rloc;
20965 TREE_VEC_ELT (TREE_VALUE (t), 0) = omp_out;
20966 TREE_VEC_ELT (TREE_VALUE (t), 1) = omp_in;
20967 TREE_VEC_ELT (TREE_VALUE (t), 2) = combiner.value;
20968 walk_tree (&combiner.value, c_check_omp_declare_reduction_r,
20969 &TREE_VEC_ELT (TREE_VALUE (t), 0), NULL);
20970 if (omp_priv)
20971 {
20972 DECL_SOURCE_LOCATION (omp_priv) = rloc;
20973 TREE_VEC_ELT (TREE_VALUE (t), 3) = omp_priv;
20974 TREE_VEC_ELT (TREE_VALUE (t), 4) = omp_orig;
20975 TREE_VEC_ELT (TREE_VALUE (t), 5) = initializer.value;
20976 walk_tree (&initializer.value, c_check_omp_declare_reduction_r,
20977 &TREE_VEC_ELT (TREE_VALUE (t), 3), NULL);
20978 walk_tree (&DECL_INITIAL (omp_priv),
20979 c_check_omp_declare_reduction_r,
20980 &TREE_VEC_ELT (TREE_VALUE (t), 3), NULL);
20981 }
20982 }
20983
20984 pop_stmt_list (stmt);
20985 pop_scope ();
20986 if (cfun->language != NULL)
20987 {
20988 ggc_free (cfun->language);
20989 cfun->language = NULL;
20990 }
20991 set_cfun (NULL);
20992 current_function_decl = NULL_TREE;
20993 if (nested)
20994 c_pop_function_context ();
20995
20996 if (!clauses.is_empty ())
20997 {
20998 parser->tokens = &parser->tokens_buf[0];
20999 parser->tokens_avail = tokens_avail;
21000 }
21001 if (bad)
21002 goto fail;
21003 if (errs != errorcount)
21004 break;
21005 }
21006
21007 clauses.release ();
21008 types.release ();
21009 }
21010
21011
21012 /* OpenMP 4.0
21013 #pragma omp declare simd declare-simd-clauses[optseq] new-line
21014 #pragma omp declare reduction (reduction-id : typename-list : expression) \
21015 initializer-clause[opt] new-line
21016 #pragma omp declare target new-line
21017
21018 OpenMP 5.0
21019 #pragma omp declare variant (identifier) match (context-selector) */
21020
21021 static void
21022 c_parser_omp_declare (c_parser *parser, enum pragma_context context)
21023 {
21024 c_parser_consume_pragma (parser);
21025 if (c_parser_next_token_is (parser, CPP_NAME))
21026 {
21027 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
21028 if (strcmp (p, "simd") == 0)
21029 {
21030 /* c_parser_consume_token (parser); done in
21031 c_parser_omp_declare_simd. */
21032 c_parser_omp_declare_simd (parser, context);
21033 return;
21034 }
21035 if (strcmp (p, "reduction") == 0)
21036 {
21037 c_parser_consume_token (parser);
21038 c_parser_omp_declare_reduction (parser, context);
21039 return;
21040 }
21041 if (!flag_openmp) /* flag_openmp_simd */
21042 {
21043 c_parser_skip_to_pragma_eol (parser, false);
21044 return;
21045 }
21046 if (strcmp (p, "target") == 0)
21047 {
21048 c_parser_consume_token (parser);
21049 c_parser_omp_declare_target (parser);
21050 return;
21051 }
21052 if (strcmp (p, "variant") == 0)
21053 {
21054 /* c_parser_consume_token (parser); done in
21055 c_parser_omp_declare_simd. */
21056 c_parser_omp_declare_simd (parser, context);
21057 return;
21058 }
21059 }
21060
21061 c_parser_error (parser, "expected %<simd%>, %<reduction%>, "
21062 "%<target%> or %<variant%>");
21063 c_parser_skip_to_pragma_eol (parser);
21064 }
21065
21066 /* OpenMP 5.0
21067 #pragma omp requires clauses[optseq] new-line */
21068
21069 static void
21070 c_parser_omp_requires (c_parser *parser)
21071 {
21072 bool first = true;
21073 enum omp_requires new_req = (enum omp_requires) 0;
21074
21075 c_parser_consume_pragma (parser);
21076
21077 location_t loc = c_parser_peek_token (parser)->location;
21078 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
21079 {
21080 if (!first && c_parser_next_token_is (parser, CPP_COMMA))
21081 c_parser_consume_token (parser);
21082
21083 first = false;
21084
21085 if (c_parser_next_token_is (parser, CPP_NAME))
21086 {
21087 const char *p
21088 = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
21089 location_t cloc = c_parser_peek_token (parser)->location;
21090 enum omp_requires this_req = (enum omp_requires) 0;
21091
21092 if (!strcmp (p, "unified_address"))
21093 this_req = OMP_REQUIRES_UNIFIED_ADDRESS;
21094 else if (!strcmp (p, "unified_shared_memory"))
21095 this_req = OMP_REQUIRES_UNIFIED_SHARED_MEMORY;
21096 else if (!strcmp (p, "dynamic_allocators"))
21097 this_req = OMP_REQUIRES_DYNAMIC_ALLOCATORS;
21098 else if (!strcmp (p, "reverse_offload"))
21099 this_req = OMP_REQUIRES_REVERSE_OFFLOAD;
21100 else if (!strcmp (p, "atomic_default_mem_order"))
21101 {
21102 c_parser_consume_token (parser);
21103
21104 matching_parens parens;
21105 if (parens.require_open (parser))
21106 {
21107 if (c_parser_next_token_is (parser, CPP_NAME))
21108 {
21109 tree v = c_parser_peek_token (parser)->value;
21110 p = IDENTIFIER_POINTER (v);
21111
21112 if (!strcmp (p, "seq_cst"))
21113 this_req
21114 = (enum omp_requires) OMP_MEMORY_ORDER_SEQ_CST;
21115 else if (!strcmp (p, "relaxed"))
21116 this_req
21117 = (enum omp_requires) OMP_MEMORY_ORDER_RELAXED;
21118 else if (!strcmp (p, "acq_rel"))
21119 this_req
21120 = (enum omp_requires) OMP_MEMORY_ORDER_ACQ_REL;
21121 }
21122 if (this_req == 0)
21123 {
21124 error_at (c_parser_peek_token (parser)->location,
21125 "expected %<seq_cst%>, %<relaxed%> or "
21126 "%<acq_rel%>");
21127 if (c_parser_peek_2nd_token (parser)->type
21128 == CPP_CLOSE_PAREN)
21129 c_parser_consume_token (parser);
21130 }
21131 else
21132 c_parser_consume_token (parser);
21133
21134 parens.skip_until_found_close (parser);
21135 if (this_req == 0)
21136 {
21137 c_parser_skip_to_pragma_eol (parser, false);
21138 return;
21139 }
21140 }
21141 p = NULL;
21142 }
21143 else
21144 {
21145 error_at (cloc, "expected %<unified_address%>, "
21146 "%<unified_shared_memory%>, "
21147 "%<dynamic_allocators%>, "
21148 "%<reverse_offload%> "
21149 "or %<atomic_default_mem_order%> clause");
21150 c_parser_skip_to_pragma_eol (parser, false);
21151 return;
21152 }
21153 if (p)
21154 sorry_at (cloc, "%qs clause on %<requires%> directive not "
21155 "supported yet", p);
21156 if (p)
21157 c_parser_consume_token (parser);
21158 if (this_req)
21159 {
21160 if ((this_req & ~OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
21161 {
21162 if ((this_req & new_req) != 0)
21163 error_at (cloc, "too many %qs clauses", p);
21164 if (this_req != OMP_REQUIRES_DYNAMIC_ALLOCATORS
21165 && (omp_requires_mask & OMP_REQUIRES_TARGET_USED) != 0)
21166 error_at (cloc, "%qs clause used lexically after first "
21167 "target construct or offloading API", p);
21168 }
21169 else if ((new_req & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
21170 {
21171 error_at (cloc, "too many %qs clauses",
21172 "atomic_default_mem_order");
21173 this_req = (enum omp_requires) 0;
21174 }
21175 else if ((omp_requires_mask
21176 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
21177 {
21178 error_at (cloc, "more than one %<atomic_default_mem_order%>"
21179 " clause in a single compilation unit");
21180 this_req
21181 = (enum omp_requires)
21182 (omp_requires_mask
21183 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER);
21184 }
21185 else if ((omp_requires_mask
21186 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER_USED) != 0)
21187 error_at (cloc, "%<atomic_default_mem_order%> clause used "
21188 "lexically after first %<atomic%> construct "
21189 "without memory order clause");
21190 new_req = (enum omp_requires) (new_req | this_req);
21191 omp_requires_mask
21192 = (enum omp_requires) (omp_requires_mask | this_req);
21193 continue;
21194 }
21195 }
21196 break;
21197 }
21198 c_parser_skip_to_pragma_eol (parser);
21199
21200 if (new_req == 0)
21201 error_at (loc, "%<pragma omp requires%> requires at least one clause");
21202 }
21203
21204 /* Helper function for c_parser_omp_taskloop.
21205 Disallow zero sized or potentially zero sized task reductions. */
21206
21207 static tree
21208 c_finish_taskloop_clauses (tree clauses)
21209 {
21210 tree *pc = &clauses;
21211 for (tree c = clauses; c; c = *pc)
21212 {
21213 bool remove = false;
21214 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
21215 {
21216 tree type = strip_array_types (TREE_TYPE (OMP_CLAUSE_DECL (c)));
21217 if (integer_zerop (TYPE_SIZE_UNIT (type)))
21218 {
21219 error_at (OMP_CLAUSE_LOCATION (c),
21220 "zero sized type %qT in %<reduction%> clause", type);
21221 remove = true;
21222 }
21223 else if (TREE_CODE (TYPE_SIZE_UNIT (type)) != INTEGER_CST)
21224 {
21225 error_at (OMP_CLAUSE_LOCATION (c),
21226 "variable sized type %qT in %<reduction%> clause",
21227 type);
21228 remove = true;
21229 }
21230 }
21231 if (remove)
21232 *pc = OMP_CLAUSE_CHAIN (c);
21233 else
21234 pc = &OMP_CLAUSE_CHAIN (c);
21235 }
21236 return clauses;
21237 }
21238
21239 /* OpenMP 4.5:
21240 #pragma omp taskloop taskloop-clause[optseq] new-line
21241 for-loop
21242
21243 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
21244 for-loop */
21245
21246 #define OMP_TASKLOOP_CLAUSE_MASK \
21247 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
21248 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
21249 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
21250 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
21251 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
21252 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
21253 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
21254 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
21255 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
21256 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
21257 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
21258 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
21259 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
21260 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY) \
21261 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
21262 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION))
21263
21264 static tree
21265 c_parser_omp_taskloop (location_t loc, c_parser *parser,
21266 char *p_name, omp_clause_mask mask, tree *cclauses,
21267 bool *if_p)
21268 {
21269 tree clauses, block, ret;
21270
21271 strcat (p_name, " taskloop");
21272 mask |= OMP_TASKLOOP_CLAUSE_MASK;
21273 /* #pragma omp parallel master taskloop{, simd} disallow in_reduction
21274 clause. */
21275 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS)) != 0)
21276 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION);
21277
21278 if (c_parser_next_token_is (parser, CPP_NAME))
21279 {
21280 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
21281
21282 if (strcmp (p, "simd") == 0)
21283 {
21284 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
21285 if (cclauses == NULL)
21286 cclauses = cclauses_buf;
21287 c_parser_consume_token (parser);
21288 if (!flag_openmp) /* flag_openmp_simd */
21289 return c_parser_omp_simd (loc, parser, p_name, mask, cclauses,
21290 if_p);
21291 block = c_begin_compound_stmt (true);
21292 ret = c_parser_omp_simd (loc, parser, p_name, mask, cclauses, if_p);
21293 block = c_end_compound_stmt (loc, block, true);
21294 if (ret == NULL)
21295 return ret;
21296 ret = make_node (OMP_TASKLOOP);
21297 TREE_TYPE (ret) = void_type_node;
21298 OMP_FOR_BODY (ret) = block;
21299 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
21300 OMP_FOR_CLAUSES (ret)
21301 = c_finish_taskloop_clauses (OMP_FOR_CLAUSES (ret));
21302 SET_EXPR_LOCATION (ret, loc);
21303 add_stmt (ret);
21304 return ret;
21305 }
21306 }
21307 if (!flag_openmp) /* flag_openmp_simd */
21308 {
21309 c_parser_skip_to_pragma_eol (parser, false);
21310 return NULL_TREE;
21311 }
21312
21313 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
21314 if (cclauses)
21315 {
21316 omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
21317 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
21318 }
21319
21320 clauses = c_finish_taskloop_clauses (clauses);
21321 block = c_begin_compound_stmt (true);
21322 ret = c_parser_omp_for_loop (loc, parser, OMP_TASKLOOP, clauses, NULL, if_p);
21323 block = c_end_compound_stmt (loc, block, true);
21324 add_stmt (block);
21325
21326 return ret;
21327 }
21328
21329 /* Main entry point to parsing most OpenMP pragmas. */
21330
21331 static void
21332 c_parser_omp_construct (c_parser *parser, bool *if_p)
21333 {
21334 enum pragma_kind p_kind;
21335 location_t loc;
21336 tree stmt;
21337 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
21338 omp_clause_mask mask (0);
21339
21340 loc = c_parser_peek_token (parser)->location;
21341 p_kind = c_parser_peek_token (parser)->pragma_kind;
21342 c_parser_consume_pragma (parser);
21343
21344 switch (p_kind)
21345 {
21346 case PRAGMA_OACC_ATOMIC:
21347 c_parser_omp_atomic (loc, parser);
21348 return;
21349 case PRAGMA_OACC_CACHE:
21350 strcpy (p_name, "#pragma acc");
21351 stmt = c_parser_oacc_cache (loc, parser);
21352 break;
21353 case PRAGMA_OACC_DATA:
21354 stmt = c_parser_oacc_data (loc, parser, if_p);
21355 break;
21356 case PRAGMA_OACC_HOST_DATA:
21357 stmt = c_parser_oacc_host_data (loc, parser, if_p);
21358 break;
21359 case PRAGMA_OACC_KERNELS:
21360 case PRAGMA_OACC_PARALLEL:
21361 case PRAGMA_OACC_SERIAL:
21362 strcpy (p_name, "#pragma acc");
21363 stmt = c_parser_oacc_compute (loc, parser, p_kind, p_name, if_p);
21364 break;
21365 case PRAGMA_OACC_LOOP:
21366 strcpy (p_name, "#pragma acc");
21367 stmt = c_parser_oacc_loop (loc, parser, p_name, mask, NULL, if_p);
21368 break;
21369 case PRAGMA_OACC_WAIT:
21370 strcpy (p_name, "#pragma wait");
21371 stmt = c_parser_oacc_wait (loc, parser, p_name);
21372 break;
21373 case PRAGMA_OMP_ATOMIC:
21374 c_parser_omp_atomic (loc, parser);
21375 return;
21376 case PRAGMA_OMP_CRITICAL:
21377 stmt = c_parser_omp_critical (loc, parser, if_p);
21378 break;
21379 case PRAGMA_OMP_DISTRIBUTE:
21380 strcpy (p_name, "#pragma omp");
21381 stmt = c_parser_omp_distribute (loc, parser, p_name, mask, NULL, if_p);
21382 break;
21383 case PRAGMA_OMP_FOR:
21384 strcpy (p_name, "#pragma omp");
21385 stmt = c_parser_omp_for (loc, parser, p_name, mask, NULL, if_p);
21386 break;
21387 case PRAGMA_OMP_LOOP:
21388 strcpy (p_name, "#pragma omp");
21389 stmt = c_parser_omp_loop (loc, parser, p_name, mask, NULL, if_p);
21390 break;
21391 case PRAGMA_OMP_MASTER:
21392 strcpy (p_name, "#pragma omp");
21393 stmt = c_parser_omp_master (loc, parser, p_name, mask, NULL, if_p);
21394 break;
21395 case PRAGMA_OMP_PARALLEL:
21396 strcpy (p_name, "#pragma omp");
21397 stmt = c_parser_omp_parallel (loc, parser, p_name, mask, NULL, if_p);
21398 break;
21399 case PRAGMA_OMP_SECTIONS:
21400 strcpy (p_name, "#pragma omp");
21401 stmt = c_parser_omp_sections (loc, parser, p_name, mask, NULL);
21402 break;
21403 case PRAGMA_OMP_SIMD:
21404 strcpy (p_name, "#pragma omp");
21405 stmt = c_parser_omp_simd (loc, parser, p_name, mask, NULL, if_p);
21406 break;
21407 case PRAGMA_OMP_SINGLE:
21408 stmt = c_parser_omp_single (loc, parser, if_p);
21409 break;
21410 case PRAGMA_OMP_TASK:
21411 stmt = c_parser_omp_task (loc, parser, if_p);
21412 break;
21413 case PRAGMA_OMP_TASKGROUP:
21414 stmt = c_parser_omp_taskgroup (loc, parser, if_p);
21415 break;
21416 case PRAGMA_OMP_TASKLOOP:
21417 strcpy (p_name, "#pragma omp");
21418 stmt = c_parser_omp_taskloop (loc, parser, p_name, mask, NULL, if_p);
21419 break;
21420 case PRAGMA_OMP_TEAMS:
21421 strcpy (p_name, "#pragma omp");
21422 stmt = c_parser_omp_teams (loc, parser, p_name, mask, NULL, if_p);
21423 break;
21424 default:
21425 gcc_unreachable ();
21426 }
21427
21428 if (stmt)
21429 gcc_assert (EXPR_LOCATION (stmt) != UNKNOWN_LOCATION);
21430 }
21431
21432
21433 /* OpenMP 2.5:
21434 # pragma omp threadprivate (variable-list) */
21435
21436 static void
21437 c_parser_omp_threadprivate (c_parser *parser)
21438 {
21439 tree vars, t;
21440 location_t loc;
21441
21442 c_parser_consume_pragma (parser);
21443 loc = c_parser_peek_token (parser)->location;
21444 vars = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
21445
21446 /* Mark every variable in VARS to be assigned thread local storage. */
21447 for (t = vars; t; t = TREE_CHAIN (t))
21448 {
21449 tree v = TREE_PURPOSE (t);
21450
21451 /* FIXME diagnostics: Ideally we should keep individual
21452 locations for all the variables in the var list to make the
21453 following errors more precise. Perhaps
21454 c_parser_omp_var_list_parens() should construct a list of
21455 locations to go along with the var list. */
21456
21457 /* If V had already been marked threadprivate, it doesn't matter
21458 whether it had been used prior to this point. */
21459 if (!VAR_P (v))
21460 error_at (loc, "%qD is not a variable", v);
21461 else if (TREE_USED (v) && !C_DECL_THREADPRIVATE_P (v))
21462 error_at (loc, "%qE declared %<threadprivate%> after first use", v);
21463 else if (! is_global_var (v))
21464 error_at (loc, "automatic variable %qE cannot be %<threadprivate%>", v);
21465 else if (TREE_TYPE (v) == error_mark_node)
21466 ;
21467 else if (! COMPLETE_TYPE_P (TREE_TYPE (v)))
21468 error_at (loc, "%<threadprivate%> %qE has incomplete type", v);
21469 else
21470 {
21471 if (! DECL_THREAD_LOCAL_P (v))
21472 {
21473 set_decl_tls_model (v, decl_default_tls_model (v));
21474 /* If rtl has been already set for this var, call
21475 make_decl_rtl once again, so that encode_section_info
21476 has a chance to look at the new decl flags. */
21477 if (DECL_RTL_SET_P (v))
21478 make_decl_rtl (v);
21479 }
21480 C_DECL_THREADPRIVATE_P (v) = 1;
21481 }
21482 }
21483
21484 c_parser_skip_to_pragma_eol (parser);
21485 }
21486
21487 /* Parse a transaction attribute (GCC Extension).
21488
21489 transaction-attribute:
21490 gnu-attributes
21491 attribute-specifier
21492 */
21493
21494 static tree
21495 c_parser_transaction_attributes (c_parser *parser)
21496 {
21497 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
21498 return c_parser_gnu_attributes (parser);
21499
21500 if (!c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
21501 return NULL_TREE;
21502 return c_parser_std_attribute_specifier (parser, true);
21503 }
21504
21505 /* Parse a __transaction_atomic or __transaction_relaxed statement
21506 (GCC Extension).
21507
21508 transaction-statement:
21509 __transaction_atomic transaction-attribute[opt] compound-statement
21510 __transaction_relaxed compound-statement
21511
21512 Note that the only valid attribute is: "outer".
21513 */
21514
21515 static tree
21516 c_parser_transaction (c_parser *parser, enum rid keyword)
21517 {
21518 unsigned int old_in = parser->in_transaction;
21519 unsigned int this_in = 1, new_in;
21520 location_t loc = c_parser_peek_token (parser)->location;
21521 tree stmt, attrs;
21522
21523 gcc_assert ((keyword == RID_TRANSACTION_ATOMIC
21524 || keyword == RID_TRANSACTION_RELAXED)
21525 && c_parser_next_token_is_keyword (parser, keyword));
21526 c_parser_consume_token (parser);
21527
21528 if (keyword == RID_TRANSACTION_RELAXED)
21529 this_in |= TM_STMT_ATTR_RELAXED;
21530 else
21531 {
21532 attrs = c_parser_transaction_attributes (parser);
21533 if (attrs)
21534 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
21535 }
21536
21537 /* Keep track if we're in the lexical scope of an outer transaction. */
21538 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
21539
21540 parser->in_transaction = new_in;
21541 stmt = c_parser_compound_statement (parser);
21542 parser->in_transaction = old_in;
21543
21544 if (flag_tm)
21545 stmt = c_finish_transaction (loc, stmt, this_in);
21546 else
21547 error_at (loc, (keyword == RID_TRANSACTION_ATOMIC ?
21548 "%<__transaction_atomic%> without transactional memory support enabled"
21549 : "%<__transaction_relaxed %> "
21550 "without transactional memory support enabled"));
21551
21552 return stmt;
21553 }
21554
21555 /* Parse a __transaction_atomic or __transaction_relaxed expression
21556 (GCC Extension).
21557
21558 transaction-expression:
21559 __transaction_atomic ( expression )
21560 __transaction_relaxed ( expression )
21561 */
21562
21563 static struct c_expr
21564 c_parser_transaction_expression (c_parser *parser, enum rid keyword)
21565 {
21566 struct c_expr ret;
21567 unsigned int old_in = parser->in_transaction;
21568 unsigned int this_in = 1;
21569 location_t loc = c_parser_peek_token (parser)->location;
21570 tree attrs;
21571
21572 gcc_assert ((keyword == RID_TRANSACTION_ATOMIC
21573 || keyword == RID_TRANSACTION_RELAXED)
21574 && c_parser_next_token_is_keyword (parser, keyword));
21575 c_parser_consume_token (parser);
21576
21577 if (keyword == RID_TRANSACTION_RELAXED)
21578 this_in |= TM_STMT_ATTR_RELAXED;
21579 else
21580 {
21581 attrs = c_parser_transaction_attributes (parser);
21582 if (attrs)
21583 this_in |= parse_tm_stmt_attr (attrs, 0);
21584 }
21585
21586 parser->in_transaction = this_in;
21587 matching_parens parens;
21588 if (parens.require_open (parser))
21589 {
21590 tree expr = c_parser_expression (parser).value;
21591 ret.original_type = TREE_TYPE (expr);
21592 ret.value = build1 (TRANSACTION_EXPR, ret.original_type, expr);
21593 if (this_in & TM_STMT_ATTR_RELAXED)
21594 TRANSACTION_EXPR_RELAXED (ret.value) = 1;
21595 SET_EXPR_LOCATION (ret.value, loc);
21596 ret.original_code = TRANSACTION_EXPR;
21597 if (!parens.require_close (parser))
21598 {
21599 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
21600 goto error;
21601 }
21602 }
21603 else
21604 {
21605 error:
21606 ret.set_error ();
21607 ret.original_code = ERROR_MARK;
21608 ret.original_type = NULL;
21609 }
21610 parser->in_transaction = old_in;
21611
21612 if (!flag_tm)
21613 error_at (loc, (keyword == RID_TRANSACTION_ATOMIC ?
21614 "%<__transaction_atomic%> without transactional memory support enabled"
21615 : "%<__transaction_relaxed %> "
21616 "without transactional memory support enabled"));
21617
21618 set_c_expr_source_range (&ret, loc, loc);
21619
21620 return ret;
21621 }
21622
21623 /* Parse a __transaction_cancel statement (GCC Extension).
21624
21625 transaction-cancel-statement:
21626 __transaction_cancel transaction-attribute[opt] ;
21627
21628 Note that the only valid attribute is "outer".
21629 */
21630
21631 static tree
21632 c_parser_transaction_cancel (c_parser *parser)
21633 {
21634 location_t loc = c_parser_peek_token (parser)->location;
21635 tree attrs;
21636 bool is_outer = false;
21637
21638 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TRANSACTION_CANCEL));
21639 c_parser_consume_token (parser);
21640
21641 attrs = c_parser_transaction_attributes (parser);
21642 if (attrs)
21643 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
21644
21645 if (!flag_tm)
21646 {
21647 error_at (loc, "%<__transaction_cancel%> without "
21648 "transactional memory support enabled");
21649 goto ret_error;
21650 }
21651 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
21652 {
21653 error_at (loc, "%<__transaction_cancel%> within a "
21654 "%<__transaction_relaxed%>");
21655 goto ret_error;
21656 }
21657 else if (is_outer)
21658 {
21659 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
21660 && !is_tm_may_cancel_outer (current_function_decl))
21661 {
21662 error_at (loc, "outer %<__transaction_cancel%> not "
21663 "within outer %<__transaction_atomic%> or "
21664 "a %<transaction_may_cancel_outer%> function");
21665 goto ret_error;
21666 }
21667 }
21668 else if (parser->in_transaction == 0)
21669 {
21670 error_at (loc, "%<__transaction_cancel%> not within "
21671 "%<__transaction_atomic%>");
21672 goto ret_error;
21673 }
21674
21675 return add_stmt (build_tm_abort_call (loc, is_outer));
21676
21677 ret_error:
21678 return build1 (NOP_EXPR, void_type_node, error_mark_node);
21679 }
21680 \f
21681 /* Parse a single source file. */
21682
21683 void
21684 c_parse_file (void)
21685 {
21686 /* Use local storage to begin. If the first token is a pragma, parse it.
21687 If it is #pragma GCC pch_preprocess, then this will load a PCH file
21688 which will cause garbage collection. */
21689 c_parser tparser;
21690
21691 memset (&tparser, 0, sizeof tparser);
21692 tparser.translate_strings_p = true;
21693 tparser.tokens = &tparser.tokens_buf[0];
21694 the_parser = &tparser;
21695
21696 if (c_parser_peek_token (&tparser)->pragma_kind == PRAGMA_GCC_PCH_PREPROCESS)
21697 c_parser_pragma_pch_preprocess (&tparser);
21698 else
21699 c_common_no_more_pch ();
21700
21701 the_parser = ggc_alloc<c_parser> ();
21702 *the_parser = tparser;
21703 if (tparser.tokens == &tparser.tokens_buf[0])
21704 the_parser->tokens = &the_parser->tokens_buf[0];
21705
21706 /* Initialize EH, if we've been told to do so. */
21707 if (flag_exceptions)
21708 using_eh_for_cleanups ();
21709
21710 c_parser_translation_unit (the_parser);
21711 the_parser = NULL;
21712 }
21713
21714 /* Parse the body of a function declaration marked with "__RTL".
21715
21716 The RTL parser works on the level of characters read from a
21717 FILE *, whereas c_parser works at the level of tokens.
21718 Square this circle by consuming all of the tokens up to and
21719 including the closing brace, recording the start/end of the RTL
21720 fragment, and reopening the file and re-reading the relevant
21721 lines within the RTL parser.
21722
21723 This requires the opening and closing braces of the C function
21724 to be on separate lines from the RTL they wrap.
21725
21726 Take ownership of START_WITH_PASS, if non-NULL. */
21727
21728 void
21729 c_parser_parse_rtl_body (c_parser *parser, char *start_with_pass)
21730 {
21731 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
21732 {
21733 free (start_with_pass);
21734 return;
21735 }
21736
21737 location_t start_loc = c_parser_peek_token (parser)->location;
21738
21739 /* Consume all tokens, up to the closing brace, handling
21740 matching pairs of braces in the rtl dump. */
21741 int num_open_braces = 1;
21742 while (1)
21743 {
21744 switch (c_parser_peek_token (parser)->type)
21745 {
21746 case CPP_OPEN_BRACE:
21747 num_open_braces++;
21748 break;
21749 case CPP_CLOSE_BRACE:
21750 if (--num_open_braces == 0)
21751 goto found_closing_brace;
21752 break;
21753 case CPP_EOF:
21754 error_at (start_loc, "no closing brace");
21755 free (start_with_pass);
21756 return;
21757 default:
21758 break;
21759 }
21760 c_parser_consume_token (parser);
21761 }
21762
21763 found_closing_brace:
21764 /* At the closing brace; record its location. */
21765 location_t end_loc = c_parser_peek_token (parser)->location;
21766
21767 /* Consume the closing brace. */
21768 c_parser_consume_token (parser);
21769
21770 /* Invoke the RTL parser. */
21771 if (!read_rtl_function_body_from_file_range (start_loc, end_loc))
21772 {
21773 free (start_with_pass);
21774 return;
21775 }
21776
21777 /* Run the backend on the cfun created above, transferring ownership of
21778 START_WITH_PASS. */
21779 run_rtl_passes (start_with_pass);
21780 }
21781
21782 #include "gt-c-c-parser.h"