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