Add quotes for constexpr keyword.
[official-gcc.git] / gcc / cp / semantics.c
blob51489d17ad58f56da3f8fb8837e0e8962f4a2c3b
1 /* Perform the semantic phase of parsing, i.e., the process of
2 building tree structure, checking semantic consistency, and
3 building RTL. These routines are used both during actual parsing
4 and during the instantiation of template functions.
6 Copyright (C) 1998-2017 Free Software Foundation, Inc.
7 Written by Mark Mitchell (mmitchell@usa.net) based on code found
8 formerly in parse.y and pt.c.
10 This file is part of GCC.
12 GCC is free software; you can redistribute it and/or modify it
13 under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 3, or (at your option)
15 any later version.
17 GCC is distributed in the hope that it will be useful, but
18 WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with GCC; see the file COPYING3. If not see
24 <http://www.gnu.org/licenses/>. */
26 #include "config.h"
27 #include "system.h"
28 #include "coretypes.h"
29 #include "target.h"
30 #include "bitmap.h"
31 #include "cp-tree.h"
32 #include "stringpool.h"
33 #include "cgraph.h"
34 #include "stmt.h"
35 #include "varasm.h"
36 #include "stor-layout.h"
37 #include "c-family/c-objc.h"
38 #include "tree-inline.h"
39 #include "intl.h"
40 #include "tree-iterator.h"
41 #include "omp-general.h"
42 #include "convert.h"
43 #include "stringpool.h"
44 #include "attribs.h"
45 #include "gomp-constants.h"
46 #include "predict.h"
48 /* There routines provide a modular interface to perform many parsing
49 operations. They may therefore be used during actual parsing, or
50 during template instantiation, which may be regarded as a
51 degenerate form of parsing. */
53 static tree maybe_convert_cond (tree);
54 static tree finalize_nrv_r (tree *, int *, void *);
55 static tree capture_decltype (tree);
57 /* Used for OpenMP non-static data member privatization. */
59 static hash_map<tree, tree> *omp_private_member_map;
60 static vec<tree> omp_private_member_vec;
61 static bool omp_private_member_ignore_next;
64 /* Deferred Access Checking Overview
65 ---------------------------------
67 Most C++ expressions and declarations require access checking
68 to be performed during parsing. However, in several cases,
69 this has to be treated differently.
71 For member declarations, access checking has to be deferred
72 until more information about the declaration is known. For
73 example:
75 class A {
76 typedef int X;
77 public:
78 X f();
81 A::X A::f();
82 A::X g();
84 When we are parsing the function return type `A::X', we don't
85 really know if this is allowed until we parse the function name.
87 Furthermore, some contexts require that access checking is
88 never performed at all. These include class heads, and template
89 instantiations.
91 Typical use of access checking functions is described here:
93 1. When we enter a context that requires certain access checking
94 mode, the function `push_deferring_access_checks' is called with
95 DEFERRING argument specifying the desired mode. Access checking
96 may be performed immediately (dk_no_deferred), deferred
97 (dk_deferred), or not performed (dk_no_check).
99 2. When a declaration such as a type, or a variable, is encountered,
100 the function `perform_or_defer_access_check' is called. It
101 maintains a vector of all deferred checks.
103 3. The global `current_class_type' or `current_function_decl' is then
104 setup by the parser. `enforce_access' relies on these information
105 to check access.
107 4. Upon exiting the context mentioned in step 1,
108 `perform_deferred_access_checks' is called to check all declaration
109 stored in the vector. `pop_deferring_access_checks' is then
110 called to restore the previous access checking mode.
112 In case of parsing error, we simply call `pop_deferring_access_checks'
113 without `perform_deferred_access_checks'. */
115 struct GTY(()) deferred_access {
116 /* A vector representing name-lookups for which we have deferred
117 checking access controls. We cannot check the accessibility of
118 names used in a decl-specifier-seq until we know what is being
119 declared because code like:
121 class A {
122 class B {};
123 B* f();
126 A::B* A::f() { return 0; }
128 is valid, even though `A::B' is not generally accessible. */
129 vec<deferred_access_check, va_gc> * GTY(()) deferred_access_checks;
131 /* The current mode of access checks. */
132 enum deferring_kind deferring_access_checks_kind;
136 /* Data for deferred access checking. */
137 static GTY(()) vec<deferred_access, va_gc> *deferred_access_stack;
138 static GTY(()) unsigned deferred_access_no_check;
140 /* Save the current deferred access states and start deferred
141 access checking iff DEFER_P is true. */
143 void
144 push_deferring_access_checks (deferring_kind deferring)
146 /* For context like template instantiation, access checking
147 disabling applies to all nested context. */
148 if (deferred_access_no_check || deferring == dk_no_check)
149 deferred_access_no_check++;
150 else
152 deferred_access e = {NULL, deferring};
153 vec_safe_push (deferred_access_stack, e);
157 /* Save the current deferred access states and start deferred access
158 checking, continuing the set of deferred checks in CHECKS. */
160 void
161 reopen_deferring_access_checks (vec<deferred_access_check, va_gc> * checks)
163 push_deferring_access_checks (dk_deferred);
164 if (!deferred_access_no_check)
165 deferred_access_stack->last().deferred_access_checks = checks;
168 /* Resume deferring access checks again after we stopped doing
169 this previously. */
171 void
172 resume_deferring_access_checks (void)
174 if (!deferred_access_no_check)
175 deferred_access_stack->last().deferring_access_checks_kind = dk_deferred;
178 /* Stop deferring access checks. */
180 void
181 stop_deferring_access_checks (void)
183 if (!deferred_access_no_check)
184 deferred_access_stack->last().deferring_access_checks_kind = dk_no_deferred;
187 /* Discard the current deferred access checks and restore the
188 previous states. */
190 void
191 pop_deferring_access_checks (void)
193 if (deferred_access_no_check)
194 deferred_access_no_check--;
195 else
196 deferred_access_stack->pop ();
199 /* Returns a TREE_LIST representing the deferred checks.
200 The TREE_PURPOSE of each node is the type through which the
201 access occurred; the TREE_VALUE is the declaration named.
204 vec<deferred_access_check, va_gc> *
205 get_deferred_access_checks (void)
207 if (deferred_access_no_check)
208 return NULL;
209 else
210 return (deferred_access_stack->last().deferred_access_checks);
213 /* Take current deferred checks and combine with the
214 previous states if we also defer checks previously.
215 Otherwise perform checks now. */
217 void
218 pop_to_parent_deferring_access_checks (void)
220 if (deferred_access_no_check)
221 deferred_access_no_check--;
222 else
224 vec<deferred_access_check, va_gc> *checks;
225 deferred_access *ptr;
227 checks = (deferred_access_stack->last ().deferred_access_checks);
229 deferred_access_stack->pop ();
230 ptr = &deferred_access_stack->last ();
231 if (ptr->deferring_access_checks_kind == dk_no_deferred)
233 /* Check access. */
234 perform_access_checks (checks, tf_warning_or_error);
236 else
238 /* Merge with parent. */
239 int i, j;
240 deferred_access_check *chk, *probe;
242 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
244 FOR_EACH_VEC_SAFE_ELT (ptr->deferred_access_checks, j, probe)
246 if (probe->binfo == chk->binfo &&
247 probe->decl == chk->decl &&
248 probe->diag_decl == chk->diag_decl)
249 goto found;
251 /* Insert into parent's checks. */
252 vec_safe_push (ptr->deferred_access_checks, *chk);
253 found:;
259 /* Perform the access checks in CHECKS. The TREE_PURPOSE of each node
260 is the BINFO indicating the qualifying scope used to access the
261 DECL node stored in the TREE_VALUE of the node. If CHECKS is empty
262 or we aren't in SFINAE context or all the checks succeed return TRUE,
263 otherwise FALSE. */
265 bool
266 perform_access_checks (vec<deferred_access_check, va_gc> *checks,
267 tsubst_flags_t complain)
269 int i;
270 deferred_access_check *chk;
271 location_t loc = input_location;
272 bool ok = true;
274 if (!checks)
275 return true;
277 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
279 input_location = chk->loc;
280 ok &= enforce_access (chk->binfo, chk->decl, chk->diag_decl, complain);
283 input_location = loc;
284 return (complain & tf_error) ? true : ok;
287 /* Perform the deferred access checks.
289 After performing the checks, we still have to keep the list
290 `deferred_access_stack->deferred_access_checks' since we may want
291 to check access for them again later in a different context.
292 For example:
294 class A {
295 typedef int X;
296 static X a;
298 A::X A::a, x; // No error for `A::a', error for `x'
300 We have to perform deferred access of `A::X', first with `A::a',
301 next with `x'. Return value like perform_access_checks above. */
303 bool
304 perform_deferred_access_checks (tsubst_flags_t complain)
306 return perform_access_checks (get_deferred_access_checks (), complain);
309 /* Defer checking the accessibility of DECL, when looked up in
310 BINFO. DIAG_DECL is the declaration to use to print diagnostics.
311 Return value like perform_access_checks above.
312 If non-NULL, report failures to AFI. */
314 bool
315 perform_or_defer_access_check (tree binfo, tree decl, tree diag_decl,
316 tsubst_flags_t complain,
317 access_failure_info *afi)
319 int i;
320 deferred_access *ptr;
321 deferred_access_check *chk;
324 /* Exit if we are in a context that no access checking is performed.
326 if (deferred_access_no_check)
327 return true;
329 gcc_assert (TREE_CODE (binfo) == TREE_BINFO);
331 ptr = &deferred_access_stack->last ();
333 /* If we are not supposed to defer access checks, just check now. */
334 if (ptr->deferring_access_checks_kind == dk_no_deferred)
336 bool ok = enforce_access (binfo, decl, diag_decl, complain, afi);
337 return (complain & tf_error) ? true : ok;
340 /* See if we are already going to perform this check. */
341 FOR_EACH_VEC_SAFE_ELT (ptr->deferred_access_checks, i, chk)
343 if (chk->decl == decl && chk->binfo == binfo &&
344 chk->diag_decl == diag_decl)
346 return true;
349 /* If not, record the check. */
350 deferred_access_check new_access = {binfo, decl, diag_decl, input_location};
351 vec_safe_push (ptr->deferred_access_checks, new_access);
353 return true;
356 /* Returns nonzero if the current statement is a full expression,
357 i.e. temporaries created during that statement should be destroyed
358 at the end of the statement. */
361 stmts_are_full_exprs_p (void)
363 return current_stmt_tree ()->stmts_are_full_exprs_p;
366 /* T is a statement. Add it to the statement-tree. This is the C++
367 version. The C/ObjC frontends have a slightly different version of
368 this function. */
370 tree
371 add_stmt (tree t)
373 enum tree_code code = TREE_CODE (t);
375 if (EXPR_P (t) && code != LABEL_EXPR)
377 if (!EXPR_HAS_LOCATION (t))
378 SET_EXPR_LOCATION (t, input_location);
380 /* When we expand a statement-tree, we must know whether or not the
381 statements are full-expressions. We record that fact here. */
382 STMT_IS_FULL_EXPR_P (t) = stmts_are_full_exprs_p ();
385 if (code == LABEL_EXPR || code == CASE_LABEL_EXPR)
386 STATEMENT_LIST_HAS_LABEL (cur_stmt_list) = 1;
388 /* Add T to the statement-tree. Non-side-effect statements need to be
389 recorded during statement expressions. */
390 gcc_checking_assert (!stmt_list_stack->is_empty ());
391 append_to_statement_list_force (t, &cur_stmt_list);
393 return t;
396 /* Returns the stmt_tree to which statements are currently being added. */
398 stmt_tree
399 current_stmt_tree (void)
401 return (cfun
402 ? &cfun->language->base.x_stmt_tree
403 : &scope_chain->x_stmt_tree);
406 /* If statements are full expressions, wrap STMT in a CLEANUP_POINT_EXPR. */
408 static tree
409 maybe_cleanup_point_expr (tree expr)
411 if (!processing_template_decl && stmts_are_full_exprs_p ())
412 expr = fold_build_cleanup_point_expr (TREE_TYPE (expr), expr);
413 return expr;
416 /* Like maybe_cleanup_point_expr except have the type of the new expression be
417 void so we don't need to create a temporary variable to hold the inner
418 expression. The reason why we do this is because the original type might be
419 an aggregate and we cannot create a temporary variable for that type. */
421 tree
422 maybe_cleanup_point_expr_void (tree expr)
424 if (!processing_template_decl && stmts_are_full_exprs_p ())
425 expr = fold_build_cleanup_point_expr (void_type_node, expr);
426 return expr;
431 /* Create a declaration statement for the declaration given by the DECL. */
433 void
434 add_decl_expr (tree decl)
436 tree r = build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl);
437 if (DECL_INITIAL (decl)
438 || (DECL_SIZE (decl) && TREE_SIDE_EFFECTS (DECL_SIZE (decl))))
439 r = maybe_cleanup_point_expr_void (r);
440 add_stmt (r);
443 /* Finish a scope. */
445 tree
446 do_poplevel (tree stmt_list)
448 tree block = NULL;
450 if (stmts_are_full_exprs_p ())
451 block = poplevel (kept_level_p (), 1, 0);
453 stmt_list = pop_stmt_list (stmt_list);
455 if (!processing_template_decl)
457 stmt_list = c_build_bind_expr (input_location, block, stmt_list);
458 /* ??? See c_end_compound_stmt re statement expressions. */
461 return stmt_list;
464 /* Begin a new scope. */
466 static tree
467 do_pushlevel (scope_kind sk)
469 tree ret = push_stmt_list ();
470 if (stmts_are_full_exprs_p ())
471 begin_scope (sk, NULL);
472 return ret;
475 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
476 when the current scope is exited. EH_ONLY is true when this is not
477 meant to apply to normal control flow transfer. */
479 void
480 push_cleanup (tree decl, tree cleanup, bool eh_only)
482 tree stmt = build_stmt (input_location, CLEANUP_STMT, NULL, cleanup, decl);
483 CLEANUP_EH_ONLY (stmt) = eh_only;
484 add_stmt (stmt);
485 CLEANUP_BODY (stmt) = push_stmt_list ();
488 /* Simple infinite loop tracking for -Wreturn-type. We keep a stack of all
489 the current loops, represented by 'NULL_TREE' if we've seen a possible
490 exit, and 'error_mark_node' if not. This is currently used only to
491 suppress the warning about a function with no return statements, and
492 therefore we don't bother noting returns as possible exits. We also
493 don't bother with gotos. */
495 static void
496 begin_maybe_infinite_loop (tree cond)
498 /* Only track this while parsing a function, not during instantiation. */
499 if (!cfun || (DECL_TEMPLATE_INSTANTIATION (current_function_decl)
500 && !processing_template_decl))
501 return;
502 bool maybe_infinite = true;
503 if (cond)
505 cond = fold_non_dependent_expr (cond);
506 maybe_infinite = integer_nonzerop (cond);
508 vec_safe_push (cp_function_chain->infinite_loops,
509 maybe_infinite ? error_mark_node : NULL_TREE);
513 /* A break is a possible exit for the current loop. */
515 void
516 break_maybe_infinite_loop (void)
518 if (!cfun)
519 return;
520 cp_function_chain->infinite_loops->last() = NULL_TREE;
523 /* If we reach the end of the loop without seeing a possible exit, we have
524 an infinite loop. */
526 static void
527 end_maybe_infinite_loop (tree cond)
529 if (!cfun || (DECL_TEMPLATE_INSTANTIATION (current_function_decl)
530 && !processing_template_decl))
531 return;
532 tree current = cp_function_chain->infinite_loops->pop();
533 if (current != NULL_TREE)
535 cond = fold_non_dependent_expr (cond);
536 if (integer_nonzerop (cond))
537 current_function_infinite_loop = 1;
542 /* Begin a conditional that might contain a declaration. When generating
543 normal code, we want the declaration to appear before the statement
544 containing the conditional. When generating template code, we want the
545 conditional to be rendered as the raw DECL_EXPR. */
547 static void
548 begin_cond (tree *cond_p)
550 if (processing_template_decl)
551 *cond_p = push_stmt_list ();
554 /* Finish such a conditional. */
556 static void
557 finish_cond (tree *cond_p, tree expr)
559 if (processing_template_decl)
561 tree cond = pop_stmt_list (*cond_p);
563 if (expr == NULL_TREE)
564 /* Empty condition in 'for'. */
565 gcc_assert (empty_expr_stmt_p (cond));
566 else if (check_for_bare_parameter_packs (expr))
567 expr = error_mark_node;
568 else if (!empty_expr_stmt_p (cond))
569 expr = build2 (COMPOUND_EXPR, TREE_TYPE (expr), cond, expr);
571 *cond_p = expr;
574 /* If *COND_P specifies a conditional with a declaration, transform the
575 loop such that
576 while (A x = 42) { }
577 for (; A x = 42;) { }
578 becomes
579 while (true) { A x = 42; if (!x) break; }
580 for (;;) { A x = 42; if (!x) break; }
581 The statement list for BODY will be empty if the conditional did
582 not declare anything. */
584 static void
585 simplify_loop_decl_cond (tree *cond_p, tree body)
587 tree cond, if_stmt;
589 if (!TREE_SIDE_EFFECTS (body))
590 return;
592 cond = *cond_p;
593 *cond_p = boolean_true_node;
595 if_stmt = begin_if_stmt ();
596 cond = cp_build_unary_op (TRUTH_NOT_EXPR, cond, false, tf_warning_or_error);
597 finish_if_stmt_cond (cond, if_stmt);
598 finish_break_stmt ();
599 finish_then_clause (if_stmt);
600 finish_if_stmt (if_stmt);
603 /* Finish a goto-statement. */
605 tree
606 finish_goto_stmt (tree destination)
608 if (identifier_p (destination))
609 destination = lookup_label (destination);
611 /* We warn about unused labels with -Wunused. That means we have to
612 mark the used labels as used. */
613 if (TREE_CODE (destination) == LABEL_DECL)
614 TREE_USED (destination) = 1;
615 else
617 if (check_no_cilk (destination,
618 "Cilk array notation cannot be used as a computed goto expression",
619 "%<_Cilk_spawn%> statement cannot be used as a computed goto expression"))
620 destination = error_mark_node;
621 destination = mark_rvalue_use (destination);
622 if (!processing_template_decl)
624 destination = cp_convert (ptr_type_node, destination,
625 tf_warning_or_error);
626 if (error_operand_p (destination))
627 return NULL_TREE;
628 destination
629 = fold_build_cleanup_point_expr (TREE_TYPE (destination),
630 destination);
634 check_goto (destination);
636 add_stmt (build_predict_expr (PRED_GOTO, NOT_TAKEN));
637 return add_stmt (build_stmt (input_location, GOTO_EXPR, destination));
640 /* COND is the condition-expression for an if, while, etc.,
641 statement. Convert it to a boolean value, if appropriate.
642 In addition, verify sequence points if -Wsequence-point is enabled. */
644 static tree
645 maybe_convert_cond (tree cond)
647 /* Empty conditions remain empty. */
648 if (!cond)
649 return NULL_TREE;
651 /* Wait until we instantiate templates before doing conversion. */
652 if (processing_template_decl)
653 return cond;
655 if (warn_sequence_point)
656 verify_sequence_points (cond);
658 /* Do the conversion. */
659 cond = convert_from_reference (cond);
661 if (TREE_CODE (cond) == MODIFY_EXPR
662 && !TREE_NO_WARNING (cond)
663 && warn_parentheses)
665 warning_at (EXPR_LOC_OR_LOC (cond, input_location), OPT_Wparentheses,
666 "suggest parentheses around assignment used as truth value");
667 TREE_NO_WARNING (cond) = 1;
670 return condition_conversion (cond);
673 /* Finish an expression-statement, whose EXPRESSION is as indicated. */
675 tree
676 finish_expr_stmt (tree expr)
678 tree r = NULL_TREE;
679 location_t loc = EXPR_LOCATION (expr);
681 if (expr != NULL_TREE)
683 /* If we ran into a problem, make sure we complained. */
684 gcc_assert (expr != error_mark_node || seen_error ());
686 if (!processing_template_decl)
688 if (warn_sequence_point)
689 verify_sequence_points (expr);
690 expr = convert_to_void (expr, ICV_STATEMENT, tf_warning_or_error);
692 else if (!type_dependent_expression_p (expr))
693 convert_to_void (build_non_dependent_expr (expr), ICV_STATEMENT,
694 tf_warning_or_error);
696 if (check_for_bare_parameter_packs (expr))
697 expr = error_mark_node;
699 /* Simplification of inner statement expressions, compound exprs,
700 etc can result in us already having an EXPR_STMT. */
701 if (TREE_CODE (expr) != CLEANUP_POINT_EXPR)
703 if (TREE_CODE (expr) != EXPR_STMT)
704 expr = build_stmt (loc, EXPR_STMT, expr);
705 expr = maybe_cleanup_point_expr_void (expr);
708 r = add_stmt (expr);
711 return r;
715 /* Begin an if-statement. Returns a newly created IF_STMT if
716 appropriate. */
718 tree
719 begin_if_stmt (void)
721 tree r, scope;
722 scope = do_pushlevel (sk_cond);
723 r = build_stmt (input_location, IF_STMT, NULL_TREE,
724 NULL_TREE, NULL_TREE, scope);
725 current_binding_level->this_entity = r;
726 begin_cond (&IF_COND (r));
727 return r;
730 /* Process the COND of an if-statement, which may be given by
731 IF_STMT. */
733 tree
734 finish_if_stmt_cond (tree cond, tree if_stmt)
736 cond = maybe_convert_cond (cond);
737 if (IF_STMT_CONSTEXPR_P (if_stmt)
738 && is_constant_expression (cond)
739 && !value_dependent_expression_p (cond))
741 cond = instantiate_non_dependent_expr (cond);
742 cond = cxx_constant_value (cond, NULL_TREE);
744 finish_cond (&IF_COND (if_stmt), cond);
745 add_stmt (if_stmt);
746 THEN_CLAUSE (if_stmt) = push_stmt_list ();
747 return cond;
750 /* Finish the then-clause of an if-statement, which may be given by
751 IF_STMT. */
753 tree
754 finish_then_clause (tree if_stmt)
756 THEN_CLAUSE (if_stmt) = pop_stmt_list (THEN_CLAUSE (if_stmt));
757 return if_stmt;
760 /* Begin the else-clause of an if-statement. */
762 void
763 begin_else_clause (tree if_stmt)
765 ELSE_CLAUSE (if_stmt) = push_stmt_list ();
768 /* Finish the else-clause of an if-statement, which may be given by
769 IF_STMT. */
771 void
772 finish_else_clause (tree if_stmt)
774 ELSE_CLAUSE (if_stmt) = pop_stmt_list (ELSE_CLAUSE (if_stmt));
777 /* Finish an if-statement. */
779 void
780 finish_if_stmt (tree if_stmt)
782 tree scope = IF_SCOPE (if_stmt);
783 IF_SCOPE (if_stmt) = NULL;
784 add_stmt (do_poplevel (scope));
787 /* Begin a while-statement. Returns a newly created WHILE_STMT if
788 appropriate. */
790 tree
791 begin_while_stmt (void)
793 tree r;
794 r = build_stmt (input_location, WHILE_STMT, NULL_TREE, NULL_TREE);
795 add_stmt (r);
796 WHILE_BODY (r) = do_pushlevel (sk_block);
797 begin_cond (&WHILE_COND (r));
798 return r;
801 /* Process the COND of a while-statement, which may be given by
802 WHILE_STMT. */
804 void
805 finish_while_stmt_cond (tree cond, tree while_stmt, bool ivdep)
807 if (check_no_cilk (cond,
808 "Cilk array notation cannot be used as a condition for while statement",
809 "%<_Cilk_spawn%> statement cannot be used as a condition for while statement"))
810 cond = error_mark_node;
811 cond = maybe_convert_cond (cond);
812 finish_cond (&WHILE_COND (while_stmt), cond);
813 begin_maybe_infinite_loop (cond);
814 if (ivdep && cond != error_mark_node)
815 WHILE_COND (while_stmt) = build2 (ANNOTATE_EXPR,
816 TREE_TYPE (WHILE_COND (while_stmt)),
817 WHILE_COND (while_stmt),
818 build_int_cst (integer_type_node,
819 annot_expr_ivdep_kind));
820 simplify_loop_decl_cond (&WHILE_COND (while_stmt), WHILE_BODY (while_stmt));
823 /* Finish a while-statement, which may be given by WHILE_STMT. */
825 void
826 finish_while_stmt (tree while_stmt)
828 end_maybe_infinite_loop (boolean_true_node);
829 WHILE_BODY (while_stmt) = do_poplevel (WHILE_BODY (while_stmt));
832 /* Begin a do-statement. Returns a newly created DO_STMT if
833 appropriate. */
835 tree
836 begin_do_stmt (void)
838 tree r = build_stmt (input_location, DO_STMT, NULL_TREE, NULL_TREE);
839 begin_maybe_infinite_loop (boolean_true_node);
840 add_stmt (r);
841 DO_BODY (r) = push_stmt_list ();
842 return r;
845 /* Finish the body of a do-statement, which may be given by DO_STMT. */
847 void
848 finish_do_body (tree do_stmt)
850 tree body = DO_BODY (do_stmt) = pop_stmt_list (DO_BODY (do_stmt));
852 if (TREE_CODE (body) == STATEMENT_LIST && STATEMENT_LIST_TAIL (body))
853 body = STATEMENT_LIST_TAIL (body)->stmt;
855 if (IS_EMPTY_STMT (body))
856 warning (OPT_Wempty_body,
857 "suggest explicit braces around empty body in %<do%> statement");
860 /* Finish a do-statement, which may be given by DO_STMT, and whose
861 COND is as indicated. */
863 void
864 finish_do_stmt (tree cond, tree do_stmt, bool ivdep)
866 if (check_no_cilk (cond,
867 "Cilk array notation cannot be used as a condition for a do-while statement",
868 "%<_Cilk_spawn%> statement cannot be used as a condition for a do-while statement"))
869 cond = error_mark_node;
870 cond = maybe_convert_cond (cond);
871 end_maybe_infinite_loop (cond);
872 if (ivdep && cond != error_mark_node)
873 cond = build2 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
874 build_int_cst (integer_type_node, annot_expr_ivdep_kind));
875 DO_COND (do_stmt) = cond;
878 /* Finish a return-statement. The EXPRESSION returned, if any, is as
879 indicated. */
881 tree
882 finish_return_stmt (tree expr)
884 tree r;
885 bool no_warning;
887 expr = check_return_expr (expr, &no_warning);
889 if (error_operand_p (expr)
890 || (flag_openmp && !check_omp_return ()))
892 /* Suppress -Wreturn-type for this function. */
893 if (warn_return_type)
894 TREE_NO_WARNING (current_function_decl) = true;
895 return error_mark_node;
898 if (!processing_template_decl)
900 if (warn_sequence_point)
901 verify_sequence_points (expr);
903 if (DECL_DESTRUCTOR_P (current_function_decl)
904 || (DECL_CONSTRUCTOR_P (current_function_decl)
905 && targetm.cxx.cdtor_returns_this ()))
907 /* Similarly, all destructors must run destructors for
908 base-classes before returning. So, all returns in a
909 destructor get sent to the DTOR_LABEL; finish_function emits
910 code to return a value there. */
911 return finish_goto_stmt (cdtor_label);
915 r = build_stmt (input_location, RETURN_EXPR, expr);
916 TREE_NO_WARNING (r) |= no_warning;
917 r = maybe_cleanup_point_expr_void (r);
918 r = add_stmt (r);
920 return r;
923 /* Begin the scope of a for-statement or a range-for-statement.
924 Both the returned trees are to be used in a call to
925 begin_for_stmt or begin_range_for_stmt. */
927 tree
928 begin_for_scope (tree *init)
930 tree scope = NULL_TREE;
931 if (flag_new_for_scope > 0)
932 scope = do_pushlevel (sk_for);
934 if (processing_template_decl)
935 *init = push_stmt_list ();
936 else
937 *init = NULL_TREE;
939 return scope;
942 /* Begin a for-statement. Returns a new FOR_STMT.
943 SCOPE and INIT should be the return of begin_for_scope,
944 or both NULL_TREE */
946 tree
947 begin_for_stmt (tree scope, tree init)
949 tree r;
951 r = build_stmt (input_location, FOR_STMT, NULL_TREE, NULL_TREE,
952 NULL_TREE, NULL_TREE, NULL_TREE);
954 if (scope == NULL_TREE)
956 gcc_assert (!init || !(flag_new_for_scope > 0));
957 if (!init)
958 scope = begin_for_scope (&init);
960 FOR_INIT_STMT (r) = init;
961 FOR_SCOPE (r) = scope;
963 return r;
966 /* Finish the init-statement of a for-statement, which may be
967 given by FOR_STMT. */
969 void
970 finish_init_stmt (tree for_stmt)
972 if (processing_template_decl)
973 FOR_INIT_STMT (for_stmt) = pop_stmt_list (FOR_INIT_STMT (for_stmt));
974 add_stmt (for_stmt);
975 FOR_BODY (for_stmt) = do_pushlevel (sk_block);
976 begin_cond (&FOR_COND (for_stmt));
979 /* Finish the COND of a for-statement, which may be given by
980 FOR_STMT. */
982 void
983 finish_for_cond (tree cond, tree for_stmt, bool ivdep)
985 if (check_no_cilk (cond,
986 "Cilk array notation cannot be used in a condition for a for-loop",
987 "%<_Cilk_spawn%> statement cannot be used in a condition for a for-loop"))
988 cond = error_mark_node;
989 cond = maybe_convert_cond (cond);
990 finish_cond (&FOR_COND (for_stmt), cond);
991 begin_maybe_infinite_loop (cond);
992 if (ivdep && cond != error_mark_node)
993 FOR_COND (for_stmt) = build2 (ANNOTATE_EXPR,
994 TREE_TYPE (FOR_COND (for_stmt)),
995 FOR_COND (for_stmt),
996 build_int_cst (integer_type_node,
997 annot_expr_ivdep_kind));
998 simplify_loop_decl_cond (&FOR_COND (for_stmt), FOR_BODY (for_stmt));
1001 /* Finish the increment-EXPRESSION in a for-statement, which may be
1002 given by FOR_STMT. */
1004 void
1005 finish_for_expr (tree expr, tree for_stmt)
1007 if (!expr)
1008 return;
1009 /* If EXPR is an overloaded function, issue an error; there is no
1010 context available to use to perform overload resolution. */
1011 if (type_unknown_p (expr))
1013 cxx_incomplete_type_error (expr, TREE_TYPE (expr));
1014 expr = error_mark_node;
1016 if (!processing_template_decl)
1018 if (warn_sequence_point)
1019 verify_sequence_points (expr);
1020 expr = convert_to_void (expr, ICV_THIRD_IN_FOR,
1021 tf_warning_or_error);
1023 else if (!type_dependent_expression_p (expr))
1024 convert_to_void (build_non_dependent_expr (expr), ICV_THIRD_IN_FOR,
1025 tf_warning_or_error);
1026 expr = maybe_cleanup_point_expr_void (expr);
1027 if (check_for_bare_parameter_packs (expr))
1028 expr = error_mark_node;
1029 FOR_EXPR (for_stmt) = expr;
1032 /* Finish the body of a for-statement, which may be given by
1033 FOR_STMT. The increment-EXPR for the loop must be
1034 provided.
1035 It can also finish RANGE_FOR_STMT. */
1037 void
1038 finish_for_stmt (tree for_stmt)
1040 end_maybe_infinite_loop (boolean_true_node);
1042 if (TREE_CODE (for_stmt) == RANGE_FOR_STMT)
1043 RANGE_FOR_BODY (for_stmt) = do_poplevel (RANGE_FOR_BODY (for_stmt));
1044 else
1045 FOR_BODY (for_stmt) = do_poplevel (FOR_BODY (for_stmt));
1047 /* Pop the scope for the body of the loop. */
1048 if (flag_new_for_scope > 0)
1050 tree scope;
1051 tree *scope_ptr = (TREE_CODE (for_stmt) == RANGE_FOR_STMT
1052 ? &RANGE_FOR_SCOPE (for_stmt)
1053 : &FOR_SCOPE (for_stmt));
1054 scope = *scope_ptr;
1055 *scope_ptr = NULL;
1056 add_stmt (do_poplevel (scope));
1060 /* Begin a range-for-statement. Returns a new RANGE_FOR_STMT.
1061 SCOPE and INIT should be the return of begin_for_scope,
1062 or both NULL_TREE .
1063 To finish it call finish_for_stmt(). */
1065 tree
1066 begin_range_for_stmt (tree scope, tree init)
1068 tree r;
1070 begin_maybe_infinite_loop (boolean_false_node);
1072 r = build_stmt (input_location, RANGE_FOR_STMT,
1073 NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE);
1075 if (scope == NULL_TREE)
1077 gcc_assert (!init || !(flag_new_for_scope > 0));
1078 if (!init)
1079 scope = begin_for_scope (&init);
1082 /* RANGE_FOR_STMTs do not use nor save the init tree, so we
1083 pop it now. */
1084 if (init)
1085 pop_stmt_list (init);
1086 RANGE_FOR_SCOPE (r) = scope;
1088 return r;
1091 /* Finish the head of a range-based for statement, which may
1092 be given by RANGE_FOR_STMT. DECL must be the declaration
1093 and EXPR must be the loop expression. */
1095 void
1096 finish_range_for_decl (tree range_for_stmt, tree decl, tree expr)
1098 RANGE_FOR_DECL (range_for_stmt) = decl;
1099 RANGE_FOR_EXPR (range_for_stmt) = expr;
1100 add_stmt (range_for_stmt);
1101 RANGE_FOR_BODY (range_for_stmt) = do_pushlevel (sk_block);
1104 /* Finish a break-statement. */
1106 tree
1107 finish_break_stmt (void)
1109 /* In switch statements break is sometimes stylistically used after
1110 a return statement. This can lead to spurious warnings about
1111 control reaching the end of a non-void function when it is
1112 inlined. Note that we are calling block_may_fallthru with
1113 language specific tree nodes; this works because
1114 block_may_fallthru returns true when given something it does not
1115 understand. */
1116 if (!block_may_fallthru (cur_stmt_list))
1117 return void_node;
1118 return add_stmt (build_stmt (input_location, BREAK_STMT));
1121 /* Finish a continue-statement. */
1123 tree
1124 finish_continue_stmt (void)
1126 return add_stmt (build_stmt (input_location, CONTINUE_STMT));
1129 /* Begin a switch-statement. Returns a new SWITCH_STMT if
1130 appropriate. */
1132 tree
1133 begin_switch_stmt (void)
1135 tree r, scope;
1137 scope = do_pushlevel (sk_cond);
1138 r = build_stmt (input_location, SWITCH_STMT, NULL_TREE, NULL_TREE, NULL_TREE, scope);
1140 begin_cond (&SWITCH_STMT_COND (r));
1142 return r;
1145 /* Finish the cond of a switch-statement. */
1147 void
1148 finish_switch_cond (tree cond, tree switch_stmt)
1150 tree orig_type = NULL;
1152 if (check_no_cilk (cond,
1153 "Cilk array notation cannot be used as a condition for switch statement",
1154 "%<_Cilk_spawn%> statement cannot be used as a condition for switch statement"))
1155 cond = error_mark_node;
1157 if (!processing_template_decl)
1159 /* Convert the condition to an integer or enumeration type. */
1160 cond = build_expr_type_conversion (WANT_INT | WANT_ENUM, cond, true);
1161 if (cond == NULL_TREE)
1163 error ("switch quantity not an integer");
1164 cond = error_mark_node;
1166 /* We want unlowered type here to handle enum bit-fields. */
1167 orig_type = unlowered_expr_type (cond);
1168 if (TREE_CODE (orig_type) != ENUMERAL_TYPE)
1169 orig_type = TREE_TYPE (cond);
1170 if (cond != error_mark_node)
1172 /* [stmt.switch]
1174 Integral promotions are performed. */
1175 cond = perform_integral_promotions (cond);
1176 cond = maybe_cleanup_point_expr (cond);
1179 if (check_for_bare_parameter_packs (cond))
1180 cond = error_mark_node;
1181 else if (!processing_template_decl && warn_sequence_point)
1182 verify_sequence_points (cond);
1184 finish_cond (&SWITCH_STMT_COND (switch_stmt), cond);
1185 SWITCH_STMT_TYPE (switch_stmt) = orig_type;
1186 add_stmt (switch_stmt);
1187 push_switch (switch_stmt);
1188 SWITCH_STMT_BODY (switch_stmt) = push_stmt_list ();
1191 /* Finish the body of a switch-statement, which may be given by
1192 SWITCH_STMT. The COND to switch on is indicated. */
1194 void
1195 finish_switch_stmt (tree switch_stmt)
1197 tree scope;
1199 SWITCH_STMT_BODY (switch_stmt) =
1200 pop_stmt_list (SWITCH_STMT_BODY (switch_stmt));
1201 pop_switch ();
1203 scope = SWITCH_STMT_SCOPE (switch_stmt);
1204 SWITCH_STMT_SCOPE (switch_stmt) = NULL;
1205 add_stmt (do_poplevel (scope));
1208 /* Begin a try-block. Returns a newly-created TRY_BLOCK if
1209 appropriate. */
1211 tree
1212 begin_try_block (void)
1214 tree r = build_stmt (input_location, TRY_BLOCK, NULL_TREE, NULL_TREE);
1215 add_stmt (r);
1216 TRY_STMTS (r) = push_stmt_list ();
1217 return r;
1220 /* Likewise, for a function-try-block. The block returned in
1221 *COMPOUND_STMT is an artificial outer scope, containing the
1222 function-try-block. */
1224 tree
1225 begin_function_try_block (tree *compound_stmt)
1227 tree r;
1228 /* This outer scope does not exist in the C++ standard, but we need
1229 a place to put __FUNCTION__ and similar variables. */
1230 *compound_stmt = begin_compound_stmt (0);
1231 r = begin_try_block ();
1232 FN_TRY_BLOCK_P (r) = 1;
1233 return r;
1236 /* Finish a try-block, which may be given by TRY_BLOCK. */
1238 void
1239 finish_try_block (tree try_block)
1241 TRY_STMTS (try_block) = pop_stmt_list (TRY_STMTS (try_block));
1242 TRY_HANDLERS (try_block) = push_stmt_list ();
1245 /* Finish the body of a cleanup try-block, which may be given by
1246 TRY_BLOCK. */
1248 void
1249 finish_cleanup_try_block (tree try_block)
1251 TRY_STMTS (try_block) = pop_stmt_list (TRY_STMTS (try_block));
1254 /* Finish an implicitly generated try-block, with a cleanup is given
1255 by CLEANUP. */
1257 void
1258 finish_cleanup (tree cleanup, tree try_block)
1260 TRY_HANDLERS (try_block) = cleanup;
1261 CLEANUP_P (try_block) = 1;
1264 /* Likewise, for a function-try-block. */
1266 void
1267 finish_function_try_block (tree try_block)
1269 finish_try_block (try_block);
1270 /* FIXME : something queer about CTOR_INITIALIZER somehow following
1271 the try block, but moving it inside. */
1272 in_function_try_handler = 1;
1275 /* Finish a handler-sequence for a try-block, which may be given by
1276 TRY_BLOCK. */
1278 void
1279 finish_handler_sequence (tree try_block)
1281 TRY_HANDLERS (try_block) = pop_stmt_list (TRY_HANDLERS (try_block));
1282 check_handlers (TRY_HANDLERS (try_block));
1285 /* Finish the handler-seq for a function-try-block, given by
1286 TRY_BLOCK. COMPOUND_STMT is the outer block created by
1287 begin_function_try_block. */
1289 void
1290 finish_function_handler_sequence (tree try_block, tree compound_stmt)
1292 in_function_try_handler = 0;
1293 finish_handler_sequence (try_block);
1294 finish_compound_stmt (compound_stmt);
1297 /* Begin a handler. Returns a HANDLER if appropriate. */
1299 tree
1300 begin_handler (void)
1302 tree r;
1304 r = build_stmt (input_location, HANDLER, NULL_TREE, NULL_TREE);
1305 add_stmt (r);
1307 /* Create a binding level for the eh_info and the exception object
1308 cleanup. */
1309 HANDLER_BODY (r) = do_pushlevel (sk_catch);
1311 return r;
1314 /* Finish the handler-parameters for a handler, which may be given by
1315 HANDLER. DECL is the declaration for the catch parameter, or NULL
1316 if this is a `catch (...)' clause. */
1318 void
1319 finish_handler_parms (tree decl, tree handler)
1321 tree type = NULL_TREE;
1322 if (processing_template_decl)
1324 if (decl)
1326 decl = pushdecl (decl);
1327 decl = push_template_decl (decl);
1328 HANDLER_PARMS (handler) = decl;
1329 type = TREE_TYPE (decl);
1332 else
1334 type = expand_start_catch_block (decl);
1335 if (warn_catch_value
1336 && type != NULL_TREE
1337 && type != error_mark_node
1338 && TREE_CODE (TREE_TYPE (decl)) != REFERENCE_TYPE)
1340 tree orig_type = TREE_TYPE (decl);
1341 if (CLASS_TYPE_P (orig_type))
1343 if (TYPE_POLYMORPHIC_P (orig_type))
1344 warning (OPT_Wcatch_value_,
1345 "catching polymorphic type %q#T by value", orig_type);
1346 else if (warn_catch_value > 1)
1347 warning (OPT_Wcatch_value_,
1348 "catching type %q#T by value", orig_type);
1350 else if (warn_catch_value > 2)
1351 warning (OPT_Wcatch_value_,
1352 "catching non-reference type %q#T", orig_type);
1355 HANDLER_TYPE (handler) = type;
1358 /* Finish a handler, which may be given by HANDLER. The BLOCKs are
1359 the return value from the matching call to finish_handler_parms. */
1361 void
1362 finish_handler (tree handler)
1364 if (!processing_template_decl)
1365 expand_end_catch_block ();
1366 HANDLER_BODY (handler) = do_poplevel (HANDLER_BODY (handler));
1369 /* Begin a compound statement. FLAGS contains some bits that control the
1370 behavior and context. If BCS_NO_SCOPE is set, the compound statement
1371 does not define a scope. If BCS_FN_BODY is set, this is the outermost
1372 block of a function. If BCS_TRY_BLOCK is set, this is the block
1373 created on behalf of a TRY statement. Returns a token to be passed to
1374 finish_compound_stmt. */
1376 tree
1377 begin_compound_stmt (unsigned int flags)
1379 tree r;
1381 if (flags & BCS_NO_SCOPE)
1383 r = push_stmt_list ();
1384 STATEMENT_LIST_NO_SCOPE (r) = 1;
1386 /* Normally, we try hard to keep the BLOCK for a statement-expression.
1387 But, if it's a statement-expression with a scopeless block, there's
1388 nothing to keep, and we don't want to accidentally keep a block
1389 *inside* the scopeless block. */
1390 keep_next_level (false);
1392 else
1394 scope_kind sk = sk_block;
1395 if (flags & BCS_TRY_BLOCK)
1396 sk = sk_try;
1397 else if (flags & BCS_TRANSACTION)
1398 sk = sk_transaction;
1399 r = do_pushlevel (sk);
1402 /* When processing a template, we need to remember where the braces were,
1403 so that we can set up identical scopes when instantiating the template
1404 later. BIND_EXPR is a handy candidate for this.
1405 Note that do_poplevel won't create a BIND_EXPR itself here (and thus
1406 result in nested BIND_EXPRs), since we don't build BLOCK nodes when
1407 processing templates. */
1408 if (processing_template_decl)
1410 r = build3 (BIND_EXPR, NULL, NULL, r, NULL);
1411 BIND_EXPR_TRY_BLOCK (r) = (flags & BCS_TRY_BLOCK) != 0;
1412 BIND_EXPR_BODY_BLOCK (r) = (flags & BCS_FN_BODY) != 0;
1413 TREE_SIDE_EFFECTS (r) = 1;
1416 return r;
1419 /* Finish a compound-statement, which is given by STMT. */
1421 void
1422 finish_compound_stmt (tree stmt)
1424 if (TREE_CODE (stmt) == BIND_EXPR)
1426 tree body = do_poplevel (BIND_EXPR_BODY (stmt));
1427 /* If the STATEMENT_LIST is empty and this BIND_EXPR isn't special,
1428 discard the BIND_EXPR so it can be merged with the containing
1429 STATEMENT_LIST. */
1430 if (TREE_CODE (body) == STATEMENT_LIST
1431 && STATEMENT_LIST_HEAD (body) == NULL
1432 && !BIND_EXPR_BODY_BLOCK (stmt)
1433 && !BIND_EXPR_TRY_BLOCK (stmt))
1434 stmt = body;
1435 else
1436 BIND_EXPR_BODY (stmt) = body;
1438 else if (STATEMENT_LIST_NO_SCOPE (stmt))
1439 stmt = pop_stmt_list (stmt);
1440 else
1442 /* Destroy any ObjC "super" receivers that may have been
1443 created. */
1444 objc_clear_super_receiver ();
1446 stmt = do_poplevel (stmt);
1449 /* ??? See c_end_compound_stmt wrt statement expressions. */
1450 add_stmt (stmt);
1453 /* Finish an asm-statement, whose components are a STRING, some
1454 OUTPUT_OPERANDS, some INPUT_OPERANDS, some CLOBBERS and some
1455 LABELS. Also note whether the asm-statement should be
1456 considered volatile. */
1458 tree
1459 finish_asm_stmt (int volatile_p, tree string, tree output_operands,
1460 tree input_operands, tree clobbers, tree labels)
1462 tree r;
1463 tree t;
1464 int ninputs = list_length (input_operands);
1465 int noutputs = list_length (output_operands);
1467 if (!processing_template_decl)
1469 const char *constraint;
1470 const char **oconstraints;
1471 bool allows_mem, allows_reg, is_inout;
1472 tree operand;
1473 int i;
1475 oconstraints = XALLOCAVEC (const char *, noutputs);
1477 string = resolve_asm_operand_names (string, output_operands,
1478 input_operands, labels);
1480 for (i = 0, t = output_operands; t; t = TREE_CHAIN (t), ++i)
1482 operand = TREE_VALUE (t);
1484 /* ??? Really, this should not be here. Users should be using a
1485 proper lvalue, dammit. But there's a long history of using
1486 casts in the output operands. In cases like longlong.h, this
1487 becomes a primitive form of typechecking -- if the cast can be
1488 removed, then the output operand had a type of the proper width;
1489 otherwise we'll get an error. Gross, but ... */
1490 STRIP_NOPS (operand);
1492 operand = mark_lvalue_use (operand);
1494 if (!lvalue_or_else (operand, lv_asm, tf_warning_or_error))
1495 operand = error_mark_node;
1497 if (operand != error_mark_node
1498 && (TREE_READONLY (operand)
1499 || CP_TYPE_CONST_P (TREE_TYPE (operand))
1500 /* Functions are not modifiable, even though they are
1501 lvalues. */
1502 || TREE_CODE (TREE_TYPE (operand)) == FUNCTION_TYPE
1503 || TREE_CODE (TREE_TYPE (operand)) == METHOD_TYPE
1504 /* If it's an aggregate and any field is const, then it is
1505 effectively const. */
1506 || (CLASS_TYPE_P (TREE_TYPE (operand))
1507 && C_TYPE_FIELDS_READONLY (TREE_TYPE (operand)))))
1508 cxx_readonly_error (operand, lv_asm);
1510 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
1511 oconstraints[i] = constraint;
1513 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
1514 &allows_mem, &allows_reg, &is_inout))
1516 /* If the operand is going to end up in memory,
1517 mark it addressable. */
1518 if (!allows_reg && !cxx_mark_addressable (operand))
1519 operand = error_mark_node;
1521 else
1522 operand = error_mark_node;
1524 TREE_VALUE (t) = operand;
1527 for (i = 0, t = input_operands; t; ++i, t = TREE_CHAIN (t))
1529 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
1530 bool constraint_parsed
1531 = parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
1532 oconstraints, &allows_mem, &allows_reg);
1533 /* If the operand is going to end up in memory, don't call
1534 decay_conversion. */
1535 if (constraint_parsed && !allows_reg && allows_mem)
1536 operand = mark_lvalue_use (TREE_VALUE (t));
1537 else
1538 operand = decay_conversion (TREE_VALUE (t), tf_warning_or_error);
1540 /* If the type of the operand hasn't been determined (e.g.,
1541 because it involves an overloaded function), then issue
1542 an error message. There's no context available to
1543 resolve the overloading. */
1544 if (TREE_TYPE (operand) == unknown_type_node)
1546 error ("type of asm operand %qE could not be determined",
1547 TREE_VALUE (t));
1548 operand = error_mark_node;
1551 if (constraint_parsed)
1553 /* If the operand is going to end up in memory,
1554 mark it addressable. */
1555 if (!allows_reg && allows_mem)
1557 /* Strip the nops as we allow this case. FIXME, this really
1558 should be rejected or made deprecated. */
1559 STRIP_NOPS (operand);
1560 if (!cxx_mark_addressable (operand))
1561 operand = error_mark_node;
1563 else if (!allows_reg && !allows_mem)
1565 /* If constraint allows neither register nor memory,
1566 try harder to get a constant. */
1567 tree constop = maybe_constant_value (operand);
1568 if (TREE_CONSTANT (constop))
1569 operand = constop;
1572 else
1573 operand = error_mark_node;
1575 TREE_VALUE (t) = operand;
1579 r = build_stmt (input_location, ASM_EXPR, string,
1580 output_operands, input_operands,
1581 clobbers, labels);
1582 ASM_VOLATILE_P (r) = volatile_p || noutputs == 0;
1583 r = maybe_cleanup_point_expr_void (r);
1584 return add_stmt (r);
1587 /* Finish a label with the indicated NAME. Returns the new label. */
1589 tree
1590 finish_label_stmt (tree name)
1592 tree decl = define_label (input_location, name);
1594 if (decl == error_mark_node)
1595 return error_mark_node;
1597 add_stmt (build_stmt (input_location, LABEL_EXPR, decl));
1599 return decl;
1602 /* Finish a series of declarations for local labels. G++ allows users
1603 to declare "local" labels, i.e., labels with scope. This extension
1604 is useful when writing code involving statement-expressions. */
1606 void
1607 finish_label_decl (tree name)
1609 if (!at_function_scope_p ())
1611 error ("__label__ declarations are only allowed in function scopes");
1612 return;
1615 add_decl_expr (declare_local_label (name));
1618 /* When DECL goes out of scope, make sure that CLEANUP is executed. */
1620 void
1621 finish_decl_cleanup (tree decl, tree cleanup)
1623 push_cleanup (decl, cleanup, false);
1626 /* If the current scope exits with an exception, run CLEANUP. */
1628 void
1629 finish_eh_cleanup (tree cleanup)
1631 push_cleanup (NULL, cleanup, true);
1634 /* The MEM_INITS is a list of mem-initializers, in reverse of the
1635 order they were written by the user. Each node is as for
1636 emit_mem_initializers. */
1638 void
1639 finish_mem_initializers (tree mem_inits)
1641 /* Reorder the MEM_INITS so that they are in the order they appeared
1642 in the source program. */
1643 mem_inits = nreverse (mem_inits);
1645 if (processing_template_decl)
1647 tree mem;
1649 for (mem = mem_inits; mem; mem = TREE_CHAIN (mem))
1651 /* If the TREE_PURPOSE is a TYPE_PACK_EXPANSION, skip the
1652 check for bare parameter packs in the TREE_VALUE, because
1653 any parameter packs in the TREE_VALUE have already been
1654 bound as part of the TREE_PURPOSE. See
1655 make_pack_expansion for more information. */
1656 if (TREE_CODE (TREE_PURPOSE (mem)) != TYPE_PACK_EXPANSION
1657 && check_for_bare_parameter_packs (TREE_VALUE (mem)))
1658 TREE_VALUE (mem) = error_mark_node;
1661 add_stmt (build_min_nt_loc (UNKNOWN_LOCATION,
1662 CTOR_INITIALIZER, mem_inits));
1664 else
1665 emit_mem_initializers (mem_inits);
1668 /* Obfuscate EXPR if it looks like an id-expression or member access so
1669 that the call to finish_decltype in do_auto_deduction will give the
1670 right result. */
1672 tree
1673 force_paren_expr (tree expr)
1675 /* This is only needed for decltype(auto) in C++14. */
1676 if (cxx_dialect < cxx14)
1677 return expr;
1679 /* If we're in unevaluated context, we can't be deducing a
1680 return/initializer type, so we don't need to mess with this. */
1681 if (cp_unevaluated_operand)
1682 return expr;
1684 if (!DECL_P (expr) && TREE_CODE (expr) != COMPONENT_REF
1685 && TREE_CODE (expr) != SCOPE_REF)
1686 return expr;
1688 if (TREE_CODE (expr) == COMPONENT_REF
1689 || TREE_CODE (expr) == SCOPE_REF)
1690 REF_PARENTHESIZED_P (expr) = true;
1691 else if (type_dependent_expression_p (expr))
1692 expr = build1 (PAREN_EXPR, TREE_TYPE (expr), expr);
1693 else if (VAR_P (expr) && DECL_HARD_REGISTER (expr))
1694 /* We can't bind a hard register variable to a reference. */;
1695 else
1697 cp_lvalue_kind kind = lvalue_kind (expr);
1698 if ((kind & ~clk_class) != clk_none)
1700 tree type = unlowered_expr_type (expr);
1701 bool rval = !!(kind & clk_rvalueref);
1702 type = cp_build_reference_type (type, rval);
1703 /* This inhibits warnings in, eg, cxx_mark_addressable
1704 (c++/60955). */
1705 warning_sentinel s (extra_warnings);
1706 expr = build_static_cast (type, expr, tf_error);
1707 if (expr != error_mark_node)
1708 REF_PARENTHESIZED_P (expr) = true;
1712 return expr;
1715 /* If T is an id-expression obfuscated by force_paren_expr, undo the
1716 obfuscation and return the underlying id-expression. Otherwise
1717 return T. */
1719 tree
1720 maybe_undo_parenthesized_ref (tree t)
1722 if (cxx_dialect >= cxx14
1723 && INDIRECT_REF_P (t)
1724 && REF_PARENTHESIZED_P (t))
1726 t = TREE_OPERAND (t, 0);
1727 while (TREE_CODE (t) == NON_LVALUE_EXPR
1728 || TREE_CODE (t) == NOP_EXPR)
1729 t = TREE_OPERAND (t, 0);
1731 gcc_assert (TREE_CODE (t) == ADDR_EXPR
1732 || TREE_CODE (t) == STATIC_CAST_EXPR);
1733 t = TREE_OPERAND (t, 0);
1736 return t;
1739 /* Finish a parenthesized expression EXPR. */
1741 cp_expr
1742 finish_parenthesized_expr (cp_expr expr)
1744 if (EXPR_P (expr))
1745 /* This inhibits warnings in c_common_truthvalue_conversion. */
1746 TREE_NO_WARNING (expr) = 1;
1748 if (TREE_CODE (expr) == OFFSET_REF
1749 || TREE_CODE (expr) == SCOPE_REF)
1750 /* [expr.unary.op]/3 The qualified id of a pointer-to-member must not be
1751 enclosed in parentheses. */
1752 PTRMEM_OK_P (expr) = 0;
1754 if (TREE_CODE (expr) == STRING_CST)
1755 PAREN_STRING_LITERAL_P (expr) = 1;
1757 expr = cp_expr (force_paren_expr (expr), expr.get_location ());
1759 return expr;
1762 /* Finish a reference to a non-static data member (DECL) that is not
1763 preceded by `.' or `->'. */
1765 tree
1766 finish_non_static_data_member (tree decl, tree object, tree qualifying_scope)
1768 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
1769 bool try_omp_private = !object && omp_private_member_map;
1770 tree ret;
1772 if (!object)
1774 tree scope = qualifying_scope;
1775 if (scope == NULL_TREE)
1776 scope = context_for_name_lookup (decl);
1777 object = maybe_dummy_object (scope, NULL);
1780 object = maybe_resolve_dummy (object, true);
1781 if (object == error_mark_node)
1782 return error_mark_node;
1784 /* DR 613/850: Can use non-static data members without an associated
1785 object in sizeof/decltype/alignof. */
1786 if (is_dummy_object (object) && cp_unevaluated_operand == 0
1787 && (!processing_template_decl || !current_class_ref))
1789 if (current_function_decl
1790 && DECL_STATIC_FUNCTION_P (current_function_decl))
1791 error ("invalid use of member %qD in static member function", decl);
1792 else
1793 error ("invalid use of non-static data member %qD", decl);
1794 inform (DECL_SOURCE_LOCATION (decl), "declared here");
1796 return error_mark_node;
1799 if (current_class_ptr)
1800 TREE_USED (current_class_ptr) = 1;
1801 if (processing_template_decl && !qualifying_scope)
1803 tree type = TREE_TYPE (decl);
1805 if (TREE_CODE (type) == REFERENCE_TYPE)
1806 /* Quals on the object don't matter. */;
1807 else if (PACK_EXPANSION_P (type))
1808 /* Don't bother trying to represent this. */
1809 type = NULL_TREE;
1810 else
1812 /* Set the cv qualifiers. */
1813 int quals = cp_type_quals (TREE_TYPE (object));
1815 if (DECL_MUTABLE_P (decl))
1816 quals &= ~TYPE_QUAL_CONST;
1818 quals |= cp_type_quals (TREE_TYPE (decl));
1819 type = cp_build_qualified_type (type, quals);
1822 ret = (convert_from_reference
1823 (build_min (COMPONENT_REF, type, object, decl, NULL_TREE)));
1825 /* If PROCESSING_TEMPLATE_DECL is nonzero here, then
1826 QUALIFYING_SCOPE is also non-null. Wrap this in a SCOPE_REF
1827 for now. */
1828 else if (processing_template_decl)
1829 ret = build_qualified_name (TREE_TYPE (decl),
1830 qualifying_scope,
1831 decl,
1832 /*template_p=*/false);
1833 else
1835 tree access_type = TREE_TYPE (object);
1837 perform_or_defer_access_check (TYPE_BINFO (access_type), decl,
1838 decl, tf_warning_or_error);
1840 /* If the data member was named `C::M', convert `*this' to `C'
1841 first. */
1842 if (qualifying_scope)
1844 tree binfo = NULL_TREE;
1845 object = build_scoped_ref (object, qualifying_scope,
1846 &binfo);
1849 ret = build_class_member_access_expr (object, decl,
1850 /*access_path=*/NULL_TREE,
1851 /*preserve_reference=*/false,
1852 tf_warning_or_error);
1854 if (try_omp_private)
1856 tree *v = omp_private_member_map->get (decl);
1857 if (v)
1858 ret = convert_from_reference (*v);
1860 return ret;
1863 /* If we are currently parsing a template and we encountered a typedef
1864 TYPEDEF_DECL that is being accessed though CONTEXT, this function
1865 adds the typedef to a list tied to the current template.
1866 At template instantiation time, that list is walked and access check
1867 performed for each typedef.
1868 LOCATION is the location of the usage point of TYPEDEF_DECL. */
1870 void
1871 add_typedef_to_current_template_for_access_check (tree typedef_decl,
1872 tree context,
1873 location_t location)
1875 tree template_info = NULL;
1876 tree cs = current_scope ();
1878 if (!is_typedef_decl (typedef_decl)
1879 || !context
1880 || !CLASS_TYPE_P (context)
1881 || !cs)
1882 return;
1884 if (CLASS_TYPE_P (cs) || TREE_CODE (cs) == FUNCTION_DECL)
1885 template_info = get_template_info (cs);
1887 if (template_info
1888 && TI_TEMPLATE (template_info)
1889 && !currently_open_class (context))
1890 append_type_to_template_for_access_check (cs, typedef_decl,
1891 context, location);
1894 /* DECL was the declaration to which a qualified-id resolved. Issue
1895 an error message if it is not accessible. If OBJECT_TYPE is
1896 non-NULL, we have just seen `x->' or `x.' and OBJECT_TYPE is the
1897 type of `*x', or `x', respectively. If the DECL was named as
1898 `A::B' then NESTED_NAME_SPECIFIER is `A'. */
1900 void
1901 check_accessibility_of_qualified_id (tree decl,
1902 tree object_type,
1903 tree nested_name_specifier)
1905 tree scope;
1906 tree qualifying_type = NULL_TREE;
1908 /* If we are parsing a template declaration and if decl is a typedef,
1909 add it to a list tied to the template.
1910 At template instantiation time, that list will be walked and
1911 access check performed. */
1912 add_typedef_to_current_template_for_access_check (decl,
1913 nested_name_specifier
1914 ? nested_name_specifier
1915 : DECL_CONTEXT (decl),
1916 input_location);
1918 /* If we're not checking, return immediately. */
1919 if (deferred_access_no_check)
1920 return;
1922 /* Determine the SCOPE of DECL. */
1923 scope = context_for_name_lookup (decl);
1924 /* If the SCOPE is not a type, then DECL is not a member. */
1925 if (!TYPE_P (scope))
1926 return;
1927 /* Compute the scope through which DECL is being accessed. */
1928 if (object_type
1929 /* OBJECT_TYPE might not be a class type; consider:
1931 class A { typedef int I; };
1932 I *p;
1933 p->A::I::~I();
1935 In this case, we will have "A::I" as the DECL, but "I" as the
1936 OBJECT_TYPE. */
1937 && CLASS_TYPE_P (object_type)
1938 && DERIVED_FROM_P (scope, object_type))
1939 /* If we are processing a `->' or `.' expression, use the type of the
1940 left-hand side. */
1941 qualifying_type = object_type;
1942 else if (nested_name_specifier)
1944 /* If the reference is to a non-static member of the
1945 current class, treat it as if it were referenced through
1946 `this'. */
1947 tree ct;
1948 if (DECL_NONSTATIC_MEMBER_P (decl)
1949 && current_class_ptr
1950 && DERIVED_FROM_P (scope, ct = current_nonlambda_class_type ()))
1951 qualifying_type = ct;
1952 /* Otherwise, use the type indicated by the
1953 nested-name-specifier. */
1954 else
1955 qualifying_type = nested_name_specifier;
1957 else
1958 /* Otherwise, the name must be from the current class or one of
1959 its bases. */
1960 qualifying_type = currently_open_derived_class (scope);
1962 if (qualifying_type
1963 /* It is possible for qualifying type to be a TEMPLATE_TYPE_PARM
1964 or similar in a default argument value. */
1965 && CLASS_TYPE_P (qualifying_type)
1966 && !dependent_type_p (qualifying_type))
1967 perform_or_defer_access_check (TYPE_BINFO (qualifying_type), decl,
1968 decl, tf_warning_or_error);
1971 /* EXPR is the result of a qualified-id. The QUALIFYING_CLASS was the
1972 class named to the left of the "::" operator. DONE is true if this
1973 expression is a complete postfix-expression; it is false if this
1974 expression is followed by '->', '[', '(', etc. ADDRESS_P is true
1975 iff this expression is the operand of '&'. TEMPLATE_P is true iff
1976 the qualified-id was of the form "A::template B". TEMPLATE_ARG_P
1977 is true iff this qualified name appears as a template argument. */
1979 tree
1980 finish_qualified_id_expr (tree qualifying_class,
1981 tree expr,
1982 bool done,
1983 bool address_p,
1984 bool template_p,
1985 bool template_arg_p,
1986 tsubst_flags_t complain)
1988 gcc_assert (TYPE_P (qualifying_class));
1990 if (error_operand_p (expr))
1991 return error_mark_node;
1993 if ((DECL_P (expr) || BASELINK_P (expr))
1994 && !mark_used (expr, complain))
1995 return error_mark_node;
1997 if (template_p)
1999 if (TREE_CODE (expr) == UNBOUND_CLASS_TEMPLATE)
2000 /* cp_parser_lookup_name thought we were looking for a type,
2001 but we're actually looking for a declaration. */
2002 expr = build_qualified_name (/*type*/NULL_TREE,
2003 TYPE_CONTEXT (expr),
2004 TYPE_IDENTIFIER (expr),
2005 /*template_p*/true);
2006 else
2007 check_template_keyword (expr);
2010 /* If EXPR occurs as the operand of '&', use special handling that
2011 permits a pointer-to-member. */
2012 if (address_p && done)
2014 if (TREE_CODE (expr) == SCOPE_REF)
2015 expr = TREE_OPERAND (expr, 1);
2016 expr = build_offset_ref (qualifying_class, expr,
2017 /*address_p=*/true, complain);
2018 return expr;
2021 /* No need to check access within an enum. */
2022 if (TREE_CODE (qualifying_class) == ENUMERAL_TYPE)
2023 return expr;
2025 /* Within the scope of a class, turn references to non-static
2026 members into expression of the form "this->...". */
2027 if (template_arg_p)
2028 /* But, within a template argument, we do not want make the
2029 transformation, as there is no "this" pointer. */
2031 else if (TREE_CODE (expr) == FIELD_DECL)
2033 push_deferring_access_checks (dk_no_check);
2034 expr = finish_non_static_data_member (expr, NULL_TREE,
2035 qualifying_class);
2036 pop_deferring_access_checks ();
2038 else if (BASELINK_P (expr))
2040 /* See if any of the functions are non-static members. */
2041 /* If so, the expression may be relative to 'this'. */
2042 if (!shared_member_p (expr)
2043 && current_class_ptr
2044 && DERIVED_FROM_P (qualifying_class,
2045 current_nonlambda_class_type ()))
2046 expr = (build_class_member_access_expr
2047 (maybe_dummy_object (qualifying_class, NULL),
2048 expr,
2049 BASELINK_ACCESS_BINFO (expr),
2050 /*preserve_reference=*/false,
2051 complain));
2052 else if (done)
2053 /* The expression is a qualified name whose address is not
2054 being taken. */
2055 expr = build_offset_ref (qualifying_class, expr, /*address_p=*/false,
2056 complain);
2058 else
2060 /* In a template, return a SCOPE_REF for most qualified-ids
2061 so that we can check access at instantiation time. But if
2062 we're looking at a member of the current instantiation, we
2063 know we have access and building up the SCOPE_REF confuses
2064 non-type template argument handling. */
2065 if (processing_template_decl
2066 && (!currently_open_class (qualifying_class)
2067 || TREE_CODE (expr) == BIT_NOT_EXPR))
2068 expr = build_qualified_name (TREE_TYPE (expr),
2069 qualifying_class, expr,
2070 template_p);
2072 expr = convert_from_reference (expr);
2075 return expr;
2078 /* Begin a statement-expression. The value returned must be passed to
2079 finish_stmt_expr. */
2081 tree
2082 begin_stmt_expr (void)
2084 return push_stmt_list ();
2087 /* Process the final expression of a statement expression. EXPR can be
2088 NULL, if the final expression is empty. Return a STATEMENT_LIST
2089 containing all the statements in the statement-expression, or
2090 ERROR_MARK_NODE if there was an error. */
2092 tree
2093 finish_stmt_expr_expr (tree expr, tree stmt_expr)
2095 if (error_operand_p (expr))
2097 /* The type of the statement-expression is the type of the last
2098 expression. */
2099 TREE_TYPE (stmt_expr) = error_mark_node;
2100 return error_mark_node;
2103 /* If the last statement does not have "void" type, then the value
2104 of the last statement is the value of the entire expression. */
2105 if (expr)
2107 tree type = TREE_TYPE (expr);
2109 if (processing_template_decl)
2111 expr = build_stmt (input_location, EXPR_STMT, expr);
2112 expr = add_stmt (expr);
2113 /* Mark the last statement so that we can recognize it as such at
2114 template-instantiation time. */
2115 EXPR_STMT_STMT_EXPR_RESULT (expr) = 1;
2117 else if (VOID_TYPE_P (type))
2119 /* Just treat this like an ordinary statement. */
2120 expr = finish_expr_stmt (expr);
2122 else
2124 /* It actually has a value we need to deal with. First, force it
2125 to be an rvalue so that we won't need to build up a copy
2126 constructor call later when we try to assign it to something. */
2127 expr = force_rvalue (expr, tf_warning_or_error);
2128 if (error_operand_p (expr))
2129 return error_mark_node;
2131 /* Update for array-to-pointer decay. */
2132 type = TREE_TYPE (expr);
2134 /* Wrap it in a CLEANUP_POINT_EXPR and add it to the list like a
2135 normal statement, but don't convert to void or actually add
2136 the EXPR_STMT. */
2137 if (TREE_CODE (expr) != CLEANUP_POINT_EXPR)
2138 expr = maybe_cleanup_point_expr (expr);
2139 add_stmt (expr);
2142 /* The type of the statement-expression is the type of the last
2143 expression. */
2144 TREE_TYPE (stmt_expr) = type;
2147 return stmt_expr;
2150 /* Finish a statement-expression. EXPR should be the value returned
2151 by the previous begin_stmt_expr. Returns an expression
2152 representing the statement-expression. */
2154 tree
2155 finish_stmt_expr (tree stmt_expr, bool has_no_scope)
2157 tree type;
2158 tree result;
2160 if (error_operand_p (stmt_expr))
2162 pop_stmt_list (stmt_expr);
2163 return error_mark_node;
2166 gcc_assert (TREE_CODE (stmt_expr) == STATEMENT_LIST);
2168 type = TREE_TYPE (stmt_expr);
2169 result = pop_stmt_list (stmt_expr);
2170 TREE_TYPE (result) = type;
2172 if (processing_template_decl)
2174 result = build_min (STMT_EXPR, type, result);
2175 TREE_SIDE_EFFECTS (result) = 1;
2176 STMT_EXPR_NO_SCOPE (result) = has_no_scope;
2178 else if (CLASS_TYPE_P (type))
2180 /* Wrap the statement-expression in a TARGET_EXPR so that the
2181 temporary object created by the final expression is destroyed at
2182 the end of the full-expression containing the
2183 statement-expression. */
2184 result = force_target_expr (type, result, tf_warning_or_error);
2187 return result;
2190 /* Returns the expression which provides the value of STMT_EXPR. */
2192 tree
2193 stmt_expr_value_expr (tree stmt_expr)
2195 tree t = STMT_EXPR_STMT (stmt_expr);
2197 if (TREE_CODE (t) == BIND_EXPR)
2198 t = BIND_EXPR_BODY (t);
2200 if (TREE_CODE (t) == STATEMENT_LIST && STATEMENT_LIST_TAIL (t))
2201 t = STATEMENT_LIST_TAIL (t)->stmt;
2203 if (TREE_CODE (t) == EXPR_STMT)
2204 t = EXPR_STMT_EXPR (t);
2206 return t;
2209 /* Return TRUE iff EXPR_STMT is an empty list of
2210 expression statements. */
2212 bool
2213 empty_expr_stmt_p (tree expr_stmt)
2215 tree body = NULL_TREE;
2217 if (expr_stmt == void_node)
2218 return true;
2220 if (expr_stmt)
2222 if (TREE_CODE (expr_stmt) == EXPR_STMT)
2223 body = EXPR_STMT_EXPR (expr_stmt);
2224 else if (TREE_CODE (expr_stmt) == STATEMENT_LIST)
2225 body = expr_stmt;
2228 if (body)
2230 if (TREE_CODE (body) == STATEMENT_LIST)
2231 return tsi_end_p (tsi_start (body));
2232 else
2233 return empty_expr_stmt_p (body);
2235 return false;
2238 /* Perform Koenig lookup. FN is the postfix-expression representing
2239 the function (or functions) to call; ARGS are the arguments to the
2240 call. Returns the functions to be considered by overload resolution. */
2242 cp_expr
2243 perform_koenig_lookup (cp_expr fn, vec<tree, va_gc> *args,
2244 tsubst_flags_t complain)
2246 tree identifier = NULL_TREE;
2247 tree functions = NULL_TREE;
2248 tree tmpl_args = NULL_TREE;
2249 bool template_id = false;
2250 location_t loc = fn.get_location ();
2252 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
2254 /* Use a separate flag to handle null args. */
2255 template_id = true;
2256 tmpl_args = TREE_OPERAND (fn, 1);
2257 fn = TREE_OPERAND (fn, 0);
2260 /* Find the name of the overloaded function. */
2261 if (identifier_p (fn))
2262 identifier = fn;
2263 else
2265 functions = fn;
2266 identifier = OVL_NAME (functions);
2269 /* A call to a namespace-scope function using an unqualified name.
2271 Do Koenig lookup -- unless any of the arguments are
2272 type-dependent. */
2273 if (!any_type_dependent_arguments_p (args)
2274 && !any_dependent_template_arguments_p (tmpl_args))
2276 fn = lookup_arg_dependent (identifier, functions, args);
2277 if (!fn)
2279 /* The unqualified name could not be resolved. */
2280 if (complain & tf_error)
2281 fn = unqualified_fn_lookup_error (cp_expr (identifier, loc));
2282 else
2283 fn = identifier;
2287 if (fn && template_id && fn != error_mark_node)
2288 fn = build2 (TEMPLATE_ID_EXPR, unknown_type_node, fn, tmpl_args);
2290 return fn;
2293 /* Generate an expression for `FN (ARGS)'. This may change the
2294 contents of ARGS.
2296 If DISALLOW_VIRTUAL is true, the call to FN will be not generated
2297 as a virtual call, even if FN is virtual. (This flag is set when
2298 encountering an expression where the function name is explicitly
2299 qualified. For example a call to `X::f' never generates a virtual
2300 call.)
2302 Returns code for the call. */
2304 tree
2305 finish_call_expr (tree fn, vec<tree, va_gc> **args, bool disallow_virtual,
2306 bool koenig_p, tsubst_flags_t complain)
2308 tree result;
2309 tree orig_fn;
2310 vec<tree, va_gc> *orig_args = NULL;
2312 if (fn == error_mark_node)
2313 return error_mark_node;
2315 gcc_assert (!TYPE_P (fn));
2317 /* If FN may be a FUNCTION_DECL obfuscated by force_paren_expr, undo
2318 it so that we can tell this is a call to a known function. */
2319 fn = maybe_undo_parenthesized_ref (fn);
2321 orig_fn = fn;
2323 if (processing_template_decl)
2325 /* If FN is a local extern declaration or set thereof, look them up
2326 again at instantiation time. */
2327 if (is_overloaded_fn (fn))
2329 tree ifn = get_first_fn (fn);
2330 if (TREE_CODE (ifn) == FUNCTION_DECL
2331 && DECL_LOCAL_FUNCTION_P (ifn))
2332 orig_fn = DECL_NAME (ifn);
2335 /* If the call expression is dependent, build a CALL_EXPR node
2336 with no type; type_dependent_expression_p recognizes
2337 expressions with no type as being dependent. */
2338 if (type_dependent_expression_p (fn)
2339 || any_type_dependent_arguments_p (*args))
2341 result = build_min_nt_call_vec (orig_fn, *args);
2342 SET_EXPR_LOCATION (result, EXPR_LOC_OR_LOC (fn, input_location));
2343 KOENIG_LOOKUP_P (result) = koenig_p;
2344 if (is_overloaded_fn (fn))
2346 fn = get_fns (fn);
2347 lookup_keep (fn, true);
2350 if (cfun)
2352 bool abnormal = true;
2353 for (lkp_iterator iter (fn); abnormal && iter; ++iter)
2355 tree fndecl = *iter;
2356 if (TREE_CODE (fndecl) != FUNCTION_DECL
2357 || !TREE_THIS_VOLATILE (fndecl))
2358 abnormal = false;
2360 /* FIXME: Stop warning about falling off end of non-void
2361 function. But this is wrong. Even if we only see
2362 no-return fns at this point, we could select a
2363 future-defined return fn during instantiation. Or
2364 vice-versa. */
2365 if (abnormal)
2366 current_function_returns_abnormally = 1;
2368 return result;
2370 orig_args = make_tree_vector_copy (*args);
2371 if (!BASELINK_P (fn)
2372 && TREE_CODE (fn) != PSEUDO_DTOR_EXPR
2373 && TREE_TYPE (fn) != unknown_type_node)
2374 fn = build_non_dependent_expr (fn);
2375 make_args_non_dependent (*args);
2378 if (TREE_CODE (fn) == COMPONENT_REF)
2380 tree member = TREE_OPERAND (fn, 1);
2381 if (BASELINK_P (member))
2383 tree object = TREE_OPERAND (fn, 0);
2384 return build_new_method_call (object, member,
2385 args, NULL_TREE,
2386 (disallow_virtual
2387 ? LOOKUP_NORMAL | LOOKUP_NONVIRTUAL
2388 : LOOKUP_NORMAL),
2389 /*fn_p=*/NULL,
2390 complain);
2394 /* Per 13.3.1.1, '(&f)(...)' is the same as '(f)(...)'. */
2395 if (TREE_CODE (fn) == ADDR_EXPR
2396 && TREE_CODE (TREE_OPERAND (fn, 0)) == OVERLOAD)
2397 fn = TREE_OPERAND (fn, 0);
2399 if (is_overloaded_fn (fn))
2400 fn = baselink_for_fns (fn);
2402 result = NULL_TREE;
2403 if (BASELINK_P (fn))
2405 tree object;
2407 /* A call to a member function. From [over.call.func]:
2409 If the keyword this is in scope and refers to the class of
2410 that member function, or a derived class thereof, then the
2411 function call is transformed into a qualified function call
2412 using (*this) as the postfix-expression to the left of the
2413 . operator.... [Otherwise] a contrived object of type T
2414 becomes the implied object argument.
2416 In this situation:
2418 struct A { void f(); };
2419 struct B : public A {};
2420 struct C : public A { void g() { B::f(); }};
2422 "the class of that member function" refers to `A'. But 11.2
2423 [class.access.base] says that we need to convert 'this' to B* as
2424 part of the access, so we pass 'B' to maybe_dummy_object. */
2426 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (get_first_fn (fn)))
2428 /* A constructor call always uses a dummy object. (This constructor
2429 call which has the form A::A () is actually invalid and we are
2430 going to reject it later in build_new_method_call.) */
2431 object = build_dummy_object (BINFO_TYPE (BASELINK_ACCESS_BINFO (fn)));
2433 else
2434 object = maybe_dummy_object (BINFO_TYPE (BASELINK_ACCESS_BINFO (fn)),
2435 NULL);
2437 result = build_new_method_call (object, fn, args, NULL_TREE,
2438 (disallow_virtual
2439 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
2440 : LOOKUP_NORMAL),
2441 /*fn_p=*/NULL,
2442 complain);
2444 else if (is_overloaded_fn (fn))
2446 /* If the function is an overloaded builtin, resolve it. */
2447 if (TREE_CODE (fn) == FUNCTION_DECL
2448 && (DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL
2449 || DECL_BUILT_IN_CLASS (fn) == BUILT_IN_MD))
2450 result = resolve_overloaded_builtin (input_location, fn, *args);
2452 if (!result)
2454 if (warn_sizeof_pointer_memaccess
2455 && (complain & tf_warning)
2456 && !vec_safe_is_empty (*args)
2457 && !processing_template_decl)
2459 location_t sizeof_arg_loc[3];
2460 tree sizeof_arg[3];
2461 unsigned int i;
2462 for (i = 0; i < 3; i++)
2464 tree t;
2466 sizeof_arg_loc[i] = UNKNOWN_LOCATION;
2467 sizeof_arg[i] = NULL_TREE;
2468 if (i >= (*args)->length ())
2469 continue;
2470 t = (**args)[i];
2471 if (TREE_CODE (t) != SIZEOF_EXPR)
2472 continue;
2473 if (SIZEOF_EXPR_TYPE_P (t))
2474 sizeof_arg[i] = TREE_TYPE (TREE_OPERAND (t, 0));
2475 else
2476 sizeof_arg[i] = TREE_OPERAND (t, 0);
2477 sizeof_arg_loc[i] = EXPR_LOCATION (t);
2479 sizeof_pointer_memaccess_warning
2480 (sizeof_arg_loc, fn, *args,
2481 sizeof_arg, same_type_ignoring_top_level_qualifiers_p);
2484 /* A call to a namespace-scope function. */
2485 result = build_new_function_call (fn, args, complain);
2488 else if (TREE_CODE (fn) == PSEUDO_DTOR_EXPR)
2490 if (!vec_safe_is_empty (*args))
2491 error ("arguments to destructor are not allowed");
2492 /* Mark the pseudo-destructor call as having side-effects so
2493 that we do not issue warnings about its use. */
2494 result = build1 (NOP_EXPR,
2495 void_type_node,
2496 TREE_OPERAND (fn, 0));
2497 TREE_SIDE_EFFECTS (result) = 1;
2499 else if (CLASS_TYPE_P (TREE_TYPE (fn)))
2500 /* If the "function" is really an object of class type, it might
2501 have an overloaded `operator ()'. */
2502 result = build_op_call (fn, args, complain);
2504 if (!result)
2505 /* A call where the function is unknown. */
2506 result = cp_build_function_call_vec (fn, args, complain);
2508 if (processing_template_decl && result != error_mark_node)
2510 if (INDIRECT_REF_P (result))
2511 result = TREE_OPERAND (result, 0);
2512 result = build_call_vec (TREE_TYPE (result), orig_fn, orig_args);
2513 SET_EXPR_LOCATION (result, input_location);
2514 KOENIG_LOOKUP_P (result) = koenig_p;
2515 release_tree_vector (orig_args);
2516 result = convert_from_reference (result);
2519 /* Free or retain OVERLOADs from lookup. */
2520 if (is_overloaded_fn (orig_fn))
2521 lookup_keep (get_fns (orig_fn), processing_template_decl);
2523 return result;
2526 /* Finish a call to a postfix increment or decrement or EXPR. (Which
2527 is indicated by CODE, which should be POSTINCREMENT_EXPR or
2528 POSTDECREMENT_EXPR.) */
2530 cp_expr
2531 finish_increment_expr (cp_expr expr, enum tree_code code)
2533 /* input_location holds the location of the trailing operator token.
2534 Build a location of the form:
2535 expr++
2536 ~~~~^~
2537 with the caret at the operator token, ranging from the start
2538 of EXPR to the end of the operator token. */
2539 location_t combined_loc = make_location (input_location,
2540 expr.get_start (),
2541 get_finish (input_location));
2542 cp_expr result = build_x_unary_op (combined_loc, code, expr,
2543 tf_warning_or_error);
2544 /* TODO: build_x_unary_op doesn't honor the location, so set it here. */
2545 result.set_location (combined_loc);
2546 return result;
2549 /* Finish a use of `this'. Returns an expression for `this'. */
2551 tree
2552 finish_this_expr (void)
2554 tree result = NULL_TREE;
2556 if (current_class_ptr)
2558 tree type = TREE_TYPE (current_class_ref);
2560 /* In a lambda expression, 'this' refers to the captured 'this'. */
2561 if (LAMBDA_TYPE_P (type))
2562 result = lambda_expr_this_capture (CLASSTYPE_LAMBDA_EXPR (type), true);
2563 else
2564 result = current_class_ptr;
2567 if (result)
2568 /* The keyword 'this' is a prvalue expression. */
2569 return rvalue (result);
2571 tree fn = current_nonlambda_function ();
2572 if (fn && DECL_STATIC_FUNCTION_P (fn))
2573 error ("%<this%> is unavailable for static member functions");
2574 else if (fn)
2575 error ("invalid use of %<this%> in non-member function");
2576 else
2577 error ("invalid use of %<this%> at top level");
2578 return error_mark_node;
2581 /* Finish a pseudo-destructor expression. If SCOPE is NULL, the
2582 expression was of the form `OBJECT.~DESTRUCTOR' where DESTRUCTOR is
2583 the TYPE for the type given. If SCOPE is non-NULL, the expression
2584 was of the form `OBJECT.SCOPE::~DESTRUCTOR'. */
2586 tree
2587 finish_pseudo_destructor_expr (tree object, tree scope, tree destructor,
2588 location_t loc)
2590 if (object == error_mark_node || destructor == error_mark_node)
2591 return error_mark_node;
2593 gcc_assert (TYPE_P (destructor));
2595 if (!processing_template_decl)
2597 if (scope == error_mark_node)
2599 error_at (loc, "invalid qualifying scope in pseudo-destructor name");
2600 return error_mark_node;
2602 if (is_auto (destructor))
2603 destructor = TREE_TYPE (object);
2604 if (scope && TYPE_P (scope) && !check_dtor_name (scope, destructor))
2606 error_at (loc,
2607 "qualified type %qT does not match destructor name ~%qT",
2608 scope, destructor);
2609 return error_mark_node;
2613 /* [expr.pseudo] says both:
2615 The type designated by the pseudo-destructor-name shall be
2616 the same as the object type.
2618 and:
2620 The cv-unqualified versions of the object type and of the
2621 type designated by the pseudo-destructor-name shall be the
2622 same type.
2624 We implement the more generous second sentence, since that is
2625 what most other compilers do. */
2626 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (object),
2627 destructor))
2629 error_at (loc, "%qE is not of type %qT", object, destructor);
2630 return error_mark_node;
2634 return build3_loc (loc, PSEUDO_DTOR_EXPR, void_type_node, object,
2635 scope, destructor);
2638 /* Finish an expression of the form CODE EXPR. */
2640 cp_expr
2641 finish_unary_op_expr (location_t op_loc, enum tree_code code, cp_expr expr,
2642 tsubst_flags_t complain)
2644 /* Build a location of the form:
2645 ++expr
2646 ^~~~~~
2647 with the caret at the operator token, ranging from the start
2648 of the operator token to the end of EXPR. */
2649 location_t combined_loc = make_location (op_loc,
2650 op_loc, expr.get_finish ());
2651 cp_expr result = build_x_unary_op (combined_loc, code, expr, complain);
2652 /* TODO: build_x_unary_op doesn't always honor the location. */
2653 result.set_location (combined_loc);
2655 tree result_ovl, expr_ovl;
2657 if (!(complain & tf_warning))
2658 return result;
2660 result_ovl = result;
2661 expr_ovl = expr;
2663 if (!processing_template_decl)
2664 expr_ovl = cp_fully_fold (expr_ovl);
2666 if (!CONSTANT_CLASS_P (expr_ovl)
2667 || TREE_OVERFLOW_P (expr_ovl))
2668 return result;
2670 if (!processing_template_decl)
2671 result_ovl = cp_fully_fold (result_ovl);
2673 if (CONSTANT_CLASS_P (result_ovl) && TREE_OVERFLOW_P (result_ovl))
2674 overflow_warning (combined_loc, result_ovl);
2676 return result;
2679 /* Finish a compound-literal expression or C++11 functional cast with aggregate
2680 initializer. TYPE is the type to which the CONSTRUCTOR in COMPOUND_LITERAL
2681 is being cast. */
2683 tree
2684 finish_compound_literal (tree type, tree compound_literal,
2685 tsubst_flags_t complain,
2686 fcl_t fcl_context)
2688 if (type == error_mark_node)
2689 return error_mark_node;
2691 if (TREE_CODE (type) == REFERENCE_TYPE)
2693 compound_literal
2694 = finish_compound_literal (TREE_TYPE (type), compound_literal,
2695 complain, fcl_context);
2696 return cp_build_c_cast (type, compound_literal, complain);
2699 if (!TYPE_OBJ_P (type))
2701 if (complain & tf_error)
2702 error ("compound literal of non-object type %qT", type);
2703 return error_mark_node;
2706 if (tree anode = type_uses_auto (type))
2707 if (CLASS_PLACEHOLDER_TEMPLATE (anode))
2709 type = do_auto_deduction (type, compound_literal, anode, complain,
2710 adc_variable_type);
2711 if (type == error_mark_node)
2712 return error_mark_node;
2715 if (processing_template_decl)
2717 TREE_TYPE (compound_literal) = type;
2718 /* Mark the expression as a compound literal. */
2719 TREE_HAS_CONSTRUCTOR (compound_literal) = 1;
2720 if (fcl_context == fcl_c99)
2721 CONSTRUCTOR_C99_COMPOUND_LITERAL (compound_literal) = 1;
2722 return compound_literal;
2725 type = complete_type (type);
2727 if (TYPE_NON_AGGREGATE_CLASS (type))
2729 /* Trying to deal with a CONSTRUCTOR instead of a TREE_LIST
2730 everywhere that deals with function arguments would be a pain, so
2731 just wrap it in a TREE_LIST. The parser set a flag so we know
2732 that it came from T{} rather than T({}). */
2733 CONSTRUCTOR_IS_DIRECT_INIT (compound_literal) = 1;
2734 compound_literal = build_tree_list (NULL_TREE, compound_literal);
2735 return build_functional_cast (type, compound_literal, complain);
2738 if (TREE_CODE (type) == ARRAY_TYPE
2739 && check_array_initializer (NULL_TREE, type, compound_literal))
2740 return error_mark_node;
2741 compound_literal = reshape_init (type, compound_literal, complain);
2742 if (SCALAR_TYPE_P (type)
2743 && !BRACE_ENCLOSED_INITIALIZER_P (compound_literal)
2744 && !check_narrowing (type, compound_literal, complain))
2745 return error_mark_node;
2746 if (TREE_CODE (type) == ARRAY_TYPE
2747 && TYPE_DOMAIN (type) == NULL_TREE)
2749 cp_complete_array_type_or_error (&type, compound_literal,
2750 false, complain);
2751 if (type == error_mark_node)
2752 return error_mark_node;
2754 compound_literal = digest_init_flags (type, compound_literal, LOOKUP_NORMAL,
2755 complain);
2756 if (TREE_CODE (compound_literal) == CONSTRUCTOR)
2758 TREE_HAS_CONSTRUCTOR (compound_literal) = true;
2759 if (fcl_context == fcl_c99)
2760 CONSTRUCTOR_C99_COMPOUND_LITERAL (compound_literal) = 1;
2763 /* Put static/constant array temporaries in static variables. */
2764 /* FIXME all C99 compound literals should be variables rather than C++
2765 temporaries, unless they are used as an aggregate initializer. */
2766 if ((!at_function_scope_p () || CP_TYPE_CONST_P (type))
2767 && fcl_context == fcl_c99
2768 && TREE_CODE (type) == ARRAY_TYPE
2769 && !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
2770 && initializer_constant_valid_p (compound_literal, type))
2772 tree decl = create_temporary_var (type);
2773 DECL_INITIAL (decl) = compound_literal;
2774 TREE_STATIC (decl) = 1;
2775 if (literal_type_p (type) && CP_TYPE_CONST_NON_VOLATILE_P (type))
2777 /* 5.19 says that a constant expression can include an
2778 lvalue-rvalue conversion applied to "a glvalue of literal type
2779 that refers to a non-volatile temporary object initialized
2780 with a constant expression". Rather than try to communicate
2781 that this VAR_DECL is a temporary, just mark it constexpr. */
2782 DECL_DECLARED_CONSTEXPR_P (decl) = true;
2783 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
2784 TREE_CONSTANT (decl) = true;
2786 cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
2787 decl = pushdecl_top_level (decl);
2788 DECL_NAME (decl) = make_anon_name ();
2789 SET_DECL_ASSEMBLER_NAME (decl, DECL_NAME (decl));
2790 /* Make sure the destructor is callable. */
2791 tree clean = cxx_maybe_build_cleanup (decl, complain);
2792 if (clean == error_mark_node)
2793 return error_mark_node;
2794 return decl;
2797 /* Represent other compound literals with TARGET_EXPR so we produce
2798 an lvalue, but can elide copies. */
2799 if (!VECTOR_TYPE_P (type))
2800 compound_literal = get_target_expr_sfinae (compound_literal, complain);
2802 return compound_literal;
2805 /* Return the declaration for the function-name variable indicated by
2806 ID. */
2808 tree
2809 finish_fname (tree id)
2811 tree decl;
2813 decl = fname_decl (input_location, C_RID_CODE (id), id);
2814 if (processing_template_decl && current_function_decl
2815 && decl != error_mark_node)
2816 decl = DECL_NAME (decl);
2817 return decl;
2820 /* Finish a translation unit. */
2822 void
2823 finish_translation_unit (void)
2825 /* In case there were missing closebraces,
2826 get us back to the global binding level. */
2827 pop_everything ();
2828 while (current_namespace != global_namespace)
2829 pop_namespace ();
2831 /* Do file scope __FUNCTION__ et al. */
2832 finish_fname_decls ();
2835 /* Finish a template type parameter, specified as AGGR IDENTIFIER.
2836 Returns the parameter. */
2838 tree
2839 finish_template_type_parm (tree aggr, tree identifier)
2841 if (aggr != class_type_node)
2843 permerror (input_location, "template type parameters must use the keyword %<class%> or %<typename%>");
2844 aggr = class_type_node;
2847 return build_tree_list (aggr, identifier);
2850 /* Finish a template template parameter, specified as AGGR IDENTIFIER.
2851 Returns the parameter. */
2853 tree
2854 finish_template_template_parm (tree aggr, tree identifier)
2856 tree decl = build_decl (input_location,
2857 TYPE_DECL, identifier, NULL_TREE);
2859 tree tmpl = build_lang_decl (TEMPLATE_DECL, identifier, NULL_TREE);
2860 DECL_TEMPLATE_PARMS (tmpl) = current_template_parms;
2861 DECL_TEMPLATE_RESULT (tmpl) = decl;
2862 DECL_ARTIFICIAL (decl) = 1;
2864 // Associate the constraints with the underlying declaration,
2865 // not the template.
2866 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
2867 tree constr = build_constraints (reqs, NULL_TREE);
2868 set_constraints (decl, constr);
2870 end_template_decl ();
2872 gcc_assert (DECL_TEMPLATE_PARMS (tmpl));
2874 check_default_tmpl_args (decl, DECL_TEMPLATE_PARMS (tmpl),
2875 /*is_primary=*/true, /*is_partial=*/false,
2876 /*is_friend=*/0);
2878 return finish_template_type_parm (aggr, tmpl);
2881 /* ARGUMENT is the default-argument value for a template template
2882 parameter. If ARGUMENT is invalid, issue error messages and return
2883 the ERROR_MARK_NODE. Otherwise, ARGUMENT itself is returned. */
2885 tree
2886 check_template_template_default_arg (tree argument)
2888 if (TREE_CODE (argument) != TEMPLATE_DECL
2889 && TREE_CODE (argument) != TEMPLATE_TEMPLATE_PARM
2890 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
2892 if (TREE_CODE (argument) == TYPE_DECL)
2893 error ("invalid use of type %qT as a default value for a template "
2894 "template-parameter", TREE_TYPE (argument));
2895 else
2896 error ("invalid default argument for a template template parameter");
2897 return error_mark_node;
2900 return argument;
2903 /* Begin a class definition, as indicated by T. */
2905 tree
2906 begin_class_definition (tree t)
2908 if (error_operand_p (t) || error_operand_p (TYPE_MAIN_DECL (t)))
2909 return error_mark_node;
2911 if (processing_template_parmlist)
2913 error ("definition of %q#T inside template parameter list", t);
2914 return error_mark_node;
2917 /* According to the C++ ABI, decimal classes defined in ISO/IEC TR 24733
2918 are passed the same as decimal scalar types. */
2919 if (TREE_CODE (t) == RECORD_TYPE
2920 && !processing_template_decl)
2922 tree ns = TYPE_CONTEXT (t);
2923 if (ns && TREE_CODE (ns) == NAMESPACE_DECL
2924 && DECL_CONTEXT (ns) == std_node
2925 && DECL_NAME (ns)
2926 && id_equal (DECL_NAME (ns), "decimal"))
2928 const char *n = TYPE_NAME_STRING (t);
2929 if ((strcmp (n, "decimal32") == 0)
2930 || (strcmp (n, "decimal64") == 0)
2931 || (strcmp (n, "decimal128") == 0))
2932 TYPE_TRANSPARENT_AGGR (t) = 1;
2936 /* A non-implicit typename comes from code like:
2938 template <typename T> struct A {
2939 template <typename U> struct A<T>::B ...
2941 This is erroneous. */
2942 else if (TREE_CODE (t) == TYPENAME_TYPE)
2944 error ("invalid definition of qualified type %qT", t);
2945 t = error_mark_node;
2948 if (t == error_mark_node || ! MAYBE_CLASS_TYPE_P (t))
2950 t = make_class_type (RECORD_TYPE);
2951 pushtag (make_anon_name (), t, /*tag_scope=*/ts_current);
2954 if (TYPE_BEING_DEFINED (t))
2956 t = make_class_type (TREE_CODE (t));
2957 pushtag (TYPE_IDENTIFIER (t), t, /*tag_scope=*/ts_current);
2959 maybe_process_partial_specialization (t);
2960 pushclass (t);
2961 TYPE_BEING_DEFINED (t) = 1;
2962 class_binding_level->defining_class_p = 1;
2964 if (flag_pack_struct)
2966 tree v;
2967 TYPE_PACKED (t) = 1;
2968 /* Even though the type is being defined for the first time
2969 here, there might have been a forward declaration, so there
2970 might be cv-qualified variants of T. */
2971 for (v = TYPE_NEXT_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v))
2972 TYPE_PACKED (v) = 1;
2974 /* Reset the interface data, at the earliest possible
2975 moment, as it might have been set via a class foo;
2976 before. */
2977 if (! TYPE_UNNAMED_P (t))
2979 struct c_fileinfo *finfo = \
2980 get_fileinfo (LOCATION_FILE (input_location));
2981 CLASSTYPE_INTERFACE_ONLY (t) = finfo->interface_only;
2982 SET_CLASSTYPE_INTERFACE_UNKNOWN_X
2983 (t, finfo->interface_unknown);
2985 reset_specialization();
2987 /* Make a declaration for this class in its own scope. */
2988 build_self_reference ();
2990 return t;
2993 /* Finish the member declaration given by DECL. */
2995 void
2996 finish_member_declaration (tree decl)
2998 if (decl == error_mark_node || decl == NULL_TREE)
2999 return;
3001 if (decl == void_type_node)
3002 /* The COMPONENT was a friend, not a member, and so there's
3003 nothing for us to do. */
3004 return;
3006 /* We should see only one DECL at a time. */
3007 gcc_assert (DECL_CHAIN (decl) == NULL_TREE);
3009 /* Don't add decls after definition. */
3010 gcc_assert (TYPE_BEING_DEFINED (current_class_type)
3011 /* We can add lambda types when late parsing default
3012 arguments. */
3013 || LAMBDA_TYPE_P (TREE_TYPE (decl)));
3015 /* Set up access control for DECL. */
3016 TREE_PRIVATE (decl)
3017 = (current_access_specifier == access_private_node);
3018 TREE_PROTECTED (decl)
3019 = (current_access_specifier == access_protected_node);
3020 if (TREE_CODE (decl) == TEMPLATE_DECL)
3022 TREE_PRIVATE (DECL_TEMPLATE_RESULT (decl)) = TREE_PRIVATE (decl);
3023 TREE_PROTECTED (DECL_TEMPLATE_RESULT (decl)) = TREE_PROTECTED (decl);
3026 /* Mark the DECL as a member of the current class, unless it's
3027 a member of an enumeration. */
3028 if (TREE_CODE (decl) != CONST_DECL)
3029 DECL_CONTEXT (decl) = current_class_type;
3031 if (TREE_CODE (decl) == USING_DECL)
3032 /* For now, ignore class-scope USING_DECLS, so that debugging
3033 backends do not see them. */
3034 DECL_IGNORED_P (decl) = 1;
3036 /* Check for bare parameter packs in the non-static data member
3037 declaration. */
3038 if (TREE_CODE (decl) == FIELD_DECL)
3040 if (check_for_bare_parameter_packs (TREE_TYPE (decl)))
3041 TREE_TYPE (decl) = error_mark_node;
3042 if (check_for_bare_parameter_packs (DECL_ATTRIBUTES (decl)))
3043 DECL_ATTRIBUTES (decl) = NULL_TREE;
3046 /* [dcl.link]
3048 A C language linkage is ignored for the names of class members
3049 and the member function type of class member functions. */
3050 if (DECL_LANG_SPECIFIC (decl))
3051 SET_DECL_LANGUAGE (decl, lang_cplusplus);
3053 bool add = false;
3055 /* Functions and non-functions are added differently. */
3056 if (DECL_DECLARES_FUNCTION_P (decl))
3057 add = add_method (current_class_type, decl, false);
3058 /* Enter the DECL into the scope of the class, if the class
3059 isn't a closure (whose fields are supposed to be unnamed). */
3060 else if (CLASSTYPE_LAMBDA_EXPR (current_class_type)
3061 || pushdecl_class_level (decl))
3062 add = true;
3064 if (add)
3066 /* All TYPE_DECLs go at the end of TYPE_FIELDS. Ordinary fields
3067 go at the beginning. The reason is that
3068 legacy_nonfn_member_lookup searches the list in order, and we
3069 want a field name to override a type name so that the "struct
3070 stat hack" will work. In particular:
3072 struct S { enum E { }; static const int E = 5; int ary[S::E]; } s;
3074 is valid. */
3076 if (TREE_CODE (decl) == TYPE_DECL)
3077 TYPE_FIELDS (current_class_type)
3078 = chainon (TYPE_FIELDS (current_class_type), decl);
3079 else
3081 DECL_CHAIN (decl) = TYPE_FIELDS (current_class_type);
3082 TYPE_FIELDS (current_class_type) = decl;
3085 maybe_add_class_template_decl_list (current_class_type, decl,
3086 /*friend_p=*/0);
3090 /* Finish processing a complete template declaration. The PARMS are
3091 the template parameters. */
3093 void
3094 finish_template_decl (tree parms)
3096 if (parms)
3097 end_template_decl ();
3098 else
3099 end_specialization ();
3102 // Returns the template type of the class scope being entered. If we're
3103 // entering a constrained class scope. TYPE is the class template
3104 // scope being entered and we may need to match the intended type with
3105 // a constrained specialization. For example:
3107 // template<Object T>
3108 // struct S { void f(); }; #1
3110 // template<Object T>
3111 // void S<T>::f() { } #2
3113 // We check, in #2, that S<T> refers precisely to the type declared by
3114 // #1 (i.e., that the constraints match). Note that the following should
3115 // be an error since there is no specialization of S<T> that is
3116 // unconstrained, but this is not diagnosed here.
3118 // template<typename T>
3119 // void S<T>::f() { }
3121 // We cannot diagnose this problem here since this function also matches
3122 // qualified template names that are not part of a definition. For example:
3124 // template<Integral T, Floating_point U>
3125 // typename pair<T, U>::first_type void f(T, U);
3127 // Here, it is unlikely that there is a partial specialization of
3128 // pair constrained for for Integral and Floating_point arguments.
3130 // The general rule is: if a constrained specialization with matching
3131 // constraints is found return that type. Also note that if TYPE is not a
3132 // class-type (e.g. a typename type), then no fixup is needed.
3134 static tree
3135 fixup_template_type (tree type)
3137 // Find the template parameter list at the a depth appropriate to
3138 // the scope we're trying to enter.
3139 tree parms = current_template_parms;
3140 int depth = template_class_depth (type);
3141 for (int n = processing_template_decl; n > depth && parms; --n)
3142 parms = TREE_CHAIN (parms);
3143 if (!parms)
3144 return type;
3145 tree cur_reqs = TEMPLATE_PARMS_CONSTRAINTS (parms);
3146 tree cur_constr = build_constraints (cur_reqs, NULL_TREE);
3148 // Search for a specialization whose type and constraints match.
3149 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
3150 tree specs = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
3151 while (specs)
3153 tree spec_constr = get_constraints (TREE_VALUE (specs));
3155 // If the type and constraints match a specialization, then we
3156 // are entering that type.
3157 if (same_type_p (type, TREE_TYPE (specs))
3158 && equivalent_constraints (cur_constr, spec_constr))
3159 return TREE_TYPE (specs);
3160 specs = TREE_CHAIN (specs);
3163 // If no specialization matches, then must return the type
3164 // previously found.
3165 return type;
3168 /* Finish processing a template-id (which names a type) of the form
3169 NAME < ARGS >. Return the TYPE_DECL for the type named by the
3170 template-id. If ENTERING_SCOPE is nonzero we are about to enter
3171 the scope of template-id indicated. */
3173 tree
3174 finish_template_type (tree name, tree args, int entering_scope)
3176 tree type;
3178 type = lookup_template_class (name, args,
3179 NULL_TREE, NULL_TREE, entering_scope,
3180 tf_warning_or_error | tf_user);
3182 /* If we might be entering the scope of a partial specialization,
3183 find the one with the right constraints. */
3184 if (flag_concepts
3185 && entering_scope
3186 && CLASS_TYPE_P (type)
3187 && CLASSTYPE_TEMPLATE_INFO (type)
3188 && dependent_type_p (type)
3189 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
3190 type = fixup_template_type (type);
3192 if (type == error_mark_node)
3193 return type;
3194 else if (CLASS_TYPE_P (type) && !alias_type_or_template_p (type))
3195 return TYPE_STUB_DECL (type);
3196 else
3197 return TYPE_NAME (type);
3200 /* Finish processing a BASE_CLASS with the indicated ACCESS_SPECIFIER.
3201 Return a TREE_LIST containing the ACCESS_SPECIFIER and the
3202 BASE_CLASS, or NULL_TREE if an error occurred. The
3203 ACCESS_SPECIFIER is one of
3204 access_{default,public,protected_private}_node. For a virtual base
3205 we set TREE_TYPE. */
3207 tree
3208 finish_base_specifier (tree base, tree access, bool virtual_p)
3210 tree result;
3212 if (base == error_mark_node)
3214 error ("invalid base-class specification");
3215 result = NULL_TREE;
3217 else if (! MAYBE_CLASS_TYPE_P (base))
3219 error ("%qT is not a class type", base);
3220 result = NULL_TREE;
3222 else
3224 if (cp_type_quals (base) != 0)
3226 /* DR 484: Can a base-specifier name a cv-qualified
3227 class type? */
3228 base = TYPE_MAIN_VARIANT (base);
3230 result = build_tree_list (access, base);
3231 if (virtual_p)
3232 TREE_TYPE (result) = integer_type_node;
3235 return result;
3238 /* If FNS is a member function, a set of member functions, or a
3239 template-id referring to one or more member functions, return a
3240 BASELINK for FNS, incorporating the current access context.
3241 Otherwise, return FNS unchanged. */
3243 tree
3244 baselink_for_fns (tree fns)
3246 tree scope;
3247 tree cl;
3249 if (BASELINK_P (fns)
3250 || error_operand_p (fns))
3251 return fns;
3253 scope = ovl_scope (fns);
3254 if (!CLASS_TYPE_P (scope))
3255 return fns;
3257 cl = currently_open_derived_class (scope);
3258 if (!cl)
3259 cl = scope;
3260 cl = TYPE_BINFO (cl);
3261 return build_baselink (cl, cl, fns, /*optype=*/NULL_TREE);
3264 /* Returns true iff DECL is a variable from a function outside
3265 the current one. */
3267 static bool
3268 outer_var_p (tree decl)
3270 return ((VAR_P (decl) || TREE_CODE (decl) == PARM_DECL)
3271 && DECL_FUNCTION_SCOPE_P (decl)
3272 /* Don't get confused by temporaries. */
3273 && DECL_NAME (decl)
3274 && (DECL_CONTEXT (decl) != current_function_decl
3275 || parsing_nsdmi ()));
3278 /* As above, but also checks that DECL is automatic. */
3280 bool
3281 outer_automatic_var_p (tree decl)
3283 return (outer_var_p (decl)
3284 && !TREE_STATIC (decl));
3287 /* DECL satisfies outer_automatic_var_p. Possibly complain about it or
3288 rewrite it for lambda capture.
3290 If ODR_USE is true, we're being called from mark_use, and we complain about
3291 use of constant variables. If ODR_USE is false, we're being called for the
3292 id-expression, and we do lambda capture. */
3294 tree
3295 process_outer_var_ref (tree decl, tsubst_flags_t complain, bool odr_use)
3297 if (cp_unevaluated_operand)
3298 /* It's not a use (3.2) if we're in an unevaluated context. */
3299 return decl;
3300 if (decl == error_mark_node)
3301 return decl;
3303 tree context = DECL_CONTEXT (decl);
3304 tree containing_function = current_function_decl;
3305 tree lambda_stack = NULL_TREE;
3306 tree lambda_expr = NULL_TREE;
3307 tree initializer = convert_from_reference (decl);
3309 /* Mark it as used now even if the use is ill-formed. */
3310 if (!mark_used (decl, complain))
3311 return error_mark_node;
3313 if (parsing_nsdmi ())
3314 containing_function = NULL_TREE;
3316 if (containing_function && LAMBDA_FUNCTION_P (containing_function))
3318 /* Check whether we've already built a proxy. */
3319 tree var = decl;
3320 while (is_normal_capture_proxy (var))
3321 var = DECL_CAPTURED_VARIABLE (var);
3322 tree d = retrieve_local_specialization (var);
3324 if (d && d != decl && is_capture_proxy (d))
3326 if (DECL_CONTEXT (d) == containing_function)
3327 /* We already have an inner proxy. */
3328 return d;
3329 else
3330 /* We need to capture an outer proxy. */
3331 return process_outer_var_ref (d, complain, odr_use);
3335 /* If we are in a lambda function, we can move out until we hit
3336 1. the context,
3337 2. a non-lambda function, or
3338 3. a non-default capturing lambda function. */
3339 while (context != containing_function
3340 /* containing_function can be null with invalid generic lambdas. */
3341 && containing_function
3342 && LAMBDA_FUNCTION_P (containing_function))
3344 tree closure = DECL_CONTEXT (containing_function);
3345 lambda_expr = CLASSTYPE_LAMBDA_EXPR (closure);
3347 if (TYPE_CLASS_SCOPE_P (closure))
3348 /* A lambda in an NSDMI (c++/64496). */
3349 break;
3351 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr)
3352 == CPLD_NONE)
3353 break;
3355 lambda_stack = tree_cons (NULL_TREE,
3356 lambda_expr,
3357 lambda_stack);
3359 containing_function
3360 = decl_function_context (containing_function);
3363 /* In a lambda within a template, wait until instantiation
3364 time to implicitly capture. */
3365 if (context == containing_function
3366 && DECL_TEMPLATE_INFO (containing_function)
3367 && uses_template_parms (DECL_TI_ARGS (containing_function)))
3368 return decl;
3370 if (lambda_expr && VAR_P (decl)
3371 && DECL_ANON_UNION_VAR_P (decl))
3373 if (complain & tf_error)
3374 error ("cannot capture member %qD of anonymous union", decl);
3375 return error_mark_node;
3377 /* Do lambda capture when processing the id-expression, not when
3378 odr-using a variable. */
3379 if (!odr_use && context == containing_function)
3381 decl = add_default_capture (lambda_stack,
3382 /*id=*/DECL_NAME (decl),
3383 initializer);
3385 /* Only an odr-use of an outer automatic variable causes an
3386 error, and a constant variable can decay to a prvalue
3387 constant without odr-use. So don't complain yet. */
3388 else if (!odr_use && decl_constant_var_p (decl))
3389 return decl;
3390 else if (lambda_expr)
3392 if (complain & tf_error)
3394 error ("%qD is not captured", decl);
3395 tree closure = LAMBDA_EXPR_CLOSURE (lambda_expr);
3396 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr)
3397 == CPLD_NONE)
3398 inform (location_of (closure),
3399 "the lambda has no capture-default");
3400 else if (TYPE_CLASS_SCOPE_P (closure))
3401 inform (UNKNOWN_LOCATION, "lambda in local class %q+T cannot "
3402 "capture variables from the enclosing context",
3403 TYPE_CONTEXT (closure));
3404 inform (DECL_SOURCE_LOCATION (decl), "%q#D declared here", decl);
3406 return error_mark_node;
3408 else
3410 if (complain & tf_error)
3412 error (VAR_P (decl)
3413 ? G_("use of local variable with automatic storage from "
3414 "containing function")
3415 : G_("use of parameter from containing function"));
3416 inform (DECL_SOURCE_LOCATION (decl), "%q#D declared here", decl);
3418 return error_mark_node;
3420 return decl;
3423 /* ID_EXPRESSION is a representation of parsed, but unprocessed,
3424 id-expression. (See cp_parser_id_expression for details.) SCOPE,
3425 if non-NULL, is the type or namespace used to explicitly qualify
3426 ID_EXPRESSION. DECL is the entity to which that name has been
3427 resolved.
3429 *CONSTANT_EXPRESSION_P is true if we are presently parsing a
3430 constant-expression. In that case, *NON_CONSTANT_EXPRESSION_P will
3431 be set to true if this expression isn't permitted in a
3432 constant-expression, but it is otherwise not set by this function.
3433 *ALLOW_NON_CONSTANT_EXPRESSION_P is true if we are parsing a
3434 constant-expression, but a non-constant expression is also
3435 permissible.
3437 DONE is true if this expression is a complete postfix-expression;
3438 it is false if this expression is followed by '->', '[', '(', etc.
3439 ADDRESS_P is true iff this expression is the operand of '&'.
3440 TEMPLATE_P is true iff the qualified-id was of the form
3441 "A::template B". TEMPLATE_ARG_P is true iff this qualified name
3442 appears as a template argument.
3444 If an error occurs, and it is the kind of error that might cause
3445 the parser to abort a tentative parse, *ERROR_MSG is filled in. It
3446 is the caller's responsibility to issue the message. *ERROR_MSG
3447 will be a string with static storage duration, so the caller need
3448 not "free" it.
3450 Return an expression for the entity, after issuing appropriate
3451 diagnostics. This function is also responsible for transforming a
3452 reference to a non-static member into a COMPONENT_REF that makes
3453 the use of "this" explicit.
3455 Upon return, *IDK will be filled in appropriately. */
3456 cp_expr
3457 finish_id_expression (tree id_expression,
3458 tree decl,
3459 tree scope,
3460 cp_id_kind *idk,
3461 bool integral_constant_expression_p,
3462 bool allow_non_integral_constant_expression_p,
3463 bool *non_integral_constant_expression_p,
3464 bool template_p,
3465 bool done,
3466 bool address_p,
3467 bool template_arg_p,
3468 const char **error_msg,
3469 location_t location)
3471 decl = strip_using_decl (decl);
3473 /* Initialize the output parameters. */
3474 *idk = CP_ID_KIND_NONE;
3475 *error_msg = NULL;
3477 if (id_expression == error_mark_node)
3478 return error_mark_node;
3479 /* If we have a template-id, then no further lookup is
3480 required. If the template-id was for a template-class, we
3481 will sometimes have a TYPE_DECL at this point. */
3482 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3483 || TREE_CODE (decl) == TYPE_DECL)
3485 /* Look up the name. */
3486 else
3488 if (decl == error_mark_node)
3490 /* Name lookup failed. */
3491 if (scope
3492 && (!TYPE_P (scope)
3493 || (!dependent_type_p (scope)
3494 && !(identifier_p (id_expression)
3495 && IDENTIFIER_CONV_OP_P (id_expression)
3496 && dependent_type_p (TREE_TYPE (id_expression))))))
3498 /* If the qualifying type is non-dependent (and the name
3499 does not name a conversion operator to a dependent
3500 type), issue an error. */
3501 qualified_name_lookup_error (scope, id_expression, decl, location);
3502 return error_mark_node;
3504 else if (!scope)
3506 /* It may be resolved via Koenig lookup. */
3507 *idk = CP_ID_KIND_UNQUALIFIED;
3508 return id_expression;
3510 else
3511 decl = id_expression;
3513 /* If DECL is a variable that would be out of scope under
3514 ANSI/ISO rules, but in scope in the ARM, name lookup
3515 will succeed. Issue a diagnostic here. */
3516 else
3517 decl = check_for_out_of_scope_variable (decl);
3519 /* Remember that the name was used in the definition of
3520 the current class so that we can check later to see if
3521 the meaning would have been different after the class
3522 was entirely defined. */
3523 if (!scope && decl != error_mark_node && identifier_p (id_expression))
3524 maybe_note_name_used_in_class (id_expression, decl);
3526 /* A use in unevaluated operand might not be instantiated appropriately
3527 if tsubst_copy builds a dummy parm, or if we never instantiate a
3528 generic lambda, so mark it now. */
3529 if (processing_template_decl && cp_unevaluated_operand)
3530 mark_type_use (decl);
3532 /* Disallow uses of local variables from containing functions, except
3533 within lambda-expressions. */
3534 if (outer_automatic_var_p (decl))
3536 decl = process_outer_var_ref (decl, tf_warning_or_error);
3537 if (decl == error_mark_node)
3538 return error_mark_node;
3541 /* Also disallow uses of function parameters outside the function
3542 body, except inside an unevaluated context (i.e. decltype). */
3543 if (TREE_CODE (decl) == PARM_DECL
3544 && DECL_CONTEXT (decl) == NULL_TREE
3545 && !cp_unevaluated_operand)
3547 *error_msg = G_("use of parameter outside function body");
3548 return error_mark_node;
3552 /* If we didn't find anything, or what we found was a type,
3553 then this wasn't really an id-expression. */
3554 if (TREE_CODE (decl) == TEMPLATE_DECL
3555 && !DECL_FUNCTION_TEMPLATE_P (decl))
3557 *error_msg = G_("missing template arguments");
3558 return error_mark_node;
3560 else if (TREE_CODE (decl) == TYPE_DECL
3561 || TREE_CODE (decl) == NAMESPACE_DECL)
3563 *error_msg = G_("expected primary-expression");
3564 return error_mark_node;
3567 /* If the name resolved to a template parameter, there is no
3568 need to look it up again later. */
3569 if ((TREE_CODE (decl) == CONST_DECL && DECL_TEMPLATE_PARM_P (decl))
3570 || TREE_CODE (decl) == TEMPLATE_PARM_INDEX)
3572 tree r;
3574 *idk = CP_ID_KIND_NONE;
3575 if (TREE_CODE (decl) == TEMPLATE_PARM_INDEX)
3576 decl = TEMPLATE_PARM_DECL (decl);
3577 r = convert_from_reference (DECL_INITIAL (decl));
3579 if (integral_constant_expression_p
3580 && !dependent_type_p (TREE_TYPE (decl))
3581 && !(INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (r))))
3583 if (!allow_non_integral_constant_expression_p)
3584 error ("template parameter %qD of type %qT is not allowed in "
3585 "an integral constant expression because it is not of "
3586 "integral or enumeration type", decl, TREE_TYPE (decl));
3587 *non_integral_constant_expression_p = true;
3589 return r;
3591 else
3593 bool dependent_p = type_dependent_expression_p (decl);
3595 /* If the declaration was explicitly qualified indicate
3596 that. The semantics of `A::f(3)' are different than
3597 `f(3)' if `f' is virtual. */
3598 *idk = (scope
3599 ? CP_ID_KIND_QUALIFIED
3600 : (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3601 ? CP_ID_KIND_TEMPLATE_ID
3602 : (dependent_p
3603 ? CP_ID_KIND_UNQUALIFIED_DEPENDENT
3604 : CP_ID_KIND_UNQUALIFIED)));
3606 if (dependent_p
3607 && DECL_P (decl)
3608 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (decl)))
3609 /* Dependent type attributes on the decl mean that the TREE_TYPE is
3610 wrong, so just return the identifier. */
3611 return id_expression;
3613 if (TREE_CODE (decl) == NAMESPACE_DECL)
3615 error ("use of namespace %qD as expression", decl);
3616 return error_mark_node;
3618 else if (DECL_CLASS_TEMPLATE_P (decl))
3620 error ("use of class template %qT as expression", decl);
3621 return error_mark_node;
3623 else if (TREE_CODE (decl) == TREE_LIST)
3625 /* Ambiguous reference to base members. */
3626 error ("request for member %qD is ambiguous in "
3627 "multiple inheritance lattice", id_expression);
3628 print_candidates (decl);
3629 return error_mark_node;
3632 /* Mark variable-like entities as used. Functions are similarly
3633 marked either below or after overload resolution. */
3634 if ((VAR_P (decl)
3635 || TREE_CODE (decl) == PARM_DECL
3636 || TREE_CODE (decl) == CONST_DECL
3637 || TREE_CODE (decl) == RESULT_DECL)
3638 && !mark_used (decl))
3639 return error_mark_node;
3641 /* Only certain kinds of names are allowed in constant
3642 expression. Template parameters have already
3643 been handled above. */
3644 if (! error_operand_p (decl)
3645 && !dependent_p
3646 && integral_constant_expression_p
3647 && ! decl_constant_var_p (decl)
3648 && TREE_CODE (decl) != CONST_DECL
3649 && ! builtin_valid_in_constant_expr_p (decl))
3651 if (!allow_non_integral_constant_expression_p)
3653 error ("%qD cannot appear in a constant-expression", decl);
3654 return error_mark_node;
3656 *non_integral_constant_expression_p = true;
3659 tree wrap;
3660 if (VAR_P (decl)
3661 && !cp_unevaluated_operand
3662 && !processing_template_decl
3663 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl))
3664 && CP_DECL_THREAD_LOCAL_P (decl)
3665 && (wrap = get_tls_wrapper_fn (decl)))
3667 /* Replace an evaluated use of the thread_local variable with
3668 a call to its wrapper. */
3669 decl = build_cxx_call (wrap, 0, NULL, tf_warning_or_error);
3671 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3672 && !dependent_p
3673 && variable_template_p (TREE_OPERAND (decl, 0)))
3675 decl = finish_template_variable (decl);
3676 mark_used (decl);
3677 decl = convert_from_reference (decl);
3679 else if (scope)
3681 if (TREE_CODE (decl) == SCOPE_REF)
3683 gcc_assert (same_type_p (scope, TREE_OPERAND (decl, 0)));
3684 decl = TREE_OPERAND (decl, 1);
3687 decl = (adjust_result_of_qualified_name_lookup
3688 (decl, scope, current_nonlambda_class_type()));
3690 if (TREE_CODE (decl) == FUNCTION_DECL)
3691 mark_used (decl);
3693 if (TYPE_P (scope))
3694 decl = finish_qualified_id_expr (scope,
3695 decl,
3696 done,
3697 address_p,
3698 template_p,
3699 template_arg_p,
3700 tf_warning_or_error);
3701 else
3702 decl = convert_from_reference (decl);
3704 else if (TREE_CODE (decl) == FIELD_DECL)
3706 /* Since SCOPE is NULL here, this is an unqualified name.
3707 Access checking has been performed during name lookup
3708 already. Turn off checking to avoid duplicate errors. */
3709 push_deferring_access_checks (dk_no_check);
3710 decl = finish_non_static_data_member (decl, NULL_TREE,
3711 /*qualifying_scope=*/NULL_TREE);
3712 pop_deferring_access_checks ();
3714 else if (is_overloaded_fn (decl))
3716 tree first_fn = get_first_fn (decl);
3718 if (TREE_CODE (first_fn) == TEMPLATE_DECL)
3719 first_fn = DECL_TEMPLATE_RESULT (first_fn);
3721 /* [basic.def.odr]: "A function whose name appears as a
3722 potentially-evaluated expression is odr-used if it is the unique
3723 lookup result".
3725 But only mark it if it's a complete postfix-expression; in a call,
3726 ADL might select a different function, and we'll call mark_used in
3727 build_over_call. */
3728 if (done
3729 && !really_overloaded_fn (decl)
3730 && !mark_used (first_fn))
3731 return error_mark_node;
3733 if (!template_arg_p
3734 && TREE_CODE (first_fn) == FUNCTION_DECL
3735 && DECL_FUNCTION_MEMBER_P (first_fn)
3736 && !shared_member_p (decl))
3738 /* A set of member functions. */
3739 decl = maybe_dummy_object (DECL_CONTEXT (first_fn), 0);
3740 return finish_class_member_access_expr (decl, id_expression,
3741 /*template_p=*/false,
3742 tf_warning_or_error);
3745 decl = baselink_for_fns (decl);
3747 else
3749 if (DECL_P (decl) && DECL_NONLOCAL (decl)
3750 && DECL_CLASS_SCOPE_P (decl))
3752 tree context = context_for_name_lookup (decl);
3753 if (context != current_class_type)
3755 tree path = currently_open_derived_class (context);
3756 perform_or_defer_access_check (TYPE_BINFO (path),
3757 decl, decl,
3758 tf_warning_or_error);
3762 decl = convert_from_reference (decl);
3766 return cp_expr (decl, location);
3769 /* Implement the __typeof keyword: Return the type of EXPR, suitable for
3770 use as a type-specifier. */
3772 tree
3773 finish_typeof (tree expr)
3775 tree type;
3777 if (type_dependent_expression_p (expr))
3779 type = cxx_make_type (TYPEOF_TYPE);
3780 TYPEOF_TYPE_EXPR (type) = expr;
3781 SET_TYPE_STRUCTURAL_EQUALITY (type);
3783 return type;
3786 expr = mark_type_use (expr);
3788 type = unlowered_expr_type (expr);
3790 if (!type || type == unknown_type_node)
3792 error ("type of %qE is unknown", expr);
3793 return error_mark_node;
3796 return type;
3799 /* Implement the __underlying_type keyword: Return the underlying
3800 type of TYPE, suitable for use as a type-specifier. */
3802 tree
3803 finish_underlying_type (tree type)
3805 tree underlying_type;
3807 if (processing_template_decl)
3809 underlying_type = cxx_make_type (UNDERLYING_TYPE);
3810 UNDERLYING_TYPE_TYPE (underlying_type) = type;
3811 SET_TYPE_STRUCTURAL_EQUALITY (underlying_type);
3813 return underlying_type;
3816 if (!complete_type_or_else (type, NULL_TREE))
3817 return error_mark_node;
3819 if (TREE_CODE (type) != ENUMERAL_TYPE)
3821 error ("%qT is not an enumeration type", type);
3822 return error_mark_node;
3825 underlying_type = ENUM_UNDERLYING_TYPE (type);
3827 /* Fixup necessary in this case because ENUM_UNDERLYING_TYPE
3828 includes TYPE_MIN_VALUE and TYPE_MAX_VALUE information.
3829 See finish_enum_value_list for details. */
3830 if (!ENUM_FIXED_UNDERLYING_TYPE_P (type))
3831 underlying_type
3832 = c_common_type_for_mode (TYPE_MODE (underlying_type),
3833 TYPE_UNSIGNED (underlying_type));
3835 return underlying_type;
3838 /* Implement the __direct_bases keyword: Return the direct base classes
3839 of type */
3841 tree
3842 calculate_direct_bases (tree type)
3844 vec<tree, va_gc> *vector = make_tree_vector();
3845 tree bases_vec = NULL_TREE;
3846 vec<tree, va_gc> *base_binfos;
3847 tree binfo;
3848 unsigned i;
3850 complete_type (type);
3852 if (!NON_UNION_CLASS_TYPE_P (type))
3853 return make_tree_vec (0);
3855 base_binfos = BINFO_BASE_BINFOS (TYPE_BINFO (type));
3857 /* Virtual bases are initialized first */
3858 for (i = 0; base_binfos->iterate (i, &binfo); i++)
3860 if (BINFO_VIRTUAL_P (binfo))
3862 vec_safe_push (vector, binfo);
3866 /* Now non-virtuals */
3867 for (i = 0; base_binfos->iterate (i, &binfo); i++)
3869 if (!BINFO_VIRTUAL_P (binfo))
3871 vec_safe_push (vector, binfo);
3876 bases_vec = make_tree_vec (vector->length ());
3878 for (i = 0; i < vector->length (); ++i)
3880 TREE_VEC_ELT (bases_vec, i) = BINFO_TYPE ((*vector)[i]);
3882 return bases_vec;
3885 /* Implement the __bases keyword: Return the base classes
3886 of type */
3888 /* Find morally non-virtual base classes by walking binfo hierarchy */
3889 /* Virtual base classes are handled separately in finish_bases */
3891 static tree
3892 dfs_calculate_bases_pre (tree binfo, void * /*data_*/)
3894 /* Don't walk bases of virtual bases */
3895 return BINFO_VIRTUAL_P (binfo) ? dfs_skip_bases : NULL_TREE;
3898 static tree
3899 dfs_calculate_bases_post (tree binfo, void *data_)
3901 vec<tree, va_gc> **data = ((vec<tree, va_gc> **) data_);
3902 if (!BINFO_VIRTUAL_P (binfo))
3904 vec_safe_push (*data, BINFO_TYPE (binfo));
3906 return NULL_TREE;
3909 /* Calculates the morally non-virtual base classes of a class */
3910 static vec<tree, va_gc> *
3911 calculate_bases_helper (tree type)
3913 vec<tree, va_gc> *vector = make_tree_vector();
3915 /* Now add non-virtual base classes in order of construction */
3916 if (TYPE_BINFO (type))
3917 dfs_walk_all (TYPE_BINFO (type),
3918 dfs_calculate_bases_pre, dfs_calculate_bases_post, &vector);
3919 return vector;
3922 tree
3923 calculate_bases (tree type)
3925 vec<tree, va_gc> *vector = make_tree_vector();
3926 tree bases_vec = NULL_TREE;
3927 unsigned i;
3928 vec<tree, va_gc> *vbases;
3929 vec<tree, va_gc> *nonvbases;
3930 tree binfo;
3932 complete_type (type);
3934 if (!NON_UNION_CLASS_TYPE_P (type))
3935 return make_tree_vec (0);
3937 /* First go through virtual base classes */
3938 for (vbases = CLASSTYPE_VBASECLASSES (type), i = 0;
3939 vec_safe_iterate (vbases, i, &binfo); i++)
3941 vec<tree, va_gc> *vbase_bases;
3942 vbase_bases = calculate_bases_helper (BINFO_TYPE (binfo));
3943 vec_safe_splice (vector, vbase_bases);
3944 release_tree_vector (vbase_bases);
3947 /* Now for the non-virtual bases */
3948 nonvbases = calculate_bases_helper (type);
3949 vec_safe_splice (vector, nonvbases);
3950 release_tree_vector (nonvbases);
3952 /* Note that during error recovery vector->length can even be zero. */
3953 if (vector->length () > 1)
3955 /* Last element is entire class, so don't copy */
3956 bases_vec = make_tree_vec (vector->length() - 1);
3958 for (i = 0; i < vector->length () - 1; ++i)
3959 TREE_VEC_ELT (bases_vec, i) = (*vector)[i];
3961 else
3962 bases_vec = make_tree_vec (0);
3964 release_tree_vector (vector);
3965 return bases_vec;
3968 tree
3969 finish_bases (tree type, bool direct)
3971 tree bases = NULL_TREE;
3973 if (!processing_template_decl)
3975 /* Parameter packs can only be used in templates */
3976 error ("Parameter pack __bases only valid in template declaration");
3977 return error_mark_node;
3980 bases = cxx_make_type (BASES);
3981 BASES_TYPE (bases) = type;
3982 BASES_DIRECT (bases) = direct;
3983 SET_TYPE_STRUCTURAL_EQUALITY (bases);
3985 return bases;
3988 /* Perform C++-specific checks for __builtin_offsetof before calling
3989 fold_offsetof. */
3991 tree
3992 finish_offsetof (tree object_ptr, tree expr, location_t loc)
3994 /* If we're processing a template, we can't finish the semantics yet.
3995 Otherwise we can fold the entire expression now. */
3996 if (processing_template_decl)
3998 expr = build2 (OFFSETOF_EXPR, size_type_node, expr, object_ptr);
3999 SET_EXPR_LOCATION (expr, loc);
4000 return expr;
4003 if (TREE_CODE (expr) == PSEUDO_DTOR_EXPR)
4005 error ("cannot apply %<offsetof%> to destructor %<~%T%>",
4006 TREE_OPERAND (expr, 2));
4007 return error_mark_node;
4009 if (TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE
4010 || TREE_CODE (TREE_TYPE (expr)) == METHOD_TYPE
4011 || TREE_TYPE (expr) == unknown_type_node)
4013 if (INDIRECT_REF_P (expr))
4014 error ("second operand of %<offsetof%> is neither a single "
4015 "identifier nor a sequence of member accesses and "
4016 "array references");
4017 else
4019 if (TREE_CODE (expr) == COMPONENT_REF
4020 || TREE_CODE (expr) == COMPOUND_EXPR)
4021 expr = TREE_OPERAND (expr, 1);
4022 error ("cannot apply %<offsetof%> to member function %qD", expr);
4024 return error_mark_node;
4026 if (REFERENCE_REF_P (expr))
4027 expr = TREE_OPERAND (expr, 0);
4028 if (!complete_type_or_else (TREE_TYPE (TREE_TYPE (object_ptr)), object_ptr))
4029 return error_mark_node;
4030 if (warn_invalid_offsetof
4031 && CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (object_ptr)))
4032 && CLASSTYPE_NON_STD_LAYOUT (TREE_TYPE (TREE_TYPE (object_ptr)))
4033 && cp_unevaluated_operand == 0)
4034 pedwarn (loc, OPT_Winvalid_offsetof,
4035 "offsetof within non-standard-layout type %qT is undefined",
4036 TREE_TYPE (TREE_TYPE (object_ptr)));
4037 return fold_offsetof (expr);
4040 /* Replace the AGGR_INIT_EXPR at *TP with an equivalent CALL_EXPR. This
4041 function is broken out from the above for the benefit of the tree-ssa
4042 project. */
4044 void
4045 simplify_aggr_init_expr (tree *tp)
4047 tree aggr_init_expr = *tp;
4049 /* Form an appropriate CALL_EXPR. */
4050 tree fn = AGGR_INIT_EXPR_FN (aggr_init_expr);
4051 tree slot = AGGR_INIT_EXPR_SLOT (aggr_init_expr);
4052 tree type = TREE_TYPE (slot);
4054 tree call_expr;
4055 enum style_t { ctor, arg, pcc } style;
4057 if (AGGR_INIT_VIA_CTOR_P (aggr_init_expr))
4058 style = ctor;
4059 #ifdef PCC_STATIC_STRUCT_RETURN
4060 else if (1)
4061 style = pcc;
4062 #endif
4063 else
4065 gcc_assert (TREE_ADDRESSABLE (type));
4066 style = arg;
4069 call_expr = build_call_array_loc (input_location,
4070 TREE_TYPE (TREE_TYPE (TREE_TYPE (fn))),
4072 aggr_init_expr_nargs (aggr_init_expr),
4073 AGGR_INIT_EXPR_ARGP (aggr_init_expr));
4074 TREE_NOTHROW (call_expr) = TREE_NOTHROW (aggr_init_expr);
4075 CALL_FROM_THUNK_P (call_expr) = AGGR_INIT_FROM_THUNK_P (aggr_init_expr);
4076 CALL_EXPR_OPERATOR_SYNTAX (call_expr)
4077 = CALL_EXPR_OPERATOR_SYNTAX (aggr_init_expr);
4078 CALL_EXPR_ORDERED_ARGS (call_expr) = CALL_EXPR_ORDERED_ARGS (aggr_init_expr);
4079 CALL_EXPR_REVERSE_ARGS (call_expr) = CALL_EXPR_REVERSE_ARGS (aggr_init_expr);
4080 /* Preserve CILK_SPAWN flag. */
4081 EXPR_CILK_SPAWN (call_expr) = EXPR_CILK_SPAWN (aggr_init_expr);
4083 if (style == ctor)
4085 /* Replace the first argument to the ctor with the address of the
4086 slot. */
4087 cxx_mark_addressable (slot);
4088 CALL_EXPR_ARG (call_expr, 0) =
4089 build1 (ADDR_EXPR, build_pointer_type (type), slot);
4091 else if (style == arg)
4093 /* Just mark it addressable here, and leave the rest to
4094 expand_call{,_inline}. */
4095 cxx_mark_addressable (slot);
4096 CALL_EXPR_RETURN_SLOT_OPT (call_expr) = true;
4097 call_expr = build2 (INIT_EXPR, TREE_TYPE (call_expr), slot, call_expr);
4099 else if (style == pcc)
4101 /* If we're using the non-reentrant PCC calling convention, then we
4102 need to copy the returned value out of the static buffer into the
4103 SLOT. */
4104 push_deferring_access_checks (dk_no_check);
4105 call_expr = build_aggr_init (slot, call_expr,
4106 DIRECT_BIND | LOOKUP_ONLYCONVERTING,
4107 tf_warning_or_error);
4108 pop_deferring_access_checks ();
4109 call_expr = build2 (COMPOUND_EXPR, TREE_TYPE (slot), call_expr, slot);
4112 if (AGGR_INIT_ZERO_FIRST (aggr_init_expr))
4114 tree init = build_zero_init (type, NULL_TREE,
4115 /*static_storage_p=*/false);
4116 init = build2 (INIT_EXPR, void_type_node, slot, init);
4117 call_expr = build2 (COMPOUND_EXPR, TREE_TYPE (call_expr),
4118 init, call_expr);
4121 *tp = call_expr;
4124 /* Emit all thunks to FN that should be emitted when FN is emitted. */
4126 void
4127 emit_associated_thunks (tree fn)
4129 /* When we use vcall offsets, we emit thunks with the virtual
4130 functions to which they thunk. The whole point of vcall offsets
4131 is so that you can know statically the entire set of thunks that
4132 will ever be needed for a given virtual function, thereby
4133 enabling you to output all the thunks with the function itself. */
4134 if (DECL_VIRTUAL_P (fn)
4135 /* Do not emit thunks for extern template instantiations. */
4136 && ! DECL_REALLY_EXTERN (fn))
4138 tree thunk;
4140 for (thunk = DECL_THUNKS (fn); thunk; thunk = DECL_CHAIN (thunk))
4142 if (!THUNK_ALIAS (thunk))
4144 use_thunk (thunk, /*emit_p=*/1);
4145 if (DECL_RESULT_THUNK_P (thunk))
4147 tree probe;
4149 for (probe = DECL_THUNKS (thunk);
4150 probe; probe = DECL_CHAIN (probe))
4151 use_thunk (probe, /*emit_p=*/1);
4154 else
4155 gcc_assert (!DECL_THUNKS (thunk));
4160 /* Generate RTL for FN. */
4162 bool
4163 expand_or_defer_fn_1 (tree fn)
4165 /* When the parser calls us after finishing the body of a template
4166 function, we don't really want to expand the body. */
4167 if (processing_template_decl)
4169 /* Normally, collection only occurs in rest_of_compilation. So,
4170 if we don't collect here, we never collect junk generated
4171 during the processing of templates until we hit a
4172 non-template function. It's not safe to do this inside a
4173 nested class, though, as the parser may have local state that
4174 is not a GC root. */
4175 if (!function_depth)
4176 ggc_collect ();
4177 return false;
4180 gcc_assert (DECL_SAVED_TREE (fn));
4182 /* We make a decision about linkage for these functions at the end
4183 of the compilation. Until that point, we do not want the back
4184 end to output them -- but we do want it to see the bodies of
4185 these functions so that it can inline them as appropriate. */
4186 if (DECL_DECLARED_INLINE_P (fn) || DECL_IMPLICIT_INSTANTIATION (fn))
4188 if (DECL_INTERFACE_KNOWN (fn))
4189 /* We've already made a decision as to how this function will
4190 be handled. */;
4191 else if (!at_eof)
4192 tentative_decl_linkage (fn);
4193 else
4194 import_export_decl (fn);
4196 /* If the user wants us to keep all inline functions, then mark
4197 this function as needed so that finish_file will make sure to
4198 output it later. Similarly, all dllexport'd functions must
4199 be emitted; there may be callers in other DLLs. */
4200 if (DECL_DECLARED_INLINE_P (fn)
4201 && !DECL_REALLY_EXTERN (fn)
4202 && (flag_keep_inline_functions
4203 || (flag_keep_inline_dllexport
4204 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (fn)))))
4206 mark_needed (fn);
4207 DECL_EXTERNAL (fn) = 0;
4211 /* If this is a constructor or destructor body, we have to clone
4212 it. */
4213 if (maybe_clone_body (fn))
4215 /* We don't want to process FN again, so pretend we've written
4216 it out, even though we haven't. */
4217 TREE_ASM_WRITTEN (fn) = 1;
4218 /* If this is a constexpr function, keep DECL_SAVED_TREE. */
4219 if (!DECL_DECLARED_CONSTEXPR_P (fn))
4220 DECL_SAVED_TREE (fn) = NULL_TREE;
4221 return false;
4224 /* There's no reason to do any of the work here if we're only doing
4225 semantic analysis; this code just generates RTL. */
4226 if (flag_syntax_only)
4227 return false;
4229 return true;
4232 void
4233 expand_or_defer_fn (tree fn)
4235 if (expand_or_defer_fn_1 (fn))
4237 function_depth++;
4239 /* Expand or defer, at the whim of the compilation unit manager. */
4240 cgraph_node::finalize_function (fn, function_depth > 1);
4241 emit_associated_thunks (fn);
4243 function_depth--;
4247 struct nrv_data
4249 nrv_data () : visited (37) {}
4251 tree var;
4252 tree result;
4253 hash_table<nofree_ptr_hash <tree_node> > visited;
4256 /* Helper function for walk_tree, used by finalize_nrv below. */
4258 static tree
4259 finalize_nrv_r (tree* tp, int* walk_subtrees, void* data)
4261 struct nrv_data *dp = (struct nrv_data *)data;
4262 tree_node **slot;
4264 /* No need to walk into types. There wouldn't be any need to walk into
4265 non-statements, except that we have to consider STMT_EXPRs. */
4266 if (TYPE_P (*tp))
4267 *walk_subtrees = 0;
4268 /* Change all returns to just refer to the RESULT_DECL; this is a nop,
4269 but differs from using NULL_TREE in that it indicates that we care
4270 about the value of the RESULT_DECL. */
4271 else if (TREE_CODE (*tp) == RETURN_EXPR)
4272 TREE_OPERAND (*tp, 0) = dp->result;
4273 /* Change all cleanups for the NRV to only run when an exception is
4274 thrown. */
4275 else if (TREE_CODE (*tp) == CLEANUP_STMT
4276 && CLEANUP_DECL (*tp) == dp->var)
4277 CLEANUP_EH_ONLY (*tp) = 1;
4278 /* Replace the DECL_EXPR for the NRV with an initialization of the
4279 RESULT_DECL, if needed. */
4280 else if (TREE_CODE (*tp) == DECL_EXPR
4281 && DECL_EXPR_DECL (*tp) == dp->var)
4283 tree init;
4284 if (DECL_INITIAL (dp->var)
4285 && DECL_INITIAL (dp->var) != error_mark_node)
4286 init = build2 (INIT_EXPR, void_type_node, dp->result,
4287 DECL_INITIAL (dp->var));
4288 else
4289 init = build_empty_stmt (EXPR_LOCATION (*tp));
4290 DECL_INITIAL (dp->var) = NULL_TREE;
4291 SET_EXPR_LOCATION (init, EXPR_LOCATION (*tp));
4292 *tp = init;
4294 /* And replace all uses of the NRV with the RESULT_DECL. */
4295 else if (*tp == dp->var)
4296 *tp = dp->result;
4298 /* Avoid walking into the same tree more than once. Unfortunately, we
4299 can't just use walk_tree_without duplicates because it would only call
4300 us for the first occurrence of dp->var in the function body. */
4301 slot = dp->visited.find_slot (*tp, INSERT);
4302 if (*slot)
4303 *walk_subtrees = 0;
4304 else
4305 *slot = *tp;
4307 /* Keep iterating. */
4308 return NULL_TREE;
4311 /* Called from finish_function to implement the named return value
4312 optimization by overriding all the RETURN_EXPRs and pertinent
4313 CLEANUP_STMTs and replacing all occurrences of VAR with RESULT, the
4314 RESULT_DECL for the function. */
4316 void
4317 finalize_nrv (tree *tp, tree var, tree result)
4319 struct nrv_data data;
4321 /* Copy name from VAR to RESULT. */
4322 DECL_NAME (result) = DECL_NAME (var);
4323 /* Don't forget that we take its address. */
4324 TREE_ADDRESSABLE (result) = TREE_ADDRESSABLE (var);
4325 /* Finally set DECL_VALUE_EXPR to avoid assigning
4326 a stack slot at -O0 for the original var and debug info
4327 uses RESULT location for VAR. */
4328 SET_DECL_VALUE_EXPR (var, result);
4329 DECL_HAS_VALUE_EXPR_P (var) = 1;
4331 data.var = var;
4332 data.result = result;
4333 cp_walk_tree (tp, finalize_nrv_r, &data, 0);
4336 /* Create CP_OMP_CLAUSE_INFO for clause C. Returns true if it is invalid. */
4338 bool
4339 cxx_omp_create_clause_info (tree c, tree type, bool need_default_ctor,
4340 bool need_copy_ctor, bool need_copy_assignment,
4341 bool need_dtor)
4343 int save_errorcount = errorcount;
4344 tree info, t;
4346 /* Always allocate 3 elements for simplicity. These are the
4347 function decls for the ctor, dtor, and assignment op.
4348 This layout is known to the three lang hooks,
4349 cxx_omp_clause_default_init, cxx_omp_clause_copy_init,
4350 and cxx_omp_clause_assign_op. */
4351 info = make_tree_vec (3);
4352 CP_OMP_CLAUSE_INFO (c) = info;
4354 if (need_default_ctor || need_copy_ctor)
4356 if (need_default_ctor)
4357 t = get_default_ctor (type);
4358 else
4359 t = get_copy_ctor (type, tf_warning_or_error);
4361 if (t && !trivial_fn_p (t))
4362 TREE_VEC_ELT (info, 0) = t;
4365 if (need_dtor && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
4366 TREE_VEC_ELT (info, 1) = get_dtor (type, tf_warning_or_error);
4368 if (need_copy_assignment)
4370 t = get_copy_assign (type);
4372 if (t && !trivial_fn_p (t))
4373 TREE_VEC_ELT (info, 2) = t;
4376 return errorcount != save_errorcount;
4379 /* If DECL is DECL_OMP_PRIVATIZED_MEMBER, return corresponding
4380 FIELD_DECL, otherwise return DECL itself. */
4382 static tree
4383 omp_clause_decl_field (tree decl)
4385 if (VAR_P (decl)
4386 && DECL_HAS_VALUE_EXPR_P (decl)
4387 && DECL_ARTIFICIAL (decl)
4388 && DECL_LANG_SPECIFIC (decl)
4389 && DECL_OMP_PRIVATIZED_MEMBER (decl))
4391 tree f = DECL_VALUE_EXPR (decl);
4392 if (TREE_CODE (f) == INDIRECT_REF)
4393 f = TREE_OPERAND (f, 0);
4394 if (TREE_CODE (f) == COMPONENT_REF)
4396 f = TREE_OPERAND (f, 1);
4397 gcc_assert (TREE_CODE (f) == FIELD_DECL);
4398 return f;
4401 return NULL_TREE;
4404 /* Adjust DECL if needed for printing using %qE. */
4406 static tree
4407 omp_clause_printable_decl (tree decl)
4409 tree t = omp_clause_decl_field (decl);
4410 if (t)
4411 return t;
4412 return decl;
4415 /* For a FIELD_DECL F and corresponding DECL_OMP_PRIVATIZED_MEMBER
4416 VAR_DECL T that doesn't need a DECL_EXPR added, record it for
4417 privatization. */
4419 static void
4420 omp_note_field_privatization (tree f, tree t)
4422 if (!omp_private_member_map)
4423 omp_private_member_map = new hash_map<tree, tree>;
4424 tree &v = omp_private_member_map->get_or_insert (f);
4425 if (v == NULL_TREE)
4427 v = t;
4428 omp_private_member_vec.safe_push (f);
4429 /* Signal that we don't want to create DECL_EXPR for this dummy var. */
4430 omp_private_member_vec.safe_push (integer_zero_node);
4434 /* Privatize FIELD_DECL T, return corresponding DECL_OMP_PRIVATIZED_MEMBER
4435 dummy VAR_DECL. */
4437 tree
4438 omp_privatize_field (tree t, bool shared)
4440 tree m = finish_non_static_data_member (t, NULL_TREE, NULL_TREE);
4441 if (m == error_mark_node)
4442 return error_mark_node;
4443 if (!omp_private_member_map && !shared)
4444 omp_private_member_map = new hash_map<tree, tree>;
4445 if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE)
4447 gcc_assert (TREE_CODE (m) == INDIRECT_REF);
4448 m = TREE_OPERAND (m, 0);
4450 tree vb = NULL_TREE;
4451 tree &v = shared ? vb : omp_private_member_map->get_or_insert (t);
4452 if (v == NULL_TREE)
4454 v = create_temporary_var (TREE_TYPE (m));
4455 retrofit_lang_decl (v);
4456 DECL_OMP_PRIVATIZED_MEMBER (v) = 1;
4457 SET_DECL_VALUE_EXPR (v, m);
4458 DECL_HAS_VALUE_EXPR_P (v) = 1;
4459 if (!shared)
4460 omp_private_member_vec.safe_push (t);
4462 return v;
4465 /* Helper function for handle_omp_array_sections. Called recursively
4466 to handle multiple array-section-subscripts. C is the clause,
4467 T current expression (initially OMP_CLAUSE_DECL), which is either
4468 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
4469 expression if specified, TREE_VALUE length expression if specified,
4470 TREE_CHAIN is what it has been specified after, or some decl.
4471 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
4472 set to true if any of the array-section-subscript could have length
4473 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
4474 first array-section-subscript which is known not to have length
4475 of one. Given say:
4476 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
4477 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
4478 all are or may have length of 1, array-section-subscript [:2] is the
4479 first one known not to have length 1. For array-section-subscript
4480 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
4481 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
4482 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
4483 case though, as some lengths could be zero. */
4485 static tree
4486 handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
4487 bool &maybe_zero_len, unsigned int &first_non_one,
4488 enum c_omp_region_type ort)
4490 tree ret, low_bound, length, type;
4491 if (TREE_CODE (t) != TREE_LIST)
4493 if (error_operand_p (t))
4494 return error_mark_node;
4495 if (REFERENCE_REF_P (t)
4496 && TREE_CODE (TREE_OPERAND (t, 0)) == COMPONENT_REF)
4497 t = TREE_OPERAND (t, 0);
4498 ret = t;
4499 if (TREE_CODE (t) == COMPONENT_REF
4500 && ort == C_ORT_OMP
4501 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
4502 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO
4503 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FROM)
4504 && !type_dependent_expression_p (t))
4506 if (TREE_CODE (TREE_OPERAND (t, 1)) == FIELD_DECL
4507 && DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
4509 error_at (OMP_CLAUSE_LOCATION (c),
4510 "bit-field %qE in %qs clause",
4511 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4512 return error_mark_node;
4514 while (TREE_CODE (t) == COMPONENT_REF)
4516 if (TREE_TYPE (TREE_OPERAND (t, 0))
4517 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == UNION_TYPE)
4519 error_at (OMP_CLAUSE_LOCATION (c),
4520 "%qE is a member of a union", t);
4521 return error_mark_node;
4523 t = TREE_OPERAND (t, 0);
4525 if (REFERENCE_REF_P (t))
4526 t = TREE_OPERAND (t, 0);
4528 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
4530 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
4531 return NULL_TREE;
4532 if (DECL_P (t))
4533 error_at (OMP_CLAUSE_LOCATION (c),
4534 "%qD is not a variable in %qs clause", t,
4535 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4536 else
4537 error_at (OMP_CLAUSE_LOCATION (c),
4538 "%qE is not a variable in %qs clause", t,
4539 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4540 return error_mark_node;
4542 else if (TREE_CODE (t) == PARM_DECL
4543 && DECL_ARTIFICIAL (t)
4544 && DECL_NAME (t) == this_identifier)
4546 error_at (OMP_CLAUSE_LOCATION (c),
4547 "%<this%> allowed in OpenMP only in %<declare simd%>"
4548 " clauses");
4549 return error_mark_node;
4551 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
4552 && VAR_P (t) && CP_DECL_THREAD_LOCAL_P (t))
4554 error_at (OMP_CLAUSE_LOCATION (c),
4555 "%qD is threadprivate variable in %qs clause", t,
4556 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4557 return error_mark_node;
4559 if (type_dependent_expression_p (ret))
4560 return NULL_TREE;
4561 ret = convert_from_reference (ret);
4562 return ret;
4565 if (ort == C_ORT_OMP
4566 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
4567 && TREE_CODE (TREE_CHAIN (t)) == FIELD_DECL)
4568 TREE_CHAIN (t) = omp_privatize_field (TREE_CHAIN (t), false);
4569 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
4570 maybe_zero_len, first_non_one, ort);
4571 if (ret == error_mark_node || ret == NULL_TREE)
4572 return ret;
4574 type = TREE_TYPE (ret);
4575 low_bound = TREE_PURPOSE (t);
4576 length = TREE_VALUE (t);
4577 if ((low_bound && type_dependent_expression_p (low_bound))
4578 || (length && type_dependent_expression_p (length)))
4579 return NULL_TREE;
4581 if (low_bound == error_mark_node || length == error_mark_node)
4582 return error_mark_node;
4584 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
4586 error_at (OMP_CLAUSE_LOCATION (c),
4587 "low bound %qE of array section does not have integral type",
4588 low_bound);
4589 return error_mark_node;
4591 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
4593 error_at (OMP_CLAUSE_LOCATION (c),
4594 "length %qE of array section does not have integral type",
4595 length);
4596 return error_mark_node;
4598 if (low_bound)
4599 low_bound = mark_rvalue_use (low_bound);
4600 if (length)
4601 length = mark_rvalue_use (length);
4602 /* We need to reduce to real constant-values for checks below. */
4603 if (length)
4604 length = fold_simple (length);
4605 if (low_bound)
4606 low_bound = fold_simple (low_bound);
4607 if (low_bound
4608 && TREE_CODE (low_bound) == INTEGER_CST
4609 && TYPE_PRECISION (TREE_TYPE (low_bound))
4610 > TYPE_PRECISION (sizetype))
4611 low_bound = fold_convert (sizetype, low_bound);
4612 if (length
4613 && TREE_CODE (length) == INTEGER_CST
4614 && TYPE_PRECISION (TREE_TYPE (length))
4615 > TYPE_PRECISION (sizetype))
4616 length = fold_convert (sizetype, length);
4617 if (low_bound == NULL_TREE)
4618 low_bound = integer_zero_node;
4620 if (length != NULL_TREE)
4622 if (!integer_nonzerop (length))
4624 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
4625 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
4627 if (integer_zerop (length))
4629 error_at (OMP_CLAUSE_LOCATION (c),
4630 "zero length array section in %qs clause",
4631 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4632 return error_mark_node;
4635 else
4636 maybe_zero_len = true;
4638 if (first_non_one == types.length ()
4639 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
4640 first_non_one++;
4642 if (TREE_CODE (type) == ARRAY_TYPE)
4644 if (length == NULL_TREE
4645 && (TYPE_DOMAIN (type) == NULL_TREE
4646 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
4648 error_at (OMP_CLAUSE_LOCATION (c),
4649 "for unknown bound array type length expression must "
4650 "be specified");
4651 return error_mark_node;
4653 if (TREE_CODE (low_bound) == INTEGER_CST
4654 && tree_int_cst_sgn (low_bound) == -1)
4656 error_at (OMP_CLAUSE_LOCATION (c),
4657 "negative low bound in array section in %qs clause",
4658 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4659 return error_mark_node;
4661 if (length != NULL_TREE
4662 && TREE_CODE (length) == INTEGER_CST
4663 && tree_int_cst_sgn (length) == -1)
4665 error_at (OMP_CLAUSE_LOCATION (c),
4666 "negative length in array section in %qs clause",
4667 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4668 return error_mark_node;
4670 if (TYPE_DOMAIN (type)
4671 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
4672 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
4673 == INTEGER_CST)
4675 tree size
4676 = fold_convert (sizetype, TYPE_MAX_VALUE (TYPE_DOMAIN (type)));
4677 size = size_binop (PLUS_EXPR, size, size_one_node);
4678 if (TREE_CODE (low_bound) == INTEGER_CST)
4680 if (tree_int_cst_lt (size, low_bound))
4682 error_at (OMP_CLAUSE_LOCATION (c),
4683 "low bound %qE above array section size "
4684 "in %qs clause", low_bound,
4685 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4686 return error_mark_node;
4688 if (tree_int_cst_equal (size, low_bound))
4690 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
4691 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
4693 error_at (OMP_CLAUSE_LOCATION (c),
4694 "zero length array section in %qs clause",
4695 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4696 return error_mark_node;
4698 maybe_zero_len = true;
4700 else if (length == NULL_TREE
4701 && first_non_one == types.length ()
4702 && tree_int_cst_equal
4703 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
4704 low_bound))
4705 first_non_one++;
4707 else if (length == NULL_TREE)
4709 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
4710 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION)
4711 maybe_zero_len = true;
4712 if (first_non_one == types.length ())
4713 first_non_one++;
4715 if (length && TREE_CODE (length) == INTEGER_CST)
4717 if (tree_int_cst_lt (size, length))
4719 error_at (OMP_CLAUSE_LOCATION (c),
4720 "length %qE above array section size "
4721 "in %qs clause", length,
4722 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4723 return error_mark_node;
4725 if (TREE_CODE (low_bound) == INTEGER_CST)
4727 tree lbpluslen
4728 = size_binop (PLUS_EXPR,
4729 fold_convert (sizetype, low_bound),
4730 fold_convert (sizetype, length));
4731 if (TREE_CODE (lbpluslen) == INTEGER_CST
4732 && tree_int_cst_lt (size, lbpluslen))
4734 error_at (OMP_CLAUSE_LOCATION (c),
4735 "high bound %qE above array section size "
4736 "in %qs clause", lbpluslen,
4737 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4738 return error_mark_node;
4743 else if (length == NULL_TREE)
4745 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
4746 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION)
4747 maybe_zero_len = true;
4748 if (first_non_one == types.length ())
4749 first_non_one++;
4752 /* For [lb:] we will need to evaluate lb more than once. */
4753 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
4755 tree lb = cp_save_expr (low_bound);
4756 if (lb != low_bound)
4758 TREE_PURPOSE (t) = lb;
4759 low_bound = lb;
4763 else if (TREE_CODE (type) == POINTER_TYPE)
4765 if (length == NULL_TREE)
4767 error_at (OMP_CLAUSE_LOCATION (c),
4768 "for pointer type length expression must be specified");
4769 return error_mark_node;
4771 if (length != NULL_TREE
4772 && TREE_CODE (length) == INTEGER_CST
4773 && tree_int_cst_sgn (length) == -1)
4775 error_at (OMP_CLAUSE_LOCATION (c),
4776 "negative length in array section in %qs clause",
4777 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4778 return error_mark_node;
4780 /* If there is a pointer type anywhere but in the very first
4781 array-section-subscript, the array section can't be contiguous. */
4782 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
4783 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
4785 error_at (OMP_CLAUSE_LOCATION (c),
4786 "array section is not contiguous in %qs clause",
4787 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4788 return error_mark_node;
4791 else
4793 error_at (OMP_CLAUSE_LOCATION (c),
4794 "%qE does not have pointer or array type", ret);
4795 return error_mark_node;
4797 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
4798 types.safe_push (TREE_TYPE (ret));
4799 /* We will need to evaluate lb more than once. */
4800 tree lb = cp_save_expr (low_bound);
4801 if (lb != low_bound)
4803 TREE_PURPOSE (t) = lb;
4804 low_bound = lb;
4806 ret = grok_array_decl (OMP_CLAUSE_LOCATION (c), ret, low_bound, false);
4807 return ret;
4810 /* Handle array sections for clause C. */
4812 static bool
4813 handle_omp_array_sections (tree c, enum c_omp_region_type ort)
4815 bool maybe_zero_len = false;
4816 unsigned int first_non_one = 0;
4817 auto_vec<tree, 10> types;
4818 tree first = handle_omp_array_sections_1 (c, OMP_CLAUSE_DECL (c), types,
4819 maybe_zero_len, first_non_one,
4820 ort);
4821 if (first == error_mark_node)
4822 return true;
4823 if (first == NULL_TREE)
4824 return false;
4825 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
4827 tree t = OMP_CLAUSE_DECL (c);
4828 tree tem = NULL_TREE;
4829 if (processing_template_decl)
4830 return false;
4831 /* Need to evaluate side effects in the length expressions
4832 if any. */
4833 while (TREE_CODE (t) == TREE_LIST)
4835 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
4837 if (tem == NULL_TREE)
4838 tem = TREE_VALUE (t);
4839 else
4840 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
4841 TREE_VALUE (t), tem);
4843 t = TREE_CHAIN (t);
4845 if (tem)
4846 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
4847 OMP_CLAUSE_DECL (c) = first;
4849 else
4851 unsigned int num = types.length (), i;
4852 tree t, side_effects = NULL_TREE, size = NULL_TREE;
4853 tree condition = NULL_TREE;
4855 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
4856 maybe_zero_len = true;
4857 if (processing_template_decl && maybe_zero_len)
4858 return false;
4860 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
4861 t = TREE_CHAIN (t))
4863 tree low_bound = TREE_PURPOSE (t);
4864 tree length = TREE_VALUE (t);
4866 i--;
4867 if (low_bound
4868 && TREE_CODE (low_bound) == INTEGER_CST
4869 && TYPE_PRECISION (TREE_TYPE (low_bound))
4870 > TYPE_PRECISION (sizetype))
4871 low_bound = fold_convert (sizetype, low_bound);
4872 if (length
4873 && TREE_CODE (length) == INTEGER_CST
4874 && TYPE_PRECISION (TREE_TYPE (length))
4875 > TYPE_PRECISION (sizetype))
4876 length = fold_convert (sizetype, length);
4877 if (low_bound == NULL_TREE)
4878 low_bound = integer_zero_node;
4879 if (!maybe_zero_len && i > first_non_one)
4881 if (integer_nonzerop (low_bound))
4882 goto do_warn_noncontiguous;
4883 if (length != NULL_TREE
4884 && TREE_CODE (length) == INTEGER_CST
4885 && TYPE_DOMAIN (types[i])
4886 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
4887 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
4888 == INTEGER_CST)
4890 tree size;
4891 size = size_binop (PLUS_EXPR,
4892 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
4893 size_one_node);
4894 if (!tree_int_cst_equal (length, size))
4896 do_warn_noncontiguous:
4897 error_at (OMP_CLAUSE_LOCATION (c),
4898 "array section is not contiguous in %qs "
4899 "clause",
4900 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4901 return true;
4904 if (!processing_template_decl
4905 && length != NULL_TREE
4906 && TREE_SIDE_EFFECTS (length))
4908 if (side_effects == NULL_TREE)
4909 side_effects = length;
4910 else
4911 side_effects = build2 (COMPOUND_EXPR,
4912 TREE_TYPE (side_effects),
4913 length, side_effects);
4916 else if (processing_template_decl)
4917 continue;
4918 else
4920 tree l;
4922 if (i > first_non_one
4923 && ((length && integer_nonzerop (length))
4924 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION))
4925 continue;
4926 if (length)
4927 l = fold_convert (sizetype, length);
4928 else
4930 l = size_binop (PLUS_EXPR,
4931 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
4932 size_one_node);
4933 l = size_binop (MINUS_EXPR, l,
4934 fold_convert (sizetype, low_bound));
4936 if (i > first_non_one)
4938 l = fold_build2 (NE_EXPR, boolean_type_node, l,
4939 size_zero_node);
4940 if (condition == NULL_TREE)
4941 condition = l;
4942 else
4943 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
4944 l, condition);
4946 else if (size == NULL_TREE)
4948 size = size_in_bytes (TREE_TYPE (types[i]));
4949 tree eltype = TREE_TYPE (types[num - 1]);
4950 while (TREE_CODE (eltype) == ARRAY_TYPE)
4951 eltype = TREE_TYPE (eltype);
4952 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
4953 size = size_binop (EXACT_DIV_EXPR, size,
4954 size_in_bytes (eltype));
4955 size = size_binop (MULT_EXPR, size, l);
4956 if (condition)
4957 size = fold_build3 (COND_EXPR, sizetype, condition,
4958 size, size_zero_node);
4960 else
4961 size = size_binop (MULT_EXPR, size, l);
4964 if (!processing_template_decl)
4966 if (side_effects)
4967 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
4968 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
4970 size = size_binop (MINUS_EXPR, size, size_one_node);
4971 tree index_type = build_index_type (size);
4972 tree eltype = TREE_TYPE (first);
4973 while (TREE_CODE (eltype) == ARRAY_TYPE)
4974 eltype = TREE_TYPE (eltype);
4975 tree type = build_array_type (eltype, index_type);
4976 tree ptype = build_pointer_type (eltype);
4977 if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE
4978 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (t))))
4979 t = convert_from_reference (t);
4980 else if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
4981 t = build_fold_addr_expr (t);
4982 tree t2 = build_fold_addr_expr (first);
4983 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
4984 ptrdiff_type_node, t2);
4985 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
4986 ptrdiff_type_node, t2,
4987 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
4988 ptrdiff_type_node, t));
4989 if (tree_fits_shwi_p (t2))
4990 t = build2 (MEM_REF, type, t,
4991 build_int_cst (ptype, tree_to_shwi (t2)));
4992 else
4994 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
4995 sizetype, t2);
4996 t = build2_loc (OMP_CLAUSE_LOCATION (c), POINTER_PLUS_EXPR,
4997 TREE_TYPE (t), t, t2);
4998 t = build2 (MEM_REF, type, t, build_int_cst (ptype, 0));
5000 OMP_CLAUSE_DECL (c) = t;
5001 return false;
5003 OMP_CLAUSE_DECL (c) = first;
5004 OMP_CLAUSE_SIZE (c) = size;
5005 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
5006 || (TREE_CODE (t) == COMPONENT_REF
5007 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE))
5008 return false;
5009 if (ort == C_ORT_OMP || ort == C_ORT_ACC)
5010 switch (OMP_CLAUSE_MAP_KIND (c))
5012 case GOMP_MAP_ALLOC:
5013 case GOMP_MAP_TO:
5014 case GOMP_MAP_FROM:
5015 case GOMP_MAP_TOFROM:
5016 case GOMP_MAP_ALWAYS_TO:
5017 case GOMP_MAP_ALWAYS_FROM:
5018 case GOMP_MAP_ALWAYS_TOFROM:
5019 case GOMP_MAP_RELEASE:
5020 case GOMP_MAP_DELETE:
5021 case GOMP_MAP_FORCE_TO:
5022 case GOMP_MAP_FORCE_FROM:
5023 case GOMP_MAP_FORCE_TOFROM:
5024 case GOMP_MAP_FORCE_PRESENT:
5025 OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (c) = 1;
5026 break;
5027 default:
5028 break;
5030 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
5031 OMP_CLAUSE_MAP);
5032 if ((ort & C_ORT_OMP_DECLARE_SIMD) != C_ORT_OMP && ort != C_ORT_ACC)
5033 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_POINTER);
5034 else if (TREE_CODE (t) == COMPONENT_REF)
5035 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ALWAYS_POINTER);
5036 else if (REFERENCE_REF_P (t)
5037 && TREE_CODE (TREE_OPERAND (t, 0)) == COMPONENT_REF)
5039 t = TREE_OPERAND (t, 0);
5040 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ALWAYS_POINTER);
5042 else
5043 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_FIRSTPRIVATE_POINTER);
5044 if (OMP_CLAUSE_MAP_KIND (c2) != GOMP_MAP_FIRSTPRIVATE_POINTER
5045 && !cxx_mark_addressable (t))
5046 return false;
5047 OMP_CLAUSE_DECL (c2) = t;
5048 t = build_fold_addr_expr (first);
5049 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
5050 ptrdiff_type_node, t);
5051 tree ptr = OMP_CLAUSE_DECL (c2);
5052 ptr = convert_from_reference (ptr);
5053 if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
5054 ptr = build_fold_addr_expr (ptr);
5055 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
5056 ptrdiff_type_node, t,
5057 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
5058 ptrdiff_type_node, ptr));
5059 OMP_CLAUSE_SIZE (c2) = t;
5060 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
5061 OMP_CLAUSE_CHAIN (c) = c2;
5062 ptr = OMP_CLAUSE_DECL (c2);
5063 if (OMP_CLAUSE_MAP_KIND (c2) != GOMP_MAP_FIRSTPRIVATE_POINTER
5064 && TREE_CODE (TREE_TYPE (ptr)) == REFERENCE_TYPE
5065 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (ptr))))
5067 tree c3 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
5068 OMP_CLAUSE_MAP);
5069 OMP_CLAUSE_SET_MAP_KIND (c3, OMP_CLAUSE_MAP_KIND (c2));
5070 OMP_CLAUSE_DECL (c3) = ptr;
5071 if (OMP_CLAUSE_MAP_KIND (c2) == GOMP_MAP_ALWAYS_POINTER)
5072 OMP_CLAUSE_DECL (c2) = build_simple_mem_ref (ptr);
5073 else
5074 OMP_CLAUSE_DECL (c2) = convert_from_reference (ptr);
5075 OMP_CLAUSE_SIZE (c3) = size_zero_node;
5076 OMP_CLAUSE_CHAIN (c3) = OMP_CLAUSE_CHAIN (c2);
5077 OMP_CLAUSE_CHAIN (c2) = c3;
5081 return false;
5084 /* Return identifier to look up for omp declare reduction. */
5086 tree
5087 omp_reduction_id (enum tree_code reduction_code, tree reduction_id, tree type)
5089 const char *p = NULL;
5090 const char *m = NULL;
5091 switch (reduction_code)
5093 case PLUS_EXPR:
5094 case MULT_EXPR:
5095 case MINUS_EXPR:
5096 case BIT_AND_EXPR:
5097 case BIT_XOR_EXPR:
5098 case BIT_IOR_EXPR:
5099 case TRUTH_ANDIF_EXPR:
5100 case TRUTH_ORIF_EXPR:
5101 reduction_id = ovl_op_identifier (false, reduction_code);
5102 break;
5103 case MIN_EXPR:
5104 p = "min";
5105 break;
5106 case MAX_EXPR:
5107 p = "max";
5108 break;
5109 default:
5110 break;
5113 if (p == NULL)
5115 if (TREE_CODE (reduction_id) != IDENTIFIER_NODE)
5116 return error_mark_node;
5117 p = IDENTIFIER_POINTER (reduction_id);
5120 if (type != NULL_TREE)
5121 m = mangle_type_string (TYPE_MAIN_VARIANT (type));
5123 const char prefix[] = "omp declare reduction ";
5124 size_t lenp = sizeof (prefix);
5125 if (strncmp (p, prefix, lenp - 1) == 0)
5126 lenp = 1;
5127 size_t len = strlen (p);
5128 size_t lenm = m ? strlen (m) + 1 : 0;
5129 char *name = XALLOCAVEC (char, lenp + len + lenm);
5130 if (lenp > 1)
5131 memcpy (name, prefix, lenp - 1);
5132 memcpy (name + lenp - 1, p, len + 1);
5133 if (m)
5135 name[lenp + len - 1] = '~';
5136 memcpy (name + lenp + len, m, lenm);
5138 return get_identifier (name);
5141 /* Lookup OpenMP UDR ID for TYPE, return the corresponding artificial
5142 FUNCTION_DECL or NULL_TREE if not found. */
5144 static tree
5145 omp_reduction_lookup (location_t loc, tree id, tree type, tree *baselinkp,
5146 vec<tree> *ambiguousp)
5148 tree orig_id = id;
5149 tree baselink = NULL_TREE;
5150 if (identifier_p (id))
5152 cp_id_kind idk;
5153 bool nonint_cst_expression_p;
5154 const char *error_msg;
5155 id = omp_reduction_id (ERROR_MARK, id, type);
5156 tree decl = lookup_name (id);
5157 if (decl == NULL_TREE)
5158 decl = error_mark_node;
5159 id = finish_id_expression (id, decl, NULL_TREE, &idk, false, true,
5160 &nonint_cst_expression_p, false, true, false,
5161 false, &error_msg, loc);
5162 if (idk == CP_ID_KIND_UNQUALIFIED
5163 && identifier_p (id))
5165 vec<tree, va_gc> *args = NULL;
5166 vec_safe_push (args, build_reference_type (type));
5167 id = perform_koenig_lookup (id, args, tf_none);
5170 else if (TREE_CODE (id) == SCOPE_REF)
5171 id = lookup_qualified_name (TREE_OPERAND (id, 0),
5172 omp_reduction_id (ERROR_MARK,
5173 TREE_OPERAND (id, 1),
5174 type),
5175 false, false);
5176 tree fns = id;
5177 id = NULL_TREE;
5178 if (fns && is_overloaded_fn (fns))
5180 for (lkp_iterator iter (get_fns (fns)); iter; ++iter)
5182 tree fndecl = *iter;
5183 if (TREE_CODE (fndecl) == FUNCTION_DECL)
5185 tree argtype = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
5186 if (same_type_p (TREE_TYPE (argtype), type))
5188 id = fndecl;
5189 break;
5194 if (id && BASELINK_P (fns))
5196 if (baselinkp)
5197 *baselinkp = fns;
5198 else
5199 baselink = fns;
5203 if (!id && CLASS_TYPE_P (type) && TYPE_BINFO (type))
5205 vec<tree> ambiguous = vNULL;
5206 tree binfo = TYPE_BINFO (type), base_binfo, ret = NULL_TREE;
5207 unsigned int ix;
5208 if (ambiguousp == NULL)
5209 ambiguousp = &ambiguous;
5210 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
5212 id = omp_reduction_lookup (loc, orig_id, BINFO_TYPE (base_binfo),
5213 baselinkp ? baselinkp : &baselink,
5214 ambiguousp);
5215 if (id == NULL_TREE)
5216 continue;
5217 if (!ambiguousp->is_empty ())
5218 ambiguousp->safe_push (id);
5219 else if (ret != NULL_TREE)
5221 ambiguousp->safe_push (ret);
5222 ambiguousp->safe_push (id);
5223 ret = NULL_TREE;
5225 else
5226 ret = id;
5228 if (ambiguousp != &ambiguous)
5229 return ret;
5230 if (!ambiguous.is_empty ())
5232 const char *str = _("candidates are:");
5233 unsigned int idx;
5234 tree udr;
5235 error_at (loc, "user defined reduction lookup is ambiguous");
5236 FOR_EACH_VEC_ELT (ambiguous, idx, udr)
5238 inform (DECL_SOURCE_LOCATION (udr), "%s %#qD", str, udr);
5239 if (idx == 0)
5240 str = get_spaces (str);
5242 ambiguous.release ();
5243 ret = error_mark_node;
5244 baselink = NULL_TREE;
5246 id = ret;
5248 if (id && baselink)
5249 perform_or_defer_access_check (BASELINK_BINFO (baselink),
5250 id, id, tf_warning_or_error);
5251 return id;
5254 /* Helper function for cp_parser_omp_declare_reduction_exprs
5255 and tsubst_omp_udr.
5256 Remove CLEANUP_STMT for data (omp_priv variable).
5257 Also append INIT_EXPR for DECL_INITIAL of omp_priv after its
5258 DECL_EXPR. */
5260 tree
5261 cp_remove_omp_priv_cleanup_stmt (tree *tp, int *walk_subtrees, void *data)
5263 if (TYPE_P (*tp))
5264 *walk_subtrees = 0;
5265 else if (TREE_CODE (*tp) == CLEANUP_STMT && CLEANUP_DECL (*tp) == (tree) data)
5266 *tp = CLEANUP_BODY (*tp);
5267 else if (TREE_CODE (*tp) == DECL_EXPR)
5269 tree decl = DECL_EXPR_DECL (*tp);
5270 if (!processing_template_decl
5271 && decl == (tree) data
5272 && DECL_INITIAL (decl)
5273 && DECL_INITIAL (decl) != error_mark_node)
5275 tree list = NULL_TREE;
5276 append_to_statement_list_force (*tp, &list);
5277 tree init_expr = build2 (INIT_EXPR, void_type_node,
5278 decl, DECL_INITIAL (decl));
5279 DECL_INITIAL (decl) = NULL_TREE;
5280 append_to_statement_list_force (init_expr, &list);
5281 *tp = list;
5284 return NULL_TREE;
5287 /* Data passed from cp_check_omp_declare_reduction to
5288 cp_check_omp_declare_reduction_r. */
5290 struct cp_check_omp_declare_reduction_data
5292 location_t loc;
5293 tree stmts[7];
5294 bool combiner_p;
5297 /* Helper function for cp_check_omp_declare_reduction, called via
5298 cp_walk_tree. */
5300 static tree
5301 cp_check_omp_declare_reduction_r (tree *tp, int *, void *data)
5303 struct cp_check_omp_declare_reduction_data *udr_data
5304 = (struct cp_check_omp_declare_reduction_data *) data;
5305 if (SSA_VAR_P (*tp)
5306 && !DECL_ARTIFICIAL (*tp)
5307 && *tp != DECL_EXPR_DECL (udr_data->stmts[udr_data->combiner_p ? 0 : 3])
5308 && *tp != DECL_EXPR_DECL (udr_data->stmts[udr_data->combiner_p ? 1 : 4]))
5310 location_t loc = udr_data->loc;
5311 if (udr_data->combiner_p)
5312 error_at (loc, "%<#pragma omp declare reduction%> combiner refers to "
5313 "variable %qD which is not %<omp_out%> nor %<omp_in%>",
5314 *tp);
5315 else
5316 error_at (loc, "%<#pragma omp declare reduction%> initializer refers "
5317 "to variable %qD which is not %<omp_priv%> nor "
5318 "%<omp_orig%>",
5319 *tp);
5320 return *tp;
5322 return NULL_TREE;
5325 /* Diagnose violation of OpenMP #pragma omp declare reduction restrictions. */
5327 void
5328 cp_check_omp_declare_reduction (tree udr)
5330 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (udr)));
5331 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
5332 type = TREE_TYPE (type);
5333 int i;
5334 location_t loc = DECL_SOURCE_LOCATION (udr);
5336 if (type == error_mark_node)
5337 return;
5338 if (ARITHMETIC_TYPE_P (type))
5340 static enum tree_code predef_codes[]
5341 = { PLUS_EXPR, MULT_EXPR, MINUS_EXPR, BIT_AND_EXPR, BIT_XOR_EXPR,
5342 BIT_IOR_EXPR, TRUTH_ANDIF_EXPR, TRUTH_ORIF_EXPR };
5343 for (i = 0; i < 8; i++)
5345 tree id = omp_reduction_id (predef_codes[i], NULL_TREE, NULL_TREE);
5346 const char *n1 = IDENTIFIER_POINTER (DECL_NAME (udr));
5347 const char *n2 = IDENTIFIER_POINTER (id);
5348 if (strncmp (n1, n2, IDENTIFIER_LENGTH (id)) == 0
5349 && (n1[IDENTIFIER_LENGTH (id)] == '~'
5350 || n1[IDENTIFIER_LENGTH (id)] == '\0'))
5351 break;
5354 if (i == 8
5355 && TREE_CODE (type) != COMPLEX_EXPR)
5357 const char prefix_minmax[] = "omp declare reduction m";
5358 size_t prefix_size = sizeof (prefix_minmax) - 1;
5359 const char *n = IDENTIFIER_POINTER (DECL_NAME (udr));
5360 if (strncmp (IDENTIFIER_POINTER (DECL_NAME (udr)),
5361 prefix_minmax, prefix_size) == 0
5362 && ((n[prefix_size] == 'i' && n[prefix_size + 1] == 'n')
5363 || (n[prefix_size] == 'a' && n[prefix_size + 1] == 'x'))
5364 && (n[prefix_size + 2] == '~' || n[prefix_size + 2] == '\0'))
5365 i = 0;
5367 if (i < 8)
5369 error_at (loc, "predeclared arithmetic type %qT in "
5370 "%<#pragma omp declare reduction%>", type);
5371 return;
5374 else if (TREE_CODE (type) == FUNCTION_TYPE
5375 || TREE_CODE (type) == METHOD_TYPE
5376 || TREE_CODE (type) == ARRAY_TYPE)
5378 error_at (loc, "function or array type %qT in "
5379 "%<#pragma omp declare reduction%>", type);
5380 return;
5382 else if (TREE_CODE (type) == REFERENCE_TYPE)
5384 error_at (loc, "reference type %qT in %<#pragma omp declare reduction%>",
5385 type);
5386 return;
5388 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
5390 error_at (loc, "const, volatile or __restrict qualified type %qT in "
5391 "%<#pragma omp declare reduction%>", type);
5392 return;
5395 tree body = DECL_SAVED_TREE (udr);
5396 if (body == NULL_TREE || TREE_CODE (body) != STATEMENT_LIST)
5397 return;
5399 tree_stmt_iterator tsi;
5400 struct cp_check_omp_declare_reduction_data data;
5401 memset (data.stmts, 0, sizeof data.stmts);
5402 for (i = 0, tsi = tsi_start (body);
5403 i < 7 && !tsi_end_p (tsi);
5404 i++, tsi_next (&tsi))
5405 data.stmts[i] = tsi_stmt (tsi);
5406 data.loc = loc;
5407 gcc_assert (tsi_end_p (tsi));
5408 if (i >= 3)
5410 gcc_assert (TREE_CODE (data.stmts[0]) == DECL_EXPR
5411 && TREE_CODE (data.stmts[1]) == DECL_EXPR);
5412 if (TREE_NO_WARNING (DECL_EXPR_DECL (data.stmts[0])))
5413 return;
5414 data.combiner_p = true;
5415 if (cp_walk_tree (&data.stmts[2], cp_check_omp_declare_reduction_r,
5416 &data, NULL))
5417 TREE_NO_WARNING (DECL_EXPR_DECL (data.stmts[0])) = 1;
5419 if (i >= 6)
5421 gcc_assert (TREE_CODE (data.stmts[3]) == DECL_EXPR
5422 && TREE_CODE (data.stmts[4]) == DECL_EXPR);
5423 data.combiner_p = false;
5424 if (cp_walk_tree (&data.stmts[5], cp_check_omp_declare_reduction_r,
5425 &data, NULL)
5426 || cp_walk_tree (&DECL_INITIAL (DECL_EXPR_DECL (data.stmts[3])),
5427 cp_check_omp_declare_reduction_r, &data, NULL))
5428 TREE_NO_WARNING (DECL_EXPR_DECL (data.stmts[0])) = 1;
5429 if (i == 7)
5430 gcc_assert (TREE_CODE (data.stmts[6]) == DECL_EXPR);
5434 /* Helper function of finish_omp_clauses. Clone STMT as if we were making
5435 an inline call. But, remap
5436 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
5437 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
5439 static tree
5440 clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
5441 tree decl, tree placeholder)
5443 copy_body_data id;
5444 hash_map<tree, tree> decl_map;
5446 decl_map.put (omp_decl1, placeholder);
5447 decl_map.put (omp_decl2, decl);
5448 memset (&id, 0, sizeof (id));
5449 id.src_fn = DECL_CONTEXT (omp_decl1);
5450 id.dst_fn = current_function_decl;
5451 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
5452 id.decl_map = &decl_map;
5454 id.copy_decl = copy_decl_no_change;
5455 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
5456 id.transform_new_cfg = true;
5457 id.transform_return_to_modify = false;
5458 id.transform_lang_insert_block = NULL;
5459 id.eh_lp_nr = 0;
5460 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
5461 return stmt;
5464 /* Helper function of finish_omp_clauses, called via cp_walk_tree.
5465 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
5467 static tree
5468 find_omp_placeholder_r (tree *tp, int *, void *data)
5470 if (*tp == (tree) data)
5471 return *tp;
5472 return NULL_TREE;
5475 /* Helper function of finish_omp_clauses. Handle OMP_CLAUSE_REDUCTION C.
5476 Return true if there is some error and the clause should be removed. */
5478 static bool
5479 finish_omp_reduction_clause (tree c, bool *need_default_ctor, bool *need_dtor)
5481 tree t = OMP_CLAUSE_DECL (c);
5482 bool predefined = false;
5483 if (TREE_CODE (t) == TREE_LIST)
5485 gcc_assert (processing_template_decl);
5486 return false;
5488 tree type = TREE_TYPE (t);
5489 if (TREE_CODE (t) == MEM_REF)
5490 type = TREE_TYPE (type);
5491 if (TREE_CODE (type) == REFERENCE_TYPE)
5492 type = TREE_TYPE (type);
5493 if (TREE_CODE (type) == ARRAY_TYPE)
5495 tree oatype = type;
5496 gcc_assert (TREE_CODE (t) != MEM_REF);
5497 while (TREE_CODE (type) == ARRAY_TYPE)
5498 type = TREE_TYPE (type);
5499 if (!processing_template_decl)
5501 t = require_complete_type (t);
5502 if (t == error_mark_node)
5503 return true;
5504 tree size = size_binop (EXACT_DIV_EXPR, TYPE_SIZE_UNIT (oatype),
5505 TYPE_SIZE_UNIT (type));
5506 if (integer_zerop (size))
5508 error ("%qE in %<reduction%> clause is a zero size array",
5509 omp_clause_printable_decl (t));
5510 return true;
5512 size = size_binop (MINUS_EXPR, size, size_one_node);
5513 tree index_type = build_index_type (size);
5514 tree atype = build_array_type (type, index_type);
5515 tree ptype = build_pointer_type (type);
5516 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
5517 t = build_fold_addr_expr (t);
5518 t = build2 (MEM_REF, atype, t, build_int_cst (ptype, 0));
5519 OMP_CLAUSE_DECL (c) = t;
5522 if (type == error_mark_node)
5523 return true;
5524 else if (ARITHMETIC_TYPE_P (type))
5525 switch (OMP_CLAUSE_REDUCTION_CODE (c))
5527 case PLUS_EXPR:
5528 case MULT_EXPR:
5529 case MINUS_EXPR:
5530 predefined = true;
5531 break;
5532 case MIN_EXPR:
5533 case MAX_EXPR:
5534 if (TREE_CODE (type) == COMPLEX_TYPE)
5535 break;
5536 predefined = true;
5537 break;
5538 case BIT_AND_EXPR:
5539 case BIT_IOR_EXPR:
5540 case BIT_XOR_EXPR:
5541 if (FLOAT_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
5542 break;
5543 predefined = true;
5544 break;
5545 case TRUTH_ANDIF_EXPR:
5546 case TRUTH_ORIF_EXPR:
5547 if (FLOAT_TYPE_P (type))
5548 break;
5549 predefined = true;
5550 break;
5551 default:
5552 break;
5554 else if (TYPE_READONLY (type))
5556 error ("%qE has const type for %<reduction%>",
5557 omp_clause_printable_decl (t));
5558 return true;
5560 else if (!processing_template_decl)
5562 t = require_complete_type (t);
5563 if (t == error_mark_node)
5564 return true;
5565 OMP_CLAUSE_DECL (c) = t;
5568 if (predefined)
5570 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = NULL_TREE;
5571 return false;
5573 else if (processing_template_decl)
5574 return false;
5576 tree id = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
5578 type = TYPE_MAIN_VARIANT (type);
5579 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = NULL_TREE;
5580 if (id == NULL_TREE)
5581 id = omp_reduction_id (OMP_CLAUSE_REDUCTION_CODE (c),
5582 NULL_TREE, NULL_TREE);
5583 id = omp_reduction_lookup (OMP_CLAUSE_LOCATION (c), id, type, NULL, NULL);
5584 if (id)
5586 if (id == error_mark_node)
5587 return true;
5588 mark_used (id);
5589 tree body = DECL_SAVED_TREE (id);
5590 if (!body)
5591 return true;
5592 if (TREE_CODE (body) == STATEMENT_LIST)
5594 tree_stmt_iterator tsi;
5595 tree placeholder = NULL_TREE, decl_placeholder = NULL_TREE;
5596 int i;
5597 tree stmts[7];
5598 tree atype = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (id)));
5599 atype = TREE_TYPE (atype);
5600 bool need_static_cast = !same_type_p (type, atype);
5601 memset (stmts, 0, sizeof stmts);
5602 for (i = 0, tsi = tsi_start (body);
5603 i < 7 && !tsi_end_p (tsi);
5604 i++, tsi_next (&tsi))
5605 stmts[i] = tsi_stmt (tsi);
5606 gcc_assert (tsi_end_p (tsi));
5608 if (i >= 3)
5610 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
5611 && TREE_CODE (stmts[1]) == DECL_EXPR);
5612 placeholder = build_lang_decl (VAR_DECL, NULL_TREE, type);
5613 DECL_ARTIFICIAL (placeholder) = 1;
5614 DECL_IGNORED_P (placeholder) = 1;
5615 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
5616 if (TREE_CODE (t) == MEM_REF)
5618 decl_placeholder = build_lang_decl (VAR_DECL, NULL_TREE,
5619 type);
5620 DECL_ARTIFICIAL (decl_placeholder) = 1;
5621 DECL_IGNORED_P (decl_placeholder) = 1;
5622 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c) = decl_placeholder;
5624 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[0])))
5625 cxx_mark_addressable (placeholder);
5626 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[1]))
5627 && TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c)))
5628 != REFERENCE_TYPE)
5629 cxx_mark_addressable (decl_placeholder ? decl_placeholder
5630 : OMP_CLAUSE_DECL (c));
5631 tree omp_out = placeholder;
5632 tree omp_in = decl_placeholder ? decl_placeholder
5633 : convert_from_reference (OMP_CLAUSE_DECL (c));
5634 if (need_static_cast)
5636 tree rtype = build_reference_type (atype);
5637 omp_out = build_static_cast (rtype, omp_out,
5638 tf_warning_or_error);
5639 omp_in = build_static_cast (rtype, omp_in,
5640 tf_warning_or_error);
5641 if (omp_out == error_mark_node || omp_in == error_mark_node)
5642 return true;
5643 omp_out = convert_from_reference (omp_out);
5644 omp_in = convert_from_reference (omp_in);
5646 OMP_CLAUSE_REDUCTION_MERGE (c)
5647 = clone_omp_udr (stmts[2], DECL_EXPR_DECL (stmts[0]),
5648 DECL_EXPR_DECL (stmts[1]), omp_in, omp_out);
5650 if (i >= 6)
5652 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
5653 && TREE_CODE (stmts[4]) == DECL_EXPR);
5654 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[3])))
5655 cxx_mark_addressable (decl_placeholder ? decl_placeholder
5656 : OMP_CLAUSE_DECL (c));
5657 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[4])))
5658 cxx_mark_addressable (placeholder);
5659 tree omp_priv = decl_placeholder ? decl_placeholder
5660 : convert_from_reference (OMP_CLAUSE_DECL (c));
5661 tree omp_orig = placeholder;
5662 if (need_static_cast)
5664 if (i == 7)
5666 error_at (OMP_CLAUSE_LOCATION (c),
5667 "user defined reduction with constructor "
5668 "initializer for base class %qT", atype);
5669 return true;
5671 tree rtype = build_reference_type (atype);
5672 omp_priv = build_static_cast (rtype, omp_priv,
5673 tf_warning_or_error);
5674 omp_orig = build_static_cast (rtype, omp_orig,
5675 tf_warning_or_error);
5676 if (omp_priv == error_mark_node
5677 || omp_orig == error_mark_node)
5678 return true;
5679 omp_priv = convert_from_reference (omp_priv);
5680 omp_orig = convert_from_reference (omp_orig);
5682 if (i == 6)
5683 *need_default_ctor = true;
5684 OMP_CLAUSE_REDUCTION_INIT (c)
5685 = clone_omp_udr (stmts[5], DECL_EXPR_DECL (stmts[4]),
5686 DECL_EXPR_DECL (stmts[3]),
5687 omp_priv, omp_orig);
5688 if (cp_walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
5689 find_omp_placeholder_r, placeholder, NULL))
5690 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
5692 else if (i >= 3)
5694 if (CLASS_TYPE_P (type) && !pod_type_p (type))
5695 *need_default_ctor = true;
5696 else
5698 tree init;
5699 tree v = decl_placeholder ? decl_placeholder
5700 : convert_from_reference (t);
5701 if (AGGREGATE_TYPE_P (TREE_TYPE (v)))
5702 init = build_constructor (TREE_TYPE (v), NULL);
5703 else
5704 init = fold_convert (TREE_TYPE (v), integer_zero_node);
5705 OMP_CLAUSE_REDUCTION_INIT (c)
5706 = build2 (INIT_EXPR, TREE_TYPE (v), v, init);
5711 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
5712 *need_dtor = true;
5713 else
5715 error ("user defined reduction not found for %qE",
5716 omp_clause_printable_decl (t));
5717 return true;
5719 if (TREE_CODE (OMP_CLAUSE_DECL (c)) == MEM_REF)
5720 gcc_assert (TYPE_SIZE_UNIT (type)
5721 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST);
5722 return false;
5725 /* Called from finish_struct_1. linear(this) or linear(this:step)
5726 clauses might not be finalized yet because the class has been incomplete
5727 when parsing #pragma omp declare simd methods. Fix those up now. */
5729 void
5730 finish_omp_declare_simd_methods (tree t)
5732 if (processing_template_decl)
5733 return;
5735 for (tree x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
5737 if (TREE_CODE (TREE_TYPE (x)) != METHOD_TYPE)
5738 continue;
5739 tree ods = lookup_attribute ("omp declare simd", DECL_ATTRIBUTES (x));
5740 if (!ods || !TREE_VALUE (ods))
5741 continue;
5742 for (tree c = TREE_VALUE (TREE_VALUE (ods)); c; c = OMP_CLAUSE_CHAIN (c))
5743 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
5744 && integer_zerop (OMP_CLAUSE_DECL (c))
5745 && OMP_CLAUSE_LINEAR_STEP (c)
5746 && TREE_CODE (TREE_TYPE (OMP_CLAUSE_LINEAR_STEP (c)))
5747 == POINTER_TYPE)
5749 tree s = OMP_CLAUSE_LINEAR_STEP (c);
5750 s = fold_convert_loc (OMP_CLAUSE_LOCATION (c), sizetype, s);
5751 s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MULT_EXPR,
5752 sizetype, s, TYPE_SIZE_UNIT (t));
5753 OMP_CLAUSE_LINEAR_STEP (c) = s;
5758 /* Adjust sink depend clause to take into account pointer offsets.
5760 Return TRUE if there was a problem processing the offset, and the
5761 whole clause should be removed. */
5763 static bool
5764 cp_finish_omp_clause_depend_sink (tree sink_clause)
5766 tree t = OMP_CLAUSE_DECL (sink_clause);
5767 gcc_assert (TREE_CODE (t) == TREE_LIST);
5769 /* Make sure we don't adjust things twice for templates. */
5770 if (processing_template_decl)
5771 return false;
5773 for (; t; t = TREE_CHAIN (t))
5775 tree decl = TREE_VALUE (t);
5776 if (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
5778 tree offset = TREE_PURPOSE (t);
5779 bool neg = wi::neg_p (wi::to_wide (offset));
5780 offset = fold_unary (ABS_EXPR, TREE_TYPE (offset), offset);
5781 decl = mark_rvalue_use (decl);
5782 decl = convert_from_reference (decl);
5783 tree t2 = pointer_int_sum (OMP_CLAUSE_LOCATION (sink_clause),
5784 neg ? MINUS_EXPR : PLUS_EXPR,
5785 decl, offset);
5786 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (sink_clause),
5787 MINUS_EXPR, sizetype,
5788 fold_convert (sizetype, t2),
5789 fold_convert (sizetype, decl));
5790 if (t2 == error_mark_node)
5791 return true;
5792 TREE_PURPOSE (t) = t2;
5795 return false;
5798 /* For all elements of CLAUSES, validate them vs OpenMP constraints.
5799 Remove any elements from the list that are invalid. */
5801 tree
5802 finish_omp_clauses (tree clauses, enum c_omp_region_type ort)
5804 bitmap_head generic_head, firstprivate_head, lastprivate_head;
5805 bitmap_head aligned_head, map_head, map_field_head, oacc_reduction_head;
5806 tree c, t, *pc;
5807 tree safelen = NULL_TREE;
5808 bool branch_seen = false;
5809 bool copyprivate_seen = false;
5810 bool ordered_seen = false;
5811 bool oacc_async = false;
5813 bitmap_obstack_initialize (NULL);
5814 bitmap_initialize (&generic_head, &bitmap_default_obstack);
5815 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
5816 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
5817 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
5818 bitmap_initialize (&map_head, &bitmap_default_obstack);
5819 bitmap_initialize (&map_field_head, &bitmap_default_obstack);
5820 bitmap_initialize (&oacc_reduction_head, &bitmap_default_obstack);
5822 if (ort & C_ORT_ACC)
5823 for (c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
5824 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ASYNC)
5826 oacc_async = true;
5827 break;
5830 for (pc = &clauses, c = clauses; c ; c = *pc)
5832 bool remove = false;
5833 bool field_ok = false;
5835 switch (OMP_CLAUSE_CODE (c))
5837 case OMP_CLAUSE_SHARED:
5838 field_ok = ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP);
5839 goto check_dup_generic;
5840 case OMP_CLAUSE_PRIVATE:
5841 field_ok = ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP);
5842 goto check_dup_generic;
5843 case OMP_CLAUSE_REDUCTION:
5844 field_ok = ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP);
5845 t = OMP_CLAUSE_DECL (c);
5846 if (TREE_CODE (t) == TREE_LIST)
5848 if (handle_omp_array_sections (c, ort))
5850 remove = true;
5851 break;
5853 if (TREE_CODE (t) == TREE_LIST)
5855 while (TREE_CODE (t) == TREE_LIST)
5856 t = TREE_CHAIN (t);
5858 else
5860 gcc_assert (TREE_CODE (t) == MEM_REF);
5861 t = TREE_OPERAND (t, 0);
5862 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
5863 t = TREE_OPERAND (t, 0);
5864 if (TREE_CODE (t) == ADDR_EXPR
5865 || TREE_CODE (t) == INDIRECT_REF)
5866 t = TREE_OPERAND (t, 0);
5868 tree n = omp_clause_decl_field (t);
5869 if (n)
5870 t = n;
5871 goto check_dup_generic_t;
5873 if (oacc_async)
5874 cxx_mark_addressable (t);
5875 goto check_dup_generic;
5876 case OMP_CLAUSE_COPYPRIVATE:
5877 copyprivate_seen = true;
5878 field_ok = ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP);
5879 goto check_dup_generic;
5880 case OMP_CLAUSE_COPYIN:
5881 goto check_dup_generic;
5882 case OMP_CLAUSE_LINEAR:
5883 field_ok = ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP);
5884 t = OMP_CLAUSE_DECL (c);
5885 if (ort != C_ORT_OMP_DECLARE_SIMD
5886 && OMP_CLAUSE_LINEAR_KIND (c) != OMP_CLAUSE_LINEAR_DEFAULT)
5888 error_at (OMP_CLAUSE_LOCATION (c),
5889 "modifier should not be specified in %<linear%> "
5890 "clause on %<simd%> or %<for%> constructs");
5891 OMP_CLAUSE_LINEAR_KIND (c) = OMP_CLAUSE_LINEAR_DEFAULT;
5893 if ((VAR_P (t) || TREE_CODE (t) == PARM_DECL)
5894 && !type_dependent_expression_p (t))
5896 tree type = TREE_TYPE (t);
5897 if ((OMP_CLAUSE_LINEAR_KIND (c) == OMP_CLAUSE_LINEAR_REF
5898 || OMP_CLAUSE_LINEAR_KIND (c) == OMP_CLAUSE_LINEAR_UVAL)
5899 && TREE_CODE (type) != REFERENCE_TYPE)
5901 error ("linear clause with %qs modifier applied to "
5902 "non-reference variable with %qT type",
5903 OMP_CLAUSE_LINEAR_KIND (c) == OMP_CLAUSE_LINEAR_REF
5904 ? "ref" : "uval", TREE_TYPE (t));
5905 remove = true;
5906 break;
5908 if (TREE_CODE (type) == REFERENCE_TYPE)
5909 type = TREE_TYPE (type);
5910 if (ort == C_ORT_CILK)
5912 if (!INTEGRAL_TYPE_P (type)
5913 && !SCALAR_FLOAT_TYPE_P (type)
5914 && TREE_CODE (type) != POINTER_TYPE)
5916 error ("linear clause applied to non-integral, "
5917 "non-floating, non-pointer variable with %qT type",
5918 TREE_TYPE (t));
5919 remove = true;
5920 break;
5923 else if (OMP_CLAUSE_LINEAR_KIND (c) != OMP_CLAUSE_LINEAR_REF)
5925 if (!INTEGRAL_TYPE_P (type)
5926 && TREE_CODE (type) != POINTER_TYPE)
5928 error ("linear clause applied to non-integral non-pointer"
5929 " variable with %qT type", TREE_TYPE (t));
5930 remove = true;
5931 break;
5935 t = OMP_CLAUSE_LINEAR_STEP (c);
5936 if (t == NULL_TREE)
5937 t = integer_one_node;
5938 if (t == error_mark_node)
5940 remove = true;
5941 break;
5943 else if (!type_dependent_expression_p (t)
5944 && !INTEGRAL_TYPE_P (TREE_TYPE (t))
5945 && (ort != C_ORT_OMP_DECLARE_SIMD
5946 || TREE_CODE (t) != PARM_DECL
5947 || TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE
5948 || !INTEGRAL_TYPE_P (TREE_TYPE (TREE_TYPE (t)))))
5950 error ("linear step expression must be integral");
5951 remove = true;
5952 break;
5954 else
5956 t = mark_rvalue_use (t);
5957 if (ort == C_ORT_OMP_DECLARE_SIMD && TREE_CODE (t) == PARM_DECL)
5959 OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c) = 1;
5960 goto check_dup_generic;
5962 if (!processing_template_decl
5963 && (VAR_P (OMP_CLAUSE_DECL (c))
5964 || TREE_CODE (OMP_CLAUSE_DECL (c)) == PARM_DECL))
5966 if (ort == C_ORT_OMP_DECLARE_SIMD)
5968 t = maybe_constant_value (t);
5969 if (TREE_CODE (t) != INTEGER_CST)
5971 error_at (OMP_CLAUSE_LOCATION (c),
5972 "%<linear%> clause step %qE is neither "
5973 "constant nor a parameter", t);
5974 remove = true;
5975 break;
5978 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5979 tree type = TREE_TYPE (OMP_CLAUSE_DECL (c));
5980 if (TREE_CODE (type) == REFERENCE_TYPE)
5981 type = TREE_TYPE (type);
5982 if (OMP_CLAUSE_LINEAR_KIND (c) == OMP_CLAUSE_LINEAR_REF)
5984 type = build_pointer_type (type);
5985 tree d = fold_convert (type, OMP_CLAUSE_DECL (c));
5986 t = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
5987 d, t);
5988 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c),
5989 MINUS_EXPR, sizetype,
5990 fold_convert (sizetype, t),
5991 fold_convert (sizetype, d));
5992 if (t == error_mark_node)
5994 remove = true;
5995 break;
5998 else if (TREE_CODE (type) == POINTER_TYPE
5999 /* Can't multiply the step yet if *this
6000 is still incomplete type. */
6001 && (ort != C_ORT_OMP_DECLARE_SIMD
6002 || TREE_CODE (OMP_CLAUSE_DECL (c)) != PARM_DECL
6003 || !DECL_ARTIFICIAL (OMP_CLAUSE_DECL (c))
6004 || DECL_NAME (OMP_CLAUSE_DECL (c))
6005 != this_identifier
6006 || !TYPE_BEING_DEFINED (TREE_TYPE (type))))
6008 tree d = convert_from_reference (OMP_CLAUSE_DECL (c));
6009 t = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
6010 d, t);
6011 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c),
6012 MINUS_EXPR, sizetype,
6013 fold_convert (sizetype, t),
6014 fold_convert (sizetype, d));
6015 if (t == error_mark_node)
6017 remove = true;
6018 break;
6021 else
6022 t = fold_convert (type, t);
6024 OMP_CLAUSE_LINEAR_STEP (c) = t;
6026 goto check_dup_generic;
6027 check_dup_generic:
6028 t = omp_clause_decl_field (OMP_CLAUSE_DECL (c));
6029 if (t)
6031 if (!remove && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_SHARED)
6032 omp_note_field_privatization (t, OMP_CLAUSE_DECL (c));
6034 else
6035 t = OMP_CLAUSE_DECL (c);
6036 check_dup_generic_t:
6037 if (t == current_class_ptr
6038 && (ort != C_ORT_OMP_DECLARE_SIMD
6039 || (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_LINEAR
6040 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_UNIFORM)))
6042 error ("%<this%> allowed in OpenMP only in %<declare simd%>"
6043 " clauses");
6044 remove = true;
6045 break;
6047 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL
6048 && (!field_ok || TREE_CODE (t) != FIELD_DECL))
6050 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
6051 break;
6052 if (DECL_P (t))
6053 error ("%qD is not a variable in clause %qs", t,
6054 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6055 else
6056 error ("%qE is not a variable in clause %qs", t,
6057 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6058 remove = true;
6060 else if (ort == C_ORT_ACC
6061 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
6063 if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
6065 error ("%qD appears more than once in reduction clauses", t);
6066 remove = true;
6068 else
6069 bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
6071 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
6072 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
6073 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
6075 error ("%qD appears more than once in data clauses", t);
6076 remove = true;
6078 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
6079 && bitmap_bit_p (&map_head, DECL_UID (t)))
6081 if (ort == C_ORT_ACC)
6082 error ("%qD appears more than once in data clauses", t);
6083 else
6084 error ("%qD appears both in data and map clauses", t);
6085 remove = true;
6087 else
6088 bitmap_set_bit (&generic_head, DECL_UID (t));
6089 if (!field_ok)
6090 break;
6091 handle_field_decl:
6092 if (!remove
6093 && TREE_CODE (t) == FIELD_DECL
6094 && t == OMP_CLAUSE_DECL (c)
6095 && ort != C_ORT_ACC)
6097 OMP_CLAUSE_DECL (c)
6098 = omp_privatize_field (t, (OMP_CLAUSE_CODE (c)
6099 == OMP_CLAUSE_SHARED));
6100 if (OMP_CLAUSE_DECL (c) == error_mark_node)
6101 remove = true;
6103 break;
6105 case OMP_CLAUSE_FIRSTPRIVATE:
6106 t = omp_clause_decl_field (OMP_CLAUSE_DECL (c));
6107 if (t)
6108 omp_note_field_privatization (t, OMP_CLAUSE_DECL (c));
6109 else
6110 t = OMP_CLAUSE_DECL (c);
6111 if (ort != C_ORT_ACC && t == current_class_ptr)
6113 error ("%<this%> allowed in OpenMP only in %<declare simd%>"
6114 " clauses");
6115 remove = true;
6116 break;
6118 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL
6119 && ((ort & C_ORT_OMP_DECLARE_SIMD) != C_ORT_OMP
6120 || TREE_CODE (t) != FIELD_DECL))
6122 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
6123 break;
6124 if (DECL_P (t))
6125 error ("%qD is not a variable in clause %<firstprivate%>", t);
6126 else
6127 error ("%qE is not a variable in clause %<firstprivate%>", t);
6128 remove = true;
6130 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
6131 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
6133 error ("%qD appears more than once in data clauses", t);
6134 remove = true;
6136 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
6138 if (ort == C_ORT_ACC)
6139 error ("%qD appears more than once in data clauses", t);
6140 else
6141 error ("%qD appears both in data and map clauses", t);
6142 remove = true;
6144 else
6145 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
6146 goto handle_field_decl;
6148 case OMP_CLAUSE_LASTPRIVATE:
6149 t = omp_clause_decl_field (OMP_CLAUSE_DECL (c));
6150 if (t)
6151 omp_note_field_privatization (t, OMP_CLAUSE_DECL (c));
6152 else
6153 t = OMP_CLAUSE_DECL (c);
6154 if (t == current_class_ptr)
6156 error ("%<this%> allowed in OpenMP only in %<declare simd%>"
6157 " clauses");
6158 remove = true;
6159 break;
6161 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL
6162 && ((ort & C_ORT_OMP_DECLARE_SIMD) != C_ORT_OMP
6163 || TREE_CODE (t) != FIELD_DECL))
6165 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
6166 break;
6167 if (DECL_P (t))
6168 error ("%qD is not a variable in clause %<lastprivate%>", t);
6169 else
6170 error ("%qE is not a variable in clause %<lastprivate%>", t);
6171 remove = true;
6173 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
6174 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
6176 error ("%qD appears more than once in data clauses", t);
6177 remove = true;
6179 else
6180 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
6181 goto handle_field_decl;
6183 case OMP_CLAUSE_IF:
6184 t = OMP_CLAUSE_IF_EXPR (c);
6185 t = maybe_convert_cond (t);
6186 if (t == error_mark_node)
6187 remove = true;
6188 else if (!processing_template_decl)
6189 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6190 OMP_CLAUSE_IF_EXPR (c) = t;
6191 break;
6193 case OMP_CLAUSE_FINAL:
6194 t = OMP_CLAUSE_FINAL_EXPR (c);
6195 t = maybe_convert_cond (t);
6196 if (t == error_mark_node)
6197 remove = true;
6198 else if (!processing_template_decl)
6199 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6200 OMP_CLAUSE_FINAL_EXPR (c) = t;
6201 break;
6203 case OMP_CLAUSE_GANG:
6204 /* Operand 1 is the gang static: argument. */
6205 t = OMP_CLAUSE_OPERAND (c, 1);
6206 if (t != NULL_TREE)
6208 if (t == error_mark_node)
6209 remove = true;
6210 else if (!type_dependent_expression_p (t)
6211 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6213 error ("%<gang%> static expression must be integral");
6214 remove = true;
6216 else
6218 t = mark_rvalue_use (t);
6219 if (!processing_template_decl)
6221 t = maybe_constant_value (t);
6222 if (TREE_CODE (t) == INTEGER_CST
6223 && tree_int_cst_sgn (t) != 1
6224 && t != integer_minus_one_node)
6226 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6227 "%<gang%> static value must be "
6228 "positive");
6229 t = integer_one_node;
6231 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6234 OMP_CLAUSE_OPERAND (c, 1) = t;
6236 /* Check operand 0, the num argument. */
6237 /* FALLTHRU */
6239 case OMP_CLAUSE_WORKER:
6240 case OMP_CLAUSE_VECTOR:
6241 if (OMP_CLAUSE_OPERAND (c, 0) == NULL_TREE)
6242 break;
6243 /* FALLTHRU */
6245 case OMP_CLAUSE_NUM_TASKS:
6246 case OMP_CLAUSE_NUM_TEAMS:
6247 case OMP_CLAUSE_NUM_THREADS:
6248 case OMP_CLAUSE_NUM_GANGS:
6249 case OMP_CLAUSE_NUM_WORKERS:
6250 case OMP_CLAUSE_VECTOR_LENGTH:
6251 t = OMP_CLAUSE_OPERAND (c, 0);
6252 if (t == error_mark_node)
6253 remove = true;
6254 else if (!type_dependent_expression_p (t)
6255 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6257 switch (OMP_CLAUSE_CODE (c))
6259 case OMP_CLAUSE_GANG:
6260 error_at (OMP_CLAUSE_LOCATION (c),
6261 "%<gang%> num expression must be integral"); break;
6262 case OMP_CLAUSE_VECTOR:
6263 error_at (OMP_CLAUSE_LOCATION (c),
6264 "%<vector%> length expression must be integral");
6265 break;
6266 case OMP_CLAUSE_WORKER:
6267 error_at (OMP_CLAUSE_LOCATION (c),
6268 "%<worker%> num expression must be integral");
6269 break;
6270 default:
6271 error_at (OMP_CLAUSE_LOCATION (c),
6272 "%qs expression must be integral",
6273 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6275 remove = true;
6277 else
6279 t = mark_rvalue_use (t);
6280 if (!processing_template_decl)
6282 t = maybe_constant_value (t);
6283 if (TREE_CODE (t) == INTEGER_CST
6284 && tree_int_cst_sgn (t) != 1)
6286 switch (OMP_CLAUSE_CODE (c))
6288 case OMP_CLAUSE_GANG:
6289 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6290 "%<gang%> num value must be positive");
6291 break;
6292 case OMP_CLAUSE_VECTOR:
6293 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6294 "%<vector%> length value must be "
6295 "positive");
6296 break;
6297 case OMP_CLAUSE_WORKER:
6298 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6299 "%<worker%> num value must be "
6300 "positive");
6301 break;
6302 default:
6303 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6304 "%qs value must be positive",
6305 omp_clause_code_name
6306 [OMP_CLAUSE_CODE (c)]);
6308 t = integer_one_node;
6310 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6312 OMP_CLAUSE_OPERAND (c, 0) = t;
6314 break;
6316 case OMP_CLAUSE_SCHEDULE:
6317 if (OMP_CLAUSE_SCHEDULE_KIND (c) & OMP_CLAUSE_SCHEDULE_NONMONOTONIC)
6319 const char *p = NULL;
6320 switch (OMP_CLAUSE_SCHEDULE_KIND (c) & OMP_CLAUSE_SCHEDULE_MASK)
6322 case OMP_CLAUSE_SCHEDULE_STATIC: p = "static"; break;
6323 case OMP_CLAUSE_SCHEDULE_DYNAMIC: break;
6324 case OMP_CLAUSE_SCHEDULE_GUIDED: break;
6325 case OMP_CLAUSE_SCHEDULE_AUTO: p = "auto"; break;
6326 case OMP_CLAUSE_SCHEDULE_RUNTIME: p = "runtime"; break;
6327 default: gcc_unreachable ();
6329 if (p)
6331 error_at (OMP_CLAUSE_LOCATION (c),
6332 "%<nonmonotonic%> modifier specified for %qs "
6333 "schedule kind", p);
6334 OMP_CLAUSE_SCHEDULE_KIND (c)
6335 = (enum omp_clause_schedule_kind)
6336 (OMP_CLAUSE_SCHEDULE_KIND (c)
6337 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
6341 t = OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c);
6342 if (t == NULL)
6344 else if (t == error_mark_node)
6345 remove = true;
6346 else if (!type_dependent_expression_p (t)
6347 && (OMP_CLAUSE_SCHEDULE_KIND (c)
6348 != OMP_CLAUSE_SCHEDULE_CILKFOR)
6349 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6351 error ("schedule chunk size expression must be integral");
6352 remove = true;
6354 else
6356 t = mark_rvalue_use (t);
6357 if (!processing_template_decl)
6359 if (OMP_CLAUSE_SCHEDULE_KIND (c)
6360 == OMP_CLAUSE_SCHEDULE_CILKFOR)
6362 t = convert_to_integer (long_integer_type_node, t);
6363 if (t == error_mark_node)
6365 remove = true;
6366 break;
6369 else
6371 t = maybe_constant_value (t);
6372 if (TREE_CODE (t) == INTEGER_CST
6373 && tree_int_cst_sgn (t) != 1)
6375 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6376 "chunk size value must be positive");
6377 t = integer_one_node;
6380 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6382 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
6384 break;
6386 case OMP_CLAUSE_SIMDLEN:
6387 case OMP_CLAUSE_SAFELEN:
6388 t = OMP_CLAUSE_OPERAND (c, 0);
6389 if (t == error_mark_node)
6390 remove = true;
6391 else if (!type_dependent_expression_p (t)
6392 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6394 error ("%qs length expression must be integral",
6395 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6396 remove = true;
6398 else
6400 t = mark_rvalue_use (t);
6401 if (!processing_template_decl)
6403 t = maybe_constant_value (t);
6404 if (TREE_CODE (t) != INTEGER_CST
6405 || tree_int_cst_sgn (t) != 1)
6407 error ("%qs length expression must be positive constant"
6408 " integer expression",
6409 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6410 remove = true;
6413 OMP_CLAUSE_OPERAND (c, 0) = t;
6414 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SAFELEN)
6415 safelen = c;
6417 break;
6419 case OMP_CLAUSE_ASYNC:
6420 t = OMP_CLAUSE_ASYNC_EXPR (c);
6421 if (t == error_mark_node)
6422 remove = true;
6423 else if (!type_dependent_expression_p (t)
6424 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6426 error ("%<async%> expression must be integral");
6427 remove = true;
6429 else
6431 t = mark_rvalue_use (t);
6432 if (!processing_template_decl)
6433 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6434 OMP_CLAUSE_ASYNC_EXPR (c) = t;
6436 break;
6438 case OMP_CLAUSE_WAIT:
6439 t = OMP_CLAUSE_WAIT_EXPR (c);
6440 if (t == error_mark_node)
6441 remove = true;
6442 else if (!processing_template_decl)
6443 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6444 OMP_CLAUSE_WAIT_EXPR (c) = t;
6445 break;
6447 case OMP_CLAUSE_THREAD_LIMIT:
6448 t = OMP_CLAUSE_THREAD_LIMIT_EXPR (c);
6449 if (t == error_mark_node)
6450 remove = true;
6451 else if (!type_dependent_expression_p (t)
6452 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6454 error ("%<thread_limit%> expression must be integral");
6455 remove = true;
6457 else
6459 t = mark_rvalue_use (t);
6460 if (!processing_template_decl)
6462 t = maybe_constant_value (t);
6463 if (TREE_CODE (t) == INTEGER_CST
6464 && tree_int_cst_sgn (t) != 1)
6466 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6467 "%<thread_limit%> value must be positive");
6468 t = integer_one_node;
6470 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6472 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
6474 break;
6476 case OMP_CLAUSE_DEVICE:
6477 t = OMP_CLAUSE_DEVICE_ID (c);
6478 if (t == error_mark_node)
6479 remove = true;
6480 else if (!type_dependent_expression_p (t)
6481 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6483 error ("%<device%> id must be integral");
6484 remove = true;
6486 else
6488 t = mark_rvalue_use (t);
6489 if (!processing_template_decl)
6490 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6491 OMP_CLAUSE_DEVICE_ID (c) = t;
6493 break;
6495 case OMP_CLAUSE_DIST_SCHEDULE:
6496 t = OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c);
6497 if (t == NULL)
6499 else if (t == error_mark_node)
6500 remove = true;
6501 else if (!type_dependent_expression_p (t)
6502 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6504 error ("%<dist_schedule%> chunk size expression must be "
6505 "integral");
6506 remove = true;
6508 else
6510 t = mark_rvalue_use (t);
6511 if (!processing_template_decl)
6512 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6513 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
6515 break;
6517 case OMP_CLAUSE_ALIGNED:
6518 t = OMP_CLAUSE_DECL (c);
6519 if (t == current_class_ptr && ort != C_ORT_OMP_DECLARE_SIMD)
6521 error ("%<this%> allowed in OpenMP only in %<declare simd%>"
6522 " clauses");
6523 remove = true;
6524 break;
6526 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
6528 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
6529 break;
6530 if (DECL_P (t))
6531 error ("%qD is not a variable in %<aligned%> clause", t);
6532 else
6533 error ("%qE is not a variable in %<aligned%> clause", t);
6534 remove = true;
6536 else if (!type_dependent_expression_p (t)
6537 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE
6538 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE
6539 && (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE
6540 || (!POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (t)))
6541 && (TREE_CODE (TREE_TYPE (TREE_TYPE (t)))
6542 != ARRAY_TYPE))))
6544 error_at (OMP_CLAUSE_LOCATION (c),
6545 "%qE in %<aligned%> clause is neither a pointer nor "
6546 "an array nor a reference to pointer or array", t);
6547 remove = true;
6549 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
6551 error ("%qD appears more than once in %<aligned%> clauses", t);
6552 remove = true;
6554 else
6555 bitmap_set_bit (&aligned_head, DECL_UID (t));
6556 t = OMP_CLAUSE_ALIGNED_ALIGNMENT (c);
6557 if (t == error_mark_node)
6558 remove = true;
6559 else if (t == NULL_TREE)
6560 break;
6561 else if (!type_dependent_expression_p (t)
6562 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6564 error ("%<aligned%> clause alignment expression must "
6565 "be integral");
6566 remove = true;
6568 else
6570 t = mark_rvalue_use (t);
6571 if (!processing_template_decl)
6573 t = maybe_constant_value (t);
6574 if (TREE_CODE (t) != INTEGER_CST
6575 || tree_int_cst_sgn (t) != 1)
6577 error ("%<aligned%> clause alignment expression must be "
6578 "positive constant integer expression");
6579 remove = true;
6582 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = t;
6584 break;
6586 case OMP_CLAUSE_DEPEND:
6587 t = OMP_CLAUSE_DECL (c);
6588 if (t == NULL_TREE)
6590 gcc_assert (OMP_CLAUSE_DEPEND_KIND (c)
6591 == OMP_CLAUSE_DEPEND_SOURCE);
6592 break;
6594 if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SINK)
6596 if (cp_finish_omp_clause_depend_sink (c))
6597 remove = true;
6598 break;
6600 if (TREE_CODE (t) == TREE_LIST)
6602 if (handle_omp_array_sections (c, ort))
6603 remove = true;
6604 break;
6606 if (t == error_mark_node)
6607 remove = true;
6608 else if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
6610 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
6611 break;
6612 if (DECL_P (t))
6613 error ("%qD is not a variable in %<depend%> clause", t);
6614 else
6615 error ("%qE is not a variable in %<depend%> clause", t);
6616 remove = true;
6618 else if (t == current_class_ptr)
6620 error ("%<this%> allowed in OpenMP only in %<declare simd%>"
6621 " clauses");
6622 remove = true;
6624 else if (!processing_template_decl
6625 && !cxx_mark_addressable (t))
6626 remove = true;
6627 break;
6629 case OMP_CLAUSE_MAP:
6630 case OMP_CLAUSE_TO:
6631 case OMP_CLAUSE_FROM:
6632 case OMP_CLAUSE__CACHE_:
6633 t = OMP_CLAUSE_DECL (c);
6634 if (TREE_CODE (t) == TREE_LIST)
6636 if (handle_omp_array_sections (c, ort))
6637 remove = true;
6638 else
6640 t = OMP_CLAUSE_DECL (c);
6641 if (TREE_CODE (t) != TREE_LIST
6642 && !type_dependent_expression_p (t)
6643 && !cp_omp_mappable_type (TREE_TYPE (t)))
6645 error_at (OMP_CLAUSE_LOCATION (c),
6646 "array section does not have mappable type "
6647 "in %qs clause",
6648 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6649 remove = true;
6651 while (TREE_CODE (t) == ARRAY_REF)
6652 t = TREE_OPERAND (t, 0);
6653 if (TREE_CODE (t) == COMPONENT_REF
6654 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
6656 while (TREE_CODE (t) == COMPONENT_REF)
6657 t = TREE_OPERAND (t, 0);
6658 if (REFERENCE_REF_P (t))
6659 t = TREE_OPERAND (t, 0);
6660 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
6661 break;
6662 if (bitmap_bit_p (&map_head, DECL_UID (t)))
6664 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
6665 error ("%qD appears more than once in motion"
6666 " clauses", t);
6667 else if (ort == C_ORT_ACC)
6668 error ("%qD appears more than once in data"
6669 " clauses", t);
6670 else
6671 error ("%qD appears more than once in map"
6672 " clauses", t);
6673 remove = true;
6675 else
6677 bitmap_set_bit (&map_head, DECL_UID (t));
6678 bitmap_set_bit (&map_field_head, DECL_UID (t));
6682 break;
6684 if (t == error_mark_node)
6686 remove = true;
6687 break;
6689 if (REFERENCE_REF_P (t)
6690 && TREE_CODE (TREE_OPERAND (t, 0)) == COMPONENT_REF)
6692 t = TREE_OPERAND (t, 0);
6693 OMP_CLAUSE_DECL (c) = t;
6695 if (TREE_CODE (t) == COMPONENT_REF
6696 && (ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP
6697 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE__CACHE_)
6699 if (type_dependent_expression_p (t))
6700 break;
6701 if (TREE_CODE (TREE_OPERAND (t, 1)) == FIELD_DECL
6702 && DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
6704 error_at (OMP_CLAUSE_LOCATION (c),
6705 "bit-field %qE in %qs clause",
6706 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6707 remove = true;
6709 else if (!cp_omp_mappable_type (TREE_TYPE (t)))
6711 error_at (OMP_CLAUSE_LOCATION (c),
6712 "%qE does not have a mappable type in %qs clause",
6713 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6714 remove = true;
6716 while (TREE_CODE (t) == COMPONENT_REF)
6718 if (TREE_TYPE (TREE_OPERAND (t, 0))
6719 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0)))
6720 == UNION_TYPE))
6722 error_at (OMP_CLAUSE_LOCATION (c),
6723 "%qE is a member of a union", t);
6724 remove = true;
6725 break;
6727 t = TREE_OPERAND (t, 0);
6729 if (remove)
6730 break;
6731 if (REFERENCE_REF_P (t))
6732 t = TREE_OPERAND (t, 0);
6733 if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
6735 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
6736 goto handle_map_references;
6739 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
6741 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
6742 break;
6743 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
6744 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
6745 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ALWAYS_POINTER))
6746 break;
6747 if (DECL_P (t))
6748 error ("%qD is not a variable in %qs clause", t,
6749 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6750 else
6751 error ("%qE is not a variable in %qs clause", t,
6752 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6753 remove = true;
6755 else if (VAR_P (t) && CP_DECL_THREAD_LOCAL_P (t))
6757 error ("%qD is threadprivate variable in %qs clause", t,
6758 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6759 remove = true;
6761 else if (ort != C_ORT_ACC && t == current_class_ptr)
6763 error ("%<this%> allowed in OpenMP only in %<declare simd%>"
6764 " clauses");
6765 remove = true;
6766 break;
6768 else if (!processing_template_decl
6769 && TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE
6770 && (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
6771 || (OMP_CLAUSE_MAP_KIND (c)
6772 != GOMP_MAP_FIRSTPRIVATE_POINTER))
6773 && !cxx_mark_addressable (t))
6774 remove = true;
6775 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
6776 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
6777 || (OMP_CLAUSE_MAP_KIND (c)
6778 == GOMP_MAP_FIRSTPRIVATE_POINTER)))
6779 && t == OMP_CLAUSE_DECL (c)
6780 && !type_dependent_expression_p (t)
6781 && !cp_omp_mappable_type ((TREE_CODE (TREE_TYPE (t))
6782 == REFERENCE_TYPE)
6783 ? TREE_TYPE (TREE_TYPE (t))
6784 : TREE_TYPE (t)))
6786 error_at (OMP_CLAUSE_LOCATION (c),
6787 "%qD does not have a mappable type in %qs clause", t,
6788 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6789 remove = true;
6791 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
6792 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FORCE_DEVICEPTR
6793 && !type_dependent_expression_p (t)
6794 && !POINTER_TYPE_P (TREE_TYPE (t)))
6796 error ("%qD is not a pointer variable", t);
6797 remove = true;
6799 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
6800 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
6802 if (bitmap_bit_p (&generic_head, DECL_UID (t))
6803 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
6805 error ("%qD appears more than once in data clauses", t);
6806 remove = true;
6808 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
6810 if (ort == C_ORT_ACC)
6811 error ("%qD appears more than once in data clauses", t);
6812 else
6813 error ("%qD appears both in data and map clauses", t);
6814 remove = true;
6816 else
6817 bitmap_set_bit (&generic_head, DECL_UID (t));
6819 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
6821 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
6822 error ("%qD appears more than once in motion clauses", t);
6823 if (ort == C_ORT_ACC)
6824 error ("%qD appears more than once in data clauses", t);
6825 else
6826 error ("%qD appears more than once in map clauses", t);
6827 remove = true;
6829 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
6830 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
6832 if (ort == C_ORT_ACC)
6833 error ("%qD appears more than once in data clauses", t);
6834 else
6835 error ("%qD appears both in data and map clauses", t);
6836 remove = true;
6838 else
6840 bitmap_set_bit (&map_head, DECL_UID (t));
6841 if (t != OMP_CLAUSE_DECL (c)
6842 && TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPONENT_REF)
6843 bitmap_set_bit (&map_field_head, DECL_UID (t));
6845 handle_map_references:
6846 if (!remove
6847 && !processing_template_decl
6848 && (ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP
6849 && TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c))) == REFERENCE_TYPE)
6851 t = OMP_CLAUSE_DECL (c);
6852 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
6854 OMP_CLAUSE_DECL (c) = build_simple_mem_ref (t);
6855 if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
6856 OMP_CLAUSE_SIZE (c)
6857 = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t)));
6859 else if (OMP_CLAUSE_MAP_KIND (c)
6860 != GOMP_MAP_FIRSTPRIVATE_POINTER
6861 && (OMP_CLAUSE_MAP_KIND (c)
6862 != GOMP_MAP_FIRSTPRIVATE_REFERENCE)
6863 && (OMP_CLAUSE_MAP_KIND (c)
6864 != GOMP_MAP_ALWAYS_POINTER))
6866 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
6867 OMP_CLAUSE_MAP);
6868 if (TREE_CODE (t) == COMPONENT_REF)
6869 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ALWAYS_POINTER);
6870 else
6871 OMP_CLAUSE_SET_MAP_KIND (c2,
6872 GOMP_MAP_FIRSTPRIVATE_REFERENCE);
6873 OMP_CLAUSE_DECL (c2) = t;
6874 OMP_CLAUSE_SIZE (c2) = size_zero_node;
6875 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
6876 OMP_CLAUSE_CHAIN (c) = c2;
6877 OMP_CLAUSE_DECL (c) = build_simple_mem_ref (t);
6878 if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
6879 OMP_CLAUSE_SIZE (c)
6880 = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t)));
6881 c = c2;
6884 break;
6886 case OMP_CLAUSE_TO_DECLARE:
6887 case OMP_CLAUSE_LINK:
6888 t = OMP_CLAUSE_DECL (c);
6889 if (TREE_CODE (t) == FUNCTION_DECL
6890 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
6892 else if (!VAR_P (t))
6894 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
6896 if (TREE_CODE (t) == TEMPLATE_ID_EXPR)
6897 error_at (OMP_CLAUSE_LOCATION (c),
6898 "template %qE in clause %qs", t,
6899 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6900 else if (really_overloaded_fn (t))
6901 error_at (OMP_CLAUSE_LOCATION (c),
6902 "overloaded function name %qE in clause %qs", t,
6903 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6904 else
6905 error_at (OMP_CLAUSE_LOCATION (c),
6906 "%qE is neither a variable nor a function name "
6907 "in clause %qs", t,
6908 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6910 else
6911 error_at (OMP_CLAUSE_LOCATION (c),
6912 "%qE is not a variable in clause %qs", t,
6913 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6914 remove = true;
6916 else if (DECL_THREAD_LOCAL_P (t))
6918 error_at (OMP_CLAUSE_LOCATION (c),
6919 "%qD is threadprivate variable in %qs clause", t,
6920 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6921 remove = true;
6923 else if (!cp_omp_mappable_type (TREE_TYPE (t)))
6925 error_at (OMP_CLAUSE_LOCATION (c),
6926 "%qD does not have a mappable type in %qs clause", t,
6927 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6928 remove = true;
6930 if (remove)
6931 break;
6932 if (bitmap_bit_p (&generic_head, DECL_UID (t)))
6934 error_at (OMP_CLAUSE_LOCATION (c),
6935 "%qE appears more than once on the same "
6936 "%<declare target%> directive", t);
6937 remove = true;
6939 else
6940 bitmap_set_bit (&generic_head, DECL_UID (t));
6941 break;
6943 case OMP_CLAUSE_UNIFORM:
6944 t = OMP_CLAUSE_DECL (c);
6945 if (TREE_CODE (t) != PARM_DECL)
6947 if (processing_template_decl)
6948 break;
6949 if (DECL_P (t))
6950 error ("%qD is not an argument in %<uniform%> clause", t);
6951 else
6952 error ("%qE is not an argument in %<uniform%> clause", t);
6953 remove = true;
6954 break;
6956 /* map_head bitmap is used as uniform_head if declare_simd. */
6957 bitmap_set_bit (&map_head, DECL_UID (t));
6958 goto check_dup_generic;
6960 case OMP_CLAUSE_GRAINSIZE:
6961 t = OMP_CLAUSE_GRAINSIZE_EXPR (c);
6962 if (t == error_mark_node)
6963 remove = true;
6964 else if (!type_dependent_expression_p (t)
6965 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6967 error ("%<grainsize%> expression must be integral");
6968 remove = true;
6970 else
6972 t = mark_rvalue_use (t);
6973 if (!processing_template_decl)
6975 t = maybe_constant_value (t);
6976 if (TREE_CODE (t) == INTEGER_CST
6977 && tree_int_cst_sgn (t) != 1)
6979 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6980 "%<grainsize%> value must be positive");
6981 t = integer_one_node;
6983 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6985 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
6987 break;
6989 case OMP_CLAUSE_PRIORITY:
6990 t = OMP_CLAUSE_PRIORITY_EXPR (c);
6991 if (t == error_mark_node)
6992 remove = true;
6993 else if (!type_dependent_expression_p (t)
6994 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6996 error ("%<priority%> expression must be integral");
6997 remove = true;
6999 else
7001 t = mark_rvalue_use (t);
7002 if (!processing_template_decl)
7004 t = maybe_constant_value (t);
7005 if (TREE_CODE (t) == INTEGER_CST
7006 && tree_int_cst_sgn (t) == -1)
7008 warning_at (OMP_CLAUSE_LOCATION (c), 0,
7009 "%<priority%> value must be non-negative");
7010 t = integer_one_node;
7012 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7014 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
7016 break;
7018 case OMP_CLAUSE_HINT:
7019 t = OMP_CLAUSE_HINT_EXPR (c);
7020 if (t == error_mark_node)
7021 remove = true;
7022 else if (!type_dependent_expression_p (t)
7023 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
7025 error ("%<num_tasks%> expression must be integral");
7026 remove = true;
7028 else
7030 t = mark_rvalue_use (t);
7031 if (!processing_template_decl)
7033 t = maybe_constant_value (t);
7034 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7036 OMP_CLAUSE_HINT_EXPR (c) = t;
7038 break;
7040 case OMP_CLAUSE_IS_DEVICE_PTR:
7041 case OMP_CLAUSE_USE_DEVICE_PTR:
7042 field_ok = (ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP;
7043 t = OMP_CLAUSE_DECL (c);
7044 if (!type_dependent_expression_p (t))
7046 tree type = TREE_TYPE (t);
7047 if (TREE_CODE (type) != POINTER_TYPE
7048 && TREE_CODE (type) != ARRAY_TYPE
7049 && (TREE_CODE (type) != REFERENCE_TYPE
7050 || (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
7051 && TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE)))
7053 error_at (OMP_CLAUSE_LOCATION (c),
7054 "%qs variable is neither a pointer, nor an array "
7055 "nor reference to pointer or array",
7056 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7057 remove = true;
7060 goto check_dup_generic;
7062 case OMP_CLAUSE_NOWAIT:
7063 case OMP_CLAUSE_DEFAULT:
7064 case OMP_CLAUSE_UNTIED:
7065 case OMP_CLAUSE_COLLAPSE:
7066 case OMP_CLAUSE_MERGEABLE:
7067 case OMP_CLAUSE_PARALLEL:
7068 case OMP_CLAUSE_FOR:
7069 case OMP_CLAUSE_SECTIONS:
7070 case OMP_CLAUSE_TASKGROUP:
7071 case OMP_CLAUSE_PROC_BIND:
7072 case OMP_CLAUSE_NOGROUP:
7073 case OMP_CLAUSE_THREADS:
7074 case OMP_CLAUSE_SIMD:
7075 case OMP_CLAUSE_DEFAULTMAP:
7076 case OMP_CLAUSE__CILK_FOR_COUNT_:
7077 case OMP_CLAUSE_AUTO:
7078 case OMP_CLAUSE_INDEPENDENT:
7079 case OMP_CLAUSE_SEQ:
7080 break;
7082 case OMP_CLAUSE_TILE:
7083 for (tree list = OMP_CLAUSE_TILE_LIST (c); !remove && list;
7084 list = TREE_CHAIN (list))
7086 t = TREE_VALUE (list);
7088 if (t == error_mark_node)
7089 remove = true;
7090 else if (!type_dependent_expression_p (t)
7091 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
7093 error_at (OMP_CLAUSE_LOCATION (c),
7094 "%<tile%> argument needs integral type");
7095 remove = true;
7097 else
7099 t = mark_rvalue_use (t);
7100 if (!processing_template_decl)
7102 /* Zero is used to indicate '*', we permit you
7103 to get there via an ICE of value zero. */
7104 t = maybe_constant_value (t);
7105 if (!tree_fits_shwi_p (t)
7106 || tree_to_shwi (t) < 0)
7108 error_at (OMP_CLAUSE_LOCATION (c),
7109 "%<tile%> argument needs positive "
7110 "integral constant");
7111 remove = true;
7113 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7117 /* Update list item. */
7118 TREE_VALUE (list) = t;
7120 break;
7122 case OMP_CLAUSE_ORDERED:
7123 ordered_seen = true;
7124 break;
7126 case OMP_CLAUSE_INBRANCH:
7127 case OMP_CLAUSE_NOTINBRANCH:
7128 if (branch_seen)
7130 error ("%<inbranch%> clause is incompatible with "
7131 "%<notinbranch%>");
7132 remove = true;
7134 branch_seen = true;
7135 break;
7137 default:
7138 gcc_unreachable ();
7141 if (remove)
7142 *pc = OMP_CLAUSE_CHAIN (c);
7143 else
7144 pc = &OMP_CLAUSE_CHAIN (c);
7147 for (pc = &clauses, c = clauses; c ; c = *pc)
7149 enum omp_clause_code c_kind = OMP_CLAUSE_CODE (c);
7150 bool remove = false;
7151 bool need_complete_type = false;
7152 bool need_default_ctor = false;
7153 bool need_copy_ctor = false;
7154 bool need_copy_assignment = false;
7155 bool need_implicitly_determined = false;
7156 bool need_dtor = false;
7157 tree type, inner_type;
7159 switch (c_kind)
7161 case OMP_CLAUSE_SHARED:
7162 need_implicitly_determined = true;
7163 break;
7164 case OMP_CLAUSE_PRIVATE:
7165 need_complete_type = true;
7166 need_default_ctor = true;
7167 need_dtor = true;
7168 need_implicitly_determined = true;
7169 break;
7170 case OMP_CLAUSE_FIRSTPRIVATE:
7171 need_complete_type = true;
7172 need_copy_ctor = true;
7173 need_dtor = true;
7174 need_implicitly_determined = true;
7175 break;
7176 case OMP_CLAUSE_LASTPRIVATE:
7177 need_complete_type = true;
7178 need_copy_assignment = true;
7179 need_implicitly_determined = true;
7180 break;
7181 case OMP_CLAUSE_REDUCTION:
7182 need_implicitly_determined = true;
7183 break;
7184 case OMP_CLAUSE_LINEAR:
7185 if (ort != C_ORT_OMP_DECLARE_SIMD)
7186 need_implicitly_determined = true;
7187 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c)
7188 && !bitmap_bit_p (&map_head,
7189 DECL_UID (OMP_CLAUSE_LINEAR_STEP (c))))
7191 error_at (OMP_CLAUSE_LOCATION (c),
7192 "%<linear%> clause step is a parameter %qD not "
7193 "specified in %<uniform%> clause",
7194 OMP_CLAUSE_LINEAR_STEP (c));
7195 *pc = OMP_CLAUSE_CHAIN (c);
7196 continue;
7198 break;
7199 case OMP_CLAUSE_COPYPRIVATE:
7200 need_copy_assignment = true;
7201 break;
7202 case OMP_CLAUSE_COPYIN:
7203 need_copy_assignment = true;
7204 break;
7205 case OMP_CLAUSE_SIMDLEN:
7206 if (safelen
7207 && !processing_template_decl
7208 && tree_int_cst_lt (OMP_CLAUSE_SAFELEN_EXPR (safelen),
7209 OMP_CLAUSE_SIMDLEN_EXPR (c)))
7211 error_at (OMP_CLAUSE_LOCATION (c),
7212 "%<simdlen%> clause value is bigger than "
7213 "%<safelen%> clause value");
7214 OMP_CLAUSE_SIMDLEN_EXPR (c)
7215 = OMP_CLAUSE_SAFELEN_EXPR (safelen);
7217 pc = &OMP_CLAUSE_CHAIN (c);
7218 continue;
7219 case OMP_CLAUSE_SCHEDULE:
7220 if (ordered_seen
7221 && (OMP_CLAUSE_SCHEDULE_KIND (c)
7222 & OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
7224 error_at (OMP_CLAUSE_LOCATION (c),
7225 "%<nonmonotonic%> schedule modifier specified "
7226 "together with %<ordered%> clause");
7227 OMP_CLAUSE_SCHEDULE_KIND (c)
7228 = (enum omp_clause_schedule_kind)
7229 (OMP_CLAUSE_SCHEDULE_KIND (c)
7230 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
7232 pc = &OMP_CLAUSE_CHAIN (c);
7233 continue;
7234 case OMP_CLAUSE_NOWAIT:
7235 if (copyprivate_seen)
7237 error_at (OMP_CLAUSE_LOCATION (c),
7238 "%<nowait%> clause must not be used together "
7239 "with %<copyprivate%>");
7240 *pc = OMP_CLAUSE_CHAIN (c);
7241 continue;
7243 /* FALLTHRU */
7244 default:
7245 pc = &OMP_CLAUSE_CHAIN (c);
7246 continue;
7249 t = OMP_CLAUSE_DECL (c);
7250 if (processing_template_decl
7251 && !VAR_P (t) && TREE_CODE (t) != PARM_DECL)
7253 pc = &OMP_CLAUSE_CHAIN (c);
7254 continue;
7257 switch (c_kind)
7259 case OMP_CLAUSE_LASTPRIVATE:
7260 if (!bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
7262 need_default_ctor = true;
7263 need_dtor = true;
7265 break;
7267 case OMP_CLAUSE_REDUCTION:
7268 if (finish_omp_reduction_clause (c, &need_default_ctor,
7269 &need_dtor))
7270 remove = true;
7271 else
7272 t = OMP_CLAUSE_DECL (c);
7273 break;
7275 case OMP_CLAUSE_COPYIN:
7276 if (!VAR_P (t) || !CP_DECL_THREAD_LOCAL_P (t))
7278 error ("%qE must be %<threadprivate%> for %<copyin%>", t);
7279 remove = true;
7281 break;
7283 default:
7284 break;
7287 if (need_complete_type || need_copy_assignment)
7289 t = require_complete_type (t);
7290 if (t == error_mark_node)
7291 remove = true;
7292 else if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE
7293 && !complete_type_or_else (TREE_TYPE (TREE_TYPE (t)), t))
7294 remove = true;
7296 if (need_implicitly_determined)
7298 const char *share_name = NULL;
7300 if (VAR_P (t) && CP_DECL_THREAD_LOCAL_P (t))
7301 share_name = "threadprivate";
7302 else switch (cxx_omp_predetermined_sharing (t))
7304 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
7305 break;
7306 case OMP_CLAUSE_DEFAULT_SHARED:
7307 /* const vars may be specified in firstprivate clause. */
7308 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
7309 && cxx_omp_const_qual_no_mutable (t))
7310 break;
7311 share_name = "shared";
7312 break;
7313 case OMP_CLAUSE_DEFAULT_PRIVATE:
7314 share_name = "private";
7315 break;
7316 default:
7317 gcc_unreachable ();
7319 if (share_name)
7321 error ("%qE is predetermined %qs for %qs",
7322 omp_clause_printable_decl (t), share_name,
7323 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7324 remove = true;
7328 /* We're interested in the base element, not arrays. */
7329 inner_type = type = TREE_TYPE (t);
7330 if ((need_complete_type
7331 || need_copy_assignment
7332 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
7333 && TREE_CODE (inner_type) == REFERENCE_TYPE)
7334 inner_type = TREE_TYPE (inner_type);
7335 while (TREE_CODE (inner_type) == ARRAY_TYPE)
7336 inner_type = TREE_TYPE (inner_type);
7338 /* Check for special function availability by building a call to one.
7339 Save the results, because later we won't be in the right context
7340 for making these queries. */
7341 if (CLASS_TYPE_P (inner_type)
7342 && COMPLETE_TYPE_P (inner_type)
7343 && (need_default_ctor || need_copy_ctor
7344 || need_copy_assignment || need_dtor)
7345 && !type_dependent_expression_p (t)
7346 && cxx_omp_create_clause_info (c, inner_type, need_default_ctor,
7347 need_copy_ctor, need_copy_assignment,
7348 need_dtor))
7349 remove = true;
7351 if (!remove
7352 && c_kind == OMP_CLAUSE_SHARED
7353 && processing_template_decl)
7355 t = omp_clause_decl_field (OMP_CLAUSE_DECL (c));
7356 if (t)
7357 OMP_CLAUSE_DECL (c) = t;
7360 if (remove)
7361 *pc = OMP_CLAUSE_CHAIN (c);
7362 else
7363 pc = &OMP_CLAUSE_CHAIN (c);
7366 bitmap_obstack_release (NULL);
7367 return clauses;
7370 /* Start processing OpenMP clauses that can include any
7371 privatization clauses for non-static data members. */
7373 tree
7374 push_omp_privatization_clauses (bool ignore_next)
7376 if (omp_private_member_ignore_next)
7378 omp_private_member_ignore_next = ignore_next;
7379 return NULL_TREE;
7381 omp_private_member_ignore_next = ignore_next;
7382 if (omp_private_member_map)
7383 omp_private_member_vec.safe_push (error_mark_node);
7384 return push_stmt_list ();
7387 /* Revert remapping of any non-static data members since
7388 the last push_omp_privatization_clauses () call. */
7390 void
7391 pop_omp_privatization_clauses (tree stmt)
7393 if (stmt == NULL_TREE)
7394 return;
7395 stmt = pop_stmt_list (stmt);
7396 if (omp_private_member_map)
7398 while (!omp_private_member_vec.is_empty ())
7400 tree t = omp_private_member_vec.pop ();
7401 if (t == error_mark_node)
7403 add_stmt (stmt);
7404 return;
7406 bool no_decl_expr = t == integer_zero_node;
7407 if (no_decl_expr)
7408 t = omp_private_member_vec.pop ();
7409 tree *v = omp_private_member_map->get (t);
7410 gcc_assert (v);
7411 if (!no_decl_expr)
7412 add_decl_expr (*v);
7413 omp_private_member_map->remove (t);
7415 delete omp_private_member_map;
7416 omp_private_member_map = NULL;
7418 add_stmt (stmt);
7421 /* Remember OpenMP privatization clauses mapping and clear it.
7422 Used for lambdas. */
7424 void
7425 save_omp_privatization_clauses (vec<tree> &save)
7427 save = vNULL;
7428 if (omp_private_member_ignore_next)
7429 save.safe_push (integer_one_node);
7430 omp_private_member_ignore_next = false;
7431 if (!omp_private_member_map)
7432 return;
7434 while (!omp_private_member_vec.is_empty ())
7436 tree t = omp_private_member_vec.pop ();
7437 if (t == error_mark_node)
7439 save.safe_push (t);
7440 continue;
7442 tree n = t;
7443 if (t == integer_zero_node)
7444 t = omp_private_member_vec.pop ();
7445 tree *v = omp_private_member_map->get (t);
7446 gcc_assert (v);
7447 save.safe_push (*v);
7448 save.safe_push (t);
7449 if (n != t)
7450 save.safe_push (n);
7452 delete omp_private_member_map;
7453 omp_private_member_map = NULL;
7456 /* Restore OpenMP privatization clauses mapping saved by the
7457 above function. */
7459 void
7460 restore_omp_privatization_clauses (vec<tree> &save)
7462 gcc_assert (omp_private_member_vec.is_empty ());
7463 omp_private_member_ignore_next = false;
7464 if (save.is_empty ())
7465 return;
7466 if (save.length () == 1 && save[0] == integer_one_node)
7468 omp_private_member_ignore_next = true;
7469 save.release ();
7470 return;
7473 omp_private_member_map = new hash_map <tree, tree>;
7474 while (!save.is_empty ())
7476 tree t = save.pop ();
7477 tree n = t;
7478 if (t != error_mark_node)
7480 if (t == integer_one_node)
7482 omp_private_member_ignore_next = true;
7483 gcc_assert (save.is_empty ());
7484 break;
7486 if (t == integer_zero_node)
7487 t = save.pop ();
7488 tree &v = omp_private_member_map->get_or_insert (t);
7489 v = save.pop ();
7491 omp_private_member_vec.safe_push (t);
7492 if (n != t)
7493 omp_private_member_vec.safe_push (n);
7495 save.release ();
7498 /* For all variables in the tree_list VARS, mark them as thread local. */
7500 void
7501 finish_omp_threadprivate (tree vars)
7503 tree t;
7505 /* Mark every variable in VARS to be assigned thread local storage. */
7506 for (t = vars; t; t = TREE_CHAIN (t))
7508 tree v = TREE_PURPOSE (t);
7510 if (error_operand_p (v))
7512 else if (!VAR_P (v))
7513 error ("%<threadprivate%> %qD is not file, namespace "
7514 "or block scope variable", v);
7515 /* If V had already been marked threadprivate, it doesn't matter
7516 whether it had been used prior to this point. */
7517 else if (TREE_USED (v)
7518 && (DECL_LANG_SPECIFIC (v) == NULL
7519 || !CP_DECL_THREADPRIVATE_P (v)))
7520 error ("%qE declared %<threadprivate%> after first use", v);
7521 else if (! TREE_STATIC (v) && ! DECL_EXTERNAL (v))
7522 error ("automatic variable %qE cannot be %<threadprivate%>", v);
7523 else if (! COMPLETE_TYPE_P (complete_type (TREE_TYPE (v))))
7524 error ("%<threadprivate%> %qE has incomplete type", v);
7525 else if (TREE_STATIC (v) && TYPE_P (CP_DECL_CONTEXT (v))
7526 && CP_DECL_CONTEXT (v) != current_class_type)
7527 error ("%<threadprivate%> %qE directive not "
7528 "in %qT definition", v, CP_DECL_CONTEXT (v));
7529 else
7531 /* Allocate a LANG_SPECIFIC structure for V, if needed. */
7532 if (DECL_LANG_SPECIFIC (v) == NULL)
7534 retrofit_lang_decl (v);
7536 /* Make sure that DECL_DISCRIMINATOR_P continues to be true
7537 after the allocation of the lang_decl structure. */
7538 if (DECL_DISCRIMINATOR_P (v))
7539 DECL_LANG_SPECIFIC (v)->u.base.u2sel = 1;
7542 if (! CP_DECL_THREAD_LOCAL_P (v))
7544 CP_DECL_THREAD_LOCAL_P (v) = true;
7545 set_decl_tls_model (v, decl_default_tls_model (v));
7546 /* If rtl has been already set for this var, call
7547 make_decl_rtl once again, so that encode_section_info
7548 has a chance to look at the new decl flags. */
7549 if (DECL_RTL_SET_P (v))
7550 make_decl_rtl (v);
7552 CP_DECL_THREADPRIVATE_P (v) = 1;
7557 /* Build an OpenMP structured block. */
7559 tree
7560 begin_omp_structured_block (void)
7562 return do_pushlevel (sk_omp);
7565 tree
7566 finish_omp_structured_block (tree block)
7568 return do_poplevel (block);
7571 /* Similarly, except force the retention of the BLOCK. */
7573 tree
7574 begin_omp_parallel (void)
7576 keep_next_level (true);
7577 return begin_omp_structured_block ();
7580 /* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
7581 statement. */
7583 tree
7584 finish_oacc_data (tree clauses, tree block)
7586 tree stmt;
7588 block = finish_omp_structured_block (block);
7590 stmt = make_node (OACC_DATA);
7591 TREE_TYPE (stmt) = void_type_node;
7592 OACC_DATA_CLAUSES (stmt) = clauses;
7593 OACC_DATA_BODY (stmt) = block;
7595 return add_stmt (stmt);
7598 /* Generate OACC_HOST_DATA, with CLAUSES and BLOCK as its compound
7599 statement. */
7601 tree
7602 finish_oacc_host_data (tree clauses, tree block)
7604 tree stmt;
7606 block = finish_omp_structured_block (block);
7608 stmt = make_node (OACC_HOST_DATA);
7609 TREE_TYPE (stmt) = void_type_node;
7610 OACC_HOST_DATA_CLAUSES (stmt) = clauses;
7611 OACC_HOST_DATA_BODY (stmt) = block;
7613 return add_stmt (stmt);
7616 /* Generate OMP construct CODE, with BODY and CLAUSES as its compound
7617 statement. */
7619 tree
7620 finish_omp_construct (enum tree_code code, tree body, tree clauses)
7622 body = finish_omp_structured_block (body);
7624 tree stmt = make_node (code);
7625 TREE_TYPE (stmt) = void_type_node;
7626 OMP_BODY (stmt) = body;
7627 OMP_CLAUSES (stmt) = clauses;
7629 return add_stmt (stmt);
7632 tree
7633 finish_omp_parallel (tree clauses, tree body)
7635 tree stmt;
7637 body = finish_omp_structured_block (body);
7639 stmt = make_node (OMP_PARALLEL);
7640 TREE_TYPE (stmt) = void_type_node;
7641 OMP_PARALLEL_CLAUSES (stmt) = clauses;
7642 OMP_PARALLEL_BODY (stmt) = body;
7644 return add_stmt (stmt);
7647 tree
7648 begin_omp_task (void)
7650 keep_next_level (true);
7651 return begin_omp_structured_block ();
7654 tree
7655 finish_omp_task (tree clauses, tree body)
7657 tree stmt;
7659 body = finish_omp_structured_block (body);
7661 stmt = make_node (OMP_TASK);
7662 TREE_TYPE (stmt) = void_type_node;
7663 OMP_TASK_CLAUSES (stmt) = clauses;
7664 OMP_TASK_BODY (stmt) = body;
7666 return add_stmt (stmt);
7669 /* Helper function for finish_omp_for. Convert Ith random access iterator
7670 into integral iterator. Return FALSE if successful. */
7672 static bool
7673 handle_omp_for_class_iterator (int i, location_t locus, enum tree_code code,
7674 tree declv, tree orig_declv, tree initv,
7675 tree condv, tree incrv, tree *body,
7676 tree *pre_body, tree &clauses, tree *lastp,
7677 int collapse, int ordered)
7679 tree diff, iter_init, iter_incr = NULL, last;
7680 tree incr_var = NULL, orig_pre_body, orig_body, c;
7681 tree decl = TREE_VEC_ELT (declv, i);
7682 tree init = TREE_VEC_ELT (initv, i);
7683 tree cond = TREE_VEC_ELT (condv, i);
7684 tree incr = TREE_VEC_ELT (incrv, i);
7685 tree iter = decl;
7686 location_t elocus = locus;
7688 if (init && EXPR_HAS_LOCATION (init))
7689 elocus = EXPR_LOCATION (init);
7691 cond = cp_fully_fold (cond);
7692 switch (TREE_CODE (cond))
7694 case GT_EXPR:
7695 case GE_EXPR:
7696 case LT_EXPR:
7697 case LE_EXPR:
7698 case NE_EXPR:
7699 if (TREE_OPERAND (cond, 1) == iter)
7700 cond = build2 (swap_tree_comparison (TREE_CODE (cond)),
7701 TREE_TYPE (cond), iter, TREE_OPERAND (cond, 0));
7702 if (TREE_OPERAND (cond, 0) != iter)
7703 cond = error_mark_node;
7704 else
7706 tree tem = build_x_binary_op (EXPR_LOCATION (cond),
7707 TREE_CODE (cond),
7708 iter, ERROR_MARK,
7709 TREE_OPERAND (cond, 1), ERROR_MARK,
7710 NULL, tf_warning_or_error);
7711 if (error_operand_p (tem))
7712 return true;
7714 break;
7715 default:
7716 cond = error_mark_node;
7717 break;
7719 if (cond == error_mark_node)
7721 error_at (elocus, "invalid controlling predicate");
7722 return true;
7724 diff = build_x_binary_op (elocus, MINUS_EXPR, TREE_OPERAND (cond, 1),
7725 ERROR_MARK, iter, ERROR_MARK, NULL,
7726 tf_warning_or_error);
7727 diff = cp_fully_fold (diff);
7728 if (error_operand_p (diff))
7729 return true;
7730 if (TREE_CODE (TREE_TYPE (diff)) != INTEGER_TYPE)
7732 error_at (elocus, "difference between %qE and %qD does not have integer type",
7733 TREE_OPERAND (cond, 1), iter);
7734 return true;
7736 if (!c_omp_check_loop_iv_exprs (locus, orig_declv,
7737 TREE_VEC_ELT (declv, i), NULL_TREE,
7738 cond, cp_walk_subtrees))
7739 return true;
7741 switch (TREE_CODE (incr))
7743 case PREINCREMENT_EXPR:
7744 case PREDECREMENT_EXPR:
7745 case POSTINCREMENT_EXPR:
7746 case POSTDECREMENT_EXPR:
7747 if (TREE_OPERAND (incr, 0) != iter)
7749 incr = error_mark_node;
7750 break;
7752 iter_incr = build_x_unary_op (EXPR_LOCATION (incr),
7753 TREE_CODE (incr), iter,
7754 tf_warning_or_error);
7755 if (error_operand_p (iter_incr))
7756 return true;
7757 else if (TREE_CODE (incr) == PREINCREMENT_EXPR
7758 || TREE_CODE (incr) == POSTINCREMENT_EXPR)
7759 incr = integer_one_node;
7760 else
7761 incr = integer_minus_one_node;
7762 break;
7763 case MODIFY_EXPR:
7764 if (TREE_OPERAND (incr, 0) != iter)
7765 incr = error_mark_node;
7766 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
7767 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
7769 tree rhs = TREE_OPERAND (incr, 1);
7770 if (TREE_OPERAND (rhs, 0) == iter)
7772 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (rhs, 1)))
7773 != INTEGER_TYPE)
7774 incr = error_mark_node;
7775 else
7777 iter_incr = build_x_modify_expr (EXPR_LOCATION (rhs),
7778 iter, TREE_CODE (rhs),
7779 TREE_OPERAND (rhs, 1),
7780 tf_warning_or_error);
7781 if (error_operand_p (iter_incr))
7782 return true;
7783 incr = TREE_OPERAND (rhs, 1);
7784 incr = cp_convert (TREE_TYPE (diff), incr,
7785 tf_warning_or_error);
7786 if (TREE_CODE (rhs) == MINUS_EXPR)
7788 incr = build1 (NEGATE_EXPR, TREE_TYPE (diff), incr);
7789 incr = fold_simple (incr);
7791 if (TREE_CODE (incr) != INTEGER_CST
7792 && (TREE_CODE (incr) != NOP_EXPR
7793 || (TREE_CODE (TREE_OPERAND (incr, 0))
7794 != INTEGER_CST)))
7795 iter_incr = NULL;
7798 else if (TREE_OPERAND (rhs, 1) == iter)
7800 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (rhs, 0))) != INTEGER_TYPE
7801 || TREE_CODE (rhs) != PLUS_EXPR)
7802 incr = error_mark_node;
7803 else
7805 iter_incr = build_x_binary_op (EXPR_LOCATION (rhs),
7806 PLUS_EXPR,
7807 TREE_OPERAND (rhs, 0),
7808 ERROR_MARK, iter,
7809 ERROR_MARK, NULL,
7810 tf_warning_or_error);
7811 if (error_operand_p (iter_incr))
7812 return true;
7813 iter_incr = build_x_modify_expr (EXPR_LOCATION (rhs),
7814 iter, NOP_EXPR,
7815 iter_incr,
7816 tf_warning_or_error);
7817 if (error_operand_p (iter_incr))
7818 return true;
7819 incr = TREE_OPERAND (rhs, 0);
7820 iter_incr = NULL;
7823 else
7824 incr = error_mark_node;
7826 else
7827 incr = error_mark_node;
7828 break;
7829 default:
7830 incr = error_mark_node;
7831 break;
7834 if (incr == error_mark_node)
7836 error_at (elocus, "invalid increment expression");
7837 return true;
7840 incr = cp_convert (TREE_TYPE (diff), incr, tf_warning_or_error);
7841 bool taskloop_iv_seen = false;
7842 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
7843 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
7844 && OMP_CLAUSE_DECL (c) == iter)
7846 if (code == OMP_TASKLOOP)
7848 taskloop_iv_seen = true;
7849 OMP_CLAUSE_LASTPRIVATE_TASKLOOP_IV (c) = 1;
7851 break;
7853 else if (code == OMP_TASKLOOP
7854 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
7855 && OMP_CLAUSE_DECL (c) == iter)
7857 taskloop_iv_seen = true;
7858 OMP_CLAUSE_PRIVATE_TASKLOOP_IV (c) = 1;
7861 decl = create_temporary_var (TREE_TYPE (diff));
7862 pushdecl (decl);
7863 add_decl_expr (decl);
7864 last = create_temporary_var (TREE_TYPE (diff));
7865 pushdecl (last);
7866 add_decl_expr (last);
7867 if (c && iter_incr == NULL && TREE_CODE (incr) != INTEGER_CST
7868 && (!ordered || (i < collapse && collapse > 1)))
7870 incr_var = create_temporary_var (TREE_TYPE (diff));
7871 pushdecl (incr_var);
7872 add_decl_expr (incr_var);
7874 gcc_assert (stmts_are_full_exprs_p ());
7875 tree diffvar = NULL_TREE;
7876 if (code == OMP_TASKLOOP)
7878 if (!taskloop_iv_seen)
7880 tree ivc = build_omp_clause (locus, OMP_CLAUSE_FIRSTPRIVATE);
7881 OMP_CLAUSE_DECL (ivc) = iter;
7882 cxx_omp_finish_clause (ivc, NULL);
7883 OMP_CLAUSE_CHAIN (ivc) = clauses;
7884 clauses = ivc;
7886 tree lvc = build_omp_clause (locus, OMP_CLAUSE_FIRSTPRIVATE);
7887 OMP_CLAUSE_DECL (lvc) = last;
7888 OMP_CLAUSE_CHAIN (lvc) = clauses;
7889 clauses = lvc;
7890 diffvar = create_temporary_var (TREE_TYPE (diff));
7891 pushdecl (diffvar);
7892 add_decl_expr (diffvar);
7895 orig_pre_body = *pre_body;
7896 *pre_body = push_stmt_list ();
7897 if (orig_pre_body)
7898 add_stmt (orig_pre_body);
7899 if (init != NULL)
7900 finish_expr_stmt (build_x_modify_expr (elocus,
7901 iter, NOP_EXPR, init,
7902 tf_warning_or_error));
7903 init = build_int_cst (TREE_TYPE (diff), 0);
7904 if (c && iter_incr == NULL
7905 && (!ordered || (i < collapse && collapse > 1)))
7907 if (incr_var)
7909 finish_expr_stmt (build_x_modify_expr (elocus,
7910 incr_var, NOP_EXPR,
7911 incr, tf_warning_or_error));
7912 incr = incr_var;
7914 iter_incr = build_x_modify_expr (elocus,
7915 iter, PLUS_EXPR, incr,
7916 tf_warning_or_error);
7918 if (c && ordered && i < collapse && collapse > 1)
7919 iter_incr = incr;
7920 finish_expr_stmt (build_x_modify_expr (elocus,
7921 last, NOP_EXPR, init,
7922 tf_warning_or_error));
7923 if (diffvar)
7925 finish_expr_stmt (build_x_modify_expr (elocus,
7926 diffvar, NOP_EXPR,
7927 diff, tf_warning_or_error));
7928 diff = diffvar;
7930 *pre_body = pop_stmt_list (*pre_body);
7932 cond = cp_build_binary_op (elocus,
7933 TREE_CODE (cond), decl, diff,
7934 tf_warning_or_error);
7935 incr = build_modify_expr (elocus, decl, NULL_TREE, PLUS_EXPR,
7936 elocus, incr, NULL_TREE);
7938 orig_body = *body;
7939 *body = push_stmt_list ();
7940 iter_init = build2 (MINUS_EXPR, TREE_TYPE (diff), decl, last);
7941 iter_init = build_x_modify_expr (elocus,
7942 iter, PLUS_EXPR, iter_init,
7943 tf_warning_or_error);
7944 if (iter_init != error_mark_node)
7945 iter_init = build1 (NOP_EXPR, void_type_node, iter_init);
7946 finish_expr_stmt (iter_init);
7947 finish_expr_stmt (build_x_modify_expr (elocus,
7948 last, NOP_EXPR, decl,
7949 tf_warning_or_error));
7950 add_stmt (orig_body);
7951 *body = pop_stmt_list (*body);
7953 if (c)
7955 OMP_CLAUSE_LASTPRIVATE_STMT (c) = push_stmt_list ();
7956 if (!ordered)
7957 finish_expr_stmt (iter_incr);
7958 else
7960 iter_init = decl;
7961 if (i < collapse && collapse > 1 && !error_operand_p (iter_incr))
7962 iter_init = build2 (PLUS_EXPR, TREE_TYPE (diff),
7963 iter_init, iter_incr);
7964 iter_init = build2 (MINUS_EXPR, TREE_TYPE (diff), iter_init, last);
7965 iter_init = build_x_modify_expr (elocus,
7966 iter, PLUS_EXPR, iter_init,
7967 tf_warning_or_error);
7968 if (iter_init != error_mark_node)
7969 iter_init = build1 (NOP_EXPR, void_type_node, iter_init);
7970 finish_expr_stmt (iter_init);
7972 OMP_CLAUSE_LASTPRIVATE_STMT (c)
7973 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (c));
7976 TREE_VEC_ELT (declv, i) = decl;
7977 TREE_VEC_ELT (initv, i) = init;
7978 TREE_VEC_ELT (condv, i) = cond;
7979 TREE_VEC_ELT (incrv, i) = incr;
7980 *lastp = last;
7982 return false;
7985 /* Build and validate an OMP_FOR statement. CLAUSES, BODY, COND, INCR
7986 are directly for their associated operands in the statement. DECL
7987 and INIT are a combo; if DECL is NULL then INIT ought to be a
7988 MODIFY_EXPR, and the DECL should be extracted. PRE_BODY are
7989 optional statements that need to go before the loop into its
7990 sk_omp scope. */
7992 tree
7993 finish_omp_for (location_t locus, enum tree_code code, tree declv,
7994 tree orig_declv, tree initv, tree condv, tree incrv,
7995 tree body, tree pre_body, vec<tree> *orig_inits, tree clauses)
7997 tree omp_for = NULL, orig_incr = NULL;
7998 tree decl = NULL, init, cond, incr, orig_decl = NULL_TREE, block = NULL_TREE;
7999 tree last = NULL_TREE;
8000 location_t elocus;
8001 int i;
8002 int collapse = 1;
8003 int ordered = 0;
8005 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (initv));
8006 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (condv));
8007 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (incrv));
8008 if (TREE_VEC_LENGTH (declv) > 1)
8010 tree c;
8012 c = omp_find_clause (clauses, OMP_CLAUSE_TILE);
8013 if (c)
8014 collapse = list_length (OMP_CLAUSE_TILE_LIST (c));
8015 else
8017 c = omp_find_clause (clauses, OMP_CLAUSE_COLLAPSE);
8018 if (c)
8019 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (c));
8020 if (collapse != TREE_VEC_LENGTH (declv))
8021 ordered = TREE_VEC_LENGTH (declv);
8024 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
8026 decl = TREE_VEC_ELT (declv, i);
8027 init = TREE_VEC_ELT (initv, i);
8028 cond = TREE_VEC_ELT (condv, i);
8029 incr = TREE_VEC_ELT (incrv, i);
8030 elocus = locus;
8032 if (decl == NULL)
8034 if (init != NULL)
8035 switch (TREE_CODE (init))
8037 case MODIFY_EXPR:
8038 decl = TREE_OPERAND (init, 0);
8039 init = TREE_OPERAND (init, 1);
8040 break;
8041 case MODOP_EXPR:
8042 if (TREE_CODE (TREE_OPERAND (init, 1)) == NOP_EXPR)
8044 decl = TREE_OPERAND (init, 0);
8045 init = TREE_OPERAND (init, 2);
8047 break;
8048 default:
8049 break;
8052 if (decl == NULL)
8054 error_at (locus,
8055 "expected iteration declaration or initialization");
8056 return NULL;
8060 if (init && EXPR_HAS_LOCATION (init))
8061 elocus = EXPR_LOCATION (init);
8063 if (cond == NULL)
8065 error_at (elocus, "missing controlling predicate");
8066 return NULL;
8069 if (incr == NULL)
8071 error_at (elocus, "missing increment expression");
8072 return NULL;
8075 TREE_VEC_ELT (declv, i) = decl;
8076 TREE_VEC_ELT (initv, i) = init;
8079 if (orig_inits)
8081 bool fail = false;
8082 tree orig_init;
8083 FOR_EACH_VEC_ELT (*orig_inits, i, orig_init)
8084 if (orig_init
8085 && !c_omp_check_loop_iv_exprs (locus, declv,
8086 TREE_VEC_ELT (declv, i), orig_init,
8087 NULL_TREE, cp_walk_subtrees))
8088 fail = true;
8089 if (fail)
8090 return NULL;
8093 if (dependent_omp_for_p (declv, initv, condv, incrv))
8095 tree stmt;
8097 stmt = make_node (code);
8099 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
8101 /* This is really just a place-holder. We'll be decomposing this
8102 again and going through the cp_build_modify_expr path below when
8103 we instantiate the thing. */
8104 TREE_VEC_ELT (initv, i)
8105 = build2 (MODIFY_EXPR, void_type_node, TREE_VEC_ELT (declv, i),
8106 TREE_VEC_ELT (initv, i));
8109 TREE_TYPE (stmt) = void_type_node;
8110 OMP_FOR_INIT (stmt) = initv;
8111 OMP_FOR_COND (stmt) = condv;
8112 OMP_FOR_INCR (stmt) = incrv;
8113 OMP_FOR_BODY (stmt) = body;
8114 OMP_FOR_PRE_BODY (stmt) = pre_body;
8115 OMP_FOR_CLAUSES (stmt) = clauses;
8117 SET_EXPR_LOCATION (stmt, locus);
8118 return add_stmt (stmt);
8121 if (!orig_declv)
8122 orig_declv = copy_node (declv);
8124 if (processing_template_decl)
8125 orig_incr = make_tree_vec (TREE_VEC_LENGTH (incrv));
8127 for (i = 0; i < TREE_VEC_LENGTH (declv); )
8129 decl = TREE_VEC_ELT (declv, i);
8130 init = TREE_VEC_ELT (initv, i);
8131 cond = TREE_VEC_ELT (condv, i);
8132 incr = TREE_VEC_ELT (incrv, i);
8133 if (orig_incr)
8134 TREE_VEC_ELT (orig_incr, i) = incr;
8135 elocus = locus;
8137 if (init && EXPR_HAS_LOCATION (init))
8138 elocus = EXPR_LOCATION (init);
8140 if (!DECL_P (decl))
8142 error_at (elocus, "expected iteration declaration or initialization");
8143 return NULL;
8146 if (incr && TREE_CODE (incr) == MODOP_EXPR)
8148 if (orig_incr)
8149 TREE_VEC_ELT (orig_incr, i) = incr;
8150 incr = cp_build_modify_expr (elocus, TREE_OPERAND (incr, 0),
8151 TREE_CODE (TREE_OPERAND (incr, 1)),
8152 TREE_OPERAND (incr, 2),
8153 tf_warning_or_error);
8156 if (CLASS_TYPE_P (TREE_TYPE (decl)))
8158 if (code == OMP_SIMD)
8160 error_at (elocus, "%<#pragma omp simd%> used with class "
8161 "iteration variable %qE", decl);
8162 return NULL;
8164 if (code == CILK_FOR && i == 0)
8165 orig_decl = decl;
8166 if (handle_omp_for_class_iterator (i, locus, code, declv, orig_declv,
8167 initv, condv, incrv, &body,
8168 &pre_body, clauses, &last,
8169 collapse, ordered))
8170 return NULL;
8171 continue;
8174 if (!INTEGRAL_TYPE_P (TREE_TYPE (decl))
8175 && !TYPE_PTR_P (TREE_TYPE (decl)))
8177 error_at (elocus, "invalid type for iteration variable %qE", decl);
8178 return NULL;
8181 if (!processing_template_decl)
8183 init = fold_build_cleanup_point_expr (TREE_TYPE (init), init);
8184 init = cp_build_modify_expr (elocus, decl, NOP_EXPR, init,
8185 tf_warning_or_error);
8187 else
8188 init = build2 (MODIFY_EXPR, void_type_node, decl, init);
8189 if (cond
8190 && TREE_SIDE_EFFECTS (cond)
8191 && COMPARISON_CLASS_P (cond)
8192 && !processing_template_decl)
8194 tree t = TREE_OPERAND (cond, 0);
8195 if (TREE_SIDE_EFFECTS (t)
8196 && t != decl
8197 && (TREE_CODE (t) != NOP_EXPR
8198 || TREE_OPERAND (t, 0) != decl))
8199 TREE_OPERAND (cond, 0)
8200 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
8202 t = TREE_OPERAND (cond, 1);
8203 if (TREE_SIDE_EFFECTS (t)
8204 && t != decl
8205 && (TREE_CODE (t) != NOP_EXPR
8206 || TREE_OPERAND (t, 0) != decl))
8207 TREE_OPERAND (cond, 1)
8208 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
8210 if (decl == error_mark_node || init == error_mark_node)
8211 return NULL;
8213 TREE_VEC_ELT (declv, i) = decl;
8214 TREE_VEC_ELT (initv, i) = init;
8215 TREE_VEC_ELT (condv, i) = cond;
8216 TREE_VEC_ELT (incrv, i) = incr;
8217 i++;
8220 if (IS_EMPTY_STMT (pre_body))
8221 pre_body = NULL;
8223 if (code == CILK_FOR && !processing_template_decl)
8224 block = push_stmt_list ();
8226 omp_for = c_finish_omp_for (locus, code, declv, orig_declv, initv, condv,
8227 incrv, body, pre_body);
8229 /* Check for iterators appearing in lb, b or incr expressions. */
8230 if (omp_for && !c_omp_check_loop_iv (omp_for, orig_declv, cp_walk_subtrees))
8231 omp_for = NULL_TREE;
8233 if (omp_for == NULL)
8235 if (block)
8236 pop_stmt_list (block);
8237 return NULL;
8240 add_stmt (omp_for);
8242 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INCR (omp_for)); i++)
8244 decl = TREE_OPERAND (TREE_VEC_ELT (OMP_FOR_INIT (omp_for), i), 0);
8245 incr = TREE_VEC_ELT (OMP_FOR_INCR (omp_for), i);
8247 if (TREE_CODE (incr) != MODIFY_EXPR)
8248 continue;
8250 if (TREE_SIDE_EFFECTS (TREE_OPERAND (incr, 1))
8251 && BINARY_CLASS_P (TREE_OPERAND (incr, 1))
8252 && !processing_template_decl)
8254 tree t = TREE_OPERAND (TREE_OPERAND (incr, 1), 0);
8255 if (TREE_SIDE_EFFECTS (t)
8256 && t != decl
8257 && (TREE_CODE (t) != NOP_EXPR
8258 || TREE_OPERAND (t, 0) != decl))
8259 TREE_OPERAND (TREE_OPERAND (incr, 1), 0)
8260 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
8262 t = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
8263 if (TREE_SIDE_EFFECTS (t)
8264 && t != decl
8265 && (TREE_CODE (t) != NOP_EXPR
8266 || TREE_OPERAND (t, 0) != decl))
8267 TREE_OPERAND (TREE_OPERAND (incr, 1), 1)
8268 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
8271 if (orig_incr)
8272 TREE_VEC_ELT (OMP_FOR_INCR (omp_for), i) = TREE_VEC_ELT (orig_incr, i);
8274 OMP_FOR_CLAUSES (omp_for) = clauses;
8276 /* For simd loops with non-static data member iterators, we could have added
8277 OMP_CLAUSE_LINEAR clauses without OMP_CLAUSE_LINEAR_STEP. As we know the
8278 step at this point, fill it in. */
8279 if (code == OMP_SIMD && !processing_template_decl
8280 && TREE_VEC_LENGTH (OMP_FOR_INCR (omp_for)) == 1)
8281 for (tree c = omp_find_clause (clauses, OMP_CLAUSE_LINEAR); c;
8282 c = omp_find_clause (OMP_CLAUSE_CHAIN (c), OMP_CLAUSE_LINEAR))
8283 if (OMP_CLAUSE_LINEAR_STEP (c) == NULL_TREE)
8285 decl = TREE_OPERAND (TREE_VEC_ELT (OMP_FOR_INIT (omp_for), 0), 0);
8286 gcc_assert (decl == OMP_CLAUSE_DECL (c));
8287 incr = TREE_VEC_ELT (OMP_FOR_INCR (omp_for), 0);
8288 tree step, stept;
8289 switch (TREE_CODE (incr))
8291 case PREINCREMENT_EXPR:
8292 case POSTINCREMENT_EXPR:
8293 /* c_omp_for_incr_canonicalize_ptr() should have been
8294 called to massage things appropriately. */
8295 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (decl)));
8296 OMP_CLAUSE_LINEAR_STEP (c) = build_int_cst (TREE_TYPE (decl), 1);
8297 break;
8298 case PREDECREMENT_EXPR:
8299 case POSTDECREMENT_EXPR:
8300 /* c_omp_for_incr_canonicalize_ptr() should have been
8301 called to massage things appropriately. */
8302 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (decl)));
8303 OMP_CLAUSE_LINEAR_STEP (c)
8304 = build_int_cst (TREE_TYPE (decl), -1);
8305 break;
8306 case MODIFY_EXPR:
8307 gcc_assert (TREE_OPERAND (incr, 0) == decl);
8308 incr = TREE_OPERAND (incr, 1);
8309 switch (TREE_CODE (incr))
8311 case PLUS_EXPR:
8312 if (TREE_OPERAND (incr, 1) == decl)
8313 step = TREE_OPERAND (incr, 0);
8314 else
8315 step = TREE_OPERAND (incr, 1);
8316 break;
8317 case MINUS_EXPR:
8318 case POINTER_PLUS_EXPR:
8319 gcc_assert (TREE_OPERAND (incr, 0) == decl);
8320 step = TREE_OPERAND (incr, 1);
8321 break;
8322 default:
8323 gcc_unreachable ();
8325 stept = TREE_TYPE (decl);
8326 if (POINTER_TYPE_P (stept))
8327 stept = sizetype;
8328 step = fold_convert (stept, step);
8329 if (TREE_CODE (incr) == MINUS_EXPR)
8330 step = fold_build1 (NEGATE_EXPR, stept, step);
8331 OMP_CLAUSE_LINEAR_STEP (c) = step;
8332 break;
8333 default:
8334 gcc_unreachable ();
8338 if (block)
8340 tree omp_par = make_node (OMP_PARALLEL);
8341 TREE_TYPE (omp_par) = void_type_node;
8342 OMP_PARALLEL_CLAUSES (omp_par) = NULL_TREE;
8343 tree bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
8344 TREE_SIDE_EFFECTS (bind) = 1;
8345 BIND_EXPR_BODY (bind) = pop_stmt_list (block);
8346 OMP_PARALLEL_BODY (omp_par) = bind;
8347 if (OMP_FOR_PRE_BODY (omp_for))
8349 add_stmt (OMP_FOR_PRE_BODY (omp_for));
8350 OMP_FOR_PRE_BODY (omp_for) = NULL_TREE;
8352 init = TREE_VEC_ELT (OMP_FOR_INIT (omp_for), 0);
8353 decl = TREE_OPERAND (init, 0);
8354 cond = TREE_VEC_ELT (OMP_FOR_COND (omp_for), 0);
8355 incr = TREE_VEC_ELT (OMP_FOR_INCR (omp_for), 0);
8356 tree t = TREE_OPERAND (cond, 1), c, clauses, *pc;
8357 clauses = OMP_FOR_CLAUSES (omp_for);
8358 OMP_FOR_CLAUSES (omp_for) = NULL_TREE;
8359 for (pc = &clauses; *pc; )
8360 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_SCHEDULE)
8362 gcc_assert (OMP_FOR_CLAUSES (omp_for) == NULL_TREE);
8363 OMP_FOR_CLAUSES (omp_for) = *pc;
8364 *pc = OMP_CLAUSE_CHAIN (*pc);
8365 OMP_CLAUSE_CHAIN (OMP_FOR_CLAUSES (omp_for)) = NULL_TREE;
8367 else
8369 gcc_assert (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_FIRSTPRIVATE);
8370 pc = &OMP_CLAUSE_CHAIN (*pc);
8372 if (TREE_CODE (t) != INTEGER_CST)
8374 TREE_OPERAND (cond, 1) = get_temp_regvar (TREE_TYPE (t), t);
8375 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
8376 OMP_CLAUSE_DECL (c) = TREE_OPERAND (cond, 1);
8377 OMP_CLAUSE_CHAIN (c) = clauses;
8378 clauses = c;
8380 if (TREE_CODE (incr) == MODIFY_EXPR)
8382 t = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
8383 if (TREE_CODE (t) != INTEGER_CST)
8385 TREE_OPERAND (TREE_OPERAND (incr, 1), 1)
8386 = get_temp_regvar (TREE_TYPE (t), t);
8387 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
8388 OMP_CLAUSE_DECL (c) = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
8389 OMP_CLAUSE_CHAIN (c) = clauses;
8390 clauses = c;
8393 t = TREE_OPERAND (init, 1);
8394 if (TREE_CODE (t) != INTEGER_CST)
8396 TREE_OPERAND (init, 1) = get_temp_regvar (TREE_TYPE (t), t);
8397 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
8398 OMP_CLAUSE_DECL (c) = TREE_OPERAND (init, 1);
8399 OMP_CLAUSE_CHAIN (c) = clauses;
8400 clauses = c;
8402 if (orig_decl && orig_decl != decl)
8404 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
8405 OMP_CLAUSE_DECL (c) = orig_decl;
8406 OMP_CLAUSE_CHAIN (c) = clauses;
8407 clauses = c;
8409 if (last)
8411 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
8412 OMP_CLAUSE_DECL (c) = last;
8413 OMP_CLAUSE_CHAIN (c) = clauses;
8414 clauses = c;
8416 c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
8417 OMP_CLAUSE_DECL (c) = decl;
8418 OMP_CLAUSE_CHAIN (c) = clauses;
8419 clauses = c;
8420 c = build_omp_clause (input_location, OMP_CLAUSE__CILK_FOR_COUNT_);
8421 OMP_CLAUSE_OPERAND (c, 0)
8422 = cilk_for_number_of_iterations (omp_for);
8423 OMP_CLAUSE_CHAIN (c) = clauses;
8424 OMP_PARALLEL_CLAUSES (omp_par) = finish_omp_clauses (c, C_ORT_CILK);
8425 add_stmt (omp_par);
8426 return omp_par;
8428 else if (code == CILK_FOR && processing_template_decl)
8430 tree c, clauses = OMP_FOR_CLAUSES (omp_for);
8431 if (orig_decl && orig_decl != decl)
8433 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
8434 OMP_CLAUSE_DECL (c) = orig_decl;
8435 OMP_CLAUSE_CHAIN (c) = clauses;
8436 clauses = c;
8438 if (last)
8440 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
8441 OMP_CLAUSE_DECL (c) = last;
8442 OMP_CLAUSE_CHAIN (c) = clauses;
8443 clauses = c;
8445 OMP_FOR_CLAUSES (omp_for) = clauses;
8447 return omp_for;
8450 void
8451 finish_omp_atomic (enum tree_code code, enum tree_code opcode, tree lhs,
8452 tree rhs, tree v, tree lhs1, tree rhs1, bool seq_cst)
8454 tree orig_lhs;
8455 tree orig_rhs;
8456 tree orig_v;
8457 tree orig_lhs1;
8458 tree orig_rhs1;
8459 bool dependent_p;
8460 tree stmt;
8462 orig_lhs = lhs;
8463 orig_rhs = rhs;
8464 orig_v = v;
8465 orig_lhs1 = lhs1;
8466 orig_rhs1 = rhs1;
8467 dependent_p = false;
8468 stmt = NULL_TREE;
8470 /* Even in a template, we can detect invalid uses of the atomic
8471 pragma if neither LHS nor RHS is type-dependent. */
8472 if (processing_template_decl)
8474 dependent_p = (type_dependent_expression_p (lhs)
8475 || (rhs && type_dependent_expression_p (rhs))
8476 || (v && type_dependent_expression_p (v))
8477 || (lhs1 && type_dependent_expression_p (lhs1))
8478 || (rhs1 && type_dependent_expression_p (rhs1)));
8479 if (!dependent_p)
8481 lhs = build_non_dependent_expr (lhs);
8482 if (rhs)
8483 rhs = build_non_dependent_expr (rhs);
8484 if (v)
8485 v = build_non_dependent_expr (v);
8486 if (lhs1)
8487 lhs1 = build_non_dependent_expr (lhs1);
8488 if (rhs1)
8489 rhs1 = build_non_dependent_expr (rhs1);
8492 if (!dependent_p)
8494 bool swapped = false;
8495 if (rhs1 && cp_tree_equal (lhs, rhs))
8497 std::swap (rhs, rhs1);
8498 swapped = !commutative_tree_code (opcode);
8500 if (rhs1 && !cp_tree_equal (lhs, rhs1))
8502 if (code == OMP_ATOMIC)
8503 error ("%<#pragma omp atomic update%> uses two different "
8504 "expressions for memory");
8505 else
8506 error ("%<#pragma omp atomic capture%> uses two different "
8507 "expressions for memory");
8508 return;
8510 if (lhs1 && !cp_tree_equal (lhs, lhs1))
8512 if (code == OMP_ATOMIC)
8513 error ("%<#pragma omp atomic update%> uses two different "
8514 "expressions for memory");
8515 else
8516 error ("%<#pragma omp atomic capture%> uses two different "
8517 "expressions for memory");
8518 return;
8520 stmt = c_finish_omp_atomic (input_location, code, opcode, lhs, rhs,
8521 v, lhs1, rhs1, swapped, seq_cst,
8522 processing_template_decl != 0);
8523 if (stmt == error_mark_node)
8524 return;
8526 if (processing_template_decl)
8528 if (code == OMP_ATOMIC_READ)
8530 stmt = build_min_nt_loc (EXPR_LOCATION (orig_lhs),
8531 OMP_ATOMIC_READ, orig_lhs);
8532 OMP_ATOMIC_SEQ_CST (stmt) = seq_cst;
8533 stmt = build2 (MODIFY_EXPR, void_type_node, orig_v, stmt);
8535 else
8537 if (opcode == NOP_EXPR)
8538 stmt = build2 (MODIFY_EXPR, void_type_node, orig_lhs, orig_rhs);
8539 else
8540 stmt = build2 (opcode, void_type_node, orig_lhs, orig_rhs);
8541 if (orig_rhs1)
8542 stmt = build_min_nt_loc (EXPR_LOCATION (orig_rhs1),
8543 COMPOUND_EXPR, orig_rhs1, stmt);
8544 if (code != OMP_ATOMIC)
8546 stmt = build_min_nt_loc (EXPR_LOCATION (orig_lhs1),
8547 code, orig_lhs1, stmt);
8548 OMP_ATOMIC_SEQ_CST (stmt) = seq_cst;
8549 stmt = build2 (MODIFY_EXPR, void_type_node, orig_v, stmt);
8552 stmt = build2 (OMP_ATOMIC, void_type_node, integer_zero_node, stmt);
8553 OMP_ATOMIC_SEQ_CST (stmt) = seq_cst;
8555 finish_expr_stmt (stmt);
8558 void
8559 finish_omp_barrier (void)
8561 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_BARRIER);
8562 vec<tree, va_gc> *vec = make_tree_vector ();
8563 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
8564 release_tree_vector (vec);
8565 finish_expr_stmt (stmt);
8568 void
8569 finish_omp_flush (void)
8571 tree fn = builtin_decl_explicit (BUILT_IN_SYNC_SYNCHRONIZE);
8572 vec<tree, va_gc> *vec = make_tree_vector ();
8573 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
8574 release_tree_vector (vec);
8575 finish_expr_stmt (stmt);
8578 void
8579 finish_omp_taskwait (void)
8581 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_TASKWAIT);
8582 vec<tree, va_gc> *vec = make_tree_vector ();
8583 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
8584 release_tree_vector (vec);
8585 finish_expr_stmt (stmt);
8588 void
8589 finish_omp_taskyield (void)
8591 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_TASKYIELD);
8592 vec<tree, va_gc> *vec = make_tree_vector ();
8593 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
8594 release_tree_vector (vec);
8595 finish_expr_stmt (stmt);
8598 void
8599 finish_omp_cancel (tree clauses)
8601 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
8602 int mask = 0;
8603 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
8604 mask = 1;
8605 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
8606 mask = 2;
8607 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
8608 mask = 4;
8609 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
8610 mask = 8;
8611 else
8613 error ("%<#pragma omp cancel%> must specify one of "
8614 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> clauses");
8615 return;
8617 vec<tree, va_gc> *vec = make_tree_vector ();
8618 tree ifc = omp_find_clause (clauses, OMP_CLAUSE_IF);
8619 if (ifc != NULL_TREE)
8621 tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
8622 ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
8623 boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
8624 build_zero_cst (type));
8626 else
8627 ifc = boolean_true_node;
8628 vec->quick_push (build_int_cst (integer_type_node, mask));
8629 vec->quick_push (ifc);
8630 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
8631 release_tree_vector (vec);
8632 finish_expr_stmt (stmt);
8635 void
8636 finish_omp_cancellation_point (tree clauses)
8638 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
8639 int mask = 0;
8640 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
8641 mask = 1;
8642 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
8643 mask = 2;
8644 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
8645 mask = 4;
8646 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
8647 mask = 8;
8648 else
8650 error ("%<#pragma omp cancellation point%> must specify one of "
8651 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> clauses");
8652 return;
8654 vec<tree, va_gc> *vec
8655 = make_tree_vector_single (build_int_cst (integer_type_node, mask));
8656 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
8657 release_tree_vector (vec);
8658 finish_expr_stmt (stmt);
8661 /* Begin a __transaction_atomic or __transaction_relaxed statement.
8662 If PCOMPOUND is non-null, this is for a function-transaction-block, and we
8663 should create an extra compound stmt. */
8665 tree
8666 begin_transaction_stmt (location_t loc, tree *pcompound, int flags)
8668 tree r;
8670 if (pcompound)
8671 *pcompound = begin_compound_stmt (0);
8673 r = build_stmt (loc, TRANSACTION_EXPR, NULL_TREE);
8675 /* Only add the statement to the function if support enabled. */
8676 if (flag_tm)
8677 add_stmt (r);
8678 else
8679 error_at (loc, ((flags & TM_STMT_ATTR_RELAXED) != 0
8680 ? G_("%<__transaction_relaxed%> without "
8681 "transactional memory support enabled")
8682 : G_("%<__transaction_atomic%> without "
8683 "transactional memory support enabled")));
8685 TRANSACTION_EXPR_BODY (r) = push_stmt_list ();
8686 TREE_SIDE_EFFECTS (r) = 1;
8687 return r;
8690 /* End a __transaction_atomic or __transaction_relaxed statement.
8691 If COMPOUND_STMT is non-null, this is for a function-transaction-block,
8692 and we should end the compound. If NOEX is non-NULL, we wrap the body in
8693 a MUST_NOT_THROW_EXPR with NOEX as condition. */
8695 void
8696 finish_transaction_stmt (tree stmt, tree compound_stmt, int flags, tree noex)
8698 TRANSACTION_EXPR_BODY (stmt) = pop_stmt_list (TRANSACTION_EXPR_BODY (stmt));
8699 TRANSACTION_EXPR_OUTER (stmt) = (flags & TM_STMT_ATTR_OUTER) != 0;
8700 TRANSACTION_EXPR_RELAXED (stmt) = (flags & TM_STMT_ATTR_RELAXED) != 0;
8701 TRANSACTION_EXPR_IS_STMT (stmt) = 1;
8703 /* noexcept specifications are not allowed for function transactions. */
8704 gcc_assert (!(noex && compound_stmt));
8705 if (noex)
8707 tree body = build_must_not_throw_expr (TRANSACTION_EXPR_BODY (stmt),
8708 noex);
8709 protected_set_expr_location
8710 (body, EXPR_LOCATION (TRANSACTION_EXPR_BODY (stmt)));
8711 TREE_SIDE_EFFECTS (body) = 1;
8712 TRANSACTION_EXPR_BODY (stmt) = body;
8715 if (compound_stmt)
8716 finish_compound_stmt (compound_stmt);
8719 /* Build a __transaction_atomic or __transaction_relaxed expression. If
8720 NOEX is non-NULL, we wrap the body in a MUST_NOT_THROW_EXPR with NOEX as
8721 condition. */
8723 tree
8724 build_transaction_expr (location_t loc, tree expr, int flags, tree noex)
8726 tree ret;
8727 if (noex)
8729 expr = build_must_not_throw_expr (expr, noex);
8730 protected_set_expr_location (expr, loc);
8731 TREE_SIDE_EFFECTS (expr) = 1;
8733 ret = build1 (TRANSACTION_EXPR, TREE_TYPE (expr), expr);
8734 if (flags & TM_STMT_ATTR_RELAXED)
8735 TRANSACTION_EXPR_RELAXED (ret) = 1;
8736 TREE_SIDE_EFFECTS (ret) = 1;
8737 SET_EXPR_LOCATION (ret, loc);
8738 return ret;
8741 void
8742 init_cp_semantics (void)
8746 /* Build a STATIC_ASSERT for a static assertion with the condition
8747 CONDITION and the message text MESSAGE. LOCATION is the location
8748 of the static assertion in the source code. When MEMBER_P, this
8749 static assertion is a member of a class. */
8750 void
8751 finish_static_assert (tree condition, tree message, location_t location,
8752 bool member_p)
8754 if (message == NULL_TREE
8755 || message == error_mark_node
8756 || condition == NULL_TREE
8757 || condition == error_mark_node)
8758 return;
8760 if (check_for_bare_parameter_packs (condition))
8761 condition = error_mark_node;
8763 if (type_dependent_expression_p (condition)
8764 || value_dependent_expression_p (condition))
8766 /* We're in a template; build a STATIC_ASSERT and put it in
8767 the right place. */
8768 tree assertion;
8770 assertion = make_node (STATIC_ASSERT);
8771 STATIC_ASSERT_CONDITION (assertion) = condition;
8772 STATIC_ASSERT_MESSAGE (assertion) = message;
8773 STATIC_ASSERT_SOURCE_LOCATION (assertion) = location;
8775 if (member_p)
8776 maybe_add_class_template_decl_list (current_class_type,
8777 assertion,
8778 /*friend_p=*/0);
8779 else
8780 add_stmt (assertion);
8782 return;
8785 /* Fold the expression and convert it to a boolean value. */
8786 condition = instantiate_non_dependent_expr (condition);
8787 condition = cp_convert (boolean_type_node, condition, tf_warning_or_error);
8788 condition = maybe_constant_value (condition);
8790 if (TREE_CODE (condition) == INTEGER_CST && !integer_zerop (condition))
8791 /* Do nothing; the condition is satisfied. */
8793 else
8795 location_t saved_loc = input_location;
8797 input_location = location;
8798 if (TREE_CODE (condition) == INTEGER_CST
8799 && integer_zerop (condition))
8801 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
8802 (TREE_TYPE (TREE_TYPE (message))));
8803 int len = TREE_STRING_LENGTH (message) / sz - 1;
8804 /* Report the error. */
8805 if (len == 0)
8806 error ("static assertion failed");
8807 else
8808 error ("static assertion failed: %s",
8809 TREE_STRING_POINTER (message));
8811 else if (condition && condition != error_mark_node)
8813 error ("non-constant condition for static assertion");
8814 if (require_potential_rvalue_constant_expression (condition))
8815 cxx_constant_value (condition);
8817 input_location = saved_loc;
8821 /* Implements the C++0x decltype keyword. Returns the type of EXPR,
8822 suitable for use as a type-specifier.
8824 ID_EXPRESSION_OR_MEMBER_ACCESS_P is true when EXPR was parsed as an
8825 id-expression or a class member access, FALSE when it was parsed as
8826 a full expression. */
8828 tree
8829 finish_decltype_type (tree expr, bool id_expression_or_member_access_p,
8830 tsubst_flags_t complain)
8832 tree type = NULL_TREE;
8834 if (!expr || error_operand_p (expr))
8835 return error_mark_node;
8837 if (TYPE_P (expr)
8838 || TREE_CODE (expr) == TYPE_DECL
8839 || (TREE_CODE (expr) == BIT_NOT_EXPR
8840 && TYPE_P (TREE_OPERAND (expr, 0))))
8842 if (complain & tf_error)
8843 error ("argument to decltype must be an expression");
8844 return error_mark_node;
8847 /* Depending on the resolution of DR 1172, we may later need to distinguish
8848 instantiation-dependent but not type-dependent expressions so that, say,
8849 A<decltype(sizeof(T))>::U doesn't require 'typename'. */
8850 if (instantiation_dependent_uneval_expression_p (expr))
8852 type = cxx_make_type (DECLTYPE_TYPE);
8853 DECLTYPE_TYPE_EXPR (type) = expr;
8854 DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (type)
8855 = id_expression_or_member_access_p;
8856 SET_TYPE_STRUCTURAL_EQUALITY (type);
8858 return type;
8861 /* The type denoted by decltype(e) is defined as follows: */
8863 expr = resolve_nondeduced_context (expr, complain);
8865 if (invalid_nonstatic_memfn_p (input_location, expr, complain))
8866 return error_mark_node;
8868 if (type_unknown_p (expr))
8870 if (complain & tf_error)
8871 error ("decltype cannot resolve address of overloaded function");
8872 return error_mark_node;
8875 /* To get the size of a static data member declared as an array of
8876 unknown bound, we need to instantiate it. */
8877 if (VAR_P (expr)
8878 && VAR_HAD_UNKNOWN_BOUND (expr)
8879 && DECL_TEMPLATE_INSTANTIATION (expr))
8880 instantiate_decl (expr, /*defer_ok*/true, /*expl_inst_mem*/false);
8882 if (id_expression_or_member_access_p)
8884 /* If e is an id-expression or a class member access (5.2.5
8885 [expr.ref]), decltype(e) is defined as the type of the entity
8886 named by e. If there is no such entity, or e names a set of
8887 overloaded functions, the program is ill-formed. */
8888 if (identifier_p (expr))
8889 expr = lookup_name (expr);
8891 if (INDIRECT_REF_P (expr))
8892 /* This can happen when the expression is, e.g., "a.b". Just
8893 look at the underlying operand. */
8894 expr = TREE_OPERAND (expr, 0);
8896 if (TREE_CODE (expr) == OFFSET_REF
8897 || TREE_CODE (expr) == MEMBER_REF
8898 || TREE_CODE (expr) == SCOPE_REF)
8899 /* We're only interested in the field itself. If it is a
8900 BASELINK, we will need to see through it in the next
8901 step. */
8902 expr = TREE_OPERAND (expr, 1);
8904 if (BASELINK_P (expr))
8905 /* See through BASELINK nodes to the underlying function. */
8906 expr = BASELINK_FUNCTIONS (expr);
8908 /* decltype of a decomposition name drops references in the tuple case
8909 (unlike decltype of a normal variable) and keeps cv-qualifiers from
8910 the containing object in the other cases (unlike decltype of a member
8911 access expression). */
8912 if (DECL_DECOMPOSITION_P (expr))
8914 if (DECL_HAS_VALUE_EXPR_P (expr))
8915 /* Expr is an array or struct subobject proxy, handle
8916 bit-fields properly. */
8917 return unlowered_expr_type (expr);
8918 else
8919 /* Expr is a reference variable for the tuple case. */
8920 return lookup_decomp_type (expr);
8923 switch (TREE_CODE (expr))
8925 case FIELD_DECL:
8926 if (DECL_BIT_FIELD_TYPE (expr))
8928 type = DECL_BIT_FIELD_TYPE (expr);
8929 break;
8931 /* Fall through for fields that aren't bitfields. */
8932 gcc_fallthrough ();
8934 case FUNCTION_DECL:
8935 case VAR_DECL:
8936 case CONST_DECL:
8937 case PARM_DECL:
8938 case RESULT_DECL:
8939 case TEMPLATE_PARM_INDEX:
8940 expr = mark_type_use (expr);
8941 type = TREE_TYPE (expr);
8942 break;
8944 case ERROR_MARK:
8945 type = error_mark_node;
8946 break;
8948 case COMPONENT_REF:
8949 case COMPOUND_EXPR:
8950 mark_type_use (expr);
8951 type = is_bitfield_expr_with_lowered_type (expr);
8952 if (!type)
8953 type = TREE_TYPE (TREE_OPERAND (expr, 1));
8954 break;
8956 case BIT_FIELD_REF:
8957 gcc_unreachable ();
8959 case INTEGER_CST:
8960 case PTRMEM_CST:
8961 /* We can get here when the id-expression refers to an
8962 enumerator or non-type template parameter. */
8963 type = TREE_TYPE (expr);
8964 break;
8966 default:
8967 /* Handle instantiated template non-type arguments. */
8968 type = TREE_TYPE (expr);
8969 break;
8972 else
8974 /* Within a lambda-expression:
8976 Every occurrence of decltype((x)) where x is a possibly
8977 parenthesized id-expression that names an entity of
8978 automatic storage duration is treated as if x were
8979 transformed into an access to a corresponding data member
8980 of the closure type that would have been declared if x
8981 were a use of the denoted entity. */
8982 if (outer_automatic_var_p (expr)
8983 && current_function_decl
8984 && LAMBDA_FUNCTION_P (current_function_decl))
8985 type = capture_decltype (expr);
8986 else if (error_operand_p (expr))
8987 type = error_mark_node;
8988 else if (expr == current_class_ptr)
8989 /* If the expression is just "this", we want the
8990 cv-unqualified pointer for the "this" type. */
8991 type = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
8992 else
8994 /* Otherwise, where T is the type of e, if e is an lvalue,
8995 decltype(e) is defined as T&; if an xvalue, T&&; otherwise, T. */
8996 cp_lvalue_kind clk = lvalue_kind (expr);
8997 type = unlowered_expr_type (expr);
8998 gcc_assert (TREE_CODE (type) != REFERENCE_TYPE);
9000 /* For vector types, pick a non-opaque variant. */
9001 if (VECTOR_TYPE_P (type))
9002 type = strip_typedefs (type);
9004 if (clk != clk_none && !(clk & clk_class))
9005 type = cp_build_reference_type (type, (clk & clk_rvalueref));
9009 return type;
9012 /* Called from trait_expr_value to evaluate either __has_nothrow_assign or
9013 __has_nothrow_copy, depending on assign_p. Returns true iff all
9014 the copy {ctor,assign} fns are nothrow. */
9016 static bool
9017 classtype_has_nothrow_assign_or_copy_p (tree type, bool assign_p)
9019 tree fns = NULL_TREE;
9021 if (assign_p || TYPE_HAS_COPY_CTOR (type))
9022 fns = get_class_binding (type, assign_p ? assign_op_identifier
9023 : ctor_identifier);
9025 bool saw_copy = false;
9026 for (ovl_iterator iter (fns); iter; ++iter)
9028 tree fn = *iter;
9030 if (copy_fn_p (fn) > 0)
9032 saw_copy = true;
9033 maybe_instantiate_noexcept (fn);
9034 if (!TYPE_NOTHROW_P (TREE_TYPE (fn)))
9035 return false;
9039 return saw_copy;
9042 /* Actually evaluates the trait. */
9044 static bool
9045 trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
9047 enum tree_code type_code1;
9048 tree t;
9050 type_code1 = TREE_CODE (type1);
9052 switch (kind)
9054 case CPTK_HAS_NOTHROW_ASSIGN:
9055 type1 = strip_array_types (type1);
9056 return (!CP_TYPE_CONST_P (type1) && type_code1 != REFERENCE_TYPE
9057 && (trait_expr_value (CPTK_HAS_TRIVIAL_ASSIGN, type1, type2)
9058 || (CLASS_TYPE_P (type1)
9059 && classtype_has_nothrow_assign_or_copy_p (type1,
9060 true))));
9062 case CPTK_HAS_TRIVIAL_ASSIGN:
9063 /* ??? The standard seems to be missing the "or array of such a class
9064 type" wording for this trait. */
9065 type1 = strip_array_types (type1);
9066 return (!CP_TYPE_CONST_P (type1) && type_code1 != REFERENCE_TYPE
9067 && (trivial_type_p (type1)
9068 || (CLASS_TYPE_P (type1)
9069 && TYPE_HAS_TRIVIAL_COPY_ASSIGN (type1))));
9071 case CPTK_HAS_NOTHROW_CONSTRUCTOR:
9072 type1 = strip_array_types (type1);
9073 return (trait_expr_value (CPTK_HAS_TRIVIAL_CONSTRUCTOR, type1, type2)
9074 || (CLASS_TYPE_P (type1)
9075 && (t = locate_ctor (type1))
9076 && (maybe_instantiate_noexcept (t),
9077 TYPE_NOTHROW_P (TREE_TYPE (t)))));
9079 case CPTK_HAS_TRIVIAL_CONSTRUCTOR:
9080 type1 = strip_array_types (type1);
9081 return (trivial_type_p (type1)
9082 || (CLASS_TYPE_P (type1) && TYPE_HAS_TRIVIAL_DFLT (type1)));
9084 case CPTK_HAS_NOTHROW_COPY:
9085 type1 = strip_array_types (type1);
9086 return (trait_expr_value (CPTK_HAS_TRIVIAL_COPY, type1, type2)
9087 || (CLASS_TYPE_P (type1)
9088 && classtype_has_nothrow_assign_or_copy_p (type1, false)));
9090 case CPTK_HAS_TRIVIAL_COPY:
9091 /* ??? The standard seems to be missing the "or array of such a class
9092 type" wording for this trait. */
9093 type1 = strip_array_types (type1);
9094 return (trivial_type_p (type1) || type_code1 == REFERENCE_TYPE
9095 || (CLASS_TYPE_P (type1) && TYPE_HAS_TRIVIAL_COPY_CTOR (type1)));
9097 case CPTK_HAS_TRIVIAL_DESTRUCTOR:
9098 type1 = strip_array_types (type1);
9099 return (trivial_type_p (type1) || type_code1 == REFERENCE_TYPE
9100 || (CLASS_TYPE_P (type1)
9101 && TYPE_HAS_TRIVIAL_DESTRUCTOR (type1)));
9103 case CPTK_HAS_VIRTUAL_DESTRUCTOR:
9104 return type_has_virtual_destructor (type1);
9106 case CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS:
9107 return type_has_unique_obj_representations (type1);
9109 case CPTK_IS_ABSTRACT:
9110 return ABSTRACT_CLASS_TYPE_P (type1);
9112 case CPTK_IS_AGGREGATE:
9113 return CP_AGGREGATE_TYPE_P (type1);
9115 case CPTK_IS_BASE_OF:
9116 return (NON_UNION_CLASS_TYPE_P (type1) && NON_UNION_CLASS_TYPE_P (type2)
9117 && (same_type_ignoring_top_level_qualifiers_p (type1, type2)
9118 || DERIVED_FROM_P (type1, type2)));
9120 case CPTK_IS_CLASS:
9121 return NON_UNION_CLASS_TYPE_P (type1);
9123 case CPTK_IS_EMPTY:
9124 return NON_UNION_CLASS_TYPE_P (type1) && CLASSTYPE_EMPTY_P (type1);
9126 case CPTK_IS_ENUM:
9127 return type_code1 == ENUMERAL_TYPE;
9129 case CPTK_IS_FINAL:
9130 return CLASS_TYPE_P (type1) && CLASSTYPE_FINAL (type1);
9132 case CPTK_IS_LITERAL_TYPE:
9133 return literal_type_p (type1);
9135 case CPTK_IS_POD:
9136 return pod_type_p (type1);
9138 case CPTK_IS_POLYMORPHIC:
9139 return CLASS_TYPE_P (type1) && TYPE_POLYMORPHIC_P (type1);
9141 case CPTK_IS_SAME_AS:
9142 return same_type_p (type1, type2);
9144 case CPTK_IS_STD_LAYOUT:
9145 return std_layout_type_p (type1);
9147 case CPTK_IS_TRIVIAL:
9148 return trivial_type_p (type1);
9150 case CPTK_IS_TRIVIALLY_ASSIGNABLE:
9151 return is_trivially_xible (MODIFY_EXPR, type1, type2);
9153 case CPTK_IS_TRIVIALLY_CONSTRUCTIBLE:
9154 return is_trivially_xible (INIT_EXPR, type1, type2);
9156 case CPTK_IS_TRIVIALLY_COPYABLE:
9157 return trivially_copyable_p (type1);
9159 case CPTK_IS_UNION:
9160 return type_code1 == UNION_TYPE;
9162 case CPTK_IS_ASSIGNABLE:
9163 return is_xible (MODIFY_EXPR, type1, type2);
9165 case CPTK_IS_CONSTRUCTIBLE:
9166 return is_xible (INIT_EXPR, type1, type2);
9168 default:
9169 gcc_unreachable ();
9170 return false;
9174 /* If TYPE is an array of unknown bound, or (possibly cv-qualified)
9175 void, or a complete type, returns true, otherwise false. */
9177 static bool
9178 check_trait_type (tree type)
9180 if (type == NULL_TREE)
9181 return true;
9183 if (TREE_CODE (type) == TREE_LIST)
9184 return (check_trait_type (TREE_VALUE (type))
9185 && check_trait_type (TREE_CHAIN (type)));
9187 if (TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type)
9188 && COMPLETE_TYPE_P (TREE_TYPE (type)))
9189 return true;
9191 if (VOID_TYPE_P (type))
9192 return true;
9194 return !!complete_type_or_else (strip_array_types (type), NULL_TREE);
9197 /* Process a trait expression. */
9199 tree
9200 finish_trait_expr (cp_trait_kind kind, tree type1, tree type2)
9202 if (type1 == error_mark_node
9203 || type2 == error_mark_node)
9204 return error_mark_node;
9206 if (processing_template_decl)
9208 tree trait_expr = make_node (TRAIT_EXPR);
9209 TREE_TYPE (trait_expr) = boolean_type_node;
9210 TRAIT_EXPR_TYPE1 (trait_expr) = type1;
9211 TRAIT_EXPR_TYPE2 (trait_expr) = type2;
9212 TRAIT_EXPR_KIND (trait_expr) = kind;
9213 return trait_expr;
9216 switch (kind)
9218 case CPTK_HAS_NOTHROW_ASSIGN:
9219 case CPTK_HAS_TRIVIAL_ASSIGN:
9220 case CPTK_HAS_NOTHROW_CONSTRUCTOR:
9221 case CPTK_HAS_TRIVIAL_CONSTRUCTOR:
9222 case CPTK_HAS_NOTHROW_COPY:
9223 case CPTK_HAS_TRIVIAL_COPY:
9224 case CPTK_HAS_TRIVIAL_DESTRUCTOR:
9225 case CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS:
9226 case CPTK_HAS_VIRTUAL_DESTRUCTOR:
9227 case CPTK_IS_ABSTRACT:
9228 case CPTK_IS_AGGREGATE:
9229 case CPTK_IS_EMPTY:
9230 case CPTK_IS_FINAL:
9231 case CPTK_IS_LITERAL_TYPE:
9232 case CPTK_IS_POD:
9233 case CPTK_IS_POLYMORPHIC:
9234 case CPTK_IS_STD_LAYOUT:
9235 case CPTK_IS_TRIVIAL:
9236 case CPTK_IS_TRIVIALLY_COPYABLE:
9237 if (!check_trait_type (type1))
9238 return error_mark_node;
9239 break;
9241 case CPTK_IS_ASSIGNABLE:
9242 case CPTK_IS_CONSTRUCTIBLE:
9243 break;
9245 case CPTK_IS_TRIVIALLY_ASSIGNABLE:
9246 case CPTK_IS_TRIVIALLY_CONSTRUCTIBLE:
9247 if (!check_trait_type (type1)
9248 || !check_trait_type (type2))
9249 return error_mark_node;
9250 break;
9252 case CPTK_IS_BASE_OF:
9253 if (NON_UNION_CLASS_TYPE_P (type1) && NON_UNION_CLASS_TYPE_P (type2)
9254 && !same_type_ignoring_top_level_qualifiers_p (type1, type2)
9255 && !complete_type_or_else (type2, NULL_TREE))
9256 /* We already issued an error. */
9257 return error_mark_node;
9258 break;
9260 case CPTK_IS_CLASS:
9261 case CPTK_IS_ENUM:
9262 case CPTK_IS_UNION:
9263 case CPTK_IS_SAME_AS:
9264 break;
9266 default:
9267 gcc_unreachable ();
9270 return (trait_expr_value (kind, type1, type2)
9271 ? boolean_true_node : boolean_false_node);
9274 /* Do-nothing variants of functions to handle pragma FLOAT_CONST_DECIMAL64,
9275 which is ignored for C++. */
9277 void
9278 set_float_const_decimal64 (void)
9282 void
9283 clear_float_const_decimal64 (void)
9287 bool
9288 float_const_decimal64_p (void)
9290 return 0;
9294 /* Return true if T designates the implied `this' parameter. */
9296 bool
9297 is_this_parameter (tree t)
9299 if (!DECL_P (t) || DECL_NAME (t) != this_identifier)
9300 return false;
9301 gcc_assert (TREE_CODE (t) == PARM_DECL || is_capture_proxy (t)
9302 || (cp_binding_oracle && TREE_CODE (t) == VAR_DECL));
9303 return true;
9306 /* Insert the deduced return type for an auto function. */
9308 void
9309 apply_deduced_return_type (tree fco, tree return_type)
9311 tree result;
9313 if (return_type == error_mark_node)
9314 return;
9316 if (DECL_CONV_FN_P (fco))
9317 DECL_NAME (fco) = make_conv_op_name (return_type);
9319 TREE_TYPE (fco) = change_return_type (return_type, TREE_TYPE (fco));
9321 result = DECL_RESULT (fco);
9322 if (result == NULL_TREE)
9323 return;
9324 if (TREE_TYPE (result) == return_type)
9325 return;
9327 if (!processing_template_decl && !VOID_TYPE_P (return_type)
9328 && !complete_type_or_else (return_type, NULL_TREE))
9329 return;
9331 /* We already have a DECL_RESULT from start_preparsed_function.
9332 Now we need to redo the work it and allocate_struct_function
9333 did to reflect the new type. */
9334 gcc_assert (current_function_decl == fco);
9335 result = build_decl (input_location, RESULT_DECL, NULL_TREE,
9336 TYPE_MAIN_VARIANT (return_type));
9337 DECL_ARTIFICIAL (result) = 1;
9338 DECL_IGNORED_P (result) = 1;
9339 cp_apply_type_quals_to_decl (cp_type_quals (return_type),
9340 result);
9342 DECL_RESULT (fco) = result;
9344 if (!processing_template_decl)
9346 bool aggr = aggregate_value_p (result, fco);
9347 #ifdef PCC_STATIC_STRUCT_RETURN
9348 cfun->returns_pcc_struct = aggr;
9349 #endif
9350 cfun->returns_struct = aggr;
9355 /* DECL is a local variable or parameter from the surrounding scope of a
9356 lambda-expression. Returns the decltype for a use of the capture field
9357 for DECL even if it hasn't been captured yet. */
9359 static tree
9360 capture_decltype (tree decl)
9362 tree lam = CLASSTYPE_LAMBDA_EXPR (DECL_CONTEXT (current_function_decl));
9363 /* FIXME do lookup instead of list walk? */
9364 tree cap = value_member (decl, LAMBDA_EXPR_CAPTURE_LIST (lam));
9365 tree type;
9367 if (cap)
9368 type = TREE_TYPE (TREE_PURPOSE (cap));
9369 else
9370 switch (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lam))
9372 case CPLD_NONE:
9373 error ("%qD is not captured", decl);
9374 return error_mark_node;
9376 case CPLD_COPY:
9377 type = TREE_TYPE (decl);
9378 if (TREE_CODE (type) == REFERENCE_TYPE
9379 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
9380 type = TREE_TYPE (type);
9381 break;
9383 case CPLD_REFERENCE:
9384 type = TREE_TYPE (decl);
9385 if (TREE_CODE (type) != REFERENCE_TYPE)
9386 type = build_reference_type (TREE_TYPE (decl));
9387 break;
9389 default:
9390 gcc_unreachable ();
9393 if (TREE_CODE (type) != REFERENCE_TYPE)
9395 if (!LAMBDA_EXPR_MUTABLE_P (lam))
9396 type = cp_build_qualified_type (type, (cp_type_quals (type)
9397 |TYPE_QUAL_CONST));
9398 type = build_reference_type (type);
9400 return type;
9403 /* Build a unary fold expression of EXPR over OP. If IS_RIGHT is true,
9404 this is a right unary fold. Otherwise it is a left unary fold. */
9406 static tree
9407 finish_unary_fold_expr (tree expr, int op, tree_code dir)
9409 // Build a pack expansion (assuming expr has pack type).
9410 if (!uses_parameter_packs (expr))
9412 error_at (location_of (expr), "operand of fold expression has no "
9413 "unexpanded parameter packs");
9414 return error_mark_node;
9416 tree pack = make_pack_expansion (expr);
9418 // Build the fold expression.
9419 tree code = build_int_cstu (integer_type_node, abs (op));
9420 tree fold = build_min_nt_loc (UNKNOWN_LOCATION, dir, code, pack);
9421 FOLD_EXPR_MODIFY_P (fold) = (op < 0);
9422 return fold;
9425 tree
9426 finish_left_unary_fold_expr (tree expr, int op)
9428 return finish_unary_fold_expr (expr, op, UNARY_LEFT_FOLD_EXPR);
9431 tree
9432 finish_right_unary_fold_expr (tree expr, int op)
9434 return finish_unary_fold_expr (expr, op, UNARY_RIGHT_FOLD_EXPR);
9437 /* Build a binary fold expression over EXPR1 and EXPR2. The
9438 associativity of the fold is determined by EXPR1 and EXPR2 (whichever
9439 has an unexpanded parameter pack). */
9441 tree
9442 finish_binary_fold_expr (tree pack, tree init, int op, tree_code dir)
9444 pack = make_pack_expansion (pack);
9445 tree code = build_int_cstu (integer_type_node, abs (op));
9446 tree fold = build_min_nt_loc (UNKNOWN_LOCATION, dir, code, pack, init);
9447 FOLD_EXPR_MODIFY_P (fold) = (op < 0);
9448 return fold;
9451 tree
9452 finish_binary_fold_expr (tree expr1, tree expr2, int op)
9454 // Determine which expr has an unexpanded parameter pack and
9455 // set the pack and initial term.
9456 bool pack1 = uses_parameter_packs (expr1);
9457 bool pack2 = uses_parameter_packs (expr2);
9458 if (pack1 && !pack2)
9459 return finish_binary_fold_expr (expr1, expr2, op, BINARY_RIGHT_FOLD_EXPR);
9460 else if (pack2 && !pack1)
9461 return finish_binary_fold_expr (expr2, expr1, op, BINARY_LEFT_FOLD_EXPR);
9462 else
9464 if (pack1)
9465 error ("both arguments in binary fold have unexpanded parameter packs");
9466 else
9467 error ("no unexpanded parameter packs in binary fold");
9469 return error_mark_node;
9472 /* Finish __builtin_launder (arg). */
9474 tree
9475 finish_builtin_launder (location_t loc, tree arg, tsubst_flags_t complain)
9477 tree orig_arg = arg;
9478 if (!type_dependent_expression_p (arg))
9479 arg = decay_conversion (arg, complain);
9480 if (error_operand_p (arg))
9481 return error_mark_node;
9482 if (!type_dependent_expression_p (arg)
9483 && TREE_CODE (TREE_TYPE (arg)) != POINTER_TYPE)
9485 error_at (loc, "non-pointer argument to %<__builtin_launder%>");
9486 return error_mark_node;
9488 if (processing_template_decl)
9489 arg = orig_arg;
9490 return build_call_expr_internal_loc (loc, IFN_LAUNDER,
9491 TREE_TYPE (arg), 1, arg);
9494 #include "gt-cp-semantics.h"