2015-10-18 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / gcc / cp / semantics.c
blob8796b176c15ac6cf785f43c8dbb9f2a37840db74
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-2015 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 "tm.h"
30 #include "alias.h"
31 #include "tree.h"
32 #include "stmt.h"
33 #include "varasm.h"
34 #include "stor-layout.h"
35 #include "stringpool.h"
36 #include "cp-tree.h"
37 #include "c-family/c-common.h"
38 #include "c-family/c-objc.h"
39 #include "tree-inline.h"
40 #include "intl.h"
41 #include "toplev.h"
42 #include "flags.h"
43 #include "timevar.h"
44 #include "diagnostic.h"
45 #include "hard-reg-set.h"
46 #include "function.h"
47 #include "cgraph.h"
48 #include "tree-iterator.h"
49 #include "target.h"
50 #include "gimplify.h"
51 #include "bitmap.h"
52 #include "omp-low.h"
53 #include "builtins.h"
54 #include "convert.h"
55 #include "gomp-constants.h"
57 /* There routines provide a modular interface to perform many parsing
58 operations. They may therefore be used during actual parsing, or
59 during template instantiation, which may be regarded as a
60 degenerate form of parsing. */
62 static tree maybe_convert_cond (tree);
63 static tree finalize_nrv_r (tree *, int *, void *);
64 static tree capture_decltype (tree);
66 /* Used for OpenMP non-static data member privatization. */
68 static hash_map<tree, tree> *omp_private_member_map;
69 static vec<tree> omp_private_member_vec;
70 static bool omp_private_member_ignore_next;
73 /* Deferred Access Checking Overview
74 ---------------------------------
76 Most C++ expressions and declarations require access checking
77 to be performed during parsing. However, in several cases,
78 this has to be treated differently.
80 For member declarations, access checking has to be deferred
81 until more information about the declaration is known. For
82 example:
84 class A {
85 typedef int X;
86 public:
87 X f();
90 A::X A::f();
91 A::X g();
93 When we are parsing the function return type `A::X', we don't
94 really know if this is allowed until we parse the function name.
96 Furthermore, some contexts require that access checking is
97 never performed at all. These include class heads, and template
98 instantiations.
100 Typical use of access checking functions is described here:
102 1. When we enter a context that requires certain access checking
103 mode, the function `push_deferring_access_checks' is called with
104 DEFERRING argument specifying the desired mode. Access checking
105 may be performed immediately (dk_no_deferred), deferred
106 (dk_deferred), or not performed (dk_no_check).
108 2. When a declaration such as a type, or a variable, is encountered,
109 the function `perform_or_defer_access_check' is called. It
110 maintains a vector of all deferred checks.
112 3. The global `current_class_type' or `current_function_decl' is then
113 setup by the parser. `enforce_access' relies on these information
114 to check access.
116 4. Upon exiting the context mentioned in step 1,
117 `perform_deferred_access_checks' is called to check all declaration
118 stored in the vector. `pop_deferring_access_checks' is then
119 called to restore the previous access checking mode.
121 In case of parsing error, we simply call `pop_deferring_access_checks'
122 without `perform_deferred_access_checks'. */
124 struct GTY(()) deferred_access {
125 /* A vector representing name-lookups for which we have deferred
126 checking access controls. We cannot check the accessibility of
127 names used in a decl-specifier-seq until we know what is being
128 declared because code like:
130 class A {
131 class B {};
132 B* f();
135 A::B* A::f() { return 0; }
137 is valid, even though `A::B' is not generally accessible. */
138 vec<deferred_access_check, va_gc> * GTY(()) deferred_access_checks;
140 /* The current mode of access checks. */
141 enum deferring_kind deferring_access_checks_kind;
145 /* Data for deferred access checking. */
146 static GTY(()) vec<deferred_access, va_gc> *deferred_access_stack;
147 static GTY(()) unsigned deferred_access_no_check;
149 /* Save the current deferred access states and start deferred
150 access checking iff DEFER_P is true. */
152 void
153 push_deferring_access_checks (deferring_kind deferring)
155 /* For context like template instantiation, access checking
156 disabling applies to all nested context. */
157 if (deferred_access_no_check || deferring == dk_no_check)
158 deferred_access_no_check++;
159 else
161 deferred_access e = {NULL, deferring};
162 vec_safe_push (deferred_access_stack, e);
166 /* Save the current deferred access states and start deferred access
167 checking, continuing the set of deferred checks in CHECKS. */
169 void
170 reopen_deferring_access_checks (vec<deferred_access_check, va_gc> * checks)
172 push_deferring_access_checks (dk_deferred);
173 if (!deferred_access_no_check)
174 deferred_access_stack->last().deferred_access_checks = checks;
177 /* Resume deferring access checks again after we stopped doing
178 this previously. */
180 void
181 resume_deferring_access_checks (void)
183 if (!deferred_access_no_check)
184 deferred_access_stack->last().deferring_access_checks_kind = dk_deferred;
187 /* Stop deferring access checks. */
189 void
190 stop_deferring_access_checks (void)
192 if (!deferred_access_no_check)
193 deferred_access_stack->last().deferring_access_checks_kind = dk_no_deferred;
196 /* Discard the current deferred access checks and restore the
197 previous states. */
199 void
200 pop_deferring_access_checks (void)
202 if (deferred_access_no_check)
203 deferred_access_no_check--;
204 else
205 deferred_access_stack->pop ();
208 /* Returns a TREE_LIST representing the deferred checks.
209 The TREE_PURPOSE of each node is the type through which the
210 access occurred; the TREE_VALUE is the declaration named.
213 vec<deferred_access_check, va_gc> *
214 get_deferred_access_checks (void)
216 if (deferred_access_no_check)
217 return NULL;
218 else
219 return (deferred_access_stack->last().deferred_access_checks);
222 /* Take current deferred checks and combine with the
223 previous states if we also defer checks previously.
224 Otherwise perform checks now. */
226 void
227 pop_to_parent_deferring_access_checks (void)
229 if (deferred_access_no_check)
230 deferred_access_no_check--;
231 else
233 vec<deferred_access_check, va_gc> *checks;
234 deferred_access *ptr;
236 checks = (deferred_access_stack->last ().deferred_access_checks);
238 deferred_access_stack->pop ();
239 ptr = &deferred_access_stack->last ();
240 if (ptr->deferring_access_checks_kind == dk_no_deferred)
242 /* Check access. */
243 perform_access_checks (checks, tf_warning_or_error);
245 else
247 /* Merge with parent. */
248 int i, j;
249 deferred_access_check *chk, *probe;
251 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
253 FOR_EACH_VEC_SAFE_ELT (ptr->deferred_access_checks, j, probe)
255 if (probe->binfo == chk->binfo &&
256 probe->decl == chk->decl &&
257 probe->diag_decl == chk->diag_decl)
258 goto found;
260 /* Insert into parent's checks. */
261 vec_safe_push (ptr->deferred_access_checks, *chk);
262 found:;
268 /* Perform the access checks in CHECKS. The TREE_PURPOSE of each node
269 is the BINFO indicating the qualifying scope used to access the
270 DECL node stored in the TREE_VALUE of the node. If CHECKS is empty
271 or we aren't in SFINAE context or all the checks succeed return TRUE,
272 otherwise FALSE. */
274 bool
275 perform_access_checks (vec<deferred_access_check, va_gc> *checks,
276 tsubst_flags_t complain)
278 int i;
279 deferred_access_check *chk;
280 location_t loc = input_location;
281 bool ok = true;
283 if (!checks)
284 return true;
286 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
288 input_location = chk->loc;
289 ok &= enforce_access (chk->binfo, chk->decl, chk->diag_decl, complain);
292 input_location = loc;
293 return (complain & tf_error) ? true : ok;
296 /* Perform the deferred access checks.
298 After performing the checks, we still have to keep the list
299 `deferred_access_stack->deferred_access_checks' since we may want
300 to check access for them again later in a different context.
301 For example:
303 class A {
304 typedef int X;
305 static X a;
307 A::X A::a, x; // No error for `A::a', error for `x'
309 We have to perform deferred access of `A::X', first with `A::a',
310 next with `x'. Return value like perform_access_checks above. */
312 bool
313 perform_deferred_access_checks (tsubst_flags_t complain)
315 return perform_access_checks (get_deferred_access_checks (), complain);
318 /* Defer checking the accessibility of DECL, when looked up in
319 BINFO. DIAG_DECL is the declaration to use to print diagnostics.
320 Return value like perform_access_checks above. */
322 bool
323 perform_or_defer_access_check (tree binfo, tree decl, tree diag_decl,
324 tsubst_flags_t complain)
326 int i;
327 deferred_access *ptr;
328 deferred_access_check *chk;
331 /* Exit if we are in a context that no access checking is performed.
333 if (deferred_access_no_check)
334 return true;
336 gcc_assert (TREE_CODE (binfo) == TREE_BINFO);
338 ptr = &deferred_access_stack->last ();
340 /* If we are not supposed to defer access checks, just check now. */
341 if (ptr->deferring_access_checks_kind == dk_no_deferred)
343 bool ok = enforce_access (binfo, decl, diag_decl, complain);
344 return (complain & tf_error) ? true : ok;
347 /* See if we are already going to perform this check. */
348 FOR_EACH_VEC_SAFE_ELT (ptr->deferred_access_checks, i, chk)
350 if (chk->decl == decl && chk->binfo == binfo &&
351 chk->diag_decl == diag_decl)
353 return true;
356 /* If not, record the check. */
357 deferred_access_check new_access = {binfo, decl, diag_decl, input_location};
358 vec_safe_push (ptr->deferred_access_checks, new_access);
360 return true;
363 /* Returns nonzero if the current statement is a full expression,
364 i.e. temporaries created during that statement should be destroyed
365 at the end of the statement. */
368 stmts_are_full_exprs_p (void)
370 return current_stmt_tree ()->stmts_are_full_exprs_p;
373 /* T is a statement. Add it to the statement-tree. This is the C++
374 version. The C/ObjC frontends have a slightly different version of
375 this function. */
377 tree
378 add_stmt (tree t)
380 enum tree_code code = TREE_CODE (t);
382 if (EXPR_P (t) && code != LABEL_EXPR)
384 if (!EXPR_HAS_LOCATION (t))
385 SET_EXPR_LOCATION (t, input_location);
387 /* When we expand a statement-tree, we must know whether or not the
388 statements are full-expressions. We record that fact here. */
389 STMT_IS_FULL_EXPR_P (t) = stmts_are_full_exprs_p ();
392 if (code == LABEL_EXPR || code == CASE_LABEL_EXPR)
393 STATEMENT_LIST_HAS_LABEL (cur_stmt_list) = 1;
395 /* Add T to the statement-tree. Non-side-effect statements need to be
396 recorded during statement expressions. */
397 gcc_checking_assert (!stmt_list_stack->is_empty ());
398 append_to_statement_list_force (t, &cur_stmt_list);
400 return t;
403 /* Returns the stmt_tree to which statements are currently being added. */
405 stmt_tree
406 current_stmt_tree (void)
408 return (cfun
409 ? &cfun->language->base.x_stmt_tree
410 : &scope_chain->x_stmt_tree);
413 /* If statements are full expressions, wrap STMT in a CLEANUP_POINT_EXPR. */
415 static tree
416 maybe_cleanup_point_expr (tree expr)
418 if (!processing_template_decl && stmts_are_full_exprs_p ())
419 expr = fold_build_cleanup_point_expr (TREE_TYPE (expr), expr);
420 return expr;
423 /* Like maybe_cleanup_point_expr except have the type of the new expression be
424 void so we don't need to create a temporary variable to hold the inner
425 expression. The reason why we do this is because the original type might be
426 an aggregate and we cannot create a temporary variable for that type. */
428 tree
429 maybe_cleanup_point_expr_void (tree expr)
431 if (!processing_template_decl && stmts_are_full_exprs_p ())
432 expr = fold_build_cleanup_point_expr (void_type_node, expr);
433 return expr;
438 /* Create a declaration statement for the declaration given by the DECL. */
440 void
441 add_decl_expr (tree decl)
443 tree r = build_stmt (input_location, DECL_EXPR, decl);
444 if (DECL_INITIAL (decl)
445 || (DECL_SIZE (decl) && TREE_SIDE_EFFECTS (DECL_SIZE (decl))))
446 r = maybe_cleanup_point_expr_void (r);
447 add_stmt (r);
450 /* Finish a scope. */
452 tree
453 do_poplevel (tree stmt_list)
455 tree block = NULL;
457 if (stmts_are_full_exprs_p ())
458 block = poplevel (kept_level_p (), 1, 0);
460 stmt_list = pop_stmt_list (stmt_list);
462 if (!processing_template_decl)
464 stmt_list = c_build_bind_expr (input_location, block, stmt_list);
465 /* ??? See c_end_compound_stmt re statement expressions. */
468 return stmt_list;
471 /* Begin a new scope. */
473 static tree
474 do_pushlevel (scope_kind sk)
476 tree ret = push_stmt_list ();
477 if (stmts_are_full_exprs_p ())
478 begin_scope (sk, NULL);
479 return ret;
482 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
483 when the current scope is exited. EH_ONLY is true when this is not
484 meant to apply to normal control flow transfer. */
486 void
487 push_cleanup (tree decl, tree cleanup, bool eh_only)
489 tree stmt = build_stmt (input_location, CLEANUP_STMT, NULL, cleanup, decl);
490 CLEANUP_EH_ONLY (stmt) = eh_only;
491 add_stmt (stmt);
492 CLEANUP_BODY (stmt) = push_stmt_list ();
495 /* Simple infinite loop tracking for -Wreturn-type. We keep a stack of all
496 the current loops, represented by 'NULL_TREE' if we've seen a possible
497 exit, and 'error_mark_node' if not. This is currently used only to
498 suppress the warning about a function with no return statements, and
499 therefore we don't bother noting returns as possible exits. We also
500 don't bother with gotos. */
502 static void
503 begin_maybe_infinite_loop (tree cond)
505 /* Only track this while parsing a function, not during instantiation. */
506 if (!cfun || (DECL_TEMPLATE_INSTANTIATION (current_function_decl)
507 && !processing_template_decl))
508 return;
509 bool maybe_infinite = true;
510 if (cond)
512 cond = fold_non_dependent_expr (cond);
513 maybe_infinite = integer_nonzerop (cond);
515 vec_safe_push (cp_function_chain->infinite_loops,
516 maybe_infinite ? error_mark_node : NULL_TREE);
520 /* A break is a possible exit for the current loop. */
522 void
523 break_maybe_infinite_loop (void)
525 if (!cfun)
526 return;
527 cp_function_chain->infinite_loops->last() = NULL_TREE;
530 /* If we reach the end of the loop without seeing a possible exit, we have
531 an infinite loop. */
533 static void
534 end_maybe_infinite_loop (tree cond)
536 if (!cfun || (DECL_TEMPLATE_INSTANTIATION (current_function_decl)
537 && !processing_template_decl))
538 return;
539 tree current = cp_function_chain->infinite_loops->pop();
540 if (current != NULL_TREE)
542 cond = fold_non_dependent_expr (cond);
543 if (integer_nonzerop (cond))
544 current_function_infinite_loop = 1;
549 /* Begin a conditional that might contain a declaration. When generating
550 normal code, we want the declaration to appear before the statement
551 containing the conditional. When generating template code, we want the
552 conditional to be rendered as the raw DECL_EXPR. */
554 static void
555 begin_cond (tree *cond_p)
557 if (processing_template_decl)
558 *cond_p = push_stmt_list ();
561 /* Finish such a conditional. */
563 static void
564 finish_cond (tree *cond_p, tree expr)
566 if (processing_template_decl)
568 tree cond = pop_stmt_list (*cond_p);
570 if (expr == NULL_TREE)
571 /* Empty condition in 'for'. */
572 gcc_assert (empty_expr_stmt_p (cond));
573 else if (check_for_bare_parameter_packs (expr))
574 expr = error_mark_node;
575 else if (!empty_expr_stmt_p (cond))
576 expr = build2 (COMPOUND_EXPR, TREE_TYPE (expr), cond, expr);
578 *cond_p = expr;
581 /* If *COND_P specifies a conditional with a declaration, transform the
582 loop such that
583 while (A x = 42) { }
584 for (; A x = 42;) { }
585 becomes
586 while (true) { A x = 42; if (!x) break; }
587 for (;;) { A x = 42; if (!x) break; }
588 The statement list for BODY will be empty if the conditional did
589 not declare anything. */
591 static void
592 simplify_loop_decl_cond (tree *cond_p, tree body)
594 tree cond, if_stmt;
596 if (!TREE_SIDE_EFFECTS (body))
597 return;
599 cond = *cond_p;
600 *cond_p = boolean_true_node;
602 if_stmt = begin_if_stmt ();
603 cond = cp_build_unary_op (TRUTH_NOT_EXPR, cond, 0, tf_warning_or_error);
604 finish_if_stmt_cond (cond, if_stmt);
605 finish_break_stmt ();
606 finish_then_clause (if_stmt);
607 finish_if_stmt (if_stmt);
610 /* Finish a goto-statement. */
612 tree
613 finish_goto_stmt (tree destination)
615 if (identifier_p (destination))
616 destination = lookup_label (destination);
618 /* We warn about unused labels with -Wunused. That means we have to
619 mark the used labels as used. */
620 if (TREE_CODE (destination) == LABEL_DECL)
621 TREE_USED (destination) = 1;
622 else
624 if (check_no_cilk (destination,
625 "Cilk array notation cannot be used as a computed goto expression",
626 "%<_Cilk_spawn%> statement cannot be used as a computed goto expression"))
627 destination = error_mark_node;
628 destination = mark_rvalue_use (destination);
629 if (!processing_template_decl)
631 destination = cp_convert (ptr_type_node, destination,
632 tf_warning_or_error);
633 if (error_operand_p (destination))
634 return NULL_TREE;
635 destination
636 = fold_build_cleanup_point_expr (TREE_TYPE (destination),
637 destination);
641 check_goto (destination);
643 return add_stmt (build_stmt (input_location, GOTO_EXPR, destination));
646 /* COND is the condition-expression for an if, while, etc.,
647 statement. Convert it to a boolean value, if appropriate.
648 In addition, verify sequence points if -Wsequence-point is enabled. */
650 static tree
651 maybe_convert_cond (tree cond)
653 /* Empty conditions remain empty. */
654 if (!cond)
655 return NULL_TREE;
657 /* Wait until we instantiate templates before doing conversion. */
658 if (processing_template_decl)
659 return cond;
661 if (warn_sequence_point)
662 verify_sequence_points (cond);
664 /* Do the conversion. */
665 cond = convert_from_reference (cond);
667 if (TREE_CODE (cond) == MODIFY_EXPR
668 && !TREE_NO_WARNING (cond)
669 && warn_parentheses)
671 warning (OPT_Wparentheses,
672 "suggest parentheses around assignment used as truth value");
673 TREE_NO_WARNING (cond) = 1;
676 return condition_conversion (cond);
679 /* Finish an expression-statement, whose EXPRESSION is as indicated. */
681 tree
682 finish_expr_stmt (tree expr)
684 tree r = NULL_TREE;
686 if (expr != NULL_TREE)
688 if (!processing_template_decl)
690 if (warn_sequence_point)
691 verify_sequence_points (expr);
692 expr = convert_to_void (expr, ICV_STATEMENT, tf_warning_or_error);
694 else if (!type_dependent_expression_p (expr))
695 convert_to_void (build_non_dependent_expr (expr), ICV_STATEMENT,
696 tf_warning_or_error);
698 if (check_for_bare_parameter_packs (expr))
699 expr = error_mark_node;
701 /* Simplification of inner statement expressions, compound exprs,
702 etc can result in us already having an EXPR_STMT. */
703 if (TREE_CODE (expr) != CLEANUP_POINT_EXPR)
705 if (TREE_CODE (expr) != EXPR_STMT)
706 expr = build_stmt (input_location, EXPR_STMT, expr);
707 expr = maybe_cleanup_point_expr_void (expr);
710 r = add_stmt (expr);
713 return r;
717 /* Begin an if-statement. Returns a newly created IF_STMT if
718 appropriate. */
720 tree
721 begin_if_stmt (void)
723 tree r, scope;
724 scope = do_pushlevel (sk_cond);
725 r = build_stmt (input_location, IF_STMT, NULL_TREE,
726 NULL_TREE, NULL_TREE, scope);
727 begin_cond (&IF_COND (r));
728 return r;
731 /* Process the COND of an if-statement, which may be given by
732 IF_STMT. */
734 void
735 finish_if_stmt_cond (tree cond, tree if_stmt)
737 finish_cond (&IF_COND (if_stmt), maybe_convert_cond (cond));
738 add_stmt (if_stmt);
739 THEN_CLAUSE (if_stmt) = push_stmt_list ();
742 /* Finish the then-clause of an if-statement, which may be given by
743 IF_STMT. */
745 tree
746 finish_then_clause (tree if_stmt)
748 THEN_CLAUSE (if_stmt) = pop_stmt_list (THEN_CLAUSE (if_stmt));
749 return if_stmt;
752 /* Begin the else-clause of an if-statement. */
754 void
755 begin_else_clause (tree if_stmt)
757 ELSE_CLAUSE (if_stmt) = push_stmt_list ();
760 /* Finish the else-clause of an if-statement, which may be given by
761 IF_STMT. */
763 void
764 finish_else_clause (tree if_stmt)
766 ELSE_CLAUSE (if_stmt) = pop_stmt_list (ELSE_CLAUSE (if_stmt));
769 /* Finish an if-statement. */
771 void
772 finish_if_stmt (tree if_stmt)
774 tree scope = IF_SCOPE (if_stmt);
775 IF_SCOPE (if_stmt) = NULL;
776 add_stmt (do_poplevel (scope));
779 /* Begin a while-statement. Returns a newly created WHILE_STMT if
780 appropriate. */
782 tree
783 begin_while_stmt (void)
785 tree r;
786 r = build_stmt (input_location, WHILE_STMT, NULL_TREE, NULL_TREE);
787 add_stmt (r);
788 WHILE_BODY (r) = do_pushlevel (sk_block);
789 begin_cond (&WHILE_COND (r));
790 return r;
793 /* Process the COND of a while-statement, which may be given by
794 WHILE_STMT. */
796 void
797 finish_while_stmt_cond (tree cond, tree while_stmt, bool ivdep)
799 if (check_no_cilk (cond,
800 "Cilk array notation cannot be used as a condition for while statement",
801 "%<_Cilk_spawn%> statement cannot be used as a condition for while statement"))
802 cond = error_mark_node;
803 cond = maybe_convert_cond (cond);
804 finish_cond (&WHILE_COND (while_stmt), cond);
805 begin_maybe_infinite_loop (cond);
806 if (ivdep && cond != error_mark_node)
807 WHILE_COND (while_stmt) = build2 (ANNOTATE_EXPR,
808 TREE_TYPE (WHILE_COND (while_stmt)),
809 WHILE_COND (while_stmt),
810 build_int_cst (integer_type_node,
811 annot_expr_ivdep_kind));
812 simplify_loop_decl_cond (&WHILE_COND (while_stmt), WHILE_BODY (while_stmt));
815 /* Finish a while-statement, which may be given by WHILE_STMT. */
817 void
818 finish_while_stmt (tree while_stmt)
820 end_maybe_infinite_loop (boolean_true_node);
821 WHILE_BODY (while_stmt) = do_poplevel (WHILE_BODY (while_stmt));
824 /* Begin a do-statement. Returns a newly created DO_STMT if
825 appropriate. */
827 tree
828 begin_do_stmt (void)
830 tree r = build_stmt (input_location, DO_STMT, NULL_TREE, NULL_TREE);
831 begin_maybe_infinite_loop (boolean_true_node);
832 add_stmt (r);
833 DO_BODY (r) = push_stmt_list ();
834 return r;
837 /* Finish the body of a do-statement, which may be given by DO_STMT. */
839 void
840 finish_do_body (tree do_stmt)
842 tree body = DO_BODY (do_stmt) = pop_stmt_list (DO_BODY (do_stmt));
844 if (TREE_CODE (body) == STATEMENT_LIST && STATEMENT_LIST_TAIL (body))
845 body = STATEMENT_LIST_TAIL (body)->stmt;
847 if (IS_EMPTY_STMT (body))
848 warning (OPT_Wempty_body,
849 "suggest explicit braces around empty body in %<do%> statement");
852 /* Finish a do-statement, which may be given by DO_STMT, and whose
853 COND is as indicated. */
855 void
856 finish_do_stmt (tree cond, tree do_stmt, bool ivdep)
858 if (check_no_cilk (cond,
859 "Cilk array notation cannot be used as a condition for a do-while statement",
860 "%<_Cilk_spawn%> statement cannot be used as a condition for a do-while statement"))
861 cond = error_mark_node;
862 cond = maybe_convert_cond (cond);
863 end_maybe_infinite_loop (cond);
864 if (ivdep && cond != error_mark_node)
865 cond = build2 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
866 build_int_cst (integer_type_node, annot_expr_ivdep_kind));
867 DO_COND (do_stmt) = cond;
870 /* Finish a return-statement. The EXPRESSION returned, if any, is as
871 indicated. */
873 tree
874 finish_return_stmt (tree expr)
876 tree r;
877 bool no_warning;
879 expr = check_return_expr (expr, &no_warning);
881 if (error_operand_p (expr)
882 || (flag_openmp && !check_omp_return ()))
884 /* Suppress -Wreturn-type for this function. */
885 if (warn_return_type)
886 TREE_NO_WARNING (current_function_decl) = true;
887 return error_mark_node;
890 if (!processing_template_decl)
892 if (warn_sequence_point)
893 verify_sequence_points (expr);
895 if (DECL_DESTRUCTOR_P (current_function_decl)
896 || (DECL_CONSTRUCTOR_P (current_function_decl)
897 && targetm.cxx.cdtor_returns_this ()))
899 /* Similarly, all destructors must run destructors for
900 base-classes before returning. So, all returns in a
901 destructor get sent to the DTOR_LABEL; finish_function emits
902 code to return a value there. */
903 return finish_goto_stmt (cdtor_label);
907 r = build_stmt (input_location, RETURN_EXPR, expr);
908 TREE_NO_WARNING (r) |= no_warning;
909 r = maybe_cleanup_point_expr_void (r);
910 r = add_stmt (r);
912 return r;
915 /* Begin the scope of a for-statement or a range-for-statement.
916 Both the returned trees are to be used in a call to
917 begin_for_stmt or begin_range_for_stmt. */
919 tree
920 begin_for_scope (tree *init)
922 tree scope = NULL_TREE;
923 if (flag_new_for_scope > 0)
924 scope = do_pushlevel (sk_for);
926 if (processing_template_decl)
927 *init = push_stmt_list ();
928 else
929 *init = NULL_TREE;
931 return scope;
934 /* Begin a for-statement. Returns a new FOR_STMT.
935 SCOPE and INIT should be the return of begin_for_scope,
936 or both NULL_TREE */
938 tree
939 begin_for_stmt (tree scope, tree init)
941 tree r;
943 r = build_stmt (input_location, FOR_STMT, NULL_TREE, NULL_TREE,
944 NULL_TREE, NULL_TREE, NULL_TREE);
946 if (scope == NULL_TREE)
948 gcc_assert (!init || !(flag_new_for_scope > 0));
949 if (!init)
950 scope = begin_for_scope (&init);
952 FOR_INIT_STMT (r) = init;
953 FOR_SCOPE (r) = scope;
955 return r;
958 /* Finish the for-init-statement of a for-statement, which may be
959 given by FOR_STMT. */
961 void
962 finish_for_init_stmt (tree for_stmt)
964 if (processing_template_decl)
965 FOR_INIT_STMT (for_stmt) = pop_stmt_list (FOR_INIT_STMT (for_stmt));
966 add_stmt (for_stmt);
967 FOR_BODY (for_stmt) = do_pushlevel (sk_block);
968 begin_cond (&FOR_COND (for_stmt));
971 /* Finish the COND of a for-statement, which may be given by
972 FOR_STMT. */
974 void
975 finish_for_cond (tree cond, tree for_stmt, bool ivdep)
977 if (check_no_cilk (cond,
978 "Cilk array notation cannot be used in a condition for a for-loop",
979 "%<_Cilk_spawn%> statement cannot be used in a condition for a for-loop"))
980 cond = error_mark_node;
981 cond = maybe_convert_cond (cond);
982 finish_cond (&FOR_COND (for_stmt), cond);
983 begin_maybe_infinite_loop (cond);
984 if (ivdep && cond != error_mark_node)
985 FOR_COND (for_stmt) = build2 (ANNOTATE_EXPR,
986 TREE_TYPE (FOR_COND (for_stmt)),
987 FOR_COND (for_stmt),
988 build_int_cst (integer_type_node,
989 annot_expr_ivdep_kind));
990 simplify_loop_decl_cond (&FOR_COND (for_stmt), FOR_BODY (for_stmt));
993 /* Finish the increment-EXPRESSION in a for-statement, which may be
994 given by FOR_STMT. */
996 void
997 finish_for_expr (tree expr, tree for_stmt)
999 if (!expr)
1000 return;
1001 /* If EXPR is an overloaded function, issue an error; there is no
1002 context available to use to perform overload resolution. */
1003 if (type_unknown_p (expr))
1005 cxx_incomplete_type_error (expr, TREE_TYPE (expr));
1006 expr = error_mark_node;
1008 if (!processing_template_decl)
1010 if (warn_sequence_point)
1011 verify_sequence_points (expr);
1012 expr = convert_to_void (expr, ICV_THIRD_IN_FOR,
1013 tf_warning_or_error);
1015 else if (!type_dependent_expression_p (expr))
1016 convert_to_void (build_non_dependent_expr (expr), ICV_THIRD_IN_FOR,
1017 tf_warning_or_error);
1018 expr = maybe_cleanup_point_expr_void (expr);
1019 if (check_for_bare_parameter_packs (expr))
1020 expr = error_mark_node;
1021 FOR_EXPR (for_stmt) = expr;
1024 /* Finish the body of a for-statement, which may be given by
1025 FOR_STMT. The increment-EXPR for the loop must be
1026 provided.
1027 It can also finish RANGE_FOR_STMT. */
1029 void
1030 finish_for_stmt (tree for_stmt)
1032 end_maybe_infinite_loop (boolean_true_node);
1034 if (TREE_CODE (for_stmt) == RANGE_FOR_STMT)
1035 RANGE_FOR_BODY (for_stmt) = do_poplevel (RANGE_FOR_BODY (for_stmt));
1036 else
1037 FOR_BODY (for_stmt) = do_poplevel (FOR_BODY (for_stmt));
1039 /* Pop the scope for the body of the loop. */
1040 if (flag_new_for_scope > 0)
1042 tree scope;
1043 tree *scope_ptr = (TREE_CODE (for_stmt) == RANGE_FOR_STMT
1044 ? &RANGE_FOR_SCOPE (for_stmt)
1045 : &FOR_SCOPE (for_stmt));
1046 scope = *scope_ptr;
1047 *scope_ptr = NULL;
1048 add_stmt (do_poplevel (scope));
1052 /* Begin a range-for-statement. Returns a new RANGE_FOR_STMT.
1053 SCOPE and INIT should be the return of begin_for_scope,
1054 or both NULL_TREE .
1055 To finish it call finish_for_stmt(). */
1057 tree
1058 begin_range_for_stmt (tree scope, tree init)
1060 tree r;
1062 begin_maybe_infinite_loop (boolean_false_node);
1064 r = build_stmt (input_location, RANGE_FOR_STMT,
1065 NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE);
1067 if (scope == NULL_TREE)
1069 gcc_assert (!init || !(flag_new_for_scope > 0));
1070 if (!init)
1071 scope = begin_for_scope (&init);
1074 /* RANGE_FOR_STMTs do not use nor save the init tree, so we
1075 pop it now. */
1076 if (init)
1077 pop_stmt_list (init);
1078 RANGE_FOR_SCOPE (r) = scope;
1080 return r;
1083 /* Finish the head of a range-based for statement, which may
1084 be given by RANGE_FOR_STMT. DECL must be the declaration
1085 and EXPR must be the loop expression. */
1087 void
1088 finish_range_for_decl (tree range_for_stmt, tree decl, tree expr)
1090 RANGE_FOR_DECL (range_for_stmt) = decl;
1091 RANGE_FOR_EXPR (range_for_stmt) = expr;
1092 add_stmt (range_for_stmt);
1093 RANGE_FOR_BODY (range_for_stmt) = do_pushlevel (sk_block);
1096 /* Finish a break-statement. */
1098 tree
1099 finish_break_stmt (void)
1101 /* In switch statements break is sometimes stylistically used after
1102 a return statement. This can lead to spurious warnings about
1103 control reaching the end of a non-void function when it is
1104 inlined. Note that we are calling block_may_fallthru with
1105 language specific tree nodes; this works because
1106 block_may_fallthru returns true when given something it does not
1107 understand. */
1108 if (!block_may_fallthru (cur_stmt_list))
1109 return void_node;
1110 return add_stmt (build_stmt (input_location, BREAK_STMT));
1113 /* Finish a continue-statement. */
1115 tree
1116 finish_continue_stmt (void)
1118 return add_stmt (build_stmt (input_location, CONTINUE_STMT));
1121 /* Begin a switch-statement. Returns a new SWITCH_STMT if
1122 appropriate. */
1124 tree
1125 begin_switch_stmt (void)
1127 tree r, scope;
1129 scope = do_pushlevel (sk_cond);
1130 r = build_stmt (input_location, SWITCH_STMT, NULL_TREE, NULL_TREE, NULL_TREE, scope);
1132 begin_cond (&SWITCH_STMT_COND (r));
1134 return r;
1137 /* Finish the cond of a switch-statement. */
1139 void
1140 finish_switch_cond (tree cond, tree switch_stmt)
1142 tree orig_type = NULL;
1144 if (check_no_cilk (cond,
1145 "Cilk array notation cannot be used as a condition for switch statement",
1146 "%<_Cilk_spawn%> statement cannot be used as a condition for switch statement"))
1147 cond = error_mark_node;
1149 if (!processing_template_decl)
1151 /* Convert the condition to an integer or enumeration type. */
1152 cond = build_expr_type_conversion (WANT_INT | WANT_ENUM, cond, true);
1153 if (cond == NULL_TREE)
1155 error ("switch quantity not an integer");
1156 cond = error_mark_node;
1158 /* We want unlowered type here to handle enum bit-fields. */
1159 orig_type = unlowered_expr_type (cond);
1160 if (TREE_CODE (orig_type) != ENUMERAL_TYPE)
1161 orig_type = TREE_TYPE (cond);
1162 if (cond != error_mark_node)
1164 /* [stmt.switch]
1166 Integral promotions are performed. */
1167 cond = perform_integral_promotions (cond);
1168 cond = maybe_cleanup_point_expr (cond);
1171 if (check_for_bare_parameter_packs (cond))
1172 cond = error_mark_node;
1173 else if (!processing_template_decl && warn_sequence_point)
1174 verify_sequence_points (cond);
1176 finish_cond (&SWITCH_STMT_COND (switch_stmt), cond);
1177 SWITCH_STMT_TYPE (switch_stmt) = orig_type;
1178 add_stmt (switch_stmt);
1179 push_switch (switch_stmt);
1180 SWITCH_STMT_BODY (switch_stmt) = push_stmt_list ();
1183 /* Finish the body of a switch-statement, which may be given by
1184 SWITCH_STMT. The COND to switch on is indicated. */
1186 void
1187 finish_switch_stmt (tree switch_stmt)
1189 tree scope;
1191 SWITCH_STMT_BODY (switch_stmt) =
1192 pop_stmt_list (SWITCH_STMT_BODY (switch_stmt));
1193 pop_switch ();
1195 scope = SWITCH_STMT_SCOPE (switch_stmt);
1196 SWITCH_STMT_SCOPE (switch_stmt) = NULL;
1197 add_stmt (do_poplevel (scope));
1200 /* Begin a try-block. Returns a newly-created TRY_BLOCK if
1201 appropriate. */
1203 tree
1204 begin_try_block (void)
1206 tree r = build_stmt (input_location, TRY_BLOCK, NULL_TREE, NULL_TREE);
1207 add_stmt (r);
1208 TRY_STMTS (r) = push_stmt_list ();
1209 return r;
1212 /* Likewise, for a function-try-block. The block returned in
1213 *COMPOUND_STMT is an artificial outer scope, containing the
1214 function-try-block. */
1216 tree
1217 begin_function_try_block (tree *compound_stmt)
1219 tree r;
1220 /* This outer scope does not exist in the C++ standard, but we need
1221 a place to put __FUNCTION__ and similar variables. */
1222 *compound_stmt = begin_compound_stmt (0);
1223 r = begin_try_block ();
1224 FN_TRY_BLOCK_P (r) = 1;
1225 return r;
1228 /* Finish a try-block, which may be given by TRY_BLOCK. */
1230 void
1231 finish_try_block (tree try_block)
1233 TRY_STMTS (try_block) = pop_stmt_list (TRY_STMTS (try_block));
1234 TRY_HANDLERS (try_block) = push_stmt_list ();
1237 /* Finish the body of a cleanup try-block, which may be given by
1238 TRY_BLOCK. */
1240 void
1241 finish_cleanup_try_block (tree try_block)
1243 TRY_STMTS (try_block) = pop_stmt_list (TRY_STMTS (try_block));
1246 /* Finish an implicitly generated try-block, with a cleanup is given
1247 by CLEANUP. */
1249 void
1250 finish_cleanup (tree cleanup, tree try_block)
1252 TRY_HANDLERS (try_block) = cleanup;
1253 CLEANUP_P (try_block) = 1;
1256 /* Likewise, for a function-try-block. */
1258 void
1259 finish_function_try_block (tree try_block)
1261 finish_try_block (try_block);
1262 /* FIXME : something queer about CTOR_INITIALIZER somehow following
1263 the try block, but moving it inside. */
1264 in_function_try_handler = 1;
1267 /* Finish a handler-sequence for a try-block, which may be given by
1268 TRY_BLOCK. */
1270 void
1271 finish_handler_sequence (tree try_block)
1273 TRY_HANDLERS (try_block) = pop_stmt_list (TRY_HANDLERS (try_block));
1274 check_handlers (TRY_HANDLERS (try_block));
1277 /* Finish the handler-seq for a function-try-block, given by
1278 TRY_BLOCK. COMPOUND_STMT is the outer block created by
1279 begin_function_try_block. */
1281 void
1282 finish_function_handler_sequence (tree try_block, tree compound_stmt)
1284 in_function_try_handler = 0;
1285 finish_handler_sequence (try_block);
1286 finish_compound_stmt (compound_stmt);
1289 /* Begin a handler. Returns a HANDLER if appropriate. */
1291 tree
1292 begin_handler (void)
1294 tree r;
1296 r = build_stmt (input_location, HANDLER, NULL_TREE, NULL_TREE);
1297 add_stmt (r);
1299 /* Create a binding level for the eh_info and the exception object
1300 cleanup. */
1301 HANDLER_BODY (r) = do_pushlevel (sk_catch);
1303 return r;
1306 /* Finish the handler-parameters for a handler, which may be given by
1307 HANDLER. DECL is the declaration for the catch parameter, or NULL
1308 if this is a `catch (...)' clause. */
1310 void
1311 finish_handler_parms (tree decl, tree handler)
1313 tree type = NULL_TREE;
1314 if (processing_template_decl)
1316 if (decl)
1318 decl = pushdecl (decl);
1319 decl = push_template_decl (decl);
1320 HANDLER_PARMS (handler) = decl;
1321 type = TREE_TYPE (decl);
1324 else
1325 type = expand_start_catch_block (decl);
1326 HANDLER_TYPE (handler) = type;
1329 /* Finish a handler, which may be given by HANDLER. The BLOCKs are
1330 the return value from the matching call to finish_handler_parms. */
1332 void
1333 finish_handler (tree handler)
1335 if (!processing_template_decl)
1336 expand_end_catch_block ();
1337 HANDLER_BODY (handler) = do_poplevel (HANDLER_BODY (handler));
1340 /* Begin a compound statement. FLAGS contains some bits that control the
1341 behavior and context. If BCS_NO_SCOPE is set, the compound statement
1342 does not define a scope. If BCS_FN_BODY is set, this is the outermost
1343 block of a function. If BCS_TRY_BLOCK is set, this is the block
1344 created on behalf of a TRY statement. Returns a token to be passed to
1345 finish_compound_stmt. */
1347 tree
1348 begin_compound_stmt (unsigned int flags)
1350 tree r;
1352 if (flags & BCS_NO_SCOPE)
1354 r = push_stmt_list ();
1355 STATEMENT_LIST_NO_SCOPE (r) = 1;
1357 /* Normally, we try hard to keep the BLOCK for a statement-expression.
1358 But, if it's a statement-expression with a scopeless block, there's
1359 nothing to keep, and we don't want to accidentally keep a block
1360 *inside* the scopeless block. */
1361 keep_next_level (false);
1363 else
1365 scope_kind sk = sk_block;
1366 if (flags & BCS_TRY_BLOCK)
1367 sk = sk_try;
1368 else if (flags & BCS_TRANSACTION)
1369 sk = sk_transaction;
1370 r = do_pushlevel (sk);
1373 /* When processing a template, we need to remember where the braces were,
1374 so that we can set up identical scopes when instantiating the template
1375 later. BIND_EXPR is a handy candidate for this.
1376 Note that do_poplevel won't create a BIND_EXPR itself here (and thus
1377 result in nested BIND_EXPRs), since we don't build BLOCK nodes when
1378 processing templates. */
1379 if (processing_template_decl)
1381 r = build3 (BIND_EXPR, NULL, NULL, r, NULL);
1382 BIND_EXPR_TRY_BLOCK (r) = (flags & BCS_TRY_BLOCK) != 0;
1383 BIND_EXPR_BODY_BLOCK (r) = (flags & BCS_FN_BODY) != 0;
1384 TREE_SIDE_EFFECTS (r) = 1;
1387 return r;
1390 /* Finish a compound-statement, which is given by STMT. */
1392 void
1393 finish_compound_stmt (tree stmt)
1395 if (TREE_CODE (stmt) == BIND_EXPR)
1397 tree body = do_poplevel (BIND_EXPR_BODY (stmt));
1398 /* If the STATEMENT_LIST is empty and this BIND_EXPR isn't special,
1399 discard the BIND_EXPR so it can be merged with the containing
1400 STATEMENT_LIST. */
1401 if (TREE_CODE (body) == STATEMENT_LIST
1402 && STATEMENT_LIST_HEAD (body) == NULL
1403 && !BIND_EXPR_BODY_BLOCK (stmt)
1404 && !BIND_EXPR_TRY_BLOCK (stmt))
1405 stmt = body;
1406 else
1407 BIND_EXPR_BODY (stmt) = body;
1409 else if (STATEMENT_LIST_NO_SCOPE (stmt))
1410 stmt = pop_stmt_list (stmt);
1411 else
1413 /* Destroy any ObjC "super" receivers that may have been
1414 created. */
1415 objc_clear_super_receiver ();
1417 stmt = do_poplevel (stmt);
1420 /* ??? See c_end_compound_stmt wrt statement expressions. */
1421 add_stmt (stmt);
1424 /* Finish an asm-statement, whose components are a STRING, some
1425 OUTPUT_OPERANDS, some INPUT_OPERANDS, some CLOBBERS and some
1426 LABELS. Also note whether the asm-statement should be
1427 considered volatile. */
1429 tree
1430 finish_asm_stmt (int volatile_p, tree string, tree output_operands,
1431 tree input_operands, tree clobbers, tree labels)
1433 tree r;
1434 tree t;
1435 int ninputs = list_length (input_operands);
1436 int noutputs = list_length (output_operands);
1438 if (!processing_template_decl)
1440 const char *constraint;
1441 const char **oconstraints;
1442 bool allows_mem, allows_reg, is_inout;
1443 tree operand;
1444 int i;
1446 oconstraints = XALLOCAVEC (const char *, noutputs);
1448 string = resolve_asm_operand_names (string, output_operands,
1449 input_operands, labels);
1451 for (i = 0, t = output_operands; t; t = TREE_CHAIN (t), ++i)
1453 operand = TREE_VALUE (t);
1455 /* ??? Really, this should not be here. Users should be using a
1456 proper lvalue, dammit. But there's a long history of using
1457 casts in the output operands. In cases like longlong.h, this
1458 becomes a primitive form of typechecking -- if the cast can be
1459 removed, then the output operand had a type of the proper width;
1460 otherwise we'll get an error. Gross, but ... */
1461 STRIP_NOPS (operand);
1463 operand = mark_lvalue_use (operand);
1465 if (!lvalue_or_else (operand, lv_asm, tf_warning_or_error))
1466 operand = error_mark_node;
1468 if (operand != error_mark_node
1469 && (TREE_READONLY (operand)
1470 || CP_TYPE_CONST_P (TREE_TYPE (operand))
1471 /* Functions are not modifiable, even though they are
1472 lvalues. */
1473 || TREE_CODE (TREE_TYPE (operand)) == FUNCTION_TYPE
1474 || TREE_CODE (TREE_TYPE (operand)) == METHOD_TYPE
1475 /* If it's an aggregate and any field is const, then it is
1476 effectively const. */
1477 || (CLASS_TYPE_P (TREE_TYPE (operand))
1478 && C_TYPE_FIELDS_READONLY (TREE_TYPE (operand)))))
1479 cxx_readonly_error (operand, lv_asm);
1481 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
1482 oconstraints[i] = constraint;
1484 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
1485 &allows_mem, &allows_reg, &is_inout))
1487 /* If the operand is going to end up in memory,
1488 mark it addressable. */
1489 if (!allows_reg && !cxx_mark_addressable (operand))
1490 operand = error_mark_node;
1492 else
1493 operand = error_mark_node;
1495 TREE_VALUE (t) = operand;
1498 for (i = 0, t = input_operands; t; ++i, t = TREE_CHAIN (t))
1500 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
1501 bool constraint_parsed
1502 = parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
1503 oconstraints, &allows_mem, &allows_reg);
1504 /* If the operand is going to end up in memory, don't call
1505 decay_conversion. */
1506 if (constraint_parsed && !allows_reg && allows_mem)
1507 operand = mark_lvalue_use (TREE_VALUE (t));
1508 else
1509 operand = decay_conversion (TREE_VALUE (t), tf_warning_or_error);
1511 /* If the type of the operand hasn't been determined (e.g.,
1512 because it involves an overloaded function), then issue
1513 an error message. There's no context available to
1514 resolve the overloading. */
1515 if (TREE_TYPE (operand) == unknown_type_node)
1517 error ("type of asm operand %qE could not be determined",
1518 TREE_VALUE (t));
1519 operand = error_mark_node;
1522 if (constraint_parsed)
1524 /* If the operand is going to end up in memory,
1525 mark it addressable. */
1526 if (!allows_reg && allows_mem)
1528 /* Strip the nops as we allow this case. FIXME, this really
1529 should be rejected or made deprecated. */
1530 STRIP_NOPS (operand);
1531 if (!cxx_mark_addressable (operand))
1532 operand = error_mark_node;
1534 else if (!allows_reg && !allows_mem)
1536 /* If constraint allows neither register nor memory,
1537 try harder to get a constant. */
1538 tree constop = maybe_constant_value (operand);
1539 if (TREE_CONSTANT (constop))
1540 operand = constop;
1543 else
1544 operand = error_mark_node;
1546 TREE_VALUE (t) = operand;
1550 r = build_stmt (input_location, ASM_EXPR, string,
1551 output_operands, input_operands,
1552 clobbers, labels);
1553 ASM_VOLATILE_P (r) = volatile_p || noutputs == 0;
1554 r = maybe_cleanup_point_expr_void (r);
1555 return add_stmt (r);
1558 /* Finish a label with the indicated NAME. Returns the new label. */
1560 tree
1561 finish_label_stmt (tree name)
1563 tree decl = define_label (input_location, name);
1565 if (decl == error_mark_node)
1566 return error_mark_node;
1568 add_stmt (build_stmt (input_location, LABEL_EXPR, decl));
1570 return decl;
1573 /* Finish a series of declarations for local labels. G++ allows users
1574 to declare "local" labels, i.e., labels with scope. This extension
1575 is useful when writing code involving statement-expressions. */
1577 void
1578 finish_label_decl (tree name)
1580 if (!at_function_scope_p ())
1582 error ("__label__ declarations are only allowed in function scopes");
1583 return;
1586 add_decl_expr (declare_local_label (name));
1589 /* When DECL goes out of scope, make sure that CLEANUP is executed. */
1591 void
1592 finish_decl_cleanup (tree decl, tree cleanup)
1594 push_cleanup (decl, cleanup, false);
1597 /* If the current scope exits with an exception, run CLEANUP. */
1599 void
1600 finish_eh_cleanup (tree cleanup)
1602 push_cleanup (NULL, cleanup, true);
1605 /* The MEM_INITS is a list of mem-initializers, in reverse of the
1606 order they were written by the user. Each node is as for
1607 emit_mem_initializers. */
1609 void
1610 finish_mem_initializers (tree mem_inits)
1612 /* Reorder the MEM_INITS so that they are in the order they appeared
1613 in the source program. */
1614 mem_inits = nreverse (mem_inits);
1616 if (processing_template_decl)
1618 tree mem;
1620 for (mem = mem_inits; mem; mem = TREE_CHAIN (mem))
1622 /* If the TREE_PURPOSE is a TYPE_PACK_EXPANSION, skip the
1623 check for bare parameter packs in the TREE_VALUE, because
1624 any parameter packs in the TREE_VALUE have already been
1625 bound as part of the TREE_PURPOSE. See
1626 make_pack_expansion for more information. */
1627 if (TREE_CODE (TREE_PURPOSE (mem)) != TYPE_PACK_EXPANSION
1628 && check_for_bare_parameter_packs (TREE_VALUE (mem)))
1629 TREE_VALUE (mem) = error_mark_node;
1632 add_stmt (build_min_nt_loc (UNKNOWN_LOCATION,
1633 CTOR_INITIALIZER, mem_inits));
1635 else
1636 emit_mem_initializers (mem_inits);
1639 /* Obfuscate EXPR if it looks like an id-expression or member access so
1640 that the call to finish_decltype in do_auto_deduction will give the
1641 right result. */
1643 tree
1644 force_paren_expr (tree expr)
1646 /* This is only needed for decltype(auto) in C++14. */
1647 if (cxx_dialect < cxx14)
1648 return expr;
1650 /* If we're in unevaluated context, we can't be deducing a
1651 return/initializer type, so we don't need to mess with this. */
1652 if (cp_unevaluated_operand)
1653 return expr;
1655 if (!DECL_P (expr) && TREE_CODE (expr) != COMPONENT_REF
1656 && TREE_CODE (expr) != SCOPE_REF)
1657 return expr;
1659 if (TREE_CODE (expr) == COMPONENT_REF)
1660 REF_PARENTHESIZED_P (expr) = true;
1661 else if (type_dependent_expression_p (expr))
1662 expr = build1 (PAREN_EXPR, TREE_TYPE (expr), expr);
1663 else
1665 cp_lvalue_kind kind = lvalue_kind (expr);
1666 if ((kind & ~clk_class) != clk_none)
1668 tree type = unlowered_expr_type (expr);
1669 bool rval = !!(kind & clk_rvalueref);
1670 type = cp_build_reference_type (type, rval);
1671 /* This inhibits warnings in, eg, cxx_mark_addressable
1672 (c++/60955). */
1673 warning_sentinel s (extra_warnings);
1674 expr = build_static_cast (type, expr, tf_error);
1675 if (expr != error_mark_node)
1676 REF_PARENTHESIZED_P (expr) = true;
1680 return expr;
1683 /* Finish a parenthesized expression EXPR. */
1685 tree
1686 finish_parenthesized_expr (tree expr)
1688 if (EXPR_P (expr))
1689 /* This inhibits warnings in c_common_truthvalue_conversion. */
1690 TREE_NO_WARNING (expr) = 1;
1692 if (TREE_CODE (expr) == OFFSET_REF
1693 || TREE_CODE (expr) == SCOPE_REF)
1694 /* [expr.unary.op]/3 The qualified id of a pointer-to-member must not be
1695 enclosed in parentheses. */
1696 PTRMEM_OK_P (expr) = 0;
1698 if (TREE_CODE (expr) == STRING_CST)
1699 PAREN_STRING_LITERAL_P (expr) = 1;
1701 expr = force_paren_expr (expr);
1703 return expr;
1706 /* Finish a reference to a non-static data member (DECL) that is not
1707 preceded by `.' or `->'. */
1709 tree
1710 finish_non_static_data_member (tree decl, tree object, tree qualifying_scope)
1712 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
1713 bool try_omp_private = !object && omp_private_member_map;
1714 tree ret;
1716 if (!object)
1718 tree scope = qualifying_scope;
1719 if (scope == NULL_TREE)
1720 scope = context_for_name_lookup (decl);
1721 object = maybe_dummy_object (scope, NULL);
1724 object = maybe_resolve_dummy (object, true);
1725 if (object == error_mark_node)
1726 return error_mark_node;
1728 /* DR 613/850: Can use non-static data members without an associated
1729 object in sizeof/decltype/alignof. */
1730 if (is_dummy_object (object) && cp_unevaluated_operand == 0
1731 && (!processing_template_decl || !current_class_ref))
1733 if (current_function_decl
1734 && DECL_STATIC_FUNCTION_P (current_function_decl))
1735 error ("invalid use of member %qD in static member function", decl);
1736 else
1737 error ("invalid use of non-static data member %qD", decl);
1738 inform (DECL_SOURCE_LOCATION (decl), "declared here");
1740 return error_mark_node;
1743 if (current_class_ptr)
1744 TREE_USED (current_class_ptr) = 1;
1745 if (processing_template_decl && !qualifying_scope)
1747 tree type = TREE_TYPE (decl);
1749 if (TREE_CODE (type) == REFERENCE_TYPE)
1750 /* Quals on the object don't matter. */;
1751 else if (PACK_EXPANSION_P (type))
1752 /* Don't bother trying to represent this. */
1753 type = NULL_TREE;
1754 else
1756 /* Set the cv qualifiers. */
1757 int quals = cp_type_quals (TREE_TYPE (object));
1759 if (DECL_MUTABLE_P (decl))
1760 quals &= ~TYPE_QUAL_CONST;
1762 quals |= cp_type_quals (TREE_TYPE (decl));
1763 type = cp_build_qualified_type (type, quals);
1766 ret = (convert_from_reference
1767 (build_min (COMPONENT_REF, type, object, decl, NULL_TREE)));
1769 /* If PROCESSING_TEMPLATE_DECL is nonzero here, then
1770 QUALIFYING_SCOPE is also non-null. Wrap this in a SCOPE_REF
1771 for now. */
1772 else if (processing_template_decl)
1773 ret = build_qualified_name (TREE_TYPE (decl),
1774 qualifying_scope,
1775 decl,
1776 /*template_p=*/false);
1777 else
1779 tree access_type = TREE_TYPE (object);
1781 perform_or_defer_access_check (TYPE_BINFO (access_type), decl,
1782 decl, tf_warning_or_error);
1784 /* If the data member was named `C::M', convert `*this' to `C'
1785 first. */
1786 if (qualifying_scope)
1788 tree binfo = NULL_TREE;
1789 object = build_scoped_ref (object, qualifying_scope,
1790 &binfo);
1793 ret = build_class_member_access_expr (object, decl,
1794 /*access_path=*/NULL_TREE,
1795 /*preserve_reference=*/false,
1796 tf_warning_or_error);
1798 if (try_omp_private)
1800 tree *v = omp_private_member_map->get (decl);
1801 if (v)
1802 ret = convert_from_reference (*v);
1804 return ret;
1807 /* If we are currently parsing a template and we encountered a typedef
1808 TYPEDEF_DECL that is being accessed though CONTEXT, this function
1809 adds the typedef to a list tied to the current template.
1810 At template instantiation time, that list is walked and access check
1811 performed for each typedef.
1812 LOCATION is the location of the usage point of TYPEDEF_DECL. */
1814 void
1815 add_typedef_to_current_template_for_access_check (tree typedef_decl,
1816 tree context,
1817 location_t location)
1819 tree template_info = NULL;
1820 tree cs = current_scope ();
1822 if (!is_typedef_decl (typedef_decl)
1823 || !context
1824 || !CLASS_TYPE_P (context)
1825 || !cs)
1826 return;
1828 if (CLASS_TYPE_P (cs) || TREE_CODE (cs) == FUNCTION_DECL)
1829 template_info = get_template_info (cs);
1831 if (template_info
1832 && TI_TEMPLATE (template_info)
1833 && !currently_open_class (context))
1834 append_type_to_template_for_access_check (cs, typedef_decl,
1835 context, location);
1838 /* DECL was the declaration to which a qualified-id resolved. Issue
1839 an error message if it is not accessible. If OBJECT_TYPE is
1840 non-NULL, we have just seen `x->' or `x.' and OBJECT_TYPE is the
1841 type of `*x', or `x', respectively. If the DECL was named as
1842 `A::B' then NESTED_NAME_SPECIFIER is `A'. */
1844 void
1845 check_accessibility_of_qualified_id (tree decl,
1846 tree object_type,
1847 tree nested_name_specifier)
1849 tree scope;
1850 tree qualifying_type = NULL_TREE;
1852 /* If we are parsing a template declaration and if decl is a typedef,
1853 add it to a list tied to the template.
1854 At template instantiation time, that list will be walked and
1855 access check performed. */
1856 add_typedef_to_current_template_for_access_check (decl,
1857 nested_name_specifier
1858 ? nested_name_specifier
1859 : DECL_CONTEXT (decl),
1860 input_location);
1862 /* If we're not checking, return immediately. */
1863 if (deferred_access_no_check)
1864 return;
1866 /* Determine the SCOPE of DECL. */
1867 scope = context_for_name_lookup (decl);
1868 /* If the SCOPE is not a type, then DECL is not a member. */
1869 if (!TYPE_P (scope))
1870 return;
1871 /* Compute the scope through which DECL is being accessed. */
1872 if (object_type
1873 /* OBJECT_TYPE might not be a class type; consider:
1875 class A { typedef int I; };
1876 I *p;
1877 p->A::I::~I();
1879 In this case, we will have "A::I" as the DECL, but "I" as the
1880 OBJECT_TYPE. */
1881 && CLASS_TYPE_P (object_type)
1882 && DERIVED_FROM_P (scope, object_type))
1883 /* If we are processing a `->' or `.' expression, use the type of the
1884 left-hand side. */
1885 qualifying_type = object_type;
1886 else if (nested_name_specifier)
1888 /* If the reference is to a non-static member of the
1889 current class, treat it as if it were referenced through
1890 `this'. */
1891 tree ct;
1892 if (DECL_NONSTATIC_MEMBER_P (decl)
1893 && current_class_ptr
1894 && DERIVED_FROM_P (scope, ct = current_nonlambda_class_type ()))
1895 qualifying_type = ct;
1896 /* Otherwise, use the type indicated by the
1897 nested-name-specifier. */
1898 else
1899 qualifying_type = nested_name_specifier;
1901 else
1902 /* Otherwise, the name must be from the current class or one of
1903 its bases. */
1904 qualifying_type = currently_open_derived_class (scope);
1906 if (qualifying_type
1907 /* It is possible for qualifying type to be a TEMPLATE_TYPE_PARM
1908 or similar in a default argument value. */
1909 && CLASS_TYPE_P (qualifying_type)
1910 && !dependent_type_p (qualifying_type))
1911 perform_or_defer_access_check (TYPE_BINFO (qualifying_type), decl,
1912 decl, tf_warning_or_error);
1915 /* EXPR is the result of a qualified-id. The QUALIFYING_CLASS was the
1916 class named to the left of the "::" operator. DONE is true if this
1917 expression is a complete postfix-expression; it is false if this
1918 expression is followed by '->', '[', '(', etc. ADDRESS_P is true
1919 iff this expression is the operand of '&'. TEMPLATE_P is true iff
1920 the qualified-id was of the form "A::template B". TEMPLATE_ARG_P
1921 is true iff this qualified name appears as a template argument. */
1923 tree
1924 finish_qualified_id_expr (tree qualifying_class,
1925 tree expr,
1926 bool done,
1927 bool address_p,
1928 bool template_p,
1929 bool template_arg_p,
1930 tsubst_flags_t complain)
1932 gcc_assert (TYPE_P (qualifying_class));
1934 if (error_operand_p (expr))
1935 return error_mark_node;
1937 if ((DECL_P (expr) || BASELINK_P (expr))
1938 && !mark_used (expr, complain))
1939 return error_mark_node;
1941 if (template_p)
1942 check_template_keyword (expr);
1944 /* If EXPR occurs as the operand of '&', use special handling that
1945 permits a pointer-to-member. */
1946 if (address_p && done)
1948 if (TREE_CODE (expr) == SCOPE_REF)
1949 expr = TREE_OPERAND (expr, 1);
1950 expr = build_offset_ref (qualifying_class, expr,
1951 /*address_p=*/true, complain);
1952 return expr;
1955 /* No need to check access within an enum. */
1956 if (TREE_CODE (qualifying_class) == ENUMERAL_TYPE)
1957 return expr;
1959 /* Within the scope of a class, turn references to non-static
1960 members into expression of the form "this->...". */
1961 if (template_arg_p)
1962 /* But, within a template argument, we do not want make the
1963 transformation, as there is no "this" pointer. */
1965 else if (TREE_CODE (expr) == FIELD_DECL)
1967 push_deferring_access_checks (dk_no_check);
1968 expr = finish_non_static_data_member (expr, NULL_TREE,
1969 qualifying_class);
1970 pop_deferring_access_checks ();
1972 else if (BASELINK_P (expr) && !processing_template_decl)
1974 /* See if any of the functions are non-static members. */
1975 /* If so, the expression may be relative to 'this'. */
1976 if (!shared_member_p (expr)
1977 && current_class_ptr
1978 && DERIVED_FROM_P (qualifying_class,
1979 current_nonlambda_class_type ()))
1980 expr = (build_class_member_access_expr
1981 (maybe_dummy_object (qualifying_class, NULL),
1982 expr,
1983 BASELINK_ACCESS_BINFO (expr),
1984 /*preserve_reference=*/false,
1985 complain));
1986 else if (done)
1987 /* The expression is a qualified name whose address is not
1988 being taken. */
1989 expr = build_offset_ref (qualifying_class, expr, /*address_p=*/false,
1990 complain);
1992 else if (BASELINK_P (expr))
1994 else
1996 /* In a template, return a SCOPE_REF for most qualified-ids
1997 so that we can check access at instantiation time. But if
1998 we're looking at a member of the current instantiation, we
1999 know we have access and building up the SCOPE_REF confuses
2000 non-type template argument handling. */
2001 if (processing_template_decl
2002 && !currently_open_class (qualifying_class))
2003 expr = build_qualified_name (TREE_TYPE (expr),
2004 qualifying_class, expr,
2005 template_p);
2007 expr = convert_from_reference (expr);
2010 return expr;
2013 /* Begin a statement-expression. The value returned must be passed to
2014 finish_stmt_expr. */
2016 tree
2017 begin_stmt_expr (void)
2019 return push_stmt_list ();
2022 /* Process the final expression of a statement expression. EXPR can be
2023 NULL, if the final expression is empty. Return a STATEMENT_LIST
2024 containing all the statements in the statement-expression, or
2025 ERROR_MARK_NODE if there was an error. */
2027 tree
2028 finish_stmt_expr_expr (tree expr, tree stmt_expr)
2030 if (error_operand_p (expr))
2032 /* The type of the statement-expression is the type of the last
2033 expression. */
2034 TREE_TYPE (stmt_expr) = error_mark_node;
2035 return error_mark_node;
2038 /* If the last statement does not have "void" type, then the value
2039 of the last statement is the value of the entire expression. */
2040 if (expr)
2042 tree type = TREE_TYPE (expr);
2044 if (processing_template_decl)
2046 expr = build_stmt (input_location, EXPR_STMT, expr);
2047 expr = add_stmt (expr);
2048 /* Mark the last statement so that we can recognize it as such at
2049 template-instantiation time. */
2050 EXPR_STMT_STMT_EXPR_RESULT (expr) = 1;
2052 else if (VOID_TYPE_P (type))
2054 /* Just treat this like an ordinary statement. */
2055 expr = finish_expr_stmt (expr);
2057 else
2059 /* It actually has a value we need to deal with. First, force it
2060 to be an rvalue so that we won't need to build up a copy
2061 constructor call later when we try to assign it to something. */
2062 expr = force_rvalue (expr, tf_warning_or_error);
2063 if (error_operand_p (expr))
2064 return error_mark_node;
2066 /* Update for array-to-pointer decay. */
2067 type = TREE_TYPE (expr);
2069 /* Wrap it in a CLEANUP_POINT_EXPR and add it to the list like a
2070 normal statement, but don't convert to void or actually add
2071 the EXPR_STMT. */
2072 if (TREE_CODE (expr) != CLEANUP_POINT_EXPR)
2073 expr = maybe_cleanup_point_expr (expr);
2074 add_stmt (expr);
2077 /* The type of the statement-expression is the type of the last
2078 expression. */
2079 TREE_TYPE (stmt_expr) = type;
2082 return stmt_expr;
2085 /* Finish a statement-expression. EXPR should be the value returned
2086 by the previous begin_stmt_expr. Returns an expression
2087 representing the statement-expression. */
2089 tree
2090 finish_stmt_expr (tree stmt_expr, bool has_no_scope)
2092 tree type;
2093 tree result;
2095 if (error_operand_p (stmt_expr))
2097 pop_stmt_list (stmt_expr);
2098 return error_mark_node;
2101 gcc_assert (TREE_CODE (stmt_expr) == STATEMENT_LIST);
2103 type = TREE_TYPE (stmt_expr);
2104 result = pop_stmt_list (stmt_expr);
2105 TREE_TYPE (result) = type;
2107 if (processing_template_decl)
2109 result = build_min (STMT_EXPR, type, result);
2110 TREE_SIDE_EFFECTS (result) = 1;
2111 STMT_EXPR_NO_SCOPE (result) = has_no_scope;
2113 else if (CLASS_TYPE_P (type))
2115 /* Wrap the statement-expression in a TARGET_EXPR so that the
2116 temporary object created by the final expression is destroyed at
2117 the end of the full-expression containing the
2118 statement-expression. */
2119 result = force_target_expr (type, result, tf_warning_or_error);
2122 return result;
2125 /* Returns the expression which provides the value of STMT_EXPR. */
2127 tree
2128 stmt_expr_value_expr (tree stmt_expr)
2130 tree t = STMT_EXPR_STMT (stmt_expr);
2132 if (TREE_CODE (t) == BIND_EXPR)
2133 t = BIND_EXPR_BODY (t);
2135 if (TREE_CODE (t) == STATEMENT_LIST && STATEMENT_LIST_TAIL (t))
2136 t = STATEMENT_LIST_TAIL (t)->stmt;
2138 if (TREE_CODE (t) == EXPR_STMT)
2139 t = EXPR_STMT_EXPR (t);
2141 return t;
2144 /* Return TRUE iff EXPR_STMT is an empty list of
2145 expression statements. */
2147 bool
2148 empty_expr_stmt_p (tree expr_stmt)
2150 tree body = NULL_TREE;
2152 if (expr_stmt == void_node)
2153 return true;
2155 if (expr_stmt)
2157 if (TREE_CODE (expr_stmt) == EXPR_STMT)
2158 body = EXPR_STMT_EXPR (expr_stmt);
2159 else if (TREE_CODE (expr_stmt) == STATEMENT_LIST)
2160 body = expr_stmt;
2163 if (body)
2165 if (TREE_CODE (body) == STATEMENT_LIST)
2166 return tsi_end_p (tsi_start (body));
2167 else
2168 return empty_expr_stmt_p (body);
2170 return false;
2173 /* Perform Koenig lookup. FN is the postfix-expression representing
2174 the function (or functions) to call; ARGS are the arguments to the
2175 call. Returns the functions to be considered by overload resolution. */
2177 tree
2178 perform_koenig_lookup (tree fn, vec<tree, va_gc> *args,
2179 tsubst_flags_t complain)
2181 tree identifier = NULL_TREE;
2182 tree functions = NULL_TREE;
2183 tree tmpl_args = NULL_TREE;
2184 bool template_id = false;
2186 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
2188 /* Use a separate flag to handle null args. */
2189 template_id = true;
2190 tmpl_args = TREE_OPERAND (fn, 1);
2191 fn = TREE_OPERAND (fn, 0);
2194 /* Find the name of the overloaded function. */
2195 if (identifier_p (fn))
2196 identifier = fn;
2197 else if (is_overloaded_fn (fn))
2199 functions = fn;
2200 identifier = DECL_NAME (get_first_fn (functions));
2202 else if (DECL_P (fn))
2204 functions = fn;
2205 identifier = DECL_NAME (fn);
2208 /* A call to a namespace-scope function using an unqualified name.
2210 Do Koenig lookup -- unless any of the arguments are
2211 type-dependent. */
2212 if (!any_type_dependent_arguments_p (args)
2213 && !any_dependent_template_arguments_p (tmpl_args))
2215 fn = lookup_arg_dependent (identifier, functions, args);
2216 if (!fn)
2218 /* The unqualified name could not be resolved. */
2219 if (complain)
2220 fn = unqualified_fn_lookup_error (identifier);
2221 else
2222 fn = identifier;
2226 if (fn && template_id)
2227 fn = build2 (TEMPLATE_ID_EXPR, unknown_type_node, fn, tmpl_args);
2229 return fn;
2232 /* Generate an expression for `FN (ARGS)'. This may change the
2233 contents of ARGS.
2235 If DISALLOW_VIRTUAL is true, the call to FN will be not generated
2236 as a virtual call, even if FN is virtual. (This flag is set when
2237 encountering an expression where the function name is explicitly
2238 qualified. For example a call to `X::f' never generates a virtual
2239 call.)
2241 Returns code for the call. */
2243 tree
2244 finish_call_expr (tree fn, vec<tree, va_gc> **args, bool disallow_virtual,
2245 bool koenig_p, tsubst_flags_t complain)
2247 tree result;
2248 tree orig_fn;
2249 vec<tree, va_gc> *orig_args = NULL;
2251 if (fn == error_mark_node)
2252 return error_mark_node;
2254 gcc_assert (!TYPE_P (fn));
2256 orig_fn = fn;
2258 if (processing_template_decl)
2260 /* If the call expression is dependent, build a CALL_EXPR node
2261 with no type; type_dependent_expression_p recognizes
2262 expressions with no type as being dependent. */
2263 if (type_dependent_expression_p (fn)
2264 || any_type_dependent_arguments_p (*args)
2265 /* For a non-static member function that doesn't have an
2266 explicit object argument, we need to specifically
2267 test the type dependency of the "this" pointer because it
2268 is not included in *ARGS even though it is considered to
2269 be part of the list of arguments. Note that this is
2270 related to CWG issues 515 and 1005. */
2271 || (TREE_CODE (fn) != COMPONENT_REF
2272 && non_static_member_function_p (fn)
2273 && current_class_ref
2274 && type_dependent_expression_p (current_class_ref)))
2276 result = build_nt_call_vec (fn, *args);
2277 SET_EXPR_LOCATION (result, EXPR_LOC_OR_LOC (fn, input_location));
2278 KOENIG_LOOKUP_P (result) = koenig_p;
2279 if (cfun)
2283 tree fndecl = OVL_CURRENT (fn);
2284 if (TREE_CODE (fndecl) != FUNCTION_DECL
2285 || !TREE_THIS_VOLATILE (fndecl))
2286 break;
2287 fn = OVL_NEXT (fn);
2289 while (fn);
2290 if (!fn)
2291 current_function_returns_abnormally = 1;
2293 return result;
2295 orig_args = make_tree_vector_copy (*args);
2296 if (!BASELINK_P (fn)
2297 && TREE_CODE (fn) != PSEUDO_DTOR_EXPR
2298 && TREE_TYPE (fn) != unknown_type_node)
2299 fn = build_non_dependent_expr (fn);
2300 make_args_non_dependent (*args);
2303 if (TREE_CODE (fn) == COMPONENT_REF)
2305 tree member = TREE_OPERAND (fn, 1);
2306 if (BASELINK_P (member))
2308 tree object = TREE_OPERAND (fn, 0);
2309 return build_new_method_call (object, member,
2310 args, NULL_TREE,
2311 (disallow_virtual
2312 ? LOOKUP_NORMAL | LOOKUP_NONVIRTUAL
2313 : LOOKUP_NORMAL),
2314 /*fn_p=*/NULL,
2315 complain);
2319 /* Per 13.3.1.1, '(&f)(...)' is the same as '(f)(...)'. */
2320 if (TREE_CODE (fn) == ADDR_EXPR
2321 && TREE_CODE (TREE_OPERAND (fn, 0)) == OVERLOAD)
2322 fn = TREE_OPERAND (fn, 0);
2324 if (is_overloaded_fn (fn))
2325 fn = baselink_for_fns (fn);
2327 result = NULL_TREE;
2328 if (BASELINK_P (fn))
2330 tree object;
2332 /* A call to a member function. From [over.call.func]:
2334 If the keyword this is in scope and refers to the class of
2335 that member function, or a derived class thereof, then the
2336 function call is transformed into a qualified function call
2337 using (*this) as the postfix-expression to the left of the
2338 . operator.... [Otherwise] a contrived object of type T
2339 becomes the implied object argument.
2341 In this situation:
2343 struct A { void f(); };
2344 struct B : public A {};
2345 struct C : public A { void g() { B::f(); }};
2347 "the class of that member function" refers to `A'. But 11.2
2348 [class.access.base] says that we need to convert 'this' to B* as
2349 part of the access, so we pass 'B' to maybe_dummy_object. */
2351 object = maybe_dummy_object (BINFO_TYPE (BASELINK_ACCESS_BINFO (fn)),
2352 NULL);
2354 if (processing_template_decl)
2356 if (type_dependent_expression_p (object))
2358 tree ret = build_nt_call_vec (orig_fn, orig_args);
2359 release_tree_vector (orig_args);
2360 return ret;
2362 object = build_non_dependent_expr (object);
2365 result = build_new_method_call (object, fn, args, NULL_TREE,
2366 (disallow_virtual
2367 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
2368 : LOOKUP_NORMAL),
2369 /*fn_p=*/NULL,
2370 complain);
2372 else if (is_overloaded_fn (fn))
2374 /* If the function is an overloaded builtin, resolve it. */
2375 if (TREE_CODE (fn) == FUNCTION_DECL
2376 && (DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL
2377 || DECL_BUILT_IN_CLASS (fn) == BUILT_IN_MD))
2378 result = resolve_overloaded_builtin (input_location, fn, *args);
2380 if (!result)
2382 if (warn_sizeof_pointer_memaccess
2383 && (complain & tf_warning)
2384 && !vec_safe_is_empty (*args)
2385 && !processing_template_decl)
2387 location_t sizeof_arg_loc[3];
2388 tree sizeof_arg[3];
2389 unsigned int i;
2390 for (i = 0; i < 3; i++)
2392 tree t;
2394 sizeof_arg_loc[i] = UNKNOWN_LOCATION;
2395 sizeof_arg[i] = NULL_TREE;
2396 if (i >= (*args)->length ())
2397 continue;
2398 t = (**args)[i];
2399 if (TREE_CODE (t) != SIZEOF_EXPR)
2400 continue;
2401 if (SIZEOF_EXPR_TYPE_P (t))
2402 sizeof_arg[i] = TREE_TYPE (TREE_OPERAND (t, 0));
2403 else
2404 sizeof_arg[i] = TREE_OPERAND (t, 0);
2405 sizeof_arg_loc[i] = EXPR_LOCATION (t);
2407 sizeof_pointer_memaccess_warning
2408 (sizeof_arg_loc, fn, *args,
2409 sizeof_arg, same_type_ignoring_top_level_qualifiers_p);
2412 /* A call to a namespace-scope function. */
2413 result = build_new_function_call (fn, args, koenig_p, complain);
2416 else if (TREE_CODE (fn) == PSEUDO_DTOR_EXPR)
2418 if (!vec_safe_is_empty (*args))
2419 error ("arguments to destructor are not allowed");
2420 /* Mark the pseudo-destructor call as having side-effects so
2421 that we do not issue warnings about its use. */
2422 result = build1 (NOP_EXPR,
2423 void_type_node,
2424 TREE_OPERAND (fn, 0));
2425 TREE_SIDE_EFFECTS (result) = 1;
2427 else if (CLASS_TYPE_P (TREE_TYPE (fn)))
2428 /* If the "function" is really an object of class type, it might
2429 have an overloaded `operator ()'. */
2430 result = build_op_call (fn, args, complain);
2432 if (!result)
2433 /* A call where the function is unknown. */
2434 result = cp_build_function_call_vec (fn, args, complain);
2436 if (processing_template_decl && result != error_mark_node)
2438 if (INDIRECT_REF_P (result))
2439 result = TREE_OPERAND (result, 0);
2440 result = build_call_vec (TREE_TYPE (result), orig_fn, orig_args);
2441 SET_EXPR_LOCATION (result, input_location);
2442 KOENIG_LOOKUP_P (result) = koenig_p;
2443 release_tree_vector (orig_args);
2444 result = convert_from_reference (result);
2447 if (koenig_p)
2449 /* Free garbage OVERLOADs from arg-dependent lookup. */
2450 tree next = NULL_TREE;
2451 for (fn = orig_fn;
2452 fn && TREE_CODE (fn) == OVERLOAD && OVL_ARG_DEPENDENT (fn);
2453 fn = next)
2455 if (processing_template_decl)
2456 /* In a template, we'll re-use them at instantiation time. */
2457 OVL_ARG_DEPENDENT (fn) = false;
2458 else
2460 next = OVL_CHAIN (fn);
2461 ggc_free (fn);
2466 return result;
2469 /* Finish a call to a postfix increment or decrement or EXPR. (Which
2470 is indicated by CODE, which should be POSTINCREMENT_EXPR or
2471 POSTDECREMENT_EXPR.) */
2473 tree
2474 finish_increment_expr (tree expr, enum tree_code code)
2476 return build_x_unary_op (input_location, code, expr, tf_warning_or_error);
2479 /* Finish a use of `this'. Returns an expression for `this'. */
2481 tree
2482 finish_this_expr (void)
2484 tree result = NULL_TREE;
2486 if (current_class_ptr)
2488 tree type = TREE_TYPE (current_class_ref);
2490 /* In a lambda expression, 'this' refers to the captured 'this'. */
2491 if (LAMBDA_TYPE_P (type))
2492 result = lambda_expr_this_capture (CLASSTYPE_LAMBDA_EXPR (type), true);
2493 else
2494 result = current_class_ptr;
2497 if (result)
2498 /* The keyword 'this' is a prvalue expression. */
2499 return rvalue (result);
2501 tree fn = current_nonlambda_function ();
2502 if (fn && DECL_STATIC_FUNCTION_P (fn))
2503 error ("%<this%> is unavailable for static member functions");
2504 else if (fn)
2505 error ("invalid use of %<this%> in non-member function");
2506 else
2507 error ("invalid use of %<this%> at top level");
2508 return error_mark_node;
2511 /* Finish a pseudo-destructor expression. If SCOPE is NULL, the
2512 expression was of the form `OBJECT.~DESTRUCTOR' where DESTRUCTOR is
2513 the TYPE for the type given. If SCOPE is non-NULL, the expression
2514 was of the form `OBJECT.SCOPE::~DESTRUCTOR'. */
2516 tree
2517 finish_pseudo_destructor_expr (tree object, tree scope, tree destructor,
2518 location_t loc)
2520 if (object == error_mark_node || destructor == error_mark_node)
2521 return error_mark_node;
2523 gcc_assert (TYPE_P (destructor));
2525 if (!processing_template_decl)
2527 if (scope == error_mark_node)
2529 error_at (loc, "invalid qualifying scope in pseudo-destructor name");
2530 return error_mark_node;
2532 if (is_auto (destructor))
2533 destructor = TREE_TYPE (object);
2534 if (scope && TYPE_P (scope) && !check_dtor_name (scope, destructor))
2536 error_at (loc,
2537 "qualified type %qT does not match destructor name ~%qT",
2538 scope, destructor);
2539 return error_mark_node;
2543 /* [expr.pseudo] says both:
2545 The type designated by the pseudo-destructor-name shall be
2546 the same as the object type.
2548 and:
2550 The cv-unqualified versions of the object type and of the
2551 type designated by the pseudo-destructor-name shall be the
2552 same type.
2554 We implement the more generous second sentence, since that is
2555 what most other compilers do. */
2556 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (object),
2557 destructor))
2559 error_at (loc, "%qE is not of type %qT", object, destructor);
2560 return error_mark_node;
2564 return build3_loc (loc, PSEUDO_DTOR_EXPR, void_type_node, object,
2565 scope, destructor);
2568 /* Finish an expression of the form CODE EXPR. */
2570 tree
2571 finish_unary_op_expr (location_t loc, enum tree_code code, tree expr,
2572 tsubst_flags_t complain)
2574 tree result = build_x_unary_op (loc, code, expr, complain);
2575 if ((complain & tf_warning)
2576 && TREE_OVERFLOW_P (result) && !TREE_OVERFLOW_P (expr))
2577 overflow_warning (input_location, result);
2579 return result;
2582 /* Finish a compound-literal expression. TYPE is the type to which
2583 the CONSTRUCTOR in COMPOUND_LITERAL is being cast. */
2585 tree
2586 finish_compound_literal (tree type, tree compound_literal,
2587 tsubst_flags_t complain)
2589 if (type == error_mark_node)
2590 return error_mark_node;
2592 if (TREE_CODE (type) == REFERENCE_TYPE)
2594 compound_literal
2595 = finish_compound_literal (TREE_TYPE (type), compound_literal,
2596 complain);
2597 return cp_build_c_cast (type, compound_literal, complain);
2600 if (!TYPE_OBJ_P (type))
2602 if (complain & tf_error)
2603 error ("compound literal of non-object type %qT", type);
2604 return error_mark_node;
2607 if (processing_template_decl)
2609 TREE_TYPE (compound_literal) = type;
2610 /* Mark the expression as a compound literal. */
2611 TREE_HAS_CONSTRUCTOR (compound_literal) = 1;
2612 return compound_literal;
2615 type = complete_type (type);
2617 if (TYPE_NON_AGGREGATE_CLASS (type))
2619 /* Trying to deal with a CONSTRUCTOR instead of a TREE_LIST
2620 everywhere that deals with function arguments would be a pain, so
2621 just wrap it in a TREE_LIST. The parser set a flag so we know
2622 that it came from T{} rather than T({}). */
2623 CONSTRUCTOR_IS_DIRECT_INIT (compound_literal) = 1;
2624 compound_literal = build_tree_list (NULL_TREE, compound_literal);
2625 return build_functional_cast (type, compound_literal, complain);
2628 if (TREE_CODE (type) == ARRAY_TYPE
2629 && check_array_initializer (NULL_TREE, type, compound_literal))
2630 return error_mark_node;
2631 compound_literal = reshape_init (type, compound_literal, complain);
2632 if (SCALAR_TYPE_P (type)
2633 && !BRACE_ENCLOSED_INITIALIZER_P (compound_literal)
2634 && !check_narrowing (type, compound_literal, complain))
2635 return error_mark_node;
2636 if (TREE_CODE (type) == ARRAY_TYPE
2637 && TYPE_DOMAIN (type) == NULL_TREE)
2639 cp_complete_array_type_or_error (&type, compound_literal,
2640 false, complain);
2641 if (type == error_mark_node)
2642 return error_mark_node;
2644 compound_literal = digest_init (type, compound_literal, complain);
2645 if (TREE_CODE (compound_literal) == CONSTRUCTOR)
2646 TREE_HAS_CONSTRUCTOR (compound_literal) = true;
2647 /* Put static/constant array temporaries in static variables, but always
2648 represent class temporaries with TARGET_EXPR so we elide copies. */
2649 if ((!at_function_scope_p () || CP_TYPE_CONST_P (type))
2650 && TREE_CODE (type) == ARRAY_TYPE
2651 && !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
2652 && initializer_constant_valid_p (compound_literal, type))
2654 tree decl = create_temporary_var (type);
2655 DECL_INITIAL (decl) = compound_literal;
2656 TREE_STATIC (decl) = 1;
2657 if (literal_type_p (type) && CP_TYPE_CONST_NON_VOLATILE_P (type))
2659 /* 5.19 says that a constant expression can include an
2660 lvalue-rvalue conversion applied to "a glvalue of literal type
2661 that refers to a non-volatile temporary object initialized
2662 with a constant expression". Rather than try to communicate
2663 that this VAR_DECL is a temporary, just mark it constexpr. */
2664 DECL_DECLARED_CONSTEXPR_P (decl) = true;
2665 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
2666 TREE_CONSTANT (decl) = true;
2668 cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
2669 decl = pushdecl_top_level (decl);
2670 DECL_NAME (decl) = make_anon_name ();
2671 SET_DECL_ASSEMBLER_NAME (decl, DECL_NAME (decl));
2672 /* Make sure the destructor is callable. */
2673 tree clean = cxx_maybe_build_cleanup (decl, complain);
2674 if (clean == error_mark_node)
2675 return error_mark_node;
2676 return decl;
2678 else
2679 return get_target_expr_sfinae (compound_literal, complain);
2682 /* Return the declaration for the function-name variable indicated by
2683 ID. */
2685 tree
2686 finish_fname (tree id)
2688 tree decl;
2690 decl = fname_decl (input_location, C_RID_CODE (id), id);
2691 if (processing_template_decl && current_function_decl
2692 && decl != error_mark_node)
2693 decl = DECL_NAME (decl);
2694 return decl;
2697 /* Finish a translation unit. */
2699 void
2700 finish_translation_unit (void)
2702 /* In case there were missing closebraces,
2703 get us back to the global binding level. */
2704 pop_everything ();
2705 while (current_namespace != global_namespace)
2706 pop_namespace ();
2708 /* Do file scope __FUNCTION__ et al. */
2709 finish_fname_decls ();
2712 /* Finish a template type parameter, specified as AGGR IDENTIFIER.
2713 Returns the parameter. */
2715 tree
2716 finish_template_type_parm (tree aggr, tree identifier)
2718 if (aggr != class_type_node)
2720 permerror (input_location, "template type parameters must use the keyword %<class%> or %<typename%>");
2721 aggr = class_type_node;
2724 return build_tree_list (aggr, identifier);
2727 /* Finish a template template parameter, specified as AGGR IDENTIFIER.
2728 Returns the parameter. */
2730 tree
2731 finish_template_template_parm (tree aggr, tree identifier)
2733 tree decl = build_decl (input_location,
2734 TYPE_DECL, identifier, NULL_TREE);
2736 tree tmpl = build_lang_decl (TEMPLATE_DECL, identifier, NULL_TREE);
2737 DECL_TEMPLATE_PARMS (tmpl) = current_template_parms;
2738 DECL_TEMPLATE_RESULT (tmpl) = decl;
2739 DECL_ARTIFICIAL (decl) = 1;
2741 // Associate the constraints with the underlying declaration,
2742 // not the template.
2743 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
2744 tree constr = build_constraints (reqs, NULL_TREE);
2745 set_constraints (decl, constr);
2747 end_template_decl ();
2749 gcc_assert (DECL_TEMPLATE_PARMS (tmpl));
2751 check_default_tmpl_args (decl, DECL_TEMPLATE_PARMS (tmpl),
2752 /*is_primary=*/true, /*is_partial=*/false,
2753 /*is_friend=*/0);
2755 return finish_template_type_parm (aggr, tmpl);
2758 /* ARGUMENT is the default-argument value for a template template
2759 parameter. If ARGUMENT is invalid, issue error messages and return
2760 the ERROR_MARK_NODE. Otherwise, ARGUMENT itself is returned. */
2762 tree
2763 check_template_template_default_arg (tree argument)
2765 if (TREE_CODE (argument) != TEMPLATE_DECL
2766 && TREE_CODE (argument) != TEMPLATE_TEMPLATE_PARM
2767 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
2769 if (TREE_CODE (argument) == TYPE_DECL)
2770 error ("invalid use of type %qT as a default value for a template "
2771 "template-parameter", TREE_TYPE (argument));
2772 else
2773 error ("invalid default argument for a template template parameter");
2774 return error_mark_node;
2777 return argument;
2780 /* Begin a class definition, as indicated by T. */
2782 tree
2783 begin_class_definition (tree t)
2785 if (error_operand_p (t) || error_operand_p (TYPE_MAIN_DECL (t)))
2786 return error_mark_node;
2788 if (processing_template_parmlist)
2790 error ("definition of %q#T inside template parameter list", t);
2791 return error_mark_node;
2794 /* According to the C++ ABI, decimal classes defined in ISO/IEC TR 24733
2795 are passed the same as decimal scalar types. */
2796 if (TREE_CODE (t) == RECORD_TYPE
2797 && !processing_template_decl)
2799 tree ns = TYPE_CONTEXT (t);
2800 if (ns && TREE_CODE (ns) == NAMESPACE_DECL
2801 && DECL_CONTEXT (ns) == std_node
2802 && DECL_NAME (ns)
2803 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (ns)), "decimal"))
2805 const char *n = TYPE_NAME_STRING (t);
2806 if ((strcmp (n, "decimal32") == 0)
2807 || (strcmp (n, "decimal64") == 0)
2808 || (strcmp (n, "decimal128") == 0))
2809 TYPE_TRANSPARENT_AGGR (t) = 1;
2813 /* A non-implicit typename comes from code like:
2815 template <typename T> struct A {
2816 template <typename U> struct A<T>::B ...
2818 This is erroneous. */
2819 else if (TREE_CODE (t) == TYPENAME_TYPE)
2821 error ("invalid definition of qualified type %qT", t);
2822 t = error_mark_node;
2825 if (t == error_mark_node || ! MAYBE_CLASS_TYPE_P (t))
2827 t = make_class_type (RECORD_TYPE);
2828 pushtag (make_anon_name (), t, /*tag_scope=*/ts_current);
2831 if (TYPE_BEING_DEFINED (t))
2833 t = make_class_type (TREE_CODE (t));
2834 pushtag (TYPE_IDENTIFIER (t), t, /*tag_scope=*/ts_current);
2836 maybe_process_partial_specialization (t);
2837 pushclass (t);
2838 TYPE_BEING_DEFINED (t) = 1;
2839 class_binding_level->defining_class_p = 1;
2841 if (flag_pack_struct)
2843 tree v;
2844 TYPE_PACKED (t) = 1;
2845 /* Even though the type is being defined for the first time
2846 here, there might have been a forward declaration, so there
2847 might be cv-qualified variants of T. */
2848 for (v = TYPE_NEXT_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v))
2849 TYPE_PACKED (v) = 1;
2851 /* Reset the interface data, at the earliest possible
2852 moment, as it might have been set via a class foo;
2853 before. */
2854 if (! TYPE_ANONYMOUS_P (t))
2856 struct c_fileinfo *finfo = \
2857 get_fileinfo (LOCATION_FILE (input_location));
2858 CLASSTYPE_INTERFACE_ONLY (t) = finfo->interface_only;
2859 SET_CLASSTYPE_INTERFACE_UNKNOWN_X
2860 (t, finfo->interface_unknown);
2862 reset_specialization();
2864 /* Make a declaration for this class in its own scope. */
2865 build_self_reference ();
2867 return t;
2870 /* Finish the member declaration given by DECL. */
2872 void
2873 finish_member_declaration (tree decl)
2875 if (decl == error_mark_node || decl == NULL_TREE)
2876 return;
2878 if (decl == void_type_node)
2879 /* The COMPONENT was a friend, not a member, and so there's
2880 nothing for us to do. */
2881 return;
2883 /* We should see only one DECL at a time. */
2884 gcc_assert (DECL_CHAIN (decl) == NULL_TREE);
2886 /* Set up access control for DECL. */
2887 TREE_PRIVATE (decl)
2888 = (current_access_specifier == access_private_node);
2889 TREE_PROTECTED (decl)
2890 = (current_access_specifier == access_protected_node);
2891 if (TREE_CODE (decl) == TEMPLATE_DECL)
2893 TREE_PRIVATE (DECL_TEMPLATE_RESULT (decl)) = TREE_PRIVATE (decl);
2894 TREE_PROTECTED (DECL_TEMPLATE_RESULT (decl)) = TREE_PROTECTED (decl);
2897 /* Mark the DECL as a member of the current class, unless it's
2898 a member of an enumeration. */
2899 if (TREE_CODE (decl) != CONST_DECL)
2900 DECL_CONTEXT (decl) = current_class_type;
2902 /* Check for bare parameter packs in the member variable declaration. */
2903 if (TREE_CODE (decl) == FIELD_DECL)
2905 if (check_for_bare_parameter_packs (TREE_TYPE (decl)))
2906 TREE_TYPE (decl) = error_mark_node;
2907 if (check_for_bare_parameter_packs (DECL_ATTRIBUTES (decl)))
2908 DECL_ATTRIBUTES (decl) = NULL_TREE;
2911 /* [dcl.link]
2913 A C language linkage is ignored for the names of class members
2914 and the member function type of class member functions. */
2915 if (DECL_LANG_SPECIFIC (decl) && DECL_LANGUAGE (decl) == lang_c)
2916 SET_DECL_LANGUAGE (decl, lang_cplusplus);
2918 /* Put functions on the TYPE_METHODS list and everything else on the
2919 TYPE_FIELDS list. Note that these are built up in reverse order.
2920 We reverse them (to obtain declaration order) in finish_struct. */
2921 if (DECL_DECLARES_FUNCTION_P (decl))
2923 /* We also need to add this function to the
2924 CLASSTYPE_METHOD_VEC. */
2925 if (add_method (current_class_type, decl, NULL_TREE))
2927 gcc_assert (TYPE_MAIN_VARIANT (current_class_type) == current_class_type);
2928 DECL_CHAIN (decl) = TYPE_METHODS (current_class_type);
2929 TYPE_METHODS (current_class_type) = decl;
2931 maybe_add_class_template_decl_list (current_class_type, decl,
2932 /*friend_p=*/0);
2935 /* Enter the DECL into the scope of the class, if the class
2936 isn't a closure (whose fields are supposed to be unnamed). */
2937 else if (CLASSTYPE_LAMBDA_EXPR (current_class_type)
2938 || pushdecl_class_level (decl))
2940 if (TREE_CODE (decl) == USING_DECL)
2942 /* For now, ignore class-scope USING_DECLS, so that
2943 debugging backends do not see them. */
2944 DECL_IGNORED_P (decl) = 1;
2947 /* All TYPE_DECLs go at the end of TYPE_FIELDS. Ordinary fields
2948 go at the beginning. The reason is that lookup_field_1
2949 searches the list in order, and we want a field name to
2950 override a type name so that the "struct stat hack" will
2951 work. In particular:
2953 struct S { enum E { }; int E } s;
2954 s.E = 3;
2956 is valid. In addition, the FIELD_DECLs must be maintained in
2957 declaration order so that class layout works as expected.
2958 However, we don't need that order until class layout, so we
2959 save a little time by putting FIELD_DECLs on in reverse order
2960 here, and then reversing them in finish_struct_1. (We could
2961 also keep a pointer to the correct insertion points in the
2962 list.) */
2964 if (TREE_CODE (decl) == TYPE_DECL)
2965 TYPE_FIELDS (current_class_type)
2966 = chainon (TYPE_FIELDS (current_class_type), decl);
2967 else
2969 DECL_CHAIN (decl) = TYPE_FIELDS (current_class_type);
2970 TYPE_FIELDS (current_class_type) = decl;
2973 maybe_add_class_template_decl_list (current_class_type, decl,
2974 /*friend_p=*/0);
2978 /* Finish processing a complete template declaration. The PARMS are
2979 the template parameters. */
2981 void
2982 finish_template_decl (tree parms)
2984 if (parms)
2985 end_template_decl ();
2986 else
2987 end_specialization ();
2990 // Returns the template type of the class scope being entered. If we're
2991 // entering a constrained class scope. TYPE is the class template
2992 // scope being entered and we may need to match the intended type with
2993 // a constrained specialization. For example:
2995 // template<Object T>
2996 // struct S { void f(); }; #1
2998 // template<Object T>
2999 // void S<T>::f() { } #2
3001 // We check, in #2, that S<T> refers precisely to the type declared by
3002 // #1 (i.e., that the constraints match). Note that the following should
3003 // be an error since there is no specialization of S<T> that is
3004 // unconstrained, but this is not diagnosed here.
3006 // template<typename T>
3007 // void S<T>::f() { }
3009 // We cannot diagnose this problem here since this function also matches
3010 // qualified template names that are not part of a definition. For example:
3012 // template<Integral T, Floating_point U>
3013 // typename pair<T, U>::first_type void f(T, U);
3015 // Here, it is unlikely that there is a partial specialization of
3016 // pair constrained for for Integral and Floating_point arguments.
3018 // The general rule is: if a constrained specialization with matching
3019 // constraints is found return that type. Also note that if TYPE is not a
3020 // class-type (e.g. a typename type), then no fixup is needed.
3022 static tree
3023 fixup_template_type (tree type)
3025 // Find the template parameter list at the a depth appropriate to
3026 // the scope we're trying to enter.
3027 tree parms = current_template_parms;
3028 int depth = template_class_depth (type);
3029 for (int n = processing_template_decl; n > depth && parms; --n)
3030 parms = TREE_CHAIN (parms);
3031 if (!parms)
3032 return type;
3033 tree cur_reqs = TEMPLATE_PARMS_CONSTRAINTS (parms);
3034 tree cur_constr = build_constraints (cur_reqs, NULL_TREE);
3036 // Search for a specialization whose type and constraints match.
3037 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
3038 tree specs = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
3039 while (specs)
3041 tree spec_constr = get_constraints (TREE_VALUE (specs));
3043 // If the type and constraints match a specialization, then we
3044 // are entering that type.
3045 if (same_type_p (type, TREE_TYPE (specs))
3046 && equivalent_constraints (cur_constr, spec_constr))
3047 return TREE_TYPE (specs);
3048 specs = TREE_CHAIN (specs);
3051 // If no specialization matches, then must return the type
3052 // previously found.
3053 return type;
3056 /* Finish processing a template-id (which names a type) of the form
3057 NAME < ARGS >. Return the TYPE_DECL for the type named by the
3058 template-id. If ENTERING_SCOPE is nonzero we are about to enter
3059 the scope of template-id indicated. */
3061 tree
3062 finish_template_type (tree name, tree args, int entering_scope)
3064 tree type;
3066 type = lookup_template_class (name, args,
3067 NULL_TREE, NULL_TREE, entering_scope,
3068 tf_warning_or_error | tf_user);
3070 /* If we might be entering the scope of a partial specialization,
3071 find the one with the right constraints. */
3072 if (flag_concepts
3073 && entering_scope
3074 && CLASS_TYPE_P (type)
3075 && dependent_type_p (type)
3076 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
3077 type = fixup_template_type (type);
3079 if (type == error_mark_node)
3080 return type;
3081 else if (CLASS_TYPE_P (type) && !alias_type_or_template_p (type))
3082 return TYPE_STUB_DECL (type);
3083 else
3084 return TYPE_NAME (type);
3087 /* Finish processing a BASE_CLASS with the indicated ACCESS_SPECIFIER.
3088 Return a TREE_LIST containing the ACCESS_SPECIFIER and the
3089 BASE_CLASS, or NULL_TREE if an error occurred. The
3090 ACCESS_SPECIFIER is one of
3091 access_{default,public,protected_private}_node. For a virtual base
3092 we set TREE_TYPE. */
3094 tree
3095 finish_base_specifier (tree base, tree access, bool virtual_p)
3097 tree result;
3099 if (base == error_mark_node)
3101 error ("invalid base-class specification");
3102 result = NULL_TREE;
3104 else if (! MAYBE_CLASS_TYPE_P (base))
3106 error ("%qT is not a class type", base);
3107 result = NULL_TREE;
3109 else
3111 if (cp_type_quals (base) != 0)
3113 /* DR 484: Can a base-specifier name a cv-qualified
3114 class type? */
3115 base = TYPE_MAIN_VARIANT (base);
3117 result = build_tree_list (access, base);
3118 if (virtual_p)
3119 TREE_TYPE (result) = integer_type_node;
3122 return result;
3125 /* If FNS is a member function, a set of member functions, or a
3126 template-id referring to one or more member functions, return a
3127 BASELINK for FNS, incorporating the current access context.
3128 Otherwise, return FNS unchanged. */
3130 tree
3131 baselink_for_fns (tree fns)
3133 tree scope;
3134 tree cl;
3136 if (BASELINK_P (fns)
3137 || error_operand_p (fns))
3138 return fns;
3140 scope = ovl_scope (fns);
3141 if (!CLASS_TYPE_P (scope))
3142 return fns;
3144 cl = currently_open_derived_class (scope);
3145 if (!cl)
3146 cl = scope;
3147 cl = TYPE_BINFO (cl);
3148 return build_baselink (cl, cl, fns, /*optype=*/NULL_TREE);
3151 /* Returns true iff DECL is a variable from a function outside
3152 the current one. */
3154 static bool
3155 outer_var_p (tree decl)
3157 return ((VAR_P (decl) || TREE_CODE (decl) == PARM_DECL)
3158 && DECL_FUNCTION_SCOPE_P (decl)
3159 && (DECL_CONTEXT (decl) != current_function_decl
3160 || parsing_nsdmi ()));
3163 /* As above, but also checks that DECL is automatic. */
3165 bool
3166 outer_automatic_var_p (tree decl)
3168 return (outer_var_p (decl)
3169 && !TREE_STATIC (decl));
3172 /* DECL satisfies outer_automatic_var_p. Possibly complain about it or
3173 rewrite it for lambda capture. */
3175 tree
3176 process_outer_var_ref (tree decl, tsubst_flags_t complain)
3178 if (cp_unevaluated_operand)
3179 /* It's not a use (3.2) if we're in an unevaluated context. */
3180 return decl;
3181 if (decl == error_mark_node)
3182 return decl;
3184 tree context = DECL_CONTEXT (decl);
3185 tree containing_function = current_function_decl;
3186 tree lambda_stack = NULL_TREE;
3187 tree lambda_expr = NULL_TREE;
3188 tree initializer = convert_from_reference (decl);
3190 /* Mark it as used now even if the use is ill-formed. */
3191 if (!mark_used (decl, complain) && !(complain & tf_error))
3192 return error_mark_node;
3194 /* Core issue 696: "[At the July 2009 meeting] the CWG expressed
3195 support for an approach in which a reference to a local
3196 [constant] automatic variable in a nested class or lambda body
3197 would enter the expression as an rvalue, which would reduce
3198 the complexity of the problem"
3200 FIXME update for final resolution of core issue 696. */
3201 if (decl_maybe_constant_var_p (decl))
3203 if (processing_template_decl)
3204 /* In a template, the constant value may not be in a usable
3205 form, so wait until instantiation time. */
3206 return decl;
3207 else if (decl_constant_var_p (decl))
3209 tree t = maybe_constant_value (convert_from_reference (decl));
3210 if (TREE_CONSTANT (t))
3211 return t;
3215 if (parsing_nsdmi ())
3216 containing_function = NULL_TREE;
3217 else
3218 /* If we are in a lambda function, we can move out until we hit
3219 1. the context,
3220 2. a non-lambda function, or
3221 3. a non-default capturing lambda function. */
3222 while (context != containing_function
3223 && LAMBDA_FUNCTION_P (containing_function))
3225 tree closure = DECL_CONTEXT (containing_function);
3226 lambda_expr = CLASSTYPE_LAMBDA_EXPR (closure);
3228 if (TYPE_CLASS_SCOPE_P (closure))
3229 /* A lambda in an NSDMI (c++/64496). */
3230 break;
3232 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr)
3233 == CPLD_NONE)
3234 break;
3236 lambda_stack = tree_cons (NULL_TREE,
3237 lambda_expr,
3238 lambda_stack);
3240 containing_function
3241 = decl_function_context (containing_function);
3244 if (lambda_expr && VAR_P (decl)
3245 && DECL_ANON_UNION_VAR_P (decl))
3247 if (complain & tf_error)
3248 error ("cannot capture member %qD of anonymous union", decl);
3249 return error_mark_node;
3251 if (context == containing_function)
3253 decl = add_default_capture (lambda_stack,
3254 /*id=*/DECL_NAME (decl),
3255 initializer);
3257 else if (lambda_expr)
3259 if (complain & tf_error)
3261 error ("%qD is not captured", decl);
3262 tree closure = LAMBDA_EXPR_CLOSURE (lambda_expr);
3263 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr)
3264 == CPLD_NONE)
3265 inform (location_of (closure),
3266 "the lambda has no capture-default");
3267 else if (TYPE_CLASS_SCOPE_P (closure))
3268 inform (0, "lambda in local class %q+T cannot "
3269 "capture variables from the enclosing context",
3270 TYPE_CONTEXT (closure));
3271 inform (DECL_SOURCE_LOCATION (decl), "%q#D declared here", decl);
3273 return error_mark_node;
3275 else
3277 if (complain & tf_error)
3278 error (VAR_P (decl)
3279 ? G_("use of local variable with automatic storage from containing function")
3280 : G_("use of parameter from containing function"));
3281 inform (DECL_SOURCE_LOCATION (decl), "%q#D declared here", decl);
3282 return error_mark_node;
3284 return decl;
3287 /* ID_EXPRESSION is a representation of parsed, but unprocessed,
3288 id-expression. (See cp_parser_id_expression for details.) SCOPE,
3289 if non-NULL, is the type or namespace used to explicitly qualify
3290 ID_EXPRESSION. DECL is the entity to which that name has been
3291 resolved.
3293 *CONSTANT_EXPRESSION_P is true if we are presently parsing a
3294 constant-expression. In that case, *NON_CONSTANT_EXPRESSION_P will
3295 be set to true if this expression isn't permitted in a
3296 constant-expression, but it is otherwise not set by this function.
3297 *ALLOW_NON_CONSTANT_EXPRESSION_P is true if we are parsing a
3298 constant-expression, but a non-constant expression is also
3299 permissible.
3301 DONE is true if this expression is a complete postfix-expression;
3302 it is false if this expression is followed by '->', '[', '(', etc.
3303 ADDRESS_P is true iff this expression is the operand of '&'.
3304 TEMPLATE_P is true iff the qualified-id was of the form
3305 "A::template B". TEMPLATE_ARG_P is true iff this qualified name
3306 appears as a template argument.
3308 If an error occurs, and it is the kind of error that might cause
3309 the parser to abort a tentative parse, *ERROR_MSG is filled in. It
3310 is the caller's responsibility to issue the message. *ERROR_MSG
3311 will be a string with static storage duration, so the caller need
3312 not "free" it.
3314 Return an expression for the entity, after issuing appropriate
3315 diagnostics. This function is also responsible for transforming a
3316 reference to a non-static member into a COMPONENT_REF that makes
3317 the use of "this" explicit.
3319 Upon return, *IDK will be filled in appropriately. */
3320 tree
3321 finish_id_expression (tree id_expression,
3322 tree decl,
3323 tree scope,
3324 cp_id_kind *idk,
3325 bool integral_constant_expression_p,
3326 bool allow_non_integral_constant_expression_p,
3327 bool *non_integral_constant_expression_p,
3328 bool template_p,
3329 bool done,
3330 bool address_p,
3331 bool template_arg_p,
3332 const char **error_msg,
3333 location_t location)
3335 decl = strip_using_decl (decl);
3337 /* Initialize the output parameters. */
3338 *idk = CP_ID_KIND_NONE;
3339 *error_msg = NULL;
3341 if (id_expression == error_mark_node)
3342 return error_mark_node;
3343 /* If we have a template-id, then no further lookup is
3344 required. If the template-id was for a template-class, we
3345 will sometimes have a TYPE_DECL at this point. */
3346 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3347 || TREE_CODE (decl) == TYPE_DECL)
3349 /* Look up the name. */
3350 else
3352 if (decl == error_mark_node)
3354 /* Name lookup failed. */
3355 if (scope
3356 && (!TYPE_P (scope)
3357 || (!dependent_type_p (scope)
3358 && !(identifier_p (id_expression)
3359 && IDENTIFIER_TYPENAME_P (id_expression)
3360 && dependent_type_p (TREE_TYPE (id_expression))))))
3362 /* If the qualifying type is non-dependent (and the name
3363 does not name a conversion operator to a dependent
3364 type), issue an error. */
3365 qualified_name_lookup_error (scope, id_expression, decl, location);
3366 return error_mark_node;
3368 else if (!scope)
3370 /* It may be resolved via Koenig lookup. */
3371 *idk = CP_ID_KIND_UNQUALIFIED;
3372 return id_expression;
3374 else
3375 decl = id_expression;
3377 /* If DECL is a variable that would be out of scope under
3378 ANSI/ISO rules, but in scope in the ARM, name lookup
3379 will succeed. Issue a diagnostic here. */
3380 else
3381 decl = check_for_out_of_scope_variable (decl);
3383 /* Remember that the name was used in the definition of
3384 the current class so that we can check later to see if
3385 the meaning would have been different after the class
3386 was entirely defined. */
3387 if (!scope && decl != error_mark_node && identifier_p (id_expression))
3388 maybe_note_name_used_in_class (id_expression, decl);
3390 /* Disallow uses of local variables from containing functions, except
3391 within lambda-expressions. */
3392 if (outer_automatic_var_p (decl))
3394 decl = process_outer_var_ref (decl, tf_warning_or_error);
3395 if (decl == error_mark_node)
3396 return error_mark_node;
3399 /* Also disallow uses of function parameters outside the function
3400 body, except inside an unevaluated context (i.e. decltype). */
3401 if (TREE_CODE (decl) == PARM_DECL
3402 && DECL_CONTEXT (decl) == NULL_TREE
3403 && !cp_unevaluated_operand)
3405 *error_msg = "use of parameter outside function body";
3406 return error_mark_node;
3410 /* If we didn't find anything, or what we found was a type,
3411 then this wasn't really an id-expression. */
3412 if (TREE_CODE (decl) == TEMPLATE_DECL
3413 && !DECL_FUNCTION_TEMPLATE_P (decl))
3415 *error_msg = "missing template arguments";
3416 return error_mark_node;
3418 else if (TREE_CODE (decl) == TYPE_DECL
3419 || TREE_CODE (decl) == NAMESPACE_DECL)
3421 *error_msg = "expected primary-expression";
3422 return error_mark_node;
3425 /* If the name resolved to a template parameter, there is no
3426 need to look it up again later. */
3427 if ((TREE_CODE (decl) == CONST_DECL && DECL_TEMPLATE_PARM_P (decl))
3428 || TREE_CODE (decl) == TEMPLATE_PARM_INDEX)
3430 tree r;
3432 *idk = CP_ID_KIND_NONE;
3433 if (TREE_CODE (decl) == TEMPLATE_PARM_INDEX)
3434 decl = TEMPLATE_PARM_DECL (decl);
3435 r = convert_from_reference (DECL_INITIAL (decl));
3437 if (integral_constant_expression_p
3438 && !dependent_type_p (TREE_TYPE (decl))
3439 && !(INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (r))))
3441 if (!allow_non_integral_constant_expression_p)
3442 error ("template parameter %qD of type %qT is not allowed in "
3443 "an integral constant expression because it is not of "
3444 "integral or enumeration type", decl, TREE_TYPE (decl));
3445 *non_integral_constant_expression_p = true;
3447 return r;
3449 else
3451 bool dependent_p = type_dependent_expression_p (decl);
3453 /* If the declaration was explicitly qualified indicate
3454 that. The semantics of `A::f(3)' are different than
3455 `f(3)' if `f' is virtual. */
3456 *idk = (scope
3457 ? CP_ID_KIND_QUALIFIED
3458 : (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3459 ? CP_ID_KIND_TEMPLATE_ID
3460 : (dependent_p
3461 ? CP_ID_KIND_UNQUALIFIED_DEPENDENT
3462 : CP_ID_KIND_UNQUALIFIED)));
3464 /* If the name was dependent on a template parameter, we will
3465 resolve the name at instantiation time. */
3466 if (dependent_p)
3468 /* If we found a variable, then name lookup during the
3469 instantiation will always resolve to the same VAR_DECL
3470 (or an instantiation thereof). */
3471 if (VAR_P (decl)
3472 || TREE_CODE (decl) == CONST_DECL
3473 || TREE_CODE (decl) == PARM_DECL)
3475 mark_used (decl);
3476 return convert_from_reference (decl);
3479 /* Create a SCOPE_REF for qualified names, if the scope is
3480 dependent. */
3481 if (scope)
3483 if (TYPE_P (scope))
3485 if (address_p && done)
3486 decl = finish_qualified_id_expr (scope, decl,
3487 done, address_p,
3488 template_p,
3489 template_arg_p,
3490 tf_warning_or_error);
3491 else
3493 tree type = NULL_TREE;
3494 if (DECL_P (decl) && !dependent_scope_p (scope))
3495 type = TREE_TYPE (decl);
3496 decl = build_qualified_name (type,
3497 scope,
3498 id_expression,
3499 template_p);
3502 if (TREE_TYPE (decl))
3503 decl = convert_from_reference (decl);
3504 return decl;
3506 /* A TEMPLATE_ID already contains all the information we
3507 need. */
3508 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR)
3509 return id_expression;
3510 /* The same is true for FIELD_DECL, but we also need to
3511 make sure that the syntax is correct. */
3512 else if (TREE_CODE (decl) == FIELD_DECL)
3514 /* Since SCOPE is NULL here, this is an unqualified name.
3515 Access checking has been performed during name lookup
3516 already. Turn off checking to avoid duplicate errors. */
3517 push_deferring_access_checks (dk_no_check);
3518 decl = finish_non_static_data_member
3519 (decl, NULL_TREE,
3520 /*qualifying_scope=*/NULL_TREE);
3521 pop_deferring_access_checks ();
3522 return decl;
3524 return id_expression;
3527 if (TREE_CODE (decl) == NAMESPACE_DECL)
3529 error ("use of namespace %qD as expression", decl);
3530 return error_mark_node;
3532 else if (DECL_CLASS_TEMPLATE_P (decl))
3534 error ("use of class template %qT as expression", decl);
3535 return error_mark_node;
3537 else if (TREE_CODE (decl) == TREE_LIST)
3539 /* Ambiguous reference to base members. */
3540 error ("request for member %qD is ambiguous in "
3541 "multiple inheritance lattice", id_expression);
3542 print_candidates (decl);
3543 return error_mark_node;
3546 /* Mark variable-like entities as used. Functions are similarly
3547 marked either below or after overload resolution. */
3548 if ((VAR_P (decl)
3549 || TREE_CODE (decl) == PARM_DECL
3550 || TREE_CODE (decl) == CONST_DECL
3551 || TREE_CODE (decl) == RESULT_DECL)
3552 && !mark_used (decl))
3553 return error_mark_node;
3555 /* Only certain kinds of names are allowed in constant
3556 expression. Template parameters have already
3557 been handled above. */
3558 if (! error_operand_p (decl)
3559 && integral_constant_expression_p
3560 && ! decl_constant_var_p (decl)
3561 && TREE_CODE (decl) != CONST_DECL
3562 && ! builtin_valid_in_constant_expr_p (decl))
3564 if (!allow_non_integral_constant_expression_p)
3566 error ("%qD cannot appear in a constant-expression", decl);
3567 return error_mark_node;
3569 *non_integral_constant_expression_p = true;
3572 tree wrap;
3573 if (VAR_P (decl)
3574 && !cp_unevaluated_operand
3575 && !processing_template_decl
3576 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl))
3577 && CP_DECL_THREAD_LOCAL_P (decl)
3578 && (wrap = get_tls_wrapper_fn (decl)))
3580 /* Replace an evaluated use of the thread_local variable with
3581 a call to its wrapper. */
3582 decl = build_cxx_call (wrap, 0, NULL, tf_warning_or_error);
3584 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3585 && variable_template_p (TREE_OPERAND (decl, 0)))
3587 decl = finish_template_variable (decl);
3588 mark_used (decl);
3589 decl = convert_from_reference (decl);
3591 else if (scope)
3593 decl = (adjust_result_of_qualified_name_lookup
3594 (decl, scope, current_nonlambda_class_type()));
3596 if (TREE_CODE (decl) == FUNCTION_DECL)
3597 mark_used (decl);
3599 if (TYPE_P (scope))
3600 decl = finish_qualified_id_expr (scope,
3601 decl,
3602 done,
3603 address_p,
3604 template_p,
3605 template_arg_p,
3606 tf_warning_or_error);
3607 else
3608 decl = convert_from_reference (decl);
3610 else if (TREE_CODE (decl) == FIELD_DECL)
3612 /* Since SCOPE is NULL here, this is an unqualified name.
3613 Access checking has been performed during name lookup
3614 already. Turn off checking to avoid duplicate errors. */
3615 push_deferring_access_checks (dk_no_check);
3616 decl = finish_non_static_data_member (decl, NULL_TREE,
3617 /*qualifying_scope=*/NULL_TREE);
3618 pop_deferring_access_checks ();
3620 else if (is_overloaded_fn (decl))
3622 tree first_fn;
3624 first_fn = get_first_fn (decl);
3625 if (TREE_CODE (first_fn) == TEMPLATE_DECL)
3626 first_fn = DECL_TEMPLATE_RESULT (first_fn);
3628 if (!really_overloaded_fn (decl)
3629 && !mark_used (first_fn))
3630 return error_mark_node;
3632 if (!template_arg_p
3633 && TREE_CODE (first_fn) == FUNCTION_DECL
3634 && DECL_FUNCTION_MEMBER_P (first_fn)
3635 && !shared_member_p (decl))
3637 /* A set of member functions. */
3638 decl = maybe_dummy_object (DECL_CONTEXT (first_fn), 0);
3639 return finish_class_member_access_expr (decl, id_expression,
3640 /*template_p=*/false,
3641 tf_warning_or_error);
3644 decl = baselink_for_fns (decl);
3646 else
3648 if (DECL_P (decl) && DECL_NONLOCAL (decl)
3649 && DECL_CLASS_SCOPE_P (decl))
3651 tree context = context_for_name_lookup (decl);
3652 if (context != current_class_type)
3654 tree path = currently_open_derived_class (context);
3655 perform_or_defer_access_check (TYPE_BINFO (path),
3656 decl, decl,
3657 tf_warning_or_error);
3661 decl = convert_from_reference (decl);
3665 return decl;
3668 /* Implement the __typeof keyword: Return the type of EXPR, suitable for
3669 use as a type-specifier. */
3671 tree
3672 finish_typeof (tree expr)
3674 tree type;
3676 if (type_dependent_expression_p (expr))
3678 type = cxx_make_type (TYPEOF_TYPE);
3679 TYPEOF_TYPE_EXPR (type) = expr;
3680 SET_TYPE_STRUCTURAL_EQUALITY (type);
3682 return type;
3685 expr = mark_type_use (expr);
3687 type = unlowered_expr_type (expr);
3689 if (!type || type == unknown_type_node)
3691 error ("type of %qE is unknown", expr);
3692 return error_mark_node;
3695 return type;
3698 /* Implement the __underlying_type keyword: Return the underlying
3699 type of TYPE, suitable for use as a type-specifier. */
3701 tree
3702 finish_underlying_type (tree type)
3704 tree underlying_type;
3706 if (processing_template_decl)
3708 underlying_type = cxx_make_type (UNDERLYING_TYPE);
3709 UNDERLYING_TYPE_TYPE (underlying_type) = type;
3710 SET_TYPE_STRUCTURAL_EQUALITY (underlying_type);
3712 return underlying_type;
3715 complete_type (type);
3717 if (TREE_CODE (type) != ENUMERAL_TYPE)
3719 error ("%qT is not an enumeration type", type);
3720 return error_mark_node;
3723 underlying_type = ENUM_UNDERLYING_TYPE (type);
3725 /* Fixup necessary in this case because ENUM_UNDERLYING_TYPE
3726 includes TYPE_MIN_VALUE and TYPE_MAX_VALUE information.
3727 See finish_enum_value_list for details. */
3728 if (!ENUM_FIXED_UNDERLYING_TYPE_P (type))
3729 underlying_type
3730 = c_common_type_for_mode (TYPE_MODE (underlying_type),
3731 TYPE_UNSIGNED (underlying_type));
3733 return underlying_type;
3736 /* Implement the __direct_bases keyword: Return the direct base classes
3737 of type */
3739 tree
3740 calculate_direct_bases (tree type)
3742 vec<tree, va_gc> *vector = make_tree_vector();
3743 tree bases_vec = NULL_TREE;
3744 vec<tree, va_gc> *base_binfos;
3745 tree binfo;
3746 unsigned i;
3748 complete_type (type);
3750 if (!NON_UNION_CLASS_TYPE_P (type))
3751 return make_tree_vec (0);
3753 base_binfos = BINFO_BASE_BINFOS (TYPE_BINFO (type));
3755 /* Virtual bases are initialized first */
3756 for (i = 0; base_binfos->iterate (i, &binfo); i++)
3758 if (BINFO_VIRTUAL_P (binfo))
3760 vec_safe_push (vector, binfo);
3764 /* Now non-virtuals */
3765 for (i = 0; base_binfos->iterate (i, &binfo); i++)
3767 if (!BINFO_VIRTUAL_P (binfo))
3769 vec_safe_push (vector, binfo);
3774 bases_vec = make_tree_vec (vector->length ());
3776 for (i = 0; i < vector->length (); ++i)
3778 TREE_VEC_ELT (bases_vec, i) = BINFO_TYPE ((*vector)[i]);
3780 return bases_vec;
3783 /* Implement the __bases keyword: Return the base classes
3784 of type */
3786 /* Find morally non-virtual base classes by walking binfo hierarchy */
3787 /* Virtual base classes are handled separately in finish_bases */
3789 static tree
3790 dfs_calculate_bases_pre (tree binfo, void * /*data_*/)
3792 /* Don't walk bases of virtual bases */
3793 return BINFO_VIRTUAL_P (binfo) ? dfs_skip_bases : NULL_TREE;
3796 static tree
3797 dfs_calculate_bases_post (tree binfo, void *data_)
3799 vec<tree, va_gc> **data = ((vec<tree, va_gc> **) data_);
3800 if (!BINFO_VIRTUAL_P (binfo))
3802 vec_safe_push (*data, BINFO_TYPE (binfo));
3804 return NULL_TREE;
3807 /* Calculates the morally non-virtual base classes of a class */
3808 static vec<tree, va_gc> *
3809 calculate_bases_helper (tree type)
3811 vec<tree, va_gc> *vector = make_tree_vector();
3813 /* Now add non-virtual base classes in order of construction */
3814 dfs_walk_all (TYPE_BINFO (type),
3815 dfs_calculate_bases_pre, dfs_calculate_bases_post, &vector);
3816 return vector;
3819 tree
3820 calculate_bases (tree type)
3822 vec<tree, va_gc> *vector = make_tree_vector();
3823 tree bases_vec = NULL_TREE;
3824 unsigned i;
3825 vec<tree, va_gc> *vbases;
3826 vec<tree, va_gc> *nonvbases;
3827 tree binfo;
3829 complete_type (type);
3831 if (!NON_UNION_CLASS_TYPE_P (type))
3832 return make_tree_vec (0);
3834 /* First go through virtual base classes */
3835 for (vbases = CLASSTYPE_VBASECLASSES (type), i = 0;
3836 vec_safe_iterate (vbases, i, &binfo); i++)
3838 vec<tree, va_gc> *vbase_bases;
3839 vbase_bases = calculate_bases_helper (BINFO_TYPE (binfo));
3840 vec_safe_splice (vector, vbase_bases);
3841 release_tree_vector (vbase_bases);
3844 /* Now for the non-virtual bases */
3845 nonvbases = calculate_bases_helper (type);
3846 vec_safe_splice (vector, nonvbases);
3847 release_tree_vector (nonvbases);
3849 /* Last element is entire class, so don't copy */
3850 bases_vec = make_tree_vec (vector->length () - 1);
3852 for (i = 0; i < vector->length () - 1; ++i)
3854 TREE_VEC_ELT (bases_vec, i) = (*vector)[i];
3856 release_tree_vector (vector);
3857 return bases_vec;
3860 tree
3861 finish_bases (tree type, bool direct)
3863 tree bases = NULL_TREE;
3865 if (!processing_template_decl)
3867 /* Parameter packs can only be used in templates */
3868 error ("Parameter pack __bases only valid in template declaration");
3869 return error_mark_node;
3872 bases = cxx_make_type (BASES);
3873 BASES_TYPE (bases) = type;
3874 BASES_DIRECT (bases) = direct;
3875 SET_TYPE_STRUCTURAL_EQUALITY (bases);
3877 return bases;
3880 /* Perform C++-specific checks for __builtin_offsetof before calling
3881 fold_offsetof. */
3883 tree
3884 finish_offsetof (tree expr, location_t loc)
3886 /* If we're processing a template, we can't finish the semantics yet.
3887 Otherwise we can fold the entire expression now. */
3888 if (processing_template_decl)
3890 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
3891 SET_EXPR_LOCATION (expr, loc);
3892 return expr;
3895 if (TREE_CODE (expr) == PSEUDO_DTOR_EXPR)
3897 error ("cannot apply %<offsetof%> to destructor %<~%T%>",
3898 TREE_OPERAND (expr, 2));
3899 return error_mark_node;
3901 if (TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE
3902 || TREE_CODE (TREE_TYPE (expr)) == METHOD_TYPE
3903 || TREE_TYPE (expr) == unknown_type_node)
3905 if (INDIRECT_REF_P (expr))
3906 error ("second operand of %<offsetof%> is neither a single "
3907 "identifier nor a sequence of member accesses and "
3908 "array references");
3909 else
3911 if (TREE_CODE (expr) == COMPONENT_REF
3912 || TREE_CODE (expr) == COMPOUND_EXPR)
3913 expr = TREE_OPERAND (expr, 1);
3914 error ("cannot apply %<offsetof%> to member function %qD", expr);
3916 return error_mark_node;
3918 if (REFERENCE_REF_P (expr))
3919 expr = TREE_OPERAND (expr, 0);
3920 if (TREE_CODE (expr) == COMPONENT_REF)
3922 tree object = TREE_OPERAND (expr, 0);
3923 if (!complete_type_or_else (TREE_TYPE (object), object))
3924 return error_mark_node;
3925 if (warn_invalid_offsetof
3926 && CLASS_TYPE_P (TREE_TYPE (object))
3927 && CLASSTYPE_NON_STD_LAYOUT (TREE_TYPE (object))
3928 && cp_unevaluated_operand == 0)
3929 pedwarn (loc, OPT_Winvalid_offsetof,
3930 "offsetof within non-standard-layout type %qT is undefined",
3931 TREE_TYPE (object));
3933 return fold_offsetof (expr);
3936 /* Replace the AGGR_INIT_EXPR at *TP with an equivalent CALL_EXPR. This
3937 function is broken out from the above for the benefit of the tree-ssa
3938 project. */
3940 void
3941 simplify_aggr_init_expr (tree *tp)
3943 tree aggr_init_expr = *tp;
3945 /* Form an appropriate CALL_EXPR. */
3946 tree fn = AGGR_INIT_EXPR_FN (aggr_init_expr);
3947 tree slot = AGGR_INIT_EXPR_SLOT (aggr_init_expr);
3948 tree type = TREE_TYPE (slot);
3950 tree call_expr;
3951 enum style_t { ctor, arg, pcc } style;
3953 if (AGGR_INIT_VIA_CTOR_P (aggr_init_expr))
3954 style = ctor;
3955 #ifdef PCC_STATIC_STRUCT_RETURN
3956 else if (1)
3957 style = pcc;
3958 #endif
3959 else
3961 gcc_assert (TREE_ADDRESSABLE (type));
3962 style = arg;
3965 call_expr = build_call_array_loc (input_location,
3966 TREE_TYPE (TREE_TYPE (TREE_TYPE (fn))),
3968 aggr_init_expr_nargs (aggr_init_expr),
3969 AGGR_INIT_EXPR_ARGP (aggr_init_expr));
3970 TREE_NOTHROW (call_expr) = TREE_NOTHROW (aggr_init_expr);
3971 CALL_EXPR_LIST_INIT_P (call_expr) = CALL_EXPR_LIST_INIT_P (aggr_init_expr);
3973 if (style == ctor)
3975 /* Replace the first argument to the ctor with the address of the
3976 slot. */
3977 cxx_mark_addressable (slot);
3978 CALL_EXPR_ARG (call_expr, 0) =
3979 build1 (ADDR_EXPR, build_pointer_type (type), slot);
3981 else if (style == arg)
3983 /* Just mark it addressable here, and leave the rest to
3984 expand_call{,_inline}. */
3985 cxx_mark_addressable (slot);
3986 CALL_EXPR_RETURN_SLOT_OPT (call_expr) = true;
3987 call_expr = build2 (INIT_EXPR, TREE_TYPE (call_expr), slot, call_expr);
3989 else if (style == pcc)
3991 /* If we're using the non-reentrant PCC calling convention, then we
3992 need to copy the returned value out of the static buffer into the
3993 SLOT. */
3994 push_deferring_access_checks (dk_no_check);
3995 call_expr = build_aggr_init (slot, call_expr,
3996 DIRECT_BIND | LOOKUP_ONLYCONVERTING,
3997 tf_warning_or_error);
3998 pop_deferring_access_checks ();
3999 call_expr = build2 (COMPOUND_EXPR, TREE_TYPE (slot), call_expr, slot);
4002 if (AGGR_INIT_ZERO_FIRST (aggr_init_expr))
4004 tree init = build_zero_init (type, NULL_TREE,
4005 /*static_storage_p=*/false);
4006 init = build2 (INIT_EXPR, void_type_node, slot, init);
4007 call_expr = build2 (COMPOUND_EXPR, TREE_TYPE (call_expr),
4008 init, call_expr);
4011 *tp = call_expr;
4014 /* Emit all thunks to FN that should be emitted when FN is emitted. */
4016 void
4017 emit_associated_thunks (tree fn)
4019 /* When we use vcall offsets, we emit thunks with the virtual
4020 functions to which they thunk. The whole point of vcall offsets
4021 is so that you can know statically the entire set of thunks that
4022 will ever be needed for a given virtual function, thereby
4023 enabling you to output all the thunks with the function itself. */
4024 if (DECL_VIRTUAL_P (fn)
4025 /* Do not emit thunks for extern template instantiations. */
4026 && ! DECL_REALLY_EXTERN (fn))
4028 tree thunk;
4030 for (thunk = DECL_THUNKS (fn); thunk; thunk = DECL_CHAIN (thunk))
4032 if (!THUNK_ALIAS (thunk))
4034 use_thunk (thunk, /*emit_p=*/1);
4035 if (DECL_RESULT_THUNK_P (thunk))
4037 tree probe;
4039 for (probe = DECL_THUNKS (thunk);
4040 probe; probe = DECL_CHAIN (probe))
4041 use_thunk (probe, /*emit_p=*/1);
4044 else
4045 gcc_assert (!DECL_THUNKS (thunk));
4050 /* Generate RTL for FN. */
4052 bool
4053 expand_or_defer_fn_1 (tree fn)
4055 /* When the parser calls us after finishing the body of a template
4056 function, we don't really want to expand the body. */
4057 if (processing_template_decl)
4059 /* Normally, collection only occurs in rest_of_compilation. So,
4060 if we don't collect here, we never collect junk generated
4061 during the processing of templates until we hit a
4062 non-template function. It's not safe to do this inside a
4063 nested class, though, as the parser may have local state that
4064 is not a GC root. */
4065 if (!function_depth)
4066 ggc_collect ();
4067 return false;
4070 gcc_assert (DECL_SAVED_TREE (fn));
4072 /* We make a decision about linkage for these functions at the end
4073 of the compilation. Until that point, we do not want the back
4074 end to output them -- but we do want it to see the bodies of
4075 these functions so that it can inline them as appropriate. */
4076 if (DECL_DECLARED_INLINE_P (fn) || DECL_IMPLICIT_INSTANTIATION (fn))
4078 if (DECL_INTERFACE_KNOWN (fn))
4079 /* We've already made a decision as to how this function will
4080 be handled. */;
4081 else if (!at_eof)
4082 tentative_decl_linkage (fn);
4083 else
4084 import_export_decl (fn);
4086 /* If the user wants us to keep all inline functions, then mark
4087 this function as needed so that finish_file will make sure to
4088 output it later. Similarly, all dllexport'd functions must
4089 be emitted; there may be callers in other DLLs. */
4090 if (DECL_DECLARED_INLINE_P (fn)
4091 && !DECL_REALLY_EXTERN (fn)
4092 && (flag_keep_inline_functions
4093 || (flag_keep_inline_dllexport
4094 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (fn)))))
4096 mark_needed (fn);
4097 DECL_EXTERNAL (fn) = 0;
4101 /* If this is a constructor or destructor body, we have to clone
4102 it. */
4103 if (maybe_clone_body (fn))
4105 /* We don't want to process FN again, so pretend we've written
4106 it out, even though we haven't. */
4107 TREE_ASM_WRITTEN (fn) = 1;
4108 /* If this is an instantiation of a constexpr function, keep
4109 DECL_SAVED_TREE for explain_invalid_constexpr_fn. */
4110 if (!is_instantiation_of_constexpr (fn))
4111 DECL_SAVED_TREE (fn) = NULL_TREE;
4112 return false;
4115 /* There's no reason to do any of the work here if we're only doing
4116 semantic analysis; this code just generates RTL. */
4117 if (flag_syntax_only)
4118 return false;
4120 return true;
4123 void
4124 expand_or_defer_fn (tree fn)
4126 if (expand_or_defer_fn_1 (fn))
4128 function_depth++;
4130 /* Expand or defer, at the whim of the compilation unit manager. */
4131 cgraph_node::finalize_function (fn, function_depth > 1);
4132 emit_associated_thunks (fn);
4134 function_depth--;
4138 struct nrv_data
4140 nrv_data () : visited (37) {}
4142 tree var;
4143 tree result;
4144 hash_table<nofree_ptr_hash <tree_node> > visited;
4147 /* Helper function for walk_tree, used by finalize_nrv below. */
4149 static tree
4150 finalize_nrv_r (tree* tp, int* walk_subtrees, void* data)
4152 struct nrv_data *dp = (struct nrv_data *)data;
4153 tree_node **slot;
4155 /* No need to walk into types. There wouldn't be any need to walk into
4156 non-statements, except that we have to consider STMT_EXPRs. */
4157 if (TYPE_P (*tp))
4158 *walk_subtrees = 0;
4159 /* Change all returns to just refer to the RESULT_DECL; this is a nop,
4160 but differs from using NULL_TREE in that it indicates that we care
4161 about the value of the RESULT_DECL. */
4162 else if (TREE_CODE (*tp) == RETURN_EXPR)
4163 TREE_OPERAND (*tp, 0) = dp->result;
4164 /* Change all cleanups for the NRV to only run when an exception is
4165 thrown. */
4166 else if (TREE_CODE (*tp) == CLEANUP_STMT
4167 && CLEANUP_DECL (*tp) == dp->var)
4168 CLEANUP_EH_ONLY (*tp) = 1;
4169 /* Replace the DECL_EXPR for the NRV with an initialization of the
4170 RESULT_DECL, if needed. */
4171 else if (TREE_CODE (*tp) == DECL_EXPR
4172 && DECL_EXPR_DECL (*tp) == dp->var)
4174 tree init;
4175 if (DECL_INITIAL (dp->var)
4176 && DECL_INITIAL (dp->var) != error_mark_node)
4177 init = build2 (INIT_EXPR, void_type_node, dp->result,
4178 DECL_INITIAL (dp->var));
4179 else
4180 init = build_empty_stmt (EXPR_LOCATION (*tp));
4181 DECL_INITIAL (dp->var) = NULL_TREE;
4182 SET_EXPR_LOCATION (init, EXPR_LOCATION (*tp));
4183 *tp = init;
4185 /* And replace all uses of the NRV with the RESULT_DECL. */
4186 else if (*tp == dp->var)
4187 *tp = dp->result;
4189 /* Avoid walking into the same tree more than once. Unfortunately, we
4190 can't just use walk_tree_without duplicates because it would only call
4191 us for the first occurrence of dp->var in the function body. */
4192 slot = dp->visited.find_slot (*tp, INSERT);
4193 if (*slot)
4194 *walk_subtrees = 0;
4195 else
4196 *slot = *tp;
4198 /* Keep iterating. */
4199 return NULL_TREE;
4202 /* Called from finish_function to implement the named return value
4203 optimization by overriding all the RETURN_EXPRs and pertinent
4204 CLEANUP_STMTs and replacing all occurrences of VAR with RESULT, the
4205 RESULT_DECL for the function. */
4207 void
4208 finalize_nrv (tree *tp, tree var, tree result)
4210 struct nrv_data data;
4212 /* Copy name from VAR to RESULT. */
4213 DECL_NAME (result) = DECL_NAME (var);
4214 /* Don't forget that we take its address. */
4215 TREE_ADDRESSABLE (result) = TREE_ADDRESSABLE (var);
4216 /* Finally set DECL_VALUE_EXPR to avoid assigning
4217 a stack slot at -O0 for the original var and debug info
4218 uses RESULT location for VAR. */
4219 SET_DECL_VALUE_EXPR (var, result);
4220 DECL_HAS_VALUE_EXPR_P (var) = 1;
4222 data.var = var;
4223 data.result = result;
4224 cp_walk_tree (tp, finalize_nrv_r, &data, 0);
4227 /* Create CP_OMP_CLAUSE_INFO for clause C. Returns true if it is invalid. */
4229 bool
4230 cxx_omp_create_clause_info (tree c, tree type, bool need_default_ctor,
4231 bool need_copy_ctor, bool need_copy_assignment,
4232 bool need_dtor)
4234 int save_errorcount = errorcount;
4235 tree info, t;
4237 /* Always allocate 3 elements for simplicity. These are the
4238 function decls for the ctor, dtor, and assignment op.
4239 This layout is known to the three lang hooks,
4240 cxx_omp_clause_default_init, cxx_omp_clause_copy_init,
4241 and cxx_omp_clause_assign_op. */
4242 info = make_tree_vec (3);
4243 CP_OMP_CLAUSE_INFO (c) = info;
4245 if (need_default_ctor || need_copy_ctor)
4247 if (need_default_ctor)
4248 t = get_default_ctor (type);
4249 else
4250 t = get_copy_ctor (type, tf_warning_or_error);
4252 if (t && !trivial_fn_p (t))
4253 TREE_VEC_ELT (info, 0) = t;
4256 if (need_dtor && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
4257 TREE_VEC_ELT (info, 1) = get_dtor (type, tf_warning_or_error);
4259 if (need_copy_assignment)
4261 t = get_copy_assign (type);
4263 if (t && !trivial_fn_p (t))
4264 TREE_VEC_ELT (info, 2) = t;
4267 return errorcount != save_errorcount;
4270 /* If DECL is DECL_OMP_PRIVATIZED_MEMBER, return corresponding
4271 FIELD_DECL, otherwise return DECL itself. */
4273 static tree
4274 omp_clause_decl_field (tree decl)
4276 if (VAR_P (decl)
4277 && DECL_HAS_VALUE_EXPR_P (decl)
4278 && DECL_ARTIFICIAL (decl)
4279 && DECL_LANG_SPECIFIC (decl)
4280 && DECL_OMP_PRIVATIZED_MEMBER (decl))
4282 tree f = DECL_VALUE_EXPR (decl);
4283 if (TREE_CODE (f) == INDIRECT_REF)
4284 f = TREE_OPERAND (f, 0);
4285 if (TREE_CODE (f) == COMPONENT_REF)
4287 f = TREE_OPERAND (f, 1);
4288 gcc_assert (TREE_CODE (f) == FIELD_DECL);
4289 return f;
4292 return NULL_TREE;
4295 /* Adjust DECL if needed for printing using %qE. */
4297 static tree
4298 omp_clause_printable_decl (tree decl)
4300 tree t = omp_clause_decl_field (decl);
4301 if (t)
4302 return t;
4303 return decl;
4306 /* For a FIELD_DECL F and corresponding DECL_OMP_PRIVATIZED_MEMBER
4307 VAR_DECL T that doesn't need a DECL_EXPR added, record it for
4308 privatization. */
4310 static void
4311 omp_note_field_privatization (tree f, tree t)
4313 if (!omp_private_member_map)
4314 omp_private_member_map = new hash_map<tree, tree>;
4315 tree &v = omp_private_member_map->get_or_insert (f);
4316 if (v == NULL_TREE)
4318 v = t;
4319 omp_private_member_vec.safe_push (f);
4320 /* Signal that we don't want to create DECL_EXPR for this dummy var. */
4321 omp_private_member_vec.safe_push (integer_zero_node);
4325 /* Privatize FIELD_DECL T, return corresponding DECL_OMP_PRIVATIZED_MEMBER
4326 dummy VAR_DECL. */
4328 tree
4329 omp_privatize_field (tree t)
4331 tree m = finish_non_static_data_member (t, NULL_TREE, NULL_TREE);
4332 if (m == error_mark_node)
4333 return error_mark_node;
4334 if (!omp_private_member_map)
4335 omp_private_member_map = new hash_map<tree, tree>;
4336 if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE)
4338 gcc_assert (TREE_CODE (m) == INDIRECT_REF);
4339 m = TREE_OPERAND (m, 0);
4341 tree &v = omp_private_member_map->get_or_insert (t);
4342 if (v == NULL_TREE)
4344 v = create_temporary_var (TREE_TYPE (m));
4345 if (!DECL_LANG_SPECIFIC (v))
4346 retrofit_lang_decl (v);
4347 DECL_OMP_PRIVATIZED_MEMBER (v) = 1;
4348 SET_DECL_VALUE_EXPR (v, m);
4349 DECL_HAS_VALUE_EXPR_P (v) = 1;
4350 omp_private_member_vec.safe_push (t);
4352 return v;
4355 /* Helper function for handle_omp_array_sections. Called recursively
4356 to handle multiple array-section-subscripts. C is the clause,
4357 T current expression (initially OMP_CLAUSE_DECL), which is either
4358 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
4359 expression if specified, TREE_VALUE length expression if specified,
4360 TREE_CHAIN is what it has been specified after, or some decl.
4361 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
4362 set to true if any of the array-section-subscript could have length
4363 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
4364 first array-section-subscript which is known not to have length
4365 of one. Given say:
4366 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
4367 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
4368 all are or may have length of 1, array-section-subscript [:2] is the
4369 first one known not to have length 1. For array-section-subscript
4370 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
4371 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
4372 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
4373 case though, as some lengths could be zero. */
4375 static tree
4376 handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
4377 bool &maybe_zero_len, unsigned int &first_non_one,
4378 bool is_omp)
4380 tree ret, low_bound, length, type;
4381 if (TREE_CODE (t) != TREE_LIST)
4383 if (error_operand_p (t))
4384 return error_mark_node;
4385 if (REFERENCE_REF_P (t)
4386 && TREE_CODE (TREE_OPERAND (t, 0)) == COMPONENT_REF)
4387 t = TREE_OPERAND (t, 0);
4388 ret = t;
4389 if (TREE_CODE (t) == COMPONENT_REF
4390 && is_omp
4391 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
4392 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO
4393 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FROM)
4394 && !type_dependent_expression_p (t))
4396 if (DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
4398 error_at (OMP_CLAUSE_LOCATION (c),
4399 "bit-field %qE in %qs clause",
4400 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4401 return error_mark_node;
4403 while (TREE_CODE (t) == COMPONENT_REF)
4405 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == UNION_TYPE)
4407 error_at (OMP_CLAUSE_LOCATION (c),
4408 "%qE is a member of a union", t);
4409 return error_mark_node;
4411 t = TREE_OPERAND (t, 0);
4414 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
4416 if (processing_template_decl)
4417 return NULL_TREE;
4418 if (DECL_P (t))
4419 error_at (OMP_CLAUSE_LOCATION (c),
4420 "%qD is not a variable in %qs clause", t,
4421 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4422 else
4423 error_at (OMP_CLAUSE_LOCATION (c),
4424 "%qE is not a variable in %qs clause", t,
4425 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4426 return error_mark_node;
4428 else if (TREE_CODE (t) == PARM_DECL
4429 && DECL_ARTIFICIAL (t)
4430 && DECL_NAME (t) == this_identifier)
4432 error_at (OMP_CLAUSE_LOCATION (c),
4433 "%<this%> allowed in OpenMP only in %<declare simd%>"
4434 " clauses");
4435 return error_mark_node;
4437 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
4438 && VAR_P (t) && CP_DECL_THREAD_LOCAL_P (t))
4440 error_at (OMP_CLAUSE_LOCATION (c),
4441 "%qD is threadprivate variable in %qs clause", t,
4442 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4443 return error_mark_node;
4445 if (type_dependent_expression_p (ret))
4446 return NULL_TREE;
4447 ret = convert_from_reference (ret);
4448 return ret;
4451 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
4452 && TREE_CODE (TREE_CHAIN (t)) == FIELD_DECL)
4453 TREE_CHAIN (t) = omp_privatize_field (TREE_CHAIN (t));
4454 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
4455 maybe_zero_len, first_non_one, is_omp);
4456 if (ret == error_mark_node || ret == NULL_TREE)
4457 return ret;
4459 type = TREE_TYPE (ret);
4460 low_bound = TREE_PURPOSE (t);
4461 length = TREE_VALUE (t);
4462 if ((low_bound && type_dependent_expression_p (low_bound))
4463 || (length && type_dependent_expression_p (length)))
4464 return NULL_TREE;
4466 if (low_bound == error_mark_node || length == error_mark_node)
4467 return error_mark_node;
4469 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
4471 error_at (OMP_CLAUSE_LOCATION (c),
4472 "low bound %qE of array section does not have integral type",
4473 low_bound);
4474 return error_mark_node;
4476 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
4478 error_at (OMP_CLAUSE_LOCATION (c),
4479 "length %qE of array section does not have integral type",
4480 length);
4481 return error_mark_node;
4483 if (low_bound)
4484 low_bound = mark_rvalue_use (low_bound);
4485 if (length)
4486 length = mark_rvalue_use (length);
4487 if (low_bound
4488 && TREE_CODE (low_bound) == INTEGER_CST
4489 && TYPE_PRECISION (TREE_TYPE (low_bound))
4490 > TYPE_PRECISION (sizetype))
4491 low_bound = fold_convert (sizetype, low_bound);
4492 if (length
4493 && TREE_CODE (length) == INTEGER_CST
4494 && TYPE_PRECISION (TREE_TYPE (length))
4495 > TYPE_PRECISION (sizetype))
4496 length = fold_convert (sizetype, length);
4497 if (low_bound == NULL_TREE)
4498 low_bound = integer_zero_node;
4500 if (length != NULL_TREE)
4502 if (!integer_nonzerop (length))
4504 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
4505 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
4507 if (integer_zerop (length))
4509 error_at (OMP_CLAUSE_LOCATION (c),
4510 "zero length array section in %qs clause",
4511 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4512 return error_mark_node;
4515 else
4516 maybe_zero_len = true;
4518 if (first_non_one == types.length ()
4519 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
4520 first_non_one++;
4522 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
4523 && !integer_zerop (low_bound))
4525 error_at (OMP_CLAUSE_LOCATION (c),
4526 "%<reduction%> array section has to be zero-based");
4527 return error_mark_node;
4529 if (TREE_CODE (type) == ARRAY_TYPE)
4531 if (length == NULL_TREE
4532 && (TYPE_DOMAIN (type) == NULL_TREE
4533 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
4535 error_at (OMP_CLAUSE_LOCATION (c),
4536 "for unknown bound array type length expression must "
4537 "be specified");
4538 return error_mark_node;
4540 if (TREE_CODE (low_bound) == INTEGER_CST
4541 && tree_int_cst_sgn (low_bound) == -1)
4543 error_at (OMP_CLAUSE_LOCATION (c),
4544 "negative low bound in array section in %qs clause",
4545 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4546 return error_mark_node;
4548 if (length != NULL_TREE
4549 && TREE_CODE (length) == INTEGER_CST
4550 && tree_int_cst_sgn (length) == -1)
4552 error_at (OMP_CLAUSE_LOCATION (c),
4553 "negative length in array section in %qs clause",
4554 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4555 return error_mark_node;
4557 if (TYPE_DOMAIN (type)
4558 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
4559 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
4560 == INTEGER_CST)
4562 tree size = size_binop (PLUS_EXPR,
4563 TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
4564 size_one_node);
4565 if (TREE_CODE (low_bound) == INTEGER_CST)
4567 if (tree_int_cst_lt (size, low_bound))
4569 error_at (OMP_CLAUSE_LOCATION (c),
4570 "low bound %qE above array section size "
4571 "in %qs clause", low_bound,
4572 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4573 return error_mark_node;
4575 if (tree_int_cst_equal (size, low_bound))
4577 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
4578 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
4580 error_at (OMP_CLAUSE_LOCATION (c),
4581 "zero length array section in %qs clause",
4582 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4583 return error_mark_node;
4585 maybe_zero_len = true;
4587 else if (length == NULL_TREE
4588 && first_non_one == types.length ()
4589 && tree_int_cst_equal
4590 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
4591 low_bound))
4592 first_non_one++;
4594 else if (length == NULL_TREE)
4596 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
4597 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION)
4598 maybe_zero_len = true;
4599 if (first_non_one == types.length ())
4600 first_non_one++;
4602 if (length && TREE_CODE (length) == INTEGER_CST)
4604 if (tree_int_cst_lt (size, length))
4606 error_at (OMP_CLAUSE_LOCATION (c),
4607 "length %qE above array section size "
4608 "in %qs clause", length,
4609 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4610 return error_mark_node;
4612 if (TREE_CODE (low_bound) == INTEGER_CST)
4614 tree lbpluslen
4615 = size_binop (PLUS_EXPR,
4616 fold_convert (sizetype, low_bound),
4617 fold_convert (sizetype, length));
4618 if (TREE_CODE (lbpluslen) == INTEGER_CST
4619 && tree_int_cst_lt (size, lbpluslen))
4621 error_at (OMP_CLAUSE_LOCATION (c),
4622 "high bound %qE above array section size "
4623 "in %qs clause", lbpluslen,
4624 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4625 return error_mark_node;
4630 else if (length == NULL_TREE)
4632 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
4633 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION)
4634 maybe_zero_len = true;
4635 if (first_non_one == types.length ())
4636 first_non_one++;
4639 /* For [lb:] we will need to evaluate lb more than once. */
4640 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
4642 tree lb = cp_save_expr (low_bound);
4643 if (lb != low_bound)
4645 TREE_PURPOSE (t) = lb;
4646 low_bound = lb;
4650 else if (TREE_CODE (type) == POINTER_TYPE)
4652 if (length == NULL_TREE)
4654 error_at (OMP_CLAUSE_LOCATION (c),
4655 "for pointer type length expression must be specified");
4656 return error_mark_node;
4658 if (length != NULL_TREE
4659 && TREE_CODE (length) == INTEGER_CST
4660 && tree_int_cst_sgn (length) == -1)
4662 error_at (OMP_CLAUSE_LOCATION (c),
4663 "negative length in array section in %qs clause",
4664 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4665 return error_mark_node;
4667 /* If there is a pointer type anywhere but in the very first
4668 array-section-subscript, the array section can't be contiguous. */
4669 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
4670 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
4672 error_at (OMP_CLAUSE_LOCATION (c),
4673 "array section is not contiguous in %qs clause",
4674 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4675 return error_mark_node;
4678 else
4680 error_at (OMP_CLAUSE_LOCATION (c),
4681 "%qE does not have pointer or array type", ret);
4682 return error_mark_node;
4684 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
4685 types.safe_push (TREE_TYPE (ret));
4686 /* We will need to evaluate lb more than once. */
4687 tree lb = cp_save_expr (low_bound);
4688 if (lb != low_bound)
4690 TREE_PURPOSE (t) = lb;
4691 low_bound = lb;
4693 ret = grok_array_decl (OMP_CLAUSE_LOCATION (c), ret, low_bound, false);
4694 return ret;
4697 /* Handle array sections for clause C. */
4699 static bool
4700 handle_omp_array_sections (tree c, bool is_omp)
4702 bool maybe_zero_len = false;
4703 unsigned int first_non_one = 0;
4704 auto_vec<tree, 10> types;
4705 tree first = handle_omp_array_sections_1 (c, OMP_CLAUSE_DECL (c), types,
4706 maybe_zero_len, first_non_one,
4707 is_omp);
4708 if (first == error_mark_node)
4709 return true;
4710 if (first == NULL_TREE)
4711 return false;
4712 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
4714 tree t = OMP_CLAUSE_DECL (c);
4715 tree tem = NULL_TREE;
4716 if (processing_template_decl)
4717 return false;
4718 /* Need to evaluate side effects in the length expressions
4719 if any. */
4720 while (TREE_CODE (t) == TREE_LIST)
4722 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
4724 if (tem == NULL_TREE)
4725 tem = TREE_VALUE (t);
4726 else
4727 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
4728 TREE_VALUE (t), tem);
4730 t = TREE_CHAIN (t);
4732 if (tem)
4733 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
4734 OMP_CLAUSE_DECL (c) = first;
4736 else
4738 unsigned int num = types.length (), i;
4739 tree t, side_effects = NULL_TREE, size = NULL_TREE;
4740 tree condition = NULL_TREE;
4742 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
4743 maybe_zero_len = true;
4744 if (processing_template_decl && maybe_zero_len)
4745 return false;
4747 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
4748 t = TREE_CHAIN (t))
4750 tree low_bound = TREE_PURPOSE (t);
4751 tree length = TREE_VALUE (t);
4753 i--;
4754 if (low_bound
4755 && TREE_CODE (low_bound) == INTEGER_CST
4756 && TYPE_PRECISION (TREE_TYPE (low_bound))
4757 > TYPE_PRECISION (sizetype))
4758 low_bound = fold_convert (sizetype, low_bound);
4759 if (length
4760 && TREE_CODE (length) == INTEGER_CST
4761 && TYPE_PRECISION (TREE_TYPE (length))
4762 > TYPE_PRECISION (sizetype))
4763 length = fold_convert (sizetype, length);
4764 if (low_bound == NULL_TREE)
4765 low_bound = integer_zero_node;
4766 if (!maybe_zero_len && i > first_non_one)
4768 if (integer_nonzerop (low_bound))
4769 goto do_warn_noncontiguous;
4770 if (length != NULL_TREE
4771 && TREE_CODE (length) == INTEGER_CST
4772 && TYPE_DOMAIN (types[i])
4773 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
4774 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
4775 == INTEGER_CST)
4777 tree size;
4778 size = size_binop (PLUS_EXPR,
4779 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
4780 size_one_node);
4781 if (!tree_int_cst_equal (length, size))
4783 do_warn_noncontiguous:
4784 error_at (OMP_CLAUSE_LOCATION (c),
4785 "array section is not contiguous in %qs "
4786 "clause",
4787 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4788 return true;
4791 if (!processing_template_decl
4792 && length != NULL_TREE
4793 && TREE_SIDE_EFFECTS (length))
4795 if (side_effects == NULL_TREE)
4796 side_effects = length;
4797 else
4798 side_effects = build2 (COMPOUND_EXPR,
4799 TREE_TYPE (side_effects),
4800 length, side_effects);
4803 else if (processing_template_decl)
4804 continue;
4805 else
4807 tree l;
4809 if (i > first_non_one
4810 && ((length && integer_nonzerop (length))
4811 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION))
4812 continue;
4813 if (length)
4814 l = fold_convert (sizetype, length);
4815 else
4817 l = size_binop (PLUS_EXPR,
4818 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
4819 size_one_node);
4820 l = size_binop (MINUS_EXPR, l,
4821 fold_convert (sizetype, low_bound));
4823 if (i > first_non_one)
4825 l = fold_build2 (NE_EXPR, boolean_type_node, l,
4826 size_zero_node);
4827 if (condition == NULL_TREE)
4828 condition = l;
4829 else
4830 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
4831 l, condition);
4833 else if (size == NULL_TREE)
4835 size = size_in_bytes (TREE_TYPE (types[i]));
4836 tree eltype = TREE_TYPE (types[num - 1]);
4837 while (TREE_CODE (eltype) == ARRAY_TYPE)
4838 eltype = TREE_TYPE (eltype);
4839 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
4840 size = size_binop (EXACT_DIV_EXPR, size,
4841 size_in_bytes (eltype));
4842 size = size_binop (MULT_EXPR, size, l);
4843 if (condition)
4844 size = fold_build3 (COND_EXPR, sizetype, condition,
4845 size, size_zero_node);
4847 else
4848 size = size_binop (MULT_EXPR, size, l);
4851 if (!processing_template_decl)
4853 if (side_effects)
4854 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
4855 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
4857 size = size_binop (MINUS_EXPR, size, size_one_node);
4858 tree index_type = build_index_type (size);
4859 tree eltype = TREE_TYPE (first);
4860 while (TREE_CODE (eltype) == ARRAY_TYPE)
4861 eltype = TREE_TYPE (eltype);
4862 tree type = build_array_type (eltype, index_type);
4863 tree ptype = build_pointer_type (eltype);
4864 if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE
4865 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (t))))
4866 t = convert_from_reference (t);
4867 else if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
4868 t = build_fold_addr_expr (t);
4869 t = build2 (MEM_REF, type, t, build_int_cst (ptype, 0));
4870 OMP_CLAUSE_DECL (c) = t;
4871 return false;
4873 OMP_CLAUSE_DECL (c) = first;
4874 OMP_CLAUSE_SIZE (c) = size;
4875 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
4876 || (TREE_CODE (t) == COMPONENT_REF
4877 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE))
4878 return false;
4879 if (is_omp)
4880 switch (OMP_CLAUSE_MAP_KIND (c))
4882 case GOMP_MAP_ALLOC:
4883 case GOMP_MAP_TO:
4884 case GOMP_MAP_FROM:
4885 case GOMP_MAP_TOFROM:
4886 case GOMP_MAP_ALWAYS_TO:
4887 case GOMP_MAP_ALWAYS_FROM:
4888 case GOMP_MAP_ALWAYS_TOFROM:
4889 case GOMP_MAP_RELEASE:
4890 case GOMP_MAP_DELETE:
4891 OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (c) = 1;
4892 break;
4893 default:
4894 break;
4896 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
4897 OMP_CLAUSE_MAP);
4898 OMP_CLAUSE_SET_MAP_KIND (c2, is_omp ? GOMP_MAP_FIRSTPRIVATE_POINTER
4899 : GOMP_MAP_POINTER);
4900 if (!is_omp && !cxx_mark_addressable (t))
4901 return false;
4902 OMP_CLAUSE_DECL (c2) = t;
4903 t = build_fold_addr_expr (first);
4904 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
4905 ptrdiff_type_node, t);
4906 tree ptr = OMP_CLAUSE_DECL (c2);
4907 ptr = convert_from_reference (ptr);
4908 if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
4909 ptr = build_fold_addr_expr (ptr);
4910 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
4911 ptrdiff_type_node, t,
4912 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
4913 ptrdiff_type_node, ptr));
4914 OMP_CLAUSE_SIZE (c2) = t;
4915 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
4916 OMP_CLAUSE_CHAIN (c) = c2;
4917 ptr = OMP_CLAUSE_DECL (c2);
4918 if (!is_omp
4919 && TREE_CODE (TREE_TYPE (ptr)) == REFERENCE_TYPE
4920 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (ptr))))
4922 tree c3 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
4923 OMP_CLAUSE_MAP);
4924 OMP_CLAUSE_SET_MAP_KIND (c3, GOMP_MAP_POINTER);
4925 OMP_CLAUSE_DECL (c3) = ptr;
4926 OMP_CLAUSE_DECL (c2) = convert_from_reference (ptr);
4927 OMP_CLAUSE_SIZE (c3) = size_zero_node;
4928 OMP_CLAUSE_CHAIN (c3) = OMP_CLAUSE_CHAIN (c2);
4929 OMP_CLAUSE_CHAIN (c2) = c3;
4933 return false;
4936 /* Return identifier to look up for omp declare reduction. */
4938 tree
4939 omp_reduction_id (enum tree_code reduction_code, tree reduction_id, tree type)
4941 const char *p = NULL;
4942 const char *m = NULL;
4943 switch (reduction_code)
4945 case PLUS_EXPR:
4946 case MULT_EXPR:
4947 case MINUS_EXPR:
4948 case BIT_AND_EXPR:
4949 case BIT_XOR_EXPR:
4950 case BIT_IOR_EXPR:
4951 case TRUTH_ANDIF_EXPR:
4952 case TRUTH_ORIF_EXPR:
4953 reduction_id = ansi_opname (reduction_code);
4954 break;
4955 case MIN_EXPR:
4956 p = "min";
4957 break;
4958 case MAX_EXPR:
4959 p = "max";
4960 break;
4961 default:
4962 break;
4965 if (p == NULL)
4967 if (TREE_CODE (reduction_id) != IDENTIFIER_NODE)
4968 return error_mark_node;
4969 p = IDENTIFIER_POINTER (reduction_id);
4972 if (type != NULL_TREE)
4973 m = mangle_type_string (TYPE_MAIN_VARIANT (type));
4975 const char prefix[] = "omp declare reduction ";
4976 size_t lenp = sizeof (prefix);
4977 if (strncmp (p, prefix, lenp - 1) == 0)
4978 lenp = 1;
4979 size_t len = strlen (p);
4980 size_t lenm = m ? strlen (m) + 1 : 0;
4981 char *name = XALLOCAVEC (char, lenp + len + lenm);
4982 if (lenp > 1)
4983 memcpy (name, prefix, lenp - 1);
4984 memcpy (name + lenp - 1, p, len + 1);
4985 if (m)
4987 name[lenp + len - 1] = '~';
4988 memcpy (name + lenp + len, m, lenm);
4990 return get_identifier (name);
4993 /* Lookup OpenMP UDR ID for TYPE, return the corresponding artificial
4994 FUNCTION_DECL or NULL_TREE if not found. */
4996 static tree
4997 omp_reduction_lookup (location_t loc, tree id, tree type, tree *baselinkp,
4998 vec<tree> *ambiguousp)
5000 tree orig_id = id;
5001 tree baselink = NULL_TREE;
5002 if (identifier_p (id))
5004 cp_id_kind idk;
5005 bool nonint_cst_expression_p;
5006 const char *error_msg;
5007 id = omp_reduction_id (ERROR_MARK, id, type);
5008 tree decl = lookup_name (id);
5009 if (decl == NULL_TREE)
5010 decl = error_mark_node;
5011 id = finish_id_expression (id, decl, NULL_TREE, &idk, false, true,
5012 &nonint_cst_expression_p, false, true, false,
5013 false, &error_msg, loc);
5014 if (idk == CP_ID_KIND_UNQUALIFIED
5015 && identifier_p (id))
5017 vec<tree, va_gc> *args = NULL;
5018 vec_safe_push (args, build_reference_type (type));
5019 id = perform_koenig_lookup (id, args, tf_none);
5022 else if (TREE_CODE (id) == SCOPE_REF)
5023 id = lookup_qualified_name (TREE_OPERAND (id, 0),
5024 omp_reduction_id (ERROR_MARK,
5025 TREE_OPERAND (id, 1),
5026 type),
5027 false, false);
5028 tree fns = id;
5029 if (id && is_overloaded_fn (id))
5030 id = get_fns (id);
5031 for (; id; id = OVL_NEXT (id))
5033 tree fndecl = OVL_CURRENT (id);
5034 if (TREE_CODE (fndecl) == FUNCTION_DECL)
5036 tree argtype = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
5037 if (same_type_p (TREE_TYPE (argtype), type))
5038 break;
5041 if (id && BASELINK_P (fns))
5043 if (baselinkp)
5044 *baselinkp = fns;
5045 else
5046 baselink = fns;
5048 if (id == NULL_TREE && CLASS_TYPE_P (type) && TYPE_BINFO (type))
5050 vec<tree> ambiguous = vNULL;
5051 tree binfo = TYPE_BINFO (type), base_binfo, ret = NULL_TREE;
5052 unsigned int ix;
5053 if (ambiguousp == NULL)
5054 ambiguousp = &ambiguous;
5055 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
5057 id = omp_reduction_lookup (loc, orig_id, BINFO_TYPE (base_binfo),
5058 baselinkp ? baselinkp : &baselink,
5059 ambiguousp);
5060 if (id == NULL_TREE)
5061 continue;
5062 if (!ambiguousp->is_empty ())
5063 ambiguousp->safe_push (id);
5064 else if (ret != NULL_TREE)
5066 ambiguousp->safe_push (ret);
5067 ambiguousp->safe_push (id);
5068 ret = NULL_TREE;
5070 else
5071 ret = id;
5073 if (ambiguousp != &ambiguous)
5074 return ret;
5075 if (!ambiguous.is_empty ())
5077 const char *str = _("candidates are:");
5078 unsigned int idx;
5079 tree udr;
5080 error_at (loc, "user defined reduction lookup is ambiguous");
5081 FOR_EACH_VEC_ELT (ambiguous, idx, udr)
5083 inform (DECL_SOURCE_LOCATION (udr), "%s %#D", str, udr);
5084 if (idx == 0)
5085 str = get_spaces (str);
5087 ambiguous.release ();
5088 ret = error_mark_node;
5089 baselink = NULL_TREE;
5091 id = ret;
5093 if (id && baselink)
5094 perform_or_defer_access_check (BASELINK_BINFO (baselink),
5095 id, id, tf_warning_or_error);
5096 return id;
5099 /* Helper function for cp_parser_omp_declare_reduction_exprs
5100 and tsubst_omp_udr.
5101 Remove CLEANUP_STMT for data (omp_priv variable).
5102 Also append INIT_EXPR for DECL_INITIAL of omp_priv after its
5103 DECL_EXPR. */
5105 tree
5106 cp_remove_omp_priv_cleanup_stmt (tree *tp, int *walk_subtrees, void *data)
5108 if (TYPE_P (*tp))
5109 *walk_subtrees = 0;
5110 else if (TREE_CODE (*tp) == CLEANUP_STMT && CLEANUP_DECL (*tp) == (tree) data)
5111 *tp = CLEANUP_BODY (*tp);
5112 else if (TREE_CODE (*tp) == DECL_EXPR)
5114 tree decl = DECL_EXPR_DECL (*tp);
5115 if (!processing_template_decl
5116 && decl == (tree) data
5117 && DECL_INITIAL (decl)
5118 && DECL_INITIAL (decl) != error_mark_node)
5120 tree list = NULL_TREE;
5121 append_to_statement_list_force (*tp, &list);
5122 tree init_expr = build2 (INIT_EXPR, void_type_node,
5123 decl, DECL_INITIAL (decl));
5124 DECL_INITIAL (decl) = NULL_TREE;
5125 append_to_statement_list_force (init_expr, &list);
5126 *tp = list;
5129 return NULL_TREE;
5132 /* Data passed from cp_check_omp_declare_reduction to
5133 cp_check_omp_declare_reduction_r. */
5135 struct cp_check_omp_declare_reduction_data
5137 location_t loc;
5138 tree stmts[7];
5139 bool combiner_p;
5142 /* Helper function for cp_check_omp_declare_reduction, called via
5143 cp_walk_tree. */
5145 static tree
5146 cp_check_omp_declare_reduction_r (tree *tp, int *, void *data)
5148 struct cp_check_omp_declare_reduction_data *udr_data
5149 = (struct cp_check_omp_declare_reduction_data *) data;
5150 if (SSA_VAR_P (*tp)
5151 && !DECL_ARTIFICIAL (*tp)
5152 && *tp != DECL_EXPR_DECL (udr_data->stmts[udr_data->combiner_p ? 0 : 3])
5153 && *tp != DECL_EXPR_DECL (udr_data->stmts[udr_data->combiner_p ? 1 : 4]))
5155 location_t loc = udr_data->loc;
5156 if (udr_data->combiner_p)
5157 error_at (loc, "%<#pragma omp declare reduction%> combiner refers to "
5158 "variable %qD which is not %<omp_out%> nor %<omp_in%>",
5159 *tp);
5160 else
5161 error_at (loc, "%<#pragma omp declare reduction%> initializer refers "
5162 "to variable %qD which is not %<omp_priv%> nor "
5163 "%<omp_orig%>",
5164 *tp);
5165 return *tp;
5167 return NULL_TREE;
5170 /* Diagnose violation of OpenMP #pragma omp declare reduction restrictions. */
5172 void
5173 cp_check_omp_declare_reduction (tree udr)
5175 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (udr)));
5176 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
5177 type = TREE_TYPE (type);
5178 int i;
5179 location_t loc = DECL_SOURCE_LOCATION (udr);
5181 if (type == error_mark_node)
5182 return;
5183 if (ARITHMETIC_TYPE_P (type))
5185 static enum tree_code predef_codes[]
5186 = { PLUS_EXPR, MULT_EXPR, MINUS_EXPR, BIT_AND_EXPR, BIT_XOR_EXPR,
5187 BIT_IOR_EXPR, TRUTH_ANDIF_EXPR, TRUTH_ORIF_EXPR };
5188 for (i = 0; i < 8; i++)
5190 tree id = omp_reduction_id (predef_codes[i], NULL_TREE, NULL_TREE);
5191 const char *n1 = IDENTIFIER_POINTER (DECL_NAME (udr));
5192 const char *n2 = IDENTIFIER_POINTER (id);
5193 if (strncmp (n1, n2, IDENTIFIER_LENGTH (id)) == 0
5194 && (n1[IDENTIFIER_LENGTH (id)] == '~'
5195 || n1[IDENTIFIER_LENGTH (id)] == '\0'))
5196 break;
5199 if (i == 8
5200 && TREE_CODE (type) != COMPLEX_EXPR)
5202 const char prefix_minmax[] = "omp declare reduction m";
5203 size_t prefix_size = sizeof (prefix_minmax) - 1;
5204 const char *n = IDENTIFIER_POINTER (DECL_NAME (udr));
5205 if (strncmp (IDENTIFIER_POINTER (DECL_NAME (udr)),
5206 prefix_minmax, prefix_size) == 0
5207 && ((n[prefix_size] == 'i' && n[prefix_size + 1] == 'n')
5208 || (n[prefix_size] == 'a' && n[prefix_size + 1] == 'x'))
5209 && (n[prefix_size + 2] == '~' || n[prefix_size + 2] == '\0'))
5210 i = 0;
5212 if (i < 8)
5214 error_at (loc, "predeclared arithmetic type %qT in "
5215 "%<#pragma omp declare reduction%>", type);
5216 return;
5219 else if (TREE_CODE (type) == FUNCTION_TYPE
5220 || TREE_CODE (type) == METHOD_TYPE
5221 || TREE_CODE (type) == ARRAY_TYPE)
5223 error_at (loc, "function or array type %qT in "
5224 "%<#pragma omp declare reduction%>", type);
5225 return;
5227 else if (TREE_CODE (type) == REFERENCE_TYPE)
5229 error_at (loc, "reference type %qT in %<#pragma omp declare reduction%>",
5230 type);
5231 return;
5233 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
5235 error_at (loc, "const, volatile or __restrict qualified type %qT in "
5236 "%<#pragma omp declare reduction%>", type);
5237 return;
5240 tree body = DECL_SAVED_TREE (udr);
5241 if (body == NULL_TREE || TREE_CODE (body) != STATEMENT_LIST)
5242 return;
5244 tree_stmt_iterator tsi;
5245 struct cp_check_omp_declare_reduction_data data;
5246 memset (data.stmts, 0, sizeof data.stmts);
5247 for (i = 0, tsi = tsi_start (body);
5248 i < 7 && !tsi_end_p (tsi);
5249 i++, tsi_next (&tsi))
5250 data.stmts[i] = tsi_stmt (tsi);
5251 data.loc = loc;
5252 gcc_assert (tsi_end_p (tsi));
5253 if (i >= 3)
5255 gcc_assert (TREE_CODE (data.stmts[0]) == DECL_EXPR
5256 && TREE_CODE (data.stmts[1]) == DECL_EXPR);
5257 if (TREE_NO_WARNING (DECL_EXPR_DECL (data.stmts[0])))
5258 return;
5259 data.combiner_p = true;
5260 if (cp_walk_tree (&data.stmts[2], cp_check_omp_declare_reduction_r,
5261 &data, NULL))
5262 TREE_NO_WARNING (DECL_EXPR_DECL (data.stmts[0])) = 1;
5264 if (i >= 6)
5266 gcc_assert (TREE_CODE (data.stmts[3]) == DECL_EXPR
5267 && TREE_CODE (data.stmts[4]) == DECL_EXPR);
5268 data.combiner_p = false;
5269 if (cp_walk_tree (&data.stmts[5], cp_check_omp_declare_reduction_r,
5270 &data, NULL)
5271 || cp_walk_tree (&DECL_INITIAL (DECL_EXPR_DECL (data.stmts[3])),
5272 cp_check_omp_declare_reduction_r, &data, NULL))
5273 TREE_NO_WARNING (DECL_EXPR_DECL (data.stmts[0])) = 1;
5274 if (i == 7)
5275 gcc_assert (TREE_CODE (data.stmts[6]) == DECL_EXPR);
5279 /* Helper function of finish_omp_clauses. Clone STMT as if we were making
5280 an inline call. But, remap
5281 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
5282 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
5284 static tree
5285 clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
5286 tree decl, tree placeholder)
5288 copy_body_data id;
5289 hash_map<tree, tree> decl_map;
5291 decl_map.put (omp_decl1, placeholder);
5292 decl_map.put (omp_decl2, decl);
5293 memset (&id, 0, sizeof (id));
5294 id.src_fn = DECL_CONTEXT (omp_decl1);
5295 id.dst_fn = current_function_decl;
5296 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
5297 id.decl_map = &decl_map;
5299 id.copy_decl = copy_decl_no_change;
5300 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
5301 id.transform_new_cfg = true;
5302 id.transform_return_to_modify = false;
5303 id.transform_lang_insert_block = NULL;
5304 id.eh_lp_nr = 0;
5305 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
5306 return stmt;
5309 /* Helper function of finish_omp_clauses, called via cp_walk_tree.
5310 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
5312 static tree
5313 find_omp_placeholder_r (tree *tp, int *, void *data)
5315 if (*tp == (tree) data)
5316 return *tp;
5317 return NULL_TREE;
5320 /* Helper function of finish_omp_clauses. Handle OMP_CLAUSE_REDUCTION C.
5321 Return true if there is some error and the clause should be removed. */
5323 static bool
5324 finish_omp_reduction_clause (tree c, bool *need_default_ctor, bool *need_dtor)
5326 tree t = OMP_CLAUSE_DECL (c);
5327 bool predefined = false;
5328 if (TREE_CODE (t) == TREE_LIST)
5330 gcc_assert (processing_template_decl);
5331 return false;
5333 tree type = TREE_TYPE (t);
5334 if (TREE_CODE (t) == MEM_REF)
5335 type = TREE_TYPE (type);
5336 if (TREE_CODE (type) == REFERENCE_TYPE)
5337 type = TREE_TYPE (type);
5338 if (TREE_CODE (type) == ARRAY_TYPE)
5340 tree oatype = type;
5341 gcc_assert (TREE_CODE (t) != MEM_REF);
5342 while (TREE_CODE (type) == ARRAY_TYPE)
5343 type = TREE_TYPE (type);
5344 if (!processing_template_decl)
5346 t = require_complete_type (t);
5347 if (t == error_mark_node)
5348 return true;
5349 tree size = size_binop (EXACT_DIV_EXPR, TYPE_SIZE_UNIT (oatype),
5350 TYPE_SIZE_UNIT (type));
5351 if (integer_zerop (size))
5353 error ("%qE in %<reduction%> clause is a zero size array",
5354 omp_clause_printable_decl (t));
5355 return true;
5357 size = size_binop (MINUS_EXPR, size, size_one_node);
5358 tree index_type = build_index_type (size);
5359 tree atype = build_array_type (type, index_type);
5360 tree ptype = build_pointer_type (type);
5361 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
5362 t = build_fold_addr_expr (t);
5363 t = build2 (MEM_REF, atype, t, build_int_cst (ptype, 0));
5364 OMP_CLAUSE_DECL (c) = t;
5367 if (type == error_mark_node)
5368 return true;
5369 else if (ARITHMETIC_TYPE_P (type))
5370 switch (OMP_CLAUSE_REDUCTION_CODE (c))
5372 case PLUS_EXPR:
5373 case MULT_EXPR:
5374 case MINUS_EXPR:
5375 predefined = true;
5376 break;
5377 case MIN_EXPR:
5378 case MAX_EXPR:
5379 if (TREE_CODE (type) == COMPLEX_TYPE)
5380 break;
5381 predefined = true;
5382 break;
5383 case BIT_AND_EXPR:
5384 case BIT_IOR_EXPR:
5385 case BIT_XOR_EXPR:
5386 if (FLOAT_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
5387 break;
5388 predefined = true;
5389 break;
5390 case TRUTH_ANDIF_EXPR:
5391 case TRUTH_ORIF_EXPR:
5392 if (FLOAT_TYPE_P (type))
5393 break;
5394 predefined = true;
5395 break;
5396 default:
5397 break;
5399 else if (TYPE_READONLY (type))
5401 error ("%qE has const type for %<reduction%>",
5402 omp_clause_printable_decl (t));
5403 return true;
5405 else if (!processing_template_decl)
5407 t = require_complete_type (t);
5408 if (t == error_mark_node)
5409 return true;
5410 OMP_CLAUSE_DECL (c) = t;
5413 if (predefined)
5415 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = NULL_TREE;
5416 return false;
5418 else if (processing_template_decl)
5419 return false;
5421 tree id = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
5423 type = TYPE_MAIN_VARIANT (type);
5424 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = NULL_TREE;
5425 if (id == NULL_TREE)
5426 id = omp_reduction_id (OMP_CLAUSE_REDUCTION_CODE (c),
5427 NULL_TREE, NULL_TREE);
5428 id = omp_reduction_lookup (OMP_CLAUSE_LOCATION (c), id, type, NULL, NULL);
5429 if (id)
5431 if (id == error_mark_node)
5432 return true;
5433 id = OVL_CURRENT (id);
5434 mark_used (id);
5435 tree body = DECL_SAVED_TREE (id);
5436 if (!body)
5437 return true;
5438 if (TREE_CODE (body) == STATEMENT_LIST)
5440 tree_stmt_iterator tsi;
5441 tree placeholder = NULL_TREE, decl_placeholder = NULL_TREE;
5442 int i;
5443 tree stmts[7];
5444 tree atype = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (id)));
5445 atype = TREE_TYPE (atype);
5446 bool need_static_cast = !same_type_p (type, atype);
5447 memset (stmts, 0, sizeof stmts);
5448 for (i = 0, tsi = tsi_start (body);
5449 i < 7 && !tsi_end_p (tsi);
5450 i++, tsi_next (&tsi))
5451 stmts[i] = tsi_stmt (tsi);
5452 gcc_assert (tsi_end_p (tsi));
5454 if (i >= 3)
5456 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
5457 && TREE_CODE (stmts[1]) == DECL_EXPR);
5458 placeholder = build_lang_decl (VAR_DECL, NULL_TREE, type);
5459 DECL_ARTIFICIAL (placeholder) = 1;
5460 DECL_IGNORED_P (placeholder) = 1;
5461 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
5462 if (TREE_CODE (t) == MEM_REF)
5464 decl_placeholder = build_lang_decl (VAR_DECL, NULL_TREE,
5465 type);
5466 DECL_ARTIFICIAL (decl_placeholder) = 1;
5467 DECL_IGNORED_P (decl_placeholder) = 1;
5468 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c) = decl_placeholder;
5470 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[0])))
5471 cxx_mark_addressable (placeholder);
5472 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[1]))
5473 && TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c)))
5474 != REFERENCE_TYPE)
5475 cxx_mark_addressable (decl_placeholder ? decl_placeholder
5476 : OMP_CLAUSE_DECL (c));
5477 tree omp_out = placeholder;
5478 tree omp_in = decl_placeholder ? decl_placeholder
5479 : convert_from_reference (OMP_CLAUSE_DECL (c));
5480 if (need_static_cast)
5482 tree rtype = build_reference_type (atype);
5483 omp_out = build_static_cast (rtype, omp_out,
5484 tf_warning_or_error);
5485 omp_in = build_static_cast (rtype, omp_in,
5486 tf_warning_or_error);
5487 if (omp_out == error_mark_node || omp_in == error_mark_node)
5488 return true;
5489 omp_out = convert_from_reference (omp_out);
5490 omp_in = convert_from_reference (omp_in);
5492 OMP_CLAUSE_REDUCTION_MERGE (c)
5493 = clone_omp_udr (stmts[2], DECL_EXPR_DECL (stmts[0]),
5494 DECL_EXPR_DECL (stmts[1]), omp_in, omp_out);
5496 if (i >= 6)
5498 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
5499 && TREE_CODE (stmts[4]) == DECL_EXPR);
5500 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[3])))
5501 cxx_mark_addressable (decl_placeholder ? decl_placeholder
5502 : OMP_CLAUSE_DECL (c));
5503 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[4])))
5504 cxx_mark_addressable (placeholder);
5505 tree omp_priv = decl_placeholder ? decl_placeholder
5506 : convert_from_reference (OMP_CLAUSE_DECL (c));
5507 tree omp_orig = placeholder;
5508 if (need_static_cast)
5510 if (i == 7)
5512 error_at (OMP_CLAUSE_LOCATION (c),
5513 "user defined reduction with constructor "
5514 "initializer for base class %qT", atype);
5515 return true;
5517 tree rtype = build_reference_type (atype);
5518 omp_priv = build_static_cast (rtype, omp_priv,
5519 tf_warning_or_error);
5520 omp_orig = build_static_cast (rtype, omp_orig,
5521 tf_warning_or_error);
5522 if (omp_priv == error_mark_node
5523 || omp_orig == error_mark_node)
5524 return true;
5525 omp_priv = convert_from_reference (omp_priv);
5526 omp_orig = convert_from_reference (omp_orig);
5528 if (i == 6)
5529 *need_default_ctor = true;
5530 OMP_CLAUSE_REDUCTION_INIT (c)
5531 = clone_omp_udr (stmts[5], DECL_EXPR_DECL (stmts[4]),
5532 DECL_EXPR_DECL (stmts[3]),
5533 omp_priv, omp_orig);
5534 if (cp_walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
5535 find_omp_placeholder_r, placeholder, NULL))
5536 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
5538 else if (i >= 3)
5540 if (CLASS_TYPE_P (type) && !pod_type_p (type))
5541 *need_default_ctor = true;
5542 else
5544 tree init;
5545 tree v = decl_placeholder ? decl_placeholder
5546 : convert_from_reference (t);
5547 if (AGGREGATE_TYPE_P (TREE_TYPE (v)))
5548 init = build_constructor (TREE_TYPE (v), NULL);
5549 else
5550 init = fold_convert (TREE_TYPE (v), integer_zero_node);
5551 OMP_CLAUSE_REDUCTION_INIT (c)
5552 = build2 (INIT_EXPR, TREE_TYPE (v), v, init);
5557 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
5558 *need_dtor = true;
5559 else
5561 error ("user defined reduction not found for %qE",
5562 omp_clause_printable_decl (t));
5563 return true;
5565 if (TREE_CODE (OMP_CLAUSE_DECL (c)) == MEM_REF)
5566 gcc_assert (TYPE_SIZE_UNIT (type)
5567 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST);
5568 return false;
5571 /* Called from finish_struct_1. linear(this) or linear(this:step)
5572 clauses might not be finalized yet because the class has been incomplete
5573 when parsing #pragma omp declare simd methods. Fix those up now. */
5575 void
5576 finish_omp_declare_simd_methods (tree t)
5578 if (processing_template_decl)
5579 return;
5581 for (tree x = TYPE_METHODS (t); x; x = DECL_CHAIN (x))
5583 if (TREE_CODE (TREE_TYPE (x)) != METHOD_TYPE)
5584 continue;
5585 tree ods = lookup_attribute ("omp declare simd", DECL_ATTRIBUTES (x));
5586 if (!ods || !TREE_VALUE (ods))
5587 continue;
5588 for (tree c = TREE_VALUE (TREE_VALUE (ods)); c; c = OMP_CLAUSE_CHAIN (c))
5589 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
5590 && integer_zerop (OMP_CLAUSE_DECL (c))
5591 && OMP_CLAUSE_LINEAR_STEP (c)
5592 && TREE_CODE (TREE_TYPE (OMP_CLAUSE_LINEAR_STEP (c)))
5593 == POINTER_TYPE)
5595 tree s = OMP_CLAUSE_LINEAR_STEP (c);
5596 s = fold_convert_loc (OMP_CLAUSE_LOCATION (c), sizetype, s);
5597 s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MULT_EXPR,
5598 sizetype, s, TYPE_SIZE_UNIT (t));
5599 OMP_CLAUSE_LINEAR_STEP (c) = s;
5604 /* Adjust sink depend clause to take into account pointer offsets.
5606 Return TRUE if there was a problem processing the offset, and the
5607 whole clause should be removed. */
5609 static bool
5610 cp_finish_omp_clause_depend_sink (tree sink_clause)
5612 tree t = OMP_CLAUSE_DECL (sink_clause);
5613 gcc_assert (TREE_CODE (t) == TREE_LIST);
5615 /* Make sure we don't adjust things twice for templates. */
5616 if (processing_template_decl)
5617 return false;
5619 for (; t; t = TREE_CHAIN (t))
5621 tree decl = TREE_VALUE (t);
5622 if (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
5624 tree offset = TREE_PURPOSE (t);
5625 bool neg = wi::neg_p ((wide_int) offset);
5626 offset = fold_unary (ABS_EXPR, TREE_TYPE (offset), offset);
5627 decl = mark_rvalue_use (decl);
5628 decl = convert_from_reference (decl);
5629 tree t2 = pointer_int_sum (OMP_CLAUSE_LOCATION (sink_clause),
5630 neg ? MINUS_EXPR : PLUS_EXPR,
5631 decl, offset);
5632 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (sink_clause),
5633 MINUS_EXPR, sizetype, t2,
5634 decl);
5635 if (t2 == error_mark_node)
5636 return true;
5637 TREE_PURPOSE (t) = t2;
5640 return false;
5643 /* For all elements of CLAUSES, validate them vs OpenMP constraints.
5644 Remove any elements from the list that are invalid. */
5646 tree
5647 finish_omp_clauses (tree clauses, bool allow_fields, bool declare_simd)
5649 bitmap_head generic_head, firstprivate_head, lastprivate_head;
5650 bitmap_head aligned_head, map_head, map_field_head, generic_field_head;
5651 tree c, t, *pc;
5652 tree safelen = NULL_TREE;
5653 bool branch_seen = false;
5654 bool copyprivate_seen = false;
5656 bitmap_obstack_initialize (NULL);
5657 bitmap_initialize (&generic_head, &bitmap_default_obstack);
5658 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
5659 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
5660 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
5661 bitmap_initialize (&map_head, &bitmap_default_obstack);
5662 bitmap_initialize (&map_field_head, &bitmap_default_obstack);
5663 bitmap_initialize (&generic_field_head, &bitmap_default_obstack);
5665 for (pc = &clauses, c = clauses; c ; c = *pc)
5667 bool remove = false;
5668 bool field_ok = false;
5670 switch (OMP_CLAUSE_CODE (c))
5672 case OMP_CLAUSE_SHARED:
5673 goto check_dup_generic;
5674 case OMP_CLAUSE_PRIVATE:
5675 field_ok = allow_fields;
5676 goto check_dup_generic;
5677 case OMP_CLAUSE_REDUCTION:
5678 field_ok = allow_fields;
5679 t = OMP_CLAUSE_DECL (c);
5680 if (TREE_CODE (t) == TREE_LIST)
5682 if (handle_omp_array_sections (c, allow_fields))
5684 remove = true;
5685 break;
5687 if (TREE_CODE (t) == TREE_LIST)
5689 while (TREE_CODE (t) == TREE_LIST)
5690 t = TREE_CHAIN (t);
5692 else
5694 gcc_assert (TREE_CODE (t) == MEM_REF);
5695 t = TREE_OPERAND (t, 0);
5696 if (TREE_CODE (t) == ADDR_EXPR
5697 || TREE_CODE (t) == INDIRECT_REF)
5698 t = TREE_OPERAND (t, 0);
5700 tree n = omp_clause_decl_field (t);
5701 if (n)
5702 t = n;
5703 goto check_dup_generic_t;
5705 goto check_dup_generic;
5706 case OMP_CLAUSE_COPYPRIVATE:
5707 copyprivate_seen = true;
5708 field_ok = allow_fields;
5709 goto check_dup_generic;
5710 case OMP_CLAUSE_COPYIN:
5711 goto check_dup_generic;
5712 case OMP_CLAUSE_LINEAR:
5713 field_ok = allow_fields;
5714 t = OMP_CLAUSE_DECL (c);
5715 if (!declare_simd
5716 && OMP_CLAUSE_LINEAR_KIND (c) != OMP_CLAUSE_LINEAR_DEFAULT)
5718 error_at (OMP_CLAUSE_LOCATION (c),
5719 "modifier should not be specified in %<linear%> "
5720 "clause on %<simd%> or %<for%> constructs");
5721 OMP_CLAUSE_LINEAR_KIND (c) = OMP_CLAUSE_LINEAR_DEFAULT;
5723 if ((VAR_P (t) || TREE_CODE (t) == PARM_DECL)
5724 && !type_dependent_expression_p (t))
5726 tree type = TREE_TYPE (t);
5727 if ((OMP_CLAUSE_LINEAR_KIND (c) == OMP_CLAUSE_LINEAR_REF
5728 || OMP_CLAUSE_LINEAR_KIND (c) == OMP_CLAUSE_LINEAR_UVAL)
5729 && TREE_CODE (type) != REFERENCE_TYPE)
5731 error ("linear clause with %qs modifier applied to "
5732 "non-reference variable with %qT type",
5733 OMP_CLAUSE_LINEAR_KIND (c) == OMP_CLAUSE_LINEAR_REF
5734 ? "ref" : "uval", TREE_TYPE (t));
5735 remove = true;
5736 break;
5738 if (TREE_CODE (type) == REFERENCE_TYPE)
5739 type = TREE_TYPE (type);
5740 if (!INTEGRAL_TYPE_P (type)
5741 && TREE_CODE (type) != POINTER_TYPE)
5743 error ("linear clause applied to non-integral non-pointer "
5744 "variable with %qT type", TREE_TYPE (t));
5745 remove = true;
5746 break;
5749 t = OMP_CLAUSE_LINEAR_STEP (c);
5750 if (t == NULL_TREE)
5751 t = integer_one_node;
5752 if (t == error_mark_node)
5754 remove = true;
5755 break;
5757 else if (!type_dependent_expression_p (t)
5758 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5760 error ("linear step expression must be integral");
5761 remove = true;
5762 break;
5764 else
5766 t = mark_rvalue_use (t);
5767 if (!processing_template_decl
5768 && (VAR_P (OMP_CLAUSE_DECL (c))
5769 || TREE_CODE (OMP_CLAUSE_DECL (c)) == PARM_DECL))
5771 if (TREE_CODE (OMP_CLAUSE_DECL (c)) == PARM_DECL)
5772 t = maybe_constant_value (t);
5773 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5774 tree type = TREE_TYPE (OMP_CLAUSE_DECL (c));
5775 if (TREE_CODE (type) == REFERENCE_TYPE)
5776 type = TREE_TYPE (type);
5777 if (OMP_CLAUSE_LINEAR_KIND (c) == OMP_CLAUSE_LINEAR_REF)
5779 type = build_pointer_type (type);
5780 tree d = fold_convert (type, OMP_CLAUSE_DECL (c));
5781 t = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
5782 d, t);
5783 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c),
5784 MINUS_EXPR, sizetype, t, d);
5785 if (t == error_mark_node)
5787 remove = true;
5788 break;
5791 else if (TREE_CODE (type) == POINTER_TYPE
5792 /* Can't multiply the step yet if *this
5793 is still incomplete type. */
5794 && (!declare_simd
5795 || TREE_CODE (OMP_CLAUSE_DECL (c)) != PARM_DECL
5796 || !DECL_ARTIFICIAL (OMP_CLAUSE_DECL (c))
5797 || DECL_NAME (OMP_CLAUSE_DECL (c))
5798 != this_identifier
5799 || !TYPE_BEING_DEFINED (TREE_TYPE (type))))
5801 tree d = convert_from_reference (OMP_CLAUSE_DECL (c));
5802 t = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
5803 d, t);
5804 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c),
5805 MINUS_EXPR, sizetype, t, d);
5806 if (t == error_mark_node)
5808 remove = true;
5809 break;
5812 else
5813 t = fold_convert (type, t);
5815 OMP_CLAUSE_LINEAR_STEP (c) = t;
5817 goto check_dup_generic;
5818 check_dup_generic:
5819 t = omp_clause_decl_field (OMP_CLAUSE_DECL (c));
5820 if (t)
5822 if (!remove)
5823 omp_note_field_privatization (t, OMP_CLAUSE_DECL (c));
5825 else
5826 t = OMP_CLAUSE_DECL (c);
5827 check_dup_generic_t:
5828 if (t == current_class_ptr
5829 && (!declare_simd
5830 || (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_LINEAR
5831 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_UNIFORM)))
5833 error ("%<this%> allowed in OpenMP only in %<declare simd%>"
5834 " clauses");
5835 remove = true;
5836 break;
5838 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL
5839 && (!field_ok || TREE_CODE (t) != FIELD_DECL))
5841 if (processing_template_decl)
5842 break;
5843 if (DECL_P (t))
5844 error ("%qD is not a variable in clause %qs", t,
5845 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5846 else
5847 error ("%qE is not a variable in clause %qs", t,
5848 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5849 remove = true;
5851 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
5852 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
5853 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
5855 error ("%qD appears more than once in data clauses", t);
5856 remove = true;
5858 else
5859 bitmap_set_bit (&generic_head, DECL_UID (t));
5860 if (!field_ok)
5861 break;
5862 handle_field_decl:
5863 if (!remove
5864 && TREE_CODE (t) == FIELD_DECL
5865 && t == OMP_CLAUSE_DECL (c))
5867 OMP_CLAUSE_DECL (c) = omp_privatize_field (t);
5868 if (OMP_CLAUSE_DECL (c) == error_mark_node)
5869 remove = true;
5871 break;
5873 case OMP_CLAUSE_FIRSTPRIVATE:
5874 t = omp_clause_decl_field (OMP_CLAUSE_DECL (c));
5875 if (t)
5876 omp_note_field_privatization (t, OMP_CLAUSE_DECL (c));
5877 else
5878 t = OMP_CLAUSE_DECL (c);
5879 if (t == current_class_ptr)
5881 error ("%<this%> allowed in OpenMP only in %<declare simd%>"
5882 " clauses");
5883 remove = true;
5884 break;
5886 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL
5887 && (!allow_fields || TREE_CODE (t) != FIELD_DECL))
5889 if (processing_template_decl)
5890 break;
5891 if (DECL_P (t))
5892 error ("%qD is not a variable in clause %<firstprivate%>", t);
5893 else
5894 error ("%qE is not a variable in clause %<firstprivate%>", t);
5895 remove = true;
5897 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
5898 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
5900 error ("%qD appears more than once in data clauses", t);
5901 remove = true;
5903 else
5904 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
5905 goto handle_field_decl;
5907 case OMP_CLAUSE_LASTPRIVATE:
5908 t = omp_clause_decl_field (OMP_CLAUSE_DECL (c));
5909 if (t)
5910 omp_note_field_privatization (t, OMP_CLAUSE_DECL (c));
5911 else
5912 t = OMP_CLAUSE_DECL (c);
5913 if (t == current_class_ptr)
5915 error ("%<this%> allowed in OpenMP only in %<declare simd%>"
5916 " clauses");
5917 remove = true;
5918 break;
5920 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL
5921 && (!allow_fields || TREE_CODE (t) != FIELD_DECL))
5923 if (processing_template_decl)
5924 break;
5925 if (DECL_P (t))
5926 error ("%qD is not a variable in clause %<lastprivate%>", t);
5927 else
5928 error ("%qE is not a variable in clause %<lastprivate%>", t);
5929 remove = true;
5931 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
5932 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
5934 error ("%qD appears more than once in data clauses", t);
5935 remove = true;
5937 else
5938 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
5939 goto handle_field_decl;
5941 case OMP_CLAUSE_IF:
5942 t = OMP_CLAUSE_IF_EXPR (c);
5943 t = maybe_convert_cond (t);
5944 if (t == error_mark_node)
5945 remove = true;
5946 else if (!processing_template_decl)
5947 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5948 OMP_CLAUSE_IF_EXPR (c) = t;
5949 break;
5951 case OMP_CLAUSE_FINAL:
5952 t = OMP_CLAUSE_FINAL_EXPR (c);
5953 t = maybe_convert_cond (t);
5954 if (t == error_mark_node)
5955 remove = true;
5956 else if (!processing_template_decl)
5957 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5958 OMP_CLAUSE_FINAL_EXPR (c) = t;
5959 break;
5961 case OMP_CLAUSE_NUM_THREADS:
5962 t = OMP_CLAUSE_NUM_THREADS_EXPR (c);
5963 if (t == error_mark_node)
5964 remove = true;
5965 else if (!type_dependent_expression_p (t)
5966 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5968 error ("num_threads expression must be integral");
5969 remove = true;
5971 else
5973 t = mark_rvalue_use (t);
5974 if (!processing_template_decl)
5976 t = maybe_constant_value (t);
5977 if (TREE_CODE (t) == INTEGER_CST
5978 && tree_int_cst_sgn (t) != 1)
5980 warning_at (OMP_CLAUSE_LOCATION (c), 0,
5981 "%<num_threads%> value must be positive");
5982 t = integer_one_node;
5984 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5986 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
5988 break;
5990 case OMP_CLAUSE_SCHEDULE:
5991 t = OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c);
5992 if (t == NULL)
5994 else if (t == error_mark_node)
5995 remove = true;
5996 else if (!type_dependent_expression_p (t)
5997 && (OMP_CLAUSE_SCHEDULE_KIND (c)
5998 != OMP_CLAUSE_SCHEDULE_CILKFOR)
5999 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6001 error ("schedule chunk size expression must be integral");
6002 remove = true;
6004 else
6006 t = mark_rvalue_use (t);
6007 if (!processing_template_decl)
6009 if (OMP_CLAUSE_SCHEDULE_KIND (c)
6010 == OMP_CLAUSE_SCHEDULE_CILKFOR)
6012 t = convert_to_integer (long_integer_type_node, t);
6013 if (t == error_mark_node)
6015 remove = true;
6016 break;
6019 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6021 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
6023 break;
6025 case OMP_CLAUSE_SIMDLEN:
6026 case OMP_CLAUSE_SAFELEN:
6027 t = OMP_CLAUSE_OPERAND (c, 0);
6028 if (t == error_mark_node)
6029 remove = true;
6030 else if (!type_dependent_expression_p (t)
6031 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6033 error ("%qs length expression must be integral",
6034 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6035 remove = true;
6037 else
6039 t = mark_rvalue_use (t);
6040 t = maybe_constant_value (t);
6041 if (!processing_template_decl)
6043 if (TREE_CODE (t) != INTEGER_CST
6044 || tree_int_cst_sgn (t) != 1)
6046 error ("%qs length expression must be positive constant"
6047 " integer expression",
6048 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6049 remove = true;
6052 OMP_CLAUSE_OPERAND (c, 0) = t;
6053 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SAFELEN)
6054 safelen = c;
6056 break;
6058 case OMP_CLAUSE_NUM_TEAMS:
6059 t = OMP_CLAUSE_NUM_TEAMS_EXPR (c);
6060 if (t == error_mark_node)
6061 remove = true;
6062 else if (!type_dependent_expression_p (t)
6063 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6065 error ("%<num_teams%> expression must be integral");
6066 remove = true;
6068 else
6070 t = mark_rvalue_use (t);
6071 if (!processing_template_decl)
6073 t = maybe_constant_value (t);
6074 if (TREE_CODE (t) == INTEGER_CST
6075 && tree_int_cst_sgn (t) != 1)
6077 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6078 "%<num_teams%> value must be positive");
6079 t = integer_one_node;
6081 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6083 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
6085 break;
6087 case OMP_CLAUSE_ASYNC:
6088 t = OMP_CLAUSE_ASYNC_EXPR (c);
6089 if (t == error_mark_node)
6090 remove = true;
6091 else if (!type_dependent_expression_p (t)
6092 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6094 error ("%<async%> expression must be integral");
6095 remove = true;
6097 else
6099 t = mark_rvalue_use (t);
6100 if (!processing_template_decl)
6101 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6102 OMP_CLAUSE_ASYNC_EXPR (c) = t;
6104 break;
6106 case OMP_CLAUSE_VECTOR_LENGTH:
6107 t = OMP_CLAUSE_VECTOR_LENGTH_EXPR (c);
6108 t = maybe_convert_cond (t);
6109 if (t == error_mark_node)
6110 remove = true;
6111 else if (!processing_template_decl)
6112 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6113 OMP_CLAUSE_VECTOR_LENGTH_EXPR (c) = t;
6114 break;
6116 case OMP_CLAUSE_WAIT:
6117 t = OMP_CLAUSE_WAIT_EXPR (c);
6118 if (t == error_mark_node)
6119 remove = true;
6120 else if (!processing_template_decl)
6121 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6122 OMP_CLAUSE_WAIT_EXPR (c) = t;
6123 break;
6125 case OMP_CLAUSE_THREAD_LIMIT:
6126 t = OMP_CLAUSE_THREAD_LIMIT_EXPR (c);
6127 if (t == error_mark_node)
6128 remove = true;
6129 else if (!type_dependent_expression_p (t)
6130 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6132 error ("%<thread_limit%> expression must be integral");
6133 remove = true;
6135 else
6137 t = mark_rvalue_use (t);
6138 if (!processing_template_decl)
6140 t = maybe_constant_value (t);
6141 if (TREE_CODE (t) == INTEGER_CST
6142 && tree_int_cst_sgn (t) != 1)
6144 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6145 "%<thread_limit%> value must be positive");
6146 t = integer_one_node;
6148 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6150 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
6152 break;
6154 case OMP_CLAUSE_DEVICE:
6155 t = OMP_CLAUSE_DEVICE_ID (c);
6156 if (t == error_mark_node)
6157 remove = true;
6158 else if (!type_dependent_expression_p (t)
6159 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6161 error ("%<device%> id must be integral");
6162 remove = true;
6164 else
6166 t = mark_rvalue_use (t);
6167 if (!processing_template_decl)
6168 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6169 OMP_CLAUSE_DEVICE_ID (c) = t;
6171 break;
6173 case OMP_CLAUSE_DIST_SCHEDULE:
6174 t = OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c);
6175 if (t == NULL)
6177 else if (t == error_mark_node)
6178 remove = true;
6179 else if (!type_dependent_expression_p (t)
6180 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6182 error ("%<dist_schedule%> chunk size expression must be "
6183 "integral");
6184 remove = true;
6186 else
6188 t = mark_rvalue_use (t);
6189 if (!processing_template_decl)
6190 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6191 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
6193 break;
6195 case OMP_CLAUSE_ALIGNED:
6196 t = OMP_CLAUSE_DECL (c);
6197 if (t == current_class_ptr && !declare_simd)
6199 error ("%<this%> allowed in OpenMP only in %<declare simd%>"
6200 " clauses");
6201 remove = true;
6202 break;
6204 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
6206 if (processing_template_decl)
6207 break;
6208 if (DECL_P (t))
6209 error ("%qD is not a variable in %<aligned%> clause", t);
6210 else
6211 error ("%qE is not a variable in %<aligned%> clause", t);
6212 remove = true;
6214 else if (!type_dependent_expression_p (t)
6215 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE
6216 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE
6217 && (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE
6218 || (!POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (t)))
6219 && (TREE_CODE (TREE_TYPE (TREE_TYPE (t)))
6220 != ARRAY_TYPE))))
6222 error_at (OMP_CLAUSE_LOCATION (c),
6223 "%qE in %<aligned%> clause is neither a pointer nor "
6224 "an array nor a reference to pointer or array", t);
6225 remove = true;
6227 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
6229 error ("%qD appears more than once in %<aligned%> clauses", t);
6230 remove = true;
6232 else
6233 bitmap_set_bit (&aligned_head, DECL_UID (t));
6234 t = OMP_CLAUSE_ALIGNED_ALIGNMENT (c);
6235 if (t == error_mark_node)
6236 remove = true;
6237 else if (t == NULL_TREE)
6238 break;
6239 else if (!type_dependent_expression_p (t)
6240 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6242 error ("%<aligned%> clause alignment expression must "
6243 "be integral");
6244 remove = true;
6246 else
6248 t = mark_rvalue_use (t);
6249 t = maybe_constant_value (t);
6250 if (!processing_template_decl)
6252 if (TREE_CODE (t) != INTEGER_CST
6253 || tree_int_cst_sgn (t) != 1)
6255 error ("%<aligned%> clause alignment expression must be "
6256 "positive constant integer expression");
6257 remove = true;
6260 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = t;
6262 break;
6264 case OMP_CLAUSE_DEPEND:
6265 t = OMP_CLAUSE_DECL (c);
6266 if (t == NULL_TREE)
6268 gcc_assert (OMP_CLAUSE_DEPEND_KIND (c)
6269 == OMP_CLAUSE_DEPEND_SOURCE);
6270 break;
6272 if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SINK)
6274 if (cp_finish_omp_clause_depend_sink (c))
6275 remove = true;
6276 break;
6278 if (TREE_CODE (t) == TREE_LIST)
6280 if (handle_omp_array_sections (c, allow_fields))
6281 remove = true;
6282 break;
6284 if (t == error_mark_node)
6285 remove = true;
6286 else if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
6288 if (processing_template_decl)
6289 break;
6290 if (DECL_P (t))
6291 error ("%qD is not a variable in %<depend%> clause", t);
6292 else
6293 error ("%qE is not a variable in %<depend%> clause", t);
6294 remove = true;
6296 else if (t == current_class_ptr)
6298 error ("%<this%> allowed in OpenMP only in %<declare simd%>"
6299 " clauses");
6300 remove = true;
6302 else if (!processing_template_decl
6303 && !cxx_mark_addressable (t))
6304 remove = true;
6305 break;
6307 case OMP_CLAUSE_MAP:
6308 case OMP_CLAUSE_TO:
6309 case OMP_CLAUSE_FROM:
6310 case OMP_CLAUSE__CACHE_:
6311 t = OMP_CLAUSE_DECL (c);
6312 if (TREE_CODE (t) == TREE_LIST)
6314 if (handle_omp_array_sections (c, allow_fields))
6315 remove = true;
6316 else
6318 t = OMP_CLAUSE_DECL (c);
6319 if (TREE_CODE (t) != TREE_LIST
6320 && !type_dependent_expression_p (t)
6321 && !cp_omp_mappable_type (TREE_TYPE (t)))
6323 error_at (OMP_CLAUSE_LOCATION (c),
6324 "array section does not have mappable type "
6325 "in %qs clause",
6326 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6327 remove = true;
6329 while (TREE_CODE (t) == ARRAY_REF)
6330 t = TREE_OPERAND (t, 0);
6331 if (TREE_CODE (t) == COMPONENT_REF
6332 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
6334 while (TREE_CODE (t) == COMPONENT_REF)
6335 t = TREE_OPERAND (t, 0);
6336 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
6337 break;
6338 if (bitmap_bit_p (&map_head, DECL_UID (t)))
6340 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
6341 error ("%qD appears more than once in motion"
6342 " clauses", t);
6343 else
6344 error ("%qD appears more than once in map"
6345 " clauses", t);
6346 remove = true;
6348 else
6350 bitmap_set_bit (&map_head, DECL_UID (t));
6351 bitmap_set_bit (&map_field_head, DECL_UID (t));
6355 break;
6357 if (t == error_mark_node)
6359 remove = true;
6360 break;
6362 if (REFERENCE_REF_P (t)
6363 && TREE_CODE (TREE_OPERAND (t, 0)) == COMPONENT_REF)
6364 t = TREE_OPERAND (t, 0);
6365 if (TREE_CODE (t) == COMPONENT_REF
6366 && allow_fields
6367 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE__CACHE_)
6369 if (type_dependent_expression_p (t))
6370 break;
6371 if (DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
6373 error_at (OMP_CLAUSE_LOCATION (c),
6374 "bit-field %qE in %qs clause",
6375 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6376 remove = true;
6378 else if (!cp_omp_mappable_type (TREE_TYPE (t)))
6380 error_at (OMP_CLAUSE_LOCATION (c),
6381 "%qE does not have a mappable type in %qs clause",
6382 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6383 remove = true;
6385 while (TREE_CODE (t) == COMPONENT_REF)
6387 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0)))
6388 == UNION_TYPE)
6390 error_at (OMP_CLAUSE_LOCATION (c),
6391 "%qE is a member of a union", t);
6392 remove = true;
6393 break;
6395 t = TREE_OPERAND (t, 0);
6397 if (remove)
6398 break;
6399 if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
6401 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
6402 && (OMP_CLAUSE_MAP_KIND (c)
6403 == GOMP_MAP_FIRSTPRIVATE_POINTER))
6405 if (bitmap_bit_p (&generic_field_head, DECL_UID (t)))
6406 break;
6408 else if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
6409 break;
6412 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
6414 if (processing_template_decl)
6415 break;
6416 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
6417 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER)
6418 break;
6419 if (DECL_P (t))
6420 error ("%qD is not a variable in %qs clause", t,
6421 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6422 else
6423 error ("%qE is not a variable in %qs clause", t,
6424 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6425 remove = true;
6427 else if (VAR_P (t) && CP_DECL_THREAD_LOCAL_P (t))
6429 error ("%qD is threadprivate variable in %qs clause", t,
6430 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6431 remove = true;
6433 else if (t == current_class_ptr)
6435 error ("%<this%> allowed in OpenMP only in %<declare simd%>"
6436 " clauses");
6437 remove = true;
6438 break;
6440 else if (!processing_template_decl
6441 && TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE
6442 && !cxx_mark_addressable (t))
6443 remove = true;
6444 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
6445 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
6446 || (OMP_CLAUSE_MAP_KIND (c)
6447 == GOMP_MAP_FIRSTPRIVATE_POINTER)))
6448 && t == OMP_CLAUSE_DECL (c)
6449 && !type_dependent_expression_p (t)
6450 && !cp_omp_mappable_type ((TREE_CODE (TREE_TYPE (t))
6451 == REFERENCE_TYPE)
6452 ? TREE_TYPE (TREE_TYPE (t))
6453 : TREE_TYPE (t)))
6455 error_at (OMP_CLAUSE_LOCATION (c),
6456 "%qD does not have a mappable type in %qs clause", t,
6457 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6458 remove = true;
6460 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
6461 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
6463 if (bitmap_bit_p (&generic_head, DECL_UID (t))
6464 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
6466 error ("%qD appears more than once in data clauses", t);
6467 remove = true;
6469 else
6471 bitmap_set_bit (&generic_head, DECL_UID (t));
6472 if (t != OMP_CLAUSE_DECL (c)
6473 && (TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPONENT_REF
6474 || (REFERENCE_REF_P (OMP_CLAUSE_DECL (c))
6475 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (c),
6477 == COMPONENT_REF))))
6478 bitmap_set_bit (&generic_field_head, DECL_UID (t));
6481 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
6483 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
6484 error ("%qD appears more than once in motion clauses", t);
6485 else
6486 error ("%qD appears more than once in map clauses", t);
6487 remove = true;
6489 else
6491 bitmap_set_bit (&map_head, DECL_UID (t));
6492 if (t != OMP_CLAUSE_DECL (c)
6493 && TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPONENT_REF)
6494 bitmap_set_bit (&map_field_head, DECL_UID (t));
6496 break;
6498 case OMP_CLAUSE_TO_DECLARE:
6499 t = OMP_CLAUSE_DECL (c);
6500 if (TREE_CODE (t) == FUNCTION_DECL)
6501 break;
6502 /* FALLTHRU */
6503 case OMP_CLAUSE_LINK:
6504 t = OMP_CLAUSE_DECL (c);
6505 if (!VAR_P (t))
6507 error_at (OMP_CLAUSE_LOCATION (c),
6508 "%qE is not a variable in clause %qs", t,
6509 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6510 remove = true;
6512 else if (DECL_THREAD_LOCAL_P (t))
6514 error_at (OMP_CLAUSE_LOCATION (c),
6515 "%qD is threadprivate variable in %qs clause", t,
6516 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6517 remove = true;
6519 else if (!cp_omp_mappable_type (TREE_TYPE (t)))
6521 error_at (OMP_CLAUSE_LOCATION (c),
6522 "%qD does not have a mappable type in %qs clause", t,
6523 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6524 remove = true;
6526 break;
6528 case OMP_CLAUSE_UNIFORM:
6529 t = OMP_CLAUSE_DECL (c);
6530 if (TREE_CODE (t) != PARM_DECL)
6532 if (processing_template_decl)
6533 break;
6534 if (DECL_P (t))
6535 error ("%qD is not an argument in %<uniform%> clause", t);
6536 else
6537 error ("%qE is not an argument in %<uniform%> clause", t);
6538 remove = true;
6539 break;
6541 goto check_dup_generic;
6543 case OMP_CLAUSE_NUM_TASKS:
6544 t = OMP_CLAUSE_NUM_TASKS_EXPR (c);
6545 if (t == error_mark_node)
6546 remove = true;
6547 else if (!type_dependent_expression_p (t)
6548 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6550 error ("%<num_tasks%> expression must be integral");
6551 remove = true;
6553 else
6555 t = mark_rvalue_use (t);
6556 if (!processing_template_decl)
6558 t = maybe_constant_value (t);
6559 if (TREE_CODE (t) == INTEGER_CST
6560 && tree_int_cst_sgn (t) != 1)
6562 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6563 "%<num_tasks%> value must be positive");
6564 t = integer_one_node;
6566 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6568 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
6570 break;
6572 case OMP_CLAUSE_GRAINSIZE:
6573 t = OMP_CLAUSE_GRAINSIZE_EXPR (c);
6574 if (t == error_mark_node)
6575 remove = true;
6576 else if (!type_dependent_expression_p (t)
6577 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6579 error ("%<grainsize%> expression must be integral");
6580 remove = true;
6582 else
6584 t = mark_rvalue_use (t);
6585 if (!processing_template_decl)
6587 t = maybe_constant_value (t);
6588 if (TREE_CODE (t) == INTEGER_CST
6589 && tree_int_cst_sgn (t) != 1)
6591 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6592 "%<grainsize%> value must be positive");
6593 t = integer_one_node;
6595 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6597 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
6599 break;
6601 case OMP_CLAUSE_PRIORITY:
6602 t = OMP_CLAUSE_PRIORITY_EXPR (c);
6603 if (t == error_mark_node)
6604 remove = true;
6605 else if (!type_dependent_expression_p (t)
6606 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6608 error ("%<priority%> expression must be integral");
6609 remove = true;
6611 else
6613 t = mark_rvalue_use (t);
6614 if (!processing_template_decl)
6616 t = maybe_constant_value (t);
6617 if (TREE_CODE (t) == INTEGER_CST
6618 && tree_int_cst_sgn (t) == -1)
6620 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6621 "%<priority%> value must be non-negative");
6622 t = integer_one_node;
6624 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6626 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
6628 break;
6630 case OMP_CLAUSE_HINT:
6631 t = OMP_CLAUSE_HINT_EXPR (c);
6632 if (t == error_mark_node)
6633 remove = true;
6634 else if (!type_dependent_expression_p (t)
6635 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6637 error ("%<num_tasks%> expression must be integral");
6638 remove = true;
6640 else
6642 t = mark_rvalue_use (t);
6643 if (!processing_template_decl)
6645 t = maybe_constant_value (t);
6646 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6648 OMP_CLAUSE_HINT_EXPR (c) = t;
6650 break;
6652 case OMP_CLAUSE_IS_DEVICE_PTR:
6653 case OMP_CLAUSE_USE_DEVICE_PTR:
6654 field_ok = allow_fields;
6655 t = OMP_CLAUSE_DECL (c);
6656 if (!type_dependent_expression_p (t))
6658 tree type = TREE_TYPE (t);
6659 if (TREE_CODE (type) != POINTER_TYPE
6660 && TREE_CODE (type) != ARRAY_TYPE
6661 && (TREE_CODE (type) != REFERENCE_TYPE
6662 || (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
6663 && TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE)))
6665 error_at (OMP_CLAUSE_LOCATION (c),
6666 "%qs variable is neither a pointer, nor an array"
6667 "nor reference to pointer or array",
6668 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6669 remove = true;
6672 goto check_dup_generic;
6674 case OMP_CLAUSE_NOWAIT:
6675 case OMP_CLAUSE_ORDERED:
6676 case OMP_CLAUSE_DEFAULT:
6677 case OMP_CLAUSE_UNTIED:
6678 case OMP_CLAUSE_COLLAPSE:
6679 case OMP_CLAUSE_MERGEABLE:
6680 case OMP_CLAUSE_PARALLEL:
6681 case OMP_CLAUSE_FOR:
6682 case OMP_CLAUSE_SECTIONS:
6683 case OMP_CLAUSE_TASKGROUP:
6684 case OMP_CLAUSE_PROC_BIND:
6685 case OMP_CLAUSE_NOGROUP:
6686 case OMP_CLAUSE_THREADS:
6687 case OMP_CLAUSE_SIMD:
6688 case OMP_CLAUSE_DEFAULTMAP:
6689 case OMP_CLAUSE__CILK_FOR_COUNT_:
6690 break;
6692 case OMP_CLAUSE_INBRANCH:
6693 case OMP_CLAUSE_NOTINBRANCH:
6694 if (branch_seen)
6696 error ("%<inbranch%> clause is incompatible with "
6697 "%<notinbranch%>");
6698 remove = true;
6700 branch_seen = true;
6701 break;
6703 default:
6704 gcc_unreachable ();
6707 if (remove)
6708 *pc = OMP_CLAUSE_CHAIN (c);
6709 else
6710 pc = &OMP_CLAUSE_CHAIN (c);
6713 for (pc = &clauses, c = clauses; c ; c = *pc)
6715 enum omp_clause_code c_kind = OMP_CLAUSE_CODE (c);
6716 bool remove = false;
6717 bool need_complete_type = false;
6718 bool need_default_ctor = false;
6719 bool need_copy_ctor = false;
6720 bool need_copy_assignment = false;
6721 bool need_implicitly_determined = false;
6722 bool need_dtor = false;
6723 tree type, inner_type;
6725 switch (c_kind)
6727 case OMP_CLAUSE_SHARED:
6728 need_implicitly_determined = true;
6729 break;
6730 case OMP_CLAUSE_PRIVATE:
6731 need_complete_type = true;
6732 need_default_ctor = true;
6733 need_dtor = true;
6734 need_implicitly_determined = true;
6735 break;
6736 case OMP_CLAUSE_FIRSTPRIVATE:
6737 need_complete_type = true;
6738 need_copy_ctor = true;
6739 need_dtor = true;
6740 need_implicitly_determined = true;
6741 break;
6742 case OMP_CLAUSE_LASTPRIVATE:
6743 need_complete_type = true;
6744 need_copy_assignment = true;
6745 need_implicitly_determined = true;
6746 break;
6747 case OMP_CLAUSE_REDUCTION:
6748 need_implicitly_determined = true;
6749 break;
6750 case OMP_CLAUSE_LINEAR:
6751 if (!declare_simd)
6752 need_implicitly_determined = true;
6753 break;
6754 case OMP_CLAUSE_COPYPRIVATE:
6755 need_copy_assignment = true;
6756 break;
6757 case OMP_CLAUSE_COPYIN:
6758 need_copy_assignment = true;
6759 break;
6760 case OMP_CLAUSE_SIMDLEN:
6761 if (safelen
6762 && !processing_template_decl
6763 && tree_int_cst_lt (OMP_CLAUSE_SAFELEN_EXPR (safelen),
6764 OMP_CLAUSE_SIMDLEN_EXPR (c)))
6766 error_at (OMP_CLAUSE_LOCATION (c),
6767 "%<simdlen%> clause value is bigger than "
6768 "%<safelen%> clause value");
6769 OMP_CLAUSE_SIMDLEN_EXPR (c)
6770 = OMP_CLAUSE_SAFELEN_EXPR (safelen);
6772 pc = &OMP_CLAUSE_CHAIN (c);
6773 continue;
6774 case OMP_CLAUSE_NOWAIT:
6775 if (copyprivate_seen)
6777 error_at (OMP_CLAUSE_LOCATION (c),
6778 "%<nowait%> clause must not be used together "
6779 "with %<copyprivate%>");
6780 *pc = OMP_CLAUSE_CHAIN (c);
6781 continue;
6783 /* FALLTHRU */
6784 default:
6785 pc = &OMP_CLAUSE_CHAIN (c);
6786 continue;
6789 t = OMP_CLAUSE_DECL (c);
6790 if (processing_template_decl
6791 && !VAR_P (t) && TREE_CODE (t) != PARM_DECL)
6793 pc = &OMP_CLAUSE_CHAIN (c);
6794 continue;
6797 switch (c_kind)
6799 case OMP_CLAUSE_LASTPRIVATE:
6800 if (!bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
6802 need_default_ctor = true;
6803 need_dtor = true;
6805 break;
6807 case OMP_CLAUSE_REDUCTION:
6808 if (finish_omp_reduction_clause (c, &need_default_ctor,
6809 &need_dtor))
6810 remove = true;
6811 else
6812 t = OMP_CLAUSE_DECL (c);
6813 break;
6815 case OMP_CLAUSE_COPYIN:
6816 if (!VAR_P (t) || !CP_DECL_THREAD_LOCAL_P (t))
6818 error ("%qE must be %<threadprivate%> for %<copyin%>", t);
6819 remove = true;
6821 break;
6823 default:
6824 break;
6827 if (need_complete_type || need_copy_assignment)
6829 t = require_complete_type (t);
6830 if (t == error_mark_node)
6831 remove = true;
6832 else if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE
6833 && !complete_type_or_else (TREE_TYPE (TREE_TYPE (t)), t))
6834 remove = true;
6836 if (need_implicitly_determined)
6838 const char *share_name = NULL;
6840 if (VAR_P (t) && CP_DECL_THREAD_LOCAL_P (t))
6841 share_name = "threadprivate";
6842 else switch (cxx_omp_predetermined_sharing (t))
6844 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
6845 break;
6846 case OMP_CLAUSE_DEFAULT_SHARED:
6847 /* const vars may be specified in firstprivate clause. */
6848 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
6849 && cxx_omp_const_qual_no_mutable (t))
6850 break;
6851 share_name = "shared";
6852 break;
6853 case OMP_CLAUSE_DEFAULT_PRIVATE:
6854 share_name = "private";
6855 break;
6856 default:
6857 gcc_unreachable ();
6859 if (share_name)
6861 error ("%qE is predetermined %qs for %qs",
6862 omp_clause_printable_decl (t), share_name,
6863 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6864 remove = true;
6868 /* We're interested in the base element, not arrays. */
6869 inner_type = type = TREE_TYPE (t);
6870 if ((need_complete_type
6871 || need_copy_assignment
6872 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
6873 && TREE_CODE (inner_type) == REFERENCE_TYPE)
6874 inner_type = TREE_TYPE (inner_type);
6875 while (TREE_CODE (inner_type) == ARRAY_TYPE)
6876 inner_type = TREE_TYPE (inner_type);
6878 /* Check for special function availability by building a call to one.
6879 Save the results, because later we won't be in the right context
6880 for making these queries. */
6881 if (CLASS_TYPE_P (inner_type)
6882 && COMPLETE_TYPE_P (inner_type)
6883 && (need_default_ctor || need_copy_ctor
6884 || need_copy_assignment || need_dtor)
6885 && !type_dependent_expression_p (t)
6886 && cxx_omp_create_clause_info (c, inner_type, need_default_ctor,
6887 need_copy_ctor, need_copy_assignment,
6888 need_dtor))
6889 remove = true;
6891 if (remove)
6892 *pc = OMP_CLAUSE_CHAIN (c);
6893 else
6894 pc = &OMP_CLAUSE_CHAIN (c);
6897 bitmap_obstack_release (NULL);
6898 return clauses;
6901 /* Start processing OpenMP clauses that can include any
6902 privatization clauses for non-static data members. */
6904 tree
6905 push_omp_privatization_clauses (bool ignore_next)
6907 if (omp_private_member_ignore_next)
6909 omp_private_member_ignore_next = ignore_next;
6910 return NULL_TREE;
6912 omp_private_member_ignore_next = ignore_next;
6913 if (omp_private_member_map)
6914 omp_private_member_vec.safe_push (error_mark_node);
6915 return push_stmt_list ();
6918 /* Revert remapping of any non-static data members since
6919 the last push_omp_privatization_clauses () call. */
6921 void
6922 pop_omp_privatization_clauses (tree stmt)
6924 if (stmt == NULL_TREE)
6925 return;
6926 stmt = pop_stmt_list (stmt);
6927 if (omp_private_member_map)
6929 while (!omp_private_member_vec.is_empty ())
6931 tree t = omp_private_member_vec.pop ();
6932 if (t == error_mark_node)
6934 add_stmt (stmt);
6935 return;
6937 bool no_decl_expr = t == integer_zero_node;
6938 if (no_decl_expr)
6939 t = omp_private_member_vec.pop ();
6940 tree *v = omp_private_member_map->get (t);
6941 gcc_assert (v);
6942 if (!no_decl_expr)
6943 add_decl_expr (*v);
6944 omp_private_member_map->remove (t);
6946 delete omp_private_member_map;
6947 omp_private_member_map = NULL;
6949 add_stmt (stmt);
6952 /* Remember OpenMP privatization clauses mapping and clear it.
6953 Used for lambdas. */
6955 void
6956 save_omp_privatization_clauses (vec<tree> &save)
6958 save = vNULL;
6959 if (omp_private_member_ignore_next)
6960 save.safe_push (integer_one_node);
6961 omp_private_member_ignore_next = false;
6962 if (!omp_private_member_map)
6963 return;
6965 while (!omp_private_member_vec.is_empty ())
6967 tree t = omp_private_member_vec.pop ();
6968 if (t == error_mark_node)
6970 save.safe_push (t);
6971 continue;
6973 tree n = t;
6974 if (t == integer_zero_node)
6975 t = omp_private_member_vec.pop ();
6976 tree *v = omp_private_member_map->get (t);
6977 gcc_assert (v);
6978 save.safe_push (*v);
6979 save.safe_push (t);
6980 if (n != t)
6981 save.safe_push (n);
6983 delete omp_private_member_map;
6984 omp_private_member_map = NULL;
6987 /* Restore OpenMP privatization clauses mapping saved by the
6988 above function. */
6990 void
6991 restore_omp_privatization_clauses (vec<tree> &save)
6993 gcc_assert (omp_private_member_vec.is_empty ());
6994 omp_private_member_ignore_next = false;
6995 if (save.is_empty ())
6996 return;
6997 if (save.length () == 1 && save[0] == integer_one_node)
6999 omp_private_member_ignore_next = true;
7000 save.release ();
7001 return;
7004 omp_private_member_map = new hash_map <tree, tree>;
7005 while (!save.is_empty ())
7007 tree t = save.pop ();
7008 tree n = t;
7009 if (t != error_mark_node)
7011 if (t == integer_one_node)
7013 omp_private_member_ignore_next = true;
7014 gcc_assert (save.is_empty ());
7015 break;
7017 if (t == integer_zero_node)
7018 t = save.pop ();
7019 tree &v = omp_private_member_map->get_or_insert (t);
7020 v = save.pop ();
7022 omp_private_member_vec.safe_push (t);
7023 if (n != t)
7024 omp_private_member_vec.safe_push (n);
7026 save.release ();
7029 /* For all variables in the tree_list VARS, mark them as thread local. */
7031 void
7032 finish_omp_threadprivate (tree vars)
7034 tree t;
7036 /* Mark every variable in VARS to be assigned thread local storage. */
7037 for (t = vars; t; t = TREE_CHAIN (t))
7039 tree v = TREE_PURPOSE (t);
7041 if (error_operand_p (v))
7043 else if (!VAR_P (v))
7044 error ("%<threadprivate%> %qD is not file, namespace "
7045 "or block scope variable", v);
7046 /* If V had already been marked threadprivate, it doesn't matter
7047 whether it had been used prior to this point. */
7048 else if (TREE_USED (v)
7049 && (DECL_LANG_SPECIFIC (v) == NULL
7050 || !CP_DECL_THREADPRIVATE_P (v)))
7051 error ("%qE declared %<threadprivate%> after first use", v);
7052 else if (! TREE_STATIC (v) && ! DECL_EXTERNAL (v))
7053 error ("automatic variable %qE cannot be %<threadprivate%>", v);
7054 else if (! COMPLETE_TYPE_P (complete_type (TREE_TYPE (v))))
7055 error ("%<threadprivate%> %qE has incomplete type", v);
7056 else if (TREE_STATIC (v) && TYPE_P (CP_DECL_CONTEXT (v))
7057 && CP_DECL_CONTEXT (v) != current_class_type)
7058 error ("%<threadprivate%> %qE directive not "
7059 "in %qT definition", v, CP_DECL_CONTEXT (v));
7060 else
7062 /* Allocate a LANG_SPECIFIC structure for V, if needed. */
7063 if (DECL_LANG_SPECIFIC (v) == NULL)
7065 retrofit_lang_decl (v);
7067 /* Make sure that DECL_DISCRIMINATOR_P continues to be true
7068 after the allocation of the lang_decl structure. */
7069 if (DECL_DISCRIMINATOR_P (v))
7070 DECL_LANG_SPECIFIC (v)->u.base.u2sel = 1;
7073 if (! CP_DECL_THREAD_LOCAL_P (v))
7075 CP_DECL_THREAD_LOCAL_P (v) = true;
7076 set_decl_tls_model (v, decl_default_tls_model (v));
7077 /* If rtl has been already set for this var, call
7078 make_decl_rtl once again, so that encode_section_info
7079 has a chance to look at the new decl flags. */
7080 if (DECL_RTL_SET_P (v))
7081 make_decl_rtl (v);
7083 CP_DECL_THREADPRIVATE_P (v) = 1;
7088 /* Build an OpenMP structured block. */
7090 tree
7091 begin_omp_structured_block (void)
7093 return do_pushlevel (sk_omp);
7096 tree
7097 finish_omp_structured_block (tree block)
7099 return do_poplevel (block);
7102 /* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
7103 statement. LOC is the location of the OACC_DATA. */
7105 tree
7106 finish_oacc_data (tree clauses, tree block)
7108 tree stmt;
7110 block = finish_omp_structured_block (block);
7112 stmt = make_node (OACC_DATA);
7113 TREE_TYPE (stmt) = void_type_node;
7114 OACC_DATA_CLAUSES (stmt) = clauses;
7115 OACC_DATA_BODY (stmt) = block;
7117 return add_stmt (stmt);
7120 /* Generate OACC_KERNELS, with CLAUSES and BLOCK as its compound
7121 statement. LOC is the location of the OACC_KERNELS. */
7123 tree
7124 finish_oacc_kernels (tree clauses, tree block)
7126 tree stmt;
7128 block = finish_omp_structured_block (block);
7130 stmt = make_node (OACC_KERNELS);
7131 TREE_TYPE (stmt) = void_type_node;
7132 OACC_KERNELS_CLAUSES (stmt) = clauses;
7133 OACC_KERNELS_BODY (stmt) = block;
7135 return add_stmt (stmt);
7138 /* Generate OACC_PARALLEL, with CLAUSES and BLOCK as its compound
7139 statement. LOC is the location of the OACC_PARALLEL. */
7141 tree
7142 finish_oacc_parallel (tree clauses, tree block)
7144 tree stmt;
7146 block = finish_omp_structured_block (block);
7148 stmt = make_node (OACC_PARALLEL);
7149 TREE_TYPE (stmt) = void_type_node;
7150 OACC_PARALLEL_CLAUSES (stmt) = clauses;
7151 OACC_PARALLEL_BODY (stmt) = block;
7153 return add_stmt (stmt);
7156 /* Similarly, except force the retention of the BLOCK. */
7158 tree
7159 begin_omp_parallel (void)
7161 keep_next_level (true);
7162 return begin_omp_structured_block ();
7165 tree
7166 finish_omp_parallel (tree clauses, tree body)
7168 tree stmt;
7170 body = finish_omp_structured_block (body);
7172 stmt = make_node (OMP_PARALLEL);
7173 TREE_TYPE (stmt) = void_type_node;
7174 OMP_PARALLEL_CLAUSES (stmt) = clauses;
7175 OMP_PARALLEL_BODY (stmt) = body;
7177 return add_stmt (stmt);
7180 tree
7181 begin_omp_task (void)
7183 keep_next_level (true);
7184 return begin_omp_structured_block ();
7187 tree
7188 finish_omp_task (tree clauses, tree body)
7190 tree stmt;
7192 body = finish_omp_structured_block (body);
7194 stmt = make_node (OMP_TASK);
7195 TREE_TYPE (stmt) = void_type_node;
7196 OMP_TASK_CLAUSES (stmt) = clauses;
7197 OMP_TASK_BODY (stmt) = body;
7199 return add_stmt (stmt);
7202 /* Helper function for finish_omp_for. Convert Ith random access iterator
7203 into integral iterator. Return FALSE if successful. */
7205 static bool
7206 handle_omp_for_class_iterator (int i, location_t locus, enum tree_code code,
7207 tree declv, tree initv, tree condv, tree incrv,
7208 tree *body, tree *pre_body, tree &clauses,
7209 tree *lastp, int collapse, int ordered)
7211 tree diff, iter_init, iter_incr = NULL, last;
7212 tree incr_var = NULL, orig_pre_body, orig_body, c;
7213 tree decl = TREE_VEC_ELT (declv, i);
7214 tree init = TREE_VEC_ELT (initv, i);
7215 tree cond = TREE_VEC_ELT (condv, i);
7216 tree incr = TREE_VEC_ELT (incrv, i);
7217 tree iter = decl;
7218 location_t elocus = locus;
7220 if (init && EXPR_HAS_LOCATION (init))
7221 elocus = EXPR_LOCATION (init);
7223 switch (TREE_CODE (cond))
7225 case GT_EXPR:
7226 case GE_EXPR:
7227 case LT_EXPR:
7228 case LE_EXPR:
7229 case NE_EXPR:
7230 if (TREE_OPERAND (cond, 1) == iter)
7231 cond = build2 (swap_tree_comparison (TREE_CODE (cond)),
7232 TREE_TYPE (cond), iter, TREE_OPERAND (cond, 0));
7233 if (TREE_OPERAND (cond, 0) != iter)
7234 cond = error_mark_node;
7235 else
7237 tree tem = build_x_binary_op (EXPR_LOCATION (cond),
7238 TREE_CODE (cond),
7239 iter, ERROR_MARK,
7240 TREE_OPERAND (cond, 1), ERROR_MARK,
7241 NULL, tf_warning_or_error);
7242 if (error_operand_p (tem))
7243 return true;
7245 break;
7246 default:
7247 cond = error_mark_node;
7248 break;
7250 if (cond == error_mark_node)
7252 error_at (elocus, "invalid controlling predicate");
7253 return true;
7255 diff = build_x_binary_op (elocus, MINUS_EXPR, TREE_OPERAND (cond, 1),
7256 ERROR_MARK, iter, ERROR_MARK, NULL,
7257 tf_warning_or_error);
7258 if (error_operand_p (diff))
7259 return true;
7260 if (TREE_CODE (TREE_TYPE (diff)) != INTEGER_TYPE)
7262 error_at (elocus, "difference between %qE and %qD does not have integer type",
7263 TREE_OPERAND (cond, 1), iter);
7264 return true;
7267 switch (TREE_CODE (incr))
7269 case PREINCREMENT_EXPR:
7270 case PREDECREMENT_EXPR:
7271 case POSTINCREMENT_EXPR:
7272 case POSTDECREMENT_EXPR:
7273 if (TREE_OPERAND (incr, 0) != iter)
7275 incr = error_mark_node;
7276 break;
7278 iter_incr = build_x_unary_op (EXPR_LOCATION (incr),
7279 TREE_CODE (incr), iter,
7280 tf_warning_or_error);
7281 if (error_operand_p (iter_incr))
7282 return true;
7283 else if (TREE_CODE (incr) == PREINCREMENT_EXPR
7284 || TREE_CODE (incr) == POSTINCREMENT_EXPR)
7285 incr = integer_one_node;
7286 else
7287 incr = integer_minus_one_node;
7288 break;
7289 case MODIFY_EXPR:
7290 if (TREE_OPERAND (incr, 0) != iter)
7291 incr = error_mark_node;
7292 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
7293 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
7295 tree rhs = TREE_OPERAND (incr, 1);
7296 if (TREE_OPERAND (rhs, 0) == iter)
7298 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (rhs, 1)))
7299 != INTEGER_TYPE)
7300 incr = error_mark_node;
7301 else
7303 iter_incr = build_x_modify_expr (EXPR_LOCATION (rhs),
7304 iter, TREE_CODE (rhs),
7305 TREE_OPERAND (rhs, 1),
7306 tf_warning_or_error);
7307 if (error_operand_p (iter_incr))
7308 return true;
7309 incr = TREE_OPERAND (rhs, 1);
7310 incr = cp_convert (TREE_TYPE (diff), incr,
7311 tf_warning_or_error);
7312 if (TREE_CODE (rhs) == MINUS_EXPR)
7314 incr = build1 (NEGATE_EXPR, TREE_TYPE (diff), incr);
7315 incr = fold_if_not_in_template (incr);
7317 if (TREE_CODE (incr) != INTEGER_CST
7318 && (TREE_CODE (incr) != NOP_EXPR
7319 || (TREE_CODE (TREE_OPERAND (incr, 0))
7320 != INTEGER_CST)))
7321 iter_incr = NULL;
7324 else if (TREE_OPERAND (rhs, 1) == iter)
7326 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (rhs, 0))) != INTEGER_TYPE
7327 || TREE_CODE (rhs) != PLUS_EXPR)
7328 incr = error_mark_node;
7329 else
7331 iter_incr = build_x_binary_op (EXPR_LOCATION (rhs),
7332 PLUS_EXPR,
7333 TREE_OPERAND (rhs, 0),
7334 ERROR_MARK, iter,
7335 ERROR_MARK, NULL,
7336 tf_warning_or_error);
7337 if (error_operand_p (iter_incr))
7338 return true;
7339 iter_incr = build_x_modify_expr (EXPR_LOCATION (rhs),
7340 iter, NOP_EXPR,
7341 iter_incr,
7342 tf_warning_or_error);
7343 if (error_operand_p (iter_incr))
7344 return true;
7345 incr = TREE_OPERAND (rhs, 0);
7346 iter_incr = NULL;
7349 else
7350 incr = error_mark_node;
7352 else
7353 incr = error_mark_node;
7354 break;
7355 default:
7356 incr = error_mark_node;
7357 break;
7360 if (incr == error_mark_node)
7362 error_at (elocus, "invalid increment expression");
7363 return true;
7366 incr = cp_convert (TREE_TYPE (diff), incr, tf_warning_or_error);
7367 bool taskloop_iv_seen = false;
7368 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
7369 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
7370 && OMP_CLAUSE_DECL (c) == iter)
7372 if (code == OMP_TASKLOOP)
7374 taskloop_iv_seen = true;
7375 OMP_CLAUSE_LASTPRIVATE_TASKLOOP_IV (c) = 1;
7377 break;
7379 else if (code == OMP_TASKLOOP
7380 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
7381 && OMP_CLAUSE_DECL (c) == iter)
7383 taskloop_iv_seen = true;
7384 OMP_CLAUSE_PRIVATE_TASKLOOP_IV (c) = 1;
7387 decl = create_temporary_var (TREE_TYPE (diff));
7388 pushdecl (decl);
7389 add_decl_expr (decl);
7390 last = create_temporary_var (TREE_TYPE (diff));
7391 pushdecl (last);
7392 add_decl_expr (last);
7393 if (c && iter_incr == NULL && TREE_CODE (incr) != INTEGER_CST
7394 && (!ordered || (i < collapse && collapse > 1)))
7396 incr_var = create_temporary_var (TREE_TYPE (diff));
7397 pushdecl (incr_var);
7398 add_decl_expr (incr_var);
7400 gcc_assert (stmts_are_full_exprs_p ());
7401 tree diffvar = NULL_TREE;
7402 if (code == OMP_TASKLOOP)
7404 if (!taskloop_iv_seen)
7406 tree ivc = build_omp_clause (locus, OMP_CLAUSE_FIRSTPRIVATE);
7407 OMP_CLAUSE_DECL (ivc) = iter;
7408 cxx_omp_finish_clause (ivc, NULL);
7409 OMP_CLAUSE_CHAIN (ivc) = clauses;
7410 clauses = ivc;
7412 tree lvc = build_omp_clause (locus, OMP_CLAUSE_FIRSTPRIVATE);
7413 OMP_CLAUSE_DECL (lvc) = last;
7414 OMP_CLAUSE_CHAIN (lvc) = clauses;
7415 clauses = lvc;
7416 diffvar = create_temporary_var (TREE_TYPE (diff));
7417 pushdecl (diffvar);
7418 add_decl_expr (diffvar);
7421 orig_pre_body = *pre_body;
7422 *pre_body = push_stmt_list ();
7423 if (orig_pre_body)
7424 add_stmt (orig_pre_body);
7425 if (init != NULL)
7426 finish_expr_stmt (build_x_modify_expr (elocus,
7427 iter, NOP_EXPR, init,
7428 tf_warning_or_error));
7429 init = build_int_cst (TREE_TYPE (diff), 0);
7430 if (c && iter_incr == NULL
7431 && (!ordered || (i < collapse && collapse > 1)))
7433 if (incr_var)
7435 finish_expr_stmt (build_x_modify_expr (elocus,
7436 incr_var, NOP_EXPR,
7437 incr, tf_warning_or_error));
7438 incr = incr_var;
7440 iter_incr = build_x_modify_expr (elocus,
7441 iter, PLUS_EXPR, incr,
7442 tf_warning_or_error);
7444 if (c && ordered && i < collapse && collapse > 1)
7445 iter_incr = incr;
7446 finish_expr_stmt (build_x_modify_expr (elocus,
7447 last, NOP_EXPR, init,
7448 tf_warning_or_error));
7449 if (diffvar)
7451 finish_expr_stmt (build_x_modify_expr (elocus,
7452 diffvar, NOP_EXPR,
7453 diff, tf_warning_or_error));
7454 diff = diffvar;
7456 *pre_body = pop_stmt_list (*pre_body);
7458 cond = cp_build_binary_op (elocus,
7459 TREE_CODE (cond), decl, diff,
7460 tf_warning_or_error);
7461 incr = build_modify_expr (elocus, decl, NULL_TREE, PLUS_EXPR,
7462 elocus, incr, NULL_TREE);
7464 orig_body = *body;
7465 *body = push_stmt_list ();
7466 iter_init = build2 (MINUS_EXPR, TREE_TYPE (diff), decl, last);
7467 iter_init = build_x_modify_expr (elocus,
7468 iter, PLUS_EXPR, iter_init,
7469 tf_warning_or_error);
7470 if (iter_init != error_mark_node)
7471 iter_init = build1 (NOP_EXPR, void_type_node, iter_init);
7472 finish_expr_stmt (iter_init);
7473 finish_expr_stmt (build_x_modify_expr (elocus,
7474 last, NOP_EXPR, decl,
7475 tf_warning_or_error));
7476 add_stmt (orig_body);
7477 *body = pop_stmt_list (*body);
7479 if (c)
7481 OMP_CLAUSE_LASTPRIVATE_STMT (c) = push_stmt_list ();
7482 if (!ordered)
7483 finish_expr_stmt (iter_incr);
7484 else
7486 iter_init = decl;
7487 if (i < collapse && collapse > 1 && !error_operand_p (iter_incr))
7488 iter_init = build2 (PLUS_EXPR, TREE_TYPE (diff),
7489 iter_init, iter_incr);
7490 iter_init = build2 (MINUS_EXPR, TREE_TYPE (diff), iter_init, last);
7491 iter_init = build_x_modify_expr (elocus,
7492 iter, PLUS_EXPR, iter_init,
7493 tf_warning_or_error);
7494 if (iter_init != error_mark_node)
7495 iter_init = build1 (NOP_EXPR, void_type_node, iter_init);
7496 finish_expr_stmt (iter_init);
7498 OMP_CLAUSE_LASTPRIVATE_STMT (c)
7499 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (c));
7502 TREE_VEC_ELT (declv, i) = decl;
7503 TREE_VEC_ELT (initv, i) = init;
7504 TREE_VEC_ELT (condv, i) = cond;
7505 TREE_VEC_ELT (incrv, i) = incr;
7506 *lastp = last;
7508 return false;
7511 /* Build and validate an OMP_FOR statement. CLAUSES, BODY, COND, INCR
7512 are directly for their associated operands in the statement. DECL
7513 and INIT are a combo; if DECL is NULL then INIT ought to be a
7514 MODIFY_EXPR, and the DECL should be extracted. PRE_BODY are
7515 optional statements that need to go before the loop into its
7516 sk_omp scope. */
7518 tree
7519 finish_omp_for (location_t locus, enum tree_code code, tree declv,
7520 tree orig_declv, tree initv, tree condv, tree incrv,
7521 tree body, tree pre_body, tree clauses)
7523 tree omp_for = NULL, orig_incr = NULL;
7524 tree decl = NULL, init, cond, incr, orig_decl = NULL_TREE, block = NULL_TREE;
7525 tree last = NULL_TREE;
7526 location_t elocus;
7527 int i;
7528 int collapse = 1;
7529 int ordered = 0;
7531 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (initv));
7532 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (condv));
7533 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (incrv));
7534 if (TREE_VEC_LENGTH (declv) > 1)
7536 tree c = find_omp_clause (clauses, OMP_CLAUSE_COLLAPSE);
7537 if (c)
7538 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (c));
7539 if (collapse != TREE_VEC_LENGTH (declv))
7540 ordered = TREE_VEC_LENGTH (declv);
7542 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
7544 decl = TREE_VEC_ELT (declv, i);
7545 init = TREE_VEC_ELT (initv, i);
7546 cond = TREE_VEC_ELT (condv, i);
7547 incr = TREE_VEC_ELT (incrv, i);
7548 elocus = locus;
7550 if (decl == NULL)
7552 if (init != NULL)
7553 switch (TREE_CODE (init))
7555 case MODIFY_EXPR:
7556 decl = TREE_OPERAND (init, 0);
7557 init = TREE_OPERAND (init, 1);
7558 break;
7559 case MODOP_EXPR:
7560 if (TREE_CODE (TREE_OPERAND (init, 1)) == NOP_EXPR)
7562 decl = TREE_OPERAND (init, 0);
7563 init = TREE_OPERAND (init, 2);
7565 break;
7566 default:
7567 break;
7570 if (decl == NULL)
7572 error_at (locus,
7573 "expected iteration declaration or initialization");
7574 return NULL;
7578 if (init && EXPR_HAS_LOCATION (init))
7579 elocus = EXPR_LOCATION (init);
7581 if (cond == NULL)
7583 error_at (elocus, "missing controlling predicate");
7584 return NULL;
7587 if (incr == NULL)
7589 error_at (elocus, "missing increment expression");
7590 return NULL;
7593 TREE_VEC_ELT (declv, i) = decl;
7594 TREE_VEC_ELT (initv, i) = init;
7597 if (dependent_omp_for_p (declv, initv, condv, incrv))
7599 tree stmt;
7601 stmt = make_node (code);
7603 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
7605 /* This is really just a place-holder. We'll be decomposing this
7606 again and going through the cp_build_modify_expr path below when
7607 we instantiate the thing. */
7608 TREE_VEC_ELT (initv, i)
7609 = build2 (MODIFY_EXPR, void_type_node, TREE_VEC_ELT (declv, i),
7610 TREE_VEC_ELT (initv, i));
7613 TREE_TYPE (stmt) = void_type_node;
7614 OMP_FOR_INIT (stmt) = initv;
7615 OMP_FOR_COND (stmt) = condv;
7616 OMP_FOR_INCR (stmt) = incrv;
7617 OMP_FOR_BODY (stmt) = body;
7618 OMP_FOR_PRE_BODY (stmt) = pre_body;
7619 OMP_FOR_CLAUSES (stmt) = clauses;
7621 SET_EXPR_LOCATION (stmt, locus);
7622 return add_stmt (stmt);
7625 if (!orig_declv)
7626 orig_declv = copy_node (declv);
7628 if (processing_template_decl)
7629 orig_incr = make_tree_vec (TREE_VEC_LENGTH (incrv));
7631 for (i = 0; i < TREE_VEC_LENGTH (declv); )
7633 decl = TREE_VEC_ELT (declv, i);
7634 init = TREE_VEC_ELT (initv, i);
7635 cond = TREE_VEC_ELT (condv, i);
7636 incr = TREE_VEC_ELT (incrv, i);
7637 if (orig_incr)
7638 TREE_VEC_ELT (orig_incr, i) = incr;
7639 elocus = locus;
7641 if (init && EXPR_HAS_LOCATION (init))
7642 elocus = EXPR_LOCATION (init);
7644 if (!DECL_P (decl))
7646 error_at (elocus, "expected iteration declaration or initialization");
7647 return NULL;
7650 if (incr && TREE_CODE (incr) == MODOP_EXPR)
7652 if (orig_incr)
7653 TREE_VEC_ELT (orig_incr, i) = incr;
7654 incr = cp_build_modify_expr (TREE_OPERAND (incr, 0),
7655 TREE_CODE (TREE_OPERAND (incr, 1)),
7656 TREE_OPERAND (incr, 2),
7657 tf_warning_or_error);
7660 if (CLASS_TYPE_P (TREE_TYPE (decl)))
7662 if (code == OMP_SIMD)
7664 error_at (elocus, "%<#pragma omp simd%> used with class "
7665 "iteration variable %qE", decl);
7666 return NULL;
7668 if (code == CILK_FOR && i == 0)
7669 orig_decl = decl;
7670 if (handle_omp_for_class_iterator (i, locus, code, declv, initv,
7671 condv, incrv, &body, &pre_body,
7672 clauses, &last, collapse,
7673 ordered))
7674 return NULL;
7675 continue;
7678 if (!INTEGRAL_TYPE_P (TREE_TYPE (decl))
7679 && !TYPE_PTR_P (TREE_TYPE (decl)))
7681 error_at (elocus, "invalid type for iteration variable %qE", decl);
7682 return NULL;
7685 if (!processing_template_decl)
7687 init = fold_build_cleanup_point_expr (TREE_TYPE (init), init);
7688 init = cp_build_modify_expr (decl, NOP_EXPR, init, tf_warning_or_error);
7690 else
7691 init = build2 (MODIFY_EXPR, void_type_node, decl, init);
7692 if (cond
7693 && TREE_SIDE_EFFECTS (cond)
7694 && COMPARISON_CLASS_P (cond)
7695 && !processing_template_decl)
7697 tree t = TREE_OPERAND (cond, 0);
7698 if (TREE_SIDE_EFFECTS (t)
7699 && t != decl
7700 && (TREE_CODE (t) != NOP_EXPR
7701 || TREE_OPERAND (t, 0) != decl))
7702 TREE_OPERAND (cond, 0)
7703 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7705 t = TREE_OPERAND (cond, 1);
7706 if (TREE_SIDE_EFFECTS (t)
7707 && t != decl
7708 && (TREE_CODE (t) != NOP_EXPR
7709 || TREE_OPERAND (t, 0) != decl))
7710 TREE_OPERAND (cond, 1)
7711 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7713 if (decl == error_mark_node || init == error_mark_node)
7714 return NULL;
7716 TREE_VEC_ELT (declv, i) = decl;
7717 TREE_VEC_ELT (initv, i) = init;
7718 TREE_VEC_ELT (condv, i) = cond;
7719 TREE_VEC_ELT (incrv, i) = incr;
7720 i++;
7723 if (IS_EMPTY_STMT (pre_body))
7724 pre_body = NULL;
7726 if (code == CILK_FOR && !processing_template_decl)
7727 block = push_stmt_list ();
7729 omp_for = c_finish_omp_for (locus, code, declv, orig_declv, initv, condv,
7730 incrv, body, pre_body);
7732 if (omp_for == NULL)
7734 if (block)
7735 pop_stmt_list (block);
7736 return NULL;
7739 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INCR (omp_for)); i++)
7741 decl = TREE_OPERAND (TREE_VEC_ELT (OMP_FOR_INIT (omp_for), i), 0);
7742 incr = TREE_VEC_ELT (OMP_FOR_INCR (omp_for), i);
7744 if (TREE_CODE (incr) != MODIFY_EXPR)
7745 continue;
7747 if (TREE_SIDE_EFFECTS (TREE_OPERAND (incr, 1))
7748 && BINARY_CLASS_P (TREE_OPERAND (incr, 1))
7749 && !processing_template_decl)
7751 tree t = TREE_OPERAND (TREE_OPERAND (incr, 1), 0);
7752 if (TREE_SIDE_EFFECTS (t)
7753 && t != decl
7754 && (TREE_CODE (t) != NOP_EXPR
7755 || TREE_OPERAND (t, 0) != decl))
7756 TREE_OPERAND (TREE_OPERAND (incr, 1), 0)
7757 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7759 t = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
7760 if (TREE_SIDE_EFFECTS (t)
7761 && t != decl
7762 && (TREE_CODE (t) != NOP_EXPR
7763 || TREE_OPERAND (t, 0) != decl))
7764 TREE_OPERAND (TREE_OPERAND (incr, 1), 1)
7765 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7768 if (orig_incr)
7769 TREE_VEC_ELT (OMP_FOR_INCR (omp_for), i) = TREE_VEC_ELT (orig_incr, i);
7771 OMP_FOR_CLAUSES (omp_for) = clauses;
7773 /* For simd loops with non-static data member iterators, we could have added
7774 OMP_CLAUSE_LINEAR clauses without OMP_CLAUSE_LINEAR_STEP. As we know the
7775 step at this point, fill it in. */
7776 if (code == OMP_SIMD && !processing_template_decl
7777 && TREE_VEC_LENGTH (OMP_FOR_INCR (omp_for)) == 1)
7778 for (tree c = find_omp_clause (clauses, OMP_CLAUSE_LINEAR); c;
7779 c = find_omp_clause (OMP_CLAUSE_CHAIN (c), OMP_CLAUSE_LINEAR))
7780 if (OMP_CLAUSE_LINEAR_STEP (c) == NULL_TREE)
7782 decl = TREE_OPERAND (TREE_VEC_ELT (OMP_FOR_INIT (omp_for), 0), 0);
7783 gcc_assert (decl == OMP_CLAUSE_DECL (c));
7784 incr = TREE_VEC_ELT (OMP_FOR_INCR (omp_for), 0);
7785 tree step, stept;
7786 switch (TREE_CODE (incr))
7788 case PREINCREMENT_EXPR:
7789 case POSTINCREMENT_EXPR:
7790 /* c_omp_for_incr_canonicalize_ptr() should have been
7791 called to massage things appropriately. */
7792 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (decl)));
7793 OMP_CLAUSE_LINEAR_STEP (c) = build_int_cst (TREE_TYPE (decl), 1);
7794 break;
7795 case PREDECREMENT_EXPR:
7796 case POSTDECREMENT_EXPR:
7797 /* c_omp_for_incr_canonicalize_ptr() should have been
7798 called to massage things appropriately. */
7799 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (decl)));
7800 OMP_CLAUSE_LINEAR_STEP (c)
7801 = build_int_cst (TREE_TYPE (decl), -1);
7802 break;
7803 case MODIFY_EXPR:
7804 gcc_assert (TREE_OPERAND (incr, 0) == decl);
7805 incr = TREE_OPERAND (incr, 1);
7806 switch (TREE_CODE (incr))
7808 case PLUS_EXPR:
7809 if (TREE_OPERAND (incr, 1) == decl)
7810 step = TREE_OPERAND (incr, 0);
7811 else
7812 step = TREE_OPERAND (incr, 1);
7813 break;
7814 case MINUS_EXPR:
7815 case POINTER_PLUS_EXPR:
7816 gcc_assert (TREE_OPERAND (incr, 0) == decl);
7817 step = TREE_OPERAND (incr, 1);
7818 break;
7819 default:
7820 gcc_unreachable ();
7822 stept = TREE_TYPE (decl);
7823 if (POINTER_TYPE_P (stept))
7824 stept = sizetype;
7825 step = fold_convert (stept, step);
7826 if (TREE_CODE (incr) == MINUS_EXPR)
7827 step = fold_build1 (NEGATE_EXPR, stept, step);
7828 OMP_CLAUSE_LINEAR_STEP (c) = step;
7829 break;
7830 default:
7831 gcc_unreachable ();
7835 if (block)
7837 tree omp_par = make_node (OMP_PARALLEL);
7838 TREE_TYPE (omp_par) = void_type_node;
7839 OMP_PARALLEL_CLAUSES (omp_par) = NULL_TREE;
7840 tree bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
7841 TREE_SIDE_EFFECTS (bind) = 1;
7842 BIND_EXPR_BODY (bind) = pop_stmt_list (block);
7843 OMP_PARALLEL_BODY (omp_par) = bind;
7844 if (OMP_FOR_PRE_BODY (omp_for))
7846 add_stmt (OMP_FOR_PRE_BODY (omp_for));
7847 OMP_FOR_PRE_BODY (omp_for) = NULL_TREE;
7849 init = TREE_VEC_ELT (OMP_FOR_INIT (omp_for), 0);
7850 decl = TREE_OPERAND (init, 0);
7851 cond = TREE_VEC_ELT (OMP_FOR_COND (omp_for), 0);
7852 incr = TREE_VEC_ELT (OMP_FOR_INCR (omp_for), 0);
7853 tree t = TREE_OPERAND (cond, 1), c, clauses, *pc;
7854 clauses = OMP_FOR_CLAUSES (omp_for);
7855 OMP_FOR_CLAUSES (omp_for) = NULL_TREE;
7856 for (pc = &clauses; *pc; )
7857 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_SCHEDULE)
7859 gcc_assert (OMP_FOR_CLAUSES (omp_for) == NULL_TREE);
7860 OMP_FOR_CLAUSES (omp_for) = *pc;
7861 *pc = OMP_CLAUSE_CHAIN (*pc);
7862 OMP_CLAUSE_CHAIN (OMP_FOR_CLAUSES (omp_for)) = NULL_TREE;
7864 else
7866 gcc_assert (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_FIRSTPRIVATE);
7867 pc = &OMP_CLAUSE_CHAIN (*pc);
7869 if (TREE_CODE (t) != INTEGER_CST)
7871 TREE_OPERAND (cond, 1) = get_temp_regvar (TREE_TYPE (t), t);
7872 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
7873 OMP_CLAUSE_DECL (c) = TREE_OPERAND (cond, 1);
7874 OMP_CLAUSE_CHAIN (c) = clauses;
7875 clauses = c;
7877 if (TREE_CODE (incr) == MODIFY_EXPR)
7879 t = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
7880 if (TREE_CODE (t) != INTEGER_CST)
7882 TREE_OPERAND (TREE_OPERAND (incr, 1), 1)
7883 = get_temp_regvar (TREE_TYPE (t), t);
7884 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
7885 OMP_CLAUSE_DECL (c) = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
7886 OMP_CLAUSE_CHAIN (c) = clauses;
7887 clauses = c;
7890 t = TREE_OPERAND (init, 1);
7891 if (TREE_CODE (t) != INTEGER_CST)
7893 TREE_OPERAND (init, 1) = get_temp_regvar (TREE_TYPE (t), t);
7894 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
7895 OMP_CLAUSE_DECL (c) = TREE_OPERAND (init, 1);
7896 OMP_CLAUSE_CHAIN (c) = clauses;
7897 clauses = c;
7899 if (orig_decl && orig_decl != decl)
7901 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
7902 OMP_CLAUSE_DECL (c) = orig_decl;
7903 OMP_CLAUSE_CHAIN (c) = clauses;
7904 clauses = c;
7906 if (last)
7908 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
7909 OMP_CLAUSE_DECL (c) = last;
7910 OMP_CLAUSE_CHAIN (c) = clauses;
7911 clauses = c;
7913 c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
7914 OMP_CLAUSE_DECL (c) = decl;
7915 OMP_CLAUSE_CHAIN (c) = clauses;
7916 clauses = c;
7917 c = build_omp_clause (input_location, OMP_CLAUSE__CILK_FOR_COUNT_);
7918 OMP_CLAUSE_OPERAND (c, 0)
7919 = cilk_for_number_of_iterations (omp_for);
7920 OMP_CLAUSE_CHAIN (c) = clauses;
7921 OMP_PARALLEL_CLAUSES (omp_par) = finish_omp_clauses (c, false);
7922 add_stmt (omp_par);
7923 return omp_par;
7925 else if (code == CILK_FOR && processing_template_decl)
7927 tree c, clauses = OMP_FOR_CLAUSES (omp_for);
7928 if (orig_decl && orig_decl != decl)
7930 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
7931 OMP_CLAUSE_DECL (c) = orig_decl;
7932 OMP_CLAUSE_CHAIN (c) = clauses;
7933 clauses = c;
7935 if (last)
7937 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
7938 OMP_CLAUSE_DECL (c) = last;
7939 OMP_CLAUSE_CHAIN (c) = clauses;
7940 clauses = c;
7942 OMP_FOR_CLAUSES (omp_for) = clauses;
7944 return omp_for;
7947 void
7948 finish_omp_atomic (enum tree_code code, enum tree_code opcode, tree lhs,
7949 tree rhs, tree v, tree lhs1, tree rhs1, bool seq_cst)
7951 tree orig_lhs;
7952 tree orig_rhs;
7953 tree orig_v;
7954 tree orig_lhs1;
7955 tree orig_rhs1;
7956 bool dependent_p;
7957 tree stmt;
7959 orig_lhs = lhs;
7960 orig_rhs = rhs;
7961 orig_v = v;
7962 orig_lhs1 = lhs1;
7963 orig_rhs1 = rhs1;
7964 dependent_p = false;
7965 stmt = NULL_TREE;
7967 /* Even in a template, we can detect invalid uses of the atomic
7968 pragma if neither LHS nor RHS is type-dependent. */
7969 if (processing_template_decl)
7971 dependent_p = (type_dependent_expression_p (lhs)
7972 || (rhs && type_dependent_expression_p (rhs))
7973 || (v && type_dependent_expression_p (v))
7974 || (lhs1 && type_dependent_expression_p (lhs1))
7975 || (rhs1 && type_dependent_expression_p (rhs1)));
7976 if (!dependent_p)
7978 lhs = build_non_dependent_expr (lhs);
7979 if (rhs)
7980 rhs = build_non_dependent_expr (rhs);
7981 if (v)
7982 v = build_non_dependent_expr (v);
7983 if (lhs1)
7984 lhs1 = build_non_dependent_expr (lhs1);
7985 if (rhs1)
7986 rhs1 = build_non_dependent_expr (rhs1);
7989 if (!dependent_p)
7991 bool swapped = false;
7992 if (rhs1 && cp_tree_equal (lhs, rhs))
7994 std::swap (rhs, rhs1);
7995 swapped = !commutative_tree_code (opcode);
7997 if (rhs1 && !cp_tree_equal (lhs, rhs1))
7999 if (code == OMP_ATOMIC)
8000 error ("%<#pragma omp atomic update%> uses two different "
8001 "expressions for memory");
8002 else
8003 error ("%<#pragma omp atomic capture%> uses two different "
8004 "expressions for memory");
8005 return;
8007 if (lhs1 && !cp_tree_equal (lhs, lhs1))
8009 if (code == OMP_ATOMIC)
8010 error ("%<#pragma omp atomic update%> uses two different "
8011 "expressions for memory");
8012 else
8013 error ("%<#pragma omp atomic capture%> uses two different "
8014 "expressions for memory");
8015 return;
8017 stmt = c_finish_omp_atomic (input_location, code, opcode, lhs, rhs,
8018 v, lhs1, rhs1, swapped, seq_cst);
8019 if (stmt == error_mark_node)
8020 return;
8022 if (processing_template_decl)
8024 if (code == OMP_ATOMIC_READ)
8026 stmt = build_min_nt_loc (EXPR_LOCATION (orig_lhs),
8027 OMP_ATOMIC_READ, orig_lhs);
8028 OMP_ATOMIC_SEQ_CST (stmt) = seq_cst;
8029 stmt = build2 (MODIFY_EXPR, void_type_node, orig_v, stmt);
8031 else
8033 if (opcode == NOP_EXPR)
8034 stmt = build2 (MODIFY_EXPR, void_type_node, orig_lhs, orig_rhs);
8035 else
8036 stmt = build2 (opcode, void_type_node, orig_lhs, orig_rhs);
8037 if (orig_rhs1)
8038 stmt = build_min_nt_loc (EXPR_LOCATION (orig_rhs1),
8039 COMPOUND_EXPR, orig_rhs1, stmt);
8040 if (code != OMP_ATOMIC)
8042 stmt = build_min_nt_loc (EXPR_LOCATION (orig_lhs1),
8043 code, orig_lhs1, stmt);
8044 OMP_ATOMIC_SEQ_CST (stmt) = seq_cst;
8045 stmt = build2 (MODIFY_EXPR, void_type_node, orig_v, stmt);
8048 stmt = build2 (OMP_ATOMIC, void_type_node, integer_zero_node, stmt);
8049 OMP_ATOMIC_SEQ_CST (stmt) = seq_cst;
8051 finish_expr_stmt (stmt);
8054 void
8055 finish_omp_barrier (void)
8057 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_BARRIER);
8058 vec<tree, va_gc> *vec = make_tree_vector ();
8059 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
8060 release_tree_vector (vec);
8061 finish_expr_stmt (stmt);
8064 void
8065 finish_omp_flush (void)
8067 tree fn = builtin_decl_explicit (BUILT_IN_SYNC_SYNCHRONIZE);
8068 vec<tree, va_gc> *vec = make_tree_vector ();
8069 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
8070 release_tree_vector (vec);
8071 finish_expr_stmt (stmt);
8074 void
8075 finish_omp_taskwait (void)
8077 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_TASKWAIT);
8078 vec<tree, va_gc> *vec = make_tree_vector ();
8079 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
8080 release_tree_vector (vec);
8081 finish_expr_stmt (stmt);
8084 void
8085 finish_omp_taskyield (void)
8087 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_TASKYIELD);
8088 vec<tree, va_gc> *vec = make_tree_vector ();
8089 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
8090 release_tree_vector (vec);
8091 finish_expr_stmt (stmt);
8094 void
8095 finish_omp_cancel (tree clauses)
8097 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
8098 int mask = 0;
8099 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
8100 mask = 1;
8101 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
8102 mask = 2;
8103 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
8104 mask = 4;
8105 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
8106 mask = 8;
8107 else
8109 error ("%<#pragma omp cancel must specify one of "
8110 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> clauses");
8111 return;
8113 vec<tree, va_gc> *vec = make_tree_vector ();
8114 tree ifc = find_omp_clause (clauses, OMP_CLAUSE_IF);
8115 if (ifc != NULL_TREE)
8117 tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
8118 ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
8119 boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
8120 build_zero_cst (type));
8122 else
8123 ifc = boolean_true_node;
8124 vec->quick_push (build_int_cst (integer_type_node, mask));
8125 vec->quick_push (ifc);
8126 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
8127 release_tree_vector (vec);
8128 finish_expr_stmt (stmt);
8131 void
8132 finish_omp_cancellation_point (tree clauses)
8134 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
8135 int mask = 0;
8136 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
8137 mask = 1;
8138 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
8139 mask = 2;
8140 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
8141 mask = 4;
8142 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
8143 mask = 8;
8144 else
8146 error ("%<#pragma omp cancellation point must specify one of "
8147 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> clauses");
8148 return;
8150 vec<tree, va_gc> *vec
8151 = make_tree_vector_single (build_int_cst (integer_type_node, mask));
8152 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
8153 release_tree_vector (vec);
8154 finish_expr_stmt (stmt);
8157 /* Begin a __transaction_atomic or __transaction_relaxed statement.
8158 If PCOMPOUND is non-null, this is for a function-transaction-block, and we
8159 should create an extra compound stmt. */
8161 tree
8162 begin_transaction_stmt (location_t loc, tree *pcompound, int flags)
8164 tree r;
8166 if (pcompound)
8167 *pcompound = begin_compound_stmt (0);
8169 r = build_stmt (loc, TRANSACTION_EXPR, NULL_TREE);
8171 /* Only add the statement to the function if support enabled. */
8172 if (flag_tm)
8173 add_stmt (r);
8174 else
8175 error_at (loc, ((flags & TM_STMT_ATTR_RELAXED) != 0
8176 ? G_("%<__transaction_relaxed%> without "
8177 "transactional memory support enabled")
8178 : G_("%<__transaction_atomic%> without "
8179 "transactional memory support enabled")));
8181 TRANSACTION_EXPR_BODY (r) = push_stmt_list ();
8182 TREE_SIDE_EFFECTS (r) = 1;
8183 return r;
8186 /* End a __transaction_atomic or __transaction_relaxed statement.
8187 If COMPOUND_STMT is non-null, this is for a function-transaction-block,
8188 and we should end the compound. If NOEX is non-NULL, we wrap the body in
8189 a MUST_NOT_THROW_EXPR with NOEX as condition. */
8191 void
8192 finish_transaction_stmt (tree stmt, tree compound_stmt, int flags, tree noex)
8194 TRANSACTION_EXPR_BODY (stmt) = pop_stmt_list (TRANSACTION_EXPR_BODY (stmt));
8195 TRANSACTION_EXPR_OUTER (stmt) = (flags & TM_STMT_ATTR_OUTER) != 0;
8196 TRANSACTION_EXPR_RELAXED (stmt) = (flags & TM_STMT_ATTR_RELAXED) != 0;
8197 TRANSACTION_EXPR_IS_STMT (stmt) = 1;
8199 /* noexcept specifications are not allowed for function transactions. */
8200 gcc_assert (!(noex && compound_stmt));
8201 if (noex)
8203 tree body = build_must_not_throw_expr (TRANSACTION_EXPR_BODY (stmt),
8204 noex);
8205 protected_set_expr_location
8206 (body, EXPR_LOCATION (TRANSACTION_EXPR_BODY (stmt)));
8207 TREE_SIDE_EFFECTS (body) = 1;
8208 TRANSACTION_EXPR_BODY (stmt) = body;
8211 if (compound_stmt)
8212 finish_compound_stmt (compound_stmt);
8215 /* Build a __transaction_atomic or __transaction_relaxed expression. If
8216 NOEX is non-NULL, we wrap the body in a MUST_NOT_THROW_EXPR with NOEX as
8217 condition. */
8219 tree
8220 build_transaction_expr (location_t loc, tree expr, int flags, tree noex)
8222 tree ret;
8223 if (noex)
8225 expr = build_must_not_throw_expr (expr, noex);
8226 protected_set_expr_location (expr, loc);
8227 TREE_SIDE_EFFECTS (expr) = 1;
8229 ret = build1 (TRANSACTION_EXPR, TREE_TYPE (expr), expr);
8230 if (flags & TM_STMT_ATTR_RELAXED)
8231 TRANSACTION_EXPR_RELAXED (ret) = 1;
8232 TREE_SIDE_EFFECTS (ret) = 1;
8233 SET_EXPR_LOCATION (ret, loc);
8234 return ret;
8237 void
8238 init_cp_semantics (void)
8242 /* Build a STATIC_ASSERT for a static assertion with the condition
8243 CONDITION and the message text MESSAGE. LOCATION is the location
8244 of the static assertion in the source code. When MEMBER_P, this
8245 static assertion is a member of a class. */
8246 void
8247 finish_static_assert (tree condition, tree message, location_t location,
8248 bool member_p)
8250 if (message == NULL_TREE
8251 || message == error_mark_node
8252 || condition == NULL_TREE
8253 || condition == error_mark_node)
8254 return;
8256 if (check_for_bare_parameter_packs (condition))
8257 condition = error_mark_node;
8259 if (type_dependent_expression_p (condition)
8260 || value_dependent_expression_p (condition))
8262 /* We're in a template; build a STATIC_ASSERT and put it in
8263 the right place. */
8264 tree assertion;
8266 assertion = make_node (STATIC_ASSERT);
8267 STATIC_ASSERT_CONDITION (assertion) = condition;
8268 STATIC_ASSERT_MESSAGE (assertion) = message;
8269 STATIC_ASSERT_SOURCE_LOCATION (assertion) = location;
8271 if (member_p)
8272 maybe_add_class_template_decl_list (current_class_type,
8273 assertion,
8274 /*friend_p=*/0);
8275 else
8276 add_stmt (assertion);
8278 return;
8281 /* Fold the expression and convert it to a boolean value. */
8282 condition = instantiate_non_dependent_expr (condition);
8283 condition = cp_convert (boolean_type_node, condition, tf_warning_or_error);
8284 condition = maybe_constant_value (condition);
8286 if (TREE_CODE (condition) == INTEGER_CST && !integer_zerop (condition))
8287 /* Do nothing; the condition is satisfied. */
8289 else
8291 location_t saved_loc = input_location;
8293 input_location = location;
8294 if (TREE_CODE (condition) == INTEGER_CST
8295 && integer_zerop (condition))
8297 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
8298 (TREE_TYPE (TREE_TYPE (message))));
8299 int len = TREE_STRING_LENGTH (message) / sz - 1;
8300 /* Report the error. */
8301 if (len == 0)
8302 error ("static assertion failed");
8303 else
8304 error ("static assertion failed: %s",
8305 TREE_STRING_POINTER (message));
8307 else if (condition && condition != error_mark_node)
8309 error ("non-constant condition for static assertion");
8310 if (require_potential_rvalue_constant_expression (condition))
8311 cxx_constant_value (condition);
8313 input_location = saved_loc;
8317 /* Implements the C++0x decltype keyword. Returns the type of EXPR,
8318 suitable for use as a type-specifier.
8320 ID_EXPRESSION_OR_MEMBER_ACCESS_P is true when EXPR was parsed as an
8321 id-expression or a class member access, FALSE when it was parsed as
8322 a full expression. */
8324 tree
8325 finish_decltype_type (tree expr, bool id_expression_or_member_access_p,
8326 tsubst_flags_t complain)
8328 tree type = NULL_TREE;
8330 if (!expr || error_operand_p (expr))
8331 return error_mark_node;
8333 if (TYPE_P (expr)
8334 || TREE_CODE (expr) == TYPE_DECL
8335 || (TREE_CODE (expr) == BIT_NOT_EXPR
8336 && TYPE_P (TREE_OPERAND (expr, 0))))
8338 if (complain & tf_error)
8339 error ("argument to decltype must be an expression");
8340 return error_mark_node;
8343 /* Depending on the resolution of DR 1172, we may later need to distinguish
8344 instantiation-dependent but not type-dependent expressions so that, say,
8345 A<decltype(sizeof(T))>::U doesn't require 'typename'. */
8346 if (instantiation_dependent_expression_p (expr))
8348 type = cxx_make_type (DECLTYPE_TYPE);
8349 DECLTYPE_TYPE_EXPR (type) = expr;
8350 DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (type)
8351 = id_expression_or_member_access_p;
8352 SET_TYPE_STRUCTURAL_EQUALITY (type);
8354 return type;
8357 /* The type denoted by decltype(e) is defined as follows: */
8359 expr = resolve_nondeduced_context (expr);
8361 if (invalid_nonstatic_memfn_p (input_location, expr, complain))
8362 return error_mark_node;
8364 if (type_unknown_p (expr))
8366 if (complain & tf_error)
8367 error ("decltype cannot resolve address of overloaded function");
8368 return error_mark_node;
8371 /* To get the size of a static data member declared as an array of
8372 unknown bound, we need to instantiate it. */
8373 if (VAR_P (expr)
8374 && VAR_HAD_UNKNOWN_BOUND (expr)
8375 && DECL_TEMPLATE_INSTANTIATION (expr))
8376 instantiate_decl (expr, /*defer_ok*/true, /*expl_inst_mem*/false);
8378 if (id_expression_or_member_access_p)
8380 /* If e is an id-expression or a class member access (5.2.5
8381 [expr.ref]), decltype(e) is defined as the type of the entity
8382 named by e. If there is no such entity, or e names a set of
8383 overloaded functions, the program is ill-formed. */
8384 if (identifier_p (expr))
8385 expr = lookup_name (expr);
8387 if (INDIRECT_REF_P (expr))
8388 /* This can happen when the expression is, e.g., "a.b". Just
8389 look at the underlying operand. */
8390 expr = TREE_OPERAND (expr, 0);
8392 if (TREE_CODE (expr) == OFFSET_REF
8393 || TREE_CODE (expr) == MEMBER_REF
8394 || TREE_CODE (expr) == SCOPE_REF)
8395 /* We're only interested in the field itself. If it is a
8396 BASELINK, we will need to see through it in the next
8397 step. */
8398 expr = TREE_OPERAND (expr, 1);
8400 if (BASELINK_P (expr))
8401 /* See through BASELINK nodes to the underlying function. */
8402 expr = BASELINK_FUNCTIONS (expr);
8404 switch (TREE_CODE (expr))
8406 case FIELD_DECL:
8407 if (DECL_BIT_FIELD_TYPE (expr))
8409 type = DECL_BIT_FIELD_TYPE (expr);
8410 break;
8412 /* Fall through for fields that aren't bitfields. */
8414 case FUNCTION_DECL:
8415 case VAR_DECL:
8416 case CONST_DECL:
8417 case PARM_DECL:
8418 case RESULT_DECL:
8419 case TEMPLATE_PARM_INDEX:
8420 expr = mark_type_use (expr);
8421 type = TREE_TYPE (expr);
8422 break;
8424 case ERROR_MARK:
8425 type = error_mark_node;
8426 break;
8428 case COMPONENT_REF:
8429 case COMPOUND_EXPR:
8430 mark_type_use (expr);
8431 type = is_bitfield_expr_with_lowered_type (expr);
8432 if (!type)
8433 type = TREE_TYPE (TREE_OPERAND (expr, 1));
8434 break;
8436 case BIT_FIELD_REF:
8437 gcc_unreachable ();
8439 case INTEGER_CST:
8440 case PTRMEM_CST:
8441 /* We can get here when the id-expression refers to an
8442 enumerator or non-type template parameter. */
8443 type = TREE_TYPE (expr);
8444 break;
8446 default:
8447 /* Handle instantiated template non-type arguments. */
8448 type = TREE_TYPE (expr);
8449 break;
8452 else
8454 /* Within a lambda-expression:
8456 Every occurrence of decltype((x)) where x is a possibly
8457 parenthesized id-expression that names an entity of
8458 automatic storage duration is treated as if x were
8459 transformed into an access to a corresponding data member
8460 of the closure type that would have been declared if x
8461 were a use of the denoted entity. */
8462 if (outer_automatic_var_p (expr)
8463 && current_function_decl
8464 && LAMBDA_FUNCTION_P (current_function_decl))
8465 type = capture_decltype (expr);
8466 else if (error_operand_p (expr))
8467 type = error_mark_node;
8468 else if (expr == current_class_ptr)
8469 /* If the expression is just "this", we want the
8470 cv-unqualified pointer for the "this" type. */
8471 type = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
8472 else
8474 /* Otherwise, where T is the type of e, if e is an lvalue,
8475 decltype(e) is defined as T&; if an xvalue, T&&; otherwise, T. */
8476 cp_lvalue_kind clk = lvalue_kind (expr);
8477 type = unlowered_expr_type (expr);
8478 gcc_assert (TREE_CODE (type) != REFERENCE_TYPE);
8480 /* For vector types, pick a non-opaque variant. */
8481 if (VECTOR_TYPE_P (type))
8482 type = strip_typedefs (type);
8484 if (clk != clk_none && !(clk & clk_class))
8485 type = cp_build_reference_type (type, (clk & clk_rvalueref));
8489 return type;
8492 /* Called from trait_expr_value to evaluate either __has_nothrow_assign or
8493 __has_nothrow_copy, depending on assign_p. */
8495 static bool
8496 classtype_has_nothrow_assign_or_copy_p (tree type, bool assign_p)
8498 tree fns;
8500 if (assign_p)
8502 int ix;
8503 ix = lookup_fnfields_1 (type, ansi_assopname (NOP_EXPR));
8504 if (ix < 0)
8505 return false;
8506 fns = (*CLASSTYPE_METHOD_VEC (type))[ix];
8508 else if (TYPE_HAS_COPY_CTOR (type))
8510 /* If construction of the copy constructor was postponed, create
8511 it now. */
8512 if (CLASSTYPE_LAZY_COPY_CTOR (type))
8513 lazily_declare_fn (sfk_copy_constructor, type);
8514 if (CLASSTYPE_LAZY_MOVE_CTOR (type))
8515 lazily_declare_fn (sfk_move_constructor, type);
8516 fns = CLASSTYPE_CONSTRUCTORS (type);
8518 else
8519 return false;
8521 for (; fns; fns = OVL_NEXT (fns))
8523 tree fn = OVL_CURRENT (fns);
8525 if (assign_p)
8527 if (copy_fn_p (fn) == 0)
8528 continue;
8530 else if (copy_fn_p (fn) <= 0)
8531 continue;
8533 maybe_instantiate_noexcept (fn);
8534 if (!TYPE_NOTHROW_P (TREE_TYPE (fn)))
8535 return false;
8538 return true;
8541 /* Actually evaluates the trait. */
8543 static bool
8544 trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
8546 enum tree_code type_code1;
8547 tree t;
8549 type_code1 = TREE_CODE (type1);
8551 switch (kind)
8553 case CPTK_HAS_NOTHROW_ASSIGN:
8554 type1 = strip_array_types (type1);
8555 return (!CP_TYPE_CONST_P (type1) && type_code1 != REFERENCE_TYPE
8556 && (trait_expr_value (CPTK_HAS_TRIVIAL_ASSIGN, type1, type2)
8557 || (CLASS_TYPE_P (type1)
8558 && classtype_has_nothrow_assign_or_copy_p (type1,
8559 true))));
8561 case CPTK_HAS_TRIVIAL_ASSIGN:
8562 /* ??? The standard seems to be missing the "or array of such a class
8563 type" wording for this trait. */
8564 type1 = strip_array_types (type1);
8565 return (!CP_TYPE_CONST_P (type1) && type_code1 != REFERENCE_TYPE
8566 && (trivial_type_p (type1)
8567 || (CLASS_TYPE_P (type1)
8568 && TYPE_HAS_TRIVIAL_COPY_ASSIGN (type1))));
8570 case CPTK_HAS_NOTHROW_CONSTRUCTOR:
8571 type1 = strip_array_types (type1);
8572 return (trait_expr_value (CPTK_HAS_TRIVIAL_CONSTRUCTOR, type1, type2)
8573 || (CLASS_TYPE_P (type1)
8574 && (t = locate_ctor (type1))
8575 && (maybe_instantiate_noexcept (t),
8576 TYPE_NOTHROW_P (TREE_TYPE (t)))));
8578 case CPTK_HAS_TRIVIAL_CONSTRUCTOR:
8579 type1 = strip_array_types (type1);
8580 return (trivial_type_p (type1)
8581 || (CLASS_TYPE_P (type1) && TYPE_HAS_TRIVIAL_DFLT (type1)));
8583 case CPTK_HAS_NOTHROW_COPY:
8584 type1 = strip_array_types (type1);
8585 return (trait_expr_value (CPTK_HAS_TRIVIAL_COPY, type1, type2)
8586 || (CLASS_TYPE_P (type1)
8587 && classtype_has_nothrow_assign_or_copy_p (type1, false)));
8589 case CPTK_HAS_TRIVIAL_COPY:
8590 /* ??? The standard seems to be missing the "or array of such a class
8591 type" wording for this trait. */
8592 type1 = strip_array_types (type1);
8593 return (trivial_type_p (type1) || type_code1 == REFERENCE_TYPE
8594 || (CLASS_TYPE_P (type1) && TYPE_HAS_TRIVIAL_COPY_CTOR (type1)));
8596 case CPTK_HAS_TRIVIAL_DESTRUCTOR:
8597 type1 = strip_array_types (type1);
8598 return (trivial_type_p (type1) || type_code1 == REFERENCE_TYPE
8599 || (CLASS_TYPE_P (type1)
8600 && TYPE_HAS_TRIVIAL_DESTRUCTOR (type1)));
8602 case CPTK_HAS_VIRTUAL_DESTRUCTOR:
8603 return type_has_virtual_destructor (type1);
8605 case CPTK_IS_ABSTRACT:
8606 return (ABSTRACT_CLASS_TYPE_P (type1));
8608 case CPTK_IS_BASE_OF:
8609 return (NON_UNION_CLASS_TYPE_P (type1) && NON_UNION_CLASS_TYPE_P (type2)
8610 && (same_type_ignoring_top_level_qualifiers_p (type1, type2)
8611 || DERIVED_FROM_P (type1, type2)));
8613 case CPTK_IS_CLASS:
8614 return (NON_UNION_CLASS_TYPE_P (type1));
8616 case CPTK_IS_EMPTY:
8617 return (NON_UNION_CLASS_TYPE_P (type1) && CLASSTYPE_EMPTY_P (type1));
8619 case CPTK_IS_ENUM:
8620 return (type_code1 == ENUMERAL_TYPE);
8622 case CPTK_IS_FINAL:
8623 return (CLASS_TYPE_P (type1) && CLASSTYPE_FINAL (type1));
8625 case CPTK_IS_LITERAL_TYPE:
8626 return (literal_type_p (type1));
8628 case CPTK_IS_POD:
8629 return (pod_type_p (type1));
8631 case CPTK_IS_POLYMORPHIC:
8632 return (CLASS_TYPE_P (type1) && TYPE_POLYMORPHIC_P (type1));
8634 case CPTK_IS_SAME_AS:
8635 return same_type_p (type1, type2);
8637 case CPTK_IS_STD_LAYOUT:
8638 return (std_layout_type_p (type1));
8640 case CPTK_IS_TRIVIAL:
8641 return (trivial_type_p (type1));
8643 case CPTK_IS_TRIVIALLY_ASSIGNABLE:
8644 return is_trivially_xible (MODIFY_EXPR, type1, type2);
8646 case CPTK_IS_TRIVIALLY_CONSTRUCTIBLE:
8647 return is_trivially_xible (INIT_EXPR, type1, type2);
8649 case CPTK_IS_TRIVIALLY_COPYABLE:
8650 return (trivially_copyable_p (type1));
8652 case CPTK_IS_UNION:
8653 return (type_code1 == UNION_TYPE);
8655 default:
8656 gcc_unreachable ();
8657 return false;
8661 /* If TYPE is an array of unknown bound, or (possibly cv-qualified)
8662 void, or a complete type, returns true, otherwise false. */
8664 static bool
8665 check_trait_type (tree type)
8667 if (type == NULL_TREE)
8668 return true;
8670 if (TREE_CODE (type) == TREE_LIST)
8671 return (check_trait_type (TREE_VALUE (type))
8672 && check_trait_type (TREE_CHAIN (type)));
8674 if (TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type)
8675 && COMPLETE_TYPE_P (TREE_TYPE (type)))
8676 return true;
8678 if (VOID_TYPE_P (type))
8679 return true;
8681 return !!complete_type_or_else (strip_array_types (type), NULL_TREE);
8684 /* Process a trait expression. */
8686 tree
8687 finish_trait_expr (cp_trait_kind kind, tree type1, tree type2)
8689 if (type1 == error_mark_node
8690 || type2 == error_mark_node)
8691 return error_mark_node;
8693 if (processing_template_decl)
8695 tree trait_expr = make_node (TRAIT_EXPR);
8696 TREE_TYPE (trait_expr) = boolean_type_node;
8697 TRAIT_EXPR_TYPE1 (trait_expr) = type1;
8698 TRAIT_EXPR_TYPE2 (trait_expr) = type2;
8699 TRAIT_EXPR_KIND (trait_expr) = kind;
8700 return trait_expr;
8703 switch (kind)
8705 case CPTK_HAS_NOTHROW_ASSIGN:
8706 case CPTK_HAS_TRIVIAL_ASSIGN:
8707 case CPTK_HAS_NOTHROW_CONSTRUCTOR:
8708 case CPTK_HAS_TRIVIAL_CONSTRUCTOR:
8709 case CPTK_HAS_NOTHROW_COPY:
8710 case CPTK_HAS_TRIVIAL_COPY:
8711 case CPTK_HAS_TRIVIAL_DESTRUCTOR:
8712 case CPTK_HAS_VIRTUAL_DESTRUCTOR:
8713 case CPTK_IS_ABSTRACT:
8714 case CPTK_IS_EMPTY:
8715 case CPTK_IS_FINAL:
8716 case CPTK_IS_LITERAL_TYPE:
8717 case CPTK_IS_POD:
8718 case CPTK_IS_POLYMORPHIC:
8719 case CPTK_IS_STD_LAYOUT:
8720 case CPTK_IS_TRIVIAL:
8721 case CPTK_IS_TRIVIALLY_COPYABLE:
8722 if (!check_trait_type (type1))
8723 return error_mark_node;
8724 break;
8726 case CPTK_IS_TRIVIALLY_ASSIGNABLE:
8727 case CPTK_IS_TRIVIALLY_CONSTRUCTIBLE:
8728 if (!check_trait_type (type1)
8729 || !check_trait_type (type2))
8730 return error_mark_node;
8731 break;
8733 case CPTK_IS_BASE_OF:
8734 if (NON_UNION_CLASS_TYPE_P (type1) && NON_UNION_CLASS_TYPE_P (type2)
8735 && !same_type_ignoring_top_level_qualifiers_p (type1, type2)
8736 && !complete_type_or_else (type2, NULL_TREE))
8737 /* We already issued an error. */
8738 return error_mark_node;
8739 break;
8741 case CPTK_IS_CLASS:
8742 case CPTK_IS_ENUM:
8743 case CPTK_IS_UNION:
8744 case CPTK_IS_SAME_AS:
8745 break;
8747 default:
8748 gcc_unreachable ();
8751 return (trait_expr_value (kind, type1, type2)
8752 ? boolean_true_node : boolean_false_node);
8755 /* Do-nothing variants of functions to handle pragma FLOAT_CONST_DECIMAL64,
8756 which is ignored for C++. */
8758 void
8759 set_float_const_decimal64 (void)
8763 void
8764 clear_float_const_decimal64 (void)
8768 bool
8769 float_const_decimal64_p (void)
8771 return 0;
8775 /* Return true if T designates the implied `this' parameter. */
8777 bool
8778 is_this_parameter (tree t)
8780 if (!DECL_P (t) || DECL_NAME (t) != this_identifier)
8781 return false;
8782 gcc_assert (TREE_CODE (t) == PARM_DECL || is_capture_proxy (t));
8783 return true;
8786 /* Insert the deduced return type for an auto function. */
8788 void
8789 apply_deduced_return_type (tree fco, tree return_type)
8791 tree result;
8793 if (return_type == error_mark_node)
8794 return;
8796 if (LAMBDA_FUNCTION_P (fco))
8798 tree lambda = CLASSTYPE_LAMBDA_EXPR (current_class_type);
8799 LAMBDA_EXPR_RETURN_TYPE (lambda) = return_type;
8802 if (DECL_CONV_FN_P (fco))
8803 DECL_NAME (fco) = mangle_conv_op_name_for_type (return_type);
8805 TREE_TYPE (fco) = change_return_type (return_type, TREE_TYPE (fco));
8807 result = DECL_RESULT (fco);
8808 if (result == NULL_TREE)
8809 return;
8810 if (TREE_TYPE (result) == return_type)
8811 return;
8813 /* We already have a DECL_RESULT from start_preparsed_function.
8814 Now we need to redo the work it and allocate_struct_function
8815 did to reflect the new type. */
8816 gcc_assert (current_function_decl == fco);
8817 result = build_decl (input_location, RESULT_DECL, NULL_TREE,
8818 TYPE_MAIN_VARIANT (return_type));
8819 DECL_ARTIFICIAL (result) = 1;
8820 DECL_IGNORED_P (result) = 1;
8821 cp_apply_type_quals_to_decl (cp_type_quals (return_type),
8822 result);
8824 DECL_RESULT (fco) = result;
8826 if (!processing_template_decl)
8828 if (!VOID_TYPE_P (TREE_TYPE (result)))
8829 complete_type_or_else (TREE_TYPE (result), NULL_TREE);
8830 bool aggr = aggregate_value_p (result, fco);
8831 #ifdef PCC_STATIC_STRUCT_RETURN
8832 cfun->returns_pcc_struct = aggr;
8833 #endif
8834 cfun->returns_struct = aggr;
8839 /* DECL is a local variable or parameter from the surrounding scope of a
8840 lambda-expression. Returns the decltype for a use of the capture field
8841 for DECL even if it hasn't been captured yet. */
8843 static tree
8844 capture_decltype (tree decl)
8846 tree lam = CLASSTYPE_LAMBDA_EXPR (DECL_CONTEXT (current_function_decl));
8847 /* FIXME do lookup instead of list walk? */
8848 tree cap = value_member (decl, LAMBDA_EXPR_CAPTURE_LIST (lam));
8849 tree type;
8851 if (cap)
8852 type = TREE_TYPE (TREE_PURPOSE (cap));
8853 else
8854 switch (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lam))
8856 case CPLD_NONE:
8857 error ("%qD is not captured", decl);
8858 return error_mark_node;
8860 case CPLD_COPY:
8861 type = TREE_TYPE (decl);
8862 if (TREE_CODE (type) == REFERENCE_TYPE
8863 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
8864 type = TREE_TYPE (type);
8865 break;
8867 case CPLD_REFERENCE:
8868 type = TREE_TYPE (decl);
8869 if (TREE_CODE (type) != REFERENCE_TYPE)
8870 type = build_reference_type (TREE_TYPE (decl));
8871 break;
8873 default:
8874 gcc_unreachable ();
8877 if (TREE_CODE (type) != REFERENCE_TYPE)
8879 if (!LAMBDA_EXPR_MUTABLE_P (lam))
8880 type = cp_build_qualified_type (type, (cp_type_quals (type)
8881 |TYPE_QUAL_CONST));
8882 type = build_reference_type (type);
8884 return type;
8887 /* Build a unary fold expression of EXPR over OP. If IS_RIGHT is true,
8888 this is a right unary fold. Otherwise it is a left unary fold. */
8890 static tree
8891 finish_unary_fold_expr (tree expr, int op, tree_code dir)
8893 // Build a pack expansion (assuming expr has pack type).
8894 if (!uses_parameter_packs (expr))
8896 error_at (location_of (expr), "operand of fold expression has no "
8897 "unexpanded parameter packs");
8898 return error_mark_node;
8900 tree pack = make_pack_expansion (expr);
8902 // Build the fold expression.
8903 tree code = build_int_cstu (integer_type_node, abs (op));
8904 tree fold = build_min (dir, unknown_type_node, code, pack);
8905 FOLD_EXPR_MODIFY_P (fold) = (op < 0);
8906 return fold;
8909 tree
8910 finish_left_unary_fold_expr (tree expr, int op)
8912 return finish_unary_fold_expr (expr, op, UNARY_LEFT_FOLD_EXPR);
8915 tree
8916 finish_right_unary_fold_expr (tree expr, int op)
8918 return finish_unary_fold_expr (expr, op, UNARY_RIGHT_FOLD_EXPR);
8921 /* Build a binary fold expression over EXPR1 and EXPR2. The
8922 associativity of the fold is determined by EXPR1 and EXPR2 (whichever
8923 has an unexpanded parameter pack). */
8925 tree
8926 finish_binary_fold_expr (tree pack, tree init, int op, tree_code dir)
8928 pack = make_pack_expansion (pack);
8929 tree code = build_int_cstu (integer_type_node, abs (op));
8930 tree fold = build_min (dir, unknown_type_node, code, pack, init);
8931 FOLD_EXPR_MODIFY_P (fold) = (op < 0);
8932 return fold;
8935 tree
8936 finish_binary_fold_expr (tree expr1, tree expr2, int op)
8938 // Determine which expr has an unexpanded parameter pack and
8939 // set the pack and initial term.
8940 bool pack1 = uses_parameter_packs (expr1);
8941 bool pack2 = uses_parameter_packs (expr2);
8942 if (pack1 && !pack2)
8943 return finish_binary_fold_expr (expr1, expr2, op, BINARY_RIGHT_FOLD_EXPR);
8944 else if (pack2 && !pack1)
8945 return finish_binary_fold_expr (expr2, expr1, op, BINARY_LEFT_FOLD_EXPR);
8946 else
8948 if (pack1)
8949 error ("both arguments in binary fold have unexpanded parameter packs");
8950 else
8951 error ("no unexpanded parameter packs in binary fold");
8953 return error_mark_node;
8956 #include "gt-cp-semantics.h"