PR c++/89241 - ICE with lambda in template parameter list.
[gcc.git] / gcc / cp / semantics.c
1 /* Perform the semantic phase of parsing, i.e., the process of
2 building tree structure, checking semantic consistency, and
3 building RTL. These routines are used both during actual parsing
4 and during the instantiation of template functions.
5
6 Copyright (C) 1998-2019 Free Software Foundation, Inc.
7 Written by Mark Mitchell (mmitchell@usa.net) based on code found
8 formerly in parse.y and pt.c.
9
10 This file is part of GCC.
11
12 GCC is free software; you can redistribute it and/or modify it
13 under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 3, or (at your option)
15 any later version.
16
17 GCC is distributed in the hope that it will be useful, but
18 WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 General Public License for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with GCC; see the file COPYING3. If not see
24 <http://www.gnu.org/licenses/>. */
25
26 #include "config.h"
27 #include "system.h"
28 #include "coretypes.h"
29 #include "target.h"
30 #include "bitmap.h"
31 #include "cp-tree.h"
32 #include "stringpool.h"
33 #include "cgraph.h"
34 #include "stmt.h"
35 #include "varasm.h"
36 #include "stor-layout.h"
37 #include "c-family/c-objc.h"
38 #include "tree-inline.h"
39 #include "intl.h"
40 #include "tree-iterator.h"
41 #include "omp-general.h"
42 #include "convert.h"
43 #include "stringpool.h"
44 #include "attribs.h"
45 #include "gomp-constants.h"
46 #include "predict.h"
47 #include "memmodel.h"
48
49 /* There routines provide a modular interface to perform many parsing
50 operations. They may therefore be used during actual parsing, or
51 during template instantiation, which may be regarded as a
52 degenerate form of parsing. */
53
54 static tree maybe_convert_cond (tree);
55 static tree finalize_nrv_r (tree *, int *, void *);
56 static tree capture_decltype (tree);
57
58 /* Used for OpenMP non-static data member privatization. */
59
60 static hash_map<tree, tree> *omp_private_member_map;
61 static vec<tree> omp_private_member_vec;
62 static bool omp_private_member_ignore_next;
63
64
65 /* Deferred Access Checking Overview
66 ---------------------------------
67
68 Most C++ expressions and declarations require access checking
69 to be performed during parsing. However, in several cases,
70 this has to be treated differently.
71
72 For member declarations, access checking has to be deferred
73 until more information about the declaration is known. For
74 example:
75
76 class A {
77 typedef int X;
78 public:
79 X f();
80 };
81
82 A::X A::f();
83 A::X g();
84
85 When we are parsing the function return type `A::X', we don't
86 really know if this is allowed until we parse the function name.
87
88 Furthermore, some contexts require that access checking is
89 never performed at all. These include class heads, and template
90 instantiations.
91
92 Typical use of access checking functions is described here:
93
94 1. When we enter a context that requires certain access checking
95 mode, the function `push_deferring_access_checks' is called with
96 DEFERRING argument specifying the desired mode. Access checking
97 may be performed immediately (dk_no_deferred), deferred
98 (dk_deferred), or not performed (dk_no_check).
99
100 2. When a declaration such as a type, or a variable, is encountered,
101 the function `perform_or_defer_access_check' is called. It
102 maintains a vector of all deferred checks.
103
104 3. The global `current_class_type' or `current_function_decl' is then
105 setup by the parser. `enforce_access' relies on these information
106 to check access.
107
108 4. Upon exiting the context mentioned in step 1,
109 `perform_deferred_access_checks' is called to check all declaration
110 stored in the vector. `pop_deferring_access_checks' is then
111 called to restore the previous access checking mode.
112
113 In case of parsing error, we simply call `pop_deferring_access_checks'
114 without `perform_deferred_access_checks'. */
115
116 struct GTY(()) deferred_access {
117 /* A vector representing name-lookups for which we have deferred
118 checking access controls. We cannot check the accessibility of
119 names used in a decl-specifier-seq until we know what is being
120 declared because code like:
121
122 class A {
123 class B {};
124 B* f();
125 }
126
127 A::B* A::f() { return 0; }
128
129 is valid, even though `A::B' is not generally accessible. */
130 vec<deferred_access_check, va_gc> * GTY(()) deferred_access_checks;
131
132 /* The current mode of access checks. */
133 enum deferring_kind deferring_access_checks_kind;
134
135 };
136
137 /* Data for deferred access checking. */
138 static GTY(()) vec<deferred_access, va_gc> *deferred_access_stack;
139 static GTY(()) unsigned deferred_access_no_check;
140
141 /* Save the current deferred access states and start deferred
142 access checking iff DEFER_P is true. */
143
144 void
145 push_deferring_access_checks (deferring_kind deferring)
146 {
147 /* For context like template instantiation, access checking
148 disabling applies to all nested context. */
149 if (deferred_access_no_check || deferring == dk_no_check)
150 deferred_access_no_check++;
151 else
152 {
153 deferred_access e = {NULL, deferring};
154 vec_safe_push (deferred_access_stack, e);
155 }
156 }
157
158 /* Save the current deferred access states and start deferred access
159 checking, continuing the set of deferred checks in CHECKS. */
160
161 void
162 reopen_deferring_access_checks (vec<deferred_access_check, va_gc> * checks)
163 {
164 push_deferring_access_checks (dk_deferred);
165 if (!deferred_access_no_check)
166 deferred_access_stack->last().deferred_access_checks = checks;
167 }
168
169 /* Resume deferring access checks again after we stopped doing
170 this previously. */
171
172 void
173 resume_deferring_access_checks (void)
174 {
175 if (!deferred_access_no_check)
176 deferred_access_stack->last().deferring_access_checks_kind = dk_deferred;
177 }
178
179 /* Stop deferring access checks. */
180
181 void
182 stop_deferring_access_checks (void)
183 {
184 if (!deferred_access_no_check)
185 deferred_access_stack->last().deferring_access_checks_kind = dk_no_deferred;
186 }
187
188 /* Discard the current deferred access checks and restore the
189 previous states. */
190
191 void
192 pop_deferring_access_checks (void)
193 {
194 if (deferred_access_no_check)
195 deferred_access_no_check--;
196 else
197 deferred_access_stack->pop ();
198 }
199
200 /* Returns a TREE_LIST representing the deferred checks.
201 The TREE_PURPOSE of each node is the type through which the
202 access occurred; the TREE_VALUE is the declaration named.
203 */
204
205 vec<deferred_access_check, va_gc> *
206 get_deferred_access_checks (void)
207 {
208 if (deferred_access_no_check)
209 return NULL;
210 else
211 return (deferred_access_stack->last().deferred_access_checks);
212 }
213
214 /* Take current deferred checks and combine with the
215 previous states if we also defer checks previously.
216 Otherwise perform checks now. */
217
218 void
219 pop_to_parent_deferring_access_checks (void)
220 {
221 if (deferred_access_no_check)
222 deferred_access_no_check--;
223 else
224 {
225 vec<deferred_access_check, va_gc> *checks;
226 deferred_access *ptr;
227
228 checks = (deferred_access_stack->last ().deferred_access_checks);
229
230 deferred_access_stack->pop ();
231 ptr = &deferred_access_stack->last ();
232 if (ptr->deferring_access_checks_kind == dk_no_deferred)
233 {
234 /* Check access. */
235 perform_access_checks (checks, tf_warning_or_error);
236 }
237 else
238 {
239 /* Merge with parent. */
240 int i, j;
241 deferred_access_check *chk, *probe;
242
243 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
244 {
245 FOR_EACH_VEC_SAFE_ELT (ptr->deferred_access_checks, j, probe)
246 {
247 if (probe->binfo == chk->binfo &&
248 probe->decl == chk->decl &&
249 probe->diag_decl == chk->diag_decl)
250 goto found;
251 }
252 /* Insert into parent's checks. */
253 vec_safe_push (ptr->deferred_access_checks, *chk);
254 found:;
255 }
256 }
257 }
258 }
259
260 /* Perform the access checks in CHECKS. The TREE_PURPOSE of each node
261 is the BINFO indicating the qualifying scope used to access the
262 DECL node stored in the TREE_VALUE of the node. If CHECKS is empty
263 or we aren't in SFINAE context or all the checks succeed return TRUE,
264 otherwise FALSE. */
265
266 bool
267 perform_access_checks (vec<deferred_access_check, va_gc> *checks,
268 tsubst_flags_t complain)
269 {
270 int i;
271 deferred_access_check *chk;
272 location_t loc = input_location;
273 bool ok = true;
274
275 if (!checks)
276 return true;
277
278 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
279 {
280 input_location = chk->loc;
281 ok &= enforce_access (chk->binfo, chk->decl, chk->diag_decl, complain);
282 }
283
284 input_location = loc;
285 return (complain & tf_error) ? true : ok;
286 }
287
288 /* Perform the deferred access checks.
289
290 After performing the checks, we still have to keep the list
291 `deferred_access_stack->deferred_access_checks' since we may want
292 to check access for them again later in a different context.
293 For example:
294
295 class A {
296 typedef int X;
297 static X a;
298 };
299 A::X A::a, x; // No error for `A::a', error for `x'
300
301 We have to perform deferred access of `A::X', first with `A::a',
302 next with `x'. Return value like perform_access_checks above. */
303
304 bool
305 perform_deferred_access_checks (tsubst_flags_t complain)
306 {
307 return perform_access_checks (get_deferred_access_checks (), complain);
308 }
309
310 /* Defer checking the accessibility of DECL, when looked up in
311 BINFO. DIAG_DECL is the declaration to use to print diagnostics.
312 Return value like perform_access_checks above.
313 If non-NULL, report failures to AFI. */
314
315 bool
316 perform_or_defer_access_check (tree binfo, tree decl, tree diag_decl,
317 tsubst_flags_t complain,
318 access_failure_info *afi)
319 {
320 int i;
321 deferred_access *ptr;
322 deferred_access_check *chk;
323
324
325 /* Exit if we are in a context that no access checking is performed.
326 */
327 if (deferred_access_no_check)
328 return true;
329
330 gcc_assert (TREE_CODE (binfo) == TREE_BINFO);
331
332 ptr = &deferred_access_stack->last ();
333
334 /* If we are not supposed to defer access checks, just check now. */
335 if (ptr->deferring_access_checks_kind == dk_no_deferred)
336 {
337 bool ok = enforce_access (binfo, decl, diag_decl, complain, afi);
338 return (complain & tf_error) ? true : ok;
339 }
340
341 /* See if we are already going to perform this check. */
342 FOR_EACH_VEC_SAFE_ELT (ptr->deferred_access_checks, i, chk)
343 {
344 if (chk->decl == decl && chk->binfo == binfo &&
345 chk->diag_decl == diag_decl)
346 {
347 return true;
348 }
349 }
350 /* If not, record the check. */
351 deferred_access_check new_access = {binfo, decl, diag_decl, input_location};
352 vec_safe_push (ptr->deferred_access_checks, new_access);
353
354 return true;
355 }
356
357 /* Returns nonzero if the current statement is a full expression,
358 i.e. temporaries created during that statement should be destroyed
359 at the end of the statement. */
360
361 int
362 stmts_are_full_exprs_p (void)
363 {
364 return current_stmt_tree ()->stmts_are_full_exprs_p;
365 }
366
367 /* T is a statement. Add it to the statement-tree. This is the C++
368 version. The C/ObjC frontends have a slightly different version of
369 this function. */
370
371 tree
372 add_stmt (tree t)
373 {
374 enum tree_code code = TREE_CODE (t);
375
376 if (EXPR_P (t) && code != LABEL_EXPR)
377 {
378 if (!EXPR_HAS_LOCATION (t))
379 SET_EXPR_LOCATION (t, input_location);
380
381 /* When we expand a statement-tree, we must know whether or not the
382 statements are full-expressions. We record that fact here. */
383 STMT_IS_FULL_EXPR_P (t) = stmts_are_full_exprs_p ();
384 }
385
386 if (code == LABEL_EXPR || code == CASE_LABEL_EXPR)
387 STATEMENT_LIST_HAS_LABEL (cur_stmt_list) = 1;
388
389 /* Add T to the statement-tree. Non-side-effect statements need to be
390 recorded during statement expressions. */
391 gcc_checking_assert (!stmt_list_stack->is_empty ());
392 append_to_statement_list_force (t, &cur_stmt_list);
393
394 return t;
395 }
396
397 /* Returns the stmt_tree to which statements are currently being added. */
398
399 stmt_tree
400 current_stmt_tree (void)
401 {
402 return (cfun
403 ? &cfun->language->base.x_stmt_tree
404 : &scope_chain->x_stmt_tree);
405 }
406
407 /* If statements are full expressions, wrap STMT in a CLEANUP_POINT_EXPR. */
408
409 static tree
410 maybe_cleanup_point_expr (tree expr)
411 {
412 if (!processing_template_decl && stmts_are_full_exprs_p ())
413 expr = fold_build_cleanup_point_expr (TREE_TYPE (expr), expr);
414 return expr;
415 }
416
417 /* Like maybe_cleanup_point_expr except have the type of the new expression be
418 void so we don't need to create a temporary variable to hold the inner
419 expression. The reason why we do this is because the original type might be
420 an aggregate and we cannot create a temporary variable for that type. */
421
422 tree
423 maybe_cleanup_point_expr_void (tree expr)
424 {
425 if (!processing_template_decl && stmts_are_full_exprs_p ())
426 expr = fold_build_cleanup_point_expr (void_type_node, expr);
427 return expr;
428 }
429
430
431
432 /* Create a declaration statement for the declaration given by the DECL. */
433
434 void
435 add_decl_expr (tree decl)
436 {
437 tree r = build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl);
438 if (DECL_INITIAL (decl)
439 || (DECL_SIZE (decl) && TREE_SIDE_EFFECTS (DECL_SIZE (decl))))
440 r = maybe_cleanup_point_expr_void (r);
441 add_stmt (r);
442 }
443
444 /* Finish a scope. */
445
446 tree
447 do_poplevel (tree stmt_list)
448 {
449 tree block = NULL;
450
451 if (stmts_are_full_exprs_p ())
452 block = poplevel (kept_level_p (), 1, 0);
453
454 stmt_list = pop_stmt_list (stmt_list);
455
456 if (!processing_template_decl)
457 {
458 stmt_list = c_build_bind_expr (input_location, block, stmt_list);
459 /* ??? See c_end_compound_stmt re statement expressions. */
460 }
461
462 return stmt_list;
463 }
464
465 /* Begin a new scope. */
466
467 static tree
468 do_pushlevel (scope_kind sk)
469 {
470 tree ret = push_stmt_list ();
471 if (stmts_are_full_exprs_p ())
472 begin_scope (sk, NULL);
473 return ret;
474 }
475
476 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
477 when the current scope is exited. EH_ONLY is true when this is not
478 meant to apply to normal control flow transfer. */
479
480 void
481 push_cleanup (tree decl, tree cleanup, bool eh_only)
482 {
483 tree stmt = build_stmt (input_location, CLEANUP_STMT, NULL, cleanup, decl);
484 CLEANUP_EH_ONLY (stmt) = eh_only;
485 add_stmt (stmt);
486 CLEANUP_BODY (stmt) = push_stmt_list ();
487 }
488
489 /* Simple infinite loop tracking for -Wreturn-type. We keep a stack of all
490 the current loops, represented by 'NULL_TREE' if we've seen a possible
491 exit, and 'error_mark_node' if not. This is currently used only to
492 suppress the warning about a function with no return statements, and
493 therefore we don't bother noting returns as possible exits. We also
494 don't bother with gotos. */
495
496 static void
497 begin_maybe_infinite_loop (tree cond)
498 {
499 /* Only track this while parsing a function, not during instantiation. */
500 if (!cfun || (DECL_TEMPLATE_INSTANTIATION (current_function_decl)
501 && !processing_template_decl))
502 return;
503 bool maybe_infinite = true;
504 if (cond)
505 {
506 cond = fold_non_dependent_expr (cond);
507 maybe_infinite = integer_nonzerop (cond);
508 }
509 vec_safe_push (cp_function_chain->infinite_loops,
510 maybe_infinite ? error_mark_node : NULL_TREE);
511
512 }
513
514 /* A break is a possible exit for the current loop. */
515
516 void
517 break_maybe_infinite_loop (void)
518 {
519 if (!cfun)
520 return;
521 cp_function_chain->infinite_loops->last() = NULL_TREE;
522 }
523
524 /* If we reach the end of the loop without seeing a possible exit, we have
525 an infinite loop. */
526
527 static void
528 end_maybe_infinite_loop (tree cond)
529 {
530 if (!cfun || (DECL_TEMPLATE_INSTANTIATION (current_function_decl)
531 && !processing_template_decl))
532 return;
533 tree current = cp_function_chain->infinite_loops->pop();
534 if (current != NULL_TREE)
535 {
536 cond = fold_non_dependent_expr (cond);
537 if (integer_nonzerop (cond))
538 current_function_infinite_loop = 1;
539 }
540 }
541
542
543 /* Begin a conditional that might contain a declaration. When generating
544 normal code, we want the declaration to appear before the statement
545 containing the conditional. When generating template code, we want the
546 conditional to be rendered as the raw DECL_EXPR. */
547
548 static void
549 begin_cond (tree *cond_p)
550 {
551 if (processing_template_decl)
552 *cond_p = push_stmt_list ();
553 }
554
555 /* Finish such a conditional. */
556
557 static void
558 finish_cond (tree *cond_p, tree expr)
559 {
560 if (processing_template_decl)
561 {
562 tree cond = pop_stmt_list (*cond_p);
563
564 if (expr == NULL_TREE)
565 /* Empty condition in 'for'. */
566 gcc_assert (empty_expr_stmt_p (cond));
567 else if (check_for_bare_parameter_packs (expr))
568 expr = error_mark_node;
569 else if (!empty_expr_stmt_p (cond))
570 expr = build2 (COMPOUND_EXPR, TREE_TYPE (expr), cond, expr);
571 }
572 *cond_p = expr;
573 }
574
575 /* If *COND_P specifies a conditional with a declaration, transform the
576 loop such that
577 while (A x = 42) { }
578 for (; A x = 42;) { }
579 becomes
580 while (true) { A x = 42; if (!x) break; }
581 for (;;) { A x = 42; if (!x) break; }
582 The statement list for BODY will be empty if the conditional did
583 not declare anything. */
584
585 static void
586 simplify_loop_decl_cond (tree *cond_p, tree body)
587 {
588 tree cond, if_stmt;
589
590 if (!TREE_SIDE_EFFECTS (body))
591 return;
592
593 cond = *cond_p;
594 *cond_p = boolean_true_node;
595
596 if_stmt = begin_if_stmt ();
597 cond = cp_build_unary_op (TRUTH_NOT_EXPR, cond, false, tf_warning_or_error);
598 finish_if_stmt_cond (cond, if_stmt);
599 finish_break_stmt ();
600 finish_then_clause (if_stmt);
601 finish_if_stmt (if_stmt);
602 }
603
604 /* Finish a goto-statement. */
605
606 tree
607 finish_goto_stmt (tree destination)
608 {
609 if (identifier_p (destination))
610 destination = lookup_label (destination);
611
612 /* We warn about unused labels with -Wunused. That means we have to
613 mark the used labels as used. */
614 if (TREE_CODE (destination) == LABEL_DECL)
615 TREE_USED (destination) = 1;
616 else
617 {
618 destination = mark_rvalue_use (destination);
619 if (!processing_template_decl)
620 {
621 destination = cp_convert (ptr_type_node, destination,
622 tf_warning_or_error);
623 if (error_operand_p (destination))
624 return NULL_TREE;
625 destination
626 = fold_build_cleanup_point_expr (TREE_TYPE (destination),
627 destination);
628 }
629 }
630
631 check_goto (destination);
632
633 add_stmt (build_predict_expr (PRED_GOTO, NOT_TAKEN));
634 return add_stmt (build_stmt (input_location, GOTO_EXPR, destination));
635 }
636
637 /* COND is the condition-expression for an if, while, etc.,
638 statement. Convert it to a boolean value, if appropriate.
639 In addition, verify sequence points if -Wsequence-point is enabled. */
640
641 static tree
642 maybe_convert_cond (tree cond)
643 {
644 /* Empty conditions remain empty. */
645 if (!cond)
646 return NULL_TREE;
647
648 /* Wait until we instantiate templates before doing conversion. */
649 if (type_dependent_expression_p (cond))
650 return cond;
651
652 if (warn_sequence_point && !processing_template_decl)
653 verify_sequence_points (cond);
654
655 /* Do the conversion. */
656 cond = convert_from_reference (cond);
657
658 if (TREE_CODE (cond) == MODIFY_EXPR
659 && !TREE_NO_WARNING (cond)
660 && warn_parentheses
661 && warning_at (cp_expr_loc_or_loc (cond, input_location),
662 OPT_Wparentheses, "suggest parentheses around "
663 "assignment used as truth value"))
664 TREE_NO_WARNING (cond) = 1;
665
666 return condition_conversion (cond);
667 }
668
669 /* Finish an expression-statement, whose EXPRESSION is as indicated. */
670
671 tree
672 finish_expr_stmt (tree expr)
673 {
674 tree r = NULL_TREE;
675 location_t loc = EXPR_LOCATION (expr);
676
677 if (expr != NULL_TREE)
678 {
679 /* If we ran into a problem, make sure we complained. */
680 gcc_assert (expr != error_mark_node || seen_error ());
681
682 if (!processing_template_decl)
683 {
684 if (warn_sequence_point)
685 verify_sequence_points (expr);
686 expr = convert_to_void (expr, ICV_STATEMENT, tf_warning_or_error);
687 }
688 else if (!type_dependent_expression_p (expr))
689 convert_to_void (build_non_dependent_expr (expr), ICV_STATEMENT,
690 tf_warning_or_error);
691
692 if (check_for_bare_parameter_packs (expr))
693 expr = error_mark_node;
694
695 /* Simplification of inner statement expressions, compound exprs,
696 etc can result in us already having an EXPR_STMT. */
697 if (TREE_CODE (expr) != CLEANUP_POINT_EXPR)
698 {
699 if (TREE_CODE (expr) != EXPR_STMT)
700 expr = build_stmt (loc, EXPR_STMT, expr);
701 expr = maybe_cleanup_point_expr_void (expr);
702 }
703
704 r = add_stmt (expr);
705 }
706
707 return r;
708 }
709
710
711 /* Begin an if-statement. Returns a newly created IF_STMT if
712 appropriate. */
713
714 tree
715 begin_if_stmt (void)
716 {
717 tree r, scope;
718 scope = do_pushlevel (sk_cond);
719 r = build_stmt (input_location, IF_STMT, NULL_TREE,
720 NULL_TREE, NULL_TREE, scope);
721 current_binding_level->this_entity = r;
722 begin_cond (&IF_COND (r));
723 return r;
724 }
725
726 /* Process the COND of an if-statement, which may be given by
727 IF_STMT. */
728
729 tree
730 finish_if_stmt_cond (tree cond, tree if_stmt)
731 {
732 cond = maybe_convert_cond (cond);
733 if (IF_STMT_CONSTEXPR_P (if_stmt)
734 && !type_dependent_expression_p (cond)
735 && require_constant_expression (cond)
736 && !instantiation_dependent_expression_p (cond)
737 /* Wait until instantiation time, since only then COND has been
738 converted to bool. */
739 && TYPE_MAIN_VARIANT (TREE_TYPE (cond)) == boolean_type_node)
740 {
741 cond = instantiate_non_dependent_expr (cond);
742 cond = cxx_constant_value (cond, NULL_TREE);
743 }
744 finish_cond (&IF_COND (if_stmt), cond);
745 add_stmt (if_stmt);
746 THEN_CLAUSE (if_stmt) = push_stmt_list ();
747 return cond;
748 }
749
750 /* Finish the then-clause of an if-statement, which may be given by
751 IF_STMT. */
752
753 tree
754 finish_then_clause (tree if_stmt)
755 {
756 THEN_CLAUSE (if_stmt) = pop_stmt_list (THEN_CLAUSE (if_stmt));
757 return if_stmt;
758 }
759
760 /* Begin the else-clause of an if-statement. */
761
762 void
763 begin_else_clause (tree if_stmt)
764 {
765 ELSE_CLAUSE (if_stmt) = push_stmt_list ();
766 }
767
768 /* Finish the else-clause of an if-statement, which may be given by
769 IF_STMT. */
770
771 void
772 finish_else_clause (tree if_stmt)
773 {
774 ELSE_CLAUSE (if_stmt) = pop_stmt_list (ELSE_CLAUSE (if_stmt));
775 }
776
777 /* Finish an if-statement. */
778
779 void
780 finish_if_stmt (tree if_stmt)
781 {
782 tree scope = IF_SCOPE (if_stmt);
783 IF_SCOPE (if_stmt) = NULL;
784 add_stmt (do_poplevel (scope));
785 }
786
787 /* Begin a while-statement. Returns a newly created WHILE_STMT if
788 appropriate. */
789
790 tree
791 begin_while_stmt (void)
792 {
793 tree r;
794 r = build_stmt (input_location, WHILE_STMT, NULL_TREE, NULL_TREE);
795 add_stmt (r);
796 WHILE_BODY (r) = do_pushlevel (sk_block);
797 begin_cond (&WHILE_COND (r));
798 return r;
799 }
800
801 /* Process the COND of a while-statement, which may be given by
802 WHILE_STMT. */
803
804 void
805 finish_while_stmt_cond (tree cond, tree while_stmt, bool ivdep,
806 unsigned short unroll)
807 {
808 cond = maybe_convert_cond (cond);
809 finish_cond (&WHILE_COND (while_stmt), cond);
810 begin_maybe_infinite_loop (cond);
811 if (ivdep && cond != error_mark_node)
812 WHILE_COND (while_stmt) = build3 (ANNOTATE_EXPR,
813 TREE_TYPE (WHILE_COND (while_stmt)),
814 WHILE_COND (while_stmt),
815 build_int_cst (integer_type_node,
816 annot_expr_ivdep_kind),
817 integer_zero_node);
818 if (unroll && cond != error_mark_node)
819 WHILE_COND (while_stmt) = build3 (ANNOTATE_EXPR,
820 TREE_TYPE (WHILE_COND (while_stmt)),
821 WHILE_COND (while_stmt),
822 build_int_cst (integer_type_node,
823 annot_expr_unroll_kind),
824 build_int_cst (integer_type_node,
825 unroll));
826 simplify_loop_decl_cond (&WHILE_COND (while_stmt), WHILE_BODY (while_stmt));
827 }
828
829 /* Finish a while-statement, which may be given by WHILE_STMT. */
830
831 void
832 finish_while_stmt (tree while_stmt)
833 {
834 end_maybe_infinite_loop (boolean_true_node);
835 WHILE_BODY (while_stmt) = do_poplevel (WHILE_BODY (while_stmt));
836 }
837
838 /* Begin a do-statement. Returns a newly created DO_STMT if
839 appropriate. */
840
841 tree
842 begin_do_stmt (void)
843 {
844 tree r = build_stmt (input_location, DO_STMT, NULL_TREE, NULL_TREE);
845 begin_maybe_infinite_loop (boolean_true_node);
846 add_stmt (r);
847 DO_BODY (r) = push_stmt_list ();
848 return r;
849 }
850
851 /* Finish the body of a do-statement, which may be given by DO_STMT. */
852
853 void
854 finish_do_body (tree do_stmt)
855 {
856 tree body = DO_BODY (do_stmt) = pop_stmt_list (DO_BODY (do_stmt));
857
858 if (TREE_CODE (body) == STATEMENT_LIST && STATEMENT_LIST_TAIL (body))
859 body = STATEMENT_LIST_TAIL (body)->stmt;
860
861 if (IS_EMPTY_STMT (body))
862 warning (OPT_Wempty_body,
863 "suggest explicit braces around empty body in %<do%> statement");
864 }
865
866 /* Finish a do-statement, which may be given by DO_STMT, and whose
867 COND is as indicated. */
868
869 void
870 finish_do_stmt (tree cond, tree do_stmt, bool ivdep, unsigned short unroll)
871 {
872 cond = maybe_convert_cond (cond);
873 end_maybe_infinite_loop (cond);
874 if (ivdep && cond != error_mark_node)
875 cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
876 build_int_cst (integer_type_node, annot_expr_ivdep_kind),
877 integer_zero_node);
878 if (unroll && cond != error_mark_node)
879 cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
880 build_int_cst (integer_type_node, annot_expr_unroll_kind),
881 build_int_cst (integer_type_node, unroll));
882 DO_COND (do_stmt) = cond;
883 }
884
885 /* Finish a return-statement. The EXPRESSION returned, if any, is as
886 indicated. */
887
888 tree
889 finish_return_stmt (tree expr)
890 {
891 tree r;
892 bool no_warning;
893
894 expr = check_return_expr (expr, &no_warning);
895
896 if (error_operand_p (expr)
897 || (flag_openmp && !check_omp_return ()))
898 {
899 /* Suppress -Wreturn-type for this function. */
900 if (warn_return_type)
901 TREE_NO_WARNING (current_function_decl) = true;
902 return error_mark_node;
903 }
904
905 if (!processing_template_decl)
906 {
907 if (warn_sequence_point)
908 verify_sequence_points (expr);
909
910 if (DECL_DESTRUCTOR_P (current_function_decl)
911 || (DECL_CONSTRUCTOR_P (current_function_decl)
912 && targetm.cxx.cdtor_returns_this ()))
913 {
914 /* Similarly, all destructors must run destructors for
915 base-classes before returning. So, all returns in a
916 destructor get sent to the DTOR_LABEL; finish_function emits
917 code to return a value there. */
918 return finish_goto_stmt (cdtor_label);
919 }
920 }
921
922 r = build_stmt (input_location, RETURN_EXPR, expr);
923 TREE_NO_WARNING (r) |= no_warning;
924 r = maybe_cleanup_point_expr_void (r);
925 r = add_stmt (r);
926
927 return r;
928 }
929
930 /* Begin the scope of a for-statement or a range-for-statement.
931 Both the returned trees are to be used in a call to
932 begin_for_stmt or begin_range_for_stmt. */
933
934 tree
935 begin_for_scope (tree *init)
936 {
937 tree scope = do_pushlevel (sk_for);
938
939 if (processing_template_decl)
940 *init = push_stmt_list ();
941 else
942 *init = NULL_TREE;
943
944 return scope;
945 }
946
947 /* Begin a for-statement. Returns a new FOR_STMT.
948 SCOPE and INIT should be the return of begin_for_scope,
949 or both NULL_TREE */
950
951 tree
952 begin_for_stmt (tree scope, tree init)
953 {
954 tree r;
955
956 r = build_stmt (input_location, FOR_STMT, NULL_TREE, NULL_TREE,
957 NULL_TREE, NULL_TREE, NULL_TREE);
958
959 if (scope == NULL_TREE)
960 {
961 gcc_assert (!init);
962 scope = begin_for_scope (&init);
963 }
964
965 FOR_INIT_STMT (r) = init;
966 FOR_SCOPE (r) = scope;
967
968 return r;
969 }
970
971 /* Finish the init-statement of a for-statement, which may be
972 given by FOR_STMT. */
973
974 void
975 finish_init_stmt (tree for_stmt)
976 {
977 if (processing_template_decl)
978 FOR_INIT_STMT (for_stmt) = pop_stmt_list (FOR_INIT_STMT (for_stmt));
979 add_stmt (for_stmt);
980 FOR_BODY (for_stmt) = do_pushlevel (sk_block);
981 begin_cond (&FOR_COND (for_stmt));
982 }
983
984 /* Finish the COND of a for-statement, which may be given by
985 FOR_STMT. */
986
987 void
988 finish_for_cond (tree cond, tree for_stmt, bool ivdep, unsigned short unroll)
989 {
990 cond = maybe_convert_cond (cond);
991 finish_cond (&FOR_COND (for_stmt), cond);
992 begin_maybe_infinite_loop (cond);
993 if (ivdep && cond != error_mark_node)
994 FOR_COND (for_stmt) = build3 (ANNOTATE_EXPR,
995 TREE_TYPE (FOR_COND (for_stmt)),
996 FOR_COND (for_stmt),
997 build_int_cst (integer_type_node,
998 annot_expr_ivdep_kind),
999 integer_zero_node);
1000 if (unroll && cond != error_mark_node)
1001 FOR_COND (for_stmt) = build3 (ANNOTATE_EXPR,
1002 TREE_TYPE (FOR_COND (for_stmt)),
1003 FOR_COND (for_stmt),
1004 build_int_cst (integer_type_node,
1005 annot_expr_unroll_kind),
1006 build_int_cst (integer_type_node,
1007 unroll));
1008 simplify_loop_decl_cond (&FOR_COND (for_stmt), FOR_BODY (for_stmt));
1009 }
1010
1011 /* Finish the increment-EXPRESSION in a for-statement, which may be
1012 given by FOR_STMT. */
1013
1014 void
1015 finish_for_expr (tree expr, tree for_stmt)
1016 {
1017 if (!expr)
1018 return;
1019 /* If EXPR is an overloaded function, issue an error; there is no
1020 context available to use to perform overload resolution. */
1021 if (type_unknown_p (expr))
1022 {
1023 cxx_incomplete_type_error (expr, TREE_TYPE (expr));
1024 expr = error_mark_node;
1025 }
1026 if (!processing_template_decl)
1027 {
1028 if (warn_sequence_point)
1029 verify_sequence_points (expr);
1030 expr = convert_to_void (expr, ICV_THIRD_IN_FOR,
1031 tf_warning_or_error);
1032 }
1033 else if (!type_dependent_expression_p (expr))
1034 convert_to_void (build_non_dependent_expr (expr), ICV_THIRD_IN_FOR,
1035 tf_warning_or_error);
1036 expr = maybe_cleanup_point_expr_void (expr);
1037 if (check_for_bare_parameter_packs (expr))
1038 expr = error_mark_node;
1039 FOR_EXPR (for_stmt) = expr;
1040 }
1041
1042 /* Finish the body of a for-statement, which may be given by
1043 FOR_STMT. The increment-EXPR for the loop must be
1044 provided.
1045 It can also finish RANGE_FOR_STMT. */
1046
1047 void
1048 finish_for_stmt (tree for_stmt)
1049 {
1050 end_maybe_infinite_loop (boolean_true_node);
1051
1052 if (TREE_CODE (for_stmt) == RANGE_FOR_STMT)
1053 RANGE_FOR_BODY (for_stmt) = do_poplevel (RANGE_FOR_BODY (for_stmt));
1054 else
1055 FOR_BODY (for_stmt) = do_poplevel (FOR_BODY (for_stmt));
1056
1057 /* Pop the scope for the body of the loop. */
1058 tree *scope_ptr = (TREE_CODE (for_stmt) == RANGE_FOR_STMT
1059 ? &RANGE_FOR_SCOPE (for_stmt)
1060 : &FOR_SCOPE (for_stmt));
1061 tree scope = *scope_ptr;
1062 *scope_ptr = NULL;
1063
1064 /* During parsing of the body, range for uses "__for_{range,begin,end} "
1065 decl names to make those unaccessible by code in the body.
1066 Change it to ones with underscore instead of space, so that it can
1067 be inspected in the debugger. */
1068 tree range_for_decl[3] = { NULL_TREE, NULL_TREE, NULL_TREE };
1069 gcc_assert (CPTI_FOR_BEGIN__IDENTIFIER == CPTI_FOR_RANGE__IDENTIFIER + 1
1070 && CPTI_FOR_END__IDENTIFIER == CPTI_FOR_RANGE__IDENTIFIER + 2
1071 && CPTI_FOR_RANGE_IDENTIFIER == CPTI_FOR_RANGE__IDENTIFIER + 3
1072 && CPTI_FOR_BEGIN_IDENTIFIER == CPTI_FOR_BEGIN__IDENTIFIER + 3
1073 && CPTI_FOR_END_IDENTIFIER == CPTI_FOR_END__IDENTIFIER + 3);
1074 for (int i = 0; i < 3; i++)
1075 {
1076 tree id = cp_global_trees[CPTI_FOR_RANGE__IDENTIFIER + i];
1077 if (IDENTIFIER_BINDING (id)
1078 && IDENTIFIER_BINDING (id)->scope == current_binding_level)
1079 {
1080 range_for_decl[i] = IDENTIFIER_BINDING (id)->value;
1081 gcc_assert (VAR_P (range_for_decl[i])
1082 && DECL_ARTIFICIAL (range_for_decl[i]));
1083 }
1084 }
1085
1086 add_stmt (do_poplevel (scope));
1087
1088 for (int i = 0; i < 3; i++)
1089 if (range_for_decl[i])
1090 DECL_NAME (range_for_decl[i])
1091 = cp_global_trees[CPTI_FOR_RANGE_IDENTIFIER + i];
1092 }
1093
1094 /* Begin a range-for-statement. Returns a new RANGE_FOR_STMT.
1095 SCOPE and INIT should be the return of begin_for_scope,
1096 or both NULL_TREE .
1097 To finish it call finish_for_stmt(). */
1098
1099 tree
1100 begin_range_for_stmt (tree scope, tree init)
1101 {
1102 begin_maybe_infinite_loop (boolean_false_node);
1103
1104 tree r = build_stmt (input_location, RANGE_FOR_STMT, NULL_TREE, NULL_TREE,
1105 NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE);
1106
1107 if (scope == NULL_TREE)
1108 {
1109 gcc_assert (!init);
1110 scope = begin_for_scope (&init);
1111 }
1112
1113 /* Since C++20, RANGE_FOR_STMTs can use the init tree, so save it. */
1114 RANGE_FOR_INIT_STMT (r) = init;
1115 RANGE_FOR_SCOPE (r) = scope;
1116
1117 return r;
1118 }
1119
1120 /* Finish the head of a range-based for statement, which may
1121 be given by RANGE_FOR_STMT. DECL must be the declaration
1122 and EXPR must be the loop expression. */
1123
1124 void
1125 finish_range_for_decl (tree range_for_stmt, tree decl, tree expr)
1126 {
1127 if (processing_template_decl)
1128 RANGE_FOR_INIT_STMT (range_for_stmt)
1129 = pop_stmt_list (RANGE_FOR_INIT_STMT (range_for_stmt));
1130 RANGE_FOR_DECL (range_for_stmt) = decl;
1131 RANGE_FOR_EXPR (range_for_stmt) = expr;
1132 add_stmt (range_for_stmt);
1133 RANGE_FOR_BODY (range_for_stmt) = do_pushlevel (sk_block);
1134 }
1135
1136 /* Finish a break-statement. */
1137
1138 tree
1139 finish_break_stmt (void)
1140 {
1141 /* In switch statements break is sometimes stylistically used after
1142 a return statement. This can lead to spurious warnings about
1143 control reaching the end of a non-void function when it is
1144 inlined. Note that we are calling block_may_fallthru with
1145 language specific tree nodes; this works because
1146 block_may_fallthru returns true when given something it does not
1147 understand. */
1148 if (!block_may_fallthru (cur_stmt_list))
1149 return void_node;
1150 note_break_stmt ();
1151 return add_stmt (build_stmt (input_location, BREAK_STMT));
1152 }
1153
1154 /* Finish a continue-statement. */
1155
1156 tree
1157 finish_continue_stmt (void)
1158 {
1159 return add_stmt (build_stmt (input_location, CONTINUE_STMT));
1160 }
1161
1162 /* Begin a switch-statement. Returns a new SWITCH_STMT if
1163 appropriate. */
1164
1165 tree
1166 begin_switch_stmt (void)
1167 {
1168 tree r, scope;
1169
1170 scope = do_pushlevel (sk_cond);
1171 r = build_stmt (input_location, SWITCH_STMT, NULL_TREE, NULL_TREE, NULL_TREE, scope);
1172
1173 begin_cond (&SWITCH_STMT_COND (r));
1174
1175 return r;
1176 }
1177
1178 /* Finish the cond of a switch-statement. */
1179
1180 void
1181 finish_switch_cond (tree cond, tree switch_stmt)
1182 {
1183 tree orig_type = NULL;
1184
1185 if (!processing_template_decl)
1186 {
1187 /* Convert the condition to an integer or enumeration type. */
1188 cond = build_expr_type_conversion (WANT_INT | WANT_ENUM, cond, true);
1189 if (cond == NULL_TREE)
1190 {
1191 error ("switch quantity not an integer");
1192 cond = error_mark_node;
1193 }
1194 /* We want unlowered type here to handle enum bit-fields. */
1195 orig_type = unlowered_expr_type (cond);
1196 if (TREE_CODE (orig_type) != ENUMERAL_TYPE)
1197 orig_type = TREE_TYPE (cond);
1198 if (cond != error_mark_node)
1199 {
1200 /* [stmt.switch]
1201
1202 Integral promotions are performed. */
1203 cond = perform_integral_promotions (cond);
1204 cond = maybe_cleanup_point_expr (cond);
1205 }
1206 }
1207 if (check_for_bare_parameter_packs (cond))
1208 cond = error_mark_node;
1209 else if (!processing_template_decl && warn_sequence_point)
1210 verify_sequence_points (cond);
1211
1212 finish_cond (&SWITCH_STMT_COND (switch_stmt), cond);
1213 SWITCH_STMT_TYPE (switch_stmt) = orig_type;
1214 add_stmt (switch_stmt);
1215 push_switch (switch_stmt);
1216 SWITCH_STMT_BODY (switch_stmt) = push_stmt_list ();
1217 }
1218
1219 /* Finish the body of a switch-statement, which may be given by
1220 SWITCH_STMT. The COND to switch on is indicated. */
1221
1222 void
1223 finish_switch_stmt (tree switch_stmt)
1224 {
1225 tree scope;
1226
1227 SWITCH_STMT_BODY (switch_stmt) =
1228 pop_stmt_list (SWITCH_STMT_BODY (switch_stmt));
1229 pop_switch ();
1230
1231 scope = SWITCH_STMT_SCOPE (switch_stmt);
1232 SWITCH_STMT_SCOPE (switch_stmt) = NULL;
1233 add_stmt (do_poplevel (scope));
1234 }
1235
1236 /* Begin a try-block. Returns a newly-created TRY_BLOCK if
1237 appropriate. */
1238
1239 tree
1240 begin_try_block (void)
1241 {
1242 tree r = build_stmt (input_location, TRY_BLOCK, NULL_TREE, NULL_TREE);
1243 add_stmt (r);
1244 TRY_STMTS (r) = push_stmt_list ();
1245 return r;
1246 }
1247
1248 /* Likewise, for a function-try-block. The block returned in
1249 *COMPOUND_STMT is an artificial outer scope, containing the
1250 function-try-block. */
1251
1252 tree
1253 begin_function_try_block (tree *compound_stmt)
1254 {
1255 tree r;
1256 /* This outer scope does not exist in the C++ standard, but we need
1257 a place to put __FUNCTION__ and similar variables. */
1258 *compound_stmt = begin_compound_stmt (0);
1259 r = begin_try_block ();
1260 FN_TRY_BLOCK_P (r) = 1;
1261 return r;
1262 }
1263
1264 /* Finish a try-block, which may be given by TRY_BLOCK. */
1265
1266 void
1267 finish_try_block (tree try_block)
1268 {
1269 TRY_STMTS (try_block) = pop_stmt_list (TRY_STMTS (try_block));
1270 TRY_HANDLERS (try_block) = push_stmt_list ();
1271 }
1272
1273 /* Finish the body of a cleanup try-block, which may be given by
1274 TRY_BLOCK. */
1275
1276 void
1277 finish_cleanup_try_block (tree try_block)
1278 {
1279 TRY_STMTS (try_block) = pop_stmt_list (TRY_STMTS (try_block));
1280 }
1281
1282 /* Finish an implicitly generated try-block, with a cleanup is given
1283 by CLEANUP. */
1284
1285 void
1286 finish_cleanup (tree cleanup, tree try_block)
1287 {
1288 TRY_HANDLERS (try_block) = cleanup;
1289 CLEANUP_P (try_block) = 1;
1290 }
1291
1292 /* Likewise, for a function-try-block. */
1293
1294 void
1295 finish_function_try_block (tree try_block)
1296 {
1297 finish_try_block (try_block);
1298 /* FIXME : something queer about CTOR_INITIALIZER somehow following
1299 the try block, but moving it inside. */
1300 in_function_try_handler = 1;
1301 }
1302
1303 /* Finish a handler-sequence for a try-block, which may be given by
1304 TRY_BLOCK. */
1305
1306 void
1307 finish_handler_sequence (tree try_block)
1308 {
1309 TRY_HANDLERS (try_block) = pop_stmt_list (TRY_HANDLERS (try_block));
1310 check_handlers (TRY_HANDLERS (try_block));
1311 }
1312
1313 /* Finish the handler-seq for a function-try-block, given by
1314 TRY_BLOCK. COMPOUND_STMT is the outer block created by
1315 begin_function_try_block. */
1316
1317 void
1318 finish_function_handler_sequence (tree try_block, tree compound_stmt)
1319 {
1320 in_function_try_handler = 0;
1321 finish_handler_sequence (try_block);
1322 finish_compound_stmt (compound_stmt);
1323 }
1324
1325 /* Begin a handler. Returns a HANDLER if appropriate. */
1326
1327 tree
1328 begin_handler (void)
1329 {
1330 tree r;
1331
1332 r = build_stmt (input_location, HANDLER, NULL_TREE, NULL_TREE);
1333 add_stmt (r);
1334
1335 /* Create a binding level for the eh_info and the exception object
1336 cleanup. */
1337 HANDLER_BODY (r) = do_pushlevel (sk_catch);
1338
1339 return r;
1340 }
1341
1342 /* Finish the handler-parameters for a handler, which may be given by
1343 HANDLER. DECL is the declaration for the catch parameter, or NULL
1344 if this is a `catch (...)' clause. */
1345
1346 void
1347 finish_handler_parms (tree decl, tree handler)
1348 {
1349 tree type = NULL_TREE;
1350 if (processing_template_decl)
1351 {
1352 if (decl)
1353 {
1354 decl = pushdecl (decl);
1355 decl = push_template_decl (decl);
1356 HANDLER_PARMS (handler) = decl;
1357 type = TREE_TYPE (decl);
1358 }
1359 }
1360 else
1361 {
1362 type = expand_start_catch_block (decl);
1363 if (warn_catch_value
1364 && type != NULL_TREE
1365 && type != error_mark_node
1366 && !TYPE_REF_P (TREE_TYPE (decl)))
1367 {
1368 tree orig_type = TREE_TYPE (decl);
1369 if (CLASS_TYPE_P (orig_type))
1370 {
1371 if (TYPE_POLYMORPHIC_P (orig_type))
1372 warning (OPT_Wcatch_value_,
1373 "catching polymorphic type %q#T by value", orig_type);
1374 else if (warn_catch_value > 1)
1375 warning (OPT_Wcatch_value_,
1376 "catching type %q#T by value", orig_type);
1377 }
1378 else if (warn_catch_value > 2)
1379 warning (OPT_Wcatch_value_,
1380 "catching non-reference type %q#T", orig_type);
1381 }
1382 }
1383 HANDLER_TYPE (handler) = type;
1384 }
1385
1386 /* Finish a handler, which may be given by HANDLER. The BLOCKs are
1387 the return value from the matching call to finish_handler_parms. */
1388
1389 void
1390 finish_handler (tree handler)
1391 {
1392 if (!processing_template_decl)
1393 expand_end_catch_block ();
1394 HANDLER_BODY (handler) = do_poplevel (HANDLER_BODY (handler));
1395 }
1396
1397 /* Begin a compound statement. FLAGS contains some bits that control the
1398 behavior and context. If BCS_NO_SCOPE is set, the compound statement
1399 does not define a scope. If BCS_FN_BODY is set, this is the outermost
1400 block of a function. If BCS_TRY_BLOCK is set, this is the block
1401 created on behalf of a TRY statement. Returns a token to be passed to
1402 finish_compound_stmt. */
1403
1404 tree
1405 begin_compound_stmt (unsigned int flags)
1406 {
1407 tree r;
1408
1409 if (flags & BCS_NO_SCOPE)
1410 {
1411 r = push_stmt_list ();
1412 STATEMENT_LIST_NO_SCOPE (r) = 1;
1413
1414 /* Normally, we try hard to keep the BLOCK for a statement-expression.
1415 But, if it's a statement-expression with a scopeless block, there's
1416 nothing to keep, and we don't want to accidentally keep a block
1417 *inside* the scopeless block. */
1418 keep_next_level (false);
1419 }
1420 else
1421 {
1422 scope_kind sk = sk_block;
1423 if (flags & BCS_TRY_BLOCK)
1424 sk = sk_try;
1425 else if (flags & BCS_TRANSACTION)
1426 sk = sk_transaction;
1427 r = do_pushlevel (sk);
1428 }
1429
1430 /* When processing a template, we need to remember where the braces were,
1431 so that we can set up identical scopes when instantiating the template
1432 later. BIND_EXPR is a handy candidate for this.
1433 Note that do_poplevel won't create a BIND_EXPR itself here (and thus
1434 result in nested BIND_EXPRs), since we don't build BLOCK nodes when
1435 processing templates. */
1436 if (processing_template_decl)
1437 {
1438 r = build3 (BIND_EXPR, NULL, NULL, r, NULL);
1439 BIND_EXPR_TRY_BLOCK (r) = (flags & BCS_TRY_BLOCK) != 0;
1440 BIND_EXPR_BODY_BLOCK (r) = (flags & BCS_FN_BODY) != 0;
1441 TREE_SIDE_EFFECTS (r) = 1;
1442 }
1443
1444 return r;
1445 }
1446
1447 /* Finish a compound-statement, which is given by STMT. */
1448
1449 void
1450 finish_compound_stmt (tree stmt)
1451 {
1452 if (TREE_CODE (stmt) == BIND_EXPR)
1453 {
1454 tree body = do_poplevel (BIND_EXPR_BODY (stmt));
1455 /* If the STATEMENT_LIST is empty and this BIND_EXPR isn't special,
1456 discard the BIND_EXPR so it can be merged with the containing
1457 STATEMENT_LIST. */
1458 if (TREE_CODE (body) == STATEMENT_LIST
1459 && STATEMENT_LIST_HEAD (body) == NULL
1460 && !BIND_EXPR_BODY_BLOCK (stmt)
1461 && !BIND_EXPR_TRY_BLOCK (stmt))
1462 stmt = body;
1463 else
1464 BIND_EXPR_BODY (stmt) = body;
1465 }
1466 else if (STATEMENT_LIST_NO_SCOPE (stmt))
1467 stmt = pop_stmt_list (stmt);
1468 else
1469 {
1470 /* Destroy any ObjC "super" receivers that may have been
1471 created. */
1472 objc_clear_super_receiver ();
1473
1474 stmt = do_poplevel (stmt);
1475 }
1476
1477 /* ??? See c_end_compound_stmt wrt statement expressions. */
1478 add_stmt (stmt);
1479 }
1480
1481 /* Finish an asm-statement, whose components are a STRING, some
1482 OUTPUT_OPERANDS, some INPUT_OPERANDS, some CLOBBERS and some
1483 LABELS. Also note whether the asm-statement should be
1484 considered volatile, and whether it is asm inline. */
1485
1486 tree
1487 finish_asm_stmt (int volatile_p, tree string, tree output_operands,
1488 tree input_operands, tree clobbers, tree labels, bool inline_p)
1489 {
1490 tree r;
1491 tree t;
1492 int ninputs = list_length (input_operands);
1493 int noutputs = list_length (output_operands);
1494
1495 if (!processing_template_decl)
1496 {
1497 const char *constraint;
1498 const char **oconstraints;
1499 bool allows_mem, allows_reg, is_inout;
1500 tree operand;
1501 int i;
1502
1503 oconstraints = XALLOCAVEC (const char *, noutputs);
1504
1505 string = resolve_asm_operand_names (string, output_operands,
1506 input_operands, labels);
1507
1508 for (i = 0, t = output_operands; t; t = TREE_CHAIN (t), ++i)
1509 {
1510 operand = TREE_VALUE (t);
1511
1512 /* ??? Really, this should not be here. Users should be using a
1513 proper lvalue, dammit. But there's a long history of using
1514 casts in the output operands. In cases like longlong.h, this
1515 becomes a primitive form of typechecking -- if the cast can be
1516 removed, then the output operand had a type of the proper width;
1517 otherwise we'll get an error. Gross, but ... */
1518 STRIP_NOPS (operand);
1519
1520 operand = mark_lvalue_use (operand);
1521
1522 if (!lvalue_or_else (operand, lv_asm, tf_warning_or_error))
1523 operand = error_mark_node;
1524
1525 if (operand != error_mark_node
1526 && (TREE_READONLY (operand)
1527 || CP_TYPE_CONST_P (TREE_TYPE (operand))
1528 /* Functions are not modifiable, even though they are
1529 lvalues. */
1530 || TREE_CODE (TREE_TYPE (operand)) == FUNCTION_TYPE
1531 || TREE_CODE (TREE_TYPE (operand)) == METHOD_TYPE
1532 /* If it's an aggregate and any field is const, then it is
1533 effectively const. */
1534 || (CLASS_TYPE_P (TREE_TYPE (operand))
1535 && C_TYPE_FIELDS_READONLY (TREE_TYPE (operand)))))
1536 cxx_readonly_error (input_location, operand, lv_asm);
1537
1538 tree *op = &operand;
1539 while (TREE_CODE (*op) == COMPOUND_EXPR)
1540 op = &TREE_OPERAND (*op, 1);
1541 switch (TREE_CODE (*op))
1542 {
1543 case PREINCREMENT_EXPR:
1544 case PREDECREMENT_EXPR:
1545 case MODIFY_EXPR:
1546 *op = genericize_compound_lvalue (*op);
1547 op = &TREE_OPERAND (*op, 1);
1548 break;
1549 default:
1550 break;
1551 }
1552
1553 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
1554 oconstraints[i] = constraint;
1555
1556 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
1557 &allows_mem, &allows_reg, &is_inout))
1558 {
1559 /* If the operand is going to end up in memory,
1560 mark it addressable. */
1561 if (!allows_reg && !cxx_mark_addressable (*op))
1562 operand = error_mark_node;
1563 }
1564 else
1565 operand = error_mark_node;
1566
1567 TREE_VALUE (t) = operand;
1568 }
1569
1570 for (i = 0, t = input_operands; t; ++i, t = TREE_CHAIN (t))
1571 {
1572 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
1573 bool constraint_parsed
1574 = parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
1575 oconstraints, &allows_mem, &allows_reg);
1576 /* If the operand is going to end up in memory, don't call
1577 decay_conversion. */
1578 if (constraint_parsed && !allows_reg && allows_mem)
1579 operand = mark_lvalue_use (TREE_VALUE (t));
1580 else
1581 operand = decay_conversion (TREE_VALUE (t), tf_warning_or_error);
1582
1583 /* If the type of the operand hasn't been determined (e.g.,
1584 because it involves an overloaded function), then issue
1585 an error message. There's no context available to
1586 resolve the overloading. */
1587 if (TREE_TYPE (operand) == unknown_type_node)
1588 {
1589 error ("type of asm operand %qE could not be determined",
1590 TREE_VALUE (t));
1591 operand = error_mark_node;
1592 }
1593
1594 if (constraint_parsed)
1595 {
1596 /* If the operand is going to end up in memory,
1597 mark it addressable. */
1598 if (!allows_reg && allows_mem)
1599 {
1600 /* Strip the nops as we allow this case. FIXME, this really
1601 should be rejected or made deprecated. */
1602 STRIP_NOPS (operand);
1603
1604 tree *op = &operand;
1605 while (TREE_CODE (*op) == COMPOUND_EXPR)
1606 op = &TREE_OPERAND (*op, 1);
1607 switch (TREE_CODE (*op))
1608 {
1609 case PREINCREMENT_EXPR:
1610 case PREDECREMENT_EXPR:
1611 case MODIFY_EXPR:
1612 *op = genericize_compound_lvalue (*op);
1613 op = &TREE_OPERAND (*op, 1);
1614 break;
1615 default:
1616 break;
1617 }
1618
1619 if (!cxx_mark_addressable (*op))
1620 operand = error_mark_node;
1621 }
1622 else if (!allows_reg && !allows_mem)
1623 {
1624 /* If constraint allows neither register nor memory,
1625 try harder to get a constant. */
1626 tree constop = maybe_constant_value (operand);
1627 if (TREE_CONSTANT (constop))
1628 operand = constop;
1629 }
1630 }
1631 else
1632 operand = error_mark_node;
1633
1634 TREE_VALUE (t) = operand;
1635 }
1636 }
1637
1638 r = build_stmt (input_location, ASM_EXPR, string,
1639 output_operands, input_operands,
1640 clobbers, labels);
1641 ASM_VOLATILE_P (r) = volatile_p || noutputs == 0;
1642 ASM_INLINE_P (r) = inline_p;
1643 r = maybe_cleanup_point_expr_void (r);
1644 return add_stmt (r);
1645 }
1646
1647 /* Finish a label with the indicated NAME. Returns the new label. */
1648
1649 tree
1650 finish_label_stmt (tree name)
1651 {
1652 tree decl = define_label (input_location, name);
1653
1654 if (decl == error_mark_node)
1655 return error_mark_node;
1656
1657 add_stmt (build_stmt (input_location, LABEL_EXPR, decl));
1658
1659 return decl;
1660 }
1661
1662 /* Finish a series of declarations for local labels. G++ allows users
1663 to declare "local" labels, i.e., labels with scope. This extension
1664 is useful when writing code involving statement-expressions. */
1665
1666 void
1667 finish_label_decl (tree name)
1668 {
1669 if (!at_function_scope_p ())
1670 {
1671 error ("__label__ declarations are only allowed in function scopes");
1672 return;
1673 }
1674
1675 add_decl_expr (declare_local_label (name));
1676 }
1677
1678 /* When DECL goes out of scope, make sure that CLEANUP is executed. */
1679
1680 void
1681 finish_decl_cleanup (tree decl, tree cleanup)
1682 {
1683 push_cleanup (decl, cleanup, false);
1684 }
1685
1686 /* If the current scope exits with an exception, run CLEANUP. */
1687
1688 void
1689 finish_eh_cleanup (tree cleanup)
1690 {
1691 push_cleanup (NULL, cleanup, true);
1692 }
1693
1694 /* The MEM_INITS is a list of mem-initializers, in reverse of the
1695 order they were written by the user. Each node is as for
1696 emit_mem_initializers. */
1697
1698 void
1699 finish_mem_initializers (tree mem_inits)
1700 {
1701 /* Reorder the MEM_INITS so that they are in the order they appeared
1702 in the source program. */
1703 mem_inits = nreverse (mem_inits);
1704
1705 if (processing_template_decl)
1706 {
1707 tree mem;
1708
1709 for (mem = mem_inits; mem; mem = TREE_CHAIN (mem))
1710 {
1711 /* If the TREE_PURPOSE is a TYPE_PACK_EXPANSION, skip the
1712 check for bare parameter packs in the TREE_VALUE, because
1713 any parameter packs in the TREE_VALUE have already been
1714 bound as part of the TREE_PURPOSE. See
1715 make_pack_expansion for more information. */
1716 if (TREE_CODE (TREE_PURPOSE (mem)) != TYPE_PACK_EXPANSION
1717 && check_for_bare_parameter_packs (TREE_VALUE (mem)))
1718 TREE_VALUE (mem) = error_mark_node;
1719 }
1720
1721 add_stmt (build_min_nt_loc (UNKNOWN_LOCATION,
1722 CTOR_INITIALIZER, mem_inits));
1723 }
1724 else
1725 emit_mem_initializers (mem_inits);
1726 }
1727
1728 /* Obfuscate EXPR if it looks like an id-expression or member access so
1729 that the call to finish_decltype in do_auto_deduction will give the
1730 right result. */
1731
1732 tree
1733 force_paren_expr (tree expr)
1734 {
1735 /* This is only needed for decltype(auto) in C++14. */
1736 if (cxx_dialect < cxx14)
1737 return expr;
1738
1739 /* If we're in unevaluated context, we can't be deducing a
1740 return/initializer type, so we don't need to mess with this. */
1741 if (cp_unevaluated_operand)
1742 return expr;
1743
1744 if (!DECL_P (tree_strip_any_location_wrapper (expr))
1745 && TREE_CODE (expr) != COMPONENT_REF
1746 && TREE_CODE (expr) != SCOPE_REF)
1747 return expr;
1748
1749 if (TREE_CODE (expr) == COMPONENT_REF
1750 || TREE_CODE (expr) == SCOPE_REF)
1751 REF_PARENTHESIZED_P (expr) = true;
1752 else if (processing_template_decl)
1753 expr = build1 (PAREN_EXPR, TREE_TYPE (expr), expr);
1754 else
1755 {
1756 expr = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (expr), expr);
1757 REF_PARENTHESIZED_P (expr) = true;
1758 }
1759
1760 return expr;
1761 }
1762
1763 /* If T is an id-expression obfuscated by force_paren_expr, undo the
1764 obfuscation and return the underlying id-expression. Otherwise
1765 return T. */
1766
1767 tree
1768 maybe_undo_parenthesized_ref (tree t)
1769 {
1770 if (cxx_dialect < cxx14)
1771 return t;
1772
1773 if (INDIRECT_REF_P (t) && REF_PARENTHESIZED_P (t))
1774 {
1775 t = TREE_OPERAND (t, 0);
1776 while (TREE_CODE (t) == NON_LVALUE_EXPR
1777 || TREE_CODE (t) == NOP_EXPR)
1778 t = TREE_OPERAND (t, 0);
1779
1780 gcc_assert (TREE_CODE (t) == ADDR_EXPR
1781 || TREE_CODE (t) == STATIC_CAST_EXPR);
1782 t = TREE_OPERAND (t, 0);
1783 }
1784 else if (TREE_CODE (t) == PAREN_EXPR)
1785 t = TREE_OPERAND (t, 0);
1786 else if (TREE_CODE (t) == VIEW_CONVERT_EXPR
1787 && REF_PARENTHESIZED_P (t))
1788 t = TREE_OPERAND (t, 0);
1789
1790 return t;
1791 }
1792
1793 /* Finish a parenthesized expression EXPR. */
1794
1795 cp_expr
1796 finish_parenthesized_expr (cp_expr expr)
1797 {
1798 if (EXPR_P (expr))
1799 /* This inhibits warnings in c_common_truthvalue_conversion. */
1800 TREE_NO_WARNING (expr) = 1;
1801
1802 if (TREE_CODE (expr) == OFFSET_REF
1803 || TREE_CODE (expr) == SCOPE_REF)
1804 /* [expr.unary.op]/3 The qualified id of a pointer-to-member must not be
1805 enclosed in parentheses. */
1806 PTRMEM_OK_P (expr) = 0;
1807
1808 tree stripped_expr = tree_strip_any_location_wrapper (expr);
1809 if (TREE_CODE (stripped_expr) == STRING_CST)
1810 PAREN_STRING_LITERAL_P (stripped_expr) = 1;
1811
1812 expr = cp_expr (force_paren_expr (expr), expr.get_location ());
1813
1814 return expr;
1815 }
1816
1817 /* Finish a reference to a non-static data member (DECL) that is not
1818 preceded by `.' or `->'. */
1819
1820 tree
1821 finish_non_static_data_member (tree decl, tree object, tree qualifying_scope)
1822 {
1823 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
1824 bool try_omp_private = !object && omp_private_member_map;
1825 tree ret;
1826
1827 if (!object)
1828 {
1829 tree scope = qualifying_scope;
1830 if (scope == NULL_TREE)
1831 {
1832 scope = context_for_name_lookup (decl);
1833 if (!TYPE_P (scope))
1834 {
1835 /* Can happen during error recovery (c++/85014). */
1836 gcc_assert (seen_error ());
1837 return error_mark_node;
1838 }
1839 }
1840 object = maybe_dummy_object (scope, NULL);
1841 }
1842
1843 object = maybe_resolve_dummy (object, true);
1844 if (object == error_mark_node)
1845 return error_mark_node;
1846
1847 /* DR 613/850: Can use non-static data members without an associated
1848 object in sizeof/decltype/alignof. */
1849 if (is_dummy_object (object) && cp_unevaluated_operand == 0
1850 && (!processing_template_decl || !current_class_ref))
1851 {
1852 if (current_function_decl
1853 && DECL_STATIC_FUNCTION_P (current_function_decl))
1854 error ("invalid use of member %qD in static member function", decl);
1855 else
1856 error ("invalid use of non-static data member %qD", decl);
1857 inform (DECL_SOURCE_LOCATION (decl), "declared here");
1858
1859 return error_mark_node;
1860 }
1861
1862 if (current_class_ptr)
1863 TREE_USED (current_class_ptr) = 1;
1864 if (processing_template_decl && !qualifying_scope)
1865 {
1866 tree type = TREE_TYPE (decl);
1867
1868 if (TYPE_REF_P (type))
1869 /* Quals on the object don't matter. */;
1870 else if (PACK_EXPANSION_P (type))
1871 /* Don't bother trying to represent this. */
1872 type = NULL_TREE;
1873 else
1874 {
1875 /* Set the cv qualifiers. */
1876 int quals = cp_type_quals (TREE_TYPE (object));
1877
1878 if (DECL_MUTABLE_P (decl))
1879 quals &= ~TYPE_QUAL_CONST;
1880
1881 quals |= cp_type_quals (TREE_TYPE (decl));
1882 type = cp_build_qualified_type (type, quals);
1883 }
1884
1885 ret = (convert_from_reference
1886 (build_min (COMPONENT_REF, type, object, decl, NULL_TREE)));
1887 }
1888 /* If PROCESSING_TEMPLATE_DECL is nonzero here, then
1889 QUALIFYING_SCOPE is also non-null. Wrap this in a SCOPE_REF
1890 for now. */
1891 else if (processing_template_decl)
1892 ret = build_qualified_name (TREE_TYPE (decl),
1893 qualifying_scope,
1894 decl,
1895 /*template_p=*/false);
1896 else
1897 {
1898 tree access_type = TREE_TYPE (object);
1899
1900 perform_or_defer_access_check (TYPE_BINFO (access_type), decl,
1901 decl, tf_warning_or_error);
1902
1903 /* If the data member was named `C::M', convert `*this' to `C'
1904 first. */
1905 if (qualifying_scope)
1906 {
1907 tree binfo = NULL_TREE;
1908 object = build_scoped_ref (object, qualifying_scope,
1909 &binfo);
1910 }
1911
1912 ret = build_class_member_access_expr (object, decl,
1913 /*access_path=*/NULL_TREE,
1914 /*preserve_reference=*/false,
1915 tf_warning_or_error);
1916 }
1917 if (try_omp_private)
1918 {
1919 tree *v = omp_private_member_map->get (decl);
1920 if (v)
1921 ret = convert_from_reference (*v);
1922 }
1923 return ret;
1924 }
1925
1926 /* If we are currently parsing a template and we encountered a typedef
1927 TYPEDEF_DECL that is being accessed though CONTEXT, this function
1928 adds the typedef to a list tied to the current template.
1929 At template instantiation time, that list is walked and access check
1930 performed for each typedef.
1931 LOCATION is the location of the usage point of TYPEDEF_DECL. */
1932
1933 void
1934 add_typedef_to_current_template_for_access_check (tree typedef_decl,
1935 tree context,
1936 location_t location)
1937 {
1938 tree template_info = NULL;
1939 tree cs = current_scope ();
1940
1941 if (!is_typedef_decl (typedef_decl)
1942 || !context
1943 || !CLASS_TYPE_P (context)
1944 || !cs)
1945 return;
1946
1947 if (CLASS_TYPE_P (cs) || TREE_CODE (cs) == FUNCTION_DECL)
1948 template_info = get_template_info (cs);
1949
1950 if (template_info
1951 && TI_TEMPLATE (template_info)
1952 && !currently_open_class (context))
1953 append_type_to_template_for_access_check (cs, typedef_decl,
1954 context, location);
1955 }
1956
1957 /* DECL was the declaration to which a qualified-id resolved. Issue
1958 an error message if it is not accessible. If OBJECT_TYPE is
1959 non-NULL, we have just seen `x->' or `x.' and OBJECT_TYPE is the
1960 type of `*x', or `x', respectively. If the DECL was named as
1961 `A::B' then NESTED_NAME_SPECIFIER is `A'. */
1962
1963 void
1964 check_accessibility_of_qualified_id (tree decl,
1965 tree object_type,
1966 tree nested_name_specifier)
1967 {
1968 tree scope;
1969 tree qualifying_type = NULL_TREE;
1970
1971 /* If we are parsing a template declaration and if decl is a typedef,
1972 add it to a list tied to the template.
1973 At template instantiation time, that list will be walked and
1974 access check performed. */
1975 add_typedef_to_current_template_for_access_check (decl,
1976 nested_name_specifier
1977 ? nested_name_specifier
1978 : DECL_CONTEXT (decl),
1979 input_location);
1980
1981 /* If we're not checking, return immediately. */
1982 if (deferred_access_no_check)
1983 return;
1984
1985 /* Determine the SCOPE of DECL. */
1986 scope = context_for_name_lookup (decl);
1987 /* If the SCOPE is not a type, then DECL is not a member. */
1988 if (!TYPE_P (scope))
1989 return;
1990 /* Compute the scope through which DECL is being accessed. */
1991 if (object_type
1992 /* OBJECT_TYPE might not be a class type; consider:
1993
1994 class A { typedef int I; };
1995 I *p;
1996 p->A::I::~I();
1997
1998 In this case, we will have "A::I" as the DECL, but "I" as the
1999 OBJECT_TYPE. */
2000 && CLASS_TYPE_P (object_type)
2001 && DERIVED_FROM_P (scope, object_type))
2002 /* If we are processing a `->' or `.' expression, use the type of the
2003 left-hand side. */
2004 qualifying_type = object_type;
2005 else if (nested_name_specifier)
2006 {
2007 /* If the reference is to a non-static member of the
2008 current class, treat it as if it were referenced through
2009 `this'. */
2010 tree ct;
2011 if (DECL_NONSTATIC_MEMBER_P (decl)
2012 && current_class_ptr
2013 && DERIVED_FROM_P (scope, ct = current_nonlambda_class_type ()))
2014 qualifying_type = ct;
2015 /* Otherwise, use the type indicated by the
2016 nested-name-specifier. */
2017 else
2018 qualifying_type = nested_name_specifier;
2019 }
2020 else
2021 /* Otherwise, the name must be from the current class or one of
2022 its bases. */
2023 qualifying_type = currently_open_derived_class (scope);
2024
2025 if (qualifying_type
2026 /* It is possible for qualifying type to be a TEMPLATE_TYPE_PARM
2027 or similar in a default argument value. */
2028 && CLASS_TYPE_P (qualifying_type)
2029 && !dependent_type_p (qualifying_type))
2030 perform_or_defer_access_check (TYPE_BINFO (qualifying_type), decl,
2031 decl, tf_warning_or_error);
2032 }
2033
2034 /* EXPR is the result of a qualified-id. The QUALIFYING_CLASS was the
2035 class named to the left of the "::" operator. DONE is true if this
2036 expression is a complete postfix-expression; it is false if this
2037 expression is followed by '->', '[', '(', etc. ADDRESS_P is true
2038 iff this expression is the operand of '&'. TEMPLATE_P is true iff
2039 the qualified-id was of the form "A::template B". TEMPLATE_ARG_P
2040 is true iff this qualified name appears as a template argument. */
2041
2042 tree
2043 finish_qualified_id_expr (tree qualifying_class,
2044 tree expr,
2045 bool done,
2046 bool address_p,
2047 bool template_p,
2048 bool template_arg_p,
2049 tsubst_flags_t complain)
2050 {
2051 gcc_assert (TYPE_P (qualifying_class));
2052
2053 if (error_operand_p (expr))
2054 return error_mark_node;
2055
2056 if ((DECL_P (expr) || BASELINK_P (expr))
2057 && !mark_used (expr, complain))
2058 return error_mark_node;
2059
2060 if (template_p)
2061 {
2062 if (TREE_CODE (expr) == UNBOUND_CLASS_TEMPLATE)
2063 {
2064 /* cp_parser_lookup_name thought we were looking for a type,
2065 but we're actually looking for a declaration. */
2066 qualifying_class = TYPE_CONTEXT (expr);
2067 expr = TYPE_IDENTIFIER (expr);
2068 }
2069 else
2070 check_template_keyword (expr);
2071 }
2072
2073 /* If EXPR occurs as the operand of '&', use special handling that
2074 permits a pointer-to-member. */
2075 if (address_p && done)
2076 {
2077 if (TREE_CODE (expr) == SCOPE_REF)
2078 expr = TREE_OPERAND (expr, 1);
2079 expr = build_offset_ref (qualifying_class, expr,
2080 /*address_p=*/true, complain);
2081 return expr;
2082 }
2083
2084 /* No need to check access within an enum. */
2085 if (TREE_CODE (qualifying_class) == ENUMERAL_TYPE
2086 && TREE_CODE (expr) != IDENTIFIER_NODE)
2087 return expr;
2088
2089 /* Within the scope of a class, turn references to non-static
2090 members into expression of the form "this->...". */
2091 if (template_arg_p)
2092 /* But, within a template argument, we do not want make the
2093 transformation, as there is no "this" pointer. */
2094 ;
2095 else if (TREE_CODE (expr) == FIELD_DECL)
2096 {
2097 push_deferring_access_checks (dk_no_check);
2098 expr = finish_non_static_data_member (expr, NULL_TREE,
2099 qualifying_class);
2100 pop_deferring_access_checks ();
2101 }
2102 else if (BASELINK_P (expr))
2103 {
2104 /* See if any of the functions are non-static members. */
2105 /* If so, the expression may be relative to 'this'. */
2106 if ((type_dependent_expression_p (expr)
2107 || !shared_member_p (expr))
2108 && current_class_ptr
2109 && DERIVED_FROM_P (qualifying_class,
2110 current_nonlambda_class_type ()))
2111 expr = (build_class_member_access_expr
2112 (maybe_dummy_object (qualifying_class, NULL),
2113 expr,
2114 BASELINK_ACCESS_BINFO (expr),
2115 /*preserve_reference=*/false,
2116 complain));
2117 else if (done)
2118 /* The expression is a qualified name whose address is not
2119 being taken. */
2120 expr = build_offset_ref (qualifying_class, expr, /*address_p=*/false,
2121 complain);
2122 }
2123 else if (!template_p
2124 && TREE_CODE (expr) == TEMPLATE_DECL
2125 && !DECL_FUNCTION_TEMPLATE_P (expr))
2126 {
2127 if (complain & tf_error)
2128 error ("%qE missing template arguments", expr);
2129 return error_mark_node;
2130 }
2131 else
2132 {
2133 /* In a template, return a SCOPE_REF for most qualified-ids
2134 so that we can check access at instantiation time. But if
2135 we're looking at a member of the current instantiation, we
2136 know we have access and building up the SCOPE_REF confuses
2137 non-type template argument handling. */
2138 if (processing_template_decl
2139 && (!currently_open_class (qualifying_class)
2140 || TREE_CODE (expr) == IDENTIFIER_NODE
2141 || TREE_CODE (expr) == TEMPLATE_ID_EXPR
2142 || TREE_CODE (expr) == BIT_NOT_EXPR))
2143 expr = build_qualified_name (TREE_TYPE (expr),
2144 qualifying_class, expr,
2145 template_p);
2146 else if (tree wrap = maybe_get_tls_wrapper_call (expr))
2147 expr = wrap;
2148
2149 expr = convert_from_reference (expr);
2150 }
2151
2152 return expr;
2153 }
2154
2155 /* Begin a statement-expression. The value returned must be passed to
2156 finish_stmt_expr. */
2157
2158 tree
2159 begin_stmt_expr (void)
2160 {
2161 return push_stmt_list ();
2162 }
2163
2164 /* Process the final expression of a statement expression. EXPR can be
2165 NULL, if the final expression is empty. Return a STATEMENT_LIST
2166 containing all the statements in the statement-expression, or
2167 ERROR_MARK_NODE if there was an error. */
2168
2169 tree
2170 finish_stmt_expr_expr (tree expr, tree stmt_expr)
2171 {
2172 if (error_operand_p (expr))
2173 {
2174 /* The type of the statement-expression is the type of the last
2175 expression. */
2176 TREE_TYPE (stmt_expr) = error_mark_node;
2177 return error_mark_node;
2178 }
2179
2180 /* If the last statement does not have "void" type, then the value
2181 of the last statement is the value of the entire expression. */
2182 if (expr)
2183 {
2184 tree type = TREE_TYPE (expr);
2185
2186 if (type && type_unknown_p (type))
2187 {
2188 error ("a statement expression is an insufficient context"
2189 " for overload resolution");
2190 TREE_TYPE (stmt_expr) = error_mark_node;
2191 return error_mark_node;
2192 }
2193 else if (processing_template_decl)
2194 {
2195 expr = build_stmt (input_location, EXPR_STMT, expr);
2196 expr = add_stmt (expr);
2197 /* Mark the last statement so that we can recognize it as such at
2198 template-instantiation time. */
2199 EXPR_STMT_STMT_EXPR_RESULT (expr) = 1;
2200 }
2201 else if (VOID_TYPE_P (type))
2202 {
2203 /* Just treat this like an ordinary statement. */
2204 expr = finish_expr_stmt (expr);
2205 }
2206 else
2207 {
2208 /* It actually has a value we need to deal with. First, force it
2209 to be an rvalue so that we won't need to build up a copy
2210 constructor call later when we try to assign it to something. */
2211 expr = force_rvalue (expr, tf_warning_or_error);
2212 if (error_operand_p (expr))
2213 return error_mark_node;
2214
2215 /* Update for array-to-pointer decay. */
2216 type = TREE_TYPE (expr);
2217
2218 /* Wrap it in a CLEANUP_POINT_EXPR and add it to the list like a
2219 normal statement, but don't convert to void or actually add
2220 the EXPR_STMT. */
2221 if (TREE_CODE (expr) != CLEANUP_POINT_EXPR)
2222 expr = maybe_cleanup_point_expr (expr);
2223 add_stmt (expr);
2224 }
2225
2226 /* The type of the statement-expression is the type of the last
2227 expression. */
2228 TREE_TYPE (stmt_expr) = type;
2229 }
2230
2231 return stmt_expr;
2232 }
2233
2234 /* Finish a statement-expression. EXPR should be the value returned
2235 by the previous begin_stmt_expr. Returns an expression
2236 representing the statement-expression. */
2237
2238 tree
2239 finish_stmt_expr (tree stmt_expr, bool has_no_scope)
2240 {
2241 tree type;
2242 tree result;
2243
2244 if (error_operand_p (stmt_expr))
2245 {
2246 pop_stmt_list (stmt_expr);
2247 return error_mark_node;
2248 }
2249
2250 gcc_assert (TREE_CODE (stmt_expr) == STATEMENT_LIST);
2251
2252 type = TREE_TYPE (stmt_expr);
2253 result = pop_stmt_list (stmt_expr);
2254 TREE_TYPE (result) = type;
2255
2256 if (processing_template_decl)
2257 {
2258 result = build_min (STMT_EXPR, type, result);
2259 TREE_SIDE_EFFECTS (result) = 1;
2260 STMT_EXPR_NO_SCOPE (result) = has_no_scope;
2261 }
2262 else if (CLASS_TYPE_P (type))
2263 {
2264 /* Wrap the statement-expression in a TARGET_EXPR so that the
2265 temporary object created by the final expression is destroyed at
2266 the end of the full-expression containing the
2267 statement-expression. */
2268 result = force_target_expr (type, result, tf_warning_or_error);
2269 }
2270
2271 return result;
2272 }
2273
2274 /* Returns the expression which provides the value of STMT_EXPR. */
2275
2276 tree
2277 stmt_expr_value_expr (tree stmt_expr)
2278 {
2279 tree t = STMT_EXPR_STMT (stmt_expr);
2280
2281 if (TREE_CODE (t) == BIND_EXPR)
2282 t = BIND_EXPR_BODY (t);
2283
2284 if (TREE_CODE (t) == STATEMENT_LIST && STATEMENT_LIST_TAIL (t))
2285 t = STATEMENT_LIST_TAIL (t)->stmt;
2286
2287 if (TREE_CODE (t) == EXPR_STMT)
2288 t = EXPR_STMT_EXPR (t);
2289
2290 return t;
2291 }
2292
2293 /* Return TRUE iff EXPR_STMT is an empty list of
2294 expression statements. */
2295
2296 bool
2297 empty_expr_stmt_p (tree expr_stmt)
2298 {
2299 tree body = NULL_TREE;
2300
2301 if (expr_stmt == void_node)
2302 return true;
2303
2304 if (expr_stmt)
2305 {
2306 if (TREE_CODE (expr_stmt) == EXPR_STMT)
2307 body = EXPR_STMT_EXPR (expr_stmt);
2308 else if (TREE_CODE (expr_stmt) == STATEMENT_LIST)
2309 body = expr_stmt;
2310 }
2311
2312 if (body)
2313 {
2314 if (TREE_CODE (body) == STATEMENT_LIST)
2315 return tsi_end_p (tsi_start (body));
2316 else
2317 return empty_expr_stmt_p (body);
2318 }
2319 return false;
2320 }
2321
2322 /* Perform Koenig lookup. FN_EXPR is the postfix-expression representing
2323 the function (or functions) to call; ARGS are the arguments to the
2324 call. Returns the functions to be considered by overload resolution. */
2325
2326 cp_expr
2327 perform_koenig_lookup (cp_expr fn_expr, vec<tree, va_gc> *args,
2328 tsubst_flags_t complain)
2329 {
2330 tree identifier = NULL_TREE;
2331 tree functions = NULL_TREE;
2332 tree tmpl_args = NULL_TREE;
2333 bool template_id = false;
2334 location_t loc = fn_expr.get_location ();
2335 tree fn = fn_expr.get_value ();
2336
2337 STRIP_ANY_LOCATION_WRAPPER (fn);
2338
2339 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
2340 {
2341 /* Use a separate flag to handle null args. */
2342 template_id = true;
2343 tmpl_args = TREE_OPERAND (fn, 1);
2344 fn = TREE_OPERAND (fn, 0);
2345 }
2346
2347 /* Find the name of the overloaded function. */
2348 if (identifier_p (fn))
2349 identifier = fn;
2350 else
2351 {
2352 functions = fn;
2353 identifier = OVL_NAME (functions);
2354 }
2355
2356 /* A call to a namespace-scope function using an unqualified name.
2357
2358 Do Koenig lookup -- unless any of the arguments are
2359 type-dependent. */
2360 if (!any_type_dependent_arguments_p (args)
2361 && !any_dependent_template_arguments_p (tmpl_args))
2362 {
2363 fn = lookup_arg_dependent (identifier, functions, args);
2364 if (!fn)
2365 {
2366 /* The unqualified name could not be resolved. */
2367 if (complain & tf_error)
2368 fn = unqualified_fn_lookup_error (cp_expr (identifier, loc));
2369 else
2370 fn = identifier;
2371 }
2372 }
2373
2374 if (fn && template_id && fn != error_mark_node)
2375 fn = build2 (TEMPLATE_ID_EXPR, unknown_type_node, fn, tmpl_args);
2376
2377 return cp_expr (fn, loc);
2378 }
2379
2380 /* Generate an expression for `FN (ARGS)'. This may change the
2381 contents of ARGS.
2382
2383 If DISALLOW_VIRTUAL is true, the call to FN will be not generated
2384 as a virtual call, even if FN is virtual. (This flag is set when
2385 encountering an expression where the function name is explicitly
2386 qualified. For example a call to `X::f' never generates a virtual
2387 call.)
2388
2389 Returns code for the call. */
2390
2391 tree
2392 finish_call_expr (tree fn, vec<tree, va_gc> **args, bool disallow_virtual,
2393 bool koenig_p, tsubst_flags_t complain)
2394 {
2395 tree result;
2396 tree orig_fn;
2397 vec<tree, va_gc> *orig_args = *args;
2398
2399 if (fn == error_mark_node)
2400 return error_mark_node;
2401
2402 gcc_assert (!TYPE_P (fn));
2403
2404 /* If FN may be a FUNCTION_DECL obfuscated by force_paren_expr, undo
2405 it so that we can tell this is a call to a known function. */
2406 fn = maybe_undo_parenthesized_ref (fn);
2407
2408 STRIP_ANY_LOCATION_WRAPPER (fn);
2409
2410 orig_fn = fn;
2411
2412 if (processing_template_decl)
2413 {
2414 /* If FN is a local extern declaration or set thereof, look them up
2415 again at instantiation time. */
2416 if (is_overloaded_fn (fn))
2417 {
2418 tree ifn = get_first_fn (fn);
2419 if (TREE_CODE (ifn) == FUNCTION_DECL
2420 && DECL_LOCAL_FUNCTION_P (ifn))
2421 orig_fn = DECL_NAME (ifn);
2422 }
2423
2424 /* If the call expression is dependent, build a CALL_EXPR node
2425 with no type; type_dependent_expression_p recognizes
2426 expressions with no type as being dependent. */
2427 if (type_dependent_expression_p (fn)
2428 || any_type_dependent_arguments_p (*args))
2429 {
2430 result = build_min_nt_call_vec (orig_fn, *args);
2431 SET_EXPR_LOCATION (result, cp_expr_loc_or_loc (fn, input_location));
2432 KOENIG_LOOKUP_P (result) = koenig_p;
2433 if (is_overloaded_fn (fn))
2434 fn = get_fns (fn);
2435
2436 if (cfun)
2437 {
2438 bool abnormal = true;
2439 for (lkp_iterator iter (fn); abnormal && iter; ++iter)
2440 {
2441 tree fndecl = *iter;
2442 if (TREE_CODE (fndecl) != FUNCTION_DECL
2443 || !TREE_THIS_VOLATILE (fndecl))
2444 abnormal = false;
2445 }
2446 /* FIXME: Stop warning about falling off end of non-void
2447 function. But this is wrong. Even if we only see
2448 no-return fns at this point, we could select a
2449 future-defined return fn during instantiation. Or
2450 vice-versa. */
2451 if (abnormal)
2452 current_function_returns_abnormally = 1;
2453 }
2454 return result;
2455 }
2456 orig_args = make_tree_vector_copy (*args);
2457 if (!BASELINK_P (fn)
2458 && TREE_CODE (fn) != PSEUDO_DTOR_EXPR
2459 && TREE_TYPE (fn) != unknown_type_node)
2460 fn = build_non_dependent_expr (fn);
2461 make_args_non_dependent (*args);
2462 }
2463
2464 if (TREE_CODE (fn) == COMPONENT_REF)
2465 {
2466 tree member = TREE_OPERAND (fn, 1);
2467 if (BASELINK_P (member))
2468 {
2469 tree object = TREE_OPERAND (fn, 0);
2470 return build_new_method_call (object, member,
2471 args, NULL_TREE,
2472 (disallow_virtual
2473 ? LOOKUP_NORMAL | LOOKUP_NONVIRTUAL
2474 : LOOKUP_NORMAL),
2475 /*fn_p=*/NULL,
2476 complain);
2477 }
2478 }
2479
2480 /* Per 13.3.1.1, '(&f)(...)' is the same as '(f)(...)'. */
2481 if (TREE_CODE (fn) == ADDR_EXPR
2482 && TREE_CODE (TREE_OPERAND (fn, 0)) == OVERLOAD)
2483 fn = TREE_OPERAND (fn, 0);
2484
2485 if (is_overloaded_fn (fn))
2486 fn = baselink_for_fns (fn);
2487
2488 result = NULL_TREE;
2489 if (BASELINK_P (fn))
2490 {
2491 tree object;
2492
2493 /* A call to a member function. From [over.call.func]:
2494
2495 If the keyword this is in scope and refers to the class of
2496 that member function, or a derived class thereof, then the
2497 function call is transformed into a qualified function call
2498 using (*this) as the postfix-expression to the left of the
2499 . operator.... [Otherwise] a contrived object of type T
2500 becomes the implied object argument.
2501
2502 In this situation:
2503
2504 struct A { void f(); };
2505 struct B : public A {};
2506 struct C : public A { void g() { B::f(); }};
2507
2508 "the class of that member function" refers to `A'. But 11.2
2509 [class.access.base] says that we need to convert 'this' to B* as
2510 part of the access, so we pass 'B' to maybe_dummy_object. */
2511
2512 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (get_first_fn (fn)))
2513 {
2514 /* A constructor call always uses a dummy object. (This constructor
2515 call which has the form A::A () is actually invalid and we are
2516 going to reject it later in build_new_method_call.) */
2517 object = build_dummy_object (BINFO_TYPE (BASELINK_ACCESS_BINFO (fn)));
2518 }
2519 else
2520 object = maybe_dummy_object (BINFO_TYPE (BASELINK_ACCESS_BINFO (fn)),
2521 NULL);
2522
2523 result = build_new_method_call (object, fn, args, NULL_TREE,
2524 (disallow_virtual
2525 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
2526 : LOOKUP_NORMAL),
2527 /*fn_p=*/NULL,
2528 complain);
2529 }
2530 else if (is_overloaded_fn (fn))
2531 {
2532 /* If the function is an overloaded builtin, resolve it. */
2533 if (TREE_CODE (fn) == FUNCTION_DECL
2534 && (DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL
2535 || DECL_BUILT_IN_CLASS (fn) == BUILT_IN_MD))
2536 result = resolve_overloaded_builtin (input_location, fn, *args);
2537
2538 if (!result)
2539 {
2540 if (warn_sizeof_pointer_memaccess
2541 && (complain & tf_warning)
2542 && !vec_safe_is_empty (*args)
2543 && !processing_template_decl)
2544 {
2545 location_t sizeof_arg_loc[3];
2546 tree sizeof_arg[3];
2547 unsigned int i;
2548 for (i = 0; i < 3; i++)
2549 {
2550 tree t;
2551
2552 sizeof_arg_loc[i] = UNKNOWN_LOCATION;
2553 sizeof_arg[i] = NULL_TREE;
2554 if (i >= (*args)->length ())
2555 continue;
2556 t = (**args)[i];
2557 if (TREE_CODE (t) != SIZEOF_EXPR)
2558 continue;
2559 if (SIZEOF_EXPR_TYPE_P (t))
2560 sizeof_arg[i] = TREE_TYPE (TREE_OPERAND (t, 0));
2561 else
2562 sizeof_arg[i] = TREE_OPERAND (t, 0);
2563 sizeof_arg_loc[i] = EXPR_LOCATION (t);
2564 }
2565 sizeof_pointer_memaccess_warning
2566 (sizeof_arg_loc, fn, *args,
2567 sizeof_arg, same_type_ignoring_top_level_qualifiers_p);
2568 }
2569
2570 if ((complain & tf_warning)
2571 && TREE_CODE (fn) == FUNCTION_DECL
2572 && fndecl_built_in_p (fn, BUILT_IN_MEMSET)
2573 && vec_safe_length (*args) == 3
2574 && !any_type_dependent_arguments_p (*args))
2575 {
2576 tree arg0 = (*orig_args)[0];
2577 tree arg1 = (*orig_args)[1];
2578 tree arg2 = (*orig_args)[2];
2579 int literal_mask = ((literal_integer_zerop (arg1) << 1)
2580 | (literal_integer_zerop (arg2) << 2));
2581 arg2 = instantiate_non_dependent_expr (arg2);
2582 warn_for_memset (input_location, arg0, arg2, literal_mask);
2583 }
2584
2585 /* A call to a namespace-scope function. */
2586 result = build_new_function_call (fn, args, complain);
2587 }
2588 }
2589 else if (TREE_CODE (fn) == PSEUDO_DTOR_EXPR)
2590 {
2591 if (!vec_safe_is_empty (*args))
2592 error ("arguments to destructor are not allowed");
2593 /* Mark the pseudo-destructor call as having side-effects so
2594 that we do not issue warnings about its use. */
2595 result = build1 (NOP_EXPR,
2596 void_type_node,
2597 TREE_OPERAND (fn, 0));
2598 TREE_SIDE_EFFECTS (result) = 1;
2599 }
2600 else if (CLASS_TYPE_P (TREE_TYPE (fn)))
2601 /* If the "function" is really an object of class type, it might
2602 have an overloaded `operator ()'. */
2603 result = build_op_call (fn, args, complain);
2604
2605 if (!result)
2606 /* A call where the function is unknown. */
2607 result = cp_build_function_call_vec (fn, args, complain);
2608
2609 if (processing_template_decl && result != error_mark_node)
2610 {
2611 if (INDIRECT_REF_P (result))
2612 result = TREE_OPERAND (result, 0);
2613 result = build_call_vec (TREE_TYPE (result), orig_fn, orig_args);
2614 SET_EXPR_LOCATION (result, input_location);
2615 KOENIG_LOOKUP_P (result) = koenig_p;
2616 release_tree_vector (orig_args);
2617 result = convert_from_reference (result);
2618 }
2619
2620 return result;
2621 }
2622
2623 /* Finish a call to a postfix increment or decrement or EXPR. (Which
2624 is indicated by CODE, which should be POSTINCREMENT_EXPR or
2625 POSTDECREMENT_EXPR.) */
2626
2627 cp_expr
2628 finish_increment_expr (cp_expr expr, enum tree_code code)
2629 {
2630 /* input_location holds the location of the trailing operator token.
2631 Build a location of the form:
2632 expr++
2633 ~~~~^~
2634 with the caret at the operator token, ranging from the start
2635 of EXPR to the end of the operator token. */
2636 location_t combined_loc = make_location (input_location,
2637 expr.get_start (),
2638 get_finish (input_location));
2639 cp_expr result = build_x_unary_op (combined_loc, code, expr,
2640 tf_warning_or_error);
2641 /* TODO: build_x_unary_op doesn't honor the location, so set it here. */
2642 result.set_location (combined_loc);
2643 return result;
2644 }
2645
2646 /* Finish a use of `this'. Returns an expression for `this'. */
2647
2648 tree
2649 finish_this_expr (void)
2650 {
2651 tree result = NULL_TREE;
2652
2653 if (current_class_ptr)
2654 {
2655 tree type = TREE_TYPE (current_class_ref);
2656
2657 /* In a lambda expression, 'this' refers to the captured 'this'. */
2658 if (LAMBDA_TYPE_P (type))
2659 result = lambda_expr_this_capture (CLASSTYPE_LAMBDA_EXPR (type), true);
2660 else
2661 result = current_class_ptr;
2662 }
2663
2664 if (result)
2665 /* The keyword 'this' is a prvalue expression. */
2666 return rvalue (result);
2667
2668 tree fn = current_nonlambda_function ();
2669 if (fn && DECL_STATIC_FUNCTION_P (fn))
2670 error ("%<this%> is unavailable for static member functions");
2671 else if (fn)
2672 error ("invalid use of %<this%> in non-member function");
2673 else
2674 error ("invalid use of %<this%> at top level");
2675 return error_mark_node;
2676 }
2677
2678 /* Finish a pseudo-destructor expression. If SCOPE is NULL, the
2679 expression was of the form `OBJECT.~DESTRUCTOR' where DESTRUCTOR is
2680 the TYPE for the type given. If SCOPE is non-NULL, the expression
2681 was of the form `OBJECT.SCOPE::~DESTRUCTOR'. */
2682
2683 tree
2684 finish_pseudo_destructor_expr (tree object, tree scope, tree destructor,
2685 location_t loc)
2686 {
2687 if (object == error_mark_node || destructor == error_mark_node)
2688 return error_mark_node;
2689
2690 gcc_assert (TYPE_P (destructor));
2691
2692 if (!processing_template_decl)
2693 {
2694 if (scope == error_mark_node)
2695 {
2696 error_at (loc, "invalid qualifying scope in pseudo-destructor name");
2697 return error_mark_node;
2698 }
2699 if (is_auto (destructor))
2700 destructor = TREE_TYPE (object);
2701 if (scope && TYPE_P (scope) && !check_dtor_name (scope, destructor))
2702 {
2703 error_at (loc,
2704 "qualified type %qT does not match destructor name ~%qT",
2705 scope, destructor);
2706 return error_mark_node;
2707 }
2708
2709
2710 /* [expr.pseudo] says both:
2711
2712 The type designated by the pseudo-destructor-name shall be
2713 the same as the object type.
2714
2715 and:
2716
2717 The cv-unqualified versions of the object type and of the
2718 type designated by the pseudo-destructor-name shall be the
2719 same type.
2720
2721 We implement the more generous second sentence, since that is
2722 what most other compilers do. */
2723 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (object),
2724 destructor))
2725 {
2726 error_at (loc, "%qE is not of type %qT", object, destructor);
2727 return error_mark_node;
2728 }
2729 }
2730
2731 return build3_loc (loc, PSEUDO_DTOR_EXPR, void_type_node, object,
2732 scope, destructor);
2733 }
2734
2735 /* Finish an expression of the form CODE EXPR. */
2736
2737 cp_expr
2738 finish_unary_op_expr (location_t op_loc, enum tree_code code, cp_expr expr,
2739 tsubst_flags_t complain)
2740 {
2741 /* Build a location of the form:
2742 ++expr
2743 ^~~~~~
2744 with the caret at the operator token, ranging from the start
2745 of the operator token to the end of EXPR. */
2746 location_t combined_loc = make_location (op_loc,
2747 op_loc, expr.get_finish ());
2748 cp_expr result = build_x_unary_op (combined_loc, code, expr, complain);
2749 /* TODO: build_x_unary_op doesn't always honor the location. */
2750 result.set_location (combined_loc);
2751
2752 if (result == error_mark_node)
2753 return result;
2754
2755 if (!(complain & tf_warning))
2756 return result;
2757
2758 tree result_ovl = result;
2759 tree expr_ovl = expr;
2760
2761 if (!processing_template_decl)
2762 expr_ovl = cp_fully_fold (expr_ovl);
2763
2764 if (!CONSTANT_CLASS_P (expr_ovl)
2765 || TREE_OVERFLOW_P (expr_ovl))
2766 return result;
2767
2768 if (!processing_template_decl)
2769 result_ovl = cp_fully_fold (result_ovl);
2770
2771 if (CONSTANT_CLASS_P (result_ovl) && TREE_OVERFLOW_P (result_ovl))
2772 overflow_warning (combined_loc, result_ovl);
2773
2774 return result;
2775 }
2776
2777 /* Finish a compound-literal expression or C++11 functional cast with aggregate
2778 initializer. TYPE is the type to which the CONSTRUCTOR in COMPOUND_LITERAL
2779 is being cast. */
2780
2781 tree
2782 finish_compound_literal (tree type, tree compound_literal,
2783 tsubst_flags_t complain,
2784 fcl_t fcl_context)
2785 {
2786 if (type == error_mark_node)
2787 return error_mark_node;
2788
2789 if (TYPE_REF_P (type))
2790 {
2791 compound_literal
2792 = finish_compound_literal (TREE_TYPE (type), compound_literal,
2793 complain, fcl_context);
2794 /* The prvalue is then used to direct-initialize the reference. */
2795 tree r = (perform_implicit_conversion_flags
2796 (type, compound_literal, complain, LOOKUP_NORMAL));
2797 return convert_from_reference (r);
2798 }
2799
2800 if (!TYPE_OBJ_P (type))
2801 {
2802 if (complain & tf_error)
2803 error ("compound literal of non-object type %qT", type);
2804 return error_mark_node;
2805 }
2806
2807 if (tree anode = type_uses_auto (type))
2808 if (CLASS_PLACEHOLDER_TEMPLATE (anode))
2809 {
2810 type = do_auto_deduction (type, compound_literal, anode, complain,
2811 adc_variable_type);
2812 if (type == error_mark_node)
2813 return error_mark_node;
2814 }
2815
2816 /* Used to hold a copy of the compound literal in a template. */
2817 tree orig_cl = NULL_TREE;
2818
2819 if (processing_template_decl)
2820 {
2821 const bool dependent_p
2822 = (instantiation_dependent_expression_p (compound_literal)
2823 || dependent_type_p (type));
2824 if (dependent_p)
2825 /* We're about to return, no need to copy. */
2826 orig_cl = compound_literal;
2827 else
2828 /* We're going to need a copy. */
2829 orig_cl = unshare_constructor (compound_literal);
2830 TREE_TYPE (orig_cl) = type;
2831 /* Mark the expression as a compound literal. */
2832 TREE_HAS_CONSTRUCTOR (orig_cl) = 1;
2833 /* And as instantiation-dependent. */
2834 CONSTRUCTOR_IS_DEPENDENT (orig_cl) = dependent_p;
2835 if (fcl_context == fcl_c99)
2836 CONSTRUCTOR_C99_COMPOUND_LITERAL (orig_cl) = 1;
2837 /* If the compound literal is dependent, we're done for now. */
2838 if (dependent_p)
2839 return orig_cl;
2840 /* Otherwise, do go on to e.g. check narrowing. */
2841 }
2842
2843 type = complete_type (type);
2844
2845 if (TYPE_NON_AGGREGATE_CLASS (type))
2846 {
2847 /* Trying to deal with a CONSTRUCTOR instead of a TREE_LIST
2848 everywhere that deals with function arguments would be a pain, so
2849 just wrap it in a TREE_LIST. The parser set a flag so we know
2850 that it came from T{} rather than T({}). */
2851 CONSTRUCTOR_IS_DIRECT_INIT (compound_literal) = 1;
2852 compound_literal = build_tree_list (NULL_TREE, compound_literal);
2853 return build_functional_cast (type, compound_literal, complain);
2854 }
2855
2856 if (TREE_CODE (type) == ARRAY_TYPE
2857 && check_array_initializer (NULL_TREE, type, compound_literal))
2858 return error_mark_node;
2859 compound_literal = reshape_init (type, compound_literal, complain);
2860 if (SCALAR_TYPE_P (type)
2861 && !BRACE_ENCLOSED_INITIALIZER_P (compound_literal))
2862 {
2863 tree t = instantiate_non_dependent_expr_sfinae (compound_literal,
2864 complain);
2865 if (!check_narrowing (type, t, complain))
2866 return error_mark_node;
2867 }
2868 if (TREE_CODE (type) == ARRAY_TYPE
2869 && TYPE_DOMAIN (type) == NULL_TREE)
2870 {
2871 cp_complete_array_type_or_error (&type, compound_literal,
2872 false, complain);
2873 if (type == error_mark_node)
2874 return error_mark_node;
2875 }
2876 compound_literal = digest_init_flags (type, compound_literal,
2877 LOOKUP_NORMAL | LOOKUP_NO_NARROWING,
2878 complain);
2879 if (compound_literal == error_mark_node)
2880 return error_mark_node;
2881
2882 /* If we're in a template, return the original compound literal. */
2883 if (orig_cl)
2884 {
2885 if (!VECTOR_TYPE_P (type))
2886 return get_target_expr_sfinae (orig_cl, complain);
2887 else
2888 return orig_cl;
2889 }
2890
2891 if (TREE_CODE (compound_literal) == CONSTRUCTOR)
2892 {
2893 TREE_HAS_CONSTRUCTOR (compound_literal) = true;
2894 if (fcl_context == fcl_c99)
2895 CONSTRUCTOR_C99_COMPOUND_LITERAL (compound_literal) = 1;
2896 }
2897
2898 /* Put static/constant array temporaries in static variables. */
2899 /* FIXME all C99 compound literals should be variables rather than C++
2900 temporaries, unless they are used as an aggregate initializer. */
2901 if ((!at_function_scope_p () || CP_TYPE_CONST_P (type))
2902 && fcl_context == fcl_c99
2903 && TREE_CODE (type) == ARRAY_TYPE
2904 && !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
2905 && initializer_constant_valid_p (compound_literal, type))
2906 {
2907 tree decl = create_temporary_var (type);
2908 DECL_INITIAL (decl) = compound_literal;
2909 TREE_STATIC (decl) = 1;
2910 if (literal_type_p (type) && CP_TYPE_CONST_NON_VOLATILE_P (type))
2911 {
2912 /* 5.19 says that a constant expression can include an
2913 lvalue-rvalue conversion applied to "a glvalue of literal type
2914 that refers to a non-volatile temporary object initialized
2915 with a constant expression". Rather than try to communicate
2916 that this VAR_DECL is a temporary, just mark it constexpr. */
2917 DECL_DECLARED_CONSTEXPR_P (decl) = true;
2918 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
2919 TREE_CONSTANT (decl) = true;
2920 }
2921 cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
2922 decl = pushdecl_top_level (decl);
2923 DECL_NAME (decl) = make_anon_name ();
2924 SET_DECL_ASSEMBLER_NAME (decl, DECL_NAME (decl));
2925 /* Make sure the destructor is callable. */
2926 tree clean = cxx_maybe_build_cleanup (decl, complain);
2927 if (clean == error_mark_node)
2928 return error_mark_node;
2929 return decl;
2930 }
2931
2932 /* Represent other compound literals with TARGET_EXPR so we produce
2933 an lvalue, but can elide copies. */
2934 if (!VECTOR_TYPE_P (type))
2935 compound_literal = get_target_expr_sfinae (compound_literal, complain);
2936
2937 return compound_literal;
2938 }
2939
2940 /* Return the declaration for the function-name variable indicated by
2941 ID. */
2942
2943 tree
2944 finish_fname (tree id)
2945 {
2946 tree decl;
2947
2948 decl = fname_decl (input_location, C_RID_CODE (id), id);
2949 if (processing_template_decl && current_function_decl
2950 && decl != error_mark_node)
2951 decl = DECL_NAME (decl);
2952 return decl;
2953 }
2954
2955 /* Finish a translation unit. */
2956
2957 void
2958 finish_translation_unit (void)
2959 {
2960 /* In case there were missing closebraces,
2961 get us back to the global binding level. */
2962 pop_everything ();
2963 while (current_namespace != global_namespace)
2964 pop_namespace ();
2965
2966 /* Do file scope __FUNCTION__ et al. */
2967 finish_fname_decls ();
2968 }
2969
2970 /* Finish a template type parameter, specified as AGGR IDENTIFIER.
2971 Returns the parameter. */
2972
2973 tree
2974 finish_template_type_parm (tree aggr, tree identifier)
2975 {
2976 if (aggr != class_type_node)
2977 {
2978 permerror (input_location, "template type parameters must use the keyword %<class%> or %<typename%>");
2979 aggr = class_type_node;
2980 }
2981
2982 return build_tree_list (aggr, identifier);
2983 }
2984
2985 /* Finish a template template parameter, specified as AGGR IDENTIFIER.
2986 Returns the parameter. */
2987
2988 tree
2989 finish_template_template_parm (tree aggr, tree identifier)
2990 {
2991 tree decl = build_decl (input_location,
2992 TYPE_DECL, identifier, NULL_TREE);
2993
2994 tree tmpl = build_lang_decl (TEMPLATE_DECL, identifier, NULL_TREE);
2995 DECL_TEMPLATE_PARMS (tmpl) = current_template_parms;
2996 DECL_TEMPLATE_RESULT (tmpl) = decl;
2997 DECL_ARTIFICIAL (decl) = 1;
2998
2999 // Associate the constraints with the underlying declaration,
3000 // not the template.
3001 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
3002 tree constr = build_constraints (reqs, NULL_TREE);
3003 set_constraints (decl, constr);
3004
3005 end_template_decl ();
3006
3007 gcc_assert (DECL_TEMPLATE_PARMS (tmpl));
3008
3009 check_default_tmpl_args (decl, DECL_TEMPLATE_PARMS (tmpl),
3010 /*is_primary=*/true, /*is_partial=*/false,
3011 /*is_friend=*/0);
3012
3013 return finish_template_type_parm (aggr, tmpl);
3014 }
3015
3016 /* ARGUMENT is the default-argument value for a template template
3017 parameter. If ARGUMENT is invalid, issue error messages and return
3018 the ERROR_MARK_NODE. Otherwise, ARGUMENT itself is returned. */
3019
3020 tree
3021 check_template_template_default_arg (tree argument)
3022 {
3023 if (TREE_CODE (argument) != TEMPLATE_DECL
3024 && TREE_CODE (argument) != TEMPLATE_TEMPLATE_PARM
3025 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
3026 {
3027 if (TREE_CODE (argument) == TYPE_DECL)
3028 error ("invalid use of type %qT as a default value for a template "
3029 "template-parameter", TREE_TYPE (argument));
3030 else
3031 error ("invalid default argument for a template template parameter");
3032 return error_mark_node;
3033 }
3034
3035 return argument;
3036 }
3037
3038 /* Begin a class definition, as indicated by T. */
3039
3040 tree
3041 begin_class_definition (tree t)
3042 {
3043 if (error_operand_p (t) || error_operand_p (TYPE_MAIN_DECL (t)))
3044 return error_mark_node;
3045
3046 if (processing_template_parmlist && !LAMBDA_TYPE_P (t))
3047 {
3048 error ("definition of %q#T inside template parameter list", t);
3049 return error_mark_node;
3050 }
3051
3052 /* According to the C++ ABI, decimal classes defined in ISO/IEC TR 24733
3053 are passed the same as decimal scalar types. */
3054 if (TREE_CODE (t) == RECORD_TYPE
3055 && !processing_template_decl)
3056 {
3057 tree ns = TYPE_CONTEXT (t);
3058 if (ns && TREE_CODE (ns) == NAMESPACE_DECL
3059 && DECL_CONTEXT (ns) == std_node
3060 && DECL_NAME (ns)
3061 && id_equal (DECL_NAME (ns), "decimal"))
3062 {
3063 const char *n = TYPE_NAME_STRING (t);
3064 if ((strcmp (n, "decimal32") == 0)
3065 || (strcmp (n, "decimal64") == 0)
3066 || (strcmp (n, "decimal128") == 0))
3067 TYPE_TRANSPARENT_AGGR (t) = 1;
3068 }
3069 }
3070
3071 /* A non-implicit typename comes from code like:
3072
3073 template <typename T> struct A {
3074 template <typename U> struct A<T>::B ...
3075
3076 This is erroneous. */
3077 else if (TREE_CODE (t) == TYPENAME_TYPE)
3078 {
3079 error ("invalid definition of qualified type %qT", t);
3080 t = error_mark_node;
3081 }
3082
3083 if (t == error_mark_node || ! MAYBE_CLASS_TYPE_P (t))
3084 {
3085 t = make_class_type (RECORD_TYPE);
3086 pushtag (make_anon_name (), t, /*tag_scope=*/ts_current);
3087 }
3088
3089 if (TYPE_BEING_DEFINED (t))
3090 {
3091 t = make_class_type (TREE_CODE (t));
3092 pushtag (TYPE_IDENTIFIER (t), t, /*tag_scope=*/ts_current);
3093 }
3094 maybe_process_partial_specialization (t);
3095 pushclass (t);
3096 TYPE_BEING_DEFINED (t) = 1;
3097 class_binding_level->defining_class_p = 1;
3098
3099 if (flag_pack_struct)
3100 {
3101 tree v;
3102 TYPE_PACKED (t) = 1;
3103 /* Even though the type is being defined for the first time
3104 here, there might have been a forward declaration, so there
3105 might be cv-qualified variants of T. */
3106 for (v = TYPE_NEXT_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v))
3107 TYPE_PACKED (v) = 1;
3108 }
3109 /* Reset the interface data, at the earliest possible
3110 moment, as it might have been set via a class foo;
3111 before. */
3112 if (! TYPE_UNNAMED_P (t))
3113 {
3114 struct c_fileinfo *finfo = \
3115 get_fileinfo (LOCATION_FILE (input_location));
3116 CLASSTYPE_INTERFACE_ONLY (t) = finfo->interface_only;
3117 SET_CLASSTYPE_INTERFACE_UNKNOWN_X
3118 (t, finfo->interface_unknown);
3119 }
3120 reset_specialization();
3121
3122 /* Make a declaration for this class in its own scope. */
3123 build_self_reference ();
3124
3125 return t;
3126 }
3127
3128 /* Finish the member declaration given by DECL. */
3129
3130 void
3131 finish_member_declaration (tree decl)
3132 {
3133 if (decl == error_mark_node || decl == NULL_TREE)
3134 return;
3135
3136 if (decl == void_type_node)
3137 /* The COMPONENT was a friend, not a member, and so there's
3138 nothing for us to do. */
3139 return;
3140
3141 /* We should see only one DECL at a time. */
3142 gcc_assert (DECL_CHAIN (decl) == NULL_TREE);
3143
3144 /* Don't add decls after definition. */
3145 gcc_assert (TYPE_BEING_DEFINED (current_class_type)
3146 /* We can add lambda types when late parsing default
3147 arguments. */
3148 || LAMBDA_TYPE_P (TREE_TYPE (decl)));
3149
3150 /* Set up access control for DECL. */
3151 TREE_PRIVATE (decl)
3152 = (current_access_specifier == access_private_node);
3153 TREE_PROTECTED (decl)
3154 = (current_access_specifier == access_protected_node);
3155 if (TREE_CODE (decl) == TEMPLATE_DECL)
3156 {
3157 TREE_PRIVATE (DECL_TEMPLATE_RESULT (decl)) = TREE_PRIVATE (decl);
3158 TREE_PROTECTED (DECL_TEMPLATE_RESULT (decl)) = TREE_PROTECTED (decl);
3159 }
3160
3161 /* Mark the DECL as a member of the current class, unless it's
3162 a member of an enumeration. */
3163 if (TREE_CODE (decl) != CONST_DECL)
3164 DECL_CONTEXT (decl) = current_class_type;
3165
3166 if (TREE_CODE (decl) == USING_DECL)
3167 /* For now, ignore class-scope USING_DECLS, so that debugging
3168 backends do not see them. */
3169 DECL_IGNORED_P (decl) = 1;
3170
3171 /* Check for bare parameter packs in the non-static data member
3172 declaration. */
3173 if (TREE_CODE (decl) == FIELD_DECL)
3174 {
3175 if (check_for_bare_parameter_packs (TREE_TYPE (decl)))
3176 TREE_TYPE (decl) = error_mark_node;
3177 if (check_for_bare_parameter_packs (DECL_ATTRIBUTES (decl)))
3178 DECL_ATTRIBUTES (decl) = NULL_TREE;
3179 }
3180
3181 /* [dcl.link]
3182
3183 A C language linkage is ignored for the names of class members
3184 and the member function type of class member functions. */
3185 if (DECL_LANG_SPECIFIC (decl))
3186 SET_DECL_LANGUAGE (decl, lang_cplusplus);
3187
3188 bool add = false;
3189
3190 /* Functions and non-functions are added differently. */
3191 if (DECL_DECLARES_FUNCTION_P (decl))
3192 add = add_method (current_class_type, decl, false);
3193 /* Enter the DECL into the scope of the class, if the class
3194 isn't a closure (whose fields are supposed to be unnamed). */
3195 else if (CLASSTYPE_LAMBDA_EXPR (current_class_type)
3196 || pushdecl_class_level (decl))
3197 add = true;
3198
3199 if (add)
3200 {
3201 /* All TYPE_DECLs go at the end of TYPE_FIELDS. Ordinary fields
3202 go at the beginning. The reason is that
3203 legacy_nonfn_member_lookup searches the list in order, and we
3204 want a field name to override a type name so that the "struct
3205 stat hack" will work. In particular:
3206
3207 struct S { enum E { }; static const int E = 5; int ary[S::E]; } s;
3208
3209 is valid. */
3210
3211 if (TREE_CODE (decl) == TYPE_DECL)
3212 TYPE_FIELDS (current_class_type)
3213 = chainon (TYPE_FIELDS (current_class_type), decl);
3214 else
3215 {
3216 DECL_CHAIN (decl) = TYPE_FIELDS (current_class_type);
3217 TYPE_FIELDS (current_class_type) = decl;
3218 }
3219
3220 maybe_add_class_template_decl_list (current_class_type, decl,
3221 /*friend_p=*/0);
3222 }
3223 }
3224
3225 /* Finish processing a complete template declaration. The PARMS are
3226 the template parameters. */
3227
3228 void
3229 finish_template_decl (tree parms)
3230 {
3231 if (parms)
3232 end_template_decl ();
3233 else
3234 end_specialization ();
3235 }
3236
3237 // Returns the template type of the class scope being entered. If we're
3238 // entering a constrained class scope. TYPE is the class template
3239 // scope being entered and we may need to match the intended type with
3240 // a constrained specialization. For example:
3241 //
3242 // template<Object T>
3243 // struct S { void f(); }; #1
3244 //
3245 // template<Object T>
3246 // void S<T>::f() { } #2
3247 //
3248 // We check, in #2, that S<T> refers precisely to the type declared by
3249 // #1 (i.e., that the constraints match). Note that the following should
3250 // be an error since there is no specialization of S<T> that is
3251 // unconstrained, but this is not diagnosed here.
3252 //
3253 // template<typename T>
3254 // void S<T>::f() { }
3255 //
3256 // We cannot diagnose this problem here since this function also matches
3257 // qualified template names that are not part of a definition. For example:
3258 //
3259 // template<Integral T, Floating_point U>
3260 // typename pair<T, U>::first_type void f(T, U);
3261 //
3262 // Here, it is unlikely that there is a partial specialization of
3263 // pair constrained for for Integral and Floating_point arguments.
3264 //
3265 // The general rule is: if a constrained specialization with matching
3266 // constraints is found return that type. Also note that if TYPE is not a
3267 // class-type (e.g. a typename type), then no fixup is needed.
3268
3269 static tree
3270 fixup_template_type (tree type)
3271 {
3272 // Find the template parameter list at the a depth appropriate to
3273 // the scope we're trying to enter.
3274 tree parms = current_template_parms;
3275 int depth = template_class_depth (type);
3276 for (int n = processing_template_decl; n > depth && parms; --n)
3277 parms = TREE_CHAIN (parms);
3278 if (!parms)
3279 return type;
3280 tree cur_reqs = TEMPLATE_PARMS_CONSTRAINTS (parms);
3281 tree cur_constr = build_constraints (cur_reqs, NULL_TREE);
3282
3283 // Search for a specialization whose type and constraints match.
3284 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
3285 tree specs = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
3286 while (specs)
3287 {
3288 tree spec_constr = get_constraints (TREE_VALUE (specs));
3289
3290 // If the type and constraints match a specialization, then we
3291 // are entering that type.
3292 if (same_type_p (type, TREE_TYPE (specs))
3293 && equivalent_constraints (cur_constr, spec_constr))
3294 return TREE_TYPE (specs);
3295 specs = TREE_CHAIN (specs);
3296 }
3297
3298 // If no specialization matches, then must return the type
3299 // previously found.
3300 return type;
3301 }
3302
3303 /* Finish processing a template-id (which names a type) of the form
3304 NAME < ARGS >. Return the TYPE_DECL for the type named by the
3305 template-id. If ENTERING_SCOPE is nonzero we are about to enter
3306 the scope of template-id indicated. */
3307
3308 tree
3309 finish_template_type (tree name, tree args, int entering_scope)
3310 {
3311 tree type;
3312
3313 type = lookup_template_class (name, args,
3314 NULL_TREE, NULL_TREE, entering_scope,
3315 tf_warning_or_error | tf_user);
3316
3317 /* If we might be entering the scope of a partial specialization,
3318 find the one with the right constraints. */
3319 if (flag_concepts
3320 && entering_scope
3321 && CLASS_TYPE_P (type)
3322 && CLASSTYPE_TEMPLATE_INFO (type)
3323 && dependent_type_p (type)
3324 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
3325 type = fixup_template_type (type);
3326
3327 if (type == error_mark_node)
3328 return type;
3329 else if (CLASS_TYPE_P (type) && !alias_type_or_template_p (type))
3330 return TYPE_STUB_DECL (type);
3331 else
3332 return TYPE_NAME (type);
3333 }
3334
3335 /* Finish processing a BASE_CLASS with the indicated ACCESS_SPECIFIER.
3336 Return a TREE_LIST containing the ACCESS_SPECIFIER and the
3337 BASE_CLASS, or NULL_TREE if an error occurred. The
3338 ACCESS_SPECIFIER is one of
3339 access_{default,public,protected_private}_node. For a virtual base
3340 we set TREE_TYPE. */
3341
3342 tree
3343 finish_base_specifier (tree base, tree access, bool virtual_p)
3344 {
3345 tree result;
3346
3347 if (base == error_mark_node)
3348 {
3349 error ("invalid base-class specification");
3350 result = NULL_TREE;
3351 }
3352 else if (! MAYBE_CLASS_TYPE_P (base))
3353 {
3354 error ("%qT is not a class type", base);
3355 result = NULL_TREE;
3356 }
3357 else
3358 {
3359 if (cp_type_quals (base) != 0)
3360 {
3361 /* DR 484: Can a base-specifier name a cv-qualified
3362 class type? */
3363 base = TYPE_MAIN_VARIANT (base);
3364 }
3365 result = build_tree_list (access, base);
3366 if (virtual_p)
3367 TREE_TYPE (result) = integer_type_node;
3368 }
3369
3370 return result;
3371 }
3372
3373 /* If FNS is a member function, a set of member functions, or a
3374 template-id referring to one or more member functions, return a
3375 BASELINK for FNS, incorporating the current access context.
3376 Otherwise, return FNS unchanged. */
3377
3378 tree
3379 baselink_for_fns (tree fns)
3380 {
3381 tree scope;
3382 tree cl;
3383
3384 if (BASELINK_P (fns)
3385 || error_operand_p (fns))
3386 return fns;
3387
3388 scope = ovl_scope (fns);
3389 if (!CLASS_TYPE_P (scope))
3390 return fns;
3391
3392 cl = currently_open_derived_class (scope);
3393 if (!cl)
3394 cl = scope;
3395 cl = TYPE_BINFO (cl);
3396 return build_baselink (cl, cl, fns, /*optype=*/NULL_TREE);
3397 }
3398
3399 /* Returns true iff DECL is a variable from a function outside
3400 the current one. */
3401
3402 static bool
3403 outer_var_p (tree decl)
3404 {
3405 return ((VAR_P (decl) || TREE_CODE (decl) == PARM_DECL)
3406 && DECL_FUNCTION_SCOPE_P (decl)
3407 /* Don't get confused by temporaries. */
3408 && DECL_NAME (decl)
3409 && (DECL_CONTEXT (decl) != current_function_decl
3410 || parsing_nsdmi ()));
3411 }
3412
3413 /* As above, but also checks that DECL is automatic. */
3414
3415 bool
3416 outer_automatic_var_p (tree decl)
3417 {
3418 return (outer_var_p (decl)
3419 && !TREE_STATIC (decl));
3420 }
3421
3422 /* DECL satisfies outer_automatic_var_p. Possibly complain about it or
3423 rewrite it for lambda capture.
3424
3425 If ODR_USE is true, we're being called from mark_use, and we complain about
3426 use of constant variables. If ODR_USE is false, we're being called for the
3427 id-expression, and we do lambda capture. */
3428
3429 tree
3430 process_outer_var_ref (tree decl, tsubst_flags_t complain, bool odr_use)
3431 {
3432 if (cp_unevaluated_operand)
3433 /* It's not a use (3.2) if we're in an unevaluated context. */
3434 return decl;
3435 if (decl == error_mark_node)
3436 return decl;
3437
3438 tree context = DECL_CONTEXT (decl);
3439 tree containing_function = current_function_decl;
3440 tree lambda_stack = NULL_TREE;
3441 tree lambda_expr = NULL_TREE;
3442 tree initializer = convert_from_reference (decl);
3443
3444 /* Mark it as used now even if the use is ill-formed. */
3445 if (!mark_used (decl, complain))
3446 return error_mark_node;
3447
3448 if (parsing_nsdmi ())
3449 containing_function = NULL_TREE;
3450
3451 if (containing_function && LAMBDA_FUNCTION_P (containing_function))
3452 {
3453 /* Check whether we've already built a proxy. */
3454 tree var = decl;
3455 while (is_normal_capture_proxy (var))
3456 var = DECL_CAPTURED_VARIABLE (var);
3457 tree d = retrieve_local_specialization (var);
3458
3459 if (d && d != decl && is_capture_proxy (d))
3460 {
3461 if (DECL_CONTEXT (d) == containing_function)
3462 /* We already have an inner proxy. */
3463 return d;
3464 else
3465 /* We need to capture an outer proxy. */
3466 return process_outer_var_ref (d, complain, odr_use);
3467 }
3468 }
3469
3470 /* If we are in a lambda function, we can move out until we hit
3471 1. the context,
3472 2. a non-lambda function, or
3473 3. a non-default capturing lambda function. */
3474 while (context != containing_function
3475 /* containing_function can be null with invalid generic lambdas. */
3476 && containing_function
3477 && LAMBDA_FUNCTION_P (containing_function))
3478 {
3479 tree closure = DECL_CONTEXT (containing_function);
3480 lambda_expr = CLASSTYPE_LAMBDA_EXPR (closure);
3481
3482 if (TYPE_CLASS_SCOPE_P (closure))
3483 /* A lambda in an NSDMI (c++/64496). */
3484 break;
3485
3486 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr)
3487 == CPLD_NONE)
3488 break;
3489
3490 lambda_stack = tree_cons (NULL_TREE,
3491 lambda_expr,
3492 lambda_stack);
3493
3494 containing_function
3495 = decl_function_context (containing_function);
3496 }
3497
3498 /* In a lambda within a template, wait until instantiation time to implicitly
3499 capture a parameter pack. We want to wait because we don't know if we're
3500 capturing the whole pack or a single element, and it's OK to wait because
3501 find_parameter_packs_r walks into the lambda body. */
3502 if (context == containing_function
3503 && DECL_PACK_P (decl))
3504 return decl;
3505
3506 if (lambda_expr && VAR_P (decl)
3507 && DECL_ANON_UNION_VAR_P (decl))
3508 {
3509 if (complain & tf_error)
3510 error ("cannot capture member %qD of anonymous union", decl);
3511 return error_mark_node;
3512 }
3513 /* Do lambda capture when processing the id-expression, not when
3514 odr-using a variable. */
3515 if (!odr_use && context == containing_function)
3516 {
3517 decl = add_default_capture (lambda_stack,
3518 /*id=*/DECL_NAME (decl),
3519 initializer);
3520 }
3521 /* Only an odr-use of an outer automatic variable causes an
3522 error, and a constant variable can decay to a prvalue
3523 constant without odr-use. So don't complain yet. */
3524 else if (!odr_use && decl_constant_var_p (decl))
3525 return decl;
3526 else if (lambda_expr)
3527 {
3528 if (complain & tf_error)
3529 {
3530 error ("%qD is not captured", decl);
3531 tree closure = LAMBDA_EXPR_CLOSURE (lambda_expr);
3532 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr)
3533 == CPLD_NONE)
3534 inform (location_of (closure),
3535 "the lambda has no capture-default");
3536 else if (TYPE_CLASS_SCOPE_P (closure))
3537 inform (UNKNOWN_LOCATION, "lambda in local class %q+T cannot "
3538 "capture variables from the enclosing context",
3539 TYPE_CONTEXT (closure));
3540 inform (DECL_SOURCE_LOCATION (decl), "%q#D declared here", decl);
3541 }
3542 return error_mark_node;
3543 }
3544 else
3545 {
3546 if (complain & tf_error)
3547 {
3548 error (VAR_P (decl)
3549 ? G_("use of local variable with automatic storage from "
3550 "containing function")
3551 : G_("use of parameter from containing function"));
3552 inform (DECL_SOURCE_LOCATION (decl), "%q#D declared here", decl);
3553 }
3554 return error_mark_node;
3555 }
3556 return decl;
3557 }
3558
3559 /* ID_EXPRESSION is a representation of parsed, but unprocessed,
3560 id-expression. (See cp_parser_id_expression for details.) SCOPE,
3561 if non-NULL, is the type or namespace used to explicitly qualify
3562 ID_EXPRESSION. DECL is the entity to which that name has been
3563 resolved.
3564
3565 *CONSTANT_EXPRESSION_P is true if we are presently parsing a
3566 constant-expression. In that case, *NON_CONSTANT_EXPRESSION_P will
3567 be set to true if this expression isn't permitted in a
3568 constant-expression, but it is otherwise not set by this function.
3569 *ALLOW_NON_CONSTANT_EXPRESSION_P is true if we are parsing a
3570 constant-expression, but a non-constant expression is also
3571 permissible.
3572
3573 DONE is true if this expression is a complete postfix-expression;
3574 it is false if this expression is followed by '->', '[', '(', etc.
3575 ADDRESS_P is true iff this expression is the operand of '&'.
3576 TEMPLATE_P is true iff the qualified-id was of the form
3577 "A::template B". TEMPLATE_ARG_P is true iff this qualified name
3578 appears as a template argument.
3579
3580 If an error occurs, and it is the kind of error that might cause
3581 the parser to abort a tentative parse, *ERROR_MSG is filled in. It
3582 is the caller's responsibility to issue the message. *ERROR_MSG
3583 will be a string with static storage duration, so the caller need
3584 not "free" it.
3585
3586 Return an expression for the entity, after issuing appropriate
3587 diagnostics. This function is also responsible for transforming a
3588 reference to a non-static member into a COMPONENT_REF that makes
3589 the use of "this" explicit.
3590
3591 Upon return, *IDK will be filled in appropriately. */
3592 static cp_expr
3593 finish_id_expression_1 (tree id_expression,
3594 tree decl,
3595 tree scope,
3596 cp_id_kind *idk,
3597 bool integral_constant_expression_p,
3598 bool allow_non_integral_constant_expression_p,
3599 bool *non_integral_constant_expression_p,
3600 bool template_p,
3601 bool done,
3602 bool address_p,
3603 bool template_arg_p,
3604 const char **error_msg,
3605 location_t location)
3606 {
3607 decl = strip_using_decl (decl);
3608
3609 /* Initialize the output parameters. */
3610 *idk = CP_ID_KIND_NONE;
3611 *error_msg = NULL;
3612
3613 if (id_expression == error_mark_node)
3614 return error_mark_node;
3615 /* If we have a template-id, then no further lookup is
3616 required. If the template-id was for a template-class, we
3617 will sometimes have a TYPE_DECL at this point. */
3618 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3619 || TREE_CODE (decl) == TYPE_DECL)
3620 ;
3621 /* Look up the name. */
3622 else
3623 {
3624 if (decl == error_mark_node)
3625 {
3626 /* Name lookup failed. */
3627 if (scope
3628 && (!TYPE_P (scope)
3629 || (!dependent_type_p (scope)
3630 && !(identifier_p (id_expression)
3631 && IDENTIFIER_CONV_OP_P (id_expression)
3632 && dependent_type_p (TREE_TYPE (id_expression))))))
3633 {
3634 /* If the qualifying type is non-dependent (and the name
3635 does not name a conversion operator to a dependent
3636 type), issue an error. */
3637 qualified_name_lookup_error (scope, id_expression, decl, location);
3638 return error_mark_node;
3639 }
3640 else if (!scope)
3641 {
3642 /* It may be resolved via Koenig lookup. */
3643 *idk = CP_ID_KIND_UNQUALIFIED;
3644 return id_expression;
3645 }
3646 else
3647 decl = id_expression;
3648 }
3649
3650 /* Remember that the name was used in the definition of
3651 the current class so that we can check later to see if
3652 the meaning would have been different after the class
3653 was entirely defined. */
3654 if (!scope && decl != error_mark_node && identifier_p (id_expression))
3655 maybe_note_name_used_in_class (id_expression, decl);
3656
3657 /* A use in unevaluated operand might not be instantiated appropriately
3658 if tsubst_copy builds a dummy parm, or if we never instantiate a
3659 generic lambda, so mark it now. */
3660 if (processing_template_decl && cp_unevaluated_operand)
3661 mark_type_use (decl);
3662
3663 /* Disallow uses of local variables from containing functions, except
3664 within lambda-expressions. */
3665 if (outer_automatic_var_p (decl))
3666 {
3667 decl = process_outer_var_ref (decl, tf_warning_or_error);
3668 if (decl == error_mark_node)
3669 return error_mark_node;
3670 }
3671
3672 /* Also disallow uses of function parameters outside the function
3673 body, except inside an unevaluated context (i.e. decltype). */
3674 if (TREE_CODE (decl) == PARM_DECL
3675 && DECL_CONTEXT (decl) == NULL_TREE
3676 && !cp_unevaluated_operand)
3677 {
3678 *error_msg = G_("use of parameter outside function body");
3679 return error_mark_node;
3680 }
3681 }
3682
3683 /* If we didn't find anything, or what we found was a type,
3684 then this wasn't really an id-expression. */
3685 if (TREE_CODE (decl) == TEMPLATE_DECL
3686 && !DECL_FUNCTION_TEMPLATE_P (decl))
3687 {
3688 *error_msg = G_("missing template arguments");
3689 return error_mark_node;
3690 }
3691 else if (TREE_CODE (decl) == TYPE_DECL
3692 || TREE_CODE (decl) == NAMESPACE_DECL)
3693 {
3694 *error_msg = G_("expected primary-expression");
3695 return error_mark_node;
3696 }
3697
3698 /* If the name resolved to a template parameter, there is no
3699 need to look it up again later. */
3700 if ((TREE_CODE (decl) == CONST_DECL && DECL_TEMPLATE_PARM_P (decl))
3701 || TREE_CODE (decl) == TEMPLATE_PARM_INDEX)
3702 {
3703 tree r;
3704
3705 *idk = CP_ID_KIND_NONE;
3706 if (TREE_CODE (decl) == TEMPLATE_PARM_INDEX)
3707 decl = TEMPLATE_PARM_DECL (decl);
3708 r = DECL_INITIAL (decl);
3709 if (CLASS_TYPE_P (TREE_TYPE (r)) && !CP_TYPE_CONST_P (TREE_TYPE (r)))
3710 {
3711 /* If the entity is a template parameter object for a template
3712 parameter of type T, the type of the expression is const T. */
3713 tree ctype = TREE_TYPE (r);
3714 ctype = cp_build_qualified_type (ctype, (cp_type_quals (ctype)
3715 | TYPE_QUAL_CONST));
3716 r = build1 (VIEW_CONVERT_EXPR, ctype, r);
3717 }
3718 r = convert_from_reference (r);
3719 if (integral_constant_expression_p
3720 && !dependent_type_p (TREE_TYPE (decl))
3721 && !(INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (r))))
3722 {
3723 if (!allow_non_integral_constant_expression_p)
3724 error ("template parameter %qD of type %qT is not allowed in "
3725 "an integral constant expression because it is not of "
3726 "integral or enumeration type", decl, TREE_TYPE (decl));
3727 *non_integral_constant_expression_p = true;
3728 }
3729 return r;
3730 }
3731 else
3732 {
3733 bool dependent_p = type_dependent_expression_p (decl);
3734
3735 /* If the declaration was explicitly qualified indicate
3736 that. The semantics of `A::f(3)' are different than
3737 `f(3)' if `f' is virtual. */
3738 *idk = (scope
3739 ? CP_ID_KIND_QUALIFIED
3740 : (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3741 ? CP_ID_KIND_TEMPLATE_ID
3742 : (dependent_p
3743 ? CP_ID_KIND_UNQUALIFIED_DEPENDENT
3744 : CP_ID_KIND_UNQUALIFIED)));
3745
3746 if (dependent_p
3747 && DECL_P (decl)
3748 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (decl)))
3749 /* Dependent type attributes on the decl mean that the TREE_TYPE is
3750 wrong, so just return the identifier. */
3751 return id_expression;
3752
3753 if (TREE_CODE (decl) == NAMESPACE_DECL)
3754 {
3755 error ("use of namespace %qD as expression", decl);
3756 return error_mark_node;
3757 }
3758 else if (DECL_CLASS_TEMPLATE_P (decl))
3759 {
3760 error ("use of class template %qT as expression", decl);
3761 return error_mark_node;
3762 }
3763 else if (TREE_CODE (decl) == TREE_LIST)
3764 {
3765 /* Ambiguous reference to base members. */
3766 error ("request for member %qD is ambiguous in "
3767 "multiple inheritance lattice", id_expression);
3768 print_candidates (decl);
3769 return error_mark_node;
3770 }
3771
3772 /* Mark variable-like entities as used. Functions are similarly
3773 marked either below or after overload resolution. */
3774 if ((VAR_P (decl)
3775 || TREE_CODE (decl) == PARM_DECL
3776 || TREE_CODE (decl) == CONST_DECL
3777 || TREE_CODE (decl) == RESULT_DECL)
3778 && !mark_used (decl))
3779 return error_mark_node;
3780
3781 /* Only certain kinds of names are allowed in constant
3782 expression. Template parameters have already
3783 been handled above. */
3784 if (! error_operand_p (decl)
3785 && !dependent_p
3786 && integral_constant_expression_p
3787 && ! decl_constant_var_p (decl)
3788 && TREE_CODE (decl) != CONST_DECL
3789 && ! builtin_valid_in_constant_expr_p (decl))
3790 {
3791 if (!allow_non_integral_constant_expression_p)
3792 {
3793 error ("%qD cannot appear in a constant-expression", decl);
3794 return error_mark_node;
3795 }
3796 *non_integral_constant_expression_p = true;
3797 }
3798
3799 if (tree wrap = maybe_get_tls_wrapper_call (decl))
3800 /* Replace an evaluated use of the thread_local variable with
3801 a call to its wrapper. */
3802 decl = wrap;
3803 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3804 && !dependent_p
3805 && variable_template_p (TREE_OPERAND (decl, 0)))
3806 {
3807 decl = finish_template_variable (decl);
3808 mark_used (decl);
3809 decl = convert_from_reference (decl);
3810 }
3811 else if (scope)
3812 {
3813 if (TREE_CODE (decl) == SCOPE_REF)
3814 {
3815 gcc_assert (same_type_p (scope, TREE_OPERAND (decl, 0)));
3816 decl = TREE_OPERAND (decl, 1);
3817 }
3818
3819 decl = (adjust_result_of_qualified_name_lookup
3820 (decl, scope, current_nonlambda_class_type()));
3821
3822 if (TREE_CODE (decl) == FUNCTION_DECL)
3823 mark_used (decl);
3824
3825 if (TYPE_P (scope))
3826 decl = finish_qualified_id_expr (scope,
3827 decl,
3828 done,
3829 address_p,
3830 template_p,
3831 template_arg_p,
3832 tf_warning_or_error);
3833 else
3834 decl = convert_from_reference (decl);
3835 }
3836 else if (TREE_CODE (decl) == FIELD_DECL)
3837 {
3838 /* Since SCOPE is NULL here, this is an unqualified name.
3839 Access checking has been performed during name lookup
3840 already. Turn off checking to avoid duplicate errors. */
3841 push_deferring_access_checks (dk_no_check);
3842 decl = finish_non_static_data_member (decl, NULL_TREE,
3843 /*qualifying_scope=*/NULL_TREE);
3844 pop_deferring_access_checks ();
3845 }
3846 else if (is_overloaded_fn (decl))
3847 {
3848 tree first_fn = get_first_fn (decl);
3849
3850 if (TREE_CODE (first_fn) == TEMPLATE_DECL)
3851 first_fn = DECL_TEMPLATE_RESULT (first_fn);
3852
3853 /* [basic.def.odr]: "A function whose name appears as a
3854 potentially-evaluated expression is odr-used if it is the unique
3855 lookup result".
3856
3857 But only mark it if it's a complete postfix-expression; in a call,
3858 ADL might select a different function, and we'll call mark_used in
3859 build_over_call. */
3860 if (done
3861 && !really_overloaded_fn (decl)
3862 && !mark_used (first_fn))
3863 return error_mark_node;
3864
3865 if (!template_arg_p
3866 && (TREE_CODE (first_fn) == USING_DECL
3867 || (TREE_CODE (first_fn) == FUNCTION_DECL
3868 && DECL_FUNCTION_MEMBER_P (first_fn)
3869 && !shared_member_p (decl))))
3870 {
3871 /* A set of member functions. */
3872 decl = maybe_dummy_object (DECL_CONTEXT (first_fn), 0);
3873 return finish_class_member_access_expr (decl, id_expression,
3874 /*template_p=*/false,
3875 tf_warning_or_error);
3876 }
3877
3878 decl = baselink_for_fns (decl);
3879 }
3880 else
3881 {
3882 if (DECL_P (decl) && DECL_NONLOCAL (decl)
3883 && DECL_CLASS_SCOPE_P (decl))
3884 {
3885 tree context = context_for_name_lookup (decl);
3886 if (context != current_class_type)
3887 {
3888 tree path = currently_open_derived_class (context);
3889 perform_or_defer_access_check (TYPE_BINFO (path),
3890 decl, decl,
3891 tf_warning_or_error);
3892 }
3893 }
3894
3895 decl = convert_from_reference (decl);
3896 }
3897 }
3898
3899 return cp_expr (decl, location);
3900 }
3901
3902 /* As per finish_id_expression_1, but adding a wrapper node
3903 around the result if needed to express LOCATION. */
3904
3905 cp_expr
3906 finish_id_expression (tree id_expression,
3907 tree decl,
3908 tree scope,
3909 cp_id_kind *idk,
3910 bool integral_constant_expression_p,
3911 bool allow_non_integral_constant_expression_p,
3912 bool *non_integral_constant_expression_p,
3913 bool template_p,
3914 bool done,
3915 bool address_p,
3916 bool template_arg_p,
3917 const char **error_msg,
3918 location_t location)
3919 {
3920 cp_expr result
3921 = finish_id_expression_1 (id_expression, decl, scope, idk,
3922 integral_constant_expression_p,
3923 allow_non_integral_constant_expression_p,
3924 non_integral_constant_expression_p,
3925 template_p, done, address_p, template_arg_p,
3926 error_msg, location);
3927 return result.maybe_add_location_wrapper ();
3928 }
3929
3930 /* Implement the __typeof keyword: Return the type of EXPR, suitable for
3931 use as a type-specifier. */
3932
3933 tree
3934 finish_typeof (tree expr)
3935 {
3936 tree type;
3937
3938 if (type_dependent_expression_p (expr))
3939 {
3940 type = cxx_make_type (TYPEOF_TYPE);
3941 TYPEOF_TYPE_EXPR (type) = expr;
3942 SET_TYPE_STRUCTURAL_EQUALITY (type);
3943
3944 return type;
3945 }
3946
3947 expr = mark_type_use (expr);
3948
3949 type = unlowered_expr_type (expr);
3950
3951 if (!type || type == unknown_type_node)
3952 {
3953 error ("type of %qE is unknown", expr);
3954 return error_mark_node;
3955 }
3956
3957 return type;
3958 }
3959
3960 /* Implement the __underlying_type keyword: Return the underlying
3961 type of TYPE, suitable for use as a type-specifier. */
3962
3963 tree
3964 finish_underlying_type (tree type)
3965 {
3966 tree underlying_type;
3967
3968 if (processing_template_decl)
3969 {
3970 underlying_type = cxx_make_type (UNDERLYING_TYPE);
3971 UNDERLYING_TYPE_TYPE (underlying_type) = type;
3972 SET_TYPE_STRUCTURAL_EQUALITY (underlying_type);
3973
3974 return underlying_type;
3975 }
3976
3977 if (!complete_type_or_else (type, NULL_TREE))
3978 return error_mark_node;
3979
3980 if (TREE_CODE (type) != ENUMERAL_TYPE)
3981 {
3982 error ("%qT is not an enumeration type", type);
3983 return error_mark_node;
3984 }
3985
3986 underlying_type = ENUM_UNDERLYING_TYPE (type);
3987
3988 /* Fixup necessary in this case because ENUM_UNDERLYING_TYPE
3989 includes TYPE_MIN_VALUE and TYPE_MAX_VALUE information.
3990 See finish_enum_value_list for details. */
3991 if (!ENUM_FIXED_UNDERLYING_TYPE_P (type))
3992 underlying_type
3993 = c_common_type_for_mode (TYPE_MODE (underlying_type),
3994 TYPE_UNSIGNED (underlying_type));
3995
3996 return underlying_type;
3997 }
3998
3999 /* Implement the __direct_bases keyword: Return the direct base classes
4000 of type. */
4001
4002 tree
4003 calculate_direct_bases (tree type, tsubst_flags_t complain)
4004 {
4005 if (!complete_type_or_maybe_complain (type, NULL_TREE, complain)
4006 || !NON_UNION_CLASS_TYPE_P (type))
4007 return make_tree_vec (0);
4008
4009 vec<tree, va_gc> *vector = make_tree_vector ();
4010 vec<tree, va_gc> *base_binfos = BINFO_BASE_BINFOS (TYPE_BINFO (type));
4011 tree binfo;
4012 unsigned i;
4013
4014 /* Virtual bases are initialized first */
4015 for (i = 0; base_binfos->iterate (i, &binfo); i++)
4016 if (BINFO_VIRTUAL_P (binfo))
4017 vec_safe_push (vector, binfo);
4018
4019 /* Now non-virtuals */
4020 for (i = 0; base_binfos->iterate (i, &binfo); i++)
4021 if (!BINFO_VIRTUAL_P (binfo))
4022 vec_safe_push (vector, binfo);
4023
4024 tree bases_vec = make_tree_vec (vector->length ());
4025
4026 for (i = 0; i < vector->length (); ++i)
4027 TREE_VEC_ELT (bases_vec, i) = BINFO_TYPE ((*vector)[i]);
4028
4029 release_tree_vector (vector);
4030 return bases_vec;
4031 }
4032
4033 /* Implement the __bases keyword: Return the base classes
4034 of type */
4035
4036 /* Find morally non-virtual base classes by walking binfo hierarchy */
4037 /* Virtual base classes are handled separately in finish_bases */
4038
4039 static tree
4040 dfs_calculate_bases_pre (tree binfo, void * /*data_*/)
4041 {
4042 /* Don't walk bases of virtual bases */
4043 return BINFO_VIRTUAL_P (binfo) ? dfs_skip_bases : NULL_TREE;
4044 }
4045
4046 static tree
4047 dfs_calculate_bases_post (tree binfo, void *data_)
4048 {
4049 vec<tree, va_gc> **data = ((vec<tree, va_gc> **) data_);
4050 if (!BINFO_VIRTUAL_P (binfo))
4051 vec_safe_push (*data, BINFO_TYPE (binfo));
4052 return NULL_TREE;
4053 }
4054
4055 /* Calculates the morally non-virtual base classes of a class */
4056 static vec<tree, va_gc> *
4057 calculate_bases_helper (tree type)
4058 {
4059 vec<tree, va_gc> *vector = make_tree_vector ();
4060
4061 /* Now add non-virtual base classes in order of construction */
4062 if (TYPE_BINFO (type))
4063 dfs_walk_all (TYPE_BINFO (type),
4064 dfs_calculate_bases_pre, dfs_calculate_bases_post, &vector);
4065 return vector;
4066 }
4067
4068 tree
4069 calculate_bases (tree type, tsubst_flags_t complain)
4070 {
4071 if (!complete_type_or_maybe_complain (type, NULL_TREE, complain)
4072 || !NON_UNION_CLASS_TYPE_P (type))
4073 return make_tree_vec (0);
4074
4075 vec<tree, va_gc> *vector = make_tree_vector ();
4076 tree bases_vec = NULL_TREE;
4077 unsigned i;
4078 vec<tree, va_gc> *vbases;
4079 vec<tree, va_gc> *nonvbases;
4080 tree binfo;
4081
4082 /* First go through virtual base classes */
4083 for (vbases = CLASSTYPE_VBASECLASSES (type), i = 0;
4084 vec_safe_iterate (vbases, i, &binfo); i++)
4085 {
4086 vec<tree, va_gc> *vbase_bases
4087 = calculate_bases_helper (BINFO_TYPE (binfo));
4088 vec_safe_splice (vector, vbase_bases);
4089 release_tree_vector (vbase_bases);
4090 }
4091
4092 /* Now for the non-virtual bases */
4093 nonvbases = calculate_bases_helper (type);
4094 vec_safe_splice (vector, nonvbases);
4095 release_tree_vector (nonvbases);
4096
4097 /* Note that during error recovery vector->length can even be zero. */
4098 if (vector->length () > 1)
4099 {
4100 /* Last element is entire class, so don't copy */
4101 bases_vec = make_tree_vec (vector->length () - 1);
4102
4103 for (i = 0; i < vector->length () - 1; ++i)
4104 TREE_VEC_ELT (bases_vec, i) = (*vector)[i];
4105 }
4106 else
4107 bases_vec = make_tree_vec (0);
4108
4109 release_tree_vector (vector);
4110 return bases_vec;
4111 }
4112
4113 tree
4114 finish_bases (tree type, bool direct)
4115 {
4116 tree bases = NULL_TREE;
4117
4118 if (!processing_template_decl)
4119 {
4120 /* Parameter packs can only be used in templates */
4121 error ("Parameter pack __bases only valid in template declaration");
4122 return error_mark_node;
4123 }
4124
4125 bases = cxx_make_type (BASES);
4126 BASES_TYPE (bases) = type;
4127 BASES_DIRECT (bases) = direct;
4128 SET_TYPE_STRUCTURAL_EQUALITY (bases);
4129
4130 return bases;
4131 }
4132
4133 /* Perform C++-specific checks for __builtin_offsetof before calling
4134 fold_offsetof. */
4135
4136 tree
4137 finish_offsetof (tree object_ptr, tree expr, location_t loc)
4138 {
4139 /* If we're processing a template, we can't finish the semantics yet.
4140 Otherwise we can fold the entire expression now. */
4141 if (processing_template_decl)
4142 {
4143 expr = build2 (OFFSETOF_EXPR, size_type_node, expr, object_ptr);
4144 SET_EXPR_LOCATION (expr, loc);
4145 return expr;
4146 }
4147
4148 if (TREE_CODE (expr) == PSEUDO_DTOR_EXPR)
4149 {
4150 error ("cannot apply %<offsetof%> to destructor %<~%T%>",
4151 TREE_OPERAND (expr, 2));
4152 return error_mark_node;
4153 }
4154 if (TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE
4155 || TREE_CODE (TREE_TYPE (expr)) == METHOD_TYPE
4156 || TREE_TYPE (expr) == unknown_type_node)
4157 {
4158 while (TREE_CODE (expr) == COMPONENT_REF
4159 || TREE_CODE (expr) == COMPOUND_EXPR)
4160 expr = TREE_OPERAND (expr, 1);
4161
4162 if (DECL_P (expr))
4163 {
4164 error ("cannot apply %<offsetof%> to member function %qD", expr);
4165 inform (DECL_SOURCE_LOCATION (expr), "declared here");
4166 }
4167 else
4168 error ("cannot apply %<offsetof%> to member function");
4169 return error_mark_node;
4170 }
4171 if (TREE_CODE (expr) == CONST_DECL)
4172 {
4173 error ("cannot apply %<offsetof%> to an enumerator %qD", expr);
4174 return error_mark_node;
4175 }
4176 if (REFERENCE_REF_P (expr))
4177 expr = TREE_OPERAND (expr, 0);
4178 if (!complete_type_or_else (TREE_TYPE (TREE_TYPE (object_ptr)), object_ptr))
4179 return error_mark_node;
4180 if (warn_invalid_offsetof
4181 && CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (object_ptr)))
4182 && CLASSTYPE_NON_STD_LAYOUT (TREE_TYPE (TREE_TYPE (object_ptr)))
4183 && cp_unevaluated_operand == 0)
4184 warning_at (loc, OPT_Winvalid_offsetof, "offsetof within "
4185 "non-standard-layout type %qT is conditionally-supported",
4186 TREE_TYPE (TREE_TYPE (object_ptr)));
4187 return fold_offsetof (expr);
4188 }
4189
4190 /* Replace the AGGR_INIT_EXPR at *TP with an equivalent CALL_EXPR. This
4191 function is broken out from the above for the benefit of the tree-ssa
4192 project. */
4193
4194 void
4195 simplify_aggr_init_expr (tree *tp)
4196 {
4197 tree aggr_init_expr = *tp;
4198
4199 /* Form an appropriate CALL_EXPR. */
4200 tree fn = AGGR_INIT_EXPR_FN (aggr_init_expr);
4201 tree slot = AGGR_INIT_EXPR_SLOT (aggr_init_expr);
4202 tree type = TREE_TYPE (slot);
4203
4204 tree call_expr;
4205 enum style_t { ctor, arg, pcc } style;
4206
4207 if (AGGR_INIT_VIA_CTOR_P (aggr_init_expr))
4208 style = ctor;
4209 #ifdef PCC_STATIC_STRUCT_RETURN
4210 else if (1)
4211 style = pcc;
4212 #endif
4213 else
4214 {
4215 gcc_assert (TREE_ADDRESSABLE (type));
4216 style = arg;
4217 }
4218
4219 call_expr = build_call_array_loc (input_location,
4220 TREE_TYPE (TREE_TYPE (TREE_TYPE (fn))),
4221 fn,
4222 aggr_init_expr_nargs (aggr_init_expr),
4223 AGGR_INIT_EXPR_ARGP (aggr_init_expr));
4224 TREE_NOTHROW (call_expr) = TREE_NOTHROW (aggr_init_expr);
4225 CALL_FROM_THUNK_P (call_expr) = AGGR_INIT_FROM_THUNK_P (aggr_init_expr);
4226 CALL_EXPR_OPERATOR_SYNTAX (call_expr)
4227 = CALL_EXPR_OPERATOR_SYNTAX (aggr_init_expr);
4228 CALL_EXPR_ORDERED_ARGS (call_expr) = CALL_EXPR_ORDERED_ARGS (aggr_init_expr);
4229 CALL_EXPR_REVERSE_ARGS (call_expr) = CALL_EXPR_REVERSE_ARGS (aggr_init_expr);
4230
4231 if (style == ctor)
4232 {
4233 /* Replace the first argument to the ctor with the address of the
4234 slot. */
4235 cxx_mark_addressable (slot);
4236 CALL_EXPR_ARG (call_expr, 0) =
4237 build1 (ADDR_EXPR, build_pointer_type (type), slot);
4238 }
4239 else if (style == arg)
4240 {
4241 /* Just mark it addressable here, and leave the rest to
4242 expand_call{,_inline}. */
4243 cxx_mark_addressable (slot);
4244 CALL_EXPR_RETURN_SLOT_OPT (call_expr) = true;
4245 call_expr = build2 (INIT_EXPR, TREE_TYPE (call_expr), slot, call_expr);
4246 }
4247 else if (style == pcc)
4248 {
4249 /* If we're using the non-reentrant PCC calling convention, then we
4250 need to copy the returned value out of the static buffer into the
4251 SLOT. */
4252 push_deferring_access_checks (dk_no_check);
4253 call_expr = build_aggr_init (slot, call_expr,
4254 DIRECT_BIND | LOOKUP_ONLYCONVERTING,
4255 tf_warning_or_error);
4256 pop_deferring_access_checks ();
4257 call_expr = build2 (COMPOUND_EXPR, TREE_TYPE (slot), call_expr, slot);
4258 }
4259
4260 if (AGGR_INIT_ZERO_FIRST (aggr_init_expr))
4261 {
4262 tree init = build_zero_init (type, NULL_TREE,
4263 /*static_storage_p=*/false);
4264 init = build2 (INIT_EXPR, void_type_node, slot, init);
4265 call_expr = build2 (COMPOUND_EXPR, TREE_TYPE (call_expr),
4266 init, call_expr);
4267 }
4268
4269 *tp = call_expr;
4270 }
4271
4272 /* Emit all thunks to FN that should be emitted when FN is emitted. */
4273
4274 void
4275 emit_associated_thunks (tree fn)
4276 {
4277 /* When we use vcall offsets, we emit thunks with the virtual
4278 functions to which they thunk. The whole point of vcall offsets
4279 is so that you can know statically the entire set of thunks that
4280 will ever be needed for a given virtual function, thereby
4281 enabling you to output all the thunks with the function itself. */
4282 if (DECL_VIRTUAL_P (fn)
4283 /* Do not emit thunks for extern template instantiations. */
4284 && ! DECL_REALLY_EXTERN (fn))
4285 {
4286 tree thunk;
4287
4288 for (thunk = DECL_THUNKS (fn); thunk; thunk = DECL_CHAIN (thunk))
4289 {
4290 if (!THUNK_ALIAS (thunk))
4291 {
4292 use_thunk (thunk, /*emit_p=*/1);
4293 if (DECL_RESULT_THUNK_P (thunk))
4294 {
4295 tree probe;
4296
4297 for (probe = DECL_THUNKS (thunk);
4298 probe; probe = DECL_CHAIN (probe))
4299 use_thunk (probe, /*emit_p=*/1);
4300 }
4301 }
4302 else
4303 gcc_assert (!DECL_THUNKS (thunk));
4304 }
4305 }
4306 }
4307
4308 /* Generate RTL for FN. */
4309
4310 bool
4311 expand_or_defer_fn_1 (tree fn)
4312 {
4313 /* When the parser calls us after finishing the body of a template
4314 function, we don't really want to expand the body. */
4315 if (processing_template_decl)
4316 {
4317 /* Normally, collection only occurs in rest_of_compilation. So,
4318 if we don't collect here, we never collect junk generated
4319 during the processing of templates until we hit a
4320 non-template function. It's not safe to do this inside a
4321 nested class, though, as the parser may have local state that
4322 is not a GC root. */
4323 if (!function_depth)
4324 ggc_collect ();
4325 return false;
4326 }
4327
4328 gcc_assert (DECL_SAVED_TREE (fn));
4329
4330 /* We make a decision about linkage for these functions at the end
4331 of the compilation. Until that point, we do not want the back
4332 end to output them -- but we do want it to see the bodies of
4333 these functions so that it can inline them as appropriate. */
4334 if (DECL_DECLARED_INLINE_P (fn) || DECL_IMPLICIT_INSTANTIATION (fn))
4335 {
4336 if (DECL_INTERFACE_KNOWN (fn))
4337 /* We've already made a decision as to how this function will
4338 be handled. */;
4339 else if (!at_eof)
4340 tentative_decl_linkage (fn);
4341 else
4342 import_export_decl (fn);
4343
4344 /* If the user wants us to keep all inline functions, then mark
4345 this function as needed so that finish_file will make sure to
4346 output it later. Similarly, all dllexport'd functions must
4347 be emitted; there may be callers in other DLLs. */
4348 if (DECL_DECLARED_INLINE_P (fn)
4349 && !DECL_REALLY_EXTERN (fn)
4350 && (flag_keep_inline_functions
4351 || (flag_keep_inline_dllexport
4352 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (fn)))))
4353 {
4354 mark_needed (fn);
4355 DECL_EXTERNAL (fn) = 0;
4356 }
4357 }
4358
4359 /* If this is a constructor or destructor body, we have to clone
4360 it. */
4361 if (maybe_clone_body (fn))
4362 {
4363 /* We don't want to process FN again, so pretend we've written
4364 it out, even though we haven't. */
4365 TREE_ASM_WRITTEN (fn) = 1;
4366 /* If this is a constexpr function, keep DECL_SAVED_TREE. */
4367 if (!DECL_DECLARED_CONSTEXPR_P (fn))
4368 DECL_SAVED_TREE (fn) = NULL_TREE;
4369 return false;
4370 }
4371
4372 /* There's no reason to do any of the work here if we're only doing
4373 semantic analysis; this code just generates RTL. */
4374 if (flag_syntax_only)
4375 {
4376 /* Pretend that this function has been written out so that we don't try
4377 to expand it again. */
4378 TREE_ASM_WRITTEN (fn) = 1;
4379 return false;
4380 }
4381
4382 return true;
4383 }
4384
4385 void
4386 expand_or_defer_fn (tree fn)
4387 {
4388 if (expand_or_defer_fn_1 (fn))
4389 {
4390 function_depth++;
4391
4392 /* Expand or defer, at the whim of the compilation unit manager. */
4393 cgraph_node::finalize_function (fn, function_depth > 1);
4394 emit_associated_thunks (fn);
4395
4396 function_depth--;
4397 }
4398 }
4399
4400 struct nrv_data
4401 {
4402 nrv_data () : visited (37) {}
4403
4404 tree var;
4405 tree result;
4406 hash_table<nofree_ptr_hash <tree_node> > visited;
4407 };
4408
4409 /* Helper function for walk_tree, used by finalize_nrv below. */
4410
4411 static tree
4412 finalize_nrv_r (tree* tp, int* walk_subtrees, void* data)
4413 {
4414 struct nrv_data *dp = (struct nrv_data *)data;
4415 tree_node **slot;
4416
4417 /* No need to walk into types. There wouldn't be any need to walk into
4418 non-statements, except that we have to consider STMT_EXPRs. */
4419 if (TYPE_P (*tp))
4420 *walk_subtrees = 0;
4421 /* Change all returns to just refer to the RESULT_DECL; this is a nop,
4422 but differs from using NULL_TREE in that it indicates that we care
4423 about the value of the RESULT_DECL. */
4424 else if (TREE_CODE (*tp) == RETURN_EXPR)
4425 TREE_OPERAND (*tp, 0) = dp->result;
4426 /* Change all cleanups for the NRV to only run when an exception is
4427 thrown. */
4428 else if (TREE_CODE (*tp) == CLEANUP_STMT
4429 && CLEANUP_DECL (*tp) == dp->var)
4430 CLEANUP_EH_ONLY (*tp) = 1;
4431 /* Replace the DECL_EXPR for the NRV with an initialization of the
4432 RESULT_DECL, if needed. */
4433 else if (TREE_CODE (*tp) == DECL_EXPR
4434 && DECL_EXPR_DECL (*tp) == dp->var)
4435 {
4436 tree init;
4437 if (DECL_INITIAL (dp->var)
4438 && DECL_INITIAL (dp->var) != error_mark_node)
4439 init = build2 (INIT_EXPR, void_type_node, dp->result,
4440 DECL_INITIAL (dp->var));
4441 else
4442 init = build_empty_stmt (EXPR_LOCATION (*tp));
4443 DECL_INITIAL (dp->var) = NULL_TREE;
4444 SET_EXPR_LOCATION (init, EXPR_LOCATION (*tp));
4445 *tp = init;
4446 }
4447 /* And replace all uses of the NRV with the RESULT_DECL. */
4448 else if (*tp == dp->var)
4449 *tp = dp->result;
4450
4451 /* Avoid walking into the same tree more than once. Unfortunately, we
4452 can't just use walk_tree_without duplicates because it would only call
4453 us for the first occurrence of dp->var in the function body. */
4454 slot = dp->visited.find_slot (*tp, INSERT);
4455 if (*slot)
4456 *walk_subtrees = 0;
4457 else
4458 *slot = *tp;
4459
4460 /* Keep iterating. */
4461 return NULL_TREE;
4462 }
4463
4464 /* Called from finish_function to implement the named return value
4465 optimization by overriding all the RETURN_EXPRs and pertinent
4466 CLEANUP_STMTs and replacing all occurrences of VAR with RESULT, the
4467 RESULT_DECL for the function. */
4468
4469 void
4470 finalize_nrv (tree *tp, tree var, tree result)
4471 {
4472 struct nrv_data data;
4473
4474 /* Copy name from VAR to RESULT. */
4475 DECL_NAME (result) = DECL_NAME (var);
4476 /* Don't forget that we take its address. */
4477 TREE_ADDRESSABLE (result) = TREE_ADDRESSABLE (var);
4478 /* Finally set DECL_VALUE_EXPR to avoid assigning
4479 a stack slot at -O0 for the original var and debug info
4480 uses RESULT location for VAR. */
4481 SET_DECL_VALUE_EXPR (var, result);
4482 DECL_HAS_VALUE_EXPR_P (var) = 1;
4483
4484 data.var = var;
4485 data.result = result;
4486 cp_walk_tree (tp, finalize_nrv_r, &data, 0);
4487 }
4488 \f
4489 /* Create CP_OMP_CLAUSE_INFO for clause C. Returns true if it is invalid. */
4490
4491 bool
4492 cxx_omp_create_clause_info (tree c, tree type, bool need_default_ctor,
4493 bool need_copy_ctor, bool need_copy_assignment,
4494 bool need_dtor)
4495 {
4496 int save_errorcount = errorcount;
4497 tree info, t;
4498
4499 /* Always allocate 3 elements for simplicity. These are the
4500 function decls for the ctor, dtor, and assignment op.
4501 This layout is known to the three lang hooks,
4502 cxx_omp_clause_default_init, cxx_omp_clause_copy_init,
4503 and cxx_omp_clause_assign_op. */
4504 info = make_tree_vec (3);
4505 CP_OMP_CLAUSE_INFO (c) = info;
4506
4507 if (need_default_ctor || need_copy_ctor)
4508 {
4509 if (need_default_ctor)
4510 t = get_default_ctor (type);
4511 else
4512 t = get_copy_ctor (type, tf_warning_or_error);
4513
4514 if (t && !trivial_fn_p (t))
4515 TREE_VEC_ELT (info, 0) = t;
4516 }
4517
4518 if (need_dtor && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
4519 TREE_VEC_ELT (info, 1) = get_dtor (type, tf_warning_or_error);
4520
4521 if (need_copy_assignment)
4522 {
4523 t = get_copy_assign (type);
4524
4525 if (t && !trivial_fn_p (t))
4526 TREE_VEC_ELT (info, 2) = t;
4527 }
4528
4529 return errorcount != save_errorcount;
4530 }
4531
4532 /* If DECL is DECL_OMP_PRIVATIZED_MEMBER, return corresponding
4533 FIELD_DECL, otherwise return DECL itself. */
4534
4535 static tree
4536 omp_clause_decl_field (tree decl)
4537 {
4538 if (VAR_P (decl)
4539 && DECL_HAS_VALUE_EXPR_P (decl)
4540 && DECL_ARTIFICIAL (decl)
4541 && DECL_LANG_SPECIFIC (decl)
4542 && DECL_OMP_PRIVATIZED_MEMBER (decl))
4543 {
4544 tree f = DECL_VALUE_EXPR (decl);
4545 if (INDIRECT_REF_P (f))
4546 f = TREE_OPERAND (f, 0);
4547 if (TREE_CODE (f) == COMPONENT_REF)
4548 {
4549 f = TREE_OPERAND (f, 1);
4550 gcc_assert (TREE_CODE (f) == FIELD_DECL);
4551 return f;
4552 }
4553 }
4554 return NULL_TREE;
4555 }
4556
4557 /* Adjust DECL if needed for printing using %qE. */
4558
4559 static tree
4560 omp_clause_printable_decl (tree decl)
4561 {
4562 tree t = omp_clause_decl_field (decl);
4563 if (t)
4564 return t;
4565 return decl;
4566 }
4567
4568 /* For a FIELD_DECL F and corresponding DECL_OMP_PRIVATIZED_MEMBER
4569 VAR_DECL T that doesn't need a DECL_EXPR added, record it for
4570 privatization. */
4571
4572 static void
4573 omp_note_field_privatization (tree f, tree t)
4574 {
4575 if (!omp_private_member_map)
4576 omp_private_member_map = new hash_map<tree, tree>;
4577 tree &v = omp_private_member_map->get_or_insert (f);
4578 if (v == NULL_TREE)
4579 {
4580 v = t;
4581 omp_private_member_vec.safe_push (f);
4582 /* Signal that we don't want to create DECL_EXPR for this dummy var. */
4583 omp_private_member_vec.safe_push (integer_zero_node);
4584 }
4585 }
4586
4587 /* Privatize FIELD_DECL T, return corresponding DECL_OMP_PRIVATIZED_MEMBER
4588 dummy VAR_DECL. */
4589
4590 tree
4591 omp_privatize_field (tree t, bool shared)
4592 {
4593 tree m = finish_non_static_data_member (t, NULL_TREE, NULL_TREE);
4594 if (m == error_mark_node)
4595 return error_mark_node;
4596 if (!omp_private_member_map && !shared)
4597 omp_private_member_map = new hash_map<tree, tree>;
4598 if (TYPE_REF_P (TREE_TYPE (t)))
4599 {
4600 gcc_assert (INDIRECT_REF_P (m));
4601 m = TREE_OPERAND (m, 0);
4602 }
4603 tree vb = NULL_TREE;
4604 tree &v = shared ? vb : omp_private_member_map->get_or_insert (t);
4605 if (v == NULL_TREE)
4606 {
4607 v = create_temporary_var (TREE_TYPE (m));
4608 retrofit_lang_decl (v);
4609 DECL_OMP_PRIVATIZED_MEMBER (v) = 1;
4610 SET_DECL_VALUE_EXPR (v, m);
4611 DECL_HAS_VALUE_EXPR_P (v) = 1;
4612 if (!shared)
4613 omp_private_member_vec.safe_push (t);
4614 }
4615 return v;
4616 }
4617
4618 /* Helper function for handle_omp_array_sections. Called recursively
4619 to handle multiple array-section-subscripts. C is the clause,
4620 T current expression (initially OMP_CLAUSE_DECL), which is either
4621 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
4622 expression if specified, TREE_VALUE length expression if specified,
4623 TREE_CHAIN is what it has been specified after, or some decl.
4624 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
4625 set to true if any of the array-section-subscript could have length
4626 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
4627 first array-section-subscript which is known not to have length
4628 of one. Given say:
4629 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
4630 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
4631 all are or may have length of 1, array-section-subscript [:2] is the
4632 first one known not to have length 1. For array-section-subscript
4633 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
4634 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
4635 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
4636 case though, as some lengths could be zero. */
4637
4638 static tree
4639 handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
4640 bool &maybe_zero_len, unsigned int &first_non_one,
4641 enum c_omp_region_type ort)
4642 {
4643 tree ret, low_bound, length, type;
4644 if (TREE_CODE (t) != TREE_LIST)
4645 {
4646 if (error_operand_p (t))
4647 return error_mark_node;
4648 if (REFERENCE_REF_P (t)
4649 && TREE_CODE (TREE_OPERAND (t, 0)) == COMPONENT_REF)
4650 t = TREE_OPERAND (t, 0);
4651 ret = t;
4652 if (TREE_CODE (t) == COMPONENT_REF
4653 && ort == C_ORT_OMP
4654 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
4655 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO
4656 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FROM)
4657 && !type_dependent_expression_p (t))
4658 {
4659 if (TREE_CODE (TREE_OPERAND (t, 1)) == FIELD_DECL
4660 && DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
4661 {
4662 error_at (OMP_CLAUSE_LOCATION (c),
4663 "bit-field %qE in %qs clause",
4664 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4665 return error_mark_node;
4666 }
4667 while (TREE_CODE (t) == COMPONENT_REF)
4668 {
4669 if (TREE_TYPE (TREE_OPERAND (t, 0))
4670 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == UNION_TYPE)
4671 {
4672 error_at (OMP_CLAUSE_LOCATION (c),
4673 "%qE is a member of a union", t);
4674 return error_mark_node;
4675 }
4676 t = TREE_OPERAND (t, 0);
4677 }
4678 if (REFERENCE_REF_P (t))
4679 t = TREE_OPERAND (t, 0);
4680 }
4681 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
4682 {
4683 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
4684 return NULL_TREE;
4685 if (DECL_P (t))
4686 error_at (OMP_CLAUSE_LOCATION (c),
4687 "%qD is not a variable in %qs clause", t,
4688 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4689 else
4690 error_at (OMP_CLAUSE_LOCATION (c),
4691 "%qE is not a variable in %qs clause", t,
4692 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4693 return error_mark_node;
4694 }
4695 else if (ort == C_ORT_OMP
4696 && TREE_CODE (t) == PARM_DECL
4697 && DECL_ARTIFICIAL (t)
4698 && DECL_NAME (t) == this_identifier)
4699 {
4700 error_at (OMP_CLAUSE_LOCATION (c),
4701 "%<this%> allowed in OpenMP only in %<declare simd%>"
4702 " clauses");
4703 return error_mark_node;
4704 }
4705 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
4706 && VAR_P (t) && CP_DECL_THREAD_LOCAL_P (t))
4707 {
4708 error_at (OMP_CLAUSE_LOCATION (c),
4709 "%qD is threadprivate variable in %qs clause", t,
4710 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4711 return error_mark_node;
4712 }
4713 if (type_dependent_expression_p (ret))
4714 return NULL_TREE;
4715 ret = convert_from_reference (ret);
4716 return ret;
4717 }
4718
4719 if (ort == C_ORT_OMP
4720 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
4721 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
4722 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
4723 && TREE_CODE (TREE_CHAIN (t)) == FIELD_DECL)
4724 TREE_CHAIN (t) = omp_privatize_field (TREE_CHAIN (t), false);
4725 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
4726 maybe_zero_len, first_non_one, ort);
4727 if (ret == error_mark_node || ret == NULL_TREE)
4728 return ret;
4729
4730 type = TREE_TYPE (ret);
4731 low_bound = TREE_PURPOSE (t);
4732 length = TREE_VALUE (t);
4733 if ((low_bound && type_dependent_expression_p (low_bound))
4734 || (length && type_dependent_expression_p (length)))
4735 return NULL_TREE;
4736
4737 if (low_bound == error_mark_node || length == error_mark_node)
4738 return error_mark_node;
4739
4740 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
4741 {
4742 error_at (OMP_CLAUSE_LOCATION (c),
4743 "low bound %qE of array section does not have integral type",
4744 low_bound);
4745 return error_mark_node;
4746 }
4747 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
4748 {
4749 error_at (OMP_CLAUSE_LOCATION (c),
4750 "length %qE of array section does not have integral type",
4751 length);
4752 return error_mark_node;
4753 }
4754 if (low_bound)
4755 low_bound = mark_rvalue_use (low_bound);
4756 if (length)
4757 length = mark_rvalue_use (length);
4758 /* We need to reduce to real constant-values for checks below. */
4759 if (length)
4760 length = fold_simple (length);
4761 if (low_bound)
4762 low_bound = fold_simple (low_bound);
4763 if (low_bound
4764 && TREE_CODE (low_bound) == INTEGER_CST
4765 && TYPE_PRECISION (TREE_TYPE (low_bound))
4766 > TYPE_PRECISION (sizetype))
4767 low_bound = fold_convert (sizetype, low_bound);
4768 if (length
4769 && TREE_CODE (length) == INTEGER_CST
4770 && TYPE_PRECISION (TREE_TYPE (length))
4771 > TYPE_PRECISION (sizetype))
4772 length = fold_convert (sizetype, length);
4773 if (low_bound == NULL_TREE)
4774 low_bound = integer_zero_node;
4775
4776 if (length != NULL_TREE)
4777 {
4778 if (!integer_nonzerop (length))
4779 {
4780 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
4781 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
4782 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
4783 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
4784 {
4785 if (integer_zerop (length))
4786 {
4787 error_at (OMP_CLAUSE_LOCATION (c),
4788 "zero length array section in %qs clause",
4789 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4790 return error_mark_node;
4791 }
4792 }
4793 else
4794 maybe_zero_len = true;
4795 }
4796 if (first_non_one == types.length ()
4797 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
4798 first_non_one++;
4799 }
4800 if (TREE_CODE (type) == ARRAY_TYPE)
4801 {
4802 if (length == NULL_TREE
4803 && (TYPE_DOMAIN (type) == NULL_TREE
4804 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
4805 {
4806 error_at (OMP_CLAUSE_LOCATION (c),
4807 "for unknown bound array type length expression must "
4808 "be specified");
4809 return error_mark_node;
4810 }
4811 if (TREE_CODE (low_bound) == INTEGER_CST
4812 && tree_int_cst_sgn (low_bound) == -1)
4813 {
4814 error_at (OMP_CLAUSE_LOCATION (c),
4815 "negative low bound in array section in %qs clause",
4816 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4817 return error_mark_node;
4818 }
4819 if (length != NULL_TREE
4820 && TREE_CODE (length) == INTEGER_CST
4821 && tree_int_cst_sgn (length) == -1)
4822 {
4823 error_at (OMP_CLAUSE_LOCATION (c),
4824 "negative length in array section in %qs clause",
4825 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4826 return error_mark_node;
4827 }
4828 if (TYPE_DOMAIN (type)
4829 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
4830 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
4831 == INTEGER_CST)
4832 {
4833 tree size
4834 = fold_convert (sizetype, TYPE_MAX_VALUE (TYPE_DOMAIN (type)));
4835 size = size_binop (PLUS_EXPR, size, size_one_node);
4836 if (TREE_CODE (low_bound) == INTEGER_CST)
4837 {
4838 if (tree_int_cst_lt (size, low_bound))
4839 {
4840 error_at (OMP_CLAUSE_LOCATION (c),
4841 "low bound %qE above array section size "
4842 "in %qs clause", low_bound,
4843 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4844 return error_mark_node;
4845 }
4846 if (tree_int_cst_equal (size, low_bound))
4847 {
4848 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
4849 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
4850 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
4851 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
4852 {
4853 error_at (OMP_CLAUSE_LOCATION (c),
4854 "zero length array section in %qs clause",
4855 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4856 return error_mark_node;
4857 }
4858 maybe_zero_len = true;
4859 }
4860 else if (length == NULL_TREE
4861 && first_non_one == types.length ()
4862 && tree_int_cst_equal
4863 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
4864 low_bound))
4865 first_non_one++;
4866 }
4867 else if (length == NULL_TREE)
4868 {
4869 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
4870 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
4871 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_IN_REDUCTION
4872 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_TASK_REDUCTION)
4873 maybe_zero_len = true;
4874 if (first_non_one == types.length ())
4875 first_non_one++;
4876 }
4877 if (length && TREE_CODE (length) == INTEGER_CST)
4878 {
4879 if (tree_int_cst_lt (size, length))
4880 {
4881 error_at (OMP_CLAUSE_LOCATION (c),
4882 "length %qE above array section size "
4883 "in %qs clause", length,
4884 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4885 return error_mark_node;
4886 }
4887 if (TREE_CODE (low_bound) == INTEGER_CST)
4888 {
4889 tree lbpluslen
4890 = size_binop (PLUS_EXPR,
4891 fold_convert (sizetype, low_bound),
4892 fold_convert (sizetype, length));
4893 if (TREE_CODE (lbpluslen) == INTEGER_CST
4894 && tree_int_cst_lt (size, lbpluslen))
4895 {
4896 error_at (OMP_CLAUSE_LOCATION (c),
4897 "high bound %qE above array section size "
4898 "in %qs clause", lbpluslen,
4899 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4900 return error_mark_node;
4901 }
4902 }
4903 }
4904 }
4905 else if (length == NULL_TREE)
4906 {
4907 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
4908 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
4909 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_IN_REDUCTION
4910 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_TASK_REDUCTION)
4911 maybe_zero_len = true;
4912 if (first_non_one == types.length ())
4913 first_non_one++;
4914 }
4915
4916 /* For [lb:] we will need to evaluate lb more than once. */
4917 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
4918 {
4919 tree lb = cp_save_expr (low_bound);
4920 if (lb != low_bound)
4921 {
4922 TREE_PURPOSE (t) = lb;
4923 low_bound = lb;
4924 }
4925 }
4926 }
4927 else if (TYPE_PTR_P (type))
4928 {
4929 if (length == NULL_TREE)
4930 {
4931 error_at (OMP_CLAUSE_LOCATION (c),
4932 "for pointer type length expression must be specified");
4933 return error_mark_node;
4934 }
4935 if (length != NULL_TREE
4936 && TREE_CODE (length) == INTEGER_CST
4937 && tree_int_cst_sgn (length) == -1)
4938 {
4939 error_at (OMP_CLAUSE_LOCATION (c),
4940 "negative length in array section in %qs clause",
4941 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4942 return error_mark_node;
4943 }
4944 /* If there is a pointer type anywhere but in the very first
4945 array-section-subscript, the array section can't be contiguous. */
4946 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
4947 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
4948 {
4949 error_at (OMP_CLAUSE_LOCATION (c),
4950 "array section is not contiguous in %qs clause",
4951 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4952 return error_mark_node;
4953 }
4954 }
4955 else
4956 {
4957 error_at (OMP_CLAUSE_LOCATION (c),
4958 "%qE does not have pointer or array type", ret);
4959 return error_mark_node;
4960 }
4961 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
4962 types.safe_push (TREE_TYPE (ret));
4963 /* We will need to evaluate lb more than once. */
4964 tree lb = cp_save_expr (low_bound);
4965 if (lb != low_bound)
4966 {
4967 TREE_PURPOSE (t) = lb;
4968 low_bound = lb;
4969 }
4970 ret = grok_array_decl (OMP_CLAUSE_LOCATION (c), ret, low_bound, false);
4971 return ret;
4972 }
4973
4974 /* Handle array sections for clause C. */
4975
4976 static bool
4977 handle_omp_array_sections (tree c, enum c_omp_region_type ort)
4978 {
4979 bool maybe_zero_len = false;
4980 unsigned int first_non_one = 0;
4981 auto_vec<tree, 10> types;
4982 tree *tp = &OMP_CLAUSE_DECL (c);
4983 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
4984 && TREE_CODE (*tp) == TREE_LIST
4985 && TREE_PURPOSE (*tp)
4986 && TREE_CODE (TREE_PURPOSE (*tp)) == TREE_VEC)
4987 tp = &TREE_VALUE (*tp);
4988 tree first = handle_omp_array_sections_1 (c, *tp, types,
4989 maybe_zero_len, first_non_one,
4990 ort);
4991 if (first == error_mark_node)
4992 return true;
4993 if (first == NULL_TREE)
4994 return false;
4995 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
4996 {
4997 tree t = *tp;
4998 tree tem = NULL_TREE;
4999 if (processing_template_decl)
5000 return false;
5001 /* Need to evaluate side effects in the length expressions
5002 if any. */
5003 while (TREE_CODE (t) == TREE_LIST)
5004 {
5005 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
5006 {
5007 if (tem == NULL_TREE)
5008 tem = TREE_VALUE (t);
5009 else
5010 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
5011 TREE_VALUE (t), tem);
5012 }
5013 t = TREE_CHAIN (t);
5014 }
5015 if (tem)
5016 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
5017 *tp = first;
5018 }
5019 else
5020 {
5021 unsigned int num = types.length (), i;
5022 tree t, side_effects = NULL_TREE, size = NULL_TREE;
5023 tree condition = NULL_TREE;
5024
5025 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
5026 maybe_zero_len = true;
5027 if (processing_template_decl && maybe_zero_len)
5028 return false;
5029
5030 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
5031 t = TREE_CHAIN (t))
5032 {
5033 tree low_bound = TREE_PURPOSE (t);
5034 tree length = TREE_VALUE (t);
5035
5036 i--;
5037 if (low_bound
5038 && TREE_CODE (low_bound) == INTEGER_CST
5039 && TYPE_PRECISION (TREE_TYPE (low_bound))
5040 > TYPE_PRECISION (sizetype))
5041 low_bound = fold_convert (sizetype, low_bound);
5042 if (length
5043 && TREE_CODE (length) == INTEGER_CST
5044 && TYPE_PRECISION (TREE_TYPE (length))
5045 > TYPE_PRECISION (sizetype))
5046 length = fold_convert (sizetype, length);
5047 if (low_bound == NULL_TREE)
5048 low_bound = integer_zero_node;
5049 if (!maybe_zero_len && i > first_non_one)
5050 {
5051 if (integer_nonzerop (low_bound))
5052 goto do_warn_noncontiguous;
5053 if (length != NULL_TREE
5054 && TREE_CODE (length) == INTEGER_CST
5055 && TYPE_DOMAIN (types[i])
5056 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
5057 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
5058 == INTEGER_CST)
5059 {
5060 tree size;
5061 size = size_binop (PLUS_EXPR,
5062 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
5063 size_one_node);
5064 if (!tree_int_cst_equal (length, size))
5065 {
5066 do_warn_noncontiguous:
5067 error_at (OMP_CLAUSE_LOCATION (c),
5068 "array section is not contiguous in %qs "
5069 "clause",
5070 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5071 return true;
5072 }
5073 }
5074 if (!processing_template_decl
5075 && length != NULL_TREE
5076 && TREE_SIDE_EFFECTS (length))
5077 {
5078 if (side_effects == NULL_TREE)
5079 side_effects = length;
5080 else
5081 side_effects = build2 (COMPOUND_EXPR,
5082 TREE_TYPE (side_effects),
5083 length, side_effects);
5084 }
5085 }
5086 else if (processing_template_decl)
5087 continue;
5088 else
5089 {
5090 tree l;
5091
5092 if (i > first_non_one
5093 && ((length && integer_nonzerop (length))
5094 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
5095 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
5096 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION))
5097 continue;
5098 if (length)
5099 l = fold_convert (sizetype, length);
5100 else
5101 {
5102 l = size_binop (PLUS_EXPR,
5103 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
5104 size_one_node);
5105 l = size_binop (MINUS_EXPR, l,
5106 fold_convert (sizetype, low_bound));
5107 }
5108 if (i > first_non_one)
5109 {
5110 l = fold_build2 (NE_EXPR, boolean_type_node, l,
5111 size_zero_node);
5112 if (condition == NULL_TREE)
5113 condition = l;
5114 else
5115 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
5116 l, condition);
5117 }
5118 else if (size == NULL_TREE)
5119 {
5120 size = size_in_bytes (TREE_TYPE (types[i]));
5121 tree eltype = TREE_TYPE (types[num - 1]);
5122 while (TREE_CODE (eltype) == ARRAY_TYPE)
5123 eltype = TREE_TYPE (eltype);
5124 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
5125 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
5126 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
5127 size = size_binop (EXACT_DIV_EXPR, size,
5128 size_in_bytes (eltype));
5129 size = size_binop (MULT_EXPR, size, l);
5130 if (condition)
5131 size = fold_build3 (COND_EXPR, sizetype, condition,
5132 size, size_zero_node);
5133 }
5134 else
5135 size = size_binop (MULT_EXPR, size, l);
5136 }
5137 }
5138 if (!processing_template_decl)
5139 {
5140 if (side_effects)
5141 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
5142 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
5143 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
5144 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
5145 {
5146 size = size_binop (MINUS_EXPR, size, size_one_node);
5147 size = save_expr (size);
5148 tree index_type = build_index_type (size);
5149 tree eltype = TREE_TYPE (first);
5150 while (TREE_CODE (eltype) == ARRAY_TYPE)
5151 eltype = TREE_TYPE (eltype);
5152 tree type = build_array_type (eltype, index_type);
5153 tree ptype = build_pointer_type (eltype);
5154 if (TYPE_REF_P (TREE_TYPE (t))
5155 && INDIRECT_TYPE_P (TREE_TYPE (TREE_TYPE (t))))
5156 t = convert_from_reference (t);
5157 else if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
5158 t = build_fold_addr_expr (t);
5159 tree t2 = build_fold_addr_expr (first);
5160 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
5161 ptrdiff_type_node, t2);
5162 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
5163 ptrdiff_type_node, t2,
5164 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
5165 ptrdiff_type_node, t));
5166 if (tree_fits_shwi_p (t2))
5167 t = build2 (MEM_REF, type, t,
5168 build_int_cst (ptype, tree_to_shwi (t2)));
5169 else
5170 {
5171 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
5172 sizetype, t2);
5173 t = build2_loc (OMP_CLAUSE_LOCATION (c), POINTER_PLUS_EXPR,
5174 TREE_TYPE (t), t, t2);
5175 t = build2 (MEM_REF, type, t, build_int_cst (ptype, 0));
5176 }
5177 OMP_CLAUSE_DECL (c) = t;
5178 return false;
5179 }
5180 OMP_CLAUSE_DECL (c) = first;
5181 OMP_CLAUSE_SIZE (c) = size;
5182 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
5183 || (TREE_CODE (t) == COMPONENT_REF
5184 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE))
5185 return false;
5186 if (ort == C_ORT_OMP || ort == C_ORT_ACC)
5187 switch (OMP_CLAUSE_MAP_KIND (c))
5188 {
5189 case GOMP_MAP_ALLOC:
5190 case GOMP_MAP_TO:
5191 case GOMP_MAP_FROM:
5192 case GOMP_MAP_TOFROM:
5193 case GOMP_MAP_ALWAYS_TO:
5194 case GOMP_MAP_ALWAYS_FROM:
5195 case GOMP_MAP_ALWAYS_TOFROM:
5196 case GOMP_MAP_RELEASE:
5197 case GOMP_MAP_DELETE:
5198 case GOMP_MAP_FORCE_TO:
5199 case GOMP_MAP_FORCE_FROM:
5200 case GOMP_MAP_FORCE_TOFROM:
5201 case GOMP_MAP_FORCE_PRESENT:
5202 OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (c) = 1;
5203 break;
5204 default:
5205 break;
5206 }
5207 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
5208 OMP_CLAUSE_MAP);
5209 if ((ort & C_ORT_OMP_DECLARE_SIMD) != C_ORT_OMP && ort != C_ORT_ACC)
5210 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_POINTER);
5211 else if (TREE_CODE (t) == COMPONENT_REF)
5212 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ALWAYS_POINTER);
5213 else if (REFERENCE_REF_P (t)
5214 && TREE_CODE (TREE_OPERAND (t, 0)) == COMPONENT_REF)
5215 {
5216 t = TREE_OPERAND (t, 0);
5217 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ALWAYS_POINTER);
5218 }
5219 else
5220 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_FIRSTPRIVATE_POINTER);
5221 if (OMP_CLAUSE_MAP_KIND (c2) != GOMP_MAP_FIRSTPRIVATE_POINTER
5222 && !cxx_mark_addressable (t))
5223 return false;
5224 OMP_CLAUSE_DECL (c2) = t;
5225 t = build_fold_addr_expr (first);
5226 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
5227 ptrdiff_type_node, t);
5228 tree ptr = OMP_CLAUSE_DECL (c2);
5229 ptr = convert_from_reference (ptr);
5230 if (!INDIRECT_TYPE_P (TREE_TYPE (ptr)))
5231 ptr = build_fold_addr_expr (ptr);
5232 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
5233 ptrdiff_type_node, t,
5234 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
5235 ptrdiff_type_node, ptr));
5236 OMP_CLAUSE_SIZE (c2) = t;
5237 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
5238 OMP_CLAUSE_CHAIN (c) = c2;
5239 ptr = OMP_CLAUSE_DECL (c2);
5240 if (OMP_CLAUSE_MAP_KIND (c2) != GOMP_MAP_FIRSTPRIVATE_POINTER
5241 && TYPE_REF_P (TREE_TYPE (ptr))
5242 && INDIRECT_TYPE_P (TREE_TYPE (TREE_TYPE (ptr))))
5243 {
5244 tree c3 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
5245 OMP_CLAUSE_MAP);
5246 OMP_CLAUSE_SET_MAP_KIND (c3, OMP_CLAUSE_MAP_KIND (c2));
5247 OMP_CLAUSE_DECL (c3) = ptr;
5248 if (OMP_CLAUSE_MAP_KIND (c2) == GOMP_MAP_ALWAYS_POINTER)
5249 OMP_CLAUSE_DECL (c2) = build_simple_mem_ref (ptr);
5250 else
5251 OMP_CLAUSE_DECL (c2) = convert_from_reference (ptr);
5252 OMP_CLAUSE_SIZE (c3) = size_zero_node;
5253 OMP_CLAUSE_CHAIN (c3) = OMP_CLAUSE_CHAIN (c2);
5254 OMP_CLAUSE_CHAIN (c2) = c3;
5255 }
5256 }
5257 }
5258 return false;
5259 }
5260
5261 /* Return identifier to look up for omp declare reduction. */
5262
5263 tree
5264 omp_reduction_id (enum tree_code reduction_code, tree reduction_id, tree type)
5265 {
5266 const char *p = NULL;
5267 const char *m = NULL;
5268 switch (reduction_code)
5269 {
5270 case PLUS_EXPR:
5271 case MULT_EXPR:
5272 case MINUS_EXPR:
5273 case BIT_AND_EXPR:
5274 case BIT_XOR_EXPR:
5275 case BIT_IOR_EXPR:
5276 case TRUTH_ANDIF_EXPR:
5277 case TRUTH_ORIF_EXPR:
5278 reduction_id = ovl_op_identifier (false, reduction_code);
5279 break;
5280 case MIN_EXPR:
5281 p = "min";
5282 break;
5283 case MAX_EXPR:
5284 p = "max";
5285 break;
5286 default:
5287 break;
5288 }
5289
5290 if (p == NULL)
5291 {
5292 if (TREE_CODE (reduction_id) != IDENTIFIER_NODE)
5293 return error_mark_node;
5294 p = IDENTIFIER_POINTER (reduction_id);
5295 }
5296
5297 if (type != NULL_TREE)
5298 m = mangle_type_string (TYPE_MAIN_VARIANT (type));
5299
5300 const char prefix[] = "omp declare reduction ";
5301 size_t lenp = sizeof (prefix);
5302 if (strncmp (p, prefix, lenp - 1) == 0)
5303 lenp = 1;
5304 size_t len = strlen (p);
5305 size_t lenm = m ? strlen (m) + 1 : 0;
5306 char *name = XALLOCAVEC (char, lenp + len + lenm);
5307 if (lenp > 1)
5308 memcpy (name, prefix, lenp - 1);
5309 memcpy (name + lenp - 1, p, len + 1);
5310 if (m)
5311 {
5312 name[lenp + len - 1] = '~';
5313 memcpy (name + lenp + len, m, lenm);
5314 }
5315 return get_identifier (name);
5316 }
5317
5318 /* Lookup OpenMP UDR ID for TYPE, return the corresponding artificial
5319 FUNCTION_DECL or NULL_TREE if not found. */
5320
5321 static tree
5322 omp_reduction_lookup (location_t loc, tree id, tree type, tree *baselinkp,
5323 vec<tree> *ambiguousp)
5324 {
5325 tree orig_id = id;
5326 tree baselink = NULL_TREE;
5327 if (identifier_p (id))
5328 {
5329 cp_id_kind idk;
5330 bool nonint_cst_expression_p;
5331 const char *error_msg;
5332 id = omp_reduction_id (ERROR_MARK, id, type);
5333 tree decl = lookup_name (id);
5334 if (decl == NULL_TREE)
5335 decl = error_mark_node;
5336 id = finish_id_expression (id, decl, NULL_TREE, &idk, false, true,
5337 &nonint_cst_expression_p, false, true, false,
5338 false, &error_msg, loc);
5339 if (idk == CP_ID_KIND_UNQUALIFIED
5340 && identifier_p (id))
5341 {
5342 vec<tree, va_gc> *args = NULL;
5343 vec_safe_push (args, build_reference_type (type));
5344 id = perform_koenig_lookup (id, args, tf_none);
5345 }
5346 }
5347 else if (TREE_CODE (id) == SCOPE_REF)
5348 id = lookup_qualified_name (TREE_OPERAND (id, 0),
5349 omp_reduction_id (ERROR_MARK,
5350 TREE_OPERAND (id, 1),
5351 type),
5352 false, false);
5353 tree fns = id;
5354 id = NULL_TREE;
5355 if (fns && is_overloaded_fn (fns))
5356 {
5357 for (lkp_iterator iter (get_fns (fns)); iter; ++iter)
5358 {
5359 tree fndecl = *iter;
5360 if (TREE_CODE (fndecl) == FUNCTION_DECL)
5361 {
5362 tree argtype = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
5363 if (same_type_p (TREE_TYPE (argtype), type))
5364 {
5365 id = fndecl;
5366 break;
5367 }
5368 }
5369 }
5370
5371 if (id && BASELINK_P (fns))
5372 {
5373 if (baselinkp)
5374 *baselinkp = fns;
5375 else
5376 baselink = fns;
5377 }
5378 }
5379
5380 if (!id && CLASS_TYPE_P (type) && TYPE_BINFO (type))
5381 {
5382 vec<tree> ambiguous = vNULL;
5383 tree binfo = TYPE_BINFO (type), base_binfo, ret = NULL_TREE;
5384 unsigned int ix;
5385 if (ambiguousp == NULL)
5386 ambiguousp = &ambiguous;
5387 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
5388 {
5389 id = omp_reduction_lookup (loc, orig_id, BINFO_TYPE (base_binfo),
5390 baselinkp ? baselinkp : &baselink,
5391 ambiguousp);
5392 if (id == NULL_TREE)
5393 continue;
5394 if (!ambiguousp->is_empty ())
5395 ambiguousp->safe_push (id);
5396 else if (ret != NULL_TREE)
5397 {
5398 ambiguousp->safe_push (ret);
5399 ambiguousp->safe_push (id);
5400 ret = NULL_TREE;
5401 }
5402 else
5403 ret = id;
5404 }
5405 if (ambiguousp != &ambiguous)
5406 return ret;
5407 if (!ambiguous.is_empty ())
5408 {
5409 const char *str = _("candidates are:");
5410 unsigned int idx;
5411 tree udr;
5412 error_at (loc, "user defined reduction lookup is ambiguous");
5413 FOR_EACH_VEC_ELT (ambiguous, idx, udr)
5414 {
5415 inform (DECL_SOURCE_LOCATION (udr), "%s %#qD", str, udr);
5416 if (idx == 0)
5417 str = get_spaces (str);
5418 }
5419 ambiguous.release ();
5420 ret = error_mark_node;
5421 baselink = NULL_TREE;
5422 }
5423 id = ret;
5424 }
5425 if (id && baselink)
5426 perform_or_defer_access_check (BASELINK_BINFO (baselink),
5427 id, id, tf_warning_or_error);
5428 return id;
5429 }
5430
5431 /* Helper function for cp_parser_omp_declare_reduction_exprs
5432 and tsubst_omp_udr.
5433 Remove CLEANUP_STMT for data (omp_priv variable).
5434 Also append INIT_EXPR for DECL_INITIAL of omp_priv after its
5435 DECL_EXPR. */
5436
5437 tree
5438 cp_remove_omp_priv_cleanup_stmt (tree *tp, int *walk_subtrees, void *data)
5439 {
5440 if (TYPE_P (*tp))
5441 *walk_subtrees = 0;
5442 else if (TREE_CODE (*tp) == CLEANUP_STMT && CLEANUP_DECL (*tp) == (tree) data)
5443 *tp = CLEANUP_BODY (*tp);
5444 else if (TREE_CODE (*tp) == DECL_EXPR)
5445 {
5446 tree decl = DECL_EXPR_DECL (*tp);
5447 if (!processing_template_decl
5448 && decl == (tree) data
5449 && DECL_INITIAL (decl)
5450 && DECL_INITIAL (decl) != error_mark_node)
5451 {
5452 tree list = NULL_TREE;
5453 append_to_statement_list_force (*tp, &list);
5454 tree init_expr = build2 (INIT_EXPR, void_type_node,
5455 decl, DECL_INITIAL (decl));
5456 DECL_INITIAL (decl) = NULL_TREE;
5457 append_to_statement_list_force (init_expr, &list);
5458 *tp = list;
5459 }
5460 }
5461 return NULL_TREE;
5462 }
5463
5464 /* Data passed from cp_check_omp_declare_reduction to
5465 cp_check_omp_declare_reduction_r. */
5466
5467 struct cp_check_omp_declare_reduction_data
5468 {
5469 location_t loc;
5470 tree stmts[7];
5471 bool combiner_p;
5472 };
5473
5474 /* Helper function for cp_check_omp_declare_reduction, called via
5475 cp_walk_tree. */
5476
5477 static tree
5478 cp_check_omp_declare_reduction_r (tree *tp, int *, void *data)
5479 {
5480 struct cp_check_omp_declare_reduction_data *udr_data
5481 = (struct cp_check_omp_declare_reduction_data *) data;
5482 if (SSA_VAR_P (*tp)
5483 && !DECL_ARTIFICIAL (*tp)
5484 && *tp != DECL_EXPR_DECL (udr_data->stmts[udr_data->combiner_p ? 0 : 3])
5485 && *tp != DECL_EXPR_DECL (udr_data->stmts[udr_data->combiner_p ? 1 : 4]))
5486 {
5487 location_t loc = udr_data->loc;
5488 if (udr_data->combiner_p)
5489 error_at (loc, "%<#pragma omp declare reduction%> combiner refers to "
5490 "variable %qD which is not %<omp_out%> nor %<omp_in%>",
5491 *tp);
5492 else
5493 error_at (loc, "%<#pragma omp declare reduction%> initializer refers "
5494 "to variable %qD which is not %<omp_priv%> nor "
5495 "%<omp_orig%>",
5496 *tp);
5497 return *tp;
5498 }
5499 return NULL_TREE;
5500 }
5501
5502 /* Diagnose violation of OpenMP #pragma omp declare reduction restrictions. */
5503
5504 void
5505 cp_check_omp_declare_reduction (tree udr)
5506 {
5507 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (udr)));
5508 gcc_assert (TYPE_REF_P (type));
5509 type = TREE_TYPE (type);
5510 int i;
5511 location_t loc = DECL_SOURCE_LOCATION (udr);
5512
5513 if (type == error_mark_node)
5514 return;
5515 if (ARITHMETIC_TYPE_P (type))
5516 {
5517 static enum tree_code predef_codes[]
5518 = { PLUS_EXPR, MULT_EXPR, MINUS_EXPR, BIT_AND_EXPR, BIT_XOR_EXPR,
5519 BIT_IOR_EXPR, TRUTH_ANDIF_EXPR, TRUTH_ORIF_EXPR };
5520 for (i = 0; i < 8; i++)
5521 {
5522 tree id = omp_reduction_id (predef_codes[i], NULL_TREE, NULL_TREE);
5523 const char *n1 = IDENTIFIER_POINTER (DECL_NAME (udr));
5524 const char *n2 = IDENTIFIER_POINTER (id);
5525 if (strncmp (n1, n2, IDENTIFIER_LENGTH (id)) == 0
5526 && (n1[IDENTIFIER_LENGTH (id)] == '~'
5527 || n1[IDENTIFIER_LENGTH (id)] == '\0'))
5528 break;
5529 }
5530
5531 if (i == 8
5532 && TREE_CODE (type) != COMPLEX_EXPR)
5533 {
5534 const char prefix_minmax[] = "omp declare reduction m";
5535 size_t prefix_size = sizeof (prefix_minmax) - 1;
5536 const char *n = IDENTIFIER_POINTER (DECL_NAME (udr));
5537 if (strncmp (IDENTIFIER_POINTER (DECL_NAME (udr)),
5538 prefix_minmax, prefix_size) == 0
5539 && ((n[prefix_size] == 'i' && n[prefix_size + 1] == 'n')
5540 || (n[prefix_size] == 'a' && n[prefix_size + 1] == 'x'))
5541 && (n[prefix_size + 2] == '~' || n[prefix_size + 2] == '\0'))
5542 i = 0;
5543 }
5544 if (i < 8)
5545 {
5546 error_at (loc, "predeclared arithmetic type %qT in "
5547 "%<#pragma omp declare reduction%>", type);
5548 return;
5549 }
5550 }
5551 else if (TREE_CODE (type) == FUNCTION_TYPE
5552 || TREE_CODE (type) == METHOD_TYPE
5553 || TREE_CODE (type) == ARRAY_TYPE)
5554 {
5555 error_at (loc, "function or array type %qT in "
5556 "%<#pragma omp declare reduction%>", type);
5557 return;
5558 }
5559 else if (TYPE_REF_P (type))
5560 {
5561 error_at (loc, "reference type %qT in %<#pragma omp declare reduction%>",
5562 type);
5563 return;
5564 }
5565 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
5566 {
5567 error_at (loc, "const, volatile or __restrict qualified type %qT in "
5568 "%<#pragma omp declare reduction%>", type);
5569 return;
5570 }
5571
5572 tree body = DECL_SAVED_TREE (udr);
5573 if (body == NULL_TREE || TREE_CODE (body) != STATEMENT_LIST)
5574 return;
5575
5576 tree_stmt_iterator tsi;
5577 struct cp_check_omp_declare_reduction_data data;
5578 memset (data.stmts, 0, sizeof data.stmts);
5579 for (i = 0, tsi = tsi_start (body);
5580 i < 7 && !tsi_end_p (tsi);
5581 i++, tsi_next (&tsi))
5582 data.stmts[i] = tsi_stmt (tsi);
5583 data.loc = loc;
5584 gcc_assert (tsi_end_p (tsi));
5585 if (i >= 3)
5586 {
5587 gcc_assert (TREE_CODE (data.stmts[0]) == DECL_EXPR
5588 && TREE_CODE (data.stmts[1]) == DECL_EXPR);
5589 if (TREE_NO_WARNING (DECL_EXPR_DECL (data.stmts[0])))
5590 return;
5591 data.combiner_p = true;
5592 if (cp_walk_tree (&data.stmts[2], cp_check_omp_declare_reduction_r,
5593 &data, NULL))
5594 TREE_NO_WARNING (DECL_EXPR_DECL (data.stmts[0])) = 1;
5595 }
5596 if (i >= 6)
5597 {
5598 gcc_assert (TREE_CODE (data.stmts[3]) == DECL_EXPR
5599 && TREE_CODE (data.stmts[4]) == DECL_EXPR);
5600 data.combiner_p = false;
5601 if (cp_walk_tree (&data.stmts[5], cp_check_omp_declare_reduction_r,
5602 &data, NULL)
5603 || cp_walk_tree (&DECL_INITIAL (DECL_EXPR_DECL (data.stmts[3])),
5604 cp_check_omp_declare_reduction_r, &data, NULL))
5605 TREE_NO_WARNING (DECL_EXPR_DECL (data.stmts[0])) = 1;
5606 if (i == 7)
5607 gcc_assert (TREE_CODE (data.stmts[6]) == DECL_EXPR);
5608 }
5609 }
5610
5611 /* Helper function of finish_omp_clauses. Clone STMT as if we were making
5612 an inline call. But, remap
5613 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
5614 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
5615
5616 static tree
5617 clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
5618 tree decl, tree placeholder)
5619 {
5620 copy_body_data id;
5621 hash_map<tree, tree> decl_map;
5622
5623 decl_map.put (omp_decl1, placeholder);
5624 decl_map.put (omp_decl2, decl);
5625 memset (&id, 0, sizeof (id));
5626 id.src_fn = DECL_CONTEXT (omp_decl1);
5627 id.dst_fn = current_function_decl;
5628 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
5629 id.decl_map = &decl_map;
5630
5631 id.copy_decl = copy_decl_no_change;
5632 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
5633 id.transform_new_cfg = true;
5634 id.transform_return_to_modify = false;
5635 id.transform_lang_insert_block = NULL;
5636 id.eh_lp_nr = 0;
5637 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
5638 return stmt;
5639 }
5640
5641 /* Helper function of finish_omp_clauses, called via cp_walk_tree.
5642 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
5643
5644 static tree
5645 find_omp_placeholder_r (tree *tp, int *, void *data)
5646 {
5647 if (*tp == (tree) data)
5648 return *tp;
5649 return NULL_TREE;
5650 }
5651
5652 /* Helper function of finish_omp_clauses. Handle OMP_CLAUSE_REDUCTION C.
5653 Return true if there is some error and the clause should be removed. */
5654
5655 static bool
5656 finish_omp_reduction_clause (tree c, bool *need_default_ctor, bool *need_dtor)
5657 {
5658 tree t = OMP_CLAUSE_DECL (c);
5659 bool predefined = false;
5660 if (TREE_CODE (t) == TREE_LIST)
5661 {
5662 gcc_assert (processing_template_decl);
5663 return false;
5664 }
5665 tree type = TREE_TYPE (t);
5666 if (TREE_CODE (t) == MEM_REF)
5667 type = TREE_TYPE (type);
5668 if (TYPE_REF_P (type))
5669 type = TREE_TYPE (type);
5670 if (TREE_CODE (type) == ARRAY_TYPE)
5671 {
5672 tree oatype = type;
5673 gcc_assert (TREE_CODE (t) != MEM_REF);
5674 while (TREE_CODE (type) == ARRAY_TYPE)
5675 type = TREE_TYPE (type);
5676 if (!processing_template_decl)
5677 {
5678 t = require_complete_type (t);
5679 if (t == error_mark_node)
5680 return true;
5681 tree size = size_binop (EXACT_DIV_EXPR, TYPE_SIZE_UNIT (oatype),
5682 TYPE_SIZE_UNIT (type));
5683 if (integer_zerop (size))
5684 {
5685 error_at (OMP_CLAUSE_LOCATION (c),
5686 "%qE in %<reduction%> clause is a zero size array",
5687 omp_clause_printable_decl (t));
5688 return true;
5689 }
5690 size = size_binop (MINUS_EXPR, size, size_one_node);
5691 size = save_expr (size);
5692 tree index_type = build_index_type (size);
5693 tree atype = build_array_type (type, index_type);
5694 tree ptype = build_pointer_type (type);
5695 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
5696 t = build_fold_addr_expr (t);
5697 t = build2 (MEM_REF, atype, t, build_int_cst (ptype, 0));
5698 OMP_CLAUSE_DECL (c) = t;
5699 }
5700 }
5701 if (type == error_mark_node)
5702 return true;
5703 else if (ARITHMETIC_TYPE_P (type))
5704 switch (OMP_CLAUSE_REDUCTION_CODE (c))
5705 {
5706 case PLUS_EXPR:
5707 case MULT_EXPR:
5708 case MINUS_EXPR:
5709 predefined = true;
5710 break;
5711 case MIN_EXPR:
5712 case MAX_EXPR:
5713 if (TREE_CODE (type) == COMPLEX_TYPE)
5714 break;
5715 predefined = true;
5716 break;
5717 case BIT_AND_EXPR:
5718 case BIT_IOR_EXPR:
5719 case BIT_XOR_EXPR:
5720 if (FLOAT_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
5721 break;
5722 predefined = true;
5723 break;
5724 case TRUTH_ANDIF_EXPR:
5725 case TRUTH_ORIF_EXPR:
5726 if (FLOAT_TYPE_P (type))
5727 break;
5728 predefined = true;
5729 break;
5730 default:
5731 break;
5732 }
5733 else if (TYPE_READONLY (type))
5734 {
5735 error_at (OMP_CLAUSE_LOCATION (c),
5736 "%qE has const type for %<reduction%>",
5737 omp_clause_printable_decl (t));
5738 return true;
5739 }
5740 else if (!processing_template_decl)
5741 {
5742 t = require_complete_type (t);
5743 if (t == error_mark_node)
5744 return true;
5745 OMP_CLAUSE_DECL (c) = t;
5746 }
5747
5748 if (predefined)
5749 {
5750 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = NULL_TREE;
5751 return false;
5752 }
5753 else if (processing_template_decl)
5754 {
5755 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == error_mark_node)
5756 return true;
5757 return false;
5758 }
5759
5760 tree id = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
5761
5762 type = TYPE_MAIN_VARIANT (type);
5763 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = NULL_TREE;
5764 if (id == NULL_TREE)
5765 id = omp_reduction_id (OMP_CLAUSE_REDUCTION_CODE (c),
5766 NULL_TREE, NULL_TREE);
5767 id = omp_reduction_lookup (OMP_CLAUSE_LOCATION (c), id, type, NULL, NULL);
5768 if (id)
5769 {
5770 if (id == error_mark_node)
5771 return true;
5772 mark_used (id);
5773 tree body = DECL_SAVED_TREE (id);
5774 if (!body)
5775 return true;
5776 if (TREE_CODE (body) == STATEMENT_LIST)
5777 {
5778 tree_stmt_iterator tsi;
5779 tree placeholder = NULL_TREE, decl_placeholder = NULL_TREE;
5780 int i;
5781 tree stmts[7];
5782 tree atype = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (id)));
5783 atype = TREE_TYPE (atype);
5784 bool need_static_cast = !same_type_p (type, atype);
5785 memset (stmts, 0, sizeof stmts);
5786 for (i = 0, tsi = tsi_start (body);
5787 i < 7 && !tsi_end_p (tsi);
5788 i++, tsi_next (&tsi))
5789 stmts[i] = tsi_stmt (tsi);
5790 gcc_assert (tsi_end_p (tsi));
5791
5792 if (i >= 3)
5793 {
5794 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
5795 && TREE_CODE (stmts[1]) == DECL_EXPR);
5796 placeholder = build_lang_decl (VAR_DECL, NULL_TREE, type);
5797 DECL_ARTIFICIAL (placeholder) = 1;
5798 DECL_IGNORED_P (placeholder) = 1;
5799 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
5800 if (TREE_CODE (t) == MEM_REF)
5801 {
5802 decl_placeholder = build_lang_decl (VAR_DECL, NULL_TREE,
5803 type);
5804 DECL_ARTIFICIAL (decl_placeholder) = 1;
5805 DECL_IGNORED_P (decl_placeholder) = 1;
5806 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c) = decl_placeholder;
5807 }
5808 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[0])))
5809 cxx_mark_addressable (placeholder);
5810 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[1]))
5811 && (decl_placeholder
5812 || !TYPE_REF_P (TREE_TYPE (OMP_CLAUSE_DECL (c)))))
5813 cxx_mark_addressable (decl_placeholder ? decl_placeholder
5814 : OMP_CLAUSE_DECL (c));
5815 tree omp_out = placeholder;
5816 tree omp_in = decl_placeholder ? decl_placeholder
5817 : convert_from_reference (OMP_CLAUSE_DECL (c));
5818 if (need_static_cast)
5819 {
5820 tree rtype = build_reference_type (atype);
5821 omp_out = build_static_cast (rtype, omp_out,
5822 tf_warning_or_error);
5823 omp_in = build_static_cast (rtype, omp_in,
5824 tf_warning_or_error);
5825 if (omp_out == error_mark_node || omp_in == error_mark_node)
5826 return true;
5827 omp_out = convert_from_reference (omp_out);
5828 omp_in = convert_from_reference (omp_in);
5829 }
5830 OMP_CLAUSE_REDUCTION_MERGE (c)
5831 = clone_omp_udr (stmts[2], DECL_EXPR_DECL (stmts[0]),
5832 DECL_EXPR_DECL (stmts[1]), omp_in, omp_out);
5833 }
5834 if (i >= 6)
5835 {
5836 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
5837 && TREE_CODE (stmts[4]) == DECL_EXPR);
5838 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[3]))
5839 && (decl_placeholder
5840 || !TYPE_REF_P (TREE_TYPE (OMP_CLAUSE_DECL (c)))))
5841 cxx_mark_addressable (decl_placeholder ? decl_placeholder
5842 : OMP_CLAUSE_DECL (c));
5843 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[4])))
5844 cxx_mark_addressable (placeholder);
5845 tree omp_priv = decl_placeholder ? decl_placeholder
5846 : convert_from_reference (OMP_CLAUSE_DECL (c));
5847 tree omp_orig = placeholder;
5848 if (need_static_cast)
5849 {
5850 if (i == 7)
5851 {
5852 error_at (OMP_CLAUSE_LOCATION (c),
5853 "user defined reduction with constructor "
5854 "initializer for base class %qT", atype);
5855 return true;
5856 }
5857 tree rtype = build_reference_type (atype);
5858 omp_priv = build_static_cast (rtype, omp_priv,
5859 tf_warning_or_error);
5860 omp_orig = build_static_cast (rtype, omp_orig,
5861 tf_warning_or_error);
5862 if (omp_priv == error_mark_node
5863 || omp_orig == error_mark_node)
5864 return true;
5865 omp_priv = convert_from_reference (omp_priv);
5866 omp_orig = convert_from_reference (omp_orig);
5867 }
5868 if (i == 6)
5869 *need_default_ctor = true;
5870 OMP_CLAUSE_REDUCTION_INIT (c)
5871 = clone_omp_udr (stmts[5], DECL_EXPR_DECL (stmts[4]),
5872 DECL_EXPR_DECL (stmts[3]),
5873 omp_priv, omp_orig);
5874 if (cp_walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
5875 find_omp_placeholder_r, placeholder, NULL))
5876 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
5877 }
5878 else if (i >= 3)
5879 {
5880 if (CLASS_TYPE_P (type) && !pod_type_p (type))
5881 *need_default_ctor = true;
5882 else
5883 {
5884 tree init;
5885 tree v = decl_placeholder ? decl_placeholder
5886 : convert_from_reference (t);
5887 if (AGGREGATE_TYPE_P (TREE_TYPE (v)))
5888 init = build_constructor (TREE_TYPE (v), NULL);
5889 else
5890 init = fold_convert (TREE_TYPE (v), integer_zero_node);
5891 OMP_CLAUSE_REDUCTION_INIT (c)
5892 = build2 (INIT_EXPR, TREE_TYPE (v), v, init);
5893 }
5894 }
5895 }
5896 }
5897 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
5898 *need_dtor = true;
5899 else
5900 {
5901 error_at (OMP_CLAUSE_LOCATION (c),
5902 "user defined reduction not found for %qE",
5903 omp_clause_printable_decl (t));
5904 return true;
5905 }
5906 if (TREE_CODE (OMP_CLAUSE_DECL (c)) == MEM_REF)
5907 gcc_assert (TYPE_SIZE_UNIT (type)
5908 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST);
5909 return false;
5910 }
5911
5912 /* Called from finish_struct_1. linear(this) or linear(this:step)
5913 clauses might not be finalized yet because the class has been incomplete
5914 when parsing #pragma omp declare simd methods. Fix those up now. */
5915
5916 void
5917 finish_omp_declare_simd_methods (tree t)
5918 {
5919 if (processing_template_decl)
5920 return;
5921
5922 for (tree x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
5923 {
5924 if (TREE_CODE (x) == USING_DECL
5925 || !DECL_NONSTATIC_MEMBER_FUNCTION_P (x))
5926 continue;
5927 tree ods = lookup_attribute ("omp declare simd", DECL_ATTRIBUTES (x));
5928 if (!ods || !TREE_VALUE (ods))
5929 continue;
5930 for (tree c = TREE_VALUE (TREE_VALUE (ods)); c; c = OMP_CLAUSE_CHAIN (c))
5931 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
5932 && integer_zerop (OMP_CLAUSE_DECL (c))
5933 && OMP_CLAUSE_LINEAR_STEP (c)
5934 && TYPE_PTR_P (TREE_TYPE (OMP_CLAUSE_LINEAR_STEP (c))))
5935 {
5936 tree s = OMP_CLAUSE_LINEAR_STEP (c);
5937 s = fold_convert_loc (OMP_CLAUSE_LOCATION (c), sizetype, s);
5938 s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MULT_EXPR,
5939 sizetype, s, TYPE_SIZE_UNIT (t));
5940 OMP_CLAUSE_LINEAR_STEP (c) = s;
5941 }
5942 }
5943 }
5944
5945 /* Adjust sink depend clause to take into account pointer offsets.
5946
5947 Return TRUE if there was a problem processing the offset, and the
5948 whole clause should be removed. */
5949
5950 static bool
5951 cp_finish_omp_clause_depend_sink (tree sink_clause)
5952 {
5953 tree t = OMP_CLAUSE_DECL (sink_clause);
5954 gcc_assert (TREE_CODE (t) == TREE_LIST);
5955
5956 /* Make sure we don't adjust things twice for templates. */
5957 if (processing_template_decl)
5958 return false;
5959
5960 for (; t; t = TREE_CHAIN (t))
5961 {
5962 tree decl = TREE_VALUE (t);
5963 if (TYPE_PTR_P (TREE_TYPE (decl)))
5964 {
5965 tree offset = TREE_PURPOSE (t);
5966 bool neg = wi::neg_p (wi::to_wide (offset));
5967 offset = fold_unary (ABS_EXPR, TREE_TYPE (offset), offset);
5968 decl = mark_rvalue_use (decl);
5969 decl = convert_from_reference (decl);
5970 tree t2 = pointer_int_sum (OMP_CLAUSE_LOCATION (sink_clause),
5971 neg ? MINUS_EXPR : PLUS_EXPR,
5972 decl, offset);
5973 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (sink_clause),
5974 MINUS_EXPR, sizetype,
5975 fold_convert (sizetype, t2),
5976 fold_convert (sizetype, decl));
5977 if (t2 == error_mark_node)
5978 return true;
5979 TREE_PURPOSE (t) = t2;
5980 }
5981 }
5982 return false;
5983 }
5984
5985 /* Finish OpenMP iterators ITER. Return true if they are errorneous
5986 and clauses containing them should be removed. */
5987
5988 static bool
5989 cp_omp_finish_iterators (tree iter)
5990 {
5991 bool ret = false;
5992 for (tree it = iter; it; it = TREE_CHAIN (it))
5993 {
5994 tree var = TREE_VEC_ELT (it, 0);
5995 tree begin = TREE_VEC_ELT (it, 1);
5996 tree end = TREE_VEC_ELT (it, 2);
5997 tree step = TREE_VEC_ELT (it, 3);
5998 tree orig_step;
5999 tree type = TREE_TYPE (var);
6000 location_t loc = DECL_SOURCE_LOCATION (var);
6001 if (type == error_mark_node)
6002 {
6003 ret = true;
6004 continue;
6005 }
6006 if (type_dependent_expression_p (var))
6007 continue;
6008 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
6009 {
6010 error_at (loc, "iterator %qD has neither integral nor pointer type",
6011 var);
6012 ret = true;
6013 continue;
6014 }
6015 else if (TYPE_READONLY (type))
6016 {
6017 error_at (loc, "iterator %qD has const qualified type", var);
6018 ret = true;
6019 continue;
6020 }
6021 if (type_dependent_expression_p (begin)
6022 || type_dependent_expression_p (end)
6023 || type_dependent_expression_p (step))
6024 continue;
6025 else if (error_operand_p (step))
6026 {
6027 ret = true;
6028 continue;
6029 }
6030 else if (!INTEGRAL_TYPE_P (TREE_TYPE (step)))
6031 {
6032 error_at (EXPR_LOC_OR_LOC (step, loc),
6033 "iterator step with non-integral type");
6034 ret = true;
6035 continue;
6036 }
6037
6038 begin = mark_rvalue_use (begin);
6039 end = mark_rvalue_use (end);
6040 step = mark_rvalue_use (step);
6041 begin = cp_build_c_cast (type, begin, tf_warning_or_error);
6042 end = cp_build_c_cast (type, end, tf_warning_or_error);
6043 orig_step = step;
6044 if (!processing_template_decl)
6045 step = orig_step = save_expr (step);
6046 tree stype = POINTER_TYPE_P (type) ? sizetype : type;
6047 step = cp_build_c_cast (stype, step, tf_warning_or_error);
6048 if (POINTER_TYPE_P (type) && !processing_template_decl)
6049 {
6050 begin = save_expr (begin);
6051 step = pointer_int_sum (loc, PLUS_EXPR, begin, step);
6052 step = fold_build2_loc (loc, MINUS_EXPR, sizetype,
6053 fold_convert (sizetype, step),
6054 fold_convert (sizetype, begin));
6055 step = fold_convert (ssizetype, step);
6056 }
6057 if (!processing_template_decl)
6058 {
6059 begin = maybe_constant_value (begin);
6060 end = maybe_constant_value (end);
6061 step = maybe_constant_value (step);
6062 orig_step = maybe_constant_value (orig_step);
6063 }
6064 if (integer_zerop (step))
6065 {
6066 error_at (loc, "iterator %qD has zero step", var);
6067 ret = true;
6068 continue;
6069 }
6070
6071 if (begin == error_mark_node
6072 || end == error_mark_node
6073 || step == error_mark_node
6074 || orig_step == error_mark_node)
6075 {
6076 ret = true;
6077 continue;
6078 }
6079
6080 if (!processing_template_decl)
6081 {
6082 begin = fold_build_cleanup_point_expr (TREE_TYPE (begin), begin);
6083 end = fold_build_cleanup_point_expr (TREE_TYPE (end), end);
6084 step = fold_build_cleanup_point_expr (TREE_TYPE (step), step);
6085 orig_step = fold_build_cleanup_point_expr (TREE_TYPE (orig_step),
6086 orig_step);
6087 }
6088 hash_set<tree> pset;
6089 tree it2;
6090 for (it2 = TREE_CHAIN (it); it2; it2 = TREE_CHAIN (it2))
6091 {
6092 tree var2 = TREE_VEC_ELT (it2, 0);
6093 tree begin2 = TREE_VEC_ELT (it2, 1);
6094 tree end2 = TREE_VEC_ELT (it2, 2);
6095 tree step2 = TREE_VEC_ELT (it2, 3);
6096 location_t loc2 = DECL_SOURCE_LOCATION (var2);
6097 if (cp_walk_tree (&begin2, find_omp_placeholder_r, var, &pset))
6098 {
6099 error_at (EXPR_LOC_OR_LOC (begin2, loc2),
6100 "begin expression refers to outer iterator %qD", var);
6101 break;
6102 }
6103 else if (cp_walk_tree (&end2, find_omp_placeholder_r, var, &pset))
6104 {
6105 error_at (EXPR_LOC_OR_LOC (end2, loc2),
6106 "end expression refers to outer iterator %qD", var);
6107 break;
6108 }
6109 else if (cp_walk_tree (&step2, find_omp_placeholder_r, var, &pset))
6110 {
6111 error_at (EXPR_LOC_OR_LOC (step2, loc2),
6112 "step expression refers to outer iterator %qD", var);
6113 break;
6114 }
6115 }
6116 if (it2)
6117 {
6118 ret = true;
6119 continue;
6120 }
6121 TREE_VEC_ELT (it, 1) = begin;
6122 TREE_VEC_ELT (it, 2) = end;
6123 if (processing_template_decl)
6124 TREE_VEC_ELT (it, 3) = orig_step;
6125 else
6126 {
6127 TREE_VEC_ELT (it, 3) = step;
6128 TREE_VEC_ELT (it, 4) = orig_step;
6129 }
6130 }
6131 return ret;
6132 }
6133
6134 /* For all elements of CLAUSES, validate them vs OpenMP constraints.
6135 Remove any elements from the list that are invalid. */
6136
6137 tree
6138 finish_omp_clauses (tree clauses, enum c_omp_region_type ort)
6139 {
6140 bitmap_head generic_head, firstprivate_head, lastprivate_head;
6141 bitmap_head aligned_head, map_head, map_field_head, oacc_reduction_head;
6142 tree c, t, *pc;
6143 tree safelen = NULL_TREE;
6144 bool branch_seen = false;
6145 bool copyprivate_seen = false;
6146 bool ordered_seen = false;
6147 bool oacc_async = false;
6148 tree last_iterators = NULL_TREE;
6149 bool last_iterators_remove = false;
6150 bool reduction_seen = false;
6151
6152 bitmap_obstack_initialize (NULL);
6153 bitmap_initialize (&generic_head, &bitmap_default_obstack);
6154 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
6155 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
6156 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
6157 /* If ort == C_ORT_OMP_DECLARE_SIMD used as uniform_head instead. */
6158 bitmap_initialize (&map_head, &bitmap_default_obstack);
6159 bitmap_initialize (&map_field_head, &bitmap_default_obstack);
6160 /* If ort == C_ORT_OMP used as nontemporal_head instead. */
6161 bitmap_initialize (&oacc_reduction_head, &bitmap_default_obstack);
6162
6163 if (ort & C_ORT_ACC)
6164 for (c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
6165 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ASYNC)
6166 {
6167 oacc_async = true;
6168 break;
6169 }
6170
6171 for (pc = &clauses, c = clauses; c ; c = *pc)
6172 {
6173 bool remove = false;
6174 bool field_ok = false;
6175
6176 switch (OMP_CLAUSE_CODE (c))
6177 {
6178 case OMP_CLAUSE_SHARED:
6179 field_ok = ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP);
6180 goto check_dup_generic;
6181 case OMP_CLAUSE_PRIVATE:
6182 field_ok = ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP);
6183 goto check_dup_generic;
6184 case OMP_CLAUSE_REDUCTION:
6185 reduction_seen = true;
6186 /* FALLTHRU */
6187 case OMP_CLAUSE_IN_REDUCTION:
6188 case OMP_CLAUSE_TASK_REDUCTION:
6189 field_ok = ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP);
6190 t = OMP_CLAUSE_DECL (c);
6191 if (TREE_CODE (t) == TREE_LIST)
6192 {
6193 if (handle_omp_array_sections (c, ort))
6194 {
6195 remove = true;
6196 break;
6197 }
6198 if (TREE_CODE (t) == TREE_LIST)
6199 {
6200 while (TREE_CODE (t) == TREE_LIST)
6201 t = TREE_CHAIN (t);
6202 }
6203 else
6204 {
6205 gcc_assert (TREE_CODE (t) == MEM_REF);
6206 t = TREE_OPERAND (t, 0);
6207 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
6208 t = TREE_OPERAND (t, 0);
6209 if (TREE_CODE (t) == ADDR_EXPR
6210 || INDIRECT_REF_P (t))
6211 t = TREE_OPERAND (t, 0);
6212 }
6213 tree n = omp_clause_decl_field (t);
6214 if (n)
6215 t = n;
6216 goto check_dup_generic_t;
6217 }
6218 if (oacc_async)
6219 cxx_mark_addressable (t);
6220 goto check_dup_generic;
6221 case OMP_CLAUSE_COPYPRIVATE:
6222 copyprivate_seen = true;
6223 field_ok = ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP);
6224 goto check_dup_generic;
6225 case OMP_CLAUSE_COPYIN:
6226 goto check_dup_generic;
6227 case OMP_CLAUSE_LINEAR:
6228 field_ok = ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP);
6229 t = OMP_CLAUSE_DECL (c);
6230 if (ort != C_ORT_OMP_DECLARE_SIMD
6231 && OMP_CLAUSE_LINEAR_KIND (c) != OMP_CLAUSE_LINEAR_DEFAULT)
6232 {
6233 error_at (OMP_CLAUSE_LOCATION (c),
6234 "modifier should not be specified in %<linear%> "
6235 "clause on %<simd%> or %<for%> constructs");
6236 OMP_CLAUSE_LINEAR_KIND (c) = OMP_CLAUSE_LINEAR_DEFAULT;
6237 }
6238 if ((VAR_P (t) || TREE_CODE (t) == PARM_DECL)
6239 && !type_dependent_expression_p (t))
6240 {
6241 tree type = TREE_TYPE (t);
6242 if ((OMP_CLAUSE_LINEAR_KIND (c) == OMP_CLAUSE_LINEAR_REF
6243 || OMP_CLAUSE_LINEAR_KIND (c) == OMP_CLAUSE_LINEAR_UVAL)
6244 && !TYPE_REF_P (type))
6245 {
6246 error_at (OMP_CLAUSE_LOCATION (c),
6247 "linear clause with %qs modifier applied to "
6248 "non-reference variable with %qT type",
6249 OMP_CLAUSE_LINEAR_KIND (c) == OMP_CLAUSE_LINEAR_REF
6250 ? "ref" : "uval", TREE_TYPE (t));
6251 remove = true;
6252 break;
6253 }
6254 if (TYPE_REF_P (type))
6255 type = TREE_TYPE (type);
6256 if (OMP_CLAUSE_LINEAR_KIND (c) != OMP_CLAUSE_LINEAR_REF)
6257 {
6258 if (!INTEGRAL_TYPE_P (type)
6259 && !TYPE_PTR_P (type))
6260 {
6261 error_at (OMP_CLAUSE_LOCATION (c),
6262 "linear clause applied to non-integral "
6263 "non-pointer variable with %qT type",
6264 TREE_TYPE (t));
6265 remove = true;
6266 break;
6267 }
6268 }
6269 }
6270 t = OMP_CLAUSE_LINEAR_STEP (c);
6271 if (t == NULL_TREE)
6272 t = integer_one_node;
6273 if (t == error_mark_node)
6274 {
6275 remove = true;
6276 break;
6277 }
6278 else if (!type_dependent_expression_p (t)
6279 && !INTEGRAL_TYPE_P (TREE_TYPE (t))
6280 && (ort != C_ORT_OMP_DECLARE_SIMD
6281 || TREE_CODE (t) != PARM_DECL
6282 || !TYPE_REF_P (TREE_TYPE (t))
6283 || !INTEGRAL_TYPE_P (TREE_TYPE (TREE_TYPE (t)))))
6284 {
6285 error_at (OMP_CLAUSE_LOCATION (c),
6286 "linear step expression must be integral");
6287 remove = true;
6288 break;
6289 }
6290 else
6291 {
6292 t = mark_rvalue_use (t);
6293 if (ort == C_ORT_OMP_DECLARE_SIMD && TREE_CODE (t) == PARM_DECL)
6294 {
6295 OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c) = 1;
6296 goto check_dup_generic;
6297 }
6298 if (!processing_template_decl
6299 && (VAR_P (OMP_CLAUSE_DECL (c))
6300 || TREE_CODE (OMP_CLAUSE_DECL (c)) == PARM_DECL))
6301 {
6302 if (ort == C_ORT_OMP_DECLARE_SIMD)
6303 {
6304 t = maybe_constant_value (t);
6305 if (TREE_CODE (t) != INTEGER_CST)
6306 {
6307 error_at (OMP_CLAUSE_LOCATION (c),
6308 "%<linear%> clause step %qE is neither "
6309 "constant nor a parameter", t);
6310 remove = true;
6311 break;
6312 }
6313 }
6314 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6315 tree type = TREE_TYPE (OMP_CLAUSE_DECL (c));
6316 if (TYPE_REF_P (type))
6317 type = TREE_TYPE (type);
6318 if (OMP_CLAUSE_LINEAR_KIND (c) == OMP_CLAUSE_LINEAR_REF)
6319 {
6320 type = build_pointer_type (type);
6321 tree d = fold_convert (type, OMP_CLAUSE_DECL (c));
6322 t = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
6323 d, t);
6324 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c),
6325 MINUS_EXPR, sizetype,
6326 fold_convert (sizetype, t),
6327 fold_convert (sizetype, d));
6328 if (t == error_mark_node)
6329 {
6330 remove = true;
6331 break;
6332 }
6333 }
6334 else if (TYPE_PTR_P (type)
6335 /* Can't multiply the step yet if *this
6336 is still incomplete type. */
6337 && (ort != C_ORT_OMP_DECLARE_SIMD
6338 || TREE_CODE (OMP_CLAUSE_DECL (c)) != PARM_DECL
6339 || !DECL_ARTIFICIAL (OMP_CLAUSE_DECL (c))
6340 || DECL_NAME (OMP_CLAUSE_DECL (c))
6341 != this_identifier
6342 || !TYPE_BEING_DEFINED (TREE_TYPE (type))))
6343 {
6344 tree d = convert_from_reference (OMP_CLAUSE_DECL (c));
6345 t = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
6346 d, t);
6347 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c),
6348 MINUS_EXPR, sizetype,
6349 fold_convert (sizetype, t),
6350 fold_convert (sizetype, d));
6351 if (t == error_mark_node)
6352 {
6353 remove = true;
6354 break;
6355 }
6356 }
6357 else
6358 t = fold_convert (type, t);
6359 }
6360 OMP_CLAUSE_LINEAR_STEP (c) = t;
6361 }
6362 goto check_dup_generic;
6363 check_dup_generic:
6364 t = omp_clause_decl_field (OMP_CLAUSE_DECL (c));
6365 if (t)
6366 {
6367 if (!remove && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_SHARED)
6368 omp_note_field_privatization (t, OMP_CLAUSE_DECL (c));
6369 }
6370 else
6371 t = OMP_CLAUSE_DECL (c);
6372 check_dup_generic_t:
6373 if (t == current_class_ptr
6374 && (ort != C_ORT_OMP_DECLARE_SIMD
6375 || (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_LINEAR
6376 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_UNIFORM)))
6377 {
6378 error_at (OMP_CLAUSE_LOCATION (c),
6379 "%<this%> allowed in OpenMP only in %<declare simd%>"
6380 " clauses");
6381 remove = true;
6382 break;
6383 }
6384 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL
6385 && (!field_ok || TREE_CODE (t) != FIELD_DECL))
6386 {
6387 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
6388 break;
6389 if (DECL_P (t))
6390 error_at (OMP_CLAUSE_LOCATION (c),
6391 "%qD is not a variable in clause %qs", t,
6392 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6393 else
6394 error_at (OMP_CLAUSE_LOCATION (c),
6395 "%qE is not a variable in clause %qs", t,
6396 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6397 remove = true;
6398 }
6399 else if (ort == C_ORT_ACC
6400 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
6401 {
6402 if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
6403 {
6404 error_at (OMP_CLAUSE_LOCATION (c),
6405 "%qD appears more than once in reduction clauses",
6406 t);
6407 remove = true;
6408 }
6409 else
6410 bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
6411 }
6412 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
6413 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
6414 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
6415 {
6416 error_at (OMP_CLAUSE_LOCATION (c),
6417 "%qD appears more than once in data clauses", t);
6418 remove = true;
6419 }
6420 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
6421 && bitmap_bit_p (&map_head, DECL_UID (t)))
6422 {
6423 if (ort == C_ORT_ACC)
6424 error_at (OMP_CLAUSE_LOCATION (c),
6425 "%qD appears more than once in data clauses", t);
6426 else
6427 error_at (OMP_CLAUSE_LOCATION (c),
6428 "%qD appears both in data and map clauses", t);
6429 remove = true;
6430 }
6431 else
6432 bitmap_set_bit (&generic_head, DECL_UID (t));
6433 if (!field_ok)
6434 break;
6435 handle_field_decl:
6436 if (!remove
6437 && TREE_CODE (t) == FIELD_DECL
6438 && t == OMP_CLAUSE_DECL (c)
6439 && ort != C_ORT_ACC)
6440 {
6441 OMP_CLAUSE_DECL (c)
6442 = omp_privatize_field (t, (OMP_CLAUSE_CODE (c)
6443 == OMP_CLAUSE_SHARED));
6444 if (OMP_CLAUSE_DECL (c) == error_mark_node)
6445 remove = true;
6446 }
6447 break;
6448
6449 case OMP_CLAUSE_FIRSTPRIVATE:
6450 t = omp_clause_decl_field (OMP_CLAUSE_DECL (c));
6451 if (t)
6452 omp_note_field_privatization (t, OMP_CLAUSE_DECL (c));
6453 else
6454 t = OMP_CLAUSE_DECL (c);
6455 if (ort != C_ORT_ACC && t == current_class_ptr)
6456 {
6457 error_at (OMP_CLAUSE_LOCATION (c),
6458 "%<this%> allowed in OpenMP only in %<declare simd%>"
6459 " clauses");
6460 remove = true;
6461 break;
6462 }
6463 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL
6464 && ((ort & C_ORT_OMP_DECLARE_SIMD) != C_ORT_OMP
6465 || TREE_CODE (t) != FIELD_DECL))
6466 {
6467 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
6468 break;
6469 if (DECL_P (t))
6470 error_at (OMP_CLAUSE_LOCATION (c),
6471 "%qD is not a variable in clause %<firstprivate%>",
6472 t);
6473 else
6474 error_at (OMP_CLAUSE_LOCATION (c),
6475 "%qE is not a variable in clause %<firstprivate%>",
6476 t);
6477 remove = true;
6478 }
6479 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
6480 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
6481 {
6482 error_at (OMP_CLAUSE_LOCATION (c),
6483 "%qD appears more than once in data clauses", t);
6484 remove = true;
6485 }
6486 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
6487 {
6488 if (ort == C_ORT_ACC)
6489 error_at (OMP_CLAUSE_LOCATION (c),
6490 "%qD appears more than once in data clauses", t);
6491 else
6492 error_at (OMP_CLAUSE_LOCATION (c),
6493 "%qD appears both in data and map clauses", t);
6494 remove = true;
6495 }
6496 else
6497 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
6498 goto handle_field_decl;
6499
6500 case OMP_CLAUSE_LASTPRIVATE:
6501 t = omp_clause_decl_field (OMP_CLAUSE_DECL (c));
6502 if (t)
6503 omp_note_field_privatization (t, OMP_CLAUSE_DECL (c));
6504 else
6505 t = OMP_CLAUSE_DECL (c);
6506 if (t == current_class_ptr)
6507 {
6508 error_at (OMP_CLAUSE_LOCATION (c),
6509 "%<this%> allowed in OpenMP only in %<declare simd%>"
6510 " clauses");
6511 remove = true;
6512 break;
6513 }
6514 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL
6515 && ((ort & C_ORT_OMP_DECLARE_SIMD) != C_ORT_OMP
6516 || TREE_CODE (t) != FIELD_DECL))
6517 {
6518 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
6519 break;
6520 if (DECL_P (t))
6521 error_at (OMP_CLAUSE_LOCATION (c),
6522 "%qD is not a variable in clause %<lastprivate%>",
6523 t);
6524 else
6525 error_at (OMP_CLAUSE_LOCATION (c),
6526 "%qE is not a variable in clause %<lastprivate%>",
6527 t);
6528 remove = true;
6529 }
6530 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
6531 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
6532 {
6533 error_at (OMP_CLAUSE_LOCATION (c),
6534 "%qD appears more than once in data clauses", t);
6535 remove = true;
6536 }
6537 else
6538 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
6539 goto handle_field_decl;
6540
6541 case OMP_CLAUSE_IF:
6542 t = OMP_CLAUSE_IF_EXPR (c);
6543 t = maybe_convert_cond (t);
6544 if (t == error_mark_node)
6545 remove = true;
6546 else if (!processing_template_decl)
6547 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6548 OMP_CLAUSE_IF_EXPR (c) = t;
6549 break;
6550
6551 case OMP_CLAUSE_FINAL:
6552 t = OMP_CLAUSE_FINAL_EXPR (c);
6553 t = maybe_convert_cond (t);
6554 if (t == error_mark_node)
6555 remove = true;
6556 else if (!processing_template_decl)
6557 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6558 OMP_CLAUSE_FINAL_EXPR (c) = t;
6559 break;
6560
6561 case OMP_CLAUSE_GANG:
6562 /* Operand 1 is the gang static: argument. */
6563 t = OMP_CLAUSE_OPERAND (c, 1);
6564 if (t != NULL_TREE)
6565 {
6566 if (t == error_mark_node)
6567 remove = true;
6568 else if (!type_dependent_expression_p (t)
6569 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6570 {
6571 error_at (OMP_CLAUSE_LOCATION (c),
6572 "%<gang%> static expression must be integral");
6573 remove = true;
6574 }
6575 else
6576 {
6577 t = mark_rvalue_use (t);
6578 if (!processing_template_decl)
6579 {
6580 t = maybe_constant_value (t);
6581 if (TREE_CODE (t) == INTEGER_CST
6582 && tree_int_cst_sgn (t) != 1
6583 && t != integer_minus_one_node)
6584 {
6585 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6586 "%<gang%> static value must be "
6587 "positive");
6588 t = integer_one_node;
6589 }
6590 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6591 }
6592 }
6593 OMP_CLAUSE_OPERAND (c, 1) = t;
6594 }
6595 /* Check operand 0, the num argument. */
6596 /* FALLTHRU */
6597
6598 case OMP_CLAUSE_WORKER:
6599 case OMP_CLAUSE_VECTOR:
6600 if (OMP_CLAUSE_OPERAND (c, 0) == NULL_TREE)
6601 break;
6602 /* FALLTHRU */
6603
6604 case OMP_CLAUSE_NUM_TASKS:
6605 case OMP_CLAUSE_NUM_TEAMS:
6606 case OMP_CLAUSE_NUM_THREADS:
6607 case OMP_CLAUSE_NUM_GANGS:
6608 case OMP_CLAUSE_NUM_WORKERS:
6609 case OMP_CLAUSE_VECTOR_LENGTH:
6610 t = OMP_CLAUSE_OPERAND (c, 0);
6611 if (t == error_mark_node)
6612 remove = true;
6613 else if (!type_dependent_expression_p (t)
6614 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6615 {
6616 switch (OMP_CLAUSE_CODE (c))
6617 {
6618 case OMP_CLAUSE_GANG:
6619 error_at (OMP_CLAUSE_LOCATION (c),
6620 "%<gang%> num expression must be integral"); break;
6621 case OMP_CLAUSE_VECTOR:
6622 error_at (OMP_CLAUSE_LOCATION (c),
6623 "%<vector%> length expression must be integral");
6624 break;
6625 case OMP_CLAUSE_WORKER:
6626 error_at (OMP_CLAUSE_LOCATION (c),
6627 "%<worker%> num expression must be integral");
6628 break;
6629 default:
6630 error_at (OMP_CLAUSE_LOCATION (c),
6631 "%qs expression must be integral",
6632 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6633 }
6634 remove = true;
6635 }
6636 else
6637 {
6638 t = mark_rvalue_use (t);
6639 if (!processing_template_decl)
6640 {
6641 t = maybe_constant_value (t);
6642 if (TREE_CODE (t) == INTEGER_CST
6643 && tree_int_cst_sgn (t) != 1)
6644 {
6645 switch (OMP_CLAUSE_CODE (c))
6646 {
6647 case OMP_CLAUSE_GANG:
6648 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6649 "%<gang%> num value must be positive");
6650 break;
6651 case OMP_CLAUSE_VECTOR:
6652 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6653 "%<vector%> length value must be "
6654 "positive");
6655 break;
6656 case OMP_CLAUSE_WORKER:
6657 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6658 "%<worker%> num value must be "
6659 "positive");
6660 break;
6661 default:
6662 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6663 "%qs value must be positive",
6664 omp_clause_code_name
6665 [OMP_CLAUSE_CODE (c)]);
6666 }
6667 t = integer_one_node;
6668 }
6669 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6670 }
6671 OMP_CLAUSE_OPERAND (c, 0) = t;
6672 }
6673 break;
6674
6675 case OMP_CLAUSE_SCHEDULE:
6676 t = OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c);
6677 if (t == NULL)
6678 ;
6679 else if (t == error_mark_node)
6680 remove = true;
6681 else if (!type_dependent_expression_p (t)
6682 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6683 {
6684 error_at (OMP_CLAUSE_LOCATION (c),
6685 "schedule chunk size expression must be integral");
6686 remove = true;
6687 }
6688 else
6689 {
6690 t = mark_rvalue_use (t);
6691 if (!processing_template_decl)
6692 {
6693 t = maybe_constant_value (t);
6694 if (TREE_CODE (t) == INTEGER_CST
6695 && tree_int_cst_sgn (t) != 1)
6696 {
6697 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6698 "chunk size value must be positive");
6699 t = integer_one_node;
6700 }
6701 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6702 }
6703 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
6704 }
6705 break;
6706
6707 case OMP_CLAUSE_SIMDLEN:
6708 case OMP_CLAUSE_SAFELEN:
6709 t = OMP_CLAUSE_OPERAND (c, 0);
6710 if (t == error_mark_node)
6711 remove = true;
6712 else if (!type_dependent_expression_p (t)
6713 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6714 {
6715 error_at (OMP_CLAUSE_LOCATION (c),
6716 "%qs length expression must be integral",
6717 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6718 remove = true;
6719 }
6720 else
6721 {
6722 t = mark_rvalue_use (t);
6723 if (!processing_template_decl)
6724 {
6725 t = maybe_constant_value (t);
6726 if (TREE_CODE (t) != INTEGER_CST
6727 || tree_int_cst_sgn (t) != 1)
6728 {
6729 error_at (OMP_CLAUSE_LOCATION (c),
6730 "%qs length expression must be positive "
6731 "constant integer expression",
6732 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6733 remove = true;
6734 }
6735 }
6736 OMP_CLAUSE_OPERAND (c, 0) = t;
6737 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SAFELEN)
6738 safelen = c;
6739 }
6740 break;
6741
6742 case OMP_CLAUSE_ASYNC:
6743 t = OMP_CLAUSE_ASYNC_EXPR (c);
6744 if (t == error_mark_node)
6745 remove = true;
6746 else if (!type_dependent_expression_p (t)
6747 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6748 {
6749 error_at (OMP_CLAUSE_LOCATION (c),
6750 "%<async%> expression must be integral");
6751 remove = true;
6752 }
6753 else
6754 {
6755 t = mark_rvalue_use (t);
6756 if (!processing_template_decl)
6757 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6758 OMP_CLAUSE_ASYNC_EXPR (c) = t;
6759 }
6760 break;
6761
6762 case OMP_CLAUSE_WAIT:
6763 t = OMP_CLAUSE_WAIT_EXPR (c);
6764 if (t == error_mark_node)
6765 remove = true;
6766 else if (!processing_template_decl)
6767 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6768 OMP_CLAUSE_WAIT_EXPR (c) = t;
6769 break;
6770
6771 case OMP_CLAUSE_THREAD_LIMIT:
6772 t = OMP_CLAUSE_THREAD_LIMIT_EXPR (c);
6773 if (t == error_mark_node)
6774 remove = true;
6775 else if (!type_dependent_expression_p (t)
6776 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6777 {
6778 error_at (OMP_CLAUSE_LOCATION (c),
6779 "%<thread_limit%> expression must be integral");
6780 remove = true;
6781 }
6782 else
6783 {
6784 t = mark_rvalue_use (t);
6785 if (!processing_template_decl)
6786 {
6787 t = maybe_constant_value (t);
6788 if (TREE_CODE (t) == INTEGER_CST
6789 && tree_int_cst_sgn (t) != 1)
6790 {
6791 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6792 "%<thread_limit%> value must be positive");
6793 t = integer_one_node;
6794 }
6795 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6796 }
6797 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
6798 }
6799 break;
6800
6801 case OMP_CLAUSE_DEVICE:
6802 t = OMP_CLAUSE_DEVICE_ID (c);
6803 if (t == error_mark_node)
6804 remove = true;
6805 else if (!type_dependent_expression_p (t)
6806 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6807 {
6808 error_at (OMP_CLAUSE_LOCATION (c),
6809 "%<device%> id must be integral");
6810 remove = true;
6811 }
6812 else
6813 {
6814 t = mark_rvalue_use (t);
6815 if (!processing_template_decl)
6816 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6817 OMP_CLAUSE_DEVICE_ID (c) = t;
6818 }
6819 break;
6820
6821 case OMP_CLAUSE_DIST_SCHEDULE:
6822 t = OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c);
6823 if (t == NULL)
6824 ;
6825 else if (t == error_mark_node)
6826 remove = true;
6827 else if (!type_dependent_expression_p (t)
6828 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6829 {
6830 error_at (OMP_CLAUSE_LOCATION (c),
6831 "%<dist_schedule%> chunk size expression must be "
6832 "integral");
6833 remove = true;
6834 }
6835 else
6836 {
6837 t = mark_rvalue_use (t);
6838 if (!processing_template_decl)
6839 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6840 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
6841 }
6842 break;
6843
6844 case OMP_CLAUSE_ALIGNED:
6845 t = OMP_CLAUSE_DECL (c);
6846 if (t == current_class_ptr && ort != C_ORT_OMP_DECLARE_SIMD)
6847 {
6848 error_at (OMP_CLAUSE_LOCATION (c),
6849 "%<this%> allowed in OpenMP only in %<declare simd%>"
6850 " clauses");
6851 remove = true;
6852 break;
6853 }
6854 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
6855 {
6856 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
6857 break;
6858 if (DECL_P (t))
6859 error_at (OMP_CLAUSE_LOCATION (c),
6860 "%qD is not a variable in %<aligned%> clause", t);
6861 else
6862 error_at (OMP_CLAUSE_LOCATION (c),
6863 "%qE is not a variable in %<aligned%> clause", t);
6864 remove = true;
6865 }
6866 else if (!type_dependent_expression_p (t)
6867 && !TYPE_PTR_P (TREE_TYPE (t))
6868 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE
6869 && (!TYPE_REF_P (TREE_TYPE (t))
6870 || (!INDIRECT_TYPE_P (TREE_TYPE (TREE_TYPE (t)))
6871 && (TREE_CODE (TREE_TYPE (TREE_TYPE (t)))
6872 != ARRAY_TYPE))))
6873 {
6874 error_at (OMP_CLAUSE_LOCATION (c),
6875 "%qE in %<aligned%> clause is neither a pointer nor "
6876 "an array nor a reference to pointer or array", t);
6877 remove = true;
6878 }
6879 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
6880 {
6881 error_at (OMP_CLAUSE_LOCATION (c),
6882 "%qD appears more than once in %<aligned%> clauses",
6883 t);
6884 remove = true;
6885 }
6886 else
6887 bitmap_set_bit (&aligned_head, DECL_UID (t));
6888 t = OMP_CLAUSE_ALIGNED_ALIGNMENT (c);
6889 if (t == error_mark_node)
6890 remove = true;
6891 else if (t == NULL_TREE)
6892 break;
6893 else if (!type_dependent_expression_p (t)
6894 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6895 {
6896 error_at (OMP_CLAUSE_LOCATION (c),
6897 "%<aligned%> clause alignment expression must "
6898 "be integral");
6899 remove = true;
6900 }
6901 else
6902 {
6903 t = mark_rvalue_use (t);
6904 if (!processing_template_decl)
6905 {
6906 t = maybe_constant_value (t);
6907 if (TREE_CODE (t) != INTEGER_CST
6908 || tree_int_cst_sgn (t) != 1)
6909 {
6910 error_at (OMP_CLAUSE_LOCATION (c),
6911 "%<aligned%> clause alignment expression must "
6912 "be positive constant integer expression");
6913 remove = true;
6914 }
6915 else
6916 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6917 }
6918 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = t;
6919 }
6920 break;
6921
6922 case OMP_CLAUSE_NONTEMPORAL:
6923 t = OMP_CLAUSE_DECL (c);
6924 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
6925 {
6926 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
6927 break;
6928 if (DECL_P (t))
6929 error_at (OMP_CLAUSE_LOCATION (c),
6930 "%qD is not a variable in %<nontemporal%> clause",
6931 t);
6932 else
6933 error_at (OMP_CLAUSE_LOCATION (c),
6934 "%qE is not a variable in %<nontemporal%> clause",
6935 t);
6936 remove = true;
6937 }
6938 else if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
6939 {
6940 error_at (OMP_CLAUSE_LOCATION (c),
6941 "%qD appears more than once in %<nontemporal%> "
6942 "clauses", t);
6943 remove = true;
6944 }
6945 else
6946 bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
6947 break;
6948
6949 case OMP_CLAUSE_DEPEND:
6950 t = OMP_CLAUSE_DECL (c);
6951 if (t == NULL_TREE)
6952 {
6953 gcc_assert (OMP_CLAUSE_DEPEND_KIND (c)
6954 == OMP_CLAUSE_DEPEND_SOURCE);
6955 break;
6956 }
6957 if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SINK)
6958 {
6959 if (cp_finish_omp_clause_depend_sink (c))
6960 remove = true;
6961 break;
6962 }
6963 if (TREE_CODE (t) == TREE_LIST
6964 && TREE_PURPOSE (t)
6965 && TREE_CODE (TREE_PURPOSE (t)) == TREE_VEC)
6966 {
6967 if (TREE_PURPOSE (t) != last_iterators)
6968 last_iterators_remove
6969 = cp_omp_finish_iterators (TREE_PURPOSE (t));
6970 last_iterators = TREE_PURPOSE (t);
6971 t = TREE_VALUE (t);
6972 if (last_iterators_remove)
6973 t = error_mark_node;
6974 }
6975 else
6976 last_iterators = NULL_TREE;
6977
6978 if (TREE_CODE (t) == TREE_LIST)
6979 {
6980 if (handle_omp_array_sections (c, ort))
6981 remove = true;
6982 else if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_DEPOBJ)
6983 {
6984 error_at (OMP_CLAUSE_LOCATION (c),
6985 "%<depend%> clause with %<depobj%> dependence "
6986 "type on array section");
6987 remove = true;
6988 }
6989 break;
6990 }
6991 if (t == error_mark_node)
6992 remove = true;
6993 else if (t == current_class_ptr)
6994 {
6995 error_at (OMP_CLAUSE_LOCATION (c),
6996 "%<this%> allowed in OpenMP only in %<declare simd%>"
6997 " clauses");
6998 remove = true;
6999 }
7000 else if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
7001 break;
7002 else if (!lvalue_p (t))
7003 {
7004 if (DECL_P (t))
7005 error_at (OMP_CLAUSE_LOCATION (c),
7006 "%qD is not lvalue expression nor array section "
7007 "in %<depend%> clause", t);
7008 else
7009 error_at (OMP_CLAUSE_LOCATION (c),
7010 "%qE is not lvalue expression nor array section "
7011 "in %<depend%> clause", t);
7012 remove = true;
7013 }
7014 else if (TREE_CODE (t) == COMPONENT_REF
7015 && TREE_CODE (TREE_OPERAND (t, 1)) == FIELD_DECL
7016 && DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
7017 {
7018 error_at (OMP_CLAUSE_LOCATION (c),
7019 "bit-field %qE in %qs clause", t, "depend");
7020 remove = true;
7021 }
7022 else if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_DEPOBJ)
7023 {
7024 if (!c_omp_depend_t_p (TYPE_REF_P (TREE_TYPE (t))
7025 ? TREE_TYPE (TREE_TYPE (t))
7026 : TREE_TYPE (t)))
7027 {
7028 error_at (OMP_CLAUSE_LOCATION (c),
7029 "%qE does not have %<omp_depend_t%> type in "
7030 "%<depend%> clause with %<depobj%> dependence "
7031 "type", t);
7032 remove = true;
7033 }
7034 }
7035 else if (c_omp_depend_t_p (TYPE_REF_P (TREE_TYPE (t))
7036 ? TREE_TYPE (TREE_TYPE (t))
7037 : TREE_TYPE (t)))
7038 {
7039 error_at (OMP_CLAUSE_LOCATION (c),
7040 "%qE should not have %<omp_depend_t%> type in "
7041 "%<depend%> clause with dependence type other than "
7042 "%<depobj%>", t);
7043 remove = true;
7044 }
7045 if (!remove)
7046 {
7047 tree addr = cp_build_addr_expr (t, tf_warning_or_error);
7048 if (addr == error_mark_node)
7049 remove = true;
7050 else
7051 {
7052 t = cp_build_indirect_ref (addr, RO_UNARY_STAR,
7053 tf_warning_or_error);
7054 if (t == error_mark_node)
7055 remove = true;
7056 else if (TREE_CODE (OMP_CLAUSE_DECL (c)) == TREE_LIST
7057 && TREE_PURPOSE (OMP_CLAUSE_DECL (c))
7058 && (TREE_CODE (TREE_PURPOSE (OMP_CLAUSE_DECL (c)))
7059 == TREE_VEC))
7060 TREE_VALUE (OMP_CLAUSE_DECL (c)) = t;
7061 else
7062 OMP_CLAUSE_DECL (c) = t;
7063 }
7064 }
7065 break;
7066
7067 case OMP_CLAUSE_MAP:
7068 case OMP_CLAUSE_TO:
7069 case OMP_CLAUSE_FROM:
7070 case OMP_CLAUSE__CACHE_:
7071 t = OMP_CLAUSE_DECL (c);
7072 if (TREE_CODE (t) == TREE_LIST)
7073 {
7074 if (handle_omp_array_sections (c, ort))
7075 remove = true;
7076 else
7077 {
7078 t = OMP_CLAUSE_DECL (c);
7079 if (TREE_CODE (t) != TREE_LIST
7080 && !type_dependent_expression_p (t)
7081 && !cp_omp_mappable_type (TREE_TYPE (t)))
7082 {
7083 error_at (OMP_CLAUSE_LOCATION (c),
7084 "array section does not have mappable type "
7085 "in %qs clause",
7086 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7087 remove = true;
7088 }
7089 while (TREE_CODE (t) == ARRAY_REF)
7090 t = TREE_OPERAND (t, 0);
7091 if (TREE_CODE (t) == COMPONENT_REF
7092 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
7093 {
7094 while (TREE_CODE (t) == COMPONENT_REF)
7095 t = TREE_OPERAND (t, 0);
7096 if (REFERENCE_REF_P (t))
7097 t = TREE_OPERAND (t, 0);
7098 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
7099 break;
7100 if (bitmap_bit_p (&map_head, DECL_UID (t)))
7101 {
7102 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
7103 error_at (OMP_CLAUSE_LOCATION (c),
7104 "%qD appears more than once in motion"
7105 " clauses", t);
7106 else if (ort == C_ORT_ACC)
7107 error_at (OMP_CLAUSE_LOCATION (c),
7108 "%qD appears more than once in data"
7109 " clauses", t);
7110 else
7111 error_at (OMP_CLAUSE_LOCATION (c),
7112 "%qD appears more than once in map"
7113 " clauses", t);
7114 remove = true;
7115 }
7116 else
7117 {
7118 bitmap_set_bit (&map_head, DECL_UID (t));
7119 bitmap_set_bit (&map_field_head, DECL_UID (t));
7120 }
7121 }
7122 }
7123 break;
7124 }
7125 if (t == error_mark_node)
7126 {
7127 remove = true;
7128 break;
7129 }
7130 if (REFERENCE_REF_P (t)
7131 && TREE_CODE (TREE_OPERAND (t, 0)) == COMPONENT_REF)
7132 {
7133 t = TREE_OPERAND (t, 0);
7134 OMP_CLAUSE_DECL (c) = t;
7135 }
7136 if (TREE_CODE (t) == COMPONENT_REF
7137 && (ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP
7138 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE__CACHE_)
7139 {
7140 if (type_dependent_expression_p (t))
7141 break;
7142 if (TREE_CODE (TREE_OPERAND (t, 1)) == FIELD_DECL
7143 && DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
7144 {
7145 error_at (OMP_CLAUSE_LOCATION (c),
7146 "bit-field %qE in %qs clause",
7147 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7148 remove = true;
7149 }
7150 else if (!cp_omp_mappable_type (TREE_TYPE (t)))
7151 {
7152 error_at (OMP_CLAUSE_LOCATION (c),
7153 "%qE does not have a mappable type in %qs clause",
7154 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7155 remove = true;
7156 }
7157 while (TREE_CODE (t) == COMPONENT_REF)
7158 {
7159 if (TREE_TYPE (TREE_OPERAND (t, 0))
7160 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0)))
7161 == UNION_TYPE))
7162 {
7163 error_at (OMP_CLAUSE_LOCATION (c),
7164 "%qE is a member of a union", t);
7165 remove = true;
7166 break;
7167 }
7168 t = TREE_OPERAND (t, 0);
7169 }
7170 if (remove)
7171 break;
7172 if (REFERENCE_REF_P (t))
7173 t = TREE_OPERAND (t, 0);
7174 if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
7175 {
7176 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
7177 goto handle_map_references;
7178 }
7179 }
7180 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
7181 {
7182 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
7183 break;
7184 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
7185 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
7186 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ALWAYS_POINTER))
7187 break;
7188 if (DECL_P (t))
7189 error_at (OMP_CLAUSE_LOCATION (c),
7190 "%qD is not a variable in %qs clause", t,
7191 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7192 else
7193 error_at (OMP_CLAUSE_LOCATION (c),
7194 "%qE is not a variable in %qs clause", t,
7195 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7196 remove = true;
7197 }
7198 else if (VAR_P (t) && CP_DECL_THREAD_LOCAL_P (t))
7199 {
7200 error_at (OMP_CLAUSE_LOCATION (c),
7201 "%qD is threadprivate variable in %qs clause", t,
7202 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7203 remove = true;
7204 }
7205 else if (ort != C_ORT_ACC && t == current_class_ptr)
7206 {
7207 error_at (OMP_CLAUSE_LOCATION (c),
7208 "%<this%> allowed in OpenMP only in %<declare simd%>"
7209 " clauses");
7210 remove = true;
7211 break;
7212 }
7213 else if (!processing_template_decl
7214 && !TYPE_REF_P (TREE_TYPE (t))
7215 && (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
7216 || (OMP_CLAUSE_MAP_KIND (c)
7217 != GOMP_MAP_FIRSTPRIVATE_POINTER))
7218 && !cxx_mark_addressable (t))
7219 remove = true;
7220 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
7221 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
7222 || (OMP_CLAUSE_MAP_KIND (c)
7223 == GOMP_MAP_FIRSTPRIVATE_POINTER)))
7224 && t == OMP_CLAUSE_DECL (c)
7225 && !type_dependent_expression_p (t)
7226 && !cp_omp_mappable_type (TYPE_REF_P (TREE_TYPE (t))
7227 ? TREE_TYPE (TREE_TYPE (t))
7228 : TREE_TYPE (t)))
7229 {
7230 error_at (OMP_CLAUSE_LOCATION (c),
7231 "%qD does not have a mappable type in %qs clause", t,
7232 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7233 remove = true;
7234 }
7235 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
7236 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FORCE_DEVICEPTR
7237 && !type_dependent_expression_p (t)
7238 && !INDIRECT_TYPE_P (TREE_TYPE (t)))
7239 {
7240 error_at (OMP_CLAUSE_LOCATION (c),
7241 "%qD is not a pointer variable", t);
7242 remove = true;
7243 }
7244 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
7245 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
7246 {
7247 if (bitmap_bit_p (&generic_head, DECL_UID (t))
7248 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
7249 {
7250 error_at (OMP_CLAUSE_LOCATION (c),
7251 "%qD appears more than once in data clauses", t);
7252 remove = true;
7253 }
7254 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
7255 {
7256 if (ort == C_ORT_ACC)
7257 error_at (OMP_CLAUSE_LOCATION (c),
7258 "%qD appears more than once in data clauses", t);
7259 else
7260 error_at (OMP_CLAUSE_LOCATION (c),
7261 "%qD appears both in data and map clauses", t);
7262 remove = true;
7263 }
7264 else
7265 bitmap_set_bit (&generic_head, DECL_UID (t));
7266 }
7267 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
7268 {
7269 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
7270 error_at (OMP_CLAUSE_LOCATION (c),
7271 "%qD appears more than once in motion clauses", t);
7272 if (ort == C_ORT_ACC)
7273 error_at (OMP_CLAUSE_LOCATION (c),
7274 "%qD appears more than once in data clauses", t);
7275 else
7276 error_at (OMP_CLAUSE_LOCATION (c),
7277 "%qD appears more than once in map clauses", t);
7278 remove = true;
7279 }
7280 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
7281 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
7282 {
7283 if (ort == C_ORT_ACC)
7284 error_at (OMP_CLAUSE_LOCATION (c),
7285 "%qD appears more than once in data clauses", t);
7286 else
7287 error_at (OMP_CLAUSE_LOCATION (c),
7288 "%qD appears both in data and map clauses", t);
7289 remove = true;
7290 }
7291 else
7292 {
7293 bitmap_set_bit (&map_head, DECL_UID (t));
7294 if (t != OMP_CLAUSE_DECL (c)
7295 && TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPONENT_REF)
7296 bitmap_set_bit (&map_field_head, DECL_UID (t));
7297 }
7298 handle_map_references:
7299 if (!remove
7300 && !processing_template_decl
7301 && ort != C_ORT_DECLARE_SIMD
7302 && TYPE_REF_P (TREE_TYPE (OMP_CLAUSE_DECL (c))))
7303 {
7304 t = OMP_CLAUSE_DECL (c);
7305 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
7306 {
7307 OMP_CLAUSE_DECL (c) = build_simple_mem_ref (t);
7308 if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
7309 OMP_CLAUSE_SIZE (c)
7310 = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t)));
7311 }
7312 else if (OMP_CLAUSE_MAP_KIND (c)
7313 != GOMP_MAP_FIRSTPRIVATE_POINTER
7314 && (OMP_CLAUSE_MAP_KIND (c)
7315 != GOMP_MAP_FIRSTPRIVATE_REFERENCE)
7316 && (OMP_CLAUSE_MAP_KIND (c)
7317 != GOMP_MAP_ALWAYS_POINTER))
7318 {
7319 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
7320 OMP_CLAUSE_MAP);
7321 if (TREE_CODE (t) == COMPONENT_REF)
7322 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ALWAYS_POINTER);
7323 else
7324 OMP_CLAUSE_SET_MAP_KIND (c2,
7325 GOMP_MAP_FIRSTPRIVATE_REFERENCE);
7326 OMP_CLAUSE_DECL (c2) = t;
7327 OMP_CLAUSE_SIZE (c2) = size_zero_node;
7328 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
7329 OMP_CLAUSE_CHAIN (c) = c2;
7330 OMP_CLAUSE_DECL (c) = build_simple_mem_ref (t);
7331 if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
7332 OMP_CLAUSE_SIZE (c)
7333 = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t)));
7334 c = c2;
7335 }
7336 }
7337 break;
7338
7339 case OMP_CLAUSE_TO_DECLARE:
7340 case OMP_CLAUSE_LINK:
7341 t = OMP_CLAUSE_DECL (c);
7342 if (TREE_CODE (t) == FUNCTION_DECL
7343 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
7344 ;
7345 else if (!VAR_P (t))
7346 {
7347 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
7348 {
7349 if (TREE_CODE (t) == TEMPLATE_ID_EXPR)
7350 error_at (OMP_CLAUSE_LOCATION (c),
7351 "template %qE in clause %qs", t,
7352 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7353 else if (really_overloaded_fn (t))
7354 error_at (OMP_CLAUSE_LOCATION (c),
7355 "overloaded function name %qE in clause %qs", t,
7356 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7357 else
7358 error_at (OMP_CLAUSE_LOCATION (c),
7359 "%qE is neither a variable nor a function name "
7360 "in clause %qs", t,
7361 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7362 }
7363 else
7364 error_at (OMP_CLAUSE_LOCATION (c),
7365 "%qE is not a variable in clause %qs", t,
7366 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7367 remove = true;
7368 }
7369 else if (DECL_THREAD_LOCAL_P (t))
7370 {
7371 error_at (OMP_CLAUSE_LOCATION (c),
7372 "%qD is threadprivate variable in %qs clause", t,
7373 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7374 remove = true;
7375 }
7376 else if (!cp_omp_mappable_type (TREE_TYPE (t)))
7377 {
7378 error_at (OMP_CLAUSE_LOCATION (c),
7379 "%qD does not have a mappable type in %qs clause", t,
7380 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7381 remove = true;
7382 }
7383 if (remove)
7384 break;
7385 if (bitmap_bit_p (&generic_head, DECL_UID (t)))
7386 {
7387 error_at (OMP_CLAUSE_LOCATION (c),
7388 "%qE appears more than once on the same "
7389 "%<declare target%> directive", t);
7390 remove = true;
7391 }
7392 else
7393 bitmap_set_bit (&generic_head, DECL_UID (t));
7394 break;
7395
7396 case OMP_CLAUSE_UNIFORM:
7397 t = OMP_CLAUSE_DECL (c);
7398 if (TREE_CODE (t) != PARM_DECL)
7399 {
7400 if (processing_template_decl)
7401 break;
7402 if (DECL_P (t))
7403 error_at (OMP_CLAUSE_LOCATION (c),
7404 "%qD is not an argument in %<uniform%> clause", t);
7405 else
7406 error_at (OMP_CLAUSE_LOCATION (c),
7407 "%qE is not an argument in %<uniform%> clause", t);
7408 remove = true;
7409 break;
7410 }
7411 /* map_head bitmap is used as uniform_head if declare_simd. */
7412 bitmap_set_bit (&map_head, DECL_UID (t));
7413 goto check_dup_generic;
7414
7415 case OMP_CLAUSE_GRAINSIZE:
7416 t = OMP_CLAUSE_GRAINSIZE_EXPR (c);
7417 if (t == error_mark_node)
7418 remove = true;
7419 else if (!type_dependent_expression_p (t)
7420 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
7421 {
7422 error_at (OMP_CLAUSE_LOCATION (c),
7423 "%<grainsize%> expression must be integral");
7424 remove = true;
7425 }
7426 else
7427 {
7428 t = mark_rvalue_use (t);
7429 if (!processing_template_decl)
7430 {
7431 t = maybe_constant_value (t);
7432 if (TREE_CODE (t) == INTEGER_CST
7433 && tree_int_cst_sgn (t) != 1)
7434 {
7435 warning_at (OMP_CLAUSE_LOCATION (c), 0,
7436 "%<grainsize%> value must be positive");
7437 t = integer_one_node;
7438 }
7439 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7440 }
7441 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
7442 }
7443 break;
7444
7445 case OMP_CLAUSE_PRIORITY:
7446 t = OMP_CLAUSE_PRIORITY_EXPR (c);
7447 if (t == error_mark_node)
7448 remove = true;
7449 else if (!type_dependent_expression_p (t)
7450 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
7451 {
7452 error_at (OMP_CLAUSE_LOCATION (c),
7453 "%<priority%> expression must be integral");
7454 remove = true;
7455 }
7456 else
7457 {
7458 t = mark_rvalue_use (t);
7459 if (!processing_template_decl)
7460 {
7461 t = maybe_constant_value (t);
7462 if (TREE_CODE (t) == INTEGER_CST
7463 && tree_int_cst_sgn (t) == -1)
7464 {
7465 warning_at (OMP_CLAUSE_LOCATION (c), 0,
7466 "%<priority%> value must be non-negative");
7467 t = integer_one_node;
7468 }
7469 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7470 }
7471 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
7472 }
7473 break;
7474
7475 case OMP_CLAUSE_HINT:
7476 t = OMP_CLAUSE_HINT_EXPR (c);
7477 if (t == error_mark_node)
7478 remove = true;
7479 else if (!type_dependent_expression_p (t)
7480 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
7481 {
7482 error_at (OMP_CLAUSE_LOCATION (c),
7483 "%<hint%> expression must be integral");
7484 remove = true;
7485 }
7486 else
7487 {
7488 t = mark_rvalue_use (t);
7489 if (!processing_template_decl)
7490 {
7491 t = maybe_constant_value (t);
7492 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7493 if (TREE_CODE (t) != INTEGER_CST)
7494 {
7495 error_at (OMP_CLAUSE_LOCATION (c),
7496 "%<hint%> expression must be constant integer "
7497 "expression");
7498 remove = true;
7499 }
7500 }
7501 OMP_CLAUSE_HINT_EXPR (c) = t;
7502 }
7503 break;
7504
7505 case OMP_CLAUSE_IS_DEVICE_PTR:
7506 case OMP_CLAUSE_USE_DEVICE_PTR:
7507 field_ok = (ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP;
7508 t = OMP_CLAUSE_DECL (c);
7509 if (!type_dependent_expression_p (t))
7510 {
7511 tree type = TREE_TYPE (t);
7512 if (!TYPE_PTR_P (type)
7513 && TREE_CODE (type) != ARRAY_TYPE
7514 && (!TYPE_REF_P (type)
7515 || (!TYPE_PTR_P (TREE_TYPE (type))
7516 && TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE)))
7517 {
7518 error_at (OMP_CLAUSE_LOCATION (c),
7519 "%qs variable is neither a pointer, nor an array "
7520 "nor reference to pointer or array",
7521 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7522 remove = true;
7523 }
7524 }
7525 goto check_dup_generic;
7526
7527 case OMP_CLAUSE_NOWAIT:
7528 case OMP_CLAUSE_DEFAULT:
7529 case OMP_CLAUSE_UNTIED:
7530 case OMP_CLAUSE_COLLAPSE:
7531 case OMP_CLAUSE_MERGEABLE:
7532 case OMP_CLAUSE_PARALLEL:
7533 case OMP_CLAUSE_FOR:
7534 case OMP_CLAUSE_SECTIONS:
7535 case OMP_CLAUSE_TASKGROUP:
7536 case OMP_CLAUSE_PROC_BIND:
7537 case OMP_CLAUSE_NOGROUP:
7538 case OMP_CLAUSE_THREADS:
7539 case OMP_CLAUSE_SIMD:
7540 case OMP_CLAUSE_DEFAULTMAP:
7541 case OMP_CLAUSE_AUTO:
7542 case OMP_CLAUSE_INDEPENDENT:
7543 case OMP_CLAUSE_SEQ:
7544 case OMP_CLAUSE_IF_PRESENT:
7545 case OMP_CLAUSE_FINALIZE:
7546 break;
7547
7548 case OMP_CLAUSE_TILE:
7549 for (tree list = OMP_CLAUSE_TILE_LIST (c); !remove && list;
7550 list = TREE_CHAIN (list))
7551 {
7552 t = TREE_VALUE (list);
7553
7554 if (t == error_mark_node)
7555 remove = true;
7556 else if (!type_dependent_expression_p (t)
7557 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
7558 {
7559 error_at (OMP_CLAUSE_LOCATION (c),
7560 "%<tile%> argument needs integral type");
7561 remove = true;
7562 }
7563 else
7564 {
7565 t = mark_rvalue_use (t);
7566 if (!processing_template_decl)
7567 {
7568 /* Zero is used to indicate '*', we permit you
7569 to get there via an ICE of value zero. */
7570 t = maybe_constant_value (t);
7571 if (!tree_fits_shwi_p (t)
7572 || tree_to_shwi (t) < 0)
7573 {
7574 error_at (OMP_CLAUSE_LOCATION (c),
7575 "%<tile%> argument needs positive "
7576 "integral constant");
7577 remove = true;
7578 }
7579 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7580 }
7581 }
7582
7583 /* Update list item. */
7584 TREE_VALUE (list) = t;
7585 }
7586 break;
7587
7588 case OMP_CLAUSE_ORDERED:
7589 ordered_seen = true;
7590 break;
7591
7592 case OMP_CLAUSE_INBRANCH:
7593 case OMP_CLAUSE_NOTINBRANCH:
7594 if (branch_seen)
7595 {
7596 error_at (OMP_CLAUSE_LOCATION (c),
7597 "%<inbranch%> clause is incompatible with "
7598 "%<notinbranch%>");
7599 remove = true;
7600 }
7601 branch_seen = true;
7602 break;
7603
7604 default:
7605 gcc_unreachable ();
7606 }
7607
7608 if (remove)
7609 *pc = OMP_CLAUSE_CHAIN (c);
7610 else
7611 pc = &OMP_CLAUSE_CHAIN (c);
7612 }
7613
7614 for (pc = &clauses, c = clauses; c ; c = *pc)
7615 {
7616 enum omp_clause_code c_kind = OMP_CLAUSE_CODE (c);
7617 bool remove = false;
7618 bool need_complete_type = false;
7619 bool need_default_ctor = false;
7620 bool need_copy_ctor = false;
7621 bool need_copy_assignment = false;
7622 bool need_implicitly_determined = false;
7623 bool need_dtor = false;
7624 tree type, inner_type;
7625
7626 switch (c_kind)
7627 {
7628 case OMP_CLAUSE_SHARED:
7629 need_implicitly_determined = true;
7630 break;
7631 case OMP_CLAUSE_PRIVATE:
7632 need_complete_type = true;
7633 need_default_ctor = true;
7634 need_dtor = true;
7635 need_implicitly_determined = true;
7636 break;
7637 case OMP_CLAUSE_FIRSTPRIVATE:
7638 need_complete_type = true;
7639 need_copy_ctor = true;
7640 need_dtor = true;
7641 need_implicitly_determined = true;
7642 break;
7643 case OMP_CLAUSE_LASTPRIVATE:
7644 need_complete_type = true;
7645 need_copy_assignment = true;
7646 need_implicitly_determined = true;
7647 break;
7648 case OMP_CLAUSE_REDUCTION:
7649 case OMP_CLAUSE_IN_REDUCTION:
7650 case OMP_CLAUSE_TASK_REDUCTION:
7651 need_implicitly_determined = true;
7652 break;
7653 case OMP_CLAUSE_LINEAR:
7654 if (ort != C_ORT_OMP_DECLARE_SIMD)
7655 need_implicitly_determined = true;
7656 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c)
7657 && !bitmap_bit_p (&map_head,
7658 DECL_UID (OMP_CLAUSE_LINEAR_STEP (c))))
7659 {
7660 error_at (OMP_CLAUSE_LOCATION (c),
7661 "%<linear%> clause step is a parameter %qD not "
7662 "specified in %<uniform%> clause",
7663 OMP_CLAUSE_LINEAR_STEP (c));
7664 *pc = OMP_CLAUSE_CHAIN (c);
7665 continue;
7666 }
7667 break;
7668 case OMP_CLAUSE_COPYPRIVATE:
7669 need_copy_assignment = true;
7670 break;
7671 case OMP_CLAUSE_COPYIN:
7672 need_copy_assignment = true;
7673 break;
7674 case OMP_CLAUSE_SIMDLEN:
7675 if (safelen
7676 && !processing_template_decl
7677 && tree_int_cst_lt (OMP_CLAUSE_SAFELEN_EXPR (safelen),
7678 OMP_CLAUSE_SIMDLEN_EXPR (c)))
7679 {
7680 error_at (OMP_CLAUSE_LOCATION (c),
7681 "%<simdlen%> clause value is bigger than "
7682 "%<safelen%> clause value");
7683 OMP_CLAUSE_SIMDLEN_EXPR (c)
7684 = OMP_CLAUSE_SAFELEN_EXPR (safelen);
7685 }
7686 pc = &OMP_CLAUSE_CHAIN (c);
7687 continue;
7688 case OMP_CLAUSE_SCHEDULE:
7689 if (ordered_seen
7690 && (OMP_CLAUSE_SCHEDULE_KIND (c)
7691 & OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
7692 {
7693 error_at (OMP_CLAUSE_LOCATION (c),
7694 "%<nonmonotonic%> schedule modifier specified "
7695 "together with %<ordered%> clause");
7696 OMP_CLAUSE_SCHEDULE_KIND (c)
7697 = (enum omp_clause_schedule_kind)
7698 (OMP_CLAUSE_SCHEDULE_KIND (c)
7699 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
7700 }
7701 pc = &OMP_CLAUSE_CHAIN (c);
7702 continue;
7703 case OMP_CLAUSE_NOGROUP:
7704 if (reduction_seen)
7705 {
7706 error_at (OMP_CLAUSE_LOCATION (c),
7707 "%<nogroup%> clause must not be used together with "
7708 "%<reduction%> clause");
7709 *pc = OMP_CLAUSE_CHAIN (c);
7710 continue;
7711 }
7712 pc = &OMP_CLAUSE_CHAIN (c);
7713 continue;
7714 case OMP_CLAUSE_NOWAIT:
7715 if (copyprivate_seen)
7716 {
7717 error_at (OMP_CLAUSE_LOCATION (c),
7718 "%<nowait%> clause must not be used together "
7719 "with %<copyprivate%>");
7720 *pc = OMP_CLAUSE_CHAIN (c);
7721 continue;
7722 }
7723 /* FALLTHRU */
7724 default:
7725 pc = &OMP_CLAUSE_CHAIN (c);
7726 continue;
7727 }
7728
7729 t = OMP_CLAUSE_DECL (c);
7730 if (processing_template_decl
7731 && !VAR_P (t) && TREE_CODE (t) != PARM_DECL)
7732 {
7733 pc = &OMP_CLAUSE_CHAIN (c);
7734 continue;
7735 }
7736
7737 switch (c_kind)
7738 {
7739 case OMP_CLAUSE_LASTPRIVATE:
7740 if (!bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
7741 {
7742 need_default_ctor = true;
7743 need_dtor = true;
7744 }
7745 break;
7746
7747 case OMP_CLAUSE_REDUCTION:
7748 case OMP_CLAUSE_IN_REDUCTION:
7749 case OMP_CLAUSE_TASK_REDUCTION:
7750 if (finish_omp_reduction_clause (c, &need_default_ctor,
7751 &need_dtor))
7752 remove = true;
7753 else
7754 t = OMP_CLAUSE_DECL (c);
7755 break;
7756
7757 case OMP_CLAUSE_COPYIN:
7758 if (!VAR_P (t) || !CP_DECL_THREAD_LOCAL_P (t))
7759 {
7760 error_at (OMP_CLAUSE_LOCATION (c),
7761 "%qE must be %<threadprivate%> for %<copyin%>", t);
7762 remove = true;
7763 }
7764 break;
7765
7766 default:
7767 break;
7768 }
7769
7770 if (need_complete_type || need_copy_assignment)
7771 {
7772 t = require_complete_type (t);
7773 if (t == error_mark_node)
7774 remove = true;
7775 else if (TYPE_REF_P (TREE_TYPE (t))
7776 && !complete_type_or_else (TREE_TYPE (TREE_TYPE (t)), t))
7777 remove = true;
7778 }
7779 if (need_implicitly_determined)
7780 {
7781 const char *share_name = NULL;
7782
7783 if (VAR_P (t) && CP_DECL_THREAD_LOCAL_P (t))
7784 share_name = "threadprivate";
7785 else switch (cxx_omp_predetermined_sharing_1 (t))
7786 {
7787 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
7788 break;
7789 case OMP_CLAUSE_DEFAULT_SHARED:
7790 if (VAR_P (t)
7791 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
7792 && TREE_STATIC (t)
7793 && cxx_omp_const_qual_no_mutable (t))
7794 {
7795 tree ctx = CP_DECL_CONTEXT (t);
7796 /* const qualified static data members without mutable
7797 member may be specified in firstprivate clause. */
7798 if (TYPE_P (ctx) && MAYBE_CLASS_TYPE_P (ctx))
7799 break;
7800 }
7801 share_name = "shared";
7802 break;
7803 case OMP_CLAUSE_DEFAULT_PRIVATE:
7804 share_name = "private";
7805 break;
7806 default:
7807 gcc_unreachable ();
7808 }
7809 if (share_name)
7810 {
7811 error_at (OMP_CLAUSE_LOCATION (c),
7812 "%qE is predetermined %qs for %qs",
7813 omp_clause_printable_decl (t), share_name,
7814 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7815 remove = true;
7816 }
7817 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_SHARED
7818 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_FIRSTPRIVATE
7819 && cxx_omp_const_qual_no_mutable (t))
7820 {
7821 error_at (OMP_CLAUSE_LOCATION (c),
7822 "%<const%> qualified %qE without %<mutable%> member "
7823 "may appear only in %<shared%> or %<firstprivate%> "
7824 "clauses", omp_clause_printable_decl (t));
7825 remove = true;
7826 }
7827 }
7828
7829 /* We're interested in the base element, not arrays. */
7830 inner_type = type = TREE_TYPE (t);
7831 if ((need_complete_type
7832 || need_copy_assignment
7833 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
7834 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
7835 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
7836 && TYPE_REF_P (inner_type))
7837 inner_type = TREE_TYPE (inner_type);
7838 while (TREE_CODE (inner_type) == ARRAY_TYPE)
7839 inner_type = TREE_TYPE (inner_type);
7840
7841 /* Check for special function availability by building a call to one.
7842 Save the results, because later we won't be in the right context
7843 for making these queries. */
7844 if (CLASS_TYPE_P (inner_type)
7845 && COMPLETE_TYPE_P (inner_type)
7846 && (need_default_ctor || need_copy_ctor
7847 || need_copy_assignment || need_dtor)
7848 && !type_dependent_expression_p (t)
7849 && cxx_omp_create_clause_info (c, inner_type, need_default_ctor,
7850 need_copy_ctor, need_copy_assignment,
7851 need_dtor))
7852 remove = true;
7853
7854 if (!remove
7855 && c_kind == OMP_CLAUSE_SHARED
7856 && processing_template_decl)
7857 {
7858 t = omp_clause_decl_field (OMP_CLAUSE_DECL (c));
7859 if (t)
7860 OMP_CLAUSE_DECL (c) = t;
7861 }
7862
7863 if (remove)
7864 *pc = OMP_CLAUSE_CHAIN (c);
7865 else
7866 pc = &OMP_CLAUSE_CHAIN (c);
7867 }
7868
7869 bitmap_obstack_release (NULL);
7870 return clauses;
7871 }
7872
7873 /* Start processing OpenMP clauses that can include any
7874 privatization clauses for non-static data members. */
7875
7876 tree
7877 push_omp_privatization_clauses (bool ignore_next)
7878 {
7879 if (omp_private_member_ignore_next)
7880 {
7881 omp_private_member_ignore_next = ignore_next;
7882 return NULL_TREE;
7883 }
7884 omp_private_member_ignore_next = ignore_next;
7885 if (omp_private_member_map)
7886 omp_private_member_vec.safe_push (error_mark_node);
7887 return push_stmt_list ();
7888 }
7889
7890 /* Revert remapping of any non-static data members since
7891 the last push_omp_privatization_clauses () call. */
7892
7893 void
7894 pop_omp_privatization_clauses (tree stmt)
7895 {
7896 if (stmt == NULL_TREE)
7897 return;
7898 stmt = pop_stmt_list (stmt);
7899 if (omp_private_member_map)
7900 {
7901 while (!omp_private_member_vec.is_empty ())
7902 {
7903 tree t = omp_private_member_vec.pop ();
7904 if (t == error_mark_node)
7905 {
7906 add_stmt (stmt);
7907 return;
7908 }
7909 bool no_decl_expr = t == integer_zero_node;
7910 if (no_decl_expr)
7911 t = omp_private_member_vec.pop ();
7912 tree *v = omp_private_member_map->get (t);
7913 gcc_assert (v);
7914 if (!no_decl_expr)
7915 add_decl_expr (*v);
7916 omp_private_member_map->remove (t);
7917 }
7918 delete omp_private_member_map;
7919 omp_private_member_map = NULL;
7920 }
7921 add_stmt (stmt);
7922 }
7923
7924 /* Remember OpenMP privatization clauses mapping and clear it.
7925 Used for lambdas. */
7926
7927 void
7928 save_omp_privatization_clauses (vec<tree> &save)
7929 {
7930 save = vNULL;
7931 if (omp_private_member_ignore_next)
7932 save.safe_push (integer_one_node);
7933 omp_private_member_ignore_next = false;
7934 if (!omp_private_member_map)
7935 return;
7936
7937 while (!omp_private_member_vec.is_empty ())
7938 {
7939 tree t = omp_private_member_vec.pop ();
7940 if (t == error_mark_node)
7941 {
7942 save.safe_push (t);
7943 continue;
7944 }
7945 tree n = t;
7946 if (t == integer_zero_node)
7947 t = omp_private_member_vec.pop ();
7948 tree *v = omp_private_member_map->get (t);
7949 gcc_assert (v);
7950 save.safe_push (*v);
7951 save.safe_push (t);
7952 if (n != t)
7953 save.safe_push (n);
7954 }
7955 delete omp_private_member_map;
7956 omp_private_member_map = NULL;
7957 }
7958
7959 /* Restore OpenMP privatization clauses mapping saved by the
7960 above function. */
7961
7962 void
7963 restore_omp_privatization_clauses (vec<tree> &save)
7964 {
7965 gcc_assert (omp_private_member_vec.is_empty ());
7966 omp_private_member_ignore_next = false;
7967 if (save.is_empty ())
7968 return;
7969 if (save.length () == 1 && save[0] == integer_one_node)
7970 {
7971 omp_private_member_ignore_next = true;
7972 save.release ();
7973 return;
7974 }
7975
7976 omp_private_member_map = new hash_map <tree, tree>;
7977 while (!save.is_empty ())
7978 {
7979 tree t = save.pop ();
7980 tree n = t;
7981 if (t != error_mark_node)
7982 {
7983 if (t == integer_one_node)
7984 {
7985 omp_private_member_ignore_next = true;
7986 gcc_assert (save.is_empty ());
7987 break;
7988 }
7989 if (t == integer_zero_node)
7990 t = save.pop ();
7991 tree &v = omp_private_member_map->get_or_insert (t);
7992 v = save.pop ();
7993 }
7994 omp_private_member_vec.safe_push (t);
7995 if (n != t)
7996 omp_private_member_vec.safe_push (n);
7997 }
7998 save.release ();
7999 }
8000
8001 /* For all variables in the tree_list VARS, mark them as thread local. */
8002
8003 void
8004 finish_omp_threadprivate (tree vars)
8005 {
8006 tree t;
8007
8008 /* Mark every variable in VARS to be assigned thread local storage. */
8009 for (t = vars; t; t = TREE_CHAIN (t))
8010 {
8011 tree v = TREE_PURPOSE (t);
8012
8013 if (error_operand_p (v))
8014 ;
8015 else if (!VAR_P (v))
8016 error ("%<threadprivate%> %qD is not file, namespace "
8017 "or block scope variable", v);
8018 /* If V had already been marked threadprivate, it doesn't matter
8019 whether it had been used prior to this point. */
8020 else if (TREE_USED (v)
8021 && (DECL_LANG_SPECIFIC (v) == NULL
8022 || !CP_DECL_THREADPRIVATE_P (v)))
8023 error ("%qE declared %<threadprivate%> after first use", v);
8024 else if (! TREE_STATIC (v) && ! DECL_EXTERNAL (v))
8025 error ("automatic variable %qE cannot be %<threadprivate%>", v);
8026 else if (! COMPLETE_TYPE_P (complete_type (TREE_TYPE (v))))
8027 error ("%<threadprivate%> %qE has incomplete type", v);
8028 else if (TREE_STATIC (v) && TYPE_P (CP_DECL_CONTEXT (v))
8029 && CP_DECL_CONTEXT (v) != current_class_type)
8030 error ("%<threadprivate%> %qE directive not "
8031 "in %qT definition", v, CP_DECL_CONTEXT (v));
8032 else
8033 {
8034 /* Allocate a LANG_SPECIFIC structure for V, if needed. */
8035 if (DECL_LANG_SPECIFIC (v) == NULL)
8036 retrofit_lang_decl (v);
8037
8038 if (! CP_DECL_THREAD_LOCAL_P (v))
8039 {
8040 CP_DECL_THREAD_LOCAL_P (v) = true;
8041 set_decl_tls_model (v, decl_default_tls_model (v));
8042 /* If rtl has been already set for this var, call
8043 make_decl_rtl once again, so that encode_section_info
8044 has a chance to look at the new decl flags. */
8045 if (DECL_RTL_SET_P (v))
8046 make_decl_rtl (v);
8047 }
8048 CP_DECL_THREADPRIVATE_P (v) = 1;
8049 }
8050 }
8051 }
8052
8053 /* Build an OpenMP structured block. */
8054
8055 tree
8056 begin_omp_structured_block (void)
8057 {
8058 return do_pushlevel (sk_omp);
8059 }
8060
8061 tree
8062 finish_omp_structured_block (tree block)
8063 {
8064 return do_poplevel (block);
8065 }
8066
8067 /* Similarly, except force the retention of the BLOCK. */
8068
8069 tree
8070 begin_omp_parallel (void)
8071 {
8072 keep_next_level (true);
8073 return begin_omp_structured_block ();
8074 }
8075
8076 /* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
8077 statement. */
8078
8079 tree
8080 finish_oacc_data (tree clauses, tree block)
8081 {
8082 tree stmt;
8083
8084 block = finish_omp_structured_block (block);
8085
8086 stmt = make_node (OACC_DATA);
8087 TREE_TYPE (stmt) = void_type_node;
8088 OACC_DATA_CLAUSES (stmt) = clauses;
8089 OACC_DATA_BODY (stmt) = block;
8090
8091 return add_stmt (stmt);
8092 }
8093
8094 /* Generate OACC_HOST_DATA, with CLAUSES and BLOCK as its compound
8095 statement. */
8096
8097 tree
8098 finish_oacc_host_data (tree clauses, tree block)
8099 {
8100 tree stmt;
8101
8102 block = finish_omp_structured_block (block);
8103
8104 stmt = make_node (OACC_HOST_DATA);
8105 TREE_TYPE (stmt) = void_type_node;
8106 OACC_HOST_DATA_CLAUSES (stmt) = clauses;
8107 OACC_HOST_DATA_BODY (stmt) = block;
8108
8109 return add_stmt (stmt);
8110 }
8111
8112 /* Generate OMP construct CODE, with BODY and CLAUSES as its compound
8113 statement. */
8114
8115 tree
8116 finish_omp_construct (enum tree_code code, tree body, tree clauses)
8117 {
8118 body = finish_omp_structured_block (body);
8119
8120 tree stmt = make_node (code);
8121 TREE_TYPE (stmt) = void_type_node;
8122 OMP_BODY (stmt) = body;
8123 OMP_CLAUSES (stmt) = clauses;
8124
8125 return add_stmt (stmt);
8126 }
8127
8128 tree
8129 finish_omp_parallel (tree clauses, tree body)
8130 {
8131 tree stmt;
8132
8133 body = finish_omp_structured_block (body);
8134
8135 stmt = make_node (OMP_PARALLEL);
8136 TREE_TYPE (stmt) = void_type_node;
8137 OMP_PARALLEL_CLAUSES (stmt) = clauses;
8138 OMP_PARALLEL_BODY (stmt) = body;
8139
8140 return add_stmt (stmt);
8141 }
8142
8143 tree
8144 begin_omp_task (void)
8145 {
8146 keep_next_level (true);
8147 return begin_omp_structured_block ();
8148 }
8149
8150 tree
8151 finish_omp_task (tree clauses, tree body)
8152 {
8153 tree stmt;
8154
8155 body = finish_omp_structured_block (body);
8156
8157 stmt = make_node (OMP_TASK);
8158 TREE_TYPE (stmt) = void_type_node;
8159 OMP_TASK_CLAUSES (stmt) = clauses;
8160 OMP_TASK_BODY (stmt) = body;
8161
8162 return add_stmt (stmt);
8163 }
8164
8165 /* Helper function for finish_omp_for. Convert Ith random access iterator
8166 into integral iterator. Return FALSE if successful. */
8167
8168 static bool
8169 handle_omp_for_class_iterator (int i, location_t locus, enum tree_code code,
8170 tree declv, tree orig_declv, tree initv,
8171 tree condv, tree incrv, tree *body,
8172 tree *pre_body, tree &clauses,
8173 int collapse, int ordered)
8174 {
8175 tree diff, iter_init, iter_incr = NULL, last;
8176 tree incr_var = NULL, orig_pre_body, orig_body, c;
8177 tree decl = TREE_VEC_ELT (declv, i);
8178 tree init = TREE_VEC_ELT (initv, i);
8179 tree cond = TREE_VEC_ELT (condv, i);
8180 tree incr = TREE_VEC_ELT (incrv, i);
8181 tree iter = decl;
8182 location_t elocus = locus;
8183
8184 if (init && EXPR_HAS_LOCATION (init))
8185 elocus = EXPR_LOCATION (init);
8186
8187 cond = cp_fully_fold (cond);
8188 switch (TREE_CODE (cond))
8189 {
8190 case GT_EXPR:
8191 case GE_EXPR:
8192 case LT_EXPR:
8193 case LE_EXPR:
8194 case NE_EXPR:
8195 if (TREE_OPERAND (cond, 1) == iter)
8196 cond = build2 (swap_tree_comparison (TREE_CODE (cond)),
8197 TREE_TYPE (cond), iter, TREE_OPERAND (cond, 0));
8198 if (TREE_OPERAND (cond, 0) != iter)
8199 cond = error_mark_node;
8200 else
8201 {
8202 tree tem = build_x_binary_op (EXPR_LOCATION (cond),
8203 TREE_CODE (cond),
8204 iter, ERROR_MARK,
8205 TREE_OPERAND (cond, 1), ERROR_MARK,
8206 NULL, tf_warning_or_error);
8207 if (error_operand_p (tem))
8208 return true;
8209 }
8210 break;
8211 default:
8212 cond = error_mark_node;
8213 break;
8214 }
8215 if (cond == error_mark_node)
8216 {
8217 error_at (elocus, "invalid controlling predicate");
8218 return true;
8219 }
8220 diff = build_x_binary_op (elocus, MINUS_EXPR, TREE_OPERAND (cond, 1),
8221 ERROR_MARK, iter, ERROR_MARK, NULL,
8222 tf_warning_or_error);
8223 diff = cp_fully_fold (diff);
8224 if (error_operand_p (diff))
8225 return true;
8226 if (TREE_CODE (TREE_TYPE (diff)) != INTEGER_TYPE)
8227 {
8228 error_at (elocus, "difference between %qE and %qD does not have integer type",
8229 TREE_OPERAND (cond, 1), iter);
8230 return true;
8231 }
8232 if (!c_omp_check_loop_iv_exprs (locus, orig_declv,
8233 TREE_VEC_ELT (declv, i), NULL_TREE,
8234 cond, cp_walk_subtrees))
8235 return true;
8236
8237 switch (TREE_CODE (incr))
8238 {
8239 case PREINCREMENT_EXPR:
8240 case PREDECREMENT_EXPR:
8241 case POSTINCREMENT_EXPR:
8242 case POSTDECREMENT_EXPR:
8243 if (TREE_OPERAND (incr, 0) != iter)
8244 {
8245 incr = error_mark_node;
8246 break;
8247 }
8248 iter_incr = build_x_unary_op (EXPR_LOCATION (incr),
8249 TREE_CODE (incr), iter,
8250 tf_warning_or_error);
8251 if (error_operand_p (iter_incr))
8252 return true;
8253 else if (TREE_CODE (incr) == PREINCREMENT_EXPR
8254 || TREE_CODE (incr) == POSTINCREMENT_EXPR)
8255 incr = integer_one_node;
8256 else
8257 incr = integer_minus_one_node;
8258 break;
8259 case MODIFY_EXPR:
8260 if (TREE_OPERAND (incr, 0) != iter)
8261 incr = error_mark_node;
8262 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
8263 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
8264 {
8265 tree rhs = TREE_OPERAND (incr, 1);
8266 if (TREE_OPERAND (rhs, 0) == iter)
8267 {
8268 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (rhs, 1)))
8269 != INTEGER_TYPE)
8270 incr = error_mark_node;
8271 else
8272 {
8273 iter_incr = build_x_modify_expr (EXPR_LOCATION (rhs),
8274 iter, TREE_CODE (rhs),
8275 TREE_OPERAND (rhs, 1),
8276 tf_warning_or_error);
8277 if (error_operand_p (iter_incr))
8278 return true;
8279 incr = TREE_OPERAND (rhs, 1);
8280 incr = cp_convert (TREE_TYPE (diff), incr,
8281 tf_warning_or_error);
8282 if (TREE_CODE (rhs) == MINUS_EXPR)
8283 {
8284 incr = build1 (NEGATE_EXPR, TREE_TYPE (diff), incr);
8285 incr = fold_simple (incr);
8286 }
8287 if (TREE_CODE (incr) != INTEGER_CST
8288 && (TREE_CODE (incr) != NOP_EXPR
8289 || (TREE_CODE (TREE_OPERAND (incr, 0))
8290 != INTEGER_CST)))
8291 iter_incr = NULL;
8292 }
8293 }
8294 else if (TREE_OPERAND (rhs, 1) == iter)
8295 {
8296 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (rhs, 0))) != INTEGER_TYPE
8297 || TREE_CODE (rhs) != PLUS_EXPR)
8298 incr = error_mark_node;
8299 else
8300 {
8301 iter_incr = build_x_binary_op (EXPR_LOCATION (rhs),
8302 PLUS_EXPR,
8303 TREE_OPERAND (rhs, 0),
8304 ERROR_MARK, iter,
8305 ERROR_MARK, NULL,
8306 tf_warning_or_error);
8307 if (error_operand_p (iter_incr))
8308 return true;
8309 iter_incr = build_x_modify_expr (EXPR_LOCATION (rhs),
8310 iter, NOP_EXPR,
8311 iter_incr,
8312 tf_warning_or_error);
8313 if (error_operand_p (iter_incr))
8314 return true;
8315 incr = TREE_OPERAND (rhs, 0);
8316 iter_incr = NULL;
8317 }
8318 }
8319 else
8320 incr = error_mark_node;
8321 }
8322 else
8323 incr = error_mark_node;
8324 break;
8325 default:
8326 incr = error_mark_node;
8327 break;
8328 }
8329
8330 if (incr == error_mark_node)
8331 {
8332 error_at (elocus, "invalid increment expression");
8333 return true;
8334 }
8335
8336 incr = cp_convert (TREE_TYPE (diff), incr, tf_warning_or_error);
8337 incr = cp_fully_fold (incr);
8338 bool taskloop_iv_seen = false;
8339 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
8340 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
8341 && OMP_CLAUSE_DECL (c) == iter)
8342 {
8343 if (code == OMP_TASKLOOP)
8344 {
8345 taskloop_iv_seen = true;
8346 OMP_CLAUSE_LASTPRIVATE_TASKLOOP_IV (c) = 1;
8347 }
8348 break;
8349 }
8350 else if (code == OMP_TASKLOOP
8351 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
8352 && OMP_CLAUSE_DECL (c) == iter)
8353 {
8354 taskloop_iv_seen = true;
8355 OMP_CLAUSE_PRIVATE_TASKLOOP_IV (c) = 1;
8356 }
8357
8358 decl = create_temporary_var (TREE_TYPE (diff));
8359 pushdecl (decl);
8360 add_decl_expr (decl);
8361 last = create_temporary_var (TREE_TYPE (diff));
8362 pushdecl (last);
8363 add_decl_expr (last);
8364 if (c && iter_incr == NULL && TREE_CODE (incr) != INTEGER_CST
8365 && (!ordered || (i < collapse && collapse > 1)))
8366 {
8367 incr_var = create_temporary_var (TREE_TYPE (diff));
8368 pushdecl (incr_var);
8369 add_decl_expr (incr_var);
8370 }
8371 gcc_assert (stmts_are_full_exprs_p ());
8372 tree diffvar = NULL_TREE;
8373 if (code == OMP_TASKLOOP)
8374 {
8375 if (!taskloop_iv_seen)
8376 {
8377 tree ivc = build_omp_clause (locus, OMP_CLAUSE_FIRSTPRIVATE);
8378 OMP_CLAUSE_DECL (ivc) = iter;
8379 cxx_omp_finish_clause (ivc, NULL);
8380 OMP_CLAUSE_CHAIN (ivc) = clauses;
8381 clauses = ivc;
8382 }
8383 tree lvc = build_omp_clause (locus, OMP_CLAUSE_FIRSTPRIVATE);
8384 OMP_CLAUSE_DECL (lvc) = last;
8385 OMP_CLAUSE_CHAIN (lvc) = clauses;
8386 clauses = lvc;
8387 diffvar = create_temporary_var (TREE_TYPE (diff));
8388 pushdecl (diffvar);
8389 add_decl_expr (diffvar);
8390 }
8391
8392 orig_pre_body = *pre_body;
8393 *pre_body = push_stmt_list ();
8394 if (orig_pre_body)
8395 add_stmt (orig_pre_body);
8396 if (init != NULL)
8397 finish_expr_stmt (build_x_modify_expr (elocus,
8398 iter, NOP_EXPR, init,
8399 tf_warning_or_error));
8400 init = build_int_cst (TREE_TYPE (diff), 0);
8401 if (c && iter_incr == NULL
8402 && (!ordered || (i < collapse && collapse > 1)))
8403 {
8404 if (incr_var)
8405 {
8406 finish_expr_stmt (build_x_modify_expr (elocus,
8407 incr_var, NOP_EXPR,
8408 incr, tf_warning_or_error));
8409 incr = incr_var;
8410 }
8411 iter_incr = build_x_modify_expr (elocus,
8412 iter, PLUS_EXPR, incr,
8413 tf_warning_or_error);
8414 }
8415 if (c && ordered && i < collapse && collapse > 1)
8416 iter_incr = incr;
8417 finish_expr_stmt (build_x_modify_expr (elocus,
8418 last, NOP_EXPR, init,
8419 tf_warning_or_error));
8420 if (diffvar)
8421 {
8422 finish_expr_stmt (build_x_modify_expr (elocus,
8423 diffvar, NOP_EXPR,
8424 diff, tf_warning_or_error));
8425 diff = diffvar;
8426 }
8427 *pre_body = pop_stmt_list (*pre_body);
8428
8429 cond = cp_build_binary_op (elocus,
8430 TREE_CODE (cond), decl, diff,
8431 tf_warning_or_error);
8432 incr = build_modify_expr (elocus, decl, NULL_TREE, PLUS_EXPR,
8433 elocus, incr, NULL_TREE);
8434
8435 orig_body = *body;
8436 *body = push_stmt_list ();
8437 iter_init = build2 (MINUS_EXPR, TREE_TYPE (diff), decl, last);
8438 iter_init = build_x_modify_expr (elocus,
8439 iter, PLUS_EXPR, iter_init,
8440 tf_warning_or_error);
8441 if (iter_init != error_mark_node)
8442 iter_init = build1 (NOP_EXPR, void_type_node, iter_init);
8443 finish_expr_stmt (iter_init);
8444 finish_expr_stmt (build_x_modify_expr (elocus,
8445 last, NOP_EXPR, decl,
8446 tf_warning_or_error));
8447 add_stmt (orig_body);
8448 *body = pop_stmt_list (*body);
8449
8450 if (c)
8451 {
8452 OMP_CLAUSE_LASTPRIVATE_STMT (c) = push_stmt_list ();
8453 if (!ordered)
8454 finish_expr_stmt (iter_incr);
8455 else
8456 {
8457 iter_init = decl;
8458 if (i < collapse && collapse > 1 && !error_operand_p (iter_incr))
8459 iter_init = build2 (PLUS_EXPR, TREE_TYPE (diff),
8460 iter_init, iter_incr);
8461 iter_init = build2 (MINUS_EXPR, TREE_TYPE (diff), iter_init, last);
8462 iter_init = build_x_modify_expr (elocus,
8463 iter, PLUS_EXPR, iter_init,
8464 tf_warning_or_error);
8465 if (iter_init != error_mark_node)
8466 iter_init = build1 (NOP_EXPR, void_type_node, iter_init);
8467 finish_expr_stmt (iter_init);
8468 }
8469 OMP_CLAUSE_LASTPRIVATE_STMT (c)
8470 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (c));
8471 }
8472
8473 if (TREE_CODE (TREE_VEC_ELT (orig_declv, i)) == TREE_LIST)
8474 {
8475 tree t = TREE_VEC_ELT (orig_declv, i);
8476 gcc_assert (TREE_PURPOSE (t) == NULL_TREE
8477 && TREE_VALUE (t) == NULL_TREE
8478 && TREE_CODE (TREE_CHAIN (t)) == TREE_VEC);
8479 TREE_PURPOSE (t) = TREE_VEC_ELT (declv, i);
8480 TREE_VALUE (t) = last;
8481 }
8482 else
8483 TREE_VEC_ELT (orig_declv, i)
8484 = tree_cons (TREE_VEC_ELT (declv, i), last, NULL_TREE);
8485 TREE_VEC_ELT (declv, i) = decl;
8486 TREE_VEC_ELT (initv, i) = init;
8487 TREE_VEC_ELT (condv, i) = cond;
8488 TREE_VEC_ELT (incrv, i) = incr;
8489
8490 return false;
8491 }
8492
8493 /* Build and validate an OMP_FOR statement. CLAUSES, BODY, COND, INCR
8494 are directly for their associated operands in the statement. DECL
8495 and INIT are a combo; if DECL is NULL then INIT ought to be a
8496 MODIFY_EXPR, and the DECL should be extracted. PRE_BODY are
8497 optional statements that need to go before the loop into its
8498 sk_omp scope. */
8499
8500 tree
8501 finish_omp_for (location_t locus, enum tree_code code, tree declv,
8502 tree orig_declv, tree initv, tree condv, tree incrv,
8503 tree body, tree pre_body, vec<tree> *orig_inits, tree clauses)
8504 {
8505 tree omp_for = NULL, orig_incr = NULL;
8506 tree decl = NULL, init, cond, incr;
8507 location_t elocus;
8508 int i;
8509 int collapse = 1;
8510 int ordered = 0;
8511
8512 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (initv));
8513 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (condv));
8514 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (incrv));
8515 if (TREE_VEC_LENGTH (declv) > 1)
8516 {
8517 tree c;
8518
8519 c = omp_find_clause (clauses, OMP_CLAUSE_TILE);
8520 if (c)
8521 collapse = list_length (OMP_CLAUSE_TILE_LIST (c));
8522 else
8523 {
8524 c = omp_find_clause (clauses, OMP_CLAUSE_COLLAPSE);
8525 if (c)
8526 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (c));
8527 if (collapse != TREE_VEC_LENGTH (declv))
8528 ordered = TREE_VEC_LENGTH (declv);
8529 }
8530 }
8531 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
8532 {
8533 decl = TREE_VEC_ELT (declv, i);
8534 init = TREE_VEC_ELT (initv, i);
8535 cond = TREE_VEC_ELT (condv, i);
8536 incr = TREE_VEC_ELT (incrv, i);
8537 elocus = locus;
8538
8539 if (decl == NULL)
8540 {
8541 if (init != NULL)
8542 switch (TREE_CODE (init))
8543 {
8544 case MODIFY_EXPR:
8545 decl = TREE_OPERAND (init, 0);
8546 init = TREE_OPERAND (init, 1);
8547 break;
8548 case MODOP_EXPR:
8549 if (TREE_CODE (TREE_OPERAND (init, 1)) == NOP_EXPR)
8550 {
8551 decl = TREE_OPERAND (init, 0);
8552 init = TREE_OPERAND (init, 2);
8553 }
8554 break;
8555 default:
8556 break;
8557 }
8558
8559 if (decl == NULL)
8560 {
8561 error_at (locus,
8562 "expected iteration declaration or initialization");
8563 return NULL;
8564 }
8565 }
8566
8567 if (init && EXPR_HAS_LOCATION (init))
8568 elocus = EXPR_LOCATION (init);
8569
8570 if (cond == global_namespace)
8571 continue;
8572
8573 if (cond == NULL)
8574 {
8575 error_at (elocus, "missing controlling predicate");
8576 return NULL;
8577 }
8578
8579 if (incr == NULL)
8580 {
8581 error_at (elocus, "missing increment expression");
8582 return NULL;
8583 }
8584
8585 TREE_VEC_ELT (declv, i) = decl;
8586 TREE_VEC_ELT (initv, i) = init;
8587 }
8588
8589 if (orig_inits)
8590 {
8591 bool fail = false;
8592 tree orig_init;
8593 FOR_EACH_VEC_ELT (*orig_inits, i, orig_init)
8594 if (orig_init
8595 && !c_omp_check_loop_iv_exprs (locus, orig_declv
8596 ? orig_declv : declv,
8597 TREE_VEC_ELT (declv, i), orig_init,
8598 NULL_TREE, cp_walk_subtrees))
8599 fail = true;
8600 if (fail)
8601 return NULL;
8602 }
8603
8604 if (dependent_omp_for_p (declv, initv, condv, incrv))
8605 {
8606 tree stmt;
8607
8608 stmt = make_node (code);
8609
8610 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
8611 {
8612 /* This is really just a place-holder. We'll be decomposing this
8613 again and going through the cp_build_modify_expr path below when
8614 we instantiate the thing. */
8615 TREE_VEC_ELT (initv, i)
8616 = build2 (MODIFY_EXPR, void_type_node, TREE_VEC_ELT (declv, i),
8617 TREE_VEC_ELT (initv, i));
8618 }
8619
8620 TREE_TYPE (stmt) = void_type_node;
8621 OMP_FOR_INIT (stmt) = initv;
8622 OMP_FOR_COND (stmt) = condv;
8623 OMP_FOR_INCR (stmt) = incrv;
8624 OMP_FOR_BODY (stmt) = body;
8625 OMP_FOR_PRE_BODY (stmt) = pre_body;
8626 OMP_FOR_CLAUSES (stmt) = clauses;
8627
8628 SET_EXPR_LOCATION (stmt, locus);
8629 return add_stmt (stmt);
8630 }
8631
8632 if (!orig_declv)
8633 orig_declv = copy_node (declv);
8634
8635 if (processing_template_decl)
8636 orig_incr = make_tree_vec (TREE_VEC_LENGTH (incrv));
8637
8638 for (i = 0; i < TREE_VEC_LENGTH (declv); )
8639 {
8640 decl = TREE_VEC_ELT (declv, i);
8641 init = TREE_VEC_ELT (initv, i);
8642 cond = TREE_VEC_ELT (condv, i);
8643 incr = TREE_VEC_ELT (incrv, i);
8644 if (orig_incr)
8645 TREE_VEC_ELT (orig_incr, i) = incr;
8646 elocus = locus;
8647
8648 if (init && EXPR_HAS_LOCATION (init))
8649 elocus = EXPR_LOCATION (init);
8650
8651 if (!DECL_P (decl))
8652 {
8653 error_at (elocus, "expected iteration declaration or initialization");
8654 return NULL;
8655 }
8656
8657 if (incr && TREE_CODE (incr) == MODOP_EXPR)
8658 {
8659 if (orig_incr)
8660 TREE_VEC_ELT (orig_incr, i) = incr;
8661 incr = cp_build_modify_expr (elocus, TREE_OPERAND (incr, 0),
8662 TREE_CODE (TREE_OPERAND (incr, 1)),
8663 TREE_OPERAND (incr, 2),
8664 tf_warning_or_error);
8665 }
8666
8667 if (CLASS_TYPE_P (TREE_TYPE (decl)))
8668 {
8669 if (code == OMP_SIMD)
8670 {
8671 error_at (elocus, "%<#pragma omp simd%> used with class "
8672 "iteration variable %qE", decl);
8673 return NULL;
8674 }
8675 if (handle_omp_for_class_iterator (i, locus, code, declv, orig_declv,
8676 initv, condv, incrv, &body,
8677 &pre_body, clauses,
8678 collapse, ordered))
8679 return NULL;
8680 continue;
8681 }
8682
8683 if (!INTEGRAL_TYPE_P (TREE_TYPE (decl))
8684 && !TYPE_PTR_P (TREE_TYPE (decl)))
8685 {
8686 error_at (elocus, "invalid type for iteration variable %qE", decl);
8687 return NULL;
8688 }
8689
8690 if (!processing_template_decl)
8691 {
8692 init = fold_build_cleanup_point_expr (TREE_TYPE (init), init);
8693 init = cp_build_modify_expr (elocus, decl, NOP_EXPR, init,
8694 tf_warning_or_error);
8695 }
8696 else
8697 init = build2 (MODIFY_EXPR, void_type_node, decl, init);
8698 if (cond
8699 && TREE_SIDE_EFFECTS (cond)
8700 && COMPARISON_CLASS_P (cond)
8701 && !processing_template_decl)
8702 {
8703 tree t = TREE_OPERAND (cond, 0);
8704 if (TREE_SIDE_EFFECTS (t)
8705 && t != decl
8706 && (TREE_CODE (t) != NOP_EXPR
8707 || TREE_OPERAND (t, 0) != decl))
8708 TREE_OPERAND (cond, 0)
8709 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
8710
8711 t = TREE_OPERAND (cond, 1);
8712 if (TREE_SIDE_EFFECTS (t)
8713 && t != decl
8714 && (TREE_CODE (t) != NOP_EXPR
8715 || TREE_OPERAND (t, 0) != decl))
8716 TREE_OPERAND (cond, 1)
8717 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
8718 }
8719 if (decl == error_mark_node || init == error_mark_node)
8720 return NULL;
8721
8722 TREE_VEC_ELT (declv, i) = decl;
8723 TREE_VEC_ELT (initv, i) = init;
8724 TREE_VEC_ELT (condv, i) = cond;
8725 TREE_VEC_ELT (incrv, i) = incr;
8726 i++;
8727 }
8728
8729 if (pre_body && IS_EMPTY_STMT (pre_body))
8730 pre_body = NULL;
8731
8732 omp_for = c_finish_omp_for (locus, code, declv, orig_declv, initv, condv,
8733 incrv, body, pre_body,
8734 !processing_template_decl);
8735
8736 /* Check for iterators appearing in lb, b or incr expressions. */
8737 if (omp_for && !c_omp_check_loop_iv (omp_for, orig_declv, cp_walk_subtrees))
8738 omp_for = NULL_TREE;
8739
8740 if (omp_for == NULL)
8741 {
8742 return NULL;
8743 }
8744
8745 add_stmt (omp_for);
8746
8747 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INCR (omp_for)); i++)
8748 {
8749 decl = TREE_OPERAND (TREE_VEC_ELT (OMP_FOR_INIT (omp_for), i), 0);
8750 incr = TREE_VEC_ELT (OMP_FOR_INCR (omp_for), i);
8751
8752 if (TREE_CODE (incr) != MODIFY_EXPR)
8753 continue;
8754
8755 if (TREE_SIDE_EFFECTS (TREE_OPERAND (incr, 1))
8756 && BINARY_CLASS_P (TREE_OPERAND (incr, 1))
8757 && !processing_template_decl)
8758 {
8759 tree t = TREE_OPERAND (TREE_OPERAND (incr, 1), 0);
8760 if (TREE_SIDE_EFFECTS (t)
8761 && t != decl
8762 && (TREE_CODE (t) != NOP_EXPR
8763 || TREE_OPERAND (t, 0) != decl))
8764 TREE_OPERAND (TREE_OPERAND (incr, 1), 0)
8765 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
8766
8767 t = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
8768 if (TREE_SIDE_EFFECTS (t)
8769 && t != decl
8770 && (TREE_CODE (t) != NOP_EXPR
8771 || TREE_OPERAND (t, 0) != decl))
8772 TREE_OPERAND (TREE_OPERAND (incr, 1), 1)
8773 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
8774 }
8775
8776 if (orig_incr)
8777 TREE_VEC_ELT (OMP_FOR_INCR (omp_for), i) = TREE_VEC_ELT (orig_incr, i);
8778 }
8779 OMP_FOR_CLAUSES (omp_for) = clauses;
8780
8781 /* For simd loops with non-static data member iterators, we could have added
8782 OMP_CLAUSE_LINEAR clauses without OMP_CLAUSE_LINEAR_STEP. As we know the
8783 step at this point, fill it in. */
8784 if (code == OMP_SIMD && !processing_template_decl
8785 && TREE_VEC_LENGTH (OMP_FOR_INCR (omp_for)) == 1)
8786 for (tree c = omp_find_clause (clauses, OMP_CLAUSE_LINEAR); c;
8787 c = omp_find_clause (OMP_CLAUSE_CHAIN (c), OMP_CLAUSE_LINEAR))
8788 if (OMP_CLAUSE_LINEAR_STEP (c) == NULL_TREE)
8789 {
8790 decl = TREE_OPERAND (TREE_VEC_ELT (OMP_FOR_INIT (omp_for), 0), 0);
8791 gcc_assert (decl == OMP_CLAUSE_DECL (c));
8792 incr = TREE_VEC_ELT (OMP_FOR_INCR (omp_for), 0);
8793 tree step, stept;
8794 switch (TREE_CODE (incr))
8795 {
8796 case PREINCREMENT_EXPR:
8797 case POSTINCREMENT_EXPR:
8798 /* c_omp_for_incr_canonicalize_ptr() should have been
8799 called to massage things appropriately. */
8800 gcc_assert (!INDIRECT_TYPE_P (TREE_TYPE (decl)));
8801 OMP_CLAUSE_LINEAR_STEP (c) = build_int_cst (TREE_TYPE (decl), 1);
8802 break;
8803 case PREDECREMENT_EXPR:
8804 case POSTDECREMENT_EXPR:
8805 /* c_omp_for_incr_canonicalize_ptr() should have been
8806 called to massage things appropriately. */
8807 gcc_assert (!INDIRECT_TYPE_P (TREE_TYPE (decl)));
8808 OMP_CLAUSE_LINEAR_STEP (c)
8809 = build_int_cst (TREE_TYPE (decl), -1);
8810 break;
8811 case MODIFY_EXPR:
8812 gcc_assert (TREE_OPERAND (incr, 0) == decl);
8813 incr = TREE_OPERAND (incr, 1);
8814 switch (TREE_CODE (incr))
8815 {
8816 case PLUS_EXPR:
8817 if (TREE_OPERAND (incr, 1) == decl)
8818 step = TREE_OPERAND (incr, 0);
8819 else
8820 step = TREE_OPERAND (incr, 1);
8821 break;
8822 case MINUS_EXPR:
8823 case POINTER_PLUS_EXPR:
8824 gcc_assert (TREE_OPERAND (incr, 0) == decl);
8825 step = TREE_OPERAND (incr, 1);
8826 break;
8827 default:
8828 gcc_unreachable ();
8829 }
8830 stept = TREE_TYPE (decl);
8831 if (INDIRECT_TYPE_P (stept))
8832 stept = sizetype;
8833 step = fold_convert (stept, step);
8834 if (TREE_CODE (incr) == MINUS_EXPR)
8835 step = fold_build1 (NEGATE_EXPR, stept, step);
8836 OMP_CLAUSE_LINEAR_STEP (c) = step;
8837 break;
8838 default:
8839 gcc_unreachable ();
8840 }
8841 }
8842
8843 return omp_for;
8844 }
8845
8846 /* Fix up range for decls. Those decls were pushed into BIND's BIND_EXPR_VARS
8847 and need to be moved into the BIND_EXPR inside of the OMP_FOR's body. */
8848
8849 tree
8850 finish_omp_for_block (tree bind, tree omp_for)
8851 {
8852 if (omp_for == NULL_TREE
8853 || !OMP_FOR_ORIG_DECLS (omp_for)
8854 || bind == NULL_TREE
8855 || TREE_CODE (bind) != BIND_EXPR)
8856 return bind;
8857 tree b = NULL_TREE;
8858 for (int i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (omp_for)); i++)
8859 if (TREE_CODE (TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (omp_for), i)) == TREE_LIST
8860 && TREE_CHAIN (TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (omp_for), i)))
8861 {
8862 tree v = TREE_CHAIN (TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (omp_for), i));
8863 gcc_assert (BIND_EXPR_BLOCK (bind)
8864 && (BIND_EXPR_VARS (bind)
8865 == BLOCK_VARS (BIND_EXPR_BLOCK (bind))));
8866 for (int j = 2; j < TREE_VEC_LENGTH (v); j++)
8867 for (tree *p = &BIND_EXPR_VARS (bind); *p; p = &DECL_CHAIN (*p))
8868 {
8869 if (*p == TREE_VEC_ELT (v, j))
8870 {
8871 tree var = *p;
8872 *p = DECL_CHAIN (*p);
8873 if (b == NULL_TREE)
8874 {
8875 b = make_node (BLOCK);
8876 b = build3 (BIND_EXPR, void_type_node, NULL_TREE,
8877 OMP_FOR_BODY (omp_for), b);
8878 TREE_SIDE_EFFECTS (b) = 1;
8879 OMP_FOR_BODY (omp_for) = b;
8880 }
8881 DECL_CHAIN (var) = BIND_EXPR_VARS (b);
8882 BIND_EXPR_VARS (b) = var;
8883 BLOCK_VARS (BIND_EXPR_BLOCK (b)) = var;
8884 }
8885 }
8886 BLOCK_VARS (BIND_EXPR_BLOCK (bind)) = BIND_EXPR_VARS (bind);
8887 }
8888 return bind;
8889 }
8890
8891 void
8892 finish_omp_atomic (location_t loc, enum tree_code code, enum tree_code opcode,
8893 tree lhs, tree rhs, tree v, tree lhs1, tree rhs1,
8894 tree clauses, enum omp_memory_order mo)
8895 {
8896 tree orig_lhs;
8897 tree orig_rhs;
8898 tree orig_v;
8899 tree orig_lhs1;
8900 tree orig_rhs1;
8901 bool dependent_p;
8902 tree stmt;
8903
8904 orig_lhs = lhs;
8905 orig_rhs = rhs;
8906 orig_v = v;
8907 orig_lhs1 = lhs1;
8908 orig_rhs1 = rhs1;
8909 dependent_p = false;
8910 stmt = NULL_TREE;
8911
8912 /* Even in a template, we can detect invalid uses of the atomic
8913 pragma if neither LHS nor RHS is type-dependent. */
8914 if (processing_template_decl)
8915 {
8916 dependent_p = (type_dependent_expression_p (lhs)
8917 || (rhs && type_dependent_expression_p (rhs))
8918 || (v && type_dependent_expression_p (v))
8919 || (lhs1 && type_dependent_expression_p (lhs1))
8920 || (rhs1 && type_dependent_expression_p (rhs1)));
8921 if (clauses)
8922 {
8923 gcc_assert (TREE_CODE (clauses) == OMP_CLAUSE
8924 && OMP_CLAUSE_CODE (clauses) == OMP_CLAUSE_HINT
8925 && OMP_CLAUSE_CHAIN (clauses) == NULL_TREE);
8926 if (type_dependent_expression_p (OMP_CLAUSE_HINT_EXPR (clauses))
8927 || TREE_CODE (OMP_CLAUSE_HINT_EXPR (clauses)) != INTEGER_CST)
8928 dependent_p = true;
8929 }
8930 if (!dependent_p)
8931 {
8932 lhs = build_non_dependent_expr (lhs);
8933 if (rhs)
8934 rhs = build_non_dependent_expr (rhs);
8935 if (v)
8936 v = build_non_dependent_expr (v);
8937 if (lhs1)
8938 lhs1 = build_non_dependent_expr (lhs1);
8939 if (rhs1)
8940 rhs1 = build_non_dependent_expr (rhs1);
8941 }
8942 }
8943 if (!dependent_p)
8944 {
8945 bool swapped = false;
8946 if (rhs1 && cp_tree_equal (lhs, rhs))
8947 {
8948 std::swap (rhs, rhs1);
8949 swapped = !commutative_tree_code (opcode);
8950 }
8951 if (rhs1 && !cp_tree_equal (lhs, rhs1))
8952 {
8953 if (code == OMP_ATOMIC)
8954 error ("%<#pragma omp atomic update%> uses two different "
8955 "expressions for memory");
8956 else
8957 error ("%<#pragma omp atomic capture%> uses two different "
8958 "expressions for memory");
8959 return;
8960 }
8961 if (lhs1 && !cp_tree_equal (lhs, lhs1))
8962 {
8963 if (code == OMP_ATOMIC)
8964 error ("%<#pragma omp atomic update%> uses two different "
8965 "expressions for memory");
8966 else
8967 error ("%<#pragma omp atomic capture%> uses two different "
8968 "expressions for memory");
8969 return;
8970 }
8971 stmt = c_finish_omp_atomic (loc, code, opcode, lhs, rhs,
8972 v, lhs1, rhs1, swapped, mo,
8973 processing_template_decl != 0);
8974 if (stmt == error_mark_node)
8975 return;
8976 }
8977 if (processing_template_decl)
8978 {
8979 if (code == OMP_ATOMIC_READ)
8980 {
8981 stmt = build_min_nt_loc (loc, OMP_ATOMIC_READ, orig_lhs);
8982 OMP_ATOMIC_MEMORY_ORDER (stmt) = mo;
8983 stmt = build2 (MODIFY_EXPR, void_type_node, orig_v, stmt);
8984 }
8985 else
8986 {
8987 if (opcode == NOP_EXPR)
8988 stmt = build2 (MODIFY_EXPR, void_type_node, orig_lhs, orig_rhs);
8989 else
8990 stmt = build2 (opcode, void_type_node, orig_lhs, orig_rhs);
8991 if (orig_rhs1)
8992 stmt = build_min_nt_loc (EXPR_LOCATION (orig_rhs1),
8993 COMPOUND_EXPR, orig_rhs1, stmt);
8994 if (code != OMP_ATOMIC)
8995 {
8996 stmt = build_min_nt_loc (loc, code, orig_lhs1, stmt);
8997 OMP_ATOMIC_MEMORY_ORDER (stmt) = mo;
8998 stmt = build2 (MODIFY_EXPR, void_type_node, orig_v, stmt);
8999 }
9000 }
9001 stmt = build2 (OMP_ATOMIC, void_type_node,
9002 clauses ? clauses : integer_zero_node, stmt);
9003 OMP_ATOMIC_MEMORY_ORDER (stmt) = mo;
9004 SET_EXPR_LOCATION (stmt, loc);
9005 }
9006
9007 /* Avoid -Wunused-value warnings here, the whole construct has side-effects
9008 and even if it might be wrapped from fold-const.c or c-omp.c wrapped
9009 in some tree that appears to be unused, the value is not unused. */
9010 warning_sentinel w (warn_unused_value);
9011 finish_expr_stmt (stmt);
9012 }
9013
9014 void
9015 finish_omp_barrier (void)
9016 {
9017 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_BARRIER);
9018 vec<tree, va_gc> *vec = make_tree_vector ();
9019 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
9020 release_tree_vector (vec);
9021 finish_expr_stmt (stmt);
9022 }
9023
9024 void
9025 finish_omp_depobj (location_t loc, tree depobj,
9026 enum omp_clause_depend_kind kind, tree clause)
9027 {
9028 if (!error_operand_p (depobj) && !type_dependent_expression_p (depobj))
9029 {
9030 if (!lvalue_p (depobj))
9031 {
9032 error_at (EXPR_LOC_OR_LOC (depobj, loc),
9033 "%<depobj%> expression is not lvalue expression");
9034 depobj = error_mark_node;
9035 }
9036 }
9037
9038 if (processing_template_decl)
9039 {
9040 if (clause == NULL_TREE)
9041 clause = build_int_cst (integer_type_node, kind);
9042 add_stmt (build_min_nt_loc (loc, OMP_DEPOBJ, depobj, clause));
9043 return;
9044 }
9045
9046 if (!error_operand_p (depobj))
9047 {
9048 tree addr = cp_build_addr_expr (depobj, tf_warning_or_error);
9049 if (addr == error_mark_node)
9050 depobj = error_mark_node;
9051 else
9052 depobj = cp_build_indirect_ref (addr, RO_UNARY_STAR,
9053 tf_warning_or_error);
9054 }
9055
9056 c_finish_omp_depobj (loc, depobj, kind, clause);
9057 }
9058
9059 void
9060 finish_omp_flush (int mo)
9061 {
9062 tree fn = builtin_decl_explicit (BUILT_IN_SYNC_SYNCHRONIZE);
9063 vec<tree, va_gc> *vec = make_tree_vector ();
9064 if (mo != MEMMODEL_LAST)
9065 {
9066 fn = builtin_decl_explicit (BUILT_IN_ATOMIC_THREAD_FENCE);
9067 vec->quick_push (build_int_cst (integer_type_node, mo));
9068 }
9069 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
9070 release_tree_vector (vec);
9071 finish_expr_stmt (stmt);
9072 }
9073
9074 void
9075 finish_omp_taskwait (void)
9076 {
9077 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_TASKWAIT);
9078 vec<tree, va_gc> *vec = make_tree_vector ();
9079 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
9080 release_tree_vector (vec);
9081 finish_expr_stmt (stmt);
9082 }
9083
9084 void
9085 finish_omp_taskyield (void)
9086 {
9087 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_TASKYIELD);
9088 vec<tree, va_gc> *vec = make_tree_vector ();
9089 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
9090 release_tree_vector (vec);
9091 finish_expr_stmt (stmt);
9092 }
9093
9094 void
9095 finish_omp_cancel (tree clauses)
9096 {
9097 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
9098 int mask = 0;
9099 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
9100 mask = 1;
9101 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
9102 mask = 2;
9103 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
9104 mask = 4;
9105 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
9106 mask = 8;
9107 else
9108 {
9109 error ("%<#pragma omp cancel%> must specify one of "
9110 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> clauses");
9111 return;
9112 }
9113 vec<tree, va_gc> *vec = make_tree_vector ();
9114 tree ifc = omp_find_clause (clauses, OMP_CLAUSE_IF);
9115 if (ifc != NULL_TREE)
9116 {
9117 if (OMP_CLAUSE_IF_MODIFIER (ifc) != ERROR_MARK
9118 && OMP_CLAUSE_IF_MODIFIER (ifc) != VOID_CST)
9119 error_at (OMP_CLAUSE_LOCATION (ifc),
9120 "expected %<cancel%> %<if%> clause modifier");
9121 else
9122 {
9123 tree ifc2 = omp_find_clause (OMP_CLAUSE_CHAIN (ifc), OMP_CLAUSE_IF);
9124 if (ifc2 != NULL_TREE)
9125 {
9126 gcc_assert (OMP_CLAUSE_IF_MODIFIER (ifc) == VOID_CST
9127 && OMP_CLAUSE_IF_MODIFIER (ifc2) != ERROR_MARK
9128 && OMP_CLAUSE_IF_MODIFIER (ifc2) != VOID_CST);
9129 error_at (OMP_CLAUSE_LOCATION (ifc2),
9130 "expected %<cancel%> %<if%> clause modifier");
9131 }
9132 }
9133
9134 if (!processing_template_decl)
9135 ifc = maybe_convert_cond (OMP_CLAUSE_IF_EXPR (ifc));
9136 else
9137 ifc = build_x_binary_op (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
9138 OMP_CLAUSE_IF_EXPR (ifc), ERROR_MARK,
9139 integer_zero_node, ERROR_MARK,
9140 NULL, tf_warning_or_error);
9141 }
9142 else
9143 ifc = boolean_true_node;
9144 vec->quick_push (build_int_cst (integer_type_node, mask));
9145 vec->quick_push (ifc);
9146 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
9147 release_tree_vector (vec);
9148 finish_expr_stmt (stmt);
9149 }
9150
9151 void
9152 finish_omp_cancellation_point (tree clauses)
9153 {
9154 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
9155 int mask = 0;
9156 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
9157 mask = 1;
9158 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
9159 mask = 2;
9160 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
9161 mask = 4;
9162 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
9163 mask = 8;
9164 else
9165 {
9166 error ("%<#pragma omp cancellation point%> must specify one of "
9167 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> clauses");
9168 return;
9169 }
9170 vec<tree, va_gc> *vec
9171 = make_tree_vector_single (build_int_cst (integer_type_node, mask));
9172 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
9173 release_tree_vector (vec);
9174 finish_expr_stmt (stmt);
9175 }
9176 \f
9177 /* Begin a __transaction_atomic or __transaction_relaxed statement.
9178 If PCOMPOUND is non-null, this is for a function-transaction-block, and we
9179 should create an extra compound stmt. */
9180
9181 tree
9182 begin_transaction_stmt (location_t loc, tree *pcompound, int flags)
9183 {
9184 tree r;
9185
9186 if (pcompound)
9187 *pcompound = begin_compound_stmt (0);
9188
9189 r = build_stmt (loc, TRANSACTION_EXPR, NULL_TREE);
9190
9191 /* Only add the statement to the function if support enabled. */
9192 if (flag_tm)
9193 add_stmt (r);
9194 else
9195 error_at (loc, ((flags & TM_STMT_ATTR_RELAXED) != 0
9196 ? G_("%<__transaction_relaxed%> without "
9197 "transactional memory support enabled")
9198 : G_("%<__transaction_atomic%> without "
9199 "transactional memory support enabled")));
9200
9201 TRANSACTION_EXPR_BODY (r) = push_stmt_list ();
9202 TREE_SIDE_EFFECTS (r) = 1;
9203 return r;
9204 }
9205
9206 /* End a __transaction_atomic or __transaction_relaxed statement.
9207 If COMPOUND_STMT is non-null, this is for a function-transaction-block,
9208 and we should end the compound. If NOEX is non-NULL, we wrap the body in
9209 a MUST_NOT_THROW_EXPR with NOEX as condition. */
9210
9211 void
9212 finish_transaction_stmt (tree stmt, tree compound_stmt, int flags, tree noex)
9213 {
9214 TRANSACTION_EXPR_BODY (stmt) = pop_stmt_list (TRANSACTION_EXPR_BODY (stmt));
9215 TRANSACTION_EXPR_OUTER (stmt) = (flags & TM_STMT_ATTR_OUTER) != 0;
9216 TRANSACTION_EXPR_RELAXED (stmt) = (flags & TM_STMT_ATTR_RELAXED) != 0;
9217 TRANSACTION_EXPR_IS_STMT (stmt) = 1;
9218
9219 /* noexcept specifications are not allowed for function transactions. */
9220 gcc_assert (!(noex && compound_stmt));
9221 if (noex)
9222 {
9223 tree body = build_must_not_throw_expr (TRANSACTION_EXPR_BODY (stmt),
9224 noex);
9225 protected_set_expr_location
9226 (body, EXPR_LOCATION (TRANSACTION_EXPR_BODY (stmt)));
9227 TREE_SIDE_EFFECTS (body) = 1;
9228 TRANSACTION_EXPR_BODY (stmt) = body;
9229 }
9230
9231 if (compound_stmt)
9232 finish_compound_stmt (compound_stmt);
9233 }
9234
9235 /* Build a __transaction_atomic or __transaction_relaxed expression. If
9236 NOEX is non-NULL, we wrap the body in a MUST_NOT_THROW_EXPR with NOEX as
9237 condition. */
9238
9239 tree
9240 build_transaction_expr (location_t loc, tree expr, int flags, tree noex)
9241 {
9242 tree ret;
9243 if (noex)
9244 {
9245 expr = build_must_not_throw_expr (expr, noex);
9246 protected_set_expr_location (expr, loc);
9247 TREE_SIDE_EFFECTS (expr) = 1;
9248 }
9249 ret = build1 (TRANSACTION_EXPR, TREE_TYPE (expr), expr);
9250 if (flags & TM_STMT_ATTR_RELAXED)
9251 TRANSACTION_EXPR_RELAXED (ret) = 1;
9252 TREE_SIDE_EFFECTS (ret) = 1;
9253 SET_EXPR_LOCATION (ret, loc);
9254 return ret;
9255 }
9256 \f
9257 void
9258 init_cp_semantics (void)
9259 {
9260 }
9261 \f
9262 /* Build a STATIC_ASSERT for a static assertion with the condition
9263 CONDITION and the message text MESSAGE. LOCATION is the location
9264 of the static assertion in the source code. When MEMBER_P, this
9265 static assertion is a member of a class. */
9266 void
9267 finish_static_assert (tree condition, tree message, location_t location,
9268 bool member_p)
9269 {
9270 tsubst_flags_t complain = tf_warning_or_error;
9271
9272 if (message == NULL_TREE
9273 || message == error_mark_node
9274 || condition == NULL_TREE
9275 || condition == error_mark_node)
9276 return;
9277
9278 if (check_for_bare_parameter_packs (condition))
9279 condition = error_mark_node;
9280
9281 if (instantiation_dependent_expression_p (condition))
9282 {
9283 /* We're in a template; build a STATIC_ASSERT and put it in
9284 the right place. */
9285 tree assertion;
9286
9287 assertion = make_node (STATIC_ASSERT);
9288 STATIC_ASSERT_CONDITION (assertion) = condition;
9289 STATIC_ASSERT_MESSAGE (assertion) = message;
9290 STATIC_ASSERT_SOURCE_LOCATION (assertion) = location;
9291
9292 if (member_p)
9293 maybe_add_class_template_decl_list (current_class_type,
9294 assertion,
9295 /*friend_p=*/0);
9296 else
9297 add_stmt (assertion);
9298
9299 return;
9300 }
9301
9302 /* Fold the expression and convert it to a boolean value. */
9303 condition = perform_implicit_conversion_flags (boolean_type_node, condition,
9304 complain, LOOKUP_NORMAL);
9305 condition = fold_non_dependent_expr (condition, complain,
9306 /*manifestly_const_eval=*/true);
9307
9308 if (TREE_CODE (condition) == INTEGER_CST && !integer_zerop (condition))
9309 /* Do nothing; the condition is satisfied. */
9310 ;
9311 else
9312 {
9313 location_t saved_loc = input_location;
9314
9315 input_location = location;
9316 if (TREE_CODE (condition) == INTEGER_CST
9317 && integer_zerop (condition))
9318 {
9319 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
9320 (TREE_TYPE (TREE_TYPE (message))));
9321 int len = TREE_STRING_LENGTH (message) / sz - 1;
9322 /* Report the error. */
9323 if (len == 0)
9324 error ("static assertion failed");
9325 else
9326 error ("static assertion failed: %s",
9327 TREE_STRING_POINTER (message));
9328 }
9329 else if (condition && condition != error_mark_node)
9330 {
9331 error ("non-constant condition for static assertion");
9332 if (require_rvalue_constant_expression (condition))
9333 cxx_constant_value (condition);
9334 }
9335 input_location = saved_loc;
9336 }
9337 }
9338 \f
9339 /* Implements the C++0x decltype keyword. Returns the type of EXPR,
9340 suitable for use as a type-specifier.
9341
9342 ID_EXPRESSION_OR_MEMBER_ACCESS_P is true when EXPR was parsed as an
9343 id-expression or a class member access, FALSE when it was parsed as
9344 a full expression. */
9345
9346 tree
9347 finish_decltype_type (tree expr, bool id_expression_or_member_access_p,
9348 tsubst_flags_t complain)
9349 {
9350 tree type = NULL_TREE;
9351
9352 if (!expr || error_operand_p (expr))
9353 return error_mark_node;
9354
9355 if (TYPE_P (expr)
9356 || TREE_CODE (expr) == TYPE_DECL
9357 || (TREE_CODE (expr) == BIT_NOT_EXPR
9358 && TYPE_P (TREE_OPERAND (expr, 0))))
9359 {
9360 if (complain & tf_error)
9361 error ("argument to decltype must be an expression");
9362 return error_mark_node;
9363 }
9364
9365 /* Depending on the resolution of DR 1172, we may later need to distinguish
9366 instantiation-dependent but not type-dependent expressions so that, say,
9367 A<decltype(sizeof(T))>::U doesn't require 'typename'. */
9368 if (instantiation_dependent_uneval_expression_p (expr))
9369 {
9370 type = cxx_make_type (DECLTYPE_TYPE);
9371 DECLTYPE_TYPE_EXPR (type) = expr;
9372 DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (type)
9373 = id_expression_or_member_access_p;
9374 SET_TYPE_STRUCTURAL_EQUALITY (type);
9375
9376 return type;
9377 }
9378
9379 /* The type denoted by decltype(e) is defined as follows: */
9380
9381 expr = resolve_nondeduced_context (expr, complain);
9382
9383 if (invalid_nonstatic_memfn_p (input_location, expr, complain))
9384 return error_mark_node;
9385
9386 if (type_unknown_p (expr))
9387 {
9388 if (complain & tf_error)
9389 error ("decltype cannot resolve address of overloaded function");
9390 return error_mark_node;
9391 }
9392
9393 /* To get the size of a static data member declared as an array of
9394 unknown bound, we need to instantiate it. */
9395 if (VAR_P (expr)
9396 && VAR_HAD_UNKNOWN_BOUND (expr)
9397 && DECL_TEMPLATE_INSTANTIATION (expr))
9398 instantiate_decl (expr, /*defer_ok*/true, /*expl_inst_mem*/false);
9399
9400 if (id_expression_or_member_access_p)
9401 {
9402 /* If e is an id-expression or a class member access (5.2.5
9403 [expr.ref]), decltype(e) is defined as the type of the entity
9404 named by e. If there is no such entity, or e names a set of
9405 overloaded functions, the program is ill-formed. */
9406 if (identifier_p (expr))
9407 expr = lookup_name (expr);
9408
9409 if (INDIRECT_REF_P (expr)
9410 || TREE_CODE (expr) == VIEW_CONVERT_EXPR)
9411 /* This can happen when the expression is, e.g., "a.b". Just
9412 look at the underlying operand. */
9413 expr = TREE_OPERAND (expr, 0);
9414
9415 if (TREE_CODE (expr) == OFFSET_REF
9416 || TREE_CODE (expr) == MEMBER_REF
9417 || TREE_CODE (expr) == SCOPE_REF)
9418 /* We're only interested in the field itself. If it is a
9419 BASELINK, we will need to see through it in the next
9420 step. */
9421 expr = TREE_OPERAND (expr, 1);
9422
9423 if (BASELINK_P (expr))
9424 /* See through BASELINK nodes to the underlying function. */
9425 expr = BASELINK_FUNCTIONS (expr);
9426
9427 /* decltype of a decomposition name drops references in the tuple case
9428 (unlike decltype of a normal variable) and keeps cv-qualifiers from
9429 the containing object in the other cases (unlike decltype of a member
9430 access expression). */
9431 if (DECL_DECOMPOSITION_P (expr))
9432 {
9433 if (DECL_HAS_VALUE_EXPR_P (expr))
9434 /* Expr is an array or struct subobject proxy, handle
9435 bit-fields properly. */
9436 return unlowered_expr_type (expr);
9437 else
9438 /* Expr is a reference variable for the tuple case. */
9439 return lookup_decomp_type (expr);
9440 }
9441
9442 switch (TREE_CODE (expr))
9443 {
9444 case FIELD_DECL:
9445 if (DECL_BIT_FIELD_TYPE (expr))
9446 {
9447 type = DECL_BIT_FIELD_TYPE (expr);
9448 break;
9449 }
9450 /* Fall through for fields that aren't bitfields. */
9451 gcc_fallthrough ();
9452
9453 case FUNCTION_DECL:
9454 case VAR_DECL:
9455 case CONST_DECL:
9456 case PARM_DECL:
9457 case RESULT_DECL:
9458 case TEMPLATE_PARM_INDEX:
9459 expr = mark_type_use (expr);
9460 type = TREE_TYPE (expr);
9461 break;
9462
9463 case ERROR_MARK:
9464 type = error_mark_node;
9465 break;
9466
9467 case COMPONENT_REF:
9468 case COMPOUND_EXPR:
9469 mark_type_use (expr);
9470 type = is_bitfield_expr_with_lowered_type (expr);
9471 if (!type)
9472 type = TREE_TYPE (TREE_OPERAND (expr, 1));
9473 break;
9474
9475 case BIT_FIELD_REF:
9476 gcc_unreachable ();
9477
9478 case INTEGER_CST:
9479 case PTRMEM_CST:
9480 /* We can get here when the id-expression refers to an
9481 enumerator or non-type template parameter. */
9482 type = TREE_TYPE (expr);
9483 break;
9484
9485 default:
9486 /* Handle instantiated template non-type arguments. */
9487 type = TREE_TYPE (expr);
9488 break;
9489 }
9490 }
9491 else
9492 {
9493 /* Within a lambda-expression:
9494
9495 Every occurrence of decltype((x)) where x is a possibly
9496 parenthesized id-expression that names an entity of
9497 automatic storage duration is treated as if x were
9498 transformed into an access to a corresponding data member
9499 of the closure type that would have been declared if x
9500 were a use of the denoted entity. */
9501 if (outer_automatic_var_p (expr)
9502 && current_function_decl
9503 && LAMBDA_FUNCTION_P (current_function_decl))
9504 type = capture_decltype (expr);
9505 else if (error_operand_p (expr))
9506 type = error_mark_node;
9507 else if (expr == current_class_ptr)
9508 /* If the expression is just "this", we want the
9509 cv-unqualified pointer for the "this" type. */
9510 type = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
9511 else
9512 {
9513 /* Otherwise, where T is the type of e, if e is an lvalue,
9514 decltype(e) is defined as T&; if an xvalue, T&&; otherwise, T. */
9515 cp_lvalue_kind clk = lvalue_kind (expr);
9516 type = unlowered_expr_type (expr);
9517 gcc_assert (!TYPE_REF_P (type));
9518
9519 /* For vector types, pick a non-opaque variant. */
9520 if (VECTOR_TYPE_P (type))
9521 type = strip_typedefs (type);
9522
9523 if (clk != clk_none && !(clk & clk_class))
9524 type = cp_build_reference_type (type, (clk & clk_rvalueref));
9525 }
9526 }
9527
9528 return type;
9529 }
9530
9531 /* Called from trait_expr_value to evaluate either __has_nothrow_assign or
9532 __has_nothrow_copy, depending on assign_p. Returns true iff all
9533 the copy {ctor,assign} fns are nothrow. */
9534
9535 static bool
9536 classtype_has_nothrow_assign_or_copy_p (tree type, bool assign_p)
9537 {
9538 tree fns = NULL_TREE;
9539
9540 if (assign_p || TYPE_HAS_COPY_CTOR (type))
9541 fns = get_class_binding (type, assign_p ? assign_op_identifier
9542 : ctor_identifier);
9543
9544 bool saw_copy = false;
9545 for (ovl_iterator iter (fns); iter; ++iter)
9546 {
9547 tree fn = *iter;
9548
9549 if (copy_fn_p (fn) > 0)
9550 {
9551 saw_copy = true;
9552 maybe_instantiate_noexcept (fn);
9553 if (!TYPE_NOTHROW_P (TREE_TYPE (fn)))
9554 return false;
9555 }
9556 }
9557
9558 return saw_copy;
9559 }
9560
9561 /* Actually evaluates the trait. */
9562
9563 static bool
9564 trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
9565 {
9566 enum tree_code type_code1;
9567 tree t;
9568
9569 type_code1 = TREE_CODE (type1);
9570
9571 switch (kind)
9572 {
9573 case CPTK_HAS_NOTHROW_ASSIGN:
9574 type1 = strip_array_types (type1);
9575 return (!CP_TYPE_CONST_P (type1) && type_code1 != REFERENCE_TYPE
9576 && (trait_expr_value (CPTK_HAS_TRIVIAL_ASSIGN, type1, type2)
9577 || (CLASS_TYPE_P (type1)
9578 && classtype_has_nothrow_assign_or_copy_p (type1,
9579 true))));
9580
9581 case CPTK_HAS_TRIVIAL_ASSIGN:
9582 /* ??? The standard seems to be missing the "or array of such a class
9583 type" wording for this trait. */
9584 type1 = strip_array_types (type1);
9585 return (!CP_TYPE_CONST_P (type1) && type_code1 != REFERENCE_TYPE
9586 && (trivial_type_p (type1)
9587 || (CLASS_TYPE_P (type1)
9588 && TYPE_HAS_TRIVIAL_COPY_ASSIGN (type1))));
9589
9590 case CPTK_HAS_NOTHROW_CONSTRUCTOR:
9591 type1 = strip_array_types (type1);
9592 return (trait_expr_value (CPTK_HAS_TRIVIAL_CONSTRUCTOR, type1, type2)
9593 || (CLASS_TYPE_P (type1)
9594 && (t = locate_ctor (type1))
9595 && (maybe_instantiate_noexcept (t),
9596 TYPE_NOTHROW_P (TREE_TYPE (t)))));
9597
9598 case CPTK_HAS_TRIVIAL_CONSTRUCTOR:
9599 type1 = strip_array_types (type1);
9600 return (trivial_type_p (type1)
9601 || (CLASS_TYPE_P (type1) && TYPE_HAS_TRIVIAL_DFLT (type1)));
9602
9603 case CPTK_HAS_NOTHROW_COPY:
9604 type1 = strip_array_types (type1);
9605 return (trait_expr_value (CPTK_HAS_TRIVIAL_COPY, type1, type2)
9606 || (CLASS_TYPE_P (type1)
9607 && classtype_has_nothrow_assign_or_copy_p (type1, false)));
9608
9609 case CPTK_HAS_TRIVIAL_COPY:
9610 /* ??? The standard seems to be missing the "or array of such a class
9611 type" wording for this trait. */
9612 type1 = strip_array_types (type1);
9613 return (trivial_type_p (type1) || type_code1 == REFERENCE_TYPE
9614 || (CLASS_TYPE_P (type1) && TYPE_HAS_TRIVIAL_COPY_CTOR (type1)));
9615
9616 case CPTK_HAS_TRIVIAL_DESTRUCTOR:
9617 type1 = strip_array_types (type1);
9618 return (trivial_type_p (type1) || type_code1 == REFERENCE_TYPE
9619 || (CLASS_TYPE_P (type1)
9620 && TYPE_HAS_TRIVIAL_DESTRUCTOR (type1)));
9621
9622 case CPTK_HAS_VIRTUAL_DESTRUCTOR:
9623 return type_has_virtual_destructor (type1);
9624
9625 case CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS:
9626 return type_has_unique_obj_representations (type1);
9627
9628 case CPTK_IS_ABSTRACT:
9629 return ABSTRACT_CLASS_TYPE_P (type1);
9630
9631 case CPTK_IS_AGGREGATE:
9632 return CP_AGGREGATE_TYPE_P (type1);
9633
9634 case CPTK_IS_BASE_OF:
9635 return (NON_UNION_CLASS_TYPE_P (type1) && NON_UNION_CLASS_TYPE_P (type2)
9636 && (same_type_ignoring_top_level_qualifiers_p (type1, type2)
9637 || DERIVED_FROM_P (type1, type2)));
9638
9639 case CPTK_IS_CLASS:
9640 return NON_UNION_CLASS_TYPE_P (type1);
9641
9642 case CPTK_IS_EMPTY:
9643 return NON_UNION_CLASS_TYPE_P (type1) && CLASSTYPE_EMPTY_P (type1);
9644
9645 case CPTK_IS_ENUM:
9646 return type_code1 == ENUMERAL_TYPE;
9647
9648 case CPTK_IS_FINAL:
9649 return CLASS_TYPE_P (type1) && CLASSTYPE_FINAL (type1);
9650
9651 case CPTK_IS_LITERAL_TYPE:
9652 return literal_type_p (type1);
9653
9654 case CPTK_IS_POD:
9655 return pod_type_p (type1);
9656
9657 case CPTK_IS_POLYMORPHIC:
9658 return CLASS_TYPE_P (type1) && TYPE_POLYMORPHIC_P (type1);
9659
9660 case CPTK_IS_SAME_AS:
9661 return same_type_p (type1, type2);
9662
9663 case CPTK_IS_STD_LAYOUT:
9664 return std_layout_type_p (type1);
9665
9666 case CPTK_IS_TRIVIAL:
9667 return trivial_type_p (type1);
9668
9669 case CPTK_IS_TRIVIALLY_ASSIGNABLE:
9670 return is_trivially_xible (MODIFY_EXPR, type1, type2);
9671
9672 case CPTK_IS_TRIVIALLY_CONSTRUCTIBLE:
9673 return is_trivially_xible (INIT_EXPR, type1, type2);
9674
9675 case CPTK_IS_TRIVIALLY_COPYABLE:
9676 return trivially_copyable_p (type1);
9677
9678 case CPTK_IS_UNION:
9679 return type_code1 == UNION_TYPE;
9680
9681 case CPTK_IS_ASSIGNABLE:
9682 return is_xible (MODIFY_EXPR, type1, type2);
9683
9684 case CPTK_IS_CONSTRUCTIBLE:
9685 return is_xible (INIT_EXPR, type1, type2);
9686
9687 default:
9688 gcc_unreachable ();
9689 return false;
9690 }
9691 }
9692
9693 /* If TYPE is an array of unknown bound, or (possibly cv-qualified)
9694 void, or a complete type, returns true, otherwise false. */
9695
9696 static bool
9697 check_trait_type (tree type)
9698 {
9699 if (type == NULL_TREE)
9700 return true;
9701
9702 if (TREE_CODE (type) == TREE_LIST)
9703 return (check_trait_type (TREE_VALUE (type))
9704 && check_trait_type (TREE_CHAIN (type)));
9705
9706 if (TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type)
9707 && COMPLETE_TYPE_P (TREE_TYPE (type)))
9708 return true;
9709
9710 if (VOID_TYPE_P (type))
9711 return true;
9712
9713 return !!complete_type_or_else (strip_array_types (type), NULL_TREE);
9714 }
9715
9716 /* Process a trait expression. */
9717
9718 tree
9719 finish_trait_expr (cp_trait_kind kind, tree type1, tree type2)
9720 {
9721 if (type1 == error_mark_node
9722 || type2 == error_mark_node)
9723 return error_mark_node;
9724
9725 if (processing_template_decl)
9726 {
9727 tree trait_expr = make_node (TRAIT_EXPR);
9728 TREE_TYPE (trait_expr) = boolean_type_node;
9729 TRAIT_EXPR_TYPE1 (trait_expr) = type1;
9730 TRAIT_EXPR_TYPE2 (trait_expr) = type2;
9731 TRAIT_EXPR_KIND (trait_expr) = kind;
9732 return trait_expr;
9733 }
9734
9735 switch (kind)
9736 {
9737 case CPTK_HAS_NOTHROW_ASSIGN:
9738 case CPTK_HAS_TRIVIAL_ASSIGN:
9739 case CPTK_HAS_NOTHROW_CONSTRUCTOR:
9740 case CPTK_HAS_TRIVIAL_CONSTRUCTOR:
9741 case CPTK_HAS_NOTHROW_COPY:
9742 case CPTK_HAS_TRIVIAL_COPY:
9743 case CPTK_HAS_TRIVIAL_DESTRUCTOR:
9744 case CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS:
9745 case CPTK_HAS_VIRTUAL_DESTRUCTOR:
9746 case CPTK_IS_ABSTRACT:
9747 case CPTK_IS_AGGREGATE:
9748 case CPTK_IS_EMPTY:
9749 case CPTK_IS_FINAL:
9750 case CPTK_IS_LITERAL_TYPE:
9751 case CPTK_IS_POD:
9752 case CPTK_IS_POLYMORPHIC:
9753 case CPTK_IS_STD_LAYOUT:
9754 case CPTK_IS_TRIVIAL:
9755 case CPTK_IS_TRIVIALLY_COPYABLE:
9756 if (!check_trait_type (type1))
9757 return error_mark_node;
9758 break;
9759
9760 case CPTK_IS_ASSIGNABLE:
9761 case CPTK_IS_CONSTRUCTIBLE:
9762 break;
9763
9764 case CPTK_IS_TRIVIALLY_ASSIGNABLE:
9765 case CPTK_IS_TRIVIALLY_CONSTRUCTIBLE:
9766 if (!check_trait_type (type1)
9767 || !check_trait_type (type2))
9768 return error_mark_node;
9769 break;
9770
9771 case CPTK_IS_BASE_OF:
9772 if (NON_UNION_CLASS_TYPE_P (type1) && NON_UNION_CLASS_TYPE_P (type2)
9773 && !same_type_ignoring_top_level_qualifiers_p (type1, type2)
9774 && !complete_type_or_else (type2, NULL_TREE))
9775 /* We already issued an error. */
9776 return error_mark_node;
9777 break;
9778
9779 case CPTK_IS_CLASS:
9780 case CPTK_IS_ENUM:
9781 case CPTK_IS_UNION:
9782 case CPTK_IS_SAME_AS:
9783 break;
9784
9785 default:
9786 gcc_unreachable ();
9787 }
9788
9789 return (trait_expr_value (kind, type1, type2)
9790 ? boolean_true_node : boolean_false_node);
9791 }
9792
9793 /* Do-nothing variants of functions to handle pragma FLOAT_CONST_DECIMAL64,
9794 which is ignored for C++. */
9795
9796 void
9797 set_float_const_decimal64 (void)
9798 {
9799 }
9800
9801 void
9802 clear_float_const_decimal64 (void)
9803 {
9804 }
9805
9806 bool
9807 float_const_decimal64_p (void)
9808 {
9809 return 0;
9810 }
9811
9812 \f
9813 /* Return true if T designates the implied `this' parameter. */
9814
9815 bool
9816 is_this_parameter (tree t)
9817 {
9818 if (!DECL_P (t) || DECL_NAME (t) != this_identifier)
9819 return false;
9820 gcc_assert (TREE_CODE (t) == PARM_DECL || is_capture_proxy (t)
9821 || (cp_binding_oracle && TREE_CODE (t) == VAR_DECL));
9822 return true;
9823 }
9824
9825 /* Insert the deduced return type for an auto function. */
9826
9827 void
9828 apply_deduced_return_type (tree fco, tree return_type)
9829 {
9830 tree result;
9831
9832 if (return_type == error_mark_node)
9833 return;
9834
9835 if (DECL_CONV_FN_P (fco))
9836 DECL_NAME (fco) = make_conv_op_name (return_type);
9837
9838 TREE_TYPE (fco) = change_return_type (return_type, TREE_TYPE (fco));
9839
9840 result = DECL_RESULT (fco);
9841 if (result == NULL_TREE)
9842 return;
9843 if (TREE_TYPE (result) == return_type)
9844 return;
9845
9846 if (!processing_template_decl && !VOID_TYPE_P (return_type)
9847 && !complete_type_or_else (return_type, NULL_TREE))
9848 return;
9849
9850 /* We already have a DECL_RESULT from start_preparsed_function.
9851 Now we need to redo the work it and allocate_struct_function
9852 did to reflect the new type. */
9853 gcc_assert (current_function_decl == fco);
9854 result = build_decl (input_location, RESULT_DECL, NULL_TREE,
9855 TYPE_MAIN_VARIANT (return_type));
9856 DECL_ARTIFICIAL (result) = 1;
9857 DECL_IGNORED_P (result) = 1;
9858 cp_apply_type_quals_to_decl (cp_type_quals (return_type),
9859 result);
9860
9861 DECL_RESULT (fco) = result;
9862
9863 if (!processing_template_decl)
9864 {
9865 bool aggr = aggregate_value_p (result, fco);
9866 #ifdef PCC_STATIC_STRUCT_RETURN
9867 cfun->returns_pcc_struct = aggr;
9868 #endif
9869 cfun->returns_struct = aggr;
9870 }
9871
9872 }
9873
9874 /* DECL is a local variable or parameter from the surrounding scope of a
9875 lambda-expression. Returns the decltype for a use of the capture field
9876 for DECL even if it hasn't been captured yet. */
9877
9878 static tree
9879 capture_decltype (tree decl)
9880 {
9881 tree lam = CLASSTYPE_LAMBDA_EXPR (DECL_CONTEXT (current_function_decl));
9882 tree cap = lookup_name_real (DECL_NAME (decl), /*type*/0, /*nonclass*/1,
9883 /*block_p=*/true, /*ns*/0, LOOKUP_HIDDEN);
9884 tree type;
9885
9886 if (cap && is_capture_proxy (cap))
9887 type = TREE_TYPE (cap);
9888 else
9889 switch (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lam))
9890 {
9891 case CPLD_NONE:
9892 error ("%qD is not captured", decl);
9893 return error_mark_node;
9894
9895 case CPLD_COPY:
9896 type = TREE_TYPE (decl);
9897 if (TYPE_REF_P (type)
9898 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
9899 type = TREE_TYPE (type);
9900 break;
9901
9902 case CPLD_REFERENCE:
9903 type = TREE_TYPE (decl);
9904 if (!TYPE_REF_P (type))
9905 type = build_reference_type (TREE_TYPE (decl));
9906 break;
9907
9908 default:
9909 gcc_unreachable ();
9910 }
9911
9912 if (!TYPE_REF_P (type))
9913 {
9914 if (!LAMBDA_EXPR_MUTABLE_P (lam))
9915 type = cp_build_qualified_type (type, (cp_type_quals (type)
9916 |TYPE_QUAL_CONST));
9917 type = build_reference_type (type);
9918 }
9919 return type;
9920 }
9921
9922 /* Build a unary fold expression of EXPR over OP. If IS_RIGHT is true,
9923 this is a right unary fold. Otherwise it is a left unary fold. */
9924
9925 static tree
9926 finish_unary_fold_expr (tree expr, int op, tree_code dir)
9927 {
9928 // Build a pack expansion (assuming expr has pack type).
9929 if (!uses_parameter_packs (expr))
9930 {
9931 error_at (location_of (expr), "operand of fold expression has no "
9932 "unexpanded parameter packs");
9933 return error_mark_node;
9934 }
9935 tree pack = make_pack_expansion (expr);
9936
9937 // Build the fold expression.
9938 tree code = build_int_cstu (integer_type_node, abs (op));
9939 tree fold = build_min_nt_loc (UNKNOWN_LOCATION, dir, code, pack);
9940 FOLD_EXPR_MODIFY_P (fold) = (op < 0);
9941 return fold;
9942 }
9943
9944 tree
9945 finish_left_unary_fold_expr (tree expr, int op)
9946 {
9947 return finish_unary_fold_expr (expr, op, UNARY_LEFT_FOLD_EXPR);
9948 }
9949
9950 tree
9951 finish_right_unary_fold_expr (tree expr, int op)
9952 {
9953 return finish_unary_fold_expr (expr, op, UNARY_RIGHT_FOLD_EXPR);
9954 }
9955
9956 /* Build a binary fold expression over EXPR1 and EXPR2. The
9957 associativity of the fold is determined by EXPR1 and EXPR2 (whichever
9958 has an unexpanded parameter pack). */
9959
9960 tree
9961 finish_binary_fold_expr (tree pack, tree init, int op, tree_code dir)
9962 {
9963 pack = make_pack_expansion (pack);
9964 tree code = build_int_cstu (integer_type_node, abs (op));
9965 tree fold = build_min_nt_loc (UNKNOWN_LOCATION, dir, code, pack, init);
9966 FOLD_EXPR_MODIFY_P (fold) = (op < 0);
9967 return fold;
9968 }
9969
9970 tree
9971 finish_binary_fold_expr (tree expr1, tree expr2, int op)
9972 {
9973 // Determine which expr has an unexpanded parameter pack and
9974 // set the pack and initial term.
9975 bool pack1 = uses_parameter_packs (expr1);
9976 bool pack2 = uses_parameter_packs (expr2);
9977 if (pack1 && !pack2)
9978 return finish_binary_fold_expr (expr1, expr2, op, BINARY_RIGHT_FOLD_EXPR);
9979 else if (pack2 && !pack1)
9980 return finish_binary_fold_expr (expr2, expr1, op, BINARY_LEFT_FOLD_EXPR);
9981 else
9982 {
9983 if (pack1)
9984 error ("both arguments in binary fold have unexpanded parameter packs");
9985 else
9986 error ("no unexpanded parameter packs in binary fold");
9987 }
9988 return error_mark_node;
9989 }
9990
9991 /* Finish __builtin_launder (arg). */
9992
9993 tree
9994 finish_builtin_launder (location_t loc, tree arg, tsubst_flags_t complain)
9995 {
9996 tree orig_arg = arg;
9997 if (!type_dependent_expression_p (arg))
9998 arg = decay_conversion (arg, complain);
9999 if (error_operand_p (arg))
10000 return error_mark_node;
10001 if (!type_dependent_expression_p (arg)
10002 && !TYPE_PTR_P (TREE_TYPE (arg)))
10003 {
10004 error_at (loc, "non-pointer argument to %<__builtin_launder%>");
10005 return error_mark_node;
10006 }
10007 if (processing_template_decl)
10008 arg = orig_arg;
10009 return build_call_expr_internal_loc (loc, IFN_LAUNDER,
10010 TREE_TYPE (arg), 1, arg);
10011 }
10012
10013 /* Finish __builtin_convertvector (arg, type). */
10014
10015 tree
10016 cp_build_vec_convert (tree arg, location_t loc, tree type,
10017 tsubst_flags_t complain)
10018 {
10019 if (error_operand_p (type))
10020 return error_mark_node;
10021 if (error_operand_p (arg))
10022 return error_mark_node;
10023
10024 tree ret = NULL_TREE;
10025 if (!type_dependent_expression_p (arg) && !dependent_type_p (type))
10026 ret = c_build_vec_convert (cp_expr_loc_or_loc (arg, input_location), arg,
10027 loc, type, (complain & tf_error) != 0);
10028
10029 if (!processing_template_decl)
10030 return ret;
10031
10032 return build_call_expr_internal_loc (loc, IFN_VEC_CONVERT, type, 1, arg);
10033 }
10034
10035 #include "gt-cp-semantics.h"