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