Don't warn when alignment of global common data exceeds maximum alignment.
[official-gcc.git] / gcc / cp / semantics.c
blobe191aa36c98fa4ea88bfb88bd3f94fa31e2c0bc8
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-2021 Free Software Foundation, Inc.
7 Written by Mark Mitchell (mmitchell@usa.net) based on code found
8 formerly in parse.y and pt.c.
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 protected_set_expr_location (CLEANUP_EXPR (stmts), loc);
613 set_cleanup_locs (CLEANUP_BODY (stmts), loc);
615 else if (TREE_CODE (stmts) == STATEMENT_LIST)
616 for (tree stmt : tsi_range (stmts))
617 set_cleanup_locs (stmt, loc);
620 /* Finish a scope. */
622 tree
623 do_poplevel (tree stmt_list)
625 tree block = NULL;
627 if (stmts_are_full_exprs_p ())
628 block = poplevel (kept_level_p (), 1, 0);
630 stmt_list = pop_stmt_list (stmt_list);
632 /* input_location is the last token of the scope, usually a }. */
633 set_cleanup_locs (stmt_list, input_location);
635 if (!processing_template_decl)
637 stmt_list = c_build_bind_expr (input_location, block, stmt_list);
638 /* ??? See c_end_compound_stmt re statement expressions. */
641 return stmt_list;
644 /* Begin a new scope. */
646 static tree
647 do_pushlevel (scope_kind sk)
649 tree ret = push_stmt_list ();
650 if (stmts_are_full_exprs_p ())
651 begin_scope (sk, NULL);
652 return ret;
655 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
656 when the current scope is exited. EH_ONLY is true when this is not
657 meant to apply to normal control flow transfer. */
659 void
660 push_cleanup (tree decl, tree cleanup, bool eh_only)
662 tree stmt = build_stmt (input_location, CLEANUP_STMT, NULL, cleanup, decl);
663 CLEANUP_EH_ONLY (stmt) = eh_only;
664 add_stmt (stmt);
665 CLEANUP_BODY (stmt) = push_stmt_list ();
668 /* Simple infinite loop tracking for -Wreturn-type. We keep a stack of all
669 the current loops, represented by 'NULL_TREE' if we've seen a possible
670 exit, and 'error_mark_node' if not. This is currently used only to
671 suppress the warning about a function with no return statements, and
672 therefore we don't bother noting returns as possible exits. We also
673 don't bother with gotos. */
675 static void
676 begin_maybe_infinite_loop (tree cond)
678 /* Only track this while parsing a function, not during instantiation. */
679 if (!cfun || (DECL_TEMPLATE_INSTANTIATION (current_function_decl)
680 && !processing_template_decl))
681 return;
682 bool maybe_infinite = true;
683 if (cond)
685 cond = fold_non_dependent_expr (cond);
686 maybe_infinite = integer_nonzerop (cond);
688 vec_safe_push (cp_function_chain->infinite_loops,
689 maybe_infinite ? error_mark_node : NULL_TREE);
693 /* A break is a possible exit for the current loop. */
695 void
696 break_maybe_infinite_loop (void)
698 if (!cfun)
699 return;
700 cp_function_chain->infinite_loops->last() = NULL_TREE;
703 /* If we reach the end of the loop without seeing a possible exit, we have
704 an infinite loop. */
706 static void
707 end_maybe_infinite_loop (tree cond)
709 if (!cfun || (DECL_TEMPLATE_INSTANTIATION (current_function_decl)
710 && !processing_template_decl))
711 return;
712 tree current = cp_function_chain->infinite_loops->pop();
713 if (current != NULL_TREE)
715 cond = fold_non_dependent_expr (cond);
716 if (integer_nonzerop (cond))
717 current_function_infinite_loop = 1;
722 /* Begin a conditional that might contain a declaration. When generating
723 normal code, we want the declaration to appear before the statement
724 containing the conditional. When generating template code, we want the
725 conditional to be rendered as the raw DECL_EXPR. */
727 static void
728 begin_cond (tree *cond_p)
730 if (processing_template_decl)
731 *cond_p = push_stmt_list ();
734 /* Finish such a conditional. */
736 static void
737 finish_cond (tree *cond_p, tree expr)
739 if (processing_template_decl)
741 tree cond = pop_stmt_list (*cond_p);
743 if (expr == NULL_TREE)
744 /* Empty condition in 'for'. */
745 gcc_assert (empty_expr_stmt_p (cond));
746 else if (check_for_bare_parameter_packs (expr))
747 expr = error_mark_node;
748 else if (!empty_expr_stmt_p (cond))
749 expr = build2 (COMPOUND_EXPR, TREE_TYPE (expr), cond, expr);
751 *cond_p = expr;
754 /* If *COND_P specifies a conditional with a declaration, transform the
755 loop such that
756 while (A x = 42) { }
757 for (; A x = 42;) { }
758 becomes
759 while (true) { A x = 42; if (!x) break; }
760 for (;;) { A x = 42; if (!x) break; }
761 The statement list for BODY will be empty if the conditional did
762 not declare anything. */
764 static void
765 simplify_loop_decl_cond (tree *cond_p, tree body)
767 tree cond, if_stmt;
769 if (!TREE_SIDE_EFFECTS (body))
770 return;
772 cond = *cond_p;
773 *cond_p = boolean_true_node;
775 if_stmt = begin_if_stmt ();
776 cond = cp_build_unary_op (TRUTH_NOT_EXPR, cond, false, tf_warning_or_error);
777 finish_if_stmt_cond (cond, if_stmt);
778 finish_break_stmt ();
779 finish_then_clause (if_stmt);
780 finish_if_stmt (if_stmt);
783 /* Finish a goto-statement. */
785 tree
786 finish_goto_stmt (tree destination)
788 if (identifier_p (destination))
789 destination = lookup_label (destination);
791 /* We warn about unused labels with -Wunused. That means we have to
792 mark the used labels as used. */
793 if (TREE_CODE (destination) == LABEL_DECL)
794 TREE_USED (destination) = 1;
795 else
797 destination = mark_rvalue_use (destination);
798 if (!processing_template_decl)
800 destination = cp_convert (ptr_type_node, destination,
801 tf_warning_or_error);
802 if (error_operand_p (destination))
803 return NULL_TREE;
804 destination
805 = fold_build_cleanup_point_expr (TREE_TYPE (destination),
806 destination);
810 check_goto (destination);
812 add_stmt (build_predict_expr (PRED_GOTO, NOT_TAKEN));
813 return add_stmt (build_stmt (input_location, GOTO_EXPR, destination));
816 /* COND is the condition-expression for an if, while, etc.,
817 statement. Convert it to a boolean value, if appropriate.
818 In addition, verify sequence points if -Wsequence-point is enabled. */
820 static tree
821 maybe_convert_cond (tree cond)
823 /* Empty conditions remain empty. */
824 if (!cond)
825 return NULL_TREE;
827 /* Wait until we instantiate templates before doing conversion. */
828 if (type_dependent_expression_p (cond))
829 return cond;
831 if (warn_sequence_point && !processing_template_decl)
832 verify_sequence_points (cond);
834 /* Do the conversion. */
835 cond = convert_from_reference (cond);
837 if (TREE_CODE (cond) == MODIFY_EXPR
838 && warn_parentheses
839 && !warning_suppressed_p (cond, OPT_Wparentheses)
840 && warning_at (cp_expr_loc_or_input_loc (cond),
841 OPT_Wparentheses, "suggest parentheses around "
842 "assignment used as truth value"))
843 suppress_warning (cond, OPT_Wparentheses);
845 return condition_conversion (cond);
848 /* Finish an expression-statement, whose EXPRESSION is as indicated. */
850 tree
851 finish_expr_stmt (tree expr)
853 tree r = NULL_TREE;
854 location_t loc = EXPR_LOCATION (expr);
856 if (expr != NULL_TREE)
858 /* If we ran into a problem, make sure we complained. */
859 gcc_assert (expr != error_mark_node || seen_error ());
861 if (!processing_template_decl)
863 if (warn_sequence_point)
864 verify_sequence_points (expr);
865 expr = convert_to_void (expr, ICV_STATEMENT, tf_warning_or_error);
867 else if (!type_dependent_expression_p (expr))
868 convert_to_void (build_non_dependent_expr (expr), ICV_STATEMENT,
869 tf_warning_or_error);
871 if (check_for_bare_parameter_packs (expr))
872 expr = error_mark_node;
874 /* Simplification of inner statement expressions, compound exprs,
875 etc can result in us already having an EXPR_STMT. */
876 if (TREE_CODE (expr) != CLEANUP_POINT_EXPR)
878 if (TREE_CODE (expr) != EXPR_STMT)
879 expr = build_stmt (loc, EXPR_STMT, expr);
880 expr = maybe_cleanup_point_expr_void (expr);
883 r = add_stmt (expr);
886 return r;
890 /* Begin an if-statement. Returns a newly created IF_STMT if
891 appropriate. */
893 tree
894 begin_if_stmt (void)
896 tree r, scope;
897 scope = do_pushlevel (sk_cond);
898 r = build_stmt (input_location, IF_STMT, NULL_TREE,
899 NULL_TREE, NULL_TREE, scope);
900 current_binding_level->this_entity = r;
901 begin_cond (&IF_COND (r));
902 return r;
905 /* Returns true if FN, a CALL_EXPR, is a call to
906 std::is_constant_evaluated or __builtin_is_constant_evaluated. */
908 static bool
909 is_std_constant_evaluated_p (tree fn)
911 /* std::is_constant_evaluated takes no arguments. */
912 if (call_expr_nargs (fn) != 0)
913 return false;
915 tree fndecl = cp_get_callee_fndecl_nofold (fn);
916 if (fndecl == NULL_TREE)
917 return false;
919 if (fndecl_built_in_p (fndecl, CP_BUILT_IN_IS_CONSTANT_EVALUATED,
920 BUILT_IN_FRONTEND))
921 return true;
923 if (!decl_in_std_namespace_p (fndecl))
924 return false;
926 tree name = DECL_NAME (fndecl);
927 return name && id_equal (name, "is_constant_evaluated");
930 /* Callback function for maybe_warn_for_constant_evaluated that looks
931 for calls to std::is_constant_evaluated in TP. */
933 static tree
934 find_std_constant_evaluated_r (tree *tp, int *walk_subtrees, void *)
936 tree t = *tp;
938 if (TYPE_P (t) || TREE_CONSTANT (t))
940 *walk_subtrees = false;
941 return NULL_TREE;
944 switch (TREE_CODE (t))
946 case CALL_EXPR:
947 if (is_std_constant_evaluated_p (t))
948 return t;
949 break;
950 case EXPR_STMT:
951 /* Don't warn in statement expressions. */
952 *walk_subtrees = false;
953 return NULL_TREE;
954 default:
955 break;
958 return NULL_TREE;
961 /* In certain contexts, std::is_constant_evaluated() is always true (for
962 instance, in a consteval function or in a constexpr if), or always false
963 (e.g., in a non-constexpr non-consteval function) so give the user a clue. */
965 static void
966 maybe_warn_for_constant_evaluated (tree cond, bool constexpr_if)
968 if (!warn_tautological_compare)
969 return;
971 /* Suppress warning for std::is_constant_evaluated if the conditional
972 comes from a macro. */
973 if (from_macro_expansion_at (EXPR_LOCATION (cond)))
974 return;
976 cond = cp_walk_tree_without_duplicates (&cond, find_std_constant_evaluated_r,
977 NULL);
978 if (cond)
980 if (constexpr_if)
981 warning_at (EXPR_LOCATION (cond), OPT_Wtautological_compare,
982 "%<std::is_constant_evaluated%> always evaluates to "
983 "true in %<if constexpr%>");
984 else if (!maybe_constexpr_fn (current_function_decl))
985 warning_at (EXPR_LOCATION (cond), OPT_Wtautological_compare,
986 "%<std::is_constant_evaluated%> always evaluates to "
987 "false in a non-%<constexpr%> function");
988 else if (DECL_IMMEDIATE_FUNCTION_P (current_function_decl))
989 warning_at (EXPR_LOCATION (cond), OPT_Wtautological_compare,
990 "%<std::is_constant_evaluated%> always evaluates to "
991 "true in a %<consteval%> function");
995 /* Process the COND of an if-statement, which may be given by
996 IF_STMT. */
998 tree
999 finish_if_stmt_cond (tree cond, tree if_stmt)
1001 cond = maybe_convert_cond (cond);
1002 if (IF_STMT_CONSTEXPR_P (if_stmt)
1003 && !type_dependent_expression_p (cond)
1004 && require_constant_expression (cond)
1005 && !instantiation_dependent_expression_p (cond)
1006 /* Wait until instantiation time, since only then COND has been
1007 converted to bool. */
1008 && TYPE_MAIN_VARIANT (TREE_TYPE (cond)) == boolean_type_node)
1010 maybe_warn_for_constant_evaluated (cond, /*constexpr_if=*/true);
1011 cond = instantiate_non_dependent_expr (cond);
1012 cond = cxx_constant_value (cond, NULL_TREE);
1014 else
1015 maybe_warn_for_constant_evaluated (cond, /*constexpr_if=*/false);
1016 finish_cond (&IF_COND (if_stmt), cond);
1017 add_stmt (if_stmt);
1018 THEN_CLAUSE (if_stmt) = push_stmt_list ();
1019 return cond;
1022 /* Finish the then-clause of an if-statement, which may be given by
1023 IF_STMT. */
1025 tree
1026 finish_then_clause (tree if_stmt)
1028 THEN_CLAUSE (if_stmt) = pop_stmt_list (THEN_CLAUSE (if_stmt));
1029 return if_stmt;
1032 /* Begin the else-clause of an if-statement. */
1034 void
1035 begin_else_clause (tree if_stmt)
1037 ELSE_CLAUSE (if_stmt) = push_stmt_list ();
1040 /* Finish the else-clause of an if-statement, which may be given by
1041 IF_STMT. */
1043 void
1044 finish_else_clause (tree if_stmt)
1046 ELSE_CLAUSE (if_stmt) = pop_stmt_list (ELSE_CLAUSE (if_stmt));
1049 /* Callback for cp_walk_tree to mark all {VAR,PARM}_DECLs in a tree as
1050 read. */
1052 static tree
1053 maybe_mark_exp_read_r (tree *tp, int *, void *)
1055 tree t = *tp;
1056 if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
1057 mark_exp_read (t);
1058 return NULL_TREE;
1061 /* Finish an if-statement. */
1063 void
1064 finish_if_stmt (tree if_stmt)
1066 tree scope = IF_SCOPE (if_stmt);
1067 IF_SCOPE (if_stmt) = NULL;
1068 if (IF_STMT_CONSTEXPR_P (if_stmt))
1070 /* Prevent various -Wunused warnings. We might not instantiate
1071 either of these branches, so we would not mark the variables
1072 used in that branch as read. */
1073 cp_walk_tree_without_duplicates (&THEN_CLAUSE (if_stmt),
1074 maybe_mark_exp_read_r, NULL);
1075 cp_walk_tree_without_duplicates (&ELSE_CLAUSE (if_stmt),
1076 maybe_mark_exp_read_r, NULL);
1078 add_stmt (do_poplevel (scope));
1081 /* Begin a while-statement. Returns a newly created WHILE_STMT if
1082 appropriate. */
1084 tree
1085 begin_while_stmt (void)
1087 tree r;
1088 r = build_stmt (input_location, WHILE_STMT, NULL_TREE, NULL_TREE);
1089 add_stmt (r);
1090 WHILE_BODY (r) = do_pushlevel (sk_block);
1091 begin_cond (&WHILE_COND (r));
1092 return r;
1095 /* Process the COND of a while-statement, which may be given by
1096 WHILE_STMT. */
1098 void
1099 finish_while_stmt_cond (tree cond, tree while_stmt, bool ivdep,
1100 unsigned short unroll)
1102 cond = maybe_convert_cond (cond);
1103 finish_cond (&WHILE_COND (while_stmt), cond);
1104 begin_maybe_infinite_loop (cond);
1105 if (ivdep && cond != error_mark_node)
1106 WHILE_COND (while_stmt) = build3 (ANNOTATE_EXPR,
1107 TREE_TYPE (WHILE_COND (while_stmt)),
1108 WHILE_COND (while_stmt),
1109 build_int_cst (integer_type_node,
1110 annot_expr_ivdep_kind),
1111 integer_zero_node);
1112 if (unroll && cond != error_mark_node)
1113 WHILE_COND (while_stmt) = build3 (ANNOTATE_EXPR,
1114 TREE_TYPE (WHILE_COND (while_stmt)),
1115 WHILE_COND (while_stmt),
1116 build_int_cst (integer_type_node,
1117 annot_expr_unroll_kind),
1118 build_int_cst (integer_type_node,
1119 unroll));
1120 simplify_loop_decl_cond (&WHILE_COND (while_stmt), WHILE_BODY (while_stmt));
1123 /* Finish a while-statement, which may be given by WHILE_STMT. */
1125 void
1126 finish_while_stmt (tree while_stmt)
1128 end_maybe_infinite_loop (boolean_true_node);
1129 WHILE_BODY (while_stmt) = do_poplevel (WHILE_BODY (while_stmt));
1132 /* Begin a do-statement. Returns a newly created DO_STMT if
1133 appropriate. */
1135 tree
1136 begin_do_stmt (void)
1138 tree r = build_stmt (input_location, DO_STMT, NULL_TREE, NULL_TREE);
1139 begin_maybe_infinite_loop (boolean_true_node);
1140 add_stmt (r);
1141 DO_BODY (r) = push_stmt_list ();
1142 return r;
1145 /* Finish the body of a do-statement, which may be given by DO_STMT. */
1147 void
1148 finish_do_body (tree do_stmt)
1150 tree body = DO_BODY (do_stmt) = pop_stmt_list (DO_BODY (do_stmt));
1152 if (TREE_CODE (body) == STATEMENT_LIST && STATEMENT_LIST_TAIL (body))
1153 body = STATEMENT_LIST_TAIL (body)->stmt;
1155 if (IS_EMPTY_STMT (body))
1156 warning (OPT_Wempty_body,
1157 "suggest explicit braces around empty body in %<do%> statement");
1160 /* Finish a do-statement, which may be given by DO_STMT, and whose
1161 COND is as indicated. */
1163 void
1164 finish_do_stmt (tree cond, tree do_stmt, bool ivdep, unsigned short unroll)
1166 cond = maybe_convert_cond (cond);
1167 end_maybe_infinite_loop (cond);
1168 /* Unlike other iteration statements, the condition may not contain
1169 a declaration, so we don't call finish_cond which checks for
1170 unexpanded parameter packs. */
1171 if (check_for_bare_parameter_packs (cond))
1172 cond = error_mark_node;
1173 if (ivdep && cond != error_mark_node)
1174 cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
1175 build_int_cst (integer_type_node, annot_expr_ivdep_kind),
1176 integer_zero_node);
1177 if (unroll && cond != error_mark_node)
1178 cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
1179 build_int_cst (integer_type_node, annot_expr_unroll_kind),
1180 build_int_cst (integer_type_node, unroll));
1181 DO_COND (do_stmt) = cond;
1184 /* Finish a return-statement. The EXPRESSION returned, if any, is as
1185 indicated. */
1187 tree
1188 finish_return_stmt (tree expr)
1190 tree r;
1191 bool no_warning;
1193 expr = check_return_expr (expr, &no_warning);
1195 if (error_operand_p (expr)
1196 || (flag_openmp && !check_omp_return ()))
1198 /* Suppress -Wreturn-type for this function. */
1199 if (warn_return_type)
1200 suppress_warning (current_function_decl, OPT_Wreturn_type);
1201 return error_mark_node;
1204 if (!processing_template_decl)
1206 if (warn_sequence_point)
1207 verify_sequence_points (expr);
1209 if (DECL_DESTRUCTOR_P (current_function_decl)
1210 || (DECL_CONSTRUCTOR_P (current_function_decl)
1211 && targetm.cxx.cdtor_returns_this ()))
1213 /* Similarly, all destructors must run destructors for
1214 base-classes before returning. So, all returns in a
1215 destructor get sent to the DTOR_LABEL; finish_function emits
1216 code to return a value there. */
1217 return finish_goto_stmt (cdtor_label);
1221 r = build_stmt (input_location, RETURN_EXPR, expr);
1222 if (no_warning)
1223 suppress_warning (r, OPT_Wreturn_type);
1224 r = maybe_cleanup_point_expr_void (r);
1225 r = add_stmt (r);
1227 return r;
1230 /* Begin the scope of a for-statement or a range-for-statement.
1231 Both the returned trees are to be used in a call to
1232 begin_for_stmt or begin_range_for_stmt. */
1234 tree
1235 begin_for_scope (tree *init)
1237 tree scope = do_pushlevel (sk_for);
1239 if (processing_template_decl)
1240 *init = push_stmt_list ();
1241 else
1242 *init = NULL_TREE;
1244 return scope;
1247 /* Begin a for-statement. Returns a new FOR_STMT.
1248 SCOPE and INIT should be the return of begin_for_scope,
1249 or both NULL_TREE */
1251 tree
1252 begin_for_stmt (tree scope, tree init)
1254 tree r;
1256 r = build_stmt (input_location, FOR_STMT, NULL_TREE, NULL_TREE,
1257 NULL_TREE, NULL_TREE, NULL_TREE);
1259 if (scope == NULL_TREE)
1261 gcc_assert (!init);
1262 scope = begin_for_scope (&init);
1265 FOR_INIT_STMT (r) = init;
1266 FOR_SCOPE (r) = scope;
1268 return r;
1271 /* Finish the init-statement of a for-statement, which may be
1272 given by FOR_STMT. */
1274 void
1275 finish_init_stmt (tree for_stmt)
1277 if (processing_template_decl)
1278 FOR_INIT_STMT (for_stmt) = pop_stmt_list (FOR_INIT_STMT (for_stmt));
1279 add_stmt (for_stmt);
1280 FOR_BODY (for_stmt) = do_pushlevel (sk_block);
1281 begin_cond (&FOR_COND (for_stmt));
1284 /* Finish the COND of a for-statement, which may be given by
1285 FOR_STMT. */
1287 void
1288 finish_for_cond (tree cond, tree for_stmt, bool ivdep, unsigned short unroll)
1290 cond = maybe_convert_cond (cond);
1291 finish_cond (&FOR_COND (for_stmt), cond);
1292 begin_maybe_infinite_loop (cond);
1293 if (ivdep && cond != error_mark_node)
1294 FOR_COND (for_stmt) = build3 (ANNOTATE_EXPR,
1295 TREE_TYPE (FOR_COND (for_stmt)),
1296 FOR_COND (for_stmt),
1297 build_int_cst (integer_type_node,
1298 annot_expr_ivdep_kind),
1299 integer_zero_node);
1300 if (unroll && cond != error_mark_node)
1301 FOR_COND (for_stmt) = build3 (ANNOTATE_EXPR,
1302 TREE_TYPE (FOR_COND (for_stmt)),
1303 FOR_COND (for_stmt),
1304 build_int_cst (integer_type_node,
1305 annot_expr_unroll_kind),
1306 build_int_cst (integer_type_node,
1307 unroll));
1308 simplify_loop_decl_cond (&FOR_COND (for_stmt), FOR_BODY (for_stmt));
1311 /* Finish the increment-EXPRESSION in a for-statement, which may be
1312 given by FOR_STMT. */
1314 void
1315 finish_for_expr (tree expr, tree for_stmt)
1317 if (!expr)
1318 return;
1319 /* If EXPR is an overloaded function, issue an error; there is no
1320 context available to use to perform overload resolution. */
1321 if (type_unknown_p (expr))
1323 cxx_incomplete_type_error (expr, TREE_TYPE (expr));
1324 expr = error_mark_node;
1326 if (!processing_template_decl)
1328 if (warn_sequence_point)
1329 verify_sequence_points (expr);
1330 expr = convert_to_void (expr, ICV_THIRD_IN_FOR,
1331 tf_warning_or_error);
1333 else if (!type_dependent_expression_p (expr))
1334 convert_to_void (build_non_dependent_expr (expr), ICV_THIRD_IN_FOR,
1335 tf_warning_or_error);
1336 expr = maybe_cleanup_point_expr_void (expr);
1337 if (check_for_bare_parameter_packs (expr))
1338 expr = error_mark_node;
1339 FOR_EXPR (for_stmt) = expr;
1342 /* Finish the body of a for-statement, which may be given by
1343 FOR_STMT. The increment-EXPR for the loop must be
1344 provided.
1345 It can also finish RANGE_FOR_STMT. */
1347 void
1348 finish_for_stmt (tree for_stmt)
1350 end_maybe_infinite_loop (boolean_true_node);
1352 if (TREE_CODE (for_stmt) == RANGE_FOR_STMT)
1353 RANGE_FOR_BODY (for_stmt) = do_poplevel (RANGE_FOR_BODY (for_stmt));
1354 else
1355 FOR_BODY (for_stmt) = do_poplevel (FOR_BODY (for_stmt));
1357 /* Pop the scope for the body of the loop. */
1358 tree *scope_ptr = (TREE_CODE (for_stmt) == RANGE_FOR_STMT
1359 ? &RANGE_FOR_SCOPE (for_stmt)
1360 : &FOR_SCOPE (for_stmt));
1361 tree scope = *scope_ptr;
1362 *scope_ptr = NULL;
1364 /* During parsing of the body, range for uses "__for_{range,begin,end} "
1365 decl names to make those unaccessible by code in the body.
1366 Change it to ones with underscore instead of space, so that it can
1367 be inspected in the debugger. */
1368 tree range_for_decl[3] = { NULL_TREE, NULL_TREE, NULL_TREE };
1369 gcc_assert (CPTI_FOR_BEGIN__IDENTIFIER == CPTI_FOR_RANGE__IDENTIFIER + 1
1370 && CPTI_FOR_END__IDENTIFIER == CPTI_FOR_RANGE__IDENTIFIER + 2
1371 && CPTI_FOR_RANGE_IDENTIFIER == CPTI_FOR_RANGE__IDENTIFIER + 3
1372 && CPTI_FOR_BEGIN_IDENTIFIER == CPTI_FOR_BEGIN__IDENTIFIER + 3
1373 && CPTI_FOR_END_IDENTIFIER == CPTI_FOR_END__IDENTIFIER + 3);
1374 for (int i = 0; i < 3; i++)
1376 tree id = cp_global_trees[CPTI_FOR_RANGE__IDENTIFIER + i];
1377 if (IDENTIFIER_BINDING (id)
1378 && IDENTIFIER_BINDING (id)->scope == current_binding_level)
1380 range_for_decl[i] = IDENTIFIER_BINDING (id)->value;
1381 gcc_assert (VAR_P (range_for_decl[i])
1382 && DECL_ARTIFICIAL (range_for_decl[i]));
1386 add_stmt (do_poplevel (scope));
1388 for (int i = 0; i < 3; i++)
1389 if (range_for_decl[i])
1390 DECL_NAME (range_for_decl[i])
1391 = cp_global_trees[CPTI_FOR_RANGE_IDENTIFIER + i];
1394 /* Begin a range-for-statement. Returns a new RANGE_FOR_STMT.
1395 SCOPE and INIT should be the return of begin_for_scope,
1396 or both NULL_TREE .
1397 To finish it call finish_for_stmt(). */
1399 tree
1400 begin_range_for_stmt (tree scope, tree init)
1402 begin_maybe_infinite_loop (boolean_false_node);
1404 tree r = build_stmt (input_location, RANGE_FOR_STMT, NULL_TREE, NULL_TREE,
1405 NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE);
1407 if (scope == NULL_TREE)
1409 gcc_assert (!init);
1410 scope = begin_for_scope (&init);
1413 /* Since C++20, RANGE_FOR_STMTs can use the init tree, so save it. */
1414 RANGE_FOR_INIT_STMT (r) = init;
1415 RANGE_FOR_SCOPE (r) = scope;
1417 return r;
1420 /* Finish the head of a range-based for statement, which may
1421 be given by RANGE_FOR_STMT. DECL must be the declaration
1422 and EXPR must be the loop expression. */
1424 void
1425 finish_range_for_decl (tree range_for_stmt, tree decl, tree expr)
1427 if (processing_template_decl)
1428 RANGE_FOR_INIT_STMT (range_for_stmt)
1429 = pop_stmt_list (RANGE_FOR_INIT_STMT (range_for_stmt));
1430 RANGE_FOR_DECL (range_for_stmt) = decl;
1431 RANGE_FOR_EXPR (range_for_stmt) = expr;
1432 add_stmt (range_for_stmt);
1433 RANGE_FOR_BODY (range_for_stmt) = do_pushlevel (sk_block);
1436 /* Finish a break-statement. */
1438 tree
1439 finish_break_stmt (void)
1441 /* In switch statements break is sometimes stylistically used after
1442 a return statement. This can lead to spurious warnings about
1443 control reaching the end of a non-void function when it is
1444 inlined. Note that we are calling block_may_fallthru with
1445 language specific tree nodes; this works because
1446 block_may_fallthru returns true when given something it does not
1447 understand. */
1448 if (!block_may_fallthru (cur_stmt_list))
1449 return void_node;
1450 note_break_stmt ();
1451 return add_stmt (build_stmt (input_location, BREAK_STMT));
1454 /* Finish a continue-statement. */
1456 tree
1457 finish_continue_stmt (void)
1459 return add_stmt (build_stmt (input_location, CONTINUE_STMT));
1462 /* Begin a switch-statement. Returns a new SWITCH_STMT if
1463 appropriate. */
1465 tree
1466 begin_switch_stmt (void)
1468 tree r, scope;
1470 scope = do_pushlevel (sk_cond);
1471 r = build_stmt (input_location, SWITCH_STMT, NULL_TREE, NULL_TREE, NULL_TREE, scope);
1473 begin_cond (&SWITCH_STMT_COND (r));
1475 return r;
1478 /* Finish the cond of a switch-statement. */
1480 void
1481 finish_switch_cond (tree cond, tree switch_stmt)
1483 tree orig_type = NULL;
1485 if (!processing_template_decl)
1487 /* Convert the condition to an integer or enumeration type. */
1488 tree orig_cond = cond;
1489 cond = build_expr_type_conversion (WANT_INT | WANT_ENUM, cond, true);
1490 if (cond == NULL_TREE)
1492 error_at (cp_expr_loc_or_input_loc (orig_cond),
1493 "switch quantity not an integer");
1494 cond = error_mark_node;
1496 /* We want unlowered type here to handle enum bit-fields. */
1497 orig_type = unlowered_expr_type (cond);
1498 if (TREE_CODE (orig_type) != ENUMERAL_TYPE)
1499 orig_type = TREE_TYPE (cond);
1500 if (cond != error_mark_node)
1502 /* [stmt.switch]
1504 Integral promotions are performed. */
1505 cond = perform_integral_promotions (cond);
1506 cond = maybe_cleanup_point_expr (cond);
1509 if (check_for_bare_parameter_packs (cond))
1510 cond = error_mark_node;
1511 else if (!processing_template_decl && warn_sequence_point)
1512 verify_sequence_points (cond);
1514 finish_cond (&SWITCH_STMT_COND (switch_stmt), cond);
1515 SWITCH_STMT_TYPE (switch_stmt) = orig_type;
1516 add_stmt (switch_stmt);
1517 push_switch (switch_stmt);
1518 SWITCH_STMT_BODY (switch_stmt) = push_stmt_list ();
1521 /* Finish the body of a switch-statement, which may be given by
1522 SWITCH_STMT. The COND to switch on is indicated. */
1524 void
1525 finish_switch_stmt (tree switch_stmt)
1527 tree scope;
1529 SWITCH_STMT_BODY (switch_stmt) =
1530 pop_stmt_list (SWITCH_STMT_BODY (switch_stmt));
1531 pop_switch ();
1533 scope = SWITCH_STMT_SCOPE (switch_stmt);
1534 SWITCH_STMT_SCOPE (switch_stmt) = NULL;
1535 add_stmt (do_poplevel (scope));
1538 /* Begin a try-block. Returns a newly-created TRY_BLOCK if
1539 appropriate. */
1541 tree
1542 begin_try_block (void)
1544 tree r = build_stmt (input_location, TRY_BLOCK, NULL_TREE, NULL_TREE);
1545 add_stmt (r);
1546 TRY_STMTS (r) = push_stmt_list ();
1547 return r;
1550 /* Likewise, for a function-try-block. The block returned in
1551 *COMPOUND_STMT is an artificial outer scope, containing the
1552 function-try-block. */
1554 tree
1555 begin_function_try_block (tree *compound_stmt)
1557 tree r;
1558 /* This outer scope does not exist in the C++ standard, but we need
1559 a place to put __FUNCTION__ and similar variables. */
1560 *compound_stmt = begin_compound_stmt (0);
1561 r = begin_try_block ();
1562 FN_TRY_BLOCK_P (r) = 1;
1563 return r;
1566 /* Finish a try-block, which may be given by TRY_BLOCK. */
1568 void
1569 finish_try_block (tree try_block)
1571 TRY_STMTS (try_block) = pop_stmt_list (TRY_STMTS (try_block));
1572 TRY_HANDLERS (try_block) = push_stmt_list ();
1575 /* Finish the body of a cleanup try-block, which may be given by
1576 TRY_BLOCK. */
1578 void
1579 finish_cleanup_try_block (tree try_block)
1581 TRY_STMTS (try_block) = pop_stmt_list (TRY_STMTS (try_block));
1584 /* Finish an implicitly generated try-block, with a cleanup is given
1585 by CLEANUP. */
1587 void
1588 finish_cleanup (tree cleanup, tree try_block)
1590 TRY_HANDLERS (try_block) = cleanup;
1591 CLEANUP_P (try_block) = 1;
1594 /* Likewise, for a function-try-block. */
1596 void
1597 finish_function_try_block (tree try_block)
1599 finish_try_block (try_block);
1600 /* FIXME : something queer about CTOR_INITIALIZER somehow following
1601 the try block, but moving it inside. */
1602 in_function_try_handler = 1;
1605 /* Finish a handler-sequence for a try-block, which may be given by
1606 TRY_BLOCK. */
1608 void
1609 finish_handler_sequence (tree try_block)
1611 TRY_HANDLERS (try_block) = pop_stmt_list (TRY_HANDLERS (try_block));
1612 check_handlers (TRY_HANDLERS (try_block));
1615 /* Finish the handler-seq for a function-try-block, given by
1616 TRY_BLOCK. COMPOUND_STMT is the outer block created by
1617 begin_function_try_block. */
1619 void
1620 finish_function_handler_sequence (tree try_block, tree compound_stmt)
1622 in_function_try_handler = 0;
1623 finish_handler_sequence (try_block);
1624 finish_compound_stmt (compound_stmt);
1627 /* Begin a handler. Returns a HANDLER if appropriate. */
1629 tree
1630 begin_handler (void)
1632 tree r;
1634 r = build_stmt (input_location, HANDLER, NULL_TREE, NULL_TREE);
1635 add_stmt (r);
1637 /* Create a binding level for the eh_info and the exception object
1638 cleanup. */
1639 HANDLER_BODY (r) = do_pushlevel (sk_catch);
1641 return r;
1644 /* Finish the handler-parameters for a handler, which may be given by
1645 HANDLER. DECL is the declaration for the catch parameter, or NULL
1646 if this is a `catch (...)' clause. */
1648 void
1649 finish_handler_parms (tree decl, tree handler)
1651 tree type = NULL_TREE;
1652 if (processing_template_decl)
1654 if (decl)
1656 decl = pushdecl (decl);
1657 decl = push_template_decl (decl);
1658 HANDLER_PARMS (handler) = decl;
1659 type = TREE_TYPE (decl);
1662 else
1664 type = expand_start_catch_block (decl);
1665 if (warn_catch_value
1666 && type != NULL_TREE
1667 && type != error_mark_node
1668 && !TYPE_REF_P (TREE_TYPE (decl)))
1670 tree orig_type = TREE_TYPE (decl);
1671 if (CLASS_TYPE_P (orig_type))
1673 if (TYPE_POLYMORPHIC_P (orig_type))
1674 warning_at (DECL_SOURCE_LOCATION (decl),
1675 OPT_Wcatch_value_,
1676 "catching polymorphic type %q#T by value",
1677 orig_type);
1678 else if (warn_catch_value > 1)
1679 warning_at (DECL_SOURCE_LOCATION (decl),
1680 OPT_Wcatch_value_,
1681 "catching type %q#T by value", orig_type);
1683 else if (warn_catch_value > 2)
1684 warning_at (DECL_SOURCE_LOCATION (decl),
1685 OPT_Wcatch_value_,
1686 "catching non-reference type %q#T", orig_type);
1689 HANDLER_TYPE (handler) = type;
1692 /* Finish a handler, which may be given by HANDLER. The BLOCKs are
1693 the return value from the matching call to finish_handler_parms. */
1695 void
1696 finish_handler (tree handler)
1698 if (!processing_template_decl)
1699 expand_end_catch_block ();
1700 HANDLER_BODY (handler) = do_poplevel (HANDLER_BODY (handler));
1703 /* Begin a compound statement. FLAGS contains some bits that control the
1704 behavior and context. If BCS_NO_SCOPE is set, the compound statement
1705 does not define a scope. If BCS_FN_BODY is set, this is the outermost
1706 block of a function. If BCS_TRY_BLOCK is set, this is the block
1707 created on behalf of a TRY statement. Returns a token to be passed to
1708 finish_compound_stmt. */
1710 tree
1711 begin_compound_stmt (unsigned int flags)
1713 tree r;
1715 if (flags & BCS_NO_SCOPE)
1717 r = push_stmt_list ();
1718 STATEMENT_LIST_NO_SCOPE (r) = 1;
1720 /* Normally, we try hard to keep the BLOCK for a statement-expression.
1721 But, if it's a statement-expression with a scopeless block, there's
1722 nothing to keep, and we don't want to accidentally keep a block
1723 *inside* the scopeless block. */
1724 keep_next_level (false);
1726 else
1728 scope_kind sk = sk_block;
1729 if (flags & BCS_TRY_BLOCK)
1730 sk = sk_try;
1731 else if (flags & BCS_TRANSACTION)
1732 sk = sk_transaction;
1733 r = do_pushlevel (sk);
1736 /* When processing a template, we need to remember where the braces were,
1737 so that we can set up identical scopes when instantiating the template
1738 later. BIND_EXPR is a handy candidate for this.
1739 Note that do_poplevel won't create a BIND_EXPR itself here (and thus
1740 result in nested BIND_EXPRs), since we don't build BLOCK nodes when
1741 processing templates. */
1742 if (processing_template_decl)
1744 r = build3 (BIND_EXPR, NULL, NULL, r, NULL);
1745 BIND_EXPR_TRY_BLOCK (r) = (flags & BCS_TRY_BLOCK) != 0;
1746 BIND_EXPR_BODY_BLOCK (r) = (flags & BCS_FN_BODY) != 0;
1747 TREE_SIDE_EFFECTS (r) = 1;
1750 return r;
1753 /* Finish a compound-statement, which is given by STMT. */
1755 void
1756 finish_compound_stmt (tree stmt)
1758 if (TREE_CODE (stmt) == BIND_EXPR)
1760 tree body = do_poplevel (BIND_EXPR_BODY (stmt));
1761 /* If the STATEMENT_LIST is empty and this BIND_EXPR isn't special,
1762 discard the BIND_EXPR so it can be merged with the containing
1763 STATEMENT_LIST. */
1764 if (TREE_CODE (body) == STATEMENT_LIST
1765 && STATEMENT_LIST_HEAD (body) == NULL
1766 && !BIND_EXPR_BODY_BLOCK (stmt)
1767 && !BIND_EXPR_TRY_BLOCK (stmt))
1768 stmt = body;
1769 else
1770 BIND_EXPR_BODY (stmt) = body;
1772 else if (STATEMENT_LIST_NO_SCOPE (stmt))
1773 stmt = pop_stmt_list (stmt);
1774 else
1776 /* Destroy any ObjC "super" receivers that may have been
1777 created. */
1778 objc_clear_super_receiver ();
1780 stmt = do_poplevel (stmt);
1783 /* ??? See c_end_compound_stmt wrt statement expressions. */
1784 add_stmt (stmt);
1787 /* Finish an asm-statement, whose components are a STRING, some
1788 OUTPUT_OPERANDS, some INPUT_OPERANDS, some CLOBBERS and some
1789 LABELS. Also note whether the asm-statement should be
1790 considered volatile, and whether it is asm inline. */
1792 tree
1793 finish_asm_stmt (location_t loc, int volatile_p, tree string,
1794 tree output_operands, tree input_operands, tree clobbers,
1795 tree labels, bool inline_p)
1797 tree r;
1798 tree t;
1799 int ninputs = list_length (input_operands);
1800 int noutputs = list_length (output_operands);
1802 if (!processing_template_decl)
1804 const char *constraint;
1805 const char **oconstraints;
1806 bool allows_mem, allows_reg, is_inout;
1807 tree operand;
1808 int i;
1810 oconstraints = XALLOCAVEC (const char *, noutputs);
1812 string = resolve_asm_operand_names (string, output_operands,
1813 input_operands, labels);
1815 for (i = 0, t = output_operands; t; t = TREE_CHAIN (t), ++i)
1817 operand = TREE_VALUE (t);
1819 /* ??? Really, this should not be here. Users should be using a
1820 proper lvalue, dammit. But there's a long history of using
1821 casts in the output operands. In cases like longlong.h, this
1822 becomes a primitive form of typechecking -- if the cast can be
1823 removed, then the output operand had a type of the proper width;
1824 otherwise we'll get an error. Gross, but ... */
1825 STRIP_NOPS (operand);
1827 operand = mark_lvalue_use (operand);
1829 if (!lvalue_or_else (operand, lv_asm, tf_warning_or_error))
1830 operand = error_mark_node;
1832 if (operand != error_mark_node
1833 && (TREE_READONLY (operand)
1834 || CP_TYPE_CONST_P (TREE_TYPE (operand))
1835 /* Functions are not modifiable, even though they are
1836 lvalues. */
1837 || FUNC_OR_METHOD_TYPE_P (TREE_TYPE (operand))
1838 /* If it's an aggregate and any field is const, then it is
1839 effectively const. */
1840 || (CLASS_TYPE_P (TREE_TYPE (operand))
1841 && C_TYPE_FIELDS_READONLY (TREE_TYPE (operand)))))
1842 cxx_readonly_error (loc, operand, lv_asm);
1844 tree *op = &operand;
1845 while (TREE_CODE (*op) == COMPOUND_EXPR)
1846 op = &TREE_OPERAND (*op, 1);
1847 switch (TREE_CODE (*op))
1849 case PREINCREMENT_EXPR:
1850 case PREDECREMENT_EXPR:
1851 case MODIFY_EXPR:
1852 *op = genericize_compound_lvalue (*op);
1853 op = &TREE_OPERAND (*op, 1);
1854 break;
1855 default:
1856 break;
1859 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
1860 oconstraints[i] = constraint;
1862 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
1863 &allows_mem, &allows_reg, &is_inout))
1865 /* If the operand is going to end up in memory,
1866 mark it addressable. */
1867 if (!allows_reg && !cxx_mark_addressable (*op))
1868 operand = error_mark_node;
1870 else
1871 operand = error_mark_node;
1873 TREE_VALUE (t) = operand;
1876 for (i = 0, t = input_operands; t; ++i, t = TREE_CHAIN (t))
1878 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
1879 bool constraint_parsed
1880 = parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
1881 oconstraints, &allows_mem, &allows_reg);
1882 /* If the operand is going to end up in memory, don't call
1883 decay_conversion. */
1884 if (constraint_parsed && !allows_reg && allows_mem)
1885 operand = mark_lvalue_use (TREE_VALUE (t));
1886 else
1887 operand = decay_conversion (TREE_VALUE (t), tf_warning_or_error);
1889 /* If the type of the operand hasn't been determined (e.g.,
1890 because it involves an overloaded function), then issue
1891 an error message. There's no context available to
1892 resolve the overloading. */
1893 if (TREE_TYPE (operand) == unknown_type_node)
1895 error_at (loc,
1896 "type of %<asm%> operand %qE could not be determined",
1897 TREE_VALUE (t));
1898 operand = error_mark_node;
1901 if (constraint_parsed)
1903 /* If the operand is going to end up in memory,
1904 mark it addressable. */
1905 if (!allows_reg && allows_mem)
1907 /* Strip the nops as we allow this case. FIXME, this really
1908 should be rejected or made deprecated. */
1909 STRIP_NOPS (operand);
1911 tree *op = &operand;
1912 while (TREE_CODE (*op) == COMPOUND_EXPR)
1913 op = &TREE_OPERAND (*op, 1);
1914 switch (TREE_CODE (*op))
1916 case PREINCREMENT_EXPR:
1917 case PREDECREMENT_EXPR:
1918 case MODIFY_EXPR:
1919 *op = genericize_compound_lvalue (*op);
1920 op = &TREE_OPERAND (*op, 1);
1921 break;
1922 default:
1923 break;
1926 if (!cxx_mark_addressable (*op))
1927 operand = error_mark_node;
1929 else if (!allows_reg && !allows_mem)
1931 /* If constraint allows neither register nor memory,
1932 try harder to get a constant. */
1933 tree constop = maybe_constant_value (operand);
1934 if (TREE_CONSTANT (constop))
1935 operand = constop;
1938 else
1939 operand = error_mark_node;
1941 TREE_VALUE (t) = operand;
1945 r = build_stmt (loc, ASM_EXPR, string,
1946 output_operands, input_operands,
1947 clobbers, labels);
1948 ASM_VOLATILE_P (r) = volatile_p || noutputs == 0;
1949 ASM_INLINE_P (r) = inline_p;
1950 r = maybe_cleanup_point_expr_void (r);
1951 return add_stmt (r);
1954 /* Finish a label with the indicated NAME. Returns the new label. */
1956 tree
1957 finish_label_stmt (tree name)
1959 tree decl = define_label (input_location, name);
1961 if (decl == error_mark_node)
1962 return error_mark_node;
1964 add_stmt (build_stmt (input_location, LABEL_EXPR, decl));
1966 return decl;
1969 /* Finish a series of declarations for local labels. G++ allows users
1970 to declare "local" labels, i.e., labels with scope. This extension
1971 is useful when writing code involving statement-expressions. */
1973 void
1974 finish_label_decl (tree name)
1976 if (!at_function_scope_p ())
1978 error ("%<__label__%> declarations are only allowed in function scopes");
1979 return;
1982 add_decl_expr (declare_local_label (name));
1985 /* When DECL goes out of scope, make sure that CLEANUP is executed. */
1987 void
1988 finish_decl_cleanup (tree decl, tree cleanup)
1990 push_cleanup (decl, cleanup, false);
1993 /* If the current scope exits with an exception, run CLEANUP. */
1995 void
1996 finish_eh_cleanup (tree cleanup)
1998 push_cleanup (NULL, cleanup, true);
2001 /* The MEM_INITS is a list of mem-initializers, in reverse of the
2002 order they were written by the user. Each node is as for
2003 emit_mem_initializers. */
2005 void
2006 finish_mem_initializers (tree mem_inits)
2008 /* Reorder the MEM_INITS so that they are in the order they appeared
2009 in the source program. */
2010 mem_inits = nreverse (mem_inits);
2012 if (processing_template_decl)
2014 tree mem;
2016 for (mem = mem_inits; mem; mem = TREE_CHAIN (mem))
2018 /* If the TREE_PURPOSE is a TYPE_PACK_EXPANSION, skip the
2019 check for bare parameter packs in the TREE_VALUE, because
2020 any parameter packs in the TREE_VALUE have already been
2021 bound as part of the TREE_PURPOSE. See
2022 make_pack_expansion for more information. */
2023 if (TREE_CODE (TREE_PURPOSE (mem)) != TYPE_PACK_EXPANSION
2024 && check_for_bare_parameter_packs (TREE_VALUE (mem)))
2025 TREE_VALUE (mem) = error_mark_node;
2028 add_stmt (build_min_nt_loc (UNKNOWN_LOCATION,
2029 CTOR_INITIALIZER, mem_inits));
2031 else
2032 emit_mem_initializers (mem_inits);
2035 /* Obfuscate EXPR if it looks like an id-expression or member access so
2036 that the call to finish_decltype in do_auto_deduction will give the
2037 right result. If EVEN_UNEVAL, do this even in unevaluated context. */
2039 tree
2040 force_paren_expr (tree expr, bool even_uneval)
2042 /* This is only needed for decltype(auto) in C++14. */
2043 if (cxx_dialect < cxx14)
2044 return expr;
2046 /* If we're in unevaluated context, we can't be deducing a
2047 return/initializer type, so we don't need to mess with this. */
2048 if (cp_unevaluated_operand && !even_uneval)
2049 return expr;
2051 if (!DECL_P (tree_strip_any_location_wrapper (expr))
2052 && TREE_CODE (expr) != COMPONENT_REF
2053 && TREE_CODE (expr) != SCOPE_REF)
2054 return expr;
2056 location_t loc = cp_expr_location (expr);
2058 if (TREE_CODE (expr) == COMPONENT_REF
2059 || TREE_CODE (expr) == SCOPE_REF)
2060 REF_PARENTHESIZED_P (expr) = true;
2061 else if (processing_template_decl)
2062 expr = build1_loc (loc, PAREN_EXPR, TREE_TYPE (expr), expr);
2063 else
2065 expr = build1_loc (loc, VIEW_CONVERT_EXPR, TREE_TYPE (expr), expr);
2066 REF_PARENTHESIZED_P (expr) = true;
2069 return expr;
2072 /* If T is an id-expression obfuscated by force_paren_expr, undo the
2073 obfuscation and return the underlying id-expression. Otherwise
2074 return T. */
2076 tree
2077 maybe_undo_parenthesized_ref (tree t)
2079 if (cxx_dialect < cxx14)
2080 return t;
2082 if (INDIRECT_REF_P (t) && REF_PARENTHESIZED_P (t))
2084 t = TREE_OPERAND (t, 0);
2085 while (TREE_CODE (t) == NON_LVALUE_EXPR
2086 || TREE_CODE (t) == NOP_EXPR)
2087 t = TREE_OPERAND (t, 0);
2089 gcc_assert (TREE_CODE (t) == ADDR_EXPR
2090 || TREE_CODE (t) == STATIC_CAST_EXPR);
2091 t = TREE_OPERAND (t, 0);
2093 else if (TREE_CODE (t) == PAREN_EXPR)
2094 t = TREE_OPERAND (t, 0);
2095 else if (TREE_CODE (t) == VIEW_CONVERT_EXPR
2096 && REF_PARENTHESIZED_P (t))
2097 t = TREE_OPERAND (t, 0);
2099 return t;
2102 /* Finish a parenthesized expression EXPR. */
2104 cp_expr
2105 finish_parenthesized_expr (cp_expr expr)
2107 if (EXPR_P (expr))
2108 /* This inhibits warnings in c_common_truthvalue_conversion. */
2109 suppress_warning (expr, OPT_Wparentheses);
2111 if (TREE_CODE (expr) == OFFSET_REF
2112 || TREE_CODE (expr) == SCOPE_REF)
2113 /* [expr.unary.op]/3 The qualified id of a pointer-to-member must not be
2114 enclosed in parentheses. */
2115 PTRMEM_OK_P (expr) = 0;
2117 tree stripped_expr = tree_strip_any_location_wrapper (expr);
2118 if (TREE_CODE (stripped_expr) == STRING_CST)
2119 PAREN_STRING_LITERAL_P (stripped_expr) = 1;
2121 expr = cp_expr (force_paren_expr (expr), expr.get_location ());
2123 return expr;
2126 /* Finish a reference to a non-static data member (DECL) that is not
2127 preceded by `.' or `->'. */
2129 tree
2130 finish_non_static_data_member (tree decl, tree object, tree qualifying_scope)
2132 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
2133 bool try_omp_private = !object && omp_private_member_map;
2134 tree ret;
2136 if (!object)
2138 tree scope = qualifying_scope;
2139 if (scope == NULL_TREE)
2141 scope = context_for_name_lookup (decl);
2142 if (!TYPE_P (scope))
2144 /* Can happen during error recovery (c++/85014). */
2145 gcc_assert (seen_error ());
2146 return error_mark_node;
2149 object = maybe_dummy_object (scope, NULL);
2152 object = maybe_resolve_dummy (object, true);
2153 if (object == error_mark_node)
2154 return error_mark_node;
2156 /* DR 613/850: Can use non-static data members without an associated
2157 object in sizeof/decltype/alignof. */
2158 if (is_dummy_object (object) && cp_unevaluated_operand == 0
2159 && (!processing_template_decl || !current_class_ref))
2161 if (current_function_decl
2162 && DECL_STATIC_FUNCTION_P (current_function_decl))
2163 error ("invalid use of member %qD in static member function", decl);
2164 else
2165 error ("invalid use of non-static data member %qD", decl);
2166 inform (DECL_SOURCE_LOCATION (decl), "declared here");
2168 return error_mark_node;
2171 if (current_class_ptr)
2172 TREE_USED (current_class_ptr) = 1;
2173 if (processing_template_decl)
2175 tree type = TREE_TYPE (decl);
2177 if (TYPE_REF_P (type))
2178 /* Quals on the object don't matter. */;
2179 else if (PACK_EXPANSION_P (type))
2180 /* Don't bother trying to represent this. */
2181 type = NULL_TREE;
2182 else
2184 /* Set the cv qualifiers. */
2185 int quals = cp_type_quals (TREE_TYPE (object));
2187 if (DECL_MUTABLE_P (decl))
2188 quals &= ~TYPE_QUAL_CONST;
2190 quals |= cp_type_quals (TREE_TYPE (decl));
2191 type = cp_build_qualified_type (type, quals);
2194 if (qualifying_scope)
2195 /* Wrap this in a SCOPE_REF for now. */
2196 ret = build_qualified_name (type, qualifying_scope, decl,
2197 /*template_p=*/false);
2198 else
2199 ret = (convert_from_reference
2200 (build_min (COMPONENT_REF, type, object, decl, NULL_TREE)));
2202 /* If PROCESSING_TEMPLATE_DECL is nonzero here, then
2203 QUALIFYING_SCOPE is also non-null. */
2204 else
2206 tree access_type = TREE_TYPE (object);
2208 perform_or_defer_access_check (TYPE_BINFO (access_type), decl,
2209 decl, tf_warning_or_error);
2211 /* If the data member was named `C::M', convert `*this' to `C'
2212 first. */
2213 if (qualifying_scope)
2215 tree binfo = NULL_TREE;
2216 object = build_scoped_ref (object, qualifying_scope,
2217 &binfo);
2220 ret = build_class_member_access_expr (object, decl,
2221 /*access_path=*/NULL_TREE,
2222 /*preserve_reference=*/false,
2223 tf_warning_or_error);
2225 if (try_omp_private)
2227 tree *v = omp_private_member_map->get (decl);
2228 if (v)
2229 ret = convert_from_reference (*v);
2231 return ret;
2234 /* DECL was the declaration to which a qualified-id resolved. Issue
2235 an error message if it is not accessible. If OBJECT_TYPE is
2236 non-NULL, we have just seen `x->' or `x.' and OBJECT_TYPE is the
2237 type of `*x', or `x', respectively. If the DECL was named as
2238 `A::B' then NESTED_NAME_SPECIFIER is `A'. Return value is like
2239 perform_access_checks above. */
2241 bool
2242 check_accessibility_of_qualified_id (tree decl,
2243 tree object_type,
2244 tree nested_name_specifier,
2245 tsubst_flags_t complain)
2247 /* If we're not checking, return immediately. */
2248 if (deferred_access_no_check)
2249 return true;
2251 /* Determine the SCOPE of DECL. */
2252 tree scope = context_for_name_lookup (decl);
2253 /* If the SCOPE is not a type, then DECL is not a member. */
2254 if (!TYPE_P (scope)
2255 /* If SCOPE is dependent then we can't perform this access check now,
2256 and since we'll perform this access check again after substitution
2257 there's no need to explicitly defer it. */
2258 || dependent_type_p (scope))
2259 return true;
2261 tree qualifying_type = NULL_TREE;
2262 /* Compute the scope through which DECL is being accessed. */
2263 if (object_type
2264 /* OBJECT_TYPE might not be a class type; consider:
2266 class A { typedef int I; };
2267 I *p;
2268 p->A::I::~I();
2270 In this case, we will have "A::I" as the DECL, but "I" as the
2271 OBJECT_TYPE. */
2272 && CLASS_TYPE_P (object_type)
2273 && DERIVED_FROM_P (scope, object_type))
2274 /* If we are processing a `->' or `.' expression, use the type of the
2275 left-hand side. */
2276 qualifying_type = object_type;
2277 else if (nested_name_specifier)
2279 /* If the reference is to a non-static member of the
2280 current class, treat it as if it were referenced through
2281 `this'. */
2282 if (DECL_NONSTATIC_MEMBER_P (decl)
2283 && current_class_ptr)
2284 if (tree current = current_nonlambda_class_type ())
2286 if (dependent_type_p (current))
2287 /* In general we can't know whether this access goes through
2288 `this' until instantiation time. Punt now, or else we might
2289 create a deferred access check that's not relative to `this'
2290 when it ought to be. We'll check this access again after
2291 substitution, e.g. from tsubst_qualified_id. */
2292 return true;
2294 if (DERIVED_FROM_P (scope, current))
2295 qualifying_type = current;
2297 /* Otherwise, use the type indicated by the
2298 nested-name-specifier. */
2299 if (!qualifying_type)
2300 qualifying_type = nested_name_specifier;
2302 else
2303 /* Otherwise, the name must be from the current class or one of
2304 its bases. */
2305 qualifying_type = currently_open_derived_class (scope);
2307 if (qualifying_type
2308 /* It is possible for qualifying type to be a TEMPLATE_TYPE_PARM
2309 or similar in a default argument value. */
2310 && CLASS_TYPE_P (qualifying_type))
2311 return perform_or_defer_access_check (TYPE_BINFO (qualifying_type), decl,
2312 decl, complain);
2314 return true;
2317 /* EXPR is the result of a qualified-id. The QUALIFYING_CLASS was the
2318 class named to the left of the "::" operator. DONE is true if this
2319 expression is a complete postfix-expression; it is false if this
2320 expression is followed by '->', '[', '(', etc. ADDRESS_P is true
2321 iff this expression is the operand of '&'. TEMPLATE_P is true iff
2322 the qualified-id was of the form "A::template B". TEMPLATE_ARG_P
2323 is true iff this qualified name appears as a template argument. */
2325 tree
2326 finish_qualified_id_expr (tree qualifying_class,
2327 tree expr,
2328 bool done,
2329 bool address_p,
2330 bool template_p,
2331 bool template_arg_p,
2332 tsubst_flags_t complain)
2334 gcc_assert (TYPE_P (qualifying_class));
2336 if (error_operand_p (expr))
2337 return error_mark_node;
2339 if ((DECL_P (expr) || BASELINK_P (expr))
2340 && !mark_used (expr, complain))
2341 return error_mark_node;
2343 if (template_p)
2345 if (TREE_CODE (expr) == UNBOUND_CLASS_TEMPLATE)
2347 /* cp_parser_lookup_name thought we were looking for a type,
2348 but we're actually looking for a declaration. */
2349 qualifying_class = TYPE_CONTEXT (expr);
2350 expr = TYPE_IDENTIFIER (expr);
2352 else
2353 check_template_keyword (expr);
2356 /* If EXPR occurs as the operand of '&', use special handling that
2357 permits a pointer-to-member. */
2358 if (address_p && done)
2360 if (TREE_CODE (expr) == SCOPE_REF)
2361 expr = TREE_OPERAND (expr, 1);
2362 expr = build_offset_ref (qualifying_class, expr,
2363 /*address_p=*/true, complain);
2364 return expr;
2367 /* No need to check access within an enum. */
2368 if (TREE_CODE (qualifying_class) == ENUMERAL_TYPE
2369 && TREE_CODE (expr) != IDENTIFIER_NODE)
2370 return expr;
2372 /* Within the scope of a class, turn references to non-static
2373 members into expression of the form "this->...". */
2374 if (template_arg_p)
2375 /* But, within a template argument, we do not want make the
2376 transformation, as there is no "this" pointer. */
2378 else if (TREE_CODE (expr) == FIELD_DECL)
2380 push_deferring_access_checks (dk_no_check);
2381 expr = finish_non_static_data_member (expr, NULL_TREE,
2382 qualifying_class);
2383 pop_deferring_access_checks ();
2385 else if (BASELINK_P (expr))
2387 /* See if any of the functions are non-static members. */
2388 /* If so, the expression may be relative to 'this'. */
2389 if (!shared_member_p (expr)
2390 && current_class_ptr
2391 && DERIVED_FROM_P (qualifying_class,
2392 current_nonlambda_class_type ()))
2393 expr = (build_class_member_access_expr
2394 (maybe_dummy_object (qualifying_class, NULL),
2395 expr,
2396 BASELINK_ACCESS_BINFO (expr),
2397 /*preserve_reference=*/false,
2398 complain));
2399 else if (done)
2400 /* The expression is a qualified name whose address is not
2401 being taken. */
2402 expr = build_offset_ref (qualifying_class, expr, /*address_p=*/false,
2403 complain);
2405 else if (!template_p
2406 && TREE_CODE (expr) == TEMPLATE_DECL
2407 && !DECL_FUNCTION_TEMPLATE_P (expr))
2409 if (complain & tf_error)
2410 error ("%qE missing template arguments", expr);
2411 return error_mark_node;
2413 else
2415 /* In a template, return a SCOPE_REF for most qualified-ids
2416 so that we can check access at instantiation time. But if
2417 we're looking at a member of the current instantiation, we
2418 know we have access and building up the SCOPE_REF confuses
2419 non-type template argument handling. */
2420 if (processing_template_decl
2421 && (!currently_open_class (qualifying_class)
2422 || TREE_CODE (expr) == IDENTIFIER_NODE
2423 || TREE_CODE (expr) == TEMPLATE_ID_EXPR
2424 || TREE_CODE (expr) == BIT_NOT_EXPR))
2425 expr = build_qualified_name (TREE_TYPE (expr),
2426 qualifying_class, expr,
2427 template_p);
2428 else if (tree wrap = maybe_get_tls_wrapper_call (expr))
2429 expr = wrap;
2431 expr = convert_from_reference (expr);
2434 return expr;
2437 /* Begin a statement-expression. The value returned must be passed to
2438 finish_stmt_expr. */
2440 tree
2441 begin_stmt_expr (void)
2443 return push_stmt_list ();
2446 /* Process the final expression of a statement expression. EXPR can be
2447 NULL, if the final expression is empty. Return a STATEMENT_LIST
2448 containing all the statements in the statement-expression, or
2449 ERROR_MARK_NODE if there was an error. */
2451 tree
2452 finish_stmt_expr_expr (tree expr, tree stmt_expr)
2454 if (error_operand_p (expr))
2456 /* The type of the statement-expression is the type of the last
2457 expression. */
2458 TREE_TYPE (stmt_expr) = error_mark_node;
2459 return error_mark_node;
2462 /* If the last statement does not have "void" type, then the value
2463 of the last statement is the value of the entire expression. */
2464 if (expr)
2466 tree type = TREE_TYPE (expr);
2468 if (type && type_unknown_p (type))
2470 error ("a statement expression is an insufficient context"
2471 " for overload resolution");
2472 TREE_TYPE (stmt_expr) = error_mark_node;
2473 return error_mark_node;
2475 else if (processing_template_decl)
2477 expr = build_stmt (input_location, EXPR_STMT, expr);
2478 expr = add_stmt (expr);
2479 /* Mark the last statement so that we can recognize it as such at
2480 template-instantiation time. */
2481 EXPR_STMT_STMT_EXPR_RESULT (expr) = 1;
2483 else if (VOID_TYPE_P (type))
2485 /* Just treat this like an ordinary statement. */
2486 expr = finish_expr_stmt (expr);
2488 else
2490 /* It actually has a value we need to deal with. First, force it
2491 to be an rvalue so that we won't need to build up a copy
2492 constructor call later when we try to assign it to something. */
2493 expr = force_rvalue (expr, tf_warning_or_error);
2494 if (error_operand_p (expr))
2495 return error_mark_node;
2497 /* Update for array-to-pointer decay. */
2498 type = TREE_TYPE (expr);
2500 /* Wrap it in a CLEANUP_POINT_EXPR and add it to the list like a
2501 normal statement, but don't convert to void or actually add
2502 the EXPR_STMT. */
2503 if (TREE_CODE (expr) != CLEANUP_POINT_EXPR)
2504 expr = maybe_cleanup_point_expr (expr);
2505 add_stmt (expr);
2508 /* The type of the statement-expression is the type of the last
2509 expression. */
2510 TREE_TYPE (stmt_expr) = type;
2513 return stmt_expr;
2516 /* Finish a statement-expression. EXPR should be the value returned
2517 by the previous begin_stmt_expr. Returns an expression
2518 representing the statement-expression. */
2520 tree
2521 finish_stmt_expr (tree stmt_expr, bool has_no_scope)
2523 tree type;
2524 tree result;
2526 if (error_operand_p (stmt_expr))
2528 pop_stmt_list (stmt_expr);
2529 return error_mark_node;
2532 gcc_assert (TREE_CODE (stmt_expr) == STATEMENT_LIST);
2534 type = TREE_TYPE (stmt_expr);
2535 result = pop_stmt_list (stmt_expr);
2536 TREE_TYPE (result) = type;
2538 if (processing_template_decl)
2540 result = build_min (STMT_EXPR, type, result);
2541 TREE_SIDE_EFFECTS (result) = 1;
2542 STMT_EXPR_NO_SCOPE (result) = has_no_scope;
2544 else if (CLASS_TYPE_P (type))
2546 /* Wrap the statement-expression in a TARGET_EXPR so that the
2547 temporary object created by the final expression is destroyed at
2548 the end of the full-expression containing the
2549 statement-expression. */
2550 result = force_target_expr (type, result, tf_warning_or_error);
2553 return result;
2556 /* Returns the expression which provides the value of STMT_EXPR. */
2558 tree
2559 stmt_expr_value_expr (tree stmt_expr)
2561 tree t = STMT_EXPR_STMT (stmt_expr);
2563 if (TREE_CODE (t) == BIND_EXPR)
2564 t = BIND_EXPR_BODY (t);
2566 if (TREE_CODE (t) == STATEMENT_LIST && STATEMENT_LIST_TAIL (t))
2567 t = STATEMENT_LIST_TAIL (t)->stmt;
2569 if (TREE_CODE (t) == EXPR_STMT)
2570 t = EXPR_STMT_EXPR (t);
2572 return t;
2575 /* Return TRUE iff EXPR_STMT is an empty list of
2576 expression statements. */
2578 bool
2579 empty_expr_stmt_p (tree expr_stmt)
2581 tree body = NULL_TREE;
2583 if (expr_stmt == void_node)
2584 return true;
2586 if (expr_stmt)
2588 if (TREE_CODE (expr_stmt) == EXPR_STMT)
2589 body = EXPR_STMT_EXPR (expr_stmt);
2590 else if (TREE_CODE (expr_stmt) == STATEMENT_LIST)
2591 body = expr_stmt;
2594 if (body)
2596 if (TREE_CODE (body) == STATEMENT_LIST)
2597 return tsi_end_p (tsi_start (body));
2598 else
2599 return empty_expr_stmt_p (body);
2601 return false;
2604 /* Perform Koenig lookup. FN_EXPR is the postfix-expression representing
2605 the function (or functions) to call; ARGS are the arguments to the
2606 call. Returns the functions to be considered by overload resolution. */
2608 cp_expr
2609 perform_koenig_lookup (cp_expr fn_expr, vec<tree, va_gc> *args,
2610 tsubst_flags_t complain)
2612 tree identifier = NULL_TREE;
2613 tree functions = NULL_TREE;
2614 tree tmpl_args = NULL_TREE;
2615 bool template_id = false;
2616 location_t loc = fn_expr.get_location ();
2617 tree fn = fn_expr.get_value ();
2619 STRIP_ANY_LOCATION_WRAPPER (fn);
2621 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
2623 /* Use a separate flag to handle null args. */
2624 template_id = true;
2625 tmpl_args = TREE_OPERAND (fn, 1);
2626 fn = TREE_OPERAND (fn, 0);
2629 /* Find the name of the overloaded function. */
2630 if (identifier_p (fn))
2631 identifier = fn;
2632 else
2634 functions = fn;
2635 identifier = OVL_NAME (functions);
2638 /* A call to a namespace-scope function using an unqualified name.
2640 Do Koenig lookup -- unless any of the arguments are
2641 type-dependent. */
2642 if (!any_type_dependent_arguments_p (args)
2643 && !any_dependent_template_arguments_p (tmpl_args))
2645 fn = lookup_arg_dependent (identifier, functions, args);
2646 if (!fn)
2648 /* The unqualified name could not be resolved. */
2649 if (complain & tf_error)
2650 fn = unqualified_fn_lookup_error (cp_expr (identifier, loc));
2651 else
2652 fn = identifier;
2656 if (fn && template_id && fn != error_mark_node)
2657 fn = build2 (TEMPLATE_ID_EXPR, unknown_type_node, fn, tmpl_args);
2659 return cp_expr (fn, loc);
2662 /* Generate an expression for `FN (ARGS)'. This may change the
2663 contents of ARGS.
2665 If DISALLOW_VIRTUAL is true, the call to FN will be not generated
2666 as a virtual call, even if FN is virtual. (This flag is set when
2667 encountering an expression where the function name is explicitly
2668 qualified. For example a call to `X::f' never generates a virtual
2669 call.)
2671 Returns code for the call. */
2673 tree
2674 finish_call_expr (tree fn, vec<tree, va_gc> **args, bool disallow_virtual,
2675 bool koenig_p, tsubst_flags_t complain)
2677 tree result;
2678 tree orig_fn;
2679 vec<tree, va_gc> *orig_args = *args;
2681 if (fn == error_mark_node)
2682 return error_mark_node;
2684 gcc_assert (!TYPE_P (fn));
2686 /* If FN may be a FUNCTION_DECL obfuscated by force_paren_expr, undo
2687 it so that we can tell this is a call to a known function. */
2688 fn = maybe_undo_parenthesized_ref (fn);
2690 STRIP_ANY_LOCATION_WRAPPER (fn);
2692 orig_fn = fn;
2694 if (processing_template_decl)
2696 /* If FN is a local extern declaration or set thereof, look them up
2697 again at instantiation time. */
2698 if (is_overloaded_fn (fn))
2700 tree ifn = get_first_fn (fn);
2701 if (TREE_CODE (ifn) == FUNCTION_DECL
2702 && DECL_LOCAL_DECL_P (ifn))
2703 orig_fn = DECL_NAME (ifn);
2706 /* If the call expression is dependent, build a CALL_EXPR node
2707 with no type; type_dependent_expression_p recognizes
2708 expressions with no type as being dependent. */
2709 if (type_dependent_expression_p (fn)
2710 || any_type_dependent_arguments_p (*args))
2712 result = build_min_nt_call_vec (orig_fn, *args);
2713 SET_EXPR_LOCATION (result, cp_expr_loc_or_input_loc (fn));
2714 KOENIG_LOOKUP_P (result) = koenig_p;
2715 if (is_overloaded_fn (fn))
2716 fn = get_fns (fn);
2718 if (cfun)
2720 bool abnormal = true;
2721 for (lkp_iterator iter (fn); abnormal && iter; ++iter)
2723 tree fndecl = STRIP_TEMPLATE (*iter);
2724 if (TREE_CODE (fndecl) != FUNCTION_DECL
2725 || !TREE_THIS_VOLATILE (fndecl))
2726 abnormal = false;
2728 /* FIXME: Stop warning about falling off end of non-void
2729 function. But this is wrong. Even if we only see
2730 no-return fns at this point, we could select a
2731 future-defined return fn during instantiation. Or
2732 vice-versa. */
2733 if (abnormal)
2734 current_function_returns_abnormally = 1;
2736 return result;
2738 orig_args = make_tree_vector_copy (*args);
2739 if (!BASELINK_P (fn)
2740 && TREE_CODE (fn) != PSEUDO_DTOR_EXPR
2741 && TREE_TYPE (fn) != unknown_type_node)
2742 fn = build_non_dependent_expr (fn);
2743 make_args_non_dependent (*args);
2746 if (TREE_CODE (fn) == COMPONENT_REF)
2748 tree member = TREE_OPERAND (fn, 1);
2749 if (BASELINK_P (member))
2751 tree object = TREE_OPERAND (fn, 0);
2752 return build_new_method_call (object, member,
2753 args, NULL_TREE,
2754 (disallow_virtual
2755 ? LOOKUP_NORMAL | LOOKUP_NONVIRTUAL
2756 : LOOKUP_NORMAL),
2757 /*fn_p=*/NULL,
2758 complain);
2762 /* Per 13.3.1.1, '(&f)(...)' is the same as '(f)(...)'. */
2763 if (TREE_CODE (fn) == ADDR_EXPR
2764 && TREE_CODE (TREE_OPERAND (fn, 0)) == OVERLOAD)
2765 fn = TREE_OPERAND (fn, 0);
2767 if (is_overloaded_fn (fn))
2768 fn = baselink_for_fns (fn);
2770 result = NULL_TREE;
2771 if (BASELINK_P (fn))
2773 tree object;
2775 /* A call to a member function. From [over.call.func]:
2777 If the keyword this is in scope and refers to the class of
2778 that member function, or a derived class thereof, then the
2779 function call is transformed into a qualified function call
2780 using (*this) as the postfix-expression to the left of the
2781 . operator.... [Otherwise] a contrived object of type T
2782 becomes the implied object argument.
2784 In this situation:
2786 struct A { void f(); };
2787 struct B : public A {};
2788 struct C : public A { void g() { B::f(); }};
2790 "the class of that member function" refers to `A'. But 11.2
2791 [class.access.base] says that we need to convert 'this' to B* as
2792 part of the access, so we pass 'B' to maybe_dummy_object. */
2794 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (get_first_fn (fn)))
2796 /* A constructor call always uses a dummy object. (This constructor
2797 call which has the form A::A () is actually invalid and we are
2798 going to reject it later in build_new_method_call.) */
2799 object = build_dummy_object (BINFO_TYPE (BASELINK_ACCESS_BINFO (fn)));
2801 else
2802 object = maybe_dummy_object (BINFO_TYPE (BASELINK_ACCESS_BINFO (fn)),
2803 NULL);
2805 result = build_new_method_call (object, fn, args, NULL_TREE,
2806 (disallow_virtual
2807 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
2808 : LOOKUP_NORMAL),
2809 /*fn_p=*/NULL,
2810 complain);
2812 else if (concept_check_p (fn))
2814 /* FN is actually a template-id referring to a concept definition. */
2815 tree id = unpack_concept_check (fn);
2816 tree tmpl = TREE_OPERAND (id, 0);
2817 tree args = TREE_OPERAND (id, 1);
2819 if (!function_concept_p (tmpl))
2821 error_at (EXPR_LOC_OR_LOC (fn, input_location),
2822 "cannot call a concept as a function");
2823 return error_mark_node;
2826 /* Ensure the result is wrapped as a call expression. */
2827 result = build_concept_check (tmpl, args, tf_warning_or_error);
2829 else if (is_overloaded_fn (fn))
2831 /* If the function is an overloaded builtin, resolve it. */
2832 if (TREE_CODE (fn) == FUNCTION_DECL
2833 && (DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL
2834 || DECL_BUILT_IN_CLASS (fn) == BUILT_IN_MD))
2835 result = resolve_overloaded_builtin (input_location, fn, *args);
2837 if (!result)
2839 if (warn_sizeof_pointer_memaccess
2840 && (complain & tf_warning)
2841 && !vec_safe_is_empty (*args)
2842 && !processing_template_decl)
2844 location_t sizeof_arg_loc[3];
2845 tree sizeof_arg[3];
2846 unsigned int i;
2847 for (i = 0; i < 3; i++)
2849 tree t;
2851 sizeof_arg_loc[i] = UNKNOWN_LOCATION;
2852 sizeof_arg[i] = NULL_TREE;
2853 if (i >= (*args)->length ())
2854 continue;
2855 t = (**args)[i];
2856 if (TREE_CODE (t) != SIZEOF_EXPR)
2857 continue;
2858 if (SIZEOF_EXPR_TYPE_P (t))
2859 sizeof_arg[i] = TREE_TYPE (TREE_OPERAND (t, 0));
2860 else
2861 sizeof_arg[i] = TREE_OPERAND (t, 0);
2862 sizeof_arg_loc[i] = EXPR_LOCATION (t);
2864 sizeof_pointer_memaccess_warning
2865 (sizeof_arg_loc, fn, *args,
2866 sizeof_arg, same_type_ignoring_top_level_qualifiers_p);
2869 if ((complain & tf_warning)
2870 && TREE_CODE (fn) == FUNCTION_DECL
2871 && fndecl_built_in_p (fn, BUILT_IN_MEMSET)
2872 && vec_safe_length (*args) == 3
2873 && !any_type_dependent_arguments_p (*args))
2875 tree arg0 = (*orig_args)[0];
2876 tree arg1 = (*orig_args)[1];
2877 tree arg2 = (*orig_args)[2];
2878 int literal_mask = ((literal_integer_zerop (arg1) << 1)
2879 | (literal_integer_zerop (arg2) << 2));
2880 warn_for_memset (input_location, arg0, arg2, literal_mask);
2883 /* A call to a namespace-scope function. */
2884 result = build_new_function_call (fn, args, complain);
2887 else if (TREE_CODE (fn) == PSEUDO_DTOR_EXPR)
2889 if (!vec_safe_is_empty (*args))
2890 error ("arguments to destructor are not allowed");
2891 /* C++20/DR: If the postfix-expression names a pseudo-destructor (in
2892 which case the postfix-expression is a possibly-parenthesized class
2893 member access), the function call destroys the object of scalar type
2894 denoted by the object expression of the class member access. */
2895 tree ob = TREE_OPERAND (fn, 0);
2896 if (obvalue_p (ob))
2897 result = build_trivial_dtor_call (ob, true);
2898 else
2899 /* No location to clobber. */
2900 result = convert_to_void (ob, ICV_STATEMENT, complain);
2902 else if (CLASS_TYPE_P (TREE_TYPE (fn)))
2903 /* If the "function" is really an object of class type, it might
2904 have an overloaded `operator ()'. */
2905 result = build_op_call (fn, args, complain);
2907 if (!result)
2908 /* A call where the function is unknown. */
2909 result = cp_build_function_call_vec (fn, args, complain);
2911 if (processing_template_decl && result != error_mark_node)
2913 if (INDIRECT_REF_P (result))
2914 result = TREE_OPERAND (result, 0);
2915 result = build_call_vec (TREE_TYPE (result), orig_fn, orig_args);
2916 SET_EXPR_LOCATION (result, input_location);
2917 KOENIG_LOOKUP_P (result) = koenig_p;
2918 release_tree_vector (orig_args);
2919 result = convert_from_reference (result);
2922 return result;
2925 /* Finish a call to a postfix increment or decrement or EXPR. (Which
2926 is indicated by CODE, which should be POSTINCREMENT_EXPR or
2927 POSTDECREMENT_EXPR.) */
2929 cp_expr
2930 finish_increment_expr (cp_expr expr, enum tree_code code)
2932 /* input_location holds the location of the trailing operator token.
2933 Build a location of the form:
2934 expr++
2935 ~~~~^~
2936 with the caret at the operator token, ranging from the start
2937 of EXPR to the end of the operator token. */
2938 location_t combined_loc = make_location (input_location,
2939 expr.get_start (),
2940 get_finish (input_location));
2941 cp_expr result = build_x_unary_op (combined_loc, code, expr,
2942 tf_warning_or_error);
2943 /* TODO: build_x_unary_op doesn't honor the location, so set it here. */
2944 result.set_location (combined_loc);
2945 return result;
2948 /* Finish a use of `this'. Returns an expression for `this'. */
2950 tree
2951 finish_this_expr (void)
2953 tree result = NULL_TREE;
2955 if (current_class_ptr)
2957 tree type = TREE_TYPE (current_class_ref);
2959 /* In a lambda expression, 'this' refers to the captured 'this'. */
2960 if (LAMBDA_TYPE_P (type))
2961 result = lambda_expr_this_capture (CLASSTYPE_LAMBDA_EXPR (type), true);
2962 else
2963 result = current_class_ptr;
2966 if (result)
2967 /* The keyword 'this' is a prvalue expression. */
2968 return rvalue (result);
2970 tree fn = current_nonlambda_function ();
2971 if (fn && DECL_STATIC_FUNCTION_P (fn))
2972 error ("%<this%> is unavailable for static member functions");
2973 else if (fn)
2974 error ("invalid use of %<this%> in non-member function");
2975 else
2976 error ("invalid use of %<this%> at top level");
2977 return error_mark_node;
2980 /* Finish a pseudo-destructor expression. If SCOPE is NULL, the
2981 expression was of the form `OBJECT.~DESTRUCTOR' where DESTRUCTOR is
2982 the TYPE for the type given. If SCOPE is non-NULL, the expression
2983 was of the form `OBJECT.SCOPE::~DESTRUCTOR'. */
2985 tree
2986 finish_pseudo_destructor_expr (tree object, tree scope, tree destructor,
2987 location_t loc)
2989 if (object == error_mark_node || destructor == error_mark_node)
2990 return error_mark_node;
2992 gcc_assert (TYPE_P (destructor));
2994 if (!processing_template_decl)
2996 if (scope == error_mark_node)
2998 error_at (loc, "invalid qualifying scope in pseudo-destructor name");
2999 return error_mark_node;
3001 if (is_auto (destructor))
3002 destructor = TREE_TYPE (object);
3003 if (scope && TYPE_P (scope) && !check_dtor_name (scope, destructor))
3005 error_at (loc,
3006 "qualified type %qT does not match destructor name ~%qT",
3007 scope, destructor);
3008 return error_mark_node;
3012 /* [expr.pseudo] says both:
3014 The type designated by the pseudo-destructor-name shall be
3015 the same as the object type.
3017 and:
3019 The cv-unqualified versions of the object type and of the
3020 type designated by the pseudo-destructor-name shall be the
3021 same type.
3023 We implement the more generous second sentence, since that is
3024 what most other compilers do. */
3025 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (object),
3026 destructor))
3028 error_at (loc, "%qE is not of type %qT", object, destructor);
3029 return error_mark_node;
3033 tree type = (type_dependent_expression_p (object)
3034 ? NULL_TREE : void_type_node);
3036 return build3_loc (loc, PSEUDO_DTOR_EXPR, type, object,
3037 scope, destructor);
3040 /* Finish an expression of the form CODE EXPR. */
3042 cp_expr
3043 finish_unary_op_expr (location_t op_loc, enum tree_code code, cp_expr expr,
3044 tsubst_flags_t complain)
3046 /* Build a location of the form:
3047 ++expr
3048 ^~~~~~
3049 with the caret at the operator token, ranging from the start
3050 of the operator token to the end of EXPR. */
3051 location_t combined_loc = make_location (op_loc,
3052 op_loc, expr.get_finish ());
3053 cp_expr result = build_x_unary_op (combined_loc, code, expr, complain);
3054 /* TODO: build_x_unary_op doesn't always honor the location. */
3055 result.set_location (combined_loc);
3057 if (result == error_mark_node)
3058 return result;
3060 if (!(complain & tf_warning))
3061 return result;
3063 tree result_ovl = result;
3064 tree expr_ovl = expr;
3066 if (!processing_template_decl)
3067 expr_ovl = cp_fully_fold (expr_ovl);
3069 if (!CONSTANT_CLASS_P (expr_ovl)
3070 || TREE_OVERFLOW_P (expr_ovl))
3071 return result;
3073 if (!processing_template_decl)
3074 result_ovl = cp_fully_fold (result_ovl);
3076 if (CONSTANT_CLASS_P (result_ovl) && TREE_OVERFLOW_P (result_ovl))
3077 overflow_warning (combined_loc, result_ovl);
3079 return result;
3082 /* Finish a compound-literal expression or C++11 functional cast with aggregate
3083 initializer. TYPE is the type to which the CONSTRUCTOR in COMPOUND_LITERAL
3084 is being cast. */
3086 tree
3087 finish_compound_literal (tree type, tree compound_literal,
3088 tsubst_flags_t complain,
3089 fcl_t fcl_context)
3091 if (type == error_mark_node)
3092 return error_mark_node;
3094 if (TYPE_REF_P (type))
3096 compound_literal
3097 = finish_compound_literal (TREE_TYPE (type), compound_literal,
3098 complain, fcl_context);
3099 /* The prvalue is then used to direct-initialize the reference. */
3100 tree r = (perform_implicit_conversion_flags
3101 (type, compound_literal, complain, LOOKUP_NORMAL));
3102 return convert_from_reference (r);
3105 if (!TYPE_OBJ_P (type))
3107 if (complain & tf_error)
3108 error ("compound literal of non-object type %qT", type);
3109 return error_mark_node;
3112 if (template_placeholder_p (type))
3114 type = do_auto_deduction (type, compound_literal, type, complain,
3115 adc_variable_type);
3116 if (type == error_mark_node)
3117 return error_mark_node;
3120 /* Used to hold a copy of the compound literal in a template. */
3121 tree orig_cl = NULL_TREE;
3123 if (processing_template_decl)
3125 const bool dependent_p
3126 = (instantiation_dependent_expression_p (compound_literal)
3127 || dependent_type_p (type));
3128 if (dependent_p)
3129 /* We're about to return, no need to copy. */
3130 orig_cl = compound_literal;
3131 else
3132 /* We're going to need a copy. */
3133 orig_cl = unshare_constructor (compound_literal);
3134 TREE_TYPE (orig_cl) = type;
3135 /* Mark the expression as a compound literal. */
3136 TREE_HAS_CONSTRUCTOR (orig_cl) = 1;
3137 /* And as instantiation-dependent. */
3138 CONSTRUCTOR_IS_DEPENDENT (orig_cl) = dependent_p;
3139 if (fcl_context == fcl_c99)
3140 CONSTRUCTOR_C99_COMPOUND_LITERAL (orig_cl) = 1;
3141 /* If the compound literal is dependent, we're done for now. */
3142 if (dependent_p)
3143 return orig_cl;
3144 /* Otherwise, do go on to e.g. check narrowing. */
3147 type = complete_type (type);
3149 if (TYPE_NON_AGGREGATE_CLASS (type))
3151 /* Trying to deal with a CONSTRUCTOR instead of a TREE_LIST
3152 everywhere that deals with function arguments would be a pain, so
3153 just wrap it in a TREE_LIST. The parser set a flag so we know
3154 that it came from T{} rather than T({}). */
3155 CONSTRUCTOR_IS_DIRECT_INIT (compound_literal) = 1;
3156 compound_literal = build_tree_list (NULL_TREE, compound_literal);
3157 return build_functional_cast (input_location, type,
3158 compound_literal, complain);
3161 if (TREE_CODE (type) == ARRAY_TYPE
3162 && check_array_initializer (NULL_TREE, type, compound_literal))
3163 return error_mark_node;
3164 compound_literal = reshape_init (type, compound_literal, complain);
3165 if (SCALAR_TYPE_P (type)
3166 && !BRACE_ENCLOSED_INITIALIZER_P (compound_literal))
3168 tree t = instantiate_non_dependent_expr_sfinae (compound_literal,
3169 complain);
3170 if (!check_narrowing (type, t, complain))
3171 return error_mark_node;
3173 if (TREE_CODE (type) == ARRAY_TYPE
3174 && TYPE_DOMAIN (type) == NULL_TREE)
3176 cp_complete_array_type_or_error (&type, compound_literal,
3177 false, complain);
3178 if (type == error_mark_node)
3179 return error_mark_node;
3181 compound_literal = digest_init_flags (type, compound_literal,
3182 LOOKUP_NORMAL | LOOKUP_NO_NARROWING,
3183 complain);
3184 if (compound_literal == error_mark_node)
3185 return error_mark_node;
3187 /* If we're in a template, return the original compound literal. */
3188 if (orig_cl)
3189 return orig_cl;
3191 if (TREE_CODE (compound_literal) == CONSTRUCTOR)
3193 TREE_HAS_CONSTRUCTOR (compound_literal) = true;
3194 if (fcl_context == fcl_c99)
3195 CONSTRUCTOR_C99_COMPOUND_LITERAL (compound_literal) = 1;
3198 /* Put static/constant array temporaries in static variables. */
3199 /* FIXME all C99 compound literals should be variables rather than C++
3200 temporaries, unless they are used as an aggregate initializer. */
3201 if ((!at_function_scope_p () || CP_TYPE_CONST_P (type))
3202 && fcl_context == fcl_c99
3203 && TREE_CODE (type) == ARRAY_TYPE
3204 && !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
3205 && initializer_constant_valid_p (compound_literal, type))
3207 tree decl = create_temporary_var (type);
3208 DECL_CONTEXT (decl) = NULL_TREE;
3209 DECL_INITIAL (decl) = compound_literal;
3210 TREE_STATIC (decl) = 1;
3211 if (literal_type_p (type) && CP_TYPE_CONST_NON_VOLATILE_P (type))
3213 /* 5.19 says that a constant expression can include an
3214 lvalue-rvalue conversion applied to "a glvalue of literal type
3215 that refers to a non-volatile temporary object initialized
3216 with a constant expression". Rather than try to communicate
3217 that this VAR_DECL is a temporary, just mark it constexpr. */
3218 DECL_DECLARED_CONSTEXPR_P (decl) = true;
3219 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
3220 TREE_CONSTANT (decl) = true;
3222 cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
3223 decl = pushdecl_top_level (decl);
3224 DECL_NAME (decl) = make_anon_name ();
3225 SET_DECL_ASSEMBLER_NAME (decl, DECL_NAME (decl));
3226 /* Make sure the destructor is callable. */
3227 tree clean = cxx_maybe_build_cleanup (decl, complain);
3228 if (clean == error_mark_node)
3229 return error_mark_node;
3230 return decl;
3233 /* Represent other compound literals with TARGET_EXPR so we produce
3234 a prvalue, and can elide copies. */
3235 if (!VECTOR_TYPE_P (type))
3237 /* The CONSTRUCTOR is now an initializer, not a compound literal. */
3238 TREE_HAS_CONSTRUCTOR (compound_literal) = false;
3239 compound_literal = get_target_expr_sfinae (compound_literal, complain);
3242 return compound_literal;
3245 /* Return the declaration for the function-name variable indicated by
3246 ID. */
3248 tree
3249 finish_fname (tree id)
3251 tree decl;
3253 decl = fname_decl (input_location, C_RID_CODE (id), id);
3254 if (processing_template_decl && current_function_decl
3255 && decl != error_mark_node)
3256 decl = DECL_NAME (decl);
3257 return decl;
3260 /* Finish a translation unit. */
3262 void
3263 finish_translation_unit (void)
3265 /* In case there were missing closebraces,
3266 get us back to the global binding level. */
3267 pop_everything ();
3268 while (current_namespace != global_namespace)
3269 pop_namespace ();
3271 /* Do file scope __FUNCTION__ et al. */
3272 finish_fname_decls ();
3274 if (vec_safe_length (scope_chain->omp_declare_target_attribute))
3276 if (!errorcount)
3277 error ("%<#pragma omp declare target%> without corresponding "
3278 "%<#pragma omp end declare target%>");
3279 vec_safe_truncate (scope_chain->omp_declare_target_attribute, 0);
3283 /* Finish a template type parameter, specified as AGGR IDENTIFIER.
3284 Returns the parameter. */
3286 tree
3287 finish_template_type_parm (tree aggr, tree identifier)
3289 if (aggr != class_type_node)
3291 permerror (input_location, "template type parameters must use the keyword %<class%> or %<typename%>");
3292 aggr = class_type_node;
3295 return build_tree_list (aggr, identifier);
3298 /* Finish a template template parameter, specified as AGGR IDENTIFIER.
3299 Returns the parameter. */
3301 tree
3302 finish_template_template_parm (tree aggr, tree identifier)
3304 tree decl = build_decl (input_location,
3305 TYPE_DECL, identifier, NULL_TREE);
3307 tree tmpl = build_lang_decl (TEMPLATE_DECL, identifier, NULL_TREE);
3308 DECL_TEMPLATE_PARMS (tmpl) = current_template_parms;
3309 DECL_TEMPLATE_RESULT (tmpl) = decl;
3310 DECL_ARTIFICIAL (decl) = 1;
3312 /* Associate the constraints with the underlying declaration,
3313 not the template. */
3314 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
3315 tree constr = build_constraints (reqs, NULL_TREE);
3316 set_constraints (decl, constr);
3318 end_template_decl ();
3320 gcc_assert (DECL_TEMPLATE_PARMS (tmpl));
3322 check_default_tmpl_args (decl, DECL_TEMPLATE_PARMS (tmpl),
3323 /*is_primary=*/true, /*is_partial=*/false,
3324 /*is_friend=*/0);
3326 return finish_template_type_parm (aggr, tmpl);
3329 /* ARGUMENT is the default-argument value for a template template
3330 parameter. If ARGUMENT is invalid, issue error messages and return
3331 the ERROR_MARK_NODE. Otherwise, ARGUMENT itself is returned. */
3333 tree
3334 check_template_template_default_arg (tree argument)
3336 if (TREE_CODE (argument) != TEMPLATE_DECL
3337 && TREE_CODE (argument) != TEMPLATE_TEMPLATE_PARM
3338 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
3340 if (TREE_CODE (argument) == TYPE_DECL)
3341 error ("invalid use of type %qT as a default value for a template "
3342 "template-parameter", TREE_TYPE (argument));
3343 else
3344 error ("invalid default argument for a template template parameter");
3345 return error_mark_node;
3348 return argument;
3351 /* Begin a class definition, as indicated by T. */
3353 tree
3354 begin_class_definition (tree t)
3356 if (error_operand_p (t) || error_operand_p (TYPE_MAIN_DECL (t)))
3357 return error_mark_node;
3359 if (processing_template_parmlist && !LAMBDA_TYPE_P (t))
3361 error ("definition of %q#T inside template parameter list", t);
3362 return error_mark_node;
3365 /* According to the C++ ABI, decimal classes defined in ISO/IEC TR 24733
3366 are passed the same as decimal scalar types. */
3367 if (TREE_CODE (t) == RECORD_TYPE
3368 && !processing_template_decl)
3370 tree ns = TYPE_CONTEXT (t);
3371 if (ns && TREE_CODE (ns) == NAMESPACE_DECL
3372 && DECL_CONTEXT (ns) == std_node
3373 && DECL_NAME (ns)
3374 && id_equal (DECL_NAME (ns), "decimal"))
3376 const char *n = TYPE_NAME_STRING (t);
3377 if ((strcmp (n, "decimal32") == 0)
3378 || (strcmp (n, "decimal64") == 0)
3379 || (strcmp (n, "decimal128") == 0))
3380 TYPE_TRANSPARENT_AGGR (t) = 1;
3384 /* A non-implicit typename comes from code like:
3386 template <typename T> struct A {
3387 template <typename U> struct A<T>::B ...
3389 This is erroneous. */
3390 else if (TREE_CODE (t) == TYPENAME_TYPE)
3392 error ("invalid definition of qualified type %qT", t);
3393 t = error_mark_node;
3396 if (t == error_mark_node || ! MAYBE_CLASS_TYPE_P (t))
3398 t = make_class_type (RECORD_TYPE);
3399 pushtag (make_anon_name (), t);
3402 if (TYPE_BEING_DEFINED (t))
3404 t = make_class_type (TREE_CODE (t));
3405 pushtag (TYPE_IDENTIFIER (t), t);
3408 if (modules_p ())
3410 if (!module_may_redeclare (TYPE_NAME (t)))
3412 error ("cannot declare %qD in a different module", TYPE_NAME (t));
3413 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t)), "declared here");
3414 return error_mark_node;
3416 set_instantiating_module (TYPE_NAME (t));
3417 set_defining_module (TYPE_NAME (t));
3420 maybe_process_partial_specialization (t);
3421 pushclass (t);
3422 TYPE_BEING_DEFINED (t) = 1;
3423 class_binding_level->defining_class_p = 1;
3425 if (flag_pack_struct)
3427 tree v;
3428 TYPE_PACKED (t) = 1;
3429 /* Even though the type is being defined for the first time
3430 here, there might have been a forward declaration, so there
3431 might be cv-qualified variants of T. */
3432 for (v = TYPE_NEXT_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v))
3433 TYPE_PACKED (v) = 1;
3435 /* Reset the interface data, at the earliest possible
3436 moment, as it might have been set via a class foo;
3437 before. */
3438 if (! TYPE_UNNAMED_P (t))
3440 struct c_fileinfo *finfo = \
3441 get_fileinfo (LOCATION_FILE (input_location));
3442 CLASSTYPE_INTERFACE_ONLY (t) = finfo->interface_only;
3443 SET_CLASSTYPE_INTERFACE_UNKNOWN_X
3444 (t, finfo->interface_unknown);
3446 reset_specialization ();
3448 /* Make a declaration for this class in its own scope. */
3449 build_self_reference ();
3451 return t;
3454 /* Finish the member declaration given by DECL. */
3456 void
3457 finish_member_declaration (tree decl)
3459 if (decl == error_mark_node || decl == NULL_TREE)
3460 return;
3462 if (decl == void_type_node)
3463 /* The COMPONENT was a friend, not a member, and so there's
3464 nothing for us to do. */
3465 return;
3467 /* We should see only one DECL at a time. */
3468 gcc_assert (DECL_CHAIN (decl) == NULL_TREE);
3470 /* Don't add decls after definition. */
3471 gcc_assert (TYPE_BEING_DEFINED (current_class_type)
3472 /* We can add lambda types when late parsing default
3473 arguments. */
3474 || LAMBDA_TYPE_P (TREE_TYPE (decl)));
3476 /* Set up access control for DECL. */
3477 TREE_PRIVATE (decl)
3478 = (current_access_specifier == access_private_node);
3479 TREE_PROTECTED (decl)
3480 = (current_access_specifier == access_protected_node);
3481 if (TREE_CODE (decl) == TEMPLATE_DECL)
3483 TREE_PRIVATE (DECL_TEMPLATE_RESULT (decl)) = TREE_PRIVATE (decl);
3484 TREE_PROTECTED (DECL_TEMPLATE_RESULT (decl)) = TREE_PROTECTED (decl);
3487 /* Mark the DECL as a member of the current class, unless it's
3488 a member of an enumeration. */
3489 if (TREE_CODE (decl) != CONST_DECL)
3490 DECL_CONTEXT (decl) = current_class_type;
3492 if (TREE_CODE (decl) == USING_DECL)
3493 /* For now, ignore class-scope USING_DECLS, so that debugging
3494 backends do not see them. */
3495 DECL_IGNORED_P (decl) = 1;
3497 /* Check for bare parameter packs in the non-static data member
3498 declaration. */
3499 if (TREE_CODE (decl) == FIELD_DECL)
3501 if (check_for_bare_parameter_packs (TREE_TYPE (decl)))
3502 TREE_TYPE (decl) = error_mark_node;
3503 if (check_for_bare_parameter_packs (DECL_ATTRIBUTES (decl)))
3504 DECL_ATTRIBUTES (decl) = NULL_TREE;
3507 /* [dcl.link]
3509 A C language linkage is ignored for the names of class members
3510 and the member function type of class member functions. */
3511 if (DECL_LANG_SPECIFIC (decl))
3512 SET_DECL_LANGUAGE (decl, lang_cplusplus);
3514 bool add = false;
3516 /* Functions and non-functions are added differently. */
3517 if (DECL_DECLARES_FUNCTION_P (decl))
3518 add = add_method (current_class_type, decl, false);
3519 /* Enter the DECL into the scope of the class, if the class
3520 isn't a closure (whose fields are supposed to be unnamed). */
3521 else if (CLASSTYPE_LAMBDA_EXPR (current_class_type)
3522 || pushdecl_class_level (decl))
3523 add = true;
3525 if (add)
3527 /* All TYPE_DECLs go at the end of TYPE_FIELDS. Ordinary fields
3528 go at the beginning. The reason is that
3529 legacy_nonfn_member_lookup searches the list in order, and we
3530 want a field name to override a type name so that the "struct
3531 stat hack" will work. In particular:
3533 struct S { enum E { }; static const int E = 5; int ary[S::E]; } s;
3535 is valid. */
3537 if (TREE_CODE (decl) == TYPE_DECL)
3538 TYPE_FIELDS (current_class_type)
3539 = chainon (TYPE_FIELDS (current_class_type), decl);
3540 else
3542 DECL_CHAIN (decl) = TYPE_FIELDS (current_class_type);
3543 TYPE_FIELDS (current_class_type) = decl;
3546 maybe_add_class_template_decl_list (current_class_type, decl,
3547 /*friend_p=*/0);
3551 /* Finish processing a complete template declaration. The PARMS are
3552 the template parameters. */
3554 void
3555 finish_template_decl (tree parms)
3557 if (parms)
3558 end_template_decl ();
3559 else
3560 end_specialization ();
3563 // Returns the template type of the class scope being entered. If we're
3564 // entering a constrained class scope. TYPE is the class template
3565 // scope being entered and we may need to match the intended type with
3566 // a constrained specialization. For example:
3568 // template<Object T>
3569 // struct S { void f(); }; #1
3571 // template<Object T>
3572 // void S<T>::f() { } #2
3574 // We check, in #2, that S<T> refers precisely to the type declared by
3575 // #1 (i.e., that the constraints match). Note that the following should
3576 // be an error since there is no specialization of S<T> that is
3577 // unconstrained, but this is not diagnosed here.
3579 // template<typename T>
3580 // void S<T>::f() { }
3582 // We cannot diagnose this problem here since this function also matches
3583 // qualified template names that are not part of a definition. For example:
3585 // template<Integral T, Floating_point U>
3586 // typename pair<T, U>::first_type void f(T, U);
3588 // Here, it is unlikely that there is a partial specialization of
3589 // pair constrained for for Integral and Floating_point arguments.
3591 // The general rule is: if a constrained specialization with matching
3592 // constraints is found return that type. Also note that if TYPE is not a
3593 // class-type (e.g. a typename type), then no fixup is needed.
3595 static tree
3596 fixup_template_type (tree type)
3598 // Find the template parameter list at the a depth appropriate to
3599 // the scope we're trying to enter.
3600 tree parms = current_template_parms;
3601 int depth = template_class_depth (type);
3602 for (int n = processing_template_decl; n > depth && parms; --n)
3603 parms = TREE_CHAIN (parms);
3604 if (!parms)
3605 return type;
3606 tree cur_reqs = TEMPLATE_PARMS_CONSTRAINTS (parms);
3607 tree cur_constr = build_constraints (cur_reqs, NULL_TREE);
3609 // Search for a specialization whose type and constraints match.
3610 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
3611 tree specs = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
3612 while (specs)
3614 tree spec_constr = get_constraints (TREE_VALUE (specs));
3616 // If the type and constraints match a specialization, then we
3617 // are entering that type.
3618 if (same_type_p (type, TREE_TYPE (specs))
3619 && equivalent_constraints (cur_constr, spec_constr))
3620 return TREE_TYPE (specs);
3621 specs = TREE_CHAIN (specs);
3624 // If no specialization matches, then must return the type
3625 // previously found.
3626 return type;
3629 /* Finish processing a template-id (which names a type) of the form
3630 NAME < ARGS >. Return the TYPE_DECL for the type named by the
3631 template-id. If ENTERING_SCOPE is nonzero we are about to enter
3632 the scope of template-id indicated. */
3634 tree
3635 finish_template_type (tree name, tree args, int entering_scope)
3637 tree type;
3639 type = lookup_template_class (name, args,
3640 NULL_TREE, NULL_TREE, entering_scope,
3641 tf_warning_or_error | tf_user);
3643 /* If we might be entering the scope of a partial specialization,
3644 find the one with the right constraints. */
3645 if (flag_concepts
3646 && entering_scope
3647 && CLASS_TYPE_P (type)
3648 && CLASSTYPE_TEMPLATE_INFO (type)
3649 && dependent_type_p (type)
3650 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
3651 type = fixup_template_type (type);
3653 if (type == error_mark_node)
3654 return type;
3655 else if (CLASS_TYPE_P (type) && !alias_type_or_template_p (type))
3656 return TYPE_STUB_DECL (type);
3657 else
3658 return TYPE_NAME (type);
3661 /* Finish processing a BASE_CLASS with the indicated ACCESS_SPECIFIER.
3662 Return a TREE_LIST containing the ACCESS_SPECIFIER and the
3663 BASE_CLASS, or NULL_TREE if an error occurred. The
3664 ACCESS_SPECIFIER is one of
3665 access_{default,public,protected_private}_node. For a virtual base
3666 we set TREE_TYPE. */
3668 tree
3669 finish_base_specifier (tree base, tree access, bool virtual_p)
3671 tree result;
3673 if (base == error_mark_node)
3675 error ("invalid base-class specification");
3676 result = NULL_TREE;
3678 else if (! MAYBE_CLASS_TYPE_P (base))
3680 error ("%qT is not a class type", base);
3681 result = NULL_TREE;
3683 else
3685 if (cp_type_quals (base) != 0)
3687 /* DR 484: Can a base-specifier name a cv-qualified
3688 class type? */
3689 base = TYPE_MAIN_VARIANT (base);
3691 result = build_tree_list (access, base);
3692 if (virtual_p)
3693 TREE_TYPE (result) = integer_type_node;
3696 return result;
3699 /* If FNS is a member function, a set of member functions, or a
3700 template-id referring to one or more member functions, return a
3701 BASELINK for FNS, incorporating the current access context.
3702 Otherwise, return FNS unchanged. */
3704 tree
3705 baselink_for_fns (tree fns)
3707 tree scope;
3708 tree cl;
3710 if (BASELINK_P (fns)
3711 || error_operand_p (fns))
3712 return fns;
3714 scope = ovl_scope (fns);
3715 if (!CLASS_TYPE_P (scope))
3716 return fns;
3718 cl = currently_open_derived_class (scope);
3719 if (!cl)
3720 cl = scope;
3721 tree access_path = TYPE_BINFO (cl);
3722 tree conv_path = (cl == scope ? access_path
3723 : lookup_base (cl, scope, ba_any, NULL, tf_none));
3724 return build_baselink (conv_path, access_path, fns, /*optype=*/NULL_TREE);
3727 /* Returns true iff DECL is a variable from a function outside
3728 the current one. */
3730 static bool
3731 outer_var_p (tree decl)
3733 return ((VAR_P (decl) || TREE_CODE (decl) == PARM_DECL)
3734 && DECL_FUNCTION_SCOPE_P (decl)
3735 /* Don't get confused by temporaries. */
3736 && DECL_NAME (decl)
3737 && (DECL_CONTEXT (decl) != current_function_decl
3738 || parsing_nsdmi ()));
3741 /* As above, but also checks that DECL is automatic. */
3743 bool
3744 outer_automatic_var_p (tree decl)
3746 return (outer_var_p (decl)
3747 && !TREE_STATIC (decl));
3750 /* DECL satisfies outer_automatic_var_p. Possibly complain about it or
3751 rewrite it for lambda capture.
3753 If ODR_USE is true, we're being called from mark_use, and we complain about
3754 use of constant variables. If ODR_USE is false, we're being called for the
3755 id-expression, and we do lambda capture. */
3757 tree
3758 process_outer_var_ref (tree decl, tsubst_flags_t complain, bool odr_use)
3760 if (cp_unevaluated_operand)
3762 tree type = TREE_TYPE (decl);
3763 if (!dependent_type_p (type)
3764 && variably_modified_type_p (type, NULL_TREE))
3765 /* VLAs are used even in unevaluated context. */;
3766 else
3767 /* It's not a use (3.2) if we're in an unevaluated context. */
3768 return decl;
3770 if (decl == error_mark_node)
3771 return decl;
3773 tree context = DECL_CONTEXT (decl);
3774 tree containing_function = current_function_decl;
3775 tree lambda_stack = NULL_TREE;
3776 tree lambda_expr = NULL_TREE;
3777 tree initializer = convert_from_reference (decl);
3779 /* Mark it as used now even if the use is ill-formed. */
3780 if (!mark_used (decl, complain))
3781 return error_mark_node;
3783 if (parsing_nsdmi ())
3784 containing_function = NULL_TREE;
3786 if (containing_function && LAMBDA_FUNCTION_P (containing_function))
3788 /* Check whether we've already built a proxy. */
3789 tree var = decl;
3790 while (is_normal_capture_proxy (var))
3791 var = DECL_CAPTURED_VARIABLE (var);
3792 tree d = retrieve_local_specialization (var);
3794 if (d && d != decl && is_capture_proxy (d))
3796 if (DECL_CONTEXT (d) == containing_function)
3797 /* We already have an inner proxy. */
3798 return d;
3799 else
3800 /* We need to capture an outer proxy. */
3801 return process_outer_var_ref (d, complain, odr_use);
3805 /* If we are in a lambda function, we can move out until we hit
3806 1. the context,
3807 2. a non-lambda function, or
3808 3. a non-default capturing lambda function. */
3809 while (context != containing_function
3810 /* containing_function can be null with invalid generic lambdas. */
3811 && containing_function
3812 && LAMBDA_FUNCTION_P (containing_function))
3814 tree closure = DECL_CONTEXT (containing_function);
3815 lambda_expr = CLASSTYPE_LAMBDA_EXPR (closure);
3817 if (TYPE_CLASS_SCOPE_P (closure))
3818 /* A lambda in an NSDMI (c++/64496). */
3819 break;
3821 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_NONE)
3822 break;
3824 lambda_stack = tree_cons (NULL_TREE, lambda_expr, lambda_stack);
3826 containing_function = decl_function_context (containing_function);
3829 /* In a lambda within a template, wait until instantiation time to implicitly
3830 capture a parameter pack. We want to wait because we don't know if we're
3831 capturing the whole pack or a single element, and it's OK to wait because
3832 find_parameter_packs_r walks into the lambda body. */
3833 if (context == containing_function
3834 && DECL_PACK_P (decl))
3835 return decl;
3837 if (lambda_expr && VAR_P (decl) && DECL_ANON_UNION_VAR_P (decl))
3839 if (complain & tf_error)
3840 error ("cannot capture member %qD of anonymous union", decl);
3841 return error_mark_node;
3843 /* Do lambda capture when processing the id-expression, not when
3844 odr-using a variable. */
3845 if (!odr_use && context == containing_function)
3846 decl = add_default_capture (lambda_stack,
3847 /*id=*/DECL_NAME (decl), initializer);
3848 /* Only an odr-use of an outer automatic variable causes an
3849 error, and a constant variable can decay to a prvalue
3850 constant without odr-use. So don't complain yet. */
3851 else if (!odr_use && decl_constant_var_p (decl))
3852 return decl;
3853 else if (lambda_expr)
3855 if (complain & tf_error)
3857 error ("%qD is not captured", decl);
3858 tree closure = LAMBDA_EXPR_CLOSURE (lambda_expr);
3859 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_NONE)
3860 inform (location_of (closure),
3861 "the lambda has no capture-default");
3862 else if (TYPE_CLASS_SCOPE_P (closure))
3863 inform (UNKNOWN_LOCATION, "lambda in local class %q+T cannot "
3864 "capture variables from the enclosing context",
3865 TYPE_CONTEXT (closure));
3866 inform (DECL_SOURCE_LOCATION (decl), "%q#D declared here", decl);
3868 return error_mark_node;
3870 else
3872 if (complain & tf_error)
3874 error (VAR_P (decl)
3875 ? G_("use of local variable with automatic storage from "
3876 "containing function")
3877 : G_("use of parameter from containing function"));
3878 inform (DECL_SOURCE_LOCATION (decl), "%q#D declared here", decl);
3880 return error_mark_node;
3882 return decl;
3885 /* ID_EXPRESSION is a representation of parsed, but unprocessed,
3886 id-expression. (See cp_parser_id_expression for details.) SCOPE,
3887 if non-NULL, is the type or namespace used to explicitly qualify
3888 ID_EXPRESSION. DECL is the entity to which that name has been
3889 resolved.
3891 *CONSTANT_EXPRESSION_P is true if we are presently parsing a
3892 constant-expression. In that case, *NON_CONSTANT_EXPRESSION_P will
3893 be set to true if this expression isn't permitted in a
3894 constant-expression, but it is otherwise not set by this function.
3895 *ALLOW_NON_CONSTANT_EXPRESSION_P is true if we are parsing a
3896 constant-expression, but a non-constant expression is also
3897 permissible.
3899 DONE is true if this expression is a complete postfix-expression;
3900 it is false if this expression is followed by '->', '[', '(', etc.
3901 ADDRESS_P is true iff this expression is the operand of '&'.
3902 TEMPLATE_P is true iff the qualified-id was of the form
3903 "A::template B". TEMPLATE_ARG_P is true iff this qualified name
3904 appears as a template argument.
3906 If an error occurs, and it is the kind of error that might cause
3907 the parser to abort a tentative parse, *ERROR_MSG is filled in. It
3908 is the caller's responsibility to issue the message. *ERROR_MSG
3909 will be a string with static storage duration, so the caller need
3910 not "free" it.
3912 Return an expression for the entity, after issuing appropriate
3913 diagnostics. This function is also responsible for transforming a
3914 reference to a non-static member into a COMPONENT_REF that makes
3915 the use of "this" explicit.
3917 Upon return, *IDK will be filled in appropriately. */
3918 static cp_expr
3919 finish_id_expression_1 (tree id_expression,
3920 tree decl,
3921 tree scope,
3922 cp_id_kind *idk,
3923 bool integral_constant_expression_p,
3924 bool allow_non_integral_constant_expression_p,
3925 bool *non_integral_constant_expression_p,
3926 bool template_p,
3927 bool done,
3928 bool address_p,
3929 bool template_arg_p,
3930 const char **error_msg,
3931 location_t location)
3933 decl = strip_using_decl (decl);
3935 /* Initialize the output parameters. */
3936 *idk = CP_ID_KIND_NONE;
3937 *error_msg = NULL;
3939 if (id_expression == error_mark_node)
3940 return error_mark_node;
3941 /* If we have a template-id, then no further lookup is
3942 required. If the template-id was for a template-class, we
3943 will sometimes have a TYPE_DECL at this point. */
3944 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3945 || TREE_CODE (decl) == TYPE_DECL)
3947 /* Look up the name. */
3948 else
3950 if (decl == error_mark_node)
3952 /* Name lookup failed. */
3953 if (scope
3954 && (!TYPE_P (scope)
3955 || (!dependent_type_p (scope)
3956 && !(identifier_p (id_expression)
3957 && IDENTIFIER_CONV_OP_P (id_expression)
3958 && dependent_type_p (TREE_TYPE (id_expression))))))
3960 /* If the qualifying type is non-dependent (and the name
3961 does not name a conversion operator to a dependent
3962 type), issue an error. */
3963 qualified_name_lookup_error (scope, id_expression, decl, location);
3964 return error_mark_node;
3966 else if (!scope)
3968 /* It may be resolved via Koenig lookup. */
3969 *idk = CP_ID_KIND_UNQUALIFIED;
3970 return id_expression;
3972 else
3973 decl = id_expression;
3976 /* Remember that the name was used in the definition of
3977 the current class so that we can check later to see if
3978 the meaning would have been different after the class
3979 was entirely defined. */
3980 if (!scope && decl != error_mark_node && identifier_p (id_expression))
3981 maybe_note_name_used_in_class (id_expression, decl);
3983 /* A use in unevaluated operand might not be instantiated appropriately
3984 if tsubst_copy builds a dummy parm, or if we never instantiate a
3985 generic lambda, so mark it now. */
3986 if (processing_template_decl && cp_unevaluated_operand)
3987 mark_type_use (decl);
3989 /* Disallow uses of local variables from containing functions, except
3990 within lambda-expressions. */
3991 if (outer_automatic_var_p (decl))
3993 decl = process_outer_var_ref (decl, tf_warning_or_error);
3994 if (decl == error_mark_node)
3995 return error_mark_node;
3998 /* Also disallow uses of function parameters outside the function
3999 body, except inside an unevaluated context (i.e. decltype). */
4000 if (TREE_CODE (decl) == PARM_DECL
4001 && DECL_CONTEXT (decl) == NULL_TREE
4002 && !cp_unevaluated_operand)
4004 *error_msg = G_("use of parameter outside function body");
4005 return error_mark_node;
4009 /* If we didn't find anything, or what we found was a type,
4010 then this wasn't really an id-expression. */
4011 if (TREE_CODE (decl) == TEMPLATE_DECL
4012 && !DECL_FUNCTION_TEMPLATE_P (decl))
4014 *error_msg = G_("missing template arguments");
4015 return error_mark_node;
4017 else if (TREE_CODE (decl) == TYPE_DECL
4018 || TREE_CODE (decl) == NAMESPACE_DECL)
4020 *error_msg = G_("expected primary-expression");
4021 return error_mark_node;
4024 /* If the name resolved to a template parameter, there is no
4025 need to look it up again later. */
4026 if ((TREE_CODE (decl) == CONST_DECL && DECL_TEMPLATE_PARM_P (decl))
4027 || TREE_CODE (decl) == TEMPLATE_PARM_INDEX)
4029 tree r;
4031 *idk = CP_ID_KIND_NONE;
4032 if (TREE_CODE (decl) == TEMPLATE_PARM_INDEX)
4033 decl = TEMPLATE_PARM_DECL (decl);
4034 r = DECL_INITIAL (decl);
4035 if (CLASS_TYPE_P (TREE_TYPE (r)) && !CP_TYPE_CONST_P (TREE_TYPE (r)))
4037 /* If the entity is a template parameter object for a template
4038 parameter of type T, the type of the expression is const T. */
4039 tree ctype = TREE_TYPE (r);
4040 ctype = cp_build_qualified_type (ctype, (cp_type_quals (ctype)
4041 | TYPE_QUAL_CONST));
4042 r = build1 (VIEW_CONVERT_EXPR, ctype, r);
4044 r = convert_from_reference (r);
4045 if (integral_constant_expression_p
4046 && !dependent_type_p (TREE_TYPE (decl))
4047 && !(INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (r))))
4049 if (!allow_non_integral_constant_expression_p)
4050 error ("template parameter %qD of type %qT is not allowed in "
4051 "an integral constant expression because it is not of "
4052 "integral or enumeration type", decl, TREE_TYPE (decl));
4053 *non_integral_constant_expression_p = true;
4055 return r;
4057 else
4059 bool dependent_p = type_dependent_expression_p (decl);
4061 /* If the declaration was explicitly qualified indicate
4062 that. The semantics of `A::f(3)' are different than
4063 `f(3)' if `f' is virtual. */
4064 *idk = (scope
4065 ? CP_ID_KIND_QUALIFIED
4066 : (TREE_CODE (decl) == TEMPLATE_ID_EXPR
4067 ? CP_ID_KIND_TEMPLATE_ID
4068 : (dependent_p
4069 ? CP_ID_KIND_UNQUALIFIED_DEPENDENT
4070 : CP_ID_KIND_UNQUALIFIED)));
4072 if (dependent_p
4073 && DECL_P (decl)
4074 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (decl)))
4075 /* Dependent type attributes on the decl mean that the TREE_TYPE is
4076 wrong, so just return the identifier. */
4077 return id_expression;
4079 if (DECL_CLASS_TEMPLATE_P (decl))
4081 error ("use of class template %qT as expression", decl);
4082 return error_mark_node;
4085 if (TREE_CODE (decl) == TREE_LIST)
4087 /* Ambiguous reference to base members. */
4088 error ("request for member %qD is ambiguous in "
4089 "multiple inheritance lattice", id_expression);
4090 print_candidates (decl);
4091 return error_mark_node;
4094 /* Mark variable-like entities as used. Functions are similarly
4095 marked either below or after overload resolution. */
4096 if ((VAR_P (decl)
4097 || TREE_CODE (decl) == PARM_DECL
4098 || TREE_CODE (decl) == CONST_DECL
4099 || TREE_CODE (decl) == RESULT_DECL)
4100 && !mark_used (decl))
4101 return error_mark_node;
4103 /* Only certain kinds of names are allowed in constant
4104 expression. Template parameters have already
4105 been handled above. */
4106 if (! error_operand_p (decl)
4107 && !dependent_p
4108 && integral_constant_expression_p
4109 && !decl_constant_var_p (decl)
4110 && TREE_CODE (decl) != CONST_DECL
4111 && !builtin_valid_in_constant_expr_p (decl)
4112 && !concept_check_p (decl))
4114 if (!allow_non_integral_constant_expression_p)
4116 error ("%qD cannot appear in a constant-expression", decl);
4117 return error_mark_node;
4119 *non_integral_constant_expression_p = true;
4122 if (tree wrap = maybe_get_tls_wrapper_call (decl))
4123 /* Replace an evaluated use of the thread_local variable with
4124 a call to its wrapper. */
4125 decl = wrap;
4126 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
4127 && !dependent_p
4128 && variable_template_p (TREE_OPERAND (decl, 0))
4129 && !concept_check_p (decl))
4131 decl = finish_template_variable (decl);
4132 mark_used (decl);
4133 decl = convert_from_reference (decl);
4135 else if (concept_check_p (decl))
4137 /* Nothing more to do. All of the analysis for concept checks
4138 is done by build_conept_id, called from the parser. */
4140 else if (scope)
4142 if (TREE_CODE (decl) == SCOPE_REF)
4144 gcc_assert (same_type_p (scope, TREE_OPERAND (decl, 0)));
4145 decl = TREE_OPERAND (decl, 1);
4148 decl = (adjust_result_of_qualified_name_lookup
4149 (decl, scope, current_nonlambda_class_type()));
4151 if (TREE_CODE (decl) == FUNCTION_DECL)
4152 mark_used (decl);
4154 cp_warn_deprecated_use_scopes (scope);
4156 if (TYPE_P (scope))
4157 decl = finish_qualified_id_expr (scope,
4158 decl,
4159 done,
4160 address_p,
4161 template_p,
4162 template_arg_p,
4163 tf_warning_or_error);
4164 else
4165 decl = convert_from_reference (decl);
4167 else if (TREE_CODE (decl) == FIELD_DECL)
4169 /* Since SCOPE is NULL here, this is an unqualified name.
4170 Access checking has been performed during name lookup
4171 already. Turn off checking to avoid duplicate errors. */
4172 push_deferring_access_checks (dk_no_check);
4173 decl = finish_non_static_data_member (decl, NULL_TREE,
4174 /*qualifying_scope=*/NULL_TREE);
4175 pop_deferring_access_checks ();
4177 else if (is_overloaded_fn (decl))
4179 /* We only need to look at the first function,
4180 because all the fns share the attribute we're
4181 concerned with (all member fns or all non-members). */
4182 tree first_fn = get_first_fn (decl);
4183 first_fn = STRIP_TEMPLATE (first_fn);
4185 /* [basic.def.odr]: "A function whose name appears as a
4186 potentially-evaluated expression is odr-used if it is the unique
4187 lookup result".
4189 But only mark it if it's a complete postfix-expression; in a call,
4190 ADL might select a different function, and we'll call mark_used in
4191 build_over_call. */
4192 if (done
4193 && !really_overloaded_fn (decl)
4194 && !mark_used (first_fn))
4195 return error_mark_node;
4197 if (!template_arg_p
4198 && (TREE_CODE (first_fn) == USING_DECL
4199 || (TREE_CODE (first_fn) == FUNCTION_DECL
4200 && DECL_FUNCTION_MEMBER_P (first_fn)
4201 && !shared_member_p (decl))))
4203 /* A set of member functions. */
4204 decl = maybe_dummy_object (DECL_CONTEXT (first_fn), 0);
4205 return finish_class_member_access_expr (decl, id_expression,
4206 /*template_p=*/false,
4207 tf_warning_or_error);
4210 decl = baselink_for_fns (decl);
4212 else
4214 if (DECL_P (decl) && DECL_NONLOCAL (decl)
4215 && DECL_CLASS_SCOPE_P (decl))
4217 tree context = context_for_name_lookup (decl);
4218 if (context != current_class_type)
4220 tree path = currently_open_derived_class (context);
4221 if (!path)
4222 /* PATH can be null for using an enum of an unrelated
4223 class; we checked its access in lookup_using_decl.
4225 ??? Should this case make a clone instead, like
4226 handle_using_decl? */
4227 gcc_assert (TREE_CODE (decl) == CONST_DECL);
4228 else
4229 perform_or_defer_access_check (TYPE_BINFO (path),
4230 decl, decl,
4231 tf_warning_or_error);
4235 decl = convert_from_reference (decl);
4239 return cp_expr (decl, location);
4242 /* As per finish_id_expression_1, but adding a wrapper node
4243 around the result if needed to express LOCATION. */
4245 cp_expr
4246 finish_id_expression (tree id_expression,
4247 tree decl,
4248 tree scope,
4249 cp_id_kind *idk,
4250 bool integral_constant_expression_p,
4251 bool allow_non_integral_constant_expression_p,
4252 bool *non_integral_constant_expression_p,
4253 bool template_p,
4254 bool done,
4255 bool address_p,
4256 bool template_arg_p,
4257 const char **error_msg,
4258 location_t location)
4260 cp_expr result
4261 = finish_id_expression_1 (id_expression, decl, scope, idk,
4262 integral_constant_expression_p,
4263 allow_non_integral_constant_expression_p,
4264 non_integral_constant_expression_p,
4265 template_p, done, address_p, template_arg_p,
4266 error_msg, location);
4267 return result.maybe_add_location_wrapper ();
4270 /* Implement the __typeof keyword: Return the type of EXPR, suitable for
4271 use as a type-specifier. */
4273 tree
4274 finish_typeof (tree expr)
4276 tree type;
4278 if (type_dependent_expression_p (expr))
4280 type = cxx_make_type (TYPEOF_TYPE);
4281 TYPEOF_TYPE_EXPR (type) = expr;
4282 SET_TYPE_STRUCTURAL_EQUALITY (type);
4284 return type;
4287 expr = mark_type_use (expr);
4289 type = unlowered_expr_type (expr);
4291 if (!type || type == unknown_type_node)
4293 error ("type of %qE is unknown", expr);
4294 return error_mark_node;
4297 return type;
4300 /* Implement the __underlying_type keyword: Return the underlying
4301 type of TYPE, suitable for use as a type-specifier. */
4303 tree
4304 finish_underlying_type (tree type)
4306 tree underlying_type;
4308 if (processing_template_decl)
4310 underlying_type = cxx_make_type (UNDERLYING_TYPE);
4311 UNDERLYING_TYPE_TYPE (underlying_type) = type;
4312 SET_TYPE_STRUCTURAL_EQUALITY (underlying_type);
4314 return underlying_type;
4317 if (!complete_type_or_else (type, NULL_TREE))
4318 return error_mark_node;
4320 if (TREE_CODE (type) != ENUMERAL_TYPE)
4322 error ("%qT is not an enumeration type", type);
4323 return error_mark_node;
4326 underlying_type = ENUM_UNDERLYING_TYPE (type);
4328 /* Fixup necessary in this case because ENUM_UNDERLYING_TYPE
4329 includes TYPE_MIN_VALUE and TYPE_MAX_VALUE information.
4330 See finish_enum_value_list for details. */
4331 if (!ENUM_FIXED_UNDERLYING_TYPE_P (type))
4332 underlying_type
4333 = c_common_type_for_mode (TYPE_MODE (underlying_type),
4334 TYPE_UNSIGNED (underlying_type));
4336 return underlying_type;
4339 /* Implement the __direct_bases keyword: Return the direct base classes
4340 of type. */
4342 tree
4343 calculate_direct_bases (tree type, tsubst_flags_t complain)
4345 if (!complete_type_or_maybe_complain (type, NULL_TREE, complain)
4346 || !NON_UNION_CLASS_TYPE_P (type))
4347 return make_tree_vec (0);
4349 releasing_vec vector;
4350 vec<tree, va_gc> *base_binfos = BINFO_BASE_BINFOS (TYPE_BINFO (type));
4351 tree binfo;
4352 unsigned i;
4354 /* Virtual bases are initialized first */
4355 for (i = 0; base_binfos->iterate (i, &binfo); i++)
4356 if (BINFO_VIRTUAL_P (binfo))
4357 vec_safe_push (vector, binfo);
4359 /* Now non-virtuals */
4360 for (i = 0; base_binfos->iterate (i, &binfo); i++)
4361 if (!BINFO_VIRTUAL_P (binfo))
4362 vec_safe_push (vector, binfo);
4364 tree bases_vec = make_tree_vec (vector->length ());
4366 for (i = 0; i < vector->length (); ++i)
4367 TREE_VEC_ELT (bases_vec, i) = BINFO_TYPE ((*vector)[i]);
4369 return bases_vec;
4372 /* Implement the __bases keyword: Return the base classes
4373 of type */
4375 /* Find morally non-virtual base classes by walking binfo hierarchy */
4376 /* Virtual base classes are handled separately in finish_bases */
4378 static tree
4379 dfs_calculate_bases_pre (tree binfo, void * /*data_*/)
4381 /* Don't walk bases of virtual bases */
4382 return BINFO_VIRTUAL_P (binfo) ? dfs_skip_bases : NULL_TREE;
4385 static tree
4386 dfs_calculate_bases_post (tree binfo, void *data_)
4388 vec<tree, va_gc> **data = ((vec<tree, va_gc> **) data_);
4389 if (!BINFO_VIRTUAL_P (binfo))
4390 vec_safe_push (*data, BINFO_TYPE (binfo));
4391 return NULL_TREE;
4394 /* Calculates the morally non-virtual base classes of a class */
4395 static vec<tree, va_gc> *
4396 calculate_bases_helper (tree type)
4398 vec<tree, va_gc> *vector = make_tree_vector ();
4400 /* Now add non-virtual base classes in order of construction */
4401 if (TYPE_BINFO (type))
4402 dfs_walk_all (TYPE_BINFO (type),
4403 dfs_calculate_bases_pre, dfs_calculate_bases_post, &vector);
4404 return vector;
4407 tree
4408 calculate_bases (tree type, tsubst_flags_t complain)
4410 if (!complete_type_or_maybe_complain (type, NULL_TREE, complain)
4411 || !NON_UNION_CLASS_TYPE_P (type))
4412 return make_tree_vec (0);
4414 releasing_vec vector;
4415 tree bases_vec = NULL_TREE;
4416 unsigned i;
4417 vec<tree, va_gc> *vbases;
4418 tree binfo;
4420 /* First go through virtual base classes */
4421 for (vbases = CLASSTYPE_VBASECLASSES (type), i = 0;
4422 vec_safe_iterate (vbases, i, &binfo); i++)
4424 releasing_vec vbase_bases
4425 = calculate_bases_helper (BINFO_TYPE (binfo));
4426 vec_safe_splice (vector, vbase_bases);
4429 /* Now for the non-virtual bases */
4430 releasing_vec nonvbases = calculate_bases_helper (type);
4431 vec_safe_splice (vector, nonvbases);
4433 /* Note that during error recovery vector->length can even be zero. */
4434 if (vector->length () > 1)
4436 /* Last element is entire class, so don't copy */
4437 bases_vec = make_tree_vec (vector->length () - 1);
4439 for (i = 0; i < vector->length () - 1; ++i)
4440 TREE_VEC_ELT (bases_vec, i) = (*vector)[i];
4442 else
4443 bases_vec = make_tree_vec (0);
4445 return bases_vec;
4448 tree
4449 finish_bases (tree type, bool direct)
4451 tree bases = NULL_TREE;
4453 if (!processing_template_decl)
4455 /* Parameter packs can only be used in templates */
4456 error ("parameter pack %<__bases%> only valid in template declaration");
4457 return error_mark_node;
4460 bases = cxx_make_type (BASES);
4461 BASES_TYPE (bases) = type;
4462 BASES_DIRECT (bases) = direct;
4463 SET_TYPE_STRUCTURAL_EQUALITY (bases);
4465 return bases;
4468 /* Perform C++-specific checks for __builtin_offsetof before calling
4469 fold_offsetof. */
4471 tree
4472 finish_offsetof (tree object_ptr, tree expr, location_t loc)
4474 /* If we're processing a template, we can't finish the semantics yet.
4475 Otherwise we can fold the entire expression now. */
4476 if (processing_template_decl)
4478 expr = build2 (OFFSETOF_EXPR, size_type_node, expr, object_ptr);
4479 SET_EXPR_LOCATION (expr, loc);
4480 return expr;
4483 if (expr == error_mark_node)
4484 return error_mark_node;
4486 if (TREE_CODE (expr) == PSEUDO_DTOR_EXPR)
4488 error ("cannot apply %<offsetof%> to destructor %<~%T%>",
4489 TREE_OPERAND (expr, 2));
4490 return error_mark_node;
4492 if (FUNC_OR_METHOD_TYPE_P (TREE_TYPE (expr))
4493 || TREE_TYPE (expr) == unknown_type_node)
4495 while (TREE_CODE (expr) == COMPONENT_REF
4496 || TREE_CODE (expr) == COMPOUND_EXPR)
4497 expr = TREE_OPERAND (expr, 1);
4499 if (DECL_P (expr))
4501 error ("cannot apply %<offsetof%> to member function %qD", expr);
4502 inform (DECL_SOURCE_LOCATION (expr), "declared here");
4504 else
4505 error ("cannot apply %<offsetof%> to member function");
4506 return error_mark_node;
4508 if (TREE_CODE (expr) == CONST_DECL)
4510 error ("cannot apply %<offsetof%> to an enumerator %qD", expr);
4511 return error_mark_node;
4513 if (REFERENCE_REF_P (expr))
4514 expr = TREE_OPERAND (expr, 0);
4515 if (!complete_type_or_else (TREE_TYPE (TREE_TYPE (object_ptr)), object_ptr))
4516 return error_mark_node;
4517 if (warn_invalid_offsetof
4518 && CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (object_ptr)))
4519 && CLASSTYPE_NON_STD_LAYOUT (TREE_TYPE (TREE_TYPE (object_ptr)))
4520 && cp_unevaluated_operand == 0)
4521 warning_at (loc, OPT_Winvalid_offsetof, "%<offsetof%> within "
4522 "non-standard-layout type %qT is conditionally-supported",
4523 TREE_TYPE (TREE_TYPE (object_ptr)));
4524 return fold_offsetof (expr);
4527 /* Replace the AGGR_INIT_EXPR at *TP with an equivalent CALL_EXPR. This
4528 function is broken out from the above for the benefit of the tree-ssa
4529 project. */
4531 void
4532 simplify_aggr_init_expr (tree *tp)
4534 tree aggr_init_expr = *tp;
4536 /* Form an appropriate CALL_EXPR. */
4537 tree fn = AGGR_INIT_EXPR_FN (aggr_init_expr);
4538 tree slot = AGGR_INIT_EXPR_SLOT (aggr_init_expr);
4539 tree type = TREE_TYPE (slot);
4541 tree call_expr;
4542 enum style_t { ctor, arg, pcc } style;
4544 if (AGGR_INIT_VIA_CTOR_P (aggr_init_expr))
4545 style = ctor;
4546 #ifdef PCC_STATIC_STRUCT_RETURN
4547 else if (1)
4548 style = pcc;
4549 #endif
4550 else
4552 gcc_assert (TREE_ADDRESSABLE (type));
4553 style = arg;
4556 call_expr = build_call_array_loc (input_location,
4557 TREE_TYPE (TREE_TYPE (TREE_TYPE (fn))),
4559 aggr_init_expr_nargs (aggr_init_expr),
4560 AGGR_INIT_EXPR_ARGP (aggr_init_expr));
4561 TREE_NOTHROW (call_expr) = TREE_NOTHROW (aggr_init_expr);
4562 CALL_FROM_THUNK_P (call_expr) = AGGR_INIT_FROM_THUNK_P (aggr_init_expr);
4563 CALL_EXPR_OPERATOR_SYNTAX (call_expr)
4564 = CALL_EXPR_OPERATOR_SYNTAX (aggr_init_expr);
4565 CALL_EXPR_ORDERED_ARGS (call_expr) = CALL_EXPR_ORDERED_ARGS (aggr_init_expr);
4566 CALL_EXPR_REVERSE_ARGS (call_expr) = CALL_EXPR_REVERSE_ARGS (aggr_init_expr);
4568 if (style == ctor)
4570 /* Replace the first argument to the ctor with the address of the
4571 slot. */
4572 cxx_mark_addressable (slot);
4573 CALL_EXPR_ARG (call_expr, 0) =
4574 build1 (ADDR_EXPR, build_pointer_type (type), slot);
4576 else if (style == arg)
4578 /* Just mark it addressable here, and leave the rest to
4579 expand_call{,_inline}. */
4580 cxx_mark_addressable (slot);
4581 CALL_EXPR_RETURN_SLOT_OPT (call_expr) = true;
4582 call_expr = build2 (INIT_EXPR, TREE_TYPE (call_expr), slot, call_expr);
4584 else if (style == pcc)
4586 /* If we're using the non-reentrant PCC calling convention, then we
4587 need to copy the returned value out of the static buffer into the
4588 SLOT. */
4589 push_deferring_access_checks (dk_no_check);
4590 call_expr = build_aggr_init (slot, call_expr,
4591 DIRECT_BIND | LOOKUP_ONLYCONVERTING,
4592 tf_warning_or_error);
4593 pop_deferring_access_checks ();
4594 call_expr = build2 (COMPOUND_EXPR, TREE_TYPE (slot), call_expr, slot);
4597 if (AGGR_INIT_ZERO_FIRST (aggr_init_expr))
4599 tree init = build_zero_init (type, NULL_TREE,
4600 /*static_storage_p=*/false);
4601 init = build2 (INIT_EXPR, void_type_node, slot, init);
4602 call_expr = build2 (COMPOUND_EXPR, TREE_TYPE (call_expr),
4603 init, call_expr);
4606 *tp = call_expr;
4609 /* Emit all thunks to FN that should be emitted when FN is emitted. */
4611 void
4612 emit_associated_thunks (tree fn)
4614 /* When we use vcall offsets, we emit thunks with the virtual
4615 functions to which they thunk. The whole point of vcall offsets
4616 is so that you can know statically the entire set of thunks that
4617 will ever be needed for a given virtual function, thereby
4618 enabling you to output all the thunks with the function itself. */
4619 if (DECL_VIRTUAL_P (fn)
4620 /* Do not emit thunks for extern template instantiations. */
4621 && ! DECL_REALLY_EXTERN (fn))
4623 tree thunk;
4625 for (thunk = DECL_THUNKS (fn); thunk; thunk = DECL_CHAIN (thunk))
4627 if (!THUNK_ALIAS (thunk))
4629 use_thunk (thunk, /*emit_p=*/1);
4630 if (DECL_RESULT_THUNK_P (thunk))
4632 tree probe;
4634 for (probe = DECL_THUNKS (thunk);
4635 probe; probe = DECL_CHAIN (probe))
4636 use_thunk (probe, /*emit_p=*/1);
4639 else
4640 gcc_assert (!DECL_THUNKS (thunk));
4645 /* Generate RTL for FN. */
4647 bool
4648 expand_or_defer_fn_1 (tree fn)
4650 /* When the parser calls us after finishing the body of a template
4651 function, we don't really want to expand the body. */
4652 if (processing_template_decl)
4654 /* Normally, collection only occurs in rest_of_compilation. So,
4655 if we don't collect here, we never collect junk generated
4656 during the processing of templates until we hit a
4657 non-template function. It's not safe to do this inside a
4658 nested class, though, as the parser may have local state that
4659 is not a GC root. */
4660 if (!function_depth)
4661 ggc_collect ();
4662 return false;
4665 gcc_assert (DECL_SAVED_TREE (fn));
4667 /* We make a decision about linkage for these functions at the end
4668 of the compilation. Until that point, we do not want the back
4669 end to output them -- but we do want it to see the bodies of
4670 these functions so that it can inline them as appropriate. */
4671 if (DECL_DECLARED_INLINE_P (fn) || DECL_IMPLICIT_INSTANTIATION (fn))
4673 if (DECL_INTERFACE_KNOWN (fn))
4674 /* We've already made a decision as to how this function will
4675 be handled. */;
4676 else if (!at_eof
4677 || DECL_IMMEDIATE_FUNCTION_P (fn)
4678 || DECL_OMP_DECLARE_REDUCTION_P (fn))
4679 tentative_decl_linkage (fn);
4680 else
4681 import_export_decl (fn);
4683 /* If the user wants us to keep all inline functions, then mark
4684 this function as needed so that finish_file will make sure to
4685 output it later. Similarly, all dllexport'd functions must
4686 be emitted; there may be callers in other DLLs. */
4687 if (DECL_DECLARED_INLINE_P (fn)
4688 && !DECL_REALLY_EXTERN (fn)
4689 && !DECL_IMMEDIATE_FUNCTION_P (fn)
4690 && !DECL_OMP_DECLARE_REDUCTION_P (fn)
4691 && (flag_keep_inline_functions
4692 || (flag_keep_inline_dllexport
4693 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (fn)))))
4695 mark_needed (fn);
4696 DECL_EXTERNAL (fn) = 0;
4700 /* If this is a constructor or destructor body, we have to clone
4701 it. */
4702 if (maybe_clone_body (fn))
4704 /* We don't want to process FN again, so pretend we've written
4705 it out, even though we haven't. */
4706 TREE_ASM_WRITTEN (fn) = 1;
4707 /* If this is a constexpr function, keep DECL_SAVED_TREE. */
4708 if (!DECL_DECLARED_CONSTEXPR_P (fn)
4709 && !(modules_p () && DECL_DECLARED_INLINE_P (fn)))
4710 DECL_SAVED_TREE (fn) = NULL_TREE;
4711 return false;
4714 /* There's no reason to do any of the work here if we're only doing
4715 semantic analysis; this code just generates RTL. */
4716 if (flag_syntax_only)
4718 /* Pretend that this function has been written out so that we don't try
4719 to expand it again. */
4720 TREE_ASM_WRITTEN (fn) = 1;
4721 return false;
4724 if (DECL_OMP_DECLARE_REDUCTION_P (fn))
4725 return false;
4727 return true;
4730 void
4731 expand_or_defer_fn (tree fn)
4733 if (expand_or_defer_fn_1 (fn))
4735 function_depth++;
4737 /* Expand or defer, at the whim of the compilation unit manager. */
4738 cgraph_node::finalize_function (fn, function_depth > 1);
4739 emit_associated_thunks (fn);
4741 function_depth--;
4745 class nrv_data
4747 public:
4748 nrv_data () : visited (37) {}
4750 tree var;
4751 tree result;
4752 hash_table<nofree_ptr_hash <tree_node> > visited;
4755 /* Helper function for walk_tree, used by finalize_nrv below. */
4757 static tree
4758 finalize_nrv_r (tree* tp, int* walk_subtrees, void* data)
4760 class nrv_data *dp = (class nrv_data *)data;
4761 tree_node **slot;
4763 /* No need to walk into types. There wouldn't be any need to walk into
4764 non-statements, except that we have to consider STMT_EXPRs. */
4765 if (TYPE_P (*tp))
4766 *walk_subtrees = 0;
4767 /* Change all returns to just refer to the RESULT_DECL; this is a nop,
4768 but differs from using NULL_TREE in that it indicates that we care
4769 about the value of the RESULT_DECL. */
4770 else if (TREE_CODE (*tp) == RETURN_EXPR)
4771 TREE_OPERAND (*tp, 0) = dp->result;
4772 /* Change all cleanups for the NRV to only run when an exception is
4773 thrown. */
4774 else if (TREE_CODE (*tp) == CLEANUP_STMT
4775 && CLEANUP_DECL (*tp) == dp->var)
4776 CLEANUP_EH_ONLY (*tp) = 1;
4777 /* Replace the DECL_EXPR for the NRV with an initialization of the
4778 RESULT_DECL, if needed. */
4779 else if (TREE_CODE (*tp) == DECL_EXPR
4780 && DECL_EXPR_DECL (*tp) == dp->var)
4782 tree init;
4783 if (DECL_INITIAL (dp->var)
4784 && DECL_INITIAL (dp->var) != error_mark_node)
4785 init = build2 (INIT_EXPR, void_type_node, dp->result,
4786 DECL_INITIAL (dp->var));
4787 else
4788 init = build_empty_stmt (EXPR_LOCATION (*tp));
4789 DECL_INITIAL (dp->var) = NULL_TREE;
4790 SET_EXPR_LOCATION (init, EXPR_LOCATION (*tp));
4791 *tp = init;
4793 /* And replace all uses of the NRV with the RESULT_DECL. */
4794 else if (*tp == dp->var)
4795 *tp = dp->result;
4797 /* Avoid walking into the same tree more than once. Unfortunately, we
4798 can't just use walk_tree_without duplicates because it would only call
4799 us for the first occurrence of dp->var in the function body. */
4800 slot = dp->visited.find_slot (*tp, INSERT);
4801 if (*slot)
4802 *walk_subtrees = 0;
4803 else
4804 *slot = *tp;
4806 /* Keep iterating. */
4807 return NULL_TREE;
4810 /* Called from finish_function to implement the named return value
4811 optimization by overriding all the RETURN_EXPRs and pertinent
4812 CLEANUP_STMTs and replacing all occurrences of VAR with RESULT, the
4813 RESULT_DECL for the function. */
4815 void
4816 finalize_nrv (tree *tp, tree var, tree result)
4818 class nrv_data data;
4820 /* Copy name from VAR to RESULT. */
4821 DECL_NAME (result) = DECL_NAME (var);
4822 /* Don't forget that we take its address. */
4823 TREE_ADDRESSABLE (result) = TREE_ADDRESSABLE (var);
4824 /* Finally set DECL_VALUE_EXPR to avoid assigning
4825 a stack slot at -O0 for the original var and debug info
4826 uses RESULT location for VAR. */
4827 SET_DECL_VALUE_EXPR (var, result);
4828 DECL_HAS_VALUE_EXPR_P (var) = 1;
4830 data.var = var;
4831 data.result = result;
4832 cp_walk_tree (tp, finalize_nrv_r, &data, 0);
4835 /* Create CP_OMP_CLAUSE_INFO for clause C. Returns true if it is invalid. */
4837 bool
4838 cxx_omp_create_clause_info (tree c, tree type, bool need_default_ctor,
4839 bool need_copy_ctor, bool need_copy_assignment,
4840 bool need_dtor)
4842 int save_errorcount = errorcount;
4843 tree info, t;
4845 /* Always allocate 3 elements for simplicity. These are the
4846 function decls for the ctor, dtor, and assignment op.
4847 This layout is known to the three lang hooks,
4848 cxx_omp_clause_default_init, cxx_omp_clause_copy_init,
4849 and cxx_omp_clause_assign_op. */
4850 info = make_tree_vec (3);
4851 CP_OMP_CLAUSE_INFO (c) = info;
4853 if (need_default_ctor || need_copy_ctor)
4855 if (need_default_ctor)
4856 t = get_default_ctor (type);
4857 else
4858 t = get_copy_ctor (type, tf_warning_or_error);
4860 if (t && !trivial_fn_p (t))
4861 TREE_VEC_ELT (info, 0) = t;
4864 if (need_dtor && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
4865 TREE_VEC_ELT (info, 1) = get_dtor (type, tf_warning_or_error);
4867 if (need_copy_assignment)
4869 t = get_copy_assign (type);
4871 if (t && !trivial_fn_p (t))
4872 TREE_VEC_ELT (info, 2) = t;
4875 return errorcount != save_errorcount;
4878 /* If DECL is DECL_OMP_PRIVATIZED_MEMBER, return corresponding
4879 FIELD_DECL, otherwise return DECL itself. */
4881 static tree
4882 omp_clause_decl_field (tree decl)
4884 if (VAR_P (decl)
4885 && DECL_HAS_VALUE_EXPR_P (decl)
4886 && DECL_ARTIFICIAL (decl)
4887 && DECL_LANG_SPECIFIC (decl)
4888 && DECL_OMP_PRIVATIZED_MEMBER (decl))
4890 tree f = DECL_VALUE_EXPR (decl);
4891 if (INDIRECT_REF_P (f))
4892 f = TREE_OPERAND (f, 0);
4893 if (TREE_CODE (f) == COMPONENT_REF)
4895 f = TREE_OPERAND (f, 1);
4896 gcc_assert (TREE_CODE (f) == FIELD_DECL);
4897 return f;
4900 return NULL_TREE;
4903 /* Adjust DECL if needed for printing using %qE. */
4905 static tree
4906 omp_clause_printable_decl (tree decl)
4908 tree t = omp_clause_decl_field (decl);
4909 if (t)
4910 return t;
4911 return decl;
4914 /* For a FIELD_DECL F and corresponding DECL_OMP_PRIVATIZED_MEMBER
4915 VAR_DECL T that doesn't need a DECL_EXPR added, record it for
4916 privatization. */
4918 static void
4919 omp_note_field_privatization (tree f, tree t)
4921 if (!omp_private_member_map)
4922 omp_private_member_map = new hash_map<tree, tree>;
4923 tree &v = omp_private_member_map->get_or_insert (f);
4924 if (v == NULL_TREE)
4926 v = t;
4927 omp_private_member_vec.safe_push (f);
4928 /* Signal that we don't want to create DECL_EXPR for this dummy var. */
4929 omp_private_member_vec.safe_push (integer_zero_node);
4933 /* Privatize FIELD_DECL T, return corresponding DECL_OMP_PRIVATIZED_MEMBER
4934 dummy VAR_DECL. */
4936 tree
4937 omp_privatize_field (tree t, bool shared)
4939 tree m = finish_non_static_data_member (t, NULL_TREE, NULL_TREE);
4940 if (m == error_mark_node)
4941 return error_mark_node;
4942 if (!omp_private_member_map && !shared)
4943 omp_private_member_map = new hash_map<tree, tree>;
4944 if (TYPE_REF_P (TREE_TYPE (t)))
4946 gcc_assert (INDIRECT_REF_P (m));
4947 m = TREE_OPERAND (m, 0);
4949 tree vb = NULL_TREE;
4950 tree &v = shared ? vb : omp_private_member_map->get_or_insert (t);
4951 if (v == NULL_TREE)
4953 v = create_temporary_var (TREE_TYPE (m));
4954 retrofit_lang_decl (v);
4955 DECL_OMP_PRIVATIZED_MEMBER (v) = 1;
4956 SET_DECL_VALUE_EXPR (v, m);
4957 DECL_HAS_VALUE_EXPR_P (v) = 1;
4958 if (!shared)
4959 omp_private_member_vec.safe_push (t);
4961 return v;
4964 /* Helper function for handle_omp_array_sections. Called recursively
4965 to handle multiple array-section-subscripts. C is the clause,
4966 T current expression (initially OMP_CLAUSE_DECL), which is either
4967 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
4968 expression if specified, TREE_VALUE length expression if specified,
4969 TREE_CHAIN is what it has been specified after, or some decl.
4970 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
4971 set to true if any of the array-section-subscript could have length
4972 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
4973 first array-section-subscript which is known not to have length
4974 of one. Given say:
4975 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
4976 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
4977 all are or may have length of 1, array-section-subscript [:2] is the
4978 first one known not to have length 1. For array-section-subscript
4979 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
4980 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
4981 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
4982 case though, as some lengths could be zero. */
4984 static tree
4985 handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
4986 bool &maybe_zero_len, unsigned int &first_non_one,
4987 enum c_omp_region_type ort)
4989 tree ret, low_bound, length, type;
4990 if (TREE_CODE (t) != TREE_LIST)
4992 if (error_operand_p (t))
4993 return error_mark_node;
4994 if (REFERENCE_REF_P (t)
4995 && TREE_CODE (TREE_OPERAND (t, 0)) == COMPONENT_REF)
4996 t = TREE_OPERAND (t, 0);
4997 ret = t;
4998 if (TREE_CODE (t) == COMPONENT_REF
4999 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
5000 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO
5001 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FROM)
5002 && !type_dependent_expression_p (t))
5004 if (TREE_CODE (TREE_OPERAND (t, 1)) == FIELD_DECL
5005 && DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
5007 error_at (OMP_CLAUSE_LOCATION (c),
5008 "bit-field %qE in %qs clause",
5009 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5010 return error_mark_node;
5012 while (TREE_CODE (t) == COMPONENT_REF)
5014 if (TREE_TYPE (TREE_OPERAND (t, 0))
5015 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == UNION_TYPE)
5017 error_at (OMP_CLAUSE_LOCATION (c),
5018 "%qE is a member of a union", t);
5019 return error_mark_node;
5021 t = TREE_OPERAND (t, 0);
5022 if (ort == C_ORT_ACC && TREE_CODE (t) == INDIRECT_REF)
5023 t = TREE_OPERAND (t, 0);
5025 if (REFERENCE_REF_P (t))
5026 t = TREE_OPERAND (t, 0);
5028 if (TREE_CODE (t) == FIELD_DECL
5029 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY
5030 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND))
5031 ret = finish_non_static_data_member (t, NULL_TREE, NULL_TREE);
5032 else if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
5034 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
5035 return NULL_TREE;
5036 if (DECL_P (t))
5037 error_at (OMP_CLAUSE_LOCATION (c),
5038 "%qD is not a variable in %qs clause", t,
5039 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5040 else
5041 error_at (OMP_CLAUSE_LOCATION (c),
5042 "%qE is not a variable in %qs clause", t,
5043 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5044 return error_mark_node;
5046 else if ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP
5047 && TREE_CODE (t) == PARM_DECL
5048 && DECL_ARTIFICIAL (t)
5049 && DECL_NAME (t) == this_identifier
5050 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
5051 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
5053 error_at (OMP_CLAUSE_LOCATION (c),
5054 "%<this%> allowed in OpenMP only in %<declare simd%>"
5055 " clauses");
5056 return error_mark_node;
5058 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
5059 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
5060 && VAR_P (t) && CP_DECL_THREAD_LOCAL_P (t))
5062 error_at (OMP_CLAUSE_LOCATION (c),
5063 "%qD is threadprivate variable in %qs clause", t,
5064 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5065 return error_mark_node;
5067 if (type_dependent_expression_p (ret))
5068 return NULL_TREE;
5069 ret = convert_from_reference (ret);
5070 return ret;
5073 if ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP
5074 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
5075 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
5076 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
5077 && TREE_CODE (TREE_CHAIN (t)) == FIELD_DECL)
5078 TREE_CHAIN (t) = omp_privatize_field (TREE_CHAIN (t), false);
5079 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
5080 maybe_zero_len, first_non_one, ort);
5081 if (ret == error_mark_node || ret == NULL_TREE)
5082 return ret;
5084 type = TREE_TYPE (ret);
5085 low_bound = TREE_PURPOSE (t);
5086 length = TREE_VALUE (t);
5087 if ((low_bound && type_dependent_expression_p (low_bound))
5088 || (length && type_dependent_expression_p (length)))
5089 return NULL_TREE;
5091 if (low_bound == error_mark_node || length == error_mark_node)
5092 return error_mark_node;
5094 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
5096 error_at (OMP_CLAUSE_LOCATION (c),
5097 "low bound %qE of array section does not have integral type",
5098 low_bound);
5099 return error_mark_node;
5101 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
5103 error_at (OMP_CLAUSE_LOCATION (c),
5104 "length %qE of array section does not have integral type",
5105 length);
5106 return error_mark_node;
5108 if (low_bound)
5109 low_bound = mark_rvalue_use (low_bound);
5110 if (length)
5111 length = mark_rvalue_use (length);
5112 /* We need to reduce to real constant-values for checks below. */
5113 if (length)
5114 length = fold_simple (length);
5115 if (low_bound)
5116 low_bound = fold_simple (low_bound);
5117 if (low_bound
5118 && TREE_CODE (low_bound) == INTEGER_CST
5119 && TYPE_PRECISION (TREE_TYPE (low_bound))
5120 > TYPE_PRECISION (sizetype))
5121 low_bound = fold_convert (sizetype, low_bound);
5122 if (length
5123 && TREE_CODE (length) == INTEGER_CST
5124 && TYPE_PRECISION (TREE_TYPE (length))
5125 > TYPE_PRECISION (sizetype))
5126 length = fold_convert (sizetype, length);
5127 if (low_bound == NULL_TREE)
5128 low_bound = integer_zero_node;
5130 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
5131 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
5132 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH))
5134 if (length != integer_one_node)
5136 error_at (OMP_CLAUSE_LOCATION (c),
5137 "expected single pointer in %qs clause",
5138 c_omp_map_clause_name (c, ort == C_ORT_ACC));
5139 return error_mark_node;
5142 if (length != NULL_TREE)
5144 if (!integer_nonzerop (length))
5146 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY
5147 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
5148 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
5149 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
5150 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
5152 if (integer_zerop (length))
5154 error_at (OMP_CLAUSE_LOCATION (c),
5155 "zero length array section in %qs clause",
5156 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5157 return error_mark_node;
5160 else
5161 maybe_zero_len = true;
5163 if (first_non_one == types.length ()
5164 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
5165 first_non_one++;
5167 if (TREE_CODE (type) == ARRAY_TYPE)
5169 if (length == NULL_TREE
5170 && (TYPE_DOMAIN (type) == NULL_TREE
5171 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
5173 error_at (OMP_CLAUSE_LOCATION (c),
5174 "for unknown bound array type length expression must "
5175 "be specified");
5176 return error_mark_node;
5178 if (TREE_CODE (low_bound) == INTEGER_CST
5179 && tree_int_cst_sgn (low_bound) == -1)
5181 error_at (OMP_CLAUSE_LOCATION (c),
5182 "negative low bound in array section in %qs clause",
5183 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5184 return error_mark_node;
5186 if (length != NULL_TREE
5187 && TREE_CODE (length) == INTEGER_CST
5188 && tree_int_cst_sgn (length) == -1)
5190 error_at (OMP_CLAUSE_LOCATION (c),
5191 "negative length in array section in %qs clause",
5192 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5193 return error_mark_node;
5195 if (TYPE_DOMAIN (type)
5196 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
5197 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
5198 == INTEGER_CST)
5200 tree size
5201 = fold_convert (sizetype, TYPE_MAX_VALUE (TYPE_DOMAIN (type)));
5202 size = size_binop (PLUS_EXPR, size, size_one_node);
5203 if (TREE_CODE (low_bound) == INTEGER_CST)
5205 if (tree_int_cst_lt (size, low_bound))
5207 error_at (OMP_CLAUSE_LOCATION (c),
5208 "low bound %qE above array section size "
5209 "in %qs clause", low_bound,
5210 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5211 return error_mark_node;
5213 if (tree_int_cst_equal (size, low_bound))
5215 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY
5216 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
5217 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
5218 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
5219 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
5221 error_at (OMP_CLAUSE_LOCATION (c),
5222 "zero length array section in %qs clause",
5223 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5224 return error_mark_node;
5226 maybe_zero_len = true;
5228 else if (length == NULL_TREE
5229 && first_non_one == types.length ()
5230 && tree_int_cst_equal
5231 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
5232 low_bound))
5233 first_non_one++;
5235 else if (length == NULL_TREE)
5237 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
5238 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
5239 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
5240 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_IN_REDUCTION
5241 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_TASK_REDUCTION)
5242 maybe_zero_len = true;
5243 if (first_non_one == types.length ())
5244 first_non_one++;
5246 if (length && TREE_CODE (length) == INTEGER_CST)
5248 if (tree_int_cst_lt (size, length))
5250 error_at (OMP_CLAUSE_LOCATION (c),
5251 "length %qE above array section size "
5252 "in %qs clause", length,
5253 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5254 return error_mark_node;
5256 if (TREE_CODE (low_bound) == INTEGER_CST)
5258 tree lbpluslen
5259 = size_binop (PLUS_EXPR,
5260 fold_convert (sizetype, low_bound),
5261 fold_convert (sizetype, length));
5262 if (TREE_CODE (lbpluslen) == INTEGER_CST
5263 && tree_int_cst_lt (size, lbpluslen))
5265 error_at (OMP_CLAUSE_LOCATION (c),
5266 "high bound %qE above array section size "
5267 "in %qs clause", lbpluslen,
5268 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5269 return error_mark_node;
5274 else if (length == NULL_TREE)
5276 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
5277 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
5278 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
5279 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_IN_REDUCTION
5280 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_TASK_REDUCTION)
5281 maybe_zero_len = true;
5282 if (first_non_one == types.length ())
5283 first_non_one++;
5286 /* For [lb:] we will need to evaluate lb more than once. */
5287 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
5289 tree lb = cp_save_expr (low_bound);
5290 if (lb != low_bound)
5292 TREE_PURPOSE (t) = lb;
5293 low_bound = lb;
5297 else if (TYPE_PTR_P (type))
5299 if (length == NULL_TREE)
5301 if (TREE_CODE (ret) == PARM_DECL && DECL_ARRAY_PARAMETER_P (ret))
5302 error_at (OMP_CLAUSE_LOCATION (c),
5303 "for array function parameter length expression "
5304 "must be specified");
5305 else
5306 error_at (OMP_CLAUSE_LOCATION (c),
5307 "for pointer type length expression must be specified");
5308 return error_mark_node;
5310 if (length != NULL_TREE
5311 && TREE_CODE (length) == INTEGER_CST
5312 && tree_int_cst_sgn (length) == -1)
5314 error_at (OMP_CLAUSE_LOCATION (c),
5315 "negative length in array section in %qs clause",
5316 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5317 return error_mark_node;
5319 /* If there is a pointer type anywhere but in the very first
5320 array-section-subscript, the array section can't be contiguous. */
5321 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
5322 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
5323 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
5325 error_at (OMP_CLAUSE_LOCATION (c),
5326 "array section is not contiguous in %qs clause",
5327 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5328 return error_mark_node;
5331 else
5333 error_at (OMP_CLAUSE_LOCATION (c),
5334 "%qE does not have pointer or array type", ret);
5335 return error_mark_node;
5337 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
5338 types.safe_push (TREE_TYPE (ret));
5339 /* We will need to evaluate lb more than once. */
5340 tree lb = cp_save_expr (low_bound);
5341 if (lb != low_bound)
5343 TREE_PURPOSE (t) = lb;
5344 low_bound = lb;
5346 /* Temporarily disable -fstrong-eval-order for array reductions.
5347 The SAVE_EXPR and COMPOUND_EXPR added if low_bound has side-effects
5348 is something the middle-end can't cope with and more importantly,
5349 it needs to be the actual base variable that is privatized, not some
5350 temporary assigned previous value of it. That, together with OpenMP
5351 saying how many times the side-effects are evaluated is unspecified,
5352 makes int *a, *b; ... reduction(+:a[a = b, 3:10]) really unspecified. */
5353 warning_sentinel s (flag_strong_eval_order,
5354 OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
5355 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
5356 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION);
5357 ret = grok_array_decl (OMP_CLAUSE_LOCATION (c), ret, low_bound, false);
5358 return ret;
5361 /* Handle array sections for clause C. */
5363 static bool
5364 handle_omp_array_sections (tree c, enum c_omp_region_type ort)
5366 bool maybe_zero_len = false;
5367 unsigned int first_non_one = 0;
5368 auto_vec<tree, 10> types;
5369 tree *tp = &OMP_CLAUSE_DECL (c);
5370 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
5371 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY)
5372 && TREE_CODE (*tp) == TREE_LIST
5373 && TREE_PURPOSE (*tp)
5374 && TREE_CODE (TREE_PURPOSE (*tp)) == TREE_VEC)
5375 tp = &TREE_VALUE (*tp);
5376 tree first = handle_omp_array_sections_1 (c, *tp, types,
5377 maybe_zero_len, first_non_one,
5378 ort);
5379 if (first == error_mark_node)
5380 return true;
5381 if (first == NULL_TREE)
5382 return false;
5383 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
5384 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY)
5386 tree t = *tp;
5387 tree tem = NULL_TREE;
5388 if (processing_template_decl)
5389 return false;
5390 /* Need to evaluate side effects in the length expressions
5391 if any. */
5392 while (TREE_CODE (t) == TREE_LIST)
5394 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
5396 if (tem == NULL_TREE)
5397 tem = TREE_VALUE (t);
5398 else
5399 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
5400 TREE_VALUE (t), tem);
5402 t = TREE_CHAIN (t);
5404 if (tem)
5405 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
5406 *tp = first;
5408 else
5410 unsigned int num = types.length (), i;
5411 tree t, side_effects = NULL_TREE, size = NULL_TREE;
5412 tree condition = NULL_TREE;
5414 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
5415 maybe_zero_len = true;
5416 if (processing_template_decl && maybe_zero_len)
5417 return false;
5419 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
5420 t = TREE_CHAIN (t))
5422 tree low_bound = TREE_PURPOSE (t);
5423 tree length = TREE_VALUE (t);
5425 i--;
5426 if (low_bound
5427 && TREE_CODE (low_bound) == INTEGER_CST
5428 && TYPE_PRECISION (TREE_TYPE (low_bound))
5429 > TYPE_PRECISION (sizetype))
5430 low_bound = fold_convert (sizetype, low_bound);
5431 if (length
5432 && TREE_CODE (length) == INTEGER_CST
5433 && TYPE_PRECISION (TREE_TYPE (length))
5434 > TYPE_PRECISION (sizetype))
5435 length = fold_convert (sizetype, length);
5436 if (low_bound == NULL_TREE)
5437 low_bound = integer_zero_node;
5438 if (!maybe_zero_len && i > first_non_one)
5440 if (integer_nonzerop (low_bound))
5441 goto do_warn_noncontiguous;
5442 if (length != NULL_TREE
5443 && TREE_CODE (length) == INTEGER_CST
5444 && TYPE_DOMAIN (types[i])
5445 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
5446 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
5447 == INTEGER_CST)
5449 tree size;
5450 size = size_binop (PLUS_EXPR,
5451 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
5452 size_one_node);
5453 if (!tree_int_cst_equal (length, size))
5455 do_warn_noncontiguous:
5456 error_at (OMP_CLAUSE_LOCATION (c),
5457 "array section is not contiguous in %qs "
5458 "clause",
5459 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5460 return true;
5463 if (!processing_template_decl
5464 && length != NULL_TREE
5465 && TREE_SIDE_EFFECTS (length))
5467 if (side_effects == NULL_TREE)
5468 side_effects = length;
5469 else
5470 side_effects = build2 (COMPOUND_EXPR,
5471 TREE_TYPE (side_effects),
5472 length, side_effects);
5475 else if (processing_template_decl)
5476 continue;
5477 else
5479 tree l;
5481 if (i > first_non_one
5482 && ((length && integer_nonzerop (length))
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 continue;
5487 if (length)
5488 l = fold_convert (sizetype, length);
5489 else
5491 l = size_binop (PLUS_EXPR,
5492 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
5493 size_one_node);
5494 l = size_binop (MINUS_EXPR, l,
5495 fold_convert (sizetype, low_bound));
5497 if (i > first_non_one)
5499 l = fold_build2 (NE_EXPR, boolean_type_node, l,
5500 size_zero_node);
5501 if (condition == NULL_TREE)
5502 condition = l;
5503 else
5504 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
5505 l, condition);
5507 else if (size == NULL_TREE)
5509 size = size_in_bytes (TREE_TYPE (types[i]));
5510 tree eltype = TREE_TYPE (types[num - 1]);
5511 while (TREE_CODE (eltype) == ARRAY_TYPE)
5512 eltype = TREE_TYPE (eltype);
5513 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
5514 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
5515 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
5516 size = size_binop (EXACT_DIV_EXPR, size,
5517 size_in_bytes (eltype));
5518 size = size_binop (MULT_EXPR, size, l);
5519 if (condition)
5520 size = fold_build3 (COND_EXPR, sizetype, condition,
5521 size, size_zero_node);
5523 else
5524 size = size_binop (MULT_EXPR, size, l);
5527 if (!processing_template_decl)
5529 if (side_effects)
5530 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
5531 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
5532 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
5533 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
5535 size = size_binop (MINUS_EXPR, size, size_one_node);
5536 size = save_expr (size);
5537 tree index_type = build_index_type (size);
5538 tree eltype = TREE_TYPE (first);
5539 while (TREE_CODE (eltype) == ARRAY_TYPE)
5540 eltype = TREE_TYPE (eltype);
5541 tree type = build_array_type (eltype, index_type);
5542 tree ptype = build_pointer_type (eltype);
5543 if (TYPE_REF_P (TREE_TYPE (t))
5544 && INDIRECT_TYPE_P (TREE_TYPE (TREE_TYPE (t))))
5545 t = convert_from_reference (t);
5546 else if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
5547 t = build_fold_addr_expr (t);
5548 tree t2 = build_fold_addr_expr (first);
5549 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
5550 ptrdiff_type_node, t2);
5551 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
5552 ptrdiff_type_node, t2,
5553 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
5554 ptrdiff_type_node, t));
5555 if (tree_fits_shwi_p (t2))
5556 t = build2 (MEM_REF, type, t,
5557 build_int_cst (ptype, tree_to_shwi (t2)));
5558 else
5560 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
5561 sizetype, t2);
5562 t = build2_loc (OMP_CLAUSE_LOCATION (c), POINTER_PLUS_EXPR,
5563 TREE_TYPE (t), t, t2);
5564 t = build2 (MEM_REF, type, t, build_int_cst (ptype, 0));
5566 OMP_CLAUSE_DECL (c) = t;
5567 return false;
5569 OMP_CLAUSE_DECL (c) = first;
5570 OMP_CLAUSE_SIZE (c) = size;
5571 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
5572 || (TREE_CODE (t) == COMPONENT_REF
5573 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE))
5574 return false;
5575 switch (OMP_CLAUSE_MAP_KIND (c))
5577 case GOMP_MAP_ALLOC:
5578 case GOMP_MAP_IF_PRESENT:
5579 case GOMP_MAP_TO:
5580 case GOMP_MAP_FROM:
5581 case GOMP_MAP_TOFROM:
5582 case GOMP_MAP_ALWAYS_TO:
5583 case GOMP_MAP_ALWAYS_FROM:
5584 case GOMP_MAP_ALWAYS_TOFROM:
5585 case GOMP_MAP_RELEASE:
5586 case GOMP_MAP_DELETE:
5587 case GOMP_MAP_FORCE_TO:
5588 case GOMP_MAP_FORCE_FROM:
5589 case GOMP_MAP_FORCE_TOFROM:
5590 case GOMP_MAP_FORCE_PRESENT:
5591 OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (c) = 1;
5592 break;
5593 default:
5594 break;
5596 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
5597 OMP_CLAUSE_MAP);
5598 if (TREE_CODE (t) == COMPONENT_REF)
5599 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ATTACH_DETACH);
5600 else if (REFERENCE_REF_P (t)
5601 && TREE_CODE (TREE_OPERAND (t, 0)) == COMPONENT_REF)
5603 t = TREE_OPERAND (t, 0);
5604 gomp_map_kind k = (ort == C_ORT_ACC) ? GOMP_MAP_ATTACH_DETACH
5605 : GOMP_MAP_ALWAYS_POINTER;
5606 OMP_CLAUSE_SET_MAP_KIND (c2, k);
5608 else
5609 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_FIRSTPRIVATE_POINTER);
5610 OMP_CLAUSE_MAP_IMPLICIT (c2) = OMP_CLAUSE_MAP_IMPLICIT (c);
5611 if (OMP_CLAUSE_MAP_KIND (c2) != GOMP_MAP_FIRSTPRIVATE_POINTER
5612 && !cxx_mark_addressable (t))
5613 return false;
5614 OMP_CLAUSE_DECL (c2) = t;
5615 t = build_fold_addr_expr (first);
5616 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
5617 ptrdiff_type_node, t);
5618 tree ptr = OMP_CLAUSE_DECL (c2);
5619 ptr = convert_from_reference (ptr);
5620 if (!INDIRECT_TYPE_P (TREE_TYPE (ptr)))
5621 ptr = build_fold_addr_expr (ptr);
5622 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
5623 ptrdiff_type_node, t,
5624 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
5625 ptrdiff_type_node, ptr));
5626 OMP_CLAUSE_SIZE (c2) = t;
5627 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
5628 OMP_CLAUSE_CHAIN (c) = c2;
5629 ptr = OMP_CLAUSE_DECL (c2);
5630 if (OMP_CLAUSE_MAP_KIND (c2) != GOMP_MAP_FIRSTPRIVATE_POINTER
5631 && TYPE_REF_P (TREE_TYPE (ptr))
5632 && INDIRECT_TYPE_P (TREE_TYPE (TREE_TYPE (ptr))))
5634 tree c3 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
5635 OMP_CLAUSE_MAP);
5636 OMP_CLAUSE_SET_MAP_KIND (c3, OMP_CLAUSE_MAP_KIND (c2));
5637 OMP_CLAUSE_MAP_IMPLICIT (c2) = OMP_CLAUSE_MAP_IMPLICIT (c);
5638 OMP_CLAUSE_DECL (c3) = ptr;
5639 if (OMP_CLAUSE_MAP_KIND (c2) == GOMP_MAP_ALWAYS_POINTER
5640 || OMP_CLAUSE_MAP_KIND (c2) == GOMP_MAP_ATTACH_DETACH)
5642 OMP_CLAUSE_DECL (c2) = build_simple_mem_ref (ptr);
5643 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ALWAYS_POINTER);
5645 else
5646 OMP_CLAUSE_DECL (c2) = convert_from_reference (ptr);
5647 OMP_CLAUSE_SIZE (c3) = size_zero_node;
5648 OMP_CLAUSE_CHAIN (c3) = OMP_CLAUSE_CHAIN (c2);
5649 OMP_CLAUSE_CHAIN (c2) = c3;
5653 return false;
5656 /* Return identifier to look up for omp declare reduction. */
5658 tree
5659 omp_reduction_id (enum tree_code reduction_code, tree reduction_id, tree type)
5661 const char *p = NULL;
5662 const char *m = NULL;
5663 switch (reduction_code)
5665 case PLUS_EXPR:
5666 case MULT_EXPR:
5667 case MINUS_EXPR:
5668 case BIT_AND_EXPR:
5669 case BIT_XOR_EXPR:
5670 case BIT_IOR_EXPR:
5671 case TRUTH_ANDIF_EXPR:
5672 case TRUTH_ORIF_EXPR:
5673 reduction_id = ovl_op_identifier (false, reduction_code);
5674 break;
5675 case MIN_EXPR:
5676 p = "min";
5677 break;
5678 case MAX_EXPR:
5679 p = "max";
5680 break;
5681 default:
5682 break;
5685 if (p == NULL)
5687 if (TREE_CODE (reduction_id) != IDENTIFIER_NODE)
5688 return error_mark_node;
5689 p = IDENTIFIER_POINTER (reduction_id);
5692 if (type != NULL_TREE)
5693 m = mangle_type_string (TYPE_MAIN_VARIANT (type));
5695 const char prefix[] = "omp declare reduction ";
5696 size_t lenp = sizeof (prefix);
5697 if (strncmp (p, prefix, lenp - 1) == 0)
5698 lenp = 1;
5699 size_t len = strlen (p);
5700 size_t lenm = m ? strlen (m) + 1 : 0;
5701 char *name = XALLOCAVEC (char, lenp + len + lenm);
5702 if (lenp > 1)
5703 memcpy (name, prefix, lenp - 1);
5704 memcpy (name + lenp - 1, p, len + 1);
5705 if (m)
5707 name[lenp + len - 1] = '~';
5708 memcpy (name + lenp + len, m, lenm);
5710 return get_identifier (name);
5713 /* Lookup OpenMP UDR ID for TYPE, return the corresponding artificial
5714 FUNCTION_DECL or NULL_TREE if not found. */
5716 static tree
5717 omp_reduction_lookup (location_t loc, tree id, tree type, tree *baselinkp,
5718 vec<tree> *ambiguousp)
5720 tree orig_id = id;
5721 tree baselink = NULL_TREE;
5722 if (identifier_p (id))
5724 cp_id_kind idk;
5725 bool nonint_cst_expression_p;
5726 const char *error_msg;
5727 id = omp_reduction_id (ERROR_MARK, id, type);
5728 tree decl = lookup_name (id);
5729 if (decl == NULL_TREE)
5730 decl = error_mark_node;
5731 id = finish_id_expression (id, decl, NULL_TREE, &idk, false, true,
5732 &nonint_cst_expression_p, false, true, false,
5733 false, &error_msg, loc);
5734 if (idk == CP_ID_KIND_UNQUALIFIED
5735 && identifier_p (id))
5737 vec<tree, va_gc> *args = NULL;
5738 vec_safe_push (args, build_reference_type (type));
5739 id = perform_koenig_lookup (id, args, tf_none);
5742 else if (TREE_CODE (id) == SCOPE_REF)
5743 id = lookup_qualified_name (TREE_OPERAND (id, 0),
5744 omp_reduction_id (ERROR_MARK,
5745 TREE_OPERAND (id, 1),
5746 type),
5747 LOOK_want::NORMAL, false);
5748 tree fns = id;
5749 id = NULL_TREE;
5750 if (fns && is_overloaded_fn (fns))
5752 for (lkp_iterator iter (get_fns (fns)); iter; ++iter)
5754 tree fndecl = *iter;
5755 if (TREE_CODE (fndecl) == FUNCTION_DECL)
5757 tree argtype = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
5758 if (same_type_p (TREE_TYPE (argtype), type))
5760 id = fndecl;
5761 break;
5766 if (id && BASELINK_P (fns))
5768 if (baselinkp)
5769 *baselinkp = fns;
5770 else
5771 baselink = fns;
5775 if (!id && CLASS_TYPE_P (type) && TYPE_BINFO (type))
5777 auto_vec<tree> ambiguous;
5778 tree binfo = TYPE_BINFO (type), base_binfo, ret = NULL_TREE;
5779 unsigned int ix;
5780 if (ambiguousp == NULL)
5781 ambiguousp = &ambiguous;
5782 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
5784 id = omp_reduction_lookup (loc, orig_id, BINFO_TYPE (base_binfo),
5785 baselinkp ? baselinkp : &baselink,
5786 ambiguousp);
5787 if (id == NULL_TREE)
5788 continue;
5789 if (!ambiguousp->is_empty ())
5790 ambiguousp->safe_push (id);
5791 else if (ret != NULL_TREE)
5793 ambiguousp->safe_push (ret);
5794 ambiguousp->safe_push (id);
5795 ret = NULL_TREE;
5797 else
5798 ret = id;
5800 if (ambiguousp != &ambiguous)
5801 return ret;
5802 if (!ambiguous.is_empty ())
5804 const char *str = _("candidates are:");
5805 unsigned int idx;
5806 tree udr;
5807 error_at (loc, "user defined reduction lookup is ambiguous");
5808 FOR_EACH_VEC_ELT (ambiguous, idx, udr)
5810 inform (DECL_SOURCE_LOCATION (udr), "%s %#qD", str, udr);
5811 if (idx == 0)
5812 str = get_spaces (str);
5814 ret = error_mark_node;
5815 baselink = NULL_TREE;
5817 id = ret;
5819 if (id && baselink)
5820 perform_or_defer_access_check (BASELINK_BINFO (baselink),
5821 id, id, tf_warning_or_error);
5822 return id;
5825 /* Helper function for cp_parser_omp_declare_reduction_exprs
5826 and tsubst_omp_udr.
5827 Remove CLEANUP_STMT for data (omp_priv variable).
5828 Also append INIT_EXPR for DECL_INITIAL of omp_priv after its
5829 DECL_EXPR. */
5831 tree
5832 cp_remove_omp_priv_cleanup_stmt (tree *tp, int *walk_subtrees, void *data)
5834 if (TYPE_P (*tp))
5835 *walk_subtrees = 0;
5836 else if (TREE_CODE (*tp) == CLEANUP_STMT && CLEANUP_DECL (*tp) == (tree) data)
5837 *tp = CLEANUP_BODY (*tp);
5838 else if (TREE_CODE (*tp) == DECL_EXPR)
5840 tree decl = DECL_EXPR_DECL (*tp);
5841 if (!processing_template_decl
5842 && decl == (tree) data
5843 && DECL_INITIAL (decl)
5844 && DECL_INITIAL (decl) != error_mark_node)
5846 tree list = NULL_TREE;
5847 append_to_statement_list_force (*tp, &list);
5848 tree init_expr = build2 (INIT_EXPR, void_type_node,
5849 decl, DECL_INITIAL (decl));
5850 DECL_INITIAL (decl) = NULL_TREE;
5851 append_to_statement_list_force (init_expr, &list);
5852 *tp = list;
5855 return NULL_TREE;
5858 /* Data passed from cp_check_omp_declare_reduction to
5859 cp_check_omp_declare_reduction_r. */
5861 struct cp_check_omp_declare_reduction_data
5863 location_t loc;
5864 tree stmts[7];
5865 bool combiner_p;
5868 /* Helper function for cp_check_omp_declare_reduction, called via
5869 cp_walk_tree. */
5871 static tree
5872 cp_check_omp_declare_reduction_r (tree *tp, int *, void *data)
5874 struct cp_check_omp_declare_reduction_data *udr_data
5875 = (struct cp_check_omp_declare_reduction_data *) data;
5876 if (SSA_VAR_P (*tp)
5877 && !DECL_ARTIFICIAL (*tp)
5878 && *tp != DECL_EXPR_DECL (udr_data->stmts[udr_data->combiner_p ? 0 : 3])
5879 && *tp != DECL_EXPR_DECL (udr_data->stmts[udr_data->combiner_p ? 1 : 4]))
5881 location_t loc = udr_data->loc;
5882 if (udr_data->combiner_p)
5883 error_at (loc, "%<#pragma omp declare reduction%> combiner refers to "
5884 "variable %qD which is not %<omp_out%> nor %<omp_in%>",
5885 *tp);
5886 else
5887 error_at (loc, "%<#pragma omp declare reduction%> initializer refers "
5888 "to variable %qD which is not %<omp_priv%> nor "
5889 "%<omp_orig%>",
5890 *tp);
5891 return *tp;
5893 return NULL_TREE;
5896 /* Diagnose violation of OpenMP #pragma omp declare reduction restrictions. */
5898 bool
5899 cp_check_omp_declare_reduction (tree udr)
5901 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (udr)));
5902 gcc_assert (TYPE_REF_P (type));
5903 type = TREE_TYPE (type);
5904 int i;
5905 location_t loc = DECL_SOURCE_LOCATION (udr);
5907 if (type == error_mark_node)
5908 return false;
5909 if (ARITHMETIC_TYPE_P (type))
5911 static enum tree_code predef_codes[]
5912 = { PLUS_EXPR, MULT_EXPR, MINUS_EXPR, BIT_AND_EXPR, BIT_XOR_EXPR,
5913 BIT_IOR_EXPR, TRUTH_ANDIF_EXPR, TRUTH_ORIF_EXPR };
5914 for (i = 0; i < 8; i++)
5916 tree id = omp_reduction_id (predef_codes[i], NULL_TREE, NULL_TREE);
5917 const char *n1 = IDENTIFIER_POINTER (DECL_NAME (udr));
5918 const char *n2 = IDENTIFIER_POINTER (id);
5919 if (strncmp (n1, n2, IDENTIFIER_LENGTH (id)) == 0
5920 && (n1[IDENTIFIER_LENGTH (id)] == '~'
5921 || n1[IDENTIFIER_LENGTH (id)] == '\0'))
5922 break;
5925 if (i == 8
5926 && TREE_CODE (type) != COMPLEX_EXPR)
5928 const char prefix_minmax[] = "omp declare reduction m";
5929 size_t prefix_size = sizeof (prefix_minmax) - 1;
5930 const char *n = IDENTIFIER_POINTER (DECL_NAME (udr));
5931 if (strncmp (IDENTIFIER_POINTER (DECL_NAME (udr)),
5932 prefix_minmax, prefix_size) == 0
5933 && ((n[prefix_size] == 'i' && n[prefix_size + 1] == 'n')
5934 || (n[prefix_size] == 'a' && n[prefix_size + 1] == 'x'))
5935 && (n[prefix_size + 2] == '~' || n[prefix_size + 2] == '\0'))
5936 i = 0;
5938 if (i < 8)
5940 error_at (loc, "predeclared arithmetic type %qT in "
5941 "%<#pragma omp declare reduction%>", type);
5942 return false;
5945 else if (FUNC_OR_METHOD_TYPE_P (type)
5946 || TREE_CODE (type) == ARRAY_TYPE)
5948 error_at (loc, "function or array type %qT in "
5949 "%<#pragma omp declare reduction%>", type);
5950 return false;
5952 else if (TYPE_REF_P (type))
5954 error_at (loc, "reference type %qT in %<#pragma omp declare reduction%>",
5955 type);
5956 return false;
5958 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
5960 error_at (loc, "%<const%>, %<volatile%> or %<__restrict%>-qualified "
5961 "type %qT in %<#pragma omp declare reduction%>", type);
5962 return false;
5965 tree body = DECL_SAVED_TREE (udr);
5966 if (body == NULL_TREE || TREE_CODE (body) != STATEMENT_LIST)
5967 return true;
5969 tree_stmt_iterator tsi;
5970 struct cp_check_omp_declare_reduction_data data;
5971 memset (data.stmts, 0, sizeof data.stmts);
5972 for (i = 0, tsi = tsi_start (body);
5973 i < 7 && !tsi_end_p (tsi);
5974 i++, tsi_next (&tsi))
5975 data.stmts[i] = tsi_stmt (tsi);
5976 data.loc = loc;
5977 gcc_assert (tsi_end_p (tsi));
5978 if (i >= 3)
5980 gcc_assert (TREE_CODE (data.stmts[0]) == DECL_EXPR
5981 && TREE_CODE (data.stmts[1]) == DECL_EXPR);
5982 if (warning_suppressed_p (DECL_EXPR_DECL (data.stmts[0]) /* What warning? */))
5983 return true;
5984 data.combiner_p = true;
5985 if (cp_walk_tree (&data.stmts[2], cp_check_omp_declare_reduction_r,
5986 &data, NULL))
5987 suppress_warning (DECL_EXPR_DECL (data.stmts[0]) /* What warning? */);
5989 if (i >= 6)
5991 gcc_assert (TREE_CODE (data.stmts[3]) == DECL_EXPR
5992 && TREE_CODE (data.stmts[4]) == DECL_EXPR);
5993 data.combiner_p = false;
5994 if (cp_walk_tree (&data.stmts[5], cp_check_omp_declare_reduction_r,
5995 &data, NULL)
5996 || cp_walk_tree (&DECL_INITIAL (DECL_EXPR_DECL (data.stmts[3])),
5997 cp_check_omp_declare_reduction_r, &data, NULL))
5998 suppress_warning (DECL_EXPR_DECL (data.stmts[0]) /* Wat warning? */);
5999 if (i == 7)
6000 gcc_assert (TREE_CODE (data.stmts[6]) == DECL_EXPR);
6002 return true;
6005 /* Helper function of finish_omp_clauses. Clone STMT as if we were making
6006 an inline call. But, remap
6007 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
6008 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
6010 static tree
6011 clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
6012 tree decl, tree placeholder)
6014 copy_body_data id;
6015 hash_map<tree, tree> decl_map;
6017 decl_map.put (omp_decl1, placeholder);
6018 decl_map.put (omp_decl2, decl);
6019 memset (&id, 0, sizeof (id));
6020 id.src_fn = DECL_CONTEXT (omp_decl1);
6021 id.dst_fn = current_function_decl;
6022 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
6023 id.decl_map = &decl_map;
6025 id.copy_decl = copy_decl_no_change;
6026 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
6027 id.transform_new_cfg = true;
6028 id.transform_return_to_modify = false;
6029 id.transform_lang_insert_block = NULL;
6030 id.eh_lp_nr = 0;
6031 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
6032 return stmt;
6035 /* Helper function of finish_omp_clauses, called via cp_walk_tree.
6036 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
6038 static tree
6039 find_omp_placeholder_r (tree *tp, int *, void *data)
6041 if (*tp == (tree) data)
6042 return *tp;
6043 return NULL_TREE;
6046 /* Helper function of finish_omp_clauses. Handle OMP_CLAUSE_REDUCTION C.
6047 Return true if there is some error and the clause should be removed. */
6049 static bool
6050 finish_omp_reduction_clause (tree c, bool *need_default_ctor, bool *need_dtor)
6052 tree t = OMP_CLAUSE_DECL (c);
6053 bool predefined = false;
6054 if (TREE_CODE (t) == TREE_LIST)
6056 gcc_assert (processing_template_decl);
6057 return false;
6059 tree type = TREE_TYPE (t);
6060 if (TREE_CODE (t) == MEM_REF)
6061 type = TREE_TYPE (type);
6062 if (TYPE_REF_P (type))
6063 type = TREE_TYPE (type);
6064 if (TREE_CODE (type) == ARRAY_TYPE)
6066 tree oatype = type;
6067 gcc_assert (TREE_CODE (t) != MEM_REF);
6068 while (TREE_CODE (type) == ARRAY_TYPE)
6069 type = TREE_TYPE (type);
6070 if (!processing_template_decl)
6072 t = require_complete_type (t);
6073 if (t == error_mark_node
6074 || !complete_type_or_else (oatype, NULL_TREE))
6075 return true;
6076 tree size = size_binop (EXACT_DIV_EXPR, TYPE_SIZE_UNIT (oatype),
6077 TYPE_SIZE_UNIT (type));
6078 if (integer_zerop (size))
6080 error_at (OMP_CLAUSE_LOCATION (c),
6081 "%qE in %<reduction%> clause is a zero size array",
6082 omp_clause_printable_decl (t));
6083 return true;
6085 size = size_binop (MINUS_EXPR, size, size_one_node);
6086 size = save_expr (size);
6087 tree index_type = build_index_type (size);
6088 tree atype = build_array_type (type, index_type);
6089 tree ptype = build_pointer_type (type);
6090 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
6091 t = build_fold_addr_expr (t);
6092 t = build2 (MEM_REF, atype, t, build_int_cst (ptype, 0));
6093 OMP_CLAUSE_DECL (c) = t;
6096 if (type == error_mark_node)
6097 return true;
6098 else if (ARITHMETIC_TYPE_P (type))
6099 switch (OMP_CLAUSE_REDUCTION_CODE (c))
6101 case PLUS_EXPR:
6102 case MULT_EXPR:
6103 case MINUS_EXPR:
6104 case TRUTH_ANDIF_EXPR:
6105 case TRUTH_ORIF_EXPR:
6106 predefined = true;
6107 break;
6108 case MIN_EXPR:
6109 case MAX_EXPR:
6110 if (TREE_CODE (type) == COMPLEX_TYPE)
6111 break;
6112 predefined = true;
6113 break;
6114 case BIT_AND_EXPR:
6115 case BIT_IOR_EXPR:
6116 case BIT_XOR_EXPR:
6117 if (FLOAT_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
6118 break;
6119 predefined = true;
6120 break;
6121 default:
6122 break;
6124 else if (TYPE_READONLY (type))
6126 error_at (OMP_CLAUSE_LOCATION (c),
6127 "%qE has const type for %<reduction%>",
6128 omp_clause_printable_decl (t));
6129 return true;
6131 else if (!processing_template_decl)
6133 t = require_complete_type (t);
6134 if (t == error_mark_node)
6135 return true;
6136 OMP_CLAUSE_DECL (c) = t;
6139 if (predefined)
6141 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = NULL_TREE;
6142 return false;
6144 else if (processing_template_decl)
6146 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == error_mark_node)
6147 return true;
6148 return false;
6151 tree id = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
6153 type = TYPE_MAIN_VARIANT (type);
6154 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = NULL_TREE;
6155 if (id == NULL_TREE)
6156 id = omp_reduction_id (OMP_CLAUSE_REDUCTION_CODE (c),
6157 NULL_TREE, NULL_TREE);
6158 id = omp_reduction_lookup (OMP_CLAUSE_LOCATION (c), id, type, NULL, NULL);
6159 if (id)
6161 if (id == error_mark_node)
6162 return true;
6163 mark_used (id);
6164 tree body = DECL_SAVED_TREE (id);
6165 if (!body)
6166 return true;
6167 if (TREE_CODE (body) == STATEMENT_LIST)
6169 tree_stmt_iterator tsi;
6170 tree placeholder = NULL_TREE, decl_placeholder = NULL_TREE;
6171 int i;
6172 tree stmts[7];
6173 tree atype = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (id)));
6174 atype = TREE_TYPE (atype);
6175 bool need_static_cast = !same_type_p (type, atype);
6176 memset (stmts, 0, sizeof stmts);
6177 for (i = 0, tsi = tsi_start (body);
6178 i < 7 && !tsi_end_p (tsi);
6179 i++, tsi_next (&tsi))
6180 stmts[i] = tsi_stmt (tsi);
6181 gcc_assert (tsi_end_p (tsi));
6183 if (i >= 3)
6185 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
6186 && TREE_CODE (stmts[1]) == DECL_EXPR);
6187 placeholder = build_lang_decl (VAR_DECL, NULL_TREE, type);
6188 DECL_ARTIFICIAL (placeholder) = 1;
6189 DECL_IGNORED_P (placeholder) = 1;
6190 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
6191 if (TREE_CODE (t) == MEM_REF)
6193 decl_placeholder = build_lang_decl (VAR_DECL, NULL_TREE,
6194 type);
6195 DECL_ARTIFICIAL (decl_placeholder) = 1;
6196 DECL_IGNORED_P (decl_placeholder) = 1;
6197 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c) = decl_placeholder;
6199 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[0])))
6200 cxx_mark_addressable (placeholder);
6201 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[1]))
6202 && (decl_placeholder
6203 || !TYPE_REF_P (TREE_TYPE (OMP_CLAUSE_DECL (c)))))
6204 cxx_mark_addressable (decl_placeholder ? decl_placeholder
6205 : OMP_CLAUSE_DECL (c));
6206 tree omp_out = placeholder;
6207 tree omp_in = decl_placeholder ? decl_placeholder
6208 : convert_from_reference (OMP_CLAUSE_DECL (c));
6209 if (need_static_cast)
6211 tree rtype = build_reference_type (atype);
6212 omp_out = build_static_cast (input_location,
6213 rtype, omp_out,
6214 tf_warning_or_error);
6215 omp_in = build_static_cast (input_location,
6216 rtype, omp_in,
6217 tf_warning_or_error);
6218 if (omp_out == error_mark_node || omp_in == error_mark_node)
6219 return true;
6220 omp_out = convert_from_reference (omp_out);
6221 omp_in = convert_from_reference (omp_in);
6223 OMP_CLAUSE_REDUCTION_MERGE (c)
6224 = clone_omp_udr (stmts[2], DECL_EXPR_DECL (stmts[0]),
6225 DECL_EXPR_DECL (stmts[1]), omp_in, omp_out);
6227 if (i >= 6)
6229 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
6230 && TREE_CODE (stmts[4]) == DECL_EXPR);
6231 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[3]))
6232 && (decl_placeholder
6233 || !TYPE_REF_P (TREE_TYPE (OMP_CLAUSE_DECL (c)))))
6234 cxx_mark_addressable (decl_placeholder ? decl_placeholder
6235 : OMP_CLAUSE_DECL (c));
6236 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[4])))
6237 cxx_mark_addressable (placeholder);
6238 tree omp_priv = decl_placeholder ? decl_placeholder
6239 : convert_from_reference (OMP_CLAUSE_DECL (c));
6240 tree omp_orig = placeholder;
6241 if (need_static_cast)
6243 if (i == 7)
6245 error_at (OMP_CLAUSE_LOCATION (c),
6246 "user defined reduction with constructor "
6247 "initializer for base class %qT", atype);
6248 return true;
6250 tree rtype = build_reference_type (atype);
6251 omp_priv = build_static_cast (input_location,
6252 rtype, omp_priv,
6253 tf_warning_or_error);
6254 omp_orig = build_static_cast (input_location,
6255 rtype, omp_orig,
6256 tf_warning_or_error);
6257 if (omp_priv == error_mark_node
6258 || omp_orig == error_mark_node)
6259 return true;
6260 omp_priv = convert_from_reference (omp_priv);
6261 omp_orig = convert_from_reference (omp_orig);
6263 if (i == 6)
6264 *need_default_ctor = true;
6265 OMP_CLAUSE_REDUCTION_INIT (c)
6266 = clone_omp_udr (stmts[5], DECL_EXPR_DECL (stmts[4]),
6267 DECL_EXPR_DECL (stmts[3]),
6268 omp_priv, omp_orig);
6269 if (cp_walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
6270 find_omp_placeholder_r, placeholder, NULL))
6271 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
6273 else if (i >= 3)
6275 if (CLASS_TYPE_P (type) && !pod_type_p (type))
6276 *need_default_ctor = true;
6277 else
6279 tree init;
6280 tree v = decl_placeholder ? decl_placeholder
6281 : convert_from_reference (t);
6282 if (AGGREGATE_TYPE_P (TREE_TYPE (v)))
6283 init = build_constructor (TREE_TYPE (v), NULL);
6284 else
6285 init = fold_convert (TREE_TYPE (v), integer_zero_node);
6286 OMP_CLAUSE_REDUCTION_INIT (c)
6287 = build2 (INIT_EXPR, TREE_TYPE (v), v, init);
6292 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
6293 *need_dtor = true;
6294 else
6296 error_at (OMP_CLAUSE_LOCATION (c),
6297 "user defined reduction not found for %qE",
6298 omp_clause_printable_decl (t));
6299 return true;
6301 if (TREE_CODE (OMP_CLAUSE_DECL (c)) == MEM_REF)
6302 gcc_assert (TYPE_SIZE_UNIT (type)
6303 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST);
6304 return false;
6307 /* Called from finish_struct_1. linear(this) or linear(this:step)
6308 clauses might not be finalized yet because the class has been incomplete
6309 when parsing #pragma omp declare simd methods. Fix those up now. */
6311 void
6312 finish_omp_declare_simd_methods (tree t)
6314 if (processing_template_decl)
6315 return;
6317 for (tree x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
6319 if (TREE_CODE (x) == USING_DECL
6320 || !DECL_NONSTATIC_MEMBER_FUNCTION_P (x))
6321 continue;
6322 tree ods = lookup_attribute ("omp declare simd", DECL_ATTRIBUTES (x));
6323 if (!ods || !TREE_VALUE (ods))
6324 continue;
6325 for (tree c = TREE_VALUE (TREE_VALUE (ods)); c; c = OMP_CLAUSE_CHAIN (c))
6326 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
6327 && integer_zerop (OMP_CLAUSE_DECL (c))
6328 && OMP_CLAUSE_LINEAR_STEP (c)
6329 && TYPE_PTR_P (TREE_TYPE (OMP_CLAUSE_LINEAR_STEP (c))))
6331 tree s = OMP_CLAUSE_LINEAR_STEP (c);
6332 s = fold_convert_loc (OMP_CLAUSE_LOCATION (c), sizetype, s);
6333 s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MULT_EXPR,
6334 sizetype, s, TYPE_SIZE_UNIT (t));
6335 OMP_CLAUSE_LINEAR_STEP (c) = s;
6340 /* Adjust sink depend clause to take into account pointer offsets.
6342 Return TRUE if there was a problem processing the offset, and the
6343 whole clause should be removed. */
6345 static bool
6346 cp_finish_omp_clause_depend_sink (tree sink_clause)
6348 tree t = OMP_CLAUSE_DECL (sink_clause);
6349 gcc_assert (TREE_CODE (t) == TREE_LIST);
6351 /* Make sure we don't adjust things twice for templates. */
6352 if (processing_template_decl)
6353 return false;
6355 for (; t; t = TREE_CHAIN (t))
6357 tree decl = TREE_VALUE (t);
6358 if (TYPE_PTR_P (TREE_TYPE (decl)))
6360 tree offset = TREE_PURPOSE (t);
6361 bool neg = wi::neg_p (wi::to_wide (offset));
6362 offset = fold_unary (ABS_EXPR, TREE_TYPE (offset), offset);
6363 decl = mark_rvalue_use (decl);
6364 decl = convert_from_reference (decl);
6365 tree t2 = pointer_int_sum (OMP_CLAUSE_LOCATION (sink_clause),
6366 neg ? MINUS_EXPR : PLUS_EXPR,
6367 decl, offset);
6368 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (sink_clause),
6369 MINUS_EXPR, sizetype,
6370 fold_convert (sizetype, t2),
6371 fold_convert (sizetype, decl));
6372 if (t2 == error_mark_node)
6373 return true;
6374 TREE_PURPOSE (t) = t2;
6377 return false;
6380 /* Finish OpenMP iterators ITER. Return true if they are errorneous
6381 and clauses containing them should be removed. */
6383 static bool
6384 cp_omp_finish_iterators (tree iter)
6386 bool ret = false;
6387 for (tree it = iter; it; it = TREE_CHAIN (it))
6389 tree var = TREE_VEC_ELT (it, 0);
6390 tree begin = TREE_VEC_ELT (it, 1);
6391 tree end = TREE_VEC_ELT (it, 2);
6392 tree step = TREE_VEC_ELT (it, 3);
6393 tree orig_step;
6394 tree type = TREE_TYPE (var);
6395 location_t loc = DECL_SOURCE_LOCATION (var);
6396 if (type == error_mark_node)
6398 ret = true;
6399 continue;
6401 if (type_dependent_expression_p (var))
6402 continue;
6403 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
6405 error_at (loc, "iterator %qD has neither integral nor pointer type",
6406 var);
6407 ret = true;
6408 continue;
6410 else if (TYPE_READONLY (type))
6412 error_at (loc, "iterator %qD has const qualified type", var);
6413 ret = true;
6414 continue;
6416 if (type_dependent_expression_p (begin)
6417 || type_dependent_expression_p (end)
6418 || type_dependent_expression_p (step))
6419 continue;
6420 else if (error_operand_p (step))
6422 ret = true;
6423 continue;
6425 else if (!INTEGRAL_TYPE_P (TREE_TYPE (step)))
6427 error_at (EXPR_LOC_OR_LOC (step, loc),
6428 "iterator step with non-integral type");
6429 ret = true;
6430 continue;
6433 begin = mark_rvalue_use (begin);
6434 end = mark_rvalue_use (end);
6435 step = mark_rvalue_use (step);
6436 begin = cp_build_c_cast (input_location, type, begin,
6437 tf_warning_or_error);
6438 end = cp_build_c_cast (input_location, type, end,
6439 tf_warning_or_error);
6440 orig_step = step;
6441 if (!processing_template_decl)
6442 step = orig_step = save_expr (step);
6443 tree stype = POINTER_TYPE_P (type) ? sizetype : type;
6444 step = cp_build_c_cast (input_location, stype, step,
6445 tf_warning_or_error);
6446 if (POINTER_TYPE_P (type) && !processing_template_decl)
6448 begin = save_expr (begin);
6449 step = pointer_int_sum (loc, PLUS_EXPR, begin, step);
6450 step = fold_build2_loc (loc, MINUS_EXPR, sizetype,
6451 fold_convert (sizetype, step),
6452 fold_convert (sizetype, begin));
6453 step = fold_convert (ssizetype, step);
6455 if (!processing_template_decl)
6457 begin = maybe_constant_value (begin);
6458 end = maybe_constant_value (end);
6459 step = maybe_constant_value (step);
6460 orig_step = maybe_constant_value (orig_step);
6462 if (integer_zerop (step))
6464 error_at (loc, "iterator %qD has zero step", var);
6465 ret = true;
6466 continue;
6469 if (begin == error_mark_node
6470 || end == error_mark_node
6471 || step == error_mark_node
6472 || orig_step == error_mark_node)
6474 ret = true;
6475 continue;
6478 if (!processing_template_decl)
6480 begin = fold_build_cleanup_point_expr (TREE_TYPE (begin), begin);
6481 end = fold_build_cleanup_point_expr (TREE_TYPE (end), end);
6482 step = fold_build_cleanup_point_expr (TREE_TYPE (step), step);
6483 orig_step = fold_build_cleanup_point_expr (TREE_TYPE (orig_step),
6484 orig_step);
6486 hash_set<tree> pset;
6487 tree it2;
6488 for (it2 = TREE_CHAIN (it); it2; it2 = TREE_CHAIN (it2))
6490 tree var2 = TREE_VEC_ELT (it2, 0);
6491 tree begin2 = TREE_VEC_ELT (it2, 1);
6492 tree end2 = TREE_VEC_ELT (it2, 2);
6493 tree step2 = TREE_VEC_ELT (it2, 3);
6494 location_t loc2 = DECL_SOURCE_LOCATION (var2);
6495 if (cp_walk_tree (&begin2, find_omp_placeholder_r, var, &pset))
6497 error_at (EXPR_LOC_OR_LOC (begin2, loc2),
6498 "begin expression refers to outer iterator %qD", var);
6499 break;
6501 else if (cp_walk_tree (&end2, find_omp_placeholder_r, var, &pset))
6503 error_at (EXPR_LOC_OR_LOC (end2, loc2),
6504 "end expression refers to outer iterator %qD", var);
6505 break;
6507 else if (cp_walk_tree (&step2, find_omp_placeholder_r, var, &pset))
6509 error_at (EXPR_LOC_OR_LOC (step2, loc2),
6510 "step expression refers to outer iterator %qD", var);
6511 break;
6514 if (it2)
6516 ret = true;
6517 continue;
6519 TREE_VEC_ELT (it, 1) = begin;
6520 TREE_VEC_ELT (it, 2) = end;
6521 if (processing_template_decl)
6522 TREE_VEC_ELT (it, 3) = orig_step;
6523 else
6525 TREE_VEC_ELT (it, 3) = step;
6526 TREE_VEC_ELT (it, 4) = orig_step;
6529 return ret;
6532 /* Ensure that pointers are used in OpenACC attach and detach clauses.
6533 Return true if an error has been detected. */
6535 static bool
6536 cp_oacc_check_attachments (tree c)
6538 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
6539 return false;
6541 /* OpenACC attach / detach clauses must be pointers. */
6542 if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
6543 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH)
6545 tree t = OMP_CLAUSE_DECL (c);
6546 tree type;
6548 while (TREE_CODE (t) == TREE_LIST)
6549 t = TREE_CHAIN (t);
6551 type = TREE_TYPE (t);
6553 if (TREE_CODE (type) == REFERENCE_TYPE)
6554 type = TREE_TYPE (type);
6556 if (TREE_CODE (type) != POINTER_TYPE)
6558 error_at (OMP_CLAUSE_LOCATION (c), "expected pointer in %qs clause",
6559 c_omp_map_clause_name (c, true));
6560 return true;
6564 return false;
6567 /* For all elements of CLAUSES, validate them vs OpenMP constraints.
6568 Remove any elements from the list that are invalid. */
6570 tree
6571 finish_omp_clauses (tree clauses, enum c_omp_region_type ort)
6573 bitmap_head generic_head, firstprivate_head, lastprivate_head;
6574 bitmap_head aligned_head, map_head, map_field_head, map_firstprivate_head;
6575 bitmap_head oacc_reduction_head;
6576 tree c, t, *pc;
6577 tree safelen = NULL_TREE;
6578 bool branch_seen = false;
6579 bool copyprivate_seen = false;
6580 bool ordered_seen = false;
6581 bool order_seen = false;
6582 bool schedule_seen = false;
6583 bool oacc_async = false;
6584 tree last_iterators = NULL_TREE;
6585 bool last_iterators_remove = false;
6586 /* 1 if normal/task reduction has been seen, -1 if inscan reduction
6587 has been seen, -2 if mixed inscan/normal reduction diagnosed. */
6588 int reduction_seen = 0;
6589 bool allocate_seen = false;
6590 tree detach_seen = NULL_TREE;
6591 bool mergeable_seen = false;
6592 bool implicit_moved = false;
6593 bool target_in_reduction_seen = false;
6595 bitmap_obstack_initialize (NULL);
6596 bitmap_initialize (&generic_head, &bitmap_default_obstack);
6597 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
6598 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
6599 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
6600 /* If ort == C_ORT_OMP_DECLARE_SIMD used as uniform_head instead. */
6601 bitmap_initialize (&map_head, &bitmap_default_obstack);
6602 bitmap_initialize (&map_field_head, &bitmap_default_obstack);
6603 bitmap_initialize (&map_firstprivate_head, &bitmap_default_obstack);
6604 /* If ort == C_ORT_OMP used as nontemporal_head or use_device_xxx_head
6605 instead and for ort == C_ORT_OMP_TARGET used as in_reduction_head. */
6606 bitmap_initialize (&oacc_reduction_head, &bitmap_default_obstack);
6608 if (ort & C_ORT_ACC)
6609 for (c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
6610 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ASYNC)
6612 oacc_async = true;
6613 break;
6616 for (pc = &clauses, c = clauses; c ; c = *pc)
6618 bool remove = false;
6619 bool field_ok = false;
6621 switch (OMP_CLAUSE_CODE (c))
6623 case OMP_CLAUSE_SHARED:
6624 field_ok = ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP);
6625 goto check_dup_generic;
6626 case OMP_CLAUSE_PRIVATE:
6627 field_ok = ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP);
6628 goto check_dup_generic;
6629 case OMP_CLAUSE_REDUCTION:
6630 if (reduction_seen == 0)
6631 reduction_seen = OMP_CLAUSE_REDUCTION_INSCAN (c) ? -1 : 1;
6632 else if (reduction_seen != -2
6633 && reduction_seen != (OMP_CLAUSE_REDUCTION_INSCAN (c)
6634 ? -1 : 1))
6636 error_at (OMP_CLAUSE_LOCATION (c),
6637 "%<inscan%> and non-%<inscan%> %<reduction%> clauses "
6638 "on the same construct");
6639 reduction_seen = -2;
6641 /* FALLTHRU */
6642 case OMP_CLAUSE_IN_REDUCTION:
6643 case OMP_CLAUSE_TASK_REDUCTION:
6644 field_ok = ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP);
6645 t = OMP_CLAUSE_DECL (c);
6646 if (TREE_CODE (t) == TREE_LIST)
6648 if (handle_omp_array_sections (c, ort))
6650 remove = true;
6651 break;
6653 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
6654 && OMP_CLAUSE_REDUCTION_INSCAN (c))
6656 error_at (OMP_CLAUSE_LOCATION (c),
6657 "%<inscan%> %<reduction%> clause with array "
6658 "section");
6659 remove = true;
6660 break;
6662 if (TREE_CODE (t) == TREE_LIST)
6664 while (TREE_CODE (t) == TREE_LIST)
6665 t = TREE_CHAIN (t);
6667 else
6669 gcc_assert (TREE_CODE (t) == MEM_REF);
6670 t = TREE_OPERAND (t, 0);
6671 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
6672 t = TREE_OPERAND (t, 0);
6673 if (TREE_CODE (t) == ADDR_EXPR
6674 || INDIRECT_REF_P (t))
6675 t = TREE_OPERAND (t, 0);
6677 tree n = omp_clause_decl_field (t);
6678 if (n)
6679 t = n;
6680 goto check_dup_generic_t;
6682 if (oacc_async)
6683 cxx_mark_addressable (t);
6684 goto check_dup_generic;
6685 case OMP_CLAUSE_COPYPRIVATE:
6686 copyprivate_seen = true;
6687 field_ok = ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP);
6688 goto check_dup_generic;
6689 case OMP_CLAUSE_COPYIN:
6690 goto check_dup_generic;
6691 case OMP_CLAUSE_LINEAR:
6692 field_ok = ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP);
6693 t = OMP_CLAUSE_DECL (c);
6694 if (ort != C_ORT_OMP_DECLARE_SIMD
6695 && OMP_CLAUSE_LINEAR_KIND (c) != OMP_CLAUSE_LINEAR_DEFAULT)
6697 error_at (OMP_CLAUSE_LOCATION (c),
6698 "modifier should not be specified in %<linear%> "
6699 "clause on %<simd%> or %<for%> constructs");
6700 OMP_CLAUSE_LINEAR_KIND (c) = OMP_CLAUSE_LINEAR_DEFAULT;
6702 if ((VAR_P (t) || TREE_CODE (t) == PARM_DECL)
6703 && !type_dependent_expression_p (t))
6705 tree type = TREE_TYPE (t);
6706 if ((OMP_CLAUSE_LINEAR_KIND (c) == OMP_CLAUSE_LINEAR_REF
6707 || OMP_CLAUSE_LINEAR_KIND (c) == OMP_CLAUSE_LINEAR_UVAL)
6708 && !TYPE_REF_P (type))
6710 error_at (OMP_CLAUSE_LOCATION (c),
6711 "linear clause with %qs modifier applied to "
6712 "non-reference variable with %qT type",
6713 OMP_CLAUSE_LINEAR_KIND (c) == OMP_CLAUSE_LINEAR_REF
6714 ? "ref" : "uval", TREE_TYPE (t));
6715 remove = true;
6716 break;
6718 if (TYPE_REF_P (type))
6719 type = TREE_TYPE (type);
6720 if (OMP_CLAUSE_LINEAR_KIND (c) != OMP_CLAUSE_LINEAR_REF)
6722 if (!INTEGRAL_TYPE_P (type)
6723 && !TYPE_PTR_P (type))
6725 error_at (OMP_CLAUSE_LOCATION (c),
6726 "linear clause applied to non-integral "
6727 "non-pointer variable with %qT type",
6728 TREE_TYPE (t));
6729 remove = true;
6730 break;
6734 t = OMP_CLAUSE_LINEAR_STEP (c);
6735 if (t == NULL_TREE)
6736 t = integer_one_node;
6737 if (t == error_mark_node)
6739 remove = true;
6740 break;
6742 else if (!type_dependent_expression_p (t)
6743 && !INTEGRAL_TYPE_P (TREE_TYPE (t))
6744 && (ort != C_ORT_OMP_DECLARE_SIMD
6745 || TREE_CODE (t) != PARM_DECL
6746 || !TYPE_REF_P (TREE_TYPE (t))
6747 || !INTEGRAL_TYPE_P (TREE_TYPE (TREE_TYPE (t)))))
6749 error_at (OMP_CLAUSE_LOCATION (c),
6750 "linear step expression must be integral");
6751 remove = true;
6752 break;
6754 else
6756 t = mark_rvalue_use (t);
6757 if (ort == C_ORT_OMP_DECLARE_SIMD && TREE_CODE (t) == PARM_DECL)
6759 OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c) = 1;
6760 goto check_dup_generic;
6762 if (!processing_template_decl
6763 && (VAR_P (OMP_CLAUSE_DECL (c))
6764 || TREE_CODE (OMP_CLAUSE_DECL (c)) == PARM_DECL))
6766 if (ort == C_ORT_OMP_DECLARE_SIMD)
6768 t = maybe_constant_value (t);
6769 if (TREE_CODE (t) != INTEGER_CST)
6771 error_at (OMP_CLAUSE_LOCATION (c),
6772 "%<linear%> clause step %qE is neither "
6773 "constant nor a parameter", t);
6774 remove = true;
6775 break;
6778 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6779 tree type = TREE_TYPE (OMP_CLAUSE_DECL (c));
6780 if (TYPE_REF_P (type))
6781 type = TREE_TYPE (type);
6782 if (OMP_CLAUSE_LINEAR_KIND (c) == OMP_CLAUSE_LINEAR_REF)
6784 type = build_pointer_type (type);
6785 tree d = fold_convert (type, OMP_CLAUSE_DECL (c));
6786 t = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
6787 d, t);
6788 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c),
6789 MINUS_EXPR, sizetype,
6790 fold_convert (sizetype, t),
6791 fold_convert (sizetype, d));
6792 if (t == error_mark_node)
6794 remove = true;
6795 break;
6798 else if (TYPE_PTR_P (type)
6799 /* Can't multiply the step yet if *this
6800 is still incomplete type. */
6801 && (ort != C_ORT_OMP_DECLARE_SIMD
6802 || TREE_CODE (OMP_CLAUSE_DECL (c)) != PARM_DECL
6803 || !DECL_ARTIFICIAL (OMP_CLAUSE_DECL (c))
6804 || DECL_NAME (OMP_CLAUSE_DECL (c))
6805 != this_identifier
6806 || !TYPE_BEING_DEFINED (TREE_TYPE (type))))
6808 tree d = convert_from_reference (OMP_CLAUSE_DECL (c));
6809 t = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
6810 d, t);
6811 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c),
6812 MINUS_EXPR, sizetype,
6813 fold_convert (sizetype, t),
6814 fold_convert (sizetype, d));
6815 if (t == error_mark_node)
6817 remove = true;
6818 break;
6821 else
6822 t = fold_convert (type, t);
6824 OMP_CLAUSE_LINEAR_STEP (c) = t;
6826 goto check_dup_generic;
6827 check_dup_generic:
6828 t = omp_clause_decl_field (OMP_CLAUSE_DECL (c));
6829 if (t)
6831 if (!remove && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_SHARED)
6832 omp_note_field_privatization (t, OMP_CLAUSE_DECL (c));
6834 else
6835 t = OMP_CLAUSE_DECL (c);
6836 check_dup_generic_t:
6837 if (t == current_class_ptr
6838 && ((ort != C_ORT_OMP_DECLARE_SIMD && ort != C_ORT_ACC)
6839 || (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_LINEAR
6840 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_UNIFORM)))
6842 error_at (OMP_CLAUSE_LOCATION (c),
6843 "%<this%> allowed in OpenMP only in %<declare simd%>"
6844 " clauses");
6845 remove = true;
6846 break;
6848 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL
6849 && (!field_ok || TREE_CODE (t) != FIELD_DECL))
6851 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
6852 break;
6853 if (DECL_P (t))
6854 error_at (OMP_CLAUSE_LOCATION (c),
6855 "%qD is not a variable in clause %qs", t,
6856 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6857 else
6858 error_at (OMP_CLAUSE_LOCATION (c),
6859 "%qE is not a variable in clause %qs", t,
6860 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6861 remove = true;
6863 else if ((ort == C_ORT_ACC
6864 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
6865 || (ort == C_ORT_OMP
6866 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_USE_DEVICE_PTR
6867 || (OMP_CLAUSE_CODE (c)
6868 == OMP_CLAUSE_USE_DEVICE_ADDR)))
6869 || (ort == C_ORT_OMP_TARGET
6870 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION))
6872 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
6873 && (bitmap_bit_p (&generic_head, DECL_UID (t))
6874 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))))
6876 error_at (OMP_CLAUSE_LOCATION (c),
6877 "%qD appears more than once in data-sharing "
6878 "clauses", t);
6879 remove = true;
6880 break;
6882 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION)
6883 target_in_reduction_seen = true;
6884 if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
6886 error_at (OMP_CLAUSE_LOCATION (c),
6887 ort == C_ORT_ACC
6888 ? "%qD appears more than once in reduction clauses"
6889 : "%qD appears more than once in data clauses",
6891 remove = true;
6893 else
6894 bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
6896 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
6897 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
6898 || bitmap_bit_p (&lastprivate_head, DECL_UID (t))
6899 || bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
6901 error_at (OMP_CLAUSE_LOCATION (c),
6902 "%qD appears more than once in data clauses", t);
6903 remove = true;
6905 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
6906 && bitmap_bit_p (&map_head, DECL_UID (t)))
6908 if (ort == C_ORT_ACC)
6909 error_at (OMP_CLAUSE_LOCATION (c),
6910 "%qD appears more than once in data clauses", t);
6911 else
6912 error_at (OMP_CLAUSE_LOCATION (c),
6913 "%qD appears both in data and map clauses", t);
6914 remove = true;
6916 else
6917 bitmap_set_bit (&generic_head, DECL_UID (t));
6918 if (!field_ok)
6919 break;
6920 handle_field_decl:
6921 if (!remove
6922 && TREE_CODE (t) == FIELD_DECL
6923 && t == OMP_CLAUSE_DECL (c))
6925 OMP_CLAUSE_DECL (c)
6926 = omp_privatize_field (t, (OMP_CLAUSE_CODE (c)
6927 == OMP_CLAUSE_SHARED));
6928 if (OMP_CLAUSE_DECL (c) == error_mark_node)
6929 remove = true;
6931 break;
6933 case OMP_CLAUSE_FIRSTPRIVATE:
6934 if (OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c) && !implicit_moved)
6936 move_implicit:
6937 implicit_moved = true;
6938 /* Move firstprivate and map clauses with
6939 OMP_CLAUSE_{FIRSTPRIVATE,MAP}_IMPLICIT set to the end of
6940 clauses chain. */
6941 tree cl1 = NULL_TREE, cl2 = NULL_TREE;
6942 tree *pc1 = pc, *pc2 = &cl1, *pc3 = &cl2;
6943 while (*pc1)
6944 if (OMP_CLAUSE_CODE (*pc1) == OMP_CLAUSE_FIRSTPRIVATE
6945 && OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (*pc1))
6947 *pc3 = *pc1;
6948 pc3 = &OMP_CLAUSE_CHAIN (*pc3);
6949 *pc1 = OMP_CLAUSE_CHAIN (*pc1);
6951 else if (OMP_CLAUSE_CODE (*pc1) == OMP_CLAUSE_MAP
6952 && OMP_CLAUSE_MAP_IMPLICIT (*pc1))
6954 *pc2 = *pc1;
6955 pc2 = &OMP_CLAUSE_CHAIN (*pc2);
6956 *pc1 = OMP_CLAUSE_CHAIN (*pc1);
6958 else
6959 pc1 = &OMP_CLAUSE_CHAIN (*pc1);
6960 *pc3 = NULL;
6961 *pc2 = cl2;
6962 *pc1 = cl1;
6963 continue;
6965 t = omp_clause_decl_field (OMP_CLAUSE_DECL (c));
6966 if (t)
6967 omp_note_field_privatization (t, OMP_CLAUSE_DECL (c));
6968 else
6969 t = OMP_CLAUSE_DECL (c);
6970 if (ort != C_ORT_ACC && t == current_class_ptr)
6972 error_at (OMP_CLAUSE_LOCATION (c),
6973 "%<this%> allowed in OpenMP only in %<declare simd%>"
6974 " clauses");
6975 remove = true;
6976 break;
6978 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL
6979 && ((ort & C_ORT_OMP_DECLARE_SIMD) != C_ORT_OMP
6980 || TREE_CODE (t) != FIELD_DECL))
6982 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
6983 break;
6984 if (DECL_P (t))
6985 error_at (OMP_CLAUSE_LOCATION (c),
6986 "%qD is not a variable in clause %<firstprivate%>",
6988 else
6989 error_at (OMP_CLAUSE_LOCATION (c),
6990 "%qE is not a variable in clause %<firstprivate%>",
6992 remove = true;
6994 else if (OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c)
6995 && !OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT_TARGET (c)
6996 && bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
6997 remove = true;
6998 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
6999 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
7000 || bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
7002 error_at (OMP_CLAUSE_LOCATION (c),
7003 "%qD appears more than once in data clauses", t);
7004 remove = true;
7006 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
7008 if (ort == C_ORT_ACC)
7009 error_at (OMP_CLAUSE_LOCATION (c),
7010 "%qD appears more than once in data clauses", t);
7011 else if (OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c)
7012 && !OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT_TARGET (c))
7013 /* Silently drop the clause. */;
7014 else
7015 error_at (OMP_CLAUSE_LOCATION (c),
7016 "%qD appears both in data and map clauses", t);
7017 remove = true;
7019 else
7020 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
7021 goto handle_field_decl;
7023 case OMP_CLAUSE_LASTPRIVATE:
7024 t = omp_clause_decl_field (OMP_CLAUSE_DECL (c));
7025 if (t)
7026 omp_note_field_privatization (t, OMP_CLAUSE_DECL (c));
7027 else
7028 t = OMP_CLAUSE_DECL (c);
7029 if (ort != C_ORT_ACC && t == current_class_ptr)
7031 error_at (OMP_CLAUSE_LOCATION (c),
7032 "%<this%> allowed in OpenMP only in %<declare simd%>"
7033 " clauses");
7034 remove = true;
7035 break;
7037 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL
7038 && ((ort & C_ORT_OMP_DECLARE_SIMD) != C_ORT_OMP
7039 || TREE_CODE (t) != FIELD_DECL))
7041 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
7042 break;
7043 if (DECL_P (t))
7044 error_at (OMP_CLAUSE_LOCATION (c),
7045 "%qD is not a variable in clause %<lastprivate%>",
7047 else
7048 error_at (OMP_CLAUSE_LOCATION (c),
7049 "%qE is not a variable in clause %<lastprivate%>",
7051 remove = true;
7053 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
7054 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
7056 error_at (OMP_CLAUSE_LOCATION (c),
7057 "%qD appears more than once in data clauses", t);
7058 remove = true;
7060 else
7061 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
7062 goto handle_field_decl;
7064 case OMP_CLAUSE_IF:
7065 t = OMP_CLAUSE_IF_EXPR (c);
7066 t = maybe_convert_cond (t);
7067 if (t == error_mark_node)
7068 remove = true;
7069 else if (!processing_template_decl)
7070 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7071 OMP_CLAUSE_IF_EXPR (c) = t;
7072 break;
7074 case OMP_CLAUSE_FINAL:
7075 t = OMP_CLAUSE_FINAL_EXPR (c);
7076 t = maybe_convert_cond (t);
7077 if (t == error_mark_node)
7078 remove = true;
7079 else if (!processing_template_decl)
7080 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7081 OMP_CLAUSE_FINAL_EXPR (c) = t;
7082 break;
7084 case OMP_CLAUSE_GANG:
7085 /* Operand 1 is the gang static: argument. */
7086 t = OMP_CLAUSE_OPERAND (c, 1);
7087 if (t != NULL_TREE)
7089 if (t == error_mark_node)
7090 remove = true;
7091 else if (!type_dependent_expression_p (t)
7092 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
7094 error_at (OMP_CLAUSE_LOCATION (c),
7095 "%<gang%> static expression must be integral");
7096 remove = true;
7098 else
7100 t = mark_rvalue_use (t);
7101 if (!processing_template_decl)
7103 t = maybe_constant_value (t);
7104 if (TREE_CODE (t) == INTEGER_CST
7105 && tree_int_cst_sgn (t) != 1
7106 && t != integer_minus_one_node)
7108 warning_at (OMP_CLAUSE_LOCATION (c), 0,
7109 "%<gang%> static value must be "
7110 "positive");
7111 t = integer_one_node;
7113 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7116 OMP_CLAUSE_OPERAND (c, 1) = t;
7118 /* Check operand 0, the num argument. */
7119 /* FALLTHRU */
7121 case OMP_CLAUSE_WORKER:
7122 case OMP_CLAUSE_VECTOR:
7123 if (OMP_CLAUSE_OPERAND (c, 0) == NULL_TREE)
7124 break;
7125 /* FALLTHRU */
7127 case OMP_CLAUSE_NUM_TASKS:
7128 case OMP_CLAUSE_NUM_TEAMS:
7129 case OMP_CLAUSE_NUM_THREADS:
7130 case OMP_CLAUSE_NUM_GANGS:
7131 case OMP_CLAUSE_NUM_WORKERS:
7132 case OMP_CLAUSE_VECTOR_LENGTH:
7133 t = OMP_CLAUSE_OPERAND (c, 0);
7134 if (t == error_mark_node)
7135 remove = true;
7136 else if (!type_dependent_expression_p (t)
7137 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
7139 switch (OMP_CLAUSE_CODE (c))
7141 case OMP_CLAUSE_GANG:
7142 error_at (OMP_CLAUSE_LOCATION (c),
7143 "%<gang%> num expression must be integral"); break;
7144 case OMP_CLAUSE_VECTOR:
7145 error_at (OMP_CLAUSE_LOCATION (c),
7146 "%<vector%> length expression must be integral");
7147 break;
7148 case OMP_CLAUSE_WORKER:
7149 error_at (OMP_CLAUSE_LOCATION (c),
7150 "%<worker%> num expression must be integral");
7151 break;
7152 default:
7153 error_at (OMP_CLAUSE_LOCATION (c),
7154 "%qs expression must be integral",
7155 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7157 remove = true;
7159 else
7161 t = mark_rvalue_use (t);
7162 if (!processing_template_decl)
7164 t = maybe_constant_value (t);
7165 if (TREE_CODE (t) == INTEGER_CST
7166 && tree_int_cst_sgn (t) != 1)
7168 switch (OMP_CLAUSE_CODE (c))
7170 case OMP_CLAUSE_GANG:
7171 warning_at (OMP_CLAUSE_LOCATION (c), 0,
7172 "%<gang%> num value must be positive");
7173 break;
7174 case OMP_CLAUSE_VECTOR:
7175 warning_at (OMP_CLAUSE_LOCATION (c), 0,
7176 "%<vector%> length value must be "
7177 "positive");
7178 break;
7179 case OMP_CLAUSE_WORKER:
7180 warning_at (OMP_CLAUSE_LOCATION (c), 0,
7181 "%<worker%> num value must be "
7182 "positive");
7183 break;
7184 default:
7185 warning_at (OMP_CLAUSE_LOCATION (c), 0,
7186 "%qs value must be positive",
7187 omp_clause_code_name
7188 [OMP_CLAUSE_CODE (c)]);
7190 t = integer_one_node;
7192 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7194 OMP_CLAUSE_OPERAND (c, 0) = t;
7196 break;
7198 case OMP_CLAUSE_SCHEDULE:
7199 t = OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c);
7200 if (t == NULL)
7202 else if (t == error_mark_node)
7203 remove = true;
7204 else if (!type_dependent_expression_p (t)
7205 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
7207 error_at (OMP_CLAUSE_LOCATION (c),
7208 "schedule chunk size expression must be integral");
7209 remove = true;
7211 else
7213 t = mark_rvalue_use (t);
7214 if (!processing_template_decl)
7216 t = maybe_constant_value (t);
7217 if (TREE_CODE (t) == INTEGER_CST
7218 && tree_int_cst_sgn (t) != 1)
7220 warning_at (OMP_CLAUSE_LOCATION (c), 0,
7221 "chunk size value must be positive");
7222 t = integer_one_node;
7224 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7226 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
7228 if (!remove)
7229 schedule_seen = true;
7230 break;
7232 case OMP_CLAUSE_SIMDLEN:
7233 case OMP_CLAUSE_SAFELEN:
7234 t = OMP_CLAUSE_OPERAND (c, 0);
7235 if (t == error_mark_node)
7236 remove = true;
7237 else if (!type_dependent_expression_p (t)
7238 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
7240 error_at (OMP_CLAUSE_LOCATION (c),
7241 "%qs length expression must be integral",
7242 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7243 remove = true;
7245 else
7247 t = mark_rvalue_use (t);
7248 if (!processing_template_decl)
7250 t = maybe_constant_value (t);
7251 if (TREE_CODE (t) != INTEGER_CST
7252 || tree_int_cst_sgn (t) != 1)
7254 error_at (OMP_CLAUSE_LOCATION (c),
7255 "%qs length expression must be positive "
7256 "constant integer expression",
7257 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7258 remove = true;
7261 OMP_CLAUSE_OPERAND (c, 0) = t;
7262 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SAFELEN)
7263 safelen = c;
7265 break;
7267 case OMP_CLAUSE_ASYNC:
7268 t = OMP_CLAUSE_ASYNC_EXPR (c);
7269 if (t == error_mark_node)
7270 remove = true;
7271 else if (!type_dependent_expression_p (t)
7272 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
7274 error_at (OMP_CLAUSE_LOCATION (c),
7275 "%<async%> expression must be integral");
7276 remove = true;
7278 else
7280 t = mark_rvalue_use (t);
7281 if (!processing_template_decl)
7282 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7283 OMP_CLAUSE_ASYNC_EXPR (c) = t;
7285 break;
7287 case OMP_CLAUSE_WAIT:
7288 t = OMP_CLAUSE_WAIT_EXPR (c);
7289 if (t == error_mark_node)
7290 remove = true;
7291 else if (!processing_template_decl)
7292 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7293 OMP_CLAUSE_WAIT_EXPR (c) = t;
7294 break;
7296 case OMP_CLAUSE_THREAD_LIMIT:
7297 t = OMP_CLAUSE_THREAD_LIMIT_EXPR (c);
7298 if (t == error_mark_node)
7299 remove = true;
7300 else if (!type_dependent_expression_p (t)
7301 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
7303 error_at (OMP_CLAUSE_LOCATION (c),
7304 "%<thread_limit%> expression must be integral");
7305 remove = true;
7307 else
7309 t = mark_rvalue_use (t);
7310 if (!processing_template_decl)
7312 t = maybe_constant_value (t);
7313 if (TREE_CODE (t) == INTEGER_CST
7314 && tree_int_cst_sgn (t) != 1)
7316 warning_at (OMP_CLAUSE_LOCATION (c), 0,
7317 "%<thread_limit%> value must be positive");
7318 t = integer_one_node;
7320 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7322 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
7324 break;
7326 case OMP_CLAUSE_DEVICE:
7327 t = OMP_CLAUSE_DEVICE_ID (c);
7328 if (t == error_mark_node)
7329 remove = true;
7330 else if (!type_dependent_expression_p (t)
7331 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
7333 error_at (OMP_CLAUSE_LOCATION (c),
7334 "%<device%> id must be integral");
7335 remove = true;
7337 else
7339 t = mark_rvalue_use (t);
7340 if (!processing_template_decl)
7341 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7342 OMP_CLAUSE_DEVICE_ID (c) = t;
7344 break;
7346 case OMP_CLAUSE_DIST_SCHEDULE:
7347 t = OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c);
7348 if (t == NULL)
7350 else if (t == error_mark_node)
7351 remove = true;
7352 else if (!type_dependent_expression_p (t)
7353 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
7355 error_at (OMP_CLAUSE_LOCATION (c),
7356 "%<dist_schedule%> chunk size expression must be "
7357 "integral");
7358 remove = true;
7360 else
7362 t = mark_rvalue_use (t);
7363 if (!processing_template_decl)
7364 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7365 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
7367 break;
7369 case OMP_CLAUSE_ALIGNED:
7370 t = OMP_CLAUSE_DECL (c);
7371 if (t == current_class_ptr && ort != C_ORT_OMP_DECLARE_SIMD)
7373 error_at (OMP_CLAUSE_LOCATION (c),
7374 "%<this%> allowed in OpenMP only in %<declare simd%>"
7375 " clauses");
7376 remove = true;
7377 break;
7379 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
7381 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
7382 break;
7383 if (DECL_P (t))
7384 error_at (OMP_CLAUSE_LOCATION (c),
7385 "%qD is not a variable in %<aligned%> clause", t);
7386 else
7387 error_at (OMP_CLAUSE_LOCATION (c),
7388 "%qE is not a variable in %<aligned%> clause", t);
7389 remove = true;
7391 else if (!type_dependent_expression_p (t)
7392 && !TYPE_PTR_P (TREE_TYPE (t))
7393 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE
7394 && (!TYPE_REF_P (TREE_TYPE (t))
7395 || (!INDIRECT_TYPE_P (TREE_TYPE (TREE_TYPE (t)))
7396 && (TREE_CODE (TREE_TYPE (TREE_TYPE (t)))
7397 != ARRAY_TYPE))))
7399 error_at (OMP_CLAUSE_LOCATION (c),
7400 "%qE in %<aligned%> clause is neither a pointer nor "
7401 "an array nor a reference to pointer or array", t);
7402 remove = true;
7404 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
7406 error_at (OMP_CLAUSE_LOCATION (c),
7407 "%qD appears more than once in %<aligned%> clauses",
7409 remove = true;
7411 else
7412 bitmap_set_bit (&aligned_head, DECL_UID (t));
7413 t = OMP_CLAUSE_ALIGNED_ALIGNMENT (c);
7414 if (t == error_mark_node)
7415 remove = true;
7416 else if (t == NULL_TREE)
7417 break;
7418 else if (!type_dependent_expression_p (t)
7419 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
7421 error_at (OMP_CLAUSE_LOCATION (c),
7422 "%<aligned%> clause alignment expression must "
7423 "be integral");
7424 remove = true;
7426 else
7428 t = mark_rvalue_use (t);
7429 if (!processing_template_decl)
7431 t = maybe_constant_value (t);
7432 if (TREE_CODE (t) != INTEGER_CST
7433 || tree_int_cst_sgn (t) != 1)
7435 error_at (OMP_CLAUSE_LOCATION (c),
7436 "%<aligned%> clause alignment expression must "
7437 "be positive constant integer expression");
7438 remove = true;
7440 else
7441 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7443 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = t;
7445 break;
7447 case OMP_CLAUSE_NONTEMPORAL:
7448 t = OMP_CLAUSE_DECL (c);
7449 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
7451 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
7452 break;
7453 if (DECL_P (t))
7454 error_at (OMP_CLAUSE_LOCATION (c),
7455 "%qD is not a variable in %<nontemporal%> clause",
7457 else
7458 error_at (OMP_CLAUSE_LOCATION (c),
7459 "%qE is not a variable in %<nontemporal%> clause",
7461 remove = true;
7463 else if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
7465 error_at (OMP_CLAUSE_LOCATION (c),
7466 "%qD appears more than once in %<nontemporal%> "
7467 "clauses", t);
7468 remove = true;
7470 else
7471 bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
7472 break;
7474 case OMP_CLAUSE_ALLOCATE:
7475 t = omp_clause_decl_field (OMP_CLAUSE_DECL (c));
7476 if (t)
7477 omp_note_field_privatization (t, OMP_CLAUSE_DECL (c));
7478 else
7479 t = OMP_CLAUSE_DECL (c);
7480 if (t == current_class_ptr)
7482 error_at (OMP_CLAUSE_LOCATION (c),
7483 "%<this%> not allowed in %<allocate%> clause");
7484 remove = true;
7485 break;
7487 if (!VAR_P (t)
7488 && TREE_CODE (t) != PARM_DECL
7489 && TREE_CODE (t) != FIELD_DECL)
7491 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
7492 break;
7493 if (DECL_P (t))
7494 error_at (OMP_CLAUSE_LOCATION (c),
7495 "%qD is not a variable in %<allocate%> clause", t);
7496 else
7497 error_at (OMP_CLAUSE_LOCATION (c),
7498 "%qE is not a variable in %<allocate%> clause", t);
7499 remove = true;
7501 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
7503 warning_at (OMP_CLAUSE_LOCATION (c), 0,
7504 "%qD appears more than once in %<allocate%> clauses",
7506 remove = true;
7508 else
7510 bitmap_set_bit (&aligned_head, DECL_UID (t));
7511 allocate_seen = true;
7513 tree allocator;
7514 allocator = OMP_CLAUSE_ALLOCATE_ALLOCATOR (c);
7515 if (error_operand_p (allocator))
7517 remove = true;
7518 break;
7520 if (allocator == NULL_TREE)
7521 goto handle_field_decl;
7522 tree allocatort;
7523 allocatort = TYPE_MAIN_VARIANT (TREE_TYPE (allocator));
7524 if (!type_dependent_expression_p (allocator)
7525 && (TREE_CODE (allocatort) != ENUMERAL_TYPE
7526 || TYPE_NAME (allocatort) == NULL_TREE
7527 || TREE_CODE (TYPE_NAME (allocatort)) != TYPE_DECL
7528 || (DECL_NAME (TYPE_NAME (allocatort))
7529 != get_identifier ("omp_allocator_handle_t"))
7530 || (TYPE_CONTEXT (allocatort)
7531 != DECL_CONTEXT (global_namespace))))
7533 error_at (OMP_CLAUSE_LOCATION (c),
7534 "%<allocate%> clause allocator expression has "
7535 "type %qT rather than %<omp_allocator_handle_t%>",
7536 TREE_TYPE (allocator));
7537 remove = true;
7539 else
7541 allocator = mark_rvalue_use (allocator);
7542 if (!processing_template_decl)
7543 allocator = maybe_constant_value (allocator);
7544 OMP_CLAUSE_ALLOCATE_ALLOCATOR (c) = allocator;
7546 goto handle_field_decl;
7548 case OMP_CLAUSE_DEPEND:
7549 t = OMP_CLAUSE_DECL (c);
7550 if (t == NULL_TREE)
7552 gcc_assert (OMP_CLAUSE_DEPEND_KIND (c)
7553 == OMP_CLAUSE_DEPEND_SOURCE);
7554 break;
7556 if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SINK)
7558 if (cp_finish_omp_clause_depend_sink (c))
7559 remove = true;
7560 break;
7562 /* FALLTHRU */
7563 case OMP_CLAUSE_AFFINITY:
7564 t = OMP_CLAUSE_DECL (c);
7565 if (TREE_CODE (t) == TREE_LIST
7566 && TREE_PURPOSE (t)
7567 && TREE_CODE (TREE_PURPOSE (t)) == TREE_VEC)
7569 if (TREE_PURPOSE (t) != last_iterators)
7570 last_iterators_remove
7571 = cp_omp_finish_iterators (TREE_PURPOSE (t));
7572 last_iterators = TREE_PURPOSE (t);
7573 t = TREE_VALUE (t);
7574 if (last_iterators_remove)
7575 t = error_mark_node;
7577 else
7578 last_iterators = NULL_TREE;
7580 if (TREE_CODE (t) == TREE_LIST)
7582 if (handle_omp_array_sections (c, ort))
7583 remove = true;
7584 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
7585 && (OMP_CLAUSE_DEPEND_KIND (c)
7586 == OMP_CLAUSE_DEPEND_DEPOBJ))
7588 error_at (OMP_CLAUSE_LOCATION (c),
7589 "%<depend%> clause with %<depobj%> dependence "
7590 "type on array section");
7591 remove = true;
7593 break;
7595 if (t == error_mark_node)
7596 remove = true;
7597 else if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
7598 break;
7599 else if (!lvalue_p (t))
7601 if (DECL_P (t))
7602 error_at (OMP_CLAUSE_LOCATION (c),
7603 "%qD is not lvalue expression nor array section "
7604 "in %qs clause", t,
7605 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7606 else
7607 error_at (OMP_CLAUSE_LOCATION (c),
7608 "%qE is not lvalue expression nor array section "
7609 "in %qs clause", t,
7610 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7611 remove = true;
7613 else if (TREE_CODE (t) == COMPONENT_REF
7614 && TREE_CODE (TREE_OPERAND (t, 1)) == FIELD_DECL
7615 && DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
7617 error_at (OMP_CLAUSE_LOCATION (c),
7618 "bit-field %qE in %qs clause", t,
7619 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7620 remove = true;
7622 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
7623 && OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_DEPOBJ)
7625 if (!c_omp_depend_t_p (TYPE_REF_P (TREE_TYPE (t))
7626 ? TREE_TYPE (TREE_TYPE (t))
7627 : TREE_TYPE (t)))
7629 error_at (OMP_CLAUSE_LOCATION (c),
7630 "%qE does not have %<omp_depend_t%> type in "
7631 "%<depend%> clause with %<depobj%> dependence "
7632 "type", t);
7633 remove = true;
7636 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
7637 && c_omp_depend_t_p (TYPE_REF_P (TREE_TYPE (t))
7638 ? TREE_TYPE (TREE_TYPE (t))
7639 : TREE_TYPE (t)))
7641 error_at (OMP_CLAUSE_LOCATION (c),
7642 "%qE should not have %<omp_depend_t%> type in "
7643 "%<depend%> clause with dependence type other than "
7644 "%<depobj%>", t);
7645 remove = true;
7647 if (!remove)
7649 tree addr = cp_build_addr_expr (t, tf_warning_or_error);
7650 if (addr == error_mark_node)
7651 remove = true;
7652 else
7654 t = cp_build_indirect_ref (OMP_CLAUSE_LOCATION (c),
7655 addr, RO_UNARY_STAR,
7656 tf_warning_or_error);
7657 if (t == error_mark_node)
7658 remove = true;
7659 else if (TREE_CODE (OMP_CLAUSE_DECL (c)) == TREE_LIST
7660 && TREE_PURPOSE (OMP_CLAUSE_DECL (c))
7661 && (TREE_CODE (TREE_PURPOSE (OMP_CLAUSE_DECL (c)))
7662 == TREE_VEC))
7663 TREE_VALUE (OMP_CLAUSE_DECL (c)) = t;
7664 else
7665 OMP_CLAUSE_DECL (c) = t;
7668 break;
7669 case OMP_CLAUSE_DETACH:
7670 t = OMP_CLAUSE_DECL (c);
7671 if (detach_seen)
7673 error_at (OMP_CLAUSE_LOCATION (c),
7674 "too many %qs clauses on a task construct",
7675 "detach");
7676 remove = true;
7677 break;
7679 else if (error_operand_p (t))
7681 remove = true;
7682 break;
7684 else
7686 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (t));
7687 if (!type_dependent_expression_p (t)
7688 && (!INTEGRAL_TYPE_P (type)
7689 || TREE_CODE (type) != ENUMERAL_TYPE
7690 || TYPE_NAME (type) == NULL_TREE
7691 || (DECL_NAME (TYPE_NAME (type))
7692 != get_identifier ("omp_event_handle_t"))))
7694 error_at (OMP_CLAUSE_LOCATION (c),
7695 "%<detach%> clause event handle "
7696 "has type %qT rather than "
7697 "%<omp_event_handle_t%>",
7698 type);
7699 remove = true;
7701 detach_seen = c;
7702 cxx_mark_addressable (t);
7704 break;
7706 case OMP_CLAUSE_MAP:
7707 if (OMP_CLAUSE_MAP_IMPLICIT (c) && !implicit_moved)
7708 goto move_implicit;
7709 /* FALLTHRU */
7710 case OMP_CLAUSE_TO:
7711 case OMP_CLAUSE_FROM:
7712 case OMP_CLAUSE__CACHE_:
7713 t = OMP_CLAUSE_DECL (c);
7714 if (TREE_CODE (t) == TREE_LIST)
7716 if (handle_omp_array_sections (c, ort))
7717 remove = true;
7718 else
7720 t = OMP_CLAUSE_DECL (c);
7721 if (TREE_CODE (t) != TREE_LIST
7722 && !type_dependent_expression_p (t)
7723 && !cp_omp_mappable_type (TREE_TYPE (t)))
7725 error_at (OMP_CLAUSE_LOCATION (c),
7726 "array section does not have mappable type "
7727 "in %qs clause",
7728 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7729 cp_omp_emit_unmappable_type_notes (TREE_TYPE (t));
7730 remove = true;
7732 while (TREE_CODE (t) == ARRAY_REF)
7733 t = TREE_OPERAND (t, 0);
7734 if (TREE_CODE (t) == COMPONENT_REF
7735 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
7737 while (TREE_CODE (t) == COMPONENT_REF)
7738 t = TREE_OPERAND (t, 0);
7739 if (REFERENCE_REF_P (t))
7740 t = TREE_OPERAND (t, 0);
7741 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
7742 && OMP_CLAUSE_MAP_IMPLICIT (c)
7743 && (bitmap_bit_p (&map_head, DECL_UID (t))
7744 || bitmap_bit_p (&map_field_head, DECL_UID (t))
7745 || bitmap_bit_p (&map_firstprivate_head,
7746 DECL_UID (t))))
7748 remove = true;
7749 break;
7751 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
7752 break;
7753 if (bitmap_bit_p (&map_head, DECL_UID (t)))
7755 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
7756 error_at (OMP_CLAUSE_LOCATION (c),
7757 "%qD appears more than once in motion"
7758 " clauses", t);
7759 else if (ort == C_ORT_ACC)
7760 error_at (OMP_CLAUSE_LOCATION (c),
7761 "%qD appears more than once in data"
7762 " clauses", t);
7763 else
7764 error_at (OMP_CLAUSE_LOCATION (c),
7765 "%qD appears more than once in map"
7766 " clauses", t);
7767 remove = true;
7769 else
7771 bitmap_set_bit (&map_head, DECL_UID (t));
7772 bitmap_set_bit (&map_field_head, DECL_UID (t));
7776 if (cp_oacc_check_attachments (c))
7777 remove = true;
7778 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
7779 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
7780 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH))
7781 /* In this case, we have a single array element which is a
7782 pointer, and we already set OMP_CLAUSE_SIZE in
7783 handle_omp_array_sections above. For attach/detach clauses,
7784 reset the OMP_CLAUSE_SIZE (representing a bias) to zero
7785 here. */
7786 OMP_CLAUSE_SIZE (c) = size_zero_node;
7787 break;
7789 if (t == error_mark_node)
7791 remove = true;
7792 break;
7794 /* OpenACC attach / detach clauses must be pointers. */
7795 if (cp_oacc_check_attachments (c))
7797 remove = true;
7798 break;
7800 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
7801 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
7802 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH))
7803 /* For attach/detach clauses, set OMP_CLAUSE_SIZE (representing a
7804 bias) to zero here, so it is not set erroneously to the pointer
7805 size later on in gimplify.c. */
7806 OMP_CLAUSE_SIZE (c) = size_zero_node;
7807 if (REFERENCE_REF_P (t)
7808 && TREE_CODE (TREE_OPERAND (t, 0)) == COMPONENT_REF)
7810 t = TREE_OPERAND (t, 0);
7811 OMP_CLAUSE_DECL (c) = t;
7813 if (TREE_CODE (t) == COMPONENT_REF
7814 && TREE_CODE (TREE_OPERAND (t, 0)) == INDIRECT_REF)
7815 t = TREE_OPERAND (TREE_OPERAND (t, 0), 0);
7816 if (TREE_CODE (t) == COMPONENT_REF
7817 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE__CACHE_)
7819 if (type_dependent_expression_p (t))
7820 break;
7821 if (TREE_CODE (TREE_OPERAND (t, 1)) == FIELD_DECL
7822 && DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
7824 error_at (OMP_CLAUSE_LOCATION (c),
7825 "bit-field %qE in %qs clause",
7826 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7827 remove = true;
7829 else if (!cp_omp_mappable_type (TREE_TYPE (t)))
7831 error_at (OMP_CLAUSE_LOCATION (c),
7832 "%qE does not have a mappable type in %qs clause",
7833 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7834 cp_omp_emit_unmappable_type_notes (TREE_TYPE (t));
7835 remove = true;
7837 while (TREE_CODE (t) == COMPONENT_REF)
7839 if (TREE_TYPE (TREE_OPERAND (t, 0))
7840 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0)))
7841 == UNION_TYPE))
7843 error_at (OMP_CLAUSE_LOCATION (c),
7844 "%qE is a member of a union", t);
7845 remove = true;
7846 break;
7848 t = TREE_OPERAND (t, 0);
7850 if (remove)
7851 break;
7852 if (REFERENCE_REF_P (t))
7853 t = TREE_OPERAND (t, 0);
7854 if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
7856 if (bitmap_bit_p (&map_field_head, DECL_UID (t))
7857 || (ort != C_ORT_ACC
7858 && bitmap_bit_p (&map_head, DECL_UID (t))))
7859 goto handle_map_references;
7862 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
7864 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
7865 break;
7866 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
7867 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
7868 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ALWAYS_POINTER
7869 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH_DETACH))
7870 break;
7871 if (DECL_P (t))
7872 error_at (OMP_CLAUSE_LOCATION (c),
7873 "%qD is not a variable in %qs clause", t,
7874 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7875 else
7876 error_at (OMP_CLAUSE_LOCATION (c),
7877 "%qE is not a variable in %qs clause", t,
7878 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7879 remove = true;
7881 else if (VAR_P (t) && CP_DECL_THREAD_LOCAL_P (t))
7883 error_at (OMP_CLAUSE_LOCATION (c),
7884 "%qD is threadprivate variable in %qs clause", t,
7885 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7886 remove = true;
7888 else if (ort != C_ORT_ACC && t == current_class_ptr)
7890 error_at (OMP_CLAUSE_LOCATION (c),
7891 "%<this%> allowed in OpenMP only in %<declare simd%>"
7892 " clauses");
7893 remove = true;
7894 break;
7896 else if (!processing_template_decl
7897 && !TYPE_REF_P (TREE_TYPE (t))
7898 && (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
7899 || (OMP_CLAUSE_MAP_KIND (c)
7900 != GOMP_MAP_FIRSTPRIVATE_POINTER))
7901 && !cxx_mark_addressable (t))
7902 remove = true;
7903 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
7904 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
7905 || (OMP_CLAUSE_MAP_KIND (c)
7906 == GOMP_MAP_FIRSTPRIVATE_POINTER)))
7907 && t == OMP_CLAUSE_DECL (c)
7908 && !type_dependent_expression_p (t)
7909 && !cp_omp_mappable_type (TYPE_REF_P (TREE_TYPE (t))
7910 ? TREE_TYPE (TREE_TYPE (t))
7911 : TREE_TYPE (t)))
7913 error_at (OMP_CLAUSE_LOCATION (c),
7914 "%qD does not have a mappable type in %qs clause", t,
7915 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7916 cp_omp_emit_unmappable_type_notes (TREE_TYPE (t));
7917 remove = true;
7919 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
7920 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FORCE_DEVICEPTR
7921 && !type_dependent_expression_p (t)
7922 && !INDIRECT_TYPE_P (TREE_TYPE (t)))
7924 error_at (OMP_CLAUSE_LOCATION (c),
7925 "%qD is not a pointer variable", t);
7926 remove = true;
7928 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
7929 && OMP_CLAUSE_MAP_IMPLICIT (c)
7930 && (bitmap_bit_p (&map_head, DECL_UID (t))
7931 || bitmap_bit_p (&map_field_head, DECL_UID (t))
7932 || bitmap_bit_p (&map_firstprivate_head,
7933 DECL_UID (t))))
7934 remove = true;
7935 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
7936 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
7938 if (bitmap_bit_p (&generic_head, DECL_UID (t))
7939 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
7940 || bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
7942 error_at (OMP_CLAUSE_LOCATION (c),
7943 "%qD appears more than once in data clauses", t);
7944 remove = true;
7946 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
7948 if (ort == C_ORT_ACC)
7949 error_at (OMP_CLAUSE_LOCATION (c),
7950 "%qD appears more than once in data clauses", t);
7951 else
7952 error_at (OMP_CLAUSE_LOCATION (c),
7953 "%qD appears both in data and map clauses", t);
7954 remove = true;
7956 else
7957 bitmap_set_bit (&map_firstprivate_head, DECL_UID (t));
7959 else if (bitmap_bit_p (&map_head, DECL_UID (t))
7960 && !bitmap_bit_p (&map_field_head, DECL_UID (t)))
7962 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
7963 error_at (OMP_CLAUSE_LOCATION (c),
7964 "%qD appears more than once in motion clauses", t);
7965 else if (ort == C_ORT_ACC)
7966 error_at (OMP_CLAUSE_LOCATION (c),
7967 "%qD appears more than once in data clauses", t);
7968 else
7969 error_at (OMP_CLAUSE_LOCATION (c),
7970 "%qD appears more than once in map clauses", t);
7971 remove = true;
7973 else if (ort == C_ORT_ACC
7974 && bitmap_bit_p (&generic_head, DECL_UID (t)))
7976 error_at (OMP_CLAUSE_LOCATION (c),
7977 "%qD appears more than once in data clauses", t);
7978 remove = true;
7980 else if (bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
7982 if (ort == C_ORT_ACC)
7983 error_at (OMP_CLAUSE_LOCATION (c),
7984 "%qD appears more than once in data clauses", t);
7985 else
7986 error_at (OMP_CLAUSE_LOCATION (c),
7987 "%qD appears both in data and map clauses", t);
7988 remove = true;
7990 else
7992 bitmap_set_bit (&map_head, DECL_UID (t));
7993 if (t != OMP_CLAUSE_DECL (c)
7994 && TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPONENT_REF)
7995 bitmap_set_bit (&map_field_head, DECL_UID (t));
7997 handle_map_references:
7998 if (!remove
7999 && !processing_template_decl
8000 && ort != C_ORT_DECLARE_SIMD
8001 && TYPE_REF_P (TREE_TYPE (OMP_CLAUSE_DECL (c))))
8003 t = OMP_CLAUSE_DECL (c);
8004 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
8006 OMP_CLAUSE_DECL (c) = build_simple_mem_ref (t);
8007 if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
8008 OMP_CLAUSE_SIZE (c)
8009 = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t)));
8011 else if (OMP_CLAUSE_MAP_KIND (c)
8012 != GOMP_MAP_FIRSTPRIVATE_POINTER
8013 && (OMP_CLAUSE_MAP_KIND (c)
8014 != GOMP_MAP_FIRSTPRIVATE_REFERENCE)
8015 && (OMP_CLAUSE_MAP_KIND (c)
8016 != GOMP_MAP_ALWAYS_POINTER)
8017 && (OMP_CLAUSE_MAP_KIND (c)
8018 != GOMP_MAP_ATTACH_DETACH))
8020 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
8021 OMP_CLAUSE_MAP);
8022 if (TREE_CODE (t) == COMPONENT_REF)
8023 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ATTACH_DETACH);
8024 else
8025 OMP_CLAUSE_SET_MAP_KIND (c2,
8026 GOMP_MAP_FIRSTPRIVATE_REFERENCE);
8027 OMP_CLAUSE_DECL (c2) = t;
8028 OMP_CLAUSE_SIZE (c2) = size_zero_node;
8029 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
8030 OMP_CLAUSE_CHAIN (c) = c2;
8031 OMP_CLAUSE_DECL (c) = build_simple_mem_ref (t);
8032 if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
8033 OMP_CLAUSE_SIZE (c)
8034 = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t)));
8035 c = c2;
8038 break;
8040 case OMP_CLAUSE_TO_DECLARE:
8041 case OMP_CLAUSE_LINK:
8042 t = OMP_CLAUSE_DECL (c);
8043 if (TREE_CODE (t) == FUNCTION_DECL
8044 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
8046 else if (!VAR_P (t))
8048 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
8050 if (TREE_CODE (t) == TEMPLATE_ID_EXPR)
8051 error_at (OMP_CLAUSE_LOCATION (c),
8052 "template %qE in clause %qs", t,
8053 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
8054 else if (really_overloaded_fn (t))
8055 error_at (OMP_CLAUSE_LOCATION (c),
8056 "overloaded function name %qE in clause %qs", t,
8057 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
8058 else
8059 error_at (OMP_CLAUSE_LOCATION (c),
8060 "%qE is neither a variable nor a function name "
8061 "in clause %qs", t,
8062 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
8064 else
8065 error_at (OMP_CLAUSE_LOCATION (c),
8066 "%qE is not a variable in clause %qs", t,
8067 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
8068 remove = true;
8070 else if (DECL_THREAD_LOCAL_P (t))
8072 error_at (OMP_CLAUSE_LOCATION (c),
8073 "%qD is threadprivate variable in %qs clause", t,
8074 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
8075 remove = true;
8077 else if (!cp_omp_mappable_type (TREE_TYPE (t)))
8079 error_at (OMP_CLAUSE_LOCATION (c),
8080 "%qD does not have a mappable type in %qs clause", t,
8081 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
8082 cp_omp_emit_unmappable_type_notes (TREE_TYPE (t));
8083 remove = true;
8085 if (remove)
8086 break;
8087 if (bitmap_bit_p (&generic_head, DECL_UID (t)))
8089 error_at (OMP_CLAUSE_LOCATION (c),
8090 "%qE appears more than once on the same "
8091 "%<declare target%> directive", t);
8092 remove = true;
8094 else
8095 bitmap_set_bit (&generic_head, DECL_UID (t));
8096 break;
8098 case OMP_CLAUSE_UNIFORM:
8099 t = OMP_CLAUSE_DECL (c);
8100 if (TREE_CODE (t) != PARM_DECL)
8102 if (processing_template_decl)
8103 break;
8104 if (DECL_P (t))
8105 error_at (OMP_CLAUSE_LOCATION (c),
8106 "%qD is not an argument in %<uniform%> clause", t);
8107 else
8108 error_at (OMP_CLAUSE_LOCATION (c),
8109 "%qE is not an argument in %<uniform%> clause", t);
8110 remove = true;
8111 break;
8113 /* map_head bitmap is used as uniform_head if declare_simd. */
8114 bitmap_set_bit (&map_head, DECL_UID (t));
8115 goto check_dup_generic;
8117 case OMP_CLAUSE_GRAINSIZE:
8118 t = OMP_CLAUSE_GRAINSIZE_EXPR (c);
8119 if (t == error_mark_node)
8120 remove = true;
8121 else if (!type_dependent_expression_p (t)
8122 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
8124 error_at (OMP_CLAUSE_LOCATION (c),
8125 "%<grainsize%> expression must be integral");
8126 remove = true;
8128 else
8130 t = mark_rvalue_use (t);
8131 if (!processing_template_decl)
8133 t = maybe_constant_value (t);
8134 if (TREE_CODE (t) == INTEGER_CST
8135 && tree_int_cst_sgn (t) != 1)
8137 warning_at (OMP_CLAUSE_LOCATION (c), 0,
8138 "%<grainsize%> value must be positive");
8139 t = integer_one_node;
8141 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
8143 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
8145 break;
8147 case OMP_CLAUSE_PRIORITY:
8148 t = OMP_CLAUSE_PRIORITY_EXPR (c);
8149 if (t == error_mark_node)
8150 remove = true;
8151 else if (!type_dependent_expression_p (t)
8152 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
8154 error_at (OMP_CLAUSE_LOCATION (c),
8155 "%<priority%> expression must be integral");
8156 remove = true;
8158 else
8160 t = mark_rvalue_use (t);
8161 if (!processing_template_decl)
8163 t = maybe_constant_value (t);
8164 if (TREE_CODE (t) == INTEGER_CST
8165 && tree_int_cst_sgn (t) == -1)
8167 warning_at (OMP_CLAUSE_LOCATION (c), 0,
8168 "%<priority%> value must be non-negative");
8169 t = integer_one_node;
8171 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
8173 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
8175 break;
8177 case OMP_CLAUSE_HINT:
8178 t = OMP_CLAUSE_HINT_EXPR (c);
8179 if (t == error_mark_node)
8180 remove = true;
8181 else if (!type_dependent_expression_p (t)
8182 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
8184 error_at (OMP_CLAUSE_LOCATION (c),
8185 "%<hint%> expression must be integral");
8186 remove = true;
8188 else
8190 t = mark_rvalue_use (t);
8191 if (!processing_template_decl)
8193 t = maybe_constant_value (t);
8194 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
8195 if (TREE_CODE (t) != INTEGER_CST)
8197 error_at (OMP_CLAUSE_LOCATION (c),
8198 "%<hint%> expression must be constant integer "
8199 "expression");
8200 remove = true;
8203 OMP_CLAUSE_HINT_EXPR (c) = t;
8205 break;
8207 case OMP_CLAUSE_FILTER:
8208 t = OMP_CLAUSE_FILTER_EXPR (c);
8209 if (t == error_mark_node)
8210 remove = true;
8211 else if (!type_dependent_expression_p (t)
8212 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
8214 error_at (OMP_CLAUSE_LOCATION (c),
8215 "%<filter%> expression must be integral");
8216 remove = true;
8218 else
8220 t = mark_rvalue_use (t);
8221 if (!processing_template_decl)
8223 t = maybe_constant_value (t);
8224 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
8226 OMP_CLAUSE_FILTER_EXPR (c) = t;
8228 break;
8230 case OMP_CLAUSE_IS_DEVICE_PTR:
8231 case OMP_CLAUSE_USE_DEVICE_PTR:
8232 field_ok = (ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP;
8233 t = OMP_CLAUSE_DECL (c);
8234 if (!type_dependent_expression_p (t))
8236 tree type = TREE_TYPE (t);
8237 if (!TYPE_PTR_P (type)
8238 && (!TYPE_REF_P (type) || !TYPE_PTR_P (TREE_TYPE (type))))
8240 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_USE_DEVICE_PTR
8241 && ort == C_ORT_OMP)
8243 error_at (OMP_CLAUSE_LOCATION (c),
8244 "%qs variable is neither a pointer "
8245 "nor reference to pointer",
8246 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
8247 remove = true;
8249 else if (TREE_CODE (type) != ARRAY_TYPE
8250 && (!TYPE_REF_P (type)
8251 || TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE))
8253 error_at (OMP_CLAUSE_LOCATION (c),
8254 "%qs variable is neither a pointer, nor an "
8255 "array nor reference to pointer or array",
8256 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
8257 remove = true;
8261 goto check_dup_generic;
8263 case OMP_CLAUSE_USE_DEVICE_ADDR:
8264 field_ok = true;
8265 t = OMP_CLAUSE_DECL (c);
8266 if (!processing_template_decl
8267 && (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
8268 && !TYPE_REF_P (TREE_TYPE (t))
8269 && !cxx_mark_addressable (t))
8270 remove = true;
8271 goto check_dup_generic;
8273 case OMP_CLAUSE_NOWAIT:
8274 case OMP_CLAUSE_DEFAULT:
8275 case OMP_CLAUSE_UNTIED:
8276 case OMP_CLAUSE_COLLAPSE:
8277 case OMP_CLAUSE_PARALLEL:
8278 case OMP_CLAUSE_FOR:
8279 case OMP_CLAUSE_SECTIONS:
8280 case OMP_CLAUSE_TASKGROUP:
8281 case OMP_CLAUSE_PROC_BIND:
8282 case OMP_CLAUSE_DEVICE_TYPE:
8283 case OMP_CLAUSE_NOGROUP:
8284 case OMP_CLAUSE_THREADS:
8285 case OMP_CLAUSE_SIMD:
8286 case OMP_CLAUSE_DEFAULTMAP:
8287 case OMP_CLAUSE_BIND:
8288 case OMP_CLAUSE_AUTO:
8289 case OMP_CLAUSE_INDEPENDENT:
8290 case OMP_CLAUSE_SEQ:
8291 case OMP_CLAUSE_IF_PRESENT:
8292 case OMP_CLAUSE_FINALIZE:
8293 case OMP_CLAUSE_NOHOST:
8294 break;
8296 case OMP_CLAUSE_MERGEABLE:
8297 mergeable_seen = true;
8298 break;
8300 case OMP_CLAUSE_TILE:
8301 for (tree list = OMP_CLAUSE_TILE_LIST (c); !remove && list;
8302 list = TREE_CHAIN (list))
8304 t = TREE_VALUE (list);
8306 if (t == error_mark_node)
8307 remove = true;
8308 else if (!type_dependent_expression_p (t)
8309 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
8311 error_at (OMP_CLAUSE_LOCATION (c),
8312 "%<tile%> argument needs integral type");
8313 remove = true;
8315 else
8317 t = mark_rvalue_use (t);
8318 if (!processing_template_decl)
8320 /* Zero is used to indicate '*', we permit you
8321 to get there via an ICE of value zero. */
8322 t = maybe_constant_value (t);
8323 if (!tree_fits_shwi_p (t)
8324 || tree_to_shwi (t) < 0)
8326 error_at (OMP_CLAUSE_LOCATION (c),
8327 "%<tile%> argument needs positive "
8328 "integral constant");
8329 remove = true;
8331 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
8335 /* Update list item. */
8336 TREE_VALUE (list) = t;
8338 break;
8340 case OMP_CLAUSE_ORDERED:
8341 ordered_seen = true;
8342 break;
8344 case OMP_CLAUSE_ORDER:
8345 if (order_seen)
8346 remove = true;
8347 else
8348 order_seen = true;
8349 break;
8351 case OMP_CLAUSE_INBRANCH:
8352 case OMP_CLAUSE_NOTINBRANCH:
8353 if (branch_seen)
8355 error_at (OMP_CLAUSE_LOCATION (c),
8356 "%<inbranch%> clause is incompatible with "
8357 "%<notinbranch%>");
8358 remove = true;
8360 branch_seen = true;
8361 break;
8363 case OMP_CLAUSE_INCLUSIVE:
8364 case OMP_CLAUSE_EXCLUSIVE:
8365 t = omp_clause_decl_field (OMP_CLAUSE_DECL (c));
8366 if (!t)
8367 t = OMP_CLAUSE_DECL (c);
8368 if (t == current_class_ptr)
8370 error_at (OMP_CLAUSE_LOCATION (c),
8371 "%<this%> allowed in OpenMP only in %<declare simd%>"
8372 " clauses");
8373 remove = true;
8374 break;
8376 if (!VAR_P (t)
8377 && TREE_CODE (t) != PARM_DECL
8378 && TREE_CODE (t) != FIELD_DECL)
8380 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
8381 break;
8382 if (DECL_P (t))
8383 error_at (OMP_CLAUSE_LOCATION (c),
8384 "%qD is not a variable in clause %qs", t,
8385 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
8386 else
8387 error_at (OMP_CLAUSE_LOCATION (c),
8388 "%qE is not a variable in clause %qs", t,
8389 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
8390 remove = true;
8392 break;
8394 default:
8395 gcc_unreachable ();
8398 if (remove)
8399 *pc = OMP_CLAUSE_CHAIN (c);
8400 else
8401 pc = &OMP_CLAUSE_CHAIN (c);
8404 if (reduction_seen < 0 && (ordered_seen || schedule_seen))
8405 reduction_seen = -2;
8407 for (pc = &clauses, c = clauses; c ; c = *pc)
8409 enum omp_clause_code c_kind = OMP_CLAUSE_CODE (c);
8410 bool remove = false;
8411 bool need_complete_type = false;
8412 bool need_default_ctor = false;
8413 bool need_copy_ctor = false;
8414 bool need_copy_assignment = false;
8415 bool need_implicitly_determined = false;
8416 bool need_dtor = false;
8417 tree type, inner_type;
8419 switch (c_kind)
8421 case OMP_CLAUSE_SHARED:
8422 need_implicitly_determined = true;
8423 break;
8424 case OMP_CLAUSE_PRIVATE:
8425 need_complete_type = true;
8426 need_default_ctor = true;
8427 need_dtor = true;
8428 need_implicitly_determined = true;
8429 break;
8430 case OMP_CLAUSE_FIRSTPRIVATE:
8431 need_complete_type = true;
8432 need_copy_ctor = true;
8433 need_dtor = true;
8434 need_implicitly_determined = true;
8435 break;
8436 case OMP_CLAUSE_LASTPRIVATE:
8437 need_complete_type = true;
8438 need_copy_assignment = true;
8439 need_implicitly_determined = true;
8440 break;
8441 case OMP_CLAUSE_REDUCTION:
8442 if (reduction_seen == -2)
8443 OMP_CLAUSE_REDUCTION_INSCAN (c) = 0;
8444 if (OMP_CLAUSE_REDUCTION_INSCAN (c))
8445 need_copy_assignment = true;
8446 need_implicitly_determined = true;
8447 break;
8448 case OMP_CLAUSE_IN_REDUCTION:
8449 case OMP_CLAUSE_TASK_REDUCTION:
8450 case OMP_CLAUSE_INCLUSIVE:
8451 case OMP_CLAUSE_EXCLUSIVE:
8452 need_implicitly_determined = true;
8453 break;
8454 case OMP_CLAUSE_LINEAR:
8455 if (ort != C_ORT_OMP_DECLARE_SIMD)
8456 need_implicitly_determined = true;
8457 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c)
8458 && !bitmap_bit_p (&map_head,
8459 DECL_UID (OMP_CLAUSE_LINEAR_STEP (c))))
8461 error_at (OMP_CLAUSE_LOCATION (c),
8462 "%<linear%> clause step is a parameter %qD not "
8463 "specified in %<uniform%> clause",
8464 OMP_CLAUSE_LINEAR_STEP (c));
8465 *pc = OMP_CLAUSE_CHAIN (c);
8466 continue;
8468 break;
8469 case OMP_CLAUSE_COPYPRIVATE:
8470 need_copy_assignment = true;
8471 break;
8472 case OMP_CLAUSE_COPYIN:
8473 need_copy_assignment = true;
8474 break;
8475 case OMP_CLAUSE_SIMDLEN:
8476 if (safelen
8477 && !processing_template_decl
8478 && tree_int_cst_lt (OMP_CLAUSE_SAFELEN_EXPR (safelen),
8479 OMP_CLAUSE_SIMDLEN_EXPR (c)))
8481 error_at (OMP_CLAUSE_LOCATION (c),
8482 "%<simdlen%> clause value is bigger than "
8483 "%<safelen%> clause value");
8484 OMP_CLAUSE_SIMDLEN_EXPR (c)
8485 = OMP_CLAUSE_SAFELEN_EXPR (safelen);
8487 pc = &OMP_CLAUSE_CHAIN (c);
8488 continue;
8489 case OMP_CLAUSE_SCHEDULE:
8490 if (ordered_seen
8491 && (OMP_CLAUSE_SCHEDULE_KIND (c)
8492 & OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
8494 error_at (OMP_CLAUSE_LOCATION (c),
8495 "%<nonmonotonic%> schedule modifier specified "
8496 "together with %<ordered%> clause");
8497 OMP_CLAUSE_SCHEDULE_KIND (c)
8498 = (enum omp_clause_schedule_kind)
8499 (OMP_CLAUSE_SCHEDULE_KIND (c)
8500 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
8502 if (reduction_seen == -2)
8503 error_at (OMP_CLAUSE_LOCATION (c),
8504 "%qs clause specified together with %<inscan%> "
8505 "%<reduction%> clause", "schedule");
8506 pc = &OMP_CLAUSE_CHAIN (c);
8507 continue;
8508 case OMP_CLAUSE_NOGROUP:
8509 if (reduction_seen)
8511 error_at (OMP_CLAUSE_LOCATION (c),
8512 "%<nogroup%> clause must not be used together with "
8513 "%<reduction%> clause");
8514 *pc = OMP_CLAUSE_CHAIN (c);
8515 continue;
8517 pc = &OMP_CLAUSE_CHAIN (c);
8518 continue;
8519 case OMP_CLAUSE_ORDERED:
8520 if (reduction_seen == -2)
8521 error_at (OMP_CLAUSE_LOCATION (c),
8522 "%qs clause specified together with %<inscan%> "
8523 "%<reduction%> clause", "ordered");
8524 pc = &OMP_CLAUSE_CHAIN (c);
8525 continue;
8526 case OMP_CLAUSE_ORDER:
8527 if (ordered_seen)
8529 error_at (OMP_CLAUSE_LOCATION (c),
8530 "%<order%> clause must not be used together "
8531 "with %<ordered%>");
8532 *pc = OMP_CLAUSE_CHAIN (c);
8533 continue;
8535 pc = &OMP_CLAUSE_CHAIN (c);
8536 continue;
8537 case OMP_CLAUSE_DETACH:
8538 if (mergeable_seen)
8540 error_at (OMP_CLAUSE_LOCATION (c),
8541 "%<detach%> clause must not be used together with "
8542 "%<mergeable%> clause");
8543 *pc = OMP_CLAUSE_CHAIN (c);
8544 continue;
8546 pc = &OMP_CLAUSE_CHAIN (c);
8547 continue;
8548 case OMP_CLAUSE_MAP:
8549 if (target_in_reduction_seen && !processing_template_decl)
8551 t = OMP_CLAUSE_DECL (c);
8552 while (handled_component_p (t)
8553 || TREE_CODE (t) == INDIRECT_REF
8554 || TREE_CODE (t) == ADDR_EXPR
8555 || TREE_CODE (t) == MEM_REF
8556 || TREE_CODE (t) == NON_LVALUE_EXPR)
8557 t = TREE_OPERAND (t, 0);
8558 if (DECL_P (t)
8559 && bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
8560 OMP_CLAUSE_MAP_IN_REDUCTION (c) = 1;
8562 pc = &OMP_CLAUSE_CHAIN (c);
8563 continue;
8564 case OMP_CLAUSE_NOWAIT:
8565 if (copyprivate_seen)
8567 error_at (OMP_CLAUSE_LOCATION (c),
8568 "%<nowait%> clause must not be used together "
8569 "with %<copyprivate%>");
8570 *pc = OMP_CLAUSE_CHAIN (c);
8571 continue;
8573 /* FALLTHRU */
8574 default:
8575 pc = &OMP_CLAUSE_CHAIN (c);
8576 continue;
8579 t = OMP_CLAUSE_DECL (c);
8580 switch (c_kind)
8582 case OMP_CLAUSE_LASTPRIVATE:
8583 if (DECL_P (t)
8584 && !bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
8586 need_default_ctor = true;
8587 need_dtor = true;
8589 break;
8591 case OMP_CLAUSE_REDUCTION:
8592 case OMP_CLAUSE_IN_REDUCTION:
8593 case OMP_CLAUSE_TASK_REDUCTION:
8594 if (allocate_seen)
8596 if (TREE_CODE (t) == MEM_REF)
8598 t = TREE_OPERAND (t, 0);
8599 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
8600 t = TREE_OPERAND (t, 0);
8601 if (TREE_CODE (t) == ADDR_EXPR
8602 || TREE_CODE (t) == INDIRECT_REF)
8603 t = TREE_OPERAND (t, 0);
8604 if (DECL_P (t))
8605 bitmap_clear_bit (&aligned_head, DECL_UID (t));
8607 else if (TREE_CODE (t) == TREE_LIST)
8609 while (TREE_CODE (t) == TREE_LIST)
8610 t = TREE_CHAIN (t);
8611 if (DECL_P (t))
8612 bitmap_clear_bit (&aligned_head, DECL_UID (t));
8613 t = OMP_CLAUSE_DECL (c);
8615 else if (DECL_P (t))
8616 bitmap_clear_bit (&aligned_head, DECL_UID (t));
8617 t = OMP_CLAUSE_DECL (c);
8619 if (processing_template_decl
8620 && !VAR_P (t) && TREE_CODE (t) != PARM_DECL)
8621 break;
8622 if (finish_omp_reduction_clause (c, &need_default_ctor,
8623 &need_dtor))
8624 remove = true;
8625 else
8626 t = OMP_CLAUSE_DECL (c);
8627 break;
8629 case OMP_CLAUSE_COPYIN:
8630 if (processing_template_decl
8631 && !VAR_P (t) && TREE_CODE (t) != PARM_DECL)
8632 break;
8633 if (!VAR_P (t) || !CP_DECL_THREAD_LOCAL_P (t))
8635 error_at (OMP_CLAUSE_LOCATION (c),
8636 "%qE must be %<threadprivate%> for %<copyin%>", t);
8637 remove = true;
8639 break;
8641 default:
8642 break;
8645 if (processing_template_decl
8646 && !VAR_P (t) && TREE_CODE (t) != PARM_DECL)
8648 pc = &OMP_CLAUSE_CHAIN (c);
8649 continue;
8652 if (need_complete_type || need_copy_assignment)
8654 t = require_complete_type (t);
8655 if (t == error_mark_node)
8656 remove = true;
8657 else if (!processing_template_decl
8658 && TYPE_REF_P (TREE_TYPE (t))
8659 && !complete_type_or_else (TREE_TYPE (TREE_TYPE (t)), t))
8660 remove = true;
8662 if (need_implicitly_determined)
8664 const char *share_name = NULL;
8666 if (allocate_seen
8667 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_SHARED
8668 && DECL_P (t))
8669 bitmap_clear_bit (&aligned_head, DECL_UID (t));
8671 if (VAR_P (t) && CP_DECL_THREAD_LOCAL_P (t))
8672 share_name = "threadprivate";
8673 else switch (cxx_omp_predetermined_sharing_1 (t))
8675 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
8676 break;
8677 case OMP_CLAUSE_DEFAULT_SHARED:
8678 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
8679 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE)
8680 && c_omp_predefined_variable (t))
8681 /* The __func__ variable and similar function-local predefined
8682 variables may be listed in a shared or firstprivate
8683 clause. */
8684 break;
8685 if (VAR_P (t)
8686 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
8687 && TREE_STATIC (t)
8688 && cxx_omp_const_qual_no_mutable (t))
8690 tree ctx = CP_DECL_CONTEXT (t);
8691 /* const qualified static data members without mutable
8692 member may be specified in firstprivate clause. */
8693 if (TYPE_P (ctx) && MAYBE_CLASS_TYPE_P (ctx))
8694 break;
8696 share_name = "shared";
8697 break;
8698 case OMP_CLAUSE_DEFAULT_PRIVATE:
8699 share_name = "private";
8700 break;
8701 default:
8702 gcc_unreachable ();
8704 if (share_name)
8706 error_at (OMP_CLAUSE_LOCATION (c),
8707 "%qE is predetermined %qs for %qs",
8708 omp_clause_printable_decl (t), share_name,
8709 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
8710 remove = true;
8712 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_SHARED
8713 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_FIRSTPRIVATE
8714 && cxx_omp_const_qual_no_mutable (t))
8716 error_at (OMP_CLAUSE_LOCATION (c),
8717 "%<const%> qualified %qE without %<mutable%> member "
8718 "may appear only in %<shared%> or %<firstprivate%> "
8719 "clauses", omp_clause_printable_decl (t));
8720 remove = true;
8724 if (detach_seen
8725 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
8726 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
8727 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
8728 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
8729 && OMP_CLAUSE_DECL (c) == OMP_CLAUSE_DECL (detach_seen))
8731 error_at (OMP_CLAUSE_LOCATION (c),
8732 "the event handle of a %<detach%> clause "
8733 "should not be in a data-sharing clause");
8734 remove = true;
8737 /* We're interested in the base element, not arrays. */
8738 inner_type = type = TREE_TYPE (t);
8739 if ((need_complete_type
8740 || need_copy_assignment
8741 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
8742 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
8743 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
8744 && TYPE_REF_P (inner_type))
8745 inner_type = TREE_TYPE (inner_type);
8746 while (TREE_CODE (inner_type) == ARRAY_TYPE)
8747 inner_type = TREE_TYPE (inner_type);
8749 /* Check for special function availability by building a call to one.
8750 Save the results, because later we won't be in the right context
8751 for making these queries. */
8752 if (CLASS_TYPE_P (inner_type)
8753 && COMPLETE_TYPE_P (inner_type)
8754 && (need_default_ctor || need_copy_ctor
8755 || need_copy_assignment || need_dtor)
8756 && !type_dependent_expression_p (t)
8757 && cxx_omp_create_clause_info (c, inner_type, need_default_ctor,
8758 need_copy_ctor, need_copy_assignment,
8759 need_dtor))
8760 remove = true;
8762 if (!remove
8763 && c_kind == OMP_CLAUSE_SHARED
8764 && processing_template_decl)
8766 t = omp_clause_decl_field (OMP_CLAUSE_DECL (c));
8767 if (t)
8768 OMP_CLAUSE_DECL (c) = t;
8771 if (remove)
8772 *pc = OMP_CLAUSE_CHAIN (c);
8773 else
8774 pc = &OMP_CLAUSE_CHAIN (c);
8777 if (allocate_seen)
8778 for (pc = &clauses, c = clauses; c ; c = *pc)
8780 bool remove = false;
8781 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ALLOCATE
8782 && !OMP_CLAUSE_ALLOCATE_COMBINED (c)
8783 && DECL_P (OMP_CLAUSE_DECL (c))
8784 && bitmap_bit_p (&aligned_head, DECL_UID (OMP_CLAUSE_DECL (c))))
8786 error_at (OMP_CLAUSE_LOCATION (c),
8787 "%qD specified in %<allocate%> clause but not in "
8788 "an explicit privatization clause", OMP_CLAUSE_DECL (c));
8789 remove = true;
8791 if (remove)
8792 *pc = OMP_CLAUSE_CHAIN (c);
8793 else
8794 pc = &OMP_CLAUSE_CHAIN (c);
8797 bitmap_obstack_release (NULL);
8798 return clauses;
8801 /* Start processing OpenMP clauses that can include any
8802 privatization clauses for non-static data members. */
8804 tree
8805 push_omp_privatization_clauses (bool ignore_next)
8807 if (omp_private_member_ignore_next)
8809 omp_private_member_ignore_next = ignore_next;
8810 return NULL_TREE;
8812 omp_private_member_ignore_next = ignore_next;
8813 if (omp_private_member_map)
8814 omp_private_member_vec.safe_push (error_mark_node);
8815 return push_stmt_list ();
8818 /* Revert remapping of any non-static data members since
8819 the last push_omp_privatization_clauses () call. */
8821 void
8822 pop_omp_privatization_clauses (tree stmt)
8824 if (stmt == NULL_TREE)
8825 return;
8826 stmt = pop_stmt_list (stmt);
8827 if (omp_private_member_map)
8829 while (!omp_private_member_vec.is_empty ())
8831 tree t = omp_private_member_vec.pop ();
8832 if (t == error_mark_node)
8834 add_stmt (stmt);
8835 return;
8837 bool no_decl_expr = t == integer_zero_node;
8838 if (no_decl_expr)
8839 t = omp_private_member_vec.pop ();
8840 tree *v = omp_private_member_map->get (t);
8841 gcc_assert (v);
8842 if (!no_decl_expr)
8843 add_decl_expr (*v);
8844 omp_private_member_map->remove (t);
8846 delete omp_private_member_map;
8847 omp_private_member_map = NULL;
8849 add_stmt (stmt);
8852 /* Remember OpenMP privatization clauses mapping and clear it.
8853 Used for lambdas. */
8855 void
8856 save_omp_privatization_clauses (vec<tree> &save)
8858 save = vNULL;
8859 if (omp_private_member_ignore_next)
8860 save.safe_push (integer_one_node);
8861 omp_private_member_ignore_next = false;
8862 if (!omp_private_member_map)
8863 return;
8865 while (!omp_private_member_vec.is_empty ())
8867 tree t = omp_private_member_vec.pop ();
8868 if (t == error_mark_node)
8870 save.safe_push (t);
8871 continue;
8873 tree n = t;
8874 if (t == integer_zero_node)
8875 t = omp_private_member_vec.pop ();
8876 tree *v = omp_private_member_map->get (t);
8877 gcc_assert (v);
8878 save.safe_push (*v);
8879 save.safe_push (t);
8880 if (n != t)
8881 save.safe_push (n);
8883 delete omp_private_member_map;
8884 omp_private_member_map = NULL;
8887 /* Restore OpenMP privatization clauses mapping saved by the
8888 above function. */
8890 void
8891 restore_omp_privatization_clauses (vec<tree> &save)
8893 gcc_assert (omp_private_member_vec.is_empty ());
8894 omp_private_member_ignore_next = false;
8895 if (save.is_empty ())
8896 return;
8897 if (save.length () == 1 && save[0] == integer_one_node)
8899 omp_private_member_ignore_next = true;
8900 save.release ();
8901 return;
8904 omp_private_member_map = new hash_map <tree, tree>;
8905 while (!save.is_empty ())
8907 tree t = save.pop ();
8908 tree n = t;
8909 if (t != error_mark_node)
8911 if (t == integer_one_node)
8913 omp_private_member_ignore_next = true;
8914 gcc_assert (save.is_empty ());
8915 break;
8917 if (t == integer_zero_node)
8918 t = save.pop ();
8919 tree &v = omp_private_member_map->get_or_insert (t);
8920 v = save.pop ();
8922 omp_private_member_vec.safe_push (t);
8923 if (n != t)
8924 omp_private_member_vec.safe_push (n);
8926 save.release ();
8929 /* For all variables in the tree_list VARS, mark them as thread local. */
8931 void
8932 finish_omp_threadprivate (tree vars)
8934 tree t;
8936 /* Mark every variable in VARS to be assigned thread local storage. */
8937 for (t = vars; t; t = TREE_CHAIN (t))
8939 tree v = TREE_PURPOSE (t);
8941 if (error_operand_p (v))
8943 else if (!VAR_P (v))
8944 error ("%<threadprivate%> %qD is not file, namespace "
8945 "or block scope variable", v);
8946 /* If V had already been marked threadprivate, it doesn't matter
8947 whether it had been used prior to this point. */
8948 else if (TREE_USED (v)
8949 && (DECL_LANG_SPECIFIC (v) == NULL
8950 || !CP_DECL_THREADPRIVATE_P (v)))
8951 error ("%qE declared %<threadprivate%> after first use", v);
8952 else if (! TREE_STATIC (v) && ! DECL_EXTERNAL (v))
8953 error ("automatic variable %qE cannot be %<threadprivate%>", v);
8954 else if (! COMPLETE_TYPE_P (complete_type (TREE_TYPE (v))))
8955 error ("%<threadprivate%> %qE has incomplete type", v);
8956 else if (TREE_STATIC (v) && TYPE_P (CP_DECL_CONTEXT (v))
8957 && CP_DECL_CONTEXT (v) != current_class_type)
8958 error ("%<threadprivate%> %qE directive not "
8959 "in %qT definition", v, CP_DECL_CONTEXT (v));
8960 else
8962 /* Allocate a LANG_SPECIFIC structure for V, if needed. */
8963 if (DECL_LANG_SPECIFIC (v) == NULL)
8964 retrofit_lang_decl (v);
8966 if (! CP_DECL_THREAD_LOCAL_P (v))
8968 CP_DECL_THREAD_LOCAL_P (v) = true;
8969 set_decl_tls_model (v, decl_default_tls_model (v));
8970 /* If rtl has been already set for this var, call
8971 make_decl_rtl once again, so that encode_section_info
8972 has a chance to look at the new decl flags. */
8973 if (DECL_RTL_SET_P (v))
8974 make_decl_rtl (v);
8976 CP_DECL_THREADPRIVATE_P (v) = 1;
8981 /* Build an OpenMP structured block. */
8983 tree
8984 begin_omp_structured_block (void)
8986 return do_pushlevel (sk_omp);
8989 tree
8990 finish_omp_structured_block (tree block)
8992 return do_poplevel (block);
8995 /* Similarly, except force the retention of the BLOCK. */
8997 tree
8998 begin_omp_parallel (void)
9000 keep_next_level (true);
9001 return begin_omp_structured_block ();
9004 /* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
9005 statement. */
9007 tree
9008 finish_oacc_data (tree clauses, tree block)
9010 tree stmt;
9012 block = finish_omp_structured_block (block);
9014 stmt = make_node (OACC_DATA);
9015 TREE_TYPE (stmt) = void_type_node;
9016 OACC_DATA_CLAUSES (stmt) = clauses;
9017 OACC_DATA_BODY (stmt) = block;
9019 return add_stmt (stmt);
9022 /* Generate OACC_HOST_DATA, with CLAUSES and BLOCK as its compound
9023 statement. */
9025 tree
9026 finish_oacc_host_data (tree clauses, tree block)
9028 tree stmt;
9030 block = finish_omp_structured_block (block);
9032 stmt = make_node (OACC_HOST_DATA);
9033 TREE_TYPE (stmt) = void_type_node;
9034 OACC_HOST_DATA_CLAUSES (stmt) = clauses;
9035 OACC_HOST_DATA_BODY (stmt) = block;
9037 return add_stmt (stmt);
9040 /* Generate OMP construct CODE, with BODY and CLAUSES as its compound
9041 statement. */
9043 tree
9044 finish_omp_construct (enum tree_code code, tree body, tree clauses)
9046 body = finish_omp_structured_block (body);
9048 tree stmt = make_node (code);
9049 TREE_TYPE (stmt) = void_type_node;
9050 OMP_BODY (stmt) = body;
9051 OMP_CLAUSES (stmt) = clauses;
9053 return add_stmt (stmt);
9056 tree
9057 finish_omp_parallel (tree clauses, tree body)
9059 tree stmt;
9061 body = finish_omp_structured_block (body);
9063 stmt = make_node (OMP_PARALLEL);
9064 TREE_TYPE (stmt) = void_type_node;
9065 OMP_PARALLEL_CLAUSES (stmt) = clauses;
9066 OMP_PARALLEL_BODY (stmt) = body;
9068 return add_stmt (stmt);
9071 tree
9072 begin_omp_task (void)
9074 keep_next_level (true);
9075 return begin_omp_structured_block ();
9078 tree
9079 finish_omp_task (tree clauses, tree body)
9081 tree stmt;
9083 body = finish_omp_structured_block (body);
9085 stmt = make_node (OMP_TASK);
9086 TREE_TYPE (stmt) = void_type_node;
9087 OMP_TASK_CLAUSES (stmt) = clauses;
9088 OMP_TASK_BODY (stmt) = body;
9090 return add_stmt (stmt);
9093 /* Helper function for finish_omp_for. Convert Ith random access iterator
9094 into integral iterator. Return FALSE if successful. */
9096 static bool
9097 handle_omp_for_class_iterator (int i, location_t locus, enum tree_code code,
9098 tree declv, tree orig_declv, tree initv,
9099 tree condv, tree incrv, tree *body,
9100 tree *pre_body, tree &clauses,
9101 int collapse, int ordered)
9103 tree diff, iter_init, iter_incr = NULL, last;
9104 tree incr_var = NULL, orig_pre_body, orig_body, c;
9105 tree decl = TREE_VEC_ELT (declv, i);
9106 tree init = TREE_VEC_ELT (initv, i);
9107 tree cond = TREE_VEC_ELT (condv, i);
9108 tree incr = TREE_VEC_ELT (incrv, i);
9109 tree iter = decl;
9110 location_t elocus = locus;
9112 if (init && EXPR_HAS_LOCATION (init))
9113 elocus = EXPR_LOCATION (init);
9115 switch (TREE_CODE (cond))
9117 case GT_EXPR:
9118 case GE_EXPR:
9119 case LT_EXPR:
9120 case LE_EXPR:
9121 case NE_EXPR:
9122 if (TREE_OPERAND (cond, 1) == iter)
9123 cond = build2 (swap_tree_comparison (TREE_CODE (cond)),
9124 TREE_TYPE (cond), iter, TREE_OPERAND (cond, 0));
9125 if (TREE_OPERAND (cond, 0) != iter)
9126 cond = error_mark_node;
9127 else
9129 tree tem = build_x_binary_op (EXPR_LOCATION (cond),
9130 TREE_CODE (cond),
9131 iter, ERROR_MARK,
9132 TREE_OPERAND (cond, 1), ERROR_MARK,
9133 NULL, tf_warning_or_error);
9134 if (error_operand_p (tem))
9135 return true;
9137 break;
9138 default:
9139 cond = error_mark_node;
9140 break;
9142 if (cond == error_mark_node)
9144 error_at (elocus, "invalid controlling predicate");
9145 return true;
9147 diff = build_x_binary_op (elocus, MINUS_EXPR, TREE_OPERAND (cond, 1),
9148 ERROR_MARK, iter, ERROR_MARK, NULL,
9149 tf_warning_or_error);
9150 diff = cp_fully_fold (diff);
9151 if (error_operand_p (diff))
9152 return true;
9153 if (TREE_CODE (TREE_TYPE (diff)) != INTEGER_TYPE)
9155 error_at (elocus, "difference between %qE and %qD does not have integer type",
9156 TREE_OPERAND (cond, 1), iter);
9157 return true;
9159 if (!c_omp_check_loop_iv_exprs (locus, orig_declv, i,
9160 TREE_VEC_ELT (declv, i), NULL_TREE,
9161 cond, cp_walk_subtrees))
9162 return true;
9164 switch (TREE_CODE (incr))
9166 case PREINCREMENT_EXPR:
9167 case PREDECREMENT_EXPR:
9168 case POSTINCREMENT_EXPR:
9169 case POSTDECREMENT_EXPR:
9170 if (TREE_OPERAND (incr, 0) != iter)
9172 incr = error_mark_node;
9173 break;
9175 iter_incr = build_x_unary_op (EXPR_LOCATION (incr),
9176 TREE_CODE (incr), iter,
9177 tf_warning_or_error);
9178 if (error_operand_p (iter_incr))
9179 return true;
9180 else if (TREE_CODE (incr) == PREINCREMENT_EXPR
9181 || TREE_CODE (incr) == POSTINCREMENT_EXPR)
9182 incr = integer_one_node;
9183 else
9184 incr = integer_minus_one_node;
9185 break;
9186 case MODIFY_EXPR:
9187 if (TREE_OPERAND (incr, 0) != iter)
9188 incr = error_mark_node;
9189 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
9190 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
9192 tree rhs = TREE_OPERAND (incr, 1);
9193 if (TREE_OPERAND (rhs, 0) == iter)
9195 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (rhs, 1)))
9196 != INTEGER_TYPE)
9197 incr = error_mark_node;
9198 else
9200 iter_incr = build_x_modify_expr (EXPR_LOCATION (rhs),
9201 iter, TREE_CODE (rhs),
9202 TREE_OPERAND (rhs, 1),
9203 tf_warning_or_error);
9204 if (error_operand_p (iter_incr))
9205 return true;
9206 incr = TREE_OPERAND (rhs, 1);
9207 incr = cp_convert (TREE_TYPE (diff), incr,
9208 tf_warning_or_error);
9209 if (TREE_CODE (rhs) == MINUS_EXPR)
9211 incr = build1 (NEGATE_EXPR, TREE_TYPE (diff), incr);
9212 incr = fold_simple (incr);
9214 if (TREE_CODE (incr) != INTEGER_CST
9215 && (TREE_CODE (incr) != NOP_EXPR
9216 || (TREE_CODE (TREE_OPERAND (incr, 0))
9217 != INTEGER_CST)))
9218 iter_incr = NULL;
9221 else if (TREE_OPERAND (rhs, 1) == iter)
9223 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (rhs, 0))) != INTEGER_TYPE
9224 || TREE_CODE (rhs) != PLUS_EXPR)
9225 incr = error_mark_node;
9226 else
9228 iter_incr = build_x_binary_op (EXPR_LOCATION (rhs),
9229 PLUS_EXPR,
9230 TREE_OPERAND (rhs, 0),
9231 ERROR_MARK, iter,
9232 ERROR_MARK, NULL,
9233 tf_warning_or_error);
9234 if (error_operand_p (iter_incr))
9235 return true;
9236 iter_incr = build_x_modify_expr (EXPR_LOCATION (rhs),
9237 iter, NOP_EXPR,
9238 iter_incr,
9239 tf_warning_or_error);
9240 if (error_operand_p (iter_incr))
9241 return true;
9242 incr = TREE_OPERAND (rhs, 0);
9243 iter_incr = NULL;
9246 else
9247 incr = error_mark_node;
9249 else
9250 incr = error_mark_node;
9251 break;
9252 default:
9253 incr = error_mark_node;
9254 break;
9257 if (incr == error_mark_node)
9259 error_at (elocus, "invalid increment expression");
9260 return true;
9263 incr = cp_convert (TREE_TYPE (diff), incr, tf_warning_or_error);
9264 incr = cp_fully_fold (incr);
9265 tree loop_iv_seen = NULL_TREE;
9266 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
9267 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
9268 && OMP_CLAUSE_DECL (c) == iter)
9270 if (code == OMP_TASKLOOP || code == OMP_LOOP)
9272 loop_iv_seen = c;
9273 OMP_CLAUSE_LASTPRIVATE_LOOP_IV (c) = 1;
9275 break;
9277 else if ((code == OMP_TASKLOOP || code == OMP_LOOP)
9278 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
9279 && OMP_CLAUSE_DECL (c) == iter)
9281 loop_iv_seen = c;
9282 if (code == OMP_TASKLOOP)
9283 OMP_CLAUSE_PRIVATE_TASKLOOP_IV (c) = 1;
9286 decl = create_temporary_var (TREE_TYPE (diff));
9287 pushdecl (decl);
9288 add_decl_expr (decl);
9289 last = create_temporary_var (TREE_TYPE (diff));
9290 pushdecl (last);
9291 add_decl_expr (last);
9292 if (c && iter_incr == NULL && TREE_CODE (incr) != INTEGER_CST
9293 && (!ordered || (i < collapse && collapse > 1)))
9295 incr_var = create_temporary_var (TREE_TYPE (diff));
9296 pushdecl (incr_var);
9297 add_decl_expr (incr_var);
9299 gcc_assert (stmts_are_full_exprs_p ());
9300 tree diffvar = NULL_TREE;
9301 if (code == OMP_TASKLOOP)
9303 if (!loop_iv_seen)
9305 tree ivc = build_omp_clause (locus, OMP_CLAUSE_FIRSTPRIVATE);
9306 OMP_CLAUSE_DECL (ivc) = iter;
9307 cxx_omp_finish_clause (ivc, NULL, false);
9308 OMP_CLAUSE_CHAIN (ivc) = clauses;
9309 clauses = ivc;
9311 tree lvc = build_omp_clause (locus, OMP_CLAUSE_FIRSTPRIVATE);
9312 OMP_CLAUSE_DECL (lvc) = last;
9313 OMP_CLAUSE_CHAIN (lvc) = clauses;
9314 clauses = lvc;
9315 diffvar = create_temporary_var (TREE_TYPE (diff));
9316 pushdecl (diffvar);
9317 add_decl_expr (diffvar);
9319 else if (code == OMP_LOOP)
9321 if (!loop_iv_seen)
9323 /* While iterators on the loop construct are predetermined
9324 lastprivate, if the decl is not declared inside of the
9325 loop, OMP_CLAUSE_LASTPRIVATE should have been added
9326 already. */
9327 loop_iv_seen = build_omp_clause (locus, OMP_CLAUSE_FIRSTPRIVATE);
9328 OMP_CLAUSE_DECL (loop_iv_seen) = iter;
9329 OMP_CLAUSE_CHAIN (loop_iv_seen) = clauses;
9330 clauses = loop_iv_seen;
9332 else if (OMP_CLAUSE_CODE (loop_iv_seen) == OMP_CLAUSE_PRIVATE)
9334 OMP_CLAUSE_PRIVATE_DEBUG (loop_iv_seen) = 0;
9335 OMP_CLAUSE_PRIVATE_OUTER_REF (loop_iv_seen) = 0;
9336 OMP_CLAUSE_CODE (loop_iv_seen) = OMP_CLAUSE_FIRSTPRIVATE;
9338 if (OMP_CLAUSE_CODE (loop_iv_seen) == OMP_CLAUSE_FIRSTPRIVATE)
9339 cxx_omp_finish_clause (loop_iv_seen, NULL, false);
9342 orig_pre_body = *pre_body;
9343 *pre_body = push_stmt_list ();
9344 if (orig_pre_body)
9345 add_stmt (orig_pre_body);
9346 if (init != NULL)
9347 finish_expr_stmt (build_x_modify_expr (elocus,
9348 iter, NOP_EXPR, init,
9349 tf_warning_or_error));
9350 init = build_int_cst (TREE_TYPE (diff), 0);
9351 if (c && iter_incr == NULL
9352 && (!ordered || (i < collapse && collapse > 1)))
9354 if (incr_var)
9356 finish_expr_stmt (build_x_modify_expr (elocus,
9357 incr_var, NOP_EXPR,
9358 incr, tf_warning_or_error));
9359 incr = incr_var;
9361 iter_incr = build_x_modify_expr (elocus,
9362 iter, PLUS_EXPR, incr,
9363 tf_warning_or_error);
9365 if (c && ordered && i < collapse && collapse > 1)
9366 iter_incr = incr;
9367 finish_expr_stmt (build_x_modify_expr (elocus,
9368 last, NOP_EXPR, init,
9369 tf_warning_or_error));
9370 if (diffvar)
9372 finish_expr_stmt (build_x_modify_expr (elocus,
9373 diffvar, NOP_EXPR,
9374 diff, tf_warning_or_error));
9375 diff = diffvar;
9377 *pre_body = pop_stmt_list (*pre_body);
9379 cond = cp_build_binary_op (elocus,
9380 TREE_CODE (cond), decl, diff,
9381 tf_warning_or_error);
9382 incr = build_modify_expr (elocus, decl, NULL_TREE, PLUS_EXPR,
9383 elocus, incr, NULL_TREE);
9385 orig_body = *body;
9386 *body = push_stmt_list ();
9387 iter_init = build2 (MINUS_EXPR, TREE_TYPE (diff), decl, last);
9388 iter_init = build_x_modify_expr (elocus,
9389 iter, PLUS_EXPR, iter_init,
9390 tf_warning_or_error);
9391 if (iter_init != error_mark_node)
9392 iter_init = build1 (NOP_EXPR, void_type_node, iter_init);
9393 finish_expr_stmt (iter_init);
9394 finish_expr_stmt (build_x_modify_expr (elocus,
9395 last, NOP_EXPR, decl,
9396 tf_warning_or_error));
9397 add_stmt (orig_body);
9398 *body = pop_stmt_list (*body);
9400 if (c)
9402 OMP_CLAUSE_LASTPRIVATE_STMT (c) = push_stmt_list ();
9403 if (!ordered)
9404 finish_expr_stmt (iter_incr);
9405 else
9407 iter_init = decl;
9408 if (i < collapse && collapse > 1 && !error_operand_p (iter_incr))
9409 iter_init = build2 (PLUS_EXPR, TREE_TYPE (diff),
9410 iter_init, iter_incr);
9411 iter_init = build2 (MINUS_EXPR, TREE_TYPE (diff), iter_init, last);
9412 iter_init = build_x_modify_expr (elocus,
9413 iter, PLUS_EXPR, iter_init,
9414 tf_warning_or_error);
9415 if (iter_init != error_mark_node)
9416 iter_init = build1 (NOP_EXPR, void_type_node, iter_init);
9417 finish_expr_stmt (iter_init);
9419 OMP_CLAUSE_LASTPRIVATE_STMT (c)
9420 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (c));
9423 if (TREE_CODE (TREE_VEC_ELT (orig_declv, i)) == TREE_LIST)
9425 tree t = TREE_VEC_ELT (orig_declv, i);
9426 gcc_assert (TREE_PURPOSE (t) == NULL_TREE
9427 && TREE_VALUE (t) == NULL_TREE
9428 && TREE_CODE (TREE_CHAIN (t)) == TREE_VEC);
9429 TREE_PURPOSE (t) = TREE_VEC_ELT (declv, i);
9430 TREE_VALUE (t) = last;
9432 else
9433 TREE_VEC_ELT (orig_declv, i)
9434 = tree_cons (TREE_VEC_ELT (declv, i), last, NULL_TREE);
9435 TREE_VEC_ELT (declv, i) = decl;
9436 TREE_VEC_ELT (initv, i) = init;
9437 TREE_VEC_ELT (condv, i) = cond;
9438 TREE_VEC_ELT (incrv, i) = incr;
9440 return false;
9443 /* Build and validate an OMP_FOR statement. CLAUSES, BODY, COND, INCR
9444 are directly for their associated operands in the statement. DECL
9445 and INIT are a combo; if DECL is NULL then INIT ought to be a
9446 MODIFY_EXPR, and the DECL should be extracted. PRE_BODY are
9447 optional statements that need to go before the loop into its
9448 sk_omp scope. */
9450 tree
9451 finish_omp_for (location_t locus, enum tree_code code, tree declv,
9452 tree orig_declv, tree initv, tree condv, tree incrv,
9453 tree body, tree pre_body, vec<tree> *orig_inits, tree clauses)
9455 tree omp_for = NULL, orig_incr = NULL;
9456 tree decl = NULL, init, cond, incr;
9457 location_t elocus;
9458 int i;
9459 int collapse = 1;
9460 int ordered = 0;
9462 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (initv));
9463 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (condv));
9464 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (incrv));
9465 if (TREE_VEC_LENGTH (declv) > 1)
9467 tree c;
9469 c = omp_find_clause (clauses, OMP_CLAUSE_TILE);
9470 if (c)
9471 collapse = list_length (OMP_CLAUSE_TILE_LIST (c));
9472 else
9474 c = omp_find_clause (clauses, OMP_CLAUSE_COLLAPSE);
9475 if (c)
9476 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (c));
9477 if (collapse != TREE_VEC_LENGTH (declv))
9478 ordered = TREE_VEC_LENGTH (declv);
9481 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
9483 decl = TREE_VEC_ELT (declv, i);
9484 init = TREE_VEC_ELT (initv, i);
9485 cond = TREE_VEC_ELT (condv, i);
9486 incr = TREE_VEC_ELT (incrv, i);
9487 elocus = locus;
9489 if (decl == NULL)
9491 if (init != NULL)
9492 switch (TREE_CODE (init))
9494 case MODIFY_EXPR:
9495 decl = TREE_OPERAND (init, 0);
9496 init = TREE_OPERAND (init, 1);
9497 break;
9498 case MODOP_EXPR:
9499 if (TREE_CODE (TREE_OPERAND (init, 1)) == NOP_EXPR)
9501 decl = TREE_OPERAND (init, 0);
9502 init = TREE_OPERAND (init, 2);
9504 break;
9505 default:
9506 break;
9509 if (decl == NULL)
9511 error_at (locus,
9512 "expected iteration declaration or initialization");
9513 return NULL;
9517 if (init && EXPR_HAS_LOCATION (init))
9518 elocus = EXPR_LOCATION (init);
9520 if (cond == global_namespace)
9521 continue;
9523 if (cond == NULL)
9525 error_at (elocus, "missing controlling predicate");
9526 return NULL;
9529 if (incr == NULL)
9531 error_at (elocus, "missing increment expression");
9532 return NULL;
9535 TREE_VEC_ELT (declv, i) = decl;
9536 TREE_VEC_ELT (initv, i) = init;
9539 if (orig_inits)
9541 bool fail = false;
9542 tree orig_init;
9543 FOR_EACH_VEC_ELT (*orig_inits, i, orig_init)
9544 if (orig_init
9545 && !c_omp_check_loop_iv_exprs (locus,
9546 orig_declv ? orig_declv : declv, i,
9547 TREE_VEC_ELT (declv, i), orig_init,
9548 NULL_TREE, cp_walk_subtrees))
9549 fail = true;
9550 if (fail)
9551 return NULL;
9554 if (dependent_omp_for_p (declv, initv, condv, incrv))
9556 tree stmt;
9558 stmt = make_node (code);
9560 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
9562 /* This is really just a place-holder. We'll be decomposing this
9563 again and going through the cp_build_modify_expr path below when
9564 we instantiate the thing. */
9565 TREE_VEC_ELT (initv, i)
9566 = build2 (MODIFY_EXPR, void_type_node, TREE_VEC_ELT (declv, i),
9567 TREE_VEC_ELT (initv, i));
9570 TREE_TYPE (stmt) = void_type_node;
9571 OMP_FOR_INIT (stmt) = initv;
9572 OMP_FOR_COND (stmt) = condv;
9573 OMP_FOR_INCR (stmt) = incrv;
9574 OMP_FOR_BODY (stmt) = body;
9575 OMP_FOR_PRE_BODY (stmt) = pre_body;
9576 OMP_FOR_CLAUSES (stmt) = clauses;
9578 SET_EXPR_LOCATION (stmt, locus);
9579 return add_stmt (stmt);
9582 if (!orig_declv)
9583 orig_declv = copy_node (declv);
9585 if (processing_template_decl)
9586 orig_incr = make_tree_vec (TREE_VEC_LENGTH (incrv));
9588 for (i = 0; i < TREE_VEC_LENGTH (declv); )
9590 decl = TREE_VEC_ELT (declv, i);
9591 init = TREE_VEC_ELT (initv, i);
9592 cond = TREE_VEC_ELT (condv, i);
9593 incr = TREE_VEC_ELT (incrv, i);
9594 if (orig_incr)
9595 TREE_VEC_ELT (orig_incr, i) = incr;
9596 elocus = locus;
9598 if (init && EXPR_HAS_LOCATION (init))
9599 elocus = EXPR_LOCATION (init);
9601 if (!DECL_P (decl))
9603 error_at (elocus, "expected iteration declaration or initialization");
9604 return NULL;
9607 if (incr && TREE_CODE (incr) == MODOP_EXPR)
9609 if (orig_incr)
9610 TREE_VEC_ELT (orig_incr, i) = incr;
9611 incr = cp_build_modify_expr (elocus, TREE_OPERAND (incr, 0),
9612 TREE_CODE (TREE_OPERAND (incr, 1)),
9613 TREE_OPERAND (incr, 2),
9614 tf_warning_or_error);
9617 if (CLASS_TYPE_P (TREE_TYPE (decl)))
9619 if (code == OMP_SIMD)
9621 error_at (elocus, "%<#pragma omp simd%> used with class "
9622 "iteration variable %qE", decl);
9623 return NULL;
9625 if (handle_omp_for_class_iterator (i, locus, code, declv, orig_declv,
9626 initv, condv, incrv, &body,
9627 &pre_body, clauses,
9628 collapse, ordered))
9629 return NULL;
9630 continue;
9633 if (!INTEGRAL_TYPE_P (TREE_TYPE (decl))
9634 && !TYPE_PTR_P (TREE_TYPE (decl)))
9636 error_at (elocus, "invalid type for iteration variable %qE", decl);
9637 return NULL;
9640 if (!processing_template_decl && TREE_CODE (init) != TREE_VEC)
9641 init = cp_build_modify_expr (elocus, decl, NOP_EXPR, init,
9642 tf_warning_or_error);
9643 else
9644 init = build2 (MODIFY_EXPR, void_type_node, decl, init);
9645 if (decl == error_mark_node || init == error_mark_node)
9646 return NULL;
9648 TREE_VEC_ELT (declv, i) = decl;
9649 TREE_VEC_ELT (initv, i) = init;
9650 TREE_VEC_ELT (condv, i) = cond;
9651 TREE_VEC_ELT (incrv, i) = incr;
9652 i++;
9655 if (pre_body && IS_EMPTY_STMT (pre_body))
9656 pre_body = NULL;
9658 omp_for = c_finish_omp_for (locus, code, declv, orig_declv, initv, condv,
9659 incrv, body, pre_body,
9660 !processing_template_decl);
9662 /* Check for iterators appearing in lb, b or incr expressions. */
9663 if (omp_for && !c_omp_check_loop_iv (omp_for, orig_declv, cp_walk_subtrees))
9664 omp_for = NULL_TREE;
9666 if (omp_for == NULL)
9667 return NULL;
9669 add_stmt (omp_for);
9671 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INCR (omp_for)); i++)
9673 init = TREE_VEC_ELT (OMP_FOR_INIT (omp_for), i);
9674 decl = TREE_OPERAND (init, 0);
9675 cond = TREE_VEC_ELT (OMP_FOR_COND (omp_for), i);
9676 incr = TREE_VEC_ELT (OMP_FOR_INCR (omp_for), i);
9678 if (!processing_template_decl)
9680 if (TREE_CODE (TREE_OPERAND (init, 1)) == TREE_VEC)
9682 tree t = TREE_VEC_ELT (TREE_OPERAND (init, 1), 1);
9683 TREE_VEC_ELT (TREE_OPERAND (init, 1), 1)
9684 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
9685 t = TREE_VEC_ELT (TREE_OPERAND (init, 1), 2);
9686 TREE_VEC_ELT (TREE_OPERAND (init, 1), 2)
9687 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
9689 else
9691 tree t = TREE_OPERAND (init, 1);
9692 TREE_OPERAND (init, 1)
9693 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
9695 if (TREE_CODE (TREE_OPERAND (cond, 1)) == TREE_VEC)
9697 tree t = TREE_VEC_ELT (TREE_OPERAND (cond, 1), 1);
9698 TREE_VEC_ELT (TREE_OPERAND (cond, 1), 1)
9699 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
9700 t = TREE_VEC_ELT (TREE_OPERAND (cond, 1), 2);
9701 TREE_VEC_ELT (TREE_OPERAND (cond, 1), 2)
9702 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
9704 else
9706 tree t = TREE_OPERAND (cond, 1);
9707 TREE_OPERAND (cond, 1)
9708 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
9712 if (TREE_CODE (incr) != MODIFY_EXPR)
9713 continue;
9715 if (TREE_SIDE_EFFECTS (TREE_OPERAND (incr, 1))
9716 && BINARY_CLASS_P (TREE_OPERAND (incr, 1))
9717 && !processing_template_decl)
9719 tree t = TREE_OPERAND (TREE_OPERAND (incr, 1), 0);
9720 if (TREE_SIDE_EFFECTS (t)
9721 && t != decl
9722 && (TREE_CODE (t) != NOP_EXPR
9723 || TREE_OPERAND (t, 0) != decl))
9724 TREE_OPERAND (TREE_OPERAND (incr, 1), 0)
9725 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
9727 t = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
9728 if (TREE_SIDE_EFFECTS (t)
9729 && t != decl
9730 && (TREE_CODE (t) != NOP_EXPR
9731 || TREE_OPERAND (t, 0) != decl))
9732 TREE_OPERAND (TREE_OPERAND (incr, 1), 1)
9733 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
9736 if (orig_incr)
9737 TREE_VEC_ELT (OMP_FOR_INCR (omp_for), i) = TREE_VEC_ELT (orig_incr, i);
9739 OMP_FOR_CLAUSES (omp_for) = clauses;
9741 /* For simd loops with non-static data member iterators, we could have added
9742 OMP_CLAUSE_LINEAR clauses without OMP_CLAUSE_LINEAR_STEP. As we know the
9743 step at this point, fill it in. */
9744 if (code == OMP_SIMD && !processing_template_decl
9745 && TREE_VEC_LENGTH (OMP_FOR_INCR (omp_for)) == 1)
9746 for (tree c = omp_find_clause (clauses, OMP_CLAUSE_LINEAR); c;
9747 c = omp_find_clause (OMP_CLAUSE_CHAIN (c), OMP_CLAUSE_LINEAR))
9748 if (OMP_CLAUSE_LINEAR_STEP (c) == NULL_TREE)
9750 decl = TREE_OPERAND (TREE_VEC_ELT (OMP_FOR_INIT (omp_for), 0), 0);
9751 gcc_assert (decl == OMP_CLAUSE_DECL (c));
9752 incr = TREE_VEC_ELT (OMP_FOR_INCR (omp_for), 0);
9753 tree step, stept;
9754 switch (TREE_CODE (incr))
9756 case PREINCREMENT_EXPR:
9757 case POSTINCREMENT_EXPR:
9758 /* c_omp_for_incr_canonicalize_ptr() should have been
9759 called to massage things appropriately. */
9760 gcc_assert (!INDIRECT_TYPE_P (TREE_TYPE (decl)));
9761 OMP_CLAUSE_LINEAR_STEP (c) = build_int_cst (TREE_TYPE (decl), 1);
9762 break;
9763 case PREDECREMENT_EXPR:
9764 case POSTDECREMENT_EXPR:
9765 /* c_omp_for_incr_canonicalize_ptr() should have been
9766 called to massage things appropriately. */
9767 gcc_assert (!INDIRECT_TYPE_P (TREE_TYPE (decl)));
9768 OMP_CLAUSE_LINEAR_STEP (c)
9769 = build_int_cst (TREE_TYPE (decl), -1);
9770 break;
9771 case MODIFY_EXPR:
9772 gcc_assert (TREE_OPERAND (incr, 0) == decl);
9773 incr = TREE_OPERAND (incr, 1);
9774 switch (TREE_CODE (incr))
9776 case PLUS_EXPR:
9777 if (TREE_OPERAND (incr, 1) == decl)
9778 step = TREE_OPERAND (incr, 0);
9779 else
9780 step = TREE_OPERAND (incr, 1);
9781 break;
9782 case MINUS_EXPR:
9783 case POINTER_PLUS_EXPR:
9784 gcc_assert (TREE_OPERAND (incr, 0) == decl);
9785 step = TREE_OPERAND (incr, 1);
9786 break;
9787 default:
9788 gcc_unreachable ();
9790 stept = TREE_TYPE (decl);
9791 if (INDIRECT_TYPE_P (stept))
9792 stept = sizetype;
9793 step = fold_convert (stept, step);
9794 if (TREE_CODE (incr) == MINUS_EXPR)
9795 step = fold_build1 (NEGATE_EXPR, stept, step);
9796 OMP_CLAUSE_LINEAR_STEP (c) = step;
9797 break;
9798 default:
9799 gcc_unreachable ();
9802 /* Override saved methods on OMP_LOOP's OMP_CLAUSE_LASTPRIVATE_LOOP_IV
9803 clauses, we need copy ctor for those rather than default ctor,
9804 plus as for other lastprivates assignment op and dtor. */
9805 if (code == OMP_LOOP && !processing_template_decl)
9806 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
9807 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
9808 && OMP_CLAUSE_LASTPRIVATE_LOOP_IV (c)
9809 && cxx_omp_create_clause_info (c, TREE_TYPE (OMP_CLAUSE_DECL (c)),
9810 false, true, true, true))
9811 CP_OMP_CLAUSE_INFO (c) = NULL_TREE;
9813 return omp_for;
9816 /* Fix up range for decls. Those decls were pushed into BIND's BIND_EXPR_VARS
9817 and need to be moved into the BIND_EXPR inside of the OMP_FOR's body. */
9819 tree
9820 finish_omp_for_block (tree bind, tree omp_for)
9822 if (omp_for == NULL_TREE
9823 || !OMP_FOR_ORIG_DECLS (omp_for)
9824 || bind == NULL_TREE
9825 || TREE_CODE (bind) != BIND_EXPR)
9826 return bind;
9827 tree b = NULL_TREE;
9828 for (int i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (omp_for)); i++)
9829 if (TREE_CODE (TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (omp_for), i)) == TREE_LIST
9830 && TREE_CHAIN (TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (omp_for), i)))
9832 tree v = TREE_CHAIN (TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (omp_for), i));
9833 gcc_assert (BIND_EXPR_BLOCK (bind)
9834 && (BIND_EXPR_VARS (bind)
9835 == BLOCK_VARS (BIND_EXPR_BLOCK (bind))));
9836 for (int j = 2; j < TREE_VEC_LENGTH (v); j++)
9837 for (tree *p = &BIND_EXPR_VARS (bind); *p; p = &DECL_CHAIN (*p))
9839 if (*p == TREE_VEC_ELT (v, j))
9841 tree var = *p;
9842 *p = DECL_CHAIN (*p);
9843 if (b == NULL_TREE)
9845 b = make_node (BLOCK);
9846 b = build3 (BIND_EXPR, void_type_node, NULL_TREE,
9847 OMP_FOR_BODY (omp_for), b);
9848 TREE_SIDE_EFFECTS (b) = 1;
9849 OMP_FOR_BODY (omp_for) = b;
9851 DECL_CHAIN (var) = BIND_EXPR_VARS (b);
9852 BIND_EXPR_VARS (b) = var;
9853 BLOCK_VARS (BIND_EXPR_BLOCK (b)) = var;
9856 BLOCK_VARS (BIND_EXPR_BLOCK (bind)) = BIND_EXPR_VARS (bind);
9858 return bind;
9861 void
9862 finish_omp_atomic (location_t loc, enum tree_code code, enum tree_code opcode,
9863 tree lhs, tree rhs, tree v, tree lhs1, tree rhs1,
9864 tree clauses, enum omp_memory_order mo)
9866 tree orig_lhs;
9867 tree orig_rhs;
9868 tree orig_v;
9869 tree orig_lhs1;
9870 tree orig_rhs1;
9871 bool dependent_p;
9872 tree stmt;
9874 orig_lhs = lhs;
9875 orig_rhs = rhs;
9876 orig_v = v;
9877 orig_lhs1 = lhs1;
9878 orig_rhs1 = rhs1;
9879 dependent_p = false;
9880 stmt = NULL_TREE;
9882 /* Even in a template, we can detect invalid uses of the atomic
9883 pragma if neither LHS nor RHS is type-dependent. */
9884 if (processing_template_decl)
9886 dependent_p = (type_dependent_expression_p (lhs)
9887 || (rhs && type_dependent_expression_p (rhs))
9888 || (v && type_dependent_expression_p (v))
9889 || (lhs1 && type_dependent_expression_p (lhs1))
9890 || (rhs1 && type_dependent_expression_p (rhs1)));
9891 if (clauses)
9893 gcc_assert (TREE_CODE (clauses) == OMP_CLAUSE
9894 && OMP_CLAUSE_CODE (clauses) == OMP_CLAUSE_HINT
9895 && OMP_CLAUSE_CHAIN (clauses) == NULL_TREE);
9896 if (type_dependent_expression_p (OMP_CLAUSE_HINT_EXPR (clauses))
9897 || TREE_CODE (OMP_CLAUSE_HINT_EXPR (clauses)) != INTEGER_CST)
9898 dependent_p = true;
9900 if (!dependent_p)
9902 lhs = build_non_dependent_expr (lhs);
9903 if (rhs)
9904 rhs = build_non_dependent_expr (rhs);
9905 if (v)
9906 v = build_non_dependent_expr (v);
9907 if (lhs1)
9908 lhs1 = build_non_dependent_expr (lhs1);
9909 if (rhs1)
9910 rhs1 = build_non_dependent_expr (rhs1);
9913 if (!dependent_p)
9915 bool swapped = false;
9916 if (rhs1 && cp_tree_equal (lhs, rhs))
9918 std::swap (rhs, rhs1);
9919 swapped = !commutative_tree_code (opcode);
9921 if (rhs1 && !cp_tree_equal (lhs, rhs1))
9923 if (code == OMP_ATOMIC)
9924 error ("%<#pragma omp atomic update%> uses two different "
9925 "expressions for memory");
9926 else
9927 error ("%<#pragma omp atomic capture%> uses two different "
9928 "expressions for memory");
9929 return;
9931 if (lhs1 && !cp_tree_equal (lhs, lhs1))
9933 if (code == OMP_ATOMIC)
9934 error ("%<#pragma omp atomic update%> uses two different "
9935 "expressions for memory");
9936 else
9937 error ("%<#pragma omp atomic capture%> uses two different "
9938 "expressions for memory");
9939 return;
9941 stmt = c_finish_omp_atomic (loc, code, opcode, lhs, rhs,
9942 v, lhs1, rhs1, swapped, mo,
9943 processing_template_decl != 0);
9944 if (stmt == error_mark_node)
9945 return;
9947 if (processing_template_decl)
9949 if (code == OMP_ATOMIC_READ)
9951 stmt = build_min_nt_loc (loc, OMP_ATOMIC_READ, orig_lhs);
9952 OMP_ATOMIC_MEMORY_ORDER (stmt) = mo;
9953 stmt = build2 (MODIFY_EXPR, void_type_node, orig_v, stmt);
9955 else
9957 if (opcode == NOP_EXPR)
9958 stmt = build2 (MODIFY_EXPR, void_type_node, orig_lhs, orig_rhs);
9959 else
9960 stmt = build2 (opcode, void_type_node, orig_lhs, orig_rhs);
9961 if (orig_rhs1)
9962 stmt = build_min_nt_loc (EXPR_LOCATION (orig_rhs1),
9963 COMPOUND_EXPR, orig_rhs1, stmt);
9964 if (code != OMP_ATOMIC)
9966 stmt = build_min_nt_loc (loc, code, orig_lhs1, stmt);
9967 OMP_ATOMIC_MEMORY_ORDER (stmt) = mo;
9968 stmt = build2 (MODIFY_EXPR, void_type_node, orig_v, stmt);
9971 stmt = build2 (OMP_ATOMIC, void_type_node,
9972 clauses ? clauses : integer_zero_node, stmt);
9973 OMP_ATOMIC_MEMORY_ORDER (stmt) = mo;
9974 SET_EXPR_LOCATION (stmt, loc);
9977 /* Avoid -Wunused-value warnings here, the whole construct has side-effects
9978 and even if it might be wrapped from fold-const.c or c-omp.c wrapped
9979 in some tree that appears to be unused, the value is not unused. */
9980 warning_sentinel w (warn_unused_value);
9981 finish_expr_stmt (stmt);
9984 void
9985 finish_omp_barrier (void)
9987 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_BARRIER);
9988 releasing_vec vec;
9989 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
9990 finish_expr_stmt (stmt);
9993 void
9994 finish_omp_depobj (location_t loc, tree depobj,
9995 enum omp_clause_depend_kind kind, tree clause)
9997 if (!error_operand_p (depobj) && !type_dependent_expression_p (depobj))
9999 if (!lvalue_p (depobj))
10001 error_at (EXPR_LOC_OR_LOC (depobj, loc),
10002 "%<depobj%> expression is not lvalue expression");
10003 depobj = error_mark_node;
10007 if (processing_template_decl)
10009 if (clause == NULL_TREE)
10010 clause = build_int_cst (integer_type_node, kind);
10011 add_stmt (build_min_nt_loc (loc, OMP_DEPOBJ, depobj, clause));
10012 return;
10015 if (!error_operand_p (depobj))
10017 tree addr = cp_build_addr_expr (depobj, tf_warning_or_error);
10018 if (addr == error_mark_node)
10019 depobj = error_mark_node;
10020 else
10021 depobj = cp_build_indirect_ref (loc, addr, RO_UNARY_STAR,
10022 tf_warning_or_error);
10025 c_finish_omp_depobj (loc, depobj, kind, clause);
10028 void
10029 finish_omp_flush (int mo)
10031 tree fn = builtin_decl_explicit (BUILT_IN_SYNC_SYNCHRONIZE);
10032 releasing_vec vec;
10033 if (mo != MEMMODEL_LAST)
10035 fn = builtin_decl_explicit (BUILT_IN_ATOMIC_THREAD_FENCE);
10036 vec->quick_push (build_int_cst (integer_type_node, mo));
10038 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
10039 finish_expr_stmt (stmt);
10042 void
10043 finish_omp_taskwait (void)
10045 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_TASKWAIT);
10046 releasing_vec vec;
10047 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
10048 finish_expr_stmt (stmt);
10051 void
10052 finish_omp_taskyield (void)
10054 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_TASKYIELD);
10055 releasing_vec vec;
10056 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
10057 finish_expr_stmt (stmt);
10060 void
10061 finish_omp_cancel (tree clauses)
10063 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
10064 int mask = 0;
10065 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
10066 mask = 1;
10067 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
10068 mask = 2;
10069 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
10070 mask = 4;
10071 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
10072 mask = 8;
10073 else
10075 error ("%<#pragma omp cancel%> must specify one of "
10076 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> clauses");
10077 return;
10079 releasing_vec vec;
10080 tree ifc = omp_find_clause (clauses, OMP_CLAUSE_IF);
10081 if (ifc != NULL_TREE)
10083 if (OMP_CLAUSE_IF_MODIFIER (ifc) != ERROR_MARK
10084 && OMP_CLAUSE_IF_MODIFIER (ifc) != VOID_CST)
10085 error_at (OMP_CLAUSE_LOCATION (ifc),
10086 "expected %<cancel%> %<if%> clause modifier");
10087 else
10089 tree ifc2 = omp_find_clause (OMP_CLAUSE_CHAIN (ifc), OMP_CLAUSE_IF);
10090 if (ifc2 != NULL_TREE)
10092 gcc_assert (OMP_CLAUSE_IF_MODIFIER (ifc) == VOID_CST
10093 && OMP_CLAUSE_IF_MODIFIER (ifc2) != ERROR_MARK
10094 && OMP_CLAUSE_IF_MODIFIER (ifc2) != VOID_CST);
10095 error_at (OMP_CLAUSE_LOCATION (ifc2),
10096 "expected %<cancel%> %<if%> clause modifier");
10100 if (!processing_template_decl)
10101 ifc = maybe_convert_cond (OMP_CLAUSE_IF_EXPR (ifc));
10102 else
10103 ifc = build_x_binary_op (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
10104 OMP_CLAUSE_IF_EXPR (ifc), ERROR_MARK,
10105 integer_zero_node, ERROR_MARK,
10106 NULL, tf_warning_or_error);
10108 else
10109 ifc = boolean_true_node;
10110 vec->quick_push (build_int_cst (integer_type_node, mask));
10111 vec->quick_push (ifc);
10112 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
10113 finish_expr_stmt (stmt);
10116 void
10117 finish_omp_cancellation_point (tree clauses)
10119 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
10120 int mask = 0;
10121 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
10122 mask = 1;
10123 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
10124 mask = 2;
10125 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
10126 mask = 4;
10127 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
10128 mask = 8;
10129 else
10131 error ("%<#pragma omp cancellation point%> must specify one of "
10132 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> clauses");
10133 return;
10135 releasing_vec vec
10136 = make_tree_vector_single (build_int_cst (integer_type_node, mask));
10137 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
10138 finish_expr_stmt (stmt);
10141 /* Begin a __transaction_atomic or __transaction_relaxed statement.
10142 If PCOMPOUND is non-null, this is for a function-transaction-block, and we
10143 should create an extra compound stmt. */
10145 tree
10146 begin_transaction_stmt (location_t loc, tree *pcompound, int flags)
10148 tree r;
10150 if (pcompound)
10151 *pcompound = begin_compound_stmt (0);
10153 r = build_stmt (loc, TRANSACTION_EXPR, NULL_TREE);
10155 /* Only add the statement to the function if support enabled. */
10156 if (flag_tm)
10157 add_stmt (r);
10158 else
10159 error_at (loc, ((flags & TM_STMT_ATTR_RELAXED) != 0
10160 ? G_("%<__transaction_relaxed%> without "
10161 "transactional memory support enabled")
10162 : G_("%<__transaction_atomic%> without "
10163 "transactional memory support enabled")));
10165 TRANSACTION_EXPR_BODY (r) = push_stmt_list ();
10166 TREE_SIDE_EFFECTS (r) = 1;
10167 return r;
10170 /* End a __transaction_atomic or __transaction_relaxed statement.
10171 If COMPOUND_STMT is non-null, this is for a function-transaction-block,
10172 and we should end the compound. If NOEX is non-NULL, we wrap the body in
10173 a MUST_NOT_THROW_EXPR with NOEX as condition. */
10175 void
10176 finish_transaction_stmt (tree stmt, tree compound_stmt, int flags, tree noex)
10178 TRANSACTION_EXPR_BODY (stmt) = pop_stmt_list (TRANSACTION_EXPR_BODY (stmt));
10179 TRANSACTION_EXPR_OUTER (stmt) = (flags & TM_STMT_ATTR_OUTER) != 0;
10180 TRANSACTION_EXPR_RELAXED (stmt) = (flags & TM_STMT_ATTR_RELAXED) != 0;
10181 TRANSACTION_EXPR_IS_STMT (stmt) = 1;
10183 /* noexcept specifications are not allowed for function transactions. */
10184 gcc_assert (!(noex && compound_stmt));
10185 if (noex)
10187 tree body = build_must_not_throw_expr (TRANSACTION_EXPR_BODY (stmt),
10188 noex);
10189 protected_set_expr_location
10190 (body, EXPR_LOCATION (TRANSACTION_EXPR_BODY (stmt)));
10191 TREE_SIDE_EFFECTS (body) = 1;
10192 TRANSACTION_EXPR_BODY (stmt) = body;
10195 if (compound_stmt)
10196 finish_compound_stmt (compound_stmt);
10199 /* Build a __transaction_atomic or __transaction_relaxed expression. If
10200 NOEX is non-NULL, we wrap the body in a MUST_NOT_THROW_EXPR with NOEX as
10201 condition. */
10203 tree
10204 build_transaction_expr (location_t loc, tree expr, int flags, tree noex)
10206 tree ret;
10207 if (noex)
10209 expr = build_must_not_throw_expr (expr, noex);
10210 protected_set_expr_location (expr, loc);
10211 TREE_SIDE_EFFECTS (expr) = 1;
10213 ret = build1 (TRANSACTION_EXPR, TREE_TYPE (expr), expr);
10214 if (flags & TM_STMT_ATTR_RELAXED)
10215 TRANSACTION_EXPR_RELAXED (ret) = 1;
10216 TREE_SIDE_EFFECTS (ret) = 1;
10217 SET_EXPR_LOCATION (ret, loc);
10218 return ret;
10221 void
10222 init_cp_semantics (void)
10227 /* If we have a condition in conjunctive normal form (CNF), find the first
10228 failing clause. In other words, given an expression like
10230 true && true && false && true && false
10232 return the first 'false'. EXPR is the expression. */
10234 static tree
10235 find_failing_clause_r (tree expr)
10237 if (TREE_CODE (expr) == TRUTH_ANDIF_EXPR)
10239 /* First check the left side... */
10240 tree e = find_failing_clause_r (TREE_OPERAND (expr, 0));
10241 if (e == NULL_TREE)
10242 /* ...if we didn't find a false clause, check the right side. */
10243 e = find_failing_clause_r (TREE_OPERAND (expr, 1));
10244 return e;
10246 tree e = contextual_conv_bool (expr, tf_none);
10247 e = fold_non_dependent_expr (e, tf_none, /*manifestly_const_eval=*/true);
10248 if (integer_zerop (e))
10249 /* This is the failing clause. */
10250 return expr;
10251 return NULL_TREE;
10254 /* Wrapper for find_failing_clause_r. */
10256 static tree
10257 find_failing_clause (tree expr)
10259 if (TREE_CODE (expr) != TRUTH_ANDIF_EXPR)
10260 return NULL_TREE;
10261 return find_failing_clause_r (expr);
10264 /* Build a STATIC_ASSERT for a static assertion with the condition
10265 CONDITION and the message text MESSAGE. LOCATION is the location
10266 of the static assertion in the source code. When MEMBER_P, this
10267 static assertion is a member of a class. If SHOW_EXPR_P is true,
10268 print the condition (because it was instantiation-dependent). */
10270 void
10271 finish_static_assert (tree condition, tree message, location_t location,
10272 bool member_p, bool show_expr_p)
10274 tsubst_flags_t complain = tf_warning_or_error;
10276 if (message == NULL_TREE
10277 || message == error_mark_node
10278 || condition == NULL_TREE
10279 || condition == error_mark_node)
10280 return;
10282 if (check_for_bare_parameter_packs (condition))
10283 condition = error_mark_node;
10285 if (instantiation_dependent_expression_p (condition))
10287 /* We're in a template; build a STATIC_ASSERT and put it in
10288 the right place. */
10289 tree assertion;
10291 assertion = make_node (STATIC_ASSERT);
10292 STATIC_ASSERT_CONDITION (assertion) = condition;
10293 STATIC_ASSERT_MESSAGE (assertion) = message;
10294 STATIC_ASSERT_SOURCE_LOCATION (assertion) = location;
10296 if (member_p)
10297 maybe_add_class_template_decl_list (current_class_type,
10298 assertion,
10299 /*friend_p=*/0);
10300 else
10301 add_stmt (assertion);
10303 return;
10306 /* Save the condition in case it was a concept check. */
10307 tree orig_condition = condition;
10309 /* Fold the expression and convert it to a boolean value. */
10310 condition = contextual_conv_bool (condition, complain);
10311 condition = fold_non_dependent_expr (condition, complain,
10312 /*manifestly_const_eval=*/true);
10314 if (TREE_CODE (condition) == INTEGER_CST && !integer_zerop (condition))
10315 /* Do nothing; the condition is satisfied. */
10317 else
10319 iloc_sentinel ils (location);
10321 if (integer_zerop (condition))
10323 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
10324 (TREE_TYPE (TREE_TYPE (message))));
10325 int len = TREE_STRING_LENGTH (message) / sz - 1;
10327 /* See if we can find which clause was failing (for logical AND). */
10328 tree bad = find_failing_clause (orig_condition);
10329 /* If not, or its location is unusable, fall back to the previous
10330 location. */
10331 location_t cloc = location;
10332 if (cp_expr_location (bad) != UNKNOWN_LOCATION)
10333 cloc = cp_expr_location (bad);
10335 /* Report the error. */
10336 if (len == 0)
10337 error_at (cloc, "static assertion failed");
10338 else
10339 error_at (cloc, "static assertion failed: %s",
10340 TREE_STRING_POINTER (message));
10341 if (show_expr_p)
10342 inform (cloc, "%qE evaluates to false",
10343 /* Nobody wants to see the artificial (bool) cast. */
10344 (bad ? tree_strip_nop_conversions (bad) : orig_condition));
10346 /* Actually explain the failure if this is a concept check or a
10347 requires-expression. */
10348 if (concept_check_p (orig_condition)
10349 || TREE_CODE (orig_condition) == REQUIRES_EXPR)
10350 diagnose_constraints (location, orig_condition, NULL_TREE);
10352 else if (condition && condition != error_mark_node)
10354 error ("non-constant condition for static assertion");
10355 if (require_rvalue_constant_expression (condition))
10356 cxx_constant_value (condition);
10361 /* Implements the C++0x decltype keyword. Returns the type of EXPR,
10362 suitable for use as a type-specifier.
10364 ID_EXPRESSION_OR_MEMBER_ACCESS_P is true when EXPR was parsed as an
10365 id-expression or a class member access, FALSE when it was parsed as
10366 a full expression. */
10368 tree
10369 finish_decltype_type (tree expr, bool id_expression_or_member_access_p,
10370 tsubst_flags_t complain)
10372 tree type = NULL_TREE;
10374 if (!expr || error_operand_p (expr))
10375 return error_mark_node;
10377 if (TYPE_P (expr)
10378 || TREE_CODE (expr) == TYPE_DECL
10379 || (TREE_CODE (expr) == BIT_NOT_EXPR
10380 && TYPE_P (TREE_OPERAND (expr, 0))))
10382 if (complain & tf_error)
10383 error ("argument to %<decltype%> must be an expression");
10384 return error_mark_node;
10387 /* decltype is an unevaluated context. */
10388 cp_unevaluated u;
10390 /* Depending on the resolution of DR 1172, we may later need to distinguish
10391 instantiation-dependent but not type-dependent expressions so that, say,
10392 A<decltype(sizeof(T))>::U doesn't require 'typename'. */
10393 if (instantiation_dependent_uneval_expression_p (expr))
10395 type = cxx_make_type (DECLTYPE_TYPE);
10396 DECLTYPE_TYPE_EXPR (type) = expr;
10397 DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (type)
10398 = id_expression_or_member_access_p;
10399 SET_TYPE_STRUCTURAL_EQUALITY (type);
10401 return type;
10403 else if (processing_template_decl)
10405 expr = instantiate_non_dependent_expr_sfinae (expr, complain);
10406 if (expr == error_mark_node)
10407 return error_mark_node;
10410 /* The type denoted by decltype(e) is defined as follows: */
10412 expr = resolve_nondeduced_context (expr, complain);
10414 if (invalid_nonstatic_memfn_p (input_location, expr, complain))
10415 return error_mark_node;
10417 if (type_unknown_p (expr))
10419 if (complain & tf_error)
10420 error ("%<decltype%> cannot resolve address of overloaded function");
10421 return error_mark_node;
10424 /* To get the size of a static data member declared as an array of
10425 unknown bound, we need to instantiate it. */
10426 if (VAR_P (expr)
10427 && VAR_HAD_UNKNOWN_BOUND (expr)
10428 && DECL_TEMPLATE_INSTANTIATION (expr))
10429 instantiate_decl (expr, /*defer_ok*/true, /*expl_inst_mem*/false);
10431 if (id_expression_or_member_access_p)
10433 /* If e is an id-expression or a class member access (5.2.5
10434 [expr.ref]), decltype(e) is defined as the type of the entity
10435 named by e. If there is no such entity, or e names a set of
10436 overloaded functions, the program is ill-formed. */
10437 if (identifier_p (expr))
10438 expr = lookup_name (expr);
10440 if (INDIRECT_REF_P (expr)
10441 || TREE_CODE (expr) == VIEW_CONVERT_EXPR)
10442 /* This can happen when the expression is, e.g., "a.b". Just
10443 look at the underlying operand. */
10444 expr = TREE_OPERAND (expr, 0);
10446 if (TREE_CODE (expr) == OFFSET_REF
10447 || TREE_CODE (expr) == MEMBER_REF
10448 || TREE_CODE (expr) == SCOPE_REF)
10449 /* We're only interested in the field itself. If it is a
10450 BASELINK, we will need to see through it in the next
10451 step. */
10452 expr = TREE_OPERAND (expr, 1);
10454 if (BASELINK_P (expr))
10455 /* See through BASELINK nodes to the underlying function. */
10456 expr = BASELINK_FUNCTIONS (expr);
10458 /* decltype of a decomposition name drops references in the tuple case
10459 (unlike decltype of a normal variable) and keeps cv-qualifiers from
10460 the containing object in the other cases (unlike decltype of a member
10461 access expression). */
10462 if (DECL_DECOMPOSITION_P (expr))
10464 if (DECL_HAS_VALUE_EXPR_P (expr))
10465 /* Expr is an array or struct subobject proxy, handle
10466 bit-fields properly. */
10467 return unlowered_expr_type (expr);
10468 else
10469 /* Expr is a reference variable for the tuple case. */
10470 return lookup_decomp_type (expr);
10473 switch (TREE_CODE (expr))
10475 case FIELD_DECL:
10476 if (DECL_BIT_FIELD_TYPE (expr))
10478 type = DECL_BIT_FIELD_TYPE (expr);
10479 break;
10481 /* Fall through for fields that aren't bitfields. */
10482 gcc_fallthrough ();
10484 case FUNCTION_DECL:
10485 case VAR_DECL:
10486 case CONST_DECL:
10487 case PARM_DECL:
10488 case RESULT_DECL:
10489 case TEMPLATE_PARM_INDEX:
10490 expr = mark_type_use (expr);
10491 type = TREE_TYPE (expr);
10492 break;
10494 case ERROR_MARK:
10495 type = error_mark_node;
10496 break;
10498 case COMPONENT_REF:
10499 case COMPOUND_EXPR:
10500 mark_type_use (expr);
10501 type = is_bitfield_expr_with_lowered_type (expr);
10502 if (!type)
10503 type = TREE_TYPE (TREE_OPERAND (expr, 1));
10504 break;
10506 case BIT_FIELD_REF:
10507 gcc_unreachable ();
10509 case INTEGER_CST:
10510 case PTRMEM_CST:
10511 /* We can get here when the id-expression refers to an
10512 enumerator or non-type template parameter. */
10513 type = TREE_TYPE (expr);
10514 break;
10516 default:
10517 /* Handle instantiated template non-type arguments. */
10518 type = TREE_TYPE (expr);
10519 break;
10522 else
10524 /* Within a lambda-expression:
10526 Every occurrence of decltype((x)) where x is a possibly
10527 parenthesized id-expression that names an entity of
10528 automatic storage duration is treated as if x were
10529 transformed into an access to a corresponding data member
10530 of the closure type that would have been declared if x
10531 were a use of the denoted entity. */
10532 if (outer_automatic_var_p (expr)
10533 && current_function_decl
10534 && LAMBDA_FUNCTION_P (current_function_decl))
10535 type = capture_decltype (expr);
10536 else if (error_operand_p (expr))
10537 type = error_mark_node;
10538 else if (expr == current_class_ptr)
10539 /* If the expression is just "this", we want the
10540 cv-unqualified pointer for the "this" type. */
10541 type = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
10542 else
10544 /* Otherwise, where T is the type of e, if e is an lvalue,
10545 decltype(e) is defined as T&; if an xvalue, T&&; otherwise, T. */
10546 cp_lvalue_kind clk = lvalue_kind (expr);
10547 type = unlowered_expr_type (expr);
10548 gcc_assert (!TYPE_REF_P (type));
10550 /* For vector types, pick a non-opaque variant. */
10551 if (VECTOR_TYPE_P (type))
10552 type = strip_typedefs (type);
10554 if (clk != clk_none && !(clk & clk_class))
10555 type = cp_build_reference_type (type, (clk & clk_rvalueref));
10559 return type;
10562 /* Called from trait_expr_value to evaluate either __has_nothrow_assign or
10563 __has_nothrow_copy, depending on assign_p. Returns true iff all
10564 the copy {ctor,assign} fns are nothrow. */
10566 static bool
10567 classtype_has_nothrow_assign_or_copy_p (tree type, bool assign_p)
10569 tree fns = NULL_TREE;
10571 if (assign_p || TYPE_HAS_COPY_CTOR (type))
10572 fns = get_class_binding (type, assign_p ? assign_op_identifier
10573 : ctor_identifier);
10575 bool saw_copy = false;
10576 for (ovl_iterator iter (fns); iter; ++iter)
10578 tree fn = *iter;
10580 if (copy_fn_p (fn) > 0)
10582 saw_copy = true;
10583 if (!maybe_instantiate_noexcept (fn)
10584 || !TYPE_NOTHROW_P (TREE_TYPE (fn)))
10585 return false;
10589 return saw_copy;
10592 /* Return true if DERIVED is pointer interconvertible base of BASE. */
10594 static bool
10595 pointer_interconvertible_base_of_p (tree base, tree derived)
10597 if (base == error_mark_node || derived == error_mark_node)
10598 return false;
10599 base = TYPE_MAIN_VARIANT (base);
10600 derived = TYPE_MAIN_VARIANT (derived);
10601 if (!NON_UNION_CLASS_TYPE_P (base)
10602 || !NON_UNION_CLASS_TYPE_P (derived))
10603 return false;
10605 if (same_type_p (base, derived))
10606 return true;
10608 if (!std_layout_type_p (derived))
10609 return false;
10611 return uniquely_derived_from_p (base, derived);
10614 /* Helper function for fold_builtin_is_pointer_inverconvertible_with_class,
10615 return true if MEMBERTYPE is the type of the first non-static data member
10616 of TYPE or for unions of any members. */
10617 static bool
10618 first_nonstatic_data_member_p (tree type, tree membertype)
10620 for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
10622 if (TREE_CODE (field) != FIELD_DECL)
10623 continue;
10624 if (DECL_FIELD_IS_BASE (field) && is_empty_field (field))
10625 continue;
10626 if (DECL_FIELD_IS_BASE (field))
10627 return first_nonstatic_data_member_p (TREE_TYPE (field), membertype);
10628 if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
10630 if ((TREE_CODE (TREE_TYPE (field)) == UNION_TYPE
10631 || std_layout_type_p (TREE_TYPE (field)))
10632 && first_nonstatic_data_member_p (TREE_TYPE (field), membertype))
10633 return true;
10635 else if (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (field),
10636 membertype))
10637 return true;
10638 if (TREE_CODE (type) != UNION_TYPE)
10639 return false;
10641 return false;
10644 /* Fold __builtin_is_pointer_interconvertible_with_class call. */
10646 tree
10647 fold_builtin_is_pointer_inverconvertible_with_class (location_t loc, int nargs,
10648 tree *args)
10650 /* Unless users call the builtin directly, the following 3 checks should be
10651 ensured from std::is_pointer_interconvertible_with_class function
10652 template. */
10653 if (nargs != 1)
10655 error_at (loc, "%<__builtin_is_pointer_interconvertible_with_class%> "
10656 "needs a single argument");
10657 return boolean_false_node;
10659 tree arg = args[0];
10660 if (error_operand_p (arg))
10661 return boolean_false_node;
10662 if (!TYPE_PTRMEM_P (TREE_TYPE (arg)))
10664 error_at (loc, "%<__builtin_is_pointer_interconvertible_with_class%> "
10665 "argument is not pointer to member");
10666 return boolean_false_node;
10669 if (!TYPE_PTRDATAMEM_P (TREE_TYPE (arg)))
10670 return boolean_false_node;
10672 tree membertype = TREE_TYPE (TREE_TYPE (arg));
10673 tree basetype = TYPE_OFFSET_BASETYPE (TREE_TYPE (arg));
10674 if (!complete_type_or_else (basetype, NULL_TREE))
10675 return boolean_false_node;
10677 if (TREE_CODE (basetype) != UNION_TYPE
10678 && !std_layout_type_p (basetype))
10679 return boolean_false_node;
10681 if (!first_nonstatic_data_member_p (basetype, membertype))
10682 return boolean_false_node;
10684 if (TREE_CODE (arg) == PTRMEM_CST)
10685 arg = cplus_expand_constant (arg);
10687 if (integer_nonzerop (arg))
10688 return boolean_false_node;
10689 if (integer_zerop (arg))
10690 return boolean_true_node;
10692 return fold_build2 (EQ_EXPR, boolean_type_node, arg,
10693 build_zero_cst (TREE_TYPE (arg)));
10696 /* Helper function for is_corresponding_member_aggr. Return true if
10697 MEMBERTYPE pointer-to-data-member ARG can be found in anonymous
10698 union or structure BASETYPE. */
10700 static bool
10701 is_corresponding_member_union (tree basetype, tree membertype, tree arg)
10703 for (tree field = TYPE_FIELDS (basetype); field; field = DECL_CHAIN (field))
10704 if (TREE_CODE (field) != FIELD_DECL || DECL_BIT_FIELD_TYPE (field))
10705 continue;
10706 else if (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (field),
10707 membertype))
10709 if (TREE_CODE (arg) != INTEGER_CST
10710 || tree_int_cst_equal (arg, byte_position (field)))
10711 return true;
10713 else if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
10715 tree narg = arg;
10716 if (TREE_CODE (basetype) != UNION_TYPE
10717 && TREE_CODE (narg) == INTEGER_CST)
10718 narg = size_binop (MINUS_EXPR, arg, byte_position (field));
10719 if (is_corresponding_member_union (TREE_TYPE (field),
10720 membertype, narg))
10721 return true;
10723 return false;
10726 /* Helper function for fold_builtin_is_corresponding_member call.
10727 Return boolean_false_node if MEMBERTYPE1 BASETYPE1::*ARG1 and
10728 MEMBERTYPE2 BASETYPE2::*ARG2 aren't corresponding members,
10729 boolean_true_node if they are corresponding members, or for
10730 non-constant ARG2 the highest member offset for corresponding
10731 members. */
10733 static tree
10734 is_corresponding_member_aggr (location_t loc, tree basetype1, tree membertype1,
10735 tree arg1, tree basetype2, tree membertype2,
10736 tree arg2)
10738 tree field1 = TYPE_FIELDS (basetype1);
10739 tree field2 = TYPE_FIELDS (basetype2);
10740 tree ret = boolean_false_node;
10741 while (1)
10743 bool r = next_common_initial_seqence (field1, field2);
10744 if (field1 == NULL_TREE || field2 == NULL_TREE)
10745 break;
10746 if (r
10747 && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (field1),
10748 membertype1)
10749 && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (field2),
10750 membertype2))
10752 tree pos = byte_position (field1);
10753 if (TREE_CODE (arg1) == INTEGER_CST
10754 && tree_int_cst_equal (arg1, pos))
10756 if (TREE_CODE (arg2) == INTEGER_CST)
10757 return boolean_true_node;
10758 return pos;
10760 else if (TREE_CODE (arg1) != INTEGER_CST)
10761 ret = pos;
10763 else if (ANON_AGGR_TYPE_P (TREE_TYPE (field1))
10764 && ANON_AGGR_TYPE_P (TREE_TYPE (field2)))
10766 if ((!lookup_attribute ("no_unique_address",
10767 DECL_ATTRIBUTES (field1)))
10768 != !lookup_attribute ("no_unique_address",
10769 DECL_ATTRIBUTES (field2)))
10770 break;
10771 if (!tree_int_cst_equal (bit_position (field1),
10772 bit_position (field2)))
10773 break;
10774 bool overlap = true;
10775 tree pos = byte_position (field1);
10776 if (TREE_CODE (arg1) == INTEGER_CST)
10778 tree off1 = fold_convert (sizetype, arg1);
10779 tree sz1 = TYPE_SIZE_UNIT (TREE_TYPE (field1));
10780 if (tree_int_cst_lt (off1, pos)
10781 || tree_int_cst_le (size_binop (PLUS_EXPR, pos, sz1), off1))
10782 overlap = false;
10784 if (TREE_CODE (arg2) == INTEGER_CST)
10786 tree off2 = fold_convert (sizetype, arg2);
10787 tree sz2 = TYPE_SIZE_UNIT (TREE_TYPE (field2));
10788 if (tree_int_cst_lt (off2, pos)
10789 || tree_int_cst_le (size_binop (PLUS_EXPR, pos, sz2), off2))
10790 overlap = false;
10792 if (overlap
10793 && NON_UNION_CLASS_TYPE_P (TREE_TYPE (field1))
10794 && NON_UNION_CLASS_TYPE_P (TREE_TYPE (field2)))
10796 tree narg1 = arg1;
10797 if (TREE_CODE (arg1) == INTEGER_CST)
10798 narg1 = size_binop (MINUS_EXPR,
10799 fold_convert (sizetype, arg1), pos);
10800 tree narg2 = arg2;
10801 if (TREE_CODE (arg2) == INTEGER_CST)
10802 narg2 = size_binop (MINUS_EXPR,
10803 fold_convert (sizetype, arg2), pos);
10804 tree t1 = TREE_TYPE (field1);
10805 tree t2 = TREE_TYPE (field2);
10806 tree nret = is_corresponding_member_aggr (loc, t1, membertype1,
10807 narg1, t2, membertype2,
10808 narg2);
10809 if (nret != boolean_false_node)
10811 if (nret == boolean_true_node)
10812 return nret;
10813 if (TREE_CODE (arg1) == INTEGER_CST)
10814 return size_binop (PLUS_EXPR, nret, pos);
10815 ret = size_binop (PLUS_EXPR, nret, pos);
10818 else if (overlap
10819 && TREE_CODE (TREE_TYPE (field1)) == UNION_TYPE
10820 && TREE_CODE (TREE_TYPE (field2)) == UNION_TYPE)
10822 tree narg1 = arg1;
10823 if (TREE_CODE (arg1) == INTEGER_CST)
10824 narg1 = size_binop (MINUS_EXPR,
10825 fold_convert (sizetype, arg1), pos);
10826 tree narg2 = arg2;
10827 if (TREE_CODE (arg2) == INTEGER_CST)
10828 narg2 = size_binop (MINUS_EXPR,
10829 fold_convert (sizetype, arg2), pos);
10830 if (is_corresponding_member_union (TREE_TYPE (field1),
10831 membertype1, narg1)
10832 && is_corresponding_member_union (TREE_TYPE (field2),
10833 membertype2, narg2))
10835 sorry_at (loc, "%<__builtin_is_corresponding_member%> "
10836 "not well defined for anonymous unions");
10837 return boolean_false_node;
10841 if (!r)
10842 break;
10843 field1 = DECL_CHAIN (field1);
10844 field2 = DECL_CHAIN (field2);
10846 return ret;
10849 /* Fold __builtin_is_corresponding_member call. */
10851 tree
10852 fold_builtin_is_corresponding_member (location_t loc, int nargs,
10853 tree *args)
10855 /* Unless users call the builtin directly, the following 3 checks should be
10856 ensured from std::is_corresponding_member function template. */
10857 if (nargs != 2)
10859 error_at (loc, "%<__builtin_is_corresponding_member%> "
10860 "needs two arguments");
10861 return boolean_false_node;
10863 tree arg1 = args[0];
10864 tree arg2 = args[1];
10865 if (error_operand_p (arg1) || error_operand_p (arg2))
10866 return boolean_false_node;
10867 if (!TYPE_PTRMEM_P (TREE_TYPE (arg1))
10868 || !TYPE_PTRMEM_P (TREE_TYPE (arg2)))
10870 error_at (loc, "%<__builtin_is_corresponding_member%> "
10871 "argument is not pointer to member");
10872 return boolean_false_node;
10875 if (!TYPE_PTRDATAMEM_P (TREE_TYPE (arg1))
10876 || !TYPE_PTRDATAMEM_P (TREE_TYPE (arg2)))
10877 return boolean_false_node;
10879 tree membertype1 = TREE_TYPE (TREE_TYPE (arg1));
10880 tree basetype1 = TYPE_OFFSET_BASETYPE (TREE_TYPE (arg1));
10881 if (!complete_type_or_else (basetype1, NULL_TREE))
10882 return boolean_false_node;
10884 tree membertype2 = TREE_TYPE (TREE_TYPE (arg2));
10885 tree basetype2 = TYPE_OFFSET_BASETYPE (TREE_TYPE (arg2));
10886 if (!complete_type_or_else (basetype2, NULL_TREE))
10887 return boolean_false_node;
10889 if (!NON_UNION_CLASS_TYPE_P (basetype1)
10890 || !NON_UNION_CLASS_TYPE_P (basetype2)
10891 || !std_layout_type_p (basetype1)
10892 || !std_layout_type_p (basetype2))
10893 return boolean_false_node;
10895 /* If the member types aren't layout compatible, then they
10896 can't be corresponding members. */
10897 if (!layout_compatible_type_p (membertype1, membertype2))
10898 return boolean_false_node;
10900 if (TREE_CODE (arg1) == PTRMEM_CST)
10901 arg1 = cplus_expand_constant (arg1);
10902 if (TREE_CODE (arg2) == PTRMEM_CST)
10903 arg2 = cplus_expand_constant (arg2);
10905 if (null_member_pointer_value_p (arg1)
10906 || null_member_pointer_value_p (arg2))
10907 return boolean_false_node;
10909 if (TREE_CODE (arg1) == INTEGER_CST
10910 && TREE_CODE (arg2) == INTEGER_CST
10911 && !tree_int_cst_equal (arg1, arg2))
10912 return boolean_false_node;
10914 if (TREE_CODE (arg2) == INTEGER_CST
10915 && TREE_CODE (arg1) != INTEGER_CST)
10917 std::swap (arg1, arg2);
10918 std::swap (membertype1, membertype2);
10919 std::swap (basetype1, basetype2);
10922 tree ret = is_corresponding_member_aggr (loc, basetype1, membertype1, arg1,
10923 basetype2, membertype2, arg2);
10924 if (TREE_TYPE (ret) == boolean_type_node)
10925 return ret;
10926 /* If both arg1 and arg2 are INTEGER_CSTs, is_corresponding_member_aggr
10927 already returns boolean_{true,false}_node whether those particular
10928 members are corresponding members or not. Otherwise, if only
10929 one of them is INTEGER_CST (canonicalized to first being INTEGER_CST
10930 above), it returns boolean_false_node if it is certainly not a
10931 corresponding member and otherwise we need to do a runtime check that
10932 those two OFFSET_TYPE offsets are equal.
10933 If neither of the operands is INTEGER_CST, is_corresponding_member_aggr
10934 returns the largest offset at which the members would be corresponding
10935 members, so perform arg1 <= ret && arg1 == arg2 runtime check. */
10936 gcc_assert (TREE_CODE (arg2) != INTEGER_CST);
10937 if (TREE_CODE (arg1) == INTEGER_CST)
10938 return fold_build2 (EQ_EXPR, boolean_type_node, arg1,
10939 fold_convert (TREE_TYPE (arg1), arg2));
10940 ret = fold_build2 (LE_EXPR, boolean_type_node,
10941 fold_convert (pointer_sized_int_node, arg1),
10942 fold_convert (pointer_sized_int_node, ret));
10943 return fold_build2 (TRUTH_AND_EXPR, boolean_type_node, ret,
10944 fold_build2 (EQ_EXPR, boolean_type_node, arg1,
10945 fold_convert (TREE_TYPE (arg1), arg2)));
10948 /* Actually evaluates the trait. */
10950 static bool
10951 trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
10953 enum tree_code type_code1;
10954 tree t;
10956 type_code1 = TREE_CODE (type1);
10958 switch (kind)
10960 case CPTK_HAS_NOTHROW_ASSIGN:
10961 type1 = strip_array_types (type1);
10962 return (!CP_TYPE_CONST_P (type1) && type_code1 != REFERENCE_TYPE
10963 && (trait_expr_value (CPTK_HAS_TRIVIAL_ASSIGN, type1, type2)
10964 || (CLASS_TYPE_P (type1)
10965 && classtype_has_nothrow_assign_or_copy_p (type1,
10966 true))));
10968 case CPTK_HAS_TRIVIAL_ASSIGN:
10969 /* ??? The standard seems to be missing the "or array of such a class
10970 type" wording for this trait. */
10971 type1 = strip_array_types (type1);
10972 return (!CP_TYPE_CONST_P (type1) && type_code1 != REFERENCE_TYPE
10973 && (trivial_type_p (type1)
10974 || (CLASS_TYPE_P (type1)
10975 && TYPE_HAS_TRIVIAL_COPY_ASSIGN (type1))));
10977 case CPTK_HAS_NOTHROW_CONSTRUCTOR:
10978 type1 = strip_array_types (type1);
10979 return (trait_expr_value (CPTK_HAS_TRIVIAL_CONSTRUCTOR, type1, type2)
10980 || (CLASS_TYPE_P (type1)
10981 && (t = locate_ctor (type1))
10982 && maybe_instantiate_noexcept (t)
10983 && TYPE_NOTHROW_P (TREE_TYPE (t))));
10985 case CPTK_HAS_TRIVIAL_CONSTRUCTOR:
10986 type1 = strip_array_types (type1);
10987 return (trivial_type_p (type1)
10988 || (CLASS_TYPE_P (type1) && TYPE_HAS_TRIVIAL_DFLT (type1)));
10990 case CPTK_HAS_NOTHROW_COPY:
10991 type1 = strip_array_types (type1);
10992 return (trait_expr_value (CPTK_HAS_TRIVIAL_COPY, type1, type2)
10993 || (CLASS_TYPE_P (type1)
10994 && classtype_has_nothrow_assign_or_copy_p (type1, false)));
10996 case CPTK_HAS_TRIVIAL_COPY:
10997 /* ??? The standard seems to be missing the "or array of such a class
10998 type" wording for this trait. */
10999 type1 = strip_array_types (type1);
11000 return (trivial_type_p (type1) || type_code1 == REFERENCE_TYPE
11001 || (CLASS_TYPE_P (type1) && TYPE_HAS_TRIVIAL_COPY_CTOR (type1)));
11003 case CPTK_HAS_TRIVIAL_DESTRUCTOR:
11004 type1 = strip_array_types (type1);
11005 return (trivial_type_p (type1) || type_code1 == REFERENCE_TYPE
11006 || (CLASS_TYPE_P (type1)
11007 && TYPE_HAS_TRIVIAL_DESTRUCTOR (type1)));
11009 case CPTK_HAS_VIRTUAL_DESTRUCTOR:
11010 return type_has_virtual_destructor (type1);
11012 case CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS:
11013 return type_has_unique_obj_representations (type1);
11015 case CPTK_IS_ABSTRACT:
11016 return ABSTRACT_CLASS_TYPE_P (type1);
11018 case CPTK_IS_AGGREGATE:
11019 return CP_AGGREGATE_TYPE_P (type1);
11021 case CPTK_IS_BASE_OF:
11022 return (NON_UNION_CLASS_TYPE_P (type1) && NON_UNION_CLASS_TYPE_P (type2)
11023 && (same_type_ignoring_top_level_qualifiers_p (type1, type2)
11024 || DERIVED_FROM_P (type1, type2)));
11026 case CPTK_IS_CLASS:
11027 return NON_UNION_CLASS_TYPE_P (type1);
11029 case CPTK_IS_EMPTY:
11030 return NON_UNION_CLASS_TYPE_P (type1) && CLASSTYPE_EMPTY_P (type1);
11032 case CPTK_IS_ENUM:
11033 return type_code1 == ENUMERAL_TYPE;
11035 case CPTK_IS_FINAL:
11036 return CLASS_TYPE_P (type1) && CLASSTYPE_FINAL (type1);
11038 case CPTK_IS_LAYOUT_COMPATIBLE:
11039 return layout_compatible_type_p (type1, type2);
11041 case CPTK_IS_LITERAL_TYPE:
11042 return literal_type_p (type1);
11044 case CPTK_IS_POINTER_INTERCONVERTIBLE_BASE_OF:
11045 return pointer_interconvertible_base_of_p (type1, type2);
11047 case CPTK_IS_POD:
11048 return pod_type_p (type1);
11050 case CPTK_IS_POLYMORPHIC:
11051 return CLASS_TYPE_P (type1) && TYPE_POLYMORPHIC_P (type1);
11053 case CPTK_IS_SAME_AS:
11054 return same_type_p (type1, type2);
11056 case CPTK_IS_STD_LAYOUT:
11057 return std_layout_type_p (type1);
11059 case CPTK_IS_TRIVIAL:
11060 return trivial_type_p (type1);
11062 case CPTK_IS_TRIVIALLY_ASSIGNABLE:
11063 return is_trivially_xible (MODIFY_EXPR, type1, type2);
11065 case CPTK_IS_TRIVIALLY_CONSTRUCTIBLE:
11066 return is_trivially_xible (INIT_EXPR, type1, type2);
11068 case CPTK_IS_TRIVIALLY_COPYABLE:
11069 return trivially_copyable_p (type1);
11071 case CPTK_IS_UNION:
11072 return type_code1 == UNION_TYPE;
11074 case CPTK_IS_ASSIGNABLE:
11075 return is_xible (MODIFY_EXPR, type1, type2);
11077 case CPTK_IS_CONSTRUCTIBLE:
11078 return is_xible (INIT_EXPR, type1, type2);
11080 case CPTK_IS_NOTHROW_ASSIGNABLE:
11081 return is_nothrow_xible (MODIFY_EXPR, type1, type2);
11083 case CPTK_IS_NOTHROW_CONSTRUCTIBLE:
11084 return is_nothrow_xible (INIT_EXPR, type1, type2);
11086 default:
11087 gcc_unreachable ();
11088 return false;
11092 /* If TYPE is an array of unknown bound, or (possibly cv-qualified)
11093 void, or a complete type, returns true, otherwise false. */
11095 static bool
11096 check_trait_type (tree type)
11098 if (type == NULL_TREE)
11099 return true;
11101 if (TREE_CODE (type) == TREE_LIST)
11102 return (check_trait_type (TREE_VALUE (type))
11103 && check_trait_type (TREE_CHAIN (type)));
11105 if (TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type)
11106 && COMPLETE_TYPE_P (TREE_TYPE (type)))
11107 return true;
11109 if (VOID_TYPE_P (type))
11110 return true;
11112 return !!complete_type_or_else (strip_array_types (type), NULL_TREE);
11115 /* Process a trait expression. */
11117 tree
11118 finish_trait_expr (location_t loc, cp_trait_kind kind, tree type1, tree type2)
11120 if (type1 == error_mark_node
11121 || type2 == error_mark_node)
11122 return error_mark_node;
11124 if (processing_template_decl)
11126 tree trait_expr = make_node (TRAIT_EXPR);
11127 TREE_TYPE (trait_expr) = boolean_type_node;
11128 TRAIT_EXPR_TYPE1 (trait_expr) = type1;
11129 TRAIT_EXPR_TYPE2 (trait_expr) = type2;
11130 TRAIT_EXPR_KIND (trait_expr) = kind;
11131 TRAIT_EXPR_LOCATION (trait_expr) = loc;
11132 return trait_expr;
11135 switch (kind)
11137 case CPTK_HAS_NOTHROW_ASSIGN:
11138 case CPTK_HAS_TRIVIAL_ASSIGN:
11139 case CPTK_HAS_NOTHROW_CONSTRUCTOR:
11140 case CPTK_HAS_TRIVIAL_CONSTRUCTOR:
11141 case CPTK_HAS_NOTHROW_COPY:
11142 case CPTK_HAS_TRIVIAL_COPY:
11143 case CPTK_HAS_TRIVIAL_DESTRUCTOR:
11144 case CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS:
11145 case CPTK_HAS_VIRTUAL_DESTRUCTOR:
11146 case CPTK_IS_ABSTRACT:
11147 case CPTK_IS_AGGREGATE:
11148 case CPTK_IS_EMPTY:
11149 case CPTK_IS_FINAL:
11150 case CPTK_IS_LITERAL_TYPE:
11151 case CPTK_IS_POD:
11152 case CPTK_IS_POLYMORPHIC:
11153 case CPTK_IS_STD_LAYOUT:
11154 case CPTK_IS_TRIVIAL:
11155 case CPTK_IS_TRIVIALLY_COPYABLE:
11156 if (!check_trait_type (type1))
11157 return error_mark_node;
11158 break;
11160 case CPTK_IS_ASSIGNABLE:
11161 case CPTK_IS_CONSTRUCTIBLE:
11162 break;
11164 case CPTK_IS_TRIVIALLY_ASSIGNABLE:
11165 case CPTK_IS_TRIVIALLY_CONSTRUCTIBLE:
11166 case CPTK_IS_NOTHROW_ASSIGNABLE:
11167 case CPTK_IS_NOTHROW_CONSTRUCTIBLE:
11168 if (!check_trait_type (type1)
11169 || !check_trait_type (type2))
11170 return error_mark_node;
11171 break;
11173 case CPTK_IS_BASE_OF:
11174 case CPTK_IS_POINTER_INTERCONVERTIBLE_BASE_OF:
11175 if (NON_UNION_CLASS_TYPE_P (type1) && NON_UNION_CLASS_TYPE_P (type2)
11176 && !same_type_ignoring_top_level_qualifiers_p (type1, type2)
11177 && !complete_type_or_else (type2, NULL_TREE))
11178 /* We already issued an error. */
11179 return error_mark_node;
11180 break;
11182 case CPTK_IS_CLASS:
11183 case CPTK_IS_ENUM:
11184 case CPTK_IS_UNION:
11185 case CPTK_IS_SAME_AS:
11186 break;
11188 case CPTK_IS_LAYOUT_COMPATIBLE:
11189 if (!array_of_unknown_bound_p (type1)
11190 && TREE_CODE (type1) != VOID_TYPE
11191 && !complete_type_or_else (type1, NULL_TREE))
11192 /* We already issued an error. */
11193 return error_mark_node;
11194 if (!array_of_unknown_bound_p (type2)
11195 && TREE_CODE (type2) != VOID_TYPE
11196 && !complete_type_or_else (type2, NULL_TREE))
11197 /* We already issued an error. */
11198 return error_mark_node;
11199 break;
11201 default:
11202 gcc_unreachable ();
11205 tree val = (trait_expr_value (kind, type1, type2)
11206 ? boolean_true_node : boolean_false_node);
11207 return maybe_wrap_with_location (val, loc);
11210 /* Do-nothing variants of functions to handle pragma FLOAT_CONST_DECIMAL64,
11211 which is ignored for C++. */
11213 void
11214 set_float_const_decimal64 (void)
11218 void
11219 clear_float_const_decimal64 (void)
11223 bool
11224 float_const_decimal64_p (void)
11226 return 0;
11230 /* Return true if T designates the implied `this' parameter. */
11232 bool
11233 is_this_parameter (tree t)
11235 if (!DECL_P (t) || DECL_NAME (t) != this_identifier)
11236 return false;
11237 gcc_assert (TREE_CODE (t) == PARM_DECL || is_capture_proxy (t)
11238 || (cp_binding_oracle && TREE_CODE (t) == VAR_DECL));
11239 return true;
11242 /* Insert the deduced return type for an auto function. */
11244 void
11245 apply_deduced_return_type (tree fco, tree return_type)
11247 tree result;
11249 if (return_type == error_mark_node)
11250 return;
11252 if (DECL_CONV_FN_P (fco))
11253 DECL_NAME (fco) = make_conv_op_name (return_type);
11255 TREE_TYPE (fco) = change_return_type (return_type, TREE_TYPE (fco));
11257 result = DECL_RESULT (fco);
11258 if (result == NULL_TREE)
11259 return;
11260 if (TREE_TYPE (result) == return_type)
11261 return;
11263 if (!processing_template_decl && !VOID_TYPE_P (return_type)
11264 && !complete_type_or_else (return_type, NULL_TREE))
11265 return;
11267 /* We already have a DECL_RESULT from start_preparsed_function.
11268 Now we need to redo the work it and allocate_struct_function
11269 did to reflect the new type. */
11270 gcc_assert (current_function_decl == fco);
11271 result = build_decl (input_location, RESULT_DECL, NULL_TREE,
11272 TYPE_MAIN_VARIANT (return_type));
11273 DECL_ARTIFICIAL (result) = 1;
11274 DECL_IGNORED_P (result) = 1;
11275 cp_apply_type_quals_to_decl (cp_type_quals (return_type),
11276 result);
11278 DECL_RESULT (fco) = result;
11280 if (!processing_template_decl)
11282 bool aggr = aggregate_value_p (result, fco);
11283 #ifdef PCC_STATIC_STRUCT_RETURN
11284 cfun->returns_pcc_struct = aggr;
11285 #endif
11286 cfun->returns_struct = aggr;
11290 /* DECL is a local variable or parameter from the surrounding scope of a
11291 lambda-expression. Returns the decltype for a use of the capture field
11292 for DECL even if it hasn't been captured yet. */
11294 static tree
11295 capture_decltype (tree decl)
11297 tree lam = CLASSTYPE_LAMBDA_EXPR (DECL_CONTEXT (current_function_decl));
11298 tree cap = lookup_name (DECL_NAME (decl), LOOK_where::BLOCK,
11299 LOOK_want::HIDDEN_LAMBDA);
11300 tree type;
11302 if (cap && is_capture_proxy (cap))
11303 type = TREE_TYPE (cap);
11304 else
11305 switch (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lam))
11307 case CPLD_NONE:
11308 error ("%qD is not captured", decl);
11309 return error_mark_node;
11311 case CPLD_COPY:
11312 type = TREE_TYPE (decl);
11313 if (TYPE_REF_P (type)
11314 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
11315 type = TREE_TYPE (type);
11316 break;
11318 case CPLD_REFERENCE:
11319 type = TREE_TYPE (decl);
11320 if (!TYPE_REF_P (type))
11321 type = build_reference_type (TREE_TYPE (decl));
11322 break;
11324 default:
11325 gcc_unreachable ();
11328 if (!TYPE_REF_P (type))
11330 if (!LAMBDA_EXPR_MUTABLE_P (lam))
11331 type = cp_build_qualified_type (type, (cp_type_quals (type)
11332 |TYPE_QUAL_CONST));
11333 type = build_reference_type (type);
11335 return type;
11338 /* Build a unary fold expression of EXPR over OP. If IS_RIGHT is true,
11339 this is a right unary fold. Otherwise it is a left unary fold. */
11341 static tree
11342 finish_unary_fold_expr (tree expr, int op, tree_code dir)
11344 /* Build a pack expansion (assuming expr has pack type). */
11345 if (!uses_parameter_packs (expr))
11347 error_at (location_of (expr), "operand of fold expression has no "
11348 "unexpanded parameter packs");
11349 return error_mark_node;
11351 tree pack = make_pack_expansion (expr);
11353 /* Build the fold expression. */
11354 tree code = build_int_cstu (integer_type_node, abs (op));
11355 tree fold = build_min_nt_loc (UNKNOWN_LOCATION, dir, code, pack);
11356 FOLD_EXPR_MODIFY_P (fold) = (op < 0);
11357 return fold;
11360 tree
11361 finish_left_unary_fold_expr (tree expr, int op)
11363 return finish_unary_fold_expr (expr, op, UNARY_LEFT_FOLD_EXPR);
11366 tree
11367 finish_right_unary_fold_expr (tree expr, int op)
11369 return finish_unary_fold_expr (expr, op, UNARY_RIGHT_FOLD_EXPR);
11372 /* Build a binary fold expression over EXPR1 and EXPR2. The
11373 associativity of the fold is determined by EXPR1 and EXPR2 (whichever
11374 has an unexpanded parameter pack). */
11376 tree
11377 finish_binary_fold_expr (tree pack, tree init, int op, tree_code dir)
11379 pack = make_pack_expansion (pack);
11380 tree code = build_int_cstu (integer_type_node, abs (op));
11381 tree fold = build_min_nt_loc (UNKNOWN_LOCATION, dir, code, pack, init);
11382 FOLD_EXPR_MODIFY_P (fold) = (op < 0);
11383 return fold;
11386 tree
11387 finish_binary_fold_expr (tree expr1, tree expr2, int op)
11389 // Determine which expr has an unexpanded parameter pack and
11390 // set the pack and initial term.
11391 bool pack1 = uses_parameter_packs (expr1);
11392 bool pack2 = uses_parameter_packs (expr2);
11393 if (pack1 && !pack2)
11394 return finish_binary_fold_expr (expr1, expr2, op, BINARY_RIGHT_FOLD_EXPR);
11395 else if (pack2 && !pack1)
11396 return finish_binary_fold_expr (expr2, expr1, op, BINARY_LEFT_FOLD_EXPR);
11397 else
11399 if (pack1)
11400 error ("both arguments in binary fold have unexpanded parameter packs");
11401 else
11402 error ("no unexpanded parameter packs in binary fold");
11404 return error_mark_node;
11407 /* Finish __builtin_launder (arg). */
11409 tree
11410 finish_builtin_launder (location_t loc, tree arg, tsubst_flags_t complain)
11412 tree orig_arg = arg;
11413 if (!type_dependent_expression_p (arg))
11414 arg = decay_conversion (arg, complain);
11415 if (error_operand_p (arg))
11416 return error_mark_node;
11417 if (!type_dependent_expression_p (arg)
11418 && !TYPE_PTR_P (TREE_TYPE (arg)))
11420 error_at (loc, "non-pointer argument to %<__builtin_launder%>");
11421 return error_mark_node;
11423 if (processing_template_decl)
11424 arg = orig_arg;
11425 return build_call_expr_internal_loc (loc, IFN_LAUNDER,
11426 TREE_TYPE (arg), 1, arg);
11429 /* Finish __builtin_convertvector (arg, type). */
11431 tree
11432 cp_build_vec_convert (tree arg, location_t loc, tree type,
11433 tsubst_flags_t complain)
11435 if (error_operand_p (type))
11436 return error_mark_node;
11437 if (error_operand_p (arg))
11438 return error_mark_node;
11440 tree ret = NULL_TREE;
11441 if (!type_dependent_expression_p (arg) && !dependent_type_p (type))
11442 ret = c_build_vec_convert (cp_expr_loc_or_input_loc (arg),
11443 decay_conversion (arg, complain),
11444 loc, type, (complain & tf_error) != 0);
11446 if (!processing_template_decl)
11447 return ret;
11449 return build_call_expr_internal_loc (loc, IFN_VEC_CONVERT, type, 1, arg);
11452 /* Finish __builtin_bit_cast (type, arg). */
11454 tree
11455 cp_build_bit_cast (location_t loc, tree type, tree arg,
11456 tsubst_flags_t complain)
11458 if (error_operand_p (type))
11459 return error_mark_node;
11460 if (!dependent_type_p (type))
11462 if (!complete_type_or_maybe_complain (type, NULL_TREE, complain))
11463 return error_mark_node;
11464 if (TREE_CODE (type) == ARRAY_TYPE)
11466 /* std::bit_cast for destination ARRAY_TYPE is not possible,
11467 as functions may not return an array, so don't bother trying
11468 to support this (and then deal with VLAs etc.). */
11469 error_at (loc, "%<__builtin_bit_cast%> destination type %qT "
11470 "is an array type", type);
11471 return error_mark_node;
11473 if (!trivially_copyable_p (type))
11475 error_at (loc, "%<__builtin_bit_cast%> destination type %qT "
11476 "is not trivially copyable", type);
11477 return error_mark_node;
11481 if (error_operand_p (arg))
11482 return error_mark_node;
11484 if (!type_dependent_expression_p (arg))
11486 if (TREE_CODE (TREE_TYPE (arg)) == ARRAY_TYPE)
11488 /* Don't perform array-to-pointer conversion. */
11489 arg = mark_rvalue_use (arg, loc, true);
11490 if (!complete_type_or_maybe_complain (TREE_TYPE (arg), arg, complain))
11491 return error_mark_node;
11493 else
11494 arg = decay_conversion (arg, complain);
11496 if (error_operand_p (arg))
11497 return error_mark_node;
11499 if (!trivially_copyable_p (TREE_TYPE (arg)))
11501 error_at (cp_expr_loc_or_loc (arg, loc),
11502 "%<__builtin_bit_cast%> source type %qT "
11503 "is not trivially copyable", TREE_TYPE (arg));
11504 return error_mark_node;
11506 if (!dependent_type_p (type)
11507 && !cp_tree_equal (TYPE_SIZE_UNIT (type),
11508 TYPE_SIZE_UNIT (TREE_TYPE (arg))))
11510 error_at (loc, "%<__builtin_bit_cast%> source size %qE "
11511 "not equal to destination type size %qE",
11512 TYPE_SIZE_UNIT (TREE_TYPE (arg)),
11513 TYPE_SIZE_UNIT (type));
11514 return error_mark_node;
11518 tree ret = build_min (BIT_CAST_EXPR, type, arg);
11519 SET_EXPR_LOCATION (ret, loc);
11521 if (!processing_template_decl && CLASS_TYPE_P (type))
11522 ret = get_target_expr_sfinae (ret, complain);
11524 return ret;
11527 #include "gt-cp-semantics.h"