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