c8a6283b1206f5b60e3077ab4daf21ee27d85167
[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
7434 {
7435 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (t));
7436 if (!type_dependent_expression_p (t)
7437 && (!INTEGRAL_TYPE_P (type)
7438 || TREE_CODE (type) != ENUMERAL_TYPE
7439 || (DECL_NAME (TYPE_NAME (type))
7440 != get_identifier ("omp_event_handle_t"))))
7441 {
7442 error_at (OMP_CLAUSE_LOCATION (c),
7443 "%<detach%> clause event handle "
7444 "has type %qT rather than "
7445 "%<omp_event_handle_t%>",
7446 type);
7447 remove = true;
7448 }
7449 detach_seen = true;
7450 cxx_mark_addressable (t);
7451 }
7452 break;
7453
7454 case OMP_CLAUSE_MAP:
7455 case OMP_CLAUSE_TO:
7456 case OMP_CLAUSE_FROM:
7457 case OMP_CLAUSE__CACHE_:
7458 t = OMP_CLAUSE_DECL (c);
7459 if (TREE_CODE (t) == TREE_LIST)
7460 {
7461 if (handle_omp_array_sections (c, ort))
7462 remove = true;
7463 else
7464 {
7465 t = OMP_CLAUSE_DECL (c);
7466 if (TREE_CODE (t) != TREE_LIST
7467 && !type_dependent_expression_p (t)
7468 && !cp_omp_mappable_type (TREE_TYPE (t)))
7469 {
7470 error_at (OMP_CLAUSE_LOCATION (c),
7471 "array section does not have mappable type "
7472 "in %qs clause",
7473 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7474 cp_omp_emit_unmappable_type_notes (TREE_TYPE (t));
7475 remove = true;
7476 }
7477 while (TREE_CODE (t) == ARRAY_REF)
7478 t = TREE_OPERAND (t, 0);
7479 if (TREE_CODE (t) == COMPONENT_REF
7480 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
7481 {
7482 while (TREE_CODE (t) == COMPONENT_REF)
7483 t = TREE_OPERAND (t, 0);
7484 if (REFERENCE_REF_P (t))
7485 t = TREE_OPERAND (t, 0);
7486 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
7487 break;
7488 if (bitmap_bit_p (&map_head, DECL_UID (t)))
7489 {
7490 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
7491 error_at (OMP_CLAUSE_LOCATION (c),
7492 "%qD appears more than once in motion"
7493 " clauses", t);
7494 else if (ort == C_ORT_ACC)
7495 error_at (OMP_CLAUSE_LOCATION (c),
7496 "%qD appears more than once in data"
7497 " clauses", t);
7498 else
7499 error_at (OMP_CLAUSE_LOCATION (c),
7500 "%qD appears more than once in map"
7501 " clauses", t);
7502 remove = true;
7503 }
7504 else
7505 {
7506 bitmap_set_bit (&map_head, DECL_UID (t));
7507 bitmap_set_bit (&map_field_head, DECL_UID (t));
7508 }
7509 }
7510 }
7511 if (cp_oacc_check_attachments (c))
7512 remove = true;
7513 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
7514 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
7515 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH))
7516 /* In this case, we have a single array element which is a
7517 pointer, and we already set OMP_CLAUSE_SIZE in
7518 handle_omp_array_sections above. For attach/detach clauses,
7519 reset the OMP_CLAUSE_SIZE (representing a bias) to zero
7520 here. */
7521 OMP_CLAUSE_SIZE (c) = size_zero_node;
7522 break;
7523 }
7524 if (t == error_mark_node)
7525 {
7526 remove = true;
7527 break;
7528 }
7529 /* OpenACC attach / detach clauses must be pointers. */
7530 if (cp_oacc_check_attachments (c))
7531 {
7532 remove = true;
7533 break;
7534 }
7535 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
7536 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
7537 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH))
7538 /* For attach/detach clauses, set OMP_CLAUSE_SIZE (representing a
7539 bias) to zero here, so it is not set erroneously to the pointer
7540 size later on in gimplify.c. */
7541 OMP_CLAUSE_SIZE (c) = size_zero_node;
7542 if (REFERENCE_REF_P (t)
7543 && TREE_CODE (TREE_OPERAND (t, 0)) == COMPONENT_REF)
7544 {
7545 t = TREE_OPERAND (t, 0);
7546 OMP_CLAUSE_DECL (c) = t;
7547 }
7548 if ((ort == C_ORT_ACC || ort == C_ORT_OMP)
7549 && TREE_CODE (t) == COMPONENT_REF
7550 && TREE_CODE (TREE_OPERAND (t, 0)) == INDIRECT_REF)
7551 t = TREE_OPERAND (TREE_OPERAND (t, 0), 0);
7552 if (TREE_CODE (t) == COMPONENT_REF
7553 && ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP
7554 || ort == C_ORT_ACC)
7555 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE__CACHE_)
7556 {
7557 if (type_dependent_expression_p (t))
7558 break;
7559 if (TREE_CODE (TREE_OPERAND (t, 1)) == FIELD_DECL
7560 && DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
7561 {
7562 error_at (OMP_CLAUSE_LOCATION (c),
7563 "bit-field %qE in %qs clause",
7564 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7565 remove = true;
7566 }
7567 else if (!cp_omp_mappable_type (TREE_TYPE (t)))
7568 {
7569 error_at (OMP_CLAUSE_LOCATION (c),
7570 "%qE does not have a mappable type in %qs clause",
7571 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7572 cp_omp_emit_unmappable_type_notes (TREE_TYPE (t));
7573 remove = true;
7574 }
7575 while (TREE_CODE (t) == COMPONENT_REF)
7576 {
7577 if (TREE_TYPE (TREE_OPERAND (t, 0))
7578 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0)))
7579 == UNION_TYPE))
7580 {
7581 error_at (OMP_CLAUSE_LOCATION (c),
7582 "%qE is a member of a union", t);
7583 remove = true;
7584 break;
7585 }
7586 t = TREE_OPERAND (t, 0);
7587 }
7588 if (remove)
7589 break;
7590 if (REFERENCE_REF_P (t))
7591 t = TREE_OPERAND (t, 0);
7592 if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
7593 {
7594 if (bitmap_bit_p (&map_field_head, DECL_UID (t))
7595 || (ort == C_ORT_OMP
7596 && bitmap_bit_p (&map_head, DECL_UID (t))))
7597 goto handle_map_references;
7598 }
7599 }
7600 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
7601 {
7602 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
7603 break;
7604 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
7605 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
7606 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ALWAYS_POINTER
7607 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH_DETACH))
7608 break;
7609 if (DECL_P (t))
7610 error_at (OMP_CLAUSE_LOCATION (c),
7611 "%qD is not a variable in %qs clause", t,
7612 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7613 else
7614 error_at (OMP_CLAUSE_LOCATION (c),
7615 "%qE is not a variable in %qs clause", t,
7616 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7617 remove = true;
7618 }
7619 else if (VAR_P (t) && CP_DECL_THREAD_LOCAL_P (t))
7620 {
7621 error_at (OMP_CLAUSE_LOCATION (c),
7622 "%qD is threadprivate variable in %qs clause", t,
7623 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7624 remove = true;
7625 }
7626 else if (ort != C_ORT_ACC && t == current_class_ptr)
7627 {
7628 error_at (OMP_CLAUSE_LOCATION (c),
7629 "%<this%> allowed in OpenMP only in %<declare simd%>"
7630 " clauses");
7631 remove = true;
7632 break;
7633 }
7634 else if (!processing_template_decl
7635 && !TYPE_REF_P (TREE_TYPE (t))
7636 && (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
7637 || (OMP_CLAUSE_MAP_KIND (c)
7638 != GOMP_MAP_FIRSTPRIVATE_POINTER))
7639 && !cxx_mark_addressable (t))
7640 remove = true;
7641 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
7642 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
7643 || (OMP_CLAUSE_MAP_KIND (c)
7644 == GOMP_MAP_FIRSTPRIVATE_POINTER)))
7645 && t == OMP_CLAUSE_DECL (c)
7646 && !type_dependent_expression_p (t)
7647 && !cp_omp_mappable_type (TYPE_REF_P (TREE_TYPE (t))
7648 ? TREE_TYPE (TREE_TYPE (t))
7649 : TREE_TYPE (t)))
7650 {
7651 error_at (OMP_CLAUSE_LOCATION (c),
7652 "%qD does not have a mappable type in %qs clause", t,
7653 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7654 cp_omp_emit_unmappable_type_notes (TREE_TYPE (t));
7655 remove = true;
7656 }
7657 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
7658 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FORCE_DEVICEPTR
7659 && !type_dependent_expression_p (t)
7660 && !INDIRECT_TYPE_P (TREE_TYPE (t)))
7661 {
7662 error_at (OMP_CLAUSE_LOCATION (c),
7663 "%qD is not a pointer variable", t);
7664 remove = true;
7665 }
7666 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
7667 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
7668 {
7669 if (bitmap_bit_p (&generic_head, DECL_UID (t))
7670 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
7671 {
7672 error_at (OMP_CLAUSE_LOCATION (c),
7673 "%qD appears more than once in data clauses", t);
7674 remove = true;
7675 }
7676 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
7677 {
7678 if (ort == C_ORT_ACC)
7679 error_at (OMP_CLAUSE_LOCATION (c),
7680 "%qD appears more than once in data clauses", t);
7681 else
7682 error_at (OMP_CLAUSE_LOCATION (c),
7683 "%qD appears both in data and map clauses", t);
7684 remove = true;
7685 }
7686 else
7687 bitmap_set_bit (&generic_head, DECL_UID (t));
7688 }
7689 else if (bitmap_bit_p (&map_head, DECL_UID (t))
7690 && !bitmap_bit_p (&map_field_head, DECL_UID (t)))
7691 {
7692 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
7693 error_at (OMP_CLAUSE_LOCATION (c),
7694 "%qD appears more than once in motion clauses", t);
7695 else if (ort == C_ORT_ACC)
7696 error_at (OMP_CLAUSE_LOCATION (c),
7697 "%qD appears more than once in data clauses", t);
7698 else
7699 error_at (OMP_CLAUSE_LOCATION (c),
7700 "%qD appears more than once in map clauses", t);
7701 remove = true;
7702 }
7703 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
7704 && ort == C_ORT_ACC)
7705 {
7706 error_at (OMP_CLAUSE_LOCATION (c),
7707 "%qD appears more than once in data clauses", t);
7708 remove = true;
7709 }
7710 else if (bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
7711 {
7712 if (ort == C_ORT_ACC)
7713 error_at (OMP_CLAUSE_LOCATION (c),
7714 "%qD appears more than once in data clauses", t);
7715 else
7716 error_at (OMP_CLAUSE_LOCATION (c),
7717 "%qD appears both in data and map clauses", t);
7718 remove = true;
7719 }
7720 else
7721 {
7722 bitmap_set_bit (&map_head, DECL_UID (t));
7723 if (t != OMP_CLAUSE_DECL (c)
7724 && TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPONENT_REF)
7725 bitmap_set_bit (&map_field_head, DECL_UID (t));
7726 }
7727 handle_map_references:
7728 if (!remove
7729 && !processing_template_decl
7730 && ort != C_ORT_DECLARE_SIMD
7731 && TYPE_REF_P (TREE_TYPE (OMP_CLAUSE_DECL (c))))
7732 {
7733 t = OMP_CLAUSE_DECL (c);
7734 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
7735 {
7736 OMP_CLAUSE_DECL (c) = build_simple_mem_ref (t);
7737 if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
7738 OMP_CLAUSE_SIZE (c)
7739 = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t)));
7740 }
7741 else if (OMP_CLAUSE_MAP_KIND (c)
7742 != GOMP_MAP_FIRSTPRIVATE_POINTER
7743 && (OMP_CLAUSE_MAP_KIND (c)
7744 != GOMP_MAP_FIRSTPRIVATE_REFERENCE)
7745 && (OMP_CLAUSE_MAP_KIND (c)
7746 != GOMP_MAP_ALWAYS_POINTER)
7747 && (OMP_CLAUSE_MAP_KIND (c)
7748 != GOMP_MAP_ATTACH_DETACH))
7749 {
7750 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
7751 OMP_CLAUSE_MAP);
7752 if (TREE_CODE (t) == COMPONENT_REF)
7753 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ATTACH_DETACH);
7754 else
7755 OMP_CLAUSE_SET_MAP_KIND (c2,
7756 GOMP_MAP_FIRSTPRIVATE_REFERENCE);
7757 OMP_CLAUSE_DECL (c2) = t;
7758 OMP_CLAUSE_SIZE (c2) = size_zero_node;
7759 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
7760 OMP_CLAUSE_CHAIN (c) = c2;
7761 OMP_CLAUSE_DECL (c) = build_simple_mem_ref (t);
7762 if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
7763 OMP_CLAUSE_SIZE (c)
7764 = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t)));
7765 c = c2;
7766 }
7767 }
7768 break;
7769
7770 case OMP_CLAUSE_TO_DECLARE:
7771 case OMP_CLAUSE_LINK:
7772 t = OMP_CLAUSE_DECL (c);
7773 if (TREE_CODE (t) == FUNCTION_DECL
7774 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
7775 ;
7776 else if (!VAR_P (t))
7777 {
7778 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
7779 {
7780 if (TREE_CODE (t) == TEMPLATE_ID_EXPR)
7781 error_at (OMP_CLAUSE_LOCATION (c),
7782 "template %qE in clause %qs", t,
7783 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7784 else if (really_overloaded_fn (t))
7785 error_at (OMP_CLAUSE_LOCATION (c),
7786 "overloaded function name %qE in clause %qs", t,
7787 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7788 else
7789 error_at (OMP_CLAUSE_LOCATION (c),
7790 "%qE is neither a variable nor a function name "
7791 "in clause %qs", t,
7792 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7793 }
7794 else
7795 error_at (OMP_CLAUSE_LOCATION (c),
7796 "%qE is not a variable in clause %qs", t,
7797 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7798 remove = true;
7799 }
7800 else if (DECL_THREAD_LOCAL_P (t))
7801 {
7802 error_at (OMP_CLAUSE_LOCATION (c),
7803 "%qD is threadprivate variable in %qs clause", t,
7804 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7805 remove = true;
7806 }
7807 else if (!cp_omp_mappable_type (TREE_TYPE (t)))
7808 {
7809 error_at (OMP_CLAUSE_LOCATION (c),
7810 "%qD does not have a mappable type in %qs clause", t,
7811 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7812 cp_omp_emit_unmappable_type_notes (TREE_TYPE (t));
7813 remove = true;
7814 }
7815 if (remove)
7816 break;
7817 if (bitmap_bit_p (&generic_head, DECL_UID (t)))
7818 {
7819 error_at (OMP_CLAUSE_LOCATION (c),
7820 "%qE appears more than once on the same "
7821 "%<declare target%> directive", t);
7822 remove = true;
7823 }
7824 else
7825 bitmap_set_bit (&generic_head, DECL_UID (t));
7826 break;
7827
7828 case OMP_CLAUSE_UNIFORM:
7829 t = OMP_CLAUSE_DECL (c);
7830 if (TREE_CODE (t) != PARM_DECL)
7831 {
7832 if (processing_template_decl)
7833 break;
7834 if (DECL_P (t))
7835 error_at (OMP_CLAUSE_LOCATION (c),
7836 "%qD is not an argument in %<uniform%> clause", t);
7837 else
7838 error_at (OMP_CLAUSE_LOCATION (c),
7839 "%qE is not an argument in %<uniform%> clause", t);
7840 remove = true;
7841 break;
7842 }
7843 /* map_head bitmap is used as uniform_head if declare_simd. */
7844 bitmap_set_bit (&map_head, DECL_UID (t));
7845 goto check_dup_generic;
7846
7847 case OMP_CLAUSE_GRAINSIZE:
7848 t = OMP_CLAUSE_GRAINSIZE_EXPR (c);
7849 if (t == error_mark_node)
7850 remove = true;
7851 else if (!type_dependent_expression_p (t)
7852 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
7853 {
7854 error_at (OMP_CLAUSE_LOCATION (c),
7855 "%<grainsize%> expression must be integral");
7856 remove = true;
7857 }
7858 else
7859 {
7860 t = mark_rvalue_use (t);
7861 if (!processing_template_decl)
7862 {
7863 t = maybe_constant_value (t);
7864 if (TREE_CODE (t) == INTEGER_CST
7865 && tree_int_cst_sgn (t) != 1)
7866 {
7867 warning_at (OMP_CLAUSE_LOCATION (c), 0,
7868 "%<grainsize%> value must be positive");
7869 t = integer_one_node;
7870 }
7871 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7872 }
7873 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
7874 }
7875 break;
7876
7877 case OMP_CLAUSE_PRIORITY:
7878 t = OMP_CLAUSE_PRIORITY_EXPR (c);
7879 if (t == error_mark_node)
7880 remove = true;
7881 else if (!type_dependent_expression_p (t)
7882 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
7883 {
7884 error_at (OMP_CLAUSE_LOCATION (c),
7885 "%<priority%> expression must be integral");
7886 remove = true;
7887 }
7888 else
7889 {
7890 t = mark_rvalue_use (t);
7891 if (!processing_template_decl)
7892 {
7893 t = maybe_constant_value (t);
7894 if (TREE_CODE (t) == INTEGER_CST
7895 && tree_int_cst_sgn (t) == -1)
7896 {
7897 warning_at (OMP_CLAUSE_LOCATION (c), 0,
7898 "%<priority%> value must be non-negative");
7899 t = integer_one_node;
7900 }
7901 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7902 }
7903 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
7904 }
7905 break;
7906
7907 case OMP_CLAUSE_HINT:
7908 t = OMP_CLAUSE_HINT_EXPR (c);
7909 if (t == error_mark_node)
7910 remove = true;
7911 else if (!type_dependent_expression_p (t)
7912 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
7913 {
7914 error_at (OMP_CLAUSE_LOCATION (c),
7915 "%<hint%> expression must be integral");
7916 remove = true;
7917 }
7918 else
7919 {
7920 t = mark_rvalue_use (t);
7921 if (!processing_template_decl)
7922 {
7923 t = maybe_constant_value (t);
7924 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7925 if (TREE_CODE (t) != INTEGER_CST)
7926 {
7927 error_at (OMP_CLAUSE_LOCATION (c),
7928 "%<hint%> expression must be constant integer "
7929 "expression");
7930 remove = true;
7931 }
7932 }
7933 OMP_CLAUSE_HINT_EXPR (c) = t;
7934 }
7935 break;
7936
7937 case OMP_CLAUSE_IS_DEVICE_PTR:
7938 case OMP_CLAUSE_USE_DEVICE_PTR:
7939 field_ok = (ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP;
7940 t = OMP_CLAUSE_DECL (c);
7941 if (!type_dependent_expression_p (t))
7942 {
7943 tree type = TREE_TYPE (t);
7944 if (!TYPE_PTR_P (type)
7945 && (!TYPE_REF_P (type) || !TYPE_PTR_P (TREE_TYPE (type))))
7946 {
7947 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_USE_DEVICE_PTR
7948 && ort == C_ORT_OMP)
7949 {
7950 error_at (OMP_CLAUSE_LOCATION (c),
7951 "%qs variable is neither a pointer "
7952 "nor reference to pointer",
7953 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7954 remove = true;
7955 }
7956 else if (TREE_CODE (type) != ARRAY_TYPE
7957 && (!TYPE_REF_P (type)
7958 || TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE))
7959 {
7960 error_at (OMP_CLAUSE_LOCATION (c),
7961 "%qs variable is neither a pointer, nor an "
7962 "array nor reference to pointer or array",
7963 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7964 remove = true;
7965 }
7966 }
7967 }
7968 goto check_dup_generic;
7969
7970 case OMP_CLAUSE_USE_DEVICE_ADDR:
7971 field_ok = true;
7972 t = OMP_CLAUSE_DECL (c);
7973 if (!processing_template_decl
7974 && (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
7975 && !TYPE_REF_P (TREE_TYPE (t))
7976 && !cxx_mark_addressable (t))
7977 remove = true;
7978 goto check_dup_generic;
7979
7980 case OMP_CLAUSE_NOWAIT:
7981 case OMP_CLAUSE_DEFAULT:
7982 case OMP_CLAUSE_UNTIED:
7983 case OMP_CLAUSE_COLLAPSE:
7984 case OMP_CLAUSE_PARALLEL:
7985 case OMP_CLAUSE_FOR:
7986 case OMP_CLAUSE_SECTIONS:
7987 case OMP_CLAUSE_TASKGROUP:
7988 case OMP_CLAUSE_PROC_BIND:
7989 case OMP_CLAUSE_DEVICE_TYPE:
7990 case OMP_CLAUSE_NOGROUP:
7991 case OMP_CLAUSE_THREADS:
7992 case OMP_CLAUSE_SIMD:
7993 case OMP_CLAUSE_DEFAULTMAP:
7994 case OMP_CLAUSE_BIND:
7995 case OMP_CLAUSE_AUTO:
7996 case OMP_CLAUSE_INDEPENDENT:
7997 case OMP_CLAUSE_SEQ:
7998 case OMP_CLAUSE_IF_PRESENT:
7999 case OMP_CLAUSE_FINALIZE:
8000 break;
8001
8002 case OMP_CLAUSE_MERGEABLE:
8003 mergeable_seen = true;
8004 break;
8005
8006 case OMP_CLAUSE_TILE:
8007 for (tree list = OMP_CLAUSE_TILE_LIST (c); !remove && list;
8008 list = TREE_CHAIN (list))
8009 {
8010 t = TREE_VALUE (list);
8011
8012 if (t == error_mark_node)
8013 remove = true;
8014 else if (!type_dependent_expression_p (t)
8015 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
8016 {
8017 error_at (OMP_CLAUSE_LOCATION (c),
8018 "%<tile%> argument needs integral type");
8019 remove = true;
8020 }
8021 else
8022 {
8023 t = mark_rvalue_use (t);
8024 if (!processing_template_decl)
8025 {
8026 /* Zero is used to indicate '*', we permit you
8027 to get there via an ICE of value zero. */
8028 t = maybe_constant_value (t);
8029 if (!tree_fits_shwi_p (t)
8030 || tree_to_shwi (t) < 0)
8031 {
8032 error_at (OMP_CLAUSE_LOCATION (c),
8033 "%<tile%> argument needs positive "
8034 "integral constant");
8035 remove = true;
8036 }
8037 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
8038 }
8039 }
8040
8041 /* Update list item. */
8042 TREE_VALUE (list) = t;
8043 }
8044 break;
8045
8046 case OMP_CLAUSE_ORDERED:
8047 ordered_seen = true;
8048 break;
8049
8050 case OMP_CLAUSE_ORDER:
8051 if (order_seen)
8052 remove = true;
8053 else
8054 order_seen = true;
8055 break;
8056
8057 case OMP_CLAUSE_INBRANCH:
8058 case OMP_CLAUSE_NOTINBRANCH:
8059 if (branch_seen)
8060 {
8061 error_at (OMP_CLAUSE_LOCATION (c),
8062 "%<inbranch%> clause is incompatible with "
8063 "%<notinbranch%>");
8064 remove = true;
8065 }
8066 branch_seen = true;
8067 break;
8068
8069 case OMP_CLAUSE_INCLUSIVE:
8070 case OMP_CLAUSE_EXCLUSIVE:
8071 t = omp_clause_decl_field (OMP_CLAUSE_DECL (c));
8072 if (!t)
8073 t = OMP_CLAUSE_DECL (c);
8074 if (t == current_class_ptr)
8075 {
8076 error_at (OMP_CLAUSE_LOCATION (c),
8077 "%<this%> allowed in OpenMP only in %<declare simd%>"
8078 " clauses");
8079 remove = true;
8080 break;
8081 }
8082 if (!VAR_P (t)
8083 && TREE_CODE (t) != PARM_DECL
8084 && TREE_CODE (t) != FIELD_DECL)
8085 {
8086 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
8087 break;
8088 if (DECL_P (t))
8089 error_at (OMP_CLAUSE_LOCATION (c),
8090 "%qD is not a variable in clause %qs", t,
8091 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
8092 else
8093 error_at (OMP_CLAUSE_LOCATION (c),
8094 "%qE is not a variable in clause %qs", t,
8095 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
8096 remove = true;
8097 }
8098 break;
8099
8100 default:
8101 gcc_unreachable ();
8102 }
8103
8104 if (remove)
8105 *pc = OMP_CLAUSE_CHAIN (c);
8106 else
8107 pc = &OMP_CLAUSE_CHAIN (c);
8108 }
8109
8110 if (reduction_seen < 0 && (ordered_seen || schedule_seen))
8111 reduction_seen = -2;
8112
8113 for (pc = &clauses, c = clauses; c ; c = *pc)
8114 {
8115 enum omp_clause_code c_kind = OMP_CLAUSE_CODE (c);
8116 bool remove = false;
8117 bool need_complete_type = false;
8118 bool need_default_ctor = false;
8119 bool need_copy_ctor = false;
8120 bool need_copy_assignment = false;
8121 bool need_implicitly_determined = false;
8122 bool need_dtor = false;
8123 tree type, inner_type;
8124
8125 switch (c_kind)
8126 {
8127 case OMP_CLAUSE_SHARED:
8128 need_implicitly_determined = true;
8129 break;
8130 case OMP_CLAUSE_PRIVATE:
8131 need_complete_type = true;
8132 need_default_ctor = true;
8133 need_dtor = true;
8134 need_implicitly_determined = true;
8135 break;
8136 case OMP_CLAUSE_FIRSTPRIVATE:
8137 need_complete_type = true;
8138 need_copy_ctor = true;
8139 need_dtor = true;
8140 need_implicitly_determined = true;
8141 break;
8142 case OMP_CLAUSE_LASTPRIVATE:
8143 need_complete_type = true;
8144 need_copy_assignment = true;
8145 need_implicitly_determined = true;
8146 break;
8147 case OMP_CLAUSE_REDUCTION:
8148 if (reduction_seen == -2)
8149 OMP_CLAUSE_REDUCTION_INSCAN (c) = 0;
8150 if (OMP_CLAUSE_REDUCTION_INSCAN (c))
8151 need_copy_assignment = true;
8152 need_implicitly_determined = true;
8153 break;
8154 case OMP_CLAUSE_IN_REDUCTION:
8155 case OMP_CLAUSE_TASK_REDUCTION:
8156 case OMP_CLAUSE_INCLUSIVE:
8157 case OMP_CLAUSE_EXCLUSIVE:
8158 need_implicitly_determined = true;
8159 break;
8160 case OMP_CLAUSE_LINEAR:
8161 if (ort != C_ORT_OMP_DECLARE_SIMD)
8162 need_implicitly_determined = true;
8163 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c)
8164 && !bitmap_bit_p (&map_head,
8165 DECL_UID (OMP_CLAUSE_LINEAR_STEP (c))))
8166 {
8167 error_at (OMP_CLAUSE_LOCATION (c),
8168 "%<linear%> clause step is a parameter %qD not "
8169 "specified in %<uniform%> clause",
8170 OMP_CLAUSE_LINEAR_STEP (c));
8171 *pc = OMP_CLAUSE_CHAIN (c);
8172 continue;
8173 }
8174 break;
8175 case OMP_CLAUSE_COPYPRIVATE:
8176 need_copy_assignment = true;
8177 break;
8178 case OMP_CLAUSE_COPYIN:
8179 need_copy_assignment = true;
8180 break;
8181 case OMP_CLAUSE_SIMDLEN:
8182 if (safelen
8183 && !processing_template_decl
8184 && tree_int_cst_lt (OMP_CLAUSE_SAFELEN_EXPR (safelen),
8185 OMP_CLAUSE_SIMDLEN_EXPR (c)))
8186 {
8187 error_at (OMP_CLAUSE_LOCATION (c),
8188 "%<simdlen%> clause value is bigger than "
8189 "%<safelen%> clause value");
8190 OMP_CLAUSE_SIMDLEN_EXPR (c)
8191 = OMP_CLAUSE_SAFELEN_EXPR (safelen);
8192 }
8193 pc = &OMP_CLAUSE_CHAIN (c);
8194 continue;
8195 case OMP_CLAUSE_SCHEDULE:
8196 if (ordered_seen
8197 && (OMP_CLAUSE_SCHEDULE_KIND (c)
8198 & OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
8199 {
8200 error_at (OMP_CLAUSE_LOCATION (c),
8201 "%<nonmonotonic%> schedule modifier specified "
8202 "together with %<ordered%> clause");
8203 OMP_CLAUSE_SCHEDULE_KIND (c)
8204 = (enum omp_clause_schedule_kind)
8205 (OMP_CLAUSE_SCHEDULE_KIND (c)
8206 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
8207 }
8208 if (reduction_seen == -2)
8209 error_at (OMP_CLAUSE_LOCATION (c),
8210 "%qs clause specified together with %<inscan%> "
8211 "%<reduction%> clause", "schedule");
8212 pc = &OMP_CLAUSE_CHAIN (c);
8213 continue;
8214 case OMP_CLAUSE_NOGROUP:
8215 if (reduction_seen)
8216 {
8217 error_at (OMP_CLAUSE_LOCATION (c),
8218 "%<nogroup%> clause must not be used together with "
8219 "%<reduction%> clause");
8220 *pc = OMP_CLAUSE_CHAIN (c);
8221 continue;
8222 }
8223 pc = &OMP_CLAUSE_CHAIN (c);
8224 continue;
8225 case OMP_CLAUSE_ORDERED:
8226 if (reduction_seen == -2)
8227 error_at (OMP_CLAUSE_LOCATION (c),
8228 "%qs clause specified together with %<inscan%> "
8229 "%<reduction%> clause", "ordered");
8230 pc = &OMP_CLAUSE_CHAIN (c);
8231 continue;
8232 case OMP_CLAUSE_ORDER:
8233 if (ordered_seen)
8234 {
8235 error_at (OMP_CLAUSE_LOCATION (c),
8236 "%<order%> clause must not be used together "
8237 "with %<ordered%>");
8238 *pc = OMP_CLAUSE_CHAIN (c);
8239 continue;
8240 }
8241 pc = &OMP_CLAUSE_CHAIN (c);
8242 continue;
8243 case OMP_CLAUSE_DETACH:
8244 if (mergeable_seen)
8245 {
8246 error_at (OMP_CLAUSE_LOCATION (c),
8247 "%<detach%> clause must not be used together with "
8248 "%<mergeable%> clause");
8249 *pc = OMP_CLAUSE_CHAIN (c);
8250 continue;
8251 }
8252 pc = &OMP_CLAUSE_CHAIN (c);
8253 continue;
8254 case OMP_CLAUSE_NOWAIT:
8255 if (copyprivate_seen)
8256 {
8257 error_at (OMP_CLAUSE_LOCATION (c),
8258 "%<nowait%> clause must not be used together "
8259 "with %<copyprivate%>");
8260 *pc = OMP_CLAUSE_CHAIN (c);
8261 continue;
8262 }
8263 /* FALLTHRU */
8264 default:
8265 pc = &OMP_CLAUSE_CHAIN (c);
8266 continue;
8267 }
8268
8269 t = OMP_CLAUSE_DECL (c);
8270 switch (c_kind)
8271 {
8272 case OMP_CLAUSE_LASTPRIVATE:
8273 if (DECL_P (t)
8274 && !bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
8275 {
8276 need_default_ctor = true;
8277 need_dtor = true;
8278 }
8279 break;
8280
8281 case OMP_CLAUSE_REDUCTION:
8282 case OMP_CLAUSE_IN_REDUCTION:
8283 case OMP_CLAUSE_TASK_REDUCTION:
8284 if (allocate_seen)
8285 {
8286 if (TREE_CODE (t) == MEM_REF)
8287 {
8288 t = TREE_OPERAND (t, 0);
8289 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
8290 t = TREE_OPERAND (t, 0);
8291 if (TREE_CODE (t) == ADDR_EXPR
8292 || TREE_CODE (t) == INDIRECT_REF)
8293 t = TREE_OPERAND (t, 0);
8294 if (DECL_P (t))
8295 bitmap_clear_bit (&aligned_head, DECL_UID (t));
8296 }
8297 else if (TREE_CODE (t) == TREE_LIST)
8298 {
8299 while (TREE_CODE (t) == TREE_LIST)
8300 t = TREE_CHAIN (t);
8301 if (DECL_P (t))
8302 bitmap_clear_bit (&aligned_head, DECL_UID (t));
8303 t = OMP_CLAUSE_DECL (c);
8304 }
8305 else if (DECL_P (t))
8306 bitmap_clear_bit (&aligned_head, DECL_UID (t));
8307 t = OMP_CLAUSE_DECL (c);
8308 }
8309 if (processing_template_decl
8310 && !VAR_P (t) && TREE_CODE (t) != PARM_DECL)
8311 break;
8312 if (finish_omp_reduction_clause (c, &need_default_ctor,
8313 &need_dtor))
8314 remove = true;
8315 else
8316 t = OMP_CLAUSE_DECL (c);
8317 break;
8318
8319 case OMP_CLAUSE_COPYIN:
8320 if (processing_template_decl
8321 && !VAR_P (t) && TREE_CODE (t) != PARM_DECL)
8322 break;
8323 if (!VAR_P (t) || !CP_DECL_THREAD_LOCAL_P (t))
8324 {
8325 error_at (OMP_CLAUSE_LOCATION (c),
8326 "%qE must be %<threadprivate%> for %<copyin%>", t);
8327 remove = true;
8328 }
8329 break;
8330
8331 default:
8332 break;
8333 }
8334
8335 if (processing_template_decl
8336 && !VAR_P (t) && TREE_CODE (t) != PARM_DECL)
8337 {
8338 pc = &OMP_CLAUSE_CHAIN (c);
8339 continue;
8340 }
8341
8342 if (need_complete_type || need_copy_assignment)
8343 {
8344 t = require_complete_type (t);
8345 if (t == error_mark_node)
8346 remove = true;
8347 else if (!processing_template_decl
8348 && TYPE_REF_P (TREE_TYPE (t))
8349 && !complete_type_or_else (TREE_TYPE (TREE_TYPE (t)), t))
8350 remove = true;
8351 }
8352 if (need_implicitly_determined)
8353 {
8354 const char *share_name = NULL;
8355
8356 if (allocate_seen
8357 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_SHARED
8358 && DECL_P (t))
8359 bitmap_clear_bit (&aligned_head, DECL_UID (t));
8360
8361 if (VAR_P (t) && CP_DECL_THREAD_LOCAL_P (t))
8362 share_name = "threadprivate";
8363 else switch (cxx_omp_predetermined_sharing_1 (t))
8364 {
8365 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
8366 break;
8367 case OMP_CLAUSE_DEFAULT_SHARED:
8368 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
8369 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE)
8370 && c_omp_predefined_variable (t))
8371 /* The __func__ variable and similar function-local predefined
8372 variables may be listed in a shared or firstprivate
8373 clause. */
8374 break;
8375 if (VAR_P (t)
8376 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
8377 && TREE_STATIC (t)
8378 && cxx_omp_const_qual_no_mutable (t))
8379 {
8380 tree ctx = CP_DECL_CONTEXT (t);
8381 /* const qualified static data members without mutable
8382 member may be specified in firstprivate clause. */
8383 if (TYPE_P (ctx) && MAYBE_CLASS_TYPE_P (ctx))
8384 break;
8385 }
8386 share_name = "shared";
8387 break;
8388 case OMP_CLAUSE_DEFAULT_PRIVATE:
8389 share_name = "private";
8390 break;
8391 default:
8392 gcc_unreachable ();
8393 }
8394 if (share_name)
8395 {
8396 error_at (OMP_CLAUSE_LOCATION (c),
8397 "%qE is predetermined %qs for %qs",
8398 omp_clause_printable_decl (t), share_name,
8399 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
8400 remove = true;
8401 }
8402 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_SHARED
8403 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_FIRSTPRIVATE
8404 && cxx_omp_const_qual_no_mutable (t))
8405 {
8406 error_at (OMP_CLAUSE_LOCATION (c),
8407 "%<const%> qualified %qE without %<mutable%> member "
8408 "may appear only in %<shared%> or %<firstprivate%> "
8409 "clauses", omp_clause_printable_decl (t));
8410 remove = true;
8411 }
8412 }
8413
8414 if (detach_seen
8415 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
8416 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
8417 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
8418 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
8419 && OMP_CLAUSE_DECL (c) == t)
8420 {
8421 error_at (OMP_CLAUSE_LOCATION (c),
8422 "the event handle of a %<detach%> clause "
8423 "should not be in a data-sharing clause");
8424 remove = true;
8425 }
8426
8427 /* We're interested in the base element, not arrays. */
8428 inner_type = type = TREE_TYPE (t);
8429 if ((need_complete_type
8430 || need_copy_assignment
8431 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
8432 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
8433 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
8434 && TYPE_REF_P (inner_type))
8435 inner_type = TREE_TYPE (inner_type);
8436 while (TREE_CODE (inner_type) == ARRAY_TYPE)
8437 inner_type = TREE_TYPE (inner_type);
8438
8439 /* Check for special function availability by building a call to one.
8440 Save the results, because later we won't be in the right context
8441 for making these queries. */
8442 if (CLASS_TYPE_P (inner_type)
8443 && COMPLETE_TYPE_P (inner_type)
8444 && (need_default_ctor || need_copy_ctor
8445 || need_copy_assignment || need_dtor)
8446 && !type_dependent_expression_p (t)
8447 && cxx_omp_create_clause_info (c, inner_type, need_default_ctor,
8448 need_copy_ctor, need_copy_assignment,
8449 need_dtor))
8450 remove = true;
8451
8452 if (!remove
8453 && c_kind == OMP_CLAUSE_SHARED
8454 && processing_template_decl)
8455 {
8456 t = omp_clause_decl_field (OMP_CLAUSE_DECL (c));
8457 if (t)
8458 OMP_CLAUSE_DECL (c) = t;
8459 }
8460
8461 if (remove)
8462 *pc = OMP_CLAUSE_CHAIN (c);
8463 else
8464 pc = &OMP_CLAUSE_CHAIN (c);
8465 }
8466
8467 if (allocate_seen)
8468 for (pc = &clauses, c = clauses; c ; c = *pc)
8469 {
8470 bool remove = false;
8471 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ALLOCATE
8472 && !OMP_CLAUSE_ALLOCATE_COMBINED (c)
8473 && DECL_P (OMP_CLAUSE_DECL (c))
8474 && bitmap_bit_p (&aligned_head, DECL_UID (OMP_CLAUSE_DECL (c))))
8475 {
8476 error_at (OMP_CLAUSE_LOCATION (c),
8477 "%qD specified in %<allocate%> clause but not in "
8478 "an explicit privatization clause", OMP_CLAUSE_DECL (c));
8479 remove = true;
8480 }
8481 if (remove)
8482 *pc = OMP_CLAUSE_CHAIN (c);
8483 else
8484 pc = &OMP_CLAUSE_CHAIN (c);
8485 }
8486
8487 bitmap_obstack_release (NULL);
8488 return clauses;
8489 }
8490
8491 /* Start processing OpenMP clauses that can include any
8492 privatization clauses for non-static data members. */
8493
8494 tree
8495 push_omp_privatization_clauses (bool ignore_next)
8496 {
8497 if (omp_private_member_ignore_next)
8498 {
8499 omp_private_member_ignore_next = ignore_next;
8500 return NULL_TREE;
8501 }
8502 omp_private_member_ignore_next = ignore_next;
8503 if (omp_private_member_map)
8504 omp_private_member_vec.safe_push (error_mark_node);
8505 return push_stmt_list ();
8506 }
8507
8508 /* Revert remapping of any non-static data members since
8509 the last push_omp_privatization_clauses () call. */
8510
8511 void
8512 pop_omp_privatization_clauses (tree stmt)
8513 {
8514 if (stmt == NULL_TREE)
8515 return;
8516 stmt = pop_stmt_list (stmt);
8517 if (omp_private_member_map)
8518 {
8519 while (!omp_private_member_vec.is_empty ())
8520 {
8521 tree t = omp_private_member_vec.pop ();
8522 if (t == error_mark_node)
8523 {
8524 add_stmt (stmt);
8525 return;
8526 }
8527 bool no_decl_expr = t == integer_zero_node;
8528 if (no_decl_expr)
8529 t = omp_private_member_vec.pop ();
8530 tree *v = omp_private_member_map->get (t);
8531 gcc_assert (v);
8532 if (!no_decl_expr)
8533 add_decl_expr (*v);
8534 omp_private_member_map->remove (t);
8535 }
8536 delete omp_private_member_map;
8537 omp_private_member_map = NULL;
8538 }
8539 add_stmt (stmt);
8540 }
8541
8542 /* Remember OpenMP privatization clauses mapping and clear it.
8543 Used for lambdas. */
8544
8545 void
8546 save_omp_privatization_clauses (vec<tree> &save)
8547 {
8548 save = vNULL;
8549 if (omp_private_member_ignore_next)
8550 save.safe_push (integer_one_node);
8551 omp_private_member_ignore_next = false;
8552 if (!omp_private_member_map)
8553 return;
8554
8555 while (!omp_private_member_vec.is_empty ())
8556 {
8557 tree t = omp_private_member_vec.pop ();
8558 if (t == error_mark_node)
8559 {
8560 save.safe_push (t);
8561 continue;
8562 }
8563 tree n = t;
8564 if (t == integer_zero_node)
8565 t = omp_private_member_vec.pop ();
8566 tree *v = omp_private_member_map->get (t);
8567 gcc_assert (v);
8568 save.safe_push (*v);
8569 save.safe_push (t);
8570 if (n != t)
8571 save.safe_push (n);
8572 }
8573 delete omp_private_member_map;
8574 omp_private_member_map = NULL;
8575 }
8576
8577 /* Restore OpenMP privatization clauses mapping saved by the
8578 above function. */
8579
8580 void
8581 restore_omp_privatization_clauses (vec<tree> &save)
8582 {
8583 gcc_assert (omp_private_member_vec.is_empty ());
8584 omp_private_member_ignore_next = false;
8585 if (save.is_empty ())
8586 return;
8587 if (save.length () == 1 && save[0] == integer_one_node)
8588 {
8589 omp_private_member_ignore_next = true;
8590 save.release ();
8591 return;
8592 }
8593
8594 omp_private_member_map = new hash_map <tree, tree>;
8595 while (!save.is_empty ())
8596 {
8597 tree t = save.pop ();
8598 tree n = t;
8599 if (t != error_mark_node)
8600 {
8601 if (t == integer_one_node)
8602 {
8603 omp_private_member_ignore_next = true;
8604 gcc_assert (save.is_empty ());
8605 break;
8606 }
8607 if (t == integer_zero_node)
8608 t = save.pop ();
8609 tree &v = omp_private_member_map->get_or_insert (t);
8610 v = save.pop ();
8611 }
8612 omp_private_member_vec.safe_push (t);
8613 if (n != t)
8614 omp_private_member_vec.safe_push (n);
8615 }
8616 save.release ();
8617 }
8618
8619 /* For all variables in the tree_list VARS, mark them as thread local. */
8620
8621 void
8622 finish_omp_threadprivate (tree vars)
8623 {
8624 tree t;
8625
8626 /* Mark every variable in VARS to be assigned thread local storage. */
8627 for (t = vars; t; t = TREE_CHAIN (t))
8628 {
8629 tree v = TREE_PURPOSE (t);
8630
8631 if (error_operand_p (v))
8632 ;
8633 else if (!VAR_P (v))
8634 error ("%<threadprivate%> %qD is not file, namespace "
8635 "or block scope variable", v);
8636 /* If V had already been marked threadprivate, it doesn't matter
8637 whether it had been used prior to this point. */
8638 else if (TREE_USED (v)
8639 && (DECL_LANG_SPECIFIC (v) == NULL
8640 || !CP_DECL_THREADPRIVATE_P (v)))
8641 error ("%qE declared %<threadprivate%> after first use", v);
8642 else if (! TREE_STATIC (v) && ! DECL_EXTERNAL (v))
8643 error ("automatic variable %qE cannot be %<threadprivate%>", v);
8644 else if (! COMPLETE_TYPE_P (complete_type (TREE_TYPE (v))))
8645 error ("%<threadprivate%> %qE has incomplete type", v);
8646 else if (TREE_STATIC (v) && TYPE_P (CP_DECL_CONTEXT (v))
8647 && CP_DECL_CONTEXT (v) != current_class_type)
8648 error ("%<threadprivate%> %qE directive not "
8649 "in %qT definition", v, CP_DECL_CONTEXT (v));
8650 else
8651 {
8652 /* Allocate a LANG_SPECIFIC structure for V, if needed. */
8653 if (DECL_LANG_SPECIFIC (v) == NULL)
8654 retrofit_lang_decl (v);
8655
8656 if (! CP_DECL_THREAD_LOCAL_P (v))
8657 {
8658 CP_DECL_THREAD_LOCAL_P (v) = true;
8659 set_decl_tls_model (v, decl_default_tls_model (v));
8660 /* If rtl has been already set for this var, call
8661 make_decl_rtl once again, so that encode_section_info
8662 has a chance to look at the new decl flags. */
8663 if (DECL_RTL_SET_P (v))
8664 make_decl_rtl (v);
8665 }
8666 CP_DECL_THREADPRIVATE_P (v) = 1;
8667 }
8668 }
8669 }
8670
8671 /* Build an OpenMP structured block. */
8672
8673 tree
8674 begin_omp_structured_block (void)
8675 {
8676 return do_pushlevel (sk_omp);
8677 }
8678
8679 tree
8680 finish_omp_structured_block (tree block)
8681 {
8682 return do_poplevel (block);
8683 }
8684
8685 /* Similarly, except force the retention of the BLOCK. */
8686
8687 tree
8688 begin_omp_parallel (void)
8689 {
8690 keep_next_level (true);
8691 return begin_omp_structured_block ();
8692 }
8693
8694 /* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
8695 statement. */
8696
8697 tree
8698 finish_oacc_data (tree clauses, tree block)
8699 {
8700 tree stmt;
8701
8702 block = finish_omp_structured_block (block);
8703
8704 stmt = make_node (OACC_DATA);
8705 TREE_TYPE (stmt) = void_type_node;
8706 OACC_DATA_CLAUSES (stmt) = clauses;
8707 OACC_DATA_BODY (stmt) = block;
8708
8709 return add_stmt (stmt);
8710 }
8711
8712 /* Generate OACC_HOST_DATA, with CLAUSES and BLOCK as its compound
8713 statement. */
8714
8715 tree
8716 finish_oacc_host_data (tree clauses, tree block)
8717 {
8718 tree stmt;
8719
8720 block = finish_omp_structured_block (block);
8721
8722 stmt = make_node (OACC_HOST_DATA);
8723 TREE_TYPE (stmt) = void_type_node;
8724 OACC_HOST_DATA_CLAUSES (stmt) = clauses;
8725 OACC_HOST_DATA_BODY (stmt) = block;
8726
8727 return add_stmt (stmt);
8728 }
8729
8730 /* Generate OMP construct CODE, with BODY and CLAUSES as its compound
8731 statement. */
8732
8733 tree
8734 finish_omp_construct (enum tree_code code, tree body, tree clauses)
8735 {
8736 body = finish_omp_structured_block (body);
8737
8738 tree stmt = make_node (code);
8739 TREE_TYPE (stmt) = void_type_node;
8740 OMP_BODY (stmt) = body;
8741 OMP_CLAUSES (stmt) = clauses;
8742
8743 return add_stmt (stmt);
8744 }
8745
8746 tree
8747 finish_omp_parallel (tree clauses, tree body)
8748 {
8749 tree stmt;
8750
8751 body = finish_omp_structured_block (body);
8752
8753 stmt = make_node (OMP_PARALLEL);
8754 TREE_TYPE (stmt) = void_type_node;
8755 OMP_PARALLEL_CLAUSES (stmt) = clauses;
8756 OMP_PARALLEL_BODY (stmt) = body;
8757
8758 return add_stmt (stmt);
8759 }
8760
8761 tree
8762 begin_omp_task (void)
8763 {
8764 keep_next_level (true);
8765 return begin_omp_structured_block ();
8766 }
8767
8768 tree
8769 finish_omp_task (tree clauses, tree body)
8770 {
8771 tree stmt;
8772
8773 body = finish_omp_structured_block (body);
8774
8775 stmt = make_node (OMP_TASK);
8776 TREE_TYPE (stmt) = void_type_node;
8777 OMP_TASK_CLAUSES (stmt) = clauses;
8778 OMP_TASK_BODY (stmt) = body;
8779
8780 return add_stmt (stmt);
8781 }
8782
8783 /* Helper function for finish_omp_for. Convert Ith random access iterator
8784 into integral iterator. Return FALSE if successful. */
8785
8786 static bool
8787 handle_omp_for_class_iterator (int i, location_t locus, enum tree_code code,
8788 tree declv, tree orig_declv, tree initv,
8789 tree condv, tree incrv, tree *body,
8790 tree *pre_body, tree &clauses,
8791 int collapse, int ordered)
8792 {
8793 tree diff, iter_init, iter_incr = NULL, last;
8794 tree incr_var = NULL, orig_pre_body, orig_body, c;
8795 tree decl = TREE_VEC_ELT (declv, i);
8796 tree init = TREE_VEC_ELT (initv, i);
8797 tree cond = TREE_VEC_ELT (condv, i);
8798 tree incr = TREE_VEC_ELT (incrv, i);
8799 tree iter = decl;
8800 location_t elocus = locus;
8801
8802 if (init && EXPR_HAS_LOCATION (init))
8803 elocus = EXPR_LOCATION (init);
8804
8805 switch (TREE_CODE (cond))
8806 {
8807 case GT_EXPR:
8808 case GE_EXPR:
8809 case LT_EXPR:
8810 case LE_EXPR:
8811 case NE_EXPR:
8812 if (TREE_OPERAND (cond, 1) == iter)
8813 cond = build2 (swap_tree_comparison (TREE_CODE (cond)),
8814 TREE_TYPE (cond), iter, TREE_OPERAND (cond, 0));
8815 if (TREE_OPERAND (cond, 0) != iter)
8816 cond = error_mark_node;
8817 else
8818 {
8819 tree tem = build_x_binary_op (EXPR_LOCATION (cond),
8820 TREE_CODE (cond),
8821 iter, ERROR_MARK,
8822 TREE_OPERAND (cond, 1), ERROR_MARK,
8823 NULL, tf_warning_or_error);
8824 if (error_operand_p (tem))
8825 return true;
8826 }
8827 break;
8828 default:
8829 cond = error_mark_node;
8830 break;
8831 }
8832 if (cond == error_mark_node)
8833 {
8834 error_at (elocus, "invalid controlling predicate");
8835 return true;
8836 }
8837 diff = build_x_binary_op (elocus, MINUS_EXPR, TREE_OPERAND (cond, 1),
8838 ERROR_MARK, iter, ERROR_MARK, NULL,
8839 tf_warning_or_error);
8840 diff = cp_fully_fold (diff);
8841 if (error_operand_p (diff))
8842 return true;
8843 if (TREE_CODE (TREE_TYPE (diff)) != INTEGER_TYPE)
8844 {
8845 error_at (elocus, "difference between %qE and %qD does not have integer type",
8846 TREE_OPERAND (cond, 1), iter);
8847 return true;
8848 }
8849 if (!c_omp_check_loop_iv_exprs (locus, orig_declv, i,
8850 TREE_VEC_ELT (declv, i), NULL_TREE,
8851 cond, cp_walk_subtrees))
8852 return true;
8853
8854 switch (TREE_CODE (incr))
8855 {
8856 case PREINCREMENT_EXPR:
8857 case PREDECREMENT_EXPR:
8858 case POSTINCREMENT_EXPR:
8859 case POSTDECREMENT_EXPR:
8860 if (TREE_OPERAND (incr, 0) != iter)
8861 {
8862 incr = error_mark_node;
8863 break;
8864 }
8865 iter_incr = build_x_unary_op (EXPR_LOCATION (incr),
8866 TREE_CODE (incr), iter,
8867 tf_warning_or_error);
8868 if (error_operand_p (iter_incr))
8869 return true;
8870 else if (TREE_CODE (incr) == PREINCREMENT_EXPR
8871 || TREE_CODE (incr) == POSTINCREMENT_EXPR)
8872 incr = integer_one_node;
8873 else
8874 incr = integer_minus_one_node;
8875 break;
8876 case MODIFY_EXPR:
8877 if (TREE_OPERAND (incr, 0) != iter)
8878 incr = error_mark_node;
8879 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
8880 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
8881 {
8882 tree rhs = TREE_OPERAND (incr, 1);
8883 if (TREE_OPERAND (rhs, 0) == iter)
8884 {
8885 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (rhs, 1)))
8886 != INTEGER_TYPE)
8887 incr = error_mark_node;
8888 else
8889 {
8890 iter_incr = build_x_modify_expr (EXPR_LOCATION (rhs),
8891 iter, TREE_CODE (rhs),
8892 TREE_OPERAND (rhs, 1),
8893 tf_warning_or_error);
8894 if (error_operand_p (iter_incr))
8895 return true;
8896 incr = TREE_OPERAND (rhs, 1);
8897 incr = cp_convert (TREE_TYPE (diff), incr,
8898 tf_warning_or_error);
8899 if (TREE_CODE (rhs) == MINUS_EXPR)
8900 {
8901 incr = build1 (NEGATE_EXPR, TREE_TYPE (diff), incr);
8902 incr = fold_simple (incr);
8903 }
8904 if (TREE_CODE (incr) != INTEGER_CST
8905 && (TREE_CODE (incr) != NOP_EXPR
8906 || (TREE_CODE (TREE_OPERAND (incr, 0))
8907 != INTEGER_CST)))
8908 iter_incr = NULL;
8909 }
8910 }
8911 else if (TREE_OPERAND (rhs, 1) == iter)
8912 {
8913 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (rhs, 0))) != INTEGER_TYPE
8914 || TREE_CODE (rhs) != PLUS_EXPR)
8915 incr = error_mark_node;
8916 else
8917 {
8918 iter_incr = build_x_binary_op (EXPR_LOCATION (rhs),
8919 PLUS_EXPR,
8920 TREE_OPERAND (rhs, 0),
8921 ERROR_MARK, iter,
8922 ERROR_MARK, NULL,
8923 tf_warning_or_error);
8924 if (error_operand_p (iter_incr))
8925 return true;
8926 iter_incr = build_x_modify_expr (EXPR_LOCATION (rhs),
8927 iter, NOP_EXPR,
8928 iter_incr,
8929 tf_warning_or_error);
8930 if (error_operand_p (iter_incr))
8931 return true;
8932 incr = TREE_OPERAND (rhs, 0);
8933 iter_incr = NULL;
8934 }
8935 }
8936 else
8937 incr = error_mark_node;
8938 }
8939 else
8940 incr = error_mark_node;
8941 break;
8942 default:
8943 incr = error_mark_node;
8944 break;
8945 }
8946
8947 if (incr == error_mark_node)
8948 {
8949 error_at (elocus, "invalid increment expression");
8950 return true;
8951 }
8952
8953 incr = cp_convert (TREE_TYPE (diff), incr, tf_warning_or_error);
8954 incr = cp_fully_fold (incr);
8955 tree loop_iv_seen = NULL_TREE;
8956 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
8957 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
8958 && OMP_CLAUSE_DECL (c) == iter)
8959 {
8960 if (code == OMP_TASKLOOP || code == OMP_LOOP)
8961 {
8962 loop_iv_seen = c;
8963 OMP_CLAUSE_LASTPRIVATE_LOOP_IV (c) = 1;
8964 }
8965 break;
8966 }
8967 else if ((code == OMP_TASKLOOP || code == OMP_LOOP)
8968 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
8969 && OMP_CLAUSE_DECL (c) == iter)
8970 {
8971 loop_iv_seen = c;
8972 if (code == OMP_TASKLOOP)
8973 OMP_CLAUSE_PRIVATE_TASKLOOP_IV (c) = 1;
8974 }
8975
8976 decl = create_temporary_var (TREE_TYPE (diff));
8977 pushdecl (decl);
8978 add_decl_expr (decl);
8979 last = create_temporary_var (TREE_TYPE (diff));
8980 pushdecl (last);
8981 add_decl_expr (last);
8982 if (c && iter_incr == NULL && TREE_CODE (incr) != INTEGER_CST
8983 && (!ordered || (i < collapse && collapse > 1)))
8984 {
8985 incr_var = create_temporary_var (TREE_TYPE (diff));
8986 pushdecl (incr_var);
8987 add_decl_expr (incr_var);
8988 }
8989 gcc_assert (stmts_are_full_exprs_p ());
8990 tree diffvar = NULL_TREE;
8991 if (code == OMP_TASKLOOP)
8992 {
8993 if (!loop_iv_seen)
8994 {
8995 tree ivc = build_omp_clause (locus, OMP_CLAUSE_FIRSTPRIVATE);
8996 OMP_CLAUSE_DECL (ivc) = iter;
8997 cxx_omp_finish_clause (ivc, NULL, false);
8998 OMP_CLAUSE_CHAIN (ivc) = clauses;
8999 clauses = ivc;
9000 }
9001 tree lvc = build_omp_clause (locus, OMP_CLAUSE_FIRSTPRIVATE);
9002 OMP_CLAUSE_DECL (lvc) = last;
9003 OMP_CLAUSE_CHAIN (lvc) = clauses;
9004 clauses = lvc;
9005 diffvar = create_temporary_var (TREE_TYPE (diff));
9006 pushdecl (diffvar);
9007 add_decl_expr (diffvar);
9008 }
9009 else if (code == OMP_LOOP)
9010 {
9011 if (!loop_iv_seen)
9012 {
9013 /* While iterators on the loop construct are predetermined
9014 lastprivate, if the decl is not declared inside of the
9015 loop, OMP_CLAUSE_LASTPRIVATE should have been added
9016 already. */
9017 loop_iv_seen = build_omp_clause (locus, OMP_CLAUSE_FIRSTPRIVATE);
9018 OMP_CLAUSE_DECL (loop_iv_seen) = iter;
9019 OMP_CLAUSE_CHAIN (loop_iv_seen) = clauses;
9020 clauses = loop_iv_seen;
9021 }
9022 else if (OMP_CLAUSE_CODE (loop_iv_seen) == OMP_CLAUSE_PRIVATE)
9023 {
9024 OMP_CLAUSE_PRIVATE_DEBUG (loop_iv_seen) = 0;
9025 OMP_CLAUSE_PRIVATE_OUTER_REF (loop_iv_seen) = 0;
9026 OMP_CLAUSE_CODE (loop_iv_seen) = OMP_CLAUSE_FIRSTPRIVATE;
9027 }
9028 if (OMP_CLAUSE_CODE (loop_iv_seen) == OMP_CLAUSE_FIRSTPRIVATE)
9029 cxx_omp_finish_clause (loop_iv_seen, NULL, false);
9030 }
9031
9032 orig_pre_body = *pre_body;
9033 *pre_body = push_stmt_list ();
9034 if (orig_pre_body)
9035 add_stmt (orig_pre_body);
9036 if (init != NULL)
9037 finish_expr_stmt (build_x_modify_expr (elocus,
9038 iter, NOP_EXPR, init,
9039 tf_warning_or_error));
9040 init = build_int_cst (TREE_TYPE (diff), 0);
9041 if (c && iter_incr == NULL
9042 && (!ordered || (i < collapse && collapse > 1)))
9043 {
9044 if (incr_var)
9045 {
9046 finish_expr_stmt (build_x_modify_expr (elocus,
9047 incr_var, NOP_EXPR,
9048 incr, tf_warning_or_error));
9049 incr = incr_var;
9050 }
9051 iter_incr = build_x_modify_expr (elocus,
9052 iter, PLUS_EXPR, incr,
9053 tf_warning_or_error);
9054 }
9055 if (c && ordered && i < collapse && collapse > 1)
9056 iter_incr = incr;
9057 finish_expr_stmt (build_x_modify_expr (elocus,
9058 last, NOP_EXPR, init,
9059 tf_warning_or_error));
9060 if (diffvar)
9061 {
9062 finish_expr_stmt (build_x_modify_expr (elocus,
9063 diffvar, NOP_EXPR,
9064 diff, tf_warning_or_error));
9065 diff = diffvar;
9066 }
9067 *pre_body = pop_stmt_list (*pre_body);
9068
9069 cond = cp_build_binary_op (elocus,
9070 TREE_CODE (cond), decl, diff,
9071 tf_warning_or_error);
9072 incr = build_modify_expr (elocus, decl, NULL_TREE, PLUS_EXPR,
9073 elocus, incr, NULL_TREE);
9074
9075 orig_body = *body;
9076 *body = push_stmt_list ();
9077 iter_init = build2 (MINUS_EXPR, TREE_TYPE (diff), decl, last);
9078 iter_init = build_x_modify_expr (elocus,
9079 iter, PLUS_EXPR, iter_init,
9080 tf_warning_or_error);
9081 if (iter_init != error_mark_node)
9082 iter_init = build1 (NOP_EXPR, void_type_node, iter_init);
9083 finish_expr_stmt (iter_init);
9084 finish_expr_stmt (build_x_modify_expr (elocus,
9085 last, NOP_EXPR, decl,
9086 tf_warning_or_error));
9087 add_stmt (orig_body);
9088 *body = pop_stmt_list (*body);
9089
9090 if (c)
9091 {
9092 OMP_CLAUSE_LASTPRIVATE_STMT (c) = push_stmt_list ();
9093 if (!ordered)
9094 finish_expr_stmt (iter_incr);
9095 else
9096 {
9097 iter_init = decl;
9098 if (i < collapse && collapse > 1 && !error_operand_p (iter_incr))
9099 iter_init = build2 (PLUS_EXPR, TREE_TYPE (diff),
9100 iter_init, iter_incr);
9101 iter_init = build2 (MINUS_EXPR, TREE_TYPE (diff), iter_init, last);
9102 iter_init = build_x_modify_expr (elocus,
9103 iter, PLUS_EXPR, iter_init,
9104 tf_warning_or_error);
9105 if (iter_init != error_mark_node)
9106 iter_init = build1 (NOP_EXPR, void_type_node, iter_init);
9107 finish_expr_stmt (iter_init);
9108 }
9109 OMP_CLAUSE_LASTPRIVATE_STMT (c)
9110 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (c));
9111 }
9112
9113 if (TREE_CODE (TREE_VEC_ELT (orig_declv, i)) == TREE_LIST)
9114 {
9115 tree t = TREE_VEC_ELT (orig_declv, i);
9116 gcc_assert (TREE_PURPOSE (t) == NULL_TREE
9117 && TREE_VALUE (t) == NULL_TREE
9118 && TREE_CODE (TREE_CHAIN (t)) == TREE_VEC);
9119 TREE_PURPOSE (t) = TREE_VEC_ELT (declv, i);
9120 TREE_VALUE (t) = last;
9121 }
9122 else
9123 TREE_VEC_ELT (orig_declv, i)
9124 = tree_cons (TREE_VEC_ELT (declv, i), last, NULL_TREE);
9125 TREE_VEC_ELT (declv, i) = decl;
9126 TREE_VEC_ELT (initv, i) = init;
9127 TREE_VEC_ELT (condv, i) = cond;
9128 TREE_VEC_ELT (incrv, i) = incr;
9129
9130 return false;
9131 }
9132
9133 /* Build and validate an OMP_FOR statement. CLAUSES, BODY, COND, INCR
9134 are directly for their associated operands in the statement. DECL
9135 and INIT are a combo; if DECL is NULL then INIT ought to be a
9136 MODIFY_EXPR, and the DECL should be extracted. PRE_BODY are
9137 optional statements that need to go before the loop into its
9138 sk_omp scope. */
9139
9140 tree
9141 finish_omp_for (location_t locus, enum tree_code code, tree declv,
9142 tree orig_declv, tree initv, tree condv, tree incrv,
9143 tree body, tree pre_body, vec<tree> *orig_inits, tree clauses)
9144 {
9145 tree omp_for = NULL, orig_incr = NULL;
9146 tree decl = NULL, init, cond, incr;
9147 location_t elocus;
9148 int i;
9149 int collapse = 1;
9150 int ordered = 0;
9151
9152 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (initv));
9153 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (condv));
9154 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (incrv));
9155 if (TREE_VEC_LENGTH (declv) > 1)
9156 {
9157 tree c;
9158
9159 c = omp_find_clause (clauses, OMP_CLAUSE_TILE);
9160 if (c)
9161 collapse = list_length (OMP_CLAUSE_TILE_LIST (c));
9162 else
9163 {
9164 c = omp_find_clause (clauses, OMP_CLAUSE_COLLAPSE);
9165 if (c)
9166 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (c));
9167 if (collapse != TREE_VEC_LENGTH (declv))
9168 ordered = TREE_VEC_LENGTH (declv);
9169 }
9170 }
9171 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
9172 {
9173 decl = TREE_VEC_ELT (declv, i);
9174 init = TREE_VEC_ELT (initv, i);
9175 cond = TREE_VEC_ELT (condv, i);
9176 incr = TREE_VEC_ELT (incrv, i);
9177 elocus = locus;
9178
9179 if (decl == NULL)
9180 {
9181 if (init != NULL)
9182 switch (TREE_CODE (init))
9183 {
9184 case MODIFY_EXPR:
9185 decl = TREE_OPERAND (init, 0);
9186 init = TREE_OPERAND (init, 1);
9187 break;
9188 case MODOP_EXPR:
9189 if (TREE_CODE (TREE_OPERAND (init, 1)) == NOP_EXPR)
9190 {
9191 decl = TREE_OPERAND (init, 0);
9192 init = TREE_OPERAND (init, 2);
9193 }
9194 break;
9195 default:
9196 break;
9197 }
9198
9199 if (decl == NULL)
9200 {
9201 error_at (locus,
9202 "expected iteration declaration or initialization");
9203 return NULL;
9204 }
9205 }
9206
9207 if (init && EXPR_HAS_LOCATION (init))
9208 elocus = EXPR_LOCATION (init);
9209
9210 if (cond == global_namespace)
9211 continue;
9212
9213 if (cond == NULL)
9214 {
9215 error_at (elocus, "missing controlling predicate");
9216 return NULL;
9217 }
9218
9219 if (incr == NULL)
9220 {
9221 error_at (elocus, "missing increment expression");
9222 return NULL;
9223 }
9224
9225 TREE_VEC_ELT (declv, i) = decl;
9226 TREE_VEC_ELT (initv, i) = init;
9227 }
9228
9229 if (orig_inits)
9230 {
9231 bool fail = false;
9232 tree orig_init;
9233 FOR_EACH_VEC_ELT (*orig_inits, i, orig_init)
9234 if (orig_init
9235 && !c_omp_check_loop_iv_exprs (locus,
9236 orig_declv ? orig_declv : declv, i,
9237 TREE_VEC_ELT (declv, i), orig_init,
9238 NULL_TREE, cp_walk_subtrees))
9239 fail = true;
9240 if (fail)
9241 return NULL;
9242 }
9243
9244 if (dependent_omp_for_p (declv, initv, condv, incrv))
9245 {
9246 tree stmt;
9247
9248 stmt = make_node (code);
9249
9250 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
9251 {
9252 /* This is really just a place-holder. We'll be decomposing this
9253 again and going through the cp_build_modify_expr path below when
9254 we instantiate the thing. */
9255 TREE_VEC_ELT (initv, i)
9256 = build2 (MODIFY_EXPR, void_type_node, TREE_VEC_ELT (declv, i),
9257 TREE_VEC_ELT (initv, i));
9258 }
9259
9260 TREE_TYPE (stmt) = void_type_node;
9261 OMP_FOR_INIT (stmt) = initv;
9262 OMP_FOR_COND (stmt) = condv;
9263 OMP_FOR_INCR (stmt) = incrv;
9264 OMP_FOR_BODY (stmt) = body;
9265 OMP_FOR_PRE_BODY (stmt) = pre_body;
9266 OMP_FOR_CLAUSES (stmt) = clauses;
9267
9268 SET_EXPR_LOCATION (stmt, locus);
9269 return add_stmt (stmt);
9270 }
9271
9272 if (!orig_declv)
9273 orig_declv = copy_node (declv);
9274
9275 if (processing_template_decl)
9276 orig_incr = make_tree_vec (TREE_VEC_LENGTH (incrv));
9277
9278 for (i = 0; i < TREE_VEC_LENGTH (declv); )
9279 {
9280 decl = TREE_VEC_ELT (declv, i);
9281 init = TREE_VEC_ELT (initv, i);
9282 cond = TREE_VEC_ELT (condv, i);
9283 incr = TREE_VEC_ELT (incrv, i);
9284 if (orig_incr)
9285 TREE_VEC_ELT (orig_incr, i) = incr;
9286 elocus = locus;
9287
9288 if (init && EXPR_HAS_LOCATION (init))
9289 elocus = EXPR_LOCATION (init);
9290
9291 if (!DECL_P (decl))
9292 {
9293 error_at (elocus, "expected iteration declaration or initialization");
9294 return NULL;
9295 }
9296
9297 if (incr && TREE_CODE (incr) == MODOP_EXPR)
9298 {
9299 if (orig_incr)
9300 TREE_VEC_ELT (orig_incr, i) = incr;
9301 incr = cp_build_modify_expr (elocus, TREE_OPERAND (incr, 0),
9302 TREE_CODE (TREE_OPERAND (incr, 1)),
9303 TREE_OPERAND (incr, 2),
9304 tf_warning_or_error);
9305 }
9306
9307 if (CLASS_TYPE_P (TREE_TYPE (decl)))
9308 {
9309 if (code == OMP_SIMD)
9310 {
9311 error_at (elocus, "%<#pragma omp simd%> used with class "
9312 "iteration variable %qE", decl);
9313 return NULL;
9314 }
9315 if (handle_omp_for_class_iterator (i, locus, code, declv, orig_declv,
9316 initv, condv, incrv, &body,
9317 &pre_body, clauses,
9318 collapse, ordered))
9319 return NULL;
9320 continue;
9321 }
9322
9323 if (!INTEGRAL_TYPE_P (TREE_TYPE (decl))
9324 && !TYPE_PTR_P (TREE_TYPE (decl)))
9325 {
9326 error_at (elocus, "invalid type for iteration variable %qE", decl);
9327 return NULL;
9328 }
9329
9330 if (!processing_template_decl && TREE_CODE (init) != TREE_VEC)
9331 init = cp_build_modify_expr (elocus, decl, NOP_EXPR, init,
9332 tf_warning_or_error);
9333 else
9334 init = build2 (MODIFY_EXPR, void_type_node, decl, init);
9335 if (decl == error_mark_node || init == error_mark_node)
9336 return NULL;
9337
9338 TREE_VEC_ELT (declv, i) = decl;
9339 TREE_VEC_ELT (initv, i) = init;
9340 TREE_VEC_ELT (condv, i) = cond;
9341 TREE_VEC_ELT (incrv, i) = incr;
9342 i++;
9343 }
9344
9345 if (pre_body && IS_EMPTY_STMT (pre_body))
9346 pre_body = NULL;
9347
9348 omp_for = c_finish_omp_for (locus, code, declv, orig_declv, initv, condv,
9349 incrv, body, pre_body,
9350 !processing_template_decl);
9351
9352 /* Check for iterators appearing in lb, b or incr expressions. */
9353 if (omp_for && !c_omp_check_loop_iv (omp_for, orig_declv, cp_walk_subtrees))
9354 omp_for = NULL_TREE;
9355
9356 if (omp_for == NULL)
9357 return NULL;
9358
9359 add_stmt (omp_for);
9360
9361 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INCR (omp_for)); i++)
9362 {
9363 init = TREE_VEC_ELT (OMP_FOR_INIT (omp_for), i);
9364 decl = TREE_OPERAND (init, 0);
9365 cond = TREE_VEC_ELT (OMP_FOR_COND (omp_for), i);
9366 incr = TREE_VEC_ELT (OMP_FOR_INCR (omp_for), i);
9367
9368 if (!processing_template_decl)
9369 {
9370 if (TREE_CODE (TREE_OPERAND (init, 1)) == TREE_VEC)
9371 {
9372 tree t = TREE_VEC_ELT (TREE_OPERAND (init, 1), 1);
9373 TREE_VEC_ELT (TREE_OPERAND (init, 1), 1)
9374 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
9375 t = TREE_VEC_ELT (TREE_OPERAND (init, 1), 2);
9376 TREE_VEC_ELT (TREE_OPERAND (init, 1), 2)
9377 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
9378 }
9379 else
9380 {
9381 tree t = TREE_OPERAND (init, 1);
9382 TREE_OPERAND (init, 1)
9383 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
9384 }
9385 if (TREE_CODE (TREE_OPERAND (cond, 1)) == TREE_VEC)
9386 {
9387 tree t = TREE_VEC_ELT (TREE_OPERAND (cond, 1), 1);
9388 TREE_VEC_ELT (TREE_OPERAND (cond, 1), 1)
9389 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
9390 t = TREE_VEC_ELT (TREE_OPERAND (cond, 1), 2);
9391 TREE_VEC_ELT (TREE_OPERAND (cond, 1), 2)
9392 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
9393 }
9394 else
9395 {
9396 tree t = TREE_OPERAND (cond, 1);
9397 TREE_OPERAND (cond, 1)
9398 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
9399 }
9400 }
9401
9402 if (TREE_CODE (incr) != MODIFY_EXPR)
9403 continue;
9404
9405 if (TREE_SIDE_EFFECTS (TREE_OPERAND (incr, 1))
9406 && BINARY_CLASS_P (TREE_OPERAND (incr, 1))
9407 && !processing_template_decl)
9408 {
9409 tree t = TREE_OPERAND (TREE_OPERAND (incr, 1), 0);
9410 if (TREE_SIDE_EFFECTS (t)
9411 && t != decl
9412 && (TREE_CODE (t) != NOP_EXPR
9413 || TREE_OPERAND (t, 0) != decl))
9414 TREE_OPERAND (TREE_OPERAND (incr, 1), 0)
9415 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
9416
9417 t = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
9418 if (TREE_SIDE_EFFECTS (t)
9419 && t != decl
9420 && (TREE_CODE (t) != NOP_EXPR
9421 || TREE_OPERAND (t, 0) != decl))
9422 TREE_OPERAND (TREE_OPERAND (incr, 1), 1)
9423 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
9424 }
9425
9426 if (orig_incr)
9427 TREE_VEC_ELT (OMP_FOR_INCR (omp_for), i) = TREE_VEC_ELT (orig_incr, i);
9428 }
9429 OMP_FOR_CLAUSES (omp_for) = clauses;
9430
9431 /* For simd loops with non-static data member iterators, we could have added
9432 OMP_CLAUSE_LINEAR clauses without OMP_CLAUSE_LINEAR_STEP. As we know the
9433 step at this point, fill it in. */
9434 if (code == OMP_SIMD && !processing_template_decl
9435 && TREE_VEC_LENGTH (OMP_FOR_INCR (omp_for)) == 1)
9436 for (tree c = omp_find_clause (clauses, OMP_CLAUSE_LINEAR); c;
9437 c = omp_find_clause (OMP_CLAUSE_CHAIN (c), OMP_CLAUSE_LINEAR))
9438 if (OMP_CLAUSE_LINEAR_STEP (c) == NULL_TREE)
9439 {
9440 decl = TREE_OPERAND (TREE_VEC_ELT (OMP_FOR_INIT (omp_for), 0), 0);
9441 gcc_assert (decl == OMP_CLAUSE_DECL (c));
9442 incr = TREE_VEC_ELT (OMP_FOR_INCR (omp_for), 0);
9443 tree step, stept;
9444 switch (TREE_CODE (incr))
9445 {
9446 case PREINCREMENT_EXPR:
9447 case POSTINCREMENT_EXPR:
9448 /* c_omp_for_incr_canonicalize_ptr() should have been
9449 called to massage things appropriately. */
9450 gcc_assert (!INDIRECT_TYPE_P (TREE_TYPE (decl)));
9451 OMP_CLAUSE_LINEAR_STEP (c) = build_int_cst (TREE_TYPE (decl), 1);
9452 break;
9453 case PREDECREMENT_EXPR:
9454 case POSTDECREMENT_EXPR:
9455 /* c_omp_for_incr_canonicalize_ptr() should have been
9456 called to massage things appropriately. */
9457 gcc_assert (!INDIRECT_TYPE_P (TREE_TYPE (decl)));
9458 OMP_CLAUSE_LINEAR_STEP (c)
9459 = build_int_cst (TREE_TYPE (decl), -1);
9460 break;
9461 case MODIFY_EXPR:
9462 gcc_assert (TREE_OPERAND (incr, 0) == decl);
9463 incr = TREE_OPERAND (incr, 1);
9464 switch (TREE_CODE (incr))
9465 {
9466 case PLUS_EXPR:
9467 if (TREE_OPERAND (incr, 1) == decl)
9468 step = TREE_OPERAND (incr, 0);
9469 else
9470 step = TREE_OPERAND (incr, 1);
9471 break;
9472 case MINUS_EXPR:
9473 case POINTER_PLUS_EXPR:
9474 gcc_assert (TREE_OPERAND (incr, 0) == decl);
9475 step = TREE_OPERAND (incr, 1);
9476 break;
9477 default:
9478 gcc_unreachable ();
9479 }
9480 stept = TREE_TYPE (decl);
9481 if (INDIRECT_TYPE_P (stept))
9482 stept = sizetype;
9483 step = fold_convert (stept, step);
9484 if (TREE_CODE (incr) == MINUS_EXPR)
9485 step = fold_build1 (NEGATE_EXPR, stept, step);
9486 OMP_CLAUSE_LINEAR_STEP (c) = step;
9487 break;
9488 default:
9489 gcc_unreachable ();
9490 }
9491 }
9492 /* Override saved methods on OMP_LOOP's OMP_CLAUSE_LASTPRIVATE_LOOP_IV
9493 clauses, we need copy ctor for those rather than default ctor,
9494 plus as for other lastprivates assignment op and dtor. */
9495 if (code == OMP_LOOP && !processing_template_decl)
9496 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
9497 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
9498 && OMP_CLAUSE_LASTPRIVATE_LOOP_IV (c)
9499 && cxx_omp_create_clause_info (c, TREE_TYPE (OMP_CLAUSE_DECL (c)),
9500 false, true, true, true))
9501 CP_OMP_CLAUSE_INFO (c) = NULL_TREE;
9502
9503 return omp_for;
9504 }
9505
9506 /* Fix up range for decls. Those decls were pushed into BIND's BIND_EXPR_VARS
9507 and need to be moved into the BIND_EXPR inside of the OMP_FOR's body. */
9508
9509 tree
9510 finish_omp_for_block (tree bind, tree omp_for)
9511 {
9512 if (omp_for == NULL_TREE
9513 || !OMP_FOR_ORIG_DECLS (omp_for)
9514 || bind == NULL_TREE
9515 || TREE_CODE (bind) != BIND_EXPR)
9516 return bind;
9517 tree b = NULL_TREE;
9518 for (int i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (omp_for)); i++)
9519 if (TREE_CODE (TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (omp_for), i)) == TREE_LIST
9520 && TREE_CHAIN (TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (omp_for), i)))
9521 {
9522 tree v = TREE_CHAIN (TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (omp_for), i));
9523 gcc_assert (BIND_EXPR_BLOCK (bind)
9524 && (BIND_EXPR_VARS (bind)
9525 == BLOCK_VARS (BIND_EXPR_BLOCK (bind))));
9526 for (int j = 2; j < TREE_VEC_LENGTH (v); j++)
9527 for (tree *p = &BIND_EXPR_VARS (bind); *p; p = &DECL_CHAIN (*p))
9528 {
9529 if (*p == TREE_VEC_ELT (v, j))
9530 {
9531 tree var = *p;
9532 *p = DECL_CHAIN (*p);
9533 if (b == NULL_TREE)
9534 {
9535 b = make_node (BLOCK);
9536 b = build3 (BIND_EXPR, void_type_node, NULL_TREE,
9537 OMP_FOR_BODY (omp_for), b);
9538 TREE_SIDE_EFFECTS (b) = 1;
9539 OMP_FOR_BODY (omp_for) = b;
9540 }
9541 DECL_CHAIN (var) = BIND_EXPR_VARS (b);
9542 BIND_EXPR_VARS (b) = var;
9543 BLOCK_VARS (BIND_EXPR_BLOCK (b)) = var;
9544 }
9545 }
9546 BLOCK_VARS (BIND_EXPR_BLOCK (bind)) = BIND_EXPR_VARS (bind);
9547 }
9548 return bind;
9549 }
9550
9551 void
9552 finish_omp_atomic (location_t loc, enum tree_code code, enum tree_code opcode,
9553 tree lhs, tree rhs, tree v, tree lhs1, tree rhs1,
9554 tree clauses, enum omp_memory_order mo)
9555 {
9556 tree orig_lhs;
9557 tree orig_rhs;
9558 tree orig_v;
9559 tree orig_lhs1;
9560 tree orig_rhs1;
9561 bool dependent_p;
9562 tree stmt;
9563
9564 orig_lhs = lhs;
9565 orig_rhs = rhs;
9566 orig_v = v;
9567 orig_lhs1 = lhs1;
9568 orig_rhs1 = rhs1;
9569 dependent_p = false;
9570 stmt = NULL_TREE;
9571
9572 /* Even in a template, we can detect invalid uses of the atomic
9573 pragma if neither LHS nor RHS is type-dependent. */
9574 if (processing_template_decl)
9575 {
9576 dependent_p = (type_dependent_expression_p (lhs)
9577 || (rhs && type_dependent_expression_p (rhs))
9578 || (v && type_dependent_expression_p (v))
9579 || (lhs1 && type_dependent_expression_p (lhs1))
9580 || (rhs1 && type_dependent_expression_p (rhs1)));
9581 if (clauses)
9582 {
9583 gcc_assert (TREE_CODE (clauses) == OMP_CLAUSE
9584 && OMP_CLAUSE_CODE (clauses) == OMP_CLAUSE_HINT
9585 && OMP_CLAUSE_CHAIN (clauses) == NULL_TREE);
9586 if (type_dependent_expression_p (OMP_CLAUSE_HINT_EXPR (clauses))
9587 || TREE_CODE (OMP_CLAUSE_HINT_EXPR (clauses)) != INTEGER_CST)
9588 dependent_p = true;
9589 }
9590 if (!dependent_p)
9591 {
9592 lhs = build_non_dependent_expr (lhs);
9593 if (rhs)
9594 rhs = build_non_dependent_expr (rhs);
9595 if (v)
9596 v = build_non_dependent_expr (v);
9597 if (lhs1)
9598 lhs1 = build_non_dependent_expr (lhs1);
9599 if (rhs1)
9600 rhs1 = build_non_dependent_expr (rhs1);
9601 }
9602 }
9603 if (!dependent_p)
9604 {
9605 bool swapped = false;
9606 if (rhs1 && cp_tree_equal (lhs, rhs))
9607 {
9608 std::swap (rhs, rhs1);
9609 swapped = !commutative_tree_code (opcode);
9610 }
9611 if (rhs1 && !cp_tree_equal (lhs, rhs1))
9612 {
9613 if (code == OMP_ATOMIC)
9614 error ("%<#pragma omp atomic update%> uses two different "
9615 "expressions for memory");
9616 else
9617 error ("%<#pragma omp atomic capture%> uses two different "
9618 "expressions for memory");
9619 return;
9620 }
9621 if (lhs1 && !cp_tree_equal (lhs, lhs1))
9622 {
9623 if (code == OMP_ATOMIC)
9624 error ("%<#pragma omp atomic update%> uses two different "
9625 "expressions for memory");
9626 else
9627 error ("%<#pragma omp atomic capture%> uses two different "
9628 "expressions for memory");
9629 return;
9630 }
9631 stmt = c_finish_omp_atomic (loc, code, opcode, lhs, rhs,
9632 v, lhs1, rhs1, swapped, mo,
9633 processing_template_decl != 0);
9634 if (stmt == error_mark_node)
9635 return;
9636 }
9637 if (processing_template_decl)
9638 {
9639 if (code == OMP_ATOMIC_READ)
9640 {
9641 stmt = build_min_nt_loc (loc, OMP_ATOMIC_READ, orig_lhs);
9642 OMP_ATOMIC_MEMORY_ORDER (stmt) = mo;
9643 stmt = build2 (MODIFY_EXPR, void_type_node, orig_v, stmt);
9644 }
9645 else
9646 {
9647 if (opcode == NOP_EXPR)
9648 stmt = build2 (MODIFY_EXPR, void_type_node, orig_lhs, orig_rhs);
9649 else
9650 stmt = build2 (opcode, void_type_node, orig_lhs, orig_rhs);
9651 if (orig_rhs1)
9652 stmt = build_min_nt_loc (EXPR_LOCATION (orig_rhs1),
9653 COMPOUND_EXPR, orig_rhs1, stmt);
9654 if (code != OMP_ATOMIC)
9655 {
9656 stmt = build_min_nt_loc (loc, code, orig_lhs1, stmt);
9657 OMP_ATOMIC_MEMORY_ORDER (stmt) = mo;
9658 stmt = build2 (MODIFY_EXPR, void_type_node, orig_v, stmt);
9659 }
9660 }
9661 stmt = build2 (OMP_ATOMIC, void_type_node,
9662 clauses ? clauses : integer_zero_node, stmt);
9663 OMP_ATOMIC_MEMORY_ORDER (stmt) = mo;
9664 SET_EXPR_LOCATION (stmt, loc);
9665 }
9666
9667 /* Avoid -Wunused-value warnings here, the whole construct has side-effects
9668 and even if it might be wrapped from fold-const.c or c-omp.c wrapped
9669 in some tree that appears to be unused, the value is not unused. */
9670 warning_sentinel w (warn_unused_value);
9671 finish_expr_stmt (stmt);
9672 }
9673
9674 void
9675 finish_omp_barrier (void)
9676 {
9677 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_BARRIER);
9678 releasing_vec vec;
9679 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
9680 finish_expr_stmt (stmt);
9681 }
9682
9683 void
9684 finish_omp_depobj (location_t loc, tree depobj,
9685 enum omp_clause_depend_kind kind, tree clause)
9686 {
9687 if (!error_operand_p (depobj) && !type_dependent_expression_p (depobj))
9688 {
9689 if (!lvalue_p (depobj))
9690 {
9691 error_at (EXPR_LOC_OR_LOC (depobj, loc),
9692 "%<depobj%> expression is not lvalue expression");
9693 depobj = error_mark_node;
9694 }
9695 }
9696
9697 if (processing_template_decl)
9698 {
9699 if (clause == NULL_TREE)
9700 clause = build_int_cst (integer_type_node, kind);
9701 add_stmt (build_min_nt_loc (loc, OMP_DEPOBJ, depobj, clause));
9702 return;
9703 }
9704
9705 if (!error_operand_p (depobj))
9706 {
9707 tree addr = cp_build_addr_expr (depobj, tf_warning_or_error);
9708 if (addr == error_mark_node)
9709 depobj = error_mark_node;
9710 else
9711 depobj = cp_build_indirect_ref (loc, addr, RO_UNARY_STAR,
9712 tf_warning_or_error);
9713 }
9714
9715 c_finish_omp_depobj (loc, depobj, kind, clause);
9716 }
9717
9718 void
9719 finish_omp_flush (int mo)
9720 {
9721 tree fn = builtin_decl_explicit (BUILT_IN_SYNC_SYNCHRONIZE);
9722 releasing_vec vec;
9723 if (mo != MEMMODEL_LAST)
9724 {
9725 fn = builtin_decl_explicit (BUILT_IN_ATOMIC_THREAD_FENCE);
9726 vec->quick_push (build_int_cst (integer_type_node, mo));
9727 }
9728 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
9729 finish_expr_stmt (stmt);
9730 }
9731
9732 void
9733 finish_omp_taskwait (void)
9734 {
9735 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_TASKWAIT);
9736 releasing_vec vec;
9737 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
9738 finish_expr_stmt (stmt);
9739 }
9740
9741 void
9742 finish_omp_taskyield (void)
9743 {
9744 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_TASKYIELD);
9745 releasing_vec vec;
9746 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
9747 finish_expr_stmt (stmt);
9748 }
9749
9750 void
9751 finish_omp_cancel (tree clauses)
9752 {
9753 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
9754 int mask = 0;
9755 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
9756 mask = 1;
9757 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
9758 mask = 2;
9759 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
9760 mask = 4;
9761 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
9762 mask = 8;
9763 else
9764 {
9765 error ("%<#pragma omp cancel%> must specify one of "
9766 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> clauses");
9767 return;
9768 }
9769 releasing_vec vec;
9770 tree ifc = omp_find_clause (clauses, OMP_CLAUSE_IF);
9771 if (ifc != NULL_TREE)
9772 {
9773 if (OMP_CLAUSE_IF_MODIFIER (ifc) != ERROR_MARK
9774 && OMP_CLAUSE_IF_MODIFIER (ifc) != VOID_CST)
9775 error_at (OMP_CLAUSE_LOCATION (ifc),
9776 "expected %<cancel%> %<if%> clause modifier");
9777 else
9778 {
9779 tree ifc2 = omp_find_clause (OMP_CLAUSE_CHAIN (ifc), OMP_CLAUSE_IF);
9780 if (ifc2 != NULL_TREE)
9781 {
9782 gcc_assert (OMP_CLAUSE_IF_MODIFIER (ifc) == VOID_CST
9783 && OMP_CLAUSE_IF_MODIFIER (ifc2) != ERROR_MARK
9784 && OMP_CLAUSE_IF_MODIFIER (ifc2) != VOID_CST);
9785 error_at (OMP_CLAUSE_LOCATION (ifc2),
9786 "expected %<cancel%> %<if%> clause modifier");
9787 }
9788 }
9789
9790 if (!processing_template_decl)
9791 ifc = maybe_convert_cond (OMP_CLAUSE_IF_EXPR (ifc));
9792 else
9793 ifc = build_x_binary_op (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
9794 OMP_CLAUSE_IF_EXPR (ifc), ERROR_MARK,
9795 integer_zero_node, ERROR_MARK,
9796 NULL, tf_warning_or_error);
9797 }
9798 else
9799 ifc = boolean_true_node;
9800 vec->quick_push (build_int_cst (integer_type_node, mask));
9801 vec->quick_push (ifc);
9802 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
9803 finish_expr_stmt (stmt);
9804 }
9805
9806 void
9807 finish_omp_cancellation_point (tree clauses)
9808 {
9809 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
9810 int mask = 0;
9811 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
9812 mask = 1;
9813 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
9814 mask = 2;
9815 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
9816 mask = 4;
9817 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
9818 mask = 8;
9819 else
9820 {
9821 error ("%<#pragma omp cancellation point%> must specify one of "
9822 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> clauses");
9823 return;
9824 }
9825 releasing_vec vec
9826 = make_tree_vector_single (build_int_cst (integer_type_node, mask));
9827 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
9828 finish_expr_stmt (stmt);
9829 }
9830 \f
9831 /* Begin a __transaction_atomic or __transaction_relaxed statement.
9832 If PCOMPOUND is non-null, this is for a function-transaction-block, and we
9833 should create an extra compound stmt. */
9834
9835 tree
9836 begin_transaction_stmt (location_t loc, tree *pcompound, int flags)
9837 {
9838 tree r;
9839
9840 if (pcompound)
9841 *pcompound = begin_compound_stmt (0);
9842
9843 r = build_stmt (loc, TRANSACTION_EXPR, NULL_TREE);
9844
9845 /* Only add the statement to the function if support enabled. */
9846 if (flag_tm)
9847 add_stmt (r);
9848 else
9849 error_at (loc, ((flags & TM_STMT_ATTR_RELAXED) != 0
9850 ? G_("%<__transaction_relaxed%> without "
9851 "transactional memory support enabled")
9852 : G_("%<__transaction_atomic%> without "
9853 "transactional memory support enabled")));
9854
9855 TRANSACTION_EXPR_BODY (r) = push_stmt_list ();
9856 TREE_SIDE_EFFECTS (r) = 1;
9857 return r;
9858 }
9859
9860 /* End a __transaction_atomic or __transaction_relaxed statement.
9861 If COMPOUND_STMT is non-null, this is for a function-transaction-block,
9862 and we should end the compound. If NOEX is non-NULL, we wrap the body in
9863 a MUST_NOT_THROW_EXPR with NOEX as condition. */
9864
9865 void
9866 finish_transaction_stmt (tree stmt, tree compound_stmt, int flags, tree noex)
9867 {
9868 TRANSACTION_EXPR_BODY (stmt) = pop_stmt_list (TRANSACTION_EXPR_BODY (stmt));
9869 TRANSACTION_EXPR_OUTER (stmt) = (flags & TM_STMT_ATTR_OUTER) != 0;
9870 TRANSACTION_EXPR_RELAXED (stmt) = (flags & TM_STMT_ATTR_RELAXED) != 0;
9871 TRANSACTION_EXPR_IS_STMT (stmt) = 1;
9872
9873 /* noexcept specifications are not allowed for function transactions. */
9874 gcc_assert (!(noex && compound_stmt));
9875 if (noex)
9876 {
9877 tree body = build_must_not_throw_expr (TRANSACTION_EXPR_BODY (stmt),
9878 noex);
9879 protected_set_expr_location
9880 (body, EXPR_LOCATION (TRANSACTION_EXPR_BODY (stmt)));
9881 TREE_SIDE_EFFECTS (body) = 1;
9882 TRANSACTION_EXPR_BODY (stmt) = body;
9883 }
9884
9885 if (compound_stmt)
9886 finish_compound_stmt (compound_stmt);
9887 }
9888
9889 /* Build a __transaction_atomic or __transaction_relaxed expression. If
9890 NOEX is non-NULL, we wrap the body in a MUST_NOT_THROW_EXPR with NOEX as
9891 condition. */
9892
9893 tree
9894 build_transaction_expr (location_t loc, tree expr, int flags, tree noex)
9895 {
9896 tree ret;
9897 if (noex)
9898 {
9899 expr = build_must_not_throw_expr (expr, noex);
9900 protected_set_expr_location (expr, loc);
9901 TREE_SIDE_EFFECTS (expr) = 1;
9902 }
9903 ret = build1 (TRANSACTION_EXPR, TREE_TYPE (expr), expr);
9904 if (flags & TM_STMT_ATTR_RELAXED)
9905 TRANSACTION_EXPR_RELAXED (ret) = 1;
9906 TREE_SIDE_EFFECTS (ret) = 1;
9907 SET_EXPR_LOCATION (ret, loc);
9908 return ret;
9909 }
9910 \f
9911 void
9912 init_cp_semantics (void)
9913 {
9914 }
9915 \f
9916
9917 /* If we have a condition in conjunctive normal form (CNF), find the first
9918 failing clause. In other words, given an expression like
9919
9920 true && true && false && true && false
9921
9922 return the first 'false'. EXPR is the expression. */
9923
9924 static tree
9925 find_failing_clause_r (tree expr)
9926 {
9927 if (TREE_CODE (expr) == TRUTH_ANDIF_EXPR)
9928 {
9929 /* First check the left side... */
9930 tree e = find_failing_clause_r (TREE_OPERAND (expr, 0));
9931 if (e == NULL_TREE)
9932 /* ...if we didn't find a false clause, check the right side. */
9933 e = find_failing_clause_r (TREE_OPERAND (expr, 1));
9934 return e;
9935 }
9936 tree e = contextual_conv_bool (expr, tf_none);
9937 e = fold_non_dependent_expr (e, tf_none, /*manifestly_const_eval=*/true);
9938 if (integer_zerop (e))
9939 /* This is the failing clause. */
9940 return expr;
9941 return NULL_TREE;
9942 }
9943
9944 /* Wrapper for find_failing_clause_r. */
9945
9946 static tree
9947 find_failing_clause (tree expr)
9948 {
9949 if (TREE_CODE (expr) != TRUTH_ANDIF_EXPR)
9950 return NULL_TREE;
9951 return find_failing_clause_r (expr);
9952 }
9953
9954 /* Build a STATIC_ASSERT for a static assertion with the condition
9955 CONDITION and the message text MESSAGE. LOCATION is the location
9956 of the static assertion in the source code. When MEMBER_P, this
9957 static assertion is a member of a class. If SHOW_EXPR_P is true,
9958 print the condition (because it was instantiation-dependent). */
9959
9960 void
9961 finish_static_assert (tree condition, tree message, location_t location,
9962 bool member_p, bool show_expr_p)
9963 {
9964 tsubst_flags_t complain = tf_warning_or_error;
9965
9966 if (message == NULL_TREE
9967 || message == error_mark_node
9968 || condition == NULL_TREE
9969 || condition == error_mark_node)
9970 return;
9971
9972 if (check_for_bare_parameter_packs (condition))
9973 condition = error_mark_node;
9974
9975 if (instantiation_dependent_expression_p (condition))
9976 {
9977 /* We're in a template; build a STATIC_ASSERT and put it in
9978 the right place. */
9979 tree assertion;
9980
9981 assertion = make_node (STATIC_ASSERT);
9982 STATIC_ASSERT_CONDITION (assertion) = condition;
9983 STATIC_ASSERT_MESSAGE (assertion) = message;
9984 STATIC_ASSERT_SOURCE_LOCATION (assertion) = location;
9985
9986 if (member_p)
9987 maybe_add_class_template_decl_list (current_class_type,
9988 assertion,
9989 /*friend_p=*/0);
9990 else
9991 add_stmt (assertion);
9992
9993 return;
9994 }
9995
9996 /* Save the condition in case it was a concept check. */
9997 tree orig_condition = condition;
9998
9999 /* Fold the expression and convert it to a boolean value. */
10000 condition = contextual_conv_bool (condition, complain);
10001 condition = fold_non_dependent_expr (condition, complain,
10002 /*manifestly_const_eval=*/true);
10003
10004 if (TREE_CODE (condition) == INTEGER_CST && !integer_zerop (condition))
10005 /* Do nothing; the condition is satisfied. */
10006 ;
10007 else
10008 {
10009 iloc_sentinel ils (location);
10010
10011 if (integer_zerop (condition))
10012 {
10013 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
10014 (TREE_TYPE (TREE_TYPE (message))));
10015 int len = TREE_STRING_LENGTH (message) / sz - 1;
10016
10017 /* See if we can find which clause was failing (for logical AND). */
10018 tree bad = find_failing_clause (orig_condition);
10019 /* If not, or its location is unusable, fall back to the previous
10020 location. */
10021 location_t cloc = location;
10022 if (cp_expr_location (bad) != UNKNOWN_LOCATION)
10023 cloc = cp_expr_location (bad);
10024
10025 /* Report the error. */
10026 if (len == 0)
10027 error_at (cloc, "static assertion failed");
10028 else
10029 error_at (cloc, "static assertion failed: %s",
10030 TREE_STRING_POINTER (message));
10031 if (show_expr_p)
10032 inform (cloc, "%qE evaluates to false",
10033 /* Nobody wants to see the artificial (bool) cast. */
10034 (bad ? tree_strip_nop_conversions (bad) : orig_condition));
10035
10036 /* Actually explain the failure if this is a concept check or a
10037 requires-expression. */
10038 if (concept_check_p (orig_condition)
10039 || TREE_CODE (orig_condition) == REQUIRES_EXPR)
10040 diagnose_constraints (location, orig_condition, NULL_TREE);
10041 }
10042 else if (condition && condition != error_mark_node)
10043 {
10044 error ("non-constant condition for static assertion");
10045 if (require_rvalue_constant_expression (condition))
10046 cxx_constant_value (condition);
10047 }
10048 }
10049 }
10050 \f
10051 /* Implements the C++0x decltype keyword. Returns the type of EXPR,
10052 suitable for use as a type-specifier.
10053
10054 ID_EXPRESSION_OR_MEMBER_ACCESS_P is true when EXPR was parsed as an
10055 id-expression or a class member access, FALSE when it was parsed as
10056 a full expression. */
10057
10058 tree
10059 finish_decltype_type (tree expr, bool id_expression_or_member_access_p,
10060 tsubst_flags_t complain)
10061 {
10062 tree type = NULL_TREE;
10063
10064 if (!expr || error_operand_p (expr))
10065 return error_mark_node;
10066
10067 if (TYPE_P (expr)
10068 || TREE_CODE (expr) == TYPE_DECL
10069 || (TREE_CODE (expr) == BIT_NOT_EXPR
10070 && TYPE_P (TREE_OPERAND (expr, 0))))
10071 {
10072 if (complain & tf_error)
10073 error ("argument to %<decltype%> must be an expression");
10074 return error_mark_node;
10075 }
10076
10077 /* Depending on the resolution of DR 1172, we may later need to distinguish
10078 instantiation-dependent but not type-dependent expressions so that, say,
10079 A<decltype(sizeof(T))>::U doesn't require 'typename'. */
10080 if (instantiation_dependent_uneval_expression_p (expr))
10081 {
10082 type = cxx_make_type (DECLTYPE_TYPE);
10083 DECLTYPE_TYPE_EXPR (type) = expr;
10084 DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (type)
10085 = id_expression_or_member_access_p;
10086 SET_TYPE_STRUCTURAL_EQUALITY (type);
10087
10088 return type;
10089 }
10090 else if (processing_template_decl)
10091 {
10092 ++cp_unevaluated_operand;
10093 expr = instantiate_non_dependent_expr_sfinae (expr, complain);
10094 --cp_unevaluated_operand;
10095 if (expr == error_mark_node)
10096 return error_mark_node;
10097 }
10098
10099 /* The type denoted by decltype(e) is defined as follows: */
10100
10101 expr = resolve_nondeduced_context (expr, complain);
10102
10103 if (invalid_nonstatic_memfn_p (input_location, expr, complain))
10104 return error_mark_node;
10105
10106 if (type_unknown_p (expr))
10107 {
10108 if (complain & tf_error)
10109 error ("%<decltype%> cannot resolve address of overloaded function");
10110 return error_mark_node;
10111 }
10112
10113 /* To get the size of a static data member declared as an array of
10114 unknown bound, we need to instantiate it. */
10115 if (VAR_P (expr)
10116 && VAR_HAD_UNKNOWN_BOUND (expr)
10117 && DECL_TEMPLATE_INSTANTIATION (expr))
10118 instantiate_decl (expr, /*defer_ok*/true, /*expl_inst_mem*/false);
10119
10120 if (id_expression_or_member_access_p)
10121 {
10122 /* If e is an id-expression or a class member access (5.2.5
10123 [expr.ref]), decltype(e) is defined as the type of the entity
10124 named by e. If there is no such entity, or e names a set of
10125 overloaded functions, the program is ill-formed. */
10126 if (identifier_p (expr))
10127 expr = lookup_name (expr);
10128
10129 if (INDIRECT_REF_P (expr)
10130 || TREE_CODE (expr) == VIEW_CONVERT_EXPR)
10131 /* This can happen when the expression is, e.g., "a.b". Just
10132 look at the underlying operand. */
10133 expr = TREE_OPERAND (expr, 0);
10134
10135 if (TREE_CODE (expr) == OFFSET_REF
10136 || TREE_CODE (expr) == MEMBER_REF
10137 || TREE_CODE (expr) == SCOPE_REF)
10138 /* We're only interested in the field itself. If it is a
10139 BASELINK, we will need to see through it in the next
10140 step. */
10141 expr = TREE_OPERAND (expr, 1);
10142
10143 if (BASELINK_P (expr))
10144 /* See through BASELINK nodes to the underlying function. */
10145 expr = BASELINK_FUNCTIONS (expr);
10146
10147 /* decltype of a decomposition name drops references in the tuple case
10148 (unlike decltype of a normal variable) and keeps cv-qualifiers from
10149 the containing object in the other cases (unlike decltype of a member
10150 access expression). */
10151 if (DECL_DECOMPOSITION_P (expr))
10152 {
10153 if (DECL_HAS_VALUE_EXPR_P (expr))
10154 /* Expr is an array or struct subobject proxy, handle
10155 bit-fields properly. */
10156 return unlowered_expr_type (expr);
10157 else
10158 /* Expr is a reference variable for the tuple case. */
10159 return lookup_decomp_type (expr);
10160 }
10161
10162 switch (TREE_CODE (expr))
10163 {
10164 case FIELD_DECL:
10165 if (DECL_BIT_FIELD_TYPE (expr))
10166 {
10167 type = DECL_BIT_FIELD_TYPE (expr);
10168 break;
10169 }
10170 /* Fall through for fields that aren't bitfields. */
10171 gcc_fallthrough ();
10172
10173 case FUNCTION_DECL:
10174 case VAR_DECL:
10175 case CONST_DECL:
10176 case PARM_DECL:
10177 case RESULT_DECL:
10178 case TEMPLATE_PARM_INDEX:
10179 expr = mark_type_use (expr);
10180 type = TREE_TYPE (expr);
10181 break;
10182
10183 case ERROR_MARK:
10184 type = error_mark_node;
10185 break;
10186
10187 case COMPONENT_REF:
10188 case COMPOUND_EXPR:
10189 mark_type_use (expr);
10190 type = is_bitfield_expr_with_lowered_type (expr);
10191 if (!type)
10192 type = TREE_TYPE (TREE_OPERAND (expr, 1));
10193 break;
10194
10195 case BIT_FIELD_REF:
10196 gcc_unreachable ();
10197
10198 case INTEGER_CST:
10199 case PTRMEM_CST:
10200 /* We can get here when the id-expression refers to an
10201 enumerator or non-type template parameter. */
10202 type = TREE_TYPE (expr);
10203 break;
10204
10205 default:
10206 /* Handle instantiated template non-type arguments. */
10207 type = TREE_TYPE (expr);
10208 break;
10209 }
10210 }
10211 else
10212 {
10213 /* Within a lambda-expression:
10214
10215 Every occurrence of decltype((x)) where x is a possibly
10216 parenthesized id-expression that names an entity of
10217 automatic storage duration is treated as if x were
10218 transformed into an access to a corresponding data member
10219 of the closure type that would have been declared if x
10220 were a use of the denoted entity. */
10221 if (outer_automatic_var_p (expr)
10222 && current_function_decl
10223 && LAMBDA_FUNCTION_P (current_function_decl))
10224 type = capture_decltype (expr);
10225 else if (error_operand_p (expr))
10226 type = error_mark_node;
10227 else if (expr == current_class_ptr)
10228 /* If the expression is just "this", we want the
10229 cv-unqualified pointer for the "this" type. */
10230 type = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
10231 else
10232 {
10233 /* Otherwise, where T is the type of e, if e is an lvalue,
10234 decltype(e) is defined as T&; if an xvalue, T&&; otherwise, T. */
10235 cp_lvalue_kind clk = lvalue_kind (expr);
10236 type = unlowered_expr_type (expr);
10237 gcc_assert (!TYPE_REF_P (type));
10238
10239 /* For vector types, pick a non-opaque variant. */
10240 if (VECTOR_TYPE_P (type))
10241 type = strip_typedefs (type);
10242
10243 if (clk != clk_none && !(clk & clk_class))
10244 type = cp_build_reference_type (type, (clk & clk_rvalueref));
10245 }
10246 }
10247
10248 return type;
10249 }
10250
10251 /* Called from trait_expr_value to evaluate either __has_nothrow_assign or
10252 __has_nothrow_copy, depending on assign_p. Returns true iff all
10253 the copy {ctor,assign} fns are nothrow. */
10254
10255 static bool
10256 classtype_has_nothrow_assign_or_copy_p (tree type, bool assign_p)
10257 {
10258 tree fns = NULL_TREE;
10259
10260 if (assign_p || TYPE_HAS_COPY_CTOR (type))
10261 fns = get_class_binding (type, assign_p ? assign_op_identifier
10262 : ctor_identifier);
10263
10264 bool saw_copy = false;
10265 for (ovl_iterator iter (fns); iter; ++iter)
10266 {
10267 tree fn = *iter;
10268
10269 if (copy_fn_p (fn) > 0)
10270 {
10271 saw_copy = true;
10272 if (!maybe_instantiate_noexcept (fn)
10273 || !TYPE_NOTHROW_P (TREE_TYPE (fn)))
10274 return false;
10275 }
10276 }
10277
10278 return saw_copy;
10279 }
10280
10281 /* Actually evaluates the trait. */
10282
10283 static bool
10284 trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
10285 {
10286 enum tree_code type_code1;
10287 tree t;
10288
10289 type_code1 = TREE_CODE (type1);
10290
10291 switch (kind)
10292 {
10293 case CPTK_HAS_NOTHROW_ASSIGN:
10294 type1 = strip_array_types (type1);
10295 return (!CP_TYPE_CONST_P (type1) && type_code1 != REFERENCE_TYPE
10296 && (trait_expr_value (CPTK_HAS_TRIVIAL_ASSIGN, type1, type2)
10297 || (CLASS_TYPE_P (type1)
10298 && classtype_has_nothrow_assign_or_copy_p (type1,
10299 true))));
10300
10301 case CPTK_HAS_TRIVIAL_ASSIGN:
10302 /* ??? The standard seems to be missing the "or array of such a class
10303 type" wording for this trait. */
10304 type1 = strip_array_types (type1);
10305 return (!CP_TYPE_CONST_P (type1) && type_code1 != REFERENCE_TYPE
10306 && (trivial_type_p (type1)
10307 || (CLASS_TYPE_P (type1)
10308 && TYPE_HAS_TRIVIAL_COPY_ASSIGN (type1))));
10309
10310 case CPTK_HAS_NOTHROW_CONSTRUCTOR:
10311 type1 = strip_array_types (type1);
10312 return (trait_expr_value (CPTK_HAS_TRIVIAL_CONSTRUCTOR, type1, type2)
10313 || (CLASS_TYPE_P (type1)
10314 && (t = locate_ctor (type1))
10315 && maybe_instantiate_noexcept (t)
10316 && TYPE_NOTHROW_P (TREE_TYPE (t))));
10317
10318 case CPTK_HAS_TRIVIAL_CONSTRUCTOR:
10319 type1 = strip_array_types (type1);
10320 return (trivial_type_p (type1)
10321 || (CLASS_TYPE_P (type1) && TYPE_HAS_TRIVIAL_DFLT (type1)));
10322
10323 case CPTK_HAS_NOTHROW_COPY:
10324 type1 = strip_array_types (type1);
10325 return (trait_expr_value (CPTK_HAS_TRIVIAL_COPY, type1, type2)
10326 || (CLASS_TYPE_P (type1)
10327 && classtype_has_nothrow_assign_or_copy_p (type1, false)));
10328
10329 case CPTK_HAS_TRIVIAL_COPY:
10330 /* ??? The standard seems to be missing the "or array of such a class
10331 type" wording for this trait. */
10332 type1 = strip_array_types (type1);
10333 return (trivial_type_p (type1) || type_code1 == REFERENCE_TYPE
10334 || (CLASS_TYPE_P (type1) && TYPE_HAS_TRIVIAL_COPY_CTOR (type1)));
10335
10336 case CPTK_HAS_TRIVIAL_DESTRUCTOR:
10337 type1 = strip_array_types (type1);
10338 return (trivial_type_p (type1) || type_code1 == REFERENCE_TYPE
10339 || (CLASS_TYPE_P (type1)
10340 && TYPE_HAS_TRIVIAL_DESTRUCTOR (type1)));
10341
10342 case CPTK_HAS_VIRTUAL_DESTRUCTOR:
10343 return type_has_virtual_destructor (type1);
10344
10345 case CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS:
10346 return type_has_unique_obj_representations (type1);
10347
10348 case CPTK_IS_ABSTRACT:
10349 return ABSTRACT_CLASS_TYPE_P (type1);
10350
10351 case CPTK_IS_AGGREGATE:
10352 return CP_AGGREGATE_TYPE_P (type1);
10353
10354 case CPTK_IS_BASE_OF:
10355 return (NON_UNION_CLASS_TYPE_P (type1) && NON_UNION_CLASS_TYPE_P (type2)
10356 && (same_type_ignoring_top_level_qualifiers_p (type1, type2)
10357 || DERIVED_FROM_P (type1, type2)));
10358
10359 case CPTK_IS_CLASS:
10360 return NON_UNION_CLASS_TYPE_P (type1);
10361
10362 case CPTK_IS_EMPTY:
10363 return NON_UNION_CLASS_TYPE_P (type1) && CLASSTYPE_EMPTY_P (type1);
10364
10365 case CPTK_IS_ENUM:
10366 return type_code1 == ENUMERAL_TYPE;
10367
10368 case CPTK_IS_FINAL:
10369 return CLASS_TYPE_P (type1) && CLASSTYPE_FINAL (type1);
10370
10371 case CPTK_IS_LITERAL_TYPE:
10372 return literal_type_p (type1);
10373
10374 case CPTK_IS_POD:
10375 return pod_type_p (type1);
10376
10377 case CPTK_IS_POLYMORPHIC:
10378 return CLASS_TYPE_P (type1) && TYPE_POLYMORPHIC_P (type1);
10379
10380 case CPTK_IS_SAME_AS:
10381 return same_type_p (type1, type2);
10382
10383 case CPTK_IS_STD_LAYOUT:
10384 return std_layout_type_p (type1);
10385
10386 case CPTK_IS_TRIVIAL:
10387 return trivial_type_p (type1);
10388
10389 case CPTK_IS_TRIVIALLY_ASSIGNABLE:
10390 return is_trivially_xible (MODIFY_EXPR, type1, type2);
10391
10392 case CPTK_IS_TRIVIALLY_CONSTRUCTIBLE:
10393 return is_trivially_xible (INIT_EXPR, type1, type2);
10394
10395 case CPTK_IS_TRIVIALLY_COPYABLE:
10396 return trivially_copyable_p (type1);
10397
10398 case CPTK_IS_UNION:
10399 return type_code1 == UNION_TYPE;
10400
10401 case CPTK_IS_ASSIGNABLE:
10402 return is_xible (MODIFY_EXPR, type1, type2);
10403
10404 case CPTK_IS_CONSTRUCTIBLE:
10405 return is_xible (INIT_EXPR, type1, type2);
10406
10407 case CPTK_IS_NOTHROW_ASSIGNABLE:
10408 return is_nothrow_xible (MODIFY_EXPR, type1, type2);
10409
10410 case CPTK_IS_NOTHROW_CONSTRUCTIBLE:
10411 return is_nothrow_xible (INIT_EXPR, type1, type2);
10412
10413 default:
10414 gcc_unreachable ();
10415 return false;
10416 }
10417 }
10418
10419 /* If TYPE is an array of unknown bound, or (possibly cv-qualified)
10420 void, or a complete type, returns true, otherwise false. */
10421
10422 static bool
10423 check_trait_type (tree type)
10424 {
10425 if (type == NULL_TREE)
10426 return true;
10427
10428 if (TREE_CODE (type) == TREE_LIST)
10429 return (check_trait_type (TREE_VALUE (type))
10430 && check_trait_type (TREE_CHAIN (type)));
10431
10432 if (TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type)
10433 && COMPLETE_TYPE_P (TREE_TYPE (type)))
10434 return true;
10435
10436 if (VOID_TYPE_P (type))
10437 return true;
10438
10439 return !!complete_type_or_else (strip_array_types (type), NULL_TREE);
10440 }
10441
10442 /* Process a trait expression. */
10443
10444 tree
10445 finish_trait_expr (location_t loc, cp_trait_kind kind, tree type1, tree type2)
10446 {
10447 if (type1 == error_mark_node
10448 || type2 == error_mark_node)
10449 return error_mark_node;
10450
10451 if (processing_template_decl)
10452 {
10453 tree trait_expr = make_node (TRAIT_EXPR);
10454 TREE_TYPE (trait_expr) = boolean_type_node;
10455 TRAIT_EXPR_TYPE1 (trait_expr) = type1;
10456 TRAIT_EXPR_TYPE2 (trait_expr) = type2;
10457 TRAIT_EXPR_KIND (trait_expr) = kind;
10458 TRAIT_EXPR_LOCATION (trait_expr) = loc;
10459 return trait_expr;
10460 }
10461
10462 switch (kind)
10463 {
10464 case CPTK_HAS_NOTHROW_ASSIGN:
10465 case CPTK_HAS_TRIVIAL_ASSIGN:
10466 case CPTK_HAS_NOTHROW_CONSTRUCTOR:
10467 case CPTK_HAS_TRIVIAL_CONSTRUCTOR:
10468 case CPTK_HAS_NOTHROW_COPY:
10469 case CPTK_HAS_TRIVIAL_COPY:
10470 case CPTK_HAS_TRIVIAL_DESTRUCTOR:
10471 case CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS:
10472 case CPTK_HAS_VIRTUAL_DESTRUCTOR:
10473 case CPTK_IS_ABSTRACT:
10474 case CPTK_IS_AGGREGATE:
10475 case CPTK_IS_EMPTY:
10476 case CPTK_IS_FINAL:
10477 case CPTK_IS_LITERAL_TYPE:
10478 case CPTK_IS_POD:
10479 case CPTK_IS_POLYMORPHIC:
10480 case CPTK_IS_STD_LAYOUT:
10481 case CPTK_IS_TRIVIAL:
10482 case CPTK_IS_TRIVIALLY_COPYABLE:
10483 if (!check_trait_type (type1))
10484 return error_mark_node;
10485 break;
10486
10487 case CPTK_IS_ASSIGNABLE:
10488 case CPTK_IS_CONSTRUCTIBLE:
10489 break;
10490
10491 case CPTK_IS_TRIVIALLY_ASSIGNABLE:
10492 case CPTK_IS_TRIVIALLY_CONSTRUCTIBLE:
10493 case CPTK_IS_NOTHROW_ASSIGNABLE:
10494 case CPTK_IS_NOTHROW_CONSTRUCTIBLE:
10495 if (!check_trait_type (type1)
10496 || !check_trait_type (type2))
10497 return error_mark_node;
10498 break;
10499
10500 case CPTK_IS_BASE_OF:
10501 if (NON_UNION_CLASS_TYPE_P (type1) && NON_UNION_CLASS_TYPE_P (type2)
10502 && !same_type_ignoring_top_level_qualifiers_p (type1, type2)
10503 && !complete_type_or_else (type2, NULL_TREE))
10504 /* We already issued an error. */
10505 return error_mark_node;
10506 break;
10507
10508 case CPTK_IS_CLASS:
10509 case CPTK_IS_ENUM:
10510 case CPTK_IS_UNION:
10511 case CPTK_IS_SAME_AS:
10512 break;
10513
10514 default:
10515 gcc_unreachable ();
10516 }
10517
10518 tree val = (trait_expr_value (kind, type1, type2)
10519 ? boolean_true_node : boolean_false_node);
10520 return maybe_wrap_with_location (val, loc);
10521 }
10522
10523 /* Do-nothing variants of functions to handle pragma FLOAT_CONST_DECIMAL64,
10524 which is ignored for C++. */
10525
10526 void
10527 set_float_const_decimal64 (void)
10528 {
10529 }
10530
10531 void
10532 clear_float_const_decimal64 (void)
10533 {
10534 }
10535
10536 bool
10537 float_const_decimal64_p (void)
10538 {
10539 return 0;
10540 }
10541
10542 \f
10543 /* Return true if T designates the implied `this' parameter. */
10544
10545 bool
10546 is_this_parameter (tree t)
10547 {
10548 if (!DECL_P (t) || DECL_NAME (t) != this_identifier)
10549 return false;
10550 gcc_assert (TREE_CODE (t) == PARM_DECL || is_capture_proxy (t)
10551 || (cp_binding_oracle && TREE_CODE (t) == VAR_DECL));
10552 return true;
10553 }
10554
10555 /* Insert the deduced return type for an auto function. */
10556
10557 void
10558 apply_deduced_return_type (tree fco, tree return_type)
10559 {
10560 tree result;
10561
10562 if (return_type == error_mark_node)
10563 return;
10564
10565 if (DECL_CONV_FN_P (fco))
10566 DECL_NAME (fco) = make_conv_op_name (return_type);
10567
10568 TREE_TYPE (fco) = change_return_type (return_type, TREE_TYPE (fco));
10569
10570 result = DECL_RESULT (fco);
10571 if (result == NULL_TREE)
10572 return;
10573 if (TREE_TYPE (result) == return_type)
10574 return;
10575
10576 if (!processing_template_decl && !VOID_TYPE_P (return_type)
10577 && !complete_type_or_else (return_type, NULL_TREE))
10578 return;
10579
10580 /* We already have a DECL_RESULT from start_preparsed_function.
10581 Now we need to redo the work it and allocate_struct_function
10582 did to reflect the new type. */
10583 gcc_assert (current_function_decl == fco);
10584 result = build_decl (input_location, RESULT_DECL, NULL_TREE,
10585 TYPE_MAIN_VARIANT (return_type));
10586 DECL_ARTIFICIAL (result) = 1;
10587 DECL_IGNORED_P (result) = 1;
10588 cp_apply_type_quals_to_decl (cp_type_quals (return_type),
10589 result);
10590
10591 DECL_RESULT (fco) = result;
10592
10593 if (!processing_template_decl)
10594 {
10595 bool aggr = aggregate_value_p (result, fco);
10596 #ifdef PCC_STATIC_STRUCT_RETURN
10597 cfun->returns_pcc_struct = aggr;
10598 #endif
10599 cfun->returns_struct = aggr;
10600 }
10601 }
10602
10603 /* DECL is a local variable or parameter from the surrounding scope of a
10604 lambda-expression. Returns the decltype for a use of the capture field
10605 for DECL even if it hasn't been captured yet. */
10606
10607 static tree
10608 capture_decltype (tree decl)
10609 {
10610 tree lam = CLASSTYPE_LAMBDA_EXPR (DECL_CONTEXT (current_function_decl));
10611 tree cap = lookup_name (DECL_NAME (decl), LOOK_where::BLOCK,
10612 LOOK_want::HIDDEN_LAMBDA);
10613 tree type;
10614
10615 if (cap && is_capture_proxy (cap))
10616 type = TREE_TYPE (cap);
10617 else
10618 switch (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lam))
10619 {
10620 case CPLD_NONE:
10621 error ("%qD is not captured", decl);
10622 return error_mark_node;
10623
10624 case CPLD_COPY:
10625 type = TREE_TYPE (decl);
10626 if (TYPE_REF_P (type)
10627 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
10628 type = TREE_TYPE (type);
10629 break;
10630
10631 case CPLD_REFERENCE:
10632 type = TREE_TYPE (decl);
10633 if (!TYPE_REF_P (type))
10634 type = build_reference_type (TREE_TYPE (decl));
10635 break;
10636
10637 default:
10638 gcc_unreachable ();
10639 }
10640
10641 if (!TYPE_REF_P (type))
10642 {
10643 if (!LAMBDA_EXPR_MUTABLE_P (lam))
10644 type = cp_build_qualified_type (type, (cp_type_quals (type)
10645 |TYPE_QUAL_CONST));
10646 type = build_reference_type (type);
10647 }
10648 return type;
10649 }
10650
10651 /* Build a unary fold expression of EXPR over OP. If IS_RIGHT is true,
10652 this is a right unary fold. Otherwise it is a left unary fold. */
10653
10654 static tree
10655 finish_unary_fold_expr (tree expr, int op, tree_code dir)
10656 {
10657 /* Build a pack expansion (assuming expr has pack type). */
10658 if (!uses_parameter_packs (expr))
10659 {
10660 error_at (location_of (expr), "operand of fold expression has no "
10661 "unexpanded parameter packs");
10662 return error_mark_node;
10663 }
10664 tree pack = make_pack_expansion (expr);
10665
10666 /* Build the fold expression. */
10667 tree code = build_int_cstu (integer_type_node, abs (op));
10668 tree fold = build_min_nt_loc (UNKNOWN_LOCATION, dir, code, pack);
10669 FOLD_EXPR_MODIFY_P (fold) = (op < 0);
10670 return fold;
10671 }
10672
10673 tree
10674 finish_left_unary_fold_expr (tree expr, int op)
10675 {
10676 return finish_unary_fold_expr (expr, op, UNARY_LEFT_FOLD_EXPR);
10677 }
10678
10679 tree
10680 finish_right_unary_fold_expr (tree expr, int op)
10681 {
10682 return finish_unary_fold_expr (expr, op, UNARY_RIGHT_FOLD_EXPR);
10683 }
10684
10685 /* Build a binary fold expression over EXPR1 and EXPR2. The
10686 associativity of the fold is determined by EXPR1 and EXPR2 (whichever
10687 has an unexpanded parameter pack). */
10688
10689 tree
10690 finish_binary_fold_expr (tree pack, tree init, int op, tree_code dir)
10691 {
10692 pack = make_pack_expansion (pack);
10693 tree code = build_int_cstu (integer_type_node, abs (op));
10694 tree fold = build_min_nt_loc (UNKNOWN_LOCATION, dir, code, pack, init);
10695 FOLD_EXPR_MODIFY_P (fold) = (op < 0);
10696 return fold;
10697 }
10698
10699 tree
10700 finish_binary_fold_expr (tree expr1, tree expr2, int op)
10701 {
10702 // Determine which expr has an unexpanded parameter pack and
10703 // set the pack and initial term.
10704 bool pack1 = uses_parameter_packs (expr1);
10705 bool pack2 = uses_parameter_packs (expr2);
10706 if (pack1 && !pack2)
10707 return finish_binary_fold_expr (expr1, expr2, op, BINARY_RIGHT_FOLD_EXPR);
10708 else if (pack2 && !pack1)
10709 return finish_binary_fold_expr (expr2, expr1, op, BINARY_LEFT_FOLD_EXPR);
10710 else
10711 {
10712 if (pack1)
10713 error ("both arguments in binary fold have unexpanded parameter packs");
10714 else
10715 error ("no unexpanded parameter packs in binary fold");
10716 }
10717 return error_mark_node;
10718 }
10719
10720 /* Finish __builtin_launder (arg). */
10721
10722 tree
10723 finish_builtin_launder (location_t loc, tree arg, tsubst_flags_t complain)
10724 {
10725 tree orig_arg = arg;
10726 if (!type_dependent_expression_p (arg))
10727 arg = decay_conversion (arg, complain);
10728 if (error_operand_p (arg))
10729 return error_mark_node;
10730 if (!type_dependent_expression_p (arg)
10731 && !TYPE_PTR_P (TREE_TYPE (arg)))
10732 {
10733 error_at (loc, "non-pointer argument to %<__builtin_launder%>");
10734 return error_mark_node;
10735 }
10736 if (processing_template_decl)
10737 arg = orig_arg;
10738 return build_call_expr_internal_loc (loc, IFN_LAUNDER,
10739 TREE_TYPE (arg), 1, arg);
10740 }
10741
10742 /* Finish __builtin_convertvector (arg, type). */
10743
10744 tree
10745 cp_build_vec_convert (tree arg, location_t loc, tree type,
10746 tsubst_flags_t complain)
10747 {
10748 if (error_operand_p (type))
10749 return error_mark_node;
10750 if (error_operand_p (arg))
10751 return error_mark_node;
10752
10753 tree ret = NULL_TREE;
10754 if (!type_dependent_expression_p (arg) && !dependent_type_p (type))
10755 ret = c_build_vec_convert (cp_expr_loc_or_input_loc (arg),
10756 decay_conversion (arg, complain),
10757 loc, type, (complain & tf_error) != 0);
10758
10759 if (!processing_template_decl)
10760 return ret;
10761
10762 return build_call_expr_internal_loc (loc, IFN_VEC_CONVERT, type, 1, arg);
10763 }
10764
10765 /* Finish __builtin_bit_cast (type, arg). */
10766
10767 tree
10768 cp_build_bit_cast (location_t loc, tree type, tree arg,
10769 tsubst_flags_t complain)
10770 {
10771 if (error_operand_p (type))
10772 return error_mark_node;
10773 if (!dependent_type_p (type))
10774 {
10775 if (!complete_type_or_maybe_complain (type, NULL_TREE, complain))
10776 return error_mark_node;
10777 if (TREE_CODE (type) == ARRAY_TYPE)
10778 {
10779 /* std::bit_cast for destination ARRAY_TYPE is not possible,
10780 as functions may not return an array, so don't bother trying
10781 to support this (and then deal with VLAs etc.). */
10782 error_at (loc, "%<__builtin_bit_cast%> destination type %qT "
10783 "is an array type", type);
10784 return error_mark_node;
10785 }
10786 if (!trivially_copyable_p (type))
10787 {
10788 error_at (loc, "%<__builtin_bit_cast%> destination type %qT "
10789 "is not trivially copyable", type);
10790 return error_mark_node;
10791 }
10792 }
10793
10794 if (error_operand_p (arg))
10795 return error_mark_node;
10796
10797 if (!type_dependent_expression_p (arg))
10798 {
10799 if (TREE_CODE (TREE_TYPE (arg)) == ARRAY_TYPE)
10800 {
10801 /* Don't perform array-to-pointer conversion. */
10802 arg = mark_rvalue_use (arg, loc, true);
10803 if (!complete_type_or_maybe_complain (TREE_TYPE (arg), arg, complain))
10804 return error_mark_node;
10805 }
10806 else
10807 arg = decay_conversion (arg, complain);
10808
10809 if (error_operand_p (arg))
10810 return error_mark_node;
10811
10812 if (!trivially_copyable_p (TREE_TYPE (arg)))
10813 {
10814 error_at (cp_expr_loc_or_loc (arg, loc),
10815 "%<__builtin_bit_cast%> source type %qT "
10816 "is not trivially copyable", TREE_TYPE (arg));
10817 return error_mark_node;
10818 }
10819 if (!dependent_type_p (type)
10820 && !cp_tree_equal (TYPE_SIZE_UNIT (type),
10821 TYPE_SIZE_UNIT (TREE_TYPE (arg))))
10822 {
10823 error_at (loc, "%<__builtin_bit_cast%> source size %qE "
10824 "not equal to destination type size %qE",
10825 TYPE_SIZE_UNIT (TREE_TYPE (arg)),
10826 TYPE_SIZE_UNIT (type));
10827 return error_mark_node;
10828 }
10829 }
10830
10831 tree ret = build_min (BIT_CAST_EXPR, type, arg);
10832 SET_EXPR_LOCATION (ret, loc);
10833
10834 if (!processing_template_decl && CLASS_TYPE_P (type))
10835 ret = get_target_expr_sfinae (ret, complain);
10836
10837 return ret;
10838 }
10839
10840 #include "gt-cp-semantics.h"