vec: use auto_vec in a few more places
[official-gcc.git] / gcc / cp / semantics.c
blobb97dc1f6624f143c85c9090d692552e213419f91
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 (scope_chain->omp_declare_target_attribute)
3276 if (!errorcount)
3277 error ("%<#pragma omp declare target%> without corresponding "
3278 "%<#pragma omp end declare target%>");
3279 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 return true;
6075 tree size = size_binop (EXACT_DIV_EXPR, TYPE_SIZE_UNIT (oatype),
6076 TYPE_SIZE_UNIT (type));
6077 if (integer_zerop (size))
6079 error_at (OMP_CLAUSE_LOCATION (c),
6080 "%qE in %<reduction%> clause is a zero size array",
6081 omp_clause_printable_decl (t));
6082 return true;
6084 size = size_binop (MINUS_EXPR, size, size_one_node);
6085 size = save_expr (size);
6086 tree index_type = build_index_type (size);
6087 tree atype = build_array_type (type, index_type);
6088 tree ptype = build_pointer_type (type);
6089 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
6090 t = build_fold_addr_expr (t);
6091 t = build2 (MEM_REF, atype, t, build_int_cst (ptype, 0));
6092 OMP_CLAUSE_DECL (c) = t;
6095 if (type == error_mark_node)
6096 return true;
6097 else if (ARITHMETIC_TYPE_P (type))
6098 switch (OMP_CLAUSE_REDUCTION_CODE (c))
6100 case PLUS_EXPR:
6101 case MULT_EXPR:
6102 case MINUS_EXPR:
6103 case TRUTH_ANDIF_EXPR:
6104 case TRUTH_ORIF_EXPR:
6105 predefined = true;
6106 break;
6107 case MIN_EXPR:
6108 case MAX_EXPR:
6109 if (TREE_CODE (type) == COMPLEX_TYPE)
6110 break;
6111 predefined = true;
6112 break;
6113 case BIT_AND_EXPR:
6114 case BIT_IOR_EXPR:
6115 case BIT_XOR_EXPR:
6116 if (FLOAT_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
6117 break;
6118 predefined = true;
6119 break;
6120 default:
6121 break;
6123 else if (TYPE_READONLY (type))
6125 error_at (OMP_CLAUSE_LOCATION (c),
6126 "%qE has const type for %<reduction%>",
6127 omp_clause_printable_decl (t));
6128 return true;
6130 else if (!processing_template_decl)
6132 t = require_complete_type (t);
6133 if (t == error_mark_node)
6134 return true;
6135 OMP_CLAUSE_DECL (c) = t;
6138 if (predefined)
6140 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = NULL_TREE;
6141 return false;
6143 else if (processing_template_decl)
6145 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == error_mark_node)
6146 return true;
6147 return false;
6150 tree id = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
6152 type = TYPE_MAIN_VARIANT (type);
6153 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = NULL_TREE;
6154 if (id == NULL_TREE)
6155 id = omp_reduction_id (OMP_CLAUSE_REDUCTION_CODE (c),
6156 NULL_TREE, NULL_TREE);
6157 id = omp_reduction_lookup (OMP_CLAUSE_LOCATION (c), id, type, NULL, NULL);
6158 if (id)
6160 if (id == error_mark_node)
6161 return true;
6162 mark_used (id);
6163 tree body = DECL_SAVED_TREE (id);
6164 if (!body)
6165 return true;
6166 if (TREE_CODE (body) == STATEMENT_LIST)
6168 tree_stmt_iterator tsi;
6169 tree placeholder = NULL_TREE, decl_placeholder = NULL_TREE;
6170 int i;
6171 tree stmts[7];
6172 tree atype = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (id)));
6173 atype = TREE_TYPE (atype);
6174 bool need_static_cast = !same_type_p (type, atype);
6175 memset (stmts, 0, sizeof stmts);
6176 for (i = 0, tsi = tsi_start (body);
6177 i < 7 && !tsi_end_p (tsi);
6178 i++, tsi_next (&tsi))
6179 stmts[i] = tsi_stmt (tsi);
6180 gcc_assert (tsi_end_p (tsi));
6182 if (i >= 3)
6184 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
6185 && TREE_CODE (stmts[1]) == DECL_EXPR);
6186 placeholder = build_lang_decl (VAR_DECL, NULL_TREE, type);
6187 DECL_ARTIFICIAL (placeholder) = 1;
6188 DECL_IGNORED_P (placeholder) = 1;
6189 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
6190 if (TREE_CODE (t) == MEM_REF)
6192 decl_placeholder = build_lang_decl (VAR_DECL, NULL_TREE,
6193 type);
6194 DECL_ARTIFICIAL (decl_placeholder) = 1;
6195 DECL_IGNORED_P (decl_placeholder) = 1;
6196 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c) = decl_placeholder;
6198 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[0])))
6199 cxx_mark_addressable (placeholder);
6200 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[1]))
6201 && (decl_placeholder
6202 || !TYPE_REF_P (TREE_TYPE (OMP_CLAUSE_DECL (c)))))
6203 cxx_mark_addressable (decl_placeholder ? decl_placeholder
6204 : OMP_CLAUSE_DECL (c));
6205 tree omp_out = placeholder;
6206 tree omp_in = decl_placeholder ? decl_placeholder
6207 : convert_from_reference (OMP_CLAUSE_DECL (c));
6208 if (need_static_cast)
6210 tree rtype = build_reference_type (atype);
6211 omp_out = build_static_cast (input_location,
6212 rtype, omp_out,
6213 tf_warning_or_error);
6214 omp_in = build_static_cast (input_location,
6215 rtype, omp_in,
6216 tf_warning_or_error);
6217 if (omp_out == error_mark_node || omp_in == error_mark_node)
6218 return true;
6219 omp_out = convert_from_reference (omp_out);
6220 omp_in = convert_from_reference (omp_in);
6222 OMP_CLAUSE_REDUCTION_MERGE (c)
6223 = clone_omp_udr (stmts[2], DECL_EXPR_DECL (stmts[0]),
6224 DECL_EXPR_DECL (stmts[1]), omp_in, omp_out);
6226 if (i >= 6)
6228 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
6229 && TREE_CODE (stmts[4]) == DECL_EXPR);
6230 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[3]))
6231 && (decl_placeholder
6232 || !TYPE_REF_P (TREE_TYPE (OMP_CLAUSE_DECL (c)))))
6233 cxx_mark_addressable (decl_placeholder ? decl_placeholder
6234 : OMP_CLAUSE_DECL (c));
6235 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[4])))
6236 cxx_mark_addressable (placeholder);
6237 tree omp_priv = decl_placeholder ? decl_placeholder
6238 : convert_from_reference (OMP_CLAUSE_DECL (c));
6239 tree omp_orig = placeholder;
6240 if (need_static_cast)
6242 if (i == 7)
6244 error_at (OMP_CLAUSE_LOCATION (c),
6245 "user defined reduction with constructor "
6246 "initializer for base class %qT", atype);
6247 return true;
6249 tree rtype = build_reference_type (atype);
6250 omp_priv = build_static_cast (input_location,
6251 rtype, omp_priv,
6252 tf_warning_or_error);
6253 omp_orig = build_static_cast (input_location,
6254 rtype, omp_orig,
6255 tf_warning_or_error);
6256 if (omp_priv == error_mark_node
6257 || omp_orig == error_mark_node)
6258 return true;
6259 omp_priv = convert_from_reference (omp_priv);
6260 omp_orig = convert_from_reference (omp_orig);
6262 if (i == 6)
6263 *need_default_ctor = true;
6264 OMP_CLAUSE_REDUCTION_INIT (c)
6265 = clone_omp_udr (stmts[5], DECL_EXPR_DECL (stmts[4]),
6266 DECL_EXPR_DECL (stmts[3]),
6267 omp_priv, omp_orig);
6268 if (cp_walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
6269 find_omp_placeholder_r, placeholder, NULL))
6270 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
6272 else if (i >= 3)
6274 if (CLASS_TYPE_P (type) && !pod_type_p (type))
6275 *need_default_ctor = true;
6276 else
6278 tree init;
6279 tree v = decl_placeholder ? decl_placeholder
6280 : convert_from_reference (t);
6281 if (AGGREGATE_TYPE_P (TREE_TYPE (v)))
6282 init = build_constructor (TREE_TYPE (v), NULL);
6283 else
6284 init = fold_convert (TREE_TYPE (v), integer_zero_node);
6285 OMP_CLAUSE_REDUCTION_INIT (c)
6286 = build2 (INIT_EXPR, TREE_TYPE (v), v, init);
6291 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
6292 *need_dtor = true;
6293 else
6295 error_at (OMP_CLAUSE_LOCATION (c),
6296 "user defined reduction not found for %qE",
6297 omp_clause_printable_decl (t));
6298 return true;
6300 if (TREE_CODE (OMP_CLAUSE_DECL (c)) == MEM_REF)
6301 gcc_assert (TYPE_SIZE_UNIT (type)
6302 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST);
6303 return false;
6306 /* Called from finish_struct_1. linear(this) or linear(this:step)
6307 clauses might not be finalized yet because the class has been incomplete
6308 when parsing #pragma omp declare simd methods. Fix those up now. */
6310 void
6311 finish_omp_declare_simd_methods (tree t)
6313 if (processing_template_decl)
6314 return;
6316 for (tree x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
6318 if (TREE_CODE (x) == USING_DECL
6319 || !DECL_NONSTATIC_MEMBER_FUNCTION_P (x))
6320 continue;
6321 tree ods = lookup_attribute ("omp declare simd", DECL_ATTRIBUTES (x));
6322 if (!ods || !TREE_VALUE (ods))
6323 continue;
6324 for (tree c = TREE_VALUE (TREE_VALUE (ods)); c; c = OMP_CLAUSE_CHAIN (c))
6325 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
6326 && integer_zerop (OMP_CLAUSE_DECL (c))
6327 && OMP_CLAUSE_LINEAR_STEP (c)
6328 && TYPE_PTR_P (TREE_TYPE (OMP_CLAUSE_LINEAR_STEP (c))))
6330 tree s = OMP_CLAUSE_LINEAR_STEP (c);
6331 s = fold_convert_loc (OMP_CLAUSE_LOCATION (c), sizetype, s);
6332 s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MULT_EXPR,
6333 sizetype, s, TYPE_SIZE_UNIT (t));
6334 OMP_CLAUSE_LINEAR_STEP (c) = s;
6339 /* Adjust sink depend clause to take into account pointer offsets.
6341 Return TRUE if there was a problem processing the offset, and the
6342 whole clause should be removed. */
6344 static bool
6345 cp_finish_omp_clause_depend_sink (tree sink_clause)
6347 tree t = OMP_CLAUSE_DECL (sink_clause);
6348 gcc_assert (TREE_CODE (t) == TREE_LIST);
6350 /* Make sure we don't adjust things twice for templates. */
6351 if (processing_template_decl)
6352 return false;
6354 for (; t; t = TREE_CHAIN (t))
6356 tree decl = TREE_VALUE (t);
6357 if (TYPE_PTR_P (TREE_TYPE (decl)))
6359 tree offset = TREE_PURPOSE (t);
6360 bool neg = wi::neg_p (wi::to_wide (offset));
6361 offset = fold_unary (ABS_EXPR, TREE_TYPE (offset), offset);
6362 decl = mark_rvalue_use (decl);
6363 decl = convert_from_reference (decl);
6364 tree t2 = pointer_int_sum (OMP_CLAUSE_LOCATION (sink_clause),
6365 neg ? MINUS_EXPR : PLUS_EXPR,
6366 decl, offset);
6367 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (sink_clause),
6368 MINUS_EXPR, sizetype,
6369 fold_convert (sizetype, t2),
6370 fold_convert (sizetype, decl));
6371 if (t2 == error_mark_node)
6372 return true;
6373 TREE_PURPOSE (t) = t2;
6376 return false;
6379 /* Finish OpenMP iterators ITER. Return true if they are errorneous
6380 and clauses containing them should be removed. */
6382 static bool
6383 cp_omp_finish_iterators (tree iter)
6385 bool ret = false;
6386 for (tree it = iter; it; it = TREE_CHAIN (it))
6388 tree var = TREE_VEC_ELT (it, 0);
6389 tree begin = TREE_VEC_ELT (it, 1);
6390 tree end = TREE_VEC_ELT (it, 2);
6391 tree step = TREE_VEC_ELT (it, 3);
6392 tree orig_step;
6393 tree type = TREE_TYPE (var);
6394 location_t loc = DECL_SOURCE_LOCATION (var);
6395 if (type == error_mark_node)
6397 ret = true;
6398 continue;
6400 if (type_dependent_expression_p (var))
6401 continue;
6402 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
6404 error_at (loc, "iterator %qD has neither integral nor pointer type",
6405 var);
6406 ret = true;
6407 continue;
6409 else if (TYPE_READONLY (type))
6411 error_at (loc, "iterator %qD has const qualified type", var);
6412 ret = true;
6413 continue;
6415 if (type_dependent_expression_p (begin)
6416 || type_dependent_expression_p (end)
6417 || type_dependent_expression_p (step))
6418 continue;
6419 else if (error_operand_p (step))
6421 ret = true;
6422 continue;
6424 else if (!INTEGRAL_TYPE_P (TREE_TYPE (step)))
6426 error_at (EXPR_LOC_OR_LOC (step, loc),
6427 "iterator step with non-integral type");
6428 ret = true;
6429 continue;
6432 begin = mark_rvalue_use (begin);
6433 end = mark_rvalue_use (end);
6434 step = mark_rvalue_use (step);
6435 begin = cp_build_c_cast (input_location, type, begin,
6436 tf_warning_or_error);
6437 end = cp_build_c_cast (input_location, type, end,
6438 tf_warning_or_error);
6439 orig_step = step;
6440 if (!processing_template_decl)
6441 step = orig_step = save_expr (step);
6442 tree stype = POINTER_TYPE_P (type) ? sizetype : type;
6443 step = cp_build_c_cast (input_location, stype, step,
6444 tf_warning_or_error);
6445 if (POINTER_TYPE_P (type) && !processing_template_decl)
6447 begin = save_expr (begin);
6448 step = pointer_int_sum (loc, PLUS_EXPR, begin, step);
6449 step = fold_build2_loc (loc, MINUS_EXPR, sizetype,
6450 fold_convert (sizetype, step),
6451 fold_convert (sizetype, begin));
6452 step = fold_convert (ssizetype, step);
6454 if (!processing_template_decl)
6456 begin = maybe_constant_value (begin);
6457 end = maybe_constant_value (end);
6458 step = maybe_constant_value (step);
6459 orig_step = maybe_constant_value (orig_step);
6461 if (integer_zerop (step))
6463 error_at (loc, "iterator %qD has zero step", var);
6464 ret = true;
6465 continue;
6468 if (begin == error_mark_node
6469 || end == error_mark_node
6470 || step == error_mark_node
6471 || orig_step == error_mark_node)
6473 ret = true;
6474 continue;
6477 if (!processing_template_decl)
6479 begin = fold_build_cleanup_point_expr (TREE_TYPE (begin), begin);
6480 end = fold_build_cleanup_point_expr (TREE_TYPE (end), end);
6481 step = fold_build_cleanup_point_expr (TREE_TYPE (step), step);
6482 orig_step = fold_build_cleanup_point_expr (TREE_TYPE (orig_step),
6483 orig_step);
6485 hash_set<tree> pset;
6486 tree it2;
6487 for (it2 = TREE_CHAIN (it); it2; it2 = TREE_CHAIN (it2))
6489 tree var2 = TREE_VEC_ELT (it2, 0);
6490 tree begin2 = TREE_VEC_ELT (it2, 1);
6491 tree end2 = TREE_VEC_ELT (it2, 2);
6492 tree step2 = TREE_VEC_ELT (it2, 3);
6493 location_t loc2 = DECL_SOURCE_LOCATION (var2);
6494 if (cp_walk_tree (&begin2, find_omp_placeholder_r, var, &pset))
6496 error_at (EXPR_LOC_OR_LOC (begin2, loc2),
6497 "begin expression refers to outer iterator %qD", var);
6498 break;
6500 else if (cp_walk_tree (&end2, find_omp_placeholder_r, var, &pset))
6502 error_at (EXPR_LOC_OR_LOC (end2, loc2),
6503 "end expression refers to outer iterator %qD", var);
6504 break;
6506 else if (cp_walk_tree (&step2, find_omp_placeholder_r, var, &pset))
6508 error_at (EXPR_LOC_OR_LOC (step2, loc2),
6509 "step expression refers to outer iterator %qD", var);
6510 break;
6513 if (it2)
6515 ret = true;
6516 continue;
6518 TREE_VEC_ELT (it, 1) = begin;
6519 TREE_VEC_ELT (it, 2) = end;
6520 if (processing_template_decl)
6521 TREE_VEC_ELT (it, 3) = orig_step;
6522 else
6524 TREE_VEC_ELT (it, 3) = step;
6525 TREE_VEC_ELT (it, 4) = orig_step;
6528 return ret;
6531 /* Ensure that pointers are used in OpenACC attach and detach clauses.
6532 Return true if an error has been detected. */
6534 static bool
6535 cp_oacc_check_attachments (tree c)
6537 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
6538 return false;
6540 /* OpenACC attach / detach clauses must be pointers. */
6541 if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
6542 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH)
6544 tree t = OMP_CLAUSE_DECL (c);
6545 tree type;
6547 while (TREE_CODE (t) == TREE_LIST)
6548 t = TREE_CHAIN (t);
6550 type = TREE_TYPE (t);
6552 if (TREE_CODE (type) == REFERENCE_TYPE)
6553 type = TREE_TYPE (type);
6555 if (TREE_CODE (type) != POINTER_TYPE)
6557 error_at (OMP_CLAUSE_LOCATION (c), "expected pointer in %qs clause",
6558 c_omp_map_clause_name (c, true));
6559 return true;
6563 return false;
6566 /* For all elements of CLAUSES, validate them vs OpenMP constraints.
6567 Remove any elements from the list that are invalid. */
6569 tree
6570 finish_omp_clauses (tree clauses, enum c_omp_region_type ort)
6572 bitmap_head generic_head, firstprivate_head, lastprivate_head;
6573 bitmap_head aligned_head, map_head, map_field_head, map_firstprivate_head;
6574 bitmap_head oacc_reduction_head;
6575 tree c, t, *pc;
6576 tree safelen = NULL_TREE;
6577 bool branch_seen = false;
6578 bool copyprivate_seen = false;
6579 bool ordered_seen = false;
6580 bool order_seen = false;
6581 bool schedule_seen = false;
6582 bool oacc_async = false;
6583 tree last_iterators = NULL_TREE;
6584 bool last_iterators_remove = false;
6585 /* 1 if normal/task reduction has been seen, -1 if inscan reduction
6586 has been seen, -2 if mixed inscan/normal reduction diagnosed. */
6587 int reduction_seen = 0;
6588 bool allocate_seen = false;
6589 tree detach_seen = NULL_TREE;
6590 bool mergeable_seen = false;
6591 bool implicit_moved = false;
6592 bool target_in_reduction_seen = false;
6594 bitmap_obstack_initialize (NULL);
6595 bitmap_initialize (&generic_head, &bitmap_default_obstack);
6596 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
6597 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
6598 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
6599 /* If ort == C_ORT_OMP_DECLARE_SIMD used as uniform_head instead. */
6600 bitmap_initialize (&map_head, &bitmap_default_obstack);
6601 bitmap_initialize (&map_field_head, &bitmap_default_obstack);
6602 bitmap_initialize (&map_firstprivate_head, &bitmap_default_obstack);
6603 /* If ort == C_ORT_OMP used as nontemporal_head or use_device_xxx_head
6604 instead and for ort == C_ORT_OMP_TARGET used as in_reduction_head. */
6605 bitmap_initialize (&oacc_reduction_head, &bitmap_default_obstack);
6607 if (ort & C_ORT_ACC)
6608 for (c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
6609 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ASYNC)
6611 oacc_async = true;
6612 break;
6615 for (pc = &clauses, c = clauses; c ; c = *pc)
6617 bool remove = false;
6618 bool field_ok = false;
6620 switch (OMP_CLAUSE_CODE (c))
6622 case OMP_CLAUSE_SHARED:
6623 field_ok = ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP);
6624 goto check_dup_generic;
6625 case OMP_CLAUSE_PRIVATE:
6626 field_ok = ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP);
6627 goto check_dup_generic;
6628 case OMP_CLAUSE_REDUCTION:
6629 if (reduction_seen == 0)
6630 reduction_seen = OMP_CLAUSE_REDUCTION_INSCAN (c) ? -1 : 1;
6631 else if (reduction_seen != -2
6632 && reduction_seen != (OMP_CLAUSE_REDUCTION_INSCAN (c)
6633 ? -1 : 1))
6635 error_at (OMP_CLAUSE_LOCATION (c),
6636 "%<inscan%> and non-%<inscan%> %<reduction%> clauses "
6637 "on the same construct");
6638 reduction_seen = -2;
6640 /* FALLTHRU */
6641 case OMP_CLAUSE_IN_REDUCTION:
6642 case OMP_CLAUSE_TASK_REDUCTION:
6643 field_ok = ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP);
6644 t = OMP_CLAUSE_DECL (c);
6645 if (TREE_CODE (t) == TREE_LIST)
6647 if (handle_omp_array_sections (c, ort))
6649 remove = true;
6650 break;
6652 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
6653 && OMP_CLAUSE_REDUCTION_INSCAN (c))
6655 error_at (OMP_CLAUSE_LOCATION (c),
6656 "%<inscan%> %<reduction%> clause with array "
6657 "section");
6658 remove = true;
6659 break;
6661 if (TREE_CODE (t) == TREE_LIST)
6663 while (TREE_CODE (t) == TREE_LIST)
6664 t = TREE_CHAIN (t);
6666 else
6668 gcc_assert (TREE_CODE (t) == MEM_REF);
6669 t = TREE_OPERAND (t, 0);
6670 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
6671 t = TREE_OPERAND (t, 0);
6672 if (TREE_CODE (t) == ADDR_EXPR
6673 || INDIRECT_REF_P (t))
6674 t = TREE_OPERAND (t, 0);
6676 tree n = omp_clause_decl_field (t);
6677 if (n)
6678 t = n;
6679 goto check_dup_generic_t;
6681 if (oacc_async)
6682 cxx_mark_addressable (t);
6683 goto check_dup_generic;
6684 case OMP_CLAUSE_COPYPRIVATE:
6685 copyprivate_seen = true;
6686 field_ok = ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP);
6687 goto check_dup_generic;
6688 case OMP_CLAUSE_COPYIN:
6689 goto check_dup_generic;
6690 case OMP_CLAUSE_LINEAR:
6691 field_ok = ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP);
6692 t = OMP_CLAUSE_DECL (c);
6693 if (ort != C_ORT_OMP_DECLARE_SIMD
6694 && OMP_CLAUSE_LINEAR_KIND (c) != OMP_CLAUSE_LINEAR_DEFAULT)
6696 error_at (OMP_CLAUSE_LOCATION (c),
6697 "modifier should not be specified in %<linear%> "
6698 "clause on %<simd%> or %<for%> constructs");
6699 OMP_CLAUSE_LINEAR_KIND (c) = OMP_CLAUSE_LINEAR_DEFAULT;
6701 if ((VAR_P (t) || TREE_CODE (t) == PARM_DECL)
6702 && !type_dependent_expression_p (t))
6704 tree type = TREE_TYPE (t);
6705 if ((OMP_CLAUSE_LINEAR_KIND (c) == OMP_CLAUSE_LINEAR_REF
6706 || OMP_CLAUSE_LINEAR_KIND (c) == OMP_CLAUSE_LINEAR_UVAL)
6707 && !TYPE_REF_P (type))
6709 error_at (OMP_CLAUSE_LOCATION (c),
6710 "linear clause with %qs modifier applied to "
6711 "non-reference variable with %qT type",
6712 OMP_CLAUSE_LINEAR_KIND (c) == OMP_CLAUSE_LINEAR_REF
6713 ? "ref" : "uval", TREE_TYPE (t));
6714 remove = true;
6715 break;
6717 if (TYPE_REF_P (type))
6718 type = TREE_TYPE (type);
6719 if (OMP_CLAUSE_LINEAR_KIND (c) != OMP_CLAUSE_LINEAR_REF)
6721 if (!INTEGRAL_TYPE_P (type)
6722 && !TYPE_PTR_P (type))
6724 error_at (OMP_CLAUSE_LOCATION (c),
6725 "linear clause applied to non-integral "
6726 "non-pointer variable with %qT type",
6727 TREE_TYPE (t));
6728 remove = true;
6729 break;
6733 t = OMP_CLAUSE_LINEAR_STEP (c);
6734 if (t == NULL_TREE)
6735 t = integer_one_node;
6736 if (t == error_mark_node)
6738 remove = true;
6739 break;
6741 else if (!type_dependent_expression_p (t)
6742 && !INTEGRAL_TYPE_P (TREE_TYPE (t))
6743 && (ort != C_ORT_OMP_DECLARE_SIMD
6744 || TREE_CODE (t) != PARM_DECL
6745 || !TYPE_REF_P (TREE_TYPE (t))
6746 || !INTEGRAL_TYPE_P (TREE_TYPE (TREE_TYPE (t)))))
6748 error_at (OMP_CLAUSE_LOCATION (c),
6749 "linear step expression must be integral");
6750 remove = true;
6751 break;
6753 else
6755 t = mark_rvalue_use (t);
6756 if (ort == C_ORT_OMP_DECLARE_SIMD && TREE_CODE (t) == PARM_DECL)
6758 OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c) = 1;
6759 goto check_dup_generic;
6761 if (!processing_template_decl
6762 && (VAR_P (OMP_CLAUSE_DECL (c))
6763 || TREE_CODE (OMP_CLAUSE_DECL (c)) == PARM_DECL))
6765 if (ort == C_ORT_OMP_DECLARE_SIMD)
6767 t = maybe_constant_value (t);
6768 if (TREE_CODE (t) != INTEGER_CST)
6770 error_at (OMP_CLAUSE_LOCATION (c),
6771 "%<linear%> clause step %qE is neither "
6772 "constant nor a parameter", t);
6773 remove = true;
6774 break;
6777 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6778 tree type = TREE_TYPE (OMP_CLAUSE_DECL (c));
6779 if (TYPE_REF_P (type))
6780 type = TREE_TYPE (type);
6781 if (OMP_CLAUSE_LINEAR_KIND (c) == OMP_CLAUSE_LINEAR_REF)
6783 type = build_pointer_type (type);
6784 tree d = fold_convert (type, OMP_CLAUSE_DECL (c));
6785 t = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
6786 d, t);
6787 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c),
6788 MINUS_EXPR, sizetype,
6789 fold_convert (sizetype, t),
6790 fold_convert (sizetype, d));
6791 if (t == error_mark_node)
6793 remove = true;
6794 break;
6797 else if (TYPE_PTR_P (type)
6798 /* Can't multiply the step yet if *this
6799 is still incomplete type. */
6800 && (ort != C_ORT_OMP_DECLARE_SIMD
6801 || TREE_CODE (OMP_CLAUSE_DECL (c)) != PARM_DECL
6802 || !DECL_ARTIFICIAL (OMP_CLAUSE_DECL (c))
6803 || DECL_NAME (OMP_CLAUSE_DECL (c))
6804 != this_identifier
6805 || !TYPE_BEING_DEFINED (TREE_TYPE (type))))
6807 tree d = convert_from_reference (OMP_CLAUSE_DECL (c));
6808 t = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
6809 d, t);
6810 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c),
6811 MINUS_EXPR, sizetype,
6812 fold_convert (sizetype, t),
6813 fold_convert (sizetype, d));
6814 if (t == error_mark_node)
6816 remove = true;
6817 break;
6820 else
6821 t = fold_convert (type, t);
6823 OMP_CLAUSE_LINEAR_STEP (c) = t;
6825 goto check_dup_generic;
6826 check_dup_generic:
6827 t = omp_clause_decl_field (OMP_CLAUSE_DECL (c));
6828 if (t)
6830 if (!remove && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_SHARED)
6831 omp_note_field_privatization (t, OMP_CLAUSE_DECL (c));
6833 else
6834 t = OMP_CLAUSE_DECL (c);
6835 check_dup_generic_t:
6836 if (t == current_class_ptr
6837 && ((ort != C_ORT_OMP_DECLARE_SIMD && ort != C_ORT_ACC)
6838 || (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_LINEAR
6839 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_UNIFORM)))
6841 error_at (OMP_CLAUSE_LOCATION (c),
6842 "%<this%> allowed in OpenMP only in %<declare simd%>"
6843 " clauses");
6844 remove = true;
6845 break;
6847 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL
6848 && (!field_ok || TREE_CODE (t) != FIELD_DECL))
6850 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
6851 break;
6852 if (DECL_P (t))
6853 error_at (OMP_CLAUSE_LOCATION (c),
6854 "%qD is not a variable in clause %qs", t,
6855 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6856 else
6857 error_at (OMP_CLAUSE_LOCATION (c),
6858 "%qE is not a variable in clause %qs", t,
6859 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6860 remove = true;
6862 else if ((ort == C_ORT_ACC
6863 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
6864 || (ort == C_ORT_OMP
6865 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_USE_DEVICE_PTR
6866 || (OMP_CLAUSE_CODE (c)
6867 == OMP_CLAUSE_USE_DEVICE_ADDR)))
6868 || (ort == C_ORT_OMP_TARGET
6869 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION))
6871 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
6872 && (bitmap_bit_p (&generic_head, DECL_UID (t))
6873 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))))
6875 error_at (OMP_CLAUSE_LOCATION (c),
6876 "%qD appears more than once in data-sharing "
6877 "clauses", t);
6878 remove = true;
6879 break;
6881 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION)
6882 target_in_reduction_seen = true;
6883 if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
6885 error_at (OMP_CLAUSE_LOCATION (c),
6886 ort == C_ORT_ACC
6887 ? "%qD appears more than once in reduction clauses"
6888 : "%qD appears more than once in data clauses",
6890 remove = true;
6892 else
6893 bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
6895 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
6896 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
6897 || bitmap_bit_p (&lastprivate_head, DECL_UID (t))
6898 || bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
6900 error_at (OMP_CLAUSE_LOCATION (c),
6901 "%qD appears more than once in data clauses", t);
6902 remove = true;
6904 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
6905 && bitmap_bit_p (&map_head, DECL_UID (t)))
6907 if (ort == C_ORT_ACC)
6908 error_at (OMP_CLAUSE_LOCATION (c),
6909 "%qD appears more than once in data clauses", t);
6910 else
6911 error_at (OMP_CLAUSE_LOCATION (c),
6912 "%qD appears both in data and map clauses", t);
6913 remove = true;
6915 else
6916 bitmap_set_bit (&generic_head, DECL_UID (t));
6917 if (!field_ok)
6918 break;
6919 handle_field_decl:
6920 if (!remove
6921 && TREE_CODE (t) == FIELD_DECL
6922 && t == OMP_CLAUSE_DECL (c))
6924 OMP_CLAUSE_DECL (c)
6925 = omp_privatize_field (t, (OMP_CLAUSE_CODE (c)
6926 == OMP_CLAUSE_SHARED));
6927 if (OMP_CLAUSE_DECL (c) == error_mark_node)
6928 remove = true;
6930 break;
6932 case OMP_CLAUSE_FIRSTPRIVATE:
6933 if (OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c) && !implicit_moved)
6935 move_implicit:
6936 implicit_moved = true;
6937 /* Move firstprivate and map clauses with
6938 OMP_CLAUSE_{FIRSTPRIVATE,MAP}_IMPLICIT set to the end of
6939 clauses chain. */
6940 tree cl1 = NULL_TREE, cl2 = NULL_TREE;
6941 tree *pc1 = pc, *pc2 = &cl1, *pc3 = &cl2;
6942 while (*pc1)
6943 if (OMP_CLAUSE_CODE (*pc1) == OMP_CLAUSE_FIRSTPRIVATE
6944 && OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (*pc1))
6946 *pc3 = *pc1;
6947 pc3 = &OMP_CLAUSE_CHAIN (*pc3);
6948 *pc1 = OMP_CLAUSE_CHAIN (*pc1);
6950 else if (OMP_CLAUSE_CODE (*pc1) == OMP_CLAUSE_MAP
6951 && OMP_CLAUSE_MAP_IMPLICIT (*pc1))
6953 *pc2 = *pc1;
6954 pc2 = &OMP_CLAUSE_CHAIN (*pc2);
6955 *pc1 = OMP_CLAUSE_CHAIN (*pc1);
6957 else
6958 pc1 = &OMP_CLAUSE_CHAIN (*pc1);
6959 *pc3 = NULL;
6960 *pc2 = cl2;
6961 *pc1 = cl1;
6962 continue;
6964 t = omp_clause_decl_field (OMP_CLAUSE_DECL (c));
6965 if (t)
6966 omp_note_field_privatization (t, OMP_CLAUSE_DECL (c));
6967 else
6968 t = OMP_CLAUSE_DECL (c);
6969 if (ort != C_ORT_ACC && t == current_class_ptr)
6971 error_at (OMP_CLAUSE_LOCATION (c),
6972 "%<this%> allowed in OpenMP only in %<declare simd%>"
6973 " clauses");
6974 remove = true;
6975 break;
6977 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL
6978 && ((ort & C_ORT_OMP_DECLARE_SIMD) != C_ORT_OMP
6979 || TREE_CODE (t) != FIELD_DECL))
6981 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
6982 break;
6983 if (DECL_P (t))
6984 error_at (OMP_CLAUSE_LOCATION (c),
6985 "%qD is not a variable in clause %<firstprivate%>",
6987 else
6988 error_at (OMP_CLAUSE_LOCATION (c),
6989 "%qE is not a variable in clause %<firstprivate%>",
6991 remove = true;
6993 else if (OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c)
6994 && !OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT_TARGET (c)
6995 && bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
6996 remove = true;
6997 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
6998 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
6999 || bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
7001 error_at (OMP_CLAUSE_LOCATION (c),
7002 "%qD appears more than once in data clauses", t);
7003 remove = true;
7005 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
7007 if (ort == C_ORT_ACC)
7008 error_at (OMP_CLAUSE_LOCATION (c),
7009 "%qD appears more than once in data clauses", t);
7010 else if (OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c)
7011 && !OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT_TARGET (c))
7012 /* Silently drop the clause. */;
7013 else
7014 error_at (OMP_CLAUSE_LOCATION (c),
7015 "%qD appears both in data and map clauses", t);
7016 remove = true;
7018 else
7019 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
7020 goto handle_field_decl;
7022 case OMP_CLAUSE_LASTPRIVATE:
7023 t = omp_clause_decl_field (OMP_CLAUSE_DECL (c));
7024 if (t)
7025 omp_note_field_privatization (t, OMP_CLAUSE_DECL (c));
7026 else
7027 t = OMP_CLAUSE_DECL (c);
7028 if (ort != C_ORT_ACC && t == current_class_ptr)
7030 error_at (OMP_CLAUSE_LOCATION (c),
7031 "%<this%> allowed in OpenMP only in %<declare simd%>"
7032 " clauses");
7033 remove = true;
7034 break;
7036 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL
7037 && ((ort & C_ORT_OMP_DECLARE_SIMD) != C_ORT_OMP
7038 || TREE_CODE (t) != FIELD_DECL))
7040 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
7041 break;
7042 if (DECL_P (t))
7043 error_at (OMP_CLAUSE_LOCATION (c),
7044 "%qD is not a variable in clause %<lastprivate%>",
7046 else
7047 error_at (OMP_CLAUSE_LOCATION (c),
7048 "%qE is not a variable in clause %<lastprivate%>",
7050 remove = true;
7052 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
7053 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
7055 error_at (OMP_CLAUSE_LOCATION (c),
7056 "%qD appears more than once in data clauses", t);
7057 remove = true;
7059 else
7060 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
7061 goto handle_field_decl;
7063 case OMP_CLAUSE_IF:
7064 t = OMP_CLAUSE_IF_EXPR (c);
7065 t = maybe_convert_cond (t);
7066 if (t == error_mark_node)
7067 remove = true;
7068 else if (!processing_template_decl)
7069 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7070 OMP_CLAUSE_IF_EXPR (c) = t;
7071 break;
7073 case OMP_CLAUSE_FINAL:
7074 t = OMP_CLAUSE_FINAL_EXPR (c);
7075 t = maybe_convert_cond (t);
7076 if (t == error_mark_node)
7077 remove = true;
7078 else if (!processing_template_decl)
7079 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7080 OMP_CLAUSE_FINAL_EXPR (c) = t;
7081 break;
7083 case OMP_CLAUSE_GANG:
7084 /* Operand 1 is the gang static: argument. */
7085 t = OMP_CLAUSE_OPERAND (c, 1);
7086 if (t != NULL_TREE)
7088 if (t == error_mark_node)
7089 remove = true;
7090 else if (!type_dependent_expression_p (t)
7091 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
7093 error_at (OMP_CLAUSE_LOCATION (c),
7094 "%<gang%> static expression must be integral");
7095 remove = true;
7097 else
7099 t = mark_rvalue_use (t);
7100 if (!processing_template_decl)
7102 t = maybe_constant_value (t);
7103 if (TREE_CODE (t) == INTEGER_CST
7104 && tree_int_cst_sgn (t) != 1
7105 && t != integer_minus_one_node)
7107 warning_at (OMP_CLAUSE_LOCATION (c), 0,
7108 "%<gang%> static value must be "
7109 "positive");
7110 t = integer_one_node;
7112 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7115 OMP_CLAUSE_OPERAND (c, 1) = t;
7117 /* Check operand 0, the num argument. */
7118 /* FALLTHRU */
7120 case OMP_CLAUSE_WORKER:
7121 case OMP_CLAUSE_VECTOR:
7122 if (OMP_CLAUSE_OPERAND (c, 0) == NULL_TREE)
7123 break;
7124 /* FALLTHRU */
7126 case OMP_CLAUSE_NUM_TASKS:
7127 case OMP_CLAUSE_NUM_TEAMS:
7128 case OMP_CLAUSE_NUM_THREADS:
7129 case OMP_CLAUSE_NUM_GANGS:
7130 case OMP_CLAUSE_NUM_WORKERS:
7131 case OMP_CLAUSE_VECTOR_LENGTH:
7132 t = OMP_CLAUSE_OPERAND (c, 0);
7133 if (t == error_mark_node)
7134 remove = true;
7135 else if (!type_dependent_expression_p (t)
7136 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
7138 switch (OMP_CLAUSE_CODE (c))
7140 case OMP_CLAUSE_GANG:
7141 error_at (OMP_CLAUSE_LOCATION (c),
7142 "%<gang%> num expression must be integral"); break;
7143 case OMP_CLAUSE_VECTOR:
7144 error_at (OMP_CLAUSE_LOCATION (c),
7145 "%<vector%> length expression must be integral");
7146 break;
7147 case OMP_CLAUSE_WORKER:
7148 error_at (OMP_CLAUSE_LOCATION (c),
7149 "%<worker%> num expression must be integral");
7150 break;
7151 default:
7152 error_at (OMP_CLAUSE_LOCATION (c),
7153 "%qs expression must be integral",
7154 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7156 remove = true;
7158 else
7160 t = mark_rvalue_use (t);
7161 if (!processing_template_decl)
7163 t = maybe_constant_value (t);
7164 if (TREE_CODE (t) == INTEGER_CST
7165 && tree_int_cst_sgn (t) != 1)
7167 switch (OMP_CLAUSE_CODE (c))
7169 case OMP_CLAUSE_GANG:
7170 warning_at (OMP_CLAUSE_LOCATION (c), 0,
7171 "%<gang%> num value must be positive");
7172 break;
7173 case OMP_CLAUSE_VECTOR:
7174 warning_at (OMP_CLAUSE_LOCATION (c), 0,
7175 "%<vector%> length value must be "
7176 "positive");
7177 break;
7178 case OMP_CLAUSE_WORKER:
7179 warning_at (OMP_CLAUSE_LOCATION (c), 0,
7180 "%<worker%> num value must be "
7181 "positive");
7182 break;
7183 default:
7184 warning_at (OMP_CLAUSE_LOCATION (c), 0,
7185 "%qs value must be positive",
7186 omp_clause_code_name
7187 [OMP_CLAUSE_CODE (c)]);
7189 t = integer_one_node;
7191 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7193 OMP_CLAUSE_OPERAND (c, 0) = t;
7195 break;
7197 case OMP_CLAUSE_SCHEDULE:
7198 t = OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c);
7199 if (t == NULL)
7201 else if (t == error_mark_node)
7202 remove = true;
7203 else if (!type_dependent_expression_p (t)
7204 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
7206 error_at (OMP_CLAUSE_LOCATION (c),
7207 "schedule chunk size expression must be integral");
7208 remove = true;
7210 else
7212 t = mark_rvalue_use (t);
7213 if (!processing_template_decl)
7215 t = maybe_constant_value (t);
7216 if (TREE_CODE (t) == INTEGER_CST
7217 && tree_int_cst_sgn (t) != 1)
7219 warning_at (OMP_CLAUSE_LOCATION (c), 0,
7220 "chunk size value must be positive");
7221 t = integer_one_node;
7223 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7225 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
7227 if (!remove)
7228 schedule_seen = true;
7229 break;
7231 case OMP_CLAUSE_SIMDLEN:
7232 case OMP_CLAUSE_SAFELEN:
7233 t = OMP_CLAUSE_OPERAND (c, 0);
7234 if (t == error_mark_node)
7235 remove = true;
7236 else if (!type_dependent_expression_p (t)
7237 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
7239 error_at (OMP_CLAUSE_LOCATION (c),
7240 "%qs length expression must be integral",
7241 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7242 remove = true;
7244 else
7246 t = mark_rvalue_use (t);
7247 if (!processing_template_decl)
7249 t = maybe_constant_value (t);
7250 if (TREE_CODE (t) != INTEGER_CST
7251 || tree_int_cst_sgn (t) != 1)
7253 error_at (OMP_CLAUSE_LOCATION (c),
7254 "%qs length expression must be positive "
7255 "constant integer expression",
7256 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7257 remove = true;
7260 OMP_CLAUSE_OPERAND (c, 0) = t;
7261 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SAFELEN)
7262 safelen = c;
7264 break;
7266 case OMP_CLAUSE_ASYNC:
7267 t = OMP_CLAUSE_ASYNC_EXPR (c);
7268 if (t == error_mark_node)
7269 remove = true;
7270 else if (!type_dependent_expression_p (t)
7271 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
7273 error_at (OMP_CLAUSE_LOCATION (c),
7274 "%<async%> expression must be integral");
7275 remove = true;
7277 else
7279 t = mark_rvalue_use (t);
7280 if (!processing_template_decl)
7281 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7282 OMP_CLAUSE_ASYNC_EXPR (c) = t;
7284 break;
7286 case OMP_CLAUSE_WAIT:
7287 t = OMP_CLAUSE_WAIT_EXPR (c);
7288 if (t == error_mark_node)
7289 remove = true;
7290 else if (!processing_template_decl)
7291 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7292 OMP_CLAUSE_WAIT_EXPR (c) = t;
7293 break;
7295 case OMP_CLAUSE_THREAD_LIMIT:
7296 t = OMP_CLAUSE_THREAD_LIMIT_EXPR (c);
7297 if (t == error_mark_node)
7298 remove = true;
7299 else if (!type_dependent_expression_p (t)
7300 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
7302 error_at (OMP_CLAUSE_LOCATION (c),
7303 "%<thread_limit%> expression must be integral");
7304 remove = true;
7306 else
7308 t = mark_rvalue_use (t);
7309 if (!processing_template_decl)
7311 t = maybe_constant_value (t);
7312 if (TREE_CODE (t) == INTEGER_CST
7313 && tree_int_cst_sgn (t) != 1)
7315 warning_at (OMP_CLAUSE_LOCATION (c), 0,
7316 "%<thread_limit%> value must be positive");
7317 t = integer_one_node;
7319 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7321 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
7323 break;
7325 case OMP_CLAUSE_DEVICE:
7326 t = OMP_CLAUSE_DEVICE_ID (c);
7327 if (t == error_mark_node)
7328 remove = true;
7329 else if (!type_dependent_expression_p (t)
7330 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
7332 error_at (OMP_CLAUSE_LOCATION (c),
7333 "%<device%> id must be integral");
7334 remove = true;
7336 else
7338 t = mark_rvalue_use (t);
7339 if (!processing_template_decl)
7340 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7341 OMP_CLAUSE_DEVICE_ID (c) = t;
7343 break;
7345 case OMP_CLAUSE_DIST_SCHEDULE:
7346 t = OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c);
7347 if (t == NULL)
7349 else if (t == error_mark_node)
7350 remove = true;
7351 else if (!type_dependent_expression_p (t)
7352 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
7354 error_at (OMP_CLAUSE_LOCATION (c),
7355 "%<dist_schedule%> chunk size expression must be "
7356 "integral");
7357 remove = true;
7359 else
7361 t = mark_rvalue_use (t);
7362 if (!processing_template_decl)
7363 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7364 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
7366 break;
7368 case OMP_CLAUSE_ALIGNED:
7369 t = OMP_CLAUSE_DECL (c);
7370 if (t == current_class_ptr && ort != C_ORT_OMP_DECLARE_SIMD)
7372 error_at (OMP_CLAUSE_LOCATION (c),
7373 "%<this%> allowed in OpenMP only in %<declare simd%>"
7374 " clauses");
7375 remove = true;
7376 break;
7378 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
7380 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
7381 break;
7382 if (DECL_P (t))
7383 error_at (OMP_CLAUSE_LOCATION (c),
7384 "%qD is not a variable in %<aligned%> clause", t);
7385 else
7386 error_at (OMP_CLAUSE_LOCATION (c),
7387 "%qE is not a variable in %<aligned%> clause", t);
7388 remove = true;
7390 else if (!type_dependent_expression_p (t)
7391 && !TYPE_PTR_P (TREE_TYPE (t))
7392 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE
7393 && (!TYPE_REF_P (TREE_TYPE (t))
7394 || (!INDIRECT_TYPE_P (TREE_TYPE (TREE_TYPE (t)))
7395 && (TREE_CODE (TREE_TYPE (TREE_TYPE (t)))
7396 != ARRAY_TYPE))))
7398 error_at (OMP_CLAUSE_LOCATION (c),
7399 "%qE in %<aligned%> clause is neither a pointer nor "
7400 "an array nor a reference to pointer or array", t);
7401 remove = true;
7403 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
7405 error_at (OMP_CLAUSE_LOCATION (c),
7406 "%qD appears more than once in %<aligned%> clauses",
7408 remove = true;
7410 else
7411 bitmap_set_bit (&aligned_head, DECL_UID (t));
7412 t = OMP_CLAUSE_ALIGNED_ALIGNMENT (c);
7413 if (t == error_mark_node)
7414 remove = true;
7415 else if (t == NULL_TREE)
7416 break;
7417 else if (!type_dependent_expression_p (t)
7418 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
7420 error_at (OMP_CLAUSE_LOCATION (c),
7421 "%<aligned%> clause alignment expression must "
7422 "be integral");
7423 remove = true;
7425 else
7427 t = mark_rvalue_use (t);
7428 if (!processing_template_decl)
7430 t = maybe_constant_value (t);
7431 if (TREE_CODE (t) != INTEGER_CST
7432 || tree_int_cst_sgn (t) != 1)
7434 error_at (OMP_CLAUSE_LOCATION (c),
7435 "%<aligned%> clause alignment expression must "
7436 "be positive constant integer expression");
7437 remove = true;
7439 else
7440 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7442 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = t;
7444 break;
7446 case OMP_CLAUSE_NONTEMPORAL:
7447 t = OMP_CLAUSE_DECL (c);
7448 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
7450 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
7451 break;
7452 if (DECL_P (t))
7453 error_at (OMP_CLAUSE_LOCATION (c),
7454 "%qD is not a variable in %<nontemporal%> clause",
7456 else
7457 error_at (OMP_CLAUSE_LOCATION (c),
7458 "%qE is not a variable in %<nontemporal%> clause",
7460 remove = true;
7462 else if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
7464 error_at (OMP_CLAUSE_LOCATION (c),
7465 "%qD appears more than once in %<nontemporal%> "
7466 "clauses", t);
7467 remove = true;
7469 else
7470 bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
7471 break;
7473 case OMP_CLAUSE_ALLOCATE:
7474 t = omp_clause_decl_field (OMP_CLAUSE_DECL (c));
7475 if (t)
7476 omp_note_field_privatization (t, OMP_CLAUSE_DECL (c));
7477 else
7478 t = OMP_CLAUSE_DECL (c);
7479 if (t == current_class_ptr)
7481 error_at (OMP_CLAUSE_LOCATION (c),
7482 "%<this%> not allowed in %<allocate%> clause");
7483 remove = true;
7484 break;
7486 if (!VAR_P (t)
7487 && TREE_CODE (t) != PARM_DECL
7488 && TREE_CODE (t) != FIELD_DECL)
7490 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
7491 break;
7492 if (DECL_P (t))
7493 error_at (OMP_CLAUSE_LOCATION (c),
7494 "%qD is not a variable in %<allocate%> clause", t);
7495 else
7496 error_at (OMP_CLAUSE_LOCATION (c),
7497 "%qE is not a variable in %<allocate%> clause", t);
7498 remove = true;
7500 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
7502 warning_at (OMP_CLAUSE_LOCATION (c), 0,
7503 "%qD appears more than once in %<allocate%> clauses",
7505 remove = true;
7507 else
7509 bitmap_set_bit (&aligned_head, DECL_UID (t));
7510 allocate_seen = true;
7512 tree allocator;
7513 allocator = OMP_CLAUSE_ALLOCATE_ALLOCATOR (c);
7514 if (error_operand_p (allocator))
7516 remove = true;
7517 break;
7519 if (allocator == NULL_TREE)
7520 goto handle_field_decl;
7521 tree allocatort;
7522 allocatort = TYPE_MAIN_VARIANT (TREE_TYPE (allocator));
7523 if (!type_dependent_expression_p (allocator)
7524 && (TREE_CODE (allocatort) != ENUMERAL_TYPE
7525 || TYPE_NAME (allocatort) == NULL_TREE
7526 || TREE_CODE (TYPE_NAME (allocatort)) != TYPE_DECL
7527 || (DECL_NAME (TYPE_NAME (allocatort))
7528 != get_identifier ("omp_allocator_handle_t"))
7529 || (TYPE_CONTEXT (allocatort)
7530 != DECL_CONTEXT (global_namespace))))
7532 error_at (OMP_CLAUSE_LOCATION (c),
7533 "%<allocate%> clause allocator expression has "
7534 "type %qT rather than %<omp_allocator_handle_t%>",
7535 TREE_TYPE (allocator));
7536 remove = true;
7538 else
7540 allocator = mark_rvalue_use (allocator);
7541 if (!processing_template_decl)
7542 allocator = maybe_constant_value (allocator);
7543 OMP_CLAUSE_ALLOCATE_ALLOCATOR (c) = allocator;
7545 goto handle_field_decl;
7547 case OMP_CLAUSE_DEPEND:
7548 t = OMP_CLAUSE_DECL (c);
7549 if (t == NULL_TREE)
7551 gcc_assert (OMP_CLAUSE_DEPEND_KIND (c)
7552 == OMP_CLAUSE_DEPEND_SOURCE);
7553 break;
7555 if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SINK)
7557 if (cp_finish_omp_clause_depend_sink (c))
7558 remove = true;
7559 break;
7561 /* FALLTHRU */
7562 case OMP_CLAUSE_AFFINITY:
7563 t = OMP_CLAUSE_DECL (c);
7564 if (TREE_CODE (t) == TREE_LIST
7565 && TREE_PURPOSE (t)
7566 && TREE_CODE (TREE_PURPOSE (t)) == TREE_VEC)
7568 if (TREE_PURPOSE (t) != last_iterators)
7569 last_iterators_remove
7570 = cp_omp_finish_iterators (TREE_PURPOSE (t));
7571 last_iterators = TREE_PURPOSE (t);
7572 t = TREE_VALUE (t);
7573 if (last_iterators_remove)
7574 t = error_mark_node;
7576 else
7577 last_iterators = NULL_TREE;
7579 if (TREE_CODE (t) == TREE_LIST)
7581 if (handle_omp_array_sections (c, ort))
7582 remove = true;
7583 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
7584 && (OMP_CLAUSE_DEPEND_KIND (c)
7585 == OMP_CLAUSE_DEPEND_DEPOBJ))
7587 error_at (OMP_CLAUSE_LOCATION (c),
7588 "%<depend%> clause with %<depobj%> dependence "
7589 "type on array section");
7590 remove = true;
7592 break;
7594 if (t == error_mark_node)
7595 remove = true;
7596 else if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
7597 break;
7598 else if (!lvalue_p (t))
7600 if (DECL_P (t))
7601 error_at (OMP_CLAUSE_LOCATION (c),
7602 "%qD is not lvalue expression nor array section "
7603 "in %qs clause", t,
7604 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7605 else
7606 error_at (OMP_CLAUSE_LOCATION (c),
7607 "%qE is not lvalue expression nor array section "
7608 "in %qs clause", t,
7609 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7610 remove = true;
7612 else if (TREE_CODE (t) == COMPONENT_REF
7613 && TREE_CODE (TREE_OPERAND (t, 1)) == FIELD_DECL
7614 && DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
7616 error_at (OMP_CLAUSE_LOCATION (c),
7617 "bit-field %qE in %qs clause", t,
7618 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7619 remove = true;
7621 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
7622 && OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_DEPOBJ)
7624 if (!c_omp_depend_t_p (TYPE_REF_P (TREE_TYPE (t))
7625 ? TREE_TYPE (TREE_TYPE (t))
7626 : TREE_TYPE (t)))
7628 error_at (OMP_CLAUSE_LOCATION (c),
7629 "%qE does not have %<omp_depend_t%> type in "
7630 "%<depend%> clause with %<depobj%> dependence "
7631 "type", t);
7632 remove = true;
7635 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
7636 && c_omp_depend_t_p (TYPE_REF_P (TREE_TYPE (t))
7637 ? TREE_TYPE (TREE_TYPE (t))
7638 : TREE_TYPE (t)))
7640 error_at (OMP_CLAUSE_LOCATION (c),
7641 "%qE should not have %<omp_depend_t%> type in "
7642 "%<depend%> clause with dependence type other than "
7643 "%<depobj%>", t);
7644 remove = true;
7646 if (!remove)
7648 tree addr = cp_build_addr_expr (t, tf_warning_or_error);
7649 if (addr == error_mark_node)
7650 remove = true;
7651 else
7653 t = cp_build_indirect_ref (OMP_CLAUSE_LOCATION (c),
7654 addr, RO_UNARY_STAR,
7655 tf_warning_or_error);
7656 if (t == error_mark_node)
7657 remove = true;
7658 else if (TREE_CODE (OMP_CLAUSE_DECL (c)) == TREE_LIST
7659 && TREE_PURPOSE (OMP_CLAUSE_DECL (c))
7660 && (TREE_CODE (TREE_PURPOSE (OMP_CLAUSE_DECL (c)))
7661 == TREE_VEC))
7662 TREE_VALUE (OMP_CLAUSE_DECL (c)) = t;
7663 else
7664 OMP_CLAUSE_DECL (c) = t;
7667 break;
7668 case OMP_CLAUSE_DETACH:
7669 t = OMP_CLAUSE_DECL (c);
7670 if (detach_seen)
7672 error_at (OMP_CLAUSE_LOCATION (c),
7673 "too many %qs clauses on a task construct",
7674 "detach");
7675 remove = true;
7676 break;
7678 else if (error_operand_p (t))
7680 remove = true;
7681 break;
7683 else
7685 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (t));
7686 if (!type_dependent_expression_p (t)
7687 && (!INTEGRAL_TYPE_P (type)
7688 || TREE_CODE (type) != ENUMERAL_TYPE
7689 || TYPE_NAME (type) == NULL_TREE
7690 || (DECL_NAME (TYPE_NAME (type))
7691 != get_identifier ("omp_event_handle_t"))))
7693 error_at (OMP_CLAUSE_LOCATION (c),
7694 "%<detach%> clause event handle "
7695 "has type %qT rather than "
7696 "%<omp_event_handle_t%>",
7697 type);
7698 remove = true;
7700 detach_seen = c;
7701 cxx_mark_addressable (t);
7703 break;
7705 case OMP_CLAUSE_MAP:
7706 if (OMP_CLAUSE_MAP_IMPLICIT (c) && !implicit_moved)
7707 goto move_implicit;
7708 /* FALLTHRU */
7709 case OMP_CLAUSE_TO:
7710 case OMP_CLAUSE_FROM:
7711 case OMP_CLAUSE__CACHE_:
7712 t = OMP_CLAUSE_DECL (c);
7713 if (TREE_CODE (t) == TREE_LIST)
7715 if (handle_omp_array_sections (c, ort))
7716 remove = true;
7717 else
7719 t = OMP_CLAUSE_DECL (c);
7720 if (TREE_CODE (t) != TREE_LIST
7721 && !type_dependent_expression_p (t)
7722 && !cp_omp_mappable_type (TREE_TYPE (t)))
7724 error_at (OMP_CLAUSE_LOCATION (c),
7725 "array section does not have mappable type "
7726 "in %qs clause",
7727 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7728 cp_omp_emit_unmappable_type_notes (TREE_TYPE (t));
7729 remove = true;
7731 while (TREE_CODE (t) == ARRAY_REF)
7732 t = TREE_OPERAND (t, 0);
7733 if (TREE_CODE (t) == COMPONENT_REF
7734 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
7736 while (TREE_CODE (t) == COMPONENT_REF)
7737 t = TREE_OPERAND (t, 0);
7738 if (REFERENCE_REF_P (t))
7739 t = TREE_OPERAND (t, 0);
7740 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
7741 && OMP_CLAUSE_MAP_IMPLICIT (c)
7742 && (bitmap_bit_p (&map_head, DECL_UID (t))
7743 || bitmap_bit_p (&map_field_head, DECL_UID (t))
7744 || bitmap_bit_p (&map_firstprivate_head,
7745 DECL_UID (t))))
7747 remove = true;
7748 break;
7750 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
7751 break;
7752 if (bitmap_bit_p (&map_head, DECL_UID (t)))
7754 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
7755 error_at (OMP_CLAUSE_LOCATION (c),
7756 "%qD appears more than once in motion"
7757 " clauses", t);
7758 else if (ort == C_ORT_ACC)
7759 error_at (OMP_CLAUSE_LOCATION (c),
7760 "%qD appears more than once in data"
7761 " clauses", t);
7762 else
7763 error_at (OMP_CLAUSE_LOCATION (c),
7764 "%qD appears more than once in map"
7765 " clauses", t);
7766 remove = true;
7768 else
7770 bitmap_set_bit (&map_head, DECL_UID (t));
7771 bitmap_set_bit (&map_field_head, DECL_UID (t));
7775 if (cp_oacc_check_attachments (c))
7776 remove = true;
7777 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
7778 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
7779 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH))
7780 /* In this case, we have a single array element which is a
7781 pointer, and we already set OMP_CLAUSE_SIZE in
7782 handle_omp_array_sections above. For attach/detach clauses,
7783 reset the OMP_CLAUSE_SIZE (representing a bias) to zero
7784 here. */
7785 OMP_CLAUSE_SIZE (c) = size_zero_node;
7786 break;
7788 if (t == error_mark_node)
7790 remove = true;
7791 break;
7793 /* OpenACC attach / detach clauses must be pointers. */
7794 if (cp_oacc_check_attachments (c))
7796 remove = true;
7797 break;
7799 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
7800 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
7801 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH))
7802 /* For attach/detach clauses, set OMP_CLAUSE_SIZE (representing a
7803 bias) to zero here, so it is not set erroneously to the pointer
7804 size later on in gimplify.c. */
7805 OMP_CLAUSE_SIZE (c) = size_zero_node;
7806 if (REFERENCE_REF_P (t)
7807 && TREE_CODE (TREE_OPERAND (t, 0)) == COMPONENT_REF)
7809 t = TREE_OPERAND (t, 0);
7810 OMP_CLAUSE_DECL (c) = t;
7812 if (TREE_CODE (t) == COMPONENT_REF
7813 && TREE_CODE (TREE_OPERAND (t, 0)) == INDIRECT_REF)
7814 t = TREE_OPERAND (TREE_OPERAND (t, 0), 0);
7815 if (TREE_CODE (t) == COMPONENT_REF
7816 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE__CACHE_)
7818 if (type_dependent_expression_p (t))
7819 break;
7820 if (TREE_CODE (TREE_OPERAND (t, 1)) == FIELD_DECL
7821 && DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
7823 error_at (OMP_CLAUSE_LOCATION (c),
7824 "bit-field %qE in %qs clause",
7825 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7826 remove = true;
7828 else if (!cp_omp_mappable_type (TREE_TYPE (t)))
7830 error_at (OMP_CLAUSE_LOCATION (c),
7831 "%qE does not have a mappable type in %qs clause",
7832 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7833 cp_omp_emit_unmappable_type_notes (TREE_TYPE (t));
7834 remove = true;
7836 while (TREE_CODE (t) == COMPONENT_REF)
7838 if (TREE_TYPE (TREE_OPERAND (t, 0))
7839 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0)))
7840 == UNION_TYPE))
7842 error_at (OMP_CLAUSE_LOCATION (c),
7843 "%qE is a member of a union", t);
7844 remove = true;
7845 break;
7847 t = TREE_OPERAND (t, 0);
7849 if (remove)
7850 break;
7851 if (REFERENCE_REF_P (t))
7852 t = TREE_OPERAND (t, 0);
7853 if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
7855 if (bitmap_bit_p (&map_field_head, DECL_UID (t))
7856 || (ort != C_ORT_ACC
7857 && bitmap_bit_p (&map_head, DECL_UID (t))))
7858 goto handle_map_references;
7861 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
7863 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
7864 break;
7865 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
7866 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
7867 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ALWAYS_POINTER
7868 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH_DETACH))
7869 break;
7870 if (DECL_P (t))
7871 error_at (OMP_CLAUSE_LOCATION (c),
7872 "%qD is not a variable in %qs clause", t,
7873 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7874 else
7875 error_at (OMP_CLAUSE_LOCATION (c),
7876 "%qE is not a variable in %qs clause", t,
7877 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7878 remove = true;
7880 else if (VAR_P (t) && CP_DECL_THREAD_LOCAL_P (t))
7882 error_at (OMP_CLAUSE_LOCATION (c),
7883 "%qD is threadprivate variable in %qs clause", t,
7884 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7885 remove = true;
7887 else if (ort != C_ORT_ACC && t == current_class_ptr)
7889 error_at (OMP_CLAUSE_LOCATION (c),
7890 "%<this%> allowed in OpenMP only in %<declare simd%>"
7891 " clauses");
7892 remove = true;
7893 break;
7895 else if (!processing_template_decl
7896 && !TYPE_REF_P (TREE_TYPE (t))
7897 && (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
7898 || (OMP_CLAUSE_MAP_KIND (c)
7899 != GOMP_MAP_FIRSTPRIVATE_POINTER))
7900 && !cxx_mark_addressable (t))
7901 remove = true;
7902 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
7903 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
7904 || (OMP_CLAUSE_MAP_KIND (c)
7905 == GOMP_MAP_FIRSTPRIVATE_POINTER)))
7906 && t == OMP_CLAUSE_DECL (c)
7907 && !type_dependent_expression_p (t)
7908 && !cp_omp_mappable_type (TYPE_REF_P (TREE_TYPE (t))
7909 ? TREE_TYPE (TREE_TYPE (t))
7910 : TREE_TYPE (t)))
7912 error_at (OMP_CLAUSE_LOCATION (c),
7913 "%qD does not have a mappable type in %qs clause", t,
7914 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7915 cp_omp_emit_unmappable_type_notes (TREE_TYPE (t));
7916 remove = true;
7918 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
7919 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FORCE_DEVICEPTR
7920 && !type_dependent_expression_p (t)
7921 && !INDIRECT_TYPE_P (TREE_TYPE (t)))
7923 error_at (OMP_CLAUSE_LOCATION (c),
7924 "%qD is not a pointer variable", t);
7925 remove = true;
7927 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
7928 && OMP_CLAUSE_MAP_IMPLICIT (c)
7929 && (bitmap_bit_p (&map_head, DECL_UID (t))
7930 || bitmap_bit_p (&map_field_head, DECL_UID (t))
7931 || bitmap_bit_p (&map_firstprivate_head,
7932 DECL_UID (t))))
7933 remove = true;
7934 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
7935 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
7937 if (bitmap_bit_p (&generic_head, DECL_UID (t))
7938 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
7939 || bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
7941 error_at (OMP_CLAUSE_LOCATION (c),
7942 "%qD appears more than once in data clauses", t);
7943 remove = true;
7945 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
7947 if (ort == C_ORT_ACC)
7948 error_at (OMP_CLAUSE_LOCATION (c),
7949 "%qD appears more than once in data clauses", t);
7950 else
7951 error_at (OMP_CLAUSE_LOCATION (c),
7952 "%qD appears both in data and map clauses", t);
7953 remove = true;
7955 else
7956 bitmap_set_bit (&map_firstprivate_head, DECL_UID (t));
7958 else if (bitmap_bit_p (&map_head, DECL_UID (t))
7959 && !bitmap_bit_p (&map_field_head, DECL_UID (t)))
7961 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
7962 error_at (OMP_CLAUSE_LOCATION (c),
7963 "%qD appears more than once in motion clauses", t);
7964 else if (ort == C_ORT_ACC)
7965 error_at (OMP_CLAUSE_LOCATION (c),
7966 "%qD appears more than once in data clauses", t);
7967 else
7968 error_at (OMP_CLAUSE_LOCATION (c),
7969 "%qD appears more than once in map clauses", t);
7970 remove = true;
7972 else if (ort == C_ORT_ACC
7973 && bitmap_bit_p (&generic_head, DECL_UID (t)))
7975 error_at (OMP_CLAUSE_LOCATION (c),
7976 "%qD appears more than once in data clauses", t);
7977 remove = true;
7979 else if (bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
7981 if (ort == C_ORT_ACC)
7982 error_at (OMP_CLAUSE_LOCATION (c),
7983 "%qD appears more than once in data clauses", t);
7984 else
7985 error_at (OMP_CLAUSE_LOCATION (c),
7986 "%qD appears both in data and map clauses", t);
7987 remove = true;
7989 else
7991 bitmap_set_bit (&map_head, DECL_UID (t));
7992 if (t != OMP_CLAUSE_DECL (c)
7993 && TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPONENT_REF)
7994 bitmap_set_bit (&map_field_head, DECL_UID (t));
7996 handle_map_references:
7997 if (!remove
7998 && !processing_template_decl
7999 && ort != C_ORT_DECLARE_SIMD
8000 && TYPE_REF_P (TREE_TYPE (OMP_CLAUSE_DECL (c))))
8002 t = OMP_CLAUSE_DECL (c);
8003 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
8005 OMP_CLAUSE_DECL (c) = build_simple_mem_ref (t);
8006 if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
8007 OMP_CLAUSE_SIZE (c)
8008 = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t)));
8010 else if (OMP_CLAUSE_MAP_KIND (c)
8011 != GOMP_MAP_FIRSTPRIVATE_POINTER
8012 && (OMP_CLAUSE_MAP_KIND (c)
8013 != GOMP_MAP_FIRSTPRIVATE_REFERENCE)
8014 && (OMP_CLAUSE_MAP_KIND (c)
8015 != GOMP_MAP_ALWAYS_POINTER)
8016 && (OMP_CLAUSE_MAP_KIND (c)
8017 != GOMP_MAP_ATTACH_DETACH))
8019 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
8020 OMP_CLAUSE_MAP);
8021 if (TREE_CODE (t) == COMPONENT_REF)
8022 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ATTACH_DETACH);
8023 else
8024 OMP_CLAUSE_SET_MAP_KIND (c2,
8025 GOMP_MAP_FIRSTPRIVATE_REFERENCE);
8026 OMP_CLAUSE_DECL (c2) = t;
8027 OMP_CLAUSE_SIZE (c2) = size_zero_node;
8028 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
8029 OMP_CLAUSE_CHAIN (c) = c2;
8030 OMP_CLAUSE_DECL (c) = build_simple_mem_ref (t);
8031 if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
8032 OMP_CLAUSE_SIZE (c)
8033 = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t)));
8034 c = c2;
8037 break;
8039 case OMP_CLAUSE_TO_DECLARE:
8040 case OMP_CLAUSE_LINK:
8041 t = OMP_CLAUSE_DECL (c);
8042 if (TREE_CODE (t) == FUNCTION_DECL
8043 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
8045 else if (!VAR_P (t))
8047 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
8049 if (TREE_CODE (t) == TEMPLATE_ID_EXPR)
8050 error_at (OMP_CLAUSE_LOCATION (c),
8051 "template %qE in clause %qs", t,
8052 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
8053 else if (really_overloaded_fn (t))
8054 error_at (OMP_CLAUSE_LOCATION (c),
8055 "overloaded function name %qE in clause %qs", t,
8056 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
8057 else
8058 error_at (OMP_CLAUSE_LOCATION (c),
8059 "%qE is neither a variable nor a function name "
8060 "in clause %qs", t,
8061 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
8063 else
8064 error_at (OMP_CLAUSE_LOCATION (c),
8065 "%qE is not a variable in clause %qs", t,
8066 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
8067 remove = true;
8069 else if (DECL_THREAD_LOCAL_P (t))
8071 error_at (OMP_CLAUSE_LOCATION (c),
8072 "%qD is threadprivate variable in %qs clause", t,
8073 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
8074 remove = true;
8076 else if (!cp_omp_mappable_type (TREE_TYPE (t)))
8078 error_at (OMP_CLAUSE_LOCATION (c),
8079 "%qD does not have a mappable type in %qs clause", t,
8080 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
8081 cp_omp_emit_unmappable_type_notes (TREE_TYPE (t));
8082 remove = true;
8084 if (remove)
8085 break;
8086 if (bitmap_bit_p (&generic_head, DECL_UID (t)))
8088 error_at (OMP_CLAUSE_LOCATION (c),
8089 "%qE appears more than once on the same "
8090 "%<declare target%> directive", t);
8091 remove = true;
8093 else
8094 bitmap_set_bit (&generic_head, DECL_UID (t));
8095 break;
8097 case OMP_CLAUSE_UNIFORM:
8098 t = OMP_CLAUSE_DECL (c);
8099 if (TREE_CODE (t) != PARM_DECL)
8101 if (processing_template_decl)
8102 break;
8103 if (DECL_P (t))
8104 error_at (OMP_CLAUSE_LOCATION (c),
8105 "%qD is not an argument in %<uniform%> clause", t);
8106 else
8107 error_at (OMP_CLAUSE_LOCATION (c),
8108 "%qE is not an argument in %<uniform%> clause", t);
8109 remove = true;
8110 break;
8112 /* map_head bitmap is used as uniform_head if declare_simd. */
8113 bitmap_set_bit (&map_head, DECL_UID (t));
8114 goto check_dup_generic;
8116 case OMP_CLAUSE_GRAINSIZE:
8117 t = OMP_CLAUSE_GRAINSIZE_EXPR (c);
8118 if (t == error_mark_node)
8119 remove = true;
8120 else if (!type_dependent_expression_p (t)
8121 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
8123 error_at (OMP_CLAUSE_LOCATION (c),
8124 "%<grainsize%> expression must be integral");
8125 remove = true;
8127 else
8129 t = mark_rvalue_use (t);
8130 if (!processing_template_decl)
8132 t = maybe_constant_value (t);
8133 if (TREE_CODE (t) == INTEGER_CST
8134 && tree_int_cst_sgn (t) != 1)
8136 warning_at (OMP_CLAUSE_LOCATION (c), 0,
8137 "%<grainsize%> value must be positive");
8138 t = integer_one_node;
8140 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
8142 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
8144 break;
8146 case OMP_CLAUSE_PRIORITY:
8147 t = OMP_CLAUSE_PRIORITY_EXPR (c);
8148 if (t == error_mark_node)
8149 remove = true;
8150 else if (!type_dependent_expression_p (t)
8151 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
8153 error_at (OMP_CLAUSE_LOCATION (c),
8154 "%<priority%> expression must be integral");
8155 remove = true;
8157 else
8159 t = mark_rvalue_use (t);
8160 if (!processing_template_decl)
8162 t = maybe_constant_value (t);
8163 if (TREE_CODE (t) == INTEGER_CST
8164 && tree_int_cst_sgn (t) == -1)
8166 warning_at (OMP_CLAUSE_LOCATION (c), 0,
8167 "%<priority%> value must be non-negative");
8168 t = integer_one_node;
8170 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
8172 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
8174 break;
8176 case OMP_CLAUSE_HINT:
8177 t = OMP_CLAUSE_HINT_EXPR (c);
8178 if (t == error_mark_node)
8179 remove = true;
8180 else if (!type_dependent_expression_p (t)
8181 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
8183 error_at (OMP_CLAUSE_LOCATION (c),
8184 "%<hint%> expression must be integral");
8185 remove = true;
8187 else
8189 t = mark_rvalue_use (t);
8190 if (!processing_template_decl)
8192 t = maybe_constant_value (t);
8193 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
8194 if (TREE_CODE (t) != INTEGER_CST)
8196 error_at (OMP_CLAUSE_LOCATION (c),
8197 "%<hint%> expression must be constant integer "
8198 "expression");
8199 remove = true;
8202 OMP_CLAUSE_HINT_EXPR (c) = t;
8204 break;
8206 case OMP_CLAUSE_IS_DEVICE_PTR:
8207 case OMP_CLAUSE_USE_DEVICE_PTR:
8208 field_ok = (ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP;
8209 t = OMP_CLAUSE_DECL (c);
8210 if (!type_dependent_expression_p (t))
8212 tree type = TREE_TYPE (t);
8213 if (!TYPE_PTR_P (type)
8214 && (!TYPE_REF_P (type) || !TYPE_PTR_P (TREE_TYPE (type))))
8216 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_USE_DEVICE_PTR
8217 && ort == C_ORT_OMP)
8219 error_at (OMP_CLAUSE_LOCATION (c),
8220 "%qs variable is neither a pointer "
8221 "nor reference to pointer",
8222 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
8223 remove = true;
8225 else if (TREE_CODE (type) != ARRAY_TYPE
8226 && (!TYPE_REF_P (type)
8227 || TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE))
8229 error_at (OMP_CLAUSE_LOCATION (c),
8230 "%qs variable is neither a pointer, nor an "
8231 "array nor reference to pointer or array",
8232 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
8233 remove = true;
8237 goto check_dup_generic;
8239 case OMP_CLAUSE_USE_DEVICE_ADDR:
8240 field_ok = true;
8241 t = OMP_CLAUSE_DECL (c);
8242 if (!processing_template_decl
8243 && (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
8244 && !TYPE_REF_P (TREE_TYPE (t))
8245 && !cxx_mark_addressable (t))
8246 remove = true;
8247 goto check_dup_generic;
8249 case OMP_CLAUSE_NOWAIT:
8250 case OMP_CLAUSE_DEFAULT:
8251 case OMP_CLAUSE_UNTIED:
8252 case OMP_CLAUSE_COLLAPSE:
8253 case OMP_CLAUSE_PARALLEL:
8254 case OMP_CLAUSE_FOR:
8255 case OMP_CLAUSE_SECTIONS:
8256 case OMP_CLAUSE_TASKGROUP:
8257 case OMP_CLAUSE_PROC_BIND:
8258 case OMP_CLAUSE_DEVICE_TYPE:
8259 case OMP_CLAUSE_NOGROUP:
8260 case OMP_CLAUSE_THREADS:
8261 case OMP_CLAUSE_SIMD:
8262 case OMP_CLAUSE_DEFAULTMAP:
8263 case OMP_CLAUSE_BIND:
8264 case OMP_CLAUSE_AUTO:
8265 case OMP_CLAUSE_INDEPENDENT:
8266 case OMP_CLAUSE_SEQ:
8267 case OMP_CLAUSE_IF_PRESENT:
8268 case OMP_CLAUSE_FINALIZE:
8269 break;
8271 case OMP_CLAUSE_MERGEABLE:
8272 mergeable_seen = true;
8273 break;
8275 case OMP_CLAUSE_TILE:
8276 for (tree list = OMP_CLAUSE_TILE_LIST (c); !remove && list;
8277 list = TREE_CHAIN (list))
8279 t = TREE_VALUE (list);
8281 if (t == error_mark_node)
8282 remove = true;
8283 else if (!type_dependent_expression_p (t)
8284 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
8286 error_at (OMP_CLAUSE_LOCATION (c),
8287 "%<tile%> argument needs integral type");
8288 remove = true;
8290 else
8292 t = mark_rvalue_use (t);
8293 if (!processing_template_decl)
8295 /* Zero is used to indicate '*', we permit you
8296 to get there via an ICE of value zero. */
8297 t = maybe_constant_value (t);
8298 if (!tree_fits_shwi_p (t)
8299 || tree_to_shwi (t) < 0)
8301 error_at (OMP_CLAUSE_LOCATION (c),
8302 "%<tile%> argument needs positive "
8303 "integral constant");
8304 remove = true;
8306 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
8310 /* Update list item. */
8311 TREE_VALUE (list) = t;
8313 break;
8315 case OMP_CLAUSE_ORDERED:
8316 ordered_seen = true;
8317 break;
8319 case OMP_CLAUSE_ORDER:
8320 if (order_seen)
8321 remove = true;
8322 else
8323 order_seen = true;
8324 break;
8326 case OMP_CLAUSE_INBRANCH:
8327 case OMP_CLAUSE_NOTINBRANCH:
8328 if (branch_seen)
8330 error_at (OMP_CLAUSE_LOCATION (c),
8331 "%<inbranch%> clause is incompatible with "
8332 "%<notinbranch%>");
8333 remove = true;
8335 branch_seen = true;
8336 break;
8338 case OMP_CLAUSE_INCLUSIVE:
8339 case OMP_CLAUSE_EXCLUSIVE:
8340 t = omp_clause_decl_field (OMP_CLAUSE_DECL (c));
8341 if (!t)
8342 t = OMP_CLAUSE_DECL (c);
8343 if (t == current_class_ptr)
8345 error_at (OMP_CLAUSE_LOCATION (c),
8346 "%<this%> allowed in OpenMP only in %<declare simd%>"
8347 " clauses");
8348 remove = true;
8349 break;
8351 if (!VAR_P (t)
8352 && TREE_CODE (t) != PARM_DECL
8353 && TREE_CODE (t) != FIELD_DECL)
8355 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
8356 break;
8357 if (DECL_P (t))
8358 error_at (OMP_CLAUSE_LOCATION (c),
8359 "%qD is not a variable in clause %qs", t,
8360 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
8361 else
8362 error_at (OMP_CLAUSE_LOCATION (c),
8363 "%qE is not a variable in clause %qs", t,
8364 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
8365 remove = true;
8367 break;
8369 default:
8370 gcc_unreachable ();
8373 if (remove)
8374 *pc = OMP_CLAUSE_CHAIN (c);
8375 else
8376 pc = &OMP_CLAUSE_CHAIN (c);
8379 if (reduction_seen < 0 && (ordered_seen || schedule_seen))
8380 reduction_seen = -2;
8382 for (pc = &clauses, c = clauses; c ; c = *pc)
8384 enum omp_clause_code c_kind = OMP_CLAUSE_CODE (c);
8385 bool remove = false;
8386 bool need_complete_type = false;
8387 bool need_default_ctor = false;
8388 bool need_copy_ctor = false;
8389 bool need_copy_assignment = false;
8390 bool need_implicitly_determined = false;
8391 bool need_dtor = false;
8392 tree type, inner_type;
8394 switch (c_kind)
8396 case OMP_CLAUSE_SHARED:
8397 need_implicitly_determined = true;
8398 break;
8399 case OMP_CLAUSE_PRIVATE:
8400 need_complete_type = true;
8401 need_default_ctor = true;
8402 need_dtor = true;
8403 need_implicitly_determined = true;
8404 break;
8405 case OMP_CLAUSE_FIRSTPRIVATE:
8406 need_complete_type = true;
8407 need_copy_ctor = true;
8408 need_dtor = true;
8409 need_implicitly_determined = true;
8410 break;
8411 case OMP_CLAUSE_LASTPRIVATE:
8412 need_complete_type = true;
8413 need_copy_assignment = true;
8414 need_implicitly_determined = true;
8415 break;
8416 case OMP_CLAUSE_REDUCTION:
8417 if (reduction_seen == -2)
8418 OMP_CLAUSE_REDUCTION_INSCAN (c) = 0;
8419 if (OMP_CLAUSE_REDUCTION_INSCAN (c))
8420 need_copy_assignment = true;
8421 need_implicitly_determined = true;
8422 break;
8423 case OMP_CLAUSE_IN_REDUCTION:
8424 case OMP_CLAUSE_TASK_REDUCTION:
8425 case OMP_CLAUSE_INCLUSIVE:
8426 case OMP_CLAUSE_EXCLUSIVE:
8427 need_implicitly_determined = true;
8428 break;
8429 case OMP_CLAUSE_LINEAR:
8430 if (ort != C_ORT_OMP_DECLARE_SIMD)
8431 need_implicitly_determined = true;
8432 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c)
8433 && !bitmap_bit_p (&map_head,
8434 DECL_UID (OMP_CLAUSE_LINEAR_STEP (c))))
8436 error_at (OMP_CLAUSE_LOCATION (c),
8437 "%<linear%> clause step is a parameter %qD not "
8438 "specified in %<uniform%> clause",
8439 OMP_CLAUSE_LINEAR_STEP (c));
8440 *pc = OMP_CLAUSE_CHAIN (c);
8441 continue;
8443 break;
8444 case OMP_CLAUSE_COPYPRIVATE:
8445 need_copy_assignment = true;
8446 break;
8447 case OMP_CLAUSE_COPYIN:
8448 need_copy_assignment = true;
8449 break;
8450 case OMP_CLAUSE_SIMDLEN:
8451 if (safelen
8452 && !processing_template_decl
8453 && tree_int_cst_lt (OMP_CLAUSE_SAFELEN_EXPR (safelen),
8454 OMP_CLAUSE_SIMDLEN_EXPR (c)))
8456 error_at (OMP_CLAUSE_LOCATION (c),
8457 "%<simdlen%> clause value is bigger than "
8458 "%<safelen%> clause value");
8459 OMP_CLAUSE_SIMDLEN_EXPR (c)
8460 = OMP_CLAUSE_SAFELEN_EXPR (safelen);
8462 pc = &OMP_CLAUSE_CHAIN (c);
8463 continue;
8464 case OMP_CLAUSE_SCHEDULE:
8465 if (ordered_seen
8466 && (OMP_CLAUSE_SCHEDULE_KIND (c)
8467 & OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
8469 error_at (OMP_CLAUSE_LOCATION (c),
8470 "%<nonmonotonic%> schedule modifier specified "
8471 "together with %<ordered%> clause");
8472 OMP_CLAUSE_SCHEDULE_KIND (c)
8473 = (enum omp_clause_schedule_kind)
8474 (OMP_CLAUSE_SCHEDULE_KIND (c)
8475 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
8477 if (reduction_seen == -2)
8478 error_at (OMP_CLAUSE_LOCATION (c),
8479 "%qs clause specified together with %<inscan%> "
8480 "%<reduction%> clause", "schedule");
8481 pc = &OMP_CLAUSE_CHAIN (c);
8482 continue;
8483 case OMP_CLAUSE_NOGROUP:
8484 if (reduction_seen)
8486 error_at (OMP_CLAUSE_LOCATION (c),
8487 "%<nogroup%> clause must not be used together with "
8488 "%<reduction%> clause");
8489 *pc = OMP_CLAUSE_CHAIN (c);
8490 continue;
8492 pc = &OMP_CLAUSE_CHAIN (c);
8493 continue;
8494 case OMP_CLAUSE_ORDERED:
8495 if (reduction_seen == -2)
8496 error_at (OMP_CLAUSE_LOCATION (c),
8497 "%qs clause specified together with %<inscan%> "
8498 "%<reduction%> clause", "ordered");
8499 pc = &OMP_CLAUSE_CHAIN (c);
8500 continue;
8501 case OMP_CLAUSE_ORDER:
8502 if (ordered_seen)
8504 error_at (OMP_CLAUSE_LOCATION (c),
8505 "%<order%> clause must not be used together "
8506 "with %<ordered%>");
8507 *pc = OMP_CLAUSE_CHAIN (c);
8508 continue;
8510 pc = &OMP_CLAUSE_CHAIN (c);
8511 continue;
8512 case OMP_CLAUSE_DETACH:
8513 if (mergeable_seen)
8515 error_at (OMP_CLAUSE_LOCATION (c),
8516 "%<detach%> clause must not be used together with "
8517 "%<mergeable%> clause");
8518 *pc = OMP_CLAUSE_CHAIN (c);
8519 continue;
8521 pc = &OMP_CLAUSE_CHAIN (c);
8522 continue;
8523 case OMP_CLAUSE_MAP:
8524 if (target_in_reduction_seen && !processing_template_decl)
8526 t = OMP_CLAUSE_DECL (c);
8527 while (handled_component_p (t)
8528 || TREE_CODE (t) == INDIRECT_REF
8529 || TREE_CODE (t) == ADDR_EXPR
8530 || TREE_CODE (t) == MEM_REF
8531 || TREE_CODE (t) == NON_LVALUE_EXPR)
8532 t = TREE_OPERAND (t, 0);
8533 if (DECL_P (t)
8534 && bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
8535 OMP_CLAUSE_MAP_IN_REDUCTION (c) = 1;
8537 pc = &OMP_CLAUSE_CHAIN (c);
8538 continue;
8539 case OMP_CLAUSE_NOWAIT:
8540 if (copyprivate_seen)
8542 error_at (OMP_CLAUSE_LOCATION (c),
8543 "%<nowait%> clause must not be used together "
8544 "with %<copyprivate%>");
8545 *pc = OMP_CLAUSE_CHAIN (c);
8546 continue;
8548 /* FALLTHRU */
8549 default:
8550 pc = &OMP_CLAUSE_CHAIN (c);
8551 continue;
8554 t = OMP_CLAUSE_DECL (c);
8555 switch (c_kind)
8557 case OMP_CLAUSE_LASTPRIVATE:
8558 if (DECL_P (t)
8559 && !bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
8561 need_default_ctor = true;
8562 need_dtor = true;
8564 break;
8566 case OMP_CLAUSE_REDUCTION:
8567 case OMP_CLAUSE_IN_REDUCTION:
8568 case OMP_CLAUSE_TASK_REDUCTION:
8569 if (allocate_seen)
8571 if (TREE_CODE (t) == MEM_REF)
8573 t = TREE_OPERAND (t, 0);
8574 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
8575 t = TREE_OPERAND (t, 0);
8576 if (TREE_CODE (t) == ADDR_EXPR
8577 || TREE_CODE (t) == INDIRECT_REF)
8578 t = TREE_OPERAND (t, 0);
8579 if (DECL_P (t))
8580 bitmap_clear_bit (&aligned_head, DECL_UID (t));
8582 else if (TREE_CODE (t) == TREE_LIST)
8584 while (TREE_CODE (t) == TREE_LIST)
8585 t = TREE_CHAIN (t);
8586 if (DECL_P (t))
8587 bitmap_clear_bit (&aligned_head, DECL_UID (t));
8588 t = OMP_CLAUSE_DECL (c);
8590 else if (DECL_P (t))
8591 bitmap_clear_bit (&aligned_head, DECL_UID (t));
8592 t = OMP_CLAUSE_DECL (c);
8594 if (processing_template_decl
8595 && !VAR_P (t) && TREE_CODE (t) != PARM_DECL)
8596 break;
8597 if (finish_omp_reduction_clause (c, &need_default_ctor,
8598 &need_dtor))
8599 remove = true;
8600 else
8601 t = OMP_CLAUSE_DECL (c);
8602 break;
8604 case OMP_CLAUSE_COPYIN:
8605 if (processing_template_decl
8606 && !VAR_P (t) && TREE_CODE (t) != PARM_DECL)
8607 break;
8608 if (!VAR_P (t) || !CP_DECL_THREAD_LOCAL_P (t))
8610 error_at (OMP_CLAUSE_LOCATION (c),
8611 "%qE must be %<threadprivate%> for %<copyin%>", t);
8612 remove = true;
8614 break;
8616 default:
8617 break;
8620 if (processing_template_decl
8621 && !VAR_P (t) && TREE_CODE (t) != PARM_DECL)
8623 pc = &OMP_CLAUSE_CHAIN (c);
8624 continue;
8627 if (need_complete_type || need_copy_assignment)
8629 t = require_complete_type (t);
8630 if (t == error_mark_node)
8631 remove = true;
8632 else if (!processing_template_decl
8633 && TYPE_REF_P (TREE_TYPE (t))
8634 && !complete_type_or_else (TREE_TYPE (TREE_TYPE (t)), t))
8635 remove = true;
8637 if (need_implicitly_determined)
8639 const char *share_name = NULL;
8641 if (allocate_seen
8642 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_SHARED
8643 && DECL_P (t))
8644 bitmap_clear_bit (&aligned_head, DECL_UID (t));
8646 if (VAR_P (t) && CP_DECL_THREAD_LOCAL_P (t))
8647 share_name = "threadprivate";
8648 else switch (cxx_omp_predetermined_sharing_1 (t))
8650 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
8651 break;
8652 case OMP_CLAUSE_DEFAULT_SHARED:
8653 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
8654 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE)
8655 && c_omp_predefined_variable (t))
8656 /* The __func__ variable and similar function-local predefined
8657 variables may be listed in a shared or firstprivate
8658 clause. */
8659 break;
8660 if (VAR_P (t)
8661 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
8662 && TREE_STATIC (t)
8663 && cxx_omp_const_qual_no_mutable (t))
8665 tree ctx = CP_DECL_CONTEXT (t);
8666 /* const qualified static data members without mutable
8667 member may be specified in firstprivate clause. */
8668 if (TYPE_P (ctx) && MAYBE_CLASS_TYPE_P (ctx))
8669 break;
8671 share_name = "shared";
8672 break;
8673 case OMP_CLAUSE_DEFAULT_PRIVATE:
8674 share_name = "private";
8675 break;
8676 default:
8677 gcc_unreachable ();
8679 if (share_name)
8681 error_at (OMP_CLAUSE_LOCATION (c),
8682 "%qE is predetermined %qs for %qs",
8683 omp_clause_printable_decl (t), share_name,
8684 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
8685 remove = true;
8687 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_SHARED
8688 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_FIRSTPRIVATE
8689 && cxx_omp_const_qual_no_mutable (t))
8691 error_at (OMP_CLAUSE_LOCATION (c),
8692 "%<const%> qualified %qE without %<mutable%> member "
8693 "may appear only in %<shared%> or %<firstprivate%> "
8694 "clauses", omp_clause_printable_decl (t));
8695 remove = true;
8699 if (detach_seen
8700 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
8701 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
8702 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
8703 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
8704 && OMP_CLAUSE_DECL (c) == OMP_CLAUSE_DECL (detach_seen))
8706 error_at (OMP_CLAUSE_LOCATION (c),
8707 "the event handle of a %<detach%> clause "
8708 "should not be in a data-sharing clause");
8709 remove = true;
8712 /* We're interested in the base element, not arrays. */
8713 inner_type = type = TREE_TYPE (t);
8714 if ((need_complete_type
8715 || need_copy_assignment
8716 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
8717 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
8718 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
8719 && TYPE_REF_P (inner_type))
8720 inner_type = TREE_TYPE (inner_type);
8721 while (TREE_CODE (inner_type) == ARRAY_TYPE)
8722 inner_type = TREE_TYPE (inner_type);
8724 /* Check for special function availability by building a call to one.
8725 Save the results, because later we won't be in the right context
8726 for making these queries. */
8727 if (CLASS_TYPE_P (inner_type)
8728 && COMPLETE_TYPE_P (inner_type)
8729 && (need_default_ctor || need_copy_ctor
8730 || need_copy_assignment || need_dtor)
8731 && !type_dependent_expression_p (t)
8732 && cxx_omp_create_clause_info (c, inner_type, need_default_ctor,
8733 need_copy_ctor, need_copy_assignment,
8734 need_dtor))
8735 remove = true;
8737 if (!remove
8738 && c_kind == OMP_CLAUSE_SHARED
8739 && processing_template_decl)
8741 t = omp_clause_decl_field (OMP_CLAUSE_DECL (c));
8742 if (t)
8743 OMP_CLAUSE_DECL (c) = t;
8746 if (remove)
8747 *pc = OMP_CLAUSE_CHAIN (c);
8748 else
8749 pc = &OMP_CLAUSE_CHAIN (c);
8752 if (allocate_seen)
8753 for (pc = &clauses, c = clauses; c ; c = *pc)
8755 bool remove = false;
8756 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ALLOCATE
8757 && !OMP_CLAUSE_ALLOCATE_COMBINED (c)
8758 && DECL_P (OMP_CLAUSE_DECL (c))
8759 && bitmap_bit_p (&aligned_head, DECL_UID (OMP_CLAUSE_DECL (c))))
8761 error_at (OMP_CLAUSE_LOCATION (c),
8762 "%qD specified in %<allocate%> clause but not in "
8763 "an explicit privatization clause", OMP_CLAUSE_DECL (c));
8764 remove = true;
8766 if (remove)
8767 *pc = OMP_CLAUSE_CHAIN (c);
8768 else
8769 pc = &OMP_CLAUSE_CHAIN (c);
8772 bitmap_obstack_release (NULL);
8773 return clauses;
8776 /* Start processing OpenMP clauses that can include any
8777 privatization clauses for non-static data members. */
8779 tree
8780 push_omp_privatization_clauses (bool ignore_next)
8782 if (omp_private_member_ignore_next)
8784 omp_private_member_ignore_next = ignore_next;
8785 return NULL_TREE;
8787 omp_private_member_ignore_next = ignore_next;
8788 if (omp_private_member_map)
8789 omp_private_member_vec.safe_push (error_mark_node);
8790 return push_stmt_list ();
8793 /* Revert remapping of any non-static data members since
8794 the last push_omp_privatization_clauses () call. */
8796 void
8797 pop_omp_privatization_clauses (tree stmt)
8799 if (stmt == NULL_TREE)
8800 return;
8801 stmt = pop_stmt_list (stmt);
8802 if (omp_private_member_map)
8804 while (!omp_private_member_vec.is_empty ())
8806 tree t = omp_private_member_vec.pop ();
8807 if (t == error_mark_node)
8809 add_stmt (stmt);
8810 return;
8812 bool no_decl_expr = t == integer_zero_node;
8813 if (no_decl_expr)
8814 t = omp_private_member_vec.pop ();
8815 tree *v = omp_private_member_map->get (t);
8816 gcc_assert (v);
8817 if (!no_decl_expr)
8818 add_decl_expr (*v);
8819 omp_private_member_map->remove (t);
8821 delete omp_private_member_map;
8822 omp_private_member_map = NULL;
8824 add_stmt (stmt);
8827 /* Remember OpenMP privatization clauses mapping and clear it.
8828 Used for lambdas. */
8830 void
8831 save_omp_privatization_clauses (vec<tree> &save)
8833 save = vNULL;
8834 if (omp_private_member_ignore_next)
8835 save.safe_push (integer_one_node);
8836 omp_private_member_ignore_next = false;
8837 if (!omp_private_member_map)
8838 return;
8840 while (!omp_private_member_vec.is_empty ())
8842 tree t = omp_private_member_vec.pop ();
8843 if (t == error_mark_node)
8845 save.safe_push (t);
8846 continue;
8848 tree n = t;
8849 if (t == integer_zero_node)
8850 t = omp_private_member_vec.pop ();
8851 tree *v = omp_private_member_map->get (t);
8852 gcc_assert (v);
8853 save.safe_push (*v);
8854 save.safe_push (t);
8855 if (n != t)
8856 save.safe_push (n);
8858 delete omp_private_member_map;
8859 omp_private_member_map = NULL;
8862 /* Restore OpenMP privatization clauses mapping saved by the
8863 above function. */
8865 void
8866 restore_omp_privatization_clauses (vec<tree> &save)
8868 gcc_assert (omp_private_member_vec.is_empty ());
8869 omp_private_member_ignore_next = false;
8870 if (save.is_empty ())
8871 return;
8872 if (save.length () == 1 && save[0] == integer_one_node)
8874 omp_private_member_ignore_next = true;
8875 save.release ();
8876 return;
8879 omp_private_member_map = new hash_map <tree, tree>;
8880 while (!save.is_empty ())
8882 tree t = save.pop ();
8883 tree n = t;
8884 if (t != error_mark_node)
8886 if (t == integer_one_node)
8888 omp_private_member_ignore_next = true;
8889 gcc_assert (save.is_empty ());
8890 break;
8892 if (t == integer_zero_node)
8893 t = save.pop ();
8894 tree &v = omp_private_member_map->get_or_insert (t);
8895 v = save.pop ();
8897 omp_private_member_vec.safe_push (t);
8898 if (n != t)
8899 omp_private_member_vec.safe_push (n);
8901 save.release ();
8904 /* For all variables in the tree_list VARS, mark them as thread local. */
8906 void
8907 finish_omp_threadprivate (tree vars)
8909 tree t;
8911 /* Mark every variable in VARS to be assigned thread local storage. */
8912 for (t = vars; t; t = TREE_CHAIN (t))
8914 tree v = TREE_PURPOSE (t);
8916 if (error_operand_p (v))
8918 else if (!VAR_P (v))
8919 error ("%<threadprivate%> %qD is not file, namespace "
8920 "or block scope variable", v);
8921 /* If V had already been marked threadprivate, it doesn't matter
8922 whether it had been used prior to this point. */
8923 else if (TREE_USED (v)
8924 && (DECL_LANG_SPECIFIC (v) == NULL
8925 || !CP_DECL_THREADPRIVATE_P (v)))
8926 error ("%qE declared %<threadprivate%> after first use", v);
8927 else if (! TREE_STATIC (v) && ! DECL_EXTERNAL (v))
8928 error ("automatic variable %qE cannot be %<threadprivate%>", v);
8929 else if (! COMPLETE_TYPE_P (complete_type (TREE_TYPE (v))))
8930 error ("%<threadprivate%> %qE has incomplete type", v);
8931 else if (TREE_STATIC (v) && TYPE_P (CP_DECL_CONTEXT (v))
8932 && CP_DECL_CONTEXT (v) != current_class_type)
8933 error ("%<threadprivate%> %qE directive not "
8934 "in %qT definition", v, CP_DECL_CONTEXT (v));
8935 else
8937 /* Allocate a LANG_SPECIFIC structure for V, if needed. */
8938 if (DECL_LANG_SPECIFIC (v) == NULL)
8939 retrofit_lang_decl (v);
8941 if (! CP_DECL_THREAD_LOCAL_P (v))
8943 CP_DECL_THREAD_LOCAL_P (v) = true;
8944 set_decl_tls_model (v, decl_default_tls_model (v));
8945 /* If rtl has been already set for this var, call
8946 make_decl_rtl once again, so that encode_section_info
8947 has a chance to look at the new decl flags. */
8948 if (DECL_RTL_SET_P (v))
8949 make_decl_rtl (v);
8951 CP_DECL_THREADPRIVATE_P (v) = 1;
8956 /* Build an OpenMP structured block. */
8958 tree
8959 begin_omp_structured_block (void)
8961 return do_pushlevel (sk_omp);
8964 tree
8965 finish_omp_structured_block (tree block)
8967 return do_poplevel (block);
8970 /* Similarly, except force the retention of the BLOCK. */
8972 tree
8973 begin_omp_parallel (void)
8975 keep_next_level (true);
8976 return begin_omp_structured_block ();
8979 /* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
8980 statement. */
8982 tree
8983 finish_oacc_data (tree clauses, tree block)
8985 tree stmt;
8987 block = finish_omp_structured_block (block);
8989 stmt = make_node (OACC_DATA);
8990 TREE_TYPE (stmt) = void_type_node;
8991 OACC_DATA_CLAUSES (stmt) = clauses;
8992 OACC_DATA_BODY (stmt) = block;
8994 return add_stmt (stmt);
8997 /* Generate OACC_HOST_DATA, with CLAUSES and BLOCK as its compound
8998 statement. */
9000 tree
9001 finish_oacc_host_data (tree clauses, tree block)
9003 tree stmt;
9005 block = finish_omp_structured_block (block);
9007 stmt = make_node (OACC_HOST_DATA);
9008 TREE_TYPE (stmt) = void_type_node;
9009 OACC_HOST_DATA_CLAUSES (stmt) = clauses;
9010 OACC_HOST_DATA_BODY (stmt) = block;
9012 return add_stmt (stmt);
9015 /* Generate OMP construct CODE, with BODY and CLAUSES as its compound
9016 statement. */
9018 tree
9019 finish_omp_construct (enum tree_code code, tree body, tree clauses)
9021 body = finish_omp_structured_block (body);
9023 tree stmt = make_node (code);
9024 TREE_TYPE (stmt) = void_type_node;
9025 OMP_BODY (stmt) = body;
9026 OMP_CLAUSES (stmt) = clauses;
9028 return add_stmt (stmt);
9031 tree
9032 finish_omp_parallel (tree clauses, tree body)
9034 tree stmt;
9036 body = finish_omp_structured_block (body);
9038 stmt = make_node (OMP_PARALLEL);
9039 TREE_TYPE (stmt) = void_type_node;
9040 OMP_PARALLEL_CLAUSES (stmt) = clauses;
9041 OMP_PARALLEL_BODY (stmt) = body;
9043 return add_stmt (stmt);
9046 tree
9047 begin_omp_task (void)
9049 keep_next_level (true);
9050 return begin_omp_structured_block ();
9053 tree
9054 finish_omp_task (tree clauses, tree body)
9056 tree stmt;
9058 body = finish_omp_structured_block (body);
9060 stmt = make_node (OMP_TASK);
9061 TREE_TYPE (stmt) = void_type_node;
9062 OMP_TASK_CLAUSES (stmt) = clauses;
9063 OMP_TASK_BODY (stmt) = body;
9065 return add_stmt (stmt);
9068 /* Helper function for finish_omp_for. Convert Ith random access iterator
9069 into integral iterator. Return FALSE if successful. */
9071 static bool
9072 handle_omp_for_class_iterator (int i, location_t locus, enum tree_code code,
9073 tree declv, tree orig_declv, tree initv,
9074 tree condv, tree incrv, tree *body,
9075 tree *pre_body, tree &clauses,
9076 int collapse, int ordered)
9078 tree diff, iter_init, iter_incr = NULL, last;
9079 tree incr_var = NULL, orig_pre_body, orig_body, c;
9080 tree decl = TREE_VEC_ELT (declv, i);
9081 tree init = TREE_VEC_ELT (initv, i);
9082 tree cond = TREE_VEC_ELT (condv, i);
9083 tree incr = TREE_VEC_ELT (incrv, i);
9084 tree iter = decl;
9085 location_t elocus = locus;
9087 if (init && EXPR_HAS_LOCATION (init))
9088 elocus = EXPR_LOCATION (init);
9090 switch (TREE_CODE (cond))
9092 case GT_EXPR:
9093 case GE_EXPR:
9094 case LT_EXPR:
9095 case LE_EXPR:
9096 case NE_EXPR:
9097 if (TREE_OPERAND (cond, 1) == iter)
9098 cond = build2 (swap_tree_comparison (TREE_CODE (cond)),
9099 TREE_TYPE (cond), iter, TREE_OPERAND (cond, 0));
9100 if (TREE_OPERAND (cond, 0) != iter)
9101 cond = error_mark_node;
9102 else
9104 tree tem = build_x_binary_op (EXPR_LOCATION (cond),
9105 TREE_CODE (cond),
9106 iter, ERROR_MARK,
9107 TREE_OPERAND (cond, 1), ERROR_MARK,
9108 NULL, tf_warning_or_error);
9109 if (error_operand_p (tem))
9110 return true;
9112 break;
9113 default:
9114 cond = error_mark_node;
9115 break;
9117 if (cond == error_mark_node)
9119 error_at (elocus, "invalid controlling predicate");
9120 return true;
9122 diff = build_x_binary_op (elocus, MINUS_EXPR, TREE_OPERAND (cond, 1),
9123 ERROR_MARK, iter, ERROR_MARK, NULL,
9124 tf_warning_or_error);
9125 diff = cp_fully_fold (diff);
9126 if (error_operand_p (diff))
9127 return true;
9128 if (TREE_CODE (TREE_TYPE (diff)) != INTEGER_TYPE)
9130 error_at (elocus, "difference between %qE and %qD does not have integer type",
9131 TREE_OPERAND (cond, 1), iter);
9132 return true;
9134 if (!c_omp_check_loop_iv_exprs (locus, orig_declv, i,
9135 TREE_VEC_ELT (declv, i), NULL_TREE,
9136 cond, cp_walk_subtrees))
9137 return true;
9139 switch (TREE_CODE (incr))
9141 case PREINCREMENT_EXPR:
9142 case PREDECREMENT_EXPR:
9143 case POSTINCREMENT_EXPR:
9144 case POSTDECREMENT_EXPR:
9145 if (TREE_OPERAND (incr, 0) != iter)
9147 incr = error_mark_node;
9148 break;
9150 iter_incr = build_x_unary_op (EXPR_LOCATION (incr),
9151 TREE_CODE (incr), iter,
9152 tf_warning_or_error);
9153 if (error_operand_p (iter_incr))
9154 return true;
9155 else if (TREE_CODE (incr) == PREINCREMENT_EXPR
9156 || TREE_CODE (incr) == POSTINCREMENT_EXPR)
9157 incr = integer_one_node;
9158 else
9159 incr = integer_minus_one_node;
9160 break;
9161 case MODIFY_EXPR:
9162 if (TREE_OPERAND (incr, 0) != iter)
9163 incr = error_mark_node;
9164 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
9165 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
9167 tree rhs = TREE_OPERAND (incr, 1);
9168 if (TREE_OPERAND (rhs, 0) == iter)
9170 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (rhs, 1)))
9171 != INTEGER_TYPE)
9172 incr = error_mark_node;
9173 else
9175 iter_incr = build_x_modify_expr (EXPR_LOCATION (rhs),
9176 iter, TREE_CODE (rhs),
9177 TREE_OPERAND (rhs, 1),
9178 tf_warning_or_error);
9179 if (error_operand_p (iter_incr))
9180 return true;
9181 incr = TREE_OPERAND (rhs, 1);
9182 incr = cp_convert (TREE_TYPE (diff), incr,
9183 tf_warning_or_error);
9184 if (TREE_CODE (rhs) == MINUS_EXPR)
9186 incr = build1 (NEGATE_EXPR, TREE_TYPE (diff), incr);
9187 incr = fold_simple (incr);
9189 if (TREE_CODE (incr) != INTEGER_CST
9190 && (TREE_CODE (incr) != NOP_EXPR
9191 || (TREE_CODE (TREE_OPERAND (incr, 0))
9192 != INTEGER_CST)))
9193 iter_incr = NULL;
9196 else if (TREE_OPERAND (rhs, 1) == iter)
9198 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (rhs, 0))) != INTEGER_TYPE
9199 || TREE_CODE (rhs) != PLUS_EXPR)
9200 incr = error_mark_node;
9201 else
9203 iter_incr = build_x_binary_op (EXPR_LOCATION (rhs),
9204 PLUS_EXPR,
9205 TREE_OPERAND (rhs, 0),
9206 ERROR_MARK, iter,
9207 ERROR_MARK, NULL,
9208 tf_warning_or_error);
9209 if (error_operand_p (iter_incr))
9210 return true;
9211 iter_incr = build_x_modify_expr (EXPR_LOCATION (rhs),
9212 iter, NOP_EXPR,
9213 iter_incr,
9214 tf_warning_or_error);
9215 if (error_operand_p (iter_incr))
9216 return true;
9217 incr = TREE_OPERAND (rhs, 0);
9218 iter_incr = NULL;
9221 else
9222 incr = error_mark_node;
9224 else
9225 incr = error_mark_node;
9226 break;
9227 default:
9228 incr = error_mark_node;
9229 break;
9232 if (incr == error_mark_node)
9234 error_at (elocus, "invalid increment expression");
9235 return true;
9238 incr = cp_convert (TREE_TYPE (diff), incr, tf_warning_or_error);
9239 incr = cp_fully_fold (incr);
9240 tree loop_iv_seen = NULL_TREE;
9241 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
9242 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
9243 && OMP_CLAUSE_DECL (c) == iter)
9245 if (code == OMP_TASKLOOP || code == OMP_LOOP)
9247 loop_iv_seen = c;
9248 OMP_CLAUSE_LASTPRIVATE_LOOP_IV (c) = 1;
9250 break;
9252 else if ((code == OMP_TASKLOOP || code == OMP_LOOP)
9253 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
9254 && OMP_CLAUSE_DECL (c) == iter)
9256 loop_iv_seen = c;
9257 if (code == OMP_TASKLOOP)
9258 OMP_CLAUSE_PRIVATE_TASKLOOP_IV (c) = 1;
9261 decl = create_temporary_var (TREE_TYPE (diff));
9262 pushdecl (decl);
9263 add_decl_expr (decl);
9264 last = create_temporary_var (TREE_TYPE (diff));
9265 pushdecl (last);
9266 add_decl_expr (last);
9267 if (c && iter_incr == NULL && TREE_CODE (incr) != INTEGER_CST
9268 && (!ordered || (i < collapse && collapse > 1)))
9270 incr_var = create_temporary_var (TREE_TYPE (diff));
9271 pushdecl (incr_var);
9272 add_decl_expr (incr_var);
9274 gcc_assert (stmts_are_full_exprs_p ());
9275 tree diffvar = NULL_TREE;
9276 if (code == OMP_TASKLOOP)
9278 if (!loop_iv_seen)
9280 tree ivc = build_omp_clause (locus, OMP_CLAUSE_FIRSTPRIVATE);
9281 OMP_CLAUSE_DECL (ivc) = iter;
9282 cxx_omp_finish_clause (ivc, NULL, false);
9283 OMP_CLAUSE_CHAIN (ivc) = clauses;
9284 clauses = ivc;
9286 tree lvc = build_omp_clause (locus, OMP_CLAUSE_FIRSTPRIVATE);
9287 OMP_CLAUSE_DECL (lvc) = last;
9288 OMP_CLAUSE_CHAIN (lvc) = clauses;
9289 clauses = lvc;
9290 diffvar = create_temporary_var (TREE_TYPE (diff));
9291 pushdecl (diffvar);
9292 add_decl_expr (diffvar);
9294 else if (code == OMP_LOOP)
9296 if (!loop_iv_seen)
9298 /* While iterators on the loop construct are predetermined
9299 lastprivate, if the decl is not declared inside of the
9300 loop, OMP_CLAUSE_LASTPRIVATE should have been added
9301 already. */
9302 loop_iv_seen = build_omp_clause (locus, OMP_CLAUSE_FIRSTPRIVATE);
9303 OMP_CLAUSE_DECL (loop_iv_seen) = iter;
9304 OMP_CLAUSE_CHAIN (loop_iv_seen) = clauses;
9305 clauses = loop_iv_seen;
9307 else if (OMP_CLAUSE_CODE (loop_iv_seen) == OMP_CLAUSE_PRIVATE)
9309 OMP_CLAUSE_PRIVATE_DEBUG (loop_iv_seen) = 0;
9310 OMP_CLAUSE_PRIVATE_OUTER_REF (loop_iv_seen) = 0;
9311 OMP_CLAUSE_CODE (loop_iv_seen) = OMP_CLAUSE_FIRSTPRIVATE;
9313 if (OMP_CLAUSE_CODE (loop_iv_seen) == OMP_CLAUSE_FIRSTPRIVATE)
9314 cxx_omp_finish_clause (loop_iv_seen, NULL, false);
9317 orig_pre_body = *pre_body;
9318 *pre_body = push_stmt_list ();
9319 if (orig_pre_body)
9320 add_stmt (orig_pre_body);
9321 if (init != NULL)
9322 finish_expr_stmt (build_x_modify_expr (elocus,
9323 iter, NOP_EXPR, init,
9324 tf_warning_or_error));
9325 init = build_int_cst (TREE_TYPE (diff), 0);
9326 if (c && iter_incr == NULL
9327 && (!ordered || (i < collapse && collapse > 1)))
9329 if (incr_var)
9331 finish_expr_stmt (build_x_modify_expr (elocus,
9332 incr_var, NOP_EXPR,
9333 incr, tf_warning_or_error));
9334 incr = incr_var;
9336 iter_incr = build_x_modify_expr (elocus,
9337 iter, PLUS_EXPR, incr,
9338 tf_warning_or_error);
9340 if (c && ordered && i < collapse && collapse > 1)
9341 iter_incr = incr;
9342 finish_expr_stmt (build_x_modify_expr (elocus,
9343 last, NOP_EXPR, init,
9344 tf_warning_or_error));
9345 if (diffvar)
9347 finish_expr_stmt (build_x_modify_expr (elocus,
9348 diffvar, NOP_EXPR,
9349 diff, tf_warning_or_error));
9350 diff = diffvar;
9352 *pre_body = pop_stmt_list (*pre_body);
9354 cond = cp_build_binary_op (elocus,
9355 TREE_CODE (cond), decl, diff,
9356 tf_warning_or_error);
9357 incr = build_modify_expr (elocus, decl, NULL_TREE, PLUS_EXPR,
9358 elocus, incr, NULL_TREE);
9360 orig_body = *body;
9361 *body = push_stmt_list ();
9362 iter_init = build2 (MINUS_EXPR, TREE_TYPE (diff), decl, last);
9363 iter_init = build_x_modify_expr (elocus,
9364 iter, PLUS_EXPR, iter_init,
9365 tf_warning_or_error);
9366 if (iter_init != error_mark_node)
9367 iter_init = build1 (NOP_EXPR, void_type_node, iter_init);
9368 finish_expr_stmt (iter_init);
9369 finish_expr_stmt (build_x_modify_expr (elocus,
9370 last, NOP_EXPR, decl,
9371 tf_warning_or_error));
9372 add_stmt (orig_body);
9373 *body = pop_stmt_list (*body);
9375 if (c)
9377 OMP_CLAUSE_LASTPRIVATE_STMT (c) = push_stmt_list ();
9378 if (!ordered)
9379 finish_expr_stmt (iter_incr);
9380 else
9382 iter_init = decl;
9383 if (i < collapse && collapse > 1 && !error_operand_p (iter_incr))
9384 iter_init = build2 (PLUS_EXPR, TREE_TYPE (diff),
9385 iter_init, iter_incr);
9386 iter_init = build2 (MINUS_EXPR, TREE_TYPE (diff), iter_init, last);
9387 iter_init = build_x_modify_expr (elocus,
9388 iter, PLUS_EXPR, iter_init,
9389 tf_warning_or_error);
9390 if (iter_init != error_mark_node)
9391 iter_init = build1 (NOP_EXPR, void_type_node, iter_init);
9392 finish_expr_stmt (iter_init);
9394 OMP_CLAUSE_LASTPRIVATE_STMT (c)
9395 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (c));
9398 if (TREE_CODE (TREE_VEC_ELT (orig_declv, i)) == TREE_LIST)
9400 tree t = TREE_VEC_ELT (orig_declv, i);
9401 gcc_assert (TREE_PURPOSE (t) == NULL_TREE
9402 && TREE_VALUE (t) == NULL_TREE
9403 && TREE_CODE (TREE_CHAIN (t)) == TREE_VEC);
9404 TREE_PURPOSE (t) = TREE_VEC_ELT (declv, i);
9405 TREE_VALUE (t) = last;
9407 else
9408 TREE_VEC_ELT (orig_declv, i)
9409 = tree_cons (TREE_VEC_ELT (declv, i), last, NULL_TREE);
9410 TREE_VEC_ELT (declv, i) = decl;
9411 TREE_VEC_ELT (initv, i) = init;
9412 TREE_VEC_ELT (condv, i) = cond;
9413 TREE_VEC_ELT (incrv, i) = incr;
9415 return false;
9418 /* Build and validate an OMP_FOR statement. CLAUSES, BODY, COND, INCR
9419 are directly for their associated operands in the statement. DECL
9420 and INIT are a combo; if DECL is NULL then INIT ought to be a
9421 MODIFY_EXPR, and the DECL should be extracted. PRE_BODY are
9422 optional statements that need to go before the loop into its
9423 sk_omp scope. */
9425 tree
9426 finish_omp_for (location_t locus, enum tree_code code, tree declv,
9427 tree orig_declv, tree initv, tree condv, tree incrv,
9428 tree body, tree pre_body, vec<tree> *orig_inits, tree clauses)
9430 tree omp_for = NULL, orig_incr = NULL;
9431 tree decl = NULL, init, cond, incr;
9432 location_t elocus;
9433 int i;
9434 int collapse = 1;
9435 int ordered = 0;
9437 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (initv));
9438 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (condv));
9439 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (incrv));
9440 if (TREE_VEC_LENGTH (declv) > 1)
9442 tree c;
9444 c = omp_find_clause (clauses, OMP_CLAUSE_TILE);
9445 if (c)
9446 collapse = list_length (OMP_CLAUSE_TILE_LIST (c));
9447 else
9449 c = omp_find_clause (clauses, OMP_CLAUSE_COLLAPSE);
9450 if (c)
9451 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (c));
9452 if (collapse != TREE_VEC_LENGTH (declv))
9453 ordered = TREE_VEC_LENGTH (declv);
9456 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
9458 decl = TREE_VEC_ELT (declv, i);
9459 init = TREE_VEC_ELT (initv, i);
9460 cond = TREE_VEC_ELT (condv, i);
9461 incr = TREE_VEC_ELT (incrv, i);
9462 elocus = locus;
9464 if (decl == NULL)
9466 if (init != NULL)
9467 switch (TREE_CODE (init))
9469 case MODIFY_EXPR:
9470 decl = TREE_OPERAND (init, 0);
9471 init = TREE_OPERAND (init, 1);
9472 break;
9473 case MODOP_EXPR:
9474 if (TREE_CODE (TREE_OPERAND (init, 1)) == NOP_EXPR)
9476 decl = TREE_OPERAND (init, 0);
9477 init = TREE_OPERAND (init, 2);
9479 break;
9480 default:
9481 break;
9484 if (decl == NULL)
9486 error_at (locus,
9487 "expected iteration declaration or initialization");
9488 return NULL;
9492 if (init && EXPR_HAS_LOCATION (init))
9493 elocus = EXPR_LOCATION (init);
9495 if (cond == global_namespace)
9496 continue;
9498 if (cond == NULL)
9500 error_at (elocus, "missing controlling predicate");
9501 return NULL;
9504 if (incr == NULL)
9506 error_at (elocus, "missing increment expression");
9507 return NULL;
9510 TREE_VEC_ELT (declv, i) = decl;
9511 TREE_VEC_ELT (initv, i) = init;
9514 if (orig_inits)
9516 bool fail = false;
9517 tree orig_init;
9518 FOR_EACH_VEC_ELT (*orig_inits, i, orig_init)
9519 if (orig_init
9520 && !c_omp_check_loop_iv_exprs (locus,
9521 orig_declv ? orig_declv : declv, i,
9522 TREE_VEC_ELT (declv, i), orig_init,
9523 NULL_TREE, cp_walk_subtrees))
9524 fail = true;
9525 if (fail)
9526 return NULL;
9529 if (dependent_omp_for_p (declv, initv, condv, incrv))
9531 tree stmt;
9533 stmt = make_node (code);
9535 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
9537 /* This is really just a place-holder. We'll be decomposing this
9538 again and going through the cp_build_modify_expr path below when
9539 we instantiate the thing. */
9540 TREE_VEC_ELT (initv, i)
9541 = build2 (MODIFY_EXPR, void_type_node, TREE_VEC_ELT (declv, i),
9542 TREE_VEC_ELT (initv, i));
9545 TREE_TYPE (stmt) = void_type_node;
9546 OMP_FOR_INIT (stmt) = initv;
9547 OMP_FOR_COND (stmt) = condv;
9548 OMP_FOR_INCR (stmt) = incrv;
9549 OMP_FOR_BODY (stmt) = body;
9550 OMP_FOR_PRE_BODY (stmt) = pre_body;
9551 OMP_FOR_CLAUSES (stmt) = clauses;
9553 SET_EXPR_LOCATION (stmt, locus);
9554 return add_stmt (stmt);
9557 if (!orig_declv)
9558 orig_declv = copy_node (declv);
9560 if (processing_template_decl)
9561 orig_incr = make_tree_vec (TREE_VEC_LENGTH (incrv));
9563 for (i = 0; i < TREE_VEC_LENGTH (declv); )
9565 decl = TREE_VEC_ELT (declv, i);
9566 init = TREE_VEC_ELT (initv, i);
9567 cond = TREE_VEC_ELT (condv, i);
9568 incr = TREE_VEC_ELT (incrv, i);
9569 if (orig_incr)
9570 TREE_VEC_ELT (orig_incr, i) = incr;
9571 elocus = locus;
9573 if (init && EXPR_HAS_LOCATION (init))
9574 elocus = EXPR_LOCATION (init);
9576 if (!DECL_P (decl))
9578 error_at (elocus, "expected iteration declaration or initialization");
9579 return NULL;
9582 if (incr && TREE_CODE (incr) == MODOP_EXPR)
9584 if (orig_incr)
9585 TREE_VEC_ELT (orig_incr, i) = incr;
9586 incr = cp_build_modify_expr (elocus, TREE_OPERAND (incr, 0),
9587 TREE_CODE (TREE_OPERAND (incr, 1)),
9588 TREE_OPERAND (incr, 2),
9589 tf_warning_or_error);
9592 if (CLASS_TYPE_P (TREE_TYPE (decl)))
9594 if (code == OMP_SIMD)
9596 error_at (elocus, "%<#pragma omp simd%> used with class "
9597 "iteration variable %qE", decl);
9598 return NULL;
9600 if (handle_omp_for_class_iterator (i, locus, code, declv, orig_declv,
9601 initv, condv, incrv, &body,
9602 &pre_body, clauses,
9603 collapse, ordered))
9604 return NULL;
9605 continue;
9608 if (!INTEGRAL_TYPE_P (TREE_TYPE (decl))
9609 && !TYPE_PTR_P (TREE_TYPE (decl)))
9611 error_at (elocus, "invalid type for iteration variable %qE", decl);
9612 return NULL;
9615 if (!processing_template_decl && TREE_CODE (init) != TREE_VEC)
9616 init = cp_build_modify_expr (elocus, decl, NOP_EXPR, init,
9617 tf_warning_or_error);
9618 else
9619 init = build2 (MODIFY_EXPR, void_type_node, decl, init);
9620 if (decl == error_mark_node || init == error_mark_node)
9621 return NULL;
9623 TREE_VEC_ELT (declv, i) = decl;
9624 TREE_VEC_ELT (initv, i) = init;
9625 TREE_VEC_ELT (condv, i) = cond;
9626 TREE_VEC_ELT (incrv, i) = incr;
9627 i++;
9630 if (pre_body && IS_EMPTY_STMT (pre_body))
9631 pre_body = NULL;
9633 omp_for = c_finish_omp_for (locus, code, declv, orig_declv, initv, condv,
9634 incrv, body, pre_body,
9635 !processing_template_decl);
9637 /* Check for iterators appearing in lb, b or incr expressions. */
9638 if (omp_for && !c_omp_check_loop_iv (omp_for, orig_declv, cp_walk_subtrees))
9639 omp_for = NULL_TREE;
9641 if (omp_for == NULL)
9642 return NULL;
9644 add_stmt (omp_for);
9646 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INCR (omp_for)); i++)
9648 init = TREE_VEC_ELT (OMP_FOR_INIT (omp_for), i);
9649 decl = TREE_OPERAND (init, 0);
9650 cond = TREE_VEC_ELT (OMP_FOR_COND (omp_for), i);
9651 incr = TREE_VEC_ELT (OMP_FOR_INCR (omp_for), i);
9653 if (!processing_template_decl)
9655 if (TREE_CODE (TREE_OPERAND (init, 1)) == TREE_VEC)
9657 tree t = TREE_VEC_ELT (TREE_OPERAND (init, 1), 1);
9658 TREE_VEC_ELT (TREE_OPERAND (init, 1), 1)
9659 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
9660 t = TREE_VEC_ELT (TREE_OPERAND (init, 1), 2);
9661 TREE_VEC_ELT (TREE_OPERAND (init, 1), 2)
9662 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
9664 else
9666 tree t = TREE_OPERAND (init, 1);
9667 TREE_OPERAND (init, 1)
9668 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
9670 if (TREE_CODE (TREE_OPERAND (cond, 1)) == TREE_VEC)
9672 tree t = TREE_VEC_ELT (TREE_OPERAND (cond, 1), 1);
9673 TREE_VEC_ELT (TREE_OPERAND (cond, 1), 1)
9674 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
9675 t = TREE_VEC_ELT (TREE_OPERAND (cond, 1), 2);
9676 TREE_VEC_ELT (TREE_OPERAND (cond, 1), 2)
9677 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
9679 else
9681 tree t = TREE_OPERAND (cond, 1);
9682 TREE_OPERAND (cond, 1)
9683 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
9687 if (TREE_CODE (incr) != MODIFY_EXPR)
9688 continue;
9690 if (TREE_SIDE_EFFECTS (TREE_OPERAND (incr, 1))
9691 && BINARY_CLASS_P (TREE_OPERAND (incr, 1))
9692 && !processing_template_decl)
9694 tree t = TREE_OPERAND (TREE_OPERAND (incr, 1), 0);
9695 if (TREE_SIDE_EFFECTS (t)
9696 && t != decl
9697 && (TREE_CODE (t) != NOP_EXPR
9698 || TREE_OPERAND (t, 0) != decl))
9699 TREE_OPERAND (TREE_OPERAND (incr, 1), 0)
9700 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
9702 t = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
9703 if (TREE_SIDE_EFFECTS (t)
9704 && t != decl
9705 && (TREE_CODE (t) != NOP_EXPR
9706 || TREE_OPERAND (t, 0) != decl))
9707 TREE_OPERAND (TREE_OPERAND (incr, 1), 1)
9708 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
9711 if (orig_incr)
9712 TREE_VEC_ELT (OMP_FOR_INCR (omp_for), i) = TREE_VEC_ELT (orig_incr, i);
9714 OMP_FOR_CLAUSES (omp_for) = clauses;
9716 /* For simd loops with non-static data member iterators, we could have added
9717 OMP_CLAUSE_LINEAR clauses without OMP_CLAUSE_LINEAR_STEP. As we know the
9718 step at this point, fill it in. */
9719 if (code == OMP_SIMD && !processing_template_decl
9720 && TREE_VEC_LENGTH (OMP_FOR_INCR (omp_for)) == 1)
9721 for (tree c = omp_find_clause (clauses, OMP_CLAUSE_LINEAR); c;
9722 c = omp_find_clause (OMP_CLAUSE_CHAIN (c), OMP_CLAUSE_LINEAR))
9723 if (OMP_CLAUSE_LINEAR_STEP (c) == NULL_TREE)
9725 decl = TREE_OPERAND (TREE_VEC_ELT (OMP_FOR_INIT (omp_for), 0), 0);
9726 gcc_assert (decl == OMP_CLAUSE_DECL (c));
9727 incr = TREE_VEC_ELT (OMP_FOR_INCR (omp_for), 0);
9728 tree step, stept;
9729 switch (TREE_CODE (incr))
9731 case PREINCREMENT_EXPR:
9732 case POSTINCREMENT_EXPR:
9733 /* c_omp_for_incr_canonicalize_ptr() should have been
9734 called to massage things appropriately. */
9735 gcc_assert (!INDIRECT_TYPE_P (TREE_TYPE (decl)));
9736 OMP_CLAUSE_LINEAR_STEP (c) = build_int_cst (TREE_TYPE (decl), 1);
9737 break;
9738 case PREDECREMENT_EXPR:
9739 case POSTDECREMENT_EXPR:
9740 /* c_omp_for_incr_canonicalize_ptr() should have been
9741 called to massage things appropriately. */
9742 gcc_assert (!INDIRECT_TYPE_P (TREE_TYPE (decl)));
9743 OMP_CLAUSE_LINEAR_STEP (c)
9744 = build_int_cst (TREE_TYPE (decl), -1);
9745 break;
9746 case MODIFY_EXPR:
9747 gcc_assert (TREE_OPERAND (incr, 0) == decl);
9748 incr = TREE_OPERAND (incr, 1);
9749 switch (TREE_CODE (incr))
9751 case PLUS_EXPR:
9752 if (TREE_OPERAND (incr, 1) == decl)
9753 step = TREE_OPERAND (incr, 0);
9754 else
9755 step = TREE_OPERAND (incr, 1);
9756 break;
9757 case MINUS_EXPR:
9758 case POINTER_PLUS_EXPR:
9759 gcc_assert (TREE_OPERAND (incr, 0) == decl);
9760 step = TREE_OPERAND (incr, 1);
9761 break;
9762 default:
9763 gcc_unreachable ();
9765 stept = TREE_TYPE (decl);
9766 if (INDIRECT_TYPE_P (stept))
9767 stept = sizetype;
9768 step = fold_convert (stept, step);
9769 if (TREE_CODE (incr) == MINUS_EXPR)
9770 step = fold_build1 (NEGATE_EXPR, stept, step);
9771 OMP_CLAUSE_LINEAR_STEP (c) = step;
9772 break;
9773 default:
9774 gcc_unreachable ();
9777 /* Override saved methods on OMP_LOOP's OMP_CLAUSE_LASTPRIVATE_LOOP_IV
9778 clauses, we need copy ctor for those rather than default ctor,
9779 plus as for other lastprivates assignment op and dtor. */
9780 if (code == OMP_LOOP && !processing_template_decl)
9781 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
9782 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
9783 && OMP_CLAUSE_LASTPRIVATE_LOOP_IV (c)
9784 && cxx_omp_create_clause_info (c, TREE_TYPE (OMP_CLAUSE_DECL (c)),
9785 false, true, true, true))
9786 CP_OMP_CLAUSE_INFO (c) = NULL_TREE;
9788 return omp_for;
9791 /* Fix up range for decls. Those decls were pushed into BIND's BIND_EXPR_VARS
9792 and need to be moved into the BIND_EXPR inside of the OMP_FOR's body. */
9794 tree
9795 finish_omp_for_block (tree bind, tree omp_for)
9797 if (omp_for == NULL_TREE
9798 || !OMP_FOR_ORIG_DECLS (omp_for)
9799 || bind == NULL_TREE
9800 || TREE_CODE (bind) != BIND_EXPR)
9801 return bind;
9802 tree b = NULL_TREE;
9803 for (int i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (omp_for)); i++)
9804 if (TREE_CODE (TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (omp_for), i)) == TREE_LIST
9805 && TREE_CHAIN (TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (omp_for), i)))
9807 tree v = TREE_CHAIN (TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (omp_for), i));
9808 gcc_assert (BIND_EXPR_BLOCK (bind)
9809 && (BIND_EXPR_VARS (bind)
9810 == BLOCK_VARS (BIND_EXPR_BLOCK (bind))));
9811 for (int j = 2; j < TREE_VEC_LENGTH (v); j++)
9812 for (tree *p = &BIND_EXPR_VARS (bind); *p; p = &DECL_CHAIN (*p))
9814 if (*p == TREE_VEC_ELT (v, j))
9816 tree var = *p;
9817 *p = DECL_CHAIN (*p);
9818 if (b == NULL_TREE)
9820 b = make_node (BLOCK);
9821 b = build3 (BIND_EXPR, void_type_node, NULL_TREE,
9822 OMP_FOR_BODY (omp_for), b);
9823 TREE_SIDE_EFFECTS (b) = 1;
9824 OMP_FOR_BODY (omp_for) = b;
9826 DECL_CHAIN (var) = BIND_EXPR_VARS (b);
9827 BIND_EXPR_VARS (b) = var;
9828 BLOCK_VARS (BIND_EXPR_BLOCK (b)) = var;
9831 BLOCK_VARS (BIND_EXPR_BLOCK (bind)) = BIND_EXPR_VARS (bind);
9833 return bind;
9836 void
9837 finish_omp_atomic (location_t loc, enum tree_code code, enum tree_code opcode,
9838 tree lhs, tree rhs, tree v, tree lhs1, tree rhs1,
9839 tree clauses, enum omp_memory_order mo)
9841 tree orig_lhs;
9842 tree orig_rhs;
9843 tree orig_v;
9844 tree orig_lhs1;
9845 tree orig_rhs1;
9846 bool dependent_p;
9847 tree stmt;
9849 orig_lhs = lhs;
9850 orig_rhs = rhs;
9851 orig_v = v;
9852 orig_lhs1 = lhs1;
9853 orig_rhs1 = rhs1;
9854 dependent_p = false;
9855 stmt = NULL_TREE;
9857 /* Even in a template, we can detect invalid uses of the atomic
9858 pragma if neither LHS nor RHS is type-dependent. */
9859 if (processing_template_decl)
9861 dependent_p = (type_dependent_expression_p (lhs)
9862 || (rhs && type_dependent_expression_p (rhs))
9863 || (v && type_dependent_expression_p (v))
9864 || (lhs1 && type_dependent_expression_p (lhs1))
9865 || (rhs1 && type_dependent_expression_p (rhs1)));
9866 if (clauses)
9868 gcc_assert (TREE_CODE (clauses) == OMP_CLAUSE
9869 && OMP_CLAUSE_CODE (clauses) == OMP_CLAUSE_HINT
9870 && OMP_CLAUSE_CHAIN (clauses) == NULL_TREE);
9871 if (type_dependent_expression_p (OMP_CLAUSE_HINT_EXPR (clauses))
9872 || TREE_CODE (OMP_CLAUSE_HINT_EXPR (clauses)) != INTEGER_CST)
9873 dependent_p = true;
9875 if (!dependent_p)
9877 lhs = build_non_dependent_expr (lhs);
9878 if (rhs)
9879 rhs = build_non_dependent_expr (rhs);
9880 if (v)
9881 v = build_non_dependent_expr (v);
9882 if (lhs1)
9883 lhs1 = build_non_dependent_expr (lhs1);
9884 if (rhs1)
9885 rhs1 = build_non_dependent_expr (rhs1);
9888 if (!dependent_p)
9890 bool swapped = false;
9891 if (rhs1 && cp_tree_equal (lhs, rhs))
9893 std::swap (rhs, rhs1);
9894 swapped = !commutative_tree_code (opcode);
9896 if (rhs1 && !cp_tree_equal (lhs, rhs1))
9898 if (code == OMP_ATOMIC)
9899 error ("%<#pragma omp atomic update%> uses two different "
9900 "expressions for memory");
9901 else
9902 error ("%<#pragma omp atomic capture%> uses two different "
9903 "expressions for memory");
9904 return;
9906 if (lhs1 && !cp_tree_equal (lhs, lhs1))
9908 if (code == OMP_ATOMIC)
9909 error ("%<#pragma omp atomic update%> uses two different "
9910 "expressions for memory");
9911 else
9912 error ("%<#pragma omp atomic capture%> uses two different "
9913 "expressions for memory");
9914 return;
9916 stmt = c_finish_omp_atomic (loc, code, opcode, lhs, rhs,
9917 v, lhs1, rhs1, swapped, mo,
9918 processing_template_decl != 0);
9919 if (stmt == error_mark_node)
9920 return;
9922 if (processing_template_decl)
9924 if (code == OMP_ATOMIC_READ)
9926 stmt = build_min_nt_loc (loc, OMP_ATOMIC_READ, orig_lhs);
9927 OMP_ATOMIC_MEMORY_ORDER (stmt) = mo;
9928 stmt = build2 (MODIFY_EXPR, void_type_node, orig_v, stmt);
9930 else
9932 if (opcode == NOP_EXPR)
9933 stmt = build2 (MODIFY_EXPR, void_type_node, orig_lhs, orig_rhs);
9934 else
9935 stmt = build2 (opcode, void_type_node, orig_lhs, orig_rhs);
9936 if (orig_rhs1)
9937 stmt = build_min_nt_loc (EXPR_LOCATION (orig_rhs1),
9938 COMPOUND_EXPR, orig_rhs1, stmt);
9939 if (code != OMP_ATOMIC)
9941 stmt = build_min_nt_loc (loc, code, orig_lhs1, stmt);
9942 OMP_ATOMIC_MEMORY_ORDER (stmt) = mo;
9943 stmt = build2 (MODIFY_EXPR, void_type_node, orig_v, stmt);
9946 stmt = build2 (OMP_ATOMIC, void_type_node,
9947 clauses ? clauses : integer_zero_node, stmt);
9948 OMP_ATOMIC_MEMORY_ORDER (stmt) = mo;
9949 SET_EXPR_LOCATION (stmt, loc);
9952 /* Avoid -Wunused-value warnings here, the whole construct has side-effects
9953 and even if it might be wrapped from fold-const.c or c-omp.c wrapped
9954 in some tree that appears to be unused, the value is not unused. */
9955 warning_sentinel w (warn_unused_value);
9956 finish_expr_stmt (stmt);
9959 void
9960 finish_omp_barrier (void)
9962 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_BARRIER);
9963 releasing_vec vec;
9964 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
9965 finish_expr_stmt (stmt);
9968 void
9969 finish_omp_depobj (location_t loc, tree depobj,
9970 enum omp_clause_depend_kind kind, tree clause)
9972 if (!error_operand_p (depobj) && !type_dependent_expression_p (depobj))
9974 if (!lvalue_p (depobj))
9976 error_at (EXPR_LOC_OR_LOC (depobj, loc),
9977 "%<depobj%> expression is not lvalue expression");
9978 depobj = error_mark_node;
9982 if (processing_template_decl)
9984 if (clause == NULL_TREE)
9985 clause = build_int_cst (integer_type_node, kind);
9986 add_stmt (build_min_nt_loc (loc, OMP_DEPOBJ, depobj, clause));
9987 return;
9990 if (!error_operand_p (depobj))
9992 tree addr = cp_build_addr_expr (depobj, tf_warning_or_error);
9993 if (addr == error_mark_node)
9994 depobj = error_mark_node;
9995 else
9996 depobj = cp_build_indirect_ref (loc, addr, RO_UNARY_STAR,
9997 tf_warning_or_error);
10000 c_finish_omp_depobj (loc, depobj, kind, clause);
10003 void
10004 finish_omp_flush (int mo)
10006 tree fn = builtin_decl_explicit (BUILT_IN_SYNC_SYNCHRONIZE);
10007 releasing_vec vec;
10008 if (mo != MEMMODEL_LAST)
10010 fn = builtin_decl_explicit (BUILT_IN_ATOMIC_THREAD_FENCE);
10011 vec->quick_push (build_int_cst (integer_type_node, mo));
10013 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
10014 finish_expr_stmt (stmt);
10017 void
10018 finish_omp_taskwait (void)
10020 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_TASKWAIT);
10021 releasing_vec vec;
10022 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
10023 finish_expr_stmt (stmt);
10026 void
10027 finish_omp_taskyield (void)
10029 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_TASKYIELD);
10030 releasing_vec vec;
10031 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
10032 finish_expr_stmt (stmt);
10035 void
10036 finish_omp_cancel (tree clauses)
10038 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
10039 int mask = 0;
10040 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
10041 mask = 1;
10042 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
10043 mask = 2;
10044 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
10045 mask = 4;
10046 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
10047 mask = 8;
10048 else
10050 error ("%<#pragma omp cancel%> must specify one of "
10051 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> clauses");
10052 return;
10054 releasing_vec vec;
10055 tree ifc = omp_find_clause (clauses, OMP_CLAUSE_IF);
10056 if (ifc != NULL_TREE)
10058 if (OMP_CLAUSE_IF_MODIFIER (ifc) != ERROR_MARK
10059 && OMP_CLAUSE_IF_MODIFIER (ifc) != VOID_CST)
10060 error_at (OMP_CLAUSE_LOCATION (ifc),
10061 "expected %<cancel%> %<if%> clause modifier");
10062 else
10064 tree ifc2 = omp_find_clause (OMP_CLAUSE_CHAIN (ifc), OMP_CLAUSE_IF);
10065 if (ifc2 != NULL_TREE)
10067 gcc_assert (OMP_CLAUSE_IF_MODIFIER (ifc) == VOID_CST
10068 && OMP_CLAUSE_IF_MODIFIER (ifc2) != ERROR_MARK
10069 && OMP_CLAUSE_IF_MODIFIER (ifc2) != VOID_CST);
10070 error_at (OMP_CLAUSE_LOCATION (ifc2),
10071 "expected %<cancel%> %<if%> clause modifier");
10075 if (!processing_template_decl)
10076 ifc = maybe_convert_cond (OMP_CLAUSE_IF_EXPR (ifc));
10077 else
10078 ifc = build_x_binary_op (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
10079 OMP_CLAUSE_IF_EXPR (ifc), ERROR_MARK,
10080 integer_zero_node, ERROR_MARK,
10081 NULL, tf_warning_or_error);
10083 else
10084 ifc = boolean_true_node;
10085 vec->quick_push (build_int_cst (integer_type_node, mask));
10086 vec->quick_push (ifc);
10087 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
10088 finish_expr_stmt (stmt);
10091 void
10092 finish_omp_cancellation_point (tree clauses)
10094 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
10095 int mask = 0;
10096 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
10097 mask = 1;
10098 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
10099 mask = 2;
10100 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
10101 mask = 4;
10102 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
10103 mask = 8;
10104 else
10106 error ("%<#pragma omp cancellation point%> must specify one of "
10107 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> clauses");
10108 return;
10110 releasing_vec vec
10111 = make_tree_vector_single (build_int_cst (integer_type_node, mask));
10112 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
10113 finish_expr_stmt (stmt);
10116 /* Begin a __transaction_atomic or __transaction_relaxed statement.
10117 If PCOMPOUND is non-null, this is for a function-transaction-block, and we
10118 should create an extra compound stmt. */
10120 tree
10121 begin_transaction_stmt (location_t loc, tree *pcompound, int flags)
10123 tree r;
10125 if (pcompound)
10126 *pcompound = begin_compound_stmt (0);
10128 r = build_stmt (loc, TRANSACTION_EXPR, NULL_TREE);
10130 /* Only add the statement to the function if support enabled. */
10131 if (flag_tm)
10132 add_stmt (r);
10133 else
10134 error_at (loc, ((flags & TM_STMT_ATTR_RELAXED) != 0
10135 ? G_("%<__transaction_relaxed%> without "
10136 "transactional memory support enabled")
10137 : G_("%<__transaction_atomic%> without "
10138 "transactional memory support enabled")));
10140 TRANSACTION_EXPR_BODY (r) = push_stmt_list ();
10141 TREE_SIDE_EFFECTS (r) = 1;
10142 return r;
10145 /* End a __transaction_atomic or __transaction_relaxed statement.
10146 If COMPOUND_STMT is non-null, this is for a function-transaction-block,
10147 and we should end the compound. If NOEX is non-NULL, we wrap the body in
10148 a MUST_NOT_THROW_EXPR with NOEX as condition. */
10150 void
10151 finish_transaction_stmt (tree stmt, tree compound_stmt, int flags, tree noex)
10153 TRANSACTION_EXPR_BODY (stmt) = pop_stmt_list (TRANSACTION_EXPR_BODY (stmt));
10154 TRANSACTION_EXPR_OUTER (stmt) = (flags & TM_STMT_ATTR_OUTER) != 0;
10155 TRANSACTION_EXPR_RELAXED (stmt) = (flags & TM_STMT_ATTR_RELAXED) != 0;
10156 TRANSACTION_EXPR_IS_STMT (stmt) = 1;
10158 /* noexcept specifications are not allowed for function transactions. */
10159 gcc_assert (!(noex && compound_stmt));
10160 if (noex)
10162 tree body = build_must_not_throw_expr (TRANSACTION_EXPR_BODY (stmt),
10163 noex);
10164 protected_set_expr_location
10165 (body, EXPR_LOCATION (TRANSACTION_EXPR_BODY (stmt)));
10166 TREE_SIDE_EFFECTS (body) = 1;
10167 TRANSACTION_EXPR_BODY (stmt) = body;
10170 if (compound_stmt)
10171 finish_compound_stmt (compound_stmt);
10174 /* Build a __transaction_atomic or __transaction_relaxed expression. If
10175 NOEX is non-NULL, we wrap the body in a MUST_NOT_THROW_EXPR with NOEX as
10176 condition. */
10178 tree
10179 build_transaction_expr (location_t loc, tree expr, int flags, tree noex)
10181 tree ret;
10182 if (noex)
10184 expr = build_must_not_throw_expr (expr, noex);
10185 protected_set_expr_location (expr, loc);
10186 TREE_SIDE_EFFECTS (expr) = 1;
10188 ret = build1 (TRANSACTION_EXPR, TREE_TYPE (expr), expr);
10189 if (flags & TM_STMT_ATTR_RELAXED)
10190 TRANSACTION_EXPR_RELAXED (ret) = 1;
10191 TREE_SIDE_EFFECTS (ret) = 1;
10192 SET_EXPR_LOCATION (ret, loc);
10193 return ret;
10196 void
10197 init_cp_semantics (void)
10202 /* If we have a condition in conjunctive normal form (CNF), find the first
10203 failing clause. In other words, given an expression like
10205 true && true && false && true && false
10207 return the first 'false'. EXPR is the expression. */
10209 static tree
10210 find_failing_clause_r (tree expr)
10212 if (TREE_CODE (expr) == TRUTH_ANDIF_EXPR)
10214 /* First check the left side... */
10215 tree e = find_failing_clause_r (TREE_OPERAND (expr, 0));
10216 if (e == NULL_TREE)
10217 /* ...if we didn't find a false clause, check the right side. */
10218 e = find_failing_clause_r (TREE_OPERAND (expr, 1));
10219 return e;
10221 tree e = contextual_conv_bool (expr, tf_none);
10222 e = fold_non_dependent_expr (e, tf_none, /*manifestly_const_eval=*/true);
10223 if (integer_zerop (e))
10224 /* This is the failing clause. */
10225 return expr;
10226 return NULL_TREE;
10229 /* Wrapper for find_failing_clause_r. */
10231 static tree
10232 find_failing_clause (tree expr)
10234 if (TREE_CODE (expr) != TRUTH_ANDIF_EXPR)
10235 return NULL_TREE;
10236 return find_failing_clause_r (expr);
10239 /* Build a STATIC_ASSERT for a static assertion with the condition
10240 CONDITION and the message text MESSAGE. LOCATION is the location
10241 of the static assertion in the source code. When MEMBER_P, this
10242 static assertion is a member of a class. If SHOW_EXPR_P is true,
10243 print the condition (because it was instantiation-dependent). */
10245 void
10246 finish_static_assert (tree condition, tree message, location_t location,
10247 bool member_p, bool show_expr_p)
10249 tsubst_flags_t complain = tf_warning_or_error;
10251 if (message == NULL_TREE
10252 || message == error_mark_node
10253 || condition == NULL_TREE
10254 || condition == error_mark_node)
10255 return;
10257 if (check_for_bare_parameter_packs (condition))
10258 condition = error_mark_node;
10260 if (instantiation_dependent_expression_p (condition))
10262 /* We're in a template; build a STATIC_ASSERT and put it in
10263 the right place. */
10264 tree assertion;
10266 assertion = make_node (STATIC_ASSERT);
10267 STATIC_ASSERT_CONDITION (assertion) = condition;
10268 STATIC_ASSERT_MESSAGE (assertion) = message;
10269 STATIC_ASSERT_SOURCE_LOCATION (assertion) = location;
10271 if (member_p)
10272 maybe_add_class_template_decl_list (current_class_type,
10273 assertion,
10274 /*friend_p=*/0);
10275 else
10276 add_stmt (assertion);
10278 return;
10281 /* Save the condition in case it was a concept check. */
10282 tree orig_condition = condition;
10284 /* Fold the expression and convert it to a boolean value. */
10285 condition = contextual_conv_bool (condition, complain);
10286 condition = fold_non_dependent_expr (condition, complain,
10287 /*manifestly_const_eval=*/true);
10289 if (TREE_CODE (condition) == INTEGER_CST && !integer_zerop (condition))
10290 /* Do nothing; the condition is satisfied. */
10292 else
10294 iloc_sentinel ils (location);
10296 if (integer_zerop (condition))
10298 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
10299 (TREE_TYPE (TREE_TYPE (message))));
10300 int len = TREE_STRING_LENGTH (message) / sz - 1;
10302 /* See if we can find which clause was failing (for logical AND). */
10303 tree bad = find_failing_clause (orig_condition);
10304 /* If not, or its location is unusable, fall back to the previous
10305 location. */
10306 location_t cloc = location;
10307 if (cp_expr_location (bad) != UNKNOWN_LOCATION)
10308 cloc = cp_expr_location (bad);
10310 /* Report the error. */
10311 if (len == 0)
10312 error_at (cloc, "static assertion failed");
10313 else
10314 error_at (cloc, "static assertion failed: %s",
10315 TREE_STRING_POINTER (message));
10316 if (show_expr_p)
10317 inform (cloc, "%qE evaluates to false",
10318 /* Nobody wants to see the artificial (bool) cast. */
10319 (bad ? tree_strip_nop_conversions (bad) : orig_condition));
10321 /* Actually explain the failure if this is a concept check or a
10322 requires-expression. */
10323 if (concept_check_p (orig_condition)
10324 || TREE_CODE (orig_condition) == REQUIRES_EXPR)
10325 diagnose_constraints (location, orig_condition, NULL_TREE);
10327 else if (condition && condition != error_mark_node)
10329 error ("non-constant condition for static assertion");
10330 if (require_rvalue_constant_expression (condition))
10331 cxx_constant_value (condition);
10336 /* Implements the C++0x decltype keyword. Returns the type of EXPR,
10337 suitable for use as a type-specifier.
10339 ID_EXPRESSION_OR_MEMBER_ACCESS_P is true when EXPR was parsed as an
10340 id-expression or a class member access, FALSE when it was parsed as
10341 a full expression. */
10343 tree
10344 finish_decltype_type (tree expr, bool id_expression_or_member_access_p,
10345 tsubst_flags_t complain)
10347 tree type = NULL_TREE;
10349 if (!expr || error_operand_p (expr))
10350 return error_mark_node;
10352 if (TYPE_P (expr)
10353 || TREE_CODE (expr) == TYPE_DECL
10354 || (TREE_CODE (expr) == BIT_NOT_EXPR
10355 && TYPE_P (TREE_OPERAND (expr, 0))))
10357 if (complain & tf_error)
10358 error ("argument to %<decltype%> must be an expression");
10359 return error_mark_node;
10362 /* decltype is an unevaluated context. */
10363 cp_unevaluated u;
10365 /* Depending on the resolution of DR 1172, we may later need to distinguish
10366 instantiation-dependent but not type-dependent expressions so that, say,
10367 A<decltype(sizeof(T))>::U doesn't require 'typename'. */
10368 if (instantiation_dependent_uneval_expression_p (expr))
10370 type = cxx_make_type (DECLTYPE_TYPE);
10371 DECLTYPE_TYPE_EXPR (type) = expr;
10372 DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (type)
10373 = id_expression_or_member_access_p;
10374 SET_TYPE_STRUCTURAL_EQUALITY (type);
10376 return type;
10378 else if (processing_template_decl)
10380 expr = instantiate_non_dependent_expr_sfinae (expr, complain);
10381 if (expr == error_mark_node)
10382 return error_mark_node;
10385 /* The type denoted by decltype(e) is defined as follows: */
10387 expr = resolve_nondeduced_context (expr, complain);
10389 if (invalid_nonstatic_memfn_p (input_location, expr, complain))
10390 return error_mark_node;
10392 if (type_unknown_p (expr))
10394 if (complain & tf_error)
10395 error ("%<decltype%> cannot resolve address of overloaded function");
10396 return error_mark_node;
10399 /* To get the size of a static data member declared as an array of
10400 unknown bound, we need to instantiate it. */
10401 if (VAR_P (expr)
10402 && VAR_HAD_UNKNOWN_BOUND (expr)
10403 && DECL_TEMPLATE_INSTANTIATION (expr))
10404 instantiate_decl (expr, /*defer_ok*/true, /*expl_inst_mem*/false);
10406 if (id_expression_or_member_access_p)
10408 /* If e is an id-expression or a class member access (5.2.5
10409 [expr.ref]), decltype(e) is defined as the type of the entity
10410 named by e. If there is no such entity, or e names a set of
10411 overloaded functions, the program is ill-formed. */
10412 if (identifier_p (expr))
10413 expr = lookup_name (expr);
10415 if (INDIRECT_REF_P (expr)
10416 || TREE_CODE (expr) == VIEW_CONVERT_EXPR)
10417 /* This can happen when the expression is, e.g., "a.b". Just
10418 look at the underlying operand. */
10419 expr = TREE_OPERAND (expr, 0);
10421 if (TREE_CODE (expr) == OFFSET_REF
10422 || TREE_CODE (expr) == MEMBER_REF
10423 || TREE_CODE (expr) == SCOPE_REF)
10424 /* We're only interested in the field itself. If it is a
10425 BASELINK, we will need to see through it in the next
10426 step. */
10427 expr = TREE_OPERAND (expr, 1);
10429 if (BASELINK_P (expr))
10430 /* See through BASELINK nodes to the underlying function. */
10431 expr = BASELINK_FUNCTIONS (expr);
10433 /* decltype of a decomposition name drops references in the tuple case
10434 (unlike decltype of a normal variable) and keeps cv-qualifiers from
10435 the containing object in the other cases (unlike decltype of a member
10436 access expression). */
10437 if (DECL_DECOMPOSITION_P (expr))
10439 if (DECL_HAS_VALUE_EXPR_P (expr))
10440 /* Expr is an array or struct subobject proxy, handle
10441 bit-fields properly. */
10442 return unlowered_expr_type (expr);
10443 else
10444 /* Expr is a reference variable for the tuple case. */
10445 return lookup_decomp_type (expr);
10448 switch (TREE_CODE (expr))
10450 case FIELD_DECL:
10451 if (DECL_BIT_FIELD_TYPE (expr))
10453 type = DECL_BIT_FIELD_TYPE (expr);
10454 break;
10456 /* Fall through for fields that aren't bitfields. */
10457 gcc_fallthrough ();
10459 case FUNCTION_DECL:
10460 case VAR_DECL:
10461 case CONST_DECL:
10462 case PARM_DECL:
10463 case RESULT_DECL:
10464 case TEMPLATE_PARM_INDEX:
10465 expr = mark_type_use (expr);
10466 type = TREE_TYPE (expr);
10467 break;
10469 case ERROR_MARK:
10470 type = error_mark_node;
10471 break;
10473 case COMPONENT_REF:
10474 case COMPOUND_EXPR:
10475 mark_type_use (expr);
10476 type = is_bitfield_expr_with_lowered_type (expr);
10477 if (!type)
10478 type = TREE_TYPE (TREE_OPERAND (expr, 1));
10479 break;
10481 case BIT_FIELD_REF:
10482 gcc_unreachable ();
10484 case INTEGER_CST:
10485 case PTRMEM_CST:
10486 /* We can get here when the id-expression refers to an
10487 enumerator or non-type template parameter. */
10488 type = TREE_TYPE (expr);
10489 break;
10491 default:
10492 /* Handle instantiated template non-type arguments. */
10493 type = TREE_TYPE (expr);
10494 break;
10497 else
10499 /* Within a lambda-expression:
10501 Every occurrence of decltype((x)) where x is a possibly
10502 parenthesized id-expression that names an entity of
10503 automatic storage duration is treated as if x were
10504 transformed into an access to a corresponding data member
10505 of the closure type that would have been declared if x
10506 were a use of the denoted entity. */
10507 if (outer_automatic_var_p (expr)
10508 && current_function_decl
10509 && LAMBDA_FUNCTION_P (current_function_decl))
10510 type = capture_decltype (expr);
10511 else if (error_operand_p (expr))
10512 type = error_mark_node;
10513 else if (expr == current_class_ptr)
10514 /* If the expression is just "this", we want the
10515 cv-unqualified pointer for the "this" type. */
10516 type = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
10517 else
10519 /* Otherwise, where T is the type of e, if e is an lvalue,
10520 decltype(e) is defined as T&; if an xvalue, T&&; otherwise, T. */
10521 cp_lvalue_kind clk = lvalue_kind (expr);
10522 type = unlowered_expr_type (expr);
10523 gcc_assert (!TYPE_REF_P (type));
10525 /* For vector types, pick a non-opaque variant. */
10526 if (VECTOR_TYPE_P (type))
10527 type = strip_typedefs (type);
10529 if (clk != clk_none && !(clk & clk_class))
10530 type = cp_build_reference_type (type, (clk & clk_rvalueref));
10534 return type;
10537 /* Called from trait_expr_value to evaluate either __has_nothrow_assign or
10538 __has_nothrow_copy, depending on assign_p. Returns true iff all
10539 the copy {ctor,assign} fns are nothrow. */
10541 static bool
10542 classtype_has_nothrow_assign_or_copy_p (tree type, bool assign_p)
10544 tree fns = NULL_TREE;
10546 if (assign_p || TYPE_HAS_COPY_CTOR (type))
10547 fns = get_class_binding (type, assign_p ? assign_op_identifier
10548 : ctor_identifier);
10550 bool saw_copy = false;
10551 for (ovl_iterator iter (fns); iter; ++iter)
10553 tree fn = *iter;
10555 if (copy_fn_p (fn) > 0)
10557 saw_copy = true;
10558 if (!maybe_instantiate_noexcept (fn)
10559 || !TYPE_NOTHROW_P (TREE_TYPE (fn)))
10560 return false;
10564 return saw_copy;
10567 /* Actually evaluates the trait. */
10569 static bool
10570 trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
10572 enum tree_code type_code1;
10573 tree t;
10575 type_code1 = TREE_CODE (type1);
10577 switch (kind)
10579 case CPTK_HAS_NOTHROW_ASSIGN:
10580 type1 = strip_array_types (type1);
10581 return (!CP_TYPE_CONST_P (type1) && type_code1 != REFERENCE_TYPE
10582 && (trait_expr_value (CPTK_HAS_TRIVIAL_ASSIGN, type1, type2)
10583 || (CLASS_TYPE_P (type1)
10584 && classtype_has_nothrow_assign_or_copy_p (type1,
10585 true))));
10587 case CPTK_HAS_TRIVIAL_ASSIGN:
10588 /* ??? The standard seems to be missing the "or array of such a class
10589 type" wording for this trait. */
10590 type1 = strip_array_types (type1);
10591 return (!CP_TYPE_CONST_P (type1) && type_code1 != REFERENCE_TYPE
10592 && (trivial_type_p (type1)
10593 || (CLASS_TYPE_P (type1)
10594 && TYPE_HAS_TRIVIAL_COPY_ASSIGN (type1))));
10596 case CPTK_HAS_NOTHROW_CONSTRUCTOR:
10597 type1 = strip_array_types (type1);
10598 return (trait_expr_value (CPTK_HAS_TRIVIAL_CONSTRUCTOR, type1, type2)
10599 || (CLASS_TYPE_P (type1)
10600 && (t = locate_ctor (type1))
10601 && maybe_instantiate_noexcept (t)
10602 && TYPE_NOTHROW_P (TREE_TYPE (t))));
10604 case CPTK_HAS_TRIVIAL_CONSTRUCTOR:
10605 type1 = strip_array_types (type1);
10606 return (trivial_type_p (type1)
10607 || (CLASS_TYPE_P (type1) && TYPE_HAS_TRIVIAL_DFLT (type1)));
10609 case CPTK_HAS_NOTHROW_COPY:
10610 type1 = strip_array_types (type1);
10611 return (trait_expr_value (CPTK_HAS_TRIVIAL_COPY, type1, type2)
10612 || (CLASS_TYPE_P (type1)
10613 && classtype_has_nothrow_assign_or_copy_p (type1, false)));
10615 case CPTK_HAS_TRIVIAL_COPY:
10616 /* ??? The standard seems to be missing the "or array of such a class
10617 type" wording for this trait. */
10618 type1 = strip_array_types (type1);
10619 return (trivial_type_p (type1) || type_code1 == REFERENCE_TYPE
10620 || (CLASS_TYPE_P (type1) && TYPE_HAS_TRIVIAL_COPY_CTOR (type1)));
10622 case CPTK_HAS_TRIVIAL_DESTRUCTOR:
10623 type1 = strip_array_types (type1);
10624 return (trivial_type_p (type1) || type_code1 == REFERENCE_TYPE
10625 || (CLASS_TYPE_P (type1)
10626 && TYPE_HAS_TRIVIAL_DESTRUCTOR (type1)));
10628 case CPTK_HAS_VIRTUAL_DESTRUCTOR:
10629 return type_has_virtual_destructor (type1);
10631 case CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS:
10632 return type_has_unique_obj_representations (type1);
10634 case CPTK_IS_ABSTRACT:
10635 return ABSTRACT_CLASS_TYPE_P (type1);
10637 case CPTK_IS_AGGREGATE:
10638 return CP_AGGREGATE_TYPE_P (type1);
10640 case CPTK_IS_BASE_OF:
10641 return (NON_UNION_CLASS_TYPE_P (type1) && NON_UNION_CLASS_TYPE_P (type2)
10642 && (same_type_ignoring_top_level_qualifiers_p (type1, type2)
10643 || DERIVED_FROM_P (type1, type2)));
10645 case CPTK_IS_CLASS:
10646 return NON_UNION_CLASS_TYPE_P (type1);
10648 case CPTK_IS_EMPTY:
10649 return NON_UNION_CLASS_TYPE_P (type1) && CLASSTYPE_EMPTY_P (type1);
10651 case CPTK_IS_ENUM:
10652 return type_code1 == ENUMERAL_TYPE;
10654 case CPTK_IS_FINAL:
10655 return CLASS_TYPE_P (type1) && CLASSTYPE_FINAL (type1);
10657 case CPTK_IS_LITERAL_TYPE:
10658 return literal_type_p (type1);
10660 case CPTK_IS_POD:
10661 return pod_type_p (type1);
10663 case CPTK_IS_POLYMORPHIC:
10664 return CLASS_TYPE_P (type1) && TYPE_POLYMORPHIC_P (type1);
10666 case CPTK_IS_SAME_AS:
10667 return same_type_p (type1, type2);
10669 case CPTK_IS_STD_LAYOUT:
10670 return std_layout_type_p (type1);
10672 case CPTK_IS_TRIVIAL:
10673 return trivial_type_p (type1);
10675 case CPTK_IS_TRIVIALLY_ASSIGNABLE:
10676 return is_trivially_xible (MODIFY_EXPR, type1, type2);
10678 case CPTK_IS_TRIVIALLY_CONSTRUCTIBLE:
10679 return is_trivially_xible (INIT_EXPR, type1, type2);
10681 case CPTK_IS_TRIVIALLY_COPYABLE:
10682 return trivially_copyable_p (type1);
10684 case CPTK_IS_UNION:
10685 return type_code1 == UNION_TYPE;
10687 case CPTK_IS_ASSIGNABLE:
10688 return is_xible (MODIFY_EXPR, type1, type2);
10690 case CPTK_IS_CONSTRUCTIBLE:
10691 return is_xible (INIT_EXPR, type1, type2);
10693 case CPTK_IS_NOTHROW_ASSIGNABLE:
10694 return is_nothrow_xible (MODIFY_EXPR, type1, type2);
10696 case CPTK_IS_NOTHROW_CONSTRUCTIBLE:
10697 return is_nothrow_xible (INIT_EXPR, type1, type2);
10699 default:
10700 gcc_unreachable ();
10701 return false;
10705 /* If TYPE is an array of unknown bound, or (possibly cv-qualified)
10706 void, or a complete type, returns true, otherwise false. */
10708 static bool
10709 check_trait_type (tree type)
10711 if (type == NULL_TREE)
10712 return true;
10714 if (TREE_CODE (type) == TREE_LIST)
10715 return (check_trait_type (TREE_VALUE (type))
10716 && check_trait_type (TREE_CHAIN (type)));
10718 if (TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type)
10719 && COMPLETE_TYPE_P (TREE_TYPE (type)))
10720 return true;
10722 if (VOID_TYPE_P (type))
10723 return true;
10725 return !!complete_type_or_else (strip_array_types (type), NULL_TREE);
10728 /* Process a trait expression. */
10730 tree
10731 finish_trait_expr (location_t loc, cp_trait_kind kind, tree type1, tree type2)
10733 if (type1 == error_mark_node
10734 || type2 == error_mark_node)
10735 return error_mark_node;
10737 if (processing_template_decl)
10739 tree trait_expr = make_node (TRAIT_EXPR);
10740 TREE_TYPE (trait_expr) = boolean_type_node;
10741 TRAIT_EXPR_TYPE1 (trait_expr) = type1;
10742 TRAIT_EXPR_TYPE2 (trait_expr) = type2;
10743 TRAIT_EXPR_KIND (trait_expr) = kind;
10744 TRAIT_EXPR_LOCATION (trait_expr) = loc;
10745 return trait_expr;
10748 switch (kind)
10750 case CPTK_HAS_NOTHROW_ASSIGN:
10751 case CPTK_HAS_TRIVIAL_ASSIGN:
10752 case CPTK_HAS_NOTHROW_CONSTRUCTOR:
10753 case CPTK_HAS_TRIVIAL_CONSTRUCTOR:
10754 case CPTK_HAS_NOTHROW_COPY:
10755 case CPTK_HAS_TRIVIAL_COPY:
10756 case CPTK_HAS_TRIVIAL_DESTRUCTOR:
10757 case CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS:
10758 case CPTK_HAS_VIRTUAL_DESTRUCTOR:
10759 case CPTK_IS_ABSTRACT:
10760 case CPTK_IS_AGGREGATE:
10761 case CPTK_IS_EMPTY:
10762 case CPTK_IS_FINAL:
10763 case CPTK_IS_LITERAL_TYPE:
10764 case CPTK_IS_POD:
10765 case CPTK_IS_POLYMORPHIC:
10766 case CPTK_IS_STD_LAYOUT:
10767 case CPTK_IS_TRIVIAL:
10768 case CPTK_IS_TRIVIALLY_COPYABLE:
10769 if (!check_trait_type (type1))
10770 return error_mark_node;
10771 break;
10773 case CPTK_IS_ASSIGNABLE:
10774 case CPTK_IS_CONSTRUCTIBLE:
10775 break;
10777 case CPTK_IS_TRIVIALLY_ASSIGNABLE:
10778 case CPTK_IS_TRIVIALLY_CONSTRUCTIBLE:
10779 case CPTK_IS_NOTHROW_ASSIGNABLE:
10780 case CPTK_IS_NOTHROW_CONSTRUCTIBLE:
10781 if (!check_trait_type (type1)
10782 || !check_trait_type (type2))
10783 return error_mark_node;
10784 break;
10786 case CPTK_IS_BASE_OF:
10787 if (NON_UNION_CLASS_TYPE_P (type1) && NON_UNION_CLASS_TYPE_P (type2)
10788 && !same_type_ignoring_top_level_qualifiers_p (type1, type2)
10789 && !complete_type_or_else (type2, NULL_TREE))
10790 /* We already issued an error. */
10791 return error_mark_node;
10792 break;
10794 case CPTK_IS_CLASS:
10795 case CPTK_IS_ENUM:
10796 case CPTK_IS_UNION:
10797 case CPTK_IS_SAME_AS:
10798 break;
10800 default:
10801 gcc_unreachable ();
10804 tree val = (trait_expr_value (kind, type1, type2)
10805 ? boolean_true_node : boolean_false_node);
10806 return maybe_wrap_with_location (val, loc);
10809 /* Do-nothing variants of functions to handle pragma FLOAT_CONST_DECIMAL64,
10810 which is ignored for C++. */
10812 void
10813 set_float_const_decimal64 (void)
10817 void
10818 clear_float_const_decimal64 (void)
10822 bool
10823 float_const_decimal64_p (void)
10825 return 0;
10829 /* Return true if T designates the implied `this' parameter. */
10831 bool
10832 is_this_parameter (tree t)
10834 if (!DECL_P (t) || DECL_NAME (t) != this_identifier)
10835 return false;
10836 gcc_assert (TREE_CODE (t) == PARM_DECL || is_capture_proxy (t)
10837 || (cp_binding_oracle && TREE_CODE (t) == VAR_DECL));
10838 return true;
10841 /* Insert the deduced return type for an auto function. */
10843 void
10844 apply_deduced_return_type (tree fco, tree return_type)
10846 tree result;
10848 if (return_type == error_mark_node)
10849 return;
10851 if (DECL_CONV_FN_P (fco))
10852 DECL_NAME (fco) = make_conv_op_name (return_type);
10854 TREE_TYPE (fco) = change_return_type (return_type, TREE_TYPE (fco));
10856 result = DECL_RESULT (fco);
10857 if (result == NULL_TREE)
10858 return;
10859 if (TREE_TYPE (result) == return_type)
10860 return;
10862 if (!processing_template_decl && !VOID_TYPE_P (return_type)
10863 && !complete_type_or_else (return_type, NULL_TREE))
10864 return;
10866 /* We already have a DECL_RESULT from start_preparsed_function.
10867 Now we need to redo the work it and allocate_struct_function
10868 did to reflect the new type. */
10869 gcc_assert (current_function_decl == fco);
10870 result = build_decl (input_location, RESULT_DECL, NULL_TREE,
10871 TYPE_MAIN_VARIANT (return_type));
10872 DECL_ARTIFICIAL (result) = 1;
10873 DECL_IGNORED_P (result) = 1;
10874 cp_apply_type_quals_to_decl (cp_type_quals (return_type),
10875 result);
10877 DECL_RESULT (fco) = result;
10879 if (!processing_template_decl)
10881 bool aggr = aggregate_value_p (result, fco);
10882 #ifdef PCC_STATIC_STRUCT_RETURN
10883 cfun->returns_pcc_struct = aggr;
10884 #endif
10885 cfun->returns_struct = aggr;
10889 /* DECL is a local variable or parameter from the surrounding scope of a
10890 lambda-expression. Returns the decltype for a use of the capture field
10891 for DECL even if it hasn't been captured yet. */
10893 static tree
10894 capture_decltype (tree decl)
10896 tree lam = CLASSTYPE_LAMBDA_EXPR (DECL_CONTEXT (current_function_decl));
10897 tree cap = lookup_name (DECL_NAME (decl), LOOK_where::BLOCK,
10898 LOOK_want::HIDDEN_LAMBDA);
10899 tree type;
10901 if (cap && is_capture_proxy (cap))
10902 type = TREE_TYPE (cap);
10903 else
10904 switch (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lam))
10906 case CPLD_NONE:
10907 error ("%qD is not captured", decl);
10908 return error_mark_node;
10910 case CPLD_COPY:
10911 type = TREE_TYPE (decl);
10912 if (TYPE_REF_P (type)
10913 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
10914 type = TREE_TYPE (type);
10915 break;
10917 case CPLD_REFERENCE:
10918 type = TREE_TYPE (decl);
10919 if (!TYPE_REF_P (type))
10920 type = build_reference_type (TREE_TYPE (decl));
10921 break;
10923 default:
10924 gcc_unreachable ();
10927 if (!TYPE_REF_P (type))
10929 if (!LAMBDA_EXPR_MUTABLE_P (lam))
10930 type = cp_build_qualified_type (type, (cp_type_quals (type)
10931 |TYPE_QUAL_CONST));
10932 type = build_reference_type (type);
10934 return type;
10937 /* Build a unary fold expression of EXPR over OP. If IS_RIGHT is true,
10938 this is a right unary fold. Otherwise it is a left unary fold. */
10940 static tree
10941 finish_unary_fold_expr (tree expr, int op, tree_code dir)
10943 /* Build a pack expansion (assuming expr has pack type). */
10944 if (!uses_parameter_packs (expr))
10946 error_at (location_of (expr), "operand of fold expression has no "
10947 "unexpanded parameter packs");
10948 return error_mark_node;
10950 tree pack = make_pack_expansion (expr);
10952 /* Build the fold expression. */
10953 tree code = build_int_cstu (integer_type_node, abs (op));
10954 tree fold = build_min_nt_loc (UNKNOWN_LOCATION, dir, code, pack);
10955 FOLD_EXPR_MODIFY_P (fold) = (op < 0);
10956 return fold;
10959 tree
10960 finish_left_unary_fold_expr (tree expr, int op)
10962 return finish_unary_fold_expr (expr, op, UNARY_LEFT_FOLD_EXPR);
10965 tree
10966 finish_right_unary_fold_expr (tree expr, int op)
10968 return finish_unary_fold_expr (expr, op, UNARY_RIGHT_FOLD_EXPR);
10971 /* Build a binary fold expression over EXPR1 and EXPR2. The
10972 associativity of the fold is determined by EXPR1 and EXPR2 (whichever
10973 has an unexpanded parameter pack). */
10975 tree
10976 finish_binary_fold_expr (tree pack, tree init, int op, tree_code dir)
10978 pack = make_pack_expansion (pack);
10979 tree code = build_int_cstu (integer_type_node, abs (op));
10980 tree fold = build_min_nt_loc (UNKNOWN_LOCATION, dir, code, pack, init);
10981 FOLD_EXPR_MODIFY_P (fold) = (op < 0);
10982 return fold;
10985 tree
10986 finish_binary_fold_expr (tree expr1, tree expr2, int op)
10988 // Determine which expr has an unexpanded parameter pack and
10989 // set the pack and initial term.
10990 bool pack1 = uses_parameter_packs (expr1);
10991 bool pack2 = uses_parameter_packs (expr2);
10992 if (pack1 && !pack2)
10993 return finish_binary_fold_expr (expr1, expr2, op, BINARY_RIGHT_FOLD_EXPR);
10994 else if (pack2 && !pack1)
10995 return finish_binary_fold_expr (expr2, expr1, op, BINARY_LEFT_FOLD_EXPR);
10996 else
10998 if (pack1)
10999 error ("both arguments in binary fold have unexpanded parameter packs");
11000 else
11001 error ("no unexpanded parameter packs in binary fold");
11003 return error_mark_node;
11006 /* Finish __builtin_launder (arg). */
11008 tree
11009 finish_builtin_launder (location_t loc, tree arg, tsubst_flags_t complain)
11011 tree orig_arg = arg;
11012 if (!type_dependent_expression_p (arg))
11013 arg = decay_conversion (arg, complain);
11014 if (error_operand_p (arg))
11015 return error_mark_node;
11016 if (!type_dependent_expression_p (arg)
11017 && !TYPE_PTR_P (TREE_TYPE (arg)))
11019 error_at (loc, "non-pointer argument to %<__builtin_launder%>");
11020 return error_mark_node;
11022 if (processing_template_decl)
11023 arg = orig_arg;
11024 return build_call_expr_internal_loc (loc, IFN_LAUNDER,
11025 TREE_TYPE (arg), 1, arg);
11028 /* Finish __builtin_convertvector (arg, type). */
11030 tree
11031 cp_build_vec_convert (tree arg, location_t loc, tree type,
11032 tsubst_flags_t complain)
11034 if (error_operand_p (type))
11035 return error_mark_node;
11036 if (error_operand_p (arg))
11037 return error_mark_node;
11039 tree ret = NULL_TREE;
11040 if (!type_dependent_expression_p (arg) && !dependent_type_p (type))
11041 ret = c_build_vec_convert (cp_expr_loc_or_input_loc (arg),
11042 decay_conversion (arg, complain),
11043 loc, type, (complain & tf_error) != 0);
11045 if (!processing_template_decl)
11046 return ret;
11048 return build_call_expr_internal_loc (loc, IFN_VEC_CONVERT, type, 1, arg);
11051 /* Finish __builtin_bit_cast (type, arg). */
11053 tree
11054 cp_build_bit_cast (location_t loc, tree type, tree arg,
11055 tsubst_flags_t complain)
11057 if (error_operand_p (type))
11058 return error_mark_node;
11059 if (!dependent_type_p (type))
11061 if (!complete_type_or_maybe_complain (type, NULL_TREE, complain))
11062 return error_mark_node;
11063 if (TREE_CODE (type) == ARRAY_TYPE)
11065 /* std::bit_cast for destination ARRAY_TYPE is not possible,
11066 as functions may not return an array, so don't bother trying
11067 to support this (and then deal with VLAs etc.). */
11068 error_at (loc, "%<__builtin_bit_cast%> destination type %qT "
11069 "is an array type", type);
11070 return error_mark_node;
11072 if (!trivially_copyable_p (type))
11074 error_at (loc, "%<__builtin_bit_cast%> destination type %qT "
11075 "is not trivially copyable", type);
11076 return error_mark_node;
11080 if (error_operand_p (arg))
11081 return error_mark_node;
11083 if (!type_dependent_expression_p (arg))
11085 if (TREE_CODE (TREE_TYPE (arg)) == ARRAY_TYPE)
11087 /* Don't perform array-to-pointer conversion. */
11088 arg = mark_rvalue_use (arg, loc, true);
11089 if (!complete_type_or_maybe_complain (TREE_TYPE (arg), arg, complain))
11090 return error_mark_node;
11092 else
11093 arg = decay_conversion (arg, complain);
11095 if (error_operand_p (arg))
11096 return error_mark_node;
11098 if (!trivially_copyable_p (TREE_TYPE (arg)))
11100 error_at (cp_expr_loc_or_loc (arg, loc),
11101 "%<__builtin_bit_cast%> source type %qT "
11102 "is not trivially copyable", TREE_TYPE (arg));
11103 return error_mark_node;
11105 if (!dependent_type_p (type)
11106 && !cp_tree_equal (TYPE_SIZE_UNIT (type),
11107 TYPE_SIZE_UNIT (TREE_TYPE (arg))))
11109 error_at (loc, "%<__builtin_bit_cast%> source size %qE "
11110 "not equal to destination type size %qE",
11111 TYPE_SIZE_UNIT (TREE_TYPE (arg)),
11112 TYPE_SIZE_UNIT (type));
11113 return error_mark_node;
11117 tree ret = build_min (BIT_CAST_EXPR, type, arg);
11118 SET_EXPR_LOCATION (ret, loc);
11120 if (!processing_template_decl && CLASS_TYPE_P (type))
11121 ret = get_target_expr_sfinae (ret, complain);
11123 return ret;
11126 #include "gt-cp-semantics.h"