Implement P0315R4, Lambdas in unevaluated contexts.
[official-gcc.git] / gcc / cp / semantics.c
blob20fd9c47f1f46ec57f4999e049564749aea05f99
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-2018 Free Software Foundation, Inc.
7 Written by Mark Mitchell (mmitchell@usa.net) based on code found
8 formerly in parse.y and pt.c.
10 This file is part of GCC.
12 GCC is free software; you can redistribute it and/or modify it
13 under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 3, or (at your option)
15 any later version.
17 GCC is distributed in the hope that it will be useful, but
18 WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with GCC; see the file COPYING3. If not see
24 <http://www.gnu.org/licenses/>. */
26 #include "config.h"
27 #include "system.h"
28 #include "coretypes.h"
29 #include "target.h"
30 #include "bitmap.h"
31 #include "cp-tree.h"
32 #include "stringpool.h"
33 #include "cgraph.h"
34 #include "stmt.h"
35 #include "varasm.h"
36 #include "stor-layout.h"
37 #include "c-family/c-objc.h"
38 #include "tree-inline.h"
39 #include "intl.h"
40 #include "tree-iterator.h"
41 #include "omp-general.h"
42 #include "convert.h"
43 #include "stringpool.h"
44 #include "attribs.h"
45 #include "gomp-constants.h"
46 #include "predict.h"
47 #include "memmodel.h"
49 /* There routines provide a modular interface to perform many parsing
50 operations. They may therefore be used during actual parsing, or
51 during template instantiation, which may be regarded as a
52 degenerate form of parsing. */
54 static tree maybe_convert_cond (tree);
55 static tree finalize_nrv_r (tree *, int *, void *);
56 static tree capture_decltype (tree);
58 /* Used for OpenMP non-static data member privatization. */
60 static hash_map<tree, tree> *omp_private_member_map;
61 static vec<tree> omp_private_member_vec;
62 static bool omp_private_member_ignore_next;
65 /* Deferred Access Checking Overview
66 ---------------------------------
68 Most C++ expressions and declarations require access checking
69 to be performed during parsing. However, in several cases,
70 this has to be treated differently.
72 For member declarations, access checking has to be deferred
73 until more information about the declaration is known. For
74 example:
76 class A {
77 typedef int X;
78 public:
79 X f();
82 A::X A::f();
83 A::X g();
85 When we are parsing the function return type `A::X', we don't
86 really know if this is allowed until we parse the function name.
88 Furthermore, some contexts require that access checking is
89 never performed at all. These include class heads, and template
90 instantiations.
92 Typical use of access checking functions is described here:
94 1. When we enter a context that requires certain access checking
95 mode, the function `push_deferring_access_checks' is called with
96 DEFERRING argument specifying the desired mode. Access checking
97 may be performed immediately (dk_no_deferred), deferred
98 (dk_deferred), or not performed (dk_no_check).
100 2. When a declaration such as a type, or a variable, is encountered,
101 the function `perform_or_defer_access_check' is called. It
102 maintains a vector of all deferred checks.
104 3. The global `current_class_type' or `current_function_decl' is then
105 setup by the parser. `enforce_access' relies on these information
106 to check access.
108 4. Upon exiting the context mentioned in step 1,
109 `perform_deferred_access_checks' is called to check all declaration
110 stored in the vector. `pop_deferring_access_checks' is then
111 called to restore the previous access checking mode.
113 In case of parsing error, we simply call `pop_deferring_access_checks'
114 without `perform_deferred_access_checks'. */
116 struct GTY(()) deferred_access {
117 /* A vector representing name-lookups for which we have deferred
118 checking access controls. We cannot check the accessibility of
119 names used in a decl-specifier-seq until we know what is being
120 declared because code like:
122 class A {
123 class B {};
124 B* f();
127 A::B* A::f() { return 0; }
129 is valid, even though `A::B' is not generally accessible. */
130 vec<deferred_access_check, va_gc> * GTY(()) deferred_access_checks;
132 /* The current mode of access checks. */
133 enum deferring_kind deferring_access_checks_kind;
137 /* Data for deferred access checking. */
138 static GTY(()) vec<deferred_access, va_gc> *deferred_access_stack;
139 static GTY(()) unsigned deferred_access_no_check;
141 /* Save the current deferred access states and start deferred
142 access checking iff DEFER_P is true. */
144 void
145 push_deferring_access_checks (deferring_kind deferring)
147 /* For context like template instantiation, access checking
148 disabling applies to all nested context. */
149 if (deferred_access_no_check || deferring == dk_no_check)
150 deferred_access_no_check++;
151 else
153 deferred_access e = {NULL, deferring};
154 vec_safe_push (deferred_access_stack, e);
158 /* Save the current deferred access states and start deferred access
159 checking, continuing the set of deferred checks in CHECKS. */
161 void
162 reopen_deferring_access_checks (vec<deferred_access_check, va_gc> * checks)
164 push_deferring_access_checks (dk_deferred);
165 if (!deferred_access_no_check)
166 deferred_access_stack->last().deferred_access_checks = checks;
169 /* Resume deferring access checks again after we stopped doing
170 this previously. */
172 void
173 resume_deferring_access_checks (void)
175 if (!deferred_access_no_check)
176 deferred_access_stack->last().deferring_access_checks_kind = dk_deferred;
179 /* Stop deferring access checks. */
181 void
182 stop_deferring_access_checks (void)
184 if (!deferred_access_no_check)
185 deferred_access_stack->last().deferring_access_checks_kind = dk_no_deferred;
188 /* Discard the current deferred access checks and restore the
189 previous states. */
191 void
192 pop_deferring_access_checks (void)
194 if (deferred_access_no_check)
195 deferred_access_no_check--;
196 else
197 deferred_access_stack->pop ();
200 /* Returns a TREE_LIST representing the deferred checks.
201 The TREE_PURPOSE of each node is the type through which the
202 access occurred; the TREE_VALUE is the declaration named.
205 vec<deferred_access_check, va_gc> *
206 get_deferred_access_checks (void)
208 if (deferred_access_no_check)
209 return NULL;
210 else
211 return (deferred_access_stack->last().deferred_access_checks);
214 /* Take current deferred checks and combine with the
215 previous states if we also defer checks previously.
216 Otherwise perform checks now. */
218 void
219 pop_to_parent_deferring_access_checks (void)
221 if (deferred_access_no_check)
222 deferred_access_no_check--;
223 else
225 vec<deferred_access_check, va_gc> *checks;
226 deferred_access *ptr;
228 checks = (deferred_access_stack->last ().deferred_access_checks);
230 deferred_access_stack->pop ();
231 ptr = &deferred_access_stack->last ();
232 if (ptr->deferring_access_checks_kind == dk_no_deferred)
234 /* Check access. */
235 perform_access_checks (checks, tf_warning_or_error);
237 else
239 /* Merge with parent. */
240 int i, j;
241 deferred_access_check *chk, *probe;
243 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
245 FOR_EACH_VEC_SAFE_ELT (ptr->deferred_access_checks, j, probe)
247 if (probe->binfo == chk->binfo &&
248 probe->decl == chk->decl &&
249 probe->diag_decl == chk->diag_decl)
250 goto found;
252 /* Insert into parent's checks. */
253 vec_safe_push (ptr->deferred_access_checks, *chk);
254 found:;
260 /* Perform the access checks in CHECKS. The TREE_PURPOSE of each node
261 is the BINFO indicating the qualifying scope used to access the
262 DECL node stored in the TREE_VALUE of the node. If CHECKS is empty
263 or we aren't in SFINAE context or all the checks succeed return TRUE,
264 otherwise FALSE. */
266 bool
267 perform_access_checks (vec<deferred_access_check, va_gc> *checks,
268 tsubst_flags_t complain)
270 int i;
271 deferred_access_check *chk;
272 location_t loc = input_location;
273 bool ok = true;
275 if (!checks)
276 return true;
278 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
280 input_location = chk->loc;
281 ok &= enforce_access (chk->binfo, chk->decl, chk->diag_decl, complain);
284 input_location = loc;
285 return (complain & tf_error) ? true : ok;
288 /* Perform the deferred access checks.
290 After performing the checks, we still have to keep the list
291 `deferred_access_stack->deferred_access_checks' since we may want
292 to check access for them again later in a different context.
293 For example:
295 class A {
296 typedef int X;
297 static X a;
299 A::X A::a, x; // No error for `A::a', error for `x'
301 We have to perform deferred access of `A::X', first with `A::a',
302 next with `x'. Return value like perform_access_checks above. */
304 bool
305 perform_deferred_access_checks (tsubst_flags_t complain)
307 return perform_access_checks (get_deferred_access_checks (), complain);
310 /* Defer checking the accessibility of DECL, when looked up in
311 BINFO. DIAG_DECL is the declaration to use to print diagnostics.
312 Return value like perform_access_checks above.
313 If non-NULL, report failures to AFI. */
315 bool
316 perform_or_defer_access_check (tree binfo, tree decl, tree diag_decl,
317 tsubst_flags_t complain,
318 access_failure_info *afi)
320 int i;
321 deferred_access *ptr;
322 deferred_access_check *chk;
325 /* Exit if we are in a context that no access checking is performed.
327 if (deferred_access_no_check)
328 return true;
330 gcc_assert (TREE_CODE (binfo) == TREE_BINFO);
332 ptr = &deferred_access_stack->last ();
334 /* If we are not supposed to defer access checks, just check now. */
335 if (ptr->deferring_access_checks_kind == dk_no_deferred)
337 bool ok = enforce_access (binfo, decl, diag_decl, complain, afi);
338 return (complain & tf_error) ? true : ok;
341 /* See if we are already going to perform this check. */
342 FOR_EACH_VEC_SAFE_ELT (ptr->deferred_access_checks, i, chk)
344 if (chk->decl == decl && chk->binfo == binfo &&
345 chk->diag_decl == diag_decl)
347 return true;
350 /* If not, record the check. */
351 deferred_access_check new_access = {binfo, decl, diag_decl, input_location};
352 vec_safe_push (ptr->deferred_access_checks, new_access);
354 return true;
357 /* Returns nonzero if the current statement is a full expression,
358 i.e. temporaries created during that statement should be destroyed
359 at the end of the statement. */
362 stmts_are_full_exprs_p (void)
364 return current_stmt_tree ()->stmts_are_full_exprs_p;
367 /* T is a statement. Add it to the statement-tree. This is the C++
368 version. The C/ObjC frontends have a slightly different version of
369 this function. */
371 tree
372 add_stmt (tree t)
374 enum tree_code code = TREE_CODE (t);
376 if (EXPR_P (t) && code != LABEL_EXPR)
378 if (!EXPR_HAS_LOCATION (t))
379 SET_EXPR_LOCATION (t, input_location);
381 /* When we expand a statement-tree, we must know whether or not the
382 statements are full-expressions. We record that fact here. */
383 STMT_IS_FULL_EXPR_P (t) = stmts_are_full_exprs_p ();
386 if (code == LABEL_EXPR || code == CASE_LABEL_EXPR)
387 STATEMENT_LIST_HAS_LABEL (cur_stmt_list) = 1;
389 /* Add T to the statement-tree. Non-side-effect statements need to be
390 recorded during statement expressions. */
391 gcc_checking_assert (!stmt_list_stack->is_empty ());
392 append_to_statement_list_force (t, &cur_stmt_list);
394 return t;
397 /* Returns the stmt_tree to which statements are currently being added. */
399 stmt_tree
400 current_stmt_tree (void)
402 return (cfun
403 ? &cfun->language->base.x_stmt_tree
404 : &scope_chain->x_stmt_tree);
407 /* If statements are full expressions, wrap STMT in a CLEANUP_POINT_EXPR. */
409 static tree
410 maybe_cleanup_point_expr (tree expr)
412 if (!processing_template_decl && stmts_are_full_exprs_p ())
413 expr = fold_build_cleanup_point_expr (TREE_TYPE (expr), expr);
414 return expr;
417 /* Like maybe_cleanup_point_expr except have the type of the new expression be
418 void so we don't need to create a temporary variable to hold the inner
419 expression. The reason why we do this is because the original type might be
420 an aggregate and we cannot create a temporary variable for that type. */
422 tree
423 maybe_cleanup_point_expr_void (tree expr)
425 if (!processing_template_decl && stmts_are_full_exprs_p ())
426 expr = fold_build_cleanup_point_expr (void_type_node, expr);
427 return expr;
432 /* Create a declaration statement for the declaration given by the DECL. */
434 void
435 add_decl_expr (tree decl)
437 tree r = build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl);
438 if (DECL_INITIAL (decl)
439 || (DECL_SIZE (decl) && TREE_SIDE_EFFECTS (DECL_SIZE (decl))))
440 r = maybe_cleanup_point_expr_void (r);
441 add_stmt (r);
444 /* Finish a scope. */
446 tree
447 do_poplevel (tree stmt_list)
449 tree block = NULL;
451 if (stmts_are_full_exprs_p ())
452 block = poplevel (kept_level_p (), 1, 0);
454 stmt_list = pop_stmt_list (stmt_list);
456 if (!processing_template_decl)
458 stmt_list = c_build_bind_expr (input_location, block, stmt_list);
459 /* ??? See c_end_compound_stmt re statement expressions. */
462 return stmt_list;
465 /* Begin a new scope. */
467 static tree
468 do_pushlevel (scope_kind sk)
470 tree ret = push_stmt_list ();
471 if (stmts_are_full_exprs_p ())
472 begin_scope (sk, NULL);
473 return ret;
476 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
477 when the current scope is exited. EH_ONLY is true when this is not
478 meant to apply to normal control flow transfer. */
480 void
481 push_cleanup (tree decl, tree cleanup, bool eh_only)
483 tree stmt = build_stmt (input_location, CLEANUP_STMT, NULL, cleanup, decl);
484 CLEANUP_EH_ONLY (stmt) = eh_only;
485 add_stmt (stmt);
486 CLEANUP_BODY (stmt) = push_stmt_list ();
489 /* Simple infinite loop tracking for -Wreturn-type. We keep a stack of all
490 the current loops, represented by 'NULL_TREE' if we've seen a possible
491 exit, and 'error_mark_node' if not. This is currently used only to
492 suppress the warning about a function with no return statements, and
493 therefore we don't bother noting returns as possible exits. We also
494 don't bother with gotos. */
496 static void
497 begin_maybe_infinite_loop (tree cond)
499 /* Only track this while parsing a function, not during instantiation. */
500 if (!cfun || (DECL_TEMPLATE_INSTANTIATION (current_function_decl)
501 && !processing_template_decl))
502 return;
503 bool maybe_infinite = true;
504 if (cond)
506 cond = fold_non_dependent_expr (cond);
507 maybe_infinite = integer_nonzerop (cond);
509 vec_safe_push (cp_function_chain->infinite_loops,
510 maybe_infinite ? error_mark_node : NULL_TREE);
514 /* A break is a possible exit for the current loop. */
516 void
517 break_maybe_infinite_loop (void)
519 if (!cfun)
520 return;
521 cp_function_chain->infinite_loops->last() = NULL_TREE;
524 /* If we reach the end of the loop without seeing a possible exit, we have
525 an infinite loop. */
527 static void
528 end_maybe_infinite_loop (tree cond)
530 if (!cfun || (DECL_TEMPLATE_INSTANTIATION (current_function_decl)
531 && !processing_template_decl))
532 return;
533 tree current = cp_function_chain->infinite_loops->pop();
534 if (current != NULL_TREE)
536 cond = fold_non_dependent_expr (cond);
537 if (integer_nonzerop (cond))
538 current_function_infinite_loop = 1;
543 /* Begin a conditional that might contain a declaration. When generating
544 normal code, we want the declaration to appear before the statement
545 containing the conditional. When generating template code, we want the
546 conditional to be rendered as the raw DECL_EXPR. */
548 static void
549 begin_cond (tree *cond_p)
551 if (processing_template_decl)
552 *cond_p = push_stmt_list ();
555 /* Finish such a conditional. */
557 static void
558 finish_cond (tree *cond_p, tree expr)
560 if (processing_template_decl)
562 tree cond = pop_stmt_list (*cond_p);
564 if (expr == NULL_TREE)
565 /* Empty condition in 'for'. */
566 gcc_assert (empty_expr_stmt_p (cond));
567 else if (check_for_bare_parameter_packs (expr))
568 expr = error_mark_node;
569 else if (!empty_expr_stmt_p (cond))
570 expr = build2 (COMPOUND_EXPR, TREE_TYPE (expr), cond, expr);
572 *cond_p = expr;
575 /* If *COND_P specifies a conditional with a declaration, transform the
576 loop such that
577 while (A x = 42) { }
578 for (; A x = 42;) { }
579 becomes
580 while (true) { A x = 42; if (!x) break; }
581 for (;;) { A x = 42; if (!x) break; }
582 The statement list for BODY will be empty if the conditional did
583 not declare anything. */
585 static void
586 simplify_loop_decl_cond (tree *cond_p, tree body)
588 tree cond, if_stmt;
590 if (!TREE_SIDE_EFFECTS (body))
591 return;
593 cond = *cond_p;
594 *cond_p = boolean_true_node;
596 if_stmt = begin_if_stmt ();
597 cond = cp_build_unary_op (TRUTH_NOT_EXPR, cond, false, tf_warning_or_error);
598 finish_if_stmt_cond (cond, if_stmt);
599 finish_break_stmt ();
600 finish_then_clause (if_stmt);
601 finish_if_stmt (if_stmt);
604 /* Finish a goto-statement. */
606 tree
607 finish_goto_stmt (tree destination)
609 if (identifier_p (destination))
610 destination = lookup_label (destination);
612 /* We warn about unused labels with -Wunused. That means we have to
613 mark the used labels as used. */
614 if (TREE_CODE (destination) == LABEL_DECL)
615 TREE_USED (destination) = 1;
616 else
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 add_stmt (build_predict_expr (PRED_GOTO, NOT_TAKEN));
634 return add_stmt (build_stmt (input_location, GOTO_EXPR, destination));
637 /* COND is the condition-expression for an if, while, etc.,
638 statement. Convert it to a boolean value, if appropriate.
639 In addition, verify sequence points if -Wsequence-point is enabled. */
641 static tree
642 maybe_convert_cond (tree cond)
644 /* Empty conditions remain empty. */
645 if (!cond)
646 return NULL_TREE;
648 /* Wait until we instantiate templates before doing conversion. */
649 if (processing_template_decl)
650 return cond;
652 if (warn_sequence_point)
653 verify_sequence_points (cond);
655 /* Do the conversion. */
656 cond = convert_from_reference (cond);
658 if (TREE_CODE (cond) == MODIFY_EXPR
659 && !TREE_NO_WARNING (cond)
660 && warn_parentheses)
662 warning_at (cp_expr_loc_or_loc (cond, input_location), OPT_Wparentheses,
663 "suggest parentheses around assignment used as truth value");
664 TREE_NO_WARNING (cond) = 1;
667 return condition_conversion (cond);
670 /* Finish an expression-statement, whose EXPRESSION is as indicated. */
672 tree
673 finish_expr_stmt (tree expr)
675 tree r = NULL_TREE;
676 location_t loc = EXPR_LOCATION (expr);
678 if (expr != NULL_TREE)
680 /* If we ran into a problem, make sure we complained. */
681 gcc_assert (expr != error_mark_node || seen_error ());
683 if (!processing_template_decl)
685 if (warn_sequence_point)
686 verify_sequence_points (expr);
687 expr = convert_to_void (expr, ICV_STATEMENT, tf_warning_or_error);
689 else if (!type_dependent_expression_p (expr))
690 convert_to_void (build_non_dependent_expr (expr), ICV_STATEMENT,
691 tf_warning_or_error);
693 if (check_for_bare_parameter_packs (expr))
694 expr = error_mark_node;
696 /* Simplification of inner statement expressions, compound exprs,
697 etc can result in us already having an EXPR_STMT. */
698 if (TREE_CODE (expr) != CLEANUP_POINT_EXPR)
700 if (TREE_CODE (expr) != EXPR_STMT)
701 expr = build_stmt (loc, EXPR_STMT, expr);
702 expr = maybe_cleanup_point_expr_void (expr);
705 r = add_stmt (expr);
708 return r;
712 /* Begin an if-statement. Returns a newly created IF_STMT if
713 appropriate. */
715 tree
716 begin_if_stmt (void)
718 tree r, scope;
719 scope = do_pushlevel (sk_cond);
720 r = build_stmt (input_location, IF_STMT, NULL_TREE,
721 NULL_TREE, NULL_TREE, scope);
722 current_binding_level->this_entity = r;
723 begin_cond (&IF_COND (r));
724 return r;
727 /* Process the COND of an if-statement, which may be given by
728 IF_STMT. */
730 tree
731 finish_if_stmt_cond (tree cond, tree if_stmt)
733 cond = maybe_convert_cond (cond);
734 if (IF_STMT_CONSTEXPR_P (if_stmt)
735 && !type_dependent_expression_p (cond)
736 && require_constant_expression (cond)
737 && !instantiation_dependent_expression_p (cond)
738 /* Wait until instantiation time, since only then COND has been
739 converted to bool. */
740 && TYPE_MAIN_VARIANT (TREE_TYPE (cond)) == boolean_type_node)
742 cond = instantiate_non_dependent_expr (cond);
743 cond = cxx_constant_value (cond, NULL_TREE);
745 finish_cond (&IF_COND (if_stmt), cond);
746 add_stmt (if_stmt);
747 THEN_CLAUSE (if_stmt) = push_stmt_list ();
748 return cond;
751 /* Finish the then-clause of an if-statement, which may be given by
752 IF_STMT. */
754 tree
755 finish_then_clause (tree if_stmt)
757 THEN_CLAUSE (if_stmt) = pop_stmt_list (THEN_CLAUSE (if_stmt));
758 return if_stmt;
761 /* Begin the else-clause of an if-statement. */
763 void
764 begin_else_clause (tree if_stmt)
766 ELSE_CLAUSE (if_stmt) = push_stmt_list ();
769 /* Finish the else-clause of an if-statement, which may be given by
770 IF_STMT. */
772 void
773 finish_else_clause (tree if_stmt)
775 ELSE_CLAUSE (if_stmt) = pop_stmt_list (ELSE_CLAUSE (if_stmt));
778 /* Finish an if-statement. */
780 void
781 finish_if_stmt (tree if_stmt)
783 tree scope = IF_SCOPE (if_stmt);
784 IF_SCOPE (if_stmt) = NULL;
785 add_stmt (do_poplevel (scope));
788 /* Begin a while-statement. Returns a newly created WHILE_STMT if
789 appropriate. */
791 tree
792 begin_while_stmt (void)
794 tree r;
795 r = build_stmt (input_location, WHILE_STMT, NULL_TREE, NULL_TREE);
796 add_stmt (r);
797 WHILE_BODY (r) = do_pushlevel (sk_block);
798 begin_cond (&WHILE_COND (r));
799 return r;
802 /* Process the COND of a while-statement, which may be given by
803 WHILE_STMT. */
805 void
806 finish_while_stmt_cond (tree cond, tree while_stmt, bool ivdep,
807 unsigned short unroll)
809 cond = maybe_convert_cond (cond);
810 finish_cond (&WHILE_COND (while_stmt), cond);
811 begin_maybe_infinite_loop (cond);
812 if (ivdep && cond != error_mark_node)
813 WHILE_COND (while_stmt) = build3 (ANNOTATE_EXPR,
814 TREE_TYPE (WHILE_COND (while_stmt)),
815 WHILE_COND (while_stmt),
816 build_int_cst (integer_type_node,
817 annot_expr_ivdep_kind),
818 integer_zero_node);
819 if (unroll && cond != error_mark_node)
820 WHILE_COND (while_stmt) = build3 (ANNOTATE_EXPR,
821 TREE_TYPE (WHILE_COND (while_stmt)),
822 WHILE_COND (while_stmt),
823 build_int_cst (integer_type_node,
824 annot_expr_unroll_kind),
825 build_int_cst (integer_type_node,
826 unroll));
827 simplify_loop_decl_cond (&WHILE_COND (while_stmt), WHILE_BODY (while_stmt));
830 /* Finish a while-statement, which may be given by WHILE_STMT. */
832 void
833 finish_while_stmt (tree while_stmt)
835 end_maybe_infinite_loop (boolean_true_node);
836 WHILE_BODY (while_stmt) = do_poplevel (WHILE_BODY (while_stmt));
839 /* Begin a do-statement. Returns a newly created DO_STMT if
840 appropriate. */
842 tree
843 begin_do_stmt (void)
845 tree r = build_stmt (input_location, DO_STMT, NULL_TREE, NULL_TREE);
846 begin_maybe_infinite_loop (boolean_true_node);
847 add_stmt (r);
848 DO_BODY (r) = push_stmt_list ();
849 return r;
852 /* Finish the body of a do-statement, which may be given by DO_STMT. */
854 void
855 finish_do_body (tree do_stmt)
857 tree body = DO_BODY (do_stmt) = pop_stmt_list (DO_BODY (do_stmt));
859 if (TREE_CODE (body) == STATEMENT_LIST && STATEMENT_LIST_TAIL (body))
860 body = STATEMENT_LIST_TAIL (body)->stmt;
862 if (IS_EMPTY_STMT (body))
863 warning (OPT_Wempty_body,
864 "suggest explicit braces around empty body in %<do%> statement");
867 /* Finish a do-statement, which may be given by DO_STMT, and whose
868 COND is as indicated. */
870 void
871 finish_do_stmt (tree cond, tree do_stmt, bool ivdep, unsigned short unroll)
873 cond = maybe_convert_cond (cond);
874 end_maybe_infinite_loop (cond);
875 if (ivdep && cond != error_mark_node)
876 cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
877 build_int_cst (integer_type_node, annot_expr_ivdep_kind),
878 integer_zero_node);
879 if (unroll && cond != error_mark_node)
880 cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
881 build_int_cst (integer_type_node, annot_expr_unroll_kind),
882 build_int_cst (integer_type_node, unroll));
883 DO_COND (do_stmt) = cond;
886 /* Finish a return-statement. The EXPRESSION returned, if any, is as
887 indicated. */
889 tree
890 finish_return_stmt (tree expr)
892 tree r;
893 bool no_warning;
895 expr = check_return_expr (expr, &no_warning);
897 if (error_operand_p (expr)
898 || (flag_openmp && !check_omp_return ()))
900 /* Suppress -Wreturn-type for this function. */
901 if (warn_return_type)
902 TREE_NO_WARNING (current_function_decl) = true;
903 return error_mark_node;
906 if (!processing_template_decl)
908 if (warn_sequence_point)
909 verify_sequence_points (expr);
911 if (DECL_DESTRUCTOR_P (current_function_decl)
912 || (DECL_CONSTRUCTOR_P (current_function_decl)
913 && targetm.cxx.cdtor_returns_this ()))
915 /* Similarly, all destructors must run destructors for
916 base-classes before returning. So, all returns in a
917 destructor get sent to the DTOR_LABEL; finish_function emits
918 code to return a value there. */
919 return finish_goto_stmt (cdtor_label);
923 r = build_stmt (input_location, RETURN_EXPR, expr);
924 TREE_NO_WARNING (r) |= no_warning;
925 r = maybe_cleanup_point_expr_void (r);
926 r = add_stmt (r);
928 return r;
931 /* Begin the scope of a for-statement or a range-for-statement.
932 Both the returned trees are to be used in a call to
933 begin_for_stmt or begin_range_for_stmt. */
935 tree
936 begin_for_scope (tree *init)
938 tree scope = do_pushlevel (sk_for);
940 if (processing_template_decl)
941 *init = push_stmt_list ();
942 else
943 *init = NULL_TREE;
945 return scope;
948 /* Begin a for-statement. Returns a new FOR_STMT.
949 SCOPE and INIT should be the return of begin_for_scope,
950 or both NULL_TREE */
952 tree
953 begin_for_stmt (tree scope, tree init)
955 tree r;
957 r = build_stmt (input_location, FOR_STMT, NULL_TREE, NULL_TREE,
958 NULL_TREE, NULL_TREE, NULL_TREE);
960 if (scope == NULL_TREE)
962 gcc_assert (!init);
963 scope = begin_for_scope (&init);
966 FOR_INIT_STMT (r) = init;
967 FOR_SCOPE (r) = scope;
969 return r;
972 /* Finish the init-statement of a for-statement, which may be
973 given by FOR_STMT. */
975 void
976 finish_init_stmt (tree for_stmt)
978 if (processing_template_decl)
979 FOR_INIT_STMT (for_stmt) = pop_stmt_list (FOR_INIT_STMT (for_stmt));
980 add_stmt (for_stmt);
981 FOR_BODY (for_stmt) = do_pushlevel (sk_block);
982 begin_cond (&FOR_COND (for_stmt));
985 /* Finish the COND of a for-statement, which may be given by
986 FOR_STMT. */
988 void
989 finish_for_cond (tree cond, tree for_stmt, bool ivdep, unsigned short unroll)
991 cond = maybe_convert_cond (cond);
992 finish_cond (&FOR_COND (for_stmt), cond);
993 begin_maybe_infinite_loop (cond);
994 if (ivdep && cond != error_mark_node)
995 FOR_COND (for_stmt) = build3 (ANNOTATE_EXPR,
996 TREE_TYPE (FOR_COND (for_stmt)),
997 FOR_COND (for_stmt),
998 build_int_cst (integer_type_node,
999 annot_expr_ivdep_kind),
1000 integer_zero_node);
1001 if (unroll && cond != error_mark_node)
1002 FOR_COND (for_stmt) = build3 (ANNOTATE_EXPR,
1003 TREE_TYPE (FOR_COND (for_stmt)),
1004 FOR_COND (for_stmt),
1005 build_int_cst (integer_type_node,
1006 annot_expr_unroll_kind),
1007 build_int_cst (integer_type_node,
1008 unroll));
1009 simplify_loop_decl_cond (&FOR_COND (for_stmt), FOR_BODY (for_stmt));
1012 /* Finish the increment-EXPRESSION in a for-statement, which may be
1013 given by FOR_STMT. */
1015 void
1016 finish_for_expr (tree expr, tree for_stmt)
1018 if (!expr)
1019 return;
1020 /* If EXPR is an overloaded function, issue an error; there is no
1021 context available to use to perform overload resolution. */
1022 if (type_unknown_p (expr))
1024 cxx_incomplete_type_error (expr, TREE_TYPE (expr));
1025 expr = error_mark_node;
1027 if (!processing_template_decl)
1029 if (warn_sequence_point)
1030 verify_sequence_points (expr);
1031 expr = convert_to_void (expr, ICV_THIRD_IN_FOR,
1032 tf_warning_or_error);
1034 else if (!type_dependent_expression_p (expr))
1035 convert_to_void (build_non_dependent_expr (expr), ICV_THIRD_IN_FOR,
1036 tf_warning_or_error);
1037 expr = maybe_cleanup_point_expr_void (expr);
1038 if (check_for_bare_parameter_packs (expr))
1039 expr = error_mark_node;
1040 FOR_EXPR (for_stmt) = expr;
1043 /* Finish the body of a for-statement, which may be given by
1044 FOR_STMT. The increment-EXPR for the loop must be
1045 provided.
1046 It can also finish RANGE_FOR_STMT. */
1048 void
1049 finish_for_stmt (tree for_stmt)
1051 end_maybe_infinite_loop (boolean_true_node);
1053 if (TREE_CODE (for_stmt) == RANGE_FOR_STMT)
1054 RANGE_FOR_BODY (for_stmt) = do_poplevel (RANGE_FOR_BODY (for_stmt));
1055 else
1056 FOR_BODY (for_stmt) = do_poplevel (FOR_BODY (for_stmt));
1058 /* Pop the scope for the body of the loop. */
1059 tree *scope_ptr = (TREE_CODE (for_stmt) == RANGE_FOR_STMT
1060 ? &RANGE_FOR_SCOPE (for_stmt)
1061 : &FOR_SCOPE (for_stmt));
1062 tree scope = *scope_ptr;
1063 *scope_ptr = NULL;
1065 /* During parsing of the body, range for uses "__for_{range,begin,end} "
1066 decl names to make those unaccessible by code in the body.
1067 Change it to ones with underscore instead of space, so that it can
1068 be inspected in the debugger. */
1069 tree range_for_decl[3] = { NULL_TREE, NULL_TREE, NULL_TREE };
1070 gcc_assert (CPTI_FOR_BEGIN__IDENTIFIER == CPTI_FOR_RANGE__IDENTIFIER + 1
1071 && CPTI_FOR_END__IDENTIFIER == CPTI_FOR_RANGE__IDENTIFIER + 2
1072 && CPTI_FOR_RANGE_IDENTIFIER == CPTI_FOR_RANGE__IDENTIFIER + 3
1073 && CPTI_FOR_BEGIN_IDENTIFIER == CPTI_FOR_BEGIN__IDENTIFIER + 3
1074 && CPTI_FOR_END_IDENTIFIER == CPTI_FOR_END__IDENTIFIER + 3);
1075 for (int i = 0; i < 3; i++)
1077 tree id = cp_global_trees[CPTI_FOR_RANGE__IDENTIFIER + i];
1078 if (IDENTIFIER_BINDING (id)
1079 && IDENTIFIER_BINDING (id)->scope == current_binding_level)
1081 range_for_decl[i] = IDENTIFIER_BINDING (id)->value;
1082 gcc_assert (VAR_P (range_for_decl[i])
1083 && DECL_ARTIFICIAL (range_for_decl[i]));
1087 add_stmt (do_poplevel (scope));
1089 for (int i = 0; i < 3; i++)
1090 if (range_for_decl[i])
1091 DECL_NAME (range_for_decl[i])
1092 = cp_global_trees[CPTI_FOR_RANGE_IDENTIFIER + i];
1095 /* Begin a range-for-statement. Returns a new RANGE_FOR_STMT.
1096 SCOPE and INIT should be the return of begin_for_scope,
1097 or both NULL_TREE .
1098 To finish it call finish_for_stmt(). */
1100 tree
1101 begin_range_for_stmt (tree scope, tree init)
1103 begin_maybe_infinite_loop (boolean_false_node);
1105 tree r = build_stmt (input_location, RANGE_FOR_STMT, NULL_TREE, NULL_TREE,
1106 NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE);
1108 if (scope == NULL_TREE)
1110 gcc_assert (!init);
1111 scope = begin_for_scope (&init);
1114 /* Since C++20, RANGE_FOR_STMTs can use the init tree, so save it. */
1115 RANGE_FOR_INIT_STMT (r) = init;
1116 RANGE_FOR_SCOPE (r) = scope;
1118 return r;
1121 /* Finish the head of a range-based for statement, which may
1122 be given by RANGE_FOR_STMT. DECL must be the declaration
1123 and EXPR must be the loop expression. */
1125 void
1126 finish_range_for_decl (tree range_for_stmt, tree decl, tree expr)
1128 if (processing_template_decl)
1129 RANGE_FOR_INIT_STMT (range_for_stmt)
1130 = pop_stmt_list (RANGE_FOR_INIT_STMT (range_for_stmt));
1131 RANGE_FOR_DECL (range_for_stmt) = decl;
1132 RANGE_FOR_EXPR (range_for_stmt) = expr;
1133 add_stmt (range_for_stmt);
1134 RANGE_FOR_BODY (range_for_stmt) = do_pushlevel (sk_block);
1137 /* Finish a break-statement. */
1139 tree
1140 finish_break_stmt (void)
1142 /* In switch statements break is sometimes stylistically used after
1143 a return statement. This can lead to spurious warnings about
1144 control reaching the end of a non-void function when it is
1145 inlined. Note that we are calling block_may_fallthru with
1146 language specific tree nodes; this works because
1147 block_may_fallthru returns true when given something it does not
1148 understand. */
1149 if (!block_may_fallthru (cur_stmt_list))
1150 return void_node;
1151 note_break_stmt ();
1152 return add_stmt (build_stmt (input_location, BREAK_STMT));
1155 /* Finish a continue-statement. */
1157 tree
1158 finish_continue_stmt (void)
1160 return add_stmt (build_stmt (input_location, CONTINUE_STMT));
1163 /* Begin a switch-statement. Returns a new SWITCH_STMT if
1164 appropriate. */
1166 tree
1167 begin_switch_stmt (void)
1169 tree r, scope;
1171 scope = do_pushlevel (sk_cond);
1172 r = build_stmt (input_location, SWITCH_STMT, NULL_TREE, NULL_TREE, NULL_TREE, scope);
1174 begin_cond (&SWITCH_STMT_COND (r));
1176 return r;
1179 /* Finish the cond of a switch-statement. */
1181 void
1182 finish_switch_cond (tree cond, tree switch_stmt)
1184 tree orig_type = NULL;
1186 if (!processing_template_decl)
1188 /* Convert the condition to an integer or enumeration type. */
1189 cond = build_expr_type_conversion (WANT_INT | WANT_ENUM, cond, true);
1190 if (cond == NULL_TREE)
1192 error ("switch quantity not an integer");
1193 cond = error_mark_node;
1195 /* We want unlowered type here to handle enum bit-fields. */
1196 orig_type = unlowered_expr_type (cond);
1197 if (TREE_CODE (orig_type) != ENUMERAL_TYPE)
1198 orig_type = TREE_TYPE (cond);
1199 if (cond != error_mark_node)
1201 /* [stmt.switch]
1203 Integral promotions are performed. */
1204 cond = perform_integral_promotions (cond);
1205 cond = maybe_cleanup_point_expr (cond);
1208 if (check_for_bare_parameter_packs (cond))
1209 cond = error_mark_node;
1210 else if (!processing_template_decl && warn_sequence_point)
1211 verify_sequence_points (cond);
1213 finish_cond (&SWITCH_STMT_COND (switch_stmt), cond);
1214 SWITCH_STMT_TYPE (switch_stmt) = orig_type;
1215 add_stmt (switch_stmt);
1216 push_switch (switch_stmt);
1217 SWITCH_STMT_BODY (switch_stmt) = push_stmt_list ();
1220 /* Finish the body of a switch-statement, which may be given by
1221 SWITCH_STMT. The COND to switch on is indicated. */
1223 void
1224 finish_switch_stmt (tree switch_stmt)
1226 tree scope;
1228 SWITCH_STMT_BODY (switch_stmt) =
1229 pop_stmt_list (SWITCH_STMT_BODY (switch_stmt));
1230 pop_switch ();
1232 scope = SWITCH_STMT_SCOPE (switch_stmt);
1233 SWITCH_STMT_SCOPE (switch_stmt) = NULL;
1234 add_stmt (do_poplevel (scope));
1237 /* Begin a try-block. Returns a newly-created TRY_BLOCK if
1238 appropriate. */
1240 tree
1241 begin_try_block (void)
1243 tree r = build_stmt (input_location, TRY_BLOCK, NULL_TREE, NULL_TREE);
1244 add_stmt (r);
1245 TRY_STMTS (r) = push_stmt_list ();
1246 return r;
1249 /* Likewise, for a function-try-block. The block returned in
1250 *COMPOUND_STMT is an artificial outer scope, containing the
1251 function-try-block. */
1253 tree
1254 begin_function_try_block (tree *compound_stmt)
1256 tree r;
1257 /* This outer scope does not exist in the C++ standard, but we need
1258 a place to put __FUNCTION__ and similar variables. */
1259 *compound_stmt = begin_compound_stmt (0);
1260 r = begin_try_block ();
1261 FN_TRY_BLOCK_P (r) = 1;
1262 return r;
1265 /* Finish a try-block, which may be given by TRY_BLOCK. */
1267 void
1268 finish_try_block (tree try_block)
1270 TRY_STMTS (try_block) = pop_stmt_list (TRY_STMTS (try_block));
1271 TRY_HANDLERS (try_block) = push_stmt_list ();
1274 /* Finish the body of a cleanup try-block, which may be given by
1275 TRY_BLOCK. */
1277 void
1278 finish_cleanup_try_block (tree try_block)
1280 TRY_STMTS (try_block) = pop_stmt_list (TRY_STMTS (try_block));
1283 /* Finish an implicitly generated try-block, with a cleanup is given
1284 by CLEANUP. */
1286 void
1287 finish_cleanup (tree cleanup, tree try_block)
1289 TRY_HANDLERS (try_block) = cleanup;
1290 CLEANUP_P (try_block) = 1;
1293 /* Likewise, for a function-try-block. */
1295 void
1296 finish_function_try_block (tree try_block)
1298 finish_try_block (try_block);
1299 /* FIXME : something queer about CTOR_INITIALIZER somehow following
1300 the try block, but moving it inside. */
1301 in_function_try_handler = 1;
1304 /* Finish a handler-sequence for a try-block, which may be given by
1305 TRY_BLOCK. */
1307 void
1308 finish_handler_sequence (tree try_block)
1310 TRY_HANDLERS (try_block) = pop_stmt_list (TRY_HANDLERS (try_block));
1311 check_handlers (TRY_HANDLERS (try_block));
1314 /* Finish the handler-seq for a function-try-block, given by
1315 TRY_BLOCK. COMPOUND_STMT is the outer block created by
1316 begin_function_try_block. */
1318 void
1319 finish_function_handler_sequence (tree try_block, tree compound_stmt)
1321 in_function_try_handler = 0;
1322 finish_handler_sequence (try_block);
1323 finish_compound_stmt (compound_stmt);
1326 /* Begin a handler. Returns a HANDLER if appropriate. */
1328 tree
1329 begin_handler (void)
1331 tree r;
1333 r = build_stmt (input_location, HANDLER, NULL_TREE, NULL_TREE);
1334 add_stmt (r);
1336 /* Create a binding level for the eh_info and the exception object
1337 cleanup. */
1338 HANDLER_BODY (r) = do_pushlevel (sk_catch);
1340 return r;
1343 /* Finish the handler-parameters for a handler, which may be given by
1344 HANDLER. DECL is the declaration for the catch parameter, or NULL
1345 if this is a `catch (...)' clause. */
1347 void
1348 finish_handler_parms (tree decl, tree handler)
1350 tree type = NULL_TREE;
1351 if (processing_template_decl)
1353 if (decl)
1355 decl = pushdecl (decl);
1356 decl = push_template_decl (decl);
1357 HANDLER_PARMS (handler) = decl;
1358 type = TREE_TYPE (decl);
1361 else
1363 type = expand_start_catch_block (decl);
1364 if (warn_catch_value
1365 && type != NULL_TREE
1366 && type != error_mark_node
1367 && !TYPE_REF_P (TREE_TYPE (decl)))
1369 tree orig_type = TREE_TYPE (decl);
1370 if (CLASS_TYPE_P (orig_type))
1372 if (TYPE_POLYMORPHIC_P (orig_type))
1373 warning (OPT_Wcatch_value_,
1374 "catching polymorphic type %q#T by value", orig_type);
1375 else if (warn_catch_value > 1)
1376 warning (OPT_Wcatch_value_,
1377 "catching type %q#T by value", orig_type);
1379 else if (warn_catch_value > 2)
1380 warning (OPT_Wcatch_value_,
1381 "catching non-reference type %q#T", orig_type);
1384 HANDLER_TYPE (handler) = type;
1387 /* Finish a handler, which may be given by HANDLER. The BLOCKs are
1388 the return value from the matching call to finish_handler_parms. */
1390 void
1391 finish_handler (tree handler)
1393 if (!processing_template_decl)
1394 expand_end_catch_block ();
1395 HANDLER_BODY (handler) = do_poplevel (HANDLER_BODY (handler));
1398 /* Begin a compound statement. FLAGS contains some bits that control the
1399 behavior and context. If BCS_NO_SCOPE is set, the compound statement
1400 does not define a scope. If BCS_FN_BODY is set, this is the outermost
1401 block of a function. If BCS_TRY_BLOCK is set, this is the block
1402 created on behalf of a TRY statement. Returns a token to be passed to
1403 finish_compound_stmt. */
1405 tree
1406 begin_compound_stmt (unsigned int flags)
1408 tree r;
1410 if (flags & BCS_NO_SCOPE)
1412 r = push_stmt_list ();
1413 STATEMENT_LIST_NO_SCOPE (r) = 1;
1415 /* Normally, we try hard to keep the BLOCK for a statement-expression.
1416 But, if it's a statement-expression with a scopeless block, there's
1417 nothing to keep, and we don't want to accidentally keep a block
1418 *inside* the scopeless block. */
1419 keep_next_level (false);
1421 else
1423 scope_kind sk = sk_block;
1424 if (flags & BCS_TRY_BLOCK)
1425 sk = sk_try;
1426 else if (flags & BCS_TRANSACTION)
1427 sk = sk_transaction;
1428 r = do_pushlevel (sk);
1431 /* When processing a template, we need to remember where the braces were,
1432 so that we can set up identical scopes when instantiating the template
1433 later. BIND_EXPR is a handy candidate for this.
1434 Note that do_poplevel won't create a BIND_EXPR itself here (and thus
1435 result in nested BIND_EXPRs), since we don't build BLOCK nodes when
1436 processing templates. */
1437 if (processing_template_decl)
1439 r = build3 (BIND_EXPR, NULL, NULL, r, NULL);
1440 BIND_EXPR_TRY_BLOCK (r) = (flags & BCS_TRY_BLOCK) != 0;
1441 BIND_EXPR_BODY_BLOCK (r) = (flags & BCS_FN_BODY) != 0;
1442 TREE_SIDE_EFFECTS (r) = 1;
1445 return r;
1448 /* Finish a compound-statement, which is given by STMT. */
1450 void
1451 finish_compound_stmt (tree stmt)
1453 if (TREE_CODE (stmt) == BIND_EXPR)
1455 tree body = do_poplevel (BIND_EXPR_BODY (stmt));
1456 /* If the STATEMENT_LIST is empty and this BIND_EXPR isn't special,
1457 discard the BIND_EXPR so it can be merged with the containing
1458 STATEMENT_LIST. */
1459 if (TREE_CODE (body) == STATEMENT_LIST
1460 && STATEMENT_LIST_HEAD (body) == NULL
1461 && !BIND_EXPR_BODY_BLOCK (stmt)
1462 && !BIND_EXPR_TRY_BLOCK (stmt))
1463 stmt = body;
1464 else
1465 BIND_EXPR_BODY (stmt) = body;
1467 else if (STATEMENT_LIST_NO_SCOPE (stmt))
1468 stmt = pop_stmt_list (stmt);
1469 else
1471 /* Destroy any ObjC "super" receivers that may have been
1472 created. */
1473 objc_clear_super_receiver ();
1475 stmt = do_poplevel (stmt);
1478 /* ??? See c_end_compound_stmt wrt statement expressions. */
1479 add_stmt (stmt);
1482 /* Finish an asm-statement, whose components are a STRING, some
1483 OUTPUT_OPERANDS, some INPUT_OPERANDS, some CLOBBERS and some
1484 LABELS. Also note whether the asm-statement should be
1485 considered volatile. */
1487 tree
1488 finish_asm_stmt (int volatile_p, tree string, tree output_operands,
1489 tree input_operands, tree clobbers, tree labels)
1491 tree r;
1492 tree t;
1493 int ninputs = list_length (input_operands);
1494 int noutputs = list_length (output_operands);
1496 if (!processing_template_decl)
1498 const char *constraint;
1499 const char **oconstraints;
1500 bool allows_mem, allows_reg, is_inout;
1501 tree operand;
1502 int i;
1504 oconstraints = XALLOCAVEC (const char *, noutputs);
1506 string = resolve_asm_operand_names (string, output_operands,
1507 input_operands, labels);
1509 for (i = 0, t = output_operands; t; t = TREE_CHAIN (t), ++i)
1511 operand = TREE_VALUE (t);
1513 /* ??? Really, this should not be here. Users should be using a
1514 proper lvalue, dammit. But there's a long history of using
1515 casts in the output operands. In cases like longlong.h, this
1516 becomes a primitive form of typechecking -- if the cast can be
1517 removed, then the output operand had a type of the proper width;
1518 otherwise we'll get an error. Gross, but ... */
1519 STRIP_NOPS (operand);
1521 operand = mark_lvalue_use (operand);
1523 if (!lvalue_or_else (operand, lv_asm, tf_warning_or_error))
1524 operand = error_mark_node;
1526 if (operand != error_mark_node
1527 && (TREE_READONLY (operand)
1528 || CP_TYPE_CONST_P (TREE_TYPE (operand))
1529 /* Functions are not modifiable, even though they are
1530 lvalues. */
1531 || TREE_CODE (TREE_TYPE (operand)) == FUNCTION_TYPE
1532 || TREE_CODE (TREE_TYPE (operand)) == METHOD_TYPE
1533 /* If it's an aggregate and any field is const, then it is
1534 effectively const. */
1535 || (CLASS_TYPE_P (TREE_TYPE (operand))
1536 && C_TYPE_FIELDS_READONLY (TREE_TYPE (operand)))))
1537 cxx_readonly_error (input_location, operand, lv_asm);
1539 tree *op = &operand;
1540 while (TREE_CODE (*op) == COMPOUND_EXPR)
1541 op = &TREE_OPERAND (*op, 1);
1542 switch (TREE_CODE (*op))
1544 case PREINCREMENT_EXPR:
1545 case PREDECREMENT_EXPR:
1546 case MODIFY_EXPR:
1547 *op = genericize_compound_lvalue (*op);
1548 op = &TREE_OPERAND (*op, 1);
1549 break;
1550 default:
1551 break;
1554 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
1555 oconstraints[i] = constraint;
1557 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
1558 &allows_mem, &allows_reg, &is_inout))
1560 /* If the operand is going to end up in memory,
1561 mark it addressable. */
1562 if (!allows_reg && !cxx_mark_addressable (*op))
1563 operand = error_mark_node;
1565 else
1566 operand = error_mark_node;
1568 TREE_VALUE (t) = operand;
1571 for (i = 0, t = input_operands; t; ++i, t = TREE_CHAIN (t))
1573 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
1574 bool constraint_parsed
1575 = parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
1576 oconstraints, &allows_mem, &allows_reg);
1577 /* If the operand is going to end up in memory, don't call
1578 decay_conversion. */
1579 if (constraint_parsed && !allows_reg && allows_mem)
1580 operand = mark_lvalue_use (TREE_VALUE (t));
1581 else
1582 operand = decay_conversion (TREE_VALUE (t), tf_warning_or_error);
1584 /* If the type of the operand hasn't been determined (e.g.,
1585 because it involves an overloaded function), then issue
1586 an error message. There's no context available to
1587 resolve the overloading. */
1588 if (TREE_TYPE (operand) == unknown_type_node)
1590 error ("type of asm operand %qE could not be determined",
1591 TREE_VALUE (t));
1592 operand = error_mark_node;
1595 if (constraint_parsed)
1597 /* If the operand is going to end up in memory,
1598 mark it addressable. */
1599 if (!allows_reg && allows_mem)
1601 /* Strip the nops as we allow this case. FIXME, this really
1602 should be rejected or made deprecated. */
1603 STRIP_NOPS (operand);
1605 tree *op = &operand;
1606 while (TREE_CODE (*op) == COMPOUND_EXPR)
1607 op = &TREE_OPERAND (*op, 1);
1608 switch (TREE_CODE (*op))
1610 case PREINCREMENT_EXPR:
1611 case PREDECREMENT_EXPR:
1612 case MODIFY_EXPR:
1613 *op = genericize_compound_lvalue (*op);
1614 op = &TREE_OPERAND (*op, 1);
1615 break;
1616 default:
1617 break;
1620 if (!cxx_mark_addressable (*op))
1621 operand = error_mark_node;
1623 else if (!allows_reg && !allows_mem)
1625 /* If constraint allows neither register nor memory,
1626 try harder to get a constant. */
1627 tree constop = maybe_constant_value (operand);
1628 if (TREE_CONSTANT (constop))
1629 operand = constop;
1632 else
1633 operand = error_mark_node;
1635 TREE_VALUE (t) = operand;
1639 r = build_stmt (input_location, ASM_EXPR, string,
1640 output_operands, input_operands,
1641 clobbers, labels);
1642 ASM_VOLATILE_P (r) = volatile_p || noutputs == 0;
1643 r = maybe_cleanup_point_expr_void (r);
1644 return add_stmt (r);
1647 /* Finish a label with the indicated NAME. Returns the new label. */
1649 tree
1650 finish_label_stmt (tree name)
1652 tree decl = define_label (input_location, name);
1654 if (decl == error_mark_node)
1655 return error_mark_node;
1657 add_stmt (build_stmt (input_location, LABEL_EXPR, decl));
1659 return decl;
1662 /* Finish a series of declarations for local labels. G++ allows users
1663 to declare "local" labels, i.e., labels with scope. This extension
1664 is useful when writing code involving statement-expressions. */
1666 void
1667 finish_label_decl (tree name)
1669 if (!at_function_scope_p ())
1671 error ("__label__ declarations are only allowed in function scopes");
1672 return;
1675 add_decl_expr (declare_local_label (name));
1678 /* When DECL goes out of scope, make sure that CLEANUP is executed. */
1680 void
1681 finish_decl_cleanup (tree decl, tree cleanup)
1683 push_cleanup (decl, cleanup, false);
1686 /* If the current scope exits with an exception, run CLEANUP. */
1688 void
1689 finish_eh_cleanup (tree cleanup)
1691 push_cleanup (NULL, cleanup, true);
1694 /* The MEM_INITS is a list of mem-initializers, in reverse of the
1695 order they were written by the user. Each node is as for
1696 emit_mem_initializers. */
1698 void
1699 finish_mem_initializers (tree mem_inits)
1701 /* Reorder the MEM_INITS so that they are in the order they appeared
1702 in the source program. */
1703 mem_inits = nreverse (mem_inits);
1705 if (processing_template_decl)
1707 tree mem;
1709 for (mem = mem_inits; mem; mem = TREE_CHAIN (mem))
1711 /* If the TREE_PURPOSE is a TYPE_PACK_EXPANSION, skip the
1712 check for bare parameter packs in the TREE_VALUE, because
1713 any parameter packs in the TREE_VALUE have already been
1714 bound as part of the TREE_PURPOSE. See
1715 make_pack_expansion for more information. */
1716 if (TREE_CODE (TREE_PURPOSE (mem)) != TYPE_PACK_EXPANSION
1717 && check_for_bare_parameter_packs (TREE_VALUE (mem)))
1718 TREE_VALUE (mem) = error_mark_node;
1721 add_stmt (build_min_nt_loc (UNKNOWN_LOCATION,
1722 CTOR_INITIALIZER, mem_inits));
1724 else
1725 emit_mem_initializers (mem_inits);
1728 /* Obfuscate EXPR if it looks like an id-expression or member access so
1729 that the call to finish_decltype in do_auto_deduction will give the
1730 right result. */
1732 tree
1733 force_paren_expr (tree expr)
1735 /* This is only needed for decltype(auto) in C++14. */
1736 if (cxx_dialect < cxx14)
1737 return expr;
1739 /* If we're in unevaluated context, we can't be deducing a
1740 return/initializer type, so we don't need to mess with this. */
1741 if (cp_unevaluated_operand)
1742 return expr;
1744 if (!DECL_P (expr) && TREE_CODE (expr) != COMPONENT_REF
1745 && TREE_CODE (expr) != SCOPE_REF)
1746 return expr;
1748 if (TREE_CODE (expr) == COMPONENT_REF
1749 || TREE_CODE (expr) == SCOPE_REF)
1750 REF_PARENTHESIZED_P (expr) = true;
1751 else if (processing_template_decl)
1752 expr = build1 (PAREN_EXPR, TREE_TYPE (expr), expr);
1753 else
1755 expr = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (expr), expr);
1756 REF_PARENTHESIZED_P (expr) = true;
1759 return expr;
1762 /* If T is an id-expression obfuscated by force_paren_expr, undo the
1763 obfuscation and return the underlying id-expression. Otherwise
1764 return T. */
1766 tree
1767 maybe_undo_parenthesized_ref (tree t)
1769 if (cxx_dialect < cxx14)
1770 return t;
1772 if (INDIRECT_REF_P (t) && REF_PARENTHESIZED_P (t))
1774 t = TREE_OPERAND (t, 0);
1775 while (TREE_CODE (t) == NON_LVALUE_EXPR
1776 || TREE_CODE (t) == NOP_EXPR)
1777 t = TREE_OPERAND (t, 0);
1779 gcc_assert (TREE_CODE (t) == ADDR_EXPR
1780 || TREE_CODE (t) == STATIC_CAST_EXPR);
1781 t = TREE_OPERAND (t, 0);
1783 else if (TREE_CODE (t) == PAREN_EXPR)
1784 t = TREE_OPERAND (t, 0);
1785 else if (TREE_CODE (t) == VIEW_CONVERT_EXPR
1786 && REF_PARENTHESIZED_P (t))
1787 t = TREE_OPERAND (t, 0);
1789 return t;
1792 /* Finish a parenthesized expression EXPR. */
1794 cp_expr
1795 finish_parenthesized_expr (cp_expr expr)
1797 if (EXPR_P (expr))
1798 /* This inhibits warnings in c_common_truthvalue_conversion. */
1799 TREE_NO_WARNING (expr) = 1;
1801 if (TREE_CODE (expr) == OFFSET_REF
1802 || TREE_CODE (expr) == SCOPE_REF)
1803 /* [expr.unary.op]/3 The qualified id of a pointer-to-member must not be
1804 enclosed in parentheses. */
1805 PTRMEM_OK_P (expr) = 0;
1807 if (TREE_CODE (expr) == STRING_CST)
1808 PAREN_STRING_LITERAL_P (expr) = 1;
1810 expr = cp_expr (force_paren_expr (expr), expr.get_location ());
1812 return expr;
1815 /* Finish a reference to a non-static data member (DECL) that is not
1816 preceded by `.' or `->'. */
1818 tree
1819 finish_non_static_data_member (tree decl, tree object, tree qualifying_scope)
1821 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
1822 bool try_omp_private = !object && omp_private_member_map;
1823 tree ret;
1825 if (!object)
1827 tree scope = qualifying_scope;
1828 if (scope == NULL_TREE)
1829 scope = context_for_name_lookup (decl);
1830 object = maybe_dummy_object (scope, NULL);
1833 object = maybe_resolve_dummy (object, true);
1834 if (object == error_mark_node)
1835 return error_mark_node;
1837 /* DR 613/850: Can use non-static data members without an associated
1838 object in sizeof/decltype/alignof. */
1839 if (is_dummy_object (object) && cp_unevaluated_operand == 0
1840 && (!processing_template_decl || !current_class_ref))
1842 if (current_function_decl
1843 && DECL_STATIC_FUNCTION_P (current_function_decl))
1844 error ("invalid use of member %qD in static member function", decl);
1845 else
1846 error ("invalid use of non-static data member %qD", decl);
1847 inform (DECL_SOURCE_LOCATION (decl), "declared here");
1849 return error_mark_node;
1852 if (current_class_ptr)
1853 TREE_USED (current_class_ptr) = 1;
1854 if (processing_template_decl && !qualifying_scope)
1856 tree type = TREE_TYPE (decl);
1858 if (TYPE_REF_P (type))
1859 /* Quals on the object don't matter. */;
1860 else if (PACK_EXPANSION_P (type))
1861 /* Don't bother trying to represent this. */
1862 type = NULL_TREE;
1863 else
1865 /* Set the cv qualifiers. */
1866 int quals = cp_type_quals (TREE_TYPE (object));
1868 if (DECL_MUTABLE_P (decl))
1869 quals &= ~TYPE_QUAL_CONST;
1871 quals |= cp_type_quals (TREE_TYPE (decl));
1872 type = cp_build_qualified_type (type, quals);
1875 ret = (convert_from_reference
1876 (build_min (COMPONENT_REF, type, object, decl, NULL_TREE)));
1878 /* If PROCESSING_TEMPLATE_DECL is nonzero here, then
1879 QUALIFYING_SCOPE is also non-null. Wrap this in a SCOPE_REF
1880 for now. */
1881 else if (processing_template_decl)
1882 ret = build_qualified_name (TREE_TYPE (decl),
1883 qualifying_scope,
1884 decl,
1885 /*template_p=*/false);
1886 else
1888 tree access_type = TREE_TYPE (object);
1890 perform_or_defer_access_check (TYPE_BINFO (access_type), decl,
1891 decl, tf_warning_or_error);
1893 /* If the data member was named `C::M', convert `*this' to `C'
1894 first. */
1895 if (qualifying_scope)
1897 tree binfo = NULL_TREE;
1898 object = build_scoped_ref (object, qualifying_scope,
1899 &binfo);
1902 ret = build_class_member_access_expr (object, decl,
1903 /*access_path=*/NULL_TREE,
1904 /*preserve_reference=*/false,
1905 tf_warning_or_error);
1907 if (try_omp_private)
1909 tree *v = omp_private_member_map->get (decl);
1910 if (v)
1911 ret = convert_from_reference (*v);
1913 return ret;
1916 /* If we are currently parsing a template and we encountered a typedef
1917 TYPEDEF_DECL that is being accessed though CONTEXT, this function
1918 adds the typedef to a list tied to the current template.
1919 At template instantiation time, that list is walked and access check
1920 performed for each typedef.
1921 LOCATION is the location of the usage point of TYPEDEF_DECL. */
1923 void
1924 add_typedef_to_current_template_for_access_check (tree typedef_decl,
1925 tree context,
1926 location_t location)
1928 tree template_info = NULL;
1929 tree cs = current_scope ();
1931 if (!is_typedef_decl (typedef_decl)
1932 || !context
1933 || !CLASS_TYPE_P (context)
1934 || !cs)
1935 return;
1937 if (CLASS_TYPE_P (cs) || TREE_CODE (cs) == FUNCTION_DECL)
1938 template_info = get_template_info (cs);
1940 if (template_info
1941 && TI_TEMPLATE (template_info)
1942 && !currently_open_class (context))
1943 append_type_to_template_for_access_check (cs, typedef_decl,
1944 context, location);
1947 /* DECL was the declaration to which a qualified-id resolved. Issue
1948 an error message if it is not accessible. If OBJECT_TYPE is
1949 non-NULL, we have just seen `x->' or `x.' and OBJECT_TYPE is the
1950 type of `*x', or `x', respectively. If the DECL was named as
1951 `A::B' then NESTED_NAME_SPECIFIER is `A'. */
1953 void
1954 check_accessibility_of_qualified_id (tree decl,
1955 tree object_type,
1956 tree nested_name_specifier)
1958 tree scope;
1959 tree qualifying_type = NULL_TREE;
1961 /* If we are parsing a template declaration and if decl is a typedef,
1962 add it to a list tied to the template.
1963 At template instantiation time, that list will be walked and
1964 access check performed. */
1965 add_typedef_to_current_template_for_access_check (decl,
1966 nested_name_specifier
1967 ? nested_name_specifier
1968 : DECL_CONTEXT (decl),
1969 input_location);
1971 /* If we're not checking, return immediately. */
1972 if (deferred_access_no_check)
1973 return;
1975 /* Determine the SCOPE of DECL. */
1976 scope = context_for_name_lookup (decl);
1977 /* If the SCOPE is not a type, then DECL is not a member. */
1978 if (!TYPE_P (scope))
1979 return;
1980 /* Compute the scope through which DECL is being accessed. */
1981 if (object_type
1982 /* OBJECT_TYPE might not be a class type; consider:
1984 class A { typedef int I; };
1985 I *p;
1986 p->A::I::~I();
1988 In this case, we will have "A::I" as the DECL, but "I" as the
1989 OBJECT_TYPE. */
1990 && CLASS_TYPE_P (object_type)
1991 && DERIVED_FROM_P (scope, object_type))
1992 /* If we are processing a `->' or `.' expression, use the type of the
1993 left-hand side. */
1994 qualifying_type = object_type;
1995 else if (nested_name_specifier)
1997 /* If the reference is to a non-static member of the
1998 current class, treat it as if it were referenced through
1999 `this'. */
2000 tree ct;
2001 if (DECL_NONSTATIC_MEMBER_P (decl)
2002 && current_class_ptr
2003 && DERIVED_FROM_P (scope, ct = current_nonlambda_class_type ()))
2004 qualifying_type = ct;
2005 /* Otherwise, use the type indicated by the
2006 nested-name-specifier. */
2007 else
2008 qualifying_type = nested_name_specifier;
2010 else
2011 /* Otherwise, the name must be from the current class or one of
2012 its bases. */
2013 qualifying_type = currently_open_derived_class (scope);
2015 if (qualifying_type
2016 /* It is possible for qualifying type to be a TEMPLATE_TYPE_PARM
2017 or similar in a default argument value. */
2018 && CLASS_TYPE_P (qualifying_type)
2019 && !dependent_type_p (qualifying_type))
2020 perform_or_defer_access_check (TYPE_BINFO (qualifying_type), decl,
2021 decl, tf_warning_or_error);
2024 /* EXPR is the result of a qualified-id. The QUALIFYING_CLASS was the
2025 class named to the left of the "::" operator. DONE is true if this
2026 expression is a complete postfix-expression; it is false if this
2027 expression is followed by '->', '[', '(', etc. ADDRESS_P is true
2028 iff this expression is the operand of '&'. TEMPLATE_P is true iff
2029 the qualified-id was of the form "A::template B". TEMPLATE_ARG_P
2030 is true iff this qualified name appears as a template argument. */
2032 tree
2033 finish_qualified_id_expr (tree qualifying_class,
2034 tree expr,
2035 bool done,
2036 bool address_p,
2037 bool template_p,
2038 bool template_arg_p,
2039 tsubst_flags_t complain)
2041 gcc_assert (TYPE_P (qualifying_class));
2043 if (error_operand_p (expr))
2044 return error_mark_node;
2046 if ((DECL_P (expr) || BASELINK_P (expr))
2047 && !mark_used (expr, complain))
2048 return error_mark_node;
2050 if (template_p)
2052 if (TREE_CODE (expr) == UNBOUND_CLASS_TEMPLATE)
2054 /* cp_parser_lookup_name thought we were looking for a type,
2055 but we're actually looking for a declaration. */
2056 qualifying_class = TYPE_CONTEXT (expr);
2057 expr = TYPE_IDENTIFIER (expr);
2059 else
2060 check_template_keyword (expr);
2063 /* If EXPR occurs as the operand of '&', use special handling that
2064 permits a pointer-to-member. */
2065 if (address_p && done)
2067 if (TREE_CODE (expr) == SCOPE_REF)
2068 expr = TREE_OPERAND (expr, 1);
2069 expr = build_offset_ref (qualifying_class, expr,
2070 /*address_p=*/true, complain);
2071 return expr;
2074 /* No need to check access within an enum. */
2075 if (TREE_CODE (qualifying_class) == ENUMERAL_TYPE
2076 && TREE_CODE (expr) != IDENTIFIER_NODE)
2077 return expr;
2079 /* Within the scope of a class, turn references to non-static
2080 members into expression of the form "this->...". */
2081 if (template_arg_p)
2082 /* But, within a template argument, we do not want make the
2083 transformation, as there is no "this" pointer. */
2085 else if (TREE_CODE (expr) == FIELD_DECL)
2087 push_deferring_access_checks (dk_no_check);
2088 expr = finish_non_static_data_member (expr, NULL_TREE,
2089 qualifying_class);
2090 pop_deferring_access_checks ();
2092 else if (BASELINK_P (expr))
2094 /* See if any of the functions are non-static members. */
2095 /* If so, the expression may be relative to 'this'. */
2096 if (!shared_member_p (expr)
2097 && current_class_ptr
2098 && DERIVED_FROM_P (qualifying_class,
2099 current_nonlambda_class_type ()))
2100 expr = (build_class_member_access_expr
2101 (maybe_dummy_object (qualifying_class, NULL),
2102 expr,
2103 BASELINK_ACCESS_BINFO (expr),
2104 /*preserve_reference=*/false,
2105 complain));
2106 else if (done)
2107 /* The expression is a qualified name whose address is not
2108 being taken. */
2109 expr = build_offset_ref (qualifying_class, expr, /*address_p=*/false,
2110 complain);
2112 else
2114 /* In a template, return a SCOPE_REF for most qualified-ids
2115 so that we can check access at instantiation time. But if
2116 we're looking at a member of the current instantiation, we
2117 know we have access and building up the SCOPE_REF confuses
2118 non-type template argument handling. */
2119 if (processing_template_decl
2120 && (!currently_open_class (qualifying_class)
2121 || TREE_CODE (expr) == IDENTIFIER_NODE
2122 || TREE_CODE (expr) == TEMPLATE_ID_EXPR
2123 || TREE_CODE (expr) == BIT_NOT_EXPR))
2124 expr = build_qualified_name (TREE_TYPE (expr),
2125 qualifying_class, expr,
2126 template_p);
2128 expr = convert_from_reference (expr);
2131 return expr;
2134 /* Begin a statement-expression. The value returned must be passed to
2135 finish_stmt_expr. */
2137 tree
2138 begin_stmt_expr (void)
2140 return push_stmt_list ();
2143 /* Process the final expression of a statement expression. EXPR can be
2144 NULL, if the final expression is empty. Return a STATEMENT_LIST
2145 containing all the statements in the statement-expression, or
2146 ERROR_MARK_NODE if there was an error. */
2148 tree
2149 finish_stmt_expr_expr (tree expr, tree stmt_expr)
2151 if (error_operand_p (expr))
2153 /* The type of the statement-expression is the type of the last
2154 expression. */
2155 TREE_TYPE (stmt_expr) = error_mark_node;
2156 return error_mark_node;
2159 /* If the last statement does not have "void" type, then the value
2160 of the last statement is the value of the entire expression. */
2161 if (expr)
2163 tree type = TREE_TYPE (expr);
2165 if (type && type_unknown_p (type))
2167 error ("a statement expression is an insufficient context"
2168 " for overload resolution");
2169 TREE_TYPE (stmt_expr) = error_mark_node;
2170 return error_mark_node;
2172 else if (processing_template_decl)
2174 expr = build_stmt (input_location, EXPR_STMT, expr);
2175 expr = add_stmt (expr);
2176 /* Mark the last statement so that we can recognize it as such at
2177 template-instantiation time. */
2178 EXPR_STMT_STMT_EXPR_RESULT (expr) = 1;
2180 else if (VOID_TYPE_P (type))
2182 /* Just treat this like an ordinary statement. */
2183 expr = finish_expr_stmt (expr);
2185 else
2187 /* It actually has a value we need to deal with. First, force it
2188 to be an rvalue so that we won't need to build up a copy
2189 constructor call later when we try to assign it to something. */
2190 expr = force_rvalue (expr, tf_warning_or_error);
2191 if (error_operand_p (expr))
2192 return error_mark_node;
2194 /* Update for array-to-pointer decay. */
2195 type = TREE_TYPE (expr);
2197 /* Wrap it in a CLEANUP_POINT_EXPR and add it to the list like a
2198 normal statement, but don't convert to void or actually add
2199 the EXPR_STMT. */
2200 if (TREE_CODE (expr) != CLEANUP_POINT_EXPR)
2201 expr = maybe_cleanup_point_expr (expr);
2202 add_stmt (expr);
2205 /* The type of the statement-expression is the type of the last
2206 expression. */
2207 TREE_TYPE (stmt_expr) = type;
2210 return stmt_expr;
2213 /* Finish a statement-expression. EXPR should be the value returned
2214 by the previous begin_stmt_expr. Returns an expression
2215 representing the statement-expression. */
2217 tree
2218 finish_stmt_expr (tree stmt_expr, bool has_no_scope)
2220 tree type;
2221 tree result;
2223 if (error_operand_p (stmt_expr))
2225 pop_stmt_list (stmt_expr);
2226 return error_mark_node;
2229 gcc_assert (TREE_CODE (stmt_expr) == STATEMENT_LIST);
2231 type = TREE_TYPE (stmt_expr);
2232 result = pop_stmt_list (stmt_expr);
2233 TREE_TYPE (result) = type;
2235 if (processing_template_decl)
2237 result = build_min (STMT_EXPR, type, result);
2238 TREE_SIDE_EFFECTS (result) = 1;
2239 STMT_EXPR_NO_SCOPE (result) = has_no_scope;
2241 else if (CLASS_TYPE_P (type))
2243 /* Wrap the statement-expression in a TARGET_EXPR so that the
2244 temporary object created by the final expression is destroyed at
2245 the end of the full-expression containing the
2246 statement-expression. */
2247 result = force_target_expr (type, result, tf_warning_or_error);
2250 return result;
2253 /* Returns the expression which provides the value of STMT_EXPR. */
2255 tree
2256 stmt_expr_value_expr (tree stmt_expr)
2258 tree t = STMT_EXPR_STMT (stmt_expr);
2260 if (TREE_CODE (t) == BIND_EXPR)
2261 t = BIND_EXPR_BODY (t);
2263 if (TREE_CODE (t) == STATEMENT_LIST && STATEMENT_LIST_TAIL (t))
2264 t = STATEMENT_LIST_TAIL (t)->stmt;
2266 if (TREE_CODE (t) == EXPR_STMT)
2267 t = EXPR_STMT_EXPR (t);
2269 return t;
2272 /* Return TRUE iff EXPR_STMT is an empty list of
2273 expression statements. */
2275 bool
2276 empty_expr_stmt_p (tree expr_stmt)
2278 tree body = NULL_TREE;
2280 if (expr_stmt == void_node)
2281 return true;
2283 if (expr_stmt)
2285 if (TREE_CODE (expr_stmt) == EXPR_STMT)
2286 body = EXPR_STMT_EXPR (expr_stmt);
2287 else if (TREE_CODE (expr_stmt) == STATEMENT_LIST)
2288 body = expr_stmt;
2291 if (body)
2293 if (TREE_CODE (body) == STATEMENT_LIST)
2294 return tsi_end_p (tsi_start (body));
2295 else
2296 return empty_expr_stmt_p (body);
2298 return false;
2301 /* Perform Koenig lookup. FN is the postfix-expression representing
2302 the function (or functions) to call; ARGS are the arguments to the
2303 call. Returns the functions to be considered by overload resolution. */
2305 cp_expr
2306 perform_koenig_lookup (cp_expr fn, vec<tree, va_gc> *args,
2307 tsubst_flags_t complain)
2309 tree identifier = NULL_TREE;
2310 tree functions = NULL_TREE;
2311 tree tmpl_args = NULL_TREE;
2312 bool template_id = false;
2313 location_t loc = fn.get_location ();
2315 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
2317 /* Use a separate flag to handle null args. */
2318 template_id = true;
2319 tmpl_args = TREE_OPERAND (fn, 1);
2320 fn = TREE_OPERAND (fn, 0);
2323 /* Find the name of the overloaded function. */
2324 if (identifier_p (fn))
2325 identifier = fn;
2326 else
2328 functions = fn;
2329 identifier = OVL_NAME (functions);
2332 /* A call to a namespace-scope function using an unqualified name.
2334 Do Koenig lookup -- unless any of the arguments are
2335 type-dependent. */
2336 if (!any_type_dependent_arguments_p (args)
2337 && !any_dependent_template_arguments_p (tmpl_args))
2339 fn = lookup_arg_dependent (identifier, functions, args);
2340 if (!fn)
2342 /* The unqualified name could not be resolved. */
2343 if (complain & tf_error)
2344 fn = unqualified_fn_lookup_error (cp_expr (identifier, loc));
2345 else
2346 fn = identifier;
2348 else if (TREE_CODE (fn) == OVERLOAD && processing_template_decl)
2349 /* FIXME: We shouldn't really need to mark the lookup here, as
2350 resolving the (non-dependent) call should save the single
2351 function we resolve to. Related to PR c++/83529. */
2352 lookup_keep (fn);
2355 if (fn && template_id && fn != error_mark_node)
2356 fn = build2 (TEMPLATE_ID_EXPR, unknown_type_node, fn, tmpl_args);
2358 return fn;
2361 /* Generate an expression for `FN (ARGS)'. This may change the
2362 contents of ARGS.
2364 If DISALLOW_VIRTUAL is true, the call to FN will be not generated
2365 as a virtual call, even if FN is virtual. (This flag is set when
2366 encountering an expression where the function name is explicitly
2367 qualified. For example a call to `X::f' never generates a virtual
2368 call.)
2370 Returns code for the call. */
2372 tree
2373 finish_call_expr (tree fn, vec<tree, va_gc> **args, bool disallow_virtual,
2374 bool koenig_p, tsubst_flags_t complain)
2376 tree result;
2377 tree orig_fn;
2378 vec<tree, va_gc> *orig_args = *args;
2380 if (fn == error_mark_node)
2381 return error_mark_node;
2383 gcc_assert (!TYPE_P (fn));
2385 /* If FN may be a FUNCTION_DECL obfuscated by force_paren_expr, undo
2386 it so that we can tell this is a call to a known function. */
2387 fn = maybe_undo_parenthesized_ref (fn);
2389 orig_fn = fn;
2391 if (processing_template_decl)
2393 /* If FN is a local extern declaration or set thereof, look them up
2394 again at instantiation time. */
2395 if (is_overloaded_fn (fn))
2397 tree ifn = get_first_fn (fn);
2398 if (TREE_CODE (ifn) == FUNCTION_DECL
2399 && DECL_LOCAL_FUNCTION_P (ifn))
2400 orig_fn = DECL_NAME (ifn);
2403 /* If the call expression is dependent, build a CALL_EXPR node
2404 with no type; type_dependent_expression_p recognizes
2405 expressions with no type as being dependent. */
2406 if (type_dependent_expression_p (fn)
2407 || any_type_dependent_arguments_p (*args))
2409 result = build_min_nt_call_vec (orig_fn, *args);
2410 SET_EXPR_LOCATION (result, cp_expr_loc_or_loc (fn, input_location));
2411 KOENIG_LOOKUP_P (result) = koenig_p;
2412 if (is_overloaded_fn (fn))
2413 fn = get_fns (fn);
2415 if (cfun)
2417 bool abnormal = true;
2418 for (lkp_iterator iter (fn); abnormal && iter; ++iter)
2420 tree fndecl = *iter;
2421 if (TREE_CODE (fndecl) != FUNCTION_DECL
2422 || !TREE_THIS_VOLATILE (fndecl))
2423 abnormal = false;
2425 /* FIXME: Stop warning about falling off end of non-void
2426 function. But this is wrong. Even if we only see
2427 no-return fns at this point, we could select a
2428 future-defined return fn during instantiation. Or
2429 vice-versa. */
2430 if (abnormal)
2431 current_function_returns_abnormally = 1;
2433 return result;
2435 orig_args = make_tree_vector_copy (*args);
2436 if (!BASELINK_P (fn)
2437 && TREE_CODE (fn) != PSEUDO_DTOR_EXPR
2438 && TREE_TYPE (fn) != unknown_type_node)
2439 fn = build_non_dependent_expr (fn);
2440 make_args_non_dependent (*args);
2443 if (TREE_CODE (fn) == COMPONENT_REF)
2445 tree member = TREE_OPERAND (fn, 1);
2446 if (BASELINK_P (member))
2448 tree object = TREE_OPERAND (fn, 0);
2449 return build_new_method_call (object, member,
2450 args, NULL_TREE,
2451 (disallow_virtual
2452 ? LOOKUP_NORMAL | LOOKUP_NONVIRTUAL
2453 : LOOKUP_NORMAL),
2454 /*fn_p=*/NULL,
2455 complain);
2459 /* Per 13.3.1.1, '(&f)(...)' is the same as '(f)(...)'. */
2460 if (TREE_CODE (fn) == ADDR_EXPR
2461 && TREE_CODE (TREE_OPERAND (fn, 0)) == OVERLOAD)
2462 fn = TREE_OPERAND (fn, 0);
2464 if (is_overloaded_fn (fn))
2465 fn = baselink_for_fns (fn);
2467 result = NULL_TREE;
2468 if (BASELINK_P (fn))
2470 tree object;
2472 /* A call to a member function. From [over.call.func]:
2474 If the keyword this is in scope and refers to the class of
2475 that member function, or a derived class thereof, then the
2476 function call is transformed into a qualified function call
2477 using (*this) as the postfix-expression to the left of the
2478 . operator.... [Otherwise] a contrived object of type T
2479 becomes the implied object argument.
2481 In this situation:
2483 struct A { void f(); };
2484 struct B : public A {};
2485 struct C : public A { void g() { B::f(); }};
2487 "the class of that member function" refers to `A'. But 11.2
2488 [class.access.base] says that we need to convert 'this' to B* as
2489 part of the access, so we pass 'B' to maybe_dummy_object. */
2491 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (get_first_fn (fn)))
2493 /* A constructor call always uses a dummy object. (This constructor
2494 call which has the form A::A () is actually invalid and we are
2495 going to reject it later in build_new_method_call.) */
2496 object = build_dummy_object (BINFO_TYPE (BASELINK_ACCESS_BINFO (fn)));
2498 else
2499 object = maybe_dummy_object (BINFO_TYPE (BASELINK_ACCESS_BINFO (fn)),
2500 NULL);
2502 result = build_new_method_call (object, fn, args, NULL_TREE,
2503 (disallow_virtual
2504 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
2505 : LOOKUP_NORMAL),
2506 /*fn_p=*/NULL,
2507 complain);
2509 else if (is_overloaded_fn (fn))
2511 /* If the function is an overloaded builtin, resolve it. */
2512 if (TREE_CODE (fn) == FUNCTION_DECL
2513 && (DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL
2514 || DECL_BUILT_IN_CLASS (fn) == BUILT_IN_MD))
2515 result = resolve_overloaded_builtin (input_location, fn, *args);
2517 if (!result)
2519 if (warn_sizeof_pointer_memaccess
2520 && (complain & tf_warning)
2521 && !vec_safe_is_empty (*args)
2522 && !processing_template_decl)
2524 location_t sizeof_arg_loc[3];
2525 tree sizeof_arg[3];
2526 unsigned int i;
2527 for (i = 0; i < 3; i++)
2529 tree t;
2531 sizeof_arg_loc[i] = UNKNOWN_LOCATION;
2532 sizeof_arg[i] = NULL_TREE;
2533 if (i >= (*args)->length ())
2534 continue;
2535 t = (**args)[i];
2536 if (TREE_CODE (t) != SIZEOF_EXPR)
2537 continue;
2538 if (SIZEOF_EXPR_TYPE_P (t))
2539 sizeof_arg[i] = TREE_TYPE (TREE_OPERAND (t, 0));
2540 else
2541 sizeof_arg[i] = TREE_OPERAND (t, 0);
2542 sizeof_arg_loc[i] = EXPR_LOCATION (t);
2544 sizeof_pointer_memaccess_warning
2545 (sizeof_arg_loc, fn, *args,
2546 sizeof_arg, same_type_ignoring_top_level_qualifiers_p);
2549 if ((complain & tf_warning)
2550 && TREE_CODE (fn) == FUNCTION_DECL
2551 && fndecl_built_in_p (fn, BUILT_IN_MEMSET)
2552 && vec_safe_length (*args) == 3
2553 && !any_type_dependent_arguments_p (*args))
2555 tree arg0 = (*orig_args)[0];
2556 tree arg1 = (*orig_args)[1];
2557 tree arg2 = (*orig_args)[2];
2558 int literal_mask = ((literal_integer_zerop (arg1) << 1)
2559 | (literal_integer_zerop (arg2) << 2));
2560 arg2 = instantiate_non_dependent_expr (arg2);
2561 warn_for_memset (input_location, arg0, arg2, literal_mask);
2564 /* A call to a namespace-scope function. */
2565 result = build_new_function_call (fn, args, complain);
2568 else if (TREE_CODE (fn) == PSEUDO_DTOR_EXPR)
2570 if (!vec_safe_is_empty (*args))
2571 error ("arguments to destructor are not allowed");
2572 /* Mark the pseudo-destructor call as having side-effects so
2573 that we do not issue warnings about its use. */
2574 result = build1 (NOP_EXPR,
2575 void_type_node,
2576 TREE_OPERAND (fn, 0));
2577 TREE_SIDE_EFFECTS (result) = 1;
2579 else if (CLASS_TYPE_P (TREE_TYPE (fn)))
2580 /* If the "function" is really an object of class type, it might
2581 have an overloaded `operator ()'. */
2582 result = build_op_call (fn, args, complain);
2584 if (!result)
2585 /* A call where the function is unknown. */
2586 result = cp_build_function_call_vec (fn, args, complain);
2588 if (processing_template_decl && result != error_mark_node)
2590 if (INDIRECT_REF_P (result))
2591 result = TREE_OPERAND (result, 0);
2592 result = build_call_vec (TREE_TYPE (result), orig_fn, orig_args);
2593 SET_EXPR_LOCATION (result, input_location);
2594 KOENIG_LOOKUP_P (result) = koenig_p;
2595 release_tree_vector (orig_args);
2596 result = convert_from_reference (result);
2599 return result;
2602 /* Finish a call to a postfix increment or decrement or EXPR. (Which
2603 is indicated by CODE, which should be POSTINCREMENT_EXPR or
2604 POSTDECREMENT_EXPR.) */
2606 cp_expr
2607 finish_increment_expr (cp_expr expr, enum tree_code code)
2609 /* input_location holds the location of the trailing operator token.
2610 Build a location of the form:
2611 expr++
2612 ~~~~^~
2613 with the caret at the operator token, ranging from the start
2614 of EXPR to the end of the operator token. */
2615 location_t combined_loc = make_location (input_location,
2616 expr.get_start (),
2617 get_finish (input_location));
2618 cp_expr result = build_x_unary_op (combined_loc, code, expr,
2619 tf_warning_or_error);
2620 /* TODO: build_x_unary_op doesn't honor the location, so set it here. */
2621 result.set_location (combined_loc);
2622 return result;
2625 /* Finish a use of `this'. Returns an expression for `this'. */
2627 tree
2628 finish_this_expr (void)
2630 tree result = NULL_TREE;
2632 if (current_class_ptr)
2634 tree type = TREE_TYPE (current_class_ref);
2636 /* In a lambda expression, 'this' refers to the captured 'this'. */
2637 if (LAMBDA_TYPE_P (type))
2638 result = lambda_expr_this_capture (CLASSTYPE_LAMBDA_EXPR (type), true);
2639 else
2640 result = current_class_ptr;
2643 if (result)
2644 /* The keyword 'this' is a prvalue expression. */
2645 return rvalue (result);
2647 tree fn = current_nonlambda_function ();
2648 if (fn && DECL_STATIC_FUNCTION_P (fn))
2649 error ("%<this%> is unavailable for static member functions");
2650 else if (fn)
2651 error ("invalid use of %<this%> in non-member function");
2652 else
2653 error ("invalid use of %<this%> at top level");
2654 return error_mark_node;
2657 /* Finish a pseudo-destructor expression. If SCOPE is NULL, the
2658 expression was of the form `OBJECT.~DESTRUCTOR' where DESTRUCTOR is
2659 the TYPE for the type given. If SCOPE is non-NULL, the expression
2660 was of the form `OBJECT.SCOPE::~DESTRUCTOR'. */
2662 tree
2663 finish_pseudo_destructor_expr (tree object, tree scope, tree destructor,
2664 location_t loc)
2666 if (object == error_mark_node || destructor == error_mark_node)
2667 return error_mark_node;
2669 gcc_assert (TYPE_P (destructor));
2671 if (!processing_template_decl)
2673 if (scope == error_mark_node)
2675 error_at (loc, "invalid qualifying scope in pseudo-destructor name");
2676 return error_mark_node;
2678 if (is_auto (destructor))
2679 destructor = TREE_TYPE (object);
2680 if (scope && TYPE_P (scope) && !check_dtor_name (scope, destructor))
2682 error_at (loc,
2683 "qualified type %qT does not match destructor name ~%qT",
2684 scope, destructor);
2685 return error_mark_node;
2689 /* [expr.pseudo] says both:
2691 The type designated by the pseudo-destructor-name shall be
2692 the same as the object type.
2694 and:
2696 The cv-unqualified versions of the object type and of the
2697 type designated by the pseudo-destructor-name shall be the
2698 same type.
2700 We implement the more generous second sentence, since that is
2701 what most other compilers do. */
2702 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (object),
2703 destructor))
2705 error_at (loc, "%qE is not of type %qT", object, destructor);
2706 return error_mark_node;
2710 return build3_loc (loc, PSEUDO_DTOR_EXPR, void_type_node, object,
2711 scope, destructor);
2714 /* Finish an expression of the form CODE EXPR. */
2716 cp_expr
2717 finish_unary_op_expr (location_t op_loc, enum tree_code code, cp_expr expr,
2718 tsubst_flags_t complain)
2720 /* Build a location of the form:
2721 ++expr
2722 ^~~~~~
2723 with the caret at the operator token, ranging from the start
2724 of the operator token to the end of EXPR. */
2725 location_t combined_loc = make_location (op_loc,
2726 op_loc, expr.get_finish ());
2727 cp_expr result = build_x_unary_op (combined_loc, code, expr, complain);
2728 /* TODO: build_x_unary_op doesn't always honor the location. */
2729 result.set_location (combined_loc);
2731 if (result == error_mark_node)
2732 return result;
2734 if (!(complain & tf_warning))
2735 return result;
2737 tree result_ovl = result;
2738 tree expr_ovl = expr;
2740 if (!processing_template_decl)
2741 expr_ovl = cp_fully_fold (expr_ovl);
2743 if (!CONSTANT_CLASS_P (expr_ovl)
2744 || TREE_OVERFLOW_P (expr_ovl))
2745 return result;
2747 if (!processing_template_decl)
2748 result_ovl = cp_fully_fold (result_ovl);
2750 if (CONSTANT_CLASS_P (result_ovl) && TREE_OVERFLOW_P (result_ovl))
2751 overflow_warning (combined_loc, result_ovl);
2753 return result;
2756 /* Finish a compound-literal expression or C++11 functional cast with aggregate
2757 initializer. TYPE is the type to which the CONSTRUCTOR in COMPOUND_LITERAL
2758 is being cast. */
2760 tree
2761 finish_compound_literal (tree type, tree compound_literal,
2762 tsubst_flags_t complain,
2763 fcl_t fcl_context)
2765 if (type == error_mark_node)
2766 return error_mark_node;
2768 if (TYPE_REF_P (type))
2770 compound_literal
2771 = finish_compound_literal (TREE_TYPE (type), compound_literal,
2772 complain, fcl_context);
2773 /* The prvalue is then used to direct-initialize the reference. */
2774 tree r = (perform_implicit_conversion_flags
2775 (type, compound_literal, complain, LOOKUP_NORMAL));
2776 return convert_from_reference (r);
2779 if (!TYPE_OBJ_P (type))
2781 if (complain & tf_error)
2782 error ("compound literal of non-object type %qT", type);
2783 return error_mark_node;
2786 if (tree anode = type_uses_auto (type))
2787 if (CLASS_PLACEHOLDER_TEMPLATE (anode))
2789 type = do_auto_deduction (type, compound_literal, anode, complain,
2790 adc_variable_type);
2791 if (type == error_mark_node)
2792 return error_mark_node;
2795 if (processing_template_decl)
2797 TREE_TYPE (compound_literal) = type;
2798 /* Mark the expression as a compound literal. */
2799 TREE_HAS_CONSTRUCTOR (compound_literal) = 1;
2800 if (fcl_context == fcl_c99)
2801 CONSTRUCTOR_C99_COMPOUND_LITERAL (compound_literal) = 1;
2802 return compound_literal;
2805 type = complete_type (type);
2807 if (TYPE_NON_AGGREGATE_CLASS (type))
2809 /* Trying to deal with a CONSTRUCTOR instead of a TREE_LIST
2810 everywhere that deals with function arguments would be a pain, so
2811 just wrap it in a TREE_LIST. The parser set a flag so we know
2812 that it came from T{} rather than T({}). */
2813 CONSTRUCTOR_IS_DIRECT_INIT (compound_literal) = 1;
2814 compound_literal = build_tree_list (NULL_TREE, compound_literal);
2815 return build_functional_cast (type, compound_literal, complain);
2818 if (TREE_CODE (type) == ARRAY_TYPE
2819 && check_array_initializer (NULL_TREE, type, compound_literal))
2820 return error_mark_node;
2821 compound_literal = reshape_init (type, compound_literal, complain);
2822 if (SCALAR_TYPE_P (type)
2823 && !BRACE_ENCLOSED_INITIALIZER_P (compound_literal)
2824 && !check_narrowing (type, compound_literal, complain))
2825 return error_mark_node;
2826 if (TREE_CODE (type) == ARRAY_TYPE
2827 && TYPE_DOMAIN (type) == NULL_TREE)
2829 cp_complete_array_type_or_error (&type, compound_literal,
2830 false, complain);
2831 if (type == error_mark_node)
2832 return error_mark_node;
2834 compound_literal = digest_init_flags (type, compound_literal, LOOKUP_NORMAL,
2835 complain);
2836 if (TREE_CODE (compound_literal) == CONSTRUCTOR)
2838 TREE_HAS_CONSTRUCTOR (compound_literal) = true;
2839 if (fcl_context == fcl_c99)
2840 CONSTRUCTOR_C99_COMPOUND_LITERAL (compound_literal) = 1;
2843 /* Put static/constant array temporaries in static variables. */
2844 /* FIXME all C99 compound literals should be variables rather than C++
2845 temporaries, unless they are used as an aggregate initializer. */
2846 if ((!at_function_scope_p () || CP_TYPE_CONST_P (type))
2847 && fcl_context == fcl_c99
2848 && TREE_CODE (type) == ARRAY_TYPE
2849 && !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
2850 && initializer_constant_valid_p (compound_literal, type))
2852 tree decl = create_temporary_var (type);
2853 DECL_INITIAL (decl) = compound_literal;
2854 TREE_STATIC (decl) = 1;
2855 if (literal_type_p (type) && CP_TYPE_CONST_NON_VOLATILE_P (type))
2857 /* 5.19 says that a constant expression can include an
2858 lvalue-rvalue conversion applied to "a glvalue of literal type
2859 that refers to a non-volatile temporary object initialized
2860 with a constant expression". Rather than try to communicate
2861 that this VAR_DECL is a temporary, just mark it constexpr. */
2862 DECL_DECLARED_CONSTEXPR_P (decl) = true;
2863 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
2864 TREE_CONSTANT (decl) = true;
2866 cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
2867 decl = pushdecl_top_level (decl);
2868 DECL_NAME (decl) = make_anon_name ();
2869 SET_DECL_ASSEMBLER_NAME (decl, DECL_NAME (decl));
2870 /* Make sure the destructor is callable. */
2871 tree clean = cxx_maybe_build_cleanup (decl, complain);
2872 if (clean == error_mark_node)
2873 return error_mark_node;
2874 return decl;
2877 /* Represent other compound literals with TARGET_EXPR so we produce
2878 an lvalue, but can elide copies. */
2879 if (!VECTOR_TYPE_P (type))
2880 compound_literal = get_target_expr_sfinae (compound_literal, complain);
2882 return compound_literal;
2885 /* Return the declaration for the function-name variable indicated by
2886 ID. */
2888 tree
2889 finish_fname (tree id)
2891 tree decl;
2893 decl = fname_decl (input_location, C_RID_CODE (id), id);
2894 if (processing_template_decl && current_function_decl
2895 && decl != error_mark_node)
2896 decl = DECL_NAME (decl);
2897 return decl;
2900 /* Finish a translation unit. */
2902 void
2903 finish_translation_unit (void)
2905 /* In case there were missing closebraces,
2906 get us back to the global binding level. */
2907 pop_everything ();
2908 while (current_namespace != global_namespace)
2909 pop_namespace ();
2911 /* Do file scope __FUNCTION__ et al. */
2912 finish_fname_decls ();
2915 /* Finish a template type parameter, specified as AGGR IDENTIFIER.
2916 Returns the parameter. */
2918 tree
2919 finish_template_type_parm (tree aggr, tree identifier)
2921 if (aggr != class_type_node)
2923 permerror (input_location, "template type parameters must use the keyword %<class%> or %<typename%>");
2924 aggr = class_type_node;
2927 return build_tree_list (aggr, identifier);
2930 /* Finish a template template parameter, specified as AGGR IDENTIFIER.
2931 Returns the parameter. */
2933 tree
2934 finish_template_template_parm (tree aggr, tree identifier)
2936 tree decl = build_decl (input_location,
2937 TYPE_DECL, identifier, NULL_TREE);
2939 tree tmpl = build_lang_decl (TEMPLATE_DECL, identifier, NULL_TREE);
2940 DECL_TEMPLATE_PARMS (tmpl) = current_template_parms;
2941 DECL_TEMPLATE_RESULT (tmpl) = decl;
2942 DECL_ARTIFICIAL (decl) = 1;
2944 // Associate the constraints with the underlying declaration,
2945 // not the template.
2946 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
2947 tree constr = build_constraints (reqs, NULL_TREE);
2948 set_constraints (decl, constr);
2950 end_template_decl ();
2952 gcc_assert (DECL_TEMPLATE_PARMS (tmpl));
2954 check_default_tmpl_args (decl, DECL_TEMPLATE_PARMS (tmpl),
2955 /*is_primary=*/true, /*is_partial=*/false,
2956 /*is_friend=*/0);
2958 return finish_template_type_parm (aggr, tmpl);
2961 /* ARGUMENT is the default-argument value for a template template
2962 parameter. If ARGUMENT is invalid, issue error messages and return
2963 the ERROR_MARK_NODE. Otherwise, ARGUMENT itself is returned. */
2965 tree
2966 check_template_template_default_arg (tree argument)
2968 if (TREE_CODE (argument) != TEMPLATE_DECL
2969 && TREE_CODE (argument) != TEMPLATE_TEMPLATE_PARM
2970 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
2972 if (TREE_CODE (argument) == TYPE_DECL)
2973 error ("invalid use of type %qT as a default value for a template "
2974 "template-parameter", TREE_TYPE (argument));
2975 else
2976 error ("invalid default argument for a template template parameter");
2977 return error_mark_node;
2980 return argument;
2983 /* Begin a class definition, as indicated by T. */
2985 tree
2986 begin_class_definition (tree t)
2988 if (error_operand_p (t) || error_operand_p (TYPE_MAIN_DECL (t)))
2989 return error_mark_node;
2991 /* According to the C++ ABI, decimal classes defined in ISO/IEC TR 24733
2992 are passed the same as decimal scalar types. */
2993 if (TREE_CODE (t) == RECORD_TYPE
2994 && !processing_template_decl)
2996 tree ns = TYPE_CONTEXT (t);
2997 if (ns && TREE_CODE (ns) == NAMESPACE_DECL
2998 && DECL_CONTEXT (ns) == std_node
2999 && DECL_NAME (ns)
3000 && id_equal (DECL_NAME (ns), "decimal"))
3002 const char *n = TYPE_NAME_STRING (t);
3003 if ((strcmp (n, "decimal32") == 0)
3004 || (strcmp (n, "decimal64") == 0)
3005 || (strcmp (n, "decimal128") == 0))
3006 TYPE_TRANSPARENT_AGGR (t) = 1;
3010 /* A non-implicit typename comes from code like:
3012 template <typename T> struct A {
3013 template <typename U> struct A<T>::B ...
3015 This is erroneous. */
3016 else if (TREE_CODE (t) == TYPENAME_TYPE)
3018 error ("invalid definition of qualified type %qT", t);
3019 t = error_mark_node;
3022 if (t == error_mark_node || ! MAYBE_CLASS_TYPE_P (t))
3024 t = make_class_type (RECORD_TYPE);
3025 pushtag (make_anon_name (), t, /*tag_scope=*/ts_current);
3028 if (TYPE_BEING_DEFINED (t))
3030 t = make_class_type (TREE_CODE (t));
3031 pushtag (TYPE_IDENTIFIER (t), t, /*tag_scope=*/ts_current);
3033 maybe_process_partial_specialization (t);
3034 pushclass (t);
3035 TYPE_BEING_DEFINED (t) = 1;
3036 class_binding_level->defining_class_p = 1;
3038 if (flag_pack_struct)
3040 tree v;
3041 TYPE_PACKED (t) = 1;
3042 /* Even though the type is being defined for the first time
3043 here, there might have been a forward declaration, so there
3044 might be cv-qualified variants of T. */
3045 for (v = TYPE_NEXT_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v))
3046 TYPE_PACKED (v) = 1;
3048 /* Reset the interface data, at the earliest possible
3049 moment, as it might have been set via a class foo;
3050 before. */
3051 if (! TYPE_UNNAMED_P (t))
3053 struct c_fileinfo *finfo = \
3054 get_fileinfo (LOCATION_FILE (input_location));
3055 CLASSTYPE_INTERFACE_ONLY (t) = finfo->interface_only;
3056 SET_CLASSTYPE_INTERFACE_UNKNOWN_X
3057 (t, finfo->interface_unknown);
3059 reset_specialization();
3061 /* Make a declaration for this class in its own scope. */
3062 build_self_reference ();
3064 return t;
3067 /* Finish the member declaration given by DECL. */
3069 void
3070 finish_member_declaration (tree decl)
3072 if (decl == error_mark_node || decl == NULL_TREE)
3073 return;
3075 if (decl == void_type_node)
3076 /* The COMPONENT was a friend, not a member, and so there's
3077 nothing for us to do. */
3078 return;
3080 /* We should see only one DECL at a time. */
3081 gcc_assert (DECL_CHAIN (decl) == NULL_TREE);
3083 /* Don't add decls after definition. */
3084 gcc_assert (TYPE_BEING_DEFINED (current_class_type)
3085 /* We can add lambda types when late parsing default
3086 arguments. */
3087 || LAMBDA_TYPE_P (TREE_TYPE (decl)));
3089 /* Set up access control for DECL. */
3090 TREE_PRIVATE (decl)
3091 = (current_access_specifier == access_private_node);
3092 TREE_PROTECTED (decl)
3093 = (current_access_specifier == access_protected_node);
3094 if (TREE_CODE (decl) == TEMPLATE_DECL)
3096 TREE_PRIVATE (DECL_TEMPLATE_RESULT (decl)) = TREE_PRIVATE (decl);
3097 TREE_PROTECTED (DECL_TEMPLATE_RESULT (decl)) = TREE_PROTECTED (decl);
3100 /* Mark the DECL as a member of the current class, unless it's
3101 a member of an enumeration. */
3102 if (TREE_CODE (decl) != CONST_DECL)
3103 DECL_CONTEXT (decl) = current_class_type;
3105 if (TREE_CODE (decl) == USING_DECL)
3106 /* For now, ignore class-scope USING_DECLS, so that debugging
3107 backends do not see them. */
3108 DECL_IGNORED_P (decl) = 1;
3110 /* Check for bare parameter packs in the non-static data member
3111 declaration. */
3112 if (TREE_CODE (decl) == FIELD_DECL)
3114 if (check_for_bare_parameter_packs (TREE_TYPE (decl)))
3115 TREE_TYPE (decl) = error_mark_node;
3116 if (check_for_bare_parameter_packs (DECL_ATTRIBUTES (decl)))
3117 DECL_ATTRIBUTES (decl) = NULL_TREE;
3120 /* [dcl.link]
3122 A C language linkage is ignored for the names of class members
3123 and the member function type of class member functions. */
3124 if (DECL_LANG_SPECIFIC (decl))
3125 SET_DECL_LANGUAGE (decl, lang_cplusplus);
3127 bool add = false;
3129 /* Functions and non-functions are added differently. */
3130 if (DECL_DECLARES_FUNCTION_P (decl))
3131 add = add_method (current_class_type, decl, false);
3132 /* Enter the DECL into the scope of the class, if the class
3133 isn't a closure (whose fields are supposed to be unnamed). */
3134 else if (CLASSTYPE_LAMBDA_EXPR (current_class_type)
3135 || pushdecl_class_level (decl))
3136 add = true;
3138 if (add)
3140 /* All TYPE_DECLs go at the end of TYPE_FIELDS. Ordinary fields
3141 go at the beginning. The reason is that
3142 legacy_nonfn_member_lookup searches the list in order, and we
3143 want a field name to override a type name so that the "struct
3144 stat hack" will work. In particular:
3146 struct S { enum E { }; static const int E = 5; int ary[S::E]; } s;
3148 is valid. */
3150 if (TREE_CODE (decl) == TYPE_DECL)
3151 TYPE_FIELDS (current_class_type)
3152 = chainon (TYPE_FIELDS (current_class_type), decl);
3153 else
3155 DECL_CHAIN (decl) = TYPE_FIELDS (current_class_type);
3156 TYPE_FIELDS (current_class_type) = decl;
3159 maybe_add_class_template_decl_list (current_class_type, decl,
3160 /*friend_p=*/0);
3164 /* Finish processing a complete template declaration. The PARMS are
3165 the template parameters. */
3167 void
3168 finish_template_decl (tree parms)
3170 if (parms)
3171 end_template_decl ();
3172 else
3173 end_specialization ();
3176 // Returns the template type of the class scope being entered. If we're
3177 // entering a constrained class scope. TYPE is the class template
3178 // scope being entered and we may need to match the intended type with
3179 // a constrained specialization. For example:
3181 // template<Object T>
3182 // struct S { void f(); }; #1
3184 // template<Object T>
3185 // void S<T>::f() { } #2
3187 // We check, in #2, that S<T> refers precisely to the type declared by
3188 // #1 (i.e., that the constraints match). Note that the following should
3189 // be an error since there is no specialization of S<T> that is
3190 // unconstrained, but this is not diagnosed here.
3192 // template<typename T>
3193 // void S<T>::f() { }
3195 // We cannot diagnose this problem here since this function also matches
3196 // qualified template names that are not part of a definition. For example:
3198 // template<Integral T, Floating_point U>
3199 // typename pair<T, U>::first_type void f(T, U);
3201 // Here, it is unlikely that there is a partial specialization of
3202 // pair constrained for for Integral and Floating_point arguments.
3204 // The general rule is: if a constrained specialization with matching
3205 // constraints is found return that type. Also note that if TYPE is not a
3206 // class-type (e.g. a typename type), then no fixup is needed.
3208 static tree
3209 fixup_template_type (tree type)
3211 // Find the template parameter list at the a depth appropriate to
3212 // the scope we're trying to enter.
3213 tree parms = current_template_parms;
3214 int depth = template_class_depth (type);
3215 for (int n = processing_template_decl; n > depth && parms; --n)
3216 parms = TREE_CHAIN (parms);
3217 if (!parms)
3218 return type;
3219 tree cur_reqs = TEMPLATE_PARMS_CONSTRAINTS (parms);
3220 tree cur_constr = build_constraints (cur_reqs, NULL_TREE);
3222 // Search for a specialization whose type and constraints match.
3223 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
3224 tree specs = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
3225 while (specs)
3227 tree spec_constr = get_constraints (TREE_VALUE (specs));
3229 // If the type and constraints match a specialization, then we
3230 // are entering that type.
3231 if (same_type_p (type, TREE_TYPE (specs))
3232 && equivalent_constraints (cur_constr, spec_constr))
3233 return TREE_TYPE (specs);
3234 specs = TREE_CHAIN (specs);
3237 // If no specialization matches, then must return the type
3238 // previously found.
3239 return type;
3242 /* Finish processing a template-id (which names a type) of the form
3243 NAME < ARGS >. Return the TYPE_DECL for the type named by the
3244 template-id. If ENTERING_SCOPE is nonzero we are about to enter
3245 the scope of template-id indicated. */
3247 tree
3248 finish_template_type (tree name, tree args, int entering_scope)
3250 tree type;
3252 type = lookup_template_class (name, args,
3253 NULL_TREE, NULL_TREE, entering_scope,
3254 tf_warning_or_error | tf_user);
3256 /* If we might be entering the scope of a partial specialization,
3257 find the one with the right constraints. */
3258 if (flag_concepts
3259 && entering_scope
3260 && CLASS_TYPE_P (type)
3261 && CLASSTYPE_TEMPLATE_INFO (type)
3262 && dependent_type_p (type)
3263 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
3264 type = fixup_template_type (type);
3266 if (type == error_mark_node)
3267 return type;
3268 else if (CLASS_TYPE_P (type) && !alias_type_or_template_p (type))
3269 return TYPE_STUB_DECL (type);
3270 else
3271 return TYPE_NAME (type);
3274 /* Finish processing a BASE_CLASS with the indicated ACCESS_SPECIFIER.
3275 Return a TREE_LIST containing the ACCESS_SPECIFIER and the
3276 BASE_CLASS, or NULL_TREE if an error occurred. The
3277 ACCESS_SPECIFIER is one of
3278 access_{default,public,protected_private}_node. For a virtual base
3279 we set TREE_TYPE. */
3281 tree
3282 finish_base_specifier (tree base, tree access, bool virtual_p)
3284 tree result;
3286 if (base == error_mark_node)
3288 error ("invalid base-class specification");
3289 result = NULL_TREE;
3291 else if (! MAYBE_CLASS_TYPE_P (base))
3293 error ("%qT is not a class type", base);
3294 result = NULL_TREE;
3296 else
3298 if (cp_type_quals (base) != 0)
3300 /* DR 484: Can a base-specifier name a cv-qualified
3301 class type? */
3302 base = TYPE_MAIN_VARIANT (base);
3304 result = build_tree_list (access, base);
3305 if (virtual_p)
3306 TREE_TYPE (result) = integer_type_node;
3309 return result;
3312 /* If FNS is a member function, a set of member functions, or a
3313 template-id referring to one or more member functions, return a
3314 BASELINK for FNS, incorporating the current access context.
3315 Otherwise, return FNS unchanged. */
3317 tree
3318 baselink_for_fns (tree fns)
3320 tree scope;
3321 tree cl;
3323 if (BASELINK_P (fns)
3324 || error_operand_p (fns))
3325 return fns;
3327 scope = ovl_scope (fns);
3328 if (!CLASS_TYPE_P (scope))
3329 return fns;
3331 cl = currently_open_derived_class (scope);
3332 if (!cl)
3333 cl = scope;
3334 cl = TYPE_BINFO (cl);
3335 return build_baselink (cl, cl, fns, /*optype=*/NULL_TREE);
3338 /* Returns true iff DECL is a variable from a function outside
3339 the current one. */
3341 static bool
3342 outer_var_p (tree decl)
3344 return ((VAR_P (decl) || TREE_CODE (decl) == PARM_DECL)
3345 && DECL_FUNCTION_SCOPE_P (decl)
3346 /* Don't get confused by temporaries. */
3347 && DECL_NAME (decl)
3348 && (DECL_CONTEXT (decl) != current_function_decl
3349 || parsing_nsdmi ()));
3352 /* As above, but also checks that DECL is automatic. */
3354 bool
3355 outer_automatic_var_p (tree decl)
3357 return (outer_var_p (decl)
3358 && !TREE_STATIC (decl));
3361 /* DECL satisfies outer_automatic_var_p. Possibly complain about it or
3362 rewrite it for lambda capture.
3364 If ODR_USE is true, we're being called from mark_use, and we complain about
3365 use of constant variables. If ODR_USE is false, we're being called for the
3366 id-expression, and we do lambda capture. */
3368 tree
3369 process_outer_var_ref (tree decl, tsubst_flags_t complain, bool odr_use)
3371 if (cp_unevaluated_operand)
3372 /* It's not a use (3.2) if we're in an unevaluated context. */
3373 return decl;
3374 if (decl == error_mark_node)
3375 return decl;
3377 tree context = DECL_CONTEXT (decl);
3378 tree containing_function = current_function_decl;
3379 tree lambda_stack = NULL_TREE;
3380 tree lambda_expr = NULL_TREE;
3381 tree initializer = convert_from_reference (decl);
3383 /* Mark it as used now even if the use is ill-formed. */
3384 if (!mark_used (decl, complain))
3385 return error_mark_node;
3387 if (parsing_nsdmi ())
3388 containing_function = NULL_TREE;
3390 if (containing_function && LAMBDA_FUNCTION_P (containing_function))
3392 /* Check whether we've already built a proxy. */
3393 tree var = decl;
3394 while (is_normal_capture_proxy (var))
3395 var = DECL_CAPTURED_VARIABLE (var);
3396 tree d = retrieve_local_specialization (var);
3398 if (d && d != decl && is_capture_proxy (d))
3400 if (DECL_CONTEXT (d) == containing_function)
3401 /* We already have an inner proxy. */
3402 return d;
3403 else
3404 /* We need to capture an outer proxy. */
3405 return process_outer_var_ref (d, complain, odr_use);
3409 /* If we are in a lambda function, we can move out until we hit
3410 1. the context,
3411 2. a non-lambda function, or
3412 3. a non-default capturing lambda function. */
3413 while (context != containing_function
3414 /* containing_function can be null with invalid generic lambdas. */
3415 && containing_function
3416 && LAMBDA_FUNCTION_P (containing_function))
3418 tree closure = DECL_CONTEXT (containing_function);
3419 lambda_expr = CLASSTYPE_LAMBDA_EXPR (closure);
3421 if (TYPE_CLASS_SCOPE_P (closure))
3422 /* A lambda in an NSDMI (c++/64496). */
3423 break;
3425 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr)
3426 == CPLD_NONE)
3427 break;
3429 lambda_stack = tree_cons (NULL_TREE,
3430 lambda_expr,
3431 lambda_stack);
3433 containing_function
3434 = decl_function_context (containing_function);
3437 /* In a lambda within a template, wait until instantiation
3438 time to implicitly capture. */
3439 if (context == containing_function
3440 && DECL_TEMPLATE_INFO (containing_function)
3441 && uses_template_parms (DECL_TI_ARGS (containing_function)))
3442 return decl;
3444 if (lambda_expr && VAR_P (decl)
3445 && DECL_ANON_UNION_VAR_P (decl))
3447 if (complain & tf_error)
3448 error ("cannot capture member %qD of anonymous union", decl);
3449 return error_mark_node;
3451 /* Do lambda capture when processing the id-expression, not when
3452 odr-using a variable. */
3453 if (!odr_use && context == containing_function)
3455 decl = add_default_capture (lambda_stack,
3456 /*id=*/DECL_NAME (decl),
3457 initializer);
3459 /* Only an odr-use of an outer automatic variable causes an
3460 error, and a constant variable can decay to a prvalue
3461 constant without odr-use. So don't complain yet. */
3462 else if (!odr_use && decl_constant_var_p (decl))
3463 return decl;
3464 else if (lambda_expr)
3466 if (complain & tf_error)
3468 error ("%qD is not captured", decl);
3469 tree closure = LAMBDA_EXPR_CLOSURE (lambda_expr);
3470 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr)
3471 == CPLD_NONE)
3472 inform (location_of (closure),
3473 "the lambda has no capture-default");
3474 else if (TYPE_CLASS_SCOPE_P (closure))
3475 inform (UNKNOWN_LOCATION, "lambda in local class %q+T cannot "
3476 "capture variables from the enclosing context",
3477 TYPE_CONTEXT (closure));
3478 inform (DECL_SOURCE_LOCATION (decl), "%q#D declared here", decl);
3480 return error_mark_node;
3482 else
3484 if (complain & tf_error)
3486 error (VAR_P (decl)
3487 ? G_("use of local variable with automatic storage from "
3488 "containing function")
3489 : G_("use of parameter from containing function"));
3490 inform (DECL_SOURCE_LOCATION (decl), "%q#D declared here", decl);
3492 return error_mark_node;
3494 return decl;
3497 /* ID_EXPRESSION is a representation of parsed, but unprocessed,
3498 id-expression. (See cp_parser_id_expression for details.) SCOPE,
3499 if non-NULL, is the type or namespace used to explicitly qualify
3500 ID_EXPRESSION. DECL is the entity to which that name has been
3501 resolved.
3503 *CONSTANT_EXPRESSION_P is true if we are presently parsing a
3504 constant-expression. In that case, *NON_CONSTANT_EXPRESSION_P will
3505 be set to true if this expression isn't permitted in a
3506 constant-expression, but it is otherwise not set by this function.
3507 *ALLOW_NON_CONSTANT_EXPRESSION_P is true if we are parsing a
3508 constant-expression, but a non-constant expression is also
3509 permissible.
3511 DONE is true if this expression is a complete postfix-expression;
3512 it is false if this expression is followed by '->', '[', '(', etc.
3513 ADDRESS_P is true iff this expression is the operand of '&'.
3514 TEMPLATE_P is true iff the qualified-id was of the form
3515 "A::template B". TEMPLATE_ARG_P is true iff this qualified name
3516 appears as a template argument.
3518 If an error occurs, and it is the kind of error that might cause
3519 the parser to abort a tentative parse, *ERROR_MSG is filled in. It
3520 is the caller's responsibility to issue the message. *ERROR_MSG
3521 will be a string with static storage duration, so the caller need
3522 not "free" it.
3524 Return an expression for the entity, after issuing appropriate
3525 diagnostics. This function is also responsible for transforming a
3526 reference to a non-static member into a COMPONENT_REF that makes
3527 the use of "this" explicit.
3529 Upon return, *IDK will be filled in appropriately. */
3530 cp_expr
3531 finish_id_expression (tree id_expression,
3532 tree decl,
3533 tree scope,
3534 cp_id_kind *idk,
3535 bool integral_constant_expression_p,
3536 bool allow_non_integral_constant_expression_p,
3537 bool *non_integral_constant_expression_p,
3538 bool template_p,
3539 bool done,
3540 bool address_p,
3541 bool template_arg_p,
3542 const char **error_msg,
3543 location_t location)
3545 decl = strip_using_decl (decl);
3547 /* Initialize the output parameters. */
3548 *idk = CP_ID_KIND_NONE;
3549 *error_msg = NULL;
3551 if (id_expression == error_mark_node)
3552 return error_mark_node;
3553 /* If we have a template-id, then no further lookup is
3554 required. If the template-id was for a template-class, we
3555 will sometimes have a TYPE_DECL at this point. */
3556 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3557 || TREE_CODE (decl) == TYPE_DECL)
3559 /* Look up the name. */
3560 else
3562 if (decl == error_mark_node)
3564 /* Name lookup failed. */
3565 if (scope
3566 && (!TYPE_P (scope)
3567 || (!dependent_type_p (scope)
3568 && !(identifier_p (id_expression)
3569 && IDENTIFIER_CONV_OP_P (id_expression)
3570 && dependent_type_p (TREE_TYPE (id_expression))))))
3572 /* If the qualifying type is non-dependent (and the name
3573 does not name a conversion operator to a dependent
3574 type), issue an error. */
3575 qualified_name_lookup_error (scope, id_expression, decl, location);
3576 return error_mark_node;
3578 else if (!scope)
3580 /* It may be resolved via Koenig lookup. */
3581 *idk = CP_ID_KIND_UNQUALIFIED;
3582 return id_expression;
3584 else
3585 decl = id_expression;
3588 /* Remember that the name was used in the definition of
3589 the current class so that we can check later to see if
3590 the meaning would have been different after the class
3591 was entirely defined. */
3592 if (!scope && decl != error_mark_node && identifier_p (id_expression))
3593 maybe_note_name_used_in_class (id_expression, decl);
3595 /* A use in unevaluated operand might not be instantiated appropriately
3596 if tsubst_copy builds a dummy parm, or if we never instantiate a
3597 generic lambda, so mark it now. */
3598 if (processing_template_decl && cp_unevaluated_operand)
3599 mark_type_use (decl);
3601 /* Disallow uses of local variables from containing functions, except
3602 within lambda-expressions. */
3603 if (outer_automatic_var_p (decl))
3605 decl = process_outer_var_ref (decl, tf_warning_or_error);
3606 if (decl == error_mark_node)
3607 return error_mark_node;
3610 /* Also disallow uses of function parameters outside the function
3611 body, except inside an unevaluated context (i.e. decltype). */
3612 if (TREE_CODE (decl) == PARM_DECL
3613 && DECL_CONTEXT (decl) == NULL_TREE
3614 && !cp_unevaluated_operand)
3616 *error_msg = G_("use of parameter outside function body");
3617 return error_mark_node;
3621 /* If we didn't find anything, or what we found was a type,
3622 then this wasn't really an id-expression. */
3623 if (TREE_CODE (decl) == TEMPLATE_DECL
3624 && !DECL_FUNCTION_TEMPLATE_P (decl))
3626 *error_msg = G_("missing template arguments");
3627 return error_mark_node;
3629 else if (TREE_CODE (decl) == TYPE_DECL
3630 || TREE_CODE (decl) == NAMESPACE_DECL)
3632 *error_msg = G_("expected primary-expression");
3633 return error_mark_node;
3636 /* If the name resolved to a template parameter, there is no
3637 need to look it up again later. */
3638 if ((TREE_CODE (decl) == CONST_DECL && DECL_TEMPLATE_PARM_P (decl))
3639 || TREE_CODE (decl) == TEMPLATE_PARM_INDEX)
3641 tree r;
3643 *idk = CP_ID_KIND_NONE;
3644 if (TREE_CODE (decl) == TEMPLATE_PARM_INDEX)
3645 decl = TEMPLATE_PARM_DECL (decl);
3646 r = DECL_INITIAL (decl);
3647 if (CLASS_TYPE_P (TREE_TYPE (r)) && !CP_TYPE_CONST_P (TREE_TYPE (r)))
3649 /* If the entity is a template parameter object for a template
3650 parameter of type T, the type of the expression is const T. */
3651 tree ctype = TREE_TYPE (r);
3652 ctype = cp_build_qualified_type (ctype, (cp_type_quals (ctype)
3653 | TYPE_QUAL_CONST));
3654 r = build1 (VIEW_CONVERT_EXPR, ctype, r);
3656 r = convert_from_reference (r);
3657 if (integral_constant_expression_p
3658 && !dependent_type_p (TREE_TYPE (decl))
3659 && !(INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (r))))
3661 if (!allow_non_integral_constant_expression_p)
3662 error ("template parameter %qD of type %qT is not allowed in "
3663 "an integral constant expression because it is not of "
3664 "integral or enumeration type", decl, TREE_TYPE (decl));
3665 *non_integral_constant_expression_p = true;
3667 return r;
3669 else
3671 bool dependent_p = type_dependent_expression_p (decl);
3673 /* If the declaration was explicitly qualified indicate
3674 that. The semantics of `A::f(3)' are different than
3675 `f(3)' if `f' is virtual. */
3676 *idk = (scope
3677 ? CP_ID_KIND_QUALIFIED
3678 : (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3679 ? CP_ID_KIND_TEMPLATE_ID
3680 : (dependent_p
3681 ? CP_ID_KIND_UNQUALIFIED_DEPENDENT
3682 : CP_ID_KIND_UNQUALIFIED)));
3684 if (dependent_p
3685 && DECL_P (decl)
3686 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (decl)))
3687 /* Dependent type attributes on the decl mean that the TREE_TYPE is
3688 wrong, so just return the identifier. */
3689 return id_expression;
3691 if (TREE_CODE (decl) == NAMESPACE_DECL)
3693 error ("use of namespace %qD as expression", decl);
3694 return error_mark_node;
3696 else if (DECL_CLASS_TEMPLATE_P (decl))
3698 error ("use of class template %qT as expression", decl);
3699 return error_mark_node;
3701 else if (TREE_CODE (decl) == TREE_LIST)
3703 /* Ambiguous reference to base members. */
3704 error ("request for member %qD is ambiguous in "
3705 "multiple inheritance lattice", id_expression);
3706 print_candidates (decl);
3707 return error_mark_node;
3710 /* Mark variable-like entities as used. Functions are similarly
3711 marked either below or after overload resolution. */
3712 if ((VAR_P (decl)
3713 || TREE_CODE (decl) == PARM_DECL
3714 || TREE_CODE (decl) == CONST_DECL
3715 || TREE_CODE (decl) == RESULT_DECL)
3716 && !mark_used (decl))
3717 return error_mark_node;
3719 /* Only certain kinds of names are allowed in constant
3720 expression. Template parameters have already
3721 been handled above. */
3722 if (! error_operand_p (decl)
3723 && !dependent_p
3724 && integral_constant_expression_p
3725 && ! decl_constant_var_p (decl)
3726 && TREE_CODE (decl) != CONST_DECL
3727 && ! builtin_valid_in_constant_expr_p (decl))
3729 if (!allow_non_integral_constant_expression_p)
3731 error ("%qD cannot appear in a constant-expression", decl);
3732 return error_mark_node;
3734 *non_integral_constant_expression_p = true;
3737 tree wrap;
3738 if (VAR_P (decl)
3739 && !cp_unevaluated_operand
3740 && !processing_template_decl
3741 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl))
3742 && CP_DECL_THREAD_LOCAL_P (decl)
3743 && (wrap = get_tls_wrapper_fn (decl)))
3745 /* Replace an evaluated use of the thread_local variable with
3746 a call to its wrapper. */
3747 decl = build_cxx_call (wrap, 0, NULL, tf_warning_or_error);
3749 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3750 && !dependent_p
3751 && variable_template_p (TREE_OPERAND (decl, 0)))
3753 decl = finish_template_variable (decl);
3754 mark_used (decl);
3755 decl = convert_from_reference (decl);
3757 else if (scope)
3759 if (TREE_CODE (decl) == SCOPE_REF)
3761 gcc_assert (same_type_p (scope, TREE_OPERAND (decl, 0)));
3762 decl = TREE_OPERAND (decl, 1);
3765 decl = (adjust_result_of_qualified_name_lookup
3766 (decl, scope, current_nonlambda_class_type()));
3768 if (TREE_CODE (decl) == FUNCTION_DECL)
3769 mark_used (decl);
3771 if (TYPE_P (scope))
3772 decl = finish_qualified_id_expr (scope,
3773 decl,
3774 done,
3775 address_p,
3776 template_p,
3777 template_arg_p,
3778 tf_warning_or_error);
3779 else
3780 decl = convert_from_reference (decl);
3782 else if (TREE_CODE (decl) == FIELD_DECL)
3784 /* Since SCOPE is NULL here, this is an unqualified name.
3785 Access checking has been performed during name lookup
3786 already. Turn off checking to avoid duplicate errors. */
3787 push_deferring_access_checks (dk_no_check);
3788 decl = finish_non_static_data_member (decl, NULL_TREE,
3789 /*qualifying_scope=*/NULL_TREE);
3790 pop_deferring_access_checks ();
3792 else if (is_overloaded_fn (decl))
3794 tree first_fn = get_first_fn (decl);
3796 if (TREE_CODE (first_fn) == TEMPLATE_DECL)
3797 first_fn = DECL_TEMPLATE_RESULT (first_fn);
3799 /* [basic.def.odr]: "A function whose name appears as a
3800 potentially-evaluated expression is odr-used if it is the unique
3801 lookup result".
3803 But only mark it if it's a complete postfix-expression; in a call,
3804 ADL might select a different function, and we'll call mark_used in
3805 build_over_call. */
3806 if (done
3807 && !really_overloaded_fn (decl)
3808 && !mark_used (first_fn))
3809 return error_mark_node;
3811 if (!template_arg_p
3812 && TREE_CODE (first_fn) == FUNCTION_DECL
3813 && DECL_FUNCTION_MEMBER_P (first_fn)
3814 && !shared_member_p (decl))
3816 /* A set of member functions. */
3817 decl = maybe_dummy_object (DECL_CONTEXT (first_fn), 0);
3818 return finish_class_member_access_expr (decl, id_expression,
3819 /*template_p=*/false,
3820 tf_warning_or_error);
3823 decl = baselink_for_fns (decl);
3825 else
3827 if (DECL_P (decl) && DECL_NONLOCAL (decl)
3828 && DECL_CLASS_SCOPE_P (decl))
3830 tree context = context_for_name_lookup (decl);
3831 if (context != current_class_type)
3833 tree path = currently_open_derived_class (context);
3834 perform_or_defer_access_check (TYPE_BINFO (path),
3835 decl, decl,
3836 tf_warning_or_error);
3840 decl = convert_from_reference (decl);
3844 return cp_expr (decl, location);
3847 /* Implement the __typeof keyword: Return the type of EXPR, suitable for
3848 use as a type-specifier. */
3850 tree
3851 finish_typeof (tree expr)
3853 tree type;
3855 if (type_dependent_expression_p (expr))
3857 type = cxx_make_type (TYPEOF_TYPE);
3858 TYPEOF_TYPE_EXPR (type) = expr;
3859 SET_TYPE_STRUCTURAL_EQUALITY (type);
3861 return type;
3864 expr = mark_type_use (expr);
3866 type = unlowered_expr_type (expr);
3868 if (!type || type == unknown_type_node)
3870 error ("type of %qE is unknown", expr);
3871 return error_mark_node;
3874 return type;
3877 /* Implement the __underlying_type keyword: Return the underlying
3878 type of TYPE, suitable for use as a type-specifier. */
3880 tree
3881 finish_underlying_type (tree type)
3883 tree underlying_type;
3885 if (processing_template_decl)
3887 underlying_type = cxx_make_type (UNDERLYING_TYPE);
3888 UNDERLYING_TYPE_TYPE (underlying_type) = type;
3889 SET_TYPE_STRUCTURAL_EQUALITY (underlying_type);
3891 return underlying_type;
3894 if (!complete_type_or_else (type, NULL_TREE))
3895 return error_mark_node;
3897 if (TREE_CODE (type) != ENUMERAL_TYPE)
3899 error ("%qT is not an enumeration type", type);
3900 return error_mark_node;
3903 underlying_type = ENUM_UNDERLYING_TYPE (type);
3905 /* Fixup necessary in this case because ENUM_UNDERLYING_TYPE
3906 includes TYPE_MIN_VALUE and TYPE_MAX_VALUE information.
3907 See finish_enum_value_list for details. */
3908 if (!ENUM_FIXED_UNDERLYING_TYPE_P (type))
3909 underlying_type
3910 = c_common_type_for_mode (TYPE_MODE (underlying_type),
3911 TYPE_UNSIGNED (underlying_type));
3913 return underlying_type;
3916 /* Implement the __direct_bases keyword: Return the direct base classes
3917 of type. */
3919 tree
3920 calculate_direct_bases (tree type, tsubst_flags_t complain)
3922 if (!complete_type_or_maybe_complain (type, NULL_TREE, complain)
3923 || !NON_UNION_CLASS_TYPE_P (type))
3924 return make_tree_vec (0);
3926 vec<tree, va_gc> *vector = make_tree_vector ();
3927 vec<tree, va_gc> *base_binfos = BINFO_BASE_BINFOS (TYPE_BINFO (type));
3928 tree binfo;
3929 unsigned i;
3931 /* Virtual bases are initialized first */
3932 for (i = 0; base_binfos->iterate (i, &binfo); i++)
3933 if (BINFO_VIRTUAL_P (binfo))
3934 vec_safe_push (vector, binfo);
3936 /* Now non-virtuals */
3937 for (i = 0; base_binfos->iterate (i, &binfo); i++)
3938 if (!BINFO_VIRTUAL_P (binfo))
3939 vec_safe_push (vector, binfo);
3941 tree bases_vec = make_tree_vec (vector->length ());
3943 for (i = 0; i < vector->length (); ++i)
3944 TREE_VEC_ELT (bases_vec, i) = BINFO_TYPE ((*vector)[i]);
3946 release_tree_vector (vector);
3947 return bases_vec;
3950 /* Implement the __bases keyword: Return the base classes
3951 of type */
3953 /* Find morally non-virtual base classes by walking binfo hierarchy */
3954 /* Virtual base classes are handled separately in finish_bases */
3956 static tree
3957 dfs_calculate_bases_pre (tree binfo, void * /*data_*/)
3959 /* Don't walk bases of virtual bases */
3960 return BINFO_VIRTUAL_P (binfo) ? dfs_skip_bases : NULL_TREE;
3963 static tree
3964 dfs_calculate_bases_post (tree binfo, void *data_)
3966 vec<tree, va_gc> **data = ((vec<tree, va_gc> **) data_);
3967 if (!BINFO_VIRTUAL_P (binfo))
3968 vec_safe_push (*data, BINFO_TYPE (binfo));
3969 return NULL_TREE;
3972 /* Calculates the morally non-virtual base classes of a class */
3973 static vec<tree, va_gc> *
3974 calculate_bases_helper (tree type)
3976 vec<tree, va_gc> *vector = make_tree_vector ();
3978 /* Now add non-virtual base classes in order of construction */
3979 if (TYPE_BINFO (type))
3980 dfs_walk_all (TYPE_BINFO (type),
3981 dfs_calculate_bases_pre, dfs_calculate_bases_post, &vector);
3982 return vector;
3985 tree
3986 calculate_bases (tree type, tsubst_flags_t complain)
3988 if (!complete_type_or_maybe_complain (type, NULL_TREE, complain)
3989 || !NON_UNION_CLASS_TYPE_P (type))
3990 return make_tree_vec (0);
3992 vec<tree, va_gc> *vector = make_tree_vector ();
3993 tree bases_vec = NULL_TREE;
3994 unsigned i;
3995 vec<tree, va_gc> *vbases;
3996 vec<tree, va_gc> *nonvbases;
3997 tree binfo;
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 = 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 while (TREE_CODE (expr) == COMPONENT_REF
4076 || TREE_CODE (expr) == COMPOUND_EXPR)
4077 expr = TREE_OPERAND (expr, 1);
4079 if (DECL_P (expr))
4081 error ("cannot apply %<offsetof%> to member function %qD", expr);
4082 inform (DECL_SOURCE_LOCATION (expr), "declared here");
4084 else
4085 error ("cannot apply %<offsetof%> to member function");
4086 return error_mark_node;
4088 if (TREE_CODE (expr) == CONST_DECL)
4090 error ("cannot apply %<offsetof%> to an enumerator %qD", expr);
4091 return error_mark_node;
4093 if (REFERENCE_REF_P (expr))
4094 expr = TREE_OPERAND (expr, 0);
4095 if (!complete_type_or_else (TREE_TYPE (TREE_TYPE (object_ptr)), object_ptr))
4096 return error_mark_node;
4097 if (warn_invalid_offsetof
4098 && CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (object_ptr)))
4099 && CLASSTYPE_NON_STD_LAYOUT (TREE_TYPE (TREE_TYPE (object_ptr)))
4100 && cp_unevaluated_operand == 0)
4101 warning_at (loc, OPT_Winvalid_offsetof, "offsetof within "
4102 "non-standard-layout type %qT is conditionally-supported",
4103 TREE_TYPE (TREE_TYPE (object_ptr)));
4104 return fold_offsetof (expr);
4107 /* Replace the AGGR_INIT_EXPR at *TP with an equivalent CALL_EXPR. This
4108 function is broken out from the above for the benefit of the tree-ssa
4109 project. */
4111 void
4112 simplify_aggr_init_expr (tree *tp)
4114 tree aggr_init_expr = *tp;
4116 /* Form an appropriate CALL_EXPR. */
4117 tree fn = AGGR_INIT_EXPR_FN (aggr_init_expr);
4118 tree slot = AGGR_INIT_EXPR_SLOT (aggr_init_expr);
4119 tree type = TREE_TYPE (slot);
4121 tree call_expr;
4122 enum style_t { ctor, arg, pcc } style;
4124 if (AGGR_INIT_VIA_CTOR_P (aggr_init_expr))
4125 style = ctor;
4126 #ifdef PCC_STATIC_STRUCT_RETURN
4127 else if (1)
4128 style = pcc;
4129 #endif
4130 else
4132 gcc_assert (TREE_ADDRESSABLE (type));
4133 style = arg;
4136 call_expr = build_call_array_loc (input_location,
4137 TREE_TYPE (TREE_TYPE (TREE_TYPE (fn))),
4139 aggr_init_expr_nargs (aggr_init_expr),
4140 AGGR_INIT_EXPR_ARGP (aggr_init_expr));
4141 TREE_NOTHROW (call_expr) = TREE_NOTHROW (aggr_init_expr);
4142 CALL_FROM_THUNK_P (call_expr) = AGGR_INIT_FROM_THUNK_P (aggr_init_expr);
4143 CALL_EXPR_OPERATOR_SYNTAX (call_expr)
4144 = CALL_EXPR_OPERATOR_SYNTAX (aggr_init_expr);
4145 CALL_EXPR_ORDERED_ARGS (call_expr) = CALL_EXPR_ORDERED_ARGS (aggr_init_expr);
4146 CALL_EXPR_REVERSE_ARGS (call_expr) = CALL_EXPR_REVERSE_ARGS (aggr_init_expr);
4148 if (style == ctor)
4150 /* Replace the first argument to the ctor with the address of the
4151 slot. */
4152 cxx_mark_addressable (slot);
4153 CALL_EXPR_ARG (call_expr, 0) =
4154 build1 (ADDR_EXPR, build_pointer_type (type), slot);
4156 else if (style == arg)
4158 /* Just mark it addressable here, and leave the rest to
4159 expand_call{,_inline}. */
4160 cxx_mark_addressable (slot);
4161 CALL_EXPR_RETURN_SLOT_OPT (call_expr) = true;
4162 call_expr = build2 (INIT_EXPR, TREE_TYPE (call_expr), slot, call_expr);
4164 else if (style == pcc)
4166 /* If we're using the non-reentrant PCC calling convention, then we
4167 need to copy the returned value out of the static buffer into the
4168 SLOT. */
4169 push_deferring_access_checks (dk_no_check);
4170 call_expr = build_aggr_init (slot, call_expr,
4171 DIRECT_BIND | LOOKUP_ONLYCONVERTING,
4172 tf_warning_or_error);
4173 pop_deferring_access_checks ();
4174 call_expr = build2 (COMPOUND_EXPR, TREE_TYPE (slot), call_expr, slot);
4177 if (AGGR_INIT_ZERO_FIRST (aggr_init_expr))
4179 tree init = build_zero_init (type, NULL_TREE,
4180 /*static_storage_p=*/false);
4181 init = build2 (INIT_EXPR, void_type_node, slot, init);
4182 call_expr = build2 (COMPOUND_EXPR, TREE_TYPE (call_expr),
4183 init, call_expr);
4186 *tp = call_expr;
4189 /* Emit all thunks to FN that should be emitted when FN is emitted. */
4191 void
4192 emit_associated_thunks (tree fn)
4194 /* When we use vcall offsets, we emit thunks with the virtual
4195 functions to which they thunk. The whole point of vcall offsets
4196 is so that you can know statically the entire set of thunks that
4197 will ever be needed for a given virtual function, thereby
4198 enabling you to output all the thunks with the function itself. */
4199 if (DECL_VIRTUAL_P (fn)
4200 /* Do not emit thunks for extern template instantiations. */
4201 && ! DECL_REALLY_EXTERN (fn))
4203 tree thunk;
4205 for (thunk = DECL_THUNKS (fn); thunk; thunk = DECL_CHAIN (thunk))
4207 if (!THUNK_ALIAS (thunk))
4209 use_thunk (thunk, /*emit_p=*/1);
4210 if (DECL_RESULT_THUNK_P (thunk))
4212 tree probe;
4214 for (probe = DECL_THUNKS (thunk);
4215 probe; probe = DECL_CHAIN (probe))
4216 use_thunk (probe, /*emit_p=*/1);
4219 else
4220 gcc_assert (!DECL_THUNKS (thunk));
4225 /* Generate RTL for FN. */
4227 bool
4228 expand_or_defer_fn_1 (tree fn)
4230 /* When the parser calls us after finishing the body of a template
4231 function, we don't really want to expand the body. */
4232 if (processing_template_decl)
4234 /* Normally, collection only occurs in rest_of_compilation. So,
4235 if we don't collect here, we never collect junk generated
4236 during the processing of templates until we hit a
4237 non-template function. It's not safe to do this inside a
4238 nested class, though, as the parser may have local state that
4239 is not a GC root. */
4240 if (!function_depth)
4241 ggc_collect ();
4242 return false;
4245 gcc_assert (DECL_SAVED_TREE (fn));
4247 /* We make a decision about linkage for these functions at the end
4248 of the compilation. Until that point, we do not want the back
4249 end to output them -- but we do want it to see the bodies of
4250 these functions so that it can inline them as appropriate. */
4251 if (DECL_DECLARED_INLINE_P (fn) || DECL_IMPLICIT_INSTANTIATION (fn))
4253 if (DECL_INTERFACE_KNOWN (fn))
4254 /* We've already made a decision as to how this function will
4255 be handled. */;
4256 else if (!at_eof)
4257 tentative_decl_linkage (fn);
4258 else
4259 import_export_decl (fn);
4261 /* If the user wants us to keep all inline functions, then mark
4262 this function as needed so that finish_file will make sure to
4263 output it later. Similarly, all dllexport'd functions must
4264 be emitted; there may be callers in other DLLs. */
4265 if (DECL_DECLARED_INLINE_P (fn)
4266 && !DECL_REALLY_EXTERN (fn)
4267 && (flag_keep_inline_functions
4268 || (flag_keep_inline_dllexport
4269 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (fn)))))
4271 mark_needed (fn);
4272 DECL_EXTERNAL (fn) = 0;
4276 /* If this is a constructor or destructor body, we have to clone
4277 it. */
4278 if (maybe_clone_body (fn))
4280 /* We don't want to process FN again, so pretend we've written
4281 it out, even though we haven't. */
4282 TREE_ASM_WRITTEN (fn) = 1;
4283 /* If this is a constexpr function, keep DECL_SAVED_TREE. */
4284 if (!DECL_DECLARED_CONSTEXPR_P (fn))
4285 DECL_SAVED_TREE (fn) = NULL_TREE;
4286 return false;
4289 /* There's no reason to do any of the work here if we're only doing
4290 semantic analysis; this code just generates RTL. */
4291 if (flag_syntax_only)
4292 return false;
4294 return true;
4297 void
4298 expand_or_defer_fn (tree fn)
4300 if (expand_or_defer_fn_1 (fn))
4302 function_depth++;
4304 /* Expand or defer, at the whim of the compilation unit manager. */
4305 cgraph_node::finalize_function (fn, function_depth > 1);
4306 emit_associated_thunks (fn);
4308 function_depth--;
4312 struct nrv_data
4314 nrv_data () : visited (37) {}
4316 tree var;
4317 tree result;
4318 hash_table<nofree_ptr_hash <tree_node> > visited;
4321 /* Helper function for walk_tree, used by finalize_nrv below. */
4323 static tree
4324 finalize_nrv_r (tree* tp, int* walk_subtrees, void* data)
4326 struct nrv_data *dp = (struct nrv_data *)data;
4327 tree_node **slot;
4329 /* No need to walk into types. There wouldn't be any need to walk into
4330 non-statements, except that we have to consider STMT_EXPRs. */
4331 if (TYPE_P (*tp))
4332 *walk_subtrees = 0;
4333 /* Change all returns to just refer to the RESULT_DECL; this is a nop,
4334 but differs from using NULL_TREE in that it indicates that we care
4335 about the value of the RESULT_DECL. */
4336 else if (TREE_CODE (*tp) == RETURN_EXPR)
4337 TREE_OPERAND (*tp, 0) = dp->result;
4338 /* Change all cleanups for the NRV to only run when an exception is
4339 thrown. */
4340 else if (TREE_CODE (*tp) == CLEANUP_STMT
4341 && CLEANUP_DECL (*tp) == dp->var)
4342 CLEANUP_EH_ONLY (*tp) = 1;
4343 /* Replace the DECL_EXPR for the NRV with an initialization of the
4344 RESULT_DECL, if needed. */
4345 else if (TREE_CODE (*tp) == DECL_EXPR
4346 && DECL_EXPR_DECL (*tp) == dp->var)
4348 tree init;
4349 if (DECL_INITIAL (dp->var)
4350 && DECL_INITIAL (dp->var) != error_mark_node)
4351 init = build2 (INIT_EXPR, void_type_node, dp->result,
4352 DECL_INITIAL (dp->var));
4353 else
4354 init = build_empty_stmt (EXPR_LOCATION (*tp));
4355 DECL_INITIAL (dp->var) = NULL_TREE;
4356 SET_EXPR_LOCATION (init, EXPR_LOCATION (*tp));
4357 *tp = init;
4359 /* And replace all uses of the NRV with the RESULT_DECL. */
4360 else if (*tp == dp->var)
4361 *tp = dp->result;
4363 /* Avoid walking into the same tree more than once. Unfortunately, we
4364 can't just use walk_tree_without duplicates because it would only call
4365 us for the first occurrence of dp->var in the function body. */
4366 slot = dp->visited.find_slot (*tp, INSERT);
4367 if (*slot)
4368 *walk_subtrees = 0;
4369 else
4370 *slot = *tp;
4372 /* Keep iterating. */
4373 return NULL_TREE;
4376 /* Called from finish_function to implement the named return value
4377 optimization by overriding all the RETURN_EXPRs and pertinent
4378 CLEANUP_STMTs and replacing all occurrences of VAR with RESULT, the
4379 RESULT_DECL for the function. */
4381 void
4382 finalize_nrv (tree *tp, tree var, tree result)
4384 struct nrv_data data;
4386 /* Copy name from VAR to RESULT. */
4387 DECL_NAME (result) = DECL_NAME (var);
4388 /* Don't forget that we take its address. */
4389 TREE_ADDRESSABLE (result) = TREE_ADDRESSABLE (var);
4390 /* Finally set DECL_VALUE_EXPR to avoid assigning
4391 a stack slot at -O0 for the original var and debug info
4392 uses RESULT location for VAR. */
4393 SET_DECL_VALUE_EXPR (var, result);
4394 DECL_HAS_VALUE_EXPR_P (var) = 1;
4396 data.var = var;
4397 data.result = result;
4398 cp_walk_tree (tp, finalize_nrv_r, &data, 0);
4401 /* Create CP_OMP_CLAUSE_INFO for clause C. Returns true if it is invalid. */
4403 bool
4404 cxx_omp_create_clause_info (tree c, tree type, bool need_default_ctor,
4405 bool need_copy_ctor, bool need_copy_assignment,
4406 bool need_dtor)
4408 int save_errorcount = errorcount;
4409 tree info, t;
4411 /* Always allocate 3 elements for simplicity. These are the
4412 function decls for the ctor, dtor, and assignment op.
4413 This layout is known to the three lang hooks,
4414 cxx_omp_clause_default_init, cxx_omp_clause_copy_init,
4415 and cxx_omp_clause_assign_op. */
4416 info = make_tree_vec (3);
4417 CP_OMP_CLAUSE_INFO (c) = info;
4419 if (need_default_ctor || need_copy_ctor)
4421 if (need_default_ctor)
4422 t = get_default_ctor (type);
4423 else
4424 t = get_copy_ctor (type, tf_warning_or_error);
4426 if (t && !trivial_fn_p (t))
4427 TREE_VEC_ELT (info, 0) = t;
4430 if (need_dtor && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
4431 TREE_VEC_ELT (info, 1) = get_dtor (type, tf_warning_or_error);
4433 if (need_copy_assignment)
4435 t = get_copy_assign (type);
4437 if (t && !trivial_fn_p (t))
4438 TREE_VEC_ELT (info, 2) = t;
4441 return errorcount != save_errorcount;
4444 /* If DECL is DECL_OMP_PRIVATIZED_MEMBER, return corresponding
4445 FIELD_DECL, otherwise return DECL itself. */
4447 static tree
4448 omp_clause_decl_field (tree decl)
4450 if (VAR_P (decl)
4451 && DECL_HAS_VALUE_EXPR_P (decl)
4452 && DECL_ARTIFICIAL (decl)
4453 && DECL_LANG_SPECIFIC (decl)
4454 && DECL_OMP_PRIVATIZED_MEMBER (decl))
4456 tree f = DECL_VALUE_EXPR (decl);
4457 if (INDIRECT_REF_P (f))
4458 f = TREE_OPERAND (f, 0);
4459 if (TREE_CODE (f) == COMPONENT_REF)
4461 f = TREE_OPERAND (f, 1);
4462 gcc_assert (TREE_CODE (f) == FIELD_DECL);
4463 return f;
4466 return NULL_TREE;
4469 /* Adjust DECL if needed for printing using %qE. */
4471 static tree
4472 omp_clause_printable_decl (tree decl)
4474 tree t = omp_clause_decl_field (decl);
4475 if (t)
4476 return t;
4477 return decl;
4480 /* For a FIELD_DECL F and corresponding DECL_OMP_PRIVATIZED_MEMBER
4481 VAR_DECL T that doesn't need a DECL_EXPR added, record it for
4482 privatization. */
4484 static void
4485 omp_note_field_privatization (tree f, tree t)
4487 if (!omp_private_member_map)
4488 omp_private_member_map = new hash_map<tree, tree>;
4489 tree &v = omp_private_member_map->get_or_insert (f);
4490 if (v == NULL_TREE)
4492 v = t;
4493 omp_private_member_vec.safe_push (f);
4494 /* Signal that we don't want to create DECL_EXPR for this dummy var. */
4495 omp_private_member_vec.safe_push (integer_zero_node);
4499 /* Privatize FIELD_DECL T, return corresponding DECL_OMP_PRIVATIZED_MEMBER
4500 dummy VAR_DECL. */
4502 tree
4503 omp_privatize_field (tree t, bool shared)
4505 tree m = finish_non_static_data_member (t, NULL_TREE, NULL_TREE);
4506 if (m == error_mark_node)
4507 return error_mark_node;
4508 if (!omp_private_member_map && !shared)
4509 omp_private_member_map = new hash_map<tree, tree>;
4510 if (TYPE_REF_P (TREE_TYPE (t)))
4512 gcc_assert (INDIRECT_REF_P (m));
4513 m = TREE_OPERAND (m, 0);
4515 tree vb = NULL_TREE;
4516 tree &v = shared ? vb : omp_private_member_map->get_or_insert (t);
4517 if (v == NULL_TREE)
4519 v = create_temporary_var (TREE_TYPE (m));
4520 retrofit_lang_decl (v);
4521 DECL_OMP_PRIVATIZED_MEMBER (v) = 1;
4522 SET_DECL_VALUE_EXPR (v, m);
4523 DECL_HAS_VALUE_EXPR_P (v) = 1;
4524 if (!shared)
4525 omp_private_member_vec.safe_push (t);
4527 return v;
4530 /* Helper function for handle_omp_array_sections. Called recursively
4531 to handle multiple array-section-subscripts. C is the clause,
4532 T current expression (initially OMP_CLAUSE_DECL), which is either
4533 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
4534 expression if specified, TREE_VALUE length expression if specified,
4535 TREE_CHAIN is what it has been specified after, or some decl.
4536 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
4537 set to true if any of the array-section-subscript could have length
4538 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
4539 first array-section-subscript which is known not to have length
4540 of one. Given say:
4541 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
4542 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
4543 all are or may have length of 1, array-section-subscript [:2] is the
4544 first one known not to have length 1. For array-section-subscript
4545 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
4546 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
4547 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
4548 case though, as some lengths could be zero. */
4550 static tree
4551 handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
4552 bool &maybe_zero_len, unsigned int &first_non_one,
4553 enum c_omp_region_type ort)
4555 tree ret, low_bound, length, type;
4556 if (TREE_CODE (t) != TREE_LIST)
4558 if (error_operand_p (t))
4559 return error_mark_node;
4560 if (REFERENCE_REF_P (t)
4561 && TREE_CODE (TREE_OPERAND (t, 0)) == COMPONENT_REF)
4562 t = TREE_OPERAND (t, 0);
4563 ret = t;
4564 if (TREE_CODE (t) == COMPONENT_REF
4565 && ort == C_ORT_OMP
4566 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
4567 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO
4568 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FROM)
4569 && !type_dependent_expression_p (t))
4571 if (TREE_CODE (TREE_OPERAND (t, 1)) == FIELD_DECL
4572 && DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
4574 error_at (OMP_CLAUSE_LOCATION (c),
4575 "bit-field %qE in %qs clause",
4576 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4577 return error_mark_node;
4579 while (TREE_CODE (t) == COMPONENT_REF)
4581 if (TREE_TYPE (TREE_OPERAND (t, 0))
4582 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == UNION_TYPE)
4584 error_at (OMP_CLAUSE_LOCATION (c),
4585 "%qE is a member of a union", t);
4586 return error_mark_node;
4588 t = TREE_OPERAND (t, 0);
4590 if (REFERENCE_REF_P (t))
4591 t = TREE_OPERAND (t, 0);
4593 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
4595 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
4596 return NULL_TREE;
4597 if (DECL_P (t))
4598 error_at (OMP_CLAUSE_LOCATION (c),
4599 "%qD is not a variable in %qs clause", t,
4600 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4601 else
4602 error_at (OMP_CLAUSE_LOCATION (c),
4603 "%qE is not a variable in %qs clause", t,
4604 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4605 return error_mark_node;
4607 else if (ort == C_ORT_OMP
4608 && TREE_CODE (t) == PARM_DECL
4609 && DECL_ARTIFICIAL (t)
4610 && DECL_NAME (t) == this_identifier)
4612 error_at (OMP_CLAUSE_LOCATION (c),
4613 "%<this%> allowed in OpenMP only in %<declare simd%>"
4614 " clauses");
4615 return error_mark_node;
4617 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
4618 && VAR_P (t) && CP_DECL_THREAD_LOCAL_P (t))
4620 error_at (OMP_CLAUSE_LOCATION (c),
4621 "%qD is threadprivate variable in %qs clause", t,
4622 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4623 return error_mark_node;
4625 if (type_dependent_expression_p (ret))
4626 return NULL_TREE;
4627 ret = convert_from_reference (ret);
4628 return ret;
4631 if (ort == C_ORT_OMP
4632 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
4633 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
4634 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
4635 && TREE_CODE (TREE_CHAIN (t)) == FIELD_DECL)
4636 TREE_CHAIN (t) = omp_privatize_field (TREE_CHAIN (t), false);
4637 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
4638 maybe_zero_len, first_non_one, ort);
4639 if (ret == error_mark_node || ret == NULL_TREE)
4640 return ret;
4642 type = TREE_TYPE (ret);
4643 low_bound = TREE_PURPOSE (t);
4644 length = TREE_VALUE (t);
4645 if ((low_bound && type_dependent_expression_p (low_bound))
4646 || (length && type_dependent_expression_p (length)))
4647 return NULL_TREE;
4649 if (low_bound == error_mark_node || length == error_mark_node)
4650 return error_mark_node;
4652 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
4654 error_at (OMP_CLAUSE_LOCATION (c),
4655 "low bound %qE of array section does not have integral type",
4656 low_bound);
4657 return error_mark_node;
4659 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
4661 error_at (OMP_CLAUSE_LOCATION (c),
4662 "length %qE of array section does not have integral type",
4663 length);
4664 return error_mark_node;
4666 if (low_bound)
4667 low_bound = mark_rvalue_use (low_bound);
4668 if (length)
4669 length = mark_rvalue_use (length);
4670 /* We need to reduce to real constant-values for checks below. */
4671 if (length)
4672 length = fold_simple (length);
4673 if (low_bound)
4674 low_bound = fold_simple (low_bound);
4675 if (low_bound
4676 && TREE_CODE (low_bound) == INTEGER_CST
4677 && TYPE_PRECISION (TREE_TYPE (low_bound))
4678 > TYPE_PRECISION (sizetype))
4679 low_bound = fold_convert (sizetype, low_bound);
4680 if (length
4681 && TREE_CODE (length) == INTEGER_CST
4682 && TYPE_PRECISION (TREE_TYPE (length))
4683 > TYPE_PRECISION (sizetype))
4684 length = fold_convert (sizetype, length);
4685 if (low_bound == NULL_TREE)
4686 low_bound = integer_zero_node;
4688 if (length != NULL_TREE)
4690 if (!integer_nonzerop (length))
4692 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
4693 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
4694 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
4695 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
4697 if (integer_zerop (length))
4699 error_at (OMP_CLAUSE_LOCATION (c),
4700 "zero length array section in %qs clause",
4701 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4702 return error_mark_node;
4705 else
4706 maybe_zero_len = true;
4708 if (first_non_one == types.length ()
4709 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
4710 first_non_one++;
4712 if (TREE_CODE (type) == ARRAY_TYPE)
4714 if (length == NULL_TREE
4715 && (TYPE_DOMAIN (type) == NULL_TREE
4716 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
4718 error_at (OMP_CLAUSE_LOCATION (c),
4719 "for unknown bound array type length expression must "
4720 "be specified");
4721 return error_mark_node;
4723 if (TREE_CODE (low_bound) == INTEGER_CST
4724 && tree_int_cst_sgn (low_bound) == -1)
4726 error_at (OMP_CLAUSE_LOCATION (c),
4727 "negative low bound in array section in %qs clause",
4728 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4729 return error_mark_node;
4731 if (length != NULL_TREE
4732 && TREE_CODE (length) == INTEGER_CST
4733 && tree_int_cst_sgn (length) == -1)
4735 error_at (OMP_CLAUSE_LOCATION (c),
4736 "negative length in array section in %qs clause",
4737 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4738 return error_mark_node;
4740 if (TYPE_DOMAIN (type)
4741 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
4742 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
4743 == INTEGER_CST)
4745 tree size
4746 = fold_convert (sizetype, TYPE_MAX_VALUE (TYPE_DOMAIN (type)));
4747 size = size_binop (PLUS_EXPR, size, size_one_node);
4748 if (TREE_CODE (low_bound) == INTEGER_CST)
4750 if (tree_int_cst_lt (size, low_bound))
4752 error_at (OMP_CLAUSE_LOCATION (c),
4753 "low bound %qE above array section size "
4754 "in %qs clause", low_bound,
4755 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4756 return error_mark_node;
4758 if (tree_int_cst_equal (size, low_bound))
4760 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
4761 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
4762 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
4763 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
4765 error_at (OMP_CLAUSE_LOCATION (c),
4766 "zero length array section in %qs clause",
4767 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4768 return error_mark_node;
4770 maybe_zero_len = true;
4772 else if (length == NULL_TREE
4773 && first_non_one == types.length ()
4774 && tree_int_cst_equal
4775 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
4776 low_bound))
4777 first_non_one++;
4779 else if (length == NULL_TREE)
4781 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
4782 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
4783 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_IN_REDUCTION
4784 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_TASK_REDUCTION)
4785 maybe_zero_len = true;
4786 if (first_non_one == types.length ())
4787 first_non_one++;
4789 if (length && TREE_CODE (length) == INTEGER_CST)
4791 if (tree_int_cst_lt (size, length))
4793 error_at (OMP_CLAUSE_LOCATION (c),
4794 "length %qE above array section size "
4795 "in %qs clause", length,
4796 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4797 return error_mark_node;
4799 if (TREE_CODE (low_bound) == INTEGER_CST)
4801 tree lbpluslen
4802 = size_binop (PLUS_EXPR,
4803 fold_convert (sizetype, low_bound),
4804 fold_convert (sizetype, length));
4805 if (TREE_CODE (lbpluslen) == INTEGER_CST
4806 && tree_int_cst_lt (size, lbpluslen))
4808 error_at (OMP_CLAUSE_LOCATION (c),
4809 "high bound %qE above array section size "
4810 "in %qs clause", lbpluslen,
4811 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4812 return error_mark_node;
4817 else if (length == NULL_TREE)
4819 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
4820 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
4821 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_IN_REDUCTION
4822 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_TASK_REDUCTION)
4823 maybe_zero_len = true;
4824 if (first_non_one == types.length ())
4825 first_non_one++;
4828 /* For [lb:] we will need to evaluate lb more than once. */
4829 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
4831 tree lb = cp_save_expr (low_bound);
4832 if (lb != low_bound)
4834 TREE_PURPOSE (t) = lb;
4835 low_bound = lb;
4839 else if (TYPE_PTR_P (type))
4841 if (length == NULL_TREE)
4843 error_at (OMP_CLAUSE_LOCATION (c),
4844 "for pointer type length expression must be specified");
4845 return error_mark_node;
4847 if (length != NULL_TREE
4848 && TREE_CODE (length) == INTEGER_CST
4849 && tree_int_cst_sgn (length) == -1)
4851 error_at (OMP_CLAUSE_LOCATION (c),
4852 "negative length in array section in %qs clause",
4853 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4854 return error_mark_node;
4856 /* If there is a pointer type anywhere but in the very first
4857 array-section-subscript, the array section can't be contiguous. */
4858 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
4859 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
4861 error_at (OMP_CLAUSE_LOCATION (c),
4862 "array section is not contiguous in %qs clause",
4863 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4864 return error_mark_node;
4867 else
4869 error_at (OMP_CLAUSE_LOCATION (c),
4870 "%qE does not have pointer or array type", ret);
4871 return error_mark_node;
4873 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
4874 types.safe_push (TREE_TYPE (ret));
4875 /* We will need to evaluate lb more than once. */
4876 tree lb = cp_save_expr (low_bound);
4877 if (lb != low_bound)
4879 TREE_PURPOSE (t) = lb;
4880 low_bound = lb;
4882 ret = grok_array_decl (OMP_CLAUSE_LOCATION (c), ret, low_bound, false);
4883 return ret;
4886 /* Handle array sections for clause C. */
4888 static bool
4889 handle_omp_array_sections (tree c, enum c_omp_region_type ort)
4891 bool maybe_zero_len = false;
4892 unsigned int first_non_one = 0;
4893 auto_vec<tree, 10> types;
4894 tree *tp = &OMP_CLAUSE_DECL (c);
4895 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
4896 && TREE_CODE (*tp) == TREE_LIST
4897 && TREE_PURPOSE (*tp)
4898 && TREE_CODE (TREE_PURPOSE (*tp)) == TREE_VEC)
4899 tp = &TREE_VALUE (*tp);
4900 tree first = handle_omp_array_sections_1 (c, *tp, types,
4901 maybe_zero_len, first_non_one,
4902 ort);
4903 if (first == error_mark_node)
4904 return true;
4905 if (first == NULL_TREE)
4906 return false;
4907 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
4909 tree t = *tp;
4910 tree tem = NULL_TREE;
4911 if (processing_template_decl)
4912 return false;
4913 /* Need to evaluate side effects in the length expressions
4914 if any. */
4915 while (TREE_CODE (t) == TREE_LIST)
4917 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
4919 if (tem == NULL_TREE)
4920 tem = TREE_VALUE (t);
4921 else
4922 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
4923 TREE_VALUE (t), tem);
4925 t = TREE_CHAIN (t);
4927 if (tem)
4928 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
4929 *tp = first;
4931 else
4933 unsigned int num = types.length (), i;
4934 tree t, side_effects = NULL_TREE, size = NULL_TREE;
4935 tree condition = NULL_TREE;
4937 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
4938 maybe_zero_len = true;
4939 if (processing_template_decl && maybe_zero_len)
4940 return false;
4942 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
4943 t = TREE_CHAIN (t))
4945 tree low_bound = TREE_PURPOSE (t);
4946 tree length = TREE_VALUE (t);
4948 i--;
4949 if (low_bound
4950 && TREE_CODE (low_bound) == INTEGER_CST
4951 && TYPE_PRECISION (TREE_TYPE (low_bound))
4952 > TYPE_PRECISION (sizetype))
4953 low_bound = fold_convert (sizetype, low_bound);
4954 if (length
4955 && TREE_CODE (length) == INTEGER_CST
4956 && TYPE_PRECISION (TREE_TYPE (length))
4957 > TYPE_PRECISION (sizetype))
4958 length = fold_convert (sizetype, length);
4959 if (low_bound == NULL_TREE)
4960 low_bound = integer_zero_node;
4961 if (!maybe_zero_len && i > first_non_one)
4963 if (integer_nonzerop (low_bound))
4964 goto do_warn_noncontiguous;
4965 if (length != NULL_TREE
4966 && TREE_CODE (length) == INTEGER_CST
4967 && TYPE_DOMAIN (types[i])
4968 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
4969 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
4970 == INTEGER_CST)
4972 tree size;
4973 size = size_binop (PLUS_EXPR,
4974 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
4975 size_one_node);
4976 if (!tree_int_cst_equal (length, size))
4978 do_warn_noncontiguous:
4979 error_at (OMP_CLAUSE_LOCATION (c),
4980 "array section is not contiguous in %qs "
4981 "clause",
4982 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4983 return true;
4986 if (!processing_template_decl
4987 && length != NULL_TREE
4988 && TREE_SIDE_EFFECTS (length))
4990 if (side_effects == NULL_TREE)
4991 side_effects = length;
4992 else
4993 side_effects = build2 (COMPOUND_EXPR,
4994 TREE_TYPE (side_effects),
4995 length, side_effects);
4998 else if (processing_template_decl)
4999 continue;
5000 else
5002 tree l;
5004 if (i > first_non_one
5005 && ((length && integer_nonzerop (length))
5006 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
5007 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
5008 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION))
5009 continue;
5010 if (length)
5011 l = fold_convert (sizetype, length);
5012 else
5014 l = size_binop (PLUS_EXPR,
5015 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
5016 size_one_node);
5017 l = size_binop (MINUS_EXPR, l,
5018 fold_convert (sizetype, low_bound));
5020 if (i > first_non_one)
5022 l = fold_build2 (NE_EXPR, boolean_type_node, l,
5023 size_zero_node);
5024 if (condition == NULL_TREE)
5025 condition = l;
5026 else
5027 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
5028 l, condition);
5030 else if (size == NULL_TREE)
5032 size = size_in_bytes (TREE_TYPE (types[i]));
5033 tree eltype = TREE_TYPE (types[num - 1]);
5034 while (TREE_CODE (eltype) == ARRAY_TYPE)
5035 eltype = TREE_TYPE (eltype);
5036 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
5037 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
5038 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
5039 size = size_binop (EXACT_DIV_EXPR, size,
5040 size_in_bytes (eltype));
5041 size = size_binop (MULT_EXPR, size, l);
5042 if (condition)
5043 size = fold_build3 (COND_EXPR, sizetype, condition,
5044 size, size_zero_node);
5046 else
5047 size = size_binop (MULT_EXPR, size, l);
5050 if (!processing_template_decl)
5052 if (side_effects)
5053 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
5054 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
5055 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
5056 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
5058 size = size_binop (MINUS_EXPR, size, size_one_node);
5059 size = save_expr (size);
5060 tree index_type = build_index_type (size);
5061 tree eltype = TREE_TYPE (first);
5062 while (TREE_CODE (eltype) == ARRAY_TYPE)
5063 eltype = TREE_TYPE (eltype);
5064 tree type = build_array_type (eltype, index_type);
5065 tree ptype = build_pointer_type (eltype);
5066 if (TYPE_REF_P (TREE_TYPE (t))
5067 && INDIRECT_TYPE_P (TREE_TYPE (TREE_TYPE (t))))
5068 t = convert_from_reference (t);
5069 else if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
5070 t = build_fold_addr_expr (t);
5071 tree t2 = build_fold_addr_expr (first);
5072 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
5073 ptrdiff_type_node, t2);
5074 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
5075 ptrdiff_type_node, t2,
5076 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
5077 ptrdiff_type_node, t));
5078 if (tree_fits_shwi_p (t2))
5079 t = build2 (MEM_REF, type, t,
5080 build_int_cst (ptype, tree_to_shwi (t2)));
5081 else
5083 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
5084 sizetype, t2);
5085 t = build2_loc (OMP_CLAUSE_LOCATION (c), POINTER_PLUS_EXPR,
5086 TREE_TYPE (t), t, t2);
5087 t = build2 (MEM_REF, type, t, build_int_cst (ptype, 0));
5089 OMP_CLAUSE_DECL (c) = t;
5090 return false;
5092 OMP_CLAUSE_DECL (c) = first;
5093 OMP_CLAUSE_SIZE (c) = size;
5094 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
5095 || (TREE_CODE (t) == COMPONENT_REF
5096 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE))
5097 return false;
5098 if (ort == C_ORT_OMP || ort == C_ORT_ACC)
5099 switch (OMP_CLAUSE_MAP_KIND (c))
5101 case GOMP_MAP_ALLOC:
5102 case GOMP_MAP_TO:
5103 case GOMP_MAP_FROM:
5104 case GOMP_MAP_TOFROM:
5105 case GOMP_MAP_ALWAYS_TO:
5106 case GOMP_MAP_ALWAYS_FROM:
5107 case GOMP_MAP_ALWAYS_TOFROM:
5108 case GOMP_MAP_RELEASE:
5109 case GOMP_MAP_DELETE:
5110 case GOMP_MAP_FORCE_TO:
5111 case GOMP_MAP_FORCE_FROM:
5112 case GOMP_MAP_FORCE_TOFROM:
5113 case GOMP_MAP_FORCE_PRESENT:
5114 OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (c) = 1;
5115 break;
5116 default:
5117 break;
5119 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
5120 OMP_CLAUSE_MAP);
5121 if ((ort & C_ORT_OMP_DECLARE_SIMD) != C_ORT_OMP && ort != C_ORT_ACC)
5122 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_POINTER);
5123 else if (TREE_CODE (t) == COMPONENT_REF)
5124 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ALWAYS_POINTER);
5125 else if (REFERENCE_REF_P (t)
5126 && TREE_CODE (TREE_OPERAND (t, 0)) == COMPONENT_REF)
5128 t = TREE_OPERAND (t, 0);
5129 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ALWAYS_POINTER);
5131 else
5132 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_FIRSTPRIVATE_POINTER);
5133 if (OMP_CLAUSE_MAP_KIND (c2) != GOMP_MAP_FIRSTPRIVATE_POINTER
5134 && !cxx_mark_addressable (t))
5135 return false;
5136 OMP_CLAUSE_DECL (c2) = t;
5137 t = build_fold_addr_expr (first);
5138 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
5139 ptrdiff_type_node, t);
5140 tree ptr = OMP_CLAUSE_DECL (c2);
5141 ptr = convert_from_reference (ptr);
5142 if (!INDIRECT_TYPE_P (TREE_TYPE (ptr)))
5143 ptr = build_fold_addr_expr (ptr);
5144 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
5145 ptrdiff_type_node, t,
5146 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
5147 ptrdiff_type_node, ptr));
5148 OMP_CLAUSE_SIZE (c2) = t;
5149 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
5150 OMP_CLAUSE_CHAIN (c) = c2;
5151 ptr = OMP_CLAUSE_DECL (c2);
5152 if (OMP_CLAUSE_MAP_KIND (c2) != GOMP_MAP_FIRSTPRIVATE_POINTER
5153 && TYPE_REF_P (TREE_TYPE (ptr))
5154 && INDIRECT_TYPE_P (TREE_TYPE (TREE_TYPE (ptr))))
5156 tree c3 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
5157 OMP_CLAUSE_MAP);
5158 OMP_CLAUSE_SET_MAP_KIND (c3, OMP_CLAUSE_MAP_KIND (c2));
5159 OMP_CLAUSE_DECL (c3) = ptr;
5160 if (OMP_CLAUSE_MAP_KIND (c2) == GOMP_MAP_ALWAYS_POINTER)
5161 OMP_CLAUSE_DECL (c2) = build_simple_mem_ref (ptr);
5162 else
5163 OMP_CLAUSE_DECL (c2) = convert_from_reference (ptr);
5164 OMP_CLAUSE_SIZE (c3) = size_zero_node;
5165 OMP_CLAUSE_CHAIN (c3) = OMP_CLAUSE_CHAIN (c2);
5166 OMP_CLAUSE_CHAIN (c2) = c3;
5170 return false;
5173 /* Return identifier to look up for omp declare reduction. */
5175 tree
5176 omp_reduction_id (enum tree_code reduction_code, tree reduction_id, tree type)
5178 const char *p = NULL;
5179 const char *m = NULL;
5180 switch (reduction_code)
5182 case PLUS_EXPR:
5183 case MULT_EXPR:
5184 case MINUS_EXPR:
5185 case BIT_AND_EXPR:
5186 case BIT_XOR_EXPR:
5187 case BIT_IOR_EXPR:
5188 case TRUTH_ANDIF_EXPR:
5189 case TRUTH_ORIF_EXPR:
5190 reduction_id = ovl_op_identifier (false, reduction_code);
5191 break;
5192 case MIN_EXPR:
5193 p = "min";
5194 break;
5195 case MAX_EXPR:
5196 p = "max";
5197 break;
5198 default:
5199 break;
5202 if (p == NULL)
5204 if (TREE_CODE (reduction_id) != IDENTIFIER_NODE)
5205 return error_mark_node;
5206 p = IDENTIFIER_POINTER (reduction_id);
5209 if (type != NULL_TREE)
5210 m = mangle_type_string (TYPE_MAIN_VARIANT (type));
5212 const char prefix[] = "omp declare reduction ";
5213 size_t lenp = sizeof (prefix);
5214 if (strncmp (p, prefix, lenp - 1) == 0)
5215 lenp = 1;
5216 size_t len = strlen (p);
5217 size_t lenm = m ? strlen (m) + 1 : 0;
5218 char *name = XALLOCAVEC (char, lenp + len + lenm);
5219 if (lenp > 1)
5220 memcpy (name, prefix, lenp - 1);
5221 memcpy (name + lenp - 1, p, len + 1);
5222 if (m)
5224 name[lenp + len - 1] = '~';
5225 memcpy (name + lenp + len, m, lenm);
5227 return get_identifier (name);
5230 /* Lookup OpenMP UDR ID for TYPE, return the corresponding artificial
5231 FUNCTION_DECL or NULL_TREE if not found. */
5233 static tree
5234 omp_reduction_lookup (location_t loc, tree id, tree type, tree *baselinkp,
5235 vec<tree> *ambiguousp)
5237 tree orig_id = id;
5238 tree baselink = NULL_TREE;
5239 if (identifier_p (id))
5241 cp_id_kind idk;
5242 bool nonint_cst_expression_p;
5243 const char *error_msg;
5244 id = omp_reduction_id (ERROR_MARK, id, type);
5245 tree decl = lookup_name (id);
5246 if (decl == NULL_TREE)
5247 decl = error_mark_node;
5248 id = finish_id_expression (id, decl, NULL_TREE, &idk, false, true,
5249 &nonint_cst_expression_p, false, true, false,
5250 false, &error_msg, loc);
5251 if (idk == CP_ID_KIND_UNQUALIFIED
5252 && identifier_p (id))
5254 vec<tree, va_gc> *args = NULL;
5255 vec_safe_push (args, build_reference_type (type));
5256 id = perform_koenig_lookup (id, args, tf_none);
5259 else if (TREE_CODE (id) == SCOPE_REF)
5260 id = lookup_qualified_name (TREE_OPERAND (id, 0),
5261 omp_reduction_id (ERROR_MARK,
5262 TREE_OPERAND (id, 1),
5263 type),
5264 false, false);
5265 tree fns = id;
5266 id = NULL_TREE;
5267 if (fns && is_overloaded_fn (fns))
5269 for (lkp_iterator iter (get_fns (fns)); iter; ++iter)
5271 tree fndecl = *iter;
5272 if (TREE_CODE (fndecl) == FUNCTION_DECL)
5274 tree argtype = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
5275 if (same_type_p (TREE_TYPE (argtype), type))
5277 id = fndecl;
5278 break;
5283 if (id && BASELINK_P (fns))
5285 if (baselinkp)
5286 *baselinkp = fns;
5287 else
5288 baselink = fns;
5292 if (!id && CLASS_TYPE_P (type) && TYPE_BINFO (type))
5294 vec<tree> ambiguous = vNULL;
5295 tree binfo = TYPE_BINFO (type), base_binfo, ret = NULL_TREE;
5296 unsigned int ix;
5297 if (ambiguousp == NULL)
5298 ambiguousp = &ambiguous;
5299 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
5301 id = omp_reduction_lookup (loc, orig_id, BINFO_TYPE (base_binfo),
5302 baselinkp ? baselinkp : &baselink,
5303 ambiguousp);
5304 if (id == NULL_TREE)
5305 continue;
5306 if (!ambiguousp->is_empty ())
5307 ambiguousp->safe_push (id);
5308 else if (ret != NULL_TREE)
5310 ambiguousp->safe_push (ret);
5311 ambiguousp->safe_push (id);
5312 ret = NULL_TREE;
5314 else
5315 ret = id;
5317 if (ambiguousp != &ambiguous)
5318 return ret;
5319 if (!ambiguous.is_empty ())
5321 const char *str = _("candidates are:");
5322 unsigned int idx;
5323 tree udr;
5324 error_at (loc, "user defined reduction lookup is ambiguous");
5325 FOR_EACH_VEC_ELT (ambiguous, idx, udr)
5327 inform (DECL_SOURCE_LOCATION (udr), "%s %#qD", str, udr);
5328 if (idx == 0)
5329 str = get_spaces (str);
5331 ambiguous.release ();
5332 ret = error_mark_node;
5333 baselink = NULL_TREE;
5335 id = ret;
5337 if (id && baselink)
5338 perform_or_defer_access_check (BASELINK_BINFO (baselink),
5339 id, id, tf_warning_or_error);
5340 return id;
5343 /* Helper function for cp_parser_omp_declare_reduction_exprs
5344 and tsubst_omp_udr.
5345 Remove CLEANUP_STMT for data (omp_priv variable).
5346 Also append INIT_EXPR for DECL_INITIAL of omp_priv after its
5347 DECL_EXPR. */
5349 tree
5350 cp_remove_omp_priv_cleanup_stmt (tree *tp, int *walk_subtrees, void *data)
5352 if (TYPE_P (*tp))
5353 *walk_subtrees = 0;
5354 else if (TREE_CODE (*tp) == CLEANUP_STMT && CLEANUP_DECL (*tp) == (tree) data)
5355 *tp = CLEANUP_BODY (*tp);
5356 else if (TREE_CODE (*tp) == DECL_EXPR)
5358 tree decl = DECL_EXPR_DECL (*tp);
5359 if (!processing_template_decl
5360 && decl == (tree) data
5361 && DECL_INITIAL (decl)
5362 && DECL_INITIAL (decl) != error_mark_node)
5364 tree list = NULL_TREE;
5365 append_to_statement_list_force (*tp, &list);
5366 tree init_expr = build2 (INIT_EXPR, void_type_node,
5367 decl, DECL_INITIAL (decl));
5368 DECL_INITIAL (decl) = NULL_TREE;
5369 append_to_statement_list_force (init_expr, &list);
5370 *tp = list;
5373 return NULL_TREE;
5376 /* Data passed from cp_check_omp_declare_reduction to
5377 cp_check_omp_declare_reduction_r. */
5379 struct cp_check_omp_declare_reduction_data
5381 location_t loc;
5382 tree stmts[7];
5383 bool combiner_p;
5386 /* Helper function for cp_check_omp_declare_reduction, called via
5387 cp_walk_tree. */
5389 static tree
5390 cp_check_omp_declare_reduction_r (tree *tp, int *, void *data)
5392 struct cp_check_omp_declare_reduction_data *udr_data
5393 = (struct cp_check_omp_declare_reduction_data *) data;
5394 if (SSA_VAR_P (*tp)
5395 && !DECL_ARTIFICIAL (*tp)
5396 && *tp != DECL_EXPR_DECL (udr_data->stmts[udr_data->combiner_p ? 0 : 3])
5397 && *tp != DECL_EXPR_DECL (udr_data->stmts[udr_data->combiner_p ? 1 : 4]))
5399 location_t loc = udr_data->loc;
5400 if (udr_data->combiner_p)
5401 error_at (loc, "%<#pragma omp declare reduction%> combiner refers to "
5402 "variable %qD which is not %<omp_out%> nor %<omp_in%>",
5403 *tp);
5404 else
5405 error_at (loc, "%<#pragma omp declare reduction%> initializer refers "
5406 "to variable %qD which is not %<omp_priv%> nor "
5407 "%<omp_orig%>",
5408 *tp);
5409 return *tp;
5411 return NULL_TREE;
5414 /* Diagnose violation of OpenMP #pragma omp declare reduction restrictions. */
5416 void
5417 cp_check_omp_declare_reduction (tree udr)
5419 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (udr)));
5420 gcc_assert (TYPE_REF_P (type));
5421 type = TREE_TYPE (type);
5422 int i;
5423 location_t loc = DECL_SOURCE_LOCATION (udr);
5425 if (type == error_mark_node)
5426 return;
5427 if (ARITHMETIC_TYPE_P (type))
5429 static enum tree_code predef_codes[]
5430 = { PLUS_EXPR, MULT_EXPR, MINUS_EXPR, BIT_AND_EXPR, BIT_XOR_EXPR,
5431 BIT_IOR_EXPR, TRUTH_ANDIF_EXPR, TRUTH_ORIF_EXPR };
5432 for (i = 0; i < 8; i++)
5434 tree id = omp_reduction_id (predef_codes[i], NULL_TREE, NULL_TREE);
5435 const char *n1 = IDENTIFIER_POINTER (DECL_NAME (udr));
5436 const char *n2 = IDENTIFIER_POINTER (id);
5437 if (strncmp (n1, n2, IDENTIFIER_LENGTH (id)) == 0
5438 && (n1[IDENTIFIER_LENGTH (id)] == '~'
5439 || n1[IDENTIFIER_LENGTH (id)] == '\0'))
5440 break;
5443 if (i == 8
5444 && TREE_CODE (type) != COMPLEX_EXPR)
5446 const char prefix_minmax[] = "omp declare reduction m";
5447 size_t prefix_size = sizeof (prefix_minmax) - 1;
5448 const char *n = IDENTIFIER_POINTER (DECL_NAME (udr));
5449 if (strncmp (IDENTIFIER_POINTER (DECL_NAME (udr)),
5450 prefix_minmax, prefix_size) == 0
5451 && ((n[prefix_size] == 'i' && n[prefix_size + 1] == 'n')
5452 || (n[prefix_size] == 'a' && n[prefix_size + 1] == 'x'))
5453 && (n[prefix_size + 2] == '~' || n[prefix_size + 2] == '\0'))
5454 i = 0;
5456 if (i < 8)
5458 error_at (loc, "predeclared arithmetic type %qT in "
5459 "%<#pragma omp declare reduction%>", type);
5460 return;
5463 else if (TREE_CODE (type) == FUNCTION_TYPE
5464 || TREE_CODE (type) == METHOD_TYPE
5465 || TREE_CODE (type) == ARRAY_TYPE)
5467 error_at (loc, "function or array type %qT in "
5468 "%<#pragma omp declare reduction%>", type);
5469 return;
5471 else if (TYPE_REF_P (type))
5473 error_at (loc, "reference type %qT in %<#pragma omp declare reduction%>",
5474 type);
5475 return;
5477 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
5479 error_at (loc, "const, volatile or __restrict qualified type %qT in "
5480 "%<#pragma omp declare reduction%>", type);
5481 return;
5484 tree body = DECL_SAVED_TREE (udr);
5485 if (body == NULL_TREE || TREE_CODE (body) != STATEMENT_LIST)
5486 return;
5488 tree_stmt_iterator tsi;
5489 struct cp_check_omp_declare_reduction_data data;
5490 memset (data.stmts, 0, sizeof data.stmts);
5491 for (i = 0, tsi = tsi_start (body);
5492 i < 7 && !tsi_end_p (tsi);
5493 i++, tsi_next (&tsi))
5494 data.stmts[i] = tsi_stmt (tsi);
5495 data.loc = loc;
5496 gcc_assert (tsi_end_p (tsi));
5497 if (i >= 3)
5499 gcc_assert (TREE_CODE (data.stmts[0]) == DECL_EXPR
5500 && TREE_CODE (data.stmts[1]) == DECL_EXPR);
5501 if (TREE_NO_WARNING (DECL_EXPR_DECL (data.stmts[0])))
5502 return;
5503 data.combiner_p = true;
5504 if (cp_walk_tree (&data.stmts[2], cp_check_omp_declare_reduction_r,
5505 &data, NULL))
5506 TREE_NO_WARNING (DECL_EXPR_DECL (data.stmts[0])) = 1;
5508 if (i >= 6)
5510 gcc_assert (TREE_CODE (data.stmts[3]) == DECL_EXPR
5511 && TREE_CODE (data.stmts[4]) == DECL_EXPR);
5512 data.combiner_p = false;
5513 if (cp_walk_tree (&data.stmts[5], cp_check_omp_declare_reduction_r,
5514 &data, NULL)
5515 || cp_walk_tree (&DECL_INITIAL (DECL_EXPR_DECL (data.stmts[3])),
5516 cp_check_omp_declare_reduction_r, &data, NULL))
5517 TREE_NO_WARNING (DECL_EXPR_DECL (data.stmts[0])) = 1;
5518 if (i == 7)
5519 gcc_assert (TREE_CODE (data.stmts[6]) == DECL_EXPR);
5523 /* Helper function of finish_omp_clauses. Clone STMT as if we were making
5524 an inline call. But, remap
5525 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
5526 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
5528 static tree
5529 clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
5530 tree decl, tree placeholder)
5532 copy_body_data id;
5533 hash_map<tree, tree> decl_map;
5535 decl_map.put (omp_decl1, placeholder);
5536 decl_map.put (omp_decl2, decl);
5537 memset (&id, 0, sizeof (id));
5538 id.src_fn = DECL_CONTEXT (omp_decl1);
5539 id.dst_fn = current_function_decl;
5540 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
5541 id.decl_map = &decl_map;
5543 id.copy_decl = copy_decl_no_change;
5544 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
5545 id.transform_new_cfg = true;
5546 id.transform_return_to_modify = false;
5547 id.transform_lang_insert_block = NULL;
5548 id.eh_lp_nr = 0;
5549 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
5550 return stmt;
5553 /* Helper function of finish_omp_clauses, called via cp_walk_tree.
5554 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
5556 static tree
5557 find_omp_placeholder_r (tree *tp, int *, void *data)
5559 if (*tp == (tree) data)
5560 return *tp;
5561 return NULL_TREE;
5564 /* Helper function of finish_omp_clauses. Handle OMP_CLAUSE_REDUCTION C.
5565 Return true if there is some error and the clause should be removed. */
5567 static bool
5568 finish_omp_reduction_clause (tree c, bool *need_default_ctor, bool *need_dtor)
5570 tree t = OMP_CLAUSE_DECL (c);
5571 bool predefined = false;
5572 if (TREE_CODE (t) == TREE_LIST)
5574 gcc_assert (processing_template_decl);
5575 return false;
5577 tree type = TREE_TYPE (t);
5578 if (TREE_CODE (t) == MEM_REF)
5579 type = TREE_TYPE (type);
5580 if (TYPE_REF_P (type))
5581 type = TREE_TYPE (type);
5582 if (TREE_CODE (type) == ARRAY_TYPE)
5584 tree oatype = type;
5585 gcc_assert (TREE_CODE (t) != MEM_REF);
5586 while (TREE_CODE (type) == ARRAY_TYPE)
5587 type = TREE_TYPE (type);
5588 if (!processing_template_decl)
5590 t = require_complete_type (t);
5591 if (t == error_mark_node)
5592 return true;
5593 tree size = size_binop (EXACT_DIV_EXPR, TYPE_SIZE_UNIT (oatype),
5594 TYPE_SIZE_UNIT (type));
5595 if (integer_zerop (size))
5597 error_at (OMP_CLAUSE_LOCATION (c),
5598 "%qE in %<reduction%> clause is a zero size array",
5599 omp_clause_printable_decl (t));
5600 return true;
5602 size = size_binop (MINUS_EXPR, size, size_one_node);
5603 size = save_expr (size);
5604 tree index_type = build_index_type (size);
5605 tree atype = build_array_type (type, index_type);
5606 tree ptype = build_pointer_type (type);
5607 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
5608 t = build_fold_addr_expr (t);
5609 t = build2 (MEM_REF, atype, t, build_int_cst (ptype, 0));
5610 OMP_CLAUSE_DECL (c) = t;
5613 if (type == error_mark_node)
5614 return true;
5615 else if (ARITHMETIC_TYPE_P (type))
5616 switch (OMP_CLAUSE_REDUCTION_CODE (c))
5618 case PLUS_EXPR:
5619 case MULT_EXPR:
5620 case MINUS_EXPR:
5621 predefined = true;
5622 break;
5623 case MIN_EXPR:
5624 case MAX_EXPR:
5625 if (TREE_CODE (type) == COMPLEX_TYPE)
5626 break;
5627 predefined = true;
5628 break;
5629 case BIT_AND_EXPR:
5630 case BIT_IOR_EXPR:
5631 case BIT_XOR_EXPR:
5632 if (FLOAT_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
5633 break;
5634 predefined = true;
5635 break;
5636 case TRUTH_ANDIF_EXPR:
5637 case TRUTH_ORIF_EXPR:
5638 if (FLOAT_TYPE_P (type))
5639 break;
5640 predefined = true;
5641 break;
5642 default:
5643 break;
5645 else if (TYPE_READONLY (type))
5647 error_at (OMP_CLAUSE_LOCATION (c),
5648 "%qE has const type for %<reduction%>",
5649 omp_clause_printable_decl (t));
5650 return true;
5652 else if (!processing_template_decl)
5654 t = require_complete_type (t);
5655 if (t == error_mark_node)
5656 return true;
5657 OMP_CLAUSE_DECL (c) = t;
5660 if (predefined)
5662 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = NULL_TREE;
5663 return false;
5665 else if (processing_template_decl)
5667 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == error_mark_node)
5668 return true;
5669 return false;
5672 tree id = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
5674 type = TYPE_MAIN_VARIANT (type);
5675 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = NULL_TREE;
5676 if (id == NULL_TREE)
5677 id = omp_reduction_id (OMP_CLAUSE_REDUCTION_CODE (c),
5678 NULL_TREE, NULL_TREE);
5679 id = omp_reduction_lookup (OMP_CLAUSE_LOCATION (c), id, type, NULL, NULL);
5680 if (id)
5682 if (id == error_mark_node)
5683 return true;
5684 mark_used (id);
5685 tree body = DECL_SAVED_TREE (id);
5686 if (!body)
5687 return true;
5688 if (TREE_CODE (body) == STATEMENT_LIST)
5690 tree_stmt_iterator tsi;
5691 tree placeholder = NULL_TREE, decl_placeholder = NULL_TREE;
5692 int i;
5693 tree stmts[7];
5694 tree atype = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (id)));
5695 atype = TREE_TYPE (atype);
5696 bool need_static_cast = !same_type_p (type, atype);
5697 memset (stmts, 0, sizeof stmts);
5698 for (i = 0, tsi = tsi_start (body);
5699 i < 7 && !tsi_end_p (tsi);
5700 i++, tsi_next (&tsi))
5701 stmts[i] = tsi_stmt (tsi);
5702 gcc_assert (tsi_end_p (tsi));
5704 if (i >= 3)
5706 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
5707 && TREE_CODE (stmts[1]) == DECL_EXPR);
5708 placeholder = build_lang_decl (VAR_DECL, NULL_TREE, type);
5709 DECL_ARTIFICIAL (placeholder) = 1;
5710 DECL_IGNORED_P (placeholder) = 1;
5711 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
5712 if (TREE_CODE (t) == MEM_REF)
5714 decl_placeholder = build_lang_decl (VAR_DECL, NULL_TREE,
5715 type);
5716 DECL_ARTIFICIAL (decl_placeholder) = 1;
5717 DECL_IGNORED_P (decl_placeholder) = 1;
5718 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c) = decl_placeholder;
5720 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[0])))
5721 cxx_mark_addressable (placeholder);
5722 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[1]))
5723 && (decl_placeholder
5724 || !TYPE_REF_P (TREE_TYPE (OMP_CLAUSE_DECL (c)))))
5725 cxx_mark_addressable (decl_placeholder ? decl_placeholder
5726 : OMP_CLAUSE_DECL (c));
5727 tree omp_out = placeholder;
5728 tree omp_in = decl_placeholder ? decl_placeholder
5729 : convert_from_reference (OMP_CLAUSE_DECL (c));
5730 if (need_static_cast)
5732 tree rtype = build_reference_type (atype);
5733 omp_out = build_static_cast (rtype, omp_out,
5734 tf_warning_or_error);
5735 omp_in = build_static_cast (rtype, omp_in,
5736 tf_warning_or_error);
5737 if (omp_out == error_mark_node || omp_in == error_mark_node)
5738 return true;
5739 omp_out = convert_from_reference (omp_out);
5740 omp_in = convert_from_reference (omp_in);
5742 OMP_CLAUSE_REDUCTION_MERGE (c)
5743 = clone_omp_udr (stmts[2], DECL_EXPR_DECL (stmts[0]),
5744 DECL_EXPR_DECL (stmts[1]), omp_in, omp_out);
5746 if (i >= 6)
5748 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
5749 && TREE_CODE (stmts[4]) == DECL_EXPR);
5750 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[3]))
5751 && (decl_placeholder
5752 || !TYPE_REF_P (TREE_TYPE (OMP_CLAUSE_DECL (c)))))
5753 cxx_mark_addressable (decl_placeholder ? decl_placeholder
5754 : OMP_CLAUSE_DECL (c));
5755 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[4])))
5756 cxx_mark_addressable (placeholder);
5757 tree omp_priv = decl_placeholder ? decl_placeholder
5758 : convert_from_reference (OMP_CLAUSE_DECL (c));
5759 tree omp_orig = placeholder;
5760 if (need_static_cast)
5762 if (i == 7)
5764 error_at (OMP_CLAUSE_LOCATION (c),
5765 "user defined reduction with constructor "
5766 "initializer for base class %qT", atype);
5767 return true;
5769 tree rtype = build_reference_type (atype);
5770 omp_priv = build_static_cast (rtype, omp_priv,
5771 tf_warning_or_error);
5772 omp_orig = build_static_cast (rtype, omp_orig,
5773 tf_warning_or_error);
5774 if (omp_priv == error_mark_node
5775 || omp_orig == error_mark_node)
5776 return true;
5777 omp_priv = convert_from_reference (omp_priv);
5778 omp_orig = convert_from_reference (omp_orig);
5780 if (i == 6)
5781 *need_default_ctor = true;
5782 OMP_CLAUSE_REDUCTION_INIT (c)
5783 = clone_omp_udr (stmts[5], DECL_EXPR_DECL (stmts[4]),
5784 DECL_EXPR_DECL (stmts[3]),
5785 omp_priv, omp_orig);
5786 if (cp_walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
5787 find_omp_placeholder_r, placeholder, NULL))
5788 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
5790 else if (i >= 3)
5792 if (CLASS_TYPE_P (type) && !pod_type_p (type))
5793 *need_default_ctor = true;
5794 else
5796 tree init;
5797 tree v = decl_placeholder ? decl_placeholder
5798 : convert_from_reference (t);
5799 if (AGGREGATE_TYPE_P (TREE_TYPE (v)))
5800 init = build_constructor (TREE_TYPE (v), NULL);
5801 else
5802 init = fold_convert (TREE_TYPE (v), integer_zero_node);
5803 OMP_CLAUSE_REDUCTION_INIT (c)
5804 = build2 (INIT_EXPR, TREE_TYPE (v), v, init);
5809 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
5810 *need_dtor = true;
5811 else
5813 error_at (OMP_CLAUSE_LOCATION (c),
5814 "user defined reduction not found for %qE",
5815 omp_clause_printable_decl (t));
5816 return true;
5818 if (TREE_CODE (OMP_CLAUSE_DECL (c)) == MEM_REF)
5819 gcc_assert (TYPE_SIZE_UNIT (type)
5820 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST);
5821 return false;
5824 /* Called from finish_struct_1. linear(this) or linear(this:step)
5825 clauses might not be finalized yet because the class has been incomplete
5826 when parsing #pragma omp declare simd methods. Fix those up now. */
5828 void
5829 finish_omp_declare_simd_methods (tree t)
5831 if (processing_template_decl)
5832 return;
5834 for (tree x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
5836 if (TREE_CODE (TREE_TYPE (x)) != METHOD_TYPE)
5837 continue;
5838 tree ods = lookup_attribute ("omp declare simd", DECL_ATTRIBUTES (x));
5839 if (!ods || !TREE_VALUE (ods))
5840 continue;
5841 for (tree c = TREE_VALUE (TREE_VALUE (ods)); c; c = OMP_CLAUSE_CHAIN (c))
5842 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
5843 && integer_zerop (OMP_CLAUSE_DECL (c))
5844 && OMP_CLAUSE_LINEAR_STEP (c)
5845 && TYPE_PTR_P (TREE_TYPE (OMP_CLAUSE_LINEAR_STEP (c))))
5847 tree s = OMP_CLAUSE_LINEAR_STEP (c);
5848 s = fold_convert_loc (OMP_CLAUSE_LOCATION (c), sizetype, s);
5849 s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MULT_EXPR,
5850 sizetype, s, TYPE_SIZE_UNIT (t));
5851 OMP_CLAUSE_LINEAR_STEP (c) = s;
5856 /* Adjust sink depend clause to take into account pointer offsets.
5858 Return TRUE if there was a problem processing the offset, and the
5859 whole clause should be removed. */
5861 static bool
5862 cp_finish_omp_clause_depend_sink (tree sink_clause)
5864 tree t = OMP_CLAUSE_DECL (sink_clause);
5865 gcc_assert (TREE_CODE (t) == TREE_LIST);
5867 /* Make sure we don't adjust things twice for templates. */
5868 if (processing_template_decl)
5869 return false;
5871 for (; t; t = TREE_CHAIN (t))
5873 tree decl = TREE_VALUE (t);
5874 if (TYPE_PTR_P (TREE_TYPE (decl)))
5876 tree offset = TREE_PURPOSE (t);
5877 bool neg = wi::neg_p (wi::to_wide (offset));
5878 offset = fold_unary (ABS_EXPR, TREE_TYPE (offset), offset);
5879 decl = mark_rvalue_use (decl);
5880 decl = convert_from_reference (decl);
5881 tree t2 = pointer_int_sum (OMP_CLAUSE_LOCATION (sink_clause),
5882 neg ? MINUS_EXPR : PLUS_EXPR,
5883 decl, offset);
5884 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (sink_clause),
5885 MINUS_EXPR, sizetype,
5886 fold_convert (sizetype, t2),
5887 fold_convert (sizetype, decl));
5888 if (t2 == error_mark_node)
5889 return true;
5890 TREE_PURPOSE (t) = t2;
5893 return false;
5896 /* Finish OpenMP iterators ITER. Return true if they are errorneous
5897 and clauses containing them should be removed. */
5899 static bool
5900 cp_omp_finish_iterators (tree iter)
5902 bool ret = false;
5903 for (tree it = iter; it; it = TREE_CHAIN (it))
5905 tree var = TREE_VEC_ELT (it, 0);
5906 tree begin = TREE_VEC_ELT (it, 1);
5907 tree end = TREE_VEC_ELT (it, 2);
5908 tree step = TREE_VEC_ELT (it, 3);
5909 tree orig_step;
5910 tree type = TREE_TYPE (var);
5911 location_t loc = DECL_SOURCE_LOCATION (var);
5912 if (type == error_mark_node)
5914 ret = true;
5915 continue;
5917 if (type_dependent_expression_p (var))
5918 continue;
5919 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
5921 error_at (loc, "iterator %qD has neither integral nor pointer type",
5922 var);
5923 ret = true;
5924 continue;
5926 else if (TYPE_READONLY (type))
5928 error_at (loc, "iterator %qD has const qualified type", var);
5929 ret = true;
5930 continue;
5932 if (type_dependent_expression_p (begin)
5933 || type_dependent_expression_p (end)
5934 || type_dependent_expression_p (step))
5935 continue;
5936 else if (error_operand_p (step))
5938 ret = true;
5939 continue;
5941 else if (!INTEGRAL_TYPE_P (TREE_TYPE (step)))
5943 error_at (EXPR_LOC_OR_LOC (step, loc),
5944 "iterator step with non-integral type");
5945 ret = true;
5946 continue;
5949 begin = mark_rvalue_use (begin);
5950 end = mark_rvalue_use (end);
5951 step = mark_rvalue_use (step);
5952 begin = cp_build_c_cast (type, begin, tf_warning_or_error);
5953 end = cp_build_c_cast (type, end, tf_warning_or_error);
5954 orig_step = step;
5955 if (!processing_template_decl)
5956 step = orig_step = save_expr (step);
5957 tree stype = POINTER_TYPE_P (type) ? sizetype : type;
5958 step = cp_build_c_cast (stype, step, tf_warning_or_error);
5959 if (POINTER_TYPE_P (type) && !processing_template_decl)
5961 begin = save_expr (begin);
5962 step = pointer_int_sum (loc, PLUS_EXPR, begin, step);
5963 step = fold_build2_loc (loc, MINUS_EXPR, sizetype,
5964 fold_convert (sizetype, step),
5965 fold_convert (sizetype, begin));
5966 step = fold_convert (ssizetype, step);
5968 if (!processing_template_decl)
5970 begin = maybe_constant_value (begin);
5971 end = maybe_constant_value (end);
5972 step = maybe_constant_value (step);
5973 orig_step = maybe_constant_value (orig_step);
5975 if (integer_zerop (step))
5977 error_at (loc, "iterator %qD has zero step", var);
5978 ret = true;
5979 continue;
5982 if (begin == error_mark_node
5983 || end == error_mark_node
5984 || step == error_mark_node
5985 || orig_step == error_mark_node)
5987 ret = true;
5988 continue;
5991 if (!processing_template_decl)
5993 begin = fold_build_cleanup_point_expr (TREE_TYPE (begin), begin);
5994 end = fold_build_cleanup_point_expr (TREE_TYPE (end), end);
5995 step = fold_build_cleanup_point_expr (TREE_TYPE (step), step);
5996 orig_step = fold_build_cleanup_point_expr (TREE_TYPE (orig_step),
5997 orig_step);
5999 hash_set<tree> pset;
6000 tree it2;
6001 for (it2 = TREE_CHAIN (it); it2; it2 = TREE_CHAIN (it2))
6003 tree var2 = TREE_VEC_ELT (it2, 0);
6004 tree begin2 = TREE_VEC_ELT (it2, 1);
6005 tree end2 = TREE_VEC_ELT (it2, 2);
6006 tree step2 = TREE_VEC_ELT (it2, 3);
6007 location_t loc2 = DECL_SOURCE_LOCATION (var2);
6008 if (cp_walk_tree (&begin2, find_omp_placeholder_r, var, &pset))
6010 error_at (EXPR_LOC_OR_LOC (begin2, loc2),
6011 "begin expression refers to outer iterator %qD", var);
6012 break;
6014 else if (cp_walk_tree (&end2, find_omp_placeholder_r, var, &pset))
6016 error_at (EXPR_LOC_OR_LOC (end2, loc2),
6017 "end expression refers to outer iterator %qD", var);
6018 break;
6020 else if (cp_walk_tree (&step2, find_omp_placeholder_r, var, &pset))
6022 error_at (EXPR_LOC_OR_LOC (step2, loc2),
6023 "step expression refers to outer iterator %qD", var);
6024 break;
6027 if (it2)
6029 ret = true;
6030 continue;
6032 TREE_VEC_ELT (it, 1) = begin;
6033 TREE_VEC_ELT (it, 2) = end;
6034 if (processing_template_decl)
6035 TREE_VEC_ELT (it, 3) = orig_step;
6036 else
6038 TREE_VEC_ELT (it, 3) = step;
6039 TREE_VEC_ELT (it, 4) = orig_step;
6042 return ret;
6045 /* For all elements of CLAUSES, validate them vs OpenMP constraints.
6046 Remove any elements from the list that are invalid. */
6048 tree
6049 finish_omp_clauses (tree clauses, enum c_omp_region_type ort)
6051 bitmap_head generic_head, firstprivate_head, lastprivate_head;
6052 bitmap_head aligned_head, map_head, map_field_head, oacc_reduction_head;
6053 tree c, t, *pc;
6054 tree safelen = NULL_TREE;
6055 bool branch_seen = false;
6056 bool copyprivate_seen = false;
6057 bool ordered_seen = false;
6058 bool oacc_async = false;
6059 tree last_iterators = NULL_TREE;
6060 bool last_iterators_remove = false;
6061 bool reduction_seen = false;
6063 bitmap_obstack_initialize (NULL);
6064 bitmap_initialize (&generic_head, &bitmap_default_obstack);
6065 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
6066 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
6067 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
6068 /* If ort == C_ORT_OMP_DECLARE_SIMD used as uniform_head instead. */
6069 bitmap_initialize (&map_head, &bitmap_default_obstack);
6070 bitmap_initialize (&map_field_head, &bitmap_default_obstack);
6071 /* If ort == C_ORT_OMP used as nontemporal_head instead. */
6072 bitmap_initialize (&oacc_reduction_head, &bitmap_default_obstack);
6074 if (ort & C_ORT_ACC)
6075 for (c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
6076 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ASYNC)
6078 oacc_async = true;
6079 break;
6082 for (pc = &clauses, c = clauses; c ; c = *pc)
6084 bool remove = false;
6085 bool field_ok = false;
6087 switch (OMP_CLAUSE_CODE (c))
6089 case OMP_CLAUSE_SHARED:
6090 field_ok = ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP);
6091 goto check_dup_generic;
6092 case OMP_CLAUSE_PRIVATE:
6093 field_ok = ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP);
6094 goto check_dup_generic;
6095 case OMP_CLAUSE_REDUCTION:
6096 reduction_seen = true;
6097 /* FALLTHRU */
6098 case OMP_CLAUSE_IN_REDUCTION:
6099 case OMP_CLAUSE_TASK_REDUCTION:
6100 field_ok = ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP);
6101 t = OMP_CLAUSE_DECL (c);
6102 if (TREE_CODE (t) == TREE_LIST)
6104 if (handle_omp_array_sections (c, ort))
6106 remove = true;
6107 break;
6109 if (TREE_CODE (t) == TREE_LIST)
6111 while (TREE_CODE (t) == TREE_LIST)
6112 t = TREE_CHAIN (t);
6114 else
6116 gcc_assert (TREE_CODE (t) == MEM_REF);
6117 t = TREE_OPERAND (t, 0);
6118 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
6119 t = TREE_OPERAND (t, 0);
6120 if (TREE_CODE (t) == ADDR_EXPR
6121 || INDIRECT_REF_P (t))
6122 t = TREE_OPERAND (t, 0);
6124 tree n = omp_clause_decl_field (t);
6125 if (n)
6126 t = n;
6127 goto check_dup_generic_t;
6129 if (oacc_async)
6130 cxx_mark_addressable (t);
6131 goto check_dup_generic;
6132 case OMP_CLAUSE_COPYPRIVATE:
6133 copyprivate_seen = true;
6134 field_ok = ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP);
6135 goto check_dup_generic;
6136 case OMP_CLAUSE_COPYIN:
6137 goto check_dup_generic;
6138 case OMP_CLAUSE_LINEAR:
6139 field_ok = ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP);
6140 t = OMP_CLAUSE_DECL (c);
6141 if (ort != C_ORT_OMP_DECLARE_SIMD
6142 && OMP_CLAUSE_LINEAR_KIND (c) != OMP_CLAUSE_LINEAR_DEFAULT)
6144 error_at (OMP_CLAUSE_LOCATION (c),
6145 "modifier should not be specified in %<linear%> "
6146 "clause on %<simd%> or %<for%> constructs");
6147 OMP_CLAUSE_LINEAR_KIND (c) = OMP_CLAUSE_LINEAR_DEFAULT;
6149 if ((VAR_P (t) || TREE_CODE (t) == PARM_DECL)
6150 && !type_dependent_expression_p (t))
6152 tree type = TREE_TYPE (t);
6153 if ((OMP_CLAUSE_LINEAR_KIND (c) == OMP_CLAUSE_LINEAR_REF
6154 || OMP_CLAUSE_LINEAR_KIND (c) == OMP_CLAUSE_LINEAR_UVAL)
6155 && !TYPE_REF_P (type))
6157 error_at (OMP_CLAUSE_LOCATION (c),
6158 "linear clause with %qs modifier applied to "
6159 "non-reference variable with %qT type",
6160 OMP_CLAUSE_LINEAR_KIND (c) == OMP_CLAUSE_LINEAR_REF
6161 ? "ref" : "uval", TREE_TYPE (t));
6162 remove = true;
6163 break;
6165 if (TYPE_REF_P (type))
6166 type = TREE_TYPE (type);
6167 if (OMP_CLAUSE_LINEAR_KIND (c) != OMP_CLAUSE_LINEAR_REF)
6169 if (!INTEGRAL_TYPE_P (type)
6170 && !TYPE_PTR_P (type))
6172 error_at (OMP_CLAUSE_LOCATION (c),
6173 "linear clause applied to non-integral "
6174 "non-pointer variable with %qT type",
6175 TREE_TYPE (t));
6176 remove = true;
6177 break;
6181 t = OMP_CLAUSE_LINEAR_STEP (c);
6182 if (t == NULL_TREE)
6183 t = integer_one_node;
6184 if (t == error_mark_node)
6186 remove = true;
6187 break;
6189 else if (!type_dependent_expression_p (t)
6190 && !INTEGRAL_TYPE_P (TREE_TYPE (t))
6191 && (ort != C_ORT_OMP_DECLARE_SIMD
6192 || TREE_CODE (t) != PARM_DECL
6193 || !TYPE_REF_P (TREE_TYPE (t))
6194 || !INTEGRAL_TYPE_P (TREE_TYPE (TREE_TYPE (t)))))
6196 error_at (OMP_CLAUSE_LOCATION (c),
6197 "linear step expression must be integral");
6198 remove = true;
6199 break;
6201 else
6203 t = mark_rvalue_use (t);
6204 if (ort == C_ORT_OMP_DECLARE_SIMD && TREE_CODE (t) == PARM_DECL)
6206 OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c) = 1;
6207 goto check_dup_generic;
6209 if (!processing_template_decl
6210 && (VAR_P (OMP_CLAUSE_DECL (c))
6211 || TREE_CODE (OMP_CLAUSE_DECL (c)) == PARM_DECL))
6213 if (ort == C_ORT_OMP_DECLARE_SIMD)
6215 t = maybe_constant_value (t);
6216 if (TREE_CODE (t) != INTEGER_CST)
6218 error_at (OMP_CLAUSE_LOCATION (c),
6219 "%<linear%> clause step %qE is neither "
6220 "constant nor a parameter", t);
6221 remove = true;
6222 break;
6225 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6226 tree type = TREE_TYPE (OMP_CLAUSE_DECL (c));
6227 if (TYPE_REF_P (type))
6228 type = TREE_TYPE (type);
6229 if (OMP_CLAUSE_LINEAR_KIND (c) == OMP_CLAUSE_LINEAR_REF)
6231 type = build_pointer_type (type);
6232 tree d = fold_convert (type, OMP_CLAUSE_DECL (c));
6233 t = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
6234 d, t);
6235 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c),
6236 MINUS_EXPR, sizetype,
6237 fold_convert (sizetype, t),
6238 fold_convert (sizetype, d));
6239 if (t == error_mark_node)
6241 remove = true;
6242 break;
6245 else if (TYPE_PTR_P (type)
6246 /* Can't multiply the step yet if *this
6247 is still incomplete type. */
6248 && (ort != C_ORT_OMP_DECLARE_SIMD
6249 || TREE_CODE (OMP_CLAUSE_DECL (c)) != PARM_DECL
6250 || !DECL_ARTIFICIAL (OMP_CLAUSE_DECL (c))
6251 || DECL_NAME (OMP_CLAUSE_DECL (c))
6252 != this_identifier
6253 || !TYPE_BEING_DEFINED (TREE_TYPE (type))))
6255 tree d = convert_from_reference (OMP_CLAUSE_DECL (c));
6256 t = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
6257 d, t);
6258 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c),
6259 MINUS_EXPR, sizetype,
6260 fold_convert (sizetype, t),
6261 fold_convert (sizetype, d));
6262 if (t == error_mark_node)
6264 remove = true;
6265 break;
6268 else
6269 t = fold_convert (type, t);
6271 OMP_CLAUSE_LINEAR_STEP (c) = t;
6273 goto check_dup_generic;
6274 check_dup_generic:
6275 t = omp_clause_decl_field (OMP_CLAUSE_DECL (c));
6276 if (t)
6278 if (!remove && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_SHARED)
6279 omp_note_field_privatization (t, OMP_CLAUSE_DECL (c));
6281 else
6282 t = OMP_CLAUSE_DECL (c);
6283 check_dup_generic_t:
6284 if (t == current_class_ptr
6285 && (ort != C_ORT_OMP_DECLARE_SIMD
6286 || (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_LINEAR
6287 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_UNIFORM)))
6289 error_at (OMP_CLAUSE_LOCATION (c),
6290 "%<this%> allowed in OpenMP only in %<declare simd%>"
6291 " clauses");
6292 remove = true;
6293 break;
6295 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL
6296 && (!field_ok || TREE_CODE (t) != FIELD_DECL))
6298 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
6299 break;
6300 if (DECL_P (t))
6301 error_at (OMP_CLAUSE_LOCATION (c),
6302 "%qD is not a variable in clause %qs", t,
6303 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6304 else
6305 error_at (OMP_CLAUSE_LOCATION (c),
6306 "%qE is not a variable in clause %qs", t,
6307 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6308 remove = true;
6310 else if (ort == C_ORT_ACC
6311 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
6313 if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
6315 error_at (OMP_CLAUSE_LOCATION (c),
6316 "%qD appears more than once in reduction clauses",
6318 remove = true;
6320 else
6321 bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
6323 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
6324 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
6325 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
6327 error_at (OMP_CLAUSE_LOCATION (c),
6328 "%qD appears more than once in data clauses", t);
6329 remove = true;
6331 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
6332 && bitmap_bit_p (&map_head, DECL_UID (t)))
6334 if (ort == C_ORT_ACC)
6335 error_at (OMP_CLAUSE_LOCATION (c),
6336 "%qD appears more than once in data clauses", t);
6337 else
6338 error_at (OMP_CLAUSE_LOCATION (c),
6339 "%qD appears both in data and map clauses", t);
6340 remove = true;
6342 else
6343 bitmap_set_bit (&generic_head, DECL_UID (t));
6344 if (!field_ok)
6345 break;
6346 handle_field_decl:
6347 if (!remove
6348 && TREE_CODE (t) == FIELD_DECL
6349 && t == OMP_CLAUSE_DECL (c)
6350 && ort != C_ORT_ACC)
6352 OMP_CLAUSE_DECL (c)
6353 = omp_privatize_field (t, (OMP_CLAUSE_CODE (c)
6354 == OMP_CLAUSE_SHARED));
6355 if (OMP_CLAUSE_DECL (c) == error_mark_node)
6356 remove = true;
6358 break;
6360 case OMP_CLAUSE_FIRSTPRIVATE:
6361 t = omp_clause_decl_field (OMP_CLAUSE_DECL (c));
6362 if (t)
6363 omp_note_field_privatization (t, OMP_CLAUSE_DECL (c));
6364 else
6365 t = OMP_CLAUSE_DECL (c);
6366 if (ort != C_ORT_ACC && t == current_class_ptr)
6368 error_at (OMP_CLAUSE_LOCATION (c),
6369 "%<this%> allowed in OpenMP only in %<declare simd%>"
6370 " clauses");
6371 remove = true;
6372 break;
6374 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL
6375 && ((ort & C_ORT_OMP_DECLARE_SIMD) != C_ORT_OMP
6376 || TREE_CODE (t) != FIELD_DECL))
6378 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
6379 break;
6380 if (DECL_P (t))
6381 error_at (OMP_CLAUSE_LOCATION (c),
6382 "%qD is not a variable in clause %<firstprivate%>",
6384 else
6385 error_at (OMP_CLAUSE_LOCATION (c),
6386 "%qE is not a variable in clause %<firstprivate%>",
6388 remove = true;
6390 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
6391 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
6393 error_at (OMP_CLAUSE_LOCATION (c),
6394 "%qD appears more than once in data clauses", t);
6395 remove = true;
6397 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
6399 if (ort == C_ORT_ACC)
6400 error_at (OMP_CLAUSE_LOCATION (c),
6401 "%qD appears more than once in data clauses", t);
6402 else
6403 error_at (OMP_CLAUSE_LOCATION (c),
6404 "%qD appears both in data and map clauses", t);
6405 remove = true;
6407 else
6408 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
6409 goto handle_field_decl;
6411 case OMP_CLAUSE_LASTPRIVATE:
6412 t = omp_clause_decl_field (OMP_CLAUSE_DECL (c));
6413 if (t)
6414 omp_note_field_privatization (t, OMP_CLAUSE_DECL (c));
6415 else
6416 t = OMP_CLAUSE_DECL (c);
6417 if (t == current_class_ptr)
6419 error_at (OMP_CLAUSE_LOCATION (c),
6420 "%<this%> allowed in OpenMP only in %<declare simd%>"
6421 " clauses");
6422 remove = true;
6423 break;
6425 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL
6426 && ((ort & C_ORT_OMP_DECLARE_SIMD) != C_ORT_OMP
6427 || TREE_CODE (t) != FIELD_DECL))
6429 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
6430 break;
6431 if (DECL_P (t))
6432 error_at (OMP_CLAUSE_LOCATION (c),
6433 "%qD is not a variable in clause %<lastprivate%>",
6435 else
6436 error_at (OMP_CLAUSE_LOCATION (c),
6437 "%qE is not a variable in clause %<lastprivate%>",
6439 remove = true;
6441 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
6442 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
6444 error_at (OMP_CLAUSE_LOCATION (c),
6445 "%qD appears more than once in data clauses", t);
6446 remove = true;
6448 else
6449 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
6450 goto handle_field_decl;
6452 case OMP_CLAUSE_IF:
6453 t = OMP_CLAUSE_IF_EXPR (c);
6454 t = maybe_convert_cond (t);
6455 if (t == error_mark_node)
6456 remove = true;
6457 else if (!processing_template_decl)
6458 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6459 OMP_CLAUSE_IF_EXPR (c) = t;
6460 break;
6462 case OMP_CLAUSE_FINAL:
6463 t = OMP_CLAUSE_FINAL_EXPR (c);
6464 t = maybe_convert_cond (t);
6465 if (t == error_mark_node)
6466 remove = true;
6467 else if (!processing_template_decl)
6468 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6469 OMP_CLAUSE_FINAL_EXPR (c) = t;
6470 break;
6472 case OMP_CLAUSE_GANG:
6473 /* Operand 1 is the gang static: argument. */
6474 t = OMP_CLAUSE_OPERAND (c, 1);
6475 if (t != NULL_TREE)
6477 if (t == error_mark_node)
6478 remove = true;
6479 else if (!type_dependent_expression_p (t)
6480 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6482 error_at (OMP_CLAUSE_LOCATION (c),
6483 "%<gang%> static expression must be integral");
6484 remove = true;
6486 else
6488 t = mark_rvalue_use (t);
6489 if (!processing_template_decl)
6491 t = maybe_constant_value (t);
6492 if (TREE_CODE (t) == INTEGER_CST
6493 && tree_int_cst_sgn (t) != 1
6494 && t != integer_minus_one_node)
6496 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6497 "%<gang%> static value must be "
6498 "positive");
6499 t = integer_one_node;
6501 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6504 OMP_CLAUSE_OPERAND (c, 1) = t;
6506 /* Check operand 0, the num argument. */
6507 /* FALLTHRU */
6509 case OMP_CLAUSE_WORKER:
6510 case OMP_CLAUSE_VECTOR:
6511 if (OMP_CLAUSE_OPERAND (c, 0) == NULL_TREE)
6512 break;
6513 /* FALLTHRU */
6515 case OMP_CLAUSE_NUM_TASKS:
6516 case OMP_CLAUSE_NUM_TEAMS:
6517 case OMP_CLAUSE_NUM_THREADS:
6518 case OMP_CLAUSE_NUM_GANGS:
6519 case OMP_CLAUSE_NUM_WORKERS:
6520 case OMP_CLAUSE_VECTOR_LENGTH:
6521 t = OMP_CLAUSE_OPERAND (c, 0);
6522 if (t == error_mark_node)
6523 remove = true;
6524 else if (!type_dependent_expression_p (t)
6525 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6527 switch (OMP_CLAUSE_CODE (c))
6529 case OMP_CLAUSE_GANG:
6530 error_at (OMP_CLAUSE_LOCATION (c),
6531 "%<gang%> num expression must be integral"); break;
6532 case OMP_CLAUSE_VECTOR:
6533 error_at (OMP_CLAUSE_LOCATION (c),
6534 "%<vector%> length expression must be integral");
6535 break;
6536 case OMP_CLAUSE_WORKER:
6537 error_at (OMP_CLAUSE_LOCATION (c),
6538 "%<worker%> num expression must be integral");
6539 break;
6540 default:
6541 error_at (OMP_CLAUSE_LOCATION (c),
6542 "%qs expression must be integral",
6543 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6545 remove = true;
6547 else
6549 t = mark_rvalue_use (t);
6550 if (!processing_template_decl)
6552 t = maybe_constant_value (t);
6553 if (TREE_CODE (t) == INTEGER_CST
6554 && tree_int_cst_sgn (t) != 1)
6556 switch (OMP_CLAUSE_CODE (c))
6558 case OMP_CLAUSE_GANG:
6559 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6560 "%<gang%> num value must be positive");
6561 break;
6562 case OMP_CLAUSE_VECTOR:
6563 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6564 "%<vector%> length value must be "
6565 "positive");
6566 break;
6567 case OMP_CLAUSE_WORKER:
6568 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6569 "%<worker%> num value must be "
6570 "positive");
6571 break;
6572 default:
6573 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6574 "%qs value must be positive",
6575 omp_clause_code_name
6576 [OMP_CLAUSE_CODE (c)]);
6578 t = integer_one_node;
6580 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6582 OMP_CLAUSE_OPERAND (c, 0) = t;
6584 break;
6586 case OMP_CLAUSE_SCHEDULE:
6587 t = OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c);
6588 if (t == NULL)
6590 else if (t == error_mark_node)
6591 remove = true;
6592 else if (!type_dependent_expression_p (t)
6593 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6595 error_at (OMP_CLAUSE_LOCATION (c),
6596 "schedule chunk size expression must be integral");
6597 remove = true;
6599 else
6601 t = mark_rvalue_use (t);
6602 if (!processing_template_decl)
6604 t = maybe_constant_value (t);
6605 if (TREE_CODE (t) == INTEGER_CST
6606 && tree_int_cst_sgn (t) != 1)
6608 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6609 "chunk size value must be positive");
6610 t = integer_one_node;
6612 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6614 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
6616 break;
6618 case OMP_CLAUSE_SIMDLEN:
6619 case OMP_CLAUSE_SAFELEN:
6620 t = OMP_CLAUSE_OPERAND (c, 0);
6621 if (t == error_mark_node)
6622 remove = true;
6623 else if (!type_dependent_expression_p (t)
6624 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6626 error_at (OMP_CLAUSE_LOCATION (c),
6627 "%qs length expression must be integral",
6628 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6629 remove = true;
6631 else
6633 t = mark_rvalue_use (t);
6634 if (!processing_template_decl)
6636 t = maybe_constant_value (t);
6637 if (TREE_CODE (t) != INTEGER_CST
6638 || tree_int_cst_sgn (t) != 1)
6640 error_at (OMP_CLAUSE_LOCATION (c),
6641 "%qs length expression must be positive "
6642 "constant integer expression",
6643 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6644 remove = true;
6647 OMP_CLAUSE_OPERAND (c, 0) = t;
6648 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SAFELEN)
6649 safelen = c;
6651 break;
6653 case OMP_CLAUSE_ASYNC:
6654 t = OMP_CLAUSE_ASYNC_EXPR (c);
6655 if (t == error_mark_node)
6656 remove = true;
6657 else if (!type_dependent_expression_p (t)
6658 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6660 error_at (OMP_CLAUSE_LOCATION (c),
6661 "%<async%> expression must be integral");
6662 remove = true;
6664 else
6666 t = mark_rvalue_use (t);
6667 if (!processing_template_decl)
6668 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6669 OMP_CLAUSE_ASYNC_EXPR (c) = t;
6671 break;
6673 case OMP_CLAUSE_WAIT:
6674 t = OMP_CLAUSE_WAIT_EXPR (c);
6675 if (t == error_mark_node)
6676 remove = true;
6677 else if (!processing_template_decl)
6678 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6679 OMP_CLAUSE_WAIT_EXPR (c) = t;
6680 break;
6682 case OMP_CLAUSE_THREAD_LIMIT:
6683 t = OMP_CLAUSE_THREAD_LIMIT_EXPR (c);
6684 if (t == error_mark_node)
6685 remove = true;
6686 else if (!type_dependent_expression_p (t)
6687 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6689 error_at (OMP_CLAUSE_LOCATION (c),
6690 "%<thread_limit%> expression must be integral");
6691 remove = true;
6693 else
6695 t = mark_rvalue_use (t);
6696 if (!processing_template_decl)
6698 t = maybe_constant_value (t);
6699 if (TREE_CODE (t) == INTEGER_CST
6700 && tree_int_cst_sgn (t) != 1)
6702 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6703 "%<thread_limit%> value must be positive");
6704 t = integer_one_node;
6706 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6708 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
6710 break;
6712 case OMP_CLAUSE_DEVICE:
6713 t = OMP_CLAUSE_DEVICE_ID (c);
6714 if (t == error_mark_node)
6715 remove = true;
6716 else if (!type_dependent_expression_p (t)
6717 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6719 error_at (OMP_CLAUSE_LOCATION (c),
6720 "%<device%> id must be integral");
6721 remove = true;
6723 else
6725 t = mark_rvalue_use (t);
6726 if (!processing_template_decl)
6727 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6728 OMP_CLAUSE_DEVICE_ID (c) = t;
6730 break;
6732 case OMP_CLAUSE_DIST_SCHEDULE:
6733 t = OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c);
6734 if (t == NULL)
6736 else if (t == error_mark_node)
6737 remove = true;
6738 else if (!type_dependent_expression_p (t)
6739 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6741 error_at (OMP_CLAUSE_LOCATION (c),
6742 "%<dist_schedule%> chunk size expression must be "
6743 "integral");
6744 remove = true;
6746 else
6748 t = mark_rvalue_use (t);
6749 if (!processing_template_decl)
6750 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6751 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
6753 break;
6755 case OMP_CLAUSE_ALIGNED:
6756 t = OMP_CLAUSE_DECL (c);
6757 if (t == current_class_ptr && ort != C_ORT_OMP_DECLARE_SIMD)
6759 error_at (OMP_CLAUSE_LOCATION (c),
6760 "%<this%> allowed in OpenMP only in %<declare simd%>"
6761 " clauses");
6762 remove = true;
6763 break;
6765 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
6767 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
6768 break;
6769 if (DECL_P (t))
6770 error_at (OMP_CLAUSE_LOCATION (c),
6771 "%qD is not a variable in %<aligned%> clause", t);
6772 else
6773 error_at (OMP_CLAUSE_LOCATION (c),
6774 "%qE is not a variable in %<aligned%> clause", t);
6775 remove = true;
6777 else if (!type_dependent_expression_p (t)
6778 && !TYPE_PTR_P (TREE_TYPE (t))
6779 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE
6780 && (!TYPE_REF_P (TREE_TYPE (t))
6781 || (!INDIRECT_TYPE_P (TREE_TYPE (TREE_TYPE (t)))
6782 && (TREE_CODE (TREE_TYPE (TREE_TYPE (t)))
6783 != ARRAY_TYPE))))
6785 error_at (OMP_CLAUSE_LOCATION (c),
6786 "%qE in %<aligned%> clause is neither a pointer nor "
6787 "an array nor a reference to pointer or array", t);
6788 remove = true;
6790 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
6792 error_at (OMP_CLAUSE_LOCATION (c),
6793 "%qD appears more than once in %<aligned%> clauses",
6795 remove = true;
6797 else
6798 bitmap_set_bit (&aligned_head, DECL_UID (t));
6799 t = OMP_CLAUSE_ALIGNED_ALIGNMENT (c);
6800 if (t == error_mark_node)
6801 remove = true;
6802 else if (t == NULL_TREE)
6803 break;
6804 else if (!type_dependent_expression_p (t)
6805 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6807 error_at (OMP_CLAUSE_LOCATION (c),
6808 "%<aligned%> clause alignment expression must "
6809 "be integral");
6810 remove = true;
6812 else
6814 t = mark_rvalue_use (t);
6815 if (!processing_template_decl)
6817 t = maybe_constant_value (t);
6818 if (TREE_CODE (t) != INTEGER_CST
6819 || tree_int_cst_sgn (t) != 1)
6821 error_at (OMP_CLAUSE_LOCATION (c),
6822 "%<aligned%> clause alignment expression must "
6823 "be positive constant integer expression");
6824 remove = true;
6826 else
6827 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6829 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = t;
6831 break;
6833 case OMP_CLAUSE_NONTEMPORAL:
6834 t = OMP_CLAUSE_DECL (c);
6835 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
6837 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
6838 break;
6839 if (DECL_P (t))
6840 error_at (OMP_CLAUSE_LOCATION (c),
6841 "%qD is not a variable in %<nontemporal%> clause",
6843 else
6844 error_at (OMP_CLAUSE_LOCATION (c),
6845 "%qE is not a variable in %<nontemporal%> clause",
6847 remove = true;
6849 else if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
6851 error_at (OMP_CLAUSE_LOCATION (c),
6852 "%qD appears more than once in %<nontemporal%> "
6853 "clauses", t);
6854 remove = true;
6856 else
6857 bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
6858 break;
6860 case OMP_CLAUSE_DEPEND:
6861 t = OMP_CLAUSE_DECL (c);
6862 if (t == NULL_TREE)
6864 gcc_assert (OMP_CLAUSE_DEPEND_KIND (c)
6865 == OMP_CLAUSE_DEPEND_SOURCE);
6866 break;
6868 if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SINK)
6870 if (cp_finish_omp_clause_depend_sink (c))
6871 remove = true;
6872 break;
6874 if (TREE_CODE (t) == TREE_LIST
6875 && TREE_PURPOSE (t)
6876 && TREE_CODE (TREE_PURPOSE (t)) == TREE_VEC)
6878 if (TREE_PURPOSE (t) != last_iterators)
6879 last_iterators_remove
6880 = cp_omp_finish_iterators (TREE_PURPOSE (t));
6881 last_iterators = TREE_PURPOSE (t);
6882 t = TREE_VALUE (t);
6883 if (last_iterators_remove)
6884 t = error_mark_node;
6886 else
6887 last_iterators = NULL_TREE;
6889 if (TREE_CODE (t) == TREE_LIST)
6891 if (handle_omp_array_sections (c, ort))
6892 remove = true;
6893 else if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_DEPOBJ)
6895 error_at (OMP_CLAUSE_LOCATION (c),
6896 "%<depend%> clause with %<depobj%> dependence "
6897 "type on array section");
6898 remove = true;
6900 break;
6902 if (t == error_mark_node)
6903 remove = true;
6904 else if (t == current_class_ptr)
6906 error_at (OMP_CLAUSE_LOCATION (c),
6907 "%<this%> allowed in OpenMP only in %<declare simd%>"
6908 " clauses");
6909 remove = true;
6911 else if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
6912 break;
6913 else if (!lvalue_p (t))
6915 if (DECL_P (t))
6916 error_at (OMP_CLAUSE_LOCATION (c),
6917 "%qD is not lvalue expression nor array section "
6918 "in %<depend%> clause", t);
6919 else
6920 error_at (OMP_CLAUSE_LOCATION (c),
6921 "%qE is not lvalue expression nor array section "
6922 "in %<depend%> clause", t);
6923 remove = true;
6925 else if (TREE_CODE (t) == COMPONENT_REF
6926 && TREE_CODE (TREE_OPERAND (t, 1)) == FIELD_DECL
6927 && DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
6929 error_at (OMP_CLAUSE_LOCATION (c),
6930 "bit-field %qE in %qs clause", t, "depend");
6931 remove = true;
6933 else if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_DEPOBJ)
6935 if (!c_omp_depend_t_p (TYPE_REF_P (TREE_TYPE (t))
6936 ? TREE_TYPE (TREE_TYPE (t))
6937 : TREE_TYPE (t)))
6939 error_at (OMP_CLAUSE_LOCATION (c),
6940 "%qE does not have %<omp_depend_t%> type in "
6941 "%<depend%> clause with %<depobj%> dependence "
6942 "type", t);
6943 remove = true;
6946 else if (c_omp_depend_t_p (TYPE_REF_P (TREE_TYPE (t))
6947 ? TREE_TYPE (TREE_TYPE (t))
6948 : TREE_TYPE (t)))
6950 error_at (OMP_CLAUSE_LOCATION (c),
6951 "%qE should not have %<omp_depend_t%> type in "
6952 "%<depend%> clause with dependence type other than "
6953 "%<depobj%>", t);
6954 remove = true;
6956 if (!remove)
6958 tree addr = cp_build_addr_expr (t, tf_warning_or_error);
6959 if (addr == error_mark_node)
6960 remove = true;
6961 else
6963 t = cp_build_indirect_ref (addr, RO_UNARY_STAR,
6964 tf_warning_or_error);
6965 if (t == error_mark_node)
6966 remove = true;
6967 else if (TREE_CODE (OMP_CLAUSE_DECL (c)) == TREE_LIST
6968 && TREE_PURPOSE (OMP_CLAUSE_DECL (c))
6969 && (TREE_CODE (TREE_PURPOSE (OMP_CLAUSE_DECL (c)))
6970 == TREE_VEC))
6971 TREE_VALUE (OMP_CLAUSE_DECL (c)) = t;
6972 else
6973 OMP_CLAUSE_DECL (c) = t;
6976 break;
6978 case OMP_CLAUSE_MAP:
6979 case OMP_CLAUSE_TO:
6980 case OMP_CLAUSE_FROM:
6981 case OMP_CLAUSE__CACHE_:
6982 t = OMP_CLAUSE_DECL (c);
6983 if (TREE_CODE (t) == TREE_LIST)
6985 if (handle_omp_array_sections (c, ort))
6986 remove = true;
6987 else
6989 t = OMP_CLAUSE_DECL (c);
6990 if (TREE_CODE (t) != TREE_LIST
6991 && !type_dependent_expression_p (t)
6992 && !cp_omp_mappable_type (TREE_TYPE (t)))
6994 error_at (OMP_CLAUSE_LOCATION (c),
6995 "array section does not have mappable type "
6996 "in %qs clause",
6997 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6998 remove = true;
7000 while (TREE_CODE (t) == ARRAY_REF)
7001 t = TREE_OPERAND (t, 0);
7002 if (TREE_CODE (t) == COMPONENT_REF
7003 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
7005 while (TREE_CODE (t) == COMPONENT_REF)
7006 t = TREE_OPERAND (t, 0);
7007 if (REFERENCE_REF_P (t))
7008 t = TREE_OPERAND (t, 0);
7009 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
7010 break;
7011 if (bitmap_bit_p (&map_head, DECL_UID (t)))
7013 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
7014 error_at (OMP_CLAUSE_LOCATION (c),
7015 "%qD appears more than once in motion"
7016 " clauses", t);
7017 else if (ort == C_ORT_ACC)
7018 error_at (OMP_CLAUSE_LOCATION (c),
7019 "%qD appears more than once in data"
7020 " clauses", t);
7021 else
7022 error_at (OMP_CLAUSE_LOCATION (c),
7023 "%qD appears more than once in map"
7024 " clauses", t);
7025 remove = true;
7027 else
7029 bitmap_set_bit (&map_head, DECL_UID (t));
7030 bitmap_set_bit (&map_field_head, DECL_UID (t));
7034 break;
7036 if (t == error_mark_node)
7038 remove = true;
7039 break;
7041 if (REFERENCE_REF_P (t)
7042 && TREE_CODE (TREE_OPERAND (t, 0)) == COMPONENT_REF)
7044 t = TREE_OPERAND (t, 0);
7045 OMP_CLAUSE_DECL (c) = t;
7047 if (TREE_CODE (t) == COMPONENT_REF
7048 && (ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP
7049 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE__CACHE_)
7051 if (type_dependent_expression_p (t))
7052 break;
7053 if (TREE_CODE (TREE_OPERAND (t, 1)) == FIELD_DECL
7054 && DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
7056 error_at (OMP_CLAUSE_LOCATION (c),
7057 "bit-field %qE in %qs clause",
7058 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7059 remove = true;
7061 else if (!cp_omp_mappable_type (TREE_TYPE (t)))
7063 error_at (OMP_CLAUSE_LOCATION (c),
7064 "%qE does not have a mappable type in %qs clause",
7065 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7066 remove = true;
7068 while (TREE_CODE (t) == COMPONENT_REF)
7070 if (TREE_TYPE (TREE_OPERAND (t, 0))
7071 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0)))
7072 == UNION_TYPE))
7074 error_at (OMP_CLAUSE_LOCATION (c),
7075 "%qE is a member of a union", t);
7076 remove = true;
7077 break;
7079 t = TREE_OPERAND (t, 0);
7081 if (remove)
7082 break;
7083 if (REFERENCE_REF_P (t))
7084 t = TREE_OPERAND (t, 0);
7085 if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
7087 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
7088 goto handle_map_references;
7091 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
7093 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
7094 break;
7095 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
7096 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
7097 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ALWAYS_POINTER))
7098 break;
7099 if (DECL_P (t))
7100 error_at (OMP_CLAUSE_LOCATION (c),
7101 "%qD is not a variable in %qs clause", t,
7102 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7103 else
7104 error_at (OMP_CLAUSE_LOCATION (c),
7105 "%qE is not a variable in %qs clause", t,
7106 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7107 remove = true;
7109 else if (VAR_P (t) && CP_DECL_THREAD_LOCAL_P (t))
7111 error_at (OMP_CLAUSE_LOCATION (c),
7112 "%qD is threadprivate variable in %qs clause", t,
7113 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7114 remove = true;
7116 else if (ort != C_ORT_ACC && t == current_class_ptr)
7118 error_at (OMP_CLAUSE_LOCATION (c),
7119 "%<this%> allowed in OpenMP only in %<declare simd%>"
7120 " clauses");
7121 remove = true;
7122 break;
7124 else if (!processing_template_decl
7125 && !TYPE_REF_P (TREE_TYPE (t))
7126 && (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
7127 || (OMP_CLAUSE_MAP_KIND (c)
7128 != GOMP_MAP_FIRSTPRIVATE_POINTER))
7129 && !cxx_mark_addressable (t))
7130 remove = true;
7131 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
7132 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
7133 || (OMP_CLAUSE_MAP_KIND (c)
7134 == GOMP_MAP_FIRSTPRIVATE_POINTER)))
7135 && t == OMP_CLAUSE_DECL (c)
7136 && !type_dependent_expression_p (t)
7137 && !cp_omp_mappable_type (TYPE_REF_P (TREE_TYPE (t))
7138 ? TREE_TYPE (TREE_TYPE (t))
7139 : TREE_TYPE (t)))
7141 error_at (OMP_CLAUSE_LOCATION (c),
7142 "%qD does not have a mappable type in %qs clause", t,
7143 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7144 remove = true;
7146 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
7147 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FORCE_DEVICEPTR
7148 && !type_dependent_expression_p (t)
7149 && !INDIRECT_TYPE_P (TREE_TYPE (t)))
7151 error_at (OMP_CLAUSE_LOCATION (c),
7152 "%qD is not a pointer variable", t);
7153 remove = true;
7155 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
7156 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
7158 if (bitmap_bit_p (&generic_head, DECL_UID (t))
7159 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
7161 error_at (OMP_CLAUSE_LOCATION (c),
7162 "%qD appears more than once in data clauses", t);
7163 remove = true;
7165 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
7167 if (ort == C_ORT_ACC)
7168 error_at (OMP_CLAUSE_LOCATION (c),
7169 "%qD appears more than once in data clauses", t);
7170 else
7171 error_at (OMP_CLAUSE_LOCATION (c),
7172 "%qD appears both in data and map clauses", t);
7173 remove = true;
7175 else
7176 bitmap_set_bit (&generic_head, DECL_UID (t));
7178 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
7180 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
7181 error_at (OMP_CLAUSE_LOCATION (c),
7182 "%qD appears more than once in motion clauses", t);
7183 if (ort == C_ORT_ACC)
7184 error_at (OMP_CLAUSE_LOCATION (c),
7185 "%qD appears more than once in data clauses", t);
7186 else
7187 error_at (OMP_CLAUSE_LOCATION (c),
7188 "%qD appears more than once in map clauses", t);
7189 remove = true;
7191 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
7192 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
7194 if (ort == C_ORT_ACC)
7195 error_at (OMP_CLAUSE_LOCATION (c),
7196 "%qD appears more than once in data clauses", t);
7197 else
7198 error_at (OMP_CLAUSE_LOCATION (c),
7199 "%qD appears both in data and map clauses", t);
7200 remove = true;
7202 else
7204 bitmap_set_bit (&map_head, DECL_UID (t));
7205 if (t != OMP_CLAUSE_DECL (c)
7206 && TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPONENT_REF)
7207 bitmap_set_bit (&map_field_head, DECL_UID (t));
7209 handle_map_references:
7210 if (!remove
7211 && !processing_template_decl
7212 && ort != C_ORT_DECLARE_SIMD
7213 && TYPE_REF_P (TREE_TYPE (OMP_CLAUSE_DECL (c))))
7215 t = OMP_CLAUSE_DECL (c);
7216 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
7218 OMP_CLAUSE_DECL (c) = build_simple_mem_ref (t);
7219 if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
7220 OMP_CLAUSE_SIZE (c)
7221 = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t)));
7223 else if (OMP_CLAUSE_MAP_KIND (c)
7224 != GOMP_MAP_FIRSTPRIVATE_POINTER
7225 && (OMP_CLAUSE_MAP_KIND (c)
7226 != GOMP_MAP_FIRSTPRIVATE_REFERENCE)
7227 && (OMP_CLAUSE_MAP_KIND (c)
7228 != GOMP_MAP_ALWAYS_POINTER))
7230 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
7231 OMP_CLAUSE_MAP);
7232 if (TREE_CODE (t) == COMPONENT_REF)
7233 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ALWAYS_POINTER);
7234 else
7235 OMP_CLAUSE_SET_MAP_KIND (c2,
7236 GOMP_MAP_FIRSTPRIVATE_REFERENCE);
7237 OMP_CLAUSE_DECL (c2) = t;
7238 OMP_CLAUSE_SIZE (c2) = size_zero_node;
7239 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
7240 OMP_CLAUSE_CHAIN (c) = c2;
7241 OMP_CLAUSE_DECL (c) = build_simple_mem_ref (t);
7242 if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
7243 OMP_CLAUSE_SIZE (c)
7244 = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t)));
7245 c = c2;
7248 break;
7250 case OMP_CLAUSE_TO_DECLARE:
7251 case OMP_CLAUSE_LINK:
7252 t = OMP_CLAUSE_DECL (c);
7253 if (TREE_CODE (t) == FUNCTION_DECL
7254 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
7256 else if (!VAR_P (t))
7258 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
7260 if (TREE_CODE (t) == TEMPLATE_ID_EXPR)
7261 error_at (OMP_CLAUSE_LOCATION (c),
7262 "template %qE in clause %qs", t,
7263 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7264 else if (really_overloaded_fn (t))
7265 error_at (OMP_CLAUSE_LOCATION (c),
7266 "overloaded function name %qE in clause %qs", t,
7267 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7268 else
7269 error_at (OMP_CLAUSE_LOCATION (c),
7270 "%qE is neither a variable nor a function name "
7271 "in clause %qs", t,
7272 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7274 else
7275 error_at (OMP_CLAUSE_LOCATION (c),
7276 "%qE is not a variable in clause %qs", t,
7277 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7278 remove = true;
7280 else if (DECL_THREAD_LOCAL_P (t))
7282 error_at (OMP_CLAUSE_LOCATION (c),
7283 "%qD is threadprivate variable in %qs clause", t,
7284 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7285 remove = true;
7287 else if (!cp_omp_mappable_type (TREE_TYPE (t)))
7289 error_at (OMP_CLAUSE_LOCATION (c),
7290 "%qD does not have a mappable type in %qs clause", t,
7291 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7292 remove = true;
7294 if (remove)
7295 break;
7296 if (bitmap_bit_p (&generic_head, DECL_UID (t)))
7298 error_at (OMP_CLAUSE_LOCATION (c),
7299 "%qE appears more than once on the same "
7300 "%<declare target%> directive", t);
7301 remove = true;
7303 else
7304 bitmap_set_bit (&generic_head, DECL_UID (t));
7305 break;
7307 case OMP_CLAUSE_UNIFORM:
7308 t = OMP_CLAUSE_DECL (c);
7309 if (TREE_CODE (t) != PARM_DECL)
7311 if (processing_template_decl)
7312 break;
7313 if (DECL_P (t))
7314 error_at (OMP_CLAUSE_LOCATION (c),
7315 "%qD is not an argument in %<uniform%> clause", t);
7316 else
7317 error_at (OMP_CLAUSE_LOCATION (c),
7318 "%qE is not an argument in %<uniform%> clause", t);
7319 remove = true;
7320 break;
7322 /* map_head bitmap is used as uniform_head if declare_simd. */
7323 bitmap_set_bit (&map_head, DECL_UID (t));
7324 goto check_dup_generic;
7326 case OMP_CLAUSE_GRAINSIZE:
7327 t = OMP_CLAUSE_GRAINSIZE_EXPR (c);
7328 if (t == error_mark_node)
7329 remove = true;
7330 else if (!type_dependent_expression_p (t)
7331 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
7333 error_at (OMP_CLAUSE_LOCATION (c),
7334 "%<grainsize%> expression must be integral");
7335 remove = true;
7337 else
7339 t = mark_rvalue_use (t);
7340 if (!processing_template_decl)
7342 t = maybe_constant_value (t);
7343 if (TREE_CODE (t) == INTEGER_CST
7344 && tree_int_cst_sgn (t) != 1)
7346 warning_at (OMP_CLAUSE_LOCATION (c), 0,
7347 "%<grainsize%> value must be positive");
7348 t = integer_one_node;
7350 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7352 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
7354 break;
7356 case OMP_CLAUSE_PRIORITY:
7357 t = OMP_CLAUSE_PRIORITY_EXPR (c);
7358 if (t == error_mark_node)
7359 remove = true;
7360 else if (!type_dependent_expression_p (t)
7361 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
7363 error_at (OMP_CLAUSE_LOCATION (c),
7364 "%<priority%> expression must be integral");
7365 remove = true;
7367 else
7369 t = mark_rvalue_use (t);
7370 if (!processing_template_decl)
7372 t = maybe_constant_value (t);
7373 if (TREE_CODE (t) == INTEGER_CST
7374 && tree_int_cst_sgn (t) == -1)
7376 warning_at (OMP_CLAUSE_LOCATION (c), 0,
7377 "%<priority%> value must be non-negative");
7378 t = integer_one_node;
7380 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7382 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
7384 break;
7386 case OMP_CLAUSE_HINT:
7387 t = OMP_CLAUSE_HINT_EXPR (c);
7388 if (t == error_mark_node)
7389 remove = true;
7390 else if (!type_dependent_expression_p (t)
7391 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
7393 error_at (OMP_CLAUSE_LOCATION (c),
7394 "%<hint%> expression must be integral");
7395 remove = true;
7397 else
7399 t = mark_rvalue_use (t);
7400 if (!processing_template_decl)
7402 t = maybe_constant_value (t);
7403 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7404 if (TREE_CODE (t) != INTEGER_CST)
7406 error_at (OMP_CLAUSE_LOCATION (c),
7407 "%<hint%> expression must be constant integer "
7408 "expression");
7409 remove = true;
7412 OMP_CLAUSE_HINT_EXPR (c) = t;
7414 break;
7416 case OMP_CLAUSE_IS_DEVICE_PTR:
7417 case OMP_CLAUSE_USE_DEVICE_PTR:
7418 field_ok = (ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP;
7419 t = OMP_CLAUSE_DECL (c);
7420 if (!type_dependent_expression_p (t))
7422 tree type = TREE_TYPE (t);
7423 if (!TYPE_PTR_P (type)
7424 && TREE_CODE (type) != ARRAY_TYPE
7425 && (!TYPE_REF_P (type)
7426 || (!TYPE_PTR_P (TREE_TYPE (type))
7427 && TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE)))
7429 error_at (OMP_CLAUSE_LOCATION (c),
7430 "%qs variable is neither a pointer, nor an array "
7431 "nor reference to pointer or array",
7432 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7433 remove = true;
7436 goto check_dup_generic;
7438 case OMP_CLAUSE_NOWAIT:
7439 case OMP_CLAUSE_DEFAULT:
7440 case OMP_CLAUSE_UNTIED:
7441 case OMP_CLAUSE_COLLAPSE:
7442 case OMP_CLAUSE_MERGEABLE:
7443 case OMP_CLAUSE_PARALLEL:
7444 case OMP_CLAUSE_FOR:
7445 case OMP_CLAUSE_SECTIONS:
7446 case OMP_CLAUSE_TASKGROUP:
7447 case OMP_CLAUSE_PROC_BIND:
7448 case OMP_CLAUSE_NOGROUP:
7449 case OMP_CLAUSE_THREADS:
7450 case OMP_CLAUSE_SIMD:
7451 case OMP_CLAUSE_DEFAULTMAP:
7452 case OMP_CLAUSE_AUTO:
7453 case OMP_CLAUSE_INDEPENDENT:
7454 case OMP_CLAUSE_SEQ:
7455 case OMP_CLAUSE_IF_PRESENT:
7456 case OMP_CLAUSE_FINALIZE:
7457 break;
7459 case OMP_CLAUSE_TILE:
7460 for (tree list = OMP_CLAUSE_TILE_LIST (c); !remove && list;
7461 list = TREE_CHAIN (list))
7463 t = TREE_VALUE (list);
7465 if (t == error_mark_node)
7466 remove = true;
7467 else if (!type_dependent_expression_p (t)
7468 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
7470 error_at (OMP_CLAUSE_LOCATION (c),
7471 "%<tile%> argument needs integral type");
7472 remove = true;
7474 else
7476 t = mark_rvalue_use (t);
7477 if (!processing_template_decl)
7479 /* Zero is used to indicate '*', we permit you
7480 to get there via an ICE of value zero. */
7481 t = maybe_constant_value (t);
7482 if (!tree_fits_shwi_p (t)
7483 || tree_to_shwi (t) < 0)
7485 error_at (OMP_CLAUSE_LOCATION (c),
7486 "%<tile%> argument needs positive "
7487 "integral constant");
7488 remove = true;
7490 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7494 /* Update list item. */
7495 TREE_VALUE (list) = t;
7497 break;
7499 case OMP_CLAUSE_ORDERED:
7500 ordered_seen = true;
7501 break;
7503 case OMP_CLAUSE_INBRANCH:
7504 case OMP_CLAUSE_NOTINBRANCH:
7505 if (branch_seen)
7507 error_at (OMP_CLAUSE_LOCATION (c),
7508 "%<inbranch%> clause is incompatible with "
7509 "%<notinbranch%>");
7510 remove = true;
7512 branch_seen = true;
7513 break;
7515 default:
7516 gcc_unreachable ();
7519 if (remove)
7520 *pc = OMP_CLAUSE_CHAIN (c);
7521 else
7522 pc = &OMP_CLAUSE_CHAIN (c);
7525 for (pc = &clauses, c = clauses; c ; c = *pc)
7527 enum omp_clause_code c_kind = OMP_CLAUSE_CODE (c);
7528 bool remove = false;
7529 bool need_complete_type = false;
7530 bool need_default_ctor = false;
7531 bool need_copy_ctor = false;
7532 bool need_copy_assignment = false;
7533 bool need_implicitly_determined = false;
7534 bool need_dtor = false;
7535 tree type, inner_type;
7537 switch (c_kind)
7539 case OMP_CLAUSE_SHARED:
7540 need_implicitly_determined = true;
7541 break;
7542 case OMP_CLAUSE_PRIVATE:
7543 need_complete_type = true;
7544 need_default_ctor = true;
7545 need_dtor = true;
7546 need_implicitly_determined = true;
7547 break;
7548 case OMP_CLAUSE_FIRSTPRIVATE:
7549 need_complete_type = true;
7550 need_copy_ctor = true;
7551 need_dtor = true;
7552 need_implicitly_determined = true;
7553 break;
7554 case OMP_CLAUSE_LASTPRIVATE:
7555 need_complete_type = true;
7556 need_copy_assignment = true;
7557 need_implicitly_determined = true;
7558 break;
7559 case OMP_CLAUSE_REDUCTION:
7560 case OMP_CLAUSE_IN_REDUCTION:
7561 case OMP_CLAUSE_TASK_REDUCTION:
7562 need_implicitly_determined = true;
7563 break;
7564 case OMP_CLAUSE_LINEAR:
7565 if (ort != C_ORT_OMP_DECLARE_SIMD)
7566 need_implicitly_determined = true;
7567 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c)
7568 && !bitmap_bit_p (&map_head,
7569 DECL_UID (OMP_CLAUSE_LINEAR_STEP (c))))
7571 error_at (OMP_CLAUSE_LOCATION (c),
7572 "%<linear%> clause step is a parameter %qD not "
7573 "specified in %<uniform%> clause",
7574 OMP_CLAUSE_LINEAR_STEP (c));
7575 *pc = OMP_CLAUSE_CHAIN (c);
7576 continue;
7578 break;
7579 case OMP_CLAUSE_COPYPRIVATE:
7580 need_copy_assignment = true;
7581 break;
7582 case OMP_CLAUSE_COPYIN:
7583 need_copy_assignment = true;
7584 break;
7585 case OMP_CLAUSE_SIMDLEN:
7586 if (safelen
7587 && !processing_template_decl
7588 && tree_int_cst_lt (OMP_CLAUSE_SAFELEN_EXPR (safelen),
7589 OMP_CLAUSE_SIMDLEN_EXPR (c)))
7591 error_at (OMP_CLAUSE_LOCATION (c),
7592 "%<simdlen%> clause value is bigger than "
7593 "%<safelen%> clause value");
7594 OMP_CLAUSE_SIMDLEN_EXPR (c)
7595 = OMP_CLAUSE_SAFELEN_EXPR (safelen);
7597 pc = &OMP_CLAUSE_CHAIN (c);
7598 continue;
7599 case OMP_CLAUSE_SCHEDULE:
7600 if (ordered_seen
7601 && (OMP_CLAUSE_SCHEDULE_KIND (c)
7602 & OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
7604 error_at (OMP_CLAUSE_LOCATION (c),
7605 "%<nonmonotonic%> schedule modifier specified "
7606 "together with %<ordered%> clause");
7607 OMP_CLAUSE_SCHEDULE_KIND (c)
7608 = (enum omp_clause_schedule_kind)
7609 (OMP_CLAUSE_SCHEDULE_KIND (c)
7610 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
7612 pc = &OMP_CLAUSE_CHAIN (c);
7613 continue;
7614 case OMP_CLAUSE_NOGROUP:
7615 if (reduction_seen)
7617 error_at (OMP_CLAUSE_LOCATION (c),
7618 "%<nogroup%> clause must not be used together with "
7619 "%<reduction%> clause");
7620 *pc = OMP_CLAUSE_CHAIN (c);
7621 continue;
7623 pc = &OMP_CLAUSE_CHAIN (c);
7624 continue;
7625 case OMP_CLAUSE_NOWAIT:
7626 if (copyprivate_seen)
7628 error_at (OMP_CLAUSE_LOCATION (c),
7629 "%<nowait%> clause must not be used together "
7630 "with %<copyprivate%>");
7631 *pc = OMP_CLAUSE_CHAIN (c);
7632 continue;
7634 /* FALLTHRU */
7635 default:
7636 pc = &OMP_CLAUSE_CHAIN (c);
7637 continue;
7640 t = OMP_CLAUSE_DECL (c);
7641 if (processing_template_decl
7642 && !VAR_P (t) && TREE_CODE (t) != PARM_DECL)
7644 pc = &OMP_CLAUSE_CHAIN (c);
7645 continue;
7648 switch (c_kind)
7650 case OMP_CLAUSE_LASTPRIVATE:
7651 if (!bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
7653 need_default_ctor = true;
7654 need_dtor = true;
7656 break;
7658 case OMP_CLAUSE_REDUCTION:
7659 case OMP_CLAUSE_IN_REDUCTION:
7660 case OMP_CLAUSE_TASK_REDUCTION:
7661 if (finish_omp_reduction_clause (c, &need_default_ctor,
7662 &need_dtor))
7663 remove = true;
7664 else
7665 t = OMP_CLAUSE_DECL (c);
7666 break;
7668 case OMP_CLAUSE_COPYIN:
7669 if (!VAR_P (t) || !CP_DECL_THREAD_LOCAL_P (t))
7671 error_at (OMP_CLAUSE_LOCATION (c),
7672 "%qE must be %<threadprivate%> for %<copyin%>", t);
7673 remove = true;
7675 break;
7677 default:
7678 break;
7681 if (need_complete_type || need_copy_assignment)
7683 t = require_complete_type (t);
7684 if (t == error_mark_node)
7685 remove = true;
7686 else if (TYPE_REF_P (TREE_TYPE (t))
7687 && !complete_type_or_else (TREE_TYPE (TREE_TYPE (t)), t))
7688 remove = true;
7690 if (need_implicitly_determined)
7692 const char *share_name = NULL;
7694 if (VAR_P (t) && CP_DECL_THREAD_LOCAL_P (t))
7695 share_name = "threadprivate";
7696 else switch (cxx_omp_predetermined_sharing_1 (t))
7698 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
7699 break;
7700 case OMP_CLAUSE_DEFAULT_SHARED:
7701 if (VAR_P (t)
7702 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
7703 && TREE_STATIC (t)
7704 && cxx_omp_const_qual_no_mutable (t))
7706 tree ctx = CP_DECL_CONTEXT (t);
7707 /* const qualified static data members without mutable
7708 member may be specified in firstprivate clause. */
7709 if (TYPE_P (ctx) && MAYBE_CLASS_TYPE_P (ctx))
7710 break;
7712 share_name = "shared";
7713 break;
7714 case OMP_CLAUSE_DEFAULT_PRIVATE:
7715 share_name = "private";
7716 break;
7717 default:
7718 gcc_unreachable ();
7720 if (share_name)
7722 error_at (OMP_CLAUSE_LOCATION (c),
7723 "%qE is predetermined %qs for %qs",
7724 omp_clause_printable_decl (t), share_name,
7725 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7726 remove = true;
7728 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_SHARED
7729 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_FIRSTPRIVATE
7730 && cxx_omp_const_qual_no_mutable (t))
7732 error_at (OMP_CLAUSE_LOCATION (c),
7733 "%<const%> qualified %qE without %<mutable%> member "
7734 "may appear only in %<shared%> or %<firstprivate%> "
7735 "clauses", omp_clause_printable_decl (t));
7736 remove = true;
7740 /* We're interested in the base element, not arrays. */
7741 inner_type = type = TREE_TYPE (t);
7742 if ((need_complete_type
7743 || need_copy_assignment
7744 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
7745 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
7746 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
7747 && TYPE_REF_P (inner_type))
7748 inner_type = TREE_TYPE (inner_type);
7749 while (TREE_CODE (inner_type) == ARRAY_TYPE)
7750 inner_type = TREE_TYPE (inner_type);
7752 /* Check for special function availability by building a call to one.
7753 Save the results, because later we won't be in the right context
7754 for making these queries. */
7755 if (CLASS_TYPE_P (inner_type)
7756 && COMPLETE_TYPE_P (inner_type)
7757 && (need_default_ctor || need_copy_ctor
7758 || need_copy_assignment || need_dtor)
7759 && !type_dependent_expression_p (t)
7760 && cxx_omp_create_clause_info (c, inner_type, need_default_ctor,
7761 need_copy_ctor, need_copy_assignment,
7762 need_dtor))
7763 remove = true;
7765 if (!remove
7766 && c_kind == OMP_CLAUSE_SHARED
7767 && processing_template_decl)
7769 t = omp_clause_decl_field (OMP_CLAUSE_DECL (c));
7770 if (t)
7771 OMP_CLAUSE_DECL (c) = t;
7774 if (remove)
7775 *pc = OMP_CLAUSE_CHAIN (c);
7776 else
7777 pc = &OMP_CLAUSE_CHAIN (c);
7780 bitmap_obstack_release (NULL);
7781 return clauses;
7784 /* Start processing OpenMP clauses that can include any
7785 privatization clauses for non-static data members. */
7787 tree
7788 push_omp_privatization_clauses (bool ignore_next)
7790 if (omp_private_member_ignore_next)
7792 omp_private_member_ignore_next = ignore_next;
7793 return NULL_TREE;
7795 omp_private_member_ignore_next = ignore_next;
7796 if (omp_private_member_map)
7797 omp_private_member_vec.safe_push (error_mark_node);
7798 return push_stmt_list ();
7801 /* Revert remapping of any non-static data members since
7802 the last push_omp_privatization_clauses () call. */
7804 void
7805 pop_omp_privatization_clauses (tree stmt)
7807 if (stmt == NULL_TREE)
7808 return;
7809 stmt = pop_stmt_list (stmt);
7810 if (omp_private_member_map)
7812 while (!omp_private_member_vec.is_empty ())
7814 tree t = omp_private_member_vec.pop ();
7815 if (t == error_mark_node)
7817 add_stmt (stmt);
7818 return;
7820 bool no_decl_expr = t == integer_zero_node;
7821 if (no_decl_expr)
7822 t = omp_private_member_vec.pop ();
7823 tree *v = omp_private_member_map->get (t);
7824 gcc_assert (v);
7825 if (!no_decl_expr)
7826 add_decl_expr (*v);
7827 omp_private_member_map->remove (t);
7829 delete omp_private_member_map;
7830 omp_private_member_map = NULL;
7832 add_stmt (stmt);
7835 /* Remember OpenMP privatization clauses mapping and clear it.
7836 Used for lambdas. */
7838 void
7839 save_omp_privatization_clauses (vec<tree> &save)
7841 save = vNULL;
7842 if (omp_private_member_ignore_next)
7843 save.safe_push (integer_one_node);
7844 omp_private_member_ignore_next = false;
7845 if (!omp_private_member_map)
7846 return;
7848 while (!omp_private_member_vec.is_empty ())
7850 tree t = omp_private_member_vec.pop ();
7851 if (t == error_mark_node)
7853 save.safe_push (t);
7854 continue;
7856 tree n = t;
7857 if (t == integer_zero_node)
7858 t = omp_private_member_vec.pop ();
7859 tree *v = omp_private_member_map->get (t);
7860 gcc_assert (v);
7861 save.safe_push (*v);
7862 save.safe_push (t);
7863 if (n != t)
7864 save.safe_push (n);
7866 delete omp_private_member_map;
7867 omp_private_member_map = NULL;
7870 /* Restore OpenMP privatization clauses mapping saved by the
7871 above function. */
7873 void
7874 restore_omp_privatization_clauses (vec<tree> &save)
7876 gcc_assert (omp_private_member_vec.is_empty ());
7877 omp_private_member_ignore_next = false;
7878 if (save.is_empty ())
7879 return;
7880 if (save.length () == 1 && save[0] == integer_one_node)
7882 omp_private_member_ignore_next = true;
7883 save.release ();
7884 return;
7887 omp_private_member_map = new hash_map <tree, tree>;
7888 while (!save.is_empty ())
7890 tree t = save.pop ();
7891 tree n = t;
7892 if (t != error_mark_node)
7894 if (t == integer_one_node)
7896 omp_private_member_ignore_next = true;
7897 gcc_assert (save.is_empty ());
7898 break;
7900 if (t == integer_zero_node)
7901 t = save.pop ();
7902 tree &v = omp_private_member_map->get_or_insert (t);
7903 v = save.pop ();
7905 omp_private_member_vec.safe_push (t);
7906 if (n != t)
7907 omp_private_member_vec.safe_push (n);
7909 save.release ();
7912 /* For all variables in the tree_list VARS, mark them as thread local. */
7914 void
7915 finish_omp_threadprivate (tree vars)
7917 tree t;
7919 /* Mark every variable in VARS to be assigned thread local storage. */
7920 for (t = vars; t; t = TREE_CHAIN (t))
7922 tree v = TREE_PURPOSE (t);
7924 if (error_operand_p (v))
7926 else if (!VAR_P (v))
7927 error ("%<threadprivate%> %qD is not file, namespace "
7928 "or block scope variable", v);
7929 /* If V had already been marked threadprivate, it doesn't matter
7930 whether it had been used prior to this point. */
7931 else if (TREE_USED (v)
7932 && (DECL_LANG_SPECIFIC (v) == NULL
7933 || !CP_DECL_THREADPRIVATE_P (v)))
7934 error ("%qE declared %<threadprivate%> after first use", v);
7935 else if (! TREE_STATIC (v) && ! DECL_EXTERNAL (v))
7936 error ("automatic variable %qE cannot be %<threadprivate%>", v);
7937 else if (! COMPLETE_TYPE_P (complete_type (TREE_TYPE (v))))
7938 error ("%<threadprivate%> %qE has incomplete type", v);
7939 else if (TREE_STATIC (v) && TYPE_P (CP_DECL_CONTEXT (v))
7940 && CP_DECL_CONTEXT (v) != current_class_type)
7941 error ("%<threadprivate%> %qE directive not "
7942 "in %qT definition", v, CP_DECL_CONTEXT (v));
7943 else
7945 /* Allocate a LANG_SPECIFIC structure for V, if needed. */
7946 if (DECL_LANG_SPECIFIC (v) == NULL)
7947 retrofit_lang_decl (v);
7949 if (! CP_DECL_THREAD_LOCAL_P (v))
7951 CP_DECL_THREAD_LOCAL_P (v) = true;
7952 set_decl_tls_model (v, decl_default_tls_model (v));
7953 /* If rtl has been already set for this var, call
7954 make_decl_rtl once again, so that encode_section_info
7955 has a chance to look at the new decl flags. */
7956 if (DECL_RTL_SET_P (v))
7957 make_decl_rtl (v);
7959 CP_DECL_THREADPRIVATE_P (v) = 1;
7964 /* Build an OpenMP structured block. */
7966 tree
7967 begin_omp_structured_block (void)
7969 return do_pushlevel (sk_omp);
7972 tree
7973 finish_omp_structured_block (tree block)
7975 return do_poplevel (block);
7978 /* Similarly, except force the retention of the BLOCK. */
7980 tree
7981 begin_omp_parallel (void)
7983 keep_next_level (true);
7984 return begin_omp_structured_block ();
7987 /* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
7988 statement. */
7990 tree
7991 finish_oacc_data (tree clauses, tree block)
7993 tree stmt;
7995 block = finish_omp_structured_block (block);
7997 stmt = make_node (OACC_DATA);
7998 TREE_TYPE (stmt) = void_type_node;
7999 OACC_DATA_CLAUSES (stmt) = clauses;
8000 OACC_DATA_BODY (stmt) = block;
8002 return add_stmt (stmt);
8005 /* Generate OACC_HOST_DATA, with CLAUSES and BLOCK as its compound
8006 statement. */
8008 tree
8009 finish_oacc_host_data (tree clauses, tree block)
8011 tree stmt;
8013 block = finish_omp_structured_block (block);
8015 stmt = make_node (OACC_HOST_DATA);
8016 TREE_TYPE (stmt) = void_type_node;
8017 OACC_HOST_DATA_CLAUSES (stmt) = clauses;
8018 OACC_HOST_DATA_BODY (stmt) = block;
8020 return add_stmt (stmt);
8023 /* Generate OMP construct CODE, with BODY and CLAUSES as its compound
8024 statement. */
8026 tree
8027 finish_omp_construct (enum tree_code code, tree body, tree clauses)
8029 body = finish_omp_structured_block (body);
8031 tree stmt = make_node (code);
8032 TREE_TYPE (stmt) = void_type_node;
8033 OMP_BODY (stmt) = body;
8034 OMP_CLAUSES (stmt) = clauses;
8036 return add_stmt (stmt);
8039 tree
8040 finish_omp_parallel (tree clauses, tree body)
8042 tree stmt;
8044 body = finish_omp_structured_block (body);
8046 stmt = make_node (OMP_PARALLEL);
8047 TREE_TYPE (stmt) = void_type_node;
8048 OMP_PARALLEL_CLAUSES (stmt) = clauses;
8049 OMP_PARALLEL_BODY (stmt) = body;
8051 return add_stmt (stmt);
8054 tree
8055 begin_omp_task (void)
8057 keep_next_level (true);
8058 return begin_omp_structured_block ();
8061 tree
8062 finish_omp_task (tree clauses, tree body)
8064 tree stmt;
8066 body = finish_omp_structured_block (body);
8068 stmt = make_node (OMP_TASK);
8069 TREE_TYPE (stmt) = void_type_node;
8070 OMP_TASK_CLAUSES (stmt) = clauses;
8071 OMP_TASK_BODY (stmt) = body;
8073 return add_stmt (stmt);
8076 /* Helper function for finish_omp_for. Convert Ith random access iterator
8077 into integral iterator. Return FALSE if successful. */
8079 static bool
8080 handle_omp_for_class_iterator (int i, location_t locus, enum tree_code code,
8081 tree declv, tree orig_declv, tree initv,
8082 tree condv, tree incrv, tree *body,
8083 tree *pre_body, tree &clauses,
8084 int collapse, int ordered)
8086 tree diff, iter_init, iter_incr = NULL, last;
8087 tree incr_var = NULL, orig_pre_body, orig_body, c;
8088 tree decl = TREE_VEC_ELT (declv, i);
8089 tree init = TREE_VEC_ELT (initv, i);
8090 tree cond = TREE_VEC_ELT (condv, i);
8091 tree incr = TREE_VEC_ELT (incrv, i);
8092 tree iter = decl;
8093 location_t elocus = locus;
8095 if (init && EXPR_HAS_LOCATION (init))
8096 elocus = EXPR_LOCATION (init);
8098 cond = cp_fully_fold (cond);
8099 switch (TREE_CODE (cond))
8101 case GT_EXPR:
8102 case GE_EXPR:
8103 case LT_EXPR:
8104 case LE_EXPR:
8105 case NE_EXPR:
8106 if (TREE_OPERAND (cond, 1) == iter)
8107 cond = build2 (swap_tree_comparison (TREE_CODE (cond)),
8108 TREE_TYPE (cond), iter, TREE_OPERAND (cond, 0));
8109 if (TREE_OPERAND (cond, 0) != iter)
8110 cond = error_mark_node;
8111 else
8113 tree tem = build_x_binary_op (EXPR_LOCATION (cond),
8114 TREE_CODE (cond),
8115 iter, ERROR_MARK,
8116 TREE_OPERAND (cond, 1), ERROR_MARK,
8117 NULL, tf_warning_or_error);
8118 if (error_operand_p (tem))
8119 return true;
8121 break;
8122 default:
8123 cond = error_mark_node;
8124 break;
8126 if (cond == error_mark_node)
8128 error_at (elocus, "invalid controlling predicate");
8129 return true;
8131 diff = build_x_binary_op (elocus, MINUS_EXPR, TREE_OPERAND (cond, 1),
8132 ERROR_MARK, iter, ERROR_MARK, NULL,
8133 tf_warning_or_error);
8134 diff = cp_fully_fold (diff);
8135 if (error_operand_p (diff))
8136 return true;
8137 if (TREE_CODE (TREE_TYPE (diff)) != INTEGER_TYPE)
8139 error_at (elocus, "difference between %qE and %qD does not have integer type",
8140 TREE_OPERAND (cond, 1), iter);
8141 return true;
8143 if (!c_omp_check_loop_iv_exprs (locus, orig_declv,
8144 TREE_VEC_ELT (declv, i), NULL_TREE,
8145 cond, cp_walk_subtrees))
8146 return true;
8148 switch (TREE_CODE (incr))
8150 case PREINCREMENT_EXPR:
8151 case PREDECREMENT_EXPR:
8152 case POSTINCREMENT_EXPR:
8153 case POSTDECREMENT_EXPR:
8154 if (TREE_OPERAND (incr, 0) != iter)
8156 incr = error_mark_node;
8157 break;
8159 iter_incr = build_x_unary_op (EXPR_LOCATION (incr),
8160 TREE_CODE (incr), iter,
8161 tf_warning_or_error);
8162 if (error_operand_p (iter_incr))
8163 return true;
8164 else if (TREE_CODE (incr) == PREINCREMENT_EXPR
8165 || TREE_CODE (incr) == POSTINCREMENT_EXPR)
8166 incr = integer_one_node;
8167 else
8168 incr = integer_minus_one_node;
8169 break;
8170 case MODIFY_EXPR:
8171 if (TREE_OPERAND (incr, 0) != iter)
8172 incr = error_mark_node;
8173 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
8174 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
8176 tree rhs = TREE_OPERAND (incr, 1);
8177 if (TREE_OPERAND (rhs, 0) == iter)
8179 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (rhs, 1)))
8180 != INTEGER_TYPE)
8181 incr = error_mark_node;
8182 else
8184 iter_incr = build_x_modify_expr (EXPR_LOCATION (rhs),
8185 iter, TREE_CODE (rhs),
8186 TREE_OPERAND (rhs, 1),
8187 tf_warning_or_error);
8188 if (error_operand_p (iter_incr))
8189 return true;
8190 incr = TREE_OPERAND (rhs, 1);
8191 incr = cp_convert (TREE_TYPE (diff), incr,
8192 tf_warning_or_error);
8193 if (TREE_CODE (rhs) == MINUS_EXPR)
8195 incr = build1 (NEGATE_EXPR, TREE_TYPE (diff), incr);
8196 incr = fold_simple (incr);
8198 if (TREE_CODE (incr) != INTEGER_CST
8199 && (TREE_CODE (incr) != NOP_EXPR
8200 || (TREE_CODE (TREE_OPERAND (incr, 0))
8201 != INTEGER_CST)))
8202 iter_incr = NULL;
8205 else if (TREE_OPERAND (rhs, 1) == iter)
8207 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (rhs, 0))) != INTEGER_TYPE
8208 || TREE_CODE (rhs) != PLUS_EXPR)
8209 incr = error_mark_node;
8210 else
8212 iter_incr = build_x_binary_op (EXPR_LOCATION (rhs),
8213 PLUS_EXPR,
8214 TREE_OPERAND (rhs, 0),
8215 ERROR_MARK, iter,
8216 ERROR_MARK, NULL,
8217 tf_warning_or_error);
8218 if (error_operand_p (iter_incr))
8219 return true;
8220 iter_incr = build_x_modify_expr (EXPR_LOCATION (rhs),
8221 iter, NOP_EXPR,
8222 iter_incr,
8223 tf_warning_or_error);
8224 if (error_operand_p (iter_incr))
8225 return true;
8226 incr = TREE_OPERAND (rhs, 0);
8227 iter_incr = NULL;
8230 else
8231 incr = error_mark_node;
8233 else
8234 incr = error_mark_node;
8235 break;
8236 default:
8237 incr = error_mark_node;
8238 break;
8241 if (incr == error_mark_node)
8243 error_at (elocus, "invalid increment expression");
8244 return true;
8247 incr = cp_convert (TREE_TYPE (diff), incr, tf_warning_or_error);
8248 incr = cp_fully_fold (incr);
8249 bool taskloop_iv_seen = false;
8250 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
8251 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
8252 && OMP_CLAUSE_DECL (c) == iter)
8254 if (code == OMP_TASKLOOP)
8256 taskloop_iv_seen = true;
8257 OMP_CLAUSE_LASTPRIVATE_TASKLOOP_IV (c) = 1;
8259 break;
8261 else if (code == OMP_TASKLOOP
8262 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
8263 && OMP_CLAUSE_DECL (c) == iter)
8265 taskloop_iv_seen = true;
8266 OMP_CLAUSE_PRIVATE_TASKLOOP_IV (c) = 1;
8269 decl = create_temporary_var (TREE_TYPE (diff));
8270 pushdecl (decl);
8271 add_decl_expr (decl);
8272 last = create_temporary_var (TREE_TYPE (diff));
8273 pushdecl (last);
8274 add_decl_expr (last);
8275 if (c && iter_incr == NULL && TREE_CODE (incr) != INTEGER_CST
8276 && (!ordered || (i < collapse && collapse > 1)))
8278 incr_var = create_temporary_var (TREE_TYPE (diff));
8279 pushdecl (incr_var);
8280 add_decl_expr (incr_var);
8282 gcc_assert (stmts_are_full_exprs_p ());
8283 tree diffvar = NULL_TREE;
8284 if (code == OMP_TASKLOOP)
8286 if (!taskloop_iv_seen)
8288 tree ivc = build_omp_clause (locus, OMP_CLAUSE_FIRSTPRIVATE);
8289 OMP_CLAUSE_DECL (ivc) = iter;
8290 cxx_omp_finish_clause (ivc, NULL);
8291 OMP_CLAUSE_CHAIN (ivc) = clauses;
8292 clauses = ivc;
8294 tree lvc = build_omp_clause (locus, OMP_CLAUSE_FIRSTPRIVATE);
8295 OMP_CLAUSE_DECL (lvc) = last;
8296 OMP_CLAUSE_CHAIN (lvc) = clauses;
8297 clauses = lvc;
8298 diffvar = create_temporary_var (TREE_TYPE (diff));
8299 pushdecl (diffvar);
8300 add_decl_expr (diffvar);
8303 orig_pre_body = *pre_body;
8304 *pre_body = push_stmt_list ();
8305 if (orig_pre_body)
8306 add_stmt (orig_pre_body);
8307 if (init != NULL)
8308 finish_expr_stmt (build_x_modify_expr (elocus,
8309 iter, NOP_EXPR, init,
8310 tf_warning_or_error));
8311 init = build_int_cst (TREE_TYPE (diff), 0);
8312 if (c && iter_incr == NULL
8313 && (!ordered || (i < collapse && collapse > 1)))
8315 if (incr_var)
8317 finish_expr_stmt (build_x_modify_expr (elocus,
8318 incr_var, NOP_EXPR,
8319 incr, tf_warning_or_error));
8320 incr = incr_var;
8322 iter_incr = build_x_modify_expr (elocus,
8323 iter, PLUS_EXPR, incr,
8324 tf_warning_or_error);
8326 if (c && ordered && i < collapse && collapse > 1)
8327 iter_incr = incr;
8328 finish_expr_stmt (build_x_modify_expr (elocus,
8329 last, NOP_EXPR, init,
8330 tf_warning_or_error));
8331 if (diffvar)
8333 finish_expr_stmt (build_x_modify_expr (elocus,
8334 diffvar, NOP_EXPR,
8335 diff, tf_warning_or_error));
8336 diff = diffvar;
8338 *pre_body = pop_stmt_list (*pre_body);
8340 cond = cp_build_binary_op (elocus,
8341 TREE_CODE (cond), decl, diff,
8342 tf_warning_or_error);
8343 incr = build_modify_expr (elocus, decl, NULL_TREE, PLUS_EXPR,
8344 elocus, incr, NULL_TREE);
8346 orig_body = *body;
8347 *body = push_stmt_list ();
8348 iter_init = build2 (MINUS_EXPR, TREE_TYPE (diff), decl, last);
8349 iter_init = build_x_modify_expr (elocus,
8350 iter, PLUS_EXPR, iter_init,
8351 tf_warning_or_error);
8352 if (iter_init != error_mark_node)
8353 iter_init = build1 (NOP_EXPR, void_type_node, iter_init);
8354 finish_expr_stmt (iter_init);
8355 finish_expr_stmt (build_x_modify_expr (elocus,
8356 last, NOP_EXPR, decl,
8357 tf_warning_or_error));
8358 add_stmt (orig_body);
8359 *body = pop_stmt_list (*body);
8361 if (c)
8363 OMP_CLAUSE_LASTPRIVATE_STMT (c) = push_stmt_list ();
8364 if (!ordered)
8365 finish_expr_stmt (iter_incr);
8366 else
8368 iter_init = decl;
8369 if (i < collapse && collapse > 1 && !error_operand_p (iter_incr))
8370 iter_init = build2 (PLUS_EXPR, TREE_TYPE (diff),
8371 iter_init, iter_incr);
8372 iter_init = build2 (MINUS_EXPR, TREE_TYPE (diff), iter_init, last);
8373 iter_init = build_x_modify_expr (elocus,
8374 iter, PLUS_EXPR, iter_init,
8375 tf_warning_or_error);
8376 if (iter_init != error_mark_node)
8377 iter_init = build1 (NOP_EXPR, void_type_node, iter_init);
8378 finish_expr_stmt (iter_init);
8380 OMP_CLAUSE_LASTPRIVATE_STMT (c)
8381 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (c));
8384 if (TREE_CODE (TREE_VEC_ELT (orig_declv, i)) == TREE_LIST)
8386 tree t = TREE_VEC_ELT (orig_declv, i);
8387 gcc_assert (TREE_PURPOSE (t) == NULL_TREE
8388 && TREE_VALUE (t) == NULL_TREE
8389 && TREE_CODE (TREE_CHAIN (t)) == TREE_VEC);
8390 TREE_PURPOSE (t) = TREE_VEC_ELT (declv, i);
8391 TREE_VALUE (t) = last;
8393 else
8394 TREE_VEC_ELT (orig_declv, i)
8395 = tree_cons (TREE_VEC_ELT (declv, i), last, NULL_TREE);
8396 TREE_VEC_ELT (declv, i) = decl;
8397 TREE_VEC_ELT (initv, i) = init;
8398 TREE_VEC_ELT (condv, i) = cond;
8399 TREE_VEC_ELT (incrv, i) = incr;
8401 return false;
8404 /* Build and validate an OMP_FOR statement. CLAUSES, BODY, COND, INCR
8405 are directly for their associated operands in the statement. DECL
8406 and INIT are a combo; if DECL is NULL then INIT ought to be a
8407 MODIFY_EXPR, and the DECL should be extracted. PRE_BODY are
8408 optional statements that need to go before the loop into its
8409 sk_omp scope. */
8411 tree
8412 finish_omp_for (location_t locus, enum tree_code code, tree declv,
8413 tree orig_declv, tree initv, tree condv, tree incrv,
8414 tree body, tree pre_body, vec<tree> *orig_inits, tree clauses)
8416 tree omp_for = NULL, orig_incr = NULL;
8417 tree decl = NULL, init, cond, incr;
8418 location_t elocus;
8419 int i;
8420 int collapse = 1;
8421 int ordered = 0;
8423 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (initv));
8424 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (condv));
8425 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (incrv));
8426 if (TREE_VEC_LENGTH (declv) > 1)
8428 tree c;
8430 c = omp_find_clause (clauses, OMP_CLAUSE_TILE);
8431 if (c)
8432 collapse = list_length (OMP_CLAUSE_TILE_LIST (c));
8433 else
8435 c = omp_find_clause (clauses, OMP_CLAUSE_COLLAPSE);
8436 if (c)
8437 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (c));
8438 if (collapse != TREE_VEC_LENGTH (declv))
8439 ordered = TREE_VEC_LENGTH (declv);
8442 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
8444 decl = TREE_VEC_ELT (declv, i);
8445 init = TREE_VEC_ELT (initv, i);
8446 cond = TREE_VEC_ELT (condv, i);
8447 incr = TREE_VEC_ELT (incrv, i);
8448 elocus = locus;
8450 if (decl == NULL)
8452 if (init != NULL)
8453 switch (TREE_CODE (init))
8455 case MODIFY_EXPR:
8456 decl = TREE_OPERAND (init, 0);
8457 init = TREE_OPERAND (init, 1);
8458 break;
8459 case MODOP_EXPR:
8460 if (TREE_CODE (TREE_OPERAND (init, 1)) == NOP_EXPR)
8462 decl = TREE_OPERAND (init, 0);
8463 init = TREE_OPERAND (init, 2);
8465 break;
8466 default:
8467 break;
8470 if (decl == NULL)
8472 error_at (locus,
8473 "expected iteration declaration or initialization");
8474 return NULL;
8478 if (init && EXPR_HAS_LOCATION (init))
8479 elocus = EXPR_LOCATION (init);
8481 if (cond == global_namespace)
8482 continue;
8484 if (cond == NULL)
8486 error_at (elocus, "missing controlling predicate");
8487 return NULL;
8490 if (incr == NULL)
8492 error_at (elocus, "missing increment expression");
8493 return NULL;
8496 TREE_VEC_ELT (declv, i) = decl;
8497 TREE_VEC_ELT (initv, i) = init;
8500 if (orig_inits)
8502 bool fail = false;
8503 tree orig_init;
8504 FOR_EACH_VEC_ELT (*orig_inits, i, orig_init)
8505 if (orig_init
8506 && !c_omp_check_loop_iv_exprs (locus, orig_declv
8507 ? orig_declv : declv,
8508 TREE_VEC_ELT (declv, i), orig_init,
8509 NULL_TREE, cp_walk_subtrees))
8510 fail = true;
8511 if (fail)
8512 return NULL;
8515 if (dependent_omp_for_p (declv, initv, condv, incrv))
8517 tree stmt;
8519 stmt = make_node (code);
8521 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
8523 /* This is really just a place-holder. We'll be decomposing this
8524 again and going through the cp_build_modify_expr path below when
8525 we instantiate the thing. */
8526 TREE_VEC_ELT (initv, i)
8527 = build2 (MODIFY_EXPR, void_type_node, TREE_VEC_ELT (declv, i),
8528 TREE_VEC_ELT (initv, i));
8531 TREE_TYPE (stmt) = void_type_node;
8532 OMP_FOR_INIT (stmt) = initv;
8533 OMP_FOR_COND (stmt) = condv;
8534 OMP_FOR_INCR (stmt) = incrv;
8535 OMP_FOR_BODY (stmt) = body;
8536 OMP_FOR_PRE_BODY (stmt) = pre_body;
8537 OMP_FOR_CLAUSES (stmt) = clauses;
8539 SET_EXPR_LOCATION (stmt, locus);
8540 return add_stmt (stmt);
8543 if (!orig_declv)
8544 orig_declv = copy_node (declv);
8546 if (processing_template_decl)
8547 orig_incr = make_tree_vec (TREE_VEC_LENGTH (incrv));
8549 for (i = 0; i < TREE_VEC_LENGTH (declv); )
8551 decl = TREE_VEC_ELT (declv, i);
8552 init = TREE_VEC_ELT (initv, i);
8553 cond = TREE_VEC_ELT (condv, i);
8554 incr = TREE_VEC_ELT (incrv, i);
8555 if (orig_incr)
8556 TREE_VEC_ELT (orig_incr, i) = incr;
8557 elocus = locus;
8559 if (init && EXPR_HAS_LOCATION (init))
8560 elocus = EXPR_LOCATION (init);
8562 if (!DECL_P (decl))
8564 error_at (elocus, "expected iteration declaration or initialization");
8565 return NULL;
8568 if (incr && TREE_CODE (incr) == MODOP_EXPR)
8570 if (orig_incr)
8571 TREE_VEC_ELT (orig_incr, i) = incr;
8572 incr = cp_build_modify_expr (elocus, TREE_OPERAND (incr, 0),
8573 TREE_CODE (TREE_OPERAND (incr, 1)),
8574 TREE_OPERAND (incr, 2),
8575 tf_warning_or_error);
8578 if (CLASS_TYPE_P (TREE_TYPE (decl)))
8580 if (code == OMP_SIMD)
8582 error_at (elocus, "%<#pragma omp simd%> used with class "
8583 "iteration variable %qE", decl);
8584 return NULL;
8586 if (handle_omp_for_class_iterator (i, locus, code, declv, orig_declv,
8587 initv, condv, incrv, &body,
8588 &pre_body, clauses,
8589 collapse, ordered))
8590 return NULL;
8591 continue;
8594 if (!INTEGRAL_TYPE_P (TREE_TYPE (decl))
8595 && !TYPE_PTR_P (TREE_TYPE (decl)))
8597 error_at (elocus, "invalid type for iteration variable %qE", decl);
8598 return NULL;
8601 if (!processing_template_decl)
8603 init = fold_build_cleanup_point_expr (TREE_TYPE (init), init);
8604 init = cp_build_modify_expr (elocus, decl, NOP_EXPR, init,
8605 tf_warning_or_error);
8607 else
8608 init = build2 (MODIFY_EXPR, void_type_node, decl, init);
8609 if (cond
8610 && TREE_SIDE_EFFECTS (cond)
8611 && COMPARISON_CLASS_P (cond)
8612 && !processing_template_decl)
8614 tree t = TREE_OPERAND (cond, 0);
8615 if (TREE_SIDE_EFFECTS (t)
8616 && t != decl
8617 && (TREE_CODE (t) != NOP_EXPR
8618 || TREE_OPERAND (t, 0) != decl))
8619 TREE_OPERAND (cond, 0)
8620 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
8622 t = TREE_OPERAND (cond, 1);
8623 if (TREE_SIDE_EFFECTS (t)
8624 && t != decl
8625 && (TREE_CODE (t) != NOP_EXPR
8626 || TREE_OPERAND (t, 0) != decl))
8627 TREE_OPERAND (cond, 1)
8628 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
8630 if (decl == error_mark_node || init == error_mark_node)
8631 return NULL;
8633 TREE_VEC_ELT (declv, i) = decl;
8634 TREE_VEC_ELT (initv, i) = init;
8635 TREE_VEC_ELT (condv, i) = cond;
8636 TREE_VEC_ELT (incrv, i) = incr;
8637 i++;
8640 if (pre_body && IS_EMPTY_STMT (pre_body))
8641 pre_body = NULL;
8643 omp_for = c_finish_omp_for (locus, code, declv, orig_declv, initv, condv,
8644 incrv, body, pre_body,
8645 !processing_template_decl);
8647 /* Check for iterators appearing in lb, b or incr expressions. */
8648 if (omp_for && !c_omp_check_loop_iv (omp_for, orig_declv, cp_walk_subtrees))
8649 omp_for = NULL_TREE;
8651 if (omp_for == NULL)
8653 return NULL;
8656 add_stmt (omp_for);
8658 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INCR (omp_for)); i++)
8660 decl = TREE_OPERAND (TREE_VEC_ELT (OMP_FOR_INIT (omp_for), i), 0);
8661 incr = TREE_VEC_ELT (OMP_FOR_INCR (omp_for), i);
8663 if (TREE_CODE (incr) != MODIFY_EXPR)
8664 continue;
8666 if (TREE_SIDE_EFFECTS (TREE_OPERAND (incr, 1))
8667 && BINARY_CLASS_P (TREE_OPERAND (incr, 1))
8668 && !processing_template_decl)
8670 tree t = TREE_OPERAND (TREE_OPERAND (incr, 1), 0);
8671 if (TREE_SIDE_EFFECTS (t)
8672 && t != decl
8673 && (TREE_CODE (t) != NOP_EXPR
8674 || TREE_OPERAND (t, 0) != decl))
8675 TREE_OPERAND (TREE_OPERAND (incr, 1), 0)
8676 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
8678 t = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
8679 if (TREE_SIDE_EFFECTS (t)
8680 && t != decl
8681 && (TREE_CODE (t) != NOP_EXPR
8682 || TREE_OPERAND (t, 0) != decl))
8683 TREE_OPERAND (TREE_OPERAND (incr, 1), 1)
8684 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
8687 if (orig_incr)
8688 TREE_VEC_ELT (OMP_FOR_INCR (omp_for), i) = TREE_VEC_ELT (orig_incr, i);
8690 OMP_FOR_CLAUSES (omp_for) = clauses;
8692 /* For simd loops with non-static data member iterators, we could have added
8693 OMP_CLAUSE_LINEAR clauses without OMP_CLAUSE_LINEAR_STEP. As we know the
8694 step at this point, fill it in. */
8695 if (code == OMP_SIMD && !processing_template_decl
8696 && TREE_VEC_LENGTH (OMP_FOR_INCR (omp_for)) == 1)
8697 for (tree c = omp_find_clause (clauses, OMP_CLAUSE_LINEAR); c;
8698 c = omp_find_clause (OMP_CLAUSE_CHAIN (c), OMP_CLAUSE_LINEAR))
8699 if (OMP_CLAUSE_LINEAR_STEP (c) == NULL_TREE)
8701 decl = TREE_OPERAND (TREE_VEC_ELT (OMP_FOR_INIT (omp_for), 0), 0);
8702 gcc_assert (decl == OMP_CLAUSE_DECL (c));
8703 incr = TREE_VEC_ELT (OMP_FOR_INCR (omp_for), 0);
8704 tree step, stept;
8705 switch (TREE_CODE (incr))
8707 case PREINCREMENT_EXPR:
8708 case POSTINCREMENT_EXPR:
8709 /* c_omp_for_incr_canonicalize_ptr() should have been
8710 called to massage things appropriately. */
8711 gcc_assert (!INDIRECT_TYPE_P (TREE_TYPE (decl)));
8712 OMP_CLAUSE_LINEAR_STEP (c) = build_int_cst (TREE_TYPE (decl), 1);
8713 break;
8714 case PREDECREMENT_EXPR:
8715 case POSTDECREMENT_EXPR:
8716 /* c_omp_for_incr_canonicalize_ptr() should have been
8717 called to massage things appropriately. */
8718 gcc_assert (!INDIRECT_TYPE_P (TREE_TYPE (decl)));
8719 OMP_CLAUSE_LINEAR_STEP (c)
8720 = build_int_cst (TREE_TYPE (decl), -1);
8721 break;
8722 case MODIFY_EXPR:
8723 gcc_assert (TREE_OPERAND (incr, 0) == decl);
8724 incr = TREE_OPERAND (incr, 1);
8725 switch (TREE_CODE (incr))
8727 case PLUS_EXPR:
8728 if (TREE_OPERAND (incr, 1) == decl)
8729 step = TREE_OPERAND (incr, 0);
8730 else
8731 step = TREE_OPERAND (incr, 1);
8732 break;
8733 case MINUS_EXPR:
8734 case POINTER_PLUS_EXPR:
8735 gcc_assert (TREE_OPERAND (incr, 0) == decl);
8736 step = TREE_OPERAND (incr, 1);
8737 break;
8738 default:
8739 gcc_unreachable ();
8741 stept = TREE_TYPE (decl);
8742 if (INDIRECT_TYPE_P (stept))
8743 stept = sizetype;
8744 step = fold_convert (stept, step);
8745 if (TREE_CODE (incr) == MINUS_EXPR)
8746 step = fold_build1 (NEGATE_EXPR, stept, step);
8747 OMP_CLAUSE_LINEAR_STEP (c) = step;
8748 break;
8749 default:
8750 gcc_unreachable ();
8754 return omp_for;
8757 /* Fix up range for decls. Those decls were pushed into BIND's BIND_EXPR_VARS
8758 and need to be moved into the BIND_EXPR inside of the OMP_FOR's body. */
8760 tree
8761 finish_omp_for_block (tree bind, tree omp_for)
8763 if (omp_for == NULL_TREE
8764 || !OMP_FOR_ORIG_DECLS (omp_for)
8765 || bind == NULL_TREE
8766 || TREE_CODE (bind) != BIND_EXPR)
8767 return bind;
8768 tree b = NULL_TREE;
8769 for (int i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (omp_for)); i++)
8770 if (TREE_CODE (TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (omp_for), i)) == TREE_LIST
8771 && TREE_CHAIN (TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (omp_for), i)))
8773 tree v = TREE_CHAIN (TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (omp_for), i));
8774 gcc_assert (BIND_EXPR_BLOCK (bind)
8775 && (BIND_EXPR_VARS (bind)
8776 == BLOCK_VARS (BIND_EXPR_BLOCK (bind))));
8777 for (int j = 2; j < TREE_VEC_LENGTH (v); j++)
8778 for (tree *p = &BIND_EXPR_VARS (bind); *p; p = &DECL_CHAIN (*p))
8780 if (*p == TREE_VEC_ELT (v, j))
8782 tree var = *p;
8783 *p = DECL_CHAIN (*p);
8784 if (b == NULL_TREE)
8786 b = make_node (BLOCK);
8787 b = build3 (BIND_EXPR, void_type_node, NULL_TREE,
8788 OMP_FOR_BODY (omp_for), b);
8789 TREE_SIDE_EFFECTS (b) = 1;
8790 OMP_FOR_BODY (omp_for) = b;
8792 DECL_CHAIN (var) = BIND_EXPR_VARS (b);
8793 BIND_EXPR_VARS (b) = var;
8794 BLOCK_VARS (BIND_EXPR_BLOCK (b)) = var;
8797 BLOCK_VARS (BIND_EXPR_BLOCK (bind)) = BIND_EXPR_VARS (bind);
8799 return bind;
8802 void
8803 finish_omp_atomic (location_t loc, enum tree_code code, enum tree_code opcode,
8804 tree lhs, tree rhs, tree v, tree lhs1, tree rhs1,
8805 tree clauses, enum omp_memory_order mo)
8807 tree orig_lhs;
8808 tree orig_rhs;
8809 tree orig_v;
8810 tree orig_lhs1;
8811 tree orig_rhs1;
8812 bool dependent_p;
8813 tree stmt;
8815 orig_lhs = lhs;
8816 orig_rhs = rhs;
8817 orig_v = v;
8818 orig_lhs1 = lhs1;
8819 orig_rhs1 = rhs1;
8820 dependent_p = false;
8821 stmt = NULL_TREE;
8823 /* Even in a template, we can detect invalid uses of the atomic
8824 pragma if neither LHS nor RHS is type-dependent. */
8825 if (processing_template_decl)
8827 dependent_p = (type_dependent_expression_p (lhs)
8828 || (rhs && type_dependent_expression_p (rhs))
8829 || (v && type_dependent_expression_p (v))
8830 || (lhs1 && type_dependent_expression_p (lhs1))
8831 || (rhs1 && type_dependent_expression_p (rhs1)));
8832 if (clauses)
8834 gcc_assert (TREE_CODE (clauses) == OMP_CLAUSE
8835 && OMP_CLAUSE_CODE (clauses) == OMP_CLAUSE_HINT
8836 && OMP_CLAUSE_CHAIN (clauses) == NULL_TREE);
8837 if (type_dependent_expression_p (OMP_CLAUSE_HINT_EXPR (clauses))
8838 || TREE_CODE (OMP_CLAUSE_HINT_EXPR (clauses)) != INTEGER_CST)
8839 dependent_p = true;
8841 if (!dependent_p)
8843 lhs = build_non_dependent_expr (lhs);
8844 if (rhs)
8845 rhs = build_non_dependent_expr (rhs);
8846 if (v)
8847 v = build_non_dependent_expr (v);
8848 if (lhs1)
8849 lhs1 = build_non_dependent_expr (lhs1);
8850 if (rhs1)
8851 rhs1 = build_non_dependent_expr (rhs1);
8854 if (!dependent_p)
8856 bool swapped = false;
8857 if (rhs1 && cp_tree_equal (lhs, rhs))
8859 std::swap (rhs, rhs1);
8860 swapped = !commutative_tree_code (opcode);
8862 if (rhs1 && !cp_tree_equal (lhs, rhs1))
8864 if (code == OMP_ATOMIC)
8865 error ("%<#pragma omp atomic update%> uses two different "
8866 "expressions for memory");
8867 else
8868 error ("%<#pragma omp atomic capture%> uses two different "
8869 "expressions for memory");
8870 return;
8872 if (lhs1 && !cp_tree_equal (lhs, lhs1))
8874 if (code == OMP_ATOMIC)
8875 error ("%<#pragma omp atomic update%> uses two different "
8876 "expressions for memory");
8877 else
8878 error ("%<#pragma omp atomic capture%> uses two different "
8879 "expressions for memory");
8880 return;
8882 stmt = c_finish_omp_atomic (loc, code, opcode, lhs, rhs,
8883 v, lhs1, rhs1, swapped, mo,
8884 processing_template_decl != 0);
8885 if (stmt == error_mark_node)
8886 return;
8888 if (processing_template_decl)
8890 if (code == OMP_ATOMIC_READ)
8892 stmt = build_min_nt_loc (loc, OMP_ATOMIC_READ, orig_lhs);
8893 OMP_ATOMIC_MEMORY_ORDER (stmt) = mo;
8894 stmt = build2 (MODIFY_EXPR, void_type_node, orig_v, stmt);
8896 else
8898 if (opcode == NOP_EXPR)
8899 stmt = build2 (MODIFY_EXPR, void_type_node, orig_lhs, orig_rhs);
8900 else
8901 stmt = build2 (opcode, void_type_node, orig_lhs, orig_rhs);
8902 if (orig_rhs1)
8903 stmt = build_min_nt_loc (EXPR_LOCATION (orig_rhs1),
8904 COMPOUND_EXPR, orig_rhs1, stmt);
8905 if (code != OMP_ATOMIC)
8907 stmt = build_min_nt_loc (loc, code, orig_lhs1, stmt);
8908 OMP_ATOMIC_MEMORY_ORDER (stmt) = mo;
8909 stmt = build2 (MODIFY_EXPR, void_type_node, orig_v, stmt);
8912 stmt = build2 (OMP_ATOMIC, void_type_node,
8913 clauses ? clauses : integer_zero_node, stmt);
8914 OMP_ATOMIC_MEMORY_ORDER (stmt) = mo;
8915 SET_EXPR_LOCATION (stmt, loc);
8917 finish_expr_stmt (stmt);
8920 void
8921 finish_omp_barrier (void)
8923 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_BARRIER);
8924 vec<tree, va_gc> *vec = make_tree_vector ();
8925 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
8926 release_tree_vector (vec);
8927 finish_expr_stmt (stmt);
8930 void
8931 finish_omp_depobj (location_t loc, tree depobj,
8932 enum omp_clause_depend_kind kind, tree clause)
8934 if (!error_operand_p (depobj) && !type_dependent_expression_p (depobj))
8936 if (!lvalue_p (depobj))
8938 error_at (EXPR_LOC_OR_LOC (depobj, loc),
8939 "%<depobj%> expression is not lvalue expression");
8940 depobj = error_mark_node;
8944 if (processing_template_decl)
8946 if (clause == NULL_TREE)
8947 clause = build_int_cst (integer_type_node, kind);
8948 add_stmt (build_min_nt_loc (loc, OMP_DEPOBJ, depobj, clause));
8949 return;
8952 if (!error_operand_p (depobj))
8954 tree addr = cp_build_addr_expr (depobj, tf_warning_or_error);
8955 if (addr == error_mark_node)
8956 depobj = error_mark_node;
8957 else
8958 depobj = cp_build_indirect_ref (addr, RO_UNARY_STAR,
8959 tf_warning_or_error);
8962 c_finish_omp_depobj (loc, depobj, kind, clause);
8965 void
8966 finish_omp_flush (int mo)
8968 tree fn = builtin_decl_explicit (BUILT_IN_SYNC_SYNCHRONIZE);
8969 vec<tree, va_gc> *vec = make_tree_vector ();
8970 if (mo != MEMMODEL_LAST)
8972 fn = builtin_decl_explicit (BUILT_IN_ATOMIC_THREAD_FENCE);
8973 vec->quick_push (build_int_cst (integer_type_node, mo));
8975 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
8976 release_tree_vector (vec);
8977 finish_expr_stmt (stmt);
8980 void
8981 finish_omp_taskwait (void)
8983 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_TASKWAIT);
8984 vec<tree, va_gc> *vec = make_tree_vector ();
8985 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
8986 release_tree_vector (vec);
8987 finish_expr_stmt (stmt);
8990 void
8991 finish_omp_taskyield (void)
8993 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_TASKYIELD);
8994 vec<tree, va_gc> *vec = make_tree_vector ();
8995 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
8996 release_tree_vector (vec);
8997 finish_expr_stmt (stmt);
9000 void
9001 finish_omp_cancel (tree clauses)
9003 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
9004 int mask = 0;
9005 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
9006 mask = 1;
9007 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
9008 mask = 2;
9009 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
9010 mask = 4;
9011 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
9012 mask = 8;
9013 else
9015 error ("%<#pragma omp cancel%> must specify one of "
9016 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> clauses");
9017 return;
9019 vec<tree, va_gc> *vec = make_tree_vector ();
9020 tree ifc = omp_find_clause (clauses, OMP_CLAUSE_IF);
9021 if (ifc != NULL_TREE)
9023 if (OMP_CLAUSE_IF_MODIFIER (ifc) != ERROR_MARK
9024 && OMP_CLAUSE_IF_MODIFIER (ifc) != VOID_CST)
9025 error_at (OMP_CLAUSE_LOCATION (ifc),
9026 "expected %<cancel%> %<if%> clause modifier");
9028 tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
9029 ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
9030 boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
9031 build_zero_cst (type));
9033 else
9034 ifc = boolean_true_node;
9035 vec->quick_push (build_int_cst (integer_type_node, mask));
9036 vec->quick_push (ifc);
9037 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
9038 release_tree_vector (vec);
9039 finish_expr_stmt (stmt);
9042 void
9043 finish_omp_cancellation_point (tree clauses)
9045 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
9046 int mask = 0;
9047 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
9048 mask = 1;
9049 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
9050 mask = 2;
9051 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
9052 mask = 4;
9053 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
9054 mask = 8;
9055 else
9057 error ("%<#pragma omp cancellation point%> must specify one of "
9058 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> clauses");
9059 return;
9061 vec<tree, va_gc> *vec
9062 = make_tree_vector_single (build_int_cst (integer_type_node, mask));
9063 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
9064 release_tree_vector (vec);
9065 finish_expr_stmt (stmt);
9068 /* Begin a __transaction_atomic or __transaction_relaxed statement.
9069 If PCOMPOUND is non-null, this is for a function-transaction-block, and we
9070 should create an extra compound stmt. */
9072 tree
9073 begin_transaction_stmt (location_t loc, tree *pcompound, int flags)
9075 tree r;
9077 if (pcompound)
9078 *pcompound = begin_compound_stmt (0);
9080 r = build_stmt (loc, TRANSACTION_EXPR, NULL_TREE);
9082 /* Only add the statement to the function if support enabled. */
9083 if (flag_tm)
9084 add_stmt (r);
9085 else
9086 error_at (loc, ((flags & TM_STMT_ATTR_RELAXED) != 0
9087 ? G_("%<__transaction_relaxed%> without "
9088 "transactional memory support enabled")
9089 : G_("%<__transaction_atomic%> without "
9090 "transactional memory support enabled")));
9092 TRANSACTION_EXPR_BODY (r) = push_stmt_list ();
9093 TREE_SIDE_EFFECTS (r) = 1;
9094 return r;
9097 /* End a __transaction_atomic or __transaction_relaxed statement.
9098 If COMPOUND_STMT is non-null, this is for a function-transaction-block,
9099 and we should end the compound. If NOEX is non-NULL, we wrap the body in
9100 a MUST_NOT_THROW_EXPR with NOEX as condition. */
9102 void
9103 finish_transaction_stmt (tree stmt, tree compound_stmt, int flags, tree noex)
9105 TRANSACTION_EXPR_BODY (stmt) = pop_stmt_list (TRANSACTION_EXPR_BODY (stmt));
9106 TRANSACTION_EXPR_OUTER (stmt) = (flags & TM_STMT_ATTR_OUTER) != 0;
9107 TRANSACTION_EXPR_RELAXED (stmt) = (flags & TM_STMT_ATTR_RELAXED) != 0;
9108 TRANSACTION_EXPR_IS_STMT (stmt) = 1;
9110 /* noexcept specifications are not allowed for function transactions. */
9111 gcc_assert (!(noex && compound_stmt));
9112 if (noex)
9114 tree body = build_must_not_throw_expr (TRANSACTION_EXPR_BODY (stmt),
9115 noex);
9116 protected_set_expr_location
9117 (body, EXPR_LOCATION (TRANSACTION_EXPR_BODY (stmt)));
9118 TREE_SIDE_EFFECTS (body) = 1;
9119 TRANSACTION_EXPR_BODY (stmt) = body;
9122 if (compound_stmt)
9123 finish_compound_stmt (compound_stmt);
9126 /* Build a __transaction_atomic or __transaction_relaxed expression. If
9127 NOEX is non-NULL, we wrap the body in a MUST_NOT_THROW_EXPR with NOEX as
9128 condition. */
9130 tree
9131 build_transaction_expr (location_t loc, tree expr, int flags, tree noex)
9133 tree ret;
9134 if (noex)
9136 expr = build_must_not_throw_expr (expr, noex);
9137 protected_set_expr_location (expr, loc);
9138 TREE_SIDE_EFFECTS (expr) = 1;
9140 ret = build1 (TRANSACTION_EXPR, TREE_TYPE (expr), expr);
9141 if (flags & TM_STMT_ATTR_RELAXED)
9142 TRANSACTION_EXPR_RELAXED (ret) = 1;
9143 TREE_SIDE_EFFECTS (ret) = 1;
9144 SET_EXPR_LOCATION (ret, loc);
9145 return ret;
9148 void
9149 init_cp_semantics (void)
9153 /* Build a STATIC_ASSERT for a static assertion with the condition
9154 CONDITION and the message text MESSAGE. LOCATION is the location
9155 of the static assertion in the source code. When MEMBER_P, this
9156 static assertion is a member of a class. */
9157 void
9158 finish_static_assert (tree condition, tree message, location_t location,
9159 bool member_p)
9161 tsubst_flags_t complain = tf_warning_or_error;
9163 if (message == NULL_TREE
9164 || message == error_mark_node
9165 || condition == NULL_TREE
9166 || condition == error_mark_node)
9167 return;
9169 if (check_for_bare_parameter_packs (condition))
9170 condition = error_mark_node;
9172 if (instantiation_dependent_expression_p (condition))
9174 /* We're in a template; build a STATIC_ASSERT and put it in
9175 the right place. */
9176 tree assertion;
9178 assertion = make_node (STATIC_ASSERT);
9179 STATIC_ASSERT_CONDITION (assertion) = condition;
9180 STATIC_ASSERT_MESSAGE (assertion) = message;
9181 STATIC_ASSERT_SOURCE_LOCATION (assertion) = location;
9183 if (member_p)
9184 maybe_add_class_template_decl_list (current_class_type,
9185 assertion,
9186 /*friend_p=*/0);
9187 else
9188 add_stmt (assertion);
9190 return;
9193 /* Fold the expression and convert it to a boolean value. */
9194 condition = perform_implicit_conversion_flags (boolean_type_node, condition,
9195 complain, LOOKUP_NORMAL);
9196 condition = fold_non_dependent_expr (condition, complain);
9198 if (TREE_CODE (condition) == INTEGER_CST && !integer_zerop (condition))
9199 /* Do nothing; the condition is satisfied. */
9201 else
9203 location_t saved_loc = input_location;
9205 input_location = location;
9206 if (TREE_CODE (condition) == INTEGER_CST
9207 && integer_zerop (condition))
9209 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
9210 (TREE_TYPE (TREE_TYPE (message))));
9211 int len = TREE_STRING_LENGTH (message) / sz - 1;
9212 /* Report the error. */
9213 if (len == 0)
9214 error ("static assertion failed");
9215 else
9216 error ("static assertion failed: %s",
9217 TREE_STRING_POINTER (message));
9219 else if (condition && condition != error_mark_node)
9221 error ("non-constant condition for static assertion");
9222 if (require_rvalue_constant_expression (condition))
9223 cxx_constant_value (condition);
9225 input_location = saved_loc;
9229 /* Implements the C++0x decltype keyword. Returns the type of EXPR,
9230 suitable for use as a type-specifier.
9232 ID_EXPRESSION_OR_MEMBER_ACCESS_P is true when EXPR was parsed as an
9233 id-expression or a class member access, FALSE when it was parsed as
9234 a full expression. */
9236 tree
9237 finish_decltype_type (tree expr, bool id_expression_or_member_access_p,
9238 tsubst_flags_t complain)
9240 tree type = NULL_TREE;
9242 if (!expr || error_operand_p (expr))
9243 return error_mark_node;
9245 if (TYPE_P (expr)
9246 || TREE_CODE (expr) == TYPE_DECL
9247 || (TREE_CODE (expr) == BIT_NOT_EXPR
9248 && TYPE_P (TREE_OPERAND (expr, 0))))
9250 if (complain & tf_error)
9251 error ("argument to decltype must be an expression");
9252 return error_mark_node;
9255 /* Depending on the resolution of DR 1172, we may later need to distinguish
9256 instantiation-dependent but not type-dependent expressions so that, say,
9257 A<decltype(sizeof(T))>::U doesn't require 'typename'. */
9258 if (instantiation_dependent_uneval_expression_p (expr))
9260 type = cxx_make_type (DECLTYPE_TYPE);
9261 DECLTYPE_TYPE_EXPR (type) = expr;
9262 DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (type)
9263 = id_expression_or_member_access_p;
9264 SET_TYPE_STRUCTURAL_EQUALITY (type);
9266 return type;
9269 /* The type denoted by decltype(e) is defined as follows: */
9271 expr = resolve_nondeduced_context (expr, complain);
9273 if (invalid_nonstatic_memfn_p (input_location, expr, complain))
9274 return error_mark_node;
9276 if (type_unknown_p (expr))
9278 if (complain & tf_error)
9279 error ("decltype cannot resolve address of overloaded function");
9280 return error_mark_node;
9283 /* To get the size of a static data member declared as an array of
9284 unknown bound, we need to instantiate it. */
9285 if (VAR_P (expr)
9286 && VAR_HAD_UNKNOWN_BOUND (expr)
9287 && DECL_TEMPLATE_INSTANTIATION (expr))
9288 instantiate_decl (expr, /*defer_ok*/true, /*expl_inst_mem*/false);
9290 if (id_expression_or_member_access_p)
9292 /* If e is an id-expression or a class member access (5.2.5
9293 [expr.ref]), decltype(e) is defined as the type of the entity
9294 named by e. If there is no such entity, or e names a set of
9295 overloaded functions, the program is ill-formed. */
9296 if (identifier_p (expr))
9297 expr = lookup_name (expr);
9299 if (INDIRECT_REF_P (expr)
9300 || TREE_CODE (expr) == VIEW_CONVERT_EXPR)
9301 /* This can happen when the expression is, e.g., "a.b". Just
9302 look at the underlying operand. */
9303 expr = TREE_OPERAND (expr, 0);
9305 if (TREE_CODE (expr) == OFFSET_REF
9306 || TREE_CODE (expr) == MEMBER_REF
9307 || TREE_CODE (expr) == SCOPE_REF)
9308 /* We're only interested in the field itself. If it is a
9309 BASELINK, we will need to see through it in the next
9310 step. */
9311 expr = TREE_OPERAND (expr, 1);
9313 if (BASELINK_P (expr))
9314 /* See through BASELINK nodes to the underlying function. */
9315 expr = BASELINK_FUNCTIONS (expr);
9317 /* decltype of a decomposition name drops references in the tuple case
9318 (unlike decltype of a normal variable) and keeps cv-qualifiers from
9319 the containing object in the other cases (unlike decltype of a member
9320 access expression). */
9321 if (DECL_DECOMPOSITION_P (expr))
9323 if (DECL_HAS_VALUE_EXPR_P (expr))
9324 /* Expr is an array or struct subobject proxy, handle
9325 bit-fields properly. */
9326 return unlowered_expr_type (expr);
9327 else
9328 /* Expr is a reference variable for the tuple case. */
9329 return lookup_decomp_type (expr);
9332 switch (TREE_CODE (expr))
9334 case FIELD_DECL:
9335 if (DECL_BIT_FIELD_TYPE (expr))
9337 type = DECL_BIT_FIELD_TYPE (expr);
9338 break;
9340 /* Fall through for fields that aren't bitfields. */
9341 gcc_fallthrough ();
9343 case FUNCTION_DECL:
9344 case VAR_DECL:
9345 case CONST_DECL:
9346 case PARM_DECL:
9347 case RESULT_DECL:
9348 case TEMPLATE_PARM_INDEX:
9349 expr = mark_type_use (expr);
9350 type = TREE_TYPE (expr);
9351 break;
9353 case ERROR_MARK:
9354 type = error_mark_node;
9355 break;
9357 case COMPONENT_REF:
9358 case COMPOUND_EXPR:
9359 mark_type_use (expr);
9360 type = is_bitfield_expr_with_lowered_type (expr);
9361 if (!type)
9362 type = TREE_TYPE (TREE_OPERAND (expr, 1));
9363 break;
9365 case BIT_FIELD_REF:
9366 gcc_unreachable ();
9368 case INTEGER_CST:
9369 case PTRMEM_CST:
9370 /* We can get here when the id-expression refers to an
9371 enumerator or non-type template parameter. */
9372 type = TREE_TYPE (expr);
9373 break;
9375 default:
9376 /* Handle instantiated template non-type arguments. */
9377 type = TREE_TYPE (expr);
9378 break;
9381 else
9383 /* Within a lambda-expression:
9385 Every occurrence of decltype((x)) where x is a possibly
9386 parenthesized id-expression that names an entity of
9387 automatic storage duration is treated as if x were
9388 transformed into an access to a corresponding data member
9389 of the closure type that would have been declared if x
9390 were a use of the denoted entity. */
9391 if (outer_automatic_var_p (expr)
9392 && current_function_decl
9393 && LAMBDA_FUNCTION_P (current_function_decl))
9394 type = capture_decltype (expr);
9395 else if (error_operand_p (expr))
9396 type = error_mark_node;
9397 else if (expr == current_class_ptr)
9398 /* If the expression is just "this", we want the
9399 cv-unqualified pointer for the "this" type. */
9400 type = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
9401 else
9403 /* Otherwise, where T is the type of e, if e is an lvalue,
9404 decltype(e) is defined as T&; if an xvalue, T&&; otherwise, T. */
9405 cp_lvalue_kind clk = lvalue_kind (expr);
9406 type = unlowered_expr_type (expr);
9407 gcc_assert (!TYPE_REF_P (type));
9409 /* For vector types, pick a non-opaque variant. */
9410 if (VECTOR_TYPE_P (type))
9411 type = strip_typedefs (type);
9413 if (clk != clk_none && !(clk & clk_class))
9414 type = cp_build_reference_type (type, (clk & clk_rvalueref));
9418 return type;
9421 /* Called from trait_expr_value to evaluate either __has_nothrow_assign or
9422 __has_nothrow_copy, depending on assign_p. Returns true iff all
9423 the copy {ctor,assign} fns are nothrow. */
9425 static bool
9426 classtype_has_nothrow_assign_or_copy_p (tree type, bool assign_p)
9428 tree fns = NULL_TREE;
9430 if (assign_p || TYPE_HAS_COPY_CTOR (type))
9431 fns = get_class_binding (type, assign_p ? assign_op_identifier
9432 : ctor_identifier);
9434 bool saw_copy = false;
9435 for (ovl_iterator iter (fns); iter; ++iter)
9437 tree fn = *iter;
9439 if (copy_fn_p (fn) > 0)
9441 saw_copy = true;
9442 maybe_instantiate_noexcept (fn);
9443 if (!TYPE_NOTHROW_P (TREE_TYPE (fn)))
9444 return false;
9448 return saw_copy;
9451 /* Actually evaluates the trait. */
9453 static bool
9454 trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
9456 enum tree_code type_code1;
9457 tree t;
9459 type_code1 = TREE_CODE (type1);
9461 switch (kind)
9463 case CPTK_HAS_NOTHROW_ASSIGN:
9464 type1 = strip_array_types (type1);
9465 return (!CP_TYPE_CONST_P (type1) && type_code1 != REFERENCE_TYPE
9466 && (trait_expr_value (CPTK_HAS_TRIVIAL_ASSIGN, type1, type2)
9467 || (CLASS_TYPE_P (type1)
9468 && classtype_has_nothrow_assign_or_copy_p (type1,
9469 true))));
9471 case CPTK_HAS_TRIVIAL_ASSIGN:
9472 /* ??? The standard seems to be missing the "or array of such a class
9473 type" wording for this trait. */
9474 type1 = strip_array_types (type1);
9475 return (!CP_TYPE_CONST_P (type1) && type_code1 != REFERENCE_TYPE
9476 && (trivial_type_p (type1)
9477 || (CLASS_TYPE_P (type1)
9478 && TYPE_HAS_TRIVIAL_COPY_ASSIGN (type1))));
9480 case CPTK_HAS_NOTHROW_CONSTRUCTOR:
9481 type1 = strip_array_types (type1);
9482 return (trait_expr_value (CPTK_HAS_TRIVIAL_CONSTRUCTOR, type1, type2)
9483 || (CLASS_TYPE_P (type1)
9484 && (t = locate_ctor (type1))
9485 && (maybe_instantiate_noexcept (t),
9486 TYPE_NOTHROW_P (TREE_TYPE (t)))));
9488 case CPTK_HAS_TRIVIAL_CONSTRUCTOR:
9489 type1 = strip_array_types (type1);
9490 return (trivial_type_p (type1)
9491 || (CLASS_TYPE_P (type1) && TYPE_HAS_TRIVIAL_DFLT (type1)));
9493 case CPTK_HAS_NOTHROW_COPY:
9494 type1 = strip_array_types (type1);
9495 return (trait_expr_value (CPTK_HAS_TRIVIAL_COPY, type1, type2)
9496 || (CLASS_TYPE_P (type1)
9497 && classtype_has_nothrow_assign_or_copy_p (type1, false)));
9499 case CPTK_HAS_TRIVIAL_COPY:
9500 /* ??? The standard seems to be missing the "or array of such a class
9501 type" wording for this trait. */
9502 type1 = strip_array_types (type1);
9503 return (trivial_type_p (type1) || type_code1 == REFERENCE_TYPE
9504 || (CLASS_TYPE_P (type1) && TYPE_HAS_TRIVIAL_COPY_CTOR (type1)));
9506 case CPTK_HAS_TRIVIAL_DESTRUCTOR:
9507 type1 = strip_array_types (type1);
9508 return (trivial_type_p (type1) || type_code1 == REFERENCE_TYPE
9509 || (CLASS_TYPE_P (type1)
9510 && TYPE_HAS_TRIVIAL_DESTRUCTOR (type1)));
9512 case CPTK_HAS_VIRTUAL_DESTRUCTOR:
9513 return type_has_virtual_destructor (type1);
9515 case CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS:
9516 return type_has_unique_obj_representations (type1);
9518 case CPTK_IS_ABSTRACT:
9519 return ABSTRACT_CLASS_TYPE_P (type1);
9521 case CPTK_IS_AGGREGATE:
9522 return CP_AGGREGATE_TYPE_P (type1);
9524 case CPTK_IS_BASE_OF:
9525 return (NON_UNION_CLASS_TYPE_P (type1) && NON_UNION_CLASS_TYPE_P (type2)
9526 && (same_type_ignoring_top_level_qualifiers_p (type1, type2)
9527 || DERIVED_FROM_P (type1, type2)));
9529 case CPTK_IS_CLASS:
9530 return NON_UNION_CLASS_TYPE_P (type1);
9532 case CPTK_IS_EMPTY:
9533 return NON_UNION_CLASS_TYPE_P (type1) && CLASSTYPE_EMPTY_P (type1);
9535 case CPTK_IS_ENUM:
9536 return type_code1 == ENUMERAL_TYPE;
9538 case CPTK_IS_FINAL:
9539 return CLASS_TYPE_P (type1) && CLASSTYPE_FINAL (type1);
9541 case CPTK_IS_LITERAL_TYPE:
9542 return literal_type_p (type1);
9544 case CPTK_IS_POD:
9545 return pod_type_p (type1);
9547 case CPTK_IS_POLYMORPHIC:
9548 return CLASS_TYPE_P (type1) && TYPE_POLYMORPHIC_P (type1);
9550 case CPTK_IS_SAME_AS:
9551 return same_type_p (type1, type2);
9553 case CPTK_IS_STD_LAYOUT:
9554 return std_layout_type_p (type1);
9556 case CPTK_IS_TRIVIAL:
9557 return trivial_type_p (type1);
9559 case CPTK_IS_TRIVIALLY_ASSIGNABLE:
9560 return is_trivially_xible (MODIFY_EXPR, type1, type2);
9562 case CPTK_IS_TRIVIALLY_CONSTRUCTIBLE:
9563 return is_trivially_xible (INIT_EXPR, type1, type2);
9565 case CPTK_IS_TRIVIALLY_COPYABLE:
9566 return trivially_copyable_p (type1);
9568 case CPTK_IS_UNION:
9569 return type_code1 == UNION_TYPE;
9571 case CPTK_IS_ASSIGNABLE:
9572 return is_xible (MODIFY_EXPR, type1, type2);
9574 case CPTK_IS_CONSTRUCTIBLE:
9575 return is_xible (INIT_EXPR, type1, type2);
9577 default:
9578 gcc_unreachable ();
9579 return false;
9583 /* If TYPE is an array of unknown bound, or (possibly cv-qualified)
9584 void, or a complete type, returns true, otherwise false. */
9586 static bool
9587 check_trait_type (tree type)
9589 if (type == NULL_TREE)
9590 return true;
9592 if (TREE_CODE (type) == TREE_LIST)
9593 return (check_trait_type (TREE_VALUE (type))
9594 && check_trait_type (TREE_CHAIN (type)));
9596 if (TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type)
9597 && COMPLETE_TYPE_P (TREE_TYPE (type)))
9598 return true;
9600 if (VOID_TYPE_P (type))
9601 return true;
9603 return !!complete_type_or_else (strip_array_types (type), NULL_TREE);
9606 /* Process a trait expression. */
9608 tree
9609 finish_trait_expr (cp_trait_kind kind, tree type1, tree type2)
9611 if (type1 == error_mark_node
9612 || type2 == error_mark_node)
9613 return error_mark_node;
9615 if (processing_template_decl)
9617 tree trait_expr = make_node (TRAIT_EXPR);
9618 TREE_TYPE (trait_expr) = boolean_type_node;
9619 TRAIT_EXPR_TYPE1 (trait_expr) = type1;
9620 TRAIT_EXPR_TYPE2 (trait_expr) = type2;
9621 TRAIT_EXPR_KIND (trait_expr) = kind;
9622 return trait_expr;
9625 switch (kind)
9627 case CPTK_HAS_NOTHROW_ASSIGN:
9628 case CPTK_HAS_TRIVIAL_ASSIGN:
9629 case CPTK_HAS_NOTHROW_CONSTRUCTOR:
9630 case CPTK_HAS_TRIVIAL_CONSTRUCTOR:
9631 case CPTK_HAS_NOTHROW_COPY:
9632 case CPTK_HAS_TRIVIAL_COPY:
9633 case CPTK_HAS_TRIVIAL_DESTRUCTOR:
9634 case CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS:
9635 case CPTK_HAS_VIRTUAL_DESTRUCTOR:
9636 case CPTK_IS_ABSTRACT:
9637 case CPTK_IS_AGGREGATE:
9638 case CPTK_IS_EMPTY:
9639 case CPTK_IS_FINAL:
9640 case CPTK_IS_LITERAL_TYPE:
9641 case CPTK_IS_POD:
9642 case CPTK_IS_POLYMORPHIC:
9643 case CPTK_IS_STD_LAYOUT:
9644 case CPTK_IS_TRIVIAL:
9645 case CPTK_IS_TRIVIALLY_COPYABLE:
9646 if (!check_trait_type (type1))
9647 return error_mark_node;
9648 break;
9650 case CPTK_IS_ASSIGNABLE:
9651 case CPTK_IS_CONSTRUCTIBLE:
9652 break;
9654 case CPTK_IS_TRIVIALLY_ASSIGNABLE:
9655 case CPTK_IS_TRIVIALLY_CONSTRUCTIBLE:
9656 if (!check_trait_type (type1)
9657 || !check_trait_type (type2))
9658 return error_mark_node;
9659 break;
9661 case CPTK_IS_BASE_OF:
9662 if (NON_UNION_CLASS_TYPE_P (type1) && NON_UNION_CLASS_TYPE_P (type2)
9663 && !same_type_ignoring_top_level_qualifiers_p (type1, type2)
9664 && !complete_type_or_else (type2, NULL_TREE))
9665 /* We already issued an error. */
9666 return error_mark_node;
9667 break;
9669 case CPTK_IS_CLASS:
9670 case CPTK_IS_ENUM:
9671 case CPTK_IS_UNION:
9672 case CPTK_IS_SAME_AS:
9673 break;
9675 default:
9676 gcc_unreachable ();
9679 return (trait_expr_value (kind, type1, type2)
9680 ? boolean_true_node : boolean_false_node);
9683 /* Do-nothing variants of functions to handle pragma FLOAT_CONST_DECIMAL64,
9684 which is ignored for C++. */
9686 void
9687 set_float_const_decimal64 (void)
9691 void
9692 clear_float_const_decimal64 (void)
9696 bool
9697 float_const_decimal64_p (void)
9699 return 0;
9703 /* Return true if T designates the implied `this' parameter. */
9705 bool
9706 is_this_parameter (tree t)
9708 if (!DECL_P (t) || DECL_NAME (t) != this_identifier)
9709 return false;
9710 gcc_assert (TREE_CODE (t) == PARM_DECL || is_capture_proxy (t)
9711 || (cp_binding_oracle && TREE_CODE (t) == VAR_DECL));
9712 return true;
9715 /* Insert the deduced return type for an auto function. */
9717 void
9718 apply_deduced_return_type (tree fco, tree return_type)
9720 tree result;
9722 if (return_type == error_mark_node)
9723 return;
9725 if (DECL_CONV_FN_P (fco))
9726 DECL_NAME (fco) = make_conv_op_name (return_type);
9728 TREE_TYPE (fco) = change_return_type (return_type, TREE_TYPE (fco));
9730 result = DECL_RESULT (fco);
9731 if (result == NULL_TREE)
9732 return;
9733 if (TREE_TYPE (result) == return_type)
9734 return;
9736 if (!processing_template_decl && !VOID_TYPE_P (return_type)
9737 && !complete_type_or_else (return_type, NULL_TREE))
9738 return;
9740 /* We already have a DECL_RESULT from start_preparsed_function.
9741 Now we need to redo the work it and allocate_struct_function
9742 did to reflect the new type. */
9743 gcc_assert (current_function_decl == fco);
9744 result = build_decl (input_location, RESULT_DECL, NULL_TREE,
9745 TYPE_MAIN_VARIANT (return_type));
9746 DECL_ARTIFICIAL (result) = 1;
9747 DECL_IGNORED_P (result) = 1;
9748 cp_apply_type_quals_to_decl (cp_type_quals (return_type),
9749 result);
9751 DECL_RESULT (fco) = result;
9753 if (!processing_template_decl)
9755 bool aggr = aggregate_value_p (result, fco);
9756 #ifdef PCC_STATIC_STRUCT_RETURN
9757 cfun->returns_pcc_struct = aggr;
9758 #endif
9759 cfun->returns_struct = aggr;
9764 /* DECL is a local variable or parameter from the surrounding scope of a
9765 lambda-expression. Returns the decltype for a use of the capture field
9766 for DECL even if it hasn't been captured yet. */
9768 static tree
9769 capture_decltype (tree decl)
9771 tree lam = CLASSTYPE_LAMBDA_EXPR (DECL_CONTEXT (current_function_decl));
9772 /* FIXME do lookup instead of list walk? */
9773 tree cap = value_member (decl, LAMBDA_EXPR_CAPTURE_LIST (lam));
9774 tree type;
9776 if (cap)
9777 type = TREE_TYPE (TREE_PURPOSE (cap));
9778 else
9779 switch (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lam))
9781 case CPLD_NONE:
9782 error ("%qD is not captured", decl);
9783 return error_mark_node;
9785 case CPLD_COPY:
9786 type = TREE_TYPE (decl);
9787 if (TYPE_REF_P (type)
9788 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
9789 type = TREE_TYPE (type);
9790 break;
9792 case CPLD_REFERENCE:
9793 type = TREE_TYPE (decl);
9794 if (!TYPE_REF_P (type))
9795 type = build_reference_type (TREE_TYPE (decl));
9796 break;
9798 default:
9799 gcc_unreachable ();
9802 if (!TYPE_REF_P (type))
9804 if (!LAMBDA_EXPR_MUTABLE_P (lam))
9805 type = cp_build_qualified_type (type, (cp_type_quals (type)
9806 |TYPE_QUAL_CONST));
9807 type = build_reference_type (type);
9809 return type;
9812 /* Build a unary fold expression of EXPR over OP. If IS_RIGHT is true,
9813 this is a right unary fold. Otherwise it is a left unary fold. */
9815 static tree
9816 finish_unary_fold_expr (tree expr, int op, tree_code dir)
9818 // Build a pack expansion (assuming expr has pack type).
9819 if (!uses_parameter_packs (expr))
9821 error_at (location_of (expr), "operand of fold expression has no "
9822 "unexpanded parameter packs");
9823 return error_mark_node;
9825 tree pack = make_pack_expansion (expr);
9827 // Build the fold expression.
9828 tree code = build_int_cstu (integer_type_node, abs (op));
9829 tree fold = build_min_nt_loc (UNKNOWN_LOCATION, dir, code, pack);
9830 FOLD_EXPR_MODIFY_P (fold) = (op < 0);
9831 return fold;
9834 tree
9835 finish_left_unary_fold_expr (tree expr, int op)
9837 return finish_unary_fold_expr (expr, op, UNARY_LEFT_FOLD_EXPR);
9840 tree
9841 finish_right_unary_fold_expr (tree expr, int op)
9843 return finish_unary_fold_expr (expr, op, UNARY_RIGHT_FOLD_EXPR);
9846 /* Build a binary fold expression over EXPR1 and EXPR2. The
9847 associativity of the fold is determined by EXPR1 and EXPR2 (whichever
9848 has an unexpanded parameter pack). */
9850 tree
9851 finish_binary_fold_expr (tree pack, tree init, int op, tree_code dir)
9853 pack = make_pack_expansion (pack);
9854 tree code = build_int_cstu (integer_type_node, abs (op));
9855 tree fold = build_min_nt_loc (UNKNOWN_LOCATION, dir, code, pack, init);
9856 FOLD_EXPR_MODIFY_P (fold) = (op < 0);
9857 return fold;
9860 tree
9861 finish_binary_fold_expr (tree expr1, tree expr2, int op)
9863 // Determine which expr has an unexpanded parameter pack and
9864 // set the pack and initial term.
9865 bool pack1 = uses_parameter_packs (expr1);
9866 bool pack2 = uses_parameter_packs (expr2);
9867 if (pack1 && !pack2)
9868 return finish_binary_fold_expr (expr1, expr2, op, BINARY_RIGHT_FOLD_EXPR);
9869 else if (pack2 && !pack1)
9870 return finish_binary_fold_expr (expr2, expr1, op, BINARY_LEFT_FOLD_EXPR);
9871 else
9873 if (pack1)
9874 error ("both arguments in binary fold have unexpanded parameter packs");
9875 else
9876 error ("no unexpanded parameter packs in binary fold");
9878 return error_mark_node;
9881 /* Finish __builtin_launder (arg). */
9883 tree
9884 finish_builtin_launder (location_t loc, tree arg, tsubst_flags_t complain)
9886 tree orig_arg = arg;
9887 if (!type_dependent_expression_p (arg))
9888 arg = decay_conversion (arg, complain);
9889 if (error_operand_p (arg))
9890 return error_mark_node;
9891 if (!type_dependent_expression_p (arg)
9892 && !TYPE_PTR_P (TREE_TYPE (arg)))
9894 error_at (loc, "non-pointer argument to %<__builtin_launder%>");
9895 return error_mark_node;
9897 if (processing_template_decl)
9898 arg = orig_arg;
9899 return build_call_expr_internal_loc (loc, IFN_LAUNDER,
9900 TREE_TYPE (arg), 1, arg);
9903 #include "gt-cp-semantics.h"