Split out parts of scompare_loc_descriptor and emit_store_flag
[official-gcc.git] / gcc / cp / semantics.c
blobb2e58d2dbf056eb8d79f96f4512f6e03b6c0e138
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))
2708 type = do_auto_deduction (type, compound_literal, anode, complain,
2709 adc_variable_type);
2711 if (processing_template_decl)
2713 TREE_TYPE (compound_literal) = type;
2714 /* Mark the expression as a compound literal. */
2715 TREE_HAS_CONSTRUCTOR (compound_literal) = 1;
2716 if (fcl_context == fcl_c99)
2717 CONSTRUCTOR_C99_COMPOUND_LITERAL (compound_literal) = 1;
2718 return compound_literal;
2721 type = complete_type (type);
2723 if (TYPE_NON_AGGREGATE_CLASS (type))
2725 /* Trying to deal with a CONSTRUCTOR instead of a TREE_LIST
2726 everywhere that deals with function arguments would be a pain, so
2727 just wrap it in a TREE_LIST. The parser set a flag so we know
2728 that it came from T{} rather than T({}). */
2729 CONSTRUCTOR_IS_DIRECT_INIT (compound_literal) = 1;
2730 compound_literal = build_tree_list (NULL_TREE, compound_literal);
2731 return build_functional_cast (type, compound_literal, complain);
2734 if (TREE_CODE (type) == ARRAY_TYPE
2735 && check_array_initializer (NULL_TREE, type, compound_literal))
2736 return error_mark_node;
2737 compound_literal = reshape_init (type, compound_literal, complain);
2738 if (SCALAR_TYPE_P (type)
2739 && !BRACE_ENCLOSED_INITIALIZER_P (compound_literal)
2740 && !check_narrowing (type, compound_literal, complain))
2741 return error_mark_node;
2742 if (TREE_CODE (type) == ARRAY_TYPE
2743 && TYPE_DOMAIN (type) == NULL_TREE)
2745 cp_complete_array_type_or_error (&type, compound_literal,
2746 false, complain);
2747 if (type == error_mark_node)
2748 return error_mark_node;
2750 compound_literal = digest_init_flags (type, compound_literal, LOOKUP_NORMAL,
2751 complain);
2752 if (TREE_CODE (compound_literal) == CONSTRUCTOR)
2754 TREE_HAS_CONSTRUCTOR (compound_literal) = true;
2755 if (fcl_context == fcl_c99)
2756 CONSTRUCTOR_C99_COMPOUND_LITERAL (compound_literal) = 1;
2759 /* Put static/constant array temporaries in static variables. */
2760 /* FIXME all C99 compound literals should be variables rather than C++
2761 temporaries, unless they are used as an aggregate initializer. */
2762 if ((!at_function_scope_p () || CP_TYPE_CONST_P (type))
2763 && fcl_context == fcl_c99
2764 && TREE_CODE (type) == ARRAY_TYPE
2765 && !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
2766 && initializer_constant_valid_p (compound_literal, type))
2768 tree decl = create_temporary_var (type);
2769 DECL_INITIAL (decl) = compound_literal;
2770 TREE_STATIC (decl) = 1;
2771 if (literal_type_p (type) && CP_TYPE_CONST_NON_VOLATILE_P (type))
2773 /* 5.19 says that a constant expression can include an
2774 lvalue-rvalue conversion applied to "a glvalue of literal type
2775 that refers to a non-volatile temporary object initialized
2776 with a constant expression". Rather than try to communicate
2777 that this VAR_DECL is a temporary, just mark it constexpr. */
2778 DECL_DECLARED_CONSTEXPR_P (decl) = true;
2779 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
2780 TREE_CONSTANT (decl) = true;
2782 cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
2783 decl = pushdecl_top_level (decl);
2784 DECL_NAME (decl) = make_anon_name ();
2785 SET_DECL_ASSEMBLER_NAME (decl, DECL_NAME (decl));
2786 /* Make sure the destructor is callable. */
2787 tree clean = cxx_maybe_build_cleanup (decl, complain);
2788 if (clean == error_mark_node)
2789 return error_mark_node;
2790 return decl;
2793 /* Represent other compound literals with TARGET_EXPR so we produce
2794 an lvalue, but can elide copies. */
2795 if (!VECTOR_TYPE_P (type))
2796 compound_literal = get_target_expr_sfinae (compound_literal, complain);
2798 return compound_literal;
2801 /* Return the declaration for the function-name variable indicated by
2802 ID. */
2804 tree
2805 finish_fname (tree id)
2807 tree decl;
2809 decl = fname_decl (input_location, C_RID_CODE (id), id);
2810 if (processing_template_decl && current_function_decl
2811 && decl != error_mark_node)
2812 decl = DECL_NAME (decl);
2813 return decl;
2816 /* Finish a translation unit. */
2818 void
2819 finish_translation_unit (void)
2821 /* In case there were missing closebraces,
2822 get us back to the global binding level. */
2823 pop_everything ();
2824 while (current_namespace != global_namespace)
2825 pop_namespace ();
2827 /* Do file scope __FUNCTION__ et al. */
2828 finish_fname_decls ();
2831 /* Finish a template type parameter, specified as AGGR IDENTIFIER.
2832 Returns the parameter. */
2834 tree
2835 finish_template_type_parm (tree aggr, tree identifier)
2837 if (aggr != class_type_node)
2839 permerror (input_location, "template type parameters must use the keyword %<class%> or %<typename%>");
2840 aggr = class_type_node;
2843 return build_tree_list (aggr, identifier);
2846 /* Finish a template template parameter, specified as AGGR IDENTIFIER.
2847 Returns the parameter. */
2849 tree
2850 finish_template_template_parm (tree aggr, tree identifier)
2852 tree decl = build_decl (input_location,
2853 TYPE_DECL, identifier, NULL_TREE);
2855 tree tmpl = build_lang_decl (TEMPLATE_DECL, identifier, NULL_TREE);
2856 DECL_TEMPLATE_PARMS (tmpl) = current_template_parms;
2857 DECL_TEMPLATE_RESULT (tmpl) = decl;
2858 DECL_ARTIFICIAL (decl) = 1;
2860 // Associate the constraints with the underlying declaration,
2861 // not the template.
2862 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
2863 tree constr = build_constraints (reqs, NULL_TREE);
2864 set_constraints (decl, constr);
2866 end_template_decl ();
2868 gcc_assert (DECL_TEMPLATE_PARMS (tmpl));
2870 check_default_tmpl_args (decl, DECL_TEMPLATE_PARMS (tmpl),
2871 /*is_primary=*/true, /*is_partial=*/false,
2872 /*is_friend=*/0);
2874 return finish_template_type_parm (aggr, tmpl);
2877 /* ARGUMENT is the default-argument value for a template template
2878 parameter. If ARGUMENT is invalid, issue error messages and return
2879 the ERROR_MARK_NODE. Otherwise, ARGUMENT itself is returned. */
2881 tree
2882 check_template_template_default_arg (tree argument)
2884 if (TREE_CODE (argument) != TEMPLATE_DECL
2885 && TREE_CODE (argument) != TEMPLATE_TEMPLATE_PARM
2886 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
2888 if (TREE_CODE (argument) == TYPE_DECL)
2889 error ("invalid use of type %qT as a default value for a template "
2890 "template-parameter", TREE_TYPE (argument));
2891 else
2892 error ("invalid default argument for a template template parameter");
2893 return error_mark_node;
2896 return argument;
2899 /* Begin a class definition, as indicated by T. */
2901 tree
2902 begin_class_definition (tree t)
2904 if (error_operand_p (t) || error_operand_p (TYPE_MAIN_DECL (t)))
2905 return error_mark_node;
2907 if (processing_template_parmlist)
2909 error ("definition of %q#T inside template parameter list", t);
2910 return error_mark_node;
2913 /* According to the C++ ABI, decimal classes defined in ISO/IEC TR 24733
2914 are passed the same as decimal scalar types. */
2915 if (TREE_CODE (t) == RECORD_TYPE
2916 && !processing_template_decl)
2918 tree ns = TYPE_CONTEXT (t);
2919 if (ns && TREE_CODE (ns) == NAMESPACE_DECL
2920 && DECL_CONTEXT (ns) == std_node
2921 && DECL_NAME (ns)
2922 && id_equal (DECL_NAME (ns), "decimal"))
2924 const char *n = TYPE_NAME_STRING (t);
2925 if ((strcmp (n, "decimal32") == 0)
2926 || (strcmp (n, "decimal64") == 0)
2927 || (strcmp (n, "decimal128") == 0))
2928 TYPE_TRANSPARENT_AGGR (t) = 1;
2932 /* A non-implicit typename comes from code like:
2934 template <typename T> struct A {
2935 template <typename U> struct A<T>::B ...
2937 This is erroneous. */
2938 else if (TREE_CODE (t) == TYPENAME_TYPE)
2940 error ("invalid definition of qualified type %qT", t);
2941 t = error_mark_node;
2944 if (t == error_mark_node || ! MAYBE_CLASS_TYPE_P (t))
2946 t = make_class_type (RECORD_TYPE);
2947 pushtag (make_anon_name (), t, /*tag_scope=*/ts_current);
2950 if (TYPE_BEING_DEFINED (t))
2952 t = make_class_type (TREE_CODE (t));
2953 pushtag (TYPE_IDENTIFIER (t), t, /*tag_scope=*/ts_current);
2955 maybe_process_partial_specialization (t);
2956 pushclass (t);
2957 TYPE_BEING_DEFINED (t) = 1;
2958 class_binding_level->defining_class_p = 1;
2960 if (flag_pack_struct)
2962 tree v;
2963 TYPE_PACKED (t) = 1;
2964 /* Even though the type is being defined for the first time
2965 here, there might have been a forward declaration, so there
2966 might be cv-qualified variants of T. */
2967 for (v = TYPE_NEXT_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v))
2968 TYPE_PACKED (v) = 1;
2970 /* Reset the interface data, at the earliest possible
2971 moment, as it might have been set via a class foo;
2972 before. */
2973 if (! TYPE_UNNAMED_P (t))
2975 struct c_fileinfo *finfo = \
2976 get_fileinfo (LOCATION_FILE (input_location));
2977 CLASSTYPE_INTERFACE_ONLY (t) = finfo->interface_only;
2978 SET_CLASSTYPE_INTERFACE_UNKNOWN_X
2979 (t, finfo->interface_unknown);
2981 reset_specialization();
2983 /* Make a declaration for this class in its own scope. */
2984 build_self_reference ();
2986 return t;
2989 /* Finish the member declaration given by DECL. */
2991 void
2992 finish_member_declaration (tree decl)
2994 if (decl == error_mark_node || decl == NULL_TREE)
2995 return;
2997 if (decl == void_type_node)
2998 /* The COMPONENT was a friend, not a member, and so there's
2999 nothing for us to do. */
3000 return;
3002 /* We should see only one DECL at a time. */
3003 gcc_assert (DECL_CHAIN (decl) == NULL_TREE);
3005 /* Don't add decls after definition. */
3006 gcc_assert (TYPE_BEING_DEFINED (current_class_type)
3007 /* We can add lambda types when late parsing default
3008 arguments. */
3009 || LAMBDA_TYPE_P (TREE_TYPE (decl)));
3011 /* Set up access control for DECL. */
3012 TREE_PRIVATE (decl)
3013 = (current_access_specifier == access_private_node);
3014 TREE_PROTECTED (decl)
3015 = (current_access_specifier == access_protected_node);
3016 if (TREE_CODE (decl) == TEMPLATE_DECL)
3018 TREE_PRIVATE (DECL_TEMPLATE_RESULT (decl)) = TREE_PRIVATE (decl);
3019 TREE_PROTECTED (DECL_TEMPLATE_RESULT (decl)) = TREE_PROTECTED (decl);
3022 /* Mark the DECL as a member of the current class, unless it's
3023 a member of an enumeration. */
3024 if (TREE_CODE (decl) != CONST_DECL)
3025 DECL_CONTEXT (decl) = current_class_type;
3027 if (TREE_CODE (decl) == USING_DECL)
3028 /* For now, ignore class-scope USING_DECLS, so that debugging
3029 backends do not see them. */
3030 DECL_IGNORED_P (decl) = 1;
3032 /* Check for bare parameter packs in the non-static data member
3033 declaration. */
3034 if (TREE_CODE (decl) == FIELD_DECL)
3036 if (check_for_bare_parameter_packs (TREE_TYPE (decl)))
3037 TREE_TYPE (decl) = error_mark_node;
3038 if (check_for_bare_parameter_packs (DECL_ATTRIBUTES (decl)))
3039 DECL_ATTRIBUTES (decl) = NULL_TREE;
3042 /* [dcl.link]
3044 A C language linkage is ignored for the names of class members
3045 and the member function type of class member functions. */
3046 if (DECL_LANG_SPECIFIC (decl))
3047 SET_DECL_LANGUAGE (decl, lang_cplusplus);
3049 bool add = false;
3051 /* Functions and non-functions are added differently. */
3052 if (DECL_DECLARES_FUNCTION_P (decl))
3053 add = add_method (current_class_type, decl, false);
3054 /* Enter the DECL into the scope of the class, if the class
3055 isn't a closure (whose fields are supposed to be unnamed). */
3056 else if (CLASSTYPE_LAMBDA_EXPR (current_class_type)
3057 || pushdecl_class_level (decl))
3058 add = true;
3060 if (add)
3062 /* All TYPE_DECLs go at the end of TYPE_FIELDS. Ordinary fields
3063 go at the beginning. The reason is that
3064 legacy_nonfn_member_lookup searches the list in order, and we
3065 want a field name to override a type name so that the "struct
3066 stat hack" will work. In particular:
3068 struct S { enum E { }; static const int E = 5; int ary[S::E]; } s;
3070 is valid. */
3072 if (TREE_CODE (decl) == TYPE_DECL)
3073 TYPE_FIELDS (current_class_type)
3074 = chainon (TYPE_FIELDS (current_class_type), decl);
3075 else
3077 DECL_CHAIN (decl) = TYPE_FIELDS (current_class_type);
3078 TYPE_FIELDS (current_class_type) = decl;
3081 maybe_add_class_template_decl_list (current_class_type, decl,
3082 /*friend_p=*/0);
3086 /* Finish processing a complete template declaration. The PARMS are
3087 the template parameters. */
3089 void
3090 finish_template_decl (tree parms)
3092 if (parms)
3093 end_template_decl ();
3094 else
3095 end_specialization ();
3098 // Returns the template type of the class scope being entered. If we're
3099 // entering a constrained class scope. TYPE is the class template
3100 // scope being entered and we may need to match the intended type with
3101 // a constrained specialization. For example:
3103 // template<Object T>
3104 // struct S { void f(); }; #1
3106 // template<Object T>
3107 // void S<T>::f() { } #2
3109 // We check, in #2, that S<T> refers precisely to the type declared by
3110 // #1 (i.e., that the constraints match). Note that the following should
3111 // be an error since there is no specialization of S<T> that is
3112 // unconstrained, but this is not diagnosed here.
3114 // template<typename T>
3115 // void S<T>::f() { }
3117 // We cannot diagnose this problem here since this function also matches
3118 // qualified template names that are not part of a definition. For example:
3120 // template<Integral T, Floating_point U>
3121 // typename pair<T, U>::first_type void f(T, U);
3123 // Here, it is unlikely that there is a partial specialization of
3124 // pair constrained for for Integral and Floating_point arguments.
3126 // The general rule is: if a constrained specialization with matching
3127 // constraints is found return that type. Also note that if TYPE is not a
3128 // class-type (e.g. a typename type), then no fixup is needed.
3130 static tree
3131 fixup_template_type (tree type)
3133 // Find the template parameter list at the a depth appropriate to
3134 // the scope we're trying to enter.
3135 tree parms = current_template_parms;
3136 int depth = template_class_depth (type);
3137 for (int n = processing_template_decl; n > depth && parms; --n)
3138 parms = TREE_CHAIN (parms);
3139 if (!parms)
3140 return type;
3141 tree cur_reqs = TEMPLATE_PARMS_CONSTRAINTS (parms);
3142 tree cur_constr = build_constraints (cur_reqs, NULL_TREE);
3144 // Search for a specialization whose type and constraints match.
3145 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
3146 tree specs = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
3147 while (specs)
3149 tree spec_constr = get_constraints (TREE_VALUE (specs));
3151 // If the type and constraints match a specialization, then we
3152 // are entering that type.
3153 if (same_type_p (type, TREE_TYPE (specs))
3154 && equivalent_constraints (cur_constr, spec_constr))
3155 return TREE_TYPE (specs);
3156 specs = TREE_CHAIN (specs);
3159 // If no specialization matches, then must return the type
3160 // previously found.
3161 return type;
3164 /* Finish processing a template-id (which names a type) of the form
3165 NAME < ARGS >. Return the TYPE_DECL for the type named by the
3166 template-id. If ENTERING_SCOPE is nonzero we are about to enter
3167 the scope of template-id indicated. */
3169 tree
3170 finish_template_type (tree name, tree args, int entering_scope)
3172 tree type;
3174 type = lookup_template_class (name, args,
3175 NULL_TREE, NULL_TREE, entering_scope,
3176 tf_warning_or_error | tf_user);
3178 /* If we might be entering the scope of a partial specialization,
3179 find the one with the right constraints. */
3180 if (flag_concepts
3181 && entering_scope
3182 && CLASS_TYPE_P (type)
3183 && CLASSTYPE_TEMPLATE_INFO (type)
3184 && dependent_type_p (type)
3185 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
3186 type = fixup_template_type (type);
3188 if (type == error_mark_node)
3189 return type;
3190 else if (CLASS_TYPE_P (type) && !alias_type_or_template_p (type))
3191 return TYPE_STUB_DECL (type);
3192 else
3193 return TYPE_NAME (type);
3196 /* Finish processing a BASE_CLASS with the indicated ACCESS_SPECIFIER.
3197 Return a TREE_LIST containing the ACCESS_SPECIFIER and the
3198 BASE_CLASS, or NULL_TREE if an error occurred. The
3199 ACCESS_SPECIFIER is one of
3200 access_{default,public,protected_private}_node. For a virtual base
3201 we set TREE_TYPE. */
3203 tree
3204 finish_base_specifier (tree base, tree access, bool virtual_p)
3206 tree result;
3208 if (base == error_mark_node)
3210 error ("invalid base-class specification");
3211 result = NULL_TREE;
3213 else if (! MAYBE_CLASS_TYPE_P (base))
3215 error ("%qT is not a class type", base);
3216 result = NULL_TREE;
3218 else
3220 if (cp_type_quals (base) != 0)
3222 /* DR 484: Can a base-specifier name a cv-qualified
3223 class type? */
3224 base = TYPE_MAIN_VARIANT (base);
3226 result = build_tree_list (access, base);
3227 if (virtual_p)
3228 TREE_TYPE (result) = integer_type_node;
3231 return result;
3234 /* If FNS is a member function, a set of member functions, or a
3235 template-id referring to one or more member functions, return a
3236 BASELINK for FNS, incorporating the current access context.
3237 Otherwise, return FNS unchanged. */
3239 tree
3240 baselink_for_fns (tree fns)
3242 tree scope;
3243 tree cl;
3245 if (BASELINK_P (fns)
3246 || error_operand_p (fns))
3247 return fns;
3249 scope = ovl_scope (fns);
3250 if (!CLASS_TYPE_P (scope))
3251 return fns;
3253 cl = currently_open_derived_class (scope);
3254 if (!cl)
3255 cl = scope;
3256 cl = TYPE_BINFO (cl);
3257 return build_baselink (cl, cl, fns, /*optype=*/NULL_TREE);
3260 /* Returns true iff DECL is a variable from a function outside
3261 the current one. */
3263 static bool
3264 outer_var_p (tree decl)
3266 return ((VAR_P (decl) || TREE_CODE (decl) == PARM_DECL)
3267 && DECL_FUNCTION_SCOPE_P (decl)
3268 && (DECL_CONTEXT (decl) != current_function_decl
3269 || parsing_nsdmi ()));
3272 /* As above, but also checks that DECL is automatic. */
3274 bool
3275 outer_automatic_var_p (tree decl)
3277 return (outer_var_p (decl)
3278 && !TREE_STATIC (decl));
3281 /* DECL satisfies outer_automatic_var_p. Possibly complain about it or
3282 rewrite it for lambda capture. */
3284 tree
3285 process_outer_var_ref (tree decl, tsubst_flags_t complain)
3287 if (cp_unevaluated_operand)
3288 /* It's not a use (3.2) if we're in an unevaluated context. */
3289 return decl;
3290 if (decl == error_mark_node)
3291 return decl;
3293 tree context = DECL_CONTEXT (decl);
3294 tree containing_function = current_function_decl;
3295 tree lambda_stack = NULL_TREE;
3296 tree lambda_expr = NULL_TREE;
3297 tree initializer = convert_from_reference (decl);
3299 /* Mark it as used now even if the use is ill-formed. */
3300 if (!mark_used (decl, complain))
3301 return error_mark_node;
3303 if (parsing_nsdmi ())
3304 containing_function = NULL_TREE;
3306 if (containing_function && DECL_TEMPLATE_INFO (context)
3307 && LAMBDA_FUNCTION_P (containing_function))
3309 /* Check whether we've already built a proxy;
3310 insert_pending_capture_proxies doesn't update
3311 local_specializations. */
3312 tree d = lookup_name (DECL_NAME (decl));
3313 if (d && is_capture_proxy (d)
3314 && DECL_CONTEXT (d) == containing_function)
3315 return d;
3318 /* If we are in a lambda function, we can move out until we hit
3319 1. the context,
3320 2. a non-lambda function, or
3321 3. a non-default capturing lambda function. */
3322 while (context != containing_function
3323 /* containing_function can be null with invalid generic lambdas. */
3324 && containing_function
3325 && LAMBDA_FUNCTION_P (containing_function))
3327 tree closure = DECL_CONTEXT (containing_function);
3328 lambda_expr = CLASSTYPE_LAMBDA_EXPR (closure);
3330 if (TYPE_CLASS_SCOPE_P (closure))
3331 /* A lambda in an NSDMI (c++/64496). */
3332 break;
3334 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr)
3335 == CPLD_NONE)
3336 break;
3338 lambda_stack = tree_cons (NULL_TREE,
3339 lambda_expr,
3340 lambda_stack);
3342 containing_function
3343 = decl_function_context (containing_function);
3346 /* In a lambda within a template, wait until instantiation
3347 time to implicitly capture. */
3348 if (context == containing_function
3349 && DECL_TEMPLATE_INFO (containing_function)
3350 && any_dependent_template_arguments_p (DECL_TI_ARGS
3351 (containing_function)))
3352 return decl;
3354 /* Core issue 696: "[At the July 2009 meeting] the CWG expressed
3355 support for an approach in which a reference to a local
3356 [constant] automatic variable in a nested class or lambda body
3357 would enter the expression as an rvalue, which would reduce
3358 the complexity of the problem"
3360 FIXME update for final resolution of core issue 696. */
3361 if (decl_constant_var_p (decl))
3363 tree t = maybe_constant_value (convert_from_reference (decl));
3364 if (TREE_CONSTANT (t))
3365 return t;
3368 if (lambda_expr && VAR_P (decl)
3369 && DECL_ANON_UNION_VAR_P (decl))
3371 if (complain & tf_error)
3372 error ("cannot capture member %qD of anonymous union", decl);
3373 return error_mark_node;
3375 if (context == containing_function)
3377 decl = add_default_capture (lambda_stack,
3378 /*id=*/DECL_NAME (decl),
3379 initializer);
3381 else if (lambda_expr)
3383 if (complain & tf_error)
3385 error ("%qD is not captured", decl);
3386 tree closure = LAMBDA_EXPR_CLOSURE (lambda_expr);
3387 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr)
3388 == CPLD_NONE)
3389 inform (location_of (closure),
3390 "the lambda has no capture-default");
3391 else if (TYPE_CLASS_SCOPE_P (closure))
3392 inform (0, "lambda in local class %q+T cannot "
3393 "capture variables from the enclosing context",
3394 TYPE_CONTEXT (closure));
3395 inform (DECL_SOURCE_LOCATION (decl), "%q#D declared here", decl);
3397 return error_mark_node;
3399 else
3401 if (complain & tf_error)
3403 error (VAR_P (decl)
3404 ? G_("use of local variable with automatic storage from "
3405 "containing function")
3406 : G_("use of parameter from containing function"));
3407 inform (DECL_SOURCE_LOCATION (decl), "%q#D declared here", decl);
3409 return error_mark_node;
3411 return decl;
3414 /* ID_EXPRESSION is a representation of parsed, but unprocessed,
3415 id-expression. (See cp_parser_id_expression for details.) SCOPE,
3416 if non-NULL, is the type or namespace used to explicitly qualify
3417 ID_EXPRESSION. DECL is the entity to which that name has been
3418 resolved.
3420 *CONSTANT_EXPRESSION_P is true if we are presently parsing a
3421 constant-expression. In that case, *NON_CONSTANT_EXPRESSION_P will
3422 be set to true if this expression isn't permitted in a
3423 constant-expression, but it is otherwise not set by this function.
3424 *ALLOW_NON_CONSTANT_EXPRESSION_P is true if we are parsing a
3425 constant-expression, but a non-constant expression is also
3426 permissible.
3428 DONE is true if this expression is a complete postfix-expression;
3429 it is false if this expression is followed by '->', '[', '(', etc.
3430 ADDRESS_P is true iff this expression is the operand of '&'.
3431 TEMPLATE_P is true iff the qualified-id was of the form
3432 "A::template B". TEMPLATE_ARG_P is true iff this qualified name
3433 appears as a template argument.
3435 If an error occurs, and it is the kind of error that might cause
3436 the parser to abort a tentative parse, *ERROR_MSG is filled in. It
3437 is the caller's responsibility to issue the message. *ERROR_MSG
3438 will be a string with static storage duration, so the caller need
3439 not "free" it.
3441 Return an expression for the entity, after issuing appropriate
3442 diagnostics. This function is also responsible for transforming a
3443 reference to a non-static member into a COMPONENT_REF that makes
3444 the use of "this" explicit.
3446 Upon return, *IDK will be filled in appropriately. */
3447 cp_expr
3448 finish_id_expression (tree id_expression,
3449 tree decl,
3450 tree scope,
3451 cp_id_kind *idk,
3452 bool integral_constant_expression_p,
3453 bool allow_non_integral_constant_expression_p,
3454 bool *non_integral_constant_expression_p,
3455 bool template_p,
3456 bool done,
3457 bool address_p,
3458 bool template_arg_p,
3459 const char **error_msg,
3460 location_t location)
3462 decl = strip_using_decl (decl);
3464 /* Initialize the output parameters. */
3465 *idk = CP_ID_KIND_NONE;
3466 *error_msg = NULL;
3468 if (id_expression == error_mark_node)
3469 return error_mark_node;
3470 /* If we have a template-id, then no further lookup is
3471 required. If the template-id was for a template-class, we
3472 will sometimes have a TYPE_DECL at this point. */
3473 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3474 || TREE_CODE (decl) == TYPE_DECL)
3476 /* Look up the name. */
3477 else
3479 if (decl == error_mark_node)
3481 /* Name lookup failed. */
3482 if (scope
3483 && (!TYPE_P (scope)
3484 || (!dependent_type_p (scope)
3485 && !(identifier_p (id_expression)
3486 && IDENTIFIER_CONV_OP_P (id_expression)
3487 && dependent_type_p (TREE_TYPE (id_expression))))))
3489 /* If the qualifying type is non-dependent (and the name
3490 does not name a conversion operator to a dependent
3491 type), issue an error. */
3492 qualified_name_lookup_error (scope, id_expression, decl, location);
3493 return error_mark_node;
3495 else if (!scope)
3497 /* It may be resolved via Koenig lookup. */
3498 *idk = CP_ID_KIND_UNQUALIFIED;
3499 return id_expression;
3501 else
3502 decl = id_expression;
3504 /* If DECL is a variable that would be out of scope under
3505 ANSI/ISO rules, but in scope in the ARM, name lookup
3506 will succeed. Issue a diagnostic here. */
3507 else
3508 decl = check_for_out_of_scope_variable (decl);
3510 /* Remember that the name was used in the definition of
3511 the current class so that we can check later to see if
3512 the meaning would have been different after the class
3513 was entirely defined. */
3514 if (!scope && decl != error_mark_node && identifier_p (id_expression))
3515 maybe_note_name_used_in_class (id_expression, decl);
3517 /* A use in unevaluated operand might not be instantiated appropriately
3518 if tsubst_copy builds a dummy parm, or if we never instantiate a
3519 generic lambda, so mark it now. */
3520 if (processing_template_decl && cp_unevaluated_operand)
3521 mark_type_use (decl);
3523 /* Disallow uses of local variables from containing functions, except
3524 within lambda-expressions. */
3525 if (outer_automatic_var_p (decl))
3527 decl = process_outer_var_ref (decl, tf_warning_or_error);
3528 if (decl == error_mark_node)
3529 return error_mark_node;
3532 /* Also disallow uses of function parameters outside the function
3533 body, except inside an unevaluated context (i.e. decltype). */
3534 if (TREE_CODE (decl) == PARM_DECL
3535 && DECL_CONTEXT (decl) == NULL_TREE
3536 && !cp_unevaluated_operand)
3538 *error_msg = G_("use of parameter outside function body");
3539 return error_mark_node;
3543 /* If we didn't find anything, or what we found was a type,
3544 then this wasn't really an id-expression. */
3545 if (TREE_CODE (decl) == TEMPLATE_DECL
3546 && !DECL_FUNCTION_TEMPLATE_P (decl))
3548 *error_msg = G_("missing template arguments");
3549 return error_mark_node;
3551 else if (TREE_CODE (decl) == TYPE_DECL
3552 || TREE_CODE (decl) == NAMESPACE_DECL)
3554 *error_msg = G_("expected primary-expression");
3555 return error_mark_node;
3558 /* If the name resolved to a template parameter, there is no
3559 need to look it up again later. */
3560 if ((TREE_CODE (decl) == CONST_DECL && DECL_TEMPLATE_PARM_P (decl))
3561 || TREE_CODE (decl) == TEMPLATE_PARM_INDEX)
3563 tree r;
3565 *idk = CP_ID_KIND_NONE;
3566 if (TREE_CODE (decl) == TEMPLATE_PARM_INDEX)
3567 decl = TEMPLATE_PARM_DECL (decl);
3568 r = convert_from_reference (DECL_INITIAL (decl));
3570 if (integral_constant_expression_p
3571 && !dependent_type_p (TREE_TYPE (decl))
3572 && !(INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (r))))
3574 if (!allow_non_integral_constant_expression_p)
3575 error ("template parameter %qD of type %qT is not allowed in "
3576 "an integral constant expression because it is not of "
3577 "integral or enumeration type", decl, TREE_TYPE (decl));
3578 *non_integral_constant_expression_p = true;
3580 return r;
3582 else
3584 bool dependent_p = type_dependent_expression_p (decl);
3586 /* If the declaration was explicitly qualified indicate
3587 that. The semantics of `A::f(3)' are different than
3588 `f(3)' if `f' is virtual. */
3589 *idk = (scope
3590 ? CP_ID_KIND_QUALIFIED
3591 : (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3592 ? CP_ID_KIND_TEMPLATE_ID
3593 : (dependent_p
3594 ? CP_ID_KIND_UNQUALIFIED_DEPENDENT
3595 : CP_ID_KIND_UNQUALIFIED)));
3597 if (dependent_p
3598 && DECL_P (decl)
3599 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (decl)))
3600 /* Dependent type attributes on the decl mean that the TREE_TYPE is
3601 wrong, so just return the identifier. */
3602 return id_expression;
3604 if (TREE_CODE (decl) == NAMESPACE_DECL)
3606 error ("use of namespace %qD as expression", decl);
3607 return error_mark_node;
3609 else if (DECL_CLASS_TEMPLATE_P (decl))
3611 error ("use of class template %qT as expression", decl);
3612 return error_mark_node;
3614 else if (TREE_CODE (decl) == TREE_LIST)
3616 /* Ambiguous reference to base members. */
3617 error ("request for member %qD is ambiguous in "
3618 "multiple inheritance lattice", id_expression);
3619 print_candidates (decl);
3620 return error_mark_node;
3623 /* Mark variable-like entities as used. Functions are similarly
3624 marked either below or after overload resolution. */
3625 if ((VAR_P (decl)
3626 || TREE_CODE (decl) == PARM_DECL
3627 || TREE_CODE (decl) == CONST_DECL
3628 || TREE_CODE (decl) == RESULT_DECL)
3629 && !mark_used (decl))
3630 return error_mark_node;
3632 /* Only certain kinds of names are allowed in constant
3633 expression. Template parameters have already
3634 been handled above. */
3635 if (! error_operand_p (decl)
3636 && !dependent_p
3637 && integral_constant_expression_p
3638 && ! decl_constant_var_p (decl)
3639 && TREE_CODE (decl) != CONST_DECL
3640 && ! builtin_valid_in_constant_expr_p (decl))
3642 if (!allow_non_integral_constant_expression_p)
3644 error ("%qD cannot appear in a constant-expression", decl);
3645 return error_mark_node;
3647 *non_integral_constant_expression_p = true;
3650 tree wrap;
3651 if (VAR_P (decl)
3652 && !cp_unevaluated_operand
3653 && !processing_template_decl
3654 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl))
3655 && CP_DECL_THREAD_LOCAL_P (decl)
3656 && (wrap = get_tls_wrapper_fn (decl)))
3658 /* Replace an evaluated use of the thread_local variable with
3659 a call to its wrapper. */
3660 decl = build_cxx_call (wrap, 0, NULL, tf_warning_or_error);
3662 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3663 && !dependent_p
3664 && variable_template_p (TREE_OPERAND (decl, 0)))
3666 decl = finish_template_variable (decl);
3667 mark_used (decl);
3668 decl = convert_from_reference (decl);
3670 else if (scope)
3672 if (TREE_CODE (decl) == SCOPE_REF)
3674 gcc_assert (same_type_p (scope, TREE_OPERAND (decl, 0)));
3675 decl = TREE_OPERAND (decl, 1);
3678 decl = (adjust_result_of_qualified_name_lookup
3679 (decl, scope, current_nonlambda_class_type()));
3681 if (TREE_CODE (decl) == FUNCTION_DECL)
3682 mark_used (decl);
3684 if (TYPE_P (scope))
3685 decl = finish_qualified_id_expr (scope,
3686 decl,
3687 done,
3688 address_p,
3689 template_p,
3690 template_arg_p,
3691 tf_warning_or_error);
3692 else
3693 decl = convert_from_reference (decl);
3695 else if (TREE_CODE (decl) == FIELD_DECL)
3697 /* Since SCOPE is NULL here, this is an unqualified name.
3698 Access checking has been performed during name lookup
3699 already. Turn off checking to avoid duplicate errors. */
3700 push_deferring_access_checks (dk_no_check);
3701 decl = finish_non_static_data_member (decl, NULL_TREE,
3702 /*qualifying_scope=*/NULL_TREE);
3703 pop_deferring_access_checks ();
3705 else if (is_overloaded_fn (decl))
3707 tree first_fn = get_first_fn (decl);
3709 if (TREE_CODE (first_fn) == TEMPLATE_DECL)
3710 first_fn = DECL_TEMPLATE_RESULT (first_fn);
3712 /* [basic.def.odr]: "A function whose name appears as a
3713 potentially-evaluated expression is odr-used if it is the unique
3714 lookup result".
3716 But only mark it if it's a complete postfix-expression; in a call,
3717 ADL might select a different function, and we'll call mark_used in
3718 build_over_call. */
3719 if (done
3720 && !really_overloaded_fn (decl)
3721 && !mark_used (first_fn))
3722 return error_mark_node;
3724 if (!template_arg_p
3725 && TREE_CODE (first_fn) == FUNCTION_DECL
3726 && DECL_FUNCTION_MEMBER_P (first_fn)
3727 && !shared_member_p (decl))
3729 /* A set of member functions. */
3730 decl = maybe_dummy_object (DECL_CONTEXT (first_fn), 0);
3731 return finish_class_member_access_expr (decl, id_expression,
3732 /*template_p=*/false,
3733 tf_warning_or_error);
3736 decl = baselink_for_fns (decl);
3738 else
3740 if (DECL_P (decl) && DECL_NONLOCAL (decl)
3741 && DECL_CLASS_SCOPE_P (decl))
3743 tree context = context_for_name_lookup (decl);
3744 if (context != current_class_type)
3746 tree path = currently_open_derived_class (context);
3747 perform_or_defer_access_check (TYPE_BINFO (path),
3748 decl, decl,
3749 tf_warning_or_error);
3753 decl = convert_from_reference (decl);
3757 return cp_expr (decl, location);
3760 /* Implement the __typeof keyword: Return the type of EXPR, suitable for
3761 use as a type-specifier. */
3763 tree
3764 finish_typeof (tree expr)
3766 tree type;
3768 if (type_dependent_expression_p (expr))
3770 type = cxx_make_type (TYPEOF_TYPE);
3771 TYPEOF_TYPE_EXPR (type) = expr;
3772 SET_TYPE_STRUCTURAL_EQUALITY (type);
3774 return type;
3777 expr = mark_type_use (expr);
3779 type = unlowered_expr_type (expr);
3781 if (!type || type == unknown_type_node)
3783 error ("type of %qE is unknown", expr);
3784 return error_mark_node;
3787 return type;
3790 /* Implement the __underlying_type keyword: Return the underlying
3791 type of TYPE, suitable for use as a type-specifier. */
3793 tree
3794 finish_underlying_type (tree type)
3796 tree underlying_type;
3798 if (processing_template_decl)
3800 underlying_type = cxx_make_type (UNDERLYING_TYPE);
3801 UNDERLYING_TYPE_TYPE (underlying_type) = type;
3802 SET_TYPE_STRUCTURAL_EQUALITY (underlying_type);
3804 return underlying_type;
3807 if (!complete_type_or_else (type, NULL_TREE))
3808 return error_mark_node;
3810 if (TREE_CODE (type) != ENUMERAL_TYPE)
3812 error ("%qT is not an enumeration type", type);
3813 return error_mark_node;
3816 underlying_type = ENUM_UNDERLYING_TYPE (type);
3818 /* Fixup necessary in this case because ENUM_UNDERLYING_TYPE
3819 includes TYPE_MIN_VALUE and TYPE_MAX_VALUE information.
3820 See finish_enum_value_list for details. */
3821 if (!ENUM_FIXED_UNDERLYING_TYPE_P (type))
3822 underlying_type
3823 = c_common_type_for_mode (TYPE_MODE (underlying_type),
3824 TYPE_UNSIGNED (underlying_type));
3826 return underlying_type;
3829 /* Implement the __direct_bases keyword: Return the direct base classes
3830 of type */
3832 tree
3833 calculate_direct_bases (tree type)
3835 vec<tree, va_gc> *vector = make_tree_vector();
3836 tree bases_vec = NULL_TREE;
3837 vec<tree, va_gc> *base_binfos;
3838 tree binfo;
3839 unsigned i;
3841 complete_type (type);
3843 if (!NON_UNION_CLASS_TYPE_P (type))
3844 return make_tree_vec (0);
3846 base_binfos = BINFO_BASE_BINFOS (TYPE_BINFO (type));
3848 /* Virtual bases are initialized first */
3849 for (i = 0; base_binfos->iterate (i, &binfo); i++)
3851 if (BINFO_VIRTUAL_P (binfo))
3853 vec_safe_push (vector, binfo);
3857 /* Now non-virtuals */
3858 for (i = 0; base_binfos->iterate (i, &binfo); i++)
3860 if (!BINFO_VIRTUAL_P (binfo))
3862 vec_safe_push (vector, binfo);
3867 bases_vec = make_tree_vec (vector->length ());
3869 for (i = 0; i < vector->length (); ++i)
3871 TREE_VEC_ELT (bases_vec, i) = BINFO_TYPE ((*vector)[i]);
3873 return bases_vec;
3876 /* Implement the __bases keyword: Return the base classes
3877 of type */
3879 /* Find morally non-virtual base classes by walking binfo hierarchy */
3880 /* Virtual base classes are handled separately in finish_bases */
3882 static tree
3883 dfs_calculate_bases_pre (tree binfo, void * /*data_*/)
3885 /* Don't walk bases of virtual bases */
3886 return BINFO_VIRTUAL_P (binfo) ? dfs_skip_bases : NULL_TREE;
3889 static tree
3890 dfs_calculate_bases_post (tree binfo, void *data_)
3892 vec<tree, va_gc> **data = ((vec<tree, va_gc> **) data_);
3893 if (!BINFO_VIRTUAL_P (binfo))
3895 vec_safe_push (*data, BINFO_TYPE (binfo));
3897 return NULL_TREE;
3900 /* Calculates the morally non-virtual base classes of a class */
3901 static vec<tree, va_gc> *
3902 calculate_bases_helper (tree type)
3904 vec<tree, va_gc> *vector = make_tree_vector();
3906 /* Now add non-virtual base classes in order of construction */
3907 if (TYPE_BINFO (type))
3908 dfs_walk_all (TYPE_BINFO (type),
3909 dfs_calculate_bases_pre, dfs_calculate_bases_post, &vector);
3910 return vector;
3913 tree
3914 calculate_bases (tree type)
3916 vec<tree, va_gc> *vector = make_tree_vector();
3917 tree bases_vec = NULL_TREE;
3918 unsigned i;
3919 vec<tree, va_gc> *vbases;
3920 vec<tree, va_gc> *nonvbases;
3921 tree binfo;
3923 complete_type (type);
3925 if (!NON_UNION_CLASS_TYPE_P (type))
3926 return make_tree_vec (0);
3928 /* First go through virtual base classes */
3929 for (vbases = CLASSTYPE_VBASECLASSES (type), i = 0;
3930 vec_safe_iterate (vbases, i, &binfo); i++)
3932 vec<tree, va_gc> *vbase_bases;
3933 vbase_bases = calculate_bases_helper (BINFO_TYPE (binfo));
3934 vec_safe_splice (vector, vbase_bases);
3935 release_tree_vector (vbase_bases);
3938 /* Now for the non-virtual bases */
3939 nonvbases = calculate_bases_helper (type);
3940 vec_safe_splice (vector, nonvbases);
3941 release_tree_vector (nonvbases);
3943 /* Note that during error recovery vector->length can even be zero. */
3944 if (vector->length () > 1)
3946 /* Last element is entire class, so don't copy */
3947 bases_vec = make_tree_vec (vector->length() - 1);
3949 for (i = 0; i < vector->length () - 1; ++i)
3950 TREE_VEC_ELT (bases_vec, i) = (*vector)[i];
3952 else
3953 bases_vec = make_tree_vec (0);
3955 release_tree_vector (vector);
3956 return bases_vec;
3959 tree
3960 finish_bases (tree type, bool direct)
3962 tree bases = NULL_TREE;
3964 if (!processing_template_decl)
3966 /* Parameter packs can only be used in templates */
3967 error ("Parameter pack __bases only valid in template declaration");
3968 return error_mark_node;
3971 bases = cxx_make_type (BASES);
3972 BASES_TYPE (bases) = type;
3973 BASES_DIRECT (bases) = direct;
3974 SET_TYPE_STRUCTURAL_EQUALITY (bases);
3976 return bases;
3979 /* Perform C++-specific checks for __builtin_offsetof before calling
3980 fold_offsetof. */
3982 tree
3983 finish_offsetof (tree object_ptr, tree expr, location_t loc)
3985 /* If we're processing a template, we can't finish the semantics yet.
3986 Otherwise we can fold the entire expression now. */
3987 if (processing_template_decl)
3989 expr = build2 (OFFSETOF_EXPR, size_type_node, expr, object_ptr);
3990 SET_EXPR_LOCATION (expr, loc);
3991 return expr;
3994 if (TREE_CODE (expr) == PSEUDO_DTOR_EXPR)
3996 error ("cannot apply %<offsetof%> to destructor %<~%T%>",
3997 TREE_OPERAND (expr, 2));
3998 return error_mark_node;
4000 if (TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE
4001 || TREE_CODE (TREE_TYPE (expr)) == METHOD_TYPE
4002 || TREE_TYPE (expr) == unknown_type_node)
4004 if (INDIRECT_REF_P (expr))
4005 error ("second operand of %<offsetof%> is neither a single "
4006 "identifier nor a sequence of member accesses and "
4007 "array references");
4008 else
4010 if (TREE_CODE (expr) == COMPONENT_REF
4011 || TREE_CODE (expr) == COMPOUND_EXPR)
4012 expr = TREE_OPERAND (expr, 1);
4013 error ("cannot apply %<offsetof%> to member function %qD", expr);
4015 return error_mark_node;
4017 if (REFERENCE_REF_P (expr))
4018 expr = TREE_OPERAND (expr, 0);
4019 if (!complete_type_or_else (TREE_TYPE (TREE_TYPE (object_ptr)), object_ptr))
4020 return error_mark_node;
4021 if (warn_invalid_offsetof
4022 && CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (object_ptr)))
4023 && CLASSTYPE_NON_STD_LAYOUT (TREE_TYPE (TREE_TYPE (object_ptr)))
4024 && cp_unevaluated_operand == 0)
4025 pedwarn (loc, OPT_Winvalid_offsetof,
4026 "offsetof within non-standard-layout type %qT is undefined",
4027 TREE_TYPE (TREE_TYPE (object_ptr)));
4028 return fold_offsetof (expr);
4031 /* Replace the AGGR_INIT_EXPR at *TP with an equivalent CALL_EXPR. This
4032 function is broken out from the above for the benefit of the tree-ssa
4033 project. */
4035 void
4036 simplify_aggr_init_expr (tree *tp)
4038 tree aggr_init_expr = *tp;
4040 /* Form an appropriate CALL_EXPR. */
4041 tree fn = AGGR_INIT_EXPR_FN (aggr_init_expr);
4042 tree slot = AGGR_INIT_EXPR_SLOT (aggr_init_expr);
4043 tree type = TREE_TYPE (slot);
4045 tree call_expr;
4046 enum style_t { ctor, arg, pcc } style;
4048 if (AGGR_INIT_VIA_CTOR_P (aggr_init_expr))
4049 style = ctor;
4050 #ifdef PCC_STATIC_STRUCT_RETURN
4051 else if (1)
4052 style = pcc;
4053 #endif
4054 else
4056 gcc_assert (TREE_ADDRESSABLE (type));
4057 style = arg;
4060 call_expr = build_call_array_loc (input_location,
4061 TREE_TYPE (TREE_TYPE (TREE_TYPE (fn))),
4063 aggr_init_expr_nargs (aggr_init_expr),
4064 AGGR_INIT_EXPR_ARGP (aggr_init_expr));
4065 TREE_NOTHROW (call_expr) = TREE_NOTHROW (aggr_init_expr);
4066 CALL_FROM_THUNK_P (call_expr) = AGGR_INIT_FROM_THUNK_P (aggr_init_expr);
4067 CALL_EXPR_OPERATOR_SYNTAX (call_expr)
4068 = CALL_EXPR_OPERATOR_SYNTAX (aggr_init_expr);
4069 CALL_EXPR_ORDERED_ARGS (call_expr) = CALL_EXPR_ORDERED_ARGS (aggr_init_expr);
4070 CALL_EXPR_REVERSE_ARGS (call_expr) = CALL_EXPR_REVERSE_ARGS (aggr_init_expr);
4071 /* Preserve CILK_SPAWN flag. */
4072 EXPR_CILK_SPAWN (call_expr) = EXPR_CILK_SPAWN (aggr_init_expr);
4074 if (style == ctor)
4076 /* Replace the first argument to the ctor with the address of the
4077 slot. */
4078 cxx_mark_addressable (slot);
4079 CALL_EXPR_ARG (call_expr, 0) =
4080 build1 (ADDR_EXPR, build_pointer_type (type), slot);
4082 else if (style == arg)
4084 /* Just mark it addressable here, and leave the rest to
4085 expand_call{,_inline}. */
4086 cxx_mark_addressable (slot);
4087 CALL_EXPR_RETURN_SLOT_OPT (call_expr) = true;
4088 call_expr = build2 (INIT_EXPR, TREE_TYPE (call_expr), slot, call_expr);
4090 else if (style == pcc)
4092 /* If we're using the non-reentrant PCC calling convention, then we
4093 need to copy the returned value out of the static buffer into the
4094 SLOT. */
4095 push_deferring_access_checks (dk_no_check);
4096 call_expr = build_aggr_init (slot, call_expr,
4097 DIRECT_BIND | LOOKUP_ONLYCONVERTING,
4098 tf_warning_or_error);
4099 pop_deferring_access_checks ();
4100 call_expr = build2 (COMPOUND_EXPR, TREE_TYPE (slot), call_expr, slot);
4103 if (AGGR_INIT_ZERO_FIRST (aggr_init_expr))
4105 tree init = build_zero_init (type, NULL_TREE,
4106 /*static_storage_p=*/false);
4107 init = build2 (INIT_EXPR, void_type_node, slot, init);
4108 call_expr = build2 (COMPOUND_EXPR, TREE_TYPE (call_expr),
4109 init, call_expr);
4112 *tp = call_expr;
4115 /* Emit all thunks to FN that should be emitted when FN is emitted. */
4117 void
4118 emit_associated_thunks (tree fn)
4120 /* When we use vcall offsets, we emit thunks with the virtual
4121 functions to which they thunk. The whole point of vcall offsets
4122 is so that you can know statically the entire set of thunks that
4123 will ever be needed for a given virtual function, thereby
4124 enabling you to output all the thunks with the function itself. */
4125 if (DECL_VIRTUAL_P (fn)
4126 /* Do not emit thunks for extern template instantiations. */
4127 && ! DECL_REALLY_EXTERN (fn))
4129 tree thunk;
4131 for (thunk = DECL_THUNKS (fn); thunk; thunk = DECL_CHAIN (thunk))
4133 if (!THUNK_ALIAS (thunk))
4135 use_thunk (thunk, /*emit_p=*/1);
4136 if (DECL_RESULT_THUNK_P (thunk))
4138 tree probe;
4140 for (probe = DECL_THUNKS (thunk);
4141 probe; probe = DECL_CHAIN (probe))
4142 use_thunk (probe, /*emit_p=*/1);
4145 else
4146 gcc_assert (!DECL_THUNKS (thunk));
4151 /* Generate RTL for FN. */
4153 bool
4154 expand_or_defer_fn_1 (tree fn)
4156 /* When the parser calls us after finishing the body of a template
4157 function, we don't really want to expand the body. */
4158 if (processing_template_decl)
4160 /* Normally, collection only occurs in rest_of_compilation. So,
4161 if we don't collect here, we never collect junk generated
4162 during the processing of templates until we hit a
4163 non-template function. It's not safe to do this inside a
4164 nested class, though, as the parser may have local state that
4165 is not a GC root. */
4166 if (!function_depth)
4167 ggc_collect ();
4168 return false;
4171 gcc_assert (DECL_SAVED_TREE (fn));
4173 /* We make a decision about linkage for these functions at the end
4174 of the compilation. Until that point, we do not want the back
4175 end to output them -- but we do want it to see the bodies of
4176 these functions so that it can inline them as appropriate. */
4177 if (DECL_DECLARED_INLINE_P (fn) || DECL_IMPLICIT_INSTANTIATION (fn))
4179 if (DECL_INTERFACE_KNOWN (fn))
4180 /* We've already made a decision as to how this function will
4181 be handled. */;
4182 else if (!at_eof)
4183 tentative_decl_linkage (fn);
4184 else
4185 import_export_decl (fn);
4187 /* If the user wants us to keep all inline functions, then mark
4188 this function as needed so that finish_file will make sure to
4189 output it later. Similarly, all dllexport'd functions must
4190 be emitted; there may be callers in other DLLs. */
4191 if (DECL_DECLARED_INLINE_P (fn)
4192 && !DECL_REALLY_EXTERN (fn)
4193 && (flag_keep_inline_functions
4194 || (flag_keep_inline_dllexport
4195 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (fn)))))
4197 mark_needed (fn);
4198 DECL_EXTERNAL (fn) = 0;
4202 /* If this is a constructor or destructor body, we have to clone
4203 it. */
4204 if (maybe_clone_body (fn))
4206 /* We don't want to process FN again, so pretend we've written
4207 it out, even though we haven't. */
4208 TREE_ASM_WRITTEN (fn) = 1;
4209 /* If this is a constexpr function, keep DECL_SAVED_TREE. */
4210 if (!DECL_DECLARED_CONSTEXPR_P (fn))
4211 DECL_SAVED_TREE (fn) = NULL_TREE;
4212 return false;
4215 /* There's no reason to do any of the work here if we're only doing
4216 semantic analysis; this code just generates RTL. */
4217 if (flag_syntax_only)
4218 return false;
4220 return true;
4223 void
4224 expand_or_defer_fn (tree fn)
4226 if (expand_or_defer_fn_1 (fn))
4228 function_depth++;
4230 /* Expand or defer, at the whim of the compilation unit manager. */
4231 cgraph_node::finalize_function (fn, function_depth > 1);
4232 emit_associated_thunks (fn);
4234 function_depth--;
4238 struct nrv_data
4240 nrv_data () : visited (37) {}
4242 tree var;
4243 tree result;
4244 hash_table<nofree_ptr_hash <tree_node> > visited;
4247 /* Helper function for walk_tree, used by finalize_nrv below. */
4249 static tree
4250 finalize_nrv_r (tree* tp, int* walk_subtrees, void* data)
4252 struct nrv_data *dp = (struct nrv_data *)data;
4253 tree_node **slot;
4255 /* No need to walk into types. There wouldn't be any need to walk into
4256 non-statements, except that we have to consider STMT_EXPRs. */
4257 if (TYPE_P (*tp))
4258 *walk_subtrees = 0;
4259 /* Change all returns to just refer to the RESULT_DECL; this is a nop,
4260 but differs from using NULL_TREE in that it indicates that we care
4261 about the value of the RESULT_DECL. */
4262 else if (TREE_CODE (*tp) == RETURN_EXPR)
4263 TREE_OPERAND (*tp, 0) = dp->result;
4264 /* Change all cleanups for the NRV to only run when an exception is
4265 thrown. */
4266 else if (TREE_CODE (*tp) == CLEANUP_STMT
4267 && CLEANUP_DECL (*tp) == dp->var)
4268 CLEANUP_EH_ONLY (*tp) = 1;
4269 /* Replace the DECL_EXPR for the NRV with an initialization of the
4270 RESULT_DECL, if needed. */
4271 else if (TREE_CODE (*tp) == DECL_EXPR
4272 && DECL_EXPR_DECL (*tp) == dp->var)
4274 tree init;
4275 if (DECL_INITIAL (dp->var)
4276 && DECL_INITIAL (dp->var) != error_mark_node)
4277 init = build2 (INIT_EXPR, void_type_node, dp->result,
4278 DECL_INITIAL (dp->var));
4279 else
4280 init = build_empty_stmt (EXPR_LOCATION (*tp));
4281 DECL_INITIAL (dp->var) = NULL_TREE;
4282 SET_EXPR_LOCATION (init, EXPR_LOCATION (*tp));
4283 *tp = init;
4285 /* And replace all uses of the NRV with the RESULT_DECL. */
4286 else if (*tp == dp->var)
4287 *tp = dp->result;
4289 /* Avoid walking into the same tree more than once. Unfortunately, we
4290 can't just use walk_tree_without duplicates because it would only call
4291 us for the first occurrence of dp->var in the function body. */
4292 slot = dp->visited.find_slot (*tp, INSERT);
4293 if (*slot)
4294 *walk_subtrees = 0;
4295 else
4296 *slot = *tp;
4298 /* Keep iterating. */
4299 return NULL_TREE;
4302 /* Called from finish_function to implement the named return value
4303 optimization by overriding all the RETURN_EXPRs and pertinent
4304 CLEANUP_STMTs and replacing all occurrences of VAR with RESULT, the
4305 RESULT_DECL for the function. */
4307 void
4308 finalize_nrv (tree *tp, tree var, tree result)
4310 struct nrv_data data;
4312 /* Copy name from VAR to RESULT. */
4313 DECL_NAME (result) = DECL_NAME (var);
4314 /* Don't forget that we take its address. */
4315 TREE_ADDRESSABLE (result) = TREE_ADDRESSABLE (var);
4316 /* Finally set DECL_VALUE_EXPR to avoid assigning
4317 a stack slot at -O0 for the original var and debug info
4318 uses RESULT location for VAR. */
4319 SET_DECL_VALUE_EXPR (var, result);
4320 DECL_HAS_VALUE_EXPR_P (var) = 1;
4322 data.var = var;
4323 data.result = result;
4324 cp_walk_tree (tp, finalize_nrv_r, &data, 0);
4327 /* Create CP_OMP_CLAUSE_INFO for clause C. Returns true if it is invalid. */
4329 bool
4330 cxx_omp_create_clause_info (tree c, tree type, bool need_default_ctor,
4331 bool need_copy_ctor, bool need_copy_assignment,
4332 bool need_dtor)
4334 int save_errorcount = errorcount;
4335 tree info, t;
4337 /* Always allocate 3 elements for simplicity. These are the
4338 function decls for the ctor, dtor, and assignment op.
4339 This layout is known to the three lang hooks,
4340 cxx_omp_clause_default_init, cxx_omp_clause_copy_init,
4341 and cxx_omp_clause_assign_op. */
4342 info = make_tree_vec (3);
4343 CP_OMP_CLAUSE_INFO (c) = info;
4345 if (need_default_ctor || need_copy_ctor)
4347 if (need_default_ctor)
4348 t = get_default_ctor (type);
4349 else
4350 t = get_copy_ctor (type, tf_warning_or_error);
4352 if (t && !trivial_fn_p (t))
4353 TREE_VEC_ELT (info, 0) = t;
4356 if (need_dtor && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
4357 TREE_VEC_ELT (info, 1) = get_dtor (type, tf_warning_or_error);
4359 if (need_copy_assignment)
4361 t = get_copy_assign (type);
4363 if (t && !trivial_fn_p (t))
4364 TREE_VEC_ELT (info, 2) = t;
4367 return errorcount != save_errorcount;
4370 /* If DECL is DECL_OMP_PRIVATIZED_MEMBER, return corresponding
4371 FIELD_DECL, otherwise return DECL itself. */
4373 static tree
4374 omp_clause_decl_field (tree decl)
4376 if (VAR_P (decl)
4377 && DECL_HAS_VALUE_EXPR_P (decl)
4378 && DECL_ARTIFICIAL (decl)
4379 && DECL_LANG_SPECIFIC (decl)
4380 && DECL_OMP_PRIVATIZED_MEMBER (decl))
4382 tree f = DECL_VALUE_EXPR (decl);
4383 if (TREE_CODE (f) == INDIRECT_REF)
4384 f = TREE_OPERAND (f, 0);
4385 if (TREE_CODE (f) == COMPONENT_REF)
4387 f = TREE_OPERAND (f, 1);
4388 gcc_assert (TREE_CODE (f) == FIELD_DECL);
4389 return f;
4392 return NULL_TREE;
4395 /* Adjust DECL if needed for printing using %qE. */
4397 static tree
4398 omp_clause_printable_decl (tree decl)
4400 tree t = omp_clause_decl_field (decl);
4401 if (t)
4402 return t;
4403 return decl;
4406 /* For a FIELD_DECL F and corresponding DECL_OMP_PRIVATIZED_MEMBER
4407 VAR_DECL T that doesn't need a DECL_EXPR added, record it for
4408 privatization. */
4410 static void
4411 omp_note_field_privatization (tree f, tree t)
4413 if (!omp_private_member_map)
4414 omp_private_member_map = new hash_map<tree, tree>;
4415 tree &v = omp_private_member_map->get_or_insert (f);
4416 if (v == NULL_TREE)
4418 v = t;
4419 omp_private_member_vec.safe_push (f);
4420 /* Signal that we don't want to create DECL_EXPR for this dummy var. */
4421 omp_private_member_vec.safe_push (integer_zero_node);
4425 /* Privatize FIELD_DECL T, return corresponding DECL_OMP_PRIVATIZED_MEMBER
4426 dummy VAR_DECL. */
4428 tree
4429 omp_privatize_field (tree t, bool shared)
4431 tree m = finish_non_static_data_member (t, NULL_TREE, NULL_TREE);
4432 if (m == error_mark_node)
4433 return error_mark_node;
4434 if (!omp_private_member_map && !shared)
4435 omp_private_member_map = new hash_map<tree, tree>;
4436 if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE)
4438 gcc_assert (TREE_CODE (m) == INDIRECT_REF);
4439 m = TREE_OPERAND (m, 0);
4441 tree vb = NULL_TREE;
4442 tree &v = shared ? vb : omp_private_member_map->get_or_insert (t);
4443 if (v == NULL_TREE)
4445 v = create_temporary_var (TREE_TYPE (m));
4446 retrofit_lang_decl (v);
4447 DECL_OMP_PRIVATIZED_MEMBER (v) = 1;
4448 SET_DECL_VALUE_EXPR (v, m);
4449 DECL_HAS_VALUE_EXPR_P (v) = 1;
4450 if (!shared)
4451 omp_private_member_vec.safe_push (t);
4453 return v;
4456 /* Helper function for handle_omp_array_sections. Called recursively
4457 to handle multiple array-section-subscripts. C is the clause,
4458 T current expression (initially OMP_CLAUSE_DECL), which is either
4459 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
4460 expression if specified, TREE_VALUE length expression if specified,
4461 TREE_CHAIN is what it has been specified after, or some decl.
4462 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
4463 set to true if any of the array-section-subscript could have length
4464 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
4465 first array-section-subscript which is known not to have length
4466 of one. Given say:
4467 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
4468 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
4469 all are or may have length of 1, array-section-subscript [:2] is the
4470 first one known not to have length 1. For array-section-subscript
4471 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
4472 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
4473 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
4474 case though, as some lengths could be zero. */
4476 static tree
4477 handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
4478 bool &maybe_zero_len, unsigned int &first_non_one,
4479 enum c_omp_region_type ort)
4481 tree ret, low_bound, length, type;
4482 if (TREE_CODE (t) != TREE_LIST)
4484 if (error_operand_p (t))
4485 return error_mark_node;
4486 if (REFERENCE_REF_P (t)
4487 && TREE_CODE (TREE_OPERAND (t, 0)) == COMPONENT_REF)
4488 t = TREE_OPERAND (t, 0);
4489 ret = t;
4490 if (TREE_CODE (t) == COMPONENT_REF
4491 && ort == C_ORT_OMP
4492 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
4493 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO
4494 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FROM)
4495 && !type_dependent_expression_p (t))
4497 if (TREE_CODE (TREE_OPERAND (t, 1)) == FIELD_DECL
4498 && DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
4500 error_at (OMP_CLAUSE_LOCATION (c),
4501 "bit-field %qE in %qs clause",
4502 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4503 return error_mark_node;
4505 while (TREE_CODE (t) == COMPONENT_REF)
4507 if (TREE_TYPE (TREE_OPERAND (t, 0))
4508 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == UNION_TYPE)
4510 error_at (OMP_CLAUSE_LOCATION (c),
4511 "%qE is a member of a union", t);
4512 return error_mark_node;
4514 t = TREE_OPERAND (t, 0);
4516 if (REFERENCE_REF_P (t))
4517 t = TREE_OPERAND (t, 0);
4519 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
4521 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
4522 return NULL_TREE;
4523 if (DECL_P (t))
4524 error_at (OMP_CLAUSE_LOCATION (c),
4525 "%qD is not a variable in %qs clause", t,
4526 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4527 else
4528 error_at (OMP_CLAUSE_LOCATION (c),
4529 "%qE is not a variable in %qs clause", t,
4530 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4531 return error_mark_node;
4533 else if (TREE_CODE (t) == PARM_DECL
4534 && DECL_ARTIFICIAL (t)
4535 && DECL_NAME (t) == this_identifier)
4537 error_at (OMP_CLAUSE_LOCATION (c),
4538 "%<this%> allowed in OpenMP only in %<declare simd%>"
4539 " clauses");
4540 return error_mark_node;
4542 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
4543 && VAR_P (t) && CP_DECL_THREAD_LOCAL_P (t))
4545 error_at (OMP_CLAUSE_LOCATION (c),
4546 "%qD is threadprivate variable in %qs clause", t,
4547 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4548 return error_mark_node;
4550 if (type_dependent_expression_p (ret))
4551 return NULL_TREE;
4552 ret = convert_from_reference (ret);
4553 return ret;
4556 if (ort == C_ORT_OMP
4557 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
4558 && TREE_CODE (TREE_CHAIN (t)) == FIELD_DECL)
4559 TREE_CHAIN (t) = omp_privatize_field (TREE_CHAIN (t), false);
4560 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
4561 maybe_zero_len, first_non_one, ort);
4562 if (ret == error_mark_node || ret == NULL_TREE)
4563 return ret;
4565 type = TREE_TYPE (ret);
4566 low_bound = TREE_PURPOSE (t);
4567 length = TREE_VALUE (t);
4568 if ((low_bound && type_dependent_expression_p (low_bound))
4569 || (length && type_dependent_expression_p (length)))
4570 return NULL_TREE;
4572 if (low_bound == error_mark_node || length == error_mark_node)
4573 return error_mark_node;
4575 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
4577 error_at (OMP_CLAUSE_LOCATION (c),
4578 "low bound %qE of array section does not have integral type",
4579 low_bound);
4580 return error_mark_node;
4582 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
4584 error_at (OMP_CLAUSE_LOCATION (c),
4585 "length %qE of array section does not have integral type",
4586 length);
4587 return error_mark_node;
4589 if (low_bound)
4590 low_bound = mark_rvalue_use (low_bound);
4591 if (length)
4592 length = mark_rvalue_use (length);
4593 /* We need to reduce to real constant-values for checks below. */
4594 if (length)
4595 length = fold_simple (length);
4596 if (low_bound)
4597 low_bound = fold_simple (low_bound);
4598 if (low_bound
4599 && TREE_CODE (low_bound) == INTEGER_CST
4600 && TYPE_PRECISION (TREE_TYPE (low_bound))
4601 > TYPE_PRECISION (sizetype))
4602 low_bound = fold_convert (sizetype, low_bound);
4603 if (length
4604 && TREE_CODE (length) == INTEGER_CST
4605 && TYPE_PRECISION (TREE_TYPE (length))
4606 > TYPE_PRECISION (sizetype))
4607 length = fold_convert (sizetype, length);
4608 if (low_bound == NULL_TREE)
4609 low_bound = integer_zero_node;
4611 if (length != NULL_TREE)
4613 if (!integer_nonzerop (length))
4615 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
4616 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
4618 if (integer_zerop (length))
4620 error_at (OMP_CLAUSE_LOCATION (c),
4621 "zero length array section in %qs clause",
4622 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4623 return error_mark_node;
4626 else
4627 maybe_zero_len = true;
4629 if (first_non_one == types.length ()
4630 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
4631 first_non_one++;
4633 if (TREE_CODE (type) == ARRAY_TYPE)
4635 if (length == NULL_TREE
4636 && (TYPE_DOMAIN (type) == NULL_TREE
4637 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
4639 error_at (OMP_CLAUSE_LOCATION (c),
4640 "for unknown bound array type length expression must "
4641 "be specified");
4642 return error_mark_node;
4644 if (TREE_CODE (low_bound) == INTEGER_CST
4645 && tree_int_cst_sgn (low_bound) == -1)
4647 error_at (OMP_CLAUSE_LOCATION (c),
4648 "negative low bound in array section in %qs clause",
4649 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4650 return error_mark_node;
4652 if (length != NULL_TREE
4653 && TREE_CODE (length) == INTEGER_CST
4654 && tree_int_cst_sgn (length) == -1)
4656 error_at (OMP_CLAUSE_LOCATION (c),
4657 "negative length in array section in %qs clause",
4658 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4659 return error_mark_node;
4661 if (TYPE_DOMAIN (type)
4662 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
4663 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
4664 == INTEGER_CST)
4666 tree size
4667 = fold_convert (sizetype, TYPE_MAX_VALUE (TYPE_DOMAIN (type)));
4668 size = size_binop (PLUS_EXPR, size, size_one_node);
4669 if (TREE_CODE (low_bound) == INTEGER_CST)
4671 if (tree_int_cst_lt (size, low_bound))
4673 error_at (OMP_CLAUSE_LOCATION (c),
4674 "low bound %qE above array section size "
4675 "in %qs clause", low_bound,
4676 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4677 return error_mark_node;
4679 if (tree_int_cst_equal (size, low_bound))
4681 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
4682 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
4684 error_at (OMP_CLAUSE_LOCATION (c),
4685 "zero length array section in %qs clause",
4686 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4687 return error_mark_node;
4689 maybe_zero_len = true;
4691 else if (length == NULL_TREE
4692 && first_non_one == types.length ()
4693 && tree_int_cst_equal
4694 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
4695 low_bound))
4696 first_non_one++;
4698 else if (length == NULL_TREE)
4700 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
4701 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION)
4702 maybe_zero_len = true;
4703 if (first_non_one == types.length ())
4704 first_non_one++;
4706 if (length && TREE_CODE (length) == INTEGER_CST)
4708 if (tree_int_cst_lt (size, length))
4710 error_at (OMP_CLAUSE_LOCATION (c),
4711 "length %qE above array section size "
4712 "in %qs clause", length,
4713 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4714 return error_mark_node;
4716 if (TREE_CODE (low_bound) == INTEGER_CST)
4718 tree lbpluslen
4719 = size_binop (PLUS_EXPR,
4720 fold_convert (sizetype, low_bound),
4721 fold_convert (sizetype, length));
4722 if (TREE_CODE (lbpluslen) == INTEGER_CST
4723 && tree_int_cst_lt (size, lbpluslen))
4725 error_at (OMP_CLAUSE_LOCATION (c),
4726 "high bound %qE above array section size "
4727 "in %qs clause", lbpluslen,
4728 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4729 return error_mark_node;
4734 else if (length == NULL_TREE)
4736 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
4737 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION)
4738 maybe_zero_len = true;
4739 if (first_non_one == types.length ())
4740 first_non_one++;
4743 /* For [lb:] we will need to evaluate lb more than once. */
4744 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
4746 tree lb = cp_save_expr (low_bound);
4747 if (lb != low_bound)
4749 TREE_PURPOSE (t) = lb;
4750 low_bound = lb;
4754 else if (TREE_CODE (type) == POINTER_TYPE)
4756 if (length == NULL_TREE)
4758 error_at (OMP_CLAUSE_LOCATION (c),
4759 "for pointer type length expression must be specified");
4760 return error_mark_node;
4762 if (length != NULL_TREE
4763 && TREE_CODE (length) == INTEGER_CST
4764 && tree_int_cst_sgn (length) == -1)
4766 error_at (OMP_CLAUSE_LOCATION (c),
4767 "negative length in array section in %qs clause",
4768 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4769 return error_mark_node;
4771 /* If there is a pointer type anywhere but in the very first
4772 array-section-subscript, the array section can't be contiguous. */
4773 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
4774 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
4776 error_at (OMP_CLAUSE_LOCATION (c),
4777 "array section is not contiguous in %qs clause",
4778 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4779 return error_mark_node;
4782 else
4784 error_at (OMP_CLAUSE_LOCATION (c),
4785 "%qE does not have pointer or array type", ret);
4786 return error_mark_node;
4788 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
4789 types.safe_push (TREE_TYPE (ret));
4790 /* We will need to evaluate lb more than once. */
4791 tree lb = cp_save_expr (low_bound);
4792 if (lb != low_bound)
4794 TREE_PURPOSE (t) = lb;
4795 low_bound = lb;
4797 ret = grok_array_decl (OMP_CLAUSE_LOCATION (c), ret, low_bound, false);
4798 return ret;
4801 /* Handle array sections for clause C. */
4803 static bool
4804 handle_omp_array_sections (tree c, enum c_omp_region_type ort)
4806 bool maybe_zero_len = false;
4807 unsigned int first_non_one = 0;
4808 auto_vec<tree, 10> types;
4809 tree first = handle_omp_array_sections_1 (c, OMP_CLAUSE_DECL (c), types,
4810 maybe_zero_len, first_non_one,
4811 ort);
4812 if (first == error_mark_node)
4813 return true;
4814 if (first == NULL_TREE)
4815 return false;
4816 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
4818 tree t = OMP_CLAUSE_DECL (c);
4819 tree tem = NULL_TREE;
4820 if (processing_template_decl)
4821 return false;
4822 /* Need to evaluate side effects in the length expressions
4823 if any. */
4824 while (TREE_CODE (t) == TREE_LIST)
4826 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
4828 if (tem == NULL_TREE)
4829 tem = TREE_VALUE (t);
4830 else
4831 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
4832 TREE_VALUE (t), tem);
4834 t = TREE_CHAIN (t);
4836 if (tem)
4837 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
4838 OMP_CLAUSE_DECL (c) = first;
4840 else
4842 unsigned int num = types.length (), i;
4843 tree t, side_effects = NULL_TREE, size = NULL_TREE;
4844 tree condition = NULL_TREE;
4846 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
4847 maybe_zero_len = true;
4848 if (processing_template_decl && maybe_zero_len)
4849 return false;
4851 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
4852 t = TREE_CHAIN (t))
4854 tree low_bound = TREE_PURPOSE (t);
4855 tree length = TREE_VALUE (t);
4857 i--;
4858 if (low_bound
4859 && TREE_CODE (low_bound) == INTEGER_CST
4860 && TYPE_PRECISION (TREE_TYPE (low_bound))
4861 > TYPE_PRECISION (sizetype))
4862 low_bound = fold_convert (sizetype, low_bound);
4863 if (length
4864 && TREE_CODE (length) == INTEGER_CST
4865 && TYPE_PRECISION (TREE_TYPE (length))
4866 > TYPE_PRECISION (sizetype))
4867 length = fold_convert (sizetype, length);
4868 if (low_bound == NULL_TREE)
4869 low_bound = integer_zero_node;
4870 if (!maybe_zero_len && i > first_non_one)
4872 if (integer_nonzerop (low_bound))
4873 goto do_warn_noncontiguous;
4874 if (length != NULL_TREE
4875 && TREE_CODE (length) == INTEGER_CST
4876 && TYPE_DOMAIN (types[i])
4877 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
4878 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
4879 == INTEGER_CST)
4881 tree size;
4882 size = size_binop (PLUS_EXPR,
4883 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
4884 size_one_node);
4885 if (!tree_int_cst_equal (length, size))
4887 do_warn_noncontiguous:
4888 error_at (OMP_CLAUSE_LOCATION (c),
4889 "array section is not contiguous in %qs "
4890 "clause",
4891 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4892 return true;
4895 if (!processing_template_decl
4896 && length != NULL_TREE
4897 && TREE_SIDE_EFFECTS (length))
4899 if (side_effects == NULL_TREE)
4900 side_effects = length;
4901 else
4902 side_effects = build2 (COMPOUND_EXPR,
4903 TREE_TYPE (side_effects),
4904 length, side_effects);
4907 else if (processing_template_decl)
4908 continue;
4909 else
4911 tree l;
4913 if (i > first_non_one
4914 && ((length && integer_nonzerop (length))
4915 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION))
4916 continue;
4917 if (length)
4918 l = fold_convert (sizetype, length);
4919 else
4921 l = size_binop (PLUS_EXPR,
4922 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
4923 size_one_node);
4924 l = size_binop (MINUS_EXPR, l,
4925 fold_convert (sizetype, low_bound));
4927 if (i > first_non_one)
4929 l = fold_build2 (NE_EXPR, boolean_type_node, l,
4930 size_zero_node);
4931 if (condition == NULL_TREE)
4932 condition = l;
4933 else
4934 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
4935 l, condition);
4937 else if (size == NULL_TREE)
4939 size = size_in_bytes (TREE_TYPE (types[i]));
4940 tree eltype = TREE_TYPE (types[num - 1]);
4941 while (TREE_CODE (eltype) == ARRAY_TYPE)
4942 eltype = TREE_TYPE (eltype);
4943 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
4944 size = size_binop (EXACT_DIV_EXPR, size,
4945 size_in_bytes (eltype));
4946 size = size_binop (MULT_EXPR, size, l);
4947 if (condition)
4948 size = fold_build3 (COND_EXPR, sizetype, condition,
4949 size, size_zero_node);
4951 else
4952 size = size_binop (MULT_EXPR, size, l);
4955 if (!processing_template_decl)
4957 if (side_effects)
4958 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
4959 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
4961 size = size_binop (MINUS_EXPR, size, size_one_node);
4962 tree index_type = build_index_type (size);
4963 tree eltype = TREE_TYPE (first);
4964 while (TREE_CODE (eltype) == ARRAY_TYPE)
4965 eltype = TREE_TYPE (eltype);
4966 tree type = build_array_type (eltype, index_type);
4967 tree ptype = build_pointer_type (eltype);
4968 if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE
4969 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (t))))
4970 t = convert_from_reference (t);
4971 else if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
4972 t = build_fold_addr_expr (t);
4973 tree t2 = build_fold_addr_expr (first);
4974 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
4975 ptrdiff_type_node, t2);
4976 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
4977 ptrdiff_type_node, t2,
4978 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
4979 ptrdiff_type_node, t));
4980 if (tree_fits_shwi_p (t2))
4981 t = build2 (MEM_REF, type, t,
4982 build_int_cst (ptype, tree_to_shwi (t2)));
4983 else
4985 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
4986 sizetype, t2);
4987 t = build2_loc (OMP_CLAUSE_LOCATION (c), POINTER_PLUS_EXPR,
4988 TREE_TYPE (t), t, t2);
4989 t = build2 (MEM_REF, type, t, build_int_cst (ptype, 0));
4991 OMP_CLAUSE_DECL (c) = t;
4992 return false;
4994 OMP_CLAUSE_DECL (c) = first;
4995 OMP_CLAUSE_SIZE (c) = size;
4996 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
4997 || (TREE_CODE (t) == COMPONENT_REF
4998 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE))
4999 return false;
5000 if (ort == C_ORT_OMP || ort == C_ORT_ACC)
5001 switch (OMP_CLAUSE_MAP_KIND (c))
5003 case GOMP_MAP_ALLOC:
5004 case GOMP_MAP_TO:
5005 case GOMP_MAP_FROM:
5006 case GOMP_MAP_TOFROM:
5007 case GOMP_MAP_ALWAYS_TO:
5008 case GOMP_MAP_ALWAYS_FROM:
5009 case GOMP_MAP_ALWAYS_TOFROM:
5010 case GOMP_MAP_RELEASE:
5011 case GOMP_MAP_DELETE:
5012 case GOMP_MAP_FORCE_TO:
5013 case GOMP_MAP_FORCE_FROM:
5014 case GOMP_MAP_FORCE_TOFROM:
5015 case GOMP_MAP_FORCE_PRESENT:
5016 OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (c) = 1;
5017 break;
5018 default:
5019 break;
5021 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
5022 OMP_CLAUSE_MAP);
5023 if ((ort & C_ORT_OMP_DECLARE_SIMD) != C_ORT_OMP && ort != C_ORT_ACC)
5024 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_POINTER);
5025 else if (TREE_CODE (t) == COMPONENT_REF)
5026 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ALWAYS_POINTER);
5027 else if (REFERENCE_REF_P (t)
5028 && TREE_CODE (TREE_OPERAND (t, 0)) == COMPONENT_REF)
5030 t = TREE_OPERAND (t, 0);
5031 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ALWAYS_POINTER);
5033 else
5034 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_FIRSTPRIVATE_POINTER);
5035 if (OMP_CLAUSE_MAP_KIND (c2) != GOMP_MAP_FIRSTPRIVATE_POINTER
5036 && !cxx_mark_addressable (t))
5037 return false;
5038 OMP_CLAUSE_DECL (c2) = t;
5039 t = build_fold_addr_expr (first);
5040 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
5041 ptrdiff_type_node, t);
5042 tree ptr = OMP_CLAUSE_DECL (c2);
5043 ptr = convert_from_reference (ptr);
5044 if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
5045 ptr = build_fold_addr_expr (ptr);
5046 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
5047 ptrdiff_type_node, t,
5048 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
5049 ptrdiff_type_node, ptr));
5050 OMP_CLAUSE_SIZE (c2) = t;
5051 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
5052 OMP_CLAUSE_CHAIN (c) = c2;
5053 ptr = OMP_CLAUSE_DECL (c2);
5054 if (OMP_CLAUSE_MAP_KIND (c2) != GOMP_MAP_FIRSTPRIVATE_POINTER
5055 && TREE_CODE (TREE_TYPE (ptr)) == REFERENCE_TYPE
5056 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (ptr))))
5058 tree c3 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
5059 OMP_CLAUSE_MAP);
5060 OMP_CLAUSE_SET_MAP_KIND (c3, OMP_CLAUSE_MAP_KIND (c2));
5061 OMP_CLAUSE_DECL (c3) = ptr;
5062 if (OMP_CLAUSE_MAP_KIND (c2) == GOMP_MAP_ALWAYS_POINTER)
5063 OMP_CLAUSE_DECL (c2) = build_simple_mem_ref (ptr);
5064 else
5065 OMP_CLAUSE_DECL (c2) = convert_from_reference (ptr);
5066 OMP_CLAUSE_SIZE (c3) = size_zero_node;
5067 OMP_CLAUSE_CHAIN (c3) = OMP_CLAUSE_CHAIN (c2);
5068 OMP_CLAUSE_CHAIN (c2) = c3;
5072 return false;
5075 /* Return identifier to look up for omp declare reduction. */
5077 tree
5078 omp_reduction_id (enum tree_code reduction_code, tree reduction_id, tree type)
5080 const char *p = NULL;
5081 const char *m = NULL;
5082 switch (reduction_code)
5084 case PLUS_EXPR:
5085 case MULT_EXPR:
5086 case MINUS_EXPR:
5087 case BIT_AND_EXPR:
5088 case BIT_XOR_EXPR:
5089 case BIT_IOR_EXPR:
5090 case TRUTH_ANDIF_EXPR:
5091 case TRUTH_ORIF_EXPR:
5092 reduction_id = cp_operator_id (reduction_code);
5093 break;
5094 case MIN_EXPR:
5095 p = "min";
5096 break;
5097 case MAX_EXPR:
5098 p = "max";
5099 break;
5100 default:
5101 break;
5104 if (p == NULL)
5106 if (TREE_CODE (reduction_id) != IDENTIFIER_NODE)
5107 return error_mark_node;
5108 p = IDENTIFIER_POINTER (reduction_id);
5111 if (type != NULL_TREE)
5112 m = mangle_type_string (TYPE_MAIN_VARIANT (type));
5114 const char prefix[] = "omp declare reduction ";
5115 size_t lenp = sizeof (prefix);
5116 if (strncmp (p, prefix, lenp - 1) == 0)
5117 lenp = 1;
5118 size_t len = strlen (p);
5119 size_t lenm = m ? strlen (m) + 1 : 0;
5120 char *name = XALLOCAVEC (char, lenp + len + lenm);
5121 if (lenp > 1)
5122 memcpy (name, prefix, lenp - 1);
5123 memcpy (name + lenp - 1, p, len + 1);
5124 if (m)
5126 name[lenp + len - 1] = '~';
5127 memcpy (name + lenp + len, m, lenm);
5129 return get_identifier (name);
5132 /* Lookup OpenMP UDR ID for TYPE, return the corresponding artificial
5133 FUNCTION_DECL or NULL_TREE if not found. */
5135 static tree
5136 omp_reduction_lookup (location_t loc, tree id, tree type, tree *baselinkp,
5137 vec<tree> *ambiguousp)
5139 tree orig_id = id;
5140 tree baselink = NULL_TREE;
5141 if (identifier_p (id))
5143 cp_id_kind idk;
5144 bool nonint_cst_expression_p;
5145 const char *error_msg;
5146 id = omp_reduction_id (ERROR_MARK, id, type);
5147 tree decl = lookup_name (id);
5148 if (decl == NULL_TREE)
5149 decl = error_mark_node;
5150 id = finish_id_expression (id, decl, NULL_TREE, &idk, false, true,
5151 &nonint_cst_expression_p, false, true, false,
5152 false, &error_msg, loc);
5153 if (idk == CP_ID_KIND_UNQUALIFIED
5154 && identifier_p (id))
5156 vec<tree, va_gc> *args = NULL;
5157 vec_safe_push (args, build_reference_type (type));
5158 id = perform_koenig_lookup (id, args, tf_none);
5161 else if (TREE_CODE (id) == SCOPE_REF)
5162 id = lookup_qualified_name (TREE_OPERAND (id, 0),
5163 omp_reduction_id (ERROR_MARK,
5164 TREE_OPERAND (id, 1),
5165 type),
5166 false, false);
5167 tree fns = id;
5168 id = NULL_TREE;
5169 if (fns && is_overloaded_fn (fns))
5171 for (lkp_iterator iter (get_fns (fns)); iter; ++iter)
5173 tree fndecl = *iter;
5174 if (TREE_CODE (fndecl) == FUNCTION_DECL)
5176 tree argtype = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
5177 if (same_type_p (TREE_TYPE (argtype), type))
5179 id = fndecl;
5180 break;
5185 if (id && BASELINK_P (fns))
5187 if (baselinkp)
5188 *baselinkp = fns;
5189 else
5190 baselink = fns;
5194 if (!id && CLASS_TYPE_P (type) && TYPE_BINFO (type))
5196 vec<tree> ambiguous = vNULL;
5197 tree binfo = TYPE_BINFO (type), base_binfo, ret = NULL_TREE;
5198 unsigned int ix;
5199 if (ambiguousp == NULL)
5200 ambiguousp = &ambiguous;
5201 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
5203 id = omp_reduction_lookup (loc, orig_id, BINFO_TYPE (base_binfo),
5204 baselinkp ? baselinkp : &baselink,
5205 ambiguousp);
5206 if (id == NULL_TREE)
5207 continue;
5208 if (!ambiguousp->is_empty ())
5209 ambiguousp->safe_push (id);
5210 else if (ret != NULL_TREE)
5212 ambiguousp->safe_push (ret);
5213 ambiguousp->safe_push (id);
5214 ret = NULL_TREE;
5216 else
5217 ret = id;
5219 if (ambiguousp != &ambiguous)
5220 return ret;
5221 if (!ambiguous.is_empty ())
5223 const char *str = _("candidates are:");
5224 unsigned int idx;
5225 tree udr;
5226 error_at (loc, "user defined reduction lookup is ambiguous");
5227 FOR_EACH_VEC_ELT (ambiguous, idx, udr)
5229 inform (DECL_SOURCE_LOCATION (udr), "%s %#qD", str, udr);
5230 if (idx == 0)
5231 str = get_spaces (str);
5233 ambiguous.release ();
5234 ret = error_mark_node;
5235 baselink = NULL_TREE;
5237 id = ret;
5239 if (id && baselink)
5240 perform_or_defer_access_check (BASELINK_BINFO (baselink),
5241 id, id, tf_warning_or_error);
5242 return id;
5245 /* Helper function for cp_parser_omp_declare_reduction_exprs
5246 and tsubst_omp_udr.
5247 Remove CLEANUP_STMT for data (omp_priv variable).
5248 Also append INIT_EXPR for DECL_INITIAL of omp_priv after its
5249 DECL_EXPR. */
5251 tree
5252 cp_remove_omp_priv_cleanup_stmt (tree *tp, int *walk_subtrees, void *data)
5254 if (TYPE_P (*tp))
5255 *walk_subtrees = 0;
5256 else if (TREE_CODE (*tp) == CLEANUP_STMT && CLEANUP_DECL (*tp) == (tree) data)
5257 *tp = CLEANUP_BODY (*tp);
5258 else if (TREE_CODE (*tp) == DECL_EXPR)
5260 tree decl = DECL_EXPR_DECL (*tp);
5261 if (!processing_template_decl
5262 && decl == (tree) data
5263 && DECL_INITIAL (decl)
5264 && DECL_INITIAL (decl) != error_mark_node)
5266 tree list = NULL_TREE;
5267 append_to_statement_list_force (*tp, &list);
5268 tree init_expr = build2 (INIT_EXPR, void_type_node,
5269 decl, DECL_INITIAL (decl));
5270 DECL_INITIAL (decl) = NULL_TREE;
5271 append_to_statement_list_force (init_expr, &list);
5272 *tp = list;
5275 return NULL_TREE;
5278 /* Data passed from cp_check_omp_declare_reduction to
5279 cp_check_omp_declare_reduction_r. */
5281 struct cp_check_omp_declare_reduction_data
5283 location_t loc;
5284 tree stmts[7];
5285 bool combiner_p;
5288 /* Helper function for cp_check_omp_declare_reduction, called via
5289 cp_walk_tree. */
5291 static tree
5292 cp_check_omp_declare_reduction_r (tree *tp, int *, void *data)
5294 struct cp_check_omp_declare_reduction_data *udr_data
5295 = (struct cp_check_omp_declare_reduction_data *) data;
5296 if (SSA_VAR_P (*tp)
5297 && !DECL_ARTIFICIAL (*tp)
5298 && *tp != DECL_EXPR_DECL (udr_data->stmts[udr_data->combiner_p ? 0 : 3])
5299 && *tp != DECL_EXPR_DECL (udr_data->stmts[udr_data->combiner_p ? 1 : 4]))
5301 location_t loc = udr_data->loc;
5302 if (udr_data->combiner_p)
5303 error_at (loc, "%<#pragma omp declare reduction%> combiner refers to "
5304 "variable %qD which is not %<omp_out%> nor %<omp_in%>",
5305 *tp);
5306 else
5307 error_at (loc, "%<#pragma omp declare reduction%> initializer refers "
5308 "to variable %qD which is not %<omp_priv%> nor "
5309 "%<omp_orig%>",
5310 *tp);
5311 return *tp;
5313 return NULL_TREE;
5316 /* Diagnose violation of OpenMP #pragma omp declare reduction restrictions. */
5318 void
5319 cp_check_omp_declare_reduction (tree udr)
5321 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (udr)));
5322 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
5323 type = TREE_TYPE (type);
5324 int i;
5325 location_t loc = DECL_SOURCE_LOCATION (udr);
5327 if (type == error_mark_node)
5328 return;
5329 if (ARITHMETIC_TYPE_P (type))
5331 static enum tree_code predef_codes[]
5332 = { PLUS_EXPR, MULT_EXPR, MINUS_EXPR, BIT_AND_EXPR, BIT_XOR_EXPR,
5333 BIT_IOR_EXPR, TRUTH_ANDIF_EXPR, TRUTH_ORIF_EXPR };
5334 for (i = 0; i < 8; i++)
5336 tree id = omp_reduction_id (predef_codes[i], NULL_TREE, NULL_TREE);
5337 const char *n1 = IDENTIFIER_POINTER (DECL_NAME (udr));
5338 const char *n2 = IDENTIFIER_POINTER (id);
5339 if (strncmp (n1, n2, IDENTIFIER_LENGTH (id)) == 0
5340 && (n1[IDENTIFIER_LENGTH (id)] == '~'
5341 || n1[IDENTIFIER_LENGTH (id)] == '\0'))
5342 break;
5345 if (i == 8
5346 && TREE_CODE (type) != COMPLEX_EXPR)
5348 const char prefix_minmax[] = "omp declare reduction m";
5349 size_t prefix_size = sizeof (prefix_minmax) - 1;
5350 const char *n = IDENTIFIER_POINTER (DECL_NAME (udr));
5351 if (strncmp (IDENTIFIER_POINTER (DECL_NAME (udr)),
5352 prefix_minmax, prefix_size) == 0
5353 && ((n[prefix_size] == 'i' && n[prefix_size + 1] == 'n')
5354 || (n[prefix_size] == 'a' && n[prefix_size + 1] == 'x'))
5355 && (n[prefix_size + 2] == '~' || n[prefix_size + 2] == '\0'))
5356 i = 0;
5358 if (i < 8)
5360 error_at (loc, "predeclared arithmetic type %qT in "
5361 "%<#pragma omp declare reduction%>", type);
5362 return;
5365 else if (TREE_CODE (type) == FUNCTION_TYPE
5366 || TREE_CODE (type) == METHOD_TYPE
5367 || TREE_CODE (type) == ARRAY_TYPE)
5369 error_at (loc, "function or array type %qT in "
5370 "%<#pragma omp declare reduction%>", type);
5371 return;
5373 else if (TREE_CODE (type) == REFERENCE_TYPE)
5375 error_at (loc, "reference type %qT in %<#pragma omp declare reduction%>",
5376 type);
5377 return;
5379 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
5381 error_at (loc, "const, volatile or __restrict qualified type %qT in "
5382 "%<#pragma omp declare reduction%>", type);
5383 return;
5386 tree body = DECL_SAVED_TREE (udr);
5387 if (body == NULL_TREE || TREE_CODE (body) != STATEMENT_LIST)
5388 return;
5390 tree_stmt_iterator tsi;
5391 struct cp_check_omp_declare_reduction_data data;
5392 memset (data.stmts, 0, sizeof data.stmts);
5393 for (i = 0, tsi = tsi_start (body);
5394 i < 7 && !tsi_end_p (tsi);
5395 i++, tsi_next (&tsi))
5396 data.stmts[i] = tsi_stmt (tsi);
5397 data.loc = loc;
5398 gcc_assert (tsi_end_p (tsi));
5399 if (i >= 3)
5401 gcc_assert (TREE_CODE (data.stmts[0]) == DECL_EXPR
5402 && TREE_CODE (data.stmts[1]) == DECL_EXPR);
5403 if (TREE_NO_WARNING (DECL_EXPR_DECL (data.stmts[0])))
5404 return;
5405 data.combiner_p = true;
5406 if (cp_walk_tree (&data.stmts[2], cp_check_omp_declare_reduction_r,
5407 &data, NULL))
5408 TREE_NO_WARNING (DECL_EXPR_DECL (data.stmts[0])) = 1;
5410 if (i >= 6)
5412 gcc_assert (TREE_CODE (data.stmts[3]) == DECL_EXPR
5413 && TREE_CODE (data.stmts[4]) == DECL_EXPR);
5414 data.combiner_p = false;
5415 if (cp_walk_tree (&data.stmts[5], cp_check_omp_declare_reduction_r,
5416 &data, NULL)
5417 || cp_walk_tree (&DECL_INITIAL (DECL_EXPR_DECL (data.stmts[3])),
5418 cp_check_omp_declare_reduction_r, &data, NULL))
5419 TREE_NO_WARNING (DECL_EXPR_DECL (data.stmts[0])) = 1;
5420 if (i == 7)
5421 gcc_assert (TREE_CODE (data.stmts[6]) == DECL_EXPR);
5425 /* Helper function of finish_omp_clauses. Clone STMT as if we were making
5426 an inline call. But, remap
5427 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
5428 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
5430 static tree
5431 clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
5432 tree decl, tree placeholder)
5434 copy_body_data id;
5435 hash_map<tree, tree> decl_map;
5437 decl_map.put (omp_decl1, placeholder);
5438 decl_map.put (omp_decl2, decl);
5439 memset (&id, 0, sizeof (id));
5440 id.src_fn = DECL_CONTEXT (omp_decl1);
5441 id.dst_fn = current_function_decl;
5442 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
5443 id.decl_map = &decl_map;
5445 id.copy_decl = copy_decl_no_change;
5446 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
5447 id.transform_new_cfg = true;
5448 id.transform_return_to_modify = false;
5449 id.transform_lang_insert_block = NULL;
5450 id.eh_lp_nr = 0;
5451 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
5452 return stmt;
5455 /* Helper function of finish_omp_clauses, called via cp_walk_tree.
5456 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
5458 static tree
5459 find_omp_placeholder_r (tree *tp, int *, void *data)
5461 if (*tp == (tree) data)
5462 return *tp;
5463 return NULL_TREE;
5466 /* Helper function of finish_omp_clauses. Handle OMP_CLAUSE_REDUCTION C.
5467 Return true if there is some error and the clause should be removed. */
5469 static bool
5470 finish_omp_reduction_clause (tree c, bool *need_default_ctor, bool *need_dtor)
5472 tree t = OMP_CLAUSE_DECL (c);
5473 bool predefined = false;
5474 if (TREE_CODE (t) == TREE_LIST)
5476 gcc_assert (processing_template_decl);
5477 return false;
5479 tree type = TREE_TYPE (t);
5480 if (TREE_CODE (t) == MEM_REF)
5481 type = TREE_TYPE (type);
5482 if (TREE_CODE (type) == REFERENCE_TYPE)
5483 type = TREE_TYPE (type);
5484 if (TREE_CODE (type) == ARRAY_TYPE)
5486 tree oatype = type;
5487 gcc_assert (TREE_CODE (t) != MEM_REF);
5488 while (TREE_CODE (type) == ARRAY_TYPE)
5489 type = TREE_TYPE (type);
5490 if (!processing_template_decl)
5492 t = require_complete_type (t);
5493 if (t == error_mark_node)
5494 return true;
5495 tree size = size_binop (EXACT_DIV_EXPR, TYPE_SIZE_UNIT (oatype),
5496 TYPE_SIZE_UNIT (type));
5497 if (integer_zerop (size))
5499 error ("%qE in %<reduction%> clause is a zero size array",
5500 omp_clause_printable_decl (t));
5501 return true;
5503 size = size_binop (MINUS_EXPR, size, size_one_node);
5504 tree index_type = build_index_type (size);
5505 tree atype = build_array_type (type, index_type);
5506 tree ptype = build_pointer_type (type);
5507 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
5508 t = build_fold_addr_expr (t);
5509 t = build2 (MEM_REF, atype, t, build_int_cst (ptype, 0));
5510 OMP_CLAUSE_DECL (c) = t;
5513 if (type == error_mark_node)
5514 return true;
5515 else if (ARITHMETIC_TYPE_P (type))
5516 switch (OMP_CLAUSE_REDUCTION_CODE (c))
5518 case PLUS_EXPR:
5519 case MULT_EXPR:
5520 case MINUS_EXPR:
5521 predefined = true;
5522 break;
5523 case MIN_EXPR:
5524 case MAX_EXPR:
5525 if (TREE_CODE (type) == COMPLEX_TYPE)
5526 break;
5527 predefined = true;
5528 break;
5529 case BIT_AND_EXPR:
5530 case BIT_IOR_EXPR:
5531 case BIT_XOR_EXPR:
5532 if (FLOAT_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
5533 break;
5534 predefined = true;
5535 break;
5536 case TRUTH_ANDIF_EXPR:
5537 case TRUTH_ORIF_EXPR:
5538 if (FLOAT_TYPE_P (type))
5539 break;
5540 predefined = true;
5541 break;
5542 default:
5543 break;
5545 else if (TYPE_READONLY (type))
5547 error ("%qE has const type for %<reduction%>",
5548 omp_clause_printable_decl (t));
5549 return true;
5551 else if (!processing_template_decl)
5553 t = require_complete_type (t);
5554 if (t == error_mark_node)
5555 return true;
5556 OMP_CLAUSE_DECL (c) = t;
5559 if (predefined)
5561 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = NULL_TREE;
5562 return false;
5564 else if (processing_template_decl)
5565 return false;
5567 tree id = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
5569 type = TYPE_MAIN_VARIANT (type);
5570 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = NULL_TREE;
5571 if (id == NULL_TREE)
5572 id = omp_reduction_id (OMP_CLAUSE_REDUCTION_CODE (c),
5573 NULL_TREE, NULL_TREE);
5574 id = omp_reduction_lookup (OMP_CLAUSE_LOCATION (c), id, type, NULL, NULL);
5575 if (id)
5577 if (id == error_mark_node)
5578 return true;
5579 mark_used (id);
5580 tree body = DECL_SAVED_TREE (id);
5581 if (!body)
5582 return true;
5583 if (TREE_CODE (body) == STATEMENT_LIST)
5585 tree_stmt_iterator tsi;
5586 tree placeholder = NULL_TREE, decl_placeholder = NULL_TREE;
5587 int i;
5588 tree stmts[7];
5589 tree atype = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (id)));
5590 atype = TREE_TYPE (atype);
5591 bool need_static_cast = !same_type_p (type, atype);
5592 memset (stmts, 0, sizeof stmts);
5593 for (i = 0, tsi = tsi_start (body);
5594 i < 7 && !tsi_end_p (tsi);
5595 i++, tsi_next (&tsi))
5596 stmts[i] = tsi_stmt (tsi);
5597 gcc_assert (tsi_end_p (tsi));
5599 if (i >= 3)
5601 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
5602 && TREE_CODE (stmts[1]) == DECL_EXPR);
5603 placeholder = build_lang_decl (VAR_DECL, NULL_TREE, type);
5604 DECL_ARTIFICIAL (placeholder) = 1;
5605 DECL_IGNORED_P (placeholder) = 1;
5606 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
5607 if (TREE_CODE (t) == MEM_REF)
5609 decl_placeholder = build_lang_decl (VAR_DECL, NULL_TREE,
5610 type);
5611 DECL_ARTIFICIAL (decl_placeholder) = 1;
5612 DECL_IGNORED_P (decl_placeholder) = 1;
5613 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c) = decl_placeholder;
5615 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[0])))
5616 cxx_mark_addressable (placeholder);
5617 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[1]))
5618 && TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c)))
5619 != REFERENCE_TYPE)
5620 cxx_mark_addressable (decl_placeholder ? decl_placeholder
5621 : OMP_CLAUSE_DECL (c));
5622 tree omp_out = placeholder;
5623 tree omp_in = decl_placeholder ? decl_placeholder
5624 : convert_from_reference (OMP_CLAUSE_DECL (c));
5625 if (need_static_cast)
5627 tree rtype = build_reference_type (atype);
5628 omp_out = build_static_cast (rtype, omp_out,
5629 tf_warning_or_error);
5630 omp_in = build_static_cast (rtype, omp_in,
5631 tf_warning_or_error);
5632 if (omp_out == error_mark_node || omp_in == error_mark_node)
5633 return true;
5634 omp_out = convert_from_reference (omp_out);
5635 omp_in = convert_from_reference (omp_in);
5637 OMP_CLAUSE_REDUCTION_MERGE (c)
5638 = clone_omp_udr (stmts[2], DECL_EXPR_DECL (stmts[0]),
5639 DECL_EXPR_DECL (stmts[1]), omp_in, omp_out);
5641 if (i >= 6)
5643 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
5644 && TREE_CODE (stmts[4]) == DECL_EXPR);
5645 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[3])))
5646 cxx_mark_addressable (decl_placeholder ? decl_placeholder
5647 : OMP_CLAUSE_DECL (c));
5648 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[4])))
5649 cxx_mark_addressable (placeholder);
5650 tree omp_priv = decl_placeholder ? decl_placeholder
5651 : convert_from_reference (OMP_CLAUSE_DECL (c));
5652 tree omp_orig = placeholder;
5653 if (need_static_cast)
5655 if (i == 7)
5657 error_at (OMP_CLAUSE_LOCATION (c),
5658 "user defined reduction with constructor "
5659 "initializer for base class %qT", atype);
5660 return true;
5662 tree rtype = build_reference_type (atype);
5663 omp_priv = build_static_cast (rtype, omp_priv,
5664 tf_warning_or_error);
5665 omp_orig = build_static_cast (rtype, omp_orig,
5666 tf_warning_or_error);
5667 if (omp_priv == error_mark_node
5668 || omp_orig == error_mark_node)
5669 return true;
5670 omp_priv = convert_from_reference (omp_priv);
5671 omp_orig = convert_from_reference (omp_orig);
5673 if (i == 6)
5674 *need_default_ctor = true;
5675 OMP_CLAUSE_REDUCTION_INIT (c)
5676 = clone_omp_udr (stmts[5], DECL_EXPR_DECL (stmts[4]),
5677 DECL_EXPR_DECL (stmts[3]),
5678 omp_priv, omp_orig);
5679 if (cp_walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
5680 find_omp_placeholder_r, placeholder, NULL))
5681 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
5683 else if (i >= 3)
5685 if (CLASS_TYPE_P (type) && !pod_type_p (type))
5686 *need_default_ctor = true;
5687 else
5689 tree init;
5690 tree v = decl_placeholder ? decl_placeholder
5691 : convert_from_reference (t);
5692 if (AGGREGATE_TYPE_P (TREE_TYPE (v)))
5693 init = build_constructor (TREE_TYPE (v), NULL);
5694 else
5695 init = fold_convert (TREE_TYPE (v), integer_zero_node);
5696 OMP_CLAUSE_REDUCTION_INIT (c)
5697 = build2 (INIT_EXPR, TREE_TYPE (v), v, init);
5702 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
5703 *need_dtor = true;
5704 else
5706 error ("user defined reduction not found for %qE",
5707 omp_clause_printable_decl (t));
5708 return true;
5710 if (TREE_CODE (OMP_CLAUSE_DECL (c)) == MEM_REF)
5711 gcc_assert (TYPE_SIZE_UNIT (type)
5712 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST);
5713 return false;
5716 /* Called from finish_struct_1. linear(this) or linear(this:step)
5717 clauses might not be finalized yet because the class has been incomplete
5718 when parsing #pragma omp declare simd methods. Fix those up now. */
5720 void
5721 finish_omp_declare_simd_methods (tree t)
5723 if (processing_template_decl)
5724 return;
5726 for (tree x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
5728 if (TREE_CODE (TREE_TYPE (x)) != METHOD_TYPE)
5729 continue;
5730 tree ods = lookup_attribute ("omp declare simd", DECL_ATTRIBUTES (x));
5731 if (!ods || !TREE_VALUE (ods))
5732 continue;
5733 for (tree c = TREE_VALUE (TREE_VALUE (ods)); c; c = OMP_CLAUSE_CHAIN (c))
5734 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
5735 && integer_zerop (OMP_CLAUSE_DECL (c))
5736 && OMP_CLAUSE_LINEAR_STEP (c)
5737 && TREE_CODE (TREE_TYPE (OMP_CLAUSE_LINEAR_STEP (c)))
5738 == POINTER_TYPE)
5740 tree s = OMP_CLAUSE_LINEAR_STEP (c);
5741 s = fold_convert_loc (OMP_CLAUSE_LOCATION (c), sizetype, s);
5742 s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MULT_EXPR,
5743 sizetype, s, TYPE_SIZE_UNIT (t));
5744 OMP_CLAUSE_LINEAR_STEP (c) = s;
5749 /* Adjust sink depend clause to take into account pointer offsets.
5751 Return TRUE if there was a problem processing the offset, and the
5752 whole clause should be removed. */
5754 static bool
5755 cp_finish_omp_clause_depend_sink (tree sink_clause)
5757 tree t = OMP_CLAUSE_DECL (sink_clause);
5758 gcc_assert (TREE_CODE (t) == TREE_LIST);
5760 /* Make sure we don't adjust things twice for templates. */
5761 if (processing_template_decl)
5762 return false;
5764 for (; t; t = TREE_CHAIN (t))
5766 tree decl = TREE_VALUE (t);
5767 if (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
5769 tree offset = TREE_PURPOSE (t);
5770 bool neg = wi::neg_p ((wide_int) offset);
5771 offset = fold_unary (ABS_EXPR, TREE_TYPE (offset), offset);
5772 decl = mark_rvalue_use (decl);
5773 decl = convert_from_reference (decl);
5774 tree t2 = pointer_int_sum (OMP_CLAUSE_LOCATION (sink_clause),
5775 neg ? MINUS_EXPR : PLUS_EXPR,
5776 decl, offset);
5777 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (sink_clause),
5778 MINUS_EXPR, sizetype,
5779 fold_convert (sizetype, t2),
5780 fold_convert (sizetype, decl));
5781 if (t2 == error_mark_node)
5782 return true;
5783 TREE_PURPOSE (t) = t2;
5786 return false;
5789 /* For all elements of CLAUSES, validate them vs OpenMP constraints.
5790 Remove any elements from the list that are invalid. */
5792 tree
5793 finish_omp_clauses (tree clauses, enum c_omp_region_type ort)
5795 bitmap_head generic_head, firstprivate_head, lastprivate_head;
5796 bitmap_head aligned_head, map_head, map_field_head, oacc_reduction_head;
5797 tree c, t, *pc;
5798 tree safelen = NULL_TREE;
5799 bool branch_seen = false;
5800 bool copyprivate_seen = false;
5801 bool ordered_seen = false;
5802 bool oacc_async = false;
5804 bitmap_obstack_initialize (NULL);
5805 bitmap_initialize (&generic_head, &bitmap_default_obstack);
5806 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
5807 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
5808 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
5809 bitmap_initialize (&map_head, &bitmap_default_obstack);
5810 bitmap_initialize (&map_field_head, &bitmap_default_obstack);
5811 bitmap_initialize (&oacc_reduction_head, &bitmap_default_obstack);
5813 if (ort & C_ORT_ACC)
5814 for (c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
5815 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ASYNC)
5817 oacc_async = true;
5818 break;
5821 for (pc = &clauses, c = clauses; c ; c = *pc)
5823 bool remove = false;
5824 bool field_ok = false;
5826 switch (OMP_CLAUSE_CODE (c))
5828 case OMP_CLAUSE_SHARED:
5829 field_ok = ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP);
5830 goto check_dup_generic;
5831 case OMP_CLAUSE_PRIVATE:
5832 field_ok = ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP);
5833 goto check_dup_generic;
5834 case OMP_CLAUSE_REDUCTION:
5835 field_ok = ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP);
5836 t = OMP_CLAUSE_DECL (c);
5837 if (TREE_CODE (t) == TREE_LIST)
5839 if (handle_omp_array_sections (c, ort))
5841 remove = true;
5842 break;
5844 if (TREE_CODE (t) == TREE_LIST)
5846 while (TREE_CODE (t) == TREE_LIST)
5847 t = TREE_CHAIN (t);
5849 else
5851 gcc_assert (TREE_CODE (t) == MEM_REF);
5852 t = TREE_OPERAND (t, 0);
5853 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
5854 t = TREE_OPERAND (t, 0);
5855 if (TREE_CODE (t) == ADDR_EXPR
5856 || TREE_CODE (t) == INDIRECT_REF)
5857 t = TREE_OPERAND (t, 0);
5859 tree n = omp_clause_decl_field (t);
5860 if (n)
5861 t = n;
5862 goto check_dup_generic_t;
5864 if (oacc_async)
5865 cxx_mark_addressable (t);
5866 goto check_dup_generic;
5867 case OMP_CLAUSE_COPYPRIVATE:
5868 copyprivate_seen = true;
5869 field_ok = ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP);
5870 goto check_dup_generic;
5871 case OMP_CLAUSE_COPYIN:
5872 goto check_dup_generic;
5873 case OMP_CLAUSE_LINEAR:
5874 field_ok = ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP);
5875 t = OMP_CLAUSE_DECL (c);
5876 if (ort != C_ORT_OMP_DECLARE_SIMD
5877 && OMP_CLAUSE_LINEAR_KIND (c) != OMP_CLAUSE_LINEAR_DEFAULT)
5879 error_at (OMP_CLAUSE_LOCATION (c),
5880 "modifier should not be specified in %<linear%> "
5881 "clause on %<simd%> or %<for%> constructs");
5882 OMP_CLAUSE_LINEAR_KIND (c) = OMP_CLAUSE_LINEAR_DEFAULT;
5884 if ((VAR_P (t) || TREE_CODE (t) == PARM_DECL)
5885 && !type_dependent_expression_p (t))
5887 tree type = TREE_TYPE (t);
5888 if ((OMP_CLAUSE_LINEAR_KIND (c) == OMP_CLAUSE_LINEAR_REF
5889 || OMP_CLAUSE_LINEAR_KIND (c) == OMP_CLAUSE_LINEAR_UVAL)
5890 && TREE_CODE (type) != REFERENCE_TYPE)
5892 error ("linear clause with %qs modifier applied to "
5893 "non-reference variable with %qT type",
5894 OMP_CLAUSE_LINEAR_KIND (c) == OMP_CLAUSE_LINEAR_REF
5895 ? "ref" : "uval", TREE_TYPE (t));
5896 remove = true;
5897 break;
5899 if (TREE_CODE (type) == REFERENCE_TYPE)
5900 type = TREE_TYPE (type);
5901 if (ort == C_ORT_CILK)
5903 if (!INTEGRAL_TYPE_P (type)
5904 && !SCALAR_FLOAT_TYPE_P (type)
5905 && TREE_CODE (type) != POINTER_TYPE)
5907 error ("linear clause applied to non-integral, "
5908 "non-floating, non-pointer variable with %qT type",
5909 TREE_TYPE (t));
5910 remove = true;
5911 break;
5914 else if (OMP_CLAUSE_LINEAR_KIND (c) != OMP_CLAUSE_LINEAR_REF)
5916 if (!INTEGRAL_TYPE_P (type)
5917 && TREE_CODE (type) != POINTER_TYPE)
5919 error ("linear clause applied to non-integral non-pointer"
5920 " variable with %qT type", TREE_TYPE (t));
5921 remove = true;
5922 break;
5926 t = OMP_CLAUSE_LINEAR_STEP (c);
5927 if (t == NULL_TREE)
5928 t = integer_one_node;
5929 if (t == error_mark_node)
5931 remove = true;
5932 break;
5934 else if (!type_dependent_expression_p (t)
5935 && !INTEGRAL_TYPE_P (TREE_TYPE (t))
5936 && (ort != C_ORT_OMP_DECLARE_SIMD
5937 || TREE_CODE (t) != PARM_DECL
5938 || TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE
5939 || !INTEGRAL_TYPE_P (TREE_TYPE (TREE_TYPE (t)))))
5941 error ("linear step expression must be integral");
5942 remove = true;
5943 break;
5945 else
5947 t = mark_rvalue_use (t);
5948 if (ort == C_ORT_OMP_DECLARE_SIMD && TREE_CODE (t) == PARM_DECL)
5950 OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c) = 1;
5951 goto check_dup_generic;
5953 if (!processing_template_decl
5954 && (VAR_P (OMP_CLAUSE_DECL (c))
5955 || TREE_CODE (OMP_CLAUSE_DECL (c)) == PARM_DECL))
5957 if (ort == C_ORT_OMP_DECLARE_SIMD)
5959 t = maybe_constant_value (t);
5960 if (TREE_CODE (t) != INTEGER_CST)
5962 error_at (OMP_CLAUSE_LOCATION (c),
5963 "%<linear%> clause step %qE is neither "
5964 "constant nor a parameter", t);
5965 remove = true;
5966 break;
5969 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5970 tree type = TREE_TYPE (OMP_CLAUSE_DECL (c));
5971 if (TREE_CODE (type) == REFERENCE_TYPE)
5972 type = TREE_TYPE (type);
5973 if (OMP_CLAUSE_LINEAR_KIND (c) == OMP_CLAUSE_LINEAR_REF)
5975 type = build_pointer_type (type);
5976 tree d = fold_convert (type, OMP_CLAUSE_DECL (c));
5977 t = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
5978 d, t);
5979 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c),
5980 MINUS_EXPR, sizetype,
5981 fold_convert (sizetype, t),
5982 fold_convert (sizetype, d));
5983 if (t == error_mark_node)
5985 remove = true;
5986 break;
5989 else if (TREE_CODE (type) == POINTER_TYPE
5990 /* Can't multiply the step yet if *this
5991 is still incomplete type. */
5992 && (ort != C_ORT_OMP_DECLARE_SIMD
5993 || TREE_CODE (OMP_CLAUSE_DECL (c)) != PARM_DECL
5994 || !DECL_ARTIFICIAL (OMP_CLAUSE_DECL (c))
5995 || DECL_NAME (OMP_CLAUSE_DECL (c))
5996 != this_identifier
5997 || !TYPE_BEING_DEFINED (TREE_TYPE (type))))
5999 tree d = convert_from_reference (OMP_CLAUSE_DECL (c));
6000 t = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
6001 d, t);
6002 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c),
6003 MINUS_EXPR, sizetype,
6004 fold_convert (sizetype, t),
6005 fold_convert (sizetype, d));
6006 if (t == error_mark_node)
6008 remove = true;
6009 break;
6012 else
6013 t = fold_convert (type, t);
6015 OMP_CLAUSE_LINEAR_STEP (c) = t;
6017 goto check_dup_generic;
6018 check_dup_generic:
6019 t = omp_clause_decl_field (OMP_CLAUSE_DECL (c));
6020 if (t)
6022 if (!remove && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_SHARED)
6023 omp_note_field_privatization (t, OMP_CLAUSE_DECL (c));
6025 else
6026 t = OMP_CLAUSE_DECL (c);
6027 check_dup_generic_t:
6028 if (t == current_class_ptr
6029 && (ort != C_ORT_OMP_DECLARE_SIMD
6030 || (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_LINEAR
6031 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_UNIFORM)))
6033 error ("%<this%> allowed in OpenMP only in %<declare simd%>"
6034 " clauses");
6035 remove = true;
6036 break;
6038 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL
6039 && (!field_ok || TREE_CODE (t) != FIELD_DECL))
6041 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
6042 break;
6043 if (DECL_P (t))
6044 error ("%qD is not a variable in clause %qs", t,
6045 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6046 else
6047 error ("%qE is not a variable in clause %qs", t,
6048 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6049 remove = true;
6051 else if (ort == C_ORT_ACC
6052 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
6054 if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
6056 error ("%qD appears more than once in reduction clauses", t);
6057 remove = true;
6059 else
6060 bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
6062 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
6063 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
6064 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
6066 error ("%qD appears more than once in data clauses", t);
6067 remove = true;
6069 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
6070 && bitmap_bit_p (&map_head, DECL_UID (t)))
6072 if (ort == C_ORT_ACC)
6073 error ("%qD appears more than once in data clauses", t);
6074 else
6075 error ("%qD appears both in data and map clauses", t);
6076 remove = true;
6078 else
6079 bitmap_set_bit (&generic_head, DECL_UID (t));
6080 if (!field_ok)
6081 break;
6082 handle_field_decl:
6083 if (!remove
6084 && TREE_CODE (t) == FIELD_DECL
6085 && t == OMP_CLAUSE_DECL (c)
6086 && ort != C_ORT_ACC)
6088 OMP_CLAUSE_DECL (c)
6089 = omp_privatize_field (t, (OMP_CLAUSE_CODE (c)
6090 == OMP_CLAUSE_SHARED));
6091 if (OMP_CLAUSE_DECL (c) == error_mark_node)
6092 remove = true;
6094 break;
6096 case OMP_CLAUSE_FIRSTPRIVATE:
6097 t = omp_clause_decl_field (OMP_CLAUSE_DECL (c));
6098 if (t)
6099 omp_note_field_privatization (t, OMP_CLAUSE_DECL (c));
6100 else
6101 t = OMP_CLAUSE_DECL (c);
6102 if (ort != C_ORT_ACC && t == current_class_ptr)
6104 error ("%<this%> allowed in OpenMP only in %<declare simd%>"
6105 " clauses");
6106 remove = true;
6107 break;
6109 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL
6110 && ((ort & C_ORT_OMP_DECLARE_SIMD) != C_ORT_OMP
6111 || TREE_CODE (t) != FIELD_DECL))
6113 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
6114 break;
6115 if (DECL_P (t))
6116 error ("%qD is not a variable in clause %<firstprivate%>", t);
6117 else
6118 error ("%qE is not a variable in clause %<firstprivate%>", t);
6119 remove = true;
6121 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
6122 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
6124 error ("%qD appears more than once in data clauses", t);
6125 remove = true;
6127 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
6129 if (ort == C_ORT_ACC)
6130 error ("%qD appears more than once in data clauses", t);
6131 else
6132 error ("%qD appears both in data and map clauses", t);
6133 remove = true;
6135 else
6136 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
6137 goto handle_field_decl;
6139 case OMP_CLAUSE_LASTPRIVATE:
6140 t = omp_clause_decl_field (OMP_CLAUSE_DECL (c));
6141 if (t)
6142 omp_note_field_privatization (t, OMP_CLAUSE_DECL (c));
6143 else
6144 t = OMP_CLAUSE_DECL (c);
6145 if (t == current_class_ptr)
6147 error ("%<this%> allowed in OpenMP only in %<declare simd%>"
6148 " clauses");
6149 remove = true;
6150 break;
6152 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL
6153 && ((ort & C_ORT_OMP_DECLARE_SIMD) != C_ORT_OMP
6154 || TREE_CODE (t) != FIELD_DECL))
6156 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
6157 break;
6158 if (DECL_P (t))
6159 error ("%qD is not a variable in clause %<lastprivate%>", t);
6160 else
6161 error ("%qE is not a variable in clause %<lastprivate%>", t);
6162 remove = true;
6164 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
6165 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
6167 error ("%qD appears more than once in data clauses", t);
6168 remove = true;
6170 else
6171 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
6172 goto handle_field_decl;
6174 case OMP_CLAUSE_IF:
6175 t = OMP_CLAUSE_IF_EXPR (c);
6176 t = maybe_convert_cond (t);
6177 if (t == error_mark_node)
6178 remove = true;
6179 else if (!processing_template_decl)
6180 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6181 OMP_CLAUSE_IF_EXPR (c) = t;
6182 break;
6184 case OMP_CLAUSE_FINAL:
6185 t = OMP_CLAUSE_FINAL_EXPR (c);
6186 t = maybe_convert_cond (t);
6187 if (t == error_mark_node)
6188 remove = true;
6189 else if (!processing_template_decl)
6190 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6191 OMP_CLAUSE_FINAL_EXPR (c) = t;
6192 break;
6194 case OMP_CLAUSE_GANG:
6195 /* Operand 1 is the gang static: argument. */
6196 t = OMP_CLAUSE_OPERAND (c, 1);
6197 if (t != NULL_TREE)
6199 if (t == error_mark_node)
6200 remove = true;
6201 else if (!type_dependent_expression_p (t)
6202 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6204 error ("%<gang%> static expression must be integral");
6205 remove = true;
6207 else
6209 t = mark_rvalue_use (t);
6210 if (!processing_template_decl)
6212 t = maybe_constant_value (t);
6213 if (TREE_CODE (t) == INTEGER_CST
6214 && tree_int_cst_sgn (t) != 1
6215 && t != integer_minus_one_node)
6217 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6218 "%<gang%> static value must be "
6219 "positive");
6220 t = integer_one_node;
6223 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6225 OMP_CLAUSE_OPERAND (c, 1) = t;
6227 /* Check operand 0, the num argument. */
6228 /* FALLTHRU */
6230 case OMP_CLAUSE_WORKER:
6231 case OMP_CLAUSE_VECTOR:
6232 if (OMP_CLAUSE_OPERAND (c, 0) == NULL_TREE)
6233 break;
6234 /* FALLTHRU */
6236 case OMP_CLAUSE_NUM_TASKS:
6237 case OMP_CLAUSE_NUM_TEAMS:
6238 case OMP_CLAUSE_NUM_THREADS:
6239 case OMP_CLAUSE_NUM_GANGS:
6240 case OMP_CLAUSE_NUM_WORKERS:
6241 case OMP_CLAUSE_VECTOR_LENGTH:
6242 t = OMP_CLAUSE_OPERAND (c, 0);
6243 if (t == error_mark_node)
6244 remove = true;
6245 else if (!type_dependent_expression_p (t)
6246 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6248 switch (OMP_CLAUSE_CODE (c))
6250 case OMP_CLAUSE_GANG:
6251 error_at (OMP_CLAUSE_LOCATION (c),
6252 "%<gang%> num expression must be integral"); break;
6253 case OMP_CLAUSE_VECTOR:
6254 error_at (OMP_CLAUSE_LOCATION (c),
6255 "%<vector%> length expression must be integral");
6256 break;
6257 case OMP_CLAUSE_WORKER:
6258 error_at (OMP_CLAUSE_LOCATION (c),
6259 "%<worker%> num expression must be integral");
6260 break;
6261 default:
6262 error_at (OMP_CLAUSE_LOCATION (c),
6263 "%qs expression must be integral",
6264 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6266 remove = true;
6268 else
6270 t = mark_rvalue_use (t);
6271 if (!processing_template_decl)
6273 t = maybe_constant_value (t);
6274 if (TREE_CODE (t) == INTEGER_CST
6275 && tree_int_cst_sgn (t) != 1)
6277 switch (OMP_CLAUSE_CODE (c))
6279 case OMP_CLAUSE_GANG:
6280 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6281 "%<gang%> num value must be positive");
6282 break;
6283 case OMP_CLAUSE_VECTOR:
6284 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6285 "%<vector%> length value must be "
6286 "positive");
6287 break;
6288 case OMP_CLAUSE_WORKER:
6289 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6290 "%<worker%> num value must be "
6291 "positive");
6292 break;
6293 default:
6294 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6295 "%qs value must be positive",
6296 omp_clause_code_name
6297 [OMP_CLAUSE_CODE (c)]);
6299 t = integer_one_node;
6301 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6303 OMP_CLAUSE_OPERAND (c, 0) = t;
6305 break;
6307 case OMP_CLAUSE_SCHEDULE:
6308 if (OMP_CLAUSE_SCHEDULE_KIND (c) & OMP_CLAUSE_SCHEDULE_NONMONOTONIC)
6310 const char *p = NULL;
6311 switch (OMP_CLAUSE_SCHEDULE_KIND (c) & OMP_CLAUSE_SCHEDULE_MASK)
6313 case OMP_CLAUSE_SCHEDULE_STATIC: p = "static"; break;
6314 case OMP_CLAUSE_SCHEDULE_DYNAMIC: break;
6315 case OMP_CLAUSE_SCHEDULE_GUIDED: break;
6316 case OMP_CLAUSE_SCHEDULE_AUTO: p = "auto"; break;
6317 case OMP_CLAUSE_SCHEDULE_RUNTIME: p = "runtime"; break;
6318 default: gcc_unreachable ();
6320 if (p)
6322 error_at (OMP_CLAUSE_LOCATION (c),
6323 "%<nonmonotonic%> modifier specified for %qs "
6324 "schedule kind", p);
6325 OMP_CLAUSE_SCHEDULE_KIND (c)
6326 = (enum omp_clause_schedule_kind)
6327 (OMP_CLAUSE_SCHEDULE_KIND (c)
6328 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
6332 t = OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c);
6333 if (t == NULL)
6335 else if (t == error_mark_node)
6336 remove = true;
6337 else if (!type_dependent_expression_p (t)
6338 && (OMP_CLAUSE_SCHEDULE_KIND (c)
6339 != OMP_CLAUSE_SCHEDULE_CILKFOR)
6340 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6342 error ("schedule chunk size expression must be integral");
6343 remove = true;
6345 else
6347 t = mark_rvalue_use (t);
6348 if (!processing_template_decl)
6350 if (OMP_CLAUSE_SCHEDULE_KIND (c)
6351 == OMP_CLAUSE_SCHEDULE_CILKFOR)
6353 t = convert_to_integer (long_integer_type_node, t);
6354 if (t == error_mark_node)
6356 remove = true;
6357 break;
6360 else
6362 t = maybe_constant_value (t);
6363 if (TREE_CODE (t) == INTEGER_CST
6364 && tree_int_cst_sgn (t) != 1)
6366 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6367 "chunk size value must be positive");
6368 t = integer_one_node;
6371 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6373 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
6375 break;
6377 case OMP_CLAUSE_SIMDLEN:
6378 case OMP_CLAUSE_SAFELEN:
6379 t = OMP_CLAUSE_OPERAND (c, 0);
6380 if (t == error_mark_node)
6381 remove = true;
6382 else if (!type_dependent_expression_p (t)
6383 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6385 error ("%qs length expression must be integral",
6386 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6387 remove = true;
6389 else
6391 t = mark_rvalue_use (t);
6392 if (!processing_template_decl)
6394 t = maybe_constant_value (t);
6395 if (TREE_CODE (t) != INTEGER_CST
6396 || tree_int_cst_sgn (t) != 1)
6398 error ("%qs length expression must be positive constant"
6399 " integer expression",
6400 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6401 remove = true;
6404 OMP_CLAUSE_OPERAND (c, 0) = t;
6405 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SAFELEN)
6406 safelen = c;
6408 break;
6410 case OMP_CLAUSE_ASYNC:
6411 t = OMP_CLAUSE_ASYNC_EXPR (c);
6412 if (t == error_mark_node)
6413 remove = true;
6414 else if (!type_dependent_expression_p (t)
6415 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6417 error ("%<async%> expression must be integral");
6418 remove = true;
6420 else
6422 t = mark_rvalue_use (t);
6423 if (!processing_template_decl)
6424 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6425 OMP_CLAUSE_ASYNC_EXPR (c) = t;
6427 break;
6429 case OMP_CLAUSE_WAIT:
6430 t = OMP_CLAUSE_WAIT_EXPR (c);
6431 if (t == error_mark_node)
6432 remove = true;
6433 else if (!processing_template_decl)
6434 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6435 OMP_CLAUSE_WAIT_EXPR (c) = t;
6436 break;
6438 case OMP_CLAUSE_THREAD_LIMIT:
6439 t = OMP_CLAUSE_THREAD_LIMIT_EXPR (c);
6440 if (t == error_mark_node)
6441 remove = true;
6442 else if (!type_dependent_expression_p (t)
6443 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6445 error ("%<thread_limit%> expression must be integral");
6446 remove = true;
6448 else
6450 t = mark_rvalue_use (t);
6451 if (!processing_template_decl)
6453 t = maybe_constant_value (t);
6454 if (TREE_CODE (t) == INTEGER_CST
6455 && tree_int_cst_sgn (t) != 1)
6457 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6458 "%<thread_limit%> value must be positive");
6459 t = integer_one_node;
6461 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6463 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
6465 break;
6467 case OMP_CLAUSE_DEVICE:
6468 t = OMP_CLAUSE_DEVICE_ID (c);
6469 if (t == error_mark_node)
6470 remove = true;
6471 else if (!type_dependent_expression_p (t)
6472 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6474 error ("%<device%> id must be integral");
6475 remove = true;
6477 else
6479 t = mark_rvalue_use (t);
6480 if (!processing_template_decl)
6481 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6482 OMP_CLAUSE_DEVICE_ID (c) = t;
6484 break;
6486 case OMP_CLAUSE_DIST_SCHEDULE:
6487 t = OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c);
6488 if (t == NULL)
6490 else if (t == error_mark_node)
6491 remove = true;
6492 else if (!type_dependent_expression_p (t)
6493 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6495 error ("%<dist_schedule%> chunk size expression must be "
6496 "integral");
6497 remove = true;
6499 else
6501 t = mark_rvalue_use (t);
6502 if (!processing_template_decl)
6503 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6504 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
6506 break;
6508 case OMP_CLAUSE_ALIGNED:
6509 t = OMP_CLAUSE_DECL (c);
6510 if (t == current_class_ptr && ort != C_ORT_OMP_DECLARE_SIMD)
6512 error ("%<this%> allowed in OpenMP only in %<declare simd%>"
6513 " clauses");
6514 remove = true;
6515 break;
6517 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
6519 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
6520 break;
6521 if (DECL_P (t))
6522 error ("%qD is not a variable in %<aligned%> clause", t);
6523 else
6524 error ("%qE is not a variable in %<aligned%> clause", t);
6525 remove = true;
6527 else if (!type_dependent_expression_p (t)
6528 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE
6529 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE
6530 && (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE
6531 || (!POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (t)))
6532 && (TREE_CODE (TREE_TYPE (TREE_TYPE (t)))
6533 != ARRAY_TYPE))))
6535 error_at (OMP_CLAUSE_LOCATION (c),
6536 "%qE in %<aligned%> clause is neither a pointer nor "
6537 "an array nor a reference to pointer or array", t);
6538 remove = true;
6540 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
6542 error ("%qD appears more than once in %<aligned%> clauses", t);
6543 remove = true;
6545 else
6546 bitmap_set_bit (&aligned_head, DECL_UID (t));
6547 t = OMP_CLAUSE_ALIGNED_ALIGNMENT (c);
6548 if (t == error_mark_node)
6549 remove = true;
6550 else if (t == NULL_TREE)
6551 break;
6552 else if (!type_dependent_expression_p (t)
6553 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6555 error ("%<aligned%> clause alignment expression must "
6556 "be integral");
6557 remove = true;
6559 else
6561 t = mark_rvalue_use (t);
6562 if (!processing_template_decl)
6564 t = maybe_constant_value (t);
6565 if (TREE_CODE (t) != INTEGER_CST
6566 || tree_int_cst_sgn (t) != 1)
6568 error ("%<aligned%> clause alignment expression must be "
6569 "positive constant integer expression");
6570 remove = true;
6573 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = t;
6575 break;
6577 case OMP_CLAUSE_DEPEND:
6578 t = OMP_CLAUSE_DECL (c);
6579 if (t == NULL_TREE)
6581 gcc_assert (OMP_CLAUSE_DEPEND_KIND (c)
6582 == OMP_CLAUSE_DEPEND_SOURCE);
6583 break;
6585 if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SINK)
6587 if (cp_finish_omp_clause_depend_sink (c))
6588 remove = true;
6589 break;
6591 if (TREE_CODE (t) == TREE_LIST)
6593 if (handle_omp_array_sections (c, ort))
6594 remove = true;
6595 break;
6597 if (t == error_mark_node)
6598 remove = true;
6599 else if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
6601 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
6602 break;
6603 if (DECL_P (t))
6604 error ("%qD is not a variable in %<depend%> clause", t);
6605 else
6606 error ("%qE is not a variable in %<depend%> clause", t);
6607 remove = true;
6609 else if (t == current_class_ptr)
6611 error ("%<this%> allowed in OpenMP only in %<declare simd%>"
6612 " clauses");
6613 remove = true;
6615 else if (!processing_template_decl
6616 && !cxx_mark_addressable (t))
6617 remove = true;
6618 break;
6620 case OMP_CLAUSE_MAP:
6621 case OMP_CLAUSE_TO:
6622 case OMP_CLAUSE_FROM:
6623 case OMP_CLAUSE__CACHE_:
6624 t = OMP_CLAUSE_DECL (c);
6625 if (TREE_CODE (t) == TREE_LIST)
6627 if (handle_omp_array_sections (c, ort))
6628 remove = true;
6629 else
6631 t = OMP_CLAUSE_DECL (c);
6632 if (TREE_CODE (t) != TREE_LIST
6633 && !type_dependent_expression_p (t)
6634 && !cp_omp_mappable_type (TREE_TYPE (t)))
6636 error_at (OMP_CLAUSE_LOCATION (c),
6637 "array section does not have mappable type "
6638 "in %qs clause",
6639 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6640 remove = true;
6642 while (TREE_CODE (t) == ARRAY_REF)
6643 t = TREE_OPERAND (t, 0);
6644 if (TREE_CODE (t) == COMPONENT_REF
6645 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
6647 while (TREE_CODE (t) == COMPONENT_REF)
6648 t = TREE_OPERAND (t, 0);
6649 if (REFERENCE_REF_P (t))
6650 t = TREE_OPERAND (t, 0);
6651 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
6652 break;
6653 if (bitmap_bit_p (&map_head, DECL_UID (t)))
6655 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
6656 error ("%qD appears more than once in motion"
6657 " clauses", t);
6658 else if (ort == C_ORT_ACC)
6659 error ("%qD appears more than once in data"
6660 " clauses", t);
6661 else
6662 error ("%qD appears more than once in map"
6663 " clauses", t);
6664 remove = true;
6666 else
6668 bitmap_set_bit (&map_head, DECL_UID (t));
6669 bitmap_set_bit (&map_field_head, DECL_UID (t));
6673 break;
6675 if (t == error_mark_node)
6677 remove = true;
6678 break;
6680 if (REFERENCE_REF_P (t)
6681 && TREE_CODE (TREE_OPERAND (t, 0)) == COMPONENT_REF)
6683 t = TREE_OPERAND (t, 0);
6684 OMP_CLAUSE_DECL (c) = t;
6686 if (TREE_CODE (t) == COMPONENT_REF
6687 && (ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP
6688 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE__CACHE_)
6690 if (type_dependent_expression_p (t))
6691 break;
6692 if (TREE_CODE (TREE_OPERAND (t, 1)) == FIELD_DECL
6693 && DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
6695 error_at (OMP_CLAUSE_LOCATION (c),
6696 "bit-field %qE in %qs clause",
6697 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6698 remove = true;
6700 else if (!cp_omp_mappable_type (TREE_TYPE (t)))
6702 error_at (OMP_CLAUSE_LOCATION (c),
6703 "%qE does not have a mappable type in %qs clause",
6704 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6705 remove = true;
6707 while (TREE_CODE (t) == COMPONENT_REF)
6709 if (TREE_TYPE (TREE_OPERAND (t, 0))
6710 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0)))
6711 == UNION_TYPE))
6713 error_at (OMP_CLAUSE_LOCATION (c),
6714 "%qE is a member of a union", t);
6715 remove = true;
6716 break;
6718 t = TREE_OPERAND (t, 0);
6720 if (remove)
6721 break;
6722 if (REFERENCE_REF_P (t))
6723 t = TREE_OPERAND (t, 0);
6724 if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
6726 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
6727 goto handle_map_references;
6730 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
6732 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
6733 break;
6734 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
6735 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
6736 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ALWAYS_POINTER))
6737 break;
6738 if (DECL_P (t))
6739 error ("%qD is not a variable in %qs clause", t,
6740 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6741 else
6742 error ("%qE is not a variable in %qs clause", t,
6743 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6744 remove = true;
6746 else if (VAR_P (t) && CP_DECL_THREAD_LOCAL_P (t))
6748 error ("%qD is threadprivate variable in %qs clause", t,
6749 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6750 remove = true;
6752 else if (ort != C_ORT_ACC && t == current_class_ptr)
6754 error ("%<this%> allowed in OpenMP only in %<declare simd%>"
6755 " clauses");
6756 remove = true;
6757 break;
6759 else if (!processing_template_decl
6760 && TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE
6761 && (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
6762 || (OMP_CLAUSE_MAP_KIND (c)
6763 != GOMP_MAP_FIRSTPRIVATE_POINTER))
6764 && !cxx_mark_addressable (t))
6765 remove = true;
6766 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
6767 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
6768 || (OMP_CLAUSE_MAP_KIND (c)
6769 == GOMP_MAP_FIRSTPRIVATE_POINTER)))
6770 && t == OMP_CLAUSE_DECL (c)
6771 && !type_dependent_expression_p (t)
6772 && !cp_omp_mappable_type ((TREE_CODE (TREE_TYPE (t))
6773 == REFERENCE_TYPE)
6774 ? TREE_TYPE (TREE_TYPE (t))
6775 : TREE_TYPE (t)))
6777 error_at (OMP_CLAUSE_LOCATION (c),
6778 "%qD does not have a mappable type in %qs clause", t,
6779 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6780 remove = true;
6782 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
6783 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FORCE_DEVICEPTR
6784 && !type_dependent_expression_p (t)
6785 && !POINTER_TYPE_P (TREE_TYPE (t)))
6787 error ("%qD is not a pointer variable", t);
6788 remove = true;
6790 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
6791 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
6793 if (bitmap_bit_p (&generic_head, DECL_UID (t))
6794 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
6796 error ("%qD appears more than once in data clauses", t);
6797 remove = true;
6799 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
6801 if (ort == C_ORT_ACC)
6802 error ("%qD appears more than once in data clauses", t);
6803 else
6804 error ("%qD appears both in data and map clauses", t);
6805 remove = true;
6807 else
6808 bitmap_set_bit (&generic_head, DECL_UID (t));
6810 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
6812 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
6813 error ("%qD appears more than once in motion clauses", t);
6814 if (ort == C_ORT_ACC)
6815 error ("%qD appears more than once in data clauses", t);
6816 else
6817 error ("%qD appears more than once in map clauses", t);
6818 remove = true;
6820 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
6821 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
6823 if (ort == C_ORT_ACC)
6824 error ("%qD appears more than once in data clauses", t);
6825 else
6826 error ("%qD appears both in data and map clauses", t);
6827 remove = true;
6829 else
6831 bitmap_set_bit (&map_head, DECL_UID (t));
6832 if (t != OMP_CLAUSE_DECL (c)
6833 && TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPONENT_REF)
6834 bitmap_set_bit (&map_field_head, DECL_UID (t));
6836 handle_map_references:
6837 if (!remove
6838 && !processing_template_decl
6839 && (ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP
6840 && TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c))) == REFERENCE_TYPE)
6842 t = OMP_CLAUSE_DECL (c);
6843 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
6845 OMP_CLAUSE_DECL (c) = build_simple_mem_ref (t);
6846 if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
6847 OMP_CLAUSE_SIZE (c)
6848 = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t)));
6850 else if (OMP_CLAUSE_MAP_KIND (c)
6851 != GOMP_MAP_FIRSTPRIVATE_POINTER
6852 && (OMP_CLAUSE_MAP_KIND (c)
6853 != GOMP_MAP_FIRSTPRIVATE_REFERENCE)
6854 && (OMP_CLAUSE_MAP_KIND (c)
6855 != GOMP_MAP_ALWAYS_POINTER))
6857 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
6858 OMP_CLAUSE_MAP);
6859 if (TREE_CODE (t) == COMPONENT_REF)
6860 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ALWAYS_POINTER);
6861 else
6862 OMP_CLAUSE_SET_MAP_KIND (c2,
6863 GOMP_MAP_FIRSTPRIVATE_REFERENCE);
6864 OMP_CLAUSE_DECL (c2) = t;
6865 OMP_CLAUSE_SIZE (c2) = size_zero_node;
6866 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
6867 OMP_CLAUSE_CHAIN (c) = c2;
6868 OMP_CLAUSE_DECL (c) = build_simple_mem_ref (t);
6869 if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
6870 OMP_CLAUSE_SIZE (c)
6871 = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t)));
6872 c = c2;
6875 break;
6877 case OMP_CLAUSE_TO_DECLARE:
6878 case OMP_CLAUSE_LINK:
6879 t = OMP_CLAUSE_DECL (c);
6880 if (TREE_CODE (t) == FUNCTION_DECL
6881 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
6883 else if (!VAR_P (t))
6885 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
6887 if (TREE_CODE (t) == TEMPLATE_ID_EXPR)
6888 error_at (OMP_CLAUSE_LOCATION (c),
6889 "template %qE in clause %qs", t,
6890 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6891 else if (really_overloaded_fn (t))
6892 error_at (OMP_CLAUSE_LOCATION (c),
6893 "overloaded function name %qE in clause %qs", t,
6894 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6895 else
6896 error_at (OMP_CLAUSE_LOCATION (c),
6897 "%qE is neither a variable nor a function name "
6898 "in clause %qs", t,
6899 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6901 else
6902 error_at (OMP_CLAUSE_LOCATION (c),
6903 "%qE is not a variable in clause %qs", t,
6904 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6905 remove = true;
6907 else if (DECL_THREAD_LOCAL_P (t))
6909 error_at (OMP_CLAUSE_LOCATION (c),
6910 "%qD is threadprivate variable in %qs clause", t,
6911 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6912 remove = true;
6914 else if (!cp_omp_mappable_type (TREE_TYPE (t)))
6916 error_at (OMP_CLAUSE_LOCATION (c),
6917 "%qD does not have a mappable type in %qs clause", t,
6918 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6919 remove = true;
6921 if (remove)
6922 break;
6923 if (bitmap_bit_p (&generic_head, DECL_UID (t)))
6925 error_at (OMP_CLAUSE_LOCATION (c),
6926 "%qE appears more than once on the same "
6927 "%<declare target%> directive", t);
6928 remove = true;
6930 else
6931 bitmap_set_bit (&generic_head, DECL_UID (t));
6932 break;
6934 case OMP_CLAUSE_UNIFORM:
6935 t = OMP_CLAUSE_DECL (c);
6936 if (TREE_CODE (t) != PARM_DECL)
6938 if (processing_template_decl)
6939 break;
6940 if (DECL_P (t))
6941 error ("%qD is not an argument in %<uniform%> clause", t);
6942 else
6943 error ("%qE is not an argument in %<uniform%> clause", t);
6944 remove = true;
6945 break;
6947 /* map_head bitmap is used as uniform_head if declare_simd. */
6948 bitmap_set_bit (&map_head, DECL_UID (t));
6949 goto check_dup_generic;
6951 case OMP_CLAUSE_GRAINSIZE:
6952 t = OMP_CLAUSE_GRAINSIZE_EXPR (c);
6953 if (t == error_mark_node)
6954 remove = true;
6955 else if (!type_dependent_expression_p (t)
6956 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6958 error ("%<grainsize%> expression must be integral");
6959 remove = true;
6961 else
6963 t = mark_rvalue_use (t);
6964 if (!processing_template_decl)
6966 t = maybe_constant_value (t);
6967 if (TREE_CODE (t) == INTEGER_CST
6968 && tree_int_cst_sgn (t) != 1)
6970 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6971 "%<grainsize%> value must be positive");
6972 t = integer_one_node;
6974 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6976 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
6978 break;
6980 case OMP_CLAUSE_PRIORITY:
6981 t = OMP_CLAUSE_PRIORITY_EXPR (c);
6982 if (t == error_mark_node)
6983 remove = true;
6984 else if (!type_dependent_expression_p (t)
6985 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6987 error ("%<priority%> expression must be integral");
6988 remove = true;
6990 else
6992 t = mark_rvalue_use (t);
6993 if (!processing_template_decl)
6995 t = maybe_constant_value (t);
6996 if (TREE_CODE (t) == INTEGER_CST
6997 && tree_int_cst_sgn (t) == -1)
6999 warning_at (OMP_CLAUSE_LOCATION (c), 0,
7000 "%<priority%> value must be non-negative");
7001 t = integer_one_node;
7003 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7005 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
7007 break;
7009 case OMP_CLAUSE_HINT:
7010 t = OMP_CLAUSE_HINT_EXPR (c);
7011 if (t == error_mark_node)
7012 remove = true;
7013 else if (!type_dependent_expression_p (t)
7014 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
7016 error ("%<num_tasks%> expression must be integral");
7017 remove = true;
7019 else
7021 t = mark_rvalue_use (t);
7022 if (!processing_template_decl)
7024 t = maybe_constant_value (t);
7025 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7027 OMP_CLAUSE_HINT_EXPR (c) = t;
7029 break;
7031 case OMP_CLAUSE_IS_DEVICE_PTR:
7032 case OMP_CLAUSE_USE_DEVICE_PTR:
7033 field_ok = (ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP;
7034 t = OMP_CLAUSE_DECL (c);
7035 if (!type_dependent_expression_p (t))
7037 tree type = TREE_TYPE (t);
7038 if (TREE_CODE (type) != POINTER_TYPE
7039 && TREE_CODE (type) != ARRAY_TYPE
7040 && (TREE_CODE (type) != REFERENCE_TYPE
7041 || (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
7042 && TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE)))
7044 error_at (OMP_CLAUSE_LOCATION (c),
7045 "%qs variable is neither a pointer, nor an array "
7046 "nor reference to pointer or array",
7047 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7048 remove = true;
7051 goto check_dup_generic;
7053 case OMP_CLAUSE_NOWAIT:
7054 case OMP_CLAUSE_DEFAULT:
7055 case OMP_CLAUSE_UNTIED:
7056 case OMP_CLAUSE_COLLAPSE:
7057 case OMP_CLAUSE_MERGEABLE:
7058 case OMP_CLAUSE_PARALLEL:
7059 case OMP_CLAUSE_FOR:
7060 case OMP_CLAUSE_SECTIONS:
7061 case OMP_CLAUSE_TASKGROUP:
7062 case OMP_CLAUSE_PROC_BIND:
7063 case OMP_CLAUSE_NOGROUP:
7064 case OMP_CLAUSE_THREADS:
7065 case OMP_CLAUSE_SIMD:
7066 case OMP_CLAUSE_DEFAULTMAP:
7067 case OMP_CLAUSE__CILK_FOR_COUNT_:
7068 case OMP_CLAUSE_AUTO:
7069 case OMP_CLAUSE_INDEPENDENT:
7070 case OMP_CLAUSE_SEQ:
7071 break;
7073 case OMP_CLAUSE_TILE:
7074 for (tree list = OMP_CLAUSE_TILE_LIST (c); !remove && list;
7075 list = TREE_CHAIN (list))
7077 t = TREE_VALUE (list);
7079 if (t == error_mark_node)
7080 remove = true;
7081 else if (!type_dependent_expression_p (t)
7082 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
7084 error_at (OMP_CLAUSE_LOCATION (c),
7085 "%<tile%> argument needs integral type");
7086 remove = true;
7088 else
7090 t = mark_rvalue_use (t);
7091 if (!processing_template_decl)
7093 /* Zero is used to indicate '*', we permit you
7094 to get there via an ICE of value zero. */
7095 t = maybe_constant_value (t);
7096 if (!tree_fits_shwi_p (t)
7097 || tree_to_shwi (t) < 0)
7099 error_at (OMP_CLAUSE_LOCATION (c),
7100 "%<tile%> argument needs positive "
7101 "integral constant");
7102 remove = true;
7105 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7108 /* Update list item. */
7109 TREE_VALUE (list) = t;
7111 break;
7113 case OMP_CLAUSE_ORDERED:
7114 ordered_seen = true;
7115 break;
7117 case OMP_CLAUSE_INBRANCH:
7118 case OMP_CLAUSE_NOTINBRANCH:
7119 if (branch_seen)
7121 error ("%<inbranch%> clause is incompatible with "
7122 "%<notinbranch%>");
7123 remove = true;
7125 branch_seen = true;
7126 break;
7128 default:
7129 gcc_unreachable ();
7132 if (remove)
7133 *pc = OMP_CLAUSE_CHAIN (c);
7134 else
7135 pc = &OMP_CLAUSE_CHAIN (c);
7138 for (pc = &clauses, c = clauses; c ; c = *pc)
7140 enum omp_clause_code c_kind = OMP_CLAUSE_CODE (c);
7141 bool remove = false;
7142 bool need_complete_type = false;
7143 bool need_default_ctor = false;
7144 bool need_copy_ctor = false;
7145 bool need_copy_assignment = false;
7146 bool need_implicitly_determined = false;
7147 bool need_dtor = false;
7148 tree type, inner_type;
7150 switch (c_kind)
7152 case OMP_CLAUSE_SHARED:
7153 need_implicitly_determined = true;
7154 break;
7155 case OMP_CLAUSE_PRIVATE:
7156 need_complete_type = true;
7157 need_default_ctor = true;
7158 need_dtor = true;
7159 need_implicitly_determined = true;
7160 break;
7161 case OMP_CLAUSE_FIRSTPRIVATE:
7162 need_complete_type = true;
7163 need_copy_ctor = true;
7164 need_dtor = true;
7165 need_implicitly_determined = true;
7166 break;
7167 case OMP_CLAUSE_LASTPRIVATE:
7168 need_complete_type = true;
7169 need_copy_assignment = true;
7170 need_implicitly_determined = true;
7171 break;
7172 case OMP_CLAUSE_REDUCTION:
7173 need_implicitly_determined = true;
7174 break;
7175 case OMP_CLAUSE_LINEAR:
7176 if (ort != C_ORT_OMP_DECLARE_SIMD)
7177 need_implicitly_determined = true;
7178 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c)
7179 && !bitmap_bit_p (&map_head,
7180 DECL_UID (OMP_CLAUSE_LINEAR_STEP (c))))
7182 error_at (OMP_CLAUSE_LOCATION (c),
7183 "%<linear%> clause step is a parameter %qD not "
7184 "specified in %<uniform%> clause",
7185 OMP_CLAUSE_LINEAR_STEP (c));
7186 *pc = OMP_CLAUSE_CHAIN (c);
7187 continue;
7189 break;
7190 case OMP_CLAUSE_COPYPRIVATE:
7191 need_copy_assignment = true;
7192 break;
7193 case OMP_CLAUSE_COPYIN:
7194 need_copy_assignment = true;
7195 break;
7196 case OMP_CLAUSE_SIMDLEN:
7197 if (safelen
7198 && !processing_template_decl
7199 && tree_int_cst_lt (OMP_CLAUSE_SAFELEN_EXPR (safelen),
7200 OMP_CLAUSE_SIMDLEN_EXPR (c)))
7202 error_at (OMP_CLAUSE_LOCATION (c),
7203 "%<simdlen%> clause value is bigger than "
7204 "%<safelen%> clause value");
7205 OMP_CLAUSE_SIMDLEN_EXPR (c)
7206 = OMP_CLAUSE_SAFELEN_EXPR (safelen);
7208 pc = &OMP_CLAUSE_CHAIN (c);
7209 continue;
7210 case OMP_CLAUSE_SCHEDULE:
7211 if (ordered_seen
7212 && (OMP_CLAUSE_SCHEDULE_KIND (c)
7213 & OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
7215 error_at (OMP_CLAUSE_LOCATION (c),
7216 "%<nonmonotonic%> schedule modifier specified "
7217 "together with %<ordered%> clause");
7218 OMP_CLAUSE_SCHEDULE_KIND (c)
7219 = (enum omp_clause_schedule_kind)
7220 (OMP_CLAUSE_SCHEDULE_KIND (c)
7221 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
7223 pc = &OMP_CLAUSE_CHAIN (c);
7224 continue;
7225 case OMP_CLAUSE_NOWAIT:
7226 if (copyprivate_seen)
7228 error_at (OMP_CLAUSE_LOCATION (c),
7229 "%<nowait%> clause must not be used together "
7230 "with %<copyprivate%>");
7231 *pc = OMP_CLAUSE_CHAIN (c);
7232 continue;
7234 /* FALLTHRU */
7235 default:
7236 pc = &OMP_CLAUSE_CHAIN (c);
7237 continue;
7240 t = OMP_CLAUSE_DECL (c);
7241 if (processing_template_decl
7242 && !VAR_P (t) && TREE_CODE (t) != PARM_DECL)
7244 pc = &OMP_CLAUSE_CHAIN (c);
7245 continue;
7248 switch (c_kind)
7250 case OMP_CLAUSE_LASTPRIVATE:
7251 if (!bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
7253 need_default_ctor = true;
7254 need_dtor = true;
7256 break;
7258 case OMP_CLAUSE_REDUCTION:
7259 if (finish_omp_reduction_clause (c, &need_default_ctor,
7260 &need_dtor))
7261 remove = true;
7262 else
7263 t = OMP_CLAUSE_DECL (c);
7264 break;
7266 case OMP_CLAUSE_COPYIN:
7267 if (!VAR_P (t) || !CP_DECL_THREAD_LOCAL_P (t))
7269 error ("%qE must be %<threadprivate%> for %<copyin%>", t);
7270 remove = true;
7272 break;
7274 default:
7275 break;
7278 if (need_complete_type || need_copy_assignment)
7280 t = require_complete_type (t);
7281 if (t == error_mark_node)
7282 remove = true;
7283 else if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE
7284 && !complete_type_or_else (TREE_TYPE (TREE_TYPE (t)), t))
7285 remove = true;
7287 if (need_implicitly_determined)
7289 const char *share_name = NULL;
7291 if (VAR_P (t) && CP_DECL_THREAD_LOCAL_P (t))
7292 share_name = "threadprivate";
7293 else switch (cxx_omp_predetermined_sharing (t))
7295 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
7296 break;
7297 case OMP_CLAUSE_DEFAULT_SHARED:
7298 /* const vars may be specified in firstprivate clause. */
7299 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
7300 && cxx_omp_const_qual_no_mutable (t))
7301 break;
7302 share_name = "shared";
7303 break;
7304 case OMP_CLAUSE_DEFAULT_PRIVATE:
7305 share_name = "private";
7306 break;
7307 default:
7308 gcc_unreachable ();
7310 if (share_name)
7312 error ("%qE is predetermined %qs for %qs",
7313 omp_clause_printable_decl (t), share_name,
7314 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7315 remove = true;
7319 /* We're interested in the base element, not arrays. */
7320 inner_type = type = TREE_TYPE (t);
7321 if ((need_complete_type
7322 || need_copy_assignment
7323 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
7324 && TREE_CODE (inner_type) == REFERENCE_TYPE)
7325 inner_type = TREE_TYPE (inner_type);
7326 while (TREE_CODE (inner_type) == ARRAY_TYPE)
7327 inner_type = TREE_TYPE (inner_type);
7329 /* Check for special function availability by building a call to one.
7330 Save the results, because later we won't be in the right context
7331 for making these queries. */
7332 if (CLASS_TYPE_P (inner_type)
7333 && COMPLETE_TYPE_P (inner_type)
7334 && (need_default_ctor || need_copy_ctor
7335 || need_copy_assignment || need_dtor)
7336 && !type_dependent_expression_p (t)
7337 && cxx_omp_create_clause_info (c, inner_type, need_default_ctor,
7338 need_copy_ctor, need_copy_assignment,
7339 need_dtor))
7340 remove = true;
7342 if (!remove
7343 && c_kind == OMP_CLAUSE_SHARED
7344 && processing_template_decl)
7346 t = omp_clause_decl_field (OMP_CLAUSE_DECL (c));
7347 if (t)
7348 OMP_CLAUSE_DECL (c) = t;
7351 if (remove)
7352 *pc = OMP_CLAUSE_CHAIN (c);
7353 else
7354 pc = &OMP_CLAUSE_CHAIN (c);
7357 bitmap_obstack_release (NULL);
7358 return clauses;
7361 /* Start processing OpenMP clauses that can include any
7362 privatization clauses for non-static data members. */
7364 tree
7365 push_omp_privatization_clauses (bool ignore_next)
7367 if (omp_private_member_ignore_next)
7369 omp_private_member_ignore_next = ignore_next;
7370 return NULL_TREE;
7372 omp_private_member_ignore_next = ignore_next;
7373 if (omp_private_member_map)
7374 omp_private_member_vec.safe_push (error_mark_node);
7375 return push_stmt_list ();
7378 /* Revert remapping of any non-static data members since
7379 the last push_omp_privatization_clauses () call. */
7381 void
7382 pop_omp_privatization_clauses (tree stmt)
7384 if (stmt == NULL_TREE)
7385 return;
7386 stmt = pop_stmt_list (stmt);
7387 if (omp_private_member_map)
7389 while (!omp_private_member_vec.is_empty ())
7391 tree t = omp_private_member_vec.pop ();
7392 if (t == error_mark_node)
7394 add_stmt (stmt);
7395 return;
7397 bool no_decl_expr = t == integer_zero_node;
7398 if (no_decl_expr)
7399 t = omp_private_member_vec.pop ();
7400 tree *v = omp_private_member_map->get (t);
7401 gcc_assert (v);
7402 if (!no_decl_expr)
7403 add_decl_expr (*v);
7404 omp_private_member_map->remove (t);
7406 delete omp_private_member_map;
7407 omp_private_member_map = NULL;
7409 add_stmt (stmt);
7412 /* Remember OpenMP privatization clauses mapping and clear it.
7413 Used for lambdas. */
7415 void
7416 save_omp_privatization_clauses (vec<tree> &save)
7418 save = vNULL;
7419 if (omp_private_member_ignore_next)
7420 save.safe_push (integer_one_node);
7421 omp_private_member_ignore_next = false;
7422 if (!omp_private_member_map)
7423 return;
7425 while (!omp_private_member_vec.is_empty ())
7427 tree t = omp_private_member_vec.pop ();
7428 if (t == error_mark_node)
7430 save.safe_push (t);
7431 continue;
7433 tree n = t;
7434 if (t == integer_zero_node)
7435 t = omp_private_member_vec.pop ();
7436 tree *v = omp_private_member_map->get (t);
7437 gcc_assert (v);
7438 save.safe_push (*v);
7439 save.safe_push (t);
7440 if (n != t)
7441 save.safe_push (n);
7443 delete omp_private_member_map;
7444 omp_private_member_map = NULL;
7447 /* Restore OpenMP privatization clauses mapping saved by the
7448 above function. */
7450 void
7451 restore_omp_privatization_clauses (vec<tree> &save)
7453 gcc_assert (omp_private_member_vec.is_empty ());
7454 omp_private_member_ignore_next = false;
7455 if (save.is_empty ())
7456 return;
7457 if (save.length () == 1 && save[0] == integer_one_node)
7459 omp_private_member_ignore_next = true;
7460 save.release ();
7461 return;
7464 omp_private_member_map = new hash_map <tree, tree>;
7465 while (!save.is_empty ())
7467 tree t = save.pop ();
7468 tree n = t;
7469 if (t != error_mark_node)
7471 if (t == integer_one_node)
7473 omp_private_member_ignore_next = true;
7474 gcc_assert (save.is_empty ());
7475 break;
7477 if (t == integer_zero_node)
7478 t = save.pop ();
7479 tree &v = omp_private_member_map->get_or_insert (t);
7480 v = save.pop ();
7482 omp_private_member_vec.safe_push (t);
7483 if (n != t)
7484 omp_private_member_vec.safe_push (n);
7486 save.release ();
7489 /* For all variables in the tree_list VARS, mark them as thread local. */
7491 void
7492 finish_omp_threadprivate (tree vars)
7494 tree t;
7496 /* Mark every variable in VARS to be assigned thread local storage. */
7497 for (t = vars; t; t = TREE_CHAIN (t))
7499 tree v = TREE_PURPOSE (t);
7501 if (error_operand_p (v))
7503 else if (!VAR_P (v))
7504 error ("%<threadprivate%> %qD is not file, namespace "
7505 "or block scope variable", v);
7506 /* If V had already been marked threadprivate, it doesn't matter
7507 whether it had been used prior to this point. */
7508 else if (TREE_USED (v)
7509 && (DECL_LANG_SPECIFIC (v) == NULL
7510 || !CP_DECL_THREADPRIVATE_P (v)))
7511 error ("%qE declared %<threadprivate%> after first use", v);
7512 else if (! TREE_STATIC (v) && ! DECL_EXTERNAL (v))
7513 error ("automatic variable %qE cannot be %<threadprivate%>", v);
7514 else if (! COMPLETE_TYPE_P (complete_type (TREE_TYPE (v))))
7515 error ("%<threadprivate%> %qE has incomplete type", v);
7516 else if (TREE_STATIC (v) && TYPE_P (CP_DECL_CONTEXT (v))
7517 && CP_DECL_CONTEXT (v) != current_class_type)
7518 error ("%<threadprivate%> %qE directive not "
7519 "in %qT definition", v, CP_DECL_CONTEXT (v));
7520 else
7522 /* Allocate a LANG_SPECIFIC structure for V, if needed. */
7523 if (DECL_LANG_SPECIFIC (v) == NULL)
7525 retrofit_lang_decl (v);
7527 /* Make sure that DECL_DISCRIMINATOR_P continues to be true
7528 after the allocation of the lang_decl structure. */
7529 if (DECL_DISCRIMINATOR_P (v))
7530 DECL_LANG_SPECIFIC (v)->u.base.u2sel = 1;
7533 if (! CP_DECL_THREAD_LOCAL_P (v))
7535 CP_DECL_THREAD_LOCAL_P (v) = true;
7536 set_decl_tls_model (v, decl_default_tls_model (v));
7537 /* If rtl has been already set for this var, call
7538 make_decl_rtl once again, so that encode_section_info
7539 has a chance to look at the new decl flags. */
7540 if (DECL_RTL_SET_P (v))
7541 make_decl_rtl (v);
7543 CP_DECL_THREADPRIVATE_P (v) = 1;
7548 /* Build an OpenMP structured block. */
7550 tree
7551 begin_omp_structured_block (void)
7553 return do_pushlevel (sk_omp);
7556 tree
7557 finish_omp_structured_block (tree block)
7559 return do_poplevel (block);
7562 /* Similarly, except force the retention of the BLOCK. */
7564 tree
7565 begin_omp_parallel (void)
7567 keep_next_level (true);
7568 return begin_omp_structured_block ();
7571 /* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
7572 statement. */
7574 tree
7575 finish_oacc_data (tree clauses, tree block)
7577 tree stmt;
7579 block = finish_omp_structured_block (block);
7581 stmt = make_node (OACC_DATA);
7582 TREE_TYPE (stmt) = void_type_node;
7583 OACC_DATA_CLAUSES (stmt) = clauses;
7584 OACC_DATA_BODY (stmt) = block;
7586 return add_stmt (stmt);
7589 /* Generate OACC_HOST_DATA, with CLAUSES and BLOCK as its compound
7590 statement. */
7592 tree
7593 finish_oacc_host_data (tree clauses, tree block)
7595 tree stmt;
7597 block = finish_omp_structured_block (block);
7599 stmt = make_node (OACC_HOST_DATA);
7600 TREE_TYPE (stmt) = void_type_node;
7601 OACC_HOST_DATA_CLAUSES (stmt) = clauses;
7602 OACC_HOST_DATA_BODY (stmt) = block;
7604 return add_stmt (stmt);
7607 /* Generate OMP construct CODE, with BODY and CLAUSES as its compound
7608 statement. */
7610 tree
7611 finish_omp_construct (enum tree_code code, tree body, tree clauses)
7613 body = finish_omp_structured_block (body);
7615 tree stmt = make_node (code);
7616 TREE_TYPE (stmt) = void_type_node;
7617 OMP_BODY (stmt) = body;
7618 OMP_CLAUSES (stmt) = clauses;
7620 return add_stmt (stmt);
7623 tree
7624 finish_omp_parallel (tree clauses, tree body)
7626 tree stmt;
7628 body = finish_omp_structured_block (body);
7630 stmt = make_node (OMP_PARALLEL);
7631 TREE_TYPE (stmt) = void_type_node;
7632 OMP_PARALLEL_CLAUSES (stmt) = clauses;
7633 OMP_PARALLEL_BODY (stmt) = body;
7635 return add_stmt (stmt);
7638 tree
7639 begin_omp_task (void)
7641 keep_next_level (true);
7642 return begin_omp_structured_block ();
7645 tree
7646 finish_omp_task (tree clauses, tree body)
7648 tree stmt;
7650 body = finish_omp_structured_block (body);
7652 stmt = make_node (OMP_TASK);
7653 TREE_TYPE (stmt) = void_type_node;
7654 OMP_TASK_CLAUSES (stmt) = clauses;
7655 OMP_TASK_BODY (stmt) = body;
7657 return add_stmt (stmt);
7660 /* Helper function for finish_omp_for. Convert Ith random access iterator
7661 into integral iterator. Return FALSE if successful. */
7663 static bool
7664 handle_omp_for_class_iterator (int i, location_t locus, enum tree_code code,
7665 tree declv, tree orig_declv, tree initv,
7666 tree condv, tree incrv, tree *body,
7667 tree *pre_body, tree &clauses, tree *lastp,
7668 int collapse, int ordered)
7670 tree diff, iter_init, iter_incr = NULL, last;
7671 tree incr_var = NULL, orig_pre_body, orig_body, c;
7672 tree decl = TREE_VEC_ELT (declv, i);
7673 tree init = TREE_VEC_ELT (initv, i);
7674 tree cond = TREE_VEC_ELT (condv, i);
7675 tree incr = TREE_VEC_ELT (incrv, i);
7676 tree iter = decl;
7677 location_t elocus = locus;
7679 if (init && EXPR_HAS_LOCATION (init))
7680 elocus = EXPR_LOCATION (init);
7682 cond = cp_fully_fold (cond);
7683 switch (TREE_CODE (cond))
7685 case GT_EXPR:
7686 case GE_EXPR:
7687 case LT_EXPR:
7688 case LE_EXPR:
7689 case NE_EXPR:
7690 if (TREE_OPERAND (cond, 1) == iter)
7691 cond = build2 (swap_tree_comparison (TREE_CODE (cond)),
7692 TREE_TYPE (cond), iter, TREE_OPERAND (cond, 0));
7693 if (TREE_OPERAND (cond, 0) != iter)
7694 cond = error_mark_node;
7695 else
7697 tree tem = build_x_binary_op (EXPR_LOCATION (cond),
7698 TREE_CODE (cond),
7699 iter, ERROR_MARK,
7700 TREE_OPERAND (cond, 1), ERROR_MARK,
7701 NULL, tf_warning_or_error);
7702 if (error_operand_p (tem))
7703 return true;
7705 break;
7706 default:
7707 cond = error_mark_node;
7708 break;
7710 if (cond == error_mark_node)
7712 error_at (elocus, "invalid controlling predicate");
7713 return true;
7715 diff = build_x_binary_op (elocus, MINUS_EXPR, TREE_OPERAND (cond, 1),
7716 ERROR_MARK, iter, ERROR_MARK, NULL,
7717 tf_warning_or_error);
7718 diff = cp_fully_fold (diff);
7719 if (error_operand_p (diff))
7720 return true;
7721 if (TREE_CODE (TREE_TYPE (diff)) != INTEGER_TYPE)
7723 error_at (elocus, "difference between %qE and %qD does not have integer type",
7724 TREE_OPERAND (cond, 1), iter);
7725 return true;
7727 if (!c_omp_check_loop_iv_exprs (locus, orig_declv,
7728 TREE_VEC_ELT (declv, i), NULL_TREE,
7729 cond, cp_walk_subtrees))
7730 return true;
7732 switch (TREE_CODE (incr))
7734 case PREINCREMENT_EXPR:
7735 case PREDECREMENT_EXPR:
7736 case POSTINCREMENT_EXPR:
7737 case POSTDECREMENT_EXPR:
7738 if (TREE_OPERAND (incr, 0) != iter)
7740 incr = error_mark_node;
7741 break;
7743 iter_incr = build_x_unary_op (EXPR_LOCATION (incr),
7744 TREE_CODE (incr), iter,
7745 tf_warning_or_error);
7746 if (error_operand_p (iter_incr))
7747 return true;
7748 else if (TREE_CODE (incr) == PREINCREMENT_EXPR
7749 || TREE_CODE (incr) == POSTINCREMENT_EXPR)
7750 incr = integer_one_node;
7751 else
7752 incr = integer_minus_one_node;
7753 break;
7754 case MODIFY_EXPR:
7755 if (TREE_OPERAND (incr, 0) != iter)
7756 incr = error_mark_node;
7757 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
7758 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
7760 tree rhs = TREE_OPERAND (incr, 1);
7761 if (TREE_OPERAND (rhs, 0) == iter)
7763 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (rhs, 1)))
7764 != INTEGER_TYPE)
7765 incr = error_mark_node;
7766 else
7768 iter_incr = build_x_modify_expr (EXPR_LOCATION (rhs),
7769 iter, TREE_CODE (rhs),
7770 TREE_OPERAND (rhs, 1),
7771 tf_warning_or_error);
7772 if (error_operand_p (iter_incr))
7773 return true;
7774 incr = TREE_OPERAND (rhs, 1);
7775 incr = cp_convert (TREE_TYPE (diff), incr,
7776 tf_warning_or_error);
7777 if (TREE_CODE (rhs) == MINUS_EXPR)
7779 incr = build1 (NEGATE_EXPR, TREE_TYPE (diff), incr);
7780 incr = fold_simple (incr);
7782 if (TREE_CODE (incr) != INTEGER_CST
7783 && (TREE_CODE (incr) != NOP_EXPR
7784 || (TREE_CODE (TREE_OPERAND (incr, 0))
7785 != INTEGER_CST)))
7786 iter_incr = NULL;
7789 else if (TREE_OPERAND (rhs, 1) == iter)
7791 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (rhs, 0))) != INTEGER_TYPE
7792 || TREE_CODE (rhs) != PLUS_EXPR)
7793 incr = error_mark_node;
7794 else
7796 iter_incr = build_x_binary_op (EXPR_LOCATION (rhs),
7797 PLUS_EXPR,
7798 TREE_OPERAND (rhs, 0),
7799 ERROR_MARK, iter,
7800 ERROR_MARK, NULL,
7801 tf_warning_or_error);
7802 if (error_operand_p (iter_incr))
7803 return true;
7804 iter_incr = build_x_modify_expr (EXPR_LOCATION (rhs),
7805 iter, NOP_EXPR,
7806 iter_incr,
7807 tf_warning_or_error);
7808 if (error_operand_p (iter_incr))
7809 return true;
7810 incr = TREE_OPERAND (rhs, 0);
7811 iter_incr = NULL;
7814 else
7815 incr = error_mark_node;
7817 else
7818 incr = error_mark_node;
7819 break;
7820 default:
7821 incr = error_mark_node;
7822 break;
7825 if (incr == error_mark_node)
7827 error_at (elocus, "invalid increment expression");
7828 return true;
7831 incr = cp_convert (TREE_TYPE (diff), incr, tf_warning_or_error);
7832 bool taskloop_iv_seen = false;
7833 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
7834 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
7835 && OMP_CLAUSE_DECL (c) == iter)
7837 if (code == OMP_TASKLOOP)
7839 taskloop_iv_seen = true;
7840 OMP_CLAUSE_LASTPRIVATE_TASKLOOP_IV (c) = 1;
7842 break;
7844 else if (code == OMP_TASKLOOP
7845 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
7846 && OMP_CLAUSE_DECL (c) == iter)
7848 taskloop_iv_seen = true;
7849 OMP_CLAUSE_PRIVATE_TASKLOOP_IV (c) = 1;
7852 decl = create_temporary_var (TREE_TYPE (diff));
7853 pushdecl (decl);
7854 add_decl_expr (decl);
7855 last = create_temporary_var (TREE_TYPE (diff));
7856 pushdecl (last);
7857 add_decl_expr (last);
7858 if (c && iter_incr == NULL && TREE_CODE (incr) != INTEGER_CST
7859 && (!ordered || (i < collapse && collapse > 1)))
7861 incr_var = create_temporary_var (TREE_TYPE (diff));
7862 pushdecl (incr_var);
7863 add_decl_expr (incr_var);
7865 gcc_assert (stmts_are_full_exprs_p ());
7866 tree diffvar = NULL_TREE;
7867 if (code == OMP_TASKLOOP)
7869 if (!taskloop_iv_seen)
7871 tree ivc = build_omp_clause (locus, OMP_CLAUSE_FIRSTPRIVATE);
7872 OMP_CLAUSE_DECL (ivc) = iter;
7873 cxx_omp_finish_clause (ivc, NULL);
7874 OMP_CLAUSE_CHAIN (ivc) = clauses;
7875 clauses = ivc;
7877 tree lvc = build_omp_clause (locus, OMP_CLAUSE_FIRSTPRIVATE);
7878 OMP_CLAUSE_DECL (lvc) = last;
7879 OMP_CLAUSE_CHAIN (lvc) = clauses;
7880 clauses = lvc;
7881 diffvar = create_temporary_var (TREE_TYPE (diff));
7882 pushdecl (diffvar);
7883 add_decl_expr (diffvar);
7886 orig_pre_body = *pre_body;
7887 *pre_body = push_stmt_list ();
7888 if (orig_pre_body)
7889 add_stmt (orig_pre_body);
7890 if (init != NULL)
7891 finish_expr_stmt (build_x_modify_expr (elocus,
7892 iter, NOP_EXPR, init,
7893 tf_warning_or_error));
7894 init = build_int_cst (TREE_TYPE (diff), 0);
7895 if (c && iter_incr == NULL
7896 && (!ordered || (i < collapse && collapse > 1)))
7898 if (incr_var)
7900 finish_expr_stmt (build_x_modify_expr (elocus,
7901 incr_var, NOP_EXPR,
7902 incr, tf_warning_or_error));
7903 incr = incr_var;
7905 iter_incr = build_x_modify_expr (elocus,
7906 iter, PLUS_EXPR, incr,
7907 tf_warning_or_error);
7909 if (c && ordered && i < collapse && collapse > 1)
7910 iter_incr = incr;
7911 finish_expr_stmt (build_x_modify_expr (elocus,
7912 last, NOP_EXPR, init,
7913 tf_warning_or_error));
7914 if (diffvar)
7916 finish_expr_stmt (build_x_modify_expr (elocus,
7917 diffvar, NOP_EXPR,
7918 diff, tf_warning_or_error));
7919 diff = diffvar;
7921 *pre_body = pop_stmt_list (*pre_body);
7923 cond = cp_build_binary_op (elocus,
7924 TREE_CODE (cond), decl, diff,
7925 tf_warning_or_error);
7926 incr = build_modify_expr (elocus, decl, NULL_TREE, PLUS_EXPR,
7927 elocus, incr, NULL_TREE);
7929 orig_body = *body;
7930 *body = push_stmt_list ();
7931 iter_init = build2 (MINUS_EXPR, TREE_TYPE (diff), decl, last);
7932 iter_init = build_x_modify_expr (elocus,
7933 iter, PLUS_EXPR, iter_init,
7934 tf_warning_or_error);
7935 if (iter_init != error_mark_node)
7936 iter_init = build1 (NOP_EXPR, void_type_node, iter_init);
7937 finish_expr_stmt (iter_init);
7938 finish_expr_stmt (build_x_modify_expr (elocus,
7939 last, NOP_EXPR, decl,
7940 tf_warning_or_error));
7941 add_stmt (orig_body);
7942 *body = pop_stmt_list (*body);
7944 if (c)
7946 OMP_CLAUSE_LASTPRIVATE_STMT (c) = push_stmt_list ();
7947 if (!ordered)
7948 finish_expr_stmt (iter_incr);
7949 else
7951 iter_init = decl;
7952 if (i < collapse && collapse > 1 && !error_operand_p (iter_incr))
7953 iter_init = build2 (PLUS_EXPR, TREE_TYPE (diff),
7954 iter_init, iter_incr);
7955 iter_init = build2 (MINUS_EXPR, TREE_TYPE (diff), iter_init, last);
7956 iter_init = build_x_modify_expr (elocus,
7957 iter, PLUS_EXPR, iter_init,
7958 tf_warning_or_error);
7959 if (iter_init != error_mark_node)
7960 iter_init = build1 (NOP_EXPR, void_type_node, iter_init);
7961 finish_expr_stmt (iter_init);
7963 OMP_CLAUSE_LASTPRIVATE_STMT (c)
7964 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (c));
7967 TREE_VEC_ELT (declv, i) = decl;
7968 TREE_VEC_ELT (initv, i) = init;
7969 TREE_VEC_ELT (condv, i) = cond;
7970 TREE_VEC_ELT (incrv, i) = incr;
7971 *lastp = last;
7973 return false;
7976 /* Build and validate an OMP_FOR statement. CLAUSES, BODY, COND, INCR
7977 are directly for their associated operands in the statement. DECL
7978 and INIT are a combo; if DECL is NULL then INIT ought to be a
7979 MODIFY_EXPR, and the DECL should be extracted. PRE_BODY are
7980 optional statements that need to go before the loop into its
7981 sk_omp scope. */
7983 tree
7984 finish_omp_for (location_t locus, enum tree_code code, tree declv,
7985 tree orig_declv, tree initv, tree condv, tree incrv,
7986 tree body, tree pre_body, vec<tree> *orig_inits, tree clauses)
7988 tree omp_for = NULL, orig_incr = NULL;
7989 tree decl = NULL, init, cond, incr, orig_decl = NULL_TREE, block = NULL_TREE;
7990 tree last = NULL_TREE;
7991 location_t elocus;
7992 int i;
7993 int collapse = 1;
7994 int ordered = 0;
7996 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (initv));
7997 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (condv));
7998 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (incrv));
7999 if (TREE_VEC_LENGTH (declv) > 1)
8001 tree c;
8003 c = omp_find_clause (clauses, OMP_CLAUSE_TILE);
8004 if (c)
8005 collapse = list_length (OMP_CLAUSE_TILE_LIST (c));
8006 else
8008 c = omp_find_clause (clauses, OMP_CLAUSE_COLLAPSE);
8009 if (c)
8010 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (c));
8011 if (collapse != TREE_VEC_LENGTH (declv))
8012 ordered = TREE_VEC_LENGTH (declv);
8015 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
8017 decl = TREE_VEC_ELT (declv, i);
8018 init = TREE_VEC_ELT (initv, i);
8019 cond = TREE_VEC_ELT (condv, i);
8020 incr = TREE_VEC_ELT (incrv, i);
8021 elocus = locus;
8023 if (decl == NULL)
8025 if (init != NULL)
8026 switch (TREE_CODE (init))
8028 case MODIFY_EXPR:
8029 decl = TREE_OPERAND (init, 0);
8030 init = TREE_OPERAND (init, 1);
8031 break;
8032 case MODOP_EXPR:
8033 if (TREE_CODE (TREE_OPERAND (init, 1)) == NOP_EXPR)
8035 decl = TREE_OPERAND (init, 0);
8036 init = TREE_OPERAND (init, 2);
8038 break;
8039 default:
8040 break;
8043 if (decl == NULL)
8045 error_at (locus,
8046 "expected iteration declaration or initialization");
8047 return NULL;
8051 if (init && EXPR_HAS_LOCATION (init))
8052 elocus = EXPR_LOCATION (init);
8054 if (cond == NULL)
8056 error_at (elocus, "missing controlling predicate");
8057 return NULL;
8060 if (incr == NULL)
8062 error_at (elocus, "missing increment expression");
8063 return NULL;
8066 TREE_VEC_ELT (declv, i) = decl;
8067 TREE_VEC_ELT (initv, i) = init;
8070 if (orig_inits)
8072 bool fail = false;
8073 tree orig_init;
8074 FOR_EACH_VEC_ELT (*orig_inits, i, orig_init)
8075 if (orig_init
8076 && !c_omp_check_loop_iv_exprs (locus, declv,
8077 TREE_VEC_ELT (declv, i), orig_init,
8078 NULL_TREE, cp_walk_subtrees))
8079 fail = true;
8080 if (fail)
8081 return NULL;
8084 if (dependent_omp_for_p (declv, initv, condv, incrv))
8086 tree stmt;
8088 stmt = make_node (code);
8090 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
8092 /* This is really just a place-holder. We'll be decomposing this
8093 again and going through the cp_build_modify_expr path below when
8094 we instantiate the thing. */
8095 TREE_VEC_ELT (initv, i)
8096 = build2 (MODIFY_EXPR, void_type_node, TREE_VEC_ELT (declv, i),
8097 TREE_VEC_ELT (initv, i));
8100 TREE_TYPE (stmt) = void_type_node;
8101 OMP_FOR_INIT (stmt) = initv;
8102 OMP_FOR_COND (stmt) = condv;
8103 OMP_FOR_INCR (stmt) = incrv;
8104 OMP_FOR_BODY (stmt) = body;
8105 OMP_FOR_PRE_BODY (stmt) = pre_body;
8106 OMP_FOR_CLAUSES (stmt) = clauses;
8108 SET_EXPR_LOCATION (stmt, locus);
8109 return add_stmt (stmt);
8112 if (!orig_declv)
8113 orig_declv = copy_node (declv);
8115 if (processing_template_decl)
8116 orig_incr = make_tree_vec (TREE_VEC_LENGTH (incrv));
8118 for (i = 0; i < TREE_VEC_LENGTH (declv); )
8120 decl = TREE_VEC_ELT (declv, i);
8121 init = TREE_VEC_ELT (initv, i);
8122 cond = TREE_VEC_ELT (condv, i);
8123 incr = TREE_VEC_ELT (incrv, i);
8124 if (orig_incr)
8125 TREE_VEC_ELT (orig_incr, i) = incr;
8126 elocus = locus;
8128 if (init && EXPR_HAS_LOCATION (init))
8129 elocus = EXPR_LOCATION (init);
8131 if (!DECL_P (decl))
8133 error_at (elocus, "expected iteration declaration or initialization");
8134 return NULL;
8137 if (incr && TREE_CODE (incr) == MODOP_EXPR)
8139 if (orig_incr)
8140 TREE_VEC_ELT (orig_incr, i) = incr;
8141 incr = cp_build_modify_expr (elocus, TREE_OPERAND (incr, 0),
8142 TREE_CODE (TREE_OPERAND (incr, 1)),
8143 TREE_OPERAND (incr, 2),
8144 tf_warning_or_error);
8147 if (CLASS_TYPE_P (TREE_TYPE (decl)))
8149 if (code == OMP_SIMD)
8151 error_at (elocus, "%<#pragma omp simd%> used with class "
8152 "iteration variable %qE", decl);
8153 return NULL;
8155 if (code == CILK_FOR && i == 0)
8156 orig_decl = decl;
8157 if (handle_omp_for_class_iterator (i, locus, code, declv, orig_declv,
8158 initv, condv, incrv, &body,
8159 &pre_body, clauses, &last,
8160 collapse, ordered))
8161 return NULL;
8162 continue;
8165 if (!INTEGRAL_TYPE_P (TREE_TYPE (decl))
8166 && !TYPE_PTR_P (TREE_TYPE (decl)))
8168 error_at (elocus, "invalid type for iteration variable %qE", decl);
8169 return NULL;
8172 if (!processing_template_decl)
8174 init = fold_build_cleanup_point_expr (TREE_TYPE (init), init);
8175 init = cp_build_modify_expr (elocus, decl, NOP_EXPR, init,
8176 tf_warning_or_error);
8178 else
8179 init = build2 (MODIFY_EXPR, void_type_node, decl, init);
8180 if (cond
8181 && TREE_SIDE_EFFECTS (cond)
8182 && COMPARISON_CLASS_P (cond)
8183 && !processing_template_decl)
8185 tree t = TREE_OPERAND (cond, 0);
8186 if (TREE_SIDE_EFFECTS (t)
8187 && t != decl
8188 && (TREE_CODE (t) != NOP_EXPR
8189 || TREE_OPERAND (t, 0) != decl))
8190 TREE_OPERAND (cond, 0)
8191 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
8193 t = TREE_OPERAND (cond, 1);
8194 if (TREE_SIDE_EFFECTS (t)
8195 && t != decl
8196 && (TREE_CODE (t) != NOP_EXPR
8197 || TREE_OPERAND (t, 0) != decl))
8198 TREE_OPERAND (cond, 1)
8199 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
8201 if (decl == error_mark_node || init == error_mark_node)
8202 return NULL;
8204 TREE_VEC_ELT (declv, i) = decl;
8205 TREE_VEC_ELT (initv, i) = init;
8206 TREE_VEC_ELT (condv, i) = cond;
8207 TREE_VEC_ELT (incrv, i) = incr;
8208 i++;
8211 if (IS_EMPTY_STMT (pre_body))
8212 pre_body = NULL;
8214 if (code == CILK_FOR && !processing_template_decl)
8215 block = push_stmt_list ();
8217 omp_for = c_finish_omp_for (locus, code, declv, orig_declv, initv, condv,
8218 incrv, body, pre_body);
8220 /* Check for iterators appearing in lb, b or incr expressions. */
8221 if (omp_for && !c_omp_check_loop_iv (omp_for, orig_declv, cp_walk_subtrees))
8222 omp_for = NULL_TREE;
8224 if (omp_for == NULL)
8226 if (block)
8227 pop_stmt_list (block);
8228 return NULL;
8231 add_stmt (omp_for);
8233 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INCR (omp_for)); i++)
8235 decl = TREE_OPERAND (TREE_VEC_ELT (OMP_FOR_INIT (omp_for), i), 0);
8236 incr = TREE_VEC_ELT (OMP_FOR_INCR (omp_for), i);
8238 if (TREE_CODE (incr) != MODIFY_EXPR)
8239 continue;
8241 if (TREE_SIDE_EFFECTS (TREE_OPERAND (incr, 1))
8242 && BINARY_CLASS_P (TREE_OPERAND (incr, 1))
8243 && !processing_template_decl)
8245 tree t = TREE_OPERAND (TREE_OPERAND (incr, 1), 0);
8246 if (TREE_SIDE_EFFECTS (t)
8247 && t != decl
8248 && (TREE_CODE (t) != NOP_EXPR
8249 || TREE_OPERAND (t, 0) != decl))
8250 TREE_OPERAND (TREE_OPERAND (incr, 1), 0)
8251 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
8253 t = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
8254 if (TREE_SIDE_EFFECTS (t)
8255 && t != decl
8256 && (TREE_CODE (t) != NOP_EXPR
8257 || TREE_OPERAND (t, 0) != decl))
8258 TREE_OPERAND (TREE_OPERAND (incr, 1), 1)
8259 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
8262 if (orig_incr)
8263 TREE_VEC_ELT (OMP_FOR_INCR (omp_for), i) = TREE_VEC_ELT (orig_incr, i);
8265 OMP_FOR_CLAUSES (omp_for) = clauses;
8267 /* For simd loops with non-static data member iterators, we could have added
8268 OMP_CLAUSE_LINEAR clauses without OMP_CLAUSE_LINEAR_STEP. As we know the
8269 step at this point, fill it in. */
8270 if (code == OMP_SIMD && !processing_template_decl
8271 && TREE_VEC_LENGTH (OMP_FOR_INCR (omp_for)) == 1)
8272 for (tree c = omp_find_clause (clauses, OMP_CLAUSE_LINEAR); c;
8273 c = omp_find_clause (OMP_CLAUSE_CHAIN (c), OMP_CLAUSE_LINEAR))
8274 if (OMP_CLAUSE_LINEAR_STEP (c) == NULL_TREE)
8276 decl = TREE_OPERAND (TREE_VEC_ELT (OMP_FOR_INIT (omp_for), 0), 0);
8277 gcc_assert (decl == OMP_CLAUSE_DECL (c));
8278 incr = TREE_VEC_ELT (OMP_FOR_INCR (omp_for), 0);
8279 tree step, stept;
8280 switch (TREE_CODE (incr))
8282 case PREINCREMENT_EXPR:
8283 case POSTINCREMENT_EXPR:
8284 /* c_omp_for_incr_canonicalize_ptr() should have been
8285 called to massage things appropriately. */
8286 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (decl)));
8287 OMP_CLAUSE_LINEAR_STEP (c) = build_int_cst (TREE_TYPE (decl), 1);
8288 break;
8289 case PREDECREMENT_EXPR:
8290 case POSTDECREMENT_EXPR:
8291 /* c_omp_for_incr_canonicalize_ptr() should have been
8292 called to massage things appropriately. */
8293 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (decl)));
8294 OMP_CLAUSE_LINEAR_STEP (c)
8295 = build_int_cst (TREE_TYPE (decl), -1);
8296 break;
8297 case MODIFY_EXPR:
8298 gcc_assert (TREE_OPERAND (incr, 0) == decl);
8299 incr = TREE_OPERAND (incr, 1);
8300 switch (TREE_CODE (incr))
8302 case PLUS_EXPR:
8303 if (TREE_OPERAND (incr, 1) == decl)
8304 step = TREE_OPERAND (incr, 0);
8305 else
8306 step = TREE_OPERAND (incr, 1);
8307 break;
8308 case MINUS_EXPR:
8309 case POINTER_PLUS_EXPR:
8310 gcc_assert (TREE_OPERAND (incr, 0) == decl);
8311 step = TREE_OPERAND (incr, 1);
8312 break;
8313 default:
8314 gcc_unreachable ();
8316 stept = TREE_TYPE (decl);
8317 if (POINTER_TYPE_P (stept))
8318 stept = sizetype;
8319 step = fold_convert (stept, step);
8320 if (TREE_CODE (incr) == MINUS_EXPR)
8321 step = fold_build1 (NEGATE_EXPR, stept, step);
8322 OMP_CLAUSE_LINEAR_STEP (c) = step;
8323 break;
8324 default:
8325 gcc_unreachable ();
8329 if (block)
8331 tree omp_par = make_node (OMP_PARALLEL);
8332 TREE_TYPE (omp_par) = void_type_node;
8333 OMP_PARALLEL_CLAUSES (omp_par) = NULL_TREE;
8334 tree bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
8335 TREE_SIDE_EFFECTS (bind) = 1;
8336 BIND_EXPR_BODY (bind) = pop_stmt_list (block);
8337 OMP_PARALLEL_BODY (omp_par) = bind;
8338 if (OMP_FOR_PRE_BODY (omp_for))
8340 add_stmt (OMP_FOR_PRE_BODY (omp_for));
8341 OMP_FOR_PRE_BODY (omp_for) = NULL_TREE;
8343 init = TREE_VEC_ELT (OMP_FOR_INIT (omp_for), 0);
8344 decl = TREE_OPERAND (init, 0);
8345 cond = TREE_VEC_ELT (OMP_FOR_COND (omp_for), 0);
8346 incr = TREE_VEC_ELT (OMP_FOR_INCR (omp_for), 0);
8347 tree t = TREE_OPERAND (cond, 1), c, clauses, *pc;
8348 clauses = OMP_FOR_CLAUSES (omp_for);
8349 OMP_FOR_CLAUSES (omp_for) = NULL_TREE;
8350 for (pc = &clauses; *pc; )
8351 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_SCHEDULE)
8353 gcc_assert (OMP_FOR_CLAUSES (omp_for) == NULL_TREE);
8354 OMP_FOR_CLAUSES (omp_for) = *pc;
8355 *pc = OMP_CLAUSE_CHAIN (*pc);
8356 OMP_CLAUSE_CHAIN (OMP_FOR_CLAUSES (omp_for)) = NULL_TREE;
8358 else
8360 gcc_assert (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_FIRSTPRIVATE);
8361 pc = &OMP_CLAUSE_CHAIN (*pc);
8363 if (TREE_CODE (t) != INTEGER_CST)
8365 TREE_OPERAND (cond, 1) = get_temp_regvar (TREE_TYPE (t), t);
8366 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
8367 OMP_CLAUSE_DECL (c) = TREE_OPERAND (cond, 1);
8368 OMP_CLAUSE_CHAIN (c) = clauses;
8369 clauses = c;
8371 if (TREE_CODE (incr) == MODIFY_EXPR)
8373 t = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
8374 if (TREE_CODE (t) != INTEGER_CST)
8376 TREE_OPERAND (TREE_OPERAND (incr, 1), 1)
8377 = get_temp_regvar (TREE_TYPE (t), t);
8378 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
8379 OMP_CLAUSE_DECL (c) = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
8380 OMP_CLAUSE_CHAIN (c) = clauses;
8381 clauses = c;
8384 t = TREE_OPERAND (init, 1);
8385 if (TREE_CODE (t) != INTEGER_CST)
8387 TREE_OPERAND (init, 1) = get_temp_regvar (TREE_TYPE (t), t);
8388 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
8389 OMP_CLAUSE_DECL (c) = TREE_OPERAND (init, 1);
8390 OMP_CLAUSE_CHAIN (c) = clauses;
8391 clauses = c;
8393 if (orig_decl && orig_decl != decl)
8395 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
8396 OMP_CLAUSE_DECL (c) = orig_decl;
8397 OMP_CLAUSE_CHAIN (c) = clauses;
8398 clauses = c;
8400 if (last)
8402 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
8403 OMP_CLAUSE_DECL (c) = last;
8404 OMP_CLAUSE_CHAIN (c) = clauses;
8405 clauses = c;
8407 c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
8408 OMP_CLAUSE_DECL (c) = decl;
8409 OMP_CLAUSE_CHAIN (c) = clauses;
8410 clauses = c;
8411 c = build_omp_clause (input_location, OMP_CLAUSE__CILK_FOR_COUNT_);
8412 OMP_CLAUSE_OPERAND (c, 0)
8413 = cilk_for_number_of_iterations (omp_for);
8414 OMP_CLAUSE_CHAIN (c) = clauses;
8415 OMP_PARALLEL_CLAUSES (omp_par) = finish_omp_clauses (c, C_ORT_CILK);
8416 add_stmt (omp_par);
8417 return omp_par;
8419 else if (code == CILK_FOR && processing_template_decl)
8421 tree c, clauses = OMP_FOR_CLAUSES (omp_for);
8422 if (orig_decl && orig_decl != decl)
8424 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
8425 OMP_CLAUSE_DECL (c) = orig_decl;
8426 OMP_CLAUSE_CHAIN (c) = clauses;
8427 clauses = c;
8429 if (last)
8431 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
8432 OMP_CLAUSE_DECL (c) = last;
8433 OMP_CLAUSE_CHAIN (c) = clauses;
8434 clauses = c;
8436 OMP_FOR_CLAUSES (omp_for) = clauses;
8438 return omp_for;
8441 void
8442 finish_omp_atomic (enum tree_code code, enum tree_code opcode, tree lhs,
8443 tree rhs, tree v, tree lhs1, tree rhs1, bool seq_cst)
8445 tree orig_lhs;
8446 tree orig_rhs;
8447 tree orig_v;
8448 tree orig_lhs1;
8449 tree orig_rhs1;
8450 bool dependent_p;
8451 tree stmt;
8453 orig_lhs = lhs;
8454 orig_rhs = rhs;
8455 orig_v = v;
8456 orig_lhs1 = lhs1;
8457 orig_rhs1 = rhs1;
8458 dependent_p = false;
8459 stmt = NULL_TREE;
8461 /* Even in a template, we can detect invalid uses of the atomic
8462 pragma if neither LHS nor RHS is type-dependent. */
8463 if (processing_template_decl)
8465 dependent_p = (type_dependent_expression_p (lhs)
8466 || (rhs && type_dependent_expression_p (rhs))
8467 || (v && type_dependent_expression_p (v))
8468 || (lhs1 && type_dependent_expression_p (lhs1))
8469 || (rhs1 && type_dependent_expression_p (rhs1)));
8470 if (!dependent_p)
8472 lhs = build_non_dependent_expr (lhs);
8473 if (rhs)
8474 rhs = build_non_dependent_expr (rhs);
8475 if (v)
8476 v = build_non_dependent_expr (v);
8477 if (lhs1)
8478 lhs1 = build_non_dependent_expr (lhs1);
8479 if (rhs1)
8480 rhs1 = build_non_dependent_expr (rhs1);
8483 if (!dependent_p)
8485 bool swapped = false;
8486 if (rhs1 && cp_tree_equal (lhs, rhs))
8488 std::swap (rhs, rhs1);
8489 swapped = !commutative_tree_code (opcode);
8491 if (rhs1 && !cp_tree_equal (lhs, rhs1))
8493 if (code == OMP_ATOMIC)
8494 error ("%<#pragma omp atomic update%> uses two different "
8495 "expressions for memory");
8496 else
8497 error ("%<#pragma omp atomic capture%> uses two different "
8498 "expressions for memory");
8499 return;
8501 if (lhs1 && !cp_tree_equal (lhs, lhs1))
8503 if (code == OMP_ATOMIC)
8504 error ("%<#pragma omp atomic update%> uses two different "
8505 "expressions for memory");
8506 else
8507 error ("%<#pragma omp atomic capture%> uses two different "
8508 "expressions for memory");
8509 return;
8511 stmt = c_finish_omp_atomic (input_location, code, opcode, lhs, rhs,
8512 v, lhs1, rhs1, swapped, seq_cst,
8513 processing_template_decl != 0);
8514 if (stmt == error_mark_node)
8515 return;
8517 if (processing_template_decl)
8519 if (code == OMP_ATOMIC_READ)
8521 stmt = build_min_nt_loc (EXPR_LOCATION (orig_lhs),
8522 OMP_ATOMIC_READ, orig_lhs);
8523 OMP_ATOMIC_SEQ_CST (stmt) = seq_cst;
8524 stmt = build2 (MODIFY_EXPR, void_type_node, orig_v, stmt);
8526 else
8528 if (opcode == NOP_EXPR)
8529 stmt = build2 (MODIFY_EXPR, void_type_node, orig_lhs, orig_rhs);
8530 else
8531 stmt = build2 (opcode, void_type_node, orig_lhs, orig_rhs);
8532 if (orig_rhs1)
8533 stmt = build_min_nt_loc (EXPR_LOCATION (orig_rhs1),
8534 COMPOUND_EXPR, orig_rhs1, stmt);
8535 if (code != OMP_ATOMIC)
8537 stmt = build_min_nt_loc (EXPR_LOCATION (orig_lhs1),
8538 code, orig_lhs1, stmt);
8539 OMP_ATOMIC_SEQ_CST (stmt) = seq_cst;
8540 stmt = build2 (MODIFY_EXPR, void_type_node, orig_v, stmt);
8543 stmt = build2 (OMP_ATOMIC, void_type_node, integer_zero_node, stmt);
8544 OMP_ATOMIC_SEQ_CST (stmt) = seq_cst;
8546 finish_expr_stmt (stmt);
8549 void
8550 finish_omp_barrier (void)
8552 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_BARRIER);
8553 vec<tree, va_gc> *vec = make_tree_vector ();
8554 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
8555 release_tree_vector (vec);
8556 finish_expr_stmt (stmt);
8559 void
8560 finish_omp_flush (void)
8562 tree fn = builtin_decl_explicit (BUILT_IN_SYNC_SYNCHRONIZE);
8563 vec<tree, va_gc> *vec = make_tree_vector ();
8564 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
8565 release_tree_vector (vec);
8566 finish_expr_stmt (stmt);
8569 void
8570 finish_omp_taskwait (void)
8572 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_TASKWAIT);
8573 vec<tree, va_gc> *vec = make_tree_vector ();
8574 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
8575 release_tree_vector (vec);
8576 finish_expr_stmt (stmt);
8579 void
8580 finish_omp_taskyield (void)
8582 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_TASKYIELD);
8583 vec<tree, va_gc> *vec = make_tree_vector ();
8584 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
8585 release_tree_vector (vec);
8586 finish_expr_stmt (stmt);
8589 void
8590 finish_omp_cancel (tree clauses)
8592 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
8593 int mask = 0;
8594 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
8595 mask = 1;
8596 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
8597 mask = 2;
8598 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
8599 mask = 4;
8600 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
8601 mask = 8;
8602 else
8604 error ("%<#pragma omp cancel%> must specify one of "
8605 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> clauses");
8606 return;
8608 vec<tree, va_gc> *vec = make_tree_vector ();
8609 tree ifc = omp_find_clause (clauses, OMP_CLAUSE_IF);
8610 if (ifc != NULL_TREE)
8612 tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
8613 ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
8614 boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
8615 build_zero_cst (type));
8617 else
8618 ifc = boolean_true_node;
8619 vec->quick_push (build_int_cst (integer_type_node, mask));
8620 vec->quick_push (ifc);
8621 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
8622 release_tree_vector (vec);
8623 finish_expr_stmt (stmt);
8626 void
8627 finish_omp_cancellation_point (tree clauses)
8629 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
8630 int mask = 0;
8631 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
8632 mask = 1;
8633 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
8634 mask = 2;
8635 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
8636 mask = 4;
8637 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
8638 mask = 8;
8639 else
8641 error ("%<#pragma omp cancellation point%> must specify one of "
8642 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> clauses");
8643 return;
8645 vec<tree, va_gc> *vec
8646 = make_tree_vector_single (build_int_cst (integer_type_node, mask));
8647 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
8648 release_tree_vector (vec);
8649 finish_expr_stmt (stmt);
8652 /* Begin a __transaction_atomic or __transaction_relaxed statement.
8653 If PCOMPOUND is non-null, this is for a function-transaction-block, and we
8654 should create an extra compound stmt. */
8656 tree
8657 begin_transaction_stmt (location_t loc, tree *pcompound, int flags)
8659 tree r;
8661 if (pcompound)
8662 *pcompound = begin_compound_stmt (0);
8664 r = build_stmt (loc, TRANSACTION_EXPR, NULL_TREE);
8666 /* Only add the statement to the function if support enabled. */
8667 if (flag_tm)
8668 add_stmt (r);
8669 else
8670 error_at (loc, ((flags & TM_STMT_ATTR_RELAXED) != 0
8671 ? G_("%<__transaction_relaxed%> without "
8672 "transactional memory support enabled")
8673 : G_("%<__transaction_atomic%> without "
8674 "transactional memory support enabled")));
8676 TRANSACTION_EXPR_BODY (r) = push_stmt_list ();
8677 TREE_SIDE_EFFECTS (r) = 1;
8678 return r;
8681 /* End a __transaction_atomic or __transaction_relaxed statement.
8682 If COMPOUND_STMT is non-null, this is for a function-transaction-block,
8683 and we should end the compound. If NOEX is non-NULL, we wrap the body in
8684 a MUST_NOT_THROW_EXPR with NOEX as condition. */
8686 void
8687 finish_transaction_stmt (tree stmt, tree compound_stmt, int flags, tree noex)
8689 TRANSACTION_EXPR_BODY (stmt) = pop_stmt_list (TRANSACTION_EXPR_BODY (stmt));
8690 TRANSACTION_EXPR_OUTER (stmt) = (flags & TM_STMT_ATTR_OUTER) != 0;
8691 TRANSACTION_EXPR_RELAXED (stmt) = (flags & TM_STMT_ATTR_RELAXED) != 0;
8692 TRANSACTION_EXPR_IS_STMT (stmt) = 1;
8694 /* noexcept specifications are not allowed for function transactions. */
8695 gcc_assert (!(noex && compound_stmt));
8696 if (noex)
8698 tree body = build_must_not_throw_expr (TRANSACTION_EXPR_BODY (stmt),
8699 noex);
8700 protected_set_expr_location
8701 (body, EXPR_LOCATION (TRANSACTION_EXPR_BODY (stmt)));
8702 TREE_SIDE_EFFECTS (body) = 1;
8703 TRANSACTION_EXPR_BODY (stmt) = body;
8706 if (compound_stmt)
8707 finish_compound_stmt (compound_stmt);
8710 /* Build a __transaction_atomic or __transaction_relaxed expression. If
8711 NOEX is non-NULL, we wrap the body in a MUST_NOT_THROW_EXPR with NOEX as
8712 condition. */
8714 tree
8715 build_transaction_expr (location_t loc, tree expr, int flags, tree noex)
8717 tree ret;
8718 if (noex)
8720 expr = build_must_not_throw_expr (expr, noex);
8721 protected_set_expr_location (expr, loc);
8722 TREE_SIDE_EFFECTS (expr) = 1;
8724 ret = build1 (TRANSACTION_EXPR, TREE_TYPE (expr), expr);
8725 if (flags & TM_STMT_ATTR_RELAXED)
8726 TRANSACTION_EXPR_RELAXED (ret) = 1;
8727 TREE_SIDE_EFFECTS (ret) = 1;
8728 SET_EXPR_LOCATION (ret, loc);
8729 return ret;
8732 void
8733 init_cp_semantics (void)
8737 /* Build a STATIC_ASSERT for a static assertion with the condition
8738 CONDITION and the message text MESSAGE. LOCATION is the location
8739 of the static assertion in the source code. When MEMBER_P, this
8740 static assertion is a member of a class. */
8741 void
8742 finish_static_assert (tree condition, tree message, location_t location,
8743 bool member_p)
8745 if (message == NULL_TREE
8746 || message == error_mark_node
8747 || condition == NULL_TREE
8748 || condition == error_mark_node)
8749 return;
8751 if (check_for_bare_parameter_packs (condition))
8752 condition = error_mark_node;
8754 if (type_dependent_expression_p (condition)
8755 || value_dependent_expression_p (condition))
8757 /* We're in a template; build a STATIC_ASSERT and put it in
8758 the right place. */
8759 tree assertion;
8761 assertion = make_node (STATIC_ASSERT);
8762 STATIC_ASSERT_CONDITION (assertion) = condition;
8763 STATIC_ASSERT_MESSAGE (assertion) = message;
8764 STATIC_ASSERT_SOURCE_LOCATION (assertion) = location;
8766 if (member_p)
8767 maybe_add_class_template_decl_list (current_class_type,
8768 assertion,
8769 /*friend_p=*/0);
8770 else
8771 add_stmt (assertion);
8773 return;
8776 /* Fold the expression and convert it to a boolean value. */
8777 condition = instantiate_non_dependent_expr (condition);
8778 condition = cp_convert (boolean_type_node, condition, tf_warning_or_error);
8779 condition = maybe_constant_value (condition);
8781 if (TREE_CODE (condition) == INTEGER_CST && !integer_zerop (condition))
8782 /* Do nothing; the condition is satisfied. */
8784 else
8786 location_t saved_loc = input_location;
8788 input_location = location;
8789 if (TREE_CODE (condition) == INTEGER_CST
8790 && integer_zerop (condition))
8792 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
8793 (TREE_TYPE (TREE_TYPE (message))));
8794 int len = TREE_STRING_LENGTH (message) / sz - 1;
8795 /* Report the error. */
8796 if (len == 0)
8797 error ("static assertion failed");
8798 else
8799 error ("static assertion failed: %s",
8800 TREE_STRING_POINTER (message));
8802 else if (condition && condition != error_mark_node)
8804 error ("non-constant condition for static assertion");
8805 if (require_potential_rvalue_constant_expression (condition))
8806 cxx_constant_value (condition);
8808 input_location = saved_loc;
8812 /* Implements the C++0x decltype keyword. Returns the type of EXPR,
8813 suitable for use as a type-specifier.
8815 ID_EXPRESSION_OR_MEMBER_ACCESS_P is true when EXPR was parsed as an
8816 id-expression or a class member access, FALSE when it was parsed as
8817 a full expression. */
8819 tree
8820 finish_decltype_type (tree expr, bool id_expression_or_member_access_p,
8821 tsubst_flags_t complain)
8823 tree type = NULL_TREE;
8825 if (!expr || error_operand_p (expr))
8826 return error_mark_node;
8828 if (TYPE_P (expr)
8829 || TREE_CODE (expr) == TYPE_DECL
8830 || (TREE_CODE (expr) == BIT_NOT_EXPR
8831 && TYPE_P (TREE_OPERAND (expr, 0))))
8833 if (complain & tf_error)
8834 error ("argument to decltype must be an expression");
8835 return error_mark_node;
8838 /* Depending on the resolution of DR 1172, we may later need to distinguish
8839 instantiation-dependent but not type-dependent expressions so that, say,
8840 A<decltype(sizeof(T))>::U doesn't require 'typename'. */
8841 if (instantiation_dependent_uneval_expression_p (expr))
8843 type = cxx_make_type (DECLTYPE_TYPE);
8844 DECLTYPE_TYPE_EXPR (type) = expr;
8845 DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (type)
8846 = id_expression_or_member_access_p;
8847 SET_TYPE_STRUCTURAL_EQUALITY (type);
8849 return type;
8852 /* The type denoted by decltype(e) is defined as follows: */
8854 expr = resolve_nondeduced_context (expr, complain);
8856 if (invalid_nonstatic_memfn_p (input_location, expr, complain))
8857 return error_mark_node;
8859 if (type_unknown_p (expr))
8861 if (complain & tf_error)
8862 error ("decltype cannot resolve address of overloaded function");
8863 return error_mark_node;
8866 /* To get the size of a static data member declared as an array of
8867 unknown bound, we need to instantiate it. */
8868 if (VAR_P (expr)
8869 && VAR_HAD_UNKNOWN_BOUND (expr)
8870 && DECL_TEMPLATE_INSTANTIATION (expr))
8871 instantiate_decl (expr, /*defer_ok*/true, /*expl_inst_mem*/false);
8873 if (id_expression_or_member_access_p)
8875 /* If e is an id-expression or a class member access (5.2.5
8876 [expr.ref]), decltype(e) is defined as the type of the entity
8877 named by e. If there is no such entity, or e names a set of
8878 overloaded functions, the program is ill-formed. */
8879 if (identifier_p (expr))
8880 expr = lookup_name (expr);
8882 if (INDIRECT_REF_P (expr))
8883 /* This can happen when the expression is, e.g., "a.b". Just
8884 look at the underlying operand. */
8885 expr = TREE_OPERAND (expr, 0);
8887 if (TREE_CODE (expr) == OFFSET_REF
8888 || TREE_CODE (expr) == MEMBER_REF
8889 || TREE_CODE (expr) == SCOPE_REF)
8890 /* We're only interested in the field itself. If it is a
8891 BASELINK, we will need to see through it in the next
8892 step. */
8893 expr = TREE_OPERAND (expr, 1);
8895 if (BASELINK_P (expr))
8896 /* See through BASELINK nodes to the underlying function. */
8897 expr = BASELINK_FUNCTIONS (expr);
8899 /* decltype of a decomposition name drops references in the tuple case
8900 (unlike decltype of a normal variable) and keeps cv-qualifiers from
8901 the containing object in the other cases (unlike decltype of a member
8902 access expression). */
8903 if (DECL_DECOMPOSITION_P (expr))
8905 if (DECL_HAS_VALUE_EXPR_P (expr))
8906 /* Expr is an array or struct subobject proxy, handle
8907 bit-fields properly. */
8908 return unlowered_expr_type (expr);
8909 else
8910 /* Expr is a reference variable for the tuple case. */
8911 return lookup_decomp_type (expr);
8914 switch (TREE_CODE (expr))
8916 case FIELD_DECL:
8917 if (DECL_BIT_FIELD_TYPE (expr))
8919 type = DECL_BIT_FIELD_TYPE (expr);
8920 break;
8922 /* Fall through for fields that aren't bitfields. */
8923 gcc_fallthrough ();
8925 case FUNCTION_DECL:
8926 case VAR_DECL:
8927 case CONST_DECL:
8928 case PARM_DECL:
8929 case RESULT_DECL:
8930 case TEMPLATE_PARM_INDEX:
8931 expr = mark_type_use (expr);
8932 type = TREE_TYPE (expr);
8933 break;
8935 case ERROR_MARK:
8936 type = error_mark_node;
8937 break;
8939 case COMPONENT_REF:
8940 case COMPOUND_EXPR:
8941 mark_type_use (expr);
8942 type = is_bitfield_expr_with_lowered_type (expr);
8943 if (!type)
8944 type = TREE_TYPE (TREE_OPERAND (expr, 1));
8945 break;
8947 case BIT_FIELD_REF:
8948 gcc_unreachable ();
8950 case INTEGER_CST:
8951 case PTRMEM_CST:
8952 /* We can get here when the id-expression refers to an
8953 enumerator or non-type template parameter. */
8954 type = TREE_TYPE (expr);
8955 break;
8957 default:
8958 /* Handle instantiated template non-type arguments. */
8959 type = TREE_TYPE (expr);
8960 break;
8963 else
8965 /* Within a lambda-expression:
8967 Every occurrence of decltype((x)) where x is a possibly
8968 parenthesized id-expression that names an entity of
8969 automatic storage duration is treated as if x were
8970 transformed into an access to a corresponding data member
8971 of the closure type that would have been declared if x
8972 were a use of the denoted entity. */
8973 if (outer_automatic_var_p (expr)
8974 && current_function_decl
8975 && LAMBDA_FUNCTION_P (current_function_decl))
8976 type = capture_decltype (expr);
8977 else if (error_operand_p (expr))
8978 type = error_mark_node;
8979 else if (expr == current_class_ptr)
8980 /* If the expression is just "this", we want the
8981 cv-unqualified pointer for the "this" type. */
8982 type = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
8983 else
8985 /* Otherwise, where T is the type of e, if e is an lvalue,
8986 decltype(e) is defined as T&; if an xvalue, T&&; otherwise, T. */
8987 cp_lvalue_kind clk = lvalue_kind (expr);
8988 type = unlowered_expr_type (expr);
8989 gcc_assert (TREE_CODE (type) != REFERENCE_TYPE);
8991 /* For vector types, pick a non-opaque variant. */
8992 if (VECTOR_TYPE_P (type))
8993 type = strip_typedefs (type);
8995 if (clk != clk_none && !(clk & clk_class))
8996 type = cp_build_reference_type (type, (clk & clk_rvalueref));
9000 return type;
9003 /* Called from trait_expr_value to evaluate either __has_nothrow_assign or
9004 __has_nothrow_copy, depending on assign_p. Returns true iff all
9005 the copy {ctor,assign} fns are nothrow. */
9007 static bool
9008 classtype_has_nothrow_assign_or_copy_p (tree type, bool assign_p)
9010 tree fns = NULL_TREE;
9012 if (assign_p)
9013 fns = lookup_fnfields_slot (type, cp_assignment_operator_id (NOP_EXPR));
9014 else if (TYPE_HAS_COPY_CTOR (type))
9015 fns = lookup_fnfields_slot (type, ctor_identifier);
9017 bool saw_copy = false;
9018 for (ovl_iterator iter (fns); iter; ++iter)
9020 tree fn = *iter;
9022 if (copy_fn_p (fn) > 0)
9024 saw_copy = true;
9025 maybe_instantiate_noexcept (fn);
9026 if (!TYPE_NOTHROW_P (TREE_TYPE (fn)))
9027 return false;
9031 return saw_copy;
9034 /* Actually evaluates the trait. */
9036 static bool
9037 trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
9039 enum tree_code type_code1;
9040 tree t;
9042 type_code1 = TREE_CODE (type1);
9044 switch (kind)
9046 case CPTK_HAS_NOTHROW_ASSIGN:
9047 type1 = strip_array_types (type1);
9048 return (!CP_TYPE_CONST_P (type1) && type_code1 != REFERENCE_TYPE
9049 && (trait_expr_value (CPTK_HAS_TRIVIAL_ASSIGN, type1, type2)
9050 || (CLASS_TYPE_P (type1)
9051 && classtype_has_nothrow_assign_or_copy_p (type1,
9052 true))));
9054 case CPTK_HAS_TRIVIAL_ASSIGN:
9055 /* ??? The standard seems to be missing the "or array of such a class
9056 type" wording for this trait. */
9057 type1 = strip_array_types (type1);
9058 return (!CP_TYPE_CONST_P (type1) && type_code1 != REFERENCE_TYPE
9059 && (trivial_type_p (type1)
9060 || (CLASS_TYPE_P (type1)
9061 && TYPE_HAS_TRIVIAL_COPY_ASSIGN (type1))));
9063 case CPTK_HAS_NOTHROW_CONSTRUCTOR:
9064 type1 = strip_array_types (type1);
9065 return (trait_expr_value (CPTK_HAS_TRIVIAL_CONSTRUCTOR, type1, type2)
9066 || (CLASS_TYPE_P (type1)
9067 && (t = locate_ctor (type1))
9068 && (maybe_instantiate_noexcept (t),
9069 TYPE_NOTHROW_P (TREE_TYPE (t)))));
9071 case CPTK_HAS_TRIVIAL_CONSTRUCTOR:
9072 type1 = strip_array_types (type1);
9073 return (trivial_type_p (type1)
9074 || (CLASS_TYPE_P (type1) && TYPE_HAS_TRIVIAL_DFLT (type1)));
9076 case CPTK_HAS_NOTHROW_COPY:
9077 type1 = strip_array_types (type1);
9078 return (trait_expr_value (CPTK_HAS_TRIVIAL_COPY, type1, type2)
9079 || (CLASS_TYPE_P (type1)
9080 && classtype_has_nothrow_assign_or_copy_p (type1, false)));
9082 case CPTK_HAS_TRIVIAL_COPY:
9083 /* ??? The standard seems to be missing the "or array of such a class
9084 type" wording for this trait. */
9085 type1 = strip_array_types (type1);
9086 return (trivial_type_p (type1) || type_code1 == REFERENCE_TYPE
9087 || (CLASS_TYPE_P (type1) && TYPE_HAS_TRIVIAL_COPY_CTOR (type1)));
9089 case CPTK_HAS_TRIVIAL_DESTRUCTOR:
9090 type1 = strip_array_types (type1);
9091 return (trivial_type_p (type1) || type_code1 == REFERENCE_TYPE
9092 || (CLASS_TYPE_P (type1)
9093 && TYPE_HAS_TRIVIAL_DESTRUCTOR (type1)));
9095 case CPTK_HAS_VIRTUAL_DESTRUCTOR:
9096 return type_has_virtual_destructor (type1);
9098 case CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS:
9099 return type_has_unique_obj_representations (type1);
9101 case CPTK_IS_ABSTRACT:
9102 return ABSTRACT_CLASS_TYPE_P (type1);
9104 case CPTK_IS_AGGREGATE:
9105 return CP_AGGREGATE_TYPE_P (type1);
9107 case CPTK_IS_BASE_OF:
9108 return (NON_UNION_CLASS_TYPE_P (type1) && NON_UNION_CLASS_TYPE_P (type2)
9109 && (same_type_ignoring_top_level_qualifiers_p (type1, type2)
9110 || DERIVED_FROM_P (type1, type2)));
9112 case CPTK_IS_CLASS:
9113 return NON_UNION_CLASS_TYPE_P (type1);
9115 case CPTK_IS_EMPTY:
9116 return NON_UNION_CLASS_TYPE_P (type1) && CLASSTYPE_EMPTY_P (type1);
9118 case CPTK_IS_ENUM:
9119 return type_code1 == ENUMERAL_TYPE;
9121 case CPTK_IS_FINAL:
9122 return CLASS_TYPE_P (type1) && CLASSTYPE_FINAL (type1);
9124 case CPTK_IS_LITERAL_TYPE:
9125 return literal_type_p (type1);
9127 case CPTK_IS_POD:
9128 return pod_type_p (type1);
9130 case CPTK_IS_POLYMORPHIC:
9131 return CLASS_TYPE_P (type1) && TYPE_POLYMORPHIC_P (type1);
9133 case CPTK_IS_SAME_AS:
9134 return same_type_p (type1, type2);
9136 case CPTK_IS_STD_LAYOUT:
9137 return std_layout_type_p (type1);
9139 case CPTK_IS_TRIVIAL:
9140 return trivial_type_p (type1);
9142 case CPTK_IS_TRIVIALLY_ASSIGNABLE:
9143 return is_trivially_xible (MODIFY_EXPR, type1, type2);
9145 case CPTK_IS_TRIVIALLY_CONSTRUCTIBLE:
9146 return is_trivially_xible (INIT_EXPR, type1, type2);
9148 case CPTK_IS_TRIVIALLY_COPYABLE:
9149 return trivially_copyable_p (type1);
9151 case CPTK_IS_UNION:
9152 return type_code1 == UNION_TYPE;
9154 case CPTK_IS_ASSIGNABLE:
9155 return is_xible (MODIFY_EXPR, type1, type2);
9157 case CPTK_IS_CONSTRUCTIBLE:
9158 return is_xible (INIT_EXPR, type1, type2);
9160 default:
9161 gcc_unreachable ();
9162 return false;
9166 /* If TYPE is an array of unknown bound, or (possibly cv-qualified)
9167 void, or a complete type, returns true, otherwise false. */
9169 static bool
9170 check_trait_type (tree type)
9172 if (type == NULL_TREE)
9173 return true;
9175 if (TREE_CODE (type) == TREE_LIST)
9176 return (check_trait_type (TREE_VALUE (type))
9177 && check_trait_type (TREE_CHAIN (type)));
9179 if (TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type)
9180 && COMPLETE_TYPE_P (TREE_TYPE (type)))
9181 return true;
9183 if (VOID_TYPE_P (type))
9184 return true;
9186 return !!complete_type_or_else (strip_array_types (type), NULL_TREE);
9189 /* Process a trait expression. */
9191 tree
9192 finish_trait_expr (cp_trait_kind kind, tree type1, tree type2)
9194 if (type1 == error_mark_node
9195 || type2 == error_mark_node)
9196 return error_mark_node;
9198 if (processing_template_decl)
9200 tree trait_expr = make_node (TRAIT_EXPR);
9201 TREE_TYPE (trait_expr) = boolean_type_node;
9202 TRAIT_EXPR_TYPE1 (trait_expr) = type1;
9203 TRAIT_EXPR_TYPE2 (trait_expr) = type2;
9204 TRAIT_EXPR_KIND (trait_expr) = kind;
9205 return trait_expr;
9208 switch (kind)
9210 case CPTK_HAS_NOTHROW_ASSIGN:
9211 case CPTK_HAS_TRIVIAL_ASSIGN:
9212 case CPTK_HAS_NOTHROW_CONSTRUCTOR:
9213 case CPTK_HAS_TRIVIAL_CONSTRUCTOR:
9214 case CPTK_HAS_NOTHROW_COPY:
9215 case CPTK_HAS_TRIVIAL_COPY:
9216 case CPTK_HAS_TRIVIAL_DESTRUCTOR:
9217 case CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS:
9218 case CPTK_HAS_VIRTUAL_DESTRUCTOR:
9219 case CPTK_IS_ABSTRACT:
9220 case CPTK_IS_AGGREGATE:
9221 case CPTK_IS_EMPTY:
9222 case CPTK_IS_FINAL:
9223 case CPTK_IS_LITERAL_TYPE:
9224 case CPTK_IS_POD:
9225 case CPTK_IS_POLYMORPHIC:
9226 case CPTK_IS_STD_LAYOUT:
9227 case CPTK_IS_TRIVIAL:
9228 case CPTK_IS_TRIVIALLY_COPYABLE:
9229 if (!check_trait_type (type1))
9230 return error_mark_node;
9231 break;
9233 case CPTK_IS_ASSIGNABLE:
9234 case CPTK_IS_CONSTRUCTIBLE:
9235 break;
9237 case CPTK_IS_TRIVIALLY_ASSIGNABLE:
9238 case CPTK_IS_TRIVIALLY_CONSTRUCTIBLE:
9239 if (!check_trait_type (type1)
9240 || !check_trait_type (type2))
9241 return error_mark_node;
9242 break;
9244 case CPTK_IS_BASE_OF:
9245 if (NON_UNION_CLASS_TYPE_P (type1) && NON_UNION_CLASS_TYPE_P (type2)
9246 && !same_type_ignoring_top_level_qualifiers_p (type1, type2)
9247 && !complete_type_or_else (type2, NULL_TREE))
9248 /* We already issued an error. */
9249 return error_mark_node;
9250 break;
9252 case CPTK_IS_CLASS:
9253 case CPTK_IS_ENUM:
9254 case CPTK_IS_UNION:
9255 case CPTK_IS_SAME_AS:
9256 break;
9258 default:
9259 gcc_unreachable ();
9262 return (trait_expr_value (kind, type1, type2)
9263 ? boolean_true_node : boolean_false_node);
9266 /* Do-nothing variants of functions to handle pragma FLOAT_CONST_DECIMAL64,
9267 which is ignored for C++. */
9269 void
9270 set_float_const_decimal64 (void)
9274 void
9275 clear_float_const_decimal64 (void)
9279 bool
9280 float_const_decimal64_p (void)
9282 return 0;
9286 /* Return true if T designates the implied `this' parameter. */
9288 bool
9289 is_this_parameter (tree t)
9291 if (!DECL_P (t) || DECL_NAME (t) != this_identifier)
9292 return false;
9293 gcc_assert (TREE_CODE (t) == PARM_DECL || is_capture_proxy (t)
9294 || (cp_binding_oracle && TREE_CODE (t) == VAR_DECL));
9295 return true;
9298 /* Insert the deduced return type for an auto function. */
9300 void
9301 apply_deduced_return_type (tree fco, tree return_type)
9303 tree result;
9305 if (return_type == error_mark_node)
9306 return;
9308 if (DECL_CONV_FN_P (fco))
9309 DECL_NAME (fco) = make_conv_op_name (return_type);
9311 TREE_TYPE (fco) = change_return_type (return_type, TREE_TYPE (fco));
9313 result = DECL_RESULT (fco);
9314 if (result == NULL_TREE)
9315 return;
9316 if (TREE_TYPE (result) == return_type)
9317 return;
9319 if (!processing_template_decl && !VOID_TYPE_P (return_type)
9320 && !complete_type_or_else (return_type, NULL_TREE))
9321 return;
9323 /* We already have a DECL_RESULT from start_preparsed_function.
9324 Now we need to redo the work it and allocate_struct_function
9325 did to reflect the new type. */
9326 gcc_assert (current_function_decl == fco);
9327 result = build_decl (input_location, RESULT_DECL, NULL_TREE,
9328 TYPE_MAIN_VARIANT (return_type));
9329 DECL_ARTIFICIAL (result) = 1;
9330 DECL_IGNORED_P (result) = 1;
9331 cp_apply_type_quals_to_decl (cp_type_quals (return_type),
9332 result);
9334 DECL_RESULT (fco) = result;
9336 if (!processing_template_decl)
9338 bool aggr = aggregate_value_p (result, fco);
9339 #ifdef PCC_STATIC_STRUCT_RETURN
9340 cfun->returns_pcc_struct = aggr;
9341 #endif
9342 cfun->returns_struct = aggr;
9347 /* DECL is a local variable or parameter from the surrounding scope of a
9348 lambda-expression. Returns the decltype for a use of the capture field
9349 for DECL even if it hasn't been captured yet. */
9351 static tree
9352 capture_decltype (tree decl)
9354 tree lam = CLASSTYPE_LAMBDA_EXPR (DECL_CONTEXT (current_function_decl));
9355 /* FIXME do lookup instead of list walk? */
9356 tree cap = value_member (decl, LAMBDA_EXPR_CAPTURE_LIST (lam));
9357 tree type;
9359 if (cap)
9360 type = TREE_TYPE (TREE_PURPOSE (cap));
9361 else
9362 switch (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lam))
9364 case CPLD_NONE:
9365 error ("%qD is not captured", decl);
9366 return error_mark_node;
9368 case CPLD_COPY:
9369 type = TREE_TYPE (decl);
9370 if (TREE_CODE (type) == REFERENCE_TYPE
9371 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
9372 type = TREE_TYPE (type);
9373 break;
9375 case CPLD_REFERENCE:
9376 type = TREE_TYPE (decl);
9377 if (TREE_CODE (type) != REFERENCE_TYPE)
9378 type = build_reference_type (TREE_TYPE (decl));
9379 break;
9381 default:
9382 gcc_unreachable ();
9385 if (TREE_CODE (type) != REFERENCE_TYPE)
9387 if (!LAMBDA_EXPR_MUTABLE_P (lam))
9388 type = cp_build_qualified_type (type, (cp_type_quals (type)
9389 |TYPE_QUAL_CONST));
9390 type = build_reference_type (type);
9392 return type;
9395 /* Build a unary fold expression of EXPR over OP. If IS_RIGHT is true,
9396 this is a right unary fold. Otherwise it is a left unary fold. */
9398 static tree
9399 finish_unary_fold_expr (tree expr, int op, tree_code dir)
9401 // Build a pack expansion (assuming expr has pack type).
9402 if (!uses_parameter_packs (expr))
9404 error_at (location_of (expr), "operand of fold expression has no "
9405 "unexpanded parameter packs");
9406 return error_mark_node;
9408 tree pack = make_pack_expansion (expr);
9410 // Build the fold expression.
9411 tree code = build_int_cstu (integer_type_node, abs (op));
9412 tree fold = build_min_nt_loc (UNKNOWN_LOCATION, dir, code, pack);
9413 FOLD_EXPR_MODIFY_P (fold) = (op < 0);
9414 return fold;
9417 tree
9418 finish_left_unary_fold_expr (tree expr, int op)
9420 return finish_unary_fold_expr (expr, op, UNARY_LEFT_FOLD_EXPR);
9423 tree
9424 finish_right_unary_fold_expr (tree expr, int op)
9426 return finish_unary_fold_expr (expr, op, UNARY_RIGHT_FOLD_EXPR);
9429 /* Build a binary fold expression over EXPR1 and EXPR2. The
9430 associativity of the fold is determined by EXPR1 and EXPR2 (whichever
9431 has an unexpanded parameter pack). */
9433 tree
9434 finish_binary_fold_expr (tree pack, tree init, int op, tree_code dir)
9436 pack = make_pack_expansion (pack);
9437 tree code = build_int_cstu (integer_type_node, abs (op));
9438 tree fold = build_min_nt_loc (UNKNOWN_LOCATION, dir, code, pack, init);
9439 FOLD_EXPR_MODIFY_P (fold) = (op < 0);
9440 return fold;
9443 tree
9444 finish_binary_fold_expr (tree expr1, tree expr2, int op)
9446 // Determine which expr has an unexpanded parameter pack and
9447 // set the pack and initial term.
9448 bool pack1 = uses_parameter_packs (expr1);
9449 bool pack2 = uses_parameter_packs (expr2);
9450 if (pack1 && !pack2)
9451 return finish_binary_fold_expr (expr1, expr2, op, BINARY_RIGHT_FOLD_EXPR);
9452 else if (pack2 && !pack1)
9453 return finish_binary_fold_expr (expr2, expr1, op, BINARY_LEFT_FOLD_EXPR);
9454 else
9456 if (pack1)
9457 error ("both arguments in binary fold have unexpanded parameter packs");
9458 else
9459 error ("no unexpanded parameter packs in binary fold");
9461 return error_mark_node;
9464 /* Finish __builtin_launder (arg). */
9466 tree
9467 finish_builtin_launder (location_t loc, tree arg, tsubst_flags_t complain)
9469 tree orig_arg = arg;
9470 if (!type_dependent_expression_p (arg))
9471 arg = decay_conversion (arg, complain);
9472 if (error_operand_p (arg))
9473 return error_mark_node;
9474 if (!type_dependent_expression_p (arg)
9475 && TREE_CODE (TREE_TYPE (arg)) != POINTER_TYPE)
9477 error_at (loc, "non-pointer argument to %<__builtin_launder%>");
9478 return error_mark_node;
9480 if (processing_template_decl)
9481 arg = orig_arg;
9482 return build_call_expr_internal_loc (loc, IFN_LAUNDER,
9483 TREE_TYPE (arg), 1, arg);
9486 #include "gt-cp-semantics.h"