]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/c/c-parser.c
3aa85125cf1a06a227a53032b09f779905bae38f
[thirdparty/gcc.git] / gcc / c / c-parser.c
1 /* Parser for C and Objective-C.
2 Copyright (C) 1987-2019 Free Software Foundation, Inc.
3
4 Parser actions based on the old Bison parser; structure somewhat
5 influenced by and fragments based on the C++ parser.
6
7 This file is part of GCC.
8
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
13
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
22
23 /* TODO:
24
25 Make sure all relevant comments, and all relevant code from all
26 actions, brought over from old parser. Verify exact correspondence
27 of syntax accepted.
28
29 Add testcases covering every input symbol in every state in old and
30 new parsers.
31
32 Include full syntax for GNU C, including erroneous cases accepted
33 with error messages, in syntax productions in comments.
34
35 Make more diagnostics in the front end generally take an explicit
36 location rather than implicitly using input_location. */
37
38 #include "config.h"
39 #define INCLUDE_UNIQUE_PTR
40 #include "system.h"
41 #include "coretypes.h"
42 #include "target.h"
43 #include "function.h"
44 #include "c-tree.h"
45 #include "timevar.h"
46 #include "stringpool.h"
47 #include "cgraph.h"
48 #include "attribs.h"
49 #include "stor-layout.h"
50 #include "varasm.h"
51 #include "trans-mem.h"
52 #include "c-family/c-pragma.h"
53 #include "c-lang.h"
54 #include "c-family/c-objc.h"
55 #include "plugin.h"
56 #include "omp-general.h"
57 #include "omp-offload.h"
58 #include "builtins.h"
59 #include "gomp-constants.h"
60 #include "c-family/c-indentation.h"
61 #include "gimple-expr.h"
62 #include "context.h"
63 #include "gcc-rich-location.h"
64 #include "c-parser.h"
65 #include "gimple-parser.h"
66 #include "read-rtl-function.h"
67 #include "run-rtl-passes.h"
68 #include "intl.h"
69 #include "c-family/name-hint.h"
70 #include "tree-iterator.h"
71 #include "memmodel.h"
72
73 /* We need to walk over decls with incomplete struct/union/enum types
74 after parsing the whole translation unit.
75 In finish_decl(), if the decl is static, has incomplete
76 struct/union/enum type, it is appeneded to incomplete_record_decls.
77 In c_parser_translation_unit(), we iterate over incomplete_record_decls
78 and report error if any of the decls are still incomplete. */
79
80 vec<tree> incomplete_record_decls;
81
82 void
83 set_c_expr_source_range (c_expr *expr,
84 location_t start, location_t finish)
85 {
86 expr->src_range.m_start = start;
87 expr->src_range.m_finish = finish;
88 if (expr->value)
89 set_source_range (expr->value, start, finish);
90 }
91
92 void
93 set_c_expr_source_range (c_expr *expr,
94 source_range src_range)
95 {
96 expr->src_range = src_range;
97 if (expr->value)
98 set_source_range (expr->value, src_range);
99 }
100
101 \f
102 /* Initialization routine for this file. */
103
104 void
105 c_parse_init (void)
106 {
107 /* The only initialization required is of the reserved word
108 identifiers. */
109 unsigned int i;
110 tree id;
111 int mask = 0;
112
113 /* Make sure RID_MAX hasn't grown past the 8 bits used to hold the keyword in
114 the c_token structure. */
115 gcc_assert (RID_MAX <= 255);
116
117 mask |= D_CXXONLY;
118 if (!flag_isoc99)
119 mask |= D_C99;
120 if (flag_no_asm)
121 {
122 mask |= D_ASM | D_EXT;
123 if (!flag_isoc99)
124 mask |= D_EXT89;
125 }
126 if (!c_dialect_objc ())
127 mask |= D_OBJC | D_CXX_OBJC;
128
129 ridpointers = ggc_cleared_vec_alloc<tree> ((int) RID_MAX);
130 for (i = 0; i < num_c_common_reswords; i++)
131 {
132 /* If a keyword is disabled, do not enter it into the table
133 and so create a canonical spelling that isn't a keyword. */
134 if (c_common_reswords[i].disable & mask)
135 {
136 if (warn_cxx_compat
137 && (c_common_reswords[i].disable & D_CXXWARN))
138 {
139 id = get_identifier (c_common_reswords[i].word);
140 C_SET_RID_CODE (id, RID_CXX_COMPAT_WARN);
141 C_IS_RESERVED_WORD (id) = 1;
142 }
143 continue;
144 }
145
146 id = get_identifier (c_common_reswords[i].word);
147 C_SET_RID_CODE (id, c_common_reswords[i].rid);
148 C_IS_RESERVED_WORD (id) = 1;
149 ridpointers [(int) c_common_reswords[i].rid] = id;
150 }
151
152 for (i = 0; i < NUM_INT_N_ENTS; i++)
153 {
154 /* We always create the symbols but they aren't always supported. */
155 char name[50];
156 sprintf (name, "__int%d", int_n_data[i].bitsize);
157 id = get_identifier (name);
158 C_SET_RID_CODE (id, RID_FIRST_INT_N + i);
159 C_IS_RESERVED_WORD (id) = 1;
160 }
161 }
162 \f
163 /* A parser structure recording information about the state and
164 context of parsing. Includes lexer information with up to two
165 tokens of look-ahead; more are not needed for C. */
166 struct GTY(()) c_parser {
167 /* The look-ahead tokens. */
168 c_token * GTY((skip)) tokens;
169 /* Buffer for look-ahead tokens. */
170 c_token tokens_buf[4];
171 /* How many look-ahead tokens are available (0 - 4, or
172 more if parsing from pre-lexed tokens). */
173 unsigned int tokens_avail;
174 /* True if a syntax error is being recovered from; false otherwise.
175 c_parser_error sets this flag. It should clear this flag when
176 enough tokens have been consumed to recover from the error. */
177 BOOL_BITFIELD error : 1;
178 /* True if we're processing a pragma, and shouldn't automatically
179 consume CPP_PRAGMA_EOL. */
180 BOOL_BITFIELD in_pragma : 1;
181 /* True if we're parsing the outermost block of an if statement. */
182 BOOL_BITFIELD in_if_block : 1;
183 /* True if we want to lex an untranslated string. */
184 BOOL_BITFIELD lex_untranslated_string : 1;
185
186 /* Objective-C specific parser/lexer information. */
187
188 /* True if we are in a context where the Objective-C "PQ" keywords
189 are considered keywords. */
190 BOOL_BITFIELD objc_pq_context : 1;
191 /* True if we are parsing a (potential) Objective-C foreach
192 statement. This is set to true after we parsed 'for (' and while
193 we wait for 'in' or ';' to decide if it's a standard C for loop or an
194 Objective-C foreach loop. */
195 BOOL_BITFIELD objc_could_be_foreach_context : 1;
196 /* The following flag is needed to contextualize Objective-C lexical
197 analysis. In some cases (e.g., 'int NSObject;'), it is
198 undesirable to bind an identifier to an Objective-C class, even
199 if a class with that name exists. */
200 BOOL_BITFIELD objc_need_raw_identifier : 1;
201 /* Nonzero if we're processing a __transaction statement. The value
202 is 1 | TM_STMT_ATTR_*. */
203 unsigned int in_transaction : 4;
204 /* True if we are in a context where the Objective-C "Property attribute"
205 keywords are valid. */
206 BOOL_BITFIELD objc_property_attr_context : 1;
207
208 /* Location of the last consumed token. */
209 location_t last_token_location;
210 };
211
212 /* Return a pointer to the Nth token in PARSERs tokens_buf. */
213
214 c_token *
215 c_parser_tokens_buf (c_parser *parser, unsigned n)
216 {
217 return &parser->tokens_buf[n];
218 }
219
220 /* Return the error state of PARSER. */
221
222 bool
223 c_parser_error (c_parser *parser)
224 {
225 return parser->error;
226 }
227
228 /* Set the error state of PARSER to ERR. */
229
230 void
231 c_parser_set_error (c_parser *parser, bool err)
232 {
233 parser->error = err;
234 }
235
236
237 /* The actual parser and external interface. ??? Does this need to be
238 garbage-collected? */
239
240 static GTY (()) c_parser *the_parser;
241
242 /* Read in and lex a single token, storing it in *TOKEN. */
243
244 static void
245 c_lex_one_token (c_parser *parser, c_token *token)
246 {
247 timevar_push (TV_LEX);
248
249 token->type = c_lex_with_flags (&token->value, &token->location,
250 &token->flags,
251 (parser->lex_untranslated_string
252 ? C_LEX_STRING_NO_TRANSLATE : 0));
253 token->id_kind = C_ID_NONE;
254 token->keyword = RID_MAX;
255 token->pragma_kind = PRAGMA_NONE;
256
257 switch (token->type)
258 {
259 case CPP_NAME:
260 {
261 tree decl;
262
263 bool objc_force_identifier = parser->objc_need_raw_identifier;
264 if (c_dialect_objc ())
265 parser->objc_need_raw_identifier = false;
266
267 if (C_IS_RESERVED_WORD (token->value))
268 {
269 enum rid rid_code = C_RID_CODE (token->value);
270
271 if (rid_code == RID_CXX_COMPAT_WARN)
272 {
273 warning_at (token->location,
274 OPT_Wc___compat,
275 "identifier %qE conflicts with C++ keyword",
276 token->value);
277 }
278 else if (rid_code >= RID_FIRST_ADDR_SPACE
279 && rid_code <= RID_LAST_ADDR_SPACE)
280 {
281 addr_space_t as;
282 as = (addr_space_t) (rid_code - RID_FIRST_ADDR_SPACE);
283 targetm.addr_space.diagnose_usage (as, token->location);
284 token->id_kind = C_ID_ADDRSPACE;
285 token->keyword = rid_code;
286 break;
287 }
288 else if (c_dialect_objc () && OBJC_IS_PQ_KEYWORD (rid_code))
289 {
290 /* We found an Objective-C "pq" keyword (in, out,
291 inout, bycopy, byref, oneway). They need special
292 care because the interpretation depends on the
293 context. */
294 if (parser->objc_pq_context)
295 {
296 token->type = CPP_KEYWORD;
297 token->keyword = rid_code;
298 break;
299 }
300 else if (parser->objc_could_be_foreach_context
301 && rid_code == RID_IN)
302 {
303 /* We are in Objective-C, inside a (potential)
304 foreach context (which means after having
305 parsed 'for (', but before having parsed ';'),
306 and we found 'in'. We consider it the keyword
307 which terminates the declaration at the
308 beginning of a foreach-statement. Note that
309 this means you can't use 'in' for anything else
310 in that context; in particular, in Objective-C
311 you can't use 'in' as the name of the running
312 variable in a C for loop. We could potentially
313 try to add code here to disambiguate, but it
314 seems a reasonable limitation. */
315 token->type = CPP_KEYWORD;
316 token->keyword = rid_code;
317 break;
318 }
319 /* Else, "pq" keywords outside of the "pq" context are
320 not keywords, and we fall through to the code for
321 normal tokens. */
322 }
323 else if (c_dialect_objc () && OBJC_IS_PATTR_KEYWORD (rid_code))
324 {
325 /* We found an Objective-C "property attribute"
326 keyword (getter, setter, readonly, etc). These are
327 only valid in the property context. */
328 if (parser->objc_property_attr_context)
329 {
330 token->type = CPP_KEYWORD;
331 token->keyword = rid_code;
332 break;
333 }
334 /* Else they are not special keywords.
335 */
336 }
337 else if (c_dialect_objc ()
338 && (OBJC_IS_AT_KEYWORD (rid_code)
339 || OBJC_IS_CXX_KEYWORD (rid_code)))
340 {
341 /* We found one of the Objective-C "@" keywords (defs,
342 selector, synchronized, etc) or one of the
343 Objective-C "cxx" keywords (class, private,
344 protected, public, try, catch, throw) without a
345 preceding '@' sign. Do nothing and fall through to
346 the code for normal tokens (in C++ we would still
347 consider the CXX ones keywords, but not in C). */
348 ;
349 }
350 else
351 {
352 token->type = CPP_KEYWORD;
353 token->keyword = rid_code;
354 break;
355 }
356 }
357
358 decl = lookup_name (token->value);
359 if (decl)
360 {
361 if (TREE_CODE (decl) == TYPE_DECL)
362 {
363 token->id_kind = C_ID_TYPENAME;
364 break;
365 }
366 }
367 else if (c_dialect_objc ())
368 {
369 tree objc_interface_decl = objc_is_class_name (token->value);
370 /* Objective-C class names are in the same namespace as
371 variables and typedefs, and hence are shadowed by local
372 declarations. */
373 if (objc_interface_decl
374 && (!objc_force_identifier || global_bindings_p ()))
375 {
376 token->value = objc_interface_decl;
377 token->id_kind = C_ID_CLASSNAME;
378 break;
379 }
380 }
381 token->id_kind = C_ID_ID;
382 }
383 break;
384 case CPP_AT_NAME:
385 /* This only happens in Objective-C; it must be a keyword. */
386 token->type = CPP_KEYWORD;
387 switch (C_RID_CODE (token->value))
388 {
389 /* Replace 'class' with '@class', 'private' with '@private',
390 etc. This prevents confusion with the C++ keyword
391 'class', and makes the tokens consistent with other
392 Objective-C 'AT' keywords. For example '@class' is
393 reported as RID_AT_CLASS which is consistent with
394 '@synchronized', which is reported as
395 RID_AT_SYNCHRONIZED.
396 */
397 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
398 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
399 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
400 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
401 case RID_THROW: token->keyword = RID_AT_THROW; break;
402 case RID_TRY: token->keyword = RID_AT_TRY; break;
403 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
404 case RID_SYNCHRONIZED: token->keyword = RID_AT_SYNCHRONIZED; break;
405 default: token->keyword = C_RID_CODE (token->value);
406 }
407 break;
408 case CPP_COLON:
409 case CPP_COMMA:
410 case CPP_CLOSE_PAREN:
411 case CPP_SEMICOLON:
412 /* These tokens may affect the interpretation of any identifiers
413 following, if doing Objective-C. */
414 if (c_dialect_objc ())
415 parser->objc_need_raw_identifier = false;
416 break;
417 case CPP_PRAGMA:
418 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
419 token->pragma_kind = (enum pragma_kind) TREE_INT_CST_LOW (token->value);
420 token->value = NULL;
421 break;
422 default:
423 break;
424 }
425 timevar_pop (TV_LEX);
426 }
427
428 /* Return a pointer to the next token from PARSER, reading it in if
429 necessary. */
430
431 c_token *
432 c_parser_peek_token (c_parser *parser)
433 {
434 if (parser->tokens_avail == 0)
435 {
436 c_lex_one_token (parser, &parser->tokens[0]);
437 parser->tokens_avail = 1;
438 }
439 return &parser->tokens[0];
440 }
441
442 /* Return a pointer to the next-but-one token from PARSER, reading it
443 in if necessary. The next token is already read in. */
444
445 c_token *
446 c_parser_peek_2nd_token (c_parser *parser)
447 {
448 if (parser->tokens_avail >= 2)
449 return &parser->tokens[1];
450 gcc_assert (parser->tokens_avail == 1);
451 gcc_assert (parser->tokens[0].type != CPP_EOF);
452 gcc_assert (parser->tokens[0].type != CPP_PRAGMA_EOL);
453 c_lex_one_token (parser, &parser->tokens[1]);
454 parser->tokens_avail = 2;
455 return &parser->tokens[1];
456 }
457
458 /* Return a pointer to the Nth token from PARSER, reading it
459 in if necessary. The N-1th token is already read in. */
460
461 c_token *
462 c_parser_peek_nth_token (c_parser *parser, unsigned int n)
463 {
464 /* N is 1-based, not zero-based. */
465 gcc_assert (n > 0);
466
467 if (parser->tokens_avail >= n)
468 return &parser->tokens[n - 1];
469 gcc_assert (parser->tokens_avail == n - 1);
470 c_lex_one_token (parser, &parser->tokens[n - 1]);
471 parser->tokens_avail = n;
472 return &parser->tokens[n - 1];
473 }
474
475 bool
476 c_keyword_starts_typename (enum rid keyword)
477 {
478 switch (keyword)
479 {
480 case RID_UNSIGNED:
481 case RID_LONG:
482 case RID_SHORT:
483 case RID_SIGNED:
484 case RID_COMPLEX:
485 case RID_INT:
486 case RID_CHAR:
487 case RID_FLOAT:
488 case RID_DOUBLE:
489 case RID_VOID:
490 case RID_DFLOAT32:
491 case RID_DFLOAT64:
492 case RID_DFLOAT128:
493 CASE_RID_FLOATN_NX:
494 case RID_BOOL:
495 case RID_ENUM:
496 case RID_STRUCT:
497 case RID_UNION:
498 case RID_TYPEOF:
499 case RID_CONST:
500 case RID_ATOMIC:
501 case RID_VOLATILE:
502 case RID_RESTRICT:
503 case RID_ATTRIBUTE:
504 case RID_FRACT:
505 case RID_ACCUM:
506 case RID_SAT:
507 case RID_AUTO_TYPE:
508 case RID_ALIGNAS:
509 return true;
510 default:
511 if (keyword >= RID_FIRST_INT_N
512 && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
513 && int_n_enabled_p[keyword - RID_FIRST_INT_N])
514 return true;
515 return false;
516 }
517 }
518
519 /* Return true if TOKEN can start a type name,
520 false otherwise. */
521 bool
522 c_token_starts_typename (c_token *token)
523 {
524 switch (token->type)
525 {
526 case CPP_NAME:
527 switch (token->id_kind)
528 {
529 case C_ID_ID:
530 return false;
531 case C_ID_ADDRSPACE:
532 return true;
533 case C_ID_TYPENAME:
534 return true;
535 case C_ID_CLASSNAME:
536 gcc_assert (c_dialect_objc ());
537 return true;
538 default:
539 gcc_unreachable ();
540 }
541 case CPP_KEYWORD:
542 return c_keyword_starts_typename (token->keyword);
543 case CPP_LESS:
544 if (c_dialect_objc ())
545 return true;
546 return false;
547 default:
548 return false;
549 }
550 }
551
552 /* Return true if the next token from PARSER can start a type name,
553 false otherwise. LA specifies how to do lookahead in order to
554 detect unknown type names. If unsure, pick CLA_PREFER_ID. */
555
556 static inline bool
557 c_parser_next_tokens_start_typename (c_parser *parser, enum c_lookahead_kind la)
558 {
559 c_token *token = c_parser_peek_token (parser);
560 if (c_token_starts_typename (token))
561 return true;
562
563 /* Try a bit harder to detect an unknown typename. */
564 if (la != cla_prefer_id
565 && token->type == CPP_NAME
566 && token->id_kind == C_ID_ID
567
568 /* Do not try too hard when we could have "object in array". */
569 && !parser->objc_could_be_foreach_context
570
571 && (la == cla_prefer_type
572 || c_parser_peek_2nd_token (parser)->type == CPP_NAME
573 || c_parser_peek_2nd_token (parser)->type == CPP_MULT)
574
575 /* Only unknown identifiers. */
576 && !lookup_name (token->value))
577 return true;
578
579 return false;
580 }
581
582 /* Return true if TOKEN is a type qualifier, false otherwise. */
583 static bool
584 c_token_is_qualifier (c_token *token)
585 {
586 switch (token->type)
587 {
588 case CPP_NAME:
589 switch (token->id_kind)
590 {
591 case C_ID_ADDRSPACE:
592 return true;
593 default:
594 return false;
595 }
596 case CPP_KEYWORD:
597 switch (token->keyword)
598 {
599 case RID_CONST:
600 case RID_VOLATILE:
601 case RID_RESTRICT:
602 case RID_ATTRIBUTE:
603 case RID_ATOMIC:
604 return true;
605 default:
606 return false;
607 }
608 case CPP_LESS:
609 return false;
610 default:
611 gcc_unreachable ();
612 }
613 }
614
615 /* Return true if the next token from PARSER is a type qualifier,
616 false otherwise. */
617 static inline bool
618 c_parser_next_token_is_qualifier (c_parser *parser)
619 {
620 c_token *token = c_parser_peek_token (parser);
621 return c_token_is_qualifier (token);
622 }
623
624 /* Return true if TOKEN can start declaration specifiers, false
625 otherwise. */
626 static bool
627 c_token_starts_declspecs (c_token *token)
628 {
629 switch (token->type)
630 {
631 case CPP_NAME:
632 switch (token->id_kind)
633 {
634 case C_ID_ID:
635 return false;
636 case C_ID_ADDRSPACE:
637 return true;
638 case C_ID_TYPENAME:
639 return true;
640 case C_ID_CLASSNAME:
641 gcc_assert (c_dialect_objc ());
642 return true;
643 default:
644 gcc_unreachable ();
645 }
646 case CPP_KEYWORD:
647 switch (token->keyword)
648 {
649 case RID_STATIC:
650 case RID_EXTERN:
651 case RID_REGISTER:
652 case RID_TYPEDEF:
653 case RID_INLINE:
654 case RID_NORETURN:
655 case RID_AUTO:
656 case RID_THREAD:
657 case RID_UNSIGNED:
658 case RID_LONG:
659 case RID_SHORT:
660 case RID_SIGNED:
661 case RID_COMPLEX:
662 case RID_INT:
663 case RID_CHAR:
664 case RID_FLOAT:
665 case RID_DOUBLE:
666 case RID_VOID:
667 case RID_DFLOAT32:
668 case RID_DFLOAT64:
669 case RID_DFLOAT128:
670 CASE_RID_FLOATN_NX:
671 case RID_BOOL:
672 case RID_ENUM:
673 case RID_STRUCT:
674 case RID_UNION:
675 case RID_TYPEOF:
676 case RID_CONST:
677 case RID_VOLATILE:
678 case RID_RESTRICT:
679 case RID_ATTRIBUTE:
680 case RID_FRACT:
681 case RID_ACCUM:
682 case RID_SAT:
683 case RID_ALIGNAS:
684 case RID_ATOMIC:
685 case RID_AUTO_TYPE:
686 return true;
687 default:
688 if (token->keyword >= RID_FIRST_INT_N
689 && token->keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
690 && int_n_enabled_p[token->keyword - RID_FIRST_INT_N])
691 return true;
692 return false;
693 }
694 case CPP_LESS:
695 if (c_dialect_objc ())
696 return true;
697 return false;
698 default:
699 return false;
700 }
701 }
702
703
704 /* Return true if TOKEN can start declaration specifiers or a static
705 assertion, false otherwise. */
706 static bool
707 c_token_starts_declaration (c_token *token)
708 {
709 if (c_token_starts_declspecs (token)
710 || token->keyword == RID_STATIC_ASSERT)
711 return true;
712 else
713 return false;
714 }
715
716 /* Return true if the next token from PARSER can start declaration
717 specifiers, false otherwise. */
718 bool
719 c_parser_next_token_starts_declspecs (c_parser *parser)
720 {
721 c_token *token = c_parser_peek_token (parser);
722
723 /* In Objective-C, a classname normally starts a declspecs unless it
724 is immediately followed by a dot. In that case, it is the
725 Objective-C 2.0 "dot-syntax" for class objects, ie, calls the
726 setter/getter on the class. c_token_starts_declspecs() can't
727 differentiate between the two cases because it only checks the
728 current token, so we have a special check here. */
729 if (c_dialect_objc ()
730 && token->type == CPP_NAME
731 && token->id_kind == C_ID_CLASSNAME
732 && c_parser_peek_2nd_token (parser)->type == CPP_DOT)
733 return false;
734
735 return c_token_starts_declspecs (token);
736 }
737
738 /* Return true if the next tokens from PARSER can start declaration
739 specifiers or a static assertion, false otherwise. */
740 bool
741 c_parser_next_tokens_start_declaration (c_parser *parser)
742 {
743 c_token *token = c_parser_peek_token (parser);
744
745 /* Same as above. */
746 if (c_dialect_objc ()
747 && token->type == CPP_NAME
748 && token->id_kind == C_ID_CLASSNAME
749 && c_parser_peek_2nd_token (parser)->type == CPP_DOT)
750 return false;
751
752 /* Labels do not start declarations. */
753 if (token->type == CPP_NAME
754 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
755 return false;
756
757 if (c_token_starts_declaration (token))
758 return true;
759
760 if (c_parser_next_tokens_start_typename (parser, cla_nonabstract_decl))
761 return true;
762
763 return false;
764 }
765
766 /* Consume the next token from PARSER. */
767
768 void
769 c_parser_consume_token (c_parser *parser)
770 {
771 gcc_assert (parser->tokens_avail >= 1);
772 gcc_assert (parser->tokens[0].type != CPP_EOF);
773 gcc_assert (!parser->in_pragma || parser->tokens[0].type != CPP_PRAGMA_EOL);
774 gcc_assert (parser->error || parser->tokens[0].type != CPP_PRAGMA);
775 parser->last_token_location = parser->tokens[0].location;
776 if (parser->tokens != &parser->tokens_buf[0])
777 parser->tokens++;
778 else if (parser->tokens_avail == 2)
779 parser->tokens[0] = parser->tokens[1];
780 parser->tokens_avail--;
781 }
782
783 /* Expect the current token to be a #pragma. Consume it and remember
784 that we've begun parsing a pragma. */
785
786 static void
787 c_parser_consume_pragma (c_parser *parser)
788 {
789 gcc_assert (!parser->in_pragma);
790 gcc_assert (parser->tokens_avail >= 1);
791 gcc_assert (parser->tokens[0].type == CPP_PRAGMA);
792 if (parser->tokens != &parser->tokens_buf[0])
793 parser->tokens++;
794 else if (parser->tokens_avail == 2)
795 parser->tokens[0] = parser->tokens[1];
796 parser->tokens_avail--;
797 parser->in_pragma = true;
798 }
799
800 /* Update the global input_location from TOKEN. */
801 static inline void
802 c_parser_set_source_position_from_token (c_token *token)
803 {
804 if (token->type != CPP_EOF)
805 {
806 input_location = token->location;
807 }
808 }
809
810 /* Helper function for c_parser_error.
811 Having peeked a token of kind TOK1_KIND that might signify
812 a conflict marker, peek successor tokens to determine
813 if we actually do have a conflict marker.
814 Specifically, we consider a run of 7 '<', '=' or '>' characters
815 at the start of a line as a conflict marker.
816 These come through the lexer as three pairs and a single,
817 e.g. three CPP_LSHIFT ("<<") and a CPP_LESS ('<').
818 If it returns true, *OUT_LOC is written to with the location/range
819 of the marker. */
820
821 static bool
822 c_parser_peek_conflict_marker (c_parser *parser, enum cpp_ttype tok1_kind,
823 location_t *out_loc)
824 {
825 c_token *token2 = c_parser_peek_2nd_token (parser);
826 if (token2->type != tok1_kind)
827 return false;
828 c_token *token3 = c_parser_peek_nth_token (parser, 3);
829 if (token3->type != tok1_kind)
830 return false;
831 c_token *token4 = c_parser_peek_nth_token (parser, 4);
832 if (token4->type != conflict_marker_get_final_tok_kind (tok1_kind))
833 return false;
834
835 /* It must be at the start of the line. */
836 location_t start_loc = c_parser_peek_token (parser)->location;
837 if (LOCATION_COLUMN (start_loc) != 1)
838 return false;
839
840 /* We have a conflict marker. Construct a location of the form:
841 <<<<<<<
842 ^~~~~~~
843 with start == caret, finishing at the end of the marker. */
844 location_t finish_loc = get_finish (token4->location);
845 *out_loc = make_location (start_loc, start_loc, finish_loc);
846
847 return true;
848 }
849
850 /* Issue a diagnostic of the form
851 FILE:LINE: MESSAGE before TOKEN
852 where TOKEN is the next token in the input stream of PARSER.
853 MESSAGE (specified by the caller) is usually of the form "expected
854 OTHER-TOKEN".
855
856 Use RICHLOC as the location of the diagnostic.
857
858 Do not issue a diagnostic if still recovering from an error.
859
860 Return true iff an error was actually emitted.
861
862 ??? This is taken from the C++ parser, but building up messages in
863 this way is not i18n-friendly and some other approach should be
864 used. */
865
866 static bool
867 c_parser_error_richloc (c_parser *parser, const char *gmsgid,
868 rich_location *richloc)
869 {
870 c_token *token = c_parser_peek_token (parser);
871 if (parser->error)
872 return false;
873 parser->error = true;
874 if (!gmsgid)
875 return false;
876
877 /* If this is actually a conflict marker, report it as such. */
878 if (token->type == CPP_LSHIFT
879 || token->type == CPP_RSHIFT
880 || token->type == CPP_EQ_EQ)
881 {
882 location_t loc;
883 if (c_parser_peek_conflict_marker (parser, token->type, &loc))
884 {
885 error_at (loc, "version control conflict marker in file");
886 return true;
887 }
888 }
889
890 c_parse_error (gmsgid,
891 /* Because c_parse_error does not understand
892 CPP_KEYWORD, keywords are treated like
893 identifiers. */
894 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
895 /* ??? The C parser does not save the cpp flags of a
896 token, we need to pass 0 here and we will not get
897 the source spelling of some tokens but rather the
898 canonical spelling. */
899 token->value, /*flags=*/0, richloc);
900 return true;
901 }
902
903 /* As c_parser_error_richloc, but issue the message at the
904 location of PARSER's next token, or at input_location
905 if the next token is EOF. */
906
907 bool
908 c_parser_error (c_parser *parser, const char *gmsgid)
909 {
910 c_token *token = c_parser_peek_token (parser);
911 c_parser_set_source_position_from_token (token);
912 rich_location richloc (line_table, input_location);
913 return c_parser_error_richloc (parser, gmsgid, &richloc);
914 }
915
916 /* Some tokens naturally come in pairs e.g.'(' and ')'.
917 This class is for tracking such a matching pair of symbols.
918 In particular, it tracks the location of the first token,
919 so that if the second token is missing, we can highlight the
920 location of the first token when notifying the user about the
921 problem. */
922
923 template <typename traits_t>
924 class token_pair
925 {
926 public:
927 /* token_pair's ctor. */
928 token_pair () : m_open_loc (UNKNOWN_LOCATION) {}
929
930 /* If the next token is the opening symbol for this pair, consume it and
931 return true.
932 Otherwise, issue an error and return false.
933 In either case, record the location of the opening token. */
934
935 bool require_open (c_parser *parser)
936 {
937 c_token *token = c_parser_peek_token (parser);
938 if (token)
939 m_open_loc = token->location;
940
941 return c_parser_require (parser, traits_t::open_token_type,
942 traits_t::open_gmsgid);
943 }
944
945 /* Consume the next token from PARSER, recording its location as
946 that of the opening token within the pair. */
947
948 void consume_open (c_parser *parser)
949 {
950 c_token *token = c_parser_peek_token (parser);
951 gcc_assert (token->type == traits_t::open_token_type);
952 m_open_loc = token->location;
953 c_parser_consume_token (parser);
954 }
955
956 /* If the next token is the closing symbol for this pair, consume it
957 and return true.
958 Otherwise, issue an error, highlighting the location of the
959 corresponding opening token, and return false. */
960
961 bool require_close (c_parser *parser) const
962 {
963 return c_parser_require (parser, traits_t::close_token_type,
964 traits_t::close_gmsgid, m_open_loc);
965 }
966
967 /* Like token_pair::require_close, except that tokens will be skipped
968 until the desired token is found. An error message is still produced
969 if the next token is not as expected. */
970
971 void skip_until_found_close (c_parser *parser) const
972 {
973 c_parser_skip_until_found (parser, traits_t::close_token_type,
974 traits_t::close_gmsgid, m_open_loc);
975 }
976
977 private:
978 location_t m_open_loc;
979 };
980
981 /* Traits for token_pair<T> for tracking matching pairs of parentheses. */
982
983 struct matching_paren_traits
984 {
985 static const enum cpp_ttype open_token_type = CPP_OPEN_PAREN;
986 static const char * const open_gmsgid;
987 static const enum cpp_ttype close_token_type = CPP_CLOSE_PAREN;
988 static const char * const close_gmsgid;
989 };
990
991 const char * const matching_paren_traits::open_gmsgid = "expected %<(%>";
992 const char * const matching_paren_traits::close_gmsgid = "expected %<)%>";
993
994 /* "matching_parens" is a token_pair<T> class for tracking matching
995 pairs of parentheses. */
996
997 typedef token_pair<matching_paren_traits> matching_parens;
998
999 /* Traits for token_pair<T> for tracking matching pairs of braces. */
1000
1001 struct matching_brace_traits
1002 {
1003 static const enum cpp_ttype open_token_type = CPP_OPEN_BRACE;
1004 static const char * const open_gmsgid;
1005 static const enum cpp_ttype close_token_type = CPP_CLOSE_BRACE;
1006 static const char * const close_gmsgid;
1007 };
1008
1009 const char * const matching_brace_traits::open_gmsgid = "expected %<{%>";
1010 const char * const matching_brace_traits::close_gmsgid = "expected %<}%>";
1011
1012 /* "matching_braces" is a token_pair<T> class for tracking matching
1013 pairs of braces. */
1014
1015 typedef token_pair<matching_brace_traits> matching_braces;
1016
1017 /* Get a description of the matching symbol to TYPE e.g. "(" for
1018 CPP_CLOSE_PAREN. */
1019
1020 static const char *
1021 get_matching_symbol (enum cpp_ttype type)
1022 {
1023 switch (type)
1024 {
1025 default:
1026 gcc_unreachable ();
1027 return "";
1028 case CPP_CLOSE_PAREN:
1029 return "(";
1030 case CPP_CLOSE_BRACE:
1031 return "{";
1032 }
1033 }
1034
1035 /* If the next token is of the indicated TYPE, consume it. Otherwise,
1036 issue the error MSGID. If MSGID is NULL then a message has already
1037 been produced and no message will be produced this time. Returns
1038 true if found, false otherwise.
1039
1040 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
1041 within any error as the location of an "opening" token matching
1042 the close token TYPE (e.g. the location of the '(' when TYPE is
1043 CPP_CLOSE_PAREN).
1044
1045 If TYPE_IS_UNIQUE is true (the default) then msgid describes exactly
1046 one type (e.g. "expected %<)%>") and thus it may be reasonable to
1047 attempt to generate a fix-it hint for the problem.
1048 Otherwise msgid describes multiple token types (e.g.
1049 "expected %<;%>, %<,%> or %<)%>"), and thus we shouldn't attempt to
1050 generate a fix-it hint. */
1051
1052 bool
1053 c_parser_require (c_parser *parser,
1054 enum cpp_ttype type,
1055 const char *msgid,
1056 location_t matching_location,
1057 bool type_is_unique)
1058 {
1059 if (c_parser_next_token_is (parser, type))
1060 {
1061 c_parser_consume_token (parser);
1062 return true;
1063 }
1064 else
1065 {
1066 location_t next_token_loc = c_parser_peek_token (parser)->location;
1067 gcc_rich_location richloc (next_token_loc);
1068
1069 /* Potentially supply a fix-it hint, suggesting to add the
1070 missing token immediately after the *previous* token.
1071 This may move the primary location within richloc. */
1072 if (!parser->error && type_is_unique)
1073 maybe_suggest_missing_token_insertion (&richloc, type,
1074 parser->last_token_location);
1075
1076 /* If matching_location != UNKNOWN_LOCATION, highlight it.
1077 Attempt to consolidate diagnostics by printing it as a
1078 secondary range within the main diagnostic. */
1079 bool added_matching_location = false;
1080 if (matching_location != UNKNOWN_LOCATION)
1081 added_matching_location
1082 = richloc.add_location_if_nearby (matching_location);
1083
1084 if (c_parser_error_richloc (parser, msgid, &richloc))
1085 /* If we weren't able to consolidate matching_location, then
1086 print it as a secondary diagnostic. */
1087 if (matching_location != UNKNOWN_LOCATION && !added_matching_location)
1088 inform (matching_location, "to match this %qs",
1089 get_matching_symbol (type));
1090
1091 return false;
1092 }
1093 }
1094
1095 /* If the next token is the indicated keyword, consume it. Otherwise,
1096 issue the error MSGID. Returns true if found, false otherwise. */
1097
1098 static bool
1099 c_parser_require_keyword (c_parser *parser,
1100 enum rid keyword,
1101 const char *msgid)
1102 {
1103 if (c_parser_next_token_is_keyword (parser, keyword))
1104 {
1105 c_parser_consume_token (parser);
1106 return true;
1107 }
1108 else
1109 {
1110 c_parser_error (parser, msgid);
1111 return false;
1112 }
1113 }
1114
1115 /* Like c_parser_require, except that tokens will be skipped until the
1116 desired token is found. An error message is still produced if the
1117 next token is not as expected. If MSGID is NULL then a message has
1118 already been produced and no message will be produced this
1119 time.
1120
1121 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
1122 within any error as the location of an "opening" token matching
1123 the close token TYPE (e.g. the location of the '(' when TYPE is
1124 CPP_CLOSE_PAREN). */
1125
1126 void
1127 c_parser_skip_until_found (c_parser *parser,
1128 enum cpp_ttype type,
1129 const char *msgid,
1130 location_t matching_location)
1131 {
1132 unsigned nesting_depth = 0;
1133
1134 if (c_parser_require (parser, type, msgid, matching_location))
1135 return;
1136
1137 /* Skip tokens until the desired token is found. */
1138 while (true)
1139 {
1140 /* Peek at the next token. */
1141 c_token *token = c_parser_peek_token (parser);
1142 /* If we've reached the token we want, consume it and stop. */
1143 if (token->type == type && !nesting_depth)
1144 {
1145 c_parser_consume_token (parser);
1146 break;
1147 }
1148
1149 /* If we've run out of tokens, stop. */
1150 if (token->type == CPP_EOF)
1151 return;
1152 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
1153 return;
1154 if (token->type == CPP_OPEN_BRACE
1155 || token->type == CPP_OPEN_PAREN
1156 || token->type == CPP_OPEN_SQUARE)
1157 ++nesting_depth;
1158 else if (token->type == CPP_CLOSE_BRACE
1159 || token->type == CPP_CLOSE_PAREN
1160 || token->type == CPP_CLOSE_SQUARE)
1161 {
1162 if (nesting_depth-- == 0)
1163 break;
1164 }
1165 /* Consume this token. */
1166 c_parser_consume_token (parser);
1167 }
1168 parser->error = false;
1169 }
1170
1171 /* Skip tokens until the end of a parameter is found, but do not
1172 consume the comma, semicolon or closing delimiter. */
1173
1174 static void
1175 c_parser_skip_to_end_of_parameter (c_parser *parser)
1176 {
1177 unsigned nesting_depth = 0;
1178
1179 while (true)
1180 {
1181 c_token *token = c_parser_peek_token (parser);
1182 if ((token->type == CPP_COMMA || token->type == CPP_SEMICOLON)
1183 && !nesting_depth)
1184 break;
1185 /* If we've run out of tokens, stop. */
1186 if (token->type == CPP_EOF)
1187 return;
1188 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
1189 return;
1190 if (token->type == CPP_OPEN_BRACE
1191 || token->type == CPP_OPEN_PAREN
1192 || token->type == CPP_OPEN_SQUARE)
1193 ++nesting_depth;
1194 else if (token->type == CPP_CLOSE_BRACE
1195 || token->type == CPP_CLOSE_PAREN
1196 || token->type == CPP_CLOSE_SQUARE)
1197 {
1198 if (nesting_depth-- == 0)
1199 break;
1200 }
1201 /* Consume this token. */
1202 c_parser_consume_token (parser);
1203 }
1204 parser->error = false;
1205 }
1206
1207 /* Expect to be at the end of the pragma directive and consume an
1208 end of line marker. */
1209
1210 static void
1211 c_parser_skip_to_pragma_eol (c_parser *parser, bool error_if_not_eol = true)
1212 {
1213 gcc_assert (parser->in_pragma);
1214 parser->in_pragma = false;
1215
1216 if (error_if_not_eol && c_parser_peek_token (parser)->type != CPP_PRAGMA_EOL)
1217 c_parser_error (parser, "expected end of line");
1218
1219 cpp_ttype token_type;
1220 do
1221 {
1222 c_token *token = c_parser_peek_token (parser);
1223 token_type = token->type;
1224 if (token_type == CPP_EOF)
1225 break;
1226 c_parser_consume_token (parser);
1227 }
1228 while (token_type != CPP_PRAGMA_EOL);
1229
1230 parser->error = false;
1231 }
1232
1233 /* Skip tokens until we have consumed an entire block, or until we
1234 have consumed a non-nested ';'. */
1235
1236 static void
1237 c_parser_skip_to_end_of_block_or_statement (c_parser *parser)
1238 {
1239 unsigned nesting_depth = 0;
1240 bool save_error = parser->error;
1241
1242 while (true)
1243 {
1244 c_token *token;
1245
1246 /* Peek at the next token. */
1247 token = c_parser_peek_token (parser);
1248
1249 switch (token->type)
1250 {
1251 case CPP_EOF:
1252 return;
1253
1254 case CPP_PRAGMA_EOL:
1255 if (parser->in_pragma)
1256 return;
1257 break;
1258
1259 case CPP_SEMICOLON:
1260 /* If the next token is a ';', we have reached the
1261 end of the statement. */
1262 if (!nesting_depth)
1263 {
1264 /* Consume the ';'. */
1265 c_parser_consume_token (parser);
1266 goto finished;
1267 }
1268 break;
1269
1270 case CPP_CLOSE_BRACE:
1271 /* If the next token is a non-nested '}', then we have
1272 reached the end of the current block. */
1273 if (nesting_depth == 0 || --nesting_depth == 0)
1274 {
1275 c_parser_consume_token (parser);
1276 goto finished;
1277 }
1278 break;
1279
1280 case CPP_OPEN_BRACE:
1281 /* If it the next token is a '{', then we are entering a new
1282 block. Consume the entire block. */
1283 ++nesting_depth;
1284 break;
1285
1286 case CPP_PRAGMA:
1287 /* If we see a pragma, consume the whole thing at once. We
1288 have some safeguards against consuming pragmas willy-nilly.
1289 Normally, we'd expect to be here with parser->error set,
1290 which disables these safeguards. But it's possible to get
1291 here for secondary error recovery, after parser->error has
1292 been cleared. */
1293 c_parser_consume_pragma (parser);
1294 c_parser_skip_to_pragma_eol (parser);
1295 parser->error = save_error;
1296 continue;
1297
1298 default:
1299 break;
1300 }
1301
1302 c_parser_consume_token (parser);
1303 }
1304
1305 finished:
1306 parser->error = false;
1307 }
1308
1309 /* CPP's options (initialized by c-opts.c). */
1310 extern cpp_options *cpp_opts;
1311
1312 /* Save the warning flags which are controlled by __extension__. */
1313
1314 static inline int
1315 disable_extension_diagnostics (void)
1316 {
1317 int ret = (pedantic
1318 | (warn_pointer_arith << 1)
1319 | (warn_traditional << 2)
1320 | (flag_iso << 3)
1321 | (warn_long_long << 4)
1322 | (warn_cxx_compat << 5)
1323 | (warn_overlength_strings << 6)
1324 /* warn_c90_c99_compat has three states: -1/0/1, so we must
1325 play tricks to properly restore it. */
1326 | ((warn_c90_c99_compat == 1) << 7)
1327 | ((warn_c90_c99_compat == -1) << 8)
1328 /* Similarly for warn_c99_c11_compat. */
1329 | ((warn_c99_c11_compat == 1) << 9)
1330 | ((warn_c99_c11_compat == -1) << 10)
1331 /* Similarly for warn_c11_c2x_compat. */
1332 | ((warn_c11_c2x_compat == 1) << 11)
1333 | ((warn_c11_c2x_compat == -1) << 12)
1334 );
1335 cpp_opts->cpp_pedantic = pedantic = 0;
1336 warn_pointer_arith = 0;
1337 cpp_opts->cpp_warn_traditional = warn_traditional = 0;
1338 flag_iso = 0;
1339 cpp_opts->cpp_warn_long_long = warn_long_long = 0;
1340 warn_cxx_compat = 0;
1341 warn_overlength_strings = 0;
1342 warn_c90_c99_compat = 0;
1343 warn_c99_c11_compat = 0;
1344 warn_c11_c2x_compat = 0;
1345 return ret;
1346 }
1347
1348 /* Restore the warning flags which are controlled by __extension__.
1349 FLAGS is the return value from disable_extension_diagnostics. */
1350
1351 static inline void
1352 restore_extension_diagnostics (int flags)
1353 {
1354 cpp_opts->cpp_pedantic = pedantic = flags & 1;
1355 warn_pointer_arith = (flags >> 1) & 1;
1356 cpp_opts->cpp_warn_traditional = warn_traditional = (flags >> 2) & 1;
1357 flag_iso = (flags >> 3) & 1;
1358 cpp_opts->cpp_warn_long_long = warn_long_long = (flags >> 4) & 1;
1359 warn_cxx_compat = (flags >> 5) & 1;
1360 warn_overlength_strings = (flags >> 6) & 1;
1361 /* See above for why is this needed. */
1362 warn_c90_c99_compat = (flags >> 7) & 1 ? 1 : ((flags >> 8) & 1 ? -1 : 0);
1363 warn_c99_c11_compat = (flags >> 9) & 1 ? 1 : ((flags >> 10) & 1 ? -1 : 0);
1364 warn_c11_c2x_compat = (flags >> 11) & 1 ? 1 : ((flags >> 12) & 1 ? -1 : 0);
1365 }
1366
1367 /* Helper data structure for parsing #pragma acc routine. */
1368 struct oacc_routine_data {
1369 bool error_seen; /* Set if error has been reported. */
1370 bool fndecl_seen; /* Set if one fn decl/definition has been seen already. */
1371 tree clauses;
1372 location_t loc;
1373 };
1374
1375 static void c_parser_external_declaration (c_parser *);
1376 static void c_parser_asm_definition (c_parser *);
1377 static void c_parser_declaration_or_fndef (c_parser *, bool, bool, bool,
1378 bool, bool, tree *, vec<c_token>,
1379 struct oacc_routine_data * = NULL,
1380 bool * = NULL);
1381 static void c_parser_static_assert_declaration_no_semi (c_parser *);
1382 static void c_parser_static_assert_declaration (c_parser *);
1383 static struct c_typespec c_parser_enum_specifier (c_parser *);
1384 static struct c_typespec c_parser_struct_or_union_specifier (c_parser *);
1385 static tree c_parser_struct_declaration (c_parser *);
1386 static struct c_typespec c_parser_typeof_specifier (c_parser *);
1387 static tree c_parser_alignas_specifier (c_parser *);
1388 static struct c_declarator *c_parser_direct_declarator (c_parser *, bool,
1389 c_dtr_syn, bool *);
1390 static struct c_declarator *c_parser_direct_declarator_inner (c_parser *,
1391 bool,
1392 struct c_declarator *);
1393 static struct c_arg_info *c_parser_parms_declarator (c_parser *, bool, tree);
1394 static struct c_arg_info *c_parser_parms_list_declarator (c_parser *, tree,
1395 tree);
1396 static struct c_parm *c_parser_parameter_declaration (c_parser *, tree);
1397 static tree c_parser_simple_asm_expr (c_parser *);
1398 static tree c_parser_attributes (c_parser *);
1399 static struct c_expr c_parser_initializer (c_parser *);
1400 static struct c_expr c_parser_braced_init (c_parser *, tree, bool,
1401 struct obstack *);
1402 static void c_parser_initelt (c_parser *, struct obstack *);
1403 static void c_parser_initval (c_parser *, struct c_expr *,
1404 struct obstack *);
1405 static tree c_parser_compound_statement (c_parser *);
1406 static void c_parser_compound_statement_nostart (c_parser *);
1407 static void c_parser_label (c_parser *);
1408 static void c_parser_statement (c_parser *, bool *, location_t * = NULL);
1409 static void c_parser_statement_after_labels (c_parser *, bool *,
1410 vec<tree> * = NULL);
1411 static tree c_parser_c99_block_statement (c_parser *, bool *,
1412 location_t * = NULL);
1413 static void c_parser_if_statement (c_parser *, bool *, vec<tree> *);
1414 static void c_parser_switch_statement (c_parser *, bool *);
1415 static void c_parser_while_statement (c_parser *, bool, unsigned short, bool *);
1416 static void c_parser_do_statement (c_parser *, bool, unsigned short);
1417 static void c_parser_for_statement (c_parser *, bool, unsigned short, bool *);
1418 static tree c_parser_asm_statement (c_parser *);
1419 static tree c_parser_asm_operands (c_parser *);
1420 static tree c_parser_asm_goto_operands (c_parser *);
1421 static tree c_parser_asm_clobbers (c_parser *);
1422 static struct c_expr c_parser_expr_no_commas (c_parser *, struct c_expr *,
1423 tree = NULL_TREE);
1424 static struct c_expr c_parser_conditional_expression (c_parser *,
1425 struct c_expr *, tree);
1426 static struct c_expr c_parser_binary_expression (c_parser *, struct c_expr *,
1427 tree);
1428 static struct c_expr c_parser_cast_expression (c_parser *, struct c_expr *);
1429 static struct c_expr c_parser_unary_expression (c_parser *);
1430 static struct c_expr c_parser_sizeof_expression (c_parser *);
1431 static struct c_expr c_parser_alignof_expression (c_parser *);
1432 static struct c_expr c_parser_postfix_expression (c_parser *);
1433 static struct c_expr c_parser_postfix_expression_after_paren_type (c_parser *,
1434 struct c_type_name *,
1435 location_t);
1436 static struct c_expr c_parser_postfix_expression_after_primary (c_parser *,
1437 location_t loc,
1438 struct c_expr);
1439 static tree c_parser_transaction (c_parser *, enum rid);
1440 static struct c_expr c_parser_transaction_expression (c_parser *, enum rid);
1441 static tree c_parser_transaction_cancel (c_parser *);
1442 static struct c_expr c_parser_expression (c_parser *);
1443 static struct c_expr c_parser_expression_conv (c_parser *);
1444 static vec<tree, va_gc> *c_parser_expr_list (c_parser *, bool, bool,
1445 vec<tree, va_gc> **, location_t *,
1446 tree *, vec<location_t> *,
1447 unsigned int * = NULL);
1448 static struct c_expr c_parser_has_attribute_expression (c_parser *);
1449
1450 static void c_parser_oacc_declare (c_parser *);
1451 static void c_parser_oacc_enter_exit_data (c_parser *, bool);
1452 static void c_parser_oacc_update (c_parser *);
1453 static void c_parser_omp_construct (c_parser *, bool *);
1454 static void c_parser_omp_threadprivate (c_parser *);
1455 static void c_parser_omp_barrier (c_parser *);
1456 static void c_parser_omp_depobj (c_parser *);
1457 static void c_parser_omp_flush (c_parser *);
1458 static tree c_parser_omp_for_loop (location_t, c_parser *, enum tree_code,
1459 tree, tree *, bool *);
1460 static void c_parser_omp_taskwait (c_parser *);
1461 static void c_parser_omp_taskyield (c_parser *);
1462 static void c_parser_omp_cancel (c_parser *);
1463
1464 enum pragma_context { pragma_external, pragma_struct, pragma_param,
1465 pragma_stmt, pragma_compound };
1466 static bool c_parser_pragma (c_parser *, enum pragma_context, bool *);
1467 static void c_parser_omp_cancellation_point (c_parser *, enum pragma_context);
1468 static bool c_parser_omp_target (c_parser *, enum pragma_context, bool *);
1469 static void c_parser_omp_end_declare_target (c_parser *);
1470 static void c_parser_omp_declare (c_parser *, enum pragma_context);
1471 static void c_parser_omp_requires (c_parser *);
1472 static bool c_parser_omp_ordered (c_parser *, enum pragma_context, bool *);
1473 static void c_parser_oacc_routine (c_parser *, enum pragma_context);
1474
1475 /* These Objective-C parser functions are only ever called when
1476 compiling Objective-C. */
1477 static void c_parser_objc_class_definition (c_parser *, tree);
1478 static void c_parser_objc_class_instance_variables (c_parser *);
1479 static void c_parser_objc_class_declaration (c_parser *);
1480 static void c_parser_objc_alias_declaration (c_parser *);
1481 static void c_parser_objc_protocol_definition (c_parser *, tree);
1482 static bool c_parser_objc_method_type (c_parser *);
1483 static void c_parser_objc_method_definition (c_parser *);
1484 static void c_parser_objc_methodprotolist (c_parser *);
1485 static void c_parser_objc_methodproto (c_parser *);
1486 static tree c_parser_objc_method_decl (c_parser *, bool, tree *, tree *);
1487 static tree c_parser_objc_type_name (c_parser *);
1488 static tree c_parser_objc_protocol_refs (c_parser *);
1489 static void c_parser_objc_try_catch_finally_statement (c_parser *);
1490 static void c_parser_objc_synchronized_statement (c_parser *);
1491 static tree c_parser_objc_selector (c_parser *);
1492 static tree c_parser_objc_selector_arg (c_parser *);
1493 static tree c_parser_objc_receiver (c_parser *);
1494 static tree c_parser_objc_message_args (c_parser *);
1495 static tree c_parser_objc_keywordexpr (c_parser *);
1496 static void c_parser_objc_at_property_declaration (c_parser *);
1497 static void c_parser_objc_at_synthesize_declaration (c_parser *);
1498 static void c_parser_objc_at_dynamic_declaration (c_parser *);
1499 static bool c_parser_objc_diagnose_bad_element_prefix
1500 (c_parser *, struct c_declspecs *);
1501
1502 static void c_parser_parse_rtl_body (c_parser *parser, char *start_with_pass);
1503
1504 /* Parse a translation unit (C90 6.7, C99 6.9, C11 6.9).
1505
1506 translation-unit:
1507 external-declarations
1508
1509 external-declarations:
1510 external-declaration
1511 external-declarations external-declaration
1512
1513 GNU extensions:
1514
1515 translation-unit:
1516 empty
1517 */
1518
1519 static void
1520 c_parser_translation_unit (c_parser *parser)
1521 {
1522 if (c_parser_next_token_is (parser, CPP_EOF))
1523 {
1524 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
1525 "ISO C forbids an empty translation unit");
1526 }
1527 else
1528 {
1529 void *obstack_position = obstack_alloc (&parser_obstack, 0);
1530 mark_valid_location_for_stdc_pragma (false);
1531 do
1532 {
1533 ggc_collect ();
1534 c_parser_external_declaration (parser);
1535 obstack_free (&parser_obstack, obstack_position);
1536 }
1537 while (c_parser_next_token_is_not (parser, CPP_EOF));
1538 }
1539
1540 unsigned int i;
1541 tree decl;
1542 FOR_EACH_VEC_ELT (incomplete_record_decls, i, decl)
1543 if (DECL_SIZE (decl) == NULL_TREE && TREE_TYPE (decl) != error_mark_node)
1544 error ("storage size of %q+D isn%'t known", decl);
1545 }
1546
1547 /* Parse an external declaration (C90 6.7, C99 6.9, C11 6.9).
1548
1549 external-declaration:
1550 function-definition
1551 declaration
1552
1553 GNU extensions:
1554
1555 external-declaration:
1556 asm-definition
1557 ;
1558 __extension__ external-declaration
1559
1560 Objective-C:
1561
1562 external-declaration:
1563 objc-class-definition
1564 objc-class-declaration
1565 objc-alias-declaration
1566 objc-protocol-definition
1567 objc-method-definition
1568 @end
1569 */
1570
1571 static void
1572 c_parser_external_declaration (c_parser *parser)
1573 {
1574 int ext;
1575 switch (c_parser_peek_token (parser)->type)
1576 {
1577 case CPP_KEYWORD:
1578 switch (c_parser_peek_token (parser)->keyword)
1579 {
1580 case RID_EXTENSION:
1581 ext = disable_extension_diagnostics ();
1582 c_parser_consume_token (parser);
1583 c_parser_external_declaration (parser);
1584 restore_extension_diagnostics (ext);
1585 break;
1586 case RID_ASM:
1587 c_parser_asm_definition (parser);
1588 break;
1589 case RID_AT_INTERFACE:
1590 case RID_AT_IMPLEMENTATION:
1591 gcc_assert (c_dialect_objc ());
1592 c_parser_objc_class_definition (parser, NULL_TREE);
1593 break;
1594 case RID_AT_CLASS:
1595 gcc_assert (c_dialect_objc ());
1596 c_parser_objc_class_declaration (parser);
1597 break;
1598 case RID_AT_ALIAS:
1599 gcc_assert (c_dialect_objc ());
1600 c_parser_objc_alias_declaration (parser);
1601 break;
1602 case RID_AT_PROTOCOL:
1603 gcc_assert (c_dialect_objc ());
1604 c_parser_objc_protocol_definition (parser, NULL_TREE);
1605 break;
1606 case RID_AT_PROPERTY:
1607 gcc_assert (c_dialect_objc ());
1608 c_parser_objc_at_property_declaration (parser);
1609 break;
1610 case RID_AT_SYNTHESIZE:
1611 gcc_assert (c_dialect_objc ());
1612 c_parser_objc_at_synthesize_declaration (parser);
1613 break;
1614 case RID_AT_DYNAMIC:
1615 gcc_assert (c_dialect_objc ());
1616 c_parser_objc_at_dynamic_declaration (parser);
1617 break;
1618 case RID_AT_END:
1619 gcc_assert (c_dialect_objc ());
1620 c_parser_consume_token (parser);
1621 objc_finish_implementation ();
1622 break;
1623 default:
1624 goto decl_or_fndef;
1625 }
1626 break;
1627 case CPP_SEMICOLON:
1628 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
1629 "ISO C does not allow extra %<;%> outside of a function");
1630 c_parser_consume_token (parser);
1631 break;
1632 case CPP_PRAGMA:
1633 mark_valid_location_for_stdc_pragma (true);
1634 c_parser_pragma (parser, pragma_external, NULL);
1635 mark_valid_location_for_stdc_pragma (false);
1636 break;
1637 case CPP_PLUS:
1638 case CPP_MINUS:
1639 if (c_dialect_objc ())
1640 {
1641 c_parser_objc_method_definition (parser);
1642 break;
1643 }
1644 /* Else fall through, and yield a syntax error trying to parse
1645 as a declaration or function definition. */
1646 /* FALLTHRU */
1647 default:
1648 decl_or_fndef:
1649 /* A declaration or a function definition (or, in Objective-C,
1650 an @interface or @protocol with prefix attributes). We can
1651 only tell which after parsing the declaration specifiers, if
1652 any, and the first declarator. */
1653 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
1654 NULL, vNULL);
1655 break;
1656 }
1657 }
1658
1659 static void c_finish_omp_declare_simd (c_parser *, tree, tree, vec<c_token>);
1660 static void c_finish_oacc_routine (struct oacc_routine_data *, tree, bool);
1661
1662 /* Build and add a DEBUG_BEGIN_STMT statement with location LOC. */
1663
1664 static void
1665 add_debug_begin_stmt (location_t loc)
1666 {
1667 /* Don't add DEBUG_BEGIN_STMTs outside of functions, see PR84721. */
1668 if (!MAY_HAVE_DEBUG_MARKER_STMTS || !building_stmt_list_p ())
1669 return;
1670
1671 tree stmt = build0 (DEBUG_BEGIN_STMT, void_type_node);
1672 SET_EXPR_LOCATION (stmt, loc);
1673 add_stmt (stmt);
1674 }
1675
1676 /* Parse a declaration or function definition (C90 6.5, 6.7.1, C99
1677 6.7, 6.9.1, C11 6.7, 6.9.1). If FNDEF_OK is true, a function definition
1678 is accepted; otherwise (old-style parameter declarations) only other
1679 declarations are accepted. If STATIC_ASSERT_OK is true, a static
1680 assertion is accepted; otherwise (old-style parameter declarations)
1681 it is not. If NESTED is true, we are inside a function or parsing
1682 old-style parameter declarations; any functions encountered are
1683 nested functions and declaration specifiers are required; otherwise
1684 we are at top level and functions are normal functions and
1685 declaration specifiers may be optional. If EMPTY_OK is true, empty
1686 declarations are OK (subject to all other constraints); otherwise
1687 (old-style parameter declarations) they are diagnosed. If
1688 START_ATTR_OK is true, the declaration specifiers may start with
1689 attributes; otherwise they may not.
1690 OBJC_FOREACH_OBJECT_DECLARATION can be used to get back the parsed
1691 declaration when parsing an Objective-C foreach statement.
1692 FALLTHRU_ATTR_P is used to signal whether this function parsed
1693 "__attribute__((fallthrough));".
1694
1695 declaration:
1696 declaration-specifiers init-declarator-list[opt] ;
1697 static_assert-declaration
1698
1699 function-definition:
1700 declaration-specifiers[opt] declarator declaration-list[opt]
1701 compound-statement
1702
1703 declaration-list:
1704 declaration
1705 declaration-list declaration
1706
1707 init-declarator-list:
1708 init-declarator
1709 init-declarator-list , init-declarator
1710
1711 init-declarator:
1712 declarator simple-asm-expr[opt] attributes[opt]
1713 declarator simple-asm-expr[opt] attributes[opt] = initializer
1714
1715 GNU extensions:
1716
1717 nested-function-definition:
1718 declaration-specifiers declarator declaration-list[opt]
1719 compound-statement
1720
1721 attribute ;
1722
1723 Objective-C:
1724 attributes objc-class-definition
1725 attributes objc-category-definition
1726 attributes objc-protocol-definition
1727
1728 The simple-asm-expr and attributes are GNU extensions.
1729
1730 This function does not handle __extension__; that is handled in its
1731 callers. ??? Following the old parser, __extension__ may start
1732 external declarations, declarations in functions and declarations
1733 at the start of "for" loops, but not old-style parameter
1734 declarations.
1735
1736 C99 requires declaration specifiers in a function definition; the
1737 absence is diagnosed through the diagnosis of implicit int. In GNU
1738 C we also allow but diagnose declarations without declaration
1739 specifiers, but only at top level (elsewhere they conflict with
1740 other syntax).
1741
1742 In Objective-C, declarations of the looping variable in a foreach
1743 statement are exceptionally terminated by 'in' (for example, 'for
1744 (NSObject *object in array) { ... }').
1745
1746 OpenMP:
1747
1748 declaration:
1749 threadprivate-directive
1750
1751 GIMPLE:
1752
1753 gimple-function-definition:
1754 declaration-specifiers[opt] __GIMPLE (gimple-or-rtl-pass-list) declarator
1755 declaration-list[opt] compound-statement
1756
1757 rtl-function-definition:
1758 declaration-specifiers[opt] __RTL (gimple-or-rtl-pass-list) declarator
1759 declaration-list[opt] compound-statement */
1760
1761 static void
1762 c_parser_declaration_or_fndef (c_parser *parser, bool fndef_ok,
1763 bool static_assert_ok, bool empty_ok,
1764 bool nested, bool start_attr_ok,
1765 tree *objc_foreach_object_declaration,
1766 vec<c_token> omp_declare_simd_clauses,
1767 struct oacc_routine_data *oacc_routine_data,
1768 bool *fallthru_attr_p)
1769 {
1770 struct c_declspecs *specs;
1771 tree prefix_attrs;
1772 tree all_prefix_attrs;
1773 bool diagnosed_no_specs = false;
1774 location_t here = c_parser_peek_token (parser)->location;
1775
1776 add_debug_begin_stmt (c_parser_peek_token (parser)->location);
1777
1778 if (static_assert_ok
1779 && c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
1780 {
1781 c_parser_static_assert_declaration (parser);
1782 return;
1783 }
1784 specs = build_null_declspecs ();
1785
1786 /* Try to detect an unknown type name when we have "A B" or "A *B". */
1787 if (c_parser_peek_token (parser)->type == CPP_NAME
1788 && c_parser_peek_token (parser)->id_kind == C_ID_ID
1789 && (c_parser_peek_2nd_token (parser)->type == CPP_NAME
1790 || c_parser_peek_2nd_token (parser)->type == CPP_MULT)
1791 && (!nested || !lookup_name (c_parser_peek_token (parser)->value)))
1792 {
1793 tree name = c_parser_peek_token (parser)->value;
1794
1795 /* Issue a warning about NAME being an unknown type name, perhaps
1796 with some kind of hint.
1797 If the user forgot a "struct" etc, suggest inserting
1798 it. Otherwise, attempt to look for misspellings. */
1799 gcc_rich_location richloc (here);
1800 if (tag_exists_p (RECORD_TYPE, name))
1801 {
1802 /* This is not C++ with its implicit typedef. */
1803 richloc.add_fixit_insert_before ("struct ");
1804 error_at (&richloc,
1805 "unknown type name %qE;"
1806 " use %<struct%> keyword to refer to the type",
1807 name);
1808 }
1809 else if (tag_exists_p (UNION_TYPE, name))
1810 {
1811 richloc.add_fixit_insert_before ("union ");
1812 error_at (&richloc,
1813 "unknown type name %qE;"
1814 " use %<union%> keyword to refer to the type",
1815 name);
1816 }
1817 else if (tag_exists_p (ENUMERAL_TYPE, name))
1818 {
1819 richloc.add_fixit_insert_before ("enum ");
1820 error_at (&richloc,
1821 "unknown type name %qE;"
1822 " use %<enum%> keyword to refer to the type",
1823 name);
1824 }
1825 else
1826 {
1827 auto_diagnostic_group d;
1828 name_hint hint = lookup_name_fuzzy (name, FUZZY_LOOKUP_TYPENAME,
1829 here);
1830 if (const char *suggestion = hint.suggestion ())
1831 {
1832 richloc.add_fixit_replace (suggestion);
1833 error_at (&richloc,
1834 "unknown type name %qE; did you mean %qs?",
1835 name, suggestion);
1836 }
1837 else
1838 error_at (here, "unknown type name %qE", name);
1839 }
1840
1841 /* Parse declspecs normally to get a correct pointer type, but avoid
1842 a further "fails to be a type name" error. Refuse nested functions
1843 since it is not how the user likely wants us to recover. */
1844 c_parser_peek_token (parser)->type = CPP_KEYWORD;
1845 c_parser_peek_token (parser)->keyword = RID_VOID;
1846 c_parser_peek_token (parser)->value = error_mark_node;
1847 fndef_ok = !nested;
1848 }
1849
1850 c_parser_declspecs (parser, specs, true, true, start_attr_ok,
1851 true, true, cla_nonabstract_decl);
1852 if (parser->error)
1853 {
1854 c_parser_skip_to_end_of_block_or_statement (parser);
1855 return;
1856 }
1857 if (nested && !specs->declspecs_seen_p)
1858 {
1859 c_parser_error (parser, "expected declaration specifiers");
1860 c_parser_skip_to_end_of_block_or_statement (parser);
1861 return;
1862 }
1863
1864 finish_declspecs (specs);
1865 bool auto_type_p = specs->typespec_word == cts_auto_type;
1866 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1867 {
1868 if (auto_type_p)
1869 error_at (here, "%<__auto_type%> in empty declaration");
1870 else if (specs->typespec_kind == ctsk_none
1871 && attribute_fallthrough_p (specs->attrs))
1872 {
1873 if (fallthru_attr_p != NULL)
1874 *fallthru_attr_p = true;
1875 tree fn = build_call_expr_internal_loc (here, IFN_FALLTHROUGH,
1876 void_type_node, 0);
1877 add_stmt (fn);
1878 }
1879 else if (empty_ok)
1880 shadow_tag (specs);
1881 else
1882 {
1883 shadow_tag_warned (specs, 1);
1884 pedwarn (here, 0, "empty declaration");
1885 }
1886 c_parser_consume_token (parser);
1887 if (oacc_routine_data)
1888 c_finish_oacc_routine (oacc_routine_data, NULL_TREE, false);
1889 return;
1890 }
1891
1892 /* Provide better error recovery. Note that a type name here is usually
1893 better diagnosed as a redeclaration. */
1894 if (empty_ok
1895 && specs->typespec_kind == ctsk_tagdef
1896 && c_parser_next_token_starts_declspecs (parser)
1897 && !c_parser_next_token_is (parser, CPP_NAME))
1898 {
1899 c_parser_error (parser, "expected %<;%>, identifier or %<(%>");
1900 parser->error = false;
1901 shadow_tag_warned (specs, 1);
1902 return;
1903 }
1904 else if (c_dialect_objc () && !auto_type_p)
1905 {
1906 /* Prefix attributes are an error on method decls. */
1907 switch (c_parser_peek_token (parser)->type)
1908 {
1909 case CPP_PLUS:
1910 case CPP_MINUS:
1911 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1912 return;
1913 if (specs->attrs)
1914 {
1915 warning_at (c_parser_peek_token (parser)->location,
1916 OPT_Wattributes,
1917 "prefix attributes are ignored for methods");
1918 specs->attrs = NULL_TREE;
1919 }
1920 if (fndef_ok)
1921 c_parser_objc_method_definition (parser);
1922 else
1923 c_parser_objc_methodproto (parser);
1924 return;
1925 break;
1926 default:
1927 break;
1928 }
1929 /* This is where we parse 'attributes @interface ...',
1930 'attributes @implementation ...', 'attributes @protocol ...'
1931 (where attributes could be, for example, __attribute__
1932 ((deprecated)).
1933 */
1934 switch (c_parser_peek_token (parser)->keyword)
1935 {
1936 case RID_AT_INTERFACE:
1937 {
1938 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1939 return;
1940 c_parser_objc_class_definition (parser, specs->attrs);
1941 return;
1942 }
1943 break;
1944 case RID_AT_IMPLEMENTATION:
1945 {
1946 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1947 return;
1948 if (specs->attrs)
1949 {
1950 warning_at (c_parser_peek_token (parser)->location,
1951 OPT_Wattributes,
1952 "prefix attributes are ignored for implementations");
1953 specs->attrs = NULL_TREE;
1954 }
1955 c_parser_objc_class_definition (parser, NULL_TREE);
1956 return;
1957 }
1958 break;
1959 case RID_AT_PROTOCOL:
1960 {
1961 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1962 return;
1963 c_parser_objc_protocol_definition (parser, specs->attrs);
1964 return;
1965 }
1966 break;
1967 case RID_AT_ALIAS:
1968 case RID_AT_CLASS:
1969 case RID_AT_END:
1970 case RID_AT_PROPERTY:
1971 if (specs->attrs)
1972 {
1973 c_parser_error (parser, "unexpected attribute");
1974 specs->attrs = NULL;
1975 }
1976 break;
1977 default:
1978 break;
1979 }
1980 }
1981 else if (attribute_fallthrough_p (specs->attrs))
1982 warning_at (here, OPT_Wattributes,
1983 "%<fallthrough%> attribute not followed by %<;%>");
1984
1985 pending_xref_error ();
1986 prefix_attrs = specs->attrs;
1987 all_prefix_attrs = prefix_attrs;
1988 specs->attrs = NULL_TREE;
1989 while (true)
1990 {
1991 struct c_declarator *declarator;
1992 bool dummy = false;
1993 timevar_id_t tv;
1994 tree fnbody = NULL_TREE;
1995 /* Declaring either one or more declarators (in which case we
1996 should diagnose if there were no declaration specifiers) or a
1997 function definition (in which case the diagnostic for
1998 implicit int suffices). */
1999 declarator = c_parser_declarator (parser,
2000 specs->typespec_kind != ctsk_none,
2001 C_DTR_NORMAL, &dummy);
2002 if (declarator == NULL)
2003 {
2004 if (omp_declare_simd_clauses.exists ())
2005 c_finish_omp_declare_simd (parser, NULL_TREE, NULL_TREE,
2006 omp_declare_simd_clauses);
2007 if (oacc_routine_data)
2008 c_finish_oacc_routine (oacc_routine_data, NULL_TREE, false);
2009 c_parser_skip_to_end_of_block_or_statement (parser);
2010 return;
2011 }
2012 if (auto_type_p && declarator->kind != cdk_id)
2013 {
2014 error_at (here,
2015 "%<__auto_type%> requires a plain identifier"
2016 " as declarator");
2017 c_parser_skip_to_end_of_block_or_statement (parser);
2018 return;
2019 }
2020 if (c_parser_next_token_is (parser, CPP_EQ)
2021 || c_parser_next_token_is (parser, CPP_COMMA)
2022 || c_parser_next_token_is (parser, CPP_SEMICOLON)
2023 || c_parser_next_token_is_keyword (parser, RID_ASM)
2024 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE)
2025 || c_parser_next_token_is_keyword (parser, RID_IN))
2026 {
2027 tree asm_name = NULL_TREE;
2028 tree postfix_attrs = NULL_TREE;
2029 if (!diagnosed_no_specs && !specs->declspecs_seen_p)
2030 {
2031 diagnosed_no_specs = true;
2032 pedwarn (here, 0, "data definition has no type or storage class");
2033 }
2034 /* Having seen a data definition, there cannot now be a
2035 function definition. */
2036 fndef_ok = false;
2037 if (c_parser_next_token_is_keyword (parser, RID_ASM))
2038 asm_name = c_parser_simple_asm_expr (parser);
2039 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2040 {
2041 postfix_attrs = c_parser_attributes (parser);
2042 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2043 {
2044 /* This means there is an attribute specifier after
2045 the declarator in a function definition. Provide
2046 some more information for the user. */
2047 error_at (here, "attributes should be specified before the "
2048 "declarator in a function definition");
2049 c_parser_skip_to_end_of_block_or_statement (parser);
2050 return;
2051 }
2052 }
2053 if (c_parser_next_token_is (parser, CPP_EQ))
2054 {
2055 tree d;
2056 struct c_expr init;
2057 location_t init_loc;
2058 c_parser_consume_token (parser);
2059 if (auto_type_p)
2060 {
2061 init_loc = c_parser_peek_token (parser)->location;
2062 rich_location richloc (line_table, init_loc);
2063 start_init (NULL_TREE, asm_name, global_bindings_p (), &richloc);
2064 /* A parameter is initialized, which is invalid. Don't
2065 attempt to instrument the initializer. */
2066 int flag_sanitize_save = flag_sanitize;
2067 if (nested && !empty_ok)
2068 flag_sanitize = 0;
2069 init = c_parser_expr_no_commas (parser, NULL);
2070 flag_sanitize = flag_sanitize_save;
2071 if (TREE_CODE (init.value) == COMPONENT_REF
2072 && DECL_C_BIT_FIELD (TREE_OPERAND (init.value, 1)))
2073 error_at (here,
2074 "%<__auto_type%> used with a bit-field"
2075 " initializer");
2076 init = convert_lvalue_to_rvalue (init_loc, init, true, true);
2077 tree init_type = TREE_TYPE (init.value);
2078 /* As with typeof, remove all qualifiers from atomic types. */
2079 if (init_type != error_mark_node && TYPE_ATOMIC (init_type))
2080 init_type
2081 = c_build_qualified_type (init_type, TYPE_UNQUALIFIED);
2082 bool vm_type = variably_modified_type_p (init_type,
2083 NULL_TREE);
2084 if (vm_type)
2085 init.value = save_expr (init.value);
2086 finish_init ();
2087 specs->typespec_kind = ctsk_typeof;
2088 specs->locations[cdw_typedef] = init_loc;
2089 specs->typedef_p = true;
2090 specs->type = init_type;
2091 if (vm_type)
2092 {
2093 bool maybe_const = true;
2094 tree type_expr = c_fully_fold (init.value, false,
2095 &maybe_const);
2096 specs->expr_const_operands &= maybe_const;
2097 if (specs->expr)
2098 specs->expr = build2 (COMPOUND_EXPR,
2099 TREE_TYPE (type_expr),
2100 specs->expr, type_expr);
2101 else
2102 specs->expr = type_expr;
2103 }
2104 d = start_decl (declarator, specs, true,
2105 chainon (postfix_attrs, all_prefix_attrs));
2106 if (!d)
2107 d = error_mark_node;
2108 if (omp_declare_simd_clauses.exists ())
2109 c_finish_omp_declare_simd (parser, d, NULL_TREE,
2110 omp_declare_simd_clauses);
2111 }
2112 else
2113 {
2114 /* The declaration of the variable is in effect while
2115 its initializer is parsed. */
2116 d = start_decl (declarator, specs, true,
2117 chainon (postfix_attrs, all_prefix_attrs));
2118 if (!d)
2119 d = error_mark_node;
2120 if (omp_declare_simd_clauses.exists ())
2121 c_finish_omp_declare_simd (parser, d, NULL_TREE,
2122 omp_declare_simd_clauses);
2123 init_loc = c_parser_peek_token (parser)->location;
2124 rich_location richloc (line_table, init_loc);
2125 start_init (d, asm_name, global_bindings_p (), &richloc);
2126 /* A parameter is initialized, which is invalid. Don't
2127 attempt to instrument the initializer. */
2128 int flag_sanitize_save = flag_sanitize;
2129 if (TREE_CODE (d) == PARM_DECL)
2130 flag_sanitize = 0;
2131 init = c_parser_initializer (parser);
2132 flag_sanitize = flag_sanitize_save;
2133 finish_init ();
2134 }
2135 if (oacc_routine_data)
2136 c_finish_oacc_routine (oacc_routine_data, d, false);
2137 if (d != error_mark_node)
2138 {
2139 maybe_warn_string_init (init_loc, TREE_TYPE (d), init);
2140 finish_decl (d, init_loc, init.value,
2141 init.original_type, asm_name);
2142 }
2143 }
2144 else
2145 {
2146 if (auto_type_p)
2147 {
2148 error_at (here,
2149 "%<__auto_type%> requires an initialized "
2150 "data declaration");
2151 c_parser_skip_to_end_of_block_or_statement (parser);
2152 return;
2153 }
2154 tree d = start_decl (declarator, specs, false,
2155 chainon (postfix_attrs,
2156 all_prefix_attrs));
2157 if (d
2158 && TREE_CODE (d) == FUNCTION_DECL
2159 && declarator->kind == cdk_function
2160 && DECL_ARGUMENTS (d) == NULL_TREE
2161 && DECL_INITIAL (d) == NULL_TREE)
2162 DECL_ARGUMENTS (d) = declarator->u.arg_info->parms;
2163 if (omp_declare_simd_clauses.exists ())
2164 {
2165 tree parms = NULL_TREE;
2166 if (d && TREE_CODE (d) == FUNCTION_DECL)
2167 {
2168 struct c_declarator *ce = declarator;
2169 while (ce != NULL)
2170 if (ce->kind == cdk_function)
2171 {
2172 parms = ce->u.arg_info->parms;
2173 break;
2174 }
2175 else
2176 ce = ce->declarator;
2177 }
2178 if (parms)
2179 temp_store_parm_decls (d, parms);
2180 c_finish_omp_declare_simd (parser, d, parms,
2181 omp_declare_simd_clauses);
2182 if (parms)
2183 temp_pop_parm_decls ();
2184 }
2185 if (oacc_routine_data)
2186 c_finish_oacc_routine (oacc_routine_data, d, false);
2187 if (d)
2188 finish_decl (d, UNKNOWN_LOCATION, NULL_TREE,
2189 NULL_TREE, asm_name);
2190
2191 if (c_parser_next_token_is_keyword (parser, RID_IN))
2192 {
2193 if (d)
2194 *objc_foreach_object_declaration = d;
2195 else
2196 *objc_foreach_object_declaration = error_mark_node;
2197 }
2198 }
2199 if (c_parser_next_token_is (parser, CPP_COMMA))
2200 {
2201 if (auto_type_p)
2202 {
2203 error_at (here,
2204 "%<__auto_type%> may only be used with"
2205 " a single declarator");
2206 c_parser_skip_to_end_of_block_or_statement (parser);
2207 return;
2208 }
2209 c_parser_consume_token (parser);
2210 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2211 all_prefix_attrs = chainon (c_parser_attributes (parser),
2212 prefix_attrs);
2213 else
2214 all_prefix_attrs = prefix_attrs;
2215 continue;
2216 }
2217 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2218 {
2219 c_parser_consume_token (parser);
2220 return;
2221 }
2222 else if (c_parser_next_token_is_keyword (parser, RID_IN))
2223 {
2224 /* This can only happen in Objective-C: we found the
2225 'in' that terminates the declaration inside an
2226 Objective-C foreach statement. Do not consume the
2227 token, so that the caller can use it to determine
2228 that this indeed is a foreach context. */
2229 return;
2230 }
2231 else
2232 {
2233 c_parser_error (parser, "expected %<,%> or %<;%>");
2234 c_parser_skip_to_end_of_block_or_statement (parser);
2235 return;
2236 }
2237 }
2238 else if (auto_type_p)
2239 {
2240 error_at (here,
2241 "%<__auto_type%> requires an initialized data declaration");
2242 c_parser_skip_to_end_of_block_or_statement (parser);
2243 return;
2244 }
2245 else if (!fndef_ok)
2246 {
2247 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, "
2248 "%<asm%> or %<__attribute__%>");
2249 c_parser_skip_to_end_of_block_or_statement (parser);
2250 return;
2251 }
2252 /* Function definition (nested or otherwise). */
2253 if (nested)
2254 {
2255 pedwarn (here, OPT_Wpedantic, "ISO C forbids nested functions");
2256 c_push_function_context ();
2257 }
2258 if (!start_function (specs, declarator, all_prefix_attrs))
2259 {
2260 /* At this point we've consumed:
2261 declaration-specifiers declarator
2262 and the next token isn't CPP_EQ, CPP_COMMA, CPP_SEMICOLON,
2263 RID_ASM, RID_ATTRIBUTE, or RID_IN,
2264 but the
2265 declaration-specifiers declarator
2266 aren't grokkable as a function definition, so we have
2267 an error. */
2268 gcc_assert (!c_parser_next_token_is (parser, CPP_SEMICOLON));
2269 if (c_parser_next_token_starts_declspecs (parser))
2270 {
2271 /* If we have
2272 declaration-specifiers declarator decl-specs
2273 then assume we have a missing semicolon, which would
2274 give us:
2275 declaration-specifiers declarator decl-specs
2276 ^
2277 ;
2278 <~~~~~~~~~ declaration ~~~~~~~~~~>
2279 Use c_parser_require to get an error with a fix-it hint. */
2280 c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>");
2281 parser->error = false;
2282 }
2283 else
2284 {
2285 /* This can appear in many cases looking nothing like a
2286 function definition, so we don't give a more specific
2287 error suggesting there was one. */
2288 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, %<asm%> "
2289 "or %<__attribute__%>");
2290 }
2291 if (nested)
2292 c_pop_function_context ();
2293 break;
2294 }
2295
2296 if (DECL_DECLARED_INLINE_P (current_function_decl))
2297 tv = TV_PARSE_INLINE;
2298 else
2299 tv = TV_PARSE_FUNC;
2300 auto_timevar at (g_timer, tv);
2301
2302 /* Parse old-style parameter declarations. ??? Attributes are
2303 not allowed to start declaration specifiers here because of a
2304 syntax conflict between a function declaration with attribute
2305 suffix and a function definition with an attribute prefix on
2306 first old-style parameter declaration. Following the old
2307 parser, they are not accepted on subsequent old-style
2308 parameter declarations either. However, there is no
2309 ambiguity after the first declaration, nor indeed on the
2310 first as long as we don't allow postfix attributes after a
2311 declarator with a nonempty identifier list in a definition;
2312 and postfix attributes have never been accepted here in
2313 function definitions either. */
2314 while (c_parser_next_token_is_not (parser, CPP_EOF)
2315 && c_parser_next_token_is_not (parser, CPP_OPEN_BRACE))
2316 c_parser_declaration_or_fndef (parser, false, false, false,
2317 true, false, NULL, vNULL);
2318 store_parm_decls ();
2319 if (omp_declare_simd_clauses.exists ())
2320 c_finish_omp_declare_simd (parser, current_function_decl, NULL_TREE,
2321 omp_declare_simd_clauses);
2322 if (oacc_routine_data)
2323 c_finish_oacc_routine (oacc_routine_data, current_function_decl, true);
2324 DECL_STRUCT_FUNCTION (current_function_decl)->function_start_locus
2325 = c_parser_peek_token (parser)->location;
2326
2327 /* If the definition was marked with __RTL, use the RTL parser now,
2328 consuming the function body. */
2329 if (specs->declspec_il == cdil_rtl)
2330 {
2331 c_parser_parse_rtl_body (parser, specs->gimple_or_rtl_pass);
2332
2333 /* Normally, store_parm_decls sets next_is_function_body,
2334 anticipating a function body. We need a push_scope/pop_scope
2335 pair to flush out this state, or subsequent function parsing
2336 will go wrong. */
2337 push_scope ();
2338 pop_scope ();
2339
2340 finish_function ();
2341 return;
2342 }
2343 /* If the definition was marked with __GIMPLE then parse the
2344 function body as GIMPLE. */
2345 else if (specs->declspec_il != cdil_none)
2346 {
2347 bool saved = in_late_binary_op;
2348 in_late_binary_op = true;
2349 c_parser_parse_gimple_body (parser, specs->gimple_or_rtl_pass,
2350 specs->declspec_il,
2351 specs->entry_bb_count);
2352 in_late_binary_op = saved;
2353 }
2354 else
2355 fnbody = c_parser_compound_statement (parser);
2356 tree fndecl = current_function_decl;
2357 if (nested)
2358 {
2359 tree decl = current_function_decl;
2360 /* Mark nested functions as needing static-chain initially.
2361 lower_nested_functions will recompute it but the
2362 DECL_STATIC_CHAIN flag is also used before that happens,
2363 by initializer_constant_valid_p. See gcc.dg/nested-fn-2.c. */
2364 DECL_STATIC_CHAIN (decl) = 1;
2365 add_stmt (fnbody);
2366 finish_function ();
2367 c_pop_function_context ();
2368 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
2369 }
2370 else
2371 {
2372 if (fnbody)
2373 add_stmt (fnbody);
2374 finish_function ();
2375 }
2376 /* Get rid of the empty stmt list for GIMPLE/RTL. */
2377 if (specs->declspec_il != cdil_none)
2378 DECL_SAVED_TREE (fndecl) = NULL_TREE;
2379
2380 break;
2381 }
2382 }
2383
2384 /* Parse an asm-definition (asm() outside a function body). This is a
2385 GNU extension.
2386
2387 asm-definition:
2388 simple-asm-expr ;
2389 */
2390
2391 static void
2392 c_parser_asm_definition (c_parser *parser)
2393 {
2394 tree asm_str = c_parser_simple_asm_expr (parser);
2395 if (asm_str)
2396 symtab->finalize_toplevel_asm (asm_str);
2397 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
2398 }
2399
2400 /* Parse a static assertion (C11 6.7.10).
2401
2402 static_assert-declaration:
2403 static_assert-declaration-no-semi ;
2404 */
2405
2406 static void
2407 c_parser_static_assert_declaration (c_parser *parser)
2408 {
2409 c_parser_static_assert_declaration_no_semi (parser);
2410 if (parser->error
2411 || !c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
2412 c_parser_skip_to_end_of_block_or_statement (parser);
2413 }
2414
2415 /* Parse a static assertion (C11 6.7.10), without the trailing
2416 semicolon.
2417
2418 static_assert-declaration-no-semi:
2419 _Static_assert ( constant-expression , string-literal )
2420
2421 C2X:
2422 static_assert-declaration-no-semi:
2423 _Static_assert ( constant-expression )
2424 */
2425
2426 static void
2427 c_parser_static_assert_declaration_no_semi (c_parser *parser)
2428 {
2429 location_t assert_loc, value_loc;
2430 tree value;
2431 tree string = NULL_TREE;
2432
2433 gcc_assert (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT));
2434 assert_loc = c_parser_peek_token (parser)->location;
2435 if (flag_isoc99)
2436 pedwarn_c99 (assert_loc, OPT_Wpedantic,
2437 "ISO C99 does not support %<_Static_assert%>");
2438 else
2439 pedwarn_c99 (assert_loc, OPT_Wpedantic,
2440 "ISO C90 does not support %<_Static_assert%>");
2441 c_parser_consume_token (parser);
2442 matching_parens parens;
2443 if (!parens.require_open (parser))
2444 return;
2445 location_t value_tok_loc = c_parser_peek_token (parser)->location;
2446 value = c_parser_expr_no_commas (parser, NULL).value;
2447 value_loc = EXPR_LOC_OR_LOC (value, value_tok_loc);
2448 parser->lex_untranslated_string = true;
2449 if (c_parser_next_token_is (parser, CPP_COMMA))
2450 {
2451 c_parser_consume_token (parser);
2452 switch (c_parser_peek_token (parser)->type)
2453 {
2454 case CPP_STRING:
2455 case CPP_STRING16:
2456 case CPP_STRING32:
2457 case CPP_WSTRING:
2458 case CPP_UTF8STRING:
2459 string = c_parser_peek_token (parser)->value;
2460 c_parser_consume_token (parser);
2461 parser->lex_untranslated_string = false;
2462 break;
2463 default:
2464 c_parser_error (parser, "expected string literal");
2465 parser->lex_untranslated_string = false;
2466 return;
2467 }
2468 }
2469 else if (flag_isoc11)
2470 /* If pedantic for pre-C11, the use of _Static_assert itself will
2471 have been diagnosed, so do not also diagnose the use of this
2472 new C2X feature of _Static_assert. */
2473 pedwarn_c11 (assert_loc, OPT_Wpedantic,
2474 "ISO C11 does not support omitting the string in "
2475 "%<_Static_assert%>");
2476 parens.require_close (parser);
2477
2478 if (!INTEGRAL_TYPE_P (TREE_TYPE (value)))
2479 {
2480 error_at (value_loc, "expression in static assertion is not an integer");
2481 return;
2482 }
2483 if (TREE_CODE (value) != INTEGER_CST)
2484 {
2485 value = c_fully_fold (value, false, NULL);
2486 /* Strip no-op conversions. */
2487 STRIP_TYPE_NOPS (value);
2488 if (TREE_CODE (value) == INTEGER_CST)
2489 pedwarn (value_loc, OPT_Wpedantic, "expression in static assertion "
2490 "is not an integer constant expression");
2491 }
2492 if (TREE_CODE (value) != INTEGER_CST)
2493 {
2494 error_at (value_loc, "expression in static assertion is not constant");
2495 return;
2496 }
2497 constant_expression_warning (value);
2498 if (integer_zerop (value))
2499 {
2500 if (string)
2501 error_at (assert_loc, "static assertion failed: %E", string);
2502 else
2503 error_at (assert_loc, "static assertion failed");
2504 }
2505 }
2506
2507 /* Parse some declaration specifiers (possibly none) (C90 6.5, C99
2508 6.7, C11 6.7), adding them to SPECS (which may already include some).
2509 Storage class specifiers are accepted iff SCSPEC_OK; type
2510 specifiers are accepted iff TYPESPEC_OK; alignment specifiers are
2511 accepted iff ALIGNSPEC_OK; attributes are accepted at the start
2512 iff START_ATTR_OK; __auto_type is accepted iff AUTO_TYPE_OK.
2513
2514 declaration-specifiers:
2515 storage-class-specifier declaration-specifiers[opt]
2516 type-specifier declaration-specifiers[opt]
2517 type-qualifier declaration-specifiers[opt]
2518 function-specifier declaration-specifiers[opt]
2519 alignment-specifier declaration-specifiers[opt]
2520
2521 Function specifiers (inline) are from C99, and are currently
2522 handled as storage class specifiers, as is __thread. Alignment
2523 specifiers are from C11.
2524
2525 C90 6.5.1, C99 6.7.1, C11 6.7.1:
2526 storage-class-specifier:
2527 typedef
2528 extern
2529 static
2530 auto
2531 register
2532 _Thread_local
2533
2534 (_Thread_local is new in C11.)
2535
2536 C99 6.7.4, C11 6.7.4:
2537 function-specifier:
2538 inline
2539 _Noreturn
2540
2541 (_Noreturn is new in C11.)
2542
2543 C90 6.5.2, C99 6.7.2, C11 6.7.2:
2544 type-specifier:
2545 void
2546 char
2547 short
2548 int
2549 long
2550 float
2551 double
2552 signed
2553 unsigned
2554 _Bool
2555 _Complex
2556 [_Imaginary removed in C99 TC2]
2557 struct-or-union-specifier
2558 enum-specifier
2559 typedef-name
2560 atomic-type-specifier
2561
2562 (_Bool and _Complex are new in C99.)
2563 (atomic-type-specifier is new in C11.)
2564
2565 C90 6.5.3, C99 6.7.3, C11 6.7.3:
2566
2567 type-qualifier:
2568 const
2569 restrict
2570 volatile
2571 address-space-qualifier
2572 _Atomic
2573
2574 (restrict is new in C99.)
2575 (_Atomic is new in C11.)
2576
2577 GNU extensions:
2578
2579 declaration-specifiers:
2580 attributes declaration-specifiers[opt]
2581
2582 type-qualifier:
2583 address-space
2584
2585 address-space:
2586 identifier recognized by the target
2587
2588 storage-class-specifier:
2589 __thread
2590
2591 type-specifier:
2592 typeof-specifier
2593 __auto_type
2594 __intN
2595 _Decimal32
2596 _Decimal64
2597 _Decimal128
2598 _Fract
2599 _Accum
2600 _Sat
2601
2602 (_Fract, _Accum, and _Sat are new from ISO/IEC DTR 18037:
2603 http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1169.pdf)
2604
2605 atomic-type-specifier
2606 _Atomic ( type-name )
2607
2608 Objective-C:
2609
2610 type-specifier:
2611 class-name objc-protocol-refs[opt]
2612 typedef-name objc-protocol-refs
2613 objc-protocol-refs
2614 */
2615
2616 void
2617 c_parser_declspecs (c_parser *parser, struct c_declspecs *specs,
2618 bool scspec_ok, bool typespec_ok, bool start_attr_ok,
2619 bool alignspec_ok, bool auto_type_ok,
2620 enum c_lookahead_kind la)
2621 {
2622 bool attrs_ok = start_attr_ok;
2623 bool seen_type = specs->typespec_kind != ctsk_none;
2624
2625 if (!typespec_ok)
2626 gcc_assert (la == cla_prefer_id);
2627
2628 while (c_parser_next_token_is (parser, CPP_NAME)
2629 || c_parser_next_token_is (parser, CPP_KEYWORD)
2630 || (c_dialect_objc () && c_parser_next_token_is (parser, CPP_LESS)))
2631 {
2632 struct c_typespec t;
2633 tree attrs;
2634 tree align;
2635 location_t loc = c_parser_peek_token (parser)->location;
2636
2637 /* If we cannot accept a type, exit if the next token must start
2638 one. Also, if we already have seen a tagged definition,
2639 a typename would be an error anyway and likely the user
2640 has simply forgotten a semicolon, so we exit. */
2641 if ((!typespec_ok || specs->typespec_kind == ctsk_tagdef)
2642 && c_parser_next_tokens_start_typename (parser, la)
2643 && !c_parser_next_token_is_qualifier (parser)
2644 && !c_parser_next_token_is_keyword (parser, RID_ALIGNAS))
2645 break;
2646
2647 if (c_parser_next_token_is (parser, CPP_NAME))
2648 {
2649 c_token *name_token = c_parser_peek_token (parser);
2650 tree value = name_token->value;
2651 c_id_kind kind = name_token->id_kind;
2652
2653 if (kind == C_ID_ADDRSPACE)
2654 {
2655 addr_space_t as
2656 = name_token->keyword - RID_FIRST_ADDR_SPACE;
2657 declspecs_add_addrspace (name_token->location, specs, as);
2658 c_parser_consume_token (parser);
2659 attrs_ok = true;
2660 continue;
2661 }
2662
2663 gcc_assert (!c_parser_next_token_is_qualifier (parser));
2664
2665 /* If we cannot accept a type, and the next token must start one,
2666 exit. Do the same if we already have seen a tagged definition,
2667 since it would be an error anyway and likely the user has simply
2668 forgotten a semicolon. */
2669 if (seen_type || !c_parser_next_tokens_start_typename (parser, la))
2670 break;
2671
2672 /* Now at an unknown typename (C_ID_ID), a C_ID_TYPENAME or
2673 a C_ID_CLASSNAME. */
2674 c_parser_consume_token (parser);
2675 seen_type = true;
2676 attrs_ok = true;
2677 if (kind == C_ID_ID)
2678 {
2679 error_at (loc, "unknown type name %qE", value);
2680 t.kind = ctsk_typedef;
2681 t.spec = error_mark_node;
2682 }
2683 else if (kind == C_ID_TYPENAME
2684 && (!c_dialect_objc ()
2685 || c_parser_next_token_is_not (parser, CPP_LESS)))
2686 {
2687 t.kind = ctsk_typedef;
2688 /* For a typedef name, record the meaning, not the name.
2689 In case of 'foo foo, bar;'. */
2690 t.spec = lookup_name (value);
2691 }
2692 else
2693 {
2694 tree proto = NULL_TREE;
2695 gcc_assert (c_dialect_objc ());
2696 t.kind = ctsk_objc;
2697 if (c_parser_next_token_is (parser, CPP_LESS))
2698 proto = c_parser_objc_protocol_refs (parser);
2699 t.spec = objc_get_protocol_qualified_type (value, proto);
2700 }
2701 t.expr = NULL_TREE;
2702 t.expr_const_operands = true;
2703 declspecs_add_type (name_token->location, specs, t);
2704 continue;
2705 }
2706 if (c_parser_next_token_is (parser, CPP_LESS))
2707 {
2708 /* Make "<SomeProtocol>" equivalent to "id <SomeProtocol>" -
2709 nisse@lysator.liu.se. */
2710 tree proto;
2711 gcc_assert (c_dialect_objc ());
2712 if (!typespec_ok || seen_type)
2713 break;
2714 proto = c_parser_objc_protocol_refs (parser);
2715 t.kind = ctsk_objc;
2716 t.spec = objc_get_protocol_qualified_type (NULL_TREE, proto);
2717 t.expr = NULL_TREE;
2718 t.expr_const_operands = true;
2719 declspecs_add_type (loc, specs, t);
2720 continue;
2721 }
2722 gcc_assert (c_parser_next_token_is (parser, CPP_KEYWORD));
2723 switch (c_parser_peek_token (parser)->keyword)
2724 {
2725 case RID_STATIC:
2726 case RID_EXTERN:
2727 case RID_REGISTER:
2728 case RID_TYPEDEF:
2729 case RID_INLINE:
2730 case RID_NORETURN:
2731 case RID_AUTO:
2732 case RID_THREAD:
2733 if (!scspec_ok)
2734 goto out;
2735 attrs_ok = true;
2736 /* TODO: Distinguish between function specifiers (inline, noreturn)
2737 and storage class specifiers, either here or in
2738 declspecs_add_scspec. */
2739 declspecs_add_scspec (loc, specs,
2740 c_parser_peek_token (parser)->value);
2741 c_parser_consume_token (parser);
2742 break;
2743 case RID_AUTO_TYPE:
2744 if (!auto_type_ok)
2745 goto out;
2746 /* Fall through. */
2747 case RID_UNSIGNED:
2748 case RID_LONG:
2749 case RID_SHORT:
2750 case RID_SIGNED:
2751 case RID_COMPLEX:
2752 case RID_INT:
2753 case RID_CHAR:
2754 case RID_FLOAT:
2755 case RID_DOUBLE:
2756 case RID_VOID:
2757 case RID_DFLOAT32:
2758 case RID_DFLOAT64:
2759 case RID_DFLOAT128:
2760 CASE_RID_FLOATN_NX:
2761 case RID_BOOL:
2762 case RID_FRACT:
2763 case RID_ACCUM:
2764 case RID_SAT:
2765 case RID_INT_N_0:
2766 case RID_INT_N_1:
2767 case RID_INT_N_2:
2768 case RID_INT_N_3:
2769 if (!typespec_ok)
2770 goto out;
2771 attrs_ok = true;
2772 seen_type = true;
2773 if (c_dialect_objc ())
2774 parser->objc_need_raw_identifier = true;
2775 t.kind = ctsk_resword;
2776 t.spec = c_parser_peek_token (parser)->value;
2777 t.expr = NULL_TREE;
2778 t.expr_const_operands = true;
2779 declspecs_add_type (loc, specs, t);
2780 c_parser_consume_token (parser);
2781 break;
2782 case RID_ENUM:
2783 if (!typespec_ok)
2784 goto out;
2785 attrs_ok = true;
2786 seen_type = true;
2787 t = c_parser_enum_specifier (parser);
2788 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, t.spec);
2789 declspecs_add_type (loc, specs, t);
2790 break;
2791 case RID_STRUCT:
2792 case RID_UNION:
2793 if (!typespec_ok)
2794 goto out;
2795 attrs_ok = true;
2796 seen_type = true;
2797 t = c_parser_struct_or_union_specifier (parser);
2798 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, t.spec);
2799 declspecs_add_type (loc, specs, t);
2800 break;
2801 case RID_TYPEOF:
2802 /* ??? The old parser rejected typeof after other type
2803 specifiers, but is a syntax error the best way of
2804 handling this? */
2805 if (!typespec_ok || seen_type)
2806 goto out;
2807 attrs_ok = true;
2808 seen_type = true;
2809 t = c_parser_typeof_specifier (parser);
2810 declspecs_add_type (loc, specs, t);
2811 break;
2812 case RID_ATOMIC:
2813 /* C parser handling of Objective-C constructs needs
2814 checking for correct lvalue-to-rvalue conversions, and
2815 the code in build_modify_expr handling various
2816 Objective-C cases, and that in build_unary_op handling
2817 Objective-C cases for increment / decrement, also needs
2818 updating; uses of TYPE_MAIN_VARIANT in objc_compare_types
2819 and objc_types_are_equivalent may also need updates. */
2820 if (c_dialect_objc ())
2821 sorry ("%<_Atomic%> in Objective-C");
2822 if (flag_isoc99)
2823 pedwarn_c99 (loc, OPT_Wpedantic,
2824 "ISO C99 does not support the %<_Atomic%> qualifier");
2825 else
2826 pedwarn_c99 (loc, OPT_Wpedantic,
2827 "ISO C90 does not support the %<_Atomic%> qualifier");
2828 attrs_ok = true;
2829 tree value;
2830 value = c_parser_peek_token (parser)->value;
2831 c_parser_consume_token (parser);
2832 if (typespec_ok && c_parser_next_token_is (parser, CPP_OPEN_PAREN))
2833 {
2834 /* _Atomic ( type-name ). */
2835 seen_type = true;
2836 c_parser_consume_token (parser);
2837 struct c_type_name *type = c_parser_type_name (parser);
2838 t.kind = ctsk_typeof;
2839 t.spec = error_mark_node;
2840 t.expr = NULL_TREE;
2841 t.expr_const_operands = true;
2842 if (type != NULL)
2843 t.spec = groktypename (type, &t.expr,
2844 &t.expr_const_operands);
2845 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2846 "expected %<)%>");
2847 if (t.spec != error_mark_node)
2848 {
2849 if (TREE_CODE (t.spec) == ARRAY_TYPE)
2850 error_at (loc, "%<_Atomic%>-qualified array type");
2851 else if (TREE_CODE (t.spec) == FUNCTION_TYPE)
2852 error_at (loc, "%<_Atomic%>-qualified function type");
2853 else if (TYPE_QUALS (t.spec) != TYPE_UNQUALIFIED)
2854 error_at (loc, "%<_Atomic%> applied to a qualified type");
2855 else
2856 t.spec = c_build_qualified_type (t.spec, TYPE_QUAL_ATOMIC);
2857 }
2858 declspecs_add_type (loc, specs, t);
2859 }
2860 else
2861 declspecs_add_qual (loc, specs, value);
2862 break;
2863 case RID_CONST:
2864 case RID_VOLATILE:
2865 case RID_RESTRICT:
2866 attrs_ok = true;
2867 declspecs_add_qual (loc, specs, c_parser_peek_token (parser)->value);
2868 c_parser_consume_token (parser);
2869 break;
2870 case RID_ATTRIBUTE:
2871 if (!attrs_ok)
2872 goto out;
2873 attrs = c_parser_attributes (parser);
2874 declspecs_add_attrs (loc, specs, attrs);
2875 break;
2876 case RID_ALIGNAS:
2877 if (!alignspec_ok)
2878 goto out;
2879 align = c_parser_alignas_specifier (parser);
2880 declspecs_add_alignas (loc, specs, align);
2881 break;
2882 case RID_GIMPLE:
2883 if (! flag_gimple)
2884 error_at (loc, "%<__GIMPLE%> only valid with %<-fgimple%>");
2885 c_parser_consume_token (parser);
2886 specs->declspec_il = cdil_gimple;
2887 specs->locations[cdw_gimple] = loc;
2888 c_parser_gimple_or_rtl_pass_list (parser, specs);
2889 break;
2890 case RID_RTL:
2891 c_parser_consume_token (parser);
2892 specs->declspec_il = cdil_rtl;
2893 specs->locations[cdw_rtl] = loc;
2894 c_parser_gimple_or_rtl_pass_list (parser, specs);
2895 break;
2896 default:
2897 goto out;
2898 }
2899 }
2900 out: ;
2901 }
2902
2903 /* Parse an enum specifier (C90 6.5.2.2, C99 6.7.2.2, C11 6.7.2.2).
2904
2905 enum-specifier:
2906 enum attributes[opt] identifier[opt] { enumerator-list } attributes[opt]
2907 enum attributes[opt] identifier[opt] { enumerator-list , } attributes[opt]
2908 enum attributes[opt] identifier
2909
2910 The form with trailing comma is new in C99. The forms with
2911 attributes are GNU extensions. In GNU C, we accept any expression
2912 without commas in the syntax (assignment expressions, not just
2913 conditional expressions); assignment expressions will be diagnosed
2914 as non-constant.
2915
2916 enumerator-list:
2917 enumerator
2918 enumerator-list , enumerator
2919
2920 enumerator:
2921 enumeration-constant
2922 enumeration-constant = constant-expression
2923
2924 GNU Extensions:
2925
2926 enumerator:
2927 enumeration-constant attributes[opt]
2928 enumeration-constant attributes[opt] = constant-expression
2929
2930 */
2931
2932 static struct c_typespec
2933 c_parser_enum_specifier (c_parser *parser)
2934 {
2935 struct c_typespec ret;
2936 tree attrs;
2937 tree ident = NULL_TREE;
2938 location_t enum_loc;
2939 location_t ident_loc = UNKNOWN_LOCATION; /* Quiet warning. */
2940 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ENUM));
2941 c_parser_consume_token (parser);
2942 attrs = c_parser_attributes (parser);
2943 enum_loc = c_parser_peek_token (parser)->location;
2944 /* Set the location in case we create a decl now. */
2945 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
2946 if (c_parser_next_token_is (parser, CPP_NAME))
2947 {
2948 ident = c_parser_peek_token (parser)->value;
2949 ident_loc = c_parser_peek_token (parser)->location;
2950 enum_loc = ident_loc;
2951 c_parser_consume_token (parser);
2952 }
2953 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2954 {
2955 /* Parse an enum definition. */
2956 struct c_enum_contents the_enum;
2957 tree type;
2958 tree postfix_attrs;
2959 /* We chain the enumerators in reverse order, then put them in
2960 forward order at the end. */
2961 tree values;
2962 timevar_push (TV_PARSE_ENUM);
2963 type = start_enum (enum_loc, &the_enum, ident);
2964 values = NULL_TREE;
2965 c_parser_consume_token (parser);
2966 while (true)
2967 {
2968 tree enum_id;
2969 tree enum_value;
2970 tree enum_decl;
2971 bool seen_comma;
2972 c_token *token;
2973 location_t comma_loc = UNKNOWN_LOCATION; /* Quiet warning. */
2974 location_t decl_loc, value_loc;
2975 if (c_parser_next_token_is_not (parser, CPP_NAME))
2976 {
2977 /* Give a nicer error for "enum {}". */
2978 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE)
2979 && !parser->error)
2980 {
2981 error_at (c_parser_peek_token (parser)->location,
2982 "empty enum is invalid");
2983 parser->error = true;
2984 }
2985 else
2986 c_parser_error (parser, "expected identifier");
2987 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2988 values = error_mark_node;
2989 break;
2990 }
2991 token = c_parser_peek_token (parser);
2992 enum_id = token->value;
2993 /* Set the location in case we create a decl now. */
2994 c_parser_set_source_position_from_token (token);
2995 decl_loc = value_loc = token->location;
2996 c_parser_consume_token (parser);
2997 /* Parse any specified attributes. */
2998 tree enum_attrs = c_parser_attributes (parser);
2999 if (c_parser_next_token_is (parser, CPP_EQ))
3000 {
3001 c_parser_consume_token (parser);
3002 value_loc = c_parser_peek_token (parser)->location;
3003 enum_value = c_parser_expr_no_commas (parser, NULL).value;
3004 }
3005 else
3006 enum_value = NULL_TREE;
3007 enum_decl = build_enumerator (decl_loc, value_loc,
3008 &the_enum, enum_id, enum_value);
3009 if (enum_attrs)
3010 decl_attributes (&TREE_PURPOSE (enum_decl), enum_attrs, 0);
3011 TREE_CHAIN (enum_decl) = values;
3012 values = enum_decl;
3013 seen_comma = false;
3014 if (c_parser_next_token_is (parser, CPP_COMMA))
3015 {
3016 comma_loc = c_parser_peek_token (parser)->location;
3017 seen_comma = true;
3018 c_parser_consume_token (parser);
3019 }
3020 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3021 {
3022 if (seen_comma)
3023 pedwarn_c90 (comma_loc, OPT_Wpedantic,
3024 "comma at end of enumerator list");
3025 c_parser_consume_token (parser);
3026 break;
3027 }
3028 if (!seen_comma)
3029 {
3030 c_parser_error (parser, "expected %<,%> or %<}%>");
3031 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
3032 values = error_mark_node;
3033 break;
3034 }
3035 }
3036 postfix_attrs = c_parser_attributes (parser);
3037 ret.spec = finish_enum (type, nreverse (values),
3038 chainon (attrs, postfix_attrs));
3039 ret.kind = ctsk_tagdef;
3040 ret.expr = NULL_TREE;
3041 ret.expr_const_operands = true;
3042 timevar_pop (TV_PARSE_ENUM);
3043 return ret;
3044 }
3045 else if (!ident)
3046 {
3047 c_parser_error (parser, "expected %<{%>");
3048 ret.spec = error_mark_node;
3049 ret.kind = ctsk_tagref;
3050 ret.expr = NULL_TREE;
3051 ret.expr_const_operands = true;
3052 return ret;
3053 }
3054 ret = parser_xref_tag (ident_loc, ENUMERAL_TYPE, ident);
3055 /* In ISO C, enumerated types can be referred to only if already
3056 defined. */
3057 if (pedantic && !COMPLETE_TYPE_P (ret.spec))
3058 {
3059 gcc_assert (ident);
3060 pedwarn (enum_loc, OPT_Wpedantic,
3061 "ISO C forbids forward references to %<enum%> types");
3062 }
3063 return ret;
3064 }
3065
3066 /* Parse a struct or union specifier (C90 6.5.2.1, C99 6.7.2.1, C11 6.7.2.1).
3067
3068 struct-or-union-specifier:
3069 struct-or-union attributes[opt] identifier[opt]
3070 { struct-contents } attributes[opt]
3071 struct-or-union attributes[opt] identifier
3072
3073 struct-contents:
3074 struct-declaration-list
3075
3076 struct-declaration-list:
3077 struct-declaration ;
3078 struct-declaration-list struct-declaration ;
3079
3080 GNU extensions:
3081
3082 struct-contents:
3083 empty
3084 struct-declaration
3085 struct-declaration-list struct-declaration
3086
3087 struct-declaration-list:
3088 struct-declaration-list ;
3089 ;
3090
3091 (Note that in the syntax here, unlike that in ISO C, the semicolons
3092 are included here rather than in struct-declaration, in order to
3093 describe the syntax with extra semicolons and missing semicolon at
3094 end.)
3095
3096 Objective-C:
3097
3098 struct-declaration-list:
3099 @defs ( class-name )
3100
3101 (Note this does not include a trailing semicolon, but can be
3102 followed by further declarations, and gets a pedwarn-if-pedantic
3103 when followed by a semicolon.) */
3104
3105 static struct c_typespec
3106 c_parser_struct_or_union_specifier (c_parser *parser)
3107 {
3108 struct c_typespec ret;
3109 tree attrs;
3110 tree ident = NULL_TREE;
3111 location_t struct_loc;
3112 location_t ident_loc = UNKNOWN_LOCATION;
3113 enum tree_code code;
3114 switch (c_parser_peek_token (parser)->keyword)
3115 {
3116 case RID_STRUCT:
3117 code = RECORD_TYPE;
3118 break;
3119 case RID_UNION:
3120 code = UNION_TYPE;
3121 break;
3122 default:
3123 gcc_unreachable ();
3124 }
3125 struct_loc = c_parser_peek_token (parser)->location;
3126 c_parser_consume_token (parser);
3127 attrs = c_parser_attributes (parser);
3128
3129 /* Set the location in case we create a decl now. */
3130 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
3131
3132 if (c_parser_next_token_is (parser, CPP_NAME))
3133 {
3134 ident = c_parser_peek_token (parser)->value;
3135 ident_loc = c_parser_peek_token (parser)->location;
3136 struct_loc = ident_loc;
3137 c_parser_consume_token (parser);
3138 }
3139 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
3140 {
3141 /* Parse a struct or union definition. Start the scope of the
3142 tag before parsing components. */
3143 struct c_struct_parse_info *struct_info;
3144 tree type = start_struct (struct_loc, code, ident, &struct_info);
3145 tree postfix_attrs;
3146 /* We chain the components in reverse order, then put them in
3147 forward order at the end. Each struct-declaration may
3148 declare multiple components (comma-separated), so we must use
3149 chainon to join them, although when parsing each
3150 struct-declaration we can use TREE_CHAIN directly.
3151
3152 The theory behind all this is that there will be more
3153 semicolon separated fields than comma separated fields, and
3154 so we'll be minimizing the number of node traversals required
3155 by chainon. */
3156 tree contents;
3157 timevar_push (TV_PARSE_STRUCT);
3158 contents = NULL_TREE;
3159 c_parser_consume_token (parser);
3160 /* Handle the Objective-C @defs construct,
3161 e.g. foo(sizeof(struct{ @defs(ClassName) }));. */
3162 if (c_parser_next_token_is_keyword (parser, RID_AT_DEFS))
3163 {
3164 tree name;
3165 gcc_assert (c_dialect_objc ());
3166 c_parser_consume_token (parser);
3167 matching_parens parens;
3168 if (!parens.require_open (parser))
3169 goto end_at_defs;
3170 if (c_parser_next_token_is (parser, CPP_NAME)
3171 && c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME)
3172 {
3173 name = c_parser_peek_token (parser)->value;
3174 c_parser_consume_token (parser);
3175 }
3176 else
3177 {
3178 c_parser_error (parser, "expected class name");
3179 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3180 goto end_at_defs;
3181 }
3182 parens.skip_until_found_close (parser);
3183 contents = nreverse (objc_get_class_ivars (name));
3184 }
3185 end_at_defs:
3186 /* Parse the struct-declarations and semicolons. Problems with
3187 semicolons are diagnosed here; empty structures are diagnosed
3188 elsewhere. */
3189 while (true)
3190 {
3191 tree decls;
3192 /* Parse any stray semicolon. */
3193 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3194 {
3195 location_t semicolon_loc
3196 = c_parser_peek_token (parser)->location;
3197 gcc_rich_location richloc (semicolon_loc);
3198 richloc.add_fixit_remove ();
3199 pedwarn (&richloc, OPT_Wpedantic,
3200 "extra semicolon in struct or union specified");
3201 c_parser_consume_token (parser);
3202 continue;
3203 }
3204 /* Stop if at the end of the struct or union contents. */
3205 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3206 {
3207 c_parser_consume_token (parser);
3208 break;
3209 }
3210 /* Accept #pragmas at struct scope. */
3211 if (c_parser_next_token_is (parser, CPP_PRAGMA))
3212 {
3213 c_parser_pragma (parser, pragma_struct, NULL);
3214 continue;
3215 }
3216 /* Parse some comma-separated declarations, but not the
3217 trailing semicolon if any. */
3218 decls = c_parser_struct_declaration (parser);
3219 contents = chainon (decls, contents);
3220 /* If no semicolon follows, either we have a parse error or
3221 are at the end of the struct or union and should
3222 pedwarn. */
3223 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3224 c_parser_consume_token (parser);
3225 else
3226 {
3227 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3228 pedwarn (c_parser_peek_token (parser)->location, 0,
3229 "no semicolon at end of struct or union");
3230 else if (parser->error
3231 || !c_parser_next_token_starts_declspecs (parser))
3232 {
3233 c_parser_error (parser, "expected %<;%>");
3234 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
3235 break;
3236 }
3237
3238 /* If we come here, we have already emitted an error
3239 for an expected `;', identifier or `(', and we also
3240 recovered already. Go on with the next field. */
3241 }
3242 }
3243 postfix_attrs = c_parser_attributes (parser);
3244 ret.spec = finish_struct (struct_loc, type, nreverse (contents),
3245 chainon (attrs, postfix_attrs), struct_info);
3246 ret.kind = ctsk_tagdef;
3247 ret.expr = NULL_TREE;
3248 ret.expr_const_operands = true;
3249 timevar_pop (TV_PARSE_STRUCT);
3250 return ret;
3251 }
3252 else if (!ident)
3253 {
3254 c_parser_error (parser, "expected %<{%>");
3255 ret.spec = error_mark_node;
3256 ret.kind = ctsk_tagref;
3257 ret.expr = NULL_TREE;
3258 ret.expr_const_operands = true;
3259 return ret;
3260 }
3261 ret = parser_xref_tag (ident_loc, code, ident);
3262 return ret;
3263 }
3264
3265 /* Parse a struct-declaration (C90 6.5.2.1, C99 6.7.2.1, C11 6.7.2.1),
3266 *without* the trailing semicolon.
3267
3268 struct-declaration:
3269 specifier-qualifier-list struct-declarator-list
3270 static_assert-declaration-no-semi
3271
3272 specifier-qualifier-list:
3273 type-specifier specifier-qualifier-list[opt]
3274 type-qualifier specifier-qualifier-list[opt]
3275 alignment-specifier specifier-qualifier-list[opt]
3276 attributes specifier-qualifier-list[opt]
3277
3278 struct-declarator-list:
3279 struct-declarator
3280 struct-declarator-list , attributes[opt] struct-declarator
3281
3282 struct-declarator:
3283 declarator attributes[opt]
3284 declarator[opt] : constant-expression attributes[opt]
3285
3286 GNU extensions:
3287
3288 struct-declaration:
3289 __extension__ struct-declaration
3290 specifier-qualifier-list
3291
3292 Unlike the ISO C syntax, semicolons are handled elsewhere. The use
3293 of attributes where shown is a GNU extension. In GNU C, we accept
3294 any expression without commas in the syntax (assignment
3295 expressions, not just conditional expressions); assignment
3296 expressions will be diagnosed as non-constant. */
3297
3298 static tree
3299 c_parser_struct_declaration (c_parser *parser)
3300 {
3301 struct c_declspecs *specs;
3302 tree prefix_attrs;
3303 tree all_prefix_attrs;
3304 tree decls;
3305 location_t decl_loc;
3306 if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
3307 {
3308 int ext;
3309 tree decl;
3310 ext = disable_extension_diagnostics ();
3311 c_parser_consume_token (parser);
3312 decl = c_parser_struct_declaration (parser);
3313 restore_extension_diagnostics (ext);
3314 return decl;
3315 }
3316 if (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
3317 {
3318 c_parser_static_assert_declaration_no_semi (parser);
3319 return NULL_TREE;
3320 }
3321 specs = build_null_declspecs ();
3322 decl_loc = c_parser_peek_token (parser)->location;
3323 /* Strictly by the standard, we shouldn't allow _Alignas here,
3324 but it appears to have been intended to allow it there, so
3325 we're keeping it as it is until WG14 reaches a conclusion
3326 of N1731.
3327 <http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1731.pdf> */
3328 c_parser_declspecs (parser, specs, false, true, true,
3329 true, false, cla_nonabstract_decl);
3330 if (parser->error)
3331 return NULL_TREE;
3332 if (!specs->declspecs_seen_p)
3333 {
3334 c_parser_error (parser, "expected specifier-qualifier-list");
3335 return NULL_TREE;
3336 }
3337 finish_declspecs (specs);
3338 if (c_parser_next_token_is (parser, CPP_SEMICOLON)
3339 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3340 {
3341 tree ret;
3342 if (specs->typespec_kind == ctsk_none)
3343 {
3344 pedwarn (decl_loc, OPT_Wpedantic,
3345 "ISO C forbids member declarations with no members");
3346 shadow_tag_warned (specs, pedantic);
3347 ret = NULL_TREE;
3348 }
3349 else
3350 {
3351 /* Support for unnamed structs or unions as members of
3352 structs or unions (which is [a] useful and [b] supports
3353 MS P-SDK). */
3354 tree attrs = NULL;
3355
3356 ret = grokfield (c_parser_peek_token (parser)->location,
3357 build_id_declarator (NULL_TREE), specs,
3358 NULL_TREE, &attrs);
3359 if (ret)
3360 decl_attributes (&ret, attrs, 0);
3361 }
3362 return ret;
3363 }
3364
3365 /* Provide better error recovery. Note that a type name here is valid,
3366 and will be treated as a field name. */
3367 if (specs->typespec_kind == ctsk_tagdef
3368 && TREE_CODE (specs->type) != ENUMERAL_TYPE
3369 && c_parser_next_token_starts_declspecs (parser)
3370 && !c_parser_next_token_is (parser, CPP_NAME))
3371 {
3372 c_parser_error (parser, "expected %<;%>, identifier or %<(%>");
3373 parser->error = false;
3374 return NULL_TREE;
3375 }
3376
3377 pending_xref_error ();
3378 prefix_attrs = specs->attrs;
3379 all_prefix_attrs = prefix_attrs;
3380 specs->attrs = NULL_TREE;
3381 decls = NULL_TREE;
3382 while (true)
3383 {
3384 /* Declaring one or more declarators or un-named bit-fields. */
3385 struct c_declarator *declarator;
3386 bool dummy = false;
3387 if (c_parser_next_token_is (parser, CPP_COLON))
3388 declarator = build_id_declarator (NULL_TREE);
3389 else
3390 declarator = c_parser_declarator (parser,
3391 specs->typespec_kind != ctsk_none,
3392 C_DTR_NORMAL, &dummy);
3393 if (declarator == NULL)
3394 {
3395 c_parser_skip_to_end_of_block_or_statement (parser);
3396 break;
3397 }
3398 if (c_parser_next_token_is (parser, CPP_COLON)
3399 || c_parser_next_token_is (parser, CPP_COMMA)
3400 || c_parser_next_token_is (parser, CPP_SEMICOLON)
3401 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE)
3402 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3403 {
3404 tree postfix_attrs = NULL_TREE;
3405 tree width = NULL_TREE;
3406 tree d;
3407 if (c_parser_next_token_is (parser, CPP_COLON))
3408 {
3409 c_parser_consume_token (parser);
3410 width = c_parser_expr_no_commas (parser, NULL).value;
3411 }
3412 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3413 postfix_attrs = c_parser_attributes (parser);
3414 d = grokfield (c_parser_peek_token (parser)->location,
3415 declarator, specs, width, &all_prefix_attrs);
3416 decl_attributes (&d, chainon (postfix_attrs,
3417 all_prefix_attrs), 0);
3418 DECL_CHAIN (d) = decls;
3419 decls = d;
3420 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3421 all_prefix_attrs = chainon (c_parser_attributes (parser),
3422 prefix_attrs);
3423 else
3424 all_prefix_attrs = prefix_attrs;
3425 if (c_parser_next_token_is (parser, CPP_COMMA))
3426 c_parser_consume_token (parser);
3427 else if (c_parser_next_token_is (parser, CPP_SEMICOLON)
3428 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3429 {
3430 /* Semicolon consumed in caller. */
3431 break;
3432 }
3433 else
3434 {
3435 c_parser_error (parser, "expected %<,%>, %<;%> or %<}%>");
3436 break;
3437 }
3438 }
3439 else
3440 {
3441 c_parser_error (parser,
3442 "expected %<:%>, %<,%>, %<;%>, %<}%> or "
3443 "%<__attribute__%>");
3444 break;
3445 }
3446 }
3447 return decls;
3448 }
3449
3450 /* Parse a typeof specifier (a GNU extension).
3451
3452 typeof-specifier:
3453 typeof ( expression )
3454 typeof ( type-name )
3455 */
3456
3457 static struct c_typespec
3458 c_parser_typeof_specifier (c_parser *parser)
3459 {
3460 struct c_typespec ret;
3461 ret.kind = ctsk_typeof;
3462 ret.spec = error_mark_node;
3463 ret.expr = NULL_TREE;
3464 ret.expr_const_operands = true;
3465 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TYPEOF));
3466 c_parser_consume_token (parser);
3467 c_inhibit_evaluation_warnings++;
3468 in_typeof++;
3469 matching_parens parens;
3470 if (!parens.require_open (parser))
3471 {
3472 c_inhibit_evaluation_warnings--;
3473 in_typeof--;
3474 return ret;
3475 }
3476 if (c_parser_next_tokens_start_typename (parser, cla_prefer_id))
3477 {
3478 struct c_type_name *type = c_parser_type_name (parser);
3479 c_inhibit_evaluation_warnings--;
3480 in_typeof--;
3481 if (type != NULL)
3482 {
3483 ret.spec = groktypename (type, &ret.expr, &ret.expr_const_operands);
3484 pop_maybe_used (variably_modified_type_p (ret.spec, NULL_TREE));
3485 }
3486 }
3487 else
3488 {
3489 bool was_vm;
3490 location_t here = c_parser_peek_token (parser)->location;
3491 struct c_expr expr = c_parser_expression (parser);
3492 c_inhibit_evaluation_warnings--;
3493 in_typeof--;
3494 if (TREE_CODE (expr.value) == COMPONENT_REF
3495 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
3496 error_at (here, "%<typeof%> applied to a bit-field");
3497 mark_exp_read (expr.value);
3498 ret.spec = TREE_TYPE (expr.value);
3499 was_vm = variably_modified_type_p (ret.spec, NULL_TREE);
3500 /* This is returned with the type so that when the type is
3501 evaluated, this can be evaluated. */
3502 if (was_vm)
3503 ret.expr = c_fully_fold (expr.value, false, &ret.expr_const_operands);
3504 pop_maybe_used (was_vm);
3505 /* For use in macros such as those in <stdatomic.h>, remove all
3506 qualifiers from atomic types. (const can be an issue for more macros
3507 using typeof than just the <stdatomic.h> ones.) */
3508 if (ret.spec != error_mark_node && TYPE_ATOMIC (ret.spec))
3509 ret.spec = c_build_qualified_type (ret.spec, TYPE_UNQUALIFIED);
3510 }
3511 parens.skip_until_found_close (parser);
3512 return ret;
3513 }
3514
3515 /* Parse an alignment-specifier.
3516
3517 C11 6.7.5:
3518
3519 alignment-specifier:
3520 _Alignas ( type-name )
3521 _Alignas ( constant-expression )
3522 */
3523
3524 static tree
3525 c_parser_alignas_specifier (c_parser * parser)
3526 {
3527 tree ret = error_mark_node;
3528 location_t loc = c_parser_peek_token (parser)->location;
3529 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNAS));
3530 c_parser_consume_token (parser);
3531 if (flag_isoc99)
3532 pedwarn_c99 (loc, OPT_Wpedantic,
3533 "ISO C99 does not support %<_Alignas%>");
3534 else
3535 pedwarn_c99 (loc, OPT_Wpedantic,
3536 "ISO C90 does not support %<_Alignas%>");
3537 matching_parens parens;
3538 if (!parens.require_open (parser))
3539 return ret;
3540 if (c_parser_next_tokens_start_typename (parser, cla_prefer_id))
3541 {
3542 struct c_type_name *type = c_parser_type_name (parser);
3543 if (type != NULL)
3544 ret = c_sizeof_or_alignof_type (loc, groktypename (type, NULL, NULL),
3545 false, true, 1);
3546 }
3547 else
3548 ret = c_parser_expr_no_commas (parser, NULL).value;
3549 parens.skip_until_found_close (parser);
3550 return ret;
3551 }
3552
3553 /* Parse a declarator, possibly an abstract declarator (C90 6.5.4,
3554 6.5.5, C99 6.7.5, 6.7.6, C11 6.7.6, 6.7.7). If TYPE_SEEN_P then
3555 a typedef name may be redeclared; otherwise it may not. KIND
3556 indicates which kind of declarator is wanted. Returns a valid
3557 declarator except in the case of a syntax error in which case NULL is
3558 returned. *SEEN_ID is set to true if an identifier being declared is
3559 seen; this is used to diagnose bad forms of abstract array declarators
3560 and to determine whether an identifier list is syntactically permitted.
3561
3562 declarator:
3563 pointer[opt] direct-declarator
3564
3565 direct-declarator:
3566 identifier
3567 ( attributes[opt] declarator )
3568 direct-declarator array-declarator
3569 direct-declarator ( parameter-type-list )
3570 direct-declarator ( identifier-list[opt] )
3571
3572 pointer:
3573 * type-qualifier-list[opt]
3574 * type-qualifier-list[opt] pointer
3575
3576 type-qualifier-list:
3577 type-qualifier
3578 attributes
3579 type-qualifier-list type-qualifier
3580 type-qualifier-list attributes
3581
3582 array-declarator:
3583 [ type-qualifier-list[opt] assignment-expression[opt] ]
3584 [ static type-qualifier-list[opt] assignment-expression ]
3585 [ type-qualifier-list static assignment-expression ]
3586 [ type-qualifier-list[opt] * ]
3587
3588 parameter-type-list:
3589 parameter-list
3590 parameter-list , ...
3591
3592 parameter-list:
3593 parameter-declaration
3594 parameter-list , parameter-declaration
3595
3596 parameter-declaration:
3597 declaration-specifiers declarator attributes[opt]
3598 declaration-specifiers abstract-declarator[opt] attributes[opt]
3599
3600 identifier-list:
3601 identifier
3602 identifier-list , identifier
3603
3604 abstract-declarator:
3605 pointer
3606 pointer[opt] direct-abstract-declarator
3607
3608 direct-abstract-declarator:
3609 ( attributes[opt] abstract-declarator )
3610 direct-abstract-declarator[opt] array-declarator
3611 direct-abstract-declarator[opt] ( parameter-type-list[opt] )
3612
3613 GNU extensions:
3614
3615 direct-declarator:
3616 direct-declarator ( parameter-forward-declarations
3617 parameter-type-list[opt] )
3618
3619 direct-abstract-declarator:
3620 direct-abstract-declarator[opt] ( parameter-forward-declarations
3621 parameter-type-list[opt] )
3622
3623 parameter-forward-declarations:
3624 parameter-list ;
3625 parameter-forward-declarations parameter-list ;
3626
3627 The uses of attributes shown above are GNU extensions.
3628
3629 Some forms of array declarator are not included in C99 in the
3630 syntax for abstract declarators; these are disallowed elsewhere.
3631 This may be a defect (DR#289).
3632
3633 This function also accepts an omitted abstract declarator as being
3634 an abstract declarator, although not part of the formal syntax. */
3635
3636 struct c_declarator *
3637 c_parser_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
3638 bool *seen_id)
3639 {
3640 /* Parse any initial pointer part. */
3641 if (c_parser_next_token_is (parser, CPP_MULT))
3642 {
3643 struct c_declspecs *quals_attrs = build_null_declspecs ();
3644 struct c_declarator *inner;
3645 c_parser_consume_token (parser);
3646 c_parser_declspecs (parser, quals_attrs, false, false, true,
3647 false, false, cla_prefer_id);
3648 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
3649 if (inner == NULL)
3650 return NULL;
3651 else
3652 return make_pointer_declarator (quals_attrs, inner);
3653 }
3654 /* Now we have a direct declarator, direct abstract declarator or
3655 nothing (which counts as a direct abstract declarator here). */
3656 return c_parser_direct_declarator (parser, type_seen_p, kind, seen_id);
3657 }
3658
3659 /* Parse a direct declarator or direct abstract declarator; arguments
3660 as c_parser_declarator. */
3661
3662 static struct c_declarator *
3663 c_parser_direct_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
3664 bool *seen_id)
3665 {
3666 /* The direct declarator must start with an identifier (possibly
3667 omitted) or a parenthesized declarator (possibly abstract). In
3668 an ordinary declarator, initial parentheses must start a
3669 parenthesized declarator. In an abstract declarator or parameter
3670 declarator, they could start a parenthesized declarator or a
3671 parameter list. To tell which, the open parenthesis and any
3672 following attributes must be read. If a declaration specifier
3673 follows, then it is a parameter list; if the specifier is a
3674 typedef name, there might be an ambiguity about redeclaring it,
3675 which is resolved in the direction of treating it as a typedef
3676 name. If a close parenthesis follows, it is also an empty
3677 parameter list, as the syntax does not permit empty abstract
3678 declarators. Otherwise, it is a parenthesized declarator (in
3679 which case the analysis may be repeated inside it, recursively).
3680
3681 ??? There is an ambiguity in a parameter declaration "int
3682 (__attribute__((foo)) x)", where x is not a typedef name: it
3683 could be an abstract declarator for a function, or declare x with
3684 parentheses. The proper resolution of this ambiguity needs
3685 documenting. At present we follow an accident of the old
3686 parser's implementation, whereby the first parameter must have
3687 some declaration specifiers other than just attributes. Thus as
3688 a parameter declaration it is treated as a parenthesized
3689 parameter named x, and as an abstract declarator it is
3690 rejected.
3691
3692 ??? Also following the old parser, attributes inside an empty
3693 parameter list are ignored, making it a list not yielding a
3694 prototype, rather than giving an error or making it have one
3695 parameter with implicit type int.
3696
3697 ??? Also following the old parser, typedef names may be
3698 redeclared in declarators, but not Objective-C class names. */
3699
3700 if (kind != C_DTR_ABSTRACT
3701 && c_parser_next_token_is (parser, CPP_NAME)
3702 && ((type_seen_p
3703 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
3704 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
3705 || c_parser_peek_token (parser)->id_kind == C_ID_ID))
3706 {
3707 struct c_declarator *inner
3708 = build_id_declarator (c_parser_peek_token (parser)->value);
3709 *seen_id = true;
3710 inner->id_loc = c_parser_peek_token (parser)->location;
3711 c_parser_consume_token (parser);
3712 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3713 }
3714
3715 if (kind != C_DTR_NORMAL
3716 && c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
3717 {
3718 struct c_declarator *inner = build_id_declarator (NULL_TREE);
3719 inner->id_loc = c_parser_peek_token (parser)->location;
3720 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3721 }
3722
3723 /* Either we are at the end of an abstract declarator, or we have
3724 parentheses. */
3725
3726 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
3727 {
3728 tree attrs;
3729 struct c_declarator *inner;
3730 c_parser_consume_token (parser);
3731 attrs = c_parser_attributes (parser);
3732 if (kind != C_DTR_NORMAL
3733 && (c_parser_next_token_starts_declspecs (parser)
3734 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN)))
3735 {
3736 struct c_arg_info *args
3737 = c_parser_parms_declarator (parser, kind == C_DTR_NORMAL,
3738 attrs);
3739 if (args == NULL)
3740 return NULL;
3741 else
3742 {
3743 inner
3744 = build_function_declarator (args,
3745 build_id_declarator (NULL_TREE));
3746 return c_parser_direct_declarator_inner (parser, *seen_id,
3747 inner);
3748 }
3749 }
3750 /* A parenthesized declarator. */
3751 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
3752 if (inner != NULL && attrs != NULL)
3753 inner = build_attrs_declarator (attrs, inner);
3754 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3755 {
3756 c_parser_consume_token (parser);
3757 if (inner == NULL)
3758 return NULL;
3759 else
3760 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3761 }
3762 else
3763 {
3764 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3765 "expected %<)%>");
3766 return NULL;
3767 }
3768 }
3769 else
3770 {
3771 if (kind == C_DTR_NORMAL)
3772 {
3773 c_parser_error (parser, "expected identifier or %<(%>");
3774 return NULL;
3775 }
3776 else
3777 return build_id_declarator (NULL_TREE);
3778 }
3779 }
3780
3781 /* Parse part of a direct declarator or direct abstract declarator,
3782 given that some (in INNER) has already been parsed; ID_PRESENT is
3783 true if an identifier is present, false for an abstract
3784 declarator. */
3785
3786 static struct c_declarator *
3787 c_parser_direct_declarator_inner (c_parser *parser, bool id_present,
3788 struct c_declarator *inner)
3789 {
3790 /* Parse a sequence of array declarators and parameter lists. */
3791 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
3792 {
3793 location_t brace_loc = c_parser_peek_token (parser)->location;
3794 struct c_declarator *declarator;
3795 struct c_declspecs *quals_attrs = build_null_declspecs ();
3796 bool static_seen;
3797 bool star_seen;
3798 struct c_expr dimen;
3799 dimen.value = NULL_TREE;
3800 dimen.original_code = ERROR_MARK;
3801 dimen.original_type = NULL_TREE;
3802 c_parser_consume_token (parser);
3803 c_parser_declspecs (parser, quals_attrs, false, false, true,
3804 false, false, cla_prefer_id);
3805 static_seen = c_parser_next_token_is_keyword (parser, RID_STATIC);
3806 if (static_seen)
3807 c_parser_consume_token (parser);
3808 if (static_seen && !quals_attrs->declspecs_seen_p)
3809 c_parser_declspecs (parser, quals_attrs, false, false, true,
3810 false, false, cla_prefer_id);
3811 if (!quals_attrs->declspecs_seen_p)
3812 quals_attrs = NULL;
3813 /* If "static" is present, there must be an array dimension.
3814 Otherwise, there may be a dimension, "*", or no
3815 dimension. */
3816 if (static_seen)
3817 {
3818 star_seen = false;
3819 dimen = c_parser_expr_no_commas (parser, NULL);
3820 }
3821 else
3822 {
3823 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3824 {
3825 dimen.value = NULL_TREE;
3826 star_seen = false;
3827 }
3828 else if (c_parser_next_token_is (parser, CPP_MULT))
3829 {
3830 if (c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_SQUARE)
3831 {
3832 dimen.value = NULL_TREE;
3833 star_seen = true;
3834 c_parser_consume_token (parser);
3835 }
3836 else
3837 {
3838 star_seen = false;
3839 dimen = c_parser_expr_no_commas (parser, NULL);
3840 }
3841 }
3842 else
3843 {
3844 star_seen = false;
3845 dimen = c_parser_expr_no_commas (parser, NULL);
3846 }
3847 }
3848 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3849 c_parser_consume_token (parser);
3850 else
3851 {
3852 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3853 "expected %<]%>");
3854 return NULL;
3855 }
3856 if (dimen.value)
3857 dimen = convert_lvalue_to_rvalue (brace_loc, dimen, true, true);
3858 declarator = build_array_declarator (brace_loc, dimen.value, quals_attrs,
3859 static_seen, star_seen);
3860 if (declarator == NULL)
3861 return NULL;
3862 inner = set_array_declarator_inner (declarator, inner);
3863 return c_parser_direct_declarator_inner (parser, id_present, inner);
3864 }
3865 else if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
3866 {
3867 tree attrs;
3868 struct c_arg_info *args;
3869 c_parser_consume_token (parser);
3870 attrs = c_parser_attributes (parser);
3871 args = c_parser_parms_declarator (parser, id_present, attrs);
3872 if (args == NULL)
3873 return NULL;
3874 else
3875 {
3876 inner = build_function_declarator (args, inner);
3877 return c_parser_direct_declarator_inner (parser, id_present, inner);
3878 }
3879 }
3880 return inner;
3881 }
3882
3883 /* Parse a parameter list or identifier list, including the closing
3884 parenthesis but not the opening one. ATTRS are the attributes at
3885 the start of the list. ID_LIST_OK is true if an identifier list is
3886 acceptable; such a list must not have attributes at the start. */
3887
3888 static struct c_arg_info *
3889 c_parser_parms_declarator (c_parser *parser, bool id_list_ok, tree attrs)
3890 {
3891 push_scope ();
3892 declare_parm_level ();
3893 /* If the list starts with an identifier, it is an identifier list.
3894 Otherwise, it is either a prototype list or an empty list. */
3895 if (id_list_ok
3896 && !attrs
3897 && c_parser_next_token_is (parser, CPP_NAME)
3898 && c_parser_peek_token (parser)->id_kind == C_ID_ID
3899
3900 /* Look ahead to detect typos in type names. */
3901 && c_parser_peek_2nd_token (parser)->type != CPP_NAME
3902 && c_parser_peek_2nd_token (parser)->type != CPP_MULT
3903 && c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN
3904 && c_parser_peek_2nd_token (parser)->type != CPP_OPEN_SQUARE
3905 && c_parser_peek_2nd_token (parser)->type != CPP_KEYWORD)
3906 {
3907 tree list = NULL_TREE, *nextp = &list;
3908 while (c_parser_next_token_is (parser, CPP_NAME)
3909 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
3910 {
3911 *nextp = build_tree_list (NULL_TREE,
3912 c_parser_peek_token (parser)->value);
3913 nextp = & TREE_CHAIN (*nextp);
3914 c_parser_consume_token (parser);
3915 if (c_parser_next_token_is_not (parser, CPP_COMMA))
3916 break;
3917 c_parser_consume_token (parser);
3918 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3919 {
3920 c_parser_error (parser, "expected identifier");
3921 break;
3922 }
3923 }
3924 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3925 {
3926 struct c_arg_info *ret = build_arg_info ();
3927 ret->types = list;
3928 c_parser_consume_token (parser);
3929 pop_scope ();
3930 return ret;
3931 }
3932 else
3933 {
3934 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3935 "expected %<)%>");
3936 pop_scope ();
3937 return NULL;
3938 }
3939 }
3940 else
3941 {
3942 struct c_arg_info *ret = c_parser_parms_list_declarator (parser, attrs,
3943 NULL);
3944 pop_scope ();
3945 return ret;
3946 }
3947 }
3948
3949 /* Parse a parameter list (possibly empty), including the closing
3950 parenthesis but not the opening one. ATTRS are the attributes at
3951 the start of the list. EXPR is NULL or an expression that needs to
3952 be evaluated for the side effects of array size expressions in the
3953 parameters. */
3954
3955 static struct c_arg_info *
3956 c_parser_parms_list_declarator (c_parser *parser, tree attrs, tree expr)
3957 {
3958 bool bad_parm = false;
3959
3960 /* ??? Following the old parser, forward parameter declarations may
3961 use abstract declarators, and if no real parameter declarations
3962 follow the forward declarations then this is not diagnosed. Also
3963 note as above that attributes are ignored as the only contents of
3964 the parentheses, or as the only contents after forward
3965 declarations. */
3966 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3967 {
3968 struct c_arg_info *ret = build_arg_info ();
3969 c_parser_consume_token (parser);
3970 return ret;
3971 }
3972 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3973 {
3974 struct c_arg_info *ret = build_arg_info ();
3975
3976 if (flag_allow_parameterless_variadic_functions)
3977 {
3978 /* F (...) is allowed. */
3979 ret->types = NULL_TREE;
3980 }
3981 else
3982 {
3983 /* Suppress -Wold-style-definition for this case. */
3984 ret->types = error_mark_node;
3985 error_at (c_parser_peek_token (parser)->location,
3986 "ISO C requires a named argument before %<...%>");
3987 }
3988 c_parser_consume_token (parser);
3989 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3990 {
3991 c_parser_consume_token (parser);
3992 return ret;
3993 }
3994 else
3995 {
3996 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3997 "expected %<)%>");
3998 return NULL;
3999 }
4000 }
4001 /* Nonempty list of parameters, either terminated with semicolon
4002 (forward declarations; recurse) or with close parenthesis (normal
4003 function) or with ", ... )" (variadic function). */
4004 while (true)
4005 {
4006 /* Parse a parameter. */
4007 struct c_parm *parm = c_parser_parameter_declaration (parser, attrs);
4008 attrs = NULL_TREE;
4009 if (parm == NULL)
4010 bad_parm = true;
4011 else
4012 push_parm_decl (parm, &expr);
4013 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4014 {
4015 tree new_attrs;
4016 c_parser_consume_token (parser);
4017 mark_forward_parm_decls ();
4018 new_attrs = c_parser_attributes (parser);
4019 return c_parser_parms_list_declarator (parser, new_attrs, expr);
4020 }
4021 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4022 {
4023 c_parser_consume_token (parser);
4024 if (bad_parm)
4025 return NULL;
4026 else
4027 return get_parm_info (false, expr);
4028 }
4029 if (!c_parser_require (parser, CPP_COMMA,
4030 "expected %<;%>, %<,%> or %<)%>",
4031 UNKNOWN_LOCATION, false))
4032 {
4033 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4034 return NULL;
4035 }
4036 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
4037 {
4038 c_parser_consume_token (parser);
4039 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4040 {
4041 c_parser_consume_token (parser);
4042 if (bad_parm)
4043 return NULL;
4044 else
4045 return get_parm_info (true, expr);
4046 }
4047 else
4048 {
4049 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4050 "expected %<)%>");
4051 return NULL;
4052 }
4053 }
4054 }
4055 }
4056
4057 /* Parse a parameter declaration. ATTRS are the attributes at the
4058 start of the declaration if it is the first parameter. */
4059
4060 static struct c_parm *
4061 c_parser_parameter_declaration (c_parser *parser, tree attrs)
4062 {
4063 struct c_declspecs *specs;
4064 struct c_declarator *declarator;
4065 tree prefix_attrs;
4066 tree postfix_attrs = NULL_TREE;
4067 bool dummy = false;
4068
4069 /* Accept #pragmas between parameter declarations. */
4070 while (c_parser_next_token_is (parser, CPP_PRAGMA))
4071 c_parser_pragma (parser, pragma_param, NULL);
4072
4073 if (!c_parser_next_token_starts_declspecs (parser))
4074 {
4075 c_token *token = c_parser_peek_token (parser);
4076 if (parser->error)
4077 return NULL;
4078 c_parser_set_source_position_from_token (token);
4079 if (c_parser_next_tokens_start_typename (parser, cla_prefer_type))
4080 {
4081 auto_diagnostic_group d;
4082 name_hint hint = lookup_name_fuzzy (token->value,
4083 FUZZY_LOOKUP_TYPENAME,
4084 token->location);
4085 if (const char *suggestion = hint.suggestion ())
4086 {
4087 gcc_rich_location richloc (token->location);
4088 richloc.add_fixit_replace (suggestion);
4089 error_at (&richloc,
4090 "unknown type name %qE; did you mean %qs?",
4091 token->value, suggestion);
4092 }
4093 else
4094 error_at (token->location, "unknown type name %qE", token->value);
4095 parser->error = true;
4096 }
4097 /* ??? In some Objective-C cases '...' isn't applicable so there
4098 should be a different message. */
4099 else
4100 c_parser_error (parser,
4101 "expected declaration specifiers or %<...%>");
4102 c_parser_skip_to_end_of_parameter (parser);
4103 return NULL;
4104 }
4105
4106 location_t start_loc = c_parser_peek_token (parser)->location;
4107
4108 specs = build_null_declspecs ();
4109 if (attrs)
4110 {
4111 declspecs_add_attrs (input_location, specs, attrs);
4112 attrs = NULL_TREE;
4113 }
4114 c_parser_declspecs (parser, specs, true, true, true, true, false,
4115 cla_nonabstract_decl);
4116 finish_declspecs (specs);
4117 pending_xref_error ();
4118 prefix_attrs = specs->attrs;
4119 specs->attrs = NULL_TREE;
4120 declarator = c_parser_declarator (parser,
4121 specs->typespec_kind != ctsk_none,
4122 C_DTR_PARM, &dummy);
4123 if (declarator == NULL)
4124 {
4125 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
4126 return NULL;
4127 }
4128 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
4129 postfix_attrs = c_parser_attributes (parser);
4130
4131 /* Generate a location for the parameter, ranging from the start of the
4132 initial token to the end of the final token.
4133
4134 If we have a identifier, then use it for the caret location, e.g.
4135
4136 extern int callee (int one, int (*two)(int, int), float three);
4137 ~~~~~~^~~~~~~~~~~~~~
4138
4139 otherwise, reuse the start location for the caret location e.g.:
4140
4141 extern int callee (int one, int (*)(int, int), float three);
4142 ^~~~~~~~~~~~~~~~~
4143 */
4144 location_t end_loc = parser->last_token_location;
4145
4146 /* Find any cdk_id declarator; determine if we have an identifier. */
4147 c_declarator *id_declarator = declarator;
4148 while (id_declarator && id_declarator->kind != cdk_id)
4149 id_declarator = id_declarator->declarator;
4150 location_t caret_loc = (id_declarator->u.id
4151 ? id_declarator->id_loc
4152 : start_loc);
4153 location_t param_loc = make_location (caret_loc, start_loc, end_loc);
4154
4155 return build_c_parm (specs, chainon (postfix_attrs, prefix_attrs),
4156 declarator, param_loc);
4157 }
4158
4159 /* Parse a string literal in an asm expression. It should not be
4160 translated, and wide string literals are an error although
4161 permitted by the syntax. This is a GNU extension.
4162
4163 asm-string-literal:
4164 string-literal
4165
4166 ??? At present, following the old parser, the caller needs to have
4167 set lex_untranslated_string to 1. It would be better to follow the
4168 C++ parser rather than using this kludge. */
4169
4170 static tree
4171 c_parser_asm_string_literal (c_parser *parser)
4172 {
4173 tree str;
4174 int save_flag = warn_overlength_strings;
4175 warn_overlength_strings = 0;
4176 if (c_parser_next_token_is (parser, CPP_STRING))
4177 {
4178 str = c_parser_peek_token (parser)->value;
4179 c_parser_consume_token (parser);
4180 }
4181 else if (c_parser_next_token_is (parser, CPP_WSTRING))
4182 {
4183 error_at (c_parser_peek_token (parser)->location,
4184 "wide string literal in %<asm%>");
4185 str = build_string (1, "");
4186 c_parser_consume_token (parser);
4187 }
4188 else
4189 {
4190 c_parser_error (parser, "expected string literal");
4191 str = NULL_TREE;
4192 }
4193 warn_overlength_strings = save_flag;
4194 return str;
4195 }
4196
4197 /* Parse a simple asm expression. This is used in restricted
4198 contexts, where a full expression with inputs and outputs does not
4199 make sense. This is a GNU extension.
4200
4201 simple-asm-expr:
4202 asm ( asm-string-literal )
4203 */
4204
4205 static tree
4206 c_parser_simple_asm_expr (c_parser *parser)
4207 {
4208 tree str;
4209 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
4210 /* ??? Follow the C++ parser rather than using the
4211 lex_untranslated_string kludge. */
4212 parser->lex_untranslated_string = true;
4213 c_parser_consume_token (parser);
4214 matching_parens parens;
4215 if (!parens.require_open (parser))
4216 {
4217 parser->lex_untranslated_string = false;
4218 return NULL_TREE;
4219 }
4220 str = c_parser_asm_string_literal (parser);
4221 parser->lex_untranslated_string = false;
4222 if (!parens.require_close (parser))
4223 {
4224 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4225 return NULL_TREE;
4226 }
4227 return str;
4228 }
4229
4230 static tree
4231 c_parser_attribute_any_word (c_parser *parser)
4232 {
4233 tree attr_name = NULL_TREE;
4234
4235 if (c_parser_next_token_is (parser, CPP_KEYWORD))
4236 {
4237 /* ??? See comment above about what keywords are accepted here. */
4238 bool ok;
4239 switch (c_parser_peek_token (parser)->keyword)
4240 {
4241 case RID_STATIC:
4242 case RID_UNSIGNED:
4243 case RID_LONG:
4244 case RID_CONST:
4245 case RID_EXTERN:
4246 case RID_REGISTER:
4247 case RID_TYPEDEF:
4248 case RID_SHORT:
4249 case RID_INLINE:
4250 case RID_NORETURN:
4251 case RID_VOLATILE:
4252 case RID_SIGNED:
4253 case RID_AUTO:
4254 case RID_RESTRICT:
4255 case RID_COMPLEX:
4256 case RID_THREAD:
4257 case RID_INT:
4258 case RID_CHAR:
4259 case RID_FLOAT:
4260 case RID_DOUBLE:
4261 case RID_VOID:
4262 case RID_DFLOAT32:
4263 case RID_DFLOAT64:
4264 case RID_DFLOAT128:
4265 CASE_RID_FLOATN_NX:
4266 case RID_BOOL:
4267 case RID_FRACT:
4268 case RID_ACCUM:
4269 case RID_SAT:
4270 case RID_TRANSACTION_ATOMIC:
4271 case RID_TRANSACTION_CANCEL:
4272 case RID_ATOMIC:
4273 case RID_AUTO_TYPE:
4274 case RID_INT_N_0:
4275 case RID_INT_N_1:
4276 case RID_INT_N_2:
4277 case RID_INT_N_3:
4278 ok = true;
4279 break;
4280 default:
4281 ok = false;
4282 break;
4283 }
4284 if (!ok)
4285 return NULL_TREE;
4286
4287 /* Accept __attribute__((__const)) as __attribute__((const)) etc. */
4288 attr_name = ridpointers[(int) c_parser_peek_token (parser)->keyword];
4289 }
4290 else if (c_parser_next_token_is (parser, CPP_NAME))
4291 attr_name = c_parser_peek_token (parser)->value;
4292
4293 return attr_name;
4294 }
4295
4296 /* Parse (possibly empty) attributes. This is a GNU extension.
4297
4298 attributes:
4299 empty
4300 attributes attribute
4301
4302 attribute:
4303 __attribute__ ( ( attribute-list ) )
4304
4305 attribute-list:
4306 attrib
4307 attribute_list , attrib
4308
4309 attrib:
4310 empty
4311 any-word
4312 any-word ( identifier )
4313 any-word ( identifier , nonempty-expr-list )
4314 any-word ( expr-list )
4315
4316 where the "identifier" must not be declared as a type, and
4317 "any-word" may be any identifier (including one declared as a
4318 type), a reserved word storage class specifier, type specifier or
4319 type qualifier. ??? This still leaves out most reserved keywords
4320 (following the old parser), shouldn't we include them, and why not
4321 allow identifiers declared as types to start the arguments?
4322 When EXPECT_COMMA is true, expect the attribute to be preceded
4323 by a comma and fail if it isn't.
4324 When EMPTY_OK is true, allow and consume any number of consecutive
4325 commas with no attributes in between. */
4326
4327 static tree
4328 c_parser_attribute (c_parser *parser, tree attrs,
4329 bool expect_comma = false, bool empty_ok = true)
4330 {
4331 bool comma_first = c_parser_next_token_is (parser, CPP_COMMA);
4332 if (!comma_first
4333 && !c_parser_next_token_is (parser, CPP_NAME)
4334 && !c_parser_next_token_is (parser, CPP_KEYWORD))
4335 return NULL_TREE;
4336
4337 while (c_parser_next_token_is (parser, CPP_COMMA))
4338 {
4339 c_parser_consume_token (parser);
4340 if (!empty_ok)
4341 return attrs;
4342 }
4343
4344 tree attr_name = c_parser_attribute_any_word (parser);
4345 if (attr_name == NULL_TREE)
4346 return NULL_TREE;
4347
4348 attr_name = canonicalize_attr_name (attr_name);
4349 c_parser_consume_token (parser);
4350
4351 tree attr;
4352 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
4353 {
4354 if (expect_comma && !comma_first)
4355 {
4356 /* A comma is missing between the last attribute on the chain
4357 and this one. */
4358 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4359 "expected %<)%>");
4360 return error_mark_node;
4361 }
4362 attr = build_tree_list (attr_name, NULL_TREE);
4363 /* Add this attribute to the list. */
4364 attrs = chainon (attrs, attr);
4365 return attrs;
4366 }
4367 c_parser_consume_token (parser);
4368
4369 vec<tree, va_gc> *expr_list;
4370 tree attr_args;
4371 /* Parse the attribute contents. If they start with an
4372 identifier which is followed by a comma or close
4373 parenthesis, then the arguments start with that
4374 identifier; otherwise they are an expression list.
4375 In objective-c the identifier may be a classname. */
4376 if (c_parser_next_token_is (parser, CPP_NAME)
4377 && (c_parser_peek_token (parser)->id_kind == C_ID_ID
4378 || (c_dialect_objc ()
4379 && c_parser_peek_token (parser)->id_kind
4380 == C_ID_CLASSNAME))
4381 && ((c_parser_peek_2nd_token (parser)->type == CPP_COMMA)
4382 || (c_parser_peek_2nd_token (parser)->type
4383 == CPP_CLOSE_PAREN))
4384 && (attribute_takes_identifier_p (attr_name)
4385 || (c_dialect_objc ()
4386 && c_parser_peek_token (parser)->id_kind
4387 == C_ID_CLASSNAME)))
4388 {
4389 tree arg1 = c_parser_peek_token (parser)->value;
4390 c_parser_consume_token (parser);
4391 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4392 attr_args = build_tree_list (NULL_TREE, arg1);
4393 else
4394 {
4395 tree tree_list;
4396 c_parser_consume_token (parser);
4397 expr_list = c_parser_expr_list (parser, false, true,
4398 NULL, NULL, NULL, NULL);
4399 tree_list = build_tree_list_vec (expr_list);
4400 attr_args = tree_cons (NULL_TREE, arg1, tree_list);
4401 release_tree_vector (expr_list);
4402 }
4403 }
4404 else
4405 {
4406 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4407 attr_args = NULL_TREE;
4408 else
4409 {
4410 expr_list = c_parser_expr_list (parser, false, true,
4411 NULL, NULL, NULL, NULL);
4412 attr_args = build_tree_list_vec (expr_list);
4413 release_tree_vector (expr_list);
4414 }
4415 }
4416
4417 attr = build_tree_list (attr_name, attr_args);
4418 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4419 c_parser_consume_token (parser);
4420 else
4421 {
4422 parser->lex_untranslated_string = false;
4423 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4424 "expected %<)%>");
4425 return error_mark_node;
4426 }
4427
4428 if (expect_comma && !comma_first)
4429 {
4430 /* A comma is missing between the last attribute on the chain
4431 and this one. */
4432 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4433 "expected %<)%>");
4434 return error_mark_node;
4435 }
4436
4437 /* Add this attribute to the list. */
4438 attrs = chainon (attrs, attr);
4439 return attrs;
4440 }
4441
4442 static tree
4443 c_parser_attributes (c_parser *parser)
4444 {
4445 tree attrs = NULL_TREE;
4446 while (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
4447 {
4448 /* ??? Follow the C++ parser rather than using the
4449 lex_untranslated_string kludge. */
4450 parser->lex_untranslated_string = true;
4451 /* Consume the `__attribute__' keyword. */
4452 c_parser_consume_token (parser);
4453 /* Look for the two `(' tokens. */
4454 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4455 {
4456 parser->lex_untranslated_string = false;
4457 return attrs;
4458 }
4459 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4460 {
4461 parser->lex_untranslated_string = false;
4462 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4463 return attrs;
4464 }
4465 /* Parse the attribute list. Require a comma between successive
4466 (possibly empty) attributes. */
4467 for (bool expect_comma = false; ; expect_comma = true)
4468 {
4469 /* Parse a single attribute. */
4470 tree attr = c_parser_attribute (parser, attrs, expect_comma);
4471 if (attr == error_mark_node)
4472 return attrs;
4473 if (!attr)
4474 break;
4475 attrs = attr;
4476 }
4477
4478 /* Look for the two `)' tokens. */
4479 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4480 c_parser_consume_token (parser);
4481 else
4482 {
4483 parser->lex_untranslated_string = false;
4484 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4485 "expected %<)%>");
4486 return attrs;
4487 }
4488 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4489 c_parser_consume_token (parser);
4490 else
4491 {
4492 parser->lex_untranslated_string = false;
4493 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4494 "expected %<)%>");
4495 return attrs;
4496 }
4497 parser->lex_untranslated_string = false;
4498 }
4499
4500 return attrs;
4501 }
4502
4503 /* Parse a type name (C90 6.5.5, C99 6.7.6, C11 6.7.7). ALIGNAS_OK
4504 says whether alignment specifiers are OK (only in cases that might
4505 be the type name of a compound literal).
4506
4507 type-name:
4508 specifier-qualifier-list abstract-declarator[opt]
4509 */
4510
4511 struct c_type_name *
4512 c_parser_type_name (c_parser *parser, bool alignas_ok)
4513 {
4514 struct c_declspecs *specs = build_null_declspecs ();
4515 struct c_declarator *declarator;
4516 struct c_type_name *ret;
4517 bool dummy = false;
4518 c_parser_declspecs (parser, specs, false, true, true, alignas_ok, false,
4519 cla_prefer_type);
4520 if (!specs->declspecs_seen_p)
4521 {
4522 c_parser_error (parser, "expected specifier-qualifier-list");
4523 return NULL;
4524 }
4525 if (specs->type != error_mark_node)
4526 {
4527 pending_xref_error ();
4528 finish_declspecs (specs);
4529 }
4530 declarator = c_parser_declarator (parser,
4531 specs->typespec_kind != ctsk_none,
4532 C_DTR_ABSTRACT, &dummy);
4533 if (declarator == NULL)
4534 return NULL;
4535 ret = XOBNEW (&parser_obstack, struct c_type_name);
4536 ret->specs = specs;
4537 ret->declarator = declarator;
4538 return ret;
4539 }
4540
4541 /* Parse an initializer (C90 6.5.7, C99 6.7.8, C11 6.7.9).
4542
4543 initializer:
4544 assignment-expression
4545 { initializer-list }
4546 { initializer-list , }
4547
4548 initializer-list:
4549 designation[opt] initializer
4550 initializer-list , designation[opt] initializer
4551
4552 designation:
4553 designator-list =
4554
4555 designator-list:
4556 designator
4557 designator-list designator
4558
4559 designator:
4560 array-designator
4561 . identifier
4562
4563 array-designator:
4564 [ constant-expression ]
4565
4566 GNU extensions:
4567
4568 initializer:
4569 { }
4570
4571 designation:
4572 array-designator
4573 identifier :
4574
4575 array-designator:
4576 [ constant-expression ... constant-expression ]
4577
4578 Any expression without commas is accepted in the syntax for the
4579 constant-expressions, with non-constant expressions rejected later.
4580
4581 This function is only used for top-level initializers; for nested
4582 ones, see c_parser_initval. */
4583
4584 static struct c_expr
4585 c_parser_initializer (c_parser *parser)
4586 {
4587 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
4588 return c_parser_braced_init (parser, NULL_TREE, false, NULL);
4589 else
4590 {
4591 struct c_expr ret;
4592 location_t loc = c_parser_peek_token (parser)->location;
4593 ret = c_parser_expr_no_commas (parser, NULL);
4594 if (TREE_CODE (ret.value) != STRING_CST
4595 && TREE_CODE (ret.value) != COMPOUND_LITERAL_EXPR)
4596 ret = convert_lvalue_to_rvalue (loc, ret, true, true);
4597 return ret;
4598 }
4599 }
4600
4601 /* The location of the last comma within the current initializer list,
4602 or UNKNOWN_LOCATION if not within one. */
4603
4604 location_t last_init_list_comma;
4605
4606 /* Parse a braced initializer list. TYPE is the type specified for a
4607 compound literal, and NULL_TREE for other initializers and for
4608 nested braced lists. NESTED_P is true for nested braced lists,
4609 false for the list of a compound literal or the list that is the
4610 top-level initializer in a declaration. */
4611
4612 static struct c_expr
4613 c_parser_braced_init (c_parser *parser, tree type, bool nested_p,
4614 struct obstack *outer_obstack)
4615 {
4616 struct c_expr ret;
4617 struct obstack braced_init_obstack;
4618 location_t brace_loc = c_parser_peek_token (parser)->location;
4619 gcc_obstack_init (&braced_init_obstack);
4620 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
4621 matching_braces braces;
4622 braces.consume_open (parser);
4623 if (nested_p)
4624 {
4625 finish_implicit_inits (brace_loc, outer_obstack);
4626 push_init_level (brace_loc, 0, &braced_init_obstack);
4627 }
4628 else
4629 really_start_incremental_init (type);
4630 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4631 {
4632 pedwarn (brace_loc, OPT_Wpedantic, "ISO C forbids empty initializer braces");
4633 }
4634 else
4635 {
4636 /* Parse a non-empty initializer list, possibly with a trailing
4637 comma. */
4638 while (true)
4639 {
4640 c_parser_initelt (parser, &braced_init_obstack);
4641 if (parser->error)
4642 break;
4643 if (c_parser_next_token_is (parser, CPP_COMMA))
4644 {
4645 last_init_list_comma = c_parser_peek_token (parser)->location;
4646 c_parser_consume_token (parser);
4647 }
4648 else
4649 break;
4650 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4651 break;
4652 }
4653 }
4654 c_token *next_tok = c_parser_peek_token (parser);
4655 if (next_tok->type != CPP_CLOSE_BRACE)
4656 {
4657 ret.set_error ();
4658 ret.original_code = ERROR_MARK;
4659 ret.original_type = NULL;
4660 braces.skip_until_found_close (parser);
4661 pop_init_level (brace_loc, 0, &braced_init_obstack, last_init_list_comma);
4662 obstack_free (&braced_init_obstack, NULL);
4663 return ret;
4664 }
4665 location_t close_loc = next_tok->location;
4666 c_parser_consume_token (parser);
4667 ret = pop_init_level (brace_loc, 0, &braced_init_obstack, close_loc);
4668 obstack_free (&braced_init_obstack, NULL);
4669 set_c_expr_source_range (&ret, brace_loc, close_loc);
4670 return ret;
4671 }
4672
4673 /* Parse a nested initializer, including designators. */
4674
4675 static void
4676 c_parser_initelt (c_parser *parser, struct obstack * braced_init_obstack)
4677 {
4678 /* Parse any designator or designator list. A single array
4679 designator may have the subsequent "=" omitted in GNU C, but a
4680 longer list or a structure member designator may not. */
4681 if (c_parser_next_token_is (parser, CPP_NAME)
4682 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
4683 {
4684 /* Old-style structure member designator. */
4685 set_init_label (c_parser_peek_token (parser)->location,
4686 c_parser_peek_token (parser)->value,
4687 c_parser_peek_token (parser)->location,
4688 braced_init_obstack);
4689 /* Use the colon as the error location. */
4690 pedwarn (c_parser_peek_2nd_token (parser)->location, OPT_Wpedantic,
4691 "obsolete use of designated initializer with %<:%>");
4692 c_parser_consume_token (parser);
4693 c_parser_consume_token (parser);
4694 }
4695 else
4696 {
4697 /* des_seen is 0 if there have been no designators, 1 if there
4698 has been a single array designator and 2 otherwise. */
4699 int des_seen = 0;
4700 /* Location of a designator. */
4701 location_t des_loc = UNKNOWN_LOCATION; /* Quiet warning. */
4702 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE)
4703 || c_parser_next_token_is (parser, CPP_DOT))
4704 {
4705 int des_prev = des_seen;
4706 if (!des_seen)
4707 des_loc = c_parser_peek_token (parser)->location;
4708 if (des_seen < 2)
4709 des_seen++;
4710 if (c_parser_next_token_is (parser, CPP_DOT))
4711 {
4712 des_seen = 2;
4713 c_parser_consume_token (parser);
4714 if (c_parser_next_token_is (parser, CPP_NAME))
4715 {
4716 set_init_label (des_loc, c_parser_peek_token (parser)->value,
4717 c_parser_peek_token (parser)->location,
4718 braced_init_obstack);
4719 c_parser_consume_token (parser);
4720 }
4721 else
4722 {
4723 struct c_expr init;
4724 init.set_error ();
4725 init.original_code = ERROR_MARK;
4726 init.original_type = NULL;
4727 c_parser_error (parser, "expected identifier");
4728 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
4729 process_init_element (input_location, init, false,
4730 braced_init_obstack);
4731 return;
4732 }
4733 }
4734 else
4735 {
4736 tree first, second;
4737 location_t ellipsis_loc = UNKNOWN_LOCATION; /* Quiet warning. */
4738 location_t array_index_loc = UNKNOWN_LOCATION;
4739 /* ??? Following the old parser, [ objc-receiver
4740 objc-message-args ] is accepted as an initializer,
4741 being distinguished from a designator by what follows
4742 the first assignment expression inside the square
4743 brackets, but after a first array designator a
4744 subsequent square bracket is for Objective-C taken to
4745 start an expression, using the obsolete form of
4746 designated initializer without '=', rather than
4747 possibly being a second level of designation: in LALR
4748 terms, the '[' is shifted rather than reducing
4749 designator to designator-list. */
4750 if (des_prev == 1 && c_dialect_objc ())
4751 {
4752 des_seen = des_prev;
4753 break;
4754 }
4755 if (des_prev == 0 && c_dialect_objc ())
4756 {
4757 /* This might be an array designator or an
4758 Objective-C message expression. If the former,
4759 continue parsing here; if the latter, parse the
4760 remainder of the initializer given the starting
4761 primary-expression. ??? It might make sense to
4762 distinguish when des_prev == 1 as well; see
4763 previous comment. */
4764 tree rec, args;
4765 struct c_expr mexpr;
4766 c_parser_consume_token (parser);
4767 if (c_parser_peek_token (parser)->type == CPP_NAME
4768 && ((c_parser_peek_token (parser)->id_kind
4769 == C_ID_TYPENAME)
4770 || (c_parser_peek_token (parser)->id_kind
4771 == C_ID_CLASSNAME)))
4772 {
4773 /* Type name receiver. */
4774 tree id = c_parser_peek_token (parser)->value;
4775 c_parser_consume_token (parser);
4776 rec = objc_get_class_reference (id);
4777 goto parse_message_args;
4778 }
4779 first = c_parser_expr_no_commas (parser, NULL).value;
4780 mark_exp_read (first);
4781 if (c_parser_next_token_is (parser, CPP_ELLIPSIS)
4782 || c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
4783 goto array_desig_after_first;
4784 /* Expression receiver. So far only one part
4785 without commas has been parsed; there might be
4786 more of the expression. */
4787 rec = first;
4788 while (c_parser_next_token_is (parser, CPP_COMMA))
4789 {
4790 struct c_expr next;
4791 location_t comma_loc, exp_loc;
4792 comma_loc = c_parser_peek_token (parser)->location;
4793 c_parser_consume_token (parser);
4794 exp_loc = c_parser_peek_token (parser)->location;
4795 next = c_parser_expr_no_commas (parser, NULL);
4796 next = convert_lvalue_to_rvalue (exp_loc, next,
4797 true, true);
4798 rec = build_compound_expr (comma_loc, rec, next.value);
4799 }
4800 parse_message_args:
4801 /* Now parse the objc-message-args. */
4802 args = c_parser_objc_message_args (parser);
4803 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
4804 "expected %<]%>");
4805 mexpr.value
4806 = objc_build_message_expr (rec, args);
4807 mexpr.original_code = ERROR_MARK;
4808 mexpr.original_type = NULL;
4809 /* Now parse and process the remainder of the
4810 initializer, starting with this message
4811 expression as a primary-expression. */
4812 c_parser_initval (parser, &mexpr, braced_init_obstack);
4813 return;
4814 }
4815 c_parser_consume_token (parser);
4816 array_index_loc = c_parser_peek_token (parser)->location;
4817 first = c_parser_expr_no_commas (parser, NULL).value;
4818 mark_exp_read (first);
4819 array_desig_after_first:
4820 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
4821 {
4822 ellipsis_loc = c_parser_peek_token (parser)->location;
4823 c_parser_consume_token (parser);
4824 second = c_parser_expr_no_commas (parser, NULL).value;
4825 mark_exp_read (second);
4826 }
4827 else
4828 second = NULL_TREE;
4829 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
4830 {
4831 c_parser_consume_token (parser);
4832 set_init_index (array_index_loc, first, second,
4833 braced_init_obstack);
4834 if (second)
4835 pedwarn (ellipsis_loc, OPT_Wpedantic,
4836 "ISO C forbids specifying range of elements to initialize");
4837 }
4838 else
4839 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
4840 "expected %<]%>");
4841 }
4842 }
4843 if (des_seen >= 1)
4844 {
4845 if (c_parser_next_token_is (parser, CPP_EQ))
4846 {
4847 pedwarn_c90 (des_loc, OPT_Wpedantic,
4848 "ISO C90 forbids specifying subobject "
4849 "to initialize");
4850 c_parser_consume_token (parser);
4851 }
4852 else
4853 {
4854 if (des_seen == 1)
4855 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
4856 "obsolete use of designated initializer without %<=%>");
4857 else
4858 {
4859 struct c_expr init;
4860 init.set_error ();
4861 init.original_code = ERROR_MARK;
4862 init.original_type = NULL;
4863 c_parser_error (parser, "expected %<=%>");
4864 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
4865 process_init_element (input_location, init, false,
4866 braced_init_obstack);
4867 return;
4868 }
4869 }
4870 }
4871 }
4872 c_parser_initval (parser, NULL, braced_init_obstack);
4873 }
4874
4875 /* Parse a nested initializer; as c_parser_initializer but parses
4876 initializers within braced lists, after any designators have been
4877 applied. If AFTER is not NULL then it is an Objective-C message
4878 expression which is the primary-expression starting the
4879 initializer. */
4880
4881 static void
4882 c_parser_initval (c_parser *parser, struct c_expr *after,
4883 struct obstack * braced_init_obstack)
4884 {
4885 struct c_expr init;
4886 gcc_assert (!after || c_dialect_objc ());
4887 location_t loc = c_parser_peek_token (parser)->location;
4888
4889 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE) && !after)
4890 init = c_parser_braced_init (parser, NULL_TREE, true,
4891 braced_init_obstack);
4892 else
4893 {
4894 init = c_parser_expr_no_commas (parser, after);
4895 if (init.value != NULL_TREE
4896 && TREE_CODE (init.value) != STRING_CST
4897 && TREE_CODE (init.value) != COMPOUND_LITERAL_EXPR)
4898 init = convert_lvalue_to_rvalue (loc, init, true, true);
4899 }
4900 process_init_element (loc, init, false, braced_init_obstack);
4901 }
4902
4903 /* Parse a compound statement (possibly a function body) (C90 6.6.2,
4904 C99 6.8.2, C11 6.8.2).
4905
4906 compound-statement:
4907 { block-item-list[opt] }
4908 { label-declarations block-item-list }
4909
4910 block-item-list:
4911 block-item
4912 block-item-list block-item
4913
4914 block-item:
4915 nested-declaration
4916 statement
4917
4918 nested-declaration:
4919 declaration
4920
4921 GNU extensions:
4922
4923 compound-statement:
4924 { label-declarations block-item-list }
4925
4926 nested-declaration:
4927 __extension__ nested-declaration
4928 nested-function-definition
4929
4930 label-declarations:
4931 label-declaration
4932 label-declarations label-declaration
4933
4934 label-declaration:
4935 __label__ identifier-list ;
4936
4937 Allowing the mixing of declarations and code is new in C99. The
4938 GNU syntax also permits (not shown above) labels at the end of
4939 compound statements, which yield an error. We don't allow labels
4940 on declarations; this might seem like a natural extension, but
4941 there would be a conflict between attributes on the label and
4942 prefix attributes on the declaration. ??? The syntax follows the
4943 old parser in requiring something after label declarations.
4944 Although they are erroneous if the labels declared aren't defined,
4945 is it useful for the syntax to be this way?
4946
4947 OpenACC:
4948
4949 block-item:
4950 openacc-directive
4951
4952 openacc-directive:
4953 update-directive
4954
4955 OpenMP:
4956
4957 block-item:
4958 openmp-directive
4959
4960 openmp-directive:
4961 barrier-directive
4962 flush-directive
4963 taskwait-directive
4964 taskyield-directive
4965 cancel-directive
4966 cancellation-point-directive */
4967
4968 static tree
4969 c_parser_compound_statement (c_parser *parser)
4970 {
4971 tree stmt;
4972 location_t brace_loc;
4973 brace_loc = c_parser_peek_token (parser)->location;
4974 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
4975 {
4976 /* Ensure a scope is entered and left anyway to avoid confusion
4977 if we have just prepared to enter a function body. */
4978 stmt = c_begin_compound_stmt (true);
4979 c_end_compound_stmt (brace_loc, stmt, true);
4980 return error_mark_node;
4981 }
4982 stmt = c_begin_compound_stmt (true);
4983 c_parser_compound_statement_nostart (parser);
4984
4985 return c_end_compound_stmt (brace_loc, stmt, true);
4986 }
4987
4988 /* Parse a compound statement except for the opening brace. This is
4989 used for parsing both compound statements and statement expressions
4990 (which follow different paths to handling the opening). */
4991
4992 static void
4993 c_parser_compound_statement_nostart (c_parser *parser)
4994 {
4995 bool last_stmt = false;
4996 bool last_label = false;
4997 bool save_valid_for_pragma = valid_location_for_stdc_pragma_p ();
4998 location_t label_loc = UNKNOWN_LOCATION; /* Quiet warning. */
4999 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
5000 {
5001 add_debug_begin_stmt (c_parser_peek_token (parser)->location);
5002 c_parser_consume_token (parser);
5003 return;
5004 }
5005 mark_valid_location_for_stdc_pragma (true);
5006 if (c_parser_next_token_is_keyword (parser, RID_LABEL))
5007 {
5008 /* Read zero or more forward-declarations for labels that nested
5009 functions can jump to. */
5010 mark_valid_location_for_stdc_pragma (false);
5011 while (c_parser_next_token_is_keyword (parser, RID_LABEL))
5012 {
5013 label_loc = c_parser_peek_token (parser)->location;
5014 c_parser_consume_token (parser);
5015 /* Any identifiers, including those declared as type names,
5016 are OK here. */
5017 while (true)
5018 {
5019 tree label;
5020 if (c_parser_next_token_is_not (parser, CPP_NAME))
5021 {
5022 c_parser_error (parser, "expected identifier");
5023 break;
5024 }
5025 label
5026 = declare_label (c_parser_peek_token (parser)->value);
5027 C_DECLARED_LABEL_FLAG (label) = 1;
5028 add_stmt (build_stmt (label_loc, DECL_EXPR, label));
5029 c_parser_consume_token (parser);
5030 if (c_parser_next_token_is (parser, CPP_COMMA))
5031 c_parser_consume_token (parser);
5032 else
5033 break;
5034 }
5035 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5036 }
5037 pedwarn (label_loc, OPT_Wpedantic, "ISO C forbids label declarations");
5038 }
5039 /* We must now have at least one statement, label or declaration. */
5040 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
5041 {
5042 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
5043 c_parser_error (parser, "expected declaration or statement");
5044 c_parser_consume_token (parser);
5045 return;
5046 }
5047 while (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
5048 {
5049 location_t loc = c_parser_peek_token (parser)->location;
5050 loc = expansion_point_location_if_in_system_header (loc);
5051 if (c_parser_next_token_is_keyword (parser, RID_CASE)
5052 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
5053 || (c_parser_next_token_is (parser, CPP_NAME)
5054 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
5055 {
5056 if (c_parser_next_token_is_keyword (parser, RID_CASE))
5057 label_loc = c_parser_peek_2nd_token (parser)->location;
5058 else
5059 label_loc = c_parser_peek_token (parser)->location;
5060 last_label = true;
5061 last_stmt = false;
5062 mark_valid_location_for_stdc_pragma (false);
5063 c_parser_label (parser);
5064 }
5065 else if (!last_label
5066 && c_parser_next_tokens_start_declaration (parser))
5067 {
5068 last_label = false;
5069 mark_valid_location_for_stdc_pragma (false);
5070 bool fallthru_attr_p = false;
5071 c_parser_declaration_or_fndef (parser, true, true, true, true,
5072 true, NULL, vNULL, NULL,
5073 &fallthru_attr_p);
5074 if (last_stmt && !fallthru_attr_p)
5075 pedwarn_c90 (loc, OPT_Wdeclaration_after_statement,
5076 "ISO C90 forbids mixed declarations and code");
5077 last_stmt = fallthru_attr_p;
5078 }
5079 else if (!last_label
5080 && c_parser_next_token_is_keyword (parser, RID_EXTENSION))
5081 {
5082 /* __extension__ can start a declaration, but is also an
5083 unary operator that can start an expression. Consume all
5084 but the last of a possible series of __extension__ to
5085 determine which. */
5086 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
5087 && (c_parser_peek_2nd_token (parser)->keyword
5088 == RID_EXTENSION))
5089 c_parser_consume_token (parser);
5090 if (c_token_starts_declaration (c_parser_peek_2nd_token (parser)))
5091 {
5092 int ext;
5093 ext = disable_extension_diagnostics ();
5094 c_parser_consume_token (parser);
5095 last_label = false;
5096 mark_valid_location_for_stdc_pragma (false);
5097 c_parser_declaration_or_fndef (parser, true, true, true, true,
5098 true, NULL, vNULL);
5099 /* Following the old parser, __extension__ does not
5100 disable this diagnostic. */
5101 restore_extension_diagnostics (ext);
5102 if (last_stmt)
5103 pedwarn_c90 (loc, OPT_Wdeclaration_after_statement,
5104 "ISO C90 forbids mixed declarations and code");
5105 last_stmt = false;
5106 }
5107 else
5108 goto statement;
5109 }
5110 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
5111 {
5112 /* External pragmas, and some omp pragmas, are not associated
5113 with regular c code, and so are not to be considered statements
5114 syntactically. This ensures that the user doesn't put them
5115 places that would turn into syntax errors if the directive
5116 were ignored. */
5117 if (c_parser_pragma (parser,
5118 last_label ? pragma_stmt : pragma_compound,
5119 NULL))
5120 last_label = false, last_stmt = true;
5121 }
5122 else if (c_parser_next_token_is (parser, CPP_EOF))
5123 {
5124 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
5125 c_parser_error (parser, "expected declaration or statement");
5126 return;
5127 }
5128 else if (c_parser_next_token_is_keyword (parser, RID_ELSE))
5129 {
5130 if (parser->in_if_block)
5131 {
5132 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
5133 error_at (loc, "expected %<}%> before %<else%>");
5134 return;
5135 }
5136 else
5137 {
5138 error_at (loc, "%<else%> without a previous %<if%>");
5139 c_parser_consume_token (parser);
5140 continue;
5141 }
5142 }
5143 else
5144 {
5145 statement:
5146 last_label = false;
5147 last_stmt = true;
5148 mark_valid_location_for_stdc_pragma (false);
5149 c_parser_statement_after_labels (parser, NULL);
5150 }
5151
5152 parser->error = false;
5153 }
5154 if (last_label)
5155 error_at (label_loc, "label at end of compound statement");
5156 c_parser_consume_token (parser);
5157 /* Restore the value we started with. */
5158 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
5159 }
5160
5161 /* Parse all consecutive labels. */
5162
5163 static void
5164 c_parser_all_labels (c_parser *parser)
5165 {
5166 while (c_parser_next_token_is_keyword (parser, RID_CASE)
5167 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
5168 || (c_parser_next_token_is (parser, CPP_NAME)
5169 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
5170 c_parser_label (parser);
5171 }
5172
5173 /* Parse a label (C90 6.6.1, C99 6.8.1, C11 6.8.1).
5174
5175 label:
5176 identifier : attributes[opt]
5177 case constant-expression :
5178 default :
5179
5180 GNU extensions:
5181
5182 label:
5183 case constant-expression ... constant-expression :
5184
5185 The use of attributes on labels is a GNU extension. The syntax in
5186 GNU C accepts any expressions without commas, non-constant
5187 expressions being rejected later. */
5188
5189 static void
5190 c_parser_label (c_parser *parser)
5191 {
5192 location_t loc1 = c_parser_peek_token (parser)->location;
5193 tree label = NULL_TREE;
5194
5195 /* Remember whether this case or a user-defined label is allowed to fall
5196 through to. */
5197 bool fallthrough_p = c_parser_peek_token (parser)->flags & PREV_FALLTHROUGH;
5198
5199 if (c_parser_next_token_is_keyword (parser, RID_CASE))
5200 {
5201 tree exp1, exp2;
5202 c_parser_consume_token (parser);
5203 exp1 = c_parser_expr_no_commas (parser, NULL).value;
5204 if (c_parser_next_token_is (parser, CPP_COLON))
5205 {
5206 c_parser_consume_token (parser);
5207 label = do_case (loc1, exp1, NULL_TREE);
5208 }
5209 else if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
5210 {
5211 c_parser_consume_token (parser);
5212 exp2 = c_parser_expr_no_commas (parser, NULL).value;
5213 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
5214 label = do_case (loc1, exp1, exp2);
5215 }
5216 else
5217 c_parser_error (parser, "expected %<:%> or %<...%>");
5218 }
5219 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
5220 {
5221 c_parser_consume_token (parser);
5222 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
5223 label = do_case (loc1, NULL_TREE, NULL_TREE);
5224 }
5225 else
5226 {
5227 tree name = c_parser_peek_token (parser)->value;
5228 tree tlab;
5229 tree attrs;
5230 location_t loc2 = c_parser_peek_token (parser)->location;
5231 gcc_assert (c_parser_next_token_is (parser, CPP_NAME));
5232 c_parser_consume_token (parser);
5233 gcc_assert (c_parser_next_token_is (parser, CPP_COLON));
5234 c_parser_consume_token (parser);
5235 attrs = c_parser_attributes (parser);
5236 tlab = define_label (loc2, name);
5237 if (tlab)
5238 {
5239 decl_attributes (&tlab, attrs, 0);
5240 label = add_stmt (build_stmt (loc1, LABEL_EXPR, tlab));
5241 }
5242 }
5243 if (label)
5244 {
5245 if (TREE_CODE (label) == LABEL_EXPR)
5246 FALLTHROUGH_LABEL_P (LABEL_EXPR_LABEL (label)) = fallthrough_p;
5247 else
5248 FALLTHROUGH_LABEL_P (CASE_LABEL (label)) = fallthrough_p;
5249
5250 /* Allow '__attribute__((fallthrough));'. */
5251 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
5252 {
5253 location_t loc = c_parser_peek_token (parser)->location;
5254 tree attrs = c_parser_attributes (parser);
5255 if (attribute_fallthrough_p (attrs))
5256 {
5257 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5258 {
5259 tree fn = build_call_expr_internal_loc (loc,
5260 IFN_FALLTHROUGH,
5261 void_type_node, 0);
5262 add_stmt (fn);
5263 }
5264 else
5265 warning_at (loc, OPT_Wattributes, "%<fallthrough%> attribute "
5266 "not followed by %<;%>");
5267 }
5268 else if (attrs != NULL_TREE)
5269 warning_at (loc, OPT_Wattributes, "only attribute %<fallthrough%>"
5270 " can be applied to a null statement");
5271 }
5272 if (c_parser_next_tokens_start_declaration (parser))
5273 {
5274 error_at (c_parser_peek_token (parser)->location,
5275 "a label can only be part of a statement and "
5276 "a declaration is not a statement");
5277 c_parser_declaration_or_fndef (parser, /*fndef_ok*/ false,
5278 /*static_assert_ok*/ true,
5279 /*empty_ok*/ true, /*nested*/ true,
5280 /*start_attr_ok*/ true, NULL,
5281 vNULL);
5282 }
5283 }
5284 }
5285
5286 /* Parse a statement (C90 6.6, C99 6.8, C11 6.8).
5287
5288 statement:
5289 labeled-statement
5290 compound-statement
5291 expression-statement
5292 selection-statement
5293 iteration-statement
5294 jump-statement
5295
5296 labeled-statement:
5297 label statement
5298
5299 expression-statement:
5300 expression[opt] ;
5301
5302 selection-statement:
5303 if-statement
5304 switch-statement
5305
5306 iteration-statement:
5307 while-statement
5308 do-statement
5309 for-statement
5310
5311 jump-statement:
5312 goto identifier ;
5313 continue ;
5314 break ;
5315 return expression[opt] ;
5316
5317 GNU extensions:
5318
5319 statement:
5320 asm-statement
5321
5322 jump-statement:
5323 goto * expression ;
5324
5325 expression-statement:
5326 attributes ;
5327
5328 Objective-C:
5329
5330 statement:
5331 objc-throw-statement
5332 objc-try-catch-statement
5333 objc-synchronized-statement
5334
5335 objc-throw-statement:
5336 @throw expression ;
5337 @throw ;
5338
5339 OpenACC:
5340
5341 statement:
5342 openacc-construct
5343
5344 openacc-construct:
5345 parallel-construct
5346 kernels-construct
5347 data-construct
5348 loop-construct
5349
5350 parallel-construct:
5351 parallel-directive structured-block
5352
5353 kernels-construct:
5354 kernels-directive structured-block
5355
5356 data-construct:
5357 data-directive structured-block
5358
5359 loop-construct:
5360 loop-directive structured-block
5361
5362 OpenMP:
5363
5364 statement:
5365 openmp-construct
5366
5367 openmp-construct:
5368 parallel-construct
5369 for-construct
5370 simd-construct
5371 for-simd-construct
5372 sections-construct
5373 single-construct
5374 parallel-for-construct
5375 parallel-for-simd-construct
5376 parallel-sections-construct
5377 master-construct
5378 critical-construct
5379 atomic-construct
5380 ordered-construct
5381
5382 parallel-construct:
5383 parallel-directive structured-block
5384
5385 for-construct:
5386 for-directive iteration-statement
5387
5388 simd-construct:
5389 simd-directive iteration-statements
5390
5391 for-simd-construct:
5392 for-simd-directive iteration-statements
5393
5394 sections-construct:
5395 sections-directive section-scope
5396
5397 single-construct:
5398 single-directive structured-block
5399
5400 parallel-for-construct:
5401 parallel-for-directive iteration-statement
5402
5403 parallel-for-simd-construct:
5404 parallel-for-simd-directive iteration-statement
5405
5406 parallel-sections-construct:
5407 parallel-sections-directive section-scope
5408
5409 master-construct:
5410 master-directive structured-block
5411
5412 critical-construct:
5413 critical-directive structured-block
5414
5415 atomic-construct:
5416 atomic-directive expression-statement
5417
5418 ordered-construct:
5419 ordered-directive structured-block
5420
5421 Transactional Memory:
5422
5423 statement:
5424 transaction-statement
5425 transaction-cancel-statement
5426
5427 IF_P is used to track whether there's a (possibly labeled) if statement
5428 which is not enclosed in braces and has an else clause. This is used to
5429 implement -Wparentheses. */
5430
5431 static void
5432 c_parser_statement (c_parser *parser, bool *if_p, location_t *loc_after_labels)
5433 {
5434 c_parser_all_labels (parser);
5435 if (loc_after_labels)
5436 *loc_after_labels = c_parser_peek_token (parser)->location;
5437 c_parser_statement_after_labels (parser, if_p, NULL);
5438 }
5439
5440 /* Parse a statement, other than a labeled statement. CHAIN is a vector
5441 of if-else-if conditions.
5442
5443 IF_P is used to track whether there's a (possibly labeled) if statement
5444 which is not enclosed in braces and has an else clause. This is used to
5445 implement -Wparentheses. */
5446
5447 static void
5448 c_parser_statement_after_labels (c_parser *parser, bool *if_p,
5449 vec<tree> *chain)
5450 {
5451 location_t loc = c_parser_peek_token (parser)->location;
5452 tree stmt = NULL_TREE;
5453 bool in_if_block = parser->in_if_block;
5454 parser->in_if_block = false;
5455 if (if_p != NULL)
5456 *if_p = false;
5457
5458 if (c_parser_peek_token (parser)->type != CPP_OPEN_BRACE)
5459 add_debug_begin_stmt (loc);
5460
5461 switch (c_parser_peek_token (parser)->type)
5462 {
5463 case CPP_OPEN_BRACE:
5464 add_stmt (c_parser_compound_statement (parser));
5465 break;
5466 case CPP_KEYWORD:
5467 switch (c_parser_peek_token (parser)->keyword)
5468 {
5469 case RID_IF:
5470 c_parser_if_statement (parser, if_p, chain);
5471 break;
5472 case RID_SWITCH:
5473 c_parser_switch_statement (parser, if_p);
5474 break;
5475 case RID_WHILE:
5476 c_parser_while_statement (parser, false, 0, if_p);
5477 break;
5478 case RID_DO:
5479 c_parser_do_statement (parser, 0, false);
5480 break;
5481 case RID_FOR:
5482 c_parser_for_statement (parser, false, 0, if_p);
5483 break;
5484 case RID_GOTO:
5485 c_parser_consume_token (parser);
5486 if (c_parser_next_token_is (parser, CPP_NAME))
5487 {
5488 stmt = c_finish_goto_label (loc,
5489 c_parser_peek_token (parser)->value);
5490 c_parser_consume_token (parser);
5491 }
5492 else if (c_parser_next_token_is (parser, CPP_MULT))
5493 {
5494 struct c_expr val;
5495
5496 c_parser_consume_token (parser);
5497 val = c_parser_expression (parser);
5498 val = convert_lvalue_to_rvalue (loc, val, false, true);
5499 stmt = c_finish_goto_ptr (loc, val.value);
5500 }
5501 else
5502 c_parser_error (parser, "expected identifier or %<*%>");
5503 goto expect_semicolon;
5504 case RID_CONTINUE:
5505 c_parser_consume_token (parser);
5506 stmt = c_finish_bc_stmt (loc, &c_cont_label, false);
5507 goto expect_semicolon;
5508 case RID_BREAK:
5509 c_parser_consume_token (parser);
5510 stmt = c_finish_bc_stmt (loc, &c_break_label, true);
5511 goto expect_semicolon;
5512 case RID_RETURN:
5513 c_parser_consume_token (parser);
5514 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5515 {
5516 stmt = c_finish_return (loc, NULL_TREE, NULL_TREE);
5517 c_parser_consume_token (parser);
5518 }
5519 else
5520 {
5521 location_t xloc = c_parser_peek_token (parser)->location;
5522 struct c_expr expr = c_parser_expression_conv (parser);
5523 mark_exp_read (expr.value);
5524 stmt = c_finish_return (EXPR_LOC_OR_LOC (expr.value, xloc),
5525 expr.value, expr.original_type);
5526 goto expect_semicolon;
5527 }
5528 break;
5529 case RID_ASM:
5530 stmt = c_parser_asm_statement (parser);
5531 break;
5532 case RID_TRANSACTION_ATOMIC:
5533 case RID_TRANSACTION_RELAXED:
5534 stmt = c_parser_transaction (parser,
5535 c_parser_peek_token (parser)->keyword);
5536 break;
5537 case RID_TRANSACTION_CANCEL:
5538 stmt = c_parser_transaction_cancel (parser);
5539 goto expect_semicolon;
5540 case RID_AT_THROW:
5541 gcc_assert (c_dialect_objc ());
5542 c_parser_consume_token (parser);
5543 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5544 {
5545 stmt = objc_build_throw_stmt (loc, NULL_TREE);
5546 c_parser_consume_token (parser);
5547 }
5548 else
5549 {
5550 struct c_expr expr = c_parser_expression (parser);
5551 expr = convert_lvalue_to_rvalue (loc, expr, false, false);
5552 expr.value = c_fully_fold (expr.value, false, NULL);
5553 stmt = objc_build_throw_stmt (loc, expr.value);
5554 goto expect_semicolon;
5555 }
5556 break;
5557 case RID_AT_TRY:
5558 gcc_assert (c_dialect_objc ());
5559 c_parser_objc_try_catch_finally_statement (parser);
5560 break;
5561 case RID_AT_SYNCHRONIZED:
5562 gcc_assert (c_dialect_objc ());
5563 c_parser_objc_synchronized_statement (parser);
5564 break;
5565 case RID_ATTRIBUTE:
5566 {
5567 /* Allow '__attribute__((fallthrough));'. */
5568 tree attrs = c_parser_attributes (parser);
5569 if (attribute_fallthrough_p (attrs))
5570 {
5571 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5572 {
5573 tree fn = build_call_expr_internal_loc (loc,
5574 IFN_FALLTHROUGH,
5575 void_type_node, 0);
5576 add_stmt (fn);
5577 /* Eat the ';'. */
5578 c_parser_consume_token (parser);
5579 }
5580 else
5581 warning_at (loc, OPT_Wattributes,
5582 "%<fallthrough%> attribute not followed "
5583 "by %<;%>");
5584 }
5585 else if (attrs != NULL_TREE)
5586 warning_at (loc, OPT_Wattributes, "only attribute %<fallthrough%>"
5587 " can be applied to a null statement");
5588 break;
5589 }
5590 default:
5591 goto expr_stmt;
5592 }
5593 break;
5594 case CPP_SEMICOLON:
5595 c_parser_consume_token (parser);
5596 break;
5597 case CPP_CLOSE_PAREN:
5598 case CPP_CLOSE_SQUARE:
5599 /* Avoid infinite loop in error recovery:
5600 c_parser_skip_until_found stops at a closing nesting
5601 delimiter without consuming it, but here we need to consume
5602 it to proceed further. */
5603 c_parser_error (parser, "expected statement");
5604 c_parser_consume_token (parser);
5605 break;
5606 case CPP_PRAGMA:
5607 c_parser_pragma (parser, pragma_stmt, if_p);
5608 break;
5609 default:
5610 expr_stmt:
5611 stmt = c_finish_expr_stmt (loc, c_parser_expression_conv (parser).value);
5612 expect_semicolon:
5613 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5614 break;
5615 }
5616 /* Two cases cannot and do not have line numbers associated: If stmt
5617 is degenerate, such as "2;", then stmt is an INTEGER_CST, which
5618 cannot hold line numbers. But that's OK because the statement
5619 will either be changed to a MODIFY_EXPR during gimplification of
5620 the statement expr, or discarded. If stmt was compound, but
5621 without new variables, we will have skipped the creation of a
5622 BIND and will have a bare STATEMENT_LIST. But that's OK because
5623 (recursively) all of the component statements should already have
5624 line numbers assigned. ??? Can we discard no-op statements
5625 earlier? */
5626 if (EXPR_LOCATION (stmt) == UNKNOWN_LOCATION)
5627 protected_set_expr_location (stmt, loc);
5628
5629 parser->in_if_block = in_if_block;
5630 }
5631
5632 /* Parse the condition from an if, do, while or for statements. */
5633
5634 static tree
5635 c_parser_condition (c_parser *parser)
5636 {
5637 location_t loc = c_parser_peek_token (parser)->location;
5638 tree cond;
5639 cond = c_parser_expression_conv (parser).value;
5640 cond = c_objc_common_truthvalue_conversion (loc, cond);
5641 cond = c_fully_fold (cond, false, NULL);
5642 if (warn_sequence_point)
5643 verify_sequence_points (cond);
5644 return cond;
5645 }
5646
5647 /* Parse a parenthesized condition from an if, do or while statement.
5648
5649 condition:
5650 ( expression )
5651 */
5652 static tree
5653 c_parser_paren_condition (c_parser *parser)
5654 {
5655 tree cond;
5656 matching_parens parens;
5657 if (!parens.require_open (parser))
5658 return error_mark_node;
5659 cond = c_parser_condition (parser);
5660 parens.skip_until_found_close (parser);
5661 return cond;
5662 }
5663
5664 /* Parse a statement which is a block in C99.
5665
5666 IF_P is used to track whether there's a (possibly labeled) if statement
5667 which is not enclosed in braces and has an else clause. This is used to
5668 implement -Wparentheses. */
5669
5670 static tree
5671 c_parser_c99_block_statement (c_parser *parser, bool *if_p,
5672 location_t *loc_after_labels)
5673 {
5674 tree block = c_begin_compound_stmt (flag_isoc99);
5675 location_t loc = c_parser_peek_token (parser)->location;
5676 c_parser_statement (parser, if_p, loc_after_labels);
5677 return c_end_compound_stmt (loc, block, flag_isoc99);
5678 }
5679
5680 /* Parse the body of an if statement. This is just parsing a
5681 statement but (a) it is a block in C99, (b) we track whether the
5682 body is an if statement for the sake of -Wparentheses warnings, (c)
5683 we handle an empty body specially for the sake of -Wempty-body
5684 warnings, and (d) we call parser_compound_statement directly
5685 because c_parser_statement_after_labels resets
5686 parser->in_if_block.
5687
5688 IF_P is used to track whether there's a (possibly labeled) if statement
5689 which is not enclosed in braces and has an else clause. This is used to
5690 implement -Wparentheses. */
5691
5692 static tree
5693 c_parser_if_body (c_parser *parser, bool *if_p,
5694 const token_indent_info &if_tinfo)
5695 {
5696 tree block = c_begin_compound_stmt (flag_isoc99);
5697 location_t body_loc = c_parser_peek_token (parser)->location;
5698 location_t body_loc_after_labels = UNKNOWN_LOCATION;
5699 token_indent_info body_tinfo
5700 = get_token_indent_info (c_parser_peek_token (parser));
5701
5702 c_parser_all_labels (parser);
5703 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5704 {
5705 location_t loc = c_parser_peek_token (parser)->location;
5706 add_stmt (build_empty_stmt (loc));
5707 c_parser_consume_token (parser);
5708 if (!c_parser_next_token_is_keyword (parser, RID_ELSE))
5709 warning_at (loc, OPT_Wempty_body,
5710 "suggest braces around empty body in an %<if%> statement");
5711 }
5712 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5713 add_stmt (c_parser_compound_statement (parser));
5714 else
5715 {
5716 body_loc_after_labels = c_parser_peek_token (parser)->location;
5717 c_parser_statement_after_labels (parser, if_p);
5718 }
5719
5720 token_indent_info next_tinfo
5721 = get_token_indent_info (c_parser_peek_token (parser));
5722 warn_for_misleading_indentation (if_tinfo, body_tinfo, next_tinfo);
5723 if (body_loc_after_labels != UNKNOWN_LOCATION
5724 && next_tinfo.type != CPP_SEMICOLON)
5725 warn_for_multistatement_macros (body_loc_after_labels, next_tinfo.location,
5726 if_tinfo.location, RID_IF);
5727
5728 return c_end_compound_stmt (body_loc, block, flag_isoc99);
5729 }
5730
5731 /* Parse the else body of an if statement. This is just parsing a
5732 statement but (a) it is a block in C99, (b) we handle an empty body
5733 specially for the sake of -Wempty-body warnings. CHAIN is a vector
5734 of if-else-if conditions. */
5735
5736 static tree
5737 c_parser_else_body (c_parser *parser, const token_indent_info &else_tinfo,
5738 vec<tree> *chain)
5739 {
5740 location_t body_loc = c_parser_peek_token (parser)->location;
5741 tree block = c_begin_compound_stmt (flag_isoc99);
5742 token_indent_info body_tinfo
5743 = get_token_indent_info (c_parser_peek_token (parser));
5744 location_t body_loc_after_labels = UNKNOWN_LOCATION;
5745
5746 c_parser_all_labels (parser);
5747 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5748 {
5749 location_t loc = c_parser_peek_token (parser)->location;
5750 warning_at (loc,
5751 OPT_Wempty_body,
5752 "suggest braces around empty body in an %<else%> statement");
5753 add_stmt (build_empty_stmt (loc));
5754 c_parser_consume_token (parser);
5755 }
5756 else
5757 {
5758 if (!c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5759 body_loc_after_labels = c_parser_peek_token (parser)->location;
5760 c_parser_statement_after_labels (parser, NULL, chain);
5761 }
5762
5763 token_indent_info next_tinfo
5764 = get_token_indent_info (c_parser_peek_token (parser));
5765 warn_for_misleading_indentation (else_tinfo, body_tinfo, next_tinfo);
5766 if (body_loc_after_labels != UNKNOWN_LOCATION
5767 && next_tinfo.type != CPP_SEMICOLON)
5768 warn_for_multistatement_macros (body_loc_after_labels, next_tinfo.location,
5769 else_tinfo.location, RID_ELSE);
5770
5771 return c_end_compound_stmt (body_loc, block, flag_isoc99);
5772 }
5773
5774 /* We might need to reclassify any previously-lexed identifier, e.g.
5775 when we've left a for loop with an if-statement without else in the
5776 body - we might have used a wrong scope for the token. See PR67784. */
5777
5778 static void
5779 c_parser_maybe_reclassify_token (c_parser *parser)
5780 {
5781 if (c_parser_next_token_is (parser, CPP_NAME))
5782 {
5783 c_token *token = c_parser_peek_token (parser);
5784
5785 if (token->id_kind != C_ID_CLASSNAME)
5786 {
5787 tree decl = lookup_name (token->value);
5788
5789 token->id_kind = C_ID_ID;
5790 if (decl)
5791 {
5792 if (TREE_CODE (decl) == TYPE_DECL)
5793 token->id_kind = C_ID_TYPENAME;
5794 }
5795 else if (c_dialect_objc ())
5796 {
5797 tree objc_interface_decl = objc_is_class_name (token->value);
5798 /* Objective-C class names are in the same namespace as
5799 variables and typedefs, and hence are shadowed by local
5800 declarations. */
5801 if (objc_interface_decl)
5802 {
5803 token->value = objc_interface_decl;
5804 token->id_kind = C_ID_CLASSNAME;
5805 }
5806 }
5807 }
5808 }
5809 }
5810
5811 /* Parse an if statement (C90 6.6.4, C99 6.8.4, C11 6.8.4).
5812
5813 if-statement:
5814 if ( expression ) statement
5815 if ( expression ) statement else statement
5816
5817 CHAIN is a vector of if-else-if conditions.
5818 IF_P is used to track whether there's a (possibly labeled) if statement
5819 which is not enclosed in braces and has an else clause. This is used to
5820 implement -Wparentheses. */
5821
5822 static void
5823 c_parser_if_statement (c_parser *parser, bool *if_p, vec<tree> *chain)
5824 {
5825 tree block;
5826 location_t loc;
5827 tree cond;
5828 bool nested_if = false;
5829 tree first_body, second_body;
5830 bool in_if_block;
5831
5832 gcc_assert (c_parser_next_token_is_keyword (parser, RID_IF));
5833 token_indent_info if_tinfo
5834 = get_token_indent_info (c_parser_peek_token (parser));
5835 c_parser_consume_token (parser);
5836 block = c_begin_compound_stmt (flag_isoc99);
5837 loc = c_parser_peek_token (parser)->location;
5838 cond = c_parser_paren_condition (parser);
5839 in_if_block = parser->in_if_block;
5840 parser->in_if_block = true;
5841 first_body = c_parser_if_body (parser, &nested_if, if_tinfo);
5842 parser->in_if_block = in_if_block;
5843
5844 if (warn_duplicated_cond)
5845 warn_duplicated_cond_add_or_warn (EXPR_LOCATION (cond), cond, &chain);
5846
5847 if (c_parser_next_token_is_keyword (parser, RID_ELSE))
5848 {
5849 token_indent_info else_tinfo
5850 = get_token_indent_info (c_parser_peek_token (parser));
5851 c_parser_consume_token (parser);
5852 if (warn_duplicated_cond)
5853 {
5854 if (c_parser_next_token_is_keyword (parser, RID_IF)
5855 && chain == NULL)
5856 {
5857 /* We've got "if (COND) else if (COND2)". Start the
5858 condition chain and add COND as the first element. */
5859 chain = new vec<tree> ();
5860 if (!CONSTANT_CLASS_P (cond) && !TREE_SIDE_EFFECTS (cond))
5861 chain->safe_push (cond);
5862 }
5863 else if (!c_parser_next_token_is_keyword (parser, RID_IF))
5864 {
5865 /* This is if-else without subsequent if. Zap the condition
5866 chain; we would have already warned at this point. */
5867 delete chain;
5868 chain = NULL;
5869 }
5870 }
5871 second_body = c_parser_else_body (parser, else_tinfo, chain);
5872 /* Set IF_P to true to indicate that this if statement has an
5873 else clause. This may trigger the Wparentheses warning
5874 below when we get back up to the parent if statement. */
5875 if (if_p != NULL)
5876 *if_p = true;
5877 }
5878 else
5879 {
5880 second_body = NULL_TREE;
5881
5882 /* Diagnose an ambiguous else if if-then-else is nested inside
5883 if-then. */
5884 if (nested_if)
5885 warning_at (loc, OPT_Wdangling_else,
5886 "suggest explicit braces to avoid ambiguous %<else%>");
5887
5888 if (warn_duplicated_cond)
5889 {
5890 /* This if statement does not have an else clause. We don't
5891 need the condition chain anymore. */
5892 delete chain;
5893 chain = NULL;
5894 }
5895 }
5896 c_finish_if_stmt (loc, cond, first_body, second_body);
5897 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
5898
5899 c_parser_maybe_reclassify_token (parser);
5900 }
5901
5902 /* Parse a switch statement (C90 6.6.4, C99 6.8.4, C11 6.8.4).
5903
5904 switch-statement:
5905 switch (expression) statement
5906 */
5907
5908 static void
5909 c_parser_switch_statement (c_parser *parser, bool *if_p)
5910 {
5911 struct c_expr ce;
5912 tree block, expr, body, save_break;
5913 location_t switch_loc = c_parser_peek_token (parser)->location;
5914 location_t switch_cond_loc;
5915 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SWITCH));
5916 c_parser_consume_token (parser);
5917 block = c_begin_compound_stmt (flag_isoc99);
5918 bool explicit_cast_p = false;
5919 matching_parens parens;
5920 if (parens.require_open (parser))
5921 {
5922 switch_cond_loc = c_parser_peek_token (parser)->location;
5923 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
5924 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
5925 explicit_cast_p = true;
5926 ce = c_parser_expression (parser);
5927 ce = convert_lvalue_to_rvalue (switch_cond_loc, ce, true, false);
5928 expr = ce.value;
5929 /* ??? expr has no valid location? */
5930 parens.skip_until_found_close (parser);
5931 }
5932 else
5933 {
5934 switch_cond_loc = UNKNOWN_LOCATION;
5935 expr = error_mark_node;
5936 ce.original_type = error_mark_node;
5937 }
5938 c_start_case (switch_loc, switch_cond_loc, expr, explicit_cast_p);
5939 save_break = c_break_label;
5940 c_break_label = NULL_TREE;
5941 location_t loc_after_labels;
5942 bool open_brace_p = c_parser_peek_token (parser)->type == CPP_OPEN_BRACE;
5943 body = c_parser_c99_block_statement (parser, if_p, &loc_after_labels);
5944 location_t next_loc = c_parser_peek_token (parser)->location;
5945 if (!open_brace_p && c_parser_peek_token (parser)->type != CPP_SEMICOLON)
5946 warn_for_multistatement_macros (loc_after_labels, next_loc, switch_loc,
5947 RID_SWITCH);
5948 if (c_break_label)
5949 {
5950 location_t here = c_parser_peek_token (parser)->location;
5951 tree t = build1 (LABEL_EXPR, void_type_node, c_break_label);
5952 SET_EXPR_LOCATION (t, here);
5953 SWITCH_BREAK_LABEL_P (c_break_label) = 1;
5954 append_to_statement_list_force (t, &body);
5955 }
5956 c_finish_case (body, ce.original_type);
5957 c_break_label = save_break;
5958 add_stmt (c_end_compound_stmt (switch_loc, block, flag_isoc99));
5959 c_parser_maybe_reclassify_token (parser);
5960 }
5961
5962 /* Parse a while statement (C90 6.6.5, C99 6.8.5, C11 6.8.5).
5963
5964 while-statement:
5965 while (expression) statement
5966
5967 IF_P is used to track whether there's a (possibly labeled) if statement
5968 which is not enclosed in braces and has an else clause. This is used to
5969 implement -Wparentheses. */
5970
5971 static void
5972 c_parser_while_statement (c_parser *parser, bool ivdep, unsigned short unroll,
5973 bool *if_p)
5974 {
5975 tree block, cond, body, save_break, save_cont;
5976 location_t loc;
5977 gcc_assert (c_parser_next_token_is_keyword (parser, RID_WHILE));
5978 token_indent_info while_tinfo
5979 = get_token_indent_info (c_parser_peek_token (parser));
5980 c_parser_consume_token (parser);
5981 block = c_begin_compound_stmt (flag_isoc99);
5982 loc = c_parser_peek_token (parser)->location;
5983 cond = c_parser_paren_condition (parser);
5984 if (ivdep && cond != error_mark_node)
5985 cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
5986 build_int_cst (integer_type_node,
5987 annot_expr_ivdep_kind),
5988 integer_zero_node);
5989 if (unroll && cond != error_mark_node)
5990 cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
5991 build_int_cst (integer_type_node,
5992 annot_expr_unroll_kind),
5993 build_int_cst (integer_type_node, unroll));
5994 save_break = c_break_label;
5995 c_break_label = NULL_TREE;
5996 save_cont = c_cont_label;
5997 c_cont_label = NULL_TREE;
5998
5999 token_indent_info body_tinfo
6000 = get_token_indent_info (c_parser_peek_token (parser));
6001
6002 location_t loc_after_labels;
6003 bool open_brace = c_parser_next_token_is (parser, CPP_OPEN_BRACE);
6004 body = c_parser_c99_block_statement (parser, if_p, &loc_after_labels);
6005 c_finish_loop (loc, loc, cond, UNKNOWN_LOCATION, NULL, body,
6006 c_break_label, c_cont_label, true);
6007 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
6008 c_parser_maybe_reclassify_token (parser);
6009
6010 token_indent_info next_tinfo
6011 = get_token_indent_info (c_parser_peek_token (parser));
6012 warn_for_misleading_indentation (while_tinfo, body_tinfo, next_tinfo);
6013
6014 if (next_tinfo.type != CPP_SEMICOLON && !open_brace)
6015 warn_for_multistatement_macros (loc_after_labels, next_tinfo.location,
6016 while_tinfo.location, RID_WHILE);
6017
6018 c_break_label = save_break;
6019 c_cont_label = save_cont;
6020 }
6021
6022 /* Parse a do statement (C90 6.6.5, C99 6.8.5, C11 6.8.5).
6023
6024 do-statement:
6025 do statement while ( expression ) ;
6026 */
6027
6028 static void
6029 c_parser_do_statement (c_parser *parser, bool ivdep, unsigned short unroll)
6030 {
6031 tree block, cond, body, save_break, save_cont, new_break, new_cont;
6032 location_t loc;
6033 gcc_assert (c_parser_next_token_is_keyword (parser, RID_DO));
6034 c_parser_consume_token (parser);
6035 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
6036 warning_at (c_parser_peek_token (parser)->location,
6037 OPT_Wempty_body,
6038 "suggest braces around empty body in %<do%> statement");
6039 block = c_begin_compound_stmt (flag_isoc99);
6040 loc = c_parser_peek_token (parser)->location;
6041 save_break = c_break_label;
6042 c_break_label = NULL_TREE;
6043 save_cont = c_cont_label;
6044 c_cont_label = NULL_TREE;
6045 body = c_parser_c99_block_statement (parser, NULL);
6046 c_parser_require_keyword (parser, RID_WHILE, "expected %<while%>");
6047 new_break = c_break_label;
6048 c_break_label = save_break;
6049 new_cont = c_cont_label;
6050 c_cont_label = save_cont;
6051 location_t cond_loc = c_parser_peek_token (parser)->location;
6052 cond = c_parser_paren_condition (parser);
6053 if (ivdep && cond != error_mark_node)
6054 cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
6055 build_int_cst (integer_type_node,
6056 annot_expr_ivdep_kind),
6057 integer_zero_node);
6058 if (unroll && cond != error_mark_node)
6059 cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
6060 build_int_cst (integer_type_node,
6061 annot_expr_unroll_kind),
6062 build_int_cst (integer_type_node, unroll));
6063 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
6064 c_parser_skip_to_end_of_block_or_statement (parser);
6065 c_finish_loop (loc, cond_loc, cond, UNKNOWN_LOCATION, NULL, body,
6066 new_break, new_cont, false);
6067 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
6068 }
6069
6070 /* Parse a for statement (C90 6.6.5, C99 6.8.5, C11 6.8.5).
6071
6072 for-statement:
6073 for ( expression[opt] ; expression[opt] ; expression[opt] ) statement
6074 for ( nested-declaration expression[opt] ; expression[opt] ) statement
6075
6076 The form with a declaration is new in C99.
6077
6078 ??? In accordance with the old parser, the declaration may be a
6079 nested function, which is then rejected in check_for_loop_decls,
6080 but does it make any sense for this to be included in the grammar?
6081 Note in particular that the nested function does not include a
6082 trailing ';', whereas the "declaration" production includes one.
6083 Also, can we reject bad declarations earlier and cheaper than
6084 check_for_loop_decls?
6085
6086 In Objective-C, there are two additional variants:
6087
6088 foreach-statement:
6089 for ( expression in expresssion ) statement
6090 for ( declaration in expression ) statement
6091
6092 This is inconsistent with C, because the second variant is allowed
6093 even if c99 is not enabled.
6094
6095 The rest of the comment documents these Objective-C foreach-statement.
6096
6097 Here is the canonical example of the first variant:
6098 for (object in array) { do something with object }
6099 we call the first expression ("object") the "object_expression" and
6100 the second expression ("array") the "collection_expression".
6101 object_expression must be an lvalue of type "id" (a generic Objective-C
6102 object) because the loop works by assigning to object_expression the
6103 various objects from the collection_expression. collection_expression
6104 must evaluate to something of type "id" which responds to the method
6105 countByEnumeratingWithState:objects:count:.
6106
6107 The canonical example of the second variant is:
6108 for (id object in array) { do something with object }
6109 which is completely equivalent to
6110 {
6111 id object;
6112 for (object in array) { do something with object }
6113 }
6114 Note that initizializing 'object' in some way (eg, "for ((object =
6115 xxx) in array) { do something with object }") is possibly
6116 technically valid, but completely pointless as 'object' will be
6117 assigned to something else as soon as the loop starts. We should
6118 most likely reject it (TODO).
6119
6120 The beginning of the Objective-C foreach-statement looks exactly
6121 like the beginning of the for-statement, and we can tell it is a
6122 foreach-statement only because the initial declaration or
6123 expression is terminated by 'in' instead of ';'.
6124
6125 IF_P is used to track whether there's a (possibly labeled) if statement
6126 which is not enclosed in braces and has an else clause. This is used to
6127 implement -Wparentheses. */
6128
6129 static void
6130 c_parser_for_statement (c_parser *parser, bool ivdep, unsigned short unroll,
6131 bool *if_p)
6132 {
6133 tree block, cond, incr, save_break, save_cont, body;
6134 /* The following are only used when parsing an ObjC foreach statement. */
6135 tree object_expression;
6136 /* Silence the bogus uninitialized warning. */
6137 tree collection_expression = NULL;
6138 location_t loc = c_parser_peek_token (parser)->location;
6139 location_t for_loc = loc;
6140 location_t cond_loc = UNKNOWN_LOCATION;
6141 location_t incr_loc = UNKNOWN_LOCATION;
6142 bool is_foreach_statement = false;
6143 gcc_assert (c_parser_next_token_is_keyword (parser, RID_FOR));
6144 token_indent_info for_tinfo
6145 = get_token_indent_info (c_parser_peek_token (parser));
6146 c_parser_consume_token (parser);
6147 /* Open a compound statement in Objective-C as well, just in case this is
6148 as foreach expression. */
6149 block = c_begin_compound_stmt (flag_isoc99 || c_dialect_objc ());
6150 cond = error_mark_node;
6151 incr = error_mark_node;
6152 matching_parens parens;
6153 if (parens.require_open (parser))
6154 {
6155 /* Parse the initialization declaration or expression. */
6156 object_expression = error_mark_node;
6157 parser->objc_could_be_foreach_context = c_dialect_objc ();
6158 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
6159 {
6160 parser->objc_could_be_foreach_context = false;
6161 c_parser_consume_token (parser);
6162 c_finish_expr_stmt (loc, NULL_TREE);
6163 }
6164 else if (c_parser_next_tokens_start_declaration (parser))
6165 {
6166 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
6167 &object_expression, vNULL);
6168 parser->objc_could_be_foreach_context = false;
6169
6170 if (c_parser_next_token_is_keyword (parser, RID_IN))
6171 {
6172 c_parser_consume_token (parser);
6173 is_foreach_statement = true;
6174 if (check_for_loop_decls (for_loc, true) == NULL_TREE)
6175 c_parser_error (parser, "multiple iterating variables in "
6176 "fast enumeration");
6177 }
6178 else
6179 check_for_loop_decls (for_loc, flag_isoc99);
6180 }
6181 else if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
6182 {
6183 /* __extension__ can start a declaration, but is also an
6184 unary operator that can start an expression. Consume all
6185 but the last of a possible series of __extension__ to
6186 determine which. */
6187 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
6188 && (c_parser_peek_2nd_token (parser)->keyword
6189 == RID_EXTENSION))
6190 c_parser_consume_token (parser);
6191 if (c_token_starts_declaration (c_parser_peek_2nd_token (parser)))
6192 {
6193 int ext;
6194 ext = disable_extension_diagnostics ();
6195 c_parser_consume_token (parser);
6196 c_parser_declaration_or_fndef (parser, true, true, true, true,
6197 true, &object_expression, vNULL);
6198 parser->objc_could_be_foreach_context = false;
6199
6200 restore_extension_diagnostics (ext);
6201 if (c_parser_next_token_is_keyword (parser, RID_IN))
6202 {
6203 c_parser_consume_token (parser);
6204 is_foreach_statement = true;
6205 if (check_for_loop_decls (for_loc, true) == NULL_TREE)
6206 c_parser_error (parser, "multiple iterating variables in "
6207 "fast enumeration");
6208 }
6209 else
6210 check_for_loop_decls (for_loc, flag_isoc99);
6211 }
6212 else
6213 goto init_expr;
6214 }
6215 else
6216 {
6217 init_expr:
6218 {
6219 struct c_expr ce;
6220 tree init_expression;
6221 ce = c_parser_expression (parser);
6222 init_expression = ce.value;
6223 parser->objc_could_be_foreach_context = false;
6224 if (c_parser_next_token_is_keyword (parser, RID_IN))
6225 {
6226 c_parser_consume_token (parser);
6227 is_foreach_statement = true;
6228 if (! lvalue_p (init_expression))
6229 c_parser_error (parser, "invalid iterating variable in "
6230 "fast enumeration");
6231 object_expression
6232 = c_fully_fold (init_expression, false, NULL);
6233 }
6234 else
6235 {
6236 ce = convert_lvalue_to_rvalue (loc, ce, true, false);
6237 init_expression = ce.value;
6238 c_finish_expr_stmt (loc, init_expression);
6239 c_parser_skip_until_found (parser, CPP_SEMICOLON,
6240 "expected %<;%>");
6241 }
6242 }
6243 }
6244 /* Parse the loop condition. In the case of a foreach
6245 statement, there is no loop condition. */
6246 gcc_assert (!parser->objc_could_be_foreach_context);
6247 if (!is_foreach_statement)
6248 {
6249 cond_loc = c_parser_peek_token (parser)->location;
6250 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
6251 {
6252 if (ivdep)
6253 {
6254 c_parser_error (parser, "missing loop condition in loop "
6255 "with %<GCC ivdep%> pragma");
6256 cond = error_mark_node;
6257 }
6258 else if (unroll)
6259 {
6260 c_parser_error (parser, "missing loop condition in loop "
6261 "with %<GCC unroll%> pragma");
6262 cond = error_mark_node;
6263 }
6264 else
6265 {
6266 c_parser_consume_token (parser);
6267 cond = NULL_TREE;
6268 }
6269 }
6270 else
6271 {
6272 cond = c_parser_condition (parser);
6273 c_parser_skip_until_found (parser, CPP_SEMICOLON,
6274 "expected %<;%>");
6275 }
6276 if (ivdep && cond != error_mark_node)
6277 cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
6278 build_int_cst (integer_type_node,
6279 annot_expr_ivdep_kind),
6280 integer_zero_node);
6281 if (unroll && cond != error_mark_node)
6282 cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
6283 build_int_cst (integer_type_node,
6284 annot_expr_unroll_kind),
6285 build_int_cst (integer_type_node, unroll));
6286 }
6287 /* Parse the increment expression (the third expression in a
6288 for-statement). In the case of a foreach-statement, this is
6289 the expression that follows the 'in'. */
6290 loc = incr_loc = c_parser_peek_token (parser)->location;
6291 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
6292 {
6293 if (is_foreach_statement)
6294 {
6295 c_parser_error (parser,
6296 "missing collection in fast enumeration");
6297 collection_expression = error_mark_node;
6298 }
6299 else
6300 incr = c_process_expr_stmt (loc, NULL_TREE);
6301 }
6302 else
6303 {
6304 if (is_foreach_statement)
6305 collection_expression
6306 = c_fully_fold (c_parser_expression (parser).value, false, NULL);
6307 else
6308 {
6309 struct c_expr ce = c_parser_expression (parser);
6310 ce = convert_lvalue_to_rvalue (loc, ce, true, false);
6311 incr = c_process_expr_stmt (loc, ce.value);
6312 }
6313 }
6314 parens.skip_until_found_close (parser);
6315 }
6316 save_break = c_break_label;
6317 c_break_label = NULL_TREE;
6318 save_cont = c_cont_label;
6319 c_cont_label = NULL_TREE;
6320
6321 token_indent_info body_tinfo
6322 = get_token_indent_info (c_parser_peek_token (parser));
6323
6324 location_t loc_after_labels;
6325 bool open_brace = c_parser_next_token_is (parser, CPP_OPEN_BRACE);
6326 body = c_parser_c99_block_statement (parser, if_p, &loc_after_labels);
6327
6328 if (is_foreach_statement)
6329 objc_finish_foreach_loop (for_loc, object_expression,
6330 collection_expression, body, c_break_label,
6331 c_cont_label);
6332 else
6333 c_finish_loop (for_loc, cond_loc, cond, incr_loc, incr, body,
6334 c_break_label, c_cont_label, true);
6335 add_stmt (c_end_compound_stmt (for_loc, block,
6336 flag_isoc99 || c_dialect_objc ()));
6337 c_parser_maybe_reclassify_token (parser);
6338
6339 token_indent_info next_tinfo
6340 = get_token_indent_info (c_parser_peek_token (parser));
6341 warn_for_misleading_indentation (for_tinfo, body_tinfo, next_tinfo);
6342
6343 if (next_tinfo.type != CPP_SEMICOLON && !open_brace)
6344 warn_for_multistatement_macros (loc_after_labels, next_tinfo.location,
6345 for_tinfo.location, RID_FOR);
6346
6347 c_break_label = save_break;
6348 c_cont_label = save_cont;
6349 }
6350
6351 /* Parse an asm statement, a GNU extension. This is a full-blown asm
6352 statement with inputs, outputs, clobbers, and volatile, inline, and goto
6353 tags allowed.
6354
6355 asm-qualifier:
6356 volatile
6357 inline
6358 goto
6359
6360 asm-qualifier-list:
6361 asm-qualifier-list asm-qualifier
6362 asm-qualifier
6363
6364 asm-statement:
6365 asm asm-qualifier-list[opt] ( asm-argument ) ;
6366
6367 asm-argument:
6368 asm-string-literal
6369 asm-string-literal : asm-operands[opt]
6370 asm-string-literal : asm-operands[opt] : asm-operands[opt]
6371 asm-string-literal : asm-operands[opt] : asm-operands[opt] \
6372 : asm-clobbers[opt]
6373 asm-string-literal : : asm-operands[opt] : asm-clobbers[opt] \
6374 : asm-goto-operands
6375
6376 The form with asm-goto-operands is valid if and only if the
6377 asm-qualifier-list contains goto, and is the only allowed form in that case.
6378 Duplicate asm-qualifiers are not allowed. */
6379
6380 static tree
6381 c_parser_asm_statement (c_parser *parser)
6382 {
6383 tree str, outputs, inputs, clobbers, labels, ret;
6384 bool simple;
6385 location_t asm_loc = c_parser_peek_token (parser)->location;
6386 int section, nsections;
6387
6388 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
6389 c_parser_consume_token (parser);
6390
6391 /* Handle the asm-qualifier-list. */
6392 location_t volatile_loc = UNKNOWN_LOCATION;
6393 location_t inline_loc = UNKNOWN_LOCATION;
6394 location_t goto_loc = UNKNOWN_LOCATION;
6395 for (;;)
6396 {
6397 c_token *token = c_parser_peek_token (parser);
6398 location_t loc = token->location;
6399 switch (token->keyword)
6400 {
6401 case RID_VOLATILE:
6402 if (volatile_loc)
6403 {
6404 error_at (loc, "duplicate asm qualifier %qE", token->value);
6405 inform (volatile_loc, "first seen here");
6406 }
6407 else
6408 volatile_loc = loc;
6409 c_parser_consume_token (parser);
6410 continue;
6411
6412 case RID_INLINE:
6413 if (inline_loc)
6414 {
6415 error_at (loc, "duplicate asm qualifier %qE", token->value);
6416 inform (inline_loc, "first seen here");
6417 }
6418 else
6419 inline_loc = loc;
6420 c_parser_consume_token (parser);
6421 continue;
6422
6423 case RID_GOTO:
6424 if (goto_loc)
6425 {
6426 error_at (loc, "duplicate asm qualifier %qE", token->value);
6427 inform (goto_loc, "first seen here");
6428 }
6429 else
6430 goto_loc = loc;
6431 c_parser_consume_token (parser);
6432 continue;
6433
6434 case RID_CONST:
6435 case RID_RESTRICT:
6436 error_at (loc, "%qE is not an asm qualifier", token->value);
6437 c_parser_consume_token (parser);
6438 continue;
6439
6440 default:
6441 break;
6442 }
6443 break;
6444 }
6445
6446 bool is_volatile = (volatile_loc != UNKNOWN_LOCATION);
6447 bool is_inline = (inline_loc != UNKNOWN_LOCATION);
6448 bool is_goto = (goto_loc != UNKNOWN_LOCATION);
6449
6450 /* ??? Follow the C++ parser rather than using the
6451 lex_untranslated_string kludge. */
6452 parser->lex_untranslated_string = true;
6453 ret = NULL;
6454
6455 matching_parens parens;
6456 if (!parens.require_open (parser))
6457 goto error;
6458
6459 str = c_parser_asm_string_literal (parser);
6460 if (str == NULL_TREE)
6461 goto error_close_paren;
6462
6463 simple = true;
6464 outputs = NULL_TREE;
6465 inputs = NULL_TREE;
6466 clobbers = NULL_TREE;
6467 labels = NULL_TREE;
6468
6469 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
6470 goto done_asm;
6471
6472 /* Parse each colon-delimited section of operands. */
6473 nsections = 3 + is_goto;
6474 for (section = 0; section < nsections; ++section)
6475 {
6476 if (!c_parser_require (parser, CPP_COLON,
6477 is_goto
6478 ? G_("expected %<:%>")
6479 : G_("expected %<:%> or %<)%>"),
6480 UNKNOWN_LOCATION, is_goto))
6481 goto error_close_paren;
6482
6483 /* Once past any colon, we're no longer a simple asm. */
6484 simple = false;
6485
6486 if ((!c_parser_next_token_is (parser, CPP_COLON)
6487 && !c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
6488 || section == 3)
6489 switch (section)
6490 {
6491 case 0:
6492 /* For asm goto, we don't allow output operands, but reserve
6493 the slot for a future extension that does allow them. */
6494 if (!is_goto)
6495 outputs = c_parser_asm_operands (parser);
6496 break;
6497 case 1:
6498 inputs = c_parser_asm_operands (parser);
6499 break;
6500 case 2:
6501 clobbers = c_parser_asm_clobbers (parser);
6502 break;
6503 case 3:
6504 labels = c_parser_asm_goto_operands (parser);
6505 break;
6506 default:
6507 gcc_unreachable ();
6508 }
6509
6510 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
6511 goto done_asm;
6512 }
6513
6514 done_asm:
6515 if (!parens.require_close (parser))
6516 {
6517 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6518 goto error;
6519 }
6520
6521 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
6522 c_parser_skip_to_end_of_block_or_statement (parser);
6523
6524 ret = build_asm_stmt (is_volatile,
6525 build_asm_expr (asm_loc, str, outputs, inputs,
6526 clobbers, labels, simple, is_inline));
6527
6528 error:
6529 parser->lex_untranslated_string = false;
6530 return ret;
6531
6532 error_close_paren:
6533 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6534 goto error;
6535 }
6536
6537 /* Parse asm operands, a GNU extension.
6538
6539 asm-operands:
6540 asm-operand
6541 asm-operands , asm-operand
6542
6543 asm-operand:
6544 asm-string-literal ( expression )
6545 [ identifier ] asm-string-literal ( expression )
6546 */
6547
6548 static tree
6549 c_parser_asm_operands (c_parser *parser)
6550 {
6551 tree list = NULL_TREE;
6552 while (true)
6553 {
6554 tree name, str;
6555 struct c_expr expr;
6556 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
6557 {
6558 c_parser_consume_token (parser);
6559 if (c_parser_next_token_is (parser, CPP_NAME))
6560 {
6561 tree id = c_parser_peek_token (parser)->value;
6562 c_parser_consume_token (parser);
6563 name = build_string (IDENTIFIER_LENGTH (id),
6564 IDENTIFIER_POINTER (id));
6565 }
6566 else
6567 {
6568 c_parser_error (parser, "expected identifier");
6569 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
6570 return NULL_TREE;
6571 }
6572 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
6573 "expected %<]%>");
6574 }
6575 else
6576 name = NULL_TREE;
6577 str = c_parser_asm_string_literal (parser);
6578 if (str == NULL_TREE)
6579 return NULL_TREE;
6580 parser->lex_untranslated_string = false;
6581 matching_parens parens;
6582 if (!parens.require_open (parser))
6583 {
6584 parser->lex_untranslated_string = true;
6585 return NULL_TREE;
6586 }
6587 expr = c_parser_expression (parser);
6588 mark_exp_read (expr.value);
6589 parser->lex_untranslated_string = true;
6590 if (!parens.require_close (parser))
6591 {
6592 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6593 return NULL_TREE;
6594 }
6595 list = chainon (list, build_tree_list (build_tree_list (name, str),
6596 expr.value));
6597 if (c_parser_next_token_is (parser, CPP_COMMA))
6598 c_parser_consume_token (parser);
6599 else
6600 break;
6601 }
6602 return list;
6603 }
6604
6605 /* Parse asm clobbers, a GNU extension.
6606
6607 asm-clobbers:
6608 asm-string-literal
6609 asm-clobbers , asm-string-literal
6610 */
6611
6612 static tree
6613 c_parser_asm_clobbers (c_parser *parser)
6614 {
6615 tree list = NULL_TREE;
6616 while (true)
6617 {
6618 tree str = c_parser_asm_string_literal (parser);
6619 if (str)
6620 list = tree_cons (NULL_TREE, str, list);
6621 else
6622 return NULL_TREE;
6623 if (c_parser_next_token_is (parser, CPP_COMMA))
6624 c_parser_consume_token (parser);
6625 else
6626 break;
6627 }
6628 return list;
6629 }
6630
6631 /* Parse asm goto labels, a GNU extension.
6632
6633 asm-goto-operands:
6634 identifier
6635 asm-goto-operands , identifier
6636 */
6637
6638 static tree
6639 c_parser_asm_goto_operands (c_parser *parser)
6640 {
6641 tree list = NULL_TREE;
6642 while (true)
6643 {
6644 tree name, label;
6645
6646 if (c_parser_next_token_is (parser, CPP_NAME))
6647 {
6648 c_token *tok = c_parser_peek_token (parser);
6649 name = tok->value;
6650 label = lookup_label_for_goto (tok->location, name);
6651 c_parser_consume_token (parser);
6652 TREE_USED (label) = 1;
6653 }
6654 else
6655 {
6656 c_parser_error (parser, "expected identifier");
6657 return NULL_TREE;
6658 }
6659
6660 name = build_string (IDENTIFIER_LENGTH (name),
6661 IDENTIFIER_POINTER (name));
6662 list = tree_cons (name, label, list);
6663 if (c_parser_next_token_is (parser, CPP_COMMA))
6664 c_parser_consume_token (parser);
6665 else
6666 return nreverse (list);
6667 }
6668 }
6669
6670 /* Parse an expression other than a compound expression; that is, an
6671 assignment expression (C90 6.3.16, C99 6.5.16, C11 6.5.16). If
6672 AFTER is not NULL then it is an Objective-C message expression which
6673 is the primary-expression starting the expression as an initializer.
6674
6675 assignment-expression:
6676 conditional-expression
6677 unary-expression assignment-operator assignment-expression
6678
6679 assignment-operator: one of
6680 = *= /= %= += -= <<= >>= &= ^= |=
6681
6682 In GNU C we accept any conditional expression on the LHS and
6683 diagnose the invalid lvalue rather than producing a syntax
6684 error. */
6685
6686 static struct c_expr
6687 c_parser_expr_no_commas (c_parser *parser, struct c_expr *after,
6688 tree omp_atomic_lhs)
6689 {
6690 struct c_expr lhs, rhs, ret;
6691 enum tree_code code;
6692 location_t op_location, exp_location;
6693 gcc_assert (!after || c_dialect_objc ());
6694 lhs = c_parser_conditional_expression (parser, after, omp_atomic_lhs);
6695 op_location = c_parser_peek_token (parser)->location;
6696 switch (c_parser_peek_token (parser)->type)
6697 {
6698 case CPP_EQ:
6699 code = NOP_EXPR;
6700 break;
6701 case CPP_MULT_EQ:
6702 code = MULT_EXPR;
6703 break;
6704 case CPP_DIV_EQ:
6705 code = TRUNC_DIV_EXPR;
6706 break;
6707 case CPP_MOD_EQ:
6708 code = TRUNC_MOD_EXPR;
6709 break;
6710 case CPP_PLUS_EQ:
6711 code = PLUS_EXPR;
6712 break;
6713 case CPP_MINUS_EQ:
6714 code = MINUS_EXPR;
6715 break;
6716 case CPP_LSHIFT_EQ:
6717 code = LSHIFT_EXPR;
6718 break;
6719 case CPP_RSHIFT_EQ:
6720 code = RSHIFT_EXPR;
6721 break;
6722 case CPP_AND_EQ:
6723 code = BIT_AND_EXPR;
6724 break;
6725 case CPP_XOR_EQ:
6726 code = BIT_XOR_EXPR;
6727 break;
6728 case CPP_OR_EQ:
6729 code = BIT_IOR_EXPR;
6730 break;
6731 default:
6732 return lhs;
6733 }
6734 c_parser_consume_token (parser);
6735 exp_location = c_parser_peek_token (parser)->location;
6736 rhs = c_parser_expr_no_commas (parser, NULL);
6737 rhs = convert_lvalue_to_rvalue (exp_location, rhs, true, true);
6738
6739 ret.value = build_modify_expr (op_location, lhs.value, lhs.original_type,
6740 code, exp_location, rhs.value,
6741 rhs.original_type);
6742 set_c_expr_source_range (&ret, lhs.get_start (), rhs.get_finish ());
6743 if (code == NOP_EXPR)
6744 ret.original_code = MODIFY_EXPR;
6745 else
6746 {
6747 TREE_NO_WARNING (ret.value) = 1;
6748 ret.original_code = ERROR_MARK;
6749 }
6750 ret.original_type = NULL;
6751 return ret;
6752 }
6753
6754 /* Parse a conditional expression (C90 6.3.15, C99 6.5.15, C11 6.5.15). If
6755 AFTER is not NULL then it is an Objective-C message expression which is
6756 the primary-expression starting the expression as an initializer.
6757
6758 conditional-expression:
6759 logical-OR-expression
6760 logical-OR-expression ? expression : conditional-expression
6761
6762 GNU extensions:
6763
6764 conditional-expression:
6765 logical-OR-expression ? : conditional-expression
6766 */
6767
6768 static struct c_expr
6769 c_parser_conditional_expression (c_parser *parser, struct c_expr *after,
6770 tree omp_atomic_lhs)
6771 {
6772 struct c_expr cond, exp1, exp2, ret;
6773 location_t start, cond_loc, colon_loc;
6774
6775 gcc_assert (!after || c_dialect_objc ());
6776
6777 cond = c_parser_binary_expression (parser, after, omp_atomic_lhs);
6778
6779 if (c_parser_next_token_is_not (parser, CPP_QUERY))
6780 return cond;
6781 if (cond.value != error_mark_node)
6782 start = cond.get_start ();
6783 else
6784 start = UNKNOWN_LOCATION;
6785 cond_loc = c_parser_peek_token (parser)->location;
6786 cond = convert_lvalue_to_rvalue (cond_loc, cond, true, true);
6787 c_parser_consume_token (parser);
6788 if (c_parser_next_token_is (parser, CPP_COLON))
6789 {
6790 tree eptype = NULL_TREE;
6791
6792 location_t middle_loc = c_parser_peek_token (parser)->location;
6793 pedwarn (middle_loc, OPT_Wpedantic,
6794 "ISO C forbids omitting the middle term of a ?: expression");
6795 if (TREE_CODE (cond.value) == EXCESS_PRECISION_EXPR)
6796 {
6797 eptype = TREE_TYPE (cond.value);
6798 cond.value = TREE_OPERAND (cond.value, 0);
6799 }
6800 tree e = cond.value;
6801 while (TREE_CODE (e) == COMPOUND_EXPR)
6802 e = TREE_OPERAND (e, 1);
6803 warn_for_omitted_condop (middle_loc, e);
6804 /* Make sure first operand is calculated only once. */
6805 exp1.value = save_expr (default_conversion (cond.value));
6806 if (eptype)
6807 exp1.value = build1 (EXCESS_PRECISION_EXPR, eptype, exp1.value);
6808 exp1.original_type = NULL;
6809 exp1.src_range = cond.src_range;
6810 cond.value = c_objc_common_truthvalue_conversion (cond_loc, exp1.value);
6811 c_inhibit_evaluation_warnings += cond.value == truthvalue_true_node;
6812 }
6813 else
6814 {
6815 cond.value
6816 = c_objc_common_truthvalue_conversion
6817 (cond_loc, default_conversion (cond.value));
6818 c_inhibit_evaluation_warnings += cond.value == truthvalue_false_node;
6819 exp1 = c_parser_expression_conv (parser);
6820 mark_exp_read (exp1.value);
6821 c_inhibit_evaluation_warnings +=
6822 ((cond.value == truthvalue_true_node)
6823 - (cond.value == truthvalue_false_node));
6824 }
6825
6826 colon_loc = c_parser_peek_token (parser)->location;
6827 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
6828 {
6829 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
6830 ret.set_error ();
6831 ret.original_code = ERROR_MARK;
6832 ret.original_type = NULL;
6833 return ret;
6834 }
6835 {
6836 location_t exp2_loc = c_parser_peek_token (parser)->location;
6837 exp2 = c_parser_conditional_expression (parser, NULL, NULL_TREE);
6838 exp2 = convert_lvalue_to_rvalue (exp2_loc, exp2, true, true);
6839 }
6840 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
6841 location_t loc1 = make_location (exp1.get_start (), exp1.src_range);
6842 location_t loc2 = make_location (exp2.get_start (), exp2.src_range);
6843 ret.value = build_conditional_expr (colon_loc, cond.value,
6844 cond.original_code == C_MAYBE_CONST_EXPR,
6845 exp1.value, exp1.original_type, loc1,
6846 exp2.value, exp2.original_type, loc2);
6847 ret.original_code = ERROR_MARK;
6848 if (exp1.value == error_mark_node || exp2.value == error_mark_node)
6849 ret.original_type = NULL;
6850 else
6851 {
6852 tree t1, t2;
6853
6854 /* If both sides are enum type, the default conversion will have
6855 made the type of the result be an integer type. We want to
6856 remember the enum types we started with. */
6857 t1 = exp1.original_type ? exp1.original_type : TREE_TYPE (exp1.value);
6858 t2 = exp2.original_type ? exp2.original_type : TREE_TYPE (exp2.value);
6859 ret.original_type = ((t1 != error_mark_node
6860 && t2 != error_mark_node
6861 && (TYPE_MAIN_VARIANT (t1)
6862 == TYPE_MAIN_VARIANT (t2)))
6863 ? t1
6864 : NULL);
6865 }
6866 set_c_expr_source_range (&ret, start, exp2.get_finish ());
6867 return ret;
6868 }
6869
6870 /* Parse a binary expression; that is, a logical-OR-expression (C90
6871 6.3.5-6.3.14, C99 6.5.5-6.5.14, C11 6.5.5-6.5.14). If AFTER is not
6872 NULL then it is an Objective-C message expression which is the
6873 primary-expression starting the expression as an initializer.
6874
6875 OMP_ATOMIC_LHS is NULL, unless parsing OpenMP #pragma omp atomic,
6876 when it should be the unfolded lhs. In a valid OpenMP source,
6877 one of the operands of the toplevel binary expression must be equal
6878 to it. In that case, just return a build2 created binary operation
6879 rather than result of parser_build_binary_op.
6880
6881 multiplicative-expression:
6882 cast-expression
6883 multiplicative-expression * cast-expression
6884 multiplicative-expression / cast-expression
6885 multiplicative-expression % cast-expression
6886
6887 additive-expression:
6888 multiplicative-expression
6889 additive-expression + multiplicative-expression
6890 additive-expression - multiplicative-expression
6891
6892 shift-expression:
6893 additive-expression
6894 shift-expression << additive-expression
6895 shift-expression >> additive-expression
6896
6897 relational-expression:
6898 shift-expression
6899 relational-expression < shift-expression
6900 relational-expression > shift-expression
6901 relational-expression <= shift-expression
6902 relational-expression >= shift-expression
6903
6904 equality-expression:
6905 relational-expression
6906 equality-expression == relational-expression
6907 equality-expression != relational-expression
6908
6909 AND-expression:
6910 equality-expression
6911 AND-expression & equality-expression
6912
6913 exclusive-OR-expression:
6914 AND-expression
6915 exclusive-OR-expression ^ AND-expression
6916
6917 inclusive-OR-expression:
6918 exclusive-OR-expression
6919 inclusive-OR-expression | exclusive-OR-expression
6920
6921 logical-AND-expression:
6922 inclusive-OR-expression
6923 logical-AND-expression && inclusive-OR-expression
6924
6925 logical-OR-expression:
6926 logical-AND-expression
6927 logical-OR-expression || logical-AND-expression
6928 */
6929
6930 static struct c_expr
6931 c_parser_binary_expression (c_parser *parser, struct c_expr *after,
6932 tree omp_atomic_lhs)
6933 {
6934 /* A binary expression is parsed using operator-precedence parsing,
6935 with the operands being cast expressions. All the binary
6936 operators are left-associative. Thus a binary expression is of
6937 form:
6938
6939 E0 op1 E1 op2 E2 ...
6940
6941 which we represent on a stack. On the stack, the precedence
6942 levels are strictly increasing. When a new operator is
6943 encountered of higher precedence than that at the top of the
6944 stack, it is pushed; its LHS is the top expression, and its RHS
6945 is everything parsed until it is popped. When a new operator is
6946 encountered with precedence less than or equal to that at the top
6947 of the stack, triples E[i-1] op[i] E[i] are popped and replaced
6948 by the result of the operation until the operator at the top of
6949 the stack has lower precedence than the new operator or there is
6950 only one element on the stack; then the top expression is the LHS
6951 of the new operator. In the case of logical AND and OR
6952 expressions, we also need to adjust c_inhibit_evaluation_warnings
6953 as appropriate when the operators are pushed and popped. */
6954
6955 struct {
6956 /* The expression at this stack level. */
6957 struct c_expr expr;
6958 /* The precedence of the operator on its left, PREC_NONE at the
6959 bottom of the stack. */
6960 enum c_parser_prec prec;
6961 /* The operation on its left. */
6962 enum tree_code op;
6963 /* The source location of this operation. */
6964 location_t loc;
6965 /* The sizeof argument if expr.original_code == SIZEOF_EXPR. */
6966 tree sizeof_arg;
6967 } stack[NUM_PRECS];
6968 int sp;
6969 /* Location of the binary operator. */
6970 location_t binary_loc = UNKNOWN_LOCATION; /* Quiet warning. */
6971 #define POP \
6972 do { \
6973 switch (stack[sp].op) \
6974 { \
6975 case TRUTH_ANDIF_EXPR: \
6976 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
6977 == truthvalue_false_node); \
6978 break; \
6979 case TRUTH_ORIF_EXPR: \
6980 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
6981 == truthvalue_true_node); \
6982 break; \
6983 case TRUNC_DIV_EXPR: \
6984 if (stack[sp - 1].expr.original_code == SIZEOF_EXPR \
6985 && stack[sp].expr.original_code == SIZEOF_EXPR) \
6986 { \
6987 tree type0 = stack[sp - 1].sizeof_arg; \
6988 tree type1 = stack[sp].sizeof_arg; \
6989 tree first_arg = type0; \
6990 if (!TYPE_P (type0)) \
6991 type0 = TREE_TYPE (type0); \
6992 if (!TYPE_P (type1)) \
6993 type1 = TREE_TYPE (type1); \
6994 if (POINTER_TYPE_P (type0) \
6995 && comptypes (TREE_TYPE (type0), type1) \
6996 && !(TREE_CODE (first_arg) == PARM_DECL \
6997 && C_ARRAY_PARAMETER (first_arg) \
6998 && warn_sizeof_array_argument)) \
6999 { \
7000 auto_diagnostic_group d; \
7001 if (warning_at (stack[sp].loc, OPT_Wsizeof_pointer_div, \
7002 "division %<sizeof (%T) / sizeof (%T)%> " \
7003 "does not compute the number of array " \
7004 "elements", \
7005 type0, type1)) \
7006 if (DECL_P (first_arg)) \
7007 inform (DECL_SOURCE_LOCATION (first_arg), \
7008 "first %<sizeof%> operand was declared here"); \
7009 } \
7010 } \
7011 break; \
7012 default: \
7013 break; \
7014 } \
7015 stack[sp - 1].expr \
7016 = convert_lvalue_to_rvalue (stack[sp - 1].loc, \
7017 stack[sp - 1].expr, true, true); \
7018 stack[sp].expr \
7019 = convert_lvalue_to_rvalue (stack[sp].loc, \
7020 stack[sp].expr, true, true); \
7021 if (__builtin_expect (omp_atomic_lhs != NULL_TREE, 0) && sp == 1 \
7022 && c_parser_peek_token (parser)->type == CPP_SEMICOLON \
7023 && ((1 << stack[sp].prec) \
7024 & ((1 << PREC_BITOR) | (1 << PREC_BITXOR) | (1 << PREC_BITAND) \
7025 | (1 << PREC_SHIFT) | (1 << PREC_ADD) | (1 << PREC_MULT))) \
7026 && stack[sp].op != TRUNC_MOD_EXPR \
7027 && stack[0].expr.value != error_mark_node \
7028 && stack[1].expr.value != error_mark_node \
7029 && (c_tree_equal (stack[0].expr.value, omp_atomic_lhs) \
7030 || c_tree_equal (stack[1].expr.value, omp_atomic_lhs))) \
7031 stack[0].expr.value \
7032 = build2 (stack[1].op, TREE_TYPE (stack[0].expr.value), \
7033 stack[0].expr.value, stack[1].expr.value); \
7034 else \
7035 stack[sp - 1].expr = parser_build_binary_op (stack[sp].loc, \
7036 stack[sp].op, \
7037 stack[sp - 1].expr, \
7038 stack[sp].expr); \
7039 sp--; \
7040 } while (0)
7041 gcc_assert (!after || c_dialect_objc ());
7042 stack[0].loc = c_parser_peek_token (parser)->location;
7043 stack[0].expr = c_parser_cast_expression (parser, after);
7044 stack[0].prec = PREC_NONE;
7045 stack[0].sizeof_arg = c_last_sizeof_arg;
7046 sp = 0;
7047 while (true)
7048 {
7049 enum c_parser_prec oprec;
7050 enum tree_code ocode;
7051 source_range src_range;
7052 if (parser->error)
7053 goto out;
7054 switch (c_parser_peek_token (parser)->type)
7055 {
7056 case CPP_MULT:
7057 oprec = PREC_MULT;
7058 ocode = MULT_EXPR;
7059 break;
7060 case CPP_DIV:
7061 oprec = PREC_MULT;
7062 ocode = TRUNC_DIV_EXPR;
7063 break;
7064 case CPP_MOD:
7065 oprec = PREC_MULT;
7066 ocode = TRUNC_MOD_EXPR;
7067 break;
7068 case CPP_PLUS:
7069 oprec = PREC_ADD;
7070 ocode = PLUS_EXPR;
7071 break;
7072 case CPP_MINUS:
7073 oprec = PREC_ADD;
7074 ocode = MINUS_EXPR;
7075 break;
7076 case CPP_LSHIFT:
7077 oprec = PREC_SHIFT;
7078 ocode = LSHIFT_EXPR;
7079 break;
7080 case CPP_RSHIFT:
7081 oprec = PREC_SHIFT;
7082 ocode = RSHIFT_EXPR;
7083 break;
7084 case CPP_LESS:
7085 oprec = PREC_REL;
7086 ocode = LT_EXPR;
7087 break;
7088 case CPP_GREATER:
7089 oprec = PREC_REL;
7090 ocode = GT_EXPR;
7091 break;
7092 case CPP_LESS_EQ:
7093 oprec = PREC_REL;
7094 ocode = LE_EXPR;
7095 break;
7096 case CPP_GREATER_EQ:
7097 oprec = PREC_REL;
7098 ocode = GE_EXPR;
7099 break;
7100 case CPP_EQ_EQ:
7101 oprec = PREC_EQ;
7102 ocode = EQ_EXPR;
7103 break;
7104 case CPP_NOT_EQ:
7105 oprec = PREC_EQ;
7106 ocode = NE_EXPR;
7107 break;
7108 case CPP_AND:
7109 oprec = PREC_BITAND;
7110 ocode = BIT_AND_EXPR;
7111 break;
7112 case CPP_XOR:
7113 oprec = PREC_BITXOR;
7114 ocode = BIT_XOR_EXPR;
7115 break;
7116 case CPP_OR:
7117 oprec = PREC_BITOR;
7118 ocode = BIT_IOR_EXPR;
7119 break;
7120 case CPP_AND_AND:
7121 oprec = PREC_LOGAND;
7122 ocode = TRUTH_ANDIF_EXPR;
7123 break;
7124 case CPP_OR_OR:
7125 oprec = PREC_LOGOR;
7126 ocode = TRUTH_ORIF_EXPR;
7127 break;
7128 default:
7129 /* Not a binary operator, so end of the binary
7130 expression. */
7131 goto out;
7132 }
7133 binary_loc = c_parser_peek_token (parser)->location;
7134 while (oprec <= stack[sp].prec)
7135 POP;
7136 c_parser_consume_token (parser);
7137 switch (ocode)
7138 {
7139 case TRUTH_ANDIF_EXPR:
7140 src_range = stack[sp].expr.src_range;
7141 stack[sp].expr
7142 = convert_lvalue_to_rvalue (stack[sp].loc,
7143 stack[sp].expr, true, true);
7144 stack[sp].expr.value = c_objc_common_truthvalue_conversion
7145 (stack[sp].loc, default_conversion (stack[sp].expr.value));
7146 c_inhibit_evaluation_warnings += (stack[sp].expr.value
7147 == truthvalue_false_node);
7148 set_c_expr_source_range (&stack[sp].expr, src_range);
7149 break;
7150 case TRUTH_ORIF_EXPR:
7151 src_range = stack[sp].expr.src_range;
7152 stack[sp].expr
7153 = convert_lvalue_to_rvalue (stack[sp].loc,
7154 stack[sp].expr, true, true);
7155 stack[sp].expr.value = c_objc_common_truthvalue_conversion
7156 (stack[sp].loc, default_conversion (stack[sp].expr.value));
7157 c_inhibit_evaluation_warnings += (stack[sp].expr.value
7158 == truthvalue_true_node);
7159 set_c_expr_source_range (&stack[sp].expr, src_range);
7160 break;
7161 default:
7162 break;
7163 }
7164 sp++;
7165 stack[sp].loc = binary_loc;
7166 stack[sp].expr = c_parser_cast_expression (parser, NULL);
7167 stack[sp].prec = oprec;
7168 stack[sp].op = ocode;
7169 stack[sp].sizeof_arg = c_last_sizeof_arg;
7170 }
7171 out:
7172 while (sp > 0)
7173 POP;
7174 return stack[0].expr;
7175 #undef POP
7176 }
7177
7178 /* Parse a cast expression (C90 6.3.4, C99 6.5.4, C11 6.5.4). If AFTER
7179 is not NULL then it is an Objective-C message expression which is the
7180 primary-expression starting the expression as an initializer.
7181
7182 cast-expression:
7183 unary-expression
7184 ( type-name ) unary-expression
7185 */
7186
7187 static struct c_expr
7188 c_parser_cast_expression (c_parser *parser, struct c_expr *after)
7189 {
7190 location_t cast_loc = c_parser_peek_token (parser)->location;
7191 gcc_assert (!after || c_dialect_objc ());
7192 if (after)
7193 return c_parser_postfix_expression_after_primary (parser,
7194 cast_loc, *after);
7195 /* If the expression begins with a parenthesized type name, it may
7196 be either a cast or a compound literal; we need to see whether
7197 the next character is '{' to tell the difference. If not, it is
7198 an unary expression. Full detection of unknown typenames here
7199 would require a 3-token lookahead. */
7200 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
7201 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
7202 {
7203 struct c_type_name *type_name;
7204 struct c_expr ret;
7205 struct c_expr expr;
7206 matching_parens parens;
7207 parens.consume_open (parser);
7208 type_name = c_parser_type_name (parser, true);
7209 parens.skip_until_found_close (parser);
7210 if (type_name == NULL)
7211 {
7212 ret.set_error ();
7213 ret.original_code = ERROR_MARK;
7214 ret.original_type = NULL;
7215 return ret;
7216 }
7217
7218 /* Save casted types in the function's used types hash table. */
7219 used_types_insert (type_name->specs->type);
7220
7221 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
7222 return c_parser_postfix_expression_after_paren_type (parser, type_name,
7223 cast_loc);
7224 if (type_name->specs->alignas_p)
7225 error_at (type_name->specs->locations[cdw_alignas],
7226 "alignment specified for type name in cast");
7227 {
7228 location_t expr_loc = c_parser_peek_token (parser)->location;
7229 expr = c_parser_cast_expression (parser, NULL);
7230 expr = convert_lvalue_to_rvalue (expr_loc, expr, true, true);
7231 }
7232 ret.value = c_cast_expr (cast_loc, type_name, expr.value);
7233 if (ret.value && expr.value)
7234 set_c_expr_source_range (&ret, cast_loc, expr.get_finish ());
7235 ret.original_code = ERROR_MARK;
7236 ret.original_type = NULL;
7237 return ret;
7238 }
7239 else
7240 return c_parser_unary_expression (parser);
7241 }
7242
7243 /* Parse an unary expression (C90 6.3.3, C99 6.5.3, C11 6.5.3).
7244
7245 unary-expression:
7246 postfix-expression
7247 ++ unary-expression
7248 -- unary-expression
7249 unary-operator cast-expression
7250 sizeof unary-expression
7251 sizeof ( type-name )
7252
7253 unary-operator: one of
7254 & * + - ~ !
7255
7256 GNU extensions:
7257
7258 unary-expression:
7259 __alignof__ unary-expression
7260 __alignof__ ( type-name )
7261 && identifier
7262
7263 (C11 permits _Alignof with type names only.)
7264
7265 unary-operator: one of
7266 __extension__ __real__ __imag__
7267
7268 Transactional Memory:
7269
7270 unary-expression:
7271 transaction-expression
7272
7273 In addition, the GNU syntax treats ++ and -- as unary operators, so
7274 they may be applied to cast expressions with errors for non-lvalues
7275 given later. */
7276
7277 static struct c_expr
7278 c_parser_unary_expression (c_parser *parser)
7279 {
7280 int ext;
7281 struct c_expr ret, op;
7282 location_t op_loc = c_parser_peek_token (parser)->location;
7283 location_t exp_loc;
7284 location_t finish;
7285 ret.original_code = ERROR_MARK;
7286 ret.original_type = NULL;
7287 switch (c_parser_peek_token (parser)->type)
7288 {
7289 case CPP_PLUS_PLUS:
7290 c_parser_consume_token (parser);
7291 exp_loc = c_parser_peek_token (parser)->location;
7292 op = c_parser_cast_expression (parser, NULL);
7293
7294 op = default_function_array_read_conversion (exp_loc, op);
7295 return parser_build_unary_op (op_loc, PREINCREMENT_EXPR, op);
7296 case CPP_MINUS_MINUS:
7297 c_parser_consume_token (parser);
7298 exp_loc = c_parser_peek_token (parser)->location;
7299 op = c_parser_cast_expression (parser, NULL);
7300
7301 op = default_function_array_read_conversion (exp_loc, op);
7302 return parser_build_unary_op (op_loc, PREDECREMENT_EXPR, op);
7303 case CPP_AND:
7304 c_parser_consume_token (parser);
7305 op = c_parser_cast_expression (parser, NULL);
7306 mark_exp_read (op.value);
7307 return parser_build_unary_op (op_loc, ADDR_EXPR, op);
7308 case CPP_MULT:
7309 {
7310 c_parser_consume_token (parser);
7311 exp_loc = c_parser_peek_token (parser)->location;
7312 op = c_parser_cast_expression (parser, NULL);
7313 finish = op.get_finish ();
7314 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
7315 location_t combined_loc = make_location (op_loc, op_loc, finish);
7316 ret.value = build_indirect_ref (combined_loc, op.value, RO_UNARY_STAR);
7317 ret.src_range.m_start = op_loc;
7318 ret.src_range.m_finish = finish;
7319 return ret;
7320 }
7321 case CPP_PLUS:
7322 if (!c_dialect_objc () && !in_system_header_at (input_location))
7323 warning_at (op_loc,
7324 OPT_Wtraditional,
7325 "traditional C rejects the unary plus operator");
7326 c_parser_consume_token (parser);
7327 exp_loc = c_parser_peek_token (parser)->location;
7328 op = c_parser_cast_expression (parser, NULL);
7329 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
7330 return parser_build_unary_op (op_loc, CONVERT_EXPR, op);
7331 case CPP_MINUS:
7332 c_parser_consume_token (parser);
7333 exp_loc = c_parser_peek_token (parser)->location;
7334 op = c_parser_cast_expression (parser, NULL);
7335 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
7336 return parser_build_unary_op (op_loc, NEGATE_EXPR, op);
7337 case CPP_COMPL:
7338 c_parser_consume_token (parser);
7339 exp_loc = c_parser_peek_token (parser)->location;
7340 op = c_parser_cast_expression (parser, NULL);
7341 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
7342 return parser_build_unary_op (op_loc, BIT_NOT_EXPR, op);
7343 case CPP_NOT:
7344 c_parser_consume_token (parser);
7345 exp_loc = c_parser_peek_token (parser)->location;
7346 op = c_parser_cast_expression (parser, NULL);
7347 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
7348 return parser_build_unary_op (op_loc, TRUTH_NOT_EXPR, op);
7349 case CPP_AND_AND:
7350 /* Refer to the address of a label as a pointer. */
7351 c_parser_consume_token (parser);
7352 if (c_parser_next_token_is (parser, CPP_NAME))
7353 {
7354 ret.value = finish_label_address_expr
7355 (c_parser_peek_token (parser)->value, op_loc);
7356 set_c_expr_source_range (&ret, op_loc,
7357 c_parser_peek_token (parser)->get_finish ());
7358 c_parser_consume_token (parser);
7359 }
7360 else
7361 {
7362 c_parser_error (parser, "expected identifier");
7363 ret.set_error ();
7364 }
7365 return ret;
7366 case CPP_KEYWORD:
7367 switch (c_parser_peek_token (parser)->keyword)
7368 {
7369 case RID_SIZEOF:
7370 return c_parser_sizeof_expression (parser);
7371 case RID_ALIGNOF:
7372 return c_parser_alignof_expression (parser);
7373 case RID_BUILTIN_HAS_ATTRIBUTE:
7374 return c_parser_has_attribute_expression (parser);
7375 case RID_EXTENSION:
7376 c_parser_consume_token (parser);
7377 ext = disable_extension_diagnostics ();
7378 ret = c_parser_cast_expression (parser, NULL);
7379 restore_extension_diagnostics (ext);
7380 return ret;
7381 case RID_REALPART:
7382 c_parser_consume_token (parser);
7383 exp_loc = c_parser_peek_token (parser)->location;
7384 op = c_parser_cast_expression (parser, NULL);
7385 op = default_function_array_conversion (exp_loc, op);
7386 return parser_build_unary_op (op_loc, REALPART_EXPR, op);
7387 case RID_IMAGPART:
7388 c_parser_consume_token (parser);
7389 exp_loc = c_parser_peek_token (parser)->location;
7390 op = c_parser_cast_expression (parser, NULL);
7391 op = default_function_array_conversion (exp_loc, op);
7392 return parser_build_unary_op (op_loc, IMAGPART_EXPR, op);
7393 case RID_TRANSACTION_ATOMIC:
7394 case RID_TRANSACTION_RELAXED:
7395 return c_parser_transaction_expression (parser,
7396 c_parser_peek_token (parser)->keyword);
7397 default:
7398 return c_parser_postfix_expression (parser);
7399 }
7400 default:
7401 return c_parser_postfix_expression (parser);
7402 }
7403 }
7404
7405 /* Parse a sizeof expression. */
7406
7407 static struct c_expr
7408 c_parser_sizeof_expression (c_parser *parser)
7409 {
7410 struct c_expr expr;
7411 struct c_expr result;
7412 location_t expr_loc;
7413 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SIZEOF));
7414
7415 location_t start;
7416 location_t finish = UNKNOWN_LOCATION;
7417
7418 start = c_parser_peek_token (parser)->location;
7419
7420 c_parser_consume_token (parser);
7421 c_inhibit_evaluation_warnings++;
7422 in_sizeof++;
7423 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
7424 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
7425 {
7426 /* Either sizeof ( type-name ) or sizeof unary-expression
7427 starting with a compound literal. */
7428 struct c_type_name *type_name;
7429 matching_parens parens;
7430 parens.consume_open (parser);
7431 expr_loc = c_parser_peek_token (parser)->location;
7432 type_name = c_parser_type_name (parser, true);
7433 parens.skip_until_found_close (parser);
7434 finish = parser->tokens_buf[0].location;
7435 if (type_name == NULL)
7436 {
7437 struct c_expr ret;
7438 c_inhibit_evaluation_warnings--;
7439 in_sizeof--;
7440 ret.set_error ();
7441 ret.original_code = ERROR_MARK;
7442 ret.original_type = NULL;
7443 return ret;
7444 }
7445 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
7446 {
7447 expr = c_parser_postfix_expression_after_paren_type (parser,
7448 type_name,
7449 expr_loc);
7450 finish = expr.get_finish ();
7451 goto sizeof_expr;
7452 }
7453 /* sizeof ( type-name ). */
7454 if (type_name->specs->alignas_p)
7455 error_at (type_name->specs->locations[cdw_alignas],
7456 "alignment specified for type name in %<sizeof%>");
7457 c_inhibit_evaluation_warnings--;
7458 in_sizeof--;
7459 result = c_expr_sizeof_type (expr_loc, type_name);
7460 }
7461 else
7462 {
7463 expr_loc = c_parser_peek_token (parser)->location;
7464 expr = c_parser_unary_expression (parser);
7465 finish = expr.get_finish ();
7466 sizeof_expr:
7467 c_inhibit_evaluation_warnings--;
7468 in_sizeof--;
7469 mark_exp_read (expr.value);
7470 if (TREE_CODE (expr.value) == COMPONENT_REF
7471 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
7472 error_at (expr_loc, "%<sizeof%> applied to a bit-field");
7473 result = c_expr_sizeof_expr (expr_loc, expr);
7474 }
7475 if (finish != UNKNOWN_LOCATION)
7476 set_c_expr_source_range (&result, start, finish);
7477 return result;
7478 }
7479
7480 /* Parse an alignof expression. */
7481
7482 static struct c_expr
7483 c_parser_alignof_expression (c_parser *parser)
7484 {
7485 struct c_expr expr;
7486 location_t start_loc = c_parser_peek_token (parser)->location;
7487 location_t end_loc;
7488 tree alignof_spelling = c_parser_peek_token (parser)->value;
7489 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNOF));
7490 bool is_c11_alignof = strcmp (IDENTIFIER_POINTER (alignof_spelling),
7491 "_Alignof") == 0;
7492 /* A diagnostic is not required for the use of this identifier in
7493 the implementation namespace; only diagnose it for the C11
7494 spelling because of existing code using the other spellings. */
7495 if (is_c11_alignof)
7496 {
7497 if (flag_isoc99)
7498 pedwarn_c99 (start_loc, OPT_Wpedantic, "ISO C99 does not support %qE",
7499 alignof_spelling);
7500 else
7501 pedwarn_c99 (start_loc, OPT_Wpedantic, "ISO C90 does not support %qE",
7502 alignof_spelling);
7503 }
7504 c_parser_consume_token (parser);
7505 c_inhibit_evaluation_warnings++;
7506 in_alignof++;
7507 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
7508 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
7509 {
7510 /* Either __alignof__ ( type-name ) or __alignof__
7511 unary-expression starting with a compound literal. */
7512 location_t loc;
7513 struct c_type_name *type_name;
7514 struct c_expr ret;
7515 matching_parens parens;
7516 parens.consume_open (parser);
7517 loc = c_parser_peek_token (parser)->location;
7518 type_name = c_parser_type_name (parser, true);
7519 end_loc = c_parser_peek_token (parser)->location;
7520 parens.skip_until_found_close (parser);
7521 if (type_name == NULL)
7522 {
7523 struct c_expr ret;
7524 c_inhibit_evaluation_warnings--;
7525 in_alignof--;
7526 ret.set_error ();
7527 ret.original_code = ERROR_MARK;
7528 ret.original_type = NULL;
7529 return ret;
7530 }
7531 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
7532 {
7533 expr = c_parser_postfix_expression_after_paren_type (parser,
7534 type_name,
7535 loc);
7536 goto alignof_expr;
7537 }
7538 /* alignof ( type-name ). */
7539 if (type_name->specs->alignas_p)
7540 error_at (type_name->specs->locations[cdw_alignas],
7541 "alignment specified for type name in %qE",
7542 alignof_spelling);
7543 c_inhibit_evaluation_warnings--;
7544 in_alignof--;
7545 ret.value = c_sizeof_or_alignof_type (loc, groktypename (type_name,
7546 NULL, NULL),
7547 false, is_c11_alignof, 1);
7548 ret.original_code = ERROR_MARK;
7549 ret.original_type = NULL;
7550 set_c_expr_source_range (&ret, start_loc, end_loc);
7551 return ret;
7552 }
7553 else
7554 {
7555 struct c_expr ret;
7556 expr = c_parser_unary_expression (parser);
7557 end_loc = expr.src_range.m_finish;
7558 alignof_expr:
7559 mark_exp_read (expr.value);
7560 c_inhibit_evaluation_warnings--;
7561 in_alignof--;
7562 if (is_c11_alignof)
7563 pedwarn (start_loc,
7564 OPT_Wpedantic, "ISO C does not allow %<%E (expression)%>",
7565 alignof_spelling);
7566 ret.value = c_alignof_expr (start_loc, expr.value);
7567 ret.original_code = ERROR_MARK;
7568 ret.original_type = NULL;
7569 set_c_expr_source_range (&ret, start_loc, end_loc);
7570 return ret;
7571 }
7572 }
7573
7574 /* Parse the __builtin_has_attribute ([expr|type], attribute-spec)
7575 expression. */
7576
7577 static struct c_expr
7578 c_parser_has_attribute_expression (c_parser *parser)
7579 {
7580 gcc_assert (c_parser_next_token_is_keyword (parser,
7581 RID_BUILTIN_HAS_ATTRIBUTE));
7582 c_parser_consume_token (parser);
7583
7584 c_inhibit_evaluation_warnings++;
7585
7586 matching_parens parens;
7587 if (!parens.require_open (parser))
7588 {
7589 c_inhibit_evaluation_warnings--;
7590 in_typeof--;
7591
7592 struct c_expr result;
7593 result.set_error ();
7594 result.original_code = ERROR_MARK;
7595 result.original_type = NULL;
7596 return result;
7597 }
7598
7599 /* Treat the type argument the same way as in typeof for the purposes
7600 of warnings. FIXME: Generalize this so the warning refers to
7601 __builtin_has_attribute rather than typeof. */
7602 in_typeof++;
7603
7604 /* The first operand: one of DECL, EXPR, or TYPE. */
7605 tree oper = NULL_TREE;
7606 if (c_parser_next_tokens_start_typename (parser, cla_prefer_id))
7607 {
7608 struct c_type_name *tname = c_parser_type_name (parser);
7609 in_typeof--;
7610 if (tname)
7611 {
7612 oper = groktypename (tname, NULL, NULL);
7613 pop_maybe_used (variably_modified_type_p (oper, NULL_TREE));
7614 }
7615 }
7616 else
7617 {
7618 struct c_expr cexpr = c_parser_expr_no_commas (parser, NULL);
7619 c_inhibit_evaluation_warnings--;
7620 in_typeof--;
7621 if (cexpr.value != error_mark_node)
7622 {
7623 mark_exp_read (cexpr.value);
7624 oper = cexpr.value;
7625 tree etype = TREE_TYPE (oper);
7626 bool was_vm = variably_modified_type_p (etype, NULL_TREE);
7627 /* This is returned with the type so that when the type is
7628 evaluated, this can be evaluated. */
7629 if (was_vm)
7630 oper = c_fully_fold (oper, false, NULL);
7631 pop_maybe_used (was_vm);
7632 }
7633 }
7634
7635 struct c_expr result;
7636 result.original_code = ERROR_MARK;
7637 result.original_type = NULL;
7638
7639 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
7640 {
7641 /* Consume the closing parenthesis if that's the next token
7642 in the likely case the built-in was invoked with fewer
7643 than two arguments. */
7644 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
7645 c_parser_consume_token (parser);
7646 c_inhibit_evaluation_warnings--;
7647 result.set_error ();
7648 return result;
7649 }
7650
7651 parser->lex_untranslated_string = true;
7652
7653 location_t atloc = c_parser_peek_token (parser)->location;
7654 /* Parse a single attribute. Require no leading comma and do not
7655 allow empty attributes. */
7656 tree attr = c_parser_attribute (parser, NULL_TREE, false, false);
7657
7658 parser->lex_untranslated_string = false;
7659
7660 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
7661 c_parser_consume_token (parser);
7662 else
7663 {
7664 c_parser_error (parser, "expected identifier");
7665 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7666
7667 result.set_error ();
7668 return result;
7669 }
7670
7671 if (!attr)
7672 {
7673 error_at (atloc, "expected identifier");
7674 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7675 "expected %<)%>");
7676 result.set_error ();
7677 return result;
7678 }
7679
7680 result.original_code = INTEGER_CST;
7681 result.original_type = boolean_type_node;
7682
7683 if (has_attribute (atloc, oper, attr, default_conversion))
7684 result.value = boolean_true_node;
7685 else
7686 result.value = boolean_false_node;
7687
7688 return result;
7689 }
7690
7691 /* Helper function to read arguments of builtins which are interfaces
7692 for the middle-end nodes like COMPLEX_EXPR, VEC_PERM_EXPR and
7693 others. The name of the builtin is passed using BNAME parameter.
7694 Function returns true if there were no errors while parsing and
7695 stores the arguments in CEXPR_LIST. If it returns true,
7696 *OUT_CLOSE_PAREN_LOC is written to with the location of the closing
7697 parenthesis. */
7698 static bool
7699 c_parser_get_builtin_args (c_parser *parser, const char *bname,
7700 vec<c_expr_t, va_gc> **ret_cexpr_list,
7701 bool choose_expr_p,
7702 location_t *out_close_paren_loc)
7703 {
7704 location_t loc = c_parser_peek_token (parser)->location;
7705 vec<c_expr_t, va_gc> *cexpr_list;
7706 c_expr_t expr;
7707 bool saved_force_folding_builtin_constant_p;
7708
7709 *ret_cexpr_list = NULL;
7710 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
7711 {
7712 error_at (loc, "cannot take address of %qs", bname);
7713 return false;
7714 }
7715
7716 c_parser_consume_token (parser);
7717
7718 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
7719 {
7720 *out_close_paren_loc = c_parser_peek_token (parser)->location;
7721 c_parser_consume_token (parser);
7722 return true;
7723 }
7724
7725 saved_force_folding_builtin_constant_p
7726 = force_folding_builtin_constant_p;
7727 force_folding_builtin_constant_p |= choose_expr_p;
7728 expr = c_parser_expr_no_commas (parser, NULL);
7729 force_folding_builtin_constant_p
7730 = saved_force_folding_builtin_constant_p;
7731 vec_alloc (cexpr_list, 1);
7732 vec_safe_push (cexpr_list, expr);
7733 while (c_parser_next_token_is (parser, CPP_COMMA))
7734 {
7735 c_parser_consume_token (parser);
7736 expr = c_parser_expr_no_commas (parser, NULL);
7737 vec_safe_push (cexpr_list, expr);
7738 }
7739
7740 *out_close_paren_loc = c_parser_peek_token (parser)->location;
7741 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
7742 return false;
7743
7744 *ret_cexpr_list = cexpr_list;
7745 return true;
7746 }
7747
7748 /* This represents a single generic-association. */
7749
7750 struct c_generic_association
7751 {
7752 /* The location of the starting token of the type. */
7753 location_t type_location;
7754 /* The association's type, or NULL_TREE for 'default'. */
7755 tree type;
7756 /* The association's expression. */
7757 struct c_expr expression;
7758 };
7759
7760 /* Parse a generic-selection. (C11 6.5.1.1).
7761
7762 generic-selection:
7763 _Generic ( assignment-expression , generic-assoc-list )
7764
7765 generic-assoc-list:
7766 generic-association
7767 generic-assoc-list , generic-association
7768
7769 generic-association:
7770 type-name : assignment-expression
7771 default : assignment-expression
7772 */
7773
7774 static struct c_expr
7775 c_parser_generic_selection (c_parser *parser)
7776 {
7777 struct c_expr selector, error_expr;
7778 tree selector_type;
7779 struct c_generic_association matched_assoc;
7780 bool match_found = false;
7781 location_t generic_loc, selector_loc;
7782
7783 error_expr.original_code = ERROR_MARK;
7784 error_expr.original_type = NULL;
7785 error_expr.set_error ();
7786 matched_assoc.type_location = UNKNOWN_LOCATION;
7787 matched_assoc.type = NULL_TREE;
7788 matched_assoc.expression = error_expr;
7789
7790 gcc_assert (c_parser_next_token_is_keyword (parser, RID_GENERIC));
7791 generic_loc = c_parser_peek_token (parser)->location;
7792 c_parser_consume_token (parser);
7793 if (flag_isoc99)
7794 pedwarn_c99 (generic_loc, OPT_Wpedantic,
7795 "ISO C99 does not support %<_Generic%>");
7796 else
7797 pedwarn_c99 (generic_loc, OPT_Wpedantic,
7798 "ISO C90 does not support %<_Generic%>");
7799
7800 matching_parens parens;
7801 if (!parens.require_open (parser))
7802 return error_expr;
7803
7804 c_inhibit_evaluation_warnings++;
7805 selector_loc = c_parser_peek_token (parser)->location;
7806 selector = c_parser_expr_no_commas (parser, NULL);
7807 selector = default_function_array_conversion (selector_loc, selector);
7808 c_inhibit_evaluation_warnings--;
7809
7810 if (selector.value == error_mark_node)
7811 {
7812 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7813 return selector;
7814 }
7815 selector_type = TREE_TYPE (selector.value);
7816 /* In ISO C terms, rvalues (including the controlling expression of
7817 _Generic) do not have qualified types. */
7818 if (TREE_CODE (selector_type) != ARRAY_TYPE)
7819 selector_type = TYPE_MAIN_VARIANT (selector_type);
7820 /* In ISO C terms, _Noreturn is not part of the type of expressions
7821 such as &abort, but in GCC it is represented internally as a type
7822 qualifier. */
7823 if (FUNCTION_POINTER_TYPE_P (selector_type)
7824 && TYPE_QUALS (TREE_TYPE (selector_type)) != TYPE_UNQUALIFIED)
7825 selector_type
7826 = build_pointer_type (TYPE_MAIN_VARIANT (TREE_TYPE (selector_type)));
7827
7828 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
7829 {
7830 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7831 return error_expr;
7832 }
7833
7834 auto_vec<c_generic_association> associations;
7835 while (1)
7836 {
7837 struct c_generic_association assoc, *iter;
7838 unsigned int ix;
7839 c_token *token = c_parser_peek_token (parser);
7840
7841 assoc.type_location = token->location;
7842 if (token->type == CPP_KEYWORD && token->keyword == RID_DEFAULT)
7843 {
7844 c_parser_consume_token (parser);
7845 assoc.type = NULL_TREE;
7846 }
7847 else
7848 {
7849 struct c_type_name *type_name;
7850
7851 type_name = c_parser_type_name (parser);
7852 if (type_name == NULL)
7853 {
7854 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7855 return error_expr;
7856 }
7857 assoc.type = groktypename (type_name, NULL, NULL);
7858 if (assoc.type == error_mark_node)
7859 {
7860 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7861 return error_expr;
7862 }
7863
7864 if (TREE_CODE (assoc.type) == FUNCTION_TYPE)
7865 error_at (assoc.type_location,
7866 "%<_Generic%> association has function type");
7867 else if (!COMPLETE_TYPE_P (assoc.type))
7868 error_at (assoc.type_location,
7869 "%<_Generic%> association has incomplete type");
7870
7871 if (variably_modified_type_p (assoc.type, NULL_TREE))
7872 error_at (assoc.type_location,
7873 "%<_Generic%> association has "
7874 "variable length type");
7875 }
7876
7877 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
7878 {
7879 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7880 return error_expr;
7881 }
7882
7883 assoc.expression = c_parser_expr_no_commas (parser, NULL);
7884 if (assoc.expression.value == error_mark_node)
7885 {
7886 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7887 return error_expr;
7888 }
7889
7890 for (ix = 0; associations.iterate (ix, &iter); ++ix)
7891 {
7892 if (assoc.type == NULL_TREE)
7893 {
7894 if (iter->type == NULL_TREE)
7895 {
7896 error_at (assoc.type_location,
7897 "duplicate %<default%> case in %<_Generic%>");
7898 inform (iter->type_location, "original %<default%> is here");
7899 }
7900 }
7901 else if (iter->type != NULL_TREE)
7902 {
7903 if (comptypes (assoc.type, iter->type))
7904 {
7905 error_at (assoc.type_location,
7906 "%<_Generic%> specifies two compatible types");
7907 inform (iter->type_location, "compatible type is here");
7908 }
7909 }
7910 }
7911
7912 if (assoc.type == NULL_TREE)
7913 {
7914 if (!match_found)
7915 {
7916 matched_assoc = assoc;
7917 match_found = true;
7918 }
7919 }
7920 else if (comptypes (assoc.type, selector_type))
7921 {
7922 if (!match_found || matched_assoc.type == NULL_TREE)
7923 {
7924 matched_assoc = assoc;
7925 match_found = true;
7926 }
7927 else
7928 {
7929 error_at (assoc.type_location,
7930 "%<_Generic%> selector matches multiple associations");
7931 inform (matched_assoc.type_location,
7932 "other match is here");
7933 }
7934 }
7935
7936 associations.safe_push (assoc);
7937
7938 if (c_parser_peek_token (parser)->type != CPP_COMMA)
7939 break;
7940 c_parser_consume_token (parser);
7941 }
7942
7943 if (!parens.require_close (parser))
7944 {
7945 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7946 return error_expr;
7947 }
7948
7949 if (!match_found)
7950 {
7951 error_at (selector_loc, "%<_Generic%> selector of type %qT is not "
7952 "compatible with any association",
7953 selector_type);
7954 return error_expr;
7955 }
7956
7957 return matched_assoc.expression;
7958 }
7959
7960 /* Check the validity of a function pointer argument *EXPR (argument
7961 position POS) to __builtin_tgmath. Return the number of function
7962 arguments if possibly valid; return 0 having reported an error if
7963 not valid. */
7964
7965 static unsigned int
7966 check_tgmath_function (c_expr *expr, unsigned int pos)
7967 {
7968 tree type = TREE_TYPE (expr->value);
7969 if (!FUNCTION_POINTER_TYPE_P (type))
7970 {
7971 error_at (expr->get_location (),
7972 "argument %u of %<__builtin_tgmath%> is not a function pointer",
7973 pos);
7974 return 0;
7975 }
7976 type = TREE_TYPE (type);
7977 if (!prototype_p (type))
7978 {
7979 error_at (expr->get_location (),
7980 "argument %u of %<__builtin_tgmath%> is unprototyped", pos);
7981 return 0;
7982 }
7983 if (stdarg_p (type))
7984 {
7985 error_at (expr->get_location (),
7986 "argument %u of %<__builtin_tgmath%> has variable arguments",
7987 pos);
7988 return 0;
7989 }
7990 unsigned int nargs = 0;
7991 function_args_iterator iter;
7992 tree t;
7993 FOREACH_FUNCTION_ARGS (type, t, iter)
7994 {
7995 if (t == void_type_node)
7996 break;
7997 nargs++;
7998 }
7999 if (nargs == 0)
8000 {
8001 error_at (expr->get_location (),
8002 "argument %u of %<__builtin_tgmath%> has no arguments", pos);
8003 return 0;
8004 }
8005 return nargs;
8006 }
8007
8008 /* Ways in which a parameter or return value of a type-generic macro
8009 may vary between the different functions the macro may call. */
8010 enum tgmath_parm_kind
8011 {
8012 tgmath_fixed, tgmath_real, tgmath_complex
8013 };
8014
8015 /* Parse a postfix expression (C90 6.3.1-6.3.2, C99 6.5.1-6.5.2,
8016 C11 6.5.1-6.5.2). Compound literals aren't handled here; callers have to
8017 call c_parser_postfix_expression_after_paren_type on encountering them.
8018
8019 postfix-expression:
8020 primary-expression
8021 postfix-expression [ expression ]
8022 postfix-expression ( argument-expression-list[opt] )
8023 postfix-expression . identifier
8024 postfix-expression -> identifier
8025 postfix-expression ++
8026 postfix-expression --
8027 ( type-name ) { initializer-list }
8028 ( type-name ) { initializer-list , }
8029
8030 argument-expression-list:
8031 argument-expression
8032 argument-expression-list , argument-expression
8033
8034 primary-expression:
8035 identifier
8036 constant
8037 string-literal
8038 ( expression )
8039 generic-selection
8040
8041 GNU extensions:
8042
8043 primary-expression:
8044 __func__
8045 (treated as a keyword in GNU C)
8046 __FUNCTION__
8047 __PRETTY_FUNCTION__
8048 ( compound-statement )
8049 __builtin_va_arg ( assignment-expression , type-name )
8050 __builtin_offsetof ( type-name , offsetof-member-designator )
8051 __builtin_choose_expr ( assignment-expression ,
8052 assignment-expression ,
8053 assignment-expression )
8054 __builtin_types_compatible_p ( type-name , type-name )
8055 __builtin_tgmath ( expr-list )
8056 __builtin_complex ( assignment-expression , assignment-expression )
8057 __builtin_shuffle ( assignment-expression , assignment-expression )
8058 __builtin_shuffle ( assignment-expression ,
8059 assignment-expression ,
8060 assignment-expression, )
8061 __builtin_convertvector ( assignment-expression , type-name )
8062
8063 offsetof-member-designator:
8064 identifier
8065 offsetof-member-designator . identifier
8066 offsetof-member-designator [ expression ]
8067
8068 Objective-C:
8069
8070 primary-expression:
8071 [ objc-receiver objc-message-args ]
8072 @selector ( objc-selector-arg )
8073 @protocol ( identifier )
8074 @encode ( type-name )
8075 objc-string-literal
8076 Classname . identifier
8077 */
8078
8079 static struct c_expr
8080 c_parser_postfix_expression (c_parser *parser)
8081 {
8082 struct c_expr expr, e1;
8083 struct c_type_name *t1, *t2;
8084 location_t loc = c_parser_peek_token (parser)->location;
8085 source_range tok_range = c_parser_peek_token (parser)->get_range ();
8086 expr.original_code = ERROR_MARK;
8087 expr.original_type = NULL;
8088 switch (c_parser_peek_token (parser)->type)
8089 {
8090 case CPP_NUMBER:
8091 expr.value = c_parser_peek_token (parser)->value;
8092 set_c_expr_source_range (&expr, tok_range);
8093 loc = c_parser_peek_token (parser)->location;
8094 c_parser_consume_token (parser);
8095 if (TREE_CODE (expr.value) == FIXED_CST
8096 && !targetm.fixed_point_supported_p ())
8097 {
8098 error_at (loc, "fixed-point types not supported for this target");
8099 expr.set_error ();
8100 }
8101 break;
8102 case CPP_CHAR:
8103 case CPP_CHAR16:
8104 case CPP_CHAR32:
8105 case CPP_WCHAR:
8106 expr.value = c_parser_peek_token (parser)->value;
8107 /* For the purpose of warning when a pointer is compared with
8108 a zero character constant. */
8109 expr.original_type = char_type_node;
8110 set_c_expr_source_range (&expr, tok_range);
8111 c_parser_consume_token (parser);
8112 break;
8113 case CPP_STRING:
8114 case CPP_STRING16:
8115 case CPP_STRING32:
8116 case CPP_WSTRING:
8117 case CPP_UTF8STRING:
8118 expr.value = c_parser_peek_token (parser)->value;
8119 set_c_expr_source_range (&expr, tok_range);
8120 expr.original_code = STRING_CST;
8121 c_parser_consume_token (parser);
8122 break;
8123 case CPP_OBJC_STRING:
8124 gcc_assert (c_dialect_objc ());
8125 expr.value
8126 = objc_build_string_object (c_parser_peek_token (parser)->value);
8127 set_c_expr_source_range (&expr, tok_range);
8128 c_parser_consume_token (parser);
8129 break;
8130 case CPP_NAME:
8131 switch (c_parser_peek_token (parser)->id_kind)
8132 {
8133 case C_ID_ID:
8134 {
8135 tree id = c_parser_peek_token (parser)->value;
8136 c_parser_consume_token (parser);
8137 expr.value = build_external_ref (loc, id,
8138 (c_parser_peek_token (parser)->type
8139 == CPP_OPEN_PAREN),
8140 &expr.original_type);
8141 set_c_expr_source_range (&expr, tok_range);
8142 break;
8143 }
8144 case C_ID_CLASSNAME:
8145 {
8146 /* Here we parse the Objective-C 2.0 Class.name dot
8147 syntax. */
8148 tree class_name = c_parser_peek_token (parser)->value;
8149 tree component;
8150 c_parser_consume_token (parser);
8151 gcc_assert (c_dialect_objc ());
8152 if (!c_parser_require (parser, CPP_DOT, "expected %<.%>"))
8153 {
8154 expr.set_error ();
8155 break;
8156 }
8157 if (c_parser_next_token_is_not (parser, CPP_NAME))
8158 {
8159 c_parser_error (parser, "expected identifier");
8160 expr.set_error ();
8161 break;
8162 }
8163 c_token *component_tok = c_parser_peek_token (parser);
8164 component = component_tok->value;
8165 location_t end_loc = component_tok->get_finish ();
8166 c_parser_consume_token (parser);
8167 expr.value = objc_build_class_component_ref (class_name,
8168 component);
8169 set_c_expr_source_range (&expr, loc, end_loc);
8170 break;
8171 }
8172 default:
8173 c_parser_error (parser, "expected expression");
8174 expr.set_error ();
8175 break;
8176 }
8177 break;
8178 case CPP_OPEN_PAREN:
8179 /* A parenthesized expression, statement expression or compound
8180 literal. */
8181 if (c_parser_peek_2nd_token (parser)->type == CPP_OPEN_BRACE)
8182 {
8183 /* A statement expression. */
8184 tree stmt;
8185 location_t brace_loc;
8186 c_parser_consume_token (parser);
8187 brace_loc = c_parser_peek_token (parser)->location;
8188 c_parser_consume_token (parser);
8189 /* If we've not yet started the current function's statement list,
8190 or we're in the parameter scope of an old-style function
8191 declaration, statement expressions are not allowed. */
8192 if (!building_stmt_list_p () || old_style_parameter_scope ())
8193 {
8194 error_at (loc, "braced-group within expression allowed "
8195 "only inside a function");
8196 parser->error = true;
8197 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
8198 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
8199 expr.set_error ();
8200 break;
8201 }
8202 stmt = c_begin_stmt_expr ();
8203 c_parser_compound_statement_nostart (parser);
8204 location_t close_loc = c_parser_peek_token (parser)->location;
8205 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
8206 "expected %<)%>");
8207 pedwarn (loc, OPT_Wpedantic,
8208 "ISO C forbids braced-groups within expressions");
8209 expr.value = c_finish_stmt_expr (brace_loc, stmt);
8210 set_c_expr_source_range (&expr, loc, close_loc);
8211 mark_exp_read (expr.value);
8212 }
8213 else
8214 {
8215 /* A parenthesized expression. */
8216 location_t loc_open_paren = c_parser_peek_token (parser)->location;
8217 c_parser_consume_token (parser);
8218 expr = c_parser_expression (parser);
8219 if (TREE_CODE (expr.value) == MODIFY_EXPR)
8220 TREE_NO_WARNING (expr.value) = 1;
8221 if (expr.original_code != C_MAYBE_CONST_EXPR
8222 && expr.original_code != SIZEOF_EXPR)
8223 expr.original_code = ERROR_MARK;
8224 /* Don't change EXPR.ORIGINAL_TYPE. */
8225 location_t loc_close_paren = c_parser_peek_token (parser)->location;
8226 set_c_expr_source_range (&expr, loc_open_paren, loc_close_paren);
8227 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
8228 "expected %<)%>", loc_open_paren);
8229 }
8230 break;
8231 case CPP_KEYWORD:
8232 switch (c_parser_peek_token (parser)->keyword)
8233 {
8234 case RID_FUNCTION_NAME:
8235 pedwarn (loc, OPT_Wpedantic, "ISO C does not support "
8236 "%<__FUNCTION__%> predefined identifier");
8237 expr.value = fname_decl (loc,
8238 c_parser_peek_token (parser)->keyword,
8239 c_parser_peek_token (parser)->value);
8240 set_c_expr_source_range (&expr, loc, loc);
8241 c_parser_consume_token (parser);
8242 break;
8243 case RID_PRETTY_FUNCTION_NAME:
8244 pedwarn (loc, OPT_Wpedantic, "ISO C does not support "
8245 "%<__PRETTY_FUNCTION__%> predefined identifier");
8246 expr.value = fname_decl (loc,
8247 c_parser_peek_token (parser)->keyword,
8248 c_parser_peek_token (parser)->value);
8249 set_c_expr_source_range (&expr, loc, loc);
8250 c_parser_consume_token (parser);
8251 break;
8252 case RID_C99_FUNCTION_NAME:
8253 pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not support "
8254 "%<__func__%> predefined identifier");
8255 expr.value = fname_decl (loc,
8256 c_parser_peek_token (parser)->keyword,
8257 c_parser_peek_token (parser)->value);
8258 set_c_expr_source_range (&expr, loc, loc);
8259 c_parser_consume_token (parser);
8260 break;
8261 case RID_VA_ARG:
8262 {
8263 location_t start_loc = loc;
8264 c_parser_consume_token (parser);
8265 matching_parens parens;
8266 if (!parens.require_open (parser))
8267 {
8268 expr.set_error ();
8269 break;
8270 }
8271 e1 = c_parser_expr_no_commas (parser, NULL);
8272 mark_exp_read (e1.value);
8273 e1.value = c_fully_fold (e1.value, false, NULL);
8274 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
8275 {
8276 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
8277 expr.set_error ();
8278 break;
8279 }
8280 loc = c_parser_peek_token (parser)->location;
8281 t1 = c_parser_type_name (parser);
8282 location_t end_loc = c_parser_peek_token (parser)->get_finish ();
8283 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
8284 "expected %<)%>");
8285 if (t1 == NULL)
8286 {
8287 expr.set_error ();
8288 }
8289 else
8290 {
8291 tree type_expr = NULL_TREE;
8292 expr.value = c_build_va_arg (start_loc, e1.value, loc,
8293 groktypename (t1, &type_expr, NULL));
8294 if (type_expr)
8295 {
8296 expr.value = build2 (C_MAYBE_CONST_EXPR,
8297 TREE_TYPE (expr.value), type_expr,
8298 expr.value);
8299 C_MAYBE_CONST_EXPR_NON_CONST (expr.value) = true;
8300 }
8301 set_c_expr_source_range (&expr, start_loc, end_loc);
8302 }
8303 }
8304 break;
8305 case RID_OFFSETOF:
8306 {
8307 c_parser_consume_token (parser);
8308 matching_parens parens;
8309 if (!parens.require_open (parser))
8310 {
8311 expr.set_error ();
8312 break;
8313 }
8314 t1 = c_parser_type_name (parser);
8315 if (t1 == NULL)
8316 parser->error = true;
8317 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
8318 gcc_assert (parser->error);
8319 if (parser->error)
8320 {
8321 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
8322 expr.set_error ();
8323 break;
8324 }
8325 tree type = groktypename (t1, NULL, NULL);
8326 tree offsetof_ref;
8327 if (type == error_mark_node)
8328 offsetof_ref = error_mark_node;
8329 else
8330 {
8331 offsetof_ref = build1 (INDIRECT_REF, type, null_pointer_node);
8332 SET_EXPR_LOCATION (offsetof_ref, loc);
8333 }
8334 /* Parse the second argument to __builtin_offsetof. We
8335 must have one identifier, and beyond that we want to
8336 accept sub structure and sub array references. */
8337 if (c_parser_next_token_is (parser, CPP_NAME))
8338 {
8339 c_token *comp_tok = c_parser_peek_token (parser);
8340 offsetof_ref = build_component_ref
8341 (loc, offsetof_ref, comp_tok->value, comp_tok->location);
8342 c_parser_consume_token (parser);
8343 while (c_parser_next_token_is (parser, CPP_DOT)
8344 || c_parser_next_token_is (parser,
8345 CPP_OPEN_SQUARE)
8346 || c_parser_next_token_is (parser,
8347 CPP_DEREF))
8348 {
8349 if (c_parser_next_token_is (parser, CPP_DEREF))
8350 {
8351 loc = c_parser_peek_token (parser)->location;
8352 offsetof_ref = build_array_ref (loc,
8353 offsetof_ref,
8354 integer_zero_node);
8355 goto do_dot;
8356 }
8357 else if (c_parser_next_token_is (parser, CPP_DOT))
8358 {
8359 do_dot:
8360 c_parser_consume_token (parser);
8361 if (c_parser_next_token_is_not (parser,
8362 CPP_NAME))
8363 {
8364 c_parser_error (parser, "expected identifier");
8365 break;
8366 }
8367 c_token *comp_tok = c_parser_peek_token (parser);
8368 offsetof_ref = build_component_ref
8369 (loc, offsetof_ref, comp_tok->value,
8370 comp_tok->location);
8371 c_parser_consume_token (parser);
8372 }
8373 else
8374 {
8375 struct c_expr ce;
8376 tree idx;
8377 loc = c_parser_peek_token (parser)->location;
8378 c_parser_consume_token (parser);
8379 ce = c_parser_expression (parser);
8380 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
8381 idx = ce.value;
8382 idx = c_fully_fold (idx, false, NULL);
8383 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
8384 "expected %<]%>");
8385 offsetof_ref = build_array_ref (loc, offsetof_ref, idx);
8386 }
8387 }
8388 }
8389 else
8390 c_parser_error (parser, "expected identifier");
8391 location_t end_loc = c_parser_peek_token (parser)->get_finish ();
8392 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
8393 "expected %<)%>");
8394 expr.value = fold_offsetof (offsetof_ref);
8395 set_c_expr_source_range (&expr, loc, end_loc);
8396 }
8397 break;
8398 case RID_CHOOSE_EXPR:
8399 {
8400 vec<c_expr_t, va_gc> *cexpr_list;
8401 c_expr_t *e1_p, *e2_p, *e3_p;
8402 tree c;
8403 location_t close_paren_loc;
8404
8405 c_parser_consume_token (parser);
8406 if (!c_parser_get_builtin_args (parser,
8407 "__builtin_choose_expr",
8408 &cexpr_list, true,
8409 &close_paren_loc))
8410 {
8411 expr.set_error ();
8412 break;
8413 }
8414
8415 if (vec_safe_length (cexpr_list) != 3)
8416 {
8417 error_at (loc, "wrong number of arguments to "
8418 "%<__builtin_choose_expr%>");
8419 expr.set_error ();
8420 break;
8421 }
8422
8423 e1_p = &(*cexpr_list)[0];
8424 e2_p = &(*cexpr_list)[1];
8425 e3_p = &(*cexpr_list)[2];
8426
8427 c = e1_p->value;
8428 mark_exp_read (e2_p->value);
8429 mark_exp_read (e3_p->value);
8430 if (TREE_CODE (c) != INTEGER_CST
8431 || !INTEGRAL_TYPE_P (TREE_TYPE (c)))
8432 error_at (loc,
8433 "first argument to %<__builtin_choose_expr%> not"
8434 " a constant");
8435 constant_expression_warning (c);
8436 expr = integer_zerop (c) ? *e3_p : *e2_p;
8437 set_c_expr_source_range (&expr, loc, close_paren_loc);
8438 break;
8439 }
8440 case RID_TYPES_COMPATIBLE_P:
8441 {
8442 c_parser_consume_token (parser);
8443 matching_parens parens;
8444 if (!parens.require_open (parser))
8445 {
8446 expr.set_error ();
8447 break;
8448 }
8449 t1 = c_parser_type_name (parser);
8450 if (t1 == NULL)
8451 {
8452 expr.set_error ();
8453 break;
8454 }
8455 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
8456 {
8457 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
8458 expr.set_error ();
8459 break;
8460 }
8461 t2 = c_parser_type_name (parser);
8462 if (t2 == NULL)
8463 {
8464 expr.set_error ();
8465 break;
8466 }
8467 location_t close_paren_loc = c_parser_peek_token (parser)->location;
8468 parens.skip_until_found_close (parser);
8469 tree e1, e2;
8470 e1 = groktypename (t1, NULL, NULL);
8471 e2 = groktypename (t2, NULL, NULL);
8472 if (e1 == error_mark_node || e2 == error_mark_node)
8473 {
8474 expr.set_error ();
8475 break;
8476 }
8477
8478 e1 = TYPE_MAIN_VARIANT (e1);
8479 e2 = TYPE_MAIN_VARIANT (e2);
8480
8481 expr.value
8482 = comptypes (e1, e2) ? integer_one_node : integer_zero_node;
8483 set_c_expr_source_range (&expr, loc, close_paren_loc);
8484 }
8485 break;
8486 case RID_BUILTIN_TGMATH:
8487 {
8488 vec<c_expr_t, va_gc> *cexpr_list;
8489 location_t close_paren_loc;
8490
8491 c_parser_consume_token (parser);
8492 if (!c_parser_get_builtin_args (parser,
8493 "__builtin_tgmath",
8494 &cexpr_list, false,
8495 &close_paren_loc))
8496 {
8497 expr.set_error ();
8498 break;
8499 }
8500
8501 if (vec_safe_length (cexpr_list) < 3)
8502 {
8503 error_at (loc, "too few arguments to %<__builtin_tgmath%>");
8504 expr.set_error ();
8505 break;
8506 }
8507
8508 unsigned int i;
8509 c_expr_t *p;
8510 FOR_EACH_VEC_ELT (*cexpr_list, i, p)
8511 *p = convert_lvalue_to_rvalue (loc, *p, true, true);
8512 unsigned int nargs = check_tgmath_function (&(*cexpr_list)[0], 1);
8513 if (nargs == 0)
8514 {
8515 expr.set_error ();
8516 break;
8517 }
8518 if (vec_safe_length (cexpr_list) < nargs)
8519 {
8520 error_at (loc, "too few arguments to %<__builtin_tgmath%>");
8521 expr.set_error ();
8522 break;
8523 }
8524 unsigned int num_functions = vec_safe_length (cexpr_list) - nargs;
8525 if (num_functions < 2)
8526 {
8527 error_at (loc, "too few arguments to %<__builtin_tgmath%>");
8528 expr.set_error ();
8529 break;
8530 }
8531
8532 /* The first NUM_FUNCTIONS expressions are the function
8533 pointers. The remaining NARGS expressions are the
8534 arguments that are to be passed to one of those
8535 functions, chosen following <tgmath.h> rules. */
8536 for (unsigned int j = 1; j < num_functions; j++)
8537 {
8538 unsigned int this_nargs
8539 = check_tgmath_function (&(*cexpr_list)[j], j + 1);
8540 if (this_nargs == 0)
8541 {
8542 expr.set_error ();
8543 goto out;
8544 }
8545 if (this_nargs != nargs)
8546 {
8547 error_at ((*cexpr_list)[j].get_location (),
8548 "argument %u of %<__builtin_tgmath%> has "
8549 "wrong number of arguments", j + 1);
8550 expr.set_error ();
8551 goto out;
8552 }
8553 }
8554
8555 /* The functions all have the same number of arguments.
8556 Determine whether arguments and return types vary in
8557 ways permitted for <tgmath.h> functions. */
8558 /* The first entry in each of these vectors is for the
8559 return type, subsequent entries for parameter
8560 types. */
8561 auto_vec<enum tgmath_parm_kind> parm_kind (nargs + 1);
8562 auto_vec<tree> parm_first (nargs + 1);
8563 auto_vec<bool> parm_complex (nargs + 1);
8564 auto_vec<bool> parm_varies (nargs + 1);
8565 tree first_type = TREE_TYPE (TREE_TYPE ((*cexpr_list)[0].value));
8566 tree first_ret = TYPE_MAIN_VARIANT (TREE_TYPE (first_type));
8567 parm_first.quick_push (first_ret);
8568 parm_complex.quick_push (TREE_CODE (first_ret) == COMPLEX_TYPE);
8569 parm_varies.quick_push (false);
8570 function_args_iterator iter;
8571 tree t;
8572 unsigned int argpos;
8573 FOREACH_FUNCTION_ARGS (first_type, t, iter)
8574 {
8575 if (t == void_type_node)
8576 break;
8577 parm_first.quick_push (TYPE_MAIN_VARIANT (t));
8578 parm_complex.quick_push (TREE_CODE (t) == COMPLEX_TYPE);
8579 parm_varies.quick_push (false);
8580 }
8581 for (unsigned int j = 1; j < num_functions; j++)
8582 {
8583 tree type = TREE_TYPE (TREE_TYPE ((*cexpr_list)[j].value));
8584 tree ret = TYPE_MAIN_VARIANT (TREE_TYPE (type));
8585 if (ret != parm_first[0])
8586 {
8587 parm_varies[0] = true;
8588 if (!SCALAR_FLOAT_TYPE_P (parm_first[0])
8589 && !COMPLEX_FLOAT_TYPE_P (parm_first[0]))
8590 {
8591 error_at ((*cexpr_list)[0].get_location (),
8592 "invalid type-generic return type for "
8593 "argument %u of %<__builtin_tgmath%>",
8594 1);
8595 expr.set_error ();
8596 goto out;
8597 }
8598 if (!SCALAR_FLOAT_TYPE_P (ret)
8599 && !COMPLEX_FLOAT_TYPE_P (ret))
8600 {
8601 error_at ((*cexpr_list)[j].get_location (),
8602 "invalid type-generic return type for "
8603 "argument %u of %<__builtin_tgmath%>",
8604 j + 1);
8605 expr.set_error ();
8606 goto out;
8607 }
8608 }
8609 if (TREE_CODE (ret) == COMPLEX_TYPE)
8610 parm_complex[0] = true;
8611 argpos = 1;
8612 FOREACH_FUNCTION_ARGS (type, t, iter)
8613 {
8614 if (t == void_type_node)
8615 break;
8616 t = TYPE_MAIN_VARIANT (t);
8617 if (t != parm_first[argpos])
8618 {
8619 parm_varies[argpos] = true;
8620 if (!SCALAR_FLOAT_TYPE_P (parm_first[argpos])
8621 && !COMPLEX_FLOAT_TYPE_P (parm_first[argpos]))
8622 {
8623 error_at ((*cexpr_list)[0].get_location (),
8624 "invalid type-generic type for "
8625 "argument %u of argument %u of "
8626 "%<__builtin_tgmath%>", argpos, 1);
8627 expr.set_error ();
8628 goto out;
8629 }
8630 if (!SCALAR_FLOAT_TYPE_P (t)
8631 && !COMPLEX_FLOAT_TYPE_P (t))
8632 {
8633 error_at ((*cexpr_list)[j].get_location (),
8634 "invalid type-generic type for "
8635 "argument %u of argument %u of "
8636 "%<__builtin_tgmath%>", argpos, j + 1);
8637 expr.set_error ();
8638 goto out;
8639 }
8640 }
8641 if (TREE_CODE (t) == COMPLEX_TYPE)
8642 parm_complex[argpos] = true;
8643 argpos++;
8644 }
8645 }
8646 enum tgmath_parm_kind max_variation = tgmath_fixed;
8647 for (unsigned int j = 0; j <= nargs; j++)
8648 {
8649 enum tgmath_parm_kind this_kind;
8650 if (parm_varies[j])
8651 {
8652 if (parm_complex[j])
8653 max_variation = this_kind = tgmath_complex;
8654 else
8655 {
8656 this_kind = tgmath_real;
8657 if (max_variation != tgmath_complex)
8658 max_variation = tgmath_real;
8659 }
8660 }
8661 else
8662 this_kind = tgmath_fixed;
8663 parm_kind.quick_push (this_kind);
8664 }
8665 if (max_variation == tgmath_fixed)
8666 {
8667 error_at (loc, "function arguments of %<__builtin_tgmath%> "
8668 "all have the same type");
8669 expr.set_error ();
8670 break;
8671 }
8672
8673 /* Identify a parameter (not the return type) that varies,
8674 including with complex types if any variation includes
8675 complex types; there must be at least one such
8676 parameter. */
8677 unsigned int tgarg = 0;
8678 for (unsigned int j = 1; j <= nargs; j++)
8679 if (parm_kind[j] == max_variation)
8680 {
8681 tgarg = j;
8682 break;
8683 }
8684 if (tgarg == 0)
8685 {
8686 error_at (loc, "function arguments of %<__builtin_tgmath%> "
8687 "lack type-generic parameter");
8688 expr.set_error ();
8689 break;
8690 }
8691
8692 /* Determine the type of the relevant parameter for each
8693 function. */
8694 auto_vec<tree> tg_type (num_functions);
8695 for (unsigned int j = 0; j < num_functions; j++)
8696 {
8697 tree type = TREE_TYPE (TREE_TYPE ((*cexpr_list)[j].value));
8698 argpos = 1;
8699 FOREACH_FUNCTION_ARGS (type, t, iter)
8700 {
8701 if (argpos == tgarg)
8702 {
8703 tg_type.quick_push (TYPE_MAIN_VARIANT (t));
8704 break;
8705 }
8706 argpos++;
8707 }
8708 }
8709
8710 /* Verify that the corresponding types are different for
8711 all the listed functions. Also determine whether all
8712 the types are complex, whether all the types are
8713 standard or binary, and whether all the types are
8714 decimal. */
8715 bool all_complex = true;
8716 bool all_binary = true;
8717 bool all_decimal = true;
8718 hash_set<tree> tg_types;
8719 FOR_EACH_VEC_ELT (tg_type, i, t)
8720 {
8721 if (TREE_CODE (t) == COMPLEX_TYPE)
8722 all_decimal = false;
8723 else
8724 {
8725 all_complex = false;
8726 if (DECIMAL_FLOAT_TYPE_P (t))
8727 all_binary = false;
8728 else
8729 all_decimal = false;
8730 }
8731 if (tg_types.add (t))
8732 {
8733 error_at ((*cexpr_list)[i].get_location (),
8734 "duplicate type-generic parameter type for "
8735 "function argument %u of %<__builtin_tgmath%>",
8736 i + 1);
8737 expr.set_error ();
8738 goto out;
8739 }
8740 }
8741
8742 /* Verify that other parameters and the return type whose
8743 types vary have their types varying in the correct
8744 way. */
8745 for (unsigned int j = 0; j < num_functions; j++)
8746 {
8747 tree exp_type = tg_type[j];
8748 tree exp_real_type = exp_type;
8749 if (TREE_CODE (exp_type) == COMPLEX_TYPE)
8750 exp_real_type = TREE_TYPE (exp_type);
8751 tree type = TREE_TYPE (TREE_TYPE ((*cexpr_list)[j].value));
8752 tree ret = TYPE_MAIN_VARIANT (TREE_TYPE (type));
8753 if ((parm_kind[0] == tgmath_complex && ret != exp_type)
8754 || (parm_kind[0] == tgmath_real && ret != exp_real_type))
8755 {
8756 error_at ((*cexpr_list)[j].get_location (),
8757 "bad return type for function argument %u "
8758 "of %<__builtin_tgmath%>", j + 1);
8759 expr.set_error ();
8760 goto out;
8761 }
8762 argpos = 1;
8763 FOREACH_FUNCTION_ARGS (type, t, iter)
8764 {
8765 if (t == void_type_node)
8766 break;
8767 t = TYPE_MAIN_VARIANT (t);
8768 if ((parm_kind[argpos] == tgmath_complex
8769 && t != exp_type)
8770 || (parm_kind[argpos] == tgmath_real
8771 && t != exp_real_type))
8772 {
8773 error_at ((*cexpr_list)[j].get_location (),
8774 "bad type for argument %u of "
8775 "function argument %u of "
8776 "%<__builtin_tgmath%>", argpos, j + 1);
8777 expr.set_error ();
8778 goto out;
8779 }
8780 argpos++;
8781 }
8782 }
8783
8784 /* The functions listed are a valid set of functions for a
8785 <tgmath.h> macro to select between. Identify the
8786 matching function, if any. First, the argument types
8787 must be combined following <tgmath.h> rules. Integer
8788 types are treated as _Decimal64 if any type-generic
8789 argument is decimal, or if the only alternatives for
8790 type-generic arguments are of decimal types, and are
8791 otherwise treated as double (or _Complex double for
8792 complex integer types, or _Float64 or _Complex _Float64
8793 if all the return types are the same _FloatN or
8794 _FloatNx type). After that adjustment, types are
8795 combined following the usual arithmetic conversions.
8796 If the function only accepts complex arguments, a
8797 complex type is produced. */
8798 bool arg_complex = all_complex;
8799 bool arg_binary = all_binary;
8800 bool arg_int_decimal = all_decimal;
8801 for (unsigned int j = 1; j <= nargs; j++)
8802 {
8803 if (parm_kind[j] == tgmath_fixed)
8804 continue;
8805 c_expr_t *ce = &(*cexpr_list)[num_functions + j - 1];
8806 tree type = TREE_TYPE (ce->value);
8807 if (!INTEGRAL_TYPE_P (type)
8808 && !SCALAR_FLOAT_TYPE_P (type)
8809 && TREE_CODE (type) != COMPLEX_TYPE)
8810 {
8811 error_at (ce->get_location (),
8812 "invalid type of argument %u of type-generic "
8813 "function", j);
8814 expr.set_error ();
8815 goto out;
8816 }
8817 if (DECIMAL_FLOAT_TYPE_P (type))
8818 {
8819 arg_int_decimal = true;
8820 if (all_complex)
8821 {
8822 error_at (ce->get_location (),
8823 "decimal floating-point argument %u to "
8824 "complex-only type-generic function", j);
8825 expr.set_error ();
8826 goto out;
8827 }
8828 else if (all_binary)
8829 {
8830 error_at (ce->get_location (),
8831 "decimal floating-point argument %u to "
8832 "binary-only type-generic function", j);
8833 expr.set_error ();
8834 goto out;
8835 }
8836 else if (arg_complex)
8837 {
8838 error_at (ce->get_location (),
8839 "both complex and decimal floating-point "
8840 "arguments to type-generic function");
8841 expr.set_error ();
8842 goto out;
8843 }
8844 else if (arg_binary)
8845 {
8846 error_at (ce->get_location (),
8847 "both binary and decimal floating-point "
8848 "arguments to type-generic function");
8849 expr.set_error ();
8850 goto out;
8851 }
8852 }
8853 else if (TREE_CODE (type) == COMPLEX_TYPE)
8854 {
8855 arg_complex = true;
8856 if (COMPLEX_FLOAT_TYPE_P (type))
8857 arg_binary = true;
8858 if (all_decimal)
8859 {
8860 error_at (ce->get_location (),
8861 "complex argument %u to "
8862 "decimal-only type-generic function", j);
8863 expr.set_error ();
8864 goto out;
8865 }
8866 else if (arg_int_decimal)
8867 {
8868 error_at (ce->get_location (),
8869 "both complex and decimal floating-point "
8870 "arguments to type-generic function");
8871 expr.set_error ();
8872 goto out;
8873 }
8874 }
8875 else if (SCALAR_FLOAT_TYPE_P (type))
8876 {
8877 arg_binary = true;
8878 if (all_decimal)
8879 {
8880 error_at (ce->get_location (),
8881 "binary argument %u to "
8882 "decimal-only type-generic function", j);
8883 expr.set_error ();
8884 goto out;
8885 }
8886 else if (arg_int_decimal)
8887 {
8888 error_at (ce->get_location (),
8889 "both binary and decimal floating-point "
8890 "arguments to type-generic function");
8891 expr.set_error ();
8892 goto out;
8893 }
8894 }
8895 }
8896 /* For a macro rounding its result to a narrower type, map
8897 integer types to _Float64 not double if the return type
8898 is a _FloatN or _FloatNx type. */
8899 bool arg_int_float64 = false;
8900 if (parm_kind[0] == tgmath_fixed
8901 && SCALAR_FLOAT_TYPE_P (parm_first[0])
8902 && float64_type_node != NULL_TREE)
8903 for (unsigned int j = 0; j < NUM_FLOATN_NX_TYPES; j++)
8904 if (parm_first[0] == FLOATN_TYPE_NODE (j))
8905 {
8906 arg_int_float64 = true;
8907 break;
8908 }
8909 tree arg_real = NULL_TREE;
8910 for (unsigned int j = 1; j <= nargs; j++)
8911 {
8912 if (parm_kind[j] == tgmath_fixed)
8913 continue;
8914 c_expr_t *ce = &(*cexpr_list)[num_functions + j - 1];
8915 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (ce->value));
8916 if (TREE_CODE (type) == COMPLEX_TYPE)
8917 type = TREE_TYPE (type);
8918 if (INTEGRAL_TYPE_P (type))
8919 type = (arg_int_decimal
8920 ? dfloat64_type_node
8921 : arg_int_float64
8922 ? float64_type_node
8923 : double_type_node);
8924 if (arg_real == NULL_TREE)
8925 arg_real = type;
8926 else
8927 arg_real = common_type (arg_real, type);
8928 if (arg_real == error_mark_node)
8929 {
8930 expr.set_error ();
8931 goto out;
8932 }
8933 }
8934 tree arg_type = (arg_complex
8935 ? build_complex_type (arg_real)
8936 : arg_real);
8937
8938 /* Look for a function to call with type-generic parameter
8939 type ARG_TYPE. */
8940 c_expr_t *fn = NULL;
8941 for (unsigned int j = 0; j < num_functions; j++)
8942 {
8943 if (tg_type[j] == arg_type)
8944 {
8945 fn = &(*cexpr_list)[j];
8946 break;
8947 }
8948 }
8949 if (fn == NULL
8950 && parm_kind[0] == tgmath_fixed
8951 && SCALAR_FLOAT_TYPE_P (parm_first[0]))
8952 {
8953 /* Presume this is a macro that rounds its result to a
8954 narrower type, and look for the first function with
8955 at least the range and precision of the argument
8956 type. */
8957 for (unsigned int j = 0; j < num_functions; j++)
8958 {
8959 if (arg_complex
8960 != (TREE_CODE (tg_type[j]) == COMPLEX_TYPE))
8961 continue;
8962 tree real_tg_type = (arg_complex
8963 ? TREE_TYPE (tg_type[j])
8964 : tg_type[j]);
8965 if (DECIMAL_FLOAT_TYPE_P (arg_real)
8966 != DECIMAL_FLOAT_TYPE_P (real_tg_type))
8967 continue;
8968 scalar_float_mode arg_mode
8969 = SCALAR_FLOAT_TYPE_MODE (arg_real);
8970 scalar_float_mode tg_mode
8971 = SCALAR_FLOAT_TYPE_MODE (real_tg_type);
8972 const real_format *arg_fmt = REAL_MODE_FORMAT (arg_mode);
8973 const real_format *tg_fmt = REAL_MODE_FORMAT (tg_mode);
8974 if (arg_fmt->b == tg_fmt->b
8975 && arg_fmt->p <= tg_fmt->p
8976 && arg_fmt->emax <= tg_fmt->emax
8977 && (arg_fmt->emin - arg_fmt->p
8978 >= tg_fmt->emin - tg_fmt->p))
8979 {
8980 fn = &(*cexpr_list)[j];
8981 break;
8982 }
8983 }
8984 }
8985 if (fn == NULL)
8986 {
8987 error_at (loc, "no matching function for type-generic call");
8988 expr.set_error ();
8989 break;
8990 }
8991
8992 /* Construct a call to FN. */
8993 vec<tree, va_gc> *args;
8994 vec_alloc (args, nargs);
8995 vec<tree, va_gc> *origtypes;
8996 vec_alloc (origtypes, nargs);
8997 auto_vec<location_t> arg_loc (nargs);
8998 for (unsigned int j = 0; j < nargs; j++)
8999 {
9000 c_expr_t *ce = &(*cexpr_list)[num_functions + j];
9001 args->quick_push (ce->value);
9002 arg_loc.quick_push (ce->get_location ());
9003 origtypes->quick_push (ce->original_type);
9004 }
9005 expr.value = c_build_function_call_vec (loc, arg_loc, fn->value,
9006 args, origtypes);
9007 set_c_expr_source_range (&expr, loc, close_paren_loc);
9008 break;
9009 }
9010 case RID_BUILTIN_CALL_WITH_STATIC_CHAIN:
9011 {
9012 vec<c_expr_t, va_gc> *cexpr_list;
9013 c_expr_t *e2_p;
9014 tree chain_value;
9015 location_t close_paren_loc;
9016
9017 c_parser_consume_token (parser);
9018 if (!c_parser_get_builtin_args (parser,
9019 "__builtin_call_with_static_chain",
9020 &cexpr_list, false,
9021 &close_paren_loc))
9022 {
9023 expr.set_error ();
9024 break;
9025 }
9026 if (vec_safe_length (cexpr_list) != 2)
9027 {
9028 error_at (loc, "wrong number of arguments to "
9029 "%<__builtin_call_with_static_chain%>");
9030 expr.set_error ();
9031 break;
9032 }
9033
9034 expr = (*cexpr_list)[0];
9035 e2_p = &(*cexpr_list)[1];
9036 *e2_p = convert_lvalue_to_rvalue (loc, *e2_p, true, true);
9037 chain_value = e2_p->value;
9038 mark_exp_read (chain_value);
9039
9040 if (TREE_CODE (expr.value) != CALL_EXPR)
9041 error_at (loc, "first argument to "
9042 "%<__builtin_call_with_static_chain%> "
9043 "must be a call expression");
9044 else if (TREE_CODE (TREE_TYPE (chain_value)) != POINTER_TYPE)
9045 error_at (loc, "second argument to "
9046 "%<__builtin_call_with_static_chain%> "
9047 "must be a pointer type");
9048 else
9049 CALL_EXPR_STATIC_CHAIN (expr.value) = chain_value;
9050 set_c_expr_source_range (&expr, loc, close_paren_loc);
9051 break;
9052 }
9053 case RID_BUILTIN_COMPLEX:
9054 {
9055 vec<c_expr_t, va_gc> *cexpr_list;
9056 c_expr_t *e1_p, *e2_p;
9057 location_t close_paren_loc;
9058
9059 c_parser_consume_token (parser);
9060 if (!c_parser_get_builtin_args (parser,
9061 "__builtin_complex",
9062 &cexpr_list, false,
9063 &close_paren_loc))
9064 {
9065 expr.set_error ();
9066 break;
9067 }
9068
9069 if (vec_safe_length (cexpr_list) != 2)
9070 {
9071 error_at (loc, "wrong number of arguments to "
9072 "%<__builtin_complex%>");
9073 expr.set_error ();
9074 break;
9075 }
9076
9077 e1_p = &(*cexpr_list)[0];
9078 e2_p = &(*cexpr_list)[1];
9079
9080 *e1_p = convert_lvalue_to_rvalue (loc, *e1_p, true, true);
9081 if (TREE_CODE (e1_p->value) == EXCESS_PRECISION_EXPR)
9082 e1_p->value = convert (TREE_TYPE (e1_p->value),
9083 TREE_OPERAND (e1_p->value, 0));
9084 *e2_p = convert_lvalue_to_rvalue (loc, *e2_p, true, true);
9085 if (TREE_CODE (e2_p->value) == EXCESS_PRECISION_EXPR)
9086 e2_p->value = convert (TREE_TYPE (e2_p->value),
9087 TREE_OPERAND (e2_p->value, 0));
9088 if (!SCALAR_FLOAT_TYPE_P (TREE_TYPE (e1_p->value))
9089 || DECIMAL_FLOAT_TYPE_P (TREE_TYPE (e1_p->value))
9090 || !SCALAR_FLOAT_TYPE_P (TREE_TYPE (e2_p->value))
9091 || DECIMAL_FLOAT_TYPE_P (TREE_TYPE (e2_p->value)))
9092 {
9093 error_at (loc, "%<__builtin_complex%> operand "
9094 "not of real binary floating-point type");
9095 expr.set_error ();
9096 break;
9097 }
9098 if (TYPE_MAIN_VARIANT (TREE_TYPE (e1_p->value))
9099 != TYPE_MAIN_VARIANT (TREE_TYPE (e2_p->value)))
9100 {
9101 error_at (loc,
9102 "%<__builtin_complex%> operands of different types");
9103 expr.set_error ();
9104 break;
9105 }
9106 pedwarn_c90 (loc, OPT_Wpedantic,
9107 "ISO C90 does not support complex types");
9108 expr.value = build2_loc (loc, COMPLEX_EXPR,
9109 build_complex_type
9110 (TYPE_MAIN_VARIANT
9111 (TREE_TYPE (e1_p->value))),
9112 e1_p->value, e2_p->value);
9113 set_c_expr_source_range (&expr, loc, close_paren_loc);
9114 break;
9115 }
9116 case RID_BUILTIN_SHUFFLE:
9117 {
9118 vec<c_expr_t, va_gc> *cexpr_list;
9119 unsigned int i;
9120 c_expr_t *p;
9121 location_t close_paren_loc;
9122
9123 c_parser_consume_token (parser);
9124 if (!c_parser_get_builtin_args (parser,
9125 "__builtin_shuffle",
9126 &cexpr_list, false,
9127 &close_paren_loc))
9128 {
9129 expr.set_error ();
9130 break;
9131 }
9132
9133 FOR_EACH_VEC_SAFE_ELT (cexpr_list, i, p)
9134 *p = convert_lvalue_to_rvalue (loc, *p, true, true);
9135
9136 if (vec_safe_length (cexpr_list) == 2)
9137 expr.value = c_build_vec_perm_expr (loc, (*cexpr_list)[0].value,
9138 NULL_TREE,
9139 (*cexpr_list)[1].value);
9140
9141 else if (vec_safe_length (cexpr_list) == 3)
9142 expr.value = c_build_vec_perm_expr (loc, (*cexpr_list)[0].value,
9143 (*cexpr_list)[1].value,
9144 (*cexpr_list)[2].value);
9145 else
9146 {
9147 error_at (loc, "wrong number of arguments to "
9148 "%<__builtin_shuffle%>");
9149 expr.set_error ();
9150 }
9151 set_c_expr_source_range (&expr, loc, close_paren_loc);
9152 break;
9153 }
9154 case RID_BUILTIN_CONVERTVECTOR:
9155 {
9156 location_t start_loc = loc;
9157 c_parser_consume_token (parser);
9158 matching_parens parens;
9159 if (!parens.require_open (parser))
9160 {
9161 expr.set_error ();
9162 break;
9163 }
9164 e1 = c_parser_expr_no_commas (parser, NULL);
9165 mark_exp_read (e1.value);
9166 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
9167 {
9168 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
9169 expr.set_error ();
9170 break;
9171 }
9172 loc = c_parser_peek_token (parser)->location;
9173 t1 = c_parser_type_name (parser);
9174 location_t end_loc = c_parser_peek_token (parser)->get_finish ();
9175 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
9176 "expected %<)%>");
9177 if (t1 == NULL)
9178 expr.set_error ();
9179 else
9180 {
9181 tree type_expr = NULL_TREE;
9182 expr.value = c_build_vec_convert (start_loc, e1.value, loc,
9183 groktypename (t1, &type_expr,
9184 NULL));
9185 set_c_expr_source_range (&expr, start_loc, end_loc);
9186 }
9187 }
9188 break;
9189 case RID_AT_SELECTOR:
9190 {
9191 gcc_assert (c_dialect_objc ());
9192 c_parser_consume_token (parser);
9193 matching_parens parens;
9194 if (!parens.require_open (parser))
9195 {
9196 expr.set_error ();
9197 break;
9198 }
9199 tree sel = c_parser_objc_selector_arg (parser);
9200 location_t close_loc = c_parser_peek_token (parser)->location;
9201 parens.skip_until_found_close (parser);
9202 expr.value = objc_build_selector_expr (loc, sel);
9203 set_c_expr_source_range (&expr, loc, close_loc);
9204 }
9205 break;
9206 case RID_AT_PROTOCOL:
9207 {
9208 gcc_assert (c_dialect_objc ());
9209 c_parser_consume_token (parser);
9210 matching_parens parens;
9211 if (!parens.require_open (parser))
9212 {
9213 expr.set_error ();
9214 break;
9215 }
9216 if (c_parser_next_token_is_not (parser, CPP_NAME))
9217 {
9218 c_parser_error (parser, "expected identifier");
9219 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
9220 expr.set_error ();
9221 break;
9222 }
9223 tree id = c_parser_peek_token (parser)->value;
9224 c_parser_consume_token (parser);
9225 location_t close_loc = c_parser_peek_token (parser)->location;
9226 parens.skip_until_found_close (parser);
9227 expr.value = objc_build_protocol_expr (id);
9228 set_c_expr_source_range (&expr, loc, close_loc);
9229 }
9230 break;
9231 case RID_AT_ENCODE:
9232 {
9233 /* Extension to support C-structures in the archiver. */
9234 gcc_assert (c_dialect_objc ());
9235 c_parser_consume_token (parser);
9236 matching_parens parens;
9237 if (!parens.require_open (parser))
9238 {
9239 expr.set_error ();
9240 break;
9241 }
9242 t1 = c_parser_type_name (parser);
9243 if (t1 == NULL)
9244 {
9245 expr.set_error ();
9246 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
9247 break;
9248 }
9249 location_t close_loc = c_parser_peek_token (parser)->location;
9250 parens.skip_until_found_close (parser);
9251 tree type = groktypename (t1, NULL, NULL);
9252 expr.value = objc_build_encode_expr (type);
9253 set_c_expr_source_range (&expr, loc, close_loc);
9254 }
9255 break;
9256 case RID_GENERIC:
9257 expr = c_parser_generic_selection (parser);
9258 break;
9259 default:
9260 c_parser_error (parser, "expected expression");
9261 expr.set_error ();
9262 break;
9263 }
9264 break;
9265 case CPP_OPEN_SQUARE:
9266 if (c_dialect_objc ())
9267 {
9268 tree receiver, args;
9269 c_parser_consume_token (parser);
9270 receiver = c_parser_objc_receiver (parser);
9271 args = c_parser_objc_message_args (parser);
9272 location_t close_loc = c_parser_peek_token (parser)->location;
9273 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
9274 "expected %<]%>");
9275 expr.value = objc_build_message_expr (receiver, args);
9276 set_c_expr_source_range (&expr, loc, close_loc);
9277 break;
9278 }
9279 /* Else fall through to report error. */
9280 /* FALLTHRU */
9281 default:
9282 c_parser_error (parser, "expected expression");
9283 expr.set_error ();
9284 break;
9285 }
9286 out:
9287 return c_parser_postfix_expression_after_primary
9288 (parser, EXPR_LOC_OR_LOC (expr.value, loc), expr);
9289 }
9290
9291 /* Parse a postfix expression after a parenthesized type name: the
9292 brace-enclosed initializer of a compound literal, possibly followed
9293 by some postfix operators. This is separate because it is not
9294 possible to tell until after the type name whether a cast
9295 expression has a cast or a compound literal, or whether the operand
9296 of sizeof is a parenthesized type name or starts with a compound
9297 literal. TYPE_LOC is the location where TYPE_NAME starts--the
9298 location of the first token after the parentheses around the type
9299 name. */
9300
9301 static struct c_expr
9302 c_parser_postfix_expression_after_paren_type (c_parser *parser,
9303 struct c_type_name *type_name,
9304 location_t type_loc)
9305 {
9306 tree type;
9307 struct c_expr init;
9308 bool non_const;
9309 struct c_expr expr;
9310 location_t start_loc;
9311 tree type_expr = NULL_TREE;
9312 bool type_expr_const = true;
9313 check_compound_literal_type (type_loc, type_name);
9314 rich_location richloc (line_table, type_loc);
9315 start_init (NULL_TREE, NULL, 0, &richloc);
9316 type = groktypename (type_name, &type_expr, &type_expr_const);
9317 start_loc = c_parser_peek_token (parser)->location;
9318 if (type != error_mark_node && C_TYPE_VARIABLE_SIZE (type))
9319 {
9320 error_at (type_loc, "compound literal has variable size");
9321 type = error_mark_node;
9322 }
9323 init = c_parser_braced_init (parser, type, false, NULL);
9324 finish_init ();
9325 maybe_warn_string_init (type_loc, type, init);
9326
9327 if (type != error_mark_node
9328 && !ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (type))
9329 && current_function_decl)
9330 {
9331 error ("compound literal qualified by address-space qualifier");
9332 type = error_mark_node;
9333 }
9334
9335 pedwarn_c90 (start_loc, OPT_Wpedantic, "ISO C90 forbids compound literals");
9336 non_const = ((init.value && TREE_CODE (init.value) == CONSTRUCTOR)
9337 ? CONSTRUCTOR_NON_CONST (init.value)
9338 : init.original_code == C_MAYBE_CONST_EXPR);
9339 non_const |= !type_expr_const;
9340 unsigned int alignas_align = 0;
9341 if (type != error_mark_node
9342 && type_name->specs->align_log != -1)
9343 {
9344 alignas_align = 1U << type_name->specs->align_log;
9345 if (alignas_align < min_align_of_type (type))
9346 {
9347 error_at (type_name->specs->locations[cdw_alignas],
9348 "%<_Alignas%> specifiers cannot reduce "
9349 "alignment of compound literal");
9350 alignas_align = 0;
9351 }
9352 }
9353 expr.value = build_compound_literal (start_loc, type, init.value, non_const,
9354 alignas_align);
9355 set_c_expr_source_range (&expr, init.src_range);
9356 expr.original_code = ERROR_MARK;
9357 expr.original_type = NULL;
9358 if (type != error_mark_node
9359 && expr.value != error_mark_node
9360 && type_expr)
9361 {
9362 if (TREE_CODE (expr.value) == C_MAYBE_CONST_EXPR)
9363 {
9364 gcc_assert (C_MAYBE_CONST_EXPR_PRE (expr.value) == NULL_TREE);
9365 C_MAYBE_CONST_EXPR_PRE (expr.value) = type_expr;
9366 }
9367 else
9368 {
9369 gcc_assert (!non_const);
9370 expr.value = build2 (C_MAYBE_CONST_EXPR, type,
9371 type_expr, expr.value);
9372 }
9373 }
9374 return c_parser_postfix_expression_after_primary (parser, start_loc, expr);
9375 }
9376
9377 /* Callback function for sizeof_pointer_memaccess_warning to compare
9378 types. */
9379
9380 static bool
9381 sizeof_ptr_memacc_comptypes (tree type1, tree type2)
9382 {
9383 return comptypes (type1, type2) == 1;
9384 }
9385
9386 /* Warn for patterns where abs-like function appears to be used incorrectly,
9387 gracefully ignore any non-abs-like function. The warning location should
9388 be LOC. FNDECL is the declaration of called function, it must be a
9389 BUILT_IN_NORMAL function. ARG is the first and only argument of the
9390 call. */
9391
9392 static void
9393 warn_for_abs (location_t loc, tree fndecl, tree arg)
9394 {
9395 /* Avoid warning in unreachable subexpressions. */
9396 if (c_inhibit_evaluation_warnings)
9397 return;
9398
9399 tree atype = TREE_TYPE (arg);
9400
9401 /* Casts from pointers (and thus arrays and fndecls) will generate
9402 -Wint-conversion warnings. Most other wrong types hopefully lead to type
9403 mismatch errors. TODO: Think about what to do with FIXED_POINT_TYPE_P
9404 types and possibly other exotic types. */
9405 if (!INTEGRAL_TYPE_P (atype)
9406 && !SCALAR_FLOAT_TYPE_P (atype)
9407 && TREE_CODE (atype) != COMPLEX_TYPE)
9408 return;
9409
9410 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
9411
9412 switch (fcode)
9413 {
9414 case BUILT_IN_ABS:
9415 case BUILT_IN_LABS:
9416 case BUILT_IN_LLABS:
9417 case BUILT_IN_IMAXABS:
9418 if (!INTEGRAL_TYPE_P (atype))
9419 {
9420 if (SCALAR_FLOAT_TYPE_P (atype))
9421 warning_at (loc, OPT_Wabsolute_value,
9422 "using integer absolute value function %qD when "
9423 "argument is of floating point type %qT",
9424 fndecl, atype);
9425 else if (TREE_CODE (atype) == COMPLEX_TYPE)
9426 warning_at (loc, OPT_Wabsolute_value,
9427 "using integer absolute value function %qD when "
9428 "argument is of complex type %qT", fndecl, atype);
9429 else
9430 gcc_unreachable ();
9431 return;
9432 }
9433 if (TYPE_UNSIGNED (atype))
9434 warning_at (loc, OPT_Wabsolute_value,
9435 "taking the absolute value of unsigned type %qT "
9436 "has no effect", atype);
9437 break;
9438
9439 CASE_FLT_FN (BUILT_IN_FABS):
9440 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FABS):
9441 if (!SCALAR_FLOAT_TYPE_P (atype)
9442 || DECIMAL_FLOAT_MODE_P (TYPE_MODE (atype)))
9443 {
9444 if (INTEGRAL_TYPE_P (atype))
9445 warning_at (loc, OPT_Wabsolute_value,
9446 "using floating point absolute value function %qD "
9447 "when argument is of integer type %qT", fndecl, atype);
9448 else if (DECIMAL_FLOAT_TYPE_P (atype))
9449 warning_at (loc, OPT_Wabsolute_value,
9450 "using floating point absolute value function %qD "
9451 "when argument is of decimal floating point type %qT",
9452 fndecl, atype);
9453 else if (TREE_CODE (atype) == COMPLEX_TYPE)
9454 warning_at (loc, OPT_Wabsolute_value,
9455 "using floating point absolute value function %qD when "
9456 "argument is of complex type %qT", fndecl, atype);
9457 else
9458 gcc_unreachable ();
9459 return;
9460 }
9461 break;
9462
9463 CASE_FLT_FN (BUILT_IN_CABS):
9464 if (TREE_CODE (atype) != COMPLEX_TYPE)
9465 {
9466 if (INTEGRAL_TYPE_P (atype))
9467 warning_at (loc, OPT_Wabsolute_value,
9468 "using complex absolute value function %qD when "
9469 "argument is of integer type %qT", fndecl, atype);
9470 else if (SCALAR_FLOAT_TYPE_P (atype))
9471 warning_at (loc, OPT_Wabsolute_value,
9472 "using complex absolute value function %qD when "
9473 "argument is of floating point type %qT",
9474 fndecl, atype);
9475 else
9476 gcc_unreachable ();
9477
9478 return;
9479 }
9480 break;
9481
9482 case BUILT_IN_FABSD32:
9483 case BUILT_IN_FABSD64:
9484 case BUILT_IN_FABSD128:
9485 if (!DECIMAL_FLOAT_TYPE_P (atype))
9486 {
9487 if (INTEGRAL_TYPE_P (atype))
9488 warning_at (loc, OPT_Wabsolute_value,
9489 "using decimal floating point absolute value "
9490 "function %qD when argument is of integer type %qT",
9491 fndecl, atype);
9492 else if (SCALAR_FLOAT_TYPE_P (atype))
9493 warning_at (loc, OPT_Wabsolute_value,
9494 "using decimal floating point absolute value "
9495 "function %qD when argument is of floating point "
9496 "type %qT", fndecl, atype);
9497 else if (TREE_CODE (atype) == COMPLEX_TYPE)
9498 warning_at (loc, OPT_Wabsolute_value,
9499 "using decimal floating point absolute value "
9500 "function %qD when argument is of complex type %qT",
9501 fndecl, atype);
9502 else
9503 gcc_unreachable ();
9504 return;
9505 }
9506 break;
9507
9508 default:
9509 return;
9510 }
9511
9512 if (!TYPE_ARG_TYPES (TREE_TYPE (fndecl)))
9513 return;
9514
9515 tree ftype = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
9516 if (TREE_CODE (atype) == COMPLEX_TYPE)
9517 {
9518 gcc_assert (TREE_CODE (ftype) == COMPLEX_TYPE);
9519 atype = TREE_TYPE (atype);
9520 ftype = TREE_TYPE (ftype);
9521 }
9522
9523 if (TYPE_PRECISION (ftype) < TYPE_PRECISION (atype))
9524 warning_at (loc, OPT_Wabsolute_value,
9525 "absolute value function %qD given an argument of type %qT "
9526 "but has parameter of type %qT which may cause truncation "
9527 "of value", fndecl, atype, ftype);
9528 }
9529
9530
9531 /* Parse a postfix expression after the initial primary or compound
9532 literal; that is, parse a series of postfix operators.
9533
9534 EXPR_LOC is the location of the primary expression. */
9535
9536 static struct c_expr
9537 c_parser_postfix_expression_after_primary (c_parser *parser,
9538 location_t expr_loc,
9539 struct c_expr expr)
9540 {
9541 struct c_expr orig_expr;
9542 tree ident, idx;
9543 location_t sizeof_arg_loc[3], comp_loc;
9544 tree sizeof_arg[3];
9545 unsigned int literal_zero_mask;
9546 unsigned int i;
9547 vec<tree, va_gc> *exprlist;
9548 vec<tree, va_gc> *origtypes = NULL;
9549 vec<location_t> arg_loc = vNULL;
9550 location_t start;
9551 location_t finish;
9552
9553 while (true)
9554 {
9555 location_t op_loc = c_parser_peek_token (parser)->location;
9556 switch (c_parser_peek_token (parser)->type)
9557 {
9558 case CPP_OPEN_SQUARE:
9559 /* Array reference. */
9560 c_parser_consume_token (parser);
9561 idx = c_parser_expression (parser).value;
9562 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
9563 "expected %<]%>");
9564 start = expr.get_start ();
9565 finish = parser->tokens_buf[0].location;
9566 expr.value = build_array_ref (op_loc, expr.value, idx);
9567 set_c_expr_source_range (&expr, start, finish);
9568 expr.original_code = ERROR_MARK;
9569 expr.original_type = NULL;
9570 break;
9571 case CPP_OPEN_PAREN:
9572 /* Function call. */
9573 c_parser_consume_token (parser);
9574 for (i = 0; i < 3; i++)
9575 {
9576 sizeof_arg[i] = NULL_TREE;
9577 sizeof_arg_loc[i] = UNKNOWN_LOCATION;
9578 }
9579 literal_zero_mask = 0;
9580 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
9581 exprlist = NULL;
9582 else
9583 exprlist = c_parser_expr_list (parser, true, false, &origtypes,
9584 sizeof_arg_loc, sizeof_arg,
9585 &arg_loc, &literal_zero_mask);
9586 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
9587 "expected %<)%>");
9588 orig_expr = expr;
9589 mark_exp_read (expr.value);
9590 if (warn_sizeof_pointer_memaccess)
9591 sizeof_pointer_memaccess_warning (sizeof_arg_loc,
9592 expr.value, exprlist,
9593 sizeof_arg,
9594 sizeof_ptr_memacc_comptypes);
9595 if (TREE_CODE (expr.value) == FUNCTION_DECL)
9596 {
9597 if (fndecl_built_in_p (expr.value, BUILT_IN_MEMSET)
9598 && vec_safe_length (exprlist) == 3)
9599 {
9600 tree arg0 = (*exprlist)[0];
9601 tree arg2 = (*exprlist)[2];
9602 warn_for_memset (expr_loc, arg0, arg2, literal_zero_mask);
9603 }
9604 if (warn_absolute_value
9605 && fndecl_built_in_p (expr.value, BUILT_IN_NORMAL)
9606 && vec_safe_length (exprlist) == 1)
9607 warn_for_abs (expr_loc, expr.value, (*exprlist)[0]);
9608 }
9609
9610 start = expr.get_start ();
9611 finish = parser->tokens_buf[0].get_finish ();
9612 expr.value
9613 = c_build_function_call_vec (expr_loc, arg_loc, expr.value,
9614 exprlist, origtypes);
9615 set_c_expr_source_range (&expr, start, finish);
9616
9617 expr.original_code = ERROR_MARK;
9618 if (TREE_CODE (expr.value) == INTEGER_CST
9619 && TREE_CODE (orig_expr.value) == FUNCTION_DECL
9620 && fndecl_built_in_p (orig_expr.value, BUILT_IN_CONSTANT_P))
9621 expr.original_code = C_MAYBE_CONST_EXPR;
9622 expr.original_type = NULL;
9623 if (exprlist)
9624 {
9625 release_tree_vector (exprlist);
9626 release_tree_vector (origtypes);
9627 }
9628 arg_loc.release ();
9629 break;
9630 case CPP_DOT:
9631 /* Structure element reference. */
9632 c_parser_consume_token (parser);
9633 expr = default_function_array_conversion (expr_loc, expr);
9634 if (c_parser_next_token_is (parser, CPP_NAME))
9635 {
9636 c_token *comp_tok = c_parser_peek_token (parser);
9637 ident = comp_tok->value;
9638 comp_loc = comp_tok->location;
9639 }
9640 else
9641 {
9642 c_parser_error (parser, "expected identifier");
9643 expr.set_error ();
9644 expr.original_code = ERROR_MARK;
9645 expr.original_type = NULL;
9646 return expr;
9647 }
9648 start = expr.get_start ();
9649 finish = c_parser_peek_token (parser)->get_finish ();
9650 c_parser_consume_token (parser);
9651 expr.value = build_component_ref (op_loc, expr.value, ident,
9652 comp_loc);
9653 set_c_expr_source_range (&expr, start, finish);
9654 expr.original_code = ERROR_MARK;
9655 if (TREE_CODE (expr.value) != COMPONENT_REF)
9656 expr.original_type = NULL;
9657 else
9658 {
9659 /* Remember the original type of a bitfield. */
9660 tree field = TREE_OPERAND (expr.value, 1);
9661 if (TREE_CODE (field) != FIELD_DECL)
9662 expr.original_type = NULL;
9663 else
9664 expr.original_type = DECL_BIT_FIELD_TYPE (field);
9665 }
9666 break;
9667 case CPP_DEREF:
9668 /* Structure element reference. */
9669 c_parser_consume_token (parser);
9670 expr = convert_lvalue_to_rvalue (expr_loc, expr, true, false);
9671 if (c_parser_next_token_is (parser, CPP_NAME))
9672 {
9673 c_token *comp_tok = c_parser_peek_token (parser);
9674 ident = comp_tok->value;
9675 comp_loc = comp_tok->location;
9676 }
9677 else
9678 {
9679 c_parser_error (parser, "expected identifier");
9680 expr.set_error ();
9681 expr.original_code = ERROR_MARK;
9682 expr.original_type = NULL;
9683 return expr;
9684 }
9685 start = expr.get_start ();
9686 finish = c_parser_peek_token (parser)->get_finish ();
9687 c_parser_consume_token (parser);
9688 expr.value = build_component_ref (op_loc,
9689 build_indirect_ref (op_loc,
9690 expr.value,
9691 RO_ARROW),
9692 ident, comp_loc);
9693 set_c_expr_source_range (&expr, start, finish);
9694 expr.original_code = ERROR_MARK;
9695 if (TREE_CODE (expr.value) != COMPONENT_REF)
9696 expr.original_type = NULL;
9697 else
9698 {
9699 /* Remember the original type of a bitfield. */
9700 tree field = TREE_OPERAND (expr.value, 1);
9701 if (TREE_CODE (field) != FIELD_DECL)
9702 expr.original_type = NULL;
9703 else
9704 expr.original_type = DECL_BIT_FIELD_TYPE (field);
9705 }
9706 break;
9707 case CPP_PLUS_PLUS:
9708 /* Postincrement. */
9709 start = expr.get_start ();
9710 finish = c_parser_peek_token (parser)->get_finish ();
9711 c_parser_consume_token (parser);
9712 expr = default_function_array_read_conversion (expr_loc, expr);
9713 expr.value = build_unary_op (op_loc, POSTINCREMENT_EXPR,
9714 expr.value, false);
9715 set_c_expr_source_range (&expr, start, finish);
9716 expr.original_code = ERROR_MARK;
9717 expr.original_type = NULL;
9718 break;
9719 case CPP_MINUS_MINUS:
9720 /* Postdecrement. */
9721 start = expr.get_start ();
9722 finish = c_parser_peek_token (parser)->get_finish ();
9723 c_parser_consume_token (parser);
9724 expr = default_function_array_read_conversion (expr_loc, expr);
9725 expr.value = build_unary_op (op_loc, POSTDECREMENT_EXPR,
9726 expr.value, false);
9727 set_c_expr_source_range (&expr, start, finish);
9728 expr.original_code = ERROR_MARK;
9729 expr.original_type = NULL;
9730 break;
9731 default:
9732 return expr;
9733 }
9734 }
9735 }
9736
9737 /* Parse an expression (C90 6.3.17, C99 6.5.17, C11 6.5.17).
9738
9739 expression:
9740 assignment-expression
9741 expression , assignment-expression
9742 */
9743
9744 static struct c_expr
9745 c_parser_expression (c_parser *parser)
9746 {
9747 location_t tloc = c_parser_peek_token (parser)->location;
9748 struct c_expr expr;
9749 expr = c_parser_expr_no_commas (parser, NULL);
9750 if (c_parser_next_token_is (parser, CPP_COMMA))
9751 expr = convert_lvalue_to_rvalue (tloc, expr, true, false);
9752 while (c_parser_next_token_is (parser, CPP_COMMA))
9753 {
9754 struct c_expr next;
9755 tree lhsval;
9756 location_t loc = c_parser_peek_token (parser)->location;
9757 location_t expr_loc;
9758 c_parser_consume_token (parser);
9759 expr_loc = c_parser_peek_token (parser)->location;
9760 lhsval = expr.value;
9761 while (TREE_CODE (lhsval) == COMPOUND_EXPR)
9762 lhsval = TREE_OPERAND (lhsval, 1);
9763 if (DECL_P (lhsval) || handled_component_p (lhsval))
9764 mark_exp_read (lhsval);
9765 next = c_parser_expr_no_commas (parser, NULL);
9766 next = convert_lvalue_to_rvalue (expr_loc, next, true, false);
9767 expr.value = build_compound_expr (loc, expr.value, next.value);
9768 expr.original_code = COMPOUND_EXPR;
9769 expr.original_type = next.original_type;
9770 }
9771 return expr;
9772 }
9773
9774 /* Parse an expression and convert functions or arrays to pointers and
9775 lvalues to rvalues. */
9776
9777 static struct c_expr
9778 c_parser_expression_conv (c_parser *parser)
9779 {
9780 struct c_expr expr;
9781 location_t loc = c_parser_peek_token (parser)->location;
9782 expr = c_parser_expression (parser);
9783 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
9784 return expr;
9785 }
9786
9787 /* Helper function of c_parser_expr_list. Check if IDXth (0 based)
9788 argument is a literal zero alone and if so, set it in literal_zero_mask. */
9789
9790 static inline void
9791 c_parser_check_literal_zero (c_parser *parser, unsigned *literal_zero_mask,
9792 unsigned int idx)
9793 {
9794 if (idx >= HOST_BITS_PER_INT)
9795 return;
9796
9797 c_token *tok = c_parser_peek_token (parser);
9798 switch (tok->type)
9799 {
9800 case CPP_NUMBER:
9801 case CPP_CHAR:
9802 case CPP_WCHAR:
9803 case CPP_CHAR16:
9804 case CPP_CHAR32:
9805 /* If a parameter is literal zero alone, remember it
9806 for -Wmemset-transposed-args warning. */
9807 if (integer_zerop (tok->value)
9808 && !TREE_OVERFLOW (tok->value)
9809 && (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
9810 || c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_PAREN))
9811 *literal_zero_mask |= 1U << idx;
9812 default:
9813 break;
9814 }
9815 }
9816
9817 /* Parse a non-empty list of expressions. If CONVERT_P, convert
9818 functions and arrays to pointers and lvalues to rvalues. If
9819 FOLD_P, fold the expressions. If LOCATIONS is non-NULL, save the
9820 locations of function arguments into this vector.
9821
9822 nonempty-expr-list:
9823 assignment-expression
9824 nonempty-expr-list , assignment-expression
9825 */
9826
9827 static vec<tree, va_gc> *
9828 c_parser_expr_list (c_parser *parser, bool convert_p, bool fold_p,
9829 vec<tree, va_gc> **p_orig_types,
9830 location_t *sizeof_arg_loc, tree *sizeof_arg,
9831 vec<location_t> *locations,
9832 unsigned int *literal_zero_mask)
9833 {
9834 vec<tree, va_gc> *ret;
9835 vec<tree, va_gc> *orig_types;
9836 struct c_expr expr;
9837 unsigned int idx = 0;
9838
9839 ret = make_tree_vector ();
9840 if (p_orig_types == NULL)
9841 orig_types = NULL;
9842 else
9843 orig_types = make_tree_vector ();
9844
9845 if (literal_zero_mask)
9846 c_parser_check_literal_zero (parser, literal_zero_mask, 0);
9847 expr = c_parser_expr_no_commas (parser, NULL);
9848 if (convert_p)
9849 expr = convert_lvalue_to_rvalue (expr.get_location (), expr, true, true);
9850 if (fold_p)
9851 expr.value = c_fully_fold (expr.value, false, NULL);
9852 ret->quick_push (expr.value);
9853 if (orig_types)
9854 orig_types->quick_push (expr.original_type);
9855 if (locations)
9856 locations->safe_push (expr.get_location ());
9857 if (sizeof_arg != NULL
9858 && expr.original_code == SIZEOF_EXPR)
9859 {
9860 sizeof_arg[0] = c_last_sizeof_arg;
9861 sizeof_arg_loc[0] = c_last_sizeof_loc;
9862 }
9863 while (c_parser_next_token_is (parser, CPP_COMMA))
9864 {
9865 c_parser_consume_token (parser);
9866 if (literal_zero_mask)
9867 c_parser_check_literal_zero (parser, literal_zero_mask, idx + 1);
9868 expr = c_parser_expr_no_commas (parser, NULL);
9869 if (convert_p)
9870 expr = convert_lvalue_to_rvalue (expr.get_location (), expr, true,
9871 true);
9872 if (fold_p)
9873 expr.value = c_fully_fold (expr.value, false, NULL);
9874 vec_safe_push (ret, expr.value);
9875 if (orig_types)
9876 vec_safe_push (orig_types, expr.original_type);
9877 if (locations)
9878 locations->safe_push (expr.get_location ());
9879 if (++idx < 3
9880 && sizeof_arg != NULL
9881 && expr.original_code == SIZEOF_EXPR)
9882 {
9883 sizeof_arg[idx] = c_last_sizeof_arg;
9884 sizeof_arg_loc[idx] = c_last_sizeof_loc;
9885 }
9886 }
9887 if (orig_types)
9888 *p_orig_types = orig_types;
9889 return ret;
9890 }
9891 \f
9892 /* Parse Objective-C-specific constructs. */
9893
9894 /* Parse an objc-class-definition.
9895
9896 objc-class-definition:
9897 @interface identifier objc-superclass[opt] objc-protocol-refs[opt]
9898 objc-class-instance-variables[opt] objc-methodprotolist @end
9899 @implementation identifier objc-superclass[opt]
9900 objc-class-instance-variables[opt]
9901 @interface identifier ( identifier ) objc-protocol-refs[opt]
9902 objc-methodprotolist @end
9903 @interface identifier ( ) objc-protocol-refs[opt]
9904 objc-methodprotolist @end
9905 @implementation identifier ( identifier )
9906
9907 objc-superclass:
9908 : identifier
9909
9910 "@interface identifier (" must start "@interface identifier (
9911 identifier ) ...": objc-methodprotolist in the first production may
9912 not start with a parenthesized identifier as a declarator of a data
9913 definition with no declaration specifiers if the objc-superclass,
9914 objc-protocol-refs and objc-class-instance-variables are omitted. */
9915
9916 static void
9917 c_parser_objc_class_definition (c_parser *parser, tree attributes)
9918 {
9919 bool iface_p;
9920 tree id1;
9921 tree superclass;
9922 if (c_parser_next_token_is_keyword (parser, RID_AT_INTERFACE))
9923 iface_p = true;
9924 else if (c_parser_next_token_is_keyword (parser, RID_AT_IMPLEMENTATION))
9925 iface_p = false;
9926 else
9927 gcc_unreachable ();
9928
9929 c_parser_consume_token (parser);
9930 if (c_parser_next_token_is_not (parser, CPP_NAME))
9931 {
9932 c_parser_error (parser, "expected identifier");
9933 return;
9934 }
9935 id1 = c_parser_peek_token (parser)->value;
9936 c_parser_consume_token (parser);
9937 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
9938 {
9939 /* We have a category or class extension. */
9940 tree id2;
9941 tree proto = NULL_TREE;
9942 matching_parens parens;
9943 parens.consume_open (parser);
9944 if (c_parser_next_token_is_not (parser, CPP_NAME))
9945 {
9946 if (iface_p && c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
9947 {
9948 /* We have a class extension. */
9949 id2 = NULL_TREE;
9950 }
9951 else
9952 {
9953 c_parser_error (parser, "expected identifier or %<)%>");
9954 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
9955 return;
9956 }
9957 }
9958 else
9959 {
9960 id2 = c_parser_peek_token (parser)->value;
9961 c_parser_consume_token (parser);
9962 }
9963 parens.skip_until_found_close (parser);
9964 if (!iface_p)
9965 {
9966 objc_start_category_implementation (id1, id2);
9967 return;
9968 }
9969 if (c_parser_next_token_is (parser, CPP_LESS))
9970 proto = c_parser_objc_protocol_refs (parser);
9971 objc_start_category_interface (id1, id2, proto, attributes);
9972 c_parser_objc_methodprotolist (parser);
9973 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
9974 objc_finish_interface ();
9975 return;
9976 }
9977 if (c_parser_next_token_is (parser, CPP_COLON))
9978 {
9979 c_parser_consume_token (parser);
9980 if (c_parser_next_token_is_not (parser, CPP_NAME))
9981 {
9982 c_parser_error (parser, "expected identifier");
9983 return;
9984 }
9985 superclass = c_parser_peek_token (parser)->value;
9986 c_parser_consume_token (parser);
9987 }
9988 else
9989 superclass = NULL_TREE;
9990 if (iface_p)
9991 {
9992 tree proto = NULL_TREE;
9993 if (c_parser_next_token_is (parser, CPP_LESS))
9994 proto = c_parser_objc_protocol_refs (parser);
9995 objc_start_class_interface (id1, superclass, proto, attributes);
9996 }
9997 else
9998 objc_start_class_implementation (id1, superclass);
9999 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
10000 c_parser_objc_class_instance_variables (parser);
10001 if (iface_p)
10002 {
10003 objc_continue_interface ();
10004 c_parser_objc_methodprotolist (parser);
10005 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
10006 objc_finish_interface ();
10007 }
10008 else
10009 {
10010 objc_continue_implementation ();
10011 return;
10012 }
10013 }
10014
10015 /* Parse objc-class-instance-variables.
10016
10017 objc-class-instance-variables:
10018 { objc-instance-variable-decl-list[opt] }
10019
10020 objc-instance-variable-decl-list:
10021 objc-visibility-spec
10022 objc-instance-variable-decl ;
10023 ;
10024 objc-instance-variable-decl-list objc-visibility-spec
10025 objc-instance-variable-decl-list objc-instance-variable-decl ;
10026 objc-instance-variable-decl-list ;
10027
10028 objc-visibility-spec:
10029 @private
10030 @protected
10031 @public
10032
10033 objc-instance-variable-decl:
10034 struct-declaration
10035 */
10036
10037 static void
10038 c_parser_objc_class_instance_variables (c_parser *parser)
10039 {
10040 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
10041 c_parser_consume_token (parser);
10042 while (c_parser_next_token_is_not (parser, CPP_EOF))
10043 {
10044 tree decls;
10045 /* Parse any stray semicolon. */
10046 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
10047 {
10048 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
10049 "extra semicolon");
10050 c_parser_consume_token (parser);
10051 continue;
10052 }
10053 /* Stop if at the end of the instance variables. */
10054 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
10055 {
10056 c_parser_consume_token (parser);
10057 break;
10058 }
10059 /* Parse any objc-visibility-spec. */
10060 if (c_parser_next_token_is_keyword (parser, RID_AT_PRIVATE))
10061 {
10062 c_parser_consume_token (parser);
10063 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
10064 continue;
10065 }
10066 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROTECTED))
10067 {
10068 c_parser_consume_token (parser);
10069 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
10070 continue;
10071 }
10072 else if (c_parser_next_token_is_keyword (parser, RID_AT_PUBLIC))
10073 {
10074 c_parser_consume_token (parser);
10075 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
10076 continue;
10077 }
10078 else if (c_parser_next_token_is_keyword (parser, RID_AT_PACKAGE))
10079 {
10080 c_parser_consume_token (parser);
10081 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
10082 continue;
10083 }
10084 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
10085 {
10086 c_parser_pragma (parser, pragma_external, NULL);
10087 continue;
10088 }
10089
10090 /* Parse some comma-separated declarations. */
10091 decls = c_parser_struct_declaration (parser);
10092 if (decls == NULL)
10093 {
10094 /* There is a syntax error. We want to skip the offending
10095 tokens up to the next ';' (included) or '}'
10096 (excluded). */
10097
10098 /* First, skip manually a ')' or ']'. This is because they
10099 reduce the nesting level, so c_parser_skip_until_found()
10100 wouldn't be able to skip past them. */
10101 c_token *token = c_parser_peek_token (parser);
10102 if (token->type == CPP_CLOSE_PAREN || token->type == CPP_CLOSE_SQUARE)
10103 c_parser_consume_token (parser);
10104
10105 /* Then, do the standard skipping. */
10106 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
10107
10108 /* We hopefully recovered. Start normal parsing again. */
10109 parser->error = false;
10110 continue;
10111 }
10112 else
10113 {
10114 /* Comma-separated instance variables are chained together
10115 in reverse order; add them one by one. */
10116 tree ivar = nreverse (decls);
10117 for (; ivar; ivar = DECL_CHAIN (ivar))
10118 objc_add_instance_variable (copy_node (ivar));
10119 }
10120 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
10121 }
10122 }
10123
10124 /* Parse an objc-class-declaration.
10125
10126 objc-class-declaration:
10127 @class identifier-list ;
10128 */
10129
10130 static void
10131 c_parser_objc_class_declaration (c_parser *parser)
10132 {
10133 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_CLASS));
10134 c_parser_consume_token (parser);
10135 /* Any identifiers, including those declared as type names, are OK
10136 here. */
10137 while (true)
10138 {
10139 tree id;
10140 if (c_parser_next_token_is_not (parser, CPP_NAME))
10141 {
10142 c_parser_error (parser, "expected identifier");
10143 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
10144 parser->error = false;
10145 return;
10146 }
10147 id = c_parser_peek_token (parser)->value;
10148 objc_declare_class (id);
10149 c_parser_consume_token (parser);
10150 if (c_parser_next_token_is (parser, CPP_COMMA))
10151 c_parser_consume_token (parser);
10152 else
10153 break;
10154 }
10155 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
10156 }
10157
10158 /* Parse an objc-alias-declaration.
10159
10160 objc-alias-declaration:
10161 @compatibility_alias identifier identifier ;
10162 */
10163
10164 static void
10165 c_parser_objc_alias_declaration (c_parser *parser)
10166 {
10167 tree id1, id2;
10168 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_ALIAS));
10169 c_parser_consume_token (parser);
10170 if (c_parser_next_token_is_not (parser, CPP_NAME))
10171 {
10172 c_parser_error (parser, "expected identifier");
10173 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
10174 return;
10175 }
10176 id1 = c_parser_peek_token (parser)->value;
10177 c_parser_consume_token (parser);
10178 if (c_parser_next_token_is_not (parser, CPP_NAME))
10179 {
10180 c_parser_error (parser, "expected identifier");
10181 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
10182 return;
10183 }
10184 id2 = c_parser_peek_token (parser)->value;
10185 c_parser_consume_token (parser);
10186 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
10187 objc_declare_alias (id1, id2);
10188 }
10189
10190 /* Parse an objc-protocol-definition.
10191
10192 objc-protocol-definition:
10193 @protocol identifier objc-protocol-refs[opt] objc-methodprotolist @end
10194 @protocol identifier-list ;
10195
10196 "@protocol identifier ;" should be resolved as "@protocol
10197 identifier-list ;": objc-methodprotolist may not start with a
10198 semicolon in the first alternative if objc-protocol-refs are
10199 omitted. */
10200
10201 static void
10202 c_parser_objc_protocol_definition (c_parser *parser, tree attributes)
10203 {
10204 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROTOCOL));
10205
10206 c_parser_consume_token (parser);
10207 if (c_parser_next_token_is_not (parser, CPP_NAME))
10208 {
10209 c_parser_error (parser, "expected identifier");
10210 return;
10211 }
10212 if (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
10213 || c_parser_peek_2nd_token (parser)->type == CPP_SEMICOLON)
10214 {
10215 /* Any identifiers, including those declared as type names, are
10216 OK here. */
10217 while (true)
10218 {
10219 tree id;
10220 if (c_parser_next_token_is_not (parser, CPP_NAME))
10221 {
10222 c_parser_error (parser, "expected identifier");
10223 break;
10224 }
10225 id = c_parser_peek_token (parser)->value;
10226 objc_declare_protocol (id, attributes);
10227 c_parser_consume_token (parser);
10228 if (c_parser_next_token_is (parser, CPP_COMMA))
10229 c_parser_consume_token (parser);
10230 else
10231 break;
10232 }
10233 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
10234 }
10235 else
10236 {
10237 tree id = c_parser_peek_token (parser)->value;
10238 tree proto = NULL_TREE;
10239 c_parser_consume_token (parser);
10240 if (c_parser_next_token_is (parser, CPP_LESS))
10241 proto = c_parser_objc_protocol_refs (parser);
10242 parser->objc_pq_context = true;
10243 objc_start_protocol (id, proto, attributes);
10244 c_parser_objc_methodprotolist (parser);
10245 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
10246 parser->objc_pq_context = false;
10247 objc_finish_interface ();
10248 }
10249 }
10250
10251 /* Parse an objc-method-type.
10252
10253 objc-method-type:
10254 +
10255 -
10256
10257 Return true if it is a class method (+) and false if it is
10258 an instance method (-).
10259 */
10260 static inline bool
10261 c_parser_objc_method_type (c_parser *parser)
10262 {
10263 switch (c_parser_peek_token (parser)->type)
10264 {
10265 case CPP_PLUS:
10266 c_parser_consume_token (parser);
10267 return true;
10268 case CPP_MINUS:
10269 c_parser_consume_token (parser);
10270 return false;
10271 default:
10272 gcc_unreachable ();
10273 }
10274 }
10275
10276 /* Parse an objc-method-definition.
10277
10278 objc-method-definition:
10279 objc-method-type objc-method-decl ;[opt] compound-statement
10280 */
10281
10282 static void
10283 c_parser_objc_method_definition (c_parser *parser)
10284 {
10285 bool is_class_method = c_parser_objc_method_type (parser);
10286 tree decl, attributes = NULL_TREE, expr = NULL_TREE;
10287 parser->objc_pq_context = true;
10288 decl = c_parser_objc_method_decl (parser, is_class_method, &attributes,
10289 &expr);
10290 if (decl == error_mark_node)
10291 return; /* Bail here. */
10292
10293 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
10294 {
10295 c_parser_consume_token (parser);
10296 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
10297 "extra semicolon in method definition specified");
10298 }
10299
10300 if (!c_parser_next_token_is (parser, CPP_OPEN_BRACE))
10301 {
10302 c_parser_error (parser, "expected %<{%>");
10303 return;
10304 }
10305
10306 parser->objc_pq_context = false;
10307 if (objc_start_method_definition (is_class_method, decl, attributes, expr))
10308 {
10309 add_stmt (c_parser_compound_statement (parser));
10310 objc_finish_method_definition (current_function_decl);
10311 }
10312 else
10313 {
10314 /* This code is executed when we find a method definition
10315 outside of an @implementation context (or invalid for other
10316 reasons). Parse the method (to keep going) but do not emit
10317 any code.
10318 */
10319 c_parser_compound_statement (parser);
10320 }
10321 }
10322
10323 /* Parse an objc-methodprotolist.
10324
10325 objc-methodprotolist:
10326 empty
10327 objc-methodprotolist objc-methodproto
10328 objc-methodprotolist declaration
10329 objc-methodprotolist ;
10330 @optional
10331 @required
10332
10333 The declaration is a data definition, which may be missing
10334 declaration specifiers under the same rules and diagnostics as
10335 other data definitions outside functions, and the stray semicolon
10336 is diagnosed the same way as a stray semicolon outside a
10337 function. */
10338
10339 static void
10340 c_parser_objc_methodprotolist (c_parser *parser)
10341 {
10342 while (true)
10343 {
10344 /* The list is terminated by @end. */
10345 switch (c_parser_peek_token (parser)->type)
10346 {
10347 case CPP_SEMICOLON:
10348 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
10349 "ISO C does not allow extra %<;%> outside of a function");
10350 c_parser_consume_token (parser);
10351 break;
10352 case CPP_PLUS:
10353 case CPP_MINUS:
10354 c_parser_objc_methodproto (parser);
10355 break;
10356 case CPP_PRAGMA:
10357 c_parser_pragma (parser, pragma_external, NULL);
10358 break;
10359 case CPP_EOF:
10360 return;
10361 default:
10362 if (c_parser_next_token_is_keyword (parser, RID_AT_END))
10363 return;
10364 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROPERTY))
10365 c_parser_objc_at_property_declaration (parser);
10366 else if (c_parser_next_token_is_keyword (parser, RID_AT_OPTIONAL))
10367 {
10368 objc_set_method_opt (true);
10369 c_parser_consume_token (parser);
10370 }
10371 else if (c_parser_next_token_is_keyword (parser, RID_AT_REQUIRED))
10372 {
10373 objc_set_method_opt (false);
10374 c_parser_consume_token (parser);
10375 }
10376 else
10377 c_parser_declaration_or_fndef (parser, false, false, true,
10378 false, true, NULL, vNULL);
10379 break;
10380 }
10381 }
10382 }
10383
10384 /* Parse an objc-methodproto.
10385
10386 objc-methodproto:
10387 objc-method-type objc-method-decl ;
10388 */
10389
10390 static void
10391 c_parser_objc_methodproto (c_parser *parser)
10392 {
10393 bool is_class_method = c_parser_objc_method_type (parser);
10394 tree decl, attributes = NULL_TREE;
10395
10396 /* Remember protocol qualifiers in prototypes. */
10397 parser->objc_pq_context = true;
10398 decl = c_parser_objc_method_decl (parser, is_class_method, &attributes,
10399 NULL);
10400 /* Forget protocol qualifiers now. */
10401 parser->objc_pq_context = false;
10402
10403 /* Do not allow the presence of attributes to hide an erroneous
10404 method implementation in the interface section. */
10405 if (!c_parser_next_token_is (parser, CPP_SEMICOLON))
10406 {
10407 c_parser_error (parser, "expected %<;%>");
10408 return;
10409 }
10410
10411 if (decl != error_mark_node)
10412 objc_add_method_declaration (is_class_method, decl, attributes);
10413
10414 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
10415 }
10416
10417 /* If we are at a position that method attributes may be present, check that
10418 there are not any parsed already (a syntax error) and then collect any
10419 specified at the current location. Finally, if new attributes were present,
10420 check that the next token is legal ( ';' for decls and '{' for defs). */
10421
10422 static bool
10423 c_parser_objc_maybe_method_attributes (c_parser* parser, tree* attributes)
10424 {
10425 bool bad = false;
10426 if (*attributes)
10427 {
10428 c_parser_error (parser,
10429 "method attributes must be specified at the end only");
10430 *attributes = NULL_TREE;
10431 bad = true;
10432 }
10433
10434 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
10435 *attributes = c_parser_attributes (parser);
10436
10437 /* If there were no attributes here, just report any earlier error. */
10438 if (*attributes == NULL_TREE || bad)
10439 return bad;
10440
10441 /* If the attributes are followed by a ; or {, then just report any earlier
10442 error. */
10443 if (c_parser_next_token_is (parser, CPP_SEMICOLON)
10444 || c_parser_next_token_is (parser, CPP_OPEN_BRACE))
10445 return bad;
10446
10447 /* We've got attributes, but not at the end. */
10448 c_parser_error (parser,
10449 "expected %<;%> or %<{%> after method attribute definition");
10450 return true;
10451 }
10452
10453 /* Parse an objc-method-decl.
10454
10455 objc-method-decl:
10456 ( objc-type-name ) objc-selector
10457 objc-selector
10458 ( objc-type-name ) objc-keyword-selector objc-optparmlist
10459 objc-keyword-selector objc-optparmlist
10460 attributes
10461
10462 objc-keyword-selector:
10463 objc-keyword-decl
10464 objc-keyword-selector objc-keyword-decl
10465
10466 objc-keyword-decl:
10467 objc-selector : ( objc-type-name ) identifier
10468 objc-selector : identifier
10469 : ( objc-type-name ) identifier
10470 : identifier
10471
10472 objc-optparmlist:
10473 objc-optparms objc-optellipsis
10474
10475 objc-optparms:
10476 empty
10477 objc-opt-parms , parameter-declaration
10478
10479 objc-optellipsis:
10480 empty
10481 , ...
10482 */
10483
10484 static tree
10485 c_parser_objc_method_decl (c_parser *parser, bool is_class_method,
10486 tree *attributes, tree *expr)
10487 {
10488 tree type = NULL_TREE;
10489 tree sel;
10490 tree parms = NULL_TREE;
10491 bool ellipsis = false;
10492 bool attr_err = false;
10493
10494 *attributes = NULL_TREE;
10495 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
10496 {
10497 matching_parens parens;
10498 parens.consume_open (parser);
10499 type = c_parser_objc_type_name (parser);
10500 parens.skip_until_found_close (parser);
10501 }
10502 sel = c_parser_objc_selector (parser);
10503 /* If there is no selector, or a colon follows, we have an
10504 objc-keyword-selector. If there is a selector, and a colon does
10505 not follow, that selector ends the objc-method-decl. */
10506 if (!sel || c_parser_next_token_is (parser, CPP_COLON))
10507 {
10508 tree tsel = sel;
10509 tree list = NULL_TREE;
10510 while (true)
10511 {
10512 tree atype = NULL_TREE, id, keyworddecl;
10513 tree param_attr = NULL_TREE;
10514 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
10515 break;
10516 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
10517 {
10518 c_parser_consume_token (parser);
10519 atype = c_parser_objc_type_name (parser);
10520 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
10521 "expected %<)%>");
10522 }
10523 /* New ObjC allows attributes on method parameters. */
10524 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
10525 param_attr = c_parser_attributes (parser);
10526 if (c_parser_next_token_is_not (parser, CPP_NAME))
10527 {
10528 c_parser_error (parser, "expected identifier");
10529 return error_mark_node;
10530 }
10531 id = c_parser_peek_token (parser)->value;
10532 c_parser_consume_token (parser);
10533 keyworddecl = objc_build_keyword_decl (tsel, atype, id, param_attr);
10534 list = chainon (list, keyworddecl);
10535 tsel = c_parser_objc_selector (parser);
10536 if (!tsel && c_parser_next_token_is_not (parser, CPP_COLON))
10537 break;
10538 }
10539
10540 attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
10541
10542 /* Parse the optional parameter list. Optional Objective-C
10543 method parameters follow the C syntax, and may include '...'
10544 to denote a variable number of arguments. */
10545 parms = make_node (TREE_LIST);
10546 while (c_parser_next_token_is (parser, CPP_COMMA))
10547 {
10548 struct c_parm *parm;
10549 c_parser_consume_token (parser);
10550 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
10551 {
10552 ellipsis = true;
10553 c_parser_consume_token (parser);
10554 attr_err |= c_parser_objc_maybe_method_attributes
10555 (parser, attributes) ;
10556 break;
10557 }
10558 parm = c_parser_parameter_declaration (parser, NULL_TREE);
10559 if (parm == NULL)
10560 break;
10561 parms = chainon (parms,
10562 build_tree_list (NULL_TREE, grokparm (parm, expr)));
10563 }
10564 sel = list;
10565 }
10566 else
10567 attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
10568
10569 if (sel == NULL)
10570 {
10571 c_parser_error (parser, "objective-c method declaration is expected");
10572 return error_mark_node;
10573 }
10574
10575 if (attr_err)
10576 return error_mark_node;
10577
10578 return objc_build_method_signature (is_class_method, type, sel, parms, ellipsis);
10579 }
10580
10581 /* Parse an objc-type-name.
10582
10583 objc-type-name:
10584 objc-type-qualifiers[opt] type-name
10585 objc-type-qualifiers[opt]
10586
10587 objc-type-qualifiers:
10588 objc-type-qualifier
10589 objc-type-qualifiers objc-type-qualifier
10590
10591 objc-type-qualifier: one of
10592 in out inout bycopy byref oneway
10593 */
10594
10595 static tree
10596 c_parser_objc_type_name (c_parser *parser)
10597 {
10598 tree quals = NULL_TREE;
10599 struct c_type_name *type_name = NULL;
10600 tree type = NULL_TREE;
10601 while (true)
10602 {
10603 c_token *token = c_parser_peek_token (parser);
10604 if (token->type == CPP_KEYWORD
10605 && (token->keyword == RID_IN
10606 || token->keyword == RID_OUT
10607 || token->keyword == RID_INOUT
10608 || token->keyword == RID_BYCOPY
10609 || token->keyword == RID_BYREF
10610 || token->keyword == RID_ONEWAY))
10611 {
10612 quals = chainon (build_tree_list (NULL_TREE, token->value), quals);
10613 c_parser_consume_token (parser);
10614 }
10615 else
10616 break;
10617 }
10618 if (c_parser_next_tokens_start_typename (parser, cla_prefer_type))
10619 type_name = c_parser_type_name (parser);
10620 if (type_name)
10621 type = groktypename (type_name, NULL, NULL);
10622
10623 /* If the type is unknown, and error has already been produced and
10624 we need to recover from the error. In that case, use NULL_TREE
10625 for the type, as if no type had been specified; this will use the
10626 default type ('id') which is good for error recovery. */
10627 if (type == error_mark_node)
10628 type = NULL_TREE;
10629
10630 return build_tree_list (quals, type);
10631 }
10632
10633 /* Parse objc-protocol-refs.
10634
10635 objc-protocol-refs:
10636 < identifier-list >
10637 */
10638
10639 static tree
10640 c_parser_objc_protocol_refs (c_parser *parser)
10641 {
10642 tree list = NULL_TREE;
10643 gcc_assert (c_parser_next_token_is (parser, CPP_LESS));
10644 c_parser_consume_token (parser);
10645 /* Any identifiers, including those declared as type names, are OK
10646 here. */
10647 while (true)
10648 {
10649 tree id;
10650 if (c_parser_next_token_is_not (parser, CPP_NAME))
10651 {
10652 c_parser_error (parser, "expected identifier");
10653 break;
10654 }
10655 id = c_parser_peek_token (parser)->value;
10656 list = chainon (list, build_tree_list (NULL_TREE, id));
10657 c_parser_consume_token (parser);
10658 if (c_parser_next_token_is (parser, CPP_COMMA))
10659 c_parser_consume_token (parser);
10660 else
10661 break;
10662 }
10663 c_parser_require (parser, CPP_GREATER, "expected %<>%>");
10664 return list;
10665 }
10666
10667 /* Parse an objc-try-catch-finally-statement.
10668
10669 objc-try-catch-finally-statement:
10670 @try compound-statement objc-catch-list[opt]
10671 @try compound-statement objc-catch-list[opt] @finally compound-statement
10672
10673 objc-catch-list:
10674 @catch ( objc-catch-parameter-declaration ) compound-statement
10675 objc-catch-list @catch ( objc-catch-parameter-declaration ) compound-statement
10676
10677 objc-catch-parameter-declaration:
10678 parameter-declaration
10679 '...'
10680
10681 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
10682
10683 PS: This function is identical to cp_parser_objc_try_catch_finally_statement
10684 for C++. Keep them in sync. */
10685
10686 static void
10687 c_parser_objc_try_catch_finally_statement (c_parser *parser)
10688 {
10689 location_t location;
10690 tree stmt;
10691
10692 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_TRY));
10693 c_parser_consume_token (parser);
10694 location = c_parser_peek_token (parser)->location;
10695 objc_maybe_warn_exceptions (location);
10696 stmt = c_parser_compound_statement (parser);
10697 objc_begin_try_stmt (location, stmt);
10698
10699 while (c_parser_next_token_is_keyword (parser, RID_AT_CATCH))
10700 {
10701 struct c_parm *parm;
10702 tree parameter_declaration = error_mark_node;
10703 bool seen_open_paren = false;
10704
10705 c_parser_consume_token (parser);
10706 matching_parens parens;
10707 if (!parens.require_open (parser))
10708 seen_open_paren = true;
10709 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
10710 {
10711 /* We have "@catch (...)" (where the '...' are literally
10712 what is in the code). Skip the '...'.
10713 parameter_declaration is set to NULL_TREE, and
10714 objc_being_catch_clauses() knows that that means
10715 '...'. */
10716 c_parser_consume_token (parser);
10717 parameter_declaration = NULL_TREE;
10718 }
10719 else
10720 {
10721 /* We have "@catch (NSException *exception)" or something
10722 like that. Parse the parameter declaration. */
10723 parm = c_parser_parameter_declaration (parser, NULL_TREE);
10724 if (parm == NULL)
10725 parameter_declaration = error_mark_node;
10726 else
10727 parameter_declaration = grokparm (parm, NULL);
10728 }
10729 if (seen_open_paren)
10730 parens.require_close (parser);
10731 else
10732 {
10733 /* If there was no open parenthesis, we are recovering from
10734 an error, and we are trying to figure out what mistake
10735 the user has made. */
10736
10737 /* If there is an immediate closing parenthesis, the user
10738 probably forgot the opening one (ie, they typed "@catch
10739 NSException *e)". Parse the closing parenthesis and keep
10740 going. */
10741 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
10742 c_parser_consume_token (parser);
10743
10744 /* If these is no immediate closing parenthesis, the user
10745 probably doesn't know that parenthesis are required at
10746 all (ie, they typed "@catch NSException *e"). So, just
10747 forget about the closing parenthesis and keep going. */
10748 }
10749 objc_begin_catch_clause (parameter_declaration);
10750 if (c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
10751 c_parser_compound_statement_nostart (parser);
10752 objc_finish_catch_clause ();
10753 }
10754 if (c_parser_next_token_is_keyword (parser, RID_AT_FINALLY))
10755 {
10756 c_parser_consume_token (parser);
10757 location = c_parser_peek_token (parser)->location;
10758 stmt = c_parser_compound_statement (parser);
10759 objc_build_finally_clause (location, stmt);
10760 }
10761 objc_finish_try_stmt ();
10762 }
10763
10764 /* Parse an objc-synchronized-statement.
10765
10766 objc-synchronized-statement:
10767 @synchronized ( expression ) compound-statement
10768 */
10769
10770 static void
10771 c_parser_objc_synchronized_statement (c_parser *parser)
10772 {
10773 location_t loc;
10774 tree expr, stmt;
10775 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNCHRONIZED));
10776 c_parser_consume_token (parser);
10777 loc = c_parser_peek_token (parser)->location;
10778 objc_maybe_warn_exceptions (loc);
10779 matching_parens parens;
10780 if (parens.require_open (parser))
10781 {
10782 struct c_expr ce = c_parser_expression (parser);
10783 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
10784 expr = ce.value;
10785 expr = c_fully_fold (expr, false, NULL);
10786 parens.skip_until_found_close (parser);
10787 }
10788 else
10789 expr = error_mark_node;
10790 stmt = c_parser_compound_statement (parser);
10791 objc_build_synchronized (loc, expr, stmt);
10792 }
10793
10794 /* Parse an objc-selector; return NULL_TREE without an error if the
10795 next token is not an objc-selector.
10796
10797 objc-selector:
10798 identifier
10799 one of
10800 enum struct union if else while do for switch case default
10801 break continue return goto asm sizeof typeof __alignof
10802 unsigned long const short volatile signed restrict _Complex
10803 in out inout bycopy byref oneway int char float double void _Bool
10804 _Atomic
10805
10806 ??? Why this selection of keywords but not, for example, storage
10807 class specifiers? */
10808
10809 static tree
10810 c_parser_objc_selector (c_parser *parser)
10811 {
10812 c_token *token = c_parser_peek_token (parser);
10813 tree value = token->value;
10814 if (token->type == CPP_NAME)
10815 {
10816 c_parser_consume_token (parser);
10817 return value;
10818 }
10819 if (token->type != CPP_KEYWORD)
10820 return NULL_TREE;
10821 switch (token->keyword)
10822 {
10823 case RID_ENUM:
10824 case RID_STRUCT:
10825 case RID_UNION:
10826 case RID_IF:
10827 case RID_ELSE:
10828 case RID_WHILE:
10829 case RID_DO:
10830 case RID_FOR:
10831 case RID_SWITCH:
10832 case RID_CASE:
10833 case RID_DEFAULT:
10834 case RID_BREAK:
10835 case RID_CONTINUE:
10836 case RID_RETURN:
10837 case RID_GOTO:
10838 case RID_ASM:
10839 case RID_SIZEOF:
10840 case RID_TYPEOF:
10841 case RID_ALIGNOF:
10842 case RID_UNSIGNED:
10843 case RID_LONG:
10844 case RID_CONST:
10845 case RID_SHORT:
10846 case RID_VOLATILE:
10847 case RID_SIGNED:
10848 case RID_RESTRICT:
10849 case RID_COMPLEX:
10850 case RID_IN:
10851 case RID_OUT:
10852 case RID_INOUT:
10853 case RID_BYCOPY:
10854 case RID_BYREF:
10855 case RID_ONEWAY:
10856 case RID_INT:
10857 case RID_CHAR:
10858 case RID_FLOAT:
10859 case RID_DOUBLE:
10860 CASE_RID_FLOATN_NX:
10861 case RID_VOID:
10862 case RID_BOOL:
10863 case RID_ATOMIC:
10864 case RID_AUTO_TYPE:
10865 case RID_INT_N_0:
10866 case RID_INT_N_1:
10867 case RID_INT_N_2:
10868 case RID_INT_N_3:
10869 c_parser_consume_token (parser);
10870 return value;
10871 default:
10872 return NULL_TREE;
10873 }
10874 }
10875
10876 /* Parse an objc-selector-arg.
10877
10878 objc-selector-arg:
10879 objc-selector
10880 objc-keywordname-list
10881
10882 objc-keywordname-list:
10883 objc-keywordname
10884 objc-keywordname-list objc-keywordname
10885
10886 objc-keywordname:
10887 objc-selector :
10888 :
10889 */
10890
10891 static tree
10892 c_parser_objc_selector_arg (c_parser *parser)
10893 {
10894 tree sel = c_parser_objc_selector (parser);
10895 tree list = NULL_TREE;
10896 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
10897 return sel;
10898 while (true)
10899 {
10900 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
10901 return list;
10902 list = chainon (list, build_tree_list (sel, NULL_TREE));
10903 sel = c_parser_objc_selector (parser);
10904 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
10905 break;
10906 }
10907 return list;
10908 }
10909
10910 /* Parse an objc-receiver.
10911
10912 objc-receiver:
10913 expression
10914 class-name
10915 type-name
10916 */
10917
10918 static tree
10919 c_parser_objc_receiver (c_parser *parser)
10920 {
10921 location_t loc = c_parser_peek_token (parser)->location;
10922
10923 if (c_parser_peek_token (parser)->type == CPP_NAME
10924 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
10925 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
10926 {
10927 tree id = c_parser_peek_token (parser)->value;
10928 c_parser_consume_token (parser);
10929 return objc_get_class_reference (id);
10930 }
10931 struct c_expr ce = c_parser_expression (parser);
10932 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
10933 return c_fully_fold (ce.value, false, NULL);
10934 }
10935
10936 /* Parse objc-message-args.
10937
10938 objc-message-args:
10939 objc-selector
10940 objc-keywordarg-list
10941
10942 objc-keywordarg-list:
10943 objc-keywordarg
10944 objc-keywordarg-list objc-keywordarg
10945
10946 objc-keywordarg:
10947 objc-selector : objc-keywordexpr
10948 : objc-keywordexpr
10949 */
10950
10951 static tree
10952 c_parser_objc_message_args (c_parser *parser)
10953 {
10954 tree sel = c_parser_objc_selector (parser);
10955 tree list = NULL_TREE;
10956 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
10957 return sel;
10958 while (true)
10959 {
10960 tree keywordexpr;
10961 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
10962 return error_mark_node;
10963 keywordexpr = c_parser_objc_keywordexpr (parser);
10964 list = chainon (list, build_tree_list (sel, keywordexpr));
10965 sel = c_parser_objc_selector (parser);
10966 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
10967 break;
10968 }
10969 return list;
10970 }
10971
10972 /* Parse an objc-keywordexpr.
10973
10974 objc-keywordexpr:
10975 nonempty-expr-list
10976 */
10977
10978 static tree
10979 c_parser_objc_keywordexpr (c_parser *parser)
10980 {
10981 tree ret;
10982 vec<tree, va_gc> *expr_list = c_parser_expr_list (parser, true, true,
10983 NULL, NULL, NULL, NULL);
10984 if (vec_safe_length (expr_list) == 1)
10985 {
10986 /* Just return the expression, remove a level of
10987 indirection. */
10988 ret = (*expr_list)[0];
10989 }
10990 else
10991 {
10992 /* We have a comma expression, we will collapse later. */
10993 ret = build_tree_list_vec (expr_list);
10994 }
10995 release_tree_vector (expr_list);
10996 return ret;
10997 }
10998
10999 /* A check, needed in several places, that ObjC interface, implementation or
11000 method definitions are not prefixed by incorrect items. */
11001 static bool
11002 c_parser_objc_diagnose_bad_element_prefix (c_parser *parser,
11003 struct c_declspecs *specs)
11004 {
11005 if (!specs->declspecs_seen_p || specs->non_sc_seen_p
11006 || specs->typespec_kind != ctsk_none)
11007 {
11008 c_parser_error (parser,
11009 "no type or storage class may be specified here,");
11010 c_parser_skip_to_end_of_block_or_statement (parser);
11011 return true;
11012 }
11013 return false;
11014 }
11015
11016 /* Parse an Objective-C @property declaration. The syntax is:
11017
11018 objc-property-declaration:
11019 '@property' objc-property-attributes[opt] struct-declaration ;
11020
11021 objc-property-attributes:
11022 '(' objc-property-attribute-list ')'
11023
11024 objc-property-attribute-list:
11025 objc-property-attribute
11026 objc-property-attribute-list, objc-property-attribute
11027
11028 objc-property-attribute
11029 'getter' = identifier
11030 'setter' = identifier
11031 'readonly'
11032 'readwrite'
11033 'assign'
11034 'retain'
11035 'copy'
11036 'nonatomic'
11037
11038 For example:
11039 @property NSString *name;
11040 @property (readonly) id object;
11041 @property (retain, nonatomic, getter=getTheName) id name;
11042 @property int a, b, c;
11043
11044 PS: This function is identical to cp_parser_objc_at_propery_declaration
11045 for C++. Keep them in sync. */
11046 static void
11047 c_parser_objc_at_property_declaration (c_parser *parser)
11048 {
11049 /* The following variables hold the attributes of the properties as
11050 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
11051 seen. When we see an attribute, we set them to 'true' (if they
11052 are boolean properties) or to the identifier (if they have an
11053 argument, ie, for getter and setter). Note that here we only
11054 parse the list of attributes, check the syntax and accumulate the
11055 attributes that we find. objc_add_property_declaration() will
11056 then process the information. */
11057 bool property_assign = false;
11058 bool property_copy = false;
11059 tree property_getter_ident = NULL_TREE;
11060 bool property_nonatomic = false;
11061 bool property_readonly = false;
11062 bool property_readwrite = false;
11063 bool property_retain = false;
11064 tree property_setter_ident = NULL_TREE;
11065
11066 /* 'properties' is the list of properties that we read. Usually a
11067 single one, but maybe more (eg, in "@property int a, b, c;" there
11068 are three). */
11069 tree properties;
11070 location_t loc;
11071
11072 loc = c_parser_peek_token (parser)->location;
11073 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROPERTY));
11074
11075 c_parser_consume_token (parser); /* Eat '@property'. */
11076
11077 /* Parse the optional attribute list... */
11078 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
11079 {
11080 matching_parens parens;
11081
11082 /* Eat the '(' */
11083 parens.consume_open (parser);
11084
11085 /* Property attribute keywords are valid now. */
11086 parser->objc_property_attr_context = true;
11087
11088 while (true)
11089 {
11090 bool syntax_error = false;
11091 c_token *token = c_parser_peek_token (parser);
11092 enum rid keyword;
11093
11094 if (token->type != CPP_KEYWORD)
11095 {
11096 if (token->type == CPP_CLOSE_PAREN)
11097 c_parser_error (parser, "expected identifier");
11098 else
11099 {
11100 c_parser_consume_token (parser);
11101 c_parser_error (parser, "unknown property attribute");
11102 }
11103 break;
11104 }
11105 keyword = token->keyword;
11106 c_parser_consume_token (parser);
11107 switch (keyword)
11108 {
11109 case RID_ASSIGN: property_assign = true; break;
11110 case RID_COPY: property_copy = true; break;
11111 case RID_NONATOMIC: property_nonatomic = true; break;
11112 case RID_READONLY: property_readonly = true; break;
11113 case RID_READWRITE: property_readwrite = true; break;
11114 case RID_RETAIN: property_retain = true; break;
11115
11116 case RID_GETTER:
11117 case RID_SETTER:
11118 if (c_parser_next_token_is_not (parser, CPP_EQ))
11119 {
11120 if (keyword == RID_GETTER)
11121 c_parser_error (parser,
11122 "missing %<=%> (after %<getter%> attribute)");
11123 else
11124 c_parser_error (parser,
11125 "missing %<=%> (after %<setter%> attribute)");
11126 syntax_error = true;
11127 break;
11128 }
11129 c_parser_consume_token (parser); /* eat the = */
11130 if (c_parser_next_token_is_not (parser, CPP_NAME))
11131 {
11132 c_parser_error (parser, "expected identifier");
11133 syntax_error = true;
11134 break;
11135 }
11136 if (keyword == RID_SETTER)
11137 {
11138 if (property_setter_ident != NULL_TREE)
11139 c_parser_error (parser, "the %<setter%> attribute may only be specified once");
11140 else
11141 property_setter_ident = c_parser_peek_token (parser)->value;
11142 c_parser_consume_token (parser);
11143 if (c_parser_next_token_is_not (parser, CPP_COLON))
11144 c_parser_error (parser, "setter name must terminate with %<:%>");
11145 else
11146 c_parser_consume_token (parser);
11147 }
11148 else
11149 {
11150 if (property_getter_ident != NULL_TREE)
11151 c_parser_error (parser, "the %<getter%> attribute may only be specified once");
11152 else
11153 property_getter_ident = c_parser_peek_token (parser)->value;
11154 c_parser_consume_token (parser);
11155 }
11156 break;
11157 default:
11158 c_parser_error (parser, "unknown property attribute");
11159 syntax_error = true;
11160 break;
11161 }
11162
11163 if (syntax_error)
11164 break;
11165
11166 if (c_parser_next_token_is (parser, CPP_COMMA))
11167 c_parser_consume_token (parser);
11168 else
11169 break;
11170 }
11171 parser->objc_property_attr_context = false;
11172 parens.skip_until_found_close (parser);
11173 }
11174 /* ... and the property declaration(s). */
11175 properties = c_parser_struct_declaration (parser);
11176
11177 if (properties == error_mark_node)
11178 {
11179 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
11180 parser->error = false;
11181 return;
11182 }
11183
11184 if (properties == NULL_TREE)
11185 c_parser_error (parser, "expected identifier");
11186 else
11187 {
11188 /* Comma-separated properties are chained together in
11189 reverse order; add them one by one. */
11190 properties = nreverse (properties);
11191
11192 for (; properties; properties = TREE_CHAIN (properties))
11193 objc_add_property_declaration (loc, copy_node (properties),
11194 property_readonly, property_readwrite,
11195 property_assign, property_retain,
11196 property_copy, property_nonatomic,
11197 property_getter_ident, property_setter_ident);
11198 }
11199
11200 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
11201 parser->error = false;
11202 }
11203
11204 /* Parse an Objective-C @synthesize declaration. The syntax is:
11205
11206 objc-synthesize-declaration:
11207 @synthesize objc-synthesize-identifier-list ;
11208
11209 objc-synthesize-identifier-list:
11210 objc-synthesize-identifier
11211 objc-synthesize-identifier-list, objc-synthesize-identifier
11212
11213 objc-synthesize-identifier
11214 identifier
11215 identifier = identifier
11216
11217 For example:
11218 @synthesize MyProperty;
11219 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
11220
11221 PS: This function is identical to cp_parser_objc_at_synthesize_declaration
11222 for C++. Keep them in sync.
11223 */
11224 static void
11225 c_parser_objc_at_synthesize_declaration (c_parser *parser)
11226 {
11227 tree list = NULL_TREE;
11228 location_t loc;
11229 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNTHESIZE));
11230 loc = c_parser_peek_token (parser)->location;
11231
11232 c_parser_consume_token (parser);
11233 while (true)
11234 {
11235 tree property, ivar;
11236 if (c_parser_next_token_is_not (parser, CPP_NAME))
11237 {
11238 c_parser_error (parser, "expected identifier");
11239 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
11240 /* Once we find the semicolon, we can resume normal parsing.
11241 We have to reset parser->error manually because
11242 c_parser_skip_until_found() won't reset it for us if the
11243 next token is precisely a semicolon. */
11244 parser->error = false;
11245 return;
11246 }
11247 property = c_parser_peek_token (parser)->value;
11248 c_parser_consume_token (parser);
11249 if (c_parser_next_token_is (parser, CPP_EQ))
11250 {
11251 c_parser_consume_token (parser);
11252 if (c_parser_next_token_is_not (parser, CPP_NAME))
11253 {
11254 c_parser_error (parser, "expected identifier");
11255 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
11256 parser->error = false;
11257 return;
11258 }
11259 ivar = c_parser_peek_token (parser)->value;
11260 c_parser_consume_token (parser);
11261 }
11262 else
11263 ivar = NULL_TREE;
11264 list = chainon (list, build_tree_list (ivar, property));
11265 if (c_parser_next_token_is (parser, CPP_COMMA))
11266 c_parser_consume_token (parser);
11267 else
11268 break;
11269 }
11270 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
11271 objc_add_synthesize_declaration (loc, list);
11272 }
11273
11274 /* Parse an Objective-C @dynamic declaration. The syntax is:
11275
11276 objc-dynamic-declaration:
11277 @dynamic identifier-list ;
11278
11279 For example:
11280 @dynamic MyProperty;
11281 @dynamic MyProperty, AnotherProperty;
11282
11283 PS: This function is identical to cp_parser_objc_at_dynamic_declaration
11284 for C++. Keep them in sync.
11285 */
11286 static void
11287 c_parser_objc_at_dynamic_declaration (c_parser *parser)
11288 {
11289 tree list = NULL_TREE;
11290 location_t loc;
11291 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_DYNAMIC));
11292 loc = c_parser_peek_token (parser)->location;
11293
11294 c_parser_consume_token (parser);
11295 while (true)
11296 {
11297 tree property;
11298 if (c_parser_next_token_is_not (parser, CPP_NAME))
11299 {
11300 c_parser_error (parser, "expected identifier");
11301 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
11302 parser->error = false;
11303 return;
11304 }
11305 property = c_parser_peek_token (parser)->value;
11306 list = chainon (list, build_tree_list (NULL_TREE, property));
11307 c_parser_consume_token (parser);
11308 if (c_parser_next_token_is (parser, CPP_COMMA))
11309 c_parser_consume_token (parser);
11310 else
11311 break;
11312 }
11313 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
11314 objc_add_dynamic_declaration (loc, list);
11315 }
11316
11317 \f
11318 /* Parse a pragma GCC ivdep. */
11319
11320 static bool
11321 c_parse_pragma_ivdep (c_parser *parser)
11322 {
11323 c_parser_consume_pragma (parser);
11324 c_parser_skip_to_pragma_eol (parser);
11325 return true;
11326 }
11327
11328 /* Parse a pragma GCC unroll. */
11329
11330 static unsigned short
11331 c_parser_pragma_unroll (c_parser *parser)
11332 {
11333 unsigned short unroll;
11334 c_parser_consume_pragma (parser);
11335 location_t location = c_parser_peek_token (parser)->location;
11336 tree expr = c_parser_expr_no_commas (parser, NULL).value;
11337 mark_exp_read (expr);
11338 expr = c_fully_fold (expr, false, NULL);
11339 HOST_WIDE_INT lunroll = 0;
11340 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr))
11341 || TREE_CODE (expr) != INTEGER_CST
11342 || (lunroll = tree_to_shwi (expr)) < 0
11343 || lunroll >= USHRT_MAX)
11344 {
11345 error_at (location, "%<#pragma GCC unroll%> requires an"
11346 " assignment-expression that evaluates to a non-negative"
11347 " integral constant less than %u", USHRT_MAX);
11348 unroll = 0;
11349 }
11350 else
11351 {
11352 unroll = (unsigned short)lunroll;
11353 if (unroll == 0)
11354 unroll = 1;
11355 }
11356
11357 c_parser_skip_to_pragma_eol (parser);
11358 return unroll;
11359 }
11360
11361 /* Handle pragmas. Some OpenMP pragmas are associated with, and therefore
11362 should be considered, statements. ALLOW_STMT is true if we're within
11363 the context of a function and such pragmas are to be allowed. Returns
11364 true if we actually parsed such a pragma. */
11365
11366 static bool
11367 c_parser_pragma (c_parser *parser, enum pragma_context context, bool *if_p)
11368 {
11369 unsigned int id;
11370 const char *construct = NULL;
11371
11372 id = c_parser_peek_token (parser)->pragma_kind;
11373 gcc_assert (id != PRAGMA_NONE);
11374
11375 switch (id)
11376 {
11377 case PRAGMA_OACC_DECLARE:
11378 c_parser_oacc_declare (parser);
11379 return false;
11380
11381 case PRAGMA_OACC_ENTER_DATA:
11382 if (context != pragma_compound)
11383 {
11384 construct = "acc enter data";
11385 in_compound:
11386 if (context == pragma_stmt)
11387 {
11388 error_at (c_parser_peek_token (parser)->location,
11389 "%<#pragma %s%> may only be used in compound "
11390 "statements", construct);
11391 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
11392 return false;
11393 }
11394 goto bad_stmt;
11395 }
11396 c_parser_oacc_enter_exit_data (parser, true);
11397 return false;
11398
11399 case PRAGMA_OACC_EXIT_DATA:
11400 if (context != pragma_compound)
11401 {
11402 construct = "acc exit data";
11403 goto in_compound;
11404 }
11405 c_parser_oacc_enter_exit_data (parser, false);
11406 return false;
11407
11408 case PRAGMA_OACC_ROUTINE:
11409 if (context != pragma_external)
11410 {
11411 error_at (c_parser_peek_token (parser)->location,
11412 "%<#pragma acc routine%> must be at file scope");
11413 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
11414 return false;
11415 }
11416 c_parser_oacc_routine (parser, context);
11417 return false;
11418
11419 case PRAGMA_OACC_UPDATE:
11420 if (context != pragma_compound)
11421 {
11422 construct = "acc update";
11423 goto in_compound;
11424 }
11425 c_parser_oacc_update (parser);
11426 return false;
11427
11428 case PRAGMA_OMP_BARRIER:
11429 if (context != pragma_compound)
11430 {
11431 construct = "omp barrier";
11432 goto in_compound;
11433 }
11434 c_parser_omp_barrier (parser);
11435 return false;
11436
11437 case PRAGMA_OMP_DEPOBJ:
11438 if (context != pragma_compound)
11439 {
11440 construct = "omp depobj";
11441 goto in_compound;
11442 }
11443 c_parser_omp_depobj (parser);
11444 return false;
11445
11446 case PRAGMA_OMP_FLUSH:
11447 if (context != pragma_compound)
11448 {
11449 construct = "omp flush";
11450 goto in_compound;
11451 }
11452 c_parser_omp_flush (parser);
11453 return false;
11454
11455 case PRAGMA_OMP_TASKWAIT:
11456 if (context != pragma_compound)
11457 {
11458 construct = "omp taskwait";
11459 goto in_compound;
11460 }
11461 c_parser_omp_taskwait (parser);
11462 return false;
11463
11464 case PRAGMA_OMP_TASKYIELD:
11465 if (context != pragma_compound)
11466 {
11467 construct = "omp taskyield";
11468 goto in_compound;
11469 }
11470 c_parser_omp_taskyield (parser);
11471 return false;
11472
11473 case PRAGMA_OMP_CANCEL:
11474 if (context != pragma_compound)
11475 {
11476 construct = "omp cancel";
11477 goto in_compound;
11478 }
11479 c_parser_omp_cancel (parser);
11480 return false;
11481
11482 case PRAGMA_OMP_CANCELLATION_POINT:
11483 c_parser_omp_cancellation_point (parser, context);
11484 return false;
11485
11486 case PRAGMA_OMP_THREADPRIVATE:
11487 c_parser_omp_threadprivate (parser);
11488 return false;
11489
11490 case PRAGMA_OMP_TARGET:
11491 return c_parser_omp_target (parser, context, if_p);
11492
11493 case PRAGMA_OMP_END_DECLARE_TARGET:
11494 c_parser_omp_end_declare_target (parser);
11495 return false;
11496
11497 case PRAGMA_OMP_SECTION:
11498 error_at (c_parser_peek_token (parser)->location,
11499 "%<#pragma omp section%> may only be used in "
11500 "%<#pragma omp sections%> construct");
11501 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
11502 return false;
11503
11504 case PRAGMA_OMP_DECLARE:
11505 c_parser_omp_declare (parser, context);
11506 return false;
11507
11508 case PRAGMA_OMP_REQUIRES:
11509 c_parser_omp_requires (parser);
11510 return false;
11511
11512 case PRAGMA_OMP_ORDERED:
11513 return c_parser_omp_ordered (parser, context, if_p);
11514
11515 case PRAGMA_IVDEP:
11516 {
11517 const bool ivdep = c_parse_pragma_ivdep (parser);
11518 unsigned short unroll;
11519 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_UNROLL)
11520 unroll = c_parser_pragma_unroll (parser);
11521 else
11522 unroll = 0;
11523 if (!c_parser_next_token_is_keyword (parser, RID_FOR)
11524 && !c_parser_next_token_is_keyword (parser, RID_WHILE)
11525 && !c_parser_next_token_is_keyword (parser, RID_DO))
11526 {
11527 c_parser_error (parser, "for, while or do statement expected");
11528 return false;
11529 }
11530 if (c_parser_next_token_is_keyword (parser, RID_FOR))
11531 c_parser_for_statement (parser, ivdep, unroll, if_p);
11532 else if (c_parser_next_token_is_keyword (parser, RID_WHILE))
11533 c_parser_while_statement (parser, ivdep, unroll, if_p);
11534 else
11535 c_parser_do_statement (parser, ivdep, unroll);
11536 }
11537 return false;
11538
11539 case PRAGMA_UNROLL:
11540 {
11541 unsigned short unroll = c_parser_pragma_unroll (parser);
11542 bool ivdep;
11543 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_IVDEP)
11544 ivdep = c_parse_pragma_ivdep (parser);
11545 else
11546 ivdep = false;
11547 if (!c_parser_next_token_is_keyword (parser, RID_FOR)
11548 && !c_parser_next_token_is_keyword (parser, RID_WHILE)
11549 && !c_parser_next_token_is_keyword (parser, RID_DO))
11550 {
11551 c_parser_error (parser, "for, while or do statement expected");
11552 return false;
11553 }
11554 if (c_parser_next_token_is_keyword (parser, RID_FOR))
11555 c_parser_for_statement (parser, ivdep, unroll, if_p);
11556 else if (c_parser_next_token_is_keyword (parser, RID_WHILE))
11557 c_parser_while_statement (parser, ivdep, unroll, if_p);
11558 else
11559 c_parser_do_statement (parser, ivdep, unroll);
11560 }
11561 return false;
11562
11563 case PRAGMA_GCC_PCH_PREPROCESS:
11564 c_parser_error (parser, "%<#pragma GCC pch_preprocess%> must be first");
11565 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
11566 return false;
11567
11568 case PRAGMA_OACC_WAIT:
11569 if (context != pragma_compound)
11570 {
11571 construct = "acc wait";
11572 goto in_compound;
11573 }
11574 /* FALL THROUGH. */
11575
11576 default:
11577 if (id < PRAGMA_FIRST_EXTERNAL)
11578 {
11579 if (context != pragma_stmt && context != pragma_compound)
11580 {
11581 bad_stmt:
11582 c_parser_error (parser, "expected declaration specifiers");
11583 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
11584 return false;
11585 }
11586 c_parser_omp_construct (parser, if_p);
11587 return true;
11588 }
11589 break;
11590 }
11591
11592 c_parser_consume_pragma (parser);
11593 c_invoke_pragma_handler (id);
11594
11595 /* Skip to EOL, but suppress any error message. Those will have been
11596 generated by the handler routine through calling error, as opposed
11597 to calling c_parser_error. */
11598 parser->error = true;
11599 c_parser_skip_to_pragma_eol (parser);
11600
11601 return false;
11602 }
11603
11604 /* The interface the pragma parsers have to the lexer. */
11605
11606 enum cpp_ttype
11607 pragma_lex (tree *value, location_t *loc)
11608 {
11609 c_token *tok = c_parser_peek_token (the_parser);
11610 enum cpp_ttype ret = tok->type;
11611
11612 *value = tok->value;
11613 if (loc)
11614 *loc = tok->location;
11615
11616 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
11617 ret = CPP_EOF;
11618 else
11619 {
11620 if (ret == CPP_KEYWORD)
11621 ret = CPP_NAME;
11622 c_parser_consume_token (the_parser);
11623 }
11624
11625 return ret;
11626 }
11627
11628 static void
11629 c_parser_pragma_pch_preprocess (c_parser *parser)
11630 {
11631 tree name = NULL;
11632
11633 c_parser_consume_pragma (parser);
11634 if (c_parser_next_token_is (parser, CPP_STRING))
11635 {
11636 name = c_parser_peek_token (parser)->value;
11637 c_parser_consume_token (parser);
11638 }
11639 else
11640 c_parser_error (parser, "expected string literal");
11641 c_parser_skip_to_pragma_eol (parser);
11642
11643 if (name)
11644 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
11645 }
11646 \f
11647 /* OpenACC and OpenMP parsing routines. */
11648
11649 /* Returns name of the next clause.
11650 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
11651 the token is not consumed. Otherwise appropriate pragma_omp_clause is
11652 returned and the token is consumed. */
11653
11654 static pragma_omp_clause
11655 c_parser_omp_clause_name (c_parser *parser)
11656 {
11657 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
11658
11659 if (c_parser_next_token_is_keyword (parser, RID_AUTO))
11660 result = PRAGMA_OACC_CLAUSE_AUTO;
11661 else if (c_parser_next_token_is_keyword (parser, RID_IF))
11662 result = PRAGMA_OMP_CLAUSE_IF;
11663 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
11664 result = PRAGMA_OMP_CLAUSE_DEFAULT;
11665 else if (c_parser_next_token_is_keyword (parser, RID_FOR))
11666 result = PRAGMA_OMP_CLAUSE_FOR;
11667 else if (c_parser_next_token_is (parser, CPP_NAME))
11668 {
11669 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11670
11671 switch (p[0])
11672 {
11673 case 'a':
11674 if (!strcmp ("aligned", p))
11675 result = PRAGMA_OMP_CLAUSE_ALIGNED;
11676 else if (!strcmp ("async", p))
11677 result = PRAGMA_OACC_CLAUSE_ASYNC;
11678 break;
11679 case 'c':
11680 if (!strcmp ("collapse", p))
11681 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
11682 else if (!strcmp ("copy", p))
11683 result = PRAGMA_OACC_CLAUSE_COPY;
11684 else if (!strcmp ("copyin", p))
11685 result = PRAGMA_OMP_CLAUSE_COPYIN;
11686 else if (!strcmp ("copyout", p))
11687 result = PRAGMA_OACC_CLAUSE_COPYOUT;
11688 else if (!strcmp ("copyprivate", p))
11689 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
11690 else if (!strcmp ("create", p))
11691 result = PRAGMA_OACC_CLAUSE_CREATE;
11692 break;
11693 case 'd':
11694 if (!strcmp ("defaultmap", p))
11695 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
11696 else if (!strcmp ("delete", p))
11697 result = PRAGMA_OACC_CLAUSE_DELETE;
11698 else if (!strcmp ("depend", p))
11699 result = PRAGMA_OMP_CLAUSE_DEPEND;
11700 else if (!strcmp ("device", p))
11701 result = PRAGMA_OMP_CLAUSE_DEVICE;
11702 else if (!strcmp ("deviceptr", p))
11703 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
11704 else if (!strcmp ("device_resident", p))
11705 result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
11706 else if (!strcmp ("dist_schedule", p))
11707 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
11708 break;
11709 case 'f':
11710 if (!strcmp ("final", p))
11711 result = PRAGMA_OMP_CLAUSE_FINAL;
11712 else if (!strcmp ("finalize", p))
11713 result = PRAGMA_OACC_CLAUSE_FINALIZE;
11714 else if (!strcmp ("firstprivate", p))
11715 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
11716 else if (!strcmp ("from", p))
11717 result = PRAGMA_OMP_CLAUSE_FROM;
11718 break;
11719 case 'g':
11720 if (!strcmp ("gang", p))
11721 result = PRAGMA_OACC_CLAUSE_GANG;
11722 else if (!strcmp ("grainsize", p))
11723 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
11724 break;
11725 case 'h':
11726 if (!strcmp ("hint", p))
11727 result = PRAGMA_OMP_CLAUSE_HINT;
11728 else if (!strcmp ("host", p))
11729 result = PRAGMA_OACC_CLAUSE_HOST;
11730 break;
11731 case 'i':
11732 if (!strcmp ("if_present", p))
11733 result = PRAGMA_OACC_CLAUSE_IF_PRESENT;
11734 else if (!strcmp ("in_reduction", p))
11735 result = PRAGMA_OMP_CLAUSE_IN_REDUCTION;
11736 else if (!strcmp ("inbranch", p))
11737 result = PRAGMA_OMP_CLAUSE_INBRANCH;
11738 else if (!strcmp ("independent", p))
11739 result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
11740 else if (!strcmp ("is_device_ptr", p))
11741 result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
11742 break;
11743 case 'l':
11744 if (!strcmp ("lastprivate", p))
11745 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
11746 else if (!strcmp ("linear", p))
11747 result = PRAGMA_OMP_CLAUSE_LINEAR;
11748 else if (!strcmp ("link", p))
11749 result = PRAGMA_OMP_CLAUSE_LINK;
11750 break;
11751 case 'm':
11752 if (!strcmp ("map", p))
11753 result = PRAGMA_OMP_CLAUSE_MAP;
11754 else if (!strcmp ("mergeable", p))
11755 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
11756 break;
11757 case 'n':
11758 if (!strcmp ("nogroup", p))
11759 result = PRAGMA_OMP_CLAUSE_NOGROUP;
11760 else if (!strcmp ("nontemporal", p))
11761 result = PRAGMA_OMP_CLAUSE_NONTEMPORAL;
11762 else if (!strcmp ("notinbranch", p))
11763 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
11764 else if (!strcmp ("nowait", p))
11765 result = PRAGMA_OMP_CLAUSE_NOWAIT;
11766 else if (!strcmp ("num_gangs", p))
11767 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
11768 else if (!strcmp ("num_tasks", p))
11769 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
11770 else if (!strcmp ("num_teams", p))
11771 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
11772 else if (!strcmp ("num_threads", p))
11773 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
11774 else if (!strcmp ("num_workers", p))
11775 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
11776 break;
11777 case 'o':
11778 if (!strcmp ("ordered", p))
11779 result = PRAGMA_OMP_CLAUSE_ORDERED;
11780 break;
11781 case 'p':
11782 if (!strcmp ("parallel", p))
11783 result = PRAGMA_OMP_CLAUSE_PARALLEL;
11784 else if (!strcmp ("present", p))
11785 result = PRAGMA_OACC_CLAUSE_PRESENT;
11786 /* As of OpenACC 2.5, these are now aliases of the non-present_or
11787 clauses. */
11788 else if (!strcmp ("present_or_copy", p)
11789 || !strcmp ("pcopy", p))
11790 result = PRAGMA_OACC_CLAUSE_COPY;
11791 else if (!strcmp ("present_or_copyin", p)
11792 || !strcmp ("pcopyin", p))
11793 result = PRAGMA_OACC_CLAUSE_COPYIN;
11794 else if (!strcmp ("present_or_copyout", p)
11795 || !strcmp ("pcopyout", p))
11796 result = PRAGMA_OACC_CLAUSE_COPYOUT;
11797 else if (!strcmp ("present_or_create", p)
11798 || !strcmp ("pcreate", p))
11799 result = PRAGMA_OACC_CLAUSE_CREATE;
11800 else if (!strcmp ("priority", p))
11801 result = PRAGMA_OMP_CLAUSE_PRIORITY;
11802 else if (!strcmp ("private", p))
11803 result = PRAGMA_OMP_CLAUSE_PRIVATE;
11804 else if (!strcmp ("proc_bind", p))
11805 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
11806 break;
11807 case 'r':
11808 if (!strcmp ("reduction", p))
11809 result = PRAGMA_OMP_CLAUSE_REDUCTION;
11810 break;
11811 case 's':
11812 if (!strcmp ("safelen", p))
11813 result = PRAGMA_OMP_CLAUSE_SAFELEN;
11814 else if (!strcmp ("schedule", p))
11815 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
11816 else if (!strcmp ("sections", p))
11817 result = PRAGMA_OMP_CLAUSE_SECTIONS;
11818 else if (!strcmp ("self", p)) /* "self" is a synonym for "host". */
11819 result = PRAGMA_OACC_CLAUSE_HOST;
11820 else if (!strcmp ("seq", p))
11821 result = PRAGMA_OACC_CLAUSE_SEQ;
11822 else if (!strcmp ("shared", p))
11823 result = PRAGMA_OMP_CLAUSE_SHARED;
11824 else if (!strcmp ("simd", p))
11825 result = PRAGMA_OMP_CLAUSE_SIMD;
11826 else if (!strcmp ("simdlen", p))
11827 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
11828 break;
11829 case 't':
11830 if (!strcmp ("task_reduction", p))
11831 result = PRAGMA_OMP_CLAUSE_TASK_REDUCTION;
11832 else if (!strcmp ("taskgroup", p))
11833 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
11834 else if (!strcmp ("thread_limit", p))
11835 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
11836 else if (!strcmp ("threads", p))
11837 result = PRAGMA_OMP_CLAUSE_THREADS;
11838 else if (!strcmp ("tile", p))
11839 result = PRAGMA_OACC_CLAUSE_TILE;
11840 else if (!strcmp ("to", p))
11841 result = PRAGMA_OMP_CLAUSE_TO;
11842 break;
11843 case 'u':
11844 if (!strcmp ("uniform", p))
11845 result = PRAGMA_OMP_CLAUSE_UNIFORM;
11846 else if (!strcmp ("untied", p))
11847 result = PRAGMA_OMP_CLAUSE_UNTIED;
11848 else if (!strcmp ("use_device", p))
11849 result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
11850 else if (!strcmp ("use_device_ptr", p))
11851 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
11852 break;
11853 case 'v':
11854 if (!strcmp ("vector", p))
11855 result = PRAGMA_OACC_CLAUSE_VECTOR;
11856 else if (!strcmp ("vector_length", p))
11857 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
11858 break;
11859 case 'w':
11860 if (!strcmp ("wait", p))
11861 result = PRAGMA_OACC_CLAUSE_WAIT;
11862 else if (!strcmp ("worker", p))
11863 result = PRAGMA_OACC_CLAUSE_WORKER;
11864 break;
11865 }
11866 }
11867
11868 if (result != PRAGMA_OMP_CLAUSE_NONE)
11869 c_parser_consume_token (parser);
11870
11871 return result;
11872 }
11873
11874 /* Validate that a clause of the given type does not already exist. */
11875
11876 static void
11877 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
11878 const char *name)
11879 {
11880 tree c;
11881
11882 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
11883 if (OMP_CLAUSE_CODE (c) == code)
11884 {
11885 location_t loc = OMP_CLAUSE_LOCATION (c);
11886 error_at (loc, "too many %qs clauses", name);
11887 break;
11888 }
11889 }
11890
11891 /* OpenACC 2.0
11892 Parse wait clause or wait directive parameters. */
11893
11894 static tree
11895 c_parser_oacc_wait_list (c_parser *parser, location_t clause_loc, tree list)
11896 {
11897 vec<tree, va_gc> *args;
11898 tree t, args_tree;
11899
11900 matching_parens parens;
11901 if (!parens.require_open (parser))
11902 return list;
11903
11904 args = c_parser_expr_list (parser, false, true, NULL, NULL, NULL, NULL);
11905 args_tree = build_tree_list_vec (args);
11906
11907 for (t = args_tree; t; t = TREE_CHAIN (t))
11908 {
11909 tree targ = TREE_VALUE (t);
11910
11911 if (targ != error_mark_node)
11912 {
11913 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
11914 {
11915 c_parser_error (parser, "expression must be integral");
11916 targ = error_mark_node;
11917 }
11918 else
11919 {
11920 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
11921
11922 OMP_CLAUSE_DECL (c) = targ;
11923 OMP_CLAUSE_CHAIN (c) = list;
11924 list = c;
11925 }
11926 }
11927 }
11928
11929 release_tree_vector (args);
11930 parens.require_close (parser);
11931 return list;
11932 }
11933
11934 /* OpenACC 2.0, OpenMP 2.5:
11935 variable-list:
11936 identifier
11937 variable-list , identifier
11938
11939 If KIND is nonzero, create the appropriate node and install the
11940 decl in OMP_CLAUSE_DECL and add the node to the head of the list.
11941 If KIND is nonzero, CLAUSE_LOC is the location of the clause.
11942
11943 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
11944 return the list created. */
11945
11946 static tree
11947 c_parser_omp_variable_list (c_parser *parser,
11948 location_t clause_loc,
11949 enum omp_clause_code kind, tree list)
11950 {
11951 auto_vec<c_token> tokens;
11952 unsigned int tokens_avail = 0;
11953
11954 if (kind != OMP_CLAUSE_DEPEND
11955 && (c_parser_next_token_is_not (parser, CPP_NAME)
11956 || c_parser_peek_token (parser)->id_kind != C_ID_ID))
11957 c_parser_error (parser, "expected identifier");
11958
11959 while (kind == OMP_CLAUSE_DEPEND
11960 || (c_parser_next_token_is (parser, CPP_NAME)
11961 && c_parser_peek_token (parser)->id_kind == C_ID_ID))
11962 {
11963 bool array_section_p = false;
11964 if (kind == OMP_CLAUSE_DEPEND)
11965 {
11966 if (c_parser_next_token_is_not (parser, CPP_NAME)
11967 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
11968 {
11969 struct c_expr expr = c_parser_expr_no_commas (parser, NULL);
11970 if (expr.value != error_mark_node)
11971 {
11972 tree u = build_omp_clause (clause_loc, kind);
11973 OMP_CLAUSE_DECL (u) = expr.value;
11974 OMP_CLAUSE_CHAIN (u) = list;
11975 list = u;
11976 }
11977
11978 if (c_parser_next_token_is_not (parser, CPP_COMMA))
11979 break;
11980
11981 c_parser_consume_token (parser);
11982 continue;
11983 }
11984
11985 tokens.truncate (0);
11986 unsigned int nesting_depth = 0;
11987 while (1)
11988 {
11989 c_token *token = c_parser_peek_token (parser);
11990 switch (token->type)
11991 {
11992 case CPP_EOF:
11993 case CPP_PRAGMA_EOL:
11994 break;
11995 case CPP_OPEN_BRACE:
11996 case CPP_OPEN_PAREN:
11997 case CPP_OPEN_SQUARE:
11998 ++nesting_depth;
11999 goto add;
12000 case CPP_CLOSE_BRACE:
12001 case CPP_CLOSE_PAREN:
12002 case CPP_CLOSE_SQUARE:
12003 if (nesting_depth-- == 0)
12004 break;
12005 goto add;
12006 case CPP_COMMA:
12007 if (nesting_depth == 0)
12008 break;
12009 goto add;
12010 default:
12011 add:
12012 tokens.safe_push (*token);
12013 c_parser_consume_token (parser);
12014 continue;
12015 }
12016 break;
12017 }
12018
12019 /* Make sure nothing tries to read past the end of the tokens. */
12020 c_token eof_token;
12021 memset (&eof_token, 0, sizeof (eof_token));
12022 eof_token.type = CPP_EOF;
12023 tokens.safe_push (eof_token);
12024 tokens.safe_push (eof_token);
12025
12026 tokens_avail = parser->tokens_avail;
12027 gcc_assert (parser->tokens == &parser->tokens_buf[0]);
12028 parser->tokens = tokens.address ();
12029 parser->tokens_avail = tokens.length ();
12030 }
12031
12032 tree t = lookup_name (c_parser_peek_token (parser)->value);
12033
12034 if (t == NULL_TREE)
12035 {
12036 undeclared_variable (c_parser_peek_token (parser)->location,
12037 c_parser_peek_token (parser)->value);
12038 t = error_mark_node;
12039 }
12040
12041 c_parser_consume_token (parser);
12042
12043 if (t == error_mark_node)
12044 ;
12045 else if (kind != 0)
12046 {
12047 switch (kind)
12048 {
12049 case OMP_CLAUSE__CACHE_:
12050 /* The OpenACC cache directive explicitly only allows "array
12051 elements or subarrays". */
12052 if (c_parser_peek_token (parser)->type != CPP_OPEN_SQUARE)
12053 {
12054 c_parser_error (parser, "expected %<[%>");
12055 t = error_mark_node;
12056 break;
12057 }
12058 /* FALLTHROUGH */
12059 case OMP_CLAUSE_MAP:
12060 case OMP_CLAUSE_FROM:
12061 case OMP_CLAUSE_TO:
12062 while (c_parser_next_token_is (parser, CPP_DOT))
12063 {
12064 location_t op_loc = c_parser_peek_token (parser)->location;
12065 c_parser_consume_token (parser);
12066 if (!c_parser_next_token_is (parser, CPP_NAME))
12067 {
12068 c_parser_error (parser, "expected identifier");
12069 t = error_mark_node;
12070 break;
12071 }
12072
12073 c_token *comp_tok = c_parser_peek_token (parser);
12074 tree ident = comp_tok->value;
12075 location_t comp_loc = comp_tok->location;
12076 c_parser_consume_token (parser);
12077 t = build_component_ref (op_loc, t, ident, comp_loc);
12078 }
12079 /* FALLTHROUGH */
12080 case OMP_CLAUSE_DEPEND:
12081 case OMP_CLAUSE_REDUCTION:
12082 case OMP_CLAUSE_IN_REDUCTION:
12083 case OMP_CLAUSE_TASK_REDUCTION:
12084 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
12085 {
12086 tree low_bound = NULL_TREE, length = NULL_TREE;
12087
12088 c_parser_consume_token (parser);
12089 if (!c_parser_next_token_is (parser, CPP_COLON))
12090 {
12091 location_t expr_loc
12092 = c_parser_peek_token (parser)->location;
12093 c_expr expr = c_parser_expression (parser);
12094 expr = convert_lvalue_to_rvalue (expr_loc, expr,
12095 false, true);
12096 low_bound = expr.value;
12097 }
12098 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
12099 length = integer_one_node;
12100 else
12101 {
12102 /* Look for `:'. */
12103 if (!c_parser_require (parser, CPP_COLON,
12104 "expected %<:%>"))
12105 {
12106 t = error_mark_node;
12107 break;
12108 }
12109 array_section_p = true;
12110 if (!c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
12111 {
12112 location_t expr_loc
12113 = c_parser_peek_token (parser)->location;
12114 c_expr expr = c_parser_expression (parser);
12115 expr = convert_lvalue_to_rvalue (expr_loc, expr,
12116 false, true);
12117 length = expr.value;
12118 }
12119 }
12120 /* Look for the closing `]'. */
12121 if (!c_parser_require (parser, CPP_CLOSE_SQUARE,
12122 "expected %<]%>"))
12123 {
12124 t = error_mark_node;
12125 break;
12126 }
12127
12128 t = tree_cons (low_bound, length, t);
12129 }
12130 if (kind == OMP_CLAUSE_DEPEND
12131 && t != error_mark_node
12132 && parser->tokens_avail != 2)
12133 {
12134 if (array_section_p)
12135 {
12136 error_at (c_parser_peek_token (parser)->location,
12137 "expected %<)%> or %<,%>");
12138 t = error_mark_node;
12139 }
12140 else
12141 {
12142 parser->tokens = tokens.address ();
12143 parser->tokens_avail = tokens.length ();
12144
12145 t = c_parser_expr_no_commas (parser, NULL).value;
12146 if (t != error_mark_node && parser->tokens_avail != 2)
12147 {
12148 error_at (c_parser_peek_token (parser)->location,
12149 "expected %<)%> or %<,%>");
12150 t = error_mark_node;
12151 }
12152 }
12153 }
12154 break;
12155 default:
12156 break;
12157 }
12158
12159 if (t != error_mark_node)
12160 {
12161 tree u = build_omp_clause (clause_loc, kind);
12162 OMP_CLAUSE_DECL (u) = t;
12163 OMP_CLAUSE_CHAIN (u) = list;
12164 list = u;
12165 }
12166 }
12167 else
12168 list = tree_cons (t, NULL_TREE, list);
12169
12170 if (kind == OMP_CLAUSE_DEPEND)
12171 {
12172 parser->tokens = &parser->tokens_buf[0];
12173 parser->tokens_avail = tokens_avail;
12174 }
12175 if (c_parser_next_token_is_not (parser, CPP_COMMA))
12176 break;
12177
12178 c_parser_consume_token (parser);
12179 }
12180
12181 return list;
12182 }
12183
12184 /* Similarly, but expect leading and trailing parenthesis. This is a very
12185 common case for OpenACC and OpenMP clauses. */
12186
12187 static tree
12188 c_parser_omp_var_list_parens (c_parser *parser, enum omp_clause_code kind,
12189 tree list)
12190 {
12191 /* The clauses location. */
12192 location_t loc = c_parser_peek_token (parser)->location;
12193
12194 matching_parens parens;
12195 if (parens.require_open (parser))
12196 {
12197 list = c_parser_omp_variable_list (parser, loc, kind, list);
12198 parens.skip_until_found_close (parser);
12199 }
12200 return list;
12201 }
12202
12203 /* OpenACC 2.0:
12204 copy ( variable-list )
12205 copyin ( variable-list )
12206 copyout ( variable-list )
12207 create ( variable-list )
12208 delete ( variable-list )
12209 present ( variable-list ) */
12210
12211 static tree
12212 c_parser_oacc_data_clause (c_parser *parser, pragma_omp_clause c_kind,
12213 tree list)
12214 {
12215 enum gomp_map_kind kind;
12216 switch (c_kind)
12217 {
12218 case PRAGMA_OACC_CLAUSE_COPY:
12219 kind = GOMP_MAP_TOFROM;
12220 break;
12221 case PRAGMA_OACC_CLAUSE_COPYIN:
12222 kind = GOMP_MAP_TO;
12223 break;
12224 case PRAGMA_OACC_CLAUSE_COPYOUT:
12225 kind = GOMP_MAP_FROM;
12226 break;
12227 case PRAGMA_OACC_CLAUSE_CREATE:
12228 kind = GOMP_MAP_ALLOC;
12229 break;
12230 case PRAGMA_OACC_CLAUSE_DELETE:
12231 kind = GOMP_MAP_RELEASE;
12232 break;
12233 case PRAGMA_OACC_CLAUSE_DEVICE:
12234 kind = GOMP_MAP_FORCE_TO;
12235 break;
12236 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
12237 kind = GOMP_MAP_DEVICE_RESIDENT;
12238 break;
12239 case PRAGMA_OACC_CLAUSE_HOST:
12240 kind = GOMP_MAP_FORCE_FROM;
12241 break;
12242 case PRAGMA_OACC_CLAUSE_LINK:
12243 kind = GOMP_MAP_LINK;
12244 break;
12245 case PRAGMA_OACC_CLAUSE_PRESENT:
12246 kind = GOMP_MAP_FORCE_PRESENT;
12247 break;
12248 default:
12249 gcc_unreachable ();
12250 }
12251 tree nl, c;
12252 nl = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_MAP, list);
12253
12254 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
12255 OMP_CLAUSE_SET_MAP_KIND (c, kind);
12256
12257 return nl;
12258 }
12259
12260 /* OpenACC 2.0:
12261 deviceptr ( variable-list ) */
12262
12263 static tree
12264 c_parser_oacc_data_clause_deviceptr (c_parser *parser, tree list)
12265 {
12266 location_t loc = c_parser_peek_token (parser)->location;
12267 tree vars, t;
12268
12269 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
12270 c_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
12271 variable-list must only allow for pointer variables. */
12272 vars = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
12273 for (t = vars; t && t; t = TREE_CHAIN (t))
12274 {
12275 tree v = TREE_PURPOSE (t);
12276
12277 /* FIXME diagnostics: Ideally we should keep individual
12278 locations for all the variables in the var list to make the
12279 following errors more precise. Perhaps
12280 c_parser_omp_var_list_parens() should construct a list of
12281 locations to go along with the var list. */
12282
12283 if (!VAR_P (v) && TREE_CODE (v) != PARM_DECL)
12284 error_at (loc, "%qD is not a variable", v);
12285 else if (TREE_TYPE (v) == error_mark_node)
12286 ;
12287 else if (!POINTER_TYPE_P (TREE_TYPE (v)))
12288 error_at (loc, "%qD is not a pointer variable", v);
12289
12290 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
12291 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
12292 OMP_CLAUSE_DECL (u) = v;
12293 OMP_CLAUSE_CHAIN (u) = list;
12294 list = u;
12295 }
12296
12297 return list;
12298 }
12299
12300 /* OpenACC 2.0, OpenMP 3.0:
12301 collapse ( constant-expression ) */
12302
12303 static tree
12304 c_parser_omp_clause_collapse (c_parser *parser, tree list)
12305 {
12306 tree c, num = error_mark_node;
12307 HOST_WIDE_INT n;
12308 location_t loc;
12309
12310 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse");
12311 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile");
12312
12313 loc = c_parser_peek_token (parser)->location;
12314 matching_parens parens;
12315 if (parens.require_open (parser))
12316 {
12317 num = c_parser_expr_no_commas (parser, NULL).value;
12318 parens.skip_until_found_close (parser);
12319 }
12320 if (num == error_mark_node)
12321 return list;
12322 mark_exp_read (num);
12323 num = c_fully_fold (num, false, NULL);
12324 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
12325 || !tree_fits_shwi_p (num)
12326 || (n = tree_to_shwi (num)) <= 0
12327 || (int) n != n)
12328 {
12329 error_at (loc,
12330 "collapse argument needs positive constant integer expression");
12331 return list;
12332 }
12333 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
12334 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
12335 OMP_CLAUSE_CHAIN (c) = list;
12336 return c;
12337 }
12338
12339 /* OpenMP 2.5:
12340 copyin ( variable-list ) */
12341
12342 static tree
12343 c_parser_omp_clause_copyin (c_parser *parser, tree list)
12344 {
12345 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYIN, list);
12346 }
12347
12348 /* OpenMP 2.5:
12349 copyprivate ( variable-list ) */
12350
12351 static tree
12352 c_parser_omp_clause_copyprivate (c_parser *parser, tree list)
12353 {
12354 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYPRIVATE, list);
12355 }
12356
12357 /* OpenMP 2.5:
12358 default ( none | shared )
12359
12360 OpenACC:
12361 default ( none | present ) */
12362
12363 static tree
12364 c_parser_omp_clause_default (c_parser *parser, tree list, bool is_oacc)
12365 {
12366 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
12367 location_t loc = c_parser_peek_token (parser)->location;
12368 tree c;
12369
12370 matching_parens parens;
12371 if (!parens.require_open (parser))
12372 return list;
12373 if (c_parser_next_token_is (parser, CPP_NAME))
12374 {
12375 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12376
12377 switch (p[0])
12378 {
12379 case 'n':
12380 if (strcmp ("none", p) != 0)
12381 goto invalid_kind;
12382 kind = OMP_CLAUSE_DEFAULT_NONE;
12383 break;
12384
12385 case 'p':
12386 if (strcmp ("present", p) != 0 || !is_oacc)
12387 goto invalid_kind;
12388 kind = OMP_CLAUSE_DEFAULT_PRESENT;
12389 break;
12390
12391 case 's':
12392 if (strcmp ("shared", p) != 0 || is_oacc)
12393 goto invalid_kind;
12394 kind = OMP_CLAUSE_DEFAULT_SHARED;
12395 break;
12396
12397 default:
12398 goto invalid_kind;
12399 }
12400
12401 c_parser_consume_token (parser);
12402 }
12403 else
12404 {
12405 invalid_kind:
12406 if (is_oacc)
12407 c_parser_error (parser, "expected %<none%> or %<present%>");
12408 else
12409 c_parser_error (parser, "expected %<none%> or %<shared%>");
12410 }
12411 parens.skip_until_found_close (parser);
12412
12413 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
12414 return list;
12415
12416 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
12417 c = build_omp_clause (loc, OMP_CLAUSE_DEFAULT);
12418 OMP_CLAUSE_CHAIN (c) = list;
12419 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
12420
12421 return c;
12422 }
12423
12424 /* OpenMP 2.5:
12425 firstprivate ( variable-list ) */
12426
12427 static tree
12428 c_parser_omp_clause_firstprivate (c_parser *parser, tree list)
12429 {
12430 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_FIRSTPRIVATE, list);
12431 }
12432
12433 /* OpenMP 3.1:
12434 final ( expression ) */
12435
12436 static tree
12437 c_parser_omp_clause_final (c_parser *parser, tree list)
12438 {
12439 location_t loc = c_parser_peek_token (parser)->location;
12440 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
12441 {
12442 matching_parens parens;
12443 tree t, c;
12444 if (!parens.require_open (parser))
12445 t = error_mark_node;
12446 else
12447 {
12448 location_t eloc = c_parser_peek_token (parser)->location;
12449 c_expr expr = c_parser_expr_no_commas (parser, NULL);
12450 t = convert_lvalue_to_rvalue (eloc, expr, true, true).value;
12451 t = c_objc_common_truthvalue_conversion (eloc, t);
12452 t = c_fully_fold (t, false, NULL);
12453 parens.skip_until_found_close (parser);
12454 }
12455
12456 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final");
12457
12458 c = build_omp_clause (loc, OMP_CLAUSE_FINAL);
12459 OMP_CLAUSE_FINAL_EXPR (c) = t;
12460 OMP_CLAUSE_CHAIN (c) = list;
12461 list = c;
12462 }
12463 else
12464 c_parser_error (parser, "expected %<(%>");
12465
12466 return list;
12467 }
12468
12469 /* OpenACC, OpenMP 2.5:
12470 if ( expression )
12471
12472 OpenMP 4.5:
12473 if ( directive-name-modifier : expression )
12474
12475 directive-name-modifier:
12476 parallel | task | taskloop | target data | target | target update
12477 | target enter data | target exit data
12478
12479 OpenMP 5.0:
12480 directive-name-modifier:
12481 ... | simd | cancel */
12482
12483 static tree
12484 c_parser_omp_clause_if (c_parser *parser, tree list, bool is_omp)
12485 {
12486 location_t location = c_parser_peek_token (parser)->location;
12487 enum tree_code if_modifier = ERROR_MARK;
12488
12489 matching_parens parens;
12490 if (!parens.require_open (parser))
12491 return list;
12492
12493 if (is_omp && c_parser_next_token_is (parser, CPP_NAME))
12494 {
12495 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12496 int n = 2;
12497 if (strcmp (p, "cancel") == 0)
12498 if_modifier = VOID_CST;
12499 else if (strcmp (p, "parallel") == 0)
12500 if_modifier = OMP_PARALLEL;
12501 else if (strcmp (p, "simd") == 0)
12502 if_modifier = OMP_SIMD;
12503 else if (strcmp (p, "task") == 0)
12504 if_modifier = OMP_TASK;
12505 else if (strcmp (p, "taskloop") == 0)
12506 if_modifier = OMP_TASKLOOP;
12507 else if (strcmp (p, "target") == 0)
12508 {
12509 if_modifier = OMP_TARGET;
12510 if (c_parser_peek_2nd_token (parser)->type == CPP_NAME)
12511 {
12512 p = IDENTIFIER_POINTER (c_parser_peek_2nd_token (parser)->value);
12513 if (strcmp ("data", p) == 0)
12514 if_modifier = OMP_TARGET_DATA;
12515 else if (strcmp ("update", p) == 0)
12516 if_modifier = OMP_TARGET_UPDATE;
12517 else if (strcmp ("enter", p) == 0)
12518 if_modifier = OMP_TARGET_ENTER_DATA;
12519 else if (strcmp ("exit", p) == 0)
12520 if_modifier = OMP_TARGET_EXIT_DATA;
12521 if (if_modifier != OMP_TARGET)
12522 {
12523 n = 3;
12524 c_parser_consume_token (parser);
12525 }
12526 else
12527 {
12528 location_t loc = c_parser_peek_2nd_token (parser)->location;
12529 error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
12530 "or %<exit%>");
12531 if_modifier = ERROR_MARK;
12532 }
12533 if (if_modifier == OMP_TARGET_ENTER_DATA
12534 || if_modifier == OMP_TARGET_EXIT_DATA)
12535 {
12536 if (c_parser_peek_2nd_token (parser)->type == CPP_NAME)
12537 {
12538 p = IDENTIFIER_POINTER
12539 (c_parser_peek_2nd_token (parser)->value);
12540 if (strcmp ("data", p) == 0)
12541 n = 4;
12542 }
12543 if (n == 4)
12544 c_parser_consume_token (parser);
12545 else
12546 {
12547 location_t loc
12548 = c_parser_peek_2nd_token (parser)->location;
12549 error_at (loc, "expected %<data%>");
12550 if_modifier = ERROR_MARK;
12551 }
12552 }
12553 }
12554 }
12555 if (if_modifier != ERROR_MARK)
12556 {
12557 if (c_parser_peek_2nd_token (parser)->type == CPP_COLON)
12558 {
12559 c_parser_consume_token (parser);
12560 c_parser_consume_token (parser);
12561 }
12562 else
12563 {
12564 if (n > 2)
12565 {
12566 location_t loc = c_parser_peek_2nd_token (parser)->location;
12567 error_at (loc, "expected %<:%>");
12568 }
12569 if_modifier = ERROR_MARK;
12570 }
12571 }
12572 }
12573
12574 location_t loc = c_parser_peek_token (parser)->location;
12575 c_expr expr = c_parser_expr_no_commas (parser, NULL);
12576 expr = convert_lvalue_to_rvalue (loc, expr, true, true);
12577 tree t = c_objc_common_truthvalue_conversion (loc, expr.value), c;
12578 t = c_fully_fold (t, false, NULL);
12579 parens.skip_until_found_close (parser);
12580
12581 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
12582 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
12583 {
12584 if (if_modifier != ERROR_MARK
12585 && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
12586 {
12587 const char *p = NULL;
12588 switch (if_modifier)
12589 {
12590 case VOID_CST: p = "cancel"; break;
12591 case OMP_PARALLEL: p = "parallel"; break;
12592 case OMP_SIMD: p = "simd"; break;
12593 case OMP_TASK: p = "task"; break;
12594 case OMP_TASKLOOP: p = "taskloop"; break;
12595 case OMP_TARGET_DATA: p = "target data"; break;
12596 case OMP_TARGET: p = "target"; break;
12597 case OMP_TARGET_UPDATE: p = "target update"; break;
12598 case OMP_TARGET_ENTER_DATA: p = "enter data"; break;
12599 case OMP_TARGET_EXIT_DATA: p = "exit data"; break;
12600 default: gcc_unreachable ();
12601 }
12602 error_at (location, "too many %<if%> clauses with %qs modifier",
12603 p);
12604 return list;
12605 }
12606 else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
12607 {
12608 if (!is_omp)
12609 error_at (location, "too many %<if%> clauses");
12610 else
12611 error_at (location, "too many %<if%> clauses without modifier");
12612 return list;
12613 }
12614 else if (if_modifier == ERROR_MARK
12615 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
12616 {
12617 error_at (location, "if any %<if%> clause has modifier, then all "
12618 "%<if%> clauses have to use modifier");
12619 return list;
12620 }
12621 }
12622
12623 c = build_omp_clause (location, OMP_CLAUSE_IF);
12624 OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
12625 OMP_CLAUSE_IF_EXPR (c) = t;
12626 OMP_CLAUSE_CHAIN (c) = list;
12627 return c;
12628 }
12629
12630 /* OpenMP 2.5:
12631 lastprivate ( variable-list )
12632
12633 OpenMP 5.0:
12634 lastprivate ( [ lastprivate-modifier : ] variable-list ) */
12635
12636 static tree
12637 c_parser_omp_clause_lastprivate (c_parser *parser, tree list)
12638 {
12639 /* The clauses location. */
12640 location_t loc = c_parser_peek_token (parser)->location;
12641
12642 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12643 {
12644 bool conditional = false;
12645 if (c_parser_next_token_is (parser, CPP_NAME)
12646 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
12647 {
12648 const char *p
12649 = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12650 if (strcmp (p, "conditional") == 0)
12651 {
12652 conditional = true;
12653 c_parser_consume_token (parser);
12654 c_parser_consume_token (parser);
12655 }
12656 }
12657 tree nlist = c_parser_omp_variable_list (parser, loc,
12658 OMP_CLAUSE_LASTPRIVATE, list);
12659 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12660 if (conditional)
12661 for (tree c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
12662 OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c) = 1;
12663 return nlist;
12664 }
12665 return list;
12666 }
12667
12668 /* OpenMP 3.1:
12669 mergeable */
12670
12671 static tree
12672 c_parser_omp_clause_mergeable (c_parser *parser ATTRIBUTE_UNUSED, tree list)
12673 {
12674 tree c;
12675
12676 /* FIXME: Should we allow duplicates? */
12677 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable");
12678
12679 c = build_omp_clause (c_parser_peek_token (parser)->location,
12680 OMP_CLAUSE_MERGEABLE);
12681 OMP_CLAUSE_CHAIN (c) = list;
12682
12683 return c;
12684 }
12685
12686 /* OpenMP 2.5:
12687 nowait */
12688
12689 static tree
12690 c_parser_omp_clause_nowait (c_parser *parser ATTRIBUTE_UNUSED, tree list)
12691 {
12692 tree c;
12693 location_t loc = c_parser_peek_token (parser)->location;
12694
12695 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
12696
12697 c = build_omp_clause (loc, OMP_CLAUSE_NOWAIT);
12698 OMP_CLAUSE_CHAIN (c) = list;
12699 return c;
12700 }
12701
12702 /* OpenMP 2.5:
12703 num_threads ( expression ) */
12704
12705 static tree
12706 c_parser_omp_clause_num_threads (c_parser *parser, tree list)
12707 {
12708 location_t num_threads_loc = c_parser_peek_token (parser)->location;
12709 matching_parens parens;
12710 if (parens.require_open (parser))
12711 {
12712 location_t expr_loc = c_parser_peek_token (parser)->location;
12713 c_expr expr = c_parser_expr_no_commas (parser, NULL);
12714 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
12715 tree c, t = expr.value;
12716 t = c_fully_fold (t, false, NULL);
12717
12718 parens.skip_until_found_close (parser);
12719
12720 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
12721 {
12722 c_parser_error (parser, "expected integer expression");
12723 return list;
12724 }
12725
12726 /* Attempt to statically determine when the number isn't positive. */
12727 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
12728 build_int_cst (TREE_TYPE (t), 0));
12729 protected_set_expr_location (c, expr_loc);
12730 if (c == boolean_true_node)
12731 {
12732 warning_at (expr_loc, 0,
12733 "%<num_threads%> value must be positive");
12734 t = integer_one_node;
12735 }
12736
12737 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
12738
12739 c = build_omp_clause (num_threads_loc, OMP_CLAUSE_NUM_THREADS);
12740 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
12741 OMP_CLAUSE_CHAIN (c) = list;
12742 list = c;
12743 }
12744
12745 return list;
12746 }
12747
12748 /* OpenMP 4.5:
12749 num_tasks ( expression ) */
12750
12751 static tree
12752 c_parser_omp_clause_num_tasks (c_parser *parser, tree list)
12753 {
12754 location_t num_tasks_loc = c_parser_peek_token (parser)->location;
12755 matching_parens parens;
12756 if (parens.require_open (parser))
12757 {
12758 location_t expr_loc = c_parser_peek_token (parser)->location;
12759 c_expr expr = c_parser_expr_no_commas (parser, NULL);
12760 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
12761 tree c, t = expr.value;
12762 t = c_fully_fold (t, false, NULL);
12763
12764 parens.skip_until_found_close (parser);
12765
12766 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
12767 {
12768 c_parser_error (parser, "expected integer expression");
12769 return list;
12770 }
12771
12772 /* Attempt to statically determine when the number isn't positive. */
12773 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
12774 build_int_cst (TREE_TYPE (t), 0));
12775 if (CAN_HAVE_LOCATION_P (c))
12776 SET_EXPR_LOCATION (c, expr_loc);
12777 if (c == boolean_true_node)
12778 {
12779 warning_at (expr_loc, 0, "%<num_tasks%> value must be positive");
12780 t = integer_one_node;
12781 }
12782
12783 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS, "num_tasks");
12784
12785 c = build_omp_clause (num_tasks_loc, OMP_CLAUSE_NUM_TASKS);
12786 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
12787 OMP_CLAUSE_CHAIN (c) = list;
12788 list = c;
12789 }
12790
12791 return list;
12792 }
12793
12794 /* OpenMP 4.5:
12795 grainsize ( expression ) */
12796
12797 static tree
12798 c_parser_omp_clause_grainsize (c_parser *parser, tree list)
12799 {
12800 location_t grainsize_loc = c_parser_peek_token (parser)->location;
12801 matching_parens parens;
12802 if (parens.require_open (parser))
12803 {
12804 location_t expr_loc = c_parser_peek_token (parser)->location;
12805 c_expr expr = c_parser_expr_no_commas (parser, NULL);
12806 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
12807 tree c, t = expr.value;
12808 t = c_fully_fold (t, false, NULL);
12809
12810 parens.skip_until_found_close (parser);
12811
12812 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
12813 {
12814 c_parser_error (parser, "expected integer expression");
12815 return list;
12816 }
12817
12818 /* Attempt to statically determine when the number isn't positive. */
12819 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
12820 build_int_cst (TREE_TYPE (t), 0));
12821 if (CAN_HAVE_LOCATION_P (c))
12822 SET_EXPR_LOCATION (c, expr_loc);
12823 if (c == boolean_true_node)
12824 {
12825 warning_at (expr_loc, 0, "%<grainsize%> value must be positive");
12826 t = integer_one_node;
12827 }
12828
12829 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE, "grainsize");
12830
12831 c = build_omp_clause (grainsize_loc, OMP_CLAUSE_GRAINSIZE);
12832 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
12833 OMP_CLAUSE_CHAIN (c) = list;
12834 list = c;
12835 }
12836
12837 return list;
12838 }
12839
12840 /* OpenMP 4.5:
12841 priority ( expression ) */
12842
12843 static tree
12844 c_parser_omp_clause_priority (c_parser *parser, tree list)
12845 {
12846 location_t priority_loc = c_parser_peek_token (parser)->location;
12847 matching_parens parens;
12848 if (parens.require_open (parser))
12849 {
12850 location_t expr_loc = c_parser_peek_token (parser)->location;
12851 c_expr expr = c_parser_expr_no_commas (parser, NULL);
12852 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
12853 tree c, t = expr.value;
12854 t = c_fully_fold (t, false, NULL);
12855
12856 parens.skip_until_found_close (parser);
12857
12858 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
12859 {
12860 c_parser_error (parser, "expected integer expression");
12861 return list;
12862 }
12863
12864 /* Attempt to statically determine when the number isn't
12865 non-negative. */
12866 c = fold_build2_loc (expr_loc, LT_EXPR, boolean_type_node, t,
12867 build_int_cst (TREE_TYPE (t), 0));
12868 if (CAN_HAVE_LOCATION_P (c))
12869 SET_EXPR_LOCATION (c, expr_loc);
12870 if (c == boolean_true_node)
12871 {
12872 warning_at (expr_loc, 0, "%<priority%> value must be non-negative");
12873 t = integer_one_node;
12874 }
12875
12876 check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY, "priority");
12877
12878 c = build_omp_clause (priority_loc, OMP_CLAUSE_PRIORITY);
12879 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
12880 OMP_CLAUSE_CHAIN (c) = list;
12881 list = c;
12882 }
12883
12884 return list;
12885 }
12886
12887 /* OpenMP 4.5:
12888 hint ( expression ) */
12889
12890 static tree
12891 c_parser_omp_clause_hint (c_parser *parser, tree list)
12892 {
12893 location_t hint_loc = c_parser_peek_token (parser)->location;
12894 matching_parens parens;
12895 if (parens.require_open (parser))
12896 {
12897 location_t expr_loc = c_parser_peek_token (parser)->location;
12898 c_expr expr = c_parser_expr_no_commas (parser, NULL);
12899 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
12900 tree c, t = expr.value;
12901 t = c_fully_fold (t, false, NULL);
12902
12903 parens.skip_until_found_close (parser);
12904
12905 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
12906 || TREE_CODE (t) != INTEGER_CST)
12907 {
12908 c_parser_error (parser, "expected constant integer expression");
12909 return list;
12910 }
12911
12912 check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint");
12913
12914 c = build_omp_clause (hint_loc, OMP_CLAUSE_HINT);
12915 OMP_CLAUSE_HINT_EXPR (c) = t;
12916 OMP_CLAUSE_CHAIN (c) = list;
12917 list = c;
12918 }
12919
12920 return list;
12921 }
12922
12923 /* OpenMP 4.5:
12924 defaultmap ( tofrom : scalar )
12925
12926 OpenMP 5.0:
12927 defaultmap ( implicit-behavior [ : variable-category ] ) */
12928
12929 static tree
12930 c_parser_omp_clause_defaultmap (c_parser *parser, tree list)
12931 {
12932 location_t loc = c_parser_peek_token (parser)->location;
12933 tree c;
12934 const char *p;
12935 enum omp_clause_defaultmap_kind behavior = OMP_CLAUSE_DEFAULTMAP_DEFAULT;
12936 enum omp_clause_defaultmap_kind category
12937 = OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED;
12938
12939 matching_parens parens;
12940 if (!parens.require_open (parser))
12941 return list;
12942 if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
12943 p = "default";
12944 else if (!c_parser_next_token_is (parser, CPP_NAME))
12945 {
12946 invalid_behavior:
12947 c_parser_error (parser, "expected %<alloc%>, %<to%>, %<from%>, "
12948 "%<tofrom%>, %<firstprivate%>, %<none%> "
12949 "or %<default%>");
12950 goto out_err;
12951 }
12952 else
12953 p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12954
12955 switch (p[0])
12956 {
12957 case 'a':
12958 if (strcmp ("alloc", p) == 0)
12959 behavior = OMP_CLAUSE_DEFAULTMAP_ALLOC;
12960 else
12961 goto invalid_behavior;
12962 break;
12963
12964 case 'd':
12965 if (strcmp ("default", p) == 0)
12966 behavior = OMP_CLAUSE_DEFAULTMAP_DEFAULT;
12967 else
12968 goto invalid_behavior;
12969 break;
12970
12971 case 'f':
12972 if (strcmp ("firstprivate", p) == 0)
12973 behavior = OMP_CLAUSE_DEFAULTMAP_FIRSTPRIVATE;
12974 else if (strcmp ("from", p) == 0)
12975 behavior = OMP_CLAUSE_DEFAULTMAP_FROM;
12976 else
12977 goto invalid_behavior;
12978 break;
12979
12980 case 'n':
12981 if (strcmp ("none", p) == 0)
12982 behavior = OMP_CLAUSE_DEFAULTMAP_NONE;
12983 else
12984 goto invalid_behavior;
12985 break;
12986
12987 case 't':
12988 if (strcmp ("tofrom", p) == 0)
12989 behavior = OMP_CLAUSE_DEFAULTMAP_TOFROM;
12990 else if (strcmp ("to", p) == 0)
12991 behavior = OMP_CLAUSE_DEFAULTMAP_TO;
12992 else
12993 goto invalid_behavior;
12994 break;
12995
12996 default:
12997 goto invalid_behavior;
12998 }
12999 c_parser_consume_token (parser);
13000
13001 if (!c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
13002 {
13003 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
13004 goto out_err;
13005 if (!c_parser_next_token_is (parser, CPP_NAME))
13006 {
13007 invalid_category:
13008 c_parser_error (parser, "expected %<scalar%>, %<aggregate%> or "
13009 "%<pointer%>");
13010 goto out_err;
13011 }
13012 p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13013 switch (p[0])
13014 {
13015 case 'a':
13016 if (strcmp ("aggregate", p) == 0)
13017 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE;
13018 else
13019 goto invalid_category;
13020 break;
13021
13022 case 'p':
13023 if (strcmp ("pointer", p) == 0)
13024 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER;
13025 else
13026 goto invalid_category;
13027 break;
13028
13029 case 's':
13030 if (strcmp ("scalar", p) == 0)
13031 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR;
13032 else
13033 goto invalid_category;
13034 break;
13035
13036 default:
13037 goto invalid_category;
13038 }
13039
13040 c_parser_consume_token (parser);
13041 }
13042 parens.skip_until_found_close (parser);
13043
13044 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
13045 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEFAULTMAP
13046 && (category == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
13047 || OMP_CLAUSE_DEFAULTMAP_CATEGORY (c) == category
13048 || (OMP_CLAUSE_DEFAULTMAP_CATEGORY (c)
13049 == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED)))
13050 {
13051 enum omp_clause_defaultmap_kind cat = category;
13052 location_t loc = OMP_CLAUSE_LOCATION (c);
13053 if (cat == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED)
13054 cat = OMP_CLAUSE_DEFAULTMAP_CATEGORY (c);
13055 p = NULL;
13056 switch (cat)
13057 {
13058 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED:
13059 p = NULL;
13060 break;
13061 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE:
13062 p = "aggregate";
13063 break;
13064 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER:
13065 p = "pointer";
13066 break;
13067 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR:
13068 p = "scalar";
13069 break;
13070 default:
13071 gcc_unreachable ();
13072 }
13073 if (p)
13074 error_at (loc, "too many %<defaultmap%> clauses with %qs category",
13075 p);
13076 else
13077 error_at (loc, "too many %<defaultmap%> clauses with unspecified "
13078 "category");
13079 break;
13080 }
13081
13082 c = build_omp_clause (loc, OMP_CLAUSE_DEFAULTMAP);
13083 OMP_CLAUSE_DEFAULTMAP_SET_KIND (c, behavior, category);
13084 OMP_CLAUSE_CHAIN (c) = list;
13085 return c;
13086
13087 out_err:
13088 parens.skip_until_found_close (parser);
13089 return list;
13090 }
13091
13092 /* OpenACC 2.0:
13093 use_device ( variable-list )
13094
13095 OpenMP 4.5:
13096 use_device_ptr ( variable-list ) */
13097
13098 static tree
13099 c_parser_omp_clause_use_device_ptr (c_parser *parser, tree list)
13100 {
13101 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_USE_DEVICE_PTR,
13102 list);
13103 }
13104
13105 /* OpenMP 4.5:
13106 is_device_ptr ( variable-list ) */
13107
13108 static tree
13109 c_parser_omp_clause_is_device_ptr (c_parser *parser, tree list)
13110 {
13111 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_IS_DEVICE_PTR, list);
13112 }
13113
13114 /* OpenACC:
13115 num_gangs ( expression )
13116 num_workers ( expression )
13117 vector_length ( expression ) */
13118
13119 static tree
13120 c_parser_oacc_single_int_clause (c_parser *parser, omp_clause_code code,
13121 tree list)
13122 {
13123 location_t loc = c_parser_peek_token (parser)->location;
13124
13125 matching_parens parens;
13126 if (!parens.require_open (parser))
13127 return list;
13128
13129 location_t expr_loc = c_parser_peek_token (parser)->location;
13130 c_expr expr = c_parser_expression (parser);
13131 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
13132 tree c, t = expr.value;
13133 t = c_fully_fold (t, false, NULL);
13134
13135 parens.skip_until_found_close (parser);
13136
13137 if (t == error_mark_node)
13138 return list;
13139 else if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
13140 {
13141 error_at (expr_loc, "%qs expression must be integral",
13142 omp_clause_code_name[code]);
13143 return list;
13144 }
13145
13146 /* Attempt to statically determine when the number isn't positive. */
13147 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
13148 build_int_cst (TREE_TYPE (t), 0));
13149 protected_set_expr_location (c, expr_loc);
13150 if (c == boolean_true_node)
13151 {
13152 warning_at (expr_loc, 0,
13153 "%qs value must be positive",
13154 omp_clause_code_name[code]);
13155 t = integer_one_node;
13156 }
13157
13158 check_no_duplicate_clause (list, code, omp_clause_code_name[code]);
13159
13160 c = build_omp_clause (loc, code);
13161 OMP_CLAUSE_OPERAND (c, 0) = t;
13162 OMP_CLAUSE_CHAIN (c) = list;
13163 return c;
13164 }
13165
13166 /* OpenACC:
13167
13168 gang [( gang-arg-list )]
13169 worker [( [num:] int-expr )]
13170 vector [( [length:] int-expr )]
13171
13172 where gang-arg is one of:
13173
13174 [num:] int-expr
13175 static: size-expr
13176
13177 and size-expr may be:
13178
13179 *
13180 int-expr
13181 */
13182
13183 static tree
13184 c_parser_oacc_shape_clause (c_parser *parser, location_t loc,
13185 omp_clause_code kind,
13186 const char *str, tree list)
13187 {
13188 const char *id = "num";
13189 tree ops[2] = { NULL_TREE, NULL_TREE }, c;
13190
13191 if (kind == OMP_CLAUSE_VECTOR)
13192 id = "length";
13193
13194 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
13195 {
13196 c_parser_consume_token (parser);
13197
13198 do
13199 {
13200 c_token *next = c_parser_peek_token (parser);
13201 int idx = 0;
13202
13203 /* Gang static argument. */
13204 if (kind == OMP_CLAUSE_GANG
13205 && c_parser_next_token_is_keyword (parser, RID_STATIC))
13206 {
13207 c_parser_consume_token (parser);
13208
13209 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
13210 goto cleanup_error;
13211
13212 idx = 1;
13213 if (ops[idx] != NULL_TREE)
13214 {
13215 c_parser_error (parser, "too many %<static%> arguments");
13216 goto cleanup_error;
13217 }
13218
13219 /* Check for the '*' argument. */
13220 if (c_parser_next_token_is (parser, CPP_MULT)
13221 && (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
13222 || c_parser_peek_2nd_token (parser)->type
13223 == CPP_CLOSE_PAREN))
13224 {
13225 c_parser_consume_token (parser);
13226 ops[idx] = integer_minus_one_node;
13227
13228 if (c_parser_next_token_is (parser, CPP_COMMA))
13229 {
13230 c_parser_consume_token (parser);
13231 continue;
13232 }
13233 else
13234 break;
13235 }
13236 }
13237 /* Worker num: argument and vector length: arguments. */
13238 else if (c_parser_next_token_is (parser, CPP_NAME)
13239 && strcmp (id, IDENTIFIER_POINTER (next->value)) == 0
13240 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
13241 {
13242 c_parser_consume_token (parser); /* id */
13243 c_parser_consume_token (parser); /* ':' */
13244 }
13245
13246 /* Now collect the actual argument. */
13247 if (ops[idx] != NULL_TREE)
13248 {
13249 c_parser_error (parser, "unexpected argument");
13250 goto cleanup_error;
13251 }
13252
13253 location_t expr_loc = c_parser_peek_token (parser)->location;
13254 c_expr cexpr = c_parser_expr_no_commas (parser, NULL);
13255 cexpr = convert_lvalue_to_rvalue (expr_loc, cexpr, false, true);
13256 tree expr = cexpr.value;
13257 if (expr == error_mark_node)
13258 goto cleanup_error;
13259
13260 expr = c_fully_fold (expr, false, NULL);
13261
13262 /* Attempt to statically determine when the number isn't a
13263 positive integer. */
13264
13265 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr)))
13266 {
13267 c_parser_error (parser, "expected integer expression");
13268 return list;
13269 }
13270
13271 tree c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, expr,
13272 build_int_cst (TREE_TYPE (expr), 0));
13273 if (c == boolean_true_node)
13274 {
13275 warning_at (loc, 0,
13276 "%qs value must be positive", str);
13277 expr = integer_one_node;
13278 }
13279
13280 ops[idx] = expr;
13281
13282 if (kind == OMP_CLAUSE_GANG
13283 && c_parser_next_token_is (parser, CPP_COMMA))
13284 {
13285 c_parser_consume_token (parser);
13286 continue;
13287 }
13288 break;
13289 }
13290 while (1);
13291
13292 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
13293 goto cleanup_error;
13294 }
13295
13296 check_no_duplicate_clause (list, kind, str);
13297
13298 c = build_omp_clause (loc, kind);
13299
13300 if (ops[1])
13301 OMP_CLAUSE_OPERAND (c, 1) = ops[1];
13302
13303 OMP_CLAUSE_OPERAND (c, 0) = ops[0];
13304 OMP_CLAUSE_CHAIN (c) = list;
13305
13306 return c;
13307
13308 cleanup_error:
13309 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
13310 return list;
13311 }
13312
13313 /* OpenACC 2.5:
13314 auto
13315 finalize
13316 independent
13317 nohost
13318 seq */
13319
13320 static tree
13321 c_parser_oacc_simple_clause (location_t loc, enum omp_clause_code code,
13322 tree list)
13323 {
13324 check_no_duplicate_clause (list, code, omp_clause_code_name[code]);
13325
13326 tree c = build_omp_clause (loc, code);
13327 OMP_CLAUSE_CHAIN (c) = list;
13328
13329 return c;
13330 }
13331
13332 /* OpenACC:
13333 async [( int-expr )] */
13334
13335 static tree
13336 c_parser_oacc_clause_async (c_parser *parser, tree list)
13337 {
13338 tree c, t;
13339 location_t loc = c_parser_peek_token (parser)->location;
13340
13341 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
13342
13343 if (c_parser_peek_token (parser)->type == CPP_OPEN_PAREN)
13344 {
13345 c_parser_consume_token (parser);
13346
13347 t = c_parser_expression (parser).value;
13348 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
13349 c_parser_error (parser, "expected integer expression");
13350 else if (t == error_mark_node
13351 || !c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
13352 return list;
13353 }
13354 else
13355 t = c_fully_fold (t, false, NULL);
13356
13357 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async");
13358
13359 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
13360 OMP_CLAUSE_ASYNC_EXPR (c) = t;
13361 OMP_CLAUSE_CHAIN (c) = list;
13362 list = c;
13363
13364 return list;
13365 }
13366
13367 /* OpenACC 2.0:
13368 tile ( size-expr-list ) */
13369
13370 static tree
13371 c_parser_oacc_clause_tile (c_parser *parser, tree list)
13372 {
13373 tree c, expr = error_mark_node;
13374 location_t loc;
13375 tree tile = NULL_TREE;
13376
13377 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile");
13378 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse");
13379
13380 loc = c_parser_peek_token (parser)->location;
13381 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
13382 return list;
13383
13384 do
13385 {
13386 if (tile && !c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
13387 return list;
13388
13389 if (c_parser_next_token_is (parser, CPP_MULT)
13390 && (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
13391 || c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_PAREN))
13392 {
13393 c_parser_consume_token (parser);
13394 expr = integer_zero_node;
13395 }
13396 else
13397 {
13398 location_t expr_loc = c_parser_peek_token (parser)->location;
13399 c_expr cexpr = c_parser_expr_no_commas (parser, NULL);
13400 cexpr = convert_lvalue_to_rvalue (expr_loc, cexpr, false, true);
13401 expr = cexpr.value;
13402
13403 if (expr == error_mark_node)
13404 {
13405 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
13406 "expected %<)%>");
13407 return list;
13408 }
13409
13410 expr = c_fully_fold (expr, false, NULL);
13411
13412 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr))
13413 || !tree_fits_shwi_p (expr)
13414 || tree_to_shwi (expr) <= 0)
13415 {
13416 error_at (expr_loc, "%<tile%> argument needs positive"
13417 " integral constant");
13418 expr = integer_zero_node;
13419 }
13420 }
13421
13422 tile = tree_cons (NULL_TREE, expr, tile);
13423 }
13424 while (c_parser_next_token_is_not (parser, CPP_CLOSE_PAREN));
13425
13426 /* Consume the trailing ')'. */
13427 c_parser_consume_token (parser);
13428
13429 c = build_omp_clause (loc, OMP_CLAUSE_TILE);
13430 tile = nreverse (tile);
13431 OMP_CLAUSE_TILE_LIST (c) = tile;
13432 OMP_CLAUSE_CHAIN (c) = list;
13433 return c;
13434 }
13435
13436 /* OpenACC:
13437 wait [( int-expr-list )] */
13438
13439 static tree
13440 c_parser_oacc_clause_wait (c_parser *parser, tree list)
13441 {
13442 location_t clause_loc = c_parser_peek_token (parser)->location;
13443
13444 if (c_parser_peek_token (parser)->type == CPP_OPEN_PAREN)
13445 list = c_parser_oacc_wait_list (parser, clause_loc, list);
13446 else
13447 {
13448 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
13449
13450 OMP_CLAUSE_DECL (c) = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
13451 OMP_CLAUSE_CHAIN (c) = list;
13452 list = c;
13453 }
13454
13455 return list;
13456 }
13457
13458 /* OpenMP 2.5:
13459 ordered
13460
13461 OpenMP 4.5:
13462 ordered ( constant-expression ) */
13463
13464 static tree
13465 c_parser_omp_clause_ordered (c_parser *parser, tree list)
13466 {
13467 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
13468
13469 tree c, num = NULL_TREE;
13470 HOST_WIDE_INT n;
13471 location_t loc = c_parser_peek_token (parser)->location;
13472 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
13473 {
13474 matching_parens parens;
13475 parens.consume_open (parser);
13476 num = c_parser_expr_no_commas (parser, NULL).value;
13477 parens.skip_until_found_close (parser);
13478 }
13479 if (num == error_mark_node)
13480 return list;
13481 if (num)
13482 {
13483 mark_exp_read (num);
13484 num = c_fully_fold (num, false, NULL);
13485 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
13486 || !tree_fits_shwi_p (num)
13487 || (n = tree_to_shwi (num)) <= 0
13488 || (int) n != n)
13489 {
13490 error_at (loc, "ordered argument needs positive "
13491 "constant integer expression");
13492 return list;
13493 }
13494 }
13495 c = build_omp_clause (loc, OMP_CLAUSE_ORDERED);
13496 OMP_CLAUSE_ORDERED_EXPR (c) = num;
13497 OMP_CLAUSE_CHAIN (c) = list;
13498 return c;
13499 }
13500
13501 /* OpenMP 2.5:
13502 private ( variable-list ) */
13503
13504 static tree
13505 c_parser_omp_clause_private (c_parser *parser, tree list)
13506 {
13507 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_PRIVATE, list);
13508 }
13509
13510 /* OpenMP 2.5:
13511 reduction ( reduction-operator : variable-list )
13512
13513 reduction-operator:
13514 One of: + * - & ^ | && ||
13515
13516 OpenMP 3.1:
13517
13518 reduction-operator:
13519 One of: + * - & ^ | && || max min
13520
13521 OpenMP 4.0:
13522
13523 reduction-operator:
13524 One of: + * - & ^ | && ||
13525 identifier
13526
13527 OpenMP 5.0:
13528 reduction ( reduction-modifier, reduction-operator : variable-list )
13529 in_reduction ( reduction-operator : variable-list )
13530 task_reduction ( reduction-operator : variable-list ) */
13531
13532 static tree
13533 c_parser_omp_clause_reduction (c_parser *parser, enum omp_clause_code kind,
13534 bool is_omp, tree list)
13535 {
13536 location_t clause_loc = c_parser_peek_token (parser)->location;
13537 matching_parens parens;
13538 if (parens.require_open (parser))
13539 {
13540 bool task = false;
13541 bool inscan = false;
13542 enum tree_code code = ERROR_MARK;
13543 tree reduc_id = NULL_TREE;
13544
13545 if (kind == OMP_CLAUSE_REDUCTION && is_omp)
13546 {
13547 if (c_parser_next_token_is_keyword (parser, RID_DEFAULT)
13548 && c_parser_peek_2nd_token (parser)->type == CPP_COMMA)
13549 {
13550 c_parser_consume_token (parser);
13551 c_parser_consume_token (parser);
13552 }
13553 else if (c_parser_next_token_is (parser, CPP_NAME)
13554 && c_parser_peek_2nd_token (parser)->type == CPP_COMMA)
13555 {
13556 const char *p
13557 = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13558 if (strcmp (p, "task") == 0)
13559 task = true;
13560 else if (strcmp (p, "inscan") == 0)
13561 {
13562 inscan = true;
13563 sorry ("%<inscan%> modifier on %<reduction%> clause "
13564 "not supported yet");
13565 }
13566 if (task || inscan)
13567 {
13568 c_parser_consume_token (parser);
13569 c_parser_consume_token (parser);
13570 }
13571 }
13572 }
13573
13574 switch (c_parser_peek_token (parser)->type)
13575 {
13576 case CPP_PLUS:
13577 code = PLUS_EXPR;
13578 break;
13579 case CPP_MULT:
13580 code = MULT_EXPR;
13581 break;
13582 case CPP_MINUS:
13583 code = MINUS_EXPR;
13584 break;
13585 case CPP_AND:
13586 code = BIT_AND_EXPR;
13587 break;
13588 case CPP_XOR:
13589 code = BIT_XOR_EXPR;
13590 break;
13591 case CPP_OR:
13592 code = BIT_IOR_EXPR;
13593 break;
13594 case CPP_AND_AND:
13595 code = TRUTH_ANDIF_EXPR;
13596 break;
13597 case CPP_OR_OR:
13598 code = TRUTH_ORIF_EXPR;
13599 break;
13600 case CPP_NAME:
13601 {
13602 const char *p
13603 = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13604 if (strcmp (p, "min") == 0)
13605 {
13606 code = MIN_EXPR;
13607 break;
13608 }
13609 if (strcmp (p, "max") == 0)
13610 {
13611 code = MAX_EXPR;
13612 break;
13613 }
13614 reduc_id = c_parser_peek_token (parser)->value;
13615 break;
13616 }
13617 default:
13618 c_parser_error (parser,
13619 "expected %<+%>, %<*%>, %<-%>, %<&%>, "
13620 "%<^%>, %<|%>, %<&&%>, %<||%> or identifier");
13621 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
13622 return list;
13623 }
13624 c_parser_consume_token (parser);
13625 reduc_id = c_omp_reduction_id (code, reduc_id);
13626 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
13627 {
13628 tree nl, c;
13629
13630 nl = c_parser_omp_variable_list (parser, clause_loc, kind, list);
13631 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
13632 {
13633 tree d = OMP_CLAUSE_DECL (c), type;
13634 if (TREE_CODE (d) != TREE_LIST)
13635 type = TREE_TYPE (d);
13636 else
13637 {
13638 int cnt = 0;
13639 tree t;
13640 for (t = d; TREE_CODE (t) == TREE_LIST; t = TREE_CHAIN (t))
13641 cnt++;
13642 type = TREE_TYPE (t);
13643 while (cnt > 0)
13644 {
13645 if (TREE_CODE (type) != POINTER_TYPE
13646 && TREE_CODE (type) != ARRAY_TYPE)
13647 break;
13648 type = TREE_TYPE (type);
13649 cnt--;
13650 }
13651 }
13652 while (TREE_CODE (type) == ARRAY_TYPE)
13653 type = TREE_TYPE (type);
13654 OMP_CLAUSE_REDUCTION_CODE (c) = code;
13655 if (task)
13656 OMP_CLAUSE_REDUCTION_TASK (c) = 1;
13657 else if (inscan)
13658 OMP_CLAUSE_REDUCTION_INSCAN (c) = 1;
13659 if (code == ERROR_MARK
13660 || !(INTEGRAL_TYPE_P (type)
13661 || TREE_CODE (type) == REAL_TYPE
13662 || TREE_CODE (type) == COMPLEX_TYPE))
13663 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c)
13664 = c_omp_reduction_lookup (reduc_id,
13665 TYPE_MAIN_VARIANT (type));
13666 }
13667
13668 list = nl;
13669 }
13670 parens.skip_until_found_close (parser);
13671 }
13672 return list;
13673 }
13674
13675 /* OpenMP 2.5:
13676 schedule ( schedule-kind )
13677 schedule ( schedule-kind , expression )
13678
13679 schedule-kind:
13680 static | dynamic | guided | runtime | auto
13681
13682 OpenMP 4.5:
13683 schedule ( schedule-modifier : schedule-kind )
13684 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
13685
13686 schedule-modifier:
13687 simd
13688 monotonic
13689 nonmonotonic */
13690
13691 static tree
13692 c_parser_omp_clause_schedule (c_parser *parser, tree list)
13693 {
13694 tree c, t;
13695 location_t loc = c_parser_peek_token (parser)->location;
13696 int modifiers = 0, nmodifiers = 0;
13697
13698 matching_parens parens;
13699 if (!parens.require_open (parser))
13700 return list;
13701
13702 c = build_omp_clause (loc, OMP_CLAUSE_SCHEDULE);
13703
13704 while (c_parser_next_token_is (parser, CPP_NAME))
13705 {
13706 tree kind = c_parser_peek_token (parser)->value;
13707 const char *p = IDENTIFIER_POINTER (kind);
13708 if (strcmp ("simd", p) == 0)
13709 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
13710 else if (strcmp ("monotonic", p) == 0)
13711 modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
13712 else if (strcmp ("nonmonotonic", p) == 0)
13713 modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
13714 else
13715 break;
13716 c_parser_consume_token (parser);
13717 if (nmodifiers++ == 0
13718 && c_parser_next_token_is (parser, CPP_COMMA))
13719 c_parser_consume_token (parser);
13720 else
13721 {
13722 c_parser_require (parser, CPP_COLON, "expected %<:%>");
13723 break;
13724 }
13725 }
13726
13727 if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
13728 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
13729 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
13730 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
13731 {
13732 error_at (loc, "both %<monotonic%> and %<nonmonotonic%> modifiers "
13733 "specified");
13734 modifiers = 0;
13735 }
13736
13737 if (c_parser_next_token_is (parser, CPP_NAME))
13738 {
13739 tree kind = c_parser_peek_token (parser)->value;
13740 const char *p = IDENTIFIER_POINTER (kind);
13741
13742 switch (p[0])
13743 {
13744 case 'd':
13745 if (strcmp ("dynamic", p) != 0)
13746 goto invalid_kind;
13747 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
13748 break;
13749
13750 case 'g':
13751 if (strcmp ("guided", p) != 0)
13752 goto invalid_kind;
13753 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
13754 break;
13755
13756 case 'r':
13757 if (strcmp ("runtime", p) != 0)
13758 goto invalid_kind;
13759 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
13760 break;
13761
13762 default:
13763 goto invalid_kind;
13764 }
13765 }
13766 else if (c_parser_next_token_is_keyword (parser, RID_STATIC))
13767 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
13768 else if (c_parser_next_token_is_keyword (parser, RID_AUTO))
13769 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
13770 else
13771 goto invalid_kind;
13772
13773 c_parser_consume_token (parser);
13774 if (c_parser_next_token_is (parser, CPP_COMMA))
13775 {
13776 location_t here;
13777 c_parser_consume_token (parser);
13778
13779 here = c_parser_peek_token (parser)->location;
13780 c_expr expr = c_parser_expr_no_commas (parser, NULL);
13781 expr = convert_lvalue_to_rvalue (here, expr, false, true);
13782 t = expr.value;
13783 t = c_fully_fold (t, false, NULL);
13784
13785 if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
13786 error_at (here, "schedule %<runtime%> does not take "
13787 "a %<chunk_size%> parameter");
13788 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
13789 error_at (here,
13790 "schedule %<auto%> does not take "
13791 "a %<chunk_size%> parameter");
13792 else if (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE)
13793 {
13794 /* Attempt to statically determine when the number isn't
13795 positive. */
13796 tree s = fold_build2_loc (loc, LE_EXPR, boolean_type_node, t,
13797 build_int_cst (TREE_TYPE (t), 0));
13798 protected_set_expr_location (s, loc);
13799 if (s == boolean_true_node)
13800 {
13801 warning_at (loc, 0,
13802 "chunk size value must be positive");
13803 t = integer_one_node;
13804 }
13805 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
13806 }
13807 else
13808 c_parser_error (parser, "expected integer expression");
13809
13810 parens.skip_until_found_close (parser);
13811 }
13812 else
13813 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
13814 "expected %<,%> or %<)%>");
13815
13816 OMP_CLAUSE_SCHEDULE_KIND (c)
13817 = (enum omp_clause_schedule_kind)
13818 (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
13819
13820 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
13821 OMP_CLAUSE_CHAIN (c) = list;
13822 return c;
13823
13824 invalid_kind:
13825 c_parser_error (parser, "invalid schedule kind");
13826 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
13827 return list;
13828 }
13829
13830 /* OpenMP 2.5:
13831 shared ( variable-list ) */
13832
13833 static tree
13834 c_parser_omp_clause_shared (c_parser *parser, tree list)
13835 {
13836 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_SHARED, list);
13837 }
13838
13839 /* OpenMP 3.0:
13840 untied */
13841
13842 static tree
13843 c_parser_omp_clause_untied (c_parser *parser ATTRIBUTE_UNUSED, tree list)
13844 {
13845 tree c;
13846
13847 /* FIXME: Should we allow duplicates? */
13848 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied");
13849
13850 c = build_omp_clause (c_parser_peek_token (parser)->location,
13851 OMP_CLAUSE_UNTIED);
13852 OMP_CLAUSE_CHAIN (c) = list;
13853
13854 return c;
13855 }
13856
13857 /* OpenMP 4.0:
13858 inbranch
13859 notinbranch */
13860
13861 static tree
13862 c_parser_omp_clause_branch (c_parser *parser ATTRIBUTE_UNUSED,
13863 enum omp_clause_code code, tree list)
13864 {
13865 check_no_duplicate_clause (list, code, omp_clause_code_name[code]);
13866
13867 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
13868 OMP_CLAUSE_CHAIN (c) = list;
13869
13870 return c;
13871 }
13872
13873 /* OpenMP 4.0:
13874 parallel
13875 for
13876 sections
13877 taskgroup */
13878
13879 static tree
13880 c_parser_omp_clause_cancelkind (c_parser *parser ATTRIBUTE_UNUSED,
13881 enum omp_clause_code code, tree list)
13882 {
13883 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
13884 OMP_CLAUSE_CHAIN (c) = list;
13885
13886 return c;
13887 }
13888
13889 /* OpenMP 4.5:
13890 nogroup */
13891
13892 static tree
13893 c_parser_omp_clause_nogroup (c_parser *parser ATTRIBUTE_UNUSED, tree list)
13894 {
13895 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup");
13896 tree c = build_omp_clause (c_parser_peek_token (parser)->location,
13897 OMP_CLAUSE_NOGROUP);
13898 OMP_CLAUSE_CHAIN (c) = list;
13899 return c;
13900 }
13901
13902 /* OpenMP 4.5:
13903 simd
13904 threads */
13905
13906 static tree
13907 c_parser_omp_clause_orderedkind (c_parser *parser ATTRIBUTE_UNUSED,
13908 enum omp_clause_code code, tree list)
13909 {
13910 check_no_duplicate_clause (list, code, omp_clause_code_name[code]);
13911 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
13912 OMP_CLAUSE_CHAIN (c) = list;
13913 return c;
13914 }
13915
13916 /* OpenMP 4.0:
13917 num_teams ( expression ) */
13918
13919 static tree
13920 c_parser_omp_clause_num_teams (c_parser *parser, tree list)
13921 {
13922 location_t num_teams_loc = c_parser_peek_token (parser)->location;
13923 matching_parens parens;
13924 if (parens.require_open (parser))
13925 {
13926 location_t expr_loc = c_parser_peek_token (parser)->location;
13927 c_expr expr = c_parser_expr_no_commas (parser, NULL);
13928 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
13929 tree c, t = expr.value;
13930 t = c_fully_fold (t, false, NULL);
13931
13932 parens.skip_until_found_close (parser);
13933
13934 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
13935 {
13936 c_parser_error (parser, "expected integer expression");
13937 return list;
13938 }
13939
13940 /* Attempt to statically determine when the number isn't positive. */
13941 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
13942 build_int_cst (TREE_TYPE (t), 0));
13943 protected_set_expr_location (c, expr_loc);
13944 if (c == boolean_true_node)
13945 {
13946 warning_at (expr_loc, 0, "%<num_teams%> value must be positive");
13947 t = integer_one_node;
13948 }
13949
13950 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS, "num_teams");
13951
13952 c = build_omp_clause (num_teams_loc, OMP_CLAUSE_NUM_TEAMS);
13953 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
13954 OMP_CLAUSE_CHAIN (c) = list;
13955 list = c;
13956 }
13957
13958 return list;
13959 }
13960
13961 /* OpenMP 4.0:
13962 thread_limit ( expression ) */
13963
13964 static tree
13965 c_parser_omp_clause_thread_limit (c_parser *parser, tree list)
13966 {
13967 location_t num_thread_limit_loc = c_parser_peek_token (parser)->location;
13968 matching_parens parens;
13969 if (parens.require_open (parser))
13970 {
13971 location_t expr_loc = c_parser_peek_token (parser)->location;
13972 c_expr expr = c_parser_expr_no_commas (parser, NULL);
13973 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
13974 tree c, t = expr.value;
13975 t = c_fully_fold (t, false, NULL);
13976
13977 parens.skip_until_found_close (parser);
13978
13979 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
13980 {
13981 c_parser_error (parser, "expected integer expression");
13982 return list;
13983 }
13984
13985 /* Attempt to statically determine when the number isn't positive. */
13986 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
13987 build_int_cst (TREE_TYPE (t), 0));
13988 protected_set_expr_location (c, expr_loc);
13989 if (c == boolean_true_node)
13990 {
13991 warning_at (expr_loc, 0, "%<thread_limit%> value must be positive");
13992 t = integer_one_node;
13993 }
13994
13995 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
13996 "thread_limit");
13997
13998 c = build_omp_clause (num_thread_limit_loc, OMP_CLAUSE_THREAD_LIMIT);
13999 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
14000 OMP_CLAUSE_CHAIN (c) = list;
14001 list = c;
14002 }
14003
14004 return list;
14005 }
14006
14007 /* OpenMP 4.0:
14008 aligned ( variable-list )
14009 aligned ( variable-list : constant-expression ) */
14010
14011 static tree
14012 c_parser_omp_clause_aligned (c_parser *parser, tree list)
14013 {
14014 location_t clause_loc = c_parser_peek_token (parser)->location;
14015 tree nl, c;
14016
14017 matching_parens parens;
14018 if (!parens.require_open (parser))
14019 return list;
14020
14021 nl = c_parser_omp_variable_list (parser, clause_loc,
14022 OMP_CLAUSE_ALIGNED, list);
14023
14024 if (c_parser_next_token_is (parser, CPP_COLON))
14025 {
14026 c_parser_consume_token (parser);
14027 location_t expr_loc = c_parser_peek_token (parser)->location;
14028 c_expr expr = c_parser_expr_no_commas (parser, NULL);
14029 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
14030 tree alignment = expr.value;
14031 alignment = c_fully_fold (alignment, false, NULL);
14032 if (TREE_CODE (alignment) != INTEGER_CST
14033 || !INTEGRAL_TYPE_P (TREE_TYPE (alignment))
14034 || tree_int_cst_sgn (alignment) != 1)
14035 {
14036 error_at (clause_loc, "%<aligned%> clause alignment expression must "
14037 "be positive constant integer expression");
14038 alignment = NULL_TREE;
14039 }
14040
14041 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
14042 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
14043 }
14044
14045 parens.skip_until_found_close (parser);
14046 return nl;
14047 }
14048
14049 /* OpenMP 4.0:
14050 linear ( variable-list )
14051 linear ( variable-list : expression )
14052
14053 OpenMP 4.5:
14054 linear ( modifier ( variable-list ) )
14055 linear ( modifier ( variable-list ) : expression ) */
14056
14057 static tree
14058 c_parser_omp_clause_linear (c_parser *parser, tree list)
14059 {
14060 location_t clause_loc = c_parser_peek_token (parser)->location;
14061 tree nl, c, step;
14062 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
14063
14064 matching_parens parens;
14065 if (!parens.require_open (parser))
14066 return list;
14067
14068 if (c_parser_next_token_is (parser, CPP_NAME))
14069 {
14070 c_token *tok = c_parser_peek_token (parser);
14071 const char *p = IDENTIFIER_POINTER (tok->value);
14072 if (strcmp ("val", p) == 0)
14073 kind = OMP_CLAUSE_LINEAR_VAL;
14074 if (c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN)
14075 kind = OMP_CLAUSE_LINEAR_DEFAULT;
14076 if (kind != OMP_CLAUSE_LINEAR_DEFAULT)
14077 {
14078 c_parser_consume_token (parser);
14079 c_parser_consume_token (parser);
14080 }
14081 }
14082
14083 nl = c_parser_omp_variable_list (parser, clause_loc,
14084 OMP_CLAUSE_LINEAR, list);
14085
14086 if (kind != OMP_CLAUSE_LINEAR_DEFAULT)
14087 parens.skip_until_found_close (parser);
14088
14089 if (c_parser_next_token_is (parser, CPP_COLON))
14090 {
14091 c_parser_consume_token (parser);
14092 location_t expr_loc = c_parser_peek_token (parser)->location;
14093 c_expr expr = c_parser_expr_no_commas (parser, NULL);
14094 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
14095 step = expr.value;
14096 step = c_fully_fold (step, false, NULL);
14097 if (!INTEGRAL_TYPE_P (TREE_TYPE (step)))
14098 {
14099 error_at (clause_loc, "%<linear%> clause step expression must "
14100 "be integral");
14101 step = integer_one_node;
14102 }
14103
14104 }
14105 else
14106 step = integer_one_node;
14107
14108 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
14109 {
14110 OMP_CLAUSE_LINEAR_STEP (c) = step;
14111 OMP_CLAUSE_LINEAR_KIND (c) = kind;
14112 }
14113
14114 parens.skip_until_found_close (parser);
14115 return nl;
14116 }
14117
14118 /* OpenMP 5.0:
14119 nontemporal ( variable-list ) */
14120
14121 static tree
14122 c_parser_omp_clause_nontemporal (c_parser *parser, tree list)
14123 {
14124 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_NONTEMPORAL, list);
14125 }
14126
14127 /* OpenMP 4.0:
14128 safelen ( constant-expression ) */
14129
14130 static tree
14131 c_parser_omp_clause_safelen (c_parser *parser, tree list)
14132 {
14133 location_t clause_loc = c_parser_peek_token (parser)->location;
14134 tree c, t;
14135
14136 matching_parens parens;
14137 if (!parens.require_open (parser))
14138 return list;
14139
14140 location_t expr_loc = c_parser_peek_token (parser)->location;
14141 c_expr expr = c_parser_expr_no_commas (parser, NULL);
14142 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
14143 t = expr.value;
14144 t = c_fully_fold (t, false, NULL);
14145 if (TREE_CODE (t) != INTEGER_CST
14146 || !INTEGRAL_TYPE_P (TREE_TYPE (t))
14147 || tree_int_cst_sgn (t) != 1)
14148 {
14149 error_at (clause_loc, "%<safelen%> clause expression must "
14150 "be positive constant integer expression");
14151 t = NULL_TREE;
14152 }
14153
14154 parens.skip_until_found_close (parser);
14155 if (t == NULL_TREE || t == error_mark_node)
14156 return list;
14157
14158 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen");
14159
14160 c = build_omp_clause (clause_loc, OMP_CLAUSE_SAFELEN);
14161 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
14162 OMP_CLAUSE_CHAIN (c) = list;
14163 return c;
14164 }
14165
14166 /* OpenMP 4.0:
14167 simdlen ( constant-expression ) */
14168
14169 static tree
14170 c_parser_omp_clause_simdlen (c_parser *parser, tree list)
14171 {
14172 location_t clause_loc = c_parser_peek_token (parser)->location;
14173 tree c, t;
14174
14175 matching_parens parens;
14176 if (!parens.require_open (parser))
14177 return list;
14178
14179 location_t expr_loc = c_parser_peek_token (parser)->location;
14180 c_expr expr = c_parser_expr_no_commas (parser, NULL);
14181 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
14182 t = expr.value;
14183 t = c_fully_fold (t, false, NULL);
14184 if (TREE_CODE (t) != INTEGER_CST
14185 || !INTEGRAL_TYPE_P (TREE_TYPE (t))
14186 || tree_int_cst_sgn (t) != 1)
14187 {
14188 error_at (clause_loc, "%<simdlen%> clause expression must "
14189 "be positive constant integer expression");
14190 t = NULL_TREE;
14191 }
14192
14193 parens.skip_until_found_close (parser);
14194 if (t == NULL_TREE || t == error_mark_node)
14195 return list;
14196
14197 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen");
14198
14199 c = build_omp_clause (clause_loc, OMP_CLAUSE_SIMDLEN);
14200 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
14201 OMP_CLAUSE_CHAIN (c) = list;
14202 return c;
14203 }
14204
14205 /* OpenMP 4.5:
14206 vec:
14207 identifier [+/- integer]
14208 vec , identifier [+/- integer]
14209 */
14210
14211 static tree
14212 c_parser_omp_clause_depend_sink (c_parser *parser, location_t clause_loc,
14213 tree list)
14214 {
14215 tree vec = NULL;
14216 if (c_parser_next_token_is_not (parser, CPP_NAME)
14217 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
14218 {
14219 c_parser_error (parser, "expected identifier");
14220 return list;
14221 }
14222
14223 while (c_parser_next_token_is (parser, CPP_NAME)
14224 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
14225 {
14226 tree t = lookup_name (c_parser_peek_token (parser)->value);
14227 tree addend = NULL;
14228
14229 if (t == NULL_TREE)
14230 {
14231 undeclared_variable (c_parser_peek_token (parser)->location,
14232 c_parser_peek_token (parser)->value);
14233 t = error_mark_node;
14234 }
14235
14236 c_parser_consume_token (parser);
14237
14238 bool neg = false;
14239 if (c_parser_next_token_is (parser, CPP_MINUS))
14240 neg = true;
14241 else if (!c_parser_next_token_is (parser, CPP_PLUS))
14242 {
14243 addend = integer_zero_node;
14244 neg = false;
14245 goto add_to_vector;
14246 }
14247 c_parser_consume_token (parser);
14248
14249 if (c_parser_next_token_is_not (parser, CPP_NUMBER))
14250 {
14251 c_parser_error (parser, "expected integer");
14252 return list;
14253 }
14254
14255 addend = c_parser_peek_token (parser)->value;
14256 if (TREE_CODE (addend) != INTEGER_CST)
14257 {
14258 c_parser_error (parser, "expected integer");
14259 return list;
14260 }
14261 c_parser_consume_token (parser);
14262
14263 add_to_vector:
14264 if (t != error_mark_node)
14265 {
14266 vec = tree_cons (addend, t, vec);
14267 if (neg)
14268 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec) = 1;
14269 }
14270
14271 if (c_parser_next_token_is_not (parser, CPP_COMMA))
14272 break;
14273
14274 c_parser_consume_token (parser);
14275 }
14276
14277 if (vec == NULL_TREE)
14278 return list;
14279
14280 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
14281 OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
14282 OMP_CLAUSE_DECL (u) = nreverse (vec);
14283 OMP_CLAUSE_CHAIN (u) = list;
14284 return u;
14285 }
14286
14287 /* OpenMP 5.0:
14288 iterators ( iterators-definition )
14289
14290 iterators-definition:
14291 iterator-specifier
14292 iterator-specifier , iterators-definition
14293
14294 iterator-specifier:
14295 identifier = range-specification
14296 iterator-type identifier = range-specification
14297
14298 range-specification:
14299 begin : end
14300 begin : end : step */
14301
14302 static tree
14303 c_parser_omp_iterators (c_parser *parser)
14304 {
14305 tree ret = NULL_TREE, *last = &ret;
14306 c_parser_consume_token (parser);
14307
14308 push_scope ();
14309
14310 matching_parens parens;
14311 if (!parens.require_open (parser))
14312 return error_mark_node;
14313
14314 do
14315 {
14316 tree iter_type = NULL_TREE, type_expr = NULL_TREE;
14317 if (c_parser_next_tokens_start_typename (parser, cla_prefer_id))
14318 {
14319 struct c_type_name *type = c_parser_type_name (parser);
14320 if (type != NULL)
14321 iter_type = groktypename (type, &type_expr, NULL);
14322 }
14323 if (iter_type == NULL_TREE)
14324 iter_type = integer_type_node;
14325
14326 location_t loc = c_parser_peek_token (parser)->location;
14327 if (!c_parser_next_token_is (parser, CPP_NAME))
14328 {
14329 c_parser_error (parser, "expected identifier");
14330 break;
14331 }
14332
14333 tree id = c_parser_peek_token (parser)->value;
14334 c_parser_consume_token (parser);
14335
14336 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
14337 break;
14338
14339 location_t eloc = c_parser_peek_token (parser)->location;
14340 c_expr expr = c_parser_expr_no_commas (parser, NULL);
14341 expr = convert_lvalue_to_rvalue (eloc, expr, true, false);
14342 tree begin = expr.value;
14343
14344 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
14345 break;
14346
14347 eloc = c_parser_peek_token (parser)->location;
14348 expr = c_parser_expr_no_commas (parser, NULL);
14349 expr = convert_lvalue_to_rvalue (eloc, expr, true, false);
14350 tree end = expr.value;
14351
14352 tree step = integer_one_node;
14353 if (c_parser_next_token_is (parser, CPP_COLON))
14354 {
14355 c_parser_consume_token (parser);
14356 eloc = c_parser_peek_token (parser)->location;
14357 expr = c_parser_expr_no_commas (parser, NULL);
14358 expr = convert_lvalue_to_rvalue (eloc, expr, true, false);
14359 step = expr.value;
14360 }
14361
14362 tree iter_var = build_decl (loc, VAR_DECL, id, iter_type);
14363 DECL_ARTIFICIAL (iter_var) = 1;
14364 DECL_CONTEXT (iter_var) = current_function_decl;
14365 pushdecl (iter_var);
14366
14367 *last = make_tree_vec (6);
14368 TREE_VEC_ELT (*last, 0) = iter_var;
14369 TREE_VEC_ELT (*last, 1) = begin;
14370 TREE_VEC_ELT (*last, 2) = end;
14371 TREE_VEC_ELT (*last, 3) = step;
14372 last = &TREE_CHAIN (*last);
14373
14374 if (c_parser_next_token_is (parser, CPP_COMMA))
14375 {
14376 c_parser_consume_token (parser);
14377 continue;
14378 }
14379 break;
14380 }
14381 while (1);
14382
14383 parens.skip_until_found_close (parser);
14384 return ret ? ret : error_mark_node;
14385 }
14386
14387 /* OpenMP 4.0:
14388 depend ( depend-kind: variable-list )
14389
14390 depend-kind:
14391 in | out | inout
14392
14393 OpenMP 4.5:
14394 depend ( source )
14395
14396 depend ( sink : vec )
14397
14398 OpenMP 5.0:
14399 depend ( depend-modifier , depend-kind: variable-list )
14400
14401 depend-kind:
14402 in | out | inout | mutexinoutset | depobj
14403
14404 depend-modifier:
14405 iterator ( iterators-definition ) */
14406
14407 static tree
14408 c_parser_omp_clause_depend (c_parser *parser, tree list)
14409 {
14410 location_t clause_loc = c_parser_peek_token (parser)->location;
14411 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_LAST;
14412 tree nl, c, iterators = NULL_TREE;
14413
14414 matching_parens parens;
14415 if (!parens.require_open (parser))
14416 return list;
14417
14418 do
14419 {
14420 if (c_parser_next_token_is_not (parser, CPP_NAME))
14421 goto invalid_kind;
14422
14423 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
14424 if (strcmp ("iterator", p) == 0 && iterators == NULL_TREE)
14425 {
14426 iterators = c_parser_omp_iterators (parser);
14427 c_parser_require (parser, CPP_COMMA, "expected %<,%>");
14428 continue;
14429 }
14430 if (strcmp ("in", p) == 0)
14431 kind = OMP_CLAUSE_DEPEND_IN;
14432 else if (strcmp ("inout", p) == 0)
14433 kind = OMP_CLAUSE_DEPEND_INOUT;
14434 else if (strcmp ("mutexinoutset", p) == 0)
14435 kind = OMP_CLAUSE_DEPEND_MUTEXINOUTSET;
14436 else if (strcmp ("out", p) == 0)
14437 kind = OMP_CLAUSE_DEPEND_OUT;
14438 else if (strcmp ("depobj", p) == 0)
14439 kind = OMP_CLAUSE_DEPEND_DEPOBJ;
14440 else if (strcmp ("sink", p) == 0)
14441 kind = OMP_CLAUSE_DEPEND_SINK;
14442 else if (strcmp ("source", p) == 0)
14443 kind = OMP_CLAUSE_DEPEND_SOURCE;
14444 else
14445 goto invalid_kind;
14446 break;
14447 }
14448 while (1);
14449
14450 c_parser_consume_token (parser);
14451
14452 if (iterators
14453 && (kind == OMP_CLAUSE_DEPEND_SOURCE || kind == OMP_CLAUSE_DEPEND_SINK))
14454 {
14455 pop_scope ();
14456 error_at (clause_loc, "%<iterator%> modifier incompatible with %qs",
14457 kind == OMP_CLAUSE_DEPEND_SOURCE ? "source" : "sink");
14458 iterators = NULL_TREE;
14459 }
14460
14461 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
14462 {
14463 c = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
14464 OMP_CLAUSE_DEPEND_KIND (c) = kind;
14465 OMP_CLAUSE_DECL (c) = NULL_TREE;
14466 OMP_CLAUSE_CHAIN (c) = list;
14467 parens.skip_until_found_close (parser);
14468 return c;
14469 }
14470
14471 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
14472 goto resync_fail;
14473
14474 if (kind == OMP_CLAUSE_DEPEND_SINK)
14475 nl = c_parser_omp_clause_depend_sink (parser, clause_loc, list);
14476 else
14477 {
14478 nl = c_parser_omp_variable_list (parser, clause_loc,
14479 OMP_CLAUSE_DEPEND, list);
14480
14481 if (iterators)
14482 {
14483 tree block = pop_scope ();
14484 if (iterators == error_mark_node)
14485 iterators = NULL_TREE;
14486 else
14487 TREE_VEC_ELT (iterators, 5) = block;
14488 }
14489
14490 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
14491 {
14492 OMP_CLAUSE_DEPEND_KIND (c) = kind;
14493 if (iterators)
14494 OMP_CLAUSE_DECL (c)
14495 = build_tree_list (iterators, OMP_CLAUSE_DECL (c));
14496 }
14497 }
14498
14499 parens.skip_until_found_close (parser);
14500 return nl;
14501
14502 invalid_kind:
14503 c_parser_error (parser, "invalid depend kind");
14504 resync_fail:
14505 parens.skip_until_found_close (parser);
14506 if (iterators)
14507 pop_scope ();
14508 return list;
14509 }
14510
14511 /* OpenMP 4.0:
14512 map ( map-kind: variable-list )
14513 map ( variable-list )
14514
14515 map-kind:
14516 alloc | to | from | tofrom
14517
14518 OpenMP 4.5:
14519 map-kind:
14520 alloc | to | from | tofrom | release | delete
14521
14522 map ( always [,] map-kind: variable-list ) */
14523
14524 static tree
14525 c_parser_omp_clause_map (c_parser *parser, tree list)
14526 {
14527 location_t clause_loc = c_parser_peek_token (parser)->location;
14528 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
14529 int always = 0;
14530 enum c_id_kind always_id_kind = C_ID_NONE;
14531 location_t always_loc = UNKNOWN_LOCATION;
14532 tree always_id = NULL_TREE;
14533 tree nl, c;
14534
14535 matching_parens parens;
14536 if (!parens.require_open (parser))
14537 return list;
14538
14539 if (c_parser_next_token_is (parser, CPP_NAME))
14540 {
14541 c_token *tok = c_parser_peek_token (parser);
14542 const char *p = IDENTIFIER_POINTER (tok->value);
14543 always_id_kind = tok->id_kind;
14544 always_loc = tok->location;
14545 always_id = tok->value;
14546 if (strcmp ("always", p) == 0)
14547 {
14548 c_token *sectok = c_parser_peek_2nd_token (parser);
14549 if (sectok->type == CPP_COMMA)
14550 {
14551 c_parser_consume_token (parser);
14552 c_parser_consume_token (parser);
14553 always = 2;
14554 }
14555 else if (sectok->type == CPP_NAME)
14556 {
14557 p = IDENTIFIER_POINTER (sectok->value);
14558 if (strcmp ("alloc", p) == 0
14559 || strcmp ("to", p) == 0
14560 || strcmp ("from", p) == 0
14561 || strcmp ("tofrom", p) == 0
14562 || strcmp ("release", p) == 0
14563 || strcmp ("delete", p) == 0)
14564 {
14565 c_parser_consume_token (parser);
14566 always = 1;
14567 }
14568 }
14569 }
14570 }
14571
14572 if (c_parser_next_token_is (parser, CPP_NAME)
14573 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
14574 {
14575 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
14576 if (strcmp ("alloc", p) == 0)
14577 kind = GOMP_MAP_ALLOC;
14578 else if (strcmp ("to", p) == 0)
14579 kind = always ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
14580 else if (strcmp ("from", p) == 0)
14581 kind = always ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
14582 else if (strcmp ("tofrom", p) == 0)
14583 kind = always ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
14584 else if (strcmp ("release", p) == 0)
14585 kind = GOMP_MAP_RELEASE;
14586 else if (strcmp ("delete", p) == 0)
14587 kind = GOMP_MAP_DELETE;
14588 else
14589 {
14590 c_parser_error (parser, "invalid map kind");
14591 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
14592 "expected %<)%>");
14593 return list;
14594 }
14595 c_parser_consume_token (parser);
14596 c_parser_consume_token (parser);
14597 }
14598 else if (always)
14599 {
14600 if (always_id_kind != C_ID_ID)
14601 {
14602 c_parser_error (parser, "expected identifier");
14603 parens.skip_until_found_close (parser);
14604 return list;
14605 }
14606
14607 tree t = lookup_name (always_id);
14608 if (t == NULL_TREE)
14609 {
14610 undeclared_variable (always_loc, always_id);
14611 t = error_mark_node;
14612 }
14613 if (t != error_mark_node)
14614 {
14615 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_MAP);
14616 OMP_CLAUSE_DECL (u) = t;
14617 OMP_CLAUSE_CHAIN (u) = list;
14618 OMP_CLAUSE_SET_MAP_KIND (u, kind);
14619 list = u;
14620 }
14621 if (always == 1)
14622 {
14623 parens.skip_until_found_close (parser);
14624 return list;
14625 }
14626 }
14627
14628 nl = c_parser_omp_variable_list (parser, clause_loc, OMP_CLAUSE_MAP, list);
14629
14630 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
14631 OMP_CLAUSE_SET_MAP_KIND (c, kind);
14632
14633 parens.skip_until_found_close (parser);
14634 return nl;
14635 }
14636
14637 /* OpenMP 4.0:
14638 device ( expression ) */
14639
14640 static tree
14641 c_parser_omp_clause_device (c_parser *parser, tree list)
14642 {
14643 location_t clause_loc = c_parser_peek_token (parser)->location;
14644 matching_parens parens;
14645 if (parens.require_open (parser))
14646 {
14647 location_t expr_loc = c_parser_peek_token (parser)->location;
14648 c_expr expr = c_parser_expr_no_commas (parser, NULL);
14649 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
14650 tree c, t = expr.value;
14651 t = c_fully_fold (t, false, NULL);
14652
14653 parens.skip_until_found_close (parser);
14654
14655 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
14656 {
14657 c_parser_error (parser, "expected integer expression");
14658 return list;
14659 }
14660
14661 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE, "device");
14662
14663 c = build_omp_clause (clause_loc, OMP_CLAUSE_DEVICE);
14664 OMP_CLAUSE_DEVICE_ID (c) = t;
14665 OMP_CLAUSE_CHAIN (c) = list;
14666 list = c;
14667 }
14668
14669 return list;
14670 }
14671
14672 /* OpenMP 4.0:
14673 dist_schedule ( static )
14674 dist_schedule ( static , expression ) */
14675
14676 static tree
14677 c_parser_omp_clause_dist_schedule (c_parser *parser, tree list)
14678 {
14679 tree c, t = NULL_TREE;
14680 location_t loc = c_parser_peek_token (parser)->location;
14681
14682 matching_parens parens;
14683 if (!parens.require_open (parser))
14684 return list;
14685
14686 if (!c_parser_next_token_is_keyword (parser, RID_STATIC))
14687 {
14688 c_parser_error (parser, "invalid dist_schedule kind");
14689 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
14690 "expected %<)%>");
14691 return list;
14692 }
14693
14694 c_parser_consume_token (parser);
14695 if (c_parser_next_token_is (parser, CPP_COMMA))
14696 {
14697 c_parser_consume_token (parser);
14698
14699 location_t expr_loc = c_parser_peek_token (parser)->location;
14700 c_expr expr = c_parser_expr_no_commas (parser, NULL);
14701 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
14702 t = expr.value;
14703 t = c_fully_fold (t, false, NULL);
14704 parens.skip_until_found_close (parser);
14705 }
14706 else
14707 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
14708 "expected %<,%> or %<)%>");
14709
14710 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
14711 if (t == error_mark_node)
14712 return list;
14713
14714 c = build_omp_clause (loc, OMP_CLAUSE_DIST_SCHEDULE);
14715 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
14716 OMP_CLAUSE_CHAIN (c) = list;
14717 return c;
14718 }
14719
14720 /* OpenMP 4.0:
14721 proc_bind ( proc-bind-kind )
14722
14723 proc-bind-kind:
14724 master | close | spread */
14725
14726 static tree
14727 c_parser_omp_clause_proc_bind (c_parser *parser, tree list)
14728 {
14729 location_t clause_loc = c_parser_peek_token (parser)->location;
14730 enum omp_clause_proc_bind_kind kind;
14731 tree c;
14732
14733 matching_parens parens;
14734 if (!parens.require_open (parser))
14735 return list;
14736
14737 if (c_parser_next_token_is (parser, CPP_NAME))
14738 {
14739 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
14740 if (strcmp ("master", p) == 0)
14741 kind = OMP_CLAUSE_PROC_BIND_MASTER;
14742 else if (strcmp ("close", p) == 0)
14743 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
14744 else if (strcmp ("spread", p) == 0)
14745 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
14746 else
14747 goto invalid_kind;
14748 }
14749 else
14750 goto invalid_kind;
14751
14752 c_parser_consume_token (parser);
14753 parens.skip_until_found_close (parser);
14754 c = build_omp_clause (clause_loc, OMP_CLAUSE_PROC_BIND);
14755 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
14756 OMP_CLAUSE_CHAIN (c) = list;
14757 return c;
14758
14759 invalid_kind:
14760 c_parser_error (parser, "invalid proc_bind kind");
14761 parens.skip_until_found_close (parser);
14762 return list;
14763 }
14764
14765 /* OpenMP 4.0:
14766 to ( variable-list ) */
14767
14768 static tree
14769 c_parser_omp_clause_to (c_parser *parser, tree list)
14770 {
14771 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_TO, list);
14772 }
14773
14774 /* OpenMP 4.0:
14775 from ( variable-list ) */
14776
14777 static tree
14778 c_parser_omp_clause_from (c_parser *parser, tree list)
14779 {
14780 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_FROM, list);
14781 }
14782
14783 /* OpenMP 4.0:
14784 uniform ( variable-list ) */
14785
14786 static tree
14787 c_parser_omp_clause_uniform (c_parser *parser, tree list)
14788 {
14789 /* The clauses location. */
14790 location_t loc = c_parser_peek_token (parser)->location;
14791
14792 matching_parens parens;
14793 if (parens.require_open (parser))
14794 {
14795 list = c_parser_omp_variable_list (parser, loc, OMP_CLAUSE_UNIFORM,
14796 list);
14797 parens.skip_until_found_close (parser);
14798 }
14799 return list;
14800 }
14801
14802 /* Parse all OpenACC clauses. The set clauses allowed by the directive
14803 is a bitmask in MASK. Return the list of clauses found. */
14804
14805 static tree
14806 c_parser_oacc_all_clauses (c_parser *parser, omp_clause_mask mask,
14807 const char *where, bool finish_p = true)
14808 {
14809 tree clauses = NULL;
14810 bool first = true;
14811
14812 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
14813 {
14814 location_t here;
14815 pragma_omp_clause c_kind;
14816 const char *c_name;
14817 tree prev = clauses;
14818
14819 if (!first && c_parser_next_token_is (parser, CPP_COMMA))
14820 c_parser_consume_token (parser);
14821
14822 here = c_parser_peek_token (parser)->location;
14823 c_kind = c_parser_omp_clause_name (parser);
14824
14825 switch (c_kind)
14826 {
14827 case PRAGMA_OACC_CLAUSE_ASYNC:
14828 clauses = c_parser_oacc_clause_async (parser, clauses);
14829 c_name = "async";
14830 break;
14831 case PRAGMA_OACC_CLAUSE_AUTO:
14832 clauses = c_parser_oacc_simple_clause (here, OMP_CLAUSE_AUTO,
14833 clauses);
14834 c_name = "auto";
14835 break;
14836 case PRAGMA_OACC_CLAUSE_COLLAPSE:
14837 clauses = c_parser_omp_clause_collapse (parser, clauses);
14838 c_name = "collapse";
14839 break;
14840 case PRAGMA_OACC_CLAUSE_COPY:
14841 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
14842 c_name = "copy";
14843 break;
14844 case PRAGMA_OACC_CLAUSE_COPYIN:
14845 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
14846 c_name = "copyin";
14847 break;
14848 case PRAGMA_OACC_CLAUSE_COPYOUT:
14849 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
14850 c_name = "copyout";
14851 break;
14852 case PRAGMA_OACC_CLAUSE_CREATE:
14853 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
14854 c_name = "create";
14855 break;
14856 case PRAGMA_OACC_CLAUSE_DELETE:
14857 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
14858 c_name = "delete";
14859 break;
14860 case PRAGMA_OMP_CLAUSE_DEFAULT:
14861 clauses = c_parser_omp_clause_default (parser, clauses, true);
14862 c_name = "default";
14863 break;
14864 case PRAGMA_OACC_CLAUSE_DEVICE:
14865 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
14866 c_name = "device";
14867 break;
14868 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
14869 clauses = c_parser_oacc_data_clause_deviceptr (parser, clauses);
14870 c_name = "deviceptr";
14871 break;
14872 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
14873 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
14874 c_name = "device_resident";
14875 break;
14876 case PRAGMA_OACC_CLAUSE_FINALIZE:
14877 clauses = c_parser_oacc_simple_clause (here, OMP_CLAUSE_FINALIZE,
14878 clauses);
14879 c_name = "finalize";
14880 break;
14881 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
14882 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
14883 c_name = "firstprivate";
14884 break;
14885 case PRAGMA_OACC_CLAUSE_GANG:
14886 c_name = "gang";
14887 clauses = c_parser_oacc_shape_clause (parser, here, OMP_CLAUSE_GANG,
14888 c_name, clauses);
14889 break;
14890 case PRAGMA_OACC_CLAUSE_HOST:
14891 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
14892 c_name = "host";
14893 break;
14894 case PRAGMA_OACC_CLAUSE_IF:
14895 clauses = c_parser_omp_clause_if (parser, clauses, false);
14896 c_name = "if";
14897 break;
14898 case PRAGMA_OACC_CLAUSE_IF_PRESENT:
14899 clauses = c_parser_oacc_simple_clause (here, OMP_CLAUSE_IF_PRESENT,
14900 clauses);
14901 c_name = "if_present";
14902 break;
14903 case PRAGMA_OACC_CLAUSE_INDEPENDENT:
14904 clauses = c_parser_oacc_simple_clause (here, OMP_CLAUSE_INDEPENDENT,
14905 clauses);
14906 c_name = "independent";
14907 break;
14908 case PRAGMA_OACC_CLAUSE_LINK:
14909 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
14910 c_name = "link";
14911 break;
14912 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
14913 clauses = c_parser_oacc_single_int_clause (parser,
14914 OMP_CLAUSE_NUM_GANGS,
14915 clauses);
14916 c_name = "num_gangs";
14917 break;
14918 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
14919 clauses = c_parser_oacc_single_int_clause (parser,
14920 OMP_CLAUSE_NUM_WORKERS,
14921 clauses);
14922 c_name = "num_workers";
14923 break;
14924 case PRAGMA_OACC_CLAUSE_PRESENT:
14925 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
14926 c_name = "present";
14927 break;
14928 case PRAGMA_OACC_CLAUSE_PRIVATE:
14929 clauses = c_parser_omp_clause_private (parser, clauses);
14930 c_name = "private";
14931 break;
14932 case PRAGMA_OACC_CLAUSE_REDUCTION:
14933 clauses
14934 = c_parser_omp_clause_reduction (parser, OMP_CLAUSE_REDUCTION,
14935 false, clauses);
14936 c_name = "reduction";
14937 break;
14938 case PRAGMA_OACC_CLAUSE_SEQ:
14939 clauses = c_parser_oacc_simple_clause (here, OMP_CLAUSE_SEQ,
14940 clauses);
14941 c_name = "seq";
14942 break;
14943 case PRAGMA_OACC_CLAUSE_TILE:
14944 clauses = c_parser_oacc_clause_tile (parser, clauses);
14945 c_name = "tile";
14946 break;
14947 case PRAGMA_OACC_CLAUSE_USE_DEVICE:
14948 clauses = c_parser_omp_clause_use_device_ptr (parser, clauses);
14949 c_name = "use_device";
14950 break;
14951 case PRAGMA_OACC_CLAUSE_VECTOR:
14952 c_name = "vector";
14953 clauses = c_parser_oacc_shape_clause (parser, here, OMP_CLAUSE_VECTOR,
14954 c_name, clauses);
14955 break;
14956 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
14957 clauses = c_parser_oacc_single_int_clause (parser,
14958 OMP_CLAUSE_VECTOR_LENGTH,
14959 clauses);
14960 c_name = "vector_length";
14961 break;
14962 case PRAGMA_OACC_CLAUSE_WAIT:
14963 clauses = c_parser_oacc_clause_wait (parser, clauses);
14964 c_name = "wait";
14965 break;
14966 case PRAGMA_OACC_CLAUSE_WORKER:
14967 c_name = "worker";
14968 clauses = c_parser_oacc_shape_clause (parser, here, OMP_CLAUSE_WORKER,
14969 c_name, clauses);
14970 break;
14971 default:
14972 c_parser_error (parser, "expected %<#pragma acc%> clause");
14973 goto saw_error;
14974 }
14975
14976 first = false;
14977
14978 if (((mask >> c_kind) & 1) == 0)
14979 {
14980 /* Remove the invalid clause(s) from the list to avoid
14981 confusing the rest of the compiler. */
14982 clauses = prev;
14983 error_at (here, "%qs is not valid for %qs", c_name, where);
14984 }
14985 }
14986
14987 saw_error:
14988 c_parser_skip_to_pragma_eol (parser);
14989
14990 if (finish_p)
14991 return c_finish_omp_clauses (clauses, C_ORT_ACC);
14992
14993 return clauses;
14994 }
14995
14996 /* Parse all OpenMP clauses. The set clauses allowed by the directive
14997 is a bitmask in MASK. Return the list of clauses found. */
14998
14999 static tree
15000 c_parser_omp_all_clauses (c_parser *parser, omp_clause_mask mask,
15001 const char *where, bool finish_p = true)
15002 {
15003 tree clauses = NULL;
15004 bool first = true;
15005
15006 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
15007 {
15008 location_t here;
15009 pragma_omp_clause c_kind;
15010 const char *c_name;
15011 tree prev = clauses;
15012
15013 if (!first && c_parser_next_token_is (parser, CPP_COMMA))
15014 c_parser_consume_token (parser);
15015
15016 here = c_parser_peek_token (parser)->location;
15017 c_kind = c_parser_omp_clause_name (parser);
15018
15019 switch (c_kind)
15020 {
15021 case PRAGMA_OMP_CLAUSE_COLLAPSE:
15022 clauses = c_parser_omp_clause_collapse (parser, clauses);
15023 c_name = "collapse";
15024 break;
15025 case PRAGMA_OMP_CLAUSE_COPYIN:
15026 clauses = c_parser_omp_clause_copyin (parser, clauses);
15027 c_name = "copyin";
15028 break;
15029 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
15030 clauses = c_parser_omp_clause_copyprivate (parser, clauses);
15031 c_name = "copyprivate";
15032 break;
15033 case PRAGMA_OMP_CLAUSE_DEFAULT:
15034 clauses = c_parser_omp_clause_default (parser, clauses, false);
15035 c_name = "default";
15036 break;
15037 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
15038 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
15039 c_name = "firstprivate";
15040 break;
15041 case PRAGMA_OMP_CLAUSE_FINAL:
15042 clauses = c_parser_omp_clause_final (parser, clauses);
15043 c_name = "final";
15044 break;
15045 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
15046 clauses = c_parser_omp_clause_grainsize (parser, clauses);
15047 c_name = "grainsize";
15048 break;
15049 case PRAGMA_OMP_CLAUSE_HINT:
15050 clauses = c_parser_omp_clause_hint (parser, clauses);
15051 c_name = "hint";
15052 break;
15053 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
15054 clauses = c_parser_omp_clause_defaultmap (parser, clauses);
15055 c_name = "defaultmap";
15056 break;
15057 case PRAGMA_OMP_CLAUSE_IF:
15058 clauses = c_parser_omp_clause_if (parser, clauses, true);
15059 c_name = "if";
15060 break;
15061 case PRAGMA_OMP_CLAUSE_IN_REDUCTION:
15062 clauses
15063 = c_parser_omp_clause_reduction (parser, OMP_CLAUSE_IN_REDUCTION,
15064 true, clauses);
15065 c_name = "in_reduction";
15066 break;
15067 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
15068 clauses = c_parser_omp_clause_lastprivate (parser, clauses);
15069 c_name = "lastprivate";
15070 break;
15071 case PRAGMA_OMP_CLAUSE_MERGEABLE:
15072 clauses = c_parser_omp_clause_mergeable (parser, clauses);
15073 c_name = "mergeable";
15074 break;
15075 case PRAGMA_OMP_CLAUSE_NOWAIT:
15076 clauses = c_parser_omp_clause_nowait (parser, clauses);
15077 c_name = "nowait";
15078 break;
15079 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
15080 clauses = c_parser_omp_clause_num_tasks (parser, clauses);
15081 c_name = "num_tasks";
15082 break;
15083 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
15084 clauses = c_parser_omp_clause_num_threads (parser, clauses);
15085 c_name = "num_threads";
15086 break;
15087 case PRAGMA_OMP_CLAUSE_ORDERED:
15088 clauses = c_parser_omp_clause_ordered (parser, clauses);
15089 c_name = "ordered";
15090 break;
15091 case PRAGMA_OMP_CLAUSE_PRIORITY:
15092 clauses = c_parser_omp_clause_priority (parser, clauses);
15093 c_name = "priority";
15094 break;
15095 case PRAGMA_OMP_CLAUSE_PRIVATE:
15096 clauses = c_parser_omp_clause_private (parser, clauses);
15097 c_name = "private";
15098 break;
15099 case PRAGMA_OMP_CLAUSE_REDUCTION:
15100 clauses
15101 = c_parser_omp_clause_reduction (parser, OMP_CLAUSE_REDUCTION,
15102 true, clauses);
15103 c_name = "reduction";
15104 break;
15105 case PRAGMA_OMP_CLAUSE_SCHEDULE:
15106 clauses = c_parser_omp_clause_schedule (parser, clauses);
15107 c_name = "schedule";
15108 break;
15109 case PRAGMA_OMP_CLAUSE_SHARED:
15110 clauses = c_parser_omp_clause_shared (parser, clauses);
15111 c_name = "shared";
15112 break;
15113 case PRAGMA_OMP_CLAUSE_TASK_REDUCTION:
15114 clauses
15115 = c_parser_omp_clause_reduction (parser, OMP_CLAUSE_TASK_REDUCTION,
15116 true, clauses);
15117 c_name = "task_reduction";
15118 break;
15119 case PRAGMA_OMP_CLAUSE_UNTIED:
15120 clauses = c_parser_omp_clause_untied (parser, clauses);
15121 c_name = "untied";
15122 break;
15123 case PRAGMA_OMP_CLAUSE_INBRANCH:
15124 clauses = c_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
15125 clauses);
15126 c_name = "inbranch";
15127 break;
15128 case PRAGMA_OMP_CLAUSE_NONTEMPORAL:
15129 clauses = c_parser_omp_clause_nontemporal (parser, clauses);
15130 c_name = "nontemporal";
15131 break;
15132 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
15133 clauses = c_parser_omp_clause_branch (parser, OMP_CLAUSE_NOTINBRANCH,
15134 clauses);
15135 c_name = "notinbranch";
15136 break;
15137 case PRAGMA_OMP_CLAUSE_PARALLEL:
15138 clauses
15139 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
15140 clauses);
15141 c_name = "parallel";
15142 if (!first)
15143 {
15144 clause_not_first:
15145 error_at (here, "%qs must be the first clause of %qs",
15146 c_name, where);
15147 clauses = prev;
15148 }
15149 break;
15150 case PRAGMA_OMP_CLAUSE_FOR:
15151 clauses
15152 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
15153 clauses);
15154 c_name = "for";
15155 if (!first)
15156 goto clause_not_first;
15157 break;
15158 case PRAGMA_OMP_CLAUSE_SECTIONS:
15159 clauses
15160 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
15161 clauses);
15162 c_name = "sections";
15163 if (!first)
15164 goto clause_not_first;
15165 break;
15166 case PRAGMA_OMP_CLAUSE_TASKGROUP:
15167 clauses
15168 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
15169 clauses);
15170 c_name = "taskgroup";
15171 if (!first)
15172 goto clause_not_first;
15173 break;
15174 case PRAGMA_OMP_CLAUSE_LINK:
15175 clauses
15176 = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_LINK, clauses);
15177 c_name = "link";
15178 break;
15179 case PRAGMA_OMP_CLAUSE_TO:
15180 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
15181 clauses
15182 = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_TO_DECLARE,
15183 clauses);
15184 else
15185 clauses = c_parser_omp_clause_to (parser, clauses);
15186 c_name = "to";
15187 break;
15188 case PRAGMA_OMP_CLAUSE_FROM:
15189 clauses = c_parser_omp_clause_from (parser, clauses);
15190 c_name = "from";
15191 break;
15192 case PRAGMA_OMP_CLAUSE_UNIFORM:
15193 clauses = c_parser_omp_clause_uniform (parser, clauses);
15194 c_name = "uniform";
15195 break;
15196 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
15197 clauses = c_parser_omp_clause_num_teams (parser, clauses);
15198 c_name = "num_teams";
15199 break;
15200 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
15201 clauses = c_parser_omp_clause_thread_limit (parser, clauses);
15202 c_name = "thread_limit";
15203 break;
15204 case PRAGMA_OMP_CLAUSE_ALIGNED:
15205 clauses = c_parser_omp_clause_aligned (parser, clauses);
15206 c_name = "aligned";
15207 break;
15208 case PRAGMA_OMP_CLAUSE_LINEAR:
15209 clauses = c_parser_omp_clause_linear (parser, clauses);
15210 c_name = "linear";
15211 break;
15212 case PRAGMA_OMP_CLAUSE_DEPEND:
15213 clauses = c_parser_omp_clause_depend (parser, clauses);
15214 c_name = "depend";
15215 break;
15216 case PRAGMA_OMP_CLAUSE_MAP:
15217 clauses = c_parser_omp_clause_map (parser, clauses);
15218 c_name = "map";
15219 break;
15220 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
15221 clauses = c_parser_omp_clause_use_device_ptr (parser, clauses);
15222 c_name = "use_device_ptr";
15223 break;
15224 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
15225 clauses = c_parser_omp_clause_is_device_ptr (parser, clauses);
15226 c_name = "is_device_ptr";
15227 break;
15228 case PRAGMA_OMP_CLAUSE_DEVICE:
15229 clauses = c_parser_omp_clause_device (parser, clauses);
15230 c_name = "device";
15231 break;
15232 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
15233 clauses = c_parser_omp_clause_dist_schedule (parser, clauses);
15234 c_name = "dist_schedule";
15235 break;
15236 case PRAGMA_OMP_CLAUSE_PROC_BIND:
15237 clauses = c_parser_omp_clause_proc_bind (parser, clauses);
15238 c_name = "proc_bind";
15239 break;
15240 case PRAGMA_OMP_CLAUSE_SAFELEN:
15241 clauses = c_parser_omp_clause_safelen (parser, clauses);
15242 c_name = "safelen";
15243 break;
15244 case PRAGMA_OMP_CLAUSE_SIMDLEN:
15245 clauses = c_parser_omp_clause_simdlen (parser, clauses);
15246 c_name = "simdlen";
15247 break;
15248 case PRAGMA_OMP_CLAUSE_NOGROUP:
15249 clauses = c_parser_omp_clause_nogroup (parser, clauses);
15250 c_name = "nogroup";
15251 break;
15252 case PRAGMA_OMP_CLAUSE_THREADS:
15253 clauses
15254 = c_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
15255 clauses);
15256 c_name = "threads";
15257 break;
15258 case PRAGMA_OMP_CLAUSE_SIMD:
15259 clauses
15260 = c_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
15261 clauses);
15262 c_name = "simd";
15263 break;
15264 default:
15265 c_parser_error (parser, "expected %<#pragma omp%> clause");
15266 goto saw_error;
15267 }
15268
15269 first = false;
15270
15271 if (((mask >> c_kind) & 1) == 0)
15272 {
15273 /* Remove the invalid clause(s) from the list to avoid
15274 confusing the rest of the compiler. */
15275 clauses = prev;
15276 error_at (here, "%qs is not valid for %qs", c_name, where);
15277 }
15278 }
15279
15280 saw_error:
15281 c_parser_skip_to_pragma_eol (parser);
15282
15283 if (finish_p)
15284 {
15285 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
15286 return c_finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
15287 return c_finish_omp_clauses (clauses, C_ORT_OMP);
15288 }
15289
15290 return clauses;
15291 }
15292
15293 /* OpenACC 2.0, OpenMP 2.5:
15294 structured-block:
15295 statement
15296
15297 In practice, we're also interested in adding the statement to an
15298 outer node. So it is convenient if we work around the fact that
15299 c_parser_statement calls add_stmt. */
15300
15301 static tree
15302 c_parser_omp_structured_block (c_parser *parser, bool *if_p)
15303 {
15304 tree stmt = push_stmt_list ();
15305 c_parser_statement (parser, if_p);
15306 return pop_stmt_list (stmt);
15307 }
15308
15309 /* OpenACC 2.0:
15310 # pragma acc cache (variable-list) new-line
15311
15312 LOC is the location of the #pragma token.
15313 */
15314
15315 static tree
15316 c_parser_oacc_cache (location_t loc, c_parser *parser)
15317 {
15318 tree stmt, clauses;
15319
15320 clauses = c_parser_omp_var_list_parens (parser, OMP_CLAUSE__CACHE_, NULL);
15321 clauses = c_finish_omp_clauses (clauses, C_ORT_ACC);
15322
15323 c_parser_skip_to_pragma_eol (parser);
15324
15325 stmt = make_node (OACC_CACHE);
15326 TREE_TYPE (stmt) = void_type_node;
15327 OACC_CACHE_CLAUSES (stmt) = clauses;
15328 SET_EXPR_LOCATION (stmt, loc);
15329 add_stmt (stmt);
15330
15331 return stmt;
15332 }
15333
15334 /* OpenACC 2.0:
15335 # pragma acc data oacc-data-clause[optseq] new-line
15336 structured-block
15337
15338 LOC is the location of the #pragma token.
15339 */
15340
15341 #define OACC_DATA_CLAUSE_MASK \
15342 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
15343 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
15344 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
15345 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
15346 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
15347 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
15348 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT))
15349
15350 static tree
15351 c_parser_oacc_data (location_t loc, c_parser *parser, bool *if_p)
15352 {
15353 tree stmt, clauses, block;
15354
15355 clauses = c_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
15356 "#pragma acc data");
15357
15358 block = c_begin_omp_parallel ();
15359 add_stmt (c_parser_omp_structured_block (parser, if_p));
15360
15361 stmt = c_finish_oacc_data (loc, clauses, block);
15362
15363 return stmt;
15364 }
15365
15366 /* OpenACC 2.0:
15367 # pragma acc declare oacc-data-clause[optseq] new-line
15368 */
15369
15370 #define OACC_DECLARE_CLAUSE_MASK \
15371 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
15372 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
15373 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
15374 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
15375 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
15376 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
15377 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
15378 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT))
15379
15380 static void
15381 c_parser_oacc_declare (c_parser *parser)
15382 {
15383 location_t pragma_loc = c_parser_peek_token (parser)->location;
15384 tree clauses, stmt, t, decl;
15385
15386 bool error = false;
15387
15388 c_parser_consume_pragma (parser);
15389
15390 clauses = c_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
15391 "#pragma acc declare");
15392 if (!clauses)
15393 {
15394 error_at (pragma_loc,
15395 "no valid clauses specified in %<#pragma acc declare%>");
15396 return;
15397 }
15398
15399 for (t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
15400 {
15401 location_t loc = OMP_CLAUSE_LOCATION (t);
15402 decl = OMP_CLAUSE_DECL (t);
15403 if (!DECL_P (decl))
15404 {
15405 error_at (loc, "array section in %<#pragma acc declare%>");
15406 error = true;
15407 continue;
15408 }
15409
15410 switch (OMP_CLAUSE_MAP_KIND (t))
15411 {
15412 case GOMP_MAP_FIRSTPRIVATE_POINTER:
15413 case GOMP_MAP_ALLOC:
15414 case GOMP_MAP_TO:
15415 case GOMP_MAP_FORCE_DEVICEPTR:
15416 case GOMP_MAP_DEVICE_RESIDENT:
15417 break;
15418
15419 case GOMP_MAP_LINK:
15420 if (!global_bindings_p ()
15421 && (TREE_STATIC (decl)
15422 || !DECL_EXTERNAL (decl)))
15423 {
15424 error_at (loc,
15425 "%qD must be a global variable in "
15426 "%<#pragma acc declare link%>",
15427 decl);
15428 error = true;
15429 continue;
15430 }
15431 break;
15432
15433 default:
15434 if (global_bindings_p ())
15435 {
15436 error_at (loc, "invalid OpenACC clause at file scope");
15437 error = true;
15438 continue;
15439 }
15440 if (DECL_EXTERNAL (decl))
15441 {
15442 error_at (loc,
15443 "invalid use of %<extern%> variable %qD "
15444 "in %<#pragma acc declare%>", decl);
15445 error = true;
15446 continue;
15447 }
15448 else if (TREE_PUBLIC (decl))
15449 {
15450 error_at (loc,
15451 "invalid use of %<global%> variable %qD "
15452 "in %<#pragma acc declare%>", decl);
15453 error = true;
15454 continue;
15455 }
15456 break;
15457 }
15458
15459 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
15460 || lookup_attribute ("omp declare target link",
15461 DECL_ATTRIBUTES (decl)))
15462 {
15463 error_at (loc, "variable %qD used more than once with "
15464 "%<#pragma acc declare%>", decl);
15465 error = true;
15466 continue;
15467 }
15468
15469 if (!error)
15470 {
15471 tree id;
15472
15473 if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
15474 id = get_identifier ("omp declare target link");
15475 else
15476 id = get_identifier ("omp declare target");
15477
15478 DECL_ATTRIBUTES (decl)
15479 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
15480
15481 if (global_bindings_p ())
15482 {
15483 symtab_node *node = symtab_node::get (decl);
15484 if (node != NULL)
15485 {
15486 node->offloadable = 1;
15487 if (ENABLE_OFFLOADING)
15488 {
15489 g->have_offload = true;
15490 if (is_a <varpool_node *> (node))
15491 vec_safe_push (offload_vars, decl);
15492 }
15493 }
15494 }
15495 }
15496 }
15497
15498 if (error || global_bindings_p ())
15499 return;
15500
15501 stmt = make_node (OACC_DECLARE);
15502 TREE_TYPE (stmt) = void_type_node;
15503 OACC_DECLARE_CLAUSES (stmt) = clauses;
15504 SET_EXPR_LOCATION (stmt, pragma_loc);
15505
15506 add_stmt (stmt);
15507
15508 return;
15509 }
15510
15511 /* OpenACC 2.0:
15512 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
15513
15514 or
15515
15516 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
15517
15518
15519 LOC is the location of the #pragma token.
15520 */
15521
15522 #define OACC_ENTER_DATA_CLAUSE_MASK \
15523 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
15524 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
15525 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
15526 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
15527 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
15528
15529 #define OACC_EXIT_DATA_CLAUSE_MASK \
15530 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
15531 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
15532 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
15533 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
15534 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FINALIZE) \
15535 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
15536
15537 static void
15538 c_parser_oacc_enter_exit_data (c_parser *parser, bool enter)
15539 {
15540 location_t loc = c_parser_peek_token (parser)->location;
15541 tree clauses, stmt;
15542 const char *p = "";
15543
15544 c_parser_consume_pragma (parser);
15545
15546 if (c_parser_next_token_is (parser, CPP_NAME))
15547 {
15548 p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
15549 c_parser_consume_token (parser);
15550 }
15551
15552 if (strcmp (p, "data") != 0)
15553 {
15554 error_at (loc, "expected %<data%> after %<#pragma acc %s%>",
15555 enter ? "enter" : "exit");
15556 parser->error = true;
15557 c_parser_skip_to_pragma_eol (parser);
15558 return;
15559 }
15560
15561 if (enter)
15562 clauses = c_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
15563 "#pragma acc enter data");
15564 else
15565 clauses = c_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
15566 "#pragma acc exit data");
15567
15568 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
15569 {
15570 error_at (loc, "%<#pragma acc %s data%> has no data movement clause",
15571 enter ? "enter" : "exit");
15572 return;
15573 }
15574
15575 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
15576 TREE_TYPE (stmt) = void_type_node;
15577 OMP_STANDALONE_CLAUSES (stmt) = clauses;
15578 SET_EXPR_LOCATION (stmt, loc);
15579 add_stmt (stmt);
15580 }
15581
15582
15583 /* OpenACC 2.0:
15584 # pragma acc host_data oacc-data-clause[optseq] new-line
15585 structured-block
15586 */
15587
15588 #define OACC_HOST_DATA_CLAUSE_MASK \
15589 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
15590
15591 static tree
15592 c_parser_oacc_host_data (location_t loc, c_parser *parser, bool *if_p)
15593 {
15594 tree stmt, clauses, block;
15595
15596 clauses = c_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
15597 "#pragma acc host_data");
15598
15599 block = c_begin_omp_parallel ();
15600 add_stmt (c_parser_omp_structured_block (parser, if_p));
15601 stmt = c_finish_oacc_host_data (loc, clauses, block);
15602 return stmt;
15603 }
15604
15605
15606 /* OpenACC 2.0:
15607
15608 # pragma acc loop oacc-loop-clause[optseq] new-line
15609 structured-block
15610
15611 LOC is the location of the #pragma token.
15612 */
15613
15614 #define OACC_LOOP_CLAUSE_MASK \
15615 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
15616 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
15617 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
15618 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
15619 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
15620 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
15621 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
15622 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
15623 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
15624 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE) )
15625 static tree
15626 c_parser_oacc_loop (location_t loc, c_parser *parser, char *p_name,
15627 omp_clause_mask mask, tree *cclauses, bool *if_p)
15628 {
15629 bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
15630
15631 strcat (p_name, " loop");
15632 mask |= OACC_LOOP_CLAUSE_MASK;
15633
15634 tree clauses = c_parser_oacc_all_clauses (parser, mask, p_name,
15635 cclauses == NULL);
15636 if (cclauses)
15637 {
15638 clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
15639 if (*cclauses)
15640 *cclauses = c_finish_omp_clauses (*cclauses, C_ORT_ACC);
15641 if (clauses)
15642 clauses = c_finish_omp_clauses (clauses, C_ORT_ACC);
15643 }
15644
15645 tree block = c_begin_compound_stmt (true);
15646 tree stmt = c_parser_omp_for_loop (loc, parser, OACC_LOOP, clauses, NULL,
15647 if_p);
15648 block = c_end_compound_stmt (loc, block, true);
15649 add_stmt (block);
15650
15651 return stmt;
15652 }
15653
15654 /* OpenACC 2.0:
15655 # pragma acc kernels oacc-kernels-clause[optseq] new-line
15656 structured-block
15657
15658 or
15659
15660 # pragma acc parallel oacc-parallel-clause[optseq] new-line
15661 structured-block
15662
15663 LOC is the location of the #pragma token.
15664 */
15665
15666 #define OACC_KERNELS_CLAUSE_MASK \
15667 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
15668 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
15669 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
15670 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
15671 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
15672 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
15673 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
15674 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
15675 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
15676 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
15677 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
15678 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
15679 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
15680
15681 #define OACC_PARALLEL_CLAUSE_MASK \
15682 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
15683 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
15684 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
15685 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
15686 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
15687 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
15688 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
15689 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
15690 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
15691 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
15692 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
15693 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
15694 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
15695 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
15696 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
15697 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
15698
15699 static tree
15700 c_parser_oacc_kernels_parallel (location_t loc, c_parser *parser,
15701 enum pragma_kind p_kind, char *p_name,
15702 bool *if_p)
15703 {
15704 omp_clause_mask mask;
15705 enum tree_code code;
15706 switch (p_kind)
15707 {
15708 case PRAGMA_OACC_KERNELS:
15709 strcat (p_name, " kernels");
15710 mask = OACC_KERNELS_CLAUSE_MASK;
15711 code = OACC_KERNELS;
15712 break;
15713 case PRAGMA_OACC_PARALLEL:
15714 strcat (p_name, " parallel");
15715 mask = OACC_PARALLEL_CLAUSE_MASK;
15716 code = OACC_PARALLEL;
15717 break;
15718 default:
15719 gcc_unreachable ();
15720 }
15721
15722 if (c_parser_next_token_is (parser, CPP_NAME))
15723 {
15724 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
15725 if (strcmp (p, "loop") == 0)
15726 {
15727 c_parser_consume_token (parser);
15728 tree block = c_begin_omp_parallel ();
15729 tree clauses;
15730 c_parser_oacc_loop (loc, parser, p_name, mask, &clauses, if_p);
15731 return c_finish_omp_construct (loc, code, block, clauses);
15732 }
15733 }
15734
15735 tree clauses = c_parser_oacc_all_clauses (parser, mask, p_name);
15736
15737 tree block = c_begin_omp_parallel ();
15738 add_stmt (c_parser_omp_structured_block (parser, if_p));
15739
15740 return c_finish_omp_construct (loc, code, block, clauses);
15741 }
15742
15743 /* OpenACC 2.0:
15744 # pragma acc routine oacc-routine-clause[optseq] new-line
15745 function-definition
15746
15747 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
15748 */
15749
15750 #define OACC_ROUTINE_CLAUSE_MASK \
15751 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
15752 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
15753 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
15754 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) )
15755
15756 /* Parse an OpenACC routine directive. For named directives, we apply
15757 immediately to the named function. For unnamed ones we then parse
15758 a declaration or definition, which must be for a function. */
15759
15760 static void
15761 c_parser_oacc_routine (c_parser *parser, enum pragma_context context)
15762 {
15763 gcc_checking_assert (context == pragma_external);
15764
15765 oacc_routine_data data;
15766 data.error_seen = false;
15767 data.fndecl_seen = false;
15768 data.clauses = NULL_TREE;
15769 data.loc = c_parser_peek_token (parser)->location;
15770
15771 c_parser_consume_pragma (parser);
15772
15773 /* Look for optional '( name )'. */
15774 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
15775 {
15776 c_parser_consume_token (parser); /* '(' */
15777
15778 tree decl = NULL_TREE;
15779 c_token *name_token = c_parser_peek_token (parser);
15780 location_t name_loc = name_token->location;
15781 if (name_token->type == CPP_NAME
15782 && (name_token->id_kind == C_ID_ID
15783 || name_token->id_kind == C_ID_TYPENAME))
15784 {
15785 decl = lookup_name (name_token->value);
15786 if (!decl)
15787 error_at (name_loc,
15788 "%qE has not been declared", name_token->value);
15789 c_parser_consume_token (parser);
15790 }
15791 else
15792 c_parser_error (parser, "expected function name");
15793
15794 if (!decl
15795 || !c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
15796 {
15797 c_parser_skip_to_pragma_eol (parser, false);
15798 return;
15799 }
15800
15801 data.clauses
15802 = c_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
15803 "#pragma acc routine");
15804
15805 if (TREE_CODE (decl) != FUNCTION_DECL)
15806 {
15807 error_at (name_loc, "%qD does not refer to a function", decl);
15808 return;
15809 }
15810
15811 c_finish_oacc_routine (&data, decl, false);
15812 }
15813 else /* No optional '( name )'. */
15814 {
15815 data.clauses
15816 = c_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
15817 "#pragma acc routine");
15818
15819 /* Emit a helpful diagnostic if there's another pragma following this
15820 one. Also don't allow a static assertion declaration, as in the
15821 following we'll just parse a *single* "declaration or function
15822 definition", and the static assertion counts an one. */
15823 if (c_parser_next_token_is (parser, CPP_PRAGMA)
15824 || c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
15825 {
15826 error_at (data.loc,
15827 "%<#pragma acc routine%> not immediately followed by"
15828 " function declaration or definition");
15829 /* ..., and then just keep going. */
15830 return;
15831 }
15832
15833 /* We only have to consider the pragma_external case here. */
15834 if (c_parser_next_token_is (parser, CPP_KEYWORD)
15835 && c_parser_peek_token (parser)->keyword == RID_EXTENSION)
15836 {
15837 int ext = disable_extension_diagnostics ();
15838 do
15839 c_parser_consume_token (parser);
15840 while (c_parser_next_token_is (parser, CPP_KEYWORD)
15841 && c_parser_peek_token (parser)->keyword == RID_EXTENSION);
15842 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
15843 NULL, vNULL, &data);
15844 restore_extension_diagnostics (ext);
15845 }
15846 else
15847 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
15848 NULL, vNULL, &data);
15849 }
15850 }
15851
15852 /* Finalize an OpenACC routine pragma, applying it to FNDECL.
15853 IS_DEFN is true if we're applying it to the definition. */
15854
15855 static void
15856 c_finish_oacc_routine (struct oacc_routine_data *data, tree fndecl,
15857 bool is_defn)
15858 {
15859 /* Keep going if we're in error reporting mode. */
15860 if (data->error_seen
15861 || fndecl == error_mark_node)
15862 return;
15863
15864 if (data->fndecl_seen)
15865 {
15866 error_at (data->loc,
15867 "%<#pragma acc routine%> not immediately followed by"
15868 " a single function declaration or definition");
15869 data->error_seen = true;
15870 return;
15871 }
15872 if (fndecl == NULL_TREE || TREE_CODE (fndecl) != FUNCTION_DECL)
15873 {
15874 error_at (data->loc,
15875 "%<#pragma acc routine%> not immediately followed by"
15876 " function declaration or definition");
15877 data->error_seen = true;
15878 return;
15879 }
15880
15881 if (oacc_get_fn_attrib (fndecl))
15882 {
15883 error_at (data->loc,
15884 "%<#pragma acc routine%> already applied to %qD", fndecl);
15885 data->error_seen = true;
15886 return;
15887 }
15888
15889 if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
15890 {
15891 error_at (data->loc,
15892 TREE_USED (fndecl)
15893 ? G_("%<#pragma acc routine%> must be applied before use")
15894 : G_("%<#pragma acc routine%> must be applied before "
15895 "definition"));
15896 data->error_seen = true;
15897 return;
15898 }
15899
15900 /* Process the routine's dimension clauses. */
15901 tree dims = oacc_build_routine_dims (data->clauses);
15902 oacc_replace_fn_attrib (fndecl, dims);
15903
15904 /* Add an "omp declare target" attribute. */
15905 DECL_ATTRIBUTES (fndecl)
15906 = tree_cons (get_identifier ("omp declare target"),
15907 NULL_TREE, DECL_ATTRIBUTES (fndecl));
15908
15909 /* Remember that we've used this "#pragma acc routine". */
15910 data->fndecl_seen = true;
15911 }
15912
15913 /* OpenACC 2.0:
15914 # pragma acc update oacc-update-clause[optseq] new-line
15915 */
15916
15917 #define OACC_UPDATE_CLAUSE_MASK \
15918 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
15919 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
15920 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
15921 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
15922 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF_PRESENT) \
15923 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
15924
15925 static void
15926 c_parser_oacc_update (c_parser *parser)
15927 {
15928 location_t loc = c_parser_peek_token (parser)->location;
15929
15930 c_parser_consume_pragma (parser);
15931
15932 tree clauses = c_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
15933 "#pragma acc update");
15934 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
15935 {
15936 error_at (loc,
15937 "%<#pragma acc update%> must contain at least one "
15938 "%<device%> or %<host%> or %<self%> clause");
15939 return;
15940 }
15941
15942 if (parser->error)
15943 return;
15944
15945 tree stmt = make_node (OACC_UPDATE);
15946 TREE_TYPE (stmt) = void_type_node;
15947 OACC_UPDATE_CLAUSES (stmt) = clauses;
15948 SET_EXPR_LOCATION (stmt, loc);
15949 add_stmt (stmt);
15950 }
15951
15952 /* OpenACC 2.0:
15953 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
15954
15955 LOC is the location of the #pragma token.
15956 */
15957
15958 #define OACC_WAIT_CLAUSE_MASK \
15959 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) )
15960
15961 static tree
15962 c_parser_oacc_wait (location_t loc, c_parser *parser, char *p_name)
15963 {
15964 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
15965
15966 if (c_parser_peek_token (parser)->type == CPP_OPEN_PAREN)
15967 list = c_parser_oacc_wait_list (parser, loc, list);
15968
15969 strcpy (p_name, " wait");
15970 clauses = c_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK, p_name);
15971 stmt = c_finish_oacc_wait (loc, list, clauses);
15972 add_stmt (stmt);
15973
15974 return stmt;
15975 }
15976
15977 /* OpenMP 2.5:
15978 # pragma omp atomic new-line
15979 expression-stmt
15980
15981 expression-stmt:
15982 x binop= expr | x++ | ++x | x-- | --x
15983 binop:
15984 +, *, -, /, &, ^, |, <<, >>
15985
15986 where x is an lvalue expression with scalar type.
15987
15988 OpenMP 3.1:
15989 # pragma omp atomic new-line
15990 update-stmt
15991
15992 # pragma omp atomic read new-line
15993 read-stmt
15994
15995 # pragma omp atomic write new-line
15996 write-stmt
15997
15998 # pragma omp atomic update new-line
15999 update-stmt
16000
16001 # pragma omp atomic capture new-line
16002 capture-stmt
16003
16004 # pragma omp atomic capture new-line
16005 capture-block
16006
16007 read-stmt:
16008 v = x
16009 write-stmt:
16010 x = expr
16011 update-stmt:
16012 expression-stmt | x = x binop expr
16013 capture-stmt:
16014 v = expression-stmt
16015 capture-block:
16016 { v = x; update-stmt; } | { update-stmt; v = x; }
16017
16018 OpenMP 4.0:
16019 update-stmt:
16020 expression-stmt | x = x binop expr | x = expr binop x
16021 capture-stmt:
16022 v = update-stmt
16023 capture-block:
16024 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
16025
16026 where x and v are lvalue expressions with scalar type.
16027
16028 LOC is the location of the #pragma token. */
16029
16030 static void
16031 c_parser_omp_atomic (location_t loc, c_parser *parser)
16032 {
16033 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE;
16034 tree lhs1 = NULL_TREE, rhs1 = NULL_TREE;
16035 tree stmt, orig_lhs, unfolded_lhs = NULL_TREE, unfolded_lhs1 = NULL_TREE;
16036 enum tree_code code = ERROR_MARK, opcode = NOP_EXPR;
16037 enum omp_memory_order memory_order = OMP_MEMORY_ORDER_UNSPECIFIED;
16038 struct c_expr expr;
16039 location_t eloc;
16040 bool structured_block = false;
16041 bool swapped = false;
16042 bool non_lvalue_p;
16043 bool first = true;
16044 tree clauses = NULL_TREE;
16045
16046 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
16047 {
16048 if (!first && c_parser_next_token_is (parser, CPP_COMMA))
16049 c_parser_consume_token (parser);
16050
16051 first = false;
16052
16053 if (c_parser_next_token_is (parser, CPP_NAME))
16054 {
16055 const char *p
16056 = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16057 location_t cloc = c_parser_peek_token (parser)->location;
16058 enum tree_code new_code = ERROR_MARK;
16059 enum omp_memory_order new_memory_order
16060 = OMP_MEMORY_ORDER_UNSPECIFIED;
16061
16062 if (!strcmp (p, "read"))
16063 new_code = OMP_ATOMIC_READ;
16064 else if (!strcmp (p, "write"))
16065 new_code = NOP_EXPR;
16066 else if (!strcmp (p, "update"))
16067 new_code = OMP_ATOMIC;
16068 else if (!strcmp (p, "capture"))
16069 new_code = OMP_ATOMIC_CAPTURE_NEW;
16070 else if (!strcmp (p, "seq_cst"))
16071 new_memory_order = OMP_MEMORY_ORDER_SEQ_CST;
16072 else if (!strcmp (p, "acq_rel"))
16073 new_memory_order = OMP_MEMORY_ORDER_ACQ_REL;
16074 else if (!strcmp (p, "release"))
16075 new_memory_order = OMP_MEMORY_ORDER_RELEASE;
16076 else if (!strcmp (p, "acquire"))
16077 new_memory_order = OMP_MEMORY_ORDER_ACQUIRE;
16078 else if (!strcmp (p, "relaxed"))
16079 new_memory_order = OMP_MEMORY_ORDER_RELAXED;
16080 else if (!strcmp (p, "hint"))
16081 {
16082 c_parser_consume_token (parser);
16083 clauses = c_parser_omp_clause_hint (parser, clauses);
16084 continue;
16085 }
16086 else
16087 {
16088 p = NULL;
16089 error_at (cloc, "expected %<read%>, %<write%>, %<update%>, "
16090 "%<capture%>, %<seq_cst%>, %<acq_rel%>, "
16091 "%<release%>, %<relaxed%> or %<hint%> clause");
16092 }
16093 if (p)
16094 {
16095 if (new_code != ERROR_MARK)
16096 {
16097 if (code != ERROR_MARK)
16098 error_at (cloc, "too many atomic clauses");
16099 else
16100 code = new_code;
16101 }
16102 else if (new_memory_order != OMP_MEMORY_ORDER_UNSPECIFIED)
16103 {
16104 if (memory_order != OMP_MEMORY_ORDER_UNSPECIFIED)
16105 error_at (cloc, "too many memory order clauses");
16106 else
16107 memory_order = new_memory_order;
16108 }
16109 c_parser_consume_token (parser);
16110 continue;
16111 }
16112 }
16113 break;
16114 }
16115 c_parser_skip_to_pragma_eol (parser);
16116
16117 if (code == ERROR_MARK)
16118 code = OMP_ATOMIC;
16119 if (memory_order == OMP_MEMORY_ORDER_UNSPECIFIED)
16120 {
16121 omp_requires_mask
16122 = (enum omp_requires) (omp_requires_mask
16123 | OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER_USED);
16124 switch ((enum omp_memory_order)
16125 (omp_requires_mask & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER))
16126 {
16127 case OMP_MEMORY_ORDER_UNSPECIFIED:
16128 case OMP_MEMORY_ORDER_RELAXED:
16129 memory_order = OMP_MEMORY_ORDER_RELAXED;
16130 break;
16131 case OMP_MEMORY_ORDER_SEQ_CST:
16132 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
16133 break;
16134 case OMP_MEMORY_ORDER_ACQ_REL:
16135 switch (code)
16136 {
16137 case OMP_ATOMIC_READ:
16138 memory_order = OMP_MEMORY_ORDER_ACQUIRE;
16139 break;
16140 case NOP_EXPR: /* atomic write */
16141 case OMP_ATOMIC:
16142 memory_order = OMP_MEMORY_ORDER_RELEASE;
16143 break;
16144 default:
16145 memory_order = OMP_MEMORY_ORDER_ACQ_REL;
16146 break;
16147 }
16148 break;
16149 default:
16150 gcc_unreachable ();
16151 }
16152 }
16153 else
16154 switch (code)
16155 {
16156 case OMP_ATOMIC_READ:
16157 if (memory_order == OMP_MEMORY_ORDER_ACQ_REL
16158 || memory_order == OMP_MEMORY_ORDER_RELEASE)
16159 {
16160 error_at (loc, "%<#pragma omp atomic read%> incompatible with "
16161 "%<acq_rel%> or %<release%> clauses");
16162 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
16163 }
16164 break;
16165 case NOP_EXPR: /* atomic write */
16166 if (memory_order == OMP_MEMORY_ORDER_ACQ_REL
16167 || memory_order == OMP_MEMORY_ORDER_ACQUIRE)
16168 {
16169 error_at (loc, "%<#pragma omp atomic write%> incompatible with "
16170 "%<acq_rel%> or %<acquire%> clauses");
16171 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
16172 }
16173 break;
16174 case OMP_ATOMIC:
16175 if (memory_order == OMP_MEMORY_ORDER_ACQ_REL
16176 || memory_order == OMP_MEMORY_ORDER_ACQUIRE)
16177 {
16178 error_at (loc, "%<#pragma omp atomic update%> incompatible with "
16179 "%<acq_rel%> or %<acquire%> clauses");
16180 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
16181 }
16182 break;
16183 default:
16184 break;
16185 }
16186
16187 switch (code)
16188 {
16189 case OMP_ATOMIC_READ:
16190 case NOP_EXPR: /* atomic write */
16191 v = c_parser_cast_expression (parser, NULL).value;
16192 non_lvalue_p = !lvalue_p (v);
16193 v = c_fully_fold (v, false, NULL, true);
16194 if (v == error_mark_node)
16195 goto saw_error;
16196 if (non_lvalue_p)
16197 v = non_lvalue (v);
16198 loc = c_parser_peek_token (parser)->location;
16199 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
16200 goto saw_error;
16201 if (code == NOP_EXPR)
16202 {
16203 lhs = c_parser_expression (parser).value;
16204 lhs = c_fully_fold (lhs, false, NULL);
16205 if (lhs == error_mark_node)
16206 goto saw_error;
16207 }
16208 else
16209 {
16210 lhs = c_parser_cast_expression (parser, NULL).value;
16211 non_lvalue_p = !lvalue_p (lhs);
16212 lhs = c_fully_fold (lhs, false, NULL, true);
16213 if (lhs == error_mark_node)
16214 goto saw_error;
16215 if (non_lvalue_p)
16216 lhs = non_lvalue (lhs);
16217 }
16218 if (code == NOP_EXPR)
16219 {
16220 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
16221 opcode. */
16222 code = OMP_ATOMIC;
16223 rhs = lhs;
16224 lhs = v;
16225 v = NULL_TREE;
16226 }
16227 goto done;
16228 case OMP_ATOMIC_CAPTURE_NEW:
16229 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
16230 {
16231 c_parser_consume_token (parser);
16232 structured_block = true;
16233 }
16234 else
16235 {
16236 v = c_parser_cast_expression (parser, NULL).value;
16237 non_lvalue_p = !lvalue_p (v);
16238 v = c_fully_fold (v, false, NULL, true);
16239 if (v == error_mark_node)
16240 goto saw_error;
16241 if (non_lvalue_p)
16242 v = non_lvalue (v);
16243 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
16244 goto saw_error;
16245 }
16246 break;
16247 default:
16248 break;
16249 }
16250
16251 /* For structured_block case we don't know yet whether
16252 old or new x should be captured. */
16253 restart:
16254 eloc = c_parser_peek_token (parser)->location;
16255 expr = c_parser_cast_expression (parser, NULL);
16256 lhs = expr.value;
16257 expr = default_function_array_conversion (eloc, expr);
16258 unfolded_lhs = expr.value;
16259 lhs = c_fully_fold (lhs, false, NULL, true);
16260 orig_lhs = lhs;
16261 switch (TREE_CODE (lhs))
16262 {
16263 case ERROR_MARK:
16264 saw_error:
16265 c_parser_skip_to_end_of_block_or_statement (parser);
16266 if (structured_block)
16267 {
16268 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
16269 c_parser_consume_token (parser);
16270 else if (code == OMP_ATOMIC_CAPTURE_NEW)
16271 {
16272 c_parser_skip_to_end_of_block_or_statement (parser);
16273 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
16274 c_parser_consume_token (parser);
16275 }
16276 }
16277 return;
16278
16279 case POSTINCREMENT_EXPR:
16280 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
16281 code = OMP_ATOMIC_CAPTURE_OLD;
16282 /* FALLTHROUGH */
16283 case PREINCREMENT_EXPR:
16284 lhs = TREE_OPERAND (lhs, 0);
16285 unfolded_lhs = NULL_TREE;
16286 opcode = PLUS_EXPR;
16287 rhs = integer_one_node;
16288 break;
16289
16290 case POSTDECREMENT_EXPR:
16291 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
16292 code = OMP_ATOMIC_CAPTURE_OLD;
16293 /* FALLTHROUGH */
16294 case PREDECREMENT_EXPR:
16295 lhs = TREE_OPERAND (lhs, 0);
16296 unfolded_lhs = NULL_TREE;
16297 opcode = MINUS_EXPR;
16298 rhs = integer_one_node;
16299 break;
16300
16301 case COMPOUND_EXPR:
16302 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
16303 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
16304 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
16305 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
16306 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
16307 (TREE_OPERAND (lhs, 1), 0), 0)))
16308 == BOOLEAN_TYPE)
16309 /* Undo effects of boolean_increment for post {in,de}crement. */
16310 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
16311 /* FALLTHRU */
16312 case MODIFY_EXPR:
16313 if (TREE_CODE (lhs) == MODIFY_EXPR
16314 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
16315 {
16316 /* Undo effects of boolean_increment. */
16317 if (integer_onep (TREE_OPERAND (lhs, 1)))
16318 {
16319 /* This is pre or post increment. */
16320 rhs = TREE_OPERAND (lhs, 1);
16321 lhs = TREE_OPERAND (lhs, 0);
16322 unfolded_lhs = NULL_TREE;
16323 opcode = NOP_EXPR;
16324 if (code == OMP_ATOMIC_CAPTURE_NEW
16325 && !structured_block
16326 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
16327 code = OMP_ATOMIC_CAPTURE_OLD;
16328 break;
16329 }
16330 if (TREE_CODE (TREE_OPERAND (lhs, 1)) == TRUTH_NOT_EXPR
16331 && TREE_OPERAND (lhs, 0)
16332 == TREE_OPERAND (TREE_OPERAND (lhs, 1), 0))
16333 {
16334 /* This is pre or post decrement. */
16335 rhs = TREE_OPERAND (lhs, 1);
16336 lhs = TREE_OPERAND (lhs, 0);
16337 unfolded_lhs = NULL_TREE;
16338 opcode = NOP_EXPR;
16339 if (code == OMP_ATOMIC_CAPTURE_NEW
16340 && !structured_block
16341 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
16342 code = OMP_ATOMIC_CAPTURE_OLD;
16343 break;
16344 }
16345 }
16346 /* FALLTHRU */
16347 default:
16348 if (!lvalue_p (unfolded_lhs))
16349 lhs = non_lvalue (lhs);
16350 switch (c_parser_peek_token (parser)->type)
16351 {
16352 case CPP_MULT_EQ:
16353 opcode = MULT_EXPR;
16354 break;
16355 case CPP_DIV_EQ:
16356 opcode = TRUNC_DIV_EXPR;
16357 break;
16358 case CPP_PLUS_EQ:
16359 opcode = PLUS_EXPR;
16360 break;
16361 case CPP_MINUS_EQ:
16362 opcode = MINUS_EXPR;
16363 break;
16364 case CPP_LSHIFT_EQ:
16365 opcode = LSHIFT_EXPR;
16366 break;
16367 case CPP_RSHIFT_EQ:
16368 opcode = RSHIFT_EXPR;
16369 break;
16370 case CPP_AND_EQ:
16371 opcode = BIT_AND_EXPR;
16372 break;
16373 case CPP_OR_EQ:
16374 opcode = BIT_IOR_EXPR;
16375 break;
16376 case CPP_XOR_EQ:
16377 opcode = BIT_XOR_EXPR;
16378 break;
16379 case CPP_EQ:
16380 c_parser_consume_token (parser);
16381 eloc = c_parser_peek_token (parser)->location;
16382 expr = c_parser_expr_no_commas (parser, NULL, unfolded_lhs);
16383 rhs1 = expr.value;
16384 switch (TREE_CODE (rhs1))
16385 {
16386 case MULT_EXPR:
16387 case TRUNC_DIV_EXPR:
16388 case RDIV_EXPR:
16389 case PLUS_EXPR:
16390 case MINUS_EXPR:
16391 case LSHIFT_EXPR:
16392 case RSHIFT_EXPR:
16393 case BIT_AND_EXPR:
16394 case BIT_IOR_EXPR:
16395 case BIT_XOR_EXPR:
16396 if (c_tree_equal (TREE_OPERAND (rhs1, 0), unfolded_lhs))
16397 {
16398 opcode = TREE_CODE (rhs1);
16399 rhs = c_fully_fold (TREE_OPERAND (rhs1, 1), false, NULL,
16400 true);
16401 rhs1 = c_fully_fold (TREE_OPERAND (rhs1, 0), false, NULL,
16402 true);
16403 goto stmt_done;
16404 }
16405 if (c_tree_equal (TREE_OPERAND (rhs1, 1), unfolded_lhs))
16406 {
16407 opcode = TREE_CODE (rhs1);
16408 rhs = c_fully_fold (TREE_OPERAND (rhs1, 0), false, NULL,
16409 true);
16410 rhs1 = c_fully_fold (TREE_OPERAND (rhs1, 1), false, NULL,
16411 true);
16412 swapped = !commutative_tree_code (opcode);
16413 goto stmt_done;
16414 }
16415 break;
16416 case ERROR_MARK:
16417 goto saw_error;
16418 default:
16419 break;
16420 }
16421 if (c_parser_peek_token (parser)->type == CPP_SEMICOLON)
16422 {
16423 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
16424 {
16425 code = OMP_ATOMIC_CAPTURE_OLD;
16426 v = lhs;
16427 lhs = NULL_TREE;
16428 expr = default_function_array_read_conversion (eloc, expr);
16429 unfolded_lhs1 = expr.value;
16430 lhs1 = c_fully_fold (unfolded_lhs1, false, NULL, true);
16431 rhs1 = NULL_TREE;
16432 c_parser_consume_token (parser);
16433 goto restart;
16434 }
16435 if (structured_block)
16436 {
16437 opcode = NOP_EXPR;
16438 expr = default_function_array_read_conversion (eloc, expr);
16439 rhs = c_fully_fold (expr.value, false, NULL, true);
16440 rhs1 = NULL_TREE;
16441 goto stmt_done;
16442 }
16443 }
16444 c_parser_error (parser, "invalid form of %<#pragma omp atomic%>");
16445 goto saw_error;
16446 default:
16447 c_parser_error (parser,
16448 "invalid operator for %<#pragma omp atomic%>");
16449 goto saw_error;
16450 }
16451
16452 /* Arrange to pass the location of the assignment operator to
16453 c_finish_omp_atomic. */
16454 loc = c_parser_peek_token (parser)->location;
16455 c_parser_consume_token (parser);
16456 eloc = c_parser_peek_token (parser)->location;
16457 expr = c_parser_expression (parser);
16458 expr = default_function_array_read_conversion (eloc, expr);
16459 rhs = expr.value;
16460 rhs = c_fully_fold (rhs, false, NULL, true);
16461 break;
16462 }
16463 stmt_done:
16464 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
16465 {
16466 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
16467 goto saw_error;
16468 v = c_parser_cast_expression (parser, NULL).value;
16469 non_lvalue_p = !lvalue_p (v);
16470 v = c_fully_fold (v, false, NULL, true);
16471 if (v == error_mark_node)
16472 goto saw_error;
16473 if (non_lvalue_p)
16474 v = non_lvalue (v);
16475 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
16476 goto saw_error;
16477 eloc = c_parser_peek_token (parser)->location;
16478 expr = c_parser_cast_expression (parser, NULL);
16479 lhs1 = expr.value;
16480 expr = default_function_array_read_conversion (eloc, expr);
16481 unfolded_lhs1 = expr.value;
16482 lhs1 = c_fully_fold (lhs1, false, NULL, true);
16483 if (lhs1 == error_mark_node)
16484 goto saw_error;
16485 if (!lvalue_p (unfolded_lhs1))
16486 lhs1 = non_lvalue (lhs1);
16487 }
16488 if (structured_block)
16489 {
16490 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
16491 c_parser_require (parser, CPP_CLOSE_BRACE, "expected %<}%>");
16492 }
16493 done:
16494 if (unfolded_lhs && unfolded_lhs1
16495 && !c_tree_equal (unfolded_lhs, unfolded_lhs1))
16496 {
16497 error ("%<#pragma omp atomic capture%> uses two different "
16498 "expressions for memory");
16499 stmt = error_mark_node;
16500 }
16501 else
16502 stmt = c_finish_omp_atomic (loc, code, opcode, lhs, rhs, v, lhs1, rhs1,
16503 swapped, memory_order);
16504 if (stmt != error_mark_node)
16505 add_stmt (stmt);
16506
16507 if (!structured_block)
16508 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
16509 }
16510
16511
16512 /* OpenMP 2.5:
16513 # pragma omp barrier new-line
16514 */
16515
16516 static void
16517 c_parser_omp_barrier (c_parser *parser)
16518 {
16519 location_t loc = c_parser_peek_token (parser)->location;
16520 c_parser_consume_pragma (parser);
16521 c_parser_skip_to_pragma_eol (parser);
16522
16523 c_finish_omp_barrier (loc);
16524 }
16525
16526 /* OpenMP 2.5:
16527 # pragma omp critical [(name)] new-line
16528 structured-block
16529
16530 OpenMP 4.5:
16531 # pragma omp critical [(name) [hint(expression)]] new-line
16532
16533 LOC is the location of the #pragma itself. */
16534
16535 #define OMP_CRITICAL_CLAUSE_MASK \
16536 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
16537
16538 static tree
16539 c_parser_omp_critical (location_t loc, c_parser *parser, bool *if_p)
16540 {
16541 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
16542
16543 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
16544 {
16545 c_parser_consume_token (parser);
16546 if (c_parser_next_token_is (parser, CPP_NAME))
16547 {
16548 name = c_parser_peek_token (parser)->value;
16549 c_parser_consume_token (parser);
16550 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
16551 }
16552 else
16553 c_parser_error (parser, "expected identifier");
16554
16555 if (c_parser_next_token_is (parser, CPP_COMMA)
16556 && c_parser_peek_2nd_token (parser)->type == CPP_NAME)
16557 c_parser_consume_token (parser);
16558
16559 clauses = c_parser_omp_all_clauses (parser,
16560 OMP_CRITICAL_CLAUSE_MASK,
16561 "#pragma omp critical");
16562 }
16563 else
16564 {
16565 if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
16566 c_parser_error (parser, "expected %<(%> or end of line");
16567 c_parser_skip_to_pragma_eol (parser);
16568 }
16569
16570 stmt = c_parser_omp_structured_block (parser, if_p);
16571 return c_finish_omp_critical (loc, stmt, name, clauses);
16572 }
16573
16574 /* OpenMP 5.0:
16575 # pragma omp depobj ( depobj ) depobj-clause new-line
16576
16577 depobj-clause:
16578 depend (dependence-type : locator)
16579 destroy
16580 update (dependence-type)
16581
16582 dependence-type:
16583 in
16584 out
16585 inout
16586 mutexinout */
16587
16588 static void
16589 c_parser_omp_depobj (c_parser *parser)
16590 {
16591 location_t loc = c_parser_peek_token (parser)->location;
16592 c_parser_consume_pragma (parser);
16593 matching_parens parens;
16594 if (!parens.require_open (parser))
16595 {
16596 c_parser_skip_to_pragma_eol (parser);
16597 return;
16598 }
16599
16600 tree depobj = c_parser_expr_no_commas (parser, NULL).value;
16601 if (depobj != error_mark_node)
16602 {
16603 if (!lvalue_p (depobj))
16604 {
16605 error_at (EXPR_LOC_OR_LOC (depobj, loc),
16606 "%<depobj%> expression is not lvalue expression");
16607 depobj = error_mark_node;
16608 }
16609 else
16610 {
16611 tree addr = build_unary_op (EXPR_LOC_OR_LOC (depobj, loc), ADDR_EXPR,
16612 depobj, false);
16613 if (addr == error_mark_node)
16614 depobj = error_mark_node;
16615 else
16616 depobj = build_indirect_ref (EXPR_LOC_OR_LOC (depobj, loc),
16617 addr, RO_UNARY_STAR);
16618 }
16619 }
16620
16621 parens.skip_until_found_close (parser);
16622 tree clause = NULL_TREE;
16623 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_SOURCE;
16624 location_t c_loc = c_parser_peek_token (parser)->location;
16625 if (c_parser_next_token_is (parser, CPP_NAME))
16626 {
16627 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16628
16629 c_parser_consume_token (parser);
16630 if (!strcmp ("depend", p))
16631 {
16632 clause = c_parser_omp_clause_depend (parser, NULL_TREE);
16633 clause = c_finish_omp_clauses (clause, C_ORT_OMP);
16634 if (!clause)
16635 clause = error_mark_node;
16636 }
16637 else if (!strcmp ("destroy", p))
16638 kind = OMP_CLAUSE_DEPEND_LAST;
16639 else if (!strcmp ("update", p))
16640 {
16641 matching_parens c_parens;
16642 if (c_parens.require_open (parser))
16643 {
16644 location_t c2_loc = c_parser_peek_token (parser)->location;
16645 if (c_parser_next_token_is (parser, CPP_NAME))
16646 {
16647 const char *p2
16648 = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16649
16650 c_parser_consume_token (parser);
16651 if (!strcmp ("in", p2))
16652 kind = OMP_CLAUSE_DEPEND_IN;
16653 else if (!strcmp ("out", p2))
16654 kind = OMP_CLAUSE_DEPEND_OUT;
16655 else if (!strcmp ("inout", p2))
16656 kind = OMP_CLAUSE_DEPEND_INOUT;
16657 else if (!strcmp ("mutexinoutset", p2))
16658 kind = OMP_CLAUSE_DEPEND_MUTEXINOUTSET;
16659 }
16660 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
16661 {
16662 clause = error_mark_node;
16663 error_at (c2_loc, "expected %<in%>, %<out%>, %<inout%> or "
16664 "%<mutexinoutset%>");
16665 }
16666 c_parens.skip_until_found_close (parser);
16667 }
16668 else
16669 clause = error_mark_node;
16670 }
16671 }
16672 if (!clause && kind == OMP_CLAUSE_DEPEND_SOURCE)
16673 {
16674 clause = error_mark_node;
16675 error_at (c_loc, "expected %<depend%>, %<destroy%> or %<update%> clause");
16676 }
16677 c_parser_skip_to_pragma_eol (parser);
16678
16679 c_finish_omp_depobj (loc, depobj, kind, clause);
16680 }
16681
16682
16683 /* OpenMP 2.5:
16684 # pragma omp flush flush-vars[opt] new-line
16685
16686 flush-vars:
16687 ( variable-list )
16688
16689 OpenMP 5.0:
16690 # pragma omp flush memory-order-clause new-line */
16691
16692 static void
16693 c_parser_omp_flush (c_parser *parser)
16694 {
16695 location_t loc = c_parser_peek_token (parser)->location;
16696 c_parser_consume_pragma (parser);
16697 enum memmodel mo = MEMMODEL_LAST;
16698 if (c_parser_next_token_is (parser, CPP_NAME))
16699 {
16700 const char *p
16701 = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16702
16703 if (!strcmp (p, "acq_rel"))
16704 mo = MEMMODEL_ACQ_REL;
16705 else if (!strcmp (p, "release"))
16706 mo = MEMMODEL_RELEASE;
16707 else if (!strcmp (p, "acquire"))
16708 mo = MEMMODEL_ACQUIRE;
16709 else
16710 error_at (c_parser_peek_token (parser)->location,
16711 "expected %<acq_rel%>, %<release%> or %<acquire%>");
16712 c_parser_consume_token (parser);
16713 }
16714 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
16715 {
16716 if (mo != MEMMODEL_LAST)
16717 error_at (c_parser_peek_token (parser)->location,
16718 "%<flush%> list specified together with memory order "
16719 "clause");
16720 c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
16721 }
16722 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
16723 c_parser_error (parser, "expected %<(%> or end of line");
16724 c_parser_skip_to_pragma_eol (parser);
16725
16726 c_finish_omp_flush (loc, mo);
16727 }
16728
16729 /* Parse the restricted form of loop statements allowed by OpenACC and OpenMP.
16730 The real trick here is to determine the loop control variable early
16731 so that we can push a new decl if necessary to make it private.
16732 LOC is the location of the "acc" or "omp" in "#pragma acc" or "#pragma omp",
16733 respectively. */
16734
16735 static tree
16736 c_parser_omp_for_loop (location_t loc, c_parser *parser, enum tree_code code,
16737 tree clauses, tree *cclauses, bool *if_p)
16738 {
16739 tree decl, cond, incr, save_break, save_cont, body, init, stmt, cl;
16740 tree declv, condv, incrv, initv, ret = NULL_TREE;
16741 tree pre_body = NULL_TREE, this_pre_body;
16742 tree ordered_cl = NULL_TREE;
16743 bool fail = false, open_brace_parsed = false;
16744 int i, collapse = 1, ordered = 0, count, nbraces = 0;
16745 location_t for_loc;
16746 bool tiling = false;
16747 vec<tree, va_gc> *for_block = make_tree_vector ();
16748
16749 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
16750 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
16751 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
16752 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_TILE)
16753 {
16754 tiling = true;
16755 collapse = list_length (OMP_CLAUSE_TILE_LIST (cl));
16756 }
16757 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
16758 && OMP_CLAUSE_ORDERED_EXPR (cl))
16759 {
16760 ordered_cl = cl;
16761 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
16762 }
16763
16764 if (ordered && ordered < collapse)
16765 {
16766 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
16767 "%<ordered%> clause parameter is less than %<collapse%>");
16768 OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
16769 = build_int_cst (NULL_TREE, collapse);
16770 ordered = collapse;
16771 }
16772 if (ordered)
16773 {
16774 for (tree *pc = &clauses; *pc; )
16775 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
16776 {
16777 error_at (OMP_CLAUSE_LOCATION (*pc),
16778 "%<linear%> clause may not be specified together "
16779 "with %<ordered%> clause with a parameter");
16780 *pc = OMP_CLAUSE_CHAIN (*pc);
16781 }
16782 else
16783 pc = &OMP_CLAUSE_CHAIN (*pc);
16784 }
16785
16786 gcc_assert (tiling || (collapse >= 1 && ordered >= 0));
16787 count = ordered ? ordered : collapse;
16788
16789 declv = make_tree_vec (count);
16790 initv = make_tree_vec (count);
16791 condv = make_tree_vec (count);
16792 incrv = make_tree_vec (count);
16793
16794 if (!c_parser_next_token_is_keyword (parser, RID_FOR))
16795 {
16796 c_parser_error (parser, "for statement expected");
16797 return NULL;
16798 }
16799 for_loc = c_parser_peek_token (parser)->location;
16800 c_parser_consume_token (parser);
16801
16802 for (i = 0; i < count; i++)
16803 {
16804 int bracecount = 0;
16805
16806 matching_parens parens;
16807 if (!parens.require_open (parser))
16808 goto pop_scopes;
16809
16810 /* Parse the initialization declaration or expression. */
16811 if (c_parser_next_tokens_start_declaration (parser))
16812 {
16813 if (i > 0)
16814 vec_safe_push (for_block, c_begin_compound_stmt (true));
16815 this_pre_body = push_stmt_list ();
16816 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
16817 NULL, vNULL);
16818 if (this_pre_body)
16819 {
16820 this_pre_body = pop_stmt_list (this_pre_body);
16821 if (pre_body)
16822 {
16823 tree t = pre_body;
16824 pre_body = push_stmt_list ();
16825 add_stmt (t);
16826 add_stmt (this_pre_body);
16827 pre_body = pop_stmt_list (pre_body);
16828 }
16829 else
16830 pre_body = this_pre_body;
16831 }
16832 decl = check_for_loop_decls (for_loc, flag_isoc99);
16833 if (decl == NULL)
16834 goto error_init;
16835 if (DECL_INITIAL (decl) == error_mark_node)
16836 decl = error_mark_node;
16837 init = decl;
16838 }
16839 else if (c_parser_next_token_is (parser, CPP_NAME)
16840 && c_parser_peek_2nd_token (parser)->type == CPP_EQ)
16841 {
16842 struct c_expr decl_exp;
16843 struct c_expr init_exp;
16844 location_t init_loc;
16845
16846 decl_exp = c_parser_postfix_expression (parser);
16847 decl = decl_exp.value;
16848
16849 c_parser_require (parser, CPP_EQ, "expected %<=%>");
16850
16851 init_loc = c_parser_peek_token (parser)->location;
16852 init_exp = c_parser_expr_no_commas (parser, NULL);
16853 init_exp = default_function_array_read_conversion (init_loc,
16854 init_exp);
16855 init = build_modify_expr (init_loc, decl, decl_exp.original_type,
16856 NOP_EXPR, init_loc, init_exp.value,
16857 init_exp.original_type);
16858 init = c_process_expr_stmt (init_loc, init);
16859
16860 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
16861 }
16862 else
16863 {
16864 error_init:
16865 c_parser_error (parser,
16866 "expected iteration declaration or initialization");
16867 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
16868 "expected %<)%>");
16869 fail = true;
16870 goto parse_next;
16871 }
16872
16873 /* Parse the loop condition. */
16874 cond = NULL_TREE;
16875 if (c_parser_next_token_is_not (parser, CPP_SEMICOLON))
16876 {
16877 location_t cond_loc = c_parser_peek_token (parser)->location;
16878 struct c_expr cond_expr
16879 = c_parser_binary_expression (parser, NULL, NULL_TREE);
16880
16881 cond = cond_expr.value;
16882 cond = c_objc_common_truthvalue_conversion (cond_loc, cond);
16883 if (COMPARISON_CLASS_P (cond))
16884 {
16885 tree op0 = TREE_OPERAND (cond, 0), op1 = TREE_OPERAND (cond, 1);
16886 op0 = c_fully_fold (op0, false, NULL);
16887 op1 = c_fully_fold (op1, false, NULL);
16888 TREE_OPERAND (cond, 0) = op0;
16889 TREE_OPERAND (cond, 1) = op1;
16890 }
16891 switch (cond_expr.original_code)
16892 {
16893 case GT_EXPR:
16894 case GE_EXPR:
16895 case LT_EXPR:
16896 case LE_EXPR:
16897 break;
16898 case NE_EXPR:
16899 if (code != OACC_LOOP)
16900 break;
16901 /* FALLTHRU. */
16902 default:
16903 /* Can't be cond = error_mark_node, because we want to preserve
16904 the location until c_finish_omp_for. */
16905 cond = build1 (NOP_EXPR, boolean_type_node, error_mark_node);
16906 break;
16907 }
16908 protected_set_expr_location (cond, cond_loc);
16909 }
16910 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
16911
16912 /* Parse the increment expression. */
16913 incr = NULL_TREE;
16914 if (c_parser_next_token_is_not (parser, CPP_CLOSE_PAREN))
16915 {
16916 location_t incr_loc = c_parser_peek_token (parser)->location;
16917
16918 incr = c_process_expr_stmt (incr_loc,
16919 c_parser_expression (parser).value);
16920 }
16921 parens.skip_until_found_close (parser);
16922
16923 if (decl == NULL || decl == error_mark_node || init == error_mark_node)
16924 fail = true;
16925 else
16926 {
16927 TREE_VEC_ELT (declv, i) = decl;
16928 TREE_VEC_ELT (initv, i) = init;
16929 TREE_VEC_ELT (condv, i) = cond;
16930 TREE_VEC_ELT (incrv, i) = incr;
16931 }
16932
16933 parse_next:
16934 if (i == count - 1)
16935 break;
16936
16937 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
16938 in between the collapsed for loops to be still considered perfectly
16939 nested. Hopefully the final version clarifies this.
16940 For now handle (multiple) {'s and empty statements. */
16941 do
16942 {
16943 if (c_parser_next_token_is_keyword (parser, RID_FOR))
16944 {
16945 c_parser_consume_token (parser);
16946 break;
16947 }
16948 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
16949 {
16950 c_parser_consume_token (parser);
16951 bracecount++;
16952 }
16953 else if (bracecount
16954 && c_parser_next_token_is (parser, CPP_SEMICOLON))
16955 c_parser_consume_token (parser);
16956 else
16957 {
16958 c_parser_error (parser, "not enough perfectly nested loops");
16959 if (bracecount)
16960 {
16961 open_brace_parsed = true;
16962 bracecount--;
16963 }
16964 fail = true;
16965 count = 0;
16966 break;
16967 }
16968 }
16969 while (1);
16970
16971 nbraces += bracecount;
16972 }
16973
16974 if (nbraces)
16975 if_p = NULL;
16976
16977 save_break = c_break_label;
16978 c_break_label = size_one_node;
16979 save_cont = c_cont_label;
16980 c_cont_label = NULL_TREE;
16981 body = push_stmt_list ();
16982
16983 if (open_brace_parsed)
16984 {
16985 location_t here = c_parser_peek_token (parser)->location;
16986 stmt = c_begin_compound_stmt (true);
16987 c_parser_compound_statement_nostart (parser);
16988 add_stmt (c_end_compound_stmt (here, stmt, true));
16989 }
16990 else
16991 add_stmt (c_parser_c99_block_statement (parser, if_p));
16992 if (c_cont_label)
16993 {
16994 tree t = build1 (LABEL_EXPR, void_type_node, c_cont_label);
16995 SET_EXPR_LOCATION (t, loc);
16996 add_stmt (t);
16997 }
16998
16999 body = pop_stmt_list (body);
17000 c_break_label = save_break;
17001 c_cont_label = save_cont;
17002
17003 while (nbraces)
17004 {
17005 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
17006 {
17007 c_parser_consume_token (parser);
17008 nbraces--;
17009 }
17010 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
17011 c_parser_consume_token (parser);
17012 else
17013 {
17014 c_parser_error (parser, "collapsed loops not perfectly nested");
17015 while (nbraces)
17016 {
17017 location_t here = c_parser_peek_token (parser)->location;
17018 stmt = c_begin_compound_stmt (true);
17019 add_stmt (body);
17020 c_parser_compound_statement_nostart (parser);
17021 body = c_end_compound_stmt (here, stmt, true);
17022 nbraces--;
17023 }
17024 goto pop_scopes;
17025 }
17026 }
17027
17028 /* Only bother calling c_finish_omp_for if we haven't already generated
17029 an error from the initialization parsing. */
17030 if (!fail)
17031 {
17032 stmt = c_finish_omp_for (loc, code, declv, NULL, initv, condv,
17033 incrv, body, pre_body, true);
17034
17035 /* Check for iterators appearing in lb, b or incr expressions. */
17036 if (stmt && !c_omp_check_loop_iv (stmt, declv, NULL))
17037 stmt = NULL_TREE;
17038
17039 if (stmt)
17040 {
17041 add_stmt (stmt);
17042
17043 if (cclauses != NULL
17044 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL)
17045 {
17046 tree *c;
17047 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
17048 if (OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_FIRSTPRIVATE
17049 && OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_LASTPRIVATE)
17050 c = &OMP_CLAUSE_CHAIN (*c);
17051 else
17052 {
17053 for (i = 0; i < count; i++)
17054 if (TREE_VEC_ELT (declv, i) == OMP_CLAUSE_DECL (*c))
17055 break;
17056 if (i == count)
17057 c = &OMP_CLAUSE_CHAIN (*c);
17058 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE)
17059 {
17060 error_at (loc,
17061 "iteration variable %qD should not be firstprivate",
17062 OMP_CLAUSE_DECL (*c));
17063 *c = OMP_CLAUSE_CHAIN (*c);
17064 }
17065 else
17066 {
17067 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
17068 tree l = *c;
17069 *c = OMP_CLAUSE_CHAIN (*c);
17070 if (code == OMP_SIMD)
17071 {
17072 OMP_CLAUSE_CHAIN (l)
17073 = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
17074 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
17075 }
17076 else
17077 {
17078 OMP_CLAUSE_CHAIN (l) = clauses;
17079 clauses = l;
17080 }
17081 }
17082 }
17083 }
17084 OMP_FOR_CLAUSES (stmt) = clauses;
17085 }
17086 ret = stmt;
17087 }
17088 pop_scopes:
17089 while (!for_block->is_empty ())
17090 {
17091 /* FIXME diagnostics: LOC below should be the actual location of
17092 this particular for block. We need to build a list of
17093 locations to go along with FOR_BLOCK. */
17094 stmt = c_end_compound_stmt (loc, for_block->pop (), true);
17095 add_stmt (stmt);
17096 }
17097 release_tree_vector (for_block);
17098 return ret;
17099 }
17100
17101 /* Helper function for OpenMP parsing, split clauses and call
17102 finish_omp_clauses on each of the set of clauses afterwards. */
17103
17104 static void
17105 omp_split_clauses (location_t loc, enum tree_code code,
17106 omp_clause_mask mask, tree clauses, tree *cclauses)
17107 {
17108 int i;
17109 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
17110 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
17111 if (cclauses[i])
17112 cclauses[i] = c_finish_omp_clauses (cclauses[i], C_ORT_OMP);
17113 }
17114
17115 /* OpenMP 4.0:
17116 #pragma omp simd simd-clause[optseq] new-line
17117 for-loop
17118
17119 LOC is the location of the #pragma token.
17120 */
17121
17122 #define OMP_SIMD_CLAUSE_MASK \
17123 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
17124 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
17125 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
17126 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
17127 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
17128 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
17129 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
17130 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
17131 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
17132 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NONTEMPORAL))
17133
17134 static tree
17135 c_parser_omp_simd (location_t loc, c_parser *parser,
17136 char *p_name, omp_clause_mask mask, tree *cclauses,
17137 bool *if_p)
17138 {
17139 tree block, clauses, ret;
17140
17141 strcat (p_name, " simd");
17142 mask |= OMP_SIMD_CLAUSE_MASK;
17143
17144 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
17145 if (cclauses)
17146 {
17147 omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
17148 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
17149 tree c = omp_find_clause (cclauses[C_OMP_CLAUSE_SPLIT_FOR],
17150 OMP_CLAUSE_ORDERED);
17151 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
17152 {
17153 error_at (OMP_CLAUSE_LOCATION (c),
17154 "%<ordered%> clause with parameter may not be specified "
17155 "on %qs construct", p_name);
17156 OMP_CLAUSE_ORDERED_EXPR (c) = NULL_TREE;
17157 }
17158 }
17159
17160 block = c_begin_compound_stmt (true);
17161 ret = c_parser_omp_for_loop (loc, parser, OMP_SIMD, clauses, cclauses, if_p);
17162 block = c_end_compound_stmt (loc, block, true);
17163 add_stmt (block);
17164
17165 return ret;
17166 }
17167
17168 /* OpenMP 2.5:
17169 #pragma omp for for-clause[optseq] new-line
17170 for-loop
17171
17172 OpenMP 4.0:
17173 #pragma omp for simd for-simd-clause[optseq] new-line
17174 for-loop
17175
17176 LOC is the location of the #pragma token.
17177 */
17178
17179 #define OMP_FOR_CLAUSE_MASK \
17180 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
17181 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
17182 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
17183 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
17184 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
17185 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
17186 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
17187 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
17188 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
17189
17190 static tree
17191 c_parser_omp_for (location_t loc, c_parser *parser,
17192 char *p_name, omp_clause_mask mask, tree *cclauses,
17193 bool *if_p)
17194 {
17195 tree block, clauses, ret;
17196
17197 strcat (p_name, " for");
17198 mask |= OMP_FOR_CLAUSE_MASK;
17199 /* parallel for{, simd} disallows nowait clause, but for
17200 target {teams distribute ,}parallel for{, simd} it should be accepted. */
17201 if (cclauses && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
17202 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
17203 /* Composite distribute parallel for{, simd} disallows ordered clause. */
17204 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
17205 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
17206
17207 if (c_parser_next_token_is (parser, CPP_NAME))
17208 {
17209 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
17210
17211 if (strcmp (p, "simd") == 0)
17212 {
17213 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
17214 if (cclauses == NULL)
17215 cclauses = cclauses_buf;
17216
17217 c_parser_consume_token (parser);
17218 if (!flag_openmp) /* flag_openmp_simd */
17219 return c_parser_omp_simd (loc, parser, p_name, mask, cclauses,
17220 if_p);
17221 block = c_begin_compound_stmt (true);
17222 ret = c_parser_omp_simd (loc, parser, p_name, mask, cclauses, if_p);
17223 block = c_end_compound_stmt (loc, block, true);
17224 if (ret == NULL_TREE)
17225 return ret;
17226 ret = make_node (OMP_FOR);
17227 TREE_TYPE (ret) = void_type_node;
17228 OMP_FOR_BODY (ret) = block;
17229 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
17230 SET_EXPR_LOCATION (ret, loc);
17231 add_stmt (ret);
17232 return ret;
17233 }
17234 }
17235 if (!flag_openmp) /* flag_openmp_simd */
17236 {
17237 c_parser_skip_to_pragma_eol (parser, false);
17238 return NULL_TREE;
17239 }
17240
17241 /* Composite distribute parallel for disallows linear clause. */
17242 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
17243 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
17244
17245 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
17246 if (cclauses)
17247 {
17248 omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
17249 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
17250 }
17251
17252 block = c_begin_compound_stmt (true);
17253 ret = c_parser_omp_for_loop (loc, parser, OMP_FOR, clauses, cclauses, if_p);
17254 block = c_end_compound_stmt (loc, block, true);
17255 add_stmt (block);
17256
17257 return ret;
17258 }
17259
17260 static tree c_parser_omp_taskloop (location_t, c_parser *, char *,
17261 omp_clause_mask, tree *, bool *);
17262
17263 /* OpenMP 2.5:
17264 # pragma omp master new-line
17265 structured-block
17266
17267 LOC is the location of the #pragma token.
17268 */
17269
17270 static tree
17271 c_parser_omp_master (location_t loc, c_parser *parser,
17272 char *p_name, omp_clause_mask mask, tree *cclauses,
17273 bool *if_p)
17274 {
17275 tree block, clauses, ret;
17276
17277 strcat (p_name, " master");
17278
17279 if (c_parser_next_token_is (parser, CPP_NAME))
17280 {
17281 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
17282
17283 if (strcmp (p, "taskloop") == 0)
17284 {
17285 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
17286 if (cclauses == NULL)
17287 cclauses = cclauses_buf;
17288
17289 c_parser_consume_token (parser);
17290 if (!flag_openmp) /* flag_openmp_simd */
17291 return c_parser_omp_taskloop (loc, parser, p_name, mask, cclauses,
17292 if_p);
17293 block = c_begin_compound_stmt (true);
17294 ret = c_parser_omp_taskloop (loc, parser, p_name, mask, cclauses,
17295 if_p);
17296 block = c_end_compound_stmt (loc, block, true);
17297 if (ret == NULL_TREE)
17298 return ret;
17299 ret = c_finish_omp_master (loc, block);
17300 return ret;
17301 }
17302 }
17303 if (!flag_openmp) /* flag_openmp_simd */
17304 {
17305 c_parser_skip_to_pragma_eol (parser, false);
17306 return NULL_TREE;
17307 }
17308
17309 if (cclauses)
17310 {
17311 clauses = c_parser_omp_all_clauses (parser, mask, p_name, false);
17312 omp_split_clauses (loc, OMP_MASTER, mask, clauses, cclauses);
17313 }
17314 else
17315 c_parser_skip_to_pragma_eol (parser);
17316
17317 return c_finish_omp_master (loc, c_parser_omp_structured_block (parser,
17318 if_p));
17319 }
17320
17321 /* OpenMP 2.5:
17322 # pragma omp ordered new-line
17323 structured-block
17324
17325 OpenMP 4.5:
17326 # pragma omp ordered ordered-clauses new-line
17327 structured-block
17328
17329 # pragma omp ordered depend-clauses new-line */
17330
17331 #define OMP_ORDERED_CLAUSE_MASK \
17332 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
17333 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
17334
17335 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
17336 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
17337
17338 static bool
17339 c_parser_omp_ordered (c_parser *parser, enum pragma_context context,
17340 bool *if_p)
17341 {
17342 location_t loc = c_parser_peek_token (parser)->location;
17343 c_parser_consume_pragma (parser);
17344
17345 if (context != pragma_stmt && context != pragma_compound)
17346 {
17347 c_parser_error (parser, "expected declaration specifiers");
17348 c_parser_skip_to_pragma_eol (parser, false);
17349 return false;
17350 }
17351
17352 if (c_parser_next_token_is (parser, CPP_NAME))
17353 {
17354 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
17355
17356 if (!strcmp ("depend", p))
17357 {
17358 if (!flag_openmp) /* flag_openmp_simd */
17359 {
17360 c_parser_skip_to_pragma_eol (parser, false);
17361 return false;
17362 }
17363 if (context == pragma_stmt)
17364 {
17365 error_at (loc,
17366 "%<#pragma omp ordered%> with %<depend%> clause may "
17367 "only be used in compound statements");
17368 c_parser_skip_to_pragma_eol (parser, false);
17369 return false;
17370 }
17371
17372 tree clauses
17373 = c_parser_omp_all_clauses (parser,
17374 OMP_ORDERED_DEPEND_CLAUSE_MASK,
17375 "#pragma omp ordered");
17376 c_finish_omp_ordered (loc, clauses, NULL_TREE);
17377 return false;
17378 }
17379 }
17380
17381 tree clauses = c_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
17382 "#pragma omp ordered");
17383
17384 if (!flag_openmp /* flag_openmp_simd */
17385 && omp_find_clause (clauses, OMP_CLAUSE_SIMD) == NULL_TREE)
17386 return false;
17387
17388 c_finish_omp_ordered (loc, clauses,
17389 c_parser_omp_structured_block (parser, if_p));
17390 return true;
17391 }
17392
17393 /* OpenMP 2.5:
17394
17395 section-scope:
17396 { section-sequence }
17397
17398 section-sequence:
17399 section-directive[opt] structured-block
17400 section-sequence section-directive structured-block
17401
17402 SECTIONS_LOC is the location of the #pragma omp sections. */
17403
17404 static tree
17405 c_parser_omp_sections_scope (location_t sections_loc, c_parser *parser)
17406 {
17407 tree stmt, substmt;
17408 bool error_suppress = false;
17409 location_t loc;
17410
17411 loc = c_parser_peek_token (parser)->location;
17412 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
17413 {
17414 /* Avoid skipping until the end of the block. */
17415 parser->error = false;
17416 return NULL_TREE;
17417 }
17418
17419 stmt = push_stmt_list ();
17420
17421 if (c_parser_peek_token (parser)->pragma_kind != PRAGMA_OMP_SECTION)
17422 {
17423 substmt = c_parser_omp_structured_block (parser, NULL);
17424 substmt = build1 (OMP_SECTION, void_type_node, substmt);
17425 SET_EXPR_LOCATION (substmt, loc);
17426 add_stmt (substmt);
17427 }
17428
17429 while (1)
17430 {
17431 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
17432 break;
17433 if (c_parser_next_token_is (parser, CPP_EOF))
17434 break;
17435
17436 loc = c_parser_peek_token (parser)->location;
17437 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_OMP_SECTION)
17438 {
17439 c_parser_consume_pragma (parser);
17440 c_parser_skip_to_pragma_eol (parser);
17441 error_suppress = false;
17442 }
17443 else if (!error_suppress)
17444 {
17445 error_at (loc, "expected %<#pragma omp section%> or %<}%>");
17446 error_suppress = true;
17447 }
17448
17449 substmt = c_parser_omp_structured_block (parser, NULL);
17450 substmt = build1 (OMP_SECTION, void_type_node, substmt);
17451 SET_EXPR_LOCATION (substmt, loc);
17452 add_stmt (substmt);
17453 }
17454 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE,
17455 "expected %<#pragma omp section%> or %<}%>");
17456
17457 substmt = pop_stmt_list (stmt);
17458
17459 stmt = make_node (OMP_SECTIONS);
17460 SET_EXPR_LOCATION (stmt, sections_loc);
17461 TREE_TYPE (stmt) = void_type_node;
17462 OMP_SECTIONS_BODY (stmt) = substmt;
17463
17464 return add_stmt (stmt);
17465 }
17466
17467 /* OpenMP 2.5:
17468 # pragma omp sections sections-clause[optseq] newline
17469 sections-scope
17470
17471 LOC is the location of the #pragma token.
17472 */
17473
17474 #define OMP_SECTIONS_CLAUSE_MASK \
17475 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
17476 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
17477 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
17478 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
17479 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
17480
17481 static tree
17482 c_parser_omp_sections (location_t loc, c_parser *parser,
17483 char *p_name, omp_clause_mask mask, tree *cclauses)
17484 {
17485 tree block, clauses, ret;
17486
17487 strcat (p_name, " sections");
17488 mask |= OMP_SECTIONS_CLAUSE_MASK;
17489 if (cclauses)
17490 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
17491
17492 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
17493 if (cclauses)
17494 {
17495 omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
17496 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
17497 }
17498
17499 block = c_begin_compound_stmt (true);
17500 ret = c_parser_omp_sections_scope (loc, parser);
17501 if (ret)
17502 OMP_SECTIONS_CLAUSES (ret) = clauses;
17503 block = c_end_compound_stmt (loc, block, true);
17504 add_stmt (block);
17505
17506 return ret;
17507 }
17508
17509 /* OpenMP 2.5:
17510 # pragma omp parallel parallel-clause[optseq] new-line
17511 structured-block
17512 # pragma omp parallel for parallel-for-clause[optseq] new-line
17513 structured-block
17514 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
17515 structured-block
17516
17517 OpenMP 4.0:
17518 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
17519 structured-block
17520
17521 LOC is the location of the #pragma token.
17522 */
17523
17524 #define OMP_PARALLEL_CLAUSE_MASK \
17525 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
17526 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
17527 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
17528 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
17529 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
17530 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
17531 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
17532 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
17533 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
17534
17535 static tree
17536 c_parser_omp_parallel (location_t loc, c_parser *parser,
17537 char *p_name, omp_clause_mask mask, tree *cclauses,
17538 bool *if_p)
17539 {
17540 tree stmt, clauses, block;
17541
17542 strcat (p_name, " parallel");
17543 mask |= OMP_PARALLEL_CLAUSE_MASK;
17544 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
17545 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
17546 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
17547 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
17548
17549 if (c_parser_next_token_is_keyword (parser, RID_FOR))
17550 {
17551 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
17552 if (cclauses == NULL)
17553 cclauses = cclauses_buf;
17554
17555 c_parser_consume_token (parser);
17556 if (!flag_openmp) /* flag_openmp_simd */
17557 return c_parser_omp_for (loc, parser, p_name, mask, cclauses, if_p);
17558 block = c_begin_omp_parallel ();
17559 tree ret = c_parser_omp_for (loc, parser, p_name, mask, cclauses, if_p);
17560 stmt
17561 = c_finish_omp_parallel (loc, cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
17562 block);
17563 if (ret == NULL_TREE)
17564 return ret;
17565 OMP_PARALLEL_COMBINED (stmt) = 1;
17566 return stmt;
17567 }
17568 /* When combined with distribute, parallel has to be followed by for.
17569 #pragma omp target parallel is allowed though. */
17570 else if (cclauses
17571 && (mask & (OMP_CLAUSE_MASK_1
17572 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
17573 {
17574 error_at (loc, "expected %<for%> after %qs", p_name);
17575 c_parser_skip_to_pragma_eol (parser);
17576 return NULL_TREE;
17577 }
17578 else if (cclauses == NULL && c_parser_next_token_is (parser, CPP_NAME))
17579 {
17580 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
17581 if (strcmp (p, "master") == 0)
17582 {
17583 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
17584 cclauses = cclauses_buf;
17585
17586 c_parser_consume_token (parser);
17587 if (!flag_openmp) /* flag_openmp_simd */
17588 return c_parser_omp_master (loc, parser, p_name, mask, cclauses,
17589 if_p);
17590 block = c_begin_omp_parallel ();
17591 tree ret = c_parser_omp_master (loc, parser, p_name, mask, cclauses,
17592 if_p);
17593 stmt = c_finish_omp_parallel (loc,
17594 cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
17595 block);
17596 OMP_PARALLEL_COMBINED (stmt) = 1;
17597 if (ret == NULL)
17598 return ret;
17599 return stmt;
17600 }
17601 else if (!flag_openmp) /* flag_openmp_simd */
17602 {
17603 c_parser_skip_to_pragma_eol (parser, false);
17604 return NULL_TREE;
17605 }
17606 else if (strcmp (p, "sections") == 0)
17607 {
17608 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
17609 cclauses = cclauses_buf;
17610
17611 c_parser_consume_token (parser);
17612 block = c_begin_omp_parallel ();
17613 c_parser_omp_sections (loc, parser, p_name, mask, cclauses);
17614 stmt = c_finish_omp_parallel (loc,
17615 cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
17616 block);
17617 OMP_PARALLEL_COMBINED (stmt) = 1;
17618 return stmt;
17619 }
17620 }
17621 else if (!flag_openmp) /* flag_openmp_simd */
17622 {
17623 c_parser_skip_to_pragma_eol (parser, false);
17624 return NULL_TREE;
17625 }
17626
17627 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
17628 if (cclauses)
17629 {
17630 omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
17631 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
17632 }
17633
17634 block = c_begin_omp_parallel ();
17635 c_parser_statement (parser, if_p);
17636 stmt = c_finish_omp_parallel (loc, clauses, block);
17637
17638 return stmt;
17639 }
17640
17641 /* OpenMP 2.5:
17642 # pragma omp single single-clause[optseq] new-line
17643 structured-block
17644
17645 LOC is the location of the #pragma.
17646 */
17647
17648 #define OMP_SINGLE_CLAUSE_MASK \
17649 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
17650 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
17651 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
17652 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
17653
17654 static tree
17655 c_parser_omp_single (location_t loc, c_parser *parser, bool *if_p)
17656 {
17657 tree stmt = make_node (OMP_SINGLE);
17658 SET_EXPR_LOCATION (stmt, loc);
17659 TREE_TYPE (stmt) = void_type_node;
17660
17661 OMP_SINGLE_CLAUSES (stmt)
17662 = c_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
17663 "#pragma omp single");
17664 OMP_SINGLE_BODY (stmt) = c_parser_omp_structured_block (parser, if_p);
17665
17666 return add_stmt (stmt);
17667 }
17668
17669 /* OpenMP 3.0:
17670 # pragma omp task task-clause[optseq] new-line
17671
17672 LOC is the location of the #pragma.
17673 */
17674
17675 #define OMP_TASK_CLAUSE_MASK \
17676 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
17677 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
17678 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
17679 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
17680 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
17681 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
17682 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
17683 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
17684 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
17685 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY) \
17686 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION))
17687
17688 static tree
17689 c_parser_omp_task (location_t loc, c_parser *parser, bool *if_p)
17690 {
17691 tree clauses, block;
17692
17693 clauses = c_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
17694 "#pragma omp task");
17695
17696 block = c_begin_omp_task ();
17697 c_parser_statement (parser, if_p);
17698 return c_finish_omp_task (loc, clauses, block);
17699 }
17700
17701 /* OpenMP 3.0:
17702 # pragma omp taskwait new-line
17703
17704 OpenMP 5.0:
17705 # pragma omp taskwait taskwait-clause[optseq] new-line
17706 */
17707
17708 #define OMP_TASKWAIT_CLAUSE_MASK \
17709 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
17710
17711 static void
17712 c_parser_omp_taskwait (c_parser *parser)
17713 {
17714 location_t loc = c_parser_peek_token (parser)->location;
17715 c_parser_consume_pragma (parser);
17716
17717 tree clauses
17718 = c_parser_omp_all_clauses (parser, OMP_TASKWAIT_CLAUSE_MASK,
17719 "#pragma omp taskwait");
17720
17721 if (clauses)
17722 {
17723 tree stmt = make_node (OMP_TASK);
17724 TREE_TYPE (stmt) = void_node;
17725 OMP_TASK_CLAUSES (stmt) = clauses;
17726 OMP_TASK_BODY (stmt) = NULL_TREE;
17727 SET_EXPR_LOCATION (stmt, loc);
17728 add_stmt (stmt);
17729 }
17730 else
17731 c_finish_omp_taskwait (loc);
17732 }
17733
17734 /* OpenMP 3.1:
17735 # pragma omp taskyield new-line
17736 */
17737
17738 static void
17739 c_parser_omp_taskyield (c_parser *parser)
17740 {
17741 location_t loc = c_parser_peek_token (parser)->location;
17742 c_parser_consume_pragma (parser);
17743 c_parser_skip_to_pragma_eol (parser);
17744
17745 c_finish_omp_taskyield (loc);
17746 }
17747
17748 /* OpenMP 4.0:
17749 # pragma omp taskgroup new-line
17750
17751 OpenMP 5.0:
17752 # pragma omp taskgroup taskgroup-clause[optseq] new-line
17753 */
17754
17755 #define OMP_TASKGROUP_CLAUSE_MASK \
17756 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASK_REDUCTION))
17757
17758 static tree
17759 c_parser_omp_taskgroup (location_t loc, c_parser *parser, bool *if_p)
17760 {
17761 tree clauses = c_parser_omp_all_clauses (parser, OMP_TASKGROUP_CLAUSE_MASK,
17762 "#pragma omp taskgroup");
17763
17764 tree body = c_parser_omp_structured_block (parser, if_p);
17765 return c_finish_omp_taskgroup (loc, body, clauses);
17766 }
17767
17768 /* OpenMP 4.0:
17769 # pragma omp cancel cancel-clause[optseq] new-line
17770
17771 LOC is the location of the #pragma.
17772 */
17773
17774 #define OMP_CANCEL_CLAUSE_MASK \
17775 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
17776 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
17777 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
17778 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
17779 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
17780
17781 static void
17782 c_parser_omp_cancel (c_parser *parser)
17783 {
17784 location_t loc = c_parser_peek_token (parser)->location;
17785
17786 c_parser_consume_pragma (parser);
17787 tree clauses = c_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
17788 "#pragma omp cancel");
17789
17790 c_finish_omp_cancel (loc, clauses);
17791 }
17792
17793 /* OpenMP 4.0:
17794 # pragma omp cancellation point cancelpt-clause[optseq] new-line
17795
17796 LOC is the location of the #pragma.
17797 */
17798
17799 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
17800 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
17801 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
17802 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
17803 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
17804
17805 static void
17806 c_parser_omp_cancellation_point (c_parser *parser, enum pragma_context context)
17807 {
17808 location_t loc = c_parser_peek_token (parser)->location;
17809 tree clauses;
17810 bool point_seen = false;
17811
17812 c_parser_consume_pragma (parser);
17813 if (c_parser_next_token_is (parser, CPP_NAME))
17814 {
17815 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
17816 if (strcmp (p, "point") == 0)
17817 {
17818 c_parser_consume_token (parser);
17819 point_seen = true;
17820 }
17821 }
17822 if (!point_seen)
17823 {
17824 c_parser_error (parser, "expected %<point%>");
17825 c_parser_skip_to_pragma_eol (parser);
17826 return;
17827 }
17828
17829 if (context != pragma_compound)
17830 {
17831 if (context == pragma_stmt)
17832 error_at (loc,
17833 "%<#pragma %s%> may only be used in compound statements",
17834 "omp cancellation point");
17835 else
17836 c_parser_error (parser, "expected declaration specifiers");
17837 c_parser_skip_to_pragma_eol (parser, false);
17838 return;
17839 }
17840
17841 clauses
17842 = c_parser_omp_all_clauses (parser, OMP_CANCELLATION_POINT_CLAUSE_MASK,
17843 "#pragma omp cancellation point");
17844
17845 c_finish_omp_cancellation_point (loc, clauses);
17846 }
17847
17848 /* OpenMP 4.0:
17849 #pragma omp distribute distribute-clause[optseq] new-line
17850 for-loop */
17851
17852 #define OMP_DISTRIBUTE_CLAUSE_MASK \
17853 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
17854 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
17855 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
17856 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
17857 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
17858
17859 static tree
17860 c_parser_omp_distribute (location_t loc, c_parser *parser,
17861 char *p_name, omp_clause_mask mask, tree *cclauses,
17862 bool *if_p)
17863 {
17864 tree clauses, block, ret;
17865
17866 strcat (p_name, " distribute");
17867 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
17868
17869 if (c_parser_next_token_is (parser, CPP_NAME))
17870 {
17871 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
17872 bool simd = false;
17873 bool parallel = false;
17874
17875 if (strcmp (p, "simd") == 0)
17876 simd = true;
17877 else
17878 parallel = strcmp (p, "parallel") == 0;
17879 if (parallel || simd)
17880 {
17881 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
17882 if (cclauses == NULL)
17883 cclauses = cclauses_buf;
17884 c_parser_consume_token (parser);
17885 if (!flag_openmp) /* flag_openmp_simd */
17886 {
17887 if (simd)
17888 return c_parser_omp_simd (loc, parser, p_name, mask, cclauses,
17889 if_p);
17890 else
17891 return c_parser_omp_parallel (loc, parser, p_name, mask,
17892 cclauses, if_p);
17893 }
17894 block = c_begin_compound_stmt (true);
17895 if (simd)
17896 ret = c_parser_omp_simd (loc, parser, p_name, mask, cclauses,
17897 if_p);
17898 else
17899 ret = c_parser_omp_parallel (loc, parser, p_name, mask, cclauses,
17900 if_p);
17901 block = c_end_compound_stmt (loc, block, true);
17902 if (ret == NULL)
17903 return ret;
17904 ret = make_node (OMP_DISTRIBUTE);
17905 TREE_TYPE (ret) = void_type_node;
17906 OMP_FOR_BODY (ret) = block;
17907 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
17908 SET_EXPR_LOCATION (ret, loc);
17909 add_stmt (ret);
17910 return ret;
17911 }
17912 }
17913 if (!flag_openmp) /* flag_openmp_simd */
17914 {
17915 c_parser_skip_to_pragma_eol (parser, false);
17916 return NULL_TREE;
17917 }
17918
17919 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
17920 if (cclauses)
17921 {
17922 omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
17923 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
17924 }
17925
17926 block = c_begin_compound_stmt (true);
17927 ret = c_parser_omp_for_loop (loc, parser, OMP_DISTRIBUTE, clauses, NULL,
17928 if_p);
17929 block = c_end_compound_stmt (loc, block, true);
17930 add_stmt (block);
17931
17932 return ret;
17933 }
17934
17935 /* OpenMP 4.0:
17936 # pragma omp teams teams-clause[optseq] new-line
17937 structured-block */
17938
17939 #define OMP_TEAMS_CLAUSE_MASK \
17940 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
17941 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
17942 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
17943 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
17944 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
17945 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
17946 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
17947
17948 static tree
17949 c_parser_omp_teams (location_t loc, c_parser *parser,
17950 char *p_name, omp_clause_mask mask, tree *cclauses,
17951 bool *if_p)
17952 {
17953 tree clauses, block, ret;
17954
17955 strcat (p_name, " teams");
17956 mask |= OMP_TEAMS_CLAUSE_MASK;
17957
17958 if (c_parser_next_token_is (parser, CPP_NAME))
17959 {
17960 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
17961 if (strcmp (p, "distribute") == 0)
17962 {
17963 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
17964 if (cclauses == NULL)
17965 cclauses = cclauses_buf;
17966
17967 c_parser_consume_token (parser);
17968 if (!flag_openmp) /* flag_openmp_simd */
17969 return c_parser_omp_distribute (loc, parser, p_name, mask,
17970 cclauses, if_p);
17971 block = c_begin_omp_parallel ();
17972 ret = c_parser_omp_distribute (loc, parser, p_name, mask, cclauses,
17973 if_p);
17974 block = c_end_compound_stmt (loc, block, true);
17975 if (ret == NULL)
17976 return ret;
17977 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
17978 ret = make_node (OMP_TEAMS);
17979 TREE_TYPE (ret) = void_type_node;
17980 OMP_TEAMS_CLAUSES (ret) = clauses;
17981 OMP_TEAMS_BODY (ret) = block;
17982 OMP_TEAMS_COMBINED (ret) = 1;
17983 SET_EXPR_LOCATION (ret, loc);
17984 return add_stmt (ret);
17985 }
17986 }
17987 if (!flag_openmp) /* flag_openmp_simd */
17988 {
17989 c_parser_skip_to_pragma_eol (parser, false);
17990 return NULL_TREE;
17991 }
17992
17993 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
17994 if (cclauses)
17995 {
17996 omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
17997 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
17998 }
17999
18000 tree stmt = make_node (OMP_TEAMS);
18001 TREE_TYPE (stmt) = void_type_node;
18002 OMP_TEAMS_CLAUSES (stmt) = clauses;
18003 block = c_begin_omp_parallel ();
18004 add_stmt (c_parser_omp_structured_block (parser, if_p));
18005 OMP_TEAMS_BODY (stmt) = c_end_compound_stmt (loc, block, true);
18006 SET_EXPR_LOCATION (stmt, loc);
18007
18008 return add_stmt (stmt);
18009 }
18010
18011 /* OpenMP 4.0:
18012 # pragma omp target data target-data-clause[optseq] new-line
18013 structured-block */
18014
18015 #define OMP_TARGET_DATA_CLAUSE_MASK \
18016 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
18017 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
18018 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
18019 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
18020
18021 static tree
18022 c_parser_omp_target_data (location_t loc, c_parser *parser, bool *if_p)
18023 {
18024 tree clauses
18025 = c_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
18026 "#pragma omp target data");
18027 int map_seen = 0;
18028 for (tree *pc = &clauses; *pc;)
18029 {
18030 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
18031 switch (OMP_CLAUSE_MAP_KIND (*pc))
18032 {
18033 case GOMP_MAP_TO:
18034 case GOMP_MAP_ALWAYS_TO:
18035 case GOMP_MAP_FROM:
18036 case GOMP_MAP_ALWAYS_FROM:
18037 case GOMP_MAP_TOFROM:
18038 case GOMP_MAP_ALWAYS_TOFROM:
18039 case GOMP_MAP_ALLOC:
18040 map_seen = 3;
18041 break;
18042 case GOMP_MAP_FIRSTPRIVATE_POINTER:
18043 case GOMP_MAP_ALWAYS_POINTER:
18044 break;
18045 default:
18046 map_seen |= 1;
18047 error_at (OMP_CLAUSE_LOCATION (*pc),
18048 "%<#pragma omp target data%> with map-type other "
18049 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
18050 "on %<map%> clause");
18051 *pc = OMP_CLAUSE_CHAIN (*pc);
18052 continue;
18053 }
18054 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_USE_DEVICE_PTR)
18055 map_seen = 3;
18056 pc = &OMP_CLAUSE_CHAIN (*pc);
18057 }
18058
18059 if (map_seen != 3)
18060 {
18061 if (map_seen == 0)
18062 error_at (loc,
18063 "%<#pragma omp target data%> must contain at least "
18064 "one %<map%> or %<use_device_ptr%> clause");
18065 return NULL_TREE;
18066 }
18067
18068 tree stmt = make_node (OMP_TARGET_DATA);
18069 TREE_TYPE (stmt) = void_type_node;
18070 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
18071 keep_next_level ();
18072 tree block = c_begin_compound_stmt (true);
18073 add_stmt (c_parser_omp_structured_block (parser, if_p));
18074 OMP_TARGET_DATA_BODY (stmt) = c_end_compound_stmt (loc, block, true);
18075
18076 SET_EXPR_LOCATION (stmt, loc);
18077 return add_stmt (stmt);
18078 }
18079
18080 /* OpenMP 4.0:
18081 # pragma omp target update target-update-clause[optseq] new-line */
18082
18083 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
18084 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
18085 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
18086 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
18087 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
18088 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
18089 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
18090
18091 static bool
18092 c_parser_omp_target_update (location_t loc, c_parser *parser,
18093 enum pragma_context context)
18094 {
18095 if (context == pragma_stmt)
18096 {
18097 error_at (loc, "%<#pragma %s%> may only be used in compound statements",
18098 "omp target update");
18099 c_parser_skip_to_pragma_eol (parser, false);
18100 return false;
18101 }
18102
18103 tree clauses
18104 = c_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
18105 "#pragma omp target update");
18106 if (omp_find_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
18107 && omp_find_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
18108 {
18109 error_at (loc,
18110 "%<#pragma omp target update%> must contain at least one "
18111 "%<from%> or %<to%> clauses");
18112 return false;
18113 }
18114
18115 tree stmt = make_node (OMP_TARGET_UPDATE);
18116 TREE_TYPE (stmt) = void_type_node;
18117 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
18118 SET_EXPR_LOCATION (stmt, loc);
18119 add_stmt (stmt);
18120 return false;
18121 }
18122
18123 /* OpenMP 4.5:
18124 # pragma omp target enter data target-data-clause[optseq] new-line */
18125
18126 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
18127 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
18128 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
18129 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
18130 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
18131 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
18132
18133 static tree
18134 c_parser_omp_target_enter_data (location_t loc, c_parser *parser,
18135 enum pragma_context context)
18136 {
18137 bool data_seen = false;
18138 if (c_parser_next_token_is (parser, CPP_NAME))
18139 {
18140 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
18141 if (strcmp (p, "data") == 0)
18142 {
18143 c_parser_consume_token (parser);
18144 data_seen = true;
18145 }
18146 }
18147 if (!data_seen)
18148 {
18149 c_parser_error (parser, "expected %<data%>");
18150 c_parser_skip_to_pragma_eol (parser);
18151 return NULL_TREE;
18152 }
18153
18154 if (context == pragma_stmt)
18155 {
18156 error_at (loc, "%<#pragma %s%> may only be used in compound statements",
18157 "omp target enter data");
18158 c_parser_skip_to_pragma_eol (parser, false);
18159 return NULL_TREE;
18160 }
18161
18162 tree clauses
18163 = c_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
18164 "#pragma omp target enter data");
18165 int map_seen = 0;
18166 for (tree *pc = &clauses; *pc;)
18167 {
18168 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
18169 switch (OMP_CLAUSE_MAP_KIND (*pc))
18170 {
18171 case GOMP_MAP_TO:
18172 case GOMP_MAP_ALWAYS_TO:
18173 case GOMP_MAP_ALLOC:
18174 map_seen = 3;
18175 break;
18176 case GOMP_MAP_FIRSTPRIVATE_POINTER:
18177 case GOMP_MAP_ALWAYS_POINTER:
18178 break;
18179 default:
18180 map_seen |= 1;
18181 error_at (OMP_CLAUSE_LOCATION (*pc),
18182 "%<#pragma omp target enter data%> with map-type other "
18183 "than %<to%> or %<alloc%> on %<map%> clause");
18184 *pc = OMP_CLAUSE_CHAIN (*pc);
18185 continue;
18186 }
18187 pc = &OMP_CLAUSE_CHAIN (*pc);
18188 }
18189
18190 if (map_seen != 3)
18191 {
18192 if (map_seen == 0)
18193 error_at (loc,
18194 "%<#pragma omp target enter data%> must contain at least "
18195 "one %<map%> clause");
18196 return NULL_TREE;
18197 }
18198
18199 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
18200 TREE_TYPE (stmt) = void_type_node;
18201 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
18202 SET_EXPR_LOCATION (stmt, loc);
18203 add_stmt (stmt);
18204 return stmt;
18205 }
18206
18207 /* OpenMP 4.5:
18208 # pragma omp target exit data target-data-clause[optseq] new-line */
18209
18210 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
18211 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
18212 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
18213 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
18214 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
18215 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
18216
18217 static tree
18218 c_parser_omp_target_exit_data (location_t loc, c_parser *parser,
18219 enum pragma_context context)
18220 {
18221 bool data_seen = false;
18222 if (c_parser_next_token_is (parser, CPP_NAME))
18223 {
18224 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
18225 if (strcmp (p, "data") == 0)
18226 {
18227 c_parser_consume_token (parser);
18228 data_seen = true;
18229 }
18230 }
18231 if (!data_seen)
18232 {
18233 c_parser_error (parser, "expected %<data%>");
18234 c_parser_skip_to_pragma_eol (parser);
18235 return NULL_TREE;
18236 }
18237
18238 if (context == pragma_stmt)
18239 {
18240 error_at (loc, "%<#pragma %s%> may only be used in compound statements",
18241 "omp target exit data");
18242 c_parser_skip_to_pragma_eol (parser, false);
18243 return NULL_TREE;
18244 }
18245
18246 tree clauses
18247 = c_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
18248 "#pragma omp target exit data");
18249
18250 int map_seen = 0;
18251 for (tree *pc = &clauses; *pc;)
18252 {
18253 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
18254 switch (OMP_CLAUSE_MAP_KIND (*pc))
18255 {
18256 case GOMP_MAP_FROM:
18257 case GOMP_MAP_ALWAYS_FROM:
18258 case GOMP_MAP_RELEASE:
18259 case GOMP_MAP_DELETE:
18260 map_seen = 3;
18261 break;
18262 case GOMP_MAP_FIRSTPRIVATE_POINTER:
18263 case GOMP_MAP_ALWAYS_POINTER:
18264 break;
18265 default:
18266 map_seen |= 1;
18267 error_at (OMP_CLAUSE_LOCATION (*pc),
18268 "%<#pragma omp target exit data%> with map-type other "
18269 "than %<from%>, %<release%> or %<delete%> on %<map%>"
18270 " clause");
18271 *pc = OMP_CLAUSE_CHAIN (*pc);
18272 continue;
18273 }
18274 pc = &OMP_CLAUSE_CHAIN (*pc);
18275 }
18276
18277 if (map_seen != 3)
18278 {
18279 if (map_seen == 0)
18280 error_at (loc,
18281 "%<#pragma omp target exit data%> must contain at least one "
18282 "%<map%> clause");
18283 return NULL_TREE;
18284 }
18285
18286 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
18287 TREE_TYPE (stmt) = void_type_node;
18288 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
18289 SET_EXPR_LOCATION (stmt, loc);
18290 add_stmt (stmt);
18291 return stmt;
18292 }
18293
18294 /* OpenMP 4.0:
18295 # pragma omp target target-clause[optseq] new-line
18296 structured-block */
18297
18298 #define OMP_TARGET_CLAUSE_MASK \
18299 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
18300 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
18301 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
18302 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
18303 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
18304 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
18305 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
18306 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
18307 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
18308
18309 static bool
18310 c_parser_omp_target (c_parser *parser, enum pragma_context context, bool *if_p)
18311 {
18312 location_t loc = c_parser_peek_token (parser)->location;
18313 c_parser_consume_pragma (parser);
18314 tree *pc = NULL, stmt, block;
18315
18316 if (context != pragma_stmt && context != pragma_compound)
18317 {
18318 c_parser_error (parser, "expected declaration specifiers");
18319 c_parser_skip_to_pragma_eol (parser);
18320 return false;
18321 }
18322
18323 if (flag_openmp)
18324 omp_requires_mask
18325 = (enum omp_requires) (omp_requires_mask | OMP_REQUIRES_TARGET_USED);
18326
18327 if (c_parser_next_token_is (parser, CPP_NAME))
18328 {
18329 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
18330 enum tree_code ccode = ERROR_MARK;
18331
18332 if (strcmp (p, "teams") == 0)
18333 ccode = OMP_TEAMS;
18334 else if (strcmp (p, "parallel") == 0)
18335 ccode = OMP_PARALLEL;
18336 else if (strcmp (p, "simd") == 0)
18337 ccode = OMP_SIMD;
18338 if (ccode != ERROR_MARK)
18339 {
18340 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
18341 char p_name[sizeof ("#pragma omp target teams distribute "
18342 "parallel for simd")];
18343
18344 c_parser_consume_token (parser);
18345 strcpy (p_name, "#pragma omp target");
18346 if (!flag_openmp) /* flag_openmp_simd */
18347 {
18348 tree stmt;
18349 switch (ccode)
18350 {
18351 case OMP_TEAMS:
18352 stmt = c_parser_omp_teams (loc, parser, p_name,
18353 OMP_TARGET_CLAUSE_MASK,
18354 cclauses, if_p);
18355 break;
18356 case OMP_PARALLEL:
18357 stmt = c_parser_omp_parallel (loc, parser, p_name,
18358 OMP_TARGET_CLAUSE_MASK,
18359 cclauses, if_p);
18360 break;
18361 case OMP_SIMD:
18362 stmt = c_parser_omp_simd (loc, parser, p_name,
18363 OMP_TARGET_CLAUSE_MASK,
18364 cclauses, if_p);
18365 break;
18366 default:
18367 gcc_unreachable ();
18368 }
18369 return stmt != NULL_TREE;
18370 }
18371 keep_next_level ();
18372 tree block = c_begin_compound_stmt (true), ret;
18373 switch (ccode)
18374 {
18375 case OMP_TEAMS:
18376 ret = c_parser_omp_teams (loc, parser, p_name,
18377 OMP_TARGET_CLAUSE_MASK, cclauses,
18378 if_p);
18379 break;
18380 case OMP_PARALLEL:
18381 ret = c_parser_omp_parallel (loc, parser, p_name,
18382 OMP_TARGET_CLAUSE_MASK, cclauses,
18383 if_p);
18384 break;
18385 case OMP_SIMD:
18386 ret = c_parser_omp_simd (loc, parser, p_name,
18387 OMP_TARGET_CLAUSE_MASK, cclauses,
18388 if_p);
18389 break;
18390 default:
18391 gcc_unreachable ();
18392 }
18393 block = c_end_compound_stmt (loc, block, true);
18394 if (ret == NULL_TREE)
18395 return false;
18396 if (ccode == OMP_TEAMS)
18397 {
18398 /* For combined target teams, ensure the num_teams and
18399 thread_limit clause expressions are evaluated on the host,
18400 before entering the target construct. */
18401 tree c;
18402 for (c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
18403 c; c = OMP_CLAUSE_CHAIN (c))
18404 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
18405 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
18406 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
18407 {
18408 tree expr = OMP_CLAUSE_OPERAND (c, 0);
18409 tree tmp = create_tmp_var_raw (TREE_TYPE (expr));
18410 expr = build4 (TARGET_EXPR, TREE_TYPE (expr), tmp,
18411 expr, NULL_TREE, NULL_TREE);
18412 add_stmt (expr);
18413 OMP_CLAUSE_OPERAND (c, 0) = expr;
18414 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
18415 OMP_CLAUSE_FIRSTPRIVATE);
18416 OMP_CLAUSE_DECL (tc) = tmp;
18417 OMP_CLAUSE_CHAIN (tc)
18418 = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
18419 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
18420 }
18421 }
18422 tree stmt = make_node (OMP_TARGET);
18423 TREE_TYPE (stmt) = void_type_node;
18424 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
18425 OMP_TARGET_BODY (stmt) = block;
18426 OMP_TARGET_COMBINED (stmt) = 1;
18427 SET_EXPR_LOCATION (stmt, loc);
18428 add_stmt (stmt);
18429 pc = &OMP_TARGET_CLAUSES (stmt);
18430 goto check_clauses;
18431 }
18432 else if (!flag_openmp) /* flag_openmp_simd */
18433 {
18434 c_parser_skip_to_pragma_eol (parser, false);
18435 return false;
18436 }
18437 else if (strcmp (p, "data") == 0)
18438 {
18439 c_parser_consume_token (parser);
18440 c_parser_omp_target_data (loc, parser, if_p);
18441 return true;
18442 }
18443 else if (strcmp (p, "enter") == 0)
18444 {
18445 c_parser_consume_token (parser);
18446 c_parser_omp_target_enter_data (loc, parser, context);
18447 return false;
18448 }
18449 else if (strcmp (p, "exit") == 0)
18450 {
18451 c_parser_consume_token (parser);
18452 c_parser_omp_target_exit_data (loc, parser, context);
18453 return false;
18454 }
18455 else if (strcmp (p, "update") == 0)
18456 {
18457 c_parser_consume_token (parser);
18458 return c_parser_omp_target_update (loc, parser, context);
18459 }
18460 }
18461 if (!flag_openmp) /* flag_openmp_simd */
18462 {
18463 c_parser_skip_to_pragma_eol (parser, false);
18464 return false;
18465 }
18466
18467 stmt = make_node (OMP_TARGET);
18468 TREE_TYPE (stmt) = void_type_node;
18469
18470 OMP_TARGET_CLAUSES (stmt)
18471 = c_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
18472 "#pragma omp target");
18473 pc = &OMP_TARGET_CLAUSES (stmt);
18474 keep_next_level ();
18475 block = c_begin_compound_stmt (true);
18476 add_stmt (c_parser_omp_structured_block (parser, if_p));
18477 OMP_TARGET_BODY (stmt) = c_end_compound_stmt (loc, block, true);
18478
18479 SET_EXPR_LOCATION (stmt, loc);
18480 add_stmt (stmt);
18481
18482 check_clauses:
18483 while (*pc)
18484 {
18485 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
18486 switch (OMP_CLAUSE_MAP_KIND (*pc))
18487 {
18488 case GOMP_MAP_TO:
18489 case GOMP_MAP_ALWAYS_TO:
18490 case GOMP_MAP_FROM:
18491 case GOMP_MAP_ALWAYS_FROM:
18492 case GOMP_MAP_TOFROM:
18493 case GOMP_MAP_ALWAYS_TOFROM:
18494 case GOMP_MAP_ALLOC:
18495 case GOMP_MAP_FIRSTPRIVATE_POINTER:
18496 case GOMP_MAP_ALWAYS_POINTER:
18497 break;
18498 default:
18499 error_at (OMP_CLAUSE_LOCATION (*pc),
18500 "%<#pragma omp target%> with map-type other "
18501 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
18502 "on %<map%> clause");
18503 *pc = OMP_CLAUSE_CHAIN (*pc);
18504 continue;
18505 }
18506 pc = &OMP_CLAUSE_CHAIN (*pc);
18507 }
18508 return true;
18509 }
18510
18511 /* OpenMP 4.0:
18512 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
18513
18514 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
18515 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
18516 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
18517 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
18518 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
18519 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
18520 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
18521
18522 static void
18523 c_parser_omp_declare_simd (c_parser *parser, enum pragma_context context)
18524 {
18525 auto_vec<c_token> clauses;
18526 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
18527 {
18528 c_token *token = c_parser_peek_token (parser);
18529 if (token->type == CPP_EOF)
18530 {
18531 c_parser_skip_to_pragma_eol (parser);
18532 return;
18533 }
18534 clauses.safe_push (*token);
18535 c_parser_consume_token (parser);
18536 }
18537 clauses.safe_push (*c_parser_peek_token (parser));
18538 c_parser_skip_to_pragma_eol (parser);
18539
18540 while (c_parser_next_token_is (parser, CPP_PRAGMA))
18541 {
18542 if (c_parser_peek_token (parser)->pragma_kind
18543 != PRAGMA_OMP_DECLARE
18544 || c_parser_peek_2nd_token (parser)->type != CPP_NAME
18545 || strcmp (IDENTIFIER_POINTER
18546 (c_parser_peek_2nd_token (parser)->value),
18547 "simd") != 0)
18548 {
18549 c_parser_error (parser,
18550 "%<#pragma omp declare simd%> must be followed by "
18551 "function declaration or definition or another "
18552 "%<#pragma omp declare simd%>");
18553 return;
18554 }
18555 c_parser_consume_pragma (parser);
18556 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
18557 {
18558 c_token *token = c_parser_peek_token (parser);
18559 if (token->type == CPP_EOF)
18560 {
18561 c_parser_skip_to_pragma_eol (parser);
18562 return;
18563 }
18564 clauses.safe_push (*token);
18565 c_parser_consume_token (parser);
18566 }
18567 clauses.safe_push (*c_parser_peek_token (parser));
18568 c_parser_skip_to_pragma_eol (parser);
18569 }
18570
18571 /* Make sure nothing tries to read past the end of the tokens. */
18572 c_token eof_token;
18573 memset (&eof_token, 0, sizeof (eof_token));
18574 eof_token.type = CPP_EOF;
18575 clauses.safe_push (eof_token);
18576 clauses.safe_push (eof_token);
18577
18578 switch (context)
18579 {
18580 case pragma_external:
18581 if (c_parser_next_token_is (parser, CPP_KEYWORD)
18582 && c_parser_peek_token (parser)->keyword == RID_EXTENSION)
18583 {
18584 int ext = disable_extension_diagnostics ();
18585 do
18586 c_parser_consume_token (parser);
18587 while (c_parser_next_token_is (parser, CPP_KEYWORD)
18588 && c_parser_peek_token (parser)->keyword == RID_EXTENSION);
18589 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
18590 NULL, clauses);
18591 restore_extension_diagnostics (ext);
18592 }
18593 else
18594 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
18595 NULL, clauses);
18596 break;
18597 case pragma_struct:
18598 case pragma_param:
18599 case pragma_stmt:
18600 c_parser_error (parser, "%<#pragma omp declare simd%> must be followed by "
18601 "function declaration or definition");
18602 break;
18603 case pragma_compound:
18604 if (c_parser_next_token_is (parser, CPP_KEYWORD)
18605 && c_parser_peek_token (parser)->keyword == RID_EXTENSION)
18606 {
18607 int ext = disable_extension_diagnostics ();
18608 do
18609 c_parser_consume_token (parser);
18610 while (c_parser_next_token_is (parser, CPP_KEYWORD)
18611 && c_parser_peek_token (parser)->keyword == RID_EXTENSION);
18612 if (c_parser_next_tokens_start_declaration (parser))
18613 {
18614 c_parser_declaration_or_fndef (parser, true, true, true, true,
18615 true, NULL, clauses);
18616 restore_extension_diagnostics (ext);
18617 break;
18618 }
18619 restore_extension_diagnostics (ext);
18620 }
18621 else if (c_parser_next_tokens_start_declaration (parser))
18622 {
18623 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
18624 NULL, clauses);
18625 break;
18626 }
18627 c_parser_error (parser, "%<#pragma omp declare simd%> must be followed by "
18628 "function declaration or definition");
18629 break;
18630 default:
18631 gcc_unreachable ();
18632 }
18633 }
18634
18635 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
18636 and put that into "omp declare simd" attribute. */
18637
18638 static void
18639 c_finish_omp_declare_simd (c_parser *parser, tree fndecl, tree parms,
18640 vec<c_token> clauses)
18641 {
18642 /* Normally first token is CPP_NAME "simd". CPP_EOF there indicates
18643 error has been reported and CPP_PRAGMA that c_finish_omp_declare_simd
18644 has already processed the tokens. */
18645 if (clauses.exists () && clauses[0].type == CPP_EOF)
18646 return;
18647 if (fndecl == NULL_TREE || TREE_CODE (fndecl) != FUNCTION_DECL)
18648 {
18649 error ("%<#pragma omp declare simd%> not immediately followed by "
18650 "a function declaration or definition");
18651 clauses[0].type = CPP_EOF;
18652 return;
18653 }
18654 if (clauses.exists () && clauses[0].type != CPP_NAME)
18655 {
18656 error_at (DECL_SOURCE_LOCATION (fndecl),
18657 "%<#pragma omp declare simd%> not immediately followed by "
18658 "a single function declaration or definition");
18659 clauses[0].type = CPP_EOF;
18660 return;
18661 }
18662
18663 if (parms == NULL_TREE)
18664 parms = DECL_ARGUMENTS (fndecl);
18665
18666 unsigned int tokens_avail = parser->tokens_avail;
18667 gcc_assert (parser->tokens == &parser->tokens_buf[0]);
18668
18669
18670 parser->tokens = clauses.address ();
18671 parser->tokens_avail = clauses.length ();
18672
18673 /* c_parser_omp_declare_simd pushed 2 extra CPP_EOF tokens at the end. */
18674 while (parser->tokens_avail > 3)
18675 {
18676 c_token *token = c_parser_peek_token (parser);
18677 gcc_assert (token->type == CPP_NAME
18678 && strcmp (IDENTIFIER_POINTER (token->value), "simd") == 0);
18679 c_parser_consume_token (parser);
18680 parser->in_pragma = true;
18681
18682 tree c = NULL_TREE;
18683 c = c_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
18684 "#pragma omp declare simd");
18685 c = c_omp_declare_simd_clauses_to_numbers (parms, c);
18686 if (c != NULL_TREE)
18687 c = tree_cons (NULL_TREE, c, NULL_TREE);
18688 c = build_tree_list (get_identifier ("omp declare simd"), c);
18689 TREE_CHAIN (c) = DECL_ATTRIBUTES (fndecl);
18690 DECL_ATTRIBUTES (fndecl) = c;
18691 }
18692
18693 parser->tokens = &parser->tokens_buf[0];
18694 parser->tokens_avail = tokens_avail;
18695 if (clauses.exists ())
18696 clauses[0].type = CPP_PRAGMA;
18697 }
18698
18699
18700 /* OpenMP 4.0:
18701 # pragma omp declare target new-line
18702 declarations and definitions
18703 # pragma omp end declare target new-line
18704
18705 OpenMP 4.5:
18706 # pragma omp declare target ( extended-list ) new-line
18707
18708 # pragma omp declare target declare-target-clauses[seq] new-line */
18709
18710 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
18711 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
18712 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
18713
18714 static void
18715 c_parser_omp_declare_target (c_parser *parser)
18716 {
18717 location_t loc = c_parser_peek_token (parser)->location;
18718 tree clauses = NULL_TREE;
18719 if (c_parser_next_token_is (parser, CPP_NAME))
18720 clauses = c_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
18721 "#pragma omp declare target");
18722 else if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
18723 {
18724 clauses = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_TO_DECLARE,
18725 clauses);
18726 clauses = c_finish_omp_clauses (clauses, C_ORT_OMP);
18727 c_parser_skip_to_pragma_eol (parser);
18728 }
18729 else
18730 {
18731 c_parser_skip_to_pragma_eol (parser);
18732 current_omp_declare_target_attribute++;
18733 return;
18734 }
18735 if (current_omp_declare_target_attribute)
18736 error_at (loc, "%<#pragma omp declare target%> with clauses in between "
18737 "%<#pragma omp declare target%> without clauses and "
18738 "%<#pragma omp end declare target%>");
18739 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
18740 {
18741 tree t = OMP_CLAUSE_DECL (c), id;
18742 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
18743 tree at2 = lookup_attribute ("omp declare target link",
18744 DECL_ATTRIBUTES (t));
18745 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
18746 {
18747 id = get_identifier ("omp declare target link");
18748 std::swap (at1, at2);
18749 }
18750 else
18751 id = get_identifier ("omp declare target");
18752 if (at2)
18753 {
18754 error_at (OMP_CLAUSE_LOCATION (c),
18755 "%qD specified both in declare target %<link%> and %<to%>"
18756 " clauses", t);
18757 continue;
18758 }
18759 if (!at1)
18760 {
18761 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
18762 if (TREE_CODE (t) != FUNCTION_DECL && !is_global_var (t))
18763 continue;
18764
18765 symtab_node *node = symtab_node::get (t);
18766 if (node != NULL)
18767 {
18768 node->offloadable = 1;
18769 if (ENABLE_OFFLOADING)
18770 {
18771 g->have_offload = true;
18772 if (is_a <varpool_node *> (node))
18773 vec_safe_push (offload_vars, t);
18774 }
18775 }
18776 }
18777 }
18778 }
18779
18780 static void
18781 c_parser_omp_end_declare_target (c_parser *parser)
18782 {
18783 location_t loc = c_parser_peek_token (parser)->location;
18784 c_parser_consume_pragma (parser);
18785 if (c_parser_next_token_is (parser, CPP_NAME)
18786 && strcmp (IDENTIFIER_POINTER (c_parser_peek_token (parser)->value),
18787 "declare") == 0)
18788 {
18789 c_parser_consume_token (parser);
18790 if (c_parser_next_token_is (parser, CPP_NAME)
18791 && strcmp (IDENTIFIER_POINTER (c_parser_peek_token (parser)->value),
18792 "target") == 0)
18793 c_parser_consume_token (parser);
18794 else
18795 {
18796 c_parser_error (parser, "expected %<target%>");
18797 c_parser_skip_to_pragma_eol (parser);
18798 return;
18799 }
18800 }
18801 else
18802 {
18803 c_parser_error (parser, "expected %<declare%>");
18804 c_parser_skip_to_pragma_eol (parser);
18805 return;
18806 }
18807 c_parser_skip_to_pragma_eol (parser);
18808 if (!current_omp_declare_target_attribute)
18809 error_at (loc, "%<#pragma omp end declare target%> without corresponding "
18810 "%<#pragma omp declare target%>");
18811 else
18812 current_omp_declare_target_attribute--;
18813 }
18814
18815
18816 /* OpenMP 4.0
18817 #pragma omp declare reduction (reduction-id : typename-list : expression) \
18818 initializer-clause[opt] new-line
18819
18820 initializer-clause:
18821 initializer (omp_priv = initializer)
18822 initializer (function-name (argument-list)) */
18823
18824 static void
18825 c_parser_omp_declare_reduction (c_parser *parser, enum pragma_context context)
18826 {
18827 unsigned int tokens_avail = 0, i;
18828 vec<tree> types = vNULL;
18829 vec<c_token> clauses = vNULL;
18830 enum tree_code reduc_code = ERROR_MARK;
18831 tree reduc_id = NULL_TREE;
18832 tree type;
18833 location_t rloc = c_parser_peek_token (parser)->location;
18834
18835 if (context == pragma_struct || context == pragma_param)
18836 {
18837 error ("%<#pragma omp declare reduction%> not at file or block scope");
18838 goto fail;
18839 }
18840
18841 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
18842 goto fail;
18843
18844 switch (c_parser_peek_token (parser)->type)
18845 {
18846 case CPP_PLUS:
18847 reduc_code = PLUS_EXPR;
18848 break;
18849 case CPP_MULT:
18850 reduc_code = MULT_EXPR;
18851 break;
18852 case CPP_MINUS:
18853 reduc_code = MINUS_EXPR;
18854 break;
18855 case CPP_AND:
18856 reduc_code = BIT_AND_EXPR;
18857 break;
18858 case CPP_XOR:
18859 reduc_code = BIT_XOR_EXPR;
18860 break;
18861 case CPP_OR:
18862 reduc_code = BIT_IOR_EXPR;
18863 break;
18864 case CPP_AND_AND:
18865 reduc_code = TRUTH_ANDIF_EXPR;
18866 break;
18867 case CPP_OR_OR:
18868 reduc_code = TRUTH_ORIF_EXPR;
18869 break;
18870 case CPP_NAME:
18871 const char *p;
18872 p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
18873 if (strcmp (p, "min") == 0)
18874 {
18875 reduc_code = MIN_EXPR;
18876 break;
18877 }
18878 if (strcmp (p, "max") == 0)
18879 {
18880 reduc_code = MAX_EXPR;
18881 break;
18882 }
18883 reduc_id = c_parser_peek_token (parser)->value;
18884 break;
18885 default:
18886 c_parser_error (parser,
18887 "expected %<+%>, %<*%>, %<-%>, %<&%>, "
18888 "%<^%>, %<|%>, %<&&%>, %<||%> or identifier");
18889 goto fail;
18890 }
18891
18892 tree orig_reduc_id, reduc_decl;
18893 orig_reduc_id = reduc_id;
18894 reduc_id = c_omp_reduction_id (reduc_code, reduc_id);
18895 reduc_decl = c_omp_reduction_decl (reduc_id);
18896 c_parser_consume_token (parser);
18897
18898 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
18899 goto fail;
18900
18901 while (true)
18902 {
18903 location_t loc = c_parser_peek_token (parser)->location;
18904 struct c_type_name *ctype = c_parser_type_name (parser);
18905 if (ctype != NULL)
18906 {
18907 type = groktypename (ctype, NULL, NULL);
18908 if (type == error_mark_node)
18909 ;
18910 else if ((INTEGRAL_TYPE_P (type)
18911 || TREE_CODE (type) == REAL_TYPE
18912 || TREE_CODE (type) == COMPLEX_TYPE)
18913 && orig_reduc_id == NULL_TREE)
18914 error_at (loc, "predeclared arithmetic type in "
18915 "%<#pragma omp declare reduction%>");
18916 else if (TREE_CODE (type) == FUNCTION_TYPE
18917 || TREE_CODE (type) == ARRAY_TYPE)
18918 error_at (loc, "function or array type in "
18919 "%<#pragma omp declare reduction%>");
18920 else if (TYPE_ATOMIC (type))
18921 error_at (loc, "%<_Atomic%> qualified type in "
18922 "%<#pragma omp declare reduction%>");
18923 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
18924 error_at (loc, "const, volatile or restrict qualified type in "
18925 "%<#pragma omp declare reduction%>");
18926 else
18927 {
18928 tree t;
18929 for (t = DECL_INITIAL (reduc_decl); t; t = TREE_CHAIN (t))
18930 if (comptypes (TREE_PURPOSE (t), type))
18931 {
18932 error_at (loc, "redeclaration of %qs "
18933 "%<#pragma omp declare reduction%> for "
18934 "type %qT",
18935 IDENTIFIER_POINTER (reduc_id)
18936 + sizeof ("omp declare reduction ") - 1,
18937 type);
18938 location_t ploc
18939 = DECL_SOURCE_LOCATION (TREE_VEC_ELT (TREE_VALUE (t),
18940 0));
18941 error_at (ploc, "previous %<#pragma omp declare "
18942 "reduction%>");
18943 break;
18944 }
18945 if (t == NULL_TREE)
18946 types.safe_push (type);
18947 }
18948 if (c_parser_next_token_is (parser, CPP_COMMA))
18949 c_parser_consume_token (parser);
18950 else
18951 break;
18952 }
18953 else
18954 break;
18955 }
18956
18957 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>")
18958 || types.is_empty ())
18959 {
18960 fail:
18961 clauses.release ();
18962 types.release ();
18963 while (true)
18964 {
18965 c_token *token = c_parser_peek_token (parser);
18966 if (token->type == CPP_EOF || token->type == CPP_PRAGMA_EOL)
18967 break;
18968 c_parser_consume_token (parser);
18969 }
18970 c_parser_skip_to_pragma_eol (parser);
18971 return;
18972 }
18973
18974 if (types.length () > 1)
18975 {
18976 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
18977 {
18978 c_token *token = c_parser_peek_token (parser);
18979 if (token->type == CPP_EOF)
18980 goto fail;
18981 clauses.safe_push (*token);
18982 c_parser_consume_token (parser);
18983 }
18984 clauses.safe_push (*c_parser_peek_token (parser));
18985 c_parser_skip_to_pragma_eol (parser);
18986
18987 /* Make sure nothing tries to read past the end of the tokens. */
18988 c_token eof_token;
18989 memset (&eof_token, 0, sizeof (eof_token));
18990 eof_token.type = CPP_EOF;
18991 clauses.safe_push (eof_token);
18992 clauses.safe_push (eof_token);
18993 }
18994
18995 int errs = errorcount;
18996 FOR_EACH_VEC_ELT (types, i, type)
18997 {
18998 tokens_avail = parser->tokens_avail;
18999 gcc_assert (parser->tokens == &parser->tokens_buf[0]);
19000 if (!clauses.is_empty ())
19001 {
19002 parser->tokens = clauses.address ();
19003 parser->tokens_avail = clauses.length ();
19004 parser->in_pragma = true;
19005 }
19006
19007 bool nested = current_function_decl != NULL_TREE;
19008 if (nested)
19009 c_push_function_context ();
19010 tree fndecl = build_decl (BUILTINS_LOCATION, FUNCTION_DECL,
19011 reduc_id, default_function_type);
19012 current_function_decl = fndecl;
19013 allocate_struct_function (fndecl, true);
19014 push_scope ();
19015 tree stmt = push_stmt_list ();
19016 /* Intentionally BUILTINS_LOCATION, so that -Wshadow doesn't
19017 warn about these. */
19018 tree omp_out = build_decl (BUILTINS_LOCATION, VAR_DECL,
19019 get_identifier ("omp_out"), type);
19020 DECL_ARTIFICIAL (omp_out) = 1;
19021 DECL_CONTEXT (omp_out) = fndecl;
19022 pushdecl (omp_out);
19023 tree omp_in = build_decl (BUILTINS_LOCATION, VAR_DECL,
19024 get_identifier ("omp_in"), type);
19025 DECL_ARTIFICIAL (omp_in) = 1;
19026 DECL_CONTEXT (omp_in) = fndecl;
19027 pushdecl (omp_in);
19028 struct c_expr combiner = c_parser_expression (parser);
19029 struct c_expr initializer;
19030 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE;
19031 bool bad = false;
19032 initializer.set_error ();
19033 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
19034 bad = true;
19035 else if (c_parser_next_token_is (parser, CPP_NAME)
19036 && strcmp (IDENTIFIER_POINTER
19037 (c_parser_peek_token (parser)->value),
19038 "initializer") == 0)
19039 {
19040 c_parser_consume_token (parser);
19041 pop_scope ();
19042 push_scope ();
19043 omp_priv = build_decl (BUILTINS_LOCATION, VAR_DECL,
19044 get_identifier ("omp_priv"), type);
19045 DECL_ARTIFICIAL (omp_priv) = 1;
19046 DECL_INITIAL (omp_priv) = error_mark_node;
19047 DECL_CONTEXT (omp_priv) = fndecl;
19048 pushdecl (omp_priv);
19049 omp_orig = build_decl (BUILTINS_LOCATION, VAR_DECL,
19050 get_identifier ("omp_orig"), type);
19051 DECL_ARTIFICIAL (omp_orig) = 1;
19052 DECL_CONTEXT (omp_orig) = fndecl;
19053 pushdecl (omp_orig);
19054 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
19055 bad = true;
19056 else if (!c_parser_next_token_is (parser, CPP_NAME))
19057 {
19058 c_parser_error (parser, "expected %<omp_priv%> or "
19059 "function-name");
19060 bad = true;
19061 }
19062 else if (strcmp (IDENTIFIER_POINTER
19063 (c_parser_peek_token (parser)->value),
19064 "omp_priv") != 0)
19065 {
19066 if (c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN
19067 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
19068 {
19069 c_parser_error (parser, "expected function-name %<(%>");
19070 bad = true;
19071 }
19072 else
19073 initializer = c_parser_postfix_expression (parser);
19074 if (initializer.value
19075 && TREE_CODE (initializer.value) == CALL_EXPR)
19076 {
19077 int j;
19078 tree c = initializer.value;
19079 for (j = 0; j < call_expr_nargs (c); j++)
19080 {
19081 tree a = CALL_EXPR_ARG (c, j);
19082 STRIP_NOPS (a);
19083 if (TREE_CODE (a) == ADDR_EXPR
19084 && TREE_OPERAND (a, 0) == omp_priv)
19085 break;
19086 }
19087 if (j == call_expr_nargs (c))
19088 error ("one of the initializer call arguments should be "
19089 "%<&omp_priv%>");
19090 }
19091 }
19092 else
19093 {
19094 c_parser_consume_token (parser);
19095 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
19096 bad = true;
19097 else
19098 {
19099 tree st = push_stmt_list ();
19100 location_t loc = c_parser_peek_token (parser)->location;
19101 rich_location richloc (line_table, loc);
19102 start_init (omp_priv, NULL_TREE, 0, &richloc);
19103 struct c_expr init = c_parser_initializer (parser);
19104 finish_init ();
19105 finish_decl (omp_priv, loc, init.value,
19106 init.original_type, NULL_TREE);
19107 pop_stmt_list (st);
19108 }
19109 }
19110 if (!bad
19111 && !c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
19112 bad = true;
19113 }
19114
19115 if (!bad)
19116 {
19117 c_parser_skip_to_pragma_eol (parser);
19118
19119 tree t = tree_cons (type, make_tree_vec (omp_priv ? 6 : 3),
19120 DECL_INITIAL (reduc_decl));
19121 DECL_INITIAL (reduc_decl) = t;
19122 DECL_SOURCE_LOCATION (omp_out) = rloc;
19123 TREE_VEC_ELT (TREE_VALUE (t), 0) = omp_out;
19124 TREE_VEC_ELT (TREE_VALUE (t), 1) = omp_in;
19125 TREE_VEC_ELT (TREE_VALUE (t), 2) = combiner.value;
19126 walk_tree (&combiner.value, c_check_omp_declare_reduction_r,
19127 &TREE_VEC_ELT (TREE_VALUE (t), 0), NULL);
19128 if (omp_priv)
19129 {
19130 DECL_SOURCE_LOCATION (omp_priv) = rloc;
19131 TREE_VEC_ELT (TREE_VALUE (t), 3) = omp_priv;
19132 TREE_VEC_ELT (TREE_VALUE (t), 4) = omp_orig;
19133 TREE_VEC_ELT (TREE_VALUE (t), 5) = initializer.value;
19134 walk_tree (&initializer.value, c_check_omp_declare_reduction_r,
19135 &TREE_VEC_ELT (TREE_VALUE (t), 3), NULL);
19136 walk_tree (&DECL_INITIAL (omp_priv),
19137 c_check_omp_declare_reduction_r,
19138 &TREE_VEC_ELT (TREE_VALUE (t), 3), NULL);
19139 }
19140 }
19141
19142 pop_stmt_list (stmt);
19143 pop_scope ();
19144 if (cfun->language != NULL)
19145 {
19146 ggc_free (cfun->language);
19147 cfun->language = NULL;
19148 }
19149 set_cfun (NULL);
19150 current_function_decl = NULL_TREE;
19151 if (nested)
19152 c_pop_function_context ();
19153
19154 if (!clauses.is_empty ())
19155 {
19156 parser->tokens = &parser->tokens_buf[0];
19157 parser->tokens_avail = tokens_avail;
19158 }
19159 if (bad)
19160 goto fail;
19161 if (errs != errorcount)
19162 break;
19163 }
19164
19165 clauses.release ();
19166 types.release ();
19167 }
19168
19169
19170 /* OpenMP 4.0
19171 #pragma omp declare simd declare-simd-clauses[optseq] new-line
19172 #pragma omp declare reduction (reduction-id : typename-list : expression) \
19173 initializer-clause[opt] new-line
19174 #pragma omp declare target new-line */
19175
19176 static void
19177 c_parser_omp_declare (c_parser *parser, enum pragma_context context)
19178 {
19179 c_parser_consume_pragma (parser);
19180 if (c_parser_next_token_is (parser, CPP_NAME))
19181 {
19182 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
19183 if (strcmp (p, "simd") == 0)
19184 {
19185 /* c_parser_consume_token (parser); done in
19186 c_parser_omp_declare_simd. */
19187 c_parser_omp_declare_simd (parser, context);
19188 return;
19189 }
19190 if (strcmp (p, "reduction") == 0)
19191 {
19192 c_parser_consume_token (parser);
19193 c_parser_omp_declare_reduction (parser, context);
19194 return;
19195 }
19196 if (!flag_openmp) /* flag_openmp_simd */
19197 {
19198 c_parser_skip_to_pragma_eol (parser, false);
19199 return;
19200 }
19201 if (strcmp (p, "target") == 0)
19202 {
19203 c_parser_consume_token (parser);
19204 c_parser_omp_declare_target (parser);
19205 return;
19206 }
19207 }
19208
19209 c_parser_error (parser, "expected %<simd%> or %<reduction%> "
19210 "or %<target%>");
19211 c_parser_skip_to_pragma_eol (parser);
19212 }
19213
19214 /* OpenMP 5.0
19215 #pragma omp requires clauses[optseq] new-line */
19216
19217 static void
19218 c_parser_omp_requires (c_parser *parser)
19219 {
19220 bool first = true;
19221 enum omp_requires new_req = (enum omp_requires) 0;
19222
19223 c_parser_consume_pragma (parser);
19224
19225 location_t loc = c_parser_peek_token (parser)->location;
19226 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
19227 {
19228 if (!first && c_parser_next_token_is (parser, CPP_COMMA))
19229 c_parser_consume_token (parser);
19230
19231 first = false;
19232
19233 if (c_parser_next_token_is (parser, CPP_NAME))
19234 {
19235 const char *p
19236 = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
19237 location_t cloc = c_parser_peek_token (parser)->location;
19238 enum omp_requires this_req = (enum omp_requires) 0;
19239
19240 if (!strcmp (p, "unified_address"))
19241 this_req = OMP_REQUIRES_UNIFIED_ADDRESS;
19242 else if (!strcmp (p, "unified_shared_memory"))
19243 this_req = OMP_REQUIRES_UNIFIED_SHARED_MEMORY;
19244 else if (!strcmp (p, "dynamic_allocators"))
19245 this_req = OMP_REQUIRES_DYNAMIC_ALLOCATORS;
19246 else if (!strcmp (p, "reverse_offload"))
19247 this_req = OMP_REQUIRES_REVERSE_OFFLOAD;
19248 else if (!strcmp (p, "atomic_default_mem_order"))
19249 {
19250 c_parser_consume_token (parser);
19251
19252 matching_parens parens;
19253 if (parens.require_open (parser))
19254 {
19255 if (c_parser_next_token_is (parser, CPP_NAME))
19256 {
19257 tree v = c_parser_peek_token (parser)->value;
19258 p = IDENTIFIER_POINTER (v);
19259
19260 if (!strcmp (p, "seq_cst"))
19261 this_req
19262 = (enum omp_requires) OMP_MEMORY_ORDER_SEQ_CST;
19263 else if (!strcmp (p, "relaxed"))
19264 this_req
19265 = (enum omp_requires) OMP_MEMORY_ORDER_RELAXED;
19266 else if (!strcmp (p, "acq_rel"))
19267 this_req
19268 = (enum omp_requires) OMP_MEMORY_ORDER_ACQ_REL;
19269 }
19270 if (this_req == 0)
19271 {
19272 error_at (c_parser_peek_token (parser)->location,
19273 "expected %<seq_cst%>, %<relaxed%> or "
19274 "%<acq_rel%>");
19275 if (c_parser_peek_2nd_token (parser)->type
19276 == CPP_CLOSE_PAREN)
19277 c_parser_consume_token (parser);
19278 }
19279 else
19280 c_parser_consume_token (parser);
19281
19282 parens.skip_until_found_close (parser);
19283 if (this_req == 0)
19284 {
19285 c_parser_skip_to_pragma_eol (parser, false);
19286 return;
19287 }
19288 }
19289 p = NULL;
19290 }
19291 else
19292 {
19293 error_at (cloc, "expected %<unified_address%>, "
19294 "%<unified_shared_memory%>, "
19295 "%<dynamic_allocators%>, "
19296 "%<reverse_offload%> "
19297 "or %<atomic_default_mem_order%> clause");
19298 c_parser_skip_to_pragma_eol (parser, false);
19299 return;
19300 }
19301 if (p)
19302 sorry_at (cloc, "%qs clause on %<requires%> directive not "
19303 "supported yet", p);
19304 if (p)
19305 c_parser_consume_token (parser);
19306 if (this_req)
19307 {
19308 if ((this_req & ~OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
19309 {
19310 if ((this_req & new_req) != 0)
19311 error_at (cloc, "too many %qs clauses", p);
19312 if (this_req != OMP_REQUIRES_DYNAMIC_ALLOCATORS
19313 && (omp_requires_mask & OMP_REQUIRES_TARGET_USED) != 0)
19314 error_at (cloc, "%qs clause used lexically after first "
19315 "target construct or offloading API", p);
19316 }
19317 else if ((new_req & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
19318 {
19319 error_at (cloc, "too many %qs clauses",
19320 "atomic_default_mem_order");
19321 this_req = (enum omp_requires) 0;
19322 }
19323 else if ((omp_requires_mask
19324 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
19325 {
19326 error_at (cloc, "more than one %<atomic_default_mem_order%>"
19327 " clause in a single compilation unit");
19328 this_req
19329 = (enum omp_requires)
19330 (omp_requires_mask
19331 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER);
19332 }
19333 else if ((omp_requires_mask
19334 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER_USED) != 0)
19335 error_at (cloc, "%<atomic_default_mem_order%> clause used "
19336 "lexically after first %<atomic%> construct "
19337 "without memory order clause");
19338 new_req = (enum omp_requires) (new_req | this_req);
19339 omp_requires_mask
19340 = (enum omp_requires) (omp_requires_mask | this_req);
19341 continue;
19342 }
19343 }
19344 break;
19345 }
19346 c_parser_skip_to_pragma_eol (parser);
19347
19348 if (new_req == 0)
19349 error_at (loc, "%<pragma omp requires%> requires at least one clause");
19350 }
19351
19352 /* Helper function for c_parser_omp_taskloop.
19353 Disallow zero sized or potentially zero sized task reductions. */
19354
19355 static tree
19356 c_finish_taskloop_clauses (tree clauses)
19357 {
19358 tree *pc = &clauses;
19359 for (tree c = clauses; c; c = *pc)
19360 {
19361 bool remove = false;
19362 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
19363 {
19364 tree type = strip_array_types (TREE_TYPE (OMP_CLAUSE_DECL (c)));
19365 if (integer_zerop (TYPE_SIZE_UNIT (type)))
19366 {
19367 error_at (OMP_CLAUSE_LOCATION (c),
19368 "zero sized type %qT in %<reduction%> clause", type);
19369 remove = true;
19370 }
19371 else if (TREE_CODE (TYPE_SIZE_UNIT (type)) != INTEGER_CST)
19372 {
19373 error_at (OMP_CLAUSE_LOCATION (c),
19374 "variable sized type %qT in %<reduction%> clause",
19375 type);
19376 remove = true;
19377 }
19378 }
19379 if (remove)
19380 *pc = OMP_CLAUSE_CHAIN (c);
19381 else
19382 pc = &OMP_CLAUSE_CHAIN (c);
19383 }
19384 return clauses;
19385 }
19386
19387 /* OpenMP 4.5:
19388 #pragma omp taskloop taskloop-clause[optseq] new-line
19389 for-loop
19390
19391 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
19392 for-loop */
19393
19394 #define OMP_TASKLOOP_CLAUSE_MASK \
19395 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
19396 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
19397 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
19398 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
19399 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
19400 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
19401 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
19402 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
19403 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
19404 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
19405 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
19406 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
19407 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
19408 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY) \
19409 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
19410 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION))
19411
19412 static tree
19413 c_parser_omp_taskloop (location_t loc, c_parser *parser,
19414 char *p_name, omp_clause_mask mask, tree *cclauses,
19415 bool *if_p)
19416 {
19417 tree clauses, block, ret;
19418
19419 strcat (p_name, " taskloop");
19420 mask |= OMP_TASKLOOP_CLAUSE_MASK;
19421 /* #pragma omp parallel master taskloop{, simd} disallow in_reduction
19422 clause. */
19423 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS)) != 0)
19424 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION);
19425
19426 if (c_parser_next_token_is (parser, CPP_NAME))
19427 {
19428 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
19429
19430 if (strcmp (p, "simd") == 0)
19431 {
19432 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
19433 if (cclauses == NULL)
19434 cclauses = cclauses_buf;
19435 c_parser_consume_token (parser);
19436 if (!flag_openmp) /* flag_openmp_simd */
19437 return c_parser_omp_simd (loc, parser, p_name, mask, cclauses,
19438 if_p);
19439 block = c_begin_compound_stmt (true);
19440 ret = c_parser_omp_simd (loc, parser, p_name, mask, cclauses, if_p);
19441 block = c_end_compound_stmt (loc, block, true);
19442 if (ret == NULL)
19443 return ret;
19444 ret = make_node (OMP_TASKLOOP);
19445 TREE_TYPE (ret) = void_type_node;
19446 OMP_FOR_BODY (ret) = block;
19447 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
19448 OMP_FOR_CLAUSES (ret)
19449 = c_finish_taskloop_clauses (OMP_FOR_CLAUSES (ret));
19450 SET_EXPR_LOCATION (ret, loc);
19451 add_stmt (ret);
19452 return ret;
19453 }
19454 }
19455 if (!flag_openmp) /* flag_openmp_simd */
19456 {
19457 c_parser_skip_to_pragma_eol (parser, false);
19458 return NULL_TREE;
19459 }
19460
19461 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
19462 if (cclauses)
19463 {
19464 omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
19465 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
19466 }
19467
19468 clauses = c_finish_taskloop_clauses (clauses);
19469 block = c_begin_compound_stmt (true);
19470 ret = c_parser_omp_for_loop (loc, parser, OMP_TASKLOOP, clauses, NULL, if_p);
19471 block = c_end_compound_stmt (loc, block, true);
19472 add_stmt (block);
19473
19474 return ret;
19475 }
19476
19477 /* Main entry point to parsing most OpenMP pragmas. */
19478
19479 static void
19480 c_parser_omp_construct (c_parser *parser, bool *if_p)
19481 {
19482 enum pragma_kind p_kind;
19483 location_t loc;
19484 tree stmt;
19485 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
19486 omp_clause_mask mask (0);
19487
19488 loc = c_parser_peek_token (parser)->location;
19489 p_kind = c_parser_peek_token (parser)->pragma_kind;
19490 c_parser_consume_pragma (parser);
19491
19492 switch (p_kind)
19493 {
19494 case PRAGMA_OACC_ATOMIC:
19495 c_parser_omp_atomic (loc, parser);
19496 return;
19497 case PRAGMA_OACC_CACHE:
19498 strcpy (p_name, "#pragma acc");
19499 stmt = c_parser_oacc_cache (loc, parser);
19500 break;
19501 case PRAGMA_OACC_DATA:
19502 stmt = c_parser_oacc_data (loc, parser, if_p);
19503 break;
19504 case PRAGMA_OACC_HOST_DATA:
19505 stmt = c_parser_oacc_host_data (loc, parser, if_p);
19506 break;
19507 case PRAGMA_OACC_KERNELS:
19508 case PRAGMA_OACC_PARALLEL:
19509 strcpy (p_name, "#pragma acc");
19510 stmt = c_parser_oacc_kernels_parallel (loc, parser, p_kind, p_name,
19511 if_p);
19512 break;
19513 case PRAGMA_OACC_LOOP:
19514 strcpy (p_name, "#pragma acc");
19515 stmt = c_parser_oacc_loop (loc, parser, p_name, mask, NULL, if_p);
19516 break;
19517 case PRAGMA_OACC_WAIT:
19518 strcpy (p_name, "#pragma wait");
19519 stmt = c_parser_oacc_wait (loc, parser, p_name);
19520 break;
19521 case PRAGMA_OMP_ATOMIC:
19522 c_parser_omp_atomic (loc, parser);
19523 return;
19524 case PRAGMA_OMP_CRITICAL:
19525 stmt = c_parser_omp_critical (loc, parser, if_p);
19526 break;
19527 case PRAGMA_OMP_DISTRIBUTE:
19528 strcpy (p_name, "#pragma omp");
19529 stmt = c_parser_omp_distribute (loc, parser, p_name, mask, NULL, if_p);
19530 break;
19531 case PRAGMA_OMP_FOR:
19532 strcpy (p_name, "#pragma omp");
19533 stmt = c_parser_omp_for (loc, parser, p_name, mask, NULL, if_p);
19534 break;
19535 case PRAGMA_OMP_MASTER:
19536 strcpy (p_name, "#pragma omp");
19537 stmt = c_parser_omp_master (loc, parser, p_name, mask, NULL, if_p);
19538 break;
19539 case PRAGMA_OMP_PARALLEL:
19540 strcpy (p_name, "#pragma omp");
19541 stmt = c_parser_omp_parallel (loc, parser, p_name, mask, NULL, if_p);
19542 break;
19543 case PRAGMA_OMP_SECTIONS:
19544 strcpy (p_name, "#pragma omp");
19545 stmt = c_parser_omp_sections (loc, parser, p_name, mask, NULL);
19546 break;
19547 case PRAGMA_OMP_SIMD:
19548 strcpy (p_name, "#pragma omp");
19549 stmt = c_parser_omp_simd (loc, parser, p_name, mask, NULL, if_p);
19550 break;
19551 case PRAGMA_OMP_SINGLE:
19552 stmt = c_parser_omp_single (loc, parser, if_p);
19553 break;
19554 case PRAGMA_OMP_TASK:
19555 stmt = c_parser_omp_task (loc, parser, if_p);
19556 break;
19557 case PRAGMA_OMP_TASKGROUP:
19558 stmt = c_parser_omp_taskgroup (loc, parser, if_p);
19559 break;
19560 case PRAGMA_OMP_TASKLOOP:
19561 strcpy (p_name, "#pragma omp");
19562 stmt = c_parser_omp_taskloop (loc, parser, p_name, mask, NULL, if_p);
19563 break;
19564 case PRAGMA_OMP_TEAMS:
19565 strcpy (p_name, "#pragma omp");
19566 stmt = c_parser_omp_teams (loc, parser, p_name, mask, NULL, if_p);
19567 break;
19568 default:
19569 gcc_unreachable ();
19570 }
19571
19572 if (stmt)
19573 gcc_assert (EXPR_LOCATION (stmt) != UNKNOWN_LOCATION);
19574 }
19575
19576
19577 /* OpenMP 2.5:
19578 # pragma omp threadprivate (variable-list) */
19579
19580 static void
19581 c_parser_omp_threadprivate (c_parser *parser)
19582 {
19583 tree vars, t;
19584 location_t loc;
19585
19586 c_parser_consume_pragma (parser);
19587 loc = c_parser_peek_token (parser)->location;
19588 vars = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
19589
19590 /* Mark every variable in VARS to be assigned thread local storage. */
19591 for (t = vars; t; t = TREE_CHAIN (t))
19592 {
19593 tree v = TREE_PURPOSE (t);
19594
19595 /* FIXME diagnostics: Ideally we should keep individual
19596 locations for all the variables in the var list to make the
19597 following errors more precise. Perhaps
19598 c_parser_omp_var_list_parens() should construct a list of
19599 locations to go along with the var list. */
19600
19601 /* If V had already been marked threadprivate, it doesn't matter
19602 whether it had been used prior to this point. */
19603 if (!VAR_P (v))
19604 error_at (loc, "%qD is not a variable", v);
19605 else if (TREE_USED (v) && !C_DECL_THREADPRIVATE_P (v))
19606 error_at (loc, "%qE declared %<threadprivate%> after first use", v);
19607 else if (! is_global_var (v))
19608 error_at (loc, "automatic variable %qE cannot be %<threadprivate%>", v);
19609 else if (TREE_TYPE (v) == error_mark_node)
19610 ;
19611 else if (! COMPLETE_TYPE_P (TREE_TYPE (v)))
19612 error_at (loc, "%<threadprivate%> %qE has incomplete type", v);
19613 else
19614 {
19615 if (! DECL_THREAD_LOCAL_P (v))
19616 {
19617 set_decl_tls_model (v, decl_default_tls_model (v));
19618 /* If rtl has been already set for this var, call
19619 make_decl_rtl once again, so that encode_section_info
19620 has a chance to look at the new decl flags. */
19621 if (DECL_RTL_SET_P (v))
19622 make_decl_rtl (v);
19623 }
19624 C_DECL_THREADPRIVATE_P (v) = 1;
19625 }
19626 }
19627
19628 c_parser_skip_to_pragma_eol (parser);
19629 }
19630
19631 /* Parse a transaction attribute (GCC Extension).
19632
19633 transaction-attribute:
19634 attributes
19635 [ [ any-word ] ]
19636
19637 The transactional memory language description is written for C++,
19638 and uses the C++0x attribute syntax. For compatibility, allow the
19639 bracket style for transactions in C as well. */
19640
19641 static tree
19642 c_parser_transaction_attributes (c_parser *parser)
19643 {
19644 tree attr_name, attr = NULL;
19645
19646 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
19647 return c_parser_attributes (parser);
19648
19649 if (!c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
19650 return NULL_TREE;
19651 c_parser_consume_token (parser);
19652 if (!c_parser_require (parser, CPP_OPEN_SQUARE, "expected %<[%>"))
19653 goto error1;
19654
19655 attr_name = c_parser_attribute_any_word (parser);
19656 if (attr_name)
19657 {
19658 c_parser_consume_token (parser);
19659 attr = build_tree_list (attr_name, NULL_TREE);
19660 }
19661 else
19662 c_parser_error (parser, "expected identifier");
19663
19664 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
19665 error1:
19666 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
19667 return attr;
19668 }
19669
19670 /* Parse a __transaction_atomic or __transaction_relaxed statement
19671 (GCC Extension).
19672
19673 transaction-statement:
19674 __transaction_atomic transaction-attribute[opt] compound-statement
19675 __transaction_relaxed compound-statement
19676
19677 Note that the only valid attribute is: "outer".
19678 */
19679
19680 static tree
19681 c_parser_transaction (c_parser *parser, enum rid keyword)
19682 {
19683 unsigned int old_in = parser->in_transaction;
19684 unsigned int this_in = 1, new_in;
19685 location_t loc = c_parser_peek_token (parser)->location;
19686 tree stmt, attrs;
19687
19688 gcc_assert ((keyword == RID_TRANSACTION_ATOMIC
19689 || keyword == RID_TRANSACTION_RELAXED)
19690 && c_parser_next_token_is_keyword (parser, keyword));
19691 c_parser_consume_token (parser);
19692
19693 if (keyword == RID_TRANSACTION_RELAXED)
19694 this_in |= TM_STMT_ATTR_RELAXED;
19695 else
19696 {
19697 attrs = c_parser_transaction_attributes (parser);
19698 if (attrs)
19699 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
19700 }
19701
19702 /* Keep track if we're in the lexical scope of an outer transaction. */
19703 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
19704
19705 parser->in_transaction = new_in;
19706 stmt = c_parser_compound_statement (parser);
19707 parser->in_transaction = old_in;
19708
19709 if (flag_tm)
19710 stmt = c_finish_transaction (loc, stmt, this_in);
19711 else
19712 error_at (loc, (keyword == RID_TRANSACTION_ATOMIC ?
19713 "%<__transaction_atomic%> without transactional memory support enabled"
19714 : "%<__transaction_relaxed %> "
19715 "without transactional memory support enabled"));
19716
19717 return stmt;
19718 }
19719
19720 /* Parse a __transaction_atomic or __transaction_relaxed expression
19721 (GCC Extension).
19722
19723 transaction-expression:
19724 __transaction_atomic ( expression )
19725 __transaction_relaxed ( expression )
19726 */
19727
19728 static struct c_expr
19729 c_parser_transaction_expression (c_parser *parser, enum rid keyword)
19730 {
19731 struct c_expr ret;
19732 unsigned int old_in = parser->in_transaction;
19733 unsigned int this_in = 1;
19734 location_t loc = c_parser_peek_token (parser)->location;
19735 tree attrs;
19736
19737 gcc_assert ((keyword == RID_TRANSACTION_ATOMIC
19738 || keyword == RID_TRANSACTION_RELAXED)
19739 && c_parser_next_token_is_keyword (parser, keyword));
19740 c_parser_consume_token (parser);
19741
19742 if (keyword == RID_TRANSACTION_RELAXED)
19743 this_in |= TM_STMT_ATTR_RELAXED;
19744 else
19745 {
19746 attrs = c_parser_transaction_attributes (parser);
19747 if (attrs)
19748 this_in |= parse_tm_stmt_attr (attrs, 0);
19749 }
19750
19751 parser->in_transaction = this_in;
19752 matching_parens parens;
19753 if (parens.require_open (parser))
19754 {
19755 tree expr = c_parser_expression (parser).value;
19756 ret.original_type = TREE_TYPE (expr);
19757 ret.value = build1 (TRANSACTION_EXPR, ret.original_type, expr);
19758 if (this_in & TM_STMT_ATTR_RELAXED)
19759 TRANSACTION_EXPR_RELAXED (ret.value) = 1;
19760 SET_EXPR_LOCATION (ret.value, loc);
19761 ret.original_code = TRANSACTION_EXPR;
19762 if (!parens.require_close (parser))
19763 {
19764 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
19765 goto error;
19766 }
19767 }
19768 else
19769 {
19770 error:
19771 ret.set_error ();
19772 ret.original_code = ERROR_MARK;
19773 ret.original_type = NULL;
19774 }
19775 parser->in_transaction = old_in;
19776
19777 if (!flag_tm)
19778 error_at (loc, (keyword == RID_TRANSACTION_ATOMIC ?
19779 "%<__transaction_atomic%> without transactional memory support enabled"
19780 : "%<__transaction_relaxed %> "
19781 "without transactional memory support enabled"));
19782
19783 set_c_expr_source_range (&ret, loc, loc);
19784
19785 return ret;
19786 }
19787
19788 /* Parse a __transaction_cancel statement (GCC Extension).
19789
19790 transaction-cancel-statement:
19791 __transaction_cancel transaction-attribute[opt] ;
19792
19793 Note that the only valid attribute is "outer".
19794 */
19795
19796 static tree
19797 c_parser_transaction_cancel (c_parser *parser)
19798 {
19799 location_t loc = c_parser_peek_token (parser)->location;
19800 tree attrs;
19801 bool is_outer = false;
19802
19803 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TRANSACTION_CANCEL));
19804 c_parser_consume_token (parser);
19805
19806 attrs = c_parser_transaction_attributes (parser);
19807 if (attrs)
19808 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
19809
19810 if (!flag_tm)
19811 {
19812 error_at (loc, "%<__transaction_cancel%> without "
19813 "transactional memory support enabled");
19814 goto ret_error;
19815 }
19816 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
19817 {
19818 error_at (loc, "%<__transaction_cancel%> within a "
19819 "%<__transaction_relaxed%>");
19820 goto ret_error;
19821 }
19822 else if (is_outer)
19823 {
19824 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
19825 && !is_tm_may_cancel_outer (current_function_decl))
19826 {
19827 error_at (loc, "outer %<__transaction_cancel%> not "
19828 "within outer %<__transaction_atomic%>");
19829 error_at (loc, " or a %<transaction_may_cancel_outer%> function");
19830 goto ret_error;
19831 }
19832 }
19833 else if (parser->in_transaction == 0)
19834 {
19835 error_at (loc, "%<__transaction_cancel%> not within "
19836 "%<__transaction_atomic%>");
19837 goto ret_error;
19838 }
19839
19840 return add_stmt (build_tm_abort_call (loc, is_outer));
19841
19842 ret_error:
19843 return build1 (NOP_EXPR, void_type_node, error_mark_node);
19844 }
19845 \f
19846 /* Parse a single source file. */
19847
19848 void
19849 c_parse_file (void)
19850 {
19851 /* Use local storage to begin. If the first token is a pragma, parse it.
19852 If it is #pragma GCC pch_preprocess, then this will load a PCH file
19853 which will cause garbage collection. */
19854 c_parser tparser;
19855
19856 memset (&tparser, 0, sizeof tparser);
19857 tparser.tokens = &tparser.tokens_buf[0];
19858 the_parser = &tparser;
19859
19860 if (c_parser_peek_token (&tparser)->pragma_kind == PRAGMA_GCC_PCH_PREPROCESS)
19861 c_parser_pragma_pch_preprocess (&tparser);
19862
19863 the_parser = ggc_alloc<c_parser> ();
19864 *the_parser = tparser;
19865 if (tparser.tokens == &tparser.tokens_buf[0])
19866 the_parser->tokens = &the_parser->tokens_buf[0];
19867
19868 /* Initialize EH, if we've been told to do so. */
19869 if (flag_exceptions)
19870 using_eh_for_cleanups ();
19871
19872 c_parser_translation_unit (the_parser);
19873 the_parser = NULL;
19874 }
19875
19876 /* Parse the body of a function declaration marked with "__RTL".
19877
19878 The RTL parser works on the level of characters read from a
19879 FILE *, whereas c_parser works at the level of tokens.
19880 Square this circle by consuming all of the tokens up to and
19881 including the closing brace, recording the start/end of the RTL
19882 fragment, and reopening the file and re-reading the relevant
19883 lines within the RTL parser.
19884
19885 This requires the opening and closing braces of the C function
19886 to be on separate lines from the RTL they wrap.
19887
19888 Take ownership of START_WITH_PASS, if non-NULL. */
19889
19890 void
19891 c_parser_parse_rtl_body (c_parser *parser, char *start_with_pass)
19892 {
19893 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
19894 {
19895 free (start_with_pass);
19896 return;
19897 }
19898
19899 location_t start_loc = c_parser_peek_token (parser)->location;
19900
19901 /* Consume all tokens, up to the closing brace, handling
19902 matching pairs of braces in the rtl dump. */
19903 int num_open_braces = 1;
19904 while (1)
19905 {
19906 switch (c_parser_peek_token (parser)->type)
19907 {
19908 case CPP_OPEN_BRACE:
19909 num_open_braces++;
19910 break;
19911 case CPP_CLOSE_BRACE:
19912 if (--num_open_braces == 0)
19913 goto found_closing_brace;
19914 break;
19915 case CPP_EOF:
19916 error_at (start_loc, "no closing brace");
19917 free (start_with_pass);
19918 return;
19919 default:
19920 break;
19921 }
19922 c_parser_consume_token (parser);
19923 }
19924
19925 found_closing_brace:
19926 /* At the closing brace; record its location. */
19927 location_t end_loc = c_parser_peek_token (parser)->location;
19928
19929 /* Consume the closing brace. */
19930 c_parser_consume_token (parser);
19931
19932 /* Invoke the RTL parser. */
19933 if (!read_rtl_function_body_from_file_range (start_loc, end_loc))
19934 {
19935 free (start_with_pass);
19936 return;
19937 }
19938
19939 /* If a pass name was provided for START_WITH_PASS, run the backend
19940 accordingly now, on the cfun created above, transferring
19941 ownership of START_WITH_PASS. */
19942 if (start_with_pass)
19943 run_rtl_passes (start_with_pass);
19944 }
19945
19946 #include "gt-c-c-parser.h"