libgo: add misc/cgo files
[official-gcc.git] / gcc / cp / semantics.c
blob9589b47dd649851d52429724d66133e7982e2acb
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))
737 cond = instantiate_non_dependent_expr (cond);
738 cond = cxx_constant_value (cond, NULL_TREE);
740 finish_cond (&IF_COND (if_stmt), cond);
741 add_stmt (if_stmt);
742 THEN_CLAUSE (if_stmt) = push_stmt_list ();
743 return cond;
746 /* Finish the then-clause of an if-statement, which may be given by
747 IF_STMT. */
749 tree
750 finish_then_clause (tree if_stmt)
752 THEN_CLAUSE (if_stmt) = pop_stmt_list (THEN_CLAUSE (if_stmt));
753 return if_stmt;
756 /* Begin the else-clause of an if-statement. */
758 void
759 begin_else_clause (tree if_stmt)
761 ELSE_CLAUSE (if_stmt) = push_stmt_list ();
764 /* Finish the else-clause of an if-statement, which may be given by
765 IF_STMT. */
767 void
768 finish_else_clause (tree if_stmt)
770 ELSE_CLAUSE (if_stmt) = pop_stmt_list (ELSE_CLAUSE (if_stmt));
773 /* Finish an if-statement. */
775 void
776 finish_if_stmt (tree if_stmt)
778 tree scope = IF_SCOPE (if_stmt);
779 IF_SCOPE (if_stmt) = NULL;
780 add_stmt (do_poplevel (scope));
783 /* Begin a while-statement. Returns a newly created WHILE_STMT if
784 appropriate. */
786 tree
787 begin_while_stmt (void)
789 tree r;
790 r = build_stmt (input_location, WHILE_STMT, NULL_TREE, NULL_TREE);
791 add_stmt (r);
792 WHILE_BODY (r) = do_pushlevel (sk_block);
793 begin_cond (&WHILE_COND (r));
794 return r;
797 /* Process the COND of a while-statement, which may be given by
798 WHILE_STMT. */
800 void
801 finish_while_stmt_cond (tree cond, tree while_stmt, bool ivdep)
803 if (check_no_cilk (cond,
804 "Cilk array notation cannot be used as a condition for while statement",
805 "%<_Cilk_spawn%> statement cannot be used as a condition for while statement"))
806 cond = error_mark_node;
807 cond = maybe_convert_cond (cond);
808 finish_cond (&WHILE_COND (while_stmt), cond);
809 begin_maybe_infinite_loop (cond);
810 if (ivdep && cond != error_mark_node)
811 WHILE_COND (while_stmt) = build2 (ANNOTATE_EXPR,
812 TREE_TYPE (WHILE_COND (while_stmt)),
813 WHILE_COND (while_stmt),
814 build_int_cst (integer_type_node,
815 annot_expr_ivdep_kind));
816 simplify_loop_decl_cond (&WHILE_COND (while_stmt), WHILE_BODY (while_stmt));
819 /* Finish a while-statement, which may be given by WHILE_STMT. */
821 void
822 finish_while_stmt (tree while_stmt)
824 end_maybe_infinite_loop (boolean_true_node);
825 WHILE_BODY (while_stmt) = do_poplevel (WHILE_BODY (while_stmt));
828 /* Begin a do-statement. Returns a newly created DO_STMT if
829 appropriate. */
831 tree
832 begin_do_stmt (void)
834 tree r = build_stmt (input_location, DO_STMT, NULL_TREE, NULL_TREE);
835 begin_maybe_infinite_loop (boolean_true_node);
836 add_stmt (r);
837 DO_BODY (r) = push_stmt_list ();
838 return r;
841 /* Finish the body of a do-statement, which may be given by DO_STMT. */
843 void
844 finish_do_body (tree do_stmt)
846 tree body = DO_BODY (do_stmt) = pop_stmt_list (DO_BODY (do_stmt));
848 if (TREE_CODE (body) == STATEMENT_LIST && STATEMENT_LIST_TAIL (body))
849 body = STATEMENT_LIST_TAIL (body)->stmt;
851 if (IS_EMPTY_STMT (body))
852 warning (OPT_Wempty_body,
853 "suggest explicit braces around empty body in %<do%> statement");
856 /* Finish a do-statement, which may be given by DO_STMT, and whose
857 COND is as indicated. */
859 void
860 finish_do_stmt (tree cond, tree do_stmt, bool ivdep)
862 if (check_no_cilk (cond,
863 "Cilk array notation cannot be used as a condition for a do-while statement",
864 "%<_Cilk_spawn%> statement cannot be used as a condition for a do-while statement"))
865 cond = error_mark_node;
866 cond = maybe_convert_cond (cond);
867 end_maybe_infinite_loop (cond);
868 if (ivdep && cond != error_mark_node)
869 cond = build2 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
870 build_int_cst (integer_type_node, annot_expr_ivdep_kind));
871 DO_COND (do_stmt) = cond;
874 /* Finish a return-statement. The EXPRESSION returned, if any, is as
875 indicated. */
877 tree
878 finish_return_stmt (tree expr)
880 tree r;
881 bool no_warning;
883 expr = check_return_expr (expr, &no_warning);
885 if (error_operand_p (expr)
886 || (flag_openmp && !check_omp_return ()))
888 /* Suppress -Wreturn-type for this function. */
889 if (warn_return_type)
890 TREE_NO_WARNING (current_function_decl) = true;
891 return error_mark_node;
894 if (!processing_template_decl)
896 if (warn_sequence_point)
897 verify_sequence_points (expr);
899 if (DECL_DESTRUCTOR_P (current_function_decl)
900 || (DECL_CONSTRUCTOR_P (current_function_decl)
901 && targetm.cxx.cdtor_returns_this ()))
903 /* Similarly, all destructors must run destructors for
904 base-classes before returning. So, all returns in a
905 destructor get sent to the DTOR_LABEL; finish_function emits
906 code to return a value there. */
907 return finish_goto_stmt (cdtor_label);
911 r = build_stmt (input_location, RETURN_EXPR, expr);
912 TREE_NO_WARNING (r) |= no_warning;
913 r = maybe_cleanup_point_expr_void (r);
914 r = add_stmt (r);
916 return r;
919 /* Begin the scope of a for-statement or a range-for-statement.
920 Both the returned trees are to be used in a call to
921 begin_for_stmt or begin_range_for_stmt. */
923 tree
924 begin_for_scope (tree *init)
926 tree scope = NULL_TREE;
927 if (flag_new_for_scope > 0)
928 scope = do_pushlevel (sk_for);
930 if (processing_template_decl)
931 *init = push_stmt_list ();
932 else
933 *init = NULL_TREE;
935 return scope;
938 /* Begin a for-statement. Returns a new FOR_STMT.
939 SCOPE and INIT should be the return of begin_for_scope,
940 or both NULL_TREE */
942 tree
943 begin_for_stmt (tree scope, tree init)
945 tree r;
947 r = build_stmt (input_location, FOR_STMT, NULL_TREE, NULL_TREE,
948 NULL_TREE, NULL_TREE, NULL_TREE);
950 if (scope == NULL_TREE)
952 gcc_assert (!init || !(flag_new_for_scope > 0));
953 if (!init)
954 scope = begin_for_scope (&init);
956 FOR_INIT_STMT (r) = init;
957 FOR_SCOPE (r) = scope;
959 return r;
962 /* Finish the init-statement of a for-statement, which may be
963 given by FOR_STMT. */
965 void
966 finish_init_stmt (tree for_stmt)
968 if (processing_template_decl)
969 FOR_INIT_STMT (for_stmt) = pop_stmt_list (FOR_INIT_STMT (for_stmt));
970 add_stmt (for_stmt);
971 FOR_BODY (for_stmt) = do_pushlevel (sk_block);
972 begin_cond (&FOR_COND (for_stmt));
975 /* Finish the COND of a for-statement, which may be given by
976 FOR_STMT. */
978 void
979 finish_for_cond (tree cond, tree for_stmt, bool ivdep)
981 if (check_no_cilk (cond,
982 "Cilk array notation cannot be used in a condition for a for-loop",
983 "%<_Cilk_spawn%> statement cannot be used in a condition for a for-loop"))
984 cond = error_mark_node;
985 cond = maybe_convert_cond (cond);
986 finish_cond (&FOR_COND (for_stmt), cond);
987 begin_maybe_infinite_loop (cond);
988 if (ivdep && cond != error_mark_node)
989 FOR_COND (for_stmt) = build2 (ANNOTATE_EXPR,
990 TREE_TYPE (FOR_COND (for_stmt)),
991 FOR_COND (for_stmt),
992 build_int_cst (integer_type_node,
993 annot_expr_ivdep_kind));
994 simplify_loop_decl_cond (&FOR_COND (for_stmt), FOR_BODY (for_stmt));
997 /* Finish the increment-EXPRESSION in a for-statement, which may be
998 given by FOR_STMT. */
1000 void
1001 finish_for_expr (tree expr, tree for_stmt)
1003 if (!expr)
1004 return;
1005 /* If EXPR is an overloaded function, issue an error; there is no
1006 context available to use to perform overload resolution. */
1007 if (type_unknown_p (expr))
1009 cxx_incomplete_type_error (expr, TREE_TYPE (expr));
1010 expr = error_mark_node;
1012 if (!processing_template_decl)
1014 if (warn_sequence_point)
1015 verify_sequence_points (expr);
1016 expr = convert_to_void (expr, ICV_THIRD_IN_FOR,
1017 tf_warning_or_error);
1019 else if (!type_dependent_expression_p (expr))
1020 convert_to_void (build_non_dependent_expr (expr), ICV_THIRD_IN_FOR,
1021 tf_warning_or_error);
1022 expr = maybe_cleanup_point_expr_void (expr);
1023 if (check_for_bare_parameter_packs (expr))
1024 expr = error_mark_node;
1025 FOR_EXPR (for_stmt) = expr;
1028 /* Finish the body of a for-statement, which may be given by
1029 FOR_STMT. The increment-EXPR for the loop must be
1030 provided.
1031 It can also finish RANGE_FOR_STMT. */
1033 void
1034 finish_for_stmt (tree for_stmt)
1036 end_maybe_infinite_loop (boolean_true_node);
1038 if (TREE_CODE (for_stmt) == RANGE_FOR_STMT)
1039 RANGE_FOR_BODY (for_stmt) = do_poplevel (RANGE_FOR_BODY (for_stmt));
1040 else
1041 FOR_BODY (for_stmt) = do_poplevel (FOR_BODY (for_stmt));
1043 /* Pop the scope for the body of the loop. */
1044 if (flag_new_for_scope > 0)
1046 tree scope;
1047 tree *scope_ptr = (TREE_CODE (for_stmt) == RANGE_FOR_STMT
1048 ? &RANGE_FOR_SCOPE (for_stmt)
1049 : &FOR_SCOPE (for_stmt));
1050 scope = *scope_ptr;
1051 *scope_ptr = NULL;
1052 add_stmt (do_poplevel (scope));
1056 /* Begin a range-for-statement. Returns a new RANGE_FOR_STMT.
1057 SCOPE and INIT should be the return of begin_for_scope,
1058 or both NULL_TREE .
1059 To finish it call finish_for_stmt(). */
1061 tree
1062 begin_range_for_stmt (tree scope, tree init)
1064 tree r;
1066 begin_maybe_infinite_loop (boolean_false_node);
1068 r = build_stmt (input_location, RANGE_FOR_STMT,
1069 NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE);
1071 if (scope == NULL_TREE)
1073 gcc_assert (!init || !(flag_new_for_scope > 0));
1074 if (!init)
1075 scope = begin_for_scope (&init);
1078 /* RANGE_FOR_STMTs do not use nor save the init tree, so we
1079 pop it now. */
1080 if (init)
1081 pop_stmt_list (init);
1082 RANGE_FOR_SCOPE (r) = scope;
1084 return r;
1087 /* Finish the head of a range-based for statement, which may
1088 be given by RANGE_FOR_STMT. DECL must be the declaration
1089 and EXPR must be the loop expression. */
1091 void
1092 finish_range_for_decl (tree range_for_stmt, tree decl, tree expr)
1094 RANGE_FOR_DECL (range_for_stmt) = decl;
1095 RANGE_FOR_EXPR (range_for_stmt) = expr;
1096 add_stmt (range_for_stmt);
1097 RANGE_FOR_BODY (range_for_stmt) = do_pushlevel (sk_block);
1100 /* Finish a break-statement. */
1102 tree
1103 finish_break_stmt (void)
1105 /* In switch statements break is sometimes stylistically used after
1106 a return statement. This can lead to spurious warnings about
1107 control reaching the end of a non-void function when it is
1108 inlined. Note that we are calling block_may_fallthru with
1109 language specific tree nodes; this works because
1110 block_may_fallthru returns true when given something it does not
1111 understand. */
1112 if (!block_may_fallthru (cur_stmt_list))
1113 return void_node;
1114 return add_stmt (build_stmt (input_location, BREAK_STMT));
1117 /* Finish a continue-statement. */
1119 tree
1120 finish_continue_stmt (void)
1122 return add_stmt (build_stmt (input_location, CONTINUE_STMT));
1125 /* Begin a switch-statement. Returns a new SWITCH_STMT if
1126 appropriate. */
1128 tree
1129 begin_switch_stmt (void)
1131 tree r, scope;
1133 scope = do_pushlevel (sk_cond);
1134 r = build_stmt (input_location, SWITCH_STMT, NULL_TREE, NULL_TREE, NULL_TREE, scope);
1136 begin_cond (&SWITCH_STMT_COND (r));
1138 return r;
1141 /* Finish the cond of a switch-statement. */
1143 void
1144 finish_switch_cond (tree cond, tree switch_stmt)
1146 tree orig_type = NULL;
1148 if (check_no_cilk (cond,
1149 "Cilk array notation cannot be used as a condition for switch statement",
1150 "%<_Cilk_spawn%> statement cannot be used as a condition for switch statement"))
1151 cond = error_mark_node;
1153 if (!processing_template_decl)
1155 /* Convert the condition to an integer or enumeration type. */
1156 cond = build_expr_type_conversion (WANT_INT | WANT_ENUM, cond, true);
1157 if (cond == NULL_TREE)
1159 error ("switch quantity not an integer");
1160 cond = error_mark_node;
1162 /* We want unlowered type here to handle enum bit-fields. */
1163 orig_type = unlowered_expr_type (cond);
1164 if (TREE_CODE (orig_type) != ENUMERAL_TYPE)
1165 orig_type = TREE_TYPE (cond);
1166 if (cond != error_mark_node)
1168 /* [stmt.switch]
1170 Integral promotions are performed. */
1171 cond = perform_integral_promotions (cond);
1172 cond = maybe_cleanup_point_expr (cond);
1175 if (check_for_bare_parameter_packs (cond))
1176 cond = error_mark_node;
1177 else if (!processing_template_decl && warn_sequence_point)
1178 verify_sequence_points (cond);
1180 finish_cond (&SWITCH_STMT_COND (switch_stmt), cond);
1181 SWITCH_STMT_TYPE (switch_stmt) = orig_type;
1182 add_stmt (switch_stmt);
1183 push_switch (switch_stmt);
1184 SWITCH_STMT_BODY (switch_stmt) = push_stmt_list ();
1187 /* Finish the body of a switch-statement, which may be given by
1188 SWITCH_STMT. The COND to switch on is indicated. */
1190 void
1191 finish_switch_stmt (tree switch_stmt)
1193 tree scope;
1195 SWITCH_STMT_BODY (switch_stmt) =
1196 pop_stmt_list (SWITCH_STMT_BODY (switch_stmt));
1197 pop_switch ();
1199 scope = SWITCH_STMT_SCOPE (switch_stmt);
1200 SWITCH_STMT_SCOPE (switch_stmt) = NULL;
1201 add_stmt (do_poplevel (scope));
1204 /* Begin a try-block. Returns a newly-created TRY_BLOCK if
1205 appropriate. */
1207 tree
1208 begin_try_block (void)
1210 tree r = build_stmt (input_location, TRY_BLOCK, NULL_TREE, NULL_TREE);
1211 add_stmt (r);
1212 TRY_STMTS (r) = push_stmt_list ();
1213 return r;
1216 /* Likewise, for a function-try-block. The block returned in
1217 *COMPOUND_STMT is an artificial outer scope, containing the
1218 function-try-block. */
1220 tree
1221 begin_function_try_block (tree *compound_stmt)
1223 tree r;
1224 /* This outer scope does not exist in the C++ standard, but we need
1225 a place to put __FUNCTION__ and similar variables. */
1226 *compound_stmt = begin_compound_stmt (0);
1227 r = begin_try_block ();
1228 FN_TRY_BLOCK_P (r) = 1;
1229 return r;
1232 /* Finish a try-block, which may be given by TRY_BLOCK. */
1234 void
1235 finish_try_block (tree try_block)
1237 TRY_STMTS (try_block) = pop_stmt_list (TRY_STMTS (try_block));
1238 TRY_HANDLERS (try_block) = push_stmt_list ();
1241 /* Finish the body of a cleanup try-block, which may be given by
1242 TRY_BLOCK. */
1244 void
1245 finish_cleanup_try_block (tree try_block)
1247 TRY_STMTS (try_block) = pop_stmt_list (TRY_STMTS (try_block));
1250 /* Finish an implicitly generated try-block, with a cleanup is given
1251 by CLEANUP. */
1253 void
1254 finish_cleanup (tree cleanup, tree try_block)
1256 TRY_HANDLERS (try_block) = cleanup;
1257 CLEANUP_P (try_block) = 1;
1260 /* Likewise, for a function-try-block. */
1262 void
1263 finish_function_try_block (tree try_block)
1265 finish_try_block (try_block);
1266 /* FIXME : something queer about CTOR_INITIALIZER somehow following
1267 the try block, but moving it inside. */
1268 in_function_try_handler = 1;
1271 /* Finish a handler-sequence for a try-block, which may be given by
1272 TRY_BLOCK. */
1274 void
1275 finish_handler_sequence (tree try_block)
1277 TRY_HANDLERS (try_block) = pop_stmt_list (TRY_HANDLERS (try_block));
1278 check_handlers (TRY_HANDLERS (try_block));
1281 /* Finish the handler-seq for a function-try-block, given by
1282 TRY_BLOCK. COMPOUND_STMT is the outer block created by
1283 begin_function_try_block. */
1285 void
1286 finish_function_handler_sequence (tree try_block, tree compound_stmt)
1288 in_function_try_handler = 0;
1289 finish_handler_sequence (try_block);
1290 finish_compound_stmt (compound_stmt);
1293 /* Begin a handler. Returns a HANDLER if appropriate. */
1295 tree
1296 begin_handler (void)
1298 tree r;
1300 r = build_stmt (input_location, HANDLER, NULL_TREE, NULL_TREE);
1301 add_stmt (r);
1303 /* Create a binding level for the eh_info and the exception object
1304 cleanup. */
1305 HANDLER_BODY (r) = do_pushlevel (sk_catch);
1307 return r;
1310 /* Finish the handler-parameters for a handler, which may be given by
1311 HANDLER. DECL is the declaration for the catch parameter, or NULL
1312 if this is a `catch (...)' clause. */
1314 void
1315 finish_handler_parms (tree decl, tree handler)
1317 tree type = NULL_TREE;
1318 if (processing_template_decl)
1320 if (decl)
1322 decl = pushdecl (decl);
1323 decl = push_template_decl (decl);
1324 HANDLER_PARMS (handler) = decl;
1325 type = TREE_TYPE (decl);
1328 else
1330 type = expand_start_catch_block (decl);
1331 if (warn_catch_value
1332 && type != NULL_TREE
1333 && type != error_mark_node
1334 && TREE_CODE (TREE_TYPE (decl)) != REFERENCE_TYPE)
1336 tree orig_type = TREE_TYPE (decl);
1337 if (CLASS_TYPE_P (orig_type))
1339 if (TYPE_POLYMORPHIC_P (orig_type))
1340 warning (OPT_Wcatch_value_,
1341 "catching polymorphic type %q#T by value", orig_type);
1342 else if (warn_catch_value > 1)
1343 warning (OPT_Wcatch_value_,
1344 "catching type %q#T by value", orig_type);
1346 else if (warn_catch_value > 2)
1347 warning (OPT_Wcatch_value_,
1348 "catching non-reference type %q#T", orig_type);
1351 HANDLER_TYPE (handler) = type;
1354 /* Finish a handler, which may be given by HANDLER. The BLOCKs are
1355 the return value from the matching call to finish_handler_parms. */
1357 void
1358 finish_handler (tree handler)
1360 if (!processing_template_decl)
1361 expand_end_catch_block ();
1362 HANDLER_BODY (handler) = do_poplevel (HANDLER_BODY (handler));
1365 /* Begin a compound statement. FLAGS contains some bits that control the
1366 behavior and context. If BCS_NO_SCOPE is set, the compound statement
1367 does not define a scope. If BCS_FN_BODY is set, this is the outermost
1368 block of a function. If BCS_TRY_BLOCK is set, this is the block
1369 created on behalf of a TRY statement. Returns a token to be passed to
1370 finish_compound_stmt. */
1372 tree
1373 begin_compound_stmt (unsigned int flags)
1375 tree r;
1377 if (flags & BCS_NO_SCOPE)
1379 r = push_stmt_list ();
1380 STATEMENT_LIST_NO_SCOPE (r) = 1;
1382 /* Normally, we try hard to keep the BLOCK for a statement-expression.
1383 But, if it's a statement-expression with a scopeless block, there's
1384 nothing to keep, and we don't want to accidentally keep a block
1385 *inside* the scopeless block. */
1386 keep_next_level (false);
1388 else
1390 scope_kind sk = sk_block;
1391 if (flags & BCS_TRY_BLOCK)
1392 sk = sk_try;
1393 else if (flags & BCS_TRANSACTION)
1394 sk = sk_transaction;
1395 r = do_pushlevel (sk);
1398 /* When processing a template, we need to remember where the braces were,
1399 so that we can set up identical scopes when instantiating the template
1400 later. BIND_EXPR is a handy candidate for this.
1401 Note that do_poplevel won't create a BIND_EXPR itself here (and thus
1402 result in nested BIND_EXPRs), since we don't build BLOCK nodes when
1403 processing templates. */
1404 if (processing_template_decl)
1406 r = build3 (BIND_EXPR, NULL, NULL, r, NULL);
1407 BIND_EXPR_TRY_BLOCK (r) = (flags & BCS_TRY_BLOCK) != 0;
1408 BIND_EXPR_BODY_BLOCK (r) = (flags & BCS_FN_BODY) != 0;
1409 TREE_SIDE_EFFECTS (r) = 1;
1412 return r;
1415 /* Finish a compound-statement, which is given by STMT. */
1417 void
1418 finish_compound_stmt (tree stmt)
1420 if (TREE_CODE (stmt) == BIND_EXPR)
1422 tree body = do_poplevel (BIND_EXPR_BODY (stmt));
1423 /* If the STATEMENT_LIST is empty and this BIND_EXPR isn't special,
1424 discard the BIND_EXPR so it can be merged with the containing
1425 STATEMENT_LIST. */
1426 if (TREE_CODE (body) == STATEMENT_LIST
1427 && STATEMENT_LIST_HEAD (body) == NULL
1428 && !BIND_EXPR_BODY_BLOCK (stmt)
1429 && !BIND_EXPR_TRY_BLOCK (stmt))
1430 stmt = body;
1431 else
1432 BIND_EXPR_BODY (stmt) = body;
1434 else if (STATEMENT_LIST_NO_SCOPE (stmt))
1435 stmt = pop_stmt_list (stmt);
1436 else
1438 /* Destroy any ObjC "super" receivers that may have been
1439 created. */
1440 objc_clear_super_receiver ();
1442 stmt = do_poplevel (stmt);
1445 /* ??? See c_end_compound_stmt wrt statement expressions. */
1446 add_stmt (stmt);
1449 /* Finish an asm-statement, whose components are a STRING, some
1450 OUTPUT_OPERANDS, some INPUT_OPERANDS, some CLOBBERS and some
1451 LABELS. Also note whether the asm-statement should be
1452 considered volatile. */
1454 tree
1455 finish_asm_stmt (int volatile_p, tree string, tree output_operands,
1456 tree input_operands, tree clobbers, tree labels)
1458 tree r;
1459 tree t;
1460 int ninputs = list_length (input_operands);
1461 int noutputs = list_length (output_operands);
1463 if (!processing_template_decl)
1465 const char *constraint;
1466 const char **oconstraints;
1467 bool allows_mem, allows_reg, is_inout;
1468 tree operand;
1469 int i;
1471 oconstraints = XALLOCAVEC (const char *, noutputs);
1473 string = resolve_asm_operand_names (string, output_operands,
1474 input_operands, labels);
1476 for (i = 0, t = output_operands; t; t = TREE_CHAIN (t), ++i)
1478 operand = TREE_VALUE (t);
1480 /* ??? Really, this should not be here. Users should be using a
1481 proper lvalue, dammit. But there's a long history of using
1482 casts in the output operands. In cases like longlong.h, this
1483 becomes a primitive form of typechecking -- if the cast can be
1484 removed, then the output operand had a type of the proper width;
1485 otherwise we'll get an error. Gross, but ... */
1486 STRIP_NOPS (operand);
1488 operand = mark_lvalue_use (operand);
1490 if (!lvalue_or_else (operand, lv_asm, tf_warning_or_error))
1491 operand = error_mark_node;
1493 if (operand != error_mark_node
1494 && (TREE_READONLY (operand)
1495 || CP_TYPE_CONST_P (TREE_TYPE (operand))
1496 /* Functions are not modifiable, even though they are
1497 lvalues. */
1498 || TREE_CODE (TREE_TYPE (operand)) == FUNCTION_TYPE
1499 || TREE_CODE (TREE_TYPE (operand)) == METHOD_TYPE
1500 /* If it's an aggregate and any field is const, then it is
1501 effectively const. */
1502 || (CLASS_TYPE_P (TREE_TYPE (operand))
1503 && C_TYPE_FIELDS_READONLY (TREE_TYPE (operand)))))
1504 cxx_readonly_error (operand, lv_asm);
1506 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
1507 oconstraints[i] = constraint;
1509 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
1510 &allows_mem, &allows_reg, &is_inout))
1512 /* If the operand is going to end up in memory,
1513 mark it addressable. */
1514 if (!allows_reg && !cxx_mark_addressable (operand))
1515 operand = error_mark_node;
1517 else
1518 operand = error_mark_node;
1520 TREE_VALUE (t) = operand;
1523 for (i = 0, t = input_operands; t; ++i, t = TREE_CHAIN (t))
1525 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
1526 bool constraint_parsed
1527 = parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
1528 oconstraints, &allows_mem, &allows_reg);
1529 /* If the operand is going to end up in memory, don't call
1530 decay_conversion. */
1531 if (constraint_parsed && !allows_reg && allows_mem)
1532 operand = mark_lvalue_use (TREE_VALUE (t));
1533 else
1534 operand = decay_conversion (TREE_VALUE (t), tf_warning_or_error);
1536 /* If the type of the operand hasn't been determined (e.g.,
1537 because it involves an overloaded function), then issue
1538 an error message. There's no context available to
1539 resolve the overloading. */
1540 if (TREE_TYPE (operand) == unknown_type_node)
1542 error ("type of asm operand %qE could not be determined",
1543 TREE_VALUE (t));
1544 operand = error_mark_node;
1547 if (constraint_parsed)
1549 /* If the operand is going to end up in memory,
1550 mark it addressable. */
1551 if (!allows_reg && allows_mem)
1553 /* Strip the nops as we allow this case. FIXME, this really
1554 should be rejected or made deprecated. */
1555 STRIP_NOPS (operand);
1556 if (!cxx_mark_addressable (operand))
1557 operand = error_mark_node;
1559 else if (!allows_reg && !allows_mem)
1561 /* If constraint allows neither register nor memory,
1562 try harder to get a constant. */
1563 tree constop = maybe_constant_value (operand);
1564 if (TREE_CONSTANT (constop))
1565 operand = constop;
1568 else
1569 operand = error_mark_node;
1571 TREE_VALUE (t) = operand;
1575 r = build_stmt (input_location, ASM_EXPR, string,
1576 output_operands, input_operands,
1577 clobbers, labels);
1578 ASM_VOLATILE_P (r) = volatile_p || noutputs == 0;
1579 r = maybe_cleanup_point_expr_void (r);
1580 return add_stmt (r);
1583 /* Finish a label with the indicated NAME. Returns the new label. */
1585 tree
1586 finish_label_stmt (tree name)
1588 tree decl = define_label (input_location, name);
1590 if (decl == error_mark_node)
1591 return error_mark_node;
1593 add_stmt (build_stmt (input_location, LABEL_EXPR, decl));
1595 return decl;
1598 /* Finish a series of declarations for local labels. G++ allows users
1599 to declare "local" labels, i.e., labels with scope. This extension
1600 is useful when writing code involving statement-expressions. */
1602 void
1603 finish_label_decl (tree name)
1605 if (!at_function_scope_p ())
1607 error ("__label__ declarations are only allowed in function scopes");
1608 return;
1611 add_decl_expr (declare_local_label (name));
1614 /* When DECL goes out of scope, make sure that CLEANUP is executed. */
1616 void
1617 finish_decl_cleanup (tree decl, tree cleanup)
1619 push_cleanup (decl, cleanup, false);
1622 /* If the current scope exits with an exception, run CLEANUP. */
1624 void
1625 finish_eh_cleanup (tree cleanup)
1627 push_cleanup (NULL, cleanup, true);
1630 /* The MEM_INITS is a list of mem-initializers, in reverse of the
1631 order they were written by the user. Each node is as for
1632 emit_mem_initializers. */
1634 void
1635 finish_mem_initializers (tree mem_inits)
1637 /* Reorder the MEM_INITS so that they are in the order they appeared
1638 in the source program. */
1639 mem_inits = nreverse (mem_inits);
1641 if (processing_template_decl)
1643 tree mem;
1645 for (mem = mem_inits; mem; mem = TREE_CHAIN (mem))
1647 /* If the TREE_PURPOSE is a TYPE_PACK_EXPANSION, skip the
1648 check for bare parameter packs in the TREE_VALUE, because
1649 any parameter packs in the TREE_VALUE have already been
1650 bound as part of the TREE_PURPOSE. See
1651 make_pack_expansion for more information. */
1652 if (TREE_CODE (TREE_PURPOSE (mem)) != TYPE_PACK_EXPANSION
1653 && check_for_bare_parameter_packs (TREE_VALUE (mem)))
1654 TREE_VALUE (mem) = error_mark_node;
1657 add_stmt (build_min_nt_loc (UNKNOWN_LOCATION,
1658 CTOR_INITIALIZER, mem_inits));
1660 else
1661 emit_mem_initializers (mem_inits);
1664 /* Obfuscate EXPR if it looks like an id-expression or member access so
1665 that the call to finish_decltype in do_auto_deduction will give the
1666 right result. */
1668 tree
1669 force_paren_expr (tree expr)
1671 /* This is only needed for decltype(auto) in C++14. */
1672 if (cxx_dialect < cxx14)
1673 return expr;
1675 /* If we're in unevaluated context, we can't be deducing a
1676 return/initializer type, so we don't need to mess with this. */
1677 if (cp_unevaluated_operand)
1678 return expr;
1680 if (!DECL_P (expr) && TREE_CODE (expr) != COMPONENT_REF
1681 && TREE_CODE (expr) != SCOPE_REF)
1682 return expr;
1684 if (TREE_CODE (expr) == COMPONENT_REF
1685 || TREE_CODE (expr) == SCOPE_REF)
1686 REF_PARENTHESIZED_P (expr) = true;
1687 else if (type_dependent_expression_p (expr))
1688 expr = build1 (PAREN_EXPR, TREE_TYPE (expr), expr);
1689 else if (VAR_P (expr) && DECL_HARD_REGISTER (expr))
1690 /* We can't bind a hard register variable to a reference. */;
1691 else
1693 cp_lvalue_kind kind = lvalue_kind (expr);
1694 if ((kind & ~clk_class) != clk_none)
1696 tree type = unlowered_expr_type (expr);
1697 bool rval = !!(kind & clk_rvalueref);
1698 type = cp_build_reference_type (type, rval);
1699 /* This inhibits warnings in, eg, cxx_mark_addressable
1700 (c++/60955). */
1701 warning_sentinel s (extra_warnings);
1702 expr = build_static_cast (type, expr, tf_error);
1703 if (expr != error_mark_node)
1704 REF_PARENTHESIZED_P (expr) = true;
1708 return expr;
1711 /* If T is an id-expression obfuscated by force_paren_expr, undo the
1712 obfuscation and return the underlying id-expression. Otherwise
1713 return T. */
1715 tree
1716 maybe_undo_parenthesized_ref (tree t)
1718 if (cxx_dialect >= cxx14
1719 && INDIRECT_REF_P (t)
1720 && REF_PARENTHESIZED_P (t))
1722 t = TREE_OPERAND (t, 0);
1723 while (TREE_CODE (t) == NON_LVALUE_EXPR
1724 || TREE_CODE (t) == NOP_EXPR)
1725 t = TREE_OPERAND (t, 0);
1727 gcc_assert (TREE_CODE (t) == ADDR_EXPR
1728 || TREE_CODE (t) == STATIC_CAST_EXPR);
1729 t = TREE_OPERAND (t, 0);
1732 return t;
1735 /* Finish a parenthesized expression EXPR. */
1737 cp_expr
1738 finish_parenthesized_expr (cp_expr expr)
1740 if (EXPR_P (expr))
1741 /* This inhibits warnings in c_common_truthvalue_conversion. */
1742 TREE_NO_WARNING (expr) = 1;
1744 if (TREE_CODE (expr) == OFFSET_REF
1745 || TREE_CODE (expr) == SCOPE_REF)
1746 /* [expr.unary.op]/3 The qualified id of a pointer-to-member must not be
1747 enclosed in parentheses. */
1748 PTRMEM_OK_P (expr) = 0;
1750 if (TREE_CODE (expr) == STRING_CST)
1751 PAREN_STRING_LITERAL_P (expr) = 1;
1753 expr = cp_expr (force_paren_expr (expr), expr.get_location ());
1755 return expr;
1758 /* Finish a reference to a non-static data member (DECL) that is not
1759 preceded by `.' or `->'. */
1761 tree
1762 finish_non_static_data_member (tree decl, tree object, tree qualifying_scope)
1764 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
1765 bool try_omp_private = !object && omp_private_member_map;
1766 tree ret;
1768 if (!object)
1770 tree scope = qualifying_scope;
1771 if (scope == NULL_TREE)
1772 scope = context_for_name_lookup (decl);
1773 object = maybe_dummy_object (scope, NULL);
1776 object = maybe_resolve_dummy (object, true);
1777 if (object == error_mark_node)
1778 return error_mark_node;
1780 /* DR 613/850: Can use non-static data members without an associated
1781 object in sizeof/decltype/alignof. */
1782 if (is_dummy_object (object) && cp_unevaluated_operand == 0
1783 && (!processing_template_decl || !current_class_ref))
1785 if (current_function_decl
1786 && DECL_STATIC_FUNCTION_P (current_function_decl))
1787 error ("invalid use of member %qD in static member function", decl);
1788 else
1789 error ("invalid use of non-static data member %qD", decl);
1790 inform (DECL_SOURCE_LOCATION (decl), "declared here");
1792 return error_mark_node;
1795 if (current_class_ptr)
1796 TREE_USED (current_class_ptr) = 1;
1797 if (processing_template_decl && !qualifying_scope)
1799 tree type = TREE_TYPE (decl);
1801 if (TREE_CODE (type) == REFERENCE_TYPE)
1802 /* Quals on the object don't matter. */;
1803 else if (PACK_EXPANSION_P (type))
1804 /* Don't bother trying to represent this. */
1805 type = NULL_TREE;
1806 else
1808 /* Set the cv qualifiers. */
1809 int quals = cp_type_quals (TREE_TYPE (object));
1811 if (DECL_MUTABLE_P (decl))
1812 quals &= ~TYPE_QUAL_CONST;
1814 quals |= cp_type_quals (TREE_TYPE (decl));
1815 type = cp_build_qualified_type (type, quals);
1818 ret = (convert_from_reference
1819 (build_min (COMPONENT_REF, type, object, decl, NULL_TREE)));
1821 /* If PROCESSING_TEMPLATE_DECL is nonzero here, then
1822 QUALIFYING_SCOPE is also non-null. Wrap this in a SCOPE_REF
1823 for now. */
1824 else if (processing_template_decl)
1825 ret = build_qualified_name (TREE_TYPE (decl),
1826 qualifying_scope,
1827 decl,
1828 /*template_p=*/false);
1829 else
1831 tree access_type = TREE_TYPE (object);
1833 perform_or_defer_access_check (TYPE_BINFO (access_type), decl,
1834 decl, tf_warning_or_error);
1836 /* If the data member was named `C::M', convert `*this' to `C'
1837 first. */
1838 if (qualifying_scope)
1840 tree binfo = NULL_TREE;
1841 object = build_scoped_ref (object, qualifying_scope,
1842 &binfo);
1845 ret = build_class_member_access_expr (object, decl,
1846 /*access_path=*/NULL_TREE,
1847 /*preserve_reference=*/false,
1848 tf_warning_or_error);
1850 if (try_omp_private)
1852 tree *v = omp_private_member_map->get (decl);
1853 if (v)
1854 ret = convert_from_reference (*v);
1856 return ret;
1859 /* If we are currently parsing a template and we encountered a typedef
1860 TYPEDEF_DECL that is being accessed though CONTEXT, this function
1861 adds the typedef to a list tied to the current template.
1862 At template instantiation time, that list is walked and access check
1863 performed for each typedef.
1864 LOCATION is the location of the usage point of TYPEDEF_DECL. */
1866 void
1867 add_typedef_to_current_template_for_access_check (tree typedef_decl,
1868 tree context,
1869 location_t location)
1871 tree template_info = NULL;
1872 tree cs = current_scope ();
1874 if (!is_typedef_decl (typedef_decl)
1875 || !context
1876 || !CLASS_TYPE_P (context)
1877 || !cs)
1878 return;
1880 if (CLASS_TYPE_P (cs) || TREE_CODE (cs) == FUNCTION_DECL)
1881 template_info = get_template_info (cs);
1883 if (template_info
1884 && TI_TEMPLATE (template_info)
1885 && !currently_open_class (context))
1886 append_type_to_template_for_access_check (cs, typedef_decl,
1887 context, location);
1890 /* DECL was the declaration to which a qualified-id resolved. Issue
1891 an error message if it is not accessible. If OBJECT_TYPE is
1892 non-NULL, we have just seen `x->' or `x.' and OBJECT_TYPE is the
1893 type of `*x', or `x', respectively. If the DECL was named as
1894 `A::B' then NESTED_NAME_SPECIFIER is `A'. */
1896 void
1897 check_accessibility_of_qualified_id (tree decl,
1898 tree object_type,
1899 tree nested_name_specifier)
1901 tree scope;
1902 tree qualifying_type = NULL_TREE;
1904 /* If we are parsing a template declaration and if decl is a typedef,
1905 add it to a list tied to the template.
1906 At template instantiation time, that list will be walked and
1907 access check performed. */
1908 add_typedef_to_current_template_for_access_check (decl,
1909 nested_name_specifier
1910 ? nested_name_specifier
1911 : DECL_CONTEXT (decl),
1912 input_location);
1914 /* If we're not checking, return immediately. */
1915 if (deferred_access_no_check)
1916 return;
1918 /* Determine the SCOPE of DECL. */
1919 scope = context_for_name_lookup (decl);
1920 /* If the SCOPE is not a type, then DECL is not a member. */
1921 if (!TYPE_P (scope))
1922 return;
1923 /* Compute the scope through which DECL is being accessed. */
1924 if (object_type
1925 /* OBJECT_TYPE might not be a class type; consider:
1927 class A { typedef int I; };
1928 I *p;
1929 p->A::I::~I();
1931 In this case, we will have "A::I" as the DECL, but "I" as the
1932 OBJECT_TYPE. */
1933 && CLASS_TYPE_P (object_type)
1934 && DERIVED_FROM_P (scope, object_type))
1935 /* If we are processing a `->' or `.' expression, use the type of the
1936 left-hand side. */
1937 qualifying_type = object_type;
1938 else if (nested_name_specifier)
1940 /* If the reference is to a non-static member of the
1941 current class, treat it as if it were referenced through
1942 `this'. */
1943 tree ct;
1944 if (DECL_NONSTATIC_MEMBER_P (decl)
1945 && current_class_ptr
1946 && DERIVED_FROM_P (scope, ct = current_nonlambda_class_type ()))
1947 qualifying_type = ct;
1948 /* Otherwise, use the type indicated by the
1949 nested-name-specifier. */
1950 else
1951 qualifying_type = nested_name_specifier;
1953 else
1954 /* Otherwise, the name must be from the current class or one of
1955 its bases. */
1956 qualifying_type = currently_open_derived_class (scope);
1958 if (qualifying_type
1959 /* It is possible for qualifying type to be a TEMPLATE_TYPE_PARM
1960 or similar in a default argument value. */
1961 && CLASS_TYPE_P (qualifying_type)
1962 && !dependent_type_p (qualifying_type))
1963 perform_or_defer_access_check (TYPE_BINFO (qualifying_type), decl,
1964 decl, tf_warning_or_error);
1967 /* EXPR is the result of a qualified-id. The QUALIFYING_CLASS was the
1968 class named to the left of the "::" operator. DONE is true if this
1969 expression is a complete postfix-expression; it is false if this
1970 expression is followed by '->', '[', '(', etc. ADDRESS_P is true
1971 iff this expression is the operand of '&'. TEMPLATE_P is true iff
1972 the qualified-id was of the form "A::template B". TEMPLATE_ARG_P
1973 is true iff this qualified name appears as a template argument. */
1975 tree
1976 finish_qualified_id_expr (tree qualifying_class,
1977 tree expr,
1978 bool done,
1979 bool address_p,
1980 bool template_p,
1981 bool template_arg_p,
1982 tsubst_flags_t complain)
1984 gcc_assert (TYPE_P (qualifying_class));
1986 if (error_operand_p (expr))
1987 return error_mark_node;
1989 if ((DECL_P (expr) || BASELINK_P (expr))
1990 && !mark_used (expr, complain))
1991 return error_mark_node;
1993 if (template_p)
1995 if (TREE_CODE (expr) == UNBOUND_CLASS_TEMPLATE)
1996 /* cp_parser_lookup_name thought we were looking for a type,
1997 but we're actually looking for a declaration. */
1998 expr = build_qualified_name (/*type*/NULL_TREE,
1999 TYPE_CONTEXT (expr),
2000 TYPE_IDENTIFIER (expr),
2001 /*template_p*/true);
2002 else
2003 check_template_keyword (expr);
2006 /* If EXPR occurs as the operand of '&', use special handling that
2007 permits a pointer-to-member. */
2008 if (address_p && done)
2010 if (TREE_CODE (expr) == SCOPE_REF)
2011 expr = TREE_OPERAND (expr, 1);
2012 expr = build_offset_ref (qualifying_class, expr,
2013 /*address_p=*/true, complain);
2014 return expr;
2017 /* No need to check access within an enum. */
2018 if (TREE_CODE (qualifying_class) == ENUMERAL_TYPE)
2019 return expr;
2021 /* Within the scope of a class, turn references to non-static
2022 members into expression of the form "this->...". */
2023 if (template_arg_p)
2024 /* But, within a template argument, we do not want make the
2025 transformation, as there is no "this" pointer. */
2027 else if (TREE_CODE (expr) == FIELD_DECL)
2029 push_deferring_access_checks (dk_no_check);
2030 expr = finish_non_static_data_member (expr, NULL_TREE,
2031 qualifying_class);
2032 pop_deferring_access_checks ();
2034 else if (BASELINK_P (expr) && !processing_template_decl)
2036 /* See if any of the functions are non-static members. */
2037 /* If so, the expression may be relative to 'this'. */
2038 if (!shared_member_p (expr)
2039 && current_class_ptr
2040 && DERIVED_FROM_P (qualifying_class,
2041 current_nonlambda_class_type ()))
2042 expr = (build_class_member_access_expr
2043 (maybe_dummy_object (qualifying_class, NULL),
2044 expr,
2045 BASELINK_ACCESS_BINFO (expr),
2046 /*preserve_reference=*/false,
2047 complain));
2048 else if (done)
2049 /* The expression is a qualified name whose address is not
2050 being taken. */
2051 expr = build_offset_ref (qualifying_class, expr, /*address_p=*/false,
2052 complain);
2054 else if (BASELINK_P (expr))
2056 else
2058 /* In a template, return a SCOPE_REF for most qualified-ids
2059 so that we can check access at instantiation time. But if
2060 we're looking at a member of the current instantiation, we
2061 know we have access and building up the SCOPE_REF confuses
2062 non-type template argument handling. */
2063 if (processing_template_decl
2064 && !currently_open_class (qualifying_class))
2065 expr = build_qualified_name (TREE_TYPE (expr),
2066 qualifying_class, expr,
2067 template_p);
2069 expr = convert_from_reference (expr);
2072 return expr;
2075 /* Begin a statement-expression. The value returned must be passed to
2076 finish_stmt_expr. */
2078 tree
2079 begin_stmt_expr (void)
2081 return push_stmt_list ();
2084 /* Process the final expression of a statement expression. EXPR can be
2085 NULL, if the final expression is empty. Return a STATEMENT_LIST
2086 containing all the statements in the statement-expression, or
2087 ERROR_MARK_NODE if there was an error. */
2089 tree
2090 finish_stmt_expr_expr (tree expr, tree stmt_expr)
2092 if (error_operand_p (expr))
2094 /* The type of the statement-expression is the type of the last
2095 expression. */
2096 TREE_TYPE (stmt_expr) = error_mark_node;
2097 return error_mark_node;
2100 /* If the last statement does not have "void" type, then the value
2101 of the last statement is the value of the entire expression. */
2102 if (expr)
2104 tree type = TREE_TYPE (expr);
2106 if (processing_template_decl)
2108 expr = build_stmt (input_location, EXPR_STMT, expr);
2109 expr = add_stmt (expr);
2110 /* Mark the last statement so that we can recognize it as such at
2111 template-instantiation time. */
2112 EXPR_STMT_STMT_EXPR_RESULT (expr) = 1;
2114 else if (VOID_TYPE_P (type))
2116 /* Just treat this like an ordinary statement. */
2117 expr = finish_expr_stmt (expr);
2119 else
2121 /* It actually has a value we need to deal with. First, force it
2122 to be an rvalue so that we won't need to build up a copy
2123 constructor call later when we try to assign it to something. */
2124 expr = force_rvalue (expr, tf_warning_or_error);
2125 if (error_operand_p (expr))
2126 return error_mark_node;
2128 /* Update for array-to-pointer decay. */
2129 type = TREE_TYPE (expr);
2131 /* Wrap it in a CLEANUP_POINT_EXPR and add it to the list like a
2132 normal statement, but don't convert to void or actually add
2133 the EXPR_STMT. */
2134 if (TREE_CODE (expr) != CLEANUP_POINT_EXPR)
2135 expr = maybe_cleanup_point_expr (expr);
2136 add_stmt (expr);
2139 /* The type of the statement-expression is the type of the last
2140 expression. */
2141 TREE_TYPE (stmt_expr) = type;
2144 return stmt_expr;
2147 /* Finish a statement-expression. EXPR should be the value returned
2148 by the previous begin_stmt_expr. Returns an expression
2149 representing the statement-expression. */
2151 tree
2152 finish_stmt_expr (tree stmt_expr, bool has_no_scope)
2154 tree type;
2155 tree result;
2157 if (error_operand_p (stmt_expr))
2159 pop_stmt_list (stmt_expr);
2160 return error_mark_node;
2163 gcc_assert (TREE_CODE (stmt_expr) == STATEMENT_LIST);
2165 type = TREE_TYPE (stmt_expr);
2166 result = pop_stmt_list (stmt_expr);
2167 TREE_TYPE (result) = type;
2169 if (processing_template_decl)
2171 result = build_min (STMT_EXPR, type, result);
2172 TREE_SIDE_EFFECTS (result) = 1;
2173 STMT_EXPR_NO_SCOPE (result) = has_no_scope;
2175 else if (CLASS_TYPE_P (type))
2177 /* Wrap the statement-expression in a TARGET_EXPR so that the
2178 temporary object created by the final expression is destroyed at
2179 the end of the full-expression containing the
2180 statement-expression. */
2181 result = force_target_expr (type, result, tf_warning_or_error);
2184 return result;
2187 /* Returns the expression which provides the value of STMT_EXPR. */
2189 tree
2190 stmt_expr_value_expr (tree stmt_expr)
2192 tree t = STMT_EXPR_STMT (stmt_expr);
2194 if (TREE_CODE (t) == BIND_EXPR)
2195 t = BIND_EXPR_BODY (t);
2197 if (TREE_CODE (t) == STATEMENT_LIST && STATEMENT_LIST_TAIL (t))
2198 t = STATEMENT_LIST_TAIL (t)->stmt;
2200 if (TREE_CODE (t) == EXPR_STMT)
2201 t = EXPR_STMT_EXPR (t);
2203 return t;
2206 /* Return TRUE iff EXPR_STMT is an empty list of
2207 expression statements. */
2209 bool
2210 empty_expr_stmt_p (tree expr_stmt)
2212 tree body = NULL_TREE;
2214 if (expr_stmt == void_node)
2215 return true;
2217 if (expr_stmt)
2219 if (TREE_CODE (expr_stmt) == EXPR_STMT)
2220 body = EXPR_STMT_EXPR (expr_stmt);
2221 else if (TREE_CODE (expr_stmt) == STATEMENT_LIST)
2222 body = expr_stmt;
2225 if (body)
2227 if (TREE_CODE (body) == STATEMENT_LIST)
2228 return tsi_end_p (tsi_start (body));
2229 else
2230 return empty_expr_stmt_p (body);
2232 return false;
2235 /* Perform Koenig lookup. FN is the postfix-expression representing
2236 the function (or functions) to call; ARGS are the arguments to the
2237 call. Returns the functions to be considered by overload resolution. */
2239 cp_expr
2240 perform_koenig_lookup (cp_expr fn, vec<tree, va_gc> *args,
2241 tsubst_flags_t complain)
2243 tree identifier = NULL_TREE;
2244 tree functions = NULL_TREE;
2245 tree tmpl_args = NULL_TREE;
2246 bool template_id = false;
2247 location_t loc = fn.get_location ();
2249 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
2251 /* Use a separate flag to handle null args. */
2252 template_id = true;
2253 tmpl_args = TREE_OPERAND (fn, 1);
2254 fn = TREE_OPERAND (fn, 0);
2257 /* Find the name of the overloaded function. */
2258 if (identifier_p (fn))
2259 identifier = fn;
2260 else
2262 functions = fn;
2263 identifier = OVL_NAME (functions);
2266 /* A call to a namespace-scope function using an unqualified name.
2268 Do Koenig lookup -- unless any of the arguments are
2269 type-dependent. */
2270 if (!any_type_dependent_arguments_p (args)
2271 && !any_dependent_template_arguments_p (tmpl_args))
2273 fn = lookup_arg_dependent (identifier, functions, args);
2274 if (!fn)
2276 /* The unqualified name could not be resolved. */
2277 if (complain & tf_error)
2278 fn = unqualified_fn_lookup_error (cp_expr (identifier, loc));
2279 else
2280 fn = identifier;
2284 if (fn && template_id && fn != error_mark_node)
2285 fn = build2 (TEMPLATE_ID_EXPR, unknown_type_node, fn, tmpl_args);
2287 return fn;
2290 /* Generate an expression for `FN (ARGS)'. This may change the
2291 contents of ARGS.
2293 If DISALLOW_VIRTUAL is true, the call to FN will be not generated
2294 as a virtual call, even if FN is virtual. (This flag is set when
2295 encountering an expression where the function name is explicitly
2296 qualified. For example a call to `X::f' never generates a virtual
2297 call.)
2299 Returns code for the call. */
2301 tree
2302 finish_call_expr (tree fn, vec<tree, va_gc> **args, bool disallow_virtual,
2303 bool koenig_p, tsubst_flags_t complain)
2305 tree result;
2306 tree orig_fn;
2307 vec<tree, va_gc> *orig_args = NULL;
2309 if (fn == error_mark_node)
2310 return error_mark_node;
2312 gcc_assert (!TYPE_P (fn));
2314 /* If FN may be a FUNCTION_DECL obfuscated by force_paren_expr, undo
2315 it so that we can tell this is a call to a known function. */
2316 fn = maybe_undo_parenthesized_ref (fn);
2318 orig_fn = fn;
2320 if (processing_template_decl)
2322 /* If FN is a local extern declaration or set thereof, look them up
2323 again at instantiation time. */
2324 if (is_overloaded_fn (fn))
2326 tree ifn = get_first_fn (fn);
2327 if (TREE_CODE (ifn) == FUNCTION_DECL
2328 && DECL_LOCAL_FUNCTION_P (ifn))
2329 orig_fn = DECL_NAME (ifn);
2332 /* If the call expression is dependent, build a CALL_EXPR node
2333 with no type; type_dependent_expression_p recognizes
2334 expressions with no type as being dependent. */
2335 if (type_dependent_expression_p (fn)
2336 || any_type_dependent_arguments_p (*args))
2338 result = build_min_nt_call_vec (orig_fn, *args);
2339 SET_EXPR_LOCATION (result, EXPR_LOC_OR_LOC (fn, input_location));
2340 KOENIG_LOOKUP_P (result) = koenig_p;
2341 if (is_overloaded_fn (fn))
2343 fn = get_fns (fn);
2344 lookup_keep (fn, true);
2347 if (cfun)
2349 bool abnormal = true;
2350 for (lkp_iterator iter (fn); abnormal && iter; ++iter)
2352 tree fndecl = *iter;
2353 if (TREE_CODE (fndecl) != FUNCTION_DECL
2354 || !TREE_THIS_VOLATILE (fndecl))
2355 abnormal = false;
2357 /* FIXME: Stop warning about falling off end of non-void
2358 function. But this is wrong. Even if we only see
2359 no-return fns at this point, we could select a
2360 future-defined return fn during instantiation. Or
2361 vice-versa. */
2362 if (abnormal)
2363 current_function_returns_abnormally = 1;
2365 return result;
2367 orig_args = make_tree_vector_copy (*args);
2368 if (!BASELINK_P (fn)
2369 && TREE_CODE (fn) != PSEUDO_DTOR_EXPR
2370 && TREE_TYPE (fn) != unknown_type_node)
2371 fn = build_non_dependent_expr (fn);
2372 make_args_non_dependent (*args);
2375 if (TREE_CODE (fn) == COMPONENT_REF)
2377 tree member = TREE_OPERAND (fn, 1);
2378 if (BASELINK_P (member))
2380 tree object = TREE_OPERAND (fn, 0);
2381 return build_new_method_call (object, member,
2382 args, NULL_TREE,
2383 (disallow_virtual
2384 ? LOOKUP_NORMAL | LOOKUP_NONVIRTUAL
2385 : LOOKUP_NORMAL),
2386 /*fn_p=*/NULL,
2387 complain);
2391 /* Per 13.3.1.1, '(&f)(...)' is the same as '(f)(...)'. */
2392 if (TREE_CODE (fn) == ADDR_EXPR
2393 && TREE_CODE (TREE_OPERAND (fn, 0)) == OVERLOAD)
2394 fn = TREE_OPERAND (fn, 0);
2396 if (is_overloaded_fn (fn))
2397 fn = baselink_for_fns (fn);
2399 result = NULL_TREE;
2400 if (BASELINK_P (fn))
2402 tree object;
2404 /* A call to a member function. From [over.call.func]:
2406 If the keyword this is in scope and refers to the class of
2407 that member function, or a derived class thereof, then the
2408 function call is transformed into a qualified function call
2409 using (*this) as the postfix-expression to the left of the
2410 . operator.... [Otherwise] a contrived object of type T
2411 becomes the implied object argument.
2413 In this situation:
2415 struct A { void f(); };
2416 struct B : public A {};
2417 struct C : public A { void g() { B::f(); }};
2419 "the class of that member function" refers to `A'. But 11.2
2420 [class.access.base] says that we need to convert 'this' to B* as
2421 part of the access, so we pass 'B' to maybe_dummy_object. */
2423 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (get_first_fn (fn)))
2425 /* A constructor call always uses a dummy object. (This constructor
2426 call which has the form A::A () is actually invalid and we are
2427 going to reject it later in build_new_method_call.) */
2428 object = build_dummy_object (BINFO_TYPE (BASELINK_ACCESS_BINFO (fn)));
2430 else
2431 object = maybe_dummy_object (BINFO_TYPE (BASELINK_ACCESS_BINFO (fn)),
2432 NULL);
2434 result = build_new_method_call (object, fn, args, NULL_TREE,
2435 (disallow_virtual
2436 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
2437 : LOOKUP_NORMAL),
2438 /*fn_p=*/NULL,
2439 complain);
2441 else if (is_overloaded_fn (fn))
2443 /* If the function is an overloaded builtin, resolve it. */
2444 if (TREE_CODE (fn) == FUNCTION_DECL
2445 && (DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL
2446 || DECL_BUILT_IN_CLASS (fn) == BUILT_IN_MD))
2447 result = resolve_overloaded_builtin (input_location, fn, *args);
2449 if (!result)
2451 if (warn_sizeof_pointer_memaccess
2452 && (complain & tf_warning)
2453 && !vec_safe_is_empty (*args)
2454 && !processing_template_decl)
2456 location_t sizeof_arg_loc[3];
2457 tree sizeof_arg[3];
2458 unsigned int i;
2459 for (i = 0; i < 3; i++)
2461 tree t;
2463 sizeof_arg_loc[i] = UNKNOWN_LOCATION;
2464 sizeof_arg[i] = NULL_TREE;
2465 if (i >= (*args)->length ())
2466 continue;
2467 t = (**args)[i];
2468 if (TREE_CODE (t) != SIZEOF_EXPR)
2469 continue;
2470 if (SIZEOF_EXPR_TYPE_P (t))
2471 sizeof_arg[i] = TREE_TYPE (TREE_OPERAND (t, 0));
2472 else
2473 sizeof_arg[i] = TREE_OPERAND (t, 0);
2474 sizeof_arg_loc[i] = EXPR_LOCATION (t);
2476 sizeof_pointer_memaccess_warning
2477 (sizeof_arg_loc, fn, *args,
2478 sizeof_arg, same_type_ignoring_top_level_qualifiers_p);
2481 /* A call to a namespace-scope function. */
2482 result = build_new_function_call (fn, args, complain);
2485 else if (TREE_CODE (fn) == PSEUDO_DTOR_EXPR)
2487 if (!vec_safe_is_empty (*args))
2488 error ("arguments to destructor are not allowed");
2489 /* Mark the pseudo-destructor call as having side-effects so
2490 that we do not issue warnings about its use. */
2491 result = build1 (NOP_EXPR,
2492 void_type_node,
2493 TREE_OPERAND (fn, 0));
2494 TREE_SIDE_EFFECTS (result) = 1;
2496 else if (CLASS_TYPE_P (TREE_TYPE (fn)))
2497 /* If the "function" is really an object of class type, it might
2498 have an overloaded `operator ()'. */
2499 result = build_op_call (fn, args, complain);
2501 if (!result)
2502 /* A call where the function is unknown. */
2503 result = cp_build_function_call_vec (fn, args, complain);
2505 if (processing_template_decl && result != error_mark_node)
2507 if (INDIRECT_REF_P (result))
2508 result = TREE_OPERAND (result, 0);
2509 result = build_call_vec (TREE_TYPE (result), orig_fn, orig_args);
2510 SET_EXPR_LOCATION (result, input_location);
2511 KOENIG_LOOKUP_P (result) = koenig_p;
2512 release_tree_vector (orig_args);
2513 result = convert_from_reference (result);
2516 /* Free or retain OVERLOADs from lookup. */
2517 if (is_overloaded_fn (orig_fn))
2518 lookup_keep (get_fns (orig_fn), processing_template_decl);
2520 return result;
2523 /* Finish a call to a postfix increment or decrement or EXPR. (Which
2524 is indicated by CODE, which should be POSTINCREMENT_EXPR or
2525 POSTDECREMENT_EXPR.) */
2527 cp_expr
2528 finish_increment_expr (cp_expr expr, enum tree_code code)
2530 /* input_location holds the location of the trailing operator token.
2531 Build a location of the form:
2532 expr++
2533 ~~~~^~
2534 with the caret at the operator token, ranging from the start
2535 of EXPR to the end of the operator token. */
2536 location_t combined_loc = make_location (input_location,
2537 expr.get_start (),
2538 get_finish (input_location));
2539 cp_expr result = build_x_unary_op (combined_loc, code, expr,
2540 tf_warning_or_error);
2541 /* TODO: build_x_unary_op doesn't honor the location, so set it here. */
2542 result.set_location (combined_loc);
2543 return result;
2546 /* Finish a use of `this'. Returns an expression for `this'. */
2548 tree
2549 finish_this_expr (void)
2551 tree result = NULL_TREE;
2553 if (current_class_ptr)
2555 tree type = TREE_TYPE (current_class_ref);
2557 /* In a lambda expression, 'this' refers to the captured 'this'. */
2558 if (LAMBDA_TYPE_P (type))
2559 result = lambda_expr_this_capture (CLASSTYPE_LAMBDA_EXPR (type), true);
2560 else
2561 result = current_class_ptr;
2564 if (result)
2565 /* The keyword 'this' is a prvalue expression. */
2566 return rvalue (result);
2568 tree fn = current_nonlambda_function ();
2569 if (fn && DECL_STATIC_FUNCTION_P (fn))
2570 error ("%<this%> is unavailable for static member functions");
2571 else if (fn)
2572 error ("invalid use of %<this%> in non-member function");
2573 else
2574 error ("invalid use of %<this%> at top level");
2575 return error_mark_node;
2578 /* Finish a pseudo-destructor expression. If SCOPE is NULL, the
2579 expression was of the form `OBJECT.~DESTRUCTOR' where DESTRUCTOR is
2580 the TYPE for the type given. If SCOPE is non-NULL, the expression
2581 was of the form `OBJECT.SCOPE::~DESTRUCTOR'. */
2583 tree
2584 finish_pseudo_destructor_expr (tree object, tree scope, tree destructor,
2585 location_t loc)
2587 if (object == error_mark_node || destructor == error_mark_node)
2588 return error_mark_node;
2590 gcc_assert (TYPE_P (destructor));
2592 if (!processing_template_decl)
2594 if (scope == error_mark_node)
2596 error_at (loc, "invalid qualifying scope in pseudo-destructor name");
2597 return error_mark_node;
2599 if (is_auto (destructor))
2600 destructor = TREE_TYPE (object);
2601 if (scope && TYPE_P (scope) && !check_dtor_name (scope, destructor))
2603 error_at (loc,
2604 "qualified type %qT does not match destructor name ~%qT",
2605 scope, destructor);
2606 return error_mark_node;
2610 /* [expr.pseudo] says both:
2612 The type designated by the pseudo-destructor-name shall be
2613 the same as the object type.
2615 and:
2617 The cv-unqualified versions of the object type and of the
2618 type designated by the pseudo-destructor-name shall be the
2619 same type.
2621 We implement the more generous second sentence, since that is
2622 what most other compilers do. */
2623 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (object),
2624 destructor))
2626 error_at (loc, "%qE is not of type %qT", object, destructor);
2627 return error_mark_node;
2631 return build3_loc (loc, PSEUDO_DTOR_EXPR, void_type_node, object,
2632 scope, destructor);
2635 /* Finish an expression of the form CODE EXPR. */
2637 cp_expr
2638 finish_unary_op_expr (location_t op_loc, enum tree_code code, cp_expr expr,
2639 tsubst_flags_t complain)
2641 /* Build a location of the form:
2642 ++expr
2643 ^~~~~~
2644 with the caret at the operator token, ranging from the start
2645 of the operator token to the end of EXPR. */
2646 location_t combined_loc = make_location (op_loc,
2647 op_loc, expr.get_finish ());
2648 cp_expr result = build_x_unary_op (combined_loc, code, expr, complain);
2649 /* TODO: build_x_unary_op doesn't always honor the location. */
2650 result.set_location (combined_loc);
2652 tree result_ovl, expr_ovl;
2654 if (!(complain & tf_warning))
2655 return result;
2657 result_ovl = result;
2658 expr_ovl = expr;
2660 if (!processing_template_decl)
2661 expr_ovl = cp_fully_fold (expr_ovl);
2663 if (!CONSTANT_CLASS_P (expr_ovl)
2664 || TREE_OVERFLOW_P (expr_ovl))
2665 return result;
2667 if (!processing_template_decl)
2668 result_ovl = cp_fully_fold (result_ovl);
2670 if (CONSTANT_CLASS_P (result_ovl) && TREE_OVERFLOW_P (result_ovl))
2671 overflow_warning (combined_loc, result_ovl);
2673 return result;
2676 /* Finish a compound-literal expression or C++11 functional cast with aggregate
2677 initializer. TYPE is the type to which the CONSTRUCTOR in COMPOUND_LITERAL
2678 is being cast. */
2680 tree
2681 finish_compound_literal (tree type, tree compound_literal,
2682 tsubst_flags_t complain,
2683 fcl_t fcl_context)
2685 if (type == error_mark_node)
2686 return error_mark_node;
2688 if (TREE_CODE (type) == REFERENCE_TYPE)
2690 compound_literal
2691 = finish_compound_literal (TREE_TYPE (type), compound_literal,
2692 complain, fcl_context);
2693 return cp_build_c_cast (type, compound_literal, complain);
2696 if (!TYPE_OBJ_P (type))
2698 if (complain & tf_error)
2699 error ("compound literal of non-object type %qT", type);
2700 return error_mark_node;
2703 if (tree anode = type_uses_auto (type))
2704 if (CLASS_PLACEHOLDER_TEMPLATE (anode))
2705 type = do_auto_deduction (type, compound_literal, anode, complain,
2706 adc_variable_type);
2708 if (processing_template_decl)
2710 TREE_TYPE (compound_literal) = type;
2711 /* Mark the expression as a compound literal. */
2712 TREE_HAS_CONSTRUCTOR (compound_literal) = 1;
2713 if (fcl_context == fcl_c99)
2714 CONSTRUCTOR_C99_COMPOUND_LITERAL (compound_literal) = 1;
2715 return compound_literal;
2718 type = complete_type (type);
2720 if (TYPE_NON_AGGREGATE_CLASS (type))
2722 /* Trying to deal with a CONSTRUCTOR instead of a TREE_LIST
2723 everywhere that deals with function arguments would be a pain, so
2724 just wrap it in a TREE_LIST. The parser set a flag so we know
2725 that it came from T{} rather than T({}). */
2726 CONSTRUCTOR_IS_DIRECT_INIT (compound_literal) = 1;
2727 compound_literal = build_tree_list (NULL_TREE, compound_literal);
2728 return build_functional_cast (type, compound_literal, complain);
2731 if (TREE_CODE (type) == ARRAY_TYPE
2732 && check_array_initializer (NULL_TREE, type, compound_literal))
2733 return error_mark_node;
2734 compound_literal = reshape_init (type, compound_literal, complain);
2735 if (SCALAR_TYPE_P (type)
2736 && !BRACE_ENCLOSED_INITIALIZER_P (compound_literal)
2737 && !check_narrowing (type, compound_literal, complain))
2738 return error_mark_node;
2739 if (TREE_CODE (type) == ARRAY_TYPE
2740 && TYPE_DOMAIN (type) == NULL_TREE)
2742 cp_complete_array_type_or_error (&type, compound_literal,
2743 false, complain);
2744 if (type == error_mark_node)
2745 return error_mark_node;
2747 compound_literal = digest_init_flags (type, compound_literal, LOOKUP_NORMAL,
2748 complain);
2749 if (TREE_CODE (compound_literal) == CONSTRUCTOR)
2751 TREE_HAS_CONSTRUCTOR (compound_literal) = true;
2752 if (fcl_context == fcl_c99)
2753 CONSTRUCTOR_C99_COMPOUND_LITERAL (compound_literal) = 1;
2756 /* Put static/constant array temporaries in static variables. */
2757 /* FIXME all C99 compound literals should be variables rather than C++
2758 temporaries, unless they are used as an aggregate initializer. */
2759 if ((!at_function_scope_p () || CP_TYPE_CONST_P (type))
2760 && fcl_context == fcl_c99
2761 && TREE_CODE (type) == ARRAY_TYPE
2762 && !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
2763 && initializer_constant_valid_p (compound_literal, type))
2765 tree decl = create_temporary_var (type);
2766 DECL_INITIAL (decl) = compound_literal;
2767 TREE_STATIC (decl) = 1;
2768 if (literal_type_p (type) && CP_TYPE_CONST_NON_VOLATILE_P (type))
2770 /* 5.19 says that a constant expression can include an
2771 lvalue-rvalue conversion applied to "a glvalue of literal type
2772 that refers to a non-volatile temporary object initialized
2773 with a constant expression". Rather than try to communicate
2774 that this VAR_DECL is a temporary, just mark it constexpr. */
2775 DECL_DECLARED_CONSTEXPR_P (decl) = true;
2776 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
2777 TREE_CONSTANT (decl) = true;
2779 cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
2780 decl = pushdecl_top_level (decl);
2781 DECL_NAME (decl) = make_anon_name ();
2782 SET_DECL_ASSEMBLER_NAME (decl, DECL_NAME (decl));
2783 /* Make sure the destructor is callable. */
2784 tree clean = cxx_maybe_build_cleanup (decl, complain);
2785 if (clean == error_mark_node)
2786 return error_mark_node;
2787 return decl;
2790 /* Represent other compound literals with TARGET_EXPR so we produce
2791 an lvalue, but can elide copies. */
2792 if (!VECTOR_TYPE_P (type))
2793 compound_literal = get_target_expr_sfinae (compound_literal, complain);
2795 return compound_literal;
2798 /* Return the declaration for the function-name variable indicated by
2799 ID. */
2801 tree
2802 finish_fname (tree id)
2804 tree decl;
2806 decl = fname_decl (input_location, C_RID_CODE (id), id);
2807 if (processing_template_decl && current_function_decl
2808 && decl != error_mark_node)
2809 decl = DECL_NAME (decl);
2810 return decl;
2813 /* Finish a translation unit. */
2815 void
2816 finish_translation_unit (void)
2818 /* In case there were missing closebraces,
2819 get us back to the global binding level. */
2820 pop_everything ();
2821 while (current_namespace != global_namespace)
2822 pop_namespace ();
2824 /* Do file scope __FUNCTION__ et al. */
2825 finish_fname_decls ();
2828 /* Finish a template type parameter, specified as AGGR IDENTIFIER.
2829 Returns the parameter. */
2831 tree
2832 finish_template_type_parm (tree aggr, tree identifier)
2834 if (aggr != class_type_node)
2836 permerror (input_location, "template type parameters must use the keyword %<class%> or %<typename%>");
2837 aggr = class_type_node;
2840 return build_tree_list (aggr, identifier);
2843 /* Finish a template template parameter, specified as AGGR IDENTIFIER.
2844 Returns the parameter. */
2846 tree
2847 finish_template_template_parm (tree aggr, tree identifier)
2849 tree decl = build_decl (input_location,
2850 TYPE_DECL, identifier, NULL_TREE);
2852 tree tmpl = build_lang_decl (TEMPLATE_DECL, identifier, NULL_TREE);
2853 DECL_TEMPLATE_PARMS (tmpl) = current_template_parms;
2854 DECL_TEMPLATE_RESULT (tmpl) = decl;
2855 DECL_ARTIFICIAL (decl) = 1;
2857 // Associate the constraints with the underlying declaration,
2858 // not the template.
2859 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
2860 tree constr = build_constraints (reqs, NULL_TREE);
2861 set_constraints (decl, constr);
2863 end_template_decl ();
2865 gcc_assert (DECL_TEMPLATE_PARMS (tmpl));
2867 check_default_tmpl_args (decl, DECL_TEMPLATE_PARMS (tmpl),
2868 /*is_primary=*/true, /*is_partial=*/false,
2869 /*is_friend=*/0);
2871 return finish_template_type_parm (aggr, tmpl);
2874 /* ARGUMENT is the default-argument value for a template template
2875 parameter. If ARGUMENT is invalid, issue error messages and return
2876 the ERROR_MARK_NODE. Otherwise, ARGUMENT itself is returned. */
2878 tree
2879 check_template_template_default_arg (tree argument)
2881 if (TREE_CODE (argument) != TEMPLATE_DECL
2882 && TREE_CODE (argument) != TEMPLATE_TEMPLATE_PARM
2883 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
2885 if (TREE_CODE (argument) == TYPE_DECL)
2886 error ("invalid use of type %qT as a default value for a template "
2887 "template-parameter", TREE_TYPE (argument));
2888 else
2889 error ("invalid default argument for a template template parameter");
2890 return error_mark_node;
2893 return argument;
2896 /* Begin a class definition, as indicated by T. */
2898 tree
2899 begin_class_definition (tree t)
2901 if (error_operand_p (t) || error_operand_p (TYPE_MAIN_DECL (t)))
2902 return error_mark_node;
2904 if (processing_template_parmlist)
2906 error ("definition of %q#T inside template parameter list", t);
2907 return error_mark_node;
2910 /* According to the C++ ABI, decimal classes defined in ISO/IEC TR 24733
2911 are passed the same as decimal scalar types. */
2912 if (TREE_CODE (t) == RECORD_TYPE
2913 && !processing_template_decl)
2915 tree ns = TYPE_CONTEXT (t);
2916 if (ns && TREE_CODE (ns) == NAMESPACE_DECL
2917 && DECL_CONTEXT (ns) == std_node
2918 && DECL_NAME (ns)
2919 && id_equal (DECL_NAME (ns), "decimal"))
2921 const char *n = TYPE_NAME_STRING (t);
2922 if ((strcmp (n, "decimal32") == 0)
2923 || (strcmp (n, "decimal64") == 0)
2924 || (strcmp (n, "decimal128") == 0))
2925 TYPE_TRANSPARENT_AGGR (t) = 1;
2929 /* A non-implicit typename comes from code like:
2931 template <typename T> struct A {
2932 template <typename U> struct A<T>::B ...
2934 This is erroneous. */
2935 else if (TREE_CODE (t) == TYPENAME_TYPE)
2937 error ("invalid definition of qualified type %qT", t);
2938 t = error_mark_node;
2941 if (t == error_mark_node || ! MAYBE_CLASS_TYPE_P (t))
2943 t = make_class_type (RECORD_TYPE);
2944 pushtag (make_anon_name (), t, /*tag_scope=*/ts_current);
2947 if (TYPE_BEING_DEFINED (t))
2949 t = make_class_type (TREE_CODE (t));
2950 pushtag (TYPE_IDENTIFIER (t), t, /*tag_scope=*/ts_current);
2952 maybe_process_partial_specialization (t);
2953 pushclass (t);
2954 TYPE_BEING_DEFINED (t) = 1;
2955 class_binding_level->defining_class_p = 1;
2957 if (flag_pack_struct)
2959 tree v;
2960 TYPE_PACKED (t) = 1;
2961 /* Even though the type is being defined for the first time
2962 here, there might have been a forward declaration, so there
2963 might be cv-qualified variants of T. */
2964 for (v = TYPE_NEXT_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v))
2965 TYPE_PACKED (v) = 1;
2967 /* Reset the interface data, at the earliest possible
2968 moment, as it might have been set via a class foo;
2969 before. */
2970 if (! TYPE_UNNAMED_P (t))
2972 struct c_fileinfo *finfo = \
2973 get_fileinfo (LOCATION_FILE (input_location));
2974 CLASSTYPE_INTERFACE_ONLY (t) = finfo->interface_only;
2975 SET_CLASSTYPE_INTERFACE_UNKNOWN_X
2976 (t, finfo->interface_unknown);
2978 reset_specialization();
2980 /* Make a declaration for this class in its own scope. */
2981 build_self_reference ();
2983 return t;
2986 /* Finish the member declaration given by DECL. */
2988 void
2989 finish_member_declaration (tree decl)
2991 if (decl == error_mark_node || decl == NULL_TREE)
2992 return;
2994 if (decl == void_type_node)
2995 /* The COMPONENT was a friend, not a member, and so there's
2996 nothing for us to do. */
2997 return;
2999 /* We should see only one DECL at a time. */
3000 gcc_assert (DECL_CHAIN (decl) == NULL_TREE);
3002 /* Don't add decls after definition. */
3003 gcc_assert (TYPE_BEING_DEFINED (current_class_type)
3004 /* We can add lambda types when late parsing default
3005 arguments. */
3006 || LAMBDA_TYPE_P (TREE_TYPE (decl)));
3008 /* Set up access control for DECL. */
3009 TREE_PRIVATE (decl)
3010 = (current_access_specifier == access_private_node);
3011 TREE_PROTECTED (decl)
3012 = (current_access_specifier == access_protected_node);
3013 if (TREE_CODE (decl) == TEMPLATE_DECL)
3015 TREE_PRIVATE (DECL_TEMPLATE_RESULT (decl)) = TREE_PRIVATE (decl);
3016 TREE_PROTECTED (DECL_TEMPLATE_RESULT (decl)) = TREE_PROTECTED (decl);
3019 /* Mark the DECL as a member of the current class, unless it's
3020 a member of an enumeration. */
3021 if (TREE_CODE (decl) != CONST_DECL)
3022 DECL_CONTEXT (decl) = current_class_type;
3024 /* Check for bare parameter packs in the member variable declaration. */
3025 if (TREE_CODE (decl) == FIELD_DECL)
3027 if (check_for_bare_parameter_packs (TREE_TYPE (decl)))
3028 TREE_TYPE (decl) = error_mark_node;
3029 if (check_for_bare_parameter_packs (DECL_ATTRIBUTES (decl)))
3030 DECL_ATTRIBUTES (decl) = NULL_TREE;
3033 /* [dcl.link]
3035 A C language linkage is ignored for the names of class members
3036 and the member function type of class member functions. */
3037 if (DECL_LANG_SPECIFIC (decl) && DECL_LANGUAGE (decl) == lang_c)
3038 SET_DECL_LANGUAGE (decl, lang_cplusplus);
3040 /* Put functions on the TYPE_METHODS list and everything else on the
3041 TYPE_FIELDS list. Note that these are built up in reverse order.
3042 We reverse them (to obtain declaration order) in finish_struct. */
3043 if (DECL_DECLARES_FUNCTION_P (decl))
3045 /* We also need to add this function to the
3046 CLASSTYPE_METHOD_VEC. */
3047 if (add_method (current_class_type, decl, false))
3049 gcc_assert (TYPE_MAIN_VARIANT (current_class_type) == current_class_type);
3050 DECL_CHAIN (decl) = TYPE_METHODS (current_class_type);
3051 TYPE_METHODS (current_class_type) = decl;
3053 maybe_add_class_template_decl_list (current_class_type, decl,
3054 /*friend_p=*/0);
3057 /* Enter the DECL into the scope of the class, if the class
3058 isn't a closure (whose fields are supposed to be unnamed). */
3059 else if (CLASSTYPE_LAMBDA_EXPR (current_class_type)
3060 || pushdecl_class_level (decl))
3062 if (TREE_CODE (decl) == USING_DECL)
3064 /* For now, ignore class-scope USING_DECLS, so that
3065 debugging backends do not see them. */
3066 DECL_IGNORED_P (decl) = 1;
3069 /* All TYPE_DECLs go at the end of TYPE_FIELDS. Ordinary fields
3070 go at the beginning. The reason is that lookup_field_1
3071 searches the list in order, and we want a field name to
3072 override a type name so that the "struct stat hack" will
3073 work. In particular:
3075 struct S { enum E { }; int E } s;
3076 s.E = 3;
3078 is valid. In addition, the FIELD_DECLs must be maintained in
3079 declaration order so that class layout works as expected.
3080 However, we don't need that order until class layout, so we
3081 save a little time by putting FIELD_DECLs on in reverse order
3082 here, and then reversing them in finish_struct_1. (We could
3083 also keep a pointer to the correct insertion points in the
3084 list.) */
3086 if (TREE_CODE (decl) == TYPE_DECL)
3087 TYPE_FIELDS (current_class_type)
3088 = chainon (TYPE_FIELDS (current_class_type), decl);
3089 else
3091 DECL_CHAIN (decl) = TYPE_FIELDS (current_class_type);
3092 TYPE_FIELDS (current_class_type) = decl;
3095 maybe_add_class_template_decl_list (current_class_type, decl,
3096 /*friend_p=*/0);
3100 /* Finish processing a complete template declaration. The PARMS are
3101 the template parameters. */
3103 void
3104 finish_template_decl (tree parms)
3106 if (parms)
3107 end_template_decl ();
3108 else
3109 end_specialization ();
3112 // Returns the template type of the class scope being entered. If we're
3113 // entering a constrained class scope. TYPE is the class template
3114 // scope being entered and we may need to match the intended type with
3115 // a constrained specialization. For example:
3117 // template<Object T>
3118 // struct S { void f(); }; #1
3120 // template<Object T>
3121 // void S<T>::f() { } #2
3123 // We check, in #2, that S<T> refers precisely to the type declared by
3124 // #1 (i.e., that the constraints match). Note that the following should
3125 // be an error since there is no specialization of S<T> that is
3126 // unconstrained, but this is not diagnosed here.
3128 // template<typename T>
3129 // void S<T>::f() { }
3131 // We cannot diagnose this problem here since this function also matches
3132 // qualified template names that are not part of a definition. For example:
3134 // template<Integral T, Floating_point U>
3135 // typename pair<T, U>::first_type void f(T, U);
3137 // Here, it is unlikely that there is a partial specialization of
3138 // pair constrained for for Integral and Floating_point arguments.
3140 // The general rule is: if a constrained specialization with matching
3141 // constraints is found return that type. Also note that if TYPE is not a
3142 // class-type (e.g. a typename type), then no fixup is needed.
3144 static tree
3145 fixup_template_type (tree type)
3147 // Find the template parameter list at the a depth appropriate to
3148 // the scope we're trying to enter.
3149 tree parms = current_template_parms;
3150 int depth = template_class_depth (type);
3151 for (int n = processing_template_decl; n > depth && parms; --n)
3152 parms = TREE_CHAIN (parms);
3153 if (!parms)
3154 return type;
3155 tree cur_reqs = TEMPLATE_PARMS_CONSTRAINTS (parms);
3156 tree cur_constr = build_constraints (cur_reqs, NULL_TREE);
3158 // Search for a specialization whose type and constraints match.
3159 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
3160 tree specs = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
3161 while (specs)
3163 tree spec_constr = get_constraints (TREE_VALUE (specs));
3165 // If the type and constraints match a specialization, then we
3166 // are entering that type.
3167 if (same_type_p (type, TREE_TYPE (specs))
3168 && equivalent_constraints (cur_constr, spec_constr))
3169 return TREE_TYPE (specs);
3170 specs = TREE_CHAIN (specs);
3173 // If no specialization matches, then must return the type
3174 // previously found.
3175 return type;
3178 /* Finish processing a template-id (which names a type) of the form
3179 NAME < ARGS >. Return the TYPE_DECL for the type named by the
3180 template-id. If ENTERING_SCOPE is nonzero we are about to enter
3181 the scope of template-id indicated. */
3183 tree
3184 finish_template_type (tree name, tree args, int entering_scope)
3186 tree type;
3188 type = lookup_template_class (name, args,
3189 NULL_TREE, NULL_TREE, entering_scope,
3190 tf_warning_or_error | tf_user);
3192 /* If we might be entering the scope of a partial specialization,
3193 find the one with the right constraints. */
3194 if (flag_concepts
3195 && entering_scope
3196 && CLASS_TYPE_P (type)
3197 && CLASSTYPE_TEMPLATE_INFO (type)
3198 && dependent_type_p (type)
3199 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
3200 type = fixup_template_type (type);
3202 if (type == error_mark_node)
3203 return type;
3204 else if (CLASS_TYPE_P (type) && !alias_type_or_template_p (type))
3205 return TYPE_STUB_DECL (type);
3206 else
3207 return TYPE_NAME (type);
3210 /* Finish processing a BASE_CLASS with the indicated ACCESS_SPECIFIER.
3211 Return a TREE_LIST containing the ACCESS_SPECIFIER and the
3212 BASE_CLASS, or NULL_TREE if an error occurred. The
3213 ACCESS_SPECIFIER is one of
3214 access_{default,public,protected_private}_node. For a virtual base
3215 we set TREE_TYPE. */
3217 tree
3218 finish_base_specifier (tree base, tree access, bool virtual_p)
3220 tree result;
3222 if (base == error_mark_node)
3224 error ("invalid base-class specification");
3225 result = NULL_TREE;
3227 else if (! MAYBE_CLASS_TYPE_P (base))
3229 error ("%qT is not a class type", base);
3230 result = NULL_TREE;
3232 else
3234 if (cp_type_quals (base) != 0)
3236 /* DR 484: Can a base-specifier name a cv-qualified
3237 class type? */
3238 base = TYPE_MAIN_VARIANT (base);
3240 result = build_tree_list (access, base);
3241 if (virtual_p)
3242 TREE_TYPE (result) = integer_type_node;
3245 return result;
3248 /* If FNS is a member function, a set of member functions, or a
3249 template-id referring to one or more member functions, return a
3250 BASELINK for FNS, incorporating the current access context.
3251 Otherwise, return FNS unchanged. */
3253 tree
3254 baselink_for_fns (tree fns)
3256 tree scope;
3257 tree cl;
3259 if (BASELINK_P (fns)
3260 || error_operand_p (fns))
3261 return fns;
3263 scope = ovl_scope (fns);
3264 if (!CLASS_TYPE_P (scope))
3265 return fns;
3267 cl = currently_open_derived_class (scope);
3268 if (!cl)
3269 cl = scope;
3270 cl = TYPE_BINFO (cl);
3271 return build_baselink (cl, cl, fns, /*optype=*/NULL_TREE);
3274 /* Returns true iff DECL is a variable from a function outside
3275 the current one. */
3277 static bool
3278 outer_var_p (tree decl)
3280 return ((VAR_P (decl) || TREE_CODE (decl) == PARM_DECL)
3281 && DECL_FUNCTION_SCOPE_P (decl)
3282 && (DECL_CONTEXT (decl) != current_function_decl
3283 || parsing_nsdmi ()));
3286 /* As above, but also checks that DECL is automatic. */
3288 bool
3289 outer_automatic_var_p (tree decl)
3291 return (outer_var_p (decl)
3292 && !TREE_STATIC (decl));
3295 /* DECL satisfies outer_automatic_var_p. Possibly complain about it or
3296 rewrite it for lambda capture. */
3298 tree
3299 process_outer_var_ref (tree decl, tsubst_flags_t complain)
3301 if (cp_unevaluated_operand)
3302 /* It's not a use (3.2) if we're in an unevaluated context. */
3303 return decl;
3304 if (decl == error_mark_node)
3305 return decl;
3307 tree context = DECL_CONTEXT (decl);
3308 tree containing_function = current_function_decl;
3309 tree lambda_stack = NULL_TREE;
3310 tree lambda_expr = NULL_TREE;
3311 tree initializer = convert_from_reference (decl);
3313 /* Mark it as used now even if the use is ill-formed. */
3314 if (!mark_used (decl, complain))
3315 return error_mark_node;
3317 bool saw_generic_lambda = false;
3318 if (parsing_nsdmi ())
3319 containing_function = NULL_TREE;
3320 else
3321 /* If we are in a lambda function, we can move out until we hit
3322 1. the context,
3323 2. a non-lambda function, or
3324 3. a non-default capturing lambda function. */
3325 while (context != containing_function
3326 /* containing_function can be null with invalid generic lambdas. */
3327 && containing_function
3328 && LAMBDA_FUNCTION_P (containing_function))
3330 tree closure = DECL_CONTEXT (containing_function);
3331 lambda_expr = CLASSTYPE_LAMBDA_EXPR (closure);
3333 if (generic_lambda_fn_p (containing_function))
3334 saw_generic_lambda = true;
3336 if (TYPE_CLASS_SCOPE_P (closure))
3337 /* A lambda in an NSDMI (c++/64496). */
3338 break;
3340 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr)
3341 == CPLD_NONE)
3342 break;
3344 lambda_stack = tree_cons (NULL_TREE,
3345 lambda_expr,
3346 lambda_stack);
3348 containing_function
3349 = decl_function_context (containing_function);
3352 /* Core issue 696: "[At the July 2009 meeting] the CWG expressed
3353 support for an approach in which a reference to a local
3354 [constant] automatic variable in a nested class or lambda body
3355 would enter the expression as an rvalue, which would reduce
3356 the complexity of the problem"
3358 FIXME update for final resolution of core issue 696. */
3359 if (decl_maybe_constant_var_p (decl))
3361 if (processing_template_decl && !saw_generic_lambda)
3362 /* In a non-generic lambda within a template, wait until instantiation
3363 time to decide whether to capture. For a generic lambda, we can't
3364 wait until we instantiate the op() because the closure class is
3365 already defined at that point. FIXME to get the semantics exactly
3366 right we need to partially-instantiate the lambda body so the only
3367 dependencies left are on the generic parameters themselves. This
3368 probably means moving away from our current model of lambdas in
3369 templates (instantiating the closure type) to one based on creating
3370 the closure type when instantiating the lambda context. That is
3371 probably also the way to handle lambdas within pack expansions. */
3372 return decl;
3373 else if (decl_constant_var_p (decl))
3375 tree t = maybe_constant_value (convert_from_reference (decl));
3376 if (TREE_CONSTANT (t))
3377 return t;
3381 if (lambda_expr && VAR_P (decl)
3382 && DECL_ANON_UNION_VAR_P (decl))
3384 if (complain & tf_error)
3385 error ("cannot capture member %qD of anonymous union", decl);
3386 return error_mark_node;
3388 if (context == containing_function)
3390 decl = add_default_capture (lambda_stack,
3391 /*id=*/DECL_NAME (decl),
3392 initializer);
3394 else if (lambda_expr)
3396 if (complain & tf_error)
3398 error ("%qD is not captured", decl);
3399 tree closure = LAMBDA_EXPR_CLOSURE (lambda_expr);
3400 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr)
3401 == CPLD_NONE)
3402 inform (location_of (closure),
3403 "the lambda has no capture-default");
3404 else if (TYPE_CLASS_SCOPE_P (closure))
3405 inform (0, "lambda in local class %q+T cannot "
3406 "capture variables from the enclosing context",
3407 TYPE_CONTEXT (closure));
3408 inform (DECL_SOURCE_LOCATION (decl), "%q#D declared here", decl);
3410 return error_mark_node;
3412 else
3414 if (complain & tf_error)
3416 error (VAR_P (decl)
3417 ? G_("use of local variable with automatic storage from "
3418 "containing function")
3419 : G_("use of parameter from containing function"));
3420 inform (DECL_SOURCE_LOCATION (decl), "%q#D declared here", decl);
3422 return error_mark_node;
3424 return decl;
3427 /* ID_EXPRESSION is a representation of parsed, but unprocessed,
3428 id-expression. (See cp_parser_id_expression for details.) SCOPE,
3429 if non-NULL, is the type or namespace used to explicitly qualify
3430 ID_EXPRESSION. DECL is the entity to which that name has been
3431 resolved.
3433 *CONSTANT_EXPRESSION_P is true if we are presently parsing a
3434 constant-expression. In that case, *NON_CONSTANT_EXPRESSION_P will
3435 be set to true if this expression isn't permitted in a
3436 constant-expression, but it is otherwise not set by this function.
3437 *ALLOW_NON_CONSTANT_EXPRESSION_P is true if we are parsing a
3438 constant-expression, but a non-constant expression is also
3439 permissible.
3441 DONE is true if this expression is a complete postfix-expression;
3442 it is false if this expression is followed by '->', '[', '(', etc.
3443 ADDRESS_P is true iff this expression is the operand of '&'.
3444 TEMPLATE_P is true iff the qualified-id was of the form
3445 "A::template B". TEMPLATE_ARG_P is true iff this qualified name
3446 appears as a template argument.
3448 If an error occurs, and it is the kind of error that might cause
3449 the parser to abort a tentative parse, *ERROR_MSG is filled in. It
3450 is the caller's responsibility to issue the message. *ERROR_MSG
3451 will be a string with static storage duration, so the caller need
3452 not "free" it.
3454 Return an expression for the entity, after issuing appropriate
3455 diagnostics. This function is also responsible for transforming a
3456 reference to a non-static member into a COMPONENT_REF that makes
3457 the use of "this" explicit.
3459 Upon return, *IDK will be filled in appropriately. */
3460 cp_expr
3461 finish_id_expression (tree id_expression,
3462 tree decl,
3463 tree scope,
3464 cp_id_kind *idk,
3465 bool integral_constant_expression_p,
3466 bool allow_non_integral_constant_expression_p,
3467 bool *non_integral_constant_expression_p,
3468 bool template_p,
3469 bool done,
3470 bool address_p,
3471 bool template_arg_p,
3472 const char **error_msg,
3473 location_t location)
3475 decl = strip_using_decl (decl);
3477 /* Initialize the output parameters. */
3478 *idk = CP_ID_KIND_NONE;
3479 *error_msg = NULL;
3481 if (id_expression == error_mark_node)
3482 return error_mark_node;
3483 /* If we have a template-id, then no further lookup is
3484 required. If the template-id was for a template-class, we
3485 will sometimes have a TYPE_DECL at this point. */
3486 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3487 || TREE_CODE (decl) == TYPE_DECL)
3489 /* Look up the name. */
3490 else
3492 if (decl == error_mark_node)
3494 /* Name lookup failed. */
3495 if (scope
3496 && (!TYPE_P (scope)
3497 || (!dependent_type_p (scope)
3498 && !(identifier_p (id_expression)
3499 && IDENTIFIER_CONV_OP_P (id_expression)
3500 && dependent_type_p (TREE_TYPE (id_expression))))))
3502 /* If the qualifying type is non-dependent (and the name
3503 does not name a conversion operator to a dependent
3504 type), issue an error. */
3505 qualified_name_lookup_error (scope, id_expression, decl, location);
3506 return error_mark_node;
3508 else if (!scope)
3510 /* It may be resolved via Koenig lookup. */
3511 *idk = CP_ID_KIND_UNQUALIFIED;
3512 return id_expression;
3514 else
3515 decl = id_expression;
3517 /* If DECL is a variable that would be out of scope under
3518 ANSI/ISO rules, but in scope in the ARM, name lookup
3519 will succeed. Issue a diagnostic here. */
3520 else
3521 decl = check_for_out_of_scope_variable (decl);
3523 /* Remember that the name was used in the definition of
3524 the current class so that we can check later to see if
3525 the meaning would have been different after the class
3526 was entirely defined. */
3527 if (!scope && decl != error_mark_node && identifier_p (id_expression))
3528 maybe_note_name_used_in_class (id_expression, decl);
3530 /* A use in unevaluated operand might not be instantiated appropriately
3531 if tsubst_copy builds a dummy parm, or if we never instantiate a
3532 generic lambda, so mark it now. */
3533 if (processing_template_decl && cp_unevaluated_operand)
3534 mark_type_use (decl);
3536 /* Disallow uses of local variables from containing functions, except
3537 within lambda-expressions. */
3538 if (outer_automatic_var_p (decl))
3540 decl = process_outer_var_ref (decl, tf_warning_or_error);
3541 if (decl == error_mark_node)
3542 return error_mark_node;
3545 /* Also disallow uses of function parameters outside the function
3546 body, except inside an unevaluated context (i.e. decltype). */
3547 if (TREE_CODE (decl) == PARM_DECL
3548 && DECL_CONTEXT (decl) == NULL_TREE
3549 && !cp_unevaluated_operand)
3551 *error_msg = G_("use of parameter outside function body");
3552 return error_mark_node;
3556 /* If we didn't find anything, or what we found was a type,
3557 then this wasn't really an id-expression. */
3558 if (TREE_CODE (decl) == TEMPLATE_DECL
3559 && !DECL_FUNCTION_TEMPLATE_P (decl))
3561 *error_msg = G_("missing template arguments");
3562 return error_mark_node;
3564 else if (TREE_CODE (decl) == TYPE_DECL
3565 || TREE_CODE (decl) == NAMESPACE_DECL)
3567 *error_msg = G_("expected primary-expression");
3568 return error_mark_node;
3571 /* If the name resolved to a template parameter, there is no
3572 need to look it up again later. */
3573 if ((TREE_CODE (decl) == CONST_DECL && DECL_TEMPLATE_PARM_P (decl))
3574 || TREE_CODE (decl) == TEMPLATE_PARM_INDEX)
3576 tree r;
3578 *idk = CP_ID_KIND_NONE;
3579 if (TREE_CODE (decl) == TEMPLATE_PARM_INDEX)
3580 decl = TEMPLATE_PARM_DECL (decl);
3581 r = convert_from_reference (DECL_INITIAL (decl));
3583 if (integral_constant_expression_p
3584 && !dependent_type_p (TREE_TYPE (decl))
3585 && !(INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (r))))
3587 if (!allow_non_integral_constant_expression_p)
3588 error ("template parameter %qD of type %qT is not allowed in "
3589 "an integral constant expression because it is not of "
3590 "integral or enumeration type", decl, TREE_TYPE (decl));
3591 *non_integral_constant_expression_p = true;
3593 return r;
3595 else
3597 bool dependent_p = type_dependent_expression_p (decl);
3599 /* If the declaration was explicitly qualified indicate
3600 that. The semantics of `A::f(3)' are different than
3601 `f(3)' if `f' is virtual. */
3602 *idk = (scope
3603 ? CP_ID_KIND_QUALIFIED
3604 : (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3605 ? CP_ID_KIND_TEMPLATE_ID
3606 : (dependent_p
3607 ? CP_ID_KIND_UNQUALIFIED_DEPENDENT
3608 : CP_ID_KIND_UNQUALIFIED)));
3610 /* If the name was dependent on a template parameter and we're not in a
3611 default capturing generic lambda within a template, we will resolve the
3612 name at instantiation time. FIXME: For lambdas, we should defer
3613 building the closure type until instantiation time then we won't need
3614 the extra test here. */
3615 if (dependent_p
3616 && !parsing_default_capturing_generic_lambda_in_template ())
3618 if (DECL_P (decl)
3619 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (decl)))
3620 /* Dependent type attributes on the decl mean that the TREE_TYPE is
3621 wrong, so just return the identifier. */
3622 return id_expression;
3624 /* If we found a variable, then name lookup during the
3625 instantiation will always resolve to the same VAR_DECL
3626 (or an instantiation thereof). */
3627 if (VAR_P (decl)
3628 || TREE_CODE (decl) == CONST_DECL
3629 || TREE_CODE (decl) == PARM_DECL)
3631 mark_used (decl);
3632 return convert_from_reference (decl);
3635 /* Create a SCOPE_REF for qualified names, if the scope is
3636 dependent. */
3637 if (scope)
3639 if (TYPE_P (scope))
3641 if (address_p && done)
3642 decl = finish_qualified_id_expr (scope, decl,
3643 done, address_p,
3644 template_p,
3645 template_arg_p,
3646 tf_warning_or_error);
3647 else
3649 tree type = NULL_TREE;
3650 if (DECL_P (decl) && !dependent_scope_p (scope))
3651 type = TREE_TYPE (decl);
3652 decl = build_qualified_name (type,
3653 scope,
3654 id_expression,
3655 template_p);
3658 if (TREE_TYPE (decl))
3659 decl = convert_from_reference (decl);
3660 return decl;
3662 /* A TEMPLATE_ID already contains all the information we
3663 need. */
3664 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR)
3665 return id_expression;
3666 /* The same is true for FIELD_DECL, but we also need to
3667 make sure that the syntax is correct. */
3668 else if (TREE_CODE (decl) == FIELD_DECL)
3670 /* Since SCOPE is NULL here, this is an unqualified name.
3671 Access checking has been performed during name lookup
3672 already. Turn off checking to avoid duplicate errors. */
3673 push_deferring_access_checks (dk_no_check);
3674 decl = finish_non_static_data_member
3675 (decl, NULL_TREE,
3676 /*qualifying_scope=*/NULL_TREE);
3677 pop_deferring_access_checks ();
3678 return decl;
3680 return id_expression;
3683 if (TREE_CODE (decl) == NAMESPACE_DECL)
3685 error ("use of namespace %qD as expression", decl);
3686 return error_mark_node;
3688 else if (DECL_CLASS_TEMPLATE_P (decl))
3690 error ("use of class template %qT as expression", decl);
3691 return error_mark_node;
3693 else if (TREE_CODE (decl) == TREE_LIST)
3695 /* Ambiguous reference to base members. */
3696 error ("request for member %qD is ambiguous in "
3697 "multiple inheritance lattice", id_expression);
3698 print_candidates (decl);
3699 return error_mark_node;
3702 /* Mark variable-like entities as used. Functions are similarly
3703 marked either below or after overload resolution. */
3704 if ((VAR_P (decl)
3705 || TREE_CODE (decl) == PARM_DECL
3706 || TREE_CODE (decl) == CONST_DECL
3707 || TREE_CODE (decl) == RESULT_DECL)
3708 && !mark_used (decl))
3709 return error_mark_node;
3711 /* Only certain kinds of names are allowed in constant
3712 expression. Template parameters have already
3713 been handled above. */
3714 if (! error_operand_p (decl)
3715 && integral_constant_expression_p
3716 && ! decl_constant_var_p (decl)
3717 && TREE_CODE (decl) != CONST_DECL
3718 && ! builtin_valid_in_constant_expr_p (decl))
3720 if (!allow_non_integral_constant_expression_p)
3722 error ("%qD cannot appear in a constant-expression", decl);
3723 return error_mark_node;
3725 *non_integral_constant_expression_p = true;
3728 tree wrap;
3729 if (VAR_P (decl)
3730 && !cp_unevaluated_operand
3731 && !processing_template_decl
3732 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl))
3733 && CP_DECL_THREAD_LOCAL_P (decl)
3734 && (wrap = get_tls_wrapper_fn (decl)))
3736 /* Replace an evaluated use of the thread_local variable with
3737 a call to its wrapper. */
3738 decl = build_cxx_call (wrap, 0, NULL, tf_warning_or_error);
3740 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3741 && variable_template_p (TREE_OPERAND (decl, 0)))
3743 decl = finish_template_variable (decl);
3744 mark_used (decl);
3745 decl = convert_from_reference (decl);
3747 else if (scope)
3749 decl = (adjust_result_of_qualified_name_lookup
3750 (decl, scope, current_nonlambda_class_type()));
3752 if (TREE_CODE (decl) == FUNCTION_DECL)
3753 mark_used (decl);
3755 if (TYPE_P (scope))
3756 decl = finish_qualified_id_expr (scope,
3757 decl,
3758 done,
3759 address_p,
3760 template_p,
3761 template_arg_p,
3762 tf_warning_or_error);
3763 else
3764 decl = convert_from_reference (decl);
3766 else if (TREE_CODE (decl) == FIELD_DECL)
3768 /* Since SCOPE is NULL here, this is an unqualified name.
3769 Access checking has been performed during name lookup
3770 already. Turn off checking to avoid duplicate errors. */
3771 push_deferring_access_checks (dk_no_check);
3772 decl = finish_non_static_data_member (decl, NULL_TREE,
3773 /*qualifying_scope=*/NULL_TREE);
3774 pop_deferring_access_checks ();
3776 else if (is_overloaded_fn (decl))
3778 tree first_fn = get_first_fn (decl);
3780 if (TREE_CODE (first_fn) == TEMPLATE_DECL)
3781 first_fn = DECL_TEMPLATE_RESULT (first_fn);
3783 /* [basic.def.odr]: "A function whose name appears as a
3784 potentially-evaluated expression is odr-used if it is the unique
3785 lookup result".
3787 But only mark it if it's a complete postfix-expression; in a call,
3788 ADL might select a different function, and we'll call mark_used in
3789 build_over_call. */
3790 if (done
3791 && !really_overloaded_fn (decl)
3792 && !mark_used (first_fn))
3793 return error_mark_node;
3795 if (!template_arg_p
3796 && TREE_CODE (first_fn) == FUNCTION_DECL
3797 && DECL_FUNCTION_MEMBER_P (first_fn)
3798 && !shared_member_p (decl))
3800 /* A set of member functions. */
3801 decl = maybe_dummy_object (DECL_CONTEXT (first_fn), 0);
3802 return finish_class_member_access_expr (decl, id_expression,
3803 /*template_p=*/false,
3804 tf_warning_or_error);
3807 decl = baselink_for_fns (decl);
3809 else
3811 if (DECL_P (decl) && DECL_NONLOCAL (decl)
3812 && DECL_CLASS_SCOPE_P (decl))
3814 tree context = context_for_name_lookup (decl);
3815 if (context != current_class_type)
3817 tree path = currently_open_derived_class (context);
3818 perform_or_defer_access_check (TYPE_BINFO (path),
3819 decl, decl,
3820 tf_warning_or_error);
3824 decl = convert_from_reference (decl);
3828 return cp_expr (decl, location);
3831 /* Implement the __typeof keyword: Return the type of EXPR, suitable for
3832 use as a type-specifier. */
3834 tree
3835 finish_typeof (tree expr)
3837 tree type;
3839 if (type_dependent_expression_p (expr))
3841 type = cxx_make_type (TYPEOF_TYPE);
3842 TYPEOF_TYPE_EXPR (type) = expr;
3843 SET_TYPE_STRUCTURAL_EQUALITY (type);
3845 return type;
3848 expr = mark_type_use (expr);
3850 type = unlowered_expr_type (expr);
3852 if (!type || type == unknown_type_node)
3854 error ("type of %qE is unknown", expr);
3855 return error_mark_node;
3858 return type;
3861 /* Implement the __underlying_type keyword: Return the underlying
3862 type of TYPE, suitable for use as a type-specifier. */
3864 tree
3865 finish_underlying_type (tree type)
3867 tree underlying_type;
3869 if (processing_template_decl)
3871 underlying_type = cxx_make_type (UNDERLYING_TYPE);
3872 UNDERLYING_TYPE_TYPE (underlying_type) = type;
3873 SET_TYPE_STRUCTURAL_EQUALITY (underlying_type);
3875 return underlying_type;
3878 if (!complete_type_or_else (type, NULL_TREE))
3879 return error_mark_node;
3881 if (TREE_CODE (type) != ENUMERAL_TYPE)
3883 error ("%qT is not an enumeration type", type);
3884 return error_mark_node;
3887 underlying_type = ENUM_UNDERLYING_TYPE (type);
3889 /* Fixup necessary in this case because ENUM_UNDERLYING_TYPE
3890 includes TYPE_MIN_VALUE and TYPE_MAX_VALUE information.
3891 See finish_enum_value_list for details. */
3892 if (!ENUM_FIXED_UNDERLYING_TYPE_P (type))
3893 underlying_type
3894 = c_common_type_for_mode (TYPE_MODE (underlying_type),
3895 TYPE_UNSIGNED (underlying_type));
3897 return underlying_type;
3900 /* Implement the __direct_bases keyword: Return the direct base classes
3901 of type */
3903 tree
3904 calculate_direct_bases (tree type)
3906 vec<tree, va_gc> *vector = make_tree_vector();
3907 tree bases_vec = NULL_TREE;
3908 vec<tree, va_gc> *base_binfos;
3909 tree binfo;
3910 unsigned i;
3912 complete_type (type);
3914 if (!NON_UNION_CLASS_TYPE_P (type))
3915 return make_tree_vec (0);
3917 base_binfos = BINFO_BASE_BINFOS (TYPE_BINFO (type));
3919 /* Virtual bases are initialized first */
3920 for (i = 0; base_binfos->iterate (i, &binfo); i++)
3922 if (BINFO_VIRTUAL_P (binfo))
3924 vec_safe_push (vector, binfo);
3928 /* Now non-virtuals */
3929 for (i = 0; base_binfos->iterate (i, &binfo); i++)
3931 if (!BINFO_VIRTUAL_P (binfo))
3933 vec_safe_push (vector, binfo);
3938 bases_vec = make_tree_vec (vector->length ());
3940 for (i = 0; i < vector->length (); ++i)
3942 TREE_VEC_ELT (bases_vec, i) = BINFO_TYPE ((*vector)[i]);
3944 return bases_vec;
3947 /* Implement the __bases keyword: Return the base classes
3948 of type */
3950 /* Find morally non-virtual base classes by walking binfo hierarchy */
3951 /* Virtual base classes are handled separately in finish_bases */
3953 static tree
3954 dfs_calculate_bases_pre (tree binfo, void * /*data_*/)
3956 /* Don't walk bases of virtual bases */
3957 return BINFO_VIRTUAL_P (binfo) ? dfs_skip_bases : NULL_TREE;
3960 static tree
3961 dfs_calculate_bases_post (tree binfo, void *data_)
3963 vec<tree, va_gc> **data = ((vec<tree, va_gc> **) data_);
3964 if (!BINFO_VIRTUAL_P (binfo))
3966 vec_safe_push (*data, BINFO_TYPE (binfo));
3968 return NULL_TREE;
3971 /* Calculates the morally non-virtual base classes of a class */
3972 static vec<tree, va_gc> *
3973 calculate_bases_helper (tree type)
3975 vec<tree, va_gc> *vector = make_tree_vector();
3977 /* Now add non-virtual base classes in order of construction */
3978 if (TYPE_BINFO (type))
3979 dfs_walk_all (TYPE_BINFO (type),
3980 dfs_calculate_bases_pre, dfs_calculate_bases_post, &vector);
3981 return vector;
3984 tree
3985 calculate_bases (tree type)
3987 vec<tree, va_gc> *vector = make_tree_vector();
3988 tree bases_vec = NULL_TREE;
3989 unsigned i;
3990 vec<tree, va_gc> *vbases;
3991 vec<tree, va_gc> *nonvbases;
3992 tree binfo;
3994 complete_type (type);
3996 if (!NON_UNION_CLASS_TYPE_P (type))
3997 return make_tree_vec (0);
3999 /* First go through virtual base classes */
4000 for (vbases = CLASSTYPE_VBASECLASSES (type), i = 0;
4001 vec_safe_iterate (vbases, i, &binfo); i++)
4003 vec<tree, va_gc> *vbase_bases;
4004 vbase_bases = calculate_bases_helper (BINFO_TYPE (binfo));
4005 vec_safe_splice (vector, vbase_bases);
4006 release_tree_vector (vbase_bases);
4009 /* Now for the non-virtual bases */
4010 nonvbases = calculate_bases_helper (type);
4011 vec_safe_splice (vector, nonvbases);
4012 release_tree_vector (nonvbases);
4014 /* Note that during error recovery vector->length can even be zero. */
4015 if (vector->length () > 1)
4017 /* Last element is entire class, so don't copy */
4018 bases_vec = make_tree_vec (vector->length() - 1);
4020 for (i = 0; i < vector->length () - 1; ++i)
4021 TREE_VEC_ELT (bases_vec, i) = (*vector)[i];
4023 else
4024 bases_vec = make_tree_vec (0);
4026 release_tree_vector (vector);
4027 return bases_vec;
4030 tree
4031 finish_bases (tree type, bool direct)
4033 tree bases = NULL_TREE;
4035 if (!processing_template_decl)
4037 /* Parameter packs can only be used in templates */
4038 error ("Parameter pack __bases only valid in template declaration");
4039 return error_mark_node;
4042 bases = cxx_make_type (BASES);
4043 BASES_TYPE (bases) = type;
4044 BASES_DIRECT (bases) = direct;
4045 SET_TYPE_STRUCTURAL_EQUALITY (bases);
4047 return bases;
4050 /* Perform C++-specific checks for __builtin_offsetof before calling
4051 fold_offsetof. */
4053 tree
4054 finish_offsetof (tree object_ptr, tree expr, location_t loc)
4056 /* If we're processing a template, we can't finish the semantics yet.
4057 Otherwise we can fold the entire expression now. */
4058 if (processing_template_decl)
4060 expr = build2 (OFFSETOF_EXPR, size_type_node, expr, object_ptr);
4061 SET_EXPR_LOCATION (expr, loc);
4062 return expr;
4065 if (TREE_CODE (expr) == PSEUDO_DTOR_EXPR)
4067 error ("cannot apply %<offsetof%> to destructor %<~%T%>",
4068 TREE_OPERAND (expr, 2));
4069 return error_mark_node;
4071 if (TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE
4072 || TREE_CODE (TREE_TYPE (expr)) == METHOD_TYPE
4073 || TREE_TYPE (expr) == unknown_type_node)
4075 if (INDIRECT_REF_P (expr))
4076 error ("second operand of %<offsetof%> is neither a single "
4077 "identifier nor a sequence of member accesses and "
4078 "array references");
4079 else
4081 if (TREE_CODE (expr) == COMPONENT_REF
4082 || TREE_CODE (expr) == COMPOUND_EXPR)
4083 expr = TREE_OPERAND (expr, 1);
4084 error ("cannot apply %<offsetof%> to member function %qD", expr);
4086 return error_mark_node;
4088 if (REFERENCE_REF_P (expr))
4089 expr = TREE_OPERAND (expr, 0);
4090 if (!complete_type_or_else (TREE_TYPE (TREE_TYPE (object_ptr)), object_ptr))
4091 return error_mark_node;
4092 if (warn_invalid_offsetof
4093 && CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (object_ptr)))
4094 && CLASSTYPE_NON_STD_LAYOUT (TREE_TYPE (TREE_TYPE (object_ptr)))
4095 && cp_unevaluated_operand == 0)
4096 pedwarn (loc, OPT_Winvalid_offsetof,
4097 "offsetof within non-standard-layout type %qT is undefined",
4098 TREE_TYPE (TREE_TYPE (object_ptr)));
4099 return fold_offsetof (expr);
4102 /* Replace the AGGR_INIT_EXPR at *TP with an equivalent CALL_EXPR. This
4103 function is broken out from the above for the benefit of the tree-ssa
4104 project. */
4106 void
4107 simplify_aggr_init_expr (tree *tp)
4109 tree aggr_init_expr = *tp;
4111 /* Form an appropriate CALL_EXPR. */
4112 tree fn = AGGR_INIT_EXPR_FN (aggr_init_expr);
4113 tree slot = AGGR_INIT_EXPR_SLOT (aggr_init_expr);
4114 tree type = TREE_TYPE (slot);
4116 tree call_expr;
4117 enum style_t { ctor, arg, pcc } style;
4119 if (AGGR_INIT_VIA_CTOR_P (aggr_init_expr))
4120 style = ctor;
4121 #ifdef PCC_STATIC_STRUCT_RETURN
4122 else if (1)
4123 style = pcc;
4124 #endif
4125 else
4127 gcc_assert (TREE_ADDRESSABLE (type));
4128 style = arg;
4131 call_expr = build_call_array_loc (input_location,
4132 TREE_TYPE (TREE_TYPE (TREE_TYPE (fn))),
4134 aggr_init_expr_nargs (aggr_init_expr),
4135 AGGR_INIT_EXPR_ARGP (aggr_init_expr));
4136 TREE_NOTHROW (call_expr) = TREE_NOTHROW (aggr_init_expr);
4137 CALL_FROM_THUNK_P (call_expr) = AGGR_INIT_FROM_THUNK_P (aggr_init_expr);
4138 CALL_EXPR_OPERATOR_SYNTAX (call_expr)
4139 = CALL_EXPR_OPERATOR_SYNTAX (aggr_init_expr);
4140 CALL_EXPR_ORDERED_ARGS (call_expr) = CALL_EXPR_ORDERED_ARGS (aggr_init_expr);
4141 CALL_EXPR_REVERSE_ARGS (call_expr) = CALL_EXPR_REVERSE_ARGS (aggr_init_expr);
4142 /* Preserve CILK_SPAWN flag. */
4143 EXPR_CILK_SPAWN (call_expr) = EXPR_CILK_SPAWN (aggr_init_expr);
4145 if (style == ctor)
4147 /* Replace the first argument to the ctor with the address of the
4148 slot. */
4149 cxx_mark_addressable (slot);
4150 CALL_EXPR_ARG (call_expr, 0) =
4151 build1 (ADDR_EXPR, build_pointer_type (type), slot);
4153 else if (style == arg)
4155 /* Just mark it addressable here, and leave the rest to
4156 expand_call{,_inline}. */
4157 cxx_mark_addressable (slot);
4158 CALL_EXPR_RETURN_SLOT_OPT (call_expr) = true;
4159 call_expr = build2 (INIT_EXPR, TREE_TYPE (call_expr), slot, call_expr);
4161 else if (style == pcc)
4163 /* If we're using the non-reentrant PCC calling convention, then we
4164 need to copy the returned value out of the static buffer into the
4165 SLOT. */
4166 push_deferring_access_checks (dk_no_check);
4167 call_expr = build_aggr_init (slot, call_expr,
4168 DIRECT_BIND | LOOKUP_ONLYCONVERTING,
4169 tf_warning_or_error);
4170 pop_deferring_access_checks ();
4171 call_expr = build2 (COMPOUND_EXPR, TREE_TYPE (slot), call_expr, slot);
4174 if (AGGR_INIT_ZERO_FIRST (aggr_init_expr))
4176 tree init = build_zero_init (type, NULL_TREE,
4177 /*static_storage_p=*/false);
4178 init = build2 (INIT_EXPR, void_type_node, slot, init);
4179 call_expr = build2 (COMPOUND_EXPR, TREE_TYPE (call_expr),
4180 init, call_expr);
4183 *tp = call_expr;
4186 /* Emit all thunks to FN that should be emitted when FN is emitted. */
4188 void
4189 emit_associated_thunks (tree fn)
4191 /* When we use vcall offsets, we emit thunks with the virtual
4192 functions to which they thunk. The whole point of vcall offsets
4193 is so that you can know statically the entire set of thunks that
4194 will ever be needed for a given virtual function, thereby
4195 enabling you to output all the thunks with the function itself. */
4196 if (DECL_VIRTUAL_P (fn)
4197 /* Do not emit thunks for extern template instantiations. */
4198 && ! DECL_REALLY_EXTERN (fn))
4200 tree thunk;
4202 for (thunk = DECL_THUNKS (fn); thunk; thunk = DECL_CHAIN (thunk))
4204 if (!THUNK_ALIAS (thunk))
4206 use_thunk (thunk, /*emit_p=*/1);
4207 if (DECL_RESULT_THUNK_P (thunk))
4209 tree probe;
4211 for (probe = DECL_THUNKS (thunk);
4212 probe; probe = DECL_CHAIN (probe))
4213 use_thunk (probe, /*emit_p=*/1);
4216 else
4217 gcc_assert (!DECL_THUNKS (thunk));
4222 /* Generate RTL for FN. */
4224 bool
4225 expand_or_defer_fn_1 (tree fn)
4227 /* When the parser calls us after finishing the body of a template
4228 function, we don't really want to expand the body. */
4229 if (processing_template_decl)
4231 /* Normally, collection only occurs in rest_of_compilation. So,
4232 if we don't collect here, we never collect junk generated
4233 during the processing of templates until we hit a
4234 non-template function. It's not safe to do this inside a
4235 nested class, though, as the parser may have local state that
4236 is not a GC root. */
4237 if (!function_depth)
4238 ggc_collect ();
4239 return false;
4242 gcc_assert (DECL_SAVED_TREE (fn));
4244 /* We make a decision about linkage for these functions at the end
4245 of the compilation. Until that point, we do not want the back
4246 end to output them -- but we do want it to see the bodies of
4247 these functions so that it can inline them as appropriate. */
4248 if (DECL_DECLARED_INLINE_P (fn) || DECL_IMPLICIT_INSTANTIATION (fn))
4250 if (DECL_INTERFACE_KNOWN (fn))
4251 /* We've already made a decision as to how this function will
4252 be handled. */;
4253 else if (!at_eof)
4254 tentative_decl_linkage (fn);
4255 else
4256 import_export_decl (fn);
4258 /* If the user wants us to keep all inline functions, then mark
4259 this function as needed so that finish_file will make sure to
4260 output it later. Similarly, all dllexport'd functions must
4261 be emitted; there may be callers in other DLLs. */
4262 if (DECL_DECLARED_INLINE_P (fn)
4263 && !DECL_REALLY_EXTERN (fn)
4264 && (flag_keep_inline_functions
4265 || (flag_keep_inline_dllexport
4266 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (fn)))))
4268 mark_needed (fn);
4269 DECL_EXTERNAL (fn) = 0;
4273 /* If this is a constructor or destructor body, we have to clone
4274 it. */
4275 if (maybe_clone_body (fn))
4277 /* We don't want to process FN again, so pretend we've written
4278 it out, even though we haven't. */
4279 TREE_ASM_WRITTEN (fn) = 1;
4280 /* If this is a constexpr function, keep DECL_SAVED_TREE. */
4281 if (!DECL_DECLARED_CONSTEXPR_P (fn))
4282 DECL_SAVED_TREE (fn) = NULL_TREE;
4283 return false;
4286 /* There's no reason to do any of the work here if we're only doing
4287 semantic analysis; this code just generates RTL. */
4288 if (flag_syntax_only)
4289 return false;
4291 return true;
4294 void
4295 expand_or_defer_fn (tree fn)
4297 if (expand_or_defer_fn_1 (fn))
4299 function_depth++;
4301 /* Expand or defer, at the whim of the compilation unit manager. */
4302 cgraph_node::finalize_function (fn, function_depth > 1);
4303 emit_associated_thunks (fn);
4305 function_depth--;
4309 struct nrv_data
4311 nrv_data () : visited (37) {}
4313 tree var;
4314 tree result;
4315 hash_table<nofree_ptr_hash <tree_node> > visited;
4318 /* Helper function for walk_tree, used by finalize_nrv below. */
4320 static tree
4321 finalize_nrv_r (tree* tp, int* walk_subtrees, void* data)
4323 struct nrv_data *dp = (struct nrv_data *)data;
4324 tree_node **slot;
4326 /* No need to walk into types. There wouldn't be any need to walk into
4327 non-statements, except that we have to consider STMT_EXPRs. */
4328 if (TYPE_P (*tp))
4329 *walk_subtrees = 0;
4330 /* Change all returns to just refer to the RESULT_DECL; this is a nop,
4331 but differs from using NULL_TREE in that it indicates that we care
4332 about the value of the RESULT_DECL. */
4333 else if (TREE_CODE (*tp) == RETURN_EXPR)
4334 TREE_OPERAND (*tp, 0) = dp->result;
4335 /* Change all cleanups for the NRV to only run when an exception is
4336 thrown. */
4337 else if (TREE_CODE (*tp) == CLEANUP_STMT
4338 && CLEANUP_DECL (*tp) == dp->var)
4339 CLEANUP_EH_ONLY (*tp) = 1;
4340 /* Replace the DECL_EXPR for the NRV with an initialization of the
4341 RESULT_DECL, if needed. */
4342 else if (TREE_CODE (*tp) == DECL_EXPR
4343 && DECL_EXPR_DECL (*tp) == dp->var)
4345 tree init;
4346 if (DECL_INITIAL (dp->var)
4347 && DECL_INITIAL (dp->var) != error_mark_node)
4348 init = build2 (INIT_EXPR, void_type_node, dp->result,
4349 DECL_INITIAL (dp->var));
4350 else
4351 init = build_empty_stmt (EXPR_LOCATION (*tp));
4352 DECL_INITIAL (dp->var) = NULL_TREE;
4353 SET_EXPR_LOCATION (init, EXPR_LOCATION (*tp));
4354 *tp = init;
4356 /* And replace all uses of the NRV with the RESULT_DECL. */
4357 else if (*tp == dp->var)
4358 *tp = dp->result;
4360 /* Avoid walking into the same tree more than once. Unfortunately, we
4361 can't just use walk_tree_without duplicates because it would only call
4362 us for the first occurrence of dp->var in the function body. */
4363 slot = dp->visited.find_slot (*tp, INSERT);
4364 if (*slot)
4365 *walk_subtrees = 0;
4366 else
4367 *slot = *tp;
4369 /* Keep iterating. */
4370 return NULL_TREE;
4373 /* Called from finish_function to implement the named return value
4374 optimization by overriding all the RETURN_EXPRs and pertinent
4375 CLEANUP_STMTs and replacing all occurrences of VAR with RESULT, the
4376 RESULT_DECL for the function. */
4378 void
4379 finalize_nrv (tree *tp, tree var, tree result)
4381 struct nrv_data data;
4383 /* Copy name from VAR to RESULT. */
4384 DECL_NAME (result) = DECL_NAME (var);
4385 /* Don't forget that we take its address. */
4386 TREE_ADDRESSABLE (result) = TREE_ADDRESSABLE (var);
4387 /* Finally set DECL_VALUE_EXPR to avoid assigning
4388 a stack slot at -O0 for the original var and debug info
4389 uses RESULT location for VAR. */
4390 SET_DECL_VALUE_EXPR (var, result);
4391 DECL_HAS_VALUE_EXPR_P (var) = 1;
4393 data.var = var;
4394 data.result = result;
4395 cp_walk_tree (tp, finalize_nrv_r, &data, 0);
4398 /* Create CP_OMP_CLAUSE_INFO for clause C. Returns true if it is invalid. */
4400 bool
4401 cxx_omp_create_clause_info (tree c, tree type, bool need_default_ctor,
4402 bool need_copy_ctor, bool need_copy_assignment,
4403 bool need_dtor)
4405 int save_errorcount = errorcount;
4406 tree info, t;
4408 /* Always allocate 3 elements for simplicity. These are the
4409 function decls for the ctor, dtor, and assignment op.
4410 This layout is known to the three lang hooks,
4411 cxx_omp_clause_default_init, cxx_omp_clause_copy_init,
4412 and cxx_omp_clause_assign_op. */
4413 info = make_tree_vec (3);
4414 CP_OMP_CLAUSE_INFO (c) = info;
4416 if (need_default_ctor || need_copy_ctor)
4418 if (need_default_ctor)
4419 t = get_default_ctor (type);
4420 else
4421 t = get_copy_ctor (type, tf_warning_or_error);
4423 if (t && !trivial_fn_p (t))
4424 TREE_VEC_ELT (info, 0) = t;
4427 if (need_dtor && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
4428 TREE_VEC_ELT (info, 1) = get_dtor (type, tf_warning_or_error);
4430 if (need_copy_assignment)
4432 t = get_copy_assign (type);
4434 if (t && !trivial_fn_p (t))
4435 TREE_VEC_ELT (info, 2) = t;
4438 return errorcount != save_errorcount;
4441 /* If DECL is DECL_OMP_PRIVATIZED_MEMBER, return corresponding
4442 FIELD_DECL, otherwise return DECL itself. */
4444 static tree
4445 omp_clause_decl_field (tree decl)
4447 if (VAR_P (decl)
4448 && DECL_HAS_VALUE_EXPR_P (decl)
4449 && DECL_ARTIFICIAL (decl)
4450 && DECL_LANG_SPECIFIC (decl)
4451 && DECL_OMP_PRIVATIZED_MEMBER (decl))
4453 tree f = DECL_VALUE_EXPR (decl);
4454 if (TREE_CODE (f) == INDIRECT_REF)
4455 f = TREE_OPERAND (f, 0);
4456 if (TREE_CODE (f) == COMPONENT_REF)
4458 f = TREE_OPERAND (f, 1);
4459 gcc_assert (TREE_CODE (f) == FIELD_DECL);
4460 return f;
4463 return NULL_TREE;
4466 /* Adjust DECL if needed for printing using %qE. */
4468 static tree
4469 omp_clause_printable_decl (tree decl)
4471 tree t = omp_clause_decl_field (decl);
4472 if (t)
4473 return t;
4474 return decl;
4477 /* For a FIELD_DECL F and corresponding DECL_OMP_PRIVATIZED_MEMBER
4478 VAR_DECL T that doesn't need a DECL_EXPR added, record it for
4479 privatization. */
4481 static void
4482 omp_note_field_privatization (tree f, tree t)
4484 if (!omp_private_member_map)
4485 omp_private_member_map = new hash_map<tree, tree>;
4486 tree &v = omp_private_member_map->get_or_insert (f);
4487 if (v == NULL_TREE)
4489 v = t;
4490 omp_private_member_vec.safe_push (f);
4491 /* Signal that we don't want to create DECL_EXPR for this dummy var. */
4492 omp_private_member_vec.safe_push (integer_zero_node);
4496 /* Privatize FIELD_DECL T, return corresponding DECL_OMP_PRIVATIZED_MEMBER
4497 dummy VAR_DECL. */
4499 tree
4500 omp_privatize_field (tree t, bool shared)
4502 tree m = finish_non_static_data_member (t, NULL_TREE, NULL_TREE);
4503 if (m == error_mark_node)
4504 return error_mark_node;
4505 if (!omp_private_member_map && !shared)
4506 omp_private_member_map = new hash_map<tree, tree>;
4507 if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE)
4509 gcc_assert (TREE_CODE (m) == INDIRECT_REF);
4510 m = TREE_OPERAND (m, 0);
4512 tree vb = NULL_TREE;
4513 tree &v = shared ? vb : omp_private_member_map->get_or_insert (t);
4514 if (v == NULL_TREE)
4516 v = create_temporary_var (TREE_TYPE (m));
4517 retrofit_lang_decl (v);
4518 DECL_OMP_PRIVATIZED_MEMBER (v) = 1;
4519 SET_DECL_VALUE_EXPR (v, m);
4520 DECL_HAS_VALUE_EXPR_P (v) = 1;
4521 if (!shared)
4522 omp_private_member_vec.safe_push (t);
4524 return v;
4527 /* Helper function for handle_omp_array_sections. Called recursively
4528 to handle multiple array-section-subscripts. C is the clause,
4529 T current expression (initially OMP_CLAUSE_DECL), which is either
4530 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
4531 expression if specified, TREE_VALUE length expression if specified,
4532 TREE_CHAIN is what it has been specified after, or some decl.
4533 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
4534 set to true if any of the array-section-subscript could have length
4535 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
4536 first array-section-subscript which is known not to have length
4537 of one. Given say:
4538 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
4539 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
4540 all are or may have length of 1, array-section-subscript [:2] is the
4541 first one known not to have length 1. For array-section-subscript
4542 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
4543 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
4544 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
4545 case though, as some lengths could be zero. */
4547 static tree
4548 handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
4549 bool &maybe_zero_len, unsigned int &first_non_one,
4550 enum c_omp_region_type ort)
4552 tree ret, low_bound, length, type;
4553 if (TREE_CODE (t) != TREE_LIST)
4555 if (error_operand_p (t))
4556 return error_mark_node;
4557 if (REFERENCE_REF_P (t)
4558 && TREE_CODE (TREE_OPERAND (t, 0)) == COMPONENT_REF)
4559 t = TREE_OPERAND (t, 0);
4560 ret = t;
4561 if (TREE_CODE (t) == COMPONENT_REF
4562 && ort == C_ORT_OMP
4563 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
4564 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO
4565 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FROM)
4566 && !type_dependent_expression_p (t))
4568 if (TREE_CODE (TREE_OPERAND (t, 1)) == FIELD_DECL
4569 && DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
4571 error_at (OMP_CLAUSE_LOCATION (c),
4572 "bit-field %qE in %qs clause",
4573 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4574 return error_mark_node;
4576 while (TREE_CODE (t) == COMPONENT_REF)
4578 if (TREE_TYPE (TREE_OPERAND (t, 0))
4579 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == UNION_TYPE)
4581 error_at (OMP_CLAUSE_LOCATION (c),
4582 "%qE is a member of a union", t);
4583 return error_mark_node;
4585 t = TREE_OPERAND (t, 0);
4587 if (REFERENCE_REF_P (t))
4588 t = TREE_OPERAND (t, 0);
4590 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
4592 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
4593 return NULL_TREE;
4594 if (DECL_P (t))
4595 error_at (OMP_CLAUSE_LOCATION (c),
4596 "%qD is not a variable in %qs clause", t,
4597 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4598 else
4599 error_at (OMP_CLAUSE_LOCATION (c),
4600 "%qE is not a variable in %qs clause", t,
4601 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4602 return error_mark_node;
4604 else if (TREE_CODE (t) == PARM_DECL
4605 && DECL_ARTIFICIAL (t)
4606 && DECL_NAME (t) == this_identifier)
4608 error_at (OMP_CLAUSE_LOCATION (c),
4609 "%<this%> allowed in OpenMP only in %<declare simd%>"
4610 " clauses");
4611 return error_mark_node;
4613 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
4614 && VAR_P (t) && CP_DECL_THREAD_LOCAL_P (t))
4616 error_at (OMP_CLAUSE_LOCATION (c),
4617 "%qD is threadprivate variable in %qs clause", t,
4618 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4619 return error_mark_node;
4621 if (type_dependent_expression_p (ret))
4622 return NULL_TREE;
4623 ret = convert_from_reference (ret);
4624 return ret;
4627 if (ort == C_ORT_OMP
4628 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
4629 && TREE_CODE (TREE_CHAIN (t)) == FIELD_DECL)
4630 TREE_CHAIN (t) = omp_privatize_field (TREE_CHAIN (t), false);
4631 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
4632 maybe_zero_len, first_non_one, ort);
4633 if (ret == error_mark_node || ret == NULL_TREE)
4634 return ret;
4636 type = TREE_TYPE (ret);
4637 low_bound = TREE_PURPOSE (t);
4638 length = TREE_VALUE (t);
4639 if ((low_bound && type_dependent_expression_p (low_bound))
4640 || (length && type_dependent_expression_p (length)))
4641 return NULL_TREE;
4643 if (low_bound == error_mark_node || length == error_mark_node)
4644 return error_mark_node;
4646 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
4648 error_at (OMP_CLAUSE_LOCATION (c),
4649 "low bound %qE of array section does not have integral type",
4650 low_bound);
4651 return error_mark_node;
4653 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
4655 error_at (OMP_CLAUSE_LOCATION (c),
4656 "length %qE of array section does not have integral type",
4657 length);
4658 return error_mark_node;
4660 if (low_bound)
4661 low_bound = mark_rvalue_use (low_bound);
4662 if (length)
4663 length = mark_rvalue_use (length);
4664 /* We need to reduce to real constant-values for checks below. */
4665 if (length)
4666 length = fold_simple (length);
4667 if (low_bound)
4668 low_bound = fold_simple (low_bound);
4669 if (low_bound
4670 && TREE_CODE (low_bound) == INTEGER_CST
4671 && TYPE_PRECISION (TREE_TYPE (low_bound))
4672 > TYPE_PRECISION (sizetype))
4673 low_bound = fold_convert (sizetype, low_bound);
4674 if (length
4675 && TREE_CODE (length) == INTEGER_CST
4676 && TYPE_PRECISION (TREE_TYPE (length))
4677 > TYPE_PRECISION (sizetype))
4678 length = fold_convert (sizetype, length);
4679 if (low_bound == NULL_TREE)
4680 low_bound = integer_zero_node;
4682 if (length != NULL_TREE)
4684 if (!integer_nonzerop (length))
4686 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
4687 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
4689 if (integer_zerop (length))
4691 error_at (OMP_CLAUSE_LOCATION (c),
4692 "zero length array section in %qs clause",
4693 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4694 return error_mark_node;
4697 else
4698 maybe_zero_len = true;
4700 if (first_non_one == types.length ()
4701 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
4702 first_non_one++;
4704 if (TREE_CODE (type) == ARRAY_TYPE)
4706 if (length == NULL_TREE
4707 && (TYPE_DOMAIN (type) == NULL_TREE
4708 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
4710 error_at (OMP_CLAUSE_LOCATION (c),
4711 "for unknown bound array type length expression must "
4712 "be specified");
4713 return error_mark_node;
4715 if (TREE_CODE (low_bound) == INTEGER_CST
4716 && tree_int_cst_sgn (low_bound) == -1)
4718 error_at (OMP_CLAUSE_LOCATION (c),
4719 "negative low bound in array section in %qs clause",
4720 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4721 return error_mark_node;
4723 if (length != NULL_TREE
4724 && TREE_CODE (length) == INTEGER_CST
4725 && tree_int_cst_sgn (length) == -1)
4727 error_at (OMP_CLAUSE_LOCATION (c),
4728 "negative length in array section in %qs clause",
4729 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4730 return error_mark_node;
4732 if (TYPE_DOMAIN (type)
4733 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
4734 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
4735 == INTEGER_CST)
4737 tree size
4738 = fold_convert (sizetype, TYPE_MAX_VALUE (TYPE_DOMAIN (type)));
4739 size = size_binop (PLUS_EXPR, size, size_one_node);
4740 if (TREE_CODE (low_bound) == INTEGER_CST)
4742 if (tree_int_cst_lt (size, low_bound))
4744 error_at (OMP_CLAUSE_LOCATION (c),
4745 "low bound %qE above array section size "
4746 "in %qs clause", low_bound,
4747 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4748 return error_mark_node;
4750 if (tree_int_cst_equal (size, low_bound))
4752 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
4753 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
4755 error_at (OMP_CLAUSE_LOCATION (c),
4756 "zero length array section in %qs clause",
4757 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4758 return error_mark_node;
4760 maybe_zero_len = true;
4762 else if (length == NULL_TREE
4763 && first_non_one == types.length ()
4764 && tree_int_cst_equal
4765 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
4766 low_bound))
4767 first_non_one++;
4769 else if (length == NULL_TREE)
4771 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
4772 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION)
4773 maybe_zero_len = true;
4774 if (first_non_one == types.length ())
4775 first_non_one++;
4777 if (length && TREE_CODE (length) == INTEGER_CST)
4779 if (tree_int_cst_lt (size, length))
4781 error_at (OMP_CLAUSE_LOCATION (c),
4782 "length %qE above array section size "
4783 "in %qs clause", length,
4784 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4785 return error_mark_node;
4787 if (TREE_CODE (low_bound) == INTEGER_CST)
4789 tree lbpluslen
4790 = size_binop (PLUS_EXPR,
4791 fold_convert (sizetype, low_bound),
4792 fold_convert (sizetype, length));
4793 if (TREE_CODE (lbpluslen) == INTEGER_CST
4794 && tree_int_cst_lt (size, lbpluslen))
4796 error_at (OMP_CLAUSE_LOCATION (c),
4797 "high bound %qE above array section size "
4798 "in %qs clause", lbpluslen,
4799 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4800 return error_mark_node;
4805 else if (length == NULL_TREE)
4807 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
4808 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION)
4809 maybe_zero_len = true;
4810 if (first_non_one == types.length ())
4811 first_non_one++;
4814 /* For [lb:] we will need to evaluate lb more than once. */
4815 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
4817 tree lb = cp_save_expr (low_bound);
4818 if (lb != low_bound)
4820 TREE_PURPOSE (t) = lb;
4821 low_bound = lb;
4825 else if (TREE_CODE (type) == POINTER_TYPE)
4827 if (length == NULL_TREE)
4829 error_at (OMP_CLAUSE_LOCATION (c),
4830 "for pointer type length expression must be specified");
4831 return error_mark_node;
4833 if (length != NULL_TREE
4834 && TREE_CODE (length) == INTEGER_CST
4835 && tree_int_cst_sgn (length) == -1)
4837 error_at (OMP_CLAUSE_LOCATION (c),
4838 "negative length in array section in %qs clause",
4839 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4840 return error_mark_node;
4842 /* If there is a pointer type anywhere but in the very first
4843 array-section-subscript, the array section can't be contiguous. */
4844 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
4845 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
4847 error_at (OMP_CLAUSE_LOCATION (c),
4848 "array section is not contiguous in %qs clause",
4849 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4850 return error_mark_node;
4853 else
4855 error_at (OMP_CLAUSE_LOCATION (c),
4856 "%qE does not have pointer or array type", ret);
4857 return error_mark_node;
4859 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
4860 types.safe_push (TREE_TYPE (ret));
4861 /* We will need to evaluate lb more than once. */
4862 tree lb = cp_save_expr (low_bound);
4863 if (lb != low_bound)
4865 TREE_PURPOSE (t) = lb;
4866 low_bound = lb;
4868 ret = grok_array_decl (OMP_CLAUSE_LOCATION (c), ret, low_bound, false);
4869 return ret;
4872 /* Handle array sections for clause C. */
4874 static bool
4875 handle_omp_array_sections (tree c, enum c_omp_region_type ort)
4877 bool maybe_zero_len = false;
4878 unsigned int first_non_one = 0;
4879 auto_vec<tree, 10> types;
4880 tree first = handle_omp_array_sections_1 (c, OMP_CLAUSE_DECL (c), types,
4881 maybe_zero_len, first_non_one,
4882 ort);
4883 if (first == error_mark_node)
4884 return true;
4885 if (first == NULL_TREE)
4886 return false;
4887 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
4889 tree t = OMP_CLAUSE_DECL (c);
4890 tree tem = NULL_TREE;
4891 if (processing_template_decl)
4892 return false;
4893 /* Need to evaluate side effects in the length expressions
4894 if any. */
4895 while (TREE_CODE (t) == TREE_LIST)
4897 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
4899 if (tem == NULL_TREE)
4900 tem = TREE_VALUE (t);
4901 else
4902 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
4903 TREE_VALUE (t), tem);
4905 t = TREE_CHAIN (t);
4907 if (tem)
4908 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
4909 OMP_CLAUSE_DECL (c) = first;
4911 else
4913 unsigned int num = types.length (), i;
4914 tree t, side_effects = NULL_TREE, size = NULL_TREE;
4915 tree condition = NULL_TREE;
4917 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
4918 maybe_zero_len = true;
4919 if (processing_template_decl && maybe_zero_len)
4920 return false;
4922 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
4923 t = TREE_CHAIN (t))
4925 tree low_bound = TREE_PURPOSE (t);
4926 tree length = TREE_VALUE (t);
4928 i--;
4929 if (low_bound
4930 && TREE_CODE (low_bound) == INTEGER_CST
4931 && TYPE_PRECISION (TREE_TYPE (low_bound))
4932 > TYPE_PRECISION (sizetype))
4933 low_bound = fold_convert (sizetype, low_bound);
4934 if (length
4935 && TREE_CODE (length) == INTEGER_CST
4936 && TYPE_PRECISION (TREE_TYPE (length))
4937 > TYPE_PRECISION (sizetype))
4938 length = fold_convert (sizetype, length);
4939 if (low_bound == NULL_TREE)
4940 low_bound = integer_zero_node;
4941 if (!maybe_zero_len && i > first_non_one)
4943 if (integer_nonzerop (low_bound))
4944 goto do_warn_noncontiguous;
4945 if (length != NULL_TREE
4946 && TREE_CODE (length) == INTEGER_CST
4947 && TYPE_DOMAIN (types[i])
4948 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
4949 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
4950 == INTEGER_CST)
4952 tree size;
4953 size = size_binop (PLUS_EXPR,
4954 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
4955 size_one_node);
4956 if (!tree_int_cst_equal (length, size))
4958 do_warn_noncontiguous:
4959 error_at (OMP_CLAUSE_LOCATION (c),
4960 "array section is not contiguous in %qs "
4961 "clause",
4962 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4963 return true;
4966 if (!processing_template_decl
4967 && length != NULL_TREE
4968 && TREE_SIDE_EFFECTS (length))
4970 if (side_effects == NULL_TREE)
4971 side_effects = length;
4972 else
4973 side_effects = build2 (COMPOUND_EXPR,
4974 TREE_TYPE (side_effects),
4975 length, side_effects);
4978 else if (processing_template_decl)
4979 continue;
4980 else
4982 tree l;
4984 if (i > first_non_one
4985 && ((length && integer_nonzerop (length))
4986 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION))
4987 continue;
4988 if (length)
4989 l = fold_convert (sizetype, length);
4990 else
4992 l = size_binop (PLUS_EXPR,
4993 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
4994 size_one_node);
4995 l = size_binop (MINUS_EXPR, l,
4996 fold_convert (sizetype, low_bound));
4998 if (i > first_non_one)
5000 l = fold_build2 (NE_EXPR, boolean_type_node, l,
5001 size_zero_node);
5002 if (condition == NULL_TREE)
5003 condition = l;
5004 else
5005 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
5006 l, condition);
5008 else if (size == NULL_TREE)
5010 size = size_in_bytes (TREE_TYPE (types[i]));
5011 tree eltype = TREE_TYPE (types[num - 1]);
5012 while (TREE_CODE (eltype) == ARRAY_TYPE)
5013 eltype = TREE_TYPE (eltype);
5014 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
5015 size = size_binop (EXACT_DIV_EXPR, size,
5016 size_in_bytes (eltype));
5017 size = size_binop (MULT_EXPR, size, l);
5018 if (condition)
5019 size = fold_build3 (COND_EXPR, sizetype, condition,
5020 size, size_zero_node);
5022 else
5023 size = size_binop (MULT_EXPR, size, l);
5026 if (!processing_template_decl)
5028 if (side_effects)
5029 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
5030 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
5032 size = size_binop (MINUS_EXPR, size, size_one_node);
5033 tree index_type = build_index_type (size);
5034 tree eltype = TREE_TYPE (first);
5035 while (TREE_CODE (eltype) == ARRAY_TYPE)
5036 eltype = TREE_TYPE (eltype);
5037 tree type = build_array_type (eltype, index_type);
5038 tree ptype = build_pointer_type (eltype);
5039 if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE
5040 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (t))))
5041 t = convert_from_reference (t);
5042 else if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
5043 t = build_fold_addr_expr (t);
5044 tree t2 = build_fold_addr_expr (first);
5045 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
5046 ptrdiff_type_node, t2);
5047 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
5048 ptrdiff_type_node, t2,
5049 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
5050 ptrdiff_type_node, t));
5051 if (tree_fits_shwi_p (t2))
5052 t = build2 (MEM_REF, type, t,
5053 build_int_cst (ptype, tree_to_shwi (t2)));
5054 else
5056 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
5057 sizetype, t2);
5058 t = build2_loc (OMP_CLAUSE_LOCATION (c), POINTER_PLUS_EXPR,
5059 TREE_TYPE (t), t, t2);
5060 t = build2 (MEM_REF, type, t, build_int_cst (ptype, 0));
5062 OMP_CLAUSE_DECL (c) = t;
5063 return false;
5065 OMP_CLAUSE_DECL (c) = first;
5066 OMP_CLAUSE_SIZE (c) = size;
5067 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
5068 || (TREE_CODE (t) == COMPONENT_REF
5069 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE))
5070 return false;
5071 if (ort == C_ORT_OMP || ort == C_ORT_ACC)
5072 switch (OMP_CLAUSE_MAP_KIND (c))
5074 case GOMP_MAP_ALLOC:
5075 case GOMP_MAP_TO:
5076 case GOMP_MAP_FROM:
5077 case GOMP_MAP_TOFROM:
5078 case GOMP_MAP_ALWAYS_TO:
5079 case GOMP_MAP_ALWAYS_FROM:
5080 case GOMP_MAP_ALWAYS_TOFROM:
5081 case GOMP_MAP_RELEASE:
5082 case GOMP_MAP_DELETE:
5083 case GOMP_MAP_FORCE_TO:
5084 case GOMP_MAP_FORCE_FROM:
5085 case GOMP_MAP_FORCE_TOFROM:
5086 case GOMP_MAP_FORCE_PRESENT:
5087 OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (c) = 1;
5088 break;
5089 default:
5090 break;
5092 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
5093 OMP_CLAUSE_MAP);
5094 if ((ort & C_ORT_OMP_DECLARE_SIMD) != C_ORT_OMP && ort != C_ORT_ACC)
5095 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_POINTER);
5096 else if (TREE_CODE (t) == COMPONENT_REF)
5097 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ALWAYS_POINTER);
5098 else if (REFERENCE_REF_P (t)
5099 && TREE_CODE (TREE_OPERAND (t, 0)) == COMPONENT_REF)
5101 t = TREE_OPERAND (t, 0);
5102 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ALWAYS_POINTER);
5104 else
5105 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_FIRSTPRIVATE_POINTER);
5106 if (OMP_CLAUSE_MAP_KIND (c2) != GOMP_MAP_FIRSTPRIVATE_POINTER
5107 && !cxx_mark_addressable (t))
5108 return false;
5109 OMP_CLAUSE_DECL (c2) = t;
5110 t = build_fold_addr_expr (first);
5111 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
5112 ptrdiff_type_node, t);
5113 tree ptr = OMP_CLAUSE_DECL (c2);
5114 ptr = convert_from_reference (ptr);
5115 if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
5116 ptr = build_fold_addr_expr (ptr);
5117 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
5118 ptrdiff_type_node, t,
5119 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
5120 ptrdiff_type_node, ptr));
5121 OMP_CLAUSE_SIZE (c2) = t;
5122 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
5123 OMP_CLAUSE_CHAIN (c) = c2;
5124 ptr = OMP_CLAUSE_DECL (c2);
5125 if (OMP_CLAUSE_MAP_KIND (c2) != GOMP_MAP_FIRSTPRIVATE_POINTER
5126 && TREE_CODE (TREE_TYPE (ptr)) == REFERENCE_TYPE
5127 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (ptr))))
5129 tree c3 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
5130 OMP_CLAUSE_MAP);
5131 OMP_CLAUSE_SET_MAP_KIND (c3, OMP_CLAUSE_MAP_KIND (c2));
5132 OMP_CLAUSE_DECL (c3) = ptr;
5133 if (OMP_CLAUSE_MAP_KIND (c2) == GOMP_MAP_ALWAYS_POINTER)
5134 OMP_CLAUSE_DECL (c2) = build_simple_mem_ref (ptr);
5135 else
5136 OMP_CLAUSE_DECL (c2) = convert_from_reference (ptr);
5137 OMP_CLAUSE_SIZE (c3) = size_zero_node;
5138 OMP_CLAUSE_CHAIN (c3) = OMP_CLAUSE_CHAIN (c2);
5139 OMP_CLAUSE_CHAIN (c2) = c3;
5143 return false;
5146 /* Return identifier to look up for omp declare reduction. */
5148 tree
5149 omp_reduction_id (enum tree_code reduction_code, tree reduction_id, tree type)
5151 const char *p = NULL;
5152 const char *m = NULL;
5153 switch (reduction_code)
5155 case PLUS_EXPR:
5156 case MULT_EXPR:
5157 case MINUS_EXPR:
5158 case BIT_AND_EXPR:
5159 case BIT_XOR_EXPR:
5160 case BIT_IOR_EXPR:
5161 case TRUTH_ANDIF_EXPR:
5162 case TRUTH_ORIF_EXPR:
5163 reduction_id = cp_operator_id (reduction_code);
5164 break;
5165 case MIN_EXPR:
5166 p = "min";
5167 break;
5168 case MAX_EXPR:
5169 p = "max";
5170 break;
5171 default:
5172 break;
5175 if (p == NULL)
5177 if (TREE_CODE (reduction_id) != IDENTIFIER_NODE)
5178 return error_mark_node;
5179 p = IDENTIFIER_POINTER (reduction_id);
5182 if (type != NULL_TREE)
5183 m = mangle_type_string (TYPE_MAIN_VARIANT (type));
5185 const char prefix[] = "omp declare reduction ";
5186 size_t lenp = sizeof (prefix);
5187 if (strncmp (p, prefix, lenp - 1) == 0)
5188 lenp = 1;
5189 size_t len = strlen (p);
5190 size_t lenm = m ? strlen (m) + 1 : 0;
5191 char *name = XALLOCAVEC (char, lenp + len + lenm);
5192 if (lenp > 1)
5193 memcpy (name, prefix, lenp - 1);
5194 memcpy (name + lenp - 1, p, len + 1);
5195 if (m)
5197 name[lenp + len - 1] = '~';
5198 memcpy (name + lenp + len, m, lenm);
5200 return get_identifier (name);
5203 /* Lookup OpenMP UDR ID for TYPE, return the corresponding artificial
5204 FUNCTION_DECL or NULL_TREE if not found. */
5206 static tree
5207 omp_reduction_lookup (location_t loc, tree id, tree type, tree *baselinkp,
5208 vec<tree> *ambiguousp)
5210 tree orig_id = id;
5211 tree baselink = NULL_TREE;
5212 if (identifier_p (id))
5214 cp_id_kind idk;
5215 bool nonint_cst_expression_p;
5216 const char *error_msg;
5217 id = omp_reduction_id (ERROR_MARK, id, type);
5218 tree decl = lookup_name (id);
5219 if (decl == NULL_TREE)
5220 decl = error_mark_node;
5221 id = finish_id_expression (id, decl, NULL_TREE, &idk, false, true,
5222 &nonint_cst_expression_p, false, true, false,
5223 false, &error_msg, loc);
5224 if (idk == CP_ID_KIND_UNQUALIFIED
5225 && identifier_p (id))
5227 vec<tree, va_gc> *args = NULL;
5228 vec_safe_push (args, build_reference_type (type));
5229 id = perform_koenig_lookup (id, args, tf_none);
5232 else if (TREE_CODE (id) == SCOPE_REF)
5233 id = lookup_qualified_name (TREE_OPERAND (id, 0),
5234 omp_reduction_id (ERROR_MARK,
5235 TREE_OPERAND (id, 1),
5236 type),
5237 false, false);
5238 tree fns = id;
5239 id = NULL_TREE;
5240 if (fns && is_overloaded_fn (fns))
5242 for (lkp_iterator iter (get_fns (fns)); iter; ++iter)
5244 tree fndecl = *iter;
5245 if (TREE_CODE (fndecl) == FUNCTION_DECL)
5247 tree argtype = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
5248 if (same_type_p (TREE_TYPE (argtype), type))
5250 id = fndecl;
5251 break;
5256 if (id && BASELINK_P (fns))
5258 if (baselinkp)
5259 *baselinkp = fns;
5260 else
5261 baselink = fns;
5265 if (!id && CLASS_TYPE_P (type) && TYPE_BINFO (type))
5267 vec<tree> ambiguous = vNULL;
5268 tree binfo = TYPE_BINFO (type), base_binfo, ret = NULL_TREE;
5269 unsigned int ix;
5270 if (ambiguousp == NULL)
5271 ambiguousp = &ambiguous;
5272 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
5274 id = omp_reduction_lookup (loc, orig_id, BINFO_TYPE (base_binfo),
5275 baselinkp ? baselinkp : &baselink,
5276 ambiguousp);
5277 if (id == NULL_TREE)
5278 continue;
5279 if (!ambiguousp->is_empty ())
5280 ambiguousp->safe_push (id);
5281 else if (ret != NULL_TREE)
5283 ambiguousp->safe_push (ret);
5284 ambiguousp->safe_push (id);
5285 ret = NULL_TREE;
5287 else
5288 ret = id;
5290 if (ambiguousp != &ambiguous)
5291 return ret;
5292 if (!ambiguous.is_empty ())
5294 const char *str = _("candidates are:");
5295 unsigned int idx;
5296 tree udr;
5297 error_at (loc, "user defined reduction lookup is ambiguous");
5298 FOR_EACH_VEC_ELT (ambiguous, idx, udr)
5300 inform (DECL_SOURCE_LOCATION (udr), "%s %#qD", str, udr);
5301 if (idx == 0)
5302 str = get_spaces (str);
5304 ambiguous.release ();
5305 ret = error_mark_node;
5306 baselink = NULL_TREE;
5308 id = ret;
5310 if (id && baselink)
5311 perform_or_defer_access_check (BASELINK_BINFO (baselink),
5312 id, id, tf_warning_or_error);
5313 return id;
5316 /* Helper function for cp_parser_omp_declare_reduction_exprs
5317 and tsubst_omp_udr.
5318 Remove CLEANUP_STMT for data (omp_priv variable).
5319 Also append INIT_EXPR for DECL_INITIAL of omp_priv after its
5320 DECL_EXPR. */
5322 tree
5323 cp_remove_omp_priv_cleanup_stmt (tree *tp, int *walk_subtrees, void *data)
5325 if (TYPE_P (*tp))
5326 *walk_subtrees = 0;
5327 else if (TREE_CODE (*tp) == CLEANUP_STMT && CLEANUP_DECL (*tp) == (tree) data)
5328 *tp = CLEANUP_BODY (*tp);
5329 else if (TREE_CODE (*tp) == DECL_EXPR)
5331 tree decl = DECL_EXPR_DECL (*tp);
5332 if (!processing_template_decl
5333 && decl == (tree) data
5334 && DECL_INITIAL (decl)
5335 && DECL_INITIAL (decl) != error_mark_node)
5337 tree list = NULL_TREE;
5338 append_to_statement_list_force (*tp, &list);
5339 tree init_expr = build2 (INIT_EXPR, void_type_node,
5340 decl, DECL_INITIAL (decl));
5341 DECL_INITIAL (decl) = NULL_TREE;
5342 append_to_statement_list_force (init_expr, &list);
5343 *tp = list;
5346 return NULL_TREE;
5349 /* Data passed from cp_check_omp_declare_reduction to
5350 cp_check_omp_declare_reduction_r. */
5352 struct cp_check_omp_declare_reduction_data
5354 location_t loc;
5355 tree stmts[7];
5356 bool combiner_p;
5359 /* Helper function for cp_check_omp_declare_reduction, called via
5360 cp_walk_tree. */
5362 static tree
5363 cp_check_omp_declare_reduction_r (tree *tp, int *, void *data)
5365 struct cp_check_omp_declare_reduction_data *udr_data
5366 = (struct cp_check_omp_declare_reduction_data *) data;
5367 if (SSA_VAR_P (*tp)
5368 && !DECL_ARTIFICIAL (*tp)
5369 && *tp != DECL_EXPR_DECL (udr_data->stmts[udr_data->combiner_p ? 0 : 3])
5370 && *tp != DECL_EXPR_DECL (udr_data->stmts[udr_data->combiner_p ? 1 : 4]))
5372 location_t loc = udr_data->loc;
5373 if (udr_data->combiner_p)
5374 error_at (loc, "%<#pragma omp declare reduction%> combiner refers to "
5375 "variable %qD which is not %<omp_out%> nor %<omp_in%>",
5376 *tp);
5377 else
5378 error_at (loc, "%<#pragma omp declare reduction%> initializer refers "
5379 "to variable %qD which is not %<omp_priv%> nor "
5380 "%<omp_orig%>",
5381 *tp);
5382 return *tp;
5384 return NULL_TREE;
5387 /* Diagnose violation of OpenMP #pragma omp declare reduction restrictions. */
5389 void
5390 cp_check_omp_declare_reduction (tree udr)
5392 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (udr)));
5393 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
5394 type = TREE_TYPE (type);
5395 int i;
5396 location_t loc = DECL_SOURCE_LOCATION (udr);
5398 if (type == error_mark_node)
5399 return;
5400 if (ARITHMETIC_TYPE_P (type))
5402 static enum tree_code predef_codes[]
5403 = { PLUS_EXPR, MULT_EXPR, MINUS_EXPR, BIT_AND_EXPR, BIT_XOR_EXPR,
5404 BIT_IOR_EXPR, TRUTH_ANDIF_EXPR, TRUTH_ORIF_EXPR };
5405 for (i = 0; i < 8; i++)
5407 tree id = omp_reduction_id (predef_codes[i], NULL_TREE, NULL_TREE);
5408 const char *n1 = IDENTIFIER_POINTER (DECL_NAME (udr));
5409 const char *n2 = IDENTIFIER_POINTER (id);
5410 if (strncmp (n1, n2, IDENTIFIER_LENGTH (id)) == 0
5411 && (n1[IDENTIFIER_LENGTH (id)] == '~'
5412 || n1[IDENTIFIER_LENGTH (id)] == '\0'))
5413 break;
5416 if (i == 8
5417 && TREE_CODE (type) != COMPLEX_EXPR)
5419 const char prefix_minmax[] = "omp declare reduction m";
5420 size_t prefix_size = sizeof (prefix_minmax) - 1;
5421 const char *n = IDENTIFIER_POINTER (DECL_NAME (udr));
5422 if (strncmp (IDENTIFIER_POINTER (DECL_NAME (udr)),
5423 prefix_minmax, prefix_size) == 0
5424 && ((n[prefix_size] == 'i' && n[prefix_size + 1] == 'n')
5425 || (n[prefix_size] == 'a' && n[prefix_size + 1] == 'x'))
5426 && (n[prefix_size + 2] == '~' || n[prefix_size + 2] == '\0'))
5427 i = 0;
5429 if (i < 8)
5431 error_at (loc, "predeclared arithmetic type %qT in "
5432 "%<#pragma omp declare reduction%>", type);
5433 return;
5436 else if (TREE_CODE (type) == FUNCTION_TYPE
5437 || TREE_CODE (type) == METHOD_TYPE
5438 || TREE_CODE (type) == ARRAY_TYPE)
5440 error_at (loc, "function or array type %qT in "
5441 "%<#pragma omp declare reduction%>", type);
5442 return;
5444 else if (TREE_CODE (type) == REFERENCE_TYPE)
5446 error_at (loc, "reference type %qT in %<#pragma omp declare reduction%>",
5447 type);
5448 return;
5450 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
5452 error_at (loc, "const, volatile or __restrict qualified type %qT in "
5453 "%<#pragma omp declare reduction%>", type);
5454 return;
5457 tree body = DECL_SAVED_TREE (udr);
5458 if (body == NULL_TREE || TREE_CODE (body) != STATEMENT_LIST)
5459 return;
5461 tree_stmt_iterator tsi;
5462 struct cp_check_omp_declare_reduction_data data;
5463 memset (data.stmts, 0, sizeof data.stmts);
5464 for (i = 0, tsi = tsi_start (body);
5465 i < 7 && !tsi_end_p (tsi);
5466 i++, tsi_next (&tsi))
5467 data.stmts[i] = tsi_stmt (tsi);
5468 data.loc = loc;
5469 gcc_assert (tsi_end_p (tsi));
5470 if (i >= 3)
5472 gcc_assert (TREE_CODE (data.stmts[0]) == DECL_EXPR
5473 && TREE_CODE (data.stmts[1]) == DECL_EXPR);
5474 if (TREE_NO_WARNING (DECL_EXPR_DECL (data.stmts[0])))
5475 return;
5476 data.combiner_p = true;
5477 if (cp_walk_tree (&data.stmts[2], cp_check_omp_declare_reduction_r,
5478 &data, NULL))
5479 TREE_NO_WARNING (DECL_EXPR_DECL (data.stmts[0])) = 1;
5481 if (i >= 6)
5483 gcc_assert (TREE_CODE (data.stmts[3]) == DECL_EXPR
5484 && TREE_CODE (data.stmts[4]) == DECL_EXPR);
5485 data.combiner_p = false;
5486 if (cp_walk_tree (&data.stmts[5], cp_check_omp_declare_reduction_r,
5487 &data, NULL)
5488 || cp_walk_tree (&DECL_INITIAL (DECL_EXPR_DECL (data.stmts[3])),
5489 cp_check_omp_declare_reduction_r, &data, NULL))
5490 TREE_NO_WARNING (DECL_EXPR_DECL (data.stmts[0])) = 1;
5491 if (i == 7)
5492 gcc_assert (TREE_CODE (data.stmts[6]) == DECL_EXPR);
5496 /* Helper function of finish_omp_clauses. Clone STMT as if we were making
5497 an inline call. But, remap
5498 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
5499 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
5501 static tree
5502 clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
5503 tree decl, tree placeholder)
5505 copy_body_data id;
5506 hash_map<tree, tree> decl_map;
5508 decl_map.put (omp_decl1, placeholder);
5509 decl_map.put (omp_decl2, decl);
5510 memset (&id, 0, sizeof (id));
5511 id.src_fn = DECL_CONTEXT (omp_decl1);
5512 id.dst_fn = current_function_decl;
5513 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
5514 id.decl_map = &decl_map;
5516 id.copy_decl = copy_decl_no_change;
5517 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
5518 id.transform_new_cfg = true;
5519 id.transform_return_to_modify = false;
5520 id.transform_lang_insert_block = NULL;
5521 id.eh_lp_nr = 0;
5522 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
5523 return stmt;
5526 /* Helper function of finish_omp_clauses, called via cp_walk_tree.
5527 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
5529 static tree
5530 find_omp_placeholder_r (tree *tp, int *, void *data)
5532 if (*tp == (tree) data)
5533 return *tp;
5534 return NULL_TREE;
5537 /* Helper function of finish_omp_clauses. Handle OMP_CLAUSE_REDUCTION C.
5538 Return true if there is some error and the clause should be removed. */
5540 static bool
5541 finish_omp_reduction_clause (tree c, bool *need_default_ctor, bool *need_dtor)
5543 tree t = OMP_CLAUSE_DECL (c);
5544 bool predefined = false;
5545 if (TREE_CODE (t) == TREE_LIST)
5547 gcc_assert (processing_template_decl);
5548 return false;
5550 tree type = TREE_TYPE (t);
5551 if (TREE_CODE (t) == MEM_REF)
5552 type = TREE_TYPE (type);
5553 if (TREE_CODE (type) == REFERENCE_TYPE)
5554 type = TREE_TYPE (type);
5555 if (TREE_CODE (type) == ARRAY_TYPE)
5557 tree oatype = type;
5558 gcc_assert (TREE_CODE (t) != MEM_REF);
5559 while (TREE_CODE (type) == ARRAY_TYPE)
5560 type = TREE_TYPE (type);
5561 if (!processing_template_decl)
5563 t = require_complete_type (t);
5564 if (t == error_mark_node)
5565 return true;
5566 tree size = size_binop (EXACT_DIV_EXPR, TYPE_SIZE_UNIT (oatype),
5567 TYPE_SIZE_UNIT (type));
5568 if (integer_zerop (size))
5570 error ("%qE in %<reduction%> clause is a zero size array",
5571 omp_clause_printable_decl (t));
5572 return true;
5574 size = size_binop (MINUS_EXPR, size, size_one_node);
5575 tree index_type = build_index_type (size);
5576 tree atype = build_array_type (type, index_type);
5577 tree ptype = build_pointer_type (type);
5578 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
5579 t = build_fold_addr_expr (t);
5580 t = build2 (MEM_REF, atype, t, build_int_cst (ptype, 0));
5581 OMP_CLAUSE_DECL (c) = t;
5584 if (type == error_mark_node)
5585 return true;
5586 else if (ARITHMETIC_TYPE_P (type))
5587 switch (OMP_CLAUSE_REDUCTION_CODE (c))
5589 case PLUS_EXPR:
5590 case MULT_EXPR:
5591 case MINUS_EXPR:
5592 predefined = true;
5593 break;
5594 case MIN_EXPR:
5595 case MAX_EXPR:
5596 if (TREE_CODE (type) == COMPLEX_TYPE)
5597 break;
5598 predefined = true;
5599 break;
5600 case BIT_AND_EXPR:
5601 case BIT_IOR_EXPR:
5602 case BIT_XOR_EXPR:
5603 if (FLOAT_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
5604 break;
5605 predefined = true;
5606 break;
5607 case TRUTH_ANDIF_EXPR:
5608 case TRUTH_ORIF_EXPR:
5609 if (FLOAT_TYPE_P (type))
5610 break;
5611 predefined = true;
5612 break;
5613 default:
5614 break;
5616 else if (TYPE_READONLY (type))
5618 error ("%qE has const type for %<reduction%>",
5619 omp_clause_printable_decl (t));
5620 return true;
5622 else if (!processing_template_decl)
5624 t = require_complete_type (t);
5625 if (t == error_mark_node)
5626 return true;
5627 OMP_CLAUSE_DECL (c) = t;
5630 if (predefined)
5632 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = NULL_TREE;
5633 return false;
5635 else if (processing_template_decl)
5636 return false;
5638 tree id = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
5640 type = TYPE_MAIN_VARIANT (type);
5641 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = NULL_TREE;
5642 if (id == NULL_TREE)
5643 id = omp_reduction_id (OMP_CLAUSE_REDUCTION_CODE (c),
5644 NULL_TREE, NULL_TREE);
5645 id = omp_reduction_lookup (OMP_CLAUSE_LOCATION (c), id, type, NULL, NULL);
5646 if (id)
5648 if (id == error_mark_node)
5649 return true;
5650 mark_used (id);
5651 tree body = DECL_SAVED_TREE (id);
5652 if (!body)
5653 return true;
5654 if (TREE_CODE (body) == STATEMENT_LIST)
5656 tree_stmt_iterator tsi;
5657 tree placeholder = NULL_TREE, decl_placeholder = NULL_TREE;
5658 int i;
5659 tree stmts[7];
5660 tree atype = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (id)));
5661 atype = TREE_TYPE (atype);
5662 bool need_static_cast = !same_type_p (type, atype);
5663 memset (stmts, 0, sizeof stmts);
5664 for (i = 0, tsi = tsi_start (body);
5665 i < 7 && !tsi_end_p (tsi);
5666 i++, tsi_next (&tsi))
5667 stmts[i] = tsi_stmt (tsi);
5668 gcc_assert (tsi_end_p (tsi));
5670 if (i >= 3)
5672 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
5673 && TREE_CODE (stmts[1]) == DECL_EXPR);
5674 placeholder = build_lang_decl (VAR_DECL, NULL_TREE, type);
5675 DECL_ARTIFICIAL (placeholder) = 1;
5676 DECL_IGNORED_P (placeholder) = 1;
5677 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
5678 if (TREE_CODE (t) == MEM_REF)
5680 decl_placeholder = build_lang_decl (VAR_DECL, NULL_TREE,
5681 type);
5682 DECL_ARTIFICIAL (decl_placeholder) = 1;
5683 DECL_IGNORED_P (decl_placeholder) = 1;
5684 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c) = decl_placeholder;
5686 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[0])))
5687 cxx_mark_addressable (placeholder);
5688 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[1]))
5689 && TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c)))
5690 != REFERENCE_TYPE)
5691 cxx_mark_addressable (decl_placeholder ? decl_placeholder
5692 : OMP_CLAUSE_DECL (c));
5693 tree omp_out = placeholder;
5694 tree omp_in = decl_placeholder ? decl_placeholder
5695 : convert_from_reference (OMP_CLAUSE_DECL (c));
5696 if (need_static_cast)
5698 tree rtype = build_reference_type (atype);
5699 omp_out = build_static_cast (rtype, omp_out,
5700 tf_warning_or_error);
5701 omp_in = build_static_cast (rtype, omp_in,
5702 tf_warning_or_error);
5703 if (omp_out == error_mark_node || omp_in == error_mark_node)
5704 return true;
5705 omp_out = convert_from_reference (omp_out);
5706 omp_in = convert_from_reference (omp_in);
5708 OMP_CLAUSE_REDUCTION_MERGE (c)
5709 = clone_omp_udr (stmts[2], DECL_EXPR_DECL (stmts[0]),
5710 DECL_EXPR_DECL (stmts[1]), omp_in, omp_out);
5712 if (i >= 6)
5714 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
5715 && TREE_CODE (stmts[4]) == DECL_EXPR);
5716 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[3])))
5717 cxx_mark_addressable (decl_placeholder ? decl_placeholder
5718 : OMP_CLAUSE_DECL (c));
5719 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[4])))
5720 cxx_mark_addressable (placeholder);
5721 tree omp_priv = decl_placeholder ? decl_placeholder
5722 : convert_from_reference (OMP_CLAUSE_DECL (c));
5723 tree omp_orig = placeholder;
5724 if (need_static_cast)
5726 if (i == 7)
5728 error_at (OMP_CLAUSE_LOCATION (c),
5729 "user defined reduction with constructor "
5730 "initializer for base class %qT", atype);
5731 return true;
5733 tree rtype = build_reference_type (atype);
5734 omp_priv = build_static_cast (rtype, omp_priv,
5735 tf_warning_or_error);
5736 omp_orig = build_static_cast (rtype, omp_orig,
5737 tf_warning_or_error);
5738 if (omp_priv == error_mark_node
5739 || omp_orig == error_mark_node)
5740 return true;
5741 omp_priv = convert_from_reference (omp_priv);
5742 omp_orig = convert_from_reference (omp_orig);
5744 if (i == 6)
5745 *need_default_ctor = true;
5746 OMP_CLAUSE_REDUCTION_INIT (c)
5747 = clone_omp_udr (stmts[5], DECL_EXPR_DECL (stmts[4]),
5748 DECL_EXPR_DECL (stmts[3]),
5749 omp_priv, omp_orig);
5750 if (cp_walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
5751 find_omp_placeholder_r, placeholder, NULL))
5752 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
5754 else if (i >= 3)
5756 if (CLASS_TYPE_P (type) && !pod_type_p (type))
5757 *need_default_ctor = true;
5758 else
5760 tree init;
5761 tree v = decl_placeholder ? decl_placeholder
5762 : convert_from_reference (t);
5763 if (AGGREGATE_TYPE_P (TREE_TYPE (v)))
5764 init = build_constructor (TREE_TYPE (v), NULL);
5765 else
5766 init = fold_convert (TREE_TYPE (v), integer_zero_node);
5767 OMP_CLAUSE_REDUCTION_INIT (c)
5768 = build2 (INIT_EXPR, TREE_TYPE (v), v, init);
5773 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
5774 *need_dtor = true;
5775 else
5777 error ("user defined reduction not found for %qE",
5778 omp_clause_printable_decl (t));
5779 return true;
5781 if (TREE_CODE (OMP_CLAUSE_DECL (c)) == MEM_REF)
5782 gcc_assert (TYPE_SIZE_UNIT (type)
5783 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST);
5784 return false;
5787 /* Called from finish_struct_1. linear(this) or linear(this:step)
5788 clauses might not be finalized yet because the class has been incomplete
5789 when parsing #pragma omp declare simd methods. Fix those up now. */
5791 void
5792 finish_omp_declare_simd_methods (tree t)
5794 if (processing_template_decl)
5795 return;
5797 for (tree x = TYPE_METHODS (t); x; x = DECL_CHAIN (x))
5799 if (TREE_CODE (TREE_TYPE (x)) != METHOD_TYPE)
5800 continue;
5801 tree ods = lookup_attribute ("omp declare simd", DECL_ATTRIBUTES (x));
5802 if (!ods || !TREE_VALUE (ods))
5803 continue;
5804 for (tree c = TREE_VALUE (TREE_VALUE (ods)); c; c = OMP_CLAUSE_CHAIN (c))
5805 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
5806 && integer_zerop (OMP_CLAUSE_DECL (c))
5807 && OMP_CLAUSE_LINEAR_STEP (c)
5808 && TREE_CODE (TREE_TYPE (OMP_CLAUSE_LINEAR_STEP (c)))
5809 == POINTER_TYPE)
5811 tree s = OMP_CLAUSE_LINEAR_STEP (c);
5812 s = fold_convert_loc (OMP_CLAUSE_LOCATION (c), sizetype, s);
5813 s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MULT_EXPR,
5814 sizetype, s, TYPE_SIZE_UNIT (t));
5815 OMP_CLAUSE_LINEAR_STEP (c) = s;
5820 /* Adjust sink depend clause to take into account pointer offsets.
5822 Return TRUE if there was a problem processing the offset, and the
5823 whole clause should be removed. */
5825 static bool
5826 cp_finish_omp_clause_depend_sink (tree sink_clause)
5828 tree t = OMP_CLAUSE_DECL (sink_clause);
5829 gcc_assert (TREE_CODE (t) == TREE_LIST);
5831 /* Make sure we don't adjust things twice for templates. */
5832 if (processing_template_decl)
5833 return false;
5835 for (; t; t = TREE_CHAIN (t))
5837 tree decl = TREE_VALUE (t);
5838 if (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
5840 tree offset = TREE_PURPOSE (t);
5841 bool neg = wi::neg_p ((wide_int) offset);
5842 offset = fold_unary (ABS_EXPR, TREE_TYPE (offset), offset);
5843 decl = mark_rvalue_use (decl);
5844 decl = convert_from_reference (decl);
5845 tree t2 = pointer_int_sum (OMP_CLAUSE_LOCATION (sink_clause),
5846 neg ? MINUS_EXPR : PLUS_EXPR,
5847 decl, offset);
5848 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (sink_clause),
5849 MINUS_EXPR, sizetype,
5850 fold_convert (sizetype, t2),
5851 fold_convert (sizetype, decl));
5852 if (t2 == error_mark_node)
5853 return true;
5854 TREE_PURPOSE (t) = t2;
5857 return false;
5860 /* For all elements of CLAUSES, validate them vs OpenMP constraints.
5861 Remove any elements from the list that are invalid. */
5863 tree
5864 finish_omp_clauses (tree clauses, enum c_omp_region_type ort)
5866 bitmap_head generic_head, firstprivate_head, lastprivate_head;
5867 bitmap_head aligned_head, map_head, map_field_head, oacc_reduction_head;
5868 tree c, t, *pc;
5869 tree safelen = NULL_TREE;
5870 bool branch_seen = false;
5871 bool copyprivate_seen = false;
5872 bool ordered_seen = false;
5873 bool oacc_async = false;
5875 bitmap_obstack_initialize (NULL);
5876 bitmap_initialize (&generic_head, &bitmap_default_obstack);
5877 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
5878 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
5879 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
5880 bitmap_initialize (&map_head, &bitmap_default_obstack);
5881 bitmap_initialize (&map_field_head, &bitmap_default_obstack);
5882 bitmap_initialize (&oacc_reduction_head, &bitmap_default_obstack);
5884 if (ort & C_ORT_ACC)
5885 for (c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
5886 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ASYNC)
5888 oacc_async = true;
5889 break;
5892 for (pc = &clauses, c = clauses; c ; c = *pc)
5894 bool remove = false;
5895 bool field_ok = false;
5897 switch (OMP_CLAUSE_CODE (c))
5899 case OMP_CLAUSE_SHARED:
5900 field_ok = ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP);
5901 goto check_dup_generic;
5902 case OMP_CLAUSE_PRIVATE:
5903 field_ok = ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP);
5904 goto check_dup_generic;
5905 case OMP_CLAUSE_REDUCTION:
5906 field_ok = ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP);
5907 t = OMP_CLAUSE_DECL (c);
5908 if (TREE_CODE (t) == TREE_LIST)
5910 if (handle_omp_array_sections (c, ort))
5912 remove = true;
5913 break;
5915 if (TREE_CODE (t) == TREE_LIST)
5917 while (TREE_CODE (t) == TREE_LIST)
5918 t = TREE_CHAIN (t);
5920 else
5922 gcc_assert (TREE_CODE (t) == MEM_REF);
5923 t = TREE_OPERAND (t, 0);
5924 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
5925 t = TREE_OPERAND (t, 0);
5926 if (TREE_CODE (t) == ADDR_EXPR
5927 || TREE_CODE (t) == INDIRECT_REF)
5928 t = TREE_OPERAND (t, 0);
5930 tree n = omp_clause_decl_field (t);
5931 if (n)
5932 t = n;
5933 goto check_dup_generic_t;
5935 if (oacc_async)
5936 cxx_mark_addressable (t);
5937 goto check_dup_generic;
5938 case OMP_CLAUSE_COPYPRIVATE:
5939 copyprivate_seen = true;
5940 field_ok = ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP);
5941 goto check_dup_generic;
5942 case OMP_CLAUSE_COPYIN:
5943 goto check_dup_generic;
5944 case OMP_CLAUSE_LINEAR:
5945 field_ok = ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP);
5946 t = OMP_CLAUSE_DECL (c);
5947 if (ort != C_ORT_OMP_DECLARE_SIMD
5948 && OMP_CLAUSE_LINEAR_KIND (c) != OMP_CLAUSE_LINEAR_DEFAULT)
5950 error_at (OMP_CLAUSE_LOCATION (c),
5951 "modifier should not be specified in %<linear%> "
5952 "clause on %<simd%> or %<for%> constructs");
5953 OMP_CLAUSE_LINEAR_KIND (c) = OMP_CLAUSE_LINEAR_DEFAULT;
5955 if ((VAR_P (t) || TREE_CODE (t) == PARM_DECL)
5956 && !type_dependent_expression_p (t))
5958 tree type = TREE_TYPE (t);
5959 if ((OMP_CLAUSE_LINEAR_KIND (c) == OMP_CLAUSE_LINEAR_REF
5960 || OMP_CLAUSE_LINEAR_KIND (c) == OMP_CLAUSE_LINEAR_UVAL)
5961 && TREE_CODE (type) != REFERENCE_TYPE)
5963 error ("linear clause with %qs modifier applied to "
5964 "non-reference variable with %qT type",
5965 OMP_CLAUSE_LINEAR_KIND (c) == OMP_CLAUSE_LINEAR_REF
5966 ? "ref" : "uval", TREE_TYPE (t));
5967 remove = true;
5968 break;
5970 if (TREE_CODE (type) == REFERENCE_TYPE)
5971 type = TREE_TYPE (type);
5972 if (ort == C_ORT_CILK)
5974 if (!INTEGRAL_TYPE_P (type)
5975 && !SCALAR_FLOAT_TYPE_P (type)
5976 && TREE_CODE (type) != POINTER_TYPE)
5978 error ("linear clause applied to non-integral, "
5979 "non-floating, non-pointer variable with %qT type",
5980 TREE_TYPE (t));
5981 remove = true;
5982 break;
5985 else if (OMP_CLAUSE_LINEAR_KIND (c) != OMP_CLAUSE_LINEAR_REF)
5987 if (!INTEGRAL_TYPE_P (type)
5988 && TREE_CODE (type) != POINTER_TYPE)
5990 error ("linear clause applied to non-integral non-pointer"
5991 " variable with %qT type", TREE_TYPE (t));
5992 remove = true;
5993 break;
5997 t = OMP_CLAUSE_LINEAR_STEP (c);
5998 if (t == NULL_TREE)
5999 t = integer_one_node;
6000 if (t == error_mark_node)
6002 remove = true;
6003 break;
6005 else if (!type_dependent_expression_p (t)
6006 && !INTEGRAL_TYPE_P (TREE_TYPE (t))
6007 && (ort != C_ORT_OMP_DECLARE_SIMD
6008 || TREE_CODE (t) != PARM_DECL
6009 || TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE
6010 || !INTEGRAL_TYPE_P (TREE_TYPE (TREE_TYPE (t)))))
6012 error ("linear step expression must be integral");
6013 remove = true;
6014 break;
6016 else
6018 t = mark_rvalue_use (t);
6019 if (ort == C_ORT_OMP_DECLARE_SIMD && TREE_CODE (t) == PARM_DECL)
6021 OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c) = 1;
6022 goto check_dup_generic;
6024 if (!processing_template_decl
6025 && (VAR_P (OMP_CLAUSE_DECL (c))
6026 || TREE_CODE (OMP_CLAUSE_DECL (c)) == PARM_DECL))
6028 if (ort == C_ORT_OMP_DECLARE_SIMD)
6030 t = maybe_constant_value (t);
6031 if (TREE_CODE (t) != INTEGER_CST)
6033 error_at (OMP_CLAUSE_LOCATION (c),
6034 "%<linear%> clause step %qE is neither "
6035 "constant nor a parameter", t);
6036 remove = true;
6037 break;
6040 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6041 tree type = TREE_TYPE (OMP_CLAUSE_DECL (c));
6042 if (TREE_CODE (type) == REFERENCE_TYPE)
6043 type = TREE_TYPE (type);
6044 if (OMP_CLAUSE_LINEAR_KIND (c) == OMP_CLAUSE_LINEAR_REF)
6046 type = build_pointer_type (type);
6047 tree d = fold_convert (type, OMP_CLAUSE_DECL (c));
6048 t = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
6049 d, t);
6050 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c),
6051 MINUS_EXPR, sizetype,
6052 fold_convert (sizetype, t),
6053 fold_convert (sizetype, d));
6054 if (t == error_mark_node)
6056 remove = true;
6057 break;
6060 else if (TREE_CODE (type) == POINTER_TYPE
6061 /* Can't multiply the step yet if *this
6062 is still incomplete type. */
6063 && (ort != C_ORT_OMP_DECLARE_SIMD
6064 || TREE_CODE (OMP_CLAUSE_DECL (c)) != PARM_DECL
6065 || !DECL_ARTIFICIAL (OMP_CLAUSE_DECL (c))
6066 || DECL_NAME (OMP_CLAUSE_DECL (c))
6067 != this_identifier
6068 || !TYPE_BEING_DEFINED (TREE_TYPE (type))))
6070 tree d = convert_from_reference (OMP_CLAUSE_DECL (c));
6071 t = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
6072 d, t);
6073 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c),
6074 MINUS_EXPR, sizetype,
6075 fold_convert (sizetype, t),
6076 fold_convert (sizetype, d));
6077 if (t == error_mark_node)
6079 remove = true;
6080 break;
6083 else
6084 t = fold_convert (type, t);
6086 OMP_CLAUSE_LINEAR_STEP (c) = t;
6088 goto check_dup_generic;
6089 check_dup_generic:
6090 t = omp_clause_decl_field (OMP_CLAUSE_DECL (c));
6091 if (t)
6093 if (!remove && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_SHARED)
6094 omp_note_field_privatization (t, OMP_CLAUSE_DECL (c));
6096 else
6097 t = OMP_CLAUSE_DECL (c);
6098 check_dup_generic_t:
6099 if (t == current_class_ptr
6100 && (ort != C_ORT_OMP_DECLARE_SIMD
6101 || (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_LINEAR
6102 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_UNIFORM)))
6104 error ("%<this%> allowed in OpenMP only in %<declare simd%>"
6105 " clauses");
6106 remove = true;
6107 break;
6109 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL
6110 && (!field_ok || TREE_CODE (t) != FIELD_DECL))
6112 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
6113 break;
6114 if (DECL_P (t))
6115 error ("%qD is not a variable in clause %qs", t,
6116 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6117 else
6118 error ("%qE is not a variable in clause %qs", t,
6119 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6120 remove = true;
6122 else if (ort == C_ORT_ACC
6123 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
6125 if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
6127 error ("%qD appears more than once in reduction clauses", t);
6128 remove = true;
6130 else
6131 bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
6133 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
6134 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
6135 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
6137 error ("%qD appears more than once in data clauses", t);
6138 remove = true;
6140 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
6141 && bitmap_bit_p (&map_head, DECL_UID (t)))
6143 if (ort == C_ORT_ACC)
6144 error ("%qD appears more than once in data clauses", t);
6145 else
6146 error ("%qD appears both in data and map clauses", t);
6147 remove = true;
6149 else
6150 bitmap_set_bit (&generic_head, DECL_UID (t));
6151 if (!field_ok)
6152 break;
6153 handle_field_decl:
6154 if (!remove
6155 && TREE_CODE (t) == FIELD_DECL
6156 && t == OMP_CLAUSE_DECL (c)
6157 && ort != C_ORT_ACC)
6159 OMP_CLAUSE_DECL (c)
6160 = omp_privatize_field (t, (OMP_CLAUSE_CODE (c)
6161 == OMP_CLAUSE_SHARED));
6162 if (OMP_CLAUSE_DECL (c) == error_mark_node)
6163 remove = true;
6165 break;
6167 case OMP_CLAUSE_FIRSTPRIVATE:
6168 t = omp_clause_decl_field (OMP_CLAUSE_DECL (c));
6169 if (t)
6170 omp_note_field_privatization (t, OMP_CLAUSE_DECL (c));
6171 else
6172 t = OMP_CLAUSE_DECL (c);
6173 if (ort != C_ORT_ACC && t == current_class_ptr)
6175 error ("%<this%> allowed in OpenMP only in %<declare simd%>"
6176 " clauses");
6177 remove = true;
6178 break;
6180 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL
6181 && ((ort & C_ORT_OMP_DECLARE_SIMD) != C_ORT_OMP
6182 || TREE_CODE (t) != FIELD_DECL))
6184 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
6185 break;
6186 if (DECL_P (t))
6187 error ("%qD is not a variable in clause %<firstprivate%>", t);
6188 else
6189 error ("%qE is not a variable in clause %<firstprivate%>", t);
6190 remove = true;
6192 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
6193 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
6195 error ("%qD appears more than once in data clauses", t);
6196 remove = true;
6198 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
6200 if (ort == C_ORT_ACC)
6201 error ("%qD appears more than once in data clauses", t);
6202 else
6203 error ("%qD appears both in data and map clauses", t);
6204 remove = true;
6206 else
6207 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
6208 goto handle_field_decl;
6210 case OMP_CLAUSE_LASTPRIVATE:
6211 t = omp_clause_decl_field (OMP_CLAUSE_DECL (c));
6212 if (t)
6213 omp_note_field_privatization (t, OMP_CLAUSE_DECL (c));
6214 else
6215 t = OMP_CLAUSE_DECL (c);
6216 if (t == current_class_ptr)
6218 error ("%<this%> allowed in OpenMP only in %<declare simd%>"
6219 " clauses");
6220 remove = true;
6221 break;
6223 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL
6224 && ((ort & C_ORT_OMP_DECLARE_SIMD) != C_ORT_OMP
6225 || TREE_CODE (t) != FIELD_DECL))
6227 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
6228 break;
6229 if (DECL_P (t))
6230 error ("%qD is not a variable in clause %<lastprivate%>", t);
6231 else
6232 error ("%qE is not a variable in clause %<lastprivate%>", t);
6233 remove = true;
6235 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
6236 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
6238 error ("%qD appears more than once in data clauses", t);
6239 remove = true;
6241 else
6242 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
6243 goto handle_field_decl;
6245 case OMP_CLAUSE_IF:
6246 t = OMP_CLAUSE_IF_EXPR (c);
6247 t = maybe_convert_cond (t);
6248 if (t == error_mark_node)
6249 remove = true;
6250 else if (!processing_template_decl)
6251 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6252 OMP_CLAUSE_IF_EXPR (c) = t;
6253 break;
6255 case OMP_CLAUSE_FINAL:
6256 t = OMP_CLAUSE_FINAL_EXPR (c);
6257 t = maybe_convert_cond (t);
6258 if (t == error_mark_node)
6259 remove = true;
6260 else if (!processing_template_decl)
6261 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6262 OMP_CLAUSE_FINAL_EXPR (c) = t;
6263 break;
6265 case OMP_CLAUSE_GANG:
6266 /* Operand 1 is the gang static: argument. */
6267 t = OMP_CLAUSE_OPERAND (c, 1);
6268 if (t != NULL_TREE)
6270 if (t == error_mark_node)
6271 remove = true;
6272 else if (!type_dependent_expression_p (t)
6273 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6275 error ("%<gang%> static expression must be integral");
6276 remove = true;
6278 else
6280 t = mark_rvalue_use (t);
6281 if (!processing_template_decl)
6283 t = maybe_constant_value (t);
6284 if (TREE_CODE (t) == INTEGER_CST
6285 && tree_int_cst_sgn (t) != 1
6286 && t != integer_minus_one_node)
6288 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6289 "%<gang%> static value must be "
6290 "positive");
6291 t = integer_one_node;
6294 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6296 OMP_CLAUSE_OPERAND (c, 1) = t;
6298 /* Check operand 0, the num argument. */
6299 /* FALLTHRU */
6301 case OMP_CLAUSE_WORKER:
6302 case OMP_CLAUSE_VECTOR:
6303 if (OMP_CLAUSE_OPERAND (c, 0) == NULL_TREE)
6304 break;
6305 /* FALLTHRU */
6307 case OMP_CLAUSE_NUM_TASKS:
6308 case OMP_CLAUSE_NUM_TEAMS:
6309 case OMP_CLAUSE_NUM_THREADS:
6310 case OMP_CLAUSE_NUM_GANGS:
6311 case OMP_CLAUSE_NUM_WORKERS:
6312 case OMP_CLAUSE_VECTOR_LENGTH:
6313 t = OMP_CLAUSE_OPERAND (c, 0);
6314 if (t == error_mark_node)
6315 remove = true;
6316 else if (!type_dependent_expression_p (t)
6317 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6319 switch (OMP_CLAUSE_CODE (c))
6321 case OMP_CLAUSE_GANG:
6322 error_at (OMP_CLAUSE_LOCATION (c),
6323 "%<gang%> num expression must be integral"); break;
6324 case OMP_CLAUSE_VECTOR:
6325 error_at (OMP_CLAUSE_LOCATION (c),
6326 "%<vector%> length expression must be integral");
6327 break;
6328 case OMP_CLAUSE_WORKER:
6329 error_at (OMP_CLAUSE_LOCATION (c),
6330 "%<worker%> num expression must be integral");
6331 break;
6332 default:
6333 error_at (OMP_CLAUSE_LOCATION (c),
6334 "%qs expression must be integral",
6335 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6337 remove = true;
6339 else
6341 t = mark_rvalue_use (t);
6342 if (!processing_template_decl)
6344 t = maybe_constant_value (t);
6345 if (TREE_CODE (t) == INTEGER_CST
6346 && tree_int_cst_sgn (t) != 1)
6348 switch (OMP_CLAUSE_CODE (c))
6350 case OMP_CLAUSE_GANG:
6351 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6352 "%<gang%> num value must be positive");
6353 break;
6354 case OMP_CLAUSE_VECTOR:
6355 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6356 "%<vector%> length value must be "
6357 "positive");
6358 break;
6359 case OMP_CLAUSE_WORKER:
6360 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6361 "%<worker%> num value must be "
6362 "positive");
6363 break;
6364 default:
6365 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6366 "%qs value must be positive",
6367 omp_clause_code_name
6368 [OMP_CLAUSE_CODE (c)]);
6370 t = integer_one_node;
6372 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6374 OMP_CLAUSE_OPERAND (c, 0) = t;
6376 break;
6378 case OMP_CLAUSE_SCHEDULE:
6379 if (OMP_CLAUSE_SCHEDULE_KIND (c) & OMP_CLAUSE_SCHEDULE_NONMONOTONIC)
6381 const char *p = NULL;
6382 switch (OMP_CLAUSE_SCHEDULE_KIND (c) & OMP_CLAUSE_SCHEDULE_MASK)
6384 case OMP_CLAUSE_SCHEDULE_STATIC: p = "static"; break;
6385 case OMP_CLAUSE_SCHEDULE_DYNAMIC: break;
6386 case OMP_CLAUSE_SCHEDULE_GUIDED: break;
6387 case OMP_CLAUSE_SCHEDULE_AUTO: p = "auto"; break;
6388 case OMP_CLAUSE_SCHEDULE_RUNTIME: p = "runtime"; break;
6389 default: gcc_unreachable ();
6391 if (p)
6393 error_at (OMP_CLAUSE_LOCATION (c),
6394 "%<nonmonotonic%> modifier specified for %qs "
6395 "schedule kind", p);
6396 OMP_CLAUSE_SCHEDULE_KIND (c)
6397 = (enum omp_clause_schedule_kind)
6398 (OMP_CLAUSE_SCHEDULE_KIND (c)
6399 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
6403 t = OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c);
6404 if (t == NULL)
6406 else if (t == error_mark_node)
6407 remove = true;
6408 else if (!type_dependent_expression_p (t)
6409 && (OMP_CLAUSE_SCHEDULE_KIND (c)
6410 != OMP_CLAUSE_SCHEDULE_CILKFOR)
6411 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6413 error ("schedule chunk size expression must be integral");
6414 remove = true;
6416 else
6418 t = mark_rvalue_use (t);
6419 if (!processing_template_decl)
6421 if (OMP_CLAUSE_SCHEDULE_KIND (c)
6422 == OMP_CLAUSE_SCHEDULE_CILKFOR)
6424 t = convert_to_integer (long_integer_type_node, t);
6425 if (t == error_mark_node)
6427 remove = true;
6428 break;
6431 else
6433 t = maybe_constant_value (t);
6434 if (TREE_CODE (t) == INTEGER_CST
6435 && tree_int_cst_sgn (t) != 1)
6437 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6438 "chunk size value must be positive");
6439 t = integer_one_node;
6442 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6444 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
6446 break;
6448 case OMP_CLAUSE_SIMDLEN:
6449 case OMP_CLAUSE_SAFELEN:
6450 t = OMP_CLAUSE_OPERAND (c, 0);
6451 if (t == error_mark_node)
6452 remove = true;
6453 else if (!type_dependent_expression_p (t)
6454 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6456 error ("%qs length expression must be integral",
6457 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6458 remove = true;
6460 else
6462 t = mark_rvalue_use (t);
6463 if (!processing_template_decl)
6465 t = maybe_constant_value (t);
6466 if (TREE_CODE (t) != INTEGER_CST
6467 || tree_int_cst_sgn (t) != 1)
6469 error ("%qs length expression must be positive constant"
6470 " integer expression",
6471 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6472 remove = true;
6475 OMP_CLAUSE_OPERAND (c, 0) = t;
6476 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SAFELEN)
6477 safelen = c;
6479 break;
6481 case OMP_CLAUSE_ASYNC:
6482 t = OMP_CLAUSE_ASYNC_EXPR (c);
6483 if (t == error_mark_node)
6484 remove = true;
6485 else if (!type_dependent_expression_p (t)
6486 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6488 error ("%<async%> expression must be integral");
6489 remove = true;
6491 else
6493 t = mark_rvalue_use (t);
6494 if (!processing_template_decl)
6495 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6496 OMP_CLAUSE_ASYNC_EXPR (c) = t;
6498 break;
6500 case OMP_CLAUSE_WAIT:
6501 t = OMP_CLAUSE_WAIT_EXPR (c);
6502 if (t == error_mark_node)
6503 remove = true;
6504 else if (!processing_template_decl)
6505 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6506 OMP_CLAUSE_WAIT_EXPR (c) = t;
6507 break;
6509 case OMP_CLAUSE_THREAD_LIMIT:
6510 t = OMP_CLAUSE_THREAD_LIMIT_EXPR (c);
6511 if (t == error_mark_node)
6512 remove = true;
6513 else if (!type_dependent_expression_p (t)
6514 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6516 error ("%<thread_limit%> expression must be integral");
6517 remove = true;
6519 else
6521 t = mark_rvalue_use (t);
6522 if (!processing_template_decl)
6524 t = maybe_constant_value (t);
6525 if (TREE_CODE (t) == INTEGER_CST
6526 && tree_int_cst_sgn (t) != 1)
6528 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6529 "%<thread_limit%> value must be positive");
6530 t = integer_one_node;
6532 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6534 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
6536 break;
6538 case OMP_CLAUSE_DEVICE:
6539 t = OMP_CLAUSE_DEVICE_ID (c);
6540 if (t == error_mark_node)
6541 remove = true;
6542 else if (!type_dependent_expression_p (t)
6543 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6545 error ("%<device%> id must be integral");
6546 remove = true;
6548 else
6550 t = mark_rvalue_use (t);
6551 if (!processing_template_decl)
6552 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6553 OMP_CLAUSE_DEVICE_ID (c) = t;
6555 break;
6557 case OMP_CLAUSE_DIST_SCHEDULE:
6558 t = OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c);
6559 if (t == NULL)
6561 else if (t == error_mark_node)
6562 remove = true;
6563 else if (!type_dependent_expression_p (t)
6564 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6566 error ("%<dist_schedule%> chunk size expression must be "
6567 "integral");
6568 remove = true;
6570 else
6572 t = mark_rvalue_use (t);
6573 if (!processing_template_decl)
6574 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6575 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
6577 break;
6579 case OMP_CLAUSE_ALIGNED:
6580 t = OMP_CLAUSE_DECL (c);
6581 if (t == current_class_ptr && ort != C_ORT_OMP_DECLARE_SIMD)
6583 error ("%<this%> allowed in OpenMP only in %<declare simd%>"
6584 " clauses");
6585 remove = true;
6586 break;
6588 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
6590 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
6591 break;
6592 if (DECL_P (t))
6593 error ("%qD is not a variable in %<aligned%> clause", t);
6594 else
6595 error ("%qE is not a variable in %<aligned%> clause", t);
6596 remove = true;
6598 else if (!type_dependent_expression_p (t)
6599 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE
6600 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE
6601 && (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE
6602 || (!POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (t)))
6603 && (TREE_CODE (TREE_TYPE (TREE_TYPE (t)))
6604 != ARRAY_TYPE))))
6606 error_at (OMP_CLAUSE_LOCATION (c),
6607 "%qE in %<aligned%> clause is neither a pointer nor "
6608 "an array nor a reference to pointer or array", t);
6609 remove = true;
6611 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
6613 error ("%qD appears more than once in %<aligned%> clauses", t);
6614 remove = true;
6616 else
6617 bitmap_set_bit (&aligned_head, DECL_UID (t));
6618 t = OMP_CLAUSE_ALIGNED_ALIGNMENT (c);
6619 if (t == error_mark_node)
6620 remove = true;
6621 else if (t == NULL_TREE)
6622 break;
6623 else if (!type_dependent_expression_p (t)
6624 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6626 error ("%<aligned%> clause alignment expression must "
6627 "be integral");
6628 remove = true;
6630 else
6632 t = mark_rvalue_use (t);
6633 if (!processing_template_decl)
6635 t = maybe_constant_value (t);
6636 if (TREE_CODE (t) != INTEGER_CST
6637 || tree_int_cst_sgn (t) != 1)
6639 error ("%<aligned%> clause alignment expression must be "
6640 "positive constant integer expression");
6641 remove = true;
6644 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = t;
6646 break;
6648 case OMP_CLAUSE_DEPEND:
6649 t = OMP_CLAUSE_DECL (c);
6650 if (t == NULL_TREE)
6652 gcc_assert (OMP_CLAUSE_DEPEND_KIND (c)
6653 == OMP_CLAUSE_DEPEND_SOURCE);
6654 break;
6656 if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SINK)
6658 if (cp_finish_omp_clause_depend_sink (c))
6659 remove = true;
6660 break;
6662 if (TREE_CODE (t) == TREE_LIST)
6664 if (handle_omp_array_sections (c, ort))
6665 remove = true;
6666 break;
6668 if (t == error_mark_node)
6669 remove = true;
6670 else if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
6672 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
6673 break;
6674 if (DECL_P (t))
6675 error ("%qD is not a variable in %<depend%> clause", t);
6676 else
6677 error ("%qE is not a variable in %<depend%> clause", t);
6678 remove = true;
6680 else if (t == current_class_ptr)
6682 error ("%<this%> allowed in OpenMP only in %<declare simd%>"
6683 " clauses");
6684 remove = true;
6686 else if (!processing_template_decl
6687 && !cxx_mark_addressable (t))
6688 remove = true;
6689 break;
6691 case OMP_CLAUSE_MAP:
6692 case OMP_CLAUSE_TO:
6693 case OMP_CLAUSE_FROM:
6694 case OMP_CLAUSE__CACHE_:
6695 t = OMP_CLAUSE_DECL (c);
6696 if (TREE_CODE (t) == TREE_LIST)
6698 if (handle_omp_array_sections (c, ort))
6699 remove = true;
6700 else
6702 t = OMP_CLAUSE_DECL (c);
6703 if (TREE_CODE (t) != TREE_LIST
6704 && !type_dependent_expression_p (t)
6705 && !cp_omp_mappable_type (TREE_TYPE (t)))
6707 error_at (OMP_CLAUSE_LOCATION (c),
6708 "array section does not have mappable type "
6709 "in %qs clause",
6710 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6711 remove = true;
6713 while (TREE_CODE (t) == ARRAY_REF)
6714 t = TREE_OPERAND (t, 0);
6715 if (TREE_CODE (t) == COMPONENT_REF
6716 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
6718 while (TREE_CODE (t) == COMPONENT_REF)
6719 t = TREE_OPERAND (t, 0);
6720 if (REFERENCE_REF_P (t))
6721 t = TREE_OPERAND (t, 0);
6722 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
6723 break;
6724 if (bitmap_bit_p (&map_head, DECL_UID (t)))
6726 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
6727 error ("%qD appears more than once in motion"
6728 " clauses", t);
6729 else if (ort == C_ORT_ACC)
6730 error ("%qD appears more than once in data"
6731 " clauses", t);
6732 else
6733 error ("%qD appears more than once in map"
6734 " clauses", t);
6735 remove = true;
6737 else
6739 bitmap_set_bit (&map_head, DECL_UID (t));
6740 bitmap_set_bit (&map_field_head, DECL_UID (t));
6744 break;
6746 if (t == error_mark_node)
6748 remove = true;
6749 break;
6751 if (REFERENCE_REF_P (t)
6752 && TREE_CODE (TREE_OPERAND (t, 0)) == COMPONENT_REF)
6754 t = TREE_OPERAND (t, 0);
6755 OMP_CLAUSE_DECL (c) = t;
6757 if (TREE_CODE (t) == COMPONENT_REF
6758 && (ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP
6759 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE__CACHE_)
6761 if (type_dependent_expression_p (t))
6762 break;
6763 if (TREE_CODE (TREE_OPERAND (t, 1)) == FIELD_DECL
6764 && DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
6766 error_at (OMP_CLAUSE_LOCATION (c),
6767 "bit-field %qE in %qs clause",
6768 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6769 remove = true;
6771 else if (!cp_omp_mappable_type (TREE_TYPE (t)))
6773 error_at (OMP_CLAUSE_LOCATION (c),
6774 "%qE does not have a mappable type in %qs clause",
6775 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6776 remove = true;
6778 while (TREE_CODE (t) == COMPONENT_REF)
6780 if (TREE_TYPE (TREE_OPERAND (t, 0))
6781 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0)))
6782 == UNION_TYPE))
6784 error_at (OMP_CLAUSE_LOCATION (c),
6785 "%qE is a member of a union", t);
6786 remove = true;
6787 break;
6789 t = TREE_OPERAND (t, 0);
6791 if (remove)
6792 break;
6793 if (REFERENCE_REF_P (t))
6794 t = TREE_OPERAND (t, 0);
6795 if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
6797 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
6798 goto handle_map_references;
6801 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
6803 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
6804 break;
6805 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
6806 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
6807 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ALWAYS_POINTER))
6808 break;
6809 if (DECL_P (t))
6810 error ("%qD is not a variable in %qs clause", t,
6811 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6812 else
6813 error ("%qE is not a variable in %qs clause", t,
6814 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6815 remove = true;
6817 else if (VAR_P (t) && CP_DECL_THREAD_LOCAL_P (t))
6819 error ("%qD is threadprivate variable in %qs clause", t,
6820 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6821 remove = true;
6823 else if (ort != C_ORT_ACC && t == current_class_ptr)
6825 error ("%<this%> allowed in OpenMP only in %<declare simd%>"
6826 " clauses");
6827 remove = true;
6828 break;
6830 else if (!processing_template_decl
6831 && TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE
6832 && (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
6833 || (OMP_CLAUSE_MAP_KIND (c)
6834 != GOMP_MAP_FIRSTPRIVATE_POINTER))
6835 && !cxx_mark_addressable (t))
6836 remove = true;
6837 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
6838 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
6839 || (OMP_CLAUSE_MAP_KIND (c)
6840 == GOMP_MAP_FIRSTPRIVATE_POINTER)))
6841 && t == OMP_CLAUSE_DECL (c)
6842 && !type_dependent_expression_p (t)
6843 && !cp_omp_mappable_type ((TREE_CODE (TREE_TYPE (t))
6844 == REFERENCE_TYPE)
6845 ? TREE_TYPE (TREE_TYPE (t))
6846 : TREE_TYPE (t)))
6848 error_at (OMP_CLAUSE_LOCATION (c),
6849 "%qD does not have a mappable type in %qs clause", t,
6850 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6851 remove = true;
6853 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
6854 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FORCE_DEVICEPTR
6855 && !type_dependent_expression_p (t)
6856 && !POINTER_TYPE_P (TREE_TYPE (t)))
6858 error ("%qD is not a pointer variable", t);
6859 remove = true;
6861 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
6862 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
6864 if (bitmap_bit_p (&generic_head, DECL_UID (t))
6865 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
6867 error ("%qD appears more than once in data clauses", t);
6868 remove = true;
6870 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
6872 if (ort == C_ORT_ACC)
6873 error ("%qD appears more than once in data clauses", t);
6874 else
6875 error ("%qD appears both in data and map clauses", t);
6876 remove = true;
6878 else
6879 bitmap_set_bit (&generic_head, DECL_UID (t));
6881 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
6883 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
6884 error ("%qD appears more than once in motion clauses", t);
6885 if (ort == C_ORT_ACC)
6886 error ("%qD appears more than once in data clauses", t);
6887 else
6888 error ("%qD appears more than once in map clauses", t);
6889 remove = true;
6891 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
6892 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
6894 if (ort == C_ORT_ACC)
6895 error ("%qD appears more than once in data clauses", t);
6896 else
6897 error ("%qD appears both in data and map clauses", t);
6898 remove = true;
6900 else
6902 bitmap_set_bit (&map_head, DECL_UID (t));
6903 if (t != OMP_CLAUSE_DECL (c)
6904 && TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPONENT_REF)
6905 bitmap_set_bit (&map_field_head, DECL_UID (t));
6907 handle_map_references:
6908 if (!remove
6909 && !processing_template_decl
6910 && (ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP
6911 && TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c))) == REFERENCE_TYPE)
6913 t = OMP_CLAUSE_DECL (c);
6914 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
6916 OMP_CLAUSE_DECL (c) = build_simple_mem_ref (t);
6917 if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
6918 OMP_CLAUSE_SIZE (c)
6919 = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t)));
6921 else if (OMP_CLAUSE_MAP_KIND (c)
6922 != GOMP_MAP_FIRSTPRIVATE_POINTER
6923 && (OMP_CLAUSE_MAP_KIND (c)
6924 != GOMP_MAP_FIRSTPRIVATE_REFERENCE)
6925 && (OMP_CLAUSE_MAP_KIND (c)
6926 != GOMP_MAP_ALWAYS_POINTER))
6928 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
6929 OMP_CLAUSE_MAP);
6930 if (TREE_CODE (t) == COMPONENT_REF)
6931 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ALWAYS_POINTER);
6932 else
6933 OMP_CLAUSE_SET_MAP_KIND (c2,
6934 GOMP_MAP_FIRSTPRIVATE_REFERENCE);
6935 OMP_CLAUSE_DECL (c2) = t;
6936 OMP_CLAUSE_SIZE (c2) = size_zero_node;
6937 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
6938 OMP_CLAUSE_CHAIN (c) = c2;
6939 OMP_CLAUSE_DECL (c) = build_simple_mem_ref (t);
6940 if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
6941 OMP_CLAUSE_SIZE (c)
6942 = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t)));
6943 c = c2;
6946 break;
6948 case OMP_CLAUSE_TO_DECLARE:
6949 case OMP_CLAUSE_LINK:
6950 t = OMP_CLAUSE_DECL (c);
6951 if (TREE_CODE (t) == FUNCTION_DECL
6952 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
6954 else if (!VAR_P (t))
6956 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
6958 if (TREE_CODE (t) == TEMPLATE_ID_EXPR)
6959 error_at (OMP_CLAUSE_LOCATION (c),
6960 "template %qE in clause %qs", t,
6961 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6962 else if (really_overloaded_fn (t))
6963 error_at (OMP_CLAUSE_LOCATION (c),
6964 "overloaded function name %qE in clause %qs", t,
6965 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6966 else
6967 error_at (OMP_CLAUSE_LOCATION (c),
6968 "%qE is neither a variable nor a function name "
6969 "in clause %qs", t,
6970 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6972 else
6973 error_at (OMP_CLAUSE_LOCATION (c),
6974 "%qE is not a variable in clause %qs", t,
6975 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6976 remove = true;
6978 else if (DECL_THREAD_LOCAL_P (t))
6980 error_at (OMP_CLAUSE_LOCATION (c),
6981 "%qD is threadprivate variable in %qs clause", t,
6982 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6983 remove = true;
6985 else if (!cp_omp_mappable_type (TREE_TYPE (t)))
6987 error_at (OMP_CLAUSE_LOCATION (c),
6988 "%qD does not have a mappable type in %qs clause", t,
6989 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6990 remove = true;
6992 if (remove)
6993 break;
6994 if (bitmap_bit_p (&generic_head, DECL_UID (t)))
6996 error_at (OMP_CLAUSE_LOCATION (c),
6997 "%qE appears more than once on the same "
6998 "%<declare target%> directive", t);
6999 remove = true;
7001 else
7002 bitmap_set_bit (&generic_head, DECL_UID (t));
7003 break;
7005 case OMP_CLAUSE_UNIFORM:
7006 t = OMP_CLAUSE_DECL (c);
7007 if (TREE_CODE (t) != PARM_DECL)
7009 if (processing_template_decl)
7010 break;
7011 if (DECL_P (t))
7012 error ("%qD is not an argument in %<uniform%> clause", t);
7013 else
7014 error ("%qE is not an argument in %<uniform%> clause", t);
7015 remove = true;
7016 break;
7018 /* map_head bitmap is used as uniform_head if declare_simd. */
7019 bitmap_set_bit (&map_head, DECL_UID (t));
7020 goto check_dup_generic;
7022 case OMP_CLAUSE_GRAINSIZE:
7023 t = OMP_CLAUSE_GRAINSIZE_EXPR (c);
7024 if (t == error_mark_node)
7025 remove = true;
7026 else if (!type_dependent_expression_p (t)
7027 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
7029 error ("%<grainsize%> expression must be integral");
7030 remove = true;
7032 else
7034 t = mark_rvalue_use (t);
7035 if (!processing_template_decl)
7037 t = maybe_constant_value (t);
7038 if (TREE_CODE (t) == INTEGER_CST
7039 && tree_int_cst_sgn (t) != 1)
7041 warning_at (OMP_CLAUSE_LOCATION (c), 0,
7042 "%<grainsize%> value must be positive");
7043 t = integer_one_node;
7045 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7047 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
7049 break;
7051 case OMP_CLAUSE_PRIORITY:
7052 t = OMP_CLAUSE_PRIORITY_EXPR (c);
7053 if (t == error_mark_node)
7054 remove = true;
7055 else if (!type_dependent_expression_p (t)
7056 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
7058 error ("%<priority%> expression must be integral");
7059 remove = true;
7061 else
7063 t = mark_rvalue_use (t);
7064 if (!processing_template_decl)
7066 t = maybe_constant_value (t);
7067 if (TREE_CODE (t) == INTEGER_CST
7068 && tree_int_cst_sgn (t) == -1)
7070 warning_at (OMP_CLAUSE_LOCATION (c), 0,
7071 "%<priority%> value must be non-negative");
7072 t = integer_one_node;
7074 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7076 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
7078 break;
7080 case OMP_CLAUSE_HINT:
7081 t = OMP_CLAUSE_HINT_EXPR (c);
7082 if (t == error_mark_node)
7083 remove = true;
7084 else if (!type_dependent_expression_p (t)
7085 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
7087 error ("%<num_tasks%> expression must be integral");
7088 remove = true;
7090 else
7092 t = mark_rvalue_use (t);
7093 if (!processing_template_decl)
7095 t = maybe_constant_value (t);
7096 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7098 OMP_CLAUSE_HINT_EXPR (c) = t;
7100 break;
7102 case OMP_CLAUSE_IS_DEVICE_PTR:
7103 case OMP_CLAUSE_USE_DEVICE_PTR:
7104 field_ok = (ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP;
7105 t = OMP_CLAUSE_DECL (c);
7106 if (!type_dependent_expression_p (t))
7108 tree type = TREE_TYPE (t);
7109 if (TREE_CODE (type) != POINTER_TYPE
7110 && TREE_CODE (type) != ARRAY_TYPE
7111 && (TREE_CODE (type) != REFERENCE_TYPE
7112 || (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
7113 && TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE)))
7115 error_at (OMP_CLAUSE_LOCATION (c),
7116 "%qs variable is neither a pointer, nor an array "
7117 "nor reference to pointer or array",
7118 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7119 remove = true;
7122 goto check_dup_generic;
7124 case OMP_CLAUSE_NOWAIT:
7125 case OMP_CLAUSE_DEFAULT:
7126 case OMP_CLAUSE_UNTIED:
7127 case OMP_CLAUSE_COLLAPSE:
7128 case OMP_CLAUSE_MERGEABLE:
7129 case OMP_CLAUSE_PARALLEL:
7130 case OMP_CLAUSE_FOR:
7131 case OMP_CLAUSE_SECTIONS:
7132 case OMP_CLAUSE_TASKGROUP:
7133 case OMP_CLAUSE_PROC_BIND:
7134 case OMP_CLAUSE_NOGROUP:
7135 case OMP_CLAUSE_THREADS:
7136 case OMP_CLAUSE_SIMD:
7137 case OMP_CLAUSE_DEFAULTMAP:
7138 case OMP_CLAUSE__CILK_FOR_COUNT_:
7139 case OMP_CLAUSE_AUTO:
7140 case OMP_CLAUSE_INDEPENDENT:
7141 case OMP_CLAUSE_SEQ:
7142 break;
7144 case OMP_CLAUSE_TILE:
7145 for (tree list = OMP_CLAUSE_TILE_LIST (c); !remove && list;
7146 list = TREE_CHAIN (list))
7148 t = TREE_VALUE (list);
7150 if (t == error_mark_node)
7151 remove = true;
7152 else if (!type_dependent_expression_p (t)
7153 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
7155 error_at (OMP_CLAUSE_LOCATION (c),
7156 "%<tile%> argument needs integral type");
7157 remove = true;
7159 else
7161 t = mark_rvalue_use (t);
7162 if (!processing_template_decl)
7164 /* Zero is used to indicate '*', we permit you
7165 to get there via an ICE of value zero. */
7166 t = maybe_constant_value (t);
7167 if (!tree_fits_shwi_p (t)
7168 || tree_to_shwi (t) < 0)
7170 error_at (OMP_CLAUSE_LOCATION (c),
7171 "%<tile%> argument needs positive "
7172 "integral constant");
7173 remove = true;
7176 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7179 /* Update list item. */
7180 TREE_VALUE (list) = t;
7182 break;
7184 case OMP_CLAUSE_ORDERED:
7185 ordered_seen = true;
7186 break;
7188 case OMP_CLAUSE_INBRANCH:
7189 case OMP_CLAUSE_NOTINBRANCH:
7190 if (branch_seen)
7192 error ("%<inbranch%> clause is incompatible with "
7193 "%<notinbranch%>");
7194 remove = true;
7196 branch_seen = true;
7197 break;
7199 default:
7200 gcc_unreachable ();
7203 if (remove)
7204 *pc = OMP_CLAUSE_CHAIN (c);
7205 else
7206 pc = &OMP_CLAUSE_CHAIN (c);
7209 for (pc = &clauses, c = clauses; c ; c = *pc)
7211 enum omp_clause_code c_kind = OMP_CLAUSE_CODE (c);
7212 bool remove = false;
7213 bool need_complete_type = false;
7214 bool need_default_ctor = false;
7215 bool need_copy_ctor = false;
7216 bool need_copy_assignment = false;
7217 bool need_implicitly_determined = false;
7218 bool need_dtor = false;
7219 tree type, inner_type;
7221 switch (c_kind)
7223 case OMP_CLAUSE_SHARED:
7224 need_implicitly_determined = true;
7225 break;
7226 case OMP_CLAUSE_PRIVATE:
7227 need_complete_type = true;
7228 need_default_ctor = true;
7229 need_dtor = true;
7230 need_implicitly_determined = true;
7231 break;
7232 case OMP_CLAUSE_FIRSTPRIVATE:
7233 need_complete_type = true;
7234 need_copy_ctor = true;
7235 need_dtor = true;
7236 need_implicitly_determined = true;
7237 break;
7238 case OMP_CLAUSE_LASTPRIVATE:
7239 need_complete_type = true;
7240 need_copy_assignment = true;
7241 need_implicitly_determined = true;
7242 break;
7243 case OMP_CLAUSE_REDUCTION:
7244 need_implicitly_determined = true;
7245 break;
7246 case OMP_CLAUSE_LINEAR:
7247 if (ort != C_ORT_OMP_DECLARE_SIMD)
7248 need_implicitly_determined = true;
7249 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c)
7250 && !bitmap_bit_p (&map_head,
7251 DECL_UID (OMP_CLAUSE_LINEAR_STEP (c))))
7253 error_at (OMP_CLAUSE_LOCATION (c),
7254 "%<linear%> clause step is a parameter %qD not "
7255 "specified in %<uniform%> clause",
7256 OMP_CLAUSE_LINEAR_STEP (c));
7257 *pc = OMP_CLAUSE_CHAIN (c);
7258 continue;
7260 break;
7261 case OMP_CLAUSE_COPYPRIVATE:
7262 need_copy_assignment = true;
7263 break;
7264 case OMP_CLAUSE_COPYIN:
7265 need_copy_assignment = true;
7266 break;
7267 case OMP_CLAUSE_SIMDLEN:
7268 if (safelen
7269 && !processing_template_decl
7270 && tree_int_cst_lt (OMP_CLAUSE_SAFELEN_EXPR (safelen),
7271 OMP_CLAUSE_SIMDLEN_EXPR (c)))
7273 error_at (OMP_CLAUSE_LOCATION (c),
7274 "%<simdlen%> clause value is bigger than "
7275 "%<safelen%> clause value");
7276 OMP_CLAUSE_SIMDLEN_EXPR (c)
7277 = OMP_CLAUSE_SAFELEN_EXPR (safelen);
7279 pc = &OMP_CLAUSE_CHAIN (c);
7280 continue;
7281 case OMP_CLAUSE_SCHEDULE:
7282 if (ordered_seen
7283 && (OMP_CLAUSE_SCHEDULE_KIND (c)
7284 & OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
7286 error_at (OMP_CLAUSE_LOCATION (c),
7287 "%<nonmonotonic%> schedule modifier specified "
7288 "together with %<ordered%> clause");
7289 OMP_CLAUSE_SCHEDULE_KIND (c)
7290 = (enum omp_clause_schedule_kind)
7291 (OMP_CLAUSE_SCHEDULE_KIND (c)
7292 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
7294 pc = &OMP_CLAUSE_CHAIN (c);
7295 continue;
7296 case OMP_CLAUSE_NOWAIT:
7297 if (copyprivate_seen)
7299 error_at (OMP_CLAUSE_LOCATION (c),
7300 "%<nowait%> clause must not be used together "
7301 "with %<copyprivate%>");
7302 *pc = OMP_CLAUSE_CHAIN (c);
7303 continue;
7305 /* FALLTHRU */
7306 default:
7307 pc = &OMP_CLAUSE_CHAIN (c);
7308 continue;
7311 t = OMP_CLAUSE_DECL (c);
7312 if (processing_template_decl
7313 && !VAR_P (t) && TREE_CODE (t) != PARM_DECL)
7315 pc = &OMP_CLAUSE_CHAIN (c);
7316 continue;
7319 switch (c_kind)
7321 case OMP_CLAUSE_LASTPRIVATE:
7322 if (!bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
7324 need_default_ctor = true;
7325 need_dtor = true;
7327 break;
7329 case OMP_CLAUSE_REDUCTION:
7330 if (finish_omp_reduction_clause (c, &need_default_ctor,
7331 &need_dtor))
7332 remove = true;
7333 else
7334 t = OMP_CLAUSE_DECL (c);
7335 break;
7337 case OMP_CLAUSE_COPYIN:
7338 if (!VAR_P (t) || !CP_DECL_THREAD_LOCAL_P (t))
7340 error ("%qE must be %<threadprivate%> for %<copyin%>", t);
7341 remove = true;
7343 break;
7345 default:
7346 break;
7349 if (need_complete_type || need_copy_assignment)
7351 t = require_complete_type (t);
7352 if (t == error_mark_node)
7353 remove = true;
7354 else if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE
7355 && !complete_type_or_else (TREE_TYPE (TREE_TYPE (t)), t))
7356 remove = true;
7358 if (need_implicitly_determined)
7360 const char *share_name = NULL;
7362 if (VAR_P (t) && CP_DECL_THREAD_LOCAL_P (t))
7363 share_name = "threadprivate";
7364 else switch (cxx_omp_predetermined_sharing (t))
7366 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
7367 break;
7368 case OMP_CLAUSE_DEFAULT_SHARED:
7369 /* const vars may be specified in firstprivate clause. */
7370 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
7371 && cxx_omp_const_qual_no_mutable (t))
7372 break;
7373 share_name = "shared";
7374 break;
7375 case OMP_CLAUSE_DEFAULT_PRIVATE:
7376 share_name = "private";
7377 break;
7378 default:
7379 gcc_unreachable ();
7381 if (share_name)
7383 error ("%qE is predetermined %qs for %qs",
7384 omp_clause_printable_decl (t), share_name,
7385 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7386 remove = true;
7390 /* We're interested in the base element, not arrays. */
7391 inner_type = type = TREE_TYPE (t);
7392 if ((need_complete_type
7393 || need_copy_assignment
7394 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
7395 && TREE_CODE (inner_type) == REFERENCE_TYPE)
7396 inner_type = TREE_TYPE (inner_type);
7397 while (TREE_CODE (inner_type) == ARRAY_TYPE)
7398 inner_type = TREE_TYPE (inner_type);
7400 /* Check for special function availability by building a call to one.
7401 Save the results, because later we won't be in the right context
7402 for making these queries. */
7403 if (CLASS_TYPE_P (inner_type)
7404 && COMPLETE_TYPE_P (inner_type)
7405 && (need_default_ctor || need_copy_ctor
7406 || need_copy_assignment || need_dtor)
7407 && !type_dependent_expression_p (t)
7408 && cxx_omp_create_clause_info (c, inner_type, need_default_ctor,
7409 need_copy_ctor, need_copy_assignment,
7410 need_dtor))
7411 remove = true;
7413 if (!remove
7414 && c_kind == OMP_CLAUSE_SHARED
7415 && processing_template_decl)
7417 t = omp_clause_decl_field (OMP_CLAUSE_DECL (c));
7418 if (t)
7419 OMP_CLAUSE_DECL (c) = t;
7422 if (remove)
7423 *pc = OMP_CLAUSE_CHAIN (c);
7424 else
7425 pc = &OMP_CLAUSE_CHAIN (c);
7428 bitmap_obstack_release (NULL);
7429 return clauses;
7432 /* Start processing OpenMP clauses that can include any
7433 privatization clauses for non-static data members. */
7435 tree
7436 push_omp_privatization_clauses (bool ignore_next)
7438 if (omp_private_member_ignore_next)
7440 omp_private_member_ignore_next = ignore_next;
7441 return NULL_TREE;
7443 omp_private_member_ignore_next = ignore_next;
7444 if (omp_private_member_map)
7445 omp_private_member_vec.safe_push (error_mark_node);
7446 return push_stmt_list ();
7449 /* Revert remapping of any non-static data members since
7450 the last push_omp_privatization_clauses () call. */
7452 void
7453 pop_omp_privatization_clauses (tree stmt)
7455 if (stmt == NULL_TREE)
7456 return;
7457 stmt = pop_stmt_list (stmt);
7458 if (omp_private_member_map)
7460 while (!omp_private_member_vec.is_empty ())
7462 tree t = omp_private_member_vec.pop ();
7463 if (t == error_mark_node)
7465 add_stmt (stmt);
7466 return;
7468 bool no_decl_expr = t == integer_zero_node;
7469 if (no_decl_expr)
7470 t = omp_private_member_vec.pop ();
7471 tree *v = omp_private_member_map->get (t);
7472 gcc_assert (v);
7473 if (!no_decl_expr)
7474 add_decl_expr (*v);
7475 omp_private_member_map->remove (t);
7477 delete omp_private_member_map;
7478 omp_private_member_map = NULL;
7480 add_stmt (stmt);
7483 /* Remember OpenMP privatization clauses mapping and clear it.
7484 Used for lambdas. */
7486 void
7487 save_omp_privatization_clauses (vec<tree> &save)
7489 save = vNULL;
7490 if (omp_private_member_ignore_next)
7491 save.safe_push (integer_one_node);
7492 omp_private_member_ignore_next = false;
7493 if (!omp_private_member_map)
7494 return;
7496 while (!omp_private_member_vec.is_empty ())
7498 tree t = omp_private_member_vec.pop ();
7499 if (t == error_mark_node)
7501 save.safe_push (t);
7502 continue;
7504 tree n = t;
7505 if (t == integer_zero_node)
7506 t = omp_private_member_vec.pop ();
7507 tree *v = omp_private_member_map->get (t);
7508 gcc_assert (v);
7509 save.safe_push (*v);
7510 save.safe_push (t);
7511 if (n != t)
7512 save.safe_push (n);
7514 delete omp_private_member_map;
7515 omp_private_member_map = NULL;
7518 /* Restore OpenMP privatization clauses mapping saved by the
7519 above function. */
7521 void
7522 restore_omp_privatization_clauses (vec<tree> &save)
7524 gcc_assert (omp_private_member_vec.is_empty ());
7525 omp_private_member_ignore_next = false;
7526 if (save.is_empty ())
7527 return;
7528 if (save.length () == 1 && save[0] == integer_one_node)
7530 omp_private_member_ignore_next = true;
7531 save.release ();
7532 return;
7535 omp_private_member_map = new hash_map <tree, tree>;
7536 while (!save.is_empty ())
7538 tree t = save.pop ();
7539 tree n = t;
7540 if (t != error_mark_node)
7542 if (t == integer_one_node)
7544 omp_private_member_ignore_next = true;
7545 gcc_assert (save.is_empty ());
7546 break;
7548 if (t == integer_zero_node)
7549 t = save.pop ();
7550 tree &v = omp_private_member_map->get_or_insert (t);
7551 v = save.pop ();
7553 omp_private_member_vec.safe_push (t);
7554 if (n != t)
7555 omp_private_member_vec.safe_push (n);
7557 save.release ();
7560 /* For all variables in the tree_list VARS, mark them as thread local. */
7562 void
7563 finish_omp_threadprivate (tree vars)
7565 tree t;
7567 /* Mark every variable in VARS to be assigned thread local storage. */
7568 for (t = vars; t; t = TREE_CHAIN (t))
7570 tree v = TREE_PURPOSE (t);
7572 if (error_operand_p (v))
7574 else if (!VAR_P (v))
7575 error ("%<threadprivate%> %qD is not file, namespace "
7576 "or block scope variable", v);
7577 /* If V had already been marked threadprivate, it doesn't matter
7578 whether it had been used prior to this point. */
7579 else if (TREE_USED (v)
7580 && (DECL_LANG_SPECIFIC (v) == NULL
7581 || !CP_DECL_THREADPRIVATE_P (v)))
7582 error ("%qE declared %<threadprivate%> after first use", v);
7583 else if (! TREE_STATIC (v) && ! DECL_EXTERNAL (v))
7584 error ("automatic variable %qE cannot be %<threadprivate%>", v);
7585 else if (! COMPLETE_TYPE_P (complete_type (TREE_TYPE (v))))
7586 error ("%<threadprivate%> %qE has incomplete type", v);
7587 else if (TREE_STATIC (v) && TYPE_P (CP_DECL_CONTEXT (v))
7588 && CP_DECL_CONTEXT (v) != current_class_type)
7589 error ("%<threadprivate%> %qE directive not "
7590 "in %qT definition", v, CP_DECL_CONTEXT (v));
7591 else
7593 /* Allocate a LANG_SPECIFIC structure for V, if needed. */
7594 if (DECL_LANG_SPECIFIC (v) == NULL)
7596 retrofit_lang_decl (v);
7598 /* Make sure that DECL_DISCRIMINATOR_P continues to be true
7599 after the allocation of the lang_decl structure. */
7600 if (DECL_DISCRIMINATOR_P (v))
7601 DECL_LANG_SPECIFIC (v)->u.base.u2sel = 1;
7604 if (! CP_DECL_THREAD_LOCAL_P (v))
7606 CP_DECL_THREAD_LOCAL_P (v) = true;
7607 set_decl_tls_model (v, decl_default_tls_model (v));
7608 /* If rtl has been already set for this var, call
7609 make_decl_rtl once again, so that encode_section_info
7610 has a chance to look at the new decl flags. */
7611 if (DECL_RTL_SET_P (v))
7612 make_decl_rtl (v);
7614 CP_DECL_THREADPRIVATE_P (v) = 1;
7619 /* Build an OpenMP structured block. */
7621 tree
7622 begin_omp_structured_block (void)
7624 return do_pushlevel (sk_omp);
7627 tree
7628 finish_omp_structured_block (tree block)
7630 return do_poplevel (block);
7633 /* Similarly, except force the retention of the BLOCK. */
7635 tree
7636 begin_omp_parallel (void)
7638 keep_next_level (true);
7639 return begin_omp_structured_block ();
7642 /* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
7643 statement. */
7645 tree
7646 finish_oacc_data (tree clauses, tree block)
7648 tree stmt;
7650 block = finish_omp_structured_block (block);
7652 stmt = make_node (OACC_DATA);
7653 TREE_TYPE (stmt) = void_type_node;
7654 OACC_DATA_CLAUSES (stmt) = clauses;
7655 OACC_DATA_BODY (stmt) = block;
7657 return add_stmt (stmt);
7660 /* Generate OACC_HOST_DATA, with CLAUSES and BLOCK as its compound
7661 statement. */
7663 tree
7664 finish_oacc_host_data (tree clauses, tree block)
7666 tree stmt;
7668 block = finish_omp_structured_block (block);
7670 stmt = make_node (OACC_HOST_DATA);
7671 TREE_TYPE (stmt) = void_type_node;
7672 OACC_HOST_DATA_CLAUSES (stmt) = clauses;
7673 OACC_HOST_DATA_BODY (stmt) = block;
7675 return add_stmt (stmt);
7678 /* Generate OMP construct CODE, with BODY and CLAUSES as its compound
7679 statement. */
7681 tree
7682 finish_omp_construct (enum tree_code code, tree body, tree clauses)
7684 body = finish_omp_structured_block (body);
7686 tree stmt = make_node (code);
7687 TREE_TYPE (stmt) = void_type_node;
7688 OMP_BODY (stmt) = body;
7689 OMP_CLAUSES (stmt) = clauses;
7691 return add_stmt (stmt);
7694 tree
7695 finish_omp_parallel (tree clauses, tree body)
7697 tree stmt;
7699 body = finish_omp_structured_block (body);
7701 stmt = make_node (OMP_PARALLEL);
7702 TREE_TYPE (stmt) = void_type_node;
7703 OMP_PARALLEL_CLAUSES (stmt) = clauses;
7704 OMP_PARALLEL_BODY (stmt) = body;
7706 return add_stmt (stmt);
7709 tree
7710 begin_omp_task (void)
7712 keep_next_level (true);
7713 return begin_omp_structured_block ();
7716 tree
7717 finish_omp_task (tree clauses, tree body)
7719 tree stmt;
7721 body = finish_omp_structured_block (body);
7723 stmt = make_node (OMP_TASK);
7724 TREE_TYPE (stmt) = void_type_node;
7725 OMP_TASK_CLAUSES (stmt) = clauses;
7726 OMP_TASK_BODY (stmt) = body;
7728 return add_stmt (stmt);
7731 /* Helper function for finish_omp_for. Convert Ith random access iterator
7732 into integral iterator. Return FALSE if successful. */
7734 static bool
7735 handle_omp_for_class_iterator (int i, location_t locus, enum tree_code code,
7736 tree declv, tree orig_declv, tree initv,
7737 tree condv, tree incrv, tree *body,
7738 tree *pre_body, tree &clauses, tree *lastp,
7739 int collapse, int ordered)
7741 tree diff, iter_init, iter_incr = NULL, last;
7742 tree incr_var = NULL, orig_pre_body, orig_body, c;
7743 tree decl = TREE_VEC_ELT (declv, i);
7744 tree init = TREE_VEC_ELT (initv, i);
7745 tree cond = TREE_VEC_ELT (condv, i);
7746 tree incr = TREE_VEC_ELT (incrv, i);
7747 tree iter = decl;
7748 location_t elocus = locus;
7750 if (init && EXPR_HAS_LOCATION (init))
7751 elocus = EXPR_LOCATION (init);
7753 cond = cp_fully_fold (cond);
7754 switch (TREE_CODE (cond))
7756 case GT_EXPR:
7757 case GE_EXPR:
7758 case LT_EXPR:
7759 case LE_EXPR:
7760 case NE_EXPR:
7761 if (TREE_OPERAND (cond, 1) == iter)
7762 cond = build2 (swap_tree_comparison (TREE_CODE (cond)),
7763 TREE_TYPE (cond), iter, TREE_OPERAND (cond, 0));
7764 if (TREE_OPERAND (cond, 0) != iter)
7765 cond = error_mark_node;
7766 else
7768 tree tem = build_x_binary_op (EXPR_LOCATION (cond),
7769 TREE_CODE (cond),
7770 iter, ERROR_MARK,
7771 TREE_OPERAND (cond, 1), ERROR_MARK,
7772 NULL, tf_warning_or_error);
7773 if (error_operand_p (tem))
7774 return true;
7776 break;
7777 default:
7778 cond = error_mark_node;
7779 break;
7781 if (cond == error_mark_node)
7783 error_at (elocus, "invalid controlling predicate");
7784 return true;
7786 diff = build_x_binary_op (elocus, MINUS_EXPR, TREE_OPERAND (cond, 1),
7787 ERROR_MARK, iter, ERROR_MARK, NULL,
7788 tf_warning_or_error);
7789 diff = cp_fully_fold (diff);
7790 if (error_operand_p (diff))
7791 return true;
7792 if (TREE_CODE (TREE_TYPE (diff)) != INTEGER_TYPE)
7794 error_at (elocus, "difference between %qE and %qD does not have integer type",
7795 TREE_OPERAND (cond, 1), iter);
7796 return true;
7798 if (!c_omp_check_loop_iv_exprs (locus, orig_declv,
7799 TREE_VEC_ELT (declv, i), NULL_TREE,
7800 cond, cp_walk_subtrees))
7801 return true;
7803 switch (TREE_CODE (incr))
7805 case PREINCREMENT_EXPR:
7806 case PREDECREMENT_EXPR:
7807 case POSTINCREMENT_EXPR:
7808 case POSTDECREMENT_EXPR:
7809 if (TREE_OPERAND (incr, 0) != iter)
7811 incr = error_mark_node;
7812 break;
7814 iter_incr = build_x_unary_op (EXPR_LOCATION (incr),
7815 TREE_CODE (incr), iter,
7816 tf_warning_or_error);
7817 if (error_operand_p (iter_incr))
7818 return true;
7819 else if (TREE_CODE (incr) == PREINCREMENT_EXPR
7820 || TREE_CODE (incr) == POSTINCREMENT_EXPR)
7821 incr = integer_one_node;
7822 else
7823 incr = integer_minus_one_node;
7824 break;
7825 case MODIFY_EXPR:
7826 if (TREE_OPERAND (incr, 0) != iter)
7827 incr = error_mark_node;
7828 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
7829 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
7831 tree rhs = TREE_OPERAND (incr, 1);
7832 if (TREE_OPERAND (rhs, 0) == iter)
7834 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (rhs, 1)))
7835 != INTEGER_TYPE)
7836 incr = error_mark_node;
7837 else
7839 iter_incr = build_x_modify_expr (EXPR_LOCATION (rhs),
7840 iter, TREE_CODE (rhs),
7841 TREE_OPERAND (rhs, 1),
7842 tf_warning_or_error);
7843 if (error_operand_p (iter_incr))
7844 return true;
7845 incr = TREE_OPERAND (rhs, 1);
7846 incr = cp_convert (TREE_TYPE (diff), incr,
7847 tf_warning_or_error);
7848 if (TREE_CODE (rhs) == MINUS_EXPR)
7850 incr = build1 (NEGATE_EXPR, TREE_TYPE (diff), incr);
7851 incr = fold_simple (incr);
7853 if (TREE_CODE (incr) != INTEGER_CST
7854 && (TREE_CODE (incr) != NOP_EXPR
7855 || (TREE_CODE (TREE_OPERAND (incr, 0))
7856 != INTEGER_CST)))
7857 iter_incr = NULL;
7860 else if (TREE_OPERAND (rhs, 1) == iter)
7862 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (rhs, 0))) != INTEGER_TYPE
7863 || TREE_CODE (rhs) != PLUS_EXPR)
7864 incr = error_mark_node;
7865 else
7867 iter_incr = build_x_binary_op (EXPR_LOCATION (rhs),
7868 PLUS_EXPR,
7869 TREE_OPERAND (rhs, 0),
7870 ERROR_MARK, iter,
7871 ERROR_MARK, NULL,
7872 tf_warning_or_error);
7873 if (error_operand_p (iter_incr))
7874 return true;
7875 iter_incr = build_x_modify_expr (EXPR_LOCATION (rhs),
7876 iter, NOP_EXPR,
7877 iter_incr,
7878 tf_warning_or_error);
7879 if (error_operand_p (iter_incr))
7880 return true;
7881 incr = TREE_OPERAND (rhs, 0);
7882 iter_incr = NULL;
7885 else
7886 incr = error_mark_node;
7888 else
7889 incr = error_mark_node;
7890 break;
7891 default:
7892 incr = error_mark_node;
7893 break;
7896 if (incr == error_mark_node)
7898 error_at (elocus, "invalid increment expression");
7899 return true;
7902 incr = cp_convert (TREE_TYPE (diff), incr, tf_warning_or_error);
7903 bool taskloop_iv_seen = false;
7904 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
7905 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
7906 && OMP_CLAUSE_DECL (c) == iter)
7908 if (code == OMP_TASKLOOP)
7910 taskloop_iv_seen = true;
7911 OMP_CLAUSE_LASTPRIVATE_TASKLOOP_IV (c) = 1;
7913 break;
7915 else if (code == OMP_TASKLOOP
7916 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
7917 && OMP_CLAUSE_DECL (c) == iter)
7919 taskloop_iv_seen = true;
7920 OMP_CLAUSE_PRIVATE_TASKLOOP_IV (c) = 1;
7923 decl = create_temporary_var (TREE_TYPE (diff));
7924 pushdecl (decl);
7925 add_decl_expr (decl);
7926 last = create_temporary_var (TREE_TYPE (diff));
7927 pushdecl (last);
7928 add_decl_expr (last);
7929 if (c && iter_incr == NULL && TREE_CODE (incr) != INTEGER_CST
7930 && (!ordered || (i < collapse && collapse > 1)))
7932 incr_var = create_temporary_var (TREE_TYPE (diff));
7933 pushdecl (incr_var);
7934 add_decl_expr (incr_var);
7936 gcc_assert (stmts_are_full_exprs_p ());
7937 tree diffvar = NULL_TREE;
7938 if (code == OMP_TASKLOOP)
7940 if (!taskloop_iv_seen)
7942 tree ivc = build_omp_clause (locus, OMP_CLAUSE_FIRSTPRIVATE);
7943 OMP_CLAUSE_DECL (ivc) = iter;
7944 cxx_omp_finish_clause (ivc, NULL);
7945 OMP_CLAUSE_CHAIN (ivc) = clauses;
7946 clauses = ivc;
7948 tree lvc = build_omp_clause (locus, OMP_CLAUSE_FIRSTPRIVATE);
7949 OMP_CLAUSE_DECL (lvc) = last;
7950 OMP_CLAUSE_CHAIN (lvc) = clauses;
7951 clauses = lvc;
7952 diffvar = create_temporary_var (TREE_TYPE (diff));
7953 pushdecl (diffvar);
7954 add_decl_expr (diffvar);
7957 orig_pre_body = *pre_body;
7958 *pre_body = push_stmt_list ();
7959 if (orig_pre_body)
7960 add_stmt (orig_pre_body);
7961 if (init != NULL)
7962 finish_expr_stmt (build_x_modify_expr (elocus,
7963 iter, NOP_EXPR, init,
7964 tf_warning_or_error));
7965 init = build_int_cst (TREE_TYPE (diff), 0);
7966 if (c && iter_incr == NULL
7967 && (!ordered || (i < collapse && collapse > 1)))
7969 if (incr_var)
7971 finish_expr_stmt (build_x_modify_expr (elocus,
7972 incr_var, NOP_EXPR,
7973 incr, tf_warning_or_error));
7974 incr = incr_var;
7976 iter_incr = build_x_modify_expr (elocus,
7977 iter, PLUS_EXPR, incr,
7978 tf_warning_or_error);
7980 if (c && ordered && i < collapse && collapse > 1)
7981 iter_incr = incr;
7982 finish_expr_stmt (build_x_modify_expr (elocus,
7983 last, NOP_EXPR, init,
7984 tf_warning_or_error));
7985 if (diffvar)
7987 finish_expr_stmt (build_x_modify_expr (elocus,
7988 diffvar, NOP_EXPR,
7989 diff, tf_warning_or_error));
7990 diff = diffvar;
7992 *pre_body = pop_stmt_list (*pre_body);
7994 cond = cp_build_binary_op (elocus,
7995 TREE_CODE (cond), decl, diff,
7996 tf_warning_or_error);
7997 incr = build_modify_expr (elocus, decl, NULL_TREE, PLUS_EXPR,
7998 elocus, incr, NULL_TREE);
8000 orig_body = *body;
8001 *body = push_stmt_list ();
8002 iter_init = build2 (MINUS_EXPR, TREE_TYPE (diff), decl, last);
8003 iter_init = build_x_modify_expr (elocus,
8004 iter, PLUS_EXPR, iter_init,
8005 tf_warning_or_error);
8006 if (iter_init != error_mark_node)
8007 iter_init = build1 (NOP_EXPR, void_type_node, iter_init);
8008 finish_expr_stmt (iter_init);
8009 finish_expr_stmt (build_x_modify_expr (elocus,
8010 last, NOP_EXPR, decl,
8011 tf_warning_or_error));
8012 add_stmt (orig_body);
8013 *body = pop_stmt_list (*body);
8015 if (c)
8017 OMP_CLAUSE_LASTPRIVATE_STMT (c) = push_stmt_list ();
8018 if (!ordered)
8019 finish_expr_stmt (iter_incr);
8020 else
8022 iter_init = decl;
8023 if (i < collapse && collapse > 1 && !error_operand_p (iter_incr))
8024 iter_init = build2 (PLUS_EXPR, TREE_TYPE (diff),
8025 iter_init, iter_incr);
8026 iter_init = build2 (MINUS_EXPR, TREE_TYPE (diff), iter_init, last);
8027 iter_init = build_x_modify_expr (elocus,
8028 iter, PLUS_EXPR, iter_init,
8029 tf_warning_or_error);
8030 if (iter_init != error_mark_node)
8031 iter_init = build1 (NOP_EXPR, void_type_node, iter_init);
8032 finish_expr_stmt (iter_init);
8034 OMP_CLAUSE_LASTPRIVATE_STMT (c)
8035 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (c));
8038 TREE_VEC_ELT (declv, i) = decl;
8039 TREE_VEC_ELT (initv, i) = init;
8040 TREE_VEC_ELT (condv, i) = cond;
8041 TREE_VEC_ELT (incrv, i) = incr;
8042 *lastp = last;
8044 return false;
8047 /* Build and validate an OMP_FOR statement. CLAUSES, BODY, COND, INCR
8048 are directly for their associated operands in the statement. DECL
8049 and INIT are a combo; if DECL is NULL then INIT ought to be a
8050 MODIFY_EXPR, and the DECL should be extracted. PRE_BODY are
8051 optional statements that need to go before the loop into its
8052 sk_omp scope. */
8054 tree
8055 finish_omp_for (location_t locus, enum tree_code code, tree declv,
8056 tree orig_declv, tree initv, tree condv, tree incrv,
8057 tree body, tree pre_body, vec<tree> *orig_inits, tree clauses)
8059 tree omp_for = NULL, orig_incr = NULL;
8060 tree decl = NULL, init, cond, incr, orig_decl = NULL_TREE, block = NULL_TREE;
8061 tree last = NULL_TREE;
8062 location_t elocus;
8063 int i;
8064 int collapse = 1;
8065 int ordered = 0;
8067 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (initv));
8068 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (condv));
8069 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (incrv));
8070 if (TREE_VEC_LENGTH (declv) > 1)
8072 tree c;
8074 c = omp_find_clause (clauses, OMP_CLAUSE_TILE);
8075 if (c)
8076 collapse = list_length (OMP_CLAUSE_TILE_LIST (c));
8077 else
8079 c = omp_find_clause (clauses, OMP_CLAUSE_COLLAPSE);
8080 if (c)
8081 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (c));
8082 if (collapse != TREE_VEC_LENGTH (declv))
8083 ordered = TREE_VEC_LENGTH (declv);
8086 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
8088 decl = TREE_VEC_ELT (declv, i);
8089 init = TREE_VEC_ELT (initv, i);
8090 cond = TREE_VEC_ELT (condv, i);
8091 incr = TREE_VEC_ELT (incrv, i);
8092 elocus = locus;
8094 if (decl == NULL)
8096 if (init != NULL)
8097 switch (TREE_CODE (init))
8099 case MODIFY_EXPR:
8100 decl = TREE_OPERAND (init, 0);
8101 init = TREE_OPERAND (init, 1);
8102 break;
8103 case MODOP_EXPR:
8104 if (TREE_CODE (TREE_OPERAND (init, 1)) == NOP_EXPR)
8106 decl = TREE_OPERAND (init, 0);
8107 init = TREE_OPERAND (init, 2);
8109 break;
8110 default:
8111 break;
8114 if (decl == NULL)
8116 error_at (locus,
8117 "expected iteration declaration or initialization");
8118 return NULL;
8122 if (init && EXPR_HAS_LOCATION (init))
8123 elocus = EXPR_LOCATION (init);
8125 if (cond == NULL)
8127 error_at (elocus, "missing controlling predicate");
8128 return NULL;
8131 if (incr == NULL)
8133 error_at (elocus, "missing increment expression");
8134 return NULL;
8137 TREE_VEC_ELT (declv, i) = decl;
8138 TREE_VEC_ELT (initv, i) = init;
8141 if (orig_inits)
8143 bool fail = false;
8144 tree orig_init;
8145 FOR_EACH_VEC_ELT (*orig_inits, i, orig_init)
8146 if (orig_init
8147 && !c_omp_check_loop_iv_exprs (locus, declv,
8148 TREE_VEC_ELT (declv, i), orig_init,
8149 NULL_TREE, cp_walk_subtrees))
8150 fail = true;
8151 if (fail)
8152 return NULL;
8155 if (dependent_omp_for_p (declv, initv, condv, incrv))
8157 tree stmt;
8159 stmt = make_node (code);
8161 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
8163 /* This is really just a place-holder. We'll be decomposing this
8164 again and going through the cp_build_modify_expr path below when
8165 we instantiate the thing. */
8166 TREE_VEC_ELT (initv, i)
8167 = build2 (MODIFY_EXPR, void_type_node, TREE_VEC_ELT (declv, i),
8168 TREE_VEC_ELT (initv, i));
8171 TREE_TYPE (stmt) = void_type_node;
8172 OMP_FOR_INIT (stmt) = initv;
8173 OMP_FOR_COND (stmt) = condv;
8174 OMP_FOR_INCR (stmt) = incrv;
8175 OMP_FOR_BODY (stmt) = body;
8176 OMP_FOR_PRE_BODY (stmt) = pre_body;
8177 OMP_FOR_CLAUSES (stmt) = clauses;
8179 SET_EXPR_LOCATION (stmt, locus);
8180 return add_stmt (stmt);
8183 if (!orig_declv)
8184 orig_declv = copy_node (declv);
8186 if (processing_template_decl)
8187 orig_incr = make_tree_vec (TREE_VEC_LENGTH (incrv));
8189 for (i = 0; i < TREE_VEC_LENGTH (declv); )
8191 decl = TREE_VEC_ELT (declv, i);
8192 init = TREE_VEC_ELT (initv, i);
8193 cond = TREE_VEC_ELT (condv, i);
8194 incr = TREE_VEC_ELT (incrv, i);
8195 if (orig_incr)
8196 TREE_VEC_ELT (orig_incr, i) = incr;
8197 elocus = locus;
8199 if (init && EXPR_HAS_LOCATION (init))
8200 elocus = EXPR_LOCATION (init);
8202 if (!DECL_P (decl))
8204 error_at (elocus, "expected iteration declaration or initialization");
8205 return NULL;
8208 if (incr && TREE_CODE (incr) == MODOP_EXPR)
8210 if (orig_incr)
8211 TREE_VEC_ELT (orig_incr, i) = incr;
8212 incr = cp_build_modify_expr (elocus, TREE_OPERAND (incr, 0),
8213 TREE_CODE (TREE_OPERAND (incr, 1)),
8214 TREE_OPERAND (incr, 2),
8215 tf_warning_or_error);
8218 if (CLASS_TYPE_P (TREE_TYPE (decl)))
8220 if (code == OMP_SIMD)
8222 error_at (elocus, "%<#pragma omp simd%> used with class "
8223 "iteration variable %qE", decl);
8224 return NULL;
8226 if (code == CILK_FOR && i == 0)
8227 orig_decl = decl;
8228 if (handle_omp_for_class_iterator (i, locus, code, declv, orig_declv,
8229 initv, condv, incrv, &body,
8230 &pre_body, clauses, &last,
8231 collapse, ordered))
8232 return NULL;
8233 continue;
8236 if (!INTEGRAL_TYPE_P (TREE_TYPE (decl))
8237 && !TYPE_PTR_P (TREE_TYPE (decl)))
8239 error_at (elocus, "invalid type for iteration variable %qE", decl);
8240 return NULL;
8243 if (!processing_template_decl)
8245 init = fold_build_cleanup_point_expr (TREE_TYPE (init), init);
8246 init = cp_build_modify_expr (elocus, decl, NOP_EXPR, init,
8247 tf_warning_or_error);
8249 else
8250 init = build2 (MODIFY_EXPR, void_type_node, decl, init);
8251 if (cond
8252 && TREE_SIDE_EFFECTS (cond)
8253 && COMPARISON_CLASS_P (cond)
8254 && !processing_template_decl)
8256 tree t = TREE_OPERAND (cond, 0);
8257 if (TREE_SIDE_EFFECTS (t)
8258 && t != decl
8259 && (TREE_CODE (t) != NOP_EXPR
8260 || TREE_OPERAND (t, 0) != decl))
8261 TREE_OPERAND (cond, 0)
8262 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
8264 t = TREE_OPERAND (cond, 1);
8265 if (TREE_SIDE_EFFECTS (t)
8266 && t != decl
8267 && (TREE_CODE (t) != NOP_EXPR
8268 || TREE_OPERAND (t, 0) != decl))
8269 TREE_OPERAND (cond, 1)
8270 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
8272 if (decl == error_mark_node || init == error_mark_node)
8273 return NULL;
8275 TREE_VEC_ELT (declv, i) = decl;
8276 TREE_VEC_ELT (initv, i) = init;
8277 TREE_VEC_ELT (condv, i) = cond;
8278 TREE_VEC_ELT (incrv, i) = incr;
8279 i++;
8282 if (IS_EMPTY_STMT (pre_body))
8283 pre_body = NULL;
8285 if (code == CILK_FOR && !processing_template_decl)
8286 block = push_stmt_list ();
8288 omp_for = c_finish_omp_for (locus, code, declv, orig_declv, initv, condv,
8289 incrv, body, pre_body);
8291 /* Check for iterators appearing in lb, b or incr expressions. */
8292 if (omp_for && !c_omp_check_loop_iv (omp_for, orig_declv, cp_walk_subtrees))
8293 omp_for = NULL_TREE;
8295 if (omp_for == NULL)
8297 if (block)
8298 pop_stmt_list (block);
8299 return NULL;
8302 add_stmt (omp_for);
8304 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INCR (omp_for)); i++)
8306 decl = TREE_OPERAND (TREE_VEC_ELT (OMP_FOR_INIT (omp_for), i), 0);
8307 incr = TREE_VEC_ELT (OMP_FOR_INCR (omp_for), i);
8309 if (TREE_CODE (incr) != MODIFY_EXPR)
8310 continue;
8312 if (TREE_SIDE_EFFECTS (TREE_OPERAND (incr, 1))
8313 && BINARY_CLASS_P (TREE_OPERAND (incr, 1))
8314 && !processing_template_decl)
8316 tree t = TREE_OPERAND (TREE_OPERAND (incr, 1), 0);
8317 if (TREE_SIDE_EFFECTS (t)
8318 && t != decl
8319 && (TREE_CODE (t) != NOP_EXPR
8320 || TREE_OPERAND (t, 0) != decl))
8321 TREE_OPERAND (TREE_OPERAND (incr, 1), 0)
8322 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
8324 t = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
8325 if (TREE_SIDE_EFFECTS (t)
8326 && t != decl
8327 && (TREE_CODE (t) != NOP_EXPR
8328 || TREE_OPERAND (t, 0) != decl))
8329 TREE_OPERAND (TREE_OPERAND (incr, 1), 1)
8330 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
8333 if (orig_incr)
8334 TREE_VEC_ELT (OMP_FOR_INCR (omp_for), i) = TREE_VEC_ELT (orig_incr, i);
8336 OMP_FOR_CLAUSES (omp_for) = clauses;
8338 /* For simd loops with non-static data member iterators, we could have added
8339 OMP_CLAUSE_LINEAR clauses without OMP_CLAUSE_LINEAR_STEP. As we know the
8340 step at this point, fill it in. */
8341 if (code == OMP_SIMD && !processing_template_decl
8342 && TREE_VEC_LENGTH (OMP_FOR_INCR (omp_for)) == 1)
8343 for (tree c = omp_find_clause (clauses, OMP_CLAUSE_LINEAR); c;
8344 c = omp_find_clause (OMP_CLAUSE_CHAIN (c), OMP_CLAUSE_LINEAR))
8345 if (OMP_CLAUSE_LINEAR_STEP (c) == NULL_TREE)
8347 decl = TREE_OPERAND (TREE_VEC_ELT (OMP_FOR_INIT (omp_for), 0), 0);
8348 gcc_assert (decl == OMP_CLAUSE_DECL (c));
8349 incr = TREE_VEC_ELT (OMP_FOR_INCR (omp_for), 0);
8350 tree step, stept;
8351 switch (TREE_CODE (incr))
8353 case PREINCREMENT_EXPR:
8354 case POSTINCREMENT_EXPR:
8355 /* c_omp_for_incr_canonicalize_ptr() should have been
8356 called to massage things appropriately. */
8357 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (decl)));
8358 OMP_CLAUSE_LINEAR_STEP (c) = build_int_cst (TREE_TYPE (decl), 1);
8359 break;
8360 case PREDECREMENT_EXPR:
8361 case POSTDECREMENT_EXPR:
8362 /* c_omp_for_incr_canonicalize_ptr() should have been
8363 called to massage things appropriately. */
8364 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (decl)));
8365 OMP_CLAUSE_LINEAR_STEP (c)
8366 = build_int_cst (TREE_TYPE (decl), -1);
8367 break;
8368 case MODIFY_EXPR:
8369 gcc_assert (TREE_OPERAND (incr, 0) == decl);
8370 incr = TREE_OPERAND (incr, 1);
8371 switch (TREE_CODE (incr))
8373 case PLUS_EXPR:
8374 if (TREE_OPERAND (incr, 1) == decl)
8375 step = TREE_OPERAND (incr, 0);
8376 else
8377 step = TREE_OPERAND (incr, 1);
8378 break;
8379 case MINUS_EXPR:
8380 case POINTER_PLUS_EXPR:
8381 gcc_assert (TREE_OPERAND (incr, 0) == decl);
8382 step = TREE_OPERAND (incr, 1);
8383 break;
8384 default:
8385 gcc_unreachable ();
8387 stept = TREE_TYPE (decl);
8388 if (POINTER_TYPE_P (stept))
8389 stept = sizetype;
8390 step = fold_convert (stept, step);
8391 if (TREE_CODE (incr) == MINUS_EXPR)
8392 step = fold_build1 (NEGATE_EXPR, stept, step);
8393 OMP_CLAUSE_LINEAR_STEP (c) = step;
8394 break;
8395 default:
8396 gcc_unreachable ();
8400 if (block)
8402 tree omp_par = make_node (OMP_PARALLEL);
8403 TREE_TYPE (omp_par) = void_type_node;
8404 OMP_PARALLEL_CLAUSES (omp_par) = NULL_TREE;
8405 tree bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
8406 TREE_SIDE_EFFECTS (bind) = 1;
8407 BIND_EXPR_BODY (bind) = pop_stmt_list (block);
8408 OMP_PARALLEL_BODY (omp_par) = bind;
8409 if (OMP_FOR_PRE_BODY (omp_for))
8411 add_stmt (OMP_FOR_PRE_BODY (omp_for));
8412 OMP_FOR_PRE_BODY (omp_for) = NULL_TREE;
8414 init = TREE_VEC_ELT (OMP_FOR_INIT (omp_for), 0);
8415 decl = TREE_OPERAND (init, 0);
8416 cond = TREE_VEC_ELT (OMP_FOR_COND (omp_for), 0);
8417 incr = TREE_VEC_ELT (OMP_FOR_INCR (omp_for), 0);
8418 tree t = TREE_OPERAND (cond, 1), c, clauses, *pc;
8419 clauses = OMP_FOR_CLAUSES (omp_for);
8420 OMP_FOR_CLAUSES (omp_for) = NULL_TREE;
8421 for (pc = &clauses; *pc; )
8422 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_SCHEDULE)
8424 gcc_assert (OMP_FOR_CLAUSES (omp_for) == NULL_TREE);
8425 OMP_FOR_CLAUSES (omp_for) = *pc;
8426 *pc = OMP_CLAUSE_CHAIN (*pc);
8427 OMP_CLAUSE_CHAIN (OMP_FOR_CLAUSES (omp_for)) = NULL_TREE;
8429 else
8431 gcc_assert (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_FIRSTPRIVATE);
8432 pc = &OMP_CLAUSE_CHAIN (*pc);
8434 if (TREE_CODE (t) != INTEGER_CST)
8436 TREE_OPERAND (cond, 1) = get_temp_regvar (TREE_TYPE (t), t);
8437 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
8438 OMP_CLAUSE_DECL (c) = TREE_OPERAND (cond, 1);
8439 OMP_CLAUSE_CHAIN (c) = clauses;
8440 clauses = c;
8442 if (TREE_CODE (incr) == MODIFY_EXPR)
8444 t = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
8445 if (TREE_CODE (t) != INTEGER_CST)
8447 TREE_OPERAND (TREE_OPERAND (incr, 1), 1)
8448 = get_temp_regvar (TREE_TYPE (t), t);
8449 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
8450 OMP_CLAUSE_DECL (c) = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
8451 OMP_CLAUSE_CHAIN (c) = clauses;
8452 clauses = c;
8455 t = TREE_OPERAND (init, 1);
8456 if (TREE_CODE (t) != INTEGER_CST)
8458 TREE_OPERAND (init, 1) = get_temp_regvar (TREE_TYPE (t), t);
8459 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
8460 OMP_CLAUSE_DECL (c) = TREE_OPERAND (init, 1);
8461 OMP_CLAUSE_CHAIN (c) = clauses;
8462 clauses = c;
8464 if (orig_decl && orig_decl != decl)
8466 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
8467 OMP_CLAUSE_DECL (c) = orig_decl;
8468 OMP_CLAUSE_CHAIN (c) = clauses;
8469 clauses = c;
8471 if (last)
8473 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
8474 OMP_CLAUSE_DECL (c) = last;
8475 OMP_CLAUSE_CHAIN (c) = clauses;
8476 clauses = c;
8478 c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
8479 OMP_CLAUSE_DECL (c) = decl;
8480 OMP_CLAUSE_CHAIN (c) = clauses;
8481 clauses = c;
8482 c = build_omp_clause (input_location, OMP_CLAUSE__CILK_FOR_COUNT_);
8483 OMP_CLAUSE_OPERAND (c, 0)
8484 = cilk_for_number_of_iterations (omp_for);
8485 OMP_CLAUSE_CHAIN (c) = clauses;
8486 OMP_PARALLEL_CLAUSES (omp_par) = finish_omp_clauses (c, C_ORT_CILK);
8487 add_stmt (omp_par);
8488 return omp_par;
8490 else if (code == CILK_FOR && processing_template_decl)
8492 tree c, clauses = OMP_FOR_CLAUSES (omp_for);
8493 if (orig_decl && orig_decl != decl)
8495 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
8496 OMP_CLAUSE_DECL (c) = orig_decl;
8497 OMP_CLAUSE_CHAIN (c) = clauses;
8498 clauses = c;
8500 if (last)
8502 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
8503 OMP_CLAUSE_DECL (c) = last;
8504 OMP_CLAUSE_CHAIN (c) = clauses;
8505 clauses = c;
8507 OMP_FOR_CLAUSES (omp_for) = clauses;
8509 return omp_for;
8512 void
8513 finish_omp_atomic (enum tree_code code, enum tree_code opcode, tree lhs,
8514 tree rhs, tree v, tree lhs1, tree rhs1, bool seq_cst)
8516 tree orig_lhs;
8517 tree orig_rhs;
8518 tree orig_v;
8519 tree orig_lhs1;
8520 tree orig_rhs1;
8521 bool dependent_p;
8522 tree stmt;
8524 orig_lhs = lhs;
8525 orig_rhs = rhs;
8526 orig_v = v;
8527 orig_lhs1 = lhs1;
8528 orig_rhs1 = rhs1;
8529 dependent_p = false;
8530 stmt = NULL_TREE;
8532 /* Even in a template, we can detect invalid uses of the atomic
8533 pragma if neither LHS nor RHS is type-dependent. */
8534 if (processing_template_decl)
8536 dependent_p = (type_dependent_expression_p (lhs)
8537 || (rhs && type_dependent_expression_p (rhs))
8538 || (v && type_dependent_expression_p (v))
8539 || (lhs1 && type_dependent_expression_p (lhs1))
8540 || (rhs1 && type_dependent_expression_p (rhs1)));
8541 if (!dependent_p)
8543 lhs = build_non_dependent_expr (lhs);
8544 if (rhs)
8545 rhs = build_non_dependent_expr (rhs);
8546 if (v)
8547 v = build_non_dependent_expr (v);
8548 if (lhs1)
8549 lhs1 = build_non_dependent_expr (lhs1);
8550 if (rhs1)
8551 rhs1 = build_non_dependent_expr (rhs1);
8554 if (!dependent_p)
8556 bool swapped = false;
8557 if (rhs1 && cp_tree_equal (lhs, rhs))
8559 std::swap (rhs, rhs1);
8560 swapped = !commutative_tree_code (opcode);
8562 if (rhs1 && !cp_tree_equal (lhs, rhs1))
8564 if (code == OMP_ATOMIC)
8565 error ("%<#pragma omp atomic update%> uses two different "
8566 "expressions for memory");
8567 else
8568 error ("%<#pragma omp atomic capture%> uses two different "
8569 "expressions for memory");
8570 return;
8572 if (lhs1 && !cp_tree_equal (lhs, lhs1))
8574 if (code == OMP_ATOMIC)
8575 error ("%<#pragma omp atomic update%> uses two different "
8576 "expressions for memory");
8577 else
8578 error ("%<#pragma omp atomic capture%> uses two different "
8579 "expressions for memory");
8580 return;
8582 stmt = c_finish_omp_atomic (input_location, code, opcode, lhs, rhs,
8583 v, lhs1, rhs1, swapped, seq_cst,
8584 processing_template_decl != 0);
8585 if (stmt == error_mark_node)
8586 return;
8588 if (processing_template_decl)
8590 if (code == OMP_ATOMIC_READ)
8592 stmt = build_min_nt_loc (EXPR_LOCATION (orig_lhs),
8593 OMP_ATOMIC_READ, orig_lhs);
8594 OMP_ATOMIC_SEQ_CST (stmt) = seq_cst;
8595 stmt = build2 (MODIFY_EXPR, void_type_node, orig_v, stmt);
8597 else
8599 if (opcode == NOP_EXPR)
8600 stmt = build2 (MODIFY_EXPR, void_type_node, orig_lhs, orig_rhs);
8601 else
8602 stmt = build2 (opcode, void_type_node, orig_lhs, orig_rhs);
8603 if (orig_rhs1)
8604 stmt = build_min_nt_loc (EXPR_LOCATION (orig_rhs1),
8605 COMPOUND_EXPR, orig_rhs1, stmt);
8606 if (code != OMP_ATOMIC)
8608 stmt = build_min_nt_loc (EXPR_LOCATION (orig_lhs1),
8609 code, orig_lhs1, stmt);
8610 OMP_ATOMIC_SEQ_CST (stmt) = seq_cst;
8611 stmt = build2 (MODIFY_EXPR, void_type_node, orig_v, stmt);
8614 stmt = build2 (OMP_ATOMIC, void_type_node, integer_zero_node, stmt);
8615 OMP_ATOMIC_SEQ_CST (stmt) = seq_cst;
8617 finish_expr_stmt (stmt);
8620 void
8621 finish_omp_barrier (void)
8623 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_BARRIER);
8624 vec<tree, va_gc> *vec = make_tree_vector ();
8625 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
8626 release_tree_vector (vec);
8627 finish_expr_stmt (stmt);
8630 void
8631 finish_omp_flush (void)
8633 tree fn = builtin_decl_explicit (BUILT_IN_SYNC_SYNCHRONIZE);
8634 vec<tree, va_gc> *vec = make_tree_vector ();
8635 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
8636 release_tree_vector (vec);
8637 finish_expr_stmt (stmt);
8640 void
8641 finish_omp_taskwait (void)
8643 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_TASKWAIT);
8644 vec<tree, va_gc> *vec = make_tree_vector ();
8645 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
8646 release_tree_vector (vec);
8647 finish_expr_stmt (stmt);
8650 void
8651 finish_omp_taskyield (void)
8653 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_TASKYIELD);
8654 vec<tree, va_gc> *vec = make_tree_vector ();
8655 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
8656 release_tree_vector (vec);
8657 finish_expr_stmt (stmt);
8660 void
8661 finish_omp_cancel (tree clauses)
8663 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
8664 int mask = 0;
8665 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
8666 mask = 1;
8667 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
8668 mask = 2;
8669 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
8670 mask = 4;
8671 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
8672 mask = 8;
8673 else
8675 error ("%<#pragma omp cancel%> must specify one of "
8676 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> clauses");
8677 return;
8679 vec<tree, va_gc> *vec = make_tree_vector ();
8680 tree ifc = omp_find_clause (clauses, OMP_CLAUSE_IF);
8681 if (ifc != NULL_TREE)
8683 tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
8684 ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
8685 boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
8686 build_zero_cst (type));
8688 else
8689 ifc = boolean_true_node;
8690 vec->quick_push (build_int_cst (integer_type_node, mask));
8691 vec->quick_push (ifc);
8692 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
8693 release_tree_vector (vec);
8694 finish_expr_stmt (stmt);
8697 void
8698 finish_omp_cancellation_point (tree clauses)
8700 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
8701 int mask = 0;
8702 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
8703 mask = 1;
8704 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
8705 mask = 2;
8706 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
8707 mask = 4;
8708 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
8709 mask = 8;
8710 else
8712 error ("%<#pragma omp cancellation point%> must specify one of "
8713 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> clauses");
8714 return;
8716 vec<tree, va_gc> *vec
8717 = make_tree_vector_single (build_int_cst (integer_type_node, mask));
8718 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
8719 release_tree_vector (vec);
8720 finish_expr_stmt (stmt);
8723 /* Begin a __transaction_atomic or __transaction_relaxed statement.
8724 If PCOMPOUND is non-null, this is for a function-transaction-block, and we
8725 should create an extra compound stmt. */
8727 tree
8728 begin_transaction_stmt (location_t loc, tree *pcompound, int flags)
8730 tree r;
8732 if (pcompound)
8733 *pcompound = begin_compound_stmt (0);
8735 r = build_stmt (loc, TRANSACTION_EXPR, NULL_TREE);
8737 /* Only add the statement to the function if support enabled. */
8738 if (flag_tm)
8739 add_stmt (r);
8740 else
8741 error_at (loc, ((flags & TM_STMT_ATTR_RELAXED) != 0
8742 ? G_("%<__transaction_relaxed%> without "
8743 "transactional memory support enabled")
8744 : G_("%<__transaction_atomic%> without "
8745 "transactional memory support enabled")));
8747 TRANSACTION_EXPR_BODY (r) = push_stmt_list ();
8748 TREE_SIDE_EFFECTS (r) = 1;
8749 return r;
8752 /* End a __transaction_atomic or __transaction_relaxed statement.
8753 If COMPOUND_STMT is non-null, this is for a function-transaction-block,
8754 and we should end the compound. If NOEX is non-NULL, we wrap the body in
8755 a MUST_NOT_THROW_EXPR with NOEX as condition. */
8757 void
8758 finish_transaction_stmt (tree stmt, tree compound_stmt, int flags, tree noex)
8760 TRANSACTION_EXPR_BODY (stmt) = pop_stmt_list (TRANSACTION_EXPR_BODY (stmt));
8761 TRANSACTION_EXPR_OUTER (stmt) = (flags & TM_STMT_ATTR_OUTER) != 0;
8762 TRANSACTION_EXPR_RELAXED (stmt) = (flags & TM_STMT_ATTR_RELAXED) != 0;
8763 TRANSACTION_EXPR_IS_STMT (stmt) = 1;
8765 /* noexcept specifications are not allowed for function transactions. */
8766 gcc_assert (!(noex && compound_stmt));
8767 if (noex)
8769 tree body = build_must_not_throw_expr (TRANSACTION_EXPR_BODY (stmt),
8770 noex);
8771 protected_set_expr_location
8772 (body, EXPR_LOCATION (TRANSACTION_EXPR_BODY (stmt)));
8773 TREE_SIDE_EFFECTS (body) = 1;
8774 TRANSACTION_EXPR_BODY (stmt) = body;
8777 if (compound_stmt)
8778 finish_compound_stmt (compound_stmt);
8781 /* Build a __transaction_atomic or __transaction_relaxed expression. If
8782 NOEX is non-NULL, we wrap the body in a MUST_NOT_THROW_EXPR with NOEX as
8783 condition. */
8785 tree
8786 build_transaction_expr (location_t loc, tree expr, int flags, tree noex)
8788 tree ret;
8789 if (noex)
8791 expr = build_must_not_throw_expr (expr, noex);
8792 protected_set_expr_location (expr, loc);
8793 TREE_SIDE_EFFECTS (expr) = 1;
8795 ret = build1 (TRANSACTION_EXPR, TREE_TYPE (expr), expr);
8796 if (flags & TM_STMT_ATTR_RELAXED)
8797 TRANSACTION_EXPR_RELAXED (ret) = 1;
8798 TREE_SIDE_EFFECTS (ret) = 1;
8799 SET_EXPR_LOCATION (ret, loc);
8800 return ret;
8803 void
8804 init_cp_semantics (void)
8808 /* Build a STATIC_ASSERT for a static assertion with the condition
8809 CONDITION and the message text MESSAGE. LOCATION is the location
8810 of the static assertion in the source code. When MEMBER_P, this
8811 static assertion is a member of a class. */
8812 void
8813 finish_static_assert (tree condition, tree message, location_t location,
8814 bool member_p)
8816 if (message == NULL_TREE
8817 || message == error_mark_node
8818 || condition == NULL_TREE
8819 || condition == error_mark_node)
8820 return;
8822 if (check_for_bare_parameter_packs (condition))
8823 condition = error_mark_node;
8825 if (type_dependent_expression_p (condition)
8826 || value_dependent_expression_p (condition))
8828 /* We're in a template; build a STATIC_ASSERT and put it in
8829 the right place. */
8830 tree assertion;
8832 assertion = make_node (STATIC_ASSERT);
8833 STATIC_ASSERT_CONDITION (assertion) = condition;
8834 STATIC_ASSERT_MESSAGE (assertion) = message;
8835 STATIC_ASSERT_SOURCE_LOCATION (assertion) = location;
8837 if (member_p)
8838 maybe_add_class_template_decl_list (current_class_type,
8839 assertion,
8840 /*friend_p=*/0);
8841 else
8842 add_stmt (assertion);
8844 return;
8847 /* Fold the expression and convert it to a boolean value. */
8848 condition = instantiate_non_dependent_expr (condition);
8849 condition = cp_convert (boolean_type_node, condition, tf_warning_or_error);
8850 condition = maybe_constant_value (condition);
8852 if (TREE_CODE (condition) == INTEGER_CST && !integer_zerop (condition))
8853 /* Do nothing; the condition is satisfied. */
8855 else
8857 location_t saved_loc = input_location;
8859 input_location = location;
8860 if (TREE_CODE (condition) == INTEGER_CST
8861 && integer_zerop (condition))
8863 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
8864 (TREE_TYPE (TREE_TYPE (message))));
8865 int len = TREE_STRING_LENGTH (message) / sz - 1;
8866 /* Report the error. */
8867 if (len == 0)
8868 error ("static assertion failed");
8869 else
8870 error ("static assertion failed: %s",
8871 TREE_STRING_POINTER (message));
8873 else if (condition && condition != error_mark_node)
8875 error ("non-constant condition for static assertion");
8876 if (require_potential_rvalue_constant_expression (condition))
8877 cxx_constant_value (condition);
8879 input_location = saved_loc;
8883 /* Implements the C++0x decltype keyword. Returns the type of EXPR,
8884 suitable for use as a type-specifier.
8886 ID_EXPRESSION_OR_MEMBER_ACCESS_P is true when EXPR was parsed as an
8887 id-expression or a class member access, FALSE when it was parsed as
8888 a full expression. */
8890 tree
8891 finish_decltype_type (tree expr, bool id_expression_or_member_access_p,
8892 tsubst_flags_t complain)
8894 tree type = NULL_TREE;
8896 if (!expr || error_operand_p (expr))
8897 return error_mark_node;
8899 if (TYPE_P (expr)
8900 || TREE_CODE (expr) == TYPE_DECL
8901 || (TREE_CODE (expr) == BIT_NOT_EXPR
8902 && TYPE_P (TREE_OPERAND (expr, 0))))
8904 if (complain & tf_error)
8905 error ("argument to decltype must be an expression");
8906 return error_mark_node;
8909 /* Depending on the resolution of DR 1172, we may later need to distinguish
8910 instantiation-dependent but not type-dependent expressions so that, say,
8911 A<decltype(sizeof(T))>::U doesn't require 'typename'. */
8912 if (instantiation_dependent_uneval_expression_p (expr))
8914 type = cxx_make_type (DECLTYPE_TYPE);
8915 DECLTYPE_TYPE_EXPR (type) = expr;
8916 DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (type)
8917 = id_expression_or_member_access_p;
8918 SET_TYPE_STRUCTURAL_EQUALITY (type);
8920 return type;
8923 /* The type denoted by decltype(e) is defined as follows: */
8925 expr = resolve_nondeduced_context (expr, complain);
8927 if (invalid_nonstatic_memfn_p (input_location, expr, complain))
8928 return error_mark_node;
8930 if (type_unknown_p (expr))
8932 if (complain & tf_error)
8933 error ("decltype cannot resolve address of overloaded function");
8934 return error_mark_node;
8937 /* To get the size of a static data member declared as an array of
8938 unknown bound, we need to instantiate it. */
8939 if (VAR_P (expr)
8940 && VAR_HAD_UNKNOWN_BOUND (expr)
8941 && DECL_TEMPLATE_INSTANTIATION (expr))
8942 instantiate_decl (expr, /*defer_ok*/true, /*expl_inst_mem*/false);
8944 if (id_expression_or_member_access_p)
8946 /* If e is an id-expression or a class member access (5.2.5
8947 [expr.ref]), decltype(e) is defined as the type of the entity
8948 named by e. If there is no such entity, or e names a set of
8949 overloaded functions, the program is ill-formed. */
8950 if (identifier_p (expr))
8951 expr = lookup_name (expr);
8953 if (INDIRECT_REF_P (expr))
8954 /* This can happen when the expression is, e.g., "a.b". Just
8955 look at the underlying operand. */
8956 expr = TREE_OPERAND (expr, 0);
8958 if (TREE_CODE (expr) == OFFSET_REF
8959 || TREE_CODE (expr) == MEMBER_REF
8960 || TREE_CODE (expr) == SCOPE_REF)
8961 /* We're only interested in the field itself. If it is a
8962 BASELINK, we will need to see through it in the next
8963 step. */
8964 expr = TREE_OPERAND (expr, 1);
8966 if (BASELINK_P (expr))
8967 /* See through BASELINK nodes to the underlying function. */
8968 expr = BASELINK_FUNCTIONS (expr);
8970 /* decltype of a decomposition name drops references in the tuple case
8971 (unlike decltype of a normal variable) and keeps cv-qualifiers from
8972 the containing object in the other cases (unlike decltype of a member
8973 access expression). */
8974 if (DECL_DECOMPOSITION_P (expr))
8976 if (DECL_HAS_VALUE_EXPR_P (expr))
8977 /* Expr is an array or struct subobject proxy, handle
8978 bit-fields properly. */
8979 return unlowered_expr_type (expr);
8980 else
8981 /* Expr is a reference variable for the tuple case. */
8982 return lookup_decomp_type (expr);
8985 switch (TREE_CODE (expr))
8987 case FIELD_DECL:
8988 if (DECL_BIT_FIELD_TYPE (expr))
8990 type = DECL_BIT_FIELD_TYPE (expr);
8991 break;
8993 /* Fall through for fields that aren't bitfields. */
8994 gcc_fallthrough ();
8996 case FUNCTION_DECL:
8997 case VAR_DECL:
8998 case CONST_DECL:
8999 case PARM_DECL:
9000 case RESULT_DECL:
9001 case TEMPLATE_PARM_INDEX:
9002 expr = mark_type_use (expr);
9003 type = TREE_TYPE (expr);
9004 break;
9006 case ERROR_MARK:
9007 type = error_mark_node;
9008 break;
9010 case COMPONENT_REF:
9011 case COMPOUND_EXPR:
9012 mark_type_use (expr);
9013 type = is_bitfield_expr_with_lowered_type (expr);
9014 if (!type)
9015 type = TREE_TYPE (TREE_OPERAND (expr, 1));
9016 break;
9018 case BIT_FIELD_REF:
9019 gcc_unreachable ();
9021 case INTEGER_CST:
9022 case PTRMEM_CST:
9023 /* We can get here when the id-expression refers to an
9024 enumerator or non-type template parameter. */
9025 type = TREE_TYPE (expr);
9026 break;
9028 default:
9029 /* Handle instantiated template non-type arguments. */
9030 type = TREE_TYPE (expr);
9031 break;
9034 else
9036 /* Within a lambda-expression:
9038 Every occurrence of decltype((x)) where x is a possibly
9039 parenthesized id-expression that names an entity of
9040 automatic storage duration is treated as if x were
9041 transformed into an access to a corresponding data member
9042 of the closure type that would have been declared if x
9043 were a use of the denoted entity. */
9044 if (outer_automatic_var_p (expr)
9045 && current_function_decl
9046 && LAMBDA_FUNCTION_P (current_function_decl))
9047 type = capture_decltype (expr);
9048 else if (error_operand_p (expr))
9049 type = error_mark_node;
9050 else if (expr == current_class_ptr)
9051 /* If the expression is just "this", we want the
9052 cv-unqualified pointer for the "this" type. */
9053 type = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
9054 else
9056 /* Otherwise, where T is the type of e, if e is an lvalue,
9057 decltype(e) is defined as T&; if an xvalue, T&&; otherwise, T. */
9058 cp_lvalue_kind clk = lvalue_kind (expr);
9059 type = unlowered_expr_type (expr);
9060 gcc_assert (TREE_CODE (type) != REFERENCE_TYPE);
9062 /* For vector types, pick a non-opaque variant. */
9063 if (VECTOR_TYPE_P (type))
9064 type = strip_typedefs (type);
9066 if (clk != clk_none && !(clk & clk_class))
9067 type = cp_build_reference_type (type, (clk & clk_rvalueref));
9071 return type;
9074 /* Called from trait_expr_value to evaluate either __has_nothrow_assign or
9075 __has_nothrow_copy, depending on assign_p. */
9077 static bool
9078 classtype_has_nothrow_assign_or_copy_p (tree type, bool assign_p)
9080 tree fns;
9082 if (assign_p)
9084 int ix;
9085 ix = lookup_fnfields_1 (type, cp_assignment_operator_id (NOP_EXPR));
9086 if (ix < 0)
9087 return false;
9088 fns = (*CLASSTYPE_METHOD_VEC (type))[ix];
9090 else if (TYPE_HAS_COPY_CTOR (type))
9092 /* If construction of the copy constructor was postponed, create
9093 it now. */
9094 if (CLASSTYPE_LAZY_COPY_CTOR (type))
9095 lazily_declare_fn (sfk_copy_constructor, type);
9096 if (CLASSTYPE_LAZY_MOVE_CTOR (type))
9097 lazily_declare_fn (sfk_move_constructor, type);
9098 fns = CLASSTYPE_CONSTRUCTORS (type);
9100 else
9101 return false;
9103 for (ovl_iterator iter (fns); iter; ++iter)
9105 tree fn = *iter;
9107 if (assign_p)
9109 if (copy_fn_p (fn) == 0)
9110 continue;
9112 else if (copy_fn_p (fn) <= 0)
9113 continue;
9115 maybe_instantiate_noexcept (fn);
9116 if (!TYPE_NOTHROW_P (TREE_TYPE (fn)))
9117 return false;
9120 return true;
9123 /* Actually evaluates the trait. */
9125 static bool
9126 trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
9128 enum tree_code type_code1;
9129 tree t;
9131 type_code1 = TREE_CODE (type1);
9133 switch (kind)
9135 case CPTK_HAS_NOTHROW_ASSIGN:
9136 type1 = strip_array_types (type1);
9137 return (!CP_TYPE_CONST_P (type1) && type_code1 != REFERENCE_TYPE
9138 && (trait_expr_value (CPTK_HAS_TRIVIAL_ASSIGN, type1, type2)
9139 || (CLASS_TYPE_P (type1)
9140 && classtype_has_nothrow_assign_or_copy_p (type1,
9141 true))));
9143 case CPTK_HAS_TRIVIAL_ASSIGN:
9144 /* ??? The standard seems to be missing the "or array of such a class
9145 type" wording for this trait. */
9146 type1 = strip_array_types (type1);
9147 return (!CP_TYPE_CONST_P (type1) && type_code1 != REFERENCE_TYPE
9148 && (trivial_type_p (type1)
9149 || (CLASS_TYPE_P (type1)
9150 && TYPE_HAS_TRIVIAL_COPY_ASSIGN (type1))));
9152 case CPTK_HAS_NOTHROW_CONSTRUCTOR:
9153 type1 = strip_array_types (type1);
9154 return (trait_expr_value (CPTK_HAS_TRIVIAL_CONSTRUCTOR, type1, type2)
9155 || (CLASS_TYPE_P (type1)
9156 && (t = locate_ctor (type1))
9157 && (maybe_instantiate_noexcept (t),
9158 TYPE_NOTHROW_P (TREE_TYPE (t)))));
9160 case CPTK_HAS_TRIVIAL_CONSTRUCTOR:
9161 type1 = strip_array_types (type1);
9162 return (trivial_type_p (type1)
9163 || (CLASS_TYPE_P (type1) && TYPE_HAS_TRIVIAL_DFLT (type1)));
9165 case CPTK_HAS_NOTHROW_COPY:
9166 type1 = strip_array_types (type1);
9167 return (trait_expr_value (CPTK_HAS_TRIVIAL_COPY, type1, type2)
9168 || (CLASS_TYPE_P (type1)
9169 && classtype_has_nothrow_assign_or_copy_p (type1, false)));
9171 case CPTK_HAS_TRIVIAL_COPY:
9172 /* ??? The standard seems to be missing the "or array of such a class
9173 type" wording for this trait. */
9174 type1 = strip_array_types (type1);
9175 return (trivial_type_p (type1) || type_code1 == REFERENCE_TYPE
9176 || (CLASS_TYPE_P (type1) && TYPE_HAS_TRIVIAL_COPY_CTOR (type1)));
9178 case CPTK_HAS_TRIVIAL_DESTRUCTOR:
9179 type1 = strip_array_types (type1);
9180 return (trivial_type_p (type1) || type_code1 == REFERENCE_TYPE
9181 || (CLASS_TYPE_P (type1)
9182 && TYPE_HAS_TRIVIAL_DESTRUCTOR (type1)));
9184 case CPTK_HAS_VIRTUAL_DESTRUCTOR:
9185 return type_has_virtual_destructor (type1);
9187 case CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS:
9188 return type_has_unique_obj_representations (type1);
9190 case CPTK_IS_ABSTRACT:
9191 return ABSTRACT_CLASS_TYPE_P (type1);
9193 case CPTK_IS_AGGREGATE:
9194 return CP_AGGREGATE_TYPE_P (type1);
9196 case CPTK_IS_BASE_OF:
9197 return (NON_UNION_CLASS_TYPE_P (type1) && NON_UNION_CLASS_TYPE_P (type2)
9198 && (same_type_ignoring_top_level_qualifiers_p (type1, type2)
9199 || DERIVED_FROM_P (type1, type2)));
9201 case CPTK_IS_CLASS:
9202 return NON_UNION_CLASS_TYPE_P (type1);
9204 case CPTK_IS_EMPTY:
9205 return NON_UNION_CLASS_TYPE_P (type1) && CLASSTYPE_EMPTY_P (type1);
9207 case CPTK_IS_ENUM:
9208 return type_code1 == ENUMERAL_TYPE;
9210 case CPTK_IS_FINAL:
9211 return CLASS_TYPE_P (type1) && CLASSTYPE_FINAL (type1);
9213 case CPTK_IS_LITERAL_TYPE:
9214 return literal_type_p (type1);
9216 case CPTK_IS_POD:
9217 return pod_type_p (type1);
9219 case CPTK_IS_POLYMORPHIC:
9220 return CLASS_TYPE_P (type1) && TYPE_POLYMORPHIC_P (type1);
9222 case CPTK_IS_SAME_AS:
9223 return same_type_p (type1, type2);
9225 case CPTK_IS_STD_LAYOUT:
9226 return std_layout_type_p (type1);
9228 case CPTK_IS_TRIVIAL:
9229 return trivial_type_p (type1);
9231 case CPTK_IS_TRIVIALLY_ASSIGNABLE:
9232 return is_trivially_xible (MODIFY_EXPR, type1, type2);
9234 case CPTK_IS_TRIVIALLY_CONSTRUCTIBLE:
9235 return is_trivially_xible (INIT_EXPR, type1, type2);
9237 case CPTK_IS_TRIVIALLY_COPYABLE:
9238 return trivially_copyable_p (type1);
9240 case CPTK_IS_UNION:
9241 return type_code1 == UNION_TYPE;
9243 case CPTK_IS_ASSIGNABLE:
9244 return is_xible (MODIFY_EXPR, type1, type2);
9246 case CPTK_IS_CONSTRUCTIBLE:
9247 return is_xible (INIT_EXPR, type1, type2);
9249 default:
9250 gcc_unreachable ();
9251 return false;
9255 /* If TYPE is an array of unknown bound, or (possibly cv-qualified)
9256 void, or a complete type, returns true, otherwise false. */
9258 static bool
9259 check_trait_type (tree type)
9261 if (type == NULL_TREE)
9262 return true;
9264 if (TREE_CODE (type) == TREE_LIST)
9265 return (check_trait_type (TREE_VALUE (type))
9266 && check_trait_type (TREE_CHAIN (type)));
9268 if (TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type)
9269 && COMPLETE_TYPE_P (TREE_TYPE (type)))
9270 return true;
9272 if (VOID_TYPE_P (type))
9273 return true;
9275 return !!complete_type_or_else (strip_array_types (type), NULL_TREE);
9278 /* Process a trait expression. */
9280 tree
9281 finish_trait_expr (cp_trait_kind kind, tree type1, tree type2)
9283 if (type1 == error_mark_node
9284 || type2 == error_mark_node)
9285 return error_mark_node;
9287 if (processing_template_decl)
9289 tree trait_expr = make_node (TRAIT_EXPR);
9290 TREE_TYPE (trait_expr) = boolean_type_node;
9291 TRAIT_EXPR_TYPE1 (trait_expr) = type1;
9292 TRAIT_EXPR_TYPE2 (trait_expr) = type2;
9293 TRAIT_EXPR_KIND (trait_expr) = kind;
9294 return trait_expr;
9297 switch (kind)
9299 case CPTK_HAS_NOTHROW_ASSIGN:
9300 case CPTK_HAS_TRIVIAL_ASSIGN:
9301 case CPTK_HAS_NOTHROW_CONSTRUCTOR:
9302 case CPTK_HAS_TRIVIAL_CONSTRUCTOR:
9303 case CPTK_HAS_NOTHROW_COPY:
9304 case CPTK_HAS_TRIVIAL_COPY:
9305 case CPTK_HAS_TRIVIAL_DESTRUCTOR:
9306 case CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS:
9307 case CPTK_HAS_VIRTUAL_DESTRUCTOR:
9308 case CPTK_IS_ABSTRACT:
9309 case CPTK_IS_AGGREGATE:
9310 case CPTK_IS_EMPTY:
9311 case CPTK_IS_FINAL:
9312 case CPTK_IS_LITERAL_TYPE:
9313 case CPTK_IS_POD:
9314 case CPTK_IS_POLYMORPHIC:
9315 case CPTK_IS_STD_LAYOUT:
9316 case CPTK_IS_TRIVIAL:
9317 case CPTK_IS_TRIVIALLY_COPYABLE:
9318 if (!check_trait_type (type1))
9319 return error_mark_node;
9320 break;
9322 case CPTK_IS_ASSIGNABLE:
9323 case CPTK_IS_CONSTRUCTIBLE:
9324 break;
9326 case CPTK_IS_TRIVIALLY_ASSIGNABLE:
9327 case CPTK_IS_TRIVIALLY_CONSTRUCTIBLE:
9328 if (!check_trait_type (type1)
9329 || !check_trait_type (type2))
9330 return error_mark_node;
9331 break;
9333 case CPTK_IS_BASE_OF:
9334 if (NON_UNION_CLASS_TYPE_P (type1) && NON_UNION_CLASS_TYPE_P (type2)
9335 && !same_type_ignoring_top_level_qualifiers_p (type1, type2)
9336 && !complete_type_or_else (type2, NULL_TREE))
9337 /* We already issued an error. */
9338 return error_mark_node;
9339 break;
9341 case CPTK_IS_CLASS:
9342 case CPTK_IS_ENUM:
9343 case CPTK_IS_UNION:
9344 case CPTK_IS_SAME_AS:
9345 break;
9347 default:
9348 gcc_unreachable ();
9351 return (trait_expr_value (kind, type1, type2)
9352 ? boolean_true_node : boolean_false_node);
9355 /* Do-nothing variants of functions to handle pragma FLOAT_CONST_DECIMAL64,
9356 which is ignored for C++. */
9358 void
9359 set_float_const_decimal64 (void)
9363 void
9364 clear_float_const_decimal64 (void)
9368 bool
9369 float_const_decimal64_p (void)
9371 return 0;
9375 /* Return true if T designates the implied `this' parameter. */
9377 bool
9378 is_this_parameter (tree t)
9380 if (!DECL_P (t) || DECL_NAME (t) != this_identifier)
9381 return false;
9382 gcc_assert (TREE_CODE (t) == PARM_DECL || is_capture_proxy (t)
9383 || (cp_binding_oracle && TREE_CODE (t) == VAR_DECL));
9384 return true;
9387 /* Insert the deduced return type for an auto function. */
9389 void
9390 apply_deduced_return_type (tree fco, tree return_type)
9392 tree result;
9394 if (return_type == error_mark_node)
9395 return;
9397 if (LAMBDA_FUNCTION_P (fco))
9399 tree lambda = CLASSTYPE_LAMBDA_EXPR (current_class_type);
9400 LAMBDA_EXPR_RETURN_TYPE (lambda) = return_type;
9403 if (DECL_CONV_FN_P (fco))
9404 DECL_NAME (fco) = mangle_conv_op_name_for_type (return_type);
9406 TREE_TYPE (fco) = change_return_type (return_type, TREE_TYPE (fco));
9408 result = DECL_RESULT (fco);
9409 if (result == NULL_TREE)
9410 return;
9411 if (TREE_TYPE (result) == return_type)
9412 return;
9414 if (!processing_template_decl && !VOID_TYPE_P (return_type)
9415 && !complete_type_or_else (return_type, NULL_TREE))
9416 return;
9418 /* We already have a DECL_RESULT from start_preparsed_function.
9419 Now we need to redo the work it and allocate_struct_function
9420 did to reflect the new type. */
9421 gcc_assert (current_function_decl == fco);
9422 result = build_decl (input_location, RESULT_DECL, NULL_TREE,
9423 TYPE_MAIN_VARIANT (return_type));
9424 DECL_ARTIFICIAL (result) = 1;
9425 DECL_IGNORED_P (result) = 1;
9426 cp_apply_type_quals_to_decl (cp_type_quals (return_type),
9427 result);
9429 DECL_RESULT (fco) = result;
9431 if (!processing_template_decl)
9433 bool aggr = aggregate_value_p (result, fco);
9434 #ifdef PCC_STATIC_STRUCT_RETURN
9435 cfun->returns_pcc_struct = aggr;
9436 #endif
9437 cfun->returns_struct = aggr;
9442 /* DECL is a local variable or parameter from the surrounding scope of a
9443 lambda-expression. Returns the decltype for a use of the capture field
9444 for DECL even if it hasn't been captured yet. */
9446 static tree
9447 capture_decltype (tree decl)
9449 tree lam = CLASSTYPE_LAMBDA_EXPR (DECL_CONTEXT (current_function_decl));
9450 /* FIXME do lookup instead of list walk? */
9451 tree cap = value_member (decl, LAMBDA_EXPR_CAPTURE_LIST (lam));
9452 tree type;
9454 if (cap)
9455 type = TREE_TYPE (TREE_PURPOSE (cap));
9456 else
9457 switch (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lam))
9459 case CPLD_NONE:
9460 error ("%qD is not captured", decl);
9461 return error_mark_node;
9463 case CPLD_COPY:
9464 type = TREE_TYPE (decl);
9465 if (TREE_CODE (type) == REFERENCE_TYPE
9466 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
9467 type = TREE_TYPE (type);
9468 break;
9470 case CPLD_REFERENCE:
9471 type = TREE_TYPE (decl);
9472 if (TREE_CODE (type) != REFERENCE_TYPE)
9473 type = build_reference_type (TREE_TYPE (decl));
9474 break;
9476 default:
9477 gcc_unreachable ();
9480 if (TREE_CODE (type) != REFERENCE_TYPE)
9482 if (!LAMBDA_EXPR_MUTABLE_P (lam))
9483 type = cp_build_qualified_type (type, (cp_type_quals (type)
9484 |TYPE_QUAL_CONST));
9485 type = build_reference_type (type);
9487 return type;
9490 /* Build a unary fold expression of EXPR over OP. If IS_RIGHT is true,
9491 this is a right unary fold. Otherwise it is a left unary fold. */
9493 static tree
9494 finish_unary_fold_expr (tree expr, int op, tree_code dir)
9496 // Build a pack expansion (assuming expr has pack type).
9497 if (!uses_parameter_packs (expr))
9499 error_at (location_of (expr), "operand of fold expression has no "
9500 "unexpanded parameter packs");
9501 return error_mark_node;
9503 tree pack = make_pack_expansion (expr);
9505 // Build the fold expression.
9506 tree code = build_int_cstu (integer_type_node, abs (op));
9507 tree fold = build_min_nt_loc (UNKNOWN_LOCATION, dir, code, pack);
9508 FOLD_EXPR_MODIFY_P (fold) = (op < 0);
9509 return fold;
9512 tree
9513 finish_left_unary_fold_expr (tree expr, int op)
9515 return finish_unary_fold_expr (expr, op, UNARY_LEFT_FOLD_EXPR);
9518 tree
9519 finish_right_unary_fold_expr (tree expr, int op)
9521 return finish_unary_fold_expr (expr, op, UNARY_RIGHT_FOLD_EXPR);
9524 /* Build a binary fold expression over EXPR1 and EXPR2. The
9525 associativity of the fold is determined by EXPR1 and EXPR2 (whichever
9526 has an unexpanded parameter pack). */
9528 tree
9529 finish_binary_fold_expr (tree pack, tree init, int op, tree_code dir)
9531 pack = make_pack_expansion (pack);
9532 tree code = build_int_cstu (integer_type_node, abs (op));
9533 tree fold = build_min_nt_loc (UNKNOWN_LOCATION, dir, code, pack, init);
9534 FOLD_EXPR_MODIFY_P (fold) = (op < 0);
9535 return fold;
9538 tree
9539 finish_binary_fold_expr (tree expr1, tree expr2, int op)
9541 // Determine which expr has an unexpanded parameter pack and
9542 // set the pack and initial term.
9543 bool pack1 = uses_parameter_packs (expr1);
9544 bool pack2 = uses_parameter_packs (expr2);
9545 if (pack1 && !pack2)
9546 return finish_binary_fold_expr (expr1, expr2, op, BINARY_RIGHT_FOLD_EXPR);
9547 else if (pack2 && !pack1)
9548 return finish_binary_fold_expr (expr2, expr1, op, BINARY_LEFT_FOLD_EXPR);
9549 else
9551 if (pack1)
9552 error ("both arguments in binary fold have unexpanded parameter packs");
9553 else
9554 error ("no unexpanded parameter packs in binary fold");
9556 return error_mark_node;
9559 /* Finish __builtin_launder (arg). */
9561 tree
9562 finish_builtin_launder (location_t loc, tree arg, tsubst_flags_t complain)
9564 tree orig_arg = arg;
9565 if (!type_dependent_expression_p (arg))
9566 arg = decay_conversion (arg, complain);
9567 if (error_operand_p (arg))
9568 return error_mark_node;
9569 if (!type_dependent_expression_p (arg)
9570 && TREE_CODE (TREE_TYPE (arg)) != POINTER_TYPE)
9572 error_at (loc, "non-pointer argument to %<__builtin_launder%>");
9573 return error_mark_node;
9575 if (processing_template_decl)
9576 arg = orig_arg;
9577 return build_call_expr_internal_loc (loc, IFN_LAUNDER,
9578 TREE_TYPE (arg), 1, arg);
9581 #include "gt-cp-semantics.h"