* cp-tree.h (OVL_ARG_DEPENDENT): Delete.
[official-gcc.git] / gcc / cp / semantics.c
blob1ba961ec226c2bc94e7b496b499905a619c1a0c6
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.
309 If non-NULL, report failures to AFI. */
311 bool
312 perform_or_defer_access_check (tree binfo, tree decl, tree diag_decl,
313 tsubst_flags_t complain,
314 access_failure_info *afi)
316 int i;
317 deferred_access *ptr;
318 deferred_access_check *chk;
321 /* Exit if we are in a context that no access checking is performed.
323 if (deferred_access_no_check)
324 return true;
326 gcc_assert (TREE_CODE (binfo) == TREE_BINFO);
328 ptr = &deferred_access_stack->last ();
330 /* If we are not supposed to defer access checks, just check now. */
331 if (ptr->deferring_access_checks_kind == dk_no_deferred)
333 bool ok = enforce_access (binfo, decl, diag_decl, complain, afi);
334 return (complain & tf_error) ? true : ok;
337 /* See if we are already going to perform this check. */
338 FOR_EACH_VEC_SAFE_ELT (ptr->deferred_access_checks, i, chk)
340 if (chk->decl == decl && chk->binfo == binfo &&
341 chk->diag_decl == diag_decl)
343 return true;
346 /* If not, record the check. */
347 deferred_access_check new_access = {binfo, decl, diag_decl, input_location};
348 vec_safe_push (ptr->deferred_access_checks, new_access);
350 return true;
353 /* Returns nonzero if the current statement is a full expression,
354 i.e. temporaries created during that statement should be destroyed
355 at the end of the statement. */
358 stmts_are_full_exprs_p (void)
360 return current_stmt_tree ()->stmts_are_full_exprs_p;
363 /* T is a statement. Add it to the statement-tree. This is the C++
364 version. The C/ObjC frontends have a slightly different version of
365 this function. */
367 tree
368 add_stmt (tree t)
370 enum tree_code code = TREE_CODE (t);
372 if (EXPR_P (t) && code != LABEL_EXPR)
374 if (!EXPR_HAS_LOCATION (t))
375 SET_EXPR_LOCATION (t, input_location);
377 /* When we expand a statement-tree, we must know whether or not the
378 statements are full-expressions. We record that fact here. */
379 STMT_IS_FULL_EXPR_P (t) = stmts_are_full_exprs_p ();
382 if (code == LABEL_EXPR || code == CASE_LABEL_EXPR)
383 STATEMENT_LIST_HAS_LABEL (cur_stmt_list) = 1;
385 /* Add T to the statement-tree. Non-side-effect statements need to be
386 recorded during statement expressions. */
387 gcc_checking_assert (!stmt_list_stack->is_empty ());
388 append_to_statement_list_force (t, &cur_stmt_list);
390 return t;
393 /* Returns the stmt_tree to which statements are currently being added. */
395 stmt_tree
396 current_stmt_tree (void)
398 return (cfun
399 ? &cfun->language->base.x_stmt_tree
400 : &scope_chain->x_stmt_tree);
403 /* If statements are full expressions, wrap STMT in a CLEANUP_POINT_EXPR. */
405 static tree
406 maybe_cleanup_point_expr (tree expr)
408 if (!processing_template_decl && stmts_are_full_exprs_p ())
409 expr = fold_build_cleanup_point_expr (TREE_TYPE (expr), expr);
410 return expr;
413 /* Like maybe_cleanup_point_expr except have the type of the new expression be
414 void so we don't need to create a temporary variable to hold the inner
415 expression. The reason why we do this is because the original type might be
416 an aggregate and we cannot create a temporary variable for that type. */
418 tree
419 maybe_cleanup_point_expr_void (tree expr)
421 if (!processing_template_decl && stmts_are_full_exprs_p ())
422 expr = fold_build_cleanup_point_expr (void_type_node, expr);
423 return expr;
428 /* Create a declaration statement for the declaration given by the DECL. */
430 void
431 add_decl_expr (tree decl)
433 tree r = build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl);
434 if (DECL_INITIAL (decl)
435 || (DECL_SIZE (decl) && TREE_SIDE_EFFECTS (DECL_SIZE (decl))))
436 r = maybe_cleanup_point_expr_void (r);
437 add_stmt (r);
440 /* Finish a scope. */
442 tree
443 do_poplevel (tree stmt_list)
445 tree block = NULL;
447 if (stmts_are_full_exprs_p ())
448 block = poplevel (kept_level_p (), 1, 0);
450 stmt_list = pop_stmt_list (stmt_list);
452 if (!processing_template_decl)
454 stmt_list = c_build_bind_expr (input_location, block, stmt_list);
455 /* ??? See c_end_compound_stmt re statement expressions. */
458 return stmt_list;
461 /* Begin a new scope. */
463 static tree
464 do_pushlevel (scope_kind sk)
466 tree ret = push_stmt_list ();
467 if (stmts_are_full_exprs_p ())
468 begin_scope (sk, NULL);
469 return ret;
472 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
473 when the current scope is exited. EH_ONLY is true when this is not
474 meant to apply to normal control flow transfer. */
476 void
477 push_cleanup (tree decl, tree cleanup, bool eh_only)
479 tree stmt = build_stmt (input_location, CLEANUP_STMT, NULL, cleanup, decl);
480 CLEANUP_EH_ONLY (stmt) = eh_only;
481 add_stmt (stmt);
482 CLEANUP_BODY (stmt) = push_stmt_list ();
485 /* Simple infinite loop tracking for -Wreturn-type. We keep a stack of all
486 the current loops, represented by 'NULL_TREE' if we've seen a possible
487 exit, and 'error_mark_node' if not. This is currently used only to
488 suppress the warning about a function with no return statements, and
489 therefore we don't bother noting returns as possible exits. We also
490 don't bother with gotos. */
492 static void
493 begin_maybe_infinite_loop (tree cond)
495 /* Only track this while parsing a function, not during instantiation. */
496 if (!cfun || (DECL_TEMPLATE_INSTANTIATION (current_function_decl)
497 && !processing_template_decl))
498 return;
499 bool maybe_infinite = true;
500 if (cond)
502 cond = fold_non_dependent_expr (cond);
503 maybe_infinite = integer_nonzerop (cond);
505 vec_safe_push (cp_function_chain->infinite_loops,
506 maybe_infinite ? error_mark_node : NULL_TREE);
510 /* A break is a possible exit for the current loop. */
512 void
513 break_maybe_infinite_loop (void)
515 if (!cfun)
516 return;
517 cp_function_chain->infinite_loops->last() = NULL_TREE;
520 /* If we reach the end of the loop without seeing a possible exit, we have
521 an infinite loop. */
523 static void
524 end_maybe_infinite_loop (tree cond)
526 if (!cfun || (DECL_TEMPLATE_INSTANTIATION (current_function_decl)
527 && !processing_template_decl))
528 return;
529 tree current = cp_function_chain->infinite_loops->pop();
530 if (current != NULL_TREE)
532 cond = fold_non_dependent_expr (cond);
533 if (integer_nonzerop (cond))
534 current_function_infinite_loop = 1;
539 /* Begin a conditional that might contain a declaration. When generating
540 normal code, we want the declaration to appear before the statement
541 containing the conditional. When generating template code, we want the
542 conditional to be rendered as the raw DECL_EXPR. */
544 static void
545 begin_cond (tree *cond_p)
547 if (processing_template_decl)
548 *cond_p = push_stmt_list ();
551 /* Finish such a conditional. */
553 static void
554 finish_cond (tree *cond_p, tree expr)
556 if (processing_template_decl)
558 tree cond = pop_stmt_list (*cond_p);
560 if (expr == NULL_TREE)
561 /* Empty condition in 'for'. */
562 gcc_assert (empty_expr_stmt_p (cond));
563 else if (check_for_bare_parameter_packs (expr))
564 expr = error_mark_node;
565 else if (!empty_expr_stmt_p (cond))
566 expr = build2 (COMPOUND_EXPR, TREE_TYPE (expr), cond, expr);
568 *cond_p = expr;
571 /* If *COND_P specifies a conditional with a declaration, transform the
572 loop such that
573 while (A x = 42) { }
574 for (; A x = 42;) { }
575 becomes
576 while (true) { A x = 42; if (!x) break; }
577 for (;;) { A x = 42; if (!x) break; }
578 The statement list for BODY will be empty if the conditional did
579 not declare anything. */
581 static void
582 simplify_loop_decl_cond (tree *cond_p, tree body)
584 tree cond, if_stmt;
586 if (!TREE_SIDE_EFFECTS (body))
587 return;
589 cond = *cond_p;
590 *cond_p = boolean_true_node;
592 if_stmt = begin_if_stmt ();
593 cond = cp_build_unary_op (TRUTH_NOT_EXPR, cond, false, tf_warning_or_error);
594 finish_if_stmt_cond (cond, if_stmt);
595 finish_break_stmt ();
596 finish_then_clause (if_stmt);
597 finish_if_stmt (if_stmt);
600 /* Finish a goto-statement. */
602 tree
603 finish_goto_stmt (tree destination)
605 if (identifier_p (destination))
606 destination = lookup_label (destination);
608 /* We warn about unused labels with -Wunused. That means we have to
609 mark the used labels as used. */
610 if (TREE_CODE (destination) == LABEL_DECL)
611 TREE_USED (destination) = 1;
612 else
614 if (check_no_cilk (destination,
615 "Cilk array notation cannot be used as a computed goto expression",
616 "%<_Cilk_spawn%> statement cannot be used as a computed goto expression"))
617 destination = error_mark_node;
618 destination = mark_rvalue_use (destination);
619 if (!processing_template_decl)
621 destination = cp_convert (ptr_type_node, destination,
622 tf_warning_or_error);
623 if (error_operand_p (destination))
624 return NULL_TREE;
625 destination
626 = fold_build_cleanup_point_expr (TREE_TYPE (destination),
627 destination);
631 check_goto (destination);
633 return add_stmt (build_stmt (input_location, GOTO_EXPR, destination));
636 /* COND is the condition-expression for an if, while, etc.,
637 statement. Convert it to a boolean value, if appropriate.
638 In addition, verify sequence points if -Wsequence-point is enabled. */
640 static tree
641 maybe_convert_cond (tree cond)
643 /* Empty conditions remain empty. */
644 if (!cond)
645 return NULL_TREE;
647 /* Wait until we instantiate templates before doing conversion. */
648 if (processing_template_decl)
649 return cond;
651 if (warn_sequence_point)
652 verify_sequence_points (cond);
654 /* Do the conversion. */
655 cond = convert_from_reference (cond);
657 if (TREE_CODE (cond) == MODIFY_EXPR
658 && !TREE_NO_WARNING (cond)
659 && warn_parentheses)
661 warning_at (EXPR_LOC_OR_LOC (cond, input_location), OPT_Wparentheses,
662 "suggest parentheses around assignment used as truth value");
663 TREE_NO_WARNING (cond) = 1;
666 return condition_conversion (cond);
669 /* Finish an expression-statement, whose EXPRESSION is as indicated. */
671 tree
672 finish_expr_stmt (tree expr)
674 tree r = NULL_TREE;
675 location_t loc = EXPR_LOCATION (expr);
677 if (expr != NULL_TREE)
679 /* If we ran into a problem, make sure we complained. */
680 gcc_assert (expr != error_mark_node || seen_error ());
682 if (!processing_template_decl)
684 if (warn_sequence_point)
685 verify_sequence_points (expr);
686 expr = convert_to_void (expr, ICV_STATEMENT, tf_warning_or_error);
688 else if (!type_dependent_expression_p (expr))
689 convert_to_void (build_non_dependent_expr (expr), ICV_STATEMENT,
690 tf_warning_or_error);
692 if (check_for_bare_parameter_packs (expr))
693 expr = error_mark_node;
695 /* Simplification of inner statement expressions, compound exprs,
696 etc can result in us already having an EXPR_STMT. */
697 if (TREE_CODE (expr) != CLEANUP_POINT_EXPR)
699 if (TREE_CODE (expr) != EXPR_STMT)
700 expr = build_stmt (loc, EXPR_STMT, expr);
701 expr = maybe_cleanup_point_expr_void (expr);
704 r = add_stmt (expr);
707 return r;
711 /* Begin an if-statement. Returns a newly created IF_STMT if
712 appropriate. */
714 tree
715 begin_if_stmt (void)
717 tree r, scope;
718 scope = do_pushlevel (sk_cond);
719 r = build_stmt (input_location, IF_STMT, NULL_TREE,
720 NULL_TREE, NULL_TREE, scope);
721 current_binding_level->this_entity = r;
722 begin_cond (&IF_COND (r));
723 return r;
726 /* Process the COND of an if-statement, which may be given by
727 IF_STMT. */
729 tree
730 finish_if_stmt_cond (tree cond, tree if_stmt)
732 cond = maybe_convert_cond (cond);
733 if (IF_STMT_CONSTEXPR_P (if_stmt)
734 && require_potential_rvalue_constant_expression (cond)
735 && !value_dependent_expression_p (cond))
736 cond = cxx_constant_value (cond, NULL_TREE);
737 finish_cond (&IF_COND (if_stmt), cond);
738 add_stmt (if_stmt);
739 THEN_CLAUSE (if_stmt) = push_stmt_list ();
740 return cond;
743 /* Finish the then-clause of an if-statement, which may be given by
744 IF_STMT. */
746 tree
747 finish_then_clause (tree if_stmt)
749 THEN_CLAUSE (if_stmt) = pop_stmt_list (THEN_CLAUSE (if_stmt));
750 return if_stmt;
753 /* Begin the else-clause of an if-statement. */
755 void
756 begin_else_clause (tree if_stmt)
758 ELSE_CLAUSE (if_stmt) = push_stmt_list ();
761 /* Finish the else-clause of an if-statement, which may be given by
762 IF_STMT. */
764 void
765 finish_else_clause (tree if_stmt)
767 ELSE_CLAUSE (if_stmt) = pop_stmt_list (ELSE_CLAUSE (if_stmt));
770 /* Finish an if-statement. */
772 void
773 finish_if_stmt (tree if_stmt)
775 tree scope = IF_SCOPE (if_stmt);
776 IF_SCOPE (if_stmt) = NULL;
777 add_stmt (do_poplevel (scope));
780 /* Begin a while-statement. Returns a newly created WHILE_STMT if
781 appropriate. */
783 tree
784 begin_while_stmt (void)
786 tree r;
787 r = build_stmt (input_location, WHILE_STMT, NULL_TREE, NULL_TREE);
788 add_stmt (r);
789 WHILE_BODY (r) = do_pushlevel (sk_block);
790 begin_cond (&WHILE_COND (r));
791 return r;
794 /* Process the COND of a while-statement, which may be given by
795 WHILE_STMT. */
797 void
798 finish_while_stmt_cond (tree cond, tree while_stmt, bool ivdep)
800 if (check_no_cilk (cond,
801 "Cilk array notation cannot be used as a condition for while statement",
802 "%<_Cilk_spawn%> statement cannot be used as a condition for while statement"))
803 cond = error_mark_node;
804 cond = maybe_convert_cond (cond);
805 finish_cond (&WHILE_COND (while_stmt), cond);
806 begin_maybe_infinite_loop (cond);
807 if (ivdep && cond != error_mark_node)
808 WHILE_COND (while_stmt) = build2 (ANNOTATE_EXPR,
809 TREE_TYPE (WHILE_COND (while_stmt)),
810 WHILE_COND (while_stmt),
811 build_int_cst (integer_type_node,
812 annot_expr_ivdep_kind));
813 simplify_loop_decl_cond (&WHILE_COND (while_stmt), WHILE_BODY (while_stmt));
816 /* Finish a while-statement, which may be given by WHILE_STMT. */
818 void
819 finish_while_stmt (tree while_stmt)
821 end_maybe_infinite_loop (boolean_true_node);
822 WHILE_BODY (while_stmt) = do_poplevel (WHILE_BODY (while_stmt));
825 /* Begin a do-statement. Returns a newly created DO_STMT if
826 appropriate. */
828 tree
829 begin_do_stmt (void)
831 tree r = build_stmt (input_location, DO_STMT, NULL_TREE, NULL_TREE);
832 begin_maybe_infinite_loop (boolean_true_node);
833 add_stmt (r);
834 DO_BODY (r) = push_stmt_list ();
835 return r;
838 /* Finish the body of a do-statement, which may be given by DO_STMT. */
840 void
841 finish_do_body (tree do_stmt)
843 tree body = DO_BODY (do_stmt) = pop_stmt_list (DO_BODY (do_stmt));
845 if (TREE_CODE (body) == STATEMENT_LIST && STATEMENT_LIST_TAIL (body))
846 body = STATEMENT_LIST_TAIL (body)->stmt;
848 if (IS_EMPTY_STMT (body))
849 warning (OPT_Wempty_body,
850 "suggest explicit braces around empty body in %<do%> statement");
853 /* Finish a do-statement, which may be given by DO_STMT, and whose
854 COND is as indicated. */
856 void
857 finish_do_stmt (tree cond, tree do_stmt, bool ivdep)
859 if (check_no_cilk (cond,
860 "Cilk array notation cannot be used as a condition for a do-while statement",
861 "%<_Cilk_spawn%> statement cannot be used as a condition for a do-while statement"))
862 cond = error_mark_node;
863 cond = maybe_convert_cond (cond);
864 end_maybe_infinite_loop (cond);
865 if (ivdep && cond != error_mark_node)
866 cond = build2 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
867 build_int_cst (integer_type_node, annot_expr_ivdep_kind));
868 DO_COND (do_stmt) = cond;
871 /* Finish a return-statement. The EXPRESSION returned, if any, is as
872 indicated. */
874 tree
875 finish_return_stmt (tree expr)
877 tree r;
878 bool no_warning;
880 expr = check_return_expr (expr, &no_warning);
882 if (error_operand_p (expr)
883 || (flag_openmp && !check_omp_return ()))
885 /* Suppress -Wreturn-type for this function. */
886 if (warn_return_type)
887 TREE_NO_WARNING (current_function_decl) = true;
888 return error_mark_node;
891 if (!processing_template_decl)
893 if (warn_sequence_point)
894 verify_sequence_points (expr);
896 if (DECL_DESTRUCTOR_P (current_function_decl)
897 || (DECL_CONSTRUCTOR_P (current_function_decl)
898 && targetm.cxx.cdtor_returns_this ()))
900 /* Similarly, all destructors must run destructors for
901 base-classes before returning. So, all returns in a
902 destructor get sent to the DTOR_LABEL; finish_function emits
903 code to return a value there. */
904 return finish_goto_stmt (cdtor_label);
908 r = build_stmt (input_location, RETURN_EXPR, expr);
909 TREE_NO_WARNING (r) |= no_warning;
910 r = maybe_cleanup_point_expr_void (r);
911 r = add_stmt (r);
913 return r;
916 /* Begin the scope of a for-statement or a range-for-statement.
917 Both the returned trees are to be used in a call to
918 begin_for_stmt or begin_range_for_stmt. */
920 tree
921 begin_for_scope (tree *init)
923 tree scope = NULL_TREE;
924 if (flag_new_for_scope > 0)
925 scope = do_pushlevel (sk_for);
927 if (processing_template_decl)
928 *init = push_stmt_list ();
929 else
930 *init = NULL_TREE;
932 return scope;
935 /* Begin a for-statement. Returns a new FOR_STMT.
936 SCOPE and INIT should be the return of begin_for_scope,
937 or both NULL_TREE */
939 tree
940 begin_for_stmt (tree scope, tree init)
942 tree r;
944 r = build_stmt (input_location, FOR_STMT, NULL_TREE, NULL_TREE,
945 NULL_TREE, NULL_TREE, NULL_TREE);
947 if (scope == NULL_TREE)
949 gcc_assert (!init || !(flag_new_for_scope > 0));
950 if (!init)
951 scope = begin_for_scope (&init);
953 FOR_INIT_STMT (r) = init;
954 FOR_SCOPE (r) = scope;
956 return r;
959 /* Finish the init-statement of a for-statement, which may be
960 given by FOR_STMT. */
962 void
963 finish_init_stmt (tree for_stmt)
965 if (processing_template_decl)
966 FOR_INIT_STMT (for_stmt) = pop_stmt_list (FOR_INIT_STMT (for_stmt));
967 add_stmt (for_stmt);
968 FOR_BODY (for_stmt) = do_pushlevel (sk_block);
969 begin_cond (&FOR_COND (for_stmt));
972 /* Finish the COND of a for-statement, which may be given by
973 FOR_STMT. */
975 void
976 finish_for_cond (tree cond, tree for_stmt, bool ivdep)
978 if (check_no_cilk (cond,
979 "Cilk array notation cannot be used in a condition for a for-loop",
980 "%<_Cilk_spawn%> statement cannot be used in a condition for a for-loop"))
981 cond = error_mark_node;
982 cond = maybe_convert_cond (cond);
983 finish_cond (&FOR_COND (for_stmt), cond);
984 begin_maybe_infinite_loop (cond);
985 if (ivdep && cond != error_mark_node)
986 FOR_COND (for_stmt) = build2 (ANNOTATE_EXPR,
987 TREE_TYPE (FOR_COND (for_stmt)),
988 FOR_COND (for_stmt),
989 build_int_cst (integer_type_node,
990 annot_expr_ivdep_kind));
991 simplify_loop_decl_cond (&FOR_COND (for_stmt), FOR_BODY (for_stmt));
994 /* Finish the increment-EXPRESSION in a for-statement, which may be
995 given by FOR_STMT. */
997 void
998 finish_for_expr (tree expr, tree for_stmt)
1000 if (!expr)
1001 return;
1002 /* If EXPR is an overloaded function, issue an error; there is no
1003 context available to use to perform overload resolution. */
1004 if (type_unknown_p (expr))
1006 cxx_incomplete_type_error (expr, TREE_TYPE (expr));
1007 expr = error_mark_node;
1009 if (!processing_template_decl)
1011 if (warn_sequence_point)
1012 verify_sequence_points (expr);
1013 expr = convert_to_void (expr, ICV_THIRD_IN_FOR,
1014 tf_warning_or_error);
1016 else if (!type_dependent_expression_p (expr))
1017 convert_to_void (build_non_dependent_expr (expr), ICV_THIRD_IN_FOR,
1018 tf_warning_or_error);
1019 expr = maybe_cleanup_point_expr_void (expr);
1020 if (check_for_bare_parameter_packs (expr))
1021 expr = error_mark_node;
1022 FOR_EXPR (for_stmt) = expr;
1025 /* Finish the body of a for-statement, which may be given by
1026 FOR_STMT. The increment-EXPR for the loop must be
1027 provided.
1028 It can also finish RANGE_FOR_STMT. */
1030 void
1031 finish_for_stmt (tree for_stmt)
1033 end_maybe_infinite_loop (boolean_true_node);
1035 if (TREE_CODE (for_stmt) == RANGE_FOR_STMT)
1036 RANGE_FOR_BODY (for_stmt) = do_poplevel (RANGE_FOR_BODY (for_stmt));
1037 else
1038 FOR_BODY (for_stmt) = do_poplevel (FOR_BODY (for_stmt));
1040 /* Pop the scope for the body of the loop. */
1041 if (flag_new_for_scope > 0)
1043 tree scope;
1044 tree *scope_ptr = (TREE_CODE (for_stmt) == RANGE_FOR_STMT
1045 ? &RANGE_FOR_SCOPE (for_stmt)
1046 : &FOR_SCOPE (for_stmt));
1047 scope = *scope_ptr;
1048 *scope_ptr = NULL;
1049 add_stmt (do_poplevel (scope));
1053 /* Begin a range-for-statement. Returns a new RANGE_FOR_STMT.
1054 SCOPE and INIT should be the return of begin_for_scope,
1055 or both NULL_TREE .
1056 To finish it call finish_for_stmt(). */
1058 tree
1059 begin_range_for_stmt (tree scope, tree init)
1061 tree r;
1063 begin_maybe_infinite_loop (boolean_false_node);
1065 r = build_stmt (input_location, RANGE_FOR_STMT,
1066 NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE);
1068 if (scope == NULL_TREE)
1070 gcc_assert (!init || !(flag_new_for_scope > 0));
1071 if (!init)
1072 scope = begin_for_scope (&init);
1075 /* RANGE_FOR_STMTs do not use nor save the init tree, so we
1076 pop it now. */
1077 if (init)
1078 pop_stmt_list (init);
1079 RANGE_FOR_SCOPE (r) = scope;
1081 return r;
1084 /* Finish the head of a range-based for statement, which may
1085 be given by RANGE_FOR_STMT. DECL must be the declaration
1086 and EXPR must be the loop expression. */
1088 void
1089 finish_range_for_decl (tree range_for_stmt, tree decl, tree expr)
1091 RANGE_FOR_DECL (range_for_stmt) = decl;
1092 RANGE_FOR_EXPR (range_for_stmt) = expr;
1093 add_stmt (range_for_stmt);
1094 RANGE_FOR_BODY (range_for_stmt) = do_pushlevel (sk_block);
1097 /* Finish a break-statement. */
1099 tree
1100 finish_break_stmt (void)
1102 /* In switch statements break is sometimes stylistically used after
1103 a return statement. This can lead to spurious warnings about
1104 control reaching the end of a non-void function when it is
1105 inlined. Note that we are calling block_may_fallthru with
1106 language specific tree nodes; this works because
1107 block_may_fallthru returns true when given something it does not
1108 understand. */
1109 if (!block_may_fallthru (cur_stmt_list))
1110 return void_node;
1111 return add_stmt (build_stmt (input_location, BREAK_STMT));
1114 /* Finish a continue-statement. */
1116 tree
1117 finish_continue_stmt (void)
1119 return add_stmt (build_stmt (input_location, CONTINUE_STMT));
1122 /* Begin a switch-statement. Returns a new SWITCH_STMT if
1123 appropriate. */
1125 tree
1126 begin_switch_stmt (void)
1128 tree r, scope;
1130 scope = do_pushlevel (sk_cond);
1131 r = build_stmt (input_location, SWITCH_STMT, NULL_TREE, NULL_TREE, NULL_TREE, scope);
1133 begin_cond (&SWITCH_STMT_COND (r));
1135 return r;
1138 /* Finish the cond of a switch-statement. */
1140 void
1141 finish_switch_cond (tree cond, tree switch_stmt)
1143 tree orig_type = NULL;
1145 if (check_no_cilk (cond,
1146 "Cilk array notation cannot be used as a condition for switch statement",
1147 "%<_Cilk_spawn%> statement cannot be used as a condition for switch statement"))
1148 cond = error_mark_node;
1150 if (!processing_template_decl)
1152 /* Convert the condition to an integer or enumeration type. */
1153 cond = build_expr_type_conversion (WANT_INT | WANT_ENUM, cond, true);
1154 if (cond == NULL_TREE)
1156 error ("switch quantity not an integer");
1157 cond = error_mark_node;
1159 /* We want unlowered type here to handle enum bit-fields. */
1160 orig_type = unlowered_expr_type (cond);
1161 if (TREE_CODE (orig_type) != ENUMERAL_TYPE)
1162 orig_type = TREE_TYPE (cond);
1163 if (cond != error_mark_node)
1165 /* [stmt.switch]
1167 Integral promotions are performed. */
1168 cond = perform_integral_promotions (cond);
1169 cond = maybe_cleanup_point_expr (cond);
1172 if (check_for_bare_parameter_packs (cond))
1173 cond = error_mark_node;
1174 else if (!processing_template_decl && warn_sequence_point)
1175 verify_sequence_points (cond);
1177 finish_cond (&SWITCH_STMT_COND (switch_stmt), cond);
1178 SWITCH_STMT_TYPE (switch_stmt) = orig_type;
1179 add_stmt (switch_stmt);
1180 push_switch (switch_stmt);
1181 SWITCH_STMT_BODY (switch_stmt) = push_stmt_list ();
1184 /* Finish the body of a switch-statement, which may be given by
1185 SWITCH_STMT. The COND to switch on is indicated. */
1187 void
1188 finish_switch_stmt (tree switch_stmt)
1190 tree scope;
1192 SWITCH_STMT_BODY (switch_stmt) =
1193 pop_stmt_list (SWITCH_STMT_BODY (switch_stmt));
1194 pop_switch ();
1196 scope = SWITCH_STMT_SCOPE (switch_stmt);
1197 SWITCH_STMT_SCOPE (switch_stmt) = NULL;
1198 add_stmt (do_poplevel (scope));
1201 /* Begin a try-block. Returns a newly-created TRY_BLOCK if
1202 appropriate. */
1204 tree
1205 begin_try_block (void)
1207 tree r = build_stmt (input_location, TRY_BLOCK, NULL_TREE, NULL_TREE);
1208 add_stmt (r);
1209 TRY_STMTS (r) = push_stmt_list ();
1210 return r;
1213 /* Likewise, for a function-try-block. The block returned in
1214 *COMPOUND_STMT is an artificial outer scope, containing the
1215 function-try-block. */
1217 tree
1218 begin_function_try_block (tree *compound_stmt)
1220 tree r;
1221 /* This outer scope does not exist in the C++ standard, but we need
1222 a place to put __FUNCTION__ and similar variables. */
1223 *compound_stmt = begin_compound_stmt (0);
1224 r = begin_try_block ();
1225 FN_TRY_BLOCK_P (r) = 1;
1226 return r;
1229 /* Finish a try-block, which may be given by TRY_BLOCK. */
1231 void
1232 finish_try_block (tree try_block)
1234 TRY_STMTS (try_block) = pop_stmt_list (TRY_STMTS (try_block));
1235 TRY_HANDLERS (try_block) = push_stmt_list ();
1238 /* Finish the body of a cleanup try-block, which may be given by
1239 TRY_BLOCK. */
1241 void
1242 finish_cleanup_try_block (tree try_block)
1244 TRY_STMTS (try_block) = pop_stmt_list (TRY_STMTS (try_block));
1247 /* Finish an implicitly generated try-block, with a cleanup is given
1248 by CLEANUP. */
1250 void
1251 finish_cleanup (tree cleanup, tree try_block)
1253 TRY_HANDLERS (try_block) = cleanup;
1254 CLEANUP_P (try_block) = 1;
1257 /* Likewise, for a function-try-block. */
1259 void
1260 finish_function_try_block (tree try_block)
1262 finish_try_block (try_block);
1263 /* FIXME : something queer about CTOR_INITIALIZER somehow following
1264 the try block, but moving it inside. */
1265 in_function_try_handler = 1;
1268 /* Finish a handler-sequence for a try-block, which may be given by
1269 TRY_BLOCK. */
1271 void
1272 finish_handler_sequence (tree try_block)
1274 TRY_HANDLERS (try_block) = pop_stmt_list (TRY_HANDLERS (try_block));
1275 check_handlers (TRY_HANDLERS (try_block));
1278 /* Finish the handler-seq for a function-try-block, given by
1279 TRY_BLOCK. COMPOUND_STMT is the outer block created by
1280 begin_function_try_block. */
1282 void
1283 finish_function_handler_sequence (tree try_block, tree compound_stmt)
1285 in_function_try_handler = 0;
1286 finish_handler_sequence (try_block);
1287 finish_compound_stmt (compound_stmt);
1290 /* Begin a handler. Returns a HANDLER if appropriate. */
1292 tree
1293 begin_handler (void)
1295 tree r;
1297 r = build_stmt (input_location, HANDLER, NULL_TREE, NULL_TREE);
1298 add_stmt (r);
1300 /* Create a binding level for the eh_info and the exception object
1301 cleanup. */
1302 HANDLER_BODY (r) = do_pushlevel (sk_catch);
1304 return r;
1307 /* Finish the handler-parameters for a handler, which may be given by
1308 HANDLER. DECL is the declaration for the catch parameter, or NULL
1309 if this is a `catch (...)' clause. */
1311 void
1312 finish_handler_parms (tree decl, tree handler)
1314 tree type = NULL_TREE;
1315 if (processing_template_decl)
1317 if (decl)
1319 decl = pushdecl (decl);
1320 decl = push_template_decl (decl);
1321 HANDLER_PARMS (handler) = decl;
1322 type = TREE_TYPE (decl);
1325 else
1326 type = expand_start_catch_block (decl);
1327 HANDLER_TYPE (handler) = type;
1330 /* Finish a handler, which may be given by HANDLER. The BLOCKs are
1331 the return value from the matching call to finish_handler_parms. */
1333 void
1334 finish_handler (tree handler)
1336 if (!processing_template_decl)
1337 expand_end_catch_block ();
1338 HANDLER_BODY (handler) = do_poplevel (HANDLER_BODY (handler));
1341 /* Begin a compound statement. FLAGS contains some bits that control the
1342 behavior and context. If BCS_NO_SCOPE is set, the compound statement
1343 does not define a scope. If BCS_FN_BODY is set, this is the outermost
1344 block of a function. If BCS_TRY_BLOCK is set, this is the block
1345 created on behalf of a TRY statement. Returns a token to be passed to
1346 finish_compound_stmt. */
1348 tree
1349 begin_compound_stmt (unsigned int flags)
1351 tree r;
1353 if (flags & BCS_NO_SCOPE)
1355 r = push_stmt_list ();
1356 STATEMENT_LIST_NO_SCOPE (r) = 1;
1358 /* Normally, we try hard to keep the BLOCK for a statement-expression.
1359 But, if it's a statement-expression with a scopeless block, there's
1360 nothing to keep, and we don't want to accidentally keep a block
1361 *inside* the scopeless block. */
1362 keep_next_level (false);
1364 else
1366 scope_kind sk = sk_block;
1367 if (flags & BCS_TRY_BLOCK)
1368 sk = sk_try;
1369 else if (flags & BCS_TRANSACTION)
1370 sk = sk_transaction;
1371 r = do_pushlevel (sk);
1374 /* When processing a template, we need to remember where the braces were,
1375 so that we can set up identical scopes when instantiating the template
1376 later. BIND_EXPR is a handy candidate for this.
1377 Note that do_poplevel won't create a BIND_EXPR itself here (and thus
1378 result in nested BIND_EXPRs), since we don't build BLOCK nodes when
1379 processing templates. */
1380 if (processing_template_decl)
1382 r = build3 (BIND_EXPR, NULL, NULL, r, NULL);
1383 BIND_EXPR_TRY_BLOCK (r) = (flags & BCS_TRY_BLOCK) != 0;
1384 BIND_EXPR_BODY_BLOCK (r) = (flags & BCS_FN_BODY) != 0;
1385 TREE_SIDE_EFFECTS (r) = 1;
1388 return r;
1391 /* Finish a compound-statement, which is given by STMT. */
1393 void
1394 finish_compound_stmt (tree stmt)
1396 if (TREE_CODE (stmt) == BIND_EXPR)
1398 tree body = do_poplevel (BIND_EXPR_BODY (stmt));
1399 /* If the STATEMENT_LIST is empty and this BIND_EXPR isn't special,
1400 discard the BIND_EXPR so it can be merged with the containing
1401 STATEMENT_LIST. */
1402 if (TREE_CODE (body) == STATEMENT_LIST
1403 && STATEMENT_LIST_HEAD (body) == NULL
1404 && !BIND_EXPR_BODY_BLOCK (stmt)
1405 && !BIND_EXPR_TRY_BLOCK (stmt))
1406 stmt = body;
1407 else
1408 BIND_EXPR_BODY (stmt) = body;
1410 else if (STATEMENT_LIST_NO_SCOPE (stmt))
1411 stmt = pop_stmt_list (stmt);
1412 else
1414 /* Destroy any ObjC "super" receivers that may have been
1415 created. */
1416 objc_clear_super_receiver ();
1418 stmt = do_poplevel (stmt);
1421 /* ??? See c_end_compound_stmt wrt statement expressions. */
1422 add_stmt (stmt);
1425 /* Finish an asm-statement, whose components are a STRING, some
1426 OUTPUT_OPERANDS, some INPUT_OPERANDS, some CLOBBERS and some
1427 LABELS. Also note whether the asm-statement should be
1428 considered volatile. */
1430 tree
1431 finish_asm_stmt (int volatile_p, tree string, tree output_operands,
1432 tree input_operands, tree clobbers, tree labels)
1434 tree r;
1435 tree t;
1436 int ninputs = list_length (input_operands);
1437 int noutputs = list_length (output_operands);
1439 if (!processing_template_decl)
1441 const char *constraint;
1442 const char **oconstraints;
1443 bool allows_mem, allows_reg, is_inout;
1444 tree operand;
1445 int i;
1447 oconstraints = XALLOCAVEC (const char *, noutputs);
1449 string = resolve_asm_operand_names (string, output_operands,
1450 input_operands, labels);
1452 for (i = 0, t = output_operands; t; t = TREE_CHAIN (t), ++i)
1454 operand = TREE_VALUE (t);
1456 /* ??? Really, this should not be here. Users should be using a
1457 proper lvalue, dammit. But there's a long history of using
1458 casts in the output operands. In cases like longlong.h, this
1459 becomes a primitive form of typechecking -- if the cast can be
1460 removed, then the output operand had a type of the proper width;
1461 otherwise we'll get an error. Gross, but ... */
1462 STRIP_NOPS (operand);
1464 operand = mark_lvalue_use (operand);
1466 if (!lvalue_or_else (operand, lv_asm, tf_warning_or_error))
1467 operand = error_mark_node;
1469 if (operand != error_mark_node
1470 && (TREE_READONLY (operand)
1471 || CP_TYPE_CONST_P (TREE_TYPE (operand))
1472 /* Functions are not modifiable, even though they are
1473 lvalues. */
1474 || TREE_CODE (TREE_TYPE (operand)) == FUNCTION_TYPE
1475 || TREE_CODE (TREE_TYPE (operand)) == METHOD_TYPE
1476 /* If it's an aggregate and any field is const, then it is
1477 effectively const. */
1478 || (CLASS_TYPE_P (TREE_TYPE (operand))
1479 && C_TYPE_FIELDS_READONLY (TREE_TYPE (operand)))))
1480 cxx_readonly_error (operand, lv_asm);
1482 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
1483 oconstraints[i] = constraint;
1485 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
1486 &allows_mem, &allows_reg, &is_inout))
1488 /* If the operand is going to end up in memory,
1489 mark it addressable. */
1490 if (!allows_reg && !cxx_mark_addressable (operand))
1491 operand = error_mark_node;
1493 else
1494 operand = error_mark_node;
1496 TREE_VALUE (t) = operand;
1499 for (i = 0, t = input_operands; t; ++i, t = TREE_CHAIN (t))
1501 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
1502 bool constraint_parsed
1503 = parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
1504 oconstraints, &allows_mem, &allows_reg);
1505 /* If the operand is going to end up in memory, don't call
1506 decay_conversion. */
1507 if (constraint_parsed && !allows_reg && allows_mem)
1508 operand = mark_lvalue_use (TREE_VALUE (t));
1509 else
1510 operand = decay_conversion (TREE_VALUE (t), tf_warning_or_error);
1512 /* If the type of the operand hasn't been determined (e.g.,
1513 because it involves an overloaded function), then issue
1514 an error message. There's no context available to
1515 resolve the overloading. */
1516 if (TREE_TYPE (operand) == unknown_type_node)
1518 error ("type of asm operand %qE could not be determined",
1519 TREE_VALUE (t));
1520 operand = error_mark_node;
1523 if (constraint_parsed)
1525 /* If the operand is going to end up in memory,
1526 mark it addressable. */
1527 if (!allows_reg && allows_mem)
1529 /* Strip the nops as we allow this case. FIXME, this really
1530 should be rejected or made deprecated. */
1531 STRIP_NOPS (operand);
1532 if (!cxx_mark_addressable (operand))
1533 operand = error_mark_node;
1535 else if (!allows_reg && !allows_mem)
1537 /* If constraint allows neither register nor memory,
1538 try harder to get a constant. */
1539 tree constop = maybe_constant_value (operand);
1540 if (TREE_CONSTANT (constop))
1541 operand = constop;
1544 else
1545 operand = error_mark_node;
1547 TREE_VALUE (t) = operand;
1551 r = build_stmt (input_location, ASM_EXPR, string,
1552 output_operands, input_operands,
1553 clobbers, labels);
1554 ASM_VOLATILE_P (r) = volatile_p || noutputs == 0;
1555 r = maybe_cleanup_point_expr_void (r);
1556 return add_stmt (r);
1559 /* Finish a label with the indicated NAME. Returns the new label. */
1561 tree
1562 finish_label_stmt (tree name)
1564 tree decl = define_label (input_location, name);
1566 if (decl == error_mark_node)
1567 return error_mark_node;
1569 add_stmt (build_stmt (input_location, LABEL_EXPR, decl));
1571 return decl;
1574 /* Finish a series of declarations for local labels. G++ allows users
1575 to declare "local" labels, i.e., labels with scope. This extension
1576 is useful when writing code involving statement-expressions. */
1578 void
1579 finish_label_decl (tree name)
1581 if (!at_function_scope_p ())
1583 error ("__label__ declarations are only allowed in function scopes");
1584 return;
1587 add_decl_expr (declare_local_label (name));
1590 /* When DECL goes out of scope, make sure that CLEANUP is executed. */
1592 void
1593 finish_decl_cleanup (tree decl, tree cleanup)
1595 push_cleanup (decl, cleanup, false);
1598 /* If the current scope exits with an exception, run CLEANUP. */
1600 void
1601 finish_eh_cleanup (tree cleanup)
1603 push_cleanup (NULL, cleanup, true);
1606 /* The MEM_INITS is a list of mem-initializers, in reverse of the
1607 order they were written by the user. Each node is as for
1608 emit_mem_initializers. */
1610 void
1611 finish_mem_initializers (tree mem_inits)
1613 /* Reorder the MEM_INITS so that they are in the order they appeared
1614 in the source program. */
1615 mem_inits = nreverse (mem_inits);
1617 if (processing_template_decl)
1619 tree mem;
1621 for (mem = mem_inits; mem; mem = TREE_CHAIN (mem))
1623 /* If the TREE_PURPOSE is a TYPE_PACK_EXPANSION, skip the
1624 check for bare parameter packs in the TREE_VALUE, because
1625 any parameter packs in the TREE_VALUE have already been
1626 bound as part of the TREE_PURPOSE. See
1627 make_pack_expansion for more information. */
1628 if (TREE_CODE (TREE_PURPOSE (mem)) != TYPE_PACK_EXPANSION
1629 && check_for_bare_parameter_packs (TREE_VALUE (mem)))
1630 TREE_VALUE (mem) = error_mark_node;
1633 add_stmt (build_min_nt_loc (UNKNOWN_LOCATION,
1634 CTOR_INITIALIZER, mem_inits));
1636 else
1637 emit_mem_initializers (mem_inits);
1640 /* Obfuscate EXPR if it looks like an id-expression or member access so
1641 that the call to finish_decltype in do_auto_deduction will give the
1642 right result. */
1644 tree
1645 force_paren_expr (tree expr)
1647 /* This is only needed for decltype(auto) in C++14. */
1648 if (cxx_dialect < cxx14)
1649 return expr;
1651 /* If we're in unevaluated context, we can't be deducing a
1652 return/initializer type, so we don't need to mess with this. */
1653 if (cp_unevaluated_operand)
1654 return expr;
1656 if (!DECL_P (expr) && TREE_CODE (expr) != COMPONENT_REF
1657 && TREE_CODE (expr) != SCOPE_REF)
1658 return expr;
1660 if (TREE_CODE (expr) == COMPONENT_REF
1661 || TREE_CODE (expr) == SCOPE_REF)
1662 REF_PARENTHESIZED_P (expr) = true;
1663 else if (type_dependent_expression_p (expr))
1664 expr = build1 (PAREN_EXPR, TREE_TYPE (expr), expr);
1665 else if (VAR_P (expr) && DECL_HARD_REGISTER (expr))
1666 /* We can't bind a hard register variable to a reference. */;
1667 else
1669 cp_lvalue_kind kind = lvalue_kind (expr);
1670 if ((kind & ~clk_class) != clk_none)
1672 tree type = unlowered_expr_type (expr);
1673 bool rval = !!(kind & clk_rvalueref);
1674 type = cp_build_reference_type (type, rval);
1675 /* This inhibits warnings in, eg, cxx_mark_addressable
1676 (c++/60955). */
1677 warning_sentinel s (extra_warnings);
1678 expr = build_static_cast (type, expr, tf_error);
1679 if (expr != error_mark_node)
1680 REF_PARENTHESIZED_P (expr) = true;
1684 return expr;
1687 /* If T is an id-expression obfuscated by force_paren_expr, undo the
1688 obfuscation and return the underlying id-expression. Otherwise
1689 return T. */
1691 tree
1692 maybe_undo_parenthesized_ref (tree t)
1694 if (cxx_dialect >= cxx14
1695 && INDIRECT_REF_P (t)
1696 && REF_PARENTHESIZED_P (t))
1698 t = TREE_OPERAND (t, 0);
1699 while (TREE_CODE (t) == NON_LVALUE_EXPR
1700 || TREE_CODE (t) == NOP_EXPR)
1701 t = TREE_OPERAND (t, 0);
1703 gcc_assert (TREE_CODE (t) == ADDR_EXPR
1704 || TREE_CODE (t) == STATIC_CAST_EXPR);
1705 t = TREE_OPERAND (t, 0);
1708 return t;
1711 /* Finish a parenthesized expression EXPR. */
1713 cp_expr
1714 finish_parenthesized_expr (cp_expr expr)
1716 if (EXPR_P (expr))
1717 /* This inhibits warnings in c_common_truthvalue_conversion. */
1718 TREE_NO_WARNING (expr) = 1;
1720 if (TREE_CODE (expr) == OFFSET_REF
1721 || TREE_CODE (expr) == SCOPE_REF)
1722 /* [expr.unary.op]/3 The qualified id of a pointer-to-member must not be
1723 enclosed in parentheses. */
1724 PTRMEM_OK_P (expr) = 0;
1726 if (TREE_CODE (expr) == STRING_CST)
1727 PAREN_STRING_LITERAL_P (expr) = 1;
1729 expr = cp_expr (force_paren_expr (expr), expr.get_location ());
1731 return expr;
1734 /* Finish a reference to a non-static data member (DECL) that is not
1735 preceded by `.' or `->'. */
1737 tree
1738 finish_non_static_data_member (tree decl, tree object, tree qualifying_scope)
1740 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
1741 bool try_omp_private = !object && omp_private_member_map;
1742 tree ret;
1744 if (!object)
1746 tree scope = qualifying_scope;
1747 if (scope == NULL_TREE)
1748 scope = context_for_name_lookup (decl);
1749 object = maybe_dummy_object (scope, NULL);
1752 object = maybe_resolve_dummy (object, true);
1753 if (object == error_mark_node)
1754 return error_mark_node;
1756 /* DR 613/850: Can use non-static data members without an associated
1757 object in sizeof/decltype/alignof. */
1758 if (is_dummy_object (object) && cp_unevaluated_operand == 0
1759 && (!processing_template_decl || !current_class_ref))
1761 if (current_function_decl
1762 && DECL_STATIC_FUNCTION_P (current_function_decl))
1763 error ("invalid use of member %qD in static member function", decl);
1764 else
1765 error ("invalid use of non-static data member %qD", decl);
1766 inform (DECL_SOURCE_LOCATION (decl), "declared here");
1768 return error_mark_node;
1771 if (current_class_ptr)
1772 TREE_USED (current_class_ptr) = 1;
1773 if (processing_template_decl && !qualifying_scope)
1775 tree type = TREE_TYPE (decl);
1777 if (TREE_CODE (type) == REFERENCE_TYPE)
1778 /* Quals on the object don't matter. */;
1779 else if (PACK_EXPANSION_P (type))
1780 /* Don't bother trying to represent this. */
1781 type = NULL_TREE;
1782 else
1784 /* Set the cv qualifiers. */
1785 int quals = cp_type_quals (TREE_TYPE (object));
1787 if (DECL_MUTABLE_P (decl))
1788 quals &= ~TYPE_QUAL_CONST;
1790 quals |= cp_type_quals (TREE_TYPE (decl));
1791 type = cp_build_qualified_type (type, quals);
1794 ret = (convert_from_reference
1795 (build_min (COMPONENT_REF, type, object, decl, NULL_TREE)));
1797 /* If PROCESSING_TEMPLATE_DECL is nonzero here, then
1798 QUALIFYING_SCOPE is also non-null. Wrap this in a SCOPE_REF
1799 for now. */
1800 else if (processing_template_decl)
1801 ret = build_qualified_name (TREE_TYPE (decl),
1802 qualifying_scope,
1803 decl,
1804 /*template_p=*/false);
1805 else
1807 tree access_type = TREE_TYPE (object);
1809 perform_or_defer_access_check (TYPE_BINFO (access_type), decl,
1810 decl, tf_warning_or_error);
1812 /* If the data member was named `C::M', convert `*this' to `C'
1813 first. */
1814 if (qualifying_scope)
1816 tree binfo = NULL_TREE;
1817 object = build_scoped_ref (object, qualifying_scope,
1818 &binfo);
1821 ret = build_class_member_access_expr (object, decl,
1822 /*access_path=*/NULL_TREE,
1823 /*preserve_reference=*/false,
1824 tf_warning_or_error);
1826 if (try_omp_private)
1828 tree *v = omp_private_member_map->get (decl);
1829 if (v)
1830 ret = convert_from_reference (*v);
1832 return ret;
1835 /* If we are currently parsing a template and we encountered a typedef
1836 TYPEDEF_DECL that is being accessed though CONTEXT, this function
1837 adds the typedef to a list tied to the current template.
1838 At template instantiation time, that list is walked and access check
1839 performed for each typedef.
1840 LOCATION is the location of the usage point of TYPEDEF_DECL. */
1842 void
1843 add_typedef_to_current_template_for_access_check (tree typedef_decl,
1844 tree context,
1845 location_t location)
1847 tree template_info = NULL;
1848 tree cs = current_scope ();
1850 if (!is_typedef_decl (typedef_decl)
1851 || !context
1852 || !CLASS_TYPE_P (context)
1853 || !cs)
1854 return;
1856 if (CLASS_TYPE_P (cs) || TREE_CODE (cs) == FUNCTION_DECL)
1857 template_info = get_template_info (cs);
1859 if (template_info
1860 && TI_TEMPLATE (template_info)
1861 && !currently_open_class (context))
1862 append_type_to_template_for_access_check (cs, typedef_decl,
1863 context, location);
1866 /* DECL was the declaration to which a qualified-id resolved. Issue
1867 an error message if it is not accessible. If OBJECT_TYPE is
1868 non-NULL, we have just seen `x->' or `x.' and OBJECT_TYPE is the
1869 type of `*x', or `x', respectively. If the DECL was named as
1870 `A::B' then NESTED_NAME_SPECIFIER is `A'. */
1872 void
1873 check_accessibility_of_qualified_id (tree decl,
1874 tree object_type,
1875 tree nested_name_specifier)
1877 tree scope;
1878 tree qualifying_type = NULL_TREE;
1880 /* If we are parsing a template declaration and if decl is a typedef,
1881 add it to a list tied to the template.
1882 At template instantiation time, that list will be walked and
1883 access check performed. */
1884 add_typedef_to_current_template_for_access_check (decl,
1885 nested_name_specifier
1886 ? nested_name_specifier
1887 : DECL_CONTEXT (decl),
1888 input_location);
1890 /* If we're not checking, return immediately. */
1891 if (deferred_access_no_check)
1892 return;
1894 /* Determine the SCOPE of DECL. */
1895 scope = context_for_name_lookup (decl);
1896 /* If the SCOPE is not a type, then DECL is not a member. */
1897 if (!TYPE_P (scope))
1898 return;
1899 /* Compute the scope through which DECL is being accessed. */
1900 if (object_type
1901 /* OBJECT_TYPE might not be a class type; consider:
1903 class A { typedef int I; };
1904 I *p;
1905 p->A::I::~I();
1907 In this case, we will have "A::I" as the DECL, but "I" as the
1908 OBJECT_TYPE. */
1909 && CLASS_TYPE_P (object_type)
1910 && DERIVED_FROM_P (scope, object_type))
1911 /* If we are processing a `->' or `.' expression, use the type of the
1912 left-hand side. */
1913 qualifying_type = object_type;
1914 else if (nested_name_specifier)
1916 /* If the reference is to a non-static member of the
1917 current class, treat it as if it were referenced through
1918 `this'. */
1919 tree ct;
1920 if (DECL_NONSTATIC_MEMBER_P (decl)
1921 && current_class_ptr
1922 && DERIVED_FROM_P (scope, ct = current_nonlambda_class_type ()))
1923 qualifying_type = ct;
1924 /* Otherwise, use the type indicated by the
1925 nested-name-specifier. */
1926 else
1927 qualifying_type = nested_name_specifier;
1929 else
1930 /* Otherwise, the name must be from the current class or one of
1931 its bases. */
1932 qualifying_type = currently_open_derived_class (scope);
1934 if (qualifying_type
1935 /* It is possible for qualifying type to be a TEMPLATE_TYPE_PARM
1936 or similar in a default argument value. */
1937 && CLASS_TYPE_P (qualifying_type)
1938 && !dependent_type_p (qualifying_type))
1939 perform_or_defer_access_check (TYPE_BINFO (qualifying_type), decl,
1940 decl, tf_warning_or_error);
1943 /* EXPR is the result of a qualified-id. The QUALIFYING_CLASS was the
1944 class named to the left of the "::" operator. DONE is true if this
1945 expression is a complete postfix-expression; it is false if this
1946 expression is followed by '->', '[', '(', etc. ADDRESS_P is true
1947 iff this expression is the operand of '&'. TEMPLATE_P is true iff
1948 the qualified-id was of the form "A::template B". TEMPLATE_ARG_P
1949 is true iff this qualified name appears as a template argument. */
1951 tree
1952 finish_qualified_id_expr (tree qualifying_class,
1953 tree expr,
1954 bool done,
1955 bool address_p,
1956 bool template_p,
1957 bool template_arg_p,
1958 tsubst_flags_t complain)
1960 gcc_assert (TYPE_P (qualifying_class));
1962 if (error_operand_p (expr))
1963 return error_mark_node;
1965 if ((DECL_P (expr) || BASELINK_P (expr))
1966 && !mark_used (expr, complain))
1967 return error_mark_node;
1969 if (template_p)
1971 if (TREE_CODE (expr) == UNBOUND_CLASS_TEMPLATE)
1972 /* cp_parser_lookup_name thought we were looking for a type,
1973 but we're actually looking for a declaration. */
1974 expr = build_qualified_name (/*type*/NULL_TREE,
1975 TYPE_CONTEXT (expr),
1976 TYPE_IDENTIFIER (expr),
1977 /*template_p*/true);
1978 else
1979 check_template_keyword (expr);
1982 /* If EXPR occurs as the operand of '&', use special handling that
1983 permits a pointer-to-member. */
1984 if (address_p && done)
1986 if (TREE_CODE (expr) == SCOPE_REF)
1987 expr = TREE_OPERAND (expr, 1);
1988 expr = build_offset_ref (qualifying_class, expr,
1989 /*address_p=*/true, complain);
1990 return expr;
1993 /* No need to check access within an enum. */
1994 if (TREE_CODE (qualifying_class) == ENUMERAL_TYPE)
1995 return expr;
1997 /* Within the scope of a class, turn references to non-static
1998 members into expression of the form "this->...". */
1999 if (template_arg_p)
2000 /* But, within a template argument, we do not want make the
2001 transformation, as there is no "this" pointer. */
2003 else if (TREE_CODE (expr) == FIELD_DECL)
2005 push_deferring_access_checks (dk_no_check);
2006 expr = finish_non_static_data_member (expr, NULL_TREE,
2007 qualifying_class);
2008 pop_deferring_access_checks ();
2010 else if (BASELINK_P (expr) && !processing_template_decl)
2012 /* See if any of the functions are non-static members. */
2013 /* If so, the expression may be relative to 'this'. */
2014 if (!shared_member_p (expr)
2015 && current_class_ptr
2016 && DERIVED_FROM_P (qualifying_class,
2017 current_nonlambda_class_type ()))
2018 expr = (build_class_member_access_expr
2019 (maybe_dummy_object (qualifying_class, NULL),
2020 expr,
2021 BASELINK_ACCESS_BINFO (expr),
2022 /*preserve_reference=*/false,
2023 complain));
2024 else if (done)
2025 /* The expression is a qualified name whose address is not
2026 being taken. */
2027 expr = build_offset_ref (qualifying_class, expr, /*address_p=*/false,
2028 complain);
2030 else if (BASELINK_P (expr))
2032 else
2034 /* In a template, return a SCOPE_REF for most qualified-ids
2035 so that we can check access at instantiation time. But if
2036 we're looking at a member of the current instantiation, we
2037 know we have access and building up the SCOPE_REF confuses
2038 non-type template argument handling. */
2039 if (processing_template_decl
2040 && !currently_open_class (qualifying_class))
2041 expr = build_qualified_name (TREE_TYPE (expr),
2042 qualifying_class, expr,
2043 template_p);
2045 expr = convert_from_reference (expr);
2048 return expr;
2051 /* Begin a statement-expression. The value returned must be passed to
2052 finish_stmt_expr. */
2054 tree
2055 begin_stmt_expr (void)
2057 return push_stmt_list ();
2060 /* Process the final expression of a statement expression. EXPR can be
2061 NULL, if the final expression is empty. Return a STATEMENT_LIST
2062 containing all the statements in the statement-expression, or
2063 ERROR_MARK_NODE if there was an error. */
2065 tree
2066 finish_stmt_expr_expr (tree expr, tree stmt_expr)
2068 if (error_operand_p (expr))
2070 /* The type of the statement-expression is the type of the last
2071 expression. */
2072 TREE_TYPE (stmt_expr) = error_mark_node;
2073 return error_mark_node;
2076 /* If the last statement does not have "void" type, then the value
2077 of the last statement is the value of the entire expression. */
2078 if (expr)
2080 tree type = TREE_TYPE (expr);
2082 if (processing_template_decl)
2084 expr = build_stmt (input_location, EXPR_STMT, expr);
2085 expr = add_stmt (expr);
2086 /* Mark the last statement so that we can recognize it as such at
2087 template-instantiation time. */
2088 EXPR_STMT_STMT_EXPR_RESULT (expr) = 1;
2090 else if (VOID_TYPE_P (type))
2092 /* Just treat this like an ordinary statement. */
2093 expr = finish_expr_stmt (expr);
2095 else
2097 /* It actually has a value we need to deal with. First, force it
2098 to be an rvalue so that we won't need to build up a copy
2099 constructor call later when we try to assign it to something. */
2100 expr = force_rvalue (expr, tf_warning_or_error);
2101 if (error_operand_p (expr))
2102 return error_mark_node;
2104 /* Update for array-to-pointer decay. */
2105 type = TREE_TYPE (expr);
2107 /* Wrap it in a CLEANUP_POINT_EXPR and add it to the list like a
2108 normal statement, but don't convert to void or actually add
2109 the EXPR_STMT. */
2110 if (TREE_CODE (expr) != CLEANUP_POINT_EXPR)
2111 expr = maybe_cleanup_point_expr (expr);
2112 add_stmt (expr);
2115 /* The type of the statement-expression is the type of the last
2116 expression. */
2117 TREE_TYPE (stmt_expr) = type;
2120 return stmt_expr;
2123 /* Finish a statement-expression. EXPR should be the value returned
2124 by the previous begin_stmt_expr. Returns an expression
2125 representing the statement-expression. */
2127 tree
2128 finish_stmt_expr (tree stmt_expr, bool has_no_scope)
2130 tree type;
2131 tree result;
2133 if (error_operand_p (stmt_expr))
2135 pop_stmt_list (stmt_expr);
2136 return error_mark_node;
2139 gcc_assert (TREE_CODE (stmt_expr) == STATEMENT_LIST);
2141 type = TREE_TYPE (stmt_expr);
2142 result = pop_stmt_list (stmt_expr);
2143 TREE_TYPE (result) = type;
2145 if (processing_template_decl)
2147 result = build_min (STMT_EXPR, type, result);
2148 TREE_SIDE_EFFECTS (result) = 1;
2149 STMT_EXPR_NO_SCOPE (result) = has_no_scope;
2151 else if (CLASS_TYPE_P (type))
2153 /* Wrap the statement-expression in a TARGET_EXPR so that the
2154 temporary object created by the final expression is destroyed at
2155 the end of the full-expression containing the
2156 statement-expression. */
2157 result = force_target_expr (type, result, tf_warning_or_error);
2160 return result;
2163 /* Returns the expression which provides the value of STMT_EXPR. */
2165 tree
2166 stmt_expr_value_expr (tree stmt_expr)
2168 tree t = STMT_EXPR_STMT (stmt_expr);
2170 if (TREE_CODE (t) == BIND_EXPR)
2171 t = BIND_EXPR_BODY (t);
2173 if (TREE_CODE (t) == STATEMENT_LIST && STATEMENT_LIST_TAIL (t))
2174 t = STATEMENT_LIST_TAIL (t)->stmt;
2176 if (TREE_CODE (t) == EXPR_STMT)
2177 t = EXPR_STMT_EXPR (t);
2179 return t;
2182 /* Return TRUE iff EXPR_STMT is an empty list of
2183 expression statements. */
2185 bool
2186 empty_expr_stmt_p (tree expr_stmt)
2188 tree body = NULL_TREE;
2190 if (expr_stmt == void_node)
2191 return true;
2193 if (expr_stmt)
2195 if (TREE_CODE (expr_stmt) == EXPR_STMT)
2196 body = EXPR_STMT_EXPR (expr_stmt);
2197 else if (TREE_CODE (expr_stmt) == STATEMENT_LIST)
2198 body = expr_stmt;
2201 if (body)
2203 if (TREE_CODE (body) == STATEMENT_LIST)
2204 return tsi_end_p (tsi_start (body));
2205 else
2206 return empty_expr_stmt_p (body);
2208 return false;
2211 /* Perform Koenig lookup. FN is the postfix-expression representing
2212 the function (or functions) to call; ARGS are the arguments to the
2213 call. Returns the functions to be considered by overload resolution. */
2215 cp_expr
2216 perform_koenig_lookup (cp_expr fn, vec<tree, va_gc> *args,
2217 tsubst_flags_t complain)
2219 tree identifier = NULL_TREE;
2220 tree functions = NULL_TREE;
2221 tree tmpl_args = NULL_TREE;
2222 bool template_id = false;
2223 location_t loc = fn.get_location ();
2225 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
2227 /* Use a separate flag to handle null args. */
2228 template_id = true;
2229 tmpl_args = TREE_OPERAND (fn, 1);
2230 fn = TREE_OPERAND (fn, 0);
2233 /* Find the name of the overloaded function. */
2234 if (identifier_p (fn))
2235 identifier = fn;
2236 else
2238 functions = fn;
2239 identifier = OVL_NAME (functions);
2242 /* A call to a namespace-scope function using an unqualified name.
2244 Do Koenig lookup -- unless any of the arguments are
2245 type-dependent. */
2246 if (!any_type_dependent_arguments_p (args)
2247 && !any_dependent_template_arguments_p (tmpl_args))
2249 fn = lookup_arg_dependent (identifier, functions, args);
2250 if (!fn)
2252 /* The unqualified name could not be resolved. */
2253 if (complain & tf_error)
2254 fn = unqualified_fn_lookup_error (cp_expr (identifier, loc));
2255 else
2256 fn = identifier;
2260 if (fn && template_id && fn != error_mark_node)
2261 fn = build2 (TEMPLATE_ID_EXPR, unknown_type_node, fn, tmpl_args);
2263 return fn;
2266 /* Generate an expression for `FN (ARGS)'. This may change the
2267 contents of ARGS.
2269 If DISALLOW_VIRTUAL is true, the call to FN will be not generated
2270 as a virtual call, even if FN is virtual. (This flag is set when
2271 encountering an expression where the function name is explicitly
2272 qualified. For example a call to `X::f' never generates a virtual
2273 call.)
2275 Returns code for the call. */
2277 tree
2278 finish_call_expr (tree fn, vec<tree, va_gc> **args, bool disallow_virtual,
2279 bool koenig_p, tsubst_flags_t complain)
2281 tree result;
2282 tree orig_fn;
2283 vec<tree, va_gc> *orig_args = NULL;
2285 if (fn == error_mark_node)
2286 return error_mark_node;
2288 gcc_assert (!TYPE_P (fn));
2290 /* If FN may be a FUNCTION_DECL obfuscated by force_paren_expr, undo
2291 it so that we can tell this is a call to a known function. */
2292 fn = maybe_undo_parenthesized_ref (fn);
2294 orig_fn = fn;
2296 if (processing_template_decl)
2298 /* If the call expression is dependent, build a CALL_EXPR node
2299 with no type; type_dependent_expression_p recognizes
2300 expressions with no type as being dependent. */
2301 if (type_dependent_expression_p (fn)
2302 || any_type_dependent_arguments_p (*args))
2304 result = build_nt_call_vec (fn, *args);
2305 SET_EXPR_LOCATION (result, EXPR_LOC_OR_LOC (fn, input_location));
2306 KOENIG_LOOKUP_P (result) = koenig_p;
2307 if (is_overloaded_fn (fn))
2309 fn = get_fns (fn);
2310 lookup_keep (fn, true);
2313 if (cfun)
2315 bool abnormal = true;
2316 for (lkp_iterator iter (fn); abnormal && iter; ++iter)
2318 tree fndecl = *iter;
2319 if (TREE_CODE (fndecl) != FUNCTION_DECL
2320 || !TREE_THIS_VOLATILE (fndecl))
2321 abnormal = false;
2323 /* FIXME: Stop warning about falling off end of non-void
2324 function. But this is wrong. Even if we only see
2325 no-return fns at this point, we could select a
2326 future-defined return fn during instantiation. Or
2327 vice-versa. */
2328 if (abnormal)
2329 current_function_returns_abnormally = 1;
2331 return result;
2333 orig_args = make_tree_vector_copy (*args);
2334 if (!BASELINK_P (fn)
2335 && TREE_CODE (fn) != PSEUDO_DTOR_EXPR
2336 && TREE_TYPE (fn) != unknown_type_node)
2337 fn = build_non_dependent_expr (fn);
2338 make_args_non_dependent (*args);
2341 if (TREE_CODE (fn) == COMPONENT_REF)
2343 tree member = TREE_OPERAND (fn, 1);
2344 if (BASELINK_P (member))
2346 tree object = TREE_OPERAND (fn, 0);
2347 return build_new_method_call (object, member,
2348 args, NULL_TREE,
2349 (disallow_virtual
2350 ? LOOKUP_NORMAL | LOOKUP_NONVIRTUAL
2351 : LOOKUP_NORMAL),
2352 /*fn_p=*/NULL,
2353 complain);
2357 /* Per 13.3.1.1, '(&f)(...)' is the same as '(f)(...)'. */
2358 if (TREE_CODE (fn) == ADDR_EXPR
2359 && TREE_CODE (TREE_OPERAND (fn, 0)) == OVERLOAD)
2360 fn = TREE_OPERAND (fn, 0);
2362 if (is_overloaded_fn (fn))
2363 fn = baselink_for_fns (fn);
2365 result = NULL_TREE;
2366 if (BASELINK_P (fn))
2368 tree object;
2370 /* A call to a member function. From [over.call.func]:
2372 If the keyword this is in scope and refers to the class of
2373 that member function, or a derived class thereof, then the
2374 function call is transformed into a qualified function call
2375 using (*this) as the postfix-expression to the left of the
2376 . operator.... [Otherwise] a contrived object of type T
2377 becomes the implied object argument.
2379 In this situation:
2381 struct A { void f(); };
2382 struct B : public A {};
2383 struct C : public A { void g() { B::f(); }};
2385 "the class of that member function" refers to `A'. But 11.2
2386 [class.access.base] says that we need to convert 'this' to B* as
2387 part of the access, so we pass 'B' to maybe_dummy_object. */
2389 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (get_first_fn (fn)))
2391 /* A constructor call always uses a dummy object. (This constructor
2392 call which has the form A::A () is actually invalid and we are
2393 going to reject it later in build_new_method_call.) */
2394 object = build_dummy_object (BINFO_TYPE (BASELINK_ACCESS_BINFO (fn)));
2396 else
2397 object = maybe_dummy_object (BINFO_TYPE (BASELINK_ACCESS_BINFO (fn)),
2398 NULL);
2400 result = build_new_method_call (object, fn, args, NULL_TREE,
2401 (disallow_virtual
2402 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
2403 : LOOKUP_NORMAL),
2404 /*fn_p=*/NULL,
2405 complain);
2407 else if (is_overloaded_fn (fn))
2409 /* If the function is an overloaded builtin, resolve it. */
2410 if (TREE_CODE (fn) == FUNCTION_DECL
2411 && (DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL
2412 || DECL_BUILT_IN_CLASS (fn) == BUILT_IN_MD))
2413 result = resolve_overloaded_builtin (input_location, fn, *args);
2415 if (!result)
2417 if (warn_sizeof_pointer_memaccess
2418 && (complain & tf_warning)
2419 && !vec_safe_is_empty (*args)
2420 && !processing_template_decl)
2422 location_t sizeof_arg_loc[3];
2423 tree sizeof_arg[3];
2424 unsigned int i;
2425 for (i = 0; i < 3; i++)
2427 tree t;
2429 sizeof_arg_loc[i] = UNKNOWN_LOCATION;
2430 sizeof_arg[i] = NULL_TREE;
2431 if (i >= (*args)->length ())
2432 continue;
2433 t = (**args)[i];
2434 if (TREE_CODE (t) != SIZEOF_EXPR)
2435 continue;
2436 if (SIZEOF_EXPR_TYPE_P (t))
2437 sizeof_arg[i] = TREE_TYPE (TREE_OPERAND (t, 0));
2438 else
2439 sizeof_arg[i] = TREE_OPERAND (t, 0);
2440 sizeof_arg_loc[i] = EXPR_LOCATION (t);
2442 sizeof_pointer_memaccess_warning
2443 (sizeof_arg_loc, fn, *args,
2444 sizeof_arg, same_type_ignoring_top_level_qualifiers_p);
2447 /* A call to a namespace-scope function. */
2448 result = build_new_function_call (fn, args, complain);
2451 else if (TREE_CODE (fn) == PSEUDO_DTOR_EXPR)
2453 if (!vec_safe_is_empty (*args))
2454 error ("arguments to destructor are not allowed");
2455 /* Mark the pseudo-destructor call as having side-effects so
2456 that we do not issue warnings about its use. */
2457 result = build1 (NOP_EXPR,
2458 void_type_node,
2459 TREE_OPERAND (fn, 0));
2460 TREE_SIDE_EFFECTS (result) = 1;
2462 else if (CLASS_TYPE_P (TREE_TYPE (fn)))
2463 /* If the "function" is really an object of class type, it might
2464 have an overloaded `operator ()'. */
2465 result = build_op_call (fn, args, complain);
2467 if (!result)
2468 /* A call where the function is unknown. */
2469 result = cp_build_function_call_vec (fn, args, complain);
2471 if (processing_template_decl && result != error_mark_node)
2473 if (INDIRECT_REF_P (result))
2474 result = TREE_OPERAND (result, 0);
2475 result = build_call_vec (TREE_TYPE (result), orig_fn, orig_args);
2476 SET_EXPR_LOCATION (result, input_location);
2477 KOENIG_LOOKUP_P (result) = koenig_p;
2478 release_tree_vector (orig_args);
2479 result = convert_from_reference (result);
2482 /* Free or retain OVERLOADs from lookup. */
2483 if (is_overloaded_fn (orig_fn))
2484 lookup_keep (get_fns (orig_fn), processing_template_decl);
2486 return result;
2489 /* Finish a call to a postfix increment or decrement or EXPR. (Which
2490 is indicated by CODE, which should be POSTINCREMENT_EXPR or
2491 POSTDECREMENT_EXPR.) */
2493 cp_expr
2494 finish_increment_expr (cp_expr expr, enum tree_code code)
2496 /* input_location holds the location of the trailing operator token.
2497 Build a location of the form:
2498 expr++
2499 ~~~~^~
2500 with the caret at the operator token, ranging from the start
2501 of EXPR to the end of the operator token. */
2502 location_t combined_loc = make_location (input_location,
2503 expr.get_start (),
2504 get_finish (input_location));
2505 cp_expr result = build_x_unary_op (combined_loc, code, expr,
2506 tf_warning_or_error);
2507 /* TODO: build_x_unary_op doesn't honor the location, so set it here. */
2508 result.set_location (combined_loc);
2509 return result;
2512 /* Finish a use of `this'. Returns an expression for `this'. */
2514 tree
2515 finish_this_expr (void)
2517 tree result = NULL_TREE;
2519 if (current_class_ptr)
2521 tree type = TREE_TYPE (current_class_ref);
2523 /* In a lambda expression, 'this' refers to the captured 'this'. */
2524 if (LAMBDA_TYPE_P (type))
2525 result = lambda_expr_this_capture (CLASSTYPE_LAMBDA_EXPR (type), true);
2526 else
2527 result = current_class_ptr;
2530 if (result)
2531 /* The keyword 'this' is a prvalue expression. */
2532 return rvalue (result);
2534 tree fn = current_nonlambda_function ();
2535 if (fn && DECL_STATIC_FUNCTION_P (fn))
2536 error ("%<this%> is unavailable for static member functions");
2537 else if (fn)
2538 error ("invalid use of %<this%> in non-member function");
2539 else
2540 error ("invalid use of %<this%> at top level");
2541 return error_mark_node;
2544 /* Finish a pseudo-destructor expression. If SCOPE is NULL, the
2545 expression was of the form `OBJECT.~DESTRUCTOR' where DESTRUCTOR is
2546 the TYPE for the type given. If SCOPE is non-NULL, the expression
2547 was of the form `OBJECT.SCOPE::~DESTRUCTOR'. */
2549 tree
2550 finish_pseudo_destructor_expr (tree object, tree scope, tree destructor,
2551 location_t loc)
2553 if (object == error_mark_node || destructor == error_mark_node)
2554 return error_mark_node;
2556 gcc_assert (TYPE_P (destructor));
2558 if (!processing_template_decl)
2560 if (scope == error_mark_node)
2562 error_at (loc, "invalid qualifying scope in pseudo-destructor name");
2563 return error_mark_node;
2565 if (is_auto (destructor))
2566 destructor = TREE_TYPE (object);
2567 if (scope && TYPE_P (scope) && !check_dtor_name (scope, destructor))
2569 error_at (loc,
2570 "qualified type %qT does not match destructor name ~%qT",
2571 scope, destructor);
2572 return error_mark_node;
2576 /* [expr.pseudo] says both:
2578 The type designated by the pseudo-destructor-name shall be
2579 the same as the object type.
2581 and:
2583 The cv-unqualified versions of the object type and of the
2584 type designated by the pseudo-destructor-name shall be the
2585 same type.
2587 We implement the more generous second sentence, since that is
2588 what most other compilers do. */
2589 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (object),
2590 destructor))
2592 error_at (loc, "%qE is not of type %qT", object, destructor);
2593 return error_mark_node;
2597 return build3_loc (loc, PSEUDO_DTOR_EXPR, void_type_node, object,
2598 scope, destructor);
2601 /* Finish an expression of the form CODE EXPR. */
2603 cp_expr
2604 finish_unary_op_expr (location_t op_loc, enum tree_code code, cp_expr expr,
2605 tsubst_flags_t complain)
2607 /* Build a location of the form:
2608 ++expr
2609 ^~~~~~
2610 with the caret at the operator token, ranging from the start
2611 of the operator token to the end of EXPR. */
2612 location_t combined_loc = make_location (op_loc,
2613 op_loc, expr.get_finish ());
2614 cp_expr result = build_x_unary_op (combined_loc, code, expr, complain);
2615 /* TODO: build_x_unary_op doesn't always honor the location. */
2616 result.set_location (combined_loc);
2618 tree result_ovl, expr_ovl;
2620 if (!(complain & tf_warning))
2621 return result;
2623 result_ovl = result;
2624 expr_ovl = expr;
2626 if (!processing_template_decl)
2627 expr_ovl = cp_fully_fold (expr_ovl);
2629 if (!CONSTANT_CLASS_P (expr_ovl)
2630 || TREE_OVERFLOW_P (expr_ovl))
2631 return result;
2633 if (!processing_template_decl)
2634 result_ovl = cp_fully_fold (result_ovl);
2636 if (CONSTANT_CLASS_P (result_ovl) && TREE_OVERFLOW_P (result_ovl))
2637 overflow_warning (combined_loc, result_ovl);
2639 return result;
2642 /* Finish a compound-literal expression or C++11 functional cast with aggregate
2643 initializer. TYPE is the type to which the CONSTRUCTOR in COMPOUND_LITERAL
2644 is being cast. */
2646 tree
2647 finish_compound_literal (tree type, tree compound_literal,
2648 tsubst_flags_t complain,
2649 fcl_t fcl_context)
2651 if (type == error_mark_node)
2652 return error_mark_node;
2654 if (TREE_CODE (type) == REFERENCE_TYPE)
2656 compound_literal
2657 = finish_compound_literal (TREE_TYPE (type), compound_literal,
2658 complain, fcl_context);
2659 return cp_build_c_cast (type, compound_literal, complain);
2662 if (!TYPE_OBJ_P (type))
2664 if (complain & tf_error)
2665 error ("compound literal of non-object type %qT", type);
2666 return error_mark_node;
2669 if (tree anode = type_uses_auto (type))
2670 if (CLASS_PLACEHOLDER_TEMPLATE (anode))
2671 type = do_auto_deduction (type, compound_literal, anode, complain,
2672 adc_variable_type);
2674 if (processing_template_decl)
2676 TREE_TYPE (compound_literal) = type;
2677 /* Mark the expression as a compound literal. */
2678 TREE_HAS_CONSTRUCTOR (compound_literal) = 1;
2679 if (fcl_context == fcl_c99)
2680 CONSTRUCTOR_C99_COMPOUND_LITERAL (compound_literal) = 1;
2681 return compound_literal;
2684 type = complete_type (type);
2686 if (TYPE_NON_AGGREGATE_CLASS (type))
2688 /* Trying to deal with a CONSTRUCTOR instead of a TREE_LIST
2689 everywhere that deals with function arguments would be a pain, so
2690 just wrap it in a TREE_LIST. The parser set a flag so we know
2691 that it came from T{} rather than T({}). */
2692 CONSTRUCTOR_IS_DIRECT_INIT (compound_literal) = 1;
2693 compound_literal = build_tree_list (NULL_TREE, compound_literal);
2694 return build_functional_cast (type, compound_literal, complain);
2697 if (TREE_CODE (type) == ARRAY_TYPE
2698 && check_array_initializer (NULL_TREE, type, compound_literal))
2699 return error_mark_node;
2700 compound_literal = reshape_init (type, compound_literal, complain);
2701 if (SCALAR_TYPE_P (type)
2702 && !BRACE_ENCLOSED_INITIALIZER_P (compound_literal)
2703 && !check_narrowing (type, compound_literal, complain))
2704 return error_mark_node;
2705 if (TREE_CODE (type) == ARRAY_TYPE
2706 && TYPE_DOMAIN (type) == NULL_TREE)
2708 cp_complete_array_type_or_error (&type, compound_literal,
2709 false, complain);
2710 if (type == error_mark_node)
2711 return error_mark_node;
2713 compound_literal = digest_init_flags (type, compound_literal, LOOKUP_NORMAL,
2714 complain);
2715 if (TREE_CODE (compound_literal) == CONSTRUCTOR)
2717 TREE_HAS_CONSTRUCTOR (compound_literal) = true;
2718 if (fcl_context == fcl_c99)
2719 CONSTRUCTOR_C99_COMPOUND_LITERAL (compound_literal) = 1;
2722 /* Put static/constant array temporaries in static variables. */
2723 /* FIXME all C99 compound literals should be variables rather than C++
2724 temporaries, unless they are used as an aggregate initializer. */
2725 if ((!at_function_scope_p () || CP_TYPE_CONST_P (type))
2726 && fcl_context == fcl_c99
2727 && TREE_CODE (type) == ARRAY_TYPE
2728 && !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
2729 && initializer_constant_valid_p (compound_literal, type))
2731 tree decl = create_temporary_var (type);
2732 DECL_INITIAL (decl) = compound_literal;
2733 TREE_STATIC (decl) = 1;
2734 if (literal_type_p (type) && CP_TYPE_CONST_NON_VOLATILE_P (type))
2736 /* 5.19 says that a constant expression can include an
2737 lvalue-rvalue conversion applied to "a glvalue of literal type
2738 that refers to a non-volatile temporary object initialized
2739 with a constant expression". Rather than try to communicate
2740 that this VAR_DECL is a temporary, just mark it constexpr. */
2741 DECL_DECLARED_CONSTEXPR_P (decl) = true;
2742 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
2743 TREE_CONSTANT (decl) = true;
2745 cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
2746 decl = pushdecl_top_level (decl);
2747 DECL_NAME (decl) = make_anon_name ();
2748 SET_DECL_ASSEMBLER_NAME (decl, DECL_NAME (decl));
2749 /* Make sure the destructor is callable. */
2750 tree clean = cxx_maybe_build_cleanup (decl, complain);
2751 if (clean == error_mark_node)
2752 return error_mark_node;
2753 return decl;
2756 /* Represent other compound literals with TARGET_EXPR so we produce
2757 an lvalue, but can elide copies. */
2758 if (!VECTOR_TYPE_P (type))
2759 compound_literal = get_target_expr_sfinae (compound_literal, complain);
2761 return compound_literal;
2764 /* Return the declaration for the function-name variable indicated by
2765 ID. */
2767 tree
2768 finish_fname (tree id)
2770 tree decl;
2772 decl = fname_decl (input_location, C_RID_CODE (id), id);
2773 if (processing_template_decl && current_function_decl
2774 && decl != error_mark_node)
2775 decl = DECL_NAME (decl);
2776 return decl;
2779 /* Finish a translation unit. */
2781 void
2782 finish_translation_unit (void)
2784 /* In case there were missing closebraces,
2785 get us back to the global binding level. */
2786 pop_everything ();
2787 while (current_namespace != global_namespace)
2788 pop_namespace ();
2790 /* Do file scope __FUNCTION__ et al. */
2791 finish_fname_decls ();
2794 /* Finish a template type parameter, specified as AGGR IDENTIFIER.
2795 Returns the parameter. */
2797 tree
2798 finish_template_type_parm (tree aggr, tree identifier)
2800 if (aggr != class_type_node)
2802 permerror (input_location, "template type parameters must use the keyword %<class%> or %<typename%>");
2803 aggr = class_type_node;
2806 return build_tree_list (aggr, identifier);
2809 /* Finish a template template parameter, specified as AGGR IDENTIFIER.
2810 Returns the parameter. */
2812 tree
2813 finish_template_template_parm (tree aggr, tree identifier)
2815 tree decl = build_decl (input_location,
2816 TYPE_DECL, identifier, NULL_TREE);
2818 tree tmpl = build_lang_decl (TEMPLATE_DECL, identifier, NULL_TREE);
2819 DECL_TEMPLATE_PARMS (tmpl) = current_template_parms;
2820 DECL_TEMPLATE_RESULT (tmpl) = decl;
2821 DECL_ARTIFICIAL (decl) = 1;
2823 // Associate the constraints with the underlying declaration,
2824 // not the template.
2825 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
2826 tree constr = build_constraints (reqs, NULL_TREE);
2827 set_constraints (decl, constr);
2829 end_template_decl ();
2831 gcc_assert (DECL_TEMPLATE_PARMS (tmpl));
2833 check_default_tmpl_args (decl, DECL_TEMPLATE_PARMS (tmpl),
2834 /*is_primary=*/true, /*is_partial=*/false,
2835 /*is_friend=*/0);
2837 return finish_template_type_parm (aggr, tmpl);
2840 /* ARGUMENT is the default-argument value for a template template
2841 parameter. If ARGUMENT is invalid, issue error messages and return
2842 the ERROR_MARK_NODE. Otherwise, ARGUMENT itself is returned. */
2844 tree
2845 check_template_template_default_arg (tree argument)
2847 if (TREE_CODE (argument) != TEMPLATE_DECL
2848 && TREE_CODE (argument) != TEMPLATE_TEMPLATE_PARM
2849 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
2851 if (TREE_CODE (argument) == TYPE_DECL)
2852 error ("invalid use of type %qT as a default value for a template "
2853 "template-parameter", TREE_TYPE (argument));
2854 else
2855 error ("invalid default argument for a template template parameter");
2856 return error_mark_node;
2859 return argument;
2862 /* Begin a class definition, as indicated by T. */
2864 tree
2865 begin_class_definition (tree t)
2867 if (error_operand_p (t) || error_operand_p (TYPE_MAIN_DECL (t)))
2868 return error_mark_node;
2870 if (processing_template_parmlist)
2872 error ("definition of %q#T inside template parameter list", t);
2873 return error_mark_node;
2876 /* According to the C++ ABI, decimal classes defined in ISO/IEC TR 24733
2877 are passed the same as decimal scalar types. */
2878 if (TREE_CODE (t) == RECORD_TYPE
2879 && !processing_template_decl)
2881 tree ns = TYPE_CONTEXT (t);
2882 if (ns && TREE_CODE (ns) == NAMESPACE_DECL
2883 && DECL_CONTEXT (ns) == std_node
2884 && DECL_NAME (ns)
2885 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (ns)), "decimal"))
2887 const char *n = TYPE_NAME_STRING (t);
2888 if ((strcmp (n, "decimal32") == 0)
2889 || (strcmp (n, "decimal64") == 0)
2890 || (strcmp (n, "decimal128") == 0))
2891 TYPE_TRANSPARENT_AGGR (t) = 1;
2895 /* A non-implicit typename comes from code like:
2897 template <typename T> struct A {
2898 template <typename U> struct A<T>::B ...
2900 This is erroneous. */
2901 else if (TREE_CODE (t) == TYPENAME_TYPE)
2903 error ("invalid definition of qualified type %qT", t);
2904 t = error_mark_node;
2907 if (t == error_mark_node || ! MAYBE_CLASS_TYPE_P (t))
2909 t = make_class_type (RECORD_TYPE);
2910 pushtag (make_anon_name (), t, /*tag_scope=*/ts_current);
2913 if (TYPE_BEING_DEFINED (t))
2915 t = make_class_type (TREE_CODE (t));
2916 pushtag (TYPE_IDENTIFIER (t), t, /*tag_scope=*/ts_current);
2918 maybe_process_partial_specialization (t);
2919 pushclass (t);
2920 TYPE_BEING_DEFINED (t) = 1;
2921 class_binding_level->defining_class_p = 1;
2923 if (flag_pack_struct)
2925 tree v;
2926 TYPE_PACKED (t) = 1;
2927 /* Even though the type is being defined for the first time
2928 here, there might have been a forward declaration, so there
2929 might be cv-qualified variants of T. */
2930 for (v = TYPE_NEXT_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v))
2931 TYPE_PACKED (v) = 1;
2933 /* Reset the interface data, at the earliest possible
2934 moment, as it might have been set via a class foo;
2935 before. */
2936 if (! TYPE_UNNAMED_P (t))
2938 struct c_fileinfo *finfo = \
2939 get_fileinfo (LOCATION_FILE (input_location));
2940 CLASSTYPE_INTERFACE_ONLY (t) = finfo->interface_only;
2941 SET_CLASSTYPE_INTERFACE_UNKNOWN_X
2942 (t, finfo->interface_unknown);
2944 reset_specialization();
2946 /* Make a declaration for this class in its own scope. */
2947 build_self_reference ();
2949 return t;
2952 /* Finish the member declaration given by DECL. */
2954 void
2955 finish_member_declaration (tree decl)
2957 if (decl == error_mark_node || decl == NULL_TREE)
2958 return;
2960 if (decl == void_type_node)
2961 /* The COMPONENT was a friend, not a member, and so there's
2962 nothing for us to do. */
2963 return;
2965 /* We should see only one DECL at a time. */
2966 gcc_assert (DECL_CHAIN (decl) == NULL_TREE);
2968 /* Don't add decls after definition. */
2969 gcc_assert (TYPE_BEING_DEFINED (current_class_type)
2970 /* We can add lambda types when late parsing default
2971 arguments. */
2972 || LAMBDA_TYPE_P (TREE_TYPE (decl)));
2974 /* Set up access control for DECL. */
2975 TREE_PRIVATE (decl)
2976 = (current_access_specifier == access_private_node);
2977 TREE_PROTECTED (decl)
2978 = (current_access_specifier == access_protected_node);
2979 if (TREE_CODE (decl) == TEMPLATE_DECL)
2981 TREE_PRIVATE (DECL_TEMPLATE_RESULT (decl)) = TREE_PRIVATE (decl);
2982 TREE_PROTECTED (DECL_TEMPLATE_RESULT (decl)) = TREE_PROTECTED (decl);
2985 /* Mark the DECL as a member of the current class, unless it's
2986 a member of an enumeration. */
2987 if (TREE_CODE (decl) != CONST_DECL)
2988 DECL_CONTEXT (decl) = current_class_type;
2990 /* Check for bare parameter packs in the member variable declaration. */
2991 if (TREE_CODE (decl) == FIELD_DECL)
2993 if (check_for_bare_parameter_packs (TREE_TYPE (decl)))
2994 TREE_TYPE (decl) = error_mark_node;
2995 if (check_for_bare_parameter_packs (DECL_ATTRIBUTES (decl)))
2996 DECL_ATTRIBUTES (decl) = NULL_TREE;
2999 /* [dcl.link]
3001 A C language linkage is ignored for the names of class members
3002 and the member function type of class member functions. */
3003 if (DECL_LANG_SPECIFIC (decl) && DECL_LANGUAGE (decl) == lang_c)
3004 SET_DECL_LANGUAGE (decl, lang_cplusplus);
3006 /* Put functions on the TYPE_METHODS list and everything else on the
3007 TYPE_FIELDS list. Note that these are built up in reverse order.
3008 We reverse them (to obtain declaration order) in finish_struct. */
3009 if (DECL_DECLARES_FUNCTION_P (decl))
3011 /* We also need to add this function to the
3012 CLASSTYPE_METHOD_VEC. */
3013 if (add_method (current_class_type, decl, false))
3015 gcc_assert (TYPE_MAIN_VARIANT (current_class_type) == current_class_type);
3016 DECL_CHAIN (decl) = TYPE_METHODS (current_class_type);
3017 TYPE_METHODS (current_class_type) = decl;
3019 maybe_add_class_template_decl_list (current_class_type, decl,
3020 /*friend_p=*/0);
3023 /* Enter the DECL into the scope of the class, if the class
3024 isn't a closure (whose fields are supposed to be unnamed). */
3025 else if (CLASSTYPE_LAMBDA_EXPR (current_class_type)
3026 || pushdecl_class_level (decl))
3028 if (TREE_CODE (decl) == USING_DECL)
3030 /* For now, ignore class-scope USING_DECLS, so that
3031 debugging backends do not see them. */
3032 DECL_IGNORED_P (decl) = 1;
3035 /* All TYPE_DECLs go at the end of TYPE_FIELDS. Ordinary fields
3036 go at the beginning. The reason is that lookup_field_1
3037 searches the list in order, and we want a field name to
3038 override a type name so that the "struct stat hack" will
3039 work. In particular:
3041 struct S { enum E { }; int E } s;
3042 s.E = 3;
3044 is valid. In addition, the FIELD_DECLs must be maintained in
3045 declaration order so that class layout works as expected.
3046 However, we don't need that order until class layout, so we
3047 save a little time by putting FIELD_DECLs on in reverse order
3048 here, and then reversing them in finish_struct_1. (We could
3049 also keep a pointer to the correct insertion points in the
3050 list.) */
3052 if (TREE_CODE (decl) == TYPE_DECL)
3053 TYPE_FIELDS (current_class_type)
3054 = chainon (TYPE_FIELDS (current_class_type), decl);
3055 else
3057 DECL_CHAIN (decl) = TYPE_FIELDS (current_class_type);
3058 TYPE_FIELDS (current_class_type) = decl;
3061 maybe_add_class_template_decl_list (current_class_type, decl,
3062 /*friend_p=*/0);
3066 /* Finish processing a complete template declaration. The PARMS are
3067 the template parameters. */
3069 void
3070 finish_template_decl (tree parms)
3072 if (parms)
3073 end_template_decl ();
3074 else
3075 end_specialization ();
3078 // Returns the template type of the class scope being entered. If we're
3079 // entering a constrained class scope. TYPE is the class template
3080 // scope being entered and we may need to match the intended type with
3081 // a constrained specialization. For example:
3083 // template<Object T>
3084 // struct S { void f(); }; #1
3086 // template<Object T>
3087 // void S<T>::f() { } #2
3089 // We check, in #2, that S<T> refers precisely to the type declared by
3090 // #1 (i.e., that the constraints match). Note that the following should
3091 // be an error since there is no specialization of S<T> that is
3092 // unconstrained, but this is not diagnosed here.
3094 // template<typename T>
3095 // void S<T>::f() { }
3097 // We cannot diagnose this problem here since this function also matches
3098 // qualified template names that are not part of a definition. For example:
3100 // template<Integral T, Floating_point U>
3101 // typename pair<T, U>::first_type void f(T, U);
3103 // Here, it is unlikely that there is a partial specialization of
3104 // pair constrained for for Integral and Floating_point arguments.
3106 // The general rule is: if a constrained specialization with matching
3107 // constraints is found return that type. Also note that if TYPE is not a
3108 // class-type (e.g. a typename type), then no fixup is needed.
3110 static tree
3111 fixup_template_type (tree type)
3113 // Find the template parameter list at the a depth appropriate to
3114 // the scope we're trying to enter.
3115 tree parms = current_template_parms;
3116 int depth = template_class_depth (type);
3117 for (int n = processing_template_decl; n > depth && parms; --n)
3118 parms = TREE_CHAIN (parms);
3119 if (!parms)
3120 return type;
3121 tree cur_reqs = TEMPLATE_PARMS_CONSTRAINTS (parms);
3122 tree cur_constr = build_constraints (cur_reqs, NULL_TREE);
3124 // Search for a specialization whose type and constraints match.
3125 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
3126 tree specs = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
3127 while (specs)
3129 tree spec_constr = get_constraints (TREE_VALUE (specs));
3131 // If the type and constraints match a specialization, then we
3132 // are entering that type.
3133 if (same_type_p (type, TREE_TYPE (specs))
3134 && equivalent_constraints (cur_constr, spec_constr))
3135 return TREE_TYPE (specs);
3136 specs = TREE_CHAIN (specs);
3139 // If no specialization matches, then must return the type
3140 // previously found.
3141 return type;
3144 /* Finish processing a template-id (which names a type) of the form
3145 NAME < ARGS >. Return the TYPE_DECL for the type named by the
3146 template-id. If ENTERING_SCOPE is nonzero we are about to enter
3147 the scope of template-id indicated. */
3149 tree
3150 finish_template_type (tree name, tree args, int entering_scope)
3152 tree type;
3154 type = lookup_template_class (name, args,
3155 NULL_TREE, NULL_TREE, entering_scope,
3156 tf_warning_or_error | tf_user);
3158 /* If we might be entering the scope of a partial specialization,
3159 find the one with the right constraints. */
3160 if (flag_concepts
3161 && entering_scope
3162 && CLASS_TYPE_P (type)
3163 && CLASSTYPE_TEMPLATE_INFO (type)
3164 && dependent_type_p (type)
3165 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
3166 type = fixup_template_type (type);
3168 if (type == error_mark_node)
3169 return type;
3170 else if (CLASS_TYPE_P (type) && !alias_type_or_template_p (type))
3171 return TYPE_STUB_DECL (type);
3172 else
3173 return TYPE_NAME (type);
3176 /* Finish processing a BASE_CLASS with the indicated ACCESS_SPECIFIER.
3177 Return a TREE_LIST containing the ACCESS_SPECIFIER and the
3178 BASE_CLASS, or NULL_TREE if an error occurred. The
3179 ACCESS_SPECIFIER is one of
3180 access_{default,public,protected_private}_node. For a virtual base
3181 we set TREE_TYPE. */
3183 tree
3184 finish_base_specifier (tree base, tree access, bool virtual_p)
3186 tree result;
3188 if (base == error_mark_node)
3190 error ("invalid base-class specification");
3191 result = NULL_TREE;
3193 else if (! MAYBE_CLASS_TYPE_P (base))
3195 error ("%qT is not a class type", base);
3196 result = NULL_TREE;
3198 else
3200 if (cp_type_quals (base) != 0)
3202 /* DR 484: Can a base-specifier name a cv-qualified
3203 class type? */
3204 base = TYPE_MAIN_VARIANT (base);
3206 result = build_tree_list (access, base);
3207 if (virtual_p)
3208 TREE_TYPE (result) = integer_type_node;
3211 return result;
3214 /* If FNS is a member function, a set of member functions, or a
3215 template-id referring to one or more member functions, return a
3216 BASELINK for FNS, incorporating the current access context.
3217 Otherwise, return FNS unchanged. */
3219 tree
3220 baselink_for_fns (tree fns)
3222 tree scope;
3223 tree cl;
3225 if (BASELINK_P (fns)
3226 || error_operand_p (fns))
3227 return fns;
3229 scope = ovl_scope (fns);
3230 if (!CLASS_TYPE_P (scope))
3231 return fns;
3233 cl = currently_open_derived_class (scope);
3234 if (!cl)
3235 cl = scope;
3236 cl = TYPE_BINFO (cl);
3237 return build_baselink (cl, cl, fns, /*optype=*/NULL_TREE);
3240 /* Returns true iff DECL is a variable from a function outside
3241 the current one. */
3243 static bool
3244 outer_var_p (tree decl)
3246 return ((VAR_P (decl) || TREE_CODE (decl) == PARM_DECL)
3247 && DECL_FUNCTION_SCOPE_P (decl)
3248 && (DECL_CONTEXT (decl) != current_function_decl
3249 || parsing_nsdmi ()));
3252 /* As above, but also checks that DECL is automatic. */
3254 bool
3255 outer_automatic_var_p (tree decl)
3257 return (outer_var_p (decl)
3258 && !TREE_STATIC (decl));
3261 /* DECL satisfies outer_automatic_var_p. Possibly complain about it or
3262 rewrite it for lambda capture. */
3264 tree
3265 process_outer_var_ref (tree decl, tsubst_flags_t complain)
3267 if (cp_unevaluated_operand)
3268 /* It's not a use (3.2) if we're in an unevaluated context. */
3269 return decl;
3270 if (decl == error_mark_node)
3271 return decl;
3273 tree context = DECL_CONTEXT (decl);
3274 tree containing_function = current_function_decl;
3275 tree lambda_stack = NULL_TREE;
3276 tree lambda_expr = NULL_TREE;
3277 tree initializer = convert_from_reference (decl);
3279 /* Mark it as used now even if the use is ill-formed. */
3280 if (!mark_used (decl, complain))
3281 return error_mark_node;
3283 bool saw_generic_lambda = false;
3284 if (parsing_nsdmi ())
3285 containing_function = NULL_TREE;
3286 else
3287 /* If we are in a lambda function, we can move out until we hit
3288 1. the context,
3289 2. a non-lambda function, or
3290 3. a non-default capturing lambda function. */
3291 while (context != containing_function
3292 /* containing_function can be null with invalid generic lambdas. */
3293 && containing_function
3294 && LAMBDA_FUNCTION_P (containing_function))
3296 tree closure = DECL_CONTEXT (containing_function);
3297 lambda_expr = CLASSTYPE_LAMBDA_EXPR (closure);
3299 if (generic_lambda_fn_p (containing_function))
3300 saw_generic_lambda = true;
3302 if (TYPE_CLASS_SCOPE_P (closure))
3303 /* A lambda in an NSDMI (c++/64496). */
3304 break;
3306 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr)
3307 == CPLD_NONE)
3308 break;
3310 lambda_stack = tree_cons (NULL_TREE,
3311 lambda_expr,
3312 lambda_stack);
3314 containing_function
3315 = decl_function_context (containing_function);
3318 /* Core issue 696: "[At the July 2009 meeting] the CWG expressed
3319 support for an approach in which a reference to a local
3320 [constant] automatic variable in a nested class or lambda body
3321 would enter the expression as an rvalue, which would reduce
3322 the complexity of the problem"
3324 FIXME update for final resolution of core issue 696. */
3325 if (decl_maybe_constant_var_p (decl))
3327 if (processing_template_decl && !saw_generic_lambda)
3328 /* In a non-generic lambda within a template, wait until instantiation
3329 time to decide whether to capture. For a generic lambda, we can't
3330 wait until we instantiate the op() because the closure class is
3331 already defined at that point. FIXME to get the semantics exactly
3332 right we need to partially-instantiate the lambda body so the only
3333 dependencies left are on the generic parameters themselves. This
3334 probably means moving away from our current model of lambdas in
3335 templates (instantiating the closure type) to one based on creating
3336 the closure type when instantiating the lambda context. That is
3337 probably also the way to handle lambdas within pack expansions. */
3338 return decl;
3339 else if (decl_constant_var_p (decl))
3341 tree t = maybe_constant_value (convert_from_reference (decl));
3342 if (TREE_CONSTANT (t))
3343 return t;
3347 if (lambda_expr && VAR_P (decl)
3348 && DECL_ANON_UNION_VAR_P (decl))
3350 if (complain & tf_error)
3351 error ("cannot capture member %qD of anonymous union", decl);
3352 return error_mark_node;
3354 if (context == containing_function)
3356 decl = add_default_capture (lambda_stack,
3357 /*id=*/DECL_NAME (decl),
3358 initializer);
3360 else if (lambda_expr)
3362 if (complain & tf_error)
3364 error ("%qD is not captured", decl);
3365 tree closure = LAMBDA_EXPR_CLOSURE (lambda_expr);
3366 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr)
3367 == CPLD_NONE)
3368 inform (location_of (closure),
3369 "the lambda has no capture-default");
3370 else if (TYPE_CLASS_SCOPE_P (closure))
3371 inform (0, "lambda in local class %q+T cannot "
3372 "capture variables from the enclosing context",
3373 TYPE_CONTEXT (closure));
3374 inform (DECL_SOURCE_LOCATION (decl), "%q#D declared here", decl);
3376 return error_mark_node;
3378 else
3380 if (complain & tf_error)
3382 error (VAR_P (decl)
3383 ? G_("use of local variable with automatic storage from "
3384 "containing function")
3385 : G_("use of parameter from containing function"));
3386 inform (DECL_SOURCE_LOCATION (decl), "%q#D declared here", decl);
3388 return error_mark_node;
3390 return decl;
3393 /* ID_EXPRESSION is a representation of parsed, but unprocessed,
3394 id-expression. (See cp_parser_id_expression for details.) SCOPE,
3395 if non-NULL, is the type or namespace used to explicitly qualify
3396 ID_EXPRESSION. DECL is the entity to which that name has been
3397 resolved.
3399 *CONSTANT_EXPRESSION_P is true if we are presently parsing a
3400 constant-expression. In that case, *NON_CONSTANT_EXPRESSION_P will
3401 be set to true if this expression isn't permitted in a
3402 constant-expression, but it is otherwise not set by this function.
3403 *ALLOW_NON_CONSTANT_EXPRESSION_P is true if we are parsing a
3404 constant-expression, but a non-constant expression is also
3405 permissible.
3407 DONE is true if this expression is a complete postfix-expression;
3408 it is false if this expression is followed by '->', '[', '(', etc.
3409 ADDRESS_P is true iff this expression is the operand of '&'.
3410 TEMPLATE_P is true iff the qualified-id was of the form
3411 "A::template B". TEMPLATE_ARG_P is true iff this qualified name
3412 appears as a template argument.
3414 If an error occurs, and it is the kind of error that might cause
3415 the parser to abort a tentative parse, *ERROR_MSG is filled in. It
3416 is the caller's responsibility to issue the message. *ERROR_MSG
3417 will be a string with static storage duration, so the caller need
3418 not "free" it.
3420 Return an expression for the entity, after issuing appropriate
3421 diagnostics. This function is also responsible for transforming a
3422 reference to a non-static member into a COMPONENT_REF that makes
3423 the use of "this" explicit.
3425 Upon return, *IDK will be filled in appropriately. */
3426 cp_expr
3427 finish_id_expression (tree id_expression,
3428 tree decl,
3429 tree scope,
3430 cp_id_kind *idk,
3431 bool integral_constant_expression_p,
3432 bool allow_non_integral_constant_expression_p,
3433 bool *non_integral_constant_expression_p,
3434 bool template_p,
3435 bool done,
3436 bool address_p,
3437 bool template_arg_p,
3438 const char **error_msg,
3439 location_t location)
3441 decl = strip_using_decl (decl);
3443 /* Initialize the output parameters. */
3444 *idk = CP_ID_KIND_NONE;
3445 *error_msg = NULL;
3447 if (id_expression == error_mark_node)
3448 return error_mark_node;
3449 /* If we have a template-id, then no further lookup is
3450 required. If the template-id was for a template-class, we
3451 will sometimes have a TYPE_DECL at this point. */
3452 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3453 || TREE_CODE (decl) == TYPE_DECL)
3455 /* Look up the name. */
3456 else
3458 if (decl == error_mark_node)
3460 /* Name lookup failed. */
3461 if (scope
3462 && (!TYPE_P (scope)
3463 || (!dependent_type_p (scope)
3464 && !(identifier_p (id_expression)
3465 && IDENTIFIER_TYPENAME_P (id_expression)
3466 && dependent_type_p (TREE_TYPE (id_expression))))))
3468 /* If the qualifying type is non-dependent (and the name
3469 does not name a conversion operator to a dependent
3470 type), issue an error. */
3471 qualified_name_lookup_error (scope, id_expression, decl, location);
3472 return error_mark_node;
3474 else if (!scope)
3476 /* It may be resolved via Koenig lookup. */
3477 *idk = CP_ID_KIND_UNQUALIFIED;
3478 return id_expression;
3480 else
3481 decl = id_expression;
3483 /* If DECL is a variable that would be out of scope under
3484 ANSI/ISO rules, but in scope in the ARM, name lookup
3485 will succeed. Issue a diagnostic here. */
3486 else
3487 decl = check_for_out_of_scope_variable (decl);
3489 /* Remember that the name was used in the definition of
3490 the current class so that we can check later to see if
3491 the meaning would have been different after the class
3492 was entirely defined. */
3493 if (!scope && decl != error_mark_node && identifier_p (id_expression))
3494 maybe_note_name_used_in_class (id_expression, decl);
3496 /* A use in unevaluated operand might not be instantiated appropriately
3497 if tsubst_copy builds a dummy parm, or if we never instantiate a
3498 generic lambda, so mark it now. */
3499 if (processing_template_decl && cp_unevaluated_operand)
3500 mark_type_use (decl);
3502 /* Disallow uses of local variables from containing functions, except
3503 within lambda-expressions. */
3504 if (outer_automatic_var_p (decl))
3506 decl = process_outer_var_ref (decl, tf_warning_or_error);
3507 if (decl == error_mark_node)
3508 return error_mark_node;
3511 /* Also disallow uses of function parameters outside the function
3512 body, except inside an unevaluated context (i.e. decltype). */
3513 if (TREE_CODE (decl) == PARM_DECL
3514 && DECL_CONTEXT (decl) == NULL_TREE
3515 && !cp_unevaluated_operand)
3517 *error_msg = G_("use of parameter outside function body");
3518 return error_mark_node;
3522 /* If we didn't find anything, or what we found was a type,
3523 then this wasn't really an id-expression. */
3524 if (TREE_CODE (decl) == TEMPLATE_DECL
3525 && !DECL_FUNCTION_TEMPLATE_P (decl))
3527 *error_msg = G_("missing template arguments");
3528 return error_mark_node;
3530 else if (TREE_CODE (decl) == TYPE_DECL
3531 || TREE_CODE (decl) == NAMESPACE_DECL)
3533 *error_msg = G_("expected primary-expression");
3534 return error_mark_node;
3537 /* If the name resolved to a template parameter, there is no
3538 need to look it up again later. */
3539 if ((TREE_CODE (decl) == CONST_DECL && DECL_TEMPLATE_PARM_P (decl))
3540 || TREE_CODE (decl) == TEMPLATE_PARM_INDEX)
3542 tree r;
3544 *idk = CP_ID_KIND_NONE;
3545 if (TREE_CODE (decl) == TEMPLATE_PARM_INDEX)
3546 decl = TEMPLATE_PARM_DECL (decl);
3547 r = convert_from_reference (DECL_INITIAL (decl));
3549 if (integral_constant_expression_p
3550 && !dependent_type_p (TREE_TYPE (decl))
3551 && !(INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (r))))
3553 if (!allow_non_integral_constant_expression_p)
3554 error ("template parameter %qD of type %qT is not allowed in "
3555 "an integral constant expression because it is not of "
3556 "integral or enumeration type", decl, TREE_TYPE (decl));
3557 *non_integral_constant_expression_p = true;
3559 return r;
3561 else
3563 bool dependent_p = type_dependent_expression_p (decl);
3565 /* If the declaration was explicitly qualified indicate
3566 that. The semantics of `A::f(3)' are different than
3567 `f(3)' if `f' is virtual. */
3568 *idk = (scope
3569 ? CP_ID_KIND_QUALIFIED
3570 : (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3571 ? CP_ID_KIND_TEMPLATE_ID
3572 : (dependent_p
3573 ? CP_ID_KIND_UNQUALIFIED_DEPENDENT
3574 : CP_ID_KIND_UNQUALIFIED)));
3576 /* If the name was dependent on a template parameter and we're not in a
3577 default capturing generic lambda within a template, we will resolve the
3578 name at instantiation time. FIXME: For lambdas, we should defer
3579 building the closure type until instantiation time then we won't need
3580 the extra test here. */
3581 if (dependent_p
3582 && !parsing_default_capturing_generic_lambda_in_template ())
3584 if (DECL_P (decl)
3585 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (decl)))
3586 /* Dependent type attributes on the decl mean that the TREE_TYPE is
3587 wrong, so just return the identifier. */
3588 return id_expression;
3590 /* If we found a variable, then name lookup during the
3591 instantiation will always resolve to the same VAR_DECL
3592 (or an instantiation thereof). */
3593 if (VAR_P (decl)
3594 || TREE_CODE (decl) == CONST_DECL
3595 || TREE_CODE (decl) == PARM_DECL)
3597 mark_used (decl);
3598 return convert_from_reference (decl);
3601 /* Create a SCOPE_REF for qualified names, if the scope is
3602 dependent. */
3603 if (scope)
3605 if (TYPE_P (scope))
3607 if (address_p && done)
3608 decl = finish_qualified_id_expr (scope, decl,
3609 done, address_p,
3610 template_p,
3611 template_arg_p,
3612 tf_warning_or_error);
3613 else
3615 tree type = NULL_TREE;
3616 if (DECL_P (decl) && !dependent_scope_p (scope))
3617 type = TREE_TYPE (decl);
3618 decl = build_qualified_name (type,
3619 scope,
3620 id_expression,
3621 template_p);
3624 if (TREE_TYPE (decl))
3625 decl = convert_from_reference (decl);
3626 return decl;
3628 /* A TEMPLATE_ID already contains all the information we
3629 need. */
3630 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR)
3631 return id_expression;
3632 /* The same is true for FIELD_DECL, but we also need to
3633 make sure that the syntax is correct. */
3634 else if (TREE_CODE (decl) == FIELD_DECL)
3636 /* Since SCOPE is NULL here, this is an unqualified name.
3637 Access checking has been performed during name lookup
3638 already. Turn off checking to avoid duplicate errors. */
3639 push_deferring_access_checks (dk_no_check);
3640 decl = finish_non_static_data_member
3641 (decl, NULL_TREE,
3642 /*qualifying_scope=*/NULL_TREE);
3643 pop_deferring_access_checks ();
3644 return decl;
3646 return id_expression;
3649 if (TREE_CODE (decl) == NAMESPACE_DECL)
3651 error ("use of namespace %qD as expression", decl);
3652 return error_mark_node;
3654 else if (DECL_CLASS_TEMPLATE_P (decl))
3656 error ("use of class template %qT as expression", decl);
3657 return error_mark_node;
3659 else if (TREE_CODE (decl) == TREE_LIST)
3661 /* Ambiguous reference to base members. */
3662 error ("request for member %qD is ambiguous in "
3663 "multiple inheritance lattice", id_expression);
3664 print_candidates (decl);
3665 return error_mark_node;
3668 /* Mark variable-like entities as used. Functions are similarly
3669 marked either below or after overload resolution. */
3670 if ((VAR_P (decl)
3671 || TREE_CODE (decl) == PARM_DECL
3672 || TREE_CODE (decl) == CONST_DECL
3673 || TREE_CODE (decl) == RESULT_DECL)
3674 && !mark_used (decl))
3675 return error_mark_node;
3677 /* Only certain kinds of names are allowed in constant
3678 expression. Template parameters have already
3679 been handled above. */
3680 if (! error_operand_p (decl)
3681 && integral_constant_expression_p
3682 && ! decl_constant_var_p (decl)
3683 && TREE_CODE (decl) != CONST_DECL
3684 && ! builtin_valid_in_constant_expr_p (decl))
3686 if (!allow_non_integral_constant_expression_p)
3688 error ("%qD cannot appear in a constant-expression", decl);
3689 return error_mark_node;
3691 *non_integral_constant_expression_p = true;
3694 tree wrap;
3695 if (VAR_P (decl)
3696 && !cp_unevaluated_operand
3697 && !processing_template_decl
3698 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl))
3699 && CP_DECL_THREAD_LOCAL_P (decl)
3700 && (wrap = get_tls_wrapper_fn (decl)))
3702 /* Replace an evaluated use of the thread_local variable with
3703 a call to its wrapper. */
3704 decl = build_cxx_call (wrap, 0, NULL, tf_warning_or_error);
3706 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3707 && variable_template_p (TREE_OPERAND (decl, 0)))
3709 decl = finish_template_variable (decl);
3710 mark_used (decl);
3711 decl = convert_from_reference (decl);
3713 else if (scope)
3715 decl = (adjust_result_of_qualified_name_lookup
3716 (decl, scope, current_nonlambda_class_type()));
3718 if (TREE_CODE (decl) == FUNCTION_DECL)
3719 mark_used (decl);
3721 if (TYPE_P (scope))
3722 decl = finish_qualified_id_expr (scope,
3723 decl,
3724 done,
3725 address_p,
3726 template_p,
3727 template_arg_p,
3728 tf_warning_or_error);
3729 else
3730 decl = convert_from_reference (decl);
3732 else if (TREE_CODE (decl) == FIELD_DECL)
3734 /* Since SCOPE is NULL here, this is an unqualified name.
3735 Access checking has been performed during name lookup
3736 already. Turn off checking to avoid duplicate errors. */
3737 push_deferring_access_checks (dk_no_check);
3738 decl = finish_non_static_data_member (decl, NULL_TREE,
3739 /*qualifying_scope=*/NULL_TREE);
3740 pop_deferring_access_checks ();
3742 else if (is_overloaded_fn (decl))
3744 tree first_fn = get_first_fn (decl);
3746 if (TREE_CODE (first_fn) == TEMPLATE_DECL)
3747 first_fn = DECL_TEMPLATE_RESULT (first_fn);
3749 /* [basic.def.odr]: "A function whose name appears as a
3750 potentially-evaluated expression is odr-used if it is the unique
3751 lookup result".
3753 But only mark it if it's a complete postfix-expression; in a call,
3754 ADL might select a different function, and we'll call mark_used in
3755 build_over_call. */
3756 if (done
3757 && !really_overloaded_fn (decl)
3758 && !mark_used (first_fn))
3759 return error_mark_node;
3761 if (!template_arg_p
3762 && TREE_CODE (first_fn) == FUNCTION_DECL
3763 && DECL_FUNCTION_MEMBER_P (first_fn)
3764 && !shared_member_p (decl))
3766 /* A set of member functions. */
3767 decl = maybe_dummy_object (DECL_CONTEXT (first_fn), 0);
3768 return finish_class_member_access_expr (decl, id_expression,
3769 /*template_p=*/false,
3770 tf_warning_or_error);
3773 decl = baselink_for_fns (decl);
3775 else
3777 if (DECL_P (decl) && DECL_NONLOCAL (decl)
3778 && DECL_CLASS_SCOPE_P (decl))
3780 tree context = context_for_name_lookup (decl);
3781 if (context != current_class_type)
3783 tree path = currently_open_derived_class (context);
3784 perform_or_defer_access_check (TYPE_BINFO (path),
3785 decl, decl,
3786 tf_warning_or_error);
3790 decl = convert_from_reference (decl);
3794 return cp_expr (decl, location);
3797 /* Implement the __typeof keyword: Return the type of EXPR, suitable for
3798 use as a type-specifier. */
3800 tree
3801 finish_typeof (tree expr)
3803 tree type;
3805 if (type_dependent_expression_p (expr))
3807 type = cxx_make_type (TYPEOF_TYPE);
3808 TYPEOF_TYPE_EXPR (type) = expr;
3809 SET_TYPE_STRUCTURAL_EQUALITY (type);
3811 return type;
3814 expr = mark_type_use (expr);
3816 type = unlowered_expr_type (expr);
3818 if (!type || type == unknown_type_node)
3820 error ("type of %qE is unknown", expr);
3821 return error_mark_node;
3824 return type;
3827 /* Implement the __underlying_type keyword: Return the underlying
3828 type of TYPE, suitable for use as a type-specifier. */
3830 tree
3831 finish_underlying_type (tree type)
3833 tree underlying_type;
3835 if (processing_template_decl)
3837 underlying_type = cxx_make_type (UNDERLYING_TYPE);
3838 UNDERLYING_TYPE_TYPE (underlying_type) = type;
3839 SET_TYPE_STRUCTURAL_EQUALITY (underlying_type);
3841 return underlying_type;
3844 if (!complete_type_or_else (type, NULL_TREE))
3845 return error_mark_node;
3847 if (TREE_CODE (type) != ENUMERAL_TYPE)
3849 error ("%qT is not an enumeration type", type);
3850 return error_mark_node;
3853 underlying_type = ENUM_UNDERLYING_TYPE (type);
3855 /* Fixup necessary in this case because ENUM_UNDERLYING_TYPE
3856 includes TYPE_MIN_VALUE and TYPE_MAX_VALUE information.
3857 See finish_enum_value_list for details. */
3858 if (!ENUM_FIXED_UNDERLYING_TYPE_P (type))
3859 underlying_type
3860 = c_common_type_for_mode (TYPE_MODE (underlying_type),
3861 TYPE_UNSIGNED (underlying_type));
3863 return underlying_type;
3866 /* Implement the __direct_bases keyword: Return the direct base classes
3867 of type */
3869 tree
3870 calculate_direct_bases (tree type)
3872 vec<tree, va_gc> *vector = make_tree_vector();
3873 tree bases_vec = NULL_TREE;
3874 vec<tree, va_gc> *base_binfos;
3875 tree binfo;
3876 unsigned i;
3878 complete_type (type);
3880 if (!NON_UNION_CLASS_TYPE_P (type))
3881 return make_tree_vec (0);
3883 base_binfos = BINFO_BASE_BINFOS (TYPE_BINFO (type));
3885 /* Virtual bases are initialized first */
3886 for (i = 0; base_binfos->iterate (i, &binfo); i++)
3888 if (BINFO_VIRTUAL_P (binfo))
3890 vec_safe_push (vector, binfo);
3894 /* Now non-virtuals */
3895 for (i = 0; base_binfos->iterate (i, &binfo); i++)
3897 if (!BINFO_VIRTUAL_P (binfo))
3899 vec_safe_push (vector, binfo);
3904 bases_vec = make_tree_vec (vector->length ());
3906 for (i = 0; i < vector->length (); ++i)
3908 TREE_VEC_ELT (bases_vec, i) = BINFO_TYPE ((*vector)[i]);
3910 return bases_vec;
3913 /* Implement the __bases keyword: Return the base classes
3914 of type */
3916 /* Find morally non-virtual base classes by walking binfo hierarchy */
3917 /* Virtual base classes are handled separately in finish_bases */
3919 static tree
3920 dfs_calculate_bases_pre (tree binfo, void * /*data_*/)
3922 /* Don't walk bases of virtual bases */
3923 return BINFO_VIRTUAL_P (binfo) ? dfs_skip_bases : NULL_TREE;
3926 static tree
3927 dfs_calculate_bases_post (tree binfo, void *data_)
3929 vec<tree, va_gc> **data = ((vec<tree, va_gc> **) data_);
3930 if (!BINFO_VIRTUAL_P (binfo))
3932 vec_safe_push (*data, BINFO_TYPE (binfo));
3934 return NULL_TREE;
3937 /* Calculates the morally non-virtual base classes of a class */
3938 static vec<tree, va_gc> *
3939 calculate_bases_helper (tree type)
3941 vec<tree, va_gc> *vector = make_tree_vector();
3943 /* Now add non-virtual base classes in order of construction */
3944 if (TYPE_BINFO (type))
3945 dfs_walk_all (TYPE_BINFO (type),
3946 dfs_calculate_bases_pre, dfs_calculate_bases_post, &vector);
3947 return vector;
3950 tree
3951 calculate_bases (tree type)
3953 vec<tree, va_gc> *vector = make_tree_vector();
3954 tree bases_vec = NULL_TREE;
3955 unsigned i;
3956 vec<tree, va_gc> *vbases;
3957 vec<tree, va_gc> *nonvbases;
3958 tree binfo;
3960 complete_type (type);
3962 if (!NON_UNION_CLASS_TYPE_P (type))
3963 return make_tree_vec (0);
3965 /* First go through virtual base classes */
3966 for (vbases = CLASSTYPE_VBASECLASSES (type), i = 0;
3967 vec_safe_iterate (vbases, i, &binfo); i++)
3969 vec<tree, va_gc> *vbase_bases;
3970 vbase_bases = calculate_bases_helper (BINFO_TYPE (binfo));
3971 vec_safe_splice (vector, vbase_bases);
3972 release_tree_vector (vbase_bases);
3975 /* Now for the non-virtual bases */
3976 nonvbases = calculate_bases_helper (type);
3977 vec_safe_splice (vector, nonvbases);
3978 release_tree_vector (nonvbases);
3980 /* Note that during error recovery vector->length can even be zero. */
3981 if (vector->length () > 1)
3983 /* Last element is entire class, so don't copy */
3984 bases_vec = make_tree_vec (vector->length() - 1);
3986 for (i = 0; i < vector->length () - 1; ++i)
3987 TREE_VEC_ELT (bases_vec, i) = (*vector)[i];
3989 else
3990 bases_vec = make_tree_vec (0);
3992 release_tree_vector (vector);
3993 return bases_vec;
3996 tree
3997 finish_bases (tree type, bool direct)
3999 tree bases = NULL_TREE;
4001 if (!processing_template_decl)
4003 /* Parameter packs can only be used in templates */
4004 error ("Parameter pack __bases only valid in template declaration");
4005 return error_mark_node;
4008 bases = cxx_make_type (BASES);
4009 BASES_TYPE (bases) = type;
4010 BASES_DIRECT (bases) = direct;
4011 SET_TYPE_STRUCTURAL_EQUALITY (bases);
4013 return bases;
4016 /* Perform C++-specific checks for __builtin_offsetof before calling
4017 fold_offsetof. */
4019 tree
4020 finish_offsetof (tree object_ptr, tree expr, location_t loc)
4022 /* If we're processing a template, we can't finish the semantics yet.
4023 Otherwise we can fold the entire expression now. */
4024 if (processing_template_decl)
4026 expr = build2 (OFFSETOF_EXPR, size_type_node, expr, object_ptr);
4027 SET_EXPR_LOCATION (expr, loc);
4028 return expr;
4031 if (TREE_CODE (expr) == PSEUDO_DTOR_EXPR)
4033 error ("cannot apply %<offsetof%> to destructor %<~%T%>",
4034 TREE_OPERAND (expr, 2));
4035 return error_mark_node;
4037 if (TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE
4038 || TREE_CODE (TREE_TYPE (expr)) == METHOD_TYPE
4039 || TREE_TYPE (expr) == unknown_type_node)
4041 if (INDIRECT_REF_P (expr))
4042 error ("second operand of %<offsetof%> is neither a single "
4043 "identifier nor a sequence of member accesses and "
4044 "array references");
4045 else
4047 if (TREE_CODE (expr) == COMPONENT_REF
4048 || TREE_CODE (expr) == COMPOUND_EXPR)
4049 expr = TREE_OPERAND (expr, 1);
4050 error ("cannot apply %<offsetof%> to member function %qD", expr);
4052 return error_mark_node;
4054 if (REFERENCE_REF_P (expr))
4055 expr = TREE_OPERAND (expr, 0);
4056 if (!complete_type_or_else (TREE_TYPE (TREE_TYPE (object_ptr)), object_ptr))
4057 return error_mark_node;
4058 if (warn_invalid_offsetof
4059 && CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (object_ptr)))
4060 && CLASSTYPE_NON_STD_LAYOUT (TREE_TYPE (TREE_TYPE (object_ptr)))
4061 && cp_unevaluated_operand == 0)
4062 pedwarn (loc, OPT_Winvalid_offsetof,
4063 "offsetof within non-standard-layout type %qT is undefined",
4064 TREE_TYPE (TREE_TYPE (object_ptr)));
4065 return fold_offsetof (expr);
4068 /* Replace the AGGR_INIT_EXPR at *TP with an equivalent CALL_EXPR. This
4069 function is broken out from the above for the benefit of the tree-ssa
4070 project. */
4072 void
4073 simplify_aggr_init_expr (tree *tp)
4075 tree aggr_init_expr = *tp;
4077 /* Form an appropriate CALL_EXPR. */
4078 tree fn = AGGR_INIT_EXPR_FN (aggr_init_expr);
4079 tree slot = AGGR_INIT_EXPR_SLOT (aggr_init_expr);
4080 tree type = TREE_TYPE (slot);
4082 tree call_expr;
4083 enum style_t { ctor, arg, pcc } style;
4085 if (AGGR_INIT_VIA_CTOR_P (aggr_init_expr))
4086 style = ctor;
4087 #ifdef PCC_STATIC_STRUCT_RETURN
4088 else if (1)
4089 style = pcc;
4090 #endif
4091 else
4093 gcc_assert (TREE_ADDRESSABLE (type));
4094 style = arg;
4097 call_expr = build_call_array_loc (input_location,
4098 TREE_TYPE (TREE_TYPE (TREE_TYPE (fn))),
4100 aggr_init_expr_nargs (aggr_init_expr),
4101 AGGR_INIT_EXPR_ARGP (aggr_init_expr));
4102 TREE_NOTHROW (call_expr) = TREE_NOTHROW (aggr_init_expr);
4103 CALL_FROM_THUNK_P (call_expr) = AGGR_INIT_FROM_THUNK_P (aggr_init_expr);
4104 CALL_EXPR_OPERATOR_SYNTAX (call_expr)
4105 = CALL_EXPR_OPERATOR_SYNTAX (aggr_init_expr);
4106 CALL_EXPR_ORDERED_ARGS (call_expr) = CALL_EXPR_ORDERED_ARGS (aggr_init_expr);
4107 CALL_EXPR_REVERSE_ARGS (call_expr) = CALL_EXPR_REVERSE_ARGS (aggr_init_expr);
4108 /* Preserve CILK_SPAWN flag. */
4109 EXPR_CILK_SPAWN (call_expr) = EXPR_CILK_SPAWN (aggr_init_expr);
4111 if (style == ctor)
4113 /* Replace the first argument to the ctor with the address of the
4114 slot. */
4115 cxx_mark_addressable (slot);
4116 CALL_EXPR_ARG (call_expr, 0) =
4117 build1 (ADDR_EXPR, build_pointer_type (type), slot);
4119 else if (style == arg)
4121 /* Just mark it addressable here, and leave the rest to
4122 expand_call{,_inline}. */
4123 cxx_mark_addressable (slot);
4124 CALL_EXPR_RETURN_SLOT_OPT (call_expr) = true;
4125 call_expr = build2 (INIT_EXPR, TREE_TYPE (call_expr), slot, call_expr);
4127 else if (style == pcc)
4129 /* If we're using the non-reentrant PCC calling convention, then we
4130 need to copy the returned value out of the static buffer into the
4131 SLOT. */
4132 push_deferring_access_checks (dk_no_check);
4133 call_expr = build_aggr_init (slot, call_expr,
4134 DIRECT_BIND | LOOKUP_ONLYCONVERTING,
4135 tf_warning_or_error);
4136 pop_deferring_access_checks ();
4137 call_expr = build2 (COMPOUND_EXPR, TREE_TYPE (slot), call_expr, slot);
4140 if (AGGR_INIT_ZERO_FIRST (aggr_init_expr))
4142 tree init = build_zero_init (type, NULL_TREE,
4143 /*static_storage_p=*/false);
4144 init = build2 (INIT_EXPR, void_type_node, slot, init);
4145 call_expr = build2 (COMPOUND_EXPR, TREE_TYPE (call_expr),
4146 init, call_expr);
4149 *tp = call_expr;
4152 /* Emit all thunks to FN that should be emitted when FN is emitted. */
4154 void
4155 emit_associated_thunks (tree fn)
4157 /* When we use vcall offsets, we emit thunks with the virtual
4158 functions to which they thunk. The whole point of vcall offsets
4159 is so that you can know statically the entire set of thunks that
4160 will ever be needed for a given virtual function, thereby
4161 enabling you to output all the thunks with the function itself. */
4162 if (DECL_VIRTUAL_P (fn)
4163 /* Do not emit thunks for extern template instantiations. */
4164 && ! DECL_REALLY_EXTERN (fn))
4166 tree thunk;
4168 for (thunk = DECL_THUNKS (fn); thunk; thunk = DECL_CHAIN (thunk))
4170 if (!THUNK_ALIAS (thunk))
4172 use_thunk (thunk, /*emit_p=*/1);
4173 if (DECL_RESULT_THUNK_P (thunk))
4175 tree probe;
4177 for (probe = DECL_THUNKS (thunk);
4178 probe; probe = DECL_CHAIN (probe))
4179 use_thunk (probe, /*emit_p=*/1);
4182 else
4183 gcc_assert (!DECL_THUNKS (thunk));
4188 /* Generate RTL for FN. */
4190 bool
4191 expand_or_defer_fn_1 (tree fn)
4193 /* When the parser calls us after finishing the body of a template
4194 function, we don't really want to expand the body. */
4195 if (processing_template_decl)
4197 /* Normally, collection only occurs in rest_of_compilation. So,
4198 if we don't collect here, we never collect junk generated
4199 during the processing of templates until we hit a
4200 non-template function. It's not safe to do this inside a
4201 nested class, though, as the parser may have local state that
4202 is not a GC root. */
4203 if (!function_depth)
4204 ggc_collect ();
4205 return false;
4208 gcc_assert (DECL_SAVED_TREE (fn));
4210 /* We make a decision about linkage for these functions at the end
4211 of the compilation. Until that point, we do not want the back
4212 end to output them -- but we do want it to see the bodies of
4213 these functions so that it can inline them as appropriate. */
4214 if (DECL_DECLARED_INLINE_P (fn) || DECL_IMPLICIT_INSTANTIATION (fn))
4216 if (DECL_INTERFACE_KNOWN (fn))
4217 /* We've already made a decision as to how this function will
4218 be handled. */;
4219 else if (!at_eof)
4220 tentative_decl_linkage (fn);
4221 else
4222 import_export_decl (fn);
4224 /* If the user wants us to keep all inline functions, then mark
4225 this function as needed so that finish_file will make sure to
4226 output it later. Similarly, all dllexport'd functions must
4227 be emitted; there may be callers in other DLLs. */
4228 if (DECL_DECLARED_INLINE_P (fn)
4229 && !DECL_REALLY_EXTERN (fn)
4230 && (flag_keep_inline_functions
4231 || (flag_keep_inline_dllexport
4232 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (fn)))))
4234 mark_needed (fn);
4235 DECL_EXTERNAL (fn) = 0;
4239 /* If this is a constructor or destructor body, we have to clone
4240 it. */
4241 if (maybe_clone_body (fn))
4243 /* We don't want to process FN again, so pretend we've written
4244 it out, even though we haven't. */
4245 TREE_ASM_WRITTEN (fn) = 1;
4246 /* If this is a constexpr function, keep DECL_SAVED_TREE. */
4247 if (!DECL_DECLARED_CONSTEXPR_P (fn))
4248 DECL_SAVED_TREE (fn) = NULL_TREE;
4249 return false;
4252 /* There's no reason to do any of the work here if we're only doing
4253 semantic analysis; this code just generates RTL. */
4254 if (flag_syntax_only)
4255 return false;
4257 return true;
4260 void
4261 expand_or_defer_fn (tree fn)
4263 if (expand_or_defer_fn_1 (fn))
4265 function_depth++;
4267 /* Expand or defer, at the whim of the compilation unit manager. */
4268 cgraph_node::finalize_function (fn, function_depth > 1);
4269 emit_associated_thunks (fn);
4271 function_depth--;
4275 struct nrv_data
4277 nrv_data () : visited (37) {}
4279 tree var;
4280 tree result;
4281 hash_table<nofree_ptr_hash <tree_node> > visited;
4284 /* Helper function for walk_tree, used by finalize_nrv below. */
4286 static tree
4287 finalize_nrv_r (tree* tp, int* walk_subtrees, void* data)
4289 struct nrv_data *dp = (struct nrv_data *)data;
4290 tree_node **slot;
4292 /* No need to walk into types. There wouldn't be any need to walk into
4293 non-statements, except that we have to consider STMT_EXPRs. */
4294 if (TYPE_P (*tp))
4295 *walk_subtrees = 0;
4296 /* Change all returns to just refer to the RESULT_DECL; this is a nop,
4297 but differs from using NULL_TREE in that it indicates that we care
4298 about the value of the RESULT_DECL. */
4299 else if (TREE_CODE (*tp) == RETURN_EXPR)
4300 TREE_OPERAND (*tp, 0) = dp->result;
4301 /* Change all cleanups for the NRV to only run when an exception is
4302 thrown. */
4303 else if (TREE_CODE (*tp) == CLEANUP_STMT
4304 && CLEANUP_DECL (*tp) == dp->var)
4305 CLEANUP_EH_ONLY (*tp) = 1;
4306 /* Replace the DECL_EXPR for the NRV with an initialization of the
4307 RESULT_DECL, if needed. */
4308 else if (TREE_CODE (*tp) == DECL_EXPR
4309 && DECL_EXPR_DECL (*tp) == dp->var)
4311 tree init;
4312 if (DECL_INITIAL (dp->var)
4313 && DECL_INITIAL (dp->var) != error_mark_node)
4314 init = build2 (INIT_EXPR, void_type_node, dp->result,
4315 DECL_INITIAL (dp->var));
4316 else
4317 init = build_empty_stmt (EXPR_LOCATION (*tp));
4318 DECL_INITIAL (dp->var) = NULL_TREE;
4319 SET_EXPR_LOCATION (init, EXPR_LOCATION (*tp));
4320 *tp = init;
4322 /* And replace all uses of the NRV with the RESULT_DECL. */
4323 else if (*tp == dp->var)
4324 *tp = dp->result;
4326 /* Avoid walking into the same tree more than once. Unfortunately, we
4327 can't just use walk_tree_without duplicates because it would only call
4328 us for the first occurrence of dp->var in the function body. */
4329 slot = dp->visited.find_slot (*tp, INSERT);
4330 if (*slot)
4331 *walk_subtrees = 0;
4332 else
4333 *slot = *tp;
4335 /* Keep iterating. */
4336 return NULL_TREE;
4339 /* Called from finish_function to implement the named return value
4340 optimization by overriding all the RETURN_EXPRs and pertinent
4341 CLEANUP_STMTs and replacing all occurrences of VAR with RESULT, the
4342 RESULT_DECL for the function. */
4344 void
4345 finalize_nrv (tree *tp, tree var, tree result)
4347 struct nrv_data data;
4349 /* Copy name from VAR to RESULT. */
4350 DECL_NAME (result) = DECL_NAME (var);
4351 /* Don't forget that we take its address. */
4352 TREE_ADDRESSABLE (result) = TREE_ADDRESSABLE (var);
4353 /* Finally set DECL_VALUE_EXPR to avoid assigning
4354 a stack slot at -O0 for the original var and debug info
4355 uses RESULT location for VAR. */
4356 SET_DECL_VALUE_EXPR (var, result);
4357 DECL_HAS_VALUE_EXPR_P (var) = 1;
4359 data.var = var;
4360 data.result = result;
4361 cp_walk_tree (tp, finalize_nrv_r, &data, 0);
4364 /* Create CP_OMP_CLAUSE_INFO for clause C. Returns true if it is invalid. */
4366 bool
4367 cxx_omp_create_clause_info (tree c, tree type, bool need_default_ctor,
4368 bool need_copy_ctor, bool need_copy_assignment,
4369 bool need_dtor)
4371 int save_errorcount = errorcount;
4372 tree info, t;
4374 /* Always allocate 3 elements for simplicity. These are the
4375 function decls for the ctor, dtor, and assignment op.
4376 This layout is known to the three lang hooks,
4377 cxx_omp_clause_default_init, cxx_omp_clause_copy_init,
4378 and cxx_omp_clause_assign_op. */
4379 info = make_tree_vec (3);
4380 CP_OMP_CLAUSE_INFO (c) = info;
4382 if (need_default_ctor || need_copy_ctor)
4384 if (need_default_ctor)
4385 t = get_default_ctor (type);
4386 else
4387 t = get_copy_ctor (type, tf_warning_or_error);
4389 if (t && !trivial_fn_p (t))
4390 TREE_VEC_ELT (info, 0) = t;
4393 if (need_dtor && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
4394 TREE_VEC_ELT (info, 1) = get_dtor (type, tf_warning_or_error);
4396 if (need_copy_assignment)
4398 t = get_copy_assign (type);
4400 if (t && !trivial_fn_p (t))
4401 TREE_VEC_ELT (info, 2) = t;
4404 return errorcount != save_errorcount;
4407 /* If DECL is DECL_OMP_PRIVATIZED_MEMBER, return corresponding
4408 FIELD_DECL, otherwise return DECL itself. */
4410 static tree
4411 omp_clause_decl_field (tree decl)
4413 if (VAR_P (decl)
4414 && DECL_HAS_VALUE_EXPR_P (decl)
4415 && DECL_ARTIFICIAL (decl)
4416 && DECL_LANG_SPECIFIC (decl)
4417 && DECL_OMP_PRIVATIZED_MEMBER (decl))
4419 tree f = DECL_VALUE_EXPR (decl);
4420 if (TREE_CODE (f) == INDIRECT_REF)
4421 f = TREE_OPERAND (f, 0);
4422 if (TREE_CODE (f) == COMPONENT_REF)
4424 f = TREE_OPERAND (f, 1);
4425 gcc_assert (TREE_CODE (f) == FIELD_DECL);
4426 return f;
4429 return NULL_TREE;
4432 /* Adjust DECL if needed for printing using %qE. */
4434 static tree
4435 omp_clause_printable_decl (tree decl)
4437 tree t = omp_clause_decl_field (decl);
4438 if (t)
4439 return t;
4440 return decl;
4443 /* For a FIELD_DECL F and corresponding DECL_OMP_PRIVATIZED_MEMBER
4444 VAR_DECL T that doesn't need a DECL_EXPR added, record it for
4445 privatization. */
4447 static void
4448 omp_note_field_privatization (tree f, tree t)
4450 if (!omp_private_member_map)
4451 omp_private_member_map = new hash_map<tree, tree>;
4452 tree &v = omp_private_member_map->get_or_insert (f);
4453 if (v == NULL_TREE)
4455 v = t;
4456 omp_private_member_vec.safe_push (f);
4457 /* Signal that we don't want to create DECL_EXPR for this dummy var. */
4458 omp_private_member_vec.safe_push (integer_zero_node);
4462 /* Privatize FIELD_DECL T, return corresponding DECL_OMP_PRIVATIZED_MEMBER
4463 dummy VAR_DECL. */
4465 tree
4466 omp_privatize_field (tree t, bool shared)
4468 tree m = finish_non_static_data_member (t, NULL_TREE, NULL_TREE);
4469 if (m == error_mark_node)
4470 return error_mark_node;
4471 if (!omp_private_member_map && !shared)
4472 omp_private_member_map = new hash_map<tree, tree>;
4473 if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE)
4475 gcc_assert (TREE_CODE (m) == INDIRECT_REF);
4476 m = TREE_OPERAND (m, 0);
4478 tree vb = NULL_TREE;
4479 tree &v = shared ? vb : omp_private_member_map->get_or_insert (t);
4480 if (v == NULL_TREE)
4482 v = create_temporary_var (TREE_TYPE (m));
4483 retrofit_lang_decl (v);
4484 DECL_OMP_PRIVATIZED_MEMBER (v) = 1;
4485 SET_DECL_VALUE_EXPR (v, m);
4486 DECL_HAS_VALUE_EXPR_P (v) = 1;
4487 if (!shared)
4488 omp_private_member_vec.safe_push (t);
4490 return v;
4493 /* Helper function for handle_omp_array_sections. Called recursively
4494 to handle multiple array-section-subscripts. C is the clause,
4495 T current expression (initially OMP_CLAUSE_DECL), which is either
4496 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
4497 expression if specified, TREE_VALUE length expression if specified,
4498 TREE_CHAIN is what it has been specified after, or some decl.
4499 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
4500 set to true if any of the array-section-subscript could have length
4501 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
4502 first array-section-subscript which is known not to have length
4503 of one. Given say:
4504 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
4505 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
4506 all are or may have length of 1, array-section-subscript [:2] is the
4507 first one known not to have length 1. For array-section-subscript
4508 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
4509 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
4510 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
4511 case though, as some lengths could be zero. */
4513 static tree
4514 handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
4515 bool &maybe_zero_len, unsigned int &first_non_one,
4516 enum c_omp_region_type ort)
4518 tree ret, low_bound, length, type;
4519 if (TREE_CODE (t) != TREE_LIST)
4521 if (error_operand_p (t))
4522 return error_mark_node;
4523 if (REFERENCE_REF_P (t)
4524 && TREE_CODE (TREE_OPERAND (t, 0)) == COMPONENT_REF)
4525 t = TREE_OPERAND (t, 0);
4526 ret = t;
4527 if (TREE_CODE (t) == COMPONENT_REF
4528 && ort == C_ORT_OMP
4529 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
4530 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO
4531 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FROM)
4532 && !type_dependent_expression_p (t))
4534 if (TREE_CODE (TREE_OPERAND (t, 1)) == FIELD_DECL
4535 && DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
4537 error_at (OMP_CLAUSE_LOCATION (c),
4538 "bit-field %qE in %qs clause",
4539 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4540 return error_mark_node;
4542 while (TREE_CODE (t) == COMPONENT_REF)
4544 if (TREE_TYPE (TREE_OPERAND (t, 0))
4545 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == UNION_TYPE)
4547 error_at (OMP_CLAUSE_LOCATION (c),
4548 "%qE is a member of a union", t);
4549 return error_mark_node;
4551 t = TREE_OPERAND (t, 0);
4553 if (REFERENCE_REF_P (t))
4554 t = TREE_OPERAND (t, 0);
4556 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
4558 if (processing_template_decl)
4559 return NULL_TREE;
4560 if (DECL_P (t))
4561 error_at (OMP_CLAUSE_LOCATION (c),
4562 "%qD is not a variable in %qs clause", t,
4563 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4564 else
4565 error_at (OMP_CLAUSE_LOCATION (c),
4566 "%qE is not a variable in %qs clause", t,
4567 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4568 return error_mark_node;
4570 else if (TREE_CODE (t) == PARM_DECL
4571 && DECL_ARTIFICIAL (t)
4572 && DECL_NAME (t) == this_identifier)
4574 error_at (OMP_CLAUSE_LOCATION (c),
4575 "%<this%> allowed in OpenMP only in %<declare simd%>"
4576 " clauses");
4577 return error_mark_node;
4579 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
4580 && VAR_P (t) && CP_DECL_THREAD_LOCAL_P (t))
4582 error_at (OMP_CLAUSE_LOCATION (c),
4583 "%qD is threadprivate variable in %qs clause", t,
4584 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4585 return error_mark_node;
4587 if (type_dependent_expression_p (ret))
4588 return NULL_TREE;
4589 ret = convert_from_reference (ret);
4590 return ret;
4593 if (ort == C_ORT_OMP
4594 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
4595 && TREE_CODE (TREE_CHAIN (t)) == FIELD_DECL)
4596 TREE_CHAIN (t) = omp_privatize_field (TREE_CHAIN (t), false);
4597 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
4598 maybe_zero_len, first_non_one, ort);
4599 if (ret == error_mark_node || ret == NULL_TREE)
4600 return ret;
4602 type = TREE_TYPE (ret);
4603 low_bound = TREE_PURPOSE (t);
4604 length = TREE_VALUE (t);
4605 if ((low_bound && type_dependent_expression_p (low_bound))
4606 || (length && type_dependent_expression_p (length)))
4607 return NULL_TREE;
4609 if (low_bound == error_mark_node || length == error_mark_node)
4610 return error_mark_node;
4612 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
4614 error_at (OMP_CLAUSE_LOCATION (c),
4615 "low bound %qE of array section does not have integral type",
4616 low_bound);
4617 return error_mark_node;
4619 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
4621 error_at (OMP_CLAUSE_LOCATION (c),
4622 "length %qE of array section does not have integral type",
4623 length);
4624 return error_mark_node;
4626 if (low_bound)
4627 low_bound = mark_rvalue_use (low_bound);
4628 if (length)
4629 length = mark_rvalue_use (length);
4630 /* We need to reduce to real constant-values for checks below. */
4631 if (length)
4632 length = fold_simple (length);
4633 if (low_bound)
4634 low_bound = fold_simple (low_bound);
4635 if (low_bound
4636 && TREE_CODE (low_bound) == INTEGER_CST
4637 && TYPE_PRECISION (TREE_TYPE (low_bound))
4638 > TYPE_PRECISION (sizetype))
4639 low_bound = fold_convert (sizetype, low_bound);
4640 if (length
4641 && TREE_CODE (length) == INTEGER_CST
4642 && TYPE_PRECISION (TREE_TYPE (length))
4643 > TYPE_PRECISION (sizetype))
4644 length = fold_convert (sizetype, length);
4645 if (low_bound == NULL_TREE)
4646 low_bound = integer_zero_node;
4648 if (length != NULL_TREE)
4650 if (!integer_nonzerop (length))
4652 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
4653 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
4655 if (integer_zerop (length))
4657 error_at (OMP_CLAUSE_LOCATION (c),
4658 "zero length array section in %qs clause",
4659 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4660 return error_mark_node;
4663 else
4664 maybe_zero_len = true;
4666 if (first_non_one == types.length ()
4667 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
4668 first_non_one++;
4670 if (TREE_CODE (type) == ARRAY_TYPE)
4672 if (length == NULL_TREE
4673 && (TYPE_DOMAIN (type) == NULL_TREE
4674 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
4676 error_at (OMP_CLAUSE_LOCATION (c),
4677 "for unknown bound array type length expression must "
4678 "be specified");
4679 return error_mark_node;
4681 if (TREE_CODE (low_bound) == INTEGER_CST
4682 && tree_int_cst_sgn (low_bound) == -1)
4684 error_at (OMP_CLAUSE_LOCATION (c),
4685 "negative low bound in array section in %qs clause",
4686 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4687 return error_mark_node;
4689 if (length != NULL_TREE
4690 && TREE_CODE (length) == INTEGER_CST
4691 && tree_int_cst_sgn (length) == -1)
4693 error_at (OMP_CLAUSE_LOCATION (c),
4694 "negative length in array section in %qs clause",
4695 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4696 return error_mark_node;
4698 if (TYPE_DOMAIN (type)
4699 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
4700 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
4701 == INTEGER_CST)
4703 tree size = size_binop (PLUS_EXPR,
4704 TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
4705 size_one_node);
4706 if (TREE_CODE (low_bound) == INTEGER_CST)
4708 if (tree_int_cst_lt (size, low_bound))
4710 error_at (OMP_CLAUSE_LOCATION (c),
4711 "low bound %qE above array section size "
4712 "in %qs clause", low_bound,
4713 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4714 return error_mark_node;
4716 if (tree_int_cst_equal (size, low_bound))
4718 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
4719 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
4721 error_at (OMP_CLAUSE_LOCATION (c),
4722 "zero length array section in %qs clause",
4723 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4724 return error_mark_node;
4726 maybe_zero_len = true;
4728 else if (length == NULL_TREE
4729 && first_non_one == types.length ()
4730 && tree_int_cst_equal
4731 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
4732 low_bound))
4733 first_non_one++;
4735 else if (length == NULL_TREE)
4737 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
4738 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION)
4739 maybe_zero_len = true;
4740 if (first_non_one == types.length ())
4741 first_non_one++;
4743 if (length && TREE_CODE (length) == INTEGER_CST)
4745 if (tree_int_cst_lt (size, length))
4747 error_at (OMP_CLAUSE_LOCATION (c),
4748 "length %qE above array section size "
4749 "in %qs clause", length,
4750 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4751 return error_mark_node;
4753 if (TREE_CODE (low_bound) == INTEGER_CST)
4755 tree lbpluslen
4756 = size_binop (PLUS_EXPR,
4757 fold_convert (sizetype, low_bound),
4758 fold_convert (sizetype, length));
4759 if (TREE_CODE (lbpluslen) == INTEGER_CST
4760 && tree_int_cst_lt (size, lbpluslen))
4762 error_at (OMP_CLAUSE_LOCATION (c),
4763 "high bound %qE above array section size "
4764 "in %qs clause", lbpluslen,
4765 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4766 return error_mark_node;
4771 else if (length == NULL_TREE)
4773 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
4774 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION)
4775 maybe_zero_len = true;
4776 if (first_non_one == types.length ())
4777 first_non_one++;
4780 /* For [lb:] we will need to evaluate lb more than once. */
4781 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
4783 tree lb = cp_save_expr (low_bound);
4784 if (lb != low_bound)
4786 TREE_PURPOSE (t) = lb;
4787 low_bound = lb;
4791 else if (TREE_CODE (type) == POINTER_TYPE)
4793 if (length == NULL_TREE)
4795 error_at (OMP_CLAUSE_LOCATION (c),
4796 "for pointer type length expression must be specified");
4797 return error_mark_node;
4799 if (length != NULL_TREE
4800 && TREE_CODE (length) == INTEGER_CST
4801 && tree_int_cst_sgn (length) == -1)
4803 error_at (OMP_CLAUSE_LOCATION (c),
4804 "negative length in array section in %qs clause",
4805 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4806 return error_mark_node;
4808 /* If there is a pointer type anywhere but in the very first
4809 array-section-subscript, the array section can't be contiguous. */
4810 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
4811 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
4813 error_at (OMP_CLAUSE_LOCATION (c),
4814 "array section is not contiguous in %qs clause",
4815 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4816 return error_mark_node;
4819 else
4821 error_at (OMP_CLAUSE_LOCATION (c),
4822 "%qE does not have pointer or array type", ret);
4823 return error_mark_node;
4825 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
4826 types.safe_push (TREE_TYPE (ret));
4827 /* We will need to evaluate lb more than once. */
4828 tree lb = cp_save_expr (low_bound);
4829 if (lb != low_bound)
4831 TREE_PURPOSE (t) = lb;
4832 low_bound = lb;
4834 ret = grok_array_decl (OMP_CLAUSE_LOCATION (c), ret, low_bound, false);
4835 return ret;
4838 /* Handle array sections for clause C. */
4840 static bool
4841 handle_omp_array_sections (tree c, enum c_omp_region_type ort)
4843 bool maybe_zero_len = false;
4844 unsigned int first_non_one = 0;
4845 auto_vec<tree, 10> types;
4846 tree first = handle_omp_array_sections_1 (c, OMP_CLAUSE_DECL (c), types,
4847 maybe_zero_len, first_non_one,
4848 ort);
4849 if (first == error_mark_node)
4850 return true;
4851 if (first == NULL_TREE)
4852 return false;
4853 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
4855 tree t = OMP_CLAUSE_DECL (c);
4856 tree tem = NULL_TREE;
4857 if (processing_template_decl)
4858 return false;
4859 /* Need to evaluate side effects in the length expressions
4860 if any. */
4861 while (TREE_CODE (t) == TREE_LIST)
4863 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
4865 if (tem == NULL_TREE)
4866 tem = TREE_VALUE (t);
4867 else
4868 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
4869 TREE_VALUE (t), tem);
4871 t = TREE_CHAIN (t);
4873 if (tem)
4874 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
4875 OMP_CLAUSE_DECL (c) = first;
4877 else
4879 unsigned int num = types.length (), i;
4880 tree t, side_effects = NULL_TREE, size = NULL_TREE;
4881 tree condition = NULL_TREE;
4883 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
4884 maybe_zero_len = true;
4885 if (processing_template_decl && maybe_zero_len)
4886 return false;
4888 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
4889 t = TREE_CHAIN (t))
4891 tree low_bound = TREE_PURPOSE (t);
4892 tree length = TREE_VALUE (t);
4894 i--;
4895 if (low_bound
4896 && TREE_CODE (low_bound) == INTEGER_CST
4897 && TYPE_PRECISION (TREE_TYPE (low_bound))
4898 > TYPE_PRECISION (sizetype))
4899 low_bound = fold_convert (sizetype, low_bound);
4900 if (length
4901 && TREE_CODE (length) == INTEGER_CST
4902 && TYPE_PRECISION (TREE_TYPE (length))
4903 > TYPE_PRECISION (sizetype))
4904 length = fold_convert (sizetype, length);
4905 if (low_bound == NULL_TREE)
4906 low_bound = integer_zero_node;
4907 if (!maybe_zero_len && i > first_non_one)
4909 if (integer_nonzerop (low_bound))
4910 goto do_warn_noncontiguous;
4911 if (length != NULL_TREE
4912 && TREE_CODE (length) == INTEGER_CST
4913 && TYPE_DOMAIN (types[i])
4914 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
4915 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
4916 == INTEGER_CST)
4918 tree size;
4919 size = size_binop (PLUS_EXPR,
4920 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
4921 size_one_node);
4922 if (!tree_int_cst_equal (length, size))
4924 do_warn_noncontiguous:
4925 error_at (OMP_CLAUSE_LOCATION (c),
4926 "array section is not contiguous in %qs "
4927 "clause",
4928 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4929 return true;
4932 if (!processing_template_decl
4933 && length != NULL_TREE
4934 && TREE_SIDE_EFFECTS (length))
4936 if (side_effects == NULL_TREE)
4937 side_effects = length;
4938 else
4939 side_effects = build2 (COMPOUND_EXPR,
4940 TREE_TYPE (side_effects),
4941 length, side_effects);
4944 else if (processing_template_decl)
4945 continue;
4946 else
4948 tree l;
4950 if (i > first_non_one
4951 && ((length && integer_nonzerop (length))
4952 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION))
4953 continue;
4954 if (length)
4955 l = fold_convert (sizetype, length);
4956 else
4958 l = size_binop (PLUS_EXPR,
4959 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
4960 size_one_node);
4961 l = size_binop (MINUS_EXPR, l,
4962 fold_convert (sizetype, low_bound));
4964 if (i > first_non_one)
4966 l = fold_build2 (NE_EXPR, boolean_type_node, l,
4967 size_zero_node);
4968 if (condition == NULL_TREE)
4969 condition = l;
4970 else
4971 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
4972 l, condition);
4974 else if (size == NULL_TREE)
4976 size = size_in_bytes (TREE_TYPE (types[i]));
4977 tree eltype = TREE_TYPE (types[num - 1]);
4978 while (TREE_CODE (eltype) == ARRAY_TYPE)
4979 eltype = TREE_TYPE (eltype);
4980 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
4981 size = size_binop (EXACT_DIV_EXPR, size,
4982 size_in_bytes (eltype));
4983 size = size_binop (MULT_EXPR, size, l);
4984 if (condition)
4985 size = fold_build3 (COND_EXPR, sizetype, condition,
4986 size, size_zero_node);
4988 else
4989 size = size_binop (MULT_EXPR, size, l);
4992 if (!processing_template_decl)
4994 if (side_effects)
4995 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
4996 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
4998 size = size_binop (MINUS_EXPR, size, size_one_node);
4999 tree index_type = build_index_type (size);
5000 tree eltype = TREE_TYPE (first);
5001 while (TREE_CODE (eltype) == ARRAY_TYPE)
5002 eltype = TREE_TYPE (eltype);
5003 tree type = build_array_type (eltype, index_type);
5004 tree ptype = build_pointer_type (eltype);
5005 if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE
5006 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (t))))
5007 t = convert_from_reference (t);
5008 else if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
5009 t = build_fold_addr_expr (t);
5010 tree t2 = build_fold_addr_expr (first);
5011 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
5012 ptrdiff_type_node, t2);
5013 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
5014 ptrdiff_type_node, t2,
5015 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
5016 ptrdiff_type_node, t));
5017 if (tree_fits_shwi_p (t2))
5018 t = build2 (MEM_REF, type, t,
5019 build_int_cst (ptype, tree_to_shwi (t2)));
5020 else
5022 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
5023 sizetype, t2);
5024 t = build2_loc (OMP_CLAUSE_LOCATION (c), POINTER_PLUS_EXPR,
5025 TREE_TYPE (t), t, t2);
5026 t = build2 (MEM_REF, type, t, build_int_cst (ptype, 0));
5028 OMP_CLAUSE_DECL (c) = t;
5029 return false;
5031 OMP_CLAUSE_DECL (c) = first;
5032 OMP_CLAUSE_SIZE (c) = size;
5033 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
5034 || (TREE_CODE (t) == COMPONENT_REF
5035 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE))
5036 return false;
5037 if (ort == C_ORT_OMP || ort == C_ORT_ACC)
5038 switch (OMP_CLAUSE_MAP_KIND (c))
5040 case GOMP_MAP_ALLOC:
5041 case GOMP_MAP_TO:
5042 case GOMP_MAP_FROM:
5043 case GOMP_MAP_TOFROM:
5044 case GOMP_MAP_ALWAYS_TO:
5045 case GOMP_MAP_ALWAYS_FROM:
5046 case GOMP_MAP_ALWAYS_TOFROM:
5047 case GOMP_MAP_RELEASE:
5048 case GOMP_MAP_DELETE:
5049 case GOMP_MAP_FORCE_TO:
5050 case GOMP_MAP_FORCE_FROM:
5051 case GOMP_MAP_FORCE_TOFROM:
5052 case GOMP_MAP_FORCE_PRESENT:
5053 OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (c) = 1;
5054 break;
5055 default:
5056 break;
5058 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
5059 OMP_CLAUSE_MAP);
5060 if ((ort & C_ORT_OMP_DECLARE_SIMD) != C_ORT_OMP && ort != C_ORT_ACC)
5061 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_POINTER);
5062 else if (TREE_CODE (t) == COMPONENT_REF)
5063 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ALWAYS_POINTER);
5064 else if (REFERENCE_REF_P (t)
5065 && TREE_CODE (TREE_OPERAND (t, 0)) == COMPONENT_REF)
5067 t = TREE_OPERAND (t, 0);
5068 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ALWAYS_POINTER);
5070 else
5071 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_FIRSTPRIVATE_POINTER);
5072 if (OMP_CLAUSE_MAP_KIND (c2) != GOMP_MAP_FIRSTPRIVATE_POINTER
5073 && !cxx_mark_addressable (t))
5074 return false;
5075 OMP_CLAUSE_DECL (c2) = t;
5076 t = build_fold_addr_expr (first);
5077 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
5078 ptrdiff_type_node, t);
5079 tree ptr = OMP_CLAUSE_DECL (c2);
5080 ptr = convert_from_reference (ptr);
5081 if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
5082 ptr = build_fold_addr_expr (ptr);
5083 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
5084 ptrdiff_type_node, t,
5085 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
5086 ptrdiff_type_node, ptr));
5087 OMP_CLAUSE_SIZE (c2) = t;
5088 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
5089 OMP_CLAUSE_CHAIN (c) = c2;
5090 ptr = OMP_CLAUSE_DECL (c2);
5091 if (OMP_CLAUSE_MAP_KIND (c2) != GOMP_MAP_FIRSTPRIVATE_POINTER
5092 && TREE_CODE (TREE_TYPE (ptr)) == REFERENCE_TYPE
5093 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (ptr))))
5095 tree c3 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
5096 OMP_CLAUSE_MAP);
5097 OMP_CLAUSE_SET_MAP_KIND (c3, OMP_CLAUSE_MAP_KIND (c2));
5098 OMP_CLAUSE_DECL (c3) = ptr;
5099 if (OMP_CLAUSE_MAP_KIND (c2) == GOMP_MAP_ALWAYS_POINTER)
5100 OMP_CLAUSE_DECL (c2) = build_simple_mem_ref (ptr);
5101 else
5102 OMP_CLAUSE_DECL (c2) = convert_from_reference (ptr);
5103 OMP_CLAUSE_SIZE (c3) = size_zero_node;
5104 OMP_CLAUSE_CHAIN (c3) = OMP_CLAUSE_CHAIN (c2);
5105 OMP_CLAUSE_CHAIN (c2) = c3;
5109 return false;
5112 /* Return identifier to look up for omp declare reduction. */
5114 tree
5115 omp_reduction_id (enum tree_code reduction_code, tree reduction_id, tree type)
5117 const char *p = NULL;
5118 const char *m = NULL;
5119 switch (reduction_code)
5121 case PLUS_EXPR:
5122 case MULT_EXPR:
5123 case MINUS_EXPR:
5124 case BIT_AND_EXPR:
5125 case BIT_XOR_EXPR:
5126 case BIT_IOR_EXPR:
5127 case TRUTH_ANDIF_EXPR:
5128 case TRUTH_ORIF_EXPR:
5129 reduction_id = cp_operator_id (reduction_code);
5130 break;
5131 case MIN_EXPR:
5132 p = "min";
5133 break;
5134 case MAX_EXPR:
5135 p = "max";
5136 break;
5137 default:
5138 break;
5141 if (p == NULL)
5143 if (TREE_CODE (reduction_id) != IDENTIFIER_NODE)
5144 return error_mark_node;
5145 p = IDENTIFIER_POINTER (reduction_id);
5148 if (type != NULL_TREE)
5149 m = mangle_type_string (TYPE_MAIN_VARIANT (type));
5151 const char prefix[] = "omp declare reduction ";
5152 size_t lenp = sizeof (prefix);
5153 if (strncmp (p, prefix, lenp - 1) == 0)
5154 lenp = 1;
5155 size_t len = strlen (p);
5156 size_t lenm = m ? strlen (m) + 1 : 0;
5157 char *name = XALLOCAVEC (char, lenp + len + lenm);
5158 if (lenp > 1)
5159 memcpy (name, prefix, lenp - 1);
5160 memcpy (name + lenp - 1, p, len + 1);
5161 if (m)
5163 name[lenp + len - 1] = '~';
5164 memcpy (name + lenp + len, m, lenm);
5166 return get_identifier (name);
5169 /* Lookup OpenMP UDR ID for TYPE, return the corresponding artificial
5170 FUNCTION_DECL or NULL_TREE if not found. */
5172 static tree
5173 omp_reduction_lookup (location_t loc, tree id, tree type, tree *baselinkp,
5174 vec<tree> *ambiguousp)
5176 tree orig_id = id;
5177 tree baselink = NULL_TREE;
5178 if (identifier_p (id))
5180 cp_id_kind idk;
5181 bool nonint_cst_expression_p;
5182 const char *error_msg;
5183 id = omp_reduction_id (ERROR_MARK, id, type);
5184 tree decl = lookup_name (id);
5185 if (decl == NULL_TREE)
5186 decl = error_mark_node;
5187 id = finish_id_expression (id, decl, NULL_TREE, &idk, false, true,
5188 &nonint_cst_expression_p, false, true, false,
5189 false, &error_msg, loc);
5190 if (idk == CP_ID_KIND_UNQUALIFIED
5191 && identifier_p (id))
5193 vec<tree, va_gc> *args = NULL;
5194 vec_safe_push (args, build_reference_type (type));
5195 id = perform_koenig_lookup (id, args, tf_none);
5198 else if (TREE_CODE (id) == SCOPE_REF)
5199 id = lookup_qualified_name (TREE_OPERAND (id, 0),
5200 omp_reduction_id (ERROR_MARK,
5201 TREE_OPERAND (id, 1),
5202 type),
5203 false, false);
5204 tree fns = id;
5205 id = NULL_TREE;
5206 if (fns && is_overloaded_fn (fns))
5208 for (lkp_iterator iter (get_fns (fns)); iter; ++iter)
5210 tree fndecl = *iter;
5211 if (TREE_CODE (fndecl) == FUNCTION_DECL)
5213 tree argtype = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
5214 if (same_type_p (TREE_TYPE (argtype), type))
5216 id = fndecl;
5217 break;
5222 if (id && BASELINK_P (fns))
5224 if (baselinkp)
5225 *baselinkp = fns;
5226 else
5227 baselink = fns;
5231 if (!id && CLASS_TYPE_P (type) && TYPE_BINFO (type))
5233 vec<tree> ambiguous = vNULL;
5234 tree binfo = TYPE_BINFO (type), base_binfo, ret = NULL_TREE;
5235 unsigned int ix;
5236 if (ambiguousp == NULL)
5237 ambiguousp = &ambiguous;
5238 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
5240 id = omp_reduction_lookup (loc, orig_id, BINFO_TYPE (base_binfo),
5241 baselinkp ? baselinkp : &baselink,
5242 ambiguousp);
5243 if (id == NULL_TREE)
5244 continue;
5245 if (!ambiguousp->is_empty ())
5246 ambiguousp->safe_push (id);
5247 else if (ret != NULL_TREE)
5249 ambiguousp->safe_push (ret);
5250 ambiguousp->safe_push (id);
5251 ret = NULL_TREE;
5253 else
5254 ret = id;
5256 if (ambiguousp != &ambiguous)
5257 return ret;
5258 if (!ambiguous.is_empty ())
5260 const char *str = _("candidates are:");
5261 unsigned int idx;
5262 tree udr;
5263 error_at (loc, "user defined reduction lookup is ambiguous");
5264 FOR_EACH_VEC_ELT (ambiguous, idx, udr)
5266 inform (DECL_SOURCE_LOCATION (udr), "%s %#qD", str, udr);
5267 if (idx == 0)
5268 str = get_spaces (str);
5270 ambiguous.release ();
5271 ret = error_mark_node;
5272 baselink = NULL_TREE;
5274 id = ret;
5276 if (id && baselink)
5277 perform_or_defer_access_check (BASELINK_BINFO (baselink),
5278 id, id, tf_warning_or_error);
5279 return id;
5282 /* Helper function for cp_parser_omp_declare_reduction_exprs
5283 and tsubst_omp_udr.
5284 Remove CLEANUP_STMT for data (omp_priv variable).
5285 Also append INIT_EXPR for DECL_INITIAL of omp_priv after its
5286 DECL_EXPR. */
5288 tree
5289 cp_remove_omp_priv_cleanup_stmt (tree *tp, int *walk_subtrees, void *data)
5291 if (TYPE_P (*tp))
5292 *walk_subtrees = 0;
5293 else if (TREE_CODE (*tp) == CLEANUP_STMT && CLEANUP_DECL (*tp) == (tree) data)
5294 *tp = CLEANUP_BODY (*tp);
5295 else if (TREE_CODE (*tp) == DECL_EXPR)
5297 tree decl = DECL_EXPR_DECL (*tp);
5298 if (!processing_template_decl
5299 && decl == (tree) data
5300 && DECL_INITIAL (decl)
5301 && DECL_INITIAL (decl) != error_mark_node)
5303 tree list = NULL_TREE;
5304 append_to_statement_list_force (*tp, &list);
5305 tree init_expr = build2 (INIT_EXPR, void_type_node,
5306 decl, DECL_INITIAL (decl));
5307 DECL_INITIAL (decl) = NULL_TREE;
5308 append_to_statement_list_force (init_expr, &list);
5309 *tp = list;
5312 return NULL_TREE;
5315 /* Data passed from cp_check_omp_declare_reduction to
5316 cp_check_omp_declare_reduction_r. */
5318 struct cp_check_omp_declare_reduction_data
5320 location_t loc;
5321 tree stmts[7];
5322 bool combiner_p;
5325 /* Helper function for cp_check_omp_declare_reduction, called via
5326 cp_walk_tree. */
5328 static tree
5329 cp_check_omp_declare_reduction_r (tree *tp, int *, void *data)
5331 struct cp_check_omp_declare_reduction_data *udr_data
5332 = (struct cp_check_omp_declare_reduction_data *) data;
5333 if (SSA_VAR_P (*tp)
5334 && !DECL_ARTIFICIAL (*tp)
5335 && *tp != DECL_EXPR_DECL (udr_data->stmts[udr_data->combiner_p ? 0 : 3])
5336 && *tp != DECL_EXPR_DECL (udr_data->stmts[udr_data->combiner_p ? 1 : 4]))
5338 location_t loc = udr_data->loc;
5339 if (udr_data->combiner_p)
5340 error_at (loc, "%<#pragma omp declare reduction%> combiner refers to "
5341 "variable %qD which is not %<omp_out%> nor %<omp_in%>",
5342 *tp);
5343 else
5344 error_at (loc, "%<#pragma omp declare reduction%> initializer refers "
5345 "to variable %qD which is not %<omp_priv%> nor "
5346 "%<omp_orig%>",
5347 *tp);
5348 return *tp;
5350 return NULL_TREE;
5353 /* Diagnose violation of OpenMP #pragma omp declare reduction restrictions. */
5355 void
5356 cp_check_omp_declare_reduction (tree udr)
5358 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (udr)));
5359 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
5360 type = TREE_TYPE (type);
5361 int i;
5362 location_t loc = DECL_SOURCE_LOCATION (udr);
5364 if (type == error_mark_node)
5365 return;
5366 if (ARITHMETIC_TYPE_P (type))
5368 static enum tree_code predef_codes[]
5369 = { PLUS_EXPR, MULT_EXPR, MINUS_EXPR, BIT_AND_EXPR, BIT_XOR_EXPR,
5370 BIT_IOR_EXPR, TRUTH_ANDIF_EXPR, TRUTH_ORIF_EXPR };
5371 for (i = 0; i < 8; i++)
5373 tree id = omp_reduction_id (predef_codes[i], NULL_TREE, NULL_TREE);
5374 const char *n1 = IDENTIFIER_POINTER (DECL_NAME (udr));
5375 const char *n2 = IDENTIFIER_POINTER (id);
5376 if (strncmp (n1, n2, IDENTIFIER_LENGTH (id)) == 0
5377 && (n1[IDENTIFIER_LENGTH (id)] == '~'
5378 || n1[IDENTIFIER_LENGTH (id)] == '\0'))
5379 break;
5382 if (i == 8
5383 && TREE_CODE (type) != COMPLEX_EXPR)
5385 const char prefix_minmax[] = "omp declare reduction m";
5386 size_t prefix_size = sizeof (prefix_minmax) - 1;
5387 const char *n = IDENTIFIER_POINTER (DECL_NAME (udr));
5388 if (strncmp (IDENTIFIER_POINTER (DECL_NAME (udr)),
5389 prefix_minmax, prefix_size) == 0
5390 && ((n[prefix_size] == 'i' && n[prefix_size + 1] == 'n')
5391 || (n[prefix_size] == 'a' && n[prefix_size + 1] == 'x'))
5392 && (n[prefix_size + 2] == '~' || n[prefix_size + 2] == '\0'))
5393 i = 0;
5395 if (i < 8)
5397 error_at (loc, "predeclared arithmetic type %qT in "
5398 "%<#pragma omp declare reduction%>", type);
5399 return;
5402 else if (TREE_CODE (type) == FUNCTION_TYPE
5403 || TREE_CODE (type) == METHOD_TYPE
5404 || TREE_CODE (type) == ARRAY_TYPE)
5406 error_at (loc, "function or array type %qT in "
5407 "%<#pragma omp declare reduction%>", type);
5408 return;
5410 else if (TREE_CODE (type) == REFERENCE_TYPE)
5412 error_at (loc, "reference type %qT in %<#pragma omp declare reduction%>",
5413 type);
5414 return;
5416 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
5418 error_at (loc, "const, volatile or __restrict qualified type %qT in "
5419 "%<#pragma omp declare reduction%>", type);
5420 return;
5423 tree body = DECL_SAVED_TREE (udr);
5424 if (body == NULL_TREE || TREE_CODE (body) != STATEMENT_LIST)
5425 return;
5427 tree_stmt_iterator tsi;
5428 struct cp_check_omp_declare_reduction_data data;
5429 memset (data.stmts, 0, sizeof data.stmts);
5430 for (i = 0, tsi = tsi_start (body);
5431 i < 7 && !tsi_end_p (tsi);
5432 i++, tsi_next (&tsi))
5433 data.stmts[i] = tsi_stmt (tsi);
5434 data.loc = loc;
5435 gcc_assert (tsi_end_p (tsi));
5436 if (i >= 3)
5438 gcc_assert (TREE_CODE (data.stmts[0]) == DECL_EXPR
5439 && TREE_CODE (data.stmts[1]) == DECL_EXPR);
5440 if (TREE_NO_WARNING (DECL_EXPR_DECL (data.stmts[0])))
5441 return;
5442 data.combiner_p = true;
5443 if (cp_walk_tree (&data.stmts[2], cp_check_omp_declare_reduction_r,
5444 &data, NULL))
5445 TREE_NO_WARNING (DECL_EXPR_DECL (data.stmts[0])) = 1;
5447 if (i >= 6)
5449 gcc_assert (TREE_CODE (data.stmts[3]) == DECL_EXPR
5450 && TREE_CODE (data.stmts[4]) == DECL_EXPR);
5451 data.combiner_p = false;
5452 if (cp_walk_tree (&data.stmts[5], cp_check_omp_declare_reduction_r,
5453 &data, NULL)
5454 || cp_walk_tree (&DECL_INITIAL (DECL_EXPR_DECL (data.stmts[3])),
5455 cp_check_omp_declare_reduction_r, &data, NULL))
5456 TREE_NO_WARNING (DECL_EXPR_DECL (data.stmts[0])) = 1;
5457 if (i == 7)
5458 gcc_assert (TREE_CODE (data.stmts[6]) == DECL_EXPR);
5462 /* Helper function of finish_omp_clauses. Clone STMT as if we were making
5463 an inline call. But, remap
5464 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
5465 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
5467 static tree
5468 clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
5469 tree decl, tree placeholder)
5471 copy_body_data id;
5472 hash_map<tree, tree> decl_map;
5474 decl_map.put (omp_decl1, placeholder);
5475 decl_map.put (omp_decl2, decl);
5476 memset (&id, 0, sizeof (id));
5477 id.src_fn = DECL_CONTEXT (omp_decl1);
5478 id.dst_fn = current_function_decl;
5479 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
5480 id.decl_map = &decl_map;
5482 id.copy_decl = copy_decl_no_change;
5483 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
5484 id.transform_new_cfg = true;
5485 id.transform_return_to_modify = false;
5486 id.transform_lang_insert_block = NULL;
5487 id.eh_lp_nr = 0;
5488 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
5489 return stmt;
5492 /* Helper function of finish_omp_clauses, called via cp_walk_tree.
5493 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
5495 static tree
5496 find_omp_placeholder_r (tree *tp, int *, void *data)
5498 if (*tp == (tree) data)
5499 return *tp;
5500 return NULL_TREE;
5503 /* Helper function of finish_omp_clauses. Handle OMP_CLAUSE_REDUCTION C.
5504 Return true if there is some error and the clause should be removed. */
5506 static bool
5507 finish_omp_reduction_clause (tree c, bool *need_default_ctor, bool *need_dtor)
5509 tree t = OMP_CLAUSE_DECL (c);
5510 bool predefined = false;
5511 if (TREE_CODE (t) == TREE_LIST)
5513 gcc_assert (processing_template_decl);
5514 return false;
5516 tree type = TREE_TYPE (t);
5517 if (TREE_CODE (t) == MEM_REF)
5518 type = TREE_TYPE (type);
5519 if (TREE_CODE (type) == REFERENCE_TYPE)
5520 type = TREE_TYPE (type);
5521 if (TREE_CODE (type) == ARRAY_TYPE)
5523 tree oatype = type;
5524 gcc_assert (TREE_CODE (t) != MEM_REF);
5525 while (TREE_CODE (type) == ARRAY_TYPE)
5526 type = TREE_TYPE (type);
5527 if (!processing_template_decl)
5529 t = require_complete_type (t);
5530 if (t == error_mark_node)
5531 return true;
5532 tree size = size_binop (EXACT_DIV_EXPR, TYPE_SIZE_UNIT (oatype),
5533 TYPE_SIZE_UNIT (type));
5534 if (integer_zerop (size))
5536 error ("%qE in %<reduction%> clause is a zero size array",
5537 omp_clause_printable_decl (t));
5538 return true;
5540 size = size_binop (MINUS_EXPR, size, size_one_node);
5541 tree index_type = build_index_type (size);
5542 tree atype = build_array_type (type, index_type);
5543 tree ptype = build_pointer_type (type);
5544 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
5545 t = build_fold_addr_expr (t);
5546 t = build2 (MEM_REF, atype, t, build_int_cst (ptype, 0));
5547 OMP_CLAUSE_DECL (c) = t;
5550 if (type == error_mark_node)
5551 return true;
5552 else if (ARITHMETIC_TYPE_P (type))
5553 switch (OMP_CLAUSE_REDUCTION_CODE (c))
5555 case PLUS_EXPR:
5556 case MULT_EXPR:
5557 case MINUS_EXPR:
5558 predefined = true;
5559 break;
5560 case MIN_EXPR:
5561 case MAX_EXPR:
5562 if (TREE_CODE (type) == COMPLEX_TYPE)
5563 break;
5564 predefined = true;
5565 break;
5566 case BIT_AND_EXPR:
5567 case BIT_IOR_EXPR:
5568 case BIT_XOR_EXPR:
5569 if (FLOAT_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
5570 break;
5571 predefined = true;
5572 break;
5573 case TRUTH_ANDIF_EXPR:
5574 case TRUTH_ORIF_EXPR:
5575 if (FLOAT_TYPE_P (type))
5576 break;
5577 predefined = true;
5578 break;
5579 default:
5580 break;
5582 else if (TYPE_READONLY (type))
5584 error ("%qE has const type for %<reduction%>",
5585 omp_clause_printable_decl (t));
5586 return true;
5588 else if (!processing_template_decl)
5590 t = require_complete_type (t);
5591 if (t == error_mark_node)
5592 return true;
5593 OMP_CLAUSE_DECL (c) = t;
5596 if (predefined)
5598 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = NULL_TREE;
5599 return false;
5601 else if (processing_template_decl)
5602 return false;
5604 tree id = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
5606 type = TYPE_MAIN_VARIANT (type);
5607 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = NULL_TREE;
5608 if (id == NULL_TREE)
5609 id = omp_reduction_id (OMP_CLAUSE_REDUCTION_CODE (c),
5610 NULL_TREE, NULL_TREE);
5611 id = omp_reduction_lookup (OMP_CLAUSE_LOCATION (c), id, type, NULL, NULL);
5612 if (id)
5614 if (id == error_mark_node)
5615 return true;
5616 mark_used (id);
5617 tree body = DECL_SAVED_TREE (id);
5618 if (!body)
5619 return true;
5620 if (TREE_CODE (body) == STATEMENT_LIST)
5622 tree_stmt_iterator tsi;
5623 tree placeholder = NULL_TREE, decl_placeholder = NULL_TREE;
5624 int i;
5625 tree stmts[7];
5626 tree atype = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (id)));
5627 atype = TREE_TYPE (atype);
5628 bool need_static_cast = !same_type_p (type, atype);
5629 memset (stmts, 0, sizeof stmts);
5630 for (i = 0, tsi = tsi_start (body);
5631 i < 7 && !tsi_end_p (tsi);
5632 i++, tsi_next (&tsi))
5633 stmts[i] = tsi_stmt (tsi);
5634 gcc_assert (tsi_end_p (tsi));
5636 if (i >= 3)
5638 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
5639 && TREE_CODE (stmts[1]) == DECL_EXPR);
5640 placeholder = build_lang_decl (VAR_DECL, NULL_TREE, type);
5641 DECL_ARTIFICIAL (placeholder) = 1;
5642 DECL_IGNORED_P (placeholder) = 1;
5643 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
5644 if (TREE_CODE (t) == MEM_REF)
5646 decl_placeholder = build_lang_decl (VAR_DECL, NULL_TREE,
5647 type);
5648 DECL_ARTIFICIAL (decl_placeholder) = 1;
5649 DECL_IGNORED_P (decl_placeholder) = 1;
5650 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c) = decl_placeholder;
5652 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[0])))
5653 cxx_mark_addressable (placeholder);
5654 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[1]))
5655 && TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c)))
5656 != REFERENCE_TYPE)
5657 cxx_mark_addressable (decl_placeholder ? decl_placeholder
5658 : OMP_CLAUSE_DECL (c));
5659 tree omp_out = placeholder;
5660 tree omp_in = decl_placeholder ? decl_placeholder
5661 : convert_from_reference (OMP_CLAUSE_DECL (c));
5662 if (need_static_cast)
5664 tree rtype = build_reference_type (atype);
5665 omp_out = build_static_cast (rtype, omp_out,
5666 tf_warning_or_error);
5667 omp_in = build_static_cast (rtype, omp_in,
5668 tf_warning_or_error);
5669 if (omp_out == error_mark_node || omp_in == error_mark_node)
5670 return true;
5671 omp_out = convert_from_reference (omp_out);
5672 omp_in = convert_from_reference (omp_in);
5674 OMP_CLAUSE_REDUCTION_MERGE (c)
5675 = clone_omp_udr (stmts[2], DECL_EXPR_DECL (stmts[0]),
5676 DECL_EXPR_DECL (stmts[1]), omp_in, omp_out);
5678 if (i >= 6)
5680 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
5681 && TREE_CODE (stmts[4]) == DECL_EXPR);
5682 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[3])))
5683 cxx_mark_addressable (decl_placeholder ? decl_placeholder
5684 : OMP_CLAUSE_DECL (c));
5685 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[4])))
5686 cxx_mark_addressable (placeholder);
5687 tree omp_priv = decl_placeholder ? decl_placeholder
5688 : convert_from_reference (OMP_CLAUSE_DECL (c));
5689 tree omp_orig = placeholder;
5690 if (need_static_cast)
5692 if (i == 7)
5694 error_at (OMP_CLAUSE_LOCATION (c),
5695 "user defined reduction with constructor "
5696 "initializer for base class %qT", atype);
5697 return true;
5699 tree rtype = build_reference_type (atype);
5700 omp_priv = build_static_cast (rtype, omp_priv,
5701 tf_warning_or_error);
5702 omp_orig = build_static_cast (rtype, omp_orig,
5703 tf_warning_or_error);
5704 if (omp_priv == error_mark_node
5705 || omp_orig == error_mark_node)
5706 return true;
5707 omp_priv = convert_from_reference (omp_priv);
5708 omp_orig = convert_from_reference (omp_orig);
5710 if (i == 6)
5711 *need_default_ctor = true;
5712 OMP_CLAUSE_REDUCTION_INIT (c)
5713 = clone_omp_udr (stmts[5], DECL_EXPR_DECL (stmts[4]),
5714 DECL_EXPR_DECL (stmts[3]),
5715 omp_priv, omp_orig);
5716 if (cp_walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
5717 find_omp_placeholder_r, placeholder, NULL))
5718 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
5720 else if (i >= 3)
5722 if (CLASS_TYPE_P (type) && !pod_type_p (type))
5723 *need_default_ctor = true;
5724 else
5726 tree init;
5727 tree v = decl_placeholder ? decl_placeholder
5728 : convert_from_reference (t);
5729 if (AGGREGATE_TYPE_P (TREE_TYPE (v)))
5730 init = build_constructor (TREE_TYPE (v), NULL);
5731 else
5732 init = fold_convert (TREE_TYPE (v), integer_zero_node);
5733 OMP_CLAUSE_REDUCTION_INIT (c)
5734 = build2 (INIT_EXPR, TREE_TYPE (v), v, init);
5739 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
5740 *need_dtor = true;
5741 else
5743 error ("user defined reduction not found for %qE",
5744 omp_clause_printable_decl (t));
5745 return true;
5747 if (TREE_CODE (OMP_CLAUSE_DECL (c)) == MEM_REF)
5748 gcc_assert (TYPE_SIZE_UNIT (type)
5749 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST);
5750 return false;
5753 /* Called from finish_struct_1. linear(this) or linear(this:step)
5754 clauses might not be finalized yet because the class has been incomplete
5755 when parsing #pragma omp declare simd methods. Fix those up now. */
5757 void
5758 finish_omp_declare_simd_methods (tree t)
5760 if (processing_template_decl)
5761 return;
5763 for (tree x = TYPE_METHODS (t); x; x = DECL_CHAIN (x))
5765 if (TREE_CODE (TREE_TYPE (x)) != METHOD_TYPE)
5766 continue;
5767 tree ods = lookup_attribute ("omp declare simd", DECL_ATTRIBUTES (x));
5768 if (!ods || !TREE_VALUE (ods))
5769 continue;
5770 for (tree c = TREE_VALUE (TREE_VALUE (ods)); c; c = OMP_CLAUSE_CHAIN (c))
5771 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
5772 && integer_zerop (OMP_CLAUSE_DECL (c))
5773 && OMP_CLAUSE_LINEAR_STEP (c)
5774 && TREE_CODE (TREE_TYPE (OMP_CLAUSE_LINEAR_STEP (c)))
5775 == POINTER_TYPE)
5777 tree s = OMP_CLAUSE_LINEAR_STEP (c);
5778 s = fold_convert_loc (OMP_CLAUSE_LOCATION (c), sizetype, s);
5779 s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MULT_EXPR,
5780 sizetype, s, TYPE_SIZE_UNIT (t));
5781 OMP_CLAUSE_LINEAR_STEP (c) = s;
5786 /* Adjust sink depend clause to take into account pointer offsets.
5788 Return TRUE if there was a problem processing the offset, and the
5789 whole clause should be removed. */
5791 static bool
5792 cp_finish_omp_clause_depend_sink (tree sink_clause)
5794 tree t = OMP_CLAUSE_DECL (sink_clause);
5795 gcc_assert (TREE_CODE (t) == TREE_LIST);
5797 /* Make sure we don't adjust things twice for templates. */
5798 if (processing_template_decl)
5799 return false;
5801 for (; t; t = TREE_CHAIN (t))
5803 tree decl = TREE_VALUE (t);
5804 if (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
5806 tree offset = TREE_PURPOSE (t);
5807 bool neg = wi::neg_p ((wide_int) offset);
5808 offset = fold_unary (ABS_EXPR, TREE_TYPE (offset), offset);
5809 decl = mark_rvalue_use (decl);
5810 decl = convert_from_reference (decl);
5811 tree t2 = pointer_int_sum (OMP_CLAUSE_LOCATION (sink_clause),
5812 neg ? MINUS_EXPR : PLUS_EXPR,
5813 decl, offset);
5814 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (sink_clause),
5815 MINUS_EXPR, sizetype,
5816 fold_convert (sizetype, t2),
5817 fold_convert (sizetype, decl));
5818 if (t2 == error_mark_node)
5819 return true;
5820 TREE_PURPOSE (t) = t2;
5823 return false;
5826 /* For all elements of CLAUSES, validate them vs OpenMP constraints.
5827 Remove any elements from the list that are invalid. */
5829 tree
5830 finish_omp_clauses (tree clauses, enum c_omp_region_type ort)
5832 bitmap_head generic_head, firstprivate_head, lastprivate_head;
5833 bitmap_head aligned_head, map_head, map_field_head, oacc_reduction_head;
5834 tree c, t, *pc;
5835 tree safelen = NULL_TREE;
5836 bool branch_seen = false;
5837 bool copyprivate_seen = false;
5838 bool ordered_seen = false;
5839 bool oacc_async = false;
5841 bitmap_obstack_initialize (NULL);
5842 bitmap_initialize (&generic_head, &bitmap_default_obstack);
5843 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
5844 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
5845 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
5846 bitmap_initialize (&map_head, &bitmap_default_obstack);
5847 bitmap_initialize (&map_field_head, &bitmap_default_obstack);
5848 bitmap_initialize (&oacc_reduction_head, &bitmap_default_obstack);
5850 if (ort & C_ORT_ACC)
5851 for (c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
5852 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ASYNC)
5854 oacc_async = true;
5855 break;
5858 for (pc = &clauses, c = clauses; c ; c = *pc)
5860 bool remove = false;
5861 bool field_ok = false;
5863 switch (OMP_CLAUSE_CODE (c))
5865 case OMP_CLAUSE_SHARED:
5866 field_ok = ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP);
5867 goto check_dup_generic;
5868 case OMP_CLAUSE_PRIVATE:
5869 field_ok = ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP);
5870 goto check_dup_generic;
5871 case OMP_CLAUSE_REDUCTION:
5872 field_ok = ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP);
5873 t = OMP_CLAUSE_DECL (c);
5874 if (TREE_CODE (t) == TREE_LIST)
5876 if (handle_omp_array_sections (c, ort))
5878 remove = true;
5879 break;
5881 if (TREE_CODE (t) == TREE_LIST)
5883 while (TREE_CODE (t) == TREE_LIST)
5884 t = TREE_CHAIN (t);
5886 else
5888 gcc_assert (TREE_CODE (t) == MEM_REF);
5889 t = TREE_OPERAND (t, 0);
5890 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
5891 t = TREE_OPERAND (t, 0);
5892 if (TREE_CODE (t) == ADDR_EXPR
5893 || TREE_CODE (t) == INDIRECT_REF)
5894 t = TREE_OPERAND (t, 0);
5896 tree n = omp_clause_decl_field (t);
5897 if (n)
5898 t = n;
5899 goto check_dup_generic_t;
5901 if (oacc_async)
5902 cxx_mark_addressable (t);
5903 goto check_dup_generic;
5904 case OMP_CLAUSE_COPYPRIVATE:
5905 copyprivate_seen = true;
5906 field_ok = ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP);
5907 goto check_dup_generic;
5908 case OMP_CLAUSE_COPYIN:
5909 goto check_dup_generic;
5910 case OMP_CLAUSE_LINEAR:
5911 field_ok = ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP);
5912 t = OMP_CLAUSE_DECL (c);
5913 if (ort != C_ORT_OMP_DECLARE_SIMD
5914 && OMP_CLAUSE_LINEAR_KIND (c) != OMP_CLAUSE_LINEAR_DEFAULT)
5916 error_at (OMP_CLAUSE_LOCATION (c),
5917 "modifier should not be specified in %<linear%> "
5918 "clause on %<simd%> or %<for%> constructs");
5919 OMP_CLAUSE_LINEAR_KIND (c) = OMP_CLAUSE_LINEAR_DEFAULT;
5921 if ((VAR_P (t) || TREE_CODE (t) == PARM_DECL)
5922 && !type_dependent_expression_p (t))
5924 tree type = TREE_TYPE (t);
5925 if ((OMP_CLAUSE_LINEAR_KIND (c) == OMP_CLAUSE_LINEAR_REF
5926 || OMP_CLAUSE_LINEAR_KIND (c) == OMP_CLAUSE_LINEAR_UVAL)
5927 && TREE_CODE (type) != REFERENCE_TYPE)
5929 error ("linear clause with %qs modifier applied to "
5930 "non-reference variable with %qT type",
5931 OMP_CLAUSE_LINEAR_KIND (c) == OMP_CLAUSE_LINEAR_REF
5932 ? "ref" : "uval", TREE_TYPE (t));
5933 remove = true;
5934 break;
5936 if (TREE_CODE (type) == REFERENCE_TYPE)
5937 type = TREE_TYPE (type);
5938 if (ort == C_ORT_CILK)
5940 if (!INTEGRAL_TYPE_P (type)
5941 && !SCALAR_FLOAT_TYPE_P (type)
5942 && TREE_CODE (type) != POINTER_TYPE)
5944 error ("linear clause applied to non-integral, "
5945 "non-floating, non-pointer variable with %qT type",
5946 TREE_TYPE (t));
5947 remove = true;
5948 break;
5951 else if (OMP_CLAUSE_LINEAR_KIND (c) != OMP_CLAUSE_LINEAR_REF)
5953 if (!INTEGRAL_TYPE_P (type)
5954 && TREE_CODE (type) != POINTER_TYPE)
5956 error ("linear clause applied to non-integral non-pointer"
5957 " variable with %qT type", TREE_TYPE (t));
5958 remove = true;
5959 break;
5963 t = OMP_CLAUSE_LINEAR_STEP (c);
5964 if (t == NULL_TREE)
5965 t = integer_one_node;
5966 if (t == error_mark_node)
5968 remove = true;
5969 break;
5971 else if (!type_dependent_expression_p (t)
5972 && !INTEGRAL_TYPE_P (TREE_TYPE (t))
5973 && (ort != C_ORT_OMP_DECLARE_SIMD
5974 || TREE_CODE (t) != PARM_DECL
5975 || TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE
5976 || !INTEGRAL_TYPE_P (TREE_TYPE (TREE_TYPE (t)))))
5978 error ("linear step expression must be integral");
5979 remove = true;
5980 break;
5982 else
5984 t = mark_rvalue_use (t);
5985 if (ort == C_ORT_OMP_DECLARE_SIMD && TREE_CODE (t) == PARM_DECL)
5987 OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c) = 1;
5988 goto check_dup_generic;
5990 if (!processing_template_decl
5991 && (VAR_P (OMP_CLAUSE_DECL (c))
5992 || TREE_CODE (OMP_CLAUSE_DECL (c)) == PARM_DECL))
5994 if (ort == C_ORT_OMP_DECLARE_SIMD)
5996 t = maybe_constant_value (t);
5997 if (TREE_CODE (t) != INTEGER_CST)
5999 error_at (OMP_CLAUSE_LOCATION (c),
6000 "%<linear%> clause step %qE is neither "
6001 "constant nor a parameter", t);
6002 remove = true;
6003 break;
6006 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6007 tree type = TREE_TYPE (OMP_CLAUSE_DECL (c));
6008 if (TREE_CODE (type) == REFERENCE_TYPE)
6009 type = TREE_TYPE (type);
6010 if (OMP_CLAUSE_LINEAR_KIND (c) == OMP_CLAUSE_LINEAR_REF)
6012 type = build_pointer_type (type);
6013 tree d = fold_convert (type, OMP_CLAUSE_DECL (c));
6014 t = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
6015 d, t);
6016 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c),
6017 MINUS_EXPR, sizetype,
6018 fold_convert (sizetype, t),
6019 fold_convert (sizetype, d));
6020 if (t == error_mark_node)
6022 remove = true;
6023 break;
6026 else if (TREE_CODE (type) == POINTER_TYPE
6027 /* Can't multiply the step yet if *this
6028 is still incomplete type. */
6029 && (ort != C_ORT_OMP_DECLARE_SIMD
6030 || TREE_CODE (OMP_CLAUSE_DECL (c)) != PARM_DECL
6031 || !DECL_ARTIFICIAL (OMP_CLAUSE_DECL (c))
6032 || DECL_NAME (OMP_CLAUSE_DECL (c))
6033 != this_identifier
6034 || !TYPE_BEING_DEFINED (TREE_TYPE (type))))
6036 tree d = convert_from_reference (OMP_CLAUSE_DECL (c));
6037 t = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
6038 d, t);
6039 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c),
6040 MINUS_EXPR, sizetype,
6041 fold_convert (sizetype, t),
6042 fold_convert (sizetype, d));
6043 if (t == error_mark_node)
6045 remove = true;
6046 break;
6049 else
6050 t = fold_convert (type, t);
6052 OMP_CLAUSE_LINEAR_STEP (c) = t;
6054 goto check_dup_generic;
6055 check_dup_generic:
6056 t = omp_clause_decl_field (OMP_CLAUSE_DECL (c));
6057 if (t)
6059 if (!remove && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_SHARED)
6060 omp_note_field_privatization (t, OMP_CLAUSE_DECL (c));
6062 else
6063 t = OMP_CLAUSE_DECL (c);
6064 check_dup_generic_t:
6065 if (t == current_class_ptr
6066 && (ort != C_ORT_OMP_DECLARE_SIMD
6067 || (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_LINEAR
6068 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_UNIFORM)))
6070 error ("%<this%> allowed in OpenMP only in %<declare simd%>"
6071 " clauses");
6072 remove = true;
6073 break;
6075 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL
6076 && (!field_ok || TREE_CODE (t) != FIELD_DECL))
6078 if (processing_template_decl)
6079 break;
6080 if (DECL_P (t))
6081 error ("%qD is not a variable in clause %qs", t,
6082 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6083 else
6084 error ("%qE is not a variable in clause %qs", t,
6085 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6086 remove = true;
6088 else if (ort == C_ORT_ACC
6089 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
6091 if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
6093 error ("%qD appears more than once in reduction clauses", t);
6094 remove = true;
6096 else
6097 bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
6099 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
6100 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
6101 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
6103 error ("%qD appears more than once in data clauses", t);
6104 remove = true;
6106 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
6107 && bitmap_bit_p (&map_head, DECL_UID (t)))
6109 if (ort == C_ORT_ACC)
6110 error ("%qD appears more than once in data clauses", t);
6111 else
6112 error ("%qD appears both in data and map clauses", t);
6113 remove = true;
6115 else
6116 bitmap_set_bit (&generic_head, DECL_UID (t));
6117 if (!field_ok)
6118 break;
6119 handle_field_decl:
6120 if (!remove
6121 && TREE_CODE (t) == FIELD_DECL
6122 && t == OMP_CLAUSE_DECL (c)
6123 && ort != C_ORT_ACC)
6125 OMP_CLAUSE_DECL (c)
6126 = omp_privatize_field (t, (OMP_CLAUSE_CODE (c)
6127 == OMP_CLAUSE_SHARED));
6128 if (OMP_CLAUSE_DECL (c) == error_mark_node)
6129 remove = true;
6131 break;
6133 case OMP_CLAUSE_FIRSTPRIVATE:
6134 t = omp_clause_decl_field (OMP_CLAUSE_DECL (c));
6135 if (t)
6136 omp_note_field_privatization (t, OMP_CLAUSE_DECL (c));
6137 else
6138 t = OMP_CLAUSE_DECL (c);
6139 if (ort != C_ORT_ACC && t == current_class_ptr)
6141 error ("%<this%> allowed in OpenMP only in %<declare simd%>"
6142 " clauses");
6143 remove = true;
6144 break;
6146 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL
6147 && ((ort & C_ORT_OMP_DECLARE_SIMD) != C_ORT_OMP
6148 || TREE_CODE (t) != FIELD_DECL))
6150 if (processing_template_decl)
6151 break;
6152 if (DECL_P (t))
6153 error ("%qD is not a variable in clause %<firstprivate%>", t);
6154 else
6155 error ("%qE is not a variable in clause %<firstprivate%>", t);
6156 remove = true;
6158 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
6159 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
6161 error ("%qD appears more than once in data clauses", t);
6162 remove = true;
6164 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
6166 if (ort == C_ORT_ACC)
6167 error ("%qD appears more than once in data clauses", t);
6168 else
6169 error ("%qD appears both in data and map clauses", t);
6170 remove = true;
6172 else
6173 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
6174 goto handle_field_decl;
6176 case OMP_CLAUSE_LASTPRIVATE:
6177 t = omp_clause_decl_field (OMP_CLAUSE_DECL (c));
6178 if (t)
6179 omp_note_field_privatization (t, OMP_CLAUSE_DECL (c));
6180 else
6181 t = OMP_CLAUSE_DECL (c);
6182 if (t == current_class_ptr)
6184 error ("%<this%> allowed in OpenMP only in %<declare simd%>"
6185 " clauses");
6186 remove = true;
6187 break;
6189 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL
6190 && ((ort & C_ORT_OMP_DECLARE_SIMD) != C_ORT_OMP
6191 || TREE_CODE (t) != FIELD_DECL))
6193 if (processing_template_decl)
6194 break;
6195 if (DECL_P (t))
6196 error ("%qD is not a variable in clause %<lastprivate%>", t);
6197 else
6198 error ("%qE is not a variable in clause %<lastprivate%>", t);
6199 remove = true;
6201 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
6202 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
6204 error ("%qD appears more than once in data clauses", t);
6205 remove = true;
6207 else
6208 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
6209 goto handle_field_decl;
6211 case OMP_CLAUSE_IF:
6212 t = OMP_CLAUSE_IF_EXPR (c);
6213 t = maybe_convert_cond (t);
6214 if (t == error_mark_node)
6215 remove = true;
6216 else if (!processing_template_decl)
6217 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6218 OMP_CLAUSE_IF_EXPR (c) = t;
6219 break;
6221 case OMP_CLAUSE_FINAL:
6222 t = OMP_CLAUSE_FINAL_EXPR (c);
6223 t = maybe_convert_cond (t);
6224 if (t == error_mark_node)
6225 remove = true;
6226 else if (!processing_template_decl)
6227 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6228 OMP_CLAUSE_FINAL_EXPR (c) = t;
6229 break;
6231 case OMP_CLAUSE_GANG:
6232 /* Operand 1 is the gang static: argument. */
6233 t = OMP_CLAUSE_OPERAND (c, 1);
6234 if (t != NULL_TREE)
6236 if (t == error_mark_node)
6237 remove = true;
6238 else if (!type_dependent_expression_p (t)
6239 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6241 error ("%<gang%> static expression must be integral");
6242 remove = true;
6244 else
6246 t = mark_rvalue_use (t);
6247 if (!processing_template_decl)
6249 t = maybe_constant_value (t);
6250 if (TREE_CODE (t) == INTEGER_CST
6251 && tree_int_cst_sgn (t) != 1
6252 && t != integer_minus_one_node)
6254 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6255 "%<gang%> static value must be "
6256 "positive");
6257 t = integer_one_node;
6260 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6262 OMP_CLAUSE_OPERAND (c, 1) = t;
6264 /* Check operand 0, the num argument. */
6265 /* FALLTHRU */
6267 case OMP_CLAUSE_WORKER:
6268 case OMP_CLAUSE_VECTOR:
6269 if (OMP_CLAUSE_OPERAND (c, 0) == NULL_TREE)
6270 break;
6271 /* FALLTHRU */
6273 case OMP_CLAUSE_NUM_TASKS:
6274 case OMP_CLAUSE_NUM_TEAMS:
6275 case OMP_CLAUSE_NUM_THREADS:
6276 case OMP_CLAUSE_NUM_GANGS:
6277 case OMP_CLAUSE_NUM_WORKERS:
6278 case OMP_CLAUSE_VECTOR_LENGTH:
6279 t = OMP_CLAUSE_OPERAND (c, 0);
6280 if (t == error_mark_node)
6281 remove = true;
6282 else if (!type_dependent_expression_p (t)
6283 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6285 switch (OMP_CLAUSE_CODE (c))
6287 case OMP_CLAUSE_GANG:
6288 error_at (OMP_CLAUSE_LOCATION (c),
6289 "%<gang%> num expression must be integral"); break;
6290 case OMP_CLAUSE_VECTOR:
6291 error_at (OMP_CLAUSE_LOCATION (c),
6292 "%<vector%> length expression must be integral");
6293 break;
6294 case OMP_CLAUSE_WORKER:
6295 error_at (OMP_CLAUSE_LOCATION (c),
6296 "%<worker%> num expression must be integral");
6297 break;
6298 default:
6299 error_at (OMP_CLAUSE_LOCATION (c),
6300 "%qs expression must be integral",
6301 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6303 remove = true;
6305 else
6307 t = mark_rvalue_use (t);
6308 if (!processing_template_decl)
6310 t = maybe_constant_value (t);
6311 if (TREE_CODE (t) == INTEGER_CST
6312 && tree_int_cst_sgn (t) != 1)
6314 switch (OMP_CLAUSE_CODE (c))
6316 case OMP_CLAUSE_GANG:
6317 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6318 "%<gang%> num value must be positive");
6319 break;
6320 case OMP_CLAUSE_VECTOR:
6321 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6322 "%<vector%> length value must be "
6323 "positive");
6324 break;
6325 case OMP_CLAUSE_WORKER:
6326 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6327 "%<worker%> num value must be "
6328 "positive");
6329 break;
6330 default:
6331 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6332 "%qs value must be positive",
6333 omp_clause_code_name
6334 [OMP_CLAUSE_CODE (c)]);
6336 t = integer_one_node;
6338 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6340 OMP_CLAUSE_OPERAND (c, 0) = t;
6342 break;
6344 case OMP_CLAUSE_SCHEDULE:
6345 if (OMP_CLAUSE_SCHEDULE_KIND (c) & OMP_CLAUSE_SCHEDULE_NONMONOTONIC)
6347 const char *p = NULL;
6348 switch (OMP_CLAUSE_SCHEDULE_KIND (c) & OMP_CLAUSE_SCHEDULE_MASK)
6350 case OMP_CLAUSE_SCHEDULE_STATIC: p = "static"; break;
6351 case OMP_CLAUSE_SCHEDULE_DYNAMIC: break;
6352 case OMP_CLAUSE_SCHEDULE_GUIDED: break;
6353 case OMP_CLAUSE_SCHEDULE_AUTO: p = "auto"; break;
6354 case OMP_CLAUSE_SCHEDULE_RUNTIME: p = "runtime"; break;
6355 default: gcc_unreachable ();
6357 if (p)
6359 error_at (OMP_CLAUSE_LOCATION (c),
6360 "%<nonmonotonic%> modifier specified for %qs "
6361 "schedule kind", p);
6362 OMP_CLAUSE_SCHEDULE_KIND (c)
6363 = (enum omp_clause_schedule_kind)
6364 (OMP_CLAUSE_SCHEDULE_KIND (c)
6365 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
6369 t = OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c);
6370 if (t == NULL)
6372 else if (t == error_mark_node)
6373 remove = true;
6374 else if (!type_dependent_expression_p (t)
6375 && (OMP_CLAUSE_SCHEDULE_KIND (c)
6376 != OMP_CLAUSE_SCHEDULE_CILKFOR)
6377 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6379 error ("schedule chunk size expression must be integral");
6380 remove = true;
6382 else
6384 t = mark_rvalue_use (t);
6385 if (!processing_template_decl)
6387 if (OMP_CLAUSE_SCHEDULE_KIND (c)
6388 == OMP_CLAUSE_SCHEDULE_CILKFOR)
6390 t = convert_to_integer (long_integer_type_node, t);
6391 if (t == error_mark_node)
6393 remove = true;
6394 break;
6397 else
6399 t = maybe_constant_value (t);
6400 if (TREE_CODE (t) == INTEGER_CST
6401 && tree_int_cst_sgn (t) != 1)
6403 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6404 "chunk size value must be positive");
6405 t = integer_one_node;
6408 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6410 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
6412 break;
6414 case OMP_CLAUSE_SIMDLEN:
6415 case OMP_CLAUSE_SAFELEN:
6416 t = OMP_CLAUSE_OPERAND (c, 0);
6417 if (t == error_mark_node)
6418 remove = true;
6419 else if (!type_dependent_expression_p (t)
6420 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6422 error ("%qs length expression must be integral",
6423 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6424 remove = true;
6426 else
6428 t = mark_rvalue_use (t);
6429 if (!processing_template_decl)
6431 t = maybe_constant_value (t);
6432 if (TREE_CODE (t) != INTEGER_CST
6433 || tree_int_cst_sgn (t) != 1)
6435 error ("%qs length expression must be positive constant"
6436 " integer expression",
6437 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6438 remove = true;
6441 OMP_CLAUSE_OPERAND (c, 0) = t;
6442 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SAFELEN)
6443 safelen = c;
6445 break;
6447 case OMP_CLAUSE_ASYNC:
6448 t = OMP_CLAUSE_ASYNC_EXPR (c);
6449 if (t == error_mark_node)
6450 remove = true;
6451 else if (!type_dependent_expression_p (t)
6452 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6454 error ("%<async%> expression must be integral");
6455 remove = true;
6457 else
6459 t = mark_rvalue_use (t);
6460 if (!processing_template_decl)
6461 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6462 OMP_CLAUSE_ASYNC_EXPR (c) = t;
6464 break;
6466 case OMP_CLAUSE_WAIT:
6467 t = OMP_CLAUSE_WAIT_EXPR (c);
6468 if (t == error_mark_node)
6469 remove = true;
6470 else if (!processing_template_decl)
6471 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6472 OMP_CLAUSE_WAIT_EXPR (c) = t;
6473 break;
6475 case OMP_CLAUSE_THREAD_LIMIT:
6476 t = OMP_CLAUSE_THREAD_LIMIT_EXPR (c);
6477 if (t == error_mark_node)
6478 remove = true;
6479 else if (!type_dependent_expression_p (t)
6480 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6482 error ("%<thread_limit%> expression must be integral");
6483 remove = true;
6485 else
6487 t = mark_rvalue_use (t);
6488 if (!processing_template_decl)
6490 t = maybe_constant_value (t);
6491 if (TREE_CODE (t) == INTEGER_CST
6492 && tree_int_cst_sgn (t) != 1)
6494 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6495 "%<thread_limit%> value must be positive");
6496 t = integer_one_node;
6498 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6500 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
6502 break;
6504 case OMP_CLAUSE_DEVICE:
6505 t = OMP_CLAUSE_DEVICE_ID (c);
6506 if (t == error_mark_node)
6507 remove = true;
6508 else if (!type_dependent_expression_p (t)
6509 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6511 error ("%<device%> id must be integral");
6512 remove = true;
6514 else
6516 t = mark_rvalue_use (t);
6517 if (!processing_template_decl)
6518 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6519 OMP_CLAUSE_DEVICE_ID (c) = t;
6521 break;
6523 case OMP_CLAUSE_DIST_SCHEDULE:
6524 t = OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c);
6525 if (t == NULL)
6527 else if (t == error_mark_node)
6528 remove = true;
6529 else if (!type_dependent_expression_p (t)
6530 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6532 error ("%<dist_schedule%> chunk size expression must be "
6533 "integral");
6534 remove = true;
6536 else
6538 t = mark_rvalue_use (t);
6539 if (!processing_template_decl)
6540 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6541 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
6543 break;
6545 case OMP_CLAUSE_ALIGNED:
6546 t = OMP_CLAUSE_DECL (c);
6547 if (t == current_class_ptr && ort != C_ORT_OMP_DECLARE_SIMD)
6549 error ("%<this%> allowed in OpenMP only in %<declare simd%>"
6550 " clauses");
6551 remove = true;
6552 break;
6554 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
6556 if (processing_template_decl)
6557 break;
6558 if (DECL_P (t))
6559 error ("%qD is not a variable in %<aligned%> clause", t);
6560 else
6561 error ("%qE is not a variable in %<aligned%> clause", t);
6562 remove = true;
6564 else if (!type_dependent_expression_p (t)
6565 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE
6566 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE
6567 && (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE
6568 || (!POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (t)))
6569 && (TREE_CODE (TREE_TYPE (TREE_TYPE (t)))
6570 != ARRAY_TYPE))))
6572 error_at (OMP_CLAUSE_LOCATION (c),
6573 "%qE in %<aligned%> clause is neither a pointer nor "
6574 "an array nor a reference to pointer or array", t);
6575 remove = true;
6577 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
6579 error ("%qD appears more than once in %<aligned%> clauses", t);
6580 remove = true;
6582 else
6583 bitmap_set_bit (&aligned_head, DECL_UID (t));
6584 t = OMP_CLAUSE_ALIGNED_ALIGNMENT (c);
6585 if (t == error_mark_node)
6586 remove = true;
6587 else if (t == NULL_TREE)
6588 break;
6589 else if (!type_dependent_expression_p (t)
6590 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6592 error ("%<aligned%> clause alignment expression must "
6593 "be integral");
6594 remove = true;
6596 else
6598 t = mark_rvalue_use (t);
6599 if (!processing_template_decl)
6601 t = maybe_constant_value (t);
6602 if (TREE_CODE (t) != INTEGER_CST
6603 || tree_int_cst_sgn (t) != 1)
6605 error ("%<aligned%> clause alignment expression must be "
6606 "positive constant integer expression");
6607 remove = true;
6610 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = t;
6612 break;
6614 case OMP_CLAUSE_DEPEND:
6615 t = OMP_CLAUSE_DECL (c);
6616 if (t == NULL_TREE)
6618 gcc_assert (OMP_CLAUSE_DEPEND_KIND (c)
6619 == OMP_CLAUSE_DEPEND_SOURCE);
6620 break;
6622 if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SINK)
6624 if (cp_finish_omp_clause_depend_sink (c))
6625 remove = true;
6626 break;
6628 if (TREE_CODE (t) == TREE_LIST)
6630 if (handle_omp_array_sections (c, ort))
6631 remove = true;
6632 break;
6634 if (t == error_mark_node)
6635 remove = true;
6636 else if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
6638 if (processing_template_decl)
6639 break;
6640 if (DECL_P (t))
6641 error ("%qD is not a variable in %<depend%> clause", t);
6642 else
6643 error ("%qE is not a variable in %<depend%> clause", t);
6644 remove = true;
6646 else if (t == current_class_ptr)
6648 error ("%<this%> allowed in OpenMP only in %<declare simd%>"
6649 " clauses");
6650 remove = true;
6652 else if (!processing_template_decl
6653 && !cxx_mark_addressable (t))
6654 remove = true;
6655 break;
6657 case OMP_CLAUSE_MAP:
6658 case OMP_CLAUSE_TO:
6659 case OMP_CLAUSE_FROM:
6660 case OMP_CLAUSE__CACHE_:
6661 t = OMP_CLAUSE_DECL (c);
6662 if (TREE_CODE (t) == TREE_LIST)
6664 if (handle_omp_array_sections (c, ort))
6665 remove = true;
6666 else
6668 t = OMP_CLAUSE_DECL (c);
6669 if (TREE_CODE (t) != TREE_LIST
6670 && !type_dependent_expression_p (t)
6671 && !cp_omp_mappable_type (TREE_TYPE (t)))
6673 error_at (OMP_CLAUSE_LOCATION (c),
6674 "array section does not have mappable type "
6675 "in %qs clause",
6676 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6677 remove = true;
6679 while (TREE_CODE (t) == ARRAY_REF)
6680 t = TREE_OPERAND (t, 0);
6681 if (TREE_CODE (t) == COMPONENT_REF
6682 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
6684 while (TREE_CODE (t) == COMPONENT_REF)
6685 t = TREE_OPERAND (t, 0);
6686 if (REFERENCE_REF_P (t))
6687 t = TREE_OPERAND (t, 0);
6688 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
6689 break;
6690 if (bitmap_bit_p (&map_head, DECL_UID (t)))
6692 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
6693 error ("%qD appears more than once in motion"
6694 " clauses", t);
6695 else if (ort == C_ORT_ACC)
6696 error ("%qD appears more than once in data"
6697 " clauses", t);
6698 else
6699 error ("%qD appears more than once in map"
6700 " clauses", t);
6701 remove = true;
6703 else
6705 bitmap_set_bit (&map_head, DECL_UID (t));
6706 bitmap_set_bit (&map_field_head, DECL_UID (t));
6710 break;
6712 if (t == error_mark_node)
6714 remove = true;
6715 break;
6717 if (REFERENCE_REF_P (t)
6718 && TREE_CODE (TREE_OPERAND (t, 0)) == COMPONENT_REF)
6720 t = TREE_OPERAND (t, 0);
6721 OMP_CLAUSE_DECL (c) = t;
6723 if (TREE_CODE (t) == COMPONENT_REF
6724 && (ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP
6725 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE__CACHE_)
6727 if (type_dependent_expression_p (t))
6728 break;
6729 if (TREE_CODE (TREE_OPERAND (t, 1)) == FIELD_DECL
6730 && DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
6732 error_at (OMP_CLAUSE_LOCATION (c),
6733 "bit-field %qE in %qs clause",
6734 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6735 remove = true;
6737 else if (!cp_omp_mappable_type (TREE_TYPE (t)))
6739 error_at (OMP_CLAUSE_LOCATION (c),
6740 "%qE does not have a mappable type in %qs clause",
6741 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6742 remove = true;
6744 while (TREE_CODE (t) == COMPONENT_REF)
6746 if (TREE_TYPE (TREE_OPERAND (t, 0))
6747 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0)))
6748 == UNION_TYPE))
6750 error_at (OMP_CLAUSE_LOCATION (c),
6751 "%qE is a member of a union", t);
6752 remove = true;
6753 break;
6755 t = TREE_OPERAND (t, 0);
6757 if (remove)
6758 break;
6759 if (REFERENCE_REF_P (t))
6760 t = TREE_OPERAND (t, 0);
6761 if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
6763 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
6764 goto handle_map_references;
6767 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
6769 if (processing_template_decl)
6770 break;
6771 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
6772 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
6773 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ALWAYS_POINTER))
6774 break;
6775 if (DECL_P (t))
6776 error ("%qD is not a variable in %qs clause", t,
6777 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6778 else
6779 error ("%qE is not a variable in %qs clause", t,
6780 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6781 remove = true;
6783 else if (VAR_P (t) && CP_DECL_THREAD_LOCAL_P (t))
6785 error ("%qD is threadprivate variable in %qs clause", t,
6786 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6787 remove = true;
6789 else if (ort != C_ORT_ACC && t == current_class_ptr)
6791 error ("%<this%> allowed in OpenMP only in %<declare simd%>"
6792 " clauses");
6793 remove = true;
6794 break;
6796 else if (!processing_template_decl
6797 && TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE
6798 && (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
6799 || (OMP_CLAUSE_MAP_KIND (c)
6800 != GOMP_MAP_FIRSTPRIVATE_POINTER))
6801 && !cxx_mark_addressable (t))
6802 remove = true;
6803 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
6804 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
6805 || (OMP_CLAUSE_MAP_KIND (c)
6806 == GOMP_MAP_FIRSTPRIVATE_POINTER)))
6807 && t == OMP_CLAUSE_DECL (c)
6808 && !type_dependent_expression_p (t)
6809 && !cp_omp_mappable_type ((TREE_CODE (TREE_TYPE (t))
6810 == REFERENCE_TYPE)
6811 ? TREE_TYPE (TREE_TYPE (t))
6812 : TREE_TYPE (t)))
6814 error_at (OMP_CLAUSE_LOCATION (c),
6815 "%qD does not have a mappable type in %qs clause", t,
6816 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6817 remove = true;
6819 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
6820 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FORCE_DEVICEPTR
6821 && !type_dependent_expression_p (t)
6822 && !POINTER_TYPE_P (TREE_TYPE (t)))
6824 error ("%qD is not a pointer variable", t);
6825 remove = true;
6827 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
6828 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
6830 if (bitmap_bit_p (&generic_head, DECL_UID (t))
6831 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
6833 error ("%qD appears more than once in data clauses", t);
6834 remove = true;
6836 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
6838 if (ort == C_ORT_ACC)
6839 error ("%qD appears more than once in data clauses", t);
6840 else
6841 error ("%qD appears both in data and map clauses", t);
6842 remove = true;
6844 else
6845 bitmap_set_bit (&generic_head, DECL_UID (t));
6847 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
6849 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
6850 error ("%qD appears more than once in motion clauses", t);
6851 if (ort == C_ORT_ACC)
6852 error ("%qD appears more than once in data clauses", t);
6853 else
6854 error ("%qD appears more than once in map clauses", t);
6855 remove = true;
6857 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
6858 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
6860 if (ort == C_ORT_ACC)
6861 error ("%qD appears more than once in data clauses", t);
6862 else
6863 error ("%qD appears both in data and map clauses", t);
6864 remove = true;
6866 else
6868 bitmap_set_bit (&map_head, DECL_UID (t));
6869 if (t != OMP_CLAUSE_DECL (c)
6870 && TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPONENT_REF)
6871 bitmap_set_bit (&map_field_head, DECL_UID (t));
6873 handle_map_references:
6874 if (!remove
6875 && !processing_template_decl
6876 && (ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP
6877 && TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c))) == REFERENCE_TYPE)
6879 t = OMP_CLAUSE_DECL (c);
6880 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
6882 OMP_CLAUSE_DECL (c) = build_simple_mem_ref (t);
6883 if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
6884 OMP_CLAUSE_SIZE (c)
6885 = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t)));
6887 else if (OMP_CLAUSE_MAP_KIND (c)
6888 != GOMP_MAP_FIRSTPRIVATE_POINTER
6889 && (OMP_CLAUSE_MAP_KIND (c)
6890 != GOMP_MAP_FIRSTPRIVATE_REFERENCE)
6891 && (OMP_CLAUSE_MAP_KIND (c)
6892 != GOMP_MAP_ALWAYS_POINTER))
6894 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
6895 OMP_CLAUSE_MAP);
6896 if (TREE_CODE (t) == COMPONENT_REF)
6897 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ALWAYS_POINTER);
6898 else
6899 OMP_CLAUSE_SET_MAP_KIND (c2,
6900 GOMP_MAP_FIRSTPRIVATE_REFERENCE);
6901 OMP_CLAUSE_DECL (c2) = t;
6902 OMP_CLAUSE_SIZE (c2) = size_zero_node;
6903 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
6904 OMP_CLAUSE_CHAIN (c) = c2;
6905 OMP_CLAUSE_DECL (c) = build_simple_mem_ref (t);
6906 if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
6907 OMP_CLAUSE_SIZE (c)
6908 = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t)));
6909 c = c2;
6912 break;
6914 case OMP_CLAUSE_TO_DECLARE:
6915 case OMP_CLAUSE_LINK:
6916 t = OMP_CLAUSE_DECL (c);
6917 if (TREE_CODE (t) == FUNCTION_DECL
6918 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
6920 else if (!VAR_P (t))
6922 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
6924 if (TREE_CODE (t) == TEMPLATE_ID_EXPR)
6925 error_at (OMP_CLAUSE_LOCATION (c),
6926 "template %qE in clause %qs", t,
6927 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6928 else if (really_overloaded_fn (t))
6929 error_at (OMP_CLAUSE_LOCATION (c),
6930 "overloaded function name %qE in clause %qs", t,
6931 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6932 else
6933 error_at (OMP_CLAUSE_LOCATION (c),
6934 "%qE is neither a variable nor a function name "
6935 "in clause %qs", t,
6936 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6938 else
6939 error_at (OMP_CLAUSE_LOCATION (c),
6940 "%qE is not a variable in clause %qs", t,
6941 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6942 remove = true;
6944 else if (DECL_THREAD_LOCAL_P (t))
6946 error_at (OMP_CLAUSE_LOCATION (c),
6947 "%qD is threadprivate variable in %qs clause", t,
6948 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6949 remove = true;
6951 else if (!cp_omp_mappable_type (TREE_TYPE (t)))
6953 error_at (OMP_CLAUSE_LOCATION (c),
6954 "%qD does not have a mappable type in %qs clause", t,
6955 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6956 remove = true;
6958 if (remove)
6959 break;
6960 if (bitmap_bit_p (&generic_head, DECL_UID (t)))
6962 error_at (OMP_CLAUSE_LOCATION (c),
6963 "%qE appears more than once on the same "
6964 "%<declare target%> directive", t);
6965 remove = true;
6967 else
6968 bitmap_set_bit (&generic_head, DECL_UID (t));
6969 break;
6971 case OMP_CLAUSE_UNIFORM:
6972 t = OMP_CLAUSE_DECL (c);
6973 if (TREE_CODE (t) != PARM_DECL)
6975 if (processing_template_decl)
6976 break;
6977 if (DECL_P (t))
6978 error ("%qD is not an argument in %<uniform%> clause", t);
6979 else
6980 error ("%qE is not an argument in %<uniform%> clause", t);
6981 remove = true;
6982 break;
6984 /* map_head bitmap is used as uniform_head if declare_simd. */
6985 bitmap_set_bit (&map_head, DECL_UID (t));
6986 goto check_dup_generic;
6988 case OMP_CLAUSE_GRAINSIZE:
6989 t = OMP_CLAUSE_GRAINSIZE_EXPR (c);
6990 if (t == error_mark_node)
6991 remove = true;
6992 else if (!type_dependent_expression_p (t)
6993 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6995 error ("%<grainsize%> expression must be integral");
6996 remove = true;
6998 else
7000 t = mark_rvalue_use (t);
7001 if (!processing_template_decl)
7003 t = maybe_constant_value (t);
7004 if (TREE_CODE (t) == INTEGER_CST
7005 && tree_int_cst_sgn (t) != 1)
7007 warning_at (OMP_CLAUSE_LOCATION (c), 0,
7008 "%<grainsize%> value must be positive");
7009 t = integer_one_node;
7011 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7013 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
7015 break;
7017 case OMP_CLAUSE_PRIORITY:
7018 t = OMP_CLAUSE_PRIORITY_EXPR (c);
7019 if (t == error_mark_node)
7020 remove = true;
7021 else if (!type_dependent_expression_p (t)
7022 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
7024 error ("%<priority%> expression must be integral");
7025 remove = true;
7027 else
7029 t = mark_rvalue_use (t);
7030 if (!processing_template_decl)
7032 t = maybe_constant_value (t);
7033 if (TREE_CODE (t) == INTEGER_CST
7034 && tree_int_cst_sgn (t) == -1)
7036 warning_at (OMP_CLAUSE_LOCATION (c), 0,
7037 "%<priority%> value must be non-negative");
7038 t = integer_one_node;
7040 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7042 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
7044 break;
7046 case OMP_CLAUSE_HINT:
7047 t = OMP_CLAUSE_HINT_EXPR (c);
7048 if (t == error_mark_node)
7049 remove = true;
7050 else if (!type_dependent_expression_p (t)
7051 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
7053 error ("%<num_tasks%> expression must be integral");
7054 remove = true;
7056 else
7058 t = mark_rvalue_use (t);
7059 if (!processing_template_decl)
7061 t = maybe_constant_value (t);
7062 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7064 OMP_CLAUSE_HINT_EXPR (c) = t;
7066 break;
7068 case OMP_CLAUSE_IS_DEVICE_PTR:
7069 case OMP_CLAUSE_USE_DEVICE_PTR:
7070 field_ok = (ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP;
7071 t = OMP_CLAUSE_DECL (c);
7072 if (!type_dependent_expression_p (t))
7074 tree type = TREE_TYPE (t);
7075 if (TREE_CODE (type) != POINTER_TYPE
7076 && TREE_CODE (type) != ARRAY_TYPE
7077 && (TREE_CODE (type) != REFERENCE_TYPE
7078 || (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
7079 && TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE)))
7081 error_at (OMP_CLAUSE_LOCATION (c),
7082 "%qs variable is neither a pointer, nor an array "
7083 "nor reference to pointer or array",
7084 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7085 remove = true;
7088 goto check_dup_generic;
7090 case OMP_CLAUSE_NOWAIT:
7091 case OMP_CLAUSE_DEFAULT:
7092 case OMP_CLAUSE_UNTIED:
7093 case OMP_CLAUSE_COLLAPSE:
7094 case OMP_CLAUSE_MERGEABLE:
7095 case OMP_CLAUSE_PARALLEL:
7096 case OMP_CLAUSE_FOR:
7097 case OMP_CLAUSE_SECTIONS:
7098 case OMP_CLAUSE_TASKGROUP:
7099 case OMP_CLAUSE_PROC_BIND:
7100 case OMP_CLAUSE_NOGROUP:
7101 case OMP_CLAUSE_THREADS:
7102 case OMP_CLAUSE_SIMD:
7103 case OMP_CLAUSE_DEFAULTMAP:
7104 case OMP_CLAUSE__CILK_FOR_COUNT_:
7105 case OMP_CLAUSE_AUTO:
7106 case OMP_CLAUSE_INDEPENDENT:
7107 case OMP_CLAUSE_SEQ:
7108 break;
7110 case OMP_CLAUSE_TILE:
7111 for (tree list = OMP_CLAUSE_TILE_LIST (c); !remove && list;
7112 list = TREE_CHAIN (list))
7114 t = TREE_VALUE (list);
7116 if (t == error_mark_node)
7117 remove = true;
7118 else if (!type_dependent_expression_p (t)
7119 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
7121 error_at (OMP_CLAUSE_LOCATION (c),
7122 "%<tile%> argument needs integral type");
7123 remove = true;
7125 else
7127 t = mark_rvalue_use (t);
7128 if (!processing_template_decl)
7130 /* Zero is used to indicate '*', we permit you
7131 to get there via an ICE of value zero. */
7132 t = maybe_constant_value (t);
7133 if (!tree_fits_shwi_p (t)
7134 || tree_to_shwi (t) < 0)
7136 error_at (OMP_CLAUSE_LOCATION (c),
7137 "%<tile%> argument needs positive "
7138 "integral constant");
7139 remove = true;
7142 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7145 /* Update list item. */
7146 TREE_VALUE (list) = t;
7148 break;
7150 case OMP_CLAUSE_ORDERED:
7151 ordered_seen = true;
7152 break;
7154 case OMP_CLAUSE_INBRANCH:
7155 case OMP_CLAUSE_NOTINBRANCH:
7156 if (branch_seen)
7158 error ("%<inbranch%> clause is incompatible with "
7159 "%<notinbranch%>");
7160 remove = true;
7162 branch_seen = true;
7163 break;
7165 default:
7166 gcc_unreachable ();
7169 if (remove)
7170 *pc = OMP_CLAUSE_CHAIN (c);
7171 else
7172 pc = &OMP_CLAUSE_CHAIN (c);
7175 for (pc = &clauses, c = clauses; c ; c = *pc)
7177 enum omp_clause_code c_kind = OMP_CLAUSE_CODE (c);
7178 bool remove = false;
7179 bool need_complete_type = false;
7180 bool need_default_ctor = false;
7181 bool need_copy_ctor = false;
7182 bool need_copy_assignment = false;
7183 bool need_implicitly_determined = false;
7184 bool need_dtor = false;
7185 tree type, inner_type;
7187 switch (c_kind)
7189 case OMP_CLAUSE_SHARED:
7190 need_implicitly_determined = true;
7191 break;
7192 case OMP_CLAUSE_PRIVATE:
7193 need_complete_type = true;
7194 need_default_ctor = true;
7195 need_dtor = true;
7196 need_implicitly_determined = true;
7197 break;
7198 case OMP_CLAUSE_FIRSTPRIVATE:
7199 need_complete_type = true;
7200 need_copy_ctor = true;
7201 need_dtor = true;
7202 need_implicitly_determined = true;
7203 break;
7204 case OMP_CLAUSE_LASTPRIVATE:
7205 need_complete_type = true;
7206 need_copy_assignment = true;
7207 need_implicitly_determined = true;
7208 break;
7209 case OMP_CLAUSE_REDUCTION:
7210 need_implicitly_determined = true;
7211 break;
7212 case OMP_CLAUSE_LINEAR:
7213 if (ort != C_ORT_OMP_DECLARE_SIMD)
7214 need_implicitly_determined = true;
7215 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c)
7216 && !bitmap_bit_p (&map_head,
7217 DECL_UID (OMP_CLAUSE_LINEAR_STEP (c))))
7219 error_at (OMP_CLAUSE_LOCATION (c),
7220 "%<linear%> clause step is a parameter %qD not "
7221 "specified in %<uniform%> clause",
7222 OMP_CLAUSE_LINEAR_STEP (c));
7223 *pc = OMP_CLAUSE_CHAIN (c);
7224 continue;
7226 break;
7227 case OMP_CLAUSE_COPYPRIVATE:
7228 need_copy_assignment = true;
7229 break;
7230 case OMP_CLAUSE_COPYIN:
7231 need_copy_assignment = true;
7232 break;
7233 case OMP_CLAUSE_SIMDLEN:
7234 if (safelen
7235 && !processing_template_decl
7236 && tree_int_cst_lt (OMP_CLAUSE_SAFELEN_EXPR (safelen),
7237 OMP_CLAUSE_SIMDLEN_EXPR (c)))
7239 error_at (OMP_CLAUSE_LOCATION (c),
7240 "%<simdlen%> clause value is bigger than "
7241 "%<safelen%> clause value");
7242 OMP_CLAUSE_SIMDLEN_EXPR (c)
7243 = OMP_CLAUSE_SAFELEN_EXPR (safelen);
7245 pc = &OMP_CLAUSE_CHAIN (c);
7246 continue;
7247 case OMP_CLAUSE_SCHEDULE:
7248 if (ordered_seen
7249 && (OMP_CLAUSE_SCHEDULE_KIND (c)
7250 & OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
7252 error_at (OMP_CLAUSE_LOCATION (c),
7253 "%<nonmonotonic%> schedule modifier specified "
7254 "together with %<ordered%> clause");
7255 OMP_CLAUSE_SCHEDULE_KIND (c)
7256 = (enum omp_clause_schedule_kind)
7257 (OMP_CLAUSE_SCHEDULE_KIND (c)
7258 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
7260 pc = &OMP_CLAUSE_CHAIN (c);
7261 continue;
7262 case OMP_CLAUSE_NOWAIT:
7263 if (copyprivate_seen)
7265 error_at (OMP_CLAUSE_LOCATION (c),
7266 "%<nowait%> clause must not be used together "
7267 "with %<copyprivate%>");
7268 *pc = OMP_CLAUSE_CHAIN (c);
7269 continue;
7271 /* FALLTHRU */
7272 default:
7273 pc = &OMP_CLAUSE_CHAIN (c);
7274 continue;
7277 t = OMP_CLAUSE_DECL (c);
7278 if (processing_template_decl
7279 && !VAR_P (t) && TREE_CODE (t) != PARM_DECL)
7281 pc = &OMP_CLAUSE_CHAIN (c);
7282 continue;
7285 switch (c_kind)
7287 case OMP_CLAUSE_LASTPRIVATE:
7288 if (!bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
7290 need_default_ctor = true;
7291 need_dtor = true;
7293 break;
7295 case OMP_CLAUSE_REDUCTION:
7296 if (finish_omp_reduction_clause (c, &need_default_ctor,
7297 &need_dtor))
7298 remove = true;
7299 else
7300 t = OMP_CLAUSE_DECL (c);
7301 break;
7303 case OMP_CLAUSE_COPYIN:
7304 if (!VAR_P (t) || !CP_DECL_THREAD_LOCAL_P (t))
7306 error ("%qE must be %<threadprivate%> for %<copyin%>", t);
7307 remove = true;
7309 break;
7311 default:
7312 break;
7315 if (need_complete_type || need_copy_assignment)
7317 t = require_complete_type (t);
7318 if (t == error_mark_node)
7319 remove = true;
7320 else if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE
7321 && !complete_type_or_else (TREE_TYPE (TREE_TYPE (t)), t))
7322 remove = true;
7324 if (need_implicitly_determined)
7326 const char *share_name = NULL;
7328 if (VAR_P (t) && CP_DECL_THREAD_LOCAL_P (t))
7329 share_name = "threadprivate";
7330 else switch (cxx_omp_predetermined_sharing (t))
7332 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
7333 break;
7334 case OMP_CLAUSE_DEFAULT_SHARED:
7335 /* const vars may be specified in firstprivate clause. */
7336 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
7337 && cxx_omp_const_qual_no_mutable (t))
7338 break;
7339 share_name = "shared";
7340 break;
7341 case OMP_CLAUSE_DEFAULT_PRIVATE:
7342 share_name = "private";
7343 break;
7344 default:
7345 gcc_unreachable ();
7347 if (share_name)
7349 error ("%qE is predetermined %qs for %qs",
7350 omp_clause_printable_decl (t), share_name,
7351 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7352 remove = true;
7356 /* We're interested in the base element, not arrays. */
7357 inner_type = type = TREE_TYPE (t);
7358 if ((need_complete_type
7359 || need_copy_assignment
7360 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
7361 && TREE_CODE (inner_type) == REFERENCE_TYPE)
7362 inner_type = TREE_TYPE (inner_type);
7363 while (TREE_CODE (inner_type) == ARRAY_TYPE)
7364 inner_type = TREE_TYPE (inner_type);
7366 /* Check for special function availability by building a call to one.
7367 Save the results, because later we won't be in the right context
7368 for making these queries. */
7369 if (CLASS_TYPE_P (inner_type)
7370 && COMPLETE_TYPE_P (inner_type)
7371 && (need_default_ctor || need_copy_ctor
7372 || need_copy_assignment || need_dtor)
7373 && !type_dependent_expression_p (t)
7374 && cxx_omp_create_clause_info (c, inner_type, need_default_ctor,
7375 need_copy_ctor, need_copy_assignment,
7376 need_dtor))
7377 remove = true;
7379 if (!remove
7380 && c_kind == OMP_CLAUSE_SHARED
7381 && processing_template_decl)
7383 t = omp_clause_decl_field (OMP_CLAUSE_DECL (c));
7384 if (t)
7385 OMP_CLAUSE_DECL (c) = t;
7388 if (remove)
7389 *pc = OMP_CLAUSE_CHAIN (c);
7390 else
7391 pc = &OMP_CLAUSE_CHAIN (c);
7394 bitmap_obstack_release (NULL);
7395 return clauses;
7398 /* Start processing OpenMP clauses that can include any
7399 privatization clauses for non-static data members. */
7401 tree
7402 push_omp_privatization_clauses (bool ignore_next)
7404 if (omp_private_member_ignore_next)
7406 omp_private_member_ignore_next = ignore_next;
7407 return NULL_TREE;
7409 omp_private_member_ignore_next = ignore_next;
7410 if (omp_private_member_map)
7411 omp_private_member_vec.safe_push (error_mark_node);
7412 return push_stmt_list ();
7415 /* Revert remapping of any non-static data members since
7416 the last push_omp_privatization_clauses () call. */
7418 void
7419 pop_omp_privatization_clauses (tree stmt)
7421 if (stmt == NULL_TREE)
7422 return;
7423 stmt = pop_stmt_list (stmt);
7424 if (omp_private_member_map)
7426 while (!omp_private_member_vec.is_empty ())
7428 tree t = omp_private_member_vec.pop ();
7429 if (t == error_mark_node)
7431 add_stmt (stmt);
7432 return;
7434 bool no_decl_expr = t == integer_zero_node;
7435 if (no_decl_expr)
7436 t = omp_private_member_vec.pop ();
7437 tree *v = omp_private_member_map->get (t);
7438 gcc_assert (v);
7439 if (!no_decl_expr)
7440 add_decl_expr (*v);
7441 omp_private_member_map->remove (t);
7443 delete omp_private_member_map;
7444 omp_private_member_map = NULL;
7446 add_stmt (stmt);
7449 /* Remember OpenMP privatization clauses mapping and clear it.
7450 Used for lambdas. */
7452 void
7453 save_omp_privatization_clauses (vec<tree> &save)
7455 save = vNULL;
7456 if (omp_private_member_ignore_next)
7457 save.safe_push (integer_one_node);
7458 omp_private_member_ignore_next = false;
7459 if (!omp_private_member_map)
7460 return;
7462 while (!omp_private_member_vec.is_empty ())
7464 tree t = omp_private_member_vec.pop ();
7465 if (t == error_mark_node)
7467 save.safe_push (t);
7468 continue;
7470 tree n = t;
7471 if (t == integer_zero_node)
7472 t = omp_private_member_vec.pop ();
7473 tree *v = omp_private_member_map->get (t);
7474 gcc_assert (v);
7475 save.safe_push (*v);
7476 save.safe_push (t);
7477 if (n != t)
7478 save.safe_push (n);
7480 delete omp_private_member_map;
7481 omp_private_member_map = NULL;
7484 /* Restore OpenMP privatization clauses mapping saved by the
7485 above function. */
7487 void
7488 restore_omp_privatization_clauses (vec<tree> &save)
7490 gcc_assert (omp_private_member_vec.is_empty ());
7491 omp_private_member_ignore_next = false;
7492 if (save.is_empty ())
7493 return;
7494 if (save.length () == 1 && save[0] == integer_one_node)
7496 omp_private_member_ignore_next = true;
7497 save.release ();
7498 return;
7501 omp_private_member_map = new hash_map <tree, tree>;
7502 while (!save.is_empty ())
7504 tree t = save.pop ();
7505 tree n = t;
7506 if (t != error_mark_node)
7508 if (t == integer_one_node)
7510 omp_private_member_ignore_next = true;
7511 gcc_assert (save.is_empty ());
7512 break;
7514 if (t == integer_zero_node)
7515 t = save.pop ();
7516 tree &v = omp_private_member_map->get_or_insert (t);
7517 v = save.pop ();
7519 omp_private_member_vec.safe_push (t);
7520 if (n != t)
7521 omp_private_member_vec.safe_push (n);
7523 save.release ();
7526 /* For all variables in the tree_list VARS, mark them as thread local. */
7528 void
7529 finish_omp_threadprivate (tree vars)
7531 tree t;
7533 /* Mark every variable in VARS to be assigned thread local storage. */
7534 for (t = vars; t; t = TREE_CHAIN (t))
7536 tree v = TREE_PURPOSE (t);
7538 if (error_operand_p (v))
7540 else if (!VAR_P (v))
7541 error ("%<threadprivate%> %qD is not file, namespace "
7542 "or block scope variable", v);
7543 /* If V had already been marked threadprivate, it doesn't matter
7544 whether it had been used prior to this point. */
7545 else if (TREE_USED (v)
7546 && (DECL_LANG_SPECIFIC (v) == NULL
7547 || !CP_DECL_THREADPRIVATE_P (v)))
7548 error ("%qE declared %<threadprivate%> after first use", v);
7549 else if (! TREE_STATIC (v) && ! DECL_EXTERNAL (v))
7550 error ("automatic variable %qE cannot be %<threadprivate%>", v);
7551 else if (! COMPLETE_TYPE_P (complete_type (TREE_TYPE (v))))
7552 error ("%<threadprivate%> %qE has incomplete type", v);
7553 else if (TREE_STATIC (v) && TYPE_P (CP_DECL_CONTEXT (v))
7554 && CP_DECL_CONTEXT (v) != current_class_type)
7555 error ("%<threadprivate%> %qE directive not "
7556 "in %qT definition", v, CP_DECL_CONTEXT (v));
7557 else
7559 /* Allocate a LANG_SPECIFIC structure for V, if needed. */
7560 if (DECL_LANG_SPECIFIC (v) == NULL)
7562 retrofit_lang_decl (v);
7564 /* Make sure that DECL_DISCRIMINATOR_P continues to be true
7565 after the allocation of the lang_decl structure. */
7566 if (DECL_DISCRIMINATOR_P (v))
7567 DECL_LANG_SPECIFIC (v)->u.base.u2sel = 1;
7570 if (! CP_DECL_THREAD_LOCAL_P (v))
7572 CP_DECL_THREAD_LOCAL_P (v) = true;
7573 set_decl_tls_model (v, decl_default_tls_model (v));
7574 /* If rtl has been already set for this var, call
7575 make_decl_rtl once again, so that encode_section_info
7576 has a chance to look at the new decl flags. */
7577 if (DECL_RTL_SET_P (v))
7578 make_decl_rtl (v);
7580 CP_DECL_THREADPRIVATE_P (v) = 1;
7585 /* Build an OpenMP structured block. */
7587 tree
7588 begin_omp_structured_block (void)
7590 return do_pushlevel (sk_omp);
7593 tree
7594 finish_omp_structured_block (tree block)
7596 return do_poplevel (block);
7599 /* Similarly, except force the retention of the BLOCK. */
7601 tree
7602 begin_omp_parallel (void)
7604 keep_next_level (true);
7605 return begin_omp_structured_block ();
7608 /* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
7609 statement. */
7611 tree
7612 finish_oacc_data (tree clauses, tree block)
7614 tree stmt;
7616 block = finish_omp_structured_block (block);
7618 stmt = make_node (OACC_DATA);
7619 TREE_TYPE (stmt) = void_type_node;
7620 OACC_DATA_CLAUSES (stmt) = clauses;
7621 OACC_DATA_BODY (stmt) = block;
7623 return add_stmt (stmt);
7626 /* Generate OACC_HOST_DATA, with CLAUSES and BLOCK as its compound
7627 statement. */
7629 tree
7630 finish_oacc_host_data (tree clauses, tree block)
7632 tree stmt;
7634 block = finish_omp_structured_block (block);
7636 stmt = make_node (OACC_HOST_DATA);
7637 TREE_TYPE (stmt) = void_type_node;
7638 OACC_HOST_DATA_CLAUSES (stmt) = clauses;
7639 OACC_HOST_DATA_BODY (stmt) = block;
7641 return add_stmt (stmt);
7644 /* Generate OMP construct CODE, with BODY and CLAUSES as its compound
7645 statement. */
7647 tree
7648 finish_omp_construct (enum tree_code code, tree body, tree clauses)
7650 body = finish_omp_structured_block (body);
7652 tree stmt = make_node (code);
7653 TREE_TYPE (stmt) = void_type_node;
7654 OMP_BODY (stmt) = body;
7655 OMP_CLAUSES (stmt) = clauses;
7657 return add_stmt (stmt);
7660 tree
7661 finish_omp_parallel (tree clauses, tree body)
7663 tree stmt;
7665 body = finish_omp_structured_block (body);
7667 stmt = make_node (OMP_PARALLEL);
7668 TREE_TYPE (stmt) = void_type_node;
7669 OMP_PARALLEL_CLAUSES (stmt) = clauses;
7670 OMP_PARALLEL_BODY (stmt) = body;
7672 return add_stmt (stmt);
7675 tree
7676 begin_omp_task (void)
7678 keep_next_level (true);
7679 return begin_omp_structured_block ();
7682 tree
7683 finish_omp_task (tree clauses, tree body)
7685 tree stmt;
7687 body = finish_omp_structured_block (body);
7689 stmt = make_node (OMP_TASK);
7690 TREE_TYPE (stmt) = void_type_node;
7691 OMP_TASK_CLAUSES (stmt) = clauses;
7692 OMP_TASK_BODY (stmt) = body;
7694 return add_stmt (stmt);
7697 /* Helper function for finish_omp_for. Convert Ith random access iterator
7698 into integral iterator. Return FALSE if successful. */
7700 static bool
7701 handle_omp_for_class_iterator (int i, location_t locus, enum tree_code code,
7702 tree declv, tree orig_declv, tree initv,
7703 tree condv, tree incrv, tree *body,
7704 tree *pre_body, tree &clauses, tree *lastp,
7705 int collapse, int ordered)
7707 tree diff, iter_init, iter_incr = NULL, last;
7708 tree incr_var = NULL, orig_pre_body, orig_body, c;
7709 tree decl = TREE_VEC_ELT (declv, i);
7710 tree init = TREE_VEC_ELT (initv, i);
7711 tree cond = TREE_VEC_ELT (condv, i);
7712 tree incr = TREE_VEC_ELT (incrv, i);
7713 tree iter = decl;
7714 location_t elocus = locus;
7716 if (init && EXPR_HAS_LOCATION (init))
7717 elocus = EXPR_LOCATION (init);
7719 cond = cp_fully_fold (cond);
7720 switch (TREE_CODE (cond))
7722 case GT_EXPR:
7723 case GE_EXPR:
7724 case LT_EXPR:
7725 case LE_EXPR:
7726 case NE_EXPR:
7727 if (TREE_OPERAND (cond, 1) == iter)
7728 cond = build2 (swap_tree_comparison (TREE_CODE (cond)),
7729 TREE_TYPE (cond), iter, TREE_OPERAND (cond, 0));
7730 if (TREE_OPERAND (cond, 0) != iter)
7731 cond = error_mark_node;
7732 else
7734 tree tem = build_x_binary_op (EXPR_LOCATION (cond),
7735 TREE_CODE (cond),
7736 iter, ERROR_MARK,
7737 TREE_OPERAND (cond, 1), ERROR_MARK,
7738 NULL, tf_warning_or_error);
7739 if (error_operand_p (tem))
7740 return true;
7742 break;
7743 default:
7744 cond = error_mark_node;
7745 break;
7747 if (cond == error_mark_node)
7749 error_at (elocus, "invalid controlling predicate");
7750 return true;
7752 diff = build_x_binary_op (elocus, MINUS_EXPR, TREE_OPERAND (cond, 1),
7753 ERROR_MARK, iter, ERROR_MARK, NULL,
7754 tf_warning_or_error);
7755 diff = cp_fully_fold (diff);
7756 if (error_operand_p (diff))
7757 return true;
7758 if (TREE_CODE (TREE_TYPE (diff)) != INTEGER_TYPE)
7760 error_at (elocus, "difference between %qE and %qD does not have integer type",
7761 TREE_OPERAND (cond, 1), iter);
7762 return true;
7764 if (!c_omp_check_loop_iv_exprs (locus, orig_declv,
7765 TREE_VEC_ELT (declv, i), NULL_TREE,
7766 cond, cp_walk_subtrees))
7767 return true;
7769 switch (TREE_CODE (incr))
7771 case PREINCREMENT_EXPR:
7772 case PREDECREMENT_EXPR:
7773 case POSTINCREMENT_EXPR:
7774 case POSTDECREMENT_EXPR:
7775 if (TREE_OPERAND (incr, 0) != iter)
7777 incr = error_mark_node;
7778 break;
7780 iter_incr = build_x_unary_op (EXPR_LOCATION (incr),
7781 TREE_CODE (incr), iter,
7782 tf_warning_or_error);
7783 if (error_operand_p (iter_incr))
7784 return true;
7785 else if (TREE_CODE (incr) == PREINCREMENT_EXPR
7786 || TREE_CODE (incr) == POSTINCREMENT_EXPR)
7787 incr = integer_one_node;
7788 else
7789 incr = integer_minus_one_node;
7790 break;
7791 case MODIFY_EXPR:
7792 if (TREE_OPERAND (incr, 0) != iter)
7793 incr = error_mark_node;
7794 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
7795 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
7797 tree rhs = TREE_OPERAND (incr, 1);
7798 if (TREE_OPERAND (rhs, 0) == iter)
7800 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (rhs, 1)))
7801 != INTEGER_TYPE)
7802 incr = error_mark_node;
7803 else
7805 iter_incr = build_x_modify_expr (EXPR_LOCATION (rhs),
7806 iter, TREE_CODE (rhs),
7807 TREE_OPERAND (rhs, 1),
7808 tf_warning_or_error);
7809 if (error_operand_p (iter_incr))
7810 return true;
7811 incr = TREE_OPERAND (rhs, 1);
7812 incr = cp_convert (TREE_TYPE (diff), incr,
7813 tf_warning_or_error);
7814 if (TREE_CODE (rhs) == MINUS_EXPR)
7816 incr = build1 (NEGATE_EXPR, TREE_TYPE (diff), incr);
7817 incr = fold_simple (incr);
7819 if (TREE_CODE (incr) != INTEGER_CST
7820 && (TREE_CODE (incr) != NOP_EXPR
7821 || (TREE_CODE (TREE_OPERAND (incr, 0))
7822 != INTEGER_CST)))
7823 iter_incr = NULL;
7826 else if (TREE_OPERAND (rhs, 1) == iter)
7828 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (rhs, 0))) != INTEGER_TYPE
7829 || TREE_CODE (rhs) != PLUS_EXPR)
7830 incr = error_mark_node;
7831 else
7833 iter_incr = build_x_binary_op (EXPR_LOCATION (rhs),
7834 PLUS_EXPR,
7835 TREE_OPERAND (rhs, 0),
7836 ERROR_MARK, iter,
7837 ERROR_MARK, NULL,
7838 tf_warning_or_error);
7839 if (error_operand_p (iter_incr))
7840 return true;
7841 iter_incr = build_x_modify_expr (EXPR_LOCATION (rhs),
7842 iter, NOP_EXPR,
7843 iter_incr,
7844 tf_warning_or_error);
7845 if (error_operand_p (iter_incr))
7846 return true;
7847 incr = TREE_OPERAND (rhs, 0);
7848 iter_incr = NULL;
7851 else
7852 incr = error_mark_node;
7854 else
7855 incr = error_mark_node;
7856 break;
7857 default:
7858 incr = error_mark_node;
7859 break;
7862 if (incr == error_mark_node)
7864 error_at (elocus, "invalid increment expression");
7865 return true;
7868 incr = cp_convert (TREE_TYPE (diff), incr, tf_warning_or_error);
7869 bool taskloop_iv_seen = false;
7870 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
7871 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
7872 && OMP_CLAUSE_DECL (c) == iter)
7874 if (code == OMP_TASKLOOP)
7876 taskloop_iv_seen = true;
7877 OMP_CLAUSE_LASTPRIVATE_TASKLOOP_IV (c) = 1;
7879 break;
7881 else if (code == OMP_TASKLOOP
7882 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
7883 && OMP_CLAUSE_DECL (c) == iter)
7885 taskloop_iv_seen = true;
7886 OMP_CLAUSE_PRIVATE_TASKLOOP_IV (c) = 1;
7889 decl = create_temporary_var (TREE_TYPE (diff));
7890 pushdecl (decl);
7891 add_decl_expr (decl);
7892 last = create_temporary_var (TREE_TYPE (diff));
7893 pushdecl (last);
7894 add_decl_expr (last);
7895 if (c && iter_incr == NULL && TREE_CODE (incr) != INTEGER_CST
7896 && (!ordered || (i < collapse && collapse > 1)))
7898 incr_var = create_temporary_var (TREE_TYPE (diff));
7899 pushdecl (incr_var);
7900 add_decl_expr (incr_var);
7902 gcc_assert (stmts_are_full_exprs_p ());
7903 tree diffvar = NULL_TREE;
7904 if (code == OMP_TASKLOOP)
7906 if (!taskloop_iv_seen)
7908 tree ivc = build_omp_clause (locus, OMP_CLAUSE_FIRSTPRIVATE);
7909 OMP_CLAUSE_DECL (ivc) = iter;
7910 cxx_omp_finish_clause (ivc, NULL);
7911 OMP_CLAUSE_CHAIN (ivc) = clauses;
7912 clauses = ivc;
7914 tree lvc = build_omp_clause (locus, OMP_CLAUSE_FIRSTPRIVATE);
7915 OMP_CLAUSE_DECL (lvc) = last;
7916 OMP_CLAUSE_CHAIN (lvc) = clauses;
7917 clauses = lvc;
7918 diffvar = create_temporary_var (TREE_TYPE (diff));
7919 pushdecl (diffvar);
7920 add_decl_expr (diffvar);
7923 orig_pre_body = *pre_body;
7924 *pre_body = push_stmt_list ();
7925 if (orig_pre_body)
7926 add_stmt (orig_pre_body);
7927 if (init != NULL)
7928 finish_expr_stmt (build_x_modify_expr (elocus,
7929 iter, NOP_EXPR, init,
7930 tf_warning_or_error));
7931 init = build_int_cst (TREE_TYPE (diff), 0);
7932 if (c && iter_incr == NULL
7933 && (!ordered || (i < collapse && collapse > 1)))
7935 if (incr_var)
7937 finish_expr_stmt (build_x_modify_expr (elocus,
7938 incr_var, NOP_EXPR,
7939 incr, tf_warning_or_error));
7940 incr = incr_var;
7942 iter_incr = build_x_modify_expr (elocus,
7943 iter, PLUS_EXPR, incr,
7944 tf_warning_or_error);
7946 if (c && ordered && i < collapse && collapse > 1)
7947 iter_incr = incr;
7948 finish_expr_stmt (build_x_modify_expr (elocus,
7949 last, NOP_EXPR, init,
7950 tf_warning_or_error));
7951 if (diffvar)
7953 finish_expr_stmt (build_x_modify_expr (elocus,
7954 diffvar, NOP_EXPR,
7955 diff, tf_warning_or_error));
7956 diff = diffvar;
7958 *pre_body = pop_stmt_list (*pre_body);
7960 cond = cp_build_binary_op (elocus,
7961 TREE_CODE (cond), decl, diff,
7962 tf_warning_or_error);
7963 incr = build_modify_expr (elocus, decl, NULL_TREE, PLUS_EXPR,
7964 elocus, incr, NULL_TREE);
7966 orig_body = *body;
7967 *body = push_stmt_list ();
7968 iter_init = build2 (MINUS_EXPR, TREE_TYPE (diff), decl, last);
7969 iter_init = build_x_modify_expr (elocus,
7970 iter, PLUS_EXPR, iter_init,
7971 tf_warning_or_error);
7972 if (iter_init != error_mark_node)
7973 iter_init = build1 (NOP_EXPR, void_type_node, iter_init);
7974 finish_expr_stmt (iter_init);
7975 finish_expr_stmt (build_x_modify_expr (elocus,
7976 last, NOP_EXPR, decl,
7977 tf_warning_or_error));
7978 add_stmt (orig_body);
7979 *body = pop_stmt_list (*body);
7981 if (c)
7983 OMP_CLAUSE_LASTPRIVATE_STMT (c) = push_stmt_list ();
7984 if (!ordered)
7985 finish_expr_stmt (iter_incr);
7986 else
7988 iter_init = decl;
7989 if (i < collapse && collapse > 1 && !error_operand_p (iter_incr))
7990 iter_init = build2 (PLUS_EXPR, TREE_TYPE (diff),
7991 iter_init, iter_incr);
7992 iter_init = build2 (MINUS_EXPR, TREE_TYPE (diff), iter_init, last);
7993 iter_init = build_x_modify_expr (elocus,
7994 iter, PLUS_EXPR, iter_init,
7995 tf_warning_or_error);
7996 if (iter_init != error_mark_node)
7997 iter_init = build1 (NOP_EXPR, void_type_node, iter_init);
7998 finish_expr_stmt (iter_init);
8000 OMP_CLAUSE_LASTPRIVATE_STMT (c)
8001 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (c));
8004 TREE_VEC_ELT (declv, i) = decl;
8005 TREE_VEC_ELT (initv, i) = init;
8006 TREE_VEC_ELT (condv, i) = cond;
8007 TREE_VEC_ELT (incrv, i) = incr;
8008 *lastp = last;
8010 return false;
8013 /* Build and validate an OMP_FOR statement. CLAUSES, BODY, COND, INCR
8014 are directly for their associated operands in the statement. DECL
8015 and INIT are a combo; if DECL is NULL then INIT ought to be a
8016 MODIFY_EXPR, and the DECL should be extracted. PRE_BODY are
8017 optional statements that need to go before the loop into its
8018 sk_omp scope. */
8020 tree
8021 finish_omp_for (location_t locus, enum tree_code code, tree declv,
8022 tree orig_declv, tree initv, tree condv, tree incrv,
8023 tree body, tree pre_body, vec<tree> *orig_inits, tree clauses)
8025 tree omp_for = NULL, orig_incr = NULL;
8026 tree decl = NULL, init, cond, incr, orig_decl = NULL_TREE, block = NULL_TREE;
8027 tree last = NULL_TREE;
8028 location_t elocus;
8029 int i;
8030 int collapse = 1;
8031 int ordered = 0;
8033 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (initv));
8034 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (condv));
8035 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (incrv));
8036 if (TREE_VEC_LENGTH (declv) > 1)
8038 tree c;
8040 c = omp_find_clause (clauses, OMP_CLAUSE_TILE);
8041 if (c)
8042 collapse = list_length (OMP_CLAUSE_TILE_LIST (c));
8043 else
8045 c = omp_find_clause (clauses, OMP_CLAUSE_COLLAPSE);
8046 if (c)
8047 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (c));
8048 if (collapse != TREE_VEC_LENGTH (declv))
8049 ordered = TREE_VEC_LENGTH (declv);
8052 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
8054 decl = TREE_VEC_ELT (declv, i);
8055 init = TREE_VEC_ELT (initv, i);
8056 cond = TREE_VEC_ELT (condv, i);
8057 incr = TREE_VEC_ELT (incrv, i);
8058 elocus = locus;
8060 if (decl == NULL)
8062 if (init != NULL)
8063 switch (TREE_CODE (init))
8065 case MODIFY_EXPR:
8066 decl = TREE_OPERAND (init, 0);
8067 init = TREE_OPERAND (init, 1);
8068 break;
8069 case MODOP_EXPR:
8070 if (TREE_CODE (TREE_OPERAND (init, 1)) == NOP_EXPR)
8072 decl = TREE_OPERAND (init, 0);
8073 init = TREE_OPERAND (init, 2);
8075 break;
8076 default:
8077 break;
8080 if (decl == NULL)
8082 error_at (locus,
8083 "expected iteration declaration or initialization");
8084 return NULL;
8088 if (init && EXPR_HAS_LOCATION (init))
8089 elocus = EXPR_LOCATION (init);
8091 if (cond == NULL)
8093 error_at (elocus, "missing controlling predicate");
8094 return NULL;
8097 if (incr == NULL)
8099 error_at (elocus, "missing increment expression");
8100 return NULL;
8103 TREE_VEC_ELT (declv, i) = decl;
8104 TREE_VEC_ELT (initv, i) = init;
8107 if (orig_inits)
8109 bool fail = false;
8110 tree orig_init;
8111 FOR_EACH_VEC_ELT (*orig_inits, i, orig_init)
8112 if (orig_init
8113 && !c_omp_check_loop_iv_exprs (locus, declv,
8114 TREE_VEC_ELT (declv, i), orig_init,
8115 NULL_TREE, cp_walk_subtrees))
8116 fail = true;
8117 if (fail)
8118 return NULL;
8121 if (dependent_omp_for_p (declv, initv, condv, incrv))
8123 tree stmt;
8125 stmt = make_node (code);
8127 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
8129 /* This is really just a place-holder. We'll be decomposing this
8130 again and going through the cp_build_modify_expr path below when
8131 we instantiate the thing. */
8132 TREE_VEC_ELT (initv, i)
8133 = build2 (MODIFY_EXPR, void_type_node, TREE_VEC_ELT (declv, i),
8134 TREE_VEC_ELT (initv, i));
8137 TREE_TYPE (stmt) = void_type_node;
8138 OMP_FOR_INIT (stmt) = initv;
8139 OMP_FOR_COND (stmt) = condv;
8140 OMP_FOR_INCR (stmt) = incrv;
8141 OMP_FOR_BODY (stmt) = body;
8142 OMP_FOR_PRE_BODY (stmt) = pre_body;
8143 OMP_FOR_CLAUSES (stmt) = clauses;
8145 SET_EXPR_LOCATION (stmt, locus);
8146 return add_stmt (stmt);
8149 if (!orig_declv)
8150 orig_declv = copy_node (declv);
8152 if (processing_template_decl)
8153 orig_incr = make_tree_vec (TREE_VEC_LENGTH (incrv));
8155 for (i = 0; i < TREE_VEC_LENGTH (declv); )
8157 decl = TREE_VEC_ELT (declv, i);
8158 init = TREE_VEC_ELT (initv, i);
8159 cond = TREE_VEC_ELT (condv, i);
8160 incr = TREE_VEC_ELT (incrv, i);
8161 if (orig_incr)
8162 TREE_VEC_ELT (orig_incr, i) = incr;
8163 elocus = locus;
8165 if (init && EXPR_HAS_LOCATION (init))
8166 elocus = EXPR_LOCATION (init);
8168 if (!DECL_P (decl))
8170 error_at (elocus, "expected iteration declaration or initialization");
8171 return NULL;
8174 if (incr && TREE_CODE (incr) == MODOP_EXPR)
8176 if (orig_incr)
8177 TREE_VEC_ELT (orig_incr, i) = incr;
8178 incr = cp_build_modify_expr (elocus, TREE_OPERAND (incr, 0),
8179 TREE_CODE (TREE_OPERAND (incr, 1)),
8180 TREE_OPERAND (incr, 2),
8181 tf_warning_or_error);
8184 if (CLASS_TYPE_P (TREE_TYPE (decl)))
8186 if (code == OMP_SIMD)
8188 error_at (elocus, "%<#pragma omp simd%> used with class "
8189 "iteration variable %qE", decl);
8190 return NULL;
8192 if (code == CILK_FOR && i == 0)
8193 orig_decl = decl;
8194 if (handle_omp_for_class_iterator (i, locus, code, declv, orig_declv,
8195 initv, condv, incrv, &body,
8196 &pre_body, clauses, &last,
8197 collapse, ordered))
8198 return NULL;
8199 continue;
8202 if (!INTEGRAL_TYPE_P (TREE_TYPE (decl))
8203 && !TYPE_PTR_P (TREE_TYPE (decl)))
8205 error_at (elocus, "invalid type for iteration variable %qE", decl);
8206 return NULL;
8209 if (!processing_template_decl)
8211 init = fold_build_cleanup_point_expr (TREE_TYPE (init), init);
8212 init = cp_build_modify_expr (elocus, decl, NOP_EXPR, init,
8213 tf_warning_or_error);
8215 else
8216 init = build2 (MODIFY_EXPR, void_type_node, decl, init);
8217 if (cond
8218 && TREE_SIDE_EFFECTS (cond)
8219 && COMPARISON_CLASS_P (cond)
8220 && !processing_template_decl)
8222 tree t = TREE_OPERAND (cond, 0);
8223 if (TREE_SIDE_EFFECTS (t)
8224 && t != decl
8225 && (TREE_CODE (t) != NOP_EXPR
8226 || TREE_OPERAND (t, 0) != decl))
8227 TREE_OPERAND (cond, 0)
8228 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
8230 t = TREE_OPERAND (cond, 1);
8231 if (TREE_SIDE_EFFECTS (t)
8232 && t != decl
8233 && (TREE_CODE (t) != NOP_EXPR
8234 || TREE_OPERAND (t, 0) != decl))
8235 TREE_OPERAND (cond, 1)
8236 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
8238 if (decl == error_mark_node || init == error_mark_node)
8239 return NULL;
8241 TREE_VEC_ELT (declv, i) = decl;
8242 TREE_VEC_ELT (initv, i) = init;
8243 TREE_VEC_ELT (condv, i) = cond;
8244 TREE_VEC_ELT (incrv, i) = incr;
8245 i++;
8248 if (IS_EMPTY_STMT (pre_body))
8249 pre_body = NULL;
8251 if (code == CILK_FOR && !processing_template_decl)
8252 block = push_stmt_list ();
8254 omp_for = c_finish_omp_for (locus, code, declv, orig_declv, initv, condv,
8255 incrv, body, pre_body);
8257 /* Check for iterators appearing in lb, b or incr expressions. */
8258 if (omp_for && !c_omp_check_loop_iv (omp_for, orig_declv, cp_walk_subtrees))
8259 omp_for = NULL_TREE;
8261 if (omp_for == NULL)
8263 if (block)
8264 pop_stmt_list (block);
8265 return NULL;
8268 add_stmt (omp_for);
8270 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INCR (omp_for)); i++)
8272 decl = TREE_OPERAND (TREE_VEC_ELT (OMP_FOR_INIT (omp_for), i), 0);
8273 incr = TREE_VEC_ELT (OMP_FOR_INCR (omp_for), i);
8275 if (TREE_CODE (incr) != MODIFY_EXPR)
8276 continue;
8278 if (TREE_SIDE_EFFECTS (TREE_OPERAND (incr, 1))
8279 && BINARY_CLASS_P (TREE_OPERAND (incr, 1))
8280 && !processing_template_decl)
8282 tree t = TREE_OPERAND (TREE_OPERAND (incr, 1), 0);
8283 if (TREE_SIDE_EFFECTS (t)
8284 && t != decl
8285 && (TREE_CODE (t) != NOP_EXPR
8286 || TREE_OPERAND (t, 0) != decl))
8287 TREE_OPERAND (TREE_OPERAND (incr, 1), 0)
8288 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
8290 t = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
8291 if (TREE_SIDE_EFFECTS (t)
8292 && t != decl
8293 && (TREE_CODE (t) != NOP_EXPR
8294 || TREE_OPERAND (t, 0) != decl))
8295 TREE_OPERAND (TREE_OPERAND (incr, 1), 1)
8296 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
8299 if (orig_incr)
8300 TREE_VEC_ELT (OMP_FOR_INCR (omp_for), i) = TREE_VEC_ELT (orig_incr, i);
8302 OMP_FOR_CLAUSES (omp_for) = clauses;
8304 /* For simd loops with non-static data member iterators, we could have added
8305 OMP_CLAUSE_LINEAR clauses without OMP_CLAUSE_LINEAR_STEP. As we know the
8306 step at this point, fill it in. */
8307 if (code == OMP_SIMD && !processing_template_decl
8308 && TREE_VEC_LENGTH (OMP_FOR_INCR (omp_for)) == 1)
8309 for (tree c = omp_find_clause (clauses, OMP_CLAUSE_LINEAR); c;
8310 c = omp_find_clause (OMP_CLAUSE_CHAIN (c), OMP_CLAUSE_LINEAR))
8311 if (OMP_CLAUSE_LINEAR_STEP (c) == NULL_TREE)
8313 decl = TREE_OPERAND (TREE_VEC_ELT (OMP_FOR_INIT (omp_for), 0), 0);
8314 gcc_assert (decl == OMP_CLAUSE_DECL (c));
8315 incr = TREE_VEC_ELT (OMP_FOR_INCR (omp_for), 0);
8316 tree step, stept;
8317 switch (TREE_CODE (incr))
8319 case PREINCREMENT_EXPR:
8320 case POSTINCREMENT_EXPR:
8321 /* c_omp_for_incr_canonicalize_ptr() should have been
8322 called to massage things appropriately. */
8323 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (decl)));
8324 OMP_CLAUSE_LINEAR_STEP (c) = build_int_cst (TREE_TYPE (decl), 1);
8325 break;
8326 case PREDECREMENT_EXPR:
8327 case POSTDECREMENT_EXPR:
8328 /* c_omp_for_incr_canonicalize_ptr() should have been
8329 called to massage things appropriately. */
8330 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (decl)));
8331 OMP_CLAUSE_LINEAR_STEP (c)
8332 = build_int_cst (TREE_TYPE (decl), -1);
8333 break;
8334 case MODIFY_EXPR:
8335 gcc_assert (TREE_OPERAND (incr, 0) == decl);
8336 incr = TREE_OPERAND (incr, 1);
8337 switch (TREE_CODE (incr))
8339 case PLUS_EXPR:
8340 if (TREE_OPERAND (incr, 1) == decl)
8341 step = TREE_OPERAND (incr, 0);
8342 else
8343 step = TREE_OPERAND (incr, 1);
8344 break;
8345 case MINUS_EXPR:
8346 case POINTER_PLUS_EXPR:
8347 gcc_assert (TREE_OPERAND (incr, 0) == decl);
8348 step = TREE_OPERAND (incr, 1);
8349 break;
8350 default:
8351 gcc_unreachable ();
8353 stept = TREE_TYPE (decl);
8354 if (POINTER_TYPE_P (stept))
8355 stept = sizetype;
8356 step = fold_convert (stept, step);
8357 if (TREE_CODE (incr) == MINUS_EXPR)
8358 step = fold_build1 (NEGATE_EXPR, stept, step);
8359 OMP_CLAUSE_LINEAR_STEP (c) = step;
8360 break;
8361 default:
8362 gcc_unreachable ();
8366 if (block)
8368 tree omp_par = make_node (OMP_PARALLEL);
8369 TREE_TYPE (omp_par) = void_type_node;
8370 OMP_PARALLEL_CLAUSES (omp_par) = NULL_TREE;
8371 tree bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
8372 TREE_SIDE_EFFECTS (bind) = 1;
8373 BIND_EXPR_BODY (bind) = pop_stmt_list (block);
8374 OMP_PARALLEL_BODY (omp_par) = bind;
8375 if (OMP_FOR_PRE_BODY (omp_for))
8377 add_stmt (OMP_FOR_PRE_BODY (omp_for));
8378 OMP_FOR_PRE_BODY (omp_for) = NULL_TREE;
8380 init = TREE_VEC_ELT (OMP_FOR_INIT (omp_for), 0);
8381 decl = TREE_OPERAND (init, 0);
8382 cond = TREE_VEC_ELT (OMP_FOR_COND (omp_for), 0);
8383 incr = TREE_VEC_ELT (OMP_FOR_INCR (omp_for), 0);
8384 tree t = TREE_OPERAND (cond, 1), c, clauses, *pc;
8385 clauses = OMP_FOR_CLAUSES (omp_for);
8386 OMP_FOR_CLAUSES (omp_for) = NULL_TREE;
8387 for (pc = &clauses; *pc; )
8388 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_SCHEDULE)
8390 gcc_assert (OMP_FOR_CLAUSES (omp_for) == NULL_TREE);
8391 OMP_FOR_CLAUSES (omp_for) = *pc;
8392 *pc = OMP_CLAUSE_CHAIN (*pc);
8393 OMP_CLAUSE_CHAIN (OMP_FOR_CLAUSES (omp_for)) = NULL_TREE;
8395 else
8397 gcc_assert (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_FIRSTPRIVATE);
8398 pc = &OMP_CLAUSE_CHAIN (*pc);
8400 if (TREE_CODE (t) != INTEGER_CST)
8402 TREE_OPERAND (cond, 1) = get_temp_regvar (TREE_TYPE (t), t);
8403 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
8404 OMP_CLAUSE_DECL (c) = TREE_OPERAND (cond, 1);
8405 OMP_CLAUSE_CHAIN (c) = clauses;
8406 clauses = c;
8408 if (TREE_CODE (incr) == MODIFY_EXPR)
8410 t = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
8411 if (TREE_CODE (t) != INTEGER_CST)
8413 TREE_OPERAND (TREE_OPERAND (incr, 1), 1)
8414 = get_temp_regvar (TREE_TYPE (t), t);
8415 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
8416 OMP_CLAUSE_DECL (c) = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
8417 OMP_CLAUSE_CHAIN (c) = clauses;
8418 clauses = c;
8421 t = TREE_OPERAND (init, 1);
8422 if (TREE_CODE (t) != INTEGER_CST)
8424 TREE_OPERAND (init, 1) = get_temp_regvar (TREE_TYPE (t), t);
8425 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
8426 OMP_CLAUSE_DECL (c) = TREE_OPERAND (init, 1);
8427 OMP_CLAUSE_CHAIN (c) = clauses;
8428 clauses = c;
8430 if (orig_decl && orig_decl != decl)
8432 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
8433 OMP_CLAUSE_DECL (c) = orig_decl;
8434 OMP_CLAUSE_CHAIN (c) = clauses;
8435 clauses = c;
8437 if (last)
8439 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
8440 OMP_CLAUSE_DECL (c) = last;
8441 OMP_CLAUSE_CHAIN (c) = clauses;
8442 clauses = c;
8444 c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
8445 OMP_CLAUSE_DECL (c) = decl;
8446 OMP_CLAUSE_CHAIN (c) = clauses;
8447 clauses = c;
8448 c = build_omp_clause (input_location, OMP_CLAUSE__CILK_FOR_COUNT_);
8449 OMP_CLAUSE_OPERAND (c, 0)
8450 = cilk_for_number_of_iterations (omp_for);
8451 OMP_CLAUSE_CHAIN (c) = clauses;
8452 OMP_PARALLEL_CLAUSES (omp_par) = finish_omp_clauses (c, C_ORT_CILK);
8453 add_stmt (omp_par);
8454 return omp_par;
8456 else if (code == CILK_FOR && processing_template_decl)
8458 tree c, clauses = OMP_FOR_CLAUSES (omp_for);
8459 if (orig_decl && orig_decl != decl)
8461 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
8462 OMP_CLAUSE_DECL (c) = orig_decl;
8463 OMP_CLAUSE_CHAIN (c) = clauses;
8464 clauses = c;
8466 if (last)
8468 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
8469 OMP_CLAUSE_DECL (c) = last;
8470 OMP_CLAUSE_CHAIN (c) = clauses;
8471 clauses = c;
8473 OMP_FOR_CLAUSES (omp_for) = clauses;
8475 return omp_for;
8478 void
8479 finish_omp_atomic (enum tree_code code, enum tree_code opcode, tree lhs,
8480 tree rhs, tree v, tree lhs1, tree rhs1, bool seq_cst)
8482 tree orig_lhs;
8483 tree orig_rhs;
8484 tree orig_v;
8485 tree orig_lhs1;
8486 tree orig_rhs1;
8487 bool dependent_p;
8488 tree stmt;
8490 orig_lhs = lhs;
8491 orig_rhs = rhs;
8492 orig_v = v;
8493 orig_lhs1 = lhs1;
8494 orig_rhs1 = rhs1;
8495 dependent_p = false;
8496 stmt = NULL_TREE;
8498 /* Even in a template, we can detect invalid uses of the atomic
8499 pragma if neither LHS nor RHS is type-dependent. */
8500 if (processing_template_decl)
8502 dependent_p = (type_dependent_expression_p (lhs)
8503 || (rhs && type_dependent_expression_p (rhs))
8504 || (v && type_dependent_expression_p (v))
8505 || (lhs1 && type_dependent_expression_p (lhs1))
8506 || (rhs1 && type_dependent_expression_p (rhs1)));
8507 if (!dependent_p)
8509 lhs = build_non_dependent_expr (lhs);
8510 if (rhs)
8511 rhs = build_non_dependent_expr (rhs);
8512 if (v)
8513 v = build_non_dependent_expr (v);
8514 if (lhs1)
8515 lhs1 = build_non_dependent_expr (lhs1);
8516 if (rhs1)
8517 rhs1 = build_non_dependent_expr (rhs1);
8520 if (!dependent_p)
8522 bool swapped = false;
8523 if (rhs1 && cp_tree_equal (lhs, rhs))
8525 std::swap (rhs, rhs1);
8526 swapped = !commutative_tree_code (opcode);
8528 if (rhs1 && !cp_tree_equal (lhs, rhs1))
8530 if (code == OMP_ATOMIC)
8531 error ("%<#pragma omp atomic update%> uses two different "
8532 "expressions for memory");
8533 else
8534 error ("%<#pragma omp atomic capture%> uses two different "
8535 "expressions for memory");
8536 return;
8538 if (lhs1 && !cp_tree_equal (lhs, lhs1))
8540 if (code == OMP_ATOMIC)
8541 error ("%<#pragma omp atomic update%> uses two different "
8542 "expressions for memory");
8543 else
8544 error ("%<#pragma omp atomic capture%> uses two different "
8545 "expressions for memory");
8546 return;
8548 stmt = c_finish_omp_atomic (input_location, code, opcode, lhs, rhs,
8549 v, lhs1, rhs1, swapped, seq_cst,
8550 processing_template_decl != 0);
8551 if (stmt == error_mark_node)
8552 return;
8554 if (processing_template_decl)
8556 if (code == OMP_ATOMIC_READ)
8558 stmt = build_min_nt_loc (EXPR_LOCATION (orig_lhs),
8559 OMP_ATOMIC_READ, orig_lhs);
8560 OMP_ATOMIC_SEQ_CST (stmt) = seq_cst;
8561 stmt = build2 (MODIFY_EXPR, void_type_node, orig_v, stmt);
8563 else
8565 if (opcode == NOP_EXPR)
8566 stmt = build2 (MODIFY_EXPR, void_type_node, orig_lhs, orig_rhs);
8567 else
8568 stmt = build2 (opcode, void_type_node, orig_lhs, orig_rhs);
8569 if (orig_rhs1)
8570 stmt = build_min_nt_loc (EXPR_LOCATION (orig_rhs1),
8571 COMPOUND_EXPR, orig_rhs1, stmt);
8572 if (code != OMP_ATOMIC)
8574 stmt = build_min_nt_loc (EXPR_LOCATION (orig_lhs1),
8575 code, orig_lhs1, stmt);
8576 OMP_ATOMIC_SEQ_CST (stmt) = seq_cst;
8577 stmt = build2 (MODIFY_EXPR, void_type_node, orig_v, stmt);
8580 stmt = build2 (OMP_ATOMIC, void_type_node, integer_zero_node, stmt);
8581 OMP_ATOMIC_SEQ_CST (stmt) = seq_cst;
8583 finish_expr_stmt (stmt);
8586 void
8587 finish_omp_barrier (void)
8589 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_BARRIER);
8590 vec<tree, va_gc> *vec = make_tree_vector ();
8591 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
8592 release_tree_vector (vec);
8593 finish_expr_stmt (stmt);
8596 void
8597 finish_omp_flush (void)
8599 tree fn = builtin_decl_explicit (BUILT_IN_SYNC_SYNCHRONIZE);
8600 vec<tree, va_gc> *vec = make_tree_vector ();
8601 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
8602 release_tree_vector (vec);
8603 finish_expr_stmt (stmt);
8606 void
8607 finish_omp_taskwait (void)
8609 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_TASKWAIT);
8610 vec<tree, va_gc> *vec = make_tree_vector ();
8611 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
8612 release_tree_vector (vec);
8613 finish_expr_stmt (stmt);
8616 void
8617 finish_omp_taskyield (void)
8619 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_TASKYIELD);
8620 vec<tree, va_gc> *vec = make_tree_vector ();
8621 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
8622 release_tree_vector (vec);
8623 finish_expr_stmt (stmt);
8626 void
8627 finish_omp_cancel (tree clauses)
8629 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
8630 int mask = 0;
8631 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
8632 mask = 1;
8633 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
8634 mask = 2;
8635 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
8636 mask = 4;
8637 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
8638 mask = 8;
8639 else
8641 error ("%<#pragma omp cancel%> must specify one of "
8642 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> clauses");
8643 return;
8645 vec<tree, va_gc> *vec = make_tree_vector ();
8646 tree ifc = omp_find_clause (clauses, OMP_CLAUSE_IF);
8647 if (ifc != NULL_TREE)
8649 tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
8650 ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
8651 boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
8652 build_zero_cst (type));
8654 else
8655 ifc = boolean_true_node;
8656 vec->quick_push (build_int_cst (integer_type_node, mask));
8657 vec->quick_push (ifc);
8658 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
8659 release_tree_vector (vec);
8660 finish_expr_stmt (stmt);
8663 void
8664 finish_omp_cancellation_point (tree clauses)
8666 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
8667 int mask = 0;
8668 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
8669 mask = 1;
8670 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
8671 mask = 2;
8672 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
8673 mask = 4;
8674 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
8675 mask = 8;
8676 else
8678 error ("%<#pragma omp cancellation point%> must specify one of "
8679 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> clauses");
8680 return;
8682 vec<tree, va_gc> *vec
8683 = make_tree_vector_single (build_int_cst (integer_type_node, mask));
8684 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
8685 release_tree_vector (vec);
8686 finish_expr_stmt (stmt);
8689 /* Begin a __transaction_atomic or __transaction_relaxed statement.
8690 If PCOMPOUND is non-null, this is for a function-transaction-block, and we
8691 should create an extra compound stmt. */
8693 tree
8694 begin_transaction_stmt (location_t loc, tree *pcompound, int flags)
8696 tree r;
8698 if (pcompound)
8699 *pcompound = begin_compound_stmt (0);
8701 r = build_stmt (loc, TRANSACTION_EXPR, NULL_TREE);
8703 /* Only add the statement to the function if support enabled. */
8704 if (flag_tm)
8705 add_stmt (r);
8706 else
8707 error_at (loc, ((flags & TM_STMT_ATTR_RELAXED) != 0
8708 ? G_("%<__transaction_relaxed%> without "
8709 "transactional memory support enabled")
8710 : G_("%<__transaction_atomic%> without "
8711 "transactional memory support enabled")));
8713 TRANSACTION_EXPR_BODY (r) = push_stmt_list ();
8714 TREE_SIDE_EFFECTS (r) = 1;
8715 return r;
8718 /* End a __transaction_atomic or __transaction_relaxed statement.
8719 If COMPOUND_STMT is non-null, this is for a function-transaction-block,
8720 and we should end the compound. If NOEX is non-NULL, we wrap the body in
8721 a MUST_NOT_THROW_EXPR with NOEX as condition. */
8723 void
8724 finish_transaction_stmt (tree stmt, tree compound_stmt, int flags, tree noex)
8726 TRANSACTION_EXPR_BODY (stmt) = pop_stmt_list (TRANSACTION_EXPR_BODY (stmt));
8727 TRANSACTION_EXPR_OUTER (stmt) = (flags & TM_STMT_ATTR_OUTER) != 0;
8728 TRANSACTION_EXPR_RELAXED (stmt) = (flags & TM_STMT_ATTR_RELAXED) != 0;
8729 TRANSACTION_EXPR_IS_STMT (stmt) = 1;
8731 /* noexcept specifications are not allowed for function transactions. */
8732 gcc_assert (!(noex && compound_stmt));
8733 if (noex)
8735 tree body = build_must_not_throw_expr (TRANSACTION_EXPR_BODY (stmt),
8736 noex);
8737 protected_set_expr_location
8738 (body, EXPR_LOCATION (TRANSACTION_EXPR_BODY (stmt)));
8739 TREE_SIDE_EFFECTS (body) = 1;
8740 TRANSACTION_EXPR_BODY (stmt) = body;
8743 if (compound_stmt)
8744 finish_compound_stmt (compound_stmt);
8747 /* Build a __transaction_atomic or __transaction_relaxed expression. If
8748 NOEX is non-NULL, we wrap the body in a MUST_NOT_THROW_EXPR with NOEX as
8749 condition. */
8751 tree
8752 build_transaction_expr (location_t loc, tree expr, int flags, tree noex)
8754 tree ret;
8755 if (noex)
8757 expr = build_must_not_throw_expr (expr, noex);
8758 protected_set_expr_location (expr, loc);
8759 TREE_SIDE_EFFECTS (expr) = 1;
8761 ret = build1 (TRANSACTION_EXPR, TREE_TYPE (expr), expr);
8762 if (flags & TM_STMT_ATTR_RELAXED)
8763 TRANSACTION_EXPR_RELAXED (ret) = 1;
8764 TREE_SIDE_EFFECTS (ret) = 1;
8765 SET_EXPR_LOCATION (ret, loc);
8766 return ret;
8769 void
8770 init_cp_semantics (void)
8774 /* Build a STATIC_ASSERT for a static assertion with the condition
8775 CONDITION and the message text MESSAGE. LOCATION is the location
8776 of the static assertion in the source code. When MEMBER_P, this
8777 static assertion is a member of a class. */
8778 void
8779 finish_static_assert (tree condition, tree message, location_t location,
8780 bool member_p)
8782 if (message == NULL_TREE
8783 || message == error_mark_node
8784 || condition == NULL_TREE
8785 || condition == error_mark_node)
8786 return;
8788 if (check_for_bare_parameter_packs (condition))
8789 condition = error_mark_node;
8791 if (type_dependent_expression_p (condition)
8792 || value_dependent_expression_p (condition))
8794 /* We're in a template; build a STATIC_ASSERT and put it in
8795 the right place. */
8796 tree assertion;
8798 assertion = make_node (STATIC_ASSERT);
8799 STATIC_ASSERT_CONDITION (assertion) = condition;
8800 STATIC_ASSERT_MESSAGE (assertion) = message;
8801 STATIC_ASSERT_SOURCE_LOCATION (assertion) = location;
8803 if (member_p)
8804 maybe_add_class_template_decl_list (current_class_type,
8805 assertion,
8806 /*friend_p=*/0);
8807 else
8808 add_stmt (assertion);
8810 return;
8813 /* Fold the expression and convert it to a boolean value. */
8814 condition = instantiate_non_dependent_expr (condition);
8815 condition = cp_convert (boolean_type_node, condition, tf_warning_or_error);
8816 condition = maybe_constant_value (condition);
8818 if (TREE_CODE (condition) == INTEGER_CST && !integer_zerop (condition))
8819 /* Do nothing; the condition is satisfied. */
8821 else
8823 location_t saved_loc = input_location;
8825 input_location = location;
8826 if (TREE_CODE (condition) == INTEGER_CST
8827 && integer_zerop (condition))
8829 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
8830 (TREE_TYPE (TREE_TYPE (message))));
8831 int len = TREE_STRING_LENGTH (message) / sz - 1;
8832 /* Report the error. */
8833 if (len == 0)
8834 error ("static assertion failed");
8835 else
8836 error ("static assertion failed: %s",
8837 TREE_STRING_POINTER (message));
8839 else if (condition && condition != error_mark_node)
8841 error ("non-constant condition for static assertion");
8842 if (require_potential_rvalue_constant_expression (condition))
8843 cxx_constant_value (condition);
8845 input_location = saved_loc;
8849 /* Implements the C++0x decltype keyword. Returns the type of EXPR,
8850 suitable for use as a type-specifier.
8852 ID_EXPRESSION_OR_MEMBER_ACCESS_P is true when EXPR was parsed as an
8853 id-expression or a class member access, FALSE when it was parsed as
8854 a full expression. */
8856 tree
8857 finish_decltype_type (tree expr, bool id_expression_or_member_access_p,
8858 tsubst_flags_t complain)
8860 tree type = NULL_TREE;
8862 if (!expr || error_operand_p (expr))
8863 return error_mark_node;
8865 if (TYPE_P (expr)
8866 || TREE_CODE (expr) == TYPE_DECL
8867 || (TREE_CODE (expr) == BIT_NOT_EXPR
8868 && TYPE_P (TREE_OPERAND (expr, 0))))
8870 if (complain & tf_error)
8871 error ("argument to decltype must be an expression");
8872 return error_mark_node;
8875 /* Depending on the resolution of DR 1172, we may later need to distinguish
8876 instantiation-dependent but not type-dependent expressions so that, say,
8877 A<decltype(sizeof(T))>::U doesn't require 'typename'. */
8878 if (instantiation_dependent_uneval_expression_p (expr))
8880 type = cxx_make_type (DECLTYPE_TYPE);
8881 DECLTYPE_TYPE_EXPR (type) = expr;
8882 DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (type)
8883 = id_expression_or_member_access_p;
8884 SET_TYPE_STRUCTURAL_EQUALITY (type);
8886 return type;
8889 /* The type denoted by decltype(e) is defined as follows: */
8891 expr = resolve_nondeduced_context (expr, complain);
8893 if (invalid_nonstatic_memfn_p (input_location, expr, complain))
8894 return error_mark_node;
8896 if (type_unknown_p (expr))
8898 if (complain & tf_error)
8899 error ("decltype cannot resolve address of overloaded function");
8900 return error_mark_node;
8903 /* To get the size of a static data member declared as an array of
8904 unknown bound, we need to instantiate it. */
8905 if (VAR_P (expr)
8906 && VAR_HAD_UNKNOWN_BOUND (expr)
8907 && DECL_TEMPLATE_INSTANTIATION (expr))
8908 instantiate_decl (expr, /*defer_ok*/true, /*expl_inst_mem*/false);
8910 if (id_expression_or_member_access_p)
8912 /* If e is an id-expression or a class member access (5.2.5
8913 [expr.ref]), decltype(e) is defined as the type of the entity
8914 named by e. If there is no such entity, or e names a set of
8915 overloaded functions, the program is ill-formed. */
8916 if (identifier_p (expr))
8917 expr = lookup_name (expr);
8919 if (INDIRECT_REF_P (expr))
8920 /* This can happen when the expression is, e.g., "a.b". Just
8921 look at the underlying operand. */
8922 expr = TREE_OPERAND (expr, 0);
8924 if (TREE_CODE (expr) == OFFSET_REF
8925 || TREE_CODE (expr) == MEMBER_REF
8926 || TREE_CODE (expr) == SCOPE_REF)
8927 /* We're only interested in the field itself. If it is a
8928 BASELINK, we will need to see through it in the next
8929 step. */
8930 expr = TREE_OPERAND (expr, 1);
8932 if (BASELINK_P (expr))
8933 /* See through BASELINK nodes to the underlying function. */
8934 expr = BASELINK_FUNCTIONS (expr);
8936 /* decltype of a decomposition name drops references in the tuple case
8937 (unlike decltype of a normal variable) and keeps cv-qualifiers from
8938 the containing object in the other cases (unlike decltype of a member
8939 access expression). */
8940 if (DECL_DECOMPOSITION_P (expr))
8942 if (DECL_HAS_VALUE_EXPR_P (expr))
8943 /* Expr is an array or struct subobject proxy, handle
8944 bit-fields properly. */
8945 return unlowered_expr_type (expr);
8946 else
8947 /* Expr is a reference variable for the tuple case. */
8948 return lookup_decomp_type (expr);
8951 switch (TREE_CODE (expr))
8953 case FIELD_DECL:
8954 if (DECL_BIT_FIELD_TYPE (expr))
8956 type = DECL_BIT_FIELD_TYPE (expr);
8957 break;
8959 /* Fall through for fields that aren't bitfields. */
8960 gcc_fallthrough ();
8962 case FUNCTION_DECL:
8963 case VAR_DECL:
8964 case CONST_DECL:
8965 case PARM_DECL:
8966 case RESULT_DECL:
8967 case TEMPLATE_PARM_INDEX:
8968 expr = mark_type_use (expr);
8969 type = TREE_TYPE (expr);
8970 break;
8972 case ERROR_MARK:
8973 type = error_mark_node;
8974 break;
8976 case COMPONENT_REF:
8977 case COMPOUND_EXPR:
8978 mark_type_use (expr);
8979 type = is_bitfield_expr_with_lowered_type (expr);
8980 if (!type)
8981 type = TREE_TYPE (TREE_OPERAND (expr, 1));
8982 break;
8984 case BIT_FIELD_REF:
8985 gcc_unreachable ();
8987 case INTEGER_CST:
8988 case PTRMEM_CST:
8989 /* We can get here when the id-expression refers to an
8990 enumerator or non-type template parameter. */
8991 type = TREE_TYPE (expr);
8992 break;
8994 default:
8995 /* Handle instantiated template non-type arguments. */
8996 type = TREE_TYPE (expr);
8997 break;
9000 else
9002 /* Within a lambda-expression:
9004 Every occurrence of decltype((x)) where x is a possibly
9005 parenthesized id-expression that names an entity of
9006 automatic storage duration is treated as if x were
9007 transformed into an access to a corresponding data member
9008 of the closure type that would have been declared if x
9009 were a use of the denoted entity. */
9010 if (outer_automatic_var_p (expr)
9011 && current_function_decl
9012 && LAMBDA_FUNCTION_P (current_function_decl))
9013 type = capture_decltype (expr);
9014 else if (error_operand_p (expr))
9015 type = error_mark_node;
9016 else if (expr == current_class_ptr)
9017 /* If the expression is just "this", we want the
9018 cv-unqualified pointer for the "this" type. */
9019 type = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
9020 else
9022 /* Otherwise, where T is the type of e, if e is an lvalue,
9023 decltype(e) is defined as T&; if an xvalue, T&&; otherwise, T. */
9024 cp_lvalue_kind clk = lvalue_kind (expr);
9025 type = unlowered_expr_type (expr);
9026 gcc_assert (TREE_CODE (type) != REFERENCE_TYPE);
9028 /* For vector types, pick a non-opaque variant. */
9029 if (VECTOR_TYPE_P (type))
9030 type = strip_typedefs (type);
9032 if (clk != clk_none && !(clk & clk_class))
9033 type = cp_build_reference_type (type, (clk & clk_rvalueref));
9037 return type;
9040 /* Called from trait_expr_value to evaluate either __has_nothrow_assign or
9041 __has_nothrow_copy, depending on assign_p. */
9043 static bool
9044 classtype_has_nothrow_assign_or_copy_p (tree type, bool assign_p)
9046 tree fns;
9048 if (assign_p)
9050 int ix;
9051 ix = lookup_fnfields_1 (type, cp_assignment_operator_id (NOP_EXPR));
9052 if (ix < 0)
9053 return false;
9054 fns = (*CLASSTYPE_METHOD_VEC (type))[ix];
9056 else if (TYPE_HAS_COPY_CTOR (type))
9058 /* If construction of the copy constructor was postponed, create
9059 it now. */
9060 if (CLASSTYPE_LAZY_COPY_CTOR (type))
9061 lazily_declare_fn (sfk_copy_constructor, type);
9062 if (CLASSTYPE_LAZY_MOVE_CTOR (type))
9063 lazily_declare_fn (sfk_move_constructor, type);
9064 fns = CLASSTYPE_CONSTRUCTORS (type);
9066 else
9067 return false;
9069 for (ovl_iterator iter (fns); iter; ++iter)
9071 tree fn = *iter;
9073 if (assign_p)
9075 if (copy_fn_p (fn) == 0)
9076 continue;
9078 else if (copy_fn_p (fn) <= 0)
9079 continue;
9081 maybe_instantiate_noexcept (fn);
9082 if (!TYPE_NOTHROW_P (TREE_TYPE (fn)))
9083 return false;
9086 return true;
9089 /* Actually evaluates the trait. */
9091 static bool
9092 trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
9094 enum tree_code type_code1;
9095 tree t;
9097 type_code1 = TREE_CODE (type1);
9099 switch (kind)
9101 case CPTK_HAS_NOTHROW_ASSIGN:
9102 type1 = strip_array_types (type1);
9103 return (!CP_TYPE_CONST_P (type1) && type_code1 != REFERENCE_TYPE
9104 && (trait_expr_value (CPTK_HAS_TRIVIAL_ASSIGN, type1, type2)
9105 || (CLASS_TYPE_P (type1)
9106 && classtype_has_nothrow_assign_or_copy_p (type1,
9107 true))));
9109 case CPTK_HAS_TRIVIAL_ASSIGN:
9110 /* ??? The standard seems to be missing the "or array of such a class
9111 type" wording for this trait. */
9112 type1 = strip_array_types (type1);
9113 return (!CP_TYPE_CONST_P (type1) && type_code1 != REFERENCE_TYPE
9114 && (trivial_type_p (type1)
9115 || (CLASS_TYPE_P (type1)
9116 && TYPE_HAS_TRIVIAL_COPY_ASSIGN (type1))));
9118 case CPTK_HAS_NOTHROW_CONSTRUCTOR:
9119 type1 = strip_array_types (type1);
9120 return (trait_expr_value (CPTK_HAS_TRIVIAL_CONSTRUCTOR, type1, type2)
9121 || (CLASS_TYPE_P (type1)
9122 && (t = locate_ctor (type1))
9123 && (maybe_instantiate_noexcept (t),
9124 TYPE_NOTHROW_P (TREE_TYPE (t)))));
9126 case CPTK_HAS_TRIVIAL_CONSTRUCTOR:
9127 type1 = strip_array_types (type1);
9128 return (trivial_type_p (type1)
9129 || (CLASS_TYPE_P (type1) && TYPE_HAS_TRIVIAL_DFLT (type1)));
9131 case CPTK_HAS_NOTHROW_COPY:
9132 type1 = strip_array_types (type1);
9133 return (trait_expr_value (CPTK_HAS_TRIVIAL_COPY, type1, type2)
9134 || (CLASS_TYPE_P (type1)
9135 && classtype_has_nothrow_assign_or_copy_p (type1, false)));
9137 case CPTK_HAS_TRIVIAL_COPY:
9138 /* ??? The standard seems to be missing the "or array of such a class
9139 type" wording for this trait. */
9140 type1 = strip_array_types (type1);
9141 return (trivial_type_p (type1) || type_code1 == REFERENCE_TYPE
9142 || (CLASS_TYPE_P (type1) && TYPE_HAS_TRIVIAL_COPY_CTOR (type1)));
9144 case CPTK_HAS_TRIVIAL_DESTRUCTOR:
9145 type1 = strip_array_types (type1);
9146 return (trivial_type_p (type1) || type_code1 == REFERENCE_TYPE
9147 || (CLASS_TYPE_P (type1)
9148 && TYPE_HAS_TRIVIAL_DESTRUCTOR (type1)));
9150 case CPTK_HAS_VIRTUAL_DESTRUCTOR:
9151 return type_has_virtual_destructor (type1);
9153 case CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS:
9154 return type_has_unique_obj_representations (type1);
9156 case CPTK_IS_ABSTRACT:
9157 return ABSTRACT_CLASS_TYPE_P (type1);
9159 case CPTK_IS_AGGREGATE:
9160 return CP_AGGREGATE_TYPE_P (type1);
9162 case CPTK_IS_BASE_OF:
9163 return (NON_UNION_CLASS_TYPE_P (type1) && NON_UNION_CLASS_TYPE_P (type2)
9164 && (same_type_ignoring_top_level_qualifiers_p (type1, type2)
9165 || DERIVED_FROM_P (type1, type2)));
9167 case CPTK_IS_CLASS:
9168 return NON_UNION_CLASS_TYPE_P (type1);
9170 case CPTK_IS_EMPTY:
9171 return NON_UNION_CLASS_TYPE_P (type1) && CLASSTYPE_EMPTY_P (type1);
9173 case CPTK_IS_ENUM:
9174 return type_code1 == ENUMERAL_TYPE;
9176 case CPTK_IS_FINAL:
9177 return CLASS_TYPE_P (type1) && CLASSTYPE_FINAL (type1);
9179 case CPTK_IS_LITERAL_TYPE:
9180 return literal_type_p (type1);
9182 case CPTK_IS_POD:
9183 return pod_type_p (type1);
9185 case CPTK_IS_POLYMORPHIC:
9186 return CLASS_TYPE_P (type1) && TYPE_POLYMORPHIC_P (type1);
9188 case CPTK_IS_SAME_AS:
9189 return same_type_p (type1, type2);
9191 case CPTK_IS_STD_LAYOUT:
9192 return std_layout_type_p (type1);
9194 case CPTK_IS_TRIVIAL:
9195 return trivial_type_p (type1);
9197 case CPTK_IS_TRIVIALLY_ASSIGNABLE:
9198 return is_trivially_xible (MODIFY_EXPR, type1, type2);
9200 case CPTK_IS_TRIVIALLY_CONSTRUCTIBLE:
9201 return is_trivially_xible (INIT_EXPR, type1, type2);
9203 case CPTK_IS_TRIVIALLY_COPYABLE:
9204 return trivially_copyable_p (type1);
9206 case CPTK_IS_UNION:
9207 return type_code1 == UNION_TYPE;
9209 case CPTK_IS_ASSIGNABLE:
9210 return is_xible (MODIFY_EXPR, type1, type2);
9212 case CPTK_IS_CONSTRUCTIBLE:
9213 return is_xible (INIT_EXPR, type1, type2);
9215 default:
9216 gcc_unreachable ();
9217 return false;
9221 /* If TYPE is an array of unknown bound, or (possibly cv-qualified)
9222 void, or a complete type, returns true, otherwise false. */
9224 static bool
9225 check_trait_type (tree type)
9227 if (type == NULL_TREE)
9228 return true;
9230 if (TREE_CODE (type) == TREE_LIST)
9231 return (check_trait_type (TREE_VALUE (type))
9232 && check_trait_type (TREE_CHAIN (type)));
9234 if (TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type)
9235 && COMPLETE_TYPE_P (TREE_TYPE (type)))
9236 return true;
9238 if (VOID_TYPE_P (type))
9239 return true;
9241 return !!complete_type_or_else (strip_array_types (type), NULL_TREE);
9244 /* Process a trait expression. */
9246 tree
9247 finish_trait_expr (cp_trait_kind kind, tree type1, tree type2)
9249 if (type1 == error_mark_node
9250 || type2 == error_mark_node)
9251 return error_mark_node;
9253 if (processing_template_decl)
9255 tree trait_expr = make_node (TRAIT_EXPR);
9256 TREE_TYPE (trait_expr) = boolean_type_node;
9257 TRAIT_EXPR_TYPE1 (trait_expr) = type1;
9258 TRAIT_EXPR_TYPE2 (trait_expr) = type2;
9259 TRAIT_EXPR_KIND (trait_expr) = kind;
9260 return trait_expr;
9263 switch (kind)
9265 case CPTK_HAS_NOTHROW_ASSIGN:
9266 case CPTK_HAS_TRIVIAL_ASSIGN:
9267 case CPTK_HAS_NOTHROW_CONSTRUCTOR:
9268 case CPTK_HAS_TRIVIAL_CONSTRUCTOR:
9269 case CPTK_HAS_NOTHROW_COPY:
9270 case CPTK_HAS_TRIVIAL_COPY:
9271 case CPTK_HAS_TRIVIAL_DESTRUCTOR:
9272 case CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS:
9273 case CPTK_HAS_VIRTUAL_DESTRUCTOR:
9274 case CPTK_IS_ABSTRACT:
9275 case CPTK_IS_AGGREGATE:
9276 case CPTK_IS_EMPTY:
9277 case CPTK_IS_FINAL:
9278 case CPTK_IS_LITERAL_TYPE:
9279 case CPTK_IS_POD:
9280 case CPTK_IS_POLYMORPHIC:
9281 case CPTK_IS_STD_LAYOUT:
9282 case CPTK_IS_TRIVIAL:
9283 case CPTK_IS_TRIVIALLY_COPYABLE:
9284 if (!check_trait_type (type1))
9285 return error_mark_node;
9286 break;
9288 case CPTK_IS_ASSIGNABLE:
9289 case CPTK_IS_CONSTRUCTIBLE:
9290 break;
9292 case CPTK_IS_TRIVIALLY_ASSIGNABLE:
9293 case CPTK_IS_TRIVIALLY_CONSTRUCTIBLE:
9294 if (!check_trait_type (type1)
9295 || !check_trait_type (type2))
9296 return error_mark_node;
9297 break;
9299 case CPTK_IS_BASE_OF:
9300 if (NON_UNION_CLASS_TYPE_P (type1) && NON_UNION_CLASS_TYPE_P (type2)
9301 && !same_type_ignoring_top_level_qualifiers_p (type1, type2)
9302 && !complete_type_or_else (type2, NULL_TREE))
9303 /* We already issued an error. */
9304 return error_mark_node;
9305 break;
9307 case CPTK_IS_CLASS:
9308 case CPTK_IS_ENUM:
9309 case CPTK_IS_UNION:
9310 case CPTK_IS_SAME_AS:
9311 break;
9313 default:
9314 gcc_unreachable ();
9317 return (trait_expr_value (kind, type1, type2)
9318 ? boolean_true_node : boolean_false_node);
9321 /* Do-nothing variants of functions to handle pragma FLOAT_CONST_DECIMAL64,
9322 which is ignored for C++. */
9324 void
9325 set_float_const_decimal64 (void)
9329 void
9330 clear_float_const_decimal64 (void)
9334 bool
9335 float_const_decimal64_p (void)
9337 return 0;
9341 /* Return true if T designates the implied `this' parameter. */
9343 bool
9344 is_this_parameter (tree t)
9346 if (!DECL_P (t) || DECL_NAME (t) != this_identifier)
9347 return false;
9348 gcc_assert (TREE_CODE (t) == PARM_DECL || is_capture_proxy (t)
9349 || (cp_binding_oracle && TREE_CODE (t) == VAR_DECL));
9350 return true;
9353 /* Insert the deduced return type for an auto function. */
9355 void
9356 apply_deduced_return_type (tree fco, tree return_type)
9358 tree result;
9360 if (return_type == error_mark_node)
9361 return;
9363 if (LAMBDA_FUNCTION_P (fco))
9365 tree lambda = CLASSTYPE_LAMBDA_EXPR (current_class_type);
9366 LAMBDA_EXPR_RETURN_TYPE (lambda) = return_type;
9369 if (DECL_CONV_FN_P (fco))
9370 DECL_NAME (fco) = mangle_conv_op_name_for_type (return_type);
9372 TREE_TYPE (fco) = change_return_type (return_type, TREE_TYPE (fco));
9374 result = DECL_RESULT (fco);
9375 if (result == NULL_TREE)
9376 return;
9377 if (TREE_TYPE (result) == return_type)
9378 return;
9380 if (!processing_template_decl && !VOID_TYPE_P (return_type)
9381 && !complete_type_or_else (return_type, NULL_TREE))
9382 return;
9384 /* We already have a DECL_RESULT from start_preparsed_function.
9385 Now we need to redo the work it and allocate_struct_function
9386 did to reflect the new type. */
9387 gcc_assert (current_function_decl == fco);
9388 result = build_decl (input_location, RESULT_DECL, NULL_TREE,
9389 TYPE_MAIN_VARIANT (return_type));
9390 DECL_ARTIFICIAL (result) = 1;
9391 DECL_IGNORED_P (result) = 1;
9392 cp_apply_type_quals_to_decl (cp_type_quals (return_type),
9393 result);
9395 DECL_RESULT (fco) = result;
9397 if (!processing_template_decl)
9399 bool aggr = aggregate_value_p (result, fco);
9400 #ifdef PCC_STATIC_STRUCT_RETURN
9401 cfun->returns_pcc_struct = aggr;
9402 #endif
9403 cfun->returns_struct = aggr;
9408 /* DECL is a local variable or parameter from the surrounding scope of a
9409 lambda-expression. Returns the decltype for a use of the capture field
9410 for DECL even if it hasn't been captured yet. */
9412 static tree
9413 capture_decltype (tree decl)
9415 tree lam = CLASSTYPE_LAMBDA_EXPR (DECL_CONTEXT (current_function_decl));
9416 /* FIXME do lookup instead of list walk? */
9417 tree cap = value_member (decl, LAMBDA_EXPR_CAPTURE_LIST (lam));
9418 tree type;
9420 if (cap)
9421 type = TREE_TYPE (TREE_PURPOSE (cap));
9422 else
9423 switch (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lam))
9425 case CPLD_NONE:
9426 error ("%qD is not captured", decl);
9427 return error_mark_node;
9429 case CPLD_COPY:
9430 type = TREE_TYPE (decl);
9431 if (TREE_CODE (type) == REFERENCE_TYPE
9432 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
9433 type = TREE_TYPE (type);
9434 break;
9436 case CPLD_REFERENCE:
9437 type = TREE_TYPE (decl);
9438 if (TREE_CODE (type) != REFERENCE_TYPE)
9439 type = build_reference_type (TREE_TYPE (decl));
9440 break;
9442 default:
9443 gcc_unreachable ();
9446 if (TREE_CODE (type) != REFERENCE_TYPE)
9448 if (!LAMBDA_EXPR_MUTABLE_P (lam))
9449 type = cp_build_qualified_type (type, (cp_type_quals (type)
9450 |TYPE_QUAL_CONST));
9451 type = build_reference_type (type);
9453 return type;
9456 /* Build a unary fold expression of EXPR over OP. If IS_RIGHT is true,
9457 this is a right unary fold. Otherwise it is a left unary fold. */
9459 static tree
9460 finish_unary_fold_expr (tree expr, int op, tree_code dir)
9462 // Build a pack expansion (assuming expr has pack type).
9463 if (!uses_parameter_packs (expr))
9465 error_at (location_of (expr), "operand of fold expression has no "
9466 "unexpanded parameter packs");
9467 return error_mark_node;
9469 tree pack = make_pack_expansion (expr);
9471 // Build the fold expression.
9472 tree code = build_int_cstu (integer_type_node, abs (op));
9473 tree fold = build_min_nt_loc (UNKNOWN_LOCATION, dir, code, pack);
9474 FOLD_EXPR_MODIFY_P (fold) = (op < 0);
9475 return fold;
9478 tree
9479 finish_left_unary_fold_expr (tree expr, int op)
9481 return finish_unary_fold_expr (expr, op, UNARY_LEFT_FOLD_EXPR);
9484 tree
9485 finish_right_unary_fold_expr (tree expr, int op)
9487 return finish_unary_fold_expr (expr, op, UNARY_RIGHT_FOLD_EXPR);
9490 /* Build a binary fold expression over EXPR1 and EXPR2. The
9491 associativity of the fold is determined by EXPR1 and EXPR2 (whichever
9492 has an unexpanded parameter pack). */
9494 tree
9495 finish_binary_fold_expr (tree pack, tree init, int op, tree_code dir)
9497 pack = make_pack_expansion (pack);
9498 tree code = build_int_cstu (integer_type_node, abs (op));
9499 tree fold = build_min_nt_loc (UNKNOWN_LOCATION, dir, code, pack, init);
9500 FOLD_EXPR_MODIFY_P (fold) = (op < 0);
9501 return fold;
9504 tree
9505 finish_binary_fold_expr (tree expr1, tree expr2, int op)
9507 // Determine which expr has an unexpanded parameter pack and
9508 // set the pack and initial term.
9509 bool pack1 = uses_parameter_packs (expr1);
9510 bool pack2 = uses_parameter_packs (expr2);
9511 if (pack1 && !pack2)
9512 return finish_binary_fold_expr (expr1, expr2, op, BINARY_RIGHT_FOLD_EXPR);
9513 else if (pack2 && !pack1)
9514 return finish_binary_fold_expr (expr2, expr1, op, BINARY_LEFT_FOLD_EXPR);
9515 else
9517 if (pack1)
9518 error ("both arguments in binary fold have unexpanded parameter packs");
9519 else
9520 error ("no unexpanded parameter packs in binary fold");
9522 return error_mark_node;
9525 /* Finish __builtin_launder (arg). */
9527 tree
9528 finish_builtin_launder (location_t loc, tree arg, tsubst_flags_t complain)
9530 tree orig_arg = arg;
9531 if (!type_dependent_expression_p (arg))
9532 arg = decay_conversion (arg, complain);
9533 if (error_operand_p (arg))
9534 return error_mark_node;
9535 if (!type_dependent_expression_p (arg)
9536 && TREE_CODE (TREE_TYPE (arg)) != POINTER_TYPE)
9538 error_at (loc, "non-pointer argument to %<__builtin_launder%>");
9539 return error_mark_node;
9541 if (processing_template_decl)
9542 arg = orig_arg;
9543 return build_call_expr_internal_loc (loc, IFN_LAUNDER,
9544 TREE_TYPE (arg), 1, arg);
9547 #include "gt-cp-semantics.h"