[gcc]
[official-gcc.git] / gcc / cp / semantics.c
blob3a3ae55aa4417776439fc83a076b8615619eb681
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 && uses_template_parms (DECL_TI_ARGS (containing_function)))
3351 return decl;
3353 /* Core issue 696: "[At the July 2009 meeting] the CWG expressed
3354 support for an approach in which a reference to a local
3355 [constant] automatic variable in a nested class or lambda body
3356 would enter the expression as an rvalue, which would reduce
3357 the complexity of the problem"
3359 FIXME update for final resolution of core issue 696. */
3360 if (decl_constant_var_p (decl))
3362 tree t = maybe_constant_value (convert_from_reference (decl));
3363 if (TREE_CONSTANT (t))
3364 return t;
3367 if (lambda_expr && VAR_P (decl)
3368 && DECL_ANON_UNION_VAR_P (decl))
3370 if (complain & tf_error)
3371 error ("cannot capture member %qD of anonymous union", decl);
3372 return error_mark_node;
3374 if (context == containing_function)
3376 decl = add_default_capture (lambda_stack,
3377 /*id=*/DECL_NAME (decl),
3378 initializer);
3380 else if (lambda_expr)
3382 if (complain & tf_error)
3384 error ("%qD is not captured", decl);
3385 tree closure = LAMBDA_EXPR_CLOSURE (lambda_expr);
3386 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr)
3387 == CPLD_NONE)
3388 inform (location_of (closure),
3389 "the lambda has no capture-default");
3390 else if (TYPE_CLASS_SCOPE_P (closure))
3391 inform (0, "lambda in local class %q+T cannot "
3392 "capture variables from the enclosing context",
3393 TYPE_CONTEXT (closure));
3394 inform (DECL_SOURCE_LOCATION (decl), "%q#D declared here", decl);
3396 return error_mark_node;
3398 else
3400 if (complain & tf_error)
3402 error (VAR_P (decl)
3403 ? G_("use of local variable with automatic storage from "
3404 "containing function")
3405 : G_("use of parameter from containing function"));
3406 inform (DECL_SOURCE_LOCATION (decl), "%q#D declared here", decl);
3408 return error_mark_node;
3410 return decl;
3413 /* ID_EXPRESSION is a representation of parsed, but unprocessed,
3414 id-expression. (See cp_parser_id_expression for details.) SCOPE,
3415 if non-NULL, is the type or namespace used to explicitly qualify
3416 ID_EXPRESSION. DECL is the entity to which that name has been
3417 resolved.
3419 *CONSTANT_EXPRESSION_P is true if we are presently parsing a
3420 constant-expression. In that case, *NON_CONSTANT_EXPRESSION_P will
3421 be set to true if this expression isn't permitted in a
3422 constant-expression, but it is otherwise not set by this function.
3423 *ALLOW_NON_CONSTANT_EXPRESSION_P is true if we are parsing a
3424 constant-expression, but a non-constant expression is also
3425 permissible.
3427 DONE is true if this expression is a complete postfix-expression;
3428 it is false if this expression is followed by '->', '[', '(', etc.
3429 ADDRESS_P is true iff this expression is the operand of '&'.
3430 TEMPLATE_P is true iff the qualified-id was of the form
3431 "A::template B". TEMPLATE_ARG_P is true iff this qualified name
3432 appears as a template argument.
3434 If an error occurs, and it is the kind of error that might cause
3435 the parser to abort a tentative parse, *ERROR_MSG is filled in. It
3436 is the caller's responsibility to issue the message. *ERROR_MSG
3437 will be a string with static storage duration, so the caller need
3438 not "free" it.
3440 Return an expression for the entity, after issuing appropriate
3441 diagnostics. This function is also responsible for transforming a
3442 reference to a non-static member into a COMPONENT_REF that makes
3443 the use of "this" explicit.
3445 Upon return, *IDK will be filled in appropriately. */
3446 cp_expr
3447 finish_id_expression (tree id_expression,
3448 tree decl,
3449 tree scope,
3450 cp_id_kind *idk,
3451 bool integral_constant_expression_p,
3452 bool allow_non_integral_constant_expression_p,
3453 bool *non_integral_constant_expression_p,
3454 bool template_p,
3455 bool done,
3456 bool address_p,
3457 bool template_arg_p,
3458 const char **error_msg,
3459 location_t location)
3461 decl = strip_using_decl (decl);
3463 /* Initialize the output parameters. */
3464 *idk = CP_ID_KIND_NONE;
3465 *error_msg = NULL;
3467 if (id_expression == error_mark_node)
3468 return error_mark_node;
3469 /* If we have a template-id, then no further lookup is
3470 required. If the template-id was for a template-class, we
3471 will sometimes have a TYPE_DECL at this point. */
3472 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3473 || TREE_CODE (decl) == TYPE_DECL)
3475 /* Look up the name. */
3476 else
3478 if (decl == error_mark_node)
3480 /* Name lookup failed. */
3481 if (scope
3482 && (!TYPE_P (scope)
3483 || (!dependent_type_p (scope)
3484 && !(identifier_p (id_expression)
3485 && IDENTIFIER_CONV_OP_P (id_expression)
3486 && dependent_type_p (TREE_TYPE (id_expression))))))
3488 /* If the qualifying type is non-dependent (and the name
3489 does not name a conversion operator to a dependent
3490 type), issue an error. */
3491 qualified_name_lookup_error (scope, id_expression, decl, location);
3492 return error_mark_node;
3494 else if (!scope)
3496 /* It may be resolved via Koenig lookup. */
3497 *idk = CP_ID_KIND_UNQUALIFIED;
3498 return id_expression;
3500 else
3501 decl = id_expression;
3503 /* If DECL is a variable that would be out of scope under
3504 ANSI/ISO rules, but in scope in the ARM, name lookup
3505 will succeed. Issue a diagnostic here. */
3506 else
3507 decl = check_for_out_of_scope_variable (decl);
3509 /* Remember that the name was used in the definition of
3510 the current class so that we can check later to see if
3511 the meaning would have been different after the class
3512 was entirely defined. */
3513 if (!scope && decl != error_mark_node && identifier_p (id_expression))
3514 maybe_note_name_used_in_class (id_expression, decl);
3516 /* A use in unevaluated operand might not be instantiated appropriately
3517 if tsubst_copy builds a dummy parm, or if we never instantiate a
3518 generic lambda, so mark it now. */
3519 if (processing_template_decl && cp_unevaluated_operand)
3520 mark_type_use (decl);
3522 /* Disallow uses of local variables from containing functions, except
3523 within lambda-expressions. */
3524 if (outer_automatic_var_p (decl))
3526 decl = process_outer_var_ref (decl, tf_warning_or_error);
3527 if (decl == error_mark_node)
3528 return error_mark_node;
3531 /* Also disallow uses of function parameters outside the function
3532 body, except inside an unevaluated context (i.e. decltype). */
3533 if (TREE_CODE (decl) == PARM_DECL
3534 && DECL_CONTEXT (decl) == NULL_TREE
3535 && !cp_unevaluated_operand)
3537 *error_msg = G_("use of parameter outside function body");
3538 return error_mark_node;
3542 /* If we didn't find anything, or what we found was a type,
3543 then this wasn't really an id-expression. */
3544 if (TREE_CODE (decl) == TEMPLATE_DECL
3545 && !DECL_FUNCTION_TEMPLATE_P (decl))
3547 *error_msg = G_("missing template arguments");
3548 return error_mark_node;
3550 else if (TREE_CODE (decl) == TYPE_DECL
3551 || TREE_CODE (decl) == NAMESPACE_DECL)
3553 *error_msg = G_("expected primary-expression");
3554 return error_mark_node;
3557 /* If the name resolved to a template parameter, there is no
3558 need to look it up again later. */
3559 if ((TREE_CODE (decl) == CONST_DECL && DECL_TEMPLATE_PARM_P (decl))
3560 || TREE_CODE (decl) == TEMPLATE_PARM_INDEX)
3562 tree r;
3564 *idk = CP_ID_KIND_NONE;
3565 if (TREE_CODE (decl) == TEMPLATE_PARM_INDEX)
3566 decl = TEMPLATE_PARM_DECL (decl);
3567 r = convert_from_reference (DECL_INITIAL (decl));
3569 if (integral_constant_expression_p
3570 && !dependent_type_p (TREE_TYPE (decl))
3571 && !(INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (r))))
3573 if (!allow_non_integral_constant_expression_p)
3574 error ("template parameter %qD of type %qT is not allowed in "
3575 "an integral constant expression because it is not of "
3576 "integral or enumeration type", decl, TREE_TYPE (decl));
3577 *non_integral_constant_expression_p = true;
3579 return r;
3581 else
3583 bool dependent_p = type_dependent_expression_p (decl);
3585 /* If the declaration was explicitly qualified indicate
3586 that. The semantics of `A::f(3)' are different than
3587 `f(3)' if `f' is virtual. */
3588 *idk = (scope
3589 ? CP_ID_KIND_QUALIFIED
3590 : (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3591 ? CP_ID_KIND_TEMPLATE_ID
3592 : (dependent_p
3593 ? CP_ID_KIND_UNQUALIFIED_DEPENDENT
3594 : CP_ID_KIND_UNQUALIFIED)));
3596 if (dependent_p
3597 && DECL_P (decl)
3598 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (decl)))
3599 /* Dependent type attributes on the decl mean that the TREE_TYPE is
3600 wrong, so just return the identifier. */
3601 return id_expression;
3603 if (TREE_CODE (decl) == NAMESPACE_DECL)
3605 error ("use of namespace %qD as expression", decl);
3606 return error_mark_node;
3608 else if (DECL_CLASS_TEMPLATE_P (decl))
3610 error ("use of class template %qT as expression", decl);
3611 return error_mark_node;
3613 else if (TREE_CODE (decl) == TREE_LIST)
3615 /* Ambiguous reference to base members. */
3616 error ("request for member %qD is ambiguous in "
3617 "multiple inheritance lattice", id_expression);
3618 print_candidates (decl);
3619 return error_mark_node;
3622 /* Mark variable-like entities as used. Functions are similarly
3623 marked either below or after overload resolution. */
3624 if ((VAR_P (decl)
3625 || TREE_CODE (decl) == PARM_DECL
3626 || TREE_CODE (decl) == CONST_DECL
3627 || TREE_CODE (decl) == RESULT_DECL)
3628 && !mark_used (decl))
3629 return error_mark_node;
3631 /* Only certain kinds of names are allowed in constant
3632 expression. Template parameters have already
3633 been handled above. */
3634 if (! error_operand_p (decl)
3635 && !dependent_p
3636 && integral_constant_expression_p
3637 && ! decl_constant_var_p (decl)
3638 && TREE_CODE (decl) != CONST_DECL
3639 && ! builtin_valid_in_constant_expr_p (decl))
3641 if (!allow_non_integral_constant_expression_p)
3643 error ("%qD cannot appear in a constant-expression", decl);
3644 return error_mark_node;
3646 *non_integral_constant_expression_p = true;
3649 tree wrap;
3650 if (VAR_P (decl)
3651 && !cp_unevaluated_operand
3652 && !processing_template_decl
3653 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl))
3654 && CP_DECL_THREAD_LOCAL_P (decl)
3655 && (wrap = get_tls_wrapper_fn (decl)))
3657 /* Replace an evaluated use of the thread_local variable with
3658 a call to its wrapper. */
3659 decl = build_cxx_call (wrap, 0, NULL, tf_warning_or_error);
3661 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3662 && !dependent_p
3663 && variable_template_p (TREE_OPERAND (decl, 0)))
3665 decl = finish_template_variable (decl);
3666 mark_used (decl);
3667 decl = convert_from_reference (decl);
3669 else if (scope)
3671 if (TREE_CODE (decl) == SCOPE_REF)
3673 gcc_assert (same_type_p (scope, TREE_OPERAND (decl, 0)));
3674 decl = TREE_OPERAND (decl, 1);
3677 decl = (adjust_result_of_qualified_name_lookup
3678 (decl, scope, current_nonlambda_class_type()));
3680 if (TREE_CODE (decl) == FUNCTION_DECL)
3681 mark_used (decl);
3683 if (TYPE_P (scope))
3684 decl = finish_qualified_id_expr (scope,
3685 decl,
3686 done,
3687 address_p,
3688 template_p,
3689 template_arg_p,
3690 tf_warning_or_error);
3691 else
3692 decl = convert_from_reference (decl);
3694 else if (TREE_CODE (decl) == FIELD_DECL)
3696 /* Since SCOPE is NULL here, this is an unqualified name.
3697 Access checking has been performed during name lookup
3698 already. Turn off checking to avoid duplicate errors. */
3699 push_deferring_access_checks (dk_no_check);
3700 decl = finish_non_static_data_member (decl, NULL_TREE,
3701 /*qualifying_scope=*/NULL_TREE);
3702 pop_deferring_access_checks ();
3704 else if (is_overloaded_fn (decl))
3706 tree first_fn = get_first_fn (decl);
3708 if (TREE_CODE (first_fn) == TEMPLATE_DECL)
3709 first_fn = DECL_TEMPLATE_RESULT (first_fn);
3711 /* [basic.def.odr]: "A function whose name appears as a
3712 potentially-evaluated expression is odr-used if it is the unique
3713 lookup result".
3715 But only mark it if it's a complete postfix-expression; in a call,
3716 ADL might select a different function, and we'll call mark_used in
3717 build_over_call. */
3718 if (done
3719 && !really_overloaded_fn (decl)
3720 && !mark_used (first_fn))
3721 return error_mark_node;
3723 if (!template_arg_p
3724 && TREE_CODE (first_fn) == FUNCTION_DECL
3725 && DECL_FUNCTION_MEMBER_P (first_fn)
3726 && !shared_member_p (decl))
3728 /* A set of member functions. */
3729 decl = maybe_dummy_object (DECL_CONTEXT (first_fn), 0);
3730 return finish_class_member_access_expr (decl, id_expression,
3731 /*template_p=*/false,
3732 tf_warning_or_error);
3735 decl = baselink_for_fns (decl);
3737 else
3739 if (DECL_P (decl) && DECL_NONLOCAL (decl)
3740 && DECL_CLASS_SCOPE_P (decl))
3742 tree context = context_for_name_lookup (decl);
3743 if (context != current_class_type)
3745 tree path = currently_open_derived_class (context);
3746 perform_or_defer_access_check (TYPE_BINFO (path),
3747 decl, decl,
3748 tf_warning_or_error);
3752 decl = convert_from_reference (decl);
3756 return cp_expr (decl, location);
3759 /* Implement the __typeof keyword: Return the type of EXPR, suitable for
3760 use as a type-specifier. */
3762 tree
3763 finish_typeof (tree expr)
3765 tree type;
3767 if (type_dependent_expression_p (expr))
3769 type = cxx_make_type (TYPEOF_TYPE);
3770 TYPEOF_TYPE_EXPR (type) = expr;
3771 SET_TYPE_STRUCTURAL_EQUALITY (type);
3773 return type;
3776 expr = mark_type_use (expr);
3778 type = unlowered_expr_type (expr);
3780 if (!type || type == unknown_type_node)
3782 error ("type of %qE is unknown", expr);
3783 return error_mark_node;
3786 return type;
3789 /* Implement the __underlying_type keyword: Return the underlying
3790 type of TYPE, suitable for use as a type-specifier. */
3792 tree
3793 finish_underlying_type (tree type)
3795 tree underlying_type;
3797 if (processing_template_decl)
3799 underlying_type = cxx_make_type (UNDERLYING_TYPE);
3800 UNDERLYING_TYPE_TYPE (underlying_type) = type;
3801 SET_TYPE_STRUCTURAL_EQUALITY (underlying_type);
3803 return underlying_type;
3806 if (!complete_type_or_else (type, NULL_TREE))
3807 return error_mark_node;
3809 if (TREE_CODE (type) != ENUMERAL_TYPE)
3811 error ("%qT is not an enumeration type", type);
3812 return error_mark_node;
3815 underlying_type = ENUM_UNDERLYING_TYPE (type);
3817 /* Fixup necessary in this case because ENUM_UNDERLYING_TYPE
3818 includes TYPE_MIN_VALUE and TYPE_MAX_VALUE information.
3819 See finish_enum_value_list for details. */
3820 if (!ENUM_FIXED_UNDERLYING_TYPE_P (type))
3821 underlying_type
3822 = c_common_type_for_mode (TYPE_MODE (underlying_type),
3823 TYPE_UNSIGNED (underlying_type));
3825 return underlying_type;
3828 /* Implement the __direct_bases keyword: Return the direct base classes
3829 of type */
3831 tree
3832 calculate_direct_bases (tree type)
3834 vec<tree, va_gc> *vector = make_tree_vector();
3835 tree bases_vec = NULL_TREE;
3836 vec<tree, va_gc> *base_binfos;
3837 tree binfo;
3838 unsigned i;
3840 complete_type (type);
3842 if (!NON_UNION_CLASS_TYPE_P (type))
3843 return make_tree_vec (0);
3845 base_binfos = BINFO_BASE_BINFOS (TYPE_BINFO (type));
3847 /* Virtual bases are initialized first */
3848 for (i = 0; base_binfos->iterate (i, &binfo); i++)
3850 if (BINFO_VIRTUAL_P (binfo))
3852 vec_safe_push (vector, binfo);
3856 /* Now non-virtuals */
3857 for (i = 0; base_binfos->iterate (i, &binfo); i++)
3859 if (!BINFO_VIRTUAL_P (binfo))
3861 vec_safe_push (vector, binfo);
3866 bases_vec = make_tree_vec (vector->length ());
3868 for (i = 0; i < vector->length (); ++i)
3870 TREE_VEC_ELT (bases_vec, i) = BINFO_TYPE ((*vector)[i]);
3872 return bases_vec;
3875 /* Implement the __bases keyword: Return the base classes
3876 of type */
3878 /* Find morally non-virtual base classes by walking binfo hierarchy */
3879 /* Virtual base classes are handled separately in finish_bases */
3881 static tree
3882 dfs_calculate_bases_pre (tree binfo, void * /*data_*/)
3884 /* Don't walk bases of virtual bases */
3885 return BINFO_VIRTUAL_P (binfo) ? dfs_skip_bases : NULL_TREE;
3888 static tree
3889 dfs_calculate_bases_post (tree binfo, void *data_)
3891 vec<tree, va_gc> **data = ((vec<tree, va_gc> **) data_);
3892 if (!BINFO_VIRTUAL_P (binfo))
3894 vec_safe_push (*data, BINFO_TYPE (binfo));
3896 return NULL_TREE;
3899 /* Calculates the morally non-virtual base classes of a class */
3900 static vec<tree, va_gc> *
3901 calculate_bases_helper (tree type)
3903 vec<tree, va_gc> *vector = make_tree_vector();
3905 /* Now add non-virtual base classes in order of construction */
3906 if (TYPE_BINFO (type))
3907 dfs_walk_all (TYPE_BINFO (type),
3908 dfs_calculate_bases_pre, dfs_calculate_bases_post, &vector);
3909 return vector;
3912 tree
3913 calculate_bases (tree type)
3915 vec<tree, va_gc> *vector = make_tree_vector();
3916 tree bases_vec = NULL_TREE;
3917 unsigned i;
3918 vec<tree, va_gc> *vbases;
3919 vec<tree, va_gc> *nonvbases;
3920 tree binfo;
3922 complete_type (type);
3924 if (!NON_UNION_CLASS_TYPE_P (type))
3925 return make_tree_vec (0);
3927 /* First go through virtual base classes */
3928 for (vbases = CLASSTYPE_VBASECLASSES (type), i = 0;
3929 vec_safe_iterate (vbases, i, &binfo); i++)
3931 vec<tree, va_gc> *vbase_bases;
3932 vbase_bases = calculate_bases_helper (BINFO_TYPE (binfo));
3933 vec_safe_splice (vector, vbase_bases);
3934 release_tree_vector (vbase_bases);
3937 /* Now for the non-virtual bases */
3938 nonvbases = calculate_bases_helper (type);
3939 vec_safe_splice (vector, nonvbases);
3940 release_tree_vector (nonvbases);
3942 /* Note that during error recovery vector->length can even be zero. */
3943 if (vector->length () > 1)
3945 /* Last element is entire class, so don't copy */
3946 bases_vec = make_tree_vec (vector->length() - 1);
3948 for (i = 0; i < vector->length () - 1; ++i)
3949 TREE_VEC_ELT (bases_vec, i) = (*vector)[i];
3951 else
3952 bases_vec = make_tree_vec (0);
3954 release_tree_vector (vector);
3955 return bases_vec;
3958 tree
3959 finish_bases (tree type, bool direct)
3961 tree bases = NULL_TREE;
3963 if (!processing_template_decl)
3965 /* Parameter packs can only be used in templates */
3966 error ("Parameter pack __bases only valid in template declaration");
3967 return error_mark_node;
3970 bases = cxx_make_type (BASES);
3971 BASES_TYPE (bases) = type;
3972 BASES_DIRECT (bases) = direct;
3973 SET_TYPE_STRUCTURAL_EQUALITY (bases);
3975 return bases;
3978 /* Perform C++-specific checks for __builtin_offsetof before calling
3979 fold_offsetof. */
3981 tree
3982 finish_offsetof (tree object_ptr, tree expr, location_t loc)
3984 /* If we're processing a template, we can't finish the semantics yet.
3985 Otherwise we can fold the entire expression now. */
3986 if (processing_template_decl)
3988 expr = build2 (OFFSETOF_EXPR, size_type_node, expr, object_ptr);
3989 SET_EXPR_LOCATION (expr, loc);
3990 return expr;
3993 if (TREE_CODE (expr) == PSEUDO_DTOR_EXPR)
3995 error ("cannot apply %<offsetof%> to destructor %<~%T%>",
3996 TREE_OPERAND (expr, 2));
3997 return error_mark_node;
3999 if (TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE
4000 || TREE_CODE (TREE_TYPE (expr)) == METHOD_TYPE
4001 || TREE_TYPE (expr) == unknown_type_node)
4003 if (INDIRECT_REF_P (expr))
4004 error ("second operand of %<offsetof%> is neither a single "
4005 "identifier nor a sequence of member accesses and "
4006 "array references");
4007 else
4009 if (TREE_CODE (expr) == COMPONENT_REF
4010 || TREE_CODE (expr) == COMPOUND_EXPR)
4011 expr = TREE_OPERAND (expr, 1);
4012 error ("cannot apply %<offsetof%> to member function %qD", expr);
4014 return error_mark_node;
4016 if (REFERENCE_REF_P (expr))
4017 expr = TREE_OPERAND (expr, 0);
4018 if (!complete_type_or_else (TREE_TYPE (TREE_TYPE (object_ptr)), object_ptr))
4019 return error_mark_node;
4020 if (warn_invalid_offsetof
4021 && CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (object_ptr)))
4022 && CLASSTYPE_NON_STD_LAYOUT (TREE_TYPE (TREE_TYPE (object_ptr)))
4023 && cp_unevaluated_operand == 0)
4024 pedwarn (loc, OPT_Winvalid_offsetof,
4025 "offsetof within non-standard-layout type %qT is undefined",
4026 TREE_TYPE (TREE_TYPE (object_ptr)));
4027 return fold_offsetof (expr);
4030 /* Replace the AGGR_INIT_EXPR at *TP with an equivalent CALL_EXPR. This
4031 function is broken out from the above for the benefit of the tree-ssa
4032 project. */
4034 void
4035 simplify_aggr_init_expr (tree *tp)
4037 tree aggr_init_expr = *tp;
4039 /* Form an appropriate CALL_EXPR. */
4040 tree fn = AGGR_INIT_EXPR_FN (aggr_init_expr);
4041 tree slot = AGGR_INIT_EXPR_SLOT (aggr_init_expr);
4042 tree type = TREE_TYPE (slot);
4044 tree call_expr;
4045 enum style_t { ctor, arg, pcc } style;
4047 if (AGGR_INIT_VIA_CTOR_P (aggr_init_expr))
4048 style = ctor;
4049 #ifdef PCC_STATIC_STRUCT_RETURN
4050 else if (1)
4051 style = pcc;
4052 #endif
4053 else
4055 gcc_assert (TREE_ADDRESSABLE (type));
4056 style = arg;
4059 call_expr = build_call_array_loc (input_location,
4060 TREE_TYPE (TREE_TYPE (TREE_TYPE (fn))),
4062 aggr_init_expr_nargs (aggr_init_expr),
4063 AGGR_INIT_EXPR_ARGP (aggr_init_expr));
4064 TREE_NOTHROW (call_expr) = TREE_NOTHROW (aggr_init_expr);
4065 CALL_FROM_THUNK_P (call_expr) = AGGR_INIT_FROM_THUNK_P (aggr_init_expr);
4066 CALL_EXPR_OPERATOR_SYNTAX (call_expr)
4067 = CALL_EXPR_OPERATOR_SYNTAX (aggr_init_expr);
4068 CALL_EXPR_ORDERED_ARGS (call_expr) = CALL_EXPR_ORDERED_ARGS (aggr_init_expr);
4069 CALL_EXPR_REVERSE_ARGS (call_expr) = CALL_EXPR_REVERSE_ARGS (aggr_init_expr);
4070 /* Preserve CILK_SPAWN flag. */
4071 EXPR_CILK_SPAWN (call_expr) = EXPR_CILK_SPAWN (aggr_init_expr);
4073 if (style == ctor)
4075 /* Replace the first argument to the ctor with the address of the
4076 slot. */
4077 cxx_mark_addressable (slot);
4078 CALL_EXPR_ARG (call_expr, 0) =
4079 build1 (ADDR_EXPR, build_pointer_type (type), slot);
4081 else if (style == arg)
4083 /* Just mark it addressable here, and leave the rest to
4084 expand_call{,_inline}. */
4085 cxx_mark_addressable (slot);
4086 CALL_EXPR_RETURN_SLOT_OPT (call_expr) = true;
4087 call_expr = build2 (INIT_EXPR, TREE_TYPE (call_expr), slot, call_expr);
4089 else if (style == pcc)
4091 /* If we're using the non-reentrant PCC calling convention, then we
4092 need to copy the returned value out of the static buffer into the
4093 SLOT. */
4094 push_deferring_access_checks (dk_no_check);
4095 call_expr = build_aggr_init (slot, call_expr,
4096 DIRECT_BIND | LOOKUP_ONLYCONVERTING,
4097 tf_warning_or_error);
4098 pop_deferring_access_checks ();
4099 call_expr = build2 (COMPOUND_EXPR, TREE_TYPE (slot), call_expr, slot);
4102 if (AGGR_INIT_ZERO_FIRST (aggr_init_expr))
4104 tree init = build_zero_init (type, NULL_TREE,
4105 /*static_storage_p=*/false);
4106 init = build2 (INIT_EXPR, void_type_node, slot, init);
4107 call_expr = build2 (COMPOUND_EXPR, TREE_TYPE (call_expr),
4108 init, call_expr);
4111 *tp = call_expr;
4114 /* Emit all thunks to FN that should be emitted when FN is emitted. */
4116 void
4117 emit_associated_thunks (tree fn)
4119 /* When we use vcall offsets, we emit thunks with the virtual
4120 functions to which they thunk. The whole point of vcall offsets
4121 is so that you can know statically the entire set of thunks that
4122 will ever be needed for a given virtual function, thereby
4123 enabling you to output all the thunks with the function itself. */
4124 if (DECL_VIRTUAL_P (fn)
4125 /* Do not emit thunks for extern template instantiations. */
4126 && ! DECL_REALLY_EXTERN (fn))
4128 tree thunk;
4130 for (thunk = DECL_THUNKS (fn); thunk; thunk = DECL_CHAIN (thunk))
4132 if (!THUNK_ALIAS (thunk))
4134 use_thunk (thunk, /*emit_p=*/1);
4135 if (DECL_RESULT_THUNK_P (thunk))
4137 tree probe;
4139 for (probe = DECL_THUNKS (thunk);
4140 probe; probe = DECL_CHAIN (probe))
4141 use_thunk (probe, /*emit_p=*/1);
4144 else
4145 gcc_assert (!DECL_THUNKS (thunk));
4150 /* Generate RTL for FN. */
4152 bool
4153 expand_or_defer_fn_1 (tree fn)
4155 /* When the parser calls us after finishing the body of a template
4156 function, we don't really want to expand the body. */
4157 if (processing_template_decl)
4159 /* Normally, collection only occurs in rest_of_compilation. So,
4160 if we don't collect here, we never collect junk generated
4161 during the processing of templates until we hit a
4162 non-template function. It's not safe to do this inside a
4163 nested class, though, as the parser may have local state that
4164 is not a GC root. */
4165 if (!function_depth)
4166 ggc_collect ();
4167 return false;
4170 gcc_assert (DECL_SAVED_TREE (fn));
4172 /* We make a decision about linkage for these functions at the end
4173 of the compilation. Until that point, we do not want the back
4174 end to output them -- but we do want it to see the bodies of
4175 these functions so that it can inline them as appropriate. */
4176 if (DECL_DECLARED_INLINE_P (fn) || DECL_IMPLICIT_INSTANTIATION (fn))
4178 if (DECL_INTERFACE_KNOWN (fn))
4179 /* We've already made a decision as to how this function will
4180 be handled. */;
4181 else if (!at_eof)
4182 tentative_decl_linkage (fn);
4183 else
4184 import_export_decl (fn);
4186 /* If the user wants us to keep all inline functions, then mark
4187 this function as needed so that finish_file will make sure to
4188 output it later. Similarly, all dllexport'd functions must
4189 be emitted; there may be callers in other DLLs. */
4190 if (DECL_DECLARED_INLINE_P (fn)
4191 && !DECL_REALLY_EXTERN (fn)
4192 && (flag_keep_inline_functions
4193 || (flag_keep_inline_dllexport
4194 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (fn)))))
4196 mark_needed (fn);
4197 DECL_EXTERNAL (fn) = 0;
4201 /* If this is a constructor or destructor body, we have to clone
4202 it. */
4203 if (maybe_clone_body (fn))
4205 /* We don't want to process FN again, so pretend we've written
4206 it out, even though we haven't. */
4207 TREE_ASM_WRITTEN (fn) = 1;
4208 /* If this is a constexpr function, keep DECL_SAVED_TREE. */
4209 if (!DECL_DECLARED_CONSTEXPR_P (fn))
4210 DECL_SAVED_TREE (fn) = NULL_TREE;
4211 return false;
4214 /* There's no reason to do any of the work here if we're only doing
4215 semantic analysis; this code just generates RTL. */
4216 if (flag_syntax_only)
4217 return false;
4219 return true;
4222 void
4223 expand_or_defer_fn (tree fn)
4225 if (expand_or_defer_fn_1 (fn))
4227 function_depth++;
4229 /* Expand or defer, at the whim of the compilation unit manager. */
4230 cgraph_node::finalize_function (fn, function_depth > 1);
4231 emit_associated_thunks (fn);
4233 function_depth--;
4237 struct nrv_data
4239 nrv_data () : visited (37) {}
4241 tree var;
4242 tree result;
4243 hash_table<nofree_ptr_hash <tree_node> > visited;
4246 /* Helper function for walk_tree, used by finalize_nrv below. */
4248 static tree
4249 finalize_nrv_r (tree* tp, int* walk_subtrees, void* data)
4251 struct nrv_data *dp = (struct nrv_data *)data;
4252 tree_node **slot;
4254 /* No need to walk into types. There wouldn't be any need to walk into
4255 non-statements, except that we have to consider STMT_EXPRs. */
4256 if (TYPE_P (*tp))
4257 *walk_subtrees = 0;
4258 /* Change all returns to just refer to the RESULT_DECL; this is a nop,
4259 but differs from using NULL_TREE in that it indicates that we care
4260 about the value of the RESULT_DECL. */
4261 else if (TREE_CODE (*tp) == RETURN_EXPR)
4262 TREE_OPERAND (*tp, 0) = dp->result;
4263 /* Change all cleanups for the NRV to only run when an exception is
4264 thrown. */
4265 else if (TREE_CODE (*tp) == CLEANUP_STMT
4266 && CLEANUP_DECL (*tp) == dp->var)
4267 CLEANUP_EH_ONLY (*tp) = 1;
4268 /* Replace the DECL_EXPR for the NRV with an initialization of the
4269 RESULT_DECL, if needed. */
4270 else if (TREE_CODE (*tp) == DECL_EXPR
4271 && DECL_EXPR_DECL (*tp) == dp->var)
4273 tree init;
4274 if (DECL_INITIAL (dp->var)
4275 && DECL_INITIAL (dp->var) != error_mark_node)
4276 init = build2 (INIT_EXPR, void_type_node, dp->result,
4277 DECL_INITIAL (dp->var));
4278 else
4279 init = build_empty_stmt (EXPR_LOCATION (*tp));
4280 DECL_INITIAL (dp->var) = NULL_TREE;
4281 SET_EXPR_LOCATION (init, EXPR_LOCATION (*tp));
4282 *tp = init;
4284 /* And replace all uses of the NRV with the RESULT_DECL. */
4285 else if (*tp == dp->var)
4286 *tp = dp->result;
4288 /* Avoid walking into the same tree more than once. Unfortunately, we
4289 can't just use walk_tree_without duplicates because it would only call
4290 us for the first occurrence of dp->var in the function body. */
4291 slot = dp->visited.find_slot (*tp, INSERT);
4292 if (*slot)
4293 *walk_subtrees = 0;
4294 else
4295 *slot = *tp;
4297 /* Keep iterating. */
4298 return NULL_TREE;
4301 /* Called from finish_function to implement the named return value
4302 optimization by overriding all the RETURN_EXPRs and pertinent
4303 CLEANUP_STMTs and replacing all occurrences of VAR with RESULT, the
4304 RESULT_DECL for the function. */
4306 void
4307 finalize_nrv (tree *tp, tree var, tree result)
4309 struct nrv_data data;
4311 /* Copy name from VAR to RESULT. */
4312 DECL_NAME (result) = DECL_NAME (var);
4313 /* Don't forget that we take its address. */
4314 TREE_ADDRESSABLE (result) = TREE_ADDRESSABLE (var);
4315 /* Finally set DECL_VALUE_EXPR to avoid assigning
4316 a stack slot at -O0 for the original var and debug info
4317 uses RESULT location for VAR. */
4318 SET_DECL_VALUE_EXPR (var, result);
4319 DECL_HAS_VALUE_EXPR_P (var) = 1;
4321 data.var = var;
4322 data.result = result;
4323 cp_walk_tree (tp, finalize_nrv_r, &data, 0);
4326 /* Create CP_OMP_CLAUSE_INFO for clause C. Returns true if it is invalid. */
4328 bool
4329 cxx_omp_create_clause_info (tree c, tree type, bool need_default_ctor,
4330 bool need_copy_ctor, bool need_copy_assignment,
4331 bool need_dtor)
4333 int save_errorcount = errorcount;
4334 tree info, t;
4336 /* Always allocate 3 elements for simplicity. These are the
4337 function decls for the ctor, dtor, and assignment op.
4338 This layout is known to the three lang hooks,
4339 cxx_omp_clause_default_init, cxx_omp_clause_copy_init,
4340 and cxx_omp_clause_assign_op. */
4341 info = make_tree_vec (3);
4342 CP_OMP_CLAUSE_INFO (c) = info;
4344 if (need_default_ctor || need_copy_ctor)
4346 if (need_default_ctor)
4347 t = get_default_ctor (type);
4348 else
4349 t = get_copy_ctor (type, tf_warning_or_error);
4351 if (t && !trivial_fn_p (t))
4352 TREE_VEC_ELT (info, 0) = t;
4355 if (need_dtor && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
4356 TREE_VEC_ELT (info, 1) = get_dtor (type, tf_warning_or_error);
4358 if (need_copy_assignment)
4360 t = get_copy_assign (type);
4362 if (t && !trivial_fn_p (t))
4363 TREE_VEC_ELT (info, 2) = t;
4366 return errorcount != save_errorcount;
4369 /* If DECL is DECL_OMP_PRIVATIZED_MEMBER, return corresponding
4370 FIELD_DECL, otherwise return DECL itself. */
4372 static tree
4373 omp_clause_decl_field (tree decl)
4375 if (VAR_P (decl)
4376 && DECL_HAS_VALUE_EXPR_P (decl)
4377 && DECL_ARTIFICIAL (decl)
4378 && DECL_LANG_SPECIFIC (decl)
4379 && DECL_OMP_PRIVATIZED_MEMBER (decl))
4381 tree f = DECL_VALUE_EXPR (decl);
4382 if (TREE_CODE (f) == INDIRECT_REF)
4383 f = TREE_OPERAND (f, 0);
4384 if (TREE_CODE (f) == COMPONENT_REF)
4386 f = TREE_OPERAND (f, 1);
4387 gcc_assert (TREE_CODE (f) == FIELD_DECL);
4388 return f;
4391 return NULL_TREE;
4394 /* Adjust DECL if needed for printing using %qE. */
4396 static tree
4397 omp_clause_printable_decl (tree decl)
4399 tree t = omp_clause_decl_field (decl);
4400 if (t)
4401 return t;
4402 return decl;
4405 /* For a FIELD_DECL F and corresponding DECL_OMP_PRIVATIZED_MEMBER
4406 VAR_DECL T that doesn't need a DECL_EXPR added, record it for
4407 privatization. */
4409 static void
4410 omp_note_field_privatization (tree f, tree t)
4412 if (!omp_private_member_map)
4413 omp_private_member_map = new hash_map<tree, tree>;
4414 tree &v = omp_private_member_map->get_or_insert (f);
4415 if (v == NULL_TREE)
4417 v = t;
4418 omp_private_member_vec.safe_push (f);
4419 /* Signal that we don't want to create DECL_EXPR for this dummy var. */
4420 omp_private_member_vec.safe_push (integer_zero_node);
4424 /* Privatize FIELD_DECL T, return corresponding DECL_OMP_PRIVATIZED_MEMBER
4425 dummy VAR_DECL. */
4427 tree
4428 omp_privatize_field (tree t, bool shared)
4430 tree m = finish_non_static_data_member (t, NULL_TREE, NULL_TREE);
4431 if (m == error_mark_node)
4432 return error_mark_node;
4433 if (!omp_private_member_map && !shared)
4434 omp_private_member_map = new hash_map<tree, tree>;
4435 if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE)
4437 gcc_assert (TREE_CODE (m) == INDIRECT_REF);
4438 m = TREE_OPERAND (m, 0);
4440 tree vb = NULL_TREE;
4441 tree &v = shared ? vb : omp_private_member_map->get_or_insert (t);
4442 if (v == NULL_TREE)
4444 v = create_temporary_var (TREE_TYPE (m));
4445 retrofit_lang_decl (v);
4446 DECL_OMP_PRIVATIZED_MEMBER (v) = 1;
4447 SET_DECL_VALUE_EXPR (v, m);
4448 DECL_HAS_VALUE_EXPR_P (v) = 1;
4449 if (!shared)
4450 omp_private_member_vec.safe_push (t);
4452 return v;
4455 /* Helper function for handle_omp_array_sections. Called recursively
4456 to handle multiple array-section-subscripts. C is the clause,
4457 T current expression (initially OMP_CLAUSE_DECL), which is either
4458 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
4459 expression if specified, TREE_VALUE length expression if specified,
4460 TREE_CHAIN is what it has been specified after, or some decl.
4461 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
4462 set to true if any of the array-section-subscript could have length
4463 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
4464 first array-section-subscript which is known not to have length
4465 of one. Given say:
4466 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
4467 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
4468 all are or may have length of 1, array-section-subscript [:2] is the
4469 first one known not to have length 1. For array-section-subscript
4470 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
4471 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
4472 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
4473 case though, as some lengths could be zero. */
4475 static tree
4476 handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
4477 bool &maybe_zero_len, unsigned int &first_non_one,
4478 enum c_omp_region_type ort)
4480 tree ret, low_bound, length, type;
4481 if (TREE_CODE (t) != TREE_LIST)
4483 if (error_operand_p (t))
4484 return error_mark_node;
4485 if (REFERENCE_REF_P (t)
4486 && TREE_CODE (TREE_OPERAND (t, 0)) == COMPONENT_REF)
4487 t = TREE_OPERAND (t, 0);
4488 ret = t;
4489 if (TREE_CODE (t) == COMPONENT_REF
4490 && ort == C_ORT_OMP
4491 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
4492 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO
4493 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FROM)
4494 && !type_dependent_expression_p (t))
4496 if (TREE_CODE (TREE_OPERAND (t, 1)) == FIELD_DECL
4497 && DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
4499 error_at (OMP_CLAUSE_LOCATION (c),
4500 "bit-field %qE in %qs clause",
4501 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4502 return error_mark_node;
4504 while (TREE_CODE (t) == COMPONENT_REF)
4506 if (TREE_TYPE (TREE_OPERAND (t, 0))
4507 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == UNION_TYPE)
4509 error_at (OMP_CLAUSE_LOCATION (c),
4510 "%qE is a member of a union", t);
4511 return error_mark_node;
4513 t = TREE_OPERAND (t, 0);
4515 if (REFERENCE_REF_P (t))
4516 t = TREE_OPERAND (t, 0);
4518 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
4520 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
4521 return NULL_TREE;
4522 if (DECL_P (t))
4523 error_at (OMP_CLAUSE_LOCATION (c),
4524 "%qD is not a variable in %qs clause", t,
4525 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4526 else
4527 error_at (OMP_CLAUSE_LOCATION (c),
4528 "%qE is not a variable in %qs clause", t,
4529 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4530 return error_mark_node;
4532 else if (TREE_CODE (t) == PARM_DECL
4533 && DECL_ARTIFICIAL (t)
4534 && DECL_NAME (t) == this_identifier)
4536 error_at (OMP_CLAUSE_LOCATION (c),
4537 "%<this%> allowed in OpenMP only in %<declare simd%>"
4538 " clauses");
4539 return error_mark_node;
4541 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
4542 && VAR_P (t) && CP_DECL_THREAD_LOCAL_P (t))
4544 error_at (OMP_CLAUSE_LOCATION (c),
4545 "%qD is threadprivate variable in %qs clause", t,
4546 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4547 return error_mark_node;
4549 if (type_dependent_expression_p (ret))
4550 return NULL_TREE;
4551 ret = convert_from_reference (ret);
4552 return ret;
4555 if (ort == C_ORT_OMP
4556 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
4557 && TREE_CODE (TREE_CHAIN (t)) == FIELD_DECL)
4558 TREE_CHAIN (t) = omp_privatize_field (TREE_CHAIN (t), false);
4559 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
4560 maybe_zero_len, first_non_one, ort);
4561 if (ret == error_mark_node || ret == NULL_TREE)
4562 return ret;
4564 type = TREE_TYPE (ret);
4565 low_bound = TREE_PURPOSE (t);
4566 length = TREE_VALUE (t);
4567 if ((low_bound && type_dependent_expression_p (low_bound))
4568 || (length && type_dependent_expression_p (length)))
4569 return NULL_TREE;
4571 if (low_bound == error_mark_node || length == error_mark_node)
4572 return error_mark_node;
4574 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
4576 error_at (OMP_CLAUSE_LOCATION (c),
4577 "low bound %qE of array section does not have integral type",
4578 low_bound);
4579 return error_mark_node;
4581 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
4583 error_at (OMP_CLAUSE_LOCATION (c),
4584 "length %qE of array section does not have integral type",
4585 length);
4586 return error_mark_node;
4588 if (low_bound)
4589 low_bound = mark_rvalue_use (low_bound);
4590 if (length)
4591 length = mark_rvalue_use (length);
4592 /* We need to reduce to real constant-values for checks below. */
4593 if (length)
4594 length = fold_simple (length);
4595 if (low_bound)
4596 low_bound = fold_simple (low_bound);
4597 if (low_bound
4598 && TREE_CODE (low_bound) == INTEGER_CST
4599 && TYPE_PRECISION (TREE_TYPE (low_bound))
4600 > TYPE_PRECISION (sizetype))
4601 low_bound = fold_convert (sizetype, low_bound);
4602 if (length
4603 && TREE_CODE (length) == INTEGER_CST
4604 && TYPE_PRECISION (TREE_TYPE (length))
4605 > TYPE_PRECISION (sizetype))
4606 length = fold_convert (sizetype, length);
4607 if (low_bound == NULL_TREE)
4608 low_bound = integer_zero_node;
4610 if (length != NULL_TREE)
4612 if (!integer_nonzerop (length))
4614 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
4615 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
4617 if (integer_zerop (length))
4619 error_at (OMP_CLAUSE_LOCATION (c),
4620 "zero length array section in %qs clause",
4621 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4622 return error_mark_node;
4625 else
4626 maybe_zero_len = true;
4628 if (first_non_one == types.length ()
4629 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
4630 first_non_one++;
4632 if (TREE_CODE (type) == ARRAY_TYPE)
4634 if (length == NULL_TREE
4635 && (TYPE_DOMAIN (type) == NULL_TREE
4636 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
4638 error_at (OMP_CLAUSE_LOCATION (c),
4639 "for unknown bound array type length expression must "
4640 "be specified");
4641 return error_mark_node;
4643 if (TREE_CODE (low_bound) == INTEGER_CST
4644 && tree_int_cst_sgn (low_bound) == -1)
4646 error_at (OMP_CLAUSE_LOCATION (c),
4647 "negative low bound in array section in %qs clause",
4648 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4649 return error_mark_node;
4651 if (length != NULL_TREE
4652 && TREE_CODE (length) == INTEGER_CST
4653 && tree_int_cst_sgn (length) == -1)
4655 error_at (OMP_CLAUSE_LOCATION (c),
4656 "negative length in array section in %qs clause",
4657 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4658 return error_mark_node;
4660 if (TYPE_DOMAIN (type)
4661 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
4662 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
4663 == INTEGER_CST)
4665 tree size
4666 = fold_convert (sizetype, TYPE_MAX_VALUE (TYPE_DOMAIN (type)));
4667 size = size_binop (PLUS_EXPR, size, size_one_node);
4668 if (TREE_CODE (low_bound) == INTEGER_CST)
4670 if (tree_int_cst_lt (size, low_bound))
4672 error_at (OMP_CLAUSE_LOCATION (c),
4673 "low bound %qE above array section size "
4674 "in %qs clause", low_bound,
4675 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4676 return error_mark_node;
4678 if (tree_int_cst_equal (size, low_bound))
4680 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
4681 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
4683 error_at (OMP_CLAUSE_LOCATION (c),
4684 "zero length array section in %qs clause",
4685 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4686 return error_mark_node;
4688 maybe_zero_len = true;
4690 else if (length == NULL_TREE
4691 && first_non_one == types.length ()
4692 && tree_int_cst_equal
4693 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
4694 low_bound))
4695 first_non_one++;
4697 else if (length == NULL_TREE)
4699 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
4700 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION)
4701 maybe_zero_len = true;
4702 if (first_non_one == types.length ())
4703 first_non_one++;
4705 if (length && TREE_CODE (length) == INTEGER_CST)
4707 if (tree_int_cst_lt (size, length))
4709 error_at (OMP_CLAUSE_LOCATION (c),
4710 "length %qE above array section size "
4711 "in %qs clause", length,
4712 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4713 return error_mark_node;
4715 if (TREE_CODE (low_bound) == INTEGER_CST)
4717 tree lbpluslen
4718 = size_binop (PLUS_EXPR,
4719 fold_convert (sizetype, low_bound),
4720 fold_convert (sizetype, length));
4721 if (TREE_CODE (lbpluslen) == INTEGER_CST
4722 && tree_int_cst_lt (size, lbpluslen))
4724 error_at (OMP_CLAUSE_LOCATION (c),
4725 "high bound %qE above array section size "
4726 "in %qs clause", lbpluslen,
4727 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4728 return error_mark_node;
4733 else if (length == NULL_TREE)
4735 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
4736 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION)
4737 maybe_zero_len = true;
4738 if (first_non_one == types.length ())
4739 first_non_one++;
4742 /* For [lb:] we will need to evaluate lb more than once. */
4743 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
4745 tree lb = cp_save_expr (low_bound);
4746 if (lb != low_bound)
4748 TREE_PURPOSE (t) = lb;
4749 low_bound = lb;
4753 else if (TREE_CODE (type) == POINTER_TYPE)
4755 if (length == NULL_TREE)
4757 error_at (OMP_CLAUSE_LOCATION (c),
4758 "for pointer type length expression must be specified");
4759 return error_mark_node;
4761 if (length != NULL_TREE
4762 && TREE_CODE (length) == INTEGER_CST
4763 && tree_int_cst_sgn (length) == -1)
4765 error_at (OMP_CLAUSE_LOCATION (c),
4766 "negative length in array section in %qs clause",
4767 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4768 return error_mark_node;
4770 /* If there is a pointer type anywhere but in the very first
4771 array-section-subscript, the array section can't be contiguous. */
4772 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
4773 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
4775 error_at (OMP_CLAUSE_LOCATION (c),
4776 "array section is not contiguous in %qs clause",
4777 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4778 return error_mark_node;
4781 else
4783 error_at (OMP_CLAUSE_LOCATION (c),
4784 "%qE does not have pointer or array type", ret);
4785 return error_mark_node;
4787 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
4788 types.safe_push (TREE_TYPE (ret));
4789 /* We will need to evaluate lb more than once. */
4790 tree lb = cp_save_expr (low_bound);
4791 if (lb != low_bound)
4793 TREE_PURPOSE (t) = lb;
4794 low_bound = lb;
4796 ret = grok_array_decl (OMP_CLAUSE_LOCATION (c), ret, low_bound, false);
4797 return ret;
4800 /* Handle array sections for clause C. */
4802 static bool
4803 handle_omp_array_sections (tree c, enum c_omp_region_type ort)
4805 bool maybe_zero_len = false;
4806 unsigned int first_non_one = 0;
4807 auto_vec<tree, 10> types;
4808 tree first = handle_omp_array_sections_1 (c, OMP_CLAUSE_DECL (c), types,
4809 maybe_zero_len, first_non_one,
4810 ort);
4811 if (first == error_mark_node)
4812 return true;
4813 if (first == NULL_TREE)
4814 return false;
4815 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
4817 tree t = OMP_CLAUSE_DECL (c);
4818 tree tem = NULL_TREE;
4819 if (processing_template_decl)
4820 return false;
4821 /* Need to evaluate side effects in the length expressions
4822 if any. */
4823 while (TREE_CODE (t) == TREE_LIST)
4825 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
4827 if (tem == NULL_TREE)
4828 tem = TREE_VALUE (t);
4829 else
4830 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
4831 TREE_VALUE (t), tem);
4833 t = TREE_CHAIN (t);
4835 if (tem)
4836 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
4837 OMP_CLAUSE_DECL (c) = first;
4839 else
4841 unsigned int num = types.length (), i;
4842 tree t, side_effects = NULL_TREE, size = NULL_TREE;
4843 tree condition = NULL_TREE;
4845 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
4846 maybe_zero_len = true;
4847 if (processing_template_decl && maybe_zero_len)
4848 return false;
4850 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
4851 t = TREE_CHAIN (t))
4853 tree low_bound = TREE_PURPOSE (t);
4854 tree length = TREE_VALUE (t);
4856 i--;
4857 if (low_bound
4858 && TREE_CODE (low_bound) == INTEGER_CST
4859 && TYPE_PRECISION (TREE_TYPE (low_bound))
4860 > TYPE_PRECISION (sizetype))
4861 low_bound = fold_convert (sizetype, low_bound);
4862 if (length
4863 && TREE_CODE (length) == INTEGER_CST
4864 && TYPE_PRECISION (TREE_TYPE (length))
4865 > TYPE_PRECISION (sizetype))
4866 length = fold_convert (sizetype, length);
4867 if (low_bound == NULL_TREE)
4868 low_bound = integer_zero_node;
4869 if (!maybe_zero_len && i > first_non_one)
4871 if (integer_nonzerop (low_bound))
4872 goto do_warn_noncontiguous;
4873 if (length != NULL_TREE
4874 && TREE_CODE (length) == INTEGER_CST
4875 && TYPE_DOMAIN (types[i])
4876 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
4877 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
4878 == INTEGER_CST)
4880 tree size;
4881 size = size_binop (PLUS_EXPR,
4882 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
4883 size_one_node);
4884 if (!tree_int_cst_equal (length, size))
4886 do_warn_noncontiguous:
4887 error_at (OMP_CLAUSE_LOCATION (c),
4888 "array section is not contiguous in %qs "
4889 "clause",
4890 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4891 return true;
4894 if (!processing_template_decl
4895 && length != NULL_TREE
4896 && TREE_SIDE_EFFECTS (length))
4898 if (side_effects == NULL_TREE)
4899 side_effects = length;
4900 else
4901 side_effects = build2 (COMPOUND_EXPR,
4902 TREE_TYPE (side_effects),
4903 length, side_effects);
4906 else if (processing_template_decl)
4907 continue;
4908 else
4910 tree l;
4912 if (i > first_non_one
4913 && ((length && integer_nonzerop (length))
4914 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION))
4915 continue;
4916 if (length)
4917 l = fold_convert (sizetype, length);
4918 else
4920 l = size_binop (PLUS_EXPR,
4921 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
4922 size_one_node);
4923 l = size_binop (MINUS_EXPR, l,
4924 fold_convert (sizetype, low_bound));
4926 if (i > first_non_one)
4928 l = fold_build2 (NE_EXPR, boolean_type_node, l,
4929 size_zero_node);
4930 if (condition == NULL_TREE)
4931 condition = l;
4932 else
4933 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
4934 l, condition);
4936 else if (size == NULL_TREE)
4938 size = size_in_bytes (TREE_TYPE (types[i]));
4939 tree eltype = TREE_TYPE (types[num - 1]);
4940 while (TREE_CODE (eltype) == ARRAY_TYPE)
4941 eltype = TREE_TYPE (eltype);
4942 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
4943 size = size_binop (EXACT_DIV_EXPR, size,
4944 size_in_bytes (eltype));
4945 size = size_binop (MULT_EXPR, size, l);
4946 if (condition)
4947 size = fold_build3 (COND_EXPR, sizetype, condition,
4948 size, size_zero_node);
4950 else
4951 size = size_binop (MULT_EXPR, size, l);
4954 if (!processing_template_decl)
4956 if (side_effects)
4957 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
4958 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
4960 size = size_binop (MINUS_EXPR, size, size_one_node);
4961 tree index_type = build_index_type (size);
4962 tree eltype = TREE_TYPE (first);
4963 while (TREE_CODE (eltype) == ARRAY_TYPE)
4964 eltype = TREE_TYPE (eltype);
4965 tree type = build_array_type (eltype, index_type);
4966 tree ptype = build_pointer_type (eltype);
4967 if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE
4968 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (t))))
4969 t = convert_from_reference (t);
4970 else if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
4971 t = build_fold_addr_expr (t);
4972 tree t2 = build_fold_addr_expr (first);
4973 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
4974 ptrdiff_type_node, t2);
4975 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
4976 ptrdiff_type_node, t2,
4977 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
4978 ptrdiff_type_node, t));
4979 if (tree_fits_shwi_p (t2))
4980 t = build2 (MEM_REF, type, t,
4981 build_int_cst (ptype, tree_to_shwi (t2)));
4982 else
4984 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
4985 sizetype, t2);
4986 t = build2_loc (OMP_CLAUSE_LOCATION (c), POINTER_PLUS_EXPR,
4987 TREE_TYPE (t), t, t2);
4988 t = build2 (MEM_REF, type, t, build_int_cst (ptype, 0));
4990 OMP_CLAUSE_DECL (c) = t;
4991 return false;
4993 OMP_CLAUSE_DECL (c) = first;
4994 OMP_CLAUSE_SIZE (c) = size;
4995 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
4996 || (TREE_CODE (t) == COMPONENT_REF
4997 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE))
4998 return false;
4999 if (ort == C_ORT_OMP || ort == C_ORT_ACC)
5000 switch (OMP_CLAUSE_MAP_KIND (c))
5002 case GOMP_MAP_ALLOC:
5003 case GOMP_MAP_TO:
5004 case GOMP_MAP_FROM:
5005 case GOMP_MAP_TOFROM:
5006 case GOMP_MAP_ALWAYS_TO:
5007 case GOMP_MAP_ALWAYS_FROM:
5008 case GOMP_MAP_ALWAYS_TOFROM:
5009 case GOMP_MAP_RELEASE:
5010 case GOMP_MAP_DELETE:
5011 case GOMP_MAP_FORCE_TO:
5012 case GOMP_MAP_FORCE_FROM:
5013 case GOMP_MAP_FORCE_TOFROM:
5014 case GOMP_MAP_FORCE_PRESENT:
5015 OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (c) = 1;
5016 break;
5017 default:
5018 break;
5020 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
5021 OMP_CLAUSE_MAP);
5022 if ((ort & C_ORT_OMP_DECLARE_SIMD) != C_ORT_OMP && ort != C_ORT_ACC)
5023 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_POINTER);
5024 else if (TREE_CODE (t) == COMPONENT_REF)
5025 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ALWAYS_POINTER);
5026 else if (REFERENCE_REF_P (t)
5027 && TREE_CODE (TREE_OPERAND (t, 0)) == COMPONENT_REF)
5029 t = TREE_OPERAND (t, 0);
5030 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ALWAYS_POINTER);
5032 else
5033 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_FIRSTPRIVATE_POINTER);
5034 if (OMP_CLAUSE_MAP_KIND (c2) != GOMP_MAP_FIRSTPRIVATE_POINTER
5035 && !cxx_mark_addressable (t))
5036 return false;
5037 OMP_CLAUSE_DECL (c2) = t;
5038 t = build_fold_addr_expr (first);
5039 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
5040 ptrdiff_type_node, t);
5041 tree ptr = OMP_CLAUSE_DECL (c2);
5042 ptr = convert_from_reference (ptr);
5043 if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
5044 ptr = build_fold_addr_expr (ptr);
5045 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
5046 ptrdiff_type_node, t,
5047 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
5048 ptrdiff_type_node, ptr));
5049 OMP_CLAUSE_SIZE (c2) = t;
5050 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
5051 OMP_CLAUSE_CHAIN (c) = c2;
5052 ptr = OMP_CLAUSE_DECL (c2);
5053 if (OMP_CLAUSE_MAP_KIND (c2) != GOMP_MAP_FIRSTPRIVATE_POINTER
5054 && TREE_CODE (TREE_TYPE (ptr)) == REFERENCE_TYPE
5055 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (ptr))))
5057 tree c3 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
5058 OMP_CLAUSE_MAP);
5059 OMP_CLAUSE_SET_MAP_KIND (c3, OMP_CLAUSE_MAP_KIND (c2));
5060 OMP_CLAUSE_DECL (c3) = ptr;
5061 if (OMP_CLAUSE_MAP_KIND (c2) == GOMP_MAP_ALWAYS_POINTER)
5062 OMP_CLAUSE_DECL (c2) = build_simple_mem_ref (ptr);
5063 else
5064 OMP_CLAUSE_DECL (c2) = convert_from_reference (ptr);
5065 OMP_CLAUSE_SIZE (c3) = size_zero_node;
5066 OMP_CLAUSE_CHAIN (c3) = OMP_CLAUSE_CHAIN (c2);
5067 OMP_CLAUSE_CHAIN (c2) = c3;
5071 return false;
5074 /* Return identifier to look up for omp declare reduction. */
5076 tree
5077 omp_reduction_id (enum tree_code reduction_code, tree reduction_id, tree type)
5079 const char *p = NULL;
5080 const char *m = NULL;
5081 switch (reduction_code)
5083 case PLUS_EXPR:
5084 case MULT_EXPR:
5085 case MINUS_EXPR:
5086 case BIT_AND_EXPR:
5087 case BIT_XOR_EXPR:
5088 case BIT_IOR_EXPR:
5089 case TRUTH_ANDIF_EXPR:
5090 case TRUTH_ORIF_EXPR:
5091 reduction_id = cp_operator_id (reduction_code);
5092 break;
5093 case MIN_EXPR:
5094 p = "min";
5095 break;
5096 case MAX_EXPR:
5097 p = "max";
5098 break;
5099 default:
5100 break;
5103 if (p == NULL)
5105 if (TREE_CODE (reduction_id) != IDENTIFIER_NODE)
5106 return error_mark_node;
5107 p = IDENTIFIER_POINTER (reduction_id);
5110 if (type != NULL_TREE)
5111 m = mangle_type_string (TYPE_MAIN_VARIANT (type));
5113 const char prefix[] = "omp declare reduction ";
5114 size_t lenp = sizeof (prefix);
5115 if (strncmp (p, prefix, lenp - 1) == 0)
5116 lenp = 1;
5117 size_t len = strlen (p);
5118 size_t lenm = m ? strlen (m) + 1 : 0;
5119 char *name = XALLOCAVEC (char, lenp + len + lenm);
5120 if (lenp > 1)
5121 memcpy (name, prefix, lenp - 1);
5122 memcpy (name + lenp - 1, p, len + 1);
5123 if (m)
5125 name[lenp + len - 1] = '~';
5126 memcpy (name + lenp + len, m, lenm);
5128 return get_identifier (name);
5131 /* Lookup OpenMP UDR ID for TYPE, return the corresponding artificial
5132 FUNCTION_DECL or NULL_TREE if not found. */
5134 static tree
5135 omp_reduction_lookup (location_t loc, tree id, tree type, tree *baselinkp,
5136 vec<tree> *ambiguousp)
5138 tree orig_id = id;
5139 tree baselink = NULL_TREE;
5140 if (identifier_p (id))
5142 cp_id_kind idk;
5143 bool nonint_cst_expression_p;
5144 const char *error_msg;
5145 id = omp_reduction_id (ERROR_MARK, id, type);
5146 tree decl = lookup_name (id);
5147 if (decl == NULL_TREE)
5148 decl = error_mark_node;
5149 id = finish_id_expression (id, decl, NULL_TREE, &idk, false, true,
5150 &nonint_cst_expression_p, false, true, false,
5151 false, &error_msg, loc);
5152 if (idk == CP_ID_KIND_UNQUALIFIED
5153 && identifier_p (id))
5155 vec<tree, va_gc> *args = NULL;
5156 vec_safe_push (args, build_reference_type (type));
5157 id = perform_koenig_lookup (id, args, tf_none);
5160 else if (TREE_CODE (id) == SCOPE_REF)
5161 id = lookup_qualified_name (TREE_OPERAND (id, 0),
5162 omp_reduction_id (ERROR_MARK,
5163 TREE_OPERAND (id, 1),
5164 type),
5165 false, false);
5166 tree fns = id;
5167 id = NULL_TREE;
5168 if (fns && is_overloaded_fn (fns))
5170 for (lkp_iterator iter (get_fns (fns)); iter; ++iter)
5172 tree fndecl = *iter;
5173 if (TREE_CODE (fndecl) == FUNCTION_DECL)
5175 tree argtype = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
5176 if (same_type_p (TREE_TYPE (argtype), type))
5178 id = fndecl;
5179 break;
5184 if (id && BASELINK_P (fns))
5186 if (baselinkp)
5187 *baselinkp = fns;
5188 else
5189 baselink = fns;
5193 if (!id && CLASS_TYPE_P (type) && TYPE_BINFO (type))
5195 vec<tree> ambiguous = vNULL;
5196 tree binfo = TYPE_BINFO (type), base_binfo, ret = NULL_TREE;
5197 unsigned int ix;
5198 if (ambiguousp == NULL)
5199 ambiguousp = &ambiguous;
5200 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
5202 id = omp_reduction_lookup (loc, orig_id, BINFO_TYPE (base_binfo),
5203 baselinkp ? baselinkp : &baselink,
5204 ambiguousp);
5205 if (id == NULL_TREE)
5206 continue;
5207 if (!ambiguousp->is_empty ())
5208 ambiguousp->safe_push (id);
5209 else if (ret != NULL_TREE)
5211 ambiguousp->safe_push (ret);
5212 ambiguousp->safe_push (id);
5213 ret = NULL_TREE;
5215 else
5216 ret = id;
5218 if (ambiguousp != &ambiguous)
5219 return ret;
5220 if (!ambiguous.is_empty ())
5222 const char *str = _("candidates are:");
5223 unsigned int idx;
5224 tree udr;
5225 error_at (loc, "user defined reduction lookup is ambiguous");
5226 FOR_EACH_VEC_ELT (ambiguous, idx, udr)
5228 inform (DECL_SOURCE_LOCATION (udr), "%s %#qD", str, udr);
5229 if (idx == 0)
5230 str = get_spaces (str);
5232 ambiguous.release ();
5233 ret = error_mark_node;
5234 baselink = NULL_TREE;
5236 id = ret;
5238 if (id && baselink)
5239 perform_or_defer_access_check (BASELINK_BINFO (baselink),
5240 id, id, tf_warning_or_error);
5241 return id;
5244 /* Helper function for cp_parser_omp_declare_reduction_exprs
5245 and tsubst_omp_udr.
5246 Remove CLEANUP_STMT for data (omp_priv variable).
5247 Also append INIT_EXPR for DECL_INITIAL of omp_priv after its
5248 DECL_EXPR. */
5250 tree
5251 cp_remove_omp_priv_cleanup_stmt (tree *tp, int *walk_subtrees, void *data)
5253 if (TYPE_P (*tp))
5254 *walk_subtrees = 0;
5255 else if (TREE_CODE (*tp) == CLEANUP_STMT && CLEANUP_DECL (*tp) == (tree) data)
5256 *tp = CLEANUP_BODY (*tp);
5257 else if (TREE_CODE (*tp) == DECL_EXPR)
5259 tree decl = DECL_EXPR_DECL (*tp);
5260 if (!processing_template_decl
5261 && decl == (tree) data
5262 && DECL_INITIAL (decl)
5263 && DECL_INITIAL (decl) != error_mark_node)
5265 tree list = NULL_TREE;
5266 append_to_statement_list_force (*tp, &list);
5267 tree init_expr = build2 (INIT_EXPR, void_type_node,
5268 decl, DECL_INITIAL (decl));
5269 DECL_INITIAL (decl) = NULL_TREE;
5270 append_to_statement_list_force (init_expr, &list);
5271 *tp = list;
5274 return NULL_TREE;
5277 /* Data passed from cp_check_omp_declare_reduction to
5278 cp_check_omp_declare_reduction_r. */
5280 struct cp_check_omp_declare_reduction_data
5282 location_t loc;
5283 tree stmts[7];
5284 bool combiner_p;
5287 /* Helper function for cp_check_omp_declare_reduction, called via
5288 cp_walk_tree. */
5290 static tree
5291 cp_check_omp_declare_reduction_r (tree *tp, int *, void *data)
5293 struct cp_check_omp_declare_reduction_data *udr_data
5294 = (struct cp_check_omp_declare_reduction_data *) data;
5295 if (SSA_VAR_P (*tp)
5296 && !DECL_ARTIFICIAL (*tp)
5297 && *tp != DECL_EXPR_DECL (udr_data->stmts[udr_data->combiner_p ? 0 : 3])
5298 && *tp != DECL_EXPR_DECL (udr_data->stmts[udr_data->combiner_p ? 1 : 4]))
5300 location_t loc = udr_data->loc;
5301 if (udr_data->combiner_p)
5302 error_at (loc, "%<#pragma omp declare reduction%> combiner refers to "
5303 "variable %qD which is not %<omp_out%> nor %<omp_in%>",
5304 *tp);
5305 else
5306 error_at (loc, "%<#pragma omp declare reduction%> initializer refers "
5307 "to variable %qD which is not %<omp_priv%> nor "
5308 "%<omp_orig%>",
5309 *tp);
5310 return *tp;
5312 return NULL_TREE;
5315 /* Diagnose violation of OpenMP #pragma omp declare reduction restrictions. */
5317 void
5318 cp_check_omp_declare_reduction (tree udr)
5320 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (udr)));
5321 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
5322 type = TREE_TYPE (type);
5323 int i;
5324 location_t loc = DECL_SOURCE_LOCATION (udr);
5326 if (type == error_mark_node)
5327 return;
5328 if (ARITHMETIC_TYPE_P (type))
5330 static enum tree_code predef_codes[]
5331 = { PLUS_EXPR, MULT_EXPR, MINUS_EXPR, BIT_AND_EXPR, BIT_XOR_EXPR,
5332 BIT_IOR_EXPR, TRUTH_ANDIF_EXPR, TRUTH_ORIF_EXPR };
5333 for (i = 0; i < 8; i++)
5335 tree id = omp_reduction_id (predef_codes[i], NULL_TREE, NULL_TREE);
5336 const char *n1 = IDENTIFIER_POINTER (DECL_NAME (udr));
5337 const char *n2 = IDENTIFIER_POINTER (id);
5338 if (strncmp (n1, n2, IDENTIFIER_LENGTH (id)) == 0
5339 && (n1[IDENTIFIER_LENGTH (id)] == '~'
5340 || n1[IDENTIFIER_LENGTH (id)] == '\0'))
5341 break;
5344 if (i == 8
5345 && TREE_CODE (type) != COMPLEX_EXPR)
5347 const char prefix_minmax[] = "omp declare reduction m";
5348 size_t prefix_size = sizeof (prefix_minmax) - 1;
5349 const char *n = IDENTIFIER_POINTER (DECL_NAME (udr));
5350 if (strncmp (IDENTIFIER_POINTER (DECL_NAME (udr)),
5351 prefix_minmax, prefix_size) == 0
5352 && ((n[prefix_size] == 'i' && n[prefix_size + 1] == 'n')
5353 || (n[prefix_size] == 'a' && n[prefix_size + 1] == 'x'))
5354 && (n[prefix_size + 2] == '~' || n[prefix_size + 2] == '\0'))
5355 i = 0;
5357 if (i < 8)
5359 error_at (loc, "predeclared arithmetic type %qT in "
5360 "%<#pragma omp declare reduction%>", type);
5361 return;
5364 else if (TREE_CODE (type) == FUNCTION_TYPE
5365 || TREE_CODE (type) == METHOD_TYPE
5366 || TREE_CODE (type) == ARRAY_TYPE)
5368 error_at (loc, "function or array type %qT in "
5369 "%<#pragma omp declare reduction%>", type);
5370 return;
5372 else if (TREE_CODE (type) == REFERENCE_TYPE)
5374 error_at (loc, "reference type %qT in %<#pragma omp declare reduction%>",
5375 type);
5376 return;
5378 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
5380 error_at (loc, "const, volatile or __restrict qualified type %qT in "
5381 "%<#pragma omp declare reduction%>", type);
5382 return;
5385 tree body = DECL_SAVED_TREE (udr);
5386 if (body == NULL_TREE || TREE_CODE (body) != STATEMENT_LIST)
5387 return;
5389 tree_stmt_iterator tsi;
5390 struct cp_check_omp_declare_reduction_data data;
5391 memset (data.stmts, 0, sizeof data.stmts);
5392 for (i = 0, tsi = tsi_start (body);
5393 i < 7 && !tsi_end_p (tsi);
5394 i++, tsi_next (&tsi))
5395 data.stmts[i] = tsi_stmt (tsi);
5396 data.loc = loc;
5397 gcc_assert (tsi_end_p (tsi));
5398 if (i >= 3)
5400 gcc_assert (TREE_CODE (data.stmts[0]) == DECL_EXPR
5401 && TREE_CODE (data.stmts[1]) == DECL_EXPR);
5402 if (TREE_NO_WARNING (DECL_EXPR_DECL (data.stmts[0])))
5403 return;
5404 data.combiner_p = true;
5405 if (cp_walk_tree (&data.stmts[2], cp_check_omp_declare_reduction_r,
5406 &data, NULL))
5407 TREE_NO_WARNING (DECL_EXPR_DECL (data.stmts[0])) = 1;
5409 if (i >= 6)
5411 gcc_assert (TREE_CODE (data.stmts[3]) == DECL_EXPR
5412 && TREE_CODE (data.stmts[4]) == DECL_EXPR);
5413 data.combiner_p = false;
5414 if (cp_walk_tree (&data.stmts[5], cp_check_omp_declare_reduction_r,
5415 &data, NULL)
5416 || cp_walk_tree (&DECL_INITIAL (DECL_EXPR_DECL (data.stmts[3])),
5417 cp_check_omp_declare_reduction_r, &data, NULL))
5418 TREE_NO_WARNING (DECL_EXPR_DECL (data.stmts[0])) = 1;
5419 if (i == 7)
5420 gcc_assert (TREE_CODE (data.stmts[6]) == DECL_EXPR);
5424 /* Helper function of finish_omp_clauses. Clone STMT as if we were making
5425 an inline call. But, remap
5426 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
5427 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
5429 static tree
5430 clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
5431 tree decl, tree placeholder)
5433 copy_body_data id;
5434 hash_map<tree, tree> decl_map;
5436 decl_map.put (omp_decl1, placeholder);
5437 decl_map.put (omp_decl2, decl);
5438 memset (&id, 0, sizeof (id));
5439 id.src_fn = DECL_CONTEXT (omp_decl1);
5440 id.dst_fn = current_function_decl;
5441 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
5442 id.decl_map = &decl_map;
5444 id.copy_decl = copy_decl_no_change;
5445 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
5446 id.transform_new_cfg = true;
5447 id.transform_return_to_modify = false;
5448 id.transform_lang_insert_block = NULL;
5449 id.eh_lp_nr = 0;
5450 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
5451 return stmt;
5454 /* Helper function of finish_omp_clauses, called via cp_walk_tree.
5455 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
5457 static tree
5458 find_omp_placeholder_r (tree *tp, int *, void *data)
5460 if (*tp == (tree) data)
5461 return *tp;
5462 return NULL_TREE;
5465 /* Helper function of finish_omp_clauses. Handle OMP_CLAUSE_REDUCTION C.
5466 Return true if there is some error and the clause should be removed. */
5468 static bool
5469 finish_omp_reduction_clause (tree c, bool *need_default_ctor, bool *need_dtor)
5471 tree t = OMP_CLAUSE_DECL (c);
5472 bool predefined = false;
5473 if (TREE_CODE (t) == TREE_LIST)
5475 gcc_assert (processing_template_decl);
5476 return false;
5478 tree type = TREE_TYPE (t);
5479 if (TREE_CODE (t) == MEM_REF)
5480 type = TREE_TYPE (type);
5481 if (TREE_CODE (type) == REFERENCE_TYPE)
5482 type = TREE_TYPE (type);
5483 if (TREE_CODE (type) == ARRAY_TYPE)
5485 tree oatype = type;
5486 gcc_assert (TREE_CODE (t) != MEM_REF);
5487 while (TREE_CODE (type) == ARRAY_TYPE)
5488 type = TREE_TYPE (type);
5489 if (!processing_template_decl)
5491 t = require_complete_type (t);
5492 if (t == error_mark_node)
5493 return true;
5494 tree size = size_binop (EXACT_DIV_EXPR, TYPE_SIZE_UNIT (oatype),
5495 TYPE_SIZE_UNIT (type));
5496 if (integer_zerop (size))
5498 error ("%qE in %<reduction%> clause is a zero size array",
5499 omp_clause_printable_decl (t));
5500 return true;
5502 size = size_binop (MINUS_EXPR, size, size_one_node);
5503 tree index_type = build_index_type (size);
5504 tree atype = build_array_type (type, index_type);
5505 tree ptype = build_pointer_type (type);
5506 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
5507 t = build_fold_addr_expr (t);
5508 t = build2 (MEM_REF, atype, t, build_int_cst (ptype, 0));
5509 OMP_CLAUSE_DECL (c) = t;
5512 if (type == error_mark_node)
5513 return true;
5514 else if (ARITHMETIC_TYPE_P (type))
5515 switch (OMP_CLAUSE_REDUCTION_CODE (c))
5517 case PLUS_EXPR:
5518 case MULT_EXPR:
5519 case MINUS_EXPR:
5520 predefined = true;
5521 break;
5522 case MIN_EXPR:
5523 case MAX_EXPR:
5524 if (TREE_CODE (type) == COMPLEX_TYPE)
5525 break;
5526 predefined = true;
5527 break;
5528 case BIT_AND_EXPR:
5529 case BIT_IOR_EXPR:
5530 case BIT_XOR_EXPR:
5531 if (FLOAT_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
5532 break;
5533 predefined = true;
5534 break;
5535 case TRUTH_ANDIF_EXPR:
5536 case TRUTH_ORIF_EXPR:
5537 if (FLOAT_TYPE_P (type))
5538 break;
5539 predefined = true;
5540 break;
5541 default:
5542 break;
5544 else if (TYPE_READONLY (type))
5546 error ("%qE has const type for %<reduction%>",
5547 omp_clause_printable_decl (t));
5548 return true;
5550 else if (!processing_template_decl)
5552 t = require_complete_type (t);
5553 if (t == error_mark_node)
5554 return true;
5555 OMP_CLAUSE_DECL (c) = t;
5558 if (predefined)
5560 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = NULL_TREE;
5561 return false;
5563 else if (processing_template_decl)
5564 return false;
5566 tree id = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
5568 type = TYPE_MAIN_VARIANT (type);
5569 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = NULL_TREE;
5570 if (id == NULL_TREE)
5571 id = omp_reduction_id (OMP_CLAUSE_REDUCTION_CODE (c),
5572 NULL_TREE, NULL_TREE);
5573 id = omp_reduction_lookup (OMP_CLAUSE_LOCATION (c), id, type, NULL, NULL);
5574 if (id)
5576 if (id == error_mark_node)
5577 return true;
5578 mark_used (id);
5579 tree body = DECL_SAVED_TREE (id);
5580 if (!body)
5581 return true;
5582 if (TREE_CODE (body) == STATEMENT_LIST)
5584 tree_stmt_iterator tsi;
5585 tree placeholder = NULL_TREE, decl_placeholder = NULL_TREE;
5586 int i;
5587 tree stmts[7];
5588 tree atype = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (id)));
5589 atype = TREE_TYPE (atype);
5590 bool need_static_cast = !same_type_p (type, atype);
5591 memset (stmts, 0, sizeof stmts);
5592 for (i = 0, tsi = tsi_start (body);
5593 i < 7 && !tsi_end_p (tsi);
5594 i++, tsi_next (&tsi))
5595 stmts[i] = tsi_stmt (tsi);
5596 gcc_assert (tsi_end_p (tsi));
5598 if (i >= 3)
5600 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
5601 && TREE_CODE (stmts[1]) == DECL_EXPR);
5602 placeholder = build_lang_decl (VAR_DECL, NULL_TREE, type);
5603 DECL_ARTIFICIAL (placeholder) = 1;
5604 DECL_IGNORED_P (placeholder) = 1;
5605 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
5606 if (TREE_CODE (t) == MEM_REF)
5608 decl_placeholder = build_lang_decl (VAR_DECL, NULL_TREE,
5609 type);
5610 DECL_ARTIFICIAL (decl_placeholder) = 1;
5611 DECL_IGNORED_P (decl_placeholder) = 1;
5612 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c) = decl_placeholder;
5614 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[0])))
5615 cxx_mark_addressable (placeholder);
5616 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[1]))
5617 && TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c)))
5618 != REFERENCE_TYPE)
5619 cxx_mark_addressable (decl_placeholder ? decl_placeholder
5620 : OMP_CLAUSE_DECL (c));
5621 tree omp_out = placeholder;
5622 tree omp_in = decl_placeholder ? decl_placeholder
5623 : convert_from_reference (OMP_CLAUSE_DECL (c));
5624 if (need_static_cast)
5626 tree rtype = build_reference_type (atype);
5627 omp_out = build_static_cast (rtype, omp_out,
5628 tf_warning_or_error);
5629 omp_in = build_static_cast (rtype, omp_in,
5630 tf_warning_or_error);
5631 if (omp_out == error_mark_node || omp_in == error_mark_node)
5632 return true;
5633 omp_out = convert_from_reference (omp_out);
5634 omp_in = convert_from_reference (omp_in);
5636 OMP_CLAUSE_REDUCTION_MERGE (c)
5637 = clone_omp_udr (stmts[2], DECL_EXPR_DECL (stmts[0]),
5638 DECL_EXPR_DECL (stmts[1]), omp_in, omp_out);
5640 if (i >= 6)
5642 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
5643 && TREE_CODE (stmts[4]) == DECL_EXPR);
5644 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[3])))
5645 cxx_mark_addressable (decl_placeholder ? decl_placeholder
5646 : OMP_CLAUSE_DECL (c));
5647 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[4])))
5648 cxx_mark_addressable (placeholder);
5649 tree omp_priv = decl_placeholder ? decl_placeholder
5650 : convert_from_reference (OMP_CLAUSE_DECL (c));
5651 tree omp_orig = placeholder;
5652 if (need_static_cast)
5654 if (i == 7)
5656 error_at (OMP_CLAUSE_LOCATION (c),
5657 "user defined reduction with constructor "
5658 "initializer for base class %qT", atype);
5659 return true;
5661 tree rtype = build_reference_type (atype);
5662 omp_priv = build_static_cast (rtype, omp_priv,
5663 tf_warning_or_error);
5664 omp_orig = build_static_cast (rtype, omp_orig,
5665 tf_warning_or_error);
5666 if (omp_priv == error_mark_node
5667 || omp_orig == error_mark_node)
5668 return true;
5669 omp_priv = convert_from_reference (omp_priv);
5670 omp_orig = convert_from_reference (omp_orig);
5672 if (i == 6)
5673 *need_default_ctor = true;
5674 OMP_CLAUSE_REDUCTION_INIT (c)
5675 = clone_omp_udr (stmts[5], DECL_EXPR_DECL (stmts[4]),
5676 DECL_EXPR_DECL (stmts[3]),
5677 omp_priv, omp_orig);
5678 if (cp_walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
5679 find_omp_placeholder_r, placeholder, NULL))
5680 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
5682 else if (i >= 3)
5684 if (CLASS_TYPE_P (type) && !pod_type_p (type))
5685 *need_default_ctor = true;
5686 else
5688 tree init;
5689 tree v = decl_placeholder ? decl_placeholder
5690 : convert_from_reference (t);
5691 if (AGGREGATE_TYPE_P (TREE_TYPE (v)))
5692 init = build_constructor (TREE_TYPE (v), NULL);
5693 else
5694 init = fold_convert (TREE_TYPE (v), integer_zero_node);
5695 OMP_CLAUSE_REDUCTION_INIT (c)
5696 = build2 (INIT_EXPR, TREE_TYPE (v), v, init);
5701 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
5702 *need_dtor = true;
5703 else
5705 error ("user defined reduction not found for %qE",
5706 omp_clause_printable_decl (t));
5707 return true;
5709 if (TREE_CODE (OMP_CLAUSE_DECL (c)) == MEM_REF)
5710 gcc_assert (TYPE_SIZE_UNIT (type)
5711 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST);
5712 return false;
5715 /* Called from finish_struct_1. linear(this) or linear(this:step)
5716 clauses might not be finalized yet because the class has been incomplete
5717 when parsing #pragma omp declare simd methods. Fix those up now. */
5719 void
5720 finish_omp_declare_simd_methods (tree t)
5722 if (processing_template_decl)
5723 return;
5725 for (tree x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
5727 if (TREE_CODE (TREE_TYPE (x)) != METHOD_TYPE)
5728 continue;
5729 tree ods = lookup_attribute ("omp declare simd", DECL_ATTRIBUTES (x));
5730 if (!ods || !TREE_VALUE (ods))
5731 continue;
5732 for (tree c = TREE_VALUE (TREE_VALUE (ods)); c; c = OMP_CLAUSE_CHAIN (c))
5733 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
5734 && integer_zerop (OMP_CLAUSE_DECL (c))
5735 && OMP_CLAUSE_LINEAR_STEP (c)
5736 && TREE_CODE (TREE_TYPE (OMP_CLAUSE_LINEAR_STEP (c)))
5737 == POINTER_TYPE)
5739 tree s = OMP_CLAUSE_LINEAR_STEP (c);
5740 s = fold_convert_loc (OMP_CLAUSE_LOCATION (c), sizetype, s);
5741 s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MULT_EXPR,
5742 sizetype, s, TYPE_SIZE_UNIT (t));
5743 OMP_CLAUSE_LINEAR_STEP (c) = s;
5748 /* Adjust sink depend clause to take into account pointer offsets.
5750 Return TRUE if there was a problem processing the offset, and the
5751 whole clause should be removed. */
5753 static bool
5754 cp_finish_omp_clause_depend_sink (tree sink_clause)
5756 tree t = OMP_CLAUSE_DECL (sink_clause);
5757 gcc_assert (TREE_CODE (t) == TREE_LIST);
5759 /* Make sure we don't adjust things twice for templates. */
5760 if (processing_template_decl)
5761 return false;
5763 for (; t; t = TREE_CHAIN (t))
5765 tree decl = TREE_VALUE (t);
5766 if (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
5768 tree offset = TREE_PURPOSE (t);
5769 bool neg = wi::neg_p ((wide_int) offset);
5770 offset = fold_unary (ABS_EXPR, TREE_TYPE (offset), offset);
5771 decl = mark_rvalue_use (decl);
5772 decl = convert_from_reference (decl);
5773 tree t2 = pointer_int_sum (OMP_CLAUSE_LOCATION (sink_clause),
5774 neg ? MINUS_EXPR : PLUS_EXPR,
5775 decl, offset);
5776 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (sink_clause),
5777 MINUS_EXPR, sizetype,
5778 fold_convert (sizetype, t2),
5779 fold_convert (sizetype, decl));
5780 if (t2 == error_mark_node)
5781 return true;
5782 TREE_PURPOSE (t) = t2;
5785 return false;
5788 /* For all elements of CLAUSES, validate them vs OpenMP constraints.
5789 Remove any elements from the list that are invalid. */
5791 tree
5792 finish_omp_clauses (tree clauses, enum c_omp_region_type ort)
5794 bitmap_head generic_head, firstprivate_head, lastprivate_head;
5795 bitmap_head aligned_head, map_head, map_field_head, oacc_reduction_head;
5796 tree c, t, *pc;
5797 tree safelen = NULL_TREE;
5798 bool branch_seen = false;
5799 bool copyprivate_seen = false;
5800 bool ordered_seen = false;
5801 bool oacc_async = false;
5803 bitmap_obstack_initialize (NULL);
5804 bitmap_initialize (&generic_head, &bitmap_default_obstack);
5805 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
5806 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
5807 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
5808 bitmap_initialize (&map_head, &bitmap_default_obstack);
5809 bitmap_initialize (&map_field_head, &bitmap_default_obstack);
5810 bitmap_initialize (&oacc_reduction_head, &bitmap_default_obstack);
5812 if (ort & C_ORT_ACC)
5813 for (c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
5814 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ASYNC)
5816 oacc_async = true;
5817 break;
5820 for (pc = &clauses, c = clauses; c ; c = *pc)
5822 bool remove = false;
5823 bool field_ok = false;
5825 switch (OMP_CLAUSE_CODE (c))
5827 case OMP_CLAUSE_SHARED:
5828 field_ok = ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP);
5829 goto check_dup_generic;
5830 case OMP_CLAUSE_PRIVATE:
5831 field_ok = ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP);
5832 goto check_dup_generic;
5833 case OMP_CLAUSE_REDUCTION:
5834 field_ok = ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP);
5835 t = OMP_CLAUSE_DECL (c);
5836 if (TREE_CODE (t) == TREE_LIST)
5838 if (handle_omp_array_sections (c, ort))
5840 remove = true;
5841 break;
5843 if (TREE_CODE (t) == TREE_LIST)
5845 while (TREE_CODE (t) == TREE_LIST)
5846 t = TREE_CHAIN (t);
5848 else
5850 gcc_assert (TREE_CODE (t) == MEM_REF);
5851 t = TREE_OPERAND (t, 0);
5852 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
5853 t = TREE_OPERAND (t, 0);
5854 if (TREE_CODE (t) == ADDR_EXPR
5855 || TREE_CODE (t) == INDIRECT_REF)
5856 t = TREE_OPERAND (t, 0);
5858 tree n = omp_clause_decl_field (t);
5859 if (n)
5860 t = n;
5861 goto check_dup_generic_t;
5863 if (oacc_async)
5864 cxx_mark_addressable (t);
5865 goto check_dup_generic;
5866 case OMP_CLAUSE_COPYPRIVATE:
5867 copyprivate_seen = true;
5868 field_ok = ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP);
5869 goto check_dup_generic;
5870 case OMP_CLAUSE_COPYIN:
5871 goto check_dup_generic;
5872 case OMP_CLAUSE_LINEAR:
5873 field_ok = ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP);
5874 t = OMP_CLAUSE_DECL (c);
5875 if (ort != C_ORT_OMP_DECLARE_SIMD
5876 && OMP_CLAUSE_LINEAR_KIND (c) != OMP_CLAUSE_LINEAR_DEFAULT)
5878 error_at (OMP_CLAUSE_LOCATION (c),
5879 "modifier should not be specified in %<linear%> "
5880 "clause on %<simd%> or %<for%> constructs");
5881 OMP_CLAUSE_LINEAR_KIND (c) = OMP_CLAUSE_LINEAR_DEFAULT;
5883 if ((VAR_P (t) || TREE_CODE (t) == PARM_DECL)
5884 && !type_dependent_expression_p (t))
5886 tree type = TREE_TYPE (t);
5887 if ((OMP_CLAUSE_LINEAR_KIND (c) == OMP_CLAUSE_LINEAR_REF
5888 || OMP_CLAUSE_LINEAR_KIND (c) == OMP_CLAUSE_LINEAR_UVAL)
5889 && TREE_CODE (type) != REFERENCE_TYPE)
5891 error ("linear clause with %qs modifier applied to "
5892 "non-reference variable with %qT type",
5893 OMP_CLAUSE_LINEAR_KIND (c) == OMP_CLAUSE_LINEAR_REF
5894 ? "ref" : "uval", TREE_TYPE (t));
5895 remove = true;
5896 break;
5898 if (TREE_CODE (type) == REFERENCE_TYPE)
5899 type = TREE_TYPE (type);
5900 if (ort == C_ORT_CILK)
5902 if (!INTEGRAL_TYPE_P (type)
5903 && !SCALAR_FLOAT_TYPE_P (type)
5904 && TREE_CODE (type) != POINTER_TYPE)
5906 error ("linear clause applied to non-integral, "
5907 "non-floating, non-pointer variable with %qT type",
5908 TREE_TYPE (t));
5909 remove = true;
5910 break;
5913 else if (OMP_CLAUSE_LINEAR_KIND (c) != OMP_CLAUSE_LINEAR_REF)
5915 if (!INTEGRAL_TYPE_P (type)
5916 && TREE_CODE (type) != POINTER_TYPE)
5918 error ("linear clause applied to non-integral non-pointer"
5919 " variable with %qT type", TREE_TYPE (t));
5920 remove = true;
5921 break;
5925 t = OMP_CLAUSE_LINEAR_STEP (c);
5926 if (t == NULL_TREE)
5927 t = integer_one_node;
5928 if (t == error_mark_node)
5930 remove = true;
5931 break;
5933 else if (!type_dependent_expression_p (t)
5934 && !INTEGRAL_TYPE_P (TREE_TYPE (t))
5935 && (ort != C_ORT_OMP_DECLARE_SIMD
5936 || TREE_CODE (t) != PARM_DECL
5937 || TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE
5938 || !INTEGRAL_TYPE_P (TREE_TYPE (TREE_TYPE (t)))))
5940 error ("linear step expression must be integral");
5941 remove = true;
5942 break;
5944 else
5946 t = mark_rvalue_use (t);
5947 if (ort == C_ORT_OMP_DECLARE_SIMD && TREE_CODE (t) == PARM_DECL)
5949 OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c) = 1;
5950 goto check_dup_generic;
5952 if (!processing_template_decl
5953 && (VAR_P (OMP_CLAUSE_DECL (c))
5954 || TREE_CODE (OMP_CLAUSE_DECL (c)) == PARM_DECL))
5956 if (ort == C_ORT_OMP_DECLARE_SIMD)
5958 t = maybe_constant_value (t);
5959 if (TREE_CODE (t) != INTEGER_CST)
5961 error_at (OMP_CLAUSE_LOCATION (c),
5962 "%<linear%> clause step %qE is neither "
5963 "constant nor a parameter", t);
5964 remove = true;
5965 break;
5968 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5969 tree type = TREE_TYPE (OMP_CLAUSE_DECL (c));
5970 if (TREE_CODE (type) == REFERENCE_TYPE)
5971 type = TREE_TYPE (type);
5972 if (OMP_CLAUSE_LINEAR_KIND (c) == OMP_CLAUSE_LINEAR_REF)
5974 type = build_pointer_type (type);
5975 tree d = fold_convert (type, OMP_CLAUSE_DECL (c));
5976 t = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
5977 d, t);
5978 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c),
5979 MINUS_EXPR, sizetype,
5980 fold_convert (sizetype, t),
5981 fold_convert (sizetype, d));
5982 if (t == error_mark_node)
5984 remove = true;
5985 break;
5988 else if (TREE_CODE (type) == POINTER_TYPE
5989 /* Can't multiply the step yet if *this
5990 is still incomplete type. */
5991 && (ort != C_ORT_OMP_DECLARE_SIMD
5992 || TREE_CODE (OMP_CLAUSE_DECL (c)) != PARM_DECL
5993 || !DECL_ARTIFICIAL (OMP_CLAUSE_DECL (c))
5994 || DECL_NAME (OMP_CLAUSE_DECL (c))
5995 != this_identifier
5996 || !TYPE_BEING_DEFINED (TREE_TYPE (type))))
5998 tree d = convert_from_reference (OMP_CLAUSE_DECL (c));
5999 t = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
6000 d, t);
6001 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c),
6002 MINUS_EXPR, sizetype,
6003 fold_convert (sizetype, t),
6004 fold_convert (sizetype, d));
6005 if (t == error_mark_node)
6007 remove = true;
6008 break;
6011 else
6012 t = fold_convert (type, t);
6014 OMP_CLAUSE_LINEAR_STEP (c) = t;
6016 goto check_dup_generic;
6017 check_dup_generic:
6018 t = omp_clause_decl_field (OMP_CLAUSE_DECL (c));
6019 if (t)
6021 if (!remove && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_SHARED)
6022 omp_note_field_privatization (t, OMP_CLAUSE_DECL (c));
6024 else
6025 t = OMP_CLAUSE_DECL (c);
6026 check_dup_generic_t:
6027 if (t == current_class_ptr
6028 && (ort != C_ORT_OMP_DECLARE_SIMD
6029 || (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_LINEAR
6030 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_UNIFORM)))
6032 error ("%<this%> allowed in OpenMP only in %<declare simd%>"
6033 " clauses");
6034 remove = true;
6035 break;
6037 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL
6038 && (!field_ok || TREE_CODE (t) != FIELD_DECL))
6040 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
6041 break;
6042 if (DECL_P (t))
6043 error ("%qD is not a variable in clause %qs", t,
6044 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6045 else
6046 error ("%qE is not a variable in clause %qs", t,
6047 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6048 remove = true;
6050 else if (ort == C_ORT_ACC
6051 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
6053 if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
6055 error ("%qD appears more than once in reduction clauses", t);
6056 remove = true;
6058 else
6059 bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
6061 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
6062 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
6063 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
6065 error ("%qD appears more than once in data clauses", t);
6066 remove = true;
6068 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
6069 && bitmap_bit_p (&map_head, DECL_UID (t)))
6071 if (ort == C_ORT_ACC)
6072 error ("%qD appears more than once in data clauses", t);
6073 else
6074 error ("%qD appears both in data and map clauses", t);
6075 remove = true;
6077 else
6078 bitmap_set_bit (&generic_head, DECL_UID (t));
6079 if (!field_ok)
6080 break;
6081 handle_field_decl:
6082 if (!remove
6083 && TREE_CODE (t) == FIELD_DECL
6084 && t == OMP_CLAUSE_DECL (c)
6085 && ort != C_ORT_ACC)
6087 OMP_CLAUSE_DECL (c)
6088 = omp_privatize_field (t, (OMP_CLAUSE_CODE (c)
6089 == OMP_CLAUSE_SHARED));
6090 if (OMP_CLAUSE_DECL (c) == error_mark_node)
6091 remove = true;
6093 break;
6095 case OMP_CLAUSE_FIRSTPRIVATE:
6096 t = omp_clause_decl_field (OMP_CLAUSE_DECL (c));
6097 if (t)
6098 omp_note_field_privatization (t, OMP_CLAUSE_DECL (c));
6099 else
6100 t = OMP_CLAUSE_DECL (c);
6101 if (ort != C_ORT_ACC && t == current_class_ptr)
6103 error ("%<this%> allowed in OpenMP only in %<declare simd%>"
6104 " clauses");
6105 remove = true;
6106 break;
6108 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL
6109 && ((ort & C_ORT_OMP_DECLARE_SIMD) != C_ORT_OMP
6110 || TREE_CODE (t) != FIELD_DECL))
6112 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
6113 break;
6114 if (DECL_P (t))
6115 error ("%qD is not a variable in clause %<firstprivate%>", t);
6116 else
6117 error ("%qE is not a variable in clause %<firstprivate%>", t);
6118 remove = true;
6120 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
6121 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
6123 error ("%qD appears more than once in data clauses", t);
6124 remove = true;
6126 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
6128 if (ort == C_ORT_ACC)
6129 error ("%qD appears more than once in data clauses", t);
6130 else
6131 error ("%qD appears both in data and map clauses", t);
6132 remove = true;
6134 else
6135 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
6136 goto handle_field_decl;
6138 case OMP_CLAUSE_LASTPRIVATE:
6139 t = omp_clause_decl_field (OMP_CLAUSE_DECL (c));
6140 if (t)
6141 omp_note_field_privatization (t, OMP_CLAUSE_DECL (c));
6142 else
6143 t = OMP_CLAUSE_DECL (c);
6144 if (t == current_class_ptr)
6146 error ("%<this%> allowed in OpenMP only in %<declare simd%>"
6147 " clauses");
6148 remove = true;
6149 break;
6151 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL
6152 && ((ort & C_ORT_OMP_DECLARE_SIMD) != C_ORT_OMP
6153 || TREE_CODE (t) != FIELD_DECL))
6155 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
6156 break;
6157 if (DECL_P (t))
6158 error ("%qD is not a variable in clause %<lastprivate%>", t);
6159 else
6160 error ("%qE is not a variable in clause %<lastprivate%>", t);
6161 remove = true;
6163 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
6164 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
6166 error ("%qD appears more than once in data clauses", t);
6167 remove = true;
6169 else
6170 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
6171 goto handle_field_decl;
6173 case OMP_CLAUSE_IF:
6174 t = OMP_CLAUSE_IF_EXPR (c);
6175 t = maybe_convert_cond (t);
6176 if (t == error_mark_node)
6177 remove = true;
6178 else if (!processing_template_decl)
6179 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6180 OMP_CLAUSE_IF_EXPR (c) = t;
6181 break;
6183 case OMP_CLAUSE_FINAL:
6184 t = OMP_CLAUSE_FINAL_EXPR (c);
6185 t = maybe_convert_cond (t);
6186 if (t == error_mark_node)
6187 remove = true;
6188 else if (!processing_template_decl)
6189 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6190 OMP_CLAUSE_FINAL_EXPR (c) = t;
6191 break;
6193 case OMP_CLAUSE_GANG:
6194 /* Operand 1 is the gang static: argument. */
6195 t = OMP_CLAUSE_OPERAND (c, 1);
6196 if (t != NULL_TREE)
6198 if (t == error_mark_node)
6199 remove = true;
6200 else if (!type_dependent_expression_p (t)
6201 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6203 error ("%<gang%> static expression must be integral");
6204 remove = true;
6206 else
6208 t = mark_rvalue_use (t);
6209 if (!processing_template_decl)
6211 t = maybe_constant_value (t);
6212 if (TREE_CODE (t) == INTEGER_CST
6213 && tree_int_cst_sgn (t) != 1
6214 && t != integer_minus_one_node)
6216 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6217 "%<gang%> static value must be "
6218 "positive");
6219 t = integer_one_node;
6222 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6224 OMP_CLAUSE_OPERAND (c, 1) = t;
6226 /* Check operand 0, the num argument. */
6227 /* FALLTHRU */
6229 case OMP_CLAUSE_WORKER:
6230 case OMP_CLAUSE_VECTOR:
6231 if (OMP_CLAUSE_OPERAND (c, 0) == NULL_TREE)
6232 break;
6233 /* FALLTHRU */
6235 case OMP_CLAUSE_NUM_TASKS:
6236 case OMP_CLAUSE_NUM_TEAMS:
6237 case OMP_CLAUSE_NUM_THREADS:
6238 case OMP_CLAUSE_NUM_GANGS:
6239 case OMP_CLAUSE_NUM_WORKERS:
6240 case OMP_CLAUSE_VECTOR_LENGTH:
6241 t = OMP_CLAUSE_OPERAND (c, 0);
6242 if (t == error_mark_node)
6243 remove = true;
6244 else if (!type_dependent_expression_p (t)
6245 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6247 switch (OMP_CLAUSE_CODE (c))
6249 case OMP_CLAUSE_GANG:
6250 error_at (OMP_CLAUSE_LOCATION (c),
6251 "%<gang%> num expression must be integral"); break;
6252 case OMP_CLAUSE_VECTOR:
6253 error_at (OMP_CLAUSE_LOCATION (c),
6254 "%<vector%> length expression must be integral");
6255 break;
6256 case OMP_CLAUSE_WORKER:
6257 error_at (OMP_CLAUSE_LOCATION (c),
6258 "%<worker%> num expression must be integral");
6259 break;
6260 default:
6261 error_at (OMP_CLAUSE_LOCATION (c),
6262 "%qs expression must be integral",
6263 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6265 remove = true;
6267 else
6269 t = mark_rvalue_use (t);
6270 if (!processing_template_decl)
6272 t = maybe_constant_value (t);
6273 if (TREE_CODE (t) == INTEGER_CST
6274 && tree_int_cst_sgn (t) != 1)
6276 switch (OMP_CLAUSE_CODE (c))
6278 case OMP_CLAUSE_GANG:
6279 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6280 "%<gang%> num value must be positive");
6281 break;
6282 case OMP_CLAUSE_VECTOR:
6283 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6284 "%<vector%> length value must be "
6285 "positive");
6286 break;
6287 case OMP_CLAUSE_WORKER:
6288 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6289 "%<worker%> num value must be "
6290 "positive");
6291 break;
6292 default:
6293 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6294 "%qs value must be positive",
6295 omp_clause_code_name
6296 [OMP_CLAUSE_CODE (c)]);
6298 t = integer_one_node;
6300 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6302 OMP_CLAUSE_OPERAND (c, 0) = t;
6304 break;
6306 case OMP_CLAUSE_SCHEDULE:
6307 if (OMP_CLAUSE_SCHEDULE_KIND (c) & OMP_CLAUSE_SCHEDULE_NONMONOTONIC)
6309 const char *p = NULL;
6310 switch (OMP_CLAUSE_SCHEDULE_KIND (c) & OMP_CLAUSE_SCHEDULE_MASK)
6312 case OMP_CLAUSE_SCHEDULE_STATIC: p = "static"; break;
6313 case OMP_CLAUSE_SCHEDULE_DYNAMIC: break;
6314 case OMP_CLAUSE_SCHEDULE_GUIDED: break;
6315 case OMP_CLAUSE_SCHEDULE_AUTO: p = "auto"; break;
6316 case OMP_CLAUSE_SCHEDULE_RUNTIME: p = "runtime"; break;
6317 default: gcc_unreachable ();
6319 if (p)
6321 error_at (OMP_CLAUSE_LOCATION (c),
6322 "%<nonmonotonic%> modifier specified for %qs "
6323 "schedule kind", p);
6324 OMP_CLAUSE_SCHEDULE_KIND (c)
6325 = (enum omp_clause_schedule_kind)
6326 (OMP_CLAUSE_SCHEDULE_KIND (c)
6327 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
6331 t = OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c);
6332 if (t == NULL)
6334 else if (t == error_mark_node)
6335 remove = true;
6336 else if (!type_dependent_expression_p (t)
6337 && (OMP_CLAUSE_SCHEDULE_KIND (c)
6338 != OMP_CLAUSE_SCHEDULE_CILKFOR)
6339 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6341 error ("schedule chunk size expression must be integral");
6342 remove = true;
6344 else
6346 t = mark_rvalue_use (t);
6347 if (!processing_template_decl)
6349 if (OMP_CLAUSE_SCHEDULE_KIND (c)
6350 == OMP_CLAUSE_SCHEDULE_CILKFOR)
6352 t = convert_to_integer (long_integer_type_node, t);
6353 if (t == error_mark_node)
6355 remove = true;
6356 break;
6359 else
6361 t = maybe_constant_value (t);
6362 if (TREE_CODE (t) == INTEGER_CST
6363 && tree_int_cst_sgn (t) != 1)
6365 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6366 "chunk size value must be positive");
6367 t = integer_one_node;
6370 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6372 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
6374 break;
6376 case OMP_CLAUSE_SIMDLEN:
6377 case OMP_CLAUSE_SAFELEN:
6378 t = OMP_CLAUSE_OPERAND (c, 0);
6379 if (t == error_mark_node)
6380 remove = true;
6381 else if (!type_dependent_expression_p (t)
6382 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6384 error ("%qs length expression must be integral",
6385 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6386 remove = true;
6388 else
6390 t = mark_rvalue_use (t);
6391 if (!processing_template_decl)
6393 t = maybe_constant_value (t);
6394 if (TREE_CODE (t) != INTEGER_CST
6395 || tree_int_cst_sgn (t) != 1)
6397 error ("%qs length expression must be positive constant"
6398 " integer expression",
6399 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6400 remove = true;
6403 OMP_CLAUSE_OPERAND (c, 0) = t;
6404 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SAFELEN)
6405 safelen = c;
6407 break;
6409 case OMP_CLAUSE_ASYNC:
6410 t = OMP_CLAUSE_ASYNC_EXPR (c);
6411 if (t == error_mark_node)
6412 remove = true;
6413 else if (!type_dependent_expression_p (t)
6414 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6416 error ("%<async%> expression must be integral");
6417 remove = true;
6419 else
6421 t = mark_rvalue_use (t);
6422 if (!processing_template_decl)
6423 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6424 OMP_CLAUSE_ASYNC_EXPR (c) = t;
6426 break;
6428 case OMP_CLAUSE_WAIT:
6429 t = OMP_CLAUSE_WAIT_EXPR (c);
6430 if (t == error_mark_node)
6431 remove = true;
6432 else if (!processing_template_decl)
6433 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6434 OMP_CLAUSE_WAIT_EXPR (c) = t;
6435 break;
6437 case OMP_CLAUSE_THREAD_LIMIT:
6438 t = OMP_CLAUSE_THREAD_LIMIT_EXPR (c);
6439 if (t == error_mark_node)
6440 remove = true;
6441 else if (!type_dependent_expression_p (t)
6442 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6444 error ("%<thread_limit%> expression must be integral");
6445 remove = true;
6447 else
6449 t = mark_rvalue_use (t);
6450 if (!processing_template_decl)
6452 t = maybe_constant_value (t);
6453 if (TREE_CODE (t) == INTEGER_CST
6454 && tree_int_cst_sgn (t) != 1)
6456 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6457 "%<thread_limit%> value must be positive");
6458 t = integer_one_node;
6460 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6462 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
6464 break;
6466 case OMP_CLAUSE_DEVICE:
6467 t = OMP_CLAUSE_DEVICE_ID (c);
6468 if (t == error_mark_node)
6469 remove = true;
6470 else if (!type_dependent_expression_p (t)
6471 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6473 error ("%<device%> id must be integral");
6474 remove = true;
6476 else
6478 t = mark_rvalue_use (t);
6479 if (!processing_template_decl)
6480 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6481 OMP_CLAUSE_DEVICE_ID (c) = t;
6483 break;
6485 case OMP_CLAUSE_DIST_SCHEDULE:
6486 t = OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c);
6487 if (t == NULL)
6489 else if (t == error_mark_node)
6490 remove = true;
6491 else if (!type_dependent_expression_p (t)
6492 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6494 error ("%<dist_schedule%> chunk size expression must be "
6495 "integral");
6496 remove = true;
6498 else
6500 t = mark_rvalue_use (t);
6501 if (!processing_template_decl)
6502 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6503 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
6505 break;
6507 case OMP_CLAUSE_ALIGNED:
6508 t = OMP_CLAUSE_DECL (c);
6509 if (t == current_class_ptr && ort != C_ORT_OMP_DECLARE_SIMD)
6511 error ("%<this%> allowed in OpenMP only in %<declare simd%>"
6512 " clauses");
6513 remove = true;
6514 break;
6516 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
6518 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
6519 break;
6520 if (DECL_P (t))
6521 error ("%qD is not a variable in %<aligned%> clause", t);
6522 else
6523 error ("%qE is not a variable in %<aligned%> clause", t);
6524 remove = true;
6526 else if (!type_dependent_expression_p (t)
6527 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE
6528 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE
6529 && (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE
6530 || (!POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (t)))
6531 && (TREE_CODE (TREE_TYPE (TREE_TYPE (t)))
6532 != ARRAY_TYPE))))
6534 error_at (OMP_CLAUSE_LOCATION (c),
6535 "%qE in %<aligned%> clause is neither a pointer nor "
6536 "an array nor a reference to pointer or array", t);
6537 remove = true;
6539 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
6541 error ("%qD appears more than once in %<aligned%> clauses", t);
6542 remove = true;
6544 else
6545 bitmap_set_bit (&aligned_head, DECL_UID (t));
6546 t = OMP_CLAUSE_ALIGNED_ALIGNMENT (c);
6547 if (t == error_mark_node)
6548 remove = true;
6549 else if (t == NULL_TREE)
6550 break;
6551 else if (!type_dependent_expression_p (t)
6552 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6554 error ("%<aligned%> clause alignment expression must "
6555 "be integral");
6556 remove = true;
6558 else
6560 t = mark_rvalue_use (t);
6561 if (!processing_template_decl)
6563 t = maybe_constant_value (t);
6564 if (TREE_CODE (t) != INTEGER_CST
6565 || tree_int_cst_sgn (t) != 1)
6567 error ("%<aligned%> clause alignment expression must be "
6568 "positive constant integer expression");
6569 remove = true;
6572 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = t;
6574 break;
6576 case OMP_CLAUSE_DEPEND:
6577 t = OMP_CLAUSE_DECL (c);
6578 if (t == NULL_TREE)
6580 gcc_assert (OMP_CLAUSE_DEPEND_KIND (c)
6581 == OMP_CLAUSE_DEPEND_SOURCE);
6582 break;
6584 if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SINK)
6586 if (cp_finish_omp_clause_depend_sink (c))
6587 remove = true;
6588 break;
6590 if (TREE_CODE (t) == TREE_LIST)
6592 if (handle_omp_array_sections (c, ort))
6593 remove = true;
6594 break;
6596 if (t == error_mark_node)
6597 remove = true;
6598 else if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
6600 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
6601 break;
6602 if (DECL_P (t))
6603 error ("%qD is not a variable in %<depend%> clause", t);
6604 else
6605 error ("%qE is not a variable in %<depend%> clause", t);
6606 remove = true;
6608 else if (t == current_class_ptr)
6610 error ("%<this%> allowed in OpenMP only in %<declare simd%>"
6611 " clauses");
6612 remove = true;
6614 else if (!processing_template_decl
6615 && !cxx_mark_addressable (t))
6616 remove = true;
6617 break;
6619 case OMP_CLAUSE_MAP:
6620 case OMP_CLAUSE_TO:
6621 case OMP_CLAUSE_FROM:
6622 case OMP_CLAUSE__CACHE_:
6623 t = OMP_CLAUSE_DECL (c);
6624 if (TREE_CODE (t) == TREE_LIST)
6626 if (handle_omp_array_sections (c, ort))
6627 remove = true;
6628 else
6630 t = OMP_CLAUSE_DECL (c);
6631 if (TREE_CODE (t) != TREE_LIST
6632 && !type_dependent_expression_p (t)
6633 && !cp_omp_mappable_type (TREE_TYPE (t)))
6635 error_at (OMP_CLAUSE_LOCATION (c),
6636 "array section does not have mappable type "
6637 "in %qs clause",
6638 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6639 remove = true;
6641 while (TREE_CODE (t) == ARRAY_REF)
6642 t = TREE_OPERAND (t, 0);
6643 if (TREE_CODE (t) == COMPONENT_REF
6644 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
6646 while (TREE_CODE (t) == COMPONENT_REF)
6647 t = TREE_OPERAND (t, 0);
6648 if (REFERENCE_REF_P (t))
6649 t = TREE_OPERAND (t, 0);
6650 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
6651 break;
6652 if (bitmap_bit_p (&map_head, DECL_UID (t)))
6654 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
6655 error ("%qD appears more than once in motion"
6656 " clauses", t);
6657 else if (ort == C_ORT_ACC)
6658 error ("%qD appears more than once in data"
6659 " clauses", t);
6660 else
6661 error ("%qD appears more than once in map"
6662 " clauses", t);
6663 remove = true;
6665 else
6667 bitmap_set_bit (&map_head, DECL_UID (t));
6668 bitmap_set_bit (&map_field_head, DECL_UID (t));
6672 break;
6674 if (t == error_mark_node)
6676 remove = true;
6677 break;
6679 if (REFERENCE_REF_P (t)
6680 && TREE_CODE (TREE_OPERAND (t, 0)) == COMPONENT_REF)
6682 t = TREE_OPERAND (t, 0);
6683 OMP_CLAUSE_DECL (c) = t;
6685 if (TREE_CODE (t) == COMPONENT_REF
6686 && (ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP
6687 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE__CACHE_)
6689 if (type_dependent_expression_p (t))
6690 break;
6691 if (TREE_CODE (TREE_OPERAND (t, 1)) == FIELD_DECL
6692 && DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
6694 error_at (OMP_CLAUSE_LOCATION (c),
6695 "bit-field %qE in %qs clause",
6696 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6697 remove = true;
6699 else if (!cp_omp_mappable_type (TREE_TYPE (t)))
6701 error_at (OMP_CLAUSE_LOCATION (c),
6702 "%qE does not have a mappable type in %qs clause",
6703 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6704 remove = true;
6706 while (TREE_CODE (t) == COMPONENT_REF)
6708 if (TREE_TYPE (TREE_OPERAND (t, 0))
6709 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0)))
6710 == UNION_TYPE))
6712 error_at (OMP_CLAUSE_LOCATION (c),
6713 "%qE is a member of a union", t);
6714 remove = true;
6715 break;
6717 t = TREE_OPERAND (t, 0);
6719 if (remove)
6720 break;
6721 if (REFERENCE_REF_P (t))
6722 t = TREE_OPERAND (t, 0);
6723 if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
6725 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
6726 goto handle_map_references;
6729 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
6731 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
6732 break;
6733 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
6734 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
6735 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ALWAYS_POINTER))
6736 break;
6737 if (DECL_P (t))
6738 error ("%qD is not a variable in %qs clause", t,
6739 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6740 else
6741 error ("%qE is not a variable in %qs clause", t,
6742 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6743 remove = true;
6745 else if (VAR_P (t) && CP_DECL_THREAD_LOCAL_P (t))
6747 error ("%qD is threadprivate variable in %qs clause", t,
6748 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6749 remove = true;
6751 else if (ort != C_ORT_ACC && t == current_class_ptr)
6753 error ("%<this%> allowed in OpenMP only in %<declare simd%>"
6754 " clauses");
6755 remove = true;
6756 break;
6758 else if (!processing_template_decl
6759 && TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE
6760 && (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
6761 || (OMP_CLAUSE_MAP_KIND (c)
6762 != GOMP_MAP_FIRSTPRIVATE_POINTER))
6763 && !cxx_mark_addressable (t))
6764 remove = true;
6765 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
6766 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
6767 || (OMP_CLAUSE_MAP_KIND (c)
6768 == GOMP_MAP_FIRSTPRIVATE_POINTER)))
6769 && t == OMP_CLAUSE_DECL (c)
6770 && !type_dependent_expression_p (t)
6771 && !cp_omp_mappable_type ((TREE_CODE (TREE_TYPE (t))
6772 == REFERENCE_TYPE)
6773 ? TREE_TYPE (TREE_TYPE (t))
6774 : TREE_TYPE (t)))
6776 error_at (OMP_CLAUSE_LOCATION (c),
6777 "%qD does not have a mappable type in %qs clause", t,
6778 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6779 remove = true;
6781 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
6782 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FORCE_DEVICEPTR
6783 && !type_dependent_expression_p (t)
6784 && !POINTER_TYPE_P (TREE_TYPE (t)))
6786 error ("%qD is not a pointer variable", t);
6787 remove = true;
6789 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
6790 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
6792 if (bitmap_bit_p (&generic_head, DECL_UID (t))
6793 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
6795 error ("%qD appears more than once in data clauses", t);
6796 remove = true;
6798 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
6800 if (ort == C_ORT_ACC)
6801 error ("%qD appears more than once in data clauses", t);
6802 else
6803 error ("%qD appears both in data and map clauses", t);
6804 remove = true;
6806 else
6807 bitmap_set_bit (&generic_head, DECL_UID (t));
6809 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
6811 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
6812 error ("%qD appears more than once in motion clauses", t);
6813 if (ort == C_ORT_ACC)
6814 error ("%qD appears more than once in data clauses", t);
6815 else
6816 error ("%qD appears more than once in map clauses", t);
6817 remove = true;
6819 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
6820 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
6822 if (ort == C_ORT_ACC)
6823 error ("%qD appears more than once in data clauses", t);
6824 else
6825 error ("%qD appears both in data and map clauses", t);
6826 remove = true;
6828 else
6830 bitmap_set_bit (&map_head, DECL_UID (t));
6831 if (t != OMP_CLAUSE_DECL (c)
6832 && TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPONENT_REF)
6833 bitmap_set_bit (&map_field_head, DECL_UID (t));
6835 handle_map_references:
6836 if (!remove
6837 && !processing_template_decl
6838 && (ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP
6839 && TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c))) == REFERENCE_TYPE)
6841 t = OMP_CLAUSE_DECL (c);
6842 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
6844 OMP_CLAUSE_DECL (c) = build_simple_mem_ref (t);
6845 if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
6846 OMP_CLAUSE_SIZE (c)
6847 = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t)));
6849 else if (OMP_CLAUSE_MAP_KIND (c)
6850 != GOMP_MAP_FIRSTPRIVATE_POINTER
6851 && (OMP_CLAUSE_MAP_KIND (c)
6852 != GOMP_MAP_FIRSTPRIVATE_REFERENCE)
6853 && (OMP_CLAUSE_MAP_KIND (c)
6854 != GOMP_MAP_ALWAYS_POINTER))
6856 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
6857 OMP_CLAUSE_MAP);
6858 if (TREE_CODE (t) == COMPONENT_REF)
6859 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ALWAYS_POINTER);
6860 else
6861 OMP_CLAUSE_SET_MAP_KIND (c2,
6862 GOMP_MAP_FIRSTPRIVATE_REFERENCE);
6863 OMP_CLAUSE_DECL (c2) = t;
6864 OMP_CLAUSE_SIZE (c2) = size_zero_node;
6865 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
6866 OMP_CLAUSE_CHAIN (c) = c2;
6867 OMP_CLAUSE_DECL (c) = build_simple_mem_ref (t);
6868 if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
6869 OMP_CLAUSE_SIZE (c)
6870 = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t)));
6871 c = c2;
6874 break;
6876 case OMP_CLAUSE_TO_DECLARE:
6877 case OMP_CLAUSE_LINK:
6878 t = OMP_CLAUSE_DECL (c);
6879 if (TREE_CODE (t) == FUNCTION_DECL
6880 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
6882 else if (!VAR_P (t))
6884 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
6886 if (TREE_CODE (t) == TEMPLATE_ID_EXPR)
6887 error_at (OMP_CLAUSE_LOCATION (c),
6888 "template %qE in clause %qs", t,
6889 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6890 else if (really_overloaded_fn (t))
6891 error_at (OMP_CLAUSE_LOCATION (c),
6892 "overloaded function name %qE in clause %qs", t,
6893 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6894 else
6895 error_at (OMP_CLAUSE_LOCATION (c),
6896 "%qE is neither a variable nor a function name "
6897 "in clause %qs", t,
6898 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6900 else
6901 error_at (OMP_CLAUSE_LOCATION (c),
6902 "%qE is not a variable in clause %qs", t,
6903 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6904 remove = true;
6906 else if (DECL_THREAD_LOCAL_P (t))
6908 error_at (OMP_CLAUSE_LOCATION (c),
6909 "%qD is threadprivate variable in %qs clause", t,
6910 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6911 remove = true;
6913 else if (!cp_omp_mappable_type (TREE_TYPE (t)))
6915 error_at (OMP_CLAUSE_LOCATION (c),
6916 "%qD does not have a mappable type in %qs clause", t,
6917 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6918 remove = true;
6920 if (remove)
6921 break;
6922 if (bitmap_bit_p (&generic_head, DECL_UID (t)))
6924 error_at (OMP_CLAUSE_LOCATION (c),
6925 "%qE appears more than once on the same "
6926 "%<declare target%> directive", t);
6927 remove = true;
6929 else
6930 bitmap_set_bit (&generic_head, DECL_UID (t));
6931 break;
6933 case OMP_CLAUSE_UNIFORM:
6934 t = OMP_CLAUSE_DECL (c);
6935 if (TREE_CODE (t) != PARM_DECL)
6937 if (processing_template_decl)
6938 break;
6939 if (DECL_P (t))
6940 error ("%qD is not an argument in %<uniform%> clause", t);
6941 else
6942 error ("%qE is not an argument in %<uniform%> clause", t);
6943 remove = true;
6944 break;
6946 /* map_head bitmap is used as uniform_head if declare_simd. */
6947 bitmap_set_bit (&map_head, DECL_UID (t));
6948 goto check_dup_generic;
6950 case OMP_CLAUSE_GRAINSIZE:
6951 t = OMP_CLAUSE_GRAINSIZE_EXPR (c);
6952 if (t == error_mark_node)
6953 remove = true;
6954 else if (!type_dependent_expression_p (t)
6955 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6957 error ("%<grainsize%> expression must be integral");
6958 remove = true;
6960 else
6962 t = mark_rvalue_use (t);
6963 if (!processing_template_decl)
6965 t = maybe_constant_value (t);
6966 if (TREE_CODE (t) == INTEGER_CST
6967 && tree_int_cst_sgn (t) != 1)
6969 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6970 "%<grainsize%> value must be positive");
6971 t = integer_one_node;
6973 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6975 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
6977 break;
6979 case OMP_CLAUSE_PRIORITY:
6980 t = OMP_CLAUSE_PRIORITY_EXPR (c);
6981 if (t == error_mark_node)
6982 remove = true;
6983 else if (!type_dependent_expression_p (t)
6984 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6986 error ("%<priority%> expression must be integral");
6987 remove = true;
6989 else
6991 t = mark_rvalue_use (t);
6992 if (!processing_template_decl)
6994 t = maybe_constant_value (t);
6995 if (TREE_CODE (t) == INTEGER_CST
6996 && tree_int_cst_sgn (t) == -1)
6998 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6999 "%<priority%> value must be non-negative");
7000 t = integer_one_node;
7002 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7004 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
7006 break;
7008 case OMP_CLAUSE_HINT:
7009 t = OMP_CLAUSE_HINT_EXPR (c);
7010 if (t == error_mark_node)
7011 remove = true;
7012 else if (!type_dependent_expression_p (t)
7013 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
7015 error ("%<num_tasks%> expression must be integral");
7016 remove = true;
7018 else
7020 t = mark_rvalue_use (t);
7021 if (!processing_template_decl)
7023 t = maybe_constant_value (t);
7024 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7026 OMP_CLAUSE_HINT_EXPR (c) = t;
7028 break;
7030 case OMP_CLAUSE_IS_DEVICE_PTR:
7031 case OMP_CLAUSE_USE_DEVICE_PTR:
7032 field_ok = (ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP;
7033 t = OMP_CLAUSE_DECL (c);
7034 if (!type_dependent_expression_p (t))
7036 tree type = TREE_TYPE (t);
7037 if (TREE_CODE (type) != POINTER_TYPE
7038 && TREE_CODE (type) != ARRAY_TYPE
7039 && (TREE_CODE (type) != REFERENCE_TYPE
7040 || (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
7041 && TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE)))
7043 error_at (OMP_CLAUSE_LOCATION (c),
7044 "%qs variable is neither a pointer, nor an array "
7045 "nor reference to pointer or array",
7046 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7047 remove = true;
7050 goto check_dup_generic;
7052 case OMP_CLAUSE_NOWAIT:
7053 case OMP_CLAUSE_DEFAULT:
7054 case OMP_CLAUSE_UNTIED:
7055 case OMP_CLAUSE_COLLAPSE:
7056 case OMP_CLAUSE_MERGEABLE:
7057 case OMP_CLAUSE_PARALLEL:
7058 case OMP_CLAUSE_FOR:
7059 case OMP_CLAUSE_SECTIONS:
7060 case OMP_CLAUSE_TASKGROUP:
7061 case OMP_CLAUSE_PROC_BIND:
7062 case OMP_CLAUSE_NOGROUP:
7063 case OMP_CLAUSE_THREADS:
7064 case OMP_CLAUSE_SIMD:
7065 case OMP_CLAUSE_DEFAULTMAP:
7066 case OMP_CLAUSE__CILK_FOR_COUNT_:
7067 case OMP_CLAUSE_AUTO:
7068 case OMP_CLAUSE_INDEPENDENT:
7069 case OMP_CLAUSE_SEQ:
7070 break;
7072 case OMP_CLAUSE_TILE:
7073 for (tree list = OMP_CLAUSE_TILE_LIST (c); !remove && list;
7074 list = TREE_CHAIN (list))
7076 t = TREE_VALUE (list);
7078 if (t == error_mark_node)
7079 remove = true;
7080 else if (!type_dependent_expression_p (t)
7081 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
7083 error_at (OMP_CLAUSE_LOCATION (c),
7084 "%<tile%> argument needs integral type");
7085 remove = true;
7087 else
7089 t = mark_rvalue_use (t);
7090 if (!processing_template_decl)
7092 /* Zero is used to indicate '*', we permit you
7093 to get there via an ICE of value zero. */
7094 t = maybe_constant_value (t);
7095 if (!tree_fits_shwi_p (t)
7096 || tree_to_shwi (t) < 0)
7098 error_at (OMP_CLAUSE_LOCATION (c),
7099 "%<tile%> argument needs positive "
7100 "integral constant");
7101 remove = true;
7104 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7107 /* Update list item. */
7108 TREE_VALUE (list) = t;
7110 break;
7112 case OMP_CLAUSE_ORDERED:
7113 ordered_seen = true;
7114 break;
7116 case OMP_CLAUSE_INBRANCH:
7117 case OMP_CLAUSE_NOTINBRANCH:
7118 if (branch_seen)
7120 error ("%<inbranch%> clause is incompatible with "
7121 "%<notinbranch%>");
7122 remove = true;
7124 branch_seen = true;
7125 break;
7127 default:
7128 gcc_unreachable ();
7131 if (remove)
7132 *pc = OMP_CLAUSE_CHAIN (c);
7133 else
7134 pc = &OMP_CLAUSE_CHAIN (c);
7137 for (pc = &clauses, c = clauses; c ; c = *pc)
7139 enum omp_clause_code c_kind = OMP_CLAUSE_CODE (c);
7140 bool remove = false;
7141 bool need_complete_type = false;
7142 bool need_default_ctor = false;
7143 bool need_copy_ctor = false;
7144 bool need_copy_assignment = false;
7145 bool need_implicitly_determined = false;
7146 bool need_dtor = false;
7147 tree type, inner_type;
7149 switch (c_kind)
7151 case OMP_CLAUSE_SHARED:
7152 need_implicitly_determined = true;
7153 break;
7154 case OMP_CLAUSE_PRIVATE:
7155 need_complete_type = true;
7156 need_default_ctor = true;
7157 need_dtor = true;
7158 need_implicitly_determined = true;
7159 break;
7160 case OMP_CLAUSE_FIRSTPRIVATE:
7161 need_complete_type = true;
7162 need_copy_ctor = true;
7163 need_dtor = true;
7164 need_implicitly_determined = true;
7165 break;
7166 case OMP_CLAUSE_LASTPRIVATE:
7167 need_complete_type = true;
7168 need_copy_assignment = true;
7169 need_implicitly_determined = true;
7170 break;
7171 case OMP_CLAUSE_REDUCTION:
7172 need_implicitly_determined = true;
7173 break;
7174 case OMP_CLAUSE_LINEAR:
7175 if (ort != C_ORT_OMP_DECLARE_SIMD)
7176 need_implicitly_determined = true;
7177 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c)
7178 && !bitmap_bit_p (&map_head,
7179 DECL_UID (OMP_CLAUSE_LINEAR_STEP (c))))
7181 error_at (OMP_CLAUSE_LOCATION (c),
7182 "%<linear%> clause step is a parameter %qD not "
7183 "specified in %<uniform%> clause",
7184 OMP_CLAUSE_LINEAR_STEP (c));
7185 *pc = OMP_CLAUSE_CHAIN (c);
7186 continue;
7188 break;
7189 case OMP_CLAUSE_COPYPRIVATE:
7190 need_copy_assignment = true;
7191 break;
7192 case OMP_CLAUSE_COPYIN:
7193 need_copy_assignment = true;
7194 break;
7195 case OMP_CLAUSE_SIMDLEN:
7196 if (safelen
7197 && !processing_template_decl
7198 && tree_int_cst_lt (OMP_CLAUSE_SAFELEN_EXPR (safelen),
7199 OMP_CLAUSE_SIMDLEN_EXPR (c)))
7201 error_at (OMP_CLAUSE_LOCATION (c),
7202 "%<simdlen%> clause value is bigger than "
7203 "%<safelen%> clause value");
7204 OMP_CLAUSE_SIMDLEN_EXPR (c)
7205 = OMP_CLAUSE_SAFELEN_EXPR (safelen);
7207 pc = &OMP_CLAUSE_CHAIN (c);
7208 continue;
7209 case OMP_CLAUSE_SCHEDULE:
7210 if (ordered_seen
7211 && (OMP_CLAUSE_SCHEDULE_KIND (c)
7212 & OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
7214 error_at (OMP_CLAUSE_LOCATION (c),
7215 "%<nonmonotonic%> schedule modifier specified "
7216 "together with %<ordered%> clause");
7217 OMP_CLAUSE_SCHEDULE_KIND (c)
7218 = (enum omp_clause_schedule_kind)
7219 (OMP_CLAUSE_SCHEDULE_KIND (c)
7220 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
7222 pc = &OMP_CLAUSE_CHAIN (c);
7223 continue;
7224 case OMP_CLAUSE_NOWAIT:
7225 if (copyprivate_seen)
7227 error_at (OMP_CLAUSE_LOCATION (c),
7228 "%<nowait%> clause must not be used together "
7229 "with %<copyprivate%>");
7230 *pc = OMP_CLAUSE_CHAIN (c);
7231 continue;
7233 /* FALLTHRU */
7234 default:
7235 pc = &OMP_CLAUSE_CHAIN (c);
7236 continue;
7239 t = OMP_CLAUSE_DECL (c);
7240 if (processing_template_decl
7241 && !VAR_P (t) && TREE_CODE (t) != PARM_DECL)
7243 pc = &OMP_CLAUSE_CHAIN (c);
7244 continue;
7247 switch (c_kind)
7249 case OMP_CLAUSE_LASTPRIVATE:
7250 if (!bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
7252 need_default_ctor = true;
7253 need_dtor = true;
7255 break;
7257 case OMP_CLAUSE_REDUCTION:
7258 if (finish_omp_reduction_clause (c, &need_default_ctor,
7259 &need_dtor))
7260 remove = true;
7261 else
7262 t = OMP_CLAUSE_DECL (c);
7263 break;
7265 case OMP_CLAUSE_COPYIN:
7266 if (!VAR_P (t) || !CP_DECL_THREAD_LOCAL_P (t))
7268 error ("%qE must be %<threadprivate%> for %<copyin%>", t);
7269 remove = true;
7271 break;
7273 default:
7274 break;
7277 if (need_complete_type || need_copy_assignment)
7279 t = require_complete_type (t);
7280 if (t == error_mark_node)
7281 remove = true;
7282 else if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE
7283 && !complete_type_or_else (TREE_TYPE (TREE_TYPE (t)), t))
7284 remove = true;
7286 if (need_implicitly_determined)
7288 const char *share_name = NULL;
7290 if (VAR_P (t) && CP_DECL_THREAD_LOCAL_P (t))
7291 share_name = "threadprivate";
7292 else switch (cxx_omp_predetermined_sharing (t))
7294 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
7295 break;
7296 case OMP_CLAUSE_DEFAULT_SHARED:
7297 /* const vars may be specified in firstprivate clause. */
7298 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
7299 && cxx_omp_const_qual_no_mutable (t))
7300 break;
7301 share_name = "shared";
7302 break;
7303 case OMP_CLAUSE_DEFAULT_PRIVATE:
7304 share_name = "private";
7305 break;
7306 default:
7307 gcc_unreachable ();
7309 if (share_name)
7311 error ("%qE is predetermined %qs for %qs",
7312 omp_clause_printable_decl (t), share_name,
7313 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7314 remove = true;
7318 /* We're interested in the base element, not arrays. */
7319 inner_type = type = TREE_TYPE (t);
7320 if ((need_complete_type
7321 || need_copy_assignment
7322 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
7323 && TREE_CODE (inner_type) == REFERENCE_TYPE)
7324 inner_type = TREE_TYPE (inner_type);
7325 while (TREE_CODE (inner_type) == ARRAY_TYPE)
7326 inner_type = TREE_TYPE (inner_type);
7328 /* Check for special function availability by building a call to one.
7329 Save the results, because later we won't be in the right context
7330 for making these queries. */
7331 if (CLASS_TYPE_P (inner_type)
7332 && COMPLETE_TYPE_P (inner_type)
7333 && (need_default_ctor || need_copy_ctor
7334 || need_copy_assignment || need_dtor)
7335 && !type_dependent_expression_p (t)
7336 && cxx_omp_create_clause_info (c, inner_type, need_default_ctor,
7337 need_copy_ctor, need_copy_assignment,
7338 need_dtor))
7339 remove = true;
7341 if (!remove
7342 && c_kind == OMP_CLAUSE_SHARED
7343 && processing_template_decl)
7345 t = omp_clause_decl_field (OMP_CLAUSE_DECL (c));
7346 if (t)
7347 OMP_CLAUSE_DECL (c) = t;
7350 if (remove)
7351 *pc = OMP_CLAUSE_CHAIN (c);
7352 else
7353 pc = &OMP_CLAUSE_CHAIN (c);
7356 bitmap_obstack_release (NULL);
7357 return clauses;
7360 /* Start processing OpenMP clauses that can include any
7361 privatization clauses for non-static data members. */
7363 tree
7364 push_omp_privatization_clauses (bool ignore_next)
7366 if (omp_private_member_ignore_next)
7368 omp_private_member_ignore_next = ignore_next;
7369 return NULL_TREE;
7371 omp_private_member_ignore_next = ignore_next;
7372 if (omp_private_member_map)
7373 omp_private_member_vec.safe_push (error_mark_node);
7374 return push_stmt_list ();
7377 /* Revert remapping of any non-static data members since
7378 the last push_omp_privatization_clauses () call. */
7380 void
7381 pop_omp_privatization_clauses (tree stmt)
7383 if (stmt == NULL_TREE)
7384 return;
7385 stmt = pop_stmt_list (stmt);
7386 if (omp_private_member_map)
7388 while (!omp_private_member_vec.is_empty ())
7390 tree t = omp_private_member_vec.pop ();
7391 if (t == error_mark_node)
7393 add_stmt (stmt);
7394 return;
7396 bool no_decl_expr = t == integer_zero_node;
7397 if (no_decl_expr)
7398 t = omp_private_member_vec.pop ();
7399 tree *v = omp_private_member_map->get (t);
7400 gcc_assert (v);
7401 if (!no_decl_expr)
7402 add_decl_expr (*v);
7403 omp_private_member_map->remove (t);
7405 delete omp_private_member_map;
7406 omp_private_member_map = NULL;
7408 add_stmt (stmt);
7411 /* Remember OpenMP privatization clauses mapping and clear it.
7412 Used for lambdas. */
7414 void
7415 save_omp_privatization_clauses (vec<tree> &save)
7417 save = vNULL;
7418 if (omp_private_member_ignore_next)
7419 save.safe_push (integer_one_node);
7420 omp_private_member_ignore_next = false;
7421 if (!omp_private_member_map)
7422 return;
7424 while (!omp_private_member_vec.is_empty ())
7426 tree t = omp_private_member_vec.pop ();
7427 if (t == error_mark_node)
7429 save.safe_push (t);
7430 continue;
7432 tree n = t;
7433 if (t == integer_zero_node)
7434 t = omp_private_member_vec.pop ();
7435 tree *v = omp_private_member_map->get (t);
7436 gcc_assert (v);
7437 save.safe_push (*v);
7438 save.safe_push (t);
7439 if (n != t)
7440 save.safe_push (n);
7442 delete omp_private_member_map;
7443 omp_private_member_map = NULL;
7446 /* Restore OpenMP privatization clauses mapping saved by the
7447 above function. */
7449 void
7450 restore_omp_privatization_clauses (vec<tree> &save)
7452 gcc_assert (omp_private_member_vec.is_empty ());
7453 omp_private_member_ignore_next = false;
7454 if (save.is_empty ())
7455 return;
7456 if (save.length () == 1 && save[0] == integer_one_node)
7458 omp_private_member_ignore_next = true;
7459 save.release ();
7460 return;
7463 omp_private_member_map = new hash_map <tree, tree>;
7464 while (!save.is_empty ())
7466 tree t = save.pop ();
7467 tree n = t;
7468 if (t != error_mark_node)
7470 if (t == integer_one_node)
7472 omp_private_member_ignore_next = true;
7473 gcc_assert (save.is_empty ());
7474 break;
7476 if (t == integer_zero_node)
7477 t = save.pop ();
7478 tree &v = omp_private_member_map->get_or_insert (t);
7479 v = save.pop ();
7481 omp_private_member_vec.safe_push (t);
7482 if (n != t)
7483 omp_private_member_vec.safe_push (n);
7485 save.release ();
7488 /* For all variables in the tree_list VARS, mark them as thread local. */
7490 void
7491 finish_omp_threadprivate (tree vars)
7493 tree t;
7495 /* Mark every variable in VARS to be assigned thread local storage. */
7496 for (t = vars; t; t = TREE_CHAIN (t))
7498 tree v = TREE_PURPOSE (t);
7500 if (error_operand_p (v))
7502 else if (!VAR_P (v))
7503 error ("%<threadprivate%> %qD is not file, namespace "
7504 "or block scope variable", v);
7505 /* If V had already been marked threadprivate, it doesn't matter
7506 whether it had been used prior to this point. */
7507 else if (TREE_USED (v)
7508 && (DECL_LANG_SPECIFIC (v) == NULL
7509 || !CP_DECL_THREADPRIVATE_P (v)))
7510 error ("%qE declared %<threadprivate%> after first use", v);
7511 else if (! TREE_STATIC (v) && ! DECL_EXTERNAL (v))
7512 error ("automatic variable %qE cannot be %<threadprivate%>", v);
7513 else if (! COMPLETE_TYPE_P (complete_type (TREE_TYPE (v))))
7514 error ("%<threadprivate%> %qE has incomplete type", v);
7515 else if (TREE_STATIC (v) && TYPE_P (CP_DECL_CONTEXT (v))
7516 && CP_DECL_CONTEXT (v) != current_class_type)
7517 error ("%<threadprivate%> %qE directive not "
7518 "in %qT definition", v, CP_DECL_CONTEXT (v));
7519 else
7521 /* Allocate a LANG_SPECIFIC structure for V, if needed. */
7522 if (DECL_LANG_SPECIFIC (v) == NULL)
7524 retrofit_lang_decl (v);
7526 /* Make sure that DECL_DISCRIMINATOR_P continues to be true
7527 after the allocation of the lang_decl structure. */
7528 if (DECL_DISCRIMINATOR_P (v))
7529 DECL_LANG_SPECIFIC (v)->u.base.u2sel = 1;
7532 if (! CP_DECL_THREAD_LOCAL_P (v))
7534 CP_DECL_THREAD_LOCAL_P (v) = true;
7535 set_decl_tls_model (v, decl_default_tls_model (v));
7536 /* If rtl has been already set for this var, call
7537 make_decl_rtl once again, so that encode_section_info
7538 has a chance to look at the new decl flags. */
7539 if (DECL_RTL_SET_P (v))
7540 make_decl_rtl (v);
7542 CP_DECL_THREADPRIVATE_P (v) = 1;
7547 /* Build an OpenMP structured block. */
7549 tree
7550 begin_omp_structured_block (void)
7552 return do_pushlevel (sk_omp);
7555 tree
7556 finish_omp_structured_block (tree block)
7558 return do_poplevel (block);
7561 /* Similarly, except force the retention of the BLOCK. */
7563 tree
7564 begin_omp_parallel (void)
7566 keep_next_level (true);
7567 return begin_omp_structured_block ();
7570 /* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
7571 statement. */
7573 tree
7574 finish_oacc_data (tree clauses, tree block)
7576 tree stmt;
7578 block = finish_omp_structured_block (block);
7580 stmt = make_node (OACC_DATA);
7581 TREE_TYPE (stmt) = void_type_node;
7582 OACC_DATA_CLAUSES (stmt) = clauses;
7583 OACC_DATA_BODY (stmt) = block;
7585 return add_stmt (stmt);
7588 /* Generate OACC_HOST_DATA, with CLAUSES and BLOCK as its compound
7589 statement. */
7591 tree
7592 finish_oacc_host_data (tree clauses, tree block)
7594 tree stmt;
7596 block = finish_omp_structured_block (block);
7598 stmt = make_node (OACC_HOST_DATA);
7599 TREE_TYPE (stmt) = void_type_node;
7600 OACC_HOST_DATA_CLAUSES (stmt) = clauses;
7601 OACC_HOST_DATA_BODY (stmt) = block;
7603 return add_stmt (stmt);
7606 /* Generate OMP construct CODE, with BODY and CLAUSES as its compound
7607 statement. */
7609 tree
7610 finish_omp_construct (enum tree_code code, tree body, tree clauses)
7612 body = finish_omp_structured_block (body);
7614 tree stmt = make_node (code);
7615 TREE_TYPE (stmt) = void_type_node;
7616 OMP_BODY (stmt) = body;
7617 OMP_CLAUSES (stmt) = clauses;
7619 return add_stmt (stmt);
7622 tree
7623 finish_omp_parallel (tree clauses, tree body)
7625 tree stmt;
7627 body = finish_omp_structured_block (body);
7629 stmt = make_node (OMP_PARALLEL);
7630 TREE_TYPE (stmt) = void_type_node;
7631 OMP_PARALLEL_CLAUSES (stmt) = clauses;
7632 OMP_PARALLEL_BODY (stmt) = body;
7634 return add_stmt (stmt);
7637 tree
7638 begin_omp_task (void)
7640 keep_next_level (true);
7641 return begin_omp_structured_block ();
7644 tree
7645 finish_omp_task (tree clauses, tree body)
7647 tree stmt;
7649 body = finish_omp_structured_block (body);
7651 stmt = make_node (OMP_TASK);
7652 TREE_TYPE (stmt) = void_type_node;
7653 OMP_TASK_CLAUSES (stmt) = clauses;
7654 OMP_TASK_BODY (stmt) = body;
7656 return add_stmt (stmt);
7659 /* Helper function for finish_omp_for. Convert Ith random access iterator
7660 into integral iterator. Return FALSE if successful. */
7662 static bool
7663 handle_omp_for_class_iterator (int i, location_t locus, enum tree_code code,
7664 tree declv, tree orig_declv, tree initv,
7665 tree condv, tree incrv, tree *body,
7666 tree *pre_body, tree &clauses, tree *lastp,
7667 int collapse, int ordered)
7669 tree diff, iter_init, iter_incr = NULL, last;
7670 tree incr_var = NULL, orig_pre_body, orig_body, c;
7671 tree decl = TREE_VEC_ELT (declv, i);
7672 tree init = TREE_VEC_ELT (initv, i);
7673 tree cond = TREE_VEC_ELT (condv, i);
7674 tree incr = TREE_VEC_ELT (incrv, i);
7675 tree iter = decl;
7676 location_t elocus = locus;
7678 if (init && EXPR_HAS_LOCATION (init))
7679 elocus = EXPR_LOCATION (init);
7681 cond = cp_fully_fold (cond);
7682 switch (TREE_CODE (cond))
7684 case GT_EXPR:
7685 case GE_EXPR:
7686 case LT_EXPR:
7687 case LE_EXPR:
7688 case NE_EXPR:
7689 if (TREE_OPERAND (cond, 1) == iter)
7690 cond = build2 (swap_tree_comparison (TREE_CODE (cond)),
7691 TREE_TYPE (cond), iter, TREE_OPERAND (cond, 0));
7692 if (TREE_OPERAND (cond, 0) != iter)
7693 cond = error_mark_node;
7694 else
7696 tree tem = build_x_binary_op (EXPR_LOCATION (cond),
7697 TREE_CODE (cond),
7698 iter, ERROR_MARK,
7699 TREE_OPERAND (cond, 1), ERROR_MARK,
7700 NULL, tf_warning_or_error);
7701 if (error_operand_p (tem))
7702 return true;
7704 break;
7705 default:
7706 cond = error_mark_node;
7707 break;
7709 if (cond == error_mark_node)
7711 error_at (elocus, "invalid controlling predicate");
7712 return true;
7714 diff = build_x_binary_op (elocus, MINUS_EXPR, TREE_OPERAND (cond, 1),
7715 ERROR_MARK, iter, ERROR_MARK, NULL,
7716 tf_warning_or_error);
7717 diff = cp_fully_fold (diff);
7718 if (error_operand_p (diff))
7719 return true;
7720 if (TREE_CODE (TREE_TYPE (diff)) != INTEGER_TYPE)
7722 error_at (elocus, "difference between %qE and %qD does not have integer type",
7723 TREE_OPERAND (cond, 1), iter);
7724 return true;
7726 if (!c_omp_check_loop_iv_exprs (locus, orig_declv,
7727 TREE_VEC_ELT (declv, i), NULL_TREE,
7728 cond, cp_walk_subtrees))
7729 return true;
7731 switch (TREE_CODE (incr))
7733 case PREINCREMENT_EXPR:
7734 case PREDECREMENT_EXPR:
7735 case POSTINCREMENT_EXPR:
7736 case POSTDECREMENT_EXPR:
7737 if (TREE_OPERAND (incr, 0) != iter)
7739 incr = error_mark_node;
7740 break;
7742 iter_incr = build_x_unary_op (EXPR_LOCATION (incr),
7743 TREE_CODE (incr), iter,
7744 tf_warning_or_error);
7745 if (error_operand_p (iter_incr))
7746 return true;
7747 else if (TREE_CODE (incr) == PREINCREMENT_EXPR
7748 || TREE_CODE (incr) == POSTINCREMENT_EXPR)
7749 incr = integer_one_node;
7750 else
7751 incr = integer_minus_one_node;
7752 break;
7753 case MODIFY_EXPR:
7754 if (TREE_OPERAND (incr, 0) != iter)
7755 incr = error_mark_node;
7756 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
7757 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
7759 tree rhs = TREE_OPERAND (incr, 1);
7760 if (TREE_OPERAND (rhs, 0) == iter)
7762 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (rhs, 1)))
7763 != INTEGER_TYPE)
7764 incr = error_mark_node;
7765 else
7767 iter_incr = build_x_modify_expr (EXPR_LOCATION (rhs),
7768 iter, TREE_CODE (rhs),
7769 TREE_OPERAND (rhs, 1),
7770 tf_warning_or_error);
7771 if (error_operand_p (iter_incr))
7772 return true;
7773 incr = TREE_OPERAND (rhs, 1);
7774 incr = cp_convert (TREE_TYPE (diff), incr,
7775 tf_warning_or_error);
7776 if (TREE_CODE (rhs) == MINUS_EXPR)
7778 incr = build1 (NEGATE_EXPR, TREE_TYPE (diff), incr);
7779 incr = fold_simple (incr);
7781 if (TREE_CODE (incr) != INTEGER_CST
7782 && (TREE_CODE (incr) != NOP_EXPR
7783 || (TREE_CODE (TREE_OPERAND (incr, 0))
7784 != INTEGER_CST)))
7785 iter_incr = NULL;
7788 else if (TREE_OPERAND (rhs, 1) == iter)
7790 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (rhs, 0))) != INTEGER_TYPE
7791 || TREE_CODE (rhs) != PLUS_EXPR)
7792 incr = error_mark_node;
7793 else
7795 iter_incr = build_x_binary_op (EXPR_LOCATION (rhs),
7796 PLUS_EXPR,
7797 TREE_OPERAND (rhs, 0),
7798 ERROR_MARK, iter,
7799 ERROR_MARK, NULL,
7800 tf_warning_or_error);
7801 if (error_operand_p (iter_incr))
7802 return true;
7803 iter_incr = build_x_modify_expr (EXPR_LOCATION (rhs),
7804 iter, NOP_EXPR,
7805 iter_incr,
7806 tf_warning_or_error);
7807 if (error_operand_p (iter_incr))
7808 return true;
7809 incr = TREE_OPERAND (rhs, 0);
7810 iter_incr = NULL;
7813 else
7814 incr = error_mark_node;
7816 else
7817 incr = error_mark_node;
7818 break;
7819 default:
7820 incr = error_mark_node;
7821 break;
7824 if (incr == error_mark_node)
7826 error_at (elocus, "invalid increment expression");
7827 return true;
7830 incr = cp_convert (TREE_TYPE (diff), incr, tf_warning_or_error);
7831 bool taskloop_iv_seen = false;
7832 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
7833 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
7834 && OMP_CLAUSE_DECL (c) == iter)
7836 if (code == OMP_TASKLOOP)
7838 taskloop_iv_seen = true;
7839 OMP_CLAUSE_LASTPRIVATE_TASKLOOP_IV (c) = 1;
7841 break;
7843 else if (code == OMP_TASKLOOP
7844 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
7845 && OMP_CLAUSE_DECL (c) == iter)
7847 taskloop_iv_seen = true;
7848 OMP_CLAUSE_PRIVATE_TASKLOOP_IV (c) = 1;
7851 decl = create_temporary_var (TREE_TYPE (diff));
7852 pushdecl (decl);
7853 add_decl_expr (decl);
7854 last = create_temporary_var (TREE_TYPE (diff));
7855 pushdecl (last);
7856 add_decl_expr (last);
7857 if (c && iter_incr == NULL && TREE_CODE (incr) != INTEGER_CST
7858 && (!ordered || (i < collapse && collapse > 1)))
7860 incr_var = create_temporary_var (TREE_TYPE (diff));
7861 pushdecl (incr_var);
7862 add_decl_expr (incr_var);
7864 gcc_assert (stmts_are_full_exprs_p ());
7865 tree diffvar = NULL_TREE;
7866 if (code == OMP_TASKLOOP)
7868 if (!taskloop_iv_seen)
7870 tree ivc = build_omp_clause (locus, OMP_CLAUSE_FIRSTPRIVATE);
7871 OMP_CLAUSE_DECL (ivc) = iter;
7872 cxx_omp_finish_clause (ivc, NULL);
7873 OMP_CLAUSE_CHAIN (ivc) = clauses;
7874 clauses = ivc;
7876 tree lvc = build_omp_clause (locus, OMP_CLAUSE_FIRSTPRIVATE);
7877 OMP_CLAUSE_DECL (lvc) = last;
7878 OMP_CLAUSE_CHAIN (lvc) = clauses;
7879 clauses = lvc;
7880 diffvar = create_temporary_var (TREE_TYPE (diff));
7881 pushdecl (diffvar);
7882 add_decl_expr (diffvar);
7885 orig_pre_body = *pre_body;
7886 *pre_body = push_stmt_list ();
7887 if (orig_pre_body)
7888 add_stmt (orig_pre_body);
7889 if (init != NULL)
7890 finish_expr_stmt (build_x_modify_expr (elocus,
7891 iter, NOP_EXPR, init,
7892 tf_warning_or_error));
7893 init = build_int_cst (TREE_TYPE (diff), 0);
7894 if (c && iter_incr == NULL
7895 && (!ordered || (i < collapse && collapse > 1)))
7897 if (incr_var)
7899 finish_expr_stmt (build_x_modify_expr (elocus,
7900 incr_var, NOP_EXPR,
7901 incr, tf_warning_or_error));
7902 incr = incr_var;
7904 iter_incr = build_x_modify_expr (elocus,
7905 iter, PLUS_EXPR, incr,
7906 tf_warning_or_error);
7908 if (c && ordered && i < collapse && collapse > 1)
7909 iter_incr = incr;
7910 finish_expr_stmt (build_x_modify_expr (elocus,
7911 last, NOP_EXPR, init,
7912 tf_warning_or_error));
7913 if (diffvar)
7915 finish_expr_stmt (build_x_modify_expr (elocus,
7916 diffvar, NOP_EXPR,
7917 diff, tf_warning_or_error));
7918 diff = diffvar;
7920 *pre_body = pop_stmt_list (*pre_body);
7922 cond = cp_build_binary_op (elocus,
7923 TREE_CODE (cond), decl, diff,
7924 tf_warning_or_error);
7925 incr = build_modify_expr (elocus, decl, NULL_TREE, PLUS_EXPR,
7926 elocus, incr, NULL_TREE);
7928 orig_body = *body;
7929 *body = push_stmt_list ();
7930 iter_init = build2 (MINUS_EXPR, TREE_TYPE (diff), decl, last);
7931 iter_init = build_x_modify_expr (elocus,
7932 iter, PLUS_EXPR, iter_init,
7933 tf_warning_or_error);
7934 if (iter_init != error_mark_node)
7935 iter_init = build1 (NOP_EXPR, void_type_node, iter_init);
7936 finish_expr_stmt (iter_init);
7937 finish_expr_stmt (build_x_modify_expr (elocus,
7938 last, NOP_EXPR, decl,
7939 tf_warning_or_error));
7940 add_stmt (orig_body);
7941 *body = pop_stmt_list (*body);
7943 if (c)
7945 OMP_CLAUSE_LASTPRIVATE_STMT (c) = push_stmt_list ();
7946 if (!ordered)
7947 finish_expr_stmt (iter_incr);
7948 else
7950 iter_init = decl;
7951 if (i < collapse && collapse > 1 && !error_operand_p (iter_incr))
7952 iter_init = build2 (PLUS_EXPR, TREE_TYPE (diff),
7953 iter_init, iter_incr);
7954 iter_init = build2 (MINUS_EXPR, TREE_TYPE (diff), iter_init, last);
7955 iter_init = build_x_modify_expr (elocus,
7956 iter, PLUS_EXPR, iter_init,
7957 tf_warning_or_error);
7958 if (iter_init != error_mark_node)
7959 iter_init = build1 (NOP_EXPR, void_type_node, iter_init);
7960 finish_expr_stmt (iter_init);
7962 OMP_CLAUSE_LASTPRIVATE_STMT (c)
7963 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (c));
7966 TREE_VEC_ELT (declv, i) = decl;
7967 TREE_VEC_ELT (initv, i) = init;
7968 TREE_VEC_ELT (condv, i) = cond;
7969 TREE_VEC_ELT (incrv, i) = incr;
7970 *lastp = last;
7972 return false;
7975 /* Build and validate an OMP_FOR statement. CLAUSES, BODY, COND, INCR
7976 are directly for their associated operands in the statement. DECL
7977 and INIT are a combo; if DECL is NULL then INIT ought to be a
7978 MODIFY_EXPR, and the DECL should be extracted. PRE_BODY are
7979 optional statements that need to go before the loop into its
7980 sk_omp scope. */
7982 tree
7983 finish_omp_for (location_t locus, enum tree_code code, tree declv,
7984 tree orig_declv, tree initv, tree condv, tree incrv,
7985 tree body, tree pre_body, vec<tree> *orig_inits, tree clauses)
7987 tree omp_for = NULL, orig_incr = NULL;
7988 tree decl = NULL, init, cond, incr, orig_decl = NULL_TREE, block = NULL_TREE;
7989 tree last = NULL_TREE;
7990 location_t elocus;
7991 int i;
7992 int collapse = 1;
7993 int ordered = 0;
7995 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (initv));
7996 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (condv));
7997 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (incrv));
7998 if (TREE_VEC_LENGTH (declv) > 1)
8000 tree c;
8002 c = omp_find_clause (clauses, OMP_CLAUSE_TILE);
8003 if (c)
8004 collapse = list_length (OMP_CLAUSE_TILE_LIST (c));
8005 else
8007 c = omp_find_clause (clauses, OMP_CLAUSE_COLLAPSE);
8008 if (c)
8009 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (c));
8010 if (collapse != TREE_VEC_LENGTH (declv))
8011 ordered = TREE_VEC_LENGTH (declv);
8014 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
8016 decl = TREE_VEC_ELT (declv, i);
8017 init = TREE_VEC_ELT (initv, i);
8018 cond = TREE_VEC_ELT (condv, i);
8019 incr = TREE_VEC_ELT (incrv, i);
8020 elocus = locus;
8022 if (decl == NULL)
8024 if (init != NULL)
8025 switch (TREE_CODE (init))
8027 case MODIFY_EXPR:
8028 decl = TREE_OPERAND (init, 0);
8029 init = TREE_OPERAND (init, 1);
8030 break;
8031 case MODOP_EXPR:
8032 if (TREE_CODE (TREE_OPERAND (init, 1)) == NOP_EXPR)
8034 decl = TREE_OPERAND (init, 0);
8035 init = TREE_OPERAND (init, 2);
8037 break;
8038 default:
8039 break;
8042 if (decl == NULL)
8044 error_at (locus,
8045 "expected iteration declaration or initialization");
8046 return NULL;
8050 if (init && EXPR_HAS_LOCATION (init))
8051 elocus = EXPR_LOCATION (init);
8053 if (cond == NULL)
8055 error_at (elocus, "missing controlling predicate");
8056 return NULL;
8059 if (incr == NULL)
8061 error_at (elocus, "missing increment expression");
8062 return NULL;
8065 TREE_VEC_ELT (declv, i) = decl;
8066 TREE_VEC_ELT (initv, i) = init;
8069 if (orig_inits)
8071 bool fail = false;
8072 tree orig_init;
8073 FOR_EACH_VEC_ELT (*orig_inits, i, orig_init)
8074 if (orig_init
8075 && !c_omp_check_loop_iv_exprs (locus, declv,
8076 TREE_VEC_ELT (declv, i), orig_init,
8077 NULL_TREE, cp_walk_subtrees))
8078 fail = true;
8079 if (fail)
8080 return NULL;
8083 if (dependent_omp_for_p (declv, initv, condv, incrv))
8085 tree stmt;
8087 stmt = make_node (code);
8089 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
8091 /* This is really just a place-holder. We'll be decomposing this
8092 again and going through the cp_build_modify_expr path below when
8093 we instantiate the thing. */
8094 TREE_VEC_ELT (initv, i)
8095 = build2 (MODIFY_EXPR, void_type_node, TREE_VEC_ELT (declv, i),
8096 TREE_VEC_ELT (initv, i));
8099 TREE_TYPE (stmt) = void_type_node;
8100 OMP_FOR_INIT (stmt) = initv;
8101 OMP_FOR_COND (stmt) = condv;
8102 OMP_FOR_INCR (stmt) = incrv;
8103 OMP_FOR_BODY (stmt) = body;
8104 OMP_FOR_PRE_BODY (stmt) = pre_body;
8105 OMP_FOR_CLAUSES (stmt) = clauses;
8107 SET_EXPR_LOCATION (stmt, locus);
8108 return add_stmt (stmt);
8111 if (!orig_declv)
8112 orig_declv = copy_node (declv);
8114 if (processing_template_decl)
8115 orig_incr = make_tree_vec (TREE_VEC_LENGTH (incrv));
8117 for (i = 0; i < TREE_VEC_LENGTH (declv); )
8119 decl = TREE_VEC_ELT (declv, i);
8120 init = TREE_VEC_ELT (initv, i);
8121 cond = TREE_VEC_ELT (condv, i);
8122 incr = TREE_VEC_ELT (incrv, i);
8123 if (orig_incr)
8124 TREE_VEC_ELT (orig_incr, i) = incr;
8125 elocus = locus;
8127 if (init && EXPR_HAS_LOCATION (init))
8128 elocus = EXPR_LOCATION (init);
8130 if (!DECL_P (decl))
8132 error_at (elocus, "expected iteration declaration or initialization");
8133 return NULL;
8136 if (incr && TREE_CODE (incr) == MODOP_EXPR)
8138 if (orig_incr)
8139 TREE_VEC_ELT (orig_incr, i) = incr;
8140 incr = cp_build_modify_expr (elocus, TREE_OPERAND (incr, 0),
8141 TREE_CODE (TREE_OPERAND (incr, 1)),
8142 TREE_OPERAND (incr, 2),
8143 tf_warning_or_error);
8146 if (CLASS_TYPE_P (TREE_TYPE (decl)))
8148 if (code == OMP_SIMD)
8150 error_at (elocus, "%<#pragma omp simd%> used with class "
8151 "iteration variable %qE", decl);
8152 return NULL;
8154 if (code == CILK_FOR && i == 0)
8155 orig_decl = decl;
8156 if (handle_omp_for_class_iterator (i, locus, code, declv, orig_declv,
8157 initv, condv, incrv, &body,
8158 &pre_body, clauses, &last,
8159 collapse, ordered))
8160 return NULL;
8161 continue;
8164 if (!INTEGRAL_TYPE_P (TREE_TYPE (decl))
8165 && !TYPE_PTR_P (TREE_TYPE (decl)))
8167 error_at (elocus, "invalid type for iteration variable %qE", decl);
8168 return NULL;
8171 if (!processing_template_decl)
8173 init = fold_build_cleanup_point_expr (TREE_TYPE (init), init);
8174 init = cp_build_modify_expr (elocus, decl, NOP_EXPR, init,
8175 tf_warning_or_error);
8177 else
8178 init = build2 (MODIFY_EXPR, void_type_node, decl, init);
8179 if (cond
8180 && TREE_SIDE_EFFECTS (cond)
8181 && COMPARISON_CLASS_P (cond)
8182 && !processing_template_decl)
8184 tree t = TREE_OPERAND (cond, 0);
8185 if (TREE_SIDE_EFFECTS (t)
8186 && t != decl
8187 && (TREE_CODE (t) != NOP_EXPR
8188 || TREE_OPERAND (t, 0) != decl))
8189 TREE_OPERAND (cond, 0)
8190 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
8192 t = TREE_OPERAND (cond, 1);
8193 if (TREE_SIDE_EFFECTS (t)
8194 && t != decl
8195 && (TREE_CODE (t) != NOP_EXPR
8196 || TREE_OPERAND (t, 0) != decl))
8197 TREE_OPERAND (cond, 1)
8198 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
8200 if (decl == error_mark_node || init == error_mark_node)
8201 return NULL;
8203 TREE_VEC_ELT (declv, i) = decl;
8204 TREE_VEC_ELT (initv, i) = init;
8205 TREE_VEC_ELT (condv, i) = cond;
8206 TREE_VEC_ELT (incrv, i) = incr;
8207 i++;
8210 if (IS_EMPTY_STMT (pre_body))
8211 pre_body = NULL;
8213 if (code == CILK_FOR && !processing_template_decl)
8214 block = push_stmt_list ();
8216 omp_for = c_finish_omp_for (locus, code, declv, orig_declv, initv, condv,
8217 incrv, body, pre_body);
8219 /* Check for iterators appearing in lb, b or incr expressions. */
8220 if (omp_for && !c_omp_check_loop_iv (omp_for, orig_declv, cp_walk_subtrees))
8221 omp_for = NULL_TREE;
8223 if (omp_for == NULL)
8225 if (block)
8226 pop_stmt_list (block);
8227 return NULL;
8230 add_stmt (omp_for);
8232 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INCR (omp_for)); i++)
8234 decl = TREE_OPERAND (TREE_VEC_ELT (OMP_FOR_INIT (omp_for), i), 0);
8235 incr = TREE_VEC_ELT (OMP_FOR_INCR (omp_for), i);
8237 if (TREE_CODE (incr) != MODIFY_EXPR)
8238 continue;
8240 if (TREE_SIDE_EFFECTS (TREE_OPERAND (incr, 1))
8241 && BINARY_CLASS_P (TREE_OPERAND (incr, 1))
8242 && !processing_template_decl)
8244 tree t = TREE_OPERAND (TREE_OPERAND (incr, 1), 0);
8245 if (TREE_SIDE_EFFECTS (t)
8246 && t != decl
8247 && (TREE_CODE (t) != NOP_EXPR
8248 || TREE_OPERAND (t, 0) != decl))
8249 TREE_OPERAND (TREE_OPERAND (incr, 1), 0)
8250 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
8252 t = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
8253 if (TREE_SIDE_EFFECTS (t)
8254 && t != decl
8255 && (TREE_CODE (t) != NOP_EXPR
8256 || TREE_OPERAND (t, 0) != decl))
8257 TREE_OPERAND (TREE_OPERAND (incr, 1), 1)
8258 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
8261 if (orig_incr)
8262 TREE_VEC_ELT (OMP_FOR_INCR (omp_for), i) = TREE_VEC_ELT (orig_incr, i);
8264 OMP_FOR_CLAUSES (omp_for) = clauses;
8266 /* For simd loops with non-static data member iterators, we could have added
8267 OMP_CLAUSE_LINEAR clauses without OMP_CLAUSE_LINEAR_STEP. As we know the
8268 step at this point, fill it in. */
8269 if (code == OMP_SIMD && !processing_template_decl
8270 && TREE_VEC_LENGTH (OMP_FOR_INCR (omp_for)) == 1)
8271 for (tree c = omp_find_clause (clauses, OMP_CLAUSE_LINEAR); c;
8272 c = omp_find_clause (OMP_CLAUSE_CHAIN (c), OMP_CLAUSE_LINEAR))
8273 if (OMP_CLAUSE_LINEAR_STEP (c) == NULL_TREE)
8275 decl = TREE_OPERAND (TREE_VEC_ELT (OMP_FOR_INIT (omp_for), 0), 0);
8276 gcc_assert (decl == OMP_CLAUSE_DECL (c));
8277 incr = TREE_VEC_ELT (OMP_FOR_INCR (omp_for), 0);
8278 tree step, stept;
8279 switch (TREE_CODE (incr))
8281 case PREINCREMENT_EXPR:
8282 case POSTINCREMENT_EXPR:
8283 /* c_omp_for_incr_canonicalize_ptr() should have been
8284 called to massage things appropriately. */
8285 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (decl)));
8286 OMP_CLAUSE_LINEAR_STEP (c) = build_int_cst (TREE_TYPE (decl), 1);
8287 break;
8288 case PREDECREMENT_EXPR:
8289 case POSTDECREMENT_EXPR:
8290 /* c_omp_for_incr_canonicalize_ptr() should have been
8291 called to massage things appropriately. */
8292 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (decl)));
8293 OMP_CLAUSE_LINEAR_STEP (c)
8294 = build_int_cst (TREE_TYPE (decl), -1);
8295 break;
8296 case MODIFY_EXPR:
8297 gcc_assert (TREE_OPERAND (incr, 0) == decl);
8298 incr = TREE_OPERAND (incr, 1);
8299 switch (TREE_CODE (incr))
8301 case PLUS_EXPR:
8302 if (TREE_OPERAND (incr, 1) == decl)
8303 step = TREE_OPERAND (incr, 0);
8304 else
8305 step = TREE_OPERAND (incr, 1);
8306 break;
8307 case MINUS_EXPR:
8308 case POINTER_PLUS_EXPR:
8309 gcc_assert (TREE_OPERAND (incr, 0) == decl);
8310 step = TREE_OPERAND (incr, 1);
8311 break;
8312 default:
8313 gcc_unreachable ();
8315 stept = TREE_TYPE (decl);
8316 if (POINTER_TYPE_P (stept))
8317 stept = sizetype;
8318 step = fold_convert (stept, step);
8319 if (TREE_CODE (incr) == MINUS_EXPR)
8320 step = fold_build1 (NEGATE_EXPR, stept, step);
8321 OMP_CLAUSE_LINEAR_STEP (c) = step;
8322 break;
8323 default:
8324 gcc_unreachable ();
8328 if (block)
8330 tree omp_par = make_node (OMP_PARALLEL);
8331 TREE_TYPE (omp_par) = void_type_node;
8332 OMP_PARALLEL_CLAUSES (omp_par) = NULL_TREE;
8333 tree bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
8334 TREE_SIDE_EFFECTS (bind) = 1;
8335 BIND_EXPR_BODY (bind) = pop_stmt_list (block);
8336 OMP_PARALLEL_BODY (omp_par) = bind;
8337 if (OMP_FOR_PRE_BODY (omp_for))
8339 add_stmt (OMP_FOR_PRE_BODY (omp_for));
8340 OMP_FOR_PRE_BODY (omp_for) = NULL_TREE;
8342 init = TREE_VEC_ELT (OMP_FOR_INIT (omp_for), 0);
8343 decl = TREE_OPERAND (init, 0);
8344 cond = TREE_VEC_ELT (OMP_FOR_COND (omp_for), 0);
8345 incr = TREE_VEC_ELT (OMP_FOR_INCR (omp_for), 0);
8346 tree t = TREE_OPERAND (cond, 1), c, clauses, *pc;
8347 clauses = OMP_FOR_CLAUSES (omp_for);
8348 OMP_FOR_CLAUSES (omp_for) = NULL_TREE;
8349 for (pc = &clauses; *pc; )
8350 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_SCHEDULE)
8352 gcc_assert (OMP_FOR_CLAUSES (omp_for) == NULL_TREE);
8353 OMP_FOR_CLAUSES (omp_for) = *pc;
8354 *pc = OMP_CLAUSE_CHAIN (*pc);
8355 OMP_CLAUSE_CHAIN (OMP_FOR_CLAUSES (omp_for)) = NULL_TREE;
8357 else
8359 gcc_assert (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_FIRSTPRIVATE);
8360 pc = &OMP_CLAUSE_CHAIN (*pc);
8362 if (TREE_CODE (t) != INTEGER_CST)
8364 TREE_OPERAND (cond, 1) = get_temp_regvar (TREE_TYPE (t), t);
8365 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
8366 OMP_CLAUSE_DECL (c) = TREE_OPERAND (cond, 1);
8367 OMP_CLAUSE_CHAIN (c) = clauses;
8368 clauses = c;
8370 if (TREE_CODE (incr) == MODIFY_EXPR)
8372 t = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
8373 if (TREE_CODE (t) != INTEGER_CST)
8375 TREE_OPERAND (TREE_OPERAND (incr, 1), 1)
8376 = get_temp_regvar (TREE_TYPE (t), t);
8377 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
8378 OMP_CLAUSE_DECL (c) = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
8379 OMP_CLAUSE_CHAIN (c) = clauses;
8380 clauses = c;
8383 t = TREE_OPERAND (init, 1);
8384 if (TREE_CODE (t) != INTEGER_CST)
8386 TREE_OPERAND (init, 1) = get_temp_regvar (TREE_TYPE (t), t);
8387 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
8388 OMP_CLAUSE_DECL (c) = TREE_OPERAND (init, 1);
8389 OMP_CLAUSE_CHAIN (c) = clauses;
8390 clauses = c;
8392 if (orig_decl && orig_decl != decl)
8394 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
8395 OMP_CLAUSE_DECL (c) = orig_decl;
8396 OMP_CLAUSE_CHAIN (c) = clauses;
8397 clauses = c;
8399 if (last)
8401 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
8402 OMP_CLAUSE_DECL (c) = last;
8403 OMP_CLAUSE_CHAIN (c) = clauses;
8404 clauses = c;
8406 c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
8407 OMP_CLAUSE_DECL (c) = decl;
8408 OMP_CLAUSE_CHAIN (c) = clauses;
8409 clauses = c;
8410 c = build_omp_clause (input_location, OMP_CLAUSE__CILK_FOR_COUNT_);
8411 OMP_CLAUSE_OPERAND (c, 0)
8412 = cilk_for_number_of_iterations (omp_for);
8413 OMP_CLAUSE_CHAIN (c) = clauses;
8414 OMP_PARALLEL_CLAUSES (omp_par) = finish_omp_clauses (c, C_ORT_CILK);
8415 add_stmt (omp_par);
8416 return omp_par;
8418 else if (code == CILK_FOR && processing_template_decl)
8420 tree c, clauses = OMP_FOR_CLAUSES (omp_for);
8421 if (orig_decl && orig_decl != decl)
8423 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
8424 OMP_CLAUSE_DECL (c) = orig_decl;
8425 OMP_CLAUSE_CHAIN (c) = clauses;
8426 clauses = c;
8428 if (last)
8430 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
8431 OMP_CLAUSE_DECL (c) = last;
8432 OMP_CLAUSE_CHAIN (c) = clauses;
8433 clauses = c;
8435 OMP_FOR_CLAUSES (omp_for) = clauses;
8437 return omp_for;
8440 void
8441 finish_omp_atomic (enum tree_code code, enum tree_code opcode, tree lhs,
8442 tree rhs, tree v, tree lhs1, tree rhs1, bool seq_cst)
8444 tree orig_lhs;
8445 tree orig_rhs;
8446 tree orig_v;
8447 tree orig_lhs1;
8448 tree orig_rhs1;
8449 bool dependent_p;
8450 tree stmt;
8452 orig_lhs = lhs;
8453 orig_rhs = rhs;
8454 orig_v = v;
8455 orig_lhs1 = lhs1;
8456 orig_rhs1 = rhs1;
8457 dependent_p = false;
8458 stmt = NULL_TREE;
8460 /* Even in a template, we can detect invalid uses of the atomic
8461 pragma if neither LHS nor RHS is type-dependent. */
8462 if (processing_template_decl)
8464 dependent_p = (type_dependent_expression_p (lhs)
8465 || (rhs && type_dependent_expression_p (rhs))
8466 || (v && type_dependent_expression_p (v))
8467 || (lhs1 && type_dependent_expression_p (lhs1))
8468 || (rhs1 && type_dependent_expression_p (rhs1)));
8469 if (!dependent_p)
8471 lhs = build_non_dependent_expr (lhs);
8472 if (rhs)
8473 rhs = build_non_dependent_expr (rhs);
8474 if (v)
8475 v = build_non_dependent_expr (v);
8476 if (lhs1)
8477 lhs1 = build_non_dependent_expr (lhs1);
8478 if (rhs1)
8479 rhs1 = build_non_dependent_expr (rhs1);
8482 if (!dependent_p)
8484 bool swapped = false;
8485 if (rhs1 && cp_tree_equal (lhs, rhs))
8487 std::swap (rhs, rhs1);
8488 swapped = !commutative_tree_code (opcode);
8490 if (rhs1 && !cp_tree_equal (lhs, rhs1))
8492 if (code == OMP_ATOMIC)
8493 error ("%<#pragma omp atomic update%> uses two different "
8494 "expressions for memory");
8495 else
8496 error ("%<#pragma omp atomic capture%> uses two different "
8497 "expressions for memory");
8498 return;
8500 if (lhs1 && !cp_tree_equal (lhs, lhs1))
8502 if (code == OMP_ATOMIC)
8503 error ("%<#pragma omp atomic update%> uses two different "
8504 "expressions for memory");
8505 else
8506 error ("%<#pragma omp atomic capture%> uses two different "
8507 "expressions for memory");
8508 return;
8510 stmt = c_finish_omp_atomic (input_location, code, opcode, lhs, rhs,
8511 v, lhs1, rhs1, swapped, seq_cst,
8512 processing_template_decl != 0);
8513 if (stmt == error_mark_node)
8514 return;
8516 if (processing_template_decl)
8518 if (code == OMP_ATOMIC_READ)
8520 stmt = build_min_nt_loc (EXPR_LOCATION (orig_lhs),
8521 OMP_ATOMIC_READ, orig_lhs);
8522 OMP_ATOMIC_SEQ_CST (stmt) = seq_cst;
8523 stmt = build2 (MODIFY_EXPR, void_type_node, orig_v, stmt);
8525 else
8527 if (opcode == NOP_EXPR)
8528 stmt = build2 (MODIFY_EXPR, void_type_node, orig_lhs, orig_rhs);
8529 else
8530 stmt = build2 (opcode, void_type_node, orig_lhs, orig_rhs);
8531 if (orig_rhs1)
8532 stmt = build_min_nt_loc (EXPR_LOCATION (orig_rhs1),
8533 COMPOUND_EXPR, orig_rhs1, stmt);
8534 if (code != OMP_ATOMIC)
8536 stmt = build_min_nt_loc (EXPR_LOCATION (orig_lhs1),
8537 code, orig_lhs1, stmt);
8538 OMP_ATOMIC_SEQ_CST (stmt) = seq_cst;
8539 stmt = build2 (MODIFY_EXPR, void_type_node, orig_v, stmt);
8542 stmt = build2 (OMP_ATOMIC, void_type_node, integer_zero_node, stmt);
8543 OMP_ATOMIC_SEQ_CST (stmt) = seq_cst;
8545 finish_expr_stmt (stmt);
8548 void
8549 finish_omp_barrier (void)
8551 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_BARRIER);
8552 vec<tree, va_gc> *vec = make_tree_vector ();
8553 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
8554 release_tree_vector (vec);
8555 finish_expr_stmt (stmt);
8558 void
8559 finish_omp_flush (void)
8561 tree fn = builtin_decl_explicit (BUILT_IN_SYNC_SYNCHRONIZE);
8562 vec<tree, va_gc> *vec = make_tree_vector ();
8563 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
8564 release_tree_vector (vec);
8565 finish_expr_stmt (stmt);
8568 void
8569 finish_omp_taskwait (void)
8571 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_TASKWAIT);
8572 vec<tree, va_gc> *vec = make_tree_vector ();
8573 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
8574 release_tree_vector (vec);
8575 finish_expr_stmt (stmt);
8578 void
8579 finish_omp_taskyield (void)
8581 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_TASKYIELD);
8582 vec<tree, va_gc> *vec = make_tree_vector ();
8583 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
8584 release_tree_vector (vec);
8585 finish_expr_stmt (stmt);
8588 void
8589 finish_omp_cancel (tree clauses)
8591 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
8592 int mask = 0;
8593 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
8594 mask = 1;
8595 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
8596 mask = 2;
8597 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
8598 mask = 4;
8599 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
8600 mask = 8;
8601 else
8603 error ("%<#pragma omp cancel%> must specify one of "
8604 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> clauses");
8605 return;
8607 vec<tree, va_gc> *vec = make_tree_vector ();
8608 tree ifc = omp_find_clause (clauses, OMP_CLAUSE_IF);
8609 if (ifc != NULL_TREE)
8611 tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
8612 ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
8613 boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
8614 build_zero_cst (type));
8616 else
8617 ifc = boolean_true_node;
8618 vec->quick_push (build_int_cst (integer_type_node, mask));
8619 vec->quick_push (ifc);
8620 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
8621 release_tree_vector (vec);
8622 finish_expr_stmt (stmt);
8625 void
8626 finish_omp_cancellation_point (tree clauses)
8628 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
8629 int mask = 0;
8630 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
8631 mask = 1;
8632 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
8633 mask = 2;
8634 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
8635 mask = 4;
8636 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
8637 mask = 8;
8638 else
8640 error ("%<#pragma omp cancellation point%> must specify one of "
8641 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> clauses");
8642 return;
8644 vec<tree, va_gc> *vec
8645 = make_tree_vector_single (build_int_cst (integer_type_node, mask));
8646 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
8647 release_tree_vector (vec);
8648 finish_expr_stmt (stmt);
8651 /* Begin a __transaction_atomic or __transaction_relaxed statement.
8652 If PCOMPOUND is non-null, this is for a function-transaction-block, and we
8653 should create an extra compound stmt. */
8655 tree
8656 begin_transaction_stmt (location_t loc, tree *pcompound, int flags)
8658 tree r;
8660 if (pcompound)
8661 *pcompound = begin_compound_stmt (0);
8663 r = build_stmt (loc, TRANSACTION_EXPR, NULL_TREE);
8665 /* Only add the statement to the function if support enabled. */
8666 if (flag_tm)
8667 add_stmt (r);
8668 else
8669 error_at (loc, ((flags & TM_STMT_ATTR_RELAXED) != 0
8670 ? G_("%<__transaction_relaxed%> without "
8671 "transactional memory support enabled")
8672 : G_("%<__transaction_atomic%> without "
8673 "transactional memory support enabled")));
8675 TRANSACTION_EXPR_BODY (r) = push_stmt_list ();
8676 TREE_SIDE_EFFECTS (r) = 1;
8677 return r;
8680 /* End a __transaction_atomic or __transaction_relaxed statement.
8681 If COMPOUND_STMT is non-null, this is for a function-transaction-block,
8682 and we should end the compound. If NOEX is non-NULL, we wrap the body in
8683 a MUST_NOT_THROW_EXPR with NOEX as condition. */
8685 void
8686 finish_transaction_stmt (tree stmt, tree compound_stmt, int flags, tree noex)
8688 TRANSACTION_EXPR_BODY (stmt) = pop_stmt_list (TRANSACTION_EXPR_BODY (stmt));
8689 TRANSACTION_EXPR_OUTER (stmt) = (flags & TM_STMT_ATTR_OUTER) != 0;
8690 TRANSACTION_EXPR_RELAXED (stmt) = (flags & TM_STMT_ATTR_RELAXED) != 0;
8691 TRANSACTION_EXPR_IS_STMT (stmt) = 1;
8693 /* noexcept specifications are not allowed for function transactions. */
8694 gcc_assert (!(noex && compound_stmt));
8695 if (noex)
8697 tree body = build_must_not_throw_expr (TRANSACTION_EXPR_BODY (stmt),
8698 noex);
8699 protected_set_expr_location
8700 (body, EXPR_LOCATION (TRANSACTION_EXPR_BODY (stmt)));
8701 TREE_SIDE_EFFECTS (body) = 1;
8702 TRANSACTION_EXPR_BODY (stmt) = body;
8705 if (compound_stmt)
8706 finish_compound_stmt (compound_stmt);
8709 /* Build a __transaction_atomic or __transaction_relaxed expression. If
8710 NOEX is non-NULL, we wrap the body in a MUST_NOT_THROW_EXPR with NOEX as
8711 condition. */
8713 tree
8714 build_transaction_expr (location_t loc, tree expr, int flags, tree noex)
8716 tree ret;
8717 if (noex)
8719 expr = build_must_not_throw_expr (expr, noex);
8720 protected_set_expr_location (expr, loc);
8721 TREE_SIDE_EFFECTS (expr) = 1;
8723 ret = build1 (TRANSACTION_EXPR, TREE_TYPE (expr), expr);
8724 if (flags & TM_STMT_ATTR_RELAXED)
8725 TRANSACTION_EXPR_RELAXED (ret) = 1;
8726 TREE_SIDE_EFFECTS (ret) = 1;
8727 SET_EXPR_LOCATION (ret, loc);
8728 return ret;
8731 void
8732 init_cp_semantics (void)
8736 /* Build a STATIC_ASSERT for a static assertion with the condition
8737 CONDITION and the message text MESSAGE. LOCATION is the location
8738 of the static assertion in the source code. When MEMBER_P, this
8739 static assertion is a member of a class. */
8740 void
8741 finish_static_assert (tree condition, tree message, location_t location,
8742 bool member_p)
8744 if (message == NULL_TREE
8745 || message == error_mark_node
8746 || condition == NULL_TREE
8747 || condition == error_mark_node)
8748 return;
8750 if (check_for_bare_parameter_packs (condition))
8751 condition = error_mark_node;
8753 if (type_dependent_expression_p (condition)
8754 || value_dependent_expression_p (condition))
8756 /* We're in a template; build a STATIC_ASSERT and put it in
8757 the right place. */
8758 tree assertion;
8760 assertion = make_node (STATIC_ASSERT);
8761 STATIC_ASSERT_CONDITION (assertion) = condition;
8762 STATIC_ASSERT_MESSAGE (assertion) = message;
8763 STATIC_ASSERT_SOURCE_LOCATION (assertion) = location;
8765 if (member_p)
8766 maybe_add_class_template_decl_list (current_class_type,
8767 assertion,
8768 /*friend_p=*/0);
8769 else
8770 add_stmt (assertion);
8772 return;
8775 /* Fold the expression and convert it to a boolean value. */
8776 condition = instantiate_non_dependent_expr (condition);
8777 condition = cp_convert (boolean_type_node, condition, tf_warning_or_error);
8778 condition = maybe_constant_value (condition);
8780 if (TREE_CODE (condition) == INTEGER_CST && !integer_zerop (condition))
8781 /* Do nothing; the condition is satisfied. */
8783 else
8785 location_t saved_loc = input_location;
8787 input_location = location;
8788 if (TREE_CODE (condition) == INTEGER_CST
8789 && integer_zerop (condition))
8791 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
8792 (TREE_TYPE (TREE_TYPE (message))));
8793 int len = TREE_STRING_LENGTH (message) / sz - 1;
8794 /* Report the error. */
8795 if (len == 0)
8796 error ("static assertion failed");
8797 else
8798 error ("static assertion failed: %s",
8799 TREE_STRING_POINTER (message));
8801 else if (condition && condition != error_mark_node)
8803 error ("non-constant condition for static assertion");
8804 if (require_potential_rvalue_constant_expression (condition))
8805 cxx_constant_value (condition);
8807 input_location = saved_loc;
8811 /* Implements the C++0x decltype keyword. Returns the type of EXPR,
8812 suitable for use as a type-specifier.
8814 ID_EXPRESSION_OR_MEMBER_ACCESS_P is true when EXPR was parsed as an
8815 id-expression or a class member access, FALSE when it was parsed as
8816 a full expression. */
8818 tree
8819 finish_decltype_type (tree expr, bool id_expression_or_member_access_p,
8820 tsubst_flags_t complain)
8822 tree type = NULL_TREE;
8824 if (!expr || error_operand_p (expr))
8825 return error_mark_node;
8827 if (TYPE_P (expr)
8828 || TREE_CODE (expr) == TYPE_DECL
8829 || (TREE_CODE (expr) == BIT_NOT_EXPR
8830 && TYPE_P (TREE_OPERAND (expr, 0))))
8832 if (complain & tf_error)
8833 error ("argument to decltype must be an expression");
8834 return error_mark_node;
8837 /* Depending on the resolution of DR 1172, we may later need to distinguish
8838 instantiation-dependent but not type-dependent expressions so that, say,
8839 A<decltype(sizeof(T))>::U doesn't require 'typename'. */
8840 if (instantiation_dependent_uneval_expression_p (expr))
8842 type = cxx_make_type (DECLTYPE_TYPE);
8843 DECLTYPE_TYPE_EXPR (type) = expr;
8844 DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (type)
8845 = id_expression_or_member_access_p;
8846 SET_TYPE_STRUCTURAL_EQUALITY (type);
8848 return type;
8851 /* The type denoted by decltype(e) is defined as follows: */
8853 expr = resolve_nondeduced_context (expr, complain);
8855 if (invalid_nonstatic_memfn_p (input_location, expr, complain))
8856 return error_mark_node;
8858 if (type_unknown_p (expr))
8860 if (complain & tf_error)
8861 error ("decltype cannot resolve address of overloaded function");
8862 return error_mark_node;
8865 /* To get the size of a static data member declared as an array of
8866 unknown bound, we need to instantiate it. */
8867 if (VAR_P (expr)
8868 && VAR_HAD_UNKNOWN_BOUND (expr)
8869 && DECL_TEMPLATE_INSTANTIATION (expr))
8870 instantiate_decl (expr, /*defer_ok*/true, /*expl_inst_mem*/false);
8872 if (id_expression_or_member_access_p)
8874 /* If e is an id-expression or a class member access (5.2.5
8875 [expr.ref]), decltype(e) is defined as the type of the entity
8876 named by e. If there is no such entity, or e names a set of
8877 overloaded functions, the program is ill-formed. */
8878 if (identifier_p (expr))
8879 expr = lookup_name (expr);
8881 if (INDIRECT_REF_P (expr))
8882 /* This can happen when the expression is, e.g., "a.b". Just
8883 look at the underlying operand. */
8884 expr = TREE_OPERAND (expr, 0);
8886 if (TREE_CODE (expr) == OFFSET_REF
8887 || TREE_CODE (expr) == MEMBER_REF
8888 || TREE_CODE (expr) == SCOPE_REF)
8889 /* We're only interested in the field itself. If it is a
8890 BASELINK, we will need to see through it in the next
8891 step. */
8892 expr = TREE_OPERAND (expr, 1);
8894 if (BASELINK_P (expr))
8895 /* See through BASELINK nodes to the underlying function. */
8896 expr = BASELINK_FUNCTIONS (expr);
8898 /* decltype of a decomposition name drops references in the tuple case
8899 (unlike decltype of a normal variable) and keeps cv-qualifiers from
8900 the containing object in the other cases (unlike decltype of a member
8901 access expression). */
8902 if (DECL_DECOMPOSITION_P (expr))
8904 if (DECL_HAS_VALUE_EXPR_P (expr))
8905 /* Expr is an array or struct subobject proxy, handle
8906 bit-fields properly. */
8907 return unlowered_expr_type (expr);
8908 else
8909 /* Expr is a reference variable for the tuple case. */
8910 return lookup_decomp_type (expr);
8913 switch (TREE_CODE (expr))
8915 case FIELD_DECL:
8916 if (DECL_BIT_FIELD_TYPE (expr))
8918 type = DECL_BIT_FIELD_TYPE (expr);
8919 break;
8921 /* Fall through for fields that aren't bitfields. */
8922 gcc_fallthrough ();
8924 case FUNCTION_DECL:
8925 case VAR_DECL:
8926 case CONST_DECL:
8927 case PARM_DECL:
8928 case RESULT_DECL:
8929 case TEMPLATE_PARM_INDEX:
8930 expr = mark_type_use (expr);
8931 type = TREE_TYPE (expr);
8932 break;
8934 case ERROR_MARK:
8935 type = error_mark_node;
8936 break;
8938 case COMPONENT_REF:
8939 case COMPOUND_EXPR:
8940 mark_type_use (expr);
8941 type = is_bitfield_expr_with_lowered_type (expr);
8942 if (!type)
8943 type = TREE_TYPE (TREE_OPERAND (expr, 1));
8944 break;
8946 case BIT_FIELD_REF:
8947 gcc_unreachable ();
8949 case INTEGER_CST:
8950 case PTRMEM_CST:
8951 /* We can get here when the id-expression refers to an
8952 enumerator or non-type template parameter. */
8953 type = TREE_TYPE (expr);
8954 break;
8956 default:
8957 /* Handle instantiated template non-type arguments. */
8958 type = TREE_TYPE (expr);
8959 break;
8962 else
8964 /* Within a lambda-expression:
8966 Every occurrence of decltype((x)) where x is a possibly
8967 parenthesized id-expression that names an entity of
8968 automatic storage duration is treated as if x were
8969 transformed into an access to a corresponding data member
8970 of the closure type that would have been declared if x
8971 were a use of the denoted entity. */
8972 if (outer_automatic_var_p (expr)
8973 && current_function_decl
8974 && LAMBDA_FUNCTION_P (current_function_decl))
8975 type = capture_decltype (expr);
8976 else if (error_operand_p (expr))
8977 type = error_mark_node;
8978 else if (expr == current_class_ptr)
8979 /* If the expression is just "this", we want the
8980 cv-unqualified pointer for the "this" type. */
8981 type = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
8982 else
8984 /* Otherwise, where T is the type of e, if e is an lvalue,
8985 decltype(e) is defined as T&; if an xvalue, T&&; otherwise, T. */
8986 cp_lvalue_kind clk = lvalue_kind (expr);
8987 type = unlowered_expr_type (expr);
8988 gcc_assert (TREE_CODE (type) != REFERENCE_TYPE);
8990 /* For vector types, pick a non-opaque variant. */
8991 if (VECTOR_TYPE_P (type))
8992 type = strip_typedefs (type);
8994 if (clk != clk_none && !(clk & clk_class))
8995 type = cp_build_reference_type (type, (clk & clk_rvalueref));
8999 return type;
9002 /* Called from trait_expr_value to evaluate either __has_nothrow_assign or
9003 __has_nothrow_copy, depending on assign_p. Returns true iff all
9004 the copy {ctor,assign} fns are nothrow. */
9006 static bool
9007 classtype_has_nothrow_assign_or_copy_p (tree type, bool assign_p)
9009 tree fns = NULL_TREE;
9011 if (assign_p || TYPE_HAS_COPY_CTOR (type))
9012 fns = get_class_binding (type,
9013 assign_p ? cp_assignment_operator_id (NOP_EXPR)
9014 : ctor_identifier);
9016 bool saw_copy = false;
9017 for (ovl_iterator iter (fns); iter; ++iter)
9019 tree fn = *iter;
9021 if (copy_fn_p (fn) > 0)
9023 saw_copy = true;
9024 maybe_instantiate_noexcept (fn);
9025 if (!TYPE_NOTHROW_P (TREE_TYPE (fn)))
9026 return false;
9030 return saw_copy;
9033 /* Actually evaluates the trait. */
9035 static bool
9036 trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
9038 enum tree_code type_code1;
9039 tree t;
9041 type_code1 = TREE_CODE (type1);
9043 switch (kind)
9045 case CPTK_HAS_NOTHROW_ASSIGN:
9046 type1 = strip_array_types (type1);
9047 return (!CP_TYPE_CONST_P (type1) && type_code1 != REFERENCE_TYPE
9048 && (trait_expr_value (CPTK_HAS_TRIVIAL_ASSIGN, type1, type2)
9049 || (CLASS_TYPE_P (type1)
9050 && classtype_has_nothrow_assign_or_copy_p (type1,
9051 true))));
9053 case CPTK_HAS_TRIVIAL_ASSIGN:
9054 /* ??? The standard seems to be missing the "or array of such a class
9055 type" wording for this trait. */
9056 type1 = strip_array_types (type1);
9057 return (!CP_TYPE_CONST_P (type1) && type_code1 != REFERENCE_TYPE
9058 && (trivial_type_p (type1)
9059 || (CLASS_TYPE_P (type1)
9060 && TYPE_HAS_TRIVIAL_COPY_ASSIGN (type1))));
9062 case CPTK_HAS_NOTHROW_CONSTRUCTOR:
9063 type1 = strip_array_types (type1);
9064 return (trait_expr_value (CPTK_HAS_TRIVIAL_CONSTRUCTOR, type1, type2)
9065 || (CLASS_TYPE_P (type1)
9066 && (t = locate_ctor (type1))
9067 && (maybe_instantiate_noexcept (t),
9068 TYPE_NOTHROW_P (TREE_TYPE (t)))));
9070 case CPTK_HAS_TRIVIAL_CONSTRUCTOR:
9071 type1 = strip_array_types (type1);
9072 return (trivial_type_p (type1)
9073 || (CLASS_TYPE_P (type1) && TYPE_HAS_TRIVIAL_DFLT (type1)));
9075 case CPTK_HAS_NOTHROW_COPY:
9076 type1 = strip_array_types (type1);
9077 return (trait_expr_value (CPTK_HAS_TRIVIAL_COPY, type1, type2)
9078 || (CLASS_TYPE_P (type1)
9079 && classtype_has_nothrow_assign_or_copy_p (type1, false)));
9081 case CPTK_HAS_TRIVIAL_COPY:
9082 /* ??? The standard seems to be missing the "or array of such a class
9083 type" wording for this trait. */
9084 type1 = strip_array_types (type1);
9085 return (trivial_type_p (type1) || type_code1 == REFERENCE_TYPE
9086 || (CLASS_TYPE_P (type1) && TYPE_HAS_TRIVIAL_COPY_CTOR (type1)));
9088 case CPTK_HAS_TRIVIAL_DESTRUCTOR:
9089 type1 = strip_array_types (type1);
9090 return (trivial_type_p (type1) || type_code1 == REFERENCE_TYPE
9091 || (CLASS_TYPE_P (type1)
9092 && TYPE_HAS_TRIVIAL_DESTRUCTOR (type1)));
9094 case CPTK_HAS_VIRTUAL_DESTRUCTOR:
9095 return type_has_virtual_destructor (type1);
9097 case CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS:
9098 return type_has_unique_obj_representations (type1);
9100 case CPTK_IS_ABSTRACT:
9101 return ABSTRACT_CLASS_TYPE_P (type1);
9103 case CPTK_IS_AGGREGATE:
9104 return CP_AGGREGATE_TYPE_P (type1);
9106 case CPTK_IS_BASE_OF:
9107 return (NON_UNION_CLASS_TYPE_P (type1) && NON_UNION_CLASS_TYPE_P (type2)
9108 && (same_type_ignoring_top_level_qualifiers_p (type1, type2)
9109 || DERIVED_FROM_P (type1, type2)));
9111 case CPTK_IS_CLASS:
9112 return NON_UNION_CLASS_TYPE_P (type1);
9114 case CPTK_IS_EMPTY:
9115 return NON_UNION_CLASS_TYPE_P (type1) && CLASSTYPE_EMPTY_P (type1);
9117 case CPTK_IS_ENUM:
9118 return type_code1 == ENUMERAL_TYPE;
9120 case CPTK_IS_FINAL:
9121 return CLASS_TYPE_P (type1) && CLASSTYPE_FINAL (type1);
9123 case CPTK_IS_LITERAL_TYPE:
9124 return literal_type_p (type1);
9126 case CPTK_IS_POD:
9127 return pod_type_p (type1);
9129 case CPTK_IS_POLYMORPHIC:
9130 return CLASS_TYPE_P (type1) && TYPE_POLYMORPHIC_P (type1);
9132 case CPTK_IS_SAME_AS:
9133 return same_type_p (type1, type2);
9135 case CPTK_IS_STD_LAYOUT:
9136 return std_layout_type_p (type1);
9138 case CPTK_IS_TRIVIAL:
9139 return trivial_type_p (type1);
9141 case CPTK_IS_TRIVIALLY_ASSIGNABLE:
9142 return is_trivially_xible (MODIFY_EXPR, type1, type2);
9144 case CPTK_IS_TRIVIALLY_CONSTRUCTIBLE:
9145 return is_trivially_xible (INIT_EXPR, type1, type2);
9147 case CPTK_IS_TRIVIALLY_COPYABLE:
9148 return trivially_copyable_p (type1);
9150 case CPTK_IS_UNION:
9151 return type_code1 == UNION_TYPE;
9153 case CPTK_IS_ASSIGNABLE:
9154 return is_xible (MODIFY_EXPR, type1, type2);
9156 case CPTK_IS_CONSTRUCTIBLE:
9157 return is_xible (INIT_EXPR, type1, type2);
9159 default:
9160 gcc_unreachable ();
9161 return false;
9165 /* If TYPE is an array of unknown bound, or (possibly cv-qualified)
9166 void, or a complete type, returns true, otherwise false. */
9168 static bool
9169 check_trait_type (tree type)
9171 if (type == NULL_TREE)
9172 return true;
9174 if (TREE_CODE (type) == TREE_LIST)
9175 return (check_trait_type (TREE_VALUE (type))
9176 && check_trait_type (TREE_CHAIN (type)));
9178 if (TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type)
9179 && COMPLETE_TYPE_P (TREE_TYPE (type)))
9180 return true;
9182 if (VOID_TYPE_P (type))
9183 return true;
9185 return !!complete_type_or_else (strip_array_types (type), NULL_TREE);
9188 /* Process a trait expression. */
9190 tree
9191 finish_trait_expr (cp_trait_kind kind, tree type1, tree type2)
9193 if (type1 == error_mark_node
9194 || type2 == error_mark_node)
9195 return error_mark_node;
9197 if (processing_template_decl)
9199 tree trait_expr = make_node (TRAIT_EXPR);
9200 TREE_TYPE (trait_expr) = boolean_type_node;
9201 TRAIT_EXPR_TYPE1 (trait_expr) = type1;
9202 TRAIT_EXPR_TYPE2 (trait_expr) = type2;
9203 TRAIT_EXPR_KIND (trait_expr) = kind;
9204 return trait_expr;
9207 switch (kind)
9209 case CPTK_HAS_NOTHROW_ASSIGN:
9210 case CPTK_HAS_TRIVIAL_ASSIGN:
9211 case CPTK_HAS_NOTHROW_CONSTRUCTOR:
9212 case CPTK_HAS_TRIVIAL_CONSTRUCTOR:
9213 case CPTK_HAS_NOTHROW_COPY:
9214 case CPTK_HAS_TRIVIAL_COPY:
9215 case CPTK_HAS_TRIVIAL_DESTRUCTOR:
9216 case CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS:
9217 case CPTK_HAS_VIRTUAL_DESTRUCTOR:
9218 case CPTK_IS_ABSTRACT:
9219 case CPTK_IS_AGGREGATE:
9220 case CPTK_IS_EMPTY:
9221 case CPTK_IS_FINAL:
9222 case CPTK_IS_LITERAL_TYPE:
9223 case CPTK_IS_POD:
9224 case CPTK_IS_POLYMORPHIC:
9225 case CPTK_IS_STD_LAYOUT:
9226 case CPTK_IS_TRIVIAL:
9227 case CPTK_IS_TRIVIALLY_COPYABLE:
9228 if (!check_trait_type (type1))
9229 return error_mark_node;
9230 break;
9232 case CPTK_IS_ASSIGNABLE:
9233 case CPTK_IS_CONSTRUCTIBLE:
9234 break;
9236 case CPTK_IS_TRIVIALLY_ASSIGNABLE:
9237 case CPTK_IS_TRIVIALLY_CONSTRUCTIBLE:
9238 if (!check_trait_type (type1)
9239 || !check_trait_type (type2))
9240 return error_mark_node;
9241 break;
9243 case CPTK_IS_BASE_OF:
9244 if (NON_UNION_CLASS_TYPE_P (type1) && NON_UNION_CLASS_TYPE_P (type2)
9245 && !same_type_ignoring_top_level_qualifiers_p (type1, type2)
9246 && !complete_type_or_else (type2, NULL_TREE))
9247 /* We already issued an error. */
9248 return error_mark_node;
9249 break;
9251 case CPTK_IS_CLASS:
9252 case CPTK_IS_ENUM:
9253 case CPTK_IS_UNION:
9254 case CPTK_IS_SAME_AS:
9255 break;
9257 default:
9258 gcc_unreachable ();
9261 return (trait_expr_value (kind, type1, type2)
9262 ? boolean_true_node : boolean_false_node);
9265 /* Do-nothing variants of functions to handle pragma FLOAT_CONST_DECIMAL64,
9266 which is ignored for C++. */
9268 void
9269 set_float_const_decimal64 (void)
9273 void
9274 clear_float_const_decimal64 (void)
9278 bool
9279 float_const_decimal64_p (void)
9281 return 0;
9285 /* Return true if T designates the implied `this' parameter. */
9287 bool
9288 is_this_parameter (tree t)
9290 if (!DECL_P (t) || DECL_NAME (t) != this_identifier)
9291 return false;
9292 gcc_assert (TREE_CODE (t) == PARM_DECL || is_capture_proxy (t)
9293 || (cp_binding_oracle && TREE_CODE (t) == VAR_DECL));
9294 return true;
9297 /* Insert the deduced return type for an auto function. */
9299 void
9300 apply_deduced_return_type (tree fco, tree return_type)
9302 tree result;
9304 if (return_type == error_mark_node)
9305 return;
9307 if (DECL_CONV_FN_P (fco))
9308 DECL_NAME (fco) = make_conv_op_name (return_type);
9310 TREE_TYPE (fco) = change_return_type (return_type, TREE_TYPE (fco));
9312 result = DECL_RESULT (fco);
9313 if (result == NULL_TREE)
9314 return;
9315 if (TREE_TYPE (result) == return_type)
9316 return;
9318 if (!processing_template_decl && !VOID_TYPE_P (return_type)
9319 && !complete_type_or_else (return_type, NULL_TREE))
9320 return;
9322 /* We already have a DECL_RESULT from start_preparsed_function.
9323 Now we need to redo the work it and allocate_struct_function
9324 did to reflect the new type. */
9325 gcc_assert (current_function_decl == fco);
9326 result = build_decl (input_location, RESULT_DECL, NULL_TREE,
9327 TYPE_MAIN_VARIANT (return_type));
9328 DECL_ARTIFICIAL (result) = 1;
9329 DECL_IGNORED_P (result) = 1;
9330 cp_apply_type_quals_to_decl (cp_type_quals (return_type),
9331 result);
9333 DECL_RESULT (fco) = result;
9335 if (!processing_template_decl)
9337 bool aggr = aggregate_value_p (result, fco);
9338 #ifdef PCC_STATIC_STRUCT_RETURN
9339 cfun->returns_pcc_struct = aggr;
9340 #endif
9341 cfun->returns_struct = aggr;
9346 /* DECL is a local variable or parameter from the surrounding scope of a
9347 lambda-expression. Returns the decltype for a use of the capture field
9348 for DECL even if it hasn't been captured yet. */
9350 static tree
9351 capture_decltype (tree decl)
9353 tree lam = CLASSTYPE_LAMBDA_EXPR (DECL_CONTEXT (current_function_decl));
9354 /* FIXME do lookup instead of list walk? */
9355 tree cap = value_member (decl, LAMBDA_EXPR_CAPTURE_LIST (lam));
9356 tree type;
9358 if (cap)
9359 type = TREE_TYPE (TREE_PURPOSE (cap));
9360 else
9361 switch (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lam))
9363 case CPLD_NONE:
9364 error ("%qD is not captured", decl);
9365 return error_mark_node;
9367 case CPLD_COPY:
9368 type = TREE_TYPE (decl);
9369 if (TREE_CODE (type) == REFERENCE_TYPE
9370 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
9371 type = TREE_TYPE (type);
9372 break;
9374 case CPLD_REFERENCE:
9375 type = TREE_TYPE (decl);
9376 if (TREE_CODE (type) != REFERENCE_TYPE)
9377 type = build_reference_type (TREE_TYPE (decl));
9378 break;
9380 default:
9381 gcc_unreachable ();
9384 if (TREE_CODE (type) != REFERENCE_TYPE)
9386 if (!LAMBDA_EXPR_MUTABLE_P (lam))
9387 type = cp_build_qualified_type (type, (cp_type_quals (type)
9388 |TYPE_QUAL_CONST));
9389 type = build_reference_type (type);
9391 return type;
9394 /* Build a unary fold expression of EXPR over OP. If IS_RIGHT is true,
9395 this is a right unary fold. Otherwise it is a left unary fold. */
9397 static tree
9398 finish_unary_fold_expr (tree expr, int op, tree_code dir)
9400 // Build a pack expansion (assuming expr has pack type).
9401 if (!uses_parameter_packs (expr))
9403 error_at (location_of (expr), "operand of fold expression has no "
9404 "unexpanded parameter packs");
9405 return error_mark_node;
9407 tree pack = make_pack_expansion (expr);
9409 // Build the fold expression.
9410 tree code = build_int_cstu (integer_type_node, abs (op));
9411 tree fold = build_min_nt_loc (UNKNOWN_LOCATION, dir, code, pack);
9412 FOLD_EXPR_MODIFY_P (fold) = (op < 0);
9413 return fold;
9416 tree
9417 finish_left_unary_fold_expr (tree expr, int op)
9419 return finish_unary_fold_expr (expr, op, UNARY_LEFT_FOLD_EXPR);
9422 tree
9423 finish_right_unary_fold_expr (tree expr, int op)
9425 return finish_unary_fold_expr (expr, op, UNARY_RIGHT_FOLD_EXPR);
9428 /* Build a binary fold expression over EXPR1 and EXPR2. The
9429 associativity of the fold is determined by EXPR1 and EXPR2 (whichever
9430 has an unexpanded parameter pack). */
9432 tree
9433 finish_binary_fold_expr (tree pack, tree init, int op, tree_code dir)
9435 pack = make_pack_expansion (pack);
9436 tree code = build_int_cstu (integer_type_node, abs (op));
9437 tree fold = build_min_nt_loc (UNKNOWN_LOCATION, dir, code, pack, init);
9438 FOLD_EXPR_MODIFY_P (fold) = (op < 0);
9439 return fold;
9442 tree
9443 finish_binary_fold_expr (tree expr1, tree expr2, int op)
9445 // Determine which expr has an unexpanded parameter pack and
9446 // set the pack and initial term.
9447 bool pack1 = uses_parameter_packs (expr1);
9448 bool pack2 = uses_parameter_packs (expr2);
9449 if (pack1 && !pack2)
9450 return finish_binary_fold_expr (expr1, expr2, op, BINARY_RIGHT_FOLD_EXPR);
9451 else if (pack2 && !pack1)
9452 return finish_binary_fold_expr (expr2, expr1, op, BINARY_LEFT_FOLD_EXPR);
9453 else
9455 if (pack1)
9456 error ("both arguments in binary fold have unexpanded parameter packs");
9457 else
9458 error ("no unexpanded parameter packs in binary fold");
9460 return error_mark_node;
9463 /* Finish __builtin_launder (arg). */
9465 tree
9466 finish_builtin_launder (location_t loc, tree arg, tsubst_flags_t complain)
9468 tree orig_arg = arg;
9469 if (!type_dependent_expression_p (arg))
9470 arg = decay_conversion (arg, complain);
9471 if (error_operand_p (arg))
9472 return error_mark_node;
9473 if (!type_dependent_expression_p (arg)
9474 && TREE_CODE (TREE_TYPE (arg)) != POINTER_TYPE)
9476 error_at (loc, "non-pointer argument to %<__builtin_launder%>");
9477 return error_mark_node;
9479 if (processing_template_decl)
9480 arg = orig_arg;
9481 return build_call_expr_internal_loc (loc, IFN_LAUNDER,
9482 TREE_TYPE (arg), 1, arg);
9485 #include "gt-cp-semantics.h"