* semantics.c (finish_template_type): Check CLASSTYPE_TEMPLATE_INFO.
[official-gcc.git] / gcc / cp / semantics.c
blob0a695008ada950e60a2d298146165e70df33ae4b
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-2017 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 "gomp-constants.h"
45 /* There routines provide a modular interface to perform many parsing
46 operations. They may therefore be used during actual parsing, or
47 during template instantiation, which may be regarded as a
48 degenerate form of parsing. */
50 static tree maybe_convert_cond (tree);
51 static tree finalize_nrv_r (tree *, int *, void *);
52 static tree capture_decltype (tree);
54 /* Used for OpenMP non-static data member privatization. */
56 static hash_map<tree, tree> *omp_private_member_map;
57 static vec<tree> omp_private_member_vec;
58 static bool omp_private_member_ignore_next;
61 /* Deferred Access Checking Overview
62 ---------------------------------
64 Most C++ expressions and declarations require access checking
65 to be performed during parsing. However, in several cases,
66 this has to be treated differently.
68 For member declarations, access checking has to be deferred
69 until more information about the declaration is known. For
70 example:
72 class A {
73 typedef int X;
74 public:
75 X f();
78 A::X A::f();
79 A::X g();
81 When we are parsing the function return type `A::X', we don't
82 really know if this is allowed until we parse the function name.
84 Furthermore, some contexts require that access checking is
85 never performed at all. These include class heads, and template
86 instantiations.
88 Typical use of access checking functions is described here:
90 1. When we enter a context that requires certain access checking
91 mode, the function `push_deferring_access_checks' is called with
92 DEFERRING argument specifying the desired mode. Access checking
93 may be performed immediately (dk_no_deferred), deferred
94 (dk_deferred), or not performed (dk_no_check).
96 2. When a declaration such as a type, or a variable, is encountered,
97 the function `perform_or_defer_access_check' is called. It
98 maintains a vector of all deferred checks.
100 3. The global `current_class_type' or `current_function_decl' is then
101 setup by the parser. `enforce_access' relies on these information
102 to check access.
104 4. Upon exiting the context mentioned in step 1,
105 `perform_deferred_access_checks' is called to check all declaration
106 stored in the vector. `pop_deferring_access_checks' is then
107 called to restore the previous access checking mode.
109 In case of parsing error, we simply call `pop_deferring_access_checks'
110 without `perform_deferred_access_checks'. */
112 struct GTY(()) deferred_access {
113 /* A vector representing name-lookups for which we have deferred
114 checking access controls. We cannot check the accessibility of
115 names used in a decl-specifier-seq until we know what is being
116 declared because code like:
118 class A {
119 class B {};
120 B* f();
123 A::B* A::f() { return 0; }
125 is valid, even though `A::B' is not generally accessible. */
126 vec<deferred_access_check, va_gc> * GTY(()) deferred_access_checks;
128 /* The current mode of access checks. */
129 enum deferring_kind deferring_access_checks_kind;
133 /* Data for deferred access checking. */
134 static GTY(()) vec<deferred_access, va_gc> *deferred_access_stack;
135 static GTY(()) unsigned deferred_access_no_check;
137 /* Save the current deferred access states and start deferred
138 access checking iff DEFER_P is true. */
140 void
141 push_deferring_access_checks (deferring_kind deferring)
143 /* For context like template instantiation, access checking
144 disabling applies to all nested context. */
145 if (deferred_access_no_check || deferring == dk_no_check)
146 deferred_access_no_check++;
147 else
149 deferred_access e = {NULL, deferring};
150 vec_safe_push (deferred_access_stack, e);
154 /* Save the current deferred access states and start deferred access
155 checking, continuing the set of deferred checks in CHECKS. */
157 void
158 reopen_deferring_access_checks (vec<deferred_access_check, va_gc> * checks)
160 push_deferring_access_checks (dk_deferred);
161 if (!deferred_access_no_check)
162 deferred_access_stack->last().deferred_access_checks = checks;
165 /* Resume deferring access checks again after we stopped doing
166 this previously. */
168 void
169 resume_deferring_access_checks (void)
171 if (!deferred_access_no_check)
172 deferred_access_stack->last().deferring_access_checks_kind = dk_deferred;
175 /* Stop deferring access checks. */
177 void
178 stop_deferring_access_checks (void)
180 if (!deferred_access_no_check)
181 deferred_access_stack->last().deferring_access_checks_kind = dk_no_deferred;
184 /* Discard the current deferred access checks and restore the
185 previous states. */
187 void
188 pop_deferring_access_checks (void)
190 if (deferred_access_no_check)
191 deferred_access_no_check--;
192 else
193 deferred_access_stack->pop ();
196 /* Returns a TREE_LIST representing the deferred checks.
197 The TREE_PURPOSE of each node is the type through which the
198 access occurred; the TREE_VALUE is the declaration named.
201 vec<deferred_access_check, va_gc> *
202 get_deferred_access_checks (void)
204 if (deferred_access_no_check)
205 return NULL;
206 else
207 return (deferred_access_stack->last().deferred_access_checks);
210 /* Take current deferred checks and combine with the
211 previous states if we also defer checks previously.
212 Otherwise perform checks now. */
214 void
215 pop_to_parent_deferring_access_checks (void)
217 if (deferred_access_no_check)
218 deferred_access_no_check--;
219 else
221 vec<deferred_access_check, va_gc> *checks;
222 deferred_access *ptr;
224 checks = (deferred_access_stack->last ().deferred_access_checks);
226 deferred_access_stack->pop ();
227 ptr = &deferred_access_stack->last ();
228 if (ptr->deferring_access_checks_kind == dk_no_deferred)
230 /* Check access. */
231 perform_access_checks (checks, tf_warning_or_error);
233 else
235 /* Merge with parent. */
236 int i, j;
237 deferred_access_check *chk, *probe;
239 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
241 FOR_EACH_VEC_SAFE_ELT (ptr->deferred_access_checks, j, probe)
243 if (probe->binfo == chk->binfo &&
244 probe->decl == chk->decl &&
245 probe->diag_decl == chk->diag_decl)
246 goto found;
248 /* Insert into parent's checks. */
249 vec_safe_push (ptr->deferred_access_checks, *chk);
250 found:;
256 /* Perform the access checks in CHECKS. The TREE_PURPOSE of each node
257 is the BINFO indicating the qualifying scope used to access the
258 DECL node stored in the TREE_VALUE of the node. If CHECKS is empty
259 or we aren't in SFINAE context or all the checks succeed return TRUE,
260 otherwise FALSE. */
262 bool
263 perform_access_checks (vec<deferred_access_check, va_gc> *checks,
264 tsubst_flags_t complain)
266 int i;
267 deferred_access_check *chk;
268 location_t loc = input_location;
269 bool ok = true;
271 if (!checks)
272 return true;
274 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
276 input_location = chk->loc;
277 ok &= enforce_access (chk->binfo, chk->decl, chk->diag_decl, complain);
280 input_location = loc;
281 return (complain & tf_error) ? true : ok;
284 /* Perform the deferred access checks.
286 After performing the checks, we still have to keep the list
287 `deferred_access_stack->deferred_access_checks' since we may want
288 to check access for them again later in a different context.
289 For example:
291 class A {
292 typedef int X;
293 static X a;
295 A::X A::a, x; // No error for `A::a', error for `x'
297 We have to perform deferred access of `A::X', first with `A::a',
298 next with `x'. Return value like perform_access_checks above. */
300 bool
301 perform_deferred_access_checks (tsubst_flags_t complain)
303 return perform_access_checks (get_deferred_access_checks (), complain);
306 /* Defer checking the accessibility of DECL, when looked up in
307 BINFO. DIAG_DECL is the declaration to use to print diagnostics.
308 Return value like perform_access_checks above. */
310 bool
311 perform_or_defer_access_check (tree binfo, tree decl, tree diag_decl,
312 tsubst_flags_t complain)
314 int i;
315 deferred_access *ptr;
316 deferred_access_check *chk;
319 /* Exit if we are in a context that no access checking is performed.
321 if (deferred_access_no_check)
322 return true;
324 gcc_assert (TREE_CODE (binfo) == TREE_BINFO);
326 ptr = &deferred_access_stack->last ();
328 /* If we are not supposed to defer access checks, just check now. */
329 if (ptr->deferring_access_checks_kind == dk_no_deferred)
331 bool ok = enforce_access (binfo, decl, diag_decl, complain);
332 return (complain & tf_error) ? true : ok;
335 /* See if we are already going to perform this check. */
336 FOR_EACH_VEC_SAFE_ELT (ptr->deferred_access_checks, i, chk)
338 if (chk->decl == decl && chk->binfo == binfo &&
339 chk->diag_decl == diag_decl)
341 return true;
344 /* If not, record the check. */
345 deferred_access_check new_access = {binfo, decl, diag_decl, input_location};
346 vec_safe_push (ptr->deferred_access_checks, new_access);
348 return true;
351 /* Returns nonzero if the current statement is a full expression,
352 i.e. temporaries created during that statement should be destroyed
353 at the end of the statement. */
356 stmts_are_full_exprs_p (void)
358 return current_stmt_tree ()->stmts_are_full_exprs_p;
361 /* T is a statement. Add it to the statement-tree. This is the C++
362 version. The C/ObjC frontends have a slightly different version of
363 this function. */
365 tree
366 add_stmt (tree t)
368 enum tree_code code = TREE_CODE (t);
370 if (EXPR_P (t) && code != LABEL_EXPR)
372 if (!EXPR_HAS_LOCATION (t))
373 SET_EXPR_LOCATION (t, input_location);
375 /* When we expand a statement-tree, we must know whether or not the
376 statements are full-expressions. We record that fact here. */
377 STMT_IS_FULL_EXPR_P (t) = stmts_are_full_exprs_p ();
380 if (code == LABEL_EXPR || code == CASE_LABEL_EXPR)
381 STATEMENT_LIST_HAS_LABEL (cur_stmt_list) = 1;
383 /* Add T to the statement-tree. Non-side-effect statements need to be
384 recorded during statement expressions. */
385 gcc_checking_assert (!stmt_list_stack->is_empty ());
386 append_to_statement_list_force (t, &cur_stmt_list);
388 return t;
391 /* Returns the stmt_tree to which statements are currently being added. */
393 stmt_tree
394 current_stmt_tree (void)
396 return (cfun
397 ? &cfun->language->base.x_stmt_tree
398 : &scope_chain->x_stmt_tree);
401 /* If statements are full expressions, wrap STMT in a CLEANUP_POINT_EXPR. */
403 static tree
404 maybe_cleanup_point_expr (tree expr)
406 if (!processing_template_decl && stmts_are_full_exprs_p ())
407 expr = fold_build_cleanup_point_expr (TREE_TYPE (expr), expr);
408 return expr;
411 /* Like maybe_cleanup_point_expr except have the type of the new expression be
412 void so we don't need to create a temporary variable to hold the inner
413 expression. The reason why we do this is because the original type might be
414 an aggregate and we cannot create a temporary variable for that type. */
416 tree
417 maybe_cleanup_point_expr_void (tree expr)
419 if (!processing_template_decl && stmts_are_full_exprs_p ())
420 expr = fold_build_cleanup_point_expr (void_type_node, expr);
421 return expr;
426 /* Create a declaration statement for the declaration given by the DECL. */
428 void
429 add_decl_expr (tree decl)
431 tree r = build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl);
432 if (DECL_INITIAL (decl)
433 || (DECL_SIZE (decl) && TREE_SIDE_EFFECTS (DECL_SIZE (decl))))
434 r = maybe_cleanup_point_expr_void (r);
435 add_stmt (r);
438 /* Finish a scope. */
440 tree
441 do_poplevel (tree stmt_list)
443 tree block = NULL;
445 if (stmts_are_full_exprs_p ())
446 block = poplevel (kept_level_p (), 1, 0);
448 stmt_list = pop_stmt_list (stmt_list);
450 if (!processing_template_decl)
452 stmt_list = c_build_bind_expr (input_location, block, stmt_list);
453 /* ??? See c_end_compound_stmt re statement expressions. */
456 return stmt_list;
459 /* Begin a new scope. */
461 static tree
462 do_pushlevel (scope_kind sk)
464 tree ret = push_stmt_list ();
465 if (stmts_are_full_exprs_p ())
466 begin_scope (sk, NULL);
467 return ret;
470 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
471 when the current scope is exited. EH_ONLY is true when this is not
472 meant to apply to normal control flow transfer. */
474 void
475 push_cleanup (tree decl, tree cleanup, bool eh_only)
477 tree stmt = build_stmt (input_location, CLEANUP_STMT, NULL, cleanup, decl);
478 CLEANUP_EH_ONLY (stmt) = eh_only;
479 add_stmt (stmt);
480 CLEANUP_BODY (stmt) = push_stmt_list ();
483 /* Simple infinite loop tracking for -Wreturn-type. We keep a stack of all
484 the current loops, represented by 'NULL_TREE' if we've seen a possible
485 exit, and 'error_mark_node' if not. This is currently used only to
486 suppress the warning about a function with no return statements, and
487 therefore we don't bother noting returns as possible exits. We also
488 don't bother with gotos. */
490 static void
491 begin_maybe_infinite_loop (tree cond)
493 /* Only track this while parsing a function, not during instantiation. */
494 if (!cfun || (DECL_TEMPLATE_INSTANTIATION (current_function_decl)
495 && !processing_template_decl))
496 return;
497 bool maybe_infinite = true;
498 if (cond)
500 cond = fold_non_dependent_expr (cond);
501 maybe_infinite = integer_nonzerop (cond);
503 vec_safe_push (cp_function_chain->infinite_loops,
504 maybe_infinite ? error_mark_node : NULL_TREE);
508 /* A break is a possible exit for the current loop. */
510 void
511 break_maybe_infinite_loop (void)
513 if (!cfun)
514 return;
515 cp_function_chain->infinite_loops->last() = NULL_TREE;
518 /* If we reach the end of the loop without seeing a possible exit, we have
519 an infinite loop. */
521 static void
522 end_maybe_infinite_loop (tree cond)
524 if (!cfun || (DECL_TEMPLATE_INSTANTIATION (current_function_decl)
525 && !processing_template_decl))
526 return;
527 tree current = cp_function_chain->infinite_loops->pop();
528 if (current != NULL_TREE)
530 cond = fold_non_dependent_expr (cond);
531 if (integer_nonzerop (cond))
532 current_function_infinite_loop = 1;
537 /* Begin a conditional that might contain a declaration. When generating
538 normal code, we want the declaration to appear before the statement
539 containing the conditional. When generating template code, we want the
540 conditional to be rendered as the raw DECL_EXPR. */
542 static void
543 begin_cond (tree *cond_p)
545 if (processing_template_decl)
546 *cond_p = push_stmt_list ();
549 /* Finish such a conditional. */
551 static void
552 finish_cond (tree *cond_p, tree expr)
554 if (processing_template_decl)
556 tree cond = pop_stmt_list (*cond_p);
558 if (expr == NULL_TREE)
559 /* Empty condition in 'for'. */
560 gcc_assert (empty_expr_stmt_p (cond));
561 else if (check_for_bare_parameter_packs (expr))
562 expr = error_mark_node;
563 else if (!empty_expr_stmt_p (cond))
564 expr = build2 (COMPOUND_EXPR, TREE_TYPE (expr), cond, expr);
566 *cond_p = expr;
569 /* If *COND_P specifies a conditional with a declaration, transform the
570 loop such that
571 while (A x = 42) { }
572 for (; A x = 42;) { }
573 becomes
574 while (true) { A x = 42; if (!x) break; }
575 for (;;) { A x = 42; if (!x) break; }
576 The statement list for BODY will be empty if the conditional did
577 not declare anything. */
579 static void
580 simplify_loop_decl_cond (tree *cond_p, tree body)
582 tree cond, if_stmt;
584 if (!TREE_SIDE_EFFECTS (body))
585 return;
587 cond = *cond_p;
588 *cond_p = boolean_true_node;
590 if_stmt = begin_if_stmt ();
591 cond = cp_build_unary_op (TRUTH_NOT_EXPR, cond, false, tf_warning_or_error);
592 finish_if_stmt_cond (cond, if_stmt);
593 finish_break_stmt ();
594 finish_then_clause (if_stmt);
595 finish_if_stmt (if_stmt);
598 /* Finish a goto-statement. */
600 tree
601 finish_goto_stmt (tree destination)
603 if (identifier_p (destination))
604 destination = lookup_label (destination);
606 /* We warn about unused labels with -Wunused. That means we have to
607 mark the used labels as used. */
608 if (TREE_CODE (destination) == LABEL_DECL)
609 TREE_USED (destination) = 1;
610 else
612 if (check_no_cilk (destination,
613 "Cilk array notation cannot be used as a computed goto expression",
614 "%<_Cilk_spawn%> statement cannot be used as a computed goto expression"))
615 destination = error_mark_node;
616 destination = mark_rvalue_use (destination);
617 if (!processing_template_decl)
619 destination = cp_convert (ptr_type_node, destination,
620 tf_warning_or_error);
621 if (error_operand_p (destination))
622 return NULL_TREE;
623 destination
624 = fold_build_cleanup_point_expr (TREE_TYPE (destination),
625 destination);
629 check_goto (destination);
631 return add_stmt (build_stmt (input_location, GOTO_EXPR, destination));
634 /* COND is the condition-expression for an if, while, etc.,
635 statement. Convert it to a boolean value, if appropriate.
636 In addition, verify sequence points if -Wsequence-point is enabled. */
638 static tree
639 maybe_convert_cond (tree cond)
641 /* Empty conditions remain empty. */
642 if (!cond)
643 return NULL_TREE;
645 /* Wait until we instantiate templates before doing conversion. */
646 if (processing_template_decl)
647 return cond;
649 if (warn_sequence_point)
650 verify_sequence_points (cond);
652 /* Do the conversion. */
653 cond = convert_from_reference (cond);
655 if (TREE_CODE (cond) == MODIFY_EXPR
656 && !TREE_NO_WARNING (cond)
657 && warn_parentheses)
659 warning_at (EXPR_LOC_OR_LOC (cond, input_location), OPT_Wparentheses,
660 "suggest parentheses around assignment used as truth value");
661 TREE_NO_WARNING (cond) = 1;
664 return condition_conversion (cond);
667 /* Finish an expression-statement, whose EXPRESSION is as indicated. */
669 tree
670 finish_expr_stmt (tree expr)
672 tree r = NULL_TREE;
673 location_t loc = EXPR_LOCATION (expr);
675 if (expr != NULL_TREE)
677 /* If we ran into a problem, make sure we complained. */
678 gcc_assert (expr != error_mark_node || seen_error ());
680 if (!processing_template_decl)
682 if (warn_sequence_point)
683 verify_sequence_points (expr);
684 expr = convert_to_void (expr, ICV_STATEMENT, tf_warning_or_error);
686 else if (!type_dependent_expression_p (expr))
687 convert_to_void (build_non_dependent_expr (expr), ICV_STATEMENT,
688 tf_warning_or_error);
690 if (check_for_bare_parameter_packs (expr))
691 expr = error_mark_node;
693 /* Simplification of inner statement expressions, compound exprs,
694 etc can result in us already having an EXPR_STMT. */
695 if (TREE_CODE (expr) != CLEANUP_POINT_EXPR)
697 if (TREE_CODE (expr) != EXPR_STMT)
698 expr = build_stmt (loc, EXPR_STMT, expr);
699 expr = maybe_cleanup_point_expr_void (expr);
702 r = add_stmt (expr);
705 return r;
709 /* Begin an if-statement. Returns a newly created IF_STMT if
710 appropriate. */
712 tree
713 begin_if_stmt (void)
715 tree r, scope;
716 scope = do_pushlevel (sk_cond);
717 r = build_stmt (input_location, IF_STMT, NULL_TREE,
718 NULL_TREE, NULL_TREE, scope);
719 current_binding_level->this_entity = r;
720 begin_cond (&IF_COND (r));
721 return r;
724 /* Process the COND of an if-statement, which may be given by
725 IF_STMT. */
727 tree
728 finish_if_stmt_cond (tree cond, tree if_stmt)
730 cond = maybe_convert_cond (cond);
731 if (IF_STMT_CONSTEXPR_P (if_stmt)
732 && require_potential_rvalue_constant_expression (cond)
733 && !value_dependent_expression_p (cond))
734 cond = cxx_constant_value (cond, NULL_TREE);
735 finish_cond (&IF_COND (if_stmt), cond);
736 add_stmt (if_stmt);
737 THEN_CLAUSE (if_stmt) = push_stmt_list ();
738 return cond;
741 /* Finish the then-clause of an if-statement, which may be given by
742 IF_STMT. */
744 tree
745 finish_then_clause (tree if_stmt)
747 THEN_CLAUSE (if_stmt) = pop_stmt_list (THEN_CLAUSE (if_stmt));
748 return if_stmt;
751 /* Begin the else-clause of an if-statement. */
753 void
754 begin_else_clause (tree if_stmt)
756 ELSE_CLAUSE (if_stmt) = push_stmt_list ();
759 /* Finish the else-clause of an if-statement, which may be given by
760 IF_STMT. */
762 void
763 finish_else_clause (tree if_stmt)
765 ELSE_CLAUSE (if_stmt) = pop_stmt_list (ELSE_CLAUSE (if_stmt));
768 /* Finish an if-statement. */
770 void
771 finish_if_stmt (tree if_stmt)
773 tree scope = IF_SCOPE (if_stmt);
774 IF_SCOPE (if_stmt) = NULL;
775 add_stmt (do_poplevel (scope));
778 /* Begin a while-statement. Returns a newly created WHILE_STMT if
779 appropriate. */
781 tree
782 begin_while_stmt (void)
784 tree r;
785 r = build_stmt (input_location, WHILE_STMT, NULL_TREE, NULL_TREE);
786 add_stmt (r);
787 WHILE_BODY (r) = do_pushlevel (sk_block);
788 begin_cond (&WHILE_COND (r));
789 return r;
792 /* Process the COND of a while-statement, which may be given by
793 WHILE_STMT. */
795 void
796 finish_while_stmt_cond (tree cond, tree while_stmt, bool ivdep)
798 if (check_no_cilk (cond,
799 "Cilk array notation cannot be used as a condition for while statement",
800 "%<_Cilk_spawn%> statement cannot be used as a condition for while statement"))
801 cond = error_mark_node;
802 cond = maybe_convert_cond (cond);
803 finish_cond (&WHILE_COND (while_stmt), cond);
804 begin_maybe_infinite_loop (cond);
805 if (ivdep && cond != error_mark_node)
806 WHILE_COND (while_stmt) = build2 (ANNOTATE_EXPR,
807 TREE_TYPE (WHILE_COND (while_stmt)),
808 WHILE_COND (while_stmt),
809 build_int_cst (integer_type_node,
810 annot_expr_ivdep_kind));
811 simplify_loop_decl_cond (&WHILE_COND (while_stmt), WHILE_BODY (while_stmt));
814 /* Finish a while-statement, which may be given by WHILE_STMT. */
816 void
817 finish_while_stmt (tree while_stmt)
819 end_maybe_infinite_loop (boolean_true_node);
820 WHILE_BODY (while_stmt) = do_poplevel (WHILE_BODY (while_stmt));
823 /* Begin a do-statement. Returns a newly created DO_STMT if
824 appropriate. */
826 tree
827 begin_do_stmt (void)
829 tree r = build_stmt (input_location, DO_STMT, NULL_TREE, NULL_TREE);
830 begin_maybe_infinite_loop (boolean_true_node);
831 add_stmt (r);
832 DO_BODY (r) = push_stmt_list ();
833 return r;
836 /* Finish the body of a do-statement, which may be given by DO_STMT. */
838 void
839 finish_do_body (tree do_stmt)
841 tree body = DO_BODY (do_stmt) = pop_stmt_list (DO_BODY (do_stmt));
843 if (TREE_CODE (body) == STATEMENT_LIST && STATEMENT_LIST_TAIL (body))
844 body = STATEMENT_LIST_TAIL (body)->stmt;
846 if (IS_EMPTY_STMT (body))
847 warning (OPT_Wempty_body,
848 "suggest explicit braces around empty body in %<do%> statement");
851 /* Finish a do-statement, which may be given by DO_STMT, and whose
852 COND is as indicated. */
854 void
855 finish_do_stmt (tree cond, tree do_stmt, bool ivdep)
857 if (check_no_cilk (cond,
858 "Cilk array notation cannot be used as a condition for a do-while statement",
859 "%<_Cilk_spawn%> statement cannot be used as a condition for a do-while statement"))
860 cond = error_mark_node;
861 cond = maybe_convert_cond (cond);
862 end_maybe_infinite_loop (cond);
863 if (ivdep && cond != error_mark_node)
864 cond = build2 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
865 build_int_cst (integer_type_node, annot_expr_ivdep_kind));
866 DO_COND (do_stmt) = cond;
869 /* Finish a return-statement. The EXPRESSION returned, if any, is as
870 indicated. */
872 tree
873 finish_return_stmt (tree expr)
875 tree r;
876 bool no_warning;
878 expr = check_return_expr (expr, &no_warning);
880 if (error_operand_p (expr)
881 || (flag_openmp && !check_omp_return ()))
883 /* Suppress -Wreturn-type for this function. */
884 if (warn_return_type)
885 TREE_NO_WARNING (current_function_decl) = true;
886 return error_mark_node;
889 if (!processing_template_decl)
891 if (warn_sequence_point)
892 verify_sequence_points (expr);
894 if (DECL_DESTRUCTOR_P (current_function_decl)
895 || (DECL_CONSTRUCTOR_P (current_function_decl)
896 && targetm.cxx.cdtor_returns_this ()))
898 /* Similarly, all destructors must run destructors for
899 base-classes before returning. So, all returns in a
900 destructor get sent to the DTOR_LABEL; finish_function emits
901 code to return a value there. */
902 return finish_goto_stmt (cdtor_label);
906 r = build_stmt (input_location, RETURN_EXPR, expr);
907 TREE_NO_WARNING (r) |= no_warning;
908 r = maybe_cleanup_point_expr_void (r);
909 r = add_stmt (r);
911 return r;
914 /* Begin the scope of a for-statement or a range-for-statement.
915 Both the returned trees are to be used in a call to
916 begin_for_stmt or begin_range_for_stmt. */
918 tree
919 begin_for_scope (tree *init)
921 tree scope = NULL_TREE;
922 if (flag_new_for_scope > 0)
923 scope = do_pushlevel (sk_for);
925 if (processing_template_decl)
926 *init = push_stmt_list ();
927 else
928 *init = NULL_TREE;
930 return scope;
933 /* Begin a for-statement. Returns a new FOR_STMT.
934 SCOPE and INIT should be the return of begin_for_scope,
935 or both NULL_TREE */
937 tree
938 begin_for_stmt (tree scope, tree init)
940 tree r;
942 r = build_stmt (input_location, FOR_STMT, NULL_TREE, NULL_TREE,
943 NULL_TREE, NULL_TREE, NULL_TREE);
945 if (scope == NULL_TREE)
947 gcc_assert (!init || !(flag_new_for_scope > 0));
948 if (!init)
949 scope = begin_for_scope (&init);
951 FOR_INIT_STMT (r) = init;
952 FOR_SCOPE (r) = scope;
954 return r;
957 /* Finish the init-statement of a for-statement, which may be
958 given by FOR_STMT. */
960 void
961 finish_init_stmt (tree for_stmt)
963 if (processing_template_decl)
964 FOR_INIT_STMT (for_stmt) = pop_stmt_list (FOR_INIT_STMT (for_stmt));
965 add_stmt (for_stmt);
966 FOR_BODY (for_stmt) = do_pushlevel (sk_block);
967 begin_cond (&FOR_COND (for_stmt));
970 /* Finish the COND of a for-statement, which may be given by
971 FOR_STMT. */
973 void
974 finish_for_cond (tree cond, tree for_stmt, bool ivdep)
976 if (check_no_cilk (cond,
977 "Cilk array notation cannot be used in a condition for a for-loop",
978 "%<_Cilk_spawn%> statement cannot be used in a condition for a for-loop"))
979 cond = error_mark_node;
980 cond = maybe_convert_cond (cond);
981 finish_cond (&FOR_COND (for_stmt), cond);
982 begin_maybe_infinite_loop (cond);
983 if (ivdep && cond != error_mark_node)
984 FOR_COND (for_stmt) = build2 (ANNOTATE_EXPR,
985 TREE_TYPE (FOR_COND (for_stmt)),
986 FOR_COND (for_stmt),
987 build_int_cst (integer_type_node,
988 annot_expr_ivdep_kind));
989 simplify_loop_decl_cond (&FOR_COND (for_stmt), FOR_BODY (for_stmt));
992 /* Finish the increment-EXPRESSION in a for-statement, which may be
993 given by FOR_STMT. */
995 void
996 finish_for_expr (tree expr, tree for_stmt)
998 if (!expr)
999 return;
1000 /* If EXPR is an overloaded function, issue an error; there is no
1001 context available to use to perform overload resolution. */
1002 if (type_unknown_p (expr))
1004 cxx_incomplete_type_error (expr, TREE_TYPE (expr));
1005 expr = error_mark_node;
1007 if (!processing_template_decl)
1009 if (warn_sequence_point)
1010 verify_sequence_points (expr);
1011 expr = convert_to_void (expr, ICV_THIRD_IN_FOR,
1012 tf_warning_or_error);
1014 else if (!type_dependent_expression_p (expr))
1015 convert_to_void (build_non_dependent_expr (expr), ICV_THIRD_IN_FOR,
1016 tf_warning_or_error);
1017 expr = maybe_cleanup_point_expr_void (expr);
1018 if (check_for_bare_parameter_packs (expr))
1019 expr = error_mark_node;
1020 FOR_EXPR (for_stmt) = expr;
1023 /* Finish the body of a for-statement, which may be given by
1024 FOR_STMT. The increment-EXPR for the loop must be
1025 provided.
1026 It can also finish RANGE_FOR_STMT. */
1028 void
1029 finish_for_stmt (tree for_stmt)
1031 end_maybe_infinite_loop (boolean_true_node);
1033 if (TREE_CODE (for_stmt) == RANGE_FOR_STMT)
1034 RANGE_FOR_BODY (for_stmt) = do_poplevel (RANGE_FOR_BODY (for_stmt));
1035 else
1036 FOR_BODY (for_stmt) = do_poplevel (FOR_BODY (for_stmt));
1038 /* Pop the scope for the body of the loop. */
1039 if (flag_new_for_scope > 0)
1041 tree scope;
1042 tree *scope_ptr = (TREE_CODE (for_stmt) == RANGE_FOR_STMT
1043 ? &RANGE_FOR_SCOPE (for_stmt)
1044 : &FOR_SCOPE (for_stmt));
1045 scope = *scope_ptr;
1046 *scope_ptr = NULL;
1047 add_stmt (do_poplevel (scope));
1051 /* Begin a range-for-statement. Returns a new RANGE_FOR_STMT.
1052 SCOPE and INIT should be the return of begin_for_scope,
1053 or both NULL_TREE .
1054 To finish it call finish_for_stmt(). */
1056 tree
1057 begin_range_for_stmt (tree scope, tree init)
1059 tree r;
1061 begin_maybe_infinite_loop (boolean_false_node);
1063 r = build_stmt (input_location, RANGE_FOR_STMT,
1064 NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE);
1066 if (scope == NULL_TREE)
1068 gcc_assert (!init || !(flag_new_for_scope > 0));
1069 if (!init)
1070 scope = begin_for_scope (&init);
1073 /* RANGE_FOR_STMTs do not use nor save the init tree, so we
1074 pop it now. */
1075 if (init)
1076 pop_stmt_list (init);
1077 RANGE_FOR_SCOPE (r) = scope;
1079 return r;
1082 /* Finish the head of a range-based for statement, which may
1083 be given by RANGE_FOR_STMT. DECL must be the declaration
1084 and EXPR must be the loop expression. */
1086 void
1087 finish_range_for_decl (tree range_for_stmt, tree decl, tree expr)
1089 RANGE_FOR_DECL (range_for_stmt) = decl;
1090 RANGE_FOR_EXPR (range_for_stmt) = expr;
1091 add_stmt (range_for_stmt);
1092 RANGE_FOR_BODY (range_for_stmt) = do_pushlevel (sk_block);
1095 /* Finish a break-statement. */
1097 tree
1098 finish_break_stmt (void)
1100 /* In switch statements break is sometimes stylistically used after
1101 a return statement. This can lead to spurious warnings about
1102 control reaching the end of a non-void function when it is
1103 inlined. Note that we are calling block_may_fallthru with
1104 language specific tree nodes; this works because
1105 block_may_fallthru returns true when given something it does not
1106 understand. */
1107 if (!block_may_fallthru (cur_stmt_list))
1108 return void_node;
1109 return add_stmt (build_stmt (input_location, BREAK_STMT));
1112 /* Finish a continue-statement. */
1114 tree
1115 finish_continue_stmt (void)
1117 return add_stmt (build_stmt (input_location, CONTINUE_STMT));
1120 /* Begin a switch-statement. Returns a new SWITCH_STMT if
1121 appropriate. */
1123 tree
1124 begin_switch_stmt (void)
1126 tree r, scope;
1128 scope = do_pushlevel (sk_cond);
1129 r = build_stmt (input_location, SWITCH_STMT, NULL_TREE, NULL_TREE, NULL_TREE, scope);
1131 begin_cond (&SWITCH_STMT_COND (r));
1133 return r;
1136 /* Finish the cond of a switch-statement. */
1138 void
1139 finish_switch_cond (tree cond, tree switch_stmt)
1141 tree orig_type = NULL;
1143 if (check_no_cilk (cond,
1144 "Cilk array notation cannot be used as a condition for switch statement",
1145 "%<_Cilk_spawn%> statement cannot be used as a condition for switch statement"))
1146 cond = error_mark_node;
1148 if (!processing_template_decl)
1150 /* Convert the condition to an integer or enumeration type. */
1151 cond = build_expr_type_conversion (WANT_INT | WANT_ENUM, cond, true);
1152 if (cond == NULL_TREE)
1154 error ("switch quantity not an integer");
1155 cond = error_mark_node;
1157 /* We want unlowered type here to handle enum bit-fields. */
1158 orig_type = unlowered_expr_type (cond);
1159 if (TREE_CODE (orig_type) != ENUMERAL_TYPE)
1160 orig_type = TREE_TYPE (cond);
1161 if (cond != error_mark_node)
1163 /* [stmt.switch]
1165 Integral promotions are performed. */
1166 cond = perform_integral_promotions (cond);
1167 cond = maybe_cleanup_point_expr (cond);
1170 if (check_for_bare_parameter_packs (cond))
1171 cond = error_mark_node;
1172 else if (!processing_template_decl && warn_sequence_point)
1173 verify_sequence_points (cond);
1175 finish_cond (&SWITCH_STMT_COND (switch_stmt), cond);
1176 SWITCH_STMT_TYPE (switch_stmt) = orig_type;
1177 add_stmt (switch_stmt);
1178 push_switch (switch_stmt);
1179 SWITCH_STMT_BODY (switch_stmt) = push_stmt_list ();
1182 /* Finish the body of a switch-statement, which may be given by
1183 SWITCH_STMT. The COND to switch on is indicated. */
1185 void
1186 finish_switch_stmt (tree switch_stmt)
1188 tree scope;
1190 SWITCH_STMT_BODY (switch_stmt) =
1191 pop_stmt_list (SWITCH_STMT_BODY (switch_stmt));
1192 pop_switch ();
1194 scope = SWITCH_STMT_SCOPE (switch_stmt);
1195 SWITCH_STMT_SCOPE (switch_stmt) = NULL;
1196 add_stmt (do_poplevel (scope));
1199 /* Begin a try-block. Returns a newly-created TRY_BLOCK if
1200 appropriate. */
1202 tree
1203 begin_try_block (void)
1205 tree r = build_stmt (input_location, TRY_BLOCK, NULL_TREE, NULL_TREE);
1206 add_stmt (r);
1207 TRY_STMTS (r) = push_stmt_list ();
1208 return r;
1211 /* Likewise, for a function-try-block. The block returned in
1212 *COMPOUND_STMT is an artificial outer scope, containing the
1213 function-try-block. */
1215 tree
1216 begin_function_try_block (tree *compound_stmt)
1218 tree r;
1219 /* This outer scope does not exist in the C++ standard, but we need
1220 a place to put __FUNCTION__ and similar variables. */
1221 *compound_stmt = begin_compound_stmt (0);
1222 r = begin_try_block ();
1223 FN_TRY_BLOCK_P (r) = 1;
1224 return r;
1227 /* Finish a try-block, which may be given by TRY_BLOCK. */
1229 void
1230 finish_try_block (tree try_block)
1232 TRY_STMTS (try_block) = pop_stmt_list (TRY_STMTS (try_block));
1233 TRY_HANDLERS (try_block) = push_stmt_list ();
1236 /* Finish the body of a cleanup try-block, which may be given by
1237 TRY_BLOCK. */
1239 void
1240 finish_cleanup_try_block (tree try_block)
1242 TRY_STMTS (try_block) = pop_stmt_list (TRY_STMTS (try_block));
1245 /* Finish an implicitly generated try-block, with a cleanup is given
1246 by CLEANUP. */
1248 void
1249 finish_cleanup (tree cleanup, tree try_block)
1251 TRY_HANDLERS (try_block) = cleanup;
1252 CLEANUP_P (try_block) = 1;
1255 /* Likewise, for a function-try-block. */
1257 void
1258 finish_function_try_block (tree try_block)
1260 finish_try_block (try_block);
1261 /* FIXME : something queer about CTOR_INITIALIZER somehow following
1262 the try block, but moving it inside. */
1263 in_function_try_handler = 1;
1266 /* Finish a handler-sequence for a try-block, which may be given by
1267 TRY_BLOCK. */
1269 void
1270 finish_handler_sequence (tree try_block)
1272 TRY_HANDLERS (try_block) = pop_stmt_list (TRY_HANDLERS (try_block));
1273 check_handlers (TRY_HANDLERS (try_block));
1276 /* Finish the handler-seq for a function-try-block, given by
1277 TRY_BLOCK. COMPOUND_STMT is the outer block created by
1278 begin_function_try_block. */
1280 void
1281 finish_function_handler_sequence (tree try_block, tree compound_stmt)
1283 in_function_try_handler = 0;
1284 finish_handler_sequence (try_block);
1285 finish_compound_stmt (compound_stmt);
1288 /* Begin a handler. Returns a HANDLER if appropriate. */
1290 tree
1291 begin_handler (void)
1293 tree r;
1295 r = build_stmt (input_location, HANDLER, NULL_TREE, NULL_TREE);
1296 add_stmt (r);
1298 /* Create a binding level for the eh_info and the exception object
1299 cleanup. */
1300 HANDLER_BODY (r) = do_pushlevel (sk_catch);
1302 return r;
1305 /* Finish the handler-parameters for a handler, which may be given by
1306 HANDLER. DECL is the declaration for the catch parameter, or NULL
1307 if this is a `catch (...)' clause. */
1309 void
1310 finish_handler_parms (tree decl, tree handler)
1312 tree type = NULL_TREE;
1313 if (processing_template_decl)
1315 if (decl)
1317 decl = pushdecl (decl);
1318 decl = push_template_decl (decl);
1319 HANDLER_PARMS (handler) = decl;
1320 type = TREE_TYPE (decl);
1323 else
1324 type = expand_start_catch_block (decl);
1325 HANDLER_TYPE (handler) = type;
1328 /* Finish a handler, which may be given by HANDLER. The BLOCKs are
1329 the return value from the matching call to finish_handler_parms. */
1331 void
1332 finish_handler (tree handler)
1334 if (!processing_template_decl)
1335 expand_end_catch_block ();
1336 HANDLER_BODY (handler) = do_poplevel (HANDLER_BODY (handler));
1339 /* Begin a compound statement. FLAGS contains some bits that control the
1340 behavior and context. If BCS_NO_SCOPE is set, the compound statement
1341 does not define a scope. If BCS_FN_BODY is set, this is the outermost
1342 block of a function. If BCS_TRY_BLOCK is set, this is the block
1343 created on behalf of a TRY statement. Returns a token to be passed to
1344 finish_compound_stmt. */
1346 tree
1347 begin_compound_stmt (unsigned int flags)
1349 tree r;
1351 if (flags & BCS_NO_SCOPE)
1353 r = push_stmt_list ();
1354 STATEMENT_LIST_NO_SCOPE (r) = 1;
1356 /* Normally, we try hard to keep the BLOCK for a statement-expression.
1357 But, if it's a statement-expression with a scopeless block, there's
1358 nothing to keep, and we don't want to accidentally keep a block
1359 *inside* the scopeless block. */
1360 keep_next_level (false);
1362 else
1364 scope_kind sk = sk_block;
1365 if (flags & BCS_TRY_BLOCK)
1366 sk = sk_try;
1367 else if (flags & BCS_TRANSACTION)
1368 sk = sk_transaction;
1369 r = do_pushlevel (sk);
1372 /* When processing a template, we need to remember where the braces were,
1373 so that we can set up identical scopes when instantiating the template
1374 later. BIND_EXPR is a handy candidate for this.
1375 Note that do_poplevel won't create a BIND_EXPR itself here (and thus
1376 result in nested BIND_EXPRs), since we don't build BLOCK nodes when
1377 processing templates. */
1378 if (processing_template_decl)
1380 r = build3 (BIND_EXPR, NULL, NULL, r, NULL);
1381 BIND_EXPR_TRY_BLOCK (r) = (flags & BCS_TRY_BLOCK) != 0;
1382 BIND_EXPR_BODY_BLOCK (r) = (flags & BCS_FN_BODY) != 0;
1383 TREE_SIDE_EFFECTS (r) = 1;
1386 return r;
1389 /* Finish a compound-statement, which is given by STMT. */
1391 void
1392 finish_compound_stmt (tree stmt)
1394 if (TREE_CODE (stmt) == BIND_EXPR)
1396 tree body = do_poplevel (BIND_EXPR_BODY (stmt));
1397 /* If the STATEMENT_LIST is empty and this BIND_EXPR isn't special,
1398 discard the BIND_EXPR so it can be merged with the containing
1399 STATEMENT_LIST. */
1400 if (TREE_CODE (body) == STATEMENT_LIST
1401 && STATEMENT_LIST_HEAD (body) == NULL
1402 && !BIND_EXPR_BODY_BLOCK (stmt)
1403 && !BIND_EXPR_TRY_BLOCK (stmt))
1404 stmt = body;
1405 else
1406 BIND_EXPR_BODY (stmt) = body;
1408 else if (STATEMENT_LIST_NO_SCOPE (stmt))
1409 stmt = pop_stmt_list (stmt);
1410 else
1412 /* Destroy any ObjC "super" receivers that may have been
1413 created. */
1414 objc_clear_super_receiver ();
1416 stmt = do_poplevel (stmt);
1419 /* ??? See c_end_compound_stmt wrt statement expressions. */
1420 add_stmt (stmt);
1423 /* Finish an asm-statement, whose components are a STRING, some
1424 OUTPUT_OPERANDS, some INPUT_OPERANDS, some CLOBBERS and some
1425 LABELS. Also note whether the asm-statement should be
1426 considered volatile. */
1428 tree
1429 finish_asm_stmt (int volatile_p, tree string, tree output_operands,
1430 tree input_operands, tree clobbers, tree labels)
1432 tree r;
1433 tree t;
1434 int ninputs = list_length (input_operands);
1435 int noutputs = list_length (output_operands);
1437 if (!processing_template_decl)
1439 const char *constraint;
1440 const char **oconstraints;
1441 bool allows_mem, allows_reg, is_inout;
1442 tree operand;
1443 int i;
1445 oconstraints = XALLOCAVEC (const char *, noutputs);
1447 string = resolve_asm_operand_names (string, output_operands,
1448 input_operands, labels);
1450 for (i = 0, t = output_operands; t; t = TREE_CHAIN (t), ++i)
1452 operand = TREE_VALUE (t);
1454 /* ??? Really, this should not be here. Users should be using a
1455 proper lvalue, dammit. But there's a long history of using
1456 casts in the output operands. In cases like longlong.h, this
1457 becomes a primitive form of typechecking -- if the cast can be
1458 removed, then the output operand had a type of the proper width;
1459 otherwise we'll get an error. Gross, but ... */
1460 STRIP_NOPS (operand);
1462 operand = mark_lvalue_use (operand);
1464 if (!lvalue_or_else (operand, lv_asm, tf_warning_or_error))
1465 operand = error_mark_node;
1467 if (operand != error_mark_node
1468 && (TREE_READONLY (operand)
1469 || CP_TYPE_CONST_P (TREE_TYPE (operand))
1470 /* Functions are not modifiable, even though they are
1471 lvalues. */
1472 || TREE_CODE (TREE_TYPE (operand)) == FUNCTION_TYPE
1473 || TREE_CODE (TREE_TYPE (operand)) == METHOD_TYPE
1474 /* If it's an aggregate and any field is const, then it is
1475 effectively const. */
1476 || (CLASS_TYPE_P (TREE_TYPE (operand))
1477 && C_TYPE_FIELDS_READONLY (TREE_TYPE (operand)))))
1478 cxx_readonly_error (operand, lv_asm);
1480 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
1481 oconstraints[i] = constraint;
1483 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
1484 &allows_mem, &allows_reg, &is_inout))
1486 /* If the operand is going to end up in memory,
1487 mark it addressable. */
1488 if (!allows_reg && !cxx_mark_addressable (operand))
1489 operand = error_mark_node;
1491 else
1492 operand = error_mark_node;
1494 TREE_VALUE (t) = operand;
1497 for (i = 0, t = input_operands; t; ++i, t = TREE_CHAIN (t))
1499 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
1500 bool constraint_parsed
1501 = parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
1502 oconstraints, &allows_mem, &allows_reg);
1503 /* If the operand is going to end up in memory, don't call
1504 decay_conversion. */
1505 if (constraint_parsed && !allows_reg && allows_mem)
1506 operand = mark_lvalue_use (TREE_VALUE (t));
1507 else
1508 operand = decay_conversion (TREE_VALUE (t), tf_warning_or_error);
1510 /* If the type of the operand hasn't been determined (e.g.,
1511 because it involves an overloaded function), then issue
1512 an error message. There's no context available to
1513 resolve the overloading. */
1514 if (TREE_TYPE (operand) == unknown_type_node)
1516 error ("type of asm operand %qE could not be determined",
1517 TREE_VALUE (t));
1518 operand = error_mark_node;
1521 if (constraint_parsed)
1523 /* If the operand is going to end up in memory,
1524 mark it addressable. */
1525 if (!allows_reg && allows_mem)
1527 /* Strip the nops as we allow this case. FIXME, this really
1528 should be rejected or made deprecated. */
1529 STRIP_NOPS (operand);
1530 if (!cxx_mark_addressable (operand))
1531 operand = error_mark_node;
1533 else if (!allows_reg && !allows_mem)
1535 /* If constraint allows neither register nor memory,
1536 try harder to get a constant. */
1537 tree constop = maybe_constant_value (operand);
1538 if (TREE_CONSTANT (constop))
1539 operand = constop;
1542 else
1543 operand = error_mark_node;
1545 TREE_VALUE (t) = operand;
1549 r = build_stmt (input_location, ASM_EXPR, string,
1550 output_operands, input_operands,
1551 clobbers, labels);
1552 ASM_VOLATILE_P (r) = volatile_p || noutputs == 0;
1553 r = maybe_cleanup_point_expr_void (r);
1554 return add_stmt (r);
1557 /* Finish a label with the indicated NAME. Returns the new label. */
1559 tree
1560 finish_label_stmt (tree name)
1562 tree decl = define_label (input_location, name);
1564 if (decl == error_mark_node)
1565 return error_mark_node;
1567 add_stmt (build_stmt (input_location, LABEL_EXPR, decl));
1569 return decl;
1572 /* Finish a series of declarations for local labels. G++ allows users
1573 to declare "local" labels, i.e., labels with scope. This extension
1574 is useful when writing code involving statement-expressions. */
1576 void
1577 finish_label_decl (tree name)
1579 if (!at_function_scope_p ())
1581 error ("__label__ declarations are only allowed in function scopes");
1582 return;
1585 add_decl_expr (declare_local_label (name));
1588 /* When DECL goes out of scope, make sure that CLEANUP is executed. */
1590 void
1591 finish_decl_cleanup (tree decl, tree cleanup)
1593 push_cleanup (decl, cleanup, false);
1596 /* If the current scope exits with an exception, run CLEANUP. */
1598 void
1599 finish_eh_cleanup (tree cleanup)
1601 push_cleanup (NULL, cleanup, true);
1604 /* The MEM_INITS is a list of mem-initializers, in reverse of the
1605 order they were written by the user. Each node is as for
1606 emit_mem_initializers. */
1608 void
1609 finish_mem_initializers (tree mem_inits)
1611 /* Reorder the MEM_INITS so that they are in the order they appeared
1612 in the source program. */
1613 mem_inits = nreverse (mem_inits);
1615 if (processing_template_decl)
1617 tree mem;
1619 for (mem = mem_inits; mem; mem = TREE_CHAIN (mem))
1621 /* If the TREE_PURPOSE is a TYPE_PACK_EXPANSION, skip the
1622 check for bare parameter packs in the TREE_VALUE, because
1623 any parameter packs in the TREE_VALUE have already been
1624 bound as part of the TREE_PURPOSE. See
1625 make_pack_expansion for more information. */
1626 if (TREE_CODE (TREE_PURPOSE (mem)) != TYPE_PACK_EXPANSION
1627 && check_for_bare_parameter_packs (TREE_VALUE (mem)))
1628 TREE_VALUE (mem) = error_mark_node;
1631 add_stmt (build_min_nt_loc (UNKNOWN_LOCATION,
1632 CTOR_INITIALIZER, mem_inits));
1634 else
1635 emit_mem_initializers (mem_inits);
1638 /* Obfuscate EXPR if it looks like an id-expression or member access so
1639 that the call to finish_decltype in do_auto_deduction will give the
1640 right result. */
1642 tree
1643 force_paren_expr (tree expr)
1645 /* This is only needed for decltype(auto) in C++14. */
1646 if (cxx_dialect < cxx14)
1647 return expr;
1649 /* If we're in unevaluated context, we can't be deducing a
1650 return/initializer type, so we don't need to mess with this. */
1651 if (cp_unevaluated_operand)
1652 return expr;
1654 if (!DECL_P (expr) && TREE_CODE (expr) != COMPONENT_REF
1655 && TREE_CODE (expr) != SCOPE_REF)
1656 return expr;
1658 if (TREE_CODE (expr) == COMPONENT_REF
1659 || TREE_CODE (expr) == SCOPE_REF)
1660 REF_PARENTHESIZED_P (expr) = true;
1661 else if (type_dependent_expression_p (expr))
1662 expr = build1 (PAREN_EXPR, TREE_TYPE (expr), expr);
1663 else if (VAR_P (expr) && DECL_HARD_REGISTER (expr))
1664 /* We can't bind a hard register variable to a reference. */;
1665 else
1667 cp_lvalue_kind kind = lvalue_kind (expr);
1668 if ((kind & ~clk_class) != clk_none)
1670 tree type = unlowered_expr_type (expr);
1671 bool rval = !!(kind & clk_rvalueref);
1672 type = cp_build_reference_type (type, rval);
1673 /* This inhibits warnings in, eg, cxx_mark_addressable
1674 (c++/60955). */
1675 warning_sentinel s (extra_warnings);
1676 expr = build_static_cast (type, expr, tf_error);
1677 if (expr != error_mark_node)
1678 REF_PARENTHESIZED_P (expr) = true;
1682 return expr;
1685 /* If T is an id-expression obfuscated by force_paren_expr, undo the
1686 obfuscation and return the underlying id-expression. Otherwise
1687 return T. */
1689 tree
1690 maybe_undo_parenthesized_ref (tree t)
1692 if (cxx_dialect >= cxx14
1693 && INDIRECT_REF_P (t)
1694 && REF_PARENTHESIZED_P (t))
1696 t = TREE_OPERAND (t, 0);
1697 while (TREE_CODE (t) == NON_LVALUE_EXPR
1698 || TREE_CODE (t) == NOP_EXPR)
1699 t = TREE_OPERAND (t, 0);
1701 gcc_assert (TREE_CODE (t) == ADDR_EXPR
1702 || TREE_CODE (t) == STATIC_CAST_EXPR);
1703 t = TREE_OPERAND (t, 0);
1706 return t;
1709 /* Finish a parenthesized expression EXPR. */
1711 cp_expr
1712 finish_parenthesized_expr (cp_expr expr)
1714 if (EXPR_P (expr))
1715 /* This inhibits warnings in c_common_truthvalue_conversion. */
1716 TREE_NO_WARNING (expr) = 1;
1718 if (TREE_CODE (expr) == OFFSET_REF
1719 || TREE_CODE (expr) == SCOPE_REF)
1720 /* [expr.unary.op]/3 The qualified id of a pointer-to-member must not be
1721 enclosed in parentheses. */
1722 PTRMEM_OK_P (expr) = 0;
1724 if (TREE_CODE (expr) == STRING_CST)
1725 PAREN_STRING_LITERAL_P (expr) = 1;
1727 expr = cp_expr (force_paren_expr (expr), expr.get_location ());
1729 return expr;
1732 /* Finish a reference to a non-static data member (DECL) that is not
1733 preceded by `.' or `->'. */
1735 tree
1736 finish_non_static_data_member (tree decl, tree object, tree qualifying_scope)
1738 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
1739 bool try_omp_private = !object && omp_private_member_map;
1740 tree ret;
1742 if (!object)
1744 tree scope = qualifying_scope;
1745 if (scope == NULL_TREE)
1746 scope = context_for_name_lookup (decl);
1747 object = maybe_dummy_object (scope, NULL);
1750 object = maybe_resolve_dummy (object, true);
1751 if (object == error_mark_node)
1752 return error_mark_node;
1754 /* DR 613/850: Can use non-static data members without an associated
1755 object in sizeof/decltype/alignof. */
1756 if (is_dummy_object (object) && cp_unevaluated_operand == 0
1757 && (!processing_template_decl || !current_class_ref))
1759 if (current_function_decl
1760 && DECL_STATIC_FUNCTION_P (current_function_decl))
1761 error ("invalid use of member %qD in static member function", decl);
1762 else
1763 error ("invalid use of non-static data member %qD", decl);
1764 inform (DECL_SOURCE_LOCATION (decl), "declared here");
1766 return error_mark_node;
1769 if (current_class_ptr)
1770 TREE_USED (current_class_ptr) = 1;
1771 if (processing_template_decl && !qualifying_scope)
1773 tree type = TREE_TYPE (decl);
1775 if (TREE_CODE (type) == REFERENCE_TYPE)
1776 /* Quals on the object don't matter. */;
1777 else if (PACK_EXPANSION_P (type))
1778 /* Don't bother trying to represent this. */
1779 type = NULL_TREE;
1780 else
1782 /* Set the cv qualifiers. */
1783 int quals = cp_type_quals (TREE_TYPE (object));
1785 if (DECL_MUTABLE_P (decl))
1786 quals &= ~TYPE_QUAL_CONST;
1788 quals |= cp_type_quals (TREE_TYPE (decl));
1789 type = cp_build_qualified_type (type, quals);
1792 ret = (convert_from_reference
1793 (build_min (COMPONENT_REF, type, object, decl, NULL_TREE)));
1795 /* If PROCESSING_TEMPLATE_DECL is nonzero here, then
1796 QUALIFYING_SCOPE is also non-null. Wrap this in a SCOPE_REF
1797 for now. */
1798 else if (processing_template_decl)
1799 ret = build_qualified_name (TREE_TYPE (decl),
1800 qualifying_scope,
1801 decl,
1802 /*template_p=*/false);
1803 else
1805 tree access_type = TREE_TYPE (object);
1807 perform_or_defer_access_check (TYPE_BINFO (access_type), decl,
1808 decl, tf_warning_or_error);
1810 /* If the data member was named `C::M', convert `*this' to `C'
1811 first. */
1812 if (qualifying_scope)
1814 tree binfo = NULL_TREE;
1815 object = build_scoped_ref (object, qualifying_scope,
1816 &binfo);
1819 ret = build_class_member_access_expr (object, decl,
1820 /*access_path=*/NULL_TREE,
1821 /*preserve_reference=*/false,
1822 tf_warning_or_error);
1824 if (try_omp_private)
1826 tree *v = omp_private_member_map->get (decl);
1827 if (v)
1828 ret = convert_from_reference (*v);
1830 return ret;
1833 /* If we are currently parsing a template and we encountered a typedef
1834 TYPEDEF_DECL that is being accessed though CONTEXT, this function
1835 adds the typedef to a list tied to the current template.
1836 At template instantiation time, that list is walked and access check
1837 performed for each typedef.
1838 LOCATION is the location of the usage point of TYPEDEF_DECL. */
1840 void
1841 add_typedef_to_current_template_for_access_check (tree typedef_decl,
1842 tree context,
1843 location_t location)
1845 tree template_info = NULL;
1846 tree cs = current_scope ();
1848 if (!is_typedef_decl (typedef_decl)
1849 || !context
1850 || !CLASS_TYPE_P (context)
1851 || !cs)
1852 return;
1854 if (CLASS_TYPE_P (cs) || TREE_CODE (cs) == FUNCTION_DECL)
1855 template_info = get_template_info (cs);
1857 if (template_info
1858 && TI_TEMPLATE (template_info)
1859 && !currently_open_class (context))
1860 append_type_to_template_for_access_check (cs, typedef_decl,
1861 context, location);
1864 /* DECL was the declaration to which a qualified-id resolved. Issue
1865 an error message if it is not accessible. If OBJECT_TYPE is
1866 non-NULL, we have just seen `x->' or `x.' and OBJECT_TYPE is the
1867 type of `*x', or `x', respectively. If the DECL was named as
1868 `A::B' then NESTED_NAME_SPECIFIER is `A'. */
1870 void
1871 check_accessibility_of_qualified_id (tree decl,
1872 tree object_type,
1873 tree nested_name_specifier)
1875 tree scope;
1876 tree qualifying_type = NULL_TREE;
1878 /* If we are parsing a template declaration and if decl is a typedef,
1879 add it to a list tied to the template.
1880 At template instantiation time, that list will be walked and
1881 access check performed. */
1882 add_typedef_to_current_template_for_access_check (decl,
1883 nested_name_specifier
1884 ? nested_name_specifier
1885 : DECL_CONTEXT (decl),
1886 input_location);
1888 /* If we're not checking, return immediately. */
1889 if (deferred_access_no_check)
1890 return;
1892 /* Determine the SCOPE of DECL. */
1893 scope = context_for_name_lookup (decl);
1894 /* If the SCOPE is not a type, then DECL is not a member. */
1895 if (!TYPE_P (scope))
1896 return;
1897 /* Compute the scope through which DECL is being accessed. */
1898 if (object_type
1899 /* OBJECT_TYPE might not be a class type; consider:
1901 class A { typedef int I; };
1902 I *p;
1903 p->A::I::~I();
1905 In this case, we will have "A::I" as the DECL, but "I" as the
1906 OBJECT_TYPE. */
1907 && CLASS_TYPE_P (object_type)
1908 && DERIVED_FROM_P (scope, object_type))
1909 /* If we are processing a `->' or `.' expression, use the type of the
1910 left-hand side. */
1911 qualifying_type = object_type;
1912 else if (nested_name_specifier)
1914 /* If the reference is to a non-static member of the
1915 current class, treat it as if it were referenced through
1916 `this'. */
1917 tree ct;
1918 if (DECL_NONSTATIC_MEMBER_P (decl)
1919 && current_class_ptr
1920 && DERIVED_FROM_P (scope, ct = current_nonlambda_class_type ()))
1921 qualifying_type = ct;
1922 /* Otherwise, use the type indicated by the
1923 nested-name-specifier. */
1924 else
1925 qualifying_type = nested_name_specifier;
1927 else
1928 /* Otherwise, the name must be from the current class or one of
1929 its bases. */
1930 qualifying_type = currently_open_derived_class (scope);
1932 if (qualifying_type
1933 /* It is possible for qualifying type to be a TEMPLATE_TYPE_PARM
1934 or similar in a default argument value. */
1935 && CLASS_TYPE_P (qualifying_type)
1936 && !dependent_type_p (qualifying_type))
1937 perform_or_defer_access_check (TYPE_BINFO (qualifying_type), decl,
1938 decl, tf_warning_or_error);
1941 /* EXPR is the result of a qualified-id. The QUALIFYING_CLASS was the
1942 class named to the left of the "::" operator. DONE is true if this
1943 expression is a complete postfix-expression; it is false if this
1944 expression is followed by '->', '[', '(', etc. ADDRESS_P is true
1945 iff this expression is the operand of '&'. TEMPLATE_P is true iff
1946 the qualified-id was of the form "A::template B". TEMPLATE_ARG_P
1947 is true iff this qualified name appears as a template argument. */
1949 tree
1950 finish_qualified_id_expr (tree qualifying_class,
1951 tree expr,
1952 bool done,
1953 bool address_p,
1954 bool template_p,
1955 bool template_arg_p,
1956 tsubst_flags_t complain)
1958 gcc_assert (TYPE_P (qualifying_class));
1960 if (error_operand_p (expr))
1961 return error_mark_node;
1963 if ((DECL_P (expr) || BASELINK_P (expr))
1964 && !mark_used (expr, complain))
1965 return error_mark_node;
1967 if (template_p)
1969 if (TREE_CODE (expr) == UNBOUND_CLASS_TEMPLATE)
1970 /* cp_parser_lookup_name thought we were looking for a type,
1971 but we're actually looking for a declaration. */
1972 expr = build_qualified_name (/*type*/NULL_TREE,
1973 TYPE_CONTEXT (expr),
1974 TYPE_IDENTIFIER (expr),
1975 /*template_p*/true);
1976 else
1977 check_template_keyword (expr);
1980 /* If EXPR occurs as the operand of '&', use special handling that
1981 permits a pointer-to-member. */
1982 if (address_p && done)
1984 if (TREE_CODE (expr) == SCOPE_REF)
1985 expr = TREE_OPERAND (expr, 1);
1986 expr = build_offset_ref (qualifying_class, expr,
1987 /*address_p=*/true, complain);
1988 return expr;
1991 /* No need to check access within an enum. */
1992 if (TREE_CODE (qualifying_class) == ENUMERAL_TYPE)
1993 return expr;
1995 /* Within the scope of a class, turn references to non-static
1996 members into expression of the form "this->...". */
1997 if (template_arg_p)
1998 /* But, within a template argument, we do not want make the
1999 transformation, as there is no "this" pointer. */
2001 else if (TREE_CODE (expr) == FIELD_DECL)
2003 push_deferring_access_checks (dk_no_check);
2004 expr = finish_non_static_data_member (expr, NULL_TREE,
2005 qualifying_class);
2006 pop_deferring_access_checks ();
2008 else if (BASELINK_P (expr) && !processing_template_decl)
2010 /* See if any of the functions are non-static members. */
2011 /* If so, the expression may be relative to 'this'. */
2012 if (!shared_member_p (expr)
2013 && current_class_ptr
2014 && DERIVED_FROM_P (qualifying_class,
2015 current_nonlambda_class_type ()))
2016 expr = (build_class_member_access_expr
2017 (maybe_dummy_object (qualifying_class, NULL),
2018 expr,
2019 BASELINK_ACCESS_BINFO (expr),
2020 /*preserve_reference=*/false,
2021 complain));
2022 else if (done)
2023 /* The expression is a qualified name whose address is not
2024 being taken. */
2025 expr = build_offset_ref (qualifying_class, expr, /*address_p=*/false,
2026 complain);
2028 else if (BASELINK_P (expr))
2030 else
2032 /* In a template, return a SCOPE_REF for most qualified-ids
2033 so that we can check access at instantiation time. But if
2034 we're looking at a member of the current instantiation, we
2035 know we have access and building up the SCOPE_REF confuses
2036 non-type template argument handling. */
2037 if (processing_template_decl
2038 && !currently_open_class (qualifying_class))
2039 expr = build_qualified_name (TREE_TYPE (expr),
2040 qualifying_class, expr,
2041 template_p);
2043 expr = convert_from_reference (expr);
2046 return expr;
2049 /* Begin a statement-expression. The value returned must be passed to
2050 finish_stmt_expr. */
2052 tree
2053 begin_stmt_expr (void)
2055 return push_stmt_list ();
2058 /* Process the final expression of a statement expression. EXPR can be
2059 NULL, if the final expression is empty. Return a STATEMENT_LIST
2060 containing all the statements in the statement-expression, or
2061 ERROR_MARK_NODE if there was an error. */
2063 tree
2064 finish_stmt_expr_expr (tree expr, tree stmt_expr)
2066 if (error_operand_p (expr))
2068 /* The type of the statement-expression is the type of the last
2069 expression. */
2070 TREE_TYPE (stmt_expr) = error_mark_node;
2071 return error_mark_node;
2074 /* If the last statement does not have "void" type, then the value
2075 of the last statement is the value of the entire expression. */
2076 if (expr)
2078 tree type = TREE_TYPE (expr);
2080 if (processing_template_decl)
2082 expr = build_stmt (input_location, EXPR_STMT, expr);
2083 expr = add_stmt (expr);
2084 /* Mark the last statement so that we can recognize it as such at
2085 template-instantiation time. */
2086 EXPR_STMT_STMT_EXPR_RESULT (expr) = 1;
2088 else if (VOID_TYPE_P (type))
2090 /* Just treat this like an ordinary statement. */
2091 expr = finish_expr_stmt (expr);
2093 else
2095 /* It actually has a value we need to deal with. First, force it
2096 to be an rvalue so that we won't need to build up a copy
2097 constructor call later when we try to assign it to something. */
2098 expr = force_rvalue (expr, tf_warning_or_error);
2099 if (error_operand_p (expr))
2100 return error_mark_node;
2102 /* Update for array-to-pointer decay. */
2103 type = TREE_TYPE (expr);
2105 /* Wrap it in a CLEANUP_POINT_EXPR and add it to the list like a
2106 normal statement, but don't convert to void or actually add
2107 the EXPR_STMT. */
2108 if (TREE_CODE (expr) != CLEANUP_POINT_EXPR)
2109 expr = maybe_cleanup_point_expr (expr);
2110 add_stmt (expr);
2113 /* The type of the statement-expression is the type of the last
2114 expression. */
2115 TREE_TYPE (stmt_expr) = type;
2118 return stmt_expr;
2121 /* Finish a statement-expression. EXPR should be the value returned
2122 by the previous begin_stmt_expr. Returns an expression
2123 representing the statement-expression. */
2125 tree
2126 finish_stmt_expr (tree stmt_expr, bool has_no_scope)
2128 tree type;
2129 tree result;
2131 if (error_operand_p (stmt_expr))
2133 pop_stmt_list (stmt_expr);
2134 return error_mark_node;
2137 gcc_assert (TREE_CODE (stmt_expr) == STATEMENT_LIST);
2139 type = TREE_TYPE (stmt_expr);
2140 result = pop_stmt_list (stmt_expr);
2141 TREE_TYPE (result) = type;
2143 if (processing_template_decl)
2145 result = build_min (STMT_EXPR, type, result);
2146 TREE_SIDE_EFFECTS (result) = 1;
2147 STMT_EXPR_NO_SCOPE (result) = has_no_scope;
2149 else if (CLASS_TYPE_P (type))
2151 /* Wrap the statement-expression in a TARGET_EXPR so that the
2152 temporary object created by the final expression is destroyed at
2153 the end of the full-expression containing the
2154 statement-expression. */
2155 result = force_target_expr (type, result, tf_warning_or_error);
2158 return result;
2161 /* Returns the expression which provides the value of STMT_EXPR. */
2163 tree
2164 stmt_expr_value_expr (tree stmt_expr)
2166 tree t = STMT_EXPR_STMT (stmt_expr);
2168 if (TREE_CODE (t) == BIND_EXPR)
2169 t = BIND_EXPR_BODY (t);
2171 if (TREE_CODE (t) == STATEMENT_LIST && STATEMENT_LIST_TAIL (t))
2172 t = STATEMENT_LIST_TAIL (t)->stmt;
2174 if (TREE_CODE (t) == EXPR_STMT)
2175 t = EXPR_STMT_EXPR (t);
2177 return t;
2180 /* Return TRUE iff EXPR_STMT is an empty list of
2181 expression statements. */
2183 bool
2184 empty_expr_stmt_p (tree expr_stmt)
2186 tree body = NULL_TREE;
2188 if (expr_stmt == void_node)
2189 return true;
2191 if (expr_stmt)
2193 if (TREE_CODE (expr_stmt) == EXPR_STMT)
2194 body = EXPR_STMT_EXPR (expr_stmt);
2195 else if (TREE_CODE (expr_stmt) == STATEMENT_LIST)
2196 body = expr_stmt;
2199 if (body)
2201 if (TREE_CODE (body) == STATEMENT_LIST)
2202 return tsi_end_p (tsi_start (body));
2203 else
2204 return empty_expr_stmt_p (body);
2206 return false;
2209 /* Perform Koenig lookup. FN is the postfix-expression representing
2210 the function (or functions) to call; ARGS are the arguments to the
2211 call. Returns the functions to be considered by overload resolution. */
2213 cp_expr
2214 perform_koenig_lookup (cp_expr fn, vec<tree, va_gc> *args,
2215 tsubst_flags_t complain)
2217 tree identifier = NULL_TREE;
2218 tree functions = NULL_TREE;
2219 tree tmpl_args = NULL_TREE;
2220 bool template_id = false;
2221 location_t loc = fn.get_location ();
2223 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
2225 /* Use a separate flag to handle null args. */
2226 template_id = true;
2227 tmpl_args = TREE_OPERAND (fn, 1);
2228 fn = TREE_OPERAND (fn, 0);
2231 /* Find the name of the overloaded function. */
2232 if (identifier_p (fn))
2233 identifier = fn;
2234 else if (is_overloaded_fn (fn))
2236 functions = fn;
2237 identifier = DECL_NAME (get_first_fn (functions));
2239 else if (DECL_P (fn))
2241 functions = fn;
2242 identifier = DECL_NAME (fn);
2245 /* A call to a namespace-scope function using an unqualified name.
2247 Do Koenig lookup -- unless any of the arguments are
2248 type-dependent. */
2249 if (!any_type_dependent_arguments_p (args)
2250 && !any_dependent_template_arguments_p (tmpl_args))
2252 fn = lookup_arg_dependent (identifier, functions, args);
2253 if (!fn)
2255 /* The unqualified name could not be resolved. */
2256 if (complain & tf_error)
2257 fn = unqualified_fn_lookup_error (cp_expr (identifier, loc));
2258 else
2259 fn = identifier;
2263 if (fn && template_id && fn != error_mark_node)
2264 fn = build2 (TEMPLATE_ID_EXPR, unknown_type_node, fn, tmpl_args);
2266 return fn;
2269 /* Generate an expression for `FN (ARGS)'. This may change the
2270 contents of ARGS.
2272 If DISALLOW_VIRTUAL is true, the call to FN will be not generated
2273 as a virtual call, even if FN is virtual. (This flag is set when
2274 encountering an expression where the function name is explicitly
2275 qualified. For example a call to `X::f' never generates a virtual
2276 call.)
2278 Returns code for the call. */
2280 tree
2281 finish_call_expr (tree fn, vec<tree, va_gc> **args, bool disallow_virtual,
2282 bool koenig_p, tsubst_flags_t complain)
2284 tree result;
2285 tree orig_fn;
2286 vec<tree, va_gc> *orig_args = NULL;
2288 if (fn == error_mark_node)
2289 return error_mark_node;
2291 gcc_assert (!TYPE_P (fn));
2293 /* If FN may be a FUNCTION_DECL obfuscated by force_paren_expr, undo
2294 it so that we can tell this is a call to a known function. */
2295 fn = maybe_undo_parenthesized_ref (fn);
2297 orig_fn = fn;
2299 if (processing_template_decl)
2301 /* If the call expression is dependent, build a CALL_EXPR node
2302 with no type; type_dependent_expression_p recognizes
2303 expressions with no type as being dependent. */
2304 if (type_dependent_expression_p (fn)
2305 || any_type_dependent_arguments_p (*args))
2307 result = build_nt_call_vec (fn, *args);
2308 SET_EXPR_LOCATION (result, EXPR_LOC_OR_LOC (fn, input_location));
2309 KOENIG_LOOKUP_P (result) = koenig_p;
2310 if (cfun)
2314 tree fndecl = OVL_CURRENT (fn);
2315 if (TREE_CODE (fndecl) != FUNCTION_DECL
2316 || !TREE_THIS_VOLATILE (fndecl))
2317 break;
2318 fn = OVL_NEXT (fn);
2320 while (fn);
2321 if (!fn)
2322 current_function_returns_abnormally = 1;
2324 return result;
2326 orig_args = make_tree_vector_copy (*args);
2327 if (!BASELINK_P (fn)
2328 && TREE_CODE (fn) != PSEUDO_DTOR_EXPR
2329 && TREE_TYPE (fn) != unknown_type_node)
2330 fn = build_non_dependent_expr (fn);
2331 make_args_non_dependent (*args);
2334 if (TREE_CODE (fn) == COMPONENT_REF)
2336 tree member = TREE_OPERAND (fn, 1);
2337 if (BASELINK_P (member))
2339 tree object = TREE_OPERAND (fn, 0);
2340 return build_new_method_call (object, member,
2341 args, NULL_TREE,
2342 (disallow_virtual
2343 ? LOOKUP_NORMAL | LOOKUP_NONVIRTUAL
2344 : LOOKUP_NORMAL),
2345 /*fn_p=*/NULL,
2346 complain);
2350 /* Per 13.3.1.1, '(&f)(...)' is the same as '(f)(...)'. */
2351 if (TREE_CODE (fn) == ADDR_EXPR
2352 && TREE_CODE (TREE_OPERAND (fn, 0)) == OVERLOAD)
2353 fn = TREE_OPERAND (fn, 0);
2355 if (is_overloaded_fn (fn))
2356 fn = baselink_for_fns (fn);
2358 result = NULL_TREE;
2359 if (BASELINK_P (fn))
2361 tree object;
2363 /* A call to a member function. From [over.call.func]:
2365 If the keyword this is in scope and refers to the class of
2366 that member function, or a derived class thereof, then the
2367 function call is transformed into a qualified function call
2368 using (*this) as the postfix-expression to the left of the
2369 . operator.... [Otherwise] a contrived object of type T
2370 becomes the implied object argument.
2372 In this situation:
2374 struct A { void f(); };
2375 struct B : public A {};
2376 struct C : public A { void g() { B::f(); }};
2378 "the class of that member function" refers to `A'. But 11.2
2379 [class.access.base] says that we need to convert 'this' to B* as
2380 part of the access, so we pass 'B' to maybe_dummy_object. */
2382 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (get_first_fn (fn)))
2384 /* A constructor call always uses a dummy object. (This constructor
2385 call which has the form A::A () is actually invalid and we are
2386 going to reject it later in build_new_method_call.) */
2387 object = build_dummy_object (BINFO_TYPE (BASELINK_ACCESS_BINFO (fn)));
2389 else
2390 object = maybe_dummy_object (BINFO_TYPE (BASELINK_ACCESS_BINFO (fn)),
2391 NULL);
2393 result = build_new_method_call (object, fn, args, NULL_TREE,
2394 (disallow_virtual
2395 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
2396 : LOOKUP_NORMAL),
2397 /*fn_p=*/NULL,
2398 complain);
2400 else if (is_overloaded_fn (fn))
2402 /* If the function is an overloaded builtin, resolve it. */
2403 if (TREE_CODE (fn) == FUNCTION_DECL
2404 && (DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL
2405 || DECL_BUILT_IN_CLASS (fn) == BUILT_IN_MD))
2406 result = resolve_overloaded_builtin (input_location, fn, *args);
2408 if (!result)
2410 if (warn_sizeof_pointer_memaccess
2411 && (complain & tf_warning)
2412 && !vec_safe_is_empty (*args)
2413 && !processing_template_decl)
2415 location_t sizeof_arg_loc[3];
2416 tree sizeof_arg[3];
2417 unsigned int i;
2418 for (i = 0; i < 3; i++)
2420 tree t;
2422 sizeof_arg_loc[i] = UNKNOWN_LOCATION;
2423 sizeof_arg[i] = NULL_TREE;
2424 if (i >= (*args)->length ())
2425 continue;
2426 t = (**args)[i];
2427 if (TREE_CODE (t) != SIZEOF_EXPR)
2428 continue;
2429 if (SIZEOF_EXPR_TYPE_P (t))
2430 sizeof_arg[i] = TREE_TYPE (TREE_OPERAND (t, 0));
2431 else
2432 sizeof_arg[i] = TREE_OPERAND (t, 0);
2433 sizeof_arg_loc[i] = EXPR_LOCATION (t);
2435 sizeof_pointer_memaccess_warning
2436 (sizeof_arg_loc, fn, *args,
2437 sizeof_arg, same_type_ignoring_top_level_qualifiers_p);
2440 /* A call to a namespace-scope function. */
2441 result = build_new_function_call (fn, args, koenig_p, complain);
2444 else if (TREE_CODE (fn) == PSEUDO_DTOR_EXPR)
2446 if (!vec_safe_is_empty (*args))
2447 error ("arguments to destructor are not allowed");
2448 /* Mark the pseudo-destructor call as having side-effects so
2449 that we do not issue warnings about its use. */
2450 result = build1 (NOP_EXPR,
2451 void_type_node,
2452 TREE_OPERAND (fn, 0));
2453 TREE_SIDE_EFFECTS (result) = 1;
2455 else if (CLASS_TYPE_P (TREE_TYPE (fn)))
2456 /* If the "function" is really an object of class type, it might
2457 have an overloaded `operator ()'. */
2458 result = build_op_call (fn, args, complain);
2460 if (!result)
2461 /* A call where the function is unknown. */
2462 result = cp_build_function_call_vec (fn, args, complain);
2464 if (processing_template_decl && result != error_mark_node)
2466 if (INDIRECT_REF_P (result))
2467 result = TREE_OPERAND (result, 0);
2468 result = build_call_vec (TREE_TYPE (result), orig_fn, orig_args);
2469 SET_EXPR_LOCATION (result, input_location);
2470 KOENIG_LOOKUP_P (result) = koenig_p;
2471 release_tree_vector (orig_args);
2472 result = convert_from_reference (result);
2475 if (koenig_p)
2477 /* Free garbage OVERLOADs from arg-dependent lookup. */
2478 tree next = NULL_TREE;
2479 for (fn = orig_fn;
2480 fn && TREE_CODE (fn) == OVERLOAD && OVL_ARG_DEPENDENT (fn);
2481 fn = next)
2483 if (processing_template_decl)
2484 /* In a template, we'll re-use them at instantiation time. */
2485 OVL_ARG_DEPENDENT (fn) = false;
2486 else
2488 next = OVL_CHAIN (fn);
2489 ggc_free (fn);
2494 return result;
2497 /* Finish a call to a postfix increment or decrement or EXPR. (Which
2498 is indicated by CODE, which should be POSTINCREMENT_EXPR or
2499 POSTDECREMENT_EXPR.) */
2501 cp_expr
2502 finish_increment_expr (cp_expr expr, enum tree_code code)
2504 /* input_location holds the location of the trailing operator token.
2505 Build a location of the form:
2506 expr++
2507 ~~~~^~
2508 with the caret at the operator token, ranging from the start
2509 of EXPR to the end of the operator token. */
2510 location_t combined_loc = make_location (input_location,
2511 expr.get_start (),
2512 get_finish (input_location));
2513 cp_expr result = build_x_unary_op (combined_loc, code, expr,
2514 tf_warning_or_error);
2515 /* TODO: build_x_unary_op doesn't honor the location, so set it here. */
2516 result.set_location (combined_loc);
2517 return result;
2520 /* Finish a use of `this'. Returns an expression for `this'. */
2522 tree
2523 finish_this_expr (void)
2525 tree result = NULL_TREE;
2527 if (current_class_ptr)
2529 tree type = TREE_TYPE (current_class_ref);
2531 /* In a lambda expression, 'this' refers to the captured 'this'. */
2532 if (LAMBDA_TYPE_P (type))
2533 result = lambda_expr_this_capture (CLASSTYPE_LAMBDA_EXPR (type), true);
2534 else
2535 result = current_class_ptr;
2538 if (result)
2539 /* The keyword 'this' is a prvalue expression. */
2540 return rvalue (result);
2542 tree fn = current_nonlambda_function ();
2543 if (fn && DECL_STATIC_FUNCTION_P (fn))
2544 error ("%<this%> is unavailable for static member functions");
2545 else if (fn)
2546 error ("invalid use of %<this%> in non-member function");
2547 else
2548 error ("invalid use of %<this%> at top level");
2549 return error_mark_node;
2552 /* Finish a pseudo-destructor expression. If SCOPE is NULL, the
2553 expression was of the form `OBJECT.~DESTRUCTOR' where DESTRUCTOR is
2554 the TYPE for the type given. If SCOPE is non-NULL, the expression
2555 was of the form `OBJECT.SCOPE::~DESTRUCTOR'. */
2557 tree
2558 finish_pseudo_destructor_expr (tree object, tree scope, tree destructor,
2559 location_t loc)
2561 if (object == error_mark_node || destructor == error_mark_node)
2562 return error_mark_node;
2564 gcc_assert (TYPE_P (destructor));
2566 if (!processing_template_decl)
2568 if (scope == error_mark_node)
2570 error_at (loc, "invalid qualifying scope in pseudo-destructor name");
2571 return error_mark_node;
2573 if (is_auto (destructor))
2574 destructor = TREE_TYPE (object);
2575 if (scope && TYPE_P (scope) && !check_dtor_name (scope, destructor))
2577 error_at (loc,
2578 "qualified type %qT does not match destructor name ~%qT",
2579 scope, destructor);
2580 return error_mark_node;
2584 /* [expr.pseudo] says both:
2586 The type designated by the pseudo-destructor-name shall be
2587 the same as the object type.
2589 and:
2591 The cv-unqualified versions of the object type and of the
2592 type designated by the pseudo-destructor-name shall be the
2593 same type.
2595 We implement the more generous second sentence, since that is
2596 what most other compilers do. */
2597 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (object),
2598 destructor))
2600 error_at (loc, "%qE is not of type %qT", object, destructor);
2601 return error_mark_node;
2605 return build3_loc (loc, PSEUDO_DTOR_EXPR, void_type_node, object,
2606 scope, destructor);
2609 /* Finish an expression of the form CODE EXPR. */
2611 cp_expr
2612 finish_unary_op_expr (location_t op_loc, enum tree_code code, cp_expr expr,
2613 tsubst_flags_t complain)
2615 /* Build a location of the form:
2616 ++expr
2617 ^~~~~~
2618 with the caret at the operator token, ranging from the start
2619 of the operator token to the end of EXPR. */
2620 location_t combined_loc = make_location (op_loc,
2621 op_loc, expr.get_finish ());
2622 cp_expr result = build_x_unary_op (combined_loc, code, expr, complain);
2623 /* TODO: build_x_unary_op doesn't always honor the location. */
2624 result.set_location (combined_loc);
2626 tree result_ovl, expr_ovl;
2628 if (!(complain & tf_warning))
2629 return result;
2631 result_ovl = result;
2632 expr_ovl = expr;
2634 if (!processing_template_decl)
2635 expr_ovl = cp_fully_fold (expr_ovl);
2637 if (!CONSTANT_CLASS_P (expr_ovl)
2638 || TREE_OVERFLOW_P (expr_ovl))
2639 return result;
2641 if (!processing_template_decl)
2642 result_ovl = cp_fully_fold (result_ovl);
2644 if (CONSTANT_CLASS_P (result_ovl) && TREE_OVERFLOW_P (result_ovl))
2645 overflow_warning (combined_loc, result_ovl);
2647 return result;
2650 /* Finish a compound-literal expression. TYPE is the type to which
2651 the CONSTRUCTOR in COMPOUND_LITERAL is being cast. */
2653 tree
2654 finish_compound_literal (tree type, tree compound_literal,
2655 tsubst_flags_t complain)
2657 if (type == error_mark_node)
2658 return error_mark_node;
2660 if (TREE_CODE (type) == REFERENCE_TYPE)
2662 compound_literal
2663 = finish_compound_literal (TREE_TYPE (type), compound_literal,
2664 complain);
2665 return cp_build_c_cast (type, compound_literal, complain);
2668 if (!TYPE_OBJ_P (type))
2670 if (complain & tf_error)
2671 error ("compound literal of non-object type %qT", type);
2672 return error_mark_node;
2675 if (tree anode = type_uses_auto (type))
2676 if (CLASS_PLACEHOLDER_TEMPLATE (anode))
2677 type = do_auto_deduction (type, compound_literal, anode, complain,
2678 adc_variable_type);
2680 if (processing_template_decl)
2682 TREE_TYPE (compound_literal) = type;
2683 /* Mark the expression as a compound literal. */
2684 TREE_HAS_CONSTRUCTOR (compound_literal) = 1;
2685 return compound_literal;
2688 type = complete_type (type);
2690 if (TYPE_NON_AGGREGATE_CLASS (type))
2692 /* Trying to deal with a CONSTRUCTOR instead of a TREE_LIST
2693 everywhere that deals with function arguments would be a pain, so
2694 just wrap it in a TREE_LIST. The parser set a flag so we know
2695 that it came from T{} rather than T({}). */
2696 CONSTRUCTOR_IS_DIRECT_INIT (compound_literal) = 1;
2697 compound_literal = build_tree_list (NULL_TREE, compound_literal);
2698 return build_functional_cast (type, compound_literal, complain);
2701 if (TREE_CODE (type) == ARRAY_TYPE
2702 && check_array_initializer (NULL_TREE, type, compound_literal))
2703 return error_mark_node;
2704 compound_literal = reshape_init (type, compound_literal, complain);
2705 if (SCALAR_TYPE_P (type)
2706 && !BRACE_ENCLOSED_INITIALIZER_P (compound_literal)
2707 && !check_narrowing (type, compound_literal, complain))
2708 return error_mark_node;
2709 if (TREE_CODE (type) == ARRAY_TYPE
2710 && TYPE_DOMAIN (type) == NULL_TREE)
2712 cp_complete_array_type_or_error (&type, compound_literal,
2713 false, complain);
2714 if (type == error_mark_node)
2715 return error_mark_node;
2717 compound_literal = digest_init_flags (type, compound_literal, LOOKUP_NORMAL,
2718 complain);
2719 if (TREE_CODE (compound_literal) == CONSTRUCTOR)
2720 TREE_HAS_CONSTRUCTOR (compound_literal) = true;
2722 /* Put static/constant array temporaries in static variables. */
2723 if ((!at_function_scope_p () || CP_TYPE_CONST_P (type))
2724 && TREE_CODE (type) == ARRAY_TYPE
2725 && !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
2726 && initializer_constant_valid_p (compound_literal, type))
2728 tree decl = create_temporary_var (type);
2729 DECL_INITIAL (decl) = compound_literal;
2730 TREE_STATIC (decl) = 1;
2731 if (literal_type_p (type) && CP_TYPE_CONST_NON_VOLATILE_P (type))
2733 /* 5.19 says that a constant expression can include an
2734 lvalue-rvalue conversion applied to "a glvalue of literal type
2735 that refers to a non-volatile temporary object initialized
2736 with a constant expression". Rather than try to communicate
2737 that this VAR_DECL is a temporary, just mark it constexpr. */
2738 DECL_DECLARED_CONSTEXPR_P (decl) = true;
2739 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
2740 TREE_CONSTANT (decl) = true;
2742 cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
2743 decl = pushdecl_top_level (decl);
2744 DECL_NAME (decl) = make_anon_name ();
2745 SET_DECL_ASSEMBLER_NAME (decl, DECL_NAME (decl));
2746 /* Make sure the destructor is callable. */
2747 tree clean = cxx_maybe_build_cleanup (decl, complain);
2748 if (clean == error_mark_node)
2749 return error_mark_node;
2750 return decl;
2753 /* Represent other compound literals with TARGET_EXPR so we produce
2754 an lvalue, but can elide copies. */
2755 if (!VECTOR_TYPE_P (type))
2756 compound_literal = get_target_expr_sfinae (compound_literal, complain);
2758 return compound_literal;
2761 /* Return the declaration for the function-name variable indicated by
2762 ID. */
2764 tree
2765 finish_fname (tree id)
2767 tree decl;
2769 decl = fname_decl (input_location, C_RID_CODE (id), id);
2770 if (processing_template_decl && current_function_decl
2771 && decl != error_mark_node)
2772 decl = DECL_NAME (decl);
2773 return decl;
2776 /* Finish a translation unit. */
2778 void
2779 finish_translation_unit (void)
2781 /* In case there were missing closebraces,
2782 get us back to the global binding level. */
2783 pop_everything ();
2784 while (current_namespace != global_namespace)
2785 pop_namespace ();
2787 /* Do file scope __FUNCTION__ et al. */
2788 finish_fname_decls ();
2791 /* Finish a template type parameter, specified as AGGR IDENTIFIER.
2792 Returns the parameter. */
2794 tree
2795 finish_template_type_parm (tree aggr, tree identifier)
2797 if (aggr != class_type_node)
2799 permerror (input_location, "template type parameters must use the keyword %<class%> or %<typename%>");
2800 aggr = class_type_node;
2803 return build_tree_list (aggr, identifier);
2806 /* Finish a template template parameter, specified as AGGR IDENTIFIER.
2807 Returns the parameter. */
2809 tree
2810 finish_template_template_parm (tree aggr, tree identifier)
2812 tree decl = build_decl (input_location,
2813 TYPE_DECL, identifier, NULL_TREE);
2815 tree tmpl = build_lang_decl (TEMPLATE_DECL, identifier, NULL_TREE);
2816 DECL_TEMPLATE_PARMS (tmpl) = current_template_parms;
2817 DECL_TEMPLATE_RESULT (tmpl) = decl;
2818 DECL_ARTIFICIAL (decl) = 1;
2820 // Associate the constraints with the underlying declaration,
2821 // not the template.
2822 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
2823 tree constr = build_constraints (reqs, NULL_TREE);
2824 set_constraints (decl, constr);
2826 end_template_decl ();
2828 gcc_assert (DECL_TEMPLATE_PARMS (tmpl));
2830 check_default_tmpl_args (decl, DECL_TEMPLATE_PARMS (tmpl),
2831 /*is_primary=*/true, /*is_partial=*/false,
2832 /*is_friend=*/0);
2834 return finish_template_type_parm (aggr, tmpl);
2837 /* ARGUMENT is the default-argument value for a template template
2838 parameter. If ARGUMENT is invalid, issue error messages and return
2839 the ERROR_MARK_NODE. Otherwise, ARGUMENT itself is returned. */
2841 tree
2842 check_template_template_default_arg (tree argument)
2844 if (TREE_CODE (argument) != TEMPLATE_DECL
2845 && TREE_CODE (argument) != TEMPLATE_TEMPLATE_PARM
2846 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
2848 if (TREE_CODE (argument) == TYPE_DECL)
2849 error ("invalid use of type %qT as a default value for a template "
2850 "template-parameter", TREE_TYPE (argument));
2851 else
2852 error ("invalid default argument for a template template parameter");
2853 return error_mark_node;
2856 return argument;
2859 /* Begin a class definition, as indicated by T. */
2861 tree
2862 begin_class_definition (tree t)
2864 if (error_operand_p (t) || error_operand_p (TYPE_MAIN_DECL (t)))
2865 return error_mark_node;
2867 if (processing_template_parmlist)
2869 error ("definition of %q#T inside template parameter list", t);
2870 return error_mark_node;
2873 /* According to the C++ ABI, decimal classes defined in ISO/IEC TR 24733
2874 are passed the same as decimal scalar types. */
2875 if (TREE_CODE (t) == RECORD_TYPE
2876 && !processing_template_decl)
2878 tree ns = TYPE_CONTEXT (t);
2879 if (ns && TREE_CODE (ns) == NAMESPACE_DECL
2880 && DECL_CONTEXT (ns) == std_node
2881 && DECL_NAME (ns)
2882 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (ns)), "decimal"))
2884 const char *n = TYPE_NAME_STRING (t);
2885 if ((strcmp (n, "decimal32") == 0)
2886 || (strcmp (n, "decimal64") == 0)
2887 || (strcmp (n, "decimal128") == 0))
2888 TYPE_TRANSPARENT_AGGR (t) = 1;
2892 /* A non-implicit typename comes from code like:
2894 template <typename T> struct A {
2895 template <typename U> struct A<T>::B ...
2897 This is erroneous. */
2898 else if (TREE_CODE (t) == TYPENAME_TYPE)
2900 error ("invalid definition of qualified type %qT", t);
2901 t = error_mark_node;
2904 if (t == error_mark_node || ! MAYBE_CLASS_TYPE_P (t))
2906 t = make_class_type (RECORD_TYPE);
2907 pushtag (make_anon_name (), t, /*tag_scope=*/ts_current);
2910 if (TYPE_BEING_DEFINED (t))
2912 t = make_class_type (TREE_CODE (t));
2913 pushtag (TYPE_IDENTIFIER (t), t, /*tag_scope=*/ts_current);
2915 maybe_process_partial_specialization (t);
2916 pushclass (t);
2917 TYPE_BEING_DEFINED (t) = 1;
2918 class_binding_level->defining_class_p = 1;
2920 if (flag_pack_struct)
2922 tree v;
2923 TYPE_PACKED (t) = 1;
2924 /* Even though the type is being defined for the first time
2925 here, there might have been a forward declaration, so there
2926 might be cv-qualified variants of T. */
2927 for (v = TYPE_NEXT_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v))
2928 TYPE_PACKED (v) = 1;
2930 /* Reset the interface data, at the earliest possible
2931 moment, as it might have been set via a class foo;
2932 before. */
2933 if (! TYPE_UNNAMED_P (t))
2935 struct c_fileinfo *finfo = \
2936 get_fileinfo (LOCATION_FILE (input_location));
2937 CLASSTYPE_INTERFACE_ONLY (t) = finfo->interface_only;
2938 SET_CLASSTYPE_INTERFACE_UNKNOWN_X
2939 (t, finfo->interface_unknown);
2941 reset_specialization();
2943 /* Make a declaration for this class in its own scope. */
2944 build_self_reference ();
2946 return t;
2949 /* Finish the member declaration given by DECL. */
2951 void
2952 finish_member_declaration (tree decl)
2954 if (decl == error_mark_node || decl == NULL_TREE)
2955 return;
2957 if (decl == void_type_node)
2958 /* The COMPONENT was a friend, not a member, and so there's
2959 nothing for us to do. */
2960 return;
2962 /* We should see only one DECL at a time. */
2963 gcc_assert (DECL_CHAIN (decl) == NULL_TREE);
2965 /* Don't add decls after definition. */
2966 gcc_assert (TYPE_BEING_DEFINED (current_class_type)
2967 /* We can add lambda types when late parsing default
2968 arguments. */
2969 || LAMBDA_TYPE_P (TREE_TYPE (decl)));
2971 /* Set up access control for DECL. */
2972 TREE_PRIVATE (decl)
2973 = (current_access_specifier == access_private_node);
2974 TREE_PROTECTED (decl)
2975 = (current_access_specifier == access_protected_node);
2976 if (TREE_CODE (decl) == TEMPLATE_DECL)
2978 TREE_PRIVATE (DECL_TEMPLATE_RESULT (decl)) = TREE_PRIVATE (decl);
2979 TREE_PROTECTED (DECL_TEMPLATE_RESULT (decl)) = TREE_PROTECTED (decl);
2982 /* Mark the DECL as a member of the current class, unless it's
2983 a member of an enumeration. */
2984 if (TREE_CODE (decl) != CONST_DECL)
2985 DECL_CONTEXT (decl) = current_class_type;
2987 /* Check for bare parameter packs in the member variable declaration. */
2988 if (TREE_CODE (decl) == FIELD_DECL)
2990 if (check_for_bare_parameter_packs (TREE_TYPE (decl)))
2991 TREE_TYPE (decl) = error_mark_node;
2992 if (check_for_bare_parameter_packs (DECL_ATTRIBUTES (decl)))
2993 DECL_ATTRIBUTES (decl) = NULL_TREE;
2996 /* [dcl.link]
2998 A C language linkage is ignored for the names of class members
2999 and the member function type of class member functions. */
3000 if (DECL_LANG_SPECIFIC (decl) && DECL_LANGUAGE (decl) == lang_c)
3001 SET_DECL_LANGUAGE (decl, lang_cplusplus);
3003 /* Put functions on the TYPE_METHODS list and everything else on the
3004 TYPE_FIELDS list. Note that these are built up in reverse order.
3005 We reverse them (to obtain declaration order) in finish_struct. */
3006 if (DECL_DECLARES_FUNCTION_P (decl))
3008 /* We also need to add this function to the
3009 CLASSTYPE_METHOD_VEC. */
3010 if (add_method (current_class_type, decl, NULL_TREE))
3012 gcc_assert (TYPE_MAIN_VARIANT (current_class_type) == current_class_type);
3013 DECL_CHAIN (decl) = TYPE_METHODS (current_class_type);
3014 TYPE_METHODS (current_class_type) = decl;
3016 maybe_add_class_template_decl_list (current_class_type, decl,
3017 /*friend_p=*/0);
3020 /* Enter the DECL into the scope of the class, if the class
3021 isn't a closure (whose fields are supposed to be unnamed). */
3022 else if (CLASSTYPE_LAMBDA_EXPR (current_class_type)
3023 || pushdecl_class_level (decl))
3025 if (TREE_CODE (decl) == USING_DECL)
3027 /* For now, ignore class-scope USING_DECLS, so that
3028 debugging backends do not see them. */
3029 DECL_IGNORED_P (decl) = 1;
3032 /* All TYPE_DECLs go at the end of TYPE_FIELDS. Ordinary fields
3033 go at the beginning. The reason is that lookup_field_1
3034 searches the list in order, and we want a field name to
3035 override a type name so that the "struct stat hack" will
3036 work. In particular:
3038 struct S { enum E { }; int E } s;
3039 s.E = 3;
3041 is valid. In addition, the FIELD_DECLs must be maintained in
3042 declaration order so that class layout works as expected.
3043 However, we don't need that order until class layout, so we
3044 save a little time by putting FIELD_DECLs on in reverse order
3045 here, and then reversing them in finish_struct_1. (We could
3046 also keep a pointer to the correct insertion points in the
3047 list.) */
3049 if (TREE_CODE (decl) == TYPE_DECL)
3050 TYPE_FIELDS (current_class_type)
3051 = chainon (TYPE_FIELDS (current_class_type), decl);
3052 else
3054 DECL_CHAIN (decl) = TYPE_FIELDS (current_class_type);
3055 TYPE_FIELDS (current_class_type) = decl;
3058 maybe_add_class_template_decl_list (current_class_type, decl,
3059 /*friend_p=*/0);
3063 /* Finish processing a complete template declaration. The PARMS are
3064 the template parameters. */
3066 void
3067 finish_template_decl (tree parms)
3069 if (parms)
3070 end_template_decl ();
3071 else
3072 end_specialization ();
3075 // Returns the template type of the class scope being entered. If we're
3076 // entering a constrained class scope. TYPE is the class template
3077 // scope being entered and we may need to match the intended type with
3078 // a constrained specialization. For example:
3080 // template<Object T>
3081 // struct S { void f(); }; #1
3083 // template<Object T>
3084 // void S<T>::f() { } #2
3086 // We check, in #2, that S<T> refers precisely to the type declared by
3087 // #1 (i.e., that the constraints match). Note that the following should
3088 // be an error since there is no specialization of S<T> that is
3089 // unconstrained, but this is not diagnosed here.
3091 // template<typename T>
3092 // void S<T>::f() { }
3094 // We cannot diagnose this problem here since this function also matches
3095 // qualified template names that are not part of a definition. For example:
3097 // template<Integral T, Floating_point U>
3098 // typename pair<T, U>::first_type void f(T, U);
3100 // Here, it is unlikely that there is a partial specialization of
3101 // pair constrained for for Integral and Floating_point arguments.
3103 // The general rule is: if a constrained specialization with matching
3104 // constraints is found return that type. Also note that if TYPE is not a
3105 // class-type (e.g. a typename type), then no fixup is needed.
3107 static tree
3108 fixup_template_type (tree type)
3110 // Find the template parameter list at the a depth appropriate to
3111 // the scope we're trying to enter.
3112 tree parms = current_template_parms;
3113 int depth = template_class_depth (type);
3114 for (int n = processing_template_decl; n > depth && parms; --n)
3115 parms = TREE_CHAIN (parms);
3116 if (!parms)
3117 return type;
3118 tree cur_reqs = TEMPLATE_PARMS_CONSTRAINTS (parms);
3119 tree cur_constr = build_constraints (cur_reqs, NULL_TREE);
3121 // Search for a specialization whose type and constraints match.
3122 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
3123 tree specs = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
3124 while (specs)
3126 tree spec_constr = get_constraints (TREE_VALUE (specs));
3128 // If the type and constraints match a specialization, then we
3129 // are entering that type.
3130 if (same_type_p (type, TREE_TYPE (specs))
3131 && equivalent_constraints (cur_constr, spec_constr))
3132 return TREE_TYPE (specs);
3133 specs = TREE_CHAIN (specs);
3136 // If no specialization matches, then must return the type
3137 // previously found.
3138 return type;
3141 /* Finish processing a template-id (which names a type) of the form
3142 NAME < ARGS >. Return the TYPE_DECL for the type named by the
3143 template-id. If ENTERING_SCOPE is nonzero we are about to enter
3144 the scope of template-id indicated. */
3146 tree
3147 finish_template_type (tree name, tree args, int entering_scope)
3149 tree type;
3151 type = lookup_template_class (name, args,
3152 NULL_TREE, NULL_TREE, entering_scope,
3153 tf_warning_or_error | tf_user);
3155 /* If we might be entering the scope of a partial specialization,
3156 find the one with the right constraints. */
3157 if (flag_concepts
3158 && entering_scope
3159 && CLASS_TYPE_P (type)
3160 && CLASSTYPE_TEMPLATE_INFO (type)
3161 && dependent_type_p (type)
3162 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
3163 type = fixup_template_type (type);
3165 if (type == error_mark_node)
3166 return type;
3167 else if (CLASS_TYPE_P (type) && !alias_type_or_template_p (type))
3168 return TYPE_STUB_DECL (type);
3169 else
3170 return TYPE_NAME (type);
3173 /* Finish processing a BASE_CLASS with the indicated ACCESS_SPECIFIER.
3174 Return a TREE_LIST containing the ACCESS_SPECIFIER and the
3175 BASE_CLASS, or NULL_TREE if an error occurred. The
3176 ACCESS_SPECIFIER is one of
3177 access_{default,public,protected_private}_node. For a virtual base
3178 we set TREE_TYPE. */
3180 tree
3181 finish_base_specifier (tree base, tree access, bool virtual_p)
3183 tree result;
3185 if (base == error_mark_node)
3187 error ("invalid base-class specification");
3188 result = NULL_TREE;
3190 else if (! MAYBE_CLASS_TYPE_P (base))
3192 error ("%qT is not a class type", base);
3193 result = NULL_TREE;
3195 else
3197 if (cp_type_quals (base) != 0)
3199 /* DR 484: Can a base-specifier name a cv-qualified
3200 class type? */
3201 base = TYPE_MAIN_VARIANT (base);
3203 result = build_tree_list (access, base);
3204 if (virtual_p)
3205 TREE_TYPE (result) = integer_type_node;
3208 return result;
3211 /* If FNS is a member function, a set of member functions, or a
3212 template-id referring to one or more member functions, return a
3213 BASELINK for FNS, incorporating the current access context.
3214 Otherwise, return FNS unchanged. */
3216 tree
3217 baselink_for_fns (tree fns)
3219 tree scope;
3220 tree cl;
3222 if (BASELINK_P (fns)
3223 || error_operand_p (fns))
3224 return fns;
3226 scope = ovl_scope (fns);
3227 if (!CLASS_TYPE_P (scope))
3228 return fns;
3230 cl = currently_open_derived_class (scope);
3231 if (!cl)
3232 cl = scope;
3233 cl = TYPE_BINFO (cl);
3234 return build_baselink (cl, cl, fns, /*optype=*/NULL_TREE);
3237 /* Returns true iff DECL is a variable from a function outside
3238 the current one. */
3240 static bool
3241 outer_var_p (tree decl)
3243 return ((VAR_P (decl) || TREE_CODE (decl) == PARM_DECL)
3244 && DECL_FUNCTION_SCOPE_P (decl)
3245 && (DECL_CONTEXT (decl) != current_function_decl
3246 || parsing_nsdmi ()));
3249 /* As above, but also checks that DECL is automatic. */
3251 bool
3252 outer_automatic_var_p (tree decl)
3254 return (outer_var_p (decl)
3255 && !TREE_STATIC (decl));
3258 /* DECL satisfies outer_automatic_var_p. Possibly complain about it or
3259 rewrite it for lambda capture. */
3261 tree
3262 process_outer_var_ref (tree decl, tsubst_flags_t complain)
3264 if (cp_unevaluated_operand)
3265 /* It's not a use (3.2) if we're in an unevaluated context. */
3266 return decl;
3267 if (decl == error_mark_node)
3268 return decl;
3270 tree context = DECL_CONTEXT (decl);
3271 tree containing_function = current_function_decl;
3272 tree lambda_stack = NULL_TREE;
3273 tree lambda_expr = NULL_TREE;
3274 tree initializer = convert_from_reference (decl);
3276 /* Mark it as used now even if the use is ill-formed. */
3277 if (!mark_used (decl, complain))
3278 return error_mark_node;
3280 bool saw_generic_lambda = false;
3281 if (parsing_nsdmi ())
3282 containing_function = NULL_TREE;
3283 else
3284 /* If we are in a lambda function, we can move out until we hit
3285 1. the context,
3286 2. a non-lambda function, or
3287 3. a non-default capturing lambda function. */
3288 while (context != containing_function
3289 /* containing_function can be null with invalid generic lambdas. */
3290 && containing_function
3291 && LAMBDA_FUNCTION_P (containing_function))
3293 tree closure = DECL_CONTEXT (containing_function);
3294 lambda_expr = CLASSTYPE_LAMBDA_EXPR (closure);
3296 if (generic_lambda_fn_p (containing_function))
3297 saw_generic_lambda = true;
3299 if (TYPE_CLASS_SCOPE_P (closure))
3300 /* A lambda in an NSDMI (c++/64496). */
3301 break;
3303 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr)
3304 == CPLD_NONE)
3305 break;
3307 lambda_stack = tree_cons (NULL_TREE,
3308 lambda_expr,
3309 lambda_stack);
3311 containing_function
3312 = decl_function_context (containing_function);
3315 /* Core issue 696: "[At the July 2009 meeting] the CWG expressed
3316 support for an approach in which a reference to a local
3317 [constant] automatic variable in a nested class or lambda body
3318 would enter the expression as an rvalue, which would reduce
3319 the complexity of the problem"
3321 FIXME update for final resolution of core issue 696. */
3322 if (decl_maybe_constant_var_p (decl))
3324 if (processing_template_decl && !saw_generic_lambda)
3325 /* In a non-generic lambda within a template, wait until instantiation
3326 time to decide whether to capture. For a generic lambda, we can't
3327 wait until we instantiate the op() because the closure class is
3328 already defined at that point. FIXME to get the semantics exactly
3329 right we need to partially-instantiate the lambda body so the only
3330 dependencies left are on the generic parameters themselves. This
3331 probably means moving away from our current model of lambdas in
3332 templates (instantiating the closure type) to one based on creating
3333 the closure type when instantiating the lambda context. That is
3334 probably also the way to handle lambdas within pack expansions. */
3335 return decl;
3336 else if (decl_constant_var_p (decl))
3338 tree t = maybe_constant_value (convert_from_reference (decl));
3339 if (TREE_CONSTANT (t))
3340 return t;
3344 if (lambda_expr && VAR_P (decl)
3345 && DECL_ANON_UNION_VAR_P (decl))
3347 if (complain & tf_error)
3348 error ("cannot capture member %qD of anonymous union", decl);
3349 return error_mark_node;
3351 if (context == containing_function)
3353 decl = add_default_capture (lambda_stack,
3354 /*id=*/DECL_NAME (decl),
3355 initializer);
3357 else if (lambda_expr)
3359 if (complain & tf_error)
3361 error ("%qD is not captured", decl);
3362 tree closure = LAMBDA_EXPR_CLOSURE (lambda_expr);
3363 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr)
3364 == CPLD_NONE)
3365 inform (location_of (closure),
3366 "the lambda has no capture-default");
3367 else if (TYPE_CLASS_SCOPE_P (closure))
3368 inform (0, "lambda in local class %q+T cannot "
3369 "capture variables from the enclosing context",
3370 TYPE_CONTEXT (closure));
3371 inform (DECL_SOURCE_LOCATION (decl), "%q#D declared here", decl);
3373 return error_mark_node;
3375 else
3377 if (complain & tf_error)
3379 error (VAR_P (decl)
3380 ? G_("use of local variable with automatic storage from "
3381 "containing function")
3382 : G_("use of parameter from containing function"));
3383 inform (DECL_SOURCE_LOCATION (decl), "%q#D declared here", decl);
3385 return error_mark_node;
3387 return decl;
3390 /* ID_EXPRESSION is a representation of parsed, but unprocessed,
3391 id-expression. (See cp_parser_id_expression for details.) SCOPE,
3392 if non-NULL, is the type or namespace used to explicitly qualify
3393 ID_EXPRESSION. DECL is the entity to which that name has been
3394 resolved.
3396 *CONSTANT_EXPRESSION_P is true if we are presently parsing a
3397 constant-expression. In that case, *NON_CONSTANT_EXPRESSION_P will
3398 be set to true if this expression isn't permitted in a
3399 constant-expression, but it is otherwise not set by this function.
3400 *ALLOW_NON_CONSTANT_EXPRESSION_P is true if we are parsing a
3401 constant-expression, but a non-constant expression is also
3402 permissible.
3404 DONE is true if this expression is a complete postfix-expression;
3405 it is false if this expression is followed by '->', '[', '(', etc.
3406 ADDRESS_P is true iff this expression is the operand of '&'.
3407 TEMPLATE_P is true iff the qualified-id was of the form
3408 "A::template B". TEMPLATE_ARG_P is true iff this qualified name
3409 appears as a template argument.
3411 If an error occurs, and it is the kind of error that might cause
3412 the parser to abort a tentative parse, *ERROR_MSG is filled in. It
3413 is the caller's responsibility to issue the message. *ERROR_MSG
3414 will be a string with static storage duration, so the caller need
3415 not "free" it.
3417 Return an expression for the entity, after issuing appropriate
3418 diagnostics. This function is also responsible for transforming a
3419 reference to a non-static member into a COMPONENT_REF that makes
3420 the use of "this" explicit.
3422 Upon return, *IDK will be filled in appropriately. */
3423 cp_expr
3424 finish_id_expression (tree id_expression,
3425 tree decl,
3426 tree scope,
3427 cp_id_kind *idk,
3428 bool integral_constant_expression_p,
3429 bool allow_non_integral_constant_expression_p,
3430 bool *non_integral_constant_expression_p,
3431 bool template_p,
3432 bool done,
3433 bool address_p,
3434 bool template_arg_p,
3435 const char **error_msg,
3436 location_t location)
3438 decl = strip_using_decl (decl);
3440 /* Initialize the output parameters. */
3441 *idk = CP_ID_KIND_NONE;
3442 *error_msg = NULL;
3444 if (id_expression == error_mark_node)
3445 return error_mark_node;
3446 /* If we have a template-id, then no further lookup is
3447 required. If the template-id was for a template-class, we
3448 will sometimes have a TYPE_DECL at this point. */
3449 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3450 || TREE_CODE (decl) == TYPE_DECL)
3452 /* Look up the name. */
3453 else
3455 if (decl == error_mark_node)
3457 /* Name lookup failed. */
3458 if (scope
3459 && (!TYPE_P (scope)
3460 || (!dependent_type_p (scope)
3461 && !(identifier_p (id_expression)
3462 && IDENTIFIER_TYPENAME_P (id_expression)
3463 && dependent_type_p (TREE_TYPE (id_expression))))))
3465 /* If the qualifying type is non-dependent (and the name
3466 does not name a conversion operator to a dependent
3467 type), issue an error. */
3468 qualified_name_lookup_error (scope, id_expression, decl, location);
3469 return error_mark_node;
3471 else if (!scope)
3473 /* It may be resolved via Koenig lookup. */
3474 *idk = CP_ID_KIND_UNQUALIFIED;
3475 return id_expression;
3477 else
3478 decl = id_expression;
3480 /* If DECL is a variable that would be out of scope under
3481 ANSI/ISO rules, but in scope in the ARM, name lookup
3482 will succeed. Issue a diagnostic here. */
3483 else
3484 decl = check_for_out_of_scope_variable (decl);
3486 /* Remember that the name was used in the definition of
3487 the current class so that we can check later to see if
3488 the meaning would have been different after the class
3489 was entirely defined. */
3490 if (!scope && decl != error_mark_node && identifier_p (id_expression))
3491 maybe_note_name_used_in_class (id_expression, decl);
3493 /* A use in unevaluated operand might not be instantiated appropriately
3494 if tsubst_copy builds a dummy parm, or if we never instantiate a
3495 generic lambda, so mark it now. */
3496 if (processing_template_decl && cp_unevaluated_operand)
3497 mark_type_use (decl);
3499 /* Disallow uses of local variables from containing functions, except
3500 within lambda-expressions. */
3501 if (outer_automatic_var_p (decl))
3503 decl = process_outer_var_ref (decl, tf_warning_or_error);
3504 if (decl == error_mark_node)
3505 return error_mark_node;
3508 /* Also disallow uses of function parameters outside the function
3509 body, except inside an unevaluated context (i.e. decltype). */
3510 if (TREE_CODE (decl) == PARM_DECL
3511 && DECL_CONTEXT (decl) == NULL_TREE
3512 && !cp_unevaluated_operand)
3514 *error_msg = G_("use of parameter outside function body");
3515 return error_mark_node;
3519 /* If we didn't find anything, or what we found was a type,
3520 then this wasn't really an id-expression. */
3521 if (TREE_CODE (decl) == TEMPLATE_DECL
3522 && !DECL_FUNCTION_TEMPLATE_P (decl))
3524 *error_msg = G_("missing template arguments");
3525 return error_mark_node;
3527 else if (TREE_CODE (decl) == TYPE_DECL
3528 || TREE_CODE (decl) == NAMESPACE_DECL)
3530 *error_msg = G_("expected primary-expression");
3531 return error_mark_node;
3534 /* If the name resolved to a template parameter, there is no
3535 need to look it up again later. */
3536 if ((TREE_CODE (decl) == CONST_DECL && DECL_TEMPLATE_PARM_P (decl))
3537 || TREE_CODE (decl) == TEMPLATE_PARM_INDEX)
3539 tree r;
3541 *idk = CP_ID_KIND_NONE;
3542 if (TREE_CODE (decl) == TEMPLATE_PARM_INDEX)
3543 decl = TEMPLATE_PARM_DECL (decl);
3544 r = convert_from_reference (DECL_INITIAL (decl));
3546 if (integral_constant_expression_p
3547 && !dependent_type_p (TREE_TYPE (decl))
3548 && !(INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (r))))
3550 if (!allow_non_integral_constant_expression_p)
3551 error ("template parameter %qD of type %qT is not allowed in "
3552 "an integral constant expression because it is not of "
3553 "integral or enumeration type", decl, TREE_TYPE (decl));
3554 *non_integral_constant_expression_p = true;
3556 return r;
3558 else
3560 bool dependent_p = type_dependent_expression_p (decl);
3562 /* If the declaration was explicitly qualified indicate
3563 that. The semantics of `A::f(3)' are different than
3564 `f(3)' if `f' is virtual. */
3565 *idk = (scope
3566 ? CP_ID_KIND_QUALIFIED
3567 : (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3568 ? CP_ID_KIND_TEMPLATE_ID
3569 : (dependent_p
3570 ? CP_ID_KIND_UNQUALIFIED_DEPENDENT
3571 : CP_ID_KIND_UNQUALIFIED)));
3573 /* If the name was dependent on a template parameter and we're not in a
3574 default capturing generic lambda within a template, we will resolve the
3575 name at instantiation time. FIXME: For lambdas, we should defer
3576 building the closure type until instantiation time then we won't need
3577 the extra test here. */
3578 if (dependent_p
3579 && !parsing_default_capturing_generic_lambda_in_template ())
3581 if (DECL_P (decl)
3582 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (decl)))
3583 /* Dependent type attributes on the decl mean that the TREE_TYPE is
3584 wrong, so just return the identifier. */
3585 return id_expression;
3587 /* If we found a variable, then name lookup during the
3588 instantiation will always resolve to the same VAR_DECL
3589 (or an instantiation thereof). */
3590 if (VAR_P (decl)
3591 || TREE_CODE (decl) == CONST_DECL
3592 || TREE_CODE (decl) == PARM_DECL)
3594 mark_used (decl);
3595 return convert_from_reference (decl);
3598 /* Create a SCOPE_REF for qualified names, if the scope is
3599 dependent. */
3600 if (scope)
3602 if (TYPE_P (scope))
3604 if (address_p && done)
3605 decl = finish_qualified_id_expr (scope, decl,
3606 done, address_p,
3607 template_p,
3608 template_arg_p,
3609 tf_warning_or_error);
3610 else
3612 tree type = NULL_TREE;
3613 if (DECL_P (decl) && !dependent_scope_p (scope))
3614 type = TREE_TYPE (decl);
3615 decl = build_qualified_name (type,
3616 scope,
3617 id_expression,
3618 template_p);
3621 if (TREE_TYPE (decl))
3622 decl = convert_from_reference (decl);
3623 return decl;
3625 /* A TEMPLATE_ID already contains all the information we
3626 need. */
3627 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR)
3628 return id_expression;
3629 /* The same is true for FIELD_DECL, but we also need to
3630 make sure that the syntax is correct. */
3631 else if (TREE_CODE (decl) == FIELD_DECL)
3633 /* Since SCOPE is NULL here, this is an unqualified name.
3634 Access checking has been performed during name lookup
3635 already. Turn off checking to avoid duplicate errors. */
3636 push_deferring_access_checks (dk_no_check);
3637 decl = finish_non_static_data_member
3638 (decl, NULL_TREE,
3639 /*qualifying_scope=*/NULL_TREE);
3640 pop_deferring_access_checks ();
3641 return decl;
3643 return id_expression;
3646 if (TREE_CODE (decl) == NAMESPACE_DECL)
3648 error ("use of namespace %qD as expression", decl);
3649 return error_mark_node;
3651 else if (DECL_CLASS_TEMPLATE_P (decl))
3653 error ("use of class template %qT as expression", decl);
3654 return error_mark_node;
3656 else if (TREE_CODE (decl) == TREE_LIST)
3658 /* Ambiguous reference to base members. */
3659 error ("request for member %qD is ambiguous in "
3660 "multiple inheritance lattice", id_expression);
3661 print_candidates (decl);
3662 return error_mark_node;
3665 /* Mark variable-like entities as used. Functions are similarly
3666 marked either below or after overload resolution. */
3667 if ((VAR_P (decl)
3668 || TREE_CODE (decl) == PARM_DECL
3669 || TREE_CODE (decl) == CONST_DECL
3670 || TREE_CODE (decl) == RESULT_DECL)
3671 && !mark_used (decl))
3672 return error_mark_node;
3674 /* Only certain kinds of names are allowed in constant
3675 expression. Template parameters have already
3676 been handled above. */
3677 if (! error_operand_p (decl)
3678 && integral_constant_expression_p
3679 && ! decl_constant_var_p (decl)
3680 && TREE_CODE (decl) != CONST_DECL
3681 && ! builtin_valid_in_constant_expr_p (decl))
3683 if (!allow_non_integral_constant_expression_p)
3685 error ("%qD cannot appear in a constant-expression", decl);
3686 return error_mark_node;
3688 *non_integral_constant_expression_p = true;
3691 tree wrap;
3692 if (VAR_P (decl)
3693 && !cp_unevaluated_operand
3694 && !processing_template_decl
3695 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl))
3696 && CP_DECL_THREAD_LOCAL_P (decl)
3697 && (wrap = get_tls_wrapper_fn (decl)))
3699 /* Replace an evaluated use of the thread_local variable with
3700 a call to its wrapper. */
3701 decl = build_cxx_call (wrap, 0, NULL, tf_warning_or_error);
3703 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3704 && variable_template_p (TREE_OPERAND (decl, 0)))
3706 decl = finish_template_variable (decl);
3707 mark_used (decl);
3708 decl = convert_from_reference (decl);
3710 else if (scope)
3712 decl = (adjust_result_of_qualified_name_lookup
3713 (decl, scope, current_nonlambda_class_type()));
3715 if (TREE_CODE (decl) == FUNCTION_DECL)
3716 mark_used (decl);
3718 if (TYPE_P (scope))
3719 decl = finish_qualified_id_expr (scope,
3720 decl,
3721 done,
3722 address_p,
3723 template_p,
3724 template_arg_p,
3725 tf_warning_or_error);
3726 else
3727 decl = convert_from_reference (decl);
3729 else if (TREE_CODE (decl) == FIELD_DECL)
3731 /* Since SCOPE is NULL here, this is an unqualified name.
3732 Access checking has been performed during name lookup
3733 already. Turn off checking to avoid duplicate errors. */
3734 push_deferring_access_checks (dk_no_check);
3735 decl = finish_non_static_data_member (decl, NULL_TREE,
3736 /*qualifying_scope=*/NULL_TREE);
3737 pop_deferring_access_checks ();
3739 else if (is_overloaded_fn (decl))
3741 tree first_fn;
3743 first_fn = get_first_fn (decl);
3744 if (TREE_CODE (first_fn) == TEMPLATE_DECL)
3745 first_fn = DECL_TEMPLATE_RESULT (first_fn);
3747 /* [basic.def.odr]: "A function whose name appears as a
3748 potentially-evaluated expression is odr-used if it is the unique
3749 lookup result".
3751 But only mark it if it's a complete postfix-expression; in a call,
3752 ADL might select a different function, and we'll call mark_used in
3753 build_over_call. */
3754 if (done
3755 && !really_overloaded_fn (decl)
3756 && !mark_used (first_fn))
3757 return error_mark_node;
3759 if (!template_arg_p
3760 && TREE_CODE (first_fn) == FUNCTION_DECL
3761 && DECL_FUNCTION_MEMBER_P (first_fn)
3762 && !shared_member_p (decl))
3764 /* A set of member functions. */
3765 decl = maybe_dummy_object (DECL_CONTEXT (first_fn), 0);
3766 return finish_class_member_access_expr (decl, id_expression,
3767 /*template_p=*/false,
3768 tf_warning_or_error);
3771 decl = baselink_for_fns (decl);
3773 else
3775 if (DECL_P (decl) && DECL_NONLOCAL (decl)
3776 && DECL_CLASS_SCOPE_P (decl))
3778 tree context = context_for_name_lookup (decl);
3779 if (context != current_class_type)
3781 tree path = currently_open_derived_class (context);
3782 perform_or_defer_access_check (TYPE_BINFO (path),
3783 decl, decl,
3784 tf_warning_or_error);
3788 decl = convert_from_reference (decl);
3792 return cp_expr (decl, location);
3795 /* Implement the __typeof keyword: Return the type of EXPR, suitable for
3796 use as a type-specifier. */
3798 tree
3799 finish_typeof (tree expr)
3801 tree type;
3803 if (type_dependent_expression_p (expr))
3805 type = cxx_make_type (TYPEOF_TYPE);
3806 TYPEOF_TYPE_EXPR (type) = expr;
3807 SET_TYPE_STRUCTURAL_EQUALITY (type);
3809 return type;
3812 expr = mark_type_use (expr);
3814 type = unlowered_expr_type (expr);
3816 if (!type || type == unknown_type_node)
3818 error ("type of %qE is unknown", expr);
3819 return error_mark_node;
3822 return type;
3825 /* Implement the __underlying_type keyword: Return the underlying
3826 type of TYPE, suitable for use as a type-specifier. */
3828 tree
3829 finish_underlying_type (tree type)
3831 tree underlying_type;
3833 if (processing_template_decl)
3835 underlying_type = cxx_make_type (UNDERLYING_TYPE);
3836 UNDERLYING_TYPE_TYPE (underlying_type) = type;
3837 SET_TYPE_STRUCTURAL_EQUALITY (underlying_type);
3839 return underlying_type;
3842 if (!complete_type_or_else (type, NULL_TREE))
3843 return error_mark_node;
3845 if (TREE_CODE (type) != ENUMERAL_TYPE)
3847 error ("%qT is not an enumeration type", type);
3848 return error_mark_node;
3851 underlying_type = ENUM_UNDERLYING_TYPE (type);
3853 /* Fixup necessary in this case because ENUM_UNDERLYING_TYPE
3854 includes TYPE_MIN_VALUE and TYPE_MAX_VALUE information.
3855 See finish_enum_value_list for details. */
3856 if (!ENUM_FIXED_UNDERLYING_TYPE_P (type))
3857 underlying_type
3858 = c_common_type_for_mode (TYPE_MODE (underlying_type),
3859 TYPE_UNSIGNED (underlying_type));
3861 return underlying_type;
3864 /* Implement the __direct_bases keyword: Return the direct base classes
3865 of type */
3867 tree
3868 calculate_direct_bases (tree type)
3870 vec<tree, va_gc> *vector = make_tree_vector();
3871 tree bases_vec = NULL_TREE;
3872 vec<tree, va_gc> *base_binfos;
3873 tree binfo;
3874 unsigned i;
3876 complete_type (type);
3878 if (!NON_UNION_CLASS_TYPE_P (type))
3879 return make_tree_vec (0);
3881 base_binfos = BINFO_BASE_BINFOS (TYPE_BINFO (type));
3883 /* Virtual bases are initialized first */
3884 for (i = 0; base_binfos->iterate (i, &binfo); i++)
3886 if (BINFO_VIRTUAL_P (binfo))
3888 vec_safe_push (vector, binfo);
3892 /* Now non-virtuals */
3893 for (i = 0; base_binfos->iterate (i, &binfo); i++)
3895 if (!BINFO_VIRTUAL_P (binfo))
3897 vec_safe_push (vector, binfo);
3902 bases_vec = make_tree_vec (vector->length ());
3904 for (i = 0; i < vector->length (); ++i)
3906 TREE_VEC_ELT (bases_vec, i) = BINFO_TYPE ((*vector)[i]);
3908 return bases_vec;
3911 /* Implement the __bases keyword: Return the base classes
3912 of type */
3914 /* Find morally non-virtual base classes by walking binfo hierarchy */
3915 /* Virtual base classes are handled separately in finish_bases */
3917 static tree
3918 dfs_calculate_bases_pre (tree binfo, void * /*data_*/)
3920 /* Don't walk bases of virtual bases */
3921 return BINFO_VIRTUAL_P (binfo) ? dfs_skip_bases : NULL_TREE;
3924 static tree
3925 dfs_calculate_bases_post (tree binfo, void *data_)
3927 vec<tree, va_gc> **data = ((vec<tree, va_gc> **) data_);
3928 if (!BINFO_VIRTUAL_P (binfo))
3930 vec_safe_push (*data, BINFO_TYPE (binfo));
3932 return NULL_TREE;
3935 /* Calculates the morally non-virtual base classes of a class */
3936 static vec<tree, va_gc> *
3937 calculate_bases_helper (tree type)
3939 vec<tree, va_gc> *vector = make_tree_vector();
3941 /* Now add non-virtual base classes in order of construction */
3942 if (TYPE_BINFO (type))
3943 dfs_walk_all (TYPE_BINFO (type),
3944 dfs_calculate_bases_pre, dfs_calculate_bases_post, &vector);
3945 return vector;
3948 tree
3949 calculate_bases (tree type)
3951 vec<tree, va_gc> *vector = make_tree_vector();
3952 tree bases_vec = NULL_TREE;
3953 unsigned i;
3954 vec<tree, va_gc> *vbases;
3955 vec<tree, va_gc> *nonvbases;
3956 tree binfo;
3958 complete_type (type);
3960 if (!NON_UNION_CLASS_TYPE_P (type))
3961 return make_tree_vec (0);
3963 /* First go through virtual base classes */
3964 for (vbases = CLASSTYPE_VBASECLASSES (type), i = 0;
3965 vec_safe_iterate (vbases, i, &binfo); i++)
3967 vec<tree, va_gc> *vbase_bases;
3968 vbase_bases = calculate_bases_helper (BINFO_TYPE (binfo));
3969 vec_safe_splice (vector, vbase_bases);
3970 release_tree_vector (vbase_bases);
3973 /* Now for the non-virtual bases */
3974 nonvbases = calculate_bases_helper (type);
3975 vec_safe_splice (vector, nonvbases);
3976 release_tree_vector (nonvbases);
3978 /* Note that during error recovery vector->length can even be zero. */
3979 if (vector->length () > 1)
3981 /* Last element is entire class, so don't copy */
3982 bases_vec = make_tree_vec (vector->length() - 1);
3984 for (i = 0; i < vector->length () - 1; ++i)
3985 TREE_VEC_ELT (bases_vec, i) = (*vector)[i];
3987 else
3988 bases_vec = make_tree_vec (0);
3990 release_tree_vector (vector);
3991 return bases_vec;
3994 tree
3995 finish_bases (tree type, bool direct)
3997 tree bases = NULL_TREE;
3999 if (!processing_template_decl)
4001 /* Parameter packs can only be used in templates */
4002 error ("Parameter pack __bases only valid in template declaration");
4003 return error_mark_node;
4006 bases = cxx_make_type (BASES);
4007 BASES_TYPE (bases) = type;
4008 BASES_DIRECT (bases) = direct;
4009 SET_TYPE_STRUCTURAL_EQUALITY (bases);
4011 return bases;
4014 /* Perform C++-specific checks for __builtin_offsetof before calling
4015 fold_offsetof. */
4017 tree
4018 finish_offsetof (tree object_ptr, tree expr, location_t loc)
4020 /* If we're processing a template, we can't finish the semantics yet.
4021 Otherwise we can fold the entire expression now. */
4022 if (processing_template_decl)
4024 expr = build2 (OFFSETOF_EXPR, size_type_node, expr, object_ptr);
4025 SET_EXPR_LOCATION (expr, loc);
4026 return expr;
4029 if (TREE_CODE (expr) == PSEUDO_DTOR_EXPR)
4031 error ("cannot apply %<offsetof%> to destructor %<~%T%>",
4032 TREE_OPERAND (expr, 2));
4033 return error_mark_node;
4035 if (TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE
4036 || TREE_CODE (TREE_TYPE (expr)) == METHOD_TYPE
4037 || TREE_TYPE (expr) == unknown_type_node)
4039 if (INDIRECT_REF_P (expr))
4040 error ("second operand of %<offsetof%> is neither a single "
4041 "identifier nor a sequence of member accesses and "
4042 "array references");
4043 else
4045 if (TREE_CODE (expr) == COMPONENT_REF
4046 || TREE_CODE (expr) == COMPOUND_EXPR)
4047 expr = TREE_OPERAND (expr, 1);
4048 error ("cannot apply %<offsetof%> to member function %qD", expr);
4050 return error_mark_node;
4052 if (REFERENCE_REF_P (expr))
4053 expr = TREE_OPERAND (expr, 0);
4054 if (!complete_type_or_else (TREE_TYPE (TREE_TYPE (object_ptr)), object_ptr))
4055 return error_mark_node;
4056 if (warn_invalid_offsetof
4057 && CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (object_ptr)))
4058 && CLASSTYPE_NON_STD_LAYOUT (TREE_TYPE (TREE_TYPE (object_ptr)))
4059 && cp_unevaluated_operand == 0)
4060 pedwarn (loc, OPT_Winvalid_offsetof,
4061 "offsetof within non-standard-layout type %qT is undefined",
4062 TREE_TYPE (TREE_TYPE (object_ptr)));
4063 return fold_offsetof (expr);
4066 /* Replace the AGGR_INIT_EXPR at *TP with an equivalent CALL_EXPR. This
4067 function is broken out from the above for the benefit of the tree-ssa
4068 project. */
4070 void
4071 simplify_aggr_init_expr (tree *tp)
4073 tree aggr_init_expr = *tp;
4075 /* Form an appropriate CALL_EXPR. */
4076 tree fn = AGGR_INIT_EXPR_FN (aggr_init_expr);
4077 tree slot = AGGR_INIT_EXPR_SLOT (aggr_init_expr);
4078 tree type = TREE_TYPE (slot);
4080 tree call_expr;
4081 enum style_t { ctor, arg, pcc } style;
4083 if (AGGR_INIT_VIA_CTOR_P (aggr_init_expr))
4084 style = ctor;
4085 #ifdef PCC_STATIC_STRUCT_RETURN
4086 else if (1)
4087 style = pcc;
4088 #endif
4089 else
4091 gcc_assert (TREE_ADDRESSABLE (type));
4092 style = arg;
4095 call_expr = build_call_array_loc (input_location,
4096 TREE_TYPE (TREE_TYPE (TREE_TYPE (fn))),
4098 aggr_init_expr_nargs (aggr_init_expr),
4099 AGGR_INIT_EXPR_ARGP (aggr_init_expr));
4100 TREE_NOTHROW (call_expr) = TREE_NOTHROW (aggr_init_expr);
4101 CALL_FROM_THUNK_P (call_expr) = AGGR_INIT_FROM_THUNK_P (aggr_init_expr);
4102 CALL_EXPR_OPERATOR_SYNTAX (call_expr)
4103 = CALL_EXPR_OPERATOR_SYNTAX (aggr_init_expr);
4104 CALL_EXPR_ORDERED_ARGS (call_expr) = CALL_EXPR_ORDERED_ARGS (aggr_init_expr);
4105 CALL_EXPR_REVERSE_ARGS (call_expr) = CALL_EXPR_REVERSE_ARGS (aggr_init_expr);
4107 if (style == ctor)
4109 /* Replace the first argument to the ctor with the address of the
4110 slot. */
4111 cxx_mark_addressable (slot);
4112 CALL_EXPR_ARG (call_expr, 0) =
4113 build1 (ADDR_EXPR, build_pointer_type (type), slot);
4115 else if (style == arg)
4117 /* Just mark it addressable here, and leave the rest to
4118 expand_call{,_inline}. */
4119 cxx_mark_addressable (slot);
4120 CALL_EXPR_RETURN_SLOT_OPT (call_expr) = true;
4121 call_expr = build2 (INIT_EXPR, TREE_TYPE (call_expr), slot, call_expr);
4123 else if (style == pcc)
4125 /* If we're using the non-reentrant PCC calling convention, then we
4126 need to copy the returned value out of the static buffer into the
4127 SLOT. */
4128 push_deferring_access_checks (dk_no_check);
4129 call_expr = build_aggr_init (slot, call_expr,
4130 DIRECT_BIND | LOOKUP_ONLYCONVERTING,
4131 tf_warning_or_error);
4132 pop_deferring_access_checks ();
4133 call_expr = build2 (COMPOUND_EXPR, TREE_TYPE (slot), call_expr, slot);
4136 if (AGGR_INIT_ZERO_FIRST (aggr_init_expr))
4138 tree init = build_zero_init (type, NULL_TREE,
4139 /*static_storage_p=*/false);
4140 init = build2 (INIT_EXPR, void_type_node, slot, init);
4141 call_expr = build2 (COMPOUND_EXPR, TREE_TYPE (call_expr),
4142 init, call_expr);
4145 *tp = call_expr;
4148 /* Emit all thunks to FN that should be emitted when FN is emitted. */
4150 void
4151 emit_associated_thunks (tree fn)
4153 /* When we use vcall offsets, we emit thunks with the virtual
4154 functions to which they thunk. The whole point of vcall offsets
4155 is so that you can know statically the entire set of thunks that
4156 will ever be needed for a given virtual function, thereby
4157 enabling you to output all the thunks with the function itself. */
4158 if (DECL_VIRTUAL_P (fn)
4159 /* Do not emit thunks for extern template instantiations. */
4160 && ! DECL_REALLY_EXTERN (fn))
4162 tree thunk;
4164 for (thunk = DECL_THUNKS (fn); thunk; thunk = DECL_CHAIN (thunk))
4166 if (!THUNK_ALIAS (thunk))
4168 use_thunk (thunk, /*emit_p=*/1);
4169 if (DECL_RESULT_THUNK_P (thunk))
4171 tree probe;
4173 for (probe = DECL_THUNKS (thunk);
4174 probe; probe = DECL_CHAIN (probe))
4175 use_thunk (probe, /*emit_p=*/1);
4178 else
4179 gcc_assert (!DECL_THUNKS (thunk));
4184 /* Generate RTL for FN. */
4186 bool
4187 expand_or_defer_fn_1 (tree fn)
4189 /* When the parser calls us after finishing the body of a template
4190 function, we don't really want to expand the body. */
4191 if (processing_template_decl)
4193 /* Normally, collection only occurs in rest_of_compilation. So,
4194 if we don't collect here, we never collect junk generated
4195 during the processing of templates until we hit a
4196 non-template function. It's not safe to do this inside a
4197 nested class, though, as the parser may have local state that
4198 is not a GC root. */
4199 if (!function_depth)
4200 ggc_collect ();
4201 return false;
4204 gcc_assert (DECL_SAVED_TREE (fn));
4206 /* We make a decision about linkage for these functions at the end
4207 of the compilation. Until that point, we do not want the back
4208 end to output them -- but we do want it to see the bodies of
4209 these functions so that it can inline them as appropriate. */
4210 if (DECL_DECLARED_INLINE_P (fn) || DECL_IMPLICIT_INSTANTIATION (fn))
4212 if (DECL_INTERFACE_KNOWN (fn))
4213 /* We've already made a decision as to how this function will
4214 be handled. */;
4215 else if (!at_eof)
4216 tentative_decl_linkage (fn);
4217 else
4218 import_export_decl (fn);
4220 /* If the user wants us to keep all inline functions, then mark
4221 this function as needed so that finish_file will make sure to
4222 output it later. Similarly, all dllexport'd functions must
4223 be emitted; there may be callers in other DLLs. */
4224 if (DECL_DECLARED_INLINE_P (fn)
4225 && !DECL_REALLY_EXTERN (fn)
4226 && (flag_keep_inline_functions
4227 || (flag_keep_inline_dllexport
4228 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (fn)))))
4230 mark_needed (fn);
4231 DECL_EXTERNAL (fn) = 0;
4235 /* If this is a constructor or destructor body, we have to clone
4236 it. */
4237 if (maybe_clone_body (fn))
4239 /* We don't want to process FN again, so pretend we've written
4240 it out, even though we haven't. */
4241 TREE_ASM_WRITTEN (fn) = 1;
4242 /* If this is a constexpr function, keep DECL_SAVED_TREE. */
4243 if (!DECL_DECLARED_CONSTEXPR_P (fn))
4244 DECL_SAVED_TREE (fn) = NULL_TREE;
4245 return false;
4248 /* There's no reason to do any of the work here if we're only doing
4249 semantic analysis; this code just generates RTL. */
4250 if (flag_syntax_only)
4251 return false;
4253 return true;
4256 void
4257 expand_or_defer_fn (tree fn)
4259 if (expand_or_defer_fn_1 (fn))
4261 function_depth++;
4263 /* Expand or defer, at the whim of the compilation unit manager. */
4264 cgraph_node::finalize_function (fn, function_depth > 1);
4265 emit_associated_thunks (fn);
4267 function_depth--;
4271 struct nrv_data
4273 nrv_data () : visited (37) {}
4275 tree var;
4276 tree result;
4277 hash_table<nofree_ptr_hash <tree_node> > visited;
4280 /* Helper function for walk_tree, used by finalize_nrv below. */
4282 static tree
4283 finalize_nrv_r (tree* tp, int* walk_subtrees, void* data)
4285 struct nrv_data *dp = (struct nrv_data *)data;
4286 tree_node **slot;
4288 /* No need to walk into types. There wouldn't be any need to walk into
4289 non-statements, except that we have to consider STMT_EXPRs. */
4290 if (TYPE_P (*tp))
4291 *walk_subtrees = 0;
4292 /* Change all returns to just refer to the RESULT_DECL; this is a nop,
4293 but differs from using NULL_TREE in that it indicates that we care
4294 about the value of the RESULT_DECL. */
4295 else if (TREE_CODE (*tp) == RETURN_EXPR)
4296 TREE_OPERAND (*tp, 0) = dp->result;
4297 /* Change all cleanups for the NRV to only run when an exception is
4298 thrown. */
4299 else if (TREE_CODE (*tp) == CLEANUP_STMT
4300 && CLEANUP_DECL (*tp) == dp->var)
4301 CLEANUP_EH_ONLY (*tp) = 1;
4302 /* Replace the DECL_EXPR for the NRV with an initialization of the
4303 RESULT_DECL, if needed. */
4304 else if (TREE_CODE (*tp) == DECL_EXPR
4305 && DECL_EXPR_DECL (*tp) == dp->var)
4307 tree init;
4308 if (DECL_INITIAL (dp->var)
4309 && DECL_INITIAL (dp->var) != error_mark_node)
4310 init = build2 (INIT_EXPR, void_type_node, dp->result,
4311 DECL_INITIAL (dp->var));
4312 else
4313 init = build_empty_stmt (EXPR_LOCATION (*tp));
4314 DECL_INITIAL (dp->var) = NULL_TREE;
4315 SET_EXPR_LOCATION (init, EXPR_LOCATION (*tp));
4316 *tp = init;
4318 /* And replace all uses of the NRV with the RESULT_DECL. */
4319 else if (*tp == dp->var)
4320 *tp = dp->result;
4322 /* Avoid walking into the same tree more than once. Unfortunately, we
4323 can't just use walk_tree_without duplicates because it would only call
4324 us for the first occurrence of dp->var in the function body. */
4325 slot = dp->visited.find_slot (*tp, INSERT);
4326 if (*slot)
4327 *walk_subtrees = 0;
4328 else
4329 *slot = *tp;
4331 /* Keep iterating. */
4332 return NULL_TREE;
4335 /* Called from finish_function to implement the named return value
4336 optimization by overriding all the RETURN_EXPRs and pertinent
4337 CLEANUP_STMTs and replacing all occurrences of VAR with RESULT, the
4338 RESULT_DECL for the function. */
4340 void
4341 finalize_nrv (tree *tp, tree var, tree result)
4343 struct nrv_data data;
4345 /* Copy name from VAR to RESULT. */
4346 DECL_NAME (result) = DECL_NAME (var);
4347 /* Don't forget that we take its address. */
4348 TREE_ADDRESSABLE (result) = TREE_ADDRESSABLE (var);
4349 /* Finally set DECL_VALUE_EXPR to avoid assigning
4350 a stack slot at -O0 for the original var and debug info
4351 uses RESULT location for VAR. */
4352 SET_DECL_VALUE_EXPR (var, result);
4353 DECL_HAS_VALUE_EXPR_P (var) = 1;
4355 data.var = var;
4356 data.result = result;
4357 cp_walk_tree (tp, finalize_nrv_r, &data, 0);
4360 /* Create CP_OMP_CLAUSE_INFO for clause C. Returns true if it is invalid. */
4362 bool
4363 cxx_omp_create_clause_info (tree c, tree type, bool need_default_ctor,
4364 bool need_copy_ctor, bool need_copy_assignment,
4365 bool need_dtor)
4367 int save_errorcount = errorcount;
4368 tree info, t;
4370 /* Always allocate 3 elements for simplicity. These are the
4371 function decls for the ctor, dtor, and assignment op.
4372 This layout is known to the three lang hooks,
4373 cxx_omp_clause_default_init, cxx_omp_clause_copy_init,
4374 and cxx_omp_clause_assign_op. */
4375 info = make_tree_vec (3);
4376 CP_OMP_CLAUSE_INFO (c) = info;
4378 if (need_default_ctor || need_copy_ctor)
4380 if (need_default_ctor)
4381 t = get_default_ctor (type);
4382 else
4383 t = get_copy_ctor (type, tf_warning_or_error);
4385 if (t && !trivial_fn_p (t))
4386 TREE_VEC_ELT (info, 0) = t;
4389 if (need_dtor && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
4390 TREE_VEC_ELT (info, 1) = get_dtor (type, tf_warning_or_error);
4392 if (need_copy_assignment)
4394 t = get_copy_assign (type);
4396 if (t && !trivial_fn_p (t))
4397 TREE_VEC_ELT (info, 2) = t;
4400 return errorcount != save_errorcount;
4403 /* If DECL is DECL_OMP_PRIVATIZED_MEMBER, return corresponding
4404 FIELD_DECL, otherwise return DECL itself. */
4406 static tree
4407 omp_clause_decl_field (tree decl)
4409 if (VAR_P (decl)
4410 && DECL_HAS_VALUE_EXPR_P (decl)
4411 && DECL_ARTIFICIAL (decl)
4412 && DECL_LANG_SPECIFIC (decl)
4413 && DECL_OMP_PRIVATIZED_MEMBER (decl))
4415 tree f = DECL_VALUE_EXPR (decl);
4416 if (TREE_CODE (f) == INDIRECT_REF)
4417 f = TREE_OPERAND (f, 0);
4418 if (TREE_CODE (f) == COMPONENT_REF)
4420 f = TREE_OPERAND (f, 1);
4421 gcc_assert (TREE_CODE (f) == FIELD_DECL);
4422 return f;
4425 return NULL_TREE;
4428 /* Adjust DECL if needed for printing using %qE. */
4430 static tree
4431 omp_clause_printable_decl (tree decl)
4433 tree t = omp_clause_decl_field (decl);
4434 if (t)
4435 return t;
4436 return decl;
4439 /* For a FIELD_DECL F and corresponding DECL_OMP_PRIVATIZED_MEMBER
4440 VAR_DECL T that doesn't need a DECL_EXPR added, record it for
4441 privatization. */
4443 static void
4444 omp_note_field_privatization (tree f, tree t)
4446 if (!omp_private_member_map)
4447 omp_private_member_map = new hash_map<tree, tree>;
4448 tree &v = omp_private_member_map->get_or_insert (f);
4449 if (v == NULL_TREE)
4451 v = t;
4452 omp_private_member_vec.safe_push (f);
4453 /* Signal that we don't want to create DECL_EXPR for this dummy var. */
4454 omp_private_member_vec.safe_push (integer_zero_node);
4458 /* Privatize FIELD_DECL T, return corresponding DECL_OMP_PRIVATIZED_MEMBER
4459 dummy VAR_DECL. */
4461 tree
4462 omp_privatize_field (tree t, bool shared)
4464 tree m = finish_non_static_data_member (t, NULL_TREE, NULL_TREE);
4465 if (m == error_mark_node)
4466 return error_mark_node;
4467 if (!omp_private_member_map && !shared)
4468 omp_private_member_map = new hash_map<tree, tree>;
4469 if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE)
4471 gcc_assert (TREE_CODE (m) == INDIRECT_REF);
4472 m = TREE_OPERAND (m, 0);
4474 tree vb = NULL_TREE;
4475 tree &v = shared ? vb : omp_private_member_map->get_or_insert (t);
4476 if (v == NULL_TREE)
4478 v = create_temporary_var (TREE_TYPE (m));
4479 if (!DECL_LANG_SPECIFIC (v))
4480 retrofit_lang_decl (v);
4481 DECL_OMP_PRIVATIZED_MEMBER (v) = 1;
4482 SET_DECL_VALUE_EXPR (v, m);
4483 DECL_HAS_VALUE_EXPR_P (v) = 1;
4484 if (!shared)
4485 omp_private_member_vec.safe_push (t);
4487 return v;
4490 /* Helper function for handle_omp_array_sections. Called recursively
4491 to handle multiple array-section-subscripts. C is the clause,
4492 T current expression (initially OMP_CLAUSE_DECL), which is either
4493 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
4494 expression if specified, TREE_VALUE length expression if specified,
4495 TREE_CHAIN is what it has been specified after, or some decl.
4496 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
4497 set to true if any of the array-section-subscript could have length
4498 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
4499 first array-section-subscript which is known not to have length
4500 of one. Given say:
4501 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
4502 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
4503 all are or may have length of 1, array-section-subscript [:2] is the
4504 first one known not to have length 1. For array-section-subscript
4505 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
4506 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
4507 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
4508 case though, as some lengths could be zero. */
4510 static tree
4511 handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
4512 bool &maybe_zero_len, unsigned int &first_non_one,
4513 enum c_omp_region_type ort)
4515 tree ret, low_bound, length, type;
4516 if (TREE_CODE (t) != TREE_LIST)
4518 if (error_operand_p (t))
4519 return error_mark_node;
4520 if (REFERENCE_REF_P (t)
4521 && TREE_CODE (TREE_OPERAND (t, 0)) == COMPONENT_REF)
4522 t = TREE_OPERAND (t, 0);
4523 ret = t;
4524 if (TREE_CODE (t) == COMPONENT_REF
4525 && ort == C_ORT_OMP
4526 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
4527 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO
4528 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FROM)
4529 && !type_dependent_expression_p (t))
4531 if (TREE_CODE (TREE_OPERAND (t, 1)) == FIELD_DECL
4532 && DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
4534 error_at (OMP_CLAUSE_LOCATION (c),
4535 "bit-field %qE in %qs clause",
4536 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4537 return error_mark_node;
4539 while (TREE_CODE (t) == COMPONENT_REF)
4541 if (TREE_TYPE (TREE_OPERAND (t, 0))
4542 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == UNION_TYPE)
4544 error_at (OMP_CLAUSE_LOCATION (c),
4545 "%qE is a member of a union", t);
4546 return error_mark_node;
4548 t = TREE_OPERAND (t, 0);
4550 if (REFERENCE_REF_P (t))
4551 t = TREE_OPERAND (t, 0);
4553 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
4555 if (processing_template_decl)
4556 return NULL_TREE;
4557 if (DECL_P (t))
4558 error_at (OMP_CLAUSE_LOCATION (c),
4559 "%qD is not a variable in %qs clause", t,
4560 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4561 else
4562 error_at (OMP_CLAUSE_LOCATION (c),
4563 "%qE is not a variable in %qs clause", t,
4564 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4565 return error_mark_node;
4567 else if (TREE_CODE (t) == PARM_DECL
4568 && DECL_ARTIFICIAL (t)
4569 && DECL_NAME (t) == this_identifier)
4571 error_at (OMP_CLAUSE_LOCATION (c),
4572 "%<this%> allowed in OpenMP only in %<declare simd%>"
4573 " clauses");
4574 return error_mark_node;
4576 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
4577 && VAR_P (t) && CP_DECL_THREAD_LOCAL_P (t))
4579 error_at (OMP_CLAUSE_LOCATION (c),
4580 "%qD is threadprivate variable in %qs clause", t,
4581 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4582 return error_mark_node;
4584 if (type_dependent_expression_p (ret))
4585 return NULL_TREE;
4586 ret = convert_from_reference (ret);
4587 return ret;
4590 if (ort == C_ORT_OMP
4591 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
4592 && TREE_CODE (TREE_CHAIN (t)) == FIELD_DECL)
4593 TREE_CHAIN (t) = omp_privatize_field (TREE_CHAIN (t), false);
4594 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
4595 maybe_zero_len, first_non_one, ort);
4596 if (ret == error_mark_node || ret == NULL_TREE)
4597 return ret;
4599 type = TREE_TYPE (ret);
4600 low_bound = TREE_PURPOSE (t);
4601 length = TREE_VALUE (t);
4602 if ((low_bound && type_dependent_expression_p (low_bound))
4603 || (length && type_dependent_expression_p (length)))
4604 return NULL_TREE;
4606 if (low_bound == error_mark_node || length == error_mark_node)
4607 return error_mark_node;
4609 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
4611 error_at (OMP_CLAUSE_LOCATION (c),
4612 "low bound %qE of array section does not have integral type",
4613 low_bound);
4614 return error_mark_node;
4616 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
4618 error_at (OMP_CLAUSE_LOCATION (c),
4619 "length %qE of array section does not have integral type",
4620 length);
4621 return error_mark_node;
4623 if (low_bound)
4624 low_bound = mark_rvalue_use (low_bound);
4625 if (length)
4626 length = mark_rvalue_use (length);
4627 /* We need to reduce to real constant-values for checks below. */
4628 if (length)
4629 length = fold_simple (length);
4630 if (low_bound)
4631 low_bound = fold_simple (low_bound);
4632 if (low_bound
4633 && TREE_CODE (low_bound) == INTEGER_CST
4634 && TYPE_PRECISION (TREE_TYPE (low_bound))
4635 > TYPE_PRECISION (sizetype))
4636 low_bound = fold_convert (sizetype, low_bound);
4637 if (length
4638 && TREE_CODE (length) == INTEGER_CST
4639 && TYPE_PRECISION (TREE_TYPE (length))
4640 > TYPE_PRECISION (sizetype))
4641 length = fold_convert (sizetype, length);
4642 if (low_bound == NULL_TREE)
4643 low_bound = integer_zero_node;
4645 if (length != NULL_TREE)
4647 if (!integer_nonzerop (length))
4649 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
4650 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
4652 if (integer_zerop (length))
4654 error_at (OMP_CLAUSE_LOCATION (c),
4655 "zero length array section in %qs clause",
4656 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4657 return error_mark_node;
4660 else
4661 maybe_zero_len = true;
4663 if (first_non_one == types.length ()
4664 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
4665 first_non_one++;
4667 if (TREE_CODE (type) == ARRAY_TYPE)
4669 if (length == NULL_TREE
4670 && (TYPE_DOMAIN (type) == NULL_TREE
4671 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
4673 error_at (OMP_CLAUSE_LOCATION (c),
4674 "for unknown bound array type length expression must "
4675 "be specified");
4676 return error_mark_node;
4678 if (TREE_CODE (low_bound) == INTEGER_CST
4679 && tree_int_cst_sgn (low_bound) == -1)
4681 error_at (OMP_CLAUSE_LOCATION (c),
4682 "negative low bound in array section in %qs clause",
4683 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4684 return error_mark_node;
4686 if (length != NULL_TREE
4687 && TREE_CODE (length) == INTEGER_CST
4688 && tree_int_cst_sgn (length) == -1)
4690 error_at (OMP_CLAUSE_LOCATION (c),
4691 "negative length in array section in %qs clause",
4692 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4693 return error_mark_node;
4695 if (TYPE_DOMAIN (type)
4696 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
4697 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
4698 == INTEGER_CST)
4700 tree size = size_binop (PLUS_EXPR,
4701 TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
4702 size_one_node);
4703 if (TREE_CODE (low_bound) == INTEGER_CST)
4705 if (tree_int_cst_lt (size, low_bound))
4707 error_at (OMP_CLAUSE_LOCATION (c),
4708 "low bound %qE above array section size "
4709 "in %qs clause", low_bound,
4710 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4711 return error_mark_node;
4713 if (tree_int_cst_equal (size, low_bound))
4715 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
4716 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
4718 error_at (OMP_CLAUSE_LOCATION (c),
4719 "zero length array section in %qs clause",
4720 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4721 return error_mark_node;
4723 maybe_zero_len = true;
4725 else if (length == NULL_TREE
4726 && first_non_one == types.length ()
4727 && tree_int_cst_equal
4728 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
4729 low_bound))
4730 first_non_one++;
4732 else if (length == NULL_TREE)
4734 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
4735 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION)
4736 maybe_zero_len = true;
4737 if (first_non_one == types.length ())
4738 first_non_one++;
4740 if (length && TREE_CODE (length) == INTEGER_CST)
4742 if (tree_int_cst_lt (size, length))
4744 error_at (OMP_CLAUSE_LOCATION (c),
4745 "length %qE above array section size "
4746 "in %qs clause", length,
4747 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4748 return error_mark_node;
4750 if (TREE_CODE (low_bound) == INTEGER_CST)
4752 tree lbpluslen
4753 = size_binop (PLUS_EXPR,
4754 fold_convert (sizetype, low_bound),
4755 fold_convert (sizetype, length));
4756 if (TREE_CODE (lbpluslen) == INTEGER_CST
4757 && tree_int_cst_lt (size, lbpluslen))
4759 error_at (OMP_CLAUSE_LOCATION (c),
4760 "high bound %qE above array section size "
4761 "in %qs clause", lbpluslen,
4762 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4763 return error_mark_node;
4768 else if (length == NULL_TREE)
4770 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
4771 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION)
4772 maybe_zero_len = true;
4773 if (first_non_one == types.length ())
4774 first_non_one++;
4777 /* For [lb:] we will need to evaluate lb more than once. */
4778 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
4780 tree lb = cp_save_expr (low_bound);
4781 if (lb != low_bound)
4783 TREE_PURPOSE (t) = lb;
4784 low_bound = lb;
4788 else if (TREE_CODE (type) == POINTER_TYPE)
4790 if (length == NULL_TREE)
4792 error_at (OMP_CLAUSE_LOCATION (c),
4793 "for pointer type length expression must be specified");
4794 return error_mark_node;
4796 if (length != NULL_TREE
4797 && TREE_CODE (length) == INTEGER_CST
4798 && tree_int_cst_sgn (length) == -1)
4800 error_at (OMP_CLAUSE_LOCATION (c),
4801 "negative length in array section in %qs clause",
4802 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4803 return error_mark_node;
4805 /* If there is a pointer type anywhere but in the very first
4806 array-section-subscript, the array section can't be contiguous. */
4807 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
4808 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
4810 error_at (OMP_CLAUSE_LOCATION (c),
4811 "array section is not contiguous in %qs clause",
4812 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4813 return error_mark_node;
4816 else
4818 error_at (OMP_CLAUSE_LOCATION (c),
4819 "%qE does not have pointer or array type", ret);
4820 return error_mark_node;
4822 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
4823 types.safe_push (TREE_TYPE (ret));
4824 /* We will need to evaluate lb more than once. */
4825 tree lb = cp_save_expr (low_bound);
4826 if (lb != low_bound)
4828 TREE_PURPOSE (t) = lb;
4829 low_bound = lb;
4831 ret = grok_array_decl (OMP_CLAUSE_LOCATION (c), ret, low_bound, false);
4832 return ret;
4835 /* Handle array sections for clause C. */
4837 static bool
4838 handle_omp_array_sections (tree c, enum c_omp_region_type ort)
4840 bool maybe_zero_len = false;
4841 unsigned int first_non_one = 0;
4842 auto_vec<tree, 10> types;
4843 tree first = handle_omp_array_sections_1 (c, OMP_CLAUSE_DECL (c), types,
4844 maybe_zero_len, first_non_one,
4845 ort);
4846 if (first == error_mark_node)
4847 return true;
4848 if (first == NULL_TREE)
4849 return false;
4850 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
4852 tree t = OMP_CLAUSE_DECL (c);
4853 tree tem = NULL_TREE;
4854 if (processing_template_decl)
4855 return false;
4856 /* Need to evaluate side effects in the length expressions
4857 if any. */
4858 while (TREE_CODE (t) == TREE_LIST)
4860 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
4862 if (tem == NULL_TREE)
4863 tem = TREE_VALUE (t);
4864 else
4865 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
4866 TREE_VALUE (t), tem);
4868 t = TREE_CHAIN (t);
4870 if (tem)
4871 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
4872 OMP_CLAUSE_DECL (c) = first;
4874 else
4876 unsigned int num = types.length (), i;
4877 tree t, side_effects = NULL_TREE, size = NULL_TREE;
4878 tree condition = NULL_TREE;
4880 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
4881 maybe_zero_len = true;
4882 if (processing_template_decl && maybe_zero_len)
4883 return false;
4885 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
4886 t = TREE_CHAIN (t))
4888 tree low_bound = TREE_PURPOSE (t);
4889 tree length = TREE_VALUE (t);
4891 i--;
4892 if (low_bound
4893 && TREE_CODE (low_bound) == INTEGER_CST
4894 && TYPE_PRECISION (TREE_TYPE (low_bound))
4895 > TYPE_PRECISION (sizetype))
4896 low_bound = fold_convert (sizetype, low_bound);
4897 if (length
4898 && TREE_CODE (length) == INTEGER_CST
4899 && TYPE_PRECISION (TREE_TYPE (length))
4900 > TYPE_PRECISION (sizetype))
4901 length = fold_convert (sizetype, length);
4902 if (low_bound == NULL_TREE)
4903 low_bound = integer_zero_node;
4904 if (!maybe_zero_len && i > first_non_one)
4906 if (integer_nonzerop (low_bound))
4907 goto do_warn_noncontiguous;
4908 if (length != NULL_TREE
4909 && TREE_CODE (length) == INTEGER_CST
4910 && TYPE_DOMAIN (types[i])
4911 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
4912 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
4913 == INTEGER_CST)
4915 tree size;
4916 size = size_binop (PLUS_EXPR,
4917 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
4918 size_one_node);
4919 if (!tree_int_cst_equal (length, size))
4921 do_warn_noncontiguous:
4922 error_at (OMP_CLAUSE_LOCATION (c),
4923 "array section is not contiguous in %qs "
4924 "clause",
4925 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4926 return true;
4929 if (!processing_template_decl
4930 && length != NULL_TREE
4931 && TREE_SIDE_EFFECTS (length))
4933 if (side_effects == NULL_TREE)
4934 side_effects = length;
4935 else
4936 side_effects = build2 (COMPOUND_EXPR,
4937 TREE_TYPE (side_effects),
4938 length, side_effects);
4941 else if (processing_template_decl)
4942 continue;
4943 else
4945 tree l;
4947 if (i > first_non_one
4948 && ((length && integer_nonzerop (length))
4949 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION))
4950 continue;
4951 if (length)
4952 l = fold_convert (sizetype, length);
4953 else
4955 l = size_binop (PLUS_EXPR,
4956 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
4957 size_one_node);
4958 l = size_binop (MINUS_EXPR, l,
4959 fold_convert (sizetype, low_bound));
4961 if (i > first_non_one)
4963 l = fold_build2 (NE_EXPR, boolean_type_node, l,
4964 size_zero_node);
4965 if (condition == NULL_TREE)
4966 condition = l;
4967 else
4968 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
4969 l, condition);
4971 else if (size == NULL_TREE)
4973 size = size_in_bytes (TREE_TYPE (types[i]));
4974 tree eltype = TREE_TYPE (types[num - 1]);
4975 while (TREE_CODE (eltype) == ARRAY_TYPE)
4976 eltype = TREE_TYPE (eltype);
4977 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
4978 size = size_binop (EXACT_DIV_EXPR, size,
4979 size_in_bytes (eltype));
4980 size = size_binop (MULT_EXPR, size, l);
4981 if (condition)
4982 size = fold_build3 (COND_EXPR, sizetype, condition,
4983 size, size_zero_node);
4985 else
4986 size = size_binop (MULT_EXPR, size, l);
4989 if (!processing_template_decl)
4991 if (side_effects)
4992 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
4993 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
4995 size = size_binop (MINUS_EXPR, size, size_one_node);
4996 tree index_type = build_index_type (size);
4997 tree eltype = TREE_TYPE (first);
4998 while (TREE_CODE (eltype) == ARRAY_TYPE)
4999 eltype = TREE_TYPE (eltype);
5000 tree type = build_array_type (eltype, index_type);
5001 tree ptype = build_pointer_type (eltype);
5002 if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE
5003 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (t))))
5004 t = convert_from_reference (t);
5005 else if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
5006 t = build_fold_addr_expr (t);
5007 tree t2 = build_fold_addr_expr (first);
5008 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
5009 ptrdiff_type_node, t2);
5010 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
5011 ptrdiff_type_node, t2,
5012 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
5013 ptrdiff_type_node, t));
5014 if (tree_fits_shwi_p (t2))
5015 t = build2 (MEM_REF, type, t,
5016 build_int_cst (ptype, tree_to_shwi (t2)));
5017 else
5019 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
5020 sizetype, t2);
5021 t = build2_loc (OMP_CLAUSE_LOCATION (c), POINTER_PLUS_EXPR,
5022 TREE_TYPE (t), t, t2);
5023 t = build2 (MEM_REF, type, t, build_int_cst (ptype, 0));
5025 OMP_CLAUSE_DECL (c) = t;
5026 return false;
5028 OMP_CLAUSE_DECL (c) = first;
5029 OMP_CLAUSE_SIZE (c) = size;
5030 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
5031 || (TREE_CODE (t) == COMPONENT_REF
5032 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE))
5033 return false;
5034 if (ort == C_ORT_OMP || ort == C_ORT_ACC)
5035 switch (OMP_CLAUSE_MAP_KIND (c))
5037 case GOMP_MAP_ALLOC:
5038 case GOMP_MAP_TO:
5039 case GOMP_MAP_FROM:
5040 case GOMP_MAP_TOFROM:
5041 case GOMP_MAP_ALWAYS_TO:
5042 case GOMP_MAP_ALWAYS_FROM:
5043 case GOMP_MAP_ALWAYS_TOFROM:
5044 case GOMP_MAP_RELEASE:
5045 case GOMP_MAP_DELETE:
5046 case GOMP_MAP_FORCE_TO:
5047 case GOMP_MAP_FORCE_FROM:
5048 case GOMP_MAP_FORCE_TOFROM:
5049 case GOMP_MAP_FORCE_PRESENT:
5050 OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (c) = 1;
5051 break;
5052 default:
5053 break;
5055 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
5056 OMP_CLAUSE_MAP);
5057 if ((ort & C_ORT_OMP_DECLARE_SIMD) != C_ORT_OMP && ort != C_ORT_ACC)
5058 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_POINTER);
5059 else if (TREE_CODE (t) == COMPONENT_REF)
5060 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ALWAYS_POINTER);
5061 else if (REFERENCE_REF_P (t)
5062 && TREE_CODE (TREE_OPERAND (t, 0)) == COMPONENT_REF)
5064 t = TREE_OPERAND (t, 0);
5065 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ALWAYS_POINTER);
5067 else
5068 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_FIRSTPRIVATE_POINTER);
5069 if (OMP_CLAUSE_MAP_KIND (c2) != GOMP_MAP_FIRSTPRIVATE_POINTER
5070 && !cxx_mark_addressable (t))
5071 return false;
5072 OMP_CLAUSE_DECL (c2) = t;
5073 t = build_fold_addr_expr (first);
5074 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
5075 ptrdiff_type_node, t);
5076 tree ptr = OMP_CLAUSE_DECL (c2);
5077 ptr = convert_from_reference (ptr);
5078 if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
5079 ptr = build_fold_addr_expr (ptr);
5080 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
5081 ptrdiff_type_node, t,
5082 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
5083 ptrdiff_type_node, ptr));
5084 OMP_CLAUSE_SIZE (c2) = t;
5085 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
5086 OMP_CLAUSE_CHAIN (c) = c2;
5087 ptr = OMP_CLAUSE_DECL (c2);
5088 if (OMP_CLAUSE_MAP_KIND (c2) != GOMP_MAP_FIRSTPRIVATE_POINTER
5089 && TREE_CODE (TREE_TYPE (ptr)) == REFERENCE_TYPE
5090 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (ptr))))
5092 tree c3 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
5093 OMP_CLAUSE_MAP);
5094 OMP_CLAUSE_SET_MAP_KIND (c3, OMP_CLAUSE_MAP_KIND (c2));
5095 OMP_CLAUSE_DECL (c3) = ptr;
5096 if (OMP_CLAUSE_MAP_KIND (c2) == GOMP_MAP_ALWAYS_POINTER)
5097 OMP_CLAUSE_DECL (c2) = build_simple_mem_ref (ptr);
5098 else
5099 OMP_CLAUSE_DECL (c2) = convert_from_reference (ptr);
5100 OMP_CLAUSE_SIZE (c3) = size_zero_node;
5101 OMP_CLAUSE_CHAIN (c3) = OMP_CLAUSE_CHAIN (c2);
5102 OMP_CLAUSE_CHAIN (c2) = c3;
5106 return false;
5109 /* Return identifier to look up for omp declare reduction. */
5111 tree
5112 omp_reduction_id (enum tree_code reduction_code, tree reduction_id, tree type)
5114 const char *p = NULL;
5115 const char *m = NULL;
5116 switch (reduction_code)
5118 case PLUS_EXPR:
5119 case MULT_EXPR:
5120 case MINUS_EXPR:
5121 case BIT_AND_EXPR:
5122 case BIT_XOR_EXPR:
5123 case BIT_IOR_EXPR:
5124 case TRUTH_ANDIF_EXPR:
5125 case TRUTH_ORIF_EXPR:
5126 reduction_id = cp_operator_id (reduction_code);
5127 break;
5128 case MIN_EXPR:
5129 p = "min";
5130 break;
5131 case MAX_EXPR:
5132 p = "max";
5133 break;
5134 default:
5135 break;
5138 if (p == NULL)
5140 if (TREE_CODE (reduction_id) != IDENTIFIER_NODE)
5141 return error_mark_node;
5142 p = IDENTIFIER_POINTER (reduction_id);
5145 if (type != NULL_TREE)
5146 m = mangle_type_string (TYPE_MAIN_VARIANT (type));
5148 const char prefix[] = "omp declare reduction ";
5149 size_t lenp = sizeof (prefix);
5150 if (strncmp (p, prefix, lenp - 1) == 0)
5151 lenp = 1;
5152 size_t len = strlen (p);
5153 size_t lenm = m ? strlen (m) + 1 : 0;
5154 char *name = XALLOCAVEC (char, lenp + len + lenm);
5155 if (lenp > 1)
5156 memcpy (name, prefix, lenp - 1);
5157 memcpy (name + lenp - 1, p, len + 1);
5158 if (m)
5160 name[lenp + len - 1] = '~';
5161 memcpy (name + lenp + len, m, lenm);
5163 return get_identifier (name);
5166 /* Lookup OpenMP UDR ID for TYPE, return the corresponding artificial
5167 FUNCTION_DECL or NULL_TREE if not found. */
5169 static tree
5170 omp_reduction_lookup (location_t loc, tree id, tree type, tree *baselinkp,
5171 vec<tree> *ambiguousp)
5173 tree orig_id = id;
5174 tree baselink = NULL_TREE;
5175 if (identifier_p (id))
5177 cp_id_kind idk;
5178 bool nonint_cst_expression_p;
5179 const char *error_msg;
5180 id = omp_reduction_id (ERROR_MARK, id, type);
5181 tree decl = lookup_name (id);
5182 if (decl == NULL_TREE)
5183 decl = error_mark_node;
5184 id = finish_id_expression (id, decl, NULL_TREE, &idk, false, true,
5185 &nonint_cst_expression_p, false, true, false,
5186 false, &error_msg, loc);
5187 if (idk == CP_ID_KIND_UNQUALIFIED
5188 && identifier_p (id))
5190 vec<tree, va_gc> *args = NULL;
5191 vec_safe_push (args, build_reference_type (type));
5192 id = perform_koenig_lookup (id, args, tf_none);
5195 else if (TREE_CODE (id) == SCOPE_REF)
5196 id = lookup_qualified_name (TREE_OPERAND (id, 0),
5197 omp_reduction_id (ERROR_MARK,
5198 TREE_OPERAND (id, 1),
5199 type),
5200 false, false);
5201 tree fns = id;
5202 if (id && is_overloaded_fn (id))
5203 id = get_fns (id);
5204 for (; id; id = OVL_NEXT (id))
5206 tree fndecl = OVL_CURRENT (id);
5207 if (TREE_CODE (fndecl) == FUNCTION_DECL)
5209 tree argtype = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
5210 if (same_type_p (TREE_TYPE (argtype), type))
5211 break;
5214 if (id && BASELINK_P (fns))
5216 if (baselinkp)
5217 *baselinkp = fns;
5218 else
5219 baselink = fns;
5221 if (id == NULL_TREE && CLASS_TYPE_P (type) && TYPE_BINFO (type))
5223 vec<tree> ambiguous = vNULL;
5224 tree binfo = TYPE_BINFO (type), base_binfo, ret = NULL_TREE;
5225 unsigned int ix;
5226 if (ambiguousp == NULL)
5227 ambiguousp = &ambiguous;
5228 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
5230 id = omp_reduction_lookup (loc, orig_id, BINFO_TYPE (base_binfo),
5231 baselinkp ? baselinkp : &baselink,
5232 ambiguousp);
5233 if (id == NULL_TREE)
5234 continue;
5235 if (!ambiguousp->is_empty ())
5236 ambiguousp->safe_push (id);
5237 else if (ret != NULL_TREE)
5239 ambiguousp->safe_push (ret);
5240 ambiguousp->safe_push (id);
5241 ret = NULL_TREE;
5243 else
5244 ret = id;
5246 if (ambiguousp != &ambiguous)
5247 return ret;
5248 if (!ambiguous.is_empty ())
5250 const char *str = _("candidates are:");
5251 unsigned int idx;
5252 tree udr;
5253 error_at (loc, "user defined reduction lookup is ambiguous");
5254 FOR_EACH_VEC_ELT (ambiguous, idx, udr)
5256 inform (DECL_SOURCE_LOCATION (udr), "%s %#D", str, udr);
5257 if (idx == 0)
5258 str = get_spaces (str);
5260 ambiguous.release ();
5261 ret = error_mark_node;
5262 baselink = NULL_TREE;
5264 id = ret;
5266 if (id && baselink)
5267 perform_or_defer_access_check (BASELINK_BINFO (baselink),
5268 id, id, tf_warning_or_error);
5269 return id;
5272 /* Helper function for cp_parser_omp_declare_reduction_exprs
5273 and tsubst_omp_udr.
5274 Remove CLEANUP_STMT for data (omp_priv variable).
5275 Also append INIT_EXPR for DECL_INITIAL of omp_priv after its
5276 DECL_EXPR. */
5278 tree
5279 cp_remove_omp_priv_cleanup_stmt (tree *tp, int *walk_subtrees, void *data)
5281 if (TYPE_P (*tp))
5282 *walk_subtrees = 0;
5283 else if (TREE_CODE (*tp) == CLEANUP_STMT && CLEANUP_DECL (*tp) == (tree) data)
5284 *tp = CLEANUP_BODY (*tp);
5285 else if (TREE_CODE (*tp) == DECL_EXPR)
5287 tree decl = DECL_EXPR_DECL (*tp);
5288 if (!processing_template_decl
5289 && decl == (tree) data
5290 && DECL_INITIAL (decl)
5291 && DECL_INITIAL (decl) != error_mark_node)
5293 tree list = NULL_TREE;
5294 append_to_statement_list_force (*tp, &list);
5295 tree init_expr = build2 (INIT_EXPR, void_type_node,
5296 decl, DECL_INITIAL (decl));
5297 DECL_INITIAL (decl) = NULL_TREE;
5298 append_to_statement_list_force (init_expr, &list);
5299 *tp = list;
5302 return NULL_TREE;
5305 /* Data passed from cp_check_omp_declare_reduction to
5306 cp_check_omp_declare_reduction_r. */
5308 struct cp_check_omp_declare_reduction_data
5310 location_t loc;
5311 tree stmts[7];
5312 bool combiner_p;
5315 /* Helper function for cp_check_omp_declare_reduction, called via
5316 cp_walk_tree. */
5318 static tree
5319 cp_check_omp_declare_reduction_r (tree *tp, int *, void *data)
5321 struct cp_check_omp_declare_reduction_data *udr_data
5322 = (struct cp_check_omp_declare_reduction_data *) data;
5323 if (SSA_VAR_P (*tp)
5324 && !DECL_ARTIFICIAL (*tp)
5325 && *tp != DECL_EXPR_DECL (udr_data->stmts[udr_data->combiner_p ? 0 : 3])
5326 && *tp != DECL_EXPR_DECL (udr_data->stmts[udr_data->combiner_p ? 1 : 4]))
5328 location_t loc = udr_data->loc;
5329 if (udr_data->combiner_p)
5330 error_at (loc, "%<#pragma omp declare reduction%> combiner refers to "
5331 "variable %qD which is not %<omp_out%> nor %<omp_in%>",
5332 *tp);
5333 else
5334 error_at (loc, "%<#pragma omp declare reduction%> initializer refers "
5335 "to variable %qD which is not %<omp_priv%> nor "
5336 "%<omp_orig%>",
5337 *tp);
5338 return *tp;
5340 return NULL_TREE;
5343 /* Diagnose violation of OpenMP #pragma omp declare reduction restrictions. */
5345 void
5346 cp_check_omp_declare_reduction (tree udr)
5348 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (udr)));
5349 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
5350 type = TREE_TYPE (type);
5351 int i;
5352 location_t loc = DECL_SOURCE_LOCATION (udr);
5354 if (type == error_mark_node)
5355 return;
5356 if (ARITHMETIC_TYPE_P (type))
5358 static enum tree_code predef_codes[]
5359 = { PLUS_EXPR, MULT_EXPR, MINUS_EXPR, BIT_AND_EXPR, BIT_XOR_EXPR,
5360 BIT_IOR_EXPR, TRUTH_ANDIF_EXPR, TRUTH_ORIF_EXPR };
5361 for (i = 0; i < 8; i++)
5363 tree id = omp_reduction_id (predef_codes[i], NULL_TREE, NULL_TREE);
5364 const char *n1 = IDENTIFIER_POINTER (DECL_NAME (udr));
5365 const char *n2 = IDENTIFIER_POINTER (id);
5366 if (strncmp (n1, n2, IDENTIFIER_LENGTH (id)) == 0
5367 && (n1[IDENTIFIER_LENGTH (id)] == '~'
5368 || n1[IDENTIFIER_LENGTH (id)] == '\0'))
5369 break;
5372 if (i == 8
5373 && TREE_CODE (type) != COMPLEX_EXPR)
5375 const char prefix_minmax[] = "omp declare reduction m";
5376 size_t prefix_size = sizeof (prefix_minmax) - 1;
5377 const char *n = IDENTIFIER_POINTER (DECL_NAME (udr));
5378 if (strncmp (IDENTIFIER_POINTER (DECL_NAME (udr)),
5379 prefix_minmax, prefix_size) == 0
5380 && ((n[prefix_size] == 'i' && n[prefix_size + 1] == 'n')
5381 || (n[prefix_size] == 'a' && n[prefix_size + 1] == 'x'))
5382 && (n[prefix_size + 2] == '~' || n[prefix_size + 2] == '\0'))
5383 i = 0;
5385 if (i < 8)
5387 error_at (loc, "predeclared arithmetic type %qT in "
5388 "%<#pragma omp declare reduction%>", type);
5389 return;
5392 else if (TREE_CODE (type) == FUNCTION_TYPE
5393 || TREE_CODE (type) == METHOD_TYPE
5394 || TREE_CODE (type) == ARRAY_TYPE)
5396 error_at (loc, "function or array type %qT in "
5397 "%<#pragma omp declare reduction%>", type);
5398 return;
5400 else if (TREE_CODE (type) == REFERENCE_TYPE)
5402 error_at (loc, "reference type %qT in %<#pragma omp declare reduction%>",
5403 type);
5404 return;
5406 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
5408 error_at (loc, "const, volatile or __restrict qualified type %qT in "
5409 "%<#pragma omp declare reduction%>", type);
5410 return;
5413 tree body = DECL_SAVED_TREE (udr);
5414 if (body == NULL_TREE || TREE_CODE (body) != STATEMENT_LIST)
5415 return;
5417 tree_stmt_iterator tsi;
5418 struct cp_check_omp_declare_reduction_data data;
5419 memset (data.stmts, 0, sizeof data.stmts);
5420 for (i = 0, tsi = tsi_start (body);
5421 i < 7 && !tsi_end_p (tsi);
5422 i++, tsi_next (&tsi))
5423 data.stmts[i] = tsi_stmt (tsi);
5424 data.loc = loc;
5425 gcc_assert (tsi_end_p (tsi));
5426 if (i >= 3)
5428 gcc_assert (TREE_CODE (data.stmts[0]) == DECL_EXPR
5429 && TREE_CODE (data.stmts[1]) == DECL_EXPR);
5430 if (TREE_NO_WARNING (DECL_EXPR_DECL (data.stmts[0])))
5431 return;
5432 data.combiner_p = true;
5433 if (cp_walk_tree (&data.stmts[2], cp_check_omp_declare_reduction_r,
5434 &data, NULL))
5435 TREE_NO_WARNING (DECL_EXPR_DECL (data.stmts[0])) = 1;
5437 if (i >= 6)
5439 gcc_assert (TREE_CODE (data.stmts[3]) == DECL_EXPR
5440 && TREE_CODE (data.stmts[4]) == DECL_EXPR);
5441 data.combiner_p = false;
5442 if (cp_walk_tree (&data.stmts[5], cp_check_omp_declare_reduction_r,
5443 &data, NULL)
5444 || cp_walk_tree (&DECL_INITIAL (DECL_EXPR_DECL (data.stmts[3])),
5445 cp_check_omp_declare_reduction_r, &data, NULL))
5446 TREE_NO_WARNING (DECL_EXPR_DECL (data.stmts[0])) = 1;
5447 if (i == 7)
5448 gcc_assert (TREE_CODE (data.stmts[6]) == DECL_EXPR);
5452 /* Helper function of finish_omp_clauses. Clone STMT as if we were making
5453 an inline call. But, remap
5454 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
5455 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
5457 static tree
5458 clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
5459 tree decl, tree placeholder)
5461 copy_body_data id;
5462 hash_map<tree, tree> decl_map;
5464 decl_map.put (omp_decl1, placeholder);
5465 decl_map.put (omp_decl2, decl);
5466 memset (&id, 0, sizeof (id));
5467 id.src_fn = DECL_CONTEXT (omp_decl1);
5468 id.dst_fn = current_function_decl;
5469 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
5470 id.decl_map = &decl_map;
5472 id.copy_decl = copy_decl_no_change;
5473 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
5474 id.transform_new_cfg = true;
5475 id.transform_return_to_modify = false;
5476 id.transform_lang_insert_block = NULL;
5477 id.eh_lp_nr = 0;
5478 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
5479 return stmt;
5482 /* Helper function of finish_omp_clauses, called via cp_walk_tree.
5483 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
5485 static tree
5486 find_omp_placeholder_r (tree *tp, int *, void *data)
5488 if (*tp == (tree) data)
5489 return *tp;
5490 return NULL_TREE;
5493 /* Helper function of finish_omp_clauses. Handle OMP_CLAUSE_REDUCTION C.
5494 Return true if there is some error and the clause should be removed. */
5496 static bool
5497 finish_omp_reduction_clause (tree c, bool *need_default_ctor, bool *need_dtor)
5499 tree t = OMP_CLAUSE_DECL (c);
5500 bool predefined = false;
5501 if (TREE_CODE (t) == TREE_LIST)
5503 gcc_assert (processing_template_decl);
5504 return false;
5506 tree type = TREE_TYPE (t);
5507 if (TREE_CODE (t) == MEM_REF)
5508 type = TREE_TYPE (type);
5509 if (TREE_CODE (type) == REFERENCE_TYPE)
5510 type = TREE_TYPE (type);
5511 if (TREE_CODE (type) == ARRAY_TYPE)
5513 tree oatype = type;
5514 gcc_assert (TREE_CODE (t) != MEM_REF);
5515 while (TREE_CODE (type) == ARRAY_TYPE)
5516 type = TREE_TYPE (type);
5517 if (!processing_template_decl)
5519 t = require_complete_type (t);
5520 if (t == error_mark_node)
5521 return true;
5522 tree size = size_binop (EXACT_DIV_EXPR, TYPE_SIZE_UNIT (oatype),
5523 TYPE_SIZE_UNIT (type));
5524 if (integer_zerop (size))
5526 error ("%qE in %<reduction%> clause is a zero size array",
5527 omp_clause_printable_decl (t));
5528 return true;
5530 size = size_binop (MINUS_EXPR, size, size_one_node);
5531 tree index_type = build_index_type (size);
5532 tree atype = build_array_type (type, index_type);
5533 tree ptype = build_pointer_type (type);
5534 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
5535 t = build_fold_addr_expr (t);
5536 t = build2 (MEM_REF, atype, t, build_int_cst (ptype, 0));
5537 OMP_CLAUSE_DECL (c) = t;
5540 if (type == error_mark_node)
5541 return true;
5542 else if (ARITHMETIC_TYPE_P (type))
5543 switch (OMP_CLAUSE_REDUCTION_CODE (c))
5545 case PLUS_EXPR:
5546 case MULT_EXPR:
5547 case MINUS_EXPR:
5548 predefined = true;
5549 break;
5550 case MIN_EXPR:
5551 case MAX_EXPR:
5552 if (TREE_CODE (type) == COMPLEX_TYPE)
5553 break;
5554 predefined = true;
5555 break;
5556 case BIT_AND_EXPR:
5557 case BIT_IOR_EXPR:
5558 case BIT_XOR_EXPR:
5559 if (FLOAT_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
5560 break;
5561 predefined = true;
5562 break;
5563 case TRUTH_ANDIF_EXPR:
5564 case TRUTH_ORIF_EXPR:
5565 if (FLOAT_TYPE_P (type))
5566 break;
5567 predefined = true;
5568 break;
5569 default:
5570 break;
5572 else if (TYPE_READONLY (type))
5574 error ("%qE has const type for %<reduction%>",
5575 omp_clause_printable_decl (t));
5576 return true;
5578 else if (!processing_template_decl)
5580 t = require_complete_type (t);
5581 if (t == error_mark_node)
5582 return true;
5583 OMP_CLAUSE_DECL (c) = t;
5586 if (predefined)
5588 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = NULL_TREE;
5589 return false;
5591 else if (processing_template_decl)
5592 return false;
5594 tree id = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
5596 type = TYPE_MAIN_VARIANT (type);
5597 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = NULL_TREE;
5598 if (id == NULL_TREE)
5599 id = omp_reduction_id (OMP_CLAUSE_REDUCTION_CODE (c),
5600 NULL_TREE, NULL_TREE);
5601 id = omp_reduction_lookup (OMP_CLAUSE_LOCATION (c), id, type, NULL, NULL);
5602 if (id)
5604 if (id == error_mark_node)
5605 return true;
5606 id = OVL_CURRENT (id);
5607 mark_used (id);
5608 tree body = DECL_SAVED_TREE (id);
5609 if (!body)
5610 return true;
5611 if (TREE_CODE (body) == STATEMENT_LIST)
5613 tree_stmt_iterator tsi;
5614 tree placeholder = NULL_TREE, decl_placeholder = NULL_TREE;
5615 int i;
5616 tree stmts[7];
5617 tree atype = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (id)));
5618 atype = TREE_TYPE (atype);
5619 bool need_static_cast = !same_type_p (type, atype);
5620 memset (stmts, 0, sizeof stmts);
5621 for (i = 0, tsi = tsi_start (body);
5622 i < 7 && !tsi_end_p (tsi);
5623 i++, tsi_next (&tsi))
5624 stmts[i] = tsi_stmt (tsi);
5625 gcc_assert (tsi_end_p (tsi));
5627 if (i >= 3)
5629 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
5630 && TREE_CODE (stmts[1]) == DECL_EXPR);
5631 placeholder = build_lang_decl (VAR_DECL, NULL_TREE, type);
5632 DECL_ARTIFICIAL (placeholder) = 1;
5633 DECL_IGNORED_P (placeholder) = 1;
5634 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
5635 if (TREE_CODE (t) == MEM_REF)
5637 decl_placeholder = build_lang_decl (VAR_DECL, NULL_TREE,
5638 type);
5639 DECL_ARTIFICIAL (decl_placeholder) = 1;
5640 DECL_IGNORED_P (decl_placeholder) = 1;
5641 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c) = decl_placeholder;
5643 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[0])))
5644 cxx_mark_addressable (placeholder);
5645 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[1]))
5646 && TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c)))
5647 != REFERENCE_TYPE)
5648 cxx_mark_addressable (decl_placeholder ? decl_placeholder
5649 : OMP_CLAUSE_DECL (c));
5650 tree omp_out = placeholder;
5651 tree omp_in = decl_placeholder ? decl_placeholder
5652 : convert_from_reference (OMP_CLAUSE_DECL (c));
5653 if (need_static_cast)
5655 tree rtype = build_reference_type (atype);
5656 omp_out = build_static_cast (rtype, omp_out,
5657 tf_warning_or_error);
5658 omp_in = build_static_cast (rtype, omp_in,
5659 tf_warning_or_error);
5660 if (omp_out == error_mark_node || omp_in == error_mark_node)
5661 return true;
5662 omp_out = convert_from_reference (omp_out);
5663 omp_in = convert_from_reference (omp_in);
5665 OMP_CLAUSE_REDUCTION_MERGE (c)
5666 = clone_omp_udr (stmts[2], DECL_EXPR_DECL (stmts[0]),
5667 DECL_EXPR_DECL (stmts[1]), omp_in, omp_out);
5669 if (i >= 6)
5671 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
5672 && TREE_CODE (stmts[4]) == DECL_EXPR);
5673 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[3])))
5674 cxx_mark_addressable (decl_placeholder ? decl_placeholder
5675 : OMP_CLAUSE_DECL (c));
5676 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[4])))
5677 cxx_mark_addressable (placeholder);
5678 tree omp_priv = decl_placeholder ? decl_placeholder
5679 : convert_from_reference (OMP_CLAUSE_DECL (c));
5680 tree omp_orig = placeholder;
5681 if (need_static_cast)
5683 if (i == 7)
5685 error_at (OMP_CLAUSE_LOCATION (c),
5686 "user defined reduction with constructor "
5687 "initializer for base class %qT", atype);
5688 return true;
5690 tree rtype = build_reference_type (atype);
5691 omp_priv = build_static_cast (rtype, omp_priv,
5692 tf_warning_or_error);
5693 omp_orig = build_static_cast (rtype, omp_orig,
5694 tf_warning_or_error);
5695 if (omp_priv == error_mark_node
5696 || omp_orig == error_mark_node)
5697 return true;
5698 omp_priv = convert_from_reference (omp_priv);
5699 omp_orig = convert_from_reference (omp_orig);
5701 if (i == 6)
5702 *need_default_ctor = true;
5703 OMP_CLAUSE_REDUCTION_INIT (c)
5704 = clone_omp_udr (stmts[5], DECL_EXPR_DECL (stmts[4]),
5705 DECL_EXPR_DECL (stmts[3]),
5706 omp_priv, omp_orig);
5707 if (cp_walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
5708 find_omp_placeholder_r, placeholder, NULL))
5709 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
5711 else if (i >= 3)
5713 if (CLASS_TYPE_P (type) && !pod_type_p (type))
5714 *need_default_ctor = true;
5715 else
5717 tree init;
5718 tree v = decl_placeholder ? decl_placeholder
5719 : convert_from_reference (t);
5720 if (AGGREGATE_TYPE_P (TREE_TYPE (v)))
5721 init = build_constructor (TREE_TYPE (v), NULL);
5722 else
5723 init = fold_convert (TREE_TYPE (v), integer_zero_node);
5724 OMP_CLAUSE_REDUCTION_INIT (c)
5725 = build2 (INIT_EXPR, TREE_TYPE (v), v, init);
5730 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
5731 *need_dtor = true;
5732 else
5734 error ("user defined reduction not found for %qE",
5735 omp_clause_printable_decl (t));
5736 return true;
5738 if (TREE_CODE (OMP_CLAUSE_DECL (c)) == MEM_REF)
5739 gcc_assert (TYPE_SIZE_UNIT (type)
5740 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST);
5741 return false;
5744 /* Called from finish_struct_1. linear(this) or linear(this:step)
5745 clauses might not be finalized yet because the class has been incomplete
5746 when parsing #pragma omp declare simd methods. Fix those up now. */
5748 void
5749 finish_omp_declare_simd_methods (tree t)
5751 if (processing_template_decl)
5752 return;
5754 for (tree x = TYPE_METHODS (t); x; x = DECL_CHAIN (x))
5756 if (TREE_CODE (TREE_TYPE (x)) != METHOD_TYPE)
5757 continue;
5758 tree ods = lookup_attribute ("omp declare simd", DECL_ATTRIBUTES (x));
5759 if (!ods || !TREE_VALUE (ods))
5760 continue;
5761 for (tree c = TREE_VALUE (TREE_VALUE (ods)); c; c = OMP_CLAUSE_CHAIN (c))
5762 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
5763 && integer_zerop (OMP_CLAUSE_DECL (c))
5764 && OMP_CLAUSE_LINEAR_STEP (c)
5765 && TREE_CODE (TREE_TYPE (OMP_CLAUSE_LINEAR_STEP (c)))
5766 == POINTER_TYPE)
5768 tree s = OMP_CLAUSE_LINEAR_STEP (c);
5769 s = fold_convert_loc (OMP_CLAUSE_LOCATION (c), sizetype, s);
5770 s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MULT_EXPR,
5771 sizetype, s, TYPE_SIZE_UNIT (t));
5772 OMP_CLAUSE_LINEAR_STEP (c) = s;
5777 /* Adjust sink depend clause to take into account pointer offsets.
5779 Return TRUE if there was a problem processing the offset, and the
5780 whole clause should be removed. */
5782 static bool
5783 cp_finish_omp_clause_depend_sink (tree sink_clause)
5785 tree t = OMP_CLAUSE_DECL (sink_clause);
5786 gcc_assert (TREE_CODE (t) == TREE_LIST);
5788 /* Make sure we don't adjust things twice for templates. */
5789 if (processing_template_decl)
5790 return false;
5792 for (; t; t = TREE_CHAIN (t))
5794 tree decl = TREE_VALUE (t);
5795 if (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
5797 tree offset = TREE_PURPOSE (t);
5798 bool neg = wi::neg_p ((wide_int) offset);
5799 offset = fold_unary (ABS_EXPR, TREE_TYPE (offset), offset);
5800 decl = mark_rvalue_use (decl);
5801 decl = convert_from_reference (decl);
5802 tree t2 = pointer_int_sum (OMP_CLAUSE_LOCATION (sink_clause),
5803 neg ? MINUS_EXPR : PLUS_EXPR,
5804 decl, offset);
5805 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (sink_clause),
5806 MINUS_EXPR, sizetype,
5807 fold_convert (sizetype, t2),
5808 fold_convert (sizetype, decl));
5809 if (t2 == error_mark_node)
5810 return true;
5811 TREE_PURPOSE (t) = t2;
5814 return false;
5817 /* For all elements of CLAUSES, validate them vs OpenMP constraints.
5818 Remove any elements from the list that are invalid. */
5820 tree
5821 finish_omp_clauses (tree clauses, enum c_omp_region_type ort)
5823 bitmap_head generic_head, firstprivate_head, lastprivate_head;
5824 bitmap_head aligned_head, map_head, map_field_head, oacc_reduction_head;
5825 tree c, t, *pc;
5826 tree safelen = NULL_TREE;
5827 bool branch_seen = false;
5828 bool copyprivate_seen = false;
5829 bool ordered_seen = false;
5830 bool oacc_async = false;
5832 bitmap_obstack_initialize (NULL);
5833 bitmap_initialize (&generic_head, &bitmap_default_obstack);
5834 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
5835 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
5836 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
5837 bitmap_initialize (&map_head, &bitmap_default_obstack);
5838 bitmap_initialize (&map_field_head, &bitmap_default_obstack);
5839 bitmap_initialize (&oacc_reduction_head, &bitmap_default_obstack);
5841 if (ort & C_ORT_ACC)
5842 for (c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
5843 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ASYNC)
5845 oacc_async = true;
5846 break;
5849 for (pc = &clauses, c = clauses; c ; c = *pc)
5851 bool remove = false;
5852 bool field_ok = false;
5854 switch (OMP_CLAUSE_CODE (c))
5856 case OMP_CLAUSE_SHARED:
5857 field_ok = ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP);
5858 goto check_dup_generic;
5859 case OMP_CLAUSE_PRIVATE:
5860 field_ok = ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP);
5861 goto check_dup_generic;
5862 case OMP_CLAUSE_REDUCTION:
5863 field_ok = ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP);
5864 t = OMP_CLAUSE_DECL (c);
5865 if (TREE_CODE (t) == TREE_LIST)
5867 if (handle_omp_array_sections (c, ort))
5869 remove = true;
5870 break;
5872 if (TREE_CODE (t) == TREE_LIST)
5874 while (TREE_CODE (t) == TREE_LIST)
5875 t = TREE_CHAIN (t);
5877 else
5879 gcc_assert (TREE_CODE (t) == MEM_REF);
5880 t = TREE_OPERAND (t, 0);
5881 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
5882 t = TREE_OPERAND (t, 0);
5883 if (TREE_CODE (t) == ADDR_EXPR
5884 || TREE_CODE (t) == INDIRECT_REF)
5885 t = TREE_OPERAND (t, 0);
5887 tree n = omp_clause_decl_field (t);
5888 if (n)
5889 t = n;
5890 goto check_dup_generic_t;
5892 if (oacc_async)
5893 cxx_mark_addressable (t);
5894 goto check_dup_generic;
5895 case OMP_CLAUSE_COPYPRIVATE:
5896 copyprivate_seen = true;
5897 field_ok = ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP);
5898 goto check_dup_generic;
5899 case OMP_CLAUSE_COPYIN:
5900 goto check_dup_generic;
5901 case OMP_CLAUSE_LINEAR:
5902 field_ok = ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP);
5903 t = OMP_CLAUSE_DECL (c);
5904 if (ort != C_ORT_OMP_DECLARE_SIMD
5905 && OMP_CLAUSE_LINEAR_KIND (c) != OMP_CLAUSE_LINEAR_DEFAULT)
5907 error_at (OMP_CLAUSE_LOCATION (c),
5908 "modifier should not be specified in %<linear%> "
5909 "clause on %<simd%> or %<for%> constructs");
5910 OMP_CLAUSE_LINEAR_KIND (c) = OMP_CLAUSE_LINEAR_DEFAULT;
5912 if ((VAR_P (t) || TREE_CODE (t) == PARM_DECL)
5913 && !type_dependent_expression_p (t))
5915 tree type = TREE_TYPE (t);
5916 if ((OMP_CLAUSE_LINEAR_KIND (c) == OMP_CLAUSE_LINEAR_REF
5917 || OMP_CLAUSE_LINEAR_KIND (c) == OMP_CLAUSE_LINEAR_UVAL)
5918 && TREE_CODE (type) != REFERENCE_TYPE)
5920 error ("linear clause with %qs modifier applied to "
5921 "non-reference variable with %qT type",
5922 OMP_CLAUSE_LINEAR_KIND (c) == OMP_CLAUSE_LINEAR_REF
5923 ? "ref" : "uval", TREE_TYPE (t));
5924 remove = true;
5925 break;
5927 if (TREE_CODE (type) == REFERENCE_TYPE)
5928 type = TREE_TYPE (type);
5929 if (ort == C_ORT_CILK)
5931 if (!INTEGRAL_TYPE_P (type)
5932 && !SCALAR_FLOAT_TYPE_P (type)
5933 && TREE_CODE (type) != POINTER_TYPE)
5935 error ("linear clause applied to non-integral, "
5936 "non-floating, non-pointer variable with %qT type",
5937 TREE_TYPE (t));
5938 remove = true;
5939 break;
5942 else if (OMP_CLAUSE_LINEAR_KIND (c) != OMP_CLAUSE_LINEAR_REF)
5944 if (!INTEGRAL_TYPE_P (type)
5945 && TREE_CODE (type) != POINTER_TYPE)
5947 error ("linear clause applied to non-integral non-pointer"
5948 " variable with %qT type", TREE_TYPE (t));
5949 remove = true;
5950 break;
5954 t = OMP_CLAUSE_LINEAR_STEP (c);
5955 if (t == NULL_TREE)
5956 t = integer_one_node;
5957 if (t == error_mark_node)
5959 remove = true;
5960 break;
5962 else if (!type_dependent_expression_p (t)
5963 && !INTEGRAL_TYPE_P (TREE_TYPE (t))
5964 && (ort != C_ORT_OMP_DECLARE_SIMD
5965 || TREE_CODE (t) != PARM_DECL
5966 || TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE
5967 || !INTEGRAL_TYPE_P (TREE_TYPE (TREE_TYPE (t)))))
5969 error ("linear step expression must be integral");
5970 remove = true;
5971 break;
5973 else
5975 t = mark_rvalue_use (t);
5976 if (ort == C_ORT_OMP_DECLARE_SIMD && TREE_CODE (t) == PARM_DECL)
5978 OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c) = 1;
5979 goto check_dup_generic;
5981 if (!processing_template_decl
5982 && (VAR_P (OMP_CLAUSE_DECL (c))
5983 || TREE_CODE (OMP_CLAUSE_DECL (c)) == PARM_DECL))
5985 if (ort == C_ORT_OMP_DECLARE_SIMD)
5987 t = maybe_constant_value (t);
5988 if (TREE_CODE (t) != INTEGER_CST)
5990 error_at (OMP_CLAUSE_LOCATION (c),
5991 "%<linear%> clause step %qE is neither "
5992 "constant nor a parameter", t);
5993 remove = true;
5994 break;
5997 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5998 tree type = TREE_TYPE (OMP_CLAUSE_DECL (c));
5999 if (TREE_CODE (type) == REFERENCE_TYPE)
6000 type = TREE_TYPE (type);
6001 if (OMP_CLAUSE_LINEAR_KIND (c) == OMP_CLAUSE_LINEAR_REF)
6003 type = build_pointer_type (type);
6004 tree d = fold_convert (type, OMP_CLAUSE_DECL (c));
6005 t = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
6006 d, t);
6007 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c),
6008 MINUS_EXPR, sizetype,
6009 fold_convert (sizetype, t),
6010 fold_convert (sizetype, d));
6011 if (t == error_mark_node)
6013 remove = true;
6014 break;
6017 else if (TREE_CODE (type) == POINTER_TYPE
6018 /* Can't multiply the step yet if *this
6019 is still incomplete type. */
6020 && (ort != C_ORT_OMP_DECLARE_SIMD
6021 || TREE_CODE (OMP_CLAUSE_DECL (c)) != PARM_DECL
6022 || !DECL_ARTIFICIAL (OMP_CLAUSE_DECL (c))
6023 || DECL_NAME (OMP_CLAUSE_DECL (c))
6024 != this_identifier
6025 || !TYPE_BEING_DEFINED (TREE_TYPE (type))))
6027 tree d = convert_from_reference (OMP_CLAUSE_DECL (c));
6028 t = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
6029 d, t);
6030 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c),
6031 MINUS_EXPR, sizetype,
6032 fold_convert (sizetype, t),
6033 fold_convert (sizetype, d));
6034 if (t == error_mark_node)
6036 remove = true;
6037 break;
6040 else
6041 t = fold_convert (type, t);
6043 OMP_CLAUSE_LINEAR_STEP (c) = t;
6045 goto check_dup_generic;
6046 check_dup_generic:
6047 t = omp_clause_decl_field (OMP_CLAUSE_DECL (c));
6048 if (t)
6050 if (!remove && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_SHARED)
6051 omp_note_field_privatization (t, OMP_CLAUSE_DECL (c));
6053 else
6054 t = OMP_CLAUSE_DECL (c);
6055 check_dup_generic_t:
6056 if (t == current_class_ptr
6057 && (ort != C_ORT_OMP_DECLARE_SIMD
6058 || (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_LINEAR
6059 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_UNIFORM)))
6061 error ("%<this%> allowed in OpenMP only in %<declare simd%>"
6062 " clauses");
6063 remove = true;
6064 break;
6066 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL
6067 && (!field_ok || TREE_CODE (t) != FIELD_DECL))
6069 if (processing_template_decl)
6070 break;
6071 if (DECL_P (t))
6072 error ("%qD is not a variable in clause %qs", t,
6073 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6074 else
6075 error ("%qE is not a variable in clause %qs", t,
6076 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6077 remove = true;
6079 else if (ort == C_ORT_ACC
6080 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
6082 if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
6084 error ("%qD appears more than once in reduction clauses", t);
6085 remove = true;
6087 else
6088 bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
6090 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
6091 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
6092 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
6094 error ("%qD appears more than once in data clauses", t);
6095 remove = true;
6097 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
6098 && bitmap_bit_p (&map_head, DECL_UID (t)))
6100 if (ort == C_ORT_ACC)
6101 error ("%qD appears more than once in data clauses", t);
6102 else
6103 error ("%qD appears both in data and map clauses", t);
6104 remove = true;
6106 else
6107 bitmap_set_bit (&generic_head, DECL_UID (t));
6108 if (!field_ok)
6109 break;
6110 handle_field_decl:
6111 if (!remove
6112 && TREE_CODE (t) == FIELD_DECL
6113 && t == OMP_CLAUSE_DECL (c)
6114 && ort != C_ORT_ACC)
6116 OMP_CLAUSE_DECL (c)
6117 = omp_privatize_field (t, (OMP_CLAUSE_CODE (c)
6118 == OMP_CLAUSE_SHARED));
6119 if (OMP_CLAUSE_DECL (c) == error_mark_node)
6120 remove = true;
6122 break;
6124 case OMP_CLAUSE_FIRSTPRIVATE:
6125 t = omp_clause_decl_field (OMP_CLAUSE_DECL (c));
6126 if (t)
6127 omp_note_field_privatization (t, OMP_CLAUSE_DECL (c));
6128 else
6129 t = OMP_CLAUSE_DECL (c);
6130 if (ort != C_ORT_ACC && t == current_class_ptr)
6132 error ("%<this%> allowed in OpenMP only in %<declare simd%>"
6133 " clauses");
6134 remove = true;
6135 break;
6137 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL
6138 && ((ort & C_ORT_OMP_DECLARE_SIMD) != C_ORT_OMP
6139 || TREE_CODE (t) != FIELD_DECL))
6141 if (processing_template_decl)
6142 break;
6143 if (DECL_P (t))
6144 error ("%qD is not a variable in clause %<firstprivate%>", t);
6145 else
6146 error ("%qE is not a variable in clause %<firstprivate%>", t);
6147 remove = true;
6149 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
6150 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
6152 error ("%qD appears more than once in data clauses", t);
6153 remove = true;
6155 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
6157 if (ort == C_ORT_ACC)
6158 error ("%qD appears more than once in data clauses", t);
6159 else
6160 error ("%qD appears both in data and map clauses", t);
6161 remove = true;
6163 else
6164 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
6165 goto handle_field_decl;
6167 case OMP_CLAUSE_LASTPRIVATE:
6168 t = omp_clause_decl_field (OMP_CLAUSE_DECL (c));
6169 if (t)
6170 omp_note_field_privatization (t, OMP_CLAUSE_DECL (c));
6171 else
6172 t = OMP_CLAUSE_DECL (c);
6173 if (t == current_class_ptr)
6175 error ("%<this%> allowed in OpenMP only in %<declare simd%>"
6176 " clauses");
6177 remove = true;
6178 break;
6180 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL
6181 && ((ort & C_ORT_OMP_DECLARE_SIMD) != C_ORT_OMP
6182 || TREE_CODE (t) != FIELD_DECL))
6184 if (processing_template_decl)
6185 break;
6186 if (DECL_P (t))
6187 error ("%qD is not a variable in clause %<lastprivate%>", t);
6188 else
6189 error ("%qE is not a variable in clause %<lastprivate%>", t);
6190 remove = true;
6192 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
6193 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
6195 error ("%qD appears more than once in data clauses", t);
6196 remove = true;
6198 else
6199 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
6200 goto handle_field_decl;
6202 case OMP_CLAUSE_IF:
6203 t = OMP_CLAUSE_IF_EXPR (c);
6204 t = maybe_convert_cond (t);
6205 if (t == error_mark_node)
6206 remove = true;
6207 else if (!processing_template_decl)
6208 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6209 OMP_CLAUSE_IF_EXPR (c) = t;
6210 break;
6212 case OMP_CLAUSE_FINAL:
6213 t = OMP_CLAUSE_FINAL_EXPR (c);
6214 t = maybe_convert_cond (t);
6215 if (t == error_mark_node)
6216 remove = true;
6217 else if (!processing_template_decl)
6218 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6219 OMP_CLAUSE_FINAL_EXPR (c) = t;
6220 break;
6222 case OMP_CLAUSE_GANG:
6223 /* Operand 1 is the gang static: argument. */
6224 t = OMP_CLAUSE_OPERAND (c, 1);
6225 if (t != NULL_TREE)
6227 if (t == error_mark_node)
6228 remove = true;
6229 else if (!type_dependent_expression_p (t)
6230 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6232 error ("%<gang%> static expression must be integral");
6233 remove = true;
6235 else
6237 t = mark_rvalue_use (t);
6238 if (!processing_template_decl)
6240 t = maybe_constant_value (t);
6241 if (TREE_CODE (t) == INTEGER_CST
6242 && tree_int_cst_sgn (t) != 1
6243 && t != integer_minus_one_node)
6245 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6246 "%<gang%> static value must be "
6247 "positive");
6248 t = integer_one_node;
6251 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6253 OMP_CLAUSE_OPERAND (c, 1) = t;
6255 /* Check operand 0, the num argument. */
6256 /* FALLTHRU */
6258 case OMP_CLAUSE_WORKER:
6259 case OMP_CLAUSE_VECTOR:
6260 if (OMP_CLAUSE_OPERAND (c, 0) == NULL_TREE)
6261 break;
6262 /* FALLTHRU */
6264 case OMP_CLAUSE_NUM_TASKS:
6265 case OMP_CLAUSE_NUM_TEAMS:
6266 case OMP_CLAUSE_NUM_THREADS:
6267 case OMP_CLAUSE_NUM_GANGS:
6268 case OMP_CLAUSE_NUM_WORKERS:
6269 case OMP_CLAUSE_VECTOR_LENGTH:
6270 t = OMP_CLAUSE_OPERAND (c, 0);
6271 if (t == error_mark_node)
6272 remove = true;
6273 else if (!type_dependent_expression_p (t)
6274 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6276 switch (OMP_CLAUSE_CODE (c))
6278 case OMP_CLAUSE_GANG:
6279 error_at (OMP_CLAUSE_LOCATION (c),
6280 "%<gang%> num expression must be integral"); break;
6281 case OMP_CLAUSE_VECTOR:
6282 error_at (OMP_CLAUSE_LOCATION (c),
6283 "%<vector%> length expression must be integral");
6284 break;
6285 case OMP_CLAUSE_WORKER:
6286 error_at (OMP_CLAUSE_LOCATION (c),
6287 "%<worker%> num expression must be integral");
6288 break;
6289 default:
6290 error_at (OMP_CLAUSE_LOCATION (c),
6291 "%qs expression must be integral",
6292 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6294 remove = true;
6296 else
6298 t = mark_rvalue_use (t);
6299 if (!processing_template_decl)
6301 t = maybe_constant_value (t);
6302 if (TREE_CODE (t) == INTEGER_CST
6303 && tree_int_cst_sgn (t) != 1)
6305 switch (OMP_CLAUSE_CODE (c))
6307 case OMP_CLAUSE_GANG:
6308 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6309 "%<gang%> num value must be positive");
6310 break;
6311 case OMP_CLAUSE_VECTOR:
6312 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6313 "%<vector%> length value must be "
6314 "positive");
6315 break;
6316 case OMP_CLAUSE_WORKER:
6317 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6318 "%<worker%> num value must be "
6319 "positive");
6320 break;
6321 default:
6322 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6323 "%qs value must be positive",
6324 omp_clause_code_name
6325 [OMP_CLAUSE_CODE (c)]);
6327 t = integer_one_node;
6329 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6331 OMP_CLAUSE_OPERAND (c, 0) = t;
6333 break;
6335 case OMP_CLAUSE_SCHEDULE:
6336 if (OMP_CLAUSE_SCHEDULE_KIND (c) & OMP_CLAUSE_SCHEDULE_NONMONOTONIC)
6338 const char *p = NULL;
6339 switch (OMP_CLAUSE_SCHEDULE_KIND (c) & OMP_CLAUSE_SCHEDULE_MASK)
6341 case OMP_CLAUSE_SCHEDULE_STATIC: p = "static"; break;
6342 case OMP_CLAUSE_SCHEDULE_DYNAMIC: break;
6343 case OMP_CLAUSE_SCHEDULE_GUIDED: break;
6344 case OMP_CLAUSE_SCHEDULE_AUTO: p = "auto"; break;
6345 case OMP_CLAUSE_SCHEDULE_RUNTIME: p = "runtime"; break;
6346 default: gcc_unreachable ();
6348 if (p)
6350 error_at (OMP_CLAUSE_LOCATION (c),
6351 "%<nonmonotonic%> modifier specified for %qs "
6352 "schedule kind", p);
6353 OMP_CLAUSE_SCHEDULE_KIND (c)
6354 = (enum omp_clause_schedule_kind)
6355 (OMP_CLAUSE_SCHEDULE_KIND (c)
6356 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
6360 t = OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c);
6361 if (t == NULL)
6363 else if (t == error_mark_node)
6364 remove = true;
6365 else if (!type_dependent_expression_p (t)
6366 && (OMP_CLAUSE_SCHEDULE_KIND (c)
6367 != OMP_CLAUSE_SCHEDULE_CILKFOR)
6368 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6370 error ("schedule chunk size expression must be integral");
6371 remove = true;
6373 else
6375 t = mark_rvalue_use (t);
6376 if (!processing_template_decl)
6378 if (OMP_CLAUSE_SCHEDULE_KIND (c)
6379 == OMP_CLAUSE_SCHEDULE_CILKFOR)
6381 t = convert_to_integer (long_integer_type_node, t);
6382 if (t == error_mark_node)
6384 remove = true;
6385 break;
6388 else
6390 t = maybe_constant_value (t);
6391 if (TREE_CODE (t) == INTEGER_CST
6392 && tree_int_cst_sgn (t) != 1)
6394 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6395 "chunk size value must be positive");
6396 t = integer_one_node;
6399 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6401 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
6403 break;
6405 case OMP_CLAUSE_SIMDLEN:
6406 case OMP_CLAUSE_SAFELEN:
6407 t = OMP_CLAUSE_OPERAND (c, 0);
6408 if (t == error_mark_node)
6409 remove = true;
6410 else if (!type_dependent_expression_p (t)
6411 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6413 error ("%qs length expression must be integral",
6414 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6415 remove = true;
6417 else
6419 t = mark_rvalue_use (t);
6420 if (!processing_template_decl)
6422 t = maybe_constant_value (t);
6423 if (TREE_CODE (t) != INTEGER_CST
6424 || tree_int_cst_sgn (t) != 1)
6426 error ("%qs length expression must be positive constant"
6427 " integer expression",
6428 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6429 remove = true;
6432 OMP_CLAUSE_OPERAND (c, 0) = t;
6433 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SAFELEN)
6434 safelen = c;
6436 break;
6438 case OMP_CLAUSE_ASYNC:
6439 t = OMP_CLAUSE_ASYNC_EXPR (c);
6440 if (t == error_mark_node)
6441 remove = true;
6442 else if (!type_dependent_expression_p (t)
6443 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6445 error ("%<async%> expression must be integral");
6446 remove = true;
6448 else
6450 t = mark_rvalue_use (t);
6451 if (!processing_template_decl)
6452 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6453 OMP_CLAUSE_ASYNC_EXPR (c) = t;
6455 break;
6457 case OMP_CLAUSE_WAIT:
6458 t = OMP_CLAUSE_WAIT_EXPR (c);
6459 if (t == error_mark_node)
6460 remove = true;
6461 else if (!processing_template_decl)
6462 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6463 OMP_CLAUSE_WAIT_EXPR (c) = t;
6464 break;
6466 case OMP_CLAUSE_THREAD_LIMIT:
6467 t = OMP_CLAUSE_THREAD_LIMIT_EXPR (c);
6468 if (t == error_mark_node)
6469 remove = true;
6470 else if (!type_dependent_expression_p (t)
6471 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6473 error ("%<thread_limit%> expression must be integral");
6474 remove = true;
6476 else
6478 t = mark_rvalue_use (t);
6479 if (!processing_template_decl)
6481 t = maybe_constant_value (t);
6482 if (TREE_CODE (t) == INTEGER_CST
6483 && tree_int_cst_sgn (t) != 1)
6485 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6486 "%<thread_limit%> value must be positive");
6487 t = integer_one_node;
6489 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6491 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
6493 break;
6495 case OMP_CLAUSE_DEVICE:
6496 t = OMP_CLAUSE_DEVICE_ID (c);
6497 if (t == error_mark_node)
6498 remove = true;
6499 else if (!type_dependent_expression_p (t)
6500 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6502 error ("%<device%> id must be integral");
6503 remove = true;
6505 else
6507 t = mark_rvalue_use (t);
6508 if (!processing_template_decl)
6509 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6510 OMP_CLAUSE_DEVICE_ID (c) = t;
6512 break;
6514 case OMP_CLAUSE_DIST_SCHEDULE:
6515 t = OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c);
6516 if (t == NULL)
6518 else if (t == error_mark_node)
6519 remove = true;
6520 else if (!type_dependent_expression_p (t)
6521 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6523 error ("%<dist_schedule%> chunk size expression must be "
6524 "integral");
6525 remove = true;
6527 else
6529 t = mark_rvalue_use (t);
6530 if (!processing_template_decl)
6531 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6532 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
6534 break;
6536 case OMP_CLAUSE_ALIGNED:
6537 t = OMP_CLAUSE_DECL (c);
6538 if (t == current_class_ptr && ort != C_ORT_OMP_DECLARE_SIMD)
6540 error ("%<this%> allowed in OpenMP only in %<declare simd%>"
6541 " clauses");
6542 remove = true;
6543 break;
6545 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
6547 if (processing_template_decl)
6548 break;
6549 if (DECL_P (t))
6550 error ("%qD is not a variable in %<aligned%> clause", t);
6551 else
6552 error ("%qE is not a variable in %<aligned%> clause", t);
6553 remove = true;
6555 else if (!type_dependent_expression_p (t)
6556 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE
6557 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE
6558 && (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE
6559 || (!POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (t)))
6560 && (TREE_CODE (TREE_TYPE (TREE_TYPE (t)))
6561 != ARRAY_TYPE))))
6563 error_at (OMP_CLAUSE_LOCATION (c),
6564 "%qE in %<aligned%> clause is neither a pointer nor "
6565 "an array nor a reference to pointer or array", t);
6566 remove = true;
6568 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
6570 error ("%qD appears more than once in %<aligned%> clauses", t);
6571 remove = true;
6573 else
6574 bitmap_set_bit (&aligned_head, DECL_UID (t));
6575 t = OMP_CLAUSE_ALIGNED_ALIGNMENT (c);
6576 if (t == error_mark_node)
6577 remove = true;
6578 else if (t == NULL_TREE)
6579 break;
6580 else if (!type_dependent_expression_p (t)
6581 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6583 error ("%<aligned%> clause alignment expression must "
6584 "be integral");
6585 remove = true;
6587 else
6589 t = mark_rvalue_use (t);
6590 if (!processing_template_decl)
6592 t = maybe_constant_value (t);
6593 if (TREE_CODE (t) != INTEGER_CST
6594 || tree_int_cst_sgn (t) != 1)
6596 error ("%<aligned%> clause alignment expression must be "
6597 "positive constant integer expression");
6598 remove = true;
6601 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = t;
6603 break;
6605 case OMP_CLAUSE_DEPEND:
6606 t = OMP_CLAUSE_DECL (c);
6607 if (t == NULL_TREE)
6609 gcc_assert (OMP_CLAUSE_DEPEND_KIND (c)
6610 == OMP_CLAUSE_DEPEND_SOURCE);
6611 break;
6613 if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SINK)
6615 if (cp_finish_omp_clause_depend_sink (c))
6616 remove = true;
6617 break;
6619 if (TREE_CODE (t) == TREE_LIST)
6621 if (handle_omp_array_sections (c, ort))
6622 remove = true;
6623 break;
6625 if (t == error_mark_node)
6626 remove = true;
6627 else if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
6629 if (processing_template_decl)
6630 break;
6631 if (DECL_P (t))
6632 error ("%qD is not a variable in %<depend%> clause", t);
6633 else
6634 error ("%qE is not a variable in %<depend%> clause", t);
6635 remove = true;
6637 else if (t == current_class_ptr)
6639 error ("%<this%> allowed in OpenMP only in %<declare simd%>"
6640 " clauses");
6641 remove = true;
6643 else if (!processing_template_decl
6644 && !cxx_mark_addressable (t))
6645 remove = true;
6646 break;
6648 case OMP_CLAUSE_MAP:
6649 case OMP_CLAUSE_TO:
6650 case OMP_CLAUSE_FROM:
6651 case OMP_CLAUSE__CACHE_:
6652 t = OMP_CLAUSE_DECL (c);
6653 if (TREE_CODE (t) == TREE_LIST)
6655 if (handle_omp_array_sections (c, ort))
6656 remove = true;
6657 else
6659 t = OMP_CLAUSE_DECL (c);
6660 if (TREE_CODE (t) != TREE_LIST
6661 && !type_dependent_expression_p (t)
6662 && !cp_omp_mappable_type (TREE_TYPE (t)))
6664 error_at (OMP_CLAUSE_LOCATION (c),
6665 "array section does not have mappable type "
6666 "in %qs clause",
6667 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6668 remove = true;
6670 while (TREE_CODE (t) == ARRAY_REF)
6671 t = TREE_OPERAND (t, 0);
6672 if (TREE_CODE (t) == COMPONENT_REF
6673 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
6675 while (TREE_CODE (t) == COMPONENT_REF)
6676 t = TREE_OPERAND (t, 0);
6677 if (REFERENCE_REF_P (t))
6678 t = TREE_OPERAND (t, 0);
6679 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
6680 break;
6681 if (bitmap_bit_p (&map_head, DECL_UID (t)))
6683 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
6684 error ("%qD appears more than once in motion"
6685 " clauses", t);
6686 else if (ort == C_ORT_ACC)
6687 error ("%qD appears more than once in data"
6688 " clauses", t);
6689 else
6690 error ("%qD appears more than once in map"
6691 " clauses", t);
6692 remove = true;
6694 else
6696 bitmap_set_bit (&map_head, DECL_UID (t));
6697 bitmap_set_bit (&map_field_head, DECL_UID (t));
6701 break;
6703 if (t == error_mark_node)
6705 remove = true;
6706 break;
6708 if (REFERENCE_REF_P (t)
6709 && TREE_CODE (TREE_OPERAND (t, 0)) == COMPONENT_REF)
6711 t = TREE_OPERAND (t, 0);
6712 OMP_CLAUSE_DECL (c) = t;
6714 if (TREE_CODE (t) == COMPONENT_REF
6715 && (ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP
6716 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE__CACHE_)
6718 if (type_dependent_expression_p (t))
6719 break;
6720 if (TREE_CODE (TREE_OPERAND (t, 1)) == FIELD_DECL
6721 && DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
6723 error_at (OMP_CLAUSE_LOCATION (c),
6724 "bit-field %qE in %qs clause",
6725 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6726 remove = true;
6728 else if (!cp_omp_mappable_type (TREE_TYPE (t)))
6730 error_at (OMP_CLAUSE_LOCATION (c),
6731 "%qE does not have a mappable type in %qs clause",
6732 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6733 remove = true;
6735 while (TREE_CODE (t) == COMPONENT_REF)
6737 if (TREE_TYPE (TREE_OPERAND (t, 0))
6738 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0)))
6739 == UNION_TYPE))
6741 error_at (OMP_CLAUSE_LOCATION (c),
6742 "%qE is a member of a union", t);
6743 remove = true;
6744 break;
6746 t = TREE_OPERAND (t, 0);
6748 if (remove)
6749 break;
6750 if (REFERENCE_REF_P (t))
6751 t = TREE_OPERAND (t, 0);
6752 if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
6754 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
6755 goto handle_map_references;
6758 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
6760 if (processing_template_decl)
6761 break;
6762 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
6763 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
6764 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ALWAYS_POINTER))
6765 break;
6766 if (DECL_P (t))
6767 error ("%qD is not a variable in %qs clause", t,
6768 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6769 else
6770 error ("%qE is not a variable in %qs clause", t,
6771 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6772 remove = true;
6774 else if (VAR_P (t) && CP_DECL_THREAD_LOCAL_P (t))
6776 error ("%qD is threadprivate variable in %qs clause", t,
6777 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6778 remove = true;
6780 else if (ort != C_ORT_ACC && t == current_class_ptr)
6782 error ("%<this%> allowed in OpenMP only in %<declare simd%>"
6783 " clauses");
6784 remove = true;
6785 break;
6787 else if (!processing_template_decl
6788 && TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE
6789 && (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
6790 || (OMP_CLAUSE_MAP_KIND (c)
6791 != GOMP_MAP_FIRSTPRIVATE_POINTER))
6792 && !cxx_mark_addressable (t))
6793 remove = true;
6794 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
6795 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
6796 || (OMP_CLAUSE_MAP_KIND (c)
6797 == GOMP_MAP_FIRSTPRIVATE_POINTER)))
6798 && t == OMP_CLAUSE_DECL (c)
6799 && !type_dependent_expression_p (t)
6800 && !cp_omp_mappable_type ((TREE_CODE (TREE_TYPE (t))
6801 == REFERENCE_TYPE)
6802 ? TREE_TYPE (TREE_TYPE (t))
6803 : TREE_TYPE (t)))
6805 error_at (OMP_CLAUSE_LOCATION (c),
6806 "%qD does not have a mappable type in %qs clause", t,
6807 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6808 remove = true;
6810 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
6811 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FORCE_DEVICEPTR
6812 && !type_dependent_expression_p (t)
6813 && !POINTER_TYPE_P (TREE_TYPE (t)))
6815 error ("%qD is not a pointer variable", t);
6816 remove = true;
6818 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
6819 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
6821 if (bitmap_bit_p (&generic_head, DECL_UID (t))
6822 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
6824 error ("%qD appears more than once in data clauses", t);
6825 remove = true;
6827 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
6829 if (ort == C_ORT_ACC)
6830 error ("%qD appears more than once in data clauses", t);
6831 else
6832 error ("%qD appears both in data and map clauses", t);
6833 remove = true;
6835 else
6836 bitmap_set_bit (&generic_head, DECL_UID (t));
6838 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
6840 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
6841 error ("%qD appears more than once in motion clauses", t);
6842 if (ort == C_ORT_ACC)
6843 error ("%qD appears more than once in data clauses", t);
6844 else
6845 error ("%qD appears more than once in map clauses", t);
6846 remove = true;
6848 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
6849 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
6851 if (ort == C_ORT_ACC)
6852 error ("%qD appears more than once in data clauses", t);
6853 else
6854 error ("%qD appears both in data and map clauses", t);
6855 remove = true;
6857 else
6859 bitmap_set_bit (&map_head, DECL_UID (t));
6860 if (t != OMP_CLAUSE_DECL (c)
6861 && TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPONENT_REF)
6862 bitmap_set_bit (&map_field_head, DECL_UID (t));
6864 handle_map_references:
6865 if (!remove
6866 && !processing_template_decl
6867 && (ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP
6868 && TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c))) == REFERENCE_TYPE)
6870 t = OMP_CLAUSE_DECL (c);
6871 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
6873 OMP_CLAUSE_DECL (c) = build_simple_mem_ref (t);
6874 if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
6875 OMP_CLAUSE_SIZE (c)
6876 = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t)));
6878 else if (OMP_CLAUSE_MAP_KIND (c)
6879 != GOMP_MAP_FIRSTPRIVATE_POINTER
6880 && (OMP_CLAUSE_MAP_KIND (c)
6881 != GOMP_MAP_FIRSTPRIVATE_REFERENCE)
6882 && (OMP_CLAUSE_MAP_KIND (c)
6883 != GOMP_MAP_ALWAYS_POINTER))
6885 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
6886 OMP_CLAUSE_MAP);
6887 if (TREE_CODE (t) == COMPONENT_REF)
6888 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ALWAYS_POINTER);
6889 else
6890 OMP_CLAUSE_SET_MAP_KIND (c2,
6891 GOMP_MAP_FIRSTPRIVATE_REFERENCE);
6892 OMP_CLAUSE_DECL (c2) = t;
6893 OMP_CLAUSE_SIZE (c2) = size_zero_node;
6894 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
6895 OMP_CLAUSE_CHAIN (c) = c2;
6896 OMP_CLAUSE_DECL (c) = build_simple_mem_ref (t);
6897 if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
6898 OMP_CLAUSE_SIZE (c)
6899 = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t)));
6900 c = c2;
6903 break;
6905 case OMP_CLAUSE_TO_DECLARE:
6906 case OMP_CLAUSE_LINK:
6907 t = OMP_CLAUSE_DECL (c);
6908 if (TREE_CODE (t) == FUNCTION_DECL
6909 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
6911 else if (!VAR_P (t))
6913 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
6915 if (TREE_CODE (t) == OVERLOAD && OVL_CHAIN (t))
6916 error_at (OMP_CLAUSE_LOCATION (c),
6917 "overloaded function name %qE in clause %qs", t,
6918 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6919 else if (TREE_CODE (t) == TEMPLATE_ID_EXPR)
6920 error_at (OMP_CLAUSE_LOCATION (c),
6921 "template %qE in clause %qs", t,
6922 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6923 else
6924 error_at (OMP_CLAUSE_LOCATION (c),
6925 "%qE is neither a variable nor a function name "
6926 "in clause %qs", t,
6927 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6929 else
6930 error_at (OMP_CLAUSE_LOCATION (c),
6931 "%qE is not a variable in clause %qs", t,
6932 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6933 remove = true;
6935 else if (DECL_THREAD_LOCAL_P (t))
6937 error_at (OMP_CLAUSE_LOCATION (c),
6938 "%qD is threadprivate variable in %qs clause", t,
6939 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6940 remove = true;
6942 else if (!cp_omp_mappable_type (TREE_TYPE (t)))
6944 error_at (OMP_CLAUSE_LOCATION (c),
6945 "%qD does not have a mappable type in %qs clause", t,
6946 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6947 remove = true;
6949 if (remove)
6950 break;
6951 if (bitmap_bit_p (&generic_head, DECL_UID (t)))
6953 error_at (OMP_CLAUSE_LOCATION (c),
6954 "%qE appears more than once on the same "
6955 "%<declare target%> directive", t);
6956 remove = true;
6958 else
6959 bitmap_set_bit (&generic_head, DECL_UID (t));
6960 break;
6962 case OMP_CLAUSE_UNIFORM:
6963 t = OMP_CLAUSE_DECL (c);
6964 if (TREE_CODE (t) != PARM_DECL)
6966 if (processing_template_decl)
6967 break;
6968 if (DECL_P (t))
6969 error ("%qD is not an argument in %<uniform%> clause", t);
6970 else
6971 error ("%qE is not an argument in %<uniform%> clause", t);
6972 remove = true;
6973 break;
6975 /* map_head bitmap is used as uniform_head if declare_simd. */
6976 bitmap_set_bit (&map_head, DECL_UID (t));
6977 goto check_dup_generic;
6979 case OMP_CLAUSE_GRAINSIZE:
6980 t = OMP_CLAUSE_GRAINSIZE_EXPR (c);
6981 if (t == error_mark_node)
6982 remove = true;
6983 else if (!type_dependent_expression_p (t)
6984 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6986 error ("%<grainsize%> expression must be integral");
6987 remove = true;
6989 else
6991 t = mark_rvalue_use (t);
6992 if (!processing_template_decl)
6994 t = maybe_constant_value (t);
6995 if (TREE_CODE (t) == INTEGER_CST
6996 && tree_int_cst_sgn (t) != 1)
6998 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6999 "%<grainsize%> value must be positive");
7000 t = integer_one_node;
7002 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7004 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
7006 break;
7008 case OMP_CLAUSE_PRIORITY:
7009 t = OMP_CLAUSE_PRIORITY_EXPR (c);
7010 if (t == error_mark_node)
7011 remove = true;
7012 else if (!type_dependent_expression_p (t)
7013 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
7015 error ("%<priority%> expression must be integral");
7016 remove = true;
7018 else
7020 t = mark_rvalue_use (t);
7021 if (!processing_template_decl)
7023 t = maybe_constant_value (t);
7024 if (TREE_CODE (t) == INTEGER_CST
7025 && tree_int_cst_sgn (t) == -1)
7027 warning_at (OMP_CLAUSE_LOCATION (c), 0,
7028 "%<priority%> value must be non-negative");
7029 t = integer_one_node;
7031 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7033 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
7035 break;
7037 case OMP_CLAUSE_HINT:
7038 t = OMP_CLAUSE_HINT_EXPR (c);
7039 if (t == error_mark_node)
7040 remove = true;
7041 else if (!type_dependent_expression_p (t)
7042 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
7044 error ("%<num_tasks%> expression must be integral");
7045 remove = true;
7047 else
7049 t = mark_rvalue_use (t);
7050 if (!processing_template_decl)
7052 t = maybe_constant_value (t);
7053 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7055 OMP_CLAUSE_HINT_EXPR (c) = t;
7057 break;
7059 case OMP_CLAUSE_IS_DEVICE_PTR:
7060 case OMP_CLAUSE_USE_DEVICE_PTR:
7061 field_ok = (ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP;
7062 t = OMP_CLAUSE_DECL (c);
7063 if (!type_dependent_expression_p (t))
7065 tree type = TREE_TYPE (t);
7066 if (TREE_CODE (type) != POINTER_TYPE
7067 && TREE_CODE (type) != ARRAY_TYPE
7068 && (TREE_CODE (type) != REFERENCE_TYPE
7069 || (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
7070 && TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE)))
7072 error_at (OMP_CLAUSE_LOCATION (c),
7073 "%qs variable is neither a pointer, nor an array "
7074 "nor reference to pointer or array",
7075 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7076 remove = true;
7079 goto check_dup_generic;
7081 case OMP_CLAUSE_NOWAIT:
7082 case OMP_CLAUSE_DEFAULT:
7083 case OMP_CLAUSE_UNTIED:
7084 case OMP_CLAUSE_COLLAPSE:
7085 case OMP_CLAUSE_MERGEABLE:
7086 case OMP_CLAUSE_PARALLEL:
7087 case OMP_CLAUSE_FOR:
7088 case OMP_CLAUSE_SECTIONS:
7089 case OMP_CLAUSE_TASKGROUP:
7090 case OMP_CLAUSE_PROC_BIND:
7091 case OMP_CLAUSE_NOGROUP:
7092 case OMP_CLAUSE_THREADS:
7093 case OMP_CLAUSE_SIMD:
7094 case OMP_CLAUSE_DEFAULTMAP:
7095 case OMP_CLAUSE__CILK_FOR_COUNT_:
7096 case OMP_CLAUSE_AUTO:
7097 case OMP_CLAUSE_INDEPENDENT:
7098 case OMP_CLAUSE_SEQ:
7099 break;
7101 case OMP_CLAUSE_TILE:
7102 for (tree list = OMP_CLAUSE_TILE_LIST (c); !remove && list;
7103 list = TREE_CHAIN (list))
7105 t = TREE_VALUE (list);
7107 if (t == error_mark_node)
7108 remove = true;
7109 else if (!type_dependent_expression_p (t)
7110 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
7112 error_at (OMP_CLAUSE_LOCATION (c),
7113 "%<tile%> argument needs integral type");
7114 remove = true;
7116 else
7118 t = mark_rvalue_use (t);
7119 if (!processing_template_decl)
7121 /* Zero is used to indicate '*', we permit you
7122 to get there via an ICE of value zero. */
7123 t = maybe_constant_value (t);
7124 if (!tree_fits_shwi_p (t)
7125 || tree_to_shwi (t) < 0)
7127 error_at (OMP_CLAUSE_LOCATION (c),
7128 "%<tile%> argument needs positive "
7129 "integral constant");
7130 remove = true;
7133 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7136 /* Update list item. */
7137 TREE_VALUE (list) = t;
7139 break;
7141 case OMP_CLAUSE_ORDERED:
7142 ordered_seen = true;
7143 break;
7145 case OMP_CLAUSE_INBRANCH:
7146 case OMP_CLAUSE_NOTINBRANCH:
7147 if (branch_seen)
7149 error ("%<inbranch%> clause is incompatible with "
7150 "%<notinbranch%>");
7151 remove = true;
7153 branch_seen = true;
7154 break;
7156 default:
7157 gcc_unreachable ();
7160 if (remove)
7161 *pc = OMP_CLAUSE_CHAIN (c);
7162 else
7163 pc = &OMP_CLAUSE_CHAIN (c);
7166 for (pc = &clauses, c = clauses; c ; c = *pc)
7168 enum omp_clause_code c_kind = OMP_CLAUSE_CODE (c);
7169 bool remove = false;
7170 bool need_complete_type = false;
7171 bool need_default_ctor = false;
7172 bool need_copy_ctor = false;
7173 bool need_copy_assignment = false;
7174 bool need_implicitly_determined = false;
7175 bool need_dtor = false;
7176 tree type, inner_type;
7178 switch (c_kind)
7180 case OMP_CLAUSE_SHARED:
7181 need_implicitly_determined = true;
7182 break;
7183 case OMP_CLAUSE_PRIVATE:
7184 need_complete_type = true;
7185 need_default_ctor = true;
7186 need_dtor = true;
7187 need_implicitly_determined = true;
7188 break;
7189 case OMP_CLAUSE_FIRSTPRIVATE:
7190 need_complete_type = true;
7191 need_copy_ctor = true;
7192 need_dtor = true;
7193 need_implicitly_determined = true;
7194 break;
7195 case OMP_CLAUSE_LASTPRIVATE:
7196 need_complete_type = true;
7197 need_copy_assignment = true;
7198 need_implicitly_determined = true;
7199 break;
7200 case OMP_CLAUSE_REDUCTION:
7201 need_implicitly_determined = true;
7202 break;
7203 case OMP_CLAUSE_LINEAR:
7204 if (ort != C_ORT_OMP_DECLARE_SIMD)
7205 need_implicitly_determined = true;
7206 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c)
7207 && !bitmap_bit_p (&map_head,
7208 DECL_UID (OMP_CLAUSE_LINEAR_STEP (c))))
7210 error_at (OMP_CLAUSE_LOCATION (c),
7211 "%<linear%> clause step is a parameter %qD not "
7212 "specified in %<uniform%> clause",
7213 OMP_CLAUSE_LINEAR_STEP (c));
7214 *pc = OMP_CLAUSE_CHAIN (c);
7215 continue;
7217 break;
7218 case OMP_CLAUSE_COPYPRIVATE:
7219 need_copy_assignment = true;
7220 break;
7221 case OMP_CLAUSE_COPYIN:
7222 need_copy_assignment = true;
7223 break;
7224 case OMP_CLAUSE_SIMDLEN:
7225 if (safelen
7226 && !processing_template_decl
7227 && tree_int_cst_lt (OMP_CLAUSE_SAFELEN_EXPR (safelen),
7228 OMP_CLAUSE_SIMDLEN_EXPR (c)))
7230 error_at (OMP_CLAUSE_LOCATION (c),
7231 "%<simdlen%> clause value is bigger than "
7232 "%<safelen%> clause value");
7233 OMP_CLAUSE_SIMDLEN_EXPR (c)
7234 = OMP_CLAUSE_SAFELEN_EXPR (safelen);
7236 pc = &OMP_CLAUSE_CHAIN (c);
7237 continue;
7238 case OMP_CLAUSE_SCHEDULE:
7239 if (ordered_seen
7240 && (OMP_CLAUSE_SCHEDULE_KIND (c)
7241 & OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
7243 error_at (OMP_CLAUSE_LOCATION (c),
7244 "%<nonmonotonic%> schedule modifier specified "
7245 "together with %<ordered%> clause");
7246 OMP_CLAUSE_SCHEDULE_KIND (c)
7247 = (enum omp_clause_schedule_kind)
7248 (OMP_CLAUSE_SCHEDULE_KIND (c)
7249 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
7251 pc = &OMP_CLAUSE_CHAIN (c);
7252 continue;
7253 case OMP_CLAUSE_NOWAIT:
7254 if (copyprivate_seen)
7256 error_at (OMP_CLAUSE_LOCATION (c),
7257 "%<nowait%> clause must not be used together "
7258 "with %<copyprivate%>");
7259 *pc = OMP_CLAUSE_CHAIN (c);
7260 continue;
7262 /* FALLTHRU */
7263 default:
7264 pc = &OMP_CLAUSE_CHAIN (c);
7265 continue;
7268 t = OMP_CLAUSE_DECL (c);
7269 if (processing_template_decl
7270 && !VAR_P (t) && TREE_CODE (t) != PARM_DECL)
7272 pc = &OMP_CLAUSE_CHAIN (c);
7273 continue;
7276 switch (c_kind)
7278 case OMP_CLAUSE_LASTPRIVATE:
7279 if (!bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
7281 need_default_ctor = true;
7282 need_dtor = true;
7284 break;
7286 case OMP_CLAUSE_REDUCTION:
7287 if (finish_omp_reduction_clause (c, &need_default_ctor,
7288 &need_dtor))
7289 remove = true;
7290 else
7291 t = OMP_CLAUSE_DECL (c);
7292 break;
7294 case OMP_CLAUSE_COPYIN:
7295 if (!VAR_P (t) || !CP_DECL_THREAD_LOCAL_P (t))
7297 error ("%qE must be %<threadprivate%> for %<copyin%>", t);
7298 remove = true;
7300 break;
7302 default:
7303 break;
7306 if (need_complete_type || need_copy_assignment)
7308 t = require_complete_type (t);
7309 if (t == error_mark_node)
7310 remove = true;
7311 else if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE
7312 && !complete_type_or_else (TREE_TYPE (TREE_TYPE (t)), t))
7313 remove = true;
7315 if (need_implicitly_determined)
7317 const char *share_name = NULL;
7319 if (VAR_P (t) && CP_DECL_THREAD_LOCAL_P (t))
7320 share_name = "threadprivate";
7321 else switch (cxx_omp_predetermined_sharing (t))
7323 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
7324 break;
7325 case OMP_CLAUSE_DEFAULT_SHARED:
7326 /* const vars may be specified in firstprivate clause. */
7327 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
7328 && cxx_omp_const_qual_no_mutable (t))
7329 break;
7330 share_name = "shared";
7331 break;
7332 case OMP_CLAUSE_DEFAULT_PRIVATE:
7333 share_name = "private";
7334 break;
7335 default:
7336 gcc_unreachable ();
7338 if (share_name)
7340 error ("%qE is predetermined %qs for %qs",
7341 omp_clause_printable_decl (t), share_name,
7342 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7343 remove = true;
7347 /* We're interested in the base element, not arrays. */
7348 inner_type = type = TREE_TYPE (t);
7349 if ((need_complete_type
7350 || need_copy_assignment
7351 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
7352 && TREE_CODE (inner_type) == REFERENCE_TYPE)
7353 inner_type = TREE_TYPE (inner_type);
7354 while (TREE_CODE (inner_type) == ARRAY_TYPE)
7355 inner_type = TREE_TYPE (inner_type);
7357 /* Check for special function availability by building a call to one.
7358 Save the results, because later we won't be in the right context
7359 for making these queries. */
7360 if (CLASS_TYPE_P (inner_type)
7361 && COMPLETE_TYPE_P (inner_type)
7362 && (need_default_ctor || need_copy_ctor
7363 || need_copy_assignment || need_dtor)
7364 && !type_dependent_expression_p (t)
7365 && cxx_omp_create_clause_info (c, inner_type, need_default_ctor,
7366 need_copy_ctor, need_copy_assignment,
7367 need_dtor))
7368 remove = true;
7370 if (!remove
7371 && c_kind == OMP_CLAUSE_SHARED
7372 && processing_template_decl)
7374 t = omp_clause_decl_field (OMP_CLAUSE_DECL (c));
7375 if (t)
7376 OMP_CLAUSE_DECL (c) = t;
7379 if (remove)
7380 *pc = OMP_CLAUSE_CHAIN (c);
7381 else
7382 pc = &OMP_CLAUSE_CHAIN (c);
7385 bitmap_obstack_release (NULL);
7386 return clauses;
7389 /* Start processing OpenMP clauses that can include any
7390 privatization clauses for non-static data members. */
7392 tree
7393 push_omp_privatization_clauses (bool ignore_next)
7395 if (omp_private_member_ignore_next)
7397 omp_private_member_ignore_next = ignore_next;
7398 return NULL_TREE;
7400 omp_private_member_ignore_next = ignore_next;
7401 if (omp_private_member_map)
7402 omp_private_member_vec.safe_push (error_mark_node);
7403 return push_stmt_list ();
7406 /* Revert remapping of any non-static data members since
7407 the last push_omp_privatization_clauses () call. */
7409 void
7410 pop_omp_privatization_clauses (tree stmt)
7412 if (stmt == NULL_TREE)
7413 return;
7414 stmt = pop_stmt_list (stmt);
7415 if (omp_private_member_map)
7417 while (!omp_private_member_vec.is_empty ())
7419 tree t = omp_private_member_vec.pop ();
7420 if (t == error_mark_node)
7422 add_stmt (stmt);
7423 return;
7425 bool no_decl_expr = t == integer_zero_node;
7426 if (no_decl_expr)
7427 t = omp_private_member_vec.pop ();
7428 tree *v = omp_private_member_map->get (t);
7429 gcc_assert (v);
7430 if (!no_decl_expr)
7431 add_decl_expr (*v);
7432 omp_private_member_map->remove (t);
7434 delete omp_private_member_map;
7435 omp_private_member_map = NULL;
7437 add_stmt (stmt);
7440 /* Remember OpenMP privatization clauses mapping and clear it.
7441 Used for lambdas. */
7443 void
7444 save_omp_privatization_clauses (vec<tree> &save)
7446 save = vNULL;
7447 if (omp_private_member_ignore_next)
7448 save.safe_push (integer_one_node);
7449 omp_private_member_ignore_next = false;
7450 if (!omp_private_member_map)
7451 return;
7453 while (!omp_private_member_vec.is_empty ())
7455 tree t = omp_private_member_vec.pop ();
7456 if (t == error_mark_node)
7458 save.safe_push (t);
7459 continue;
7461 tree n = t;
7462 if (t == integer_zero_node)
7463 t = omp_private_member_vec.pop ();
7464 tree *v = omp_private_member_map->get (t);
7465 gcc_assert (v);
7466 save.safe_push (*v);
7467 save.safe_push (t);
7468 if (n != t)
7469 save.safe_push (n);
7471 delete omp_private_member_map;
7472 omp_private_member_map = NULL;
7475 /* Restore OpenMP privatization clauses mapping saved by the
7476 above function. */
7478 void
7479 restore_omp_privatization_clauses (vec<tree> &save)
7481 gcc_assert (omp_private_member_vec.is_empty ());
7482 omp_private_member_ignore_next = false;
7483 if (save.is_empty ())
7484 return;
7485 if (save.length () == 1 && save[0] == integer_one_node)
7487 omp_private_member_ignore_next = true;
7488 save.release ();
7489 return;
7492 omp_private_member_map = new hash_map <tree, tree>;
7493 while (!save.is_empty ())
7495 tree t = save.pop ();
7496 tree n = t;
7497 if (t != error_mark_node)
7499 if (t == integer_one_node)
7501 omp_private_member_ignore_next = true;
7502 gcc_assert (save.is_empty ());
7503 break;
7505 if (t == integer_zero_node)
7506 t = save.pop ();
7507 tree &v = omp_private_member_map->get_or_insert (t);
7508 v = save.pop ();
7510 omp_private_member_vec.safe_push (t);
7511 if (n != t)
7512 omp_private_member_vec.safe_push (n);
7514 save.release ();
7517 /* For all variables in the tree_list VARS, mark them as thread local. */
7519 void
7520 finish_omp_threadprivate (tree vars)
7522 tree t;
7524 /* Mark every variable in VARS to be assigned thread local storage. */
7525 for (t = vars; t; t = TREE_CHAIN (t))
7527 tree v = TREE_PURPOSE (t);
7529 if (error_operand_p (v))
7531 else if (!VAR_P (v))
7532 error ("%<threadprivate%> %qD is not file, namespace "
7533 "or block scope variable", v);
7534 /* If V had already been marked threadprivate, it doesn't matter
7535 whether it had been used prior to this point. */
7536 else if (TREE_USED (v)
7537 && (DECL_LANG_SPECIFIC (v) == NULL
7538 || !CP_DECL_THREADPRIVATE_P (v)))
7539 error ("%qE declared %<threadprivate%> after first use", v);
7540 else if (! TREE_STATIC (v) && ! DECL_EXTERNAL (v))
7541 error ("automatic variable %qE cannot be %<threadprivate%>", v);
7542 else if (! COMPLETE_TYPE_P (complete_type (TREE_TYPE (v))))
7543 error ("%<threadprivate%> %qE has incomplete type", v);
7544 else if (TREE_STATIC (v) && TYPE_P (CP_DECL_CONTEXT (v))
7545 && CP_DECL_CONTEXT (v) != current_class_type)
7546 error ("%<threadprivate%> %qE directive not "
7547 "in %qT definition", v, CP_DECL_CONTEXT (v));
7548 else
7550 /* Allocate a LANG_SPECIFIC structure for V, if needed. */
7551 if (DECL_LANG_SPECIFIC (v) == NULL)
7553 retrofit_lang_decl (v);
7555 /* Make sure that DECL_DISCRIMINATOR_P continues to be true
7556 after the allocation of the lang_decl structure. */
7557 if (DECL_DISCRIMINATOR_P (v))
7558 DECL_LANG_SPECIFIC (v)->u.base.u2sel = 1;
7561 if (! CP_DECL_THREAD_LOCAL_P (v))
7563 CP_DECL_THREAD_LOCAL_P (v) = true;
7564 set_decl_tls_model (v, decl_default_tls_model (v));
7565 /* If rtl has been already set for this var, call
7566 make_decl_rtl once again, so that encode_section_info
7567 has a chance to look at the new decl flags. */
7568 if (DECL_RTL_SET_P (v))
7569 make_decl_rtl (v);
7571 CP_DECL_THREADPRIVATE_P (v) = 1;
7576 /* Build an OpenMP structured block. */
7578 tree
7579 begin_omp_structured_block (void)
7581 return do_pushlevel (sk_omp);
7584 tree
7585 finish_omp_structured_block (tree block)
7587 return do_poplevel (block);
7590 /* Similarly, except force the retention of the BLOCK. */
7592 tree
7593 begin_omp_parallel (void)
7595 keep_next_level (true);
7596 return begin_omp_structured_block ();
7599 /* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
7600 statement. */
7602 tree
7603 finish_oacc_data (tree clauses, tree block)
7605 tree stmt;
7607 block = finish_omp_structured_block (block);
7609 stmt = make_node (OACC_DATA);
7610 TREE_TYPE (stmt) = void_type_node;
7611 OACC_DATA_CLAUSES (stmt) = clauses;
7612 OACC_DATA_BODY (stmt) = block;
7614 return add_stmt (stmt);
7617 /* Generate OACC_HOST_DATA, with CLAUSES and BLOCK as its compound
7618 statement. */
7620 tree
7621 finish_oacc_host_data (tree clauses, tree block)
7623 tree stmt;
7625 block = finish_omp_structured_block (block);
7627 stmt = make_node (OACC_HOST_DATA);
7628 TREE_TYPE (stmt) = void_type_node;
7629 OACC_HOST_DATA_CLAUSES (stmt) = clauses;
7630 OACC_HOST_DATA_BODY (stmt) = block;
7632 return add_stmt (stmt);
7635 /* Generate OMP construct CODE, with BODY and CLAUSES as its compound
7636 statement. */
7638 tree
7639 finish_omp_construct (enum tree_code code, tree body, tree clauses)
7641 body = finish_omp_structured_block (body);
7643 tree stmt = make_node (code);
7644 TREE_TYPE (stmt) = void_type_node;
7645 OMP_BODY (stmt) = body;
7646 OMP_CLAUSES (stmt) = clauses;
7648 return add_stmt (stmt);
7651 tree
7652 finish_omp_parallel (tree clauses, tree body)
7654 tree stmt;
7656 body = finish_omp_structured_block (body);
7658 stmt = make_node (OMP_PARALLEL);
7659 TREE_TYPE (stmt) = void_type_node;
7660 OMP_PARALLEL_CLAUSES (stmt) = clauses;
7661 OMP_PARALLEL_BODY (stmt) = body;
7663 return add_stmt (stmt);
7666 tree
7667 begin_omp_task (void)
7669 keep_next_level (true);
7670 return begin_omp_structured_block ();
7673 tree
7674 finish_omp_task (tree clauses, tree body)
7676 tree stmt;
7678 body = finish_omp_structured_block (body);
7680 stmt = make_node (OMP_TASK);
7681 TREE_TYPE (stmt) = void_type_node;
7682 OMP_TASK_CLAUSES (stmt) = clauses;
7683 OMP_TASK_BODY (stmt) = body;
7685 return add_stmt (stmt);
7688 /* Helper function for finish_omp_for. Convert Ith random access iterator
7689 into integral iterator. Return FALSE if successful. */
7691 static bool
7692 handle_omp_for_class_iterator (int i, location_t locus, enum tree_code code,
7693 tree declv, tree orig_declv, tree initv,
7694 tree condv, tree incrv, tree *body,
7695 tree *pre_body, tree &clauses, tree *lastp,
7696 int collapse, int ordered)
7698 tree diff, iter_init, iter_incr = NULL, last;
7699 tree incr_var = NULL, orig_pre_body, orig_body, c;
7700 tree decl = TREE_VEC_ELT (declv, i);
7701 tree init = TREE_VEC_ELT (initv, i);
7702 tree cond = TREE_VEC_ELT (condv, i);
7703 tree incr = TREE_VEC_ELT (incrv, i);
7704 tree iter = decl;
7705 location_t elocus = locus;
7707 if (init && EXPR_HAS_LOCATION (init))
7708 elocus = EXPR_LOCATION (init);
7710 cond = cp_fully_fold (cond);
7711 switch (TREE_CODE (cond))
7713 case GT_EXPR:
7714 case GE_EXPR:
7715 case LT_EXPR:
7716 case LE_EXPR:
7717 case NE_EXPR:
7718 if (TREE_OPERAND (cond, 1) == iter)
7719 cond = build2 (swap_tree_comparison (TREE_CODE (cond)),
7720 TREE_TYPE (cond), iter, TREE_OPERAND (cond, 0));
7721 if (TREE_OPERAND (cond, 0) != iter)
7722 cond = error_mark_node;
7723 else
7725 tree tem = build_x_binary_op (EXPR_LOCATION (cond),
7726 TREE_CODE (cond),
7727 iter, ERROR_MARK,
7728 TREE_OPERAND (cond, 1), ERROR_MARK,
7729 NULL, tf_warning_or_error);
7730 if (error_operand_p (tem))
7731 return true;
7733 break;
7734 default:
7735 cond = error_mark_node;
7736 break;
7738 if (cond == error_mark_node)
7740 error_at (elocus, "invalid controlling predicate");
7741 return true;
7743 diff = build_x_binary_op (elocus, MINUS_EXPR, TREE_OPERAND (cond, 1),
7744 ERROR_MARK, iter, ERROR_MARK, NULL,
7745 tf_warning_or_error);
7746 diff = cp_fully_fold (diff);
7747 if (error_operand_p (diff))
7748 return true;
7749 if (TREE_CODE (TREE_TYPE (diff)) != INTEGER_TYPE)
7751 error_at (elocus, "difference between %qE and %qD does not have integer type",
7752 TREE_OPERAND (cond, 1), iter);
7753 return true;
7755 if (!c_omp_check_loop_iv_exprs (locus, orig_declv,
7756 TREE_VEC_ELT (declv, i), NULL_TREE,
7757 cond, cp_walk_subtrees))
7758 return true;
7760 switch (TREE_CODE (incr))
7762 case PREINCREMENT_EXPR:
7763 case PREDECREMENT_EXPR:
7764 case POSTINCREMENT_EXPR:
7765 case POSTDECREMENT_EXPR:
7766 if (TREE_OPERAND (incr, 0) != iter)
7768 incr = error_mark_node;
7769 break;
7771 iter_incr = build_x_unary_op (EXPR_LOCATION (incr),
7772 TREE_CODE (incr), iter,
7773 tf_warning_or_error);
7774 if (error_operand_p (iter_incr))
7775 return true;
7776 else if (TREE_CODE (incr) == PREINCREMENT_EXPR
7777 || TREE_CODE (incr) == POSTINCREMENT_EXPR)
7778 incr = integer_one_node;
7779 else
7780 incr = integer_minus_one_node;
7781 break;
7782 case MODIFY_EXPR:
7783 if (TREE_OPERAND (incr, 0) != iter)
7784 incr = error_mark_node;
7785 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
7786 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
7788 tree rhs = TREE_OPERAND (incr, 1);
7789 if (TREE_OPERAND (rhs, 0) == iter)
7791 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (rhs, 1)))
7792 != INTEGER_TYPE)
7793 incr = error_mark_node;
7794 else
7796 iter_incr = build_x_modify_expr (EXPR_LOCATION (rhs),
7797 iter, TREE_CODE (rhs),
7798 TREE_OPERAND (rhs, 1),
7799 tf_warning_or_error);
7800 if (error_operand_p (iter_incr))
7801 return true;
7802 incr = TREE_OPERAND (rhs, 1);
7803 incr = cp_convert (TREE_TYPE (diff), incr,
7804 tf_warning_or_error);
7805 if (TREE_CODE (rhs) == MINUS_EXPR)
7807 incr = build1 (NEGATE_EXPR, TREE_TYPE (diff), incr);
7808 incr = fold_simple (incr);
7810 if (TREE_CODE (incr) != INTEGER_CST
7811 && (TREE_CODE (incr) != NOP_EXPR
7812 || (TREE_CODE (TREE_OPERAND (incr, 0))
7813 != INTEGER_CST)))
7814 iter_incr = NULL;
7817 else if (TREE_OPERAND (rhs, 1) == iter)
7819 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (rhs, 0))) != INTEGER_TYPE
7820 || TREE_CODE (rhs) != PLUS_EXPR)
7821 incr = error_mark_node;
7822 else
7824 iter_incr = build_x_binary_op (EXPR_LOCATION (rhs),
7825 PLUS_EXPR,
7826 TREE_OPERAND (rhs, 0),
7827 ERROR_MARK, iter,
7828 ERROR_MARK, NULL,
7829 tf_warning_or_error);
7830 if (error_operand_p (iter_incr))
7831 return true;
7832 iter_incr = build_x_modify_expr (EXPR_LOCATION (rhs),
7833 iter, NOP_EXPR,
7834 iter_incr,
7835 tf_warning_or_error);
7836 if (error_operand_p (iter_incr))
7837 return true;
7838 incr = TREE_OPERAND (rhs, 0);
7839 iter_incr = NULL;
7842 else
7843 incr = error_mark_node;
7845 else
7846 incr = error_mark_node;
7847 break;
7848 default:
7849 incr = error_mark_node;
7850 break;
7853 if (incr == error_mark_node)
7855 error_at (elocus, "invalid increment expression");
7856 return true;
7859 incr = cp_convert (TREE_TYPE (diff), incr, tf_warning_or_error);
7860 bool taskloop_iv_seen = false;
7861 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
7862 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
7863 && OMP_CLAUSE_DECL (c) == iter)
7865 if (code == OMP_TASKLOOP)
7867 taskloop_iv_seen = true;
7868 OMP_CLAUSE_LASTPRIVATE_TASKLOOP_IV (c) = 1;
7870 break;
7872 else if (code == OMP_TASKLOOP
7873 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
7874 && OMP_CLAUSE_DECL (c) == iter)
7876 taskloop_iv_seen = true;
7877 OMP_CLAUSE_PRIVATE_TASKLOOP_IV (c) = 1;
7880 decl = create_temporary_var (TREE_TYPE (diff));
7881 pushdecl (decl);
7882 add_decl_expr (decl);
7883 last = create_temporary_var (TREE_TYPE (diff));
7884 pushdecl (last);
7885 add_decl_expr (last);
7886 if (c && iter_incr == NULL && TREE_CODE (incr) != INTEGER_CST
7887 && (!ordered || (i < collapse && collapse > 1)))
7889 incr_var = create_temporary_var (TREE_TYPE (diff));
7890 pushdecl (incr_var);
7891 add_decl_expr (incr_var);
7893 gcc_assert (stmts_are_full_exprs_p ());
7894 tree diffvar = NULL_TREE;
7895 if (code == OMP_TASKLOOP)
7897 if (!taskloop_iv_seen)
7899 tree ivc = build_omp_clause (locus, OMP_CLAUSE_FIRSTPRIVATE);
7900 OMP_CLAUSE_DECL (ivc) = iter;
7901 cxx_omp_finish_clause (ivc, NULL);
7902 OMP_CLAUSE_CHAIN (ivc) = clauses;
7903 clauses = ivc;
7905 tree lvc = build_omp_clause (locus, OMP_CLAUSE_FIRSTPRIVATE);
7906 OMP_CLAUSE_DECL (lvc) = last;
7907 OMP_CLAUSE_CHAIN (lvc) = clauses;
7908 clauses = lvc;
7909 diffvar = create_temporary_var (TREE_TYPE (diff));
7910 pushdecl (diffvar);
7911 add_decl_expr (diffvar);
7914 orig_pre_body = *pre_body;
7915 *pre_body = push_stmt_list ();
7916 if (orig_pre_body)
7917 add_stmt (orig_pre_body);
7918 if (init != NULL)
7919 finish_expr_stmt (build_x_modify_expr (elocus,
7920 iter, NOP_EXPR, init,
7921 tf_warning_or_error));
7922 init = build_int_cst (TREE_TYPE (diff), 0);
7923 if (c && iter_incr == NULL
7924 && (!ordered || (i < collapse && collapse > 1)))
7926 if (incr_var)
7928 finish_expr_stmt (build_x_modify_expr (elocus,
7929 incr_var, NOP_EXPR,
7930 incr, tf_warning_or_error));
7931 incr = incr_var;
7933 iter_incr = build_x_modify_expr (elocus,
7934 iter, PLUS_EXPR, incr,
7935 tf_warning_or_error);
7937 if (c && ordered && i < collapse && collapse > 1)
7938 iter_incr = incr;
7939 finish_expr_stmt (build_x_modify_expr (elocus,
7940 last, NOP_EXPR, init,
7941 tf_warning_or_error));
7942 if (diffvar)
7944 finish_expr_stmt (build_x_modify_expr (elocus,
7945 diffvar, NOP_EXPR,
7946 diff, tf_warning_or_error));
7947 diff = diffvar;
7949 *pre_body = pop_stmt_list (*pre_body);
7951 cond = cp_build_binary_op (elocus,
7952 TREE_CODE (cond), decl, diff,
7953 tf_warning_or_error);
7954 incr = build_modify_expr (elocus, decl, NULL_TREE, PLUS_EXPR,
7955 elocus, incr, NULL_TREE);
7957 orig_body = *body;
7958 *body = push_stmt_list ();
7959 iter_init = build2 (MINUS_EXPR, TREE_TYPE (diff), decl, last);
7960 iter_init = build_x_modify_expr (elocus,
7961 iter, PLUS_EXPR, iter_init,
7962 tf_warning_or_error);
7963 if (iter_init != error_mark_node)
7964 iter_init = build1 (NOP_EXPR, void_type_node, iter_init);
7965 finish_expr_stmt (iter_init);
7966 finish_expr_stmt (build_x_modify_expr (elocus,
7967 last, NOP_EXPR, decl,
7968 tf_warning_or_error));
7969 add_stmt (orig_body);
7970 *body = pop_stmt_list (*body);
7972 if (c)
7974 OMP_CLAUSE_LASTPRIVATE_STMT (c) = push_stmt_list ();
7975 if (!ordered)
7976 finish_expr_stmt (iter_incr);
7977 else
7979 iter_init = decl;
7980 if (i < collapse && collapse > 1 && !error_operand_p (iter_incr))
7981 iter_init = build2 (PLUS_EXPR, TREE_TYPE (diff),
7982 iter_init, iter_incr);
7983 iter_init = build2 (MINUS_EXPR, TREE_TYPE (diff), iter_init, last);
7984 iter_init = build_x_modify_expr (elocus,
7985 iter, PLUS_EXPR, iter_init,
7986 tf_warning_or_error);
7987 if (iter_init != error_mark_node)
7988 iter_init = build1 (NOP_EXPR, void_type_node, iter_init);
7989 finish_expr_stmt (iter_init);
7991 OMP_CLAUSE_LASTPRIVATE_STMT (c)
7992 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (c));
7995 TREE_VEC_ELT (declv, i) = decl;
7996 TREE_VEC_ELT (initv, i) = init;
7997 TREE_VEC_ELT (condv, i) = cond;
7998 TREE_VEC_ELT (incrv, i) = incr;
7999 *lastp = last;
8001 return false;
8004 /* Build and validate an OMP_FOR statement. CLAUSES, BODY, COND, INCR
8005 are directly for their associated operands in the statement. DECL
8006 and INIT are a combo; if DECL is NULL then INIT ought to be a
8007 MODIFY_EXPR, and the DECL should be extracted. PRE_BODY are
8008 optional statements that need to go before the loop into its
8009 sk_omp scope. */
8011 tree
8012 finish_omp_for (location_t locus, enum tree_code code, tree declv,
8013 tree orig_declv, tree initv, tree condv, tree incrv,
8014 tree body, tree pre_body, vec<tree> *orig_inits, tree clauses)
8016 tree omp_for = NULL, orig_incr = NULL;
8017 tree decl = NULL, init, cond, incr, orig_decl = NULL_TREE, block = NULL_TREE;
8018 tree last = NULL_TREE;
8019 location_t elocus;
8020 int i;
8021 int collapse = 1;
8022 int ordered = 0;
8024 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (initv));
8025 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (condv));
8026 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (incrv));
8027 if (TREE_VEC_LENGTH (declv) > 1)
8029 tree c;
8031 c = omp_find_clause (clauses, OMP_CLAUSE_TILE);
8032 if (c)
8033 collapse = list_length (OMP_CLAUSE_TILE_LIST (c));
8034 else
8036 c = omp_find_clause (clauses, OMP_CLAUSE_COLLAPSE);
8037 if (c)
8038 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (c));
8039 if (collapse != TREE_VEC_LENGTH (declv))
8040 ordered = TREE_VEC_LENGTH (declv);
8043 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
8045 decl = TREE_VEC_ELT (declv, i);
8046 init = TREE_VEC_ELT (initv, i);
8047 cond = TREE_VEC_ELT (condv, i);
8048 incr = TREE_VEC_ELT (incrv, i);
8049 elocus = locus;
8051 if (decl == NULL)
8053 if (init != NULL)
8054 switch (TREE_CODE (init))
8056 case MODIFY_EXPR:
8057 decl = TREE_OPERAND (init, 0);
8058 init = TREE_OPERAND (init, 1);
8059 break;
8060 case MODOP_EXPR:
8061 if (TREE_CODE (TREE_OPERAND (init, 1)) == NOP_EXPR)
8063 decl = TREE_OPERAND (init, 0);
8064 init = TREE_OPERAND (init, 2);
8066 break;
8067 default:
8068 break;
8071 if (decl == NULL)
8073 error_at (locus,
8074 "expected iteration declaration or initialization");
8075 return NULL;
8079 if (init && EXPR_HAS_LOCATION (init))
8080 elocus = EXPR_LOCATION (init);
8082 if (cond == NULL)
8084 error_at (elocus, "missing controlling predicate");
8085 return NULL;
8088 if (incr == NULL)
8090 error_at (elocus, "missing increment expression");
8091 return NULL;
8094 TREE_VEC_ELT (declv, i) = decl;
8095 TREE_VEC_ELT (initv, i) = init;
8098 if (orig_inits)
8100 bool fail = false;
8101 tree orig_init;
8102 FOR_EACH_VEC_ELT (*orig_inits, i, orig_init)
8103 if (orig_init
8104 && !c_omp_check_loop_iv_exprs (locus, declv,
8105 TREE_VEC_ELT (declv, i), orig_init,
8106 NULL_TREE, cp_walk_subtrees))
8107 fail = true;
8108 if (fail)
8109 return NULL;
8112 if (dependent_omp_for_p (declv, initv, condv, incrv))
8114 tree stmt;
8116 stmt = make_node (code);
8118 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
8120 /* This is really just a place-holder. We'll be decomposing this
8121 again and going through the cp_build_modify_expr path below when
8122 we instantiate the thing. */
8123 TREE_VEC_ELT (initv, i)
8124 = build2 (MODIFY_EXPR, void_type_node, TREE_VEC_ELT (declv, i),
8125 TREE_VEC_ELT (initv, i));
8128 TREE_TYPE (stmt) = void_type_node;
8129 OMP_FOR_INIT (stmt) = initv;
8130 OMP_FOR_COND (stmt) = condv;
8131 OMP_FOR_INCR (stmt) = incrv;
8132 OMP_FOR_BODY (stmt) = body;
8133 OMP_FOR_PRE_BODY (stmt) = pre_body;
8134 OMP_FOR_CLAUSES (stmt) = clauses;
8136 SET_EXPR_LOCATION (stmt, locus);
8137 return add_stmt (stmt);
8140 if (!orig_declv)
8141 orig_declv = copy_node (declv);
8143 if (processing_template_decl)
8144 orig_incr = make_tree_vec (TREE_VEC_LENGTH (incrv));
8146 for (i = 0; i < TREE_VEC_LENGTH (declv); )
8148 decl = TREE_VEC_ELT (declv, i);
8149 init = TREE_VEC_ELT (initv, i);
8150 cond = TREE_VEC_ELT (condv, i);
8151 incr = TREE_VEC_ELT (incrv, i);
8152 if (orig_incr)
8153 TREE_VEC_ELT (orig_incr, i) = incr;
8154 elocus = locus;
8156 if (init && EXPR_HAS_LOCATION (init))
8157 elocus = EXPR_LOCATION (init);
8159 if (!DECL_P (decl))
8161 error_at (elocus, "expected iteration declaration or initialization");
8162 return NULL;
8165 if (incr && TREE_CODE (incr) == MODOP_EXPR)
8167 if (orig_incr)
8168 TREE_VEC_ELT (orig_incr, i) = incr;
8169 incr = cp_build_modify_expr (elocus, TREE_OPERAND (incr, 0),
8170 TREE_CODE (TREE_OPERAND (incr, 1)),
8171 TREE_OPERAND (incr, 2),
8172 tf_warning_or_error);
8175 if (CLASS_TYPE_P (TREE_TYPE (decl)))
8177 if (code == OMP_SIMD)
8179 error_at (elocus, "%<#pragma omp simd%> used with class "
8180 "iteration variable %qE", decl);
8181 return NULL;
8183 if (code == CILK_FOR && i == 0)
8184 orig_decl = decl;
8185 if (handle_omp_for_class_iterator (i, locus, code, declv, orig_declv,
8186 initv, condv, incrv, &body,
8187 &pre_body, clauses, &last,
8188 collapse, ordered))
8189 return NULL;
8190 continue;
8193 if (!INTEGRAL_TYPE_P (TREE_TYPE (decl))
8194 && !TYPE_PTR_P (TREE_TYPE (decl)))
8196 error_at (elocus, "invalid type for iteration variable %qE", decl);
8197 return NULL;
8200 if (!processing_template_decl)
8202 init = fold_build_cleanup_point_expr (TREE_TYPE (init), init);
8203 init = cp_build_modify_expr (elocus, decl, NOP_EXPR, init,
8204 tf_warning_or_error);
8206 else
8207 init = build2 (MODIFY_EXPR, void_type_node, decl, init);
8208 if (cond
8209 && TREE_SIDE_EFFECTS (cond)
8210 && COMPARISON_CLASS_P (cond)
8211 && !processing_template_decl)
8213 tree t = TREE_OPERAND (cond, 0);
8214 if (TREE_SIDE_EFFECTS (t)
8215 && t != decl
8216 && (TREE_CODE (t) != NOP_EXPR
8217 || TREE_OPERAND (t, 0) != decl))
8218 TREE_OPERAND (cond, 0)
8219 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
8221 t = TREE_OPERAND (cond, 1);
8222 if (TREE_SIDE_EFFECTS (t)
8223 && t != decl
8224 && (TREE_CODE (t) != NOP_EXPR
8225 || TREE_OPERAND (t, 0) != decl))
8226 TREE_OPERAND (cond, 1)
8227 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
8229 if (decl == error_mark_node || init == error_mark_node)
8230 return NULL;
8232 TREE_VEC_ELT (declv, i) = decl;
8233 TREE_VEC_ELT (initv, i) = init;
8234 TREE_VEC_ELT (condv, i) = cond;
8235 TREE_VEC_ELT (incrv, i) = incr;
8236 i++;
8239 if (IS_EMPTY_STMT (pre_body))
8240 pre_body = NULL;
8242 if (code == CILK_FOR && !processing_template_decl)
8243 block = push_stmt_list ();
8245 omp_for = c_finish_omp_for (locus, code, declv, orig_declv, initv, condv,
8246 incrv, body, pre_body);
8248 /* Check for iterators appearing in lb, b or incr expressions. */
8249 if (omp_for && !c_omp_check_loop_iv (omp_for, orig_declv, cp_walk_subtrees))
8250 omp_for = NULL_TREE;
8252 if (omp_for == NULL)
8254 if (block)
8255 pop_stmt_list (block);
8256 return NULL;
8259 add_stmt (omp_for);
8261 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INCR (omp_for)); i++)
8263 decl = TREE_OPERAND (TREE_VEC_ELT (OMP_FOR_INIT (omp_for), i), 0);
8264 incr = TREE_VEC_ELT (OMP_FOR_INCR (omp_for), i);
8266 if (TREE_CODE (incr) != MODIFY_EXPR)
8267 continue;
8269 if (TREE_SIDE_EFFECTS (TREE_OPERAND (incr, 1))
8270 && BINARY_CLASS_P (TREE_OPERAND (incr, 1))
8271 && !processing_template_decl)
8273 tree t = TREE_OPERAND (TREE_OPERAND (incr, 1), 0);
8274 if (TREE_SIDE_EFFECTS (t)
8275 && t != decl
8276 && (TREE_CODE (t) != NOP_EXPR
8277 || TREE_OPERAND (t, 0) != decl))
8278 TREE_OPERAND (TREE_OPERAND (incr, 1), 0)
8279 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
8281 t = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
8282 if (TREE_SIDE_EFFECTS (t)
8283 && t != decl
8284 && (TREE_CODE (t) != NOP_EXPR
8285 || TREE_OPERAND (t, 0) != decl))
8286 TREE_OPERAND (TREE_OPERAND (incr, 1), 1)
8287 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
8290 if (orig_incr)
8291 TREE_VEC_ELT (OMP_FOR_INCR (omp_for), i) = TREE_VEC_ELT (orig_incr, i);
8293 OMP_FOR_CLAUSES (omp_for) = clauses;
8295 /* For simd loops with non-static data member iterators, we could have added
8296 OMP_CLAUSE_LINEAR clauses without OMP_CLAUSE_LINEAR_STEP. As we know the
8297 step at this point, fill it in. */
8298 if (code == OMP_SIMD && !processing_template_decl
8299 && TREE_VEC_LENGTH (OMP_FOR_INCR (omp_for)) == 1)
8300 for (tree c = omp_find_clause (clauses, OMP_CLAUSE_LINEAR); c;
8301 c = omp_find_clause (OMP_CLAUSE_CHAIN (c), OMP_CLAUSE_LINEAR))
8302 if (OMP_CLAUSE_LINEAR_STEP (c) == NULL_TREE)
8304 decl = TREE_OPERAND (TREE_VEC_ELT (OMP_FOR_INIT (omp_for), 0), 0);
8305 gcc_assert (decl == OMP_CLAUSE_DECL (c));
8306 incr = TREE_VEC_ELT (OMP_FOR_INCR (omp_for), 0);
8307 tree step, stept;
8308 switch (TREE_CODE (incr))
8310 case PREINCREMENT_EXPR:
8311 case POSTINCREMENT_EXPR:
8312 /* c_omp_for_incr_canonicalize_ptr() should have been
8313 called to massage things appropriately. */
8314 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (decl)));
8315 OMP_CLAUSE_LINEAR_STEP (c) = build_int_cst (TREE_TYPE (decl), 1);
8316 break;
8317 case PREDECREMENT_EXPR:
8318 case POSTDECREMENT_EXPR:
8319 /* c_omp_for_incr_canonicalize_ptr() should have been
8320 called to massage things appropriately. */
8321 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (decl)));
8322 OMP_CLAUSE_LINEAR_STEP (c)
8323 = build_int_cst (TREE_TYPE (decl), -1);
8324 break;
8325 case MODIFY_EXPR:
8326 gcc_assert (TREE_OPERAND (incr, 0) == decl);
8327 incr = TREE_OPERAND (incr, 1);
8328 switch (TREE_CODE (incr))
8330 case PLUS_EXPR:
8331 if (TREE_OPERAND (incr, 1) == decl)
8332 step = TREE_OPERAND (incr, 0);
8333 else
8334 step = TREE_OPERAND (incr, 1);
8335 break;
8336 case MINUS_EXPR:
8337 case POINTER_PLUS_EXPR:
8338 gcc_assert (TREE_OPERAND (incr, 0) == decl);
8339 step = TREE_OPERAND (incr, 1);
8340 break;
8341 default:
8342 gcc_unreachable ();
8344 stept = TREE_TYPE (decl);
8345 if (POINTER_TYPE_P (stept))
8346 stept = sizetype;
8347 step = fold_convert (stept, step);
8348 if (TREE_CODE (incr) == MINUS_EXPR)
8349 step = fold_build1 (NEGATE_EXPR, stept, step);
8350 OMP_CLAUSE_LINEAR_STEP (c) = step;
8351 break;
8352 default:
8353 gcc_unreachable ();
8357 if (block)
8359 tree omp_par = make_node (OMP_PARALLEL);
8360 TREE_TYPE (omp_par) = void_type_node;
8361 OMP_PARALLEL_CLAUSES (omp_par) = NULL_TREE;
8362 tree bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
8363 TREE_SIDE_EFFECTS (bind) = 1;
8364 BIND_EXPR_BODY (bind) = pop_stmt_list (block);
8365 OMP_PARALLEL_BODY (omp_par) = bind;
8366 if (OMP_FOR_PRE_BODY (omp_for))
8368 add_stmt (OMP_FOR_PRE_BODY (omp_for));
8369 OMP_FOR_PRE_BODY (omp_for) = NULL_TREE;
8371 init = TREE_VEC_ELT (OMP_FOR_INIT (omp_for), 0);
8372 decl = TREE_OPERAND (init, 0);
8373 cond = TREE_VEC_ELT (OMP_FOR_COND (omp_for), 0);
8374 incr = TREE_VEC_ELT (OMP_FOR_INCR (omp_for), 0);
8375 tree t = TREE_OPERAND (cond, 1), c, clauses, *pc;
8376 clauses = OMP_FOR_CLAUSES (omp_for);
8377 OMP_FOR_CLAUSES (omp_for) = NULL_TREE;
8378 for (pc = &clauses; *pc; )
8379 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_SCHEDULE)
8381 gcc_assert (OMP_FOR_CLAUSES (omp_for) == NULL_TREE);
8382 OMP_FOR_CLAUSES (omp_for) = *pc;
8383 *pc = OMP_CLAUSE_CHAIN (*pc);
8384 OMP_CLAUSE_CHAIN (OMP_FOR_CLAUSES (omp_for)) = NULL_TREE;
8386 else
8388 gcc_assert (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_FIRSTPRIVATE);
8389 pc = &OMP_CLAUSE_CHAIN (*pc);
8391 if (TREE_CODE (t) != INTEGER_CST)
8393 TREE_OPERAND (cond, 1) = get_temp_regvar (TREE_TYPE (t), t);
8394 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
8395 OMP_CLAUSE_DECL (c) = TREE_OPERAND (cond, 1);
8396 OMP_CLAUSE_CHAIN (c) = clauses;
8397 clauses = c;
8399 if (TREE_CODE (incr) == MODIFY_EXPR)
8401 t = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
8402 if (TREE_CODE (t) != INTEGER_CST)
8404 TREE_OPERAND (TREE_OPERAND (incr, 1), 1)
8405 = get_temp_regvar (TREE_TYPE (t), t);
8406 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
8407 OMP_CLAUSE_DECL (c) = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
8408 OMP_CLAUSE_CHAIN (c) = clauses;
8409 clauses = c;
8412 t = TREE_OPERAND (init, 1);
8413 if (TREE_CODE (t) != INTEGER_CST)
8415 TREE_OPERAND (init, 1) = get_temp_regvar (TREE_TYPE (t), t);
8416 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
8417 OMP_CLAUSE_DECL (c) = TREE_OPERAND (init, 1);
8418 OMP_CLAUSE_CHAIN (c) = clauses;
8419 clauses = c;
8421 if (orig_decl && orig_decl != decl)
8423 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
8424 OMP_CLAUSE_DECL (c) = orig_decl;
8425 OMP_CLAUSE_CHAIN (c) = clauses;
8426 clauses = c;
8428 if (last)
8430 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
8431 OMP_CLAUSE_DECL (c) = last;
8432 OMP_CLAUSE_CHAIN (c) = clauses;
8433 clauses = c;
8435 c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
8436 OMP_CLAUSE_DECL (c) = decl;
8437 OMP_CLAUSE_CHAIN (c) = clauses;
8438 clauses = c;
8439 c = build_omp_clause (input_location, OMP_CLAUSE__CILK_FOR_COUNT_);
8440 OMP_CLAUSE_OPERAND (c, 0)
8441 = cilk_for_number_of_iterations (omp_for);
8442 OMP_CLAUSE_CHAIN (c) = clauses;
8443 OMP_PARALLEL_CLAUSES (omp_par) = finish_omp_clauses (c, C_ORT_CILK);
8444 add_stmt (omp_par);
8445 return omp_par;
8447 else if (code == CILK_FOR && processing_template_decl)
8449 tree c, clauses = OMP_FOR_CLAUSES (omp_for);
8450 if (orig_decl && orig_decl != decl)
8452 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
8453 OMP_CLAUSE_DECL (c) = orig_decl;
8454 OMP_CLAUSE_CHAIN (c) = clauses;
8455 clauses = c;
8457 if (last)
8459 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
8460 OMP_CLAUSE_DECL (c) = last;
8461 OMP_CLAUSE_CHAIN (c) = clauses;
8462 clauses = c;
8464 OMP_FOR_CLAUSES (omp_for) = clauses;
8466 return omp_for;
8469 void
8470 finish_omp_atomic (enum tree_code code, enum tree_code opcode, tree lhs,
8471 tree rhs, tree v, tree lhs1, tree rhs1, bool seq_cst)
8473 tree orig_lhs;
8474 tree orig_rhs;
8475 tree orig_v;
8476 tree orig_lhs1;
8477 tree orig_rhs1;
8478 bool dependent_p;
8479 tree stmt;
8481 orig_lhs = lhs;
8482 orig_rhs = rhs;
8483 orig_v = v;
8484 orig_lhs1 = lhs1;
8485 orig_rhs1 = rhs1;
8486 dependent_p = false;
8487 stmt = NULL_TREE;
8489 /* Even in a template, we can detect invalid uses of the atomic
8490 pragma if neither LHS nor RHS is type-dependent. */
8491 if (processing_template_decl)
8493 dependent_p = (type_dependent_expression_p (lhs)
8494 || (rhs && type_dependent_expression_p (rhs))
8495 || (v && type_dependent_expression_p (v))
8496 || (lhs1 && type_dependent_expression_p (lhs1))
8497 || (rhs1 && type_dependent_expression_p (rhs1)));
8498 if (!dependent_p)
8500 lhs = build_non_dependent_expr (lhs);
8501 if (rhs)
8502 rhs = build_non_dependent_expr (rhs);
8503 if (v)
8504 v = build_non_dependent_expr (v);
8505 if (lhs1)
8506 lhs1 = build_non_dependent_expr (lhs1);
8507 if (rhs1)
8508 rhs1 = build_non_dependent_expr (rhs1);
8511 if (!dependent_p)
8513 bool swapped = false;
8514 if (rhs1 && cp_tree_equal (lhs, rhs))
8516 std::swap (rhs, rhs1);
8517 swapped = !commutative_tree_code (opcode);
8519 if (rhs1 && !cp_tree_equal (lhs, rhs1))
8521 if (code == OMP_ATOMIC)
8522 error ("%<#pragma omp atomic update%> uses two different "
8523 "expressions for memory");
8524 else
8525 error ("%<#pragma omp atomic capture%> uses two different "
8526 "expressions for memory");
8527 return;
8529 if (lhs1 && !cp_tree_equal (lhs, lhs1))
8531 if (code == OMP_ATOMIC)
8532 error ("%<#pragma omp atomic update%> uses two different "
8533 "expressions for memory");
8534 else
8535 error ("%<#pragma omp atomic capture%> uses two different "
8536 "expressions for memory");
8537 return;
8539 stmt = c_finish_omp_atomic (input_location, code, opcode, lhs, rhs,
8540 v, lhs1, rhs1, swapped, seq_cst,
8541 processing_template_decl != 0);
8542 if (stmt == error_mark_node)
8543 return;
8545 if (processing_template_decl)
8547 if (code == OMP_ATOMIC_READ)
8549 stmt = build_min_nt_loc (EXPR_LOCATION (orig_lhs),
8550 OMP_ATOMIC_READ, orig_lhs);
8551 OMP_ATOMIC_SEQ_CST (stmt) = seq_cst;
8552 stmt = build2 (MODIFY_EXPR, void_type_node, orig_v, stmt);
8554 else
8556 if (opcode == NOP_EXPR)
8557 stmt = build2 (MODIFY_EXPR, void_type_node, orig_lhs, orig_rhs);
8558 else
8559 stmt = build2 (opcode, void_type_node, orig_lhs, orig_rhs);
8560 if (orig_rhs1)
8561 stmt = build_min_nt_loc (EXPR_LOCATION (orig_rhs1),
8562 COMPOUND_EXPR, orig_rhs1, stmt);
8563 if (code != OMP_ATOMIC)
8565 stmt = build_min_nt_loc (EXPR_LOCATION (orig_lhs1),
8566 code, orig_lhs1, stmt);
8567 OMP_ATOMIC_SEQ_CST (stmt) = seq_cst;
8568 stmt = build2 (MODIFY_EXPR, void_type_node, orig_v, stmt);
8571 stmt = build2 (OMP_ATOMIC, void_type_node, integer_zero_node, stmt);
8572 OMP_ATOMIC_SEQ_CST (stmt) = seq_cst;
8574 finish_expr_stmt (stmt);
8577 void
8578 finish_omp_barrier (void)
8580 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_BARRIER);
8581 vec<tree, va_gc> *vec = make_tree_vector ();
8582 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
8583 release_tree_vector (vec);
8584 finish_expr_stmt (stmt);
8587 void
8588 finish_omp_flush (void)
8590 tree fn = builtin_decl_explicit (BUILT_IN_SYNC_SYNCHRONIZE);
8591 vec<tree, va_gc> *vec = make_tree_vector ();
8592 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
8593 release_tree_vector (vec);
8594 finish_expr_stmt (stmt);
8597 void
8598 finish_omp_taskwait (void)
8600 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_TASKWAIT);
8601 vec<tree, va_gc> *vec = make_tree_vector ();
8602 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
8603 release_tree_vector (vec);
8604 finish_expr_stmt (stmt);
8607 void
8608 finish_omp_taskyield (void)
8610 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_TASKYIELD);
8611 vec<tree, va_gc> *vec = make_tree_vector ();
8612 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
8613 release_tree_vector (vec);
8614 finish_expr_stmt (stmt);
8617 void
8618 finish_omp_cancel (tree clauses)
8620 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
8621 int mask = 0;
8622 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
8623 mask = 1;
8624 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
8625 mask = 2;
8626 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
8627 mask = 4;
8628 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
8629 mask = 8;
8630 else
8632 error ("%<#pragma omp cancel%> must specify one of "
8633 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> clauses");
8634 return;
8636 vec<tree, va_gc> *vec = make_tree_vector ();
8637 tree ifc = omp_find_clause (clauses, OMP_CLAUSE_IF);
8638 if (ifc != NULL_TREE)
8640 tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
8641 ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
8642 boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
8643 build_zero_cst (type));
8645 else
8646 ifc = boolean_true_node;
8647 vec->quick_push (build_int_cst (integer_type_node, mask));
8648 vec->quick_push (ifc);
8649 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
8650 release_tree_vector (vec);
8651 finish_expr_stmt (stmt);
8654 void
8655 finish_omp_cancellation_point (tree clauses)
8657 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
8658 int mask = 0;
8659 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
8660 mask = 1;
8661 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
8662 mask = 2;
8663 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
8664 mask = 4;
8665 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
8666 mask = 8;
8667 else
8669 error ("%<#pragma omp cancellation point%> must specify one of "
8670 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> clauses");
8671 return;
8673 vec<tree, va_gc> *vec
8674 = make_tree_vector_single (build_int_cst (integer_type_node, mask));
8675 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
8676 release_tree_vector (vec);
8677 finish_expr_stmt (stmt);
8680 /* Begin a __transaction_atomic or __transaction_relaxed statement.
8681 If PCOMPOUND is non-null, this is for a function-transaction-block, and we
8682 should create an extra compound stmt. */
8684 tree
8685 begin_transaction_stmt (location_t loc, tree *pcompound, int flags)
8687 tree r;
8689 if (pcompound)
8690 *pcompound = begin_compound_stmt (0);
8692 r = build_stmt (loc, TRANSACTION_EXPR, NULL_TREE);
8694 /* Only add the statement to the function if support enabled. */
8695 if (flag_tm)
8696 add_stmt (r);
8697 else
8698 error_at (loc, ((flags & TM_STMT_ATTR_RELAXED) != 0
8699 ? G_("%<__transaction_relaxed%> without "
8700 "transactional memory support enabled")
8701 : G_("%<__transaction_atomic%> without "
8702 "transactional memory support enabled")));
8704 TRANSACTION_EXPR_BODY (r) = push_stmt_list ();
8705 TREE_SIDE_EFFECTS (r) = 1;
8706 return r;
8709 /* End a __transaction_atomic or __transaction_relaxed statement.
8710 If COMPOUND_STMT is non-null, this is for a function-transaction-block,
8711 and we should end the compound. If NOEX is non-NULL, we wrap the body in
8712 a MUST_NOT_THROW_EXPR with NOEX as condition. */
8714 void
8715 finish_transaction_stmt (tree stmt, tree compound_stmt, int flags, tree noex)
8717 TRANSACTION_EXPR_BODY (stmt) = pop_stmt_list (TRANSACTION_EXPR_BODY (stmt));
8718 TRANSACTION_EXPR_OUTER (stmt) = (flags & TM_STMT_ATTR_OUTER) != 0;
8719 TRANSACTION_EXPR_RELAXED (stmt) = (flags & TM_STMT_ATTR_RELAXED) != 0;
8720 TRANSACTION_EXPR_IS_STMT (stmt) = 1;
8722 /* noexcept specifications are not allowed for function transactions. */
8723 gcc_assert (!(noex && compound_stmt));
8724 if (noex)
8726 tree body = build_must_not_throw_expr (TRANSACTION_EXPR_BODY (stmt),
8727 noex);
8728 protected_set_expr_location
8729 (body, EXPR_LOCATION (TRANSACTION_EXPR_BODY (stmt)));
8730 TREE_SIDE_EFFECTS (body) = 1;
8731 TRANSACTION_EXPR_BODY (stmt) = body;
8734 if (compound_stmt)
8735 finish_compound_stmt (compound_stmt);
8738 /* Build a __transaction_atomic or __transaction_relaxed expression. If
8739 NOEX is non-NULL, we wrap the body in a MUST_NOT_THROW_EXPR with NOEX as
8740 condition. */
8742 tree
8743 build_transaction_expr (location_t loc, tree expr, int flags, tree noex)
8745 tree ret;
8746 if (noex)
8748 expr = build_must_not_throw_expr (expr, noex);
8749 protected_set_expr_location (expr, loc);
8750 TREE_SIDE_EFFECTS (expr) = 1;
8752 ret = build1 (TRANSACTION_EXPR, TREE_TYPE (expr), expr);
8753 if (flags & TM_STMT_ATTR_RELAXED)
8754 TRANSACTION_EXPR_RELAXED (ret) = 1;
8755 TREE_SIDE_EFFECTS (ret) = 1;
8756 SET_EXPR_LOCATION (ret, loc);
8757 return ret;
8760 void
8761 init_cp_semantics (void)
8765 /* Build a STATIC_ASSERT for a static assertion with the condition
8766 CONDITION and the message text MESSAGE. LOCATION is the location
8767 of the static assertion in the source code. When MEMBER_P, this
8768 static assertion is a member of a class. */
8769 void
8770 finish_static_assert (tree condition, tree message, location_t location,
8771 bool member_p)
8773 if (message == NULL_TREE
8774 || message == error_mark_node
8775 || condition == NULL_TREE
8776 || condition == error_mark_node)
8777 return;
8779 if (check_for_bare_parameter_packs (condition))
8780 condition = error_mark_node;
8782 if (type_dependent_expression_p (condition)
8783 || value_dependent_expression_p (condition))
8785 /* We're in a template; build a STATIC_ASSERT and put it in
8786 the right place. */
8787 tree assertion;
8789 assertion = make_node (STATIC_ASSERT);
8790 STATIC_ASSERT_CONDITION (assertion) = condition;
8791 STATIC_ASSERT_MESSAGE (assertion) = message;
8792 STATIC_ASSERT_SOURCE_LOCATION (assertion) = location;
8794 if (member_p)
8795 maybe_add_class_template_decl_list (current_class_type,
8796 assertion,
8797 /*friend_p=*/0);
8798 else
8799 add_stmt (assertion);
8801 return;
8804 /* Fold the expression and convert it to a boolean value. */
8805 condition = instantiate_non_dependent_expr (condition);
8806 condition = cp_convert (boolean_type_node, condition, tf_warning_or_error);
8807 condition = maybe_constant_value (condition);
8809 if (TREE_CODE (condition) == INTEGER_CST && !integer_zerop (condition))
8810 /* Do nothing; the condition is satisfied. */
8812 else
8814 location_t saved_loc = input_location;
8816 input_location = location;
8817 if (TREE_CODE (condition) == INTEGER_CST
8818 && integer_zerop (condition))
8820 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
8821 (TREE_TYPE (TREE_TYPE (message))));
8822 int len = TREE_STRING_LENGTH (message) / sz - 1;
8823 /* Report the error. */
8824 if (len == 0)
8825 error ("static assertion failed");
8826 else
8827 error ("static assertion failed: %s",
8828 TREE_STRING_POINTER (message));
8830 else if (condition && condition != error_mark_node)
8832 error ("non-constant condition for static assertion");
8833 if (require_potential_rvalue_constant_expression (condition))
8834 cxx_constant_value (condition);
8836 input_location = saved_loc;
8840 /* Implements the C++0x decltype keyword. Returns the type of EXPR,
8841 suitable for use as a type-specifier.
8843 ID_EXPRESSION_OR_MEMBER_ACCESS_P is true when EXPR was parsed as an
8844 id-expression or a class member access, FALSE when it was parsed as
8845 a full expression. */
8847 tree
8848 finish_decltype_type (tree expr, bool id_expression_or_member_access_p,
8849 tsubst_flags_t complain)
8851 tree type = NULL_TREE;
8853 if (!expr || error_operand_p (expr))
8854 return error_mark_node;
8856 if (TYPE_P (expr)
8857 || TREE_CODE (expr) == TYPE_DECL
8858 || (TREE_CODE (expr) == BIT_NOT_EXPR
8859 && TYPE_P (TREE_OPERAND (expr, 0))))
8861 if (complain & tf_error)
8862 error ("argument to decltype must be an expression");
8863 return error_mark_node;
8866 /* Depending on the resolution of DR 1172, we may later need to distinguish
8867 instantiation-dependent but not type-dependent expressions so that, say,
8868 A<decltype(sizeof(T))>::U doesn't require 'typename'. */
8869 if (instantiation_dependent_uneval_expression_p (expr))
8871 type = cxx_make_type (DECLTYPE_TYPE);
8872 DECLTYPE_TYPE_EXPR (type) = expr;
8873 DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (type)
8874 = id_expression_or_member_access_p;
8875 SET_TYPE_STRUCTURAL_EQUALITY (type);
8877 return type;
8880 /* The type denoted by decltype(e) is defined as follows: */
8882 expr = resolve_nondeduced_context (expr, complain);
8884 if (invalid_nonstatic_memfn_p (input_location, expr, complain))
8885 return error_mark_node;
8887 if (type_unknown_p (expr))
8889 if (complain & tf_error)
8890 error ("decltype cannot resolve address of overloaded function");
8891 return error_mark_node;
8894 /* To get the size of a static data member declared as an array of
8895 unknown bound, we need to instantiate it. */
8896 if (VAR_P (expr)
8897 && VAR_HAD_UNKNOWN_BOUND (expr)
8898 && DECL_TEMPLATE_INSTANTIATION (expr))
8899 instantiate_decl (expr, /*defer_ok*/true, /*expl_inst_mem*/false);
8901 if (id_expression_or_member_access_p)
8903 /* If e is an id-expression or a class member access (5.2.5
8904 [expr.ref]), decltype(e) is defined as the type of the entity
8905 named by e. If there is no such entity, or e names a set of
8906 overloaded functions, the program is ill-formed. */
8907 if (identifier_p (expr))
8908 expr = lookup_name (expr);
8910 if (INDIRECT_REF_P (expr))
8911 /* This can happen when the expression is, e.g., "a.b". Just
8912 look at the underlying operand. */
8913 expr = TREE_OPERAND (expr, 0);
8915 if (TREE_CODE (expr) == OFFSET_REF
8916 || TREE_CODE (expr) == MEMBER_REF
8917 || TREE_CODE (expr) == SCOPE_REF)
8918 /* We're only interested in the field itself. If it is a
8919 BASELINK, we will need to see through it in the next
8920 step. */
8921 expr = TREE_OPERAND (expr, 1);
8923 if (BASELINK_P (expr))
8924 /* See through BASELINK nodes to the underlying function. */
8925 expr = BASELINK_FUNCTIONS (expr);
8927 /* decltype of a decomposition name drops references in the tuple case
8928 (unlike decltype of a normal variable) and keeps cv-qualifiers from
8929 the containing object in the other cases (unlike decltype of a member
8930 access expression). */
8931 if (DECL_DECOMPOSITION_P (expr))
8933 if (DECL_HAS_VALUE_EXPR_P (expr))
8934 /* Expr is an array or struct subobject proxy, handle
8935 bit-fields properly. */
8936 return unlowered_expr_type (expr);
8937 else
8938 /* Expr is a reference variable for the tuple case. */
8939 return lookup_decomp_type (expr);
8942 switch (TREE_CODE (expr))
8944 case FIELD_DECL:
8945 if (DECL_BIT_FIELD_TYPE (expr))
8947 type = DECL_BIT_FIELD_TYPE (expr);
8948 break;
8950 /* Fall through for fields that aren't bitfields. */
8951 gcc_fallthrough ();
8953 case FUNCTION_DECL:
8954 case VAR_DECL:
8955 case CONST_DECL:
8956 case PARM_DECL:
8957 case RESULT_DECL:
8958 case TEMPLATE_PARM_INDEX:
8959 expr = mark_type_use (expr);
8960 type = TREE_TYPE (expr);
8961 break;
8963 case ERROR_MARK:
8964 type = error_mark_node;
8965 break;
8967 case COMPONENT_REF:
8968 case COMPOUND_EXPR:
8969 mark_type_use (expr);
8970 type = is_bitfield_expr_with_lowered_type (expr);
8971 if (!type)
8972 type = TREE_TYPE (TREE_OPERAND (expr, 1));
8973 break;
8975 case BIT_FIELD_REF:
8976 gcc_unreachable ();
8978 case INTEGER_CST:
8979 case PTRMEM_CST:
8980 /* We can get here when the id-expression refers to an
8981 enumerator or non-type template parameter. */
8982 type = TREE_TYPE (expr);
8983 break;
8985 default:
8986 /* Handle instantiated template non-type arguments. */
8987 type = TREE_TYPE (expr);
8988 break;
8991 else
8993 /* Within a lambda-expression:
8995 Every occurrence of decltype((x)) where x is a possibly
8996 parenthesized id-expression that names an entity of
8997 automatic storage duration is treated as if x were
8998 transformed into an access to a corresponding data member
8999 of the closure type that would have been declared if x
9000 were a use of the denoted entity. */
9001 if (outer_automatic_var_p (expr)
9002 && current_function_decl
9003 && LAMBDA_FUNCTION_P (current_function_decl))
9004 type = capture_decltype (expr);
9005 else if (error_operand_p (expr))
9006 type = error_mark_node;
9007 else if (expr == current_class_ptr)
9008 /* If the expression is just "this", we want the
9009 cv-unqualified pointer for the "this" type. */
9010 type = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
9011 else
9013 /* Otherwise, where T is the type of e, if e is an lvalue,
9014 decltype(e) is defined as T&; if an xvalue, T&&; otherwise, T. */
9015 cp_lvalue_kind clk = lvalue_kind (expr);
9016 type = unlowered_expr_type (expr);
9017 gcc_assert (TREE_CODE (type) != REFERENCE_TYPE);
9019 /* For vector types, pick a non-opaque variant. */
9020 if (VECTOR_TYPE_P (type))
9021 type = strip_typedefs (type);
9023 if (clk != clk_none && !(clk & clk_class))
9024 type = cp_build_reference_type (type, (clk & clk_rvalueref));
9028 return type;
9031 /* Called from trait_expr_value to evaluate either __has_nothrow_assign or
9032 __has_nothrow_copy, depending on assign_p. */
9034 static bool
9035 classtype_has_nothrow_assign_or_copy_p (tree type, bool assign_p)
9037 tree fns;
9039 if (assign_p)
9041 int ix;
9042 ix = lookup_fnfields_1 (type, cp_assignment_operator_id (NOP_EXPR));
9043 if (ix < 0)
9044 return false;
9045 fns = (*CLASSTYPE_METHOD_VEC (type))[ix];
9047 else if (TYPE_HAS_COPY_CTOR (type))
9049 /* If construction of the copy constructor was postponed, create
9050 it now. */
9051 if (CLASSTYPE_LAZY_COPY_CTOR (type))
9052 lazily_declare_fn (sfk_copy_constructor, type);
9053 if (CLASSTYPE_LAZY_MOVE_CTOR (type))
9054 lazily_declare_fn (sfk_move_constructor, type);
9055 fns = CLASSTYPE_CONSTRUCTORS (type);
9057 else
9058 return false;
9060 for (; fns; fns = OVL_NEXT (fns))
9062 tree fn = OVL_CURRENT (fns);
9064 if (assign_p)
9066 if (copy_fn_p (fn) == 0)
9067 continue;
9069 else if (copy_fn_p (fn) <= 0)
9070 continue;
9072 maybe_instantiate_noexcept (fn);
9073 if (!TYPE_NOTHROW_P (TREE_TYPE (fn)))
9074 return false;
9077 return true;
9080 /* Actually evaluates the trait. */
9082 static bool
9083 trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
9085 enum tree_code type_code1;
9086 tree t;
9088 type_code1 = TREE_CODE (type1);
9090 switch (kind)
9092 case CPTK_HAS_NOTHROW_ASSIGN:
9093 type1 = strip_array_types (type1);
9094 return (!CP_TYPE_CONST_P (type1) && type_code1 != REFERENCE_TYPE
9095 && (trait_expr_value (CPTK_HAS_TRIVIAL_ASSIGN, type1, type2)
9096 || (CLASS_TYPE_P (type1)
9097 && classtype_has_nothrow_assign_or_copy_p (type1,
9098 true))));
9100 case CPTK_HAS_TRIVIAL_ASSIGN:
9101 /* ??? The standard seems to be missing the "or array of such a class
9102 type" wording for this trait. */
9103 type1 = strip_array_types (type1);
9104 return (!CP_TYPE_CONST_P (type1) && type_code1 != REFERENCE_TYPE
9105 && (trivial_type_p (type1)
9106 || (CLASS_TYPE_P (type1)
9107 && TYPE_HAS_TRIVIAL_COPY_ASSIGN (type1))));
9109 case CPTK_HAS_NOTHROW_CONSTRUCTOR:
9110 type1 = strip_array_types (type1);
9111 return (trait_expr_value (CPTK_HAS_TRIVIAL_CONSTRUCTOR, type1, type2)
9112 || (CLASS_TYPE_P (type1)
9113 && (t = locate_ctor (type1))
9114 && (maybe_instantiate_noexcept (t),
9115 TYPE_NOTHROW_P (TREE_TYPE (t)))));
9117 case CPTK_HAS_TRIVIAL_CONSTRUCTOR:
9118 type1 = strip_array_types (type1);
9119 return (trivial_type_p (type1)
9120 || (CLASS_TYPE_P (type1) && TYPE_HAS_TRIVIAL_DFLT (type1)));
9122 case CPTK_HAS_NOTHROW_COPY:
9123 type1 = strip_array_types (type1);
9124 return (trait_expr_value (CPTK_HAS_TRIVIAL_COPY, type1, type2)
9125 || (CLASS_TYPE_P (type1)
9126 && classtype_has_nothrow_assign_or_copy_p (type1, false)));
9128 case CPTK_HAS_TRIVIAL_COPY:
9129 /* ??? The standard seems to be missing the "or array of such a class
9130 type" wording for this trait. */
9131 type1 = strip_array_types (type1);
9132 return (trivial_type_p (type1) || type_code1 == REFERENCE_TYPE
9133 || (CLASS_TYPE_P (type1) && TYPE_HAS_TRIVIAL_COPY_CTOR (type1)));
9135 case CPTK_HAS_TRIVIAL_DESTRUCTOR:
9136 type1 = strip_array_types (type1);
9137 return (trivial_type_p (type1) || type_code1 == REFERENCE_TYPE
9138 || (CLASS_TYPE_P (type1)
9139 && TYPE_HAS_TRIVIAL_DESTRUCTOR (type1)));
9141 case CPTK_HAS_VIRTUAL_DESTRUCTOR:
9142 return type_has_virtual_destructor (type1);
9144 case CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS:
9145 return type_has_unique_obj_representations (type1);
9147 case CPTK_IS_ABSTRACT:
9148 return ABSTRACT_CLASS_TYPE_P (type1);
9150 case CPTK_IS_AGGREGATE:
9151 return CP_AGGREGATE_TYPE_P (type1);
9153 case CPTK_IS_BASE_OF:
9154 return (NON_UNION_CLASS_TYPE_P (type1) && NON_UNION_CLASS_TYPE_P (type2)
9155 && (same_type_ignoring_top_level_qualifiers_p (type1, type2)
9156 || DERIVED_FROM_P (type1, type2)));
9158 case CPTK_IS_CLASS:
9159 return NON_UNION_CLASS_TYPE_P (type1);
9161 case CPTK_IS_EMPTY:
9162 return NON_UNION_CLASS_TYPE_P (type1) && CLASSTYPE_EMPTY_P (type1);
9164 case CPTK_IS_ENUM:
9165 return type_code1 == ENUMERAL_TYPE;
9167 case CPTK_IS_FINAL:
9168 return CLASS_TYPE_P (type1) && CLASSTYPE_FINAL (type1);
9170 case CPTK_IS_LITERAL_TYPE:
9171 return literal_type_p (type1);
9173 case CPTK_IS_POD:
9174 return pod_type_p (type1);
9176 case CPTK_IS_POLYMORPHIC:
9177 return CLASS_TYPE_P (type1) && TYPE_POLYMORPHIC_P (type1);
9179 case CPTK_IS_SAME_AS:
9180 return same_type_p (type1, type2);
9182 case CPTK_IS_STD_LAYOUT:
9183 return std_layout_type_p (type1);
9185 case CPTK_IS_TRIVIAL:
9186 return trivial_type_p (type1);
9188 case CPTK_IS_TRIVIALLY_ASSIGNABLE:
9189 return is_trivially_xible (MODIFY_EXPR, type1, type2);
9191 case CPTK_IS_TRIVIALLY_CONSTRUCTIBLE:
9192 return is_trivially_xible (INIT_EXPR, type1, type2);
9194 case CPTK_IS_TRIVIALLY_COPYABLE:
9195 return trivially_copyable_p (type1);
9197 case CPTK_IS_UNION:
9198 return type_code1 == UNION_TYPE;
9200 default:
9201 gcc_unreachable ();
9202 return false;
9206 /* If TYPE is an array of unknown bound, or (possibly cv-qualified)
9207 void, or a complete type, returns true, otherwise false. */
9209 static bool
9210 check_trait_type (tree type)
9212 if (type == NULL_TREE)
9213 return true;
9215 if (TREE_CODE (type) == TREE_LIST)
9216 return (check_trait_type (TREE_VALUE (type))
9217 && check_trait_type (TREE_CHAIN (type)));
9219 if (TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type)
9220 && COMPLETE_TYPE_P (TREE_TYPE (type)))
9221 return true;
9223 if (VOID_TYPE_P (type))
9224 return true;
9226 return !!complete_type_or_else (strip_array_types (type), NULL_TREE);
9229 /* Process a trait expression. */
9231 tree
9232 finish_trait_expr (cp_trait_kind kind, tree type1, tree type2)
9234 if (type1 == error_mark_node
9235 || type2 == error_mark_node)
9236 return error_mark_node;
9238 if (processing_template_decl)
9240 tree trait_expr = make_node (TRAIT_EXPR);
9241 TREE_TYPE (trait_expr) = boolean_type_node;
9242 TRAIT_EXPR_TYPE1 (trait_expr) = type1;
9243 TRAIT_EXPR_TYPE2 (trait_expr) = type2;
9244 TRAIT_EXPR_KIND (trait_expr) = kind;
9245 return trait_expr;
9248 switch (kind)
9250 case CPTK_HAS_NOTHROW_ASSIGN:
9251 case CPTK_HAS_TRIVIAL_ASSIGN:
9252 case CPTK_HAS_NOTHROW_CONSTRUCTOR:
9253 case CPTK_HAS_TRIVIAL_CONSTRUCTOR:
9254 case CPTK_HAS_NOTHROW_COPY:
9255 case CPTK_HAS_TRIVIAL_COPY:
9256 case CPTK_HAS_TRIVIAL_DESTRUCTOR:
9257 case CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS:
9258 case CPTK_HAS_VIRTUAL_DESTRUCTOR:
9259 case CPTK_IS_ABSTRACT:
9260 case CPTK_IS_AGGREGATE:
9261 case CPTK_IS_EMPTY:
9262 case CPTK_IS_FINAL:
9263 case CPTK_IS_LITERAL_TYPE:
9264 case CPTK_IS_POD:
9265 case CPTK_IS_POLYMORPHIC:
9266 case CPTK_IS_STD_LAYOUT:
9267 case CPTK_IS_TRIVIAL:
9268 case CPTK_IS_TRIVIALLY_COPYABLE:
9269 if (!check_trait_type (type1))
9270 return error_mark_node;
9271 break;
9273 case CPTK_IS_TRIVIALLY_ASSIGNABLE:
9274 case CPTK_IS_TRIVIALLY_CONSTRUCTIBLE:
9275 if (!check_trait_type (type1)
9276 || !check_trait_type (type2))
9277 return error_mark_node;
9278 break;
9280 case CPTK_IS_BASE_OF:
9281 if (NON_UNION_CLASS_TYPE_P (type1) && NON_UNION_CLASS_TYPE_P (type2)
9282 && !same_type_ignoring_top_level_qualifiers_p (type1, type2)
9283 && !complete_type_or_else (type2, NULL_TREE))
9284 /* We already issued an error. */
9285 return error_mark_node;
9286 break;
9288 case CPTK_IS_CLASS:
9289 case CPTK_IS_ENUM:
9290 case CPTK_IS_UNION:
9291 case CPTK_IS_SAME_AS:
9292 break;
9294 default:
9295 gcc_unreachable ();
9298 return (trait_expr_value (kind, type1, type2)
9299 ? boolean_true_node : boolean_false_node);
9302 /* Do-nothing variants of functions to handle pragma FLOAT_CONST_DECIMAL64,
9303 which is ignored for C++. */
9305 void
9306 set_float_const_decimal64 (void)
9310 void
9311 clear_float_const_decimal64 (void)
9315 bool
9316 float_const_decimal64_p (void)
9318 return 0;
9322 /* Return true if T designates the implied `this' parameter. */
9324 bool
9325 is_this_parameter (tree t)
9327 if (!DECL_P (t) || DECL_NAME (t) != this_identifier)
9328 return false;
9329 gcc_assert (TREE_CODE (t) == PARM_DECL || is_capture_proxy (t)
9330 || (cp_binding_oracle && TREE_CODE (t) == VAR_DECL));
9331 return true;
9334 /* Insert the deduced return type for an auto function. */
9336 void
9337 apply_deduced_return_type (tree fco, tree return_type)
9339 tree result;
9341 if (return_type == error_mark_node)
9342 return;
9344 if (LAMBDA_FUNCTION_P (fco))
9346 tree lambda = CLASSTYPE_LAMBDA_EXPR (current_class_type);
9347 LAMBDA_EXPR_RETURN_TYPE (lambda) = return_type;
9350 if (DECL_CONV_FN_P (fco))
9351 DECL_NAME (fco) = mangle_conv_op_name_for_type (return_type);
9353 TREE_TYPE (fco) = change_return_type (return_type, TREE_TYPE (fco));
9355 result = DECL_RESULT (fco);
9356 if (result == NULL_TREE)
9357 return;
9358 if (TREE_TYPE (result) == return_type)
9359 return;
9361 if (!processing_template_decl && !VOID_TYPE_P (return_type)
9362 && !complete_type_or_else (return_type, NULL_TREE))
9363 return;
9365 /* We already have a DECL_RESULT from start_preparsed_function.
9366 Now we need to redo the work it and allocate_struct_function
9367 did to reflect the new type. */
9368 gcc_assert (current_function_decl == fco);
9369 result = build_decl (input_location, RESULT_DECL, NULL_TREE,
9370 TYPE_MAIN_VARIANT (return_type));
9371 DECL_ARTIFICIAL (result) = 1;
9372 DECL_IGNORED_P (result) = 1;
9373 cp_apply_type_quals_to_decl (cp_type_quals (return_type),
9374 result);
9376 DECL_RESULT (fco) = result;
9378 if (!processing_template_decl)
9380 bool aggr = aggregate_value_p (result, fco);
9381 #ifdef PCC_STATIC_STRUCT_RETURN
9382 cfun->returns_pcc_struct = aggr;
9383 #endif
9384 cfun->returns_struct = aggr;
9389 /* DECL is a local variable or parameter from the surrounding scope of a
9390 lambda-expression. Returns the decltype for a use of the capture field
9391 for DECL even if it hasn't been captured yet. */
9393 static tree
9394 capture_decltype (tree decl)
9396 tree lam = CLASSTYPE_LAMBDA_EXPR (DECL_CONTEXT (current_function_decl));
9397 /* FIXME do lookup instead of list walk? */
9398 tree cap = value_member (decl, LAMBDA_EXPR_CAPTURE_LIST (lam));
9399 tree type;
9401 if (cap)
9402 type = TREE_TYPE (TREE_PURPOSE (cap));
9403 else
9404 switch (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lam))
9406 case CPLD_NONE:
9407 error ("%qD is not captured", decl);
9408 return error_mark_node;
9410 case CPLD_COPY:
9411 type = TREE_TYPE (decl);
9412 if (TREE_CODE (type) == REFERENCE_TYPE
9413 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
9414 type = TREE_TYPE (type);
9415 break;
9417 case CPLD_REFERENCE:
9418 type = TREE_TYPE (decl);
9419 if (TREE_CODE (type) != REFERENCE_TYPE)
9420 type = build_reference_type (TREE_TYPE (decl));
9421 break;
9423 default:
9424 gcc_unreachable ();
9427 if (TREE_CODE (type) != REFERENCE_TYPE)
9429 if (!LAMBDA_EXPR_MUTABLE_P (lam))
9430 type = cp_build_qualified_type (type, (cp_type_quals (type)
9431 |TYPE_QUAL_CONST));
9432 type = build_reference_type (type);
9434 return type;
9437 /* Build a unary fold expression of EXPR over OP. If IS_RIGHT is true,
9438 this is a right unary fold. Otherwise it is a left unary fold. */
9440 static tree
9441 finish_unary_fold_expr (tree expr, int op, tree_code dir)
9443 // Build a pack expansion (assuming expr has pack type).
9444 if (!uses_parameter_packs (expr))
9446 error_at (location_of (expr), "operand of fold expression has no "
9447 "unexpanded parameter packs");
9448 return error_mark_node;
9450 tree pack = make_pack_expansion (expr);
9452 // Build the fold expression.
9453 tree code = build_int_cstu (integer_type_node, abs (op));
9454 tree fold = build_min_nt_loc (UNKNOWN_LOCATION, dir, code, pack);
9455 FOLD_EXPR_MODIFY_P (fold) = (op < 0);
9456 return fold;
9459 tree
9460 finish_left_unary_fold_expr (tree expr, int op)
9462 return finish_unary_fold_expr (expr, op, UNARY_LEFT_FOLD_EXPR);
9465 tree
9466 finish_right_unary_fold_expr (tree expr, int op)
9468 return finish_unary_fold_expr (expr, op, UNARY_RIGHT_FOLD_EXPR);
9471 /* Build a binary fold expression over EXPR1 and EXPR2. The
9472 associativity of the fold is determined by EXPR1 and EXPR2 (whichever
9473 has an unexpanded parameter pack). */
9475 tree
9476 finish_binary_fold_expr (tree pack, tree init, int op, tree_code dir)
9478 pack = make_pack_expansion (pack);
9479 tree code = build_int_cstu (integer_type_node, abs (op));
9480 tree fold = build_min_nt_loc (UNKNOWN_LOCATION, dir, code, pack, init);
9481 FOLD_EXPR_MODIFY_P (fold) = (op < 0);
9482 return fold;
9485 tree
9486 finish_binary_fold_expr (tree expr1, tree expr2, int op)
9488 // Determine which expr has an unexpanded parameter pack and
9489 // set the pack and initial term.
9490 bool pack1 = uses_parameter_packs (expr1);
9491 bool pack2 = uses_parameter_packs (expr2);
9492 if (pack1 && !pack2)
9493 return finish_binary_fold_expr (expr1, expr2, op, BINARY_RIGHT_FOLD_EXPR);
9494 else if (pack2 && !pack1)
9495 return finish_binary_fold_expr (expr2, expr1, op, BINARY_LEFT_FOLD_EXPR);
9496 else
9498 if (pack1)
9499 error ("both arguments in binary fold have unexpanded parameter packs");
9500 else
9501 error ("no unexpanded parameter packs in binary fold");
9503 return error_mark_node;
9506 /* Finish __builtin_launder (arg). */
9508 tree
9509 finish_builtin_launder (location_t loc, tree arg, tsubst_flags_t complain)
9511 tree orig_arg = arg;
9512 if (!type_dependent_expression_p (arg))
9513 arg = decay_conversion (arg, complain);
9514 if (error_operand_p (arg))
9515 return error_mark_node;
9516 if (!type_dependent_expression_p (arg)
9517 && TREE_CODE (TREE_TYPE (arg)) != POINTER_TYPE)
9519 error_at (loc, "non-pointer argument to %<__builtin_launder%>");
9520 return error_mark_node;
9522 if (processing_template_decl)
9523 arg = orig_arg;
9524 return build_call_expr_internal_loc (loc, IFN_LAUNDER,
9525 TREE_TYPE (arg), 1, arg);
9528 #include "gt-cp-semantics.h"