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