PR c++/86728 - C variadic generic lambda.
[official-gcc.git] / gcc / cp / semantics.c
blobbfdca5024d3128b66a6b9b6a4408871991c03335
1 /* Perform the semantic phase of parsing, i.e., the process of
2 building tree structure, checking semantic consistency, and
3 building RTL. These routines are used both during actual parsing
4 and during the instantiation of template functions.
6 Copyright (C) 1998-2018 Free Software Foundation, Inc.
7 Written by Mark Mitchell (mmitchell@usa.net) based on code found
8 formerly in parse.y and pt.c.
10 This file is part of GCC.
12 GCC is free software; you can redistribute it and/or modify it
13 under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 3, or (at your option)
15 any later version.
17 GCC is distributed in the hope that it will be useful, but
18 WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with GCC; see the file COPYING3. If not see
24 <http://www.gnu.org/licenses/>. */
26 #include "config.h"
27 #include "system.h"
28 #include "coretypes.h"
29 #include "target.h"
30 #include "bitmap.h"
31 #include "cp-tree.h"
32 #include "stringpool.h"
33 #include "cgraph.h"
34 #include "stmt.h"
35 #include "varasm.h"
36 #include "stor-layout.h"
37 #include "c-family/c-objc.h"
38 #include "tree-inline.h"
39 #include "intl.h"
40 #include "tree-iterator.h"
41 #include "omp-general.h"
42 #include "convert.h"
43 #include "stringpool.h"
44 #include "attribs.h"
45 #include "gomp-constants.h"
46 #include "predict.h"
48 /* There routines provide a modular interface to perform many parsing
49 operations. They may therefore be used during actual parsing, or
50 during template instantiation, which may be regarded as a
51 degenerate form of parsing. */
53 static tree maybe_convert_cond (tree);
54 static tree finalize_nrv_r (tree *, int *, void *);
55 static tree capture_decltype (tree);
57 /* Used for OpenMP non-static data member privatization. */
59 static hash_map<tree, tree> *omp_private_member_map;
60 static vec<tree> omp_private_member_vec;
61 static bool omp_private_member_ignore_next;
64 /* Deferred Access Checking Overview
65 ---------------------------------
67 Most C++ expressions and declarations require access checking
68 to be performed during parsing. However, in several cases,
69 this has to be treated differently.
71 For member declarations, access checking has to be deferred
72 until more information about the declaration is known. For
73 example:
75 class A {
76 typedef int X;
77 public:
78 X f();
81 A::X A::f();
82 A::X g();
84 When we are parsing the function return type `A::X', we don't
85 really know if this is allowed until we parse the function name.
87 Furthermore, some contexts require that access checking is
88 never performed at all. These include class heads, and template
89 instantiations.
91 Typical use of access checking functions is described here:
93 1. When we enter a context that requires certain access checking
94 mode, the function `push_deferring_access_checks' is called with
95 DEFERRING argument specifying the desired mode. Access checking
96 may be performed immediately (dk_no_deferred), deferred
97 (dk_deferred), or not performed (dk_no_check).
99 2. When a declaration such as a type, or a variable, is encountered,
100 the function `perform_or_defer_access_check' is called. It
101 maintains a vector of all deferred checks.
103 3. The global `current_class_type' or `current_function_decl' is then
104 setup by the parser. `enforce_access' relies on these information
105 to check access.
107 4. Upon exiting the context mentioned in step 1,
108 `perform_deferred_access_checks' is called to check all declaration
109 stored in the vector. `pop_deferring_access_checks' is then
110 called to restore the previous access checking mode.
112 In case of parsing error, we simply call `pop_deferring_access_checks'
113 without `perform_deferred_access_checks'. */
115 struct GTY(()) deferred_access {
116 /* A vector representing name-lookups for which we have deferred
117 checking access controls. We cannot check the accessibility of
118 names used in a decl-specifier-seq until we know what is being
119 declared because code like:
121 class A {
122 class B {};
123 B* f();
126 A::B* A::f() { return 0; }
128 is valid, even though `A::B' is not generally accessible. */
129 vec<deferred_access_check, va_gc> * GTY(()) deferred_access_checks;
131 /* The current mode of access checks. */
132 enum deferring_kind deferring_access_checks_kind;
136 /* Data for deferred access checking. */
137 static GTY(()) vec<deferred_access, va_gc> *deferred_access_stack;
138 static GTY(()) unsigned deferred_access_no_check;
140 /* Save the current deferred access states and start deferred
141 access checking iff DEFER_P is true. */
143 void
144 push_deferring_access_checks (deferring_kind deferring)
146 /* For context like template instantiation, access checking
147 disabling applies to all nested context. */
148 if (deferred_access_no_check || deferring == dk_no_check)
149 deferred_access_no_check++;
150 else
152 deferred_access e = {NULL, deferring};
153 vec_safe_push (deferred_access_stack, e);
157 /* Save the current deferred access states and start deferred access
158 checking, continuing the set of deferred checks in CHECKS. */
160 void
161 reopen_deferring_access_checks (vec<deferred_access_check, va_gc> * checks)
163 push_deferring_access_checks (dk_deferred);
164 if (!deferred_access_no_check)
165 deferred_access_stack->last().deferred_access_checks = checks;
168 /* Resume deferring access checks again after we stopped doing
169 this previously. */
171 void
172 resume_deferring_access_checks (void)
174 if (!deferred_access_no_check)
175 deferred_access_stack->last().deferring_access_checks_kind = dk_deferred;
178 /* Stop deferring access checks. */
180 void
181 stop_deferring_access_checks (void)
183 if (!deferred_access_no_check)
184 deferred_access_stack->last().deferring_access_checks_kind = dk_no_deferred;
187 /* Discard the current deferred access checks and restore the
188 previous states. */
190 void
191 pop_deferring_access_checks (void)
193 if (deferred_access_no_check)
194 deferred_access_no_check--;
195 else
196 deferred_access_stack->pop ();
199 /* Returns a TREE_LIST representing the deferred checks.
200 The TREE_PURPOSE of each node is the type through which the
201 access occurred; the TREE_VALUE is the declaration named.
204 vec<deferred_access_check, va_gc> *
205 get_deferred_access_checks (void)
207 if (deferred_access_no_check)
208 return NULL;
209 else
210 return (deferred_access_stack->last().deferred_access_checks);
213 /* Take current deferred checks and combine with the
214 previous states if we also defer checks previously.
215 Otherwise perform checks now. */
217 void
218 pop_to_parent_deferring_access_checks (void)
220 if (deferred_access_no_check)
221 deferred_access_no_check--;
222 else
224 vec<deferred_access_check, va_gc> *checks;
225 deferred_access *ptr;
227 checks = (deferred_access_stack->last ().deferred_access_checks);
229 deferred_access_stack->pop ();
230 ptr = &deferred_access_stack->last ();
231 if (ptr->deferring_access_checks_kind == dk_no_deferred)
233 /* Check access. */
234 perform_access_checks (checks, tf_warning_or_error);
236 else
238 /* Merge with parent. */
239 int i, j;
240 deferred_access_check *chk, *probe;
242 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
244 FOR_EACH_VEC_SAFE_ELT (ptr->deferred_access_checks, j, probe)
246 if (probe->binfo == chk->binfo &&
247 probe->decl == chk->decl &&
248 probe->diag_decl == chk->diag_decl)
249 goto found;
251 /* Insert into parent's checks. */
252 vec_safe_push (ptr->deferred_access_checks, *chk);
253 found:;
259 /* Perform the access checks in CHECKS. The TREE_PURPOSE of each node
260 is the BINFO indicating the qualifying scope used to access the
261 DECL node stored in the TREE_VALUE of the node. If CHECKS is empty
262 or we aren't in SFINAE context or all the checks succeed return TRUE,
263 otherwise FALSE. */
265 bool
266 perform_access_checks (vec<deferred_access_check, va_gc> *checks,
267 tsubst_flags_t complain)
269 int i;
270 deferred_access_check *chk;
271 location_t loc = input_location;
272 bool ok = true;
274 if (!checks)
275 return true;
277 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
279 input_location = chk->loc;
280 ok &= enforce_access (chk->binfo, chk->decl, chk->diag_decl, complain);
283 input_location = loc;
284 return (complain & tf_error) ? true : ok;
287 /* Perform the deferred access checks.
289 After performing the checks, we still have to keep the list
290 `deferred_access_stack->deferred_access_checks' since we may want
291 to check access for them again later in a different context.
292 For example:
294 class A {
295 typedef int X;
296 static X a;
298 A::X A::a, x; // No error for `A::a', error for `x'
300 We have to perform deferred access of `A::X', first with `A::a',
301 next with `x'. Return value like perform_access_checks above. */
303 bool
304 perform_deferred_access_checks (tsubst_flags_t complain)
306 return perform_access_checks (get_deferred_access_checks (), complain);
309 /* Defer checking the accessibility of DECL, when looked up in
310 BINFO. DIAG_DECL is the declaration to use to print diagnostics.
311 Return value like perform_access_checks above.
312 If non-NULL, report failures to AFI. */
314 bool
315 perform_or_defer_access_check (tree binfo, tree decl, tree diag_decl,
316 tsubst_flags_t complain,
317 access_failure_info *afi)
319 int i;
320 deferred_access *ptr;
321 deferred_access_check *chk;
324 /* Exit if we are in a context that no access checking is performed.
326 if (deferred_access_no_check)
327 return true;
329 gcc_assert (TREE_CODE (binfo) == TREE_BINFO);
331 ptr = &deferred_access_stack->last ();
333 /* If we are not supposed to defer access checks, just check now. */
334 if (ptr->deferring_access_checks_kind == dk_no_deferred)
336 bool ok = enforce_access (binfo, decl, diag_decl, complain, afi);
337 return (complain & tf_error) ? true : ok;
340 /* See if we are already going to perform this check. */
341 FOR_EACH_VEC_SAFE_ELT (ptr->deferred_access_checks, i, chk)
343 if (chk->decl == decl && chk->binfo == binfo &&
344 chk->diag_decl == diag_decl)
346 return true;
349 /* If not, record the check. */
350 deferred_access_check new_access = {binfo, decl, diag_decl, input_location};
351 vec_safe_push (ptr->deferred_access_checks, new_access);
353 return true;
356 /* Returns nonzero if the current statement is a full expression,
357 i.e. temporaries created during that statement should be destroyed
358 at the end of the statement. */
361 stmts_are_full_exprs_p (void)
363 return current_stmt_tree ()->stmts_are_full_exprs_p;
366 /* T is a statement. Add it to the statement-tree. This is the C++
367 version. The C/ObjC frontends have a slightly different version of
368 this function. */
370 tree
371 add_stmt (tree t)
373 enum tree_code code = TREE_CODE (t);
375 if (EXPR_P (t) && code != LABEL_EXPR)
377 if (!EXPR_HAS_LOCATION (t))
378 SET_EXPR_LOCATION (t, input_location);
380 /* When we expand a statement-tree, we must know whether or not the
381 statements are full-expressions. We record that fact here. */
382 STMT_IS_FULL_EXPR_P (t) = stmts_are_full_exprs_p ();
385 if (code == LABEL_EXPR || code == CASE_LABEL_EXPR)
386 STATEMENT_LIST_HAS_LABEL (cur_stmt_list) = 1;
388 /* Add T to the statement-tree. Non-side-effect statements need to be
389 recorded during statement expressions. */
390 gcc_checking_assert (!stmt_list_stack->is_empty ());
391 append_to_statement_list_force (t, &cur_stmt_list);
393 return t;
396 /* Returns the stmt_tree to which statements are currently being added. */
398 stmt_tree
399 current_stmt_tree (void)
401 return (cfun
402 ? &cfun->language->base.x_stmt_tree
403 : &scope_chain->x_stmt_tree);
406 /* If statements are full expressions, wrap STMT in a CLEANUP_POINT_EXPR. */
408 static tree
409 maybe_cleanup_point_expr (tree expr)
411 if (!processing_template_decl && stmts_are_full_exprs_p ())
412 expr = fold_build_cleanup_point_expr (TREE_TYPE (expr), expr);
413 return expr;
416 /* Like maybe_cleanup_point_expr except have the type of the new expression be
417 void so we don't need to create a temporary variable to hold the inner
418 expression. The reason why we do this is because the original type might be
419 an aggregate and we cannot create a temporary variable for that type. */
421 tree
422 maybe_cleanup_point_expr_void (tree expr)
424 if (!processing_template_decl && stmts_are_full_exprs_p ())
425 expr = fold_build_cleanup_point_expr (void_type_node, expr);
426 return expr;
431 /* Create a declaration statement for the declaration given by the DECL. */
433 void
434 add_decl_expr (tree decl)
436 tree r = build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl);
437 if (DECL_INITIAL (decl)
438 || (DECL_SIZE (decl) && TREE_SIDE_EFFECTS (DECL_SIZE (decl))))
439 r = maybe_cleanup_point_expr_void (r);
440 add_stmt (r);
443 /* Finish a scope. */
445 tree
446 do_poplevel (tree stmt_list)
448 tree block = NULL;
450 if (stmts_are_full_exprs_p ())
451 block = poplevel (kept_level_p (), 1, 0);
453 stmt_list = pop_stmt_list (stmt_list);
455 if (!processing_template_decl)
457 stmt_list = c_build_bind_expr (input_location, block, stmt_list);
458 /* ??? See c_end_compound_stmt re statement expressions. */
461 return stmt_list;
464 /* Begin a new scope. */
466 static tree
467 do_pushlevel (scope_kind sk)
469 tree ret = push_stmt_list ();
470 if (stmts_are_full_exprs_p ())
471 begin_scope (sk, NULL);
472 return ret;
475 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
476 when the current scope is exited. EH_ONLY is true when this is not
477 meant to apply to normal control flow transfer. */
479 void
480 push_cleanup (tree decl, tree cleanup, bool eh_only)
482 tree stmt = build_stmt (input_location, CLEANUP_STMT, NULL, cleanup, decl);
483 CLEANUP_EH_ONLY (stmt) = eh_only;
484 add_stmt (stmt);
485 CLEANUP_BODY (stmt) = push_stmt_list ();
488 /* Simple infinite loop tracking for -Wreturn-type. We keep a stack of all
489 the current loops, represented by 'NULL_TREE' if we've seen a possible
490 exit, and 'error_mark_node' if not. This is currently used only to
491 suppress the warning about a function with no return statements, and
492 therefore we don't bother noting returns as possible exits. We also
493 don't bother with gotos. */
495 static void
496 begin_maybe_infinite_loop (tree cond)
498 /* Only track this while parsing a function, not during instantiation. */
499 if (!cfun || (DECL_TEMPLATE_INSTANTIATION (current_function_decl)
500 && !processing_template_decl))
501 return;
502 bool maybe_infinite = true;
503 if (cond)
505 cond = fold_non_dependent_expr (cond);
506 maybe_infinite = integer_nonzerop (cond);
508 vec_safe_push (cp_function_chain->infinite_loops,
509 maybe_infinite ? error_mark_node : NULL_TREE);
513 /* A break is a possible exit for the current loop. */
515 void
516 break_maybe_infinite_loop (void)
518 if (!cfun)
519 return;
520 cp_function_chain->infinite_loops->last() = NULL_TREE;
523 /* If we reach the end of the loop without seeing a possible exit, we have
524 an infinite loop. */
526 static void
527 end_maybe_infinite_loop (tree cond)
529 if (!cfun || (DECL_TEMPLATE_INSTANTIATION (current_function_decl)
530 && !processing_template_decl))
531 return;
532 tree current = cp_function_chain->infinite_loops->pop();
533 if (current != NULL_TREE)
535 cond = fold_non_dependent_expr (cond);
536 if (integer_nonzerop (cond))
537 current_function_infinite_loop = 1;
542 /* Begin a conditional that might contain a declaration. When generating
543 normal code, we want the declaration to appear before the statement
544 containing the conditional. When generating template code, we want the
545 conditional to be rendered as the raw DECL_EXPR. */
547 static void
548 begin_cond (tree *cond_p)
550 if (processing_template_decl)
551 *cond_p = push_stmt_list ();
554 /* Finish such a conditional. */
556 static void
557 finish_cond (tree *cond_p, tree expr)
559 if (processing_template_decl)
561 tree cond = pop_stmt_list (*cond_p);
563 if (expr == NULL_TREE)
564 /* Empty condition in 'for'. */
565 gcc_assert (empty_expr_stmt_p (cond));
566 else if (check_for_bare_parameter_packs (expr))
567 expr = error_mark_node;
568 else if (!empty_expr_stmt_p (cond))
569 expr = build2 (COMPOUND_EXPR, TREE_TYPE (expr), cond, expr);
571 *cond_p = expr;
574 /* If *COND_P specifies a conditional with a declaration, transform the
575 loop such that
576 while (A x = 42) { }
577 for (; A x = 42;) { }
578 becomes
579 while (true) { A x = 42; if (!x) break; }
580 for (;;) { A x = 42; if (!x) break; }
581 The statement list for BODY will be empty if the conditional did
582 not declare anything. */
584 static void
585 simplify_loop_decl_cond (tree *cond_p, tree body)
587 tree cond, if_stmt;
589 if (!TREE_SIDE_EFFECTS (body))
590 return;
592 cond = *cond_p;
593 *cond_p = boolean_true_node;
595 if_stmt = begin_if_stmt ();
596 cond = cp_build_unary_op (TRUTH_NOT_EXPR, cond, false, tf_warning_or_error);
597 finish_if_stmt_cond (cond, if_stmt);
598 finish_break_stmt ();
599 finish_then_clause (if_stmt);
600 finish_if_stmt (if_stmt);
603 /* Finish a goto-statement. */
605 tree
606 finish_goto_stmt (tree destination)
608 if (identifier_p (destination))
609 destination = lookup_label (destination);
611 /* We warn about unused labels with -Wunused. That means we have to
612 mark the used labels as used. */
613 if (TREE_CODE (destination) == LABEL_DECL)
614 TREE_USED (destination) = 1;
615 else
617 destination = mark_rvalue_use (destination);
618 if (!processing_template_decl)
620 destination = cp_convert (ptr_type_node, destination,
621 tf_warning_or_error);
622 if (error_operand_p (destination))
623 return NULL_TREE;
624 destination
625 = fold_build_cleanup_point_expr (TREE_TYPE (destination),
626 destination);
630 check_goto (destination);
632 add_stmt (build_predict_expr (PRED_GOTO, NOT_TAKEN));
633 return add_stmt (build_stmt (input_location, GOTO_EXPR, destination));
636 /* COND is the condition-expression for an if, while, etc.,
637 statement. Convert it to a boolean value, if appropriate.
638 In addition, verify sequence points if -Wsequence-point is enabled. */
640 static tree
641 maybe_convert_cond (tree cond)
643 /* Empty conditions remain empty. */
644 if (!cond)
645 return NULL_TREE;
647 /* Wait until we instantiate templates before doing conversion. */
648 if (processing_template_decl)
649 return cond;
651 if (warn_sequence_point)
652 verify_sequence_points (cond);
654 /* Do the conversion. */
655 cond = convert_from_reference (cond);
657 if (TREE_CODE (cond) == MODIFY_EXPR
658 && !TREE_NO_WARNING (cond)
659 && warn_parentheses)
661 warning_at (cp_expr_loc_or_loc (cond, input_location), OPT_Wparentheses,
662 "suggest parentheses around assignment used as truth value");
663 TREE_NO_WARNING (cond) = 1;
666 return condition_conversion (cond);
669 /* Finish an expression-statement, whose EXPRESSION is as indicated. */
671 tree
672 finish_expr_stmt (tree expr)
674 tree r = NULL_TREE;
675 location_t loc = EXPR_LOCATION (expr);
677 if (expr != NULL_TREE)
679 /* If we ran into a problem, make sure we complained. */
680 gcc_assert (expr != error_mark_node || seen_error ());
682 if (!processing_template_decl)
684 if (warn_sequence_point)
685 verify_sequence_points (expr);
686 expr = convert_to_void (expr, ICV_STATEMENT, tf_warning_or_error);
688 else if (!type_dependent_expression_p (expr))
689 convert_to_void (build_non_dependent_expr (expr), ICV_STATEMENT,
690 tf_warning_or_error);
692 if (check_for_bare_parameter_packs (expr))
693 expr = error_mark_node;
695 /* Simplification of inner statement expressions, compound exprs,
696 etc can result in us already having an EXPR_STMT. */
697 if (TREE_CODE (expr) != CLEANUP_POINT_EXPR)
699 if (TREE_CODE (expr) != EXPR_STMT)
700 expr = build_stmt (loc, EXPR_STMT, expr);
701 expr = maybe_cleanup_point_expr_void (expr);
704 r = add_stmt (expr);
707 return r;
711 /* Begin an if-statement. Returns a newly created IF_STMT if
712 appropriate. */
714 tree
715 begin_if_stmt (void)
717 tree r, scope;
718 scope = do_pushlevel (sk_cond);
719 r = build_stmt (input_location, IF_STMT, NULL_TREE,
720 NULL_TREE, NULL_TREE, scope);
721 current_binding_level->this_entity = r;
722 begin_cond (&IF_COND (r));
723 return r;
726 /* Process the COND of an if-statement, which may be given by
727 IF_STMT. */
729 tree
730 finish_if_stmt_cond (tree cond, tree if_stmt)
732 cond = maybe_convert_cond (cond);
733 if (IF_STMT_CONSTEXPR_P (if_stmt)
734 && !type_dependent_expression_p (cond)
735 && require_constant_expression (cond)
736 && !instantiation_dependent_expression_p (cond)
737 /* Wait until instantiation time, since only then COND has been
738 converted to bool. */
739 && TYPE_MAIN_VARIANT (TREE_TYPE (cond)) == boolean_type_node)
741 cond = instantiate_non_dependent_expr (cond);
742 cond = cxx_constant_value (cond, NULL_TREE);
744 finish_cond (&IF_COND (if_stmt), cond);
745 add_stmt (if_stmt);
746 THEN_CLAUSE (if_stmt) = push_stmt_list ();
747 return cond;
750 /* Finish the then-clause of an if-statement, which may be given by
751 IF_STMT. */
753 tree
754 finish_then_clause (tree if_stmt)
756 THEN_CLAUSE (if_stmt) = pop_stmt_list (THEN_CLAUSE (if_stmt));
757 return if_stmt;
760 /* Begin the else-clause of an if-statement. */
762 void
763 begin_else_clause (tree if_stmt)
765 ELSE_CLAUSE (if_stmt) = push_stmt_list ();
768 /* Finish the else-clause of an if-statement, which may be given by
769 IF_STMT. */
771 void
772 finish_else_clause (tree if_stmt)
774 ELSE_CLAUSE (if_stmt) = pop_stmt_list (ELSE_CLAUSE (if_stmt));
777 /* Finish an if-statement. */
779 void
780 finish_if_stmt (tree if_stmt)
782 tree scope = IF_SCOPE (if_stmt);
783 IF_SCOPE (if_stmt) = NULL;
784 add_stmt (do_poplevel (scope));
787 /* Begin a while-statement. Returns a newly created WHILE_STMT if
788 appropriate. */
790 tree
791 begin_while_stmt (void)
793 tree r;
794 r = build_stmt (input_location, WHILE_STMT, NULL_TREE, NULL_TREE);
795 add_stmt (r);
796 WHILE_BODY (r) = do_pushlevel (sk_block);
797 begin_cond (&WHILE_COND (r));
798 return r;
801 /* Process the COND of a while-statement, which may be given by
802 WHILE_STMT. */
804 void
805 finish_while_stmt_cond (tree cond, tree while_stmt, bool ivdep,
806 unsigned short unroll)
808 cond = maybe_convert_cond (cond);
809 finish_cond (&WHILE_COND (while_stmt), cond);
810 begin_maybe_infinite_loop (cond);
811 if (ivdep && cond != error_mark_node)
812 WHILE_COND (while_stmt) = build3 (ANNOTATE_EXPR,
813 TREE_TYPE (WHILE_COND (while_stmt)),
814 WHILE_COND (while_stmt),
815 build_int_cst (integer_type_node,
816 annot_expr_ivdep_kind),
817 integer_zero_node);
818 if (unroll && cond != error_mark_node)
819 WHILE_COND (while_stmt) = build3 (ANNOTATE_EXPR,
820 TREE_TYPE (WHILE_COND (while_stmt)),
821 WHILE_COND (while_stmt),
822 build_int_cst (integer_type_node,
823 annot_expr_unroll_kind),
824 build_int_cst (integer_type_node,
825 unroll));
826 simplify_loop_decl_cond (&WHILE_COND (while_stmt), WHILE_BODY (while_stmt));
829 /* Finish a while-statement, which may be given by WHILE_STMT. */
831 void
832 finish_while_stmt (tree while_stmt)
834 end_maybe_infinite_loop (boolean_true_node);
835 WHILE_BODY (while_stmt) = do_poplevel (WHILE_BODY (while_stmt));
838 /* Begin a do-statement. Returns a newly created DO_STMT if
839 appropriate. */
841 tree
842 begin_do_stmt (void)
844 tree r = build_stmt (input_location, DO_STMT, NULL_TREE, NULL_TREE);
845 begin_maybe_infinite_loop (boolean_true_node);
846 add_stmt (r);
847 DO_BODY (r) = push_stmt_list ();
848 return r;
851 /* Finish the body of a do-statement, which may be given by DO_STMT. */
853 void
854 finish_do_body (tree do_stmt)
856 tree body = DO_BODY (do_stmt) = pop_stmt_list (DO_BODY (do_stmt));
858 if (TREE_CODE (body) == STATEMENT_LIST && STATEMENT_LIST_TAIL (body))
859 body = STATEMENT_LIST_TAIL (body)->stmt;
861 if (IS_EMPTY_STMT (body))
862 warning (OPT_Wempty_body,
863 "suggest explicit braces around empty body in %<do%> statement");
866 /* Finish a do-statement, which may be given by DO_STMT, and whose
867 COND is as indicated. */
869 void
870 finish_do_stmt (tree cond, tree do_stmt, bool ivdep, unsigned short unroll)
872 cond = maybe_convert_cond (cond);
873 end_maybe_infinite_loop (cond);
874 if (ivdep && cond != error_mark_node)
875 cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
876 build_int_cst (integer_type_node, annot_expr_ivdep_kind),
877 integer_zero_node);
878 if (unroll && cond != error_mark_node)
879 cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
880 build_int_cst (integer_type_node, annot_expr_unroll_kind),
881 build_int_cst (integer_type_node, unroll));
882 DO_COND (do_stmt) = cond;
885 /* Finish a return-statement. The EXPRESSION returned, if any, is as
886 indicated. */
888 tree
889 finish_return_stmt (tree expr)
891 tree r;
892 bool no_warning;
894 expr = check_return_expr (expr, &no_warning);
896 if (error_operand_p (expr)
897 || (flag_openmp && !check_omp_return ()))
899 /* Suppress -Wreturn-type for this function. */
900 if (warn_return_type)
901 TREE_NO_WARNING (current_function_decl) = true;
902 return error_mark_node;
905 if (!processing_template_decl)
907 if (warn_sequence_point)
908 verify_sequence_points (expr);
910 if (DECL_DESTRUCTOR_P (current_function_decl)
911 || (DECL_CONSTRUCTOR_P (current_function_decl)
912 && targetm.cxx.cdtor_returns_this ()))
914 /* Similarly, all destructors must run destructors for
915 base-classes before returning. So, all returns in a
916 destructor get sent to the DTOR_LABEL; finish_function emits
917 code to return a value there. */
918 return finish_goto_stmt (cdtor_label);
922 r = build_stmt (input_location, RETURN_EXPR, expr);
923 TREE_NO_WARNING (r) |= no_warning;
924 r = maybe_cleanup_point_expr_void (r);
925 r = add_stmt (r);
927 return r;
930 /* Begin the scope of a for-statement or a range-for-statement.
931 Both the returned trees are to be used in a call to
932 begin_for_stmt or begin_range_for_stmt. */
934 tree
935 begin_for_scope (tree *init)
937 tree scope = do_pushlevel (sk_for);
939 if (processing_template_decl)
940 *init = push_stmt_list ();
941 else
942 *init = NULL_TREE;
944 return scope;
947 /* Begin a for-statement. Returns a new FOR_STMT.
948 SCOPE and INIT should be the return of begin_for_scope,
949 or both NULL_TREE */
951 tree
952 begin_for_stmt (tree scope, tree init)
954 tree r;
956 r = build_stmt (input_location, FOR_STMT, NULL_TREE, NULL_TREE,
957 NULL_TREE, NULL_TREE, NULL_TREE);
959 if (scope == NULL_TREE)
961 gcc_assert (!init);
962 scope = begin_for_scope (&init);
965 FOR_INIT_STMT (r) = init;
966 FOR_SCOPE (r) = scope;
968 return r;
971 /* Finish the init-statement of a for-statement, which may be
972 given by FOR_STMT. */
974 void
975 finish_init_stmt (tree for_stmt)
977 if (processing_template_decl)
978 FOR_INIT_STMT (for_stmt) = pop_stmt_list (FOR_INIT_STMT (for_stmt));
979 add_stmt (for_stmt);
980 FOR_BODY (for_stmt) = do_pushlevel (sk_block);
981 begin_cond (&FOR_COND (for_stmt));
984 /* Finish the COND of a for-statement, which may be given by
985 FOR_STMT. */
987 void
988 finish_for_cond (tree cond, tree for_stmt, bool ivdep, unsigned short unroll)
990 cond = maybe_convert_cond (cond);
991 finish_cond (&FOR_COND (for_stmt), cond);
992 begin_maybe_infinite_loop (cond);
993 if (ivdep && cond != error_mark_node)
994 FOR_COND (for_stmt) = build3 (ANNOTATE_EXPR,
995 TREE_TYPE (FOR_COND (for_stmt)),
996 FOR_COND (for_stmt),
997 build_int_cst (integer_type_node,
998 annot_expr_ivdep_kind),
999 integer_zero_node);
1000 if (unroll && cond != error_mark_node)
1001 FOR_COND (for_stmt) = build3 (ANNOTATE_EXPR,
1002 TREE_TYPE (FOR_COND (for_stmt)),
1003 FOR_COND (for_stmt),
1004 build_int_cst (integer_type_node,
1005 annot_expr_unroll_kind),
1006 build_int_cst (integer_type_node,
1007 unroll));
1008 simplify_loop_decl_cond (&FOR_COND (for_stmt), FOR_BODY (for_stmt));
1011 /* Finish the increment-EXPRESSION in a for-statement, which may be
1012 given by FOR_STMT. */
1014 void
1015 finish_for_expr (tree expr, tree for_stmt)
1017 if (!expr)
1018 return;
1019 /* If EXPR is an overloaded function, issue an error; there is no
1020 context available to use to perform overload resolution. */
1021 if (type_unknown_p (expr))
1023 cxx_incomplete_type_error (expr, TREE_TYPE (expr));
1024 expr = error_mark_node;
1026 if (!processing_template_decl)
1028 if (warn_sequence_point)
1029 verify_sequence_points (expr);
1030 expr = convert_to_void (expr, ICV_THIRD_IN_FOR,
1031 tf_warning_or_error);
1033 else if (!type_dependent_expression_p (expr))
1034 convert_to_void (build_non_dependent_expr (expr), ICV_THIRD_IN_FOR,
1035 tf_warning_or_error);
1036 expr = maybe_cleanup_point_expr_void (expr);
1037 if (check_for_bare_parameter_packs (expr))
1038 expr = error_mark_node;
1039 FOR_EXPR (for_stmt) = expr;
1042 /* Finish the body of a for-statement, which may be given by
1043 FOR_STMT. The increment-EXPR for the loop must be
1044 provided.
1045 It can also finish RANGE_FOR_STMT. */
1047 void
1048 finish_for_stmt (tree for_stmt)
1050 end_maybe_infinite_loop (boolean_true_node);
1052 if (TREE_CODE (for_stmt) == RANGE_FOR_STMT)
1053 RANGE_FOR_BODY (for_stmt) = do_poplevel (RANGE_FOR_BODY (for_stmt));
1054 else
1055 FOR_BODY (for_stmt) = do_poplevel (FOR_BODY (for_stmt));
1057 /* Pop the scope for the body of the loop. */
1058 tree *scope_ptr = (TREE_CODE (for_stmt) == RANGE_FOR_STMT
1059 ? &RANGE_FOR_SCOPE (for_stmt)
1060 : &FOR_SCOPE (for_stmt));
1061 tree scope = *scope_ptr;
1062 *scope_ptr = NULL;
1064 /* During parsing of the body, range for uses "__for_{range,begin,end} "
1065 decl names to make those unaccessible by code in the body.
1066 Change it to ones with underscore instead of space, so that it can
1067 be inspected in the debugger. */
1068 tree range_for_decl[3] = { NULL_TREE, NULL_TREE, NULL_TREE };
1069 gcc_assert (CPTI_FOR_BEGIN__IDENTIFIER == CPTI_FOR_RANGE__IDENTIFIER + 1
1070 && CPTI_FOR_END__IDENTIFIER == CPTI_FOR_RANGE__IDENTIFIER + 2
1071 && CPTI_FOR_RANGE_IDENTIFIER == CPTI_FOR_RANGE__IDENTIFIER + 3
1072 && CPTI_FOR_BEGIN_IDENTIFIER == CPTI_FOR_BEGIN__IDENTIFIER + 3
1073 && CPTI_FOR_END_IDENTIFIER == CPTI_FOR_END__IDENTIFIER + 3);
1074 for (int i = 0; i < 3; i++)
1076 tree id = cp_global_trees[CPTI_FOR_RANGE__IDENTIFIER + i];
1077 if (IDENTIFIER_BINDING (id)
1078 && IDENTIFIER_BINDING (id)->scope == current_binding_level)
1080 range_for_decl[i] = IDENTIFIER_BINDING (id)->value;
1081 gcc_assert (VAR_P (range_for_decl[i])
1082 && DECL_ARTIFICIAL (range_for_decl[i]));
1086 add_stmt (do_poplevel (scope));
1088 for (int i = 0; i < 3; i++)
1089 if (range_for_decl[i])
1090 DECL_NAME (range_for_decl[i])
1091 = cp_global_trees[CPTI_FOR_RANGE_IDENTIFIER + i];
1094 /* Begin a range-for-statement. Returns a new RANGE_FOR_STMT.
1095 SCOPE and INIT should be the return of begin_for_scope,
1096 or both NULL_TREE .
1097 To finish it call finish_for_stmt(). */
1099 tree
1100 begin_range_for_stmt (tree scope, tree init)
1102 begin_maybe_infinite_loop (boolean_false_node);
1104 tree r = build_stmt (input_location, RANGE_FOR_STMT,
1105 NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE);
1107 if (scope == NULL_TREE)
1109 gcc_assert (!init);
1110 scope = begin_for_scope (&init);
1113 /* RANGE_FOR_STMTs do not use nor save the init tree, so we
1114 pop it now. */
1115 if (init)
1116 pop_stmt_list (init);
1117 RANGE_FOR_SCOPE (r) = scope;
1119 return r;
1122 /* Finish the head of a range-based for statement, which may
1123 be given by RANGE_FOR_STMT. DECL must be the declaration
1124 and EXPR must be the loop expression. */
1126 void
1127 finish_range_for_decl (tree range_for_stmt, tree decl, tree expr)
1129 RANGE_FOR_DECL (range_for_stmt) = decl;
1130 RANGE_FOR_EXPR (range_for_stmt) = expr;
1131 add_stmt (range_for_stmt);
1132 RANGE_FOR_BODY (range_for_stmt) = do_pushlevel (sk_block);
1135 /* Finish a break-statement. */
1137 tree
1138 finish_break_stmt (void)
1140 /* In switch statements break is sometimes stylistically used after
1141 a return statement. This can lead to spurious warnings about
1142 control reaching the end of a non-void function when it is
1143 inlined. Note that we are calling block_may_fallthru with
1144 language specific tree nodes; this works because
1145 block_may_fallthru returns true when given something it does not
1146 understand. */
1147 if (!block_may_fallthru (cur_stmt_list))
1148 return void_node;
1149 note_break_stmt ();
1150 return add_stmt (build_stmt (input_location, BREAK_STMT));
1153 /* Finish a continue-statement. */
1155 tree
1156 finish_continue_stmt (void)
1158 return add_stmt (build_stmt (input_location, CONTINUE_STMT));
1161 /* Begin a switch-statement. Returns a new SWITCH_STMT if
1162 appropriate. */
1164 tree
1165 begin_switch_stmt (void)
1167 tree r, scope;
1169 scope = do_pushlevel (sk_cond);
1170 r = build_stmt (input_location, SWITCH_STMT, NULL_TREE, NULL_TREE, NULL_TREE, scope);
1172 begin_cond (&SWITCH_STMT_COND (r));
1174 return r;
1177 /* Finish the cond of a switch-statement. */
1179 void
1180 finish_switch_cond (tree cond, tree switch_stmt)
1182 tree orig_type = NULL;
1184 if (!processing_template_decl)
1186 /* Convert the condition to an integer or enumeration type. */
1187 cond = build_expr_type_conversion (WANT_INT | WANT_ENUM, cond, true);
1188 if (cond == NULL_TREE)
1190 error ("switch quantity not an integer");
1191 cond = error_mark_node;
1193 /* We want unlowered type here to handle enum bit-fields. */
1194 orig_type = unlowered_expr_type (cond);
1195 if (TREE_CODE (orig_type) != ENUMERAL_TYPE)
1196 orig_type = TREE_TYPE (cond);
1197 if (cond != error_mark_node)
1199 /* [stmt.switch]
1201 Integral promotions are performed. */
1202 cond = perform_integral_promotions (cond);
1203 cond = maybe_cleanup_point_expr (cond);
1206 if (check_for_bare_parameter_packs (cond))
1207 cond = error_mark_node;
1208 else if (!processing_template_decl && warn_sequence_point)
1209 verify_sequence_points (cond);
1211 finish_cond (&SWITCH_STMT_COND (switch_stmt), cond);
1212 SWITCH_STMT_TYPE (switch_stmt) = orig_type;
1213 add_stmt (switch_stmt);
1214 push_switch (switch_stmt);
1215 SWITCH_STMT_BODY (switch_stmt) = push_stmt_list ();
1218 /* Finish the body of a switch-statement, which may be given by
1219 SWITCH_STMT. The COND to switch on is indicated. */
1221 void
1222 finish_switch_stmt (tree switch_stmt)
1224 tree scope;
1226 SWITCH_STMT_BODY (switch_stmt) =
1227 pop_stmt_list (SWITCH_STMT_BODY (switch_stmt));
1228 pop_switch ();
1230 scope = SWITCH_STMT_SCOPE (switch_stmt);
1231 SWITCH_STMT_SCOPE (switch_stmt) = NULL;
1232 add_stmt (do_poplevel (scope));
1235 /* Begin a try-block. Returns a newly-created TRY_BLOCK if
1236 appropriate. */
1238 tree
1239 begin_try_block (void)
1241 tree r = build_stmt (input_location, TRY_BLOCK, NULL_TREE, NULL_TREE);
1242 add_stmt (r);
1243 TRY_STMTS (r) = push_stmt_list ();
1244 return r;
1247 /* Likewise, for a function-try-block. The block returned in
1248 *COMPOUND_STMT is an artificial outer scope, containing the
1249 function-try-block. */
1251 tree
1252 begin_function_try_block (tree *compound_stmt)
1254 tree r;
1255 /* This outer scope does not exist in the C++ standard, but we need
1256 a place to put __FUNCTION__ and similar variables. */
1257 *compound_stmt = begin_compound_stmt (0);
1258 r = begin_try_block ();
1259 FN_TRY_BLOCK_P (r) = 1;
1260 return r;
1263 /* Finish a try-block, which may be given by TRY_BLOCK. */
1265 void
1266 finish_try_block (tree try_block)
1268 TRY_STMTS (try_block) = pop_stmt_list (TRY_STMTS (try_block));
1269 TRY_HANDLERS (try_block) = push_stmt_list ();
1272 /* Finish the body of a cleanup try-block, which may be given by
1273 TRY_BLOCK. */
1275 void
1276 finish_cleanup_try_block (tree try_block)
1278 TRY_STMTS (try_block) = pop_stmt_list (TRY_STMTS (try_block));
1281 /* Finish an implicitly generated try-block, with a cleanup is given
1282 by CLEANUP. */
1284 void
1285 finish_cleanup (tree cleanup, tree try_block)
1287 TRY_HANDLERS (try_block) = cleanup;
1288 CLEANUP_P (try_block) = 1;
1291 /* Likewise, for a function-try-block. */
1293 void
1294 finish_function_try_block (tree try_block)
1296 finish_try_block (try_block);
1297 /* FIXME : something queer about CTOR_INITIALIZER somehow following
1298 the try block, but moving it inside. */
1299 in_function_try_handler = 1;
1302 /* Finish a handler-sequence for a try-block, which may be given by
1303 TRY_BLOCK. */
1305 void
1306 finish_handler_sequence (tree try_block)
1308 TRY_HANDLERS (try_block) = pop_stmt_list (TRY_HANDLERS (try_block));
1309 check_handlers (TRY_HANDLERS (try_block));
1312 /* Finish the handler-seq for a function-try-block, given by
1313 TRY_BLOCK. COMPOUND_STMT is the outer block created by
1314 begin_function_try_block. */
1316 void
1317 finish_function_handler_sequence (tree try_block, tree compound_stmt)
1319 in_function_try_handler = 0;
1320 finish_handler_sequence (try_block);
1321 finish_compound_stmt (compound_stmt);
1324 /* Begin a handler. Returns a HANDLER if appropriate. */
1326 tree
1327 begin_handler (void)
1329 tree r;
1331 r = build_stmt (input_location, HANDLER, NULL_TREE, NULL_TREE);
1332 add_stmt (r);
1334 /* Create a binding level for the eh_info and the exception object
1335 cleanup. */
1336 HANDLER_BODY (r) = do_pushlevel (sk_catch);
1338 return r;
1341 /* Finish the handler-parameters for a handler, which may be given by
1342 HANDLER. DECL is the declaration for the catch parameter, or NULL
1343 if this is a `catch (...)' clause. */
1345 void
1346 finish_handler_parms (tree decl, tree handler)
1348 tree type = NULL_TREE;
1349 if (processing_template_decl)
1351 if (decl)
1353 decl = pushdecl (decl);
1354 decl = push_template_decl (decl);
1355 HANDLER_PARMS (handler) = decl;
1356 type = TREE_TYPE (decl);
1359 else
1361 type = expand_start_catch_block (decl);
1362 if (warn_catch_value
1363 && type != NULL_TREE
1364 && type != error_mark_node
1365 && !TYPE_REF_P (TREE_TYPE (decl)))
1367 tree orig_type = TREE_TYPE (decl);
1368 if (CLASS_TYPE_P (orig_type))
1370 if (TYPE_POLYMORPHIC_P (orig_type))
1371 warning (OPT_Wcatch_value_,
1372 "catching polymorphic type %q#T by value", orig_type);
1373 else if (warn_catch_value > 1)
1374 warning (OPT_Wcatch_value_,
1375 "catching type %q#T by value", orig_type);
1377 else if (warn_catch_value > 2)
1378 warning (OPT_Wcatch_value_,
1379 "catching non-reference type %q#T", orig_type);
1382 HANDLER_TYPE (handler) = type;
1385 /* Finish a handler, which may be given by HANDLER. The BLOCKs are
1386 the return value from the matching call to finish_handler_parms. */
1388 void
1389 finish_handler (tree handler)
1391 if (!processing_template_decl)
1392 expand_end_catch_block ();
1393 HANDLER_BODY (handler) = do_poplevel (HANDLER_BODY (handler));
1396 /* Begin a compound statement. FLAGS contains some bits that control the
1397 behavior and context. If BCS_NO_SCOPE is set, the compound statement
1398 does not define a scope. If BCS_FN_BODY is set, this is the outermost
1399 block of a function. If BCS_TRY_BLOCK is set, this is the block
1400 created on behalf of a TRY statement. Returns a token to be passed to
1401 finish_compound_stmt. */
1403 tree
1404 begin_compound_stmt (unsigned int flags)
1406 tree r;
1408 if (flags & BCS_NO_SCOPE)
1410 r = push_stmt_list ();
1411 STATEMENT_LIST_NO_SCOPE (r) = 1;
1413 /* Normally, we try hard to keep the BLOCK for a statement-expression.
1414 But, if it's a statement-expression with a scopeless block, there's
1415 nothing to keep, and we don't want to accidentally keep a block
1416 *inside* the scopeless block. */
1417 keep_next_level (false);
1419 else
1421 scope_kind sk = sk_block;
1422 if (flags & BCS_TRY_BLOCK)
1423 sk = sk_try;
1424 else if (flags & BCS_TRANSACTION)
1425 sk = sk_transaction;
1426 r = do_pushlevel (sk);
1429 /* When processing a template, we need to remember where the braces were,
1430 so that we can set up identical scopes when instantiating the template
1431 later. BIND_EXPR is a handy candidate for this.
1432 Note that do_poplevel won't create a BIND_EXPR itself here (and thus
1433 result in nested BIND_EXPRs), since we don't build BLOCK nodes when
1434 processing templates. */
1435 if (processing_template_decl)
1437 r = build3 (BIND_EXPR, NULL, NULL, r, NULL);
1438 BIND_EXPR_TRY_BLOCK (r) = (flags & BCS_TRY_BLOCK) != 0;
1439 BIND_EXPR_BODY_BLOCK (r) = (flags & BCS_FN_BODY) != 0;
1440 TREE_SIDE_EFFECTS (r) = 1;
1443 return r;
1446 /* Finish a compound-statement, which is given by STMT. */
1448 void
1449 finish_compound_stmt (tree stmt)
1451 if (TREE_CODE (stmt) == BIND_EXPR)
1453 tree body = do_poplevel (BIND_EXPR_BODY (stmt));
1454 /* If the STATEMENT_LIST is empty and this BIND_EXPR isn't special,
1455 discard the BIND_EXPR so it can be merged with the containing
1456 STATEMENT_LIST. */
1457 if (TREE_CODE (body) == STATEMENT_LIST
1458 && STATEMENT_LIST_HEAD (body) == NULL
1459 && !BIND_EXPR_BODY_BLOCK (stmt)
1460 && !BIND_EXPR_TRY_BLOCK (stmt))
1461 stmt = body;
1462 else
1463 BIND_EXPR_BODY (stmt) = body;
1465 else if (STATEMENT_LIST_NO_SCOPE (stmt))
1466 stmt = pop_stmt_list (stmt);
1467 else
1469 /* Destroy any ObjC "super" receivers that may have been
1470 created. */
1471 objc_clear_super_receiver ();
1473 stmt = do_poplevel (stmt);
1476 /* ??? See c_end_compound_stmt wrt statement expressions. */
1477 add_stmt (stmt);
1480 /* Finish an asm-statement, whose components are a STRING, some
1481 OUTPUT_OPERANDS, some INPUT_OPERANDS, some CLOBBERS and some
1482 LABELS. Also note whether the asm-statement should be
1483 considered volatile. */
1485 tree
1486 finish_asm_stmt (int volatile_p, tree string, tree output_operands,
1487 tree input_operands, tree clobbers, tree labels)
1489 tree r;
1490 tree t;
1491 int ninputs = list_length (input_operands);
1492 int noutputs = list_length (output_operands);
1494 if (!processing_template_decl)
1496 const char *constraint;
1497 const char **oconstraints;
1498 bool allows_mem, allows_reg, is_inout;
1499 tree operand;
1500 int i;
1502 oconstraints = XALLOCAVEC (const char *, noutputs);
1504 string = resolve_asm_operand_names (string, output_operands,
1505 input_operands, labels);
1507 for (i = 0, t = output_operands; t; t = TREE_CHAIN (t), ++i)
1509 operand = TREE_VALUE (t);
1511 /* ??? Really, this should not be here. Users should be using a
1512 proper lvalue, dammit. But there's a long history of using
1513 casts in the output operands. In cases like longlong.h, this
1514 becomes a primitive form of typechecking -- if the cast can be
1515 removed, then the output operand had a type of the proper width;
1516 otherwise we'll get an error. Gross, but ... */
1517 STRIP_NOPS (operand);
1519 operand = mark_lvalue_use (operand);
1521 if (!lvalue_or_else (operand, lv_asm, tf_warning_or_error))
1522 operand = error_mark_node;
1524 if (operand != error_mark_node
1525 && (TREE_READONLY (operand)
1526 || CP_TYPE_CONST_P (TREE_TYPE (operand))
1527 /* Functions are not modifiable, even though they are
1528 lvalues. */
1529 || TREE_CODE (TREE_TYPE (operand)) == FUNCTION_TYPE
1530 || TREE_CODE (TREE_TYPE (operand)) == METHOD_TYPE
1531 /* If it's an aggregate and any field is const, then it is
1532 effectively const. */
1533 || (CLASS_TYPE_P (TREE_TYPE (operand))
1534 && C_TYPE_FIELDS_READONLY (TREE_TYPE (operand)))))
1535 cxx_readonly_error (operand, lv_asm);
1537 tree *op = &operand;
1538 while (TREE_CODE (*op) == COMPOUND_EXPR)
1539 op = &TREE_OPERAND (*op, 1);
1540 switch (TREE_CODE (*op))
1542 case PREINCREMENT_EXPR:
1543 case PREDECREMENT_EXPR:
1544 case MODIFY_EXPR:
1545 *op = genericize_compound_lvalue (*op);
1546 op = &TREE_OPERAND (*op, 1);
1547 break;
1548 default:
1549 break;
1552 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
1553 oconstraints[i] = constraint;
1555 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
1556 &allows_mem, &allows_reg, &is_inout))
1558 /* If the operand is going to end up in memory,
1559 mark it addressable. */
1560 if (!allows_reg && !cxx_mark_addressable (*op))
1561 operand = error_mark_node;
1563 else
1564 operand = error_mark_node;
1566 TREE_VALUE (t) = operand;
1569 for (i = 0, t = input_operands; t; ++i, t = TREE_CHAIN (t))
1571 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
1572 bool constraint_parsed
1573 = parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
1574 oconstraints, &allows_mem, &allows_reg);
1575 /* If the operand is going to end up in memory, don't call
1576 decay_conversion. */
1577 if (constraint_parsed && !allows_reg && allows_mem)
1578 operand = mark_lvalue_use (TREE_VALUE (t));
1579 else
1580 operand = decay_conversion (TREE_VALUE (t), tf_warning_or_error);
1582 /* If the type of the operand hasn't been determined (e.g.,
1583 because it involves an overloaded function), then issue
1584 an error message. There's no context available to
1585 resolve the overloading. */
1586 if (TREE_TYPE (operand) == unknown_type_node)
1588 error ("type of asm operand %qE could not be determined",
1589 TREE_VALUE (t));
1590 operand = error_mark_node;
1593 if (constraint_parsed)
1595 /* If the operand is going to end up in memory,
1596 mark it addressable. */
1597 if (!allows_reg && allows_mem)
1599 /* Strip the nops as we allow this case. FIXME, this really
1600 should be rejected or made deprecated. */
1601 STRIP_NOPS (operand);
1603 tree *op = &operand;
1604 while (TREE_CODE (*op) == COMPOUND_EXPR)
1605 op = &TREE_OPERAND (*op, 1);
1606 switch (TREE_CODE (*op))
1608 case PREINCREMENT_EXPR:
1609 case PREDECREMENT_EXPR:
1610 case MODIFY_EXPR:
1611 *op = genericize_compound_lvalue (*op);
1612 op = &TREE_OPERAND (*op, 1);
1613 break;
1614 default:
1615 break;
1618 if (!cxx_mark_addressable (*op))
1619 operand = error_mark_node;
1621 else if (!allows_reg && !allows_mem)
1623 /* If constraint allows neither register nor memory,
1624 try harder to get a constant. */
1625 tree constop = maybe_constant_value (operand);
1626 if (TREE_CONSTANT (constop))
1627 operand = constop;
1630 else
1631 operand = error_mark_node;
1633 TREE_VALUE (t) = operand;
1637 r = build_stmt (input_location, ASM_EXPR, string,
1638 output_operands, input_operands,
1639 clobbers, labels);
1640 ASM_VOLATILE_P (r) = volatile_p || noutputs == 0;
1641 r = maybe_cleanup_point_expr_void (r);
1642 return add_stmt (r);
1645 /* Finish a label with the indicated NAME. Returns the new label. */
1647 tree
1648 finish_label_stmt (tree name)
1650 tree decl = define_label (input_location, name);
1652 if (decl == error_mark_node)
1653 return error_mark_node;
1655 add_stmt (build_stmt (input_location, LABEL_EXPR, decl));
1657 return decl;
1660 /* Finish a series of declarations for local labels. G++ allows users
1661 to declare "local" labels, i.e., labels with scope. This extension
1662 is useful when writing code involving statement-expressions. */
1664 void
1665 finish_label_decl (tree name)
1667 if (!at_function_scope_p ())
1669 error ("__label__ declarations are only allowed in function scopes");
1670 return;
1673 add_decl_expr (declare_local_label (name));
1676 /* When DECL goes out of scope, make sure that CLEANUP is executed. */
1678 void
1679 finish_decl_cleanup (tree decl, tree cleanup)
1681 push_cleanup (decl, cleanup, false);
1684 /* If the current scope exits with an exception, run CLEANUP. */
1686 void
1687 finish_eh_cleanup (tree cleanup)
1689 push_cleanup (NULL, cleanup, true);
1692 /* The MEM_INITS is a list of mem-initializers, in reverse of the
1693 order they were written by the user. Each node is as for
1694 emit_mem_initializers. */
1696 void
1697 finish_mem_initializers (tree mem_inits)
1699 /* Reorder the MEM_INITS so that they are in the order they appeared
1700 in the source program. */
1701 mem_inits = nreverse (mem_inits);
1703 if (processing_template_decl)
1705 tree mem;
1707 for (mem = mem_inits; mem; mem = TREE_CHAIN (mem))
1709 /* If the TREE_PURPOSE is a TYPE_PACK_EXPANSION, skip the
1710 check for bare parameter packs in the TREE_VALUE, because
1711 any parameter packs in the TREE_VALUE have already been
1712 bound as part of the TREE_PURPOSE. See
1713 make_pack_expansion for more information. */
1714 if (TREE_CODE (TREE_PURPOSE (mem)) != TYPE_PACK_EXPANSION
1715 && check_for_bare_parameter_packs (TREE_VALUE (mem)))
1716 TREE_VALUE (mem) = error_mark_node;
1719 add_stmt (build_min_nt_loc (UNKNOWN_LOCATION,
1720 CTOR_INITIALIZER, mem_inits));
1722 else
1723 emit_mem_initializers (mem_inits);
1726 /* Obfuscate EXPR if it looks like an id-expression or member access so
1727 that the call to finish_decltype in do_auto_deduction will give the
1728 right result. */
1730 tree
1731 force_paren_expr (tree expr)
1733 /* This is only needed for decltype(auto) in C++14. */
1734 if (cxx_dialect < cxx14)
1735 return expr;
1737 /* If we're in unevaluated context, we can't be deducing a
1738 return/initializer type, so we don't need to mess with this. */
1739 if (cp_unevaluated_operand)
1740 return expr;
1742 if (!DECL_P (expr) && TREE_CODE (expr) != COMPONENT_REF
1743 && TREE_CODE (expr) != SCOPE_REF)
1744 return expr;
1746 if (TREE_CODE (expr) == COMPONENT_REF
1747 || TREE_CODE (expr) == SCOPE_REF)
1748 REF_PARENTHESIZED_P (expr) = true;
1749 else if (processing_template_decl)
1750 expr = build1 (PAREN_EXPR, TREE_TYPE (expr), expr);
1751 else
1753 expr = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (expr), expr);
1754 REF_PARENTHESIZED_P (expr) = true;
1757 return expr;
1760 /* If T is an id-expression obfuscated by force_paren_expr, undo the
1761 obfuscation and return the underlying id-expression. Otherwise
1762 return T. */
1764 tree
1765 maybe_undo_parenthesized_ref (tree t)
1767 if (cxx_dialect < cxx14)
1768 return t;
1770 if (INDIRECT_REF_P (t) && REF_PARENTHESIZED_P (t))
1772 t = TREE_OPERAND (t, 0);
1773 while (TREE_CODE (t) == NON_LVALUE_EXPR
1774 || TREE_CODE (t) == NOP_EXPR)
1775 t = TREE_OPERAND (t, 0);
1777 gcc_assert (TREE_CODE (t) == ADDR_EXPR
1778 || TREE_CODE (t) == STATIC_CAST_EXPR);
1779 t = TREE_OPERAND (t, 0);
1781 else if (TREE_CODE (t) == PAREN_EXPR)
1782 t = TREE_OPERAND (t, 0);
1783 else if (TREE_CODE (t) == VIEW_CONVERT_EXPR
1784 && REF_PARENTHESIZED_P (t))
1785 t = TREE_OPERAND (t, 0);
1787 return t;
1790 /* Finish a parenthesized expression EXPR. */
1792 cp_expr
1793 finish_parenthesized_expr (cp_expr expr)
1795 if (EXPR_P (expr))
1796 /* This inhibits warnings in c_common_truthvalue_conversion. */
1797 TREE_NO_WARNING (expr) = 1;
1799 if (TREE_CODE (expr) == OFFSET_REF
1800 || TREE_CODE (expr) == SCOPE_REF)
1801 /* [expr.unary.op]/3 The qualified id of a pointer-to-member must not be
1802 enclosed in parentheses. */
1803 PTRMEM_OK_P (expr) = 0;
1805 if (TREE_CODE (expr) == STRING_CST)
1806 PAREN_STRING_LITERAL_P (expr) = 1;
1808 expr = cp_expr (force_paren_expr (expr), expr.get_location ());
1810 return expr;
1813 /* Finish a reference to a non-static data member (DECL) that is not
1814 preceded by `.' or `->'. */
1816 tree
1817 finish_non_static_data_member (tree decl, tree object, tree qualifying_scope)
1819 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
1820 bool try_omp_private = !object && omp_private_member_map;
1821 tree ret;
1823 if (!object)
1825 tree scope = qualifying_scope;
1826 if (scope == NULL_TREE)
1827 scope = context_for_name_lookup (decl);
1828 object = maybe_dummy_object (scope, NULL);
1831 object = maybe_resolve_dummy (object, true);
1832 if (object == error_mark_node)
1833 return error_mark_node;
1835 /* DR 613/850: Can use non-static data members without an associated
1836 object in sizeof/decltype/alignof. */
1837 if (is_dummy_object (object) && cp_unevaluated_operand == 0
1838 && (!processing_template_decl || !current_class_ref))
1840 if (current_function_decl
1841 && DECL_STATIC_FUNCTION_P (current_function_decl))
1842 error ("invalid use of member %qD in static member function", decl);
1843 else
1844 error ("invalid use of non-static data member %qD", decl);
1845 inform (DECL_SOURCE_LOCATION (decl), "declared here");
1847 return error_mark_node;
1850 if (current_class_ptr)
1851 TREE_USED (current_class_ptr) = 1;
1852 if (processing_template_decl && !qualifying_scope)
1854 tree type = TREE_TYPE (decl);
1856 if (TYPE_REF_P (type))
1857 /* Quals on the object don't matter. */;
1858 else if (PACK_EXPANSION_P (type))
1859 /* Don't bother trying to represent this. */
1860 type = NULL_TREE;
1861 else
1863 /* Set the cv qualifiers. */
1864 int quals = cp_type_quals (TREE_TYPE (object));
1866 if (DECL_MUTABLE_P (decl))
1867 quals &= ~TYPE_QUAL_CONST;
1869 quals |= cp_type_quals (TREE_TYPE (decl));
1870 type = cp_build_qualified_type (type, quals);
1873 ret = (convert_from_reference
1874 (build_min (COMPONENT_REF, type, object, decl, NULL_TREE)));
1876 /* If PROCESSING_TEMPLATE_DECL is nonzero here, then
1877 QUALIFYING_SCOPE is also non-null. Wrap this in a SCOPE_REF
1878 for now. */
1879 else if (processing_template_decl)
1880 ret = build_qualified_name (TREE_TYPE (decl),
1881 qualifying_scope,
1882 decl,
1883 /*template_p=*/false);
1884 else
1886 tree access_type = TREE_TYPE (object);
1888 perform_or_defer_access_check (TYPE_BINFO (access_type), decl,
1889 decl, tf_warning_or_error);
1891 /* If the data member was named `C::M', convert `*this' to `C'
1892 first. */
1893 if (qualifying_scope)
1895 tree binfo = NULL_TREE;
1896 object = build_scoped_ref (object, qualifying_scope,
1897 &binfo);
1900 ret = build_class_member_access_expr (object, decl,
1901 /*access_path=*/NULL_TREE,
1902 /*preserve_reference=*/false,
1903 tf_warning_or_error);
1905 if (try_omp_private)
1907 tree *v = omp_private_member_map->get (decl);
1908 if (v)
1909 ret = convert_from_reference (*v);
1911 return ret;
1914 /* If we are currently parsing a template and we encountered a typedef
1915 TYPEDEF_DECL that is being accessed though CONTEXT, this function
1916 adds the typedef to a list tied to the current template.
1917 At template instantiation time, that list is walked and access check
1918 performed for each typedef.
1919 LOCATION is the location of the usage point of TYPEDEF_DECL. */
1921 void
1922 add_typedef_to_current_template_for_access_check (tree typedef_decl,
1923 tree context,
1924 location_t location)
1926 tree template_info = NULL;
1927 tree cs = current_scope ();
1929 if (!is_typedef_decl (typedef_decl)
1930 || !context
1931 || !CLASS_TYPE_P (context)
1932 || !cs)
1933 return;
1935 if (CLASS_TYPE_P (cs) || TREE_CODE (cs) == FUNCTION_DECL)
1936 template_info = get_template_info (cs);
1938 if (template_info
1939 && TI_TEMPLATE (template_info)
1940 && !currently_open_class (context))
1941 append_type_to_template_for_access_check (cs, typedef_decl,
1942 context, location);
1945 /* DECL was the declaration to which a qualified-id resolved. Issue
1946 an error message if it is not accessible. If OBJECT_TYPE is
1947 non-NULL, we have just seen `x->' or `x.' and OBJECT_TYPE is the
1948 type of `*x', or `x', respectively. If the DECL was named as
1949 `A::B' then NESTED_NAME_SPECIFIER is `A'. */
1951 void
1952 check_accessibility_of_qualified_id (tree decl,
1953 tree object_type,
1954 tree nested_name_specifier)
1956 tree scope;
1957 tree qualifying_type = NULL_TREE;
1959 /* If we are parsing a template declaration and if decl is a typedef,
1960 add it to a list tied to the template.
1961 At template instantiation time, that list will be walked and
1962 access check performed. */
1963 add_typedef_to_current_template_for_access_check (decl,
1964 nested_name_specifier
1965 ? nested_name_specifier
1966 : DECL_CONTEXT (decl),
1967 input_location);
1969 /* If we're not checking, return immediately. */
1970 if (deferred_access_no_check)
1971 return;
1973 /* Determine the SCOPE of DECL. */
1974 scope = context_for_name_lookup (decl);
1975 /* If the SCOPE is not a type, then DECL is not a member. */
1976 if (!TYPE_P (scope))
1977 return;
1978 /* Compute the scope through which DECL is being accessed. */
1979 if (object_type
1980 /* OBJECT_TYPE might not be a class type; consider:
1982 class A { typedef int I; };
1983 I *p;
1984 p->A::I::~I();
1986 In this case, we will have "A::I" as the DECL, but "I" as the
1987 OBJECT_TYPE. */
1988 && CLASS_TYPE_P (object_type)
1989 && DERIVED_FROM_P (scope, object_type))
1990 /* If we are processing a `->' or `.' expression, use the type of the
1991 left-hand side. */
1992 qualifying_type = object_type;
1993 else if (nested_name_specifier)
1995 /* If the reference is to a non-static member of the
1996 current class, treat it as if it were referenced through
1997 `this'. */
1998 tree ct;
1999 if (DECL_NONSTATIC_MEMBER_P (decl)
2000 && current_class_ptr
2001 && DERIVED_FROM_P (scope, ct = current_nonlambda_class_type ()))
2002 qualifying_type = ct;
2003 /* Otherwise, use the type indicated by the
2004 nested-name-specifier. */
2005 else
2006 qualifying_type = nested_name_specifier;
2008 else
2009 /* Otherwise, the name must be from the current class or one of
2010 its bases. */
2011 qualifying_type = currently_open_derived_class (scope);
2013 if (qualifying_type
2014 /* It is possible for qualifying type to be a TEMPLATE_TYPE_PARM
2015 or similar in a default argument value. */
2016 && CLASS_TYPE_P (qualifying_type)
2017 && !dependent_type_p (qualifying_type))
2018 perform_or_defer_access_check (TYPE_BINFO (qualifying_type), decl,
2019 decl, tf_warning_or_error);
2022 /* EXPR is the result of a qualified-id. The QUALIFYING_CLASS was the
2023 class named to the left of the "::" operator. DONE is true if this
2024 expression is a complete postfix-expression; it is false if this
2025 expression is followed by '->', '[', '(', etc. ADDRESS_P is true
2026 iff this expression is the operand of '&'. TEMPLATE_P is true iff
2027 the qualified-id was of the form "A::template B". TEMPLATE_ARG_P
2028 is true iff this qualified name appears as a template argument. */
2030 tree
2031 finish_qualified_id_expr (tree qualifying_class,
2032 tree expr,
2033 bool done,
2034 bool address_p,
2035 bool template_p,
2036 bool template_arg_p,
2037 tsubst_flags_t complain)
2039 gcc_assert (TYPE_P (qualifying_class));
2041 if (error_operand_p (expr))
2042 return error_mark_node;
2044 if ((DECL_P (expr) || BASELINK_P (expr))
2045 && !mark_used (expr, complain))
2046 return error_mark_node;
2048 if (template_p)
2050 if (TREE_CODE (expr) == UNBOUND_CLASS_TEMPLATE)
2052 /* cp_parser_lookup_name thought we were looking for a type,
2053 but we're actually looking for a declaration. */
2054 qualifying_class = TYPE_CONTEXT (expr);
2055 expr = TYPE_IDENTIFIER (expr);
2057 else
2058 check_template_keyword (expr);
2061 /* If EXPR occurs as the operand of '&', use special handling that
2062 permits a pointer-to-member. */
2063 if (address_p && done)
2065 if (TREE_CODE (expr) == SCOPE_REF)
2066 expr = TREE_OPERAND (expr, 1);
2067 expr = build_offset_ref (qualifying_class, expr,
2068 /*address_p=*/true, complain);
2069 return expr;
2072 /* No need to check access within an enum. */
2073 if (TREE_CODE (qualifying_class) == ENUMERAL_TYPE
2074 && TREE_CODE (expr) != IDENTIFIER_NODE)
2075 return expr;
2077 /* Within the scope of a class, turn references to non-static
2078 members into expression of the form "this->...". */
2079 if (template_arg_p)
2080 /* But, within a template argument, we do not want make the
2081 transformation, as there is no "this" pointer. */
2083 else if (TREE_CODE (expr) == FIELD_DECL)
2085 push_deferring_access_checks (dk_no_check);
2086 expr = finish_non_static_data_member (expr, NULL_TREE,
2087 qualifying_class);
2088 pop_deferring_access_checks ();
2090 else if (BASELINK_P (expr))
2092 /* See if any of the functions are non-static members. */
2093 /* If so, the expression may be relative to 'this'. */
2094 if (!shared_member_p (expr)
2095 && current_class_ptr
2096 && DERIVED_FROM_P (qualifying_class,
2097 current_nonlambda_class_type ()))
2098 expr = (build_class_member_access_expr
2099 (maybe_dummy_object (qualifying_class, NULL),
2100 expr,
2101 BASELINK_ACCESS_BINFO (expr),
2102 /*preserve_reference=*/false,
2103 complain));
2104 else if (done)
2105 /* The expression is a qualified name whose address is not
2106 being taken. */
2107 expr = build_offset_ref (qualifying_class, expr, /*address_p=*/false,
2108 complain);
2110 else
2112 /* In a template, return a SCOPE_REF for most qualified-ids
2113 so that we can check access at instantiation time. But if
2114 we're looking at a member of the current instantiation, we
2115 know we have access and building up the SCOPE_REF confuses
2116 non-type template argument handling. */
2117 if (processing_template_decl
2118 && (!currently_open_class (qualifying_class)
2119 || TREE_CODE (expr) == IDENTIFIER_NODE
2120 || TREE_CODE (expr) == TEMPLATE_ID_EXPR
2121 || TREE_CODE (expr) == BIT_NOT_EXPR))
2122 expr = build_qualified_name (TREE_TYPE (expr),
2123 qualifying_class, expr,
2124 template_p);
2126 expr = convert_from_reference (expr);
2129 return expr;
2132 /* Begin a statement-expression. The value returned must be passed to
2133 finish_stmt_expr. */
2135 tree
2136 begin_stmt_expr (void)
2138 return push_stmt_list ();
2141 /* Process the final expression of a statement expression. EXPR can be
2142 NULL, if the final expression is empty. Return a STATEMENT_LIST
2143 containing all the statements in the statement-expression, or
2144 ERROR_MARK_NODE if there was an error. */
2146 tree
2147 finish_stmt_expr_expr (tree expr, tree stmt_expr)
2149 if (error_operand_p (expr))
2151 /* The type of the statement-expression is the type of the last
2152 expression. */
2153 TREE_TYPE (stmt_expr) = error_mark_node;
2154 return error_mark_node;
2157 /* If the last statement does not have "void" type, then the value
2158 of the last statement is the value of the entire expression. */
2159 if (expr)
2161 tree type = TREE_TYPE (expr);
2163 if (type && type_unknown_p (type))
2165 error ("a statement expression is an insufficient context"
2166 " for overload resolution");
2167 TREE_TYPE (stmt_expr) = error_mark_node;
2168 return error_mark_node;
2170 else if (processing_template_decl)
2172 expr = build_stmt (input_location, EXPR_STMT, expr);
2173 expr = add_stmt (expr);
2174 /* Mark the last statement so that we can recognize it as such at
2175 template-instantiation time. */
2176 EXPR_STMT_STMT_EXPR_RESULT (expr) = 1;
2178 else if (VOID_TYPE_P (type))
2180 /* Just treat this like an ordinary statement. */
2181 expr = finish_expr_stmt (expr);
2183 else
2185 /* It actually has a value we need to deal with. First, force it
2186 to be an rvalue so that we won't need to build up a copy
2187 constructor call later when we try to assign it to something. */
2188 expr = force_rvalue (expr, tf_warning_or_error);
2189 if (error_operand_p (expr))
2190 return error_mark_node;
2192 /* Update for array-to-pointer decay. */
2193 type = TREE_TYPE (expr);
2195 /* Wrap it in a CLEANUP_POINT_EXPR and add it to the list like a
2196 normal statement, but don't convert to void or actually add
2197 the EXPR_STMT. */
2198 if (TREE_CODE (expr) != CLEANUP_POINT_EXPR)
2199 expr = maybe_cleanup_point_expr (expr);
2200 add_stmt (expr);
2203 /* The type of the statement-expression is the type of the last
2204 expression. */
2205 TREE_TYPE (stmt_expr) = type;
2208 return stmt_expr;
2211 /* Finish a statement-expression. EXPR should be the value returned
2212 by the previous begin_stmt_expr. Returns an expression
2213 representing the statement-expression. */
2215 tree
2216 finish_stmt_expr (tree stmt_expr, bool has_no_scope)
2218 tree type;
2219 tree result;
2221 if (error_operand_p (stmt_expr))
2223 pop_stmt_list (stmt_expr);
2224 return error_mark_node;
2227 gcc_assert (TREE_CODE (stmt_expr) == STATEMENT_LIST);
2229 type = TREE_TYPE (stmt_expr);
2230 result = pop_stmt_list (stmt_expr);
2231 TREE_TYPE (result) = type;
2233 if (processing_template_decl)
2235 result = build_min (STMT_EXPR, type, result);
2236 TREE_SIDE_EFFECTS (result) = 1;
2237 STMT_EXPR_NO_SCOPE (result) = has_no_scope;
2239 else if (CLASS_TYPE_P (type))
2241 /* Wrap the statement-expression in a TARGET_EXPR so that the
2242 temporary object created by the final expression is destroyed at
2243 the end of the full-expression containing the
2244 statement-expression. */
2245 result = force_target_expr (type, result, tf_warning_or_error);
2248 return result;
2251 /* Returns the expression which provides the value of STMT_EXPR. */
2253 tree
2254 stmt_expr_value_expr (tree stmt_expr)
2256 tree t = STMT_EXPR_STMT (stmt_expr);
2258 if (TREE_CODE (t) == BIND_EXPR)
2259 t = BIND_EXPR_BODY (t);
2261 if (TREE_CODE (t) == STATEMENT_LIST && STATEMENT_LIST_TAIL (t))
2262 t = STATEMENT_LIST_TAIL (t)->stmt;
2264 if (TREE_CODE (t) == EXPR_STMT)
2265 t = EXPR_STMT_EXPR (t);
2267 return t;
2270 /* Return TRUE iff EXPR_STMT is an empty list of
2271 expression statements. */
2273 bool
2274 empty_expr_stmt_p (tree expr_stmt)
2276 tree body = NULL_TREE;
2278 if (expr_stmt == void_node)
2279 return true;
2281 if (expr_stmt)
2283 if (TREE_CODE (expr_stmt) == EXPR_STMT)
2284 body = EXPR_STMT_EXPR (expr_stmt);
2285 else if (TREE_CODE (expr_stmt) == STATEMENT_LIST)
2286 body = expr_stmt;
2289 if (body)
2291 if (TREE_CODE (body) == STATEMENT_LIST)
2292 return tsi_end_p (tsi_start (body));
2293 else
2294 return empty_expr_stmt_p (body);
2296 return false;
2299 /* Perform Koenig lookup. FN is the postfix-expression representing
2300 the function (or functions) to call; ARGS are the arguments to the
2301 call. Returns the functions to be considered by overload resolution. */
2303 cp_expr
2304 perform_koenig_lookup (cp_expr fn, vec<tree, va_gc> *args,
2305 tsubst_flags_t complain)
2307 tree identifier = NULL_TREE;
2308 tree functions = NULL_TREE;
2309 tree tmpl_args = NULL_TREE;
2310 bool template_id = false;
2311 location_t loc = fn.get_location ();
2313 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
2315 /* Use a separate flag to handle null args. */
2316 template_id = true;
2317 tmpl_args = TREE_OPERAND (fn, 1);
2318 fn = TREE_OPERAND (fn, 0);
2321 /* Find the name of the overloaded function. */
2322 if (identifier_p (fn))
2323 identifier = fn;
2324 else
2326 functions = fn;
2327 identifier = OVL_NAME (functions);
2330 /* A call to a namespace-scope function using an unqualified name.
2332 Do Koenig lookup -- unless any of the arguments are
2333 type-dependent. */
2334 if (!any_type_dependent_arguments_p (args)
2335 && !any_dependent_template_arguments_p (tmpl_args))
2337 fn = lookup_arg_dependent (identifier, functions, args);
2338 if (!fn)
2340 /* The unqualified name could not be resolved. */
2341 if (complain & tf_error)
2342 fn = unqualified_fn_lookup_error (cp_expr (identifier, loc));
2343 else
2344 fn = identifier;
2346 else if (TREE_CODE (fn) == OVERLOAD && processing_template_decl)
2347 /* FIXME: We shouldn't really need to mark the lookup here, as
2348 resolving the (non-dependent) call should save the single
2349 function we resolve to. Related to PR c++/83529. */
2350 lookup_keep (fn);
2353 if (fn && template_id && fn != error_mark_node)
2354 fn = build2 (TEMPLATE_ID_EXPR, unknown_type_node, fn, tmpl_args);
2356 return fn;
2359 /* Generate an expression for `FN (ARGS)'. This may change the
2360 contents of ARGS.
2362 If DISALLOW_VIRTUAL is true, the call to FN will be not generated
2363 as a virtual call, even if FN is virtual. (This flag is set when
2364 encountering an expression where the function name is explicitly
2365 qualified. For example a call to `X::f' never generates a virtual
2366 call.)
2368 Returns code for the call. */
2370 tree
2371 finish_call_expr (tree fn, vec<tree, va_gc> **args, bool disallow_virtual,
2372 bool koenig_p, tsubst_flags_t complain)
2374 tree result;
2375 tree orig_fn;
2376 vec<tree, va_gc> *orig_args = *args;
2378 if (fn == error_mark_node)
2379 return error_mark_node;
2381 gcc_assert (!TYPE_P (fn));
2383 /* If FN may be a FUNCTION_DECL obfuscated by force_paren_expr, undo
2384 it so that we can tell this is a call to a known function. */
2385 fn = maybe_undo_parenthesized_ref (fn);
2387 orig_fn = fn;
2389 if (processing_template_decl)
2391 /* If FN is a local extern declaration or set thereof, look them up
2392 again at instantiation time. */
2393 if (is_overloaded_fn (fn))
2395 tree ifn = get_first_fn (fn);
2396 if (TREE_CODE (ifn) == FUNCTION_DECL
2397 && DECL_LOCAL_FUNCTION_P (ifn))
2398 orig_fn = DECL_NAME (ifn);
2401 /* If the call expression is dependent, build a CALL_EXPR node
2402 with no type; type_dependent_expression_p recognizes
2403 expressions with no type as being dependent. */
2404 if (type_dependent_expression_p (fn)
2405 || any_type_dependent_arguments_p (*args))
2407 result = build_min_nt_call_vec (orig_fn, *args);
2408 SET_EXPR_LOCATION (result, cp_expr_loc_or_loc (fn, input_location));
2409 KOENIG_LOOKUP_P (result) = koenig_p;
2410 if (is_overloaded_fn (fn))
2411 fn = get_fns (fn);
2413 if (cfun)
2415 bool abnormal = true;
2416 for (lkp_iterator iter (fn); abnormal && iter; ++iter)
2418 tree fndecl = *iter;
2419 if (TREE_CODE (fndecl) != FUNCTION_DECL
2420 || !TREE_THIS_VOLATILE (fndecl))
2421 abnormal = false;
2423 /* FIXME: Stop warning about falling off end of non-void
2424 function. But this is wrong. Even if we only see
2425 no-return fns at this point, we could select a
2426 future-defined return fn during instantiation. Or
2427 vice-versa. */
2428 if (abnormal)
2429 current_function_returns_abnormally = 1;
2431 return result;
2433 orig_args = make_tree_vector_copy (*args);
2434 if (!BASELINK_P (fn)
2435 && TREE_CODE (fn) != PSEUDO_DTOR_EXPR
2436 && TREE_TYPE (fn) != unknown_type_node)
2437 fn = build_non_dependent_expr (fn);
2438 make_args_non_dependent (*args);
2441 if (TREE_CODE (fn) == COMPONENT_REF)
2443 tree member = TREE_OPERAND (fn, 1);
2444 if (BASELINK_P (member))
2446 tree object = TREE_OPERAND (fn, 0);
2447 return build_new_method_call (object, member,
2448 args, NULL_TREE,
2449 (disallow_virtual
2450 ? LOOKUP_NORMAL | LOOKUP_NONVIRTUAL
2451 : LOOKUP_NORMAL),
2452 /*fn_p=*/NULL,
2453 complain);
2457 /* Per 13.3.1.1, '(&f)(...)' is the same as '(f)(...)'. */
2458 if (TREE_CODE (fn) == ADDR_EXPR
2459 && TREE_CODE (TREE_OPERAND (fn, 0)) == OVERLOAD)
2460 fn = TREE_OPERAND (fn, 0);
2462 if (is_overloaded_fn (fn))
2463 fn = baselink_for_fns (fn);
2465 result = NULL_TREE;
2466 if (BASELINK_P (fn))
2468 tree object;
2470 /* A call to a member function. From [over.call.func]:
2472 If the keyword this is in scope and refers to the class of
2473 that member function, or a derived class thereof, then the
2474 function call is transformed into a qualified function call
2475 using (*this) as the postfix-expression to the left of the
2476 . operator.... [Otherwise] a contrived object of type T
2477 becomes the implied object argument.
2479 In this situation:
2481 struct A { void f(); };
2482 struct B : public A {};
2483 struct C : public A { void g() { B::f(); }};
2485 "the class of that member function" refers to `A'. But 11.2
2486 [class.access.base] says that we need to convert 'this' to B* as
2487 part of the access, so we pass 'B' to maybe_dummy_object. */
2489 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (get_first_fn (fn)))
2491 /* A constructor call always uses a dummy object. (This constructor
2492 call which has the form A::A () is actually invalid and we are
2493 going to reject it later in build_new_method_call.) */
2494 object = build_dummy_object (BINFO_TYPE (BASELINK_ACCESS_BINFO (fn)));
2496 else
2497 object = maybe_dummy_object (BINFO_TYPE (BASELINK_ACCESS_BINFO (fn)),
2498 NULL);
2500 result = build_new_method_call (object, fn, args, NULL_TREE,
2501 (disallow_virtual
2502 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
2503 : LOOKUP_NORMAL),
2504 /*fn_p=*/NULL,
2505 complain);
2507 else if (is_overloaded_fn (fn))
2509 /* If the function is an overloaded builtin, resolve it. */
2510 if (TREE_CODE (fn) == FUNCTION_DECL
2511 && (DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL
2512 || DECL_BUILT_IN_CLASS (fn) == BUILT_IN_MD))
2513 result = resolve_overloaded_builtin (input_location, fn, *args);
2515 if (!result)
2517 if (warn_sizeof_pointer_memaccess
2518 && (complain & tf_warning)
2519 && !vec_safe_is_empty (*args)
2520 && !processing_template_decl)
2522 location_t sizeof_arg_loc[3];
2523 tree sizeof_arg[3];
2524 unsigned int i;
2525 for (i = 0; i < 3; i++)
2527 tree t;
2529 sizeof_arg_loc[i] = UNKNOWN_LOCATION;
2530 sizeof_arg[i] = NULL_TREE;
2531 if (i >= (*args)->length ())
2532 continue;
2533 t = (**args)[i];
2534 if (TREE_CODE (t) != SIZEOF_EXPR)
2535 continue;
2536 if (SIZEOF_EXPR_TYPE_P (t))
2537 sizeof_arg[i] = TREE_TYPE (TREE_OPERAND (t, 0));
2538 else
2539 sizeof_arg[i] = TREE_OPERAND (t, 0);
2540 sizeof_arg_loc[i] = EXPR_LOCATION (t);
2542 sizeof_pointer_memaccess_warning
2543 (sizeof_arg_loc, fn, *args,
2544 sizeof_arg, same_type_ignoring_top_level_qualifiers_p);
2547 if ((complain & tf_warning)
2548 && TREE_CODE (fn) == FUNCTION_DECL
2549 && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL
2550 && DECL_FUNCTION_CODE (fn) == BUILT_IN_MEMSET
2551 && vec_safe_length (*args) == 3
2552 && !any_type_dependent_arguments_p (*args))
2554 tree arg0 = (*orig_args)[0];
2555 tree arg1 = (*orig_args)[1];
2556 tree arg2 = (*orig_args)[2];
2557 int literal_mask = ((literal_integer_zerop (arg1) << 1)
2558 | (literal_integer_zerop (arg2) << 2));
2559 arg2 = instantiate_non_dependent_expr (arg2);
2560 warn_for_memset (input_location, arg0, arg2, literal_mask);
2563 /* A call to a namespace-scope function. */
2564 result = build_new_function_call (fn, args, complain);
2567 else if (TREE_CODE (fn) == PSEUDO_DTOR_EXPR)
2569 if (!vec_safe_is_empty (*args))
2570 error ("arguments to destructor are not allowed");
2571 /* Mark the pseudo-destructor call as having side-effects so
2572 that we do not issue warnings about its use. */
2573 result = build1 (NOP_EXPR,
2574 void_type_node,
2575 TREE_OPERAND (fn, 0));
2576 TREE_SIDE_EFFECTS (result) = 1;
2578 else if (CLASS_TYPE_P (TREE_TYPE (fn)))
2579 /* If the "function" is really an object of class type, it might
2580 have an overloaded `operator ()'. */
2581 result = build_op_call (fn, args, complain);
2583 if (!result)
2584 /* A call where the function is unknown. */
2585 result = cp_build_function_call_vec (fn, args, complain);
2587 if (processing_template_decl && result != error_mark_node)
2589 if (INDIRECT_REF_P (result))
2590 result = TREE_OPERAND (result, 0);
2591 result = build_call_vec (TREE_TYPE (result), orig_fn, orig_args);
2592 SET_EXPR_LOCATION (result, input_location);
2593 KOENIG_LOOKUP_P (result) = koenig_p;
2594 release_tree_vector (orig_args);
2595 result = convert_from_reference (result);
2598 return result;
2601 /* Finish a call to a postfix increment or decrement or EXPR. (Which
2602 is indicated by CODE, which should be POSTINCREMENT_EXPR or
2603 POSTDECREMENT_EXPR.) */
2605 cp_expr
2606 finish_increment_expr (cp_expr expr, enum tree_code code)
2608 /* input_location holds the location of the trailing operator token.
2609 Build a location of the form:
2610 expr++
2611 ~~~~^~
2612 with the caret at the operator token, ranging from the start
2613 of EXPR to the end of the operator token. */
2614 location_t combined_loc = make_location (input_location,
2615 expr.get_start (),
2616 get_finish (input_location));
2617 cp_expr result = build_x_unary_op (combined_loc, code, expr,
2618 tf_warning_or_error);
2619 /* TODO: build_x_unary_op doesn't honor the location, so set it here. */
2620 result.set_location (combined_loc);
2621 return result;
2624 /* Finish a use of `this'. Returns an expression for `this'. */
2626 tree
2627 finish_this_expr (void)
2629 tree result = NULL_TREE;
2631 if (current_class_ptr)
2633 tree type = TREE_TYPE (current_class_ref);
2635 /* In a lambda expression, 'this' refers to the captured 'this'. */
2636 if (LAMBDA_TYPE_P (type))
2637 result = lambda_expr_this_capture (CLASSTYPE_LAMBDA_EXPR (type), true);
2638 else
2639 result = current_class_ptr;
2642 if (result)
2643 /* The keyword 'this' is a prvalue expression. */
2644 return rvalue (result);
2646 tree fn = current_nonlambda_function ();
2647 if (fn && DECL_STATIC_FUNCTION_P (fn))
2648 error ("%<this%> is unavailable for static member functions");
2649 else if (fn)
2650 error ("invalid use of %<this%> in non-member function");
2651 else
2652 error ("invalid use of %<this%> at top level");
2653 return error_mark_node;
2656 /* Finish a pseudo-destructor expression. If SCOPE is NULL, the
2657 expression was of the form `OBJECT.~DESTRUCTOR' where DESTRUCTOR is
2658 the TYPE for the type given. If SCOPE is non-NULL, the expression
2659 was of the form `OBJECT.SCOPE::~DESTRUCTOR'. */
2661 tree
2662 finish_pseudo_destructor_expr (tree object, tree scope, tree destructor,
2663 location_t loc)
2665 if (object == error_mark_node || destructor == error_mark_node)
2666 return error_mark_node;
2668 gcc_assert (TYPE_P (destructor));
2670 if (!processing_template_decl)
2672 if (scope == error_mark_node)
2674 error_at (loc, "invalid qualifying scope in pseudo-destructor name");
2675 return error_mark_node;
2677 if (is_auto (destructor))
2678 destructor = TREE_TYPE (object);
2679 if (scope && TYPE_P (scope) && !check_dtor_name (scope, destructor))
2681 error_at (loc,
2682 "qualified type %qT does not match destructor name ~%qT",
2683 scope, destructor);
2684 return error_mark_node;
2688 /* [expr.pseudo] says both:
2690 The type designated by the pseudo-destructor-name shall be
2691 the same as the object type.
2693 and:
2695 The cv-unqualified versions of the object type and of the
2696 type designated by the pseudo-destructor-name shall be the
2697 same type.
2699 We implement the more generous second sentence, since that is
2700 what most other compilers do. */
2701 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (object),
2702 destructor))
2704 error_at (loc, "%qE is not of type %qT", object, destructor);
2705 return error_mark_node;
2709 return build3_loc (loc, PSEUDO_DTOR_EXPR, void_type_node, object,
2710 scope, destructor);
2713 /* Finish an expression of the form CODE EXPR. */
2715 cp_expr
2716 finish_unary_op_expr (location_t op_loc, enum tree_code code, cp_expr expr,
2717 tsubst_flags_t complain)
2719 /* Build a location of the form:
2720 ++expr
2721 ^~~~~~
2722 with the caret at the operator token, ranging from the start
2723 of the operator token to the end of EXPR. */
2724 location_t combined_loc = make_location (op_loc,
2725 op_loc, expr.get_finish ());
2726 cp_expr result = build_x_unary_op (combined_loc, code, expr, complain);
2727 /* TODO: build_x_unary_op doesn't always honor the location. */
2728 result.set_location (combined_loc);
2730 tree result_ovl, expr_ovl;
2732 if (!(complain & tf_warning))
2733 return result;
2735 result_ovl = result;
2736 expr_ovl = expr;
2738 if (!processing_template_decl)
2739 expr_ovl = cp_fully_fold (expr_ovl);
2741 if (!CONSTANT_CLASS_P (expr_ovl)
2742 || TREE_OVERFLOW_P (expr_ovl))
2743 return result;
2745 if (!processing_template_decl)
2746 result_ovl = cp_fully_fold (result_ovl);
2748 if (CONSTANT_CLASS_P (result_ovl) && TREE_OVERFLOW_P (result_ovl))
2749 overflow_warning (combined_loc, result_ovl);
2751 return result;
2754 /* Finish a compound-literal expression or C++11 functional cast with aggregate
2755 initializer. TYPE is the type to which the CONSTRUCTOR in COMPOUND_LITERAL
2756 is being cast. */
2758 tree
2759 finish_compound_literal (tree type, tree compound_literal,
2760 tsubst_flags_t complain,
2761 fcl_t fcl_context)
2763 if (type == error_mark_node)
2764 return error_mark_node;
2766 if (TYPE_REF_P (type))
2768 compound_literal
2769 = finish_compound_literal (TREE_TYPE (type), compound_literal,
2770 complain, fcl_context);
2771 /* The prvalue is then used to direct-initialize the reference. */
2772 tree r = (perform_implicit_conversion_flags
2773 (type, compound_literal, complain, LOOKUP_NORMAL));
2774 return convert_from_reference (r);
2777 if (!TYPE_OBJ_P (type))
2779 if (complain & tf_error)
2780 error ("compound literal of non-object type %qT", type);
2781 return error_mark_node;
2784 if (tree anode = type_uses_auto (type))
2785 if (CLASS_PLACEHOLDER_TEMPLATE (anode))
2787 type = do_auto_deduction (type, compound_literal, anode, complain,
2788 adc_variable_type);
2789 if (type == error_mark_node)
2790 return error_mark_node;
2793 if (processing_template_decl)
2795 TREE_TYPE (compound_literal) = type;
2796 /* Mark the expression as a compound literal. */
2797 TREE_HAS_CONSTRUCTOR (compound_literal) = 1;
2798 if (fcl_context == fcl_c99)
2799 CONSTRUCTOR_C99_COMPOUND_LITERAL (compound_literal) = 1;
2800 return compound_literal;
2803 type = complete_type (type);
2805 if (TYPE_NON_AGGREGATE_CLASS (type))
2807 /* Trying to deal with a CONSTRUCTOR instead of a TREE_LIST
2808 everywhere that deals with function arguments would be a pain, so
2809 just wrap it in a TREE_LIST. The parser set a flag so we know
2810 that it came from T{} rather than T({}). */
2811 CONSTRUCTOR_IS_DIRECT_INIT (compound_literal) = 1;
2812 compound_literal = build_tree_list (NULL_TREE, compound_literal);
2813 return build_functional_cast (type, compound_literal, complain);
2816 if (TREE_CODE (type) == ARRAY_TYPE
2817 && check_array_initializer (NULL_TREE, type, compound_literal))
2818 return error_mark_node;
2819 compound_literal = reshape_init (type, compound_literal, complain);
2820 if (SCALAR_TYPE_P (type)
2821 && !BRACE_ENCLOSED_INITIALIZER_P (compound_literal)
2822 && !check_narrowing (type, compound_literal, complain))
2823 return error_mark_node;
2824 if (TREE_CODE (type) == ARRAY_TYPE
2825 && TYPE_DOMAIN (type) == NULL_TREE)
2827 cp_complete_array_type_or_error (&type, compound_literal,
2828 false, complain);
2829 if (type == error_mark_node)
2830 return error_mark_node;
2832 compound_literal = digest_init_flags (type, compound_literal, LOOKUP_NORMAL,
2833 complain);
2834 if (TREE_CODE (compound_literal) == CONSTRUCTOR)
2836 TREE_HAS_CONSTRUCTOR (compound_literal) = true;
2837 if (fcl_context == fcl_c99)
2838 CONSTRUCTOR_C99_COMPOUND_LITERAL (compound_literal) = 1;
2841 /* Put static/constant array temporaries in static variables. */
2842 /* FIXME all C99 compound literals should be variables rather than C++
2843 temporaries, unless they are used as an aggregate initializer. */
2844 if ((!at_function_scope_p () || CP_TYPE_CONST_P (type))
2845 && fcl_context == fcl_c99
2846 && TREE_CODE (type) == ARRAY_TYPE
2847 && !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
2848 && initializer_constant_valid_p (compound_literal, type))
2850 tree decl = create_temporary_var (type);
2851 DECL_INITIAL (decl) = compound_literal;
2852 TREE_STATIC (decl) = 1;
2853 if (literal_type_p (type) && CP_TYPE_CONST_NON_VOLATILE_P (type))
2855 /* 5.19 says that a constant expression can include an
2856 lvalue-rvalue conversion applied to "a glvalue of literal type
2857 that refers to a non-volatile temporary object initialized
2858 with a constant expression". Rather than try to communicate
2859 that this VAR_DECL is a temporary, just mark it constexpr. */
2860 DECL_DECLARED_CONSTEXPR_P (decl) = true;
2861 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
2862 TREE_CONSTANT (decl) = true;
2864 cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
2865 decl = pushdecl_top_level (decl);
2866 DECL_NAME (decl) = make_anon_name ();
2867 SET_DECL_ASSEMBLER_NAME (decl, DECL_NAME (decl));
2868 /* Make sure the destructor is callable. */
2869 tree clean = cxx_maybe_build_cleanup (decl, complain);
2870 if (clean == error_mark_node)
2871 return error_mark_node;
2872 return decl;
2875 /* Represent other compound literals with TARGET_EXPR so we produce
2876 an lvalue, but can elide copies. */
2877 if (!VECTOR_TYPE_P (type))
2878 compound_literal = get_target_expr_sfinae (compound_literal, complain);
2880 return compound_literal;
2883 /* Return the declaration for the function-name variable indicated by
2884 ID. */
2886 tree
2887 finish_fname (tree id)
2889 tree decl;
2891 decl = fname_decl (input_location, C_RID_CODE (id), id);
2892 if (processing_template_decl && current_function_decl
2893 && decl != error_mark_node)
2894 decl = DECL_NAME (decl);
2895 return decl;
2898 /* Finish a translation unit. */
2900 void
2901 finish_translation_unit (void)
2903 /* In case there were missing closebraces,
2904 get us back to the global binding level. */
2905 pop_everything ();
2906 while (current_namespace != global_namespace)
2907 pop_namespace ();
2909 /* Do file scope __FUNCTION__ et al. */
2910 finish_fname_decls ();
2913 /* Finish a template type parameter, specified as AGGR IDENTIFIER.
2914 Returns the parameter. */
2916 tree
2917 finish_template_type_parm (tree aggr, tree identifier)
2919 if (aggr != class_type_node)
2921 permerror (input_location, "template type parameters must use the keyword %<class%> or %<typename%>");
2922 aggr = class_type_node;
2925 return build_tree_list (aggr, identifier);
2928 /* Finish a template template parameter, specified as AGGR IDENTIFIER.
2929 Returns the parameter. */
2931 tree
2932 finish_template_template_parm (tree aggr, tree identifier)
2934 tree decl = build_decl (input_location,
2935 TYPE_DECL, identifier, NULL_TREE);
2937 tree tmpl = build_lang_decl (TEMPLATE_DECL, identifier, NULL_TREE);
2938 DECL_TEMPLATE_PARMS (tmpl) = current_template_parms;
2939 DECL_TEMPLATE_RESULT (tmpl) = decl;
2940 DECL_ARTIFICIAL (decl) = 1;
2942 // Associate the constraints with the underlying declaration,
2943 // not the template.
2944 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
2945 tree constr = build_constraints (reqs, NULL_TREE);
2946 set_constraints (decl, constr);
2948 end_template_decl ();
2950 gcc_assert (DECL_TEMPLATE_PARMS (tmpl));
2952 check_default_tmpl_args (decl, DECL_TEMPLATE_PARMS (tmpl),
2953 /*is_primary=*/true, /*is_partial=*/false,
2954 /*is_friend=*/0);
2956 return finish_template_type_parm (aggr, tmpl);
2959 /* ARGUMENT is the default-argument value for a template template
2960 parameter. If ARGUMENT is invalid, issue error messages and return
2961 the ERROR_MARK_NODE. Otherwise, ARGUMENT itself is returned. */
2963 tree
2964 check_template_template_default_arg (tree argument)
2966 if (TREE_CODE (argument) != TEMPLATE_DECL
2967 && TREE_CODE (argument) != TEMPLATE_TEMPLATE_PARM
2968 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
2970 if (TREE_CODE (argument) == TYPE_DECL)
2971 error ("invalid use of type %qT as a default value for a template "
2972 "template-parameter", TREE_TYPE (argument));
2973 else
2974 error ("invalid default argument for a template template parameter");
2975 return error_mark_node;
2978 return argument;
2981 /* Begin a class definition, as indicated by T. */
2983 tree
2984 begin_class_definition (tree t)
2986 if (error_operand_p (t) || error_operand_p (TYPE_MAIN_DECL (t)))
2987 return error_mark_node;
2989 if (processing_template_parmlist)
2991 error ("definition of %q#T inside template parameter list", t);
2992 return error_mark_node;
2995 /* According to the C++ ABI, decimal classes defined in ISO/IEC TR 24733
2996 are passed the same as decimal scalar types. */
2997 if (TREE_CODE (t) == RECORD_TYPE
2998 && !processing_template_decl)
3000 tree ns = TYPE_CONTEXT (t);
3001 if (ns && TREE_CODE (ns) == NAMESPACE_DECL
3002 && DECL_CONTEXT (ns) == std_node
3003 && DECL_NAME (ns)
3004 && id_equal (DECL_NAME (ns), "decimal"))
3006 const char *n = TYPE_NAME_STRING (t);
3007 if ((strcmp (n, "decimal32") == 0)
3008 || (strcmp (n, "decimal64") == 0)
3009 || (strcmp (n, "decimal128") == 0))
3010 TYPE_TRANSPARENT_AGGR (t) = 1;
3014 /* A non-implicit typename comes from code like:
3016 template <typename T> struct A {
3017 template <typename U> struct A<T>::B ...
3019 This is erroneous. */
3020 else if (TREE_CODE (t) == TYPENAME_TYPE)
3022 error ("invalid definition of qualified type %qT", t);
3023 t = error_mark_node;
3026 if (t == error_mark_node || ! MAYBE_CLASS_TYPE_P (t))
3028 t = make_class_type (RECORD_TYPE);
3029 pushtag (make_anon_name (), t, /*tag_scope=*/ts_current);
3032 if (TYPE_BEING_DEFINED (t))
3034 t = make_class_type (TREE_CODE (t));
3035 pushtag (TYPE_IDENTIFIER (t), t, /*tag_scope=*/ts_current);
3037 maybe_process_partial_specialization (t);
3038 pushclass (t);
3039 TYPE_BEING_DEFINED (t) = 1;
3040 class_binding_level->defining_class_p = 1;
3042 if (flag_pack_struct)
3044 tree v;
3045 TYPE_PACKED (t) = 1;
3046 /* Even though the type is being defined for the first time
3047 here, there might have been a forward declaration, so there
3048 might be cv-qualified variants of T. */
3049 for (v = TYPE_NEXT_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v))
3050 TYPE_PACKED (v) = 1;
3052 /* Reset the interface data, at the earliest possible
3053 moment, as it might have been set via a class foo;
3054 before. */
3055 if (! TYPE_UNNAMED_P (t))
3057 struct c_fileinfo *finfo = \
3058 get_fileinfo (LOCATION_FILE (input_location));
3059 CLASSTYPE_INTERFACE_ONLY (t) = finfo->interface_only;
3060 SET_CLASSTYPE_INTERFACE_UNKNOWN_X
3061 (t, finfo->interface_unknown);
3063 reset_specialization();
3065 /* Make a declaration for this class in its own scope. */
3066 build_self_reference ();
3068 return t;
3071 /* Finish the member declaration given by DECL. */
3073 void
3074 finish_member_declaration (tree decl)
3076 if (decl == error_mark_node || decl == NULL_TREE)
3077 return;
3079 if (decl == void_type_node)
3080 /* The COMPONENT was a friend, not a member, and so there's
3081 nothing for us to do. */
3082 return;
3084 /* We should see only one DECL at a time. */
3085 gcc_assert (DECL_CHAIN (decl) == NULL_TREE);
3087 /* Don't add decls after definition. */
3088 gcc_assert (TYPE_BEING_DEFINED (current_class_type)
3089 /* We can add lambda types when late parsing default
3090 arguments. */
3091 || LAMBDA_TYPE_P (TREE_TYPE (decl)));
3093 /* Set up access control for DECL. */
3094 TREE_PRIVATE (decl)
3095 = (current_access_specifier == access_private_node);
3096 TREE_PROTECTED (decl)
3097 = (current_access_specifier == access_protected_node);
3098 if (TREE_CODE (decl) == TEMPLATE_DECL)
3100 TREE_PRIVATE (DECL_TEMPLATE_RESULT (decl)) = TREE_PRIVATE (decl);
3101 TREE_PROTECTED (DECL_TEMPLATE_RESULT (decl)) = TREE_PROTECTED (decl);
3104 /* Mark the DECL as a member of the current class, unless it's
3105 a member of an enumeration. */
3106 if (TREE_CODE (decl) != CONST_DECL)
3107 DECL_CONTEXT (decl) = current_class_type;
3109 if (TREE_CODE (decl) == USING_DECL)
3110 /* For now, ignore class-scope USING_DECLS, so that debugging
3111 backends do not see them. */
3112 DECL_IGNORED_P (decl) = 1;
3114 /* Check for bare parameter packs in the non-static data member
3115 declaration. */
3116 if (TREE_CODE (decl) == FIELD_DECL)
3118 if (check_for_bare_parameter_packs (TREE_TYPE (decl)))
3119 TREE_TYPE (decl) = error_mark_node;
3120 if (check_for_bare_parameter_packs (DECL_ATTRIBUTES (decl)))
3121 DECL_ATTRIBUTES (decl) = NULL_TREE;
3124 /* [dcl.link]
3126 A C language linkage is ignored for the names of class members
3127 and the member function type of class member functions. */
3128 if (DECL_LANG_SPECIFIC (decl))
3129 SET_DECL_LANGUAGE (decl, lang_cplusplus);
3131 bool add = false;
3133 /* Functions and non-functions are added differently. */
3134 if (DECL_DECLARES_FUNCTION_P (decl))
3135 add = add_method (current_class_type, decl, false);
3136 /* Enter the DECL into the scope of the class, if the class
3137 isn't a closure (whose fields are supposed to be unnamed). */
3138 else if (CLASSTYPE_LAMBDA_EXPR (current_class_type)
3139 || pushdecl_class_level (decl))
3140 add = true;
3142 if (add)
3144 /* All TYPE_DECLs go at the end of TYPE_FIELDS. Ordinary fields
3145 go at the beginning. The reason is that
3146 legacy_nonfn_member_lookup searches the list in order, and we
3147 want a field name to override a type name so that the "struct
3148 stat hack" will work. In particular:
3150 struct S { enum E { }; static const int E = 5; int ary[S::E]; } s;
3152 is valid. */
3154 if (TREE_CODE (decl) == TYPE_DECL)
3155 TYPE_FIELDS (current_class_type)
3156 = chainon (TYPE_FIELDS (current_class_type), decl);
3157 else
3159 DECL_CHAIN (decl) = TYPE_FIELDS (current_class_type);
3160 TYPE_FIELDS (current_class_type) = decl;
3163 maybe_add_class_template_decl_list (current_class_type, decl,
3164 /*friend_p=*/0);
3168 /* Finish processing a complete template declaration. The PARMS are
3169 the template parameters. */
3171 void
3172 finish_template_decl (tree parms)
3174 if (parms)
3175 end_template_decl ();
3176 else
3177 end_specialization ();
3180 // Returns the template type of the class scope being entered. If we're
3181 // entering a constrained class scope. TYPE is the class template
3182 // scope being entered and we may need to match the intended type with
3183 // a constrained specialization. For example:
3185 // template<Object T>
3186 // struct S { void f(); }; #1
3188 // template<Object T>
3189 // void S<T>::f() { } #2
3191 // We check, in #2, that S<T> refers precisely to the type declared by
3192 // #1 (i.e., that the constraints match). Note that the following should
3193 // be an error since there is no specialization of S<T> that is
3194 // unconstrained, but this is not diagnosed here.
3196 // template<typename T>
3197 // void S<T>::f() { }
3199 // We cannot diagnose this problem here since this function also matches
3200 // qualified template names that are not part of a definition. For example:
3202 // template<Integral T, Floating_point U>
3203 // typename pair<T, U>::first_type void f(T, U);
3205 // Here, it is unlikely that there is a partial specialization of
3206 // pair constrained for for Integral and Floating_point arguments.
3208 // The general rule is: if a constrained specialization with matching
3209 // constraints is found return that type. Also note that if TYPE is not a
3210 // class-type (e.g. a typename type), then no fixup is needed.
3212 static tree
3213 fixup_template_type (tree type)
3215 // Find the template parameter list at the a depth appropriate to
3216 // the scope we're trying to enter.
3217 tree parms = current_template_parms;
3218 int depth = template_class_depth (type);
3219 for (int n = processing_template_decl; n > depth && parms; --n)
3220 parms = TREE_CHAIN (parms);
3221 if (!parms)
3222 return type;
3223 tree cur_reqs = TEMPLATE_PARMS_CONSTRAINTS (parms);
3224 tree cur_constr = build_constraints (cur_reqs, NULL_TREE);
3226 // Search for a specialization whose type and constraints match.
3227 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
3228 tree specs = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
3229 while (specs)
3231 tree spec_constr = get_constraints (TREE_VALUE (specs));
3233 // If the type and constraints match a specialization, then we
3234 // are entering that type.
3235 if (same_type_p (type, TREE_TYPE (specs))
3236 && equivalent_constraints (cur_constr, spec_constr))
3237 return TREE_TYPE (specs);
3238 specs = TREE_CHAIN (specs);
3241 // If no specialization matches, then must return the type
3242 // previously found.
3243 return type;
3246 /* Finish processing a template-id (which names a type) of the form
3247 NAME < ARGS >. Return the TYPE_DECL for the type named by the
3248 template-id. If ENTERING_SCOPE is nonzero we are about to enter
3249 the scope of template-id indicated. */
3251 tree
3252 finish_template_type (tree name, tree args, int entering_scope)
3254 tree type;
3256 type = lookup_template_class (name, args,
3257 NULL_TREE, NULL_TREE, entering_scope,
3258 tf_warning_or_error | tf_user);
3260 /* If we might be entering the scope of a partial specialization,
3261 find the one with the right constraints. */
3262 if (flag_concepts
3263 && entering_scope
3264 && CLASS_TYPE_P (type)
3265 && CLASSTYPE_TEMPLATE_INFO (type)
3266 && dependent_type_p (type)
3267 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
3268 type = fixup_template_type (type);
3270 if (type == error_mark_node)
3271 return type;
3272 else if (CLASS_TYPE_P (type) && !alias_type_or_template_p (type))
3273 return TYPE_STUB_DECL (type);
3274 else
3275 return TYPE_NAME (type);
3278 /* Finish processing a BASE_CLASS with the indicated ACCESS_SPECIFIER.
3279 Return a TREE_LIST containing the ACCESS_SPECIFIER and the
3280 BASE_CLASS, or NULL_TREE if an error occurred. The
3281 ACCESS_SPECIFIER is one of
3282 access_{default,public,protected_private}_node. For a virtual base
3283 we set TREE_TYPE. */
3285 tree
3286 finish_base_specifier (tree base, tree access, bool virtual_p)
3288 tree result;
3290 if (base == error_mark_node)
3292 error ("invalid base-class specification");
3293 result = NULL_TREE;
3295 else if (! MAYBE_CLASS_TYPE_P (base))
3297 error ("%qT is not a class type", base);
3298 result = NULL_TREE;
3300 else
3302 if (cp_type_quals (base) != 0)
3304 /* DR 484: Can a base-specifier name a cv-qualified
3305 class type? */
3306 base = TYPE_MAIN_VARIANT (base);
3308 result = build_tree_list (access, base);
3309 if (virtual_p)
3310 TREE_TYPE (result) = integer_type_node;
3313 return result;
3316 /* If FNS is a member function, a set of member functions, or a
3317 template-id referring to one or more member functions, return a
3318 BASELINK for FNS, incorporating the current access context.
3319 Otherwise, return FNS unchanged. */
3321 tree
3322 baselink_for_fns (tree fns)
3324 tree scope;
3325 tree cl;
3327 if (BASELINK_P (fns)
3328 || error_operand_p (fns))
3329 return fns;
3331 scope = ovl_scope (fns);
3332 if (!CLASS_TYPE_P (scope))
3333 return fns;
3335 cl = currently_open_derived_class (scope);
3336 if (!cl)
3337 cl = scope;
3338 cl = TYPE_BINFO (cl);
3339 return build_baselink (cl, cl, fns, /*optype=*/NULL_TREE);
3342 /* Returns true iff DECL is a variable from a function outside
3343 the current one. */
3345 static bool
3346 outer_var_p (tree decl)
3348 return ((VAR_P (decl) || TREE_CODE (decl) == PARM_DECL)
3349 && DECL_FUNCTION_SCOPE_P (decl)
3350 /* Don't get confused by temporaries. */
3351 && DECL_NAME (decl)
3352 && (DECL_CONTEXT (decl) != current_function_decl
3353 || parsing_nsdmi ()));
3356 /* As above, but also checks that DECL is automatic. */
3358 bool
3359 outer_automatic_var_p (tree decl)
3361 return (outer_var_p (decl)
3362 && !TREE_STATIC (decl));
3365 /* DECL satisfies outer_automatic_var_p. Possibly complain about it or
3366 rewrite it for lambda capture.
3368 If ODR_USE is true, we're being called from mark_use, and we complain about
3369 use of constant variables. If ODR_USE is false, we're being called for the
3370 id-expression, and we do lambda capture. */
3372 tree
3373 process_outer_var_ref (tree decl, tsubst_flags_t complain, bool odr_use)
3375 if (cp_unevaluated_operand)
3376 /* It's not a use (3.2) if we're in an unevaluated context. */
3377 return decl;
3378 if (decl == error_mark_node)
3379 return decl;
3381 tree context = DECL_CONTEXT (decl);
3382 tree containing_function = current_function_decl;
3383 tree lambda_stack = NULL_TREE;
3384 tree lambda_expr = NULL_TREE;
3385 tree initializer = convert_from_reference (decl);
3387 /* Mark it as used now even if the use is ill-formed. */
3388 if (!mark_used (decl, complain))
3389 return error_mark_node;
3391 if (parsing_nsdmi ())
3392 containing_function = NULL_TREE;
3394 if (containing_function && LAMBDA_FUNCTION_P (containing_function))
3396 /* Check whether we've already built a proxy. */
3397 tree var = decl;
3398 while (is_normal_capture_proxy (var))
3399 var = DECL_CAPTURED_VARIABLE (var);
3400 tree d = retrieve_local_specialization (var);
3402 if (d && d != decl && is_capture_proxy (d))
3404 if (DECL_CONTEXT (d) == containing_function)
3405 /* We already have an inner proxy. */
3406 return d;
3407 else
3408 /* We need to capture an outer proxy. */
3409 return process_outer_var_ref (d, complain, odr_use);
3413 /* If we are in a lambda function, we can move out until we hit
3414 1. the context,
3415 2. a non-lambda function, or
3416 3. a non-default capturing lambda function. */
3417 while (context != containing_function
3418 /* containing_function can be null with invalid generic lambdas. */
3419 && containing_function
3420 && LAMBDA_FUNCTION_P (containing_function))
3422 tree closure = DECL_CONTEXT (containing_function);
3423 lambda_expr = CLASSTYPE_LAMBDA_EXPR (closure);
3425 if (TYPE_CLASS_SCOPE_P (closure))
3426 /* A lambda in an NSDMI (c++/64496). */
3427 break;
3429 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr)
3430 == CPLD_NONE)
3431 break;
3433 lambda_stack = tree_cons (NULL_TREE,
3434 lambda_expr,
3435 lambda_stack);
3437 containing_function
3438 = decl_function_context (containing_function);
3441 /* In a lambda within a template, wait until instantiation
3442 time to implicitly capture. */
3443 if (context == containing_function
3444 && DECL_TEMPLATE_INFO (containing_function)
3445 && uses_template_parms (DECL_TI_ARGS (containing_function)))
3446 return decl;
3448 if (lambda_expr && VAR_P (decl)
3449 && DECL_ANON_UNION_VAR_P (decl))
3451 if (complain & tf_error)
3452 error ("cannot capture member %qD of anonymous union", decl);
3453 return error_mark_node;
3455 /* Do lambda capture when processing the id-expression, not when
3456 odr-using a variable. */
3457 if (!odr_use && context == containing_function)
3459 decl = add_default_capture (lambda_stack,
3460 /*id=*/DECL_NAME (decl),
3461 initializer);
3463 /* Only an odr-use of an outer automatic variable causes an
3464 error, and a constant variable can decay to a prvalue
3465 constant without odr-use. So don't complain yet. */
3466 else if (!odr_use && decl_constant_var_p (decl))
3467 return decl;
3468 else if (lambda_expr)
3470 if (complain & tf_error)
3472 error ("%qD is not captured", decl);
3473 tree closure = LAMBDA_EXPR_CLOSURE (lambda_expr);
3474 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr)
3475 == CPLD_NONE)
3476 inform (location_of (closure),
3477 "the lambda has no capture-default");
3478 else if (TYPE_CLASS_SCOPE_P (closure))
3479 inform (UNKNOWN_LOCATION, "lambda in local class %q+T cannot "
3480 "capture variables from the enclosing context",
3481 TYPE_CONTEXT (closure));
3482 inform (DECL_SOURCE_LOCATION (decl), "%q#D declared here", decl);
3484 return error_mark_node;
3486 else
3488 if (complain & tf_error)
3490 error (VAR_P (decl)
3491 ? G_("use of local variable with automatic storage from "
3492 "containing function")
3493 : G_("use of parameter from containing function"));
3494 inform (DECL_SOURCE_LOCATION (decl), "%q#D declared here", decl);
3496 return error_mark_node;
3498 return decl;
3501 /* ID_EXPRESSION is a representation of parsed, but unprocessed,
3502 id-expression. (See cp_parser_id_expression for details.) SCOPE,
3503 if non-NULL, is the type or namespace used to explicitly qualify
3504 ID_EXPRESSION. DECL is the entity to which that name has been
3505 resolved.
3507 *CONSTANT_EXPRESSION_P is true if we are presently parsing a
3508 constant-expression. In that case, *NON_CONSTANT_EXPRESSION_P will
3509 be set to true if this expression isn't permitted in a
3510 constant-expression, but it is otherwise not set by this function.
3511 *ALLOW_NON_CONSTANT_EXPRESSION_P is true if we are parsing a
3512 constant-expression, but a non-constant expression is also
3513 permissible.
3515 DONE is true if this expression is a complete postfix-expression;
3516 it is false if this expression is followed by '->', '[', '(', etc.
3517 ADDRESS_P is true iff this expression is the operand of '&'.
3518 TEMPLATE_P is true iff the qualified-id was of the form
3519 "A::template B". TEMPLATE_ARG_P is true iff this qualified name
3520 appears as a template argument.
3522 If an error occurs, and it is the kind of error that might cause
3523 the parser to abort a tentative parse, *ERROR_MSG is filled in. It
3524 is the caller's responsibility to issue the message. *ERROR_MSG
3525 will be a string with static storage duration, so the caller need
3526 not "free" it.
3528 Return an expression for the entity, after issuing appropriate
3529 diagnostics. This function is also responsible for transforming a
3530 reference to a non-static member into a COMPONENT_REF that makes
3531 the use of "this" explicit.
3533 Upon return, *IDK will be filled in appropriately. */
3534 cp_expr
3535 finish_id_expression (tree id_expression,
3536 tree decl,
3537 tree scope,
3538 cp_id_kind *idk,
3539 bool integral_constant_expression_p,
3540 bool allow_non_integral_constant_expression_p,
3541 bool *non_integral_constant_expression_p,
3542 bool template_p,
3543 bool done,
3544 bool address_p,
3545 bool template_arg_p,
3546 const char **error_msg,
3547 location_t location)
3549 decl = strip_using_decl (decl);
3551 /* Initialize the output parameters. */
3552 *idk = CP_ID_KIND_NONE;
3553 *error_msg = NULL;
3555 if (id_expression == error_mark_node)
3556 return error_mark_node;
3557 /* If we have a template-id, then no further lookup is
3558 required. If the template-id was for a template-class, we
3559 will sometimes have a TYPE_DECL at this point. */
3560 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3561 || TREE_CODE (decl) == TYPE_DECL)
3563 /* Look up the name. */
3564 else
3566 if (decl == error_mark_node)
3568 /* Name lookup failed. */
3569 if (scope
3570 && (!TYPE_P (scope)
3571 || (!dependent_type_p (scope)
3572 && !(identifier_p (id_expression)
3573 && IDENTIFIER_CONV_OP_P (id_expression)
3574 && dependent_type_p (TREE_TYPE (id_expression))))))
3576 /* If the qualifying type is non-dependent (and the name
3577 does not name a conversion operator to a dependent
3578 type), issue an error. */
3579 qualified_name_lookup_error (scope, id_expression, decl, location);
3580 return error_mark_node;
3582 else if (!scope)
3584 /* It may be resolved via Koenig lookup. */
3585 *idk = CP_ID_KIND_UNQUALIFIED;
3586 return id_expression;
3588 else
3589 decl = id_expression;
3592 /* Remember that the name was used in the definition of
3593 the current class so that we can check later to see if
3594 the meaning would have been different after the class
3595 was entirely defined. */
3596 if (!scope && decl != error_mark_node && identifier_p (id_expression))
3597 maybe_note_name_used_in_class (id_expression, decl);
3599 /* A use in unevaluated operand might not be instantiated appropriately
3600 if tsubst_copy builds a dummy parm, or if we never instantiate a
3601 generic lambda, so mark it now. */
3602 if (processing_template_decl && cp_unevaluated_operand)
3603 mark_type_use (decl);
3605 /* Disallow uses of local variables from containing functions, except
3606 within lambda-expressions. */
3607 if (outer_automatic_var_p (decl))
3609 decl = process_outer_var_ref (decl, tf_warning_or_error);
3610 if (decl == error_mark_node)
3611 return error_mark_node;
3614 /* Also disallow uses of function parameters outside the function
3615 body, except inside an unevaluated context (i.e. decltype). */
3616 if (TREE_CODE (decl) == PARM_DECL
3617 && DECL_CONTEXT (decl) == NULL_TREE
3618 && !cp_unevaluated_operand)
3620 *error_msg = G_("use of parameter outside function body");
3621 return error_mark_node;
3625 /* If we didn't find anything, or what we found was a type,
3626 then this wasn't really an id-expression. */
3627 if (TREE_CODE (decl) == TEMPLATE_DECL
3628 && !DECL_FUNCTION_TEMPLATE_P (decl))
3630 *error_msg = G_("missing template arguments");
3631 return error_mark_node;
3633 else if (TREE_CODE (decl) == TYPE_DECL
3634 || TREE_CODE (decl) == NAMESPACE_DECL)
3636 *error_msg = G_("expected primary-expression");
3637 return error_mark_node;
3640 /* If the name resolved to a template parameter, there is no
3641 need to look it up again later. */
3642 if ((TREE_CODE (decl) == CONST_DECL && DECL_TEMPLATE_PARM_P (decl))
3643 || TREE_CODE (decl) == TEMPLATE_PARM_INDEX)
3645 tree r;
3647 *idk = CP_ID_KIND_NONE;
3648 if (TREE_CODE (decl) == TEMPLATE_PARM_INDEX)
3649 decl = TEMPLATE_PARM_DECL (decl);
3650 r = convert_from_reference (DECL_INITIAL (decl));
3652 if (integral_constant_expression_p
3653 && !dependent_type_p (TREE_TYPE (decl))
3654 && !(INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (r))))
3656 if (!allow_non_integral_constant_expression_p)
3657 error ("template parameter %qD of type %qT is not allowed in "
3658 "an integral constant expression because it is not of "
3659 "integral or enumeration type", decl, TREE_TYPE (decl));
3660 *non_integral_constant_expression_p = true;
3662 return r;
3664 else
3666 bool dependent_p = type_dependent_expression_p (decl);
3668 /* If the declaration was explicitly qualified indicate
3669 that. The semantics of `A::f(3)' are different than
3670 `f(3)' if `f' is virtual. */
3671 *idk = (scope
3672 ? CP_ID_KIND_QUALIFIED
3673 : (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3674 ? CP_ID_KIND_TEMPLATE_ID
3675 : (dependent_p
3676 ? CP_ID_KIND_UNQUALIFIED_DEPENDENT
3677 : CP_ID_KIND_UNQUALIFIED)));
3679 if (dependent_p
3680 && DECL_P (decl)
3681 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (decl)))
3682 /* Dependent type attributes on the decl mean that the TREE_TYPE is
3683 wrong, so just return the identifier. */
3684 return id_expression;
3686 if (TREE_CODE (decl) == NAMESPACE_DECL)
3688 error ("use of namespace %qD as expression", decl);
3689 return error_mark_node;
3691 else if (DECL_CLASS_TEMPLATE_P (decl))
3693 error ("use of class template %qT as expression", decl);
3694 return error_mark_node;
3696 else if (TREE_CODE (decl) == TREE_LIST)
3698 /* Ambiguous reference to base members. */
3699 error ("request for member %qD is ambiguous in "
3700 "multiple inheritance lattice", id_expression);
3701 print_candidates (decl);
3702 return error_mark_node;
3705 /* Mark variable-like entities as used. Functions are similarly
3706 marked either below or after overload resolution. */
3707 if ((VAR_P (decl)
3708 || TREE_CODE (decl) == PARM_DECL
3709 || TREE_CODE (decl) == CONST_DECL
3710 || TREE_CODE (decl) == RESULT_DECL)
3711 && !mark_used (decl))
3712 return error_mark_node;
3714 /* Only certain kinds of names are allowed in constant
3715 expression. Template parameters have already
3716 been handled above. */
3717 if (! error_operand_p (decl)
3718 && !dependent_p
3719 && integral_constant_expression_p
3720 && ! decl_constant_var_p (decl)
3721 && TREE_CODE (decl) != CONST_DECL
3722 && ! builtin_valid_in_constant_expr_p (decl))
3724 if (!allow_non_integral_constant_expression_p)
3726 error ("%qD cannot appear in a constant-expression", decl);
3727 return error_mark_node;
3729 *non_integral_constant_expression_p = true;
3732 tree wrap;
3733 if (VAR_P (decl)
3734 && !cp_unevaluated_operand
3735 && !processing_template_decl
3736 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl))
3737 && CP_DECL_THREAD_LOCAL_P (decl)
3738 && (wrap = get_tls_wrapper_fn (decl)))
3740 /* Replace an evaluated use of the thread_local variable with
3741 a call to its wrapper. */
3742 decl = build_cxx_call (wrap, 0, NULL, tf_warning_or_error);
3744 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3745 && !dependent_p
3746 && variable_template_p (TREE_OPERAND (decl, 0)))
3748 decl = finish_template_variable (decl);
3749 mark_used (decl);
3750 decl = convert_from_reference (decl);
3752 else if (scope)
3754 if (TREE_CODE (decl) == SCOPE_REF)
3756 gcc_assert (same_type_p (scope, TREE_OPERAND (decl, 0)));
3757 decl = TREE_OPERAND (decl, 1);
3760 decl = (adjust_result_of_qualified_name_lookup
3761 (decl, scope, current_nonlambda_class_type()));
3763 if (TREE_CODE (decl) == FUNCTION_DECL)
3764 mark_used (decl);
3766 if (TYPE_P (scope))
3767 decl = finish_qualified_id_expr (scope,
3768 decl,
3769 done,
3770 address_p,
3771 template_p,
3772 template_arg_p,
3773 tf_warning_or_error);
3774 else
3775 decl = convert_from_reference (decl);
3777 else if (TREE_CODE (decl) == FIELD_DECL)
3779 /* Since SCOPE is NULL here, this is an unqualified name.
3780 Access checking has been performed during name lookup
3781 already. Turn off checking to avoid duplicate errors. */
3782 push_deferring_access_checks (dk_no_check);
3783 decl = finish_non_static_data_member (decl, NULL_TREE,
3784 /*qualifying_scope=*/NULL_TREE);
3785 pop_deferring_access_checks ();
3787 else if (is_overloaded_fn (decl))
3789 tree first_fn = get_first_fn (decl);
3791 if (TREE_CODE (first_fn) == TEMPLATE_DECL)
3792 first_fn = DECL_TEMPLATE_RESULT (first_fn);
3794 /* [basic.def.odr]: "A function whose name appears as a
3795 potentially-evaluated expression is odr-used if it is the unique
3796 lookup result".
3798 But only mark it if it's a complete postfix-expression; in a call,
3799 ADL might select a different function, and we'll call mark_used in
3800 build_over_call. */
3801 if (done
3802 && !really_overloaded_fn (decl)
3803 && !mark_used (first_fn))
3804 return error_mark_node;
3806 if (!template_arg_p
3807 && TREE_CODE (first_fn) == FUNCTION_DECL
3808 && DECL_FUNCTION_MEMBER_P (first_fn)
3809 && !shared_member_p (decl))
3811 /* A set of member functions. */
3812 decl = maybe_dummy_object (DECL_CONTEXT (first_fn), 0);
3813 return finish_class_member_access_expr (decl, id_expression,
3814 /*template_p=*/false,
3815 tf_warning_or_error);
3818 decl = baselink_for_fns (decl);
3820 else
3822 if (DECL_P (decl) && DECL_NONLOCAL (decl)
3823 && DECL_CLASS_SCOPE_P (decl))
3825 tree context = context_for_name_lookup (decl);
3826 if (context != current_class_type)
3828 tree path = currently_open_derived_class (context);
3829 perform_or_defer_access_check (TYPE_BINFO (path),
3830 decl, decl,
3831 tf_warning_or_error);
3835 decl = convert_from_reference (decl);
3839 return cp_expr (decl, location);
3842 /* Implement the __typeof keyword: Return the type of EXPR, suitable for
3843 use as a type-specifier. */
3845 tree
3846 finish_typeof (tree expr)
3848 tree type;
3850 if (type_dependent_expression_p (expr))
3852 type = cxx_make_type (TYPEOF_TYPE);
3853 TYPEOF_TYPE_EXPR (type) = expr;
3854 SET_TYPE_STRUCTURAL_EQUALITY (type);
3856 return type;
3859 expr = mark_type_use (expr);
3861 type = unlowered_expr_type (expr);
3863 if (!type || type == unknown_type_node)
3865 error ("type of %qE is unknown", expr);
3866 return error_mark_node;
3869 return type;
3872 /* Implement the __underlying_type keyword: Return the underlying
3873 type of TYPE, suitable for use as a type-specifier. */
3875 tree
3876 finish_underlying_type (tree type)
3878 tree underlying_type;
3880 if (processing_template_decl)
3882 underlying_type = cxx_make_type (UNDERLYING_TYPE);
3883 UNDERLYING_TYPE_TYPE (underlying_type) = type;
3884 SET_TYPE_STRUCTURAL_EQUALITY (underlying_type);
3886 return underlying_type;
3889 if (!complete_type_or_else (type, NULL_TREE))
3890 return error_mark_node;
3892 if (TREE_CODE (type) != ENUMERAL_TYPE)
3894 error ("%qT is not an enumeration type", type);
3895 return error_mark_node;
3898 underlying_type = ENUM_UNDERLYING_TYPE (type);
3900 /* Fixup necessary in this case because ENUM_UNDERLYING_TYPE
3901 includes TYPE_MIN_VALUE and TYPE_MAX_VALUE information.
3902 See finish_enum_value_list for details. */
3903 if (!ENUM_FIXED_UNDERLYING_TYPE_P (type))
3904 underlying_type
3905 = c_common_type_for_mode (TYPE_MODE (underlying_type),
3906 TYPE_UNSIGNED (underlying_type));
3908 return underlying_type;
3911 /* Implement the __direct_bases keyword: Return the direct base classes
3912 of type. */
3914 tree
3915 calculate_direct_bases (tree type, tsubst_flags_t complain)
3917 if (!complete_type_or_maybe_complain (type, NULL_TREE, complain)
3918 || !NON_UNION_CLASS_TYPE_P (type))
3919 return make_tree_vec (0);
3921 vec<tree, va_gc> *vector = make_tree_vector ();
3922 vec<tree, va_gc> *base_binfos = BINFO_BASE_BINFOS (TYPE_BINFO (type));
3923 tree binfo;
3924 unsigned i;
3926 /* Virtual bases are initialized first */
3927 for (i = 0; base_binfos->iterate (i, &binfo); i++)
3928 if (BINFO_VIRTUAL_P (binfo))
3929 vec_safe_push (vector, binfo);
3931 /* Now non-virtuals */
3932 for (i = 0; base_binfos->iterate (i, &binfo); i++)
3933 if (!BINFO_VIRTUAL_P (binfo))
3934 vec_safe_push (vector, binfo);
3936 tree bases_vec = make_tree_vec (vector->length ());
3938 for (i = 0; i < vector->length (); ++i)
3939 TREE_VEC_ELT (bases_vec, i) = BINFO_TYPE ((*vector)[i]);
3941 release_tree_vector (vector);
3942 return bases_vec;
3945 /* Implement the __bases keyword: Return the base classes
3946 of type */
3948 /* Find morally non-virtual base classes by walking binfo hierarchy */
3949 /* Virtual base classes are handled separately in finish_bases */
3951 static tree
3952 dfs_calculate_bases_pre (tree binfo, void * /*data_*/)
3954 /* Don't walk bases of virtual bases */
3955 return BINFO_VIRTUAL_P (binfo) ? dfs_skip_bases : NULL_TREE;
3958 static tree
3959 dfs_calculate_bases_post (tree binfo, void *data_)
3961 vec<tree, va_gc> **data = ((vec<tree, va_gc> **) data_);
3962 if (!BINFO_VIRTUAL_P (binfo))
3963 vec_safe_push (*data, BINFO_TYPE (binfo));
3964 return NULL_TREE;
3967 /* Calculates the morally non-virtual base classes of a class */
3968 static vec<tree, va_gc> *
3969 calculate_bases_helper (tree type)
3971 vec<tree, va_gc> *vector = make_tree_vector ();
3973 /* Now add non-virtual base classes in order of construction */
3974 if (TYPE_BINFO (type))
3975 dfs_walk_all (TYPE_BINFO (type),
3976 dfs_calculate_bases_pre, dfs_calculate_bases_post, &vector);
3977 return vector;
3980 tree
3981 calculate_bases (tree type, tsubst_flags_t complain)
3983 if (!complete_type_or_maybe_complain (type, NULL_TREE, complain)
3984 || !NON_UNION_CLASS_TYPE_P (type))
3985 return make_tree_vec (0);
3987 vec<tree, va_gc> *vector = make_tree_vector ();
3988 tree bases_vec = NULL_TREE;
3989 unsigned i;
3990 vec<tree, va_gc> *vbases;
3991 vec<tree, va_gc> *nonvbases;
3992 tree binfo;
3994 /* First go through virtual base classes */
3995 for (vbases = CLASSTYPE_VBASECLASSES (type), i = 0;
3996 vec_safe_iterate (vbases, i, &binfo); i++)
3998 vec<tree, va_gc> *vbase_bases
3999 = calculate_bases_helper (BINFO_TYPE (binfo));
4000 vec_safe_splice (vector, vbase_bases);
4001 release_tree_vector (vbase_bases);
4004 /* Now for the non-virtual bases */
4005 nonvbases = calculate_bases_helper (type);
4006 vec_safe_splice (vector, nonvbases);
4007 release_tree_vector (nonvbases);
4009 /* Note that during error recovery vector->length can even be zero. */
4010 if (vector->length () > 1)
4012 /* Last element is entire class, so don't copy */
4013 bases_vec = make_tree_vec (vector->length () - 1);
4015 for (i = 0; i < vector->length () - 1; ++i)
4016 TREE_VEC_ELT (bases_vec, i) = (*vector)[i];
4018 else
4019 bases_vec = make_tree_vec (0);
4021 release_tree_vector (vector);
4022 return bases_vec;
4025 tree
4026 finish_bases (tree type, bool direct)
4028 tree bases = NULL_TREE;
4030 if (!processing_template_decl)
4032 /* Parameter packs can only be used in templates */
4033 error ("Parameter pack __bases only valid in template declaration");
4034 return error_mark_node;
4037 bases = cxx_make_type (BASES);
4038 BASES_TYPE (bases) = type;
4039 BASES_DIRECT (bases) = direct;
4040 SET_TYPE_STRUCTURAL_EQUALITY (bases);
4042 return bases;
4045 /* Perform C++-specific checks for __builtin_offsetof before calling
4046 fold_offsetof. */
4048 tree
4049 finish_offsetof (tree object_ptr, tree expr, location_t loc)
4051 /* If we're processing a template, we can't finish the semantics yet.
4052 Otherwise we can fold the entire expression now. */
4053 if (processing_template_decl)
4055 expr = build2 (OFFSETOF_EXPR, size_type_node, expr, object_ptr);
4056 SET_EXPR_LOCATION (expr, loc);
4057 return expr;
4060 if (TREE_CODE (expr) == PSEUDO_DTOR_EXPR)
4062 error ("cannot apply %<offsetof%> to destructor %<~%T%>",
4063 TREE_OPERAND (expr, 2));
4064 return error_mark_node;
4066 if (TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE
4067 || TREE_CODE (TREE_TYPE (expr)) == METHOD_TYPE
4068 || TREE_TYPE (expr) == unknown_type_node)
4070 while (TREE_CODE (expr) == COMPONENT_REF
4071 || TREE_CODE (expr) == COMPOUND_EXPR)
4072 expr = TREE_OPERAND (expr, 1);
4074 if (DECL_P (expr))
4076 error ("cannot apply %<offsetof%> to member function %qD", expr);
4077 inform (DECL_SOURCE_LOCATION (expr), "declared here");
4079 else
4080 error ("cannot apply %<offsetof%> to member function");
4081 return error_mark_node;
4083 if (TREE_CODE (expr) == CONST_DECL)
4085 error ("cannot apply %<offsetof%> to an enumerator %qD", expr);
4086 return error_mark_node;
4088 if (REFERENCE_REF_P (expr))
4089 expr = TREE_OPERAND (expr, 0);
4090 if (!complete_type_or_else (TREE_TYPE (TREE_TYPE (object_ptr)), object_ptr))
4091 return error_mark_node;
4092 if (warn_invalid_offsetof
4093 && CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (object_ptr)))
4094 && CLASSTYPE_NON_STD_LAYOUT (TREE_TYPE (TREE_TYPE (object_ptr)))
4095 && cp_unevaluated_operand == 0)
4096 warning_at (loc, OPT_Winvalid_offsetof, "offsetof within "
4097 "non-standard-layout type %qT is conditionally-supported",
4098 TREE_TYPE (TREE_TYPE (object_ptr)));
4099 return fold_offsetof (expr);
4102 /* Replace the AGGR_INIT_EXPR at *TP with an equivalent CALL_EXPR. This
4103 function is broken out from the above for the benefit of the tree-ssa
4104 project. */
4106 void
4107 simplify_aggr_init_expr (tree *tp)
4109 tree aggr_init_expr = *tp;
4111 /* Form an appropriate CALL_EXPR. */
4112 tree fn = AGGR_INIT_EXPR_FN (aggr_init_expr);
4113 tree slot = AGGR_INIT_EXPR_SLOT (aggr_init_expr);
4114 tree type = TREE_TYPE (slot);
4116 tree call_expr;
4117 enum style_t { ctor, arg, pcc } style;
4119 if (AGGR_INIT_VIA_CTOR_P (aggr_init_expr))
4120 style = ctor;
4121 #ifdef PCC_STATIC_STRUCT_RETURN
4122 else if (1)
4123 style = pcc;
4124 #endif
4125 else
4127 gcc_assert (TREE_ADDRESSABLE (type));
4128 style = arg;
4131 call_expr = build_call_array_loc (input_location,
4132 TREE_TYPE (TREE_TYPE (TREE_TYPE (fn))),
4134 aggr_init_expr_nargs (aggr_init_expr),
4135 AGGR_INIT_EXPR_ARGP (aggr_init_expr));
4136 TREE_NOTHROW (call_expr) = TREE_NOTHROW (aggr_init_expr);
4137 CALL_FROM_THUNK_P (call_expr) = AGGR_INIT_FROM_THUNK_P (aggr_init_expr);
4138 CALL_EXPR_OPERATOR_SYNTAX (call_expr)
4139 = CALL_EXPR_OPERATOR_SYNTAX (aggr_init_expr);
4140 CALL_EXPR_ORDERED_ARGS (call_expr) = CALL_EXPR_ORDERED_ARGS (aggr_init_expr);
4141 CALL_EXPR_REVERSE_ARGS (call_expr) = CALL_EXPR_REVERSE_ARGS (aggr_init_expr);
4143 if (style == ctor)
4145 /* Replace the first argument to the ctor with the address of the
4146 slot. */
4147 cxx_mark_addressable (slot);
4148 CALL_EXPR_ARG (call_expr, 0) =
4149 build1 (ADDR_EXPR, build_pointer_type (type), slot);
4151 else if (style == arg)
4153 /* Just mark it addressable here, and leave the rest to
4154 expand_call{,_inline}. */
4155 cxx_mark_addressable (slot);
4156 CALL_EXPR_RETURN_SLOT_OPT (call_expr) = true;
4157 call_expr = build2 (INIT_EXPR, TREE_TYPE (call_expr), slot, call_expr);
4159 else if (style == pcc)
4161 /* If we're using the non-reentrant PCC calling convention, then we
4162 need to copy the returned value out of the static buffer into the
4163 SLOT. */
4164 push_deferring_access_checks (dk_no_check);
4165 call_expr = build_aggr_init (slot, call_expr,
4166 DIRECT_BIND | LOOKUP_ONLYCONVERTING,
4167 tf_warning_or_error);
4168 pop_deferring_access_checks ();
4169 call_expr = build2 (COMPOUND_EXPR, TREE_TYPE (slot), call_expr, slot);
4172 if (AGGR_INIT_ZERO_FIRST (aggr_init_expr))
4174 tree init = build_zero_init (type, NULL_TREE,
4175 /*static_storage_p=*/false);
4176 init = build2 (INIT_EXPR, void_type_node, slot, init);
4177 call_expr = build2 (COMPOUND_EXPR, TREE_TYPE (call_expr),
4178 init, call_expr);
4181 *tp = call_expr;
4184 /* Emit all thunks to FN that should be emitted when FN is emitted. */
4186 void
4187 emit_associated_thunks (tree fn)
4189 /* When we use vcall offsets, we emit thunks with the virtual
4190 functions to which they thunk. The whole point of vcall offsets
4191 is so that you can know statically the entire set of thunks that
4192 will ever be needed for a given virtual function, thereby
4193 enabling you to output all the thunks with the function itself. */
4194 if (DECL_VIRTUAL_P (fn)
4195 /* Do not emit thunks for extern template instantiations. */
4196 && ! DECL_REALLY_EXTERN (fn))
4198 tree thunk;
4200 for (thunk = DECL_THUNKS (fn); thunk; thunk = DECL_CHAIN (thunk))
4202 if (!THUNK_ALIAS (thunk))
4204 use_thunk (thunk, /*emit_p=*/1);
4205 if (DECL_RESULT_THUNK_P (thunk))
4207 tree probe;
4209 for (probe = DECL_THUNKS (thunk);
4210 probe; probe = DECL_CHAIN (probe))
4211 use_thunk (probe, /*emit_p=*/1);
4214 else
4215 gcc_assert (!DECL_THUNKS (thunk));
4220 /* Generate RTL for FN. */
4222 bool
4223 expand_or_defer_fn_1 (tree fn)
4225 /* When the parser calls us after finishing the body of a template
4226 function, we don't really want to expand the body. */
4227 if (processing_template_decl)
4229 /* Normally, collection only occurs in rest_of_compilation. So,
4230 if we don't collect here, we never collect junk generated
4231 during the processing of templates until we hit a
4232 non-template function. It's not safe to do this inside a
4233 nested class, though, as the parser may have local state that
4234 is not a GC root. */
4235 if (!function_depth)
4236 ggc_collect ();
4237 return false;
4240 gcc_assert (DECL_SAVED_TREE (fn));
4242 /* We make a decision about linkage for these functions at the end
4243 of the compilation. Until that point, we do not want the back
4244 end to output them -- but we do want it to see the bodies of
4245 these functions so that it can inline them as appropriate. */
4246 if (DECL_DECLARED_INLINE_P (fn) || DECL_IMPLICIT_INSTANTIATION (fn))
4248 if (DECL_INTERFACE_KNOWN (fn))
4249 /* We've already made a decision as to how this function will
4250 be handled. */;
4251 else if (!at_eof)
4252 tentative_decl_linkage (fn);
4253 else
4254 import_export_decl (fn);
4256 /* If the user wants us to keep all inline functions, then mark
4257 this function as needed so that finish_file will make sure to
4258 output it later. Similarly, all dllexport'd functions must
4259 be emitted; there may be callers in other DLLs. */
4260 if (DECL_DECLARED_INLINE_P (fn)
4261 && !DECL_REALLY_EXTERN (fn)
4262 && (flag_keep_inline_functions
4263 || (flag_keep_inline_dllexport
4264 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (fn)))))
4266 mark_needed (fn);
4267 DECL_EXTERNAL (fn) = 0;
4271 /* If this is a constructor or destructor body, we have to clone
4272 it. */
4273 if (maybe_clone_body (fn))
4275 /* We don't want to process FN again, so pretend we've written
4276 it out, even though we haven't. */
4277 TREE_ASM_WRITTEN (fn) = 1;
4278 /* If this is a constexpr function, keep DECL_SAVED_TREE. */
4279 if (!DECL_DECLARED_CONSTEXPR_P (fn))
4280 DECL_SAVED_TREE (fn) = NULL_TREE;
4281 return false;
4284 /* There's no reason to do any of the work here if we're only doing
4285 semantic analysis; this code just generates RTL. */
4286 if (flag_syntax_only)
4287 return false;
4289 return true;
4292 void
4293 expand_or_defer_fn (tree fn)
4295 if (expand_or_defer_fn_1 (fn))
4297 function_depth++;
4299 /* Expand or defer, at the whim of the compilation unit manager. */
4300 cgraph_node::finalize_function (fn, function_depth > 1);
4301 emit_associated_thunks (fn);
4303 function_depth--;
4307 struct nrv_data
4309 nrv_data () : visited (37) {}
4311 tree var;
4312 tree result;
4313 hash_table<nofree_ptr_hash <tree_node> > visited;
4316 /* Helper function for walk_tree, used by finalize_nrv below. */
4318 static tree
4319 finalize_nrv_r (tree* tp, int* walk_subtrees, void* data)
4321 struct nrv_data *dp = (struct nrv_data *)data;
4322 tree_node **slot;
4324 /* No need to walk into types. There wouldn't be any need to walk into
4325 non-statements, except that we have to consider STMT_EXPRs. */
4326 if (TYPE_P (*tp))
4327 *walk_subtrees = 0;
4328 /* Change all returns to just refer to the RESULT_DECL; this is a nop,
4329 but differs from using NULL_TREE in that it indicates that we care
4330 about the value of the RESULT_DECL. */
4331 else if (TREE_CODE (*tp) == RETURN_EXPR)
4332 TREE_OPERAND (*tp, 0) = dp->result;
4333 /* Change all cleanups for the NRV to only run when an exception is
4334 thrown. */
4335 else if (TREE_CODE (*tp) == CLEANUP_STMT
4336 && CLEANUP_DECL (*tp) == dp->var)
4337 CLEANUP_EH_ONLY (*tp) = 1;
4338 /* Replace the DECL_EXPR for the NRV with an initialization of the
4339 RESULT_DECL, if needed. */
4340 else if (TREE_CODE (*tp) == DECL_EXPR
4341 && DECL_EXPR_DECL (*tp) == dp->var)
4343 tree init;
4344 if (DECL_INITIAL (dp->var)
4345 && DECL_INITIAL (dp->var) != error_mark_node)
4346 init = build2 (INIT_EXPR, void_type_node, dp->result,
4347 DECL_INITIAL (dp->var));
4348 else
4349 init = build_empty_stmt (EXPR_LOCATION (*tp));
4350 DECL_INITIAL (dp->var) = NULL_TREE;
4351 SET_EXPR_LOCATION (init, EXPR_LOCATION (*tp));
4352 *tp = init;
4354 /* And replace all uses of the NRV with the RESULT_DECL. */
4355 else if (*tp == dp->var)
4356 *tp = dp->result;
4358 /* Avoid walking into the same tree more than once. Unfortunately, we
4359 can't just use walk_tree_without duplicates because it would only call
4360 us for the first occurrence of dp->var in the function body. */
4361 slot = dp->visited.find_slot (*tp, INSERT);
4362 if (*slot)
4363 *walk_subtrees = 0;
4364 else
4365 *slot = *tp;
4367 /* Keep iterating. */
4368 return NULL_TREE;
4371 /* Called from finish_function to implement the named return value
4372 optimization by overriding all the RETURN_EXPRs and pertinent
4373 CLEANUP_STMTs and replacing all occurrences of VAR with RESULT, the
4374 RESULT_DECL for the function. */
4376 void
4377 finalize_nrv (tree *tp, tree var, tree result)
4379 struct nrv_data data;
4381 /* Copy name from VAR to RESULT. */
4382 DECL_NAME (result) = DECL_NAME (var);
4383 /* Don't forget that we take its address. */
4384 TREE_ADDRESSABLE (result) = TREE_ADDRESSABLE (var);
4385 /* Finally set DECL_VALUE_EXPR to avoid assigning
4386 a stack slot at -O0 for the original var and debug info
4387 uses RESULT location for VAR. */
4388 SET_DECL_VALUE_EXPR (var, result);
4389 DECL_HAS_VALUE_EXPR_P (var) = 1;
4391 data.var = var;
4392 data.result = result;
4393 cp_walk_tree (tp, finalize_nrv_r, &data, 0);
4396 /* Create CP_OMP_CLAUSE_INFO for clause C. Returns true if it is invalid. */
4398 bool
4399 cxx_omp_create_clause_info (tree c, tree type, bool need_default_ctor,
4400 bool need_copy_ctor, bool need_copy_assignment,
4401 bool need_dtor)
4403 int save_errorcount = errorcount;
4404 tree info, t;
4406 /* Always allocate 3 elements for simplicity. These are the
4407 function decls for the ctor, dtor, and assignment op.
4408 This layout is known to the three lang hooks,
4409 cxx_omp_clause_default_init, cxx_omp_clause_copy_init,
4410 and cxx_omp_clause_assign_op. */
4411 info = make_tree_vec (3);
4412 CP_OMP_CLAUSE_INFO (c) = info;
4414 if (need_default_ctor || need_copy_ctor)
4416 if (need_default_ctor)
4417 t = get_default_ctor (type);
4418 else
4419 t = get_copy_ctor (type, tf_warning_or_error);
4421 if (t && !trivial_fn_p (t))
4422 TREE_VEC_ELT (info, 0) = t;
4425 if (need_dtor && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
4426 TREE_VEC_ELT (info, 1) = get_dtor (type, tf_warning_or_error);
4428 if (need_copy_assignment)
4430 t = get_copy_assign (type);
4432 if (t && !trivial_fn_p (t))
4433 TREE_VEC_ELT (info, 2) = t;
4436 return errorcount != save_errorcount;
4439 /* If DECL is DECL_OMP_PRIVATIZED_MEMBER, return corresponding
4440 FIELD_DECL, otherwise return DECL itself. */
4442 static tree
4443 omp_clause_decl_field (tree decl)
4445 if (VAR_P (decl)
4446 && DECL_HAS_VALUE_EXPR_P (decl)
4447 && DECL_ARTIFICIAL (decl)
4448 && DECL_LANG_SPECIFIC (decl)
4449 && DECL_OMP_PRIVATIZED_MEMBER (decl))
4451 tree f = DECL_VALUE_EXPR (decl);
4452 if (INDIRECT_REF_P (f))
4453 f = TREE_OPERAND (f, 0);
4454 if (TREE_CODE (f) == COMPONENT_REF)
4456 f = TREE_OPERAND (f, 1);
4457 gcc_assert (TREE_CODE (f) == FIELD_DECL);
4458 return f;
4461 return NULL_TREE;
4464 /* Adjust DECL if needed for printing using %qE. */
4466 static tree
4467 omp_clause_printable_decl (tree decl)
4469 tree t = omp_clause_decl_field (decl);
4470 if (t)
4471 return t;
4472 return decl;
4475 /* For a FIELD_DECL F and corresponding DECL_OMP_PRIVATIZED_MEMBER
4476 VAR_DECL T that doesn't need a DECL_EXPR added, record it for
4477 privatization. */
4479 static void
4480 omp_note_field_privatization (tree f, tree t)
4482 if (!omp_private_member_map)
4483 omp_private_member_map = new hash_map<tree, tree>;
4484 tree &v = omp_private_member_map->get_or_insert (f);
4485 if (v == NULL_TREE)
4487 v = t;
4488 omp_private_member_vec.safe_push (f);
4489 /* Signal that we don't want to create DECL_EXPR for this dummy var. */
4490 omp_private_member_vec.safe_push (integer_zero_node);
4494 /* Privatize FIELD_DECL T, return corresponding DECL_OMP_PRIVATIZED_MEMBER
4495 dummy VAR_DECL. */
4497 tree
4498 omp_privatize_field (tree t, bool shared)
4500 tree m = finish_non_static_data_member (t, NULL_TREE, NULL_TREE);
4501 if (m == error_mark_node)
4502 return error_mark_node;
4503 if (!omp_private_member_map && !shared)
4504 omp_private_member_map = new hash_map<tree, tree>;
4505 if (TYPE_REF_P (TREE_TYPE (t)))
4507 gcc_assert (INDIRECT_REF_P (m));
4508 m = TREE_OPERAND (m, 0);
4510 tree vb = NULL_TREE;
4511 tree &v = shared ? vb : omp_private_member_map->get_or_insert (t);
4512 if (v == NULL_TREE)
4514 v = create_temporary_var (TREE_TYPE (m));
4515 retrofit_lang_decl (v);
4516 DECL_OMP_PRIVATIZED_MEMBER (v) = 1;
4517 SET_DECL_VALUE_EXPR (v, m);
4518 DECL_HAS_VALUE_EXPR_P (v) = 1;
4519 if (!shared)
4520 omp_private_member_vec.safe_push (t);
4522 return v;
4525 /* Helper function for handle_omp_array_sections. Called recursively
4526 to handle multiple array-section-subscripts. C is the clause,
4527 T current expression (initially OMP_CLAUSE_DECL), which is either
4528 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
4529 expression if specified, TREE_VALUE length expression if specified,
4530 TREE_CHAIN is what it has been specified after, or some decl.
4531 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
4532 set to true if any of the array-section-subscript could have length
4533 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
4534 first array-section-subscript which is known not to have length
4535 of one. Given say:
4536 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
4537 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
4538 all are or may have length of 1, array-section-subscript [:2] is the
4539 first one known not to have length 1. For array-section-subscript
4540 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
4541 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
4542 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
4543 case though, as some lengths could be zero. */
4545 static tree
4546 handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
4547 bool &maybe_zero_len, unsigned int &first_non_one,
4548 enum c_omp_region_type ort)
4550 tree ret, low_bound, length, type;
4551 if (TREE_CODE (t) != TREE_LIST)
4553 if (error_operand_p (t))
4554 return error_mark_node;
4555 if (REFERENCE_REF_P (t)
4556 && TREE_CODE (TREE_OPERAND (t, 0)) == COMPONENT_REF)
4557 t = TREE_OPERAND (t, 0);
4558 ret = t;
4559 if (TREE_CODE (t) == COMPONENT_REF
4560 && ort == C_ORT_OMP
4561 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
4562 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO
4563 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FROM)
4564 && !type_dependent_expression_p (t))
4566 if (TREE_CODE (TREE_OPERAND (t, 1)) == FIELD_DECL
4567 && DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
4569 error_at (OMP_CLAUSE_LOCATION (c),
4570 "bit-field %qE in %qs clause",
4571 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4572 return error_mark_node;
4574 while (TREE_CODE (t) == COMPONENT_REF)
4576 if (TREE_TYPE (TREE_OPERAND (t, 0))
4577 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == UNION_TYPE)
4579 error_at (OMP_CLAUSE_LOCATION (c),
4580 "%qE is a member of a union", t);
4581 return error_mark_node;
4583 t = TREE_OPERAND (t, 0);
4585 if (REFERENCE_REF_P (t))
4586 t = TREE_OPERAND (t, 0);
4588 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
4590 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
4591 return NULL_TREE;
4592 if (DECL_P (t))
4593 error_at (OMP_CLAUSE_LOCATION (c),
4594 "%qD is not a variable in %qs clause", t,
4595 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4596 else
4597 error_at (OMP_CLAUSE_LOCATION (c),
4598 "%qE is not a variable in %qs clause", t,
4599 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4600 return error_mark_node;
4602 else if (TREE_CODE (t) == PARM_DECL
4603 && DECL_ARTIFICIAL (t)
4604 && DECL_NAME (t) == this_identifier)
4606 error_at (OMP_CLAUSE_LOCATION (c),
4607 "%<this%> allowed in OpenMP only in %<declare simd%>"
4608 " clauses");
4609 return error_mark_node;
4611 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
4612 && VAR_P (t) && CP_DECL_THREAD_LOCAL_P (t))
4614 error_at (OMP_CLAUSE_LOCATION (c),
4615 "%qD is threadprivate variable in %qs clause", t,
4616 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4617 return error_mark_node;
4619 if (type_dependent_expression_p (ret))
4620 return NULL_TREE;
4621 ret = convert_from_reference (ret);
4622 return ret;
4625 if (ort == C_ORT_OMP
4626 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
4627 && TREE_CODE (TREE_CHAIN (t)) == FIELD_DECL)
4628 TREE_CHAIN (t) = omp_privatize_field (TREE_CHAIN (t), false);
4629 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
4630 maybe_zero_len, first_non_one, ort);
4631 if (ret == error_mark_node || ret == NULL_TREE)
4632 return ret;
4634 type = TREE_TYPE (ret);
4635 low_bound = TREE_PURPOSE (t);
4636 length = TREE_VALUE (t);
4637 if ((low_bound && type_dependent_expression_p (low_bound))
4638 || (length && type_dependent_expression_p (length)))
4639 return NULL_TREE;
4641 if (low_bound == error_mark_node || length == error_mark_node)
4642 return error_mark_node;
4644 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
4646 error_at (OMP_CLAUSE_LOCATION (c),
4647 "low bound %qE of array section does not have integral type",
4648 low_bound);
4649 return error_mark_node;
4651 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
4653 error_at (OMP_CLAUSE_LOCATION (c),
4654 "length %qE of array section does not have integral type",
4655 length);
4656 return error_mark_node;
4658 if (low_bound)
4659 low_bound = mark_rvalue_use (low_bound);
4660 if (length)
4661 length = mark_rvalue_use (length);
4662 /* We need to reduce to real constant-values for checks below. */
4663 if (length)
4664 length = fold_simple (length);
4665 if (low_bound)
4666 low_bound = fold_simple (low_bound);
4667 if (low_bound
4668 && TREE_CODE (low_bound) == INTEGER_CST
4669 && TYPE_PRECISION (TREE_TYPE (low_bound))
4670 > TYPE_PRECISION (sizetype))
4671 low_bound = fold_convert (sizetype, low_bound);
4672 if (length
4673 && TREE_CODE (length) == INTEGER_CST
4674 && TYPE_PRECISION (TREE_TYPE (length))
4675 > TYPE_PRECISION (sizetype))
4676 length = fold_convert (sizetype, length);
4677 if (low_bound == NULL_TREE)
4678 low_bound = integer_zero_node;
4680 if (length != NULL_TREE)
4682 if (!integer_nonzerop (length))
4684 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
4685 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
4687 if (integer_zerop (length))
4689 error_at (OMP_CLAUSE_LOCATION (c),
4690 "zero length array section in %qs clause",
4691 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4692 return error_mark_node;
4695 else
4696 maybe_zero_len = true;
4698 if (first_non_one == types.length ()
4699 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
4700 first_non_one++;
4702 if (TREE_CODE (type) == ARRAY_TYPE)
4704 if (length == NULL_TREE
4705 && (TYPE_DOMAIN (type) == NULL_TREE
4706 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
4708 error_at (OMP_CLAUSE_LOCATION (c),
4709 "for unknown bound array type length expression must "
4710 "be specified");
4711 return error_mark_node;
4713 if (TREE_CODE (low_bound) == INTEGER_CST
4714 && tree_int_cst_sgn (low_bound) == -1)
4716 error_at (OMP_CLAUSE_LOCATION (c),
4717 "negative low bound in array section in %qs clause",
4718 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4719 return error_mark_node;
4721 if (length != NULL_TREE
4722 && TREE_CODE (length) == INTEGER_CST
4723 && tree_int_cst_sgn (length) == -1)
4725 error_at (OMP_CLAUSE_LOCATION (c),
4726 "negative length in array section in %qs clause",
4727 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4728 return error_mark_node;
4730 if (TYPE_DOMAIN (type)
4731 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
4732 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
4733 == INTEGER_CST)
4735 tree size
4736 = fold_convert (sizetype, TYPE_MAX_VALUE (TYPE_DOMAIN (type)));
4737 size = size_binop (PLUS_EXPR, size, size_one_node);
4738 if (TREE_CODE (low_bound) == INTEGER_CST)
4740 if (tree_int_cst_lt (size, low_bound))
4742 error_at (OMP_CLAUSE_LOCATION (c),
4743 "low bound %qE above array section size "
4744 "in %qs clause", low_bound,
4745 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4746 return error_mark_node;
4748 if (tree_int_cst_equal (size, low_bound))
4750 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
4751 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
4753 error_at (OMP_CLAUSE_LOCATION (c),
4754 "zero length array section in %qs clause",
4755 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4756 return error_mark_node;
4758 maybe_zero_len = true;
4760 else if (length == NULL_TREE
4761 && first_non_one == types.length ()
4762 && tree_int_cst_equal
4763 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
4764 low_bound))
4765 first_non_one++;
4767 else if (length == NULL_TREE)
4769 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
4770 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION)
4771 maybe_zero_len = true;
4772 if (first_non_one == types.length ())
4773 first_non_one++;
4775 if (length && TREE_CODE (length) == INTEGER_CST)
4777 if (tree_int_cst_lt (size, length))
4779 error_at (OMP_CLAUSE_LOCATION (c),
4780 "length %qE above array section size "
4781 "in %qs clause", length,
4782 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4783 return error_mark_node;
4785 if (TREE_CODE (low_bound) == INTEGER_CST)
4787 tree lbpluslen
4788 = size_binop (PLUS_EXPR,
4789 fold_convert (sizetype, low_bound),
4790 fold_convert (sizetype, length));
4791 if (TREE_CODE (lbpluslen) == INTEGER_CST
4792 && tree_int_cst_lt (size, lbpluslen))
4794 error_at (OMP_CLAUSE_LOCATION (c),
4795 "high bound %qE above array section size "
4796 "in %qs clause", lbpluslen,
4797 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4798 return error_mark_node;
4803 else if (length == NULL_TREE)
4805 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
4806 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION)
4807 maybe_zero_len = true;
4808 if (first_non_one == types.length ())
4809 first_non_one++;
4812 /* For [lb:] we will need to evaluate lb more than once. */
4813 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
4815 tree lb = cp_save_expr (low_bound);
4816 if (lb != low_bound)
4818 TREE_PURPOSE (t) = lb;
4819 low_bound = lb;
4823 else if (TYPE_PTR_P (type))
4825 if (length == NULL_TREE)
4827 error_at (OMP_CLAUSE_LOCATION (c),
4828 "for pointer type length expression must be specified");
4829 return error_mark_node;
4831 if (length != NULL_TREE
4832 && TREE_CODE (length) == INTEGER_CST
4833 && tree_int_cst_sgn (length) == -1)
4835 error_at (OMP_CLAUSE_LOCATION (c),
4836 "negative length in array section in %qs clause",
4837 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4838 return error_mark_node;
4840 /* If there is a pointer type anywhere but in the very first
4841 array-section-subscript, the array section can't be contiguous. */
4842 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
4843 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
4845 error_at (OMP_CLAUSE_LOCATION (c),
4846 "array section is not contiguous in %qs clause",
4847 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4848 return error_mark_node;
4851 else
4853 error_at (OMP_CLAUSE_LOCATION (c),
4854 "%qE does not have pointer or array type", ret);
4855 return error_mark_node;
4857 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
4858 types.safe_push (TREE_TYPE (ret));
4859 /* We will need to evaluate lb more than once. */
4860 tree lb = cp_save_expr (low_bound);
4861 if (lb != low_bound)
4863 TREE_PURPOSE (t) = lb;
4864 low_bound = lb;
4866 ret = grok_array_decl (OMP_CLAUSE_LOCATION (c), ret, low_bound, false);
4867 return ret;
4870 /* Handle array sections for clause C. */
4872 static bool
4873 handle_omp_array_sections (tree c, enum c_omp_region_type ort)
4875 bool maybe_zero_len = false;
4876 unsigned int first_non_one = 0;
4877 auto_vec<tree, 10> types;
4878 tree first = handle_omp_array_sections_1 (c, OMP_CLAUSE_DECL (c), types,
4879 maybe_zero_len, first_non_one,
4880 ort);
4881 if (first == error_mark_node)
4882 return true;
4883 if (first == NULL_TREE)
4884 return false;
4885 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
4887 tree t = OMP_CLAUSE_DECL (c);
4888 tree tem = NULL_TREE;
4889 if (processing_template_decl)
4890 return false;
4891 /* Need to evaluate side effects in the length expressions
4892 if any. */
4893 while (TREE_CODE (t) == TREE_LIST)
4895 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
4897 if (tem == NULL_TREE)
4898 tem = TREE_VALUE (t);
4899 else
4900 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
4901 TREE_VALUE (t), tem);
4903 t = TREE_CHAIN (t);
4905 if (tem)
4906 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
4907 OMP_CLAUSE_DECL (c) = first;
4909 else
4911 unsigned int num = types.length (), i;
4912 tree t, side_effects = NULL_TREE, size = NULL_TREE;
4913 tree condition = NULL_TREE;
4915 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
4916 maybe_zero_len = true;
4917 if (processing_template_decl && maybe_zero_len)
4918 return false;
4920 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
4921 t = TREE_CHAIN (t))
4923 tree low_bound = TREE_PURPOSE (t);
4924 tree length = TREE_VALUE (t);
4926 i--;
4927 if (low_bound
4928 && TREE_CODE (low_bound) == INTEGER_CST
4929 && TYPE_PRECISION (TREE_TYPE (low_bound))
4930 > TYPE_PRECISION (sizetype))
4931 low_bound = fold_convert (sizetype, low_bound);
4932 if (length
4933 && TREE_CODE (length) == INTEGER_CST
4934 && TYPE_PRECISION (TREE_TYPE (length))
4935 > TYPE_PRECISION (sizetype))
4936 length = fold_convert (sizetype, length);
4937 if (low_bound == NULL_TREE)
4938 low_bound = integer_zero_node;
4939 if (!maybe_zero_len && i > first_non_one)
4941 if (integer_nonzerop (low_bound))
4942 goto do_warn_noncontiguous;
4943 if (length != NULL_TREE
4944 && TREE_CODE (length) == INTEGER_CST
4945 && TYPE_DOMAIN (types[i])
4946 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
4947 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
4948 == INTEGER_CST)
4950 tree size;
4951 size = size_binop (PLUS_EXPR,
4952 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
4953 size_one_node);
4954 if (!tree_int_cst_equal (length, size))
4956 do_warn_noncontiguous:
4957 error_at (OMP_CLAUSE_LOCATION (c),
4958 "array section is not contiguous in %qs "
4959 "clause",
4960 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4961 return true;
4964 if (!processing_template_decl
4965 && length != NULL_TREE
4966 && TREE_SIDE_EFFECTS (length))
4968 if (side_effects == NULL_TREE)
4969 side_effects = length;
4970 else
4971 side_effects = build2 (COMPOUND_EXPR,
4972 TREE_TYPE (side_effects),
4973 length, side_effects);
4976 else if (processing_template_decl)
4977 continue;
4978 else
4980 tree l;
4982 if (i > first_non_one
4983 && ((length && integer_nonzerop (length))
4984 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION))
4985 continue;
4986 if (length)
4987 l = fold_convert (sizetype, length);
4988 else
4990 l = size_binop (PLUS_EXPR,
4991 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
4992 size_one_node);
4993 l = size_binop (MINUS_EXPR, l,
4994 fold_convert (sizetype, low_bound));
4996 if (i > first_non_one)
4998 l = fold_build2 (NE_EXPR, boolean_type_node, l,
4999 size_zero_node);
5000 if (condition == NULL_TREE)
5001 condition = l;
5002 else
5003 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
5004 l, condition);
5006 else if (size == NULL_TREE)
5008 size = size_in_bytes (TREE_TYPE (types[i]));
5009 tree eltype = TREE_TYPE (types[num - 1]);
5010 while (TREE_CODE (eltype) == ARRAY_TYPE)
5011 eltype = TREE_TYPE (eltype);
5012 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
5013 size = size_binop (EXACT_DIV_EXPR, size,
5014 size_in_bytes (eltype));
5015 size = size_binop (MULT_EXPR, size, l);
5016 if (condition)
5017 size = fold_build3 (COND_EXPR, sizetype, condition,
5018 size, size_zero_node);
5020 else
5021 size = size_binop (MULT_EXPR, size, l);
5024 if (!processing_template_decl)
5026 if (side_effects)
5027 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
5028 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
5030 size = size_binop (MINUS_EXPR, size, size_one_node);
5031 tree index_type = build_index_type (size);
5032 tree eltype = TREE_TYPE (first);
5033 while (TREE_CODE (eltype) == ARRAY_TYPE)
5034 eltype = TREE_TYPE (eltype);
5035 tree type = build_array_type (eltype, index_type);
5036 tree ptype = build_pointer_type (eltype);
5037 if (TYPE_REF_P (TREE_TYPE (t))
5038 && INDIRECT_TYPE_P (TREE_TYPE (TREE_TYPE (t))))
5039 t = convert_from_reference (t);
5040 else if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
5041 t = build_fold_addr_expr (t);
5042 tree t2 = build_fold_addr_expr (first);
5043 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
5044 ptrdiff_type_node, t2);
5045 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
5046 ptrdiff_type_node, t2,
5047 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
5048 ptrdiff_type_node, t));
5049 if (tree_fits_shwi_p (t2))
5050 t = build2 (MEM_REF, type, t,
5051 build_int_cst (ptype, tree_to_shwi (t2)));
5052 else
5054 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
5055 sizetype, t2);
5056 t = build2_loc (OMP_CLAUSE_LOCATION (c), POINTER_PLUS_EXPR,
5057 TREE_TYPE (t), t, t2);
5058 t = build2 (MEM_REF, type, t, build_int_cst (ptype, 0));
5060 OMP_CLAUSE_DECL (c) = t;
5061 return false;
5063 OMP_CLAUSE_DECL (c) = first;
5064 OMP_CLAUSE_SIZE (c) = size;
5065 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
5066 || (TREE_CODE (t) == COMPONENT_REF
5067 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE))
5068 return false;
5069 if (ort == C_ORT_OMP || ort == C_ORT_ACC)
5070 switch (OMP_CLAUSE_MAP_KIND (c))
5072 case GOMP_MAP_ALLOC:
5073 case GOMP_MAP_TO:
5074 case GOMP_MAP_FROM:
5075 case GOMP_MAP_TOFROM:
5076 case GOMP_MAP_ALWAYS_TO:
5077 case GOMP_MAP_ALWAYS_FROM:
5078 case GOMP_MAP_ALWAYS_TOFROM:
5079 case GOMP_MAP_RELEASE:
5080 case GOMP_MAP_DELETE:
5081 case GOMP_MAP_FORCE_TO:
5082 case GOMP_MAP_FORCE_FROM:
5083 case GOMP_MAP_FORCE_TOFROM:
5084 case GOMP_MAP_FORCE_PRESENT:
5085 OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (c) = 1;
5086 break;
5087 default:
5088 break;
5090 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
5091 OMP_CLAUSE_MAP);
5092 if ((ort & C_ORT_OMP_DECLARE_SIMD) != C_ORT_OMP && ort != C_ORT_ACC)
5093 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_POINTER);
5094 else if (TREE_CODE (t) == COMPONENT_REF)
5095 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ALWAYS_POINTER);
5096 else if (REFERENCE_REF_P (t)
5097 && TREE_CODE (TREE_OPERAND (t, 0)) == COMPONENT_REF)
5099 t = TREE_OPERAND (t, 0);
5100 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ALWAYS_POINTER);
5102 else
5103 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_FIRSTPRIVATE_POINTER);
5104 if (OMP_CLAUSE_MAP_KIND (c2) != GOMP_MAP_FIRSTPRIVATE_POINTER
5105 && !cxx_mark_addressable (t))
5106 return false;
5107 OMP_CLAUSE_DECL (c2) = t;
5108 t = build_fold_addr_expr (first);
5109 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
5110 ptrdiff_type_node, t);
5111 tree ptr = OMP_CLAUSE_DECL (c2);
5112 ptr = convert_from_reference (ptr);
5113 if (!INDIRECT_TYPE_P (TREE_TYPE (ptr)))
5114 ptr = build_fold_addr_expr (ptr);
5115 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
5116 ptrdiff_type_node, t,
5117 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
5118 ptrdiff_type_node, ptr));
5119 OMP_CLAUSE_SIZE (c2) = t;
5120 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
5121 OMP_CLAUSE_CHAIN (c) = c2;
5122 ptr = OMP_CLAUSE_DECL (c2);
5123 if (OMP_CLAUSE_MAP_KIND (c2) != GOMP_MAP_FIRSTPRIVATE_POINTER
5124 && TYPE_REF_P (TREE_TYPE (ptr))
5125 && INDIRECT_TYPE_P (TREE_TYPE (TREE_TYPE (ptr))))
5127 tree c3 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
5128 OMP_CLAUSE_MAP);
5129 OMP_CLAUSE_SET_MAP_KIND (c3, OMP_CLAUSE_MAP_KIND (c2));
5130 OMP_CLAUSE_DECL (c3) = ptr;
5131 if (OMP_CLAUSE_MAP_KIND (c2) == GOMP_MAP_ALWAYS_POINTER)
5132 OMP_CLAUSE_DECL (c2) = build_simple_mem_ref (ptr);
5133 else
5134 OMP_CLAUSE_DECL (c2) = convert_from_reference (ptr);
5135 OMP_CLAUSE_SIZE (c3) = size_zero_node;
5136 OMP_CLAUSE_CHAIN (c3) = OMP_CLAUSE_CHAIN (c2);
5137 OMP_CLAUSE_CHAIN (c2) = c3;
5141 return false;
5144 /* Return identifier to look up for omp declare reduction. */
5146 tree
5147 omp_reduction_id (enum tree_code reduction_code, tree reduction_id, tree type)
5149 const char *p = NULL;
5150 const char *m = NULL;
5151 switch (reduction_code)
5153 case PLUS_EXPR:
5154 case MULT_EXPR:
5155 case MINUS_EXPR:
5156 case BIT_AND_EXPR:
5157 case BIT_XOR_EXPR:
5158 case BIT_IOR_EXPR:
5159 case TRUTH_ANDIF_EXPR:
5160 case TRUTH_ORIF_EXPR:
5161 reduction_id = ovl_op_identifier (false, reduction_code);
5162 break;
5163 case MIN_EXPR:
5164 p = "min";
5165 break;
5166 case MAX_EXPR:
5167 p = "max";
5168 break;
5169 default:
5170 break;
5173 if (p == NULL)
5175 if (TREE_CODE (reduction_id) != IDENTIFIER_NODE)
5176 return error_mark_node;
5177 p = IDENTIFIER_POINTER (reduction_id);
5180 if (type != NULL_TREE)
5181 m = mangle_type_string (TYPE_MAIN_VARIANT (type));
5183 const char prefix[] = "omp declare reduction ";
5184 size_t lenp = sizeof (prefix);
5185 if (strncmp (p, prefix, lenp - 1) == 0)
5186 lenp = 1;
5187 size_t len = strlen (p);
5188 size_t lenm = m ? strlen (m) + 1 : 0;
5189 char *name = XALLOCAVEC (char, lenp + len + lenm);
5190 if (lenp > 1)
5191 memcpy (name, prefix, lenp - 1);
5192 memcpy (name + lenp - 1, p, len + 1);
5193 if (m)
5195 name[lenp + len - 1] = '~';
5196 memcpy (name + lenp + len, m, lenm);
5198 return get_identifier (name);
5201 /* Lookup OpenMP UDR ID for TYPE, return the corresponding artificial
5202 FUNCTION_DECL or NULL_TREE if not found. */
5204 static tree
5205 omp_reduction_lookup (location_t loc, tree id, tree type, tree *baselinkp,
5206 vec<tree> *ambiguousp)
5208 tree orig_id = id;
5209 tree baselink = NULL_TREE;
5210 if (identifier_p (id))
5212 cp_id_kind idk;
5213 bool nonint_cst_expression_p;
5214 const char *error_msg;
5215 id = omp_reduction_id (ERROR_MARK, id, type);
5216 tree decl = lookup_name (id);
5217 if (decl == NULL_TREE)
5218 decl = error_mark_node;
5219 id = finish_id_expression (id, decl, NULL_TREE, &idk, false, true,
5220 &nonint_cst_expression_p, false, true, false,
5221 false, &error_msg, loc);
5222 if (idk == CP_ID_KIND_UNQUALIFIED
5223 && identifier_p (id))
5225 vec<tree, va_gc> *args = NULL;
5226 vec_safe_push (args, build_reference_type (type));
5227 id = perform_koenig_lookup (id, args, tf_none);
5230 else if (TREE_CODE (id) == SCOPE_REF)
5231 id = lookup_qualified_name (TREE_OPERAND (id, 0),
5232 omp_reduction_id (ERROR_MARK,
5233 TREE_OPERAND (id, 1),
5234 type),
5235 false, false);
5236 tree fns = id;
5237 id = NULL_TREE;
5238 if (fns && is_overloaded_fn (fns))
5240 for (lkp_iterator iter (get_fns (fns)); iter; ++iter)
5242 tree fndecl = *iter;
5243 if (TREE_CODE (fndecl) == FUNCTION_DECL)
5245 tree argtype = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
5246 if (same_type_p (TREE_TYPE (argtype), type))
5248 id = fndecl;
5249 break;
5254 if (id && BASELINK_P (fns))
5256 if (baselinkp)
5257 *baselinkp = fns;
5258 else
5259 baselink = fns;
5263 if (!id && CLASS_TYPE_P (type) && TYPE_BINFO (type))
5265 vec<tree> ambiguous = vNULL;
5266 tree binfo = TYPE_BINFO (type), base_binfo, ret = NULL_TREE;
5267 unsigned int ix;
5268 if (ambiguousp == NULL)
5269 ambiguousp = &ambiguous;
5270 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
5272 id = omp_reduction_lookup (loc, orig_id, BINFO_TYPE (base_binfo),
5273 baselinkp ? baselinkp : &baselink,
5274 ambiguousp);
5275 if (id == NULL_TREE)
5276 continue;
5277 if (!ambiguousp->is_empty ())
5278 ambiguousp->safe_push (id);
5279 else if (ret != NULL_TREE)
5281 ambiguousp->safe_push (ret);
5282 ambiguousp->safe_push (id);
5283 ret = NULL_TREE;
5285 else
5286 ret = id;
5288 if (ambiguousp != &ambiguous)
5289 return ret;
5290 if (!ambiguous.is_empty ())
5292 const char *str = _("candidates are:");
5293 unsigned int idx;
5294 tree udr;
5295 error_at (loc, "user defined reduction lookup is ambiguous");
5296 FOR_EACH_VEC_ELT (ambiguous, idx, udr)
5298 inform (DECL_SOURCE_LOCATION (udr), "%s %#qD", str, udr);
5299 if (idx == 0)
5300 str = get_spaces (str);
5302 ambiguous.release ();
5303 ret = error_mark_node;
5304 baselink = NULL_TREE;
5306 id = ret;
5308 if (id && baselink)
5309 perform_or_defer_access_check (BASELINK_BINFO (baselink),
5310 id, id, tf_warning_or_error);
5311 return id;
5314 /* Helper function for cp_parser_omp_declare_reduction_exprs
5315 and tsubst_omp_udr.
5316 Remove CLEANUP_STMT for data (omp_priv variable).
5317 Also append INIT_EXPR for DECL_INITIAL of omp_priv after its
5318 DECL_EXPR. */
5320 tree
5321 cp_remove_omp_priv_cleanup_stmt (tree *tp, int *walk_subtrees, void *data)
5323 if (TYPE_P (*tp))
5324 *walk_subtrees = 0;
5325 else if (TREE_CODE (*tp) == CLEANUP_STMT && CLEANUP_DECL (*tp) == (tree) data)
5326 *tp = CLEANUP_BODY (*tp);
5327 else if (TREE_CODE (*tp) == DECL_EXPR)
5329 tree decl = DECL_EXPR_DECL (*tp);
5330 if (!processing_template_decl
5331 && decl == (tree) data
5332 && DECL_INITIAL (decl)
5333 && DECL_INITIAL (decl) != error_mark_node)
5335 tree list = NULL_TREE;
5336 append_to_statement_list_force (*tp, &list);
5337 tree init_expr = build2 (INIT_EXPR, void_type_node,
5338 decl, DECL_INITIAL (decl));
5339 DECL_INITIAL (decl) = NULL_TREE;
5340 append_to_statement_list_force (init_expr, &list);
5341 *tp = list;
5344 return NULL_TREE;
5347 /* Data passed from cp_check_omp_declare_reduction to
5348 cp_check_omp_declare_reduction_r. */
5350 struct cp_check_omp_declare_reduction_data
5352 location_t loc;
5353 tree stmts[7];
5354 bool combiner_p;
5357 /* Helper function for cp_check_omp_declare_reduction, called via
5358 cp_walk_tree. */
5360 static tree
5361 cp_check_omp_declare_reduction_r (tree *tp, int *, void *data)
5363 struct cp_check_omp_declare_reduction_data *udr_data
5364 = (struct cp_check_omp_declare_reduction_data *) data;
5365 if (SSA_VAR_P (*tp)
5366 && !DECL_ARTIFICIAL (*tp)
5367 && *tp != DECL_EXPR_DECL (udr_data->stmts[udr_data->combiner_p ? 0 : 3])
5368 && *tp != DECL_EXPR_DECL (udr_data->stmts[udr_data->combiner_p ? 1 : 4]))
5370 location_t loc = udr_data->loc;
5371 if (udr_data->combiner_p)
5372 error_at (loc, "%<#pragma omp declare reduction%> combiner refers to "
5373 "variable %qD which is not %<omp_out%> nor %<omp_in%>",
5374 *tp);
5375 else
5376 error_at (loc, "%<#pragma omp declare reduction%> initializer refers "
5377 "to variable %qD which is not %<omp_priv%> nor "
5378 "%<omp_orig%>",
5379 *tp);
5380 return *tp;
5382 return NULL_TREE;
5385 /* Diagnose violation of OpenMP #pragma omp declare reduction restrictions. */
5387 void
5388 cp_check_omp_declare_reduction (tree udr)
5390 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (udr)));
5391 gcc_assert (TYPE_REF_P (type));
5392 type = TREE_TYPE (type);
5393 int i;
5394 location_t loc = DECL_SOURCE_LOCATION (udr);
5396 if (type == error_mark_node)
5397 return;
5398 if (ARITHMETIC_TYPE_P (type))
5400 static enum tree_code predef_codes[]
5401 = { PLUS_EXPR, MULT_EXPR, MINUS_EXPR, BIT_AND_EXPR, BIT_XOR_EXPR,
5402 BIT_IOR_EXPR, TRUTH_ANDIF_EXPR, TRUTH_ORIF_EXPR };
5403 for (i = 0; i < 8; i++)
5405 tree id = omp_reduction_id (predef_codes[i], NULL_TREE, NULL_TREE);
5406 const char *n1 = IDENTIFIER_POINTER (DECL_NAME (udr));
5407 const char *n2 = IDENTIFIER_POINTER (id);
5408 if (strncmp (n1, n2, IDENTIFIER_LENGTH (id)) == 0
5409 && (n1[IDENTIFIER_LENGTH (id)] == '~'
5410 || n1[IDENTIFIER_LENGTH (id)] == '\0'))
5411 break;
5414 if (i == 8
5415 && TREE_CODE (type) != COMPLEX_EXPR)
5417 const char prefix_minmax[] = "omp declare reduction m";
5418 size_t prefix_size = sizeof (prefix_minmax) - 1;
5419 const char *n = IDENTIFIER_POINTER (DECL_NAME (udr));
5420 if (strncmp (IDENTIFIER_POINTER (DECL_NAME (udr)),
5421 prefix_minmax, prefix_size) == 0
5422 && ((n[prefix_size] == 'i' && n[prefix_size + 1] == 'n')
5423 || (n[prefix_size] == 'a' && n[prefix_size + 1] == 'x'))
5424 && (n[prefix_size + 2] == '~' || n[prefix_size + 2] == '\0'))
5425 i = 0;
5427 if (i < 8)
5429 error_at (loc, "predeclared arithmetic type %qT in "
5430 "%<#pragma omp declare reduction%>", type);
5431 return;
5434 else if (TREE_CODE (type) == FUNCTION_TYPE
5435 || TREE_CODE (type) == METHOD_TYPE
5436 || TREE_CODE (type) == ARRAY_TYPE)
5438 error_at (loc, "function or array type %qT in "
5439 "%<#pragma omp declare reduction%>", type);
5440 return;
5442 else if (TYPE_REF_P (type))
5444 error_at (loc, "reference type %qT in %<#pragma omp declare reduction%>",
5445 type);
5446 return;
5448 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
5450 error_at (loc, "const, volatile or __restrict qualified type %qT in "
5451 "%<#pragma omp declare reduction%>", type);
5452 return;
5455 tree body = DECL_SAVED_TREE (udr);
5456 if (body == NULL_TREE || TREE_CODE (body) != STATEMENT_LIST)
5457 return;
5459 tree_stmt_iterator tsi;
5460 struct cp_check_omp_declare_reduction_data data;
5461 memset (data.stmts, 0, sizeof data.stmts);
5462 for (i = 0, tsi = tsi_start (body);
5463 i < 7 && !tsi_end_p (tsi);
5464 i++, tsi_next (&tsi))
5465 data.stmts[i] = tsi_stmt (tsi);
5466 data.loc = loc;
5467 gcc_assert (tsi_end_p (tsi));
5468 if (i >= 3)
5470 gcc_assert (TREE_CODE (data.stmts[0]) == DECL_EXPR
5471 && TREE_CODE (data.stmts[1]) == DECL_EXPR);
5472 if (TREE_NO_WARNING (DECL_EXPR_DECL (data.stmts[0])))
5473 return;
5474 data.combiner_p = true;
5475 if (cp_walk_tree (&data.stmts[2], cp_check_omp_declare_reduction_r,
5476 &data, NULL))
5477 TREE_NO_WARNING (DECL_EXPR_DECL (data.stmts[0])) = 1;
5479 if (i >= 6)
5481 gcc_assert (TREE_CODE (data.stmts[3]) == DECL_EXPR
5482 && TREE_CODE (data.stmts[4]) == DECL_EXPR);
5483 data.combiner_p = false;
5484 if (cp_walk_tree (&data.stmts[5], cp_check_omp_declare_reduction_r,
5485 &data, NULL)
5486 || cp_walk_tree (&DECL_INITIAL (DECL_EXPR_DECL (data.stmts[3])),
5487 cp_check_omp_declare_reduction_r, &data, NULL))
5488 TREE_NO_WARNING (DECL_EXPR_DECL (data.stmts[0])) = 1;
5489 if (i == 7)
5490 gcc_assert (TREE_CODE (data.stmts[6]) == DECL_EXPR);
5494 /* Helper function of finish_omp_clauses. Clone STMT as if we were making
5495 an inline call. But, remap
5496 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
5497 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
5499 static tree
5500 clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
5501 tree decl, tree placeholder)
5503 copy_body_data id;
5504 hash_map<tree, tree> decl_map;
5506 decl_map.put (omp_decl1, placeholder);
5507 decl_map.put (omp_decl2, decl);
5508 memset (&id, 0, sizeof (id));
5509 id.src_fn = DECL_CONTEXT (omp_decl1);
5510 id.dst_fn = current_function_decl;
5511 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
5512 id.decl_map = &decl_map;
5514 id.copy_decl = copy_decl_no_change;
5515 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
5516 id.transform_new_cfg = true;
5517 id.transform_return_to_modify = false;
5518 id.transform_lang_insert_block = NULL;
5519 id.eh_lp_nr = 0;
5520 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
5521 return stmt;
5524 /* Helper function of finish_omp_clauses, called via cp_walk_tree.
5525 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
5527 static tree
5528 find_omp_placeholder_r (tree *tp, int *, void *data)
5530 if (*tp == (tree) data)
5531 return *tp;
5532 return NULL_TREE;
5535 /* Helper function of finish_omp_clauses. Handle OMP_CLAUSE_REDUCTION C.
5536 Return true if there is some error and the clause should be removed. */
5538 static bool
5539 finish_omp_reduction_clause (tree c, bool *need_default_ctor, bool *need_dtor)
5541 tree t = OMP_CLAUSE_DECL (c);
5542 bool predefined = false;
5543 if (TREE_CODE (t) == TREE_LIST)
5545 gcc_assert (processing_template_decl);
5546 return false;
5548 tree type = TREE_TYPE (t);
5549 if (TREE_CODE (t) == MEM_REF)
5550 type = TREE_TYPE (type);
5551 if (TYPE_REF_P (type))
5552 type = TREE_TYPE (type);
5553 if (TREE_CODE (type) == ARRAY_TYPE)
5555 tree oatype = type;
5556 gcc_assert (TREE_CODE (t) != MEM_REF);
5557 while (TREE_CODE (type) == ARRAY_TYPE)
5558 type = TREE_TYPE (type);
5559 if (!processing_template_decl)
5561 t = require_complete_type (t);
5562 if (t == error_mark_node)
5563 return true;
5564 tree size = size_binop (EXACT_DIV_EXPR, TYPE_SIZE_UNIT (oatype),
5565 TYPE_SIZE_UNIT (type));
5566 if (integer_zerop (size))
5568 error ("%qE in %<reduction%> clause is a zero size array",
5569 omp_clause_printable_decl (t));
5570 return true;
5572 size = size_binop (MINUS_EXPR, size, size_one_node);
5573 tree index_type = build_index_type (size);
5574 tree atype = build_array_type (type, index_type);
5575 tree ptype = build_pointer_type (type);
5576 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
5577 t = build_fold_addr_expr (t);
5578 t = build2 (MEM_REF, atype, t, build_int_cst (ptype, 0));
5579 OMP_CLAUSE_DECL (c) = t;
5582 if (type == error_mark_node)
5583 return true;
5584 else if (ARITHMETIC_TYPE_P (type))
5585 switch (OMP_CLAUSE_REDUCTION_CODE (c))
5587 case PLUS_EXPR:
5588 case MULT_EXPR:
5589 case MINUS_EXPR:
5590 predefined = true;
5591 break;
5592 case MIN_EXPR:
5593 case MAX_EXPR:
5594 if (TREE_CODE (type) == COMPLEX_TYPE)
5595 break;
5596 predefined = true;
5597 break;
5598 case BIT_AND_EXPR:
5599 case BIT_IOR_EXPR:
5600 case BIT_XOR_EXPR:
5601 if (FLOAT_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
5602 break;
5603 predefined = true;
5604 break;
5605 case TRUTH_ANDIF_EXPR:
5606 case TRUTH_ORIF_EXPR:
5607 if (FLOAT_TYPE_P (type))
5608 break;
5609 predefined = true;
5610 break;
5611 default:
5612 break;
5614 else if (TYPE_READONLY (type))
5616 error ("%qE has const type for %<reduction%>",
5617 omp_clause_printable_decl (t));
5618 return true;
5620 else if (!processing_template_decl)
5622 t = require_complete_type (t);
5623 if (t == error_mark_node)
5624 return true;
5625 OMP_CLAUSE_DECL (c) = t;
5628 if (predefined)
5630 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = NULL_TREE;
5631 return false;
5633 else if (processing_template_decl)
5635 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == error_mark_node)
5636 return true;
5637 return false;
5640 tree id = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
5642 type = TYPE_MAIN_VARIANT (type);
5643 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = NULL_TREE;
5644 if (id == NULL_TREE)
5645 id = omp_reduction_id (OMP_CLAUSE_REDUCTION_CODE (c),
5646 NULL_TREE, NULL_TREE);
5647 id = omp_reduction_lookup (OMP_CLAUSE_LOCATION (c), id, type, NULL, NULL);
5648 if (id)
5650 if (id == error_mark_node)
5651 return true;
5652 mark_used (id);
5653 tree body = DECL_SAVED_TREE (id);
5654 if (!body)
5655 return true;
5656 if (TREE_CODE (body) == STATEMENT_LIST)
5658 tree_stmt_iterator tsi;
5659 tree placeholder = NULL_TREE, decl_placeholder = NULL_TREE;
5660 int i;
5661 tree stmts[7];
5662 tree atype = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (id)));
5663 atype = TREE_TYPE (atype);
5664 bool need_static_cast = !same_type_p (type, atype);
5665 memset (stmts, 0, sizeof stmts);
5666 for (i = 0, tsi = tsi_start (body);
5667 i < 7 && !tsi_end_p (tsi);
5668 i++, tsi_next (&tsi))
5669 stmts[i] = tsi_stmt (tsi);
5670 gcc_assert (tsi_end_p (tsi));
5672 if (i >= 3)
5674 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
5675 && TREE_CODE (stmts[1]) == DECL_EXPR);
5676 placeholder = build_lang_decl (VAR_DECL, NULL_TREE, type);
5677 DECL_ARTIFICIAL (placeholder) = 1;
5678 DECL_IGNORED_P (placeholder) = 1;
5679 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
5680 if (TREE_CODE (t) == MEM_REF)
5682 decl_placeholder = build_lang_decl (VAR_DECL, NULL_TREE,
5683 type);
5684 DECL_ARTIFICIAL (decl_placeholder) = 1;
5685 DECL_IGNORED_P (decl_placeholder) = 1;
5686 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c) = decl_placeholder;
5688 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[0])))
5689 cxx_mark_addressable (placeholder);
5690 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[1]))
5691 && !TYPE_REF_P (TREE_TYPE (OMP_CLAUSE_DECL (c))))
5692 cxx_mark_addressable (decl_placeholder ? decl_placeholder
5693 : OMP_CLAUSE_DECL (c));
5694 tree omp_out = placeholder;
5695 tree omp_in = decl_placeholder ? decl_placeholder
5696 : convert_from_reference (OMP_CLAUSE_DECL (c));
5697 if (need_static_cast)
5699 tree rtype = build_reference_type (atype);
5700 omp_out = build_static_cast (rtype, omp_out,
5701 tf_warning_or_error);
5702 omp_in = build_static_cast (rtype, omp_in,
5703 tf_warning_or_error);
5704 if (omp_out == error_mark_node || omp_in == error_mark_node)
5705 return true;
5706 omp_out = convert_from_reference (omp_out);
5707 omp_in = convert_from_reference (omp_in);
5709 OMP_CLAUSE_REDUCTION_MERGE (c)
5710 = clone_omp_udr (stmts[2], DECL_EXPR_DECL (stmts[0]),
5711 DECL_EXPR_DECL (stmts[1]), omp_in, omp_out);
5713 if (i >= 6)
5715 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
5716 && TREE_CODE (stmts[4]) == DECL_EXPR);
5717 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[3])))
5718 cxx_mark_addressable (decl_placeholder ? decl_placeholder
5719 : OMP_CLAUSE_DECL (c));
5720 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[4])))
5721 cxx_mark_addressable (placeholder);
5722 tree omp_priv = decl_placeholder ? decl_placeholder
5723 : convert_from_reference (OMP_CLAUSE_DECL (c));
5724 tree omp_orig = placeholder;
5725 if (need_static_cast)
5727 if (i == 7)
5729 error_at (OMP_CLAUSE_LOCATION (c),
5730 "user defined reduction with constructor "
5731 "initializer for base class %qT", atype);
5732 return true;
5734 tree rtype = build_reference_type (atype);
5735 omp_priv = build_static_cast (rtype, omp_priv,
5736 tf_warning_or_error);
5737 omp_orig = build_static_cast (rtype, omp_orig,
5738 tf_warning_or_error);
5739 if (omp_priv == error_mark_node
5740 || omp_orig == error_mark_node)
5741 return true;
5742 omp_priv = convert_from_reference (omp_priv);
5743 omp_orig = convert_from_reference (omp_orig);
5745 if (i == 6)
5746 *need_default_ctor = true;
5747 OMP_CLAUSE_REDUCTION_INIT (c)
5748 = clone_omp_udr (stmts[5], DECL_EXPR_DECL (stmts[4]),
5749 DECL_EXPR_DECL (stmts[3]),
5750 omp_priv, omp_orig);
5751 if (cp_walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
5752 find_omp_placeholder_r, placeholder, NULL))
5753 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
5755 else if (i >= 3)
5757 if (CLASS_TYPE_P (type) && !pod_type_p (type))
5758 *need_default_ctor = true;
5759 else
5761 tree init;
5762 tree v = decl_placeholder ? decl_placeholder
5763 : convert_from_reference (t);
5764 if (AGGREGATE_TYPE_P (TREE_TYPE (v)))
5765 init = build_constructor (TREE_TYPE (v), NULL);
5766 else
5767 init = fold_convert (TREE_TYPE (v), integer_zero_node);
5768 OMP_CLAUSE_REDUCTION_INIT (c)
5769 = build2 (INIT_EXPR, TREE_TYPE (v), v, init);
5774 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
5775 *need_dtor = true;
5776 else
5778 error ("user defined reduction not found for %qE",
5779 omp_clause_printable_decl (t));
5780 return true;
5782 if (TREE_CODE (OMP_CLAUSE_DECL (c)) == MEM_REF)
5783 gcc_assert (TYPE_SIZE_UNIT (type)
5784 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST);
5785 return false;
5788 /* Called from finish_struct_1. linear(this) or linear(this:step)
5789 clauses might not be finalized yet because the class has been incomplete
5790 when parsing #pragma omp declare simd methods. Fix those up now. */
5792 void
5793 finish_omp_declare_simd_methods (tree t)
5795 if (processing_template_decl)
5796 return;
5798 for (tree x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
5800 if (TREE_CODE (TREE_TYPE (x)) != METHOD_TYPE)
5801 continue;
5802 tree ods = lookup_attribute ("omp declare simd", DECL_ATTRIBUTES (x));
5803 if (!ods || !TREE_VALUE (ods))
5804 continue;
5805 for (tree c = TREE_VALUE (TREE_VALUE (ods)); c; c = OMP_CLAUSE_CHAIN (c))
5806 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
5807 && integer_zerop (OMP_CLAUSE_DECL (c))
5808 && OMP_CLAUSE_LINEAR_STEP (c)
5809 && TYPE_PTR_P (TREE_TYPE (OMP_CLAUSE_LINEAR_STEP (c))))
5811 tree s = OMP_CLAUSE_LINEAR_STEP (c);
5812 s = fold_convert_loc (OMP_CLAUSE_LOCATION (c), sizetype, s);
5813 s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MULT_EXPR,
5814 sizetype, s, TYPE_SIZE_UNIT (t));
5815 OMP_CLAUSE_LINEAR_STEP (c) = s;
5820 /* Adjust sink depend clause to take into account pointer offsets.
5822 Return TRUE if there was a problem processing the offset, and the
5823 whole clause should be removed. */
5825 static bool
5826 cp_finish_omp_clause_depend_sink (tree sink_clause)
5828 tree t = OMP_CLAUSE_DECL (sink_clause);
5829 gcc_assert (TREE_CODE (t) == TREE_LIST);
5831 /* Make sure we don't adjust things twice for templates. */
5832 if (processing_template_decl)
5833 return false;
5835 for (; t; t = TREE_CHAIN (t))
5837 tree decl = TREE_VALUE (t);
5838 if (TYPE_PTR_P (TREE_TYPE (decl)))
5840 tree offset = TREE_PURPOSE (t);
5841 bool neg = wi::neg_p (wi::to_wide (offset));
5842 offset = fold_unary (ABS_EXPR, TREE_TYPE (offset), offset);
5843 decl = mark_rvalue_use (decl);
5844 decl = convert_from_reference (decl);
5845 tree t2 = pointer_int_sum (OMP_CLAUSE_LOCATION (sink_clause),
5846 neg ? MINUS_EXPR : PLUS_EXPR,
5847 decl, offset);
5848 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (sink_clause),
5849 MINUS_EXPR, sizetype,
5850 fold_convert (sizetype, t2),
5851 fold_convert (sizetype, decl));
5852 if (t2 == error_mark_node)
5853 return true;
5854 TREE_PURPOSE (t) = t2;
5857 return false;
5860 /* For all elements of CLAUSES, validate them vs OpenMP constraints.
5861 Remove any elements from the list that are invalid. */
5863 tree
5864 finish_omp_clauses (tree clauses, enum c_omp_region_type ort)
5866 bitmap_head generic_head, firstprivate_head, lastprivate_head;
5867 bitmap_head aligned_head, map_head, map_field_head, oacc_reduction_head;
5868 tree c, t, *pc;
5869 tree safelen = NULL_TREE;
5870 bool branch_seen = false;
5871 bool copyprivate_seen = false;
5872 bool ordered_seen = false;
5873 bool oacc_async = false;
5875 bitmap_obstack_initialize (NULL);
5876 bitmap_initialize (&generic_head, &bitmap_default_obstack);
5877 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
5878 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
5879 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
5880 bitmap_initialize (&map_head, &bitmap_default_obstack);
5881 bitmap_initialize (&map_field_head, &bitmap_default_obstack);
5882 bitmap_initialize (&oacc_reduction_head, &bitmap_default_obstack);
5884 if (ort & C_ORT_ACC)
5885 for (c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
5886 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ASYNC)
5888 oacc_async = true;
5889 break;
5892 for (pc = &clauses, c = clauses; c ; c = *pc)
5894 bool remove = false;
5895 bool field_ok = false;
5897 switch (OMP_CLAUSE_CODE (c))
5899 case OMP_CLAUSE_SHARED:
5900 field_ok = ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP);
5901 goto check_dup_generic;
5902 case OMP_CLAUSE_PRIVATE:
5903 field_ok = ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP);
5904 goto check_dup_generic;
5905 case OMP_CLAUSE_REDUCTION:
5906 field_ok = ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP);
5907 t = OMP_CLAUSE_DECL (c);
5908 if (TREE_CODE (t) == TREE_LIST)
5910 if (handle_omp_array_sections (c, ort))
5912 remove = true;
5913 break;
5915 if (TREE_CODE (t) == TREE_LIST)
5917 while (TREE_CODE (t) == TREE_LIST)
5918 t = TREE_CHAIN (t);
5920 else
5922 gcc_assert (TREE_CODE (t) == MEM_REF);
5923 t = TREE_OPERAND (t, 0);
5924 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
5925 t = TREE_OPERAND (t, 0);
5926 if (TREE_CODE (t) == ADDR_EXPR
5927 || INDIRECT_REF_P (t))
5928 t = TREE_OPERAND (t, 0);
5930 tree n = omp_clause_decl_field (t);
5931 if (n)
5932 t = n;
5933 goto check_dup_generic_t;
5935 if (oacc_async)
5936 cxx_mark_addressable (t);
5937 goto check_dup_generic;
5938 case OMP_CLAUSE_COPYPRIVATE:
5939 copyprivate_seen = true;
5940 field_ok = ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP);
5941 goto check_dup_generic;
5942 case OMP_CLAUSE_COPYIN:
5943 goto check_dup_generic;
5944 case OMP_CLAUSE_LINEAR:
5945 field_ok = ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP);
5946 t = OMP_CLAUSE_DECL (c);
5947 if (ort != C_ORT_OMP_DECLARE_SIMD
5948 && OMP_CLAUSE_LINEAR_KIND (c) != OMP_CLAUSE_LINEAR_DEFAULT)
5950 error_at (OMP_CLAUSE_LOCATION (c),
5951 "modifier should not be specified in %<linear%> "
5952 "clause on %<simd%> or %<for%> constructs");
5953 OMP_CLAUSE_LINEAR_KIND (c) = OMP_CLAUSE_LINEAR_DEFAULT;
5955 if ((VAR_P (t) || TREE_CODE (t) == PARM_DECL)
5956 && !type_dependent_expression_p (t))
5958 tree type = TREE_TYPE (t);
5959 if ((OMP_CLAUSE_LINEAR_KIND (c) == OMP_CLAUSE_LINEAR_REF
5960 || OMP_CLAUSE_LINEAR_KIND (c) == OMP_CLAUSE_LINEAR_UVAL)
5961 && !TYPE_REF_P (type))
5963 error ("linear clause with %qs modifier applied to "
5964 "non-reference variable with %qT type",
5965 OMP_CLAUSE_LINEAR_KIND (c) == OMP_CLAUSE_LINEAR_REF
5966 ? "ref" : "uval", TREE_TYPE (t));
5967 remove = true;
5968 break;
5970 if (TYPE_REF_P (type))
5971 type = TREE_TYPE (type);
5972 if (OMP_CLAUSE_LINEAR_KIND (c) != OMP_CLAUSE_LINEAR_REF)
5974 if (!INTEGRAL_TYPE_P (type)
5975 && !TYPE_PTR_P (type))
5977 error ("linear clause applied to non-integral non-pointer"
5978 " variable with %qT type", TREE_TYPE (t));
5979 remove = true;
5980 break;
5984 t = OMP_CLAUSE_LINEAR_STEP (c);
5985 if (t == NULL_TREE)
5986 t = integer_one_node;
5987 if (t == error_mark_node)
5989 remove = true;
5990 break;
5992 else if (!type_dependent_expression_p (t)
5993 && !INTEGRAL_TYPE_P (TREE_TYPE (t))
5994 && (ort != C_ORT_OMP_DECLARE_SIMD
5995 || TREE_CODE (t) != PARM_DECL
5996 || !TYPE_REF_P (TREE_TYPE (t))
5997 || !INTEGRAL_TYPE_P (TREE_TYPE (TREE_TYPE (t)))))
5999 error ("linear step expression must be integral");
6000 remove = true;
6001 break;
6003 else
6005 t = mark_rvalue_use (t);
6006 if (ort == C_ORT_OMP_DECLARE_SIMD && TREE_CODE (t) == PARM_DECL)
6008 OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c) = 1;
6009 goto check_dup_generic;
6011 if (!processing_template_decl
6012 && (VAR_P (OMP_CLAUSE_DECL (c))
6013 || TREE_CODE (OMP_CLAUSE_DECL (c)) == PARM_DECL))
6015 if (ort == C_ORT_OMP_DECLARE_SIMD)
6017 t = maybe_constant_value (t);
6018 if (TREE_CODE (t) != INTEGER_CST)
6020 error_at (OMP_CLAUSE_LOCATION (c),
6021 "%<linear%> clause step %qE is neither "
6022 "constant nor a parameter", t);
6023 remove = true;
6024 break;
6027 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6028 tree type = TREE_TYPE (OMP_CLAUSE_DECL (c));
6029 if (TYPE_REF_P (type))
6030 type = TREE_TYPE (type);
6031 if (OMP_CLAUSE_LINEAR_KIND (c) == OMP_CLAUSE_LINEAR_REF)
6033 type = build_pointer_type (type);
6034 tree d = fold_convert (type, OMP_CLAUSE_DECL (c));
6035 t = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
6036 d, t);
6037 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c),
6038 MINUS_EXPR, sizetype,
6039 fold_convert (sizetype, t),
6040 fold_convert (sizetype, d));
6041 if (t == error_mark_node)
6043 remove = true;
6044 break;
6047 else if (TYPE_PTR_P (type)
6048 /* Can't multiply the step yet if *this
6049 is still incomplete type. */
6050 && (ort != C_ORT_OMP_DECLARE_SIMD
6051 || TREE_CODE (OMP_CLAUSE_DECL (c)) != PARM_DECL
6052 || !DECL_ARTIFICIAL (OMP_CLAUSE_DECL (c))
6053 || DECL_NAME (OMP_CLAUSE_DECL (c))
6054 != this_identifier
6055 || !TYPE_BEING_DEFINED (TREE_TYPE (type))))
6057 tree d = convert_from_reference (OMP_CLAUSE_DECL (c));
6058 t = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
6059 d, t);
6060 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c),
6061 MINUS_EXPR, sizetype,
6062 fold_convert (sizetype, t),
6063 fold_convert (sizetype, d));
6064 if (t == error_mark_node)
6066 remove = true;
6067 break;
6070 else
6071 t = fold_convert (type, t);
6073 OMP_CLAUSE_LINEAR_STEP (c) = t;
6075 goto check_dup_generic;
6076 check_dup_generic:
6077 t = omp_clause_decl_field (OMP_CLAUSE_DECL (c));
6078 if (t)
6080 if (!remove && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_SHARED)
6081 omp_note_field_privatization (t, OMP_CLAUSE_DECL (c));
6083 else
6084 t = OMP_CLAUSE_DECL (c);
6085 check_dup_generic_t:
6086 if (t == current_class_ptr
6087 && (ort != C_ORT_OMP_DECLARE_SIMD
6088 || (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_LINEAR
6089 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_UNIFORM)))
6091 error ("%<this%> allowed in OpenMP only in %<declare simd%>"
6092 " clauses");
6093 remove = true;
6094 break;
6096 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL
6097 && (!field_ok || TREE_CODE (t) != FIELD_DECL))
6099 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
6100 break;
6101 if (DECL_P (t))
6102 error ("%qD is not a variable in clause %qs", t,
6103 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6104 else
6105 error ("%qE is not a variable in clause %qs", t,
6106 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6107 remove = true;
6109 else if (ort == C_ORT_ACC
6110 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
6112 if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
6114 error ("%qD appears more than once in reduction clauses", t);
6115 remove = true;
6117 else
6118 bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
6120 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
6121 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
6122 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
6124 error ("%qD appears more than once in data clauses", t);
6125 remove = true;
6127 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
6128 && bitmap_bit_p (&map_head, DECL_UID (t)))
6130 if (ort == C_ORT_ACC)
6131 error ("%qD appears more than once in data clauses", t);
6132 else
6133 error ("%qD appears both in data and map clauses", t);
6134 remove = true;
6136 else
6137 bitmap_set_bit (&generic_head, DECL_UID (t));
6138 if (!field_ok)
6139 break;
6140 handle_field_decl:
6141 if (!remove
6142 && TREE_CODE (t) == FIELD_DECL
6143 && t == OMP_CLAUSE_DECL (c)
6144 && ort != C_ORT_ACC)
6146 OMP_CLAUSE_DECL (c)
6147 = omp_privatize_field (t, (OMP_CLAUSE_CODE (c)
6148 == OMP_CLAUSE_SHARED));
6149 if (OMP_CLAUSE_DECL (c) == error_mark_node)
6150 remove = true;
6152 break;
6154 case OMP_CLAUSE_FIRSTPRIVATE:
6155 t = omp_clause_decl_field (OMP_CLAUSE_DECL (c));
6156 if (t)
6157 omp_note_field_privatization (t, OMP_CLAUSE_DECL (c));
6158 else
6159 t = OMP_CLAUSE_DECL (c);
6160 if (ort != C_ORT_ACC && t == current_class_ptr)
6162 error ("%<this%> allowed in OpenMP only in %<declare simd%>"
6163 " clauses");
6164 remove = true;
6165 break;
6167 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL
6168 && ((ort & C_ORT_OMP_DECLARE_SIMD) != C_ORT_OMP
6169 || TREE_CODE (t) != FIELD_DECL))
6171 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
6172 break;
6173 if (DECL_P (t))
6174 error ("%qD is not a variable in clause %<firstprivate%>", t);
6175 else
6176 error ("%qE is not a variable in clause %<firstprivate%>", t);
6177 remove = true;
6179 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
6180 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
6182 error ("%qD appears more than once in data clauses", t);
6183 remove = true;
6185 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
6187 if (ort == C_ORT_ACC)
6188 error ("%qD appears more than once in data clauses", t);
6189 else
6190 error ("%qD appears both in data and map clauses", t);
6191 remove = true;
6193 else
6194 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
6195 goto handle_field_decl;
6197 case OMP_CLAUSE_LASTPRIVATE:
6198 t = omp_clause_decl_field (OMP_CLAUSE_DECL (c));
6199 if (t)
6200 omp_note_field_privatization (t, OMP_CLAUSE_DECL (c));
6201 else
6202 t = OMP_CLAUSE_DECL (c);
6203 if (t == current_class_ptr)
6205 error ("%<this%> allowed in OpenMP only in %<declare simd%>"
6206 " clauses");
6207 remove = true;
6208 break;
6210 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL
6211 && ((ort & C_ORT_OMP_DECLARE_SIMD) != C_ORT_OMP
6212 || TREE_CODE (t) != FIELD_DECL))
6214 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
6215 break;
6216 if (DECL_P (t))
6217 error ("%qD is not a variable in clause %<lastprivate%>", t);
6218 else
6219 error ("%qE is not a variable in clause %<lastprivate%>", t);
6220 remove = true;
6222 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
6223 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
6225 error ("%qD appears more than once in data clauses", t);
6226 remove = true;
6228 else
6229 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
6230 goto handle_field_decl;
6232 case OMP_CLAUSE_IF:
6233 t = OMP_CLAUSE_IF_EXPR (c);
6234 t = maybe_convert_cond (t);
6235 if (t == error_mark_node)
6236 remove = true;
6237 else if (!processing_template_decl)
6238 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6239 OMP_CLAUSE_IF_EXPR (c) = t;
6240 break;
6242 case OMP_CLAUSE_FINAL:
6243 t = OMP_CLAUSE_FINAL_EXPR (c);
6244 t = maybe_convert_cond (t);
6245 if (t == error_mark_node)
6246 remove = true;
6247 else if (!processing_template_decl)
6248 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6249 OMP_CLAUSE_FINAL_EXPR (c) = t;
6250 break;
6252 case OMP_CLAUSE_GANG:
6253 /* Operand 1 is the gang static: argument. */
6254 t = OMP_CLAUSE_OPERAND (c, 1);
6255 if (t != NULL_TREE)
6257 if (t == error_mark_node)
6258 remove = true;
6259 else if (!type_dependent_expression_p (t)
6260 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6262 error ("%<gang%> static expression must be integral");
6263 remove = true;
6265 else
6267 t = mark_rvalue_use (t);
6268 if (!processing_template_decl)
6270 t = maybe_constant_value (t);
6271 if (TREE_CODE (t) == INTEGER_CST
6272 && tree_int_cst_sgn (t) != 1
6273 && t != integer_minus_one_node)
6275 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6276 "%<gang%> static value must be "
6277 "positive");
6278 t = integer_one_node;
6280 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6283 OMP_CLAUSE_OPERAND (c, 1) = t;
6285 /* Check operand 0, the num argument. */
6286 /* FALLTHRU */
6288 case OMP_CLAUSE_WORKER:
6289 case OMP_CLAUSE_VECTOR:
6290 if (OMP_CLAUSE_OPERAND (c, 0) == NULL_TREE)
6291 break;
6292 /* FALLTHRU */
6294 case OMP_CLAUSE_NUM_TASKS:
6295 case OMP_CLAUSE_NUM_TEAMS:
6296 case OMP_CLAUSE_NUM_THREADS:
6297 case OMP_CLAUSE_NUM_GANGS:
6298 case OMP_CLAUSE_NUM_WORKERS:
6299 case OMP_CLAUSE_VECTOR_LENGTH:
6300 t = OMP_CLAUSE_OPERAND (c, 0);
6301 if (t == error_mark_node)
6302 remove = true;
6303 else if (!type_dependent_expression_p (t)
6304 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6306 switch (OMP_CLAUSE_CODE (c))
6308 case OMP_CLAUSE_GANG:
6309 error_at (OMP_CLAUSE_LOCATION (c),
6310 "%<gang%> num expression must be integral"); break;
6311 case OMP_CLAUSE_VECTOR:
6312 error_at (OMP_CLAUSE_LOCATION (c),
6313 "%<vector%> length expression must be integral");
6314 break;
6315 case OMP_CLAUSE_WORKER:
6316 error_at (OMP_CLAUSE_LOCATION (c),
6317 "%<worker%> num expression must be integral");
6318 break;
6319 default:
6320 error_at (OMP_CLAUSE_LOCATION (c),
6321 "%qs expression must be integral",
6322 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6324 remove = true;
6326 else
6328 t = mark_rvalue_use (t);
6329 if (!processing_template_decl)
6331 t = maybe_constant_value (t);
6332 if (TREE_CODE (t) == INTEGER_CST
6333 && tree_int_cst_sgn (t) != 1)
6335 switch (OMP_CLAUSE_CODE (c))
6337 case OMP_CLAUSE_GANG:
6338 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6339 "%<gang%> num value must be positive");
6340 break;
6341 case OMP_CLAUSE_VECTOR:
6342 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6343 "%<vector%> length value must be "
6344 "positive");
6345 break;
6346 case OMP_CLAUSE_WORKER:
6347 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6348 "%<worker%> num value must be "
6349 "positive");
6350 break;
6351 default:
6352 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6353 "%qs value must be positive",
6354 omp_clause_code_name
6355 [OMP_CLAUSE_CODE (c)]);
6357 t = integer_one_node;
6359 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6361 OMP_CLAUSE_OPERAND (c, 0) = t;
6363 break;
6365 case OMP_CLAUSE_SCHEDULE:
6366 if (OMP_CLAUSE_SCHEDULE_KIND (c) & OMP_CLAUSE_SCHEDULE_NONMONOTONIC)
6368 const char *p = NULL;
6369 switch (OMP_CLAUSE_SCHEDULE_KIND (c) & OMP_CLAUSE_SCHEDULE_MASK)
6371 case OMP_CLAUSE_SCHEDULE_STATIC: p = "static"; break;
6372 case OMP_CLAUSE_SCHEDULE_DYNAMIC: break;
6373 case OMP_CLAUSE_SCHEDULE_GUIDED: break;
6374 case OMP_CLAUSE_SCHEDULE_AUTO: p = "auto"; break;
6375 case OMP_CLAUSE_SCHEDULE_RUNTIME: p = "runtime"; break;
6376 default: gcc_unreachable ();
6378 if (p)
6380 error_at (OMP_CLAUSE_LOCATION (c),
6381 "%<nonmonotonic%> modifier specified for %qs "
6382 "schedule kind", p);
6383 OMP_CLAUSE_SCHEDULE_KIND (c)
6384 = (enum omp_clause_schedule_kind)
6385 (OMP_CLAUSE_SCHEDULE_KIND (c)
6386 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
6390 t = OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c);
6391 if (t == NULL)
6393 else if (t == error_mark_node)
6394 remove = true;
6395 else if (!type_dependent_expression_p (t)
6396 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6398 error ("schedule chunk size expression must be integral");
6399 remove = true;
6401 else
6403 t = mark_rvalue_use (t);
6404 if (!processing_template_decl)
6406 t = maybe_constant_value (t);
6407 if (TREE_CODE (t) == INTEGER_CST
6408 && tree_int_cst_sgn (t) != 1)
6410 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6411 "chunk size value must be positive");
6412 t = integer_one_node;
6414 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6416 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
6418 break;
6420 case OMP_CLAUSE_SIMDLEN:
6421 case OMP_CLAUSE_SAFELEN:
6422 t = OMP_CLAUSE_OPERAND (c, 0);
6423 if (t == error_mark_node)
6424 remove = true;
6425 else if (!type_dependent_expression_p (t)
6426 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6428 error ("%qs length expression must be integral",
6429 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6430 remove = true;
6432 else
6434 t = mark_rvalue_use (t);
6435 if (!processing_template_decl)
6437 t = maybe_constant_value (t);
6438 if (TREE_CODE (t) != INTEGER_CST
6439 || tree_int_cst_sgn (t) != 1)
6441 error ("%qs length expression must be positive constant"
6442 " integer expression",
6443 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6444 remove = true;
6447 OMP_CLAUSE_OPERAND (c, 0) = t;
6448 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SAFELEN)
6449 safelen = c;
6451 break;
6453 case OMP_CLAUSE_ASYNC:
6454 t = OMP_CLAUSE_ASYNC_EXPR (c);
6455 if (t == error_mark_node)
6456 remove = true;
6457 else if (!type_dependent_expression_p (t)
6458 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6460 error ("%<async%> expression must be integral");
6461 remove = true;
6463 else
6465 t = mark_rvalue_use (t);
6466 if (!processing_template_decl)
6467 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6468 OMP_CLAUSE_ASYNC_EXPR (c) = t;
6470 break;
6472 case OMP_CLAUSE_WAIT:
6473 t = OMP_CLAUSE_WAIT_EXPR (c);
6474 if (t == error_mark_node)
6475 remove = true;
6476 else if (!processing_template_decl)
6477 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6478 OMP_CLAUSE_WAIT_EXPR (c) = t;
6479 break;
6481 case OMP_CLAUSE_THREAD_LIMIT:
6482 t = OMP_CLAUSE_THREAD_LIMIT_EXPR (c);
6483 if (t == error_mark_node)
6484 remove = true;
6485 else if (!type_dependent_expression_p (t)
6486 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6488 error ("%<thread_limit%> expression must be integral");
6489 remove = true;
6491 else
6493 t = mark_rvalue_use (t);
6494 if (!processing_template_decl)
6496 t = maybe_constant_value (t);
6497 if (TREE_CODE (t) == INTEGER_CST
6498 && tree_int_cst_sgn (t) != 1)
6500 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6501 "%<thread_limit%> value must be positive");
6502 t = integer_one_node;
6504 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6506 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
6508 break;
6510 case OMP_CLAUSE_DEVICE:
6511 t = OMP_CLAUSE_DEVICE_ID (c);
6512 if (t == error_mark_node)
6513 remove = true;
6514 else if (!type_dependent_expression_p (t)
6515 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6517 error ("%<device%> id must be integral");
6518 remove = true;
6520 else
6522 t = mark_rvalue_use (t);
6523 if (!processing_template_decl)
6524 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6525 OMP_CLAUSE_DEVICE_ID (c) = t;
6527 break;
6529 case OMP_CLAUSE_DIST_SCHEDULE:
6530 t = OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c);
6531 if (t == NULL)
6533 else if (t == error_mark_node)
6534 remove = true;
6535 else if (!type_dependent_expression_p (t)
6536 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6538 error ("%<dist_schedule%> chunk size expression must be "
6539 "integral");
6540 remove = true;
6542 else
6544 t = mark_rvalue_use (t);
6545 if (!processing_template_decl)
6546 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6547 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
6549 break;
6551 case OMP_CLAUSE_ALIGNED:
6552 t = OMP_CLAUSE_DECL (c);
6553 if (t == current_class_ptr && ort != C_ORT_OMP_DECLARE_SIMD)
6555 error ("%<this%> allowed in OpenMP only in %<declare simd%>"
6556 " clauses");
6557 remove = true;
6558 break;
6560 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
6562 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
6563 break;
6564 if (DECL_P (t))
6565 error ("%qD is not a variable in %<aligned%> clause", t);
6566 else
6567 error ("%qE is not a variable in %<aligned%> clause", t);
6568 remove = true;
6570 else if (!type_dependent_expression_p (t)
6571 && !TYPE_PTR_P (TREE_TYPE (t))
6572 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE
6573 && (!TYPE_REF_P (TREE_TYPE (t))
6574 || (!INDIRECT_TYPE_P (TREE_TYPE (TREE_TYPE (t)))
6575 && (TREE_CODE (TREE_TYPE (TREE_TYPE (t)))
6576 != ARRAY_TYPE))))
6578 error_at (OMP_CLAUSE_LOCATION (c),
6579 "%qE in %<aligned%> clause is neither a pointer nor "
6580 "an array nor a reference to pointer or array", t);
6581 remove = true;
6583 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
6585 error ("%qD appears more than once in %<aligned%> clauses", t);
6586 remove = true;
6588 else
6589 bitmap_set_bit (&aligned_head, DECL_UID (t));
6590 t = OMP_CLAUSE_ALIGNED_ALIGNMENT (c);
6591 if (t == error_mark_node)
6592 remove = true;
6593 else if (t == NULL_TREE)
6594 break;
6595 else if (!type_dependent_expression_p (t)
6596 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6598 error ("%<aligned%> clause alignment expression must "
6599 "be integral");
6600 remove = true;
6602 else
6604 t = mark_rvalue_use (t);
6605 if (!processing_template_decl)
6607 t = maybe_constant_value (t);
6608 if (TREE_CODE (t) != INTEGER_CST
6609 || tree_int_cst_sgn (t) != 1)
6611 error ("%<aligned%> clause alignment expression must be "
6612 "positive constant integer expression");
6613 remove = true;
6616 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = t;
6618 break;
6620 case OMP_CLAUSE_DEPEND:
6621 t = OMP_CLAUSE_DECL (c);
6622 if (t == NULL_TREE)
6624 gcc_assert (OMP_CLAUSE_DEPEND_KIND (c)
6625 == OMP_CLAUSE_DEPEND_SOURCE);
6626 break;
6628 if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SINK)
6630 if (cp_finish_omp_clause_depend_sink (c))
6631 remove = true;
6632 break;
6634 if (TREE_CODE (t) == TREE_LIST)
6636 if (handle_omp_array_sections (c, ort))
6637 remove = true;
6638 break;
6640 if (t == error_mark_node)
6641 remove = true;
6642 else if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
6644 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
6645 break;
6646 if (DECL_P (t))
6647 error ("%qD is not a variable in %<depend%> clause", t);
6648 else
6649 error ("%qE is not a variable in %<depend%> clause", t);
6650 remove = true;
6652 else if (t == current_class_ptr)
6654 error ("%<this%> allowed in OpenMP only in %<declare simd%>"
6655 " clauses");
6656 remove = true;
6658 else if (!processing_template_decl
6659 && !cxx_mark_addressable (t))
6660 remove = true;
6661 break;
6663 case OMP_CLAUSE_MAP:
6664 case OMP_CLAUSE_TO:
6665 case OMP_CLAUSE_FROM:
6666 case OMP_CLAUSE__CACHE_:
6667 t = OMP_CLAUSE_DECL (c);
6668 if (TREE_CODE (t) == TREE_LIST)
6670 if (handle_omp_array_sections (c, ort))
6671 remove = true;
6672 else
6674 t = OMP_CLAUSE_DECL (c);
6675 if (TREE_CODE (t) != TREE_LIST
6676 && !type_dependent_expression_p (t)
6677 && !cp_omp_mappable_type (TREE_TYPE (t)))
6679 error_at (OMP_CLAUSE_LOCATION (c),
6680 "array section does not have mappable type "
6681 "in %qs clause",
6682 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6683 remove = true;
6685 while (TREE_CODE (t) == ARRAY_REF)
6686 t = TREE_OPERAND (t, 0);
6687 if (TREE_CODE (t) == COMPONENT_REF
6688 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
6690 while (TREE_CODE (t) == COMPONENT_REF)
6691 t = TREE_OPERAND (t, 0);
6692 if (REFERENCE_REF_P (t))
6693 t = TREE_OPERAND (t, 0);
6694 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
6695 break;
6696 if (bitmap_bit_p (&map_head, DECL_UID (t)))
6698 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
6699 error ("%qD appears more than once in motion"
6700 " clauses", t);
6701 else if (ort == C_ORT_ACC)
6702 error ("%qD appears more than once in data"
6703 " clauses", t);
6704 else
6705 error ("%qD appears more than once in map"
6706 " clauses", t);
6707 remove = true;
6709 else
6711 bitmap_set_bit (&map_head, DECL_UID (t));
6712 bitmap_set_bit (&map_field_head, DECL_UID (t));
6716 break;
6718 if (t == error_mark_node)
6720 remove = true;
6721 break;
6723 if (REFERENCE_REF_P (t)
6724 && TREE_CODE (TREE_OPERAND (t, 0)) == COMPONENT_REF)
6726 t = TREE_OPERAND (t, 0);
6727 OMP_CLAUSE_DECL (c) = t;
6729 if (TREE_CODE (t) == COMPONENT_REF
6730 && (ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP
6731 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE__CACHE_)
6733 if (type_dependent_expression_p (t))
6734 break;
6735 if (TREE_CODE (TREE_OPERAND (t, 1)) == FIELD_DECL
6736 && DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
6738 error_at (OMP_CLAUSE_LOCATION (c),
6739 "bit-field %qE in %qs clause",
6740 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6741 remove = true;
6743 else if (!cp_omp_mappable_type (TREE_TYPE (t)))
6745 error_at (OMP_CLAUSE_LOCATION (c),
6746 "%qE does not have a mappable type in %qs clause",
6747 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6748 remove = true;
6750 while (TREE_CODE (t) == COMPONENT_REF)
6752 if (TREE_TYPE (TREE_OPERAND (t, 0))
6753 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0)))
6754 == UNION_TYPE))
6756 error_at (OMP_CLAUSE_LOCATION (c),
6757 "%qE is a member of a union", t);
6758 remove = true;
6759 break;
6761 t = TREE_OPERAND (t, 0);
6763 if (remove)
6764 break;
6765 if (REFERENCE_REF_P (t))
6766 t = TREE_OPERAND (t, 0);
6767 if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
6769 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
6770 goto handle_map_references;
6773 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
6775 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
6776 break;
6777 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
6778 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
6779 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ALWAYS_POINTER))
6780 break;
6781 if (DECL_P (t))
6782 error ("%qD is not a variable in %qs clause", t,
6783 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6784 else
6785 error ("%qE is not a variable in %qs clause", t,
6786 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6787 remove = true;
6789 else if (VAR_P (t) && CP_DECL_THREAD_LOCAL_P (t))
6791 error ("%qD is threadprivate variable in %qs clause", t,
6792 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6793 remove = true;
6795 else if (ort != C_ORT_ACC && t == current_class_ptr)
6797 error ("%<this%> allowed in OpenMP only in %<declare simd%>"
6798 " clauses");
6799 remove = true;
6800 break;
6802 else if (!processing_template_decl
6803 && !TYPE_REF_P (TREE_TYPE (t))
6804 && (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
6805 || (OMP_CLAUSE_MAP_KIND (c)
6806 != GOMP_MAP_FIRSTPRIVATE_POINTER))
6807 && !cxx_mark_addressable (t))
6808 remove = true;
6809 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
6810 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
6811 || (OMP_CLAUSE_MAP_KIND (c)
6812 == GOMP_MAP_FIRSTPRIVATE_POINTER)))
6813 && t == OMP_CLAUSE_DECL (c)
6814 && !type_dependent_expression_p (t)
6815 && !cp_omp_mappable_type (TYPE_REF_P (TREE_TYPE (t))
6816 ? TREE_TYPE (TREE_TYPE (t))
6817 : TREE_TYPE (t)))
6819 error_at (OMP_CLAUSE_LOCATION (c),
6820 "%qD does not have a mappable type in %qs clause", t,
6821 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6822 remove = true;
6824 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
6825 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FORCE_DEVICEPTR
6826 && !type_dependent_expression_p (t)
6827 && !INDIRECT_TYPE_P (TREE_TYPE (t)))
6829 error ("%qD is not a pointer variable", t);
6830 remove = true;
6832 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
6833 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
6835 if (bitmap_bit_p (&generic_head, DECL_UID (t))
6836 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
6838 error ("%qD appears more than once in data clauses", t);
6839 remove = true;
6841 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
6843 if (ort == C_ORT_ACC)
6844 error ("%qD appears more than once in data clauses", t);
6845 else
6846 error ("%qD appears both in data and map clauses", t);
6847 remove = true;
6849 else
6850 bitmap_set_bit (&generic_head, DECL_UID (t));
6852 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
6854 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
6855 error ("%qD appears more than once in motion clauses", t);
6856 if (ort == C_ORT_ACC)
6857 error ("%qD appears more than once in data clauses", t);
6858 else
6859 error ("%qD appears more than once in map clauses", t);
6860 remove = true;
6862 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
6863 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
6865 if (ort == C_ORT_ACC)
6866 error ("%qD appears more than once in data clauses", t);
6867 else
6868 error ("%qD appears both in data and map clauses", t);
6869 remove = true;
6871 else
6873 bitmap_set_bit (&map_head, DECL_UID (t));
6874 if (t != OMP_CLAUSE_DECL (c)
6875 && TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPONENT_REF)
6876 bitmap_set_bit (&map_field_head, DECL_UID (t));
6878 handle_map_references:
6879 if (!remove
6880 && !processing_template_decl
6881 && (ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP
6882 && TYPE_REF_P (TREE_TYPE (OMP_CLAUSE_DECL (c))))
6884 t = OMP_CLAUSE_DECL (c);
6885 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
6887 OMP_CLAUSE_DECL (c) = build_simple_mem_ref (t);
6888 if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
6889 OMP_CLAUSE_SIZE (c)
6890 = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t)));
6892 else if (OMP_CLAUSE_MAP_KIND (c)
6893 != GOMP_MAP_FIRSTPRIVATE_POINTER
6894 && (OMP_CLAUSE_MAP_KIND (c)
6895 != GOMP_MAP_FIRSTPRIVATE_REFERENCE)
6896 && (OMP_CLAUSE_MAP_KIND (c)
6897 != GOMP_MAP_ALWAYS_POINTER))
6899 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
6900 OMP_CLAUSE_MAP);
6901 if (TREE_CODE (t) == COMPONENT_REF)
6902 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ALWAYS_POINTER);
6903 else
6904 OMP_CLAUSE_SET_MAP_KIND (c2,
6905 GOMP_MAP_FIRSTPRIVATE_REFERENCE);
6906 OMP_CLAUSE_DECL (c2) = t;
6907 OMP_CLAUSE_SIZE (c2) = size_zero_node;
6908 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
6909 OMP_CLAUSE_CHAIN (c) = c2;
6910 OMP_CLAUSE_DECL (c) = build_simple_mem_ref (t);
6911 if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
6912 OMP_CLAUSE_SIZE (c)
6913 = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t)));
6914 c = c2;
6917 break;
6919 case OMP_CLAUSE_TO_DECLARE:
6920 case OMP_CLAUSE_LINK:
6921 t = OMP_CLAUSE_DECL (c);
6922 if (TREE_CODE (t) == FUNCTION_DECL
6923 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
6925 else if (!VAR_P (t))
6927 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
6929 if (TREE_CODE (t) == TEMPLATE_ID_EXPR)
6930 error_at (OMP_CLAUSE_LOCATION (c),
6931 "template %qE in clause %qs", t,
6932 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6933 else if (really_overloaded_fn (t))
6934 error_at (OMP_CLAUSE_LOCATION (c),
6935 "overloaded function name %qE in clause %qs", t,
6936 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6937 else
6938 error_at (OMP_CLAUSE_LOCATION (c),
6939 "%qE is neither a variable nor a function name "
6940 "in clause %qs", t,
6941 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6943 else
6944 error_at (OMP_CLAUSE_LOCATION (c),
6945 "%qE is not a variable in clause %qs", t,
6946 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6947 remove = true;
6949 else if (DECL_THREAD_LOCAL_P (t))
6951 error_at (OMP_CLAUSE_LOCATION (c),
6952 "%qD is threadprivate variable in %qs clause", t,
6953 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6954 remove = true;
6956 else if (!cp_omp_mappable_type (TREE_TYPE (t)))
6958 error_at (OMP_CLAUSE_LOCATION (c),
6959 "%qD does not have a mappable type in %qs clause", t,
6960 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6961 remove = true;
6963 if (remove)
6964 break;
6965 if (bitmap_bit_p (&generic_head, DECL_UID (t)))
6967 error_at (OMP_CLAUSE_LOCATION (c),
6968 "%qE appears more than once on the same "
6969 "%<declare target%> directive", t);
6970 remove = true;
6972 else
6973 bitmap_set_bit (&generic_head, DECL_UID (t));
6974 break;
6976 case OMP_CLAUSE_UNIFORM:
6977 t = OMP_CLAUSE_DECL (c);
6978 if (TREE_CODE (t) != PARM_DECL)
6980 if (processing_template_decl)
6981 break;
6982 if (DECL_P (t))
6983 error ("%qD is not an argument in %<uniform%> clause", t);
6984 else
6985 error ("%qE is not an argument in %<uniform%> clause", t);
6986 remove = true;
6987 break;
6989 /* map_head bitmap is used as uniform_head if declare_simd. */
6990 bitmap_set_bit (&map_head, DECL_UID (t));
6991 goto check_dup_generic;
6993 case OMP_CLAUSE_GRAINSIZE:
6994 t = OMP_CLAUSE_GRAINSIZE_EXPR (c);
6995 if (t == error_mark_node)
6996 remove = true;
6997 else if (!type_dependent_expression_p (t)
6998 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
7000 error ("%<grainsize%> expression must be integral");
7001 remove = true;
7003 else
7005 t = mark_rvalue_use (t);
7006 if (!processing_template_decl)
7008 t = maybe_constant_value (t);
7009 if (TREE_CODE (t) == INTEGER_CST
7010 && tree_int_cst_sgn (t) != 1)
7012 warning_at (OMP_CLAUSE_LOCATION (c), 0,
7013 "%<grainsize%> value must be positive");
7014 t = integer_one_node;
7016 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7018 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
7020 break;
7022 case OMP_CLAUSE_PRIORITY:
7023 t = OMP_CLAUSE_PRIORITY_EXPR (c);
7024 if (t == error_mark_node)
7025 remove = true;
7026 else if (!type_dependent_expression_p (t)
7027 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
7029 error ("%<priority%> expression must be integral");
7030 remove = true;
7032 else
7034 t = mark_rvalue_use (t);
7035 if (!processing_template_decl)
7037 t = maybe_constant_value (t);
7038 if (TREE_CODE (t) == INTEGER_CST
7039 && tree_int_cst_sgn (t) == -1)
7041 warning_at (OMP_CLAUSE_LOCATION (c), 0,
7042 "%<priority%> value must be non-negative");
7043 t = integer_one_node;
7045 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7047 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
7049 break;
7051 case OMP_CLAUSE_HINT:
7052 t = OMP_CLAUSE_HINT_EXPR (c);
7053 if (t == error_mark_node)
7054 remove = true;
7055 else if (!type_dependent_expression_p (t)
7056 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
7058 error ("%<num_tasks%> expression must be integral");
7059 remove = true;
7061 else
7063 t = mark_rvalue_use (t);
7064 if (!processing_template_decl)
7066 t = maybe_constant_value (t);
7067 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7069 OMP_CLAUSE_HINT_EXPR (c) = t;
7071 break;
7073 case OMP_CLAUSE_IS_DEVICE_PTR:
7074 case OMP_CLAUSE_USE_DEVICE_PTR:
7075 field_ok = (ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP;
7076 t = OMP_CLAUSE_DECL (c);
7077 if (!type_dependent_expression_p (t))
7079 tree type = TREE_TYPE (t);
7080 if (!TYPE_PTR_P (type)
7081 && TREE_CODE (type) != ARRAY_TYPE
7082 && (!TYPE_REF_P (type)
7083 || (!TYPE_PTR_P (TREE_TYPE (type))
7084 && TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE)))
7086 error_at (OMP_CLAUSE_LOCATION (c),
7087 "%qs variable is neither a pointer, nor an array "
7088 "nor reference to pointer or array",
7089 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7090 remove = true;
7093 goto check_dup_generic;
7095 case OMP_CLAUSE_NOWAIT:
7096 case OMP_CLAUSE_DEFAULT:
7097 case OMP_CLAUSE_UNTIED:
7098 case OMP_CLAUSE_COLLAPSE:
7099 case OMP_CLAUSE_MERGEABLE:
7100 case OMP_CLAUSE_PARALLEL:
7101 case OMP_CLAUSE_FOR:
7102 case OMP_CLAUSE_SECTIONS:
7103 case OMP_CLAUSE_TASKGROUP:
7104 case OMP_CLAUSE_PROC_BIND:
7105 case OMP_CLAUSE_NOGROUP:
7106 case OMP_CLAUSE_THREADS:
7107 case OMP_CLAUSE_SIMD:
7108 case OMP_CLAUSE_DEFAULTMAP:
7109 case OMP_CLAUSE_AUTO:
7110 case OMP_CLAUSE_INDEPENDENT:
7111 case OMP_CLAUSE_SEQ:
7112 case OMP_CLAUSE_IF_PRESENT:
7113 case OMP_CLAUSE_FINALIZE:
7114 break;
7116 case OMP_CLAUSE_TILE:
7117 for (tree list = OMP_CLAUSE_TILE_LIST (c); !remove && list;
7118 list = TREE_CHAIN (list))
7120 t = TREE_VALUE (list);
7122 if (t == error_mark_node)
7123 remove = true;
7124 else if (!type_dependent_expression_p (t)
7125 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
7127 error_at (OMP_CLAUSE_LOCATION (c),
7128 "%<tile%> argument needs integral type");
7129 remove = true;
7131 else
7133 t = mark_rvalue_use (t);
7134 if (!processing_template_decl)
7136 /* Zero is used to indicate '*', we permit you
7137 to get there via an ICE of value zero. */
7138 t = maybe_constant_value (t);
7139 if (!tree_fits_shwi_p (t)
7140 || tree_to_shwi (t) < 0)
7142 error_at (OMP_CLAUSE_LOCATION (c),
7143 "%<tile%> argument needs positive "
7144 "integral constant");
7145 remove = true;
7147 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7151 /* Update list item. */
7152 TREE_VALUE (list) = t;
7154 break;
7156 case OMP_CLAUSE_ORDERED:
7157 ordered_seen = true;
7158 break;
7160 case OMP_CLAUSE_INBRANCH:
7161 case OMP_CLAUSE_NOTINBRANCH:
7162 if (branch_seen)
7164 error ("%<inbranch%> clause is incompatible with "
7165 "%<notinbranch%>");
7166 remove = true;
7168 branch_seen = true;
7169 break;
7171 default:
7172 gcc_unreachable ();
7175 if (remove)
7176 *pc = OMP_CLAUSE_CHAIN (c);
7177 else
7178 pc = &OMP_CLAUSE_CHAIN (c);
7181 for (pc = &clauses, c = clauses; c ; c = *pc)
7183 enum omp_clause_code c_kind = OMP_CLAUSE_CODE (c);
7184 bool remove = false;
7185 bool need_complete_type = false;
7186 bool need_default_ctor = false;
7187 bool need_copy_ctor = false;
7188 bool need_copy_assignment = false;
7189 bool need_implicitly_determined = false;
7190 bool need_dtor = false;
7191 tree type, inner_type;
7193 switch (c_kind)
7195 case OMP_CLAUSE_SHARED:
7196 need_implicitly_determined = true;
7197 break;
7198 case OMP_CLAUSE_PRIVATE:
7199 need_complete_type = true;
7200 need_default_ctor = true;
7201 need_dtor = true;
7202 need_implicitly_determined = true;
7203 break;
7204 case OMP_CLAUSE_FIRSTPRIVATE:
7205 need_complete_type = true;
7206 need_copy_ctor = true;
7207 need_dtor = true;
7208 need_implicitly_determined = true;
7209 break;
7210 case OMP_CLAUSE_LASTPRIVATE:
7211 need_complete_type = true;
7212 need_copy_assignment = true;
7213 need_implicitly_determined = true;
7214 break;
7215 case OMP_CLAUSE_REDUCTION:
7216 need_implicitly_determined = true;
7217 break;
7218 case OMP_CLAUSE_LINEAR:
7219 if (ort != C_ORT_OMP_DECLARE_SIMD)
7220 need_implicitly_determined = true;
7221 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c)
7222 && !bitmap_bit_p (&map_head,
7223 DECL_UID (OMP_CLAUSE_LINEAR_STEP (c))))
7225 error_at (OMP_CLAUSE_LOCATION (c),
7226 "%<linear%> clause step is a parameter %qD not "
7227 "specified in %<uniform%> clause",
7228 OMP_CLAUSE_LINEAR_STEP (c));
7229 *pc = OMP_CLAUSE_CHAIN (c);
7230 continue;
7232 break;
7233 case OMP_CLAUSE_COPYPRIVATE:
7234 need_copy_assignment = true;
7235 break;
7236 case OMP_CLAUSE_COPYIN:
7237 need_copy_assignment = true;
7238 break;
7239 case OMP_CLAUSE_SIMDLEN:
7240 if (safelen
7241 && !processing_template_decl
7242 && tree_int_cst_lt (OMP_CLAUSE_SAFELEN_EXPR (safelen),
7243 OMP_CLAUSE_SIMDLEN_EXPR (c)))
7245 error_at (OMP_CLAUSE_LOCATION (c),
7246 "%<simdlen%> clause value is bigger than "
7247 "%<safelen%> clause value");
7248 OMP_CLAUSE_SIMDLEN_EXPR (c)
7249 = OMP_CLAUSE_SAFELEN_EXPR (safelen);
7251 pc = &OMP_CLAUSE_CHAIN (c);
7252 continue;
7253 case OMP_CLAUSE_SCHEDULE:
7254 if (ordered_seen
7255 && (OMP_CLAUSE_SCHEDULE_KIND (c)
7256 & OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
7258 error_at (OMP_CLAUSE_LOCATION (c),
7259 "%<nonmonotonic%> schedule modifier specified "
7260 "together with %<ordered%> clause");
7261 OMP_CLAUSE_SCHEDULE_KIND (c)
7262 = (enum omp_clause_schedule_kind)
7263 (OMP_CLAUSE_SCHEDULE_KIND (c)
7264 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
7266 pc = &OMP_CLAUSE_CHAIN (c);
7267 continue;
7268 case OMP_CLAUSE_NOWAIT:
7269 if (copyprivate_seen)
7271 error_at (OMP_CLAUSE_LOCATION (c),
7272 "%<nowait%> clause must not be used together "
7273 "with %<copyprivate%>");
7274 *pc = OMP_CLAUSE_CHAIN (c);
7275 continue;
7277 /* FALLTHRU */
7278 default:
7279 pc = &OMP_CLAUSE_CHAIN (c);
7280 continue;
7283 t = OMP_CLAUSE_DECL (c);
7284 if (processing_template_decl
7285 && !VAR_P (t) && TREE_CODE (t) != PARM_DECL)
7287 pc = &OMP_CLAUSE_CHAIN (c);
7288 continue;
7291 switch (c_kind)
7293 case OMP_CLAUSE_LASTPRIVATE:
7294 if (!bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
7296 need_default_ctor = true;
7297 need_dtor = true;
7299 break;
7301 case OMP_CLAUSE_REDUCTION:
7302 if (finish_omp_reduction_clause (c, &need_default_ctor,
7303 &need_dtor))
7304 remove = true;
7305 else
7306 t = OMP_CLAUSE_DECL (c);
7307 break;
7309 case OMP_CLAUSE_COPYIN:
7310 if (!VAR_P (t) || !CP_DECL_THREAD_LOCAL_P (t))
7312 error ("%qE must be %<threadprivate%> for %<copyin%>", t);
7313 remove = true;
7315 break;
7317 default:
7318 break;
7321 if (need_complete_type || need_copy_assignment)
7323 t = require_complete_type (t);
7324 if (t == error_mark_node)
7325 remove = true;
7326 else if (TYPE_REF_P (TREE_TYPE (t))
7327 && !complete_type_or_else (TREE_TYPE (TREE_TYPE (t)), t))
7328 remove = true;
7330 if (need_implicitly_determined)
7332 const char *share_name = NULL;
7334 if (VAR_P (t) && CP_DECL_THREAD_LOCAL_P (t))
7335 share_name = "threadprivate";
7336 else switch (cxx_omp_predetermined_sharing_1 (t))
7338 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
7339 break;
7340 case OMP_CLAUSE_DEFAULT_SHARED:
7341 /* const vars may be specified in firstprivate clause. */
7342 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
7343 && cxx_omp_const_qual_no_mutable (t))
7344 break;
7345 share_name = "shared";
7346 break;
7347 case OMP_CLAUSE_DEFAULT_PRIVATE:
7348 share_name = "private";
7349 break;
7350 default:
7351 gcc_unreachable ();
7353 if (share_name)
7355 error ("%qE is predetermined %qs for %qs",
7356 omp_clause_printable_decl (t), share_name,
7357 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7358 remove = true;
7362 /* We're interested in the base element, not arrays. */
7363 inner_type = type = TREE_TYPE (t);
7364 if ((need_complete_type
7365 || need_copy_assignment
7366 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
7367 && TYPE_REF_P (inner_type))
7368 inner_type = TREE_TYPE (inner_type);
7369 while (TREE_CODE (inner_type) == ARRAY_TYPE)
7370 inner_type = TREE_TYPE (inner_type);
7372 /* Check for special function availability by building a call to one.
7373 Save the results, because later we won't be in the right context
7374 for making these queries. */
7375 if (CLASS_TYPE_P (inner_type)
7376 && COMPLETE_TYPE_P (inner_type)
7377 && (need_default_ctor || need_copy_ctor
7378 || need_copy_assignment || need_dtor)
7379 && !type_dependent_expression_p (t)
7380 && cxx_omp_create_clause_info (c, inner_type, need_default_ctor,
7381 need_copy_ctor, need_copy_assignment,
7382 need_dtor))
7383 remove = true;
7385 if (!remove
7386 && c_kind == OMP_CLAUSE_SHARED
7387 && processing_template_decl)
7389 t = omp_clause_decl_field (OMP_CLAUSE_DECL (c));
7390 if (t)
7391 OMP_CLAUSE_DECL (c) = t;
7394 if (remove)
7395 *pc = OMP_CLAUSE_CHAIN (c);
7396 else
7397 pc = &OMP_CLAUSE_CHAIN (c);
7400 bitmap_obstack_release (NULL);
7401 return clauses;
7404 /* Start processing OpenMP clauses that can include any
7405 privatization clauses for non-static data members. */
7407 tree
7408 push_omp_privatization_clauses (bool ignore_next)
7410 if (omp_private_member_ignore_next)
7412 omp_private_member_ignore_next = ignore_next;
7413 return NULL_TREE;
7415 omp_private_member_ignore_next = ignore_next;
7416 if (omp_private_member_map)
7417 omp_private_member_vec.safe_push (error_mark_node);
7418 return push_stmt_list ();
7421 /* Revert remapping of any non-static data members since
7422 the last push_omp_privatization_clauses () call. */
7424 void
7425 pop_omp_privatization_clauses (tree stmt)
7427 if (stmt == NULL_TREE)
7428 return;
7429 stmt = pop_stmt_list (stmt);
7430 if (omp_private_member_map)
7432 while (!omp_private_member_vec.is_empty ())
7434 tree t = omp_private_member_vec.pop ();
7435 if (t == error_mark_node)
7437 add_stmt (stmt);
7438 return;
7440 bool no_decl_expr = t == integer_zero_node;
7441 if (no_decl_expr)
7442 t = omp_private_member_vec.pop ();
7443 tree *v = omp_private_member_map->get (t);
7444 gcc_assert (v);
7445 if (!no_decl_expr)
7446 add_decl_expr (*v);
7447 omp_private_member_map->remove (t);
7449 delete omp_private_member_map;
7450 omp_private_member_map = NULL;
7452 add_stmt (stmt);
7455 /* Remember OpenMP privatization clauses mapping and clear it.
7456 Used for lambdas. */
7458 void
7459 save_omp_privatization_clauses (vec<tree> &save)
7461 save = vNULL;
7462 if (omp_private_member_ignore_next)
7463 save.safe_push (integer_one_node);
7464 omp_private_member_ignore_next = false;
7465 if (!omp_private_member_map)
7466 return;
7468 while (!omp_private_member_vec.is_empty ())
7470 tree t = omp_private_member_vec.pop ();
7471 if (t == error_mark_node)
7473 save.safe_push (t);
7474 continue;
7476 tree n = t;
7477 if (t == integer_zero_node)
7478 t = omp_private_member_vec.pop ();
7479 tree *v = omp_private_member_map->get (t);
7480 gcc_assert (v);
7481 save.safe_push (*v);
7482 save.safe_push (t);
7483 if (n != t)
7484 save.safe_push (n);
7486 delete omp_private_member_map;
7487 omp_private_member_map = NULL;
7490 /* Restore OpenMP privatization clauses mapping saved by the
7491 above function. */
7493 void
7494 restore_omp_privatization_clauses (vec<tree> &save)
7496 gcc_assert (omp_private_member_vec.is_empty ());
7497 omp_private_member_ignore_next = false;
7498 if (save.is_empty ())
7499 return;
7500 if (save.length () == 1 && save[0] == integer_one_node)
7502 omp_private_member_ignore_next = true;
7503 save.release ();
7504 return;
7507 omp_private_member_map = new hash_map <tree, tree>;
7508 while (!save.is_empty ())
7510 tree t = save.pop ();
7511 tree n = t;
7512 if (t != error_mark_node)
7514 if (t == integer_one_node)
7516 omp_private_member_ignore_next = true;
7517 gcc_assert (save.is_empty ());
7518 break;
7520 if (t == integer_zero_node)
7521 t = save.pop ();
7522 tree &v = omp_private_member_map->get_or_insert (t);
7523 v = save.pop ();
7525 omp_private_member_vec.safe_push (t);
7526 if (n != t)
7527 omp_private_member_vec.safe_push (n);
7529 save.release ();
7532 /* For all variables in the tree_list VARS, mark them as thread local. */
7534 void
7535 finish_omp_threadprivate (tree vars)
7537 tree t;
7539 /* Mark every variable in VARS to be assigned thread local storage. */
7540 for (t = vars; t; t = TREE_CHAIN (t))
7542 tree v = TREE_PURPOSE (t);
7544 if (error_operand_p (v))
7546 else if (!VAR_P (v))
7547 error ("%<threadprivate%> %qD is not file, namespace "
7548 "or block scope variable", v);
7549 /* If V had already been marked threadprivate, it doesn't matter
7550 whether it had been used prior to this point. */
7551 else if (TREE_USED (v)
7552 && (DECL_LANG_SPECIFIC (v) == NULL
7553 || !CP_DECL_THREADPRIVATE_P (v)))
7554 error ("%qE declared %<threadprivate%> after first use", v);
7555 else if (! TREE_STATIC (v) && ! DECL_EXTERNAL (v))
7556 error ("automatic variable %qE cannot be %<threadprivate%>", v);
7557 else if (! COMPLETE_TYPE_P (complete_type (TREE_TYPE (v))))
7558 error ("%<threadprivate%> %qE has incomplete type", v);
7559 else if (TREE_STATIC (v) && TYPE_P (CP_DECL_CONTEXT (v))
7560 && CP_DECL_CONTEXT (v) != current_class_type)
7561 error ("%<threadprivate%> %qE directive not "
7562 "in %qT definition", v, CP_DECL_CONTEXT (v));
7563 else
7565 /* Allocate a LANG_SPECIFIC structure for V, if needed. */
7566 if (DECL_LANG_SPECIFIC (v) == NULL)
7568 retrofit_lang_decl (v);
7570 /* Make sure that DECL_DISCRIMINATOR_P continues to be true
7571 after the allocation of the lang_decl structure. */
7572 if (DECL_DISCRIMINATOR_P (v))
7573 DECL_LANG_SPECIFIC (v)->u.base.u2sel = 1;
7576 if (! CP_DECL_THREAD_LOCAL_P (v))
7578 CP_DECL_THREAD_LOCAL_P (v) = true;
7579 set_decl_tls_model (v, decl_default_tls_model (v));
7580 /* If rtl has been already set for this var, call
7581 make_decl_rtl once again, so that encode_section_info
7582 has a chance to look at the new decl flags. */
7583 if (DECL_RTL_SET_P (v))
7584 make_decl_rtl (v);
7586 CP_DECL_THREADPRIVATE_P (v) = 1;
7591 /* Build an OpenMP structured block. */
7593 tree
7594 begin_omp_structured_block (void)
7596 return do_pushlevel (sk_omp);
7599 tree
7600 finish_omp_structured_block (tree block)
7602 return do_poplevel (block);
7605 /* Similarly, except force the retention of the BLOCK. */
7607 tree
7608 begin_omp_parallel (void)
7610 keep_next_level (true);
7611 return begin_omp_structured_block ();
7614 /* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
7615 statement. */
7617 tree
7618 finish_oacc_data (tree clauses, tree block)
7620 tree stmt;
7622 block = finish_omp_structured_block (block);
7624 stmt = make_node (OACC_DATA);
7625 TREE_TYPE (stmt) = void_type_node;
7626 OACC_DATA_CLAUSES (stmt) = clauses;
7627 OACC_DATA_BODY (stmt) = block;
7629 return add_stmt (stmt);
7632 /* Generate OACC_HOST_DATA, with CLAUSES and BLOCK as its compound
7633 statement. */
7635 tree
7636 finish_oacc_host_data (tree clauses, tree block)
7638 tree stmt;
7640 block = finish_omp_structured_block (block);
7642 stmt = make_node (OACC_HOST_DATA);
7643 TREE_TYPE (stmt) = void_type_node;
7644 OACC_HOST_DATA_CLAUSES (stmt) = clauses;
7645 OACC_HOST_DATA_BODY (stmt) = block;
7647 return add_stmt (stmt);
7650 /* Generate OMP construct CODE, with BODY and CLAUSES as its compound
7651 statement. */
7653 tree
7654 finish_omp_construct (enum tree_code code, tree body, tree clauses)
7656 body = finish_omp_structured_block (body);
7658 tree stmt = make_node (code);
7659 TREE_TYPE (stmt) = void_type_node;
7660 OMP_BODY (stmt) = body;
7661 OMP_CLAUSES (stmt) = clauses;
7663 return add_stmt (stmt);
7666 tree
7667 finish_omp_parallel (tree clauses, tree body)
7669 tree stmt;
7671 body = finish_omp_structured_block (body);
7673 stmt = make_node (OMP_PARALLEL);
7674 TREE_TYPE (stmt) = void_type_node;
7675 OMP_PARALLEL_CLAUSES (stmt) = clauses;
7676 OMP_PARALLEL_BODY (stmt) = body;
7678 return add_stmt (stmt);
7681 tree
7682 begin_omp_task (void)
7684 keep_next_level (true);
7685 return begin_omp_structured_block ();
7688 tree
7689 finish_omp_task (tree clauses, tree body)
7691 tree stmt;
7693 body = finish_omp_structured_block (body);
7695 stmt = make_node (OMP_TASK);
7696 TREE_TYPE (stmt) = void_type_node;
7697 OMP_TASK_CLAUSES (stmt) = clauses;
7698 OMP_TASK_BODY (stmt) = body;
7700 return add_stmt (stmt);
7703 /* Helper function for finish_omp_for. Convert Ith random access iterator
7704 into integral iterator. Return FALSE if successful. */
7706 static bool
7707 handle_omp_for_class_iterator (int i, location_t locus, enum tree_code code,
7708 tree declv, tree orig_declv, tree initv,
7709 tree condv, tree incrv, tree *body,
7710 tree *pre_body, tree &clauses,
7711 int collapse, int ordered)
7713 tree diff, iter_init, iter_incr = NULL, last;
7714 tree incr_var = NULL, orig_pre_body, orig_body, c;
7715 tree decl = TREE_VEC_ELT (declv, i);
7716 tree init = TREE_VEC_ELT (initv, i);
7717 tree cond = TREE_VEC_ELT (condv, i);
7718 tree incr = TREE_VEC_ELT (incrv, i);
7719 tree iter = decl;
7720 location_t elocus = locus;
7722 if (init && EXPR_HAS_LOCATION (init))
7723 elocus = EXPR_LOCATION (init);
7725 cond = cp_fully_fold (cond);
7726 switch (TREE_CODE (cond))
7728 case GT_EXPR:
7729 case GE_EXPR:
7730 case LT_EXPR:
7731 case LE_EXPR:
7732 case NE_EXPR:
7733 if (TREE_OPERAND (cond, 1) == iter)
7734 cond = build2 (swap_tree_comparison (TREE_CODE (cond)),
7735 TREE_TYPE (cond), iter, TREE_OPERAND (cond, 0));
7736 if (TREE_OPERAND (cond, 0) != iter)
7737 cond = error_mark_node;
7738 else
7740 tree tem = build_x_binary_op (EXPR_LOCATION (cond),
7741 TREE_CODE (cond),
7742 iter, ERROR_MARK,
7743 TREE_OPERAND (cond, 1), ERROR_MARK,
7744 NULL, tf_warning_or_error);
7745 if (error_operand_p (tem))
7746 return true;
7748 break;
7749 default:
7750 cond = error_mark_node;
7751 break;
7753 if (cond == error_mark_node)
7755 error_at (elocus, "invalid controlling predicate");
7756 return true;
7758 diff = build_x_binary_op (elocus, MINUS_EXPR, TREE_OPERAND (cond, 1),
7759 ERROR_MARK, iter, ERROR_MARK, NULL,
7760 tf_warning_or_error);
7761 diff = cp_fully_fold (diff);
7762 if (error_operand_p (diff))
7763 return true;
7764 if (TREE_CODE (TREE_TYPE (diff)) != INTEGER_TYPE)
7766 error_at (elocus, "difference between %qE and %qD does not have integer type",
7767 TREE_OPERAND (cond, 1), iter);
7768 return true;
7770 if (!c_omp_check_loop_iv_exprs (locus, orig_declv,
7771 TREE_VEC_ELT (declv, i), NULL_TREE,
7772 cond, cp_walk_subtrees))
7773 return true;
7775 switch (TREE_CODE (incr))
7777 case PREINCREMENT_EXPR:
7778 case PREDECREMENT_EXPR:
7779 case POSTINCREMENT_EXPR:
7780 case POSTDECREMENT_EXPR:
7781 if (TREE_OPERAND (incr, 0) != iter)
7783 incr = error_mark_node;
7784 break;
7786 iter_incr = build_x_unary_op (EXPR_LOCATION (incr),
7787 TREE_CODE (incr), iter,
7788 tf_warning_or_error);
7789 if (error_operand_p (iter_incr))
7790 return true;
7791 else if (TREE_CODE (incr) == PREINCREMENT_EXPR
7792 || TREE_CODE (incr) == POSTINCREMENT_EXPR)
7793 incr = integer_one_node;
7794 else
7795 incr = integer_minus_one_node;
7796 break;
7797 case MODIFY_EXPR:
7798 if (TREE_OPERAND (incr, 0) != iter)
7799 incr = error_mark_node;
7800 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
7801 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
7803 tree rhs = TREE_OPERAND (incr, 1);
7804 if (TREE_OPERAND (rhs, 0) == iter)
7806 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (rhs, 1)))
7807 != INTEGER_TYPE)
7808 incr = error_mark_node;
7809 else
7811 iter_incr = build_x_modify_expr (EXPR_LOCATION (rhs),
7812 iter, TREE_CODE (rhs),
7813 TREE_OPERAND (rhs, 1),
7814 tf_warning_or_error);
7815 if (error_operand_p (iter_incr))
7816 return true;
7817 incr = TREE_OPERAND (rhs, 1);
7818 incr = cp_convert (TREE_TYPE (diff), incr,
7819 tf_warning_or_error);
7820 if (TREE_CODE (rhs) == MINUS_EXPR)
7822 incr = build1 (NEGATE_EXPR, TREE_TYPE (diff), incr);
7823 incr = fold_simple (incr);
7825 if (TREE_CODE (incr) != INTEGER_CST
7826 && (TREE_CODE (incr) != NOP_EXPR
7827 || (TREE_CODE (TREE_OPERAND (incr, 0))
7828 != INTEGER_CST)))
7829 iter_incr = NULL;
7832 else if (TREE_OPERAND (rhs, 1) == iter)
7834 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (rhs, 0))) != INTEGER_TYPE
7835 || TREE_CODE (rhs) != PLUS_EXPR)
7836 incr = error_mark_node;
7837 else
7839 iter_incr = build_x_binary_op (EXPR_LOCATION (rhs),
7840 PLUS_EXPR,
7841 TREE_OPERAND (rhs, 0),
7842 ERROR_MARK, iter,
7843 ERROR_MARK, NULL,
7844 tf_warning_or_error);
7845 if (error_operand_p (iter_incr))
7846 return true;
7847 iter_incr = build_x_modify_expr (EXPR_LOCATION (rhs),
7848 iter, NOP_EXPR,
7849 iter_incr,
7850 tf_warning_or_error);
7851 if (error_operand_p (iter_incr))
7852 return true;
7853 incr = TREE_OPERAND (rhs, 0);
7854 iter_incr = NULL;
7857 else
7858 incr = error_mark_node;
7860 else
7861 incr = error_mark_node;
7862 break;
7863 default:
7864 incr = error_mark_node;
7865 break;
7868 if (incr == error_mark_node)
7870 error_at (elocus, "invalid increment expression");
7871 return true;
7874 incr = cp_convert (TREE_TYPE (diff), incr, tf_warning_or_error);
7875 bool taskloop_iv_seen = false;
7876 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
7877 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
7878 && OMP_CLAUSE_DECL (c) == iter)
7880 if (code == OMP_TASKLOOP)
7882 taskloop_iv_seen = true;
7883 OMP_CLAUSE_LASTPRIVATE_TASKLOOP_IV (c) = 1;
7885 break;
7887 else if (code == OMP_TASKLOOP
7888 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
7889 && OMP_CLAUSE_DECL (c) == iter)
7891 taskloop_iv_seen = true;
7892 OMP_CLAUSE_PRIVATE_TASKLOOP_IV (c) = 1;
7895 decl = create_temporary_var (TREE_TYPE (diff));
7896 pushdecl (decl);
7897 add_decl_expr (decl);
7898 last = create_temporary_var (TREE_TYPE (diff));
7899 pushdecl (last);
7900 add_decl_expr (last);
7901 if (c && iter_incr == NULL && TREE_CODE (incr) != INTEGER_CST
7902 && (!ordered || (i < collapse && collapse > 1)))
7904 incr_var = create_temporary_var (TREE_TYPE (diff));
7905 pushdecl (incr_var);
7906 add_decl_expr (incr_var);
7908 gcc_assert (stmts_are_full_exprs_p ());
7909 tree diffvar = NULL_TREE;
7910 if (code == OMP_TASKLOOP)
7912 if (!taskloop_iv_seen)
7914 tree ivc = build_omp_clause (locus, OMP_CLAUSE_FIRSTPRIVATE);
7915 OMP_CLAUSE_DECL (ivc) = iter;
7916 cxx_omp_finish_clause (ivc, NULL);
7917 OMP_CLAUSE_CHAIN (ivc) = clauses;
7918 clauses = ivc;
7920 tree lvc = build_omp_clause (locus, OMP_CLAUSE_FIRSTPRIVATE);
7921 OMP_CLAUSE_DECL (lvc) = last;
7922 OMP_CLAUSE_CHAIN (lvc) = clauses;
7923 clauses = lvc;
7924 diffvar = create_temporary_var (TREE_TYPE (diff));
7925 pushdecl (diffvar);
7926 add_decl_expr (diffvar);
7929 orig_pre_body = *pre_body;
7930 *pre_body = push_stmt_list ();
7931 if (orig_pre_body)
7932 add_stmt (orig_pre_body);
7933 if (init != NULL)
7934 finish_expr_stmt (build_x_modify_expr (elocus,
7935 iter, NOP_EXPR, init,
7936 tf_warning_or_error));
7937 init = build_int_cst (TREE_TYPE (diff), 0);
7938 if (c && iter_incr == NULL
7939 && (!ordered || (i < collapse && collapse > 1)))
7941 if (incr_var)
7943 finish_expr_stmt (build_x_modify_expr (elocus,
7944 incr_var, NOP_EXPR,
7945 incr, tf_warning_or_error));
7946 incr = incr_var;
7948 iter_incr = build_x_modify_expr (elocus,
7949 iter, PLUS_EXPR, incr,
7950 tf_warning_or_error);
7952 if (c && ordered && i < collapse && collapse > 1)
7953 iter_incr = incr;
7954 finish_expr_stmt (build_x_modify_expr (elocus,
7955 last, NOP_EXPR, init,
7956 tf_warning_or_error));
7957 if (diffvar)
7959 finish_expr_stmt (build_x_modify_expr (elocus,
7960 diffvar, NOP_EXPR,
7961 diff, tf_warning_or_error));
7962 diff = diffvar;
7964 *pre_body = pop_stmt_list (*pre_body);
7966 cond = cp_build_binary_op (elocus,
7967 TREE_CODE (cond), decl, diff,
7968 tf_warning_or_error);
7969 incr = build_modify_expr (elocus, decl, NULL_TREE, PLUS_EXPR,
7970 elocus, incr, NULL_TREE);
7972 orig_body = *body;
7973 *body = push_stmt_list ();
7974 iter_init = build2 (MINUS_EXPR, TREE_TYPE (diff), decl, last);
7975 iter_init = build_x_modify_expr (elocus,
7976 iter, PLUS_EXPR, iter_init,
7977 tf_warning_or_error);
7978 if (iter_init != error_mark_node)
7979 iter_init = build1 (NOP_EXPR, void_type_node, iter_init);
7980 finish_expr_stmt (iter_init);
7981 finish_expr_stmt (build_x_modify_expr (elocus,
7982 last, NOP_EXPR, decl,
7983 tf_warning_or_error));
7984 add_stmt (orig_body);
7985 *body = pop_stmt_list (*body);
7987 if (c)
7989 OMP_CLAUSE_LASTPRIVATE_STMT (c) = push_stmt_list ();
7990 if (!ordered)
7991 finish_expr_stmt (iter_incr);
7992 else
7994 iter_init = decl;
7995 if (i < collapse && collapse > 1 && !error_operand_p (iter_incr))
7996 iter_init = build2 (PLUS_EXPR, TREE_TYPE (diff),
7997 iter_init, iter_incr);
7998 iter_init = build2 (MINUS_EXPR, TREE_TYPE (diff), iter_init, last);
7999 iter_init = build_x_modify_expr (elocus,
8000 iter, PLUS_EXPR, iter_init,
8001 tf_warning_or_error);
8002 if (iter_init != error_mark_node)
8003 iter_init = build1 (NOP_EXPR, void_type_node, iter_init);
8004 finish_expr_stmt (iter_init);
8006 OMP_CLAUSE_LASTPRIVATE_STMT (c)
8007 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (c));
8010 TREE_VEC_ELT (declv, i) = decl;
8011 TREE_VEC_ELT (initv, i) = init;
8012 TREE_VEC_ELT (condv, i) = cond;
8013 TREE_VEC_ELT (incrv, i) = incr;
8014 TREE_VEC_ELT (orig_declv, i)
8015 = tree_cons (TREE_VEC_ELT (orig_declv, i), last, NULL_TREE);
8017 return false;
8020 /* Build and validate an OMP_FOR statement. CLAUSES, BODY, COND, INCR
8021 are directly for their associated operands in the statement. DECL
8022 and INIT are a combo; if DECL is NULL then INIT ought to be a
8023 MODIFY_EXPR, and the DECL should be extracted. PRE_BODY are
8024 optional statements that need to go before the loop into its
8025 sk_omp scope. */
8027 tree
8028 finish_omp_for (location_t locus, enum tree_code code, tree declv,
8029 tree orig_declv, tree initv, tree condv, tree incrv,
8030 tree body, tree pre_body, vec<tree> *orig_inits, tree clauses)
8032 tree omp_for = NULL, orig_incr = NULL;
8033 tree decl = NULL, init, cond, incr;
8034 location_t elocus;
8035 int i;
8036 int collapse = 1;
8037 int ordered = 0;
8039 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (initv));
8040 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (condv));
8041 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (incrv));
8042 if (TREE_VEC_LENGTH (declv) > 1)
8044 tree c;
8046 c = omp_find_clause (clauses, OMP_CLAUSE_TILE);
8047 if (c)
8048 collapse = list_length (OMP_CLAUSE_TILE_LIST (c));
8049 else
8051 c = omp_find_clause (clauses, OMP_CLAUSE_COLLAPSE);
8052 if (c)
8053 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (c));
8054 if (collapse != TREE_VEC_LENGTH (declv))
8055 ordered = TREE_VEC_LENGTH (declv);
8058 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
8060 decl = TREE_VEC_ELT (declv, i);
8061 init = TREE_VEC_ELT (initv, i);
8062 cond = TREE_VEC_ELT (condv, i);
8063 incr = TREE_VEC_ELT (incrv, i);
8064 elocus = locus;
8066 if (decl == NULL)
8068 if (init != NULL)
8069 switch (TREE_CODE (init))
8071 case MODIFY_EXPR:
8072 decl = TREE_OPERAND (init, 0);
8073 init = TREE_OPERAND (init, 1);
8074 break;
8075 case MODOP_EXPR:
8076 if (TREE_CODE (TREE_OPERAND (init, 1)) == NOP_EXPR)
8078 decl = TREE_OPERAND (init, 0);
8079 init = TREE_OPERAND (init, 2);
8081 break;
8082 default:
8083 break;
8086 if (decl == NULL)
8088 error_at (locus,
8089 "expected iteration declaration or initialization");
8090 return NULL;
8094 if (init && EXPR_HAS_LOCATION (init))
8095 elocus = EXPR_LOCATION (init);
8097 if (cond == NULL)
8099 error_at (elocus, "missing controlling predicate");
8100 return NULL;
8103 if (incr == NULL)
8105 error_at (elocus, "missing increment expression");
8106 return NULL;
8109 TREE_VEC_ELT (declv, i) = decl;
8110 TREE_VEC_ELT (initv, i) = init;
8113 if (orig_inits)
8115 bool fail = false;
8116 tree orig_init;
8117 FOR_EACH_VEC_ELT (*orig_inits, i, orig_init)
8118 if (orig_init
8119 && !c_omp_check_loop_iv_exprs (locus, declv,
8120 TREE_VEC_ELT (declv, i), orig_init,
8121 NULL_TREE, cp_walk_subtrees))
8122 fail = true;
8123 if (fail)
8124 return NULL;
8127 if (dependent_omp_for_p (declv, initv, condv, incrv))
8129 tree stmt;
8131 stmt = make_node (code);
8133 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
8135 /* This is really just a place-holder. We'll be decomposing this
8136 again and going through the cp_build_modify_expr path below when
8137 we instantiate the thing. */
8138 TREE_VEC_ELT (initv, i)
8139 = build2 (MODIFY_EXPR, void_type_node, TREE_VEC_ELT (declv, i),
8140 TREE_VEC_ELT (initv, i));
8143 TREE_TYPE (stmt) = void_type_node;
8144 OMP_FOR_INIT (stmt) = initv;
8145 OMP_FOR_COND (stmt) = condv;
8146 OMP_FOR_INCR (stmt) = incrv;
8147 OMP_FOR_BODY (stmt) = body;
8148 OMP_FOR_PRE_BODY (stmt) = pre_body;
8149 OMP_FOR_CLAUSES (stmt) = clauses;
8151 SET_EXPR_LOCATION (stmt, locus);
8152 return add_stmt (stmt);
8155 if (!orig_declv)
8156 orig_declv = copy_node (declv);
8158 if (processing_template_decl)
8159 orig_incr = make_tree_vec (TREE_VEC_LENGTH (incrv));
8161 for (i = 0; i < TREE_VEC_LENGTH (declv); )
8163 decl = TREE_VEC_ELT (declv, i);
8164 init = TREE_VEC_ELT (initv, i);
8165 cond = TREE_VEC_ELT (condv, i);
8166 incr = TREE_VEC_ELT (incrv, i);
8167 if (orig_incr)
8168 TREE_VEC_ELT (orig_incr, i) = incr;
8169 elocus = locus;
8171 if (init && EXPR_HAS_LOCATION (init))
8172 elocus = EXPR_LOCATION (init);
8174 if (!DECL_P (decl))
8176 error_at (elocus, "expected iteration declaration or initialization");
8177 return NULL;
8180 if (incr && TREE_CODE (incr) == MODOP_EXPR)
8182 if (orig_incr)
8183 TREE_VEC_ELT (orig_incr, i) = incr;
8184 incr = cp_build_modify_expr (elocus, TREE_OPERAND (incr, 0),
8185 TREE_CODE (TREE_OPERAND (incr, 1)),
8186 TREE_OPERAND (incr, 2),
8187 tf_warning_or_error);
8190 if (CLASS_TYPE_P (TREE_TYPE (decl)))
8192 if (code == OMP_SIMD)
8194 error_at (elocus, "%<#pragma omp simd%> used with class "
8195 "iteration variable %qE", decl);
8196 return NULL;
8198 if (handle_omp_for_class_iterator (i, locus, code, declv, orig_declv,
8199 initv, condv, incrv, &body,
8200 &pre_body, clauses,
8201 collapse, ordered))
8202 return NULL;
8203 continue;
8206 if (!INTEGRAL_TYPE_P (TREE_TYPE (decl))
8207 && !TYPE_PTR_P (TREE_TYPE (decl)))
8209 error_at (elocus, "invalid type for iteration variable %qE", decl);
8210 return NULL;
8213 if (!processing_template_decl)
8215 init = fold_build_cleanup_point_expr (TREE_TYPE (init), init);
8216 init = cp_build_modify_expr (elocus, decl, NOP_EXPR, init,
8217 tf_warning_or_error);
8219 else
8220 init = build2 (MODIFY_EXPR, void_type_node, decl, init);
8221 if (cond
8222 && TREE_SIDE_EFFECTS (cond)
8223 && COMPARISON_CLASS_P (cond)
8224 && !processing_template_decl)
8226 tree t = TREE_OPERAND (cond, 0);
8227 if (TREE_SIDE_EFFECTS (t)
8228 && t != decl
8229 && (TREE_CODE (t) != NOP_EXPR
8230 || TREE_OPERAND (t, 0) != decl))
8231 TREE_OPERAND (cond, 0)
8232 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
8234 t = TREE_OPERAND (cond, 1);
8235 if (TREE_SIDE_EFFECTS (t)
8236 && t != decl
8237 && (TREE_CODE (t) != NOP_EXPR
8238 || TREE_OPERAND (t, 0) != decl))
8239 TREE_OPERAND (cond, 1)
8240 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
8242 if (decl == error_mark_node || init == error_mark_node)
8243 return NULL;
8245 TREE_VEC_ELT (declv, i) = decl;
8246 TREE_VEC_ELT (initv, i) = init;
8247 TREE_VEC_ELT (condv, i) = cond;
8248 TREE_VEC_ELT (incrv, i) = incr;
8249 i++;
8252 if (IS_EMPTY_STMT (pre_body))
8253 pre_body = NULL;
8255 omp_for = c_finish_omp_for (locus, code, declv, orig_declv, initv, condv,
8256 incrv, body, pre_body);
8258 /* Check for iterators appearing in lb, b or incr expressions. */
8259 if (omp_for && !c_omp_check_loop_iv (omp_for, orig_declv, cp_walk_subtrees))
8260 omp_for = NULL_TREE;
8262 if (omp_for == NULL)
8264 return NULL;
8267 add_stmt (omp_for);
8269 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INCR (omp_for)); i++)
8271 decl = TREE_OPERAND (TREE_VEC_ELT (OMP_FOR_INIT (omp_for), i), 0);
8272 incr = TREE_VEC_ELT (OMP_FOR_INCR (omp_for), i);
8274 if (TREE_CODE (incr) != MODIFY_EXPR)
8275 continue;
8277 if (TREE_SIDE_EFFECTS (TREE_OPERAND (incr, 1))
8278 && BINARY_CLASS_P (TREE_OPERAND (incr, 1))
8279 && !processing_template_decl)
8281 tree t = TREE_OPERAND (TREE_OPERAND (incr, 1), 0);
8282 if (TREE_SIDE_EFFECTS (t)
8283 && t != decl
8284 && (TREE_CODE (t) != NOP_EXPR
8285 || TREE_OPERAND (t, 0) != decl))
8286 TREE_OPERAND (TREE_OPERAND (incr, 1), 0)
8287 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
8289 t = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
8290 if (TREE_SIDE_EFFECTS (t)
8291 && t != decl
8292 && (TREE_CODE (t) != NOP_EXPR
8293 || TREE_OPERAND (t, 0) != decl))
8294 TREE_OPERAND (TREE_OPERAND (incr, 1), 1)
8295 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
8298 if (orig_incr)
8299 TREE_VEC_ELT (OMP_FOR_INCR (omp_for), i) = TREE_VEC_ELT (orig_incr, i);
8301 OMP_FOR_CLAUSES (omp_for) = clauses;
8303 /* For simd loops with non-static data member iterators, we could have added
8304 OMP_CLAUSE_LINEAR clauses without OMP_CLAUSE_LINEAR_STEP. As we know the
8305 step at this point, fill it in. */
8306 if (code == OMP_SIMD && !processing_template_decl
8307 && TREE_VEC_LENGTH (OMP_FOR_INCR (omp_for)) == 1)
8308 for (tree c = omp_find_clause (clauses, OMP_CLAUSE_LINEAR); c;
8309 c = omp_find_clause (OMP_CLAUSE_CHAIN (c), OMP_CLAUSE_LINEAR))
8310 if (OMP_CLAUSE_LINEAR_STEP (c) == NULL_TREE)
8312 decl = TREE_OPERAND (TREE_VEC_ELT (OMP_FOR_INIT (omp_for), 0), 0);
8313 gcc_assert (decl == OMP_CLAUSE_DECL (c));
8314 incr = TREE_VEC_ELT (OMP_FOR_INCR (omp_for), 0);
8315 tree step, stept;
8316 switch (TREE_CODE (incr))
8318 case PREINCREMENT_EXPR:
8319 case POSTINCREMENT_EXPR:
8320 /* c_omp_for_incr_canonicalize_ptr() should have been
8321 called to massage things appropriately. */
8322 gcc_assert (!INDIRECT_TYPE_P (TREE_TYPE (decl)));
8323 OMP_CLAUSE_LINEAR_STEP (c) = build_int_cst (TREE_TYPE (decl), 1);
8324 break;
8325 case PREDECREMENT_EXPR:
8326 case POSTDECREMENT_EXPR:
8327 /* c_omp_for_incr_canonicalize_ptr() should have been
8328 called to massage things appropriately. */
8329 gcc_assert (!INDIRECT_TYPE_P (TREE_TYPE (decl)));
8330 OMP_CLAUSE_LINEAR_STEP (c)
8331 = build_int_cst (TREE_TYPE (decl), -1);
8332 break;
8333 case MODIFY_EXPR:
8334 gcc_assert (TREE_OPERAND (incr, 0) == decl);
8335 incr = TREE_OPERAND (incr, 1);
8336 switch (TREE_CODE (incr))
8338 case PLUS_EXPR:
8339 if (TREE_OPERAND (incr, 1) == decl)
8340 step = TREE_OPERAND (incr, 0);
8341 else
8342 step = TREE_OPERAND (incr, 1);
8343 break;
8344 case MINUS_EXPR:
8345 case POINTER_PLUS_EXPR:
8346 gcc_assert (TREE_OPERAND (incr, 0) == decl);
8347 step = TREE_OPERAND (incr, 1);
8348 break;
8349 default:
8350 gcc_unreachable ();
8352 stept = TREE_TYPE (decl);
8353 if (INDIRECT_TYPE_P (stept))
8354 stept = sizetype;
8355 step = fold_convert (stept, step);
8356 if (TREE_CODE (incr) == MINUS_EXPR)
8357 step = fold_build1 (NEGATE_EXPR, stept, step);
8358 OMP_CLAUSE_LINEAR_STEP (c) = step;
8359 break;
8360 default:
8361 gcc_unreachable ();
8365 return omp_for;
8368 void
8369 finish_omp_atomic (enum tree_code code, enum tree_code opcode, tree lhs,
8370 tree rhs, tree v, tree lhs1, tree rhs1, bool seq_cst)
8372 tree orig_lhs;
8373 tree orig_rhs;
8374 tree orig_v;
8375 tree orig_lhs1;
8376 tree orig_rhs1;
8377 bool dependent_p;
8378 tree stmt;
8380 orig_lhs = lhs;
8381 orig_rhs = rhs;
8382 orig_v = v;
8383 orig_lhs1 = lhs1;
8384 orig_rhs1 = rhs1;
8385 dependent_p = false;
8386 stmt = NULL_TREE;
8388 /* Even in a template, we can detect invalid uses of the atomic
8389 pragma if neither LHS nor RHS is type-dependent. */
8390 if (processing_template_decl)
8392 dependent_p = (type_dependent_expression_p (lhs)
8393 || (rhs && type_dependent_expression_p (rhs))
8394 || (v && type_dependent_expression_p (v))
8395 || (lhs1 && type_dependent_expression_p (lhs1))
8396 || (rhs1 && type_dependent_expression_p (rhs1)));
8397 if (!dependent_p)
8399 lhs = build_non_dependent_expr (lhs);
8400 if (rhs)
8401 rhs = build_non_dependent_expr (rhs);
8402 if (v)
8403 v = build_non_dependent_expr (v);
8404 if (lhs1)
8405 lhs1 = build_non_dependent_expr (lhs1);
8406 if (rhs1)
8407 rhs1 = build_non_dependent_expr (rhs1);
8410 if (!dependent_p)
8412 bool swapped = false;
8413 if (rhs1 && cp_tree_equal (lhs, rhs))
8415 std::swap (rhs, rhs1);
8416 swapped = !commutative_tree_code (opcode);
8418 if (rhs1 && !cp_tree_equal (lhs, rhs1))
8420 if (code == OMP_ATOMIC)
8421 error ("%<#pragma omp atomic update%> uses two different "
8422 "expressions for memory");
8423 else
8424 error ("%<#pragma omp atomic capture%> uses two different "
8425 "expressions for memory");
8426 return;
8428 if (lhs1 && !cp_tree_equal (lhs, lhs1))
8430 if (code == OMP_ATOMIC)
8431 error ("%<#pragma omp atomic update%> uses two different "
8432 "expressions for memory");
8433 else
8434 error ("%<#pragma omp atomic capture%> uses two different "
8435 "expressions for memory");
8436 return;
8438 stmt = c_finish_omp_atomic (input_location, code, opcode, lhs, rhs,
8439 v, lhs1, rhs1, swapped, seq_cst,
8440 processing_template_decl != 0);
8441 if (stmt == error_mark_node)
8442 return;
8444 if (processing_template_decl)
8446 if (code == OMP_ATOMIC_READ)
8448 stmt = build_min_nt_loc (EXPR_LOCATION (orig_lhs),
8449 OMP_ATOMIC_READ, orig_lhs);
8450 OMP_ATOMIC_SEQ_CST (stmt) = seq_cst;
8451 stmt = build2 (MODIFY_EXPR, void_type_node, orig_v, stmt);
8453 else
8455 if (opcode == NOP_EXPR)
8456 stmt = build2 (MODIFY_EXPR, void_type_node, orig_lhs, orig_rhs);
8457 else
8458 stmt = build2 (opcode, void_type_node, orig_lhs, orig_rhs);
8459 if (orig_rhs1)
8460 stmt = build_min_nt_loc (EXPR_LOCATION (orig_rhs1),
8461 COMPOUND_EXPR, orig_rhs1, stmt);
8462 if (code != OMP_ATOMIC)
8464 stmt = build_min_nt_loc (EXPR_LOCATION (orig_lhs1),
8465 code, orig_lhs1, stmt);
8466 OMP_ATOMIC_SEQ_CST (stmt) = seq_cst;
8467 stmt = build2 (MODIFY_EXPR, void_type_node, orig_v, stmt);
8470 stmt = build2 (OMP_ATOMIC, void_type_node, integer_zero_node, stmt);
8471 OMP_ATOMIC_SEQ_CST (stmt) = seq_cst;
8473 finish_expr_stmt (stmt);
8476 void
8477 finish_omp_barrier (void)
8479 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_BARRIER);
8480 vec<tree, va_gc> *vec = make_tree_vector ();
8481 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
8482 release_tree_vector (vec);
8483 finish_expr_stmt (stmt);
8486 void
8487 finish_omp_flush (void)
8489 tree fn = builtin_decl_explicit (BUILT_IN_SYNC_SYNCHRONIZE);
8490 vec<tree, va_gc> *vec = make_tree_vector ();
8491 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
8492 release_tree_vector (vec);
8493 finish_expr_stmt (stmt);
8496 void
8497 finish_omp_taskwait (void)
8499 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_TASKWAIT);
8500 vec<tree, va_gc> *vec = make_tree_vector ();
8501 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
8502 release_tree_vector (vec);
8503 finish_expr_stmt (stmt);
8506 void
8507 finish_omp_taskyield (void)
8509 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_TASKYIELD);
8510 vec<tree, va_gc> *vec = make_tree_vector ();
8511 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
8512 release_tree_vector (vec);
8513 finish_expr_stmt (stmt);
8516 void
8517 finish_omp_cancel (tree clauses)
8519 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
8520 int mask = 0;
8521 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
8522 mask = 1;
8523 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
8524 mask = 2;
8525 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
8526 mask = 4;
8527 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
8528 mask = 8;
8529 else
8531 error ("%<#pragma omp cancel%> must specify one of "
8532 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> clauses");
8533 return;
8535 vec<tree, va_gc> *vec = make_tree_vector ();
8536 tree ifc = omp_find_clause (clauses, OMP_CLAUSE_IF);
8537 if (ifc != NULL_TREE)
8539 tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
8540 ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
8541 boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
8542 build_zero_cst (type));
8544 else
8545 ifc = boolean_true_node;
8546 vec->quick_push (build_int_cst (integer_type_node, mask));
8547 vec->quick_push (ifc);
8548 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
8549 release_tree_vector (vec);
8550 finish_expr_stmt (stmt);
8553 void
8554 finish_omp_cancellation_point (tree clauses)
8556 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
8557 int mask = 0;
8558 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
8559 mask = 1;
8560 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
8561 mask = 2;
8562 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
8563 mask = 4;
8564 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
8565 mask = 8;
8566 else
8568 error ("%<#pragma omp cancellation point%> must specify one of "
8569 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> clauses");
8570 return;
8572 vec<tree, va_gc> *vec
8573 = make_tree_vector_single (build_int_cst (integer_type_node, mask));
8574 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
8575 release_tree_vector (vec);
8576 finish_expr_stmt (stmt);
8579 /* Begin a __transaction_atomic or __transaction_relaxed statement.
8580 If PCOMPOUND is non-null, this is for a function-transaction-block, and we
8581 should create an extra compound stmt. */
8583 tree
8584 begin_transaction_stmt (location_t loc, tree *pcompound, int flags)
8586 tree r;
8588 if (pcompound)
8589 *pcompound = begin_compound_stmt (0);
8591 r = build_stmt (loc, TRANSACTION_EXPR, NULL_TREE);
8593 /* Only add the statement to the function if support enabled. */
8594 if (flag_tm)
8595 add_stmt (r);
8596 else
8597 error_at (loc, ((flags & TM_STMT_ATTR_RELAXED) != 0
8598 ? G_("%<__transaction_relaxed%> without "
8599 "transactional memory support enabled")
8600 : G_("%<__transaction_atomic%> without "
8601 "transactional memory support enabled")));
8603 TRANSACTION_EXPR_BODY (r) = push_stmt_list ();
8604 TREE_SIDE_EFFECTS (r) = 1;
8605 return r;
8608 /* End a __transaction_atomic or __transaction_relaxed statement.
8609 If COMPOUND_STMT is non-null, this is for a function-transaction-block,
8610 and we should end the compound. If NOEX is non-NULL, we wrap the body in
8611 a MUST_NOT_THROW_EXPR with NOEX as condition. */
8613 void
8614 finish_transaction_stmt (tree stmt, tree compound_stmt, int flags, tree noex)
8616 TRANSACTION_EXPR_BODY (stmt) = pop_stmt_list (TRANSACTION_EXPR_BODY (stmt));
8617 TRANSACTION_EXPR_OUTER (stmt) = (flags & TM_STMT_ATTR_OUTER) != 0;
8618 TRANSACTION_EXPR_RELAXED (stmt) = (flags & TM_STMT_ATTR_RELAXED) != 0;
8619 TRANSACTION_EXPR_IS_STMT (stmt) = 1;
8621 /* noexcept specifications are not allowed for function transactions. */
8622 gcc_assert (!(noex && compound_stmt));
8623 if (noex)
8625 tree body = build_must_not_throw_expr (TRANSACTION_EXPR_BODY (stmt),
8626 noex);
8627 protected_set_expr_location
8628 (body, EXPR_LOCATION (TRANSACTION_EXPR_BODY (stmt)));
8629 TREE_SIDE_EFFECTS (body) = 1;
8630 TRANSACTION_EXPR_BODY (stmt) = body;
8633 if (compound_stmt)
8634 finish_compound_stmt (compound_stmt);
8637 /* Build a __transaction_atomic or __transaction_relaxed expression. If
8638 NOEX is non-NULL, we wrap the body in a MUST_NOT_THROW_EXPR with NOEX as
8639 condition. */
8641 tree
8642 build_transaction_expr (location_t loc, tree expr, int flags, tree noex)
8644 tree ret;
8645 if (noex)
8647 expr = build_must_not_throw_expr (expr, noex);
8648 protected_set_expr_location (expr, loc);
8649 TREE_SIDE_EFFECTS (expr) = 1;
8651 ret = build1 (TRANSACTION_EXPR, TREE_TYPE (expr), expr);
8652 if (flags & TM_STMT_ATTR_RELAXED)
8653 TRANSACTION_EXPR_RELAXED (ret) = 1;
8654 TREE_SIDE_EFFECTS (ret) = 1;
8655 SET_EXPR_LOCATION (ret, loc);
8656 return ret;
8659 void
8660 init_cp_semantics (void)
8664 /* Build a STATIC_ASSERT for a static assertion with the condition
8665 CONDITION and the message text MESSAGE. LOCATION is the location
8666 of the static assertion in the source code. When MEMBER_P, this
8667 static assertion is a member of a class. */
8668 void
8669 finish_static_assert (tree condition, tree message, location_t location,
8670 bool member_p)
8672 tsubst_flags_t complain = tf_warning_or_error;
8674 if (message == NULL_TREE
8675 || message == error_mark_node
8676 || condition == NULL_TREE
8677 || condition == error_mark_node)
8678 return;
8680 if (check_for_bare_parameter_packs (condition))
8681 condition = error_mark_node;
8683 if (instantiation_dependent_expression_p (condition))
8685 /* We're in a template; build a STATIC_ASSERT and put it in
8686 the right place. */
8687 tree assertion;
8689 assertion = make_node (STATIC_ASSERT);
8690 STATIC_ASSERT_CONDITION (assertion) = condition;
8691 STATIC_ASSERT_MESSAGE (assertion) = message;
8692 STATIC_ASSERT_SOURCE_LOCATION (assertion) = location;
8694 if (member_p)
8695 maybe_add_class_template_decl_list (current_class_type,
8696 assertion,
8697 /*friend_p=*/0);
8698 else
8699 add_stmt (assertion);
8701 return;
8704 /* Fold the expression and convert it to a boolean value. */
8705 condition = perform_implicit_conversion_flags (boolean_type_node, condition,
8706 complain, LOOKUP_NORMAL);
8707 condition = fold_non_dependent_expr (condition, complain);
8709 if (TREE_CODE (condition) == INTEGER_CST && !integer_zerop (condition))
8710 /* Do nothing; the condition is satisfied. */
8712 else
8714 location_t saved_loc = input_location;
8716 input_location = location;
8717 if (TREE_CODE (condition) == INTEGER_CST
8718 && integer_zerop (condition))
8720 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
8721 (TREE_TYPE (TREE_TYPE (message))));
8722 int len = TREE_STRING_LENGTH (message) / sz - 1;
8723 /* Report the error. */
8724 if (len == 0)
8725 error ("static assertion failed");
8726 else
8727 error ("static assertion failed: %s",
8728 TREE_STRING_POINTER (message));
8730 else if (condition && condition != error_mark_node)
8732 error ("non-constant condition for static assertion");
8733 if (require_rvalue_constant_expression (condition))
8734 cxx_constant_value (condition);
8736 input_location = saved_loc;
8740 /* Implements the C++0x decltype keyword. Returns the type of EXPR,
8741 suitable for use as a type-specifier.
8743 ID_EXPRESSION_OR_MEMBER_ACCESS_P is true when EXPR was parsed as an
8744 id-expression or a class member access, FALSE when it was parsed as
8745 a full expression. */
8747 tree
8748 finish_decltype_type (tree expr, bool id_expression_or_member_access_p,
8749 tsubst_flags_t complain)
8751 tree type = NULL_TREE;
8753 if (!expr || error_operand_p (expr))
8754 return error_mark_node;
8756 if (TYPE_P (expr)
8757 || TREE_CODE (expr) == TYPE_DECL
8758 || (TREE_CODE (expr) == BIT_NOT_EXPR
8759 && TYPE_P (TREE_OPERAND (expr, 0))))
8761 if (complain & tf_error)
8762 error ("argument to decltype must be an expression");
8763 return error_mark_node;
8766 /* Depending on the resolution of DR 1172, we may later need to distinguish
8767 instantiation-dependent but not type-dependent expressions so that, say,
8768 A<decltype(sizeof(T))>::U doesn't require 'typename'. */
8769 if (instantiation_dependent_uneval_expression_p (expr))
8771 type = cxx_make_type (DECLTYPE_TYPE);
8772 DECLTYPE_TYPE_EXPR (type) = expr;
8773 DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (type)
8774 = id_expression_or_member_access_p;
8775 SET_TYPE_STRUCTURAL_EQUALITY (type);
8777 return type;
8780 /* The type denoted by decltype(e) is defined as follows: */
8782 expr = resolve_nondeduced_context (expr, complain);
8784 if (invalid_nonstatic_memfn_p (input_location, expr, complain))
8785 return error_mark_node;
8787 if (type_unknown_p (expr))
8789 if (complain & tf_error)
8790 error ("decltype cannot resolve address of overloaded function");
8791 return error_mark_node;
8794 /* To get the size of a static data member declared as an array of
8795 unknown bound, we need to instantiate it. */
8796 if (VAR_P (expr)
8797 && VAR_HAD_UNKNOWN_BOUND (expr)
8798 && DECL_TEMPLATE_INSTANTIATION (expr))
8799 instantiate_decl (expr, /*defer_ok*/true, /*expl_inst_mem*/false);
8801 if (id_expression_or_member_access_p)
8803 /* If e is an id-expression or a class member access (5.2.5
8804 [expr.ref]), decltype(e) is defined as the type of the entity
8805 named by e. If there is no such entity, or e names a set of
8806 overloaded functions, the program is ill-formed. */
8807 if (identifier_p (expr))
8808 expr = lookup_name (expr);
8810 if (INDIRECT_REF_P (expr))
8811 /* This can happen when the expression is, e.g., "a.b". Just
8812 look at the underlying operand. */
8813 expr = TREE_OPERAND (expr, 0);
8815 if (TREE_CODE (expr) == OFFSET_REF
8816 || TREE_CODE (expr) == MEMBER_REF
8817 || TREE_CODE (expr) == SCOPE_REF)
8818 /* We're only interested in the field itself. If it is a
8819 BASELINK, we will need to see through it in the next
8820 step. */
8821 expr = TREE_OPERAND (expr, 1);
8823 if (BASELINK_P (expr))
8824 /* See through BASELINK nodes to the underlying function. */
8825 expr = BASELINK_FUNCTIONS (expr);
8827 /* decltype of a decomposition name drops references in the tuple case
8828 (unlike decltype of a normal variable) and keeps cv-qualifiers from
8829 the containing object in the other cases (unlike decltype of a member
8830 access expression). */
8831 if (DECL_DECOMPOSITION_P (expr))
8833 if (DECL_HAS_VALUE_EXPR_P (expr))
8834 /* Expr is an array or struct subobject proxy, handle
8835 bit-fields properly. */
8836 return unlowered_expr_type (expr);
8837 else
8838 /* Expr is a reference variable for the tuple case. */
8839 return lookup_decomp_type (expr);
8842 switch (TREE_CODE (expr))
8844 case FIELD_DECL:
8845 if (DECL_BIT_FIELD_TYPE (expr))
8847 type = DECL_BIT_FIELD_TYPE (expr);
8848 break;
8850 /* Fall through for fields that aren't bitfields. */
8851 gcc_fallthrough ();
8853 case FUNCTION_DECL:
8854 case VAR_DECL:
8855 case CONST_DECL:
8856 case PARM_DECL:
8857 case RESULT_DECL:
8858 case TEMPLATE_PARM_INDEX:
8859 expr = mark_type_use (expr);
8860 type = TREE_TYPE (expr);
8861 break;
8863 case ERROR_MARK:
8864 type = error_mark_node;
8865 break;
8867 case COMPONENT_REF:
8868 case COMPOUND_EXPR:
8869 mark_type_use (expr);
8870 type = is_bitfield_expr_with_lowered_type (expr);
8871 if (!type)
8872 type = TREE_TYPE (TREE_OPERAND (expr, 1));
8873 break;
8875 case BIT_FIELD_REF:
8876 gcc_unreachable ();
8878 case INTEGER_CST:
8879 case PTRMEM_CST:
8880 /* We can get here when the id-expression refers to an
8881 enumerator or non-type template parameter. */
8882 type = TREE_TYPE (expr);
8883 break;
8885 default:
8886 /* Handle instantiated template non-type arguments. */
8887 type = TREE_TYPE (expr);
8888 break;
8891 else
8893 /* Within a lambda-expression:
8895 Every occurrence of decltype((x)) where x is a possibly
8896 parenthesized id-expression that names an entity of
8897 automatic storage duration is treated as if x were
8898 transformed into an access to a corresponding data member
8899 of the closure type that would have been declared if x
8900 were a use of the denoted entity. */
8901 if (outer_automatic_var_p (expr)
8902 && current_function_decl
8903 && LAMBDA_FUNCTION_P (current_function_decl))
8904 type = capture_decltype (expr);
8905 else if (error_operand_p (expr))
8906 type = error_mark_node;
8907 else if (expr == current_class_ptr)
8908 /* If the expression is just "this", we want the
8909 cv-unqualified pointer for the "this" type. */
8910 type = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
8911 else
8913 /* Otherwise, where T is the type of e, if e is an lvalue,
8914 decltype(e) is defined as T&; if an xvalue, T&&; otherwise, T. */
8915 cp_lvalue_kind clk = lvalue_kind (expr);
8916 type = unlowered_expr_type (expr);
8917 gcc_assert (!TYPE_REF_P (type));
8919 /* For vector types, pick a non-opaque variant. */
8920 if (VECTOR_TYPE_P (type))
8921 type = strip_typedefs (type);
8923 if (clk != clk_none && !(clk & clk_class))
8924 type = cp_build_reference_type (type, (clk & clk_rvalueref));
8928 return type;
8931 /* Called from trait_expr_value to evaluate either __has_nothrow_assign or
8932 __has_nothrow_copy, depending on assign_p. Returns true iff all
8933 the copy {ctor,assign} fns are nothrow. */
8935 static bool
8936 classtype_has_nothrow_assign_or_copy_p (tree type, bool assign_p)
8938 tree fns = NULL_TREE;
8940 if (assign_p || TYPE_HAS_COPY_CTOR (type))
8941 fns = get_class_binding (type, assign_p ? assign_op_identifier
8942 : ctor_identifier);
8944 bool saw_copy = false;
8945 for (ovl_iterator iter (fns); iter; ++iter)
8947 tree fn = *iter;
8949 if (copy_fn_p (fn) > 0)
8951 saw_copy = true;
8952 maybe_instantiate_noexcept (fn);
8953 if (!TYPE_NOTHROW_P (TREE_TYPE (fn)))
8954 return false;
8958 return saw_copy;
8961 /* Actually evaluates the trait. */
8963 static bool
8964 trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
8966 enum tree_code type_code1;
8967 tree t;
8969 type_code1 = TREE_CODE (type1);
8971 switch (kind)
8973 case CPTK_HAS_NOTHROW_ASSIGN:
8974 type1 = strip_array_types (type1);
8975 return (!CP_TYPE_CONST_P (type1) && type_code1 != REFERENCE_TYPE
8976 && (trait_expr_value (CPTK_HAS_TRIVIAL_ASSIGN, type1, type2)
8977 || (CLASS_TYPE_P (type1)
8978 && classtype_has_nothrow_assign_or_copy_p (type1,
8979 true))));
8981 case CPTK_HAS_TRIVIAL_ASSIGN:
8982 /* ??? The standard seems to be missing the "or array of such a class
8983 type" wording for this trait. */
8984 type1 = strip_array_types (type1);
8985 return (!CP_TYPE_CONST_P (type1) && type_code1 != REFERENCE_TYPE
8986 && (trivial_type_p (type1)
8987 || (CLASS_TYPE_P (type1)
8988 && TYPE_HAS_TRIVIAL_COPY_ASSIGN (type1))));
8990 case CPTK_HAS_NOTHROW_CONSTRUCTOR:
8991 type1 = strip_array_types (type1);
8992 return (trait_expr_value (CPTK_HAS_TRIVIAL_CONSTRUCTOR, type1, type2)
8993 || (CLASS_TYPE_P (type1)
8994 && (t = locate_ctor (type1))
8995 && (maybe_instantiate_noexcept (t),
8996 TYPE_NOTHROW_P (TREE_TYPE (t)))));
8998 case CPTK_HAS_TRIVIAL_CONSTRUCTOR:
8999 type1 = strip_array_types (type1);
9000 return (trivial_type_p (type1)
9001 || (CLASS_TYPE_P (type1) && TYPE_HAS_TRIVIAL_DFLT (type1)));
9003 case CPTK_HAS_NOTHROW_COPY:
9004 type1 = strip_array_types (type1);
9005 return (trait_expr_value (CPTK_HAS_TRIVIAL_COPY, type1, type2)
9006 || (CLASS_TYPE_P (type1)
9007 && classtype_has_nothrow_assign_or_copy_p (type1, false)));
9009 case CPTK_HAS_TRIVIAL_COPY:
9010 /* ??? The standard seems to be missing the "or array of such a class
9011 type" wording for this trait. */
9012 type1 = strip_array_types (type1);
9013 return (trivial_type_p (type1) || type_code1 == REFERENCE_TYPE
9014 || (CLASS_TYPE_P (type1) && TYPE_HAS_TRIVIAL_COPY_CTOR (type1)));
9016 case CPTK_HAS_TRIVIAL_DESTRUCTOR:
9017 type1 = strip_array_types (type1);
9018 return (trivial_type_p (type1) || type_code1 == REFERENCE_TYPE
9019 || (CLASS_TYPE_P (type1)
9020 && TYPE_HAS_TRIVIAL_DESTRUCTOR (type1)));
9022 case CPTK_HAS_VIRTUAL_DESTRUCTOR:
9023 return type_has_virtual_destructor (type1);
9025 case CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS:
9026 return type_has_unique_obj_representations (type1);
9028 case CPTK_IS_ABSTRACT:
9029 return ABSTRACT_CLASS_TYPE_P (type1);
9031 case CPTK_IS_AGGREGATE:
9032 return CP_AGGREGATE_TYPE_P (type1);
9034 case CPTK_IS_BASE_OF:
9035 return (NON_UNION_CLASS_TYPE_P (type1) && NON_UNION_CLASS_TYPE_P (type2)
9036 && (same_type_ignoring_top_level_qualifiers_p (type1, type2)
9037 || DERIVED_FROM_P (type1, type2)));
9039 case CPTK_IS_CLASS:
9040 return NON_UNION_CLASS_TYPE_P (type1);
9042 case CPTK_IS_EMPTY:
9043 return NON_UNION_CLASS_TYPE_P (type1) && CLASSTYPE_EMPTY_P (type1);
9045 case CPTK_IS_ENUM:
9046 return type_code1 == ENUMERAL_TYPE;
9048 case CPTK_IS_FINAL:
9049 return CLASS_TYPE_P (type1) && CLASSTYPE_FINAL (type1);
9051 case CPTK_IS_LITERAL_TYPE:
9052 return literal_type_p (type1);
9054 case CPTK_IS_POD:
9055 return pod_type_p (type1);
9057 case CPTK_IS_POLYMORPHIC:
9058 return CLASS_TYPE_P (type1) && TYPE_POLYMORPHIC_P (type1);
9060 case CPTK_IS_SAME_AS:
9061 return same_type_p (type1, type2);
9063 case CPTK_IS_STD_LAYOUT:
9064 return std_layout_type_p (type1);
9066 case CPTK_IS_TRIVIAL:
9067 return trivial_type_p (type1);
9069 case CPTK_IS_TRIVIALLY_ASSIGNABLE:
9070 return is_trivially_xible (MODIFY_EXPR, type1, type2);
9072 case CPTK_IS_TRIVIALLY_CONSTRUCTIBLE:
9073 return is_trivially_xible (INIT_EXPR, type1, type2);
9075 case CPTK_IS_TRIVIALLY_COPYABLE:
9076 return trivially_copyable_p (type1);
9078 case CPTK_IS_UNION:
9079 return type_code1 == UNION_TYPE;
9081 case CPTK_IS_ASSIGNABLE:
9082 return is_xible (MODIFY_EXPR, type1, type2);
9084 case CPTK_IS_CONSTRUCTIBLE:
9085 return is_xible (INIT_EXPR, type1, type2);
9087 default:
9088 gcc_unreachable ();
9089 return false;
9093 /* If TYPE is an array of unknown bound, or (possibly cv-qualified)
9094 void, or a complete type, returns true, otherwise false. */
9096 static bool
9097 check_trait_type (tree type)
9099 if (type == NULL_TREE)
9100 return true;
9102 if (TREE_CODE (type) == TREE_LIST)
9103 return (check_trait_type (TREE_VALUE (type))
9104 && check_trait_type (TREE_CHAIN (type)));
9106 if (TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type)
9107 && COMPLETE_TYPE_P (TREE_TYPE (type)))
9108 return true;
9110 if (VOID_TYPE_P (type))
9111 return true;
9113 return !!complete_type_or_else (strip_array_types (type), NULL_TREE);
9116 /* Process a trait expression. */
9118 tree
9119 finish_trait_expr (cp_trait_kind kind, tree type1, tree type2)
9121 if (type1 == error_mark_node
9122 || type2 == error_mark_node)
9123 return error_mark_node;
9125 if (processing_template_decl)
9127 tree trait_expr = make_node (TRAIT_EXPR);
9128 TREE_TYPE (trait_expr) = boolean_type_node;
9129 TRAIT_EXPR_TYPE1 (trait_expr) = type1;
9130 TRAIT_EXPR_TYPE2 (trait_expr) = type2;
9131 TRAIT_EXPR_KIND (trait_expr) = kind;
9132 return trait_expr;
9135 switch (kind)
9137 case CPTK_HAS_NOTHROW_ASSIGN:
9138 case CPTK_HAS_TRIVIAL_ASSIGN:
9139 case CPTK_HAS_NOTHROW_CONSTRUCTOR:
9140 case CPTK_HAS_TRIVIAL_CONSTRUCTOR:
9141 case CPTK_HAS_NOTHROW_COPY:
9142 case CPTK_HAS_TRIVIAL_COPY:
9143 case CPTK_HAS_TRIVIAL_DESTRUCTOR:
9144 case CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS:
9145 case CPTK_HAS_VIRTUAL_DESTRUCTOR:
9146 case CPTK_IS_ABSTRACT:
9147 case CPTK_IS_AGGREGATE:
9148 case CPTK_IS_EMPTY:
9149 case CPTK_IS_FINAL:
9150 case CPTK_IS_LITERAL_TYPE:
9151 case CPTK_IS_POD:
9152 case CPTK_IS_POLYMORPHIC:
9153 case CPTK_IS_STD_LAYOUT:
9154 case CPTK_IS_TRIVIAL:
9155 case CPTK_IS_TRIVIALLY_COPYABLE:
9156 if (!check_trait_type (type1))
9157 return error_mark_node;
9158 break;
9160 case CPTK_IS_ASSIGNABLE:
9161 case CPTK_IS_CONSTRUCTIBLE:
9162 break;
9164 case CPTK_IS_TRIVIALLY_ASSIGNABLE:
9165 case CPTK_IS_TRIVIALLY_CONSTRUCTIBLE:
9166 if (!check_trait_type (type1)
9167 || !check_trait_type (type2))
9168 return error_mark_node;
9169 break;
9171 case CPTK_IS_BASE_OF:
9172 if (NON_UNION_CLASS_TYPE_P (type1) && NON_UNION_CLASS_TYPE_P (type2)
9173 && !same_type_ignoring_top_level_qualifiers_p (type1, type2)
9174 && !complete_type_or_else (type2, NULL_TREE))
9175 /* We already issued an error. */
9176 return error_mark_node;
9177 break;
9179 case CPTK_IS_CLASS:
9180 case CPTK_IS_ENUM:
9181 case CPTK_IS_UNION:
9182 case CPTK_IS_SAME_AS:
9183 break;
9185 default:
9186 gcc_unreachable ();
9189 return (trait_expr_value (kind, type1, type2)
9190 ? boolean_true_node : boolean_false_node);
9193 /* Do-nothing variants of functions to handle pragma FLOAT_CONST_DECIMAL64,
9194 which is ignored for C++. */
9196 void
9197 set_float_const_decimal64 (void)
9201 void
9202 clear_float_const_decimal64 (void)
9206 bool
9207 float_const_decimal64_p (void)
9209 return 0;
9213 /* Return true if T designates the implied `this' parameter. */
9215 bool
9216 is_this_parameter (tree t)
9218 if (!DECL_P (t) || DECL_NAME (t) != this_identifier)
9219 return false;
9220 gcc_assert (TREE_CODE (t) == PARM_DECL || is_capture_proxy (t)
9221 || (cp_binding_oracle && TREE_CODE (t) == VAR_DECL));
9222 return true;
9225 /* Insert the deduced return type for an auto function. */
9227 void
9228 apply_deduced_return_type (tree fco, tree return_type)
9230 tree result;
9232 if (return_type == error_mark_node)
9233 return;
9235 if (DECL_CONV_FN_P (fco))
9236 DECL_NAME (fco) = make_conv_op_name (return_type);
9238 TREE_TYPE (fco) = change_return_type (return_type, TREE_TYPE (fco));
9240 result = DECL_RESULT (fco);
9241 if (result == NULL_TREE)
9242 return;
9243 if (TREE_TYPE (result) == return_type)
9244 return;
9246 if (!processing_template_decl && !VOID_TYPE_P (return_type)
9247 && !complete_type_or_else (return_type, NULL_TREE))
9248 return;
9250 /* We already have a DECL_RESULT from start_preparsed_function.
9251 Now we need to redo the work it and allocate_struct_function
9252 did to reflect the new type. */
9253 gcc_assert (current_function_decl == fco);
9254 result = build_decl (input_location, RESULT_DECL, NULL_TREE,
9255 TYPE_MAIN_VARIANT (return_type));
9256 DECL_ARTIFICIAL (result) = 1;
9257 DECL_IGNORED_P (result) = 1;
9258 cp_apply_type_quals_to_decl (cp_type_quals (return_type),
9259 result);
9261 DECL_RESULT (fco) = result;
9263 if (!processing_template_decl)
9265 bool aggr = aggregate_value_p (result, fco);
9266 #ifdef PCC_STATIC_STRUCT_RETURN
9267 cfun->returns_pcc_struct = aggr;
9268 #endif
9269 cfun->returns_struct = aggr;
9274 /* DECL is a local variable or parameter from the surrounding scope of a
9275 lambda-expression. Returns the decltype for a use of the capture field
9276 for DECL even if it hasn't been captured yet. */
9278 static tree
9279 capture_decltype (tree decl)
9281 tree lam = CLASSTYPE_LAMBDA_EXPR (DECL_CONTEXT (current_function_decl));
9282 /* FIXME do lookup instead of list walk? */
9283 tree cap = value_member (decl, LAMBDA_EXPR_CAPTURE_LIST (lam));
9284 tree type;
9286 if (cap)
9287 type = TREE_TYPE (TREE_PURPOSE (cap));
9288 else
9289 switch (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lam))
9291 case CPLD_NONE:
9292 error ("%qD is not captured", decl);
9293 return error_mark_node;
9295 case CPLD_COPY:
9296 type = TREE_TYPE (decl);
9297 if (TYPE_REF_P (type)
9298 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
9299 type = TREE_TYPE (type);
9300 break;
9302 case CPLD_REFERENCE:
9303 type = TREE_TYPE (decl);
9304 if (!TYPE_REF_P (type))
9305 type = build_reference_type (TREE_TYPE (decl));
9306 break;
9308 default:
9309 gcc_unreachable ();
9312 if (!TYPE_REF_P (type))
9314 if (!LAMBDA_EXPR_MUTABLE_P (lam))
9315 type = cp_build_qualified_type (type, (cp_type_quals (type)
9316 |TYPE_QUAL_CONST));
9317 type = build_reference_type (type);
9319 return type;
9322 /* Build a unary fold expression of EXPR over OP. If IS_RIGHT is true,
9323 this is a right unary fold. Otherwise it is a left unary fold. */
9325 static tree
9326 finish_unary_fold_expr (tree expr, int op, tree_code dir)
9328 // Build a pack expansion (assuming expr has pack type).
9329 if (!uses_parameter_packs (expr))
9331 error_at (location_of (expr), "operand of fold expression has no "
9332 "unexpanded parameter packs");
9333 return error_mark_node;
9335 tree pack = make_pack_expansion (expr);
9337 // Build the fold expression.
9338 tree code = build_int_cstu (integer_type_node, abs (op));
9339 tree fold = build_min_nt_loc (UNKNOWN_LOCATION, dir, code, pack);
9340 FOLD_EXPR_MODIFY_P (fold) = (op < 0);
9341 return fold;
9344 tree
9345 finish_left_unary_fold_expr (tree expr, int op)
9347 return finish_unary_fold_expr (expr, op, UNARY_LEFT_FOLD_EXPR);
9350 tree
9351 finish_right_unary_fold_expr (tree expr, int op)
9353 return finish_unary_fold_expr (expr, op, UNARY_RIGHT_FOLD_EXPR);
9356 /* Build a binary fold expression over EXPR1 and EXPR2. The
9357 associativity of the fold is determined by EXPR1 and EXPR2 (whichever
9358 has an unexpanded parameter pack). */
9360 tree
9361 finish_binary_fold_expr (tree pack, tree init, int op, tree_code dir)
9363 pack = make_pack_expansion (pack);
9364 tree code = build_int_cstu (integer_type_node, abs (op));
9365 tree fold = build_min_nt_loc (UNKNOWN_LOCATION, dir, code, pack, init);
9366 FOLD_EXPR_MODIFY_P (fold) = (op < 0);
9367 return fold;
9370 tree
9371 finish_binary_fold_expr (tree expr1, tree expr2, int op)
9373 // Determine which expr has an unexpanded parameter pack and
9374 // set the pack and initial term.
9375 bool pack1 = uses_parameter_packs (expr1);
9376 bool pack2 = uses_parameter_packs (expr2);
9377 if (pack1 && !pack2)
9378 return finish_binary_fold_expr (expr1, expr2, op, BINARY_RIGHT_FOLD_EXPR);
9379 else if (pack2 && !pack1)
9380 return finish_binary_fold_expr (expr2, expr1, op, BINARY_LEFT_FOLD_EXPR);
9381 else
9383 if (pack1)
9384 error ("both arguments in binary fold have unexpanded parameter packs");
9385 else
9386 error ("no unexpanded parameter packs in binary fold");
9388 return error_mark_node;
9391 /* Finish __builtin_launder (arg). */
9393 tree
9394 finish_builtin_launder (location_t loc, tree arg, tsubst_flags_t complain)
9396 tree orig_arg = arg;
9397 if (!type_dependent_expression_p (arg))
9398 arg = decay_conversion (arg, complain);
9399 if (error_operand_p (arg))
9400 return error_mark_node;
9401 if (!type_dependent_expression_p (arg)
9402 && !TYPE_PTR_P (TREE_TYPE (arg)))
9404 error_at (loc, "non-pointer argument to %<__builtin_launder%>");
9405 return error_mark_node;
9407 if (processing_template_decl)
9408 arg = orig_arg;
9409 return build_call_expr_internal_loc (loc, IFN_LAUNDER,
9410 TREE_TYPE (arg), 1, arg);
9413 #include "gt-cp-semantics.h"