c++: Tweaks for -Wredundant-move [PR107363]
[official-gcc.git] / gcc / cp / semantics.cc
blob3c37d75bee6a3602654227dbfeec70aadfe0765b
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.
6 Copyright (C) 1998-2022 Free Software Foundation, Inc.
7 Written by Mark Mitchell (mmitchell@usa.net) based on code found
8 formerly in parse.y and pt.cc.
10 This file is part of GCC.
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.
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.
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/>. */
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"
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. */
54 static tree maybe_convert_cond (tree);
55 static tree finalize_nrv_r (tree *, int *, void *);
56 static tree capture_decltype (tree);
58 /* Used for OpenMP non-static data member privatization. */
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;
65 /* Deferred Access Checking Overview
66 ---------------------------------
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.
72 For member declarations, access checking has to be deferred
73 until more information about the declaration is known. For
74 example:
76 class A {
77 typedef int X;
78 public:
79 X f();
82 A::X A::f();
83 A::X g();
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.
88 Furthermore, some contexts require that access checking is
89 never performed at all. These include class heads, and template
90 instantiations.
92 Typical use of access checking functions is described here:
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).
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.
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.
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.
113 In case of parsing error, we simply call `pop_deferring_access_checks'
114 without `perform_deferred_access_checks'. */
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:
122 class A {
123 class B {};
124 B* f();
127 A::B* A::f() { return 0; }
129 is valid, even though `A::B' is not generally accessible. */
130 vec<deferred_access_check, va_gc> *deferred_access_checks;
132 /* The current mode of access checks. */
133 enum deferring_kind deferring_access_checks_kind;
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;
140 /* Save the current deferred access states and start deferred
141 access checking iff DEFER_P is true. */
143 void
144 push_deferring_access_checks (deferring_kind deferring)
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
152 deferred_access e = {NULL, deferring};
153 vec_safe_push (deferred_access_stack, e);
157 /* Save the current deferred access states and start deferred access
158 checking, continuing the set of deferred checks in CHECKS. */
160 void
161 reopen_deferring_access_checks (vec<deferred_access_check, va_gc> * checks)
163 push_deferring_access_checks (dk_deferred);
164 if (!deferred_access_no_check)
165 deferred_access_stack->last().deferred_access_checks = checks;
168 /* Resume deferring access checks again after we stopped doing
169 this previously. */
171 void
172 resume_deferring_access_checks (void)
174 if (!deferred_access_no_check)
175 deferred_access_stack->last().deferring_access_checks_kind = dk_deferred;
178 /* Stop deferring access checks. */
180 void
181 stop_deferring_access_checks (void)
183 if (!deferred_access_no_check)
184 deferred_access_stack->last().deferring_access_checks_kind = dk_no_deferred;
187 /* Discard the current deferred access checks and restore the
188 previous states. */
190 void
191 pop_deferring_access_checks (void)
193 if (deferred_access_no_check)
194 deferred_access_no_check--;
195 else
196 deferred_access_stack->pop ();
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.
204 vec<deferred_access_check, va_gc> *
205 get_deferred_access_checks (void)
207 if (deferred_access_no_check)
208 return NULL;
209 else
210 return (deferred_access_stack->last().deferred_access_checks);
213 /* Take current deferred checks and combine with the
214 previous states if we also defer checks previously.
215 Otherwise perform checks now. */
217 void
218 pop_to_parent_deferring_access_checks (void)
220 if (deferred_access_no_check)
221 deferred_access_no_check--;
222 else
224 vec<deferred_access_check, va_gc> *checks;
225 deferred_access *ptr;
227 checks = (deferred_access_stack->last ().deferred_access_checks);
229 deferred_access_stack->pop ();
230 ptr = &deferred_access_stack->last ();
231 if (ptr->deferring_access_checks_kind == dk_no_deferred)
233 /* Check access. */
234 perform_access_checks (checks, tf_warning_or_error);
236 else
238 /* Merge with parent. */
239 int i, j;
240 deferred_access_check *chk, *probe;
242 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
244 FOR_EACH_VEC_SAFE_ELT (ptr->deferred_access_checks, j, probe)
246 if (probe->binfo == chk->binfo &&
247 probe->decl == chk->decl &&
248 probe->diag_decl == chk->diag_decl)
249 goto found;
251 /* Insert into parent's checks. */
252 vec_safe_push (ptr->deferred_access_checks, *chk);
253 found:;
259 /* Called from enforce_access. A class has attempted (but failed) to access
260 DECL. It is already established that a baseclass of that class,
261 PARENT_BINFO, has private access to DECL. Examine certain special cases
262 to find a decl that accurately describes the source of the problem. If
263 none of the special cases apply, simply return DECL as the source of the
264 problem. */
266 static tree
267 get_class_access_diagnostic_decl (tree parent_binfo, tree decl)
269 /* When a class is denied access to a decl in a baseclass, most of the
270 time it is because the decl itself was declared as private at the point
271 of declaration.
273 However, in C++, there are (at least) two situations in which a decl
274 can be private even though it was not originally defined as such.
275 These two situations only apply if a baseclass had private access to
276 DECL (this function is only called if that is the case). */
278 /* We should first check whether the reason the parent had private access
279 to DECL was simply because DECL was created and declared as private in
280 the parent. If it was, then DECL is definitively the source of the
281 problem. */
282 if (SAME_BINFO_TYPE_P (context_for_name_lookup (decl),
283 BINFO_TYPE (parent_binfo)))
284 return decl;
286 /* 1. If the "using" keyword is used to inherit DECL within the parent,
287 this may cause DECL to be private, so we should return the using
288 statement as the source of the problem.
290 Scan the fields of PARENT_BINFO and see if there are any using decls. If
291 there are, see if they inherit DECL. If they do, that's where DECL must
292 have been declared private. */
294 for (tree parent_field = TYPE_FIELDS (BINFO_TYPE (parent_binfo));
295 parent_field;
296 parent_field = DECL_CHAIN (parent_field))
297 /* Not necessary, but also check TREE_PRIVATE for the sake of
298 eliminating obviously non-relevant using decls. */
299 if (TREE_CODE (parent_field) == USING_DECL
300 && TREE_PRIVATE (parent_field))
302 tree decl_stripped = strip_using_decl (parent_field);
304 /* The using statement might be overloaded. If so, we need to
305 check all of the overloads. */
306 for (ovl_iterator iter (decl_stripped); iter; ++iter)
307 /* If equal, the using statement inherits DECL, and so is the
308 source of the access failure, so return it. */
309 if (*iter == decl)
310 return parent_field;
313 /* 2. If DECL was privately inherited by the parent class, then DECL will
314 be inaccessible, even though it may originally have been accessible to
315 deriving classes. In that case, the fault lies with the parent, since it
316 used a private inheritance, so we return the parent as the source of the
317 problem.
319 Since this is the last check, we just assume it's true. At worst, it
320 will simply point to the class that failed to give access, which is
321 technically true. */
322 return TYPE_NAME (BINFO_TYPE (parent_binfo));
325 /* If the current scope isn't allowed to access DECL along
326 BASETYPE_PATH, give an error, or if we're parsing a function or class
327 template, defer the access check to be performed at instantiation time.
328 The most derived class in BASETYPE_PATH is the one used to qualify DECL.
329 DIAG_DECL is the declaration to use in the error diagnostic. */
331 static bool
332 enforce_access (tree basetype_path, tree decl, tree diag_decl,
333 tsubst_flags_t complain, access_failure_info *afi = NULL)
335 gcc_assert (TREE_CODE (basetype_path) == TREE_BINFO);
337 if (flag_new_inheriting_ctors
338 && DECL_INHERITED_CTOR (decl))
340 /* 7.3.3/18: The additional constructors are accessible if they would be
341 accessible when used to construct an object of the corresponding base
342 class. */
343 decl = strip_inheriting_ctors (decl);
344 basetype_path = lookup_base (basetype_path, DECL_CONTEXT (decl),
345 ba_any, NULL, complain);
348 tree cs = current_scope ();
349 if (processing_template_decl
350 && (CLASS_TYPE_P (cs) || TREE_CODE (cs) == FUNCTION_DECL))
351 if (tree template_info = get_template_info (cs))
353 /* When parsing a function or class template, we in general need to
354 defer access checks until template instantiation time, since a friend
355 declaration may grant access only to a particular specialization of
356 the template. */
358 if (accessible_p (basetype_path, decl, /*consider_local_p=*/true))
359 /* But if the member is deemed accessible at parse time, then we can
360 assume it'll be accessible at instantiation time. */
361 return true;
363 /* Access of a dependent decl should be rechecked after tsubst'ing
364 into the user of the decl, rather than explicitly deferring the
365 check here. */
366 gcc_assert (!uses_template_parms (decl));
367 if (TREE_CODE (decl) == FIELD_DECL)
368 gcc_assert (!uses_template_parms (DECL_CONTEXT (decl)));
370 /* Defer this access check until instantiation time. */
371 deferred_access_check access_check;
372 access_check.binfo = basetype_path;
373 access_check.decl = decl;
374 access_check.diag_decl = diag_decl;
375 access_check.loc = input_location;
376 vec_safe_push (TI_DEFERRED_ACCESS_CHECKS (template_info), access_check);
377 return true;
380 if (!accessible_p (basetype_path, decl, /*consider_local_p=*/true))
382 if (flag_new_inheriting_ctors)
383 diag_decl = strip_inheriting_ctors (diag_decl);
384 if (complain & tf_error)
386 access_kind access_failure_reason = ak_none;
388 /* By default, using the decl as the source of the problem will
389 usually give correct results. */
390 tree diag_location = diag_decl;
392 /* However, if a parent of BASETYPE_PATH had private access to decl,
393 then it actually might be the case that the source of the problem
394 is not DECL. */
395 tree parent_binfo = get_parent_with_private_access (decl,
396 basetype_path);
398 /* So if a parent did have private access, then we need to do
399 special checks to obtain the best diagnostic location decl. */
400 if (parent_binfo != NULL_TREE)
402 diag_location = get_class_access_diagnostic_decl (parent_binfo,
403 diag_decl);
405 /* We also at this point know that the reason access failed was
406 because decl was private. */
407 access_failure_reason = ak_private;
410 /* Finally, generate an error message. */
411 complain_about_access (decl, diag_decl, diag_location, true,
412 access_failure_reason);
414 if (afi)
415 afi->record_access_failure (basetype_path, decl, diag_decl);
416 return false;
419 return true;
422 /* Perform the access checks in CHECKS. The TREE_PURPOSE of each node
423 is the BINFO indicating the qualifying scope used to access the
424 DECL node stored in the TREE_VALUE of the node. If CHECKS is empty
425 or we aren't in SFINAE context or all the checks succeed return TRUE,
426 otherwise FALSE. */
428 bool
429 perform_access_checks (vec<deferred_access_check, va_gc> *checks,
430 tsubst_flags_t complain)
432 int i;
433 deferred_access_check *chk;
434 location_t loc = input_location;
435 bool ok = true;
437 if (!checks)
438 return true;
440 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
442 input_location = chk->loc;
443 ok &= enforce_access (chk->binfo, chk->decl, chk->diag_decl, complain);
446 input_location = loc;
447 return (complain & tf_error) ? true : ok;
450 /* Perform the deferred access checks.
452 After performing the checks, we still have to keep the list
453 `deferred_access_stack->deferred_access_checks' since we may want
454 to check access for them again later in a different context.
455 For example:
457 class A {
458 typedef int X;
459 static X a;
461 A::X A::a, x; // No error for `A::a', error for `x'
463 We have to perform deferred access of `A::X', first with `A::a',
464 next with `x'. Return value like perform_access_checks above. */
466 bool
467 perform_deferred_access_checks (tsubst_flags_t complain)
469 return perform_access_checks (get_deferred_access_checks (), complain);
472 /* Defer checking the accessibility of DECL, when looked up in
473 BINFO. DIAG_DECL is the declaration to use to print diagnostics.
474 Return value like perform_access_checks above.
475 If non-NULL, report failures to AFI. */
477 bool
478 perform_or_defer_access_check (tree binfo, tree decl, tree diag_decl,
479 tsubst_flags_t complain,
480 access_failure_info *afi)
482 int i;
483 deferred_access *ptr;
484 deferred_access_check *chk;
486 /* Exit if we are in a context that no access checking is performed. */
487 if (deferred_access_no_check)
488 return true;
490 gcc_assert (TREE_CODE (binfo) == TREE_BINFO);
492 ptr = &deferred_access_stack->last ();
494 /* If we are not supposed to defer access checks, just check now. */
495 if (ptr->deferring_access_checks_kind == dk_no_deferred)
497 bool ok = enforce_access (binfo, decl, diag_decl, complain, afi);
498 return (complain & tf_error) ? true : ok;
501 /* See if we are already going to perform this check. */
502 FOR_EACH_VEC_SAFE_ELT (ptr->deferred_access_checks, i, chk)
504 if (chk->decl == decl && chk->binfo == binfo &&
505 chk->diag_decl == diag_decl)
507 return true;
510 /* If not, record the check. */
511 deferred_access_check new_access = {binfo, decl, diag_decl, input_location};
512 vec_safe_push (ptr->deferred_access_checks, new_access);
514 return true;
517 /* Returns nonzero if the current statement is a full expression,
518 i.e. temporaries created during that statement should be destroyed
519 at the end of the statement. */
522 stmts_are_full_exprs_p (void)
524 return current_stmt_tree ()->stmts_are_full_exprs_p;
527 /* T is a statement. Add it to the statement-tree. This is the C++
528 version. The C/ObjC frontends have a slightly different version of
529 this function. */
531 tree
532 add_stmt (tree t)
534 enum tree_code code = TREE_CODE (t);
536 if (EXPR_P (t) && code != LABEL_EXPR)
538 if (!EXPR_HAS_LOCATION (t))
539 SET_EXPR_LOCATION (t, input_location);
541 /* When we expand a statement-tree, we must know whether or not the
542 statements are full-expressions. We record that fact here. */
543 if (STATEMENT_CODE_P (TREE_CODE (t)))
544 STMT_IS_FULL_EXPR_P (t) = stmts_are_full_exprs_p ();
547 if (code == LABEL_EXPR || code == CASE_LABEL_EXPR)
548 STATEMENT_LIST_HAS_LABEL (cur_stmt_list) = 1;
550 /* Add T to the statement-tree. Non-side-effect statements need to be
551 recorded during statement expressions. */
552 gcc_checking_assert (!stmt_list_stack->is_empty ());
553 append_to_statement_list_force (t, &cur_stmt_list);
555 return t;
558 /* Returns the stmt_tree to which statements are currently being added. */
560 stmt_tree
561 current_stmt_tree (void)
563 return (cfun
564 ? &cfun->language->base.x_stmt_tree
565 : &scope_chain->x_stmt_tree);
568 /* If statements are full expressions, wrap STMT in a CLEANUP_POINT_EXPR. */
570 static tree
571 maybe_cleanup_point_expr (tree expr)
573 if (!processing_template_decl && stmts_are_full_exprs_p ())
574 expr = fold_build_cleanup_point_expr (TREE_TYPE (expr), expr);
575 return expr;
578 /* Like maybe_cleanup_point_expr except have the type of the new expression be
579 void so we don't need to create a temporary variable to hold the inner
580 expression. The reason why we do this is because the original type might be
581 an aggregate and we cannot create a temporary variable for that type. */
583 tree
584 maybe_cleanup_point_expr_void (tree expr)
586 if (!processing_template_decl && stmts_are_full_exprs_p ())
587 expr = fold_build_cleanup_point_expr (void_type_node, expr);
588 return expr;
593 /* Create a declaration statement for the declaration given by the DECL. */
595 void
596 add_decl_expr (tree decl)
598 tree r = build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl);
599 if (DECL_INITIAL (decl)
600 || (DECL_SIZE (decl) && TREE_SIDE_EFFECTS (DECL_SIZE (decl))))
601 r = maybe_cleanup_point_expr_void (r);
602 add_stmt (r);
605 /* Set EXPR_LOCATION of the cleanups of any CLEANUP_STMT in STMTS to LOC. */
607 static void
608 set_cleanup_locs (tree stmts, location_t loc)
610 if (TREE_CODE (stmts) == CLEANUP_STMT)
612 tree t = CLEANUP_EXPR (stmts);
613 protected_set_expr_location (t, loc);
614 /* Avoid locus differences for C++ cdtor calls depending on whether
615 cdtor_returns_this: a conversion to void is added to discard the return
616 value, and this conversion ends up carrying the location, and when it
617 gets discarded, the location is lost. So hold it in the call as
618 well. */
619 if (TREE_CODE (t) == NOP_EXPR
620 && TREE_TYPE (t) == void_type_node
621 && TREE_CODE (TREE_OPERAND (t, 0)) == CALL_EXPR)
622 protected_set_expr_location (TREE_OPERAND (t, 0), loc);
623 set_cleanup_locs (CLEANUP_BODY (stmts), loc);
625 else if (TREE_CODE (stmts) == STATEMENT_LIST)
626 for (tree stmt : tsi_range (stmts))
627 set_cleanup_locs (stmt, loc);
630 /* Finish a scope. */
632 tree
633 do_poplevel (tree stmt_list)
635 tree block = NULL;
637 maybe_splice_retval_cleanup (stmt_list);
639 if (stmts_are_full_exprs_p ())
640 block = poplevel (kept_level_p (), 1, 0);
642 stmt_list = pop_stmt_list (stmt_list);
644 /* input_location is the last token of the scope, usually a }. */
645 set_cleanup_locs (stmt_list, input_location);
647 if (!processing_template_decl)
649 stmt_list = c_build_bind_expr (input_location, block, stmt_list);
650 /* ??? See c_end_compound_stmt re statement expressions. */
653 return stmt_list;
656 /* Begin a new scope. */
658 static tree
659 do_pushlevel (scope_kind sk)
661 tree ret = push_stmt_list ();
662 if (stmts_are_full_exprs_p ())
663 begin_scope (sk, NULL);
664 return ret;
667 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
668 when the current scope is exited. EH_ONLY is true when this is not
669 meant to apply to normal control flow transfer. DECL is the VAR_DECL
670 being cleaned up, if any, or null for temporaries or subobjects. */
672 void
673 push_cleanup (tree decl, tree cleanup, bool eh_only)
675 tree stmt = build_stmt (input_location, CLEANUP_STMT, NULL, cleanup, decl);
676 CLEANUP_EH_ONLY (stmt) = eh_only;
677 add_stmt (stmt);
678 CLEANUP_BODY (stmt) = push_stmt_list ();
681 /* Simple infinite loop tracking for -Wreturn-type. We keep a stack of all
682 the current loops, represented by 'NULL_TREE' if we've seen a possible
683 exit, and 'error_mark_node' if not. This is currently used only to
684 suppress the warning about a function with no return statements, and
685 therefore we don't bother noting returns as possible exits. We also
686 don't bother with gotos. */
688 static void
689 begin_maybe_infinite_loop (tree cond)
691 /* Only track this while parsing a function, not during instantiation. */
692 if (!cfun || (DECL_TEMPLATE_INSTANTIATION (current_function_decl)
693 && !processing_template_decl))
694 return;
695 bool maybe_infinite = true;
696 if (cond)
698 cond = fold_non_dependent_expr (cond);
699 maybe_infinite = integer_nonzerop (cond);
701 vec_safe_push (cp_function_chain->infinite_loops,
702 maybe_infinite ? error_mark_node : NULL_TREE);
706 /* A break is a possible exit for the current loop. */
708 void
709 break_maybe_infinite_loop (void)
711 if (!cfun)
712 return;
713 cp_function_chain->infinite_loops->last() = NULL_TREE;
716 /* If we reach the end of the loop without seeing a possible exit, we have
717 an infinite loop. */
719 static void
720 end_maybe_infinite_loop (tree cond)
722 if (!cfun || (DECL_TEMPLATE_INSTANTIATION (current_function_decl)
723 && !processing_template_decl))
724 return;
725 tree current = cp_function_chain->infinite_loops->pop();
726 if (current != NULL_TREE)
728 cond = fold_non_dependent_expr (cond);
729 if (integer_nonzerop (cond))
730 current_function_infinite_loop = 1;
734 /* Begin a conditional that might contain a declaration. When generating
735 normal code, we want the declaration to appear before the statement
736 containing the conditional. When generating template code, we want the
737 conditional to be rendered as the raw DECL_EXPR. */
739 static void
740 begin_cond (tree *cond_p)
742 if (processing_template_decl)
743 *cond_p = push_stmt_list ();
746 /* Finish such a conditional. */
748 static void
749 finish_cond (tree *cond_p, tree expr)
751 if (processing_template_decl)
753 tree cond = pop_stmt_list (*cond_p);
755 if (expr == NULL_TREE)
756 /* Empty condition in 'for'. */
757 gcc_assert (empty_expr_stmt_p (cond));
758 else if (check_for_bare_parameter_packs (expr))
759 expr = error_mark_node;
760 else if (!empty_expr_stmt_p (cond))
761 expr = build2 (COMPOUND_EXPR, TREE_TYPE (expr), cond, expr);
763 *cond_p = expr;
766 /* If *COND_P specifies a conditional with a declaration, transform the
767 loop such that
768 while (A x = 42) { }
769 for (; A x = 42;) { }
770 becomes
771 while (true) { A x = 42; if (!x) break; }
772 for (;;) { A x = 42; if (!x) break; }
773 The statement list for BODY will be empty if the conditional did
774 not declare anything. */
776 static void
777 simplify_loop_decl_cond (tree *cond_p, tree body)
779 tree cond, if_stmt;
781 if (!TREE_SIDE_EFFECTS (body))
782 return;
784 cond = *cond_p;
785 *cond_p = boolean_true_node;
787 if_stmt = begin_if_stmt ();
788 cond = cp_build_unary_op (TRUTH_NOT_EXPR, cond, false, tf_warning_or_error);
789 finish_if_stmt_cond (cond, if_stmt);
790 finish_break_stmt ();
791 finish_then_clause (if_stmt);
792 finish_if_stmt (if_stmt);
795 /* Finish a goto-statement. */
797 tree
798 finish_goto_stmt (tree destination)
800 if (identifier_p (destination))
801 destination = lookup_label (destination);
803 /* We warn about unused labels with -Wunused. That means we have to
804 mark the used labels as used. */
805 if (TREE_CODE (destination) == LABEL_DECL)
806 TREE_USED (destination) = 1;
807 else
809 destination = mark_rvalue_use (destination);
810 if (!processing_template_decl)
812 destination = cp_convert (ptr_type_node, destination,
813 tf_warning_or_error);
814 if (error_operand_p (destination))
815 return NULL_TREE;
816 destination
817 = fold_build_cleanup_point_expr (TREE_TYPE (destination),
818 destination);
822 check_goto (destination);
824 add_stmt (build_predict_expr (PRED_GOTO, NOT_TAKEN));
825 return add_stmt (build_stmt (input_location, GOTO_EXPR, destination));
828 /* Returns true if CALL is a (possibly wrapped) CALL_EXPR or AGGR_INIT_EXPR
829 to operator= () that is written as an operator expression. */
830 static bool
831 is_assignment_op_expr_p (tree call)
833 if (call == NULL_TREE)
834 return false;
836 call = extract_call_expr (call);
837 if (call == NULL_TREE
838 || call == error_mark_node
839 || !CALL_EXPR_OPERATOR_SYNTAX (call))
840 return false;
842 tree fndecl = cp_get_callee_fndecl_nofold (call);
843 return fndecl != NULL_TREE
844 && DECL_ASSIGNMENT_OPERATOR_P (fndecl)
845 && DECL_OVERLOADED_OPERATOR_IS (fndecl, NOP_EXPR);
848 /* COND is the condition-expression for an if, while, etc.,
849 statement. Convert it to a boolean value, if appropriate.
850 In addition, verify sequence points if -Wsequence-point is enabled. */
852 static tree
853 maybe_convert_cond (tree cond)
855 /* Empty conditions remain empty. */
856 if (!cond)
857 return NULL_TREE;
859 /* Wait until we instantiate templates before doing conversion. */
860 if (type_dependent_expression_p (cond))
861 return cond;
863 if (warn_sequence_point && !processing_template_decl)
864 verify_sequence_points (cond);
866 /* Do the conversion. */
867 cond = convert_from_reference (cond);
869 if ((TREE_CODE (cond) == MODIFY_EXPR || is_assignment_op_expr_p (cond))
870 && warn_parentheses
871 && !warning_suppressed_p (cond, OPT_Wparentheses)
872 && warning_at (cp_expr_loc_or_input_loc (cond),
873 OPT_Wparentheses, "suggest parentheses around "
874 "assignment used as truth value"))
875 suppress_warning (cond, OPT_Wparentheses);
877 return condition_conversion (cond);
880 /* Finish an expression-statement, whose EXPRESSION is as indicated. */
882 tree
883 finish_expr_stmt (tree expr)
885 tree r = NULL_TREE;
886 location_t loc = EXPR_LOCATION (expr);
888 if (expr != NULL_TREE)
890 /* If we ran into a problem, make sure we complained. */
891 gcc_assert (expr != error_mark_node || seen_error ());
893 if (!processing_template_decl)
895 if (warn_sequence_point)
896 verify_sequence_points (expr);
897 expr = convert_to_void (expr, ICV_STATEMENT, tf_warning_or_error);
899 else if (!type_dependent_expression_p (expr))
900 convert_to_void (build_non_dependent_expr (expr), ICV_STATEMENT,
901 tf_warning_or_error);
903 if (check_for_bare_parameter_packs (expr))
904 expr = error_mark_node;
906 /* Simplification of inner statement expressions, compound exprs,
907 etc can result in us already having an EXPR_STMT. */
908 if (TREE_CODE (expr) != CLEANUP_POINT_EXPR)
910 if (TREE_CODE (expr) != EXPR_STMT)
911 expr = build_stmt (loc, EXPR_STMT, expr);
912 expr = maybe_cleanup_point_expr_void (expr);
915 r = add_stmt (expr);
918 return r;
922 /* Begin an if-statement. Returns a newly created IF_STMT if
923 appropriate. */
925 tree
926 begin_if_stmt (void)
928 tree r, scope;
929 scope = do_pushlevel (sk_cond);
930 r = build_stmt (input_location, IF_STMT, NULL_TREE,
931 NULL_TREE, NULL_TREE, scope);
932 current_binding_level->this_entity = r;
933 begin_cond (&IF_COND (r));
934 return r;
937 /* Returns true if FN, a CALL_EXPR, is a call to
938 std::is_constant_evaluated or __builtin_is_constant_evaluated. */
940 static bool
941 is_std_constant_evaluated_p (tree fn)
943 /* std::is_constant_evaluated takes no arguments. */
944 if (call_expr_nargs (fn) != 0)
945 return false;
947 tree fndecl = cp_get_callee_fndecl_nofold (fn);
948 if (fndecl == NULL_TREE)
949 return false;
951 if (fndecl_built_in_p (fndecl, CP_BUILT_IN_IS_CONSTANT_EVALUATED,
952 BUILT_IN_FRONTEND))
953 return true;
955 if (!decl_in_std_namespace_p (fndecl))
956 return false;
958 tree name = DECL_NAME (fndecl);
959 return name && id_equal (name, "is_constant_evaluated");
962 /* Callback function for maybe_warn_for_constant_evaluated that looks
963 for calls to std::is_constant_evaluated in TP. */
965 static tree
966 find_std_constant_evaluated_r (tree *tp, int *walk_subtrees, void *)
968 tree t = *tp;
970 if (TYPE_P (t) || TREE_CONSTANT (t))
972 *walk_subtrees = false;
973 return NULL_TREE;
976 switch (TREE_CODE (t))
978 case CALL_EXPR:
979 if (is_std_constant_evaluated_p (t))
980 return t;
981 break;
982 case EXPR_STMT:
983 /* Don't warn in statement expressions. */
984 *walk_subtrees = false;
985 return NULL_TREE;
986 default:
987 break;
990 return NULL_TREE;
993 /* In certain contexts, std::is_constant_evaluated() is always true (for
994 instance, in a consteval function or in a constexpr if), or always false
995 (e.g., in a non-constexpr non-consteval function) so give the user a clue. */
997 static void
998 maybe_warn_for_constant_evaluated (tree cond, bool constexpr_if)
1000 if (!warn_tautological_compare)
1001 return;
1003 /* Suppress warning for std::is_constant_evaluated if the conditional
1004 comes from a macro. */
1005 if (from_macro_expansion_at (EXPR_LOCATION (cond)))
1006 return;
1008 cond = cp_walk_tree_without_duplicates (&cond, find_std_constant_evaluated_r,
1009 NULL);
1010 if (cond)
1012 if (constexpr_if)
1013 warning_at (EXPR_LOCATION (cond), OPT_Wtautological_compare,
1014 "%<std::is_constant_evaluated%> always evaluates to "
1015 "true in %<if constexpr%>");
1016 else if (!maybe_constexpr_fn (current_function_decl))
1017 warning_at (EXPR_LOCATION (cond), OPT_Wtautological_compare,
1018 "%<std::is_constant_evaluated%> always evaluates to "
1019 "false in a non-%<constexpr%> function");
1020 else if (DECL_IMMEDIATE_FUNCTION_P (current_function_decl))
1021 warning_at (EXPR_LOCATION (cond), OPT_Wtautological_compare,
1022 "%<std::is_constant_evaluated%> always evaluates to "
1023 "true in a %<consteval%> function");
1027 /* Process the COND of an if-statement, which may be given by
1028 IF_STMT. */
1030 tree
1031 finish_if_stmt_cond (tree orig_cond, tree if_stmt)
1033 tree cond = maybe_convert_cond (orig_cond);
1034 if (IF_STMT_CONSTEXPR_P (if_stmt)
1035 && !type_dependent_expression_p (cond)
1036 && require_constant_expression (cond)
1037 && !instantiation_dependent_expression_p (cond)
1038 /* Wait until instantiation time, since only then COND has been
1039 converted to bool. */
1040 && TYPE_MAIN_VARIANT (TREE_TYPE (cond)) == boolean_type_node)
1042 maybe_warn_for_constant_evaluated (cond, /*constexpr_if=*/true);
1043 cond = instantiate_non_dependent_expr (cond);
1044 cond = cxx_constant_value (cond);
1046 else
1048 maybe_warn_for_constant_evaluated (cond, /*constexpr_if=*/false);
1049 if (processing_template_decl)
1050 cond = orig_cond;
1052 finish_cond (&IF_COND (if_stmt), cond);
1053 add_stmt (if_stmt);
1054 THEN_CLAUSE (if_stmt) = push_stmt_list ();
1055 return cond;
1058 /* Finish the then-clause of an if-statement, which may be given by
1059 IF_STMT. */
1061 tree
1062 finish_then_clause (tree if_stmt)
1064 THEN_CLAUSE (if_stmt) = pop_stmt_list (THEN_CLAUSE (if_stmt));
1065 return if_stmt;
1068 /* Begin the else-clause of an if-statement. */
1070 void
1071 begin_else_clause (tree if_stmt)
1073 ELSE_CLAUSE (if_stmt) = push_stmt_list ();
1076 /* Finish the else-clause of an if-statement, which may be given by
1077 IF_STMT. */
1079 void
1080 finish_else_clause (tree if_stmt)
1082 ELSE_CLAUSE (if_stmt) = pop_stmt_list (ELSE_CLAUSE (if_stmt));
1085 /* Callback for cp_walk_tree to mark all {VAR,PARM}_DECLs in a tree as
1086 read. */
1088 static tree
1089 maybe_mark_exp_read_r (tree *tp, int *, void *)
1091 tree t = *tp;
1092 if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
1093 mark_exp_read (t);
1094 return NULL_TREE;
1097 /* Finish an if-statement. */
1099 void
1100 finish_if_stmt (tree if_stmt)
1102 tree scope = IF_SCOPE (if_stmt);
1103 IF_SCOPE (if_stmt) = NULL;
1104 if (IF_STMT_CONSTEXPR_P (if_stmt))
1106 /* Prevent various -Wunused warnings. We might not instantiate
1107 either of these branches, so we would not mark the variables
1108 used in that branch as read. */
1109 cp_walk_tree_without_duplicates (&THEN_CLAUSE (if_stmt),
1110 maybe_mark_exp_read_r, NULL);
1111 cp_walk_tree_without_duplicates (&ELSE_CLAUSE (if_stmt),
1112 maybe_mark_exp_read_r, NULL);
1114 add_stmt (do_poplevel (scope));
1117 /* Begin a while-statement. Returns a newly created WHILE_STMT if
1118 appropriate. */
1120 tree
1121 begin_while_stmt (void)
1123 tree r;
1124 r = build_stmt (input_location, WHILE_STMT, NULL_TREE, NULL_TREE);
1125 add_stmt (r);
1126 WHILE_BODY (r) = do_pushlevel (sk_block);
1127 begin_cond (&WHILE_COND (r));
1128 return r;
1131 /* Process the COND of a while-statement, which may be given by
1132 WHILE_STMT. */
1134 void
1135 finish_while_stmt_cond (tree cond, tree while_stmt, bool ivdep,
1136 unsigned short unroll)
1138 cond = maybe_convert_cond (cond);
1139 finish_cond (&WHILE_COND (while_stmt), cond);
1140 begin_maybe_infinite_loop (cond);
1141 if (ivdep && cond != error_mark_node)
1142 WHILE_COND (while_stmt) = build3 (ANNOTATE_EXPR,
1143 TREE_TYPE (WHILE_COND (while_stmt)),
1144 WHILE_COND (while_stmt),
1145 build_int_cst (integer_type_node,
1146 annot_expr_ivdep_kind),
1147 integer_zero_node);
1148 if (unroll && cond != error_mark_node)
1149 WHILE_COND (while_stmt) = build3 (ANNOTATE_EXPR,
1150 TREE_TYPE (WHILE_COND (while_stmt)),
1151 WHILE_COND (while_stmt),
1152 build_int_cst (integer_type_node,
1153 annot_expr_unroll_kind),
1154 build_int_cst (integer_type_node,
1155 unroll));
1156 simplify_loop_decl_cond (&WHILE_COND (while_stmt), WHILE_BODY (while_stmt));
1159 /* Finish a while-statement, which may be given by WHILE_STMT. */
1161 void
1162 finish_while_stmt (tree while_stmt)
1164 end_maybe_infinite_loop (boolean_true_node);
1165 WHILE_BODY (while_stmt) = do_poplevel (WHILE_BODY (while_stmt));
1168 /* Begin a do-statement. Returns a newly created DO_STMT if
1169 appropriate. */
1171 tree
1172 begin_do_stmt (void)
1174 tree r = build_stmt (input_location, DO_STMT, NULL_TREE, NULL_TREE);
1175 begin_maybe_infinite_loop (boolean_true_node);
1176 add_stmt (r);
1177 DO_BODY (r) = push_stmt_list ();
1178 return r;
1181 /* Finish the body of a do-statement, which may be given by DO_STMT. */
1183 void
1184 finish_do_body (tree do_stmt)
1186 tree body = DO_BODY (do_stmt) = pop_stmt_list (DO_BODY (do_stmt));
1188 if (TREE_CODE (body) == STATEMENT_LIST && STATEMENT_LIST_TAIL (body))
1189 body = STATEMENT_LIST_TAIL (body)->stmt;
1191 if (IS_EMPTY_STMT (body))
1192 warning (OPT_Wempty_body,
1193 "suggest explicit braces around empty body in %<do%> statement");
1196 /* Finish a do-statement, which may be given by DO_STMT, and whose
1197 COND is as indicated. */
1199 void
1200 finish_do_stmt (tree cond, tree do_stmt, bool ivdep, unsigned short unroll)
1202 cond = maybe_convert_cond (cond);
1203 end_maybe_infinite_loop (cond);
1204 /* Unlike other iteration statements, the condition may not contain
1205 a declaration, so we don't call finish_cond which checks for
1206 unexpanded parameter packs. */
1207 if (check_for_bare_parameter_packs (cond))
1208 cond = error_mark_node;
1209 if (ivdep && cond != error_mark_node)
1210 cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
1211 build_int_cst (integer_type_node, annot_expr_ivdep_kind),
1212 integer_zero_node);
1213 if (unroll && cond != error_mark_node)
1214 cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
1215 build_int_cst (integer_type_node, annot_expr_unroll_kind),
1216 build_int_cst (integer_type_node, unroll));
1217 DO_COND (do_stmt) = cond;
1220 /* Finish a return-statement. The EXPRESSION returned, if any, is as
1221 indicated. */
1223 tree
1224 finish_return_stmt (tree expr)
1226 tree r;
1227 bool no_warning;
1229 expr = check_return_expr (expr, &no_warning);
1231 if (error_operand_p (expr)
1232 || (flag_openmp && !check_omp_return ()))
1234 /* Suppress -Wreturn-type for this function. */
1235 if (warn_return_type)
1236 suppress_warning (current_function_decl, OPT_Wreturn_type);
1237 return error_mark_node;
1240 if (!processing_template_decl)
1242 if (warn_sequence_point)
1243 verify_sequence_points (expr);
1246 r = build_stmt (input_location, RETURN_EXPR, expr);
1247 if (no_warning)
1248 suppress_warning (r, OPT_Wreturn_type);
1249 r = maybe_cleanup_point_expr_void (r);
1250 r = add_stmt (r);
1252 return r;
1255 /* Begin the scope of a for-statement or a range-for-statement.
1256 Both the returned trees are to be used in a call to
1257 begin_for_stmt or begin_range_for_stmt. */
1259 tree
1260 begin_for_scope (tree *init)
1262 tree scope = do_pushlevel (sk_for);
1264 if (processing_template_decl)
1265 *init = push_stmt_list ();
1266 else
1267 *init = NULL_TREE;
1269 return scope;
1272 /* Begin a for-statement. Returns a new FOR_STMT.
1273 SCOPE and INIT should be the return of begin_for_scope,
1274 or both NULL_TREE */
1276 tree
1277 begin_for_stmt (tree scope, tree init)
1279 tree r;
1281 r = build_stmt (input_location, FOR_STMT, NULL_TREE, NULL_TREE,
1282 NULL_TREE, NULL_TREE, NULL_TREE);
1284 if (scope == NULL_TREE)
1286 gcc_assert (!init);
1287 scope = begin_for_scope (&init);
1290 FOR_INIT_STMT (r) = init;
1291 FOR_SCOPE (r) = scope;
1293 return r;
1296 /* Finish the init-statement of a for-statement, which may be
1297 given by FOR_STMT. */
1299 void
1300 finish_init_stmt (tree for_stmt)
1302 if (processing_template_decl)
1303 FOR_INIT_STMT (for_stmt) = pop_stmt_list (FOR_INIT_STMT (for_stmt));
1304 add_stmt (for_stmt);
1305 FOR_BODY (for_stmt) = do_pushlevel (sk_block);
1306 begin_cond (&FOR_COND (for_stmt));
1309 /* Finish the COND of a for-statement, which may be given by
1310 FOR_STMT. */
1312 void
1313 finish_for_cond (tree cond, tree for_stmt, bool ivdep, unsigned short unroll)
1315 cond = maybe_convert_cond (cond);
1316 finish_cond (&FOR_COND (for_stmt), cond);
1317 begin_maybe_infinite_loop (cond);
1318 if (ivdep && cond != error_mark_node)
1319 FOR_COND (for_stmt) = build3 (ANNOTATE_EXPR,
1320 TREE_TYPE (FOR_COND (for_stmt)),
1321 FOR_COND (for_stmt),
1322 build_int_cst (integer_type_node,
1323 annot_expr_ivdep_kind),
1324 integer_zero_node);
1325 if (unroll && cond != error_mark_node)
1326 FOR_COND (for_stmt) = build3 (ANNOTATE_EXPR,
1327 TREE_TYPE (FOR_COND (for_stmt)),
1328 FOR_COND (for_stmt),
1329 build_int_cst (integer_type_node,
1330 annot_expr_unroll_kind),
1331 build_int_cst (integer_type_node,
1332 unroll));
1333 simplify_loop_decl_cond (&FOR_COND (for_stmt), FOR_BODY (for_stmt));
1336 /* Finish the increment-EXPRESSION in a for-statement, which may be
1337 given by FOR_STMT. */
1339 void
1340 finish_for_expr (tree expr, tree for_stmt)
1342 if (!expr)
1343 return;
1344 /* If EXPR is an overloaded function, issue an error; there is no
1345 context available to use to perform overload resolution. */
1346 if (type_unknown_p (expr))
1348 cxx_incomplete_type_error (expr, TREE_TYPE (expr));
1349 expr = error_mark_node;
1351 if (!processing_template_decl)
1353 if (warn_sequence_point)
1354 verify_sequence_points (expr);
1355 expr = convert_to_void (expr, ICV_THIRD_IN_FOR,
1356 tf_warning_or_error);
1358 else if (!type_dependent_expression_p (expr))
1359 convert_to_void (build_non_dependent_expr (expr), ICV_THIRD_IN_FOR,
1360 tf_warning_or_error);
1361 expr = maybe_cleanup_point_expr_void (expr);
1362 if (check_for_bare_parameter_packs (expr))
1363 expr = error_mark_node;
1364 FOR_EXPR (for_stmt) = expr;
1367 /* Finish the body of a for-statement, which may be given by
1368 FOR_STMT. The increment-EXPR for the loop must be
1369 provided.
1370 It can also finish RANGE_FOR_STMT. */
1372 void
1373 finish_for_stmt (tree for_stmt)
1375 end_maybe_infinite_loop (boolean_true_node);
1377 if (TREE_CODE (for_stmt) == RANGE_FOR_STMT)
1378 RANGE_FOR_BODY (for_stmt) = do_poplevel (RANGE_FOR_BODY (for_stmt));
1379 else
1380 FOR_BODY (for_stmt) = do_poplevel (FOR_BODY (for_stmt));
1382 /* Pop the scope for the body of the loop. */
1383 tree *scope_ptr = (TREE_CODE (for_stmt) == RANGE_FOR_STMT
1384 ? &RANGE_FOR_SCOPE (for_stmt)
1385 : &FOR_SCOPE (for_stmt));
1386 tree scope = *scope_ptr;
1387 *scope_ptr = NULL;
1389 /* During parsing of the body, range for uses "__for_{range,begin,end} "
1390 decl names to make those unaccessible by code in the body.
1391 Change it to ones with underscore instead of space, so that it can
1392 be inspected in the debugger. */
1393 tree range_for_decl[3] = { NULL_TREE, NULL_TREE, NULL_TREE };
1394 gcc_assert (CPTI_FOR_BEGIN__IDENTIFIER == CPTI_FOR_RANGE__IDENTIFIER + 1
1395 && CPTI_FOR_END__IDENTIFIER == CPTI_FOR_RANGE__IDENTIFIER + 2
1396 && CPTI_FOR_RANGE_IDENTIFIER == CPTI_FOR_RANGE__IDENTIFIER + 3
1397 && CPTI_FOR_BEGIN_IDENTIFIER == CPTI_FOR_BEGIN__IDENTIFIER + 3
1398 && CPTI_FOR_END_IDENTIFIER == CPTI_FOR_END__IDENTIFIER + 3);
1399 for (int i = 0; i < 3; i++)
1401 tree id = cp_global_trees[CPTI_FOR_RANGE__IDENTIFIER + i];
1402 if (IDENTIFIER_BINDING (id)
1403 && IDENTIFIER_BINDING (id)->scope == current_binding_level)
1405 range_for_decl[i] = IDENTIFIER_BINDING (id)->value;
1406 gcc_assert (VAR_P (range_for_decl[i])
1407 && DECL_ARTIFICIAL (range_for_decl[i]));
1411 add_stmt (do_poplevel (scope));
1413 /* If we're being called from build_vec_init, don't mess with the names of
1414 the variables for an enclosing range-for. */
1415 if (!stmts_are_full_exprs_p ())
1416 return;
1418 for (int i = 0; i < 3; i++)
1419 if (range_for_decl[i])
1420 DECL_NAME (range_for_decl[i])
1421 = cp_global_trees[CPTI_FOR_RANGE_IDENTIFIER + i];
1424 /* Begin a range-for-statement. Returns a new RANGE_FOR_STMT.
1425 SCOPE and INIT should be the return of begin_for_scope,
1426 or both NULL_TREE .
1427 To finish it call finish_for_stmt(). */
1429 tree
1430 begin_range_for_stmt (tree scope, tree init)
1432 begin_maybe_infinite_loop (boolean_false_node);
1434 tree r = build_stmt (input_location, RANGE_FOR_STMT, NULL_TREE, NULL_TREE,
1435 NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE);
1437 if (scope == NULL_TREE)
1439 gcc_assert (!init);
1440 scope = begin_for_scope (&init);
1443 /* Since C++20, RANGE_FOR_STMTs can use the init tree, so save it. */
1444 RANGE_FOR_INIT_STMT (r) = init;
1445 RANGE_FOR_SCOPE (r) = scope;
1447 return r;
1450 /* Finish the head of a range-based for statement, which may
1451 be given by RANGE_FOR_STMT. DECL must be the declaration
1452 and EXPR must be the loop expression. */
1454 void
1455 finish_range_for_decl (tree range_for_stmt, tree decl, tree expr)
1457 if (processing_template_decl)
1458 RANGE_FOR_INIT_STMT (range_for_stmt)
1459 = pop_stmt_list (RANGE_FOR_INIT_STMT (range_for_stmt));
1460 RANGE_FOR_DECL (range_for_stmt) = decl;
1461 RANGE_FOR_EXPR (range_for_stmt) = expr;
1462 add_stmt (range_for_stmt);
1463 RANGE_FOR_BODY (range_for_stmt) = do_pushlevel (sk_block);
1466 /* Finish a break-statement. */
1468 tree
1469 finish_break_stmt (void)
1471 /* In switch statements break is sometimes stylistically used after
1472 a return statement. This can lead to spurious warnings about
1473 control reaching the end of a non-void function when it is
1474 inlined. Note that we are calling block_may_fallthru with
1475 language specific tree nodes; this works because
1476 block_may_fallthru returns true when given something it does not
1477 understand. */
1478 if (!block_may_fallthru (cur_stmt_list))
1479 return void_node;
1480 note_break_stmt ();
1481 return add_stmt (build_stmt (input_location, BREAK_STMT));
1484 /* Finish a continue-statement. */
1486 tree
1487 finish_continue_stmt (void)
1489 return add_stmt (build_stmt (input_location, CONTINUE_STMT));
1492 /* Begin a switch-statement. Returns a new SWITCH_STMT if
1493 appropriate. */
1495 tree
1496 begin_switch_stmt (void)
1498 tree r, scope;
1500 scope = do_pushlevel (sk_cond);
1501 r = build_stmt (input_location, SWITCH_STMT, NULL_TREE, NULL_TREE, NULL_TREE, scope);
1503 begin_cond (&SWITCH_STMT_COND (r));
1505 return r;
1508 /* Finish the cond of a switch-statement. */
1510 void
1511 finish_switch_cond (tree cond, tree switch_stmt)
1513 tree orig_type = NULL;
1515 if (!processing_template_decl)
1517 /* Convert the condition to an integer or enumeration type. */
1518 tree orig_cond = cond;
1519 cond = build_expr_type_conversion (WANT_INT | WANT_ENUM, cond, true);
1520 if (cond == NULL_TREE)
1522 error_at (cp_expr_loc_or_input_loc (orig_cond),
1523 "switch quantity not an integer");
1524 cond = error_mark_node;
1526 /* We want unlowered type here to handle enum bit-fields. */
1527 orig_type = unlowered_expr_type (cond);
1528 if (TREE_CODE (orig_type) != ENUMERAL_TYPE)
1529 orig_type = TREE_TYPE (cond);
1530 if (cond != error_mark_node)
1532 /* [stmt.switch]
1534 Integral promotions are performed. */
1535 cond = perform_integral_promotions (cond);
1536 cond = maybe_cleanup_point_expr (cond);
1539 if (check_for_bare_parameter_packs (cond))
1540 cond = error_mark_node;
1541 else if (!processing_template_decl && warn_sequence_point)
1542 verify_sequence_points (cond);
1544 finish_cond (&SWITCH_STMT_COND (switch_stmt), cond);
1545 SWITCH_STMT_TYPE (switch_stmt) = orig_type;
1546 add_stmt (switch_stmt);
1547 push_switch (switch_stmt);
1548 SWITCH_STMT_BODY (switch_stmt) = push_stmt_list ();
1551 /* Finish the body of a switch-statement, which may be given by
1552 SWITCH_STMT. The COND to switch on is indicated. */
1554 void
1555 finish_switch_stmt (tree switch_stmt)
1557 tree scope;
1559 SWITCH_STMT_BODY (switch_stmt) =
1560 pop_stmt_list (SWITCH_STMT_BODY (switch_stmt));
1561 pop_switch ();
1563 scope = SWITCH_STMT_SCOPE (switch_stmt);
1564 SWITCH_STMT_SCOPE (switch_stmt) = NULL;
1565 add_stmt (do_poplevel (scope));
1568 /* Begin a try-block. Returns a newly-created TRY_BLOCK if
1569 appropriate. */
1571 tree
1572 begin_try_block (void)
1574 tree r = build_stmt (input_location, TRY_BLOCK, NULL_TREE, NULL_TREE);
1575 add_stmt (r);
1576 TRY_STMTS (r) = push_stmt_list ();
1577 return r;
1580 /* Likewise, for a function-try-block. The block returned in
1581 *COMPOUND_STMT is an artificial outer scope, containing the
1582 function-try-block. */
1584 tree
1585 begin_function_try_block (tree *compound_stmt)
1587 tree r;
1588 /* This outer scope does not exist in the C++ standard, but we need
1589 a place to put __FUNCTION__ and similar variables. */
1590 *compound_stmt = begin_compound_stmt (0);
1591 r = begin_try_block ();
1592 FN_TRY_BLOCK_P (r) = 1;
1593 return r;
1596 /* Finish a try-block, which may be given by TRY_BLOCK. */
1598 void
1599 finish_try_block (tree try_block)
1601 TRY_STMTS (try_block) = pop_stmt_list (TRY_STMTS (try_block));
1602 TRY_HANDLERS (try_block) = push_stmt_list ();
1605 /* Finish the body of a cleanup try-block, which may be given by
1606 TRY_BLOCK. */
1608 void
1609 finish_cleanup_try_block (tree try_block)
1611 TRY_STMTS (try_block) = pop_stmt_list (TRY_STMTS (try_block));
1614 /* Finish an implicitly generated try-block, with a cleanup is given
1615 by CLEANUP. */
1617 void
1618 finish_cleanup (tree cleanup, tree try_block)
1620 TRY_HANDLERS (try_block) = cleanup;
1621 CLEANUP_P (try_block) = 1;
1624 /* Likewise, for a function-try-block. */
1626 void
1627 finish_function_try_block (tree try_block)
1629 finish_try_block (try_block);
1630 /* FIXME : something queer about CTOR_INITIALIZER somehow following
1631 the try block, but moving it inside. */
1632 in_function_try_handler = 1;
1635 /* Finish a handler-sequence for a try-block, which may be given by
1636 TRY_BLOCK. */
1638 void
1639 finish_handler_sequence (tree try_block)
1641 TRY_HANDLERS (try_block) = pop_stmt_list (TRY_HANDLERS (try_block));
1642 check_handlers (TRY_HANDLERS (try_block));
1645 /* Finish the handler-seq for a function-try-block, given by
1646 TRY_BLOCK. COMPOUND_STMT is the outer block created by
1647 begin_function_try_block. */
1649 void
1650 finish_function_handler_sequence (tree try_block, tree compound_stmt)
1652 in_function_try_handler = 0;
1653 finish_handler_sequence (try_block);
1654 finish_compound_stmt (compound_stmt);
1657 /* Begin a handler. Returns a HANDLER if appropriate. */
1659 tree
1660 begin_handler (void)
1662 tree r;
1664 r = build_stmt (input_location, HANDLER, NULL_TREE, NULL_TREE);
1665 add_stmt (r);
1667 /* Create a binding level for the eh_info and the exception object
1668 cleanup. */
1669 HANDLER_BODY (r) = do_pushlevel (sk_catch);
1671 return r;
1674 /* Finish the handler-parameters for a handler, which may be given by
1675 HANDLER. DECL is the declaration for the catch parameter, or NULL
1676 if this is a `catch (...)' clause. */
1678 void
1679 finish_handler_parms (tree decl, tree handler)
1681 tree type = NULL_TREE;
1682 if (processing_template_decl)
1684 if (decl)
1686 decl = pushdecl (decl);
1687 decl = push_template_decl (decl);
1688 HANDLER_PARMS (handler) = decl;
1689 type = TREE_TYPE (decl);
1692 else
1694 type = expand_start_catch_block (decl);
1695 if (warn_catch_value
1696 && type != NULL_TREE
1697 && type != error_mark_node
1698 && !TYPE_REF_P (TREE_TYPE (decl)))
1700 tree orig_type = TREE_TYPE (decl);
1701 if (CLASS_TYPE_P (orig_type))
1703 if (TYPE_POLYMORPHIC_P (orig_type))
1704 warning_at (DECL_SOURCE_LOCATION (decl),
1705 OPT_Wcatch_value_,
1706 "catching polymorphic type %q#T by value",
1707 orig_type);
1708 else if (warn_catch_value > 1)
1709 warning_at (DECL_SOURCE_LOCATION (decl),
1710 OPT_Wcatch_value_,
1711 "catching type %q#T by value", orig_type);
1713 else if (warn_catch_value > 2)
1714 warning_at (DECL_SOURCE_LOCATION (decl),
1715 OPT_Wcatch_value_,
1716 "catching non-reference type %q#T", orig_type);
1719 HANDLER_TYPE (handler) = type;
1722 /* Finish a handler, which may be given by HANDLER. The BLOCKs are
1723 the return value from the matching call to finish_handler_parms. */
1725 void
1726 finish_handler (tree handler)
1728 if (!processing_template_decl)
1729 expand_end_catch_block ();
1730 HANDLER_BODY (handler) = do_poplevel (HANDLER_BODY (handler));
1733 /* Begin a compound statement. FLAGS contains some bits that control the
1734 behavior and context. If BCS_NO_SCOPE is set, the compound statement
1735 does not define a scope. If BCS_FN_BODY is set, this is the outermost
1736 block of a function. If BCS_TRY_BLOCK is set, this is the block
1737 created on behalf of a TRY statement. Returns a token to be passed to
1738 finish_compound_stmt. */
1740 tree
1741 begin_compound_stmt (unsigned int flags)
1743 tree r;
1745 if (flags & BCS_NO_SCOPE)
1747 r = push_stmt_list ();
1748 STATEMENT_LIST_NO_SCOPE (r) = 1;
1750 /* Normally, we try hard to keep the BLOCK for a statement-expression.
1751 But, if it's a statement-expression with a scopeless block, there's
1752 nothing to keep, and we don't want to accidentally keep a block
1753 *inside* the scopeless block. */
1754 keep_next_level (false);
1756 else
1758 scope_kind sk = sk_block;
1759 if (flags & BCS_TRY_BLOCK)
1760 sk = sk_try;
1761 else if (flags & BCS_TRANSACTION)
1762 sk = sk_transaction;
1763 else if (flags & BCS_STMT_EXPR)
1764 sk = sk_stmt_expr;
1765 r = do_pushlevel (sk);
1768 /* When processing a template, we need to remember where the braces were,
1769 so that we can set up identical scopes when instantiating the template
1770 later. BIND_EXPR is a handy candidate for this.
1771 Note that do_poplevel won't create a BIND_EXPR itself here (and thus
1772 result in nested BIND_EXPRs), since we don't build BLOCK nodes when
1773 processing templates. */
1774 if (processing_template_decl)
1776 r = build3 (BIND_EXPR, NULL, NULL, r, NULL);
1777 BIND_EXPR_TRY_BLOCK (r) = (flags & BCS_TRY_BLOCK) != 0;
1778 BIND_EXPR_BODY_BLOCK (r) = (flags & BCS_FN_BODY) != 0;
1779 TREE_SIDE_EFFECTS (r) = 1;
1782 return r;
1785 /* Finish a compound-statement, which is given by STMT. */
1787 void
1788 finish_compound_stmt (tree stmt)
1790 if (TREE_CODE (stmt) == BIND_EXPR)
1792 tree body = do_poplevel (BIND_EXPR_BODY (stmt));
1793 /* If the STATEMENT_LIST is empty and this BIND_EXPR isn't special,
1794 discard the BIND_EXPR so it can be merged with the containing
1795 STATEMENT_LIST. */
1796 if (TREE_CODE (body) == STATEMENT_LIST
1797 && STATEMENT_LIST_HEAD (body) == NULL
1798 && !BIND_EXPR_BODY_BLOCK (stmt)
1799 && !BIND_EXPR_TRY_BLOCK (stmt))
1800 stmt = body;
1801 else
1802 BIND_EXPR_BODY (stmt) = body;
1804 else if (STATEMENT_LIST_NO_SCOPE (stmt))
1805 stmt = pop_stmt_list (stmt);
1806 else
1808 /* Destroy any ObjC "super" receivers that may have been
1809 created. */
1810 objc_clear_super_receiver ();
1812 stmt = do_poplevel (stmt);
1815 /* ??? See c_end_compound_stmt wrt statement expressions. */
1816 add_stmt (stmt);
1819 /* Finish an asm-statement, whose components are a STRING, some
1820 OUTPUT_OPERANDS, some INPUT_OPERANDS, some CLOBBERS and some
1821 LABELS. Also note whether the asm-statement should be
1822 considered volatile, and whether it is asm inline. */
1824 tree
1825 finish_asm_stmt (location_t loc, int volatile_p, tree string,
1826 tree output_operands, tree input_operands, tree clobbers,
1827 tree labels, bool inline_p)
1829 tree r;
1830 tree t;
1831 int ninputs = list_length (input_operands);
1832 int noutputs = list_length (output_operands);
1834 if (!processing_template_decl)
1836 const char *constraint;
1837 const char **oconstraints;
1838 bool allows_mem, allows_reg, is_inout;
1839 tree operand;
1840 int i;
1842 oconstraints = XALLOCAVEC (const char *, noutputs);
1844 string = resolve_asm_operand_names (string, output_operands,
1845 input_operands, labels);
1847 for (i = 0, t = output_operands; t; t = TREE_CHAIN (t), ++i)
1849 operand = TREE_VALUE (t);
1851 /* ??? Really, this should not be here. Users should be using a
1852 proper lvalue, dammit. But there's a long history of using
1853 casts in the output operands. In cases like longlong.h, this
1854 becomes a primitive form of typechecking -- if the cast can be
1855 removed, then the output operand had a type of the proper width;
1856 otherwise we'll get an error. Gross, but ... */
1857 STRIP_NOPS (operand);
1859 operand = mark_lvalue_use (operand);
1861 if (!lvalue_or_else (operand, lv_asm, tf_warning_or_error))
1862 operand = error_mark_node;
1864 if (operand != error_mark_node
1865 && (TREE_READONLY (operand)
1866 || CP_TYPE_CONST_P (TREE_TYPE (operand))
1867 /* Functions are not modifiable, even though they are
1868 lvalues. */
1869 || FUNC_OR_METHOD_TYPE_P (TREE_TYPE (operand))
1870 /* If it's an aggregate and any field is const, then it is
1871 effectively const. */
1872 || (CLASS_TYPE_P (TREE_TYPE (operand))
1873 && C_TYPE_FIELDS_READONLY (TREE_TYPE (operand)))))
1874 cxx_readonly_error (loc, operand, lv_asm);
1876 tree *op = &operand;
1877 while (TREE_CODE (*op) == COMPOUND_EXPR)
1878 op = &TREE_OPERAND (*op, 1);
1879 switch (TREE_CODE (*op))
1881 case PREINCREMENT_EXPR:
1882 case PREDECREMENT_EXPR:
1883 case MODIFY_EXPR:
1884 *op = genericize_compound_lvalue (*op);
1885 op = &TREE_OPERAND (*op, 1);
1886 break;
1887 default:
1888 break;
1891 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
1892 oconstraints[i] = constraint;
1894 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
1895 &allows_mem, &allows_reg, &is_inout))
1897 /* If the operand is going to end up in memory,
1898 mark it addressable. */
1899 if (!allows_reg && !cxx_mark_addressable (*op))
1900 operand = error_mark_node;
1902 else
1903 operand = error_mark_node;
1905 TREE_VALUE (t) = operand;
1908 for (i = 0, t = input_operands; t; ++i, t = TREE_CHAIN (t))
1910 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
1911 bool constraint_parsed
1912 = parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
1913 oconstraints, &allows_mem, &allows_reg);
1914 /* If the operand is going to end up in memory, don't call
1915 decay_conversion. */
1916 if (constraint_parsed && !allows_reg && allows_mem)
1917 operand = mark_lvalue_use (TREE_VALUE (t));
1918 else
1919 operand = decay_conversion (TREE_VALUE (t), tf_warning_or_error);
1921 /* If the type of the operand hasn't been determined (e.g.,
1922 because it involves an overloaded function), then issue
1923 an error message. There's no context available to
1924 resolve the overloading. */
1925 if (TREE_TYPE (operand) == unknown_type_node)
1927 error_at (loc,
1928 "type of %<asm%> operand %qE could not be determined",
1929 TREE_VALUE (t));
1930 operand = error_mark_node;
1933 if (constraint_parsed)
1935 /* If the operand is going to end up in memory,
1936 mark it addressable. */
1937 if (!allows_reg && allows_mem)
1939 /* Strip the nops as we allow this case. FIXME, this really
1940 should be rejected or made deprecated. */
1941 STRIP_NOPS (operand);
1943 tree *op = &operand;
1944 while (TREE_CODE (*op) == COMPOUND_EXPR)
1945 op = &TREE_OPERAND (*op, 1);
1946 switch (TREE_CODE (*op))
1948 case PREINCREMENT_EXPR:
1949 case PREDECREMENT_EXPR:
1950 case MODIFY_EXPR:
1951 *op = genericize_compound_lvalue (*op);
1952 op = &TREE_OPERAND (*op, 1);
1953 break;
1954 default:
1955 break;
1958 if (!cxx_mark_addressable (*op))
1959 operand = error_mark_node;
1961 else if (!allows_reg && !allows_mem)
1963 /* If constraint allows neither register nor memory,
1964 try harder to get a constant. */
1965 tree constop = maybe_constant_value (operand);
1966 if (TREE_CONSTANT (constop))
1967 operand = constop;
1970 else
1971 operand = error_mark_node;
1973 TREE_VALUE (t) = operand;
1977 r = build_stmt (loc, ASM_EXPR, string,
1978 output_operands, input_operands,
1979 clobbers, labels);
1980 ASM_VOLATILE_P (r) = volatile_p || noutputs == 0;
1981 ASM_INLINE_P (r) = inline_p;
1982 r = maybe_cleanup_point_expr_void (r);
1983 return add_stmt (r);
1986 /* Finish a label with the indicated NAME. Returns the new label. */
1988 tree
1989 finish_label_stmt (tree name)
1991 tree decl = define_label (input_location, name);
1993 if (decl == error_mark_node)
1994 return error_mark_node;
1996 add_stmt (build_stmt (input_location, LABEL_EXPR, decl));
1998 return decl;
2001 /* Finish a series of declarations for local labels. G++ allows users
2002 to declare "local" labels, i.e., labels with scope. This extension
2003 is useful when writing code involving statement-expressions. */
2005 void
2006 finish_label_decl (tree name)
2008 if (!at_function_scope_p ())
2010 error ("%<__label__%> declarations are only allowed in function scopes");
2011 return;
2014 add_decl_expr (declare_local_label (name));
2017 /* When DECL goes out of scope, make sure that CLEANUP is executed. */
2019 void
2020 finish_decl_cleanup (tree decl, tree cleanup)
2022 push_cleanup (decl, cleanup, false);
2025 /* If the current scope exits with an exception, run CLEANUP. */
2027 void
2028 finish_eh_cleanup (tree cleanup)
2030 push_cleanup (NULL, cleanup, true);
2033 /* The MEM_INITS is a list of mem-initializers, in reverse of the
2034 order they were written by the user. Each node is as for
2035 emit_mem_initializers. */
2037 void
2038 finish_mem_initializers (tree mem_inits)
2040 /* Reorder the MEM_INITS so that they are in the order they appeared
2041 in the source program. */
2042 mem_inits = nreverse (mem_inits);
2044 if (processing_template_decl)
2046 tree mem;
2048 for (mem = mem_inits; mem; mem = TREE_CHAIN (mem))
2050 /* If the TREE_PURPOSE is a TYPE_PACK_EXPANSION, skip the
2051 check for bare parameter packs in the TREE_VALUE, because
2052 any parameter packs in the TREE_VALUE have already been
2053 bound as part of the TREE_PURPOSE. See
2054 make_pack_expansion for more information. */
2055 if (TREE_CODE (TREE_PURPOSE (mem)) != TYPE_PACK_EXPANSION
2056 && check_for_bare_parameter_packs (TREE_VALUE (mem)))
2057 TREE_VALUE (mem) = error_mark_node;
2060 add_stmt (build_min_nt_loc (UNKNOWN_LOCATION,
2061 CTOR_INITIALIZER, mem_inits));
2063 else
2064 emit_mem_initializers (mem_inits);
2067 /* Obfuscate EXPR if it looks like an id-expression or member access so
2068 that the call to finish_decltype in do_auto_deduction will give the
2069 right result. If EVEN_UNEVAL, do this even in unevaluated context. */
2071 tree
2072 force_paren_expr (tree expr, bool even_uneval)
2074 /* This is only needed for decltype(auto) in C++14. */
2075 if (cxx_dialect < cxx14)
2076 return expr;
2078 /* If we're in unevaluated context, we can't be deducing a
2079 return/initializer type, so we don't need to mess with this. */
2080 if (cp_unevaluated_operand && !even_uneval)
2081 return expr;
2083 if (TREE_CODE (expr) == COMPONENT_REF
2084 || TREE_CODE (expr) == SCOPE_REF
2085 || REFERENCE_REF_P (expr))
2086 REF_PARENTHESIZED_P (expr) = true;
2087 else if (DECL_P (tree_strip_any_location_wrapper (expr)))
2089 location_t loc = cp_expr_location (expr);
2090 const tree_code code = processing_template_decl ? PAREN_EXPR
2091 : VIEW_CONVERT_EXPR;
2092 expr = build1_loc (loc, code, TREE_TYPE (expr), expr);
2093 REF_PARENTHESIZED_P (expr) = true;
2095 return expr;
2098 /* If T is an id-expression obfuscated by force_paren_expr, undo the
2099 obfuscation and return the underlying id-expression. Otherwise
2100 return T. */
2102 tree
2103 maybe_undo_parenthesized_ref (tree t)
2105 if (cxx_dialect < cxx14)
2106 return t;
2108 if ((TREE_CODE (t) == PAREN_EXPR || TREE_CODE (t) == VIEW_CONVERT_EXPR)
2109 && REF_PARENTHESIZED_P (t))
2110 t = TREE_OPERAND (t, 0);
2112 return t;
2115 /* Finish a parenthesized expression EXPR. */
2117 cp_expr
2118 finish_parenthesized_expr (cp_expr expr)
2120 if (EXPR_P (expr))
2121 /* This inhibits warnings in c_common_truthvalue_conversion. */
2122 suppress_warning (expr, OPT_Wparentheses);
2124 if (TREE_CODE (expr) == OFFSET_REF
2125 || TREE_CODE (expr) == SCOPE_REF)
2126 /* [expr.unary.op]/3 The qualified id of a pointer-to-member must not be
2127 enclosed in parentheses. */
2128 PTRMEM_OK_P (expr) = 0;
2130 tree stripped_expr = tree_strip_any_location_wrapper (expr);
2131 if (TREE_CODE (stripped_expr) == STRING_CST)
2132 PAREN_STRING_LITERAL_P (stripped_expr) = 1;
2134 expr = cp_expr (force_paren_expr (expr), expr.get_location ());
2136 return expr;
2139 /* Finish a reference to a non-static data member (DECL) that is not
2140 preceded by `.' or `->'. */
2142 tree
2143 finish_non_static_data_member (tree decl, tree object, tree qualifying_scope,
2144 tsubst_flags_t complain /* = tf_warning_or_error */)
2146 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
2147 bool try_omp_private = !object && omp_private_member_map;
2148 tree ret;
2150 if (!object)
2152 tree scope = qualifying_scope;
2153 if (scope == NULL_TREE)
2155 scope = context_for_name_lookup (decl);
2156 if (!TYPE_P (scope))
2158 /* Can happen during error recovery (c++/85014). */
2159 gcc_assert (seen_error ());
2160 return error_mark_node;
2163 object = maybe_dummy_object (scope, NULL);
2166 object = maybe_resolve_dummy (object, true);
2167 if (object == error_mark_node)
2168 return error_mark_node;
2170 /* DR 613/850: Can use non-static data members without an associated
2171 object in sizeof/decltype/alignof. */
2172 if (is_dummy_object (object) && cp_unevaluated_operand == 0
2173 && (!processing_template_decl || !current_class_ref))
2175 if (complain & tf_error)
2177 if (current_function_decl
2178 && DECL_STATIC_FUNCTION_P (current_function_decl))
2179 error ("invalid use of member %qD in static member function", decl);
2180 else
2181 error ("invalid use of non-static data member %qD", decl);
2182 inform (DECL_SOURCE_LOCATION (decl), "declared here");
2185 return error_mark_node;
2188 if (current_class_ptr)
2189 TREE_USED (current_class_ptr) = 1;
2190 if (processing_template_decl)
2192 tree type = TREE_TYPE (decl);
2194 if (TYPE_REF_P (type))
2195 /* Quals on the object don't matter. */;
2196 else if (PACK_EXPANSION_P (type))
2197 /* Don't bother trying to represent this. */
2198 type = NULL_TREE;
2199 else
2201 /* Set the cv qualifiers. */
2202 int quals = cp_type_quals (TREE_TYPE (object));
2204 if (DECL_MUTABLE_P (decl))
2205 quals &= ~TYPE_QUAL_CONST;
2207 quals |= cp_type_quals (TREE_TYPE (decl));
2208 type = cp_build_qualified_type (type, quals);
2211 if (qualifying_scope)
2212 /* Wrap this in a SCOPE_REF for now. */
2213 ret = build_qualified_name (type, qualifying_scope, decl,
2214 /*template_p=*/false);
2215 else
2216 ret = (convert_from_reference
2217 (build_min (COMPONENT_REF, type, object, decl, NULL_TREE)));
2219 /* If PROCESSING_TEMPLATE_DECL is nonzero here, then
2220 QUALIFYING_SCOPE is also non-null. */
2221 else
2223 tree access_type = TREE_TYPE (object);
2225 if (!perform_or_defer_access_check (TYPE_BINFO (access_type), decl,
2226 decl, complain))
2227 return error_mark_node;
2229 /* If the data member was named `C::M', convert `*this' to `C'
2230 first. */
2231 if (qualifying_scope)
2233 tree binfo = NULL_TREE;
2234 object = build_scoped_ref (object, qualifying_scope,
2235 &binfo);
2238 ret = build_class_member_access_expr (object, decl,
2239 /*access_path=*/NULL_TREE,
2240 /*preserve_reference=*/false,
2241 complain);
2243 if (try_omp_private)
2245 tree *v = omp_private_member_map->get (decl);
2246 if (v)
2247 ret = convert_from_reference (*v);
2249 return ret;
2252 /* DECL was the declaration to which a qualified-id resolved. Issue
2253 an error message if it is not accessible. If OBJECT_TYPE is
2254 non-NULL, we have just seen `x->' or `x.' and OBJECT_TYPE is the
2255 type of `*x', or `x', respectively. If the DECL was named as
2256 `A::B' then NESTED_NAME_SPECIFIER is `A'. Return value is like
2257 perform_access_checks above. */
2259 bool
2260 check_accessibility_of_qualified_id (tree decl,
2261 tree object_type,
2262 tree nested_name_specifier,
2263 tsubst_flags_t complain)
2265 /* If we're not checking, return immediately. */
2266 if (deferred_access_no_check)
2267 return true;
2269 /* Determine the SCOPE of DECL. */
2270 tree scope = context_for_name_lookup (decl);
2271 /* If the SCOPE is not a type, then DECL is not a member. */
2272 if (!TYPE_P (scope)
2273 /* If SCOPE is dependent then we can't perform this access check now,
2274 and since we'll perform this access check again after substitution
2275 there's no need to explicitly defer it. */
2276 || dependent_type_p (scope))
2277 return true;
2279 tree qualifying_type = NULL_TREE;
2280 /* Compute the scope through which DECL is being accessed. */
2281 if (object_type
2282 /* OBJECT_TYPE might not be a class type; consider:
2284 class A { typedef int I; };
2285 I *p;
2286 p->A::I::~I();
2288 In this case, we will have "A::I" as the DECL, but "I" as the
2289 OBJECT_TYPE. */
2290 && CLASS_TYPE_P (object_type)
2291 && DERIVED_FROM_P (scope, object_type))
2292 /* If we are processing a `->' or `.' expression, use the type of the
2293 left-hand side. */
2294 qualifying_type = object_type;
2295 else if (nested_name_specifier)
2297 /* If the reference is to a non-static member of the
2298 current class, treat it as if it were referenced through
2299 `this'. */
2300 if (DECL_NONSTATIC_MEMBER_P (decl)
2301 && current_class_ptr)
2302 if (tree current = current_nonlambda_class_type ())
2304 if (dependent_type_p (current))
2305 /* In general we can't know whether this access goes through
2306 `this' until instantiation time. Punt now, or else we might
2307 create a deferred access check that's not relative to `this'
2308 when it ought to be. We'll check this access again after
2309 substitution, e.g. from tsubst_qualified_id. */
2310 return true;
2312 if (DERIVED_FROM_P (scope, current))
2313 qualifying_type = current;
2315 /* Otherwise, use the type indicated by the
2316 nested-name-specifier. */
2317 if (!qualifying_type)
2318 qualifying_type = nested_name_specifier;
2320 else
2321 /* Otherwise, the name must be from the current class or one of
2322 its bases. */
2323 qualifying_type = currently_open_derived_class (scope);
2325 if (qualifying_type
2326 /* It is possible for qualifying type to be a TEMPLATE_TYPE_PARM
2327 or similar in a default argument value. */
2328 && CLASS_TYPE_P (qualifying_type))
2329 return perform_or_defer_access_check (TYPE_BINFO (qualifying_type), decl,
2330 decl, complain);
2332 return true;
2335 /* EXPR is the result of a qualified-id. The QUALIFYING_CLASS was the
2336 class named to the left of the "::" operator. DONE is true if this
2337 expression is a complete postfix-expression; it is false if this
2338 expression is followed by '->', '[', '(', etc. ADDRESS_P is true
2339 iff this expression is the operand of '&'. TEMPLATE_P is true iff
2340 the qualified-id was of the form "A::template B". TEMPLATE_ARG_P
2341 is true iff this qualified name appears as a template argument. */
2343 tree
2344 finish_qualified_id_expr (tree qualifying_class,
2345 tree expr,
2346 bool done,
2347 bool address_p,
2348 bool template_p,
2349 bool template_arg_p,
2350 tsubst_flags_t complain)
2352 gcc_assert (TYPE_P (qualifying_class));
2354 if (error_operand_p (expr))
2355 return error_mark_node;
2357 if (DECL_P (expr)
2358 /* Functions are marked after overload resolution; avoid redundant
2359 warnings. */
2360 && TREE_CODE (expr) != FUNCTION_DECL
2361 && !mark_used (expr, complain))
2362 return error_mark_node;
2364 if (template_p)
2366 if (TREE_CODE (expr) == UNBOUND_CLASS_TEMPLATE)
2368 /* cp_parser_lookup_name thought we were looking for a type,
2369 but we're actually looking for a declaration. */
2370 qualifying_class = TYPE_CONTEXT (expr);
2371 expr = TYPE_IDENTIFIER (expr);
2373 else
2374 check_template_keyword (expr);
2377 /* If EXPR occurs as the operand of '&', use special handling that
2378 permits a pointer-to-member. */
2379 if (address_p && done)
2381 if (TREE_CODE (expr) == SCOPE_REF)
2382 expr = TREE_OPERAND (expr, 1);
2383 expr = build_offset_ref (qualifying_class, expr,
2384 /*address_p=*/true, complain);
2385 return expr;
2388 /* No need to check access within an enum. */
2389 if (TREE_CODE (qualifying_class) == ENUMERAL_TYPE
2390 && TREE_CODE (expr) != IDENTIFIER_NODE)
2391 return expr;
2393 /* Within the scope of a class, turn references to non-static
2394 members into expression of the form "this->...". */
2395 if (template_arg_p)
2396 /* But, within a template argument, we do not want make the
2397 transformation, as there is no "this" pointer. */
2399 else if (TREE_CODE (expr) == FIELD_DECL)
2401 push_deferring_access_checks (dk_no_check);
2402 expr = finish_non_static_data_member (expr, NULL_TREE,
2403 qualifying_class, complain);
2404 pop_deferring_access_checks ();
2406 else if (BASELINK_P (expr))
2408 /* See if any of the functions are non-static members. */
2409 /* If so, the expression may be relative to 'this'. */
2410 if (!shared_member_p (expr)
2411 && current_class_ptr
2412 && DERIVED_FROM_P (qualifying_class,
2413 current_nonlambda_class_type ()))
2414 expr = (build_class_member_access_expr
2415 (maybe_dummy_object (qualifying_class, NULL),
2416 expr,
2417 BASELINK_ACCESS_BINFO (expr),
2418 /*preserve_reference=*/false,
2419 complain));
2420 else if (done)
2421 /* The expression is a qualified name whose address is not
2422 being taken. */
2423 expr = build_offset_ref (qualifying_class, expr, /*address_p=*/false,
2424 complain);
2426 else if (!template_p
2427 && TREE_CODE (expr) == TEMPLATE_DECL
2428 && !DECL_FUNCTION_TEMPLATE_P (expr))
2430 if (complain & tf_error)
2431 error ("%qE missing template arguments", expr);
2432 return error_mark_node;
2434 else
2436 /* In a template, return a SCOPE_REF for most qualified-ids
2437 so that we can check access at instantiation time. But if
2438 we're looking at a member of the current instantiation, we
2439 know we have access and building up the SCOPE_REF confuses
2440 non-type template argument handling. */
2441 if (processing_template_decl
2442 && (!currently_open_class (qualifying_class)
2443 || TREE_CODE (expr) == IDENTIFIER_NODE
2444 || TREE_CODE (expr) == TEMPLATE_ID_EXPR
2445 || TREE_CODE (expr) == BIT_NOT_EXPR))
2446 expr = build_qualified_name (TREE_TYPE (expr),
2447 qualifying_class, expr,
2448 template_p);
2449 else if (tree wrap = maybe_get_tls_wrapper_call (expr))
2450 expr = wrap;
2452 expr = convert_from_reference (expr);
2455 return expr;
2458 /* Begin a statement-expression. The value returned must be passed to
2459 finish_stmt_expr. */
2461 tree
2462 begin_stmt_expr (void)
2464 return push_stmt_list ();
2467 /* Process the final expression of a statement expression. EXPR can be
2468 NULL, if the final expression is empty. Return a STATEMENT_LIST
2469 containing all the statements in the statement-expression, or
2470 ERROR_MARK_NODE if there was an error. */
2472 tree
2473 finish_stmt_expr_expr (tree expr, tree stmt_expr)
2475 if (error_operand_p (expr))
2477 /* The type of the statement-expression is the type of the last
2478 expression. */
2479 TREE_TYPE (stmt_expr) = error_mark_node;
2480 return error_mark_node;
2483 /* If the last statement does not have "void" type, then the value
2484 of the last statement is the value of the entire expression. */
2485 if (expr)
2487 tree type = TREE_TYPE (expr);
2489 if (type && type_unknown_p (type))
2491 error ("a statement expression is an insufficient context"
2492 " for overload resolution");
2493 TREE_TYPE (stmt_expr) = error_mark_node;
2494 return error_mark_node;
2496 else if (processing_template_decl)
2498 expr = build_stmt (input_location, EXPR_STMT, expr);
2499 expr = add_stmt (expr);
2500 /* Mark the last statement so that we can recognize it as such at
2501 template-instantiation time. */
2502 EXPR_STMT_STMT_EXPR_RESULT (expr) = 1;
2504 else if (VOID_TYPE_P (type))
2506 /* Just treat this like an ordinary statement. */
2507 expr = finish_expr_stmt (expr);
2509 else
2511 /* It actually has a value we need to deal with. First, force it
2512 to be an rvalue so that we won't need to build up a copy
2513 constructor call later when we try to assign it to something. */
2514 expr = force_rvalue (expr, tf_warning_or_error);
2515 if (error_operand_p (expr))
2516 return error_mark_node;
2518 /* Update for array-to-pointer decay. */
2519 type = TREE_TYPE (expr);
2521 /* This TARGET_EXPR will initialize the outer one added by
2522 finish_stmt_expr. */
2523 set_target_expr_eliding (expr);
2525 /* Wrap it in a CLEANUP_POINT_EXPR and add it to the list like a
2526 normal statement, but don't convert to void or actually add
2527 the EXPR_STMT. */
2528 if (TREE_CODE (expr) != CLEANUP_POINT_EXPR)
2529 expr = maybe_cleanup_point_expr (expr);
2530 add_stmt (expr);
2533 /* The type of the statement-expression is the type of the last
2534 expression. */
2535 TREE_TYPE (stmt_expr) = type;
2538 return stmt_expr;
2541 /* Finish a statement-expression. EXPR should be the value returned
2542 by the previous begin_stmt_expr. Returns an expression
2543 representing the statement-expression. */
2545 tree
2546 finish_stmt_expr (tree stmt_expr, bool has_no_scope)
2548 tree type;
2549 tree result;
2551 if (error_operand_p (stmt_expr))
2553 pop_stmt_list (stmt_expr);
2554 return error_mark_node;
2557 gcc_assert (TREE_CODE (stmt_expr) == STATEMENT_LIST);
2559 type = TREE_TYPE (stmt_expr);
2560 result = pop_stmt_list (stmt_expr);
2561 TREE_TYPE (result) = type;
2563 if (processing_template_decl)
2565 result = build_min (STMT_EXPR, type, result);
2566 TREE_SIDE_EFFECTS (result) = 1;
2567 STMT_EXPR_NO_SCOPE (result) = has_no_scope;
2569 else if (CLASS_TYPE_P (type))
2571 /* Wrap the statement-expression in a TARGET_EXPR so that the
2572 temporary object created by the final expression is destroyed at
2573 the end of the full-expression containing the
2574 statement-expression. */
2575 result = force_target_expr (type, result, tf_warning_or_error);
2578 return result;
2581 /* Returns the expression which provides the value of STMT_EXPR. */
2583 tree
2584 stmt_expr_value_expr (tree stmt_expr)
2586 tree t = STMT_EXPR_STMT (stmt_expr);
2588 if (TREE_CODE (t) == BIND_EXPR)
2589 t = BIND_EXPR_BODY (t);
2591 if (TREE_CODE (t) == STATEMENT_LIST && STATEMENT_LIST_TAIL (t))
2592 t = STATEMENT_LIST_TAIL (t)->stmt;
2594 if (TREE_CODE (t) == EXPR_STMT)
2595 t = EXPR_STMT_EXPR (t);
2597 return t;
2600 /* Return TRUE iff EXPR_STMT is an empty list of
2601 expression statements. */
2603 bool
2604 empty_expr_stmt_p (tree expr_stmt)
2606 tree body = NULL_TREE;
2608 if (expr_stmt == void_node)
2609 return true;
2611 if (expr_stmt)
2613 if (TREE_CODE (expr_stmt) == EXPR_STMT)
2614 body = EXPR_STMT_EXPR (expr_stmt);
2615 else if (TREE_CODE (expr_stmt) == STATEMENT_LIST)
2616 body = expr_stmt;
2619 if (body)
2621 if (TREE_CODE (body) == STATEMENT_LIST)
2622 return tsi_end_p (tsi_start (body));
2623 else
2624 return empty_expr_stmt_p (body);
2626 return false;
2629 /* Perform Koenig lookup. FN_EXPR is the postfix-expression representing
2630 the function (or functions) to call; ARGS are the arguments to the
2631 call. Returns the functions to be considered by overload resolution. */
2633 cp_expr
2634 perform_koenig_lookup (cp_expr fn_expr, vec<tree, va_gc> *args,
2635 tsubst_flags_t complain)
2637 tree identifier = NULL_TREE;
2638 tree functions = NULL_TREE;
2639 tree tmpl_args = NULL_TREE;
2640 bool template_id = false;
2641 location_t loc = fn_expr.get_location ();
2642 tree fn = fn_expr.get_value ();
2644 STRIP_ANY_LOCATION_WRAPPER (fn);
2646 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
2648 /* Use a separate flag to handle null args. */
2649 template_id = true;
2650 tmpl_args = TREE_OPERAND (fn, 1);
2651 fn = TREE_OPERAND (fn, 0);
2654 /* Find the name of the overloaded function. */
2655 if (identifier_p (fn))
2656 identifier = fn;
2657 else
2659 functions = fn;
2660 identifier = OVL_NAME (functions);
2663 /* A call to a namespace-scope function using an unqualified name.
2665 Do Koenig lookup -- unless any of the arguments are
2666 type-dependent. */
2667 if (!any_type_dependent_arguments_p (args)
2668 && !any_dependent_template_arguments_p (tmpl_args))
2670 fn = lookup_arg_dependent (identifier, functions, args);
2671 if (!fn)
2673 /* The unqualified name could not be resolved. */
2674 if (complain & tf_error)
2675 fn = unqualified_fn_lookup_error (cp_expr (identifier, loc));
2676 else
2677 fn = identifier;
2681 if (fn && template_id && fn != error_mark_node)
2682 fn = build2 (TEMPLATE_ID_EXPR, unknown_type_node, fn, tmpl_args);
2684 return cp_expr (fn, loc);
2687 /* Generate an expression for `FN (ARGS)'. This may change the
2688 contents of ARGS.
2690 If DISALLOW_VIRTUAL is true, the call to FN will be not generated
2691 as a virtual call, even if FN is virtual. (This flag is set when
2692 encountering an expression where the function name is explicitly
2693 qualified. For example a call to `X::f' never generates a virtual
2694 call.)
2696 Returns code for the call. */
2698 tree
2699 finish_call_expr (tree fn, vec<tree, va_gc> **args, bool disallow_virtual,
2700 bool koenig_p, tsubst_flags_t complain)
2702 tree result;
2703 tree orig_fn;
2704 vec<tree, va_gc> *orig_args = *args;
2706 if (fn == error_mark_node)
2707 return error_mark_node;
2709 gcc_assert (!TYPE_P (fn));
2711 /* If FN may be a FUNCTION_DECL obfuscated by force_paren_expr, undo
2712 it so that we can tell this is a call to a known function. */
2713 fn = maybe_undo_parenthesized_ref (fn);
2715 STRIP_ANY_LOCATION_WRAPPER (fn);
2717 orig_fn = fn;
2719 if (processing_template_decl)
2721 /* If FN is a local extern declaration (or set thereof) in a template,
2722 look it up again at instantiation time. */
2723 if (is_overloaded_fn (fn))
2725 tree ifn = get_first_fn (fn);
2726 if (TREE_CODE (ifn) == FUNCTION_DECL
2727 && dependent_local_decl_p (ifn))
2728 orig_fn = DECL_NAME (ifn);
2731 /* If the call expression is dependent, build a CALL_EXPR node
2732 with no type; type_dependent_expression_p recognizes
2733 expressions with no type as being dependent. */
2734 if (type_dependent_expression_p (fn)
2735 || any_type_dependent_arguments_p (*args))
2737 result = build_min_nt_call_vec (orig_fn, *args);
2738 SET_EXPR_LOCATION (result, cp_expr_loc_or_input_loc (fn));
2739 KOENIG_LOOKUP_P (result) = koenig_p;
2740 /* Disable the std::move warnings since this call was dependent
2741 (c++/89780, c++/107363). This also suppresses the
2742 -Wredundant-move warning. */
2743 suppress_warning (result, OPT_Wpessimizing_move);
2744 if (is_overloaded_fn (fn))
2745 fn = get_fns (fn);
2747 if (cfun)
2749 bool abnormal = true;
2750 for (lkp_iterator iter (fn); abnormal && iter; ++iter)
2752 tree fndecl = STRIP_TEMPLATE (*iter);
2753 if (TREE_CODE (fndecl) != FUNCTION_DECL
2754 || !TREE_THIS_VOLATILE (fndecl))
2755 abnormal = false;
2757 /* FIXME: Stop warning about falling off end of non-void
2758 function. But this is wrong. Even if we only see
2759 no-return fns at this point, we could select a
2760 future-defined return fn during instantiation. Or
2761 vice-versa. */
2762 if (abnormal)
2763 current_function_returns_abnormally = 1;
2765 return result;
2767 orig_args = make_tree_vector_copy (*args);
2768 if (!BASELINK_P (fn)
2769 && TREE_CODE (fn) != PSEUDO_DTOR_EXPR
2770 && TREE_TYPE (fn) != unknown_type_node)
2771 fn = build_non_dependent_expr (fn);
2772 make_args_non_dependent (*args);
2775 if (TREE_CODE (fn) == COMPONENT_REF)
2777 tree member = TREE_OPERAND (fn, 1);
2778 if (BASELINK_P (member))
2780 tree object = TREE_OPERAND (fn, 0);
2781 return build_new_method_call (object, member,
2782 args, NULL_TREE,
2783 (disallow_virtual
2784 ? LOOKUP_NORMAL | LOOKUP_NONVIRTUAL
2785 : LOOKUP_NORMAL),
2786 /*fn_p=*/NULL,
2787 complain);
2791 /* Per 13.3.1.1, '(&f)(...)' is the same as '(f)(...)'. */
2792 if (TREE_CODE (fn) == ADDR_EXPR
2793 && TREE_CODE (TREE_OPERAND (fn, 0)) == OVERLOAD)
2794 fn = TREE_OPERAND (fn, 0);
2796 if (is_overloaded_fn (fn))
2797 fn = baselink_for_fns (fn);
2799 result = NULL_TREE;
2800 if (BASELINK_P (fn))
2802 tree object;
2804 /* A call to a member function. From [over.call.func]:
2806 If the keyword this is in scope and refers to the class of
2807 that member function, or a derived class thereof, then the
2808 function call is transformed into a qualified function call
2809 using (*this) as the postfix-expression to the left of the
2810 . operator.... [Otherwise] a contrived object of type T
2811 becomes the implied object argument.
2813 In this situation:
2815 struct A { void f(); };
2816 struct B : public A {};
2817 struct C : public A { void g() { B::f(); }};
2819 "the class of that member function" refers to `A'. But 11.2
2820 [class.access.base] says that we need to convert 'this' to B* as
2821 part of the access, so we pass 'B' to maybe_dummy_object. */
2823 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (get_first_fn (fn)))
2825 /* A constructor call always uses a dummy object. (This constructor
2826 call which has the form A::A () is actually invalid and we are
2827 going to reject it later in build_new_method_call.) */
2828 object = build_dummy_object (BINFO_TYPE (BASELINK_ACCESS_BINFO (fn)));
2830 else
2831 object = maybe_dummy_object (BINFO_TYPE (BASELINK_ACCESS_BINFO (fn)),
2832 NULL);
2834 result = build_new_method_call (object, fn, args, NULL_TREE,
2835 (disallow_virtual
2836 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
2837 : LOOKUP_NORMAL),
2838 /*fn_p=*/NULL,
2839 complain);
2841 else if (concept_check_p (fn))
2843 /* FN is actually a template-id referring to a concept definition. */
2844 tree id = unpack_concept_check (fn);
2845 tree tmpl = TREE_OPERAND (id, 0);
2846 tree args = TREE_OPERAND (id, 1);
2848 if (!function_concept_p (tmpl))
2850 error_at (EXPR_LOC_OR_LOC (fn, input_location),
2851 "cannot call a concept as a function");
2852 return error_mark_node;
2855 /* Ensure the result is wrapped as a call expression. */
2856 result = build_concept_check (tmpl, args, tf_warning_or_error);
2858 else if (is_overloaded_fn (fn))
2860 /* If the function is an overloaded builtin, resolve it. */
2861 if (TREE_CODE (fn) == FUNCTION_DECL
2862 && (DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL
2863 || DECL_BUILT_IN_CLASS (fn) == BUILT_IN_MD))
2864 result = resolve_overloaded_builtin (input_location, fn, *args);
2866 if (!result)
2868 if (warn_sizeof_pointer_memaccess
2869 && (complain & tf_warning)
2870 && !vec_safe_is_empty (*args)
2871 && !processing_template_decl)
2873 location_t sizeof_arg_loc[3];
2874 tree sizeof_arg[3];
2875 unsigned int i;
2876 for (i = 0; i < 3; i++)
2878 tree t;
2880 sizeof_arg_loc[i] = UNKNOWN_LOCATION;
2881 sizeof_arg[i] = NULL_TREE;
2882 if (i >= (*args)->length ())
2883 continue;
2884 t = (**args)[i];
2885 if (TREE_CODE (t) != SIZEOF_EXPR)
2886 continue;
2887 if (SIZEOF_EXPR_TYPE_P (t))
2888 sizeof_arg[i] = TREE_TYPE (TREE_OPERAND (t, 0));
2889 else
2890 sizeof_arg[i] = TREE_OPERAND (t, 0);
2891 sizeof_arg_loc[i] = EXPR_LOCATION (t);
2893 sizeof_pointer_memaccess_warning
2894 (sizeof_arg_loc, fn, *args,
2895 sizeof_arg, same_type_ignoring_top_level_qualifiers_p);
2898 if ((complain & tf_warning)
2899 && TREE_CODE (fn) == FUNCTION_DECL
2900 && fndecl_built_in_p (fn, BUILT_IN_MEMSET)
2901 && vec_safe_length (*args) == 3
2902 && !any_type_dependent_arguments_p (*args))
2904 tree arg0 = (*orig_args)[0];
2905 tree arg1 = (*orig_args)[1];
2906 tree arg2 = (*orig_args)[2];
2907 int literal_mask = ((literal_integer_zerop (arg1) << 1)
2908 | (literal_integer_zerop (arg2) << 2));
2909 warn_for_memset (input_location, arg0, arg2, literal_mask);
2912 /* A call to a namespace-scope function. */
2913 result = build_new_function_call (fn, args, complain);
2916 else if (TREE_CODE (fn) == PSEUDO_DTOR_EXPR)
2918 if (!vec_safe_is_empty (*args))
2919 error ("arguments to destructor are not allowed");
2920 /* C++20/DR: If the postfix-expression names a pseudo-destructor (in
2921 which case the postfix-expression is a possibly-parenthesized class
2922 member access), the function call destroys the object of scalar type
2923 denoted by the object expression of the class member access. */
2924 tree ob = TREE_OPERAND (fn, 0);
2925 if (obvalue_p (ob))
2926 result = build_trivial_dtor_call (ob, true);
2927 else
2928 /* No location to clobber. */
2929 result = convert_to_void (ob, ICV_STATEMENT, complain);
2931 else if (CLASS_TYPE_P (TREE_TYPE (fn)))
2932 /* If the "function" is really an object of class type, it might
2933 have an overloaded `operator ()'. */
2934 result = build_op_call (fn, args, complain);
2936 if (!result)
2937 /* A call where the function is unknown. */
2938 result = cp_build_function_call_vec (fn, args, complain);
2940 if (processing_template_decl && result != error_mark_node)
2942 if (INDIRECT_REF_P (result))
2943 result = TREE_OPERAND (result, 0);
2945 /* Prune all but the selected function from the original overload
2946 set so that we can avoid some duplicate work at instantiation time. */
2947 if (TREE_CODE (result) == CALL_EXPR
2948 && really_overloaded_fn (orig_fn))
2950 orig_fn = CALL_EXPR_FN (result);
2951 if (TREE_CODE (orig_fn) == COMPONENT_REF)
2953 /* The non-dependent result of build_new_method_call. */
2954 orig_fn = TREE_OPERAND (orig_fn, 1);
2955 gcc_assert (BASELINK_P (orig_fn));
2959 result = build_call_vec (TREE_TYPE (result), orig_fn, orig_args);
2960 SET_EXPR_LOCATION (result, input_location);
2961 KOENIG_LOOKUP_P (result) = koenig_p;
2962 release_tree_vector (orig_args);
2963 result = convert_from_reference (result);
2966 return result;
2969 /* Finish a call to a postfix increment or decrement or EXPR. (Which
2970 is indicated by CODE, which should be POSTINCREMENT_EXPR or
2971 POSTDECREMENT_EXPR.) */
2973 cp_expr
2974 finish_increment_expr (cp_expr expr, enum tree_code code)
2976 /* input_location holds the location of the trailing operator token.
2977 Build a location of the form:
2978 expr++
2979 ~~~~^~
2980 with the caret at the operator token, ranging from the start
2981 of EXPR to the end of the operator token. */
2982 location_t combined_loc = make_location (input_location,
2983 expr.get_start (),
2984 get_finish (input_location));
2985 cp_expr result = build_x_unary_op (combined_loc, code, expr,
2986 NULL_TREE, tf_warning_or_error);
2987 /* TODO: build_x_unary_op doesn't honor the location, so set it here. */
2988 result.set_location (combined_loc);
2989 return result;
2992 /* Finish a use of `this'. Returns an expression for `this'. */
2994 tree
2995 finish_this_expr (void)
2997 tree result = NULL_TREE;
2999 if (current_class_ptr)
3001 tree type = TREE_TYPE (current_class_ref);
3003 /* In a lambda expression, 'this' refers to the captured 'this'. */
3004 if (LAMBDA_TYPE_P (type))
3005 result = lambda_expr_this_capture (CLASSTYPE_LAMBDA_EXPR (type), true);
3006 else
3007 result = current_class_ptr;
3010 if (result)
3011 /* The keyword 'this' is a prvalue expression. */
3012 return rvalue (result);
3014 tree fn = current_nonlambda_function ();
3015 if (fn && DECL_STATIC_FUNCTION_P (fn))
3016 error ("%<this%> is unavailable for static member functions");
3017 else if (fn)
3018 error ("invalid use of %<this%> in non-member function");
3019 else
3020 error ("invalid use of %<this%> at top level");
3021 return error_mark_node;
3024 /* Finish a pseudo-destructor expression. If SCOPE is NULL, the
3025 expression was of the form `OBJECT.~DESTRUCTOR' where DESTRUCTOR is
3026 the TYPE for the type given. If SCOPE is non-NULL, the expression
3027 was of the form `OBJECT.SCOPE::~DESTRUCTOR'. */
3029 tree
3030 finish_pseudo_destructor_expr (tree object, tree scope, tree destructor,
3031 location_t loc)
3033 if (object == error_mark_node || destructor == error_mark_node)
3034 return error_mark_node;
3036 gcc_assert (TYPE_P (destructor));
3038 if (!processing_template_decl)
3040 if (scope == error_mark_node)
3042 error_at (loc, "invalid qualifying scope in pseudo-destructor name");
3043 return error_mark_node;
3045 if (is_auto (destructor))
3046 destructor = TREE_TYPE (object);
3047 if (scope && TYPE_P (scope) && !check_dtor_name (scope, destructor))
3049 error_at (loc,
3050 "qualified type %qT does not match destructor name ~%qT",
3051 scope, destructor);
3052 return error_mark_node;
3056 /* [expr.pseudo] says both:
3058 The type designated by the pseudo-destructor-name shall be
3059 the same as the object type.
3061 and:
3063 The cv-unqualified versions of the object type and of the
3064 type designated by the pseudo-destructor-name shall be the
3065 same type.
3067 We implement the more generous second sentence, since that is
3068 what most other compilers do. */
3069 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (object),
3070 destructor))
3072 error_at (loc, "%qE is not of type %qT", object, destructor);
3073 return error_mark_node;
3077 tree type = (type_dependent_expression_p (object)
3078 ? NULL_TREE : void_type_node);
3080 return build3_loc (loc, PSEUDO_DTOR_EXPR, type, object,
3081 scope, destructor);
3084 /* Finish an expression of the form CODE EXPR. */
3086 cp_expr
3087 finish_unary_op_expr (location_t op_loc, enum tree_code code, cp_expr expr,
3088 tsubst_flags_t complain)
3090 /* Build a location of the form:
3091 ++expr
3092 ^~~~~~
3093 with the caret at the operator token, ranging from the start
3094 of the operator token to the end of EXPR. */
3095 location_t combined_loc = make_location (op_loc,
3096 op_loc, expr.get_finish ());
3097 cp_expr result = build_x_unary_op (combined_loc, code, expr,
3098 NULL_TREE, complain);
3099 /* TODO: build_x_unary_op doesn't always honor the location. */
3100 result.set_location (combined_loc);
3102 if (result == error_mark_node)
3103 return result;
3105 if (!(complain & tf_warning))
3106 return result;
3108 tree result_ovl = result;
3109 tree expr_ovl = expr;
3111 if (!processing_template_decl)
3112 expr_ovl = cp_fully_fold (expr_ovl);
3114 if (!CONSTANT_CLASS_P (expr_ovl)
3115 || TREE_OVERFLOW_P (expr_ovl))
3116 return result;
3118 if (!processing_template_decl)
3119 result_ovl = cp_fully_fold (result_ovl);
3121 if (CONSTANT_CLASS_P (result_ovl) && TREE_OVERFLOW_P (result_ovl))
3122 overflow_warning (combined_loc, result_ovl);
3124 return result;
3127 /* Return true if CONSTRUCTOR EXPR after pack expansion could have no
3128 elements. */
3130 static bool
3131 maybe_zero_constructor_nelts (tree expr)
3133 if (CONSTRUCTOR_NELTS (expr) == 0)
3134 return true;
3135 if (!processing_template_decl)
3136 return false;
3137 for (constructor_elt &elt : CONSTRUCTOR_ELTS (expr))
3138 if (!PACK_EXPANSION_P (elt.value))
3139 return false;
3140 return true;
3143 /* Finish a compound-literal expression or C++11 functional cast with aggregate
3144 initializer. TYPE is the type to which the CONSTRUCTOR in COMPOUND_LITERAL
3145 is being cast. */
3147 tree
3148 finish_compound_literal (tree type, tree compound_literal,
3149 tsubst_flags_t complain,
3150 fcl_t fcl_context)
3152 if (type == error_mark_node)
3153 return error_mark_node;
3155 if (TYPE_REF_P (type))
3157 compound_literal
3158 = finish_compound_literal (TREE_TYPE (type), compound_literal,
3159 complain, fcl_context);
3160 /* The prvalue is then used to direct-initialize the reference. */
3161 tree r = (perform_implicit_conversion_flags
3162 (type, compound_literal, complain, LOOKUP_NORMAL));
3163 return convert_from_reference (r);
3166 if (!TYPE_OBJ_P (type))
3168 /* DR2351 */
3169 if (VOID_TYPE_P (type) && CONSTRUCTOR_NELTS (compound_literal) == 0)
3171 if (!processing_template_decl)
3172 return void_node;
3173 TREE_TYPE (compound_literal) = type;
3174 TREE_HAS_CONSTRUCTOR (compound_literal) = 1;
3175 CONSTRUCTOR_IS_DEPENDENT (compound_literal) = 0;
3176 return compound_literal;
3178 else if (VOID_TYPE_P (type)
3179 && processing_template_decl
3180 && maybe_zero_constructor_nelts (compound_literal))
3181 /* If there are only packs in compound_literal, it could
3182 be void{} after pack expansion. */;
3183 else
3185 if (complain & tf_error)
3186 error ("compound literal of non-object type %qT", type);
3187 return error_mark_node;
3191 if (template_placeholder_p (type))
3193 type = do_auto_deduction (type, compound_literal, type, complain,
3194 adc_variable_type);
3195 if (type == error_mark_node)
3196 return error_mark_node;
3198 /* C++23 auto{x}. */
3199 else if (is_auto (type)
3200 && !AUTO_IS_DECLTYPE (type)
3201 && CONSTRUCTOR_NELTS (compound_literal) == 1)
3203 if (is_constrained_auto (type))
3205 if (complain & tf_error)
3206 error ("%<auto{x}%> cannot be constrained");
3207 return error_mark_node;
3209 else if (cxx_dialect < cxx23)
3210 pedwarn (input_location, OPT_Wc__23_extensions,
3211 "%<auto{x}%> only available with "
3212 "%<-std=c++2b%> or %<-std=gnu++2b%>");
3213 type = do_auto_deduction (type, compound_literal, type, complain,
3214 adc_variable_type);
3215 if (type == error_mark_node)
3216 return error_mark_node;
3219 /* Used to hold a copy of the compound literal in a template. */
3220 tree orig_cl = NULL_TREE;
3222 if (processing_template_decl)
3224 const bool dependent_p
3225 = (instantiation_dependent_expression_p (compound_literal)
3226 || dependent_type_p (type));
3227 if (dependent_p)
3228 /* We're about to return, no need to copy. */
3229 orig_cl = compound_literal;
3230 else
3231 /* We're going to need a copy. */
3232 orig_cl = unshare_constructor (compound_literal);
3233 TREE_TYPE (orig_cl) = type;
3234 /* Mark the expression as a compound literal. */
3235 TREE_HAS_CONSTRUCTOR (orig_cl) = 1;
3236 /* And as instantiation-dependent. */
3237 CONSTRUCTOR_IS_DEPENDENT (orig_cl) = dependent_p;
3238 if (fcl_context == fcl_c99)
3239 CONSTRUCTOR_C99_COMPOUND_LITERAL (orig_cl) = 1;
3240 /* If the compound literal is dependent, we're done for now. */
3241 if (dependent_p)
3242 return orig_cl;
3243 /* Otherwise, do go on to e.g. check narrowing. */
3246 type = complete_type (type);
3248 if (TYPE_NON_AGGREGATE_CLASS (type))
3250 /* Trying to deal with a CONSTRUCTOR instead of a TREE_LIST
3251 everywhere that deals with function arguments would be a pain, so
3252 just wrap it in a TREE_LIST. The parser set a flag so we know
3253 that it came from T{} rather than T({}). */
3254 CONSTRUCTOR_IS_DIRECT_INIT (compound_literal) = 1;
3255 compound_literal = build_tree_list (NULL_TREE, compound_literal);
3256 return build_functional_cast (input_location, type,
3257 compound_literal, complain);
3260 if (TREE_CODE (type) == ARRAY_TYPE
3261 && check_array_initializer (NULL_TREE, type, compound_literal))
3262 return error_mark_node;
3263 compound_literal = reshape_init (type, compound_literal, complain);
3264 if (SCALAR_TYPE_P (type)
3265 && !BRACE_ENCLOSED_INITIALIZER_P (compound_literal)
3266 && !check_narrowing (type, compound_literal, complain))
3267 return error_mark_node;
3268 if (TREE_CODE (type) == ARRAY_TYPE
3269 && TYPE_DOMAIN (type) == NULL_TREE)
3271 cp_complete_array_type_or_error (&type, compound_literal,
3272 false, complain);
3273 if (type == error_mark_node)
3274 return error_mark_node;
3276 compound_literal = digest_init_flags (type, compound_literal,
3277 LOOKUP_NORMAL | LOOKUP_NO_NARROWING,
3278 complain);
3279 if (compound_literal == error_mark_node)
3280 return error_mark_node;
3282 /* If we're in a template, return the original compound literal. */
3283 if (orig_cl)
3284 return orig_cl;
3286 if (TREE_CODE (compound_literal) == CONSTRUCTOR)
3288 TREE_HAS_CONSTRUCTOR (compound_literal) = true;
3289 if (fcl_context == fcl_c99)
3290 CONSTRUCTOR_C99_COMPOUND_LITERAL (compound_literal) = 1;
3293 /* Put static/constant array temporaries in static variables. */
3294 /* FIXME all C99 compound literals should be variables rather than C++
3295 temporaries, unless they are used as an aggregate initializer. */
3296 if ((!at_function_scope_p () || CP_TYPE_CONST_P (type))
3297 && fcl_context == fcl_c99
3298 && TREE_CODE (type) == ARRAY_TYPE
3299 && !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
3300 && initializer_constant_valid_p (compound_literal, type))
3302 tree decl = create_temporary_var (type);
3303 DECL_CONTEXT (decl) = NULL_TREE;
3304 DECL_INITIAL (decl) = compound_literal;
3305 TREE_STATIC (decl) = 1;
3306 if (literal_type_p (type) && CP_TYPE_CONST_NON_VOLATILE_P (type))
3308 /* 5.19 says that a constant expression can include an
3309 lvalue-rvalue conversion applied to "a glvalue of literal type
3310 that refers to a non-volatile temporary object initialized
3311 with a constant expression". Rather than try to communicate
3312 that this VAR_DECL is a temporary, just mark it constexpr. */
3313 DECL_DECLARED_CONSTEXPR_P (decl) = true;
3314 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
3315 TREE_CONSTANT (decl) = true;
3317 cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
3318 decl = pushdecl_top_level (decl);
3319 DECL_NAME (decl) = make_anon_name ();
3320 SET_DECL_ASSEMBLER_NAME (decl, DECL_NAME (decl));
3321 /* Make sure the destructor is callable. */
3322 tree clean = cxx_maybe_build_cleanup (decl, complain);
3323 if (clean == error_mark_node)
3324 return error_mark_node;
3325 return decl;
3328 /* Represent other compound literals with TARGET_EXPR so we produce
3329 a prvalue, and can elide copies. */
3330 if (!VECTOR_TYPE_P (type)
3331 && (TREE_CODE (compound_literal) == CONSTRUCTOR
3332 || TREE_CODE (compound_literal) == VEC_INIT_EXPR))
3334 /* The CONSTRUCTOR is now an initializer, not a compound literal. */
3335 if (TREE_CODE (compound_literal) == CONSTRUCTOR)
3336 TREE_HAS_CONSTRUCTOR (compound_literal) = false;
3337 compound_literal = get_target_expr (compound_literal, complain);
3339 else
3340 /* For e.g. int{42} just make sure it's a prvalue. */
3341 compound_literal = rvalue (compound_literal);
3343 return compound_literal;
3346 /* Return the declaration for the function-name variable indicated by
3347 ID. */
3349 tree
3350 finish_fname (tree id)
3352 tree decl;
3354 decl = fname_decl (input_location, C_RID_CODE (id), id);
3355 if (processing_template_decl && current_function_decl
3356 && decl != error_mark_node)
3357 decl = DECL_NAME (decl);
3358 return decl;
3361 /* Finish a translation unit. */
3363 void
3364 finish_translation_unit (void)
3366 /* In case there were missing closebraces,
3367 get us back to the global binding level. */
3368 pop_everything ();
3369 while (current_namespace != global_namespace)
3370 pop_namespace ();
3372 /* Do file scope __FUNCTION__ et al. */
3373 finish_fname_decls ();
3375 if (vec_safe_length (scope_chain->omp_declare_target_attribute))
3377 cp_omp_declare_target_attr
3378 a = scope_chain->omp_declare_target_attribute->pop ();
3379 if (!errorcount)
3380 error ("%qs without corresponding %qs",
3381 a.device_type >= 0 ? "#pragma omp begin declare target"
3382 : "#pragma omp declare target",
3383 "#pragma omp end declare target");
3384 vec_safe_truncate (scope_chain->omp_declare_target_attribute, 0);
3386 if (vec_safe_length (scope_chain->omp_begin_assumes))
3388 if (!errorcount)
3389 error ("%qs without corresponding %qs",
3390 "#pragma omp begin assumes", "#pragma omp end assumes");
3391 vec_safe_truncate (scope_chain->omp_begin_assumes, 0);
3395 /* Finish a template type parameter, specified as AGGR IDENTIFIER.
3396 Returns the parameter. */
3398 tree
3399 finish_template_type_parm (tree aggr, tree identifier)
3401 if (aggr != class_type_node)
3403 permerror (input_location, "template type parameters must use the keyword %<class%> or %<typename%>");
3404 aggr = class_type_node;
3407 return build_tree_list (aggr, identifier);
3410 /* Finish a template template parameter, specified as AGGR IDENTIFIER.
3411 Returns the parameter. */
3413 tree
3414 finish_template_template_parm (tree aggr, tree identifier)
3416 tree decl = build_decl (input_location,
3417 TYPE_DECL, identifier, NULL_TREE);
3419 tree tmpl = build_lang_decl (TEMPLATE_DECL, identifier, NULL_TREE);
3420 DECL_TEMPLATE_PARMS (tmpl) = current_template_parms;
3421 DECL_TEMPLATE_RESULT (tmpl) = decl;
3422 DECL_ARTIFICIAL (decl) = 1;
3424 /* Associate the constraints with the underlying declaration,
3425 not the template. */
3426 tree constr = current_template_constraints ();
3427 set_constraints (decl, constr);
3429 end_template_decl ();
3431 gcc_assert (DECL_TEMPLATE_PARMS (tmpl));
3433 check_default_tmpl_args (decl, DECL_TEMPLATE_PARMS (tmpl),
3434 /*is_primary=*/true, /*is_partial=*/false,
3435 /*is_friend=*/0);
3437 return finish_template_type_parm (aggr, tmpl);
3440 /* ARGUMENT is the default-argument value for a template template
3441 parameter. If ARGUMENT is invalid, issue error messages and return
3442 the ERROR_MARK_NODE. Otherwise, ARGUMENT itself is returned. */
3444 tree
3445 check_template_template_default_arg (tree argument)
3447 if (TREE_CODE (argument) != TEMPLATE_DECL
3448 && TREE_CODE (argument) != TEMPLATE_TEMPLATE_PARM
3449 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
3451 if (TREE_CODE (argument) == TYPE_DECL)
3452 error ("invalid use of type %qT as a default value for a template "
3453 "template-parameter", TREE_TYPE (argument));
3454 else
3455 error ("invalid default argument for a template template parameter");
3456 return error_mark_node;
3459 return argument;
3462 /* Begin a class definition, as indicated by T. */
3464 tree
3465 begin_class_definition (tree t)
3467 if (error_operand_p (t) || error_operand_p (TYPE_MAIN_DECL (t)))
3468 return error_mark_node;
3470 if (processing_template_parmlist && !LAMBDA_TYPE_P (t))
3472 error ("definition of %q#T inside template parameter list", t);
3473 return error_mark_node;
3476 /* According to the C++ ABI, decimal classes defined in ISO/IEC TR 24733
3477 are passed the same as decimal scalar types. */
3478 if (TREE_CODE (t) == RECORD_TYPE
3479 && !processing_template_decl)
3481 tree ns = TYPE_CONTEXT (t);
3482 if (ns && TREE_CODE (ns) == NAMESPACE_DECL
3483 && DECL_CONTEXT (ns) == std_node
3484 && DECL_NAME (ns)
3485 && id_equal (DECL_NAME (ns), "decimal"))
3487 const char *n = TYPE_NAME_STRING (t);
3488 if ((strcmp (n, "decimal32") == 0)
3489 || (strcmp (n, "decimal64") == 0)
3490 || (strcmp (n, "decimal128") == 0))
3491 TYPE_TRANSPARENT_AGGR (t) = 1;
3495 /* A non-implicit typename comes from code like:
3497 template <typename T> struct A {
3498 template <typename U> struct A<T>::B ...
3500 This is erroneous. */
3501 else if (TREE_CODE (t) == TYPENAME_TYPE)
3503 error ("invalid definition of qualified type %qT", t);
3504 t = error_mark_node;
3507 if (t == error_mark_node || ! MAYBE_CLASS_TYPE_P (t))
3509 t = make_class_type (RECORD_TYPE);
3510 pushtag (make_anon_name (), t);
3513 if (TYPE_BEING_DEFINED (t))
3515 t = make_class_type (TREE_CODE (t));
3516 pushtag (TYPE_IDENTIFIER (t), t);
3519 if (modules_p ())
3521 if (!module_may_redeclare (TYPE_NAME (t)))
3523 error ("cannot declare %qD in a different module", TYPE_NAME (t));
3524 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t)), "declared here");
3525 return error_mark_node;
3527 set_instantiating_module (TYPE_NAME (t));
3528 set_defining_module (TYPE_NAME (t));
3531 maybe_process_partial_specialization (t);
3532 pushclass (t);
3533 TYPE_BEING_DEFINED (t) = 1;
3534 class_binding_level->defining_class_p = 1;
3536 if (flag_pack_struct)
3538 tree v;
3539 TYPE_PACKED (t) = 1;
3540 /* Even though the type is being defined for the first time
3541 here, there might have been a forward declaration, so there
3542 might be cv-qualified variants of T. */
3543 for (v = TYPE_NEXT_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v))
3544 TYPE_PACKED (v) = 1;
3546 /* Reset the interface data, at the earliest possible
3547 moment, as it might have been set via a class foo;
3548 before. */
3549 if (! TYPE_UNNAMED_P (t))
3551 struct c_fileinfo *finfo = \
3552 get_fileinfo (LOCATION_FILE (input_location));
3553 CLASSTYPE_INTERFACE_ONLY (t) = finfo->interface_only;
3554 SET_CLASSTYPE_INTERFACE_UNKNOWN_X
3555 (t, finfo->interface_unknown);
3557 reset_specialization ();
3559 /* Make a declaration for this class in its own scope. */
3560 build_self_reference ();
3562 return t;
3565 /* Finish the member declaration given by DECL. */
3567 void
3568 finish_member_declaration (tree decl)
3570 if (decl == error_mark_node || decl == NULL_TREE)
3571 return;
3573 if (decl == void_type_node)
3574 /* The COMPONENT was a friend, not a member, and so there's
3575 nothing for us to do. */
3576 return;
3578 /* We should see only one DECL at a time. */
3579 gcc_assert (DECL_CHAIN (decl) == NULL_TREE);
3581 /* Don't add decls after definition. */
3582 gcc_assert (TYPE_BEING_DEFINED (current_class_type)
3583 /* We can add lambda types when late parsing default
3584 arguments. */
3585 || LAMBDA_TYPE_P (TREE_TYPE (decl)));
3587 /* Set up access control for DECL. */
3588 TREE_PRIVATE (decl)
3589 = (current_access_specifier == access_private_node);
3590 TREE_PROTECTED (decl)
3591 = (current_access_specifier == access_protected_node);
3592 if (TREE_CODE (decl) == TEMPLATE_DECL)
3594 TREE_PRIVATE (DECL_TEMPLATE_RESULT (decl)) = TREE_PRIVATE (decl);
3595 TREE_PROTECTED (DECL_TEMPLATE_RESULT (decl)) = TREE_PROTECTED (decl);
3598 /* Mark the DECL as a member of the current class, unless it's
3599 a member of an enumeration. */
3600 if (TREE_CODE (decl) != CONST_DECL)
3601 DECL_CONTEXT (decl) = current_class_type;
3603 /* Remember the single FIELD_DECL an anonymous aggregate type is used for. */
3604 if (TREE_CODE (decl) == FIELD_DECL
3605 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
3607 gcc_assert (!ANON_AGGR_TYPE_FIELD (TYPE_MAIN_VARIANT (TREE_TYPE (decl))));
3608 ANON_AGGR_TYPE_FIELD (TYPE_MAIN_VARIANT (TREE_TYPE (decl))) = decl;
3611 if (TREE_CODE (decl) == USING_DECL)
3612 /* Avoid debug info for class-scope USING_DECLS for now, we'll
3613 call cp_emit_debug_info_for_using later. */
3614 DECL_IGNORED_P (decl) = 1;
3616 /* Check for bare parameter packs in the non-static data member
3617 declaration. */
3618 if (TREE_CODE (decl) == FIELD_DECL)
3620 if (check_for_bare_parameter_packs (TREE_TYPE (decl)))
3621 TREE_TYPE (decl) = error_mark_node;
3622 if (check_for_bare_parameter_packs (DECL_ATTRIBUTES (decl)))
3623 DECL_ATTRIBUTES (decl) = NULL_TREE;
3626 /* [dcl.link]
3628 A C language linkage is ignored for the names of class members
3629 and the member function type of class member functions. */
3630 if (DECL_LANG_SPECIFIC (decl))
3631 SET_DECL_LANGUAGE (decl, lang_cplusplus);
3633 bool add = false;
3635 /* Functions and non-functions are added differently. */
3636 if (DECL_DECLARES_FUNCTION_P (decl))
3637 add = add_method (current_class_type, decl, false);
3638 /* Enter the DECL into the scope of the class, if the class
3639 isn't a closure (whose fields are supposed to be unnamed). */
3640 else if (CLASSTYPE_LAMBDA_EXPR (current_class_type)
3641 || maybe_push_used_methods (decl)
3642 || pushdecl_class_level (decl))
3643 add = true;
3645 if (add)
3647 /* All TYPE_DECLs go at the end of TYPE_FIELDS. Ordinary fields
3648 go at the beginning. The reason is that
3649 legacy_nonfn_member_lookup searches the list in order, and we
3650 want a field name to override a type name so that the "struct
3651 stat hack" will work. In particular:
3653 struct S { enum E { }; static const int E = 5; int ary[S::E]; } s;
3655 is valid. */
3657 if (TREE_CODE (decl) == TYPE_DECL)
3658 TYPE_FIELDS (current_class_type)
3659 = chainon (TYPE_FIELDS (current_class_type), decl);
3660 else
3662 DECL_CHAIN (decl) = TYPE_FIELDS (current_class_type);
3663 TYPE_FIELDS (current_class_type) = decl;
3666 maybe_add_class_template_decl_list (current_class_type, decl,
3667 /*friend_p=*/0);
3671 /* Finish processing a complete template declaration. The PARMS are
3672 the template parameters. */
3674 void
3675 finish_template_decl (tree parms)
3677 if (parms)
3678 end_template_decl ();
3679 else
3680 end_specialization ();
3683 // Returns the template type of the class scope being entered. If we're
3684 // entering a constrained class scope. TYPE is the class template
3685 // scope being entered and we may need to match the intended type with
3686 // a constrained specialization. For example:
3688 // template<Object T>
3689 // struct S { void f(); }; #1
3691 // template<Object T>
3692 // void S<T>::f() { } #2
3694 // We check, in #2, that S<T> refers precisely to the type declared by
3695 // #1 (i.e., that the constraints match). Note that the following should
3696 // be an error since there is no specialization of S<T> that is
3697 // unconstrained, but this is not diagnosed here.
3699 // template<typename T>
3700 // void S<T>::f() { }
3702 // We cannot diagnose this problem here since this function also matches
3703 // qualified template names that are not part of a definition. For example:
3705 // template<Integral T, Floating_point U>
3706 // typename pair<T, U>::first_type void f(T, U);
3708 // Here, it is unlikely that there is a partial specialization of
3709 // pair constrained for for Integral and Floating_point arguments.
3711 // The general rule is: if a constrained specialization with matching
3712 // constraints is found return that type. Also note that if TYPE is not a
3713 // class-type (e.g. a typename type), then no fixup is needed.
3715 static tree
3716 fixup_template_type (tree type)
3718 // Find the template parameter list at the a depth appropriate to
3719 // the scope we're trying to enter.
3720 tree parms = current_template_parms;
3721 int depth = template_class_depth (type);
3722 for (int n = current_template_depth; n > depth && parms; --n)
3723 parms = TREE_CHAIN (parms);
3724 if (!parms)
3725 return type;
3726 tree cur_reqs = TEMPLATE_PARMS_CONSTRAINTS (parms);
3727 tree cur_constr = build_constraints (cur_reqs, NULL_TREE);
3729 // Search for a specialization whose type and constraints match.
3730 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
3731 tree specs = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
3732 while (specs)
3734 tree spec_constr = get_constraints (TREE_VALUE (specs));
3736 // If the type and constraints match a specialization, then we
3737 // are entering that type.
3738 if (same_type_p (type, TREE_TYPE (specs))
3739 && equivalent_constraints (cur_constr, spec_constr))
3740 return TREE_TYPE (specs);
3741 specs = TREE_CHAIN (specs);
3744 // If no specialization matches, then must return the type
3745 // previously found.
3746 return type;
3749 /* Finish processing a template-id (which names a type) of the form
3750 NAME < ARGS >. Return the TYPE_DECL for the type named by the
3751 template-id. If ENTERING_SCOPE is nonzero we are about to enter
3752 the scope of template-id indicated. */
3754 tree
3755 finish_template_type (tree name, tree args, int entering_scope)
3757 tree type;
3759 type = lookup_template_class (name, args,
3760 NULL_TREE, NULL_TREE, entering_scope,
3761 tf_warning_or_error | tf_user);
3763 /* If we might be entering the scope of a partial specialization,
3764 find the one with the right constraints. */
3765 if (flag_concepts
3766 && entering_scope
3767 && CLASS_TYPE_P (type)
3768 && CLASSTYPE_TEMPLATE_INFO (type)
3769 && dependent_type_p (type)
3770 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
3771 type = fixup_template_type (type);
3773 if (type == error_mark_node)
3774 return type;
3775 else if (CLASS_TYPE_P (type) && !alias_type_or_template_p (type))
3776 return TYPE_STUB_DECL (type);
3777 else
3778 return TYPE_NAME (type);
3781 /* Finish processing a BASE_CLASS with the indicated ACCESS_SPECIFIER.
3782 Return a TREE_LIST containing the ACCESS_SPECIFIER and the
3783 BASE_CLASS, or NULL_TREE if an error occurred. The
3784 ACCESS_SPECIFIER is one of
3785 access_{default,public,protected_private}_node. For a virtual base
3786 we set TREE_TYPE. */
3788 tree
3789 finish_base_specifier (tree base, tree access, bool virtual_p)
3791 tree result;
3793 if (base == error_mark_node)
3795 error ("invalid base-class specification");
3796 result = NULL_TREE;
3798 else if (! MAYBE_CLASS_TYPE_P (base))
3800 error ("%qT is not a class type", base);
3801 result = NULL_TREE;
3803 else
3805 if (cp_type_quals (base) != 0)
3807 /* DR 484: Can a base-specifier name a cv-qualified
3808 class type? */
3809 base = TYPE_MAIN_VARIANT (base);
3811 result = build_tree_list (access, base);
3812 if (virtual_p)
3813 TREE_TYPE (result) = integer_type_node;
3816 return result;
3819 /* If FNS is a member function, a set of member functions, or a
3820 template-id referring to one or more member functions, return a
3821 BASELINK for FNS, incorporating the current access context.
3822 Otherwise, return FNS unchanged. */
3824 tree
3825 baselink_for_fns (tree fns)
3827 tree scope;
3828 tree cl;
3830 if (BASELINK_P (fns)
3831 || error_operand_p (fns))
3832 return fns;
3834 scope = ovl_scope (fns);
3835 if (!CLASS_TYPE_P (scope))
3836 return fns;
3838 cl = currently_open_derived_class (scope);
3839 if (!cl)
3840 cl = scope;
3841 tree access_path = TYPE_BINFO (cl);
3842 tree conv_path = (cl == scope ? access_path
3843 : lookup_base (cl, scope, ba_any, NULL, tf_none));
3844 return build_baselink (conv_path, access_path, fns, /*optype=*/NULL_TREE);
3847 /* Returns true iff DECL is a variable from a function outside
3848 the current one. */
3850 static bool
3851 outer_var_p (tree decl)
3853 return ((VAR_P (decl) || TREE_CODE (decl) == PARM_DECL)
3854 && DECL_FUNCTION_SCOPE_P (decl)
3855 /* Don't get confused by temporaries. */
3856 && DECL_NAME (decl)
3857 && (DECL_CONTEXT (decl) != current_function_decl
3858 || parsing_nsdmi ()));
3861 /* As above, but also checks that DECL is automatic. */
3863 bool
3864 outer_automatic_var_p (tree decl)
3866 return (outer_var_p (decl)
3867 && !TREE_STATIC (decl));
3870 /* DECL satisfies outer_automatic_var_p. Possibly complain about it or
3871 rewrite it for lambda capture.
3873 If ODR_USE is true, we're being called from mark_use, and we complain about
3874 use of constant variables. If ODR_USE is false, we're being called for the
3875 id-expression, and we do lambda capture. */
3877 tree
3878 process_outer_var_ref (tree decl, tsubst_flags_t complain, bool odr_use)
3880 if (cp_unevaluated_operand)
3882 tree type = TREE_TYPE (decl);
3883 if (!dependent_type_p (type)
3884 && variably_modified_type_p (type, NULL_TREE))
3885 /* VLAs are used even in unevaluated context. */;
3886 else
3887 /* It's not a use (3.2) if we're in an unevaluated context. */
3888 return decl;
3890 if (decl == error_mark_node)
3891 return decl;
3893 tree context = DECL_CONTEXT (decl);
3894 tree containing_function = current_function_decl;
3895 tree lambda_stack = NULL_TREE;
3896 tree lambda_expr = NULL_TREE;
3897 tree initializer = convert_from_reference (decl);
3899 /* Mark it as used now even if the use is ill-formed. */
3900 if (!mark_used (decl, complain))
3901 return error_mark_node;
3903 if (parsing_nsdmi ())
3904 containing_function = NULL_TREE;
3906 if (containing_function && LAMBDA_FUNCTION_P (containing_function))
3908 /* Check whether we've already built a proxy. */
3909 tree var = decl;
3910 while (is_normal_capture_proxy (var))
3911 var = DECL_CAPTURED_VARIABLE (var);
3912 tree d = retrieve_local_specialization (var);
3914 if (d && d != decl && is_capture_proxy (d))
3916 if (DECL_CONTEXT (d) == containing_function)
3917 /* We already have an inner proxy. */
3918 return d;
3919 else
3920 /* We need to capture an outer proxy. */
3921 return process_outer_var_ref (d, complain, odr_use);
3925 /* If we are in a lambda function, we can move out until we hit
3926 1. the context,
3927 2. a non-lambda function, or
3928 3. a non-default capturing lambda function. */
3929 while (context != containing_function
3930 /* containing_function can be null with invalid generic lambdas. */
3931 && containing_function
3932 && LAMBDA_FUNCTION_P (containing_function))
3934 tree closure = DECL_CONTEXT (containing_function);
3935 lambda_expr = CLASSTYPE_LAMBDA_EXPR (closure);
3937 if (TYPE_CLASS_SCOPE_P (closure))
3938 /* A lambda in an NSDMI (c++/64496). */
3939 break;
3941 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_NONE)
3942 break;
3944 lambda_stack = tree_cons (NULL_TREE, lambda_expr, lambda_stack);
3946 containing_function = decl_function_context (containing_function);
3949 /* In a lambda within a template, wait until instantiation time to implicitly
3950 capture a parameter pack. We want to wait because we don't know if we're
3951 capturing the whole pack or a single element, and it's OK to wait because
3952 find_parameter_packs_r walks into the lambda body. */
3953 if (context == containing_function
3954 && DECL_PACK_P (decl))
3955 return decl;
3957 if (lambda_expr && VAR_P (decl) && DECL_ANON_UNION_VAR_P (decl))
3959 if (complain & tf_error)
3960 error ("cannot capture member %qD of anonymous union", decl);
3961 return error_mark_node;
3963 /* Do lambda capture when processing the id-expression, not when
3964 odr-using a variable. */
3965 if (!odr_use && context == containing_function)
3966 decl = add_default_capture (lambda_stack,
3967 /*id=*/DECL_NAME (decl), initializer);
3968 /* Only an odr-use of an outer automatic variable causes an
3969 error, and a constant variable can decay to a prvalue
3970 constant without odr-use. So don't complain yet. */
3971 else if (!odr_use && decl_constant_var_p (decl))
3972 return decl;
3973 else if (lambda_expr)
3975 if (complain & tf_error)
3977 error ("%qD is not captured", decl);
3978 tree closure = LAMBDA_EXPR_CLOSURE (lambda_expr);
3979 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_NONE)
3980 inform (location_of (closure),
3981 "the lambda has no capture-default");
3982 else if (TYPE_CLASS_SCOPE_P (closure))
3983 inform (UNKNOWN_LOCATION, "lambda in local class %q+T cannot "
3984 "capture variables from the enclosing context",
3985 TYPE_CONTEXT (closure));
3986 inform (DECL_SOURCE_LOCATION (decl), "%q#D declared here", decl);
3988 return error_mark_node;
3990 else
3992 if (complain & tf_error)
3994 error (VAR_P (decl)
3995 ? G_("use of local variable with automatic storage from "
3996 "containing function")
3997 : G_("use of parameter from containing function"));
3998 inform (DECL_SOURCE_LOCATION (decl), "%q#D declared here", decl);
4000 return error_mark_node;
4002 return decl;
4005 /* ID_EXPRESSION is a representation of parsed, but unprocessed,
4006 id-expression. (See cp_parser_id_expression for details.) SCOPE,
4007 if non-NULL, is the type or namespace used to explicitly qualify
4008 ID_EXPRESSION. DECL is the entity to which that name has been
4009 resolved.
4011 *CONSTANT_EXPRESSION_P is true if we are presently parsing a
4012 constant-expression. In that case, *NON_CONSTANT_EXPRESSION_P will
4013 be set to true if this expression isn't permitted in a
4014 constant-expression, but it is otherwise not set by this function.
4015 *ALLOW_NON_CONSTANT_EXPRESSION_P is true if we are parsing a
4016 constant-expression, but a non-constant expression is also
4017 permissible.
4019 DONE is true if this expression is a complete postfix-expression;
4020 it is false if this expression is followed by '->', '[', '(', etc.
4021 ADDRESS_P is true iff this expression is the operand of '&'.
4022 TEMPLATE_P is true iff the qualified-id was of the form
4023 "A::template B". TEMPLATE_ARG_P is true iff this qualified name
4024 appears as a template argument.
4026 If an error occurs, and it is the kind of error that might cause
4027 the parser to abort a tentative parse, *ERROR_MSG is filled in. It
4028 is the caller's responsibility to issue the message. *ERROR_MSG
4029 will be a string with static storage duration, so the caller need
4030 not "free" it.
4032 Return an expression for the entity, after issuing appropriate
4033 diagnostics. This function is also responsible for transforming a
4034 reference to a non-static member into a COMPONENT_REF that makes
4035 the use of "this" explicit.
4037 Upon return, *IDK will be filled in appropriately. */
4038 static cp_expr
4039 finish_id_expression_1 (tree id_expression,
4040 tree decl,
4041 tree scope,
4042 cp_id_kind *idk,
4043 bool integral_constant_expression_p,
4044 bool allow_non_integral_constant_expression_p,
4045 bool *non_integral_constant_expression_p,
4046 bool template_p,
4047 bool done,
4048 bool address_p,
4049 bool template_arg_p,
4050 const char **error_msg,
4051 location_t location)
4053 decl = strip_using_decl (decl);
4055 /* Initialize the output parameters. */
4056 *idk = CP_ID_KIND_NONE;
4057 *error_msg = NULL;
4059 if (id_expression == error_mark_node)
4060 return error_mark_node;
4061 /* If we have a template-id, then no further lookup is
4062 required. If the template-id was for a template-class, we
4063 will sometimes have a TYPE_DECL at this point. */
4064 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
4065 || TREE_CODE (decl) == TYPE_DECL)
4067 /* Look up the name. */
4068 else
4070 if (decl == error_mark_node)
4072 /* Name lookup failed. */
4073 if (scope
4074 && (!TYPE_P (scope)
4075 || (!dependent_type_p (scope)
4076 && !(identifier_p (id_expression)
4077 && IDENTIFIER_CONV_OP_P (id_expression)
4078 && dependent_type_p (TREE_TYPE (id_expression))))))
4080 /* If the qualifying type is non-dependent (and the name
4081 does not name a conversion operator to a dependent
4082 type), issue an error. */
4083 qualified_name_lookup_error (scope, id_expression, decl, location);
4084 return error_mark_node;
4086 else if (!scope)
4088 /* It may be resolved via Koenig lookup. */
4089 *idk = CP_ID_KIND_UNQUALIFIED;
4090 return id_expression;
4092 else
4093 decl = id_expression;
4096 /* Remember that the name was used in the definition of
4097 the current class so that we can check later to see if
4098 the meaning would have been different after the class
4099 was entirely defined. */
4100 if (!scope && decl != error_mark_node && identifier_p (id_expression))
4101 maybe_note_name_used_in_class (id_expression, decl);
4103 /* A use in unevaluated operand might not be instantiated appropriately
4104 if tsubst_copy builds a dummy parm, or if we never instantiate a
4105 generic lambda, so mark it now. */
4106 if (processing_template_decl && cp_unevaluated_operand)
4107 mark_type_use (decl);
4109 /* Disallow uses of local variables from containing functions, except
4110 within lambda-expressions. */
4111 if (outer_automatic_var_p (decl))
4113 decl = process_outer_var_ref (decl, tf_warning_or_error);
4114 if (decl == error_mark_node)
4115 return error_mark_node;
4118 /* Also disallow uses of function parameters outside the function
4119 body, except inside an unevaluated context (i.e. decltype). */
4120 if (TREE_CODE (decl) == PARM_DECL
4121 && DECL_CONTEXT (decl) == NULL_TREE
4122 && !cp_unevaluated_operand)
4124 *error_msg = G_("use of parameter outside function body");
4125 return error_mark_node;
4129 /* If we didn't find anything, or what we found was a type,
4130 then this wasn't really an id-expression. */
4131 if (TREE_CODE (decl) == TEMPLATE_DECL
4132 && !DECL_FUNCTION_TEMPLATE_P (decl))
4134 *error_msg = G_("missing template arguments");
4135 return error_mark_node;
4137 else if (TREE_CODE (decl) == TYPE_DECL
4138 || TREE_CODE (decl) == NAMESPACE_DECL)
4140 *error_msg = G_("expected primary-expression");
4141 return error_mark_node;
4144 /* If the name resolved to a template parameter, there is no
4145 need to look it up again later. */
4146 if ((TREE_CODE (decl) == CONST_DECL && DECL_TEMPLATE_PARM_P (decl))
4147 || TREE_CODE (decl) == TEMPLATE_PARM_INDEX)
4149 tree r;
4151 *idk = CP_ID_KIND_NONE;
4152 if (TREE_CODE (decl) == TEMPLATE_PARM_INDEX)
4153 decl = TEMPLATE_PARM_DECL (decl);
4154 r = DECL_INITIAL (decl);
4155 if (CLASS_TYPE_P (TREE_TYPE (r)) && !CP_TYPE_CONST_P (TREE_TYPE (r)))
4157 /* If the entity is a template parameter object for a template
4158 parameter of type T, the type of the expression is const T. */
4159 tree ctype = TREE_TYPE (r);
4160 ctype = cp_build_qualified_type (ctype, (cp_type_quals (ctype)
4161 | TYPE_QUAL_CONST));
4162 r = build1 (VIEW_CONVERT_EXPR, ctype, r);
4164 r = convert_from_reference (r);
4165 if (integral_constant_expression_p
4166 && !dependent_type_p (TREE_TYPE (decl))
4167 && !(INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (r))))
4169 if (!allow_non_integral_constant_expression_p)
4170 error ("template parameter %qD of type %qT is not allowed in "
4171 "an integral constant expression because it is not of "
4172 "integral or enumeration type", decl, TREE_TYPE (decl));
4173 *non_integral_constant_expression_p = true;
4175 return r;
4177 else if (TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE)
4179 gcc_checking_assert (scope);
4180 *idk = CP_ID_KIND_QUALIFIED;
4181 cp_warn_deprecated_use_scopes (scope);
4182 decl = finish_qualified_id_expr (scope, decl, done, address_p,
4183 template_p, template_arg_p,
4184 tf_warning_or_error);
4186 else
4188 bool dependent_p = type_dependent_expression_p (decl);
4190 /* If the declaration was explicitly qualified indicate
4191 that. The semantics of `A::f(3)' are different than
4192 `f(3)' if `f' is virtual. */
4193 *idk = (scope
4194 ? CP_ID_KIND_QUALIFIED
4195 : (TREE_CODE (decl) == TEMPLATE_ID_EXPR
4196 ? CP_ID_KIND_TEMPLATE_ID
4197 : (dependent_p
4198 ? CP_ID_KIND_UNQUALIFIED_DEPENDENT
4199 : CP_ID_KIND_UNQUALIFIED)));
4201 if (dependent_p
4202 && DECL_P (decl)
4203 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (decl)))
4204 /* Dependent type attributes on the decl mean that the TREE_TYPE is
4205 wrong, so just return the identifier. */
4206 return id_expression;
4208 if (DECL_CLASS_TEMPLATE_P (decl))
4210 error ("use of class template %qT as expression", decl);
4211 return error_mark_node;
4214 if (TREE_CODE (decl) == TREE_LIST)
4216 /* Ambiguous reference to base members. */
4217 error ("request for member %qD is ambiguous in "
4218 "multiple inheritance lattice", id_expression);
4219 print_candidates (decl);
4220 return error_mark_node;
4223 /* Mark variable-like entities as used. Functions are similarly
4224 marked either below or after overload resolution. */
4225 if ((VAR_P (decl)
4226 || TREE_CODE (decl) == PARM_DECL
4227 || TREE_CODE (decl) == CONST_DECL
4228 || TREE_CODE (decl) == RESULT_DECL)
4229 && !mark_used (decl))
4230 return error_mark_node;
4232 /* Only certain kinds of names are allowed in constant
4233 expression. Template parameters have already
4234 been handled above. */
4235 if (! error_operand_p (decl)
4236 && !dependent_p
4237 && integral_constant_expression_p
4238 && !decl_constant_var_p (decl)
4239 && TREE_CODE (decl) != CONST_DECL
4240 && !builtin_valid_in_constant_expr_p (decl)
4241 && !concept_check_p (decl))
4243 if (!allow_non_integral_constant_expression_p)
4245 error ("%qD cannot appear in a constant-expression", decl);
4246 return error_mark_node;
4248 *non_integral_constant_expression_p = true;
4251 if (tree wrap = maybe_get_tls_wrapper_call (decl))
4252 /* Replace an evaluated use of the thread_local variable with
4253 a call to its wrapper. */
4254 decl = wrap;
4255 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
4256 && !dependent_p
4257 && variable_template_p (TREE_OPERAND (decl, 0))
4258 && !concept_check_p (decl))
4260 decl = finish_template_variable (decl);
4261 mark_used (decl);
4262 decl = convert_from_reference (decl);
4264 else if (concept_check_p (decl))
4266 /* Nothing more to do. All of the analysis for concept checks
4267 is done by build_conept_id, called from the parser. */
4269 else if (scope)
4271 if (TREE_CODE (decl) == SCOPE_REF)
4273 gcc_assert (same_type_p (scope, TREE_OPERAND (decl, 0)));
4274 decl = TREE_OPERAND (decl, 1);
4277 decl = (adjust_result_of_qualified_name_lookup
4278 (decl, scope, current_nonlambda_class_type()));
4280 cp_warn_deprecated_use_scopes (scope);
4282 if (TYPE_P (scope))
4283 decl = finish_qualified_id_expr (scope,
4284 decl,
4285 done,
4286 address_p,
4287 template_p,
4288 template_arg_p,
4289 tf_warning_or_error);
4290 else
4291 decl = convert_from_reference (decl);
4293 else if (TREE_CODE (decl) == FIELD_DECL)
4295 /* Since SCOPE is NULL here, this is an unqualified name.
4296 Access checking has been performed during name lookup
4297 already. Turn off checking to avoid duplicate errors. */
4298 push_deferring_access_checks (dk_no_check);
4299 decl = finish_non_static_data_member (decl, NULL_TREE,
4300 /*qualifying_scope=*/NULL_TREE);
4301 pop_deferring_access_checks ();
4303 else if (is_overloaded_fn (decl))
4305 /* We only need to look at the first function,
4306 because all the fns share the attribute we're
4307 concerned with (all member fns or all non-members). */
4308 tree first_fn = get_first_fn (decl);
4309 first_fn = STRIP_TEMPLATE (first_fn);
4311 if (!template_arg_p
4312 && (TREE_CODE (first_fn) == USING_DECL
4313 || (TREE_CODE (first_fn) == FUNCTION_DECL
4314 && DECL_FUNCTION_MEMBER_P (first_fn)
4315 && !shared_member_p (decl))))
4317 /* A set of member functions. */
4318 decl = maybe_dummy_object (DECL_CONTEXT (first_fn), 0);
4319 return finish_class_member_access_expr (decl, id_expression,
4320 /*template_p=*/false,
4321 tf_warning_or_error);
4324 decl = baselink_for_fns (decl);
4326 else
4328 if (DECL_P (decl) && DECL_NONLOCAL (decl)
4329 && DECL_CLASS_SCOPE_P (decl))
4331 tree context = context_for_name_lookup (decl);
4332 if (context != current_class_type)
4334 tree path = currently_open_derived_class (context);
4335 if (!path)
4336 /* PATH can be null for using an enum of an unrelated
4337 class; we checked its access in lookup_using_decl.
4339 ??? Should this case make a clone instead, like
4340 handle_using_decl? */
4341 gcc_assert (TREE_CODE (decl) == CONST_DECL);
4342 else
4343 perform_or_defer_access_check (TYPE_BINFO (path),
4344 decl, decl,
4345 tf_warning_or_error);
4349 decl = convert_from_reference (decl);
4353 return cp_expr (decl, location);
4356 /* As per finish_id_expression_1, but adding a wrapper node
4357 around the result if needed to express LOCATION. */
4359 cp_expr
4360 finish_id_expression (tree id_expression,
4361 tree decl,
4362 tree scope,
4363 cp_id_kind *idk,
4364 bool integral_constant_expression_p,
4365 bool allow_non_integral_constant_expression_p,
4366 bool *non_integral_constant_expression_p,
4367 bool template_p,
4368 bool done,
4369 bool address_p,
4370 bool template_arg_p,
4371 const char **error_msg,
4372 location_t location)
4374 cp_expr result
4375 = finish_id_expression_1 (id_expression, decl, scope, idk,
4376 integral_constant_expression_p,
4377 allow_non_integral_constant_expression_p,
4378 non_integral_constant_expression_p,
4379 template_p, done, address_p, template_arg_p,
4380 error_msg, location);
4381 return result.maybe_add_location_wrapper ();
4384 /* Implement the __typeof keyword: Return the type of EXPR, suitable for
4385 use as a type-specifier. */
4387 tree
4388 finish_typeof (tree expr)
4390 tree type;
4392 if (type_dependent_expression_p (expr))
4394 type = cxx_make_type (TYPEOF_TYPE);
4395 TYPEOF_TYPE_EXPR (type) = expr;
4396 SET_TYPE_STRUCTURAL_EQUALITY (type);
4398 return type;
4401 expr = mark_type_use (expr);
4403 type = unlowered_expr_type (expr);
4405 if (!type || type == unknown_type_node)
4407 error ("type of %qE is unknown", expr);
4408 return error_mark_node;
4411 return type;
4414 /* Implement the __underlying_type keyword: Return the underlying
4415 type of TYPE, suitable for use as a type-specifier. */
4417 tree
4418 finish_underlying_type (tree type)
4420 if (!complete_type_or_else (type, NULL_TREE))
4421 return error_mark_node;
4423 if (TREE_CODE (type) != ENUMERAL_TYPE)
4425 error ("%qT is not an enumeration type", type);
4426 return error_mark_node;
4429 tree underlying_type = ENUM_UNDERLYING_TYPE (type);
4431 /* Fixup necessary in this case because ENUM_UNDERLYING_TYPE
4432 includes TYPE_MIN_VALUE and TYPE_MAX_VALUE information.
4433 See finish_enum_value_list for details. */
4434 if (!ENUM_FIXED_UNDERLYING_TYPE_P (type))
4435 underlying_type
4436 = c_common_type_for_mode (TYPE_MODE (underlying_type),
4437 TYPE_UNSIGNED (underlying_type));
4439 return underlying_type;
4442 /* Implement the __direct_bases keyword: Return the direct base classes
4443 of type. */
4445 tree
4446 calculate_direct_bases (tree type, tsubst_flags_t complain)
4448 if (!complete_type_or_maybe_complain (type, NULL_TREE, complain)
4449 || !NON_UNION_CLASS_TYPE_P (type))
4450 return make_tree_vec (0);
4452 releasing_vec vector;
4453 vec<tree, va_gc> *base_binfos = BINFO_BASE_BINFOS (TYPE_BINFO (type));
4454 tree binfo;
4455 unsigned i;
4457 /* Virtual bases are initialized first */
4458 for (i = 0; base_binfos->iterate (i, &binfo); i++)
4459 if (BINFO_VIRTUAL_P (binfo))
4460 vec_safe_push (vector, binfo);
4462 /* Now non-virtuals */
4463 for (i = 0; base_binfos->iterate (i, &binfo); i++)
4464 if (!BINFO_VIRTUAL_P (binfo))
4465 vec_safe_push (vector, binfo);
4467 tree bases_vec = make_tree_vec (vector->length ());
4469 for (i = 0; i < vector->length (); ++i)
4470 TREE_VEC_ELT (bases_vec, i) = BINFO_TYPE ((*vector)[i]);
4472 return bases_vec;
4475 /* Implement the __bases keyword: Return the base classes
4476 of type */
4478 /* Find morally non-virtual base classes by walking binfo hierarchy */
4479 /* Virtual base classes are handled separately in finish_bases */
4481 static tree
4482 dfs_calculate_bases_pre (tree binfo, void * /*data_*/)
4484 /* Don't walk bases of virtual bases */
4485 return BINFO_VIRTUAL_P (binfo) ? dfs_skip_bases : NULL_TREE;
4488 static tree
4489 dfs_calculate_bases_post (tree binfo, void *data_)
4491 vec<tree, va_gc> **data = ((vec<tree, va_gc> **) data_);
4492 if (!BINFO_VIRTUAL_P (binfo))
4493 vec_safe_push (*data, BINFO_TYPE (binfo));
4494 return NULL_TREE;
4497 /* Calculates the morally non-virtual base classes of a class */
4498 static vec<tree, va_gc> *
4499 calculate_bases_helper (tree type)
4501 vec<tree, va_gc> *vector = make_tree_vector ();
4503 /* Now add non-virtual base classes in order of construction */
4504 if (TYPE_BINFO (type))
4505 dfs_walk_all (TYPE_BINFO (type),
4506 dfs_calculate_bases_pre, dfs_calculate_bases_post, &vector);
4507 return vector;
4510 tree
4511 calculate_bases (tree type, tsubst_flags_t complain)
4513 if (!complete_type_or_maybe_complain (type, NULL_TREE, complain)
4514 || !NON_UNION_CLASS_TYPE_P (type))
4515 return make_tree_vec (0);
4517 releasing_vec vector;
4518 tree bases_vec = NULL_TREE;
4519 unsigned i;
4520 vec<tree, va_gc> *vbases;
4521 tree binfo;
4523 /* First go through virtual base classes */
4524 for (vbases = CLASSTYPE_VBASECLASSES (type), i = 0;
4525 vec_safe_iterate (vbases, i, &binfo); i++)
4527 releasing_vec vbase_bases
4528 = calculate_bases_helper (BINFO_TYPE (binfo));
4529 vec_safe_splice (vector, vbase_bases);
4532 /* Now for the non-virtual bases */
4533 releasing_vec nonvbases = calculate_bases_helper (type);
4534 vec_safe_splice (vector, nonvbases);
4536 /* Note that during error recovery vector->length can even be zero. */
4537 if (vector->length () > 1)
4539 /* Last element is entire class, so don't copy */
4540 bases_vec = make_tree_vec (vector->length () - 1);
4542 for (i = 0; i < vector->length () - 1; ++i)
4543 TREE_VEC_ELT (bases_vec, i) = (*vector)[i];
4545 else
4546 bases_vec = make_tree_vec (0);
4548 return bases_vec;
4551 tree
4552 finish_bases (tree type, bool direct)
4554 tree bases = NULL_TREE;
4556 if (!processing_template_decl)
4558 /* Parameter packs can only be used in templates */
4559 error ("parameter pack %<__bases%> only valid in template declaration");
4560 return error_mark_node;
4563 bases = cxx_make_type (BASES);
4564 BASES_TYPE (bases) = type;
4565 BASES_DIRECT (bases) = direct;
4566 SET_TYPE_STRUCTURAL_EQUALITY (bases);
4568 return bases;
4571 /* Perform C++-specific checks for __builtin_offsetof before calling
4572 fold_offsetof. */
4574 tree
4575 finish_offsetof (tree object_ptr, tree expr, location_t loc)
4577 /* If we're processing a template, we can't finish the semantics yet.
4578 Otherwise we can fold the entire expression now. */
4579 if (processing_template_decl)
4581 expr = build2 (OFFSETOF_EXPR, size_type_node, expr, object_ptr);
4582 SET_EXPR_LOCATION (expr, loc);
4583 return expr;
4586 if (expr == error_mark_node)
4587 return error_mark_node;
4589 if (TREE_CODE (expr) == PSEUDO_DTOR_EXPR)
4591 error ("cannot apply %<offsetof%> to destructor %<~%T%>",
4592 TREE_OPERAND (expr, 2));
4593 return error_mark_node;
4595 if (FUNC_OR_METHOD_TYPE_P (TREE_TYPE (expr))
4596 || TREE_TYPE (expr) == unknown_type_node)
4598 while (TREE_CODE (expr) == COMPONENT_REF
4599 || TREE_CODE (expr) == COMPOUND_EXPR)
4600 expr = TREE_OPERAND (expr, 1);
4602 if (DECL_P (expr))
4604 error ("cannot apply %<offsetof%> to member function %qD", expr);
4605 inform (DECL_SOURCE_LOCATION (expr), "declared here");
4607 else
4608 error ("cannot apply %<offsetof%> to member function");
4609 return error_mark_node;
4611 if (TREE_CODE (expr) == CONST_DECL)
4613 error ("cannot apply %<offsetof%> to an enumerator %qD", expr);
4614 return error_mark_node;
4616 if (REFERENCE_REF_P (expr))
4617 expr = TREE_OPERAND (expr, 0);
4618 if (!complete_type_or_else (TREE_TYPE (TREE_TYPE (object_ptr)), object_ptr))
4619 return error_mark_node;
4620 if (warn_invalid_offsetof
4621 && CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (object_ptr)))
4622 && CLASSTYPE_NON_STD_LAYOUT (TREE_TYPE (TREE_TYPE (object_ptr)))
4623 && cp_unevaluated_operand == 0)
4624 warning_at (loc, OPT_Winvalid_offsetof, "%<offsetof%> within "
4625 "non-standard-layout type %qT is conditionally-supported",
4626 TREE_TYPE (TREE_TYPE (object_ptr)));
4627 return fold_offsetof (expr);
4630 /* Replace the AGGR_INIT_EXPR at *TP with an equivalent CALL_EXPR. This
4631 function is broken out from the above for the benefit of the tree-ssa
4632 project. */
4634 void
4635 simplify_aggr_init_expr (tree *tp)
4637 tree aggr_init_expr = *tp;
4639 /* Form an appropriate CALL_EXPR. */
4640 tree fn = AGGR_INIT_EXPR_FN (aggr_init_expr);
4641 tree slot = AGGR_INIT_EXPR_SLOT (aggr_init_expr);
4642 tree type = TREE_TYPE (slot);
4644 tree call_expr;
4645 enum style_t { ctor, arg, pcc } style;
4647 if (AGGR_INIT_VIA_CTOR_P (aggr_init_expr))
4648 style = ctor;
4649 #ifdef PCC_STATIC_STRUCT_RETURN
4650 else if (1)
4651 style = pcc;
4652 #endif
4653 else
4655 gcc_assert (TREE_ADDRESSABLE (type));
4656 style = arg;
4659 call_expr = build_call_array_loc (input_location,
4660 TREE_TYPE (TREE_TYPE (TREE_TYPE (fn))),
4662 aggr_init_expr_nargs (aggr_init_expr),
4663 AGGR_INIT_EXPR_ARGP (aggr_init_expr));
4664 TREE_NOTHROW (call_expr) = TREE_NOTHROW (aggr_init_expr);
4665 CALL_FROM_THUNK_P (call_expr) = AGGR_INIT_FROM_THUNK_P (aggr_init_expr);
4666 CALL_EXPR_OPERATOR_SYNTAX (call_expr)
4667 = CALL_EXPR_OPERATOR_SYNTAX (aggr_init_expr);
4668 CALL_EXPR_ORDERED_ARGS (call_expr) = CALL_EXPR_ORDERED_ARGS (aggr_init_expr);
4669 CALL_EXPR_REVERSE_ARGS (call_expr) = CALL_EXPR_REVERSE_ARGS (aggr_init_expr);
4671 if (style == ctor)
4673 /* Replace the first argument to the ctor with the address of the
4674 slot. */
4675 cxx_mark_addressable (slot);
4676 CALL_EXPR_ARG (call_expr, 0) =
4677 build1 (ADDR_EXPR, build_pointer_type (type), slot);
4679 else if (style == arg)
4681 /* Just mark it addressable here, and leave the rest to
4682 expand_call{,_inline}. */
4683 cxx_mark_addressable (slot);
4684 CALL_EXPR_RETURN_SLOT_OPT (call_expr) = true;
4685 call_expr = cp_build_init_expr (slot, call_expr);
4687 else if (style == pcc)
4689 /* If we're using the non-reentrant PCC calling convention, then we
4690 need to copy the returned value out of the static buffer into the
4691 SLOT. */
4692 push_deferring_access_checks (dk_no_check);
4693 call_expr = build_aggr_init (slot, call_expr,
4694 DIRECT_BIND | LOOKUP_ONLYCONVERTING,
4695 tf_warning_or_error);
4696 pop_deferring_access_checks ();
4697 call_expr = build2 (COMPOUND_EXPR, TREE_TYPE (slot), call_expr, slot);
4700 if (AGGR_INIT_ZERO_FIRST (aggr_init_expr))
4702 tree init = build_zero_init (type, NULL_TREE,
4703 /*static_storage_p=*/false);
4704 init = cp_build_init_expr (slot, init);
4705 call_expr = build2 (COMPOUND_EXPR, TREE_TYPE (call_expr),
4706 init, call_expr);
4709 *tp = call_expr;
4712 /* Emit all thunks to FN that should be emitted when FN is emitted. */
4714 void
4715 emit_associated_thunks (tree fn)
4717 /* When we use vcall offsets, we emit thunks with the virtual
4718 functions to which they thunk. The whole point of vcall offsets
4719 is so that you can know statically the entire set of thunks that
4720 will ever be needed for a given virtual function, thereby
4721 enabling you to output all the thunks with the function itself. */
4722 if (DECL_VIRTUAL_P (fn)
4723 /* Do not emit thunks for extern template instantiations. */
4724 && ! DECL_REALLY_EXTERN (fn))
4726 tree thunk;
4728 for (thunk = DECL_THUNKS (fn); thunk; thunk = DECL_CHAIN (thunk))
4730 if (!THUNK_ALIAS (thunk))
4732 use_thunk (thunk, /*emit_p=*/1);
4733 if (DECL_RESULT_THUNK_P (thunk))
4735 tree probe;
4737 for (probe = DECL_THUNKS (thunk);
4738 probe; probe = DECL_CHAIN (probe))
4739 use_thunk (probe, /*emit_p=*/1);
4742 else
4743 gcc_assert (!DECL_THUNKS (thunk));
4748 /* Generate RTL for FN. */
4750 bool
4751 expand_or_defer_fn_1 (tree fn)
4753 /* When the parser calls us after finishing the body of a template
4754 function, we don't really want to expand the body. */
4755 if (processing_template_decl)
4757 /* Normally, collection only occurs in rest_of_compilation. So,
4758 if we don't collect here, we never collect junk generated
4759 during the processing of templates until we hit a
4760 non-template function. It's not safe to do this inside a
4761 nested class, though, as the parser may have local state that
4762 is not a GC root. */
4763 if (!function_depth)
4764 ggc_collect ();
4765 return false;
4768 gcc_assert (DECL_SAVED_TREE (fn));
4770 /* We make a decision about linkage for these functions at the end
4771 of the compilation. Until that point, we do not want the back
4772 end to output them -- but we do want it to see the bodies of
4773 these functions so that it can inline them as appropriate. */
4774 if (DECL_DECLARED_INLINE_P (fn) || DECL_IMPLICIT_INSTANTIATION (fn))
4776 if (DECL_INTERFACE_KNOWN (fn))
4777 /* We've already made a decision as to how this function will
4778 be handled. */;
4779 else if (!at_eof
4780 || DECL_IMMEDIATE_FUNCTION_P (fn)
4781 || DECL_OMP_DECLARE_REDUCTION_P (fn))
4782 tentative_decl_linkage (fn);
4783 else
4784 import_export_decl (fn);
4786 /* If the user wants us to keep all inline functions, then mark
4787 this function as needed so that finish_file will make sure to
4788 output it later. Similarly, all dllexport'd functions must
4789 be emitted; there may be callers in other DLLs. */
4790 if (DECL_DECLARED_INLINE_P (fn)
4791 && !DECL_REALLY_EXTERN (fn)
4792 && !DECL_IMMEDIATE_FUNCTION_P (fn)
4793 && !DECL_OMP_DECLARE_REDUCTION_P (fn)
4794 && (flag_keep_inline_functions
4795 || (flag_keep_inline_dllexport
4796 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (fn)))))
4798 mark_needed (fn);
4799 DECL_EXTERNAL (fn) = 0;
4803 /* If this is a constructor or destructor body, we have to clone
4804 it. */
4805 if (maybe_clone_body (fn))
4807 /* We don't want to process FN again, so pretend we've written
4808 it out, even though we haven't. */
4809 TREE_ASM_WRITTEN (fn) = 1;
4810 /* If this is a constexpr function, keep DECL_SAVED_TREE. */
4811 if (!DECL_DECLARED_CONSTEXPR_P (fn)
4812 && !(modules_p () && DECL_DECLARED_INLINE_P (fn)))
4813 DECL_SAVED_TREE (fn) = NULL_TREE;
4814 return false;
4817 /* There's no reason to do any of the work here if we're only doing
4818 semantic analysis; this code just generates RTL. */
4819 if (flag_syntax_only)
4821 /* Pretend that this function has been written out so that we don't try
4822 to expand it again. */
4823 TREE_ASM_WRITTEN (fn) = 1;
4824 return false;
4827 if (DECL_OMP_DECLARE_REDUCTION_P (fn))
4828 return false;
4830 return true;
4833 void
4834 expand_or_defer_fn (tree fn)
4836 if (expand_or_defer_fn_1 (fn))
4838 function_depth++;
4840 /* Expand or defer, at the whim of the compilation unit manager. */
4841 cgraph_node::finalize_function (fn, function_depth > 1);
4842 emit_associated_thunks (fn);
4844 function_depth--;
4846 if (DECL_IMMEDIATE_FUNCTION_P (fn))
4848 if (cgraph_node *node = cgraph_node::get (fn))
4850 node->body_removed = true;
4851 node->analyzed = false;
4852 node->definition = false;
4853 node->force_output = false;
4859 class nrv_data
4861 public:
4862 nrv_data () : visited (37) {}
4864 tree var;
4865 tree result;
4866 hash_table<nofree_ptr_hash <tree_node> > visited;
4869 /* Helper function for walk_tree, used by finalize_nrv below. */
4871 static tree
4872 finalize_nrv_r (tree* tp, int* walk_subtrees, void* data)
4874 class nrv_data *dp = (class nrv_data *)data;
4875 tree_node **slot;
4877 /* No need to walk into types. There wouldn't be any need to walk into
4878 non-statements, except that we have to consider STMT_EXPRs. */
4879 if (TYPE_P (*tp))
4880 *walk_subtrees = 0;
4881 /* Change all returns to just refer to the RESULT_DECL; this is a nop,
4882 but differs from using NULL_TREE in that it indicates that we care
4883 about the value of the RESULT_DECL. */
4884 else if (TREE_CODE (*tp) == RETURN_EXPR)
4885 TREE_OPERAND (*tp, 0) = dp->result;
4886 /* Change all cleanups for the NRV to only run when an exception is
4887 thrown. */
4888 else if (TREE_CODE (*tp) == CLEANUP_STMT
4889 && CLEANUP_DECL (*tp) == dp->var)
4890 CLEANUP_EH_ONLY (*tp) = 1;
4891 /* Replace the DECL_EXPR for the NRV with an initialization of the
4892 RESULT_DECL, if needed. */
4893 else if (TREE_CODE (*tp) == DECL_EXPR
4894 && DECL_EXPR_DECL (*tp) == dp->var)
4896 tree init;
4897 if (DECL_INITIAL (dp->var)
4898 && DECL_INITIAL (dp->var) != error_mark_node)
4899 init = cp_build_init_expr (dp->result,
4900 DECL_INITIAL (dp->var));
4901 else
4902 init = build_empty_stmt (EXPR_LOCATION (*tp));
4903 DECL_INITIAL (dp->var) = NULL_TREE;
4904 SET_EXPR_LOCATION (init, EXPR_LOCATION (*tp));
4905 *tp = init;
4907 /* And replace all uses of the NRV with the RESULT_DECL. */
4908 else if (*tp == dp->var)
4909 *tp = dp->result;
4911 /* Avoid walking into the same tree more than once. Unfortunately, we
4912 can't just use walk_tree_without duplicates because it would only call
4913 us for the first occurrence of dp->var in the function body. */
4914 slot = dp->visited.find_slot (*tp, INSERT);
4915 if (*slot)
4916 *walk_subtrees = 0;
4917 else
4918 *slot = *tp;
4920 /* Keep iterating. */
4921 return NULL_TREE;
4924 /* Called from finish_function to implement the named return value
4925 optimization by overriding all the RETURN_EXPRs and pertinent
4926 CLEANUP_STMTs and replacing all occurrences of VAR with RESULT, the
4927 RESULT_DECL for the function. */
4929 void
4930 finalize_nrv (tree *tp, tree var, tree result)
4932 class nrv_data data;
4934 /* Copy name from VAR to RESULT. */
4935 DECL_NAME (result) = DECL_NAME (var);
4936 /* Don't forget that we take its address. */
4937 TREE_ADDRESSABLE (result) = TREE_ADDRESSABLE (var);
4938 /* Finally set DECL_VALUE_EXPR to avoid assigning
4939 a stack slot at -O0 for the original var and debug info
4940 uses RESULT location for VAR. */
4941 SET_DECL_VALUE_EXPR (var, result);
4942 DECL_HAS_VALUE_EXPR_P (var) = 1;
4944 data.var = var;
4945 data.result = result;
4946 cp_walk_tree (tp, finalize_nrv_r, &data, 0);
4949 /* Create CP_OMP_CLAUSE_INFO for clause C. Returns true if it is invalid. */
4951 bool
4952 cxx_omp_create_clause_info (tree c, tree type, bool need_default_ctor,
4953 bool need_copy_ctor, bool need_copy_assignment,
4954 bool need_dtor)
4956 int save_errorcount = errorcount;
4957 tree info, t;
4959 /* Always allocate 3 elements for simplicity. These are the
4960 function decls for the ctor, dtor, and assignment op.
4961 This layout is known to the three lang hooks,
4962 cxx_omp_clause_default_init, cxx_omp_clause_copy_init,
4963 and cxx_omp_clause_assign_op. */
4964 info = make_tree_vec (3);
4965 CP_OMP_CLAUSE_INFO (c) = info;
4967 if (need_default_ctor || need_copy_ctor)
4969 if (need_default_ctor)
4970 t = get_default_ctor (type);
4971 else
4972 t = get_copy_ctor (type, tf_warning_or_error);
4974 if (t && !trivial_fn_p (t))
4975 TREE_VEC_ELT (info, 0) = t;
4978 if (need_dtor && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
4979 TREE_VEC_ELT (info, 1) = get_dtor (type, tf_warning_or_error);
4981 if (need_copy_assignment)
4983 t = get_copy_assign (type);
4985 if (t && !trivial_fn_p (t))
4986 TREE_VEC_ELT (info, 2) = t;
4989 return errorcount != save_errorcount;
4992 /* If DECL is DECL_OMP_PRIVATIZED_MEMBER, return corresponding
4993 FIELD_DECL, otherwise return DECL itself. */
4995 static tree
4996 omp_clause_decl_field (tree decl)
4998 if (VAR_P (decl)
4999 && DECL_HAS_VALUE_EXPR_P (decl)
5000 && DECL_ARTIFICIAL (decl)
5001 && DECL_LANG_SPECIFIC (decl)
5002 && DECL_OMP_PRIVATIZED_MEMBER (decl))
5004 tree f = DECL_VALUE_EXPR (decl);
5005 if (INDIRECT_REF_P (f))
5006 f = TREE_OPERAND (f, 0);
5007 if (TREE_CODE (f) == COMPONENT_REF)
5009 f = TREE_OPERAND (f, 1);
5010 gcc_assert (TREE_CODE (f) == FIELD_DECL);
5011 return f;
5014 return NULL_TREE;
5017 /* Adjust DECL if needed for printing using %qE. */
5019 static tree
5020 omp_clause_printable_decl (tree decl)
5022 tree t = omp_clause_decl_field (decl);
5023 if (t)
5024 return t;
5025 return decl;
5028 /* For a FIELD_DECL F and corresponding DECL_OMP_PRIVATIZED_MEMBER
5029 VAR_DECL T that doesn't need a DECL_EXPR added, record it for
5030 privatization. */
5032 static void
5033 omp_note_field_privatization (tree f, tree t)
5035 if (!omp_private_member_map)
5036 omp_private_member_map = new hash_map<tree, tree>;
5037 tree &v = omp_private_member_map->get_or_insert (f);
5038 if (v == NULL_TREE)
5040 v = t;
5041 omp_private_member_vec.safe_push (f);
5042 /* Signal that we don't want to create DECL_EXPR for this dummy var. */
5043 omp_private_member_vec.safe_push (integer_zero_node);
5047 /* Privatize FIELD_DECL T, return corresponding DECL_OMP_PRIVATIZED_MEMBER
5048 dummy VAR_DECL. */
5050 tree
5051 omp_privatize_field (tree t, bool shared)
5053 tree m = finish_non_static_data_member (t, NULL_TREE, NULL_TREE);
5054 if (m == error_mark_node)
5055 return error_mark_node;
5056 if (!omp_private_member_map && !shared)
5057 omp_private_member_map = new hash_map<tree, tree>;
5058 if (TYPE_REF_P (TREE_TYPE (t)))
5060 gcc_assert (INDIRECT_REF_P (m));
5061 m = TREE_OPERAND (m, 0);
5063 tree vb = NULL_TREE;
5064 tree &v = shared ? vb : omp_private_member_map->get_or_insert (t);
5065 if (v == NULL_TREE)
5067 v = create_temporary_var (TREE_TYPE (m));
5068 retrofit_lang_decl (v);
5069 DECL_OMP_PRIVATIZED_MEMBER (v) = 1;
5070 SET_DECL_VALUE_EXPR (v, m);
5071 DECL_HAS_VALUE_EXPR_P (v) = 1;
5072 if (!shared)
5073 omp_private_member_vec.safe_push (t);
5075 return v;
5078 /* Helper function for handle_omp_array_sections. Called recursively
5079 to handle multiple array-section-subscripts. C is the clause,
5080 T current expression (initially OMP_CLAUSE_DECL), which is either
5081 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
5082 expression if specified, TREE_VALUE length expression if specified,
5083 TREE_CHAIN is what it has been specified after, or some decl.
5084 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
5085 set to true if any of the array-section-subscript could have length
5086 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
5087 first array-section-subscript which is known not to have length
5088 of one. Given say:
5089 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
5090 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
5091 all are or may have length of 1, array-section-subscript [:2] is the
5092 first one known not to have length 1. For array-section-subscript
5093 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
5094 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
5095 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
5096 case though, as some lengths could be zero. */
5098 static tree
5099 handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
5100 bool &maybe_zero_len, unsigned int &first_non_one,
5101 enum c_omp_region_type ort)
5103 tree ret, low_bound, length, type;
5104 if (TREE_CODE (t) != TREE_LIST)
5106 if (error_operand_p (t))
5107 return error_mark_node;
5108 if (REFERENCE_REF_P (t)
5109 && TREE_CODE (TREE_OPERAND (t, 0)) == COMPONENT_REF)
5110 t = TREE_OPERAND (t, 0);
5111 ret = t;
5112 while (TREE_CODE (t) == INDIRECT_REF)
5114 t = TREE_OPERAND (t, 0);
5115 STRIP_NOPS (t);
5116 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
5117 t = TREE_OPERAND (t, 0);
5119 while (TREE_CODE (t) == COMPOUND_EXPR)
5121 t = TREE_OPERAND (t, 1);
5122 STRIP_NOPS (t);
5124 if (TREE_CODE (t) == COMPONENT_REF
5125 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
5126 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO
5127 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FROM)
5128 && !type_dependent_expression_p (t))
5130 if (TREE_CODE (TREE_OPERAND (t, 1)) == FIELD_DECL
5131 && DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
5133 error_at (OMP_CLAUSE_LOCATION (c),
5134 "bit-field %qE in %qs clause",
5135 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5136 return error_mark_node;
5138 while (TREE_CODE (t) == COMPONENT_REF)
5140 if (TREE_TYPE (TREE_OPERAND (t, 0))
5141 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == UNION_TYPE)
5143 error_at (OMP_CLAUSE_LOCATION (c),
5144 "%qE is a member of a union", t);
5145 return error_mark_node;
5147 t = TREE_OPERAND (t, 0);
5148 while (TREE_CODE (t) == MEM_REF
5149 || TREE_CODE (t) == INDIRECT_REF
5150 || TREE_CODE (t) == ARRAY_REF)
5152 t = TREE_OPERAND (t, 0);
5153 STRIP_NOPS (t);
5154 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
5155 t = TREE_OPERAND (t, 0);
5158 if (REFERENCE_REF_P (t))
5159 t = TREE_OPERAND (t, 0);
5161 if (TREE_CODE (t) == FIELD_DECL)
5162 ret = finish_non_static_data_member (t, NULL_TREE, NULL_TREE);
5163 else if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
5165 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
5166 return NULL_TREE;
5167 if (DECL_P (t))
5168 error_at (OMP_CLAUSE_LOCATION (c),
5169 "%qD is not a variable in %qs clause", t,
5170 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5171 else
5172 error_at (OMP_CLAUSE_LOCATION (c),
5173 "%qE is not a variable in %qs clause", t,
5174 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5175 return error_mark_node;
5177 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
5178 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
5179 && VAR_P (t) && CP_DECL_THREAD_LOCAL_P (t))
5181 error_at (OMP_CLAUSE_LOCATION (c),
5182 "%qD is threadprivate variable in %qs clause", t,
5183 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5184 return error_mark_node;
5186 if (type_dependent_expression_p (ret))
5187 return NULL_TREE;
5188 ret = convert_from_reference (ret);
5189 return ret;
5192 if ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP
5193 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
5194 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
5195 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
5196 && TREE_CODE (TREE_CHAIN (t)) == FIELD_DECL)
5197 TREE_CHAIN (t) = omp_privatize_field (TREE_CHAIN (t), false);
5198 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
5199 maybe_zero_len, first_non_one, ort);
5200 if (ret == error_mark_node || ret == NULL_TREE)
5201 return ret;
5203 type = TREE_TYPE (ret);
5204 low_bound = TREE_PURPOSE (t);
5205 length = TREE_VALUE (t);
5206 if ((low_bound && type_dependent_expression_p (low_bound))
5207 || (length && type_dependent_expression_p (length)))
5208 return NULL_TREE;
5210 if (low_bound == error_mark_node || length == error_mark_node)
5211 return error_mark_node;
5213 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
5215 error_at (OMP_CLAUSE_LOCATION (c),
5216 "low bound %qE of array section does not have integral type",
5217 low_bound);
5218 return error_mark_node;
5220 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
5222 error_at (OMP_CLAUSE_LOCATION (c),
5223 "length %qE of array section does not have integral type",
5224 length);
5225 return error_mark_node;
5227 if (low_bound)
5228 low_bound = mark_rvalue_use (low_bound);
5229 if (length)
5230 length = mark_rvalue_use (length);
5231 /* We need to reduce to real constant-values for checks below. */
5232 if (length)
5233 length = fold_simple (length);
5234 if (low_bound)
5235 low_bound = fold_simple (low_bound);
5236 if (low_bound
5237 && TREE_CODE (low_bound) == INTEGER_CST
5238 && TYPE_PRECISION (TREE_TYPE (low_bound))
5239 > TYPE_PRECISION (sizetype))
5240 low_bound = fold_convert (sizetype, low_bound);
5241 if (length
5242 && TREE_CODE (length) == INTEGER_CST
5243 && TYPE_PRECISION (TREE_TYPE (length))
5244 > TYPE_PRECISION (sizetype))
5245 length = fold_convert (sizetype, length);
5246 if (low_bound == NULL_TREE)
5247 low_bound = integer_zero_node;
5249 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
5250 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
5251 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH))
5253 if (length != integer_one_node)
5255 error_at (OMP_CLAUSE_LOCATION (c),
5256 "expected single pointer in %qs clause",
5257 user_omp_clause_code_name (c, ort == C_ORT_ACC));
5258 return error_mark_node;
5261 if (length != NULL_TREE)
5263 if (!integer_nonzerop (length))
5265 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY
5266 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
5267 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
5268 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
5269 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
5271 if (integer_zerop (length))
5273 error_at (OMP_CLAUSE_LOCATION (c),
5274 "zero length array section in %qs clause",
5275 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5276 return error_mark_node;
5279 else
5280 maybe_zero_len = true;
5282 if (first_non_one == types.length ()
5283 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
5284 first_non_one++;
5286 if (TREE_CODE (type) == ARRAY_TYPE)
5288 if (length == NULL_TREE
5289 && (TYPE_DOMAIN (type) == NULL_TREE
5290 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
5292 error_at (OMP_CLAUSE_LOCATION (c),
5293 "for unknown bound array type length expression must "
5294 "be specified");
5295 return error_mark_node;
5297 if (TREE_CODE (low_bound) == INTEGER_CST
5298 && tree_int_cst_sgn (low_bound) == -1)
5300 error_at (OMP_CLAUSE_LOCATION (c),
5301 "negative low bound in array section in %qs clause",
5302 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5303 return error_mark_node;
5305 if (length != NULL_TREE
5306 && TREE_CODE (length) == INTEGER_CST
5307 && tree_int_cst_sgn (length) == -1)
5309 error_at (OMP_CLAUSE_LOCATION (c),
5310 "negative length in array section in %qs clause",
5311 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5312 return error_mark_node;
5314 if (TYPE_DOMAIN (type)
5315 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
5316 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
5317 == INTEGER_CST)
5319 tree size
5320 = fold_convert (sizetype, TYPE_MAX_VALUE (TYPE_DOMAIN (type)));
5321 size = size_binop (PLUS_EXPR, size, size_one_node);
5322 if (TREE_CODE (low_bound) == INTEGER_CST)
5324 if (tree_int_cst_lt (size, low_bound))
5326 error_at (OMP_CLAUSE_LOCATION (c),
5327 "low bound %qE above array section size "
5328 "in %qs clause", low_bound,
5329 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5330 return error_mark_node;
5332 if (tree_int_cst_equal (size, low_bound))
5334 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY
5335 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
5336 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
5337 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
5338 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
5340 error_at (OMP_CLAUSE_LOCATION (c),
5341 "zero length array section in %qs clause",
5342 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5343 return error_mark_node;
5345 maybe_zero_len = true;
5347 else if (length == NULL_TREE
5348 && first_non_one == types.length ()
5349 && tree_int_cst_equal
5350 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
5351 low_bound))
5352 first_non_one++;
5354 else if (length == NULL_TREE)
5356 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
5357 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
5358 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
5359 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_IN_REDUCTION
5360 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_TASK_REDUCTION)
5361 maybe_zero_len = true;
5362 if (first_non_one == types.length ())
5363 first_non_one++;
5365 if (length && TREE_CODE (length) == INTEGER_CST)
5367 if (tree_int_cst_lt (size, length))
5369 error_at (OMP_CLAUSE_LOCATION (c),
5370 "length %qE above array section size "
5371 "in %qs clause", length,
5372 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5373 return error_mark_node;
5375 if (TREE_CODE (low_bound) == INTEGER_CST)
5377 tree lbpluslen
5378 = size_binop (PLUS_EXPR,
5379 fold_convert (sizetype, low_bound),
5380 fold_convert (sizetype, length));
5381 if (TREE_CODE (lbpluslen) == INTEGER_CST
5382 && tree_int_cst_lt (size, lbpluslen))
5384 error_at (OMP_CLAUSE_LOCATION (c),
5385 "high bound %qE above array section size "
5386 "in %qs clause", lbpluslen,
5387 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5388 return error_mark_node;
5393 else if (length == NULL_TREE)
5395 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
5396 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
5397 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
5398 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_IN_REDUCTION
5399 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_TASK_REDUCTION)
5400 maybe_zero_len = true;
5401 if (first_non_one == types.length ())
5402 first_non_one++;
5405 /* For [lb:] we will need to evaluate lb more than once. */
5406 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
5408 tree lb = cp_save_expr (low_bound);
5409 if (lb != low_bound)
5411 TREE_PURPOSE (t) = lb;
5412 low_bound = lb;
5416 else if (TYPE_PTR_P (type))
5418 if (length == NULL_TREE)
5420 if (TREE_CODE (ret) == PARM_DECL && DECL_ARRAY_PARAMETER_P (ret))
5421 error_at (OMP_CLAUSE_LOCATION (c),
5422 "for array function parameter length expression "
5423 "must be specified");
5424 else
5425 error_at (OMP_CLAUSE_LOCATION (c),
5426 "for pointer type length expression must be specified");
5427 return error_mark_node;
5429 if (length != NULL_TREE
5430 && TREE_CODE (length) == INTEGER_CST
5431 && tree_int_cst_sgn (length) == -1)
5433 error_at (OMP_CLAUSE_LOCATION (c),
5434 "negative length in array section in %qs clause",
5435 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5436 return error_mark_node;
5438 /* If there is a pointer type anywhere but in the very first
5439 array-section-subscript, the array section could be non-contiguous. */
5440 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
5441 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
5442 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
5444 /* If any prior dimension has a non-one length, then deem this
5445 array section as non-contiguous. */
5446 for (tree d = TREE_CHAIN (t); TREE_CODE (d) == TREE_LIST;
5447 d = TREE_CHAIN (d))
5449 tree d_length = TREE_VALUE (d);
5450 if (d_length == NULL_TREE || !integer_onep (d_length))
5452 error_at (OMP_CLAUSE_LOCATION (c),
5453 "array section is not contiguous in %qs clause",
5454 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5455 return error_mark_node;
5460 else
5462 error_at (OMP_CLAUSE_LOCATION (c),
5463 "%qE does not have pointer or array type", ret);
5464 return error_mark_node;
5466 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
5467 types.safe_push (TREE_TYPE (ret));
5468 /* We will need to evaluate lb more than once. */
5469 tree lb = cp_save_expr (low_bound);
5470 if (lb != low_bound)
5472 TREE_PURPOSE (t) = lb;
5473 low_bound = lb;
5475 /* Temporarily disable -fstrong-eval-order for array reductions.
5476 The SAVE_EXPR and COMPOUND_EXPR added if low_bound has side-effects
5477 is something the middle-end can't cope with and more importantly,
5478 it needs to be the actual base variable that is privatized, not some
5479 temporary assigned previous value of it. That, together with OpenMP
5480 saying how many times the side-effects are evaluated is unspecified,
5481 makes int *a, *b; ... reduction(+:a[a = b, 3:10]) really unspecified. */
5482 warning_sentinel s (flag_strong_eval_order,
5483 OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
5484 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
5485 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION);
5486 ret = grok_array_decl (OMP_CLAUSE_LOCATION (c), ret, low_bound, NULL,
5487 tf_warning_or_error);
5488 return ret;
5491 /* Handle array sections for clause C. */
5493 static bool
5494 handle_omp_array_sections (tree c, enum c_omp_region_type ort)
5496 bool maybe_zero_len = false;
5497 unsigned int first_non_one = 0;
5498 auto_vec<tree, 10> types;
5499 tree *tp = &OMP_CLAUSE_DECL (c);
5500 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
5501 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY)
5502 && TREE_CODE (*tp) == TREE_LIST
5503 && TREE_PURPOSE (*tp)
5504 && TREE_CODE (TREE_PURPOSE (*tp)) == TREE_VEC)
5505 tp = &TREE_VALUE (*tp);
5506 tree first = handle_omp_array_sections_1 (c, *tp, types,
5507 maybe_zero_len, first_non_one,
5508 ort);
5509 if (first == error_mark_node)
5510 return true;
5511 if (first == NULL_TREE)
5512 return false;
5513 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
5514 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY)
5516 tree t = *tp;
5517 tree tem = NULL_TREE;
5518 if (processing_template_decl)
5519 return false;
5520 /* Need to evaluate side effects in the length expressions
5521 if any. */
5522 while (TREE_CODE (t) == TREE_LIST)
5524 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
5526 if (tem == NULL_TREE)
5527 tem = TREE_VALUE (t);
5528 else
5529 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
5530 TREE_VALUE (t), tem);
5532 t = TREE_CHAIN (t);
5534 if (tem)
5535 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
5536 *tp = first;
5538 else
5540 unsigned int num = types.length (), i;
5541 tree t, side_effects = NULL_TREE, size = NULL_TREE;
5542 tree condition = NULL_TREE;
5544 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
5545 maybe_zero_len = true;
5546 if (processing_template_decl && maybe_zero_len)
5547 return false;
5549 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
5550 t = TREE_CHAIN (t))
5552 tree low_bound = TREE_PURPOSE (t);
5553 tree length = TREE_VALUE (t);
5555 i--;
5556 if (low_bound
5557 && TREE_CODE (low_bound) == INTEGER_CST
5558 && TYPE_PRECISION (TREE_TYPE (low_bound))
5559 > TYPE_PRECISION (sizetype))
5560 low_bound = fold_convert (sizetype, low_bound);
5561 if (length
5562 && TREE_CODE (length) == INTEGER_CST
5563 && TYPE_PRECISION (TREE_TYPE (length))
5564 > TYPE_PRECISION (sizetype))
5565 length = fold_convert (sizetype, length);
5566 if (low_bound == NULL_TREE)
5567 low_bound = integer_zero_node;
5568 if (!maybe_zero_len && i > first_non_one)
5570 if (integer_nonzerop (low_bound))
5571 goto do_warn_noncontiguous;
5572 if (length != NULL_TREE
5573 && TREE_CODE (length) == INTEGER_CST
5574 && TYPE_DOMAIN (types[i])
5575 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
5576 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
5577 == INTEGER_CST)
5579 tree size;
5580 size = size_binop (PLUS_EXPR,
5581 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
5582 size_one_node);
5583 if (!tree_int_cst_equal (length, size))
5585 do_warn_noncontiguous:
5586 error_at (OMP_CLAUSE_LOCATION (c),
5587 "array section is not contiguous in %qs "
5588 "clause",
5589 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5590 return true;
5593 if (!processing_template_decl
5594 && length != NULL_TREE
5595 && TREE_SIDE_EFFECTS (length))
5597 if (side_effects == NULL_TREE)
5598 side_effects = length;
5599 else
5600 side_effects = build2 (COMPOUND_EXPR,
5601 TREE_TYPE (side_effects),
5602 length, side_effects);
5605 else if (processing_template_decl)
5606 continue;
5607 else
5609 tree l;
5611 if (i > first_non_one
5612 && ((length && integer_nonzerop (length))
5613 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
5614 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
5615 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION))
5616 continue;
5617 if (length)
5618 l = fold_convert (sizetype, length);
5619 else
5621 l = size_binop (PLUS_EXPR,
5622 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
5623 size_one_node);
5624 l = size_binop (MINUS_EXPR, l,
5625 fold_convert (sizetype, low_bound));
5627 if (i > first_non_one)
5629 l = fold_build2 (NE_EXPR, boolean_type_node, l,
5630 size_zero_node);
5631 if (condition == NULL_TREE)
5632 condition = l;
5633 else
5634 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
5635 l, condition);
5637 else if (size == NULL_TREE)
5639 size = size_in_bytes (TREE_TYPE (types[i]));
5640 tree eltype = TREE_TYPE (types[num - 1]);
5641 while (TREE_CODE (eltype) == ARRAY_TYPE)
5642 eltype = TREE_TYPE (eltype);
5643 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
5644 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
5645 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
5646 size = size_binop (EXACT_DIV_EXPR, size,
5647 size_in_bytes (eltype));
5648 size = size_binop (MULT_EXPR, size, l);
5649 if (condition)
5650 size = fold_build3 (COND_EXPR, sizetype, condition,
5651 size, size_zero_node);
5653 else
5654 size = size_binop (MULT_EXPR, size, l);
5657 if (!processing_template_decl)
5659 if (side_effects)
5660 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
5661 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
5662 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
5663 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
5665 size = size_binop (MINUS_EXPR, size, size_one_node);
5666 size = save_expr (size);
5667 tree index_type = build_index_type (size);
5668 tree eltype = TREE_TYPE (first);
5669 while (TREE_CODE (eltype) == ARRAY_TYPE)
5670 eltype = TREE_TYPE (eltype);
5671 tree type = build_array_type (eltype, index_type);
5672 tree ptype = build_pointer_type (eltype);
5673 if (TYPE_REF_P (TREE_TYPE (t))
5674 && INDIRECT_TYPE_P (TREE_TYPE (TREE_TYPE (t))))
5675 t = convert_from_reference (t);
5676 else if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
5677 t = build_fold_addr_expr (t);
5678 tree t2 = build_fold_addr_expr (first);
5679 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
5680 ptrdiff_type_node, t2);
5681 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
5682 ptrdiff_type_node, t2,
5683 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
5684 ptrdiff_type_node, t));
5685 if (tree_fits_shwi_p (t2))
5686 t = build2 (MEM_REF, type, t,
5687 build_int_cst (ptype, tree_to_shwi (t2)));
5688 else
5690 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
5691 sizetype, t2);
5692 t = build2_loc (OMP_CLAUSE_LOCATION (c), POINTER_PLUS_EXPR,
5693 TREE_TYPE (t), t, t2);
5694 t = build2 (MEM_REF, type, t, build_int_cst (ptype, 0));
5696 OMP_CLAUSE_DECL (c) = t;
5697 return false;
5699 OMP_CLAUSE_DECL (c) = first;
5700 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_HAS_DEVICE_ADDR)
5701 return false;
5702 OMP_CLAUSE_SIZE (c) = size;
5703 if (TREE_CODE (t) == FIELD_DECL)
5704 t = finish_non_static_data_member (t, NULL_TREE, NULL_TREE);
5705 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
5706 || (TREE_CODE (t) == COMPONENT_REF
5707 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE))
5708 return false;
5709 switch (OMP_CLAUSE_MAP_KIND (c))
5711 case GOMP_MAP_ALLOC:
5712 case GOMP_MAP_IF_PRESENT:
5713 case GOMP_MAP_TO:
5714 case GOMP_MAP_FROM:
5715 case GOMP_MAP_TOFROM:
5716 case GOMP_MAP_ALWAYS_TO:
5717 case GOMP_MAP_ALWAYS_FROM:
5718 case GOMP_MAP_ALWAYS_TOFROM:
5719 case GOMP_MAP_RELEASE:
5720 case GOMP_MAP_DELETE:
5721 case GOMP_MAP_FORCE_TO:
5722 case GOMP_MAP_FORCE_FROM:
5723 case GOMP_MAP_FORCE_TOFROM:
5724 case GOMP_MAP_FORCE_PRESENT:
5725 OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (c) = 1;
5726 break;
5727 default:
5728 break;
5730 bool reference_always_pointer = true;
5731 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
5732 OMP_CLAUSE_MAP);
5733 if (TREE_CODE (t) == COMPONENT_REF)
5735 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ATTACH_DETACH);
5737 if ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP
5738 && TYPE_REF_P (TREE_TYPE (t)))
5740 if (TREE_CODE (TREE_TYPE (TREE_TYPE (t))) == ARRAY_TYPE)
5741 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ALWAYS_POINTER);
5742 else
5743 t = convert_from_reference (t);
5745 reference_always_pointer = false;
5748 else if (REFERENCE_REF_P (t)
5749 && TREE_CODE (TREE_OPERAND (t, 0)) == COMPONENT_REF)
5751 gomp_map_kind k;
5752 if ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP
5753 && TREE_CODE (TREE_TYPE (t)) == POINTER_TYPE)
5754 k = GOMP_MAP_ATTACH_DETACH;
5755 else
5757 t = TREE_OPERAND (t, 0);
5758 k = (ort == C_ORT_ACC
5759 ? GOMP_MAP_ATTACH_DETACH : GOMP_MAP_ALWAYS_POINTER);
5761 OMP_CLAUSE_SET_MAP_KIND (c2, k);
5763 else
5764 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_FIRSTPRIVATE_POINTER);
5765 OMP_CLAUSE_MAP_IMPLICIT (c2) = OMP_CLAUSE_MAP_IMPLICIT (c);
5766 if (OMP_CLAUSE_MAP_KIND (c2) != GOMP_MAP_FIRSTPRIVATE_POINTER
5767 && !cxx_mark_addressable (t))
5768 return false;
5769 OMP_CLAUSE_DECL (c2) = t;
5770 t = build_fold_addr_expr (first);
5771 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
5772 ptrdiff_type_node, t);
5773 tree ptr = OMP_CLAUSE_DECL (c2);
5774 ptr = convert_from_reference (ptr);
5775 if (!INDIRECT_TYPE_P (TREE_TYPE (ptr)))
5776 ptr = build_fold_addr_expr (ptr);
5777 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
5778 ptrdiff_type_node, t,
5779 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
5780 ptrdiff_type_node, ptr));
5781 OMP_CLAUSE_SIZE (c2) = t;
5782 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
5783 OMP_CLAUSE_CHAIN (c) = c2;
5785 ptr = OMP_CLAUSE_DECL (c2);
5786 if (reference_always_pointer
5787 && OMP_CLAUSE_MAP_KIND (c2) != GOMP_MAP_FIRSTPRIVATE_POINTER
5788 && TYPE_REF_P (TREE_TYPE (ptr))
5789 && INDIRECT_TYPE_P (TREE_TYPE (TREE_TYPE (ptr))))
5791 tree c3 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
5792 OMP_CLAUSE_MAP);
5793 OMP_CLAUSE_SET_MAP_KIND (c3, OMP_CLAUSE_MAP_KIND (c2));
5794 OMP_CLAUSE_MAP_IMPLICIT (c2) = OMP_CLAUSE_MAP_IMPLICIT (c);
5795 OMP_CLAUSE_DECL (c3) = ptr;
5796 if (OMP_CLAUSE_MAP_KIND (c2) == GOMP_MAP_ALWAYS_POINTER
5797 || OMP_CLAUSE_MAP_KIND (c2) == GOMP_MAP_ATTACH_DETACH)
5799 OMP_CLAUSE_DECL (c2) = build_simple_mem_ref (ptr);
5800 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ALWAYS_POINTER);
5802 else
5803 OMP_CLAUSE_DECL (c2) = convert_from_reference (ptr);
5804 OMP_CLAUSE_SIZE (c3) = size_zero_node;
5805 OMP_CLAUSE_CHAIN (c3) = OMP_CLAUSE_CHAIN (c2);
5806 OMP_CLAUSE_CHAIN (c2) = c3;
5810 return false;
5813 /* Return identifier to look up for omp declare reduction. */
5815 tree
5816 omp_reduction_id (enum tree_code reduction_code, tree reduction_id, tree type)
5818 const char *p = NULL;
5819 const char *m = NULL;
5820 switch (reduction_code)
5822 case PLUS_EXPR:
5823 case MULT_EXPR:
5824 case MINUS_EXPR:
5825 case BIT_AND_EXPR:
5826 case BIT_XOR_EXPR:
5827 case BIT_IOR_EXPR:
5828 case TRUTH_ANDIF_EXPR:
5829 case TRUTH_ORIF_EXPR:
5830 reduction_id = ovl_op_identifier (false, reduction_code);
5831 break;
5832 case MIN_EXPR:
5833 p = "min";
5834 break;
5835 case MAX_EXPR:
5836 p = "max";
5837 break;
5838 default:
5839 break;
5842 if (p == NULL)
5844 if (TREE_CODE (reduction_id) != IDENTIFIER_NODE)
5845 return error_mark_node;
5846 p = IDENTIFIER_POINTER (reduction_id);
5849 if (type != NULL_TREE)
5850 m = mangle_type_string (TYPE_MAIN_VARIANT (type));
5852 const char prefix[] = "omp declare reduction ";
5853 size_t lenp = sizeof (prefix);
5854 if (strncmp (p, prefix, lenp - 1) == 0)
5855 lenp = 1;
5856 size_t len = strlen (p);
5857 size_t lenm = m ? strlen (m) + 1 : 0;
5858 char *name = XALLOCAVEC (char, lenp + len + lenm);
5859 if (lenp > 1)
5860 memcpy (name, prefix, lenp - 1);
5861 memcpy (name + lenp - 1, p, len + 1);
5862 if (m)
5864 name[lenp + len - 1] = '~';
5865 memcpy (name + lenp + len, m, lenm);
5867 return get_identifier (name);
5870 /* Lookup OpenMP UDR ID for TYPE, return the corresponding artificial
5871 FUNCTION_DECL or NULL_TREE if not found. */
5873 static tree
5874 omp_reduction_lookup (location_t loc, tree id, tree type, tree *baselinkp,
5875 vec<tree> *ambiguousp)
5877 tree orig_id = id;
5878 tree baselink = NULL_TREE;
5879 if (identifier_p (id))
5881 cp_id_kind idk;
5882 bool nonint_cst_expression_p;
5883 const char *error_msg;
5884 id = omp_reduction_id (ERROR_MARK, id, type);
5885 tree decl = lookup_name (id);
5886 if (decl == NULL_TREE)
5887 decl = error_mark_node;
5888 id = finish_id_expression (id, decl, NULL_TREE, &idk, false, true,
5889 &nonint_cst_expression_p, false, true, false,
5890 false, &error_msg, loc);
5891 if (idk == CP_ID_KIND_UNQUALIFIED
5892 && identifier_p (id))
5894 vec<tree, va_gc> *args = NULL;
5895 vec_safe_push (args, build_reference_type (type));
5896 id = perform_koenig_lookup (id, args, tf_none);
5899 else if (TREE_CODE (id) == SCOPE_REF)
5900 id = lookup_qualified_name (TREE_OPERAND (id, 0),
5901 omp_reduction_id (ERROR_MARK,
5902 TREE_OPERAND (id, 1),
5903 type),
5904 LOOK_want::NORMAL, false);
5905 tree fns = id;
5906 id = NULL_TREE;
5907 if (fns && is_overloaded_fn (fns))
5909 for (lkp_iterator iter (get_fns (fns)); iter; ++iter)
5911 tree fndecl = *iter;
5912 if (TREE_CODE (fndecl) == FUNCTION_DECL)
5914 tree argtype = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
5915 if (same_type_p (TREE_TYPE (argtype), type))
5917 id = fndecl;
5918 break;
5923 if (id && BASELINK_P (fns))
5925 if (baselinkp)
5926 *baselinkp = fns;
5927 else
5928 baselink = fns;
5932 if (!id && CLASS_TYPE_P (type) && TYPE_BINFO (type))
5934 auto_vec<tree> ambiguous;
5935 tree binfo = TYPE_BINFO (type), base_binfo, ret = NULL_TREE;
5936 unsigned int ix;
5937 if (ambiguousp == NULL)
5938 ambiguousp = &ambiguous;
5939 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
5941 id = omp_reduction_lookup (loc, orig_id, BINFO_TYPE (base_binfo),
5942 baselinkp ? baselinkp : &baselink,
5943 ambiguousp);
5944 if (id == NULL_TREE)
5945 continue;
5946 if (!ambiguousp->is_empty ())
5947 ambiguousp->safe_push (id);
5948 else if (ret != NULL_TREE)
5950 ambiguousp->safe_push (ret);
5951 ambiguousp->safe_push (id);
5952 ret = NULL_TREE;
5954 else
5955 ret = id;
5957 if (ambiguousp != &ambiguous)
5958 return ret;
5959 if (!ambiguous.is_empty ())
5961 const char *str = _("candidates are:");
5962 unsigned int idx;
5963 tree udr;
5964 error_at (loc, "user defined reduction lookup is ambiguous");
5965 FOR_EACH_VEC_ELT (ambiguous, idx, udr)
5967 inform (DECL_SOURCE_LOCATION (udr), "%s %#qD", str, udr);
5968 if (idx == 0)
5969 str = get_spaces (str);
5971 ret = error_mark_node;
5972 baselink = NULL_TREE;
5974 id = ret;
5976 if (id && baselink)
5977 perform_or_defer_access_check (BASELINK_BINFO (baselink),
5978 id, id, tf_warning_or_error);
5979 return id;
5982 /* Helper function for cp_parser_omp_declare_reduction_exprs
5983 and tsubst_omp_udr.
5984 Remove CLEANUP_STMT for data (omp_priv variable).
5985 Also append INIT_EXPR for DECL_INITIAL of omp_priv after its
5986 DECL_EXPR. */
5988 tree
5989 cp_remove_omp_priv_cleanup_stmt (tree *tp, int *walk_subtrees, void *data)
5991 if (TYPE_P (*tp))
5992 *walk_subtrees = 0;
5993 else if (TREE_CODE (*tp) == CLEANUP_STMT && CLEANUP_DECL (*tp) == (tree) data)
5994 *tp = CLEANUP_BODY (*tp);
5995 else if (TREE_CODE (*tp) == DECL_EXPR)
5997 tree decl = DECL_EXPR_DECL (*tp);
5998 if (!processing_template_decl
5999 && decl == (tree) data
6000 && DECL_INITIAL (decl)
6001 && DECL_INITIAL (decl) != error_mark_node)
6003 tree list = NULL_TREE;
6004 append_to_statement_list_force (*tp, &list);
6005 tree init_expr = build2 (INIT_EXPR, void_type_node,
6006 decl, DECL_INITIAL (decl));
6007 DECL_INITIAL (decl) = NULL_TREE;
6008 append_to_statement_list_force (init_expr, &list);
6009 *tp = list;
6012 return NULL_TREE;
6015 /* Data passed from cp_check_omp_declare_reduction to
6016 cp_check_omp_declare_reduction_r. */
6018 struct cp_check_omp_declare_reduction_data
6020 location_t loc;
6021 tree stmts[7];
6022 bool combiner_p;
6025 /* Helper function for cp_check_omp_declare_reduction, called via
6026 cp_walk_tree. */
6028 static tree
6029 cp_check_omp_declare_reduction_r (tree *tp, int *, void *data)
6031 struct cp_check_omp_declare_reduction_data *udr_data
6032 = (struct cp_check_omp_declare_reduction_data *) data;
6033 if (SSA_VAR_P (*tp)
6034 && !DECL_ARTIFICIAL (*tp)
6035 && *tp != DECL_EXPR_DECL (udr_data->stmts[udr_data->combiner_p ? 0 : 3])
6036 && *tp != DECL_EXPR_DECL (udr_data->stmts[udr_data->combiner_p ? 1 : 4]))
6038 location_t loc = udr_data->loc;
6039 if (udr_data->combiner_p)
6040 error_at (loc, "%<#pragma omp declare reduction%> combiner refers to "
6041 "variable %qD which is not %<omp_out%> nor %<omp_in%>",
6042 *tp);
6043 else
6044 error_at (loc, "%<#pragma omp declare reduction%> initializer refers "
6045 "to variable %qD which is not %<omp_priv%> nor "
6046 "%<omp_orig%>",
6047 *tp);
6048 return *tp;
6050 return NULL_TREE;
6053 /* Diagnose violation of OpenMP #pragma omp declare reduction restrictions. */
6055 bool
6056 cp_check_omp_declare_reduction (tree udr)
6058 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (udr)));
6059 gcc_assert (TYPE_REF_P (type));
6060 type = TREE_TYPE (type);
6061 int i;
6062 location_t loc = DECL_SOURCE_LOCATION (udr);
6064 if (type == error_mark_node)
6065 return false;
6066 if (ARITHMETIC_TYPE_P (type))
6068 static enum tree_code predef_codes[]
6069 = { PLUS_EXPR, MULT_EXPR, MINUS_EXPR, BIT_AND_EXPR, BIT_XOR_EXPR,
6070 BIT_IOR_EXPR, TRUTH_ANDIF_EXPR, TRUTH_ORIF_EXPR };
6071 for (i = 0; i < 8; i++)
6073 tree id = omp_reduction_id (predef_codes[i], NULL_TREE, NULL_TREE);
6074 const char *n1 = IDENTIFIER_POINTER (DECL_NAME (udr));
6075 const char *n2 = IDENTIFIER_POINTER (id);
6076 if (strncmp (n1, n2, IDENTIFIER_LENGTH (id)) == 0
6077 && (n1[IDENTIFIER_LENGTH (id)] == '~'
6078 || n1[IDENTIFIER_LENGTH (id)] == '\0'))
6079 break;
6082 if (i == 8
6083 && TREE_CODE (type) != COMPLEX_EXPR)
6085 const char prefix_minmax[] = "omp declare reduction m";
6086 size_t prefix_size = sizeof (prefix_minmax) - 1;
6087 const char *n = IDENTIFIER_POINTER (DECL_NAME (udr));
6088 if (strncmp (IDENTIFIER_POINTER (DECL_NAME (udr)),
6089 prefix_minmax, prefix_size) == 0
6090 && ((n[prefix_size] == 'i' && n[prefix_size + 1] == 'n')
6091 || (n[prefix_size] == 'a' && n[prefix_size + 1] == 'x'))
6092 && (n[prefix_size + 2] == '~' || n[prefix_size + 2] == '\0'))
6093 i = 0;
6095 if (i < 8)
6097 error_at (loc, "predeclared arithmetic type %qT in "
6098 "%<#pragma omp declare reduction%>", type);
6099 return false;
6102 else if (FUNC_OR_METHOD_TYPE_P (type)
6103 || TREE_CODE (type) == ARRAY_TYPE)
6105 error_at (loc, "function or array type %qT in "
6106 "%<#pragma omp declare reduction%>", type);
6107 return false;
6109 else if (TYPE_REF_P (type))
6111 error_at (loc, "reference type %qT in %<#pragma omp declare reduction%>",
6112 type);
6113 return false;
6115 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
6117 error_at (loc, "%<const%>, %<volatile%> or %<__restrict%>-qualified "
6118 "type %qT in %<#pragma omp declare reduction%>", type);
6119 return false;
6122 tree body = DECL_SAVED_TREE (udr);
6123 if (body == NULL_TREE || TREE_CODE (body) != STATEMENT_LIST)
6124 return true;
6126 tree_stmt_iterator tsi;
6127 struct cp_check_omp_declare_reduction_data data;
6128 memset (data.stmts, 0, sizeof data.stmts);
6129 for (i = 0, tsi = tsi_start (body);
6130 i < 7 && !tsi_end_p (tsi);
6131 i++, tsi_next (&tsi))
6132 data.stmts[i] = tsi_stmt (tsi);
6133 data.loc = loc;
6134 gcc_assert (tsi_end_p (tsi));
6135 if (i >= 3)
6137 gcc_assert (TREE_CODE (data.stmts[0]) == DECL_EXPR
6138 && TREE_CODE (data.stmts[1]) == DECL_EXPR);
6139 if (warning_suppressed_p (DECL_EXPR_DECL (data.stmts[0]) /* What warning? */))
6140 return true;
6141 data.combiner_p = true;
6142 if (cp_walk_tree (&data.stmts[2], cp_check_omp_declare_reduction_r,
6143 &data, NULL))
6144 suppress_warning (DECL_EXPR_DECL (data.stmts[0]) /* What warning? */);
6146 if (i >= 6)
6148 gcc_assert (TREE_CODE (data.stmts[3]) == DECL_EXPR
6149 && TREE_CODE (data.stmts[4]) == DECL_EXPR);
6150 data.combiner_p = false;
6151 if (cp_walk_tree (&data.stmts[5], cp_check_omp_declare_reduction_r,
6152 &data, NULL)
6153 || cp_walk_tree (&DECL_INITIAL (DECL_EXPR_DECL (data.stmts[3])),
6154 cp_check_omp_declare_reduction_r, &data, NULL))
6155 suppress_warning (DECL_EXPR_DECL (data.stmts[0]) /* Wat warning? */);
6156 if (i == 7)
6157 gcc_assert (TREE_CODE (data.stmts[6]) == DECL_EXPR);
6159 return true;
6162 /* Helper function of finish_omp_clauses. Clone STMT as if we were making
6163 an inline call. But, remap
6164 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
6165 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
6167 static tree
6168 clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
6169 tree decl, tree placeholder)
6171 copy_body_data id;
6172 hash_map<tree, tree> decl_map;
6174 decl_map.put (omp_decl1, placeholder);
6175 decl_map.put (omp_decl2, decl);
6176 memset (&id, 0, sizeof (id));
6177 id.src_fn = DECL_CONTEXT (omp_decl1);
6178 id.dst_fn = current_function_decl;
6179 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
6180 id.decl_map = &decl_map;
6182 id.copy_decl = copy_decl_no_change;
6183 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
6184 id.transform_new_cfg = true;
6185 id.transform_return_to_modify = false;
6186 id.eh_lp_nr = 0;
6187 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
6188 return stmt;
6191 /* Helper function of finish_omp_clauses, called via cp_walk_tree.
6192 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
6194 static tree
6195 find_omp_placeholder_r (tree *tp, int *, void *data)
6197 if (*tp == (tree) data)
6198 return *tp;
6199 return NULL_TREE;
6202 /* Helper function of finish_omp_clauses. Handle OMP_CLAUSE_REDUCTION C.
6203 Return true if there is some error and the clause should be removed. */
6205 static bool
6206 finish_omp_reduction_clause (tree c, bool *need_default_ctor, bool *need_dtor)
6208 tree t = OMP_CLAUSE_DECL (c);
6209 bool predefined = false;
6210 if (TREE_CODE (t) == TREE_LIST)
6212 gcc_assert (processing_template_decl);
6213 return false;
6215 tree type = TREE_TYPE (t);
6216 if (TREE_CODE (t) == MEM_REF)
6217 type = TREE_TYPE (type);
6218 if (TYPE_REF_P (type))
6219 type = TREE_TYPE (type);
6220 if (TREE_CODE (type) == ARRAY_TYPE)
6222 tree oatype = type;
6223 gcc_assert (TREE_CODE (t) != MEM_REF);
6224 while (TREE_CODE (type) == ARRAY_TYPE)
6225 type = TREE_TYPE (type);
6226 if (!processing_template_decl)
6228 t = require_complete_type (t);
6229 if (t == error_mark_node
6230 || !complete_type_or_else (oatype, NULL_TREE))
6231 return true;
6232 tree size = size_binop (EXACT_DIV_EXPR, TYPE_SIZE_UNIT (oatype),
6233 TYPE_SIZE_UNIT (type));
6234 if (integer_zerop (size))
6236 error_at (OMP_CLAUSE_LOCATION (c),
6237 "%qE in %<reduction%> clause is a zero size array",
6238 omp_clause_printable_decl (t));
6239 return true;
6241 size = size_binop (MINUS_EXPR, size, size_one_node);
6242 size = save_expr (size);
6243 tree index_type = build_index_type (size);
6244 tree atype = build_array_type (type, index_type);
6245 tree ptype = build_pointer_type (type);
6246 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
6247 t = build_fold_addr_expr (t);
6248 t = build2 (MEM_REF, atype, t, build_int_cst (ptype, 0));
6249 OMP_CLAUSE_DECL (c) = t;
6252 if (type == error_mark_node)
6253 return true;
6254 else if (ARITHMETIC_TYPE_P (type))
6255 switch (OMP_CLAUSE_REDUCTION_CODE (c))
6257 case PLUS_EXPR:
6258 case MULT_EXPR:
6259 case MINUS_EXPR:
6260 case TRUTH_ANDIF_EXPR:
6261 case TRUTH_ORIF_EXPR:
6262 predefined = true;
6263 break;
6264 case MIN_EXPR:
6265 case MAX_EXPR:
6266 if (TREE_CODE (type) == COMPLEX_TYPE)
6267 break;
6268 predefined = true;
6269 break;
6270 case BIT_AND_EXPR:
6271 case BIT_IOR_EXPR:
6272 case BIT_XOR_EXPR:
6273 if (FLOAT_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
6274 break;
6275 predefined = true;
6276 break;
6277 default:
6278 break;
6280 else if (TYPE_READONLY (type))
6282 error_at (OMP_CLAUSE_LOCATION (c),
6283 "%qE has const type for %<reduction%>",
6284 omp_clause_printable_decl (t));
6285 return true;
6287 else if (!processing_template_decl)
6289 t = require_complete_type (t);
6290 if (t == error_mark_node)
6291 return true;
6292 OMP_CLAUSE_DECL (c) = t;
6295 if (predefined)
6297 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = NULL_TREE;
6298 return false;
6300 else if (processing_template_decl)
6302 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == error_mark_node)
6303 return true;
6304 return false;
6307 tree id = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
6309 type = TYPE_MAIN_VARIANT (type);
6310 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = NULL_TREE;
6311 if (id == NULL_TREE)
6312 id = omp_reduction_id (OMP_CLAUSE_REDUCTION_CODE (c),
6313 NULL_TREE, NULL_TREE);
6314 id = omp_reduction_lookup (OMP_CLAUSE_LOCATION (c), id, type, NULL, NULL);
6315 if (id)
6317 if (id == error_mark_node)
6318 return true;
6319 mark_used (id);
6320 tree body = DECL_SAVED_TREE (id);
6321 if (!body)
6322 return true;
6323 if (TREE_CODE (body) == STATEMENT_LIST)
6325 tree_stmt_iterator tsi;
6326 tree placeholder = NULL_TREE, decl_placeholder = NULL_TREE;
6327 int i;
6328 tree stmts[7];
6329 tree atype = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (id)));
6330 atype = TREE_TYPE (atype);
6331 bool need_static_cast = !same_type_p (type, atype);
6332 memset (stmts, 0, sizeof stmts);
6333 for (i = 0, tsi = tsi_start (body);
6334 i < 7 && !tsi_end_p (tsi);
6335 i++, tsi_next (&tsi))
6336 stmts[i] = tsi_stmt (tsi);
6337 gcc_assert (tsi_end_p (tsi));
6339 if (i >= 3)
6341 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
6342 && TREE_CODE (stmts[1]) == DECL_EXPR);
6343 placeholder = build_lang_decl (VAR_DECL, NULL_TREE, type);
6344 DECL_ARTIFICIAL (placeholder) = 1;
6345 DECL_IGNORED_P (placeholder) = 1;
6346 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
6347 if (TREE_CODE (t) == MEM_REF)
6349 decl_placeholder = build_lang_decl (VAR_DECL, NULL_TREE,
6350 type);
6351 DECL_ARTIFICIAL (decl_placeholder) = 1;
6352 DECL_IGNORED_P (decl_placeholder) = 1;
6353 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c) = decl_placeholder;
6355 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[0])))
6356 cxx_mark_addressable (placeholder);
6357 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[1]))
6358 && (decl_placeholder
6359 || !TYPE_REF_P (TREE_TYPE (OMP_CLAUSE_DECL (c)))))
6360 cxx_mark_addressable (decl_placeholder ? decl_placeholder
6361 : OMP_CLAUSE_DECL (c));
6362 tree omp_out = placeholder;
6363 tree omp_in = decl_placeholder ? decl_placeholder
6364 : convert_from_reference (OMP_CLAUSE_DECL (c));
6365 if (need_static_cast)
6367 tree rtype = build_reference_type (atype);
6368 omp_out = build_static_cast (input_location,
6369 rtype, omp_out,
6370 tf_warning_or_error);
6371 omp_in = build_static_cast (input_location,
6372 rtype, omp_in,
6373 tf_warning_or_error);
6374 if (omp_out == error_mark_node || omp_in == error_mark_node)
6375 return true;
6376 omp_out = convert_from_reference (omp_out);
6377 omp_in = convert_from_reference (omp_in);
6379 OMP_CLAUSE_REDUCTION_MERGE (c)
6380 = clone_omp_udr (stmts[2], DECL_EXPR_DECL (stmts[0]),
6381 DECL_EXPR_DECL (stmts[1]), omp_in, omp_out);
6383 if (i >= 6)
6385 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
6386 && TREE_CODE (stmts[4]) == DECL_EXPR);
6387 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[3]))
6388 && (decl_placeholder
6389 || !TYPE_REF_P (TREE_TYPE (OMP_CLAUSE_DECL (c)))))
6390 cxx_mark_addressable (decl_placeholder ? decl_placeholder
6391 : OMP_CLAUSE_DECL (c));
6392 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[4])))
6393 cxx_mark_addressable (placeholder);
6394 tree omp_priv = decl_placeholder ? decl_placeholder
6395 : convert_from_reference (OMP_CLAUSE_DECL (c));
6396 tree omp_orig = placeholder;
6397 if (need_static_cast)
6399 if (i == 7)
6401 error_at (OMP_CLAUSE_LOCATION (c),
6402 "user defined reduction with constructor "
6403 "initializer for base class %qT", atype);
6404 return true;
6406 tree rtype = build_reference_type (atype);
6407 omp_priv = build_static_cast (input_location,
6408 rtype, omp_priv,
6409 tf_warning_or_error);
6410 omp_orig = build_static_cast (input_location,
6411 rtype, omp_orig,
6412 tf_warning_or_error);
6413 if (omp_priv == error_mark_node
6414 || omp_orig == error_mark_node)
6415 return true;
6416 omp_priv = convert_from_reference (omp_priv);
6417 omp_orig = convert_from_reference (omp_orig);
6419 if (i == 6)
6420 *need_default_ctor = true;
6421 OMP_CLAUSE_REDUCTION_INIT (c)
6422 = clone_omp_udr (stmts[5], DECL_EXPR_DECL (stmts[4]),
6423 DECL_EXPR_DECL (stmts[3]),
6424 omp_priv, omp_orig);
6425 if (cp_walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
6426 find_omp_placeholder_r, placeholder, NULL))
6427 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
6429 else if (i >= 3)
6431 if (CLASS_TYPE_P (type) && !pod_type_p (type))
6432 *need_default_ctor = true;
6433 else
6435 tree init;
6436 tree v = decl_placeholder ? decl_placeholder
6437 : convert_from_reference (t);
6438 if (AGGREGATE_TYPE_P (TREE_TYPE (v)))
6439 init = build_constructor (TREE_TYPE (v), NULL);
6440 else
6441 init = fold_convert (TREE_TYPE (v), integer_zero_node);
6442 OMP_CLAUSE_REDUCTION_INIT (c)
6443 = cp_build_init_expr (v, init);
6448 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
6449 *need_dtor = true;
6450 else
6452 error_at (OMP_CLAUSE_LOCATION (c),
6453 "user defined reduction not found for %qE",
6454 omp_clause_printable_decl (t));
6455 return true;
6457 if (TREE_CODE (OMP_CLAUSE_DECL (c)) == MEM_REF)
6458 gcc_assert (TYPE_SIZE_UNIT (type)
6459 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST);
6460 return false;
6463 /* Called from finish_struct_1. linear(this) or linear(this:step)
6464 clauses might not be finalized yet because the class has been incomplete
6465 when parsing #pragma omp declare simd methods. Fix those up now. */
6467 void
6468 finish_omp_declare_simd_methods (tree t)
6470 if (processing_template_decl)
6471 return;
6473 for (tree x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
6475 if (TREE_CODE (x) == USING_DECL
6476 || !DECL_NONSTATIC_MEMBER_FUNCTION_P (x))
6477 continue;
6478 tree ods = lookup_attribute ("omp declare simd", DECL_ATTRIBUTES (x));
6479 if (!ods || !TREE_VALUE (ods))
6480 continue;
6481 for (tree c = TREE_VALUE (TREE_VALUE (ods)); c; c = OMP_CLAUSE_CHAIN (c))
6482 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
6483 && integer_zerop (OMP_CLAUSE_DECL (c))
6484 && OMP_CLAUSE_LINEAR_STEP (c)
6485 && TYPE_PTR_P (TREE_TYPE (OMP_CLAUSE_LINEAR_STEP (c))))
6487 tree s = OMP_CLAUSE_LINEAR_STEP (c);
6488 s = fold_convert_loc (OMP_CLAUSE_LOCATION (c), sizetype, s);
6489 s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MULT_EXPR,
6490 sizetype, s, TYPE_SIZE_UNIT (t));
6491 OMP_CLAUSE_LINEAR_STEP (c) = s;
6496 /* Adjust sink depend/doacross clause to take into account pointer offsets.
6498 Return TRUE if there was a problem processing the offset, and the
6499 whole clause should be removed. */
6501 static bool
6502 cp_finish_omp_clause_doacross_sink (tree sink_clause)
6504 tree t = OMP_CLAUSE_DECL (sink_clause);
6505 gcc_assert (TREE_CODE (t) == TREE_LIST);
6507 /* Make sure we don't adjust things twice for templates. */
6508 if (processing_template_decl)
6509 return false;
6511 for (; t; t = TREE_CHAIN (t))
6513 tree decl = TREE_VALUE (t);
6514 if (TYPE_PTR_P (TREE_TYPE (decl)))
6516 tree offset = TREE_PURPOSE (t);
6517 bool neg = wi::neg_p (wi::to_wide (offset));
6518 offset = fold_unary (ABS_EXPR, TREE_TYPE (offset), offset);
6519 decl = mark_rvalue_use (decl);
6520 decl = convert_from_reference (decl);
6521 tree t2 = pointer_int_sum (OMP_CLAUSE_LOCATION (sink_clause),
6522 neg ? MINUS_EXPR : PLUS_EXPR,
6523 decl, offset);
6524 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (sink_clause),
6525 MINUS_EXPR, sizetype,
6526 fold_convert (sizetype, t2),
6527 fold_convert (sizetype, decl));
6528 if (t2 == error_mark_node)
6529 return true;
6530 TREE_PURPOSE (t) = t2;
6533 return false;
6536 /* Finish OpenMP iterators ITER. Return true if they are errorneous
6537 and clauses containing them should be removed. */
6539 static bool
6540 cp_omp_finish_iterators (tree iter)
6542 bool ret = false;
6543 for (tree it = iter; it; it = TREE_CHAIN (it))
6545 tree var = TREE_VEC_ELT (it, 0);
6546 tree begin = TREE_VEC_ELT (it, 1);
6547 tree end = TREE_VEC_ELT (it, 2);
6548 tree step = TREE_VEC_ELT (it, 3);
6549 tree orig_step;
6550 tree type = TREE_TYPE (var);
6551 location_t loc = DECL_SOURCE_LOCATION (var);
6552 if (type == error_mark_node)
6554 ret = true;
6555 continue;
6557 if (type_dependent_expression_p (var))
6558 continue;
6559 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
6561 error_at (loc, "iterator %qD has neither integral nor pointer type",
6562 var);
6563 ret = true;
6564 continue;
6566 else if (TYPE_READONLY (type))
6568 error_at (loc, "iterator %qD has const qualified type", var);
6569 ret = true;
6570 continue;
6572 if (type_dependent_expression_p (begin)
6573 || type_dependent_expression_p (end)
6574 || type_dependent_expression_p (step))
6575 continue;
6576 else if (error_operand_p (step))
6578 ret = true;
6579 continue;
6581 else if (!INTEGRAL_TYPE_P (TREE_TYPE (step)))
6583 error_at (EXPR_LOC_OR_LOC (step, loc),
6584 "iterator step with non-integral type");
6585 ret = true;
6586 continue;
6589 begin = mark_rvalue_use (begin);
6590 end = mark_rvalue_use (end);
6591 step = mark_rvalue_use (step);
6592 begin = cp_build_c_cast (input_location, type, begin,
6593 tf_warning_or_error);
6594 end = cp_build_c_cast (input_location, type, end,
6595 tf_warning_or_error);
6596 orig_step = step;
6597 if (!processing_template_decl)
6598 step = orig_step = save_expr (step);
6599 tree stype = POINTER_TYPE_P (type) ? sizetype : type;
6600 step = cp_build_c_cast (input_location, stype, step,
6601 tf_warning_or_error);
6602 if (POINTER_TYPE_P (type) && !processing_template_decl)
6604 begin = save_expr (begin);
6605 step = pointer_int_sum (loc, PLUS_EXPR, begin, step);
6606 step = fold_build2_loc (loc, MINUS_EXPR, sizetype,
6607 fold_convert (sizetype, step),
6608 fold_convert (sizetype, begin));
6609 step = fold_convert (ssizetype, step);
6611 if (!processing_template_decl)
6613 begin = maybe_constant_value (begin);
6614 end = maybe_constant_value (end);
6615 step = maybe_constant_value (step);
6616 orig_step = maybe_constant_value (orig_step);
6618 if (integer_zerop (step))
6620 error_at (loc, "iterator %qD has zero step", var);
6621 ret = true;
6622 continue;
6625 if (begin == error_mark_node
6626 || end == error_mark_node
6627 || step == error_mark_node
6628 || orig_step == error_mark_node)
6630 ret = true;
6631 continue;
6634 if (!processing_template_decl)
6636 begin = fold_build_cleanup_point_expr (TREE_TYPE (begin), begin);
6637 end = fold_build_cleanup_point_expr (TREE_TYPE (end), end);
6638 step = fold_build_cleanup_point_expr (TREE_TYPE (step), step);
6639 orig_step = fold_build_cleanup_point_expr (TREE_TYPE (orig_step),
6640 orig_step);
6642 hash_set<tree> pset;
6643 tree it2;
6644 for (it2 = TREE_CHAIN (it); it2; it2 = TREE_CHAIN (it2))
6646 tree var2 = TREE_VEC_ELT (it2, 0);
6647 tree begin2 = TREE_VEC_ELT (it2, 1);
6648 tree end2 = TREE_VEC_ELT (it2, 2);
6649 tree step2 = TREE_VEC_ELT (it2, 3);
6650 location_t loc2 = DECL_SOURCE_LOCATION (var2);
6651 if (cp_walk_tree (&begin2, find_omp_placeholder_r, var, &pset))
6653 error_at (EXPR_LOC_OR_LOC (begin2, loc2),
6654 "begin expression refers to outer iterator %qD", var);
6655 break;
6657 else if (cp_walk_tree (&end2, find_omp_placeholder_r, var, &pset))
6659 error_at (EXPR_LOC_OR_LOC (end2, loc2),
6660 "end expression refers to outer iterator %qD", var);
6661 break;
6663 else if (cp_walk_tree (&step2, find_omp_placeholder_r, var, &pset))
6665 error_at (EXPR_LOC_OR_LOC (step2, loc2),
6666 "step expression refers to outer iterator %qD", var);
6667 break;
6670 if (it2)
6672 ret = true;
6673 continue;
6675 TREE_VEC_ELT (it, 1) = begin;
6676 TREE_VEC_ELT (it, 2) = end;
6677 if (processing_template_decl)
6678 TREE_VEC_ELT (it, 3) = orig_step;
6679 else
6681 TREE_VEC_ELT (it, 3) = step;
6682 TREE_VEC_ELT (it, 4) = orig_step;
6685 return ret;
6688 /* Ensure that pointers are used in OpenACC attach and detach clauses.
6689 Return true if an error has been detected. */
6691 static bool
6692 cp_oacc_check_attachments (tree c)
6694 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
6695 return false;
6697 /* OpenACC attach / detach clauses must be pointers. */
6698 if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
6699 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH)
6701 tree t = OMP_CLAUSE_DECL (c);
6702 tree type;
6704 while (TREE_CODE (t) == TREE_LIST)
6705 t = TREE_CHAIN (t);
6707 type = TREE_TYPE (t);
6709 if (TREE_CODE (type) == REFERENCE_TYPE)
6710 type = TREE_TYPE (type);
6712 if (TREE_CODE (type) != POINTER_TYPE)
6714 error_at (OMP_CLAUSE_LOCATION (c), "expected pointer in %qs clause",
6715 user_omp_clause_code_name (c, true));
6716 return true;
6720 return false;
6723 /* For all elements of CLAUSES, validate them vs OpenMP constraints.
6724 Remove any elements from the list that are invalid. */
6726 tree
6727 finish_omp_clauses (tree clauses, enum c_omp_region_type ort)
6729 bitmap_head generic_head, firstprivate_head, lastprivate_head;
6730 bitmap_head aligned_head, map_head, map_field_head, map_firstprivate_head;
6731 bitmap_head oacc_reduction_head, is_on_device_head;
6732 tree c, t, *pc;
6733 tree safelen = NULL_TREE;
6734 bool branch_seen = false;
6735 bool copyprivate_seen = false;
6736 bool ordered_seen = false;
6737 bool order_seen = false;
6738 bool schedule_seen = false;
6739 bool oacc_async = false;
6740 bool indir_component_ref_p = false;
6741 tree last_iterators = NULL_TREE;
6742 bool last_iterators_remove = false;
6743 /* 1 if normal/task reduction has been seen, -1 if inscan reduction
6744 has been seen, -2 if mixed inscan/normal reduction diagnosed. */
6745 int reduction_seen = 0;
6746 bool allocate_seen = false;
6747 tree detach_seen = NULL_TREE;
6748 bool mergeable_seen = false;
6749 bool implicit_moved = false;
6750 bool target_in_reduction_seen = false;
6752 bitmap_obstack_initialize (NULL);
6753 bitmap_initialize (&generic_head, &bitmap_default_obstack);
6754 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
6755 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
6756 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
6757 /* If ort == C_ORT_OMP_DECLARE_SIMD used as uniform_head instead. */
6758 bitmap_initialize (&map_head, &bitmap_default_obstack);
6759 bitmap_initialize (&map_field_head, &bitmap_default_obstack);
6760 bitmap_initialize (&map_firstprivate_head, &bitmap_default_obstack);
6761 /* If ort == C_ORT_OMP used as nontemporal_head or use_device_xxx_head
6762 instead and for ort == C_ORT_OMP_TARGET used as in_reduction_head. */
6763 bitmap_initialize (&oacc_reduction_head, &bitmap_default_obstack);
6764 bitmap_initialize (&is_on_device_head, &bitmap_default_obstack);
6766 if (ort & C_ORT_ACC)
6767 for (c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
6768 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ASYNC)
6770 oacc_async = true;
6771 break;
6774 tree *grp_start_p = NULL, grp_sentinel = NULL_TREE;
6776 for (pc = &clauses, c = clauses; c ; c = *pc)
6778 bool remove = false;
6779 bool field_ok = false;
6781 /* We've reached the end of a list of expanded nodes. Reset the group
6782 start pointer. */
6783 if (c == grp_sentinel)
6784 grp_start_p = NULL;
6786 switch (OMP_CLAUSE_CODE (c))
6788 case OMP_CLAUSE_SHARED:
6789 field_ok = ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP);
6790 goto check_dup_generic;
6791 case OMP_CLAUSE_PRIVATE:
6792 field_ok = ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP);
6793 goto check_dup_generic;
6794 case OMP_CLAUSE_REDUCTION:
6795 if (reduction_seen == 0)
6796 reduction_seen = OMP_CLAUSE_REDUCTION_INSCAN (c) ? -1 : 1;
6797 else if (reduction_seen != -2
6798 && reduction_seen != (OMP_CLAUSE_REDUCTION_INSCAN (c)
6799 ? -1 : 1))
6801 error_at (OMP_CLAUSE_LOCATION (c),
6802 "%<inscan%> and non-%<inscan%> %<reduction%> clauses "
6803 "on the same construct");
6804 reduction_seen = -2;
6806 /* FALLTHRU */
6807 case OMP_CLAUSE_IN_REDUCTION:
6808 case OMP_CLAUSE_TASK_REDUCTION:
6809 field_ok = ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP);
6810 t = OMP_CLAUSE_DECL (c);
6811 if (TREE_CODE (t) == TREE_LIST)
6813 if (handle_omp_array_sections (c, ort))
6815 remove = true;
6816 break;
6818 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
6819 && OMP_CLAUSE_REDUCTION_INSCAN (c))
6821 error_at (OMP_CLAUSE_LOCATION (c),
6822 "%<inscan%> %<reduction%> clause with array "
6823 "section");
6824 remove = true;
6825 break;
6827 if (TREE_CODE (t) == TREE_LIST)
6829 while (TREE_CODE (t) == TREE_LIST)
6830 t = TREE_CHAIN (t);
6832 else
6834 gcc_assert (TREE_CODE (t) == MEM_REF);
6835 t = TREE_OPERAND (t, 0);
6836 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
6837 t = TREE_OPERAND (t, 0);
6838 if (TREE_CODE (t) == ADDR_EXPR
6839 || INDIRECT_REF_P (t))
6840 t = TREE_OPERAND (t, 0);
6842 tree n = omp_clause_decl_field (t);
6843 if (n)
6844 t = n;
6845 goto check_dup_generic_t;
6847 if (oacc_async)
6848 cxx_mark_addressable (t);
6849 goto check_dup_generic;
6850 case OMP_CLAUSE_COPYPRIVATE:
6851 copyprivate_seen = true;
6852 field_ok = ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP);
6853 goto check_dup_generic;
6854 case OMP_CLAUSE_COPYIN:
6855 goto check_dup_generic;
6856 case OMP_CLAUSE_LINEAR:
6857 field_ok = ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP);
6858 t = OMP_CLAUSE_DECL (c);
6859 if (ort != C_ORT_OMP_DECLARE_SIMD
6860 && OMP_CLAUSE_LINEAR_KIND (c) != OMP_CLAUSE_LINEAR_DEFAULT)
6862 if (OMP_CLAUSE_LINEAR_OLD_LINEAR_MODIFIER (c))
6864 error_at (OMP_CLAUSE_LOCATION (c),
6865 "modifier should not be specified in %<linear%> "
6866 "clause on %<simd%> or %<for%> constructs when "
6867 "not using OpenMP 5.2 modifiers");
6868 OMP_CLAUSE_LINEAR_KIND (c) = OMP_CLAUSE_LINEAR_DEFAULT;
6870 else if (OMP_CLAUSE_LINEAR_KIND (c) != OMP_CLAUSE_LINEAR_VAL)
6872 error_at (OMP_CLAUSE_LOCATION (c),
6873 "modifier other than %<val%> specified in "
6874 "%<linear%> clause on %<simd%> or %<for%> "
6875 "constructs when using OpenMP 5.2 modifiers");
6876 OMP_CLAUSE_LINEAR_KIND (c) = OMP_CLAUSE_LINEAR_DEFAULT;
6879 if ((VAR_P (t) || TREE_CODE (t) == PARM_DECL)
6880 && !type_dependent_expression_p (t))
6882 tree type = TREE_TYPE (t);
6883 if ((OMP_CLAUSE_LINEAR_KIND (c) == OMP_CLAUSE_LINEAR_REF
6884 || OMP_CLAUSE_LINEAR_KIND (c) == OMP_CLAUSE_LINEAR_UVAL)
6885 && !TYPE_REF_P (type))
6887 error_at (OMP_CLAUSE_LOCATION (c),
6888 "linear clause with %qs modifier applied to "
6889 "non-reference variable with %qT type",
6890 OMP_CLAUSE_LINEAR_KIND (c) == OMP_CLAUSE_LINEAR_REF
6891 ? "ref" : "uval", TREE_TYPE (t));
6892 remove = true;
6893 break;
6895 if (TYPE_REF_P (type))
6896 type = TREE_TYPE (type);
6897 if (OMP_CLAUSE_LINEAR_KIND (c) != OMP_CLAUSE_LINEAR_REF)
6899 if (!INTEGRAL_TYPE_P (type)
6900 && !TYPE_PTR_P (type))
6902 error_at (OMP_CLAUSE_LOCATION (c),
6903 "linear clause applied to non-integral "
6904 "non-pointer variable with %qT type",
6905 TREE_TYPE (t));
6906 remove = true;
6907 break;
6911 t = OMP_CLAUSE_LINEAR_STEP (c);
6912 if (t == NULL_TREE)
6913 t = integer_one_node;
6914 if (t == error_mark_node)
6916 remove = true;
6917 break;
6919 else if (!type_dependent_expression_p (t)
6920 && !INTEGRAL_TYPE_P (TREE_TYPE (t))
6921 && (ort != C_ORT_OMP_DECLARE_SIMD
6922 || TREE_CODE (t) != PARM_DECL
6923 || !TYPE_REF_P (TREE_TYPE (t))
6924 || !INTEGRAL_TYPE_P (TREE_TYPE (TREE_TYPE (t)))))
6926 error_at (OMP_CLAUSE_LOCATION (c),
6927 "linear step expression must be integral");
6928 remove = true;
6929 break;
6931 else
6933 t = mark_rvalue_use (t);
6934 if (ort == C_ORT_OMP_DECLARE_SIMD && TREE_CODE (t) == PARM_DECL)
6936 OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c) = 1;
6937 goto check_dup_generic;
6939 if (!processing_template_decl
6940 && (VAR_P (OMP_CLAUSE_DECL (c))
6941 || TREE_CODE (OMP_CLAUSE_DECL (c)) == PARM_DECL))
6943 if (ort == C_ORT_OMP_DECLARE_SIMD)
6945 t = maybe_constant_value (t);
6946 if (TREE_CODE (t) != INTEGER_CST)
6948 error_at (OMP_CLAUSE_LOCATION (c),
6949 "%<linear%> clause step %qE is neither "
6950 "constant nor a parameter", t);
6951 remove = true;
6952 break;
6955 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6956 tree type = TREE_TYPE (OMP_CLAUSE_DECL (c));
6957 if (TYPE_REF_P (type))
6958 type = TREE_TYPE (type);
6959 if (OMP_CLAUSE_LINEAR_KIND (c) == OMP_CLAUSE_LINEAR_REF)
6961 type = build_pointer_type (type);
6962 tree d = fold_convert (type, OMP_CLAUSE_DECL (c));
6963 t = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
6964 d, t);
6965 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c),
6966 MINUS_EXPR, sizetype,
6967 fold_convert (sizetype, t),
6968 fold_convert (sizetype, d));
6969 if (t == error_mark_node)
6971 remove = true;
6972 break;
6975 else if (TYPE_PTR_P (type)
6976 /* Can't multiply the step yet if *this
6977 is still incomplete type. */
6978 && (ort != C_ORT_OMP_DECLARE_SIMD
6979 || TREE_CODE (OMP_CLAUSE_DECL (c)) != PARM_DECL
6980 || !DECL_ARTIFICIAL (OMP_CLAUSE_DECL (c))
6981 || DECL_NAME (OMP_CLAUSE_DECL (c))
6982 != this_identifier
6983 || !TYPE_BEING_DEFINED (TREE_TYPE (type))))
6985 tree d = convert_from_reference (OMP_CLAUSE_DECL (c));
6986 t = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
6987 d, t);
6988 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c),
6989 MINUS_EXPR, sizetype,
6990 fold_convert (sizetype, t),
6991 fold_convert (sizetype, d));
6992 if (t == error_mark_node)
6994 remove = true;
6995 break;
6998 else
6999 t = fold_convert (type, t);
7001 OMP_CLAUSE_LINEAR_STEP (c) = t;
7003 goto check_dup_generic;
7004 check_dup_generic:
7005 t = omp_clause_decl_field (OMP_CLAUSE_DECL (c));
7006 if (t)
7008 if (!remove && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_SHARED)
7009 omp_note_field_privatization (t, OMP_CLAUSE_DECL (c));
7011 else
7012 t = OMP_CLAUSE_DECL (c);
7013 check_dup_generic_t:
7014 if (t == current_class_ptr
7015 && ((ort != C_ORT_OMP_DECLARE_SIMD && ort != C_ORT_ACC)
7016 || (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_LINEAR
7017 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_UNIFORM)))
7019 error_at (OMP_CLAUSE_LOCATION (c),
7020 "%<this%> allowed in OpenMP only in %<declare simd%>"
7021 " clauses");
7022 remove = true;
7023 break;
7025 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL
7026 && (!field_ok || TREE_CODE (t) != FIELD_DECL))
7028 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
7029 break;
7030 if (DECL_P (t))
7031 error_at (OMP_CLAUSE_LOCATION (c),
7032 "%qD is not a variable in clause %qs", t,
7033 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7034 else
7035 error_at (OMP_CLAUSE_LOCATION (c),
7036 "%qE is not a variable in clause %qs", t,
7037 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7038 remove = true;
7040 else if ((ort == C_ORT_ACC
7041 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
7042 || (ort == C_ORT_OMP
7043 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_USE_DEVICE_PTR
7044 || (OMP_CLAUSE_CODE (c)
7045 == OMP_CLAUSE_USE_DEVICE_ADDR)))
7046 || (ort == C_ORT_OMP_TARGET
7047 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION))
7049 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
7050 && (bitmap_bit_p (&generic_head, DECL_UID (t))
7051 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))))
7053 error_at (OMP_CLAUSE_LOCATION (c),
7054 "%qD appears more than once in data-sharing "
7055 "clauses", t);
7056 remove = true;
7057 break;
7059 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION)
7060 target_in_reduction_seen = true;
7061 if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
7063 error_at (OMP_CLAUSE_LOCATION (c),
7064 ort == C_ORT_ACC
7065 ? "%qD appears more than once in reduction clauses"
7066 : "%qD appears more than once in data clauses",
7068 remove = true;
7070 else
7071 bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
7073 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
7074 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
7075 || bitmap_bit_p (&lastprivate_head, DECL_UID (t))
7076 || bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
7078 error_at (OMP_CLAUSE_LOCATION (c),
7079 "%qD appears more than once in data clauses", t);
7080 remove = true;
7082 else if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
7083 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_HAS_DEVICE_ADDR
7084 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IS_DEVICE_PTR)
7085 && bitmap_bit_p (&map_head, DECL_UID (t)))
7087 if (ort == C_ORT_ACC)
7088 error_at (OMP_CLAUSE_LOCATION (c),
7089 "%qD appears more than once in data clauses", t);
7090 else
7091 error_at (OMP_CLAUSE_LOCATION (c),
7092 "%qD appears both in data and map clauses", t);
7093 remove = true;
7095 else
7096 bitmap_set_bit (&generic_head, DECL_UID (t));
7097 if (!field_ok)
7098 break;
7099 handle_field_decl:
7100 if (!remove
7101 && TREE_CODE (t) == FIELD_DECL
7102 && t == OMP_CLAUSE_DECL (c))
7104 OMP_CLAUSE_DECL (c)
7105 = omp_privatize_field (t, (OMP_CLAUSE_CODE (c)
7106 == OMP_CLAUSE_SHARED));
7107 if (OMP_CLAUSE_DECL (c) == error_mark_node)
7108 remove = true;
7110 break;
7112 case OMP_CLAUSE_FIRSTPRIVATE:
7113 if (OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c) && !implicit_moved)
7115 move_implicit:
7116 implicit_moved = true;
7117 /* Move firstprivate and map clauses with
7118 OMP_CLAUSE_{FIRSTPRIVATE,MAP}_IMPLICIT set to the end of
7119 clauses chain. */
7120 tree cl1 = NULL_TREE, cl2 = NULL_TREE;
7121 tree *pc1 = pc, *pc2 = &cl1, *pc3 = &cl2;
7122 while (*pc1)
7123 if (OMP_CLAUSE_CODE (*pc1) == OMP_CLAUSE_FIRSTPRIVATE
7124 && OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (*pc1))
7126 *pc3 = *pc1;
7127 pc3 = &OMP_CLAUSE_CHAIN (*pc3);
7128 *pc1 = OMP_CLAUSE_CHAIN (*pc1);
7130 else if (OMP_CLAUSE_CODE (*pc1) == OMP_CLAUSE_MAP
7131 && OMP_CLAUSE_MAP_IMPLICIT (*pc1))
7133 *pc2 = *pc1;
7134 pc2 = &OMP_CLAUSE_CHAIN (*pc2);
7135 *pc1 = OMP_CLAUSE_CHAIN (*pc1);
7137 else
7138 pc1 = &OMP_CLAUSE_CHAIN (*pc1);
7139 *pc3 = NULL;
7140 *pc2 = cl2;
7141 *pc1 = cl1;
7142 continue;
7144 t = omp_clause_decl_field (OMP_CLAUSE_DECL (c));
7145 if (t)
7146 omp_note_field_privatization (t, OMP_CLAUSE_DECL (c));
7147 else
7148 t = OMP_CLAUSE_DECL (c);
7149 if (ort != C_ORT_ACC && t == current_class_ptr)
7151 error_at (OMP_CLAUSE_LOCATION (c),
7152 "%<this%> allowed in OpenMP only in %<declare simd%>"
7153 " clauses");
7154 remove = true;
7155 break;
7157 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL
7158 && ((ort & C_ORT_OMP_DECLARE_SIMD) != C_ORT_OMP
7159 || TREE_CODE (t) != FIELD_DECL))
7161 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
7162 break;
7163 if (DECL_P (t))
7164 error_at (OMP_CLAUSE_LOCATION (c),
7165 "%qD is not a variable in clause %<firstprivate%>",
7167 else
7168 error_at (OMP_CLAUSE_LOCATION (c),
7169 "%qE is not a variable in clause %<firstprivate%>",
7171 remove = true;
7173 else if (OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c)
7174 && !OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT_TARGET (c)
7175 && bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
7176 remove = true;
7177 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
7178 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
7179 || bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
7181 error_at (OMP_CLAUSE_LOCATION (c),
7182 "%qD appears more than once in data clauses", t);
7183 remove = true;
7185 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
7187 if (ort == C_ORT_ACC)
7188 error_at (OMP_CLAUSE_LOCATION (c),
7189 "%qD appears more than once in data clauses", t);
7190 else if (OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c)
7191 && !OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT_TARGET (c))
7192 /* Silently drop the clause. */;
7193 else
7194 error_at (OMP_CLAUSE_LOCATION (c),
7195 "%qD appears both in data and map clauses", t);
7196 remove = true;
7198 else
7199 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
7200 goto handle_field_decl;
7202 case OMP_CLAUSE_LASTPRIVATE:
7203 t = omp_clause_decl_field (OMP_CLAUSE_DECL (c));
7204 if (t)
7205 omp_note_field_privatization (t, OMP_CLAUSE_DECL (c));
7206 else
7207 t = OMP_CLAUSE_DECL (c);
7208 if (ort != C_ORT_ACC && t == current_class_ptr)
7210 error_at (OMP_CLAUSE_LOCATION (c),
7211 "%<this%> allowed in OpenMP only in %<declare simd%>"
7212 " clauses");
7213 remove = true;
7214 break;
7216 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL
7217 && ((ort & C_ORT_OMP_DECLARE_SIMD) != C_ORT_OMP
7218 || TREE_CODE (t) != FIELD_DECL))
7220 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
7221 break;
7222 if (DECL_P (t))
7223 error_at (OMP_CLAUSE_LOCATION (c),
7224 "%qD is not a variable in clause %<lastprivate%>",
7226 else
7227 error_at (OMP_CLAUSE_LOCATION (c),
7228 "%qE is not a variable in clause %<lastprivate%>",
7230 remove = true;
7232 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
7233 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
7235 error_at (OMP_CLAUSE_LOCATION (c),
7236 "%qD appears more than once in data clauses", t);
7237 remove = true;
7239 else
7240 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
7241 goto handle_field_decl;
7243 case OMP_CLAUSE_IF:
7244 t = OMP_CLAUSE_IF_EXPR (c);
7245 t = maybe_convert_cond (t);
7246 if (t == error_mark_node)
7247 remove = true;
7248 else if (!processing_template_decl)
7249 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7250 OMP_CLAUSE_IF_EXPR (c) = t;
7251 break;
7253 case OMP_CLAUSE_FINAL:
7254 t = OMP_CLAUSE_FINAL_EXPR (c);
7255 t = maybe_convert_cond (t);
7256 if (t == error_mark_node)
7257 remove = true;
7258 else if (!processing_template_decl)
7259 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7260 OMP_CLAUSE_FINAL_EXPR (c) = t;
7261 break;
7263 case OMP_CLAUSE_GANG:
7264 /* Operand 1 is the gang static: argument. */
7265 t = OMP_CLAUSE_OPERAND (c, 1);
7266 if (t != NULL_TREE)
7268 if (t == error_mark_node)
7269 remove = true;
7270 else if (!type_dependent_expression_p (t)
7271 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
7273 error_at (OMP_CLAUSE_LOCATION (c),
7274 "%<gang%> static expression must be integral");
7275 remove = true;
7277 else
7279 t = mark_rvalue_use (t);
7280 if (!processing_template_decl)
7282 t = maybe_constant_value (t);
7283 if (TREE_CODE (t) == INTEGER_CST
7284 && tree_int_cst_sgn (t) != 1
7285 && t != integer_minus_one_node)
7287 warning_at (OMP_CLAUSE_LOCATION (c), 0,
7288 "%<gang%> static value must be "
7289 "positive");
7290 t = integer_one_node;
7292 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7295 OMP_CLAUSE_OPERAND (c, 1) = t;
7297 /* Check operand 0, the num argument. */
7298 /* FALLTHRU */
7300 case OMP_CLAUSE_WORKER:
7301 case OMP_CLAUSE_VECTOR:
7302 if (OMP_CLAUSE_OPERAND (c, 0) == NULL_TREE)
7303 break;
7304 /* FALLTHRU */
7306 case OMP_CLAUSE_NUM_TASKS:
7307 case OMP_CLAUSE_NUM_TEAMS:
7308 case OMP_CLAUSE_NUM_THREADS:
7309 case OMP_CLAUSE_NUM_GANGS:
7310 case OMP_CLAUSE_NUM_WORKERS:
7311 case OMP_CLAUSE_VECTOR_LENGTH:
7312 t = OMP_CLAUSE_OPERAND (c, 0);
7313 if (t == error_mark_node)
7314 remove = true;
7315 else if (!type_dependent_expression_p (t)
7316 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
7318 switch (OMP_CLAUSE_CODE (c))
7320 case OMP_CLAUSE_GANG:
7321 error_at (OMP_CLAUSE_LOCATION (c),
7322 "%<gang%> num expression must be integral"); break;
7323 case OMP_CLAUSE_VECTOR:
7324 error_at (OMP_CLAUSE_LOCATION (c),
7325 "%<vector%> length expression must be integral");
7326 break;
7327 case OMP_CLAUSE_WORKER:
7328 error_at (OMP_CLAUSE_LOCATION (c),
7329 "%<worker%> num expression must be integral");
7330 break;
7331 default:
7332 error_at (OMP_CLAUSE_LOCATION (c),
7333 "%qs expression must be integral",
7334 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7336 remove = true;
7338 else
7340 t = mark_rvalue_use (t);
7341 if (!processing_template_decl)
7343 t = maybe_constant_value (t);
7344 if (TREE_CODE (t) == INTEGER_CST
7345 && tree_int_cst_sgn (t) != 1)
7347 switch (OMP_CLAUSE_CODE (c))
7349 case OMP_CLAUSE_GANG:
7350 warning_at (OMP_CLAUSE_LOCATION (c), 0,
7351 "%<gang%> num value must be positive");
7352 break;
7353 case OMP_CLAUSE_VECTOR:
7354 warning_at (OMP_CLAUSE_LOCATION (c), 0,
7355 "%<vector%> length value must be "
7356 "positive");
7357 break;
7358 case OMP_CLAUSE_WORKER:
7359 warning_at (OMP_CLAUSE_LOCATION (c), 0,
7360 "%<worker%> num value must be "
7361 "positive");
7362 break;
7363 default:
7364 warning_at (OMP_CLAUSE_LOCATION (c), 0,
7365 "%qs value must be positive",
7366 omp_clause_code_name
7367 [OMP_CLAUSE_CODE (c)]);
7369 t = integer_one_node;
7371 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7373 OMP_CLAUSE_OPERAND (c, 0) = t;
7375 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
7376 && OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (c)
7377 && !remove)
7379 t = OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (c);
7380 if (t == error_mark_node)
7381 remove = true;
7382 else if (!type_dependent_expression_p (t)
7383 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
7385 error_at (OMP_CLAUSE_LOCATION (c),
7386 "%qs expression must be integral",
7387 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7388 remove = true;
7390 else
7392 t = mark_rvalue_use (t);
7393 if (!processing_template_decl)
7395 t = maybe_constant_value (t);
7396 if (TREE_CODE (t) == INTEGER_CST
7397 && tree_int_cst_sgn (t) != 1)
7399 warning_at (OMP_CLAUSE_LOCATION (c), 0,
7400 "%qs value must be positive",
7401 omp_clause_code_name
7402 [OMP_CLAUSE_CODE (c)]);
7403 t = NULL_TREE;
7405 else
7406 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7407 tree upper = OMP_CLAUSE_NUM_TEAMS_UPPER_EXPR (c);
7408 if (t
7409 && TREE_CODE (t) == INTEGER_CST
7410 && TREE_CODE (upper) == INTEGER_CST
7411 && tree_int_cst_lt (upper, t))
7413 warning_at (OMP_CLAUSE_LOCATION (c), 0,
7414 "%<num_teams%> lower bound %qE bigger "
7415 "than upper bound %qE", t, upper);
7416 t = NULL_TREE;
7419 OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (c) = t;
7422 break;
7424 case OMP_CLAUSE_SCHEDULE:
7425 t = OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c);
7426 if (t == NULL)
7428 else if (t == error_mark_node)
7429 remove = true;
7430 else if (!type_dependent_expression_p (t)
7431 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
7433 error_at (OMP_CLAUSE_LOCATION (c),
7434 "schedule chunk size expression must be integral");
7435 remove = true;
7437 else
7439 t = mark_rvalue_use (t);
7440 if (!processing_template_decl)
7442 t = maybe_constant_value (t);
7443 if (TREE_CODE (t) == INTEGER_CST
7444 && tree_int_cst_sgn (t) != 1)
7446 warning_at (OMP_CLAUSE_LOCATION (c), 0,
7447 "chunk size value must be positive");
7448 t = integer_one_node;
7450 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7452 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
7454 if (!remove)
7455 schedule_seen = true;
7456 break;
7458 case OMP_CLAUSE_SIMDLEN:
7459 case OMP_CLAUSE_SAFELEN:
7460 t = OMP_CLAUSE_OPERAND (c, 0);
7461 if (t == error_mark_node)
7462 remove = true;
7463 else if (!type_dependent_expression_p (t)
7464 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
7466 error_at (OMP_CLAUSE_LOCATION (c),
7467 "%qs length expression must be integral",
7468 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7469 remove = true;
7471 else
7473 t = mark_rvalue_use (t);
7474 if (!processing_template_decl)
7476 t = maybe_constant_value (t);
7477 if (TREE_CODE (t) != INTEGER_CST
7478 || tree_int_cst_sgn (t) != 1)
7480 error_at (OMP_CLAUSE_LOCATION (c),
7481 "%qs length expression must be positive "
7482 "constant integer expression",
7483 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7484 remove = true;
7487 OMP_CLAUSE_OPERAND (c, 0) = t;
7488 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SAFELEN)
7489 safelen = c;
7491 break;
7493 case OMP_CLAUSE_ASYNC:
7494 t = OMP_CLAUSE_ASYNC_EXPR (c);
7495 if (t == error_mark_node)
7496 remove = true;
7497 else if (!type_dependent_expression_p (t)
7498 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
7500 error_at (OMP_CLAUSE_LOCATION (c),
7501 "%<async%> expression must be integral");
7502 remove = true;
7504 else
7506 t = mark_rvalue_use (t);
7507 if (!processing_template_decl)
7508 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7509 OMP_CLAUSE_ASYNC_EXPR (c) = t;
7511 break;
7513 case OMP_CLAUSE_WAIT:
7514 t = OMP_CLAUSE_WAIT_EXPR (c);
7515 if (t == error_mark_node)
7516 remove = true;
7517 else if (!processing_template_decl)
7518 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7519 OMP_CLAUSE_WAIT_EXPR (c) = t;
7520 break;
7522 case OMP_CLAUSE_THREAD_LIMIT:
7523 t = OMP_CLAUSE_THREAD_LIMIT_EXPR (c);
7524 if (t == error_mark_node)
7525 remove = true;
7526 else if (!type_dependent_expression_p (t)
7527 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
7529 error_at (OMP_CLAUSE_LOCATION (c),
7530 "%<thread_limit%> expression must be integral");
7531 remove = true;
7533 else
7535 t = mark_rvalue_use (t);
7536 if (!processing_template_decl)
7538 t = maybe_constant_value (t);
7539 if (TREE_CODE (t) == INTEGER_CST
7540 && tree_int_cst_sgn (t) != 1)
7542 warning_at (OMP_CLAUSE_LOCATION (c), 0,
7543 "%<thread_limit%> value must be positive");
7544 t = integer_one_node;
7546 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7548 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
7550 break;
7552 case OMP_CLAUSE_DEVICE:
7553 t = OMP_CLAUSE_DEVICE_ID (c);
7554 if (t == error_mark_node)
7555 remove = true;
7556 else if (!type_dependent_expression_p (t)
7557 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
7559 error_at (OMP_CLAUSE_LOCATION (c),
7560 "%<device%> id must be integral");
7561 remove = true;
7563 else if (OMP_CLAUSE_DEVICE_ANCESTOR (c)
7564 && TREE_CODE (t) == INTEGER_CST
7565 && !integer_onep (t))
7567 error_at (OMP_CLAUSE_LOCATION (c),
7568 "the %<device%> clause expression must evaluate to "
7569 "%<1%>");
7570 remove = true;
7572 else
7574 t = mark_rvalue_use (t);
7575 if (!processing_template_decl)
7576 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7577 OMP_CLAUSE_DEVICE_ID (c) = t;
7579 break;
7581 case OMP_CLAUSE_DIST_SCHEDULE:
7582 t = OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c);
7583 if (t == NULL)
7585 else if (t == error_mark_node)
7586 remove = true;
7587 else if (!type_dependent_expression_p (t)
7588 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
7590 error_at (OMP_CLAUSE_LOCATION (c),
7591 "%<dist_schedule%> chunk size expression must be "
7592 "integral");
7593 remove = true;
7595 else
7597 t = mark_rvalue_use (t);
7598 if (!processing_template_decl)
7599 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7600 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
7602 break;
7604 case OMP_CLAUSE_ALIGNED:
7605 t = OMP_CLAUSE_DECL (c);
7606 if (t == current_class_ptr && ort != C_ORT_OMP_DECLARE_SIMD)
7608 error_at (OMP_CLAUSE_LOCATION (c),
7609 "%<this%> allowed in OpenMP only in %<declare simd%>"
7610 " clauses");
7611 remove = true;
7612 break;
7614 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
7616 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
7617 break;
7618 if (DECL_P (t))
7619 error_at (OMP_CLAUSE_LOCATION (c),
7620 "%qD is not a variable in %<aligned%> clause", t);
7621 else
7622 error_at (OMP_CLAUSE_LOCATION (c),
7623 "%qE is not a variable in %<aligned%> clause", t);
7624 remove = true;
7626 else if (!type_dependent_expression_p (t)
7627 && !TYPE_PTR_P (TREE_TYPE (t))
7628 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE
7629 && (!TYPE_REF_P (TREE_TYPE (t))
7630 || (!INDIRECT_TYPE_P (TREE_TYPE (TREE_TYPE (t)))
7631 && (TREE_CODE (TREE_TYPE (TREE_TYPE (t)))
7632 != ARRAY_TYPE))))
7634 error_at (OMP_CLAUSE_LOCATION (c),
7635 "%qE in %<aligned%> clause is neither a pointer nor "
7636 "an array nor a reference to pointer or array", t);
7637 remove = true;
7639 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
7641 error_at (OMP_CLAUSE_LOCATION (c),
7642 "%qD appears more than once in %<aligned%> clauses",
7644 remove = true;
7646 else
7647 bitmap_set_bit (&aligned_head, DECL_UID (t));
7648 t = OMP_CLAUSE_ALIGNED_ALIGNMENT (c);
7649 if (t == error_mark_node)
7650 remove = true;
7651 else if (t == NULL_TREE)
7652 break;
7653 else if (!type_dependent_expression_p (t)
7654 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
7656 error_at (OMP_CLAUSE_LOCATION (c),
7657 "%<aligned%> clause alignment expression must "
7658 "be integral");
7659 remove = true;
7661 else
7663 t = mark_rvalue_use (t);
7664 if (!processing_template_decl)
7666 t = maybe_constant_value (t);
7667 if (TREE_CODE (t) != INTEGER_CST
7668 || tree_int_cst_sgn (t) != 1)
7670 error_at (OMP_CLAUSE_LOCATION (c),
7671 "%<aligned%> clause alignment expression must "
7672 "be positive constant integer expression");
7673 remove = true;
7675 else
7676 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7678 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = t;
7680 break;
7682 case OMP_CLAUSE_NONTEMPORAL:
7683 t = OMP_CLAUSE_DECL (c);
7684 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
7686 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
7687 break;
7688 if (DECL_P (t))
7689 error_at (OMP_CLAUSE_LOCATION (c),
7690 "%qD is not a variable in %<nontemporal%> clause",
7692 else
7693 error_at (OMP_CLAUSE_LOCATION (c),
7694 "%qE is not a variable in %<nontemporal%> clause",
7696 remove = true;
7698 else if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
7700 error_at (OMP_CLAUSE_LOCATION (c),
7701 "%qD appears more than once in %<nontemporal%> "
7702 "clauses", t);
7703 remove = true;
7705 else
7706 bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
7707 break;
7709 case OMP_CLAUSE_ALLOCATE:
7710 t = omp_clause_decl_field (OMP_CLAUSE_DECL (c));
7711 if (t)
7712 omp_note_field_privatization (t, OMP_CLAUSE_DECL (c));
7713 else
7714 t = OMP_CLAUSE_DECL (c);
7715 if (t == current_class_ptr)
7717 error_at (OMP_CLAUSE_LOCATION (c),
7718 "%<this%> not allowed in %<allocate%> clause");
7719 remove = true;
7720 break;
7722 if (!VAR_P (t)
7723 && TREE_CODE (t) != PARM_DECL
7724 && TREE_CODE (t) != FIELD_DECL)
7726 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
7727 break;
7728 if (DECL_P (t))
7729 error_at (OMP_CLAUSE_LOCATION (c),
7730 "%qD is not a variable in %<allocate%> clause", t);
7731 else
7732 error_at (OMP_CLAUSE_LOCATION (c),
7733 "%qE is not a variable in %<allocate%> clause", t);
7734 remove = true;
7736 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
7738 warning_at (OMP_CLAUSE_LOCATION (c), 0,
7739 "%qD appears more than once in %<allocate%> clauses",
7741 remove = true;
7743 else
7745 bitmap_set_bit (&aligned_head, DECL_UID (t));
7746 allocate_seen = true;
7748 tree allocator, align;
7749 align = OMP_CLAUSE_ALLOCATE_ALIGN (c);
7750 if (error_operand_p (align))
7752 remove = true;
7753 break;
7755 if (align)
7757 if (!type_dependent_expression_p (align)
7758 && !INTEGRAL_TYPE_P (TREE_TYPE (align)))
7760 error_at (OMP_CLAUSE_LOCATION (c),
7761 "%<allocate%> clause %<align%> modifier "
7762 "argument needs to be positive constant "
7763 "power of two integer expression");
7764 remove = true;
7766 else
7768 align = mark_rvalue_use (align);
7769 if (!processing_template_decl)
7771 align = maybe_constant_value (align);
7772 if (TREE_CODE (align) != INTEGER_CST
7773 || !tree_fits_uhwi_p (align)
7774 || !integer_pow2p (align))
7776 error_at (OMP_CLAUSE_LOCATION (c),
7777 "%<allocate%> clause %<align%> modifier "
7778 "argument needs to be positive constant "
7779 "power of two integer expression");
7780 remove = true;
7784 OMP_CLAUSE_ALLOCATE_ALIGN (c) = align;
7786 allocator = OMP_CLAUSE_ALLOCATE_ALLOCATOR (c);
7787 if (error_operand_p (allocator))
7789 remove = true;
7790 break;
7792 if (allocator == NULL_TREE)
7793 goto handle_field_decl;
7794 tree allocatort;
7795 allocatort = TYPE_MAIN_VARIANT (TREE_TYPE (allocator));
7796 if (!type_dependent_expression_p (allocator)
7797 && (TREE_CODE (allocatort) != ENUMERAL_TYPE
7798 || TYPE_NAME (allocatort) == NULL_TREE
7799 || TREE_CODE (TYPE_NAME (allocatort)) != TYPE_DECL
7800 || (DECL_NAME (TYPE_NAME (allocatort))
7801 != get_identifier ("omp_allocator_handle_t"))
7802 || (TYPE_CONTEXT (allocatort)
7803 != DECL_CONTEXT (global_namespace))))
7805 error_at (OMP_CLAUSE_LOCATION (c),
7806 "%<allocate%> clause allocator expression has "
7807 "type %qT rather than %<omp_allocator_handle_t%>",
7808 TREE_TYPE (allocator));
7809 remove = true;
7810 break;
7812 else
7814 allocator = mark_rvalue_use (allocator);
7815 if (!processing_template_decl)
7816 allocator = maybe_constant_value (allocator);
7817 OMP_CLAUSE_ALLOCATE_ALLOCATOR (c) = allocator;
7819 goto handle_field_decl;
7821 case OMP_CLAUSE_DOACROSS:
7822 t = OMP_CLAUSE_DECL (c);
7823 if (t == NULL_TREE)
7824 break;
7825 if (OMP_CLAUSE_DOACROSS_KIND (c) == OMP_CLAUSE_DOACROSS_SINK)
7827 if (cp_finish_omp_clause_doacross_sink (c))
7828 remove = true;
7829 break;
7831 gcc_unreachable ();
7832 case OMP_CLAUSE_DEPEND:
7833 case OMP_CLAUSE_AFFINITY:
7834 t = OMP_CLAUSE_DECL (c);
7835 if (TREE_CODE (t) == TREE_LIST
7836 && TREE_PURPOSE (t)
7837 && TREE_CODE (TREE_PURPOSE (t)) == TREE_VEC)
7839 if (TREE_PURPOSE (t) != last_iterators)
7840 last_iterators_remove
7841 = cp_omp_finish_iterators (TREE_PURPOSE (t));
7842 last_iterators = TREE_PURPOSE (t);
7843 t = TREE_VALUE (t);
7844 if (last_iterators_remove)
7845 t = error_mark_node;
7847 else
7848 last_iterators = NULL_TREE;
7850 if (TREE_CODE (t) == TREE_LIST)
7852 if (handle_omp_array_sections (c, ort))
7853 remove = true;
7854 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
7855 && (OMP_CLAUSE_DEPEND_KIND (c)
7856 == OMP_CLAUSE_DEPEND_DEPOBJ))
7858 error_at (OMP_CLAUSE_LOCATION (c),
7859 "%<depend%> clause with %<depobj%> dependence "
7860 "type on array section");
7861 remove = true;
7863 break;
7865 if (t == error_mark_node)
7866 remove = true;
7867 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
7868 && t == ridpointers[RID_OMP_ALL_MEMORY])
7870 if (OMP_CLAUSE_DEPEND_KIND (c) != OMP_CLAUSE_DEPEND_OUT
7871 && OMP_CLAUSE_DEPEND_KIND (c) != OMP_CLAUSE_DEPEND_INOUT)
7873 error_at (OMP_CLAUSE_LOCATION (c),
7874 "%<omp_all_memory%> used with %<depend%> kind "
7875 "other than %<out%> or %<inout%>");
7876 remove = true;
7878 if (processing_template_decl)
7879 break;
7881 else if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
7882 break;
7883 else if (!lvalue_p (t))
7885 if (DECL_P (t))
7886 error_at (OMP_CLAUSE_LOCATION (c),
7887 "%qD is not lvalue expression nor array section "
7888 "in %qs clause", t,
7889 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7890 else
7891 error_at (OMP_CLAUSE_LOCATION (c),
7892 "%qE is not lvalue expression nor array section "
7893 "in %qs clause", t,
7894 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7895 remove = true;
7897 else if (TREE_CODE (t) == COMPONENT_REF
7898 && TREE_CODE (TREE_OPERAND (t, 1)) == FIELD_DECL
7899 && DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
7901 error_at (OMP_CLAUSE_LOCATION (c),
7902 "bit-field %qE in %qs clause", t,
7903 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7904 remove = true;
7906 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
7907 && OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_DEPOBJ)
7909 if (!c_omp_depend_t_p (TYPE_REF_P (TREE_TYPE (t))
7910 ? TREE_TYPE (TREE_TYPE (t))
7911 : TREE_TYPE (t)))
7913 error_at (OMP_CLAUSE_LOCATION (c),
7914 "%qE does not have %<omp_depend_t%> type in "
7915 "%<depend%> clause with %<depobj%> dependence "
7916 "type", t);
7917 remove = true;
7920 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
7921 && c_omp_depend_t_p (TYPE_REF_P (TREE_TYPE (t))
7922 ? TREE_TYPE (TREE_TYPE (t))
7923 : TREE_TYPE (t)))
7925 error_at (OMP_CLAUSE_LOCATION (c),
7926 "%qE should not have %<omp_depend_t%> type in "
7927 "%<depend%> clause with dependence type other than "
7928 "%<depobj%>", t);
7929 remove = true;
7931 if (!remove)
7933 if (t == ridpointers[RID_OMP_ALL_MEMORY])
7934 t = null_pointer_node;
7935 else
7937 tree addr = cp_build_addr_expr (t, tf_warning_or_error);
7938 if (addr == error_mark_node)
7940 remove = true;
7941 break;
7943 t = cp_build_indirect_ref (OMP_CLAUSE_LOCATION (c),
7944 addr, RO_UNARY_STAR,
7945 tf_warning_or_error);
7946 if (t == error_mark_node)
7948 remove = true;
7949 break;
7952 if (TREE_CODE (OMP_CLAUSE_DECL (c)) == TREE_LIST
7953 && TREE_PURPOSE (OMP_CLAUSE_DECL (c))
7954 && (TREE_CODE (TREE_PURPOSE (OMP_CLAUSE_DECL (c)))
7955 == TREE_VEC))
7956 TREE_VALUE (OMP_CLAUSE_DECL (c)) = t;
7957 else
7958 OMP_CLAUSE_DECL (c) = t;
7960 break;
7961 case OMP_CLAUSE_DETACH:
7962 t = OMP_CLAUSE_DECL (c);
7963 if (detach_seen)
7965 error_at (OMP_CLAUSE_LOCATION (c),
7966 "too many %qs clauses on a task construct",
7967 "detach");
7968 remove = true;
7969 break;
7971 else if (error_operand_p (t))
7973 remove = true;
7974 break;
7976 else
7978 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (t));
7979 if (!type_dependent_expression_p (t)
7980 && (!INTEGRAL_TYPE_P (type)
7981 || TREE_CODE (type) != ENUMERAL_TYPE
7982 || TYPE_NAME (type) == NULL_TREE
7983 || (DECL_NAME (TYPE_NAME (type))
7984 != get_identifier ("omp_event_handle_t"))))
7986 error_at (OMP_CLAUSE_LOCATION (c),
7987 "%<detach%> clause event handle "
7988 "has type %qT rather than "
7989 "%<omp_event_handle_t%>",
7990 type);
7991 remove = true;
7993 detach_seen = c;
7994 cxx_mark_addressable (t);
7996 break;
7998 case OMP_CLAUSE_MAP:
7999 if (OMP_CLAUSE_MAP_IMPLICIT (c) && !implicit_moved)
8000 goto move_implicit;
8001 /* FALLTHRU */
8002 case OMP_CLAUSE_TO:
8003 case OMP_CLAUSE_FROM:
8004 case OMP_CLAUSE__CACHE_:
8005 t = OMP_CLAUSE_DECL (c);
8006 if (TREE_CODE (t) == TREE_LIST)
8008 grp_start_p = pc;
8009 grp_sentinel = OMP_CLAUSE_CHAIN (c);
8011 if (handle_omp_array_sections (c, ort))
8012 remove = true;
8013 else
8015 t = OMP_CLAUSE_DECL (c);
8016 if (TREE_CODE (t) != TREE_LIST
8017 && !type_dependent_expression_p (t)
8018 && !omp_mappable_type (TREE_TYPE (t)))
8020 error_at (OMP_CLAUSE_LOCATION (c),
8021 "array section does not have mappable type "
8022 "in %qs clause",
8023 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
8024 if (TREE_TYPE (t) != error_mark_node
8025 && !COMPLETE_TYPE_P (TREE_TYPE (t)))
8026 cxx_incomplete_type_inform (TREE_TYPE (t));
8027 remove = true;
8029 while (TREE_CODE (t) == ARRAY_REF)
8030 t = TREE_OPERAND (t, 0);
8031 if (TREE_CODE (t) == COMPONENT_REF
8032 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
8036 t = TREE_OPERAND (t, 0);
8037 if (REFERENCE_REF_P (t))
8038 t = TREE_OPERAND (t, 0);
8039 if (TREE_CODE (t) == MEM_REF
8040 || TREE_CODE (t) == INDIRECT_REF)
8042 t = TREE_OPERAND (t, 0);
8043 STRIP_NOPS (t);
8044 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
8045 t = TREE_OPERAND (t, 0);
8048 while (TREE_CODE (t) == COMPONENT_REF
8049 || TREE_CODE (t) == ARRAY_REF);
8051 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
8052 && OMP_CLAUSE_MAP_IMPLICIT (c)
8053 && (bitmap_bit_p (&map_head, DECL_UID (t))
8054 || bitmap_bit_p (&map_field_head, DECL_UID (t))
8055 || bitmap_bit_p (&map_firstprivate_head,
8056 DECL_UID (t))))
8058 remove = true;
8059 break;
8061 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
8062 break;
8063 if (bitmap_bit_p (&map_head, DECL_UID (t)))
8065 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
8066 error_at (OMP_CLAUSE_LOCATION (c),
8067 "%qD appears more than once in motion"
8068 " clauses", t);
8069 else if (ort == C_ORT_ACC)
8070 error_at (OMP_CLAUSE_LOCATION (c),
8071 "%qD appears more than once in data"
8072 " clauses", t);
8073 else
8074 error_at (OMP_CLAUSE_LOCATION (c),
8075 "%qD appears more than once in map"
8076 " clauses", t);
8077 remove = true;
8079 else
8081 bitmap_set_bit (&map_head, DECL_UID (t));
8082 bitmap_set_bit (&map_field_head, DECL_UID (t));
8086 if (cp_oacc_check_attachments (c))
8087 remove = true;
8088 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
8089 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
8090 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH))
8091 /* In this case, we have a single array element which is a
8092 pointer, and we already set OMP_CLAUSE_SIZE in
8093 handle_omp_array_sections above. For attach/detach clauses,
8094 reset the OMP_CLAUSE_SIZE (representing a bias) to zero
8095 here. */
8096 OMP_CLAUSE_SIZE (c) = size_zero_node;
8097 break;
8099 if (t == error_mark_node)
8101 remove = true;
8102 break;
8104 /* OpenACC attach / detach clauses must be pointers. */
8105 if (cp_oacc_check_attachments (c))
8107 remove = true;
8108 break;
8110 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
8111 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
8112 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH))
8113 /* For attach/detach clauses, set OMP_CLAUSE_SIZE (representing a
8114 bias) to zero here, so it is not set erroneously to the pointer
8115 size later on in gimplify.cc. */
8116 OMP_CLAUSE_SIZE (c) = size_zero_node;
8117 if (REFERENCE_REF_P (t)
8118 && TREE_CODE (TREE_OPERAND (t, 0)) == COMPONENT_REF)
8120 t = TREE_OPERAND (t, 0);
8121 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
8122 && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_ATTACH_DETACH)
8123 OMP_CLAUSE_DECL (c) = t;
8125 while (TREE_CODE (t) == INDIRECT_REF
8126 || TREE_CODE (t) == ARRAY_REF)
8128 t = TREE_OPERAND (t, 0);
8129 STRIP_NOPS (t);
8130 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
8131 t = TREE_OPERAND (t, 0);
8133 while (TREE_CODE (t) == COMPOUND_EXPR)
8135 t = TREE_OPERAND (t, 1);
8136 STRIP_NOPS (t);
8138 if (TREE_CODE (t) == COMPONENT_REF
8139 && invalid_nonstatic_memfn_p (EXPR_LOCATION (t), t,
8140 tf_warning_or_error))
8141 remove = true;
8142 indir_component_ref_p = false;
8143 if (TREE_CODE (t) == COMPONENT_REF
8144 && (TREE_CODE (TREE_OPERAND (t, 0)) == INDIRECT_REF
8145 || TREE_CODE (TREE_OPERAND (t, 0)) == ARRAY_REF))
8147 t = TREE_OPERAND (TREE_OPERAND (t, 0), 0);
8148 indir_component_ref_p = true;
8149 STRIP_NOPS (t);
8150 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
8151 t = TREE_OPERAND (t, 0);
8153 if (TREE_CODE (t) == COMPONENT_REF
8154 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE__CACHE_)
8156 if (type_dependent_expression_p (t))
8157 break;
8158 if (TREE_CODE (TREE_OPERAND (t, 1)) == FIELD_DECL
8159 && DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
8161 error_at (OMP_CLAUSE_LOCATION (c),
8162 "bit-field %qE in %qs clause",
8163 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
8164 remove = true;
8166 else if (!omp_mappable_type (TREE_TYPE (t)))
8168 error_at (OMP_CLAUSE_LOCATION (c),
8169 "%qE does not have a mappable type in %qs clause",
8170 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
8171 if (TREE_TYPE (t) != error_mark_node
8172 && !COMPLETE_TYPE_P (TREE_TYPE (t)))
8173 cxx_incomplete_type_inform (TREE_TYPE (t));
8174 remove = true;
8176 while (TREE_CODE (t) == COMPONENT_REF)
8178 if (TREE_TYPE (TREE_OPERAND (t, 0))
8179 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0)))
8180 == UNION_TYPE))
8182 error_at (OMP_CLAUSE_LOCATION (c),
8183 "%qE is a member of a union", t);
8184 remove = true;
8185 break;
8187 t = TREE_OPERAND (t, 0);
8188 if (TREE_CODE (t) == MEM_REF)
8190 if (maybe_ne (mem_ref_offset (t), 0))
8191 error_at (OMP_CLAUSE_LOCATION (c),
8192 "cannot dereference %qE in %qs clause", t,
8193 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
8194 else
8195 t = TREE_OPERAND (t, 0);
8197 while (TREE_CODE (t) == MEM_REF
8198 || TREE_CODE (t) == INDIRECT_REF
8199 || TREE_CODE (t) == ARRAY_REF)
8201 t = TREE_OPERAND (t, 0);
8202 STRIP_NOPS (t);
8203 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
8204 t = TREE_OPERAND (t, 0);
8207 if (remove)
8208 break;
8209 if (REFERENCE_REF_P (t))
8210 t = TREE_OPERAND (t, 0);
8211 if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
8213 if (bitmap_bit_p (&map_field_head, DECL_UID (t))
8214 || (ort != C_ORT_ACC
8215 && bitmap_bit_p (&map_head, DECL_UID (t))))
8216 goto handle_map_references;
8219 if (!processing_template_decl
8220 && TREE_CODE (t) == FIELD_DECL)
8222 OMP_CLAUSE_DECL (c) = finish_non_static_data_member (t, NULL_TREE,
8223 NULL_TREE);
8224 break;
8226 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
8228 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
8229 break;
8230 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
8231 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
8232 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ALWAYS_POINTER
8233 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH_DETACH))
8234 break;
8235 if (DECL_P (t))
8236 error_at (OMP_CLAUSE_LOCATION (c),
8237 "%qD is not a variable in %qs clause", t,
8238 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
8239 else
8240 error_at (OMP_CLAUSE_LOCATION (c),
8241 "%qE is not a variable in %qs clause", t,
8242 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
8243 remove = true;
8245 else if (VAR_P (t) && CP_DECL_THREAD_LOCAL_P (t))
8247 error_at (OMP_CLAUSE_LOCATION (c),
8248 "%qD is threadprivate variable in %qs clause", t,
8249 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
8250 remove = true;
8252 else if (!processing_template_decl
8253 && !TYPE_REF_P (TREE_TYPE (t))
8254 && (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
8255 || (OMP_CLAUSE_MAP_KIND (c)
8256 != GOMP_MAP_FIRSTPRIVATE_POINTER))
8257 && !indir_component_ref_p
8258 && !cxx_mark_addressable (t))
8259 remove = true;
8260 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
8261 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
8262 || (OMP_CLAUSE_MAP_KIND (c)
8263 == GOMP_MAP_FIRSTPRIVATE_POINTER)))
8264 && t == OMP_CLAUSE_DECL (c)
8265 && !type_dependent_expression_p (t)
8266 && !omp_mappable_type (TYPE_REF_P (TREE_TYPE (t))
8267 ? TREE_TYPE (TREE_TYPE (t))
8268 : TREE_TYPE (t)))
8270 error_at (OMP_CLAUSE_LOCATION (c),
8271 "%qD does not have a mappable type in %qs clause", t,
8272 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
8273 if (TREE_TYPE (t) != error_mark_node
8274 && !COMPLETE_TYPE_P (TREE_TYPE (t)))
8275 cxx_incomplete_type_inform (TREE_TYPE (t));
8276 remove = true;
8278 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
8279 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FORCE_DEVICEPTR
8280 && !type_dependent_expression_p (t)
8281 && !INDIRECT_TYPE_P (TREE_TYPE (t)))
8283 error_at (OMP_CLAUSE_LOCATION (c),
8284 "%qD is not a pointer variable", t);
8285 remove = true;
8287 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
8288 && OMP_CLAUSE_MAP_IMPLICIT (c)
8289 && (bitmap_bit_p (&map_head, DECL_UID (t))
8290 || bitmap_bit_p (&map_field_head, DECL_UID (t))
8291 || bitmap_bit_p (&map_firstprivate_head,
8292 DECL_UID (t))))
8293 remove = true;
8294 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
8295 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
8297 if (bitmap_bit_p (&generic_head, DECL_UID (t))
8298 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
8299 || bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
8301 error_at (OMP_CLAUSE_LOCATION (c),
8302 "%qD appears more than once in data clauses", t);
8303 remove = true;
8305 else if (bitmap_bit_p (&map_head, DECL_UID (t))
8306 && !bitmap_bit_p (&map_field_head, DECL_UID (t)))
8308 if (ort == C_ORT_ACC)
8309 error_at (OMP_CLAUSE_LOCATION (c),
8310 "%qD appears more than once in data clauses", t);
8311 else
8312 error_at (OMP_CLAUSE_LOCATION (c),
8313 "%qD appears both in data and map clauses", t);
8314 remove = true;
8316 else
8317 bitmap_set_bit (&map_firstprivate_head, DECL_UID (t));
8319 else if (bitmap_bit_p (&map_head, DECL_UID (t))
8320 && !bitmap_bit_p (&map_field_head, DECL_UID (t)))
8322 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
8323 error_at (OMP_CLAUSE_LOCATION (c),
8324 "%qD appears more than once in motion clauses", t);
8325 else if (ort == C_ORT_ACC)
8326 error_at (OMP_CLAUSE_LOCATION (c),
8327 "%qD appears more than once in data clauses", t);
8328 else
8329 error_at (OMP_CLAUSE_LOCATION (c),
8330 "%qD appears more than once in map clauses", t);
8331 remove = true;
8333 else if (ort == C_ORT_ACC
8334 && bitmap_bit_p (&generic_head, DECL_UID (t)))
8336 error_at (OMP_CLAUSE_LOCATION (c),
8337 "%qD appears more than once in data clauses", t);
8338 remove = true;
8340 else if (bitmap_bit_p (&firstprivate_head, DECL_UID (t))
8341 || bitmap_bit_p (&is_on_device_head, DECL_UID (t)))
8343 if (ort == C_ORT_ACC)
8344 error_at (OMP_CLAUSE_LOCATION (c),
8345 "%qD appears more than once in data clauses", t);
8346 else
8347 error_at (OMP_CLAUSE_LOCATION (c),
8348 "%qD appears both in data and map clauses", t);
8349 remove = true;
8351 else
8353 bitmap_set_bit (&map_head, DECL_UID (t));
8355 tree decl = OMP_CLAUSE_DECL (c);
8356 if (t != decl
8357 && (TREE_CODE (decl) == COMPONENT_REF
8358 || (INDIRECT_REF_P (decl)
8359 && TREE_CODE (TREE_OPERAND (decl, 0)) == COMPONENT_REF
8360 && TYPE_REF_P (TREE_TYPE (TREE_OPERAND (decl, 0))))))
8361 bitmap_set_bit (&map_field_head, DECL_UID (t));
8363 handle_map_references:
8364 if (!remove
8365 && !processing_template_decl
8366 && ort != C_ORT_DECLARE_SIMD
8367 && TYPE_REF_P (TREE_TYPE (OMP_CLAUSE_DECL (c))))
8369 t = OMP_CLAUSE_DECL (c);
8370 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
8372 OMP_CLAUSE_DECL (c) = build_simple_mem_ref (t);
8373 if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
8374 OMP_CLAUSE_SIZE (c)
8375 = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t)));
8377 else if (OMP_CLAUSE_MAP_KIND (c)
8378 != GOMP_MAP_FIRSTPRIVATE_POINTER
8379 && (OMP_CLAUSE_MAP_KIND (c)
8380 != GOMP_MAP_FIRSTPRIVATE_REFERENCE)
8381 && (OMP_CLAUSE_MAP_KIND (c)
8382 != GOMP_MAP_ALWAYS_POINTER)
8383 && (OMP_CLAUSE_MAP_KIND (c)
8384 != GOMP_MAP_ATTACH_DETACH))
8386 grp_start_p = pc;
8387 grp_sentinel = OMP_CLAUSE_CHAIN (c);
8389 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
8390 OMP_CLAUSE_MAP);
8391 if (TREE_CODE (t) == COMPONENT_REF)
8392 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ALWAYS_POINTER);
8393 else
8394 OMP_CLAUSE_SET_MAP_KIND (c2,
8395 GOMP_MAP_FIRSTPRIVATE_REFERENCE);
8396 OMP_CLAUSE_DECL (c2) = t;
8397 OMP_CLAUSE_SIZE (c2) = size_zero_node;
8398 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
8399 OMP_CLAUSE_CHAIN (c) = c2;
8400 OMP_CLAUSE_DECL (c) = build_simple_mem_ref (t);
8401 if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
8402 OMP_CLAUSE_SIZE (c)
8403 = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t)));
8404 c = c2;
8407 break;
8409 case OMP_CLAUSE_ENTER:
8410 case OMP_CLAUSE_LINK:
8411 t = OMP_CLAUSE_DECL (c);
8412 const char *cname;
8413 cname = omp_clause_code_name[OMP_CLAUSE_CODE (c)];
8414 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ENTER
8415 && OMP_CLAUSE_ENTER_TO (c))
8416 cname = "to";
8417 if (TREE_CODE (t) == FUNCTION_DECL
8418 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ENTER)
8420 else if (!VAR_P (t))
8422 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ENTER)
8424 if (TREE_CODE (t) == TEMPLATE_ID_EXPR)
8425 error_at (OMP_CLAUSE_LOCATION (c),
8426 "template %qE in clause %qs", t, cname);
8427 else if (really_overloaded_fn (t))
8428 error_at (OMP_CLAUSE_LOCATION (c),
8429 "overloaded function name %qE in clause %qs", t,
8430 cname);
8431 else
8432 error_at (OMP_CLAUSE_LOCATION (c),
8433 "%qE is neither a variable nor a function name "
8434 "in clause %qs", t, cname);
8436 else
8437 error_at (OMP_CLAUSE_LOCATION (c),
8438 "%qE is not a variable in clause %qs", t, cname);
8439 remove = true;
8441 else if (DECL_THREAD_LOCAL_P (t))
8443 error_at (OMP_CLAUSE_LOCATION (c),
8444 "%qD is threadprivate variable in %qs clause", t,
8445 cname);
8446 remove = true;
8448 else if (!omp_mappable_type (TREE_TYPE (t)))
8450 error_at (OMP_CLAUSE_LOCATION (c),
8451 "%qD does not have a mappable type in %qs clause", t,
8452 cname);
8453 if (TREE_TYPE (t) != error_mark_node
8454 && !COMPLETE_TYPE_P (TREE_TYPE (t)))
8455 cxx_incomplete_type_inform (TREE_TYPE (t));
8456 remove = true;
8458 if (remove)
8459 break;
8460 if (bitmap_bit_p (&generic_head, DECL_UID (t)))
8462 error_at (OMP_CLAUSE_LOCATION (c),
8463 "%qE appears more than once on the same "
8464 "%<declare target%> directive", t);
8465 remove = true;
8467 else
8468 bitmap_set_bit (&generic_head, DECL_UID (t));
8469 break;
8471 case OMP_CLAUSE_UNIFORM:
8472 t = OMP_CLAUSE_DECL (c);
8473 if (TREE_CODE (t) != PARM_DECL)
8475 if (processing_template_decl)
8476 break;
8477 if (DECL_P (t))
8478 error_at (OMP_CLAUSE_LOCATION (c),
8479 "%qD is not an argument in %<uniform%> clause", t);
8480 else
8481 error_at (OMP_CLAUSE_LOCATION (c),
8482 "%qE is not an argument in %<uniform%> clause", t);
8483 remove = true;
8484 break;
8486 /* map_head bitmap is used as uniform_head if declare_simd. */
8487 bitmap_set_bit (&map_head, DECL_UID (t));
8488 goto check_dup_generic;
8490 case OMP_CLAUSE_GRAINSIZE:
8491 t = OMP_CLAUSE_GRAINSIZE_EXPR (c);
8492 if (t == error_mark_node)
8493 remove = true;
8494 else if (!type_dependent_expression_p (t)
8495 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
8497 error_at (OMP_CLAUSE_LOCATION (c),
8498 "%<grainsize%> expression must be integral");
8499 remove = true;
8501 else
8503 t = mark_rvalue_use (t);
8504 if (!processing_template_decl)
8506 t = maybe_constant_value (t);
8507 if (TREE_CODE (t) == INTEGER_CST
8508 && tree_int_cst_sgn (t) != 1)
8510 warning_at (OMP_CLAUSE_LOCATION (c), 0,
8511 "%<grainsize%> value must be positive");
8512 t = integer_one_node;
8514 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
8516 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
8518 break;
8520 case OMP_CLAUSE_PRIORITY:
8521 t = OMP_CLAUSE_PRIORITY_EXPR (c);
8522 if (t == error_mark_node)
8523 remove = true;
8524 else if (!type_dependent_expression_p (t)
8525 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
8527 error_at (OMP_CLAUSE_LOCATION (c),
8528 "%<priority%> expression must be integral");
8529 remove = true;
8531 else
8533 t = mark_rvalue_use (t);
8534 if (!processing_template_decl)
8536 t = maybe_constant_value (t);
8537 if (TREE_CODE (t) == INTEGER_CST
8538 && tree_int_cst_sgn (t) == -1)
8540 warning_at (OMP_CLAUSE_LOCATION (c), 0,
8541 "%<priority%> value must be non-negative");
8542 t = integer_one_node;
8544 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
8546 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
8548 break;
8550 case OMP_CLAUSE_HINT:
8551 t = OMP_CLAUSE_HINT_EXPR (c);
8552 if (t == error_mark_node)
8553 remove = true;
8554 else if (!type_dependent_expression_p (t)
8555 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
8557 error_at (OMP_CLAUSE_LOCATION (c),
8558 "%<hint%> expression must be integral");
8559 remove = true;
8561 else
8563 t = mark_rvalue_use (t);
8564 if (!processing_template_decl)
8566 t = maybe_constant_value (t);
8567 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
8568 if (TREE_CODE (t) != INTEGER_CST)
8570 error_at (OMP_CLAUSE_LOCATION (c),
8571 "%<hint%> expression must be constant integer "
8572 "expression");
8573 remove = true;
8576 OMP_CLAUSE_HINT_EXPR (c) = t;
8578 break;
8580 case OMP_CLAUSE_FILTER:
8581 t = OMP_CLAUSE_FILTER_EXPR (c);
8582 if (t == error_mark_node)
8583 remove = true;
8584 else if (!type_dependent_expression_p (t)
8585 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
8587 error_at (OMP_CLAUSE_LOCATION (c),
8588 "%<filter%> expression must be integral");
8589 remove = true;
8591 else
8593 t = mark_rvalue_use (t);
8594 if (!processing_template_decl)
8596 t = maybe_constant_value (t);
8597 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
8599 OMP_CLAUSE_FILTER_EXPR (c) = t;
8601 break;
8603 case OMP_CLAUSE_IS_DEVICE_PTR:
8604 case OMP_CLAUSE_USE_DEVICE_PTR:
8605 field_ok = (ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP;
8606 t = OMP_CLAUSE_DECL (c);
8607 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IS_DEVICE_PTR)
8608 bitmap_set_bit (&is_on_device_head, DECL_UID (t));
8609 if (!type_dependent_expression_p (t))
8611 tree type = TREE_TYPE (t);
8612 if (!TYPE_PTR_P (type)
8613 && (!TYPE_REF_P (type) || !TYPE_PTR_P (TREE_TYPE (type))))
8615 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_USE_DEVICE_PTR
8616 && ort == C_ORT_OMP)
8618 error_at (OMP_CLAUSE_LOCATION (c),
8619 "%qs variable is neither a pointer "
8620 "nor reference to pointer",
8621 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
8622 remove = true;
8624 else if (TREE_CODE (type) != ARRAY_TYPE
8625 && (!TYPE_REF_P (type)
8626 || TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE))
8628 error_at (OMP_CLAUSE_LOCATION (c),
8629 "%qs variable is neither a pointer, nor an "
8630 "array nor reference to pointer or array",
8631 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
8632 remove = true;
8636 goto check_dup_generic;
8638 case OMP_CLAUSE_HAS_DEVICE_ADDR:
8639 t = OMP_CLAUSE_DECL (c);
8640 if (TREE_CODE (t) == TREE_LIST)
8642 if (handle_omp_array_sections (c, ort))
8643 remove = true;
8644 else
8646 t = OMP_CLAUSE_DECL (c);
8647 while (TREE_CODE (t) == TREE_LIST)
8648 t = TREE_CHAIN (t);
8649 while (TREE_CODE (t) == INDIRECT_REF
8650 || TREE_CODE (t) == ARRAY_REF)
8651 t = TREE_OPERAND (t, 0);
8654 if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
8656 bitmap_set_bit (&is_on_device_head, DECL_UID (t));
8657 if (!processing_template_decl
8658 && !cxx_mark_addressable (t))
8659 remove = true;
8661 goto check_dup_generic_t;
8663 case OMP_CLAUSE_USE_DEVICE_ADDR:
8664 field_ok = true;
8665 t = OMP_CLAUSE_DECL (c);
8666 if (!processing_template_decl
8667 && (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
8668 && !TYPE_REF_P (TREE_TYPE (t))
8669 && !cxx_mark_addressable (t))
8670 remove = true;
8671 goto check_dup_generic;
8673 case OMP_CLAUSE_NOWAIT:
8674 case OMP_CLAUSE_DEFAULT:
8675 case OMP_CLAUSE_UNTIED:
8676 case OMP_CLAUSE_COLLAPSE:
8677 case OMP_CLAUSE_PARALLEL:
8678 case OMP_CLAUSE_FOR:
8679 case OMP_CLAUSE_SECTIONS:
8680 case OMP_CLAUSE_TASKGROUP:
8681 case OMP_CLAUSE_PROC_BIND:
8682 case OMP_CLAUSE_DEVICE_TYPE:
8683 case OMP_CLAUSE_NOGROUP:
8684 case OMP_CLAUSE_THREADS:
8685 case OMP_CLAUSE_SIMD:
8686 case OMP_CLAUSE_DEFAULTMAP:
8687 case OMP_CLAUSE_BIND:
8688 case OMP_CLAUSE_AUTO:
8689 case OMP_CLAUSE_INDEPENDENT:
8690 case OMP_CLAUSE_SEQ:
8691 case OMP_CLAUSE_IF_PRESENT:
8692 case OMP_CLAUSE_FINALIZE:
8693 case OMP_CLAUSE_NOHOST:
8694 break;
8696 case OMP_CLAUSE_MERGEABLE:
8697 mergeable_seen = true;
8698 break;
8700 case OMP_CLAUSE_TILE:
8701 for (tree list = OMP_CLAUSE_TILE_LIST (c); !remove && list;
8702 list = TREE_CHAIN (list))
8704 t = TREE_VALUE (list);
8706 if (t == error_mark_node)
8707 remove = true;
8708 else if (!type_dependent_expression_p (t)
8709 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
8711 error_at (OMP_CLAUSE_LOCATION (c),
8712 "%<tile%> argument needs integral type");
8713 remove = true;
8715 else
8717 t = mark_rvalue_use (t);
8718 if (!processing_template_decl)
8720 /* Zero is used to indicate '*', we permit you
8721 to get there via an ICE of value zero. */
8722 t = maybe_constant_value (t);
8723 if (!tree_fits_shwi_p (t)
8724 || tree_to_shwi (t) < 0)
8726 error_at (OMP_CLAUSE_LOCATION (c),
8727 "%<tile%> argument needs positive "
8728 "integral constant");
8729 remove = true;
8731 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
8735 /* Update list item. */
8736 TREE_VALUE (list) = t;
8738 break;
8740 case OMP_CLAUSE_ORDERED:
8741 ordered_seen = true;
8742 break;
8744 case OMP_CLAUSE_ORDER:
8745 if (order_seen)
8746 remove = true;
8747 else
8748 order_seen = true;
8749 break;
8751 case OMP_CLAUSE_INBRANCH:
8752 case OMP_CLAUSE_NOTINBRANCH:
8753 if (branch_seen)
8755 error_at (OMP_CLAUSE_LOCATION (c),
8756 "%<inbranch%> clause is incompatible with "
8757 "%<notinbranch%>");
8758 remove = true;
8760 branch_seen = true;
8761 break;
8763 case OMP_CLAUSE_INCLUSIVE:
8764 case OMP_CLAUSE_EXCLUSIVE:
8765 t = omp_clause_decl_field (OMP_CLAUSE_DECL (c));
8766 if (!t)
8767 t = OMP_CLAUSE_DECL (c);
8768 if (t == current_class_ptr)
8770 error_at (OMP_CLAUSE_LOCATION (c),
8771 "%<this%> allowed in OpenMP only in %<declare simd%>"
8772 " clauses");
8773 remove = true;
8774 break;
8776 if (!VAR_P (t)
8777 && TREE_CODE (t) != PARM_DECL
8778 && TREE_CODE (t) != FIELD_DECL)
8780 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
8781 break;
8782 if (DECL_P (t))
8783 error_at (OMP_CLAUSE_LOCATION (c),
8784 "%qD is not a variable in clause %qs", t,
8785 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
8786 else
8787 error_at (OMP_CLAUSE_LOCATION (c),
8788 "%qE is not a variable in clause %qs", t,
8789 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
8790 remove = true;
8792 break;
8794 default:
8795 gcc_unreachable ();
8798 if (remove)
8800 if (grp_start_p)
8802 /* If we found a clause to remove, we want to remove the whole
8803 expanded group, otherwise gimplify can get confused. */
8804 *grp_start_p = grp_sentinel;
8805 pc = grp_start_p;
8806 grp_start_p = NULL;
8808 else
8809 *pc = OMP_CLAUSE_CHAIN (c);
8811 else
8812 pc = &OMP_CLAUSE_CHAIN (c);
8815 if (reduction_seen < 0 && (ordered_seen || schedule_seen))
8816 reduction_seen = -2;
8818 for (pc = &clauses, c = clauses; c ; c = *pc)
8820 enum omp_clause_code c_kind = OMP_CLAUSE_CODE (c);
8821 bool remove = false;
8822 bool need_complete_type = false;
8823 bool need_default_ctor = false;
8824 bool need_copy_ctor = false;
8825 bool need_copy_assignment = false;
8826 bool need_implicitly_determined = false;
8827 bool need_dtor = false;
8828 tree type, inner_type;
8830 switch (c_kind)
8832 case OMP_CLAUSE_SHARED:
8833 need_implicitly_determined = true;
8834 break;
8835 case OMP_CLAUSE_PRIVATE:
8836 need_complete_type = true;
8837 need_default_ctor = true;
8838 need_dtor = true;
8839 need_implicitly_determined = true;
8840 break;
8841 case OMP_CLAUSE_FIRSTPRIVATE:
8842 need_complete_type = true;
8843 need_copy_ctor = true;
8844 need_dtor = true;
8845 need_implicitly_determined = true;
8846 break;
8847 case OMP_CLAUSE_LASTPRIVATE:
8848 need_complete_type = true;
8849 need_copy_assignment = true;
8850 need_implicitly_determined = true;
8851 break;
8852 case OMP_CLAUSE_REDUCTION:
8853 if (reduction_seen == -2)
8854 OMP_CLAUSE_REDUCTION_INSCAN (c) = 0;
8855 if (OMP_CLAUSE_REDUCTION_INSCAN (c))
8856 need_copy_assignment = true;
8857 need_implicitly_determined = true;
8858 break;
8859 case OMP_CLAUSE_IN_REDUCTION:
8860 case OMP_CLAUSE_TASK_REDUCTION:
8861 case OMP_CLAUSE_INCLUSIVE:
8862 case OMP_CLAUSE_EXCLUSIVE:
8863 need_implicitly_determined = true;
8864 break;
8865 case OMP_CLAUSE_LINEAR:
8866 if (ort != C_ORT_OMP_DECLARE_SIMD)
8867 need_implicitly_determined = true;
8868 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c)
8869 && !bitmap_bit_p (&map_head,
8870 DECL_UID (OMP_CLAUSE_LINEAR_STEP (c))))
8872 error_at (OMP_CLAUSE_LOCATION (c),
8873 "%<linear%> clause step is a parameter %qD not "
8874 "specified in %<uniform%> clause",
8875 OMP_CLAUSE_LINEAR_STEP (c));
8876 *pc = OMP_CLAUSE_CHAIN (c);
8877 continue;
8879 break;
8880 case OMP_CLAUSE_COPYPRIVATE:
8881 need_copy_assignment = true;
8882 break;
8883 case OMP_CLAUSE_COPYIN:
8884 need_copy_assignment = true;
8885 break;
8886 case OMP_CLAUSE_SIMDLEN:
8887 if (safelen
8888 && !processing_template_decl
8889 && tree_int_cst_lt (OMP_CLAUSE_SAFELEN_EXPR (safelen),
8890 OMP_CLAUSE_SIMDLEN_EXPR (c)))
8892 error_at (OMP_CLAUSE_LOCATION (c),
8893 "%<simdlen%> clause value is bigger than "
8894 "%<safelen%> clause value");
8895 OMP_CLAUSE_SIMDLEN_EXPR (c)
8896 = OMP_CLAUSE_SAFELEN_EXPR (safelen);
8898 pc = &OMP_CLAUSE_CHAIN (c);
8899 continue;
8900 case OMP_CLAUSE_SCHEDULE:
8901 if (ordered_seen
8902 && (OMP_CLAUSE_SCHEDULE_KIND (c)
8903 & OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
8905 error_at (OMP_CLAUSE_LOCATION (c),
8906 "%<nonmonotonic%> schedule modifier specified "
8907 "together with %<ordered%> clause");
8908 OMP_CLAUSE_SCHEDULE_KIND (c)
8909 = (enum omp_clause_schedule_kind)
8910 (OMP_CLAUSE_SCHEDULE_KIND (c)
8911 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
8913 if (reduction_seen == -2)
8914 error_at (OMP_CLAUSE_LOCATION (c),
8915 "%qs clause specified together with %<inscan%> "
8916 "%<reduction%> clause", "schedule");
8917 pc = &OMP_CLAUSE_CHAIN (c);
8918 continue;
8919 case OMP_CLAUSE_NOGROUP:
8920 if (reduction_seen)
8922 error_at (OMP_CLAUSE_LOCATION (c),
8923 "%<nogroup%> clause must not be used together with "
8924 "%<reduction%> clause");
8925 *pc = OMP_CLAUSE_CHAIN (c);
8926 continue;
8928 pc = &OMP_CLAUSE_CHAIN (c);
8929 continue;
8930 case OMP_CLAUSE_ORDERED:
8931 if (reduction_seen == -2)
8932 error_at (OMP_CLAUSE_LOCATION (c),
8933 "%qs clause specified together with %<inscan%> "
8934 "%<reduction%> clause", "ordered");
8935 pc = &OMP_CLAUSE_CHAIN (c);
8936 continue;
8937 case OMP_CLAUSE_ORDER:
8938 if (ordered_seen)
8940 error_at (OMP_CLAUSE_LOCATION (c),
8941 "%<order%> clause must not be used together "
8942 "with %<ordered%>");
8943 *pc = OMP_CLAUSE_CHAIN (c);
8944 continue;
8946 pc = &OMP_CLAUSE_CHAIN (c);
8947 continue;
8948 case OMP_CLAUSE_DETACH:
8949 if (mergeable_seen)
8951 error_at (OMP_CLAUSE_LOCATION (c),
8952 "%<detach%> clause must not be used together with "
8953 "%<mergeable%> clause");
8954 *pc = OMP_CLAUSE_CHAIN (c);
8955 continue;
8957 pc = &OMP_CLAUSE_CHAIN (c);
8958 continue;
8959 case OMP_CLAUSE_MAP:
8960 if (target_in_reduction_seen && !processing_template_decl)
8962 t = OMP_CLAUSE_DECL (c);
8963 while (handled_component_p (t)
8964 || TREE_CODE (t) == INDIRECT_REF
8965 || TREE_CODE (t) == ADDR_EXPR
8966 || TREE_CODE (t) == MEM_REF
8967 || TREE_CODE (t) == NON_LVALUE_EXPR)
8968 t = TREE_OPERAND (t, 0);
8969 if (DECL_P (t)
8970 && bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
8971 OMP_CLAUSE_MAP_IN_REDUCTION (c) = 1;
8973 pc = &OMP_CLAUSE_CHAIN (c);
8974 continue;
8975 case OMP_CLAUSE_NOWAIT:
8976 if (copyprivate_seen)
8978 error_at (OMP_CLAUSE_LOCATION (c),
8979 "%<nowait%> clause must not be used together "
8980 "with %<copyprivate%>");
8981 *pc = OMP_CLAUSE_CHAIN (c);
8982 continue;
8984 /* FALLTHRU */
8985 default:
8986 pc = &OMP_CLAUSE_CHAIN (c);
8987 continue;
8990 t = OMP_CLAUSE_DECL (c);
8991 switch (c_kind)
8993 case OMP_CLAUSE_LASTPRIVATE:
8994 if (DECL_P (t)
8995 && !bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
8997 need_default_ctor = true;
8998 need_dtor = true;
9000 break;
9002 case OMP_CLAUSE_REDUCTION:
9003 case OMP_CLAUSE_IN_REDUCTION:
9004 case OMP_CLAUSE_TASK_REDUCTION:
9005 if (allocate_seen)
9007 if (TREE_CODE (t) == MEM_REF)
9009 t = TREE_OPERAND (t, 0);
9010 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
9011 t = TREE_OPERAND (t, 0);
9012 if (TREE_CODE (t) == ADDR_EXPR
9013 || TREE_CODE (t) == INDIRECT_REF)
9014 t = TREE_OPERAND (t, 0);
9015 if (DECL_P (t))
9016 bitmap_clear_bit (&aligned_head, DECL_UID (t));
9018 else if (TREE_CODE (t) == TREE_LIST)
9020 while (TREE_CODE (t) == TREE_LIST)
9021 t = TREE_CHAIN (t);
9022 if (DECL_P (t))
9023 bitmap_clear_bit (&aligned_head, DECL_UID (t));
9024 t = OMP_CLAUSE_DECL (c);
9026 else if (DECL_P (t))
9027 bitmap_clear_bit (&aligned_head, DECL_UID (t));
9028 t = OMP_CLAUSE_DECL (c);
9030 if (processing_template_decl
9031 && !VAR_P (t) && TREE_CODE (t) != PARM_DECL)
9032 break;
9033 if (finish_omp_reduction_clause (c, &need_default_ctor,
9034 &need_dtor))
9035 remove = true;
9036 else
9037 t = OMP_CLAUSE_DECL (c);
9038 break;
9040 case OMP_CLAUSE_COPYIN:
9041 if (processing_template_decl
9042 && !VAR_P (t) && TREE_CODE (t) != PARM_DECL)
9043 break;
9044 if (!VAR_P (t) || !CP_DECL_THREAD_LOCAL_P (t))
9046 error_at (OMP_CLAUSE_LOCATION (c),
9047 "%qE must be %<threadprivate%> for %<copyin%>", t);
9048 remove = true;
9050 break;
9052 default:
9053 break;
9056 if (processing_template_decl
9057 && !VAR_P (t) && TREE_CODE (t) != PARM_DECL)
9059 pc = &OMP_CLAUSE_CHAIN (c);
9060 continue;
9063 if (need_complete_type || need_copy_assignment)
9065 t = require_complete_type (t);
9066 if (t == error_mark_node)
9067 remove = true;
9068 else if (!processing_template_decl
9069 && TYPE_REF_P (TREE_TYPE (t))
9070 && !complete_type_or_else (TREE_TYPE (TREE_TYPE (t)), t))
9071 remove = true;
9073 if (need_implicitly_determined)
9075 const char *share_name = NULL;
9077 if (allocate_seen
9078 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_SHARED
9079 && DECL_P (t))
9080 bitmap_clear_bit (&aligned_head, DECL_UID (t));
9082 if (VAR_P (t) && CP_DECL_THREAD_LOCAL_P (t))
9083 share_name = "threadprivate";
9084 else switch (cxx_omp_predetermined_sharing_1 (t))
9086 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
9087 break;
9088 case OMP_CLAUSE_DEFAULT_SHARED:
9089 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
9090 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE)
9091 && c_omp_predefined_variable (t))
9092 /* The __func__ variable and similar function-local predefined
9093 variables may be listed in a shared or firstprivate
9094 clause. */
9095 break;
9096 if (VAR_P (t)
9097 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
9098 && TREE_STATIC (t)
9099 && cxx_omp_const_qual_no_mutable (t))
9101 tree ctx = CP_DECL_CONTEXT (t);
9102 /* const qualified static data members without mutable
9103 member may be specified in firstprivate clause. */
9104 if (TYPE_P (ctx) && MAYBE_CLASS_TYPE_P (ctx))
9105 break;
9107 share_name = "shared";
9108 break;
9109 case OMP_CLAUSE_DEFAULT_PRIVATE:
9110 share_name = "private";
9111 break;
9112 default:
9113 gcc_unreachable ();
9115 if (share_name)
9117 error_at (OMP_CLAUSE_LOCATION (c),
9118 "%qE is predetermined %qs for %qs",
9119 omp_clause_printable_decl (t), share_name,
9120 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
9121 remove = true;
9123 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_SHARED
9124 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_FIRSTPRIVATE
9125 && cxx_omp_const_qual_no_mutable (t))
9127 error_at (OMP_CLAUSE_LOCATION (c),
9128 "%<const%> qualified %qE without %<mutable%> member "
9129 "may appear only in %<shared%> or %<firstprivate%> "
9130 "clauses", omp_clause_printable_decl (t));
9131 remove = true;
9135 if (detach_seen
9136 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
9137 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
9138 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
9139 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
9140 && OMP_CLAUSE_DECL (c) == OMP_CLAUSE_DECL (detach_seen))
9142 error_at (OMP_CLAUSE_LOCATION (c),
9143 "the event handle of a %<detach%> clause "
9144 "should not be in a data-sharing clause");
9145 remove = true;
9148 /* We're interested in the base element, not arrays. */
9149 inner_type = type = TREE_TYPE (t);
9150 if ((need_complete_type
9151 || need_copy_assignment
9152 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
9153 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
9154 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
9155 && TYPE_REF_P (inner_type))
9156 inner_type = TREE_TYPE (inner_type);
9157 while (TREE_CODE (inner_type) == ARRAY_TYPE)
9158 inner_type = TREE_TYPE (inner_type);
9160 /* Check for special function availability by building a call to one.
9161 Save the results, because later we won't be in the right context
9162 for making these queries. */
9163 if (CLASS_TYPE_P (inner_type)
9164 && COMPLETE_TYPE_P (inner_type)
9165 && (need_default_ctor || need_copy_ctor
9166 || need_copy_assignment || need_dtor)
9167 && !type_dependent_expression_p (t)
9168 && cxx_omp_create_clause_info (c, inner_type, need_default_ctor,
9169 need_copy_ctor, need_copy_assignment,
9170 need_dtor))
9171 remove = true;
9173 if (!remove
9174 && c_kind == OMP_CLAUSE_SHARED
9175 && processing_template_decl)
9177 t = omp_clause_decl_field (OMP_CLAUSE_DECL (c));
9178 if (t)
9179 OMP_CLAUSE_DECL (c) = t;
9182 if (remove)
9183 *pc = OMP_CLAUSE_CHAIN (c);
9184 else
9185 pc = &OMP_CLAUSE_CHAIN (c);
9188 if (allocate_seen)
9189 for (pc = &clauses, c = clauses; c ; c = *pc)
9191 bool remove = false;
9192 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ALLOCATE
9193 && !OMP_CLAUSE_ALLOCATE_COMBINED (c)
9194 && DECL_P (OMP_CLAUSE_DECL (c))
9195 && bitmap_bit_p (&aligned_head, DECL_UID (OMP_CLAUSE_DECL (c))))
9197 error_at (OMP_CLAUSE_LOCATION (c),
9198 "%qD specified in %<allocate%> clause but not in "
9199 "an explicit privatization clause", OMP_CLAUSE_DECL (c));
9200 remove = true;
9202 if (remove)
9203 *pc = OMP_CLAUSE_CHAIN (c);
9204 else
9205 pc = &OMP_CLAUSE_CHAIN (c);
9208 bitmap_obstack_release (NULL);
9209 return clauses;
9212 /* Start processing OpenMP clauses that can include any
9213 privatization clauses for non-static data members. */
9215 tree
9216 push_omp_privatization_clauses (bool ignore_next)
9218 if (omp_private_member_ignore_next)
9220 omp_private_member_ignore_next = ignore_next;
9221 return NULL_TREE;
9223 omp_private_member_ignore_next = ignore_next;
9224 if (omp_private_member_map)
9225 omp_private_member_vec.safe_push (error_mark_node);
9226 return push_stmt_list ();
9229 /* Revert remapping of any non-static data members since
9230 the last push_omp_privatization_clauses () call. */
9232 void
9233 pop_omp_privatization_clauses (tree stmt)
9235 if (stmt == NULL_TREE)
9236 return;
9237 stmt = pop_stmt_list (stmt);
9238 if (omp_private_member_map)
9240 while (!omp_private_member_vec.is_empty ())
9242 tree t = omp_private_member_vec.pop ();
9243 if (t == error_mark_node)
9245 add_stmt (stmt);
9246 return;
9248 bool no_decl_expr = t == integer_zero_node;
9249 if (no_decl_expr)
9250 t = omp_private_member_vec.pop ();
9251 tree *v = omp_private_member_map->get (t);
9252 gcc_assert (v);
9253 if (!no_decl_expr)
9254 add_decl_expr (*v);
9255 omp_private_member_map->remove (t);
9257 delete omp_private_member_map;
9258 omp_private_member_map = NULL;
9260 add_stmt (stmt);
9263 /* Remember OpenMP privatization clauses mapping and clear it.
9264 Used for lambdas. */
9266 void
9267 save_omp_privatization_clauses (vec<tree> &save)
9269 save = vNULL;
9270 if (omp_private_member_ignore_next)
9271 save.safe_push (integer_one_node);
9272 omp_private_member_ignore_next = false;
9273 if (!omp_private_member_map)
9274 return;
9276 while (!omp_private_member_vec.is_empty ())
9278 tree t = omp_private_member_vec.pop ();
9279 if (t == error_mark_node)
9281 save.safe_push (t);
9282 continue;
9284 tree n = t;
9285 if (t == integer_zero_node)
9286 t = omp_private_member_vec.pop ();
9287 tree *v = omp_private_member_map->get (t);
9288 gcc_assert (v);
9289 save.safe_push (*v);
9290 save.safe_push (t);
9291 if (n != t)
9292 save.safe_push (n);
9294 delete omp_private_member_map;
9295 omp_private_member_map = NULL;
9298 /* Restore OpenMP privatization clauses mapping saved by the
9299 above function. */
9301 void
9302 restore_omp_privatization_clauses (vec<tree> &save)
9304 gcc_assert (omp_private_member_vec.is_empty ());
9305 omp_private_member_ignore_next = false;
9306 if (save.is_empty ())
9307 return;
9308 if (save.length () == 1 && save[0] == integer_one_node)
9310 omp_private_member_ignore_next = true;
9311 save.release ();
9312 return;
9315 omp_private_member_map = new hash_map <tree, tree>;
9316 while (!save.is_empty ())
9318 tree t = save.pop ();
9319 tree n = t;
9320 if (t != error_mark_node)
9322 if (t == integer_one_node)
9324 omp_private_member_ignore_next = true;
9325 gcc_assert (save.is_empty ());
9326 break;
9328 if (t == integer_zero_node)
9329 t = save.pop ();
9330 tree &v = omp_private_member_map->get_or_insert (t);
9331 v = save.pop ();
9333 omp_private_member_vec.safe_push (t);
9334 if (n != t)
9335 omp_private_member_vec.safe_push (n);
9337 save.release ();
9340 /* For all variables in the tree_list VARS, mark them as thread local. */
9342 void
9343 finish_omp_threadprivate (tree vars)
9345 tree t;
9347 /* Mark every variable in VARS to be assigned thread local storage. */
9348 for (t = vars; t; t = TREE_CHAIN (t))
9350 tree v = TREE_PURPOSE (t);
9352 if (error_operand_p (v))
9354 else if (!VAR_P (v))
9355 error ("%<threadprivate%> %qD is not file, namespace "
9356 "or block scope variable", v);
9357 /* If V had already been marked threadprivate, it doesn't matter
9358 whether it had been used prior to this point. */
9359 else if (TREE_USED (v)
9360 && (DECL_LANG_SPECIFIC (v) == NULL
9361 || !CP_DECL_THREADPRIVATE_P (v)))
9362 error ("%qE declared %<threadprivate%> after first use", v);
9363 else if (! TREE_STATIC (v) && ! DECL_EXTERNAL (v))
9364 error ("automatic variable %qE cannot be %<threadprivate%>", v);
9365 else if (! COMPLETE_TYPE_P (complete_type (TREE_TYPE (v))))
9366 error ("%<threadprivate%> %qE has incomplete type", v);
9367 else if (TREE_STATIC (v) && TYPE_P (CP_DECL_CONTEXT (v))
9368 && CP_DECL_CONTEXT (v) != current_class_type)
9369 error ("%<threadprivate%> %qE directive not "
9370 "in %qT definition", v, CP_DECL_CONTEXT (v));
9371 else
9373 /* Allocate a LANG_SPECIFIC structure for V, if needed. */
9374 if (DECL_LANG_SPECIFIC (v) == NULL)
9375 retrofit_lang_decl (v);
9377 if (! CP_DECL_THREAD_LOCAL_P (v))
9379 CP_DECL_THREAD_LOCAL_P (v) = true;
9380 set_decl_tls_model (v, decl_default_tls_model (v));
9381 /* If rtl has been already set for this var, call
9382 make_decl_rtl once again, so that encode_section_info
9383 has a chance to look at the new decl flags. */
9384 if (DECL_RTL_SET_P (v))
9385 make_decl_rtl (v);
9387 CP_DECL_THREADPRIVATE_P (v) = 1;
9392 /* Build an OpenMP structured block. */
9394 tree
9395 begin_omp_structured_block (void)
9397 return do_pushlevel (sk_omp);
9400 tree
9401 finish_omp_structured_block (tree block)
9403 return do_poplevel (block);
9406 /* Similarly, except force the retention of the BLOCK. */
9408 tree
9409 begin_omp_parallel (void)
9411 keep_next_level (true);
9412 return begin_omp_structured_block ();
9415 /* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
9416 statement. */
9418 tree
9419 finish_oacc_data (tree clauses, tree block)
9421 tree stmt;
9423 block = finish_omp_structured_block (block);
9425 stmt = make_node (OACC_DATA);
9426 TREE_TYPE (stmt) = void_type_node;
9427 OACC_DATA_CLAUSES (stmt) = clauses;
9428 OACC_DATA_BODY (stmt) = block;
9430 return add_stmt (stmt);
9433 /* Generate OACC_HOST_DATA, with CLAUSES and BLOCK as its compound
9434 statement. */
9436 tree
9437 finish_oacc_host_data (tree clauses, tree block)
9439 tree stmt;
9441 block = finish_omp_structured_block (block);
9443 stmt = make_node (OACC_HOST_DATA);
9444 TREE_TYPE (stmt) = void_type_node;
9445 OACC_HOST_DATA_CLAUSES (stmt) = clauses;
9446 OACC_HOST_DATA_BODY (stmt) = block;
9448 return add_stmt (stmt);
9451 /* Generate OMP construct CODE, with BODY and CLAUSES as its compound
9452 statement. */
9454 tree
9455 finish_omp_construct (enum tree_code code, tree body, tree clauses)
9457 body = finish_omp_structured_block (body);
9459 tree stmt = make_node (code);
9460 TREE_TYPE (stmt) = void_type_node;
9461 OMP_BODY (stmt) = body;
9462 OMP_CLAUSES (stmt) = clauses;
9464 return add_stmt (stmt);
9467 /* Used to walk OpenMP target directive body. */
9469 struct omp_target_walk_data
9471 /* Holds the 'this' expression found in current function. */
9472 tree current_object;
9474 /* True if the 'this' expression was accessed in the target body. */
9475 bool this_expr_accessed;
9477 /* For non-static functions, record which pointer-typed members were
9478 accessed, and the whole expression. */
9479 hash_map<tree, tree> ptr_members_accessed;
9481 /* Record which lambda objects were accessed in target body. */
9482 hash_set<tree> lambda_objects_accessed;
9484 /* For lambda functions, the __closure object expression of the current
9485 function, and the set of captured variables accessed in target body. */
9486 tree current_closure;
9487 hash_set<tree> closure_vars_accessed;
9489 /* Local variables declared inside a BIND_EXPR, used to filter out such
9490 variables when recording lambda_objects_accessed. */
9491 hash_set<tree> local_decls;
9494 /* Helper function of finish_omp_target_clauses, called via
9495 cp_walk_tree_without_duplicates. Traverse body of OpenMP target
9496 directive *TP, and fill out omp_target_walk_data passed in *PTR. */
9498 static tree
9499 finish_omp_target_clauses_r (tree *tp, int *walk_subtrees, void *ptr)
9501 tree t = *tp;
9502 struct omp_target_walk_data *data = (struct omp_target_walk_data *) ptr;
9503 tree current_object = data->current_object;
9504 tree current_closure = data->current_closure;
9506 /* References inside of these expression codes shouldn't incur any
9507 form of mapping, so return early. */
9508 if (TREE_CODE (t) == SIZEOF_EXPR
9509 || TREE_CODE (t) == ALIGNOF_EXPR)
9511 *walk_subtrees = 0;
9512 return NULL_TREE;
9515 if (TREE_CODE (t) == OMP_CLAUSE)
9516 return NULL_TREE;
9518 if (current_object)
9520 tree this_expr = TREE_OPERAND (current_object, 0);
9522 if (operand_equal_p (t, this_expr))
9524 data->this_expr_accessed = true;
9525 *walk_subtrees = 0;
9526 return NULL_TREE;
9529 if (TREE_CODE (t) == COMPONENT_REF
9530 && POINTER_TYPE_P (TREE_TYPE (t))
9531 && operand_equal_p (TREE_OPERAND (t, 0), current_object)
9532 && TREE_CODE (TREE_OPERAND (t, 1)) == FIELD_DECL)
9534 data->this_expr_accessed = true;
9535 tree fld = TREE_OPERAND (t, 1);
9536 if (data->ptr_members_accessed.get (fld) == NULL)
9538 if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE)
9539 t = convert_from_reference (t);
9540 data->ptr_members_accessed.put (fld, t);
9542 *walk_subtrees = 0;
9543 return NULL_TREE;
9547 /* When the current_function_decl is a lambda function, the closure object
9548 argument's type seems to not yet have fields layed out, so a recording
9549 of DECL_VALUE_EXPRs during the target body walk seems the only way to
9550 find them. */
9551 if (current_closure
9552 && (TREE_CODE (t) == VAR_DECL
9553 || TREE_CODE (t) == PARM_DECL
9554 || TREE_CODE (t) == RESULT_DECL)
9555 && DECL_HAS_VALUE_EXPR_P (t)
9556 && TREE_CODE (DECL_VALUE_EXPR (t)) == COMPONENT_REF
9557 && operand_equal_p (current_closure,
9558 TREE_OPERAND (DECL_VALUE_EXPR (t), 0)))
9560 if (!data->closure_vars_accessed.contains (t))
9561 data->closure_vars_accessed.add (t);
9562 *walk_subtrees = 0;
9563 return NULL_TREE;
9566 if (TREE_CODE (t) == BIND_EXPR)
9568 tree block = BIND_EXPR_BLOCK (t);
9569 for (tree var = BLOCK_VARS (block); var; var = DECL_CHAIN (var))
9570 if (!data->local_decls.contains (var))
9571 data->local_decls.add (var);
9572 return NULL_TREE;
9575 if (TREE_TYPE (t) && LAMBDA_TYPE_P (TREE_TYPE (t)))
9577 tree lt = TREE_TYPE (t);
9578 gcc_assert (CLASS_TYPE_P (lt));
9580 if (!data->lambda_objects_accessed.contains (t)
9581 /* Do not prepare to create target maps for locally declared
9582 lambdas or anonymous ones. */
9583 && !data->local_decls.contains (t)
9584 && TREE_CODE (t) != TARGET_EXPR)
9585 data->lambda_objects_accessed.add (t);
9586 *walk_subtrees = 0;
9587 return NULL_TREE;
9590 return NULL_TREE;
9593 /* Helper function for finish_omp_target, and also from tsubst_expr.
9594 Create additional clauses for mapping of non-static members, lambda objects,
9595 etc. */
9597 void
9598 finish_omp_target_clauses (location_t loc, tree body, tree *clauses_ptr)
9600 omp_target_walk_data data;
9601 data.this_expr_accessed = false;
9602 data.current_object = NULL_TREE;
9604 if (DECL_NONSTATIC_MEMBER_P (current_function_decl) && current_class_ptr)
9605 if (tree ct = current_nonlambda_class_type ())
9607 tree object = maybe_dummy_object (ct, NULL);
9608 object = maybe_resolve_dummy (object, true);
9609 data.current_object = object;
9612 if (DECL_LAMBDA_FUNCTION_P (current_function_decl))
9614 tree closure = DECL_ARGUMENTS (current_function_decl);
9615 data.current_closure = build_indirect_ref (loc, closure, RO_UNARY_STAR);
9617 else
9618 data.current_closure = NULL_TREE;
9620 cp_walk_tree_without_duplicates (&body, finish_omp_target_clauses_r, &data);
9622 auto_vec<tree, 16> new_clauses;
9624 tree omp_target_this_expr = NULL_TREE;
9625 tree *explicit_this_deref_map = NULL;
9626 if (data.this_expr_accessed)
9628 omp_target_this_expr = TREE_OPERAND (data.current_object, 0);
9630 /* See if explicit user-specified map(this[:]) clause already exists.
9631 If not, we create an implicit map(tofrom:this[:1]) clause. */
9632 for (tree *cp = clauses_ptr; *cp; cp = &OMP_CLAUSE_CHAIN (*cp))
9633 if (OMP_CLAUSE_CODE (*cp) == OMP_CLAUSE_MAP
9634 && (TREE_CODE (OMP_CLAUSE_DECL (*cp)) == INDIRECT_REF
9635 || TREE_CODE (OMP_CLAUSE_DECL (*cp)) == MEM_REF)
9636 && operand_equal_p (TREE_OPERAND (OMP_CLAUSE_DECL (*cp), 0),
9637 omp_target_this_expr))
9639 explicit_this_deref_map = cp;
9640 break;
9644 if (DECL_LAMBDA_FUNCTION_P (current_function_decl)
9645 && (data.this_expr_accessed
9646 || !data.closure_vars_accessed.is_empty ()))
9648 /* For lambda functions, we need to first create a copy of the
9649 __closure object. */
9650 tree closure = DECL_ARGUMENTS (current_function_decl);
9651 tree c = build_omp_clause (loc, OMP_CLAUSE_MAP);
9652 OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_TO);
9653 OMP_CLAUSE_DECL (c)
9654 = build_indirect_ref (loc, closure, RO_UNARY_STAR);
9655 OMP_CLAUSE_SIZE (c)
9656 = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (closure)));
9657 new_clauses.safe_push (c);
9659 tree closure_obj = OMP_CLAUSE_DECL (c);
9660 tree closure_type = TREE_TYPE (closure_obj);
9662 gcc_assert (LAMBDA_TYPE_P (closure_type)
9663 && CLASS_TYPE_P (closure_type));
9665 tree c2 = build_omp_clause (loc, OMP_CLAUSE_MAP);
9666 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_FIRSTPRIVATE_POINTER);
9667 OMP_CLAUSE_DECL (c2) = closure;
9668 OMP_CLAUSE_SIZE (c2) = size_zero_node;
9669 new_clauses.safe_push (c2);
9672 if (data.this_expr_accessed)
9674 /* If the this-expr was accessed, create a map(*this) clause. */
9675 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
9676 if (explicit_this_deref_map)
9678 tree this_map = *explicit_this_deref_map;
9679 tree nc = OMP_CLAUSE_CHAIN (this_map);
9680 gcc_assert (nc != NULL_TREE
9681 && OMP_CLAUSE_CODE (nc) == OMP_CLAUSE_MAP
9682 && (OMP_CLAUSE_MAP_KIND (nc)
9683 == GOMP_MAP_FIRSTPRIVATE_POINTER));
9684 kind = OMP_CLAUSE_MAP_KIND (this_map);
9685 /* Remove the original 'map(*this) map(firstprivate_ptr:this)'
9686 two-map sequence away from the chain. */
9687 *explicit_this_deref_map = OMP_CLAUSE_CHAIN (nc);
9689 tree c = build_omp_clause (loc, OMP_CLAUSE_MAP);
9690 OMP_CLAUSE_SET_MAP_KIND (c, kind);
9691 OMP_CLAUSE_DECL (c)
9692 = build_indirect_ref (loc, omp_target_this_expr, RO_UNARY_STAR);
9693 OMP_CLAUSE_SIZE (c)
9694 = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (omp_target_this_expr)));
9695 new_clauses.safe_push (c);
9697 /* If we're in a lambda function, the this-pointer will actually be
9698 '__closure->this', a mapped member of __closure, hence always_pointer.
9699 Otherwise it's a firstprivate pointer. */
9700 enum gomp_map_kind ptr_kind
9701 = (DECL_LAMBDA_FUNCTION_P (current_function_decl)
9702 ? GOMP_MAP_ALWAYS_POINTER
9703 : GOMP_MAP_FIRSTPRIVATE_POINTER);
9704 c = build_omp_clause (loc, OMP_CLAUSE_MAP);
9705 OMP_CLAUSE_SET_MAP_KIND (c, ptr_kind);
9706 OMP_CLAUSE_DECL (c) = omp_target_this_expr;
9707 OMP_CLAUSE_SIZE (c) = size_zero_node;
9708 new_clauses.safe_push (c);
9711 if (DECL_LAMBDA_FUNCTION_P (current_function_decl))
9713 if (omp_target_this_expr)
9715 STRIP_NOPS (omp_target_this_expr);
9716 gcc_assert (DECL_HAS_VALUE_EXPR_P (omp_target_this_expr));
9717 omp_target_this_expr = DECL_VALUE_EXPR (omp_target_this_expr);
9720 for (hash_set<tree>::iterator i = data.closure_vars_accessed.begin ();
9721 i != data.closure_vars_accessed.end (); ++i)
9723 tree orig_decl = *i;
9724 tree closure_expr = DECL_VALUE_EXPR (orig_decl);
9726 if (TREE_CODE (TREE_TYPE (orig_decl)) == POINTER_TYPE
9727 || TREE_CODE (TREE_TYPE (orig_decl)) == REFERENCE_TYPE)
9729 /* this-pointer is processed above, outside this loop. */
9730 if (omp_target_this_expr
9731 && operand_equal_p (closure_expr, omp_target_this_expr))
9732 continue;
9734 bool ptr_p = TREE_CODE (TREE_TYPE (orig_decl)) == POINTER_TYPE;
9735 enum gomp_map_kind kind, ptr_kind, nc_kind;
9736 tree size;
9738 if (ptr_p)
9740 /* For pointers, default mapped as zero-length array
9741 section. */
9742 kind = GOMP_MAP_ALLOC;
9743 nc_kind = GOMP_MAP_FIRSTPRIVATE_POINTER;
9744 ptr_kind = GOMP_MAP_ATTACH_ZERO_LENGTH_ARRAY_SECTION;
9745 size = size_zero_node;
9747 else
9749 /* For references, default mapped as appearing on map
9750 clause. */
9751 kind = GOMP_MAP_TOFROM;
9752 nc_kind = GOMP_MAP_FIRSTPRIVATE_REFERENCE;
9753 ptr_kind = GOMP_MAP_ALWAYS_POINTER;
9754 size = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (closure_expr)));
9757 for (tree *p = clauses_ptr; *p; p = &OMP_CLAUSE_CHAIN (*p))
9758 if (OMP_CLAUSE_CODE (*p) == OMP_CLAUSE_MAP
9759 && (TREE_CODE (OMP_CLAUSE_DECL (*p)) == INDIRECT_REF
9760 || TREE_CODE (OMP_CLAUSE_DECL (*p)) == MEM_REF)
9761 && operand_equal_p (TREE_OPERAND (OMP_CLAUSE_DECL (*p), 0),
9762 orig_decl))
9764 /* If this was already specified by user as a map,
9765 save the user specified map kind, delete the
9766 "map(*ptr/ref), map(firstprivate ptr/ref)" sequence,
9767 and insert our own sequence:
9768 "map(*__closure->ptr/ref), map(<ptr_kind>:__closure->ref"
9770 tree nc = OMP_CLAUSE_CHAIN (*p);
9771 gcc_assert (nc != NULL_TREE
9772 && OMP_CLAUSE_CODE (nc) == OMP_CLAUSE_MAP
9773 && OMP_CLAUSE_MAP_KIND (nc) == nc_kind);
9774 /* Update with user specified kind and size. */
9775 kind = OMP_CLAUSE_MAP_KIND (*p);
9776 size = OMP_CLAUSE_SIZE (*p);
9777 *p = OMP_CLAUSE_CHAIN (nc);
9778 break;
9781 tree c = build_omp_clause (loc, OMP_CLAUSE_MAP);
9782 OMP_CLAUSE_SET_MAP_KIND (c, kind);
9783 OMP_CLAUSE_DECL (c)
9784 = build_indirect_ref (loc, closure_expr, RO_UNARY_STAR);
9785 OMP_CLAUSE_SIZE (c) = size;
9786 if (ptr_p)
9787 OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (c) = 1;
9788 new_clauses.safe_push (c);
9790 c = build_omp_clause (loc, OMP_CLAUSE_MAP);
9791 OMP_CLAUSE_SET_MAP_KIND (c, ptr_kind);
9792 OMP_CLAUSE_DECL (c) = closure_expr;
9793 OMP_CLAUSE_SIZE (c) = size_zero_node;
9794 new_clauses.safe_push (c);
9799 if (!data.ptr_members_accessed.is_empty ())
9800 for (hash_map<tree, tree>::iterator i = data.ptr_members_accessed.begin ();
9801 i != data.ptr_members_accessed.end (); ++i)
9803 /* For each referenced member that is of pointer or reference-to-pointer
9804 type, create the equivalent of map(alloc:this->ptr[:0]). */
9805 tree field_decl = (*i).first;
9806 tree ptr_member = (*i).second;
9808 for (tree c = *clauses_ptr; c; c = OMP_CLAUSE_CHAIN (c))
9810 /* If map(this->ptr[:N] already exists, avoid creating another
9811 such map. */
9812 tree decl = OMP_CLAUSE_DECL (c);
9813 if ((TREE_CODE (decl) == INDIRECT_REF
9814 || TREE_CODE (decl) == MEM_REF)
9815 && operand_equal_p (TREE_OPERAND (decl, 0), ptr_member))
9816 goto next_ptr_member;
9819 if (!cxx_mark_addressable (ptr_member))
9820 gcc_unreachable ();
9822 if (TREE_CODE (TREE_TYPE (field_decl)) == REFERENCE_TYPE)
9824 /* For reference to pointers, we need to map the referenced
9825 pointer first for things to be correct. */
9826 tree ptr_member_type = TREE_TYPE (ptr_member);
9828 /* Map pointer target as zero-length array section. */
9829 tree c = build_omp_clause (loc, OMP_CLAUSE_MAP);
9830 OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_ALLOC);
9831 OMP_CLAUSE_DECL (c)
9832 = build1 (INDIRECT_REF, TREE_TYPE (ptr_member_type), ptr_member);
9833 OMP_CLAUSE_SIZE (c) = size_zero_node;
9834 OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (c) = 1;
9836 /* Map pointer to zero-length array section. */
9837 tree c2 = build_omp_clause (loc, OMP_CLAUSE_MAP);
9838 OMP_CLAUSE_SET_MAP_KIND
9839 (c2, GOMP_MAP_POINTER_TO_ZERO_LENGTH_ARRAY_SECTION);
9840 OMP_CLAUSE_DECL (c2) = ptr_member;
9841 OMP_CLAUSE_SIZE (c2) = size_zero_node;
9843 /* Attach reference-to-pointer field to pointer. */
9844 tree c3 = build_omp_clause (loc, OMP_CLAUSE_MAP);
9845 OMP_CLAUSE_SET_MAP_KIND (c3, GOMP_MAP_ATTACH);
9846 OMP_CLAUSE_DECL (c3) = TREE_OPERAND (ptr_member, 0);
9847 OMP_CLAUSE_SIZE (c3) = size_zero_node;
9849 new_clauses.safe_push (c);
9850 new_clauses.safe_push (c2);
9851 new_clauses.safe_push (c3);
9853 else if (TREE_CODE (TREE_TYPE (field_decl)) == POINTER_TYPE)
9855 /* Map pointer target as zero-length array section. */
9856 tree c = build_omp_clause (loc, OMP_CLAUSE_MAP);
9857 OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_ALLOC);
9858 OMP_CLAUSE_DECL (c) = build_indirect_ref (loc, ptr_member,
9859 RO_UNARY_STAR);
9860 OMP_CLAUSE_SIZE (c) = size_zero_node;
9861 OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (c) = 1;
9863 /* Attach zero-length array section to pointer. */
9864 tree c2 = build_omp_clause (loc, OMP_CLAUSE_MAP);
9865 OMP_CLAUSE_SET_MAP_KIND
9866 (c2, GOMP_MAP_ATTACH_ZERO_LENGTH_ARRAY_SECTION);
9867 OMP_CLAUSE_DECL (c2) = ptr_member;
9868 OMP_CLAUSE_SIZE (c2) = size_zero_node;
9870 new_clauses.safe_push (c);
9871 new_clauses.safe_push (c2);
9873 else
9874 gcc_unreachable ();
9876 next_ptr_member:
9880 for (hash_set<tree>::iterator i = data.lambda_objects_accessed.begin ();
9881 i != data.lambda_objects_accessed.end (); ++i)
9883 tree lobj = *i;
9884 if (TREE_CODE (lobj) == TARGET_EXPR)
9885 lobj = TREE_OPERAND (lobj, 0);
9887 tree lt = TREE_TYPE (lobj);
9888 gcc_assert (LAMBDA_TYPE_P (lt) && CLASS_TYPE_P (lt));
9890 tree lc = build_omp_clause (loc, OMP_CLAUSE_MAP);
9891 OMP_CLAUSE_SET_MAP_KIND (lc, GOMP_MAP_TO);
9892 OMP_CLAUSE_DECL (lc) = lobj;
9893 OMP_CLAUSE_SIZE (lc) = TYPE_SIZE_UNIT (lt);
9894 new_clauses.safe_push (lc);
9896 for (tree fld = TYPE_FIELDS (lt); fld; fld = DECL_CHAIN (fld))
9898 if (TREE_CODE (TREE_TYPE (fld)) == POINTER_TYPE)
9900 tree exp = build3 (COMPONENT_REF, TREE_TYPE (fld),
9901 lobj, fld, NULL_TREE);
9902 tree c = build_omp_clause (loc, OMP_CLAUSE_MAP);
9903 OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_ALLOC);
9904 OMP_CLAUSE_DECL (c)
9905 = build_indirect_ref (loc, exp, RO_UNARY_STAR);
9906 OMP_CLAUSE_SIZE (c) = size_zero_node;
9907 OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (c) = 1;
9908 new_clauses.safe_push (c);
9910 c = build_omp_clause (loc, OMP_CLAUSE_MAP);
9911 OMP_CLAUSE_SET_MAP_KIND
9912 (c, GOMP_MAP_ATTACH_ZERO_LENGTH_ARRAY_SECTION);
9913 OMP_CLAUSE_DECL (c) = exp;
9914 OMP_CLAUSE_SIZE (c) = size_zero_node;
9915 new_clauses.safe_push (c);
9917 else if (TREE_CODE (TREE_TYPE (fld)) == REFERENCE_TYPE)
9919 tree exp = build3 (COMPONENT_REF, TREE_TYPE (fld),
9920 lobj, fld, NULL_TREE);
9921 tree c = build_omp_clause (loc, OMP_CLAUSE_MAP);
9922 OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_TOFROM);
9923 OMP_CLAUSE_DECL (c)
9924 = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (exp)), exp);
9925 OMP_CLAUSE_SIZE (c)
9926 = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (exp)));
9927 new_clauses.safe_push (c);
9929 c = build_omp_clause (loc, OMP_CLAUSE_MAP);
9930 OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_ALWAYS_POINTER);
9931 OMP_CLAUSE_DECL (c) = exp;
9932 OMP_CLAUSE_SIZE (c) = size_zero_node;
9933 new_clauses.safe_push (c);
9938 tree c = *clauses_ptr;
9939 for (int i = new_clauses.length () - 1; i >= 0; i--)
9941 OMP_CLAUSE_CHAIN (new_clauses[i]) = c;
9942 c = new_clauses[i];
9944 *clauses_ptr = c;
9947 /* Called from cp_parser_omp_target. Create additional implicit clauses for
9948 OpenMP target directives, and do sanity checks. */
9950 tree
9951 finish_omp_target (location_t loc, tree clauses, tree body, bool combined_p)
9953 if (!processing_template_decl)
9954 finish_omp_target_clauses (loc, body, &clauses);
9956 tree stmt = make_node (OMP_TARGET);
9957 TREE_TYPE (stmt) = void_type_node;
9958 OMP_TARGET_CLAUSES (stmt) = clauses;
9959 OMP_TARGET_BODY (stmt) = body;
9960 OMP_TARGET_COMBINED (stmt) = combined_p;
9961 SET_EXPR_LOCATION (stmt, loc);
9963 tree c = clauses;
9964 while (c)
9966 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP)
9967 switch (OMP_CLAUSE_MAP_KIND (c))
9969 case GOMP_MAP_TO:
9970 case GOMP_MAP_ALWAYS_TO:
9971 case GOMP_MAP_FROM:
9972 case GOMP_MAP_ALWAYS_FROM:
9973 case GOMP_MAP_TOFROM:
9974 case GOMP_MAP_ALWAYS_TOFROM:
9975 case GOMP_MAP_ALLOC:
9976 case GOMP_MAP_FIRSTPRIVATE_POINTER:
9977 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
9978 case GOMP_MAP_ALWAYS_POINTER:
9979 case GOMP_MAP_ATTACH_DETACH:
9980 case GOMP_MAP_ATTACH:
9981 case GOMP_MAP_ATTACH_ZERO_LENGTH_ARRAY_SECTION:
9982 case GOMP_MAP_POINTER_TO_ZERO_LENGTH_ARRAY_SECTION:
9983 break;
9984 default:
9985 error_at (OMP_CLAUSE_LOCATION (c),
9986 "%<#pragma omp target%> with map-type other "
9987 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
9988 "on %<map%> clause");
9989 break;
9991 c = OMP_CLAUSE_CHAIN (c);
9993 return add_stmt (stmt);
9996 tree
9997 finish_omp_parallel (tree clauses, tree body)
9999 tree stmt;
10001 body = finish_omp_structured_block (body);
10003 stmt = make_node (OMP_PARALLEL);
10004 TREE_TYPE (stmt) = void_type_node;
10005 OMP_PARALLEL_CLAUSES (stmt) = clauses;
10006 OMP_PARALLEL_BODY (stmt) = body;
10008 return add_stmt (stmt);
10011 tree
10012 begin_omp_task (void)
10014 keep_next_level (true);
10015 return begin_omp_structured_block ();
10018 tree
10019 finish_omp_task (tree clauses, tree body)
10021 tree stmt;
10023 body = finish_omp_structured_block (body);
10025 stmt = make_node (OMP_TASK);
10026 TREE_TYPE (stmt) = void_type_node;
10027 OMP_TASK_CLAUSES (stmt) = clauses;
10028 OMP_TASK_BODY (stmt) = body;
10030 return add_stmt (stmt);
10033 /* Helper function for finish_omp_for. Convert Ith random access iterator
10034 into integral iterator. Return FALSE if successful. */
10036 static bool
10037 handle_omp_for_class_iterator (int i, location_t locus, enum tree_code code,
10038 tree declv, tree orig_declv, tree initv,
10039 tree condv, tree incrv, tree *body,
10040 tree *pre_body, tree &clauses,
10041 int collapse, int ordered)
10043 tree diff, iter_init, iter_incr = NULL, last;
10044 tree incr_var = NULL, orig_pre_body, orig_body, c;
10045 tree decl = TREE_VEC_ELT (declv, i);
10046 tree init = TREE_VEC_ELT (initv, i);
10047 tree cond = TREE_VEC_ELT (condv, i);
10048 tree incr = TREE_VEC_ELT (incrv, i);
10049 tree iter = decl;
10050 location_t elocus = locus;
10052 if (init && EXPR_HAS_LOCATION (init))
10053 elocus = EXPR_LOCATION (init);
10055 switch (TREE_CODE (cond))
10057 case GT_EXPR:
10058 case GE_EXPR:
10059 case LT_EXPR:
10060 case LE_EXPR:
10061 case NE_EXPR:
10062 if (TREE_OPERAND (cond, 1) == iter)
10063 cond = build2 (swap_tree_comparison (TREE_CODE (cond)),
10064 TREE_TYPE (cond), iter, TREE_OPERAND (cond, 0));
10065 if (TREE_OPERAND (cond, 0) != iter)
10066 cond = error_mark_node;
10067 else
10069 tree tem = build_x_binary_op (EXPR_LOCATION (cond),
10070 TREE_CODE (cond),
10071 iter, ERROR_MARK,
10072 TREE_OPERAND (cond, 1), ERROR_MARK,
10073 NULL_TREE, NULL, tf_warning_or_error);
10074 if (error_operand_p (tem))
10075 return true;
10077 break;
10078 default:
10079 cond = error_mark_node;
10080 break;
10082 if (cond == error_mark_node)
10084 error_at (elocus, "invalid controlling predicate");
10085 return true;
10087 diff = build_x_binary_op (elocus, MINUS_EXPR,
10088 TREE_OPERAND (cond, 1), ERROR_MARK,
10089 iter, ERROR_MARK,
10090 NULL_TREE, NULL, tf_warning_or_error);
10091 diff = cp_fully_fold (diff);
10092 if (error_operand_p (diff))
10093 return true;
10094 if (TREE_CODE (TREE_TYPE (diff)) != INTEGER_TYPE)
10096 error_at (elocus, "difference between %qE and %qD does not have integer type",
10097 TREE_OPERAND (cond, 1), iter);
10098 return true;
10100 if (!c_omp_check_loop_iv_exprs (locus, code, orig_declv, i,
10101 TREE_VEC_ELT (declv, i), NULL_TREE,
10102 cond, cp_walk_subtrees))
10103 return true;
10105 switch (TREE_CODE (incr))
10107 case PREINCREMENT_EXPR:
10108 case PREDECREMENT_EXPR:
10109 case POSTINCREMENT_EXPR:
10110 case POSTDECREMENT_EXPR:
10111 if (TREE_OPERAND (incr, 0) != iter)
10113 incr = error_mark_node;
10114 break;
10116 iter_incr = build_x_unary_op (EXPR_LOCATION (incr),
10117 TREE_CODE (incr), iter,
10118 NULL_TREE, tf_warning_or_error);
10119 if (error_operand_p (iter_incr))
10120 return true;
10121 else if (TREE_CODE (incr) == PREINCREMENT_EXPR
10122 || TREE_CODE (incr) == POSTINCREMENT_EXPR)
10123 incr = integer_one_node;
10124 else
10125 incr = integer_minus_one_node;
10126 break;
10127 case MODIFY_EXPR:
10128 if (TREE_OPERAND (incr, 0) != iter)
10129 incr = error_mark_node;
10130 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
10131 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
10133 tree rhs = TREE_OPERAND (incr, 1);
10134 if (TREE_OPERAND (rhs, 0) == iter)
10136 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (rhs, 1)))
10137 != INTEGER_TYPE)
10138 incr = error_mark_node;
10139 else
10141 iter_incr = build_x_modify_expr (EXPR_LOCATION (rhs),
10142 iter, TREE_CODE (rhs),
10143 TREE_OPERAND (rhs, 1),
10144 NULL_TREE,
10145 tf_warning_or_error);
10146 if (error_operand_p (iter_incr))
10147 return true;
10148 incr = TREE_OPERAND (rhs, 1);
10149 incr = cp_convert (TREE_TYPE (diff), incr,
10150 tf_warning_or_error);
10151 if (TREE_CODE (rhs) == MINUS_EXPR)
10153 incr = build1 (NEGATE_EXPR, TREE_TYPE (diff), incr);
10154 incr = fold_simple (incr);
10156 if (TREE_CODE (incr) != INTEGER_CST
10157 && (TREE_CODE (incr) != NOP_EXPR
10158 || (TREE_CODE (TREE_OPERAND (incr, 0))
10159 != INTEGER_CST)))
10160 iter_incr = NULL;
10163 else if (TREE_OPERAND (rhs, 1) == iter)
10165 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (rhs, 0))) != INTEGER_TYPE
10166 || TREE_CODE (rhs) != PLUS_EXPR)
10167 incr = error_mark_node;
10168 else
10170 iter_incr = build_x_binary_op (EXPR_LOCATION (rhs),
10171 PLUS_EXPR,
10172 TREE_OPERAND (rhs, 0),
10173 ERROR_MARK, iter,
10174 ERROR_MARK, NULL_TREE, NULL,
10175 tf_warning_or_error);
10176 if (error_operand_p (iter_incr))
10177 return true;
10178 iter_incr = build_x_modify_expr (EXPR_LOCATION (rhs),
10179 iter, NOP_EXPR,
10180 iter_incr, NULL_TREE,
10181 tf_warning_or_error);
10182 if (error_operand_p (iter_incr))
10183 return true;
10184 incr = TREE_OPERAND (rhs, 0);
10185 iter_incr = NULL;
10188 else
10189 incr = error_mark_node;
10191 else
10192 incr = error_mark_node;
10193 break;
10194 default:
10195 incr = error_mark_node;
10196 break;
10199 if (incr == error_mark_node)
10201 error_at (elocus, "invalid increment expression");
10202 return true;
10205 incr = cp_convert (TREE_TYPE (diff), incr, tf_warning_or_error);
10206 incr = cp_fully_fold (incr);
10207 tree loop_iv_seen = NULL_TREE;
10208 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
10209 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
10210 && OMP_CLAUSE_DECL (c) == iter)
10212 if (code == OMP_TASKLOOP || code == OMP_LOOP)
10214 loop_iv_seen = c;
10215 OMP_CLAUSE_LASTPRIVATE_LOOP_IV (c) = 1;
10217 break;
10219 else if ((code == OMP_TASKLOOP || code == OMP_LOOP)
10220 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
10221 && OMP_CLAUSE_DECL (c) == iter)
10223 loop_iv_seen = c;
10224 if (code == OMP_TASKLOOP)
10225 OMP_CLAUSE_PRIVATE_TASKLOOP_IV (c) = 1;
10228 decl = create_temporary_var (TREE_TYPE (diff));
10229 pushdecl (decl);
10230 add_decl_expr (decl);
10231 last = create_temporary_var (TREE_TYPE (diff));
10232 pushdecl (last);
10233 add_decl_expr (last);
10234 if (c && iter_incr == NULL && TREE_CODE (incr) != INTEGER_CST
10235 && (!ordered || (i < collapse && collapse > 1)))
10237 incr_var = create_temporary_var (TREE_TYPE (diff));
10238 pushdecl (incr_var);
10239 add_decl_expr (incr_var);
10241 gcc_assert (stmts_are_full_exprs_p ());
10242 tree diffvar = NULL_TREE;
10243 if (code == OMP_TASKLOOP)
10245 if (!loop_iv_seen)
10247 tree ivc = build_omp_clause (locus, OMP_CLAUSE_FIRSTPRIVATE);
10248 OMP_CLAUSE_DECL (ivc) = iter;
10249 cxx_omp_finish_clause (ivc, NULL, false);
10250 OMP_CLAUSE_CHAIN (ivc) = clauses;
10251 clauses = ivc;
10253 tree lvc = build_omp_clause (locus, OMP_CLAUSE_FIRSTPRIVATE);
10254 OMP_CLAUSE_DECL (lvc) = last;
10255 OMP_CLAUSE_CHAIN (lvc) = clauses;
10256 clauses = lvc;
10257 diffvar = create_temporary_var (TREE_TYPE (diff));
10258 pushdecl (diffvar);
10259 add_decl_expr (diffvar);
10261 else if (code == OMP_LOOP)
10263 if (!loop_iv_seen)
10265 /* While iterators on the loop construct are predetermined
10266 lastprivate, if the decl is not declared inside of the
10267 loop, OMP_CLAUSE_LASTPRIVATE should have been added
10268 already. */
10269 loop_iv_seen = build_omp_clause (locus, OMP_CLAUSE_FIRSTPRIVATE);
10270 OMP_CLAUSE_DECL (loop_iv_seen) = iter;
10271 OMP_CLAUSE_CHAIN (loop_iv_seen) = clauses;
10272 clauses = loop_iv_seen;
10274 else if (OMP_CLAUSE_CODE (loop_iv_seen) == OMP_CLAUSE_PRIVATE)
10276 OMP_CLAUSE_PRIVATE_DEBUG (loop_iv_seen) = 0;
10277 OMP_CLAUSE_PRIVATE_OUTER_REF (loop_iv_seen) = 0;
10278 OMP_CLAUSE_CODE (loop_iv_seen) = OMP_CLAUSE_FIRSTPRIVATE;
10280 if (OMP_CLAUSE_CODE (loop_iv_seen) == OMP_CLAUSE_FIRSTPRIVATE)
10281 cxx_omp_finish_clause (loop_iv_seen, NULL, false);
10284 orig_pre_body = *pre_body;
10285 *pre_body = push_stmt_list ();
10286 if (orig_pre_body)
10287 add_stmt (orig_pre_body);
10288 if (init != NULL)
10289 finish_expr_stmt (build_x_modify_expr (elocus,
10290 iter, NOP_EXPR, init,
10291 NULL_TREE, tf_warning_or_error));
10292 init = build_int_cst (TREE_TYPE (diff), 0);
10293 if (c && iter_incr == NULL
10294 && (!ordered || (i < collapse && collapse > 1)))
10296 if (incr_var)
10298 finish_expr_stmt (build_x_modify_expr (elocus,
10299 incr_var, NOP_EXPR,
10300 incr, NULL_TREE,
10301 tf_warning_or_error));
10302 incr = incr_var;
10304 iter_incr = build_x_modify_expr (elocus,
10305 iter, PLUS_EXPR, incr,
10306 NULL_TREE, tf_warning_or_error);
10308 if (c && ordered && i < collapse && collapse > 1)
10309 iter_incr = incr;
10310 finish_expr_stmt (build_x_modify_expr (elocus,
10311 last, NOP_EXPR, init,
10312 NULL_TREE, tf_warning_or_error));
10313 if (diffvar)
10315 finish_expr_stmt (build_x_modify_expr (elocus,
10316 diffvar, NOP_EXPR,
10317 diff, NULL_TREE, tf_warning_or_error));
10318 diff = diffvar;
10320 *pre_body = pop_stmt_list (*pre_body);
10322 cond = cp_build_binary_op (elocus,
10323 TREE_CODE (cond), decl, diff,
10324 tf_warning_or_error);
10325 incr = build_modify_expr (elocus, decl, NULL_TREE, PLUS_EXPR,
10326 elocus, incr, NULL_TREE);
10328 orig_body = *body;
10329 *body = push_stmt_list ();
10330 iter_init = build2 (MINUS_EXPR, TREE_TYPE (diff), decl, last);
10331 iter_init = build_x_modify_expr (elocus,
10332 iter, PLUS_EXPR, iter_init,
10333 NULL_TREE, tf_warning_or_error);
10334 if (iter_init != error_mark_node)
10335 iter_init = build1 (NOP_EXPR, void_type_node, iter_init);
10336 finish_expr_stmt (iter_init);
10337 finish_expr_stmt (build_x_modify_expr (elocus,
10338 last, NOP_EXPR, decl,
10339 NULL_TREE, tf_warning_or_error));
10340 add_stmt (orig_body);
10341 *body = pop_stmt_list (*body);
10343 if (c)
10345 OMP_CLAUSE_LASTPRIVATE_STMT (c) = push_stmt_list ();
10346 if (!ordered)
10347 finish_expr_stmt (iter_incr);
10348 else
10350 iter_init = decl;
10351 if (i < collapse && collapse > 1 && !error_operand_p (iter_incr))
10352 iter_init = build2 (PLUS_EXPR, TREE_TYPE (diff),
10353 iter_init, iter_incr);
10354 iter_init = build2 (MINUS_EXPR, TREE_TYPE (diff), iter_init, last);
10355 iter_init = build_x_modify_expr (elocus,
10356 iter, PLUS_EXPR, iter_init,
10357 NULL_TREE, tf_warning_or_error);
10358 if (iter_init != error_mark_node)
10359 iter_init = build1 (NOP_EXPR, void_type_node, iter_init);
10360 finish_expr_stmt (iter_init);
10362 OMP_CLAUSE_LASTPRIVATE_STMT (c)
10363 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (c));
10366 if (TREE_CODE (TREE_VEC_ELT (orig_declv, i)) == TREE_LIST)
10368 tree t = TREE_VEC_ELT (orig_declv, i);
10369 gcc_assert (TREE_PURPOSE (t) == NULL_TREE
10370 && TREE_VALUE (t) == NULL_TREE
10371 && TREE_CODE (TREE_CHAIN (t)) == TREE_VEC);
10372 TREE_PURPOSE (t) = TREE_VEC_ELT (declv, i);
10373 TREE_VALUE (t) = last;
10375 else
10376 TREE_VEC_ELT (orig_declv, i)
10377 = tree_cons (TREE_VEC_ELT (declv, i), last, NULL_TREE);
10378 TREE_VEC_ELT (declv, i) = decl;
10379 TREE_VEC_ELT (initv, i) = init;
10380 TREE_VEC_ELT (condv, i) = cond;
10381 TREE_VEC_ELT (incrv, i) = incr;
10383 return false;
10386 /* Build and validate an OMP_FOR statement. CLAUSES, BODY, COND, INCR
10387 are directly for their associated operands in the statement. DECL
10388 and INIT are a combo; if DECL is NULL then INIT ought to be a
10389 MODIFY_EXPR, and the DECL should be extracted. PRE_BODY are
10390 optional statements that need to go before the loop into its
10391 sk_omp scope. */
10393 tree
10394 finish_omp_for (location_t locus, enum tree_code code, tree declv,
10395 tree orig_declv, tree initv, tree condv, tree incrv,
10396 tree body, tree pre_body, vec<tree> *orig_inits, tree clauses)
10398 tree omp_for = NULL, orig_incr = NULL;
10399 tree decl = NULL, init, cond, incr;
10400 location_t elocus;
10401 int i;
10402 int collapse = 1;
10403 int ordered = 0;
10405 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (initv));
10406 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (condv));
10407 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (incrv));
10408 if (TREE_VEC_LENGTH (declv) > 1)
10410 tree c;
10412 c = omp_find_clause (clauses, OMP_CLAUSE_TILE);
10413 if (c)
10414 collapse = list_length (OMP_CLAUSE_TILE_LIST (c));
10415 else
10417 c = omp_find_clause (clauses, OMP_CLAUSE_COLLAPSE);
10418 if (c)
10419 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (c));
10420 if (collapse != TREE_VEC_LENGTH (declv))
10421 ordered = TREE_VEC_LENGTH (declv);
10424 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
10426 decl = TREE_VEC_ELT (declv, i);
10427 init = TREE_VEC_ELT (initv, i);
10428 cond = TREE_VEC_ELT (condv, i);
10429 incr = TREE_VEC_ELT (incrv, i);
10430 elocus = locus;
10432 if (decl == NULL)
10434 if (init != NULL)
10435 switch (TREE_CODE (init))
10437 case MODIFY_EXPR:
10438 decl = TREE_OPERAND (init, 0);
10439 init = TREE_OPERAND (init, 1);
10440 break;
10441 case MODOP_EXPR:
10442 if (TREE_CODE (TREE_OPERAND (init, 1)) == NOP_EXPR)
10444 decl = TREE_OPERAND (init, 0);
10445 init = TREE_OPERAND (init, 2);
10447 break;
10448 default:
10449 break;
10452 if (decl == NULL)
10454 error_at (locus,
10455 "expected iteration declaration or initialization");
10456 return NULL;
10460 if (init && EXPR_HAS_LOCATION (init))
10461 elocus = EXPR_LOCATION (init);
10463 if (cond == global_namespace)
10464 continue;
10466 if (cond == NULL)
10468 error_at (elocus, "missing controlling predicate");
10469 return NULL;
10472 if (incr == NULL)
10474 error_at (elocus, "missing increment expression");
10475 return NULL;
10478 TREE_VEC_ELT (declv, i) = decl;
10479 TREE_VEC_ELT (initv, i) = init;
10482 if (orig_inits)
10484 bool fail = false;
10485 tree orig_init;
10486 FOR_EACH_VEC_ELT (*orig_inits, i, orig_init)
10487 if (orig_init
10488 && !c_omp_check_loop_iv_exprs (locus, code,
10489 orig_declv ? orig_declv : declv, i,
10490 TREE_VEC_ELT (declv, i), orig_init,
10491 NULL_TREE, cp_walk_subtrees))
10492 fail = true;
10493 if (fail)
10494 return NULL;
10497 if (dependent_omp_for_p (declv, initv, condv, incrv))
10499 tree stmt;
10501 stmt = make_node (code);
10503 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
10505 /* This is really just a place-holder. We'll be decomposing this
10506 again and going through the cp_build_modify_expr path below when
10507 we instantiate the thing. */
10508 TREE_VEC_ELT (initv, i)
10509 = build2 (MODIFY_EXPR, void_type_node, TREE_VEC_ELT (declv, i),
10510 TREE_VEC_ELT (initv, i));
10513 TREE_TYPE (stmt) = void_type_node;
10514 OMP_FOR_INIT (stmt) = initv;
10515 OMP_FOR_COND (stmt) = condv;
10516 OMP_FOR_INCR (stmt) = incrv;
10517 OMP_FOR_BODY (stmt) = body;
10518 OMP_FOR_PRE_BODY (stmt) = pre_body;
10519 OMP_FOR_CLAUSES (stmt) = clauses;
10521 SET_EXPR_LOCATION (stmt, locus);
10522 return add_stmt (stmt);
10525 if (!orig_declv)
10526 orig_declv = copy_node (declv);
10528 if (processing_template_decl)
10529 orig_incr = make_tree_vec (TREE_VEC_LENGTH (incrv));
10531 for (i = 0; i < TREE_VEC_LENGTH (declv); )
10533 decl = TREE_VEC_ELT (declv, i);
10534 init = TREE_VEC_ELT (initv, i);
10535 cond = TREE_VEC_ELT (condv, i);
10536 incr = TREE_VEC_ELT (incrv, i);
10537 if (orig_incr)
10538 TREE_VEC_ELT (orig_incr, i) = incr;
10539 elocus = locus;
10541 if (init && EXPR_HAS_LOCATION (init))
10542 elocus = EXPR_LOCATION (init);
10544 if (!DECL_P (decl))
10546 error_at (elocus, "expected iteration declaration or initialization");
10547 return NULL;
10550 if (incr && TREE_CODE (incr) == MODOP_EXPR)
10552 if (orig_incr)
10553 TREE_VEC_ELT (orig_incr, i) = incr;
10554 incr = cp_build_modify_expr (elocus, TREE_OPERAND (incr, 0),
10555 TREE_CODE (TREE_OPERAND (incr, 1)),
10556 TREE_OPERAND (incr, 2),
10557 tf_warning_or_error);
10560 if (CLASS_TYPE_P (TREE_TYPE (decl)))
10562 if (code == OMP_SIMD)
10564 error_at (elocus, "%<#pragma omp simd%> used with class "
10565 "iteration variable %qE", decl);
10566 return NULL;
10568 if (handle_omp_for_class_iterator (i, locus, code, declv, orig_declv,
10569 initv, condv, incrv, &body,
10570 &pre_body, clauses,
10571 collapse, ordered))
10572 return NULL;
10573 continue;
10576 if (!INTEGRAL_TYPE_P (TREE_TYPE (decl))
10577 && !TYPE_PTR_P (TREE_TYPE (decl)))
10579 error_at (elocus, "invalid type for iteration variable %qE", decl);
10580 return NULL;
10583 if (!processing_template_decl && TREE_CODE (init) != TREE_VEC)
10584 init = cp_build_modify_expr (elocus, decl, NOP_EXPR, init,
10585 tf_warning_or_error);
10586 else
10587 init = build2 (MODIFY_EXPR, void_type_node, decl, init);
10588 if (decl == error_mark_node || init == error_mark_node)
10589 return NULL;
10591 TREE_VEC_ELT (declv, i) = decl;
10592 TREE_VEC_ELT (initv, i) = init;
10593 TREE_VEC_ELT (condv, i) = cond;
10594 TREE_VEC_ELT (incrv, i) = incr;
10595 i++;
10598 if (pre_body && IS_EMPTY_STMT (pre_body))
10599 pre_body = NULL;
10601 omp_for = c_finish_omp_for (locus, code, declv, orig_declv, initv, condv,
10602 incrv, body, pre_body,
10603 !processing_template_decl);
10605 /* Check for iterators appearing in lb, b or incr expressions. */
10606 if (omp_for && !c_omp_check_loop_iv (omp_for, orig_declv, cp_walk_subtrees))
10607 omp_for = NULL_TREE;
10609 if (omp_for == NULL)
10610 return NULL;
10612 add_stmt (omp_for);
10614 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INCR (omp_for)); i++)
10616 init = TREE_VEC_ELT (OMP_FOR_INIT (omp_for), i);
10617 decl = TREE_OPERAND (init, 0);
10618 cond = TREE_VEC_ELT (OMP_FOR_COND (omp_for), i);
10619 incr = TREE_VEC_ELT (OMP_FOR_INCR (omp_for), i);
10621 if (!processing_template_decl)
10623 if (TREE_CODE (TREE_OPERAND (init, 1)) == TREE_VEC)
10625 tree t = TREE_VEC_ELT (TREE_OPERAND (init, 1), 1);
10626 TREE_VEC_ELT (TREE_OPERAND (init, 1), 1)
10627 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
10628 t = TREE_VEC_ELT (TREE_OPERAND (init, 1), 2);
10629 TREE_VEC_ELT (TREE_OPERAND (init, 1), 2)
10630 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
10632 else
10634 tree t = TREE_OPERAND (init, 1);
10635 TREE_OPERAND (init, 1)
10636 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
10638 if (TREE_CODE (TREE_OPERAND (cond, 1)) == TREE_VEC)
10640 tree t = TREE_VEC_ELT (TREE_OPERAND (cond, 1), 1);
10641 TREE_VEC_ELT (TREE_OPERAND (cond, 1), 1)
10642 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
10643 t = TREE_VEC_ELT (TREE_OPERAND (cond, 1), 2);
10644 TREE_VEC_ELT (TREE_OPERAND (cond, 1), 2)
10645 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
10647 else
10649 tree t = TREE_OPERAND (cond, 1);
10650 TREE_OPERAND (cond, 1)
10651 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
10655 if (TREE_CODE (incr) != MODIFY_EXPR)
10656 continue;
10658 if (TREE_SIDE_EFFECTS (TREE_OPERAND (incr, 1))
10659 && BINARY_CLASS_P (TREE_OPERAND (incr, 1))
10660 && !processing_template_decl)
10662 tree t = TREE_OPERAND (TREE_OPERAND (incr, 1), 0);
10663 if (TREE_SIDE_EFFECTS (t)
10664 && t != decl
10665 && (TREE_CODE (t) != NOP_EXPR
10666 || TREE_OPERAND (t, 0) != decl))
10667 TREE_OPERAND (TREE_OPERAND (incr, 1), 0)
10668 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
10670 t = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
10671 if (TREE_SIDE_EFFECTS (t)
10672 && t != decl
10673 && (TREE_CODE (t) != NOP_EXPR
10674 || TREE_OPERAND (t, 0) != decl))
10675 TREE_OPERAND (TREE_OPERAND (incr, 1), 1)
10676 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
10679 if (orig_incr)
10680 TREE_VEC_ELT (OMP_FOR_INCR (omp_for), i) = TREE_VEC_ELT (orig_incr, i);
10682 OMP_FOR_CLAUSES (omp_for) = clauses;
10684 /* For simd loops with non-static data member iterators, we could have added
10685 OMP_CLAUSE_LINEAR clauses without OMP_CLAUSE_LINEAR_STEP. As we know the
10686 step at this point, fill it in. */
10687 if (code == OMP_SIMD && !processing_template_decl
10688 && TREE_VEC_LENGTH (OMP_FOR_INCR (omp_for)) == 1)
10689 for (tree c = omp_find_clause (clauses, OMP_CLAUSE_LINEAR); c;
10690 c = omp_find_clause (OMP_CLAUSE_CHAIN (c), OMP_CLAUSE_LINEAR))
10691 if (OMP_CLAUSE_LINEAR_STEP (c) == NULL_TREE)
10693 decl = TREE_OPERAND (TREE_VEC_ELT (OMP_FOR_INIT (omp_for), 0), 0);
10694 gcc_assert (decl == OMP_CLAUSE_DECL (c));
10695 incr = TREE_VEC_ELT (OMP_FOR_INCR (omp_for), 0);
10696 tree step, stept;
10697 switch (TREE_CODE (incr))
10699 case PREINCREMENT_EXPR:
10700 case POSTINCREMENT_EXPR:
10701 /* c_omp_for_incr_canonicalize_ptr() should have been
10702 called to massage things appropriately. */
10703 gcc_assert (!INDIRECT_TYPE_P (TREE_TYPE (decl)));
10704 OMP_CLAUSE_LINEAR_STEP (c) = build_int_cst (TREE_TYPE (decl), 1);
10705 break;
10706 case PREDECREMENT_EXPR:
10707 case POSTDECREMENT_EXPR:
10708 /* c_omp_for_incr_canonicalize_ptr() should have been
10709 called to massage things appropriately. */
10710 gcc_assert (!INDIRECT_TYPE_P (TREE_TYPE (decl)));
10711 OMP_CLAUSE_LINEAR_STEP (c)
10712 = build_int_cst (TREE_TYPE (decl), -1);
10713 break;
10714 case MODIFY_EXPR:
10715 gcc_assert (TREE_OPERAND (incr, 0) == decl);
10716 incr = TREE_OPERAND (incr, 1);
10717 switch (TREE_CODE (incr))
10719 case PLUS_EXPR:
10720 if (TREE_OPERAND (incr, 1) == decl)
10721 step = TREE_OPERAND (incr, 0);
10722 else
10723 step = TREE_OPERAND (incr, 1);
10724 break;
10725 case MINUS_EXPR:
10726 case POINTER_PLUS_EXPR:
10727 gcc_assert (TREE_OPERAND (incr, 0) == decl);
10728 step = TREE_OPERAND (incr, 1);
10729 break;
10730 default:
10731 gcc_unreachable ();
10733 stept = TREE_TYPE (decl);
10734 if (INDIRECT_TYPE_P (stept))
10735 stept = sizetype;
10736 step = fold_convert (stept, step);
10737 if (TREE_CODE (incr) == MINUS_EXPR)
10738 step = fold_build1 (NEGATE_EXPR, stept, step);
10739 OMP_CLAUSE_LINEAR_STEP (c) = step;
10740 break;
10741 default:
10742 gcc_unreachable ();
10745 /* Override saved methods on OMP_LOOP's OMP_CLAUSE_LASTPRIVATE_LOOP_IV
10746 clauses, we need copy ctor for those rather than default ctor,
10747 plus as for other lastprivates assignment op and dtor. */
10748 if (code == OMP_LOOP && !processing_template_decl)
10749 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
10750 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
10751 && OMP_CLAUSE_LASTPRIVATE_LOOP_IV (c)
10752 && cxx_omp_create_clause_info (c, TREE_TYPE (OMP_CLAUSE_DECL (c)),
10753 false, true, true, true))
10754 CP_OMP_CLAUSE_INFO (c) = NULL_TREE;
10756 return omp_for;
10759 /* Fix up range for decls. Those decls were pushed into BIND's BIND_EXPR_VARS
10760 and need to be moved into the BIND_EXPR inside of the OMP_FOR's body. */
10762 tree
10763 finish_omp_for_block (tree bind, tree omp_for)
10765 if (omp_for == NULL_TREE
10766 || !OMP_FOR_ORIG_DECLS (omp_for)
10767 || bind == NULL_TREE
10768 || TREE_CODE (bind) != BIND_EXPR)
10769 return bind;
10770 tree b = NULL_TREE;
10771 for (int i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (omp_for)); i++)
10772 if (TREE_CODE (TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (omp_for), i)) == TREE_LIST
10773 && TREE_CHAIN (TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (omp_for), i)))
10775 tree v = TREE_CHAIN (TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (omp_for), i));
10776 gcc_assert (BIND_EXPR_BLOCK (bind)
10777 && (BIND_EXPR_VARS (bind)
10778 == BLOCK_VARS (BIND_EXPR_BLOCK (bind))));
10779 for (int j = 2; j < TREE_VEC_LENGTH (v); j++)
10780 for (tree *p = &BIND_EXPR_VARS (bind); *p; p = &DECL_CHAIN (*p))
10782 if (*p == TREE_VEC_ELT (v, j))
10784 tree var = *p;
10785 *p = DECL_CHAIN (*p);
10786 if (b == NULL_TREE)
10788 b = make_node (BLOCK);
10789 b = build3 (BIND_EXPR, void_type_node, NULL_TREE,
10790 OMP_FOR_BODY (omp_for), b);
10791 TREE_SIDE_EFFECTS (b) = 1;
10792 OMP_FOR_BODY (omp_for) = b;
10794 DECL_CHAIN (var) = BIND_EXPR_VARS (b);
10795 BIND_EXPR_VARS (b) = var;
10796 BLOCK_VARS (BIND_EXPR_BLOCK (b)) = var;
10799 BLOCK_VARS (BIND_EXPR_BLOCK (bind)) = BIND_EXPR_VARS (bind);
10801 return bind;
10804 void
10805 finish_omp_atomic (location_t loc, enum tree_code code, enum tree_code opcode,
10806 tree lhs, tree rhs, tree v, tree lhs1, tree rhs1, tree r,
10807 tree clauses, enum omp_memory_order mo, bool weak)
10809 tree orig_lhs;
10810 tree orig_rhs;
10811 tree orig_v;
10812 tree orig_lhs1;
10813 tree orig_rhs1;
10814 tree orig_r;
10815 bool dependent_p;
10816 tree stmt;
10818 orig_lhs = lhs;
10819 orig_rhs = rhs;
10820 orig_v = v;
10821 orig_lhs1 = lhs1;
10822 orig_rhs1 = rhs1;
10823 orig_r = r;
10824 dependent_p = false;
10825 stmt = NULL_TREE;
10827 /* Even in a template, we can detect invalid uses of the atomic
10828 pragma if neither LHS nor RHS is type-dependent. */
10829 if (processing_template_decl)
10831 dependent_p = (type_dependent_expression_p (lhs)
10832 || (rhs && type_dependent_expression_p (rhs))
10833 || (v && type_dependent_expression_p (v))
10834 || (lhs1 && type_dependent_expression_p (lhs1))
10835 || (rhs1 && type_dependent_expression_p (rhs1))
10836 || (r
10837 && r != void_list_node
10838 && type_dependent_expression_p (r)));
10839 if (clauses)
10841 gcc_assert (TREE_CODE (clauses) == OMP_CLAUSE
10842 && OMP_CLAUSE_CODE (clauses) == OMP_CLAUSE_HINT
10843 && OMP_CLAUSE_CHAIN (clauses) == NULL_TREE);
10844 if (type_dependent_expression_p (OMP_CLAUSE_HINT_EXPR (clauses))
10845 || TREE_CODE (OMP_CLAUSE_HINT_EXPR (clauses)) != INTEGER_CST)
10846 dependent_p = true;
10848 if (!dependent_p)
10850 lhs = build_non_dependent_expr (lhs);
10851 if (rhs)
10852 rhs = build_non_dependent_expr (rhs);
10853 if (v)
10854 v = build_non_dependent_expr (v);
10855 if (lhs1)
10856 lhs1 = build_non_dependent_expr (lhs1);
10857 if (rhs1)
10858 rhs1 = build_non_dependent_expr (rhs1);
10859 if (r && r != void_list_node)
10860 r = build_non_dependent_expr (r);
10863 if (!dependent_p)
10865 bool swapped = false;
10866 if (rhs1 && opcode != COND_EXPR && cp_tree_equal (lhs, rhs))
10868 std::swap (rhs, rhs1);
10869 swapped = !commutative_tree_code (opcode);
10871 if (rhs1 && opcode != COND_EXPR && !cp_tree_equal (lhs, rhs1))
10873 if (code == OMP_ATOMIC)
10874 error ("%<#pragma omp atomic update%> uses two different "
10875 "expressions for memory");
10876 else
10877 error ("%<#pragma omp atomic capture%> uses two different "
10878 "expressions for memory");
10879 return;
10881 if (lhs1 && !cp_tree_equal (lhs, lhs1))
10883 if (code == OMP_ATOMIC)
10884 error ("%<#pragma omp atomic update%> uses two different "
10885 "expressions for memory");
10886 else
10887 error ("%<#pragma omp atomic capture%> uses two different "
10888 "expressions for memory");
10889 return;
10891 stmt = c_finish_omp_atomic (loc, code, opcode, lhs, rhs,
10892 v, lhs1, rhs1, r, swapped, mo, weak,
10893 processing_template_decl != 0);
10894 if (stmt == error_mark_node)
10895 return;
10897 if (processing_template_decl)
10899 if (code == OMP_ATOMIC_READ)
10901 stmt = build_min_nt_loc (loc, OMP_ATOMIC_READ, orig_lhs);
10902 OMP_ATOMIC_MEMORY_ORDER (stmt) = mo;
10903 stmt = build2 (MODIFY_EXPR, void_type_node, orig_v, stmt);
10905 else
10907 if (opcode == NOP_EXPR)
10908 stmt = build2 (MODIFY_EXPR, void_type_node, orig_lhs, orig_rhs);
10909 else if (opcode == COND_EXPR)
10911 stmt = build2 (EQ_EXPR, boolean_type_node, orig_lhs, orig_rhs);
10912 if (orig_r)
10913 stmt = build2 (MODIFY_EXPR, boolean_type_node, orig_r,
10914 stmt);
10915 stmt = build3 (COND_EXPR, void_type_node, stmt, orig_rhs1,
10916 orig_lhs);
10917 orig_rhs1 = NULL_TREE;
10919 else
10920 stmt = build2 (opcode, void_type_node, orig_lhs, orig_rhs);
10921 if (orig_rhs1)
10922 stmt = build_min_nt_loc (EXPR_LOCATION (orig_rhs1),
10923 COMPOUND_EXPR, orig_rhs1, stmt);
10924 if (code != OMP_ATOMIC)
10926 stmt = build_min_nt_loc (loc, code, orig_lhs1, stmt);
10927 OMP_ATOMIC_MEMORY_ORDER (stmt) = mo;
10928 OMP_ATOMIC_WEAK (stmt) = weak;
10929 stmt = build2 (MODIFY_EXPR, void_type_node, orig_v, stmt);
10932 stmt = build2 (OMP_ATOMIC, void_type_node,
10933 clauses ? clauses : integer_zero_node, stmt);
10934 OMP_ATOMIC_MEMORY_ORDER (stmt) = mo;
10935 OMP_ATOMIC_WEAK (stmt) = weak;
10936 SET_EXPR_LOCATION (stmt, loc);
10939 /* Avoid -Wunused-value warnings here, the whole construct has side-effects
10940 and even if it might be wrapped from fold-const.cc or c-omp.cc wrapped
10941 in some tree that appears to be unused, the value is not unused. */
10942 warning_sentinel w (warn_unused_value);
10943 finish_expr_stmt (stmt);
10946 void
10947 finish_omp_barrier (void)
10949 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_BARRIER);
10950 releasing_vec vec;
10951 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
10952 finish_expr_stmt (stmt);
10955 void
10956 finish_omp_depobj (location_t loc, tree depobj,
10957 enum omp_clause_depend_kind kind, tree clause)
10959 if (!error_operand_p (depobj) && !type_dependent_expression_p (depobj))
10961 if (!lvalue_p (depobj))
10963 error_at (EXPR_LOC_OR_LOC (depobj, loc),
10964 "%<depobj%> expression is not lvalue expression");
10965 depobj = error_mark_node;
10969 if (processing_template_decl)
10971 if (clause == NULL_TREE)
10972 clause = build_int_cst (integer_type_node, kind);
10973 add_stmt (build_min_nt_loc (loc, OMP_DEPOBJ, depobj, clause));
10974 return;
10977 if (!error_operand_p (depobj))
10979 tree addr = cp_build_addr_expr (depobj, tf_warning_or_error);
10980 if (addr == error_mark_node)
10981 depobj = error_mark_node;
10982 else
10983 depobj = cp_build_indirect_ref (loc, addr, RO_UNARY_STAR,
10984 tf_warning_or_error);
10987 c_finish_omp_depobj (loc, depobj, kind, clause);
10990 void
10991 finish_omp_flush (int mo)
10993 tree fn = builtin_decl_explicit (BUILT_IN_SYNC_SYNCHRONIZE);
10994 releasing_vec vec;
10995 if (mo != MEMMODEL_LAST && mo != MEMMODEL_SEQ_CST)
10997 fn = builtin_decl_explicit (BUILT_IN_ATOMIC_THREAD_FENCE);
10998 vec->quick_push (build_int_cst (integer_type_node, mo));
11000 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
11001 finish_expr_stmt (stmt);
11004 void
11005 finish_omp_taskwait (void)
11007 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_TASKWAIT);
11008 releasing_vec vec;
11009 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
11010 finish_expr_stmt (stmt);
11013 void
11014 finish_omp_taskyield (void)
11016 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_TASKYIELD);
11017 releasing_vec vec;
11018 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
11019 finish_expr_stmt (stmt);
11022 void
11023 finish_omp_cancel (tree clauses)
11025 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
11026 int mask = 0;
11027 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
11028 mask = 1;
11029 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
11030 mask = 2;
11031 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
11032 mask = 4;
11033 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
11034 mask = 8;
11035 else
11037 error ("%<#pragma omp cancel%> must specify one of "
11038 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> clauses");
11039 return;
11041 releasing_vec vec;
11042 tree ifc = omp_find_clause (clauses, OMP_CLAUSE_IF);
11043 if (ifc != NULL_TREE)
11045 if (OMP_CLAUSE_IF_MODIFIER (ifc) != ERROR_MARK
11046 && OMP_CLAUSE_IF_MODIFIER (ifc) != VOID_CST)
11047 error_at (OMP_CLAUSE_LOCATION (ifc),
11048 "expected %<cancel%> %<if%> clause modifier");
11049 else
11051 tree ifc2 = omp_find_clause (OMP_CLAUSE_CHAIN (ifc), OMP_CLAUSE_IF);
11052 if (ifc2 != NULL_TREE)
11054 gcc_assert (OMP_CLAUSE_IF_MODIFIER (ifc) == VOID_CST
11055 && OMP_CLAUSE_IF_MODIFIER (ifc2) != ERROR_MARK
11056 && OMP_CLAUSE_IF_MODIFIER (ifc2) != VOID_CST);
11057 error_at (OMP_CLAUSE_LOCATION (ifc2),
11058 "expected %<cancel%> %<if%> clause modifier");
11062 if (!processing_template_decl)
11063 ifc = maybe_convert_cond (OMP_CLAUSE_IF_EXPR (ifc));
11064 else
11065 ifc = build_x_binary_op (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
11066 OMP_CLAUSE_IF_EXPR (ifc), ERROR_MARK,
11067 integer_zero_node, ERROR_MARK,
11068 NULL_TREE, NULL, tf_warning_or_error);
11070 else
11071 ifc = boolean_true_node;
11072 vec->quick_push (build_int_cst (integer_type_node, mask));
11073 vec->quick_push (ifc);
11074 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
11075 finish_expr_stmt (stmt);
11078 void
11079 finish_omp_cancellation_point (tree clauses)
11081 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
11082 int mask = 0;
11083 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
11084 mask = 1;
11085 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
11086 mask = 2;
11087 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
11088 mask = 4;
11089 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
11090 mask = 8;
11091 else
11093 error ("%<#pragma omp cancellation point%> must specify one of "
11094 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> clauses");
11095 return;
11097 releasing_vec vec
11098 = make_tree_vector_single (build_int_cst (integer_type_node, mask));
11099 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
11100 finish_expr_stmt (stmt);
11103 /* Begin a __transaction_atomic or __transaction_relaxed statement.
11104 If PCOMPOUND is non-null, this is for a function-transaction-block, and we
11105 should create an extra compound stmt. */
11107 tree
11108 begin_transaction_stmt (location_t loc, tree *pcompound, int flags)
11110 tree r;
11112 if (pcompound)
11113 *pcompound = begin_compound_stmt (0);
11115 r = build_stmt (loc, TRANSACTION_EXPR, NULL_TREE);
11117 /* Only add the statement to the function if support enabled. */
11118 if (flag_tm)
11119 add_stmt (r);
11120 else
11121 error_at (loc, ((flags & TM_STMT_ATTR_RELAXED) != 0
11122 ? G_("%<__transaction_relaxed%> without "
11123 "transactional memory support enabled")
11124 : G_("%<__transaction_atomic%> without "
11125 "transactional memory support enabled")));
11127 TRANSACTION_EXPR_BODY (r) = push_stmt_list ();
11128 TREE_SIDE_EFFECTS (r) = 1;
11129 return r;
11132 /* End a __transaction_atomic or __transaction_relaxed statement.
11133 If COMPOUND_STMT is non-null, this is for a function-transaction-block,
11134 and we should end the compound. If NOEX is non-NULL, we wrap the body in
11135 a MUST_NOT_THROW_EXPR with NOEX as condition. */
11137 void
11138 finish_transaction_stmt (tree stmt, tree compound_stmt, int flags, tree noex)
11140 TRANSACTION_EXPR_BODY (stmt) = pop_stmt_list (TRANSACTION_EXPR_BODY (stmt));
11141 TRANSACTION_EXPR_OUTER (stmt) = (flags & TM_STMT_ATTR_OUTER) != 0;
11142 TRANSACTION_EXPR_RELAXED (stmt) = (flags & TM_STMT_ATTR_RELAXED) != 0;
11143 TRANSACTION_EXPR_IS_STMT (stmt) = 1;
11145 /* noexcept specifications are not allowed for function transactions. */
11146 gcc_assert (!(noex && compound_stmt));
11147 if (noex)
11149 tree body = build_must_not_throw_expr (TRANSACTION_EXPR_BODY (stmt),
11150 noex);
11151 protected_set_expr_location
11152 (body, EXPR_LOCATION (TRANSACTION_EXPR_BODY (stmt)));
11153 TREE_SIDE_EFFECTS (body) = 1;
11154 TRANSACTION_EXPR_BODY (stmt) = body;
11157 if (compound_stmt)
11158 finish_compound_stmt (compound_stmt);
11161 /* Build a __transaction_atomic or __transaction_relaxed expression. If
11162 NOEX is non-NULL, we wrap the body in a MUST_NOT_THROW_EXPR with NOEX as
11163 condition. */
11165 tree
11166 build_transaction_expr (location_t loc, tree expr, int flags, tree noex)
11168 tree ret;
11169 if (noex)
11171 expr = build_must_not_throw_expr (expr, noex);
11172 protected_set_expr_location (expr, loc);
11173 TREE_SIDE_EFFECTS (expr) = 1;
11175 ret = build1 (TRANSACTION_EXPR, TREE_TYPE (expr), expr);
11176 if (flags & TM_STMT_ATTR_RELAXED)
11177 TRANSACTION_EXPR_RELAXED (ret) = 1;
11178 TREE_SIDE_EFFECTS (ret) = 1;
11179 SET_EXPR_LOCATION (ret, loc);
11180 return ret;
11183 void
11184 init_cp_semantics (void)
11189 /* Build a STATIC_ASSERT for a static assertion with the condition
11190 CONDITION and the message text MESSAGE. LOCATION is the location
11191 of the static assertion in the source code. When MEMBER_P, this
11192 static assertion is a member of a class. If SHOW_EXPR_P is true,
11193 print the condition (because it was instantiation-dependent). */
11195 void
11196 finish_static_assert (tree condition, tree message, location_t location,
11197 bool member_p, bool show_expr_p)
11199 tsubst_flags_t complain = tf_warning_or_error;
11201 if (message == NULL_TREE
11202 || message == error_mark_node
11203 || condition == NULL_TREE
11204 || condition == error_mark_node)
11205 return;
11207 if (check_for_bare_parameter_packs (condition))
11208 condition = error_mark_node;
11210 if (instantiation_dependent_expression_p (condition))
11212 /* We're in a template; build a STATIC_ASSERT and put it in
11213 the right place. */
11214 tree assertion;
11216 assertion = make_node (STATIC_ASSERT);
11217 STATIC_ASSERT_CONDITION (assertion) = condition;
11218 STATIC_ASSERT_MESSAGE (assertion) = message;
11219 STATIC_ASSERT_SOURCE_LOCATION (assertion) = location;
11221 if (member_p)
11222 maybe_add_class_template_decl_list (current_class_type,
11223 assertion,
11224 /*friend_p=*/0);
11225 else
11226 add_stmt (assertion);
11228 return;
11231 /* Save the condition in case it was a concept check. */
11232 tree orig_condition = condition;
11234 /* Fold the expression and convert it to a boolean value. */
11235 condition = contextual_conv_bool (condition, complain);
11236 condition = fold_non_dependent_expr (condition, complain,
11237 /*manifestly_const_eval=*/true);
11239 if (TREE_CODE (condition) == INTEGER_CST && !integer_zerop (condition))
11240 /* Do nothing; the condition is satisfied. */
11242 else
11244 iloc_sentinel ils (location);
11246 if (integer_zerop (condition))
11248 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
11249 (TREE_TYPE (TREE_TYPE (message))));
11250 int len = TREE_STRING_LENGTH (message) / sz - 1;
11252 /* See if we can find which clause was failing (for logical AND). */
11253 tree bad = find_failing_clause (NULL, orig_condition);
11254 /* If not, or its location is unusable, fall back to the previous
11255 location. */
11256 location_t cloc = cp_expr_loc_or_loc (bad, location);
11258 auto_diagnostic_group d;
11260 /* Report the error. */
11261 if (len == 0)
11262 error_at (cloc, "static assertion failed");
11263 else
11264 error_at (cloc, "static assertion failed: %s",
11265 TREE_STRING_POINTER (message));
11267 diagnose_failing_condition (bad, cloc, show_expr_p);
11269 else if (condition && condition != error_mark_node)
11271 error ("non-constant condition for static assertion");
11272 if (require_rvalue_constant_expression (condition))
11273 cxx_constant_value (condition);
11278 /* Implements the C++0x decltype keyword. Returns the type of EXPR,
11279 suitable for use as a type-specifier.
11281 ID_EXPRESSION_OR_MEMBER_ACCESS_P is true when EXPR was parsed as an
11282 id-expression or a class member access, FALSE when it was parsed as
11283 a full expression. */
11285 tree
11286 finish_decltype_type (tree expr, bool id_expression_or_member_access_p,
11287 tsubst_flags_t complain)
11289 tree type = NULL_TREE;
11291 if (!expr || error_operand_p (expr))
11292 return error_mark_node;
11294 if (TYPE_P (expr)
11295 || TREE_CODE (expr) == TYPE_DECL
11296 || (TREE_CODE (expr) == BIT_NOT_EXPR
11297 && TYPE_P (TREE_OPERAND (expr, 0))))
11299 if (complain & tf_error)
11300 error ("argument to %<decltype%> must be an expression");
11301 return error_mark_node;
11304 /* decltype is an unevaluated context. */
11305 cp_unevaluated u;
11307 processing_template_decl_sentinel ptds (/*reset=*/false);
11309 /* Depending on the resolution of DR 1172, we may later need to distinguish
11310 instantiation-dependent but not type-dependent expressions so that, say,
11311 A<decltype(sizeof(T))>::U doesn't require 'typename'. */
11312 if (instantiation_dependent_uneval_expression_p (expr))
11314 type = cxx_make_type (DECLTYPE_TYPE);
11315 DECLTYPE_TYPE_EXPR (type) = expr;
11316 DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (type)
11317 = id_expression_or_member_access_p;
11318 SET_TYPE_STRUCTURAL_EQUALITY (type);
11320 return type;
11322 else if (processing_template_decl)
11324 expr = instantiate_non_dependent_expr (expr, complain|tf_decltype);
11325 if (expr == error_mark_node)
11326 return error_mark_node;
11327 /* Keep processing_template_decl cleared for the rest of the function
11328 (for sake of the call to lvalue_kind below, which handles templated
11329 and non-templated COND_EXPR differently). */
11330 processing_template_decl = 0;
11333 /* The type denoted by decltype(e) is defined as follows: */
11335 expr = resolve_nondeduced_context (expr, complain);
11336 if (!mark_single_function (expr, complain))
11337 return error_mark_node;
11339 if (invalid_nonstatic_memfn_p (input_location, expr, complain))
11340 return error_mark_node;
11342 if (type_unknown_p (expr))
11344 if (complain & tf_error)
11345 error ("%<decltype%> cannot resolve address of overloaded function");
11346 return error_mark_node;
11349 /* To get the size of a static data member declared as an array of
11350 unknown bound, we need to instantiate it. */
11351 if (VAR_P (expr)
11352 && VAR_HAD_UNKNOWN_BOUND (expr)
11353 && DECL_TEMPLATE_INSTANTIATION (expr))
11354 instantiate_decl (expr, /*defer_ok*/true, /*expl_inst_mem*/false);
11356 if (id_expression_or_member_access_p)
11358 /* If e is an id-expression or a class member access (5.2.5
11359 [expr.ref]), decltype(e) is defined as the type of the entity
11360 named by e. If there is no such entity, or e names a set of
11361 overloaded functions, the program is ill-formed. */
11362 if (identifier_p (expr))
11363 expr = lookup_name (expr);
11365 if (INDIRECT_REF_P (expr)
11366 || TREE_CODE (expr) == VIEW_CONVERT_EXPR)
11367 /* This can happen when the expression is, e.g., "a.b". Just
11368 look at the underlying operand. */
11369 expr = TREE_OPERAND (expr, 0);
11371 if (TREE_CODE (expr) == OFFSET_REF
11372 || TREE_CODE (expr) == MEMBER_REF
11373 || TREE_CODE (expr) == SCOPE_REF)
11374 /* We're only interested in the field itself. If it is a
11375 BASELINK, we will need to see through it in the next
11376 step. */
11377 expr = TREE_OPERAND (expr, 1);
11379 if (BASELINK_P (expr))
11380 /* See through BASELINK nodes to the underlying function. */
11381 expr = BASELINK_FUNCTIONS (expr);
11383 /* decltype of a decomposition name drops references in the tuple case
11384 (unlike decltype of a normal variable) and keeps cv-qualifiers from
11385 the containing object in the other cases (unlike decltype of a member
11386 access expression). */
11387 if (DECL_DECOMPOSITION_P (expr))
11389 if (DECL_HAS_VALUE_EXPR_P (expr))
11390 /* Expr is an array or struct subobject proxy, handle
11391 bit-fields properly. */
11392 return unlowered_expr_type (expr);
11393 else
11394 /* Expr is a reference variable for the tuple case. */
11395 return lookup_decomp_type (expr);
11398 switch (TREE_CODE (expr))
11400 case FIELD_DECL:
11401 if (DECL_BIT_FIELD_TYPE (expr))
11403 type = DECL_BIT_FIELD_TYPE (expr);
11404 break;
11406 /* Fall through for fields that aren't bitfields. */
11407 gcc_fallthrough ();
11409 case FUNCTION_DECL:
11410 case VAR_DECL:
11411 case CONST_DECL:
11412 case PARM_DECL:
11413 case RESULT_DECL:
11414 case TEMPLATE_PARM_INDEX:
11415 expr = mark_type_use (expr);
11416 type = TREE_TYPE (expr);
11417 break;
11419 case ERROR_MARK:
11420 type = error_mark_node;
11421 break;
11423 case COMPONENT_REF:
11424 case COMPOUND_EXPR:
11425 mark_type_use (expr);
11426 type = is_bitfield_expr_with_lowered_type (expr);
11427 if (!type)
11428 type = TREE_TYPE (TREE_OPERAND (expr, 1));
11429 break;
11431 case BIT_FIELD_REF:
11432 gcc_unreachable ();
11434 case INTEGER_CST:
11435 case PTRMEM_CST:
11436 /* We can get here when the id-expression refers to an
11437 enumerator or non-type template parameter. */
11438 type = TREE_TYPE (expr);
11439 break;
11441 default:
11442 /* Handle instantiated template non-type arguments. */
11443 type = TREE_TYPE (expr);
11444 break;
11447 else
11449 /* Within a lambda-expression:
11451 Every occurrence of decltype((x)) where x is a possibly
11452 parenthesized id-expression that names an entity of
11453 automatic storage duration is treated as if x were
11454 transformed into an access to a corresponding data member
11455 of the closure type that would have been declared if x
11456 were a use of the denoted entity. */
11457 if (outer_automatic_var_p (expr)
11458 && current_function_decl
11459 && LAMBDA_FUNCTION_P (current_function_decl))
11460 type = capture_decltype (expr);
11461 else if (error_operand_p (expr))
11462 type = error_mark_node;
11463 else if (expr == current_class_ptr)
11464 /* If the expression is just "this", we want the
11465 cv-unqualified pointer for the "this" type. */
11466 type = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
11467 else
11469 /* Otherwise, where T is the type of e, if e is an lvalue,
11470 decltype(e) is defined as T&; if an xvalue, T&&; otherwise, T. */
11471 cp_lvalue_kind clk = lvalue_kind (expr);
11472 type = unlowered_expr_type (expr);
11473 gcc_assert (!TYPE_REF_P (type));
11475 /* For vector types, pick a non-opaque variant. */
11476 if (VECTOR_TYPE_P (type))
11477 type = strip_typedefs (type);
11479 if (clk != clk_none && !(clk & clk_class))
11480 type = cp_build_reference_type (type, (clk & clk_rvalueref));
11484 return type;
11487 /* Called from trait_expr_value to evaluate either __has_nothrow_assign or
11488 __has_nothrow_copy, depending on assign_p. Returns true iff all
11489 the copy {ctor,assign} fns are nothrow. */
11491 static bool
11492 classtype_has_nothrow_assign_or_copy_p (tree type, bool assign_p)
11494 tree fns = NULL_TREE;
11496 if (assign_p || TYPE_HAS_COPY_CTOR (type))
11497 fns = get_class_binding (type, assign_p ? assign_op_identifier
11498 : ctor_identifier);
11500 bool saw_copy = false;
11501 for (ovl_iterator iter (fns); iter; ++iter)
11503 tree fn = *iter;
11505 if (copy_fn_p (fn) > 0)
11507 saw_copy = true;
11508 if (!maybe_instantiate_noexcept (fn)
11509 || !TYPE_NOTHROW_P (TREE_TYPE (fn)))
11510 return false;
11514 return saw_copy;
11517 /* Return true if DERIVED is pointer interconvertible base of BASE. */
11519 static bool
11520 pointer_interconvertible_base_of_p (tree base, tree derived)
11522 if (base == error_mark_node || derived == error_mark_node)
11523 return false;
11524 base = TYPE_MAIN_VARIANT (base);
11525 derived = TYPE_MAIN_VARIANT (derived);
11526 if (!NON_UNION_CLASS_TYPE_P (base)
11527 || !NON_UNION_CLASS_TYPE_P (derived))
11528 return false;
11530 if (same_type_p (base, derived))
11531 return true;
11533 if (!std_layout_type_p (derived))
11534 return false;
11536 return uniquely_derived_from_p (base, derived);
11539 /* Helper function for fold_builtin_is_pointer_inverconvertible_with_class,
11540 return true if MEMBERTYPE is the type of the first non-static data member
11541 of TYPE or for unions of any members. */
11542 static bool
11543 first_nonstatic_data_member_p (tree type, tree membertype)
11545 for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
11547 if (TREE_CODE (field) != FIELD_DECL)
11548 continue;
11549 if (DECL_FIELD_IS_BASE (field) && is_empty_field (field))
11550 continue;
11551 if (DECL_FIELD_IS_BASE (field))
11552 return first_nonstatic_data_member_p (TREE_TYPE (field), membertype);
11553 if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
11555 if ((TREE_CODE (TREE_TYPE (field)) == UNION_TYPE
11556 || std_layout_type_p (TREE_TYPE (field)))
11557 && first_nonstatic_data_member_p (TREE_TYPE (field), membertype))
11558 return true;
11560 else if (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (field),
11561 membertype))
11562 return true;
11563 if (TREE_CODE (type) != UNION_TYPE)
11564 return false;
11566 return false;
11569 /* Fold __builtin_is_pointer_interconvertible_with_class call. */
11571 tree
11572 fold_builtin_is_pointer_inverconvertible_with_class (location_t loc, int nargs,
11573 tree *args)
11575 /* Unless users call the builtin directly, the following 3 checks should be
11576 ensured from std::is_pointer_interconvertible_with_class function
11577 template. */
11578 if (nargs != 1)
11580 error_at (loc, "%<__builtin_is_pointer_interconvertible_with_class%> "
11581 "needs a single argument");
11582 return boolean_false_node;
11584 tree arg = args[0];
11585 if (error_operand_p (arg))
11586 return boolean_false_node;
11587 if (!TYPE_PTRMEM_P (TREE_TYPE (arg)))
11589 error_at (loc, "%<__builtin_is_pointer_interconvertible_with_class%> "
11590 "argument is not pointer to member");
11591 return boolean_false_node;
11594 if (!TYPE_PTRDATAMEM_P (TREE_TYPE (arg)))
11595 return boolean_false_node;
11597 tree membertype = TREE_TYPE (TREE_TYPE (arg));
11598 tree basetype = TYPE_OFFSET_BASETYPE (TREE_TYPE (arg));
11599 if (!complete_type_or_else (basetype, NULL_TREE))
11600 return boolean_false_node;
11602 if (TREE_CODE (basetype) != UNION_TYPE
11603 && !std_layout_type_p (basetype))
11604 return boolean_false_node;
11606 if (!first_nonstatic_data_member_p (basetype, membertype))
11607 return boolean_false_node;
11609 if (TREE_CODE (arg) == PTRMEM_CST)
11610 arg = cplus_expand_constant (arg);
11612 if (integer_nonzerop (arg))
11613 return boolean_false_node;
11614 if (integer_zerop (arg))
11615 return boolean_true_node;
11617 return fold_build2 (EQ_EXPR, boolean_type_node, arg,
11618 build_zero_cst (TREE_TYPE (arg)));
11621 /* Helper function for is_corresponding_member_aggr. Return true if
11622 MEMBERTYPE pointer-to-data-member ARG can be found in anonymous
11623 union or structure BASETYPE. */
11625 static bool
11626 is_corresponding_member_union (tree basetype, tree membertype, tree arg)
11628 for (tree field = TYPE_FIELDS (basetype); field; field = DECL_CHAIN (field))
11629 if (TREE_CODE (field) != FIELD_DECL || DECL_BIT_FIELD_TYPE (field))
11630 continue;
11631 else if (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (field),
11632 membertype))
11634 if (TREE_CODE (arg) != INTEGER_CST
11635 || tree_int_cst_equal (arg, byte_position (field)))
11636 return true;
11638 else if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
11640 tree narg = arg;
11641 if (TREE_CODE (basetype) != UNION_TYPE
11642 && TREE_CODE (narg) == INTEGER_CST)
11643 narg = size_binop (MINUS_EXPR, arg, byte_position (field));
11644 if (is_corresponding_member_union (TREE_TYPE (field),
11645 membertype, narg))
11646 return true;
11648 return false;
11651 /* Helper function for fold_builtin_is_corresponding_member call.
11652 Return boolean_false_node if MEMBERTYPE1 BASETYPE1::*ARG1 and
11653 MEMBERTYPE2 BASETYPE2::*ARG2 aren't corresponding members,
11654 boolean_true_node if they are corresponding members, or for
11655 non-constant ARG2 the highest member offset for corresponding
11656 members. */
11658 static tree
11659 is_corresponding_member_aggr (location_t loc, tree basetype1, tree membertype1,
11660 tree arg1, tree basetype2, tree membertype2,
11661 tree arg2)
11663 tree field1 = TYPE_FIELDS (basetype1);
11664 tree field2 = TYPE_FIELDS (basetype2);
11665 tree ret = boolean_false_node;
11666 while (1)
11668 bool r = next_common_initial_seqence (field1, field2);
11669 if (field1 == NULL_TREE || field2 == NULL_TREE)
11670 break;
11671 if (r
11672 && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (field1),
11673 membertype1)
11674 && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (field2),
11675 membertype2))
11677 tree pos = byte_position (field1);
11678 if (TREE_CODE (arg1) == INTEGER_CST
11679 && tree_int_cst_equal (arg1, pos))
11681 if (TREE_CODE (arg2) == INTEGER_CST)
11682 return boolean_true_node;
11683 return pos;
11685 else if (TREE_CODE (arg1) != INTEGER_CST)
11686 ret = pos;
11688 else if (ANON_AGGR_TYPE_P (TREE_TYPE (field1))
11689 && ANON_AGGR_TYPE_P (TREE_TYPE (field2)))
11691 if ((!lookup_attribute ("no_unique_address",
11692 DECL_ATTRIBUTES (field1)))
11693 != !lookup_attribute ("no_unique_address",
11694 DECL_ATTRIBUTES (field2)))
11695 break;
11696 if (!tree_int_cst_equal (bit_position (field1),
11697 bit_position (field2)))
11698 break;
11699 bool overlap = true;
11700 tree pos = byte_position (field1);
11701 if (TREE_CODE (arg1) == INTEGER_CST)
11703 tree off1 = fold_convert (sizetype, arg1);
11704 tree sz1 = TYPE_SIZE_UNIT (TREE_TYPE (field1));
11705 if (tree_int_cst_lt (off1, pos)
11706 || tree_int_cst_le (size_binop (PLUS_EXPR, pos, sz1), off1))
11707 overlap = false;
11709 if (TREE_CODE (arg2) == INTEGER_CST)
11711 tree off2 = fold_convert (sizetype, arg2);
11712 tree sz2 = TYPE_SIZE_UNIT (TREE_TYPE (field2));
11713 if (tree_int_cst_lt (off2, pos)
11714 || tree_int_cst_le (size_binop (PLUS_EXPR, pos, sz2), off2))
11715 overlap = false;
11717 if (overlap
11718 && NON_UNION_CLASS_TYPE_P (TREE_TYPE (field1))
11719 && NON_UNION_CLASS_TYPE_P (TREE_TYPE (field2)))
11721 tree narg1 = arg1;
11722 if (TREE_CODE (arg1) == INTEGER_CST)
11723 narg1 = size_binop (MINUS_EXPR,
11724 fold_convert (sizetype, arg1), pos);
11725 tree narg2 = arg2;
11726 if (TREE_CODE (arg2) == INTEGER_CST)
11727 narg2 = size_binop (MINUS_EXPR,
11728 fold_convert (sizetype, arg2), pos);
11729 tree t1 = TREE_TYPE (field1);
11730 tree t2 = TREE_TYPE (field2);
11731 tree nret = is_corresponding_member_aggr (loc, t1, membertype1,
11732 narg1, t2, membertype2,
11733 narg2);
11734 if (nret != boolean_false_node)
11736 if (nret == boolean_true_node)
11737 return nret;
11738 if (TREE_CODE (arg1) == INTEGER_CST)
11739 return size_binop (PLUS_EXPR, nret, pos);
11740 ret = size_binop (PLUS_EXPR, nret, pos);
11743 else if (overlap
11744 && TREE_CODE (TREE_TYPE (field1)) == UNION_TYPE
11745 && TREE_CODE (TREE_TYPE (field2)) == UNION_TYPE)
11747 tree narg1 = arg1;
11748 if (TREE_CODE (arg1) == INTEGER_CST)
11749 narg1 = size_binop (MINUS_EXPR,
11750 fold_convert (sizetype, arg1), pos);
11751 tree narg2 = arg2;
11752 if (TREE_CODE (arg2) == INTEGER_CST)
11753 narg2 = size_binop (MINUS_EXPR,
11754 fold_convert (sizetype, arg2), pos);
11755 if (is_corresponding_member_union (TREE_TYPE (field1),
11756 membertype1, narg1)
11757 && is_corresponding_member_union (TREE_TYPE (field2),
11758 membertype2, narg2))
11760 sorry_at (loc, "%<__builtin_is_corresponding_member%> "
11761 "not well defined for anonymous unions");
11762 return boolean_false_node;
11766 if (!r)
11767 break;
11768 field1 = DECL_CHAIN (field1);
11769 field2 = DECL_CHAIN (field2);
11771 return ret;
11774 /* Fold __builtin_is_corresponding_member call. */
11776 tree
11777 fold_builtin_is_corresponding_member (location_t loc, int nargs,
11778 tree *args)
11780 /* Unless users call the builtin directly, the following 3 checks should be
11781 ensured from std::is_corresponding_member function template. */
11782 if (nargs != 2)
11784 error_at (loc, "%<__builtin_is_corresponding_member%> "
11785 "needs two arguments");
11786 return boolean_false_node;
11788 tree arg1 = args[0];
11789 tree arg2 = args[1];
11790 if (error_operand_p (arg1) || error_operand_p (arg2))
11791 return boolean_false_node;
11792 if (!TYPE_PTRMEM_P (TREE_TYPE (arg1))
11793 || !TYPE_PTRMEM_P (TREE_TYPE (arg2)))
11795 error_at (loc, "%<__builtin_is_corresponding_member%> "
11796 "argument is not pointer to member");
11797 return boolean_false_node;
11800 if (!TYPE_PTRDATAMEM_P (TREE_TYPE (arg1))
11801 || !TYPE_PTRDATAMEM_P (TREE_TYPE (arg2)))
11802 return boolean_false_node;
11804 tree membertype1 = TREE_TYPE (TREE_TYPE (arg1));
11805 tree basetype1 = TYPE_OFFSET_BASETYPE (TREE_TYPE (arg1));
11806 if (!complete_type_or_else (basetype1, NULL_TREE))
11807 return boolean_false_node;
11809 tree membertype2 = TREE_TYPE (TREE_TYPE (arg2));
11810 tree basetype2 = TYPE_OFFSET_BASETYPE (TREE_TYPE (arg2));
11811 if (!complete_type_or_else (basetype2, NULL_TREE))
11812 return boolean_false_node;
11814 if (!NON_UNION_CLASS_TYPE_P (basetype1)
11815 || !NON_UNION_CLASS_TYPE_P (basetype2)
11816 || !std_layout_type_p (basetype1)
11817 || !std_layout_type_p (basetype2))
11818 return boolean_false_node;
11820 /* If the member types aren't layout compatible, then they
11821 can't be corresponding members. */
11822 if (!layout_compatible_type_p (membertype1, membertype2))
11823 return boolean_false_node;
11825 if (TREE_CODE (arg1) == PTRMEM_CST)
11826 arg1 = cplus_expand_constant (arg1);
11827 if (TREE_CODE (arg2) == PTRMEM_CST)
11828 arg2 = cplus_expand_constant (arg2);
11830 if (null_member_pointer_value_p (arg1)
11831 || null_member_pointer_value_p (arg2))
11832 return boolean_false_node;
11834 if (TREE_CODE (arg1) == INTEGER_CST
11835 && TREE_CODE (arg2) == INTEGER_CST
11836 && !tree_int_cst_equal (arg1, arg2))
11837 return boolean_false_node;
11839 if (TREE_CODE (arg2) == INTEGER_CST
11840 && TREE_CODE (arg1) != INTEGER_CST)
11842 std::swap (arg1, arg2);
11843 std::swap (membertype1, membertype2);
11844 std::swap (basetype1, basetype2);
11847 tree ret = is_corresponding_member_aggr (loc, basetype1, membertype1, arg1,
11848 basetype2, membertype2, arg2);
11849 if (TREE_TYPE (ret) == boolean_type_node)
11850 return ret;
11851 /* If both arg1 and arg2 are INTEGER_CSTs, is_corresponding_member_aggr
11852 already returns boolean_{true,false}_node whether those particular
11853 members are corresponding members or not. Otherwise, if only
11854 one of them is INTEGER_CST (canonicalized to first being INTEGER_CST
11855 above), it returns boolean_false_node if it is certainly not a
11856 corresponding member and otherwise we need to do a runtime check that
11857 those two OFFSET_TYPE offsets are equal.
11858 If neither of the operands is INTEGER_CST, is_corresponding_member_aggr
11859 returns the largest offset at which the members would be corresponding
11860 members, so perform arg1 <= ret && arg1 == arg2 runtime check. */
11861 gcc_assert (TREE_CODE (arg2) != INTEGER_CST);
11862 if (TREE_CODE (arg1) == INTEGER_CST)
11863 return fold_build2 (EQ_EXPR, boolean_type_node, arg1,
11864 fold_convert (TREE_TYPE (arg1), arg2));
11865 ret = fold_build2 (LE_EXPR, boolean_type_node,
11866 fold_convert (pointer_sized_int_node, arg1),
11867 fold_convert (pointer_sized_int_node, ret));
11868 return fold_build2 (TRUTH_AND_EXPR, boolean_type_node, ret,
11869 fold_build2 (EQ_EXPR, boolean_type_node, arg1,
11870 fold_convert (TREE_TYPE (arg1), arg2)));
11873 /* Actually evaluates the trait. */
11875 static bool
11876 trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
11878 enum tree_code type_code1;
11879 tree t;
11881 type_code1 = TREE_CODE (type1);
11883 switch (kind)
11885 case CPTK_HAS_NOTHROW_ASSIGN:
11886 type1 = strip_array_types (type1);
11887 return (!CP_TYPE_CONST_P (type1) && type_code1 != REFERENCE_TYPE
11888 && (trait_expr_value (CPTK_HAS_TRIVIAL_ASSIGN, type1, type2)
11889 || (CLASS_TYPE_P (type1)
11890 && classtype_has_nothrow_assign_or_copy_p (type1,
11891 true))));
11893 case CPTK_HAS_TRIVIAL_ASSIGN:
11894 /* ??? The standard seems to be missing the "or array of such a class
11895 type" wording for this trait. */
11896 type1 = strip_array_types (type1);
11897 return (!CP_TYPE_CONST_P (type1) && type_code1 != REFERENCE_TYPE
11898 && (trivial_type_p (type1)
11899 || (CLASS_TYPE_P (type1)
11900 && TYPE_HAS_TRIVIAL_COPY_ASSIGN (type1))));
11902 case CPTK_HAS_NOTHROW_CONSTRUCTOR:
11903 type1 = strip_array_types (type1);
11904 return (trait_expr_value (CPTK_HAS_TRIVIAL_CONSTRUCTOR, type1, type2)
11905 || (CLASS_TYPE_P (type1)
11906 && (t = locate_ctor (type1))
11907 && maybe_instantiate_noexcept (t)
11908 && TYPE_NOTHROW_P (TREE_TYPE (t))));
11910 case CPTK_HAS_TRIVIAL_CONSTRUCTOR:
11911 type1 = strip_array_types (type1);
11912 return (trivial_type_p (type1)
11913 || (CLASS_TYPE_P (type1) && TYPE_HAS_TRIVIAL_DFLT (type1)));
11915 case CPTK_HAS_NOTHROW_COPY:
11916 type1 = strip_array_types (type1);
11917 return (trait_expr_value (CPTK_HAS_TRIVIAL_COPY, type1, type2)
11918 || (CLASS_TYPE_P (type1)
11919 && classtype_has_nothrow_assign_or_copy_p (type1, false)));
11921 case CPTK_HAS_TRIVIAL_COPY:
11922 /* ??? The standard seems to be missing the "or array of such a class
11923 type" wording for this trait. */
11924 type1 = strip_array_types (type1);
11925 return (trivial_type_p (type1) || type_code1 == REFERENCE_TYPE
11926 || (CLASS_TYPE_P (type1) && TYPE_HAS_TRIVIAL_COPY_CTOR (type1)));
11928 case CPTK_HAS_TRIVIAL_DESTRUCTOR:
11929 type1 = strip_array_types (type1);
11930 return (trivial_type_p (type1) || type_code1 == REFERENCE_TYPE
11931 || (CLASS_TYPE_P (type1)
11932 && TYPE_HAS_TRIVIAL_DESTRUCTOR (type1)));
11934 case CPTK_HAS_VIRTUAL_DESTRUCTOR:
11935 return type_has_virtual_destructor (type1);
11937 case CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS:
11938 return type_has_unique_obj_representations (type1);
11940 case CPTK_IS_ABSTRACT:
11941 return ABSTRACT_CLASS_TYPE_P (type1);
11943 case CPTK_IS_AGGREGATE:
11944 return CP_AGGREGATE_TYPE_P (type1);
11946 case CPTK_IS_BASE_OF:
11947 return (NON_UNION_CLASS_TYPE_P (type1) && NON_UNION_CLASS_TYPE_P (type2)
11948 && (same_type_ignoring_top_level_qualifiers_p (type1, type2)
11949 || DERIVED_FROM_P (type1, type2)));
11951 case CPTK_IS_CLASS:
11952 return NON_UNION_CLASS_TYPE_P (type1);
11954 case CPTK_IS_EMPTY:
11955 return NON_UNION_CLASS_TYPE_P (type1) && CLASSTYPE_EMPTY_P (type1);
11957 case CPTK_IS_ENUM:
11958 return type_code1 == ENUMERAL_TYPE;
11960 case CPTK_IS_FINAL:
11961 return CLASS_TYPE_P (type1) && CLASSTYPE_FINAL (type1);
11963 case CPTK_IS_LAYOUT_COMPATIBLE:
11964 return layout_compatible_type_p (type1, type2);
11966 case CPTK_IS_LITERAL_TYPE:
11967 return literal_type_p (type1);
11969 case CPTK_IS_POINTER_INTERCONVERTIBLE_BASE_OF:
11970 return pointer_interconvertible_base_of_p (type1, type2);
11972 case CPTK_IS_POD:
11973 return pod_type_p (type1);
11975 case CPTK_IS_POLYMORPHIC:
11976 return CLASS_TYPE_P (type1) && TYPE_POLYMORPHIC_P (type1);
11978 case CPTK_IS_SAME:
11979 return same_type_p (type1, type2);
11981 case CPTK_IS_STD_LAYOUT:
11982 return std_layout_type_p (type1);
11984 case CPTK_IS_TRIVIAL:
11985 return trivial_type_p (type1);
11987 case CPTK_IS_TRIVIALLY_ASSIGNABLE:
11988 return is_trivially_xible (MODIFY_EXPR, type1, type2);
11990 case CPTK_IS_TRIVIALLY_CONSTRUCTIBLE:
11991 return is_trivially_xible (INIT_EXPR, type1, type2);
11993 case CPTK_IS_TRIVIALLY_COPYABLE:
11994 return trivially_copyable_p (type1);
11996 case CPTK_IS_UNION:
11997 return type_code1 == UNION_TYPE;
11999 case CPTK_IS_ASSIGNABLE:
12000 return is_xible (MODIFY_EXPR, type1, type2);
12002 case CPTK_IS_CONSTRUCTIBLE:
12003 return is_xible (INIT_EXPR, type1, type2);
12005 case CPTK_IS_NOTHROW_ASSIGNABLE:
12006 return is_nothrow_xible (MODIFY_EXPR, type1, type2);
12008 case CPTK_IS_NOTHROW_CONSTRUCTIBLE:
12009 return is_nothrow_xible (INIT_EXPR, type1, type2);
12011 case CPTK_IS_CONVERTIBLE:
12012 return is_convertible (type1, type2);
12014 case CPTK_IS_NOTHROW_CONVERTIBLE:
12015 return is_nothrow_convertible (type1, type2);
12017 case CPTK_REF_CONSTRUCTS_FROM_TEMPORARY:
12018 return ref_xes_from_temporary (type1, type2, /*direct_init=*/true);
12020 case CPTK_REF_CONVERTS_FROM_TEMPORARY:
12021 return ref_xes_from_temporary (type1, type2, /*direct_init=*/false);
12023 #define DEFTRAIT_TYPE(CODE, NAME, ARITY) \
12024 case CPTK_##CODE:
12025 #include "cp-trait.def"
12026 #undef DEFTRAIT_TYPE
12027 /* Type-yielding traits are handled in finish_trait_type. */
12028 break;
12031 gcc_unreachable ();
12034 /* Returns true if TYPE meets the requirements for the specified KIND,
12035 false otherwise.
12037 When KIND == 1, TYPE must be an array of unknown bound,
12038 or (possibly cv-qualified) void, or a complete type.
12040 When KIND == 2, TYPE must be a complete type, or array of complete type,
12041 or (possibly cv-qualified) void.
12043 When KIND == 3:
12044 If TYPE is a non-union class type, it must be complete.
12046 When KIND == 4:
12047 If TYPE is a class type, it must be complete. */
12049 static bool
12050 check_trait_type (tree type, int kind = 1)
12052 if (type == NULL_TREE)
12053 return true;
12055 if (TREE_CODE (type) == TREE_LIST)
12056 return (check_trait_type (TREE_VALUE (type))
12057 && check_trait_type (TREE_CHAIN (type)));
12059 if (kind == 1 && TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type))
12060 return true; // Array of unknown bound. Don't care about completeness.
12062 if (kind == 3 && !NON_UNION_CLASS_TYPE_P (type))
12063 return true; // Not a non-union class type. Don't care about completeness.
12065 if (kind == 4 && TREE_CODE (type) == ARRAY_TYPE)
12066 return true; // Not a class type. Don't care about completeness.
12068 if (VOID_TYPE_P (type))
12069 return true;
12071 return !!complete_type_or_else (strip_array_types (type), NULL_TREE);
12074 /* Process a trait expression. */
12076 tree
12077 finish_trait_expr (location_t loc, cp_trait_kind kind, tree type1, tree type2)
12079 if (type1 == error_mark_node
12080 || type2 == error_mark_node)
12081 return error_mark_node;
12083 if (processing_template_decl)
12085 tree trait_expr = make_node (TRAIT_EXPR);
12086 TREE_TYPE (trait_expr) = boolean_type_node;
12087 TRAIT_EXPR_TYPE1 (trait_expr) = type1;
12088 TRAIT_EXPR_TYPE2 (trait_expr) = type2;
12089 TRAIT_EXPR_KIND (trait_expr) = kind;
12090 TRAIT_EXPR_LOCATION (trait_expr) = loc;
12091 return trait_expr;
12094 switch (kind)
12096 case CPTK_HAS_NOTHROW_ASSIGN:
12097 case CPTK_HAS_TRIVIAL_ASSIGN:
12098 case CPTK_HAS_NOTHROW_CONSTRUCTOR:
12099 case CPTK_HAS_TRIVIAL_CONSTRUCTOR:
12100 case CPTK_HAS_NOTHROW_COPY:
12101 case CPTK_HAS_TRIVIAL_COPY:
12102 case CPTK_HAS_TRIVIAL_DESTRUCTOR:
12103 case CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS:
12104 if (!check_trait_type (type1))
12105 return error_mark_node;
12106 break;
12108 case CPTK_IS_LITERAL_TYPE:
12109 case CPTK_IS_POD:
12110 case CPTK_IS_STD_LAYOUT:
12111 case CPTK_IS_TRIVIAL:
12112 case CPTK_IS_TRIVIALLY_COPYABLE:
12113 if (!check_trait_type (type1, /* kind = */ 2))
12114 return error_mark_node;
12115 break;
12117 case CPTK_IS_EMPTY:
12118 case CPTK_IS_POLYMORPHIC:
12119 case CPTK_IS_ABSTRACT:
12120 case CPTK_HAS_VIRTUAL_DESTRUCTOR:
12121 if (!check_trait_type (type1, /* kind = */ 3))
12122 return error_mark_node;
12123 break;
12125 /* N.B. std::is_aggregate is kind=2 but we don't need a complete element
12126 type to know whether an array is an aggregate, so use kind=4 here. */
12127 case CPTK_IS_AGGREGATE:
12128 case CPTK_IS_FINAL:
12129 if (!check_trait_type (type1, /* kind = */ 4))
12130 return error_mark_node;
12131 break;
12133 case CPTK_IS_ASSIGNABLE:
12134 case CPTK_IS_CONSTRUCTIBLE:
12135 if (!check_trait_type (type1))
12136 return error_mark_node;
12137 break;
12139 case CPTK_IS_TRIVIALLY_ASSIGNABLE:
12140 case CPTK_IS_TRIVIALLY_CONSTRUCTIBLE:
12141 case CPTK_IS_NOTHROW_ASSIGNABLE:
12142 case CPTK_IS_NOTHROW_CONSTRUCTIBLE:
12143 case CPTK_IS_CONVERTIBLE:
12144 case CPTK_IS_NOTHROW_CONVERTIBLE:
12145 case CPTK_REF_CONSTRUCTS_FROM_TEMPORARY:
12146 case CPTK_REF_CONVERTS_FROM_TEMPORARY:
12147 if (!check_trait_type (type1)
12148 || !check_trait_type (type2))
12149 return error_mark_node;
12150 break;
12152 case CPTK_IS_BASE_OF:
12153 case CPTK_IS_POINTER_INTERCONVERTIBLE_BASE_OF:
12154 if (NON_UNION_CLASS_TYPE_P (type1) && NON_UNION_CLASS_TYPE_P (type2)
12155 && !same_type_ignoring_top_level_qualifiers_p (type1, type2)
12156 && !complete_type_or_else (type2, NULL_TREE))
12157 /* We already issued an error. */
12158 return error_mark_node;
12159 break;
12161 case CPTK_IS_CLASS:
12162 case CPTK_IS_ENUM:
12163 case CPTK_IS_UNION:
12164 case CPTK_IS_SAME:
12165 break;
12167 case CPTK_IS_LAYOUT_COMPATIBLE:
12168 if (!array_of_unknown_bound_p (type1)
12169 && TREE_CODE (type1) != VOID_TYPE
12170 && !complete_type_or_else (type1, NULL_TREE))
12171 /* We already issued an error. */
12172 return error_mark_node;
12173 if (!array_of_unknown_bound_p (type2)
12174 && TREE_CODE (type2) != VOID_TYPE
12175 && !complete_type_or_else (type2, NULL_TREE))
12176 /* We already issued an error. */
12177 return error_mark_node;
12178 break;
12180 #define DEFTRAIT_TYPE(CODE, NAME, ARITY) \
12181 case CPTK_##CODE:
12182 #include "cp-trait.def"
12183 #undef DEFTRAIT_TYPE
12184 /* Type-yielding traits are handled in finish_trait_type. */
12185 gcc_unreachable ();
12188 tree val = (trait_expr_value (kind, type1, type2)
12189 ? boolean_true_node : boolean_false_node);
12190 return maybe_wrap_with_location (val, loc);
12193 /* Process a trait type. */
12195 tree
12196 finish_trait_type (cp_trait_kind kind, tree type1, tree type2)
12198 if (type1 == error_mark_node
12199 || type2 == error_mark_node)
12200 return error_mark_node;
12202 if (processing_template_decl)
12204 tree type = cxx_make_type (TRAIT_TYPE);
12205 TRAIT_TYPE_TYPE1 (type) = type1;
12206 TRAIT_TYPE_TYPE2 (type) = type2;
12207 TRAIT_TYPE_KIND_RAW (type) = build_int_cstu (integer_type_node, kind);
12208 /* These traits are intended to be used in the definition of the ::type
12209 member of the corresponding standard library type trait and aren't
12210 mangleable (and thus won't appear directly in template signatures),
12211 so structural equality should suffice. */
12212 SET_TYPE_STRUCTURAL_EQUALITY (type);
12213 return type;
12216 switch (kind)
12218 case CPTK_UNDERLYING_TYPE:
12219 return finish_underlying_type (type1);
12220 case CPTK_REMOVE_CV:
12221 return cv_unqualified (type1);
12222 case CPTK_REMOVE_REFERENCE:
12223 if (TYPE_REF_P (type1))
12224 type1 = TREE_TYPE (type1);
12225 return type1;
12226 case CPTK_REMOVE_CVREF:
12227 if (TYPE_REF_P (type1))
12228 type1 = TREE_TYPE (type1);
12229 return cv_unqualified (type1);
12231 #define DEFTRAIT_EXPR(CODE, NAME, ARITY) \
12232 case CPTK_##CODE:
12233 #include "cp-trait.def"
12234 #undef DEFTRAIT_EXPR
12235 /* Expression-yielding traits are handled in finish_trait_expr. */
12236 case CPTK_BASES:
12237 case CPTK_DIRECT_BASES:
12238 /* BASES and DIRECT_BASES are handled in finish_bases. */
12239 break;
12242 gcc_unreachable ();
12245 /* Do-nothing variants of functions to handle pragma FLOAT_CONST_DECIMAL64,
12246 which is ignored for C++. */
12248 void
12249 set_float_const_decimal64 (void)
12253 void
12254 clear_float_const_decimal64 (void)
12258 bool
12259 float_const_decimal64_p (void)
12261 return 0;
12265 /* Return true if T designates the implied `this' parameter. */
12267 bool
12268 is_this_parameter (tree t)
12270 if (!DECL_P (t) || DECL_NAME (t) != this_identifier)
12271 return false;
12272 gcc_assert (TREE_CODE (t) == PARM_DECL
12273 || (TREE_CODE (t) == VAR_DECL && DECL_HAS_VALUE_EXPR_P (t))
12274 || (cp_binding_oracle && TREE_CODE (t) == VAR_DECL));
12275 return true;
12278 /* Insert the deduced return type for an auto function. */
12280 void
12281 apply_deduced_return_type (tree fco, tree return_type)
12283 tree result;
12285 if (return_type == error_mark_node)
12286 return;
12288 if (DECL_CONV_FN_P (fco))
12289 DECL_NAME (fco) = make_conv_op_name (return_type);
12291 TREE_TYPE (fco) = change_return_type (return_type, TREE_TYPE (fco));
12293 result = DECL_RESULT (fco);
12294 if (result == NULL_TREE)
12295 return;
12296 if (TREE_TYPE (result) == return_type)
12297 return;
12299 if (!processing_template_decl && !VOID_TYPE_P (return_type)
12300 && !complete_type_or_else (return_type, NULL_TREE))
12301 return;
12303 /* We already have a DECL_RESULT from start_preparsed_function.
12304 Now we need to redo the work it and allocate_struct_function
12305 did to reflect the new type. */
12306 gcc_assert (current_function_decl == fco);
12307 result = build_decl (input_location, RESULT_DECL, NULL_TREE,
12308 TYPE_MAIN_VARIANT (return_type));
12309 DECL_ARTIFICIAL (result) = 1;
12310 DECL_IGNORED_P (result) = 1;
12311 cp_apply_type_quals_to_decl (cp_type_quals (return_type),
12312 result);
12314 DECL_RESULT (fco) = result;
12316 if (!processing_template_decl)
12318 bool aggr = aggregate_value_p (result, fco);
12319 #ifdef PCC_STATIC_STRUCT_RETURN
12320 cfun->returns_pcc_struct = aggr;
12321 #endif
12322 cfun->returns_struct = aggr;
12326 /* DECL is a local variable or parameter from the surrounding scope of a
12327 lambda-expression. Returns the decltype for a use of the capture field
12328 for DECL even if it hasn't been captured yet. */
12330 static tree
12331 capture_decltype (tree decl)
12333 tree lam = CLASSTYPE_LAMBDA_EXPR (DECL_CONTEXT (current_function_decl));
12334 tree cap = lookup_name (DECL_NAME (decl), LOOK_where::BLOCK,
12335 LOOK_want::HIDDEN_LAMBDA);
12336 tree type;
12338 if (cap && is_capture_proxy (cap))
12339 type = TREE_TYPE (cap);
12340 else
12341 switch (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lam))
12343 case CPLD_NONE:
12344 error ("%qD is not captured", decl);
12345 return error_mark_node;
12347 case CPLD_COPY:
12348 type = TREE_TYPE (decl);
12349 if (TYPE_REF_P (type)
12350 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
12351 type = TREE_TYPE (type);
12352 break;
12354 case CPLD_REFERENCE:
12355 type = TREE_TYPE (decl);
12356 if (!TYPE_REF_P (type))
12357 type = build_reference_type (TREE_TYPE (decl));
12358 break;
12360 default:
12361 gcc_unreachable ();
12364 if (!TYPE_REF_P (type))
12366 if (!LAMBDA_EXPR_MUTABLE_P (lam))
12367 type = cp_build_qualified_type (type, (cp_type_quals (type)
12368 |TYPE_QUAL_CONST));
12369 type = build_reference_type (type);
12371 return type;
12374 /* Build a unary fold expression of EXPR over OP. If IS_RIGHT is true,
12375 this is a right unary fold. Otherwise it is a left unary fold. */
12377 static tree
12378 finish_unary_fold_expr (tree expr, int op, tree_code dir)
12380 /* Build a pack expansion (assuming expr has pack type). */
12381 if (!uses_parameter_packs (expr))
12383 error_at (location_of (expr), "operand of fold expression has no "
12384 "unexpanded parameter packs");
12385 return error_mark_node;
12387 tree pack = make_pack_expansion (expr);
12389 /* Build the fold expression. */
12390 tree code = build_int_cstu (integer_type_node, abs (op));
12391 tree fold = build_min_nt_loc (input_location, dir, code, pack);
12392 FOLD_EXPR_MODIFY_P (fold) = (op < 0);
12393 TREE_TYPE (fold) = build_dependent_operator_type (NULL_TREE,
12394 FOLD_EXPR_OP (fold),
12395 FOLD_EXPR_MODIFY_P (fold));
12396 return fold;
12399 tree
12400 finish_left_unary_fold_expr (tree expr, int op)
12402 return finish_unary_fold_expr (expr, op, UNARY_LEFT_FOLD_EXPR);
12405 tree
12406 finish_right_unary_fold_expr (tree expr, int op)
12408 return finish_unary_fold_expr (expr, op, UNARY_RIGHT_FOLD_EXPR);
12411 /* Build a binary fold expression over EXPR1 and EXPR2. The
12412 associativity of the fold is determined by EXPR1 and EXPR2 (whichever
12413 has an unexpanded parameter pack). */
12415 tree
12416 finish_binary_fold_expr (tree pack, tree init, int op, tree_code dir)
12418 pack = make_pack_expansion (pack);
12419 tree code = build_int_cstu (integer_type_node, abs (op));
12420 tree fold = build_min_nt_loc (input_location, dir, code, pack, init);
12421 FOLD_EXPR_MODIFY_P (fold) = (op < 0);
12422 TREE_TYPE (fold) = build_dependent_operator_type (NULL_TREE,
12423 FOLD_EXPR_OP (fold),
12424 FOLD_EXPR_MODIFY_P (fold));
12425 return fold;
12428 tree
12429 finish_binary_fold_expr (tree expr1, tree expr2, int op)
12431 // Determine which expr has an unexpanded parameter pack and
12432 // set the pack and initial term.
12433 bool pack1 = uses_parameter_packs (expr1);
12434 bool pack2 = uses_parameter_packs (expr2);
12435 if (pack1 && !pack2)
12436 return finish_binary_fold_expr (expr1, expr2, op, BINARY_RIGHT_FOLD_EXPR);
12437 else if (pack2 && !pack1)
12438 return finish_binary_fold_expr (expr2, expr1, op, BINARY_LEFT_FOLD_EXPR);
12439 else
12441 if (pack1)
12442 error ("both arguments in binary fold have unexpanded parameter packs");
12443 else
12444 error ("no unexpanded parameter packs in binary fold");
12446 return error_mark_node;
12449 /* Finish __builtin_launder (arg). */
12451 tree
12452 finish_builtin_launder (location_t loc, tree arg, tsubst_flags_t complain)
12454 tree orig_arg = arg;
12455 if (!type_dependent_expression_p (arg))
12456 arg = decay_conversion (arg, complain);
12457 if (error_operand_p (arg))
12458 return error_mark_node;
12459 if (!type_dependent_expression_p (arg)
12460 && !TYPE_PTR_P (TREE_TYPE (arg)))
12462 error_at (loc, "non-pointer argument to %<__builtin_launder%>");
12463 return error_mark_node;
12465 if (processing_template_decl)
12466 arg = orig_arg;
12467 return build_call_expr_internal_loc (loc, IFN_LAUNDER,
12468 TREE_TYPE (arg), 1, arg);
12471 /* Finish __builtin_convertvector (arg, type). */
12473 tree
12474 cp_build_vec_convert (tree arg, location_t loc, tree type,
12475 tsubst_flags_t complain)
12477 if (error_operand_p (type))
12478 return error_mark_node;
12479 if (error_operand_p (arg))
12480 return error_mark_node;
12482 tree ret = NULL_TREE;
12483 if (!type_dependent_expression_p (arg) && !dependent_type_p (type))
12484 ret = c_build_vec_convert (cp_expr_loc_or_input_loc (arg),
12485 decay_conversion (arg, complain),
12486 loc, type, (complain & tf_error) != 0);
12488 if (!processing_template_decl)
12489 return ret;
12491 return build_call_expr_internal_loc (loc, IFN_VEC_CONVERT, type, 1, arg);
12494 /* Finish __builtin_bit_cast (type, arg). */
12496 tree
12497 cp_build_bit_cast (location_t loc, tree type, tree arg,
12498 tsubst_flags_t complain)
12500 if (error_operand_p (type))
12501 return error_mark_node;
12502 if (!dependent_type_p (type))
12504 if (!complete_type_or_maybe_complain (type, NULL_TREE, complain))
12505 return error_mark_node;
12506 if (TREE_CODE (type) == ARRAY_TYPE)
12508 /* std::bit_cast for destination ARRAY_TYPE is not possible,
12509 as functions may not return an array, so don't bother trying
12510 to support this (and then deal with VLAs etc.). */
12511 error_at (loc, "%<__builtin_bit_cast%> destination type %qT "
12512 "is an array type", type);
12513 return error_mark_node;
12515 if (!trivially_copyable_p (type))
12517 error_at (loc, "%<__builtin_bit_cast%> destination type %qT "
12518 "is not trivially copyable", type);
12519 return error_mark_node;
12523 if (error_operand_p (arg))
12524 return error_mark_node;
12526 if (!type_dependent_expression_p (arg))
12528 if (TREE_CODE (TREE_TYPE (arg)) == ARRAY_TYPE)
12530 /* Don't perform array-to-pointer conversion. */
12531 arg = mark_rvalue_use (arg, loc, true);
12532 if (!complete_type_or_maybe_complain (TREE_TYPE (arg), arg, complain))
12533 return error_mark_node;
12535 else
12536 arg = decay_conversion (arg, complain);
12538 if (error_operand_p (arg))
12539 return error_mark_node;
12541 if (!trivially_copyable_p (TREE_TYPE (arg)))
12543 error_at (cp_expr_loc_or_loc (arg, loc),
12544 "%<__builtin_bit_cast%> source type %qT "
12545 "is not trivially copyable", TREE_TYPE (arg));
12546 return error_mark_node;
12548 if (!dependent_type_p (type)
12549 && !cp_tree_equal (TYPE_SIZE_UNIT (type),
12550 TYPE_SIZE_UNIT (TREE_TYPE (arg))))
12552 error_at (loc, "%<__builtin_bit_cast%> source size %qE "
12553 "not equal to destination type size %qE",
12554 TYPE_SIZE_UNIT (TREE_TYPE (arg)),
12555 TYPE_SIZE_UNIT (type));
12556 return error_mark_node;
12560 tree ret = build_min (BIT_CAST_EXPR, type, arg);
12561 SET_EXPR_LOCATION (ret, loc);
12563 if (!processing_template_decl && CLASS_TYPE_P (type))
12564 ret = get_target_expr (ret, complain);
12566 return ret;
12569 #include "gt-cp-semantics.h"