Fix TYPE_MAIN_VARIANT construction for arrays of qualified typedefs (PR c/68162).
[official-gcc.git] / gcc / cp / semantics.c
blob3bb61847ca80e02dcd5e0a65ffeef902c0e859c3
1 /* Perform the semantic phase of parsing, i.e., the process of
2 building tree structure, checking semantic consistency, and
3 building RTL. These routines are used both during actual parsing
4 and during the instantiation of template functions.
6 Copyright (C) 1998-2015 Free Software Foundation, Inc.
7 Written by Mark Mitchell (mmitchell@usa.net) based on code found
8 formerly in parse.y and pt.c.
10 This file is part of GCC.
12 GCC is free software; you can redistribute it and/or modify it
13 under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 3, or (at your option)
15 any later version.
17 GCC is distributed in the hope that it will be useful, but
18 WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with GCC; see the file COPYING3. If not see
24 <http://www.gnu.org/licenses/>. */
26 #include "config.h"
27 #include "system.h"
28 #include "coretypes.h"
29 #include "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-low.h"
42 #include "convert.h"
43 #include "gomp-constants.h"
45 /* There routines provide a modular interface to perform many parsing
46 operations. They may therefore be used during actual parsing, or
47 during template instantiation, which may be regarded as a
48 degenerate form of parsing. */
50 static tree maybe_convert_cond (tree);
51 static tree finalize_nrv_r (tree *, int *, void *);
52 static tree capture_decltype (tree);
54 /* Used for OpenMP non-static data member privatization. */
56 static hash_map<tree, tree> *omp_private_member_map;
57 static vec<tree> omp_private_member_vec;
58 static bool omp_private_member_ignore_next;
61 /* Deferred Access Checking Overview
62 ---------------------------------
64 Most C++ expressions and declarations require access checking
65 to be performed during parsing. However, in several cases,
66 this has to be treated differently.
68 For member declarations, access checking has to be deferred
69 until more information about the declaration is known. For
70 example:
72 class A {
73 typedef int X;
74 public:
75 X f();
78 A::X A::f();
79 A::X g();
81 When we are parsing the function return type `A::X', we don't
82 really know if this is allowed until we parse the function name.
84 Furthermore, some contexts require that access checking is
85 never performed at all. These include class heads, and template
86 instantiations.
88 Typical use of access checking functions is described here:
90 1. When we enter a context that requires certain access checking
91 mode, the function `push_deferring_access_checks' is called with
92 DEFERRING argument specifying the desired mode. Access checking
93 may be performed immediately (dk_no_deferred), deferred
94 (dk_deferred), or not performed (dk_no_check).
96 2. When a declaration such as a type, or a variable, is encountered,
97 the function `perform_or_defer_access_check' is called. It
98 maintains a vector of all deferred checks.
100 3. The global `current_class_type' or `current_function_decl' is then
101 setup by the parser. `enforce_access' relies on these information
102 to check access.
104 4. Upon exiting the context mentioned in step 1,
105 `perform_deferred_access_checks' is called to check all declaration
106 stored in the vector. `pop_deferring_access_checks' is then
107 called to restore the previous access checking mode.
109 In case of parsing error, we simply call `pop_deferring_access_checks'
110 without `perform_deferred_access_checks'. */
112 struct GTY(()) deferred_access {
113 /* A vector representing name-lookups for which we have deferred
114 checking access controls. We cannot check the accessibility of
115 names used in a decl-specifier-seq until we know what is being
116 declared because code like:
118 class A {
119 class B {};
120 B* f();
123 A::B* A::f() { return 0; }
125 is valid, even though `A::B' is not generally accessible. */
126 vec<deferred_access_check, va_gc> * GTY(()) deferred_access_checks;
128 /* The current mode of access checks. */
129 enum deferring_kind deferring_access_checks_kind;
133 /* Data for deferred access checking. */
134 static GTY(()) vec<deferred_access, va_gc> *deferred_access_stack;
135 static GTY(()) unsigned deferred_access_no_check;
137 /* Save the current deferred access states and start deferred
138 access checking iff DEFER_P is true. */
140 void
141 push_deferring_access_checks (deferring_kind deferring)
143 /* For context like template instantiation, access checking
144 disabling applies to all nested context. */
145 if (deferred_access_no_check || deferring == dk_no_check)
146 deferred_access_no_check++;
147 else
149 deferred_access e = {NULL, deferring};
150 vec_safe_push (deferred_access_stack, e);
154 /* Save the current deferred access states and start deferred access
155 checking, continuing the set of deferred checks in CHECKS. */
157 void
158 reopen_deferring_access_checks (vec<deferred_access_check, va_gc> * checks)
160 push_deferring_access_checks (dk_deferred);
161 if (!deferred_access_no_check)
162 deferred_access_stack->last().deferred_access_checks = checks;
165 /* Resume deferring access checks again after we stopped doing
166 this previously. */
168 void
169 resume_deferring_access_checks (void)
171 if (!deferred_access_no_check)
172 deferred_access_stack->last().deferring_access_checks_kind = dk_deferred;
175 /* Stop deferring access checks. */
177 void
178 stop_deferring_access_checks (void)
180 if (!deferred_access_no_check)
181 deferred_access_stack->last().deferring_access_checks_kind = dk_no_deferred;
184 /* Discard the current deferred access checks and restore the
185 previous states. */
187 void
188 pop_deferring_access_checks (void)
190 if (deferred_access_no_check)
191 deferred_access_no_check--;
192 else
193 deferred_access_stack->pop ();
196 /* Returns a TREE_LIST representing the deferred checks.
197 The TREE_PURPOSE of each node is the type through which the
198 access occurred; the TREE_VALUE is the declaration named.
201 vec<deferred_access_check, va_gc> *
202 get_deferred_access_checks (void)
204 if (deferred_access_no_check)
205 return NULL;
206 else
207 return (deferred_access_stack->last().deferred_access_checks);
210 /* Take current deferred checks and combine with the
211 previous states if we also defer checks previously.
212 Otherwise perform checks now. */
214 void
215 pop_to_parent_deferring_access_checks (void)
217 if (deferred_access_no_check)
218 deferred_access_no_check--;
219 else
221 vec<deferred_access_check, va_gc> *checks;
222 deferred_access *ptr;
224 checks = (deferred_access_stack->last ().deferred_access_checks);
226 deferred_access_stack->pop ();
227 ptr = &deferred_access_stack->last ();
228 if (ptr->deferring_access_checks_kind == dk_no_deferred)
230 /* Check access. */
231 perform_access_checks (checks, tf_warning_or_error);
233 else
235 /* Merge with parent. */
236 int i, j;
237 deferred_access_check *chk, *probe;
239 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
241 FOR_EACH_VEC_SAFE_ELT (ptr->deferred_access_checks, j, probe)
243 if (probe->binfo == chk->binfo &&
244 probe->decl == chk->decl &&
245 probe->diag_decl == chk->diag_decl)
246 goto found;
248 /* Insert into parent's checks. */
249 vec_safe_push (ptr->deferred_access_checks, *chk);
250 found:;
256 /* Perform the access checks in CHECKS. The TREE_PURPOSE of each node
257 is the BINFO indicating the qualifying scope used to access the
258 DECL node stored in the TREE_VALUE of the node. If CHECKS is empty
259 or we aren't in SFINAE context or all the checks succeed return TRUE,
260 otherwise FALSE. */
262 bool
263 perform_access_checks (vec<deferred_access_check, va_gc> *checks,
264 tsubst_flags_t complain)
266 int i;
267 deferred_access_check *chk;
268 location_t loc = input_location;
269 bool ok = true;
271 if (!checks)
272 return true;
274 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
276 input_location = chk->loc;
277 ok &= enforce_access (chk->binfo, chk->decl, chk->diag_decl, complain);
280 input_location = loc;
281 return (complain & tf_error) ? true : ok;
284 /* Perform the deferred access checks.
286 After performing the checks, we still have to keep the list
287 `deferred_access_stack->deferred_access_checks' since we may want
288 to check access for them again later in a different context.
289 For example:
291 class A {
292 typedef int X;
293 static X a;
295 A::X A::a, x; // No error for `A::a', error for `x'
297 We have to perform deferred access of `A::X', first with `A::a',
298 next with `x'. Return value like perform_access_checks above. */
300 bool
301 perform_deferred_access_checks (tsubst_flags_t complain)
303 return perform_access_checks (get_deferred_access_checks (), complain);
306 /* Defer checking the accessibility of DECL, when looked up in
307 BINFO. DIAG_DECL is the declaration to use to print diagnostics.
308 Return value like perform_access_checks above. */
310 bool
311 perform_or_defer_access_check (tree binfo, tree decl, tree diag_decl,
312 tsubst_flags_t complain)
314 int i;
315 deferred_access *ptr;
316 deferred_access_check *chk;
319 /* Exit if we are in a context that no access checking is performed.
321 if (deferred_access_no_check)
322 return true;
324 gcc_assert (TREE_CODE (binfo) == TREE_BINFO);
326 ptr = &deferred_access_stack->last ();
328 /* If we are not supposed to defer access checks, just check now. */
329 if (ptr->deferring_access_checks_kind == dk_no_deferred)
331 bool ok = enforce_access (binfo, decl, diag_decl, complain);
332 return (complain & tf_error) ? true : ok;
335 /* See if we are already going to perform this check. */
336 FOR_EACH_VEC_SAFE_ELT (ptr->deferred_access_checks, i, chk)
338 if (chk->decl == decl && chk->binfo == binfo &&
339 chk->diag_decl == diag_decl)
341 return true;
344 /* If not, record the check. */
345 deferred_access_check new_access = {binfo, decl, diag_decl, input_location};
346 vec_safe_push (ptr->deferred_access_checks, new_access);
348 return true;
351 /* Returns nonzero if the current statement is a full expression,
352 i.e. temporaries created during that statement should be destroyed
353 at the end of the statement. */
356 stmts_are_full_exprs_p (void)
358 return current_stmt_tree ()->stmts_are_full_exprs_p;
361 /* T is a statement. Add it to the statement-tree. This is the C++
362 version. The C/ObjC frontends have a slightly different version of
363 this function. */
365 tree
366 add_stmt (tree t)
368 enum tree_code code = TREE_CODE (t);
370 if (EXPR_P (t) && code != LABEL_EXPR)
372 if (!EXPR_HAS_LOCATION (t))
373 SET_EXPR_LOCATION (t, input_location);
375 /* When we expand a statement-tree, we must know whether or not the
376 statements are full-expressions. We record that fact here. */
377 STMT_IS_FULL_EXPR_P (t) = stmts_are_full_exprs_p ();
380 if (code == LABEL_EXPR || code == CASE_LABEL_EXPR)
381 STATEMENT_LIST_HAS_LABEL (cur_stmt_list) = 1;
383 /* Add T to the statement-tree. Non-side-effect statements need to be
384 recorded during statement expressions. */
385 gcc_checking_assert (!stmt_list_stack->is_empty ());
386 append_to_statement_list_force (t, &cur_stmt_list);
388 return t;
391 /* Returns the stmt_tree to which statements are currently being added. */
393 stmt_tree
394 current_stmt_tree (void)
396 return (cfun
397 ? &cfun->language->base.x_stmt_tree
398 : &scope_chain->x_stmt_tree);
401 /* If statements are full expressions, wrap STMT in a CLEANUP_POINT_EXPR. */
403 static tree
404 maybe_cleanup_point_expr (tree expr)
406 if (!processing_template_decl && stmts_are_full_exprs_p ())
407 expr = fold_build_cleanup_point_expr (TREE_TYPE (expr), expr);
408 return expr;
411 /* Like maybe_cleanup_point_expr except have the type of the new expression be
412 void so we don't need to create a temporary variable to hold the inner
413 expression. The reason why we do this is because the original type might be
414 an aggregate and we cannot create a temporary variable for that type. */
416 tree
417 maybe_cleanup_point_expr_void (tree expr)
419 if (!processing_template_decl && stmts_are_full_exprs_p ())
420 expr = fold_build_cleanup_point_expr (void_type_node, expr);
421 return expr;
426 /* Create a declaration statement for the declaration given by the DECL. */
428 void
429 add_decl_expr (tree decl)
431 tree r = build_stmt (input_location, DECL_EXPR, decl);
432 if (DECL_INITIAL (decl)
433 || (DECL_SIZE (decl) && TREE_SIDE_EFFECTS (DECL_SIZE (decl))))
434 r = maybe_cleanup_point_expr_void (r);
435 add_stmt (r);
438 /* Finish a scope. */
440 tree
441 do_poplevel (tree stmt_list)
443 tree block = NULL;
445 if (stmts_are_full_exprs_p ())
446 block = poplevel (kept_level_p (), 1, 0);
448 stmt_list = pop_stmt_list (stmt_list);
450 if (!processing_template_decl)
452 stmt_list = c_build_bind_expr (input_location, block, stmt_list);
453 /* ??? See c_end_compound_stmt re statement expressions. */
456 return stmt_list;
459 /* Begin a new scope. */
461 static tree
462 do_pushlevel (scope_kind sk)
464 tree ret = push_stmt_list ();
465 if (stmts_are_full_exprs_p ())
466 begin_scope (sk, NULL);
467 return ret;
470 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
471 when the current scope is exited. EH_ONLY is true when this is not
472 meant to apply to normal control flow transfer. */
474 void
475 push_cleanup (tree decl, tree cleanup, bool eh_only)
477 tree stmt = build_stmt (input_location, CLEANUP_STMT, NULL, cleanup, decl);
478 CLEANUP_EH_ONLY (stmt) = eh_only;
479 add_stmt (stmt);
480 CLEANUP_BODY (stmt) = push_stmt_list ();
483 /* Simple infinite loop tracking for -Wreturn-type. We keep a stack of all
484 the current loops, represented by 'NULL_TREE' if we've seen a possible
485 exit, and 'error_mark_node' if not. This is currently used only to
486 suppress the warning about a function with no return statements, and
487 therefore we don't bother noting returns as possible exits. We also
488 don't bother with gotos. */
490 static void
491 begin_maybe_infinite_loop (tree cond)
493 /* Only track this while parsing a function, not during instantiation. */
494 if (!cfun || (DECL_TEMPLATE_INSTANTIATION (current_function_decl)
495 && !processing_template_decl))
496 return;
497 bool maybe_infinite = true;
498 if (cond)
500 cond = fold_non_dependent_expr (cond);
501 maybe_infinite = integer_nonzerop (cond);
503 vec_safe_push (cp_function_chain->infinite_loops,
504 maybe_infinite ? error_mark_node : NULL_TREE);
508 /* A break is a possible exit for the current loop. */
510 void
511 break_maybe_infinite_loop (void)
513 if (!cfun)
514 return;
515 cp_function_chain->infinite_loops->last() = NULL_TREE;
518 /* If we reach the end of the loop without seeing a possible exit, we have
519 an infinite loop. */
521 static void
522 end_maybe_infinite_loop (tree cond)
524 if (!cfun || (DECL_TEMPLATE_INSTANTIATION (current_function_decl)
525 && !processing_template_decl))
526 return;
527 tree current = cp_function_chain->infinite_loops->pop();
528 if (current != NULL_TREE)
530 cond = fold_non_dependent_expr (cond);
531 if (integer_nonzerop (cond))
532 current_function_infinite_loop = 1;
537 /* Begin a conditional that might contain a declaration. When generating
538 normal code, we want the declaration to appear before the statement
539 containing the conditional. When generating template code, we want the
540 conditional to be rendered as the raw DECL_EXPR. */
542 static void
543 begin_cond (tree *cond_p)
545 if (processing_template_decl)
546 *cond_p = push_stmt_list ();
549 /* Finish such a conditional. */
551 static void
552 finish_cond (tree *cond_p, tree expr)
554 if (processing_template_decl)
556 tree cond = pop_stmt_list (*cond_p);
558 if (expr == NULL_TREE)
559 /* Empty condition in 'for'. */
560 gcc_assert (empty_expr_stmt_p (cond));
561 else if (check_for_bare_parameter_packs (expr))
562 expr = error_mark_node;
563 else if (!empty_expr_stmt_p (cond))
564 expr = build2 (COMPOUND_EXPR, TREE_TYPE (expr), cond, expr);
566 *cond_p = expr;
569 /* If *COND_P specifies a conditional with a declaration, transform the
570 loop such that
571 while (A x = 42) { }
572 for (; A x = 42;) { }
573 becomes
574 while (true) { A x = 42; if (!x) break; }
575 for (;;) { A x = 42; if (!x) break; }
576 The statement list for BODY will be empty if the conditional did
577 not declare anything. */
579 static void
580 simplify_loop_decl_cond (tree *cond_p, tree body)
582 tree cond, if_stmt;
584 if (!TREE_SIDE_EFFECTS (body))
585 return;
587 cond = *cond_p;
588 *cond_p = boolean_true_node;
590 if_stmt = begin_if_stmt ();
591 cond = cp_build_unary_op (TRUTH_NOT_EXPR, cond, 0, tf_warning_or_error);
592 finish_if_stmt_cond (cond, if_stmt);
593 finish_break_stmt ();
594 finish_then_clause (if_stmt);
595 finish_if_stmt (if_stmt);
598 /* Finish a goto-statement. */
600 tree
601 finish_goto_stmt (tree destination)
603 if (identifier_p (destination))
604 destination = lookup_label (destination);
606 /* We warn about unused labels with -Wunused. That means we have to
607 mark the used labels as used. */
608 if (TREE_CODE (destination) == LABEL_DECL)
609 TREE_USED (destination) = 1;
610 else
612 if (check_no_cilk (destination,
613 "Cilk array notation cannot be used as a computed goto expression",
614 "%<_Cilk_spawn%> statement cannot be used as a computed goto expression"))
615 destination = error_mark_node;
616 destination = mark_rvalue_use (destination);
617 if (!processing_template_decl)
619 destination = cp_convert (ptr_type_node, destination,
620 tf_warning_or_error);
621 if (error_operand_p (destination))
622 return NULL_TREE;
623 destination
624 = fold_build_cleanup_point_expr (TREE_TYPE (destination),
625 destination);
629 check_goto (destination);
631 return add_stmt (build_stmt (input_location, GOTO_EXPR, destination));
634 /* COND is the condition-expression for an if, while, etc.,
635 statement. Convert it to a boolean value, if appropriate.
636 In addition, verify sequence points if -Wsequence-point is enabled. */
638 static tree
639 maybe_convert_cond (tree cond)
641 /* Empty conditions remain empty. */
642 if (!cond)
643 return NULL_TREE;
645 /* Wait until we instantiate templates before doing conversion. */
646 if (processing_template_decl)
647 return cond;
649 if (warn_sequence_point)
650 verify_sequence_points (cond);
652 /* Do the conversion. */
653 cond = convert_from_reference (cond);
655 if (TREE_CODE (cond) == MODIFY_EXPR
656 && !TREE_NO_WARNING (cond)
657 && warn_parentheses)
659 warning (OPT_Wparentheses,
660 "suggest parentheses around assignment used as truth value");
661 TREE_NO_WARNING (cond) = 1;
664 return condition_conversion (cond);
667 /* Finish an expression-statement, whose EXPRESSION is as indicated. */
669 tree
670 finish_expr_stmt (tree expr)
672 tree r = NULL_TREE;
674 if (expr != NULL_TREE)
676 if (!processing_template_decl)
678 if (warn_sequence_point)
679 verify_sequence_points (expr);
680 expr = convert_to_void (expr, ICV_STATEMENT, tf_warning_or_error);
682 else if (!type_dependent_expression_p (expr))
683 convert_to_void (build_non_dependent_expr (expr), ICV_STATEMENT,
684 tf_warning_or_error);
686 if (check_for_bare_parameter_packs (expr))
687 expr = error_mark_node;
689 /* Simplification of inner statement expressions, compound exprs,
690 etc can result in us already having an EXPR_STMT. */
691 if (TREE_CODE (expr) != CLEANUP_POINT_EXPR)
693 if (TREE_CODE (expr) != EXPR_STMT)
694 expr = build_stmt (input_location, EXPR_STMT, expr);
695 expr = maybe_cleanup_point_expr_void (expr);
698 r = add_stmt (expr);
701 return r;
705 /* Begin an if-statement. Returns a newly created IF_STMT if
706 appropriate. */
708 tree
709 begin_if_stmt (void)
711 tree r, scope;
712 scope = do_pushlevel (sk_cond);
713 r = build_stmt (input_location, IF_STMT, NULL_TREE,
714 NULL_TREE, NULL_TREE, scope);
715 begin_cond (&IF_COND (r));
716 return r;
719 /* Process the COND of an if-statement, which may be given by
720 IF_STMT. */
722 void
723 finish_if_stmt_cond (tree cond, tree if_stmt)
725 finish_cond (&IF_COND (if_stmt), maybe_convert_cond (cond));
726 add_stmt (if_stmt);
727 THEN_CLAUSE (if_stmt) = push_stmt_list ();
730 /* Finish the then-clause of an if-statement, which may be given by
731 IF_STMT. */
733 tree
734 finish_then_clause (tree if_stmt)
736 THEN_CLAUSE (if_stmt) = pop_stmt_list (THEN_CLAUSE (if_stmt));
737 return if_stmt;
740 /* Begin the else-clause of an if-statement. */
742 void
743 begin_else_clause (tree if_stmt)
745 ELSE_CLAUSE (if_stmt) = push_stmt_list ();
748 /* Finish the else-clause of an if-statement, which may be given by
749 IF_STMT. */
751 void
752 finish_else_clause (tree if_stmt)
754 ELSE_CLAUSE (if_stmt) = pop_stmt_list (ELSE_CLAUSE (if_stmt));
757 /* Finish an if-statement. */
759 void
760 finish_if_stmt (tree if_stmt)
762 tree scope = IF_SCOPE (if_stmt);
763 IF_SCOPE (if_stmt) = NULL;
764 add_stmt (do_poplevel (scope));
767 /* Begin a while-statement. Returns a newly created WHILE_STMT if
768 appropriate. */
770 tree
771 begin_while_stmt (void)
773 tree r;
774 r = build_stmt (input_location, WHILE_STMT, NULL_TREE, NULL_TREE);
775 add_stmt (r);
776 WHILE_BODY (r) = do_pushlevel (sk_block);
777 begin_cond (&WHILE_COND (r));
778 return r;
781 /* Process the COND of a while-statement, which may be given by
782 WHILE_STMT. */
784 void
785 finish_while_stmt_cond (tree cond, tree while_stmt, bool ivdep)
787 if (check_no_cilk (cond,
788 "Cilk array notation cannot be used as a condition for while statement",
789 "%<_Cilk_spawn%> statement cannot be used as a condition for while statement"))
790 cond = error_mark_node;
791 cond = maybe_convert_cond (cond);
792 finish_cond (&WHILE_COND (while_stmt), cond);
793 begin_maybe_infinite_loop (cond);
794 if (ivdep && cond != error_mark_node)
795 WHILE_COND (while_stmt) = build2 (ANNOTATE_EXPR,
796 TREE_TYPE (WHILE_COND (while_stmt)),
797 WHILE_COND (while_stmt),
798 build_int_cst (integer_type_node,
799 annot_expr_ivdep_kind));
800 simplify_loop_decl_cond (&WHILE_COND (while_stmt), WHILE_BODY (while_stmt));
803 /* Finish a while-statement, which may be given by WHILE_STMT. */
805 void
806 finish_while_stmt (tree while_stmt)
808 end_maybe_infinite_loop (boolean_true_node);
809 WHILE_BODY (while_stmt) = do_poplevel (WHILE_BODY (while_stmt));
812 /* Begin a do-statement. Returns a newly created DO_STMT if
813 appropriate. */
815 tree
816 begin_do_stmt (void)
818 tree r = build_stmt (input_location, DO_STMT, NULL_TREE, NULL_TREE);
819 begin_maybe_infinite_loop (boolean_true_node);
820 add_stmt (r);
821 DO_BODY (r) = push_stmt_list ();
822 return r;
825 /* Finish the body of a do-statement, which may be given by DO_STMT. */
827 void
828 finish_do_body (tree do_stmt)
830 tree body = DO_BODY (do_stmt) = pop_stmt_list (DO_BODY (do_stmt));
832 if (TREE_CODE (body) == STATEMENT_LIST && STATEMENT_LIST_TAIL (body))
833 body = STATEMENT_LIST_TAIL (body)->stmt;
835 if (IS_EMPTY_STMT (body))
836 warning (OPT_Wempty_body,
837 "suggest explicit braces around empty body in %<do%> statement");
840 /* Finish a do-statement, which may be given by DO_STMT, and whose
841 COND is as indicated. */
843 void
844 finish_do_stmt (tree cond, tree do_stmt, bool ivdep)
846 if (check_no_cilk (cond,
847 "Cilk array notation cannot be used as a condition for a do-while statement",
848 "%<_Cilk_spawn%> statement cannot be used as a condition for a do-while statement"))
849 cond = error_mark_node;
850 cond = maybe_convert_cond (cond);
851 end_maybe_infinite_loop (cond);
852 if (ivdep && cond != error_mark_node)
853 cond = build2 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
854 build_int_cst (integer_type_node, annot_expr_ivdep_kind));
855 DO_COND (do_stmt) = cond;
858 /* Finish a return-statement. The EXPRESSION returned, if any, is as
859 indicated. */
861 tree
862 finish_return_stmt (tree expr)
864 tree r;
865 bool no_warning;
867 expr = check_return_expr (expr, &no_warning);
869 if (error_operand_p (expr)
870 || (flag_openmp && !check_omp_return ()))
872 /* Suppress -Wreturn-type for this function. */
873 if (warn_return_type)
874 TREE_NO_WARNING (current_function_decl) = true;
875 return error_mark_node;
878 if (!processing_template_decl)
880 if (warn_sequence_point)
881 verify_sequence_points (expr);
883 if (DECL_DESTRUCTOR_P (current_function_decl)
884 || (DECL_CONSTRUCTOR_P (current_function_decl)
885 && targetm.cxx.cdtor_returns_this ()))
887 /* Similarly, all destructors must run destructors for
888 base-classes before returning. So, all returns in a
889 destructor get sent to the DTOR_LABEL; finish_function emits
890 code to return a value there. */
891 return finish_goto_stmt (cdtor_label);
895 r = build_stmt (input_location, RETURN_EXPR, expr);
896 TREE_NO_WARNING (r) |= no_warning;
897 r = maybe_cleanup_point_expr_void (r);
898 r = add_stmt (r);
900 return r;
903 /* Begin the scope of a for-statement or a range-for-statement.
904 Both the returned trees are to be used in a call to
905 begin_for_stmt or begin_range_for_stmt. */
907 tree
908 begin_for_scope (tree *init)
910 tree scope = NULL_TREE;
911 if (flag_new_for_scope > 0)
912 scope = do_pushlevel (sk_for);
914 if (processing_template_decl)
915 *init = push_stmt_list ();
916 else
917 *init = NULL_TREE;
919 return scope;
922 /* Begin a for-statement. Returns a new FOR_STMT.
923 SCOPE and INIT should be the return of begin_for_scope,
924 or both NULL_TREE */
926 tree
927 begin_for_stmt (tree scope, tree init)
929 tree r;
931 r = build_stmt (input_location, FOR_STMT, NULL_TREE, NULL_TREE,
932 NULL_TREE, NULL_TREE, NULL_TREE);
934 if (scope == NULL_TREE)
936 gcc_assert (!init || !(flag_new_for_scope > 0));
937 if (!init)
938 scope = begin_for_scope (&init);
940 FOR_INIT_STMT (r) = init;
941 FOR_SCOPE (r) = scope;
943 return r;
946 /* Finish the for-init-statement of a for-statement, which may be
947 given by FOR_STMT. */
949 void
950 finish_for_init_stmt (tree for_stmt)
952 if (processing_template_decl)
953 FOR_INIT_STMT (for_stmt) = pop_stmt_list (FOR_INIT_STMT (for_stmt));
954 add_stmt (for_stmt);
955 FOR_BODY (for_stmt) = do_pushlevel (sk_block);
956 begin_cond (&FOR_COND (for_stmt));
959 /* Finish the COND of a for-statement, which may be given by
960 FOR_STMT. */
962 void
963 finish_for_cond (tree cond, tree for_stmt, bool ivdep)
965 if (check_no_cilk (cond,
966 "Cilk array notation cannot be used in a condition for a for-loop",
967 "%<_Cilk_spawn%> statement cannot be used in a condition for a for-loop"))
968 cond = error_mark_node;
969 cond = maybe_convert_cond (cond);
970 finish_cond (&FOR_COND (for_stmt), cond);
971 begin_maybe_infinite_loop (cond);
972 if (ivdep && cond != error_mark_node)
973 FOR_COND (for_stmt) = build2 (ANNOTATE_EXPR,
974 TREE_TYPE (FOR_COND (for_stmt)),
975 FOR_COND (for_stmt),
976 build_int_cst (integer_type_node,
977 annot_expr_ivdep_kind));
978 simplify_loop_decl_cond (&FOR_COND (for_stmt), FOR_BODY (for_stmt));
981 /* Finish the increment-EXPRESSION in a for-statement, which may be
982 given by FOR_STMT. */
984 void
985 finish_for_expr (tree expr, tree for_stmt)
987 if (!expr)
988 return;
989 /* If EXPR is an overloaded function, issue an error; there is no
990 context available to use to perform overload resolution. */
991 if (type_unknown_p (expr))
993 cxx_incomplete_type_error (expr, TREE_TYPE (expr));
994 expr = error_mark_node;
996 if (!processing_template_decl)
998 if (warn_sequence_point)
999 verify_sequence_points (expr);
1000 expr = convert_to_void (expr, ICV_THIRD_IN_FOR,
1001 tf_warning_or_error);
1003 else if (!type_dependent_expression_p (expr))
1004 convert_to_void (build_non_dependent_expr (expr), ICV_THIRD_IN_FOR,
1005 tf_warning_or_error);
1006 expr = maybe_cleanup_point_expr_void (expr);
1007 if (check_for_bare_parameter_packs (expr))
1008 expr = error_mark_node;
1009 FOR_EXPR (for_stmt) = expr;
1012 /* Finish the body of a for-statement, which may be given by
1013 FOR_STMT. The increment-EXPR for the loop must be
1014 provided.
1015 It can also finish RANGE_FOR_STMT. */
1017 void
1018 finish_for_stmt (tree for_stmt)
1020 end_maybe_infinite_loop (boolean_true_node);
1022 if (TREE_CODE (for_stmt) == RANGE_FOR_STMT)
1023 RANGE_FOR_BODY (for_stmt) = do_poplevel (RANGE_FOR_BODY (for_stmt));
1024 else
1025 FOR_BODY (for_stmt) = do_poplevel (FOR_BODY (for_stmt));
1027 /* Pop the scope for the body of the loop. */
1028 if (flag_new_for_scope > 0)
1030 tree scope;
1031 tree *scope_ptr = (TREE_CODE (for_stmt) == RANGE_FOR_STMT
1032 ? &RANGE_FOR_SCOPE (for_stmt)
1033 : &FOR_SCOPE (for_stmt));
1034 scope = *scope_ptr;
1035 *scope_ptr = NULL;
1036 add_stmt (do_poplevel (scope));
1040 /* Begin a range-for-statement. Returns a new RANGE_FOR_STMT.
1041 SCOPE and INIT should be the return of begin_for_scope,
1042 or both NULL_TREE .
1043 To finish it call finish_for_stmt(). */
1045 tree
1046 begin_range_for_stmt (tree scope, tree init)
1048 tree r;
1050 begin_maybe_infinite_loop (boolean_false_node);
1052 r = build_stmt (input_location, RANGE_FOR_STMT,
1053 NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE);
1055 if (scope == NULL_TREE)
1057 gcc_assert (!init || !(flag_new_for_scope > 0));
1058 if (!init)
1059 scope = begin_for_scope (&init);
1062 /* RANGE_FOR_STMTs do not use nor save the init tree, so we
1063 pop it now. */
1064 if (init)
1065 pop_stmt_list (init);
1066 RANGE_FOR_SCOPE (r) = scope;
1068 return r;
1071 /* Finish the head of a range-based for statement, which may
1072 be given by RANGE_FOR_STMT. DECL must be the declaration
1073 and EXPR must be the loop expression. */
1075 void
1076 finish_range_for_decl (tree range_for_stmt, tree decl, tree expr)
1078 RANGE_FOR_DECL (range_for_stmt) = decl;
1079 RANGE_FOR_EXPR (range_for_stmt) = expr;
1080 add_stmt (range_for_stmt);
1081 RANGE_FOR_BODY (range_for_stmt) = do_pushlevel (sk_block);
1084 /* Finish a break-statement. */
1086 tree
1087 finish_break_stmt (void)
1089 /* In switch statements break is sometimes stylistically used after
1090 a return statement. This can lead to spurious warnings about
1091 control reaching the end of a non-void function when it is
1092 inlined. Note that we are calling block_may_fallthru with
1093 language specific tree nodes; this works because
1094 block_may_fallthru returns true when given something it does not
1095 understand. */
1096 if (!block_may_fallthru (cur_stmt_list))
1097 return void_node;
1098 return add_stmt (build_stmt (input_location, BREAK_STMT));
1101 /* Finish a continue-statement. */
1103 tree
1104 finish_continue_stmt (void)
1106 return add_stmt (build_stmt (input_location, CONTINUE_STMT));
1109 /* Begin a switch-statement. Returns a new SWITCH_STMT if
1110 appropriate. */
1112 tree
1113 begin_switch_stmt (void)
1115 tree r, scope;
1117 scope = do_pushlevel (sk_cond);
1118 r = build_stmt (input_location, SWITCH_STMT, NULL_TREE, NULL_TREE, NULL_TREE, scope);
1120 begin_cond (&SWITCH_STMT_COND (r));
1122 return r;
1125 /* Finish the cond of a switch-statement. */
1127 void
1128 finish_switch_cond (tree cond, tree switch_stmt)
1130 tree orig_type = NULL;
1132 if (check_no_cilk (cond,
1133 "Cilk array notation cannot be used as a condition for switch statement",
1134 "%<_Cilk_spawn%> statement cannot be used as a condition for switch statement"))
1135 cond = error_mark_node;
1137 if (!processing_template_decl)
1139 /* Convert the condition to an integer or enumeration type. */
1140 cond = build_expr_type_conversion (WANT_INT | WANT_ENUM, cond, true);
1141 if (cond == NULL_TREE)
1143 error ("switch quantity not an integer");
1144 cond = error_mark_node;
1146 /* We want unlowered type here to handle enum bit-fields. */
1147 orig_type = unlowered_expr_type (cond);
1148 if (TREE_CODE (orig_type) != ENUMERAL_TYPE)
1149 orig_type = TREE_TYPE (cond);
1150 if (cond != error_mark_node)
1152 /* [stmt.switch]
1154 Integral promotions are performed. */
1155 cond = perform_integral_promotions (cond);
1156 cond = maybe_cleanup_point_expr (cond);
1159 if (check_for_bare_parameter_packs (cond))
1160 cond = error_mark_node;
1161 else if (!processing_template_decl && warn_sequence_point)
1162 verify_sequence_points (cond);
1164 finish_cond (&SWITCH_STMT_COND (switch_stmt), cond);
1165 SWITCH_STMT_TYPE (switch_stmt) = orig_type;
1166 add_stmt (switch_stmt);
1167 push_switch (switch_stmt);
1168 SWITCH_STMT_BODY (switch_stmt) = push_stmt_list ();
1171 /* Finish the body of a switch-statement, which may be given by
1172 SWITCH_STMT. The COND to switch on is indicated. */
1174 void
1175 finish_switch_stmt (tree switch_stmt)
1177 tree scope;
1179 SWITCH_STMT_BODY (switch_stmt) =
1180 pop_stmt_list (SWITCH_STMT_BODY (switch_stmt));
1181 pop_switch ();
1183 scope = SWITCH_STMT_SCOPE (switch_stmt);
1184 SWITCH_STMT_SCOPE (switch_stmt) = NULL;
1185 add_stmt (do_poplevel (scope));
1188 /* Begin a try-block. Returns a newly-created TRY_BLOCK if
1189 appropriate. */
1191 tree
1192 begin_try_block (void)
1194 tree r = build_stmt (input_location, TRY_BLOCK, NULL_TREE, NULL_TREE);
1195 add_stmt (r);
1196 TRY_STMTS (r) = push_stmt_list ();
1197 return r;
1200 /* Likewise, for a function-try-block. The block returned in
1201 *COMPOUND_STMT is an artificial outer scope, containing the
1202 function-try-block. */
1204 tree
1205 begin_function_try_block (tree *compound_stmt)
1207 tree r;
1208 /* This outer scope does not exist in the C++ standard, but we need
1209 a place to put __FUNCTION__ and similar variables. */
1210 *compound_stmt = begin_compound_stmt (0);
1211 r = begin_try_block ();
1212 FN_TRY_BLOCK_P (r) = 1;
1213 return r;
1216 /* Finish a try-block, which may be given by TRY_BLOCK. */
1218 void
1219 finish_try_block (tree try_block)
1221 TRY_STMTS (try_block) = pop_stmt_list (TRY_STMTS (try_block));
1222 TRY_HANDLERS (try_block) = push_stmt_list ();
1225 /* Finish the body of a cleanup try-block, which may be given by
1226 TRY_BLOCK. */
1228 void
1229 finish_cleanup_try_block (tree try_block)
1231 TRY_STMTS (try_block) = pop_stmt_list (TRY_STMTS (try_block));
1234 /* Finish an implicitly generated try-block, with a cleanup is given
1235 by CLEANUP. */
1237 void
1238 finish_cleanup (tree cleanup, tree try_block)
1240 TRY_HANDLERS (try_block) = cleanup;
1241 CLEANUP_P (try_block) = 1;
1244 /* Likewise, for a function-try-block. */
1246 void
1247 finish_function_try_block (tree try_block)
1249 finish_try_block (try_block);
1250 /* FIXME : something queer about CTOR_INITIALIZER somehow following
1251 the try block, but moving it inside. */
1252 in_function_try_handler = 1;
1255 /* Finish a handler-sequence for a try-block, which may be given by
1256 TRY_BLOCK. */
1258 void
1259 finish_handler_sequence (tree try_block)
1261 TRY_HANDLERS (try_block) = pop_stmt_list (TRY_HANDLERS (try_block));
1262 check_handlers (TRY_HANDLERS (try_block));
1265 /* Finish the handler-seq for a function-try-block, given by
1266 TRY_BLOCK. COMPOUND_STMT is the outer block created by
1267 begin_function_try_block. */
1269 void
1270 finish_function_handler_sequence (tree try_block, tree compound_stmt)
1272 in_function_try_handler = 0;
1273 finish_handler_sequence (try_block);
1274 finish_compound_stmt (compound_stmt);
1277 /* Begin a handler. Returns a HANDLER if appropriate. */
1279 tree
1280 begin_handler (void)
1282 tree r;
1284 r = build_stmt (input_location, HANDLER, NULL_TREE, NULL_TREE);
1285 add_stmt (r);
1287 /* Create a binding level for the eh_info and the exception object
1288 cleanup. */
1289 HANDLER_BODY (r) = do_pushlevel (sk_catch);
1291 return r;
1294 /* Finish the handler-parameters for a handler, which may be given by
1295 HANDLER. DECL is the declaration for the catch parameter, or NULL
1296 if this is a `catch (...)' clause. */
1298 void
1299 finish_handler_parms (tree decl, tree handler)
1301 tree type = NULL_TREE;
1302 if (processing_template_decl)
1304 if (decl)
1306 decl = pushdecl (decl);
1307 decl = push_template_decl (decl);
1308 HANDLER_PARMS (handler) = decl;
1309 type = TREE_TYPE (decl);
1312 else
1313 type = expand_start_catch_block (decl);
1314 HANDLER_TYPE (handler) = type;
1317 /* Finish a handler, which may be given by HANDLER. The BLOCKs are
1318 the return value from the matching call to finish_handler_parms. */
1320 void
1321 finish_handler (tree handler)
1323 if (!processing_template_decl)
1324 expand_end_catch_block ();
1325 HANDLER_BODY (handler) = do_poplevel (HANDLER_BODY (handler));
1328 /* Begin a compound statement. FLAGS contains some bits that control the
1329 behavior and context. If BCS_NO_SCOPE is set, the compound statement
1330 does not define a scope. If BCS_FN_BODY is set, this is the outermost
1331 block of a function. If BCS_TRY_BLOCK is set, this is the block
1332 created on behalf of a TRY statement. Returns a token to be passed to
1333 finish_compound_stmt. */
1335 tree
1336 begin_compound_stmt (unsigned int flags)
1338 tree r;
1340 if (flags & BCS_NO_SCOPE)
1342 r = push_stmt_list ();
1343 STATEMENT_LIST_NO_SCOPE (r) = 1;
1345 /* Normally, we try hard to keep the BLOCK for a statement-expression.
1346 But, if it's a statement-expression with a scopeless block, there's
1347 nothing to keep, and we don't want to accidentally keep a block
1348 *inside* the scopeless block. */
1349 keep_next_level (false);
1351 else
1353 scope_kind sk = sk_block;
1354 if (flags & BCS_TRY_BLOCK)
1355 sk = sk_try;
1356 else if (flags & BCS_TRANSACTION)
1357 sk = sk_transaction;
1358 r = do_pushlevel (sk);
1361 /* When processing a template, we need to remember where the braces were,
1362 so that we can set up identical scopes when instantiating the template
1363 later. BIND_EXPR is a handy candidate for this.
1364 Note that do_poplevel won't create a BIND_EXPR itself here (and thus
1365 result in nested BIND_EXPRs), since we don't build BLOCK nodes when
1366 processing templates. */
1367 if (processing_template_decl)
1369 r = build3 (BIND_EXPR, NULL, NULL, r, NULL);
1370 BIND_EXPR_TRY_BLOCK (r) = (flags & BCS_TRY_BLOCK) != 0;
1371 BIND_EXPR_BODY_BLOCK (r) = (flags & BCS_FN_BODY) != 0;
1372 TREE_SIDE_EFFECTS (r) = 1;
1375 return r;
1378 /* Finish a compound-statement, which is given by STMT. */
1380 void
1381 finish_compound_stmt (tree stmt)
1383 if (TREE_CODE (stmt) == BIND_EXPR)
1385 tree body = do_poplevel (BIND_EXPR_BODY (stmt));
1386 /* If the STATEMENT_LIST is empty and this BIND_EXPR isn't special,
1387 discard the BIND_EXPR so it can be merged with the containing
1388 STATEMENT_LIST. */
1389 if (TREE_CODE (body) == STATEMENT_LIST
1390 && STATEMENT_LIST_HEAD (body) == NULL
1391 && !BIND_EXPR_BODY_BLOCK (stmt)
1392 && !BIND_EXPR_TRY_BLOCK (stmt))
1393 stmt = body;
1394 else
1395 BIND_EXPR_BODY (stmt) = body;
1397 else if (STATEMENT_LIST_NO_SCOPE (stmt))
1398 stmt = pop_stmt_list (stmt);
1399 else
1401 /* Destroy any ObjC "super" receivers that may have been
1402 created. */
1403 objc_clear_super_receiver ();
1405 stmt = do_poplevel (stmt);
1408 /* ??? See c_end_compound_stmt wrt statement expressions. */
1409 add_stmt (stmt);
1412 /* Finish an asm-statement, whose components are a STRING, some
1413 OUTPUT_OPERANDS, some INPUT_OPERANDS, some CLOBBERS and some
1414 LABELS. Also note whether the asm-statement should be
1415 considered volatile. */
1417 tree
1418 finish_asm_stmt (int volatile_p, tree string, tree output_operands,
1419 tree input_operands, tree clobbers, tree labels)
1421 tree r;
1422 tree t;
1423 int ninputs = list_length (input_operands);
1424 int noutputs = list_length (output_operands);
1426 if (!processing_template_decl)
1428 const char *constraint;
1429 const char **oconstraints;
1430 bool allows_mem, allows_reg, is_inout;
1431 tree operand;
1432 int i;
1434 oconstraints = XALLOCAVEC (const char *, noutputs);
1436 string = resolve_asm_operand_names (string, output_operands,
1437 input_operands, labels);
1439 for (i = 0, t = output_operands; t; t = TREE_CHAIN (t), ++i)
1441 operand = TREE_VALUE (t);
1443 /* ??? Really, this should not be here. Users should be using a
1444 proper lvalue, dammit. But there's a long history of using
1445 casts in the output operands. In cases like longlong.h, this
1446 becomes a primitive form of typechecking -- if the cast can be
1447 removed, then the output operand had a type of the proper width;
1448 otherwise we'll get an error. Gross, but ... */
1449 STRIP_NOPS (operand);
1451 operand = mark_lvalue_use (operand);
1453 if (!lvalue_or_else (operand, lv_asm, tf_warning_or_error))
1454 operand = error_mark_node;
1456 if (operand != error_mark_node
1457 && (TREE_READONLY (operand)
1458 || CP_TYPE_CONST_P (TREE_TYPE (operand))
1459 /* Functions are not modifiable, even though they are
1460 lvalues. */
1461 || TREE_CODE (TREE_TYPE (operand)) == FUNCTION_TYPE
1462 || TREE_CODE (TREE_TYPE (operand)) == METHOD_TYPE
1463 /* If it's an aggregate and any field is const, then it is
1464 effectively const. */
1465 || (CLASS_TYPE_P (TREE_TYPE (operand))
1466 && C_TYPE_FIELDS_READONLY (TREE_TYPE (operand)))))
1467 cxx_readonly_error (operand, lv_asm);
1469 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
1470 oconstraints[i] = constraint;
1472 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
1473 &allows_mem, &allows_reg, &is_inout))
1475 /* If the operand is going to end up in memory,
1476 mark it addressable. */
1477 if (!allows_reg && !cxx_mark_addressable (operand))
1478 operand = error_mark_node;
1480 else
1481 operand = error_mark_node;
1483 TREE_VALUE (t) = operand;
1486 for (i = 0, t = input_operands; t; ++i, t = TREE_CHAIN (t))
1488 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
1489 bool constraint_parsed
1490 = parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
1491 oconstraints, &allows_mem, &allows_reg);
1492 /* If the operand is going to end up in memory, don't call
1493 decay_conversion. */
1494 if (constraint_parsed && !allows_reg && allows_mem)
1495 operand = mark_lvalue_use (TREE_VALUE (t));
1496 else
1497 operand = decay_conversion (TREE_VALUE (t), tf_warning_or_error);
1499 /* If the type of the operand hasn't been determined (e.g.,
1500 because it involves an overloaded function), then issue
1501 an error message. There's no context available to
1502 resolve the overloading. */
1503 if (TREE_TYPE (operand) == unknown_type_node)
1505 error ("type of asm operand %qE could not be determined",
1506 TREE_VALUE (t));
1507 operand = error_mark_node;
1510 if (constraint_parsed)
1512 /* If the operand is going to end up in memory,
1513 mark it addressable. */
1514 if (!allows_reg && allows_mem)
1516 /* Strip the nops as we allow this case. FIXME, this really
1517 should be rejected or made deprecated. */
1518 STRIP_NOPS (operand);
1519 if (!cxx_mark_addressable (operand))
1520 operand = error_mark_node;
1522 else if (!allows_reg && !allows_mem)
1524 /* If constraint allows neither register nor memory,
1525 try harder to get a constant. */
1526 tree constop = maybe_constant_value (operand);
1527 if (TREE_CONSTANT (constop))
1528 operand = constop;
1531 else
1532 operand = error_mark_node;
1534 TREE_VALUE (t) = operand;
1538 r = build_stmt (input_location, ASM_EXPR, string,
1539 output_operands, input_operands,
1540 clobbers, labels);
1541 ASM_VOLATILE_P (r) = volatile_p || noutputs == 0;
1542 r = maybe_cleanup_point_expr_void (r);
1543 return add_stmt (r);
1546 /* Finish a label with the indicated NAME. Returns the new label. */
1548 tree
1549 finish_label_stmt (tree name)
1551 tree decl = define_label (input_location, name);
1553 if (decl == error_mark_node)
1554 return error_mark_node;
1556 add_stmt (build_stmt (input_location, LABEL_EXPR, decl));
1558 return decl;
1561 /* Finish a series of declarations for local labels. G++ allows users
1562 to declare "local" labels, i.e., labels with scope. This extension
1563 is useful when writing code involving statement-expressions. */
1565 void
1566 finish_label_decl (tree name)
1568 if (!at_function_scope_p ())
1570 error ("__label__ declarations are only allowed in function scopes");
1571 return;
1574 add_decl_expr (declare_local_label (name));
1577 /* When DECL goes out of scope, make sure that CLEANUP is executed. */
1579 void
1580 finish_decl_cleanup (tree decl, tree cleanup)
1582 push_cleanup (decl, cleanup, false);
1585 /* If the current scope exits with an exception, run CLEANUP. */
1587 void
1588 finish_eh_cleanup (tree cleanup)
1590 push_cleanup (NULL, cleanup, true);
1593 /* The MEM_INITS is a list of mem-initializers, in reverse of the
1594 order they were written by the user. Each node is as for
1595 emit_mem_initializers. */
1597 void
1598 finish_mem_initializers (tree mem_inits)
1600 /* Reorder the MEM_INITS so that they are in the order they appeared
1601 in the source program. */
1602 mem_inits = nreverse (mem_inits);
1604 if (processing_template_decl)
1606 tree mem;
1608 for (mem = mem_inits; mem; mem = TREE_CHAIN (mem))
1610 /* If the TREE_PURPOSE is a TYPE_PACK_EXPANSION, skip the
1611 check for bare parameter packs in the TREE_VALUE, because
1612 any parameter packs in the TREE_VALUE have already been
1613 bound as part of the TREE_PURPOSE. See
1614 make_pack_expansion for more information. */
1615 if (TREE_CODE (TREE_PURPOSE (mem)) != TYPE_PACK_EXPANSION
1616 && check_for_bare_parameter_packs (TREE_VALUE (mem)))
1617 TREE_VALUE (mem) = error_mark_node;
1620 add_stmt (build_min_nt_loc (UNKNOWN_LOCATION,
1621 CTOR_INITIALIZER, mem_inits));
1623 else
1624 emit_mem_initializers (mem_inits);
1627 /* Obfuscate EXPR if it looks like an id-expression or member access so
1628 that the call to finish_decltype in do_auto_deduction will give the
1629 right result. */
1631 tree
1632 force_paren_expr (tree expr)
1634 /* This is only needed for decltype(auto) in C++14. */
1635 if (cxx_dialect < cxx14)
1636 return expr;
1638 /* If we're in unevaluated context, we can't be deducing a
1639 return/initializer type, so we don't need to mess with this. */
1640 if (cp_unevaluated_operand)
1641 return expr;
1643 if (!DECL_P (expr) && TREE_CODE (expr) != COMPONENT_REF
1644 && TREE_CODE (expr) != SCOPE_REF)
1645 return expr;
1647 if (TREE_CODE (expr) == COMPONENT_REF)
1648 REF_PARENTHESIZED_P (expr) = true;
1649 else if (type_dependent_expression_p (expr))
1650 expr = build1 (PAREN_EXPR, TREE_TYPE (expr), expr);
1651 else if (VAR_P (expr) && DECL_HARD_REGISTER (expr))
1652 /* We can't bind a hard register variable to a reference. */;
1653 else
1655 cp_lvalue_kind kind = lvalue_kind (expr);
1656 if ((kind & ~clk_class) != clk_none)
1658 tree type = unlowered_expr_type (expr);
1659 bool rval = !!(kind & clk_rvalueref);
1660 type = cp_build_reference_type (type, rval);
1661 /* This inhibits warnings in, eg, cxx_mark_addressable
1662 (c++/60955). */
1663 warning_sentinel s (extra_warnings);
1664 expr = build_static_cast (type, expr, tf_error);
1665 if (expr != error_mark_node)
1666 REF_PARENTHESIZED_P (expr) = true;
1670 return expr;
1673 /* Finish a parenthesized expression EXPR. */
1675 tree
1676 finish_parenthesized_expr (tree expr)
1678 if (EXPR_P (expr))
1679 /* This inhibits warnings in c_common_truthvalue_conversion. */
1680 TREE_NO_WARNING (expr) = 1;
1682 if (TREE_CODE (expr) == OFFSET_REF
1683 || TREE_CODE (expr) == SCOPE_REF)
1684 /* [expr.unary.op]/3 The qualified id of a pointer-to-member must not be
1685 enclosed in parentheses. */
1686 PTRMEM_OK_P (expr) = 0;
1688 if (TREE_CODE (expr) == STRING_CST)
1689 PAREN_STRING_LITERAL_P (expr) = 1;
1691 expr = force_paren_expr (expr);
1693 return expr;
1696 /* Finish a reference to a non-static data member (DECL) that is not
1697 preceded by `.' or `->'. */
1699 tree
1700 finish_non_static_data_member (tree decl, tree object, tree qualifying_scope)
1702 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
1703 bool try_omp_private = !object && omp_private_member_map;
1704 tree ret;
1706 if (!object)
1708 tree scope = qualifying_scope;
1709 if (scope == NULL_TREE)
1710 scope = context_for_name_lookup (decl);
1711 object = maybe_dummy_object (scope, NULL);
1714 object = maybe_resolve_dummy (object, true);
1715 if (object == error_mark_node)
1716 return error_mark_node;
1718 /* DR 613/850: Can use non-static data members without an associated
1719 object in sizeof/decltype/alignof. */
1720 if (is_dummy_object (object) && cp_unevaluated_operand == 0
1721 && (!processing_template_decl || !current_class_ref))
1723 if (current_function_decl
1724 && DECL_STATIC_FUNCTION_P (current_function_decl))
1725 error ("invalid use of member %qD in static member function", decl);
1726 else
1727 error ("invalid use of non-static data member %qD", decl);
1728 inform (DECL_SOURCE_LOCATION (decl), "declared here");
1730 return error_mark_node;
1733 if (current_class_ptr)
1734 TREE_USED (current_class_ptr) = 1;
1735 if (processing_template_decl && !qualifying_scope)
1737 tree type = TREE_TYPE (decl);
1739 if (TREE_CODE (type) == REFERENCE_TYPE)
1740 /* Quals on the object don't matter. */;
1741 else if (PACK_EXPANSION_P (type))
1742 /* Don't bother trying to represent this. */
1743 type = NULL_TREE;
1744 else
1746 /* Set the cv qualifiers. */
1747 int quals = cp_type_quals (TREE_TYPE (object));
1749 if (DECL_MUTABLE_P (decl))
1750 quals &= ~TYPE_QUAL_CONST;
1752 quals |= cp_type_quals (TREE_TYPE (decl));
1753 type = cp_build_qualified_type (type, quals);
1756 ret = (convert_from_reference
1757 (build_min (COMPONENT_REF, type, object, decl, NULL_TREE)));
1759 /* If PROCESSING_TEMPLATE_DECL is nonzero here, then
1760 QUALIFYING_SCOPE is also non-null. Wrap this in a SCOPE_REF
1761 for now. */
1762 else if (processing_template_decl)
1763 ret = build_qualified_name (TREE_TYPE (decl),
1764 qualifying_scope,
1765 decl,
1766 /*template_p=*/false);
1767 else
1769 tree access_type = TREE_TYPE (object);
1771 perform_or_defer_access_check (TYPE_BINFO (access_type), decl,
1772 decl, tf_warning_or_error);
1774 /* If the data member was named `C::M', convert `*this' to `C'
1775 first. */
1776 if (qualifying_scope)
1778 tree binfo = NULL_TREE;
1779 object = build_scoped_ref (object, qualifying_scope,
1780 &binfo);
1783 ret = build_class_member_access_expr (object, decl,
1784 /*access_path=*/NULL_TREE,
1785 /*preserve_reference=*/false,
1786 tf_warning_or_error);
1788 if (try_omp_private)
1790 tree *v = omp_private_member_map->get (decl);
1791 if (v)
1792 ret = convert_from_reference (*v);
1794 return ret;
1797 /* If we are currently parsing a template and we encountered a typedef
1798 TYPEDEF_DECL that is being accessed though CONTEXT, this function
1799 adds the typedef to a list tied to the current template.
1800 At template instantiation time, that list is walked and access check
1801 performed for each typedef.
1802 LOCATION is the location of the usage point of TYPEDEF_DECL. */
1804 void
1805 add_typedef_to_current_template_for_access_check (tree typedef_decl,
1806 tree context,
1807 location_t location)
1809 tree template_info = NULL;
1810 tree cs = current_scope ();
1812 if (!is_typedef_decl (typedef_decl)
1813 || !context
1814 || !CLASS_TYPE_P (context)
1815 || !cs)
1816 return;
1818 if (CLASS_TYPE_P (cs) || TREE_CODE (cs) == FUNCTION_DECL)
1819 template_info = get_template_info (cs);
1821 if (template_info
1822 && TI_TEMPLATE (template_info)
1823 && !currently_open_class (context))
1824 append_type_to_template_for_access_check (cs, typedef_decl,
1825 context, location);
1828 /* DECL was the declaration to which a qualified-id resolved. Issue
1829 an error message if it is not accessible. If OBJECT_TYPE is
1830 non-NULL, we have just seen `x->' or `x.' and OBJECT_TYPE is the
1831 type of `*x', or `x', respectively. If the DECL was named as
1832 `A::B' then NESTED_NAME_SPECIFIER is `A'. */
1834 void
1835 check_accessibility_of_qualified_id (tree decl,
1836 tree object_type,
1837 tree nested_name_specifier)
1839 tree scope;
1840 tree qualifying_type = NULL_TREE;
1842 /* If we are parsing a template declaration and if decl is a typedef,
1843 add it to a list tied to the template.
1844 At template instantiation time, that list will be walked and
1845 access check performed. */
1846 add_typedef_to_current_template_for_access_check (decl,
1847 nested_name_specifier
1848 ? nested_name_specifier
1849 : DECL_CONTEXT (decl),
1850 input_location);
1852 /* If we're not checking, return immediately. */
1853 if (deferred_access_no_check)
1854 return;
1856 /* Determine the SCOPE of DECL. */
1857 scope = context_for_name_lookup (decl);
1858 /* If the SCOPE is not a type, then DECL is not a member. */
1859 if (!TYPE_P (scope))
1860 return;
1861 /* Compute the scope through which DECL is being accessed. */
1862 if (object_type
1863 /* OBJECT_TYPE might not be a class type; consider:
1865 class A { typedef int I; };
1866 I *p;
1867 p->A::I::~I();
1869 In this case, we will have "A::I" as the DECL, but "I" as the
1870 OBJECT_TYPE. */
1871 && CLASS_TYPE_P (object_type)
1872 && DERIVED_FROM_P (scope, object_type))
1873 /* If we are processing a `->' or `.' expression, use the type of the
1874 left-hand side. */
1875 qualifying_type = object_type;
1876 else if (nested_name_specifier)
1878 /* If the reference is to a non-static member of the
1879 current class, treat it as if it were referenced through
1880 `this'. */
1881 tree ct;
1882 if (DECL_NONSTATIC_MEMBER_P (decl)
1883 && current_class_ptr
1884 && DERIVED_FROM_P (scope, ct = current_nonlambda_class_type ()))
1885 qualifying_type = ct;
1886 /* Otherwise, use the type indicated by the
1887 nested-name-specifier. */
1888 else
1889 qualifying_type = nested_name_specifier;
1891 else
1892 /* Otherwise, the name must be from the current class or one of
1893 its bases. */
1894 qualifying_type = currently_open_derived_class (scope);
1896 if (qualifying_type
1897 /* It is possible for qualifying type to be a TEMPLATE_TYPE_PARM
1898 or similar in a default argument value. */
1899 && CLASS_TYPE_P (qualifying_type)
1900 && !dependent_type_p (qualifying_type))
1901 perform_or_defer_access_check (TYPE_BINFO (qualifying_type), decl,
1902 decl, tf_warning_or_error);
1905 /* EXPR is the result of a qualified-id. The QUALIFYING_CLASS was the
1906 class named to the left of the "::" operator. DONE is true if this
1907 expression is a complete postfix-expression; it is false if this
1908 expression is followed by '->', '[', '(', etc. ADDRESS_P is true
1909 iff this expression is the operand of '&'. TEMPLATE_P is true iff
1910 the qualified-id was of the form "A::template B". TEMPLATE_ARG_P
1911 is true iff this qualified name appears as a template argument. */
1913 tree
1914 finish_qualified_id_expr (tree qualifying_class,
1915 tree expr,
1916 bool done,
1917 bool address_p,
1918 bool template_p,
1919 bool template_arg_p,
1920 tsubst_flags_t complain)
1922 gcc_assert (TYPE_P (qualifying_class));
1924 if (error_operand_p (expr))
1925 return error_mark_node;
1927 if ((DECL_P (expr) || BASELINK_P (expr))
1928 && !mark_used (expr, complain))
1929 return error_mark_node;
1931 if (template_p)
1932 check_template_keyword (expr);
1934 /* If EXPR occurs as the operand of '&', use special handling that
1935 permits a pointer-to-member. */
1936 if (address_p && done)
1938 if (TREE_CODE (expr) == SCOPE_REF)
1939 expr = TREE_OPERAND (expr, 1);
1940 expr = build_offset_ref (qualifying_class, expr,
1941 /*address_p=*/true, complain);
1942 return expr;
1945 /* No need to check access within an enum. */
1946 if (TREE_CODE (qualifying_class) == ENUMERAL_TYPE)
1947 return expr;
1949 /* Within the scope of a class, turn references to non-static
1950 members into expression of the form "this->...". */
1951 if (template_arg_p)
1952 /* But, within a template argument, we do not want make the
1953 transformation, as there is no "this" pointer. */
1955 else if (TREE_CODE (expr) == FIELD_DECL)
1957 push_deferring_access_checks (dk_no_check);
1958 expr = finish_non_static_data_member (expr, NULL_TREE,
1959 qualifying_class);
1960 pop_deferring_access_checks ();
1962 else if (BASELINK_P (expr) && !processing_template_decl)
1964 /* See if any of the functions are non-static members. */
1965 /* If so, the expression may be relative to 'this'. */
1966 if (!shared_member_p (expr)
1967 && current_class_ptr
1968 && DERIVED_FROM_P (qualifying_class,
1969 current_nonlambda_class_type ()))
1970 expr = (build_class_member_access_expr
1971 (maybe_dummy_object (qualifying_class, NULL),
1972 expr,
1973 BASELINK_ACCESS_BINFO (expr),
1974 /*preserve_reference=*/false,
1975 complain));
1976 else if (done)
1977 /* The expression is a qualified name whose address is not
1978 being taken. */
1979 expr = build_offset_ref (qualifying_class, expr, /*address_p=*/false,
1980 complain);
1982 else if (BASELINK_P (expr))
1984 else
1986 /* In a template, return a SCOPE_REF for most qualified-ids
1987 so that we can check access at instantiation time. But if
1988 we're looking at a member of the current instantiation, we
1989 know we have access and building up the SCOPE_REF confuses
1990 non-type template argument handling. */
1991 if (processing_template_decl
1992 && !currently_open_class (qualifying_class))
1993 expr = build_qualified_name (TREE_TYPE (expr),
1994 qualifying_class, expr,
1995 template_p);
1997 expr = convert_from_reference (expr);
2000 return expr;
2003 /* Begin a statement-expression. The value returned must be passed to
2004 finish_stmt_expr. */
2006 tree
2007 begin_stmt_expr (void)
2009 return push_stmt_list ();
2012 /* Process the final expression of a statement expression. EXPR can be
2013 NULL, if the final expression is empty. Return a STATEMENT_LIST
2014 containing all the statements in the statement-expression, or
2015 ERROR_MARK_NODE if there was an error. */
2017 tree
2018 finish_stmt_expr_expr (tree expr, tree stmt_expr)
2020 if (error_operand_p (expr))
2022 /* The type of the statement-expression is the type of the last
2023 expression. */
2024 TREE_TYPE (stmt_expr) = error_mark_node;
2025 return error_mark_node;
2028 /* If the last statement does not have "void" type, then the value
2029 of the last statement is the value of the entire expression. */
2030 if (expr)
2032 tree type = TREE_TYPE (expr);
2034 if (processing_template_decl)
2036 expr = build_stmt (input_location, EXPR_STMT, expr);
2037 expr = add_stmt (expr);
2038 /* Mark the last statement so that we can recognize it as such at
2039 template-instantiation time. */
2040 EXPR_STMT_STMT_EXPR_RESULT (expr) = 1;
2042 else if (VOID_TYPE_P (type))
2044 /* Just treat this like an ordinary statement. */
2045 expr = finish_expr_stmt (expr);
2047 else
2049 /* It actually has a value we need to deal with. First, force it
2050 to be an rvalue so that we won't need to build up a copy
2051 constructor call later when we try to assign it to something. */
2052 expr = force_rvalue (expr, tf_warning_or_error);
2053 if (error_operand_p (expr))
2054 return error_mark_node;
2056 /* Update for array-to-pointer decay. */
2057 type = TREE_TYPE (expr);
2059 /* Wrap it in a CLEANUP_POINT_EXPR and add it to the list like a
2060 normal statement, but don't convert to void or actually add
2061 the EXPR_STMT. */
2062 if (TREE_CODE (expr) != CLEANUP_POINT_EXPR)
2063 expr = maybe_cleanup_point_expr (expr);
2064 add_stmt (expr);
2067 /* The type of the statement-expression is the type of the last
2068 expression. */
2069 TREE_TYPE (stmt_expr) = type;
2072 return stmt_expr;
2075 /* Finish a statement-expression. EXPR should be the value returned
2076 by the previous begin_stmt_expr. Returns an expression
2077 representing the statement-expression. */
2079 tree
2080 finish_stmt_expr (tree stmt_expr, bool has_no_scope)
2082 tree type;
2083 tree result;
2085 if (error_operand_p (stmt_expr))
2087 pop_stmt_list (stmt_expr);
2088 return error_mark_node;
2091 gcc_assert (TREE_CODE (stmt_expr) == STATEMENT_LIST);
2093 type = TREE_TYPE (stmt_expr);
2094 result = pop_stmt_list (stmt_expr);
2095 TREE_TYPE (result) = type;
2097 if (processing_template_decl)
2099 result = build_min (STMT_EXPR, type, result);
2100 TREE_SIDE_EFFECTS (result) = 1;
2101 STMT_EXPR_NO_SCOPE (result) = has_no_scope;
2103 else if (CLASS_TYPE_P (type))
2105 /* Wrap the statement-expression in a TARGET_EXPR so that the
2106 temporary object created by the final expression is destroyed at
2107 the end of the full-expression containing the
2108 statement-expression. */
2109 result = force_target_expr (type, result, tf_warning_or_error);
2112 return result;
2115 /* Returns the expression which provides the value of STMT_EXPR. */
2117 tree
2118 stmt_expr_value_expr (tree stmt_expr)
2120 tree t = STMT_EXPR_STMT (stmt_expr);
2122 if (TREE_CODE (t) == BIND_EXPR)
2123 t = BIND_EXPR_BODY (t);
2125 if (TREE_CODE (t) == STATEMENT_LIST && STATEMENT_LIST_TAIL (t))
2126 t = STATEMENT_LIST_TAIL (t)->stmt;
2128 if (TREE_CODE (t) == EXPR_STMT)
2129 t = EXPR_STMT_EXPR (t);
2131 return t;
2134 /* Return TRUE iff EXPR_STMT is an empty list of
2135 expression statements. */
2137 bool
2138 empty_expr_stmt_p (tree expr_stmt)
2140 tree body = NULL_TREE;
2142 if (expr_stmt == void_node)
2143 return true;
2145 if (expr_stmt)
2147 if (TREE_CODE (expr_stmt) == EXPR_STMT)
2148 body = EXPR_STMT_EXPR (expr_stmt);
2149 else if (TREE_CODE (expr_stmt) == STATEMENT_LIST)
2150 body = expr_stmt;
2153 if (body)
2155 if (TREE_CODE (body) == STATEMENT_LIST)
2156 return tsi_end_p (tsi_start (body));
2157 else
2158 return empty_expr_stmt_p (body);
2160 return false;
2163 /* Perform Koenig lookup. FN is the postfix-expression representing
2164 the function (or functions) to call; ARGS are the arguments to the
2165 call. Returns the functions to be considered by overload resolution. */
2167 tree
2168 perform_koenig_lookup (tree fn, vec<tree, va_gc> *args,
2169 tsubst_flags_t complain)
2171 tree identifier = NULL_TREE;
2172 tree functions = NULL_TREE;
2173 tree tmpl_args = NULL_TREE;
2174 bool template_id = false;
2176 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
2178 /* Use a separate flag to handle null args. */
2179 template_id = true;
2180 tmpl_args = TREE_OPERAND (fn, 1);
2181 fn = TREE_OPERAND (fn, 0);
2184 /* Find the name of the overloaded function. */
2185 if (identifier_p (fn))
2186 identifier = fn;
2187 else if (is_overloaded_fn (fn))
2189 functions = fn;
2190 identifier = DECL_NAME (get_first_fn (functions));
2192 else if (DECL_P (fn))
2194 functions = fn;
2195 identifier = DECL_NAME (fn);
2198 /* A call to a namespace-scope function using an unqualified name.
2200 Do Koenig lookup -- unless any of the arguments are
2201 type-dependent. */
2202 if (!any_type_dependent_arguments_p (args)
2203 && !any_dependent_template_arguments_p (tmpl_args))
2205 fn = lookup_arg_dependent (identifier, functions, args);
2206 if (!fn)
2208 /* The unqualified name could not be resolved. */
2209 if (complain)
2210 fn = unqualified_fn_lookup_error (identifier);
2211 else
2212 fn = identifier;
2216 if (fn && template_id)
2217 fn = build2 (TEMPLATE_ID_EXPR, unknown_type_node, fn, tmpl_args);
2219 return fn;
2222 /* Generate an expression for `FN (ARGS)'. This may change the
2223 contents of ARGS.
2225 If DISALLOW_VIRTUAL is true, the call to FN will be not generated
2226 as a virtual call, even if FN is virtual. (This flag is set when
2227 encountering an expression where the function name is explicitly
2228 qualified. For example a call to `X::f' never generates a virtual
2229 call.)
2231 Returns code for the call. */
2233 tree
2234 finish_call_expr (tree fn, vec<tree, va_gc> **args, bool disallow_virtual,
2235 bool koenig_p, tsubst_flags_t complain)
2237 tree result;
2238 tree orig_fn;
2239 vec<tree, va_gc> *orig_args = NULL;
2241 if (fn == error_mark_node)
2242 return error_mark_node;
2244 gcc_assert (!TYPE_P (fn));
2246 orig_fn = fn;
2248 if (processing_template_decl)
2250 /* If the call expression is dependent, build a CALL_EXPR node
2251 with no type; type_dependent_expression_p recognizes
2252 expressions with no type as being dependent. */
2253 if (type_dependent_expression_p (fn)
2254 || any_type_dependent_arguments_p (*args)
2255 /* For a non-static member function that doesn't have an
2256 explicit object argument, we need to specifically
2257 test the type dependency of the "this" pointer because it
2258 is not included in *ARGS even though it is considered to
2259 be part of the list of arguments. Note that this is
2260 related to CWG issues 515 and 1005. */
2261 || (TREE_CODE (fn) != COMPONENT_REF
2262 && non_static_member_function_p (fn)
2263 && current_class_ref
2264 && type_dependent_expression_p (current_class_ref)))
2266 result = build_nt_call_vec (fn, *args);
2267 SET_EXPR_LOCATION (result, EXPR_LOC_OR_LOC (fn, input_location));
2268 KOENIG_LOOKUP_P (result) = koenig_p;
2269 if (cfun)
2273 tree fndecl = OVL_CURRENT (fn);
2274 if (TREE_CODE (fndecl) != FUNCTION_DECL
2275 || !TREE_THIS_VOLATILE (fndecl))
2276 break;
2277 fn = OVL_NEXT (fn);
2279 while (fn);
2280 if (!fn)
2281 current_function_returns_abnormally = 1;
2283 return result;
2285 orig_args = make_tree_vector_copy (*args);
2286 if (!BASELINK_P (fn)
2287 && TREE_CODE (fn) != PSEUDO_DTOR_EXPR
2288 && TREE_TYPE (fn) != unknown_type_node)
2289 fn = build_non_dependent_expr (fn);
2290 make_args_non_dependent (*args);
2293 if (TREE_CODE (fn) == COMPONENT_REF)
2295 tree member = TREE_OPERAND (fn, 1);
2296 if (BASELINK_P (member))
2298 tree object = TREE_OPERAND (fn, 0);
2299 return build_new_method_call (object, member,
2300 args, NULL_TREE,
2301 (disallow_virtual
2302 ? LOOKUP_NORMAL | LOOKUP_NONVIRTUAL
2303 : LOOKUP_NORMAL),
2304 /*fn_p=*/NULL,
2305 complain);
2309 /* Per 13.3.1.1, '(&f)(...)' is the same as '(f)(...)'. */
2310 if (TREE_CODE (fn) == ADDR_EXPR
2311 && TREE_CODE (TREE_OPERAND (fn, 0)) == OVERLOAD)
2312 fn = TREE_OPERAND (fn, 0);
2314 if (is_overloaded_fn (fn))
2315 fn = baselink_for_fns (fn);
2317 result = NULL_TREE;
2318 if (BASELINK_P (fn))
2320 tree object;
2322 /* A call to a member function. From [over.call.func]:
2324 If the keyword this is in scope and refers to the class of
2325 that member function, or a derived class thereof, then the
2326 function call is transformed into a qualified function call
2327 using (*this) as the postfix-expression to the left of the
2328 . operator.... [Otherwise] a contrived object of type T
2329 becomes the implied object argument.
2331 In this situation:
2333 struct A { void f(); };
2334 struct B : public A {};
2335 struct C : public A { void g() { B::f(); }};
2337 "the class of that member function" refers to `A'. But 11.2
2338 [class.access.base] says that we need to convert 'this' to B* as
2339 part of the access, so we pass 'B' to maybe_dummy_object. */
2341 object = maybe_dummy_object (BINFO_TYPE (BASELINK_ACCESS_BINFO (fn)),
2342 NULL);
2344 if (processing_template_decl)
2346 if (type_dependent_expression_p (object))
2348 tree ret = build_nt_call_vec (orig_fn, orig_args);
2349 release_tree_vector (orig_args);
2350 return ret;
2352 object = build_non_dependent_expr (object);
2355 result = build_new_method_call (object, fn, args, NULL_TREE,
2356 (disallow_virtual
2357 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
2358 : LOOKUP_NORMAL),
2359 /*fn_p=*/NULL,
2360 complain);
2362 else if (is_overloaded_fn (fn))
2364 /* If the function is an overloaded builtin, resolve it. */
2365 if (TREE_CODE (fn) == FUNCTION_DECL
2366 && (DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL
2367 || DECL_BUILT_IN_CLASS (fn) == BUILT_IN_MD))
2368 result = resolve_overloaded_builtin (input_location, fn, *args);
2370 if (!result)
2372 if (warn_sizeof_pointer_memaccess
2373 && (complain & tf_warning)
2374 && !vec_safe_is_empty (*args)
2375 && !processing_template_decl)
2377 location_t sizeof_arg_loc[3];
2378 tree sizeof_arg[3];
2379 unsigned int i;
2380 for (i = 0; i < 3; i++)
2382 tree t;
2384 sizeof_arg_loc[i] = UNKNOWN_LOCATION;
2385 sizeof_arg[i] = NULL_TREE;
2386 if (i >= (*args)->length ())
2387 continue;
2388 t = (**args)[i];
2389 if (TREE_CODE (t) != SIZEOF_EXPR)
2390 continue;
2391 if (SIZEOF_EXPR_TYPE_P (t))
2392 sizeof_arg[i] = TREE_TYPE (TREE_OPERAND (t, 0));
2393 else
2394 sizeof_arg[i] = TREE_OPERAND (t, 0);
2395 sizeof_arg_loc[i] = EXPR_LOCATION (t);
2397 sizeof_pointer_memaccess_warning
2398 (sizeof_arg_loc, fn, *args,
2399 sizeof_arg, same_type_ignoring_top_level_qualifiers_p);
2402 /* A call to a namespace-scope function. */
2403 result = build_new_function_call (fn, args, koenig_p, complain);
2406 else if (TREE_CODE (fn) == PSEUDO_DTOR_EXPR)
2408 if (!vec_safe_is_empty (*args))
2409 error ("arguments to destructor are not allowed");
2410 /* Mark the pseudo-destructor call as having side-effects so
2411 that we do not issue warnings about its use. */
2412 result = build1 (NOP_EXPR,
2413 void_type_node,
2414 TREE_OPERAND (fn, 0));
2415 TREE_SIDE_EFFECTS (result) = 1;
2417 else if (CLASS_TYPE_P (TREE_TYPE (fn)))
2418 /* If the "function" is really an object of class type, it might
2419 have an overloaded `operator ()'. */
2420 result = build_op_call (fn, args, complain);
2422 if (!result)
2423 /* A call where the function is unknown. */
2424 result = cp_build_function_call_vec (fn, args, complain);
2426 if (processing_template_decl && result != error_mark_node)
2428 if (INDIRECT_REF_P (result))
2429 result = TREE_OPERAND (result, 0);
2430 result = build_call_vec (TREE_TYPE (result), orig_fn, orig_args);
2431 SET_EXPR_LOCATION (result, input_location);
2432 KOENIG_LOOKUP_P (result) = koenig_p;
2433 release_tree_vector (orig_args);
2434 result = convert_from_reference (result);
2437 if (koenig_p)
2439 /* Free garbage OVERLOADs from arg-dependent lookup. */
2440 tree next = NULL_TREE;
2441 for (fn = orig_fn;
2442 fn && TREE_CODE (fn) == OVERLOAD && OVL_ARG_DEPENDENT (fn);
2443 fn = next)
2445 if (processing_template_decl)
2446 /* In a template, we'll re-use them at instantiation time. */
2447 OVL_ARG_DEPENDENT (fn) = false;
2448 else
2450 next = OVL_CHAIN (fn);
2451 ggc_free (fn);
2456 return result;
2459 /* Finish a call to a postfix increment or decrement or EXPR. (Which
2460 is indicated by CODE, which should be POSTINCREMENT_EXPR or
2461 POSTDECREMENT_EXPR.) */
2463 tree
2464 finish_increment_expr (tree expr, enum tree_code code)
2466 return build_x_unary_op (input_location, code, expr, tf_warning_or_error);
2469 /* Finish a use of `this'. Returns an expression for `this'. */
2471 tree
2472 finish_this_expr (void)
2474 tree result = NULL_TREE;
2476 if (current_class_ptr)
2478 tree type = TREE_TYPE (current_class_ref);
2480 /* In a lambda expression, 'this' refers to the captured 'this'. */
2481 if (LAMBDA_TYPE_P (type))
2482 result = lambda_expr_this_capture (CLASSTYPE_LAMBDA_EXPR (type), true);
2483 else
2484 result = current_class_ptr;
2487 if (result)
2488 /* The keyword 'this' is a prvalue expression. */
2489 return rvalue (result);
2491 tree fn = current_nonlambda_function ();
2492 if (fn && DECL_STATIC_FUNCTION_P (fn))
2493 error ("%<this%> is unavailable for static member functions");
2494 else if (fn)
2495 error ("invalid use of %<this%> in non-member function");
2496 else
2497 error ("invalid use of %<this%> at top level");
2498 return error_mark_node;
2501 /* Finish a pseudo-destructor expression. If SCOPE is NULL, the
2502 expression was of the form `OBJECT.~DESTRUCTOR' where DESTRUCTOR is
2503 the TYPE for the type given. If SCOPE is non-NULL, the expression
2504 was of the form `OBJECT.SCOPE::~DESTRUCTOR'. */
2506 tree
2507 finish_pseudo_destructor_expr (tree object, tree scope, tree destructor,
2508 location_t loc)
2510 if (object == error_mark_node || destructor == error_mark_node)
2511 return error_mark_node;
2513 gcc_assert (TYPE_P (destructor));
2515 if (!processing_template_decl)
2517 if (scope == error_mark_node)
2519 error_at (loc, "invalid qualifying scope in pseudo-destructor name");
2520 return error_mark_node;
2522 if (is_auto (destructor))
2523 destructor = TREE_TYPE (object);
2524 if (scope && TYPE_P (scope) && !check_dtor_name (scope, destructor))
2526 error_at (loc,
2527 "qualified type %qT does not match destructor name ~%qT",
2528 scope, destructor);
2529 return error_mark_node;
2533 /* [expr.pseudo] says both:
2535 The type designated by the pseudo-destructor-name shall be
2536 the same as the object type.
2538 and:
2540 The cv-unqualified versions of the object type and of the
2541 type designated by the pseudo-destructor-name shall be the
2542 same type.
2544 We implement the more generous second sentence, since that is
2545 what most other compilers do. */
2546 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (object),
2547 destructor))
2549 error_at (loc, "%qE is not of type %qT", object, destructor);
2550 return error_mark_node;
2554 return build3_loc (loc, PSEUDO_DTOR_EXPR, void_type_node, object,
2555 scope, destructor);
2558 /* Finish an expression of the form CODE EXPR. */
2560 tree
2561 finish_unary_op_expr (location_t loc, enum tree_code code, tree expr,
2562 tsubst_flags_t complain)
2564 tree result = build_x_unary_op (loc, code, expr, complain);
2565 tree result_ovl, expr_ovl;
2567 if (!(complain & tf_warning))
2568 return result;
2570 result_ovl = result;
2571 expr_ovl = expr;
2573 if (!processing_template_decl)
2574 expr_ovl = cp_fully_fold (expr_ovl);
2576 if (!CONSTANT_CLASS_P (expr_ovl)
2577 || TREE_OVERFLOW_P (expr_ovl))
2578 return result;
2580 if (!processing_template_decl)
2581 result_ovl = cp_fully_fold (result_ovl);
2583 if (CONSTANT_CLASS_P (result_ovl) && TREE_OVERFLOW_P (result_ovl))
2584 overflow_warning (input_location, result_ovl);
2586 return result;
2589 /* Finish a compound-literal expression. TYPE is the type to which
2590 the CONSTRUCTOR in COMPOUND_LITERAL is being cast. */
2592 tree
2593 finish_compound_literal (tree type, tree compound_literal,
2594 tsubst_flags_t complain)
2596 if (type == error_mark_node)
2597 return error_mark_node;
2599 if (TREE_CODE (type) == REFERENCE_TYPE)
2601 compound_literal
2602 = finish_compound_literal (TREE_TYPE (type), compound_literal,
2603 complain);
2604 return cp_build_c_cast (type, compound_literal, complain);
2607 if (!TYPE_OBJ_P (type))
2609 if (complain & tf_error)
2610 error ("compound literal of non-object type %qT", type);
2611 return error_mark_node;
2614 if (processing_template_decl)
2616 TREE_TYPE (compound_literal) = type;
2617 /* Mark the expression as a compound literal. */
2618 TREE_HAS_CONSTRUCTOR (compound_literal) = 1;
2619 return compound_literal;
2622 type = complete_type (type);
2624 if (TYPE_NON_AGGREGATE_CLASS (type))
2626 /* Trying to deal with a CONSTRUCTOR instead of a TREE_LIST
2627 everywhere that deals with function arguments would be a pain, so
2628 just wrap it in a TREE_LIST. The parser set a flag so we know
2629 that it came from T{} rather than T({}). */
2630 CONSTRUCTOR_IS_DIRECT_INIT (compound_literal) = 1;
2631 compound_literal = build_tree_list (NULL_TREE, compound_literal);
2632 return build_functional_cast (type, compound_literal, complain);
2635 if (TREE_CODE (type) == ARRAY_TYPE
2636 && check_array_initializer (NULL_TREE, type, compound_literal))
2637 return error_mark_node;
2638 compound_literal = reshape_init (type, compound_literal, complain);
2639 if (SCALAR_TYPE_P (type)
2640 && !BRACE_ENCLOSED_INITIALIZER_P (compound_literal)
2641 && !check_narrowing (type, compound_literal, complain))
2642 return error_mark_node;
2643 if (TREE_CODE (type) == ARRAY_TYPE
2644 && TYPE_DOMAIN (type) == NULL_TREE)
2646 cp_complete_array_type_or_error (&type, compound_literal,
2647 false, complain);
2648 if (type == error_mark_node)
2649 return error_mark_node;
2651 compound_literal = digest_init (type, compound_literal, complain);
2652 if (TREE_CODE (compound_literal) == CONSTRUCTOR)
2653 TREE_HAS_CONSTRUCTOR (compound_literal) = true;
2654 /* Put static/constant array temporaries in static variables, but always
2655 represent class temporaries with TARGET_EXPR so we elide copies. */
2656 if ((!at_function_scope_p () || CP_TYPE_CONST_P (type))
2657 && TREE_CODE (type) == ARRAY_TYPE
2658 && !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
2659 && initializer_constant_valid_p (compound_literal, type))
2661 tree decl = create_temporary_var (type);
2662 DECL_INITIAL (decl) = compound_literal;
2663 TREE_STATIC (decl) = 1;
2664 if (literal_type_p (type) && CP_TYPE_CONST_NON_VOLATILE_P (type))
2666 /* 5.19 says that a constant expression can include an
2667 lvalue-rvalue conversion applied to "a glvalue of literal type
2668 that refers to a non-volatile temporary object initialized
2669 with a constant expression". Rather than try to communicate
2670 that this VAR_DECL is a temporary, just mark it constexpr. */
2671 DECL_DECLARED_CONSTEXPR_P (decl) = true;
2672 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
2673 TREE_CONSTANT (decl) = true;
2675 cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
2676 decl = pushdecl_top_level (decl);
2677 DECL_NAME (decl) = make_anon_name ();
2678 SET_DECL_ASSEMBLER_NAME (decl, DECL_NAME (decl));
2679 /* Make sure the destructor is callable. */
2680 tree clean = cxx_maybe_build_cleanup (decl, complain);
2681 if (clean == error_mark_node)
2682 return error_mark_node;
2683 return decl;
2685 else
2686 return get_target_expr_sfinae (compound_literal, complain);
2689 /* Return the declaration for the function-name variable indicated by
2690 ID. */
2692 tree
2693 finish_fname (tree id)
2695 tree decl;
2697 decl = fname_decl (input_location, C_RID_CODE (id), id);
2698 if (processing_template_decl && current_function_decl
2699 && decl != error_mark_node)
2700 decl = DECL_NAME (decl);
2701 return decl;
2704 /* Finish a translation unit. */
2706 void
2707 finish_translation_unit (void)
2709 /* In case there were missing closebraces,
2710 get us back to the global binding level. */
2711 pop_everything ();
2712 while (current_namespace != global_namespace)
2713 pop_namespace ();
2715 /* Do file scope __FUNCTION__ et al. */
2716 finish_fname_decls ();
2719 /* Finish a template type parameter, specified as AGGR IDENTIFIER.
2720 Returns the parameter. */
2722 tree
2723 finish_template_type_parm (tree aggr, tree identifier)
2725 if (aggr != class_type_node)
2727 permerror (input_location, "template type parameters must use the keyword %<class%> or %<typename%>");
2728 aggr = class_type_node;
2731 return build_tree_list (aggr, identifier);
2734 /* Finish a template template parameter, specified as AGGR IDENTIFIER.
2735 Returns the parameter. */
2737 tree
2738 finish_template_template_parm (tree aggr, tree identifier)
2740 tree decl = build_decl (input_location,
2741 TYPE_DECL, identifier, NULL_TREE);
2743 tree tmpl = build_lang_decl (TEMPLATE_DECL, identifier, NULL_TREE);
2744 DECL_TEMPLATE_PARMS (tmpl) = current_template_parms;
2745 DECL_TEMPLATE_RESULT (tmpl) = decl;
2746 DECL_ARTIFICIAL (decl) = 1;
2748 // Associate the constraints with the underlying declaration,
2749 // not the template.
2750 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
2751 tree constr = build_constraints (reqs, NULL_TREE);
2752 set_constraints (decl, constr);
2754 end_template_decl ();
2756 gcc_assert (DECL_TEMPLATE_PARMS (tmpl));
2758 check_default_tmpl_args (decl, DECL_TEMPLATE_PARMS (tmpl),
2759 /*is_primary=*/true, /*is_partial=*/false,
2760 /*is_friend=*/0);
2762 return finish_template_type_parm (aggr, tmpl);
2765 /* ARGUMENT is the default-argument value for a template template
2766 parameter. If ARGUMENT is invalid, issue error messages and return
2767 the ERROR_MARK_NODE. Otherwise, ARGUMENT itself is returned. */
2769 tree
2770 check_template_template_default_arg (tree argument)
2772 if (TREE_CODE (argument) != TEMPLATE_DECL
2773 && TREE_CODE (argument) != TEMPLATE_TEMPLATE_PARM
2774 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
2776 if (TREE_CODE (argument) == TYPE_DECL)
2777 error ("invalid use of type %qT as a default value for a template "
2778 "template-parameter", TREE_TYPE (argument));
2779 else
2780 error ("invalid default argument for a template template parameter");
2781 return error_mark_node;
2784 return argument;
2787 /* Begin a class definition, as indicated by T. */
2789 tree
2790 begin_class_definition (tree t)
2792 if (error_operand_p (t) || error_operand_p (TYPE_MAIN_DECL (t)))
2793 return error_mark_node;
2795 if (processing_template_parmlist)
2797 error ("definition of %q#T inside template parameter list", t);
2798 return error_mark_node;
2801 /* According to the C++ ABI, decimal classes defined in ISO/IEC TR 24733
2802 are passed the same as decimal scalar types. */
2803 if (TREE_CODE (t) == RECORD_TYPE
2804 && !processing_template_decl)
2806 tree ns = TYPE_CONTEXT (t);
2807 if (ns && TREE_CODE (ns) == NAMESPACE_DECL
2808 && DECL_CONTEXT (ns) == std_node
2809 && DECL_NAME (ns)
2810 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (ns)), "decimal"))
2812 const char *n = TYPE_NAME_STRING (t);
2813 if ((strcmp (n, "decimal32") == 0)
2814 || (strcmp (n, "decimal64") == 0)
2815 || (strcmp (n, "decimal128") == 0))
2816 TYPE_TRANSPARENT_AGGR (t) = 1;
2820 /* A non-implicit typename comes from code like:
2822 template <typename T> struct A {
2823 template <typename U> struct A<T>::B ...
2825 This is erroneous. */
2826 else if (TREE_CODE (t) == TYPENAME_TYPE)
2828 error ("invalid definition of qualified type %qT", t);
2829 t = error_mark_node;
2832 if (t == error_mark_node || ! MAYBE_CLASS_TYPE_P (t))
2834 t = make_class_type (RECORD_TYPE);
2835 pushtag (make_anon_name (), t, /*tag_scope=*/ts_current);
2838 if (TYPE_BEING_DEFINED (t))
2840 t = make_class_type (TREE_CODE (t));
2841 pushtag (TYPE_IDENTIFIER (t), t, /*tag_scope=*/ts_current);
2843 maybe_process_partial_specialization (t);
2844 pushclass (t);
2845 TYPE_BEING_DEFINED (t) = 1;
2846 class_binding_level->defining_class_p = 1;
2848 if (flag_pack_struct)
2850 tree v;
2851 TYPE_PACKED (t) = 1;
2852 /* Even though the type is being defined for the first time
2853 here, there might have been a forward declaration, so there
2854 might be cv-qualified variants of T. */
2855 for (v = TYPE_NEXT_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v))
2856 TYPE_PACKED (v) = 1;
2858 /* Reset the interface data, at the earliest possible
2859 moment, as it might have been set via a class foo;
2860 before. */
2861 if (! TYPE_ANONYMOUS_P (t))
2863 struct c_fileinfo *finfo = \
2864 get_fileinfo (LOCATION_FILE (input_location));
2865 CLASSTYPE_INTERFACE_ONLY (t) = finfo->interface_only;
2866 SET_CLASSTYPE_INTERFACE_UNKNOWN_X
2867 (t, finfo->interface_unknown);
2869 reset_specialization();
2871 /* Make a declaration for this class in its own scope. */
2872 build_self_reference ();
2874 return t;
2877 /* Finish the member declaration given by DECL. */
2879 void
2880 finish_member_declaration (tree decl)
2882 if (decl == error_mark_node || decl == NULL_TREE)
2883 return;
2885 if (decl == void_type_node)
2886 /* The COMPONENT was a friend, not a member, and so there's
2887 nothing for us to do. */
2888 return;
2890 /* We should see only one DECL at a time. */
2891 gcc_assert (DECL_CHAIN (decl) == NULL_TREE);
2893 /* Set up access control for DECL. */
2894 TREE_PRIVATE (decl)
2895 = (current_access_specifier == access_private_node);
2896 TREE_PROTECTED (decl)
2897 = (current_access_specifier == access_protected_node);
2898 if (TREE_CODE (decl) == TEMPLATE_DECL)
2900 TREE_PRIVATE (DECL_TEMPLATE_RESULT (decl)) = TREE_PRIVATE (decl);
2901 TREE_PROTECTED (DECL_TEMPLATE_RESULT (decl)) = TREE_PROTECTED (decl);
2904 /* Mark the DECL as a member of the current class, unless it's
2905 a member of an enumeration. */
2906 if (TREE_CODE (decl) != CONST_DECL)
2907 DECL_CONTEXT (decl) = current_class_type;
2909 /* Check for bare parameter packs in the member variable declaration. */
2910 if (TREE_CODE (decl) == FIELD_DECL)
2912 if (check_for_bare_parameter_packs (TREE_TYPE (decl)))
2913 TREE_TYPE (decl) = error_mark_node;
2914 if (check_for_bare_parameter_packs (DECL_ATTRIBUTES (decl)))
2915 DECL_ATTRIBUTES (decl) = NULL_TREE;
2918 /* [dcl.link]
2920 A C language linkage is ignored for the names of class members
2921 and the member function type of class member functions. */
2922 if (DECL_LANG_SPECIFIC (decl) && DECL_LANGUAGE (decl) == lang_c)
2923 SET_DECL_LANGUAGE (decl, lang_cplusplus);
2925 /* Put functions on the TYPE_METHODS list and everything else on the
2926 TYPE_FIELDS list. Note that these are built up in reverse order.
2927 We reverse them (to obtain declaration order) in finish_struct. */
2928 if (DECL_DECLARES_FUNCTION_P (decl))
2930 /* We also need to add this function to the
2931 CLASSTYPE_METHOD_VEC. */
2932 if (add_method (current_class_type, decl, NULL_TREE))
2934 gcc_assert (TYPE_MAIN_VARIANT (current_class_type) == current_class_type);
2935 DECL_CHAIN (decl) = TYPE_METHODS (current_class_type);
2936 TYPE_METHODS (current_class_type) = decl;
2938 maybe_add_class_template_decl_list (current_class_type, decl,
2939 /*friend_p=*/0);
2942 /* Enter the DECL into the scope of the class, if the class
2943 isn't a closure (whose fields are supposed to be unnamed). */
2944 else if (CLASSTYPE_LAMBDA_EXPR (current_class_type)
2945 || pushdecl_class_level (decl))
2947 if (TREE_CODE (decl) == USING_DECL)
2949 /* For now, ignore class-scope USING_DECLS, so that
2950 debugging backends do not see them. */
2951 DECL_IGNORED_P (decl) = 1;
2954 /* All TYPE_DECLs go at the end of TYPE_FIELDS. Ordinary fields
2955 go at the beginning. The reason is that lookup_field_1
2956 searches the list in order, and we want a field name to
2957 override a type name so that the "struct stat hack" will
2958 work. In particular:
2960 struct S { enum E { }; int E } s;
2961 s.E = 3;
2963 is valid. In addition, the FIELD_DECLs must be maintained in
2964 declaration order so that class layout works as expected.
2965 However, we don't need that order until class layout, so we
2966 save a little time by putting FIELD_DECLs on in reverse order
2967 here, and then reversing them in finish_struct_1. (We could
2968 also keep a pointer to the correct insertion points in the
2969 list.) */
2971 if (TREE_CODE (decl) == TYPE_DECL)
2972 TYPE_FIELDS (current_class_type)
2973 = chainon (TYPE_FIELDS (current_class_type), decl);
2974 else
2976 DECL_CHAIN (decl) = TYPE_FIELDS (current_class_type);
2977 TYPE_FIELDS (current_class_type) = decl;
2980 maybe_add_class_template_decl_list (current_class_type, decl,
2981 /*friend_p=*/0);
2985 /* Finish processing a complete template declaration. The PARMS are
2986 the template parameters. */
2988 void
2989 finish_template_decl (tree parms)
2991 if (parms)
2992 end_template_decl ();
2993 else
2994 end_specialization ();
2997 // Returns the template type of the class scope being entered. If we're
2998 // entering a constrained class scope. TYPE is the class template
2999 // scope being entered and we may need to match the intended type with
3000 // a constrained specialization. For example:
3002 // template<Object T>
3003 // struct S { void f(); }; #1
3005 // template<Object T>
3006 // void S<T>::f() { } #2
3008 // We check, in #2, that S<T> refers precisely to the type declared by
3009 // #1 (i.e., that the constraints match). Note that the following should
3010 // be an error since there is no specialization of S<T> that is
3011 // unconstrained, but this is not diagnosed here.
3013 // template<typename T>
3014 // void S<T>::f() { }
3016 // We cannot diagnose this problem here since this function also matches
3017 // qualified template names that are not part of a definition. For example:
3019 // template<Integral T, Floating_point U>
3020 // typename pair<T, U>::first_type void f(T, U);
3022 // Here, it is unlikely that there is a partial specialization of
3023 // pair constrained for for Integral and Floating_point arguments.
3025 // The general rule is: if a constrained specialization with matching
3026 // constraints is found return that type. Also note that if TYPE is not a
3027 // class-type (e.g. a typename type), then no fixup is needed.
3029 static tree
3030 fixup_template_type (tree type)
3032 // Find the template parameter list at the a depth appropriate to
3033 // the scope we're trying to enter.
3034 tree parms = current_template_parms;
3035 int depth = template_class_depth (type);
3036 for (int n = processing_template_decl; n > depth && parms; --n)
3037 parms = TREE_CHAIN (parms);
3038 if (!parms)
3039 return type;
3040 tree cur_reqs = TEMPLATE_PARMS_CONSTRAINTS (parms);
3041 tree cur_constr = build_constraints (cur_reqs, NULL_TREE);
3043 // Search for a specialization whose type and constraints match.
3044 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
3045 tree specs = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
3046 while (specs)
3048 tree spec_constr = get_constraints (TREE_VALUE (specs));
3050 // If the type and constraints match a specialization, then we
3051 // are entering that type.
3052 if (same_type_p (type, TREE_TYPE (specs))
3053 && equivalent_constraints (cur_constr, spec_constr))
3054 return TREE_TYPE (specs);
3055 specs = TREE_CHAIN (specs);
3058 // If no specialization matches, then must return the type
3059 // previously found.
3060 return type;
3063 /* Finish processing a template-id (which names a type) of the form
3064 NAME < ARGS >. Return the TYPE_DECL for the type named by the
3065 template-id. If ENTERING_SCOPE is nonzero we are about to enter
3066 the scope of template-id indicated. */
3068 tree
3069 finish_template_type (tree name, tree args, int entering_scope)
3071 tree type;
3073 type = lookup_template_class (name, args,
3074 NULL_TREE, NULL_TREE, entering_scope,
3075 tf_warning_or_error | tf_user);
3077 /* If we might be entering the scope of a partial specialization,
3078 find the one with the right constraints. */
3079 if (flag_concepts
3080 && entering_scope
3081 && CLASS_TYPE_P (type)
3082 && dependent_type_p (type)
3083 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
3084 type = fixup_template_type (type);
3086 if (type == error_mark_node)
3087 return type;
3088 else if (CLASS_TYPE_P (type) && !alias_type_or_template_p (type))
3089 return TYPE_STUB_DECL (type);
3090 else
3091 return TYPE_NAME (type);
3094 /* Finish processing a BASE_CLASS with the indicated ACCESS_SPECIFIER.
3095 Return a TREE_LIST containing the ACCESS_SPECIFIER and the
3096 BASE_CLASS, or NULL_TREE if an error occurred. The
3097 ACCESS_SPECIFIER is one of
3098 access_{default,public,protected_private}_node. For a virtual base
3099 we set TREE_TYPE. */
3101 tree
3102 finish_base_specifier (tree base, tree access, bool virtual_p)
3104 tree result;
3106 if (base == error_mark_node)
3108 error ("invalid base-class specification");
3109 result = NULL_TREE;
3111 else if (! MAYBE_CLASS_TYPE_P (base))
3113 error ("%qT is not a class type", base);
3114 result = NULL_TREE;
3116 else
3118 if (cp_type_quals (base) != 0)
3120 /* DR 484: Can a base-specifier name a cv-qualified
3121 class type? */
3122 base = TYPE_MAIN_VARIANT (base);
3124 result = build_tree_list (access, base);
3125 if (virtual_p)
3126 TREE_TYPE (result) = integer_type_node;
3129 return result;
3132 /* If FNS is a member function, a set of member functions, or a
3133 template-id referring to one or more member functions, return a
3134 BASELINK for FNS, incorporating the current access context.
3135 Otherwise, return FNS unchanged. */
3137 tree
3138 baselink_for_fns (tree fns)
3140 tree scope;
3141 tree cl;
3143 if (BASELINK_P (fns)
3144 || error_operand_p (fns))
3145 return fns;
3147 scope = ovl_scope (fns);
3148 if (!CLASS_TYPE_P (scope))
3149 return fns;
3151 cl = currently_open_derived_class (scope);
3152 if (!cl)
3153 cl = scope;
3154 cl = TYPE_BINFO (cl);
3155 return build_baselink (cl, cl, fns, /*optype=*/NULL_TREE);
3158 /* Returns true iff DECL is a variable from a function outside
3159 the current one. */
3161 static bool
3162 outer_var_p (tree decl)
3164 return ((VAR_P (decl) || TREE_CODE (decl) == PARM_DECL)
3165 && DECL_FUNCTION_SCOPE_P (decl)
3166 && (DECL_CONTEXT (decl) != current_function_decl
3167 || parsing_nsdmi ()));
3170 /* As above, but also checks that DECL is automatic. */
3172 bool
3173 outer_automatic_var_p (tree decl)
3175 return (outer_var_p (decl)
3176 && !TREE_STATIC (decl));
3179 /* DECL satisfies outer_automatic_var_p. Possibly complain about it or
3180 rewrite it for lambda capture. */
3182 tree
3183 process_outer_var_ref (tree decl, tsubst_flags_t complain)
3185 if (cp_unevaluated_operand)
3186 /* It's not a use (3.2) if we're in an unevaluated context. */
3187 return decl;
3188 if (decl == error_mark_node)
3189 return decl;
3191 tree context = DECL_CONTEXT (decl);
3192 tree containing_function = current_function_decl;
3193 tree lambda_stack = NULL_TREE;
3194 tree lambda_expr = NULL_TREE;
3195 tree initializer = convert_from_reference (decl);
3197 /* Mark it as used now even if the use is ill-formed. */
3198 if (!mark_used (decl, complain) && !(complain & tf_error))
3199 return error_mark_node;
3201 /* Core issue 696: "[At the July 2009 meeting] the CWG expressed
3202 support for an approach in which a reference to a local
3203 [constant] automatic variable in a nested class or lambda body
3204 would enter the expression as an rvalue, which would reduce
3205 the complexity of the problem"
3207 FIXME update for final resolution of core issue 696. */
3208 if (decl_maybe_constant_var_p (decl))
3210 if (processing_template_decl)
3211 /* In a template, the constant value may not be in a usable
3212 form, so wait until instantiation time. */
3213 return decl;
3214 else if (decl_constant_var_p (decl))
3216 tree t = maybe_constant_value (convert_from_reference (decl));
3217 if (TREE_CONSTANT (t))
3218 return t;
3222 if (parsing_nsdmi ())
3223 containing_function = NULL_TREE;
3224 else
3225 /* If we are in a lambda function, we can move out until we hit
3226 1. the context,
3227 2. a non-lambda function, or
3228 3. a non-default capturing lambda function. */
3229 while (context != containing_function
3230 && LAMBDA_FUNCTION_P (containing_function))
3232 tree closure = DECL_CONTEXT (containing_function);
3233 lambda_expr = CLASSTYPE_LAMBDA_EXPR (closure);
3235 if (TYPE_CLASS_SCOPE_P (closure))
3236 /* A lambda in an NSDMI (c++/64496). */
3237 break;
3239 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr)
3240 == CPLD_NONE)
3241 break;
3243 lambda_stack = tree_cons (NULL_TREE,
3244 lambda_expr,
3245 lambda_stack);
3247 containing_function
3248 = decl_function_context (containing_function);
3251 if (lambda_expr && VAR_P (decl)
3252 && DECL_ANON_UNION_VAR_P (decl))
3254 if (complain & tf_error)
3255 error ("cannot capture member %qD of anonymous union", decl);
3256 return error_mark_node;
3258 if (context == containing_function)
3260 decl = add_default_capture (lambda_stack,
3261 /*id=*/DECL_NAME (decl),
3262 initializer);
3264 else if (lambda_expr)
3266 if (complain & tf_error)
3268 error ("%qD is not captured", decl);
3269 tree closure = LAMBDA_EXPR_CLOSURE (lambda_expr);
3270 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr)
3271 == CPLD_NONE)
3272 inform (location_of (closure),
3273 "the lambda has no capture-default");
3274 else if (TYPE_CLASS_SCOPE_P (closure))
3275 inform (0, "lambda in local class %q+T cannot "
3276 "capture variables from the enclosing context",
3277 TYPE_CONTEXT (closure));
3278 inform (DECL_SOURCE_LOCATION (decl), "%q#D declared here", decl);
3280 return error_mark_node;
3282 else
3284 if (complain & tf_error)
3285 error (VAR_P (decl)
3286 ? G_("use of local variable with automatic storage from containing function")
3287 : G_("use of parameter from containing function"));
3288 inform (DECL_SOURCE_LOCATION (decl), "%q#D declared here", decl);
3289 return error_mark_node;
3291 return decl;
3294 /* ID_EXPRESSION is a representation of parsed, but unprocessed,
3295 id-expression. (See cp_parser_id_expression for details.) SCOPE,
3296 if non-NULL, is the type or namespace used to explicitly qualify
3297 ID_EXPRESSION. DECL is the entity to which that name has been
3298 resolved.
3300 *CONSTANT_EXPRESSION_P is true if we are presently parsing a
3301 constant-expression. In that case, *NON_CONSTANT_EXPRESSION_P will
3302 be set to true if this expression isn't permitted in a
3303 constant-expression, but it is otherwise not set by this function.
3304 *ALLOW_NON_CONSTANT_EXPRESSION_P is true if we are parsing a
3305 constant-expression, but a non-constant expression is also
3306 permissible.
3308 DONE is true if this expression is a complete postfix-expression;
3309 it is false if this expression is followed by '->', '[', '(', etc.
3310 ADDRESS_P is true iff this expression is the operand of '&'.
3311 TEMPLATE_P is true iff the qualified-id was of the form
3312 "A::template B". TEMPLATE_ARG_P is true iff this qualified name
3313 appears as a template argument.
3315 If an error occurs, and it is the kind of error that might cause
3316 the parser to abort a tentative parse, *ERROR_MSG is filled in. It
3317 is the caller's responsibility to issue the message. *ERROR_MSG
3318 will be a string with static storage duration, so the caller need
3319 not "free" it.
3321 Return an expression for the entity, after issuing appropriate
3322 diagnostics. This function is also responsible for transforming a
3323 reference to a non-static member into a COMPONENT_REF that makes
3324 the use of "this" explicit.
3326 Upon return, *IDK will be filled in appropriately. */
3327 tree
3328 finish_id_expression (tree id_expression,
3329 tree decl,
3330 tree scope,
3331 cp_id_kind *idk,
3332 bool integral_constant_expression_p,
3333 bool allow_non_integral_constant_expression_p,
3334 bool *non_integral_constant_expression_p,
3335 bool template_p,
3336 bool done,
3337 bool address_p,
3338 bool template_arg_p,
3339 const char **error_msg,
3340 location_t location)
3342 decl = strip_using_decl (decl);
3344 /* Initialize the output parameters. */
3345 *idk = CP_ID_KIND_NONE;
3346 *error_msg = NULL;
3348 if (id_expression == error_mark_node)
3349 return error_mark_node;
3350 /* If we have a template-id, then no further lookup is
3351 required. If the template-id was for a template-class, we
3352 will sometimes have a TYPE_DECL at this point. */
3353 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3354 || TREE_CODE (decl) == TYPE_DECL)
3356 /* Look up the name. */
3357 else
3359 if (decl == error_mark_node)
3361 /* Name lookup failed. */
3362 if (scope
3363 && (!TYPE_P (scope)
3364 || (!dependent_type_p (scope)
3365 && !(identifier_p (id_expression)
3366 && IDENTIFIER_TYPENAME_P (id_expression)
3367 && dependent_type_p (TREE_TYPE (id_expression))))))
3369 /* If the qualifying type is non-dependent (and the name
3370 does not name a conversion operator to a dependent
3371 type), issue an error. */
3372 qualified_name_lookup_error (scope, id_expression, decl, location);
3373 return error_mark_node;
3375 else if (!scope)
3377 /* It may be resolved via Koenig lookup. */
3378 *idk = CP_ID_KIND_UNQUALIFIED;
3379 return id_expression;
3381 else
3382 decl = id_expression;
3384 /* If DECL is a variable that would be out of scope under
3385 ANSI/ISO rules, but in scope in the ARM, name lookup
3386 will succeed. Issue a diagnostic here. */
3387 else
3388 decl = check_for_out_of_scope_variable (decl);
3390 /* Remember that the name was used in the definition of
3391 the current class so that we can check later to see if
3392 the meaning would have been different after the class
3393 was entirely defined. */
3394 if (!scope && decl != error_mark_node && identifier_p (id_expression))
3395 maybe_note_name_used_in_class (id_expression, decl);
3397 /* Disallow uses of local variables from containing functions, except
3398 within lambda-expressions. */
3399 if (outer_automatic_var_p (decl))
3401 decl = process_outer_var_ref (decl, tf_warning_or_error);
3402 if (decl == error_mark_node)
3403 return error_mark_node;
3406 /* Also disallow uses of function parameters outside the function
3407 body, except inside an unevaluated context (i.e. decltype). */
3408 if (TREE_CODE (decl) == PARM_DECL
3409 && DECL_CONTEXT (decl) == NULL_TREE
3410 && !cp_unevaluated_operand)
3412 *error_msg = "use of parameter outside function body";
3413 return error_mark_node;
3417 /* If we didn't find anything, or what we found was a type,
3418 then this wasn't really an id-expression. */
3419 if (TREE_CODE (decl) == TEMPLATE_DECL
3420 && !DECL_FUNCTION_TEMPLATE_P (decl))
3422 *error_msg = "missing template arguments";
3423 return error_mark_node;
3425 else if (TREE_CODE (decl) == TYPE_DECL
3426 || TREE_CODE (decl) == NAMESPACE_DECL)
3428 *error_msg = "expected primary-expression";
3429 return error_mark_node;
3432 /* If the name resolved to a template parameter, there is no
3433 need to look it up again later. */
3434 if ((TREE_CODE (decl) == CONST_DECL && DECL_TEMPLATE_PARM_P (decl))
3435 || TREE_CODE (decl) == TEMPLATE_PARM_INDEX)
3437 tree r;
3439 *idk = CP_ID_KIND_NONE;
3440 if (TREE_CODE (decl) == TEMPLATE_PARM_INDEX)
3441 decl = TEMPLATE_PARM_DECL (decl);
3442 r = convert_from_reference (DECL_INITIAL (decl));
3444 if (integral_constant_expression_p
3445 && !dependent_type_p (TREE_TYPE (decl))
3446 && !(INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (r))))
3448 if (!allow_non_integral_constant_expression_p)
3449 error ("template parameter %qD of type %qT is not allowed in "
3450 "an integral constant expression because it is not of "
3451 "integral or enumeration type", decl, TREE_TYPE (decl));
3452 *non_integral_constant_expression_p = true;
3454 return r;
3456 else
3458 bool dependent_p = type_dependent_expression_p (decl);
3460 /* If the declaration was explicitly qualified indicate
3461 that. The semantics of `A::f(3)' are different than
3462 `f(3)' if `f' is virtual. */
3463 *idk = (scope
3464 ? CP_ID_KIND_QUALIFIED
3465 : (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3466 ? CP_ID_KIND_TEMPLATE_ID
3467 : (dependent_p
3468 ? CP_ID_KIND_UNQUALIFIED_DEPENDENT
3469 : CP_ID_KIND_UNQUALIFIED)));
3471 /* If the name was dependent on a template parameter, we will
3472 resolve the name at instantiation time. */
3473 if (dependent_p)
3475 /* If we found a variable, then name lookup during the
3476 instantiation will always resolve to the same VAR_DECL
3477 (or an instantiation thereof). */
3478 if (VAR_P (decl)
3479 || TREE_CODE (decl) == CONST_DECL
3480 || TREE_CODE (decl) == PARM_DECL)
3482 mark_used (decl);
3483 return convert_from_reference (decl);
3486 /* Create a SCOPE_REF for qualified names, if the scope is
3487 dependent. */
3488 if (scope)
3490 if (TYPE_P (scope))
3492 if (address_p && done)
3493 decl = finish_qualified_id_expr (scope, decl,
3494 done, address_p,
3495 template_p,
3496 template_arg_p,
3497 tf_warning_or_error);
3498 else
3500 tree type = NULL_TREE;
3501 if (DECL_P (decl) && !dependent_scope_p (scope))
3502 type = TREE_TYPE (decl);
3503 decl = build_qualified_name (type,
3504 scope,
3505 id_expression,
3506 template_p);
3509 if (TREE_TYPE (decl))
3510 decl = convert_from_reference (decl);
3511 return decl;
3513 /* A TEMPLATE_ID already contains all the information we
3514 need. */
3515 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR)
3516 return id_expression;
3517 /* The same is true for FIELD_DECL, but we also need to
3518 make sure that the syntax is correct. */
3519 else if (TREE_CODE (decl) == FIELD_DECL)
3521 /* Since SCOPE is NULL here, this is an unqualified name.
3522 Access checking has been performed during name lookup
3523 already. Turn off checking to avoid duplicate errors. */
3524 push_deferring_access_checks (dk_no_check);
3525 decl = finish_non_static_data_member
3526 (decl, NULL_TREE,
3527 /*qualifying_scope=*/NULL_TREE);
3528 pop_deferring_access_checks ();
3529 return decl;
3531 return id_expression;
3534 if (TREE_CODE (decl) == NAMESPACE_DECL)
3536 error ("use of namespace %qD as expression", decl);
3537 return error_mark_node;
3539 else if (DECL_CLASS_TEMPLATE_P (decl))
3541 error ("use of class template %qT as expression", decl);
3542 return error_mark_node;
3544 else if (TREE_CODE (decl) == TREE_LIST)
3546 /* Ambiguous reference to base members. */
3547 error ("request for member %qD is ambiguous in "
3548 "multiple inheritance lattice", id_expression);
3549 print_candidates (decl);
3550 return error_mark_node;
3553 /* Mark variable-like entities as used. Functions are similarly
3554 marked either below or after overload resolution. */
3555 if ((VAR_P (decl)
3556 || TREE_CODE (decl) == PARM_DECL
3557 || TREE_CODE (decl) == CONST_DECL
3558 || TREE_CODE (decl) == RESULT_DECL)
3559 && !mark_used (decl))
3560 return error_mark_node;
3562 /* Only certain kinds of names are allowed in constant
3563 expression. Template parameters have already
3564 been handled above. */
3565 if (! error_operand_p (decl)
3566 && integral_constant_expression_p
3567 && ! decl_constant_var_p (decl)
3568 && TREE_CODE (decl) != CONST_DECL
3569 && ! builtin_valid_in_constant_expr_p (decl))
3571 if (!allow_non_integral_constant_expression_p)
3573 error ("%qD cannot appear in a constant-expression", decl);
3574 return error_mark_node;
3576 *non_integral_constant_expression_p = true;
3579 tree wrap;
3580 if (VAR_P (decl)
3581 && !cp_unevaluated_operand
3582 && !processing_template_decl
3583 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl))
3584 && CP_DECL_THREAD_LOCAL_P (decl)
3585 && (wrap = get_tls_wrapper_fn (decl)))
3587 /* Replace an evaluated use of the thread_local variable with
3588 a call to its wrapper. */
3589 decl = build_cxx_call (wrap, 0, NULL, tf_warning_or_error);
3591 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3592 && variable_template_p (TREE_OPERAND (decl, 0)))
3594 decl = finish_template_variable (decl);
3595 mark_used (decl);
3596 decl = convert_from_reference (decl);
3598 else if (scope)
3600 decl = (adjust_result_of_qualified_name_lookup
3601 (decl, scope, current_nonlambda_class_type()));
3603 if (TREE_CODE (decl) == FUNCTION_DECL)
3604 mark_used (decl);
3606 if (TYPE_P (scope))
3607 decl = finish_qualified_id_expr (scope,
3608 decl,
3609 done,
3610 address_p,
3611 template_p,
3612 template_arg_p,
3613 tf_warning_or_error);
3614 else
3615 decl = convert_from_reference (decl);
3617 else if (TREE_CODE (decl) == FIELD_DECL)
3619 /* Since SCOPE is NULL here, this is an unqualified name.
3620 Access checking has been performed during name lookup
3621 already. Turn off checking to avoid duplicate errors. */
3622 push_deferring_access_checks (dk_no_check);
3623 decl = finish_non_static_data_member (decl, NULL_TREE,
3624 /*qualifying_scope=*/NULL_TREE);
3625 pop_deferring_access_checks ();
3627 else if (is_overloaded_fn (decl))
3629 tree first_fn;
3631 first_fn = get_first_fn (decl);
3632 if (TREE_CODE (first_fn) == TEMPLATE_DECL)
3633 first_fn = DECL_TEMPLATE_RESULT (first_fn);
3635 if (!really_overloaded_fn (decl)
3636 && !mark_used (first_fn))
3637 return error_mark_node;
3639 if (!template_arg_p
3640 && TREE_CODE (first_fn) == FUNCTION_DECL
3641 && DECL_FUNCTION_MEMBER_P (first_fn)
3642 && !shared_member_p (decl))
3644 /* A set of member functions. */
3645 decl = maybe_dummy_object (DECL_CONTEXT (first_fn), 0);
3646 return finish_class_member_access_expr (decl, id_expression,
3647 /*template_p=*/false,
3648 tf_warning_or_error);
3651 decl = baselink_for_fns (decl);
3653 else
3655 if (DECL_P (decl) && DECL_NONLOCAL (decl)
3656 && DECL_CLASS_SCOPE_P (decl))
3658 tree context = context_for_name_lookup (decl);
3659 if (context != current_class_type)
3661 tree path = currently_open_derived_class (context);
3662 perform_or_defer_access_check (TYPE_BINFO (path),
3663 decl, decl,
3664 tf_warning_or_error);
3668 decl = convert_from_reference (decl);
3672 return decl;
3675 /* Implement the __typeof keyword: Return the type of EXPR, suitable for
3676 use as a type-specifier. */
3678 tree
3679 finish_typeof (tree expr)
3681 tree type;
3683 if (type_dependent_expression_p (expr))
3685 type = cxx_make_type (TYPEOF_TYPE);
3686 TYPEOF_TYPE_EXPR (type) = expr;
3687 SET_TYPE_STRUCTURAL_EQUALITY (type);
3689 return type;
3692 expr = mark_type_use (expr);
3694 type = unlowered_expr_type (expr);
3696 if (!type || type == unknown_type_node)
3698 error ("type of %qE is unknown", expr);
3699 return error_mark_node;
3702 return type;
3705 /* Implement the __underlying_type keyword: Return the underlying
3706 type of TYPE, suitable for use as a type-specifier. */
3708 tree
3709 finish_underlying_type (tree type)
3711 tree underlying_type;
3713 if (processing_template_decl)
3715 underlying_type = cxx_make_type (UNDERLYING_TYPE);
3716 UNDERLYING_TYPE_TYPE (underlying_type) = type;
3717 SET_TYPE_STRUCTURAL_EQUALITY (underlying_type);
3719 return underlying_type;
3722 complete_type (type);
3724 if (TREE_CODE (type) != ENUMERAL_TYPE)
3726 error ("%qT is not an enumeration type", type);
3727 return error_mark_node;
3730 underlying_type = ENUM_UNDERLYING_TYPE (type);
3732 /* Fixup necessary in this case because ENUM_UNDERLYING_TYPE
3733 includes TYPE_MIN_VALUE and TYPE_MAX_VALUE information.
3734 See finish_enum_value_list for details. */
3735 if (!ENUM_FIXED_UNDERLYING_TYPE_P (type))
3736 underlying_type
3737 = c_common_type_for_mode (TYPE_MODE (underlying_type),
3738 TYPE_UNSIGNED (underlying_type));
3740 return underlying_type;
3743 /* Implement the __direct_bases keyword: Return the direct base classes
3744 of type */
3746 tree
3747 calculate_direct_bases (tree type)
3749 vec<tree, va_gc> *vector = make_tree_vector();
3750 tree bases_vec = NULL_TREE;
3751 vec<tree, va_gc> *base_binfos;
3752 tree binfo;
3753 unsigned i;
3755 complete_type (type);
3757 if (!NON_UNION_CLASS_TYPE_P (type))
3758 return make_tree_vec (0);
3760 base_binfos = BINFO_BASE_BINFOS (TYPE_BINFO (type));
3762 /* Virtual bases are initialized first */
3763 for (i = 0; base_binfos->iterate (i, &binfo); i++)
3765 if (BINFO_VIRTUAL_P (binfo))
3767 vec_safe_push (vector, binfo);
3771 /* Now non-virtuals */
3772 for (i = 0; base_binfos->iterate (i, &binfo); i++)
3774 if (!BINFO_VIRTUAL_P (binfo))
3776 vec_safe_push (vector, binfo);
3781 bases_vec = make_tree_vec (vector->length ());
3783 for (i = 0; i < vector->length (); ++i)
3785 TREE_VEC_ELT (bases_vec, i) = BINFO_TYPE ((*vector)[i]);
3787 return bases_vec;
3790 /* Implement the __bases keyword: Return the base classes
3791 of type */
3793 /* Find morally non-virtual base classes by walking binfo hierarchy */
3794 /* Virtual base classes are handled separately in finish_bases */
3796 static tree
3797 dfs_calculate_bases_pre (tree binfo, void * /*data_*/)
3799 /* Don't walk bases of virtual bases */
3800 return BINFO_VIRTUAL_P (binfo) ? dfs_skip_bases : NULL_TREE;
3803 static tree
3804 dfs_calculate_bases_post (tree binfo, void *data_)
3806 vec<tree, va_gc> **data = ((vec<tree, va_gc> **) data_);
3807 if (!BINFO_VIRTUAL_P (binfo))
3809 vec_safe_push (*data, BINFO_TYPE (binfo));
3811 return NULL_TREE;
3814 /* Calculates the morally non-virtual base classes of a class */
3815 static vec<tree, va_gc> *
3816 calculate_bases_helper (tree type)
3818 vec<tree, va_gc> *vector = make_tree_vector();
3820 /* Now add non-virtual base classes in order of construction */
3821 dfs_walk_all (TYPE_BINFO (type),
3822 dfs_calculate_bases_pre, dfs_calculate_bases_post, &vector);
3823 return vector;
3826 tree
3827 calculate_bases (tree type)
3829 vec<tree, va_gc> *vector = make_tree_vector();
3830 tree bases_vec = NULL_TREE;
3831 unsigned i;
3832 vec<tree, va_gc> *vbases;
3833 vec<tree, va_gc> *nonvbases;
3834 tree binfo;
3836 complete_type (type);
3838 if (!NON_UNION_CLASS_TYPE_P (type))
3839 return make_tree_vec (0);
3841 /* First go through virtual base classes */
3842 for (vbases = CLASSTYPE_VBASECLASSES (type), i = 0;
3843 vec_safe_iterate (vbases, i, &binfo); i++)
3845 vec<tree, va_gc> *vbase_bases;
3846 vbase_bases = calculate_bases_helper (BINFO_TYPE (binfo));
3847 vec_safe_splice (vector, vbase_bases);
3848 release_tree_vector (vbase_bases);
3851 /* Now for the non-virtual bases */
3852 nonvbases = calculate_bases_helper (type);
3853 vec_safe_splice (vector, nonvbases);
3854 release_tree_vector (nonvbases);
3856 /* Last element is entire class, so don't copy */
3857 bases_vec = make_tree_vec (vector->length () - 1);
3859 for (i = 0; i < vector->length () - 1; ++i)
3861 TREE_VEC_ELT (bases_vec, i) = (*vector)[i];
3863 release_tree_vector (vector);
3864 return bases_vec;
3867 tree
3868 finish_bases (tree type, bool direct)
3870 tree bases = NULL_TREE;
3872 if (!processing_template_decl)
3874 /* Parameter packs can only be used in templates */
3875 error ("Parameter pack __bases only valid in template declaration");
3876 return error_mark_node;
3879 bases = cxx_make_type (BASES);
3880 BASES_TYPE (bases) = type;
3881 BASES_DIRECT (bases) = direct;
3882 SET_TYPE_STRUCTURAL_EQUALITY (bases);
3884 return bases;
3887 /* Perform C++-specific checks for __builtin_offsetof before calling
3888 fold_offsetof. */
3890 tree
3891 finish_offsetof (tree expr, location_t loc)
3893 /* If we're processing a template, we can't finish the semantics yet.
3894 Otherwise we can fold the entire expression now. */
3895 if (processing_template_decl)
3897 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
3898 SET_EXPR_LOCATION (expr, loc);
3899 return expr;
3902 if (TREE_CODE (expr) == PSEUDO_DTOR_EXPR)
3904 error ("cannot apply %<offsetof%> to destructor %<~%T%>",
3905 TREE_OPERAND (expr, 2));
3906 return error_mark_node;
3908 if (TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE
3909 || TREE_CODE (TREE_TYPE (expr)) == METHOD_TYPE
3910 || TREE_TYPE (expr) == unknown_type_node)
3912 if (INDIRECT_REF_P (expr))
3913 error ("second operand of %<offsetof%> is neither a single "
3914 "identifier nor a sequence of member accesses and "
3915 "array references");
3916 else
3918 if (TREE_CODE (expr) == COMPONENT_REF
3919 || TREE_CODE (expr) == COMPOUND_EXPR)
3920 expr = TREE_OPERAND (expr, 1);
3921 error ("cannot apply %<offsetof%> to member function %qD", expr);
3923 return error_mark_node;
3925 if (REFERENCE_REF_P (expr))
3926 expr = TREE_OPERAND (expr, 0);
3927 if (TREE_CODE (expr) == COMPONENT_REF)
3929 tree object = TREE_OPERAND (expr, 0);
3930 if (!complete_type_or_else (TREE_TYPE (object), object))
3931 return error_mark_node;
3932 if (warn_invalid_offsetof
3933 && CLASS_TYPE_P (TREE_TYPE (object))
3934 && CLASSTYPE_NON_STD_LAYOUT (TREE_TYPE (object))
3935 && cp_unevaluated_operand == 0)
3936 pedwarn (loc, OPT_Winvalid_offsetof,
3937 "offsetof within non-standard-layout type %qT is undefined",
3938 TREE_TYPE (object));
3940 return fold_offsetof (expr);
3943 /* Replace the AGGR_INIT_EXPR at *TP with an equivalent CALL_EXPR. This
3944 function is broken out from the above for the benefit of the tree-ssa
3945 project. */
3947 void
3948 simplify_aggr_init_expr (tree *tp)
3950 tree aggr_init_expr = *tp;
3952 /* Form an appropriate CALL_EXPR. */
3953 tree fn = AGGR_INIT_EXPR_FN (aggr_init_expr);
3954 tree slot = AGGR_INIT_EXPR_SLOT (aggr_init_expr);
3955 tree type = TREE_TYPE (slot);
3957 tree call_expr;
3958 enum style_t { ctor, arg, pcc } style;
3960 if (AGGR_INIT_VIA_CTOR_P (aggr_init_expr))
3961 style = ctor;
3962 #ifdef PCC_STATIC_STRUCT_RETURN
3963 else if (1)
3964 style = pcc;
3965 #endif
3966 else
3968 gcc_assert (TREE_ADDRESSABLE (type));
3969 style = arg;
3972 call_expr = build_call_array_loc (input_location,
3973 TREE_TYPE (TREE_TYPE (TREE_TYPE (fn))),
3975 aggr_init_expr_nargs (aggr_init_expr),
3976 AGGR_INIT_EXPR_ARGP (aggr_init_expr));
3977 TREE_NOTHROW (call_expr) = TREE_NOTHROW (aggr_init_expr);
3978 CALL_EXPR_LIST_INIT_P (call_expr) = CALL_EXPR_LIST_INIT_P (aggr_init_expr);
3980 if (style == ctor)
3982 /* Replace the first argument to the ctor with the address of the
3983 slot. */
3984 cxx_mark_addressable (slot);
3985 CALL_EXPR_ARG (call_expr, 0) =
3986 build1 (ADDR_EXPR, build_pointer_type (type), slot);
3988 else if (style == arg)
3990 /* Just mark it addressable here, and leave the rest to
3991 expand_call{,_inline}. */
3992 cxx_mark_addressable (slot);
3993 CALL_EXPR_RETURN_SLOT_OPT (call_expr) = true;
3994 call_expr = build2 (INIT_EXPR, TREE_TYPE (call_expr), slot, call_expr);
3996 else if (style == pcc)
3998 /* If we're using the non-reentrant PCC calling convention, then we
3999 need to copy the returned value out of the static buffer into the
4000 SLOT. */
4001 push_deferring_access_checks (dk_no_check);
4002 call_expr = build_aggr_init (slot, call_expr,
4003 DIRECT_BIND | LOOKUP_ONLYCONVERTING,
4004 tf_warning_or_error);
4005 pop_deferring_access_checks ();
4006 call_expr = build2 (COMPOUND_EXPR, TREE_TYPE (slot), call_expr, slot);
4009 if (AGGR_INIT_ZERO_FIRST (aggr_init_expr))
4011 tree init = build_zero_init (type, NULL_TREE,
4012 /*static_storage_p=*/false);
4013 init = build2 (INIT_EXPR, void_type_node, slot, init);
4014 call_expr = build2 (COMPOUND_EXPR, TREE_TYPE (call_expr),
4015 init, call_expr);
4018 *tp = call_expr;
4021 /* Emit all thunks to FN that should be emitted when FN is emitted. */
4023 void
4024 emit_associated_thunks (tree fn)
4026 /* When we use vcall offsets, we emit thunks with the virtual
4027 functions to which they thunk. The whole point of vcall offsets
4028 is so that you can know statically the entire set of thunks that
4029 will ever be needed for a given virtual function, thereby
4030 enabling you to output all the thunks with the function itself. */
4031 if (DECL_VIRTUAL_P (fn)
4032 /* Do not emit thunks for extern template instantiations. */
4033 && ! DECL_REALLY_EXTERN (fn))
4035 tree thunk;
4037 for (thunk = DECL_THUNKS (fn); thunk; thunk = DECL_CHAIN (thunk))
4039 if (!THUNK_ALIAS (thunk))
4041 use_thunk (thunk, /*emit_p=*/1);
4042 if (DECL_RESULT_THUNK_P (thunk))
4044 tree probe;
4046 for (probe = DECL_THUNKS (thunk);
4047 probe; probe = DECL_CHAIN (probe))
4048 use_thunk (probe, /*emit_p=*/1);
4051 else
4052 gcc_assert (!DECL_THUNKS (thunk));
4057 /* Generate RTL for FN. */
4059 bool
4060 expand_or_defer_fn_1 (tree fn)
4062 /* When the parser calls us after finishing the body of a template
4063 function, we don't really want to expand the body. */
4064 if (processing_template_decl)
4066 /* Normally, collection only occurs in rest_of_compilation. So,
4067 if we don't collect here, we never collect junk generated
4068 during the processing of templates until we hit a
4069 non-template function. It's not safe to do this inside a
4070 nested class, though, as the parser may have local state that
4071 is not a GC root. */
4072 if (!function_depth)
4073 ggc_collect ();
4074 return false;
4077 gcc_assert (DECL_SAVED_TREE (fn));
4079 /* We make a decision about linkage for these functions at the end
4080 of the compilation. Until that point, we do not want the back
4081 end to output them -- but we do want it to see the bodies of
4082 these functions so that it can inline them as appropriate. */
4083 if (DECL_DECLARED_INLINE_P (fn) || DECL_IMPLICIT_INSTANTIATION (fn))
4085 if (DECL_INTERFACE_KNOWN (fn))
4086 /* We've already made a decision as to how this function will
4087 be handled. */;
4088 else if (!at_eof)
4089 tentative_decl_linkage (fn);
4090 else
4091 import_export_decl (fn);
4093 /* If the user wants us to keep all inline functions, then mark
4094 this function as needed so that finish_file will make sure to
4095 output it later. Similarly, all dllexport'd functions must
4096 be emitted; there may be callers in other DLLs. */
4097 if (DECL_DECLARED_INLINE_P (fn)
4098 && !DECL_REALLY_EXTERN (fn)
4099 && (flag_keep_inline_functions
4100 || (flag_keep_inline_dllexport
4101 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (fn)))))
4103 mark_needed (fn);
4104 DECL_EXTERNAL (fn) = 0;
4108 /* If this is a constructor or destructor body, we have to clone
4109 it. */
4110 if (maybe_clone_body (fn))
4112 /* We don't want to process FN again, so pretend we've written
4113 it out, even though we haven't. */
4114 TREE_ASM_WRITTEN (fn) = 1;
4115 /* If this is an instantiation of a constexpr function, keep
4116 DECL_SAVED_TREE for explain_invalid_constexpr_fn. */
4117 if (!is_instantiation_of_constexpr (fn))
4118 DECL_SAVED_TREE (fn) = NULL_TREE;
4119 return false;
4122 /* There's no reason to do any of the work here if we're only doing
4123 semantic analysis; this code just generates RTL. */
4124 if (flag_syntax_only)
4125 return false;
4127 return true;
4130 void
4131 expand_or_defer_fn (tree fn)
4133 if (expand_or_defer_fn_1 (fn))
4135 function_depth++;
4137 /* Expand or defer, at the whim of the compilation unit manager. */
4138 cgraph_node::finalize_function (fn, function_depth > 1);
4139 emit_associated_thunks (fn);
4141 function_depth--;
4145 struct nrv_data
4147 nrv_data () : visited (37) {}
4149 tree var;
4150 tree result;
4151 hash_table<nofree_ptr_hash <tree_node> > visited;
4154 /* Helper function for walk_tree, used by finalize_nrv below. */
4156 static tree
4157 finalize_nrv_r (tree* tp, int* walk_subtrees, void* data)
4159 struct nrv_data *dp = (struct nrv_data *)data;
4160 tree_node **slot;
4162 /* No need to walk into types. There wouldn't be any need to walk into
4163 non-statements, except that we have to consider STMT_EXPRs. */
4164 if (TYPE_P (*tp))
4165 *walk_subtrees = 0;
4166 /* Change all returns to just refer to the RESULT_DECL; this is a nop,
4167 but differs from using NULL_TREE in that it indicates that we care
4168 about the value of the RESULT_DECL. */
4169 else if (TREE_CODE (*tp) == RETURN_EXPR)
4170 TREE_OPERAND (*tp, 0) = dp->result;
4171 /* Change all cleanups for the NRV to only run when an exception is
4172 thrown. */
4173 else if (TREE_CODE (*tp) == CLEANUP_STMT
4174 && CLEANUP_DECL (*tp) == dp->var)
4175 CLEANUP_EH_ONLY (*tp) = 1;
4176 /* Replace the DECL_EXPR for the NRV with an initialization of the
4177 RESULT_DECL, if needed. */
4178 else if (TREE_CODE (*tp) == DECL_EXPR
4179 && DECL_EXPR_DECL (*tp) == dp->var)
4181 tree init;
4182 if (DECL_INITIAL (dp->var)
4183 && DECL_INITIAL (dp->var) != error_mark_node)
4184 init = build2 (INIT_EXPR, void_type_node, dp->result,
4185 DECL_INITIAL (dp->var));
4186 else
4187 init = build_empty_stmt (EXPR_LOCATION (*tp));
4188 DECL_INITIAL (dp->var) = NULL_TREE;
4189 SET_EXPR_LOCATION (init, EXPR_LOCATION (*tp));
4190 *tp = init;
4192 /* And replace all uses of the NRV with the RESULT_DECL. */
4193 else if (*tp == dp->var)
4194 *tp = dp->result;
4196 /* Avoid walking into the same tree more than once. Unfortunately, we
4197 can't just use walk_tree_without duplicates because it would only call
4198 us for the first occurrence of dp->var in the function body. */
4199 slot = dp->visited.find_slot (*tp, INSERT);
4200 if (*slot)
4201 *walk_subtrees = 0;
4202 else
4203 *slot = *tp;
4205 /* Keep iterating. */
4206 return NULL_TREE;
4209 /* Called from finish_function to implement the named return value
4210 optimization by overriding all the RETURN_EXPRs and pertinent
4211 CLEANUP_STMTs and replacing all occurrences of VAR with RESULT, the
4212 RESULT_DECL for the function. */
4214 void
4215 finalize_nrv (tree *tp, tree var, tree result)
4217 struct nrv_data data;
4219 /* Copy name from VAR to RESULT. */
4220 DECL_NAME (result) = DECL_NAME (var);
4221 /* Don't forget that we take its address. */
4222 TREE_ADDRESSABLE (result) = TREE_ADDRESSABLE (var);
4223 /* Finally set DECL_VALUE_EXPR to avoid assigning
4224 a stack slot at -O0 for the original var and debug info
4225 uses RESULT location for VAR. */
4226 SET_DECL_VALUE_EXPR (var, result);
4227 DECL_HAS_VALUE_EXPR_P (var) = 1;
4229 data.var = var;
4230 data.result = result;
4231 cp_walk_tree (tp, finalize_nrv_r, &data, 0);
4234 /* Create CP_OMP_CLAUSE_INFO for clause C. Returns true if it is invalid. */
4236 bool
4237 cxx_omp_create_clause_info (tree c, tree type, bool need_default_ctor,
4238 bool need_copy_ctor, bool need_copy_assignment,
4239 bool need_dtor)
4241 int save_errorcount = errorcount;
4242 tree info, t;
4244 /* Always allocate 3 elements for simplicity. These are the
4245 function decls for the ctor, dtor, and assignment op.
4246 This layout is known to the three lang hooks,
4247 cxx_omp_clause_default_init, cxx_omp_clause_copy_init,
4248 and cxx_omp_clause_assign_op. */
4249 info = make_tree_vec (3);
4250 CP_OMP_CLAUSE_INFO (c) = info;
4252 if (need_default_ctor || need_copy_ctor)
4254 if (need_default_ctor)
4255 t = get_default_ctor (type);
4256 else
4257 t = get_copy_ctor (type, tf_warning_or_error);
4259 if (t && !trivial_fn_p (t))
4260 TREE_VEC_ELT (info, 0) = t;
4263 if (need_dtor && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
4264 TREE_VEC_ELT (info, 1) = get_dtor (type, tf_warning_or_error);
4266 if (need_copy_assignment)
4268 t = get_copy_assign (type);
4270 if (t && !trivial_fn_p (t))
4271 TREE_VEC_ELT (info, 2) = t;
4274 return errorcount != save_errorcount;
4277 /* If DECL is DECL_OMP_PRIVATIZED_MEMBER, return corresponding
4278 FIELD_DECL, otherwise return DECL itself. */
4280 static tree
4281 omp_clause_decl_field (tree decl)
4283 if (VAR_P (decl)
4284 && DECL_HAS_VALUE_EXPR_P (decl)
4285 && DECL_ARTIFICIAL (decl)
4286 && DECL_LANG_SPECIFIC (decl)
4287 && DECL_OMP_PRIVATIZED_MEMBER (decl))
4289 tree f = DECL_VALUE_EXPR (decl);
4290 if (TREE_CODE (f) == INDIRECT_REF)
4291 f = TREE_OPERAND (f, 0);
4292 if (TREE_CODE (f) == COMPONENT_REF)
4294 f = TREE_OPERAND (f, 1);
4295 gcc_assert (TREE_CODE (f) == FIELD_DECL);
4296 return f;
4299 return NULL_TREE;
4302 /* Adjust DECL if needed for printing using %qE. */
4304 static tree
4305 omp_clause_printable_decl (tree decl)
4307 tree t = omp_clause_decl_field (decl);
4308 if (t)
4309 return t;
4310 return decl;
4313 /* For a FIELD_DECL F and corresponding DECL_OMP_PRIVATIZED_MEMBER
4314 VAR_DECL T that doesn't need a DECL_EXPR added, record it for
4315 privatization. */
4317 static void
4318 omp_note_field_privatization (tree f, tree t)
4320 if (!omp_private_member_map)
4321 omp_private_member_map = new hash_map<tree, tree>;
4322 tree &v = omp_private_member_map->get_or_insert (f);
4323 if (v == NULL_TREE)
4325 v = t;
4326 omp_private_member_vec.safe_push (f);
4327 /* Signal that we don't want to create DECL_EXPR for this dummy var. */
4328 omp_private_member_vec.safe_push (integer_zero_node);
4332 /* Privatize FIELD_DECL T, return corresponding DECL_OMP_PRIVATIZED_MEMBER
4333 dummy VAR_DECL. */
4335 tree
4336 omp_privatize_field (tree t, bool shared)
4338 tree m = finish_non_static_data_member (t, NULL_TREE, NULL_TREE);
4339 if (m == error_mark_node)
4340 return error_mark_node;
4341 if (!omp_private_member_map && !shared)
4342 omp_private_member_map = new hash_map<tree, tree>;
4343 if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE)
4345 gcc_assert (TREE_CODE (m) == INDIRECT_REF);
4346 m = TREE_OPERAND (m, 0);
4348 tree vb = NULL_TREE;
4349 tree &v = shared ? vb : omp_private_member_map->get_or_insert (t);
4350 if (v == NULL_TREE)
4352 v = create_temporary_var (TREE_TYPE (m));
4353 if (!DECL_LANG_SPECIFIC (v))
4354 retrofit_lang_decl (v);
4355 DECL_OMP_PRIVATIZED_MEMBER (v) = 1;
4356 SET_DECL_VALUE_EXPR (v, m);
4357 DECL_HAS_VALUE_EXPR_P (v) = 1;
4358 if (!shared)
4359 omp_private_member_vec.safe_push (t);
4361 return v;
4364 /* Helper function for handle_omp_array_sections. Called recursively
4365 to handle multiple array-section-subscripts. C is the clause,
4366 T current expression (initially OMP_CLAUSE_DECL), which is either
4367 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
4368 expression if specified, TREE_VALUE length expression if specified,
4369 TREE_CHAIN is what it has been specified after, or some decl.
4370 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
4371 set to true if any of the array-section-subscript could have length
4372 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
4373 first array-section-subscript which is known not to have length
4374 of one. Given say:
4375 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
4376 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
4377 all are or may have length of 1, array-section-subscript [:2] is the
4378 first one known not to have length 1. For array-section-subscript
4379 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
4380 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
4381 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
4382 case though, as some lengths could be zero. */
4384 static tree
4385 handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
4386 bool &maybe_zero_len, unsigned int &first_non_one,
4387 bool is_omp)
4389 tree ret, low_bound, length, type;
4390 if (TREE_CODE (t) != TREE_LIST)
4392 if (error_operand_p (t))
4393 return error_mark_node;
4394 if (REFERENCE_REF_P (t)
4395 && TREE_CODE (TREE_OPERAND (t, 0)) == COMPONENT_REF)
4396 t = TREE_OPERAND (t, 0);
4397 ret = t;
4398 if (TREE_CODE (t) == COMPONENT_REF
4399 && is_omp
4400 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
4401 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO
4402 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FROM)
4403 && !type_dependent_expression_p (t))
4405 if (DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
4407 error_at (OMP_CLAUSE_LOCATION (c),
4408 "bit-field %qE in %qs clause",
4409 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4410 return error_mark_node;
4412 while (TREE_CODE (t) == COMPONENT_REF)
4414 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == UNION_TYPE)
4416 error_at (OMP_CLAUSE_LOCATION (c),
4417 "%qE is a member of a union", t);
4418 return error_mark_node;
4420 t = TREE_OPERAND (t, 0);
4423 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
4425 if (processing_template_decl)
4426 return NULL_TREE;
4427 if (DECL_P (t))
4428 error_at (OMP_CLAUSE_LOCATION (c),
4429 "%qD is not a variable in %qs clause", t,
4430 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4431 else
4432 error_at (OMP_CLAUSE_LOCATION (c),
4433 "%qE is not a variable in %qs clause", t,
4434 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4435 return error_mark_node;
4437 else if (TREE_CODE (t) == PARM_DECL
4438 && DECL_ARTIFICIAL (t)
4439 && DECL_NAME (t) == this_identifier)
4441 error_at (OMP_CLAUSE_LOCATION (c),
4442 "%<this%> allowed in OpenMP only in %<declare simd%>"
4443 " clauses");
4444 return error_mark_node;
4446 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
4447 && VAR_P (t) && CP_DECL_THREAD_LOCAL_P (t))
4449 error_at (OMP_CLAUSE_LOCATION (c),
4450 "%qD is threadprivate variable in %qs clause", t,
4451 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4452 return error_mark_node;
4454 if (type_dependent_expression_p (ret))
4455 return NULL_TREE;
4456 ret = convert_from_reference (ret);
4457 return ret;
4460 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
4461 && TREE_CODE (TREE_CHAIN (t)) == FIELD_DECL)
4462 TREE_CHAIN (t) = omp_privatize_field (TREE_CHAIN (t), false);
4463 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
4464 maybe_zero_len, first_non_one, is_omp);
4465 if (ret == error_mark_node || ret == NULL_TREE)
4466 return ret;
4468 type = TREE_TYPE (ret);
4469 low_bound = TREE_PURPOSE (t);
4470 length = TREE_VALUE (t);
4471 if ((low_bound && type_dependent_expression_p (low_bound))
4472 || (length && type_dependent_expression_p (length)))
4473 return NULL_TREE;
4475 if (low_bound == error_mark_node || length == error_mark_node)
4476 return error_mark_node;
4478 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
4480 error_at (OMP_CLAUSE_LOCATION (c),
4481 "low bound %qE of array section does not have integral type",
4482 low_bound);
4483 return error_mark_node;
4485 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
4487 error_at (OMP_CLAUSE_LOCATION (c),
4488 "length %qE of array section does not have integral type",
4489 length);
4490 return error_mark_node;
4492 if (low_bound)
4493 low_bound = mark_rvalue_use (low_bound);
4494 if (length)
4495 length = mark_rvalue_use (length);
4496 /* We need to reduce to real constant-values for checks below. */
4497 if (length)
4498 length = fold_simple (length);
4499 if (low_bound)
4500 low_bound = fold_simple (low_bound);
4501 if (low_bound
4502 && TREE_CODE (low_bound) == INTEGER_CST
4503 && TYPE_PRECISION (TREE_TYPE (low_bound))
4504 > TYPE_PRECISION (sizetype))
4505 low_bound = fold_convert (sizetype, low_bound);
4506 if (length
4507 && TREE_CODE (length) == INTEGER_CST
4508 && TYPE_PRECISION (TREE_TYPE (length))
4509 > TYPE_PRECISION (sizetype))
4510 length = fold_convert (sizetype, length);
4511 if (low_bound == NULL_TREE)
4512 low_bound = integer_zero_node;
4514 if (length != NULL_TREE)
4516 if (!integer_nonzerop (length))
4518 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
4519 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
4521 if (integer_zerop (length))
4523 error_at (OMP_CLAUSE_LOCATION (c),
4524 "zero length array section in %qs clause",
4525 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4526 return error_mark_node;
4529 else
4530 maybe_zero_len = true;
4532 if (first_non_one == types.length ()
4533 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
4534 first_non_one++;
4536 if (TREE_CODE (type) == ARRAY_TYPE)
4538 if (length == NULL_TREE
4539 && (TYPE_DOMAIN (type) == NULL_TREE
4540 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
4542 error_at (OMP_CLAUSE_LOCATION (c),
4543 "for unknown bound array type length expression must "
4544 "be specified");
4545 return error_mark_node;
4547 if (TREE_CODE (low_bound) == INTEGER_CST
4548 && tree_int_cst_sgn (low_bound) == -1)
4550 error_at (OMP_CLAUSE_LOCATION (c),
4551 "negative low bound in array section in %qs clause",
4552 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4553 return error_mark_node;
4555 if (length != NULL_TREE
4556 && TREE_CODE (length) == INTEGER_CST
4557 && tree_int_cst_sgn (length) == -1)
4559 error_at (OMP_CLAUSE_LOCATION (c),
4560 "negative length in array section in %qs clause",
4561 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4562 return error_mark_node;
4564 if (TYPE_DOMAIN (type)
4565 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
4566 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
4567 == INTEGER_CST)
4569 tree size = size_binop (PLUS_EXPR,
4570 TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
4571 size_one_node);
4572 if (TREE_CODE (low_bound) == INTEGER_CST)
4574 if (tree_int_cst_lt (size, low_bound))
4576 error_at (OMP_CLAUSE_LOCATION (c),
4577 "low bound %qE above array section size "
4578 "in %qs clause", low_bound,
4579 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4580 return error_mark_node;
4582 if (tree_int_cst_equal (size, low_bound))
4584 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
4585 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
4587 error_at (OMP_CLAUSE_LOCATION (c),
4588 "zero length array section in %qs clause",
4589 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4590 return error_mark_node;
4592 maybe_zero_len = true;
4594 else if (length == NULL_TREE
4595 && first_non_one == types.length ()
4596 && tree_int_cst_equal
4597 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
4598 low_bound))
4599 first_non_one++;
4601 else if (length == NULL_TREE)
4603 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
4604 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION)
4605 maybe_zero_len = true;
4606 if (first_non_one == types.length ())
4607 first_non_one++;
4609 if (length && TREE_CODE (length) == INTEGER_CST)
4611 if (tree_int_cst_lt (size, length))
4613 error_at (OMP_CLAUSE_LOCATION (c),
4614 "length %qE above array section size "
4615 "in %qs clause", length,
4616 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4617 return error_mark_node;
4619 if (TREE_CODE (low_bound) == INTEGER_CST)
4621 tree lbpluslen
4622 = size_binop (PLUS_EXPR,
4623 fold_convert (sizetype, low_bound),
4624 fold_convert (sizetype, length));
4625 if (TREE_CODE (lbpluslen) == INTEGER_CST
4626 && tree_int_cst_lt (size, lbpluslen))
4628 error_at (OMP_CLAUSE_LOCATION (c),
4629 "high bound %qE above array section size "
4630 "in %qs clause", lbpluslen,
4631 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4632 return error_mark_node;
4637 else if (length == NULL_TREE)
4639 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
4640 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION)
4641 maybe_zero_len = true;
4642 if (first_non_one == types.length ())
4643 first_non_one++;
4646 /* For [lb:] we will need to evaluate lb more than once. */
4647 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
4649 tree lb = cp_save_expr (low_bound);
4650 if (lb != low_bound)
4652 TREE_PURPOSE (t) = lb;
4653 low_bound = lb;
4657 else if (TREE_CODE (type) == POINTER_TYPE)
4659 if (length == NULL_TREE)
4661 error_at (OMP_CLAUSE_LOCATION (c),
4662 "for pointer type length expression must be specified");
4663 return error_mark_node;
4665 if (length != NULL_TREE
4666 && TREE_CODE (length) == INTEGER_CST
4667 && tree_int_cst_sgn (length) == -1)
4669 error_at (OMP_CLAUSE_LOCATION (c),
4670 "negative length in array section in %qs clause",
4671 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4672 return error_mark_node;
4674 /* If there is a pointer type anywhere but in the very first
4675 array-section-subscript, the array section can't be contiguous. */
4676 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
4677 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
4679 error_at (OMP_CLAUSE_LOCATION (c),
4680 "array section is not contiguous in %qs clause",
4681 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4682 return error_mark_node;
4685 else
4687 error_at (OMP_CLAUSE_LOCATION (c),
4688 "%qE does not have pointer or array type", ret);
4689 return error_mark_node;
4691 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
4692 types.safe_push (TREE_TYPE (ret));
4693 /* We will need to evaluate lb more than once. */
4694 tree lb = cp_save_expr (low_bound);
4695 if (lb != low_bound)
4697 TREE_PURPOSE (t) = lb;
4698 low_bound = lb;
4700 ret = grok_array_decl (OMP_CLAUSE_LOCATION (c), ret, low_bound, false);
4701 return ret;
4704 /* Handle array sections for clause C. */
4706 static bool
4707 handle_omp_array_sections (tree c, bool is_omp)
4709 bool maybe_zero_len = false;
4710 unsigned int first_non_one = 0;
4711 auto_vec<tree, 10> types;
4712 tree first = handle_omp_array_sections_1 (c, OMP_CLAUSE_DECL (c), types,
4713 maybe_zero_len, first_non_one,
4714 is_omp);
4715 if (first == error_mark_node)
4716 return true;
4717 if (first == NULL_TREE)
4718 return false;
4719 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
4721 tree t = OMP_CLAUSE_DECL (c);
4722 tree tem = NULL_TREE;
4723 if (processing_template_decl)
4724 return false;
4725 /* Need to evaluate side effects in the length expressions
4726 if any. */
4727 while (TREE_CODE (t) == TREE_LIST)
4729 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
4731 if (tem == NULL_TREE)
4732 tem = TREE_VALUE (t);
4733 else
4734 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
4735 TREE_VALUE (t), tem);
4737 t = TREE_CHAIN (t);
4739 if (tem)
4740 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
4741 OMP_CLAUSE_DECL (c) = first;
4743 else
4745 unsigned int num = types.length (), i;
4746 tree t, side_effects = NULL_TREE, size = NULL_TREE;
4747 tree condition = NULL_TREE;
4749 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
4750 maybe_zero_len = true;
4751 if (processing_template_decl && maybe_zero_len)
4752 return false;
4754 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
4755 t = TREE_CHAIN (t))
4757 tree low_bound = TREE_PURPOSE (t);
4758 tree length = TREE_VALUE (t);
4760 i--;
4761 if (low_bound
4762 && TREE_CODE (low_bound) == INTEGER_CST
4763 && TYPE_PRECISION (TREE_TYPE (low_bound))
4764 > TYPE_PRECISION (sizetype))
4765 low_bound = fold_convert (sizetype, low_bound);
4766 if (length
4767 && TREE_CODE (length) == INTEGER_CST
4768 && TYPE_PRECISION (TREE_TYPE (length))
4769 > TYPE_PRECISION (sizetype))
4770 length = fold_convert (sizetype, length);
4771 if (low_bound == NULL_TREE)
4772 low_bound = integer_zero_node;
4773 if (!maybe_zero_len && i > first_non_one)
4775 if (integer_nonzerop (low_bound))
4776 goto do_warn_noncontiguous;
4777 if (length != NULL_TREE
4778 && TREE_CODE (length) == INTEGER_CST
4779 && TYPE_DOMAIN (types[i])
4780 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
4781 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
4782 == INTEGER_CST)
4784 tree size;
4785 size = size_binop (PLUS_EXPR,
4786 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
4787 size_one_node);
4788 if (!tree_int_cst_equal (length, size))
4790 do_warn_noncontiguous:
4791 error_at (OMP_CLAUSE_LOCATION (c),
4792 "array section is not contiguous in %qs "
4793 "clause",
4794 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4795 return true;
4798 if (!processing_template_decl
4799 && length != NULL_TREE
4800 && TREE_SIDE_EFFECTS (length))
4802 if (side_effects == NULL_TREE)
4803 side_effects = length;
4804 else
4805 side_effects = build2 (COMPOUND_EXPR,
4806 TREE_TYPE (side_effects),
4807 length, side_effects);
4810 else if (processing_template_decl)
4811 continue;
4812 else
4814 tree l;
4816 if (i > first_non_one
4817 && ((length && integer_nonzerop (length))
4818 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION))
4819 continue;
4820 if (length)
4821 l = fold_convert (sizetype, length);
4822 else
4824 l = size_binop (PLUS_EXPR,
4825 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
4826 size_one_node);
4827 l = size_binop (MINUS_EXPR, l,
4828 fold_convert (sizetype, low_bound));
4830 if (i > first_non_one)
4832 l = fold_build2 (NE_EXPR, boolean_type_node, l,
4833 size_zero_node);
4834 if (condition == NULL_TREE)
4835 condition = l;
4836 else
4837 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
4838 l, condition);
4840 else if (size == NULL_TREE)
4842 size = size_in_bytes (TREE_TYPE (types[i]));
4843 tree eltype = TREE_TYPE (types[num - 1]);
4844 while (TREE_CODE (eltype) == ARRAY_TYPE)
4845 eltype = TREE_TYPE (eltype);
4846 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
4847 size = size_binop (EXACT_DIV_EXPR, size,
4848 size_in_bytes (eltype));
4849 size = size_binop (MULT_EXPR, size, l);
4850 if (condition)
4851 size = fold_build3 (COND_EXPR, sizetype, condition,
4852 size, size_zero_node);
4854 else
4855 size = size_binop (MULT_EXPR, size, l);
4858 if (!processing_template_decl)
4860 if (side_effects)
4861 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
4862 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
4864 size = size_binop (MINUS_EXPR, size, size_one_node);
4865 tree index_type = build_index_type (size);
4866 tree eltype = TREE_TYPE (first);
4867 while (TREE_CODE (eltype) == ARRAY_TYPE)
4868 eltype = TREE_TYPE (eltype);
4869 tree type = build_array_type (eltype, index_type);
4870 tree ptype = build_pointer_type (eltype);
4871 if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE
4872 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (t))))
4873 t = convert_from_reference (t);
4874 else if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
4875 t = build_fold_addr_expr (t);
4876 tree t2 = build_fold_addr_expr (first);
4877 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
4878 ptrdiff_type_node, t2);
4879 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
4880 ptrdiff_type_node, t2,
4881 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
4882 ptrdiff_type_node, t));
4883 if (tree_fits_shwi_p (t2))
4884 t = build2 (MEM_REF, type, t,
4885 build_int_cst (ptype, tree_to_shwi (t2)));
4886 else
4888 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
4889 sizetype, t2);
4890 t = build2_loc (OMP_CLAUSE_LOCATION (c), POINTER_PLUS_EXPR,
4891 TREE_TYPE (t), t, t2);
4892 t = build2 (MEM_REF, type, t, build_int_cst (ptype, 0));
4894 OMP_CLAUSE_DECL (c) = t;
4895 return false;
4897 OMP_CLAUSE_DECL (c) = first;
4898 OMP_CLAUSE_SIZE (c) = size;
4899 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
4900 || (TREE_CODE (t) == COMPONENT_REF
4901 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE))
4902 return false;
4903 if (is_omp)
4904 switch (OMP_CLAUSE_MAP_KIND (c))
4906 case GOMP_MAP_ALLOC:
4907 case GOMP_MAP_TO:
4908 case GOMP_MAP_FROM:
4909 case GOMP_MAP_TOFROM:
4910 case GOMP_MAP_ALWAYS_TO:
4911 case GOMP_MAP_ALWAYS_FROM:
4912 case GOMP_MAP_ALWAYS_TOFROM:
4913 case GOMP_MAP_RELEASE:
4914 case GOMP_MAP_DELETE:
4915 OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (c) = 1;
4916 break;
4917 default:
4918 break;
4920 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
4921 OMP_CLAUSE_MAP);
4922 if (!is_omp)
4923 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_POINTER);
4924 else if (TREE_CODE (t) == COMPONENT_REF)
4925 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ALWAYS_POINTER);
4926 else if (REFERENCE_REF_P (t)
4927 && TREE_CODE (TREE_OPERAND (t, 0)) == COMPONENT_REF)
4929 t = TREE_OPERAND (t, 0);
4930 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ALWAYS_POINTER);
4932 else
4933 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_FIRSTPRIVATE_POINTER);
4934 if (OMP_CLAUSE_MAP_KIND (c2) != GOMP_MAP_FIRSTPRIVATE_POINTER
4935 && !cxx_mark_addressable (t))
4936 return false;
4937 OMP_CLAUSE_DECL (c2) = t;
4938 t = build_fold_addr_expr (first);
4939 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
4940 ptrdiff_type_node, t);
4941 tree ptr = OMP_CLAUSE_DECL (c2);
4942 ptr = convert_from_reference (ptr);
4943 if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
4944 ptr = build_fold_addr_expr (ptr);
4945 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
4946 ptrdiff_type_node, t,
4947 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
4948 ptrdiff_type_node, ptr));
4949 OMP_CLAUSE_SIZE (c2) = t;
4950 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
4951 OMP_CLAUSE_CHAIN (c) = c2;
4952 ptr = OMP_CLAUSE_DECL (c2);
4953 if (OMP_CLAUSE_MAP_KIND (c2) != GOMP_MAP_FIRSTPRIVATE_POINTER
4954 && TREE_CODE (TREE_TYPE (ptr)) == REFERENCE_TYPE
4955 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (ptr))))
4957 tree c3 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
4958 OMP_CLAUSE_MAP);
4959 OMP_CLAUSE_SET_MAP_KIND (c3, OMP_CLAUSE_MAP_KIND (c2));
4960 OMP_CLAUSE_DECL (c3) = ptr;
4961 if (OMP_CLAUSE_MAP_KIND (c2) == GOMP_MAP_ALWAYS_POINTER)
4962 OMP_CLAUSE_DECL (c2) = build_simple_mem_ref (ptr);
4963 else
4964 OMP_CLAUSE_DECL (c2) = convert_from_reference (ptr);
4965 OMP_CLAUSE_SIZE (c3) = size_zero_node;
4966 OMP_CLAUSE_CHAIN (c3) = OMP_CLAUSE_CHAIN (c2);
4967 OMP_CLAUSE_CHAIN (c2) = c3;
4971 return false;
4974 /* Return identifier to look up for omp declare reduction. */
4976 tree
4977 omp_reduction_id (enum tree_code reduction_code, tree reduction_id, tree type)
4979 const char *p = NULL;
4980 const char *m = NULL;
4981 switch (reduction_code)
4983 case PLUS_EXPR:
4984 case MULT_EXPR:
4985 case MINUS_EXPR:
4986 case BIT_AND_EXPR:
4987 case BIT_XOR_EXPR:
4988 case BIT_IOR_EXPR:
4989 case TRUTH_ANDIF_EXPR:
4990 case TRUTH_ORIF_EXPR:
4991 reduction_id = ansi_opname (reduction_code);
4992 break;
4993 case MIN_EXPR:
4994 p = "min";
4995 break;
4996 case MAX_EXPR:
4997 p = "max";
4998 break;
4999 default:
5000 break;
5003 if (p == NULL)
5005 if (TREE_CODE (reduction_id) != IDENTIFIER_NODE)
5006 return error_mark_node;
5007 p = IDENTIFIER_POINTER (reduction_id);
5010 if (type != NULL_TREE)
5011 m = mangle_type_string (TYPE_MAIN_VARIANT (type));
5013 const char prefix[] = "omp declare reduction ";
5014 size_t lenp = sizeof (prefix);
5015 if (strncmp (p, prefix, lenp - 1) == 0)
5016 lenp = 1;
5017 size_t len = strlen (p);
5018 size_t lenm = m ? strlen (m) + 1 : 0;
5019 char *name = XALLOCAVEC (char, lenp + len + lenm);
5020 if (lenp > 1)
5021 memcpy (name, prefix, lenp - 1);
5022 memcpy (name + lenp - 1, p, len + 1);
5023 if (m)
5025 name[lenp + len - 1] = '~';
5026 memcpy (name + lenp + len, m, lenm);
5028 return get_identifier (name);
5031 /* Lookup OpenMP UDR ID for TYPE, return the corresponding artificial
5032 FUNCTION_DECL or NULL_TREE if not found. */
5034 static tree
5035 omp_reduction_lookup (location_t loc, tree id, tree type, tree *baselinkp,
5036 vec<tree> *ambiguousp)
5038 tree orig_id = id;
5039 tree baselink = NULL_TREE;
5040 if (identifier_p (id))
5042 cp_id_kind idk;
5043 bool nonint_cst_expression_p;
5044 const char *error_msg;
5045 id = omp_reduction_id (ERROR_MARK, id, type);
5046 tree decl = lookup_name (id);
5047 if (decl == NULL_TREE)
5048 decl = error_mark_node;
5049 id = finish_id_expression (id, decl, NULL_TREE, &idk, false, true,
5050 &nonint_cst_expression_p, false, true, false,
5051 false, &error_msg, loc);
5052 if (idk == CP_ID_KIND_UNQUALIFIED
5053 && identifier_p (id))
5055 vec<tree, va_gc> *args = NULL;
5056 vec_safe_push (args, build_reference_type (type));
5057 id = perform_koenig_lookup (id, args, tf_none);
5060 else if (TREE_CODE (id) == SCOPE_REF)
5061 id = lookup_qualified_name (TREE_OPERAND (id, 0),
5062 omp_reduction_id (ERROR_MARK,
5063 TREE_OPERAND (id, 1),
5064 type),
5065 false, false);
5066 tree fns = id;
5067 if (id && is_overloaded_fn (id))
5068 id = get_fns (id);
5069 for (; id; id = OVL_NEXT (id))
5071 tree fndecl = OVL_CURRENT (id);
5072 if (TREE_CODE (fndecl) == FUNCTION_DECL)
5074 tree argtype = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
5075 if (same_type_p (TREE_TYPE (argtype), type))
5076 break;
5079 if (id && BASELINK_P (fns))
5081 if (baselinkp)
5082 *baselinkp = fns;
5083 else
5084 baselink = fns;
5086 if (id == NULL_TREE && CLASS_TYPE_P (type) && TYPE_BINFO (type))
5088 vec<tree> ambiguous = vNULL;
5089 tree binfo = TYPE_BINFO (type), base_binfo, ret = NULL_TREE;
5090 unsigned int ix;
5091 if (ambiguousp == NULL)
5092 ambiguousp = &ambiguous;
5093 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
5095 id = omp_reduction_lookup (loc, orig_id, BINFO_TYPE (base_binfo),
5096 baselinkp ? baselinkp : &baselink,
5097 ambiguousp);
5098 if (id == NULL_TREE)
5099 continue;
5100 if (!ambiguousp->is_empty ())
5101 ambiguousp->safe_push (id);
5102 else if (ret != NULL_TREE)
5104 ambiguousp->safe_push (ret);
5105 ambiguousp->safe_push (id);
5106 ret = NULL_TREE;
5108 else
5109 ret = id;
5111 if (ambiguousp != &ambiguous)
5112 return ret;
5113 if (!ambiguous.is_empty ())
5115 const char *str = _("candidates are:");
5116 unsigned int idx;
5117 tree udr;
5118 error_at (loc, "user defined reduction lookup is ambiguous");
5119 FOR_EACH_VEC_ELT (ambiguous, idx, udr)
5121 inform (DECL_SOURCE_LOCATION (udr), "%s %#D", str, udr);
5122 if (idx == 0)
5123 str = get_spaces (str);
5125 ambiguous.release ();
5126 ret = error_mark_node;
5127 baselink = NULL_TREE;
5129 id = ret;
5131 if (id && baselink)
5132 perform_or_defer_access_check (BASELINK_BINFO (baselink),
5133 id, id, tf_warning_or_error);
5134 return id;
5137 /* Helper function for cp_parser_omp_declare_reduction_exprs
5138 and tsubst_omp_udr.
5139 Remove CLEANUP_STMT for data (omp_priv variable).
5140 Also append INIT_EXPR for DECL_INITIAL of omp_priv after its
5141 DECL_EXPR. */
5143 tree
5144 cp_remove_omp_priv_cleanup_stmt (tree *tp, int *walk_subtrees, void *data)
5146 if (TYPE_P (*tp))
5147 *walk_subtrees = 0;
5148 else if (TREE_CODE (*tp) == CLEANUP_STMT && CLEANUP_DECL (*tp) == (tree) data)
5149 *tp = CLEANUP_BODY (*tp);
5150 else if (TREE_CODE (*tp) == DECL_EXPR)
5152 tree decl = DECL_EXPR_DECL (*tp);
5153 if (!processing_template_decl
5154 && decl == (tree) data
5155 && DECL_INITIAL (decl)
5156 && DECL_INITIAL (decl) != error_mark_node)
5158 tree list = NULL_TREE;
5159 append_to_statement_list_force (*tp, &list);
5160 tree init_expr = build2 (INIT_EXPR, void_type_node,
5161 decl, DECL_INITIAL (decl));
5162 DECL_INITIAL (decl) = NULL_TREE;
5163 append_to_statement_list_force (init_expr, &list);
5164 *tp = list;
5167 return NULL_TREE;
5170 /* Data passed from cp_check_omp_declare_reduction to
5171 cp_check_omp_declare_reduction_r. */
5173 struct cp_check_omp_declare_reduction_data
5175 location_t loc;
5176 tree stmts[7];
5177 bool combiner_p;
5180 /* Helper function for cp_check_omp_declare_reduction, called via
5181 cp_walk_tree. */
5183 static tree
5184 cp_check_omp_declare_reduction_r (tree *tp, int *, void *data)
5186 struct cp_check_omp_declare_reduction_data *udr_data
5187 = (struct cp_check_omp_declare_reduction_data *) data;
5188 if (SSA_VAR_P (*tp)
5189 && !DECL_ARTIFICIAL (*tp)
5190 && *tp != DECL_EXPR_DECL (udr_data->stmts[udr_data->combiner_p ? 0 : 3])
5191 && *tp != DECL_EXPR_DECL (udr_data->stmts[udr_data->combiner_p ? 1 : 4]))
5193 location_t loc = udr_data->loc;
5194 if (udr_data->combiner_p)
5195 error_at (loc, "%<#pragma omp declare reduction%> combiner refers to "
5196 "variable %qD which is not %<omp_out%> nor %<omp_in%>",
5197 *tp);
5198 else
5199 error_at (loc, "%<#pragma omp declare reduction%> initializer refers "
5200 "to variable %qD which is not %<omp_priv%> nor "
5201 "%<omp_orig%>",
5202 *tp);
5203 return *tp;
5205 return NULL_TREE;
5208 /* Diagnose violation of OpenMP #pragma omp declare reduction restrictions. */
5210 void
5211 cp_check_omp_declare_reduction (tree udr)
5213 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (udr)));
5214 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
5215 type = TREE_TYPE (type);
5216 int i;
5217 location_t loc = DECL_SOURCE_LOCATION (udr);
5219 if (type == error_mark_node)
5220 return;
5221 if (ARITHMETIC_TYPE_P (type))
5223 static enum tree_code predef_codes[]
5224 = { PLUS_EXPR, MULT_EXPR, MINUS_EXPR, BIT_AND_EXPR, BIT_XOR_EXPR,
5225 BIT_IOR_EXPR, TRUTH_ANDIF_EXPR, TRUTH_ORIF_EXPR };
5226 for (i = 0; i < 8; i++)
5228 tree id = omp_reduction_id (predef_codes[i], NULL_TREE, NULL_TREE);
5229 const char *n1 = IDENTIFIER_POINTER (DECL_NAME (udr));
5230 const char *n2 = IDENTIFIER_POINTER (id);
5231 if (strncmp (n1, n2, IDENTIFIER_LENGTH (id)) == 0
5232 && (n1[IDENTIFIER_LENGTH (id)] == '~'
5233 || n1[IDENTIFIER_LENGTH (id)] == '\0'))
5234 break;
5237 if (i == 8
5238 && TREE_CODE (type) != COMPLEX_EXPR)
5240 const char prefix_minmax[] = "omp declare reduction m";
5241 size_t prefix_size = sizeof (prefix_minmax) - 1;
5242 const char *n = IDENTIFIER_POINTER (DECL_NAME (udr));
5243 if (strncmp (IDENTIFIER_POINTER (DECL_NAME (udr)),
5244 prefix_minmax, prefix_size) == 0
5245 && ((n[prefix_size] == 'i' && n[prefix_size + 1] == 'n')
5246 || (n[prefix_size] == 'a' && n[prefix_size + 1] == 'x'))
5247 && (n[prefix_size + 2] == '~' || n[prefix_size + 2] == '\0'))
5248 i = 0;
5250 if (i < 8)
5252 error_at (loc, "predeclared arithmetic type %qT in "
5253 "%<#pragma omp declare reduction%>", type);
5254 return;
5257 else if (TREE_CODE (type) == FUNCTION_TYPE
5258 || TREE_CODE (type) == METHOD_TYPE
5259 || TREE_CODE (type) == ARRAY_TYPE)
5261 error_at (loc, "function or array type %qT in "
5262 "%<#pragma omp declare reduction%>", type);
5263 return;
5265 else if (TREE_CODE (type) == REFERENCE_TYPE)
5267 error_at (loc, "reference type %qT in %<#pragma omp declare reduction%>",
5268 type);
5269 return;
5271 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
5273 error_at (loc, "const, volatile or __restrict qualified type %qT in "
5274 "%<#pragma omp declare reduction%>", type);
5275 return;
5278 tree body = DECL_SAVED_TREE (udr);
5279 if (body == NULL_TREE || TREE_CODE (body) != STATEMENT_LIST)
5280 return;
5282 tree_stmt_iterator tsi;
5283 struct cp_check_omp_declare_reduction_data data;
5284 memset (data.stmts, 0, sizeof data.stmts);
5285 for (i = 0, tsi = tsi_start (body);
5286 i < 7 && !tsi_end_p (tsi);
5287 i++, tsi_next (&tsi))
5288 data.stmts[i] = tsi_stmt (tsi);
5289 data.loc = loc;
5290 gcc_assert (tsi_end_p (tsi));
5291 if (i >= 3)
5293 gcc_assert (TREE_CODE (data.stmts[0]) == DECL_EXPR
5294 && TREE_CODE (data.stmts[1]) == DECL_EXPR);
5295 if (TREE_NO_WARNING (DECL_EXPR_DECL (data.stmts[0])))
5296 return;
5297 data.combiner_p = true;
5298 if (cp_walk_tree (&data.stmts[2], cp_check_omp_declare_reduction_r,
5299 &data, NULL))
5300 TREE_NO_WARNING (DECL_EXPR_DECL (data.stmts[0])) = 1;
5302 if (i >= 6)
5304 gcc_assert (TREE_CODE (data.stmts[3]) == DECL_EXPR
5305 && TREE_CODE (data.stmts[4]) == DECL_EXPR);
5306 data.combiner_p = false;
5307 if (cp_walk_tree (&data.stmts[5], cp_check_omp_declare_reduction_r,
5308 &data, NULL)
5309 || cp_walk_tree (&DECL_INITIAL (DECL_EXPR_DECL (data.stmts[3])),
5310 cp_check_omp_declare_reduction_r, &data, NULL))
5311 TREE_NO_WARNING (DECL_EXPR_DECL (data.stmts[0])) = 1;
5312 if (i == 7)
5313 gcc_assert (TREE_CODE (data.stmts[6]) == DECL_EXPR);
5317 /* Helper function of finish_omp_clauses. Clone STMT as if we were making
5318 an inline call. But, remap
5319 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
5320 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
5322 static tree
5323 clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
5324 tree decl, tree placeholder)
5326 copy_body_data id;
5327 hash_map<tree, tree> decl_map;
5329 decl_map.put (omp_decl1, placeholder);
5330 decl_map.put (omp_decl2, decl);
5331 memset (&id, 0, sizeof (id));
5332 id.src_fn = DECL_CONTEXT (omp_decl1);
5333 id.dst_fn = current_function_decl;
5334 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
5335 id.decl_map = &decl_map;
5337 id.copy_decl = copy_decl_no_change;
5338 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
5339 id.transform_new_cfg = true;
5340 id.transform_return_to_modify = false;
5341 id.transform_lang_insert_block = NULL;
5342 id.eh_lp_nr = 0;
5343 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
5344 return stmt;
5347 /* Helper function of finish_omp_clauses, called via cp_walk_tree.
5348 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
5350 static tree
5351 find_omp_placeholder_r (tree *tp, int *, void *data)
5353 if (*tp == (tree) data)
5354 return *tp;
5355 return NULL_TREE;
5358 /* Helper function of finish_omp_clauses. Handle OMP_CLAUSE_REDUCTION C.
5359 Return true if there is some error and the clause should be removed. */
5361 static bool
5362 finish_omp_reduction_clause (tree c, bool *need_default_ctor, bool *need_dtor)
5364 tree t = OMP_CLAUSE_DECL (c);
5365 bool predefined = false;
5366 if (TREE_CODE (t) == TREE_LIST)
5368 gcc_assert (processing_template_decl);
5369 return false;
5371 tree type = TREE_TYPE (t);
5372 if (TREE_CODE (t) == MEM_REF)
5373 type = TREE_TYPE (type);
5374 if (TREE_CODE (type) == REFERENCE_TYPE)
5375 type = TREE_TYPE (type);
5376 if (TREE_CODE (type) == ARRAY_TYPE)
5378 tree oatype = type;
5379 gcc_assert (TREE_CODE (t) != MEM_REF);
5380 while (TREE_CODE (type) == ARRAY_TYPE)
5381 type = TREE_TYPE (type);
5382 if (!processing_template_decl)
5384 t = require_complete_type (t);
5385 if (t == error_mark_node)
5386 return true;
5387 tree size = size_binop (EXACT_DIV_EXPR, TYPE_SIZE_UNIT (oatype),
5388 TYPE_SIZE_UNIT (type));
5389 if (integer_zerop (size))
5391 error ("%qE in %<reduction%> clause is a zero size array",
5392 omp_clause_printable_decl (t));
5393 return true;
5395 size = size_binop (MINUS_EXPR, size, size_one_node);
5396 tree index_type = build_index_type (size);
5397 tree atype = build_array_type (type, index_type);
5398 tree ptype = build_pointer_type (type);
5399 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
5400 t = build_fold_addr_expr (t);
5401 t = build2 (MEM_REF, atype, t, build_int_cst (ptype, 0));
5402 OMP_CLAUSE_DECL (c) = t;
5405 if (type == error_mark_node)
5406 return true;
5407 else if (ARITHMETIC_TYPE_P (type))
5408 switch (OMP_CLAUSE_REDUCTION_CODE (c))
5410 case PLUS_EXPR:
5411 case MULT_EXPR:
5412 case MINUS_EXPR:
5413 predefined = true;
5414 break;
5415 case MIN_EXPR:
5416 case MAX_EXPR:
5417 if (TREE_CODE (type) == COMPLEX_TYPE)
5418 break;
5419 predefined = true;
5420 break;
5421 case BIT_AND_EXPR:
5422 case BIT_IOR_EXPR:
5423 case BIT_XOR_EXPR:
5424 if (FLOAT_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
5425 break;
5426 predefined = true;
5427 break;
5428 case TRUTH_ANDIF_EXPR:
5429 case TRUTH_ORIF_EXPR:
5430 if (FLOAT_TYPE_P (type))
5431 break;
5432 predefined = true;
5433 break;
5434 default:
5435 break;
5437 else if (TYPE_READONLY (type))
5439 error ("%qE has const type for %<reduction%>",
5440 omp_clause_printable_decl (t));
5441 return true;
5443 else if (!processing_template_decl)
5445 t = require_complete_type (t);
5446 if (t == error_mark_node)
5447 return true;
5448 OMP_CLAUSE_DECL (c) = t;
5451 if (predefined)
5453 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = NULL_TREE;
5454 return false;
5456 else if (processing_template_decl)
5457 return false;
5459 tree id = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
5461 type = TYPE_MAIN_VARIANT (type);
5462 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = NULL_TREE;
5463 if (id == NULL_TREE)
5464 id = omp_reduction_id (OMP_CLAUSE_REDUCTION_CODE (c),
5465 NULL_TREE, NULL_TREE);
5466 id = omp_reduction_lookup (OMP_CLAUSE_LOCATION (c), id, type, NULL, NULL);
5467 if (id)
5469 if (id == error_mark_node)
5470 return true;
5471 id = OVL_CURRENT (id);
5472 mark_used (id);
5473 tree body = DECL_SAVED_TREE (id);
5474 if (!body)
5475 return true;
5476 if (TREE_CODE (body) == STATEMENT_LIST)
5478 tree_stmt_iterator tsi;
5479 tree placeholder = NULL_TREE, decl_placeholder = NULL_TREE;
5480 int i;
5481 tree stmts[7];
5482 tree atype = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (id)));
5483 atype = TREE_TYPE (atype);
5484 bool need_static_cast = !same_type_p (type, atype);
5485 memset (stmts, 0, sizeof stmts);
5486 for (i = 0, tsi = tsi_start (body);
5487 i < 7 && !tsi_end_p (tsi);
5488 i++, tsi_next (&tsi))
5489 stmts[i] = tsi_stmt (tsi);
5490 gcc_assert (tsi_end_p (tsi));
5492 if (i >= 3)
5494 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
5495 && TREE_CODE (stmts[1]) == DECL_EXPR);
5496 placeholder = build_lang_decl (VAR_DECL, NULL_TREE, type);
5497 DECL_ARTIFICIAL (placeholder) = 1;
5498 DECL_IGNORED_P (placeholder) = 1;
5499 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
5500 if (TREE_CODE (t) == MEM_REF)
5502 decl_placeholder = build_lang_decl (VAR_DECL, NULL_TREE,
5503 type);
5504 DECL_ARTIFICIAL (decl_placeholder) = 1;
5505 DECL_IGNORED_P (decl_placeholder) = 1;
5506 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c) = decl_placeholder;
5508 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[0])))
5509 cxx_mark_addressable (placeholder);
5510 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[1]))
5511 && TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c)))
5512 != REFERENCE_TYPE)
5513 cxx_mark_addressable (decl_placeholder ? decl_placeholder
5514 : OMP_CLAUSE_DECL (c));
5515 tree omp_out = placeholder;
5516 tree omp_in = decl_placeholder ? decl_placeholder
5517 : convert_from_reference (OMP_CLAUSE_DECL (c));
5518 if (need_static_cast)
5520 tree rtype = build_reference_type (atype);
5521 omp_out = build_static_cast (rtype, omp_out,
5522 tf_warning_or_error);
5523 omp_in = build_static_cast (rtype, omp_in,
5524 tf_warning_or_error);
5525 if (omp_out == error_mark_node || omp_in == error_mark_node)
5526 return true;
5527 omp_out = convert_from_reference (omp_out);
5528 omp_in = convert_from_reference (omp_in);
5530 OMP_CLAUSE_REDUCTION_MERGE (c)
5531 = clone_omp_udr (stmts[2], DECL_EXPR_DECL (stmts[0]),
5532 DECL_EXPR_DECL (stmts[1]), omp_in, omp_out);
5534 if (i >= 6)
5536 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
5537 && TREE_CODE (stmts[4]) == DECL_EXPR);
5538 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[3])))
5539 cxx_mark_addressable (decl_placeholder ? decl_placeholder
5540 : OMP_CLAUSE_DECL (c));
5541 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[4])))
5542 cxx_mark_addressable (placeholder);
5543 tree omp_priv = decl_placeholder ? decl_placeholder
5544 : convert_from_reference (OMP_CLAUSE_DECL (c));
5545 tree omp_orig = placeholder;
5546 if (need_static_cast)
5548 if (i == 7)
5550 error_at (OMP_CLAUSE_LOCATION (c),
5551 "user defined reduction with constructor "
5552 "initializer for base class %qT", atype);
5553 return true;
5555 tree rtype = build_reference_type (atype);
5556 omp_priv = build_static_cast (rtype, omp_priv,
5557 tf_warning_or_error);
5558 omp_orig = build_static_cast (rtype, omp_orig,
5559 tf_warning_or_error);
5560 if (omp_priv == error_mark_node
5561 || omp_orig == error_mark_node)
5562 return true;
5563 omp_priv = convert_from_reference (omp_priv);
5564 omp_orig = convert_from_reference (omp_orig);
5566 if (i == 6)
5567 *need_default_ctor = true;
5568 OMP_CLAUSE_REDUCTION_INIT (c)
5569 = clone_omp_udr (stmts[5], DECL_EXPR_DECL (stmts[4]),
5570 DECL_EXPR_DECL (stmts[3]),
5571 omp_priv, omp_orig);
5572 if (cp_walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
5573 find_omp_placeholder_r, placeholder, NULL))
5574 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
5576 else if (i >= 3)
5578 if (CLASS_TYPE_P (type) && !pod_type_p (type))
5579 *need_default_ctor = true;
5580 else
5582 tree init;
5583 tree v = decl_placeholder ? decl_placeholder
5584 : convert_from_reference (t);
5585 if (AGGREGATE_TYPE_P (TREE_TYPE (v)))
5586 init = build_constructor (TREE_TYPE (v), NULL);
5587 else
5588 init = fold_convert (TREE_TYPE (v), integer_zero_node);
5589 OMP_CLAUSE_REDUCTION_INIT (c)
5590 = build2 (INIT_EXPR, TREE_TYPE (v), v, init);
5595 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
5596 *need_dtor = true;
5597 else
5599 error ("user defined reduction not found for %qE",
5600 omp_clause_printable_decl (t));
5601 return true;
5603 if (TREE_CODE (OMP_CLAUSE_DECL (c)) == MEM_REF)
5604 gcc_assert (TYPE_SIZE_UNIT (type)
5605 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST);
5606 return false;
5609 /* Called from finish_struct_1. linear(this) or linear(this:step)
5610 clauses might not be finalized yet because the class has been incomplete
5611 when parsing #pragma omp declare simd methods. Fix those up now. */
5613 void
5614 finish_omp_declare_simd_methods (tree t)
5616 if (processing_template_decl)
5617 return;
5619 for (tree x = TYPE_METHODS (t); x; x = DECL_CHAIN (x))
5621 if (TREE_CODE (TREE_TYPE (x)) != METHOD_TYPE)
5622 continue;
5623 tree ods = lookup_attribute ("omp declare simd", DECL_ATTRIBUTES (x));
5624 if (!ods || !TREE_VALUE (ods))
5625 continue;
5626 for (tree c = TREE_VALUE (TREE_VALUE (ods)); c; c = OMP_CLAUSE_CHAIN (c))
5627 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
5628 && integer_zerop (OMP_CLAUSE_DECL (c))
5629 && OMP_CLAUSE_LINEAR_STEP (c)
5630 && TREE_CODE (TREE_TYPE (OMP_CLAUSE_LINEAR_STEP (c)))
5631 == POINTER_TYPE)
5633 tree s = OMP_CLAUSE_LINEAR_STEP (c);
5634 s = fold_convert_loc (OMP_CLAUSE_LOCATION (c), sizetype, s);
5635 s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MULT_EXPR,
5636 sizetype, s, TYPE_SIZE_UNIT (t));
5637 OMP_CLAUSE_LINEAR_STEP (c) = s;
5642 /* Adjust sink depend clause to take into account pointer offsets.
5644 Return TRUE if there was a problem processing the offset, and the
5645 whole clause should be removed. */
5647 static bool
5648 cp_finish_omp_clause_depend_sink (tree sink_clause)
5650 tree t = OMP_CLAUSE_DECL (sink_clause);
5651 gcc_assert (TREE_CODE (t) == TREE_LIST);
5653 /* Make sure we don't adjust things twice for templates. */
5654 if (processing_template_decl)
5655 return false;
5657 for (; t; t = TREE_CHAIN (t))
5659 tree decl = TREE_VALUE (t);
5660 if (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
5662 tree offset = TREE_PURPOSE (t);
5663 bool neg = wi::neg_p ((wide_int) offset);
5664 offset = fold_unary (ABS_EXPR, TREE_TYPE (offset), offset);
5665 decl = mark_rvalue_use (decl);
5666 decl = convert_from_reference (decl);
5667 tree t2 = pointer_int_sum (OMP_CLAUSE_LOCATION (sink_clause),
5668 neg ? MINUS_EXPR : PLUS_EXPR,
5669 decl, offset);
5670 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (sink_clause),
5671 MINUS_EXPR, sizetype,
5672 fold_convert (sizetype, t2),
5673 fold_convert (sizetype, decl));
5674 if (t2 == error_mark_node)
5675 return true;
5676 TREE_PURPOSE (t) = t2;
5679 return false;
5682 /* For all elements of CLAUSES, validate them vs OpenMP constraints.
5683 Remove any elements from the list that are invalid. */
5685 tree
5686 finish_omp_clauses (tree clauses, bool allow_fields, bool declare_simd)
5688 bitmap_head generic_head, firstprivate_head, lastprivate_head;
5689 bitmap_head aligned_head, map_head, map_field_head;
5690 tree c, t, *pc;
5691 tree safelen = NULL_TREE;
5692 bool branch_seen = false;
5693 bool copyprivate_seen = false;
5694 bool ordered_seen = false;
5696 bitmap_obstack_initialize (NULL);
5697 bitmap_initialize (&generic_head, &bitmap_default_obstack);
5698 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
5699 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
5700 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
5701 bitmap_initialize (&map_head, &bitmap_default_obstack);
5702 bitmap_initialize (&map_field_head, &bitmap_default_obstack);
5704 for (pc = &clauses, c = clauses; c ; c = *pc)
5706 bool remove = false;
5707 bool field_ok = false;
5709 switch (OMP_CLAUSE_CODE (c))
5711 case OMP_CLAUSE_SHARED:
5712 field_ok = allow_fields;
5713 goto check_dup_generic;
5714 case OMP_CLAUSE_PRIVATE:
5715 field_ok = allow_fields;
5716 goto check_dup_generic;
5717 case OMP_CLAUSE_REDUCTION:
5718 field_ok = allow_fields;
5719 t = OMP_CLAUSE_DECL (c);
5720 if (TREE_CODE (t) == TREE_LIST)
5722 if (handle_omp_array_sections (c, allow_fields))
5724 remove = true;
5725 break;
5727 if (TREE_CODE (t) == TREE_LIST)
5729 while (TREE_CODE (t) == TREE_LIST)
5730 t = TREE_CHAIN (t);
5732 else
5734 gcc_assert (TREE_CODE (t) == MEM_REF);
5735 t = TREE_OPERAND (t, 0);
5736 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
5737 t = TREE_OPERAND (t, 0);
5738 if (TREE_CODE (t) == ADDR_EXPR
5739 || TREE_CODE (t) == INDIRECT_REF)
5740 t = TREE_OPERAND (t, 0);
5742 tree n = omp_clause_decl_field (t);
5743 if (n)
5744 t = n;
5745 goto check_dup_generic_t;
5747 goto check_dup_generic;
5748 case OMP_CLAUSE_COPYPRIVATE:
5749 copyprivate_seen = true;
5750 field_ok = allow_fields;
5751 goto check_dup_generic;
5752 case OMP_CLAUSE_COPYIN:
5753 goto check_dup_generic;
5754 case OMP_CLAUSE_LINEAR:
5755 field_ok = allow_fields;
5756 t = OMP_CLAUSE_DECL (c);
5757 if (!declare_simd
5758 && OMP_CLAUSE_LINEAR_KIND (c) != OMP_CLAUSE_LINEAR_DEFAULT)
5760 error_at (OMP_CLAUSE_LOCATION (c),
5761 "modifier should not be specified in %<linear%> "
5762 "clause on %<simd%> or %<for%> constructs");
5763 OMP_CLAUSE_LINEAR_KIND (c) = OMP_CLAUSE_LINEAR_DEFAULT;
5765 if ((VAR_P (t) || TREE_CODE (t) == PARM_DECL)
5766 && !type_dependent_expression_p (t))
5768 tree type = TREE_TYPE (t);
5769 if ((OMP_CLAUSE_LINEAR_KIND (c) == OMP_CLAUSE_LINEAR_REF
5770 || OMP_CLAUSE_LINEAR_KIND (c) == OMP_CLAUSE_LINEAR_UVAL)
5771 && TREE_CODE (type) != REFERENCE_TYPE)
5773 error ("linear clause with %qs modifier applied to "
5774 "non-reference variable with %qT type",
5775 OMP_CLAUSE_LINEAR_KIND (c) == OMP_CLAUSE_LINEAR_REF
5776 ? "ref" : "uval", TREE_TYPE (t));
5777 remove = true;
5778 break;
5780 if (TREE_CODE (type) == REFERENCE_TYPE)
5781 type = TREE_TYPE (type);
5782 if (!INTEGRAL_TYPE_P (type)
5783 && TREE_CODE (type) != POINTER_TYPE)
5785 error ("linear clause applied to non-integral non-pointer "
5786 "variable with %qT type", TREE_TYPE (t));
5787 remove = true;
5788 break;
5791 t = OMP_CLAUSE_LINEAR_STEP (c);
5792 if (t == NULL_TREE)
5793 t = integer_one_node;
5794 if (t == error_mark_node)
5796 remove = true;
5797 break;
5799 else if (!type_dependent_expression_p (t)
5800 && !INTEGRAL_TYPE_P (TREE_TYPE (t))
5801 && (!declare_simd
5802 || TREE_CODE (t) != PARM_DECL
5803 || TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE
5804 || !INTEGRAL_TYPE_P (TREE_TYPE (TREE_TYPE (t)))))
5806 error ("linear step expression must be integral");
5807 remove = true;
5808 break;
5810 else
5812 t = mark_rvalue_use (t);
5813 if (declare_simd && TREE_CODE (t) == PARM_DECL)
5815 OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c) = 1;
5816 goto check_dup_generic;
5818 if (!processing_template_decl
5819 && (VAR_P (OMP_CLAUSE_DECL (c))
5820 || TREE_CODE (OMP_CLAUSE_DECL (c)) == PARM_DECL))
5822 if (declare_simd)
5824 t = maybe_constant_value (t);
5825 if (TREE_CODE (t) != INTEGER_CST)
5827 error_at (OMP_CLAUSE_LOCATION (c),
5828 "%<linear%> clause step %qE is neither "
5829 "constant nor a parameter", t);
5830 remove = true;
5831 break;
5834 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5835 tree type = TREE_TYPE (OMP_CLAUSE_DECL (c));
5836 if (TREE_CODE (type) == REFERENCE_TYPE)
5837 type = TREE_TYPE (type);
5838 if (OMP_CLAUSE_LINEAR_KIND (c) == OMP_CLAUSE_LINEAR_REF)
5840 type = build_pointer_type (type);
5841 tree d = fold_convert (type, OMP_CLAUSE_DECL (c));
5842 t = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
5843 d, t);
5844 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c),
5845 MINUS_EXPR, sizetype,
5846 fold_convert (sizetype, t),
5847 fold_convert (sizetype, d));
5848 if (t == error_mark_node)
5850 remove = true;
5851 break;
5854 else if (TREE_CODE (type) == POINTER_TYPE
5855 /* Can't multiply the step yet if *this
5856 is still incomplete type. */
5857 && (!declare_simd
5858 || TREE_CODE (OMP_CLAUSE_DECL (c)) != PARM_DECL
5859 || !DECL_ARTIFICIAL (OMP_CLAUSE_DECL (c))
5860 || DECL_NAME (OMP_CLAUSE_DECL (c))
5861 != this_identifier
5862 || !TYPE_BEING_DEFINED (TREE_TYPE (type))))
5864 tree d = convert_from_reference (OMP_CLAUSE_DECL (c));
5865 t = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
5866 d, t);
5867 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c),
5868 MINUS_EXPR, sizetype,
5869 fold_convert (sizetype, t),
5870 fold_convert (sizetype, d));
5871 if (t == error_mark_node)
5873 remove = true;
5874 break;
5877 else
5878 t = fold_convert (type, t);
5880 OMP_CLAUSE_LINEAR_STEP (c) = t;
5882 goto check_dup_generic;
5883 check_dup_generic:
5884 t = omp_clause_decl_field (OMP_CLAUSE_DECL (c));
5885 if (t)
5887 if (!remove && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_SHARED)
5888 omp_note_field_privatization (t, OMP_CLAUSE_DECL (c));
5890 else
5891 t = OMP_CLAUSE_DECL (c);
5892 check_dup_generic_t:
5893 if (t == current_class_ptr
5894 && (!declare_simd
5895 || (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_LINEAR
5896 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_UNIFORM)))
5898 error ("%<this%> allowed in OpenMP only in %<declare simd%>"
5899 " clauses");
5900 remove = true;
5901 break;
5903 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL
5904 && (!field_ok || TREE_CODE (t) != FIELD_DECL))
5906 if (processing_template_decl)
5907 break;
5908 if (DECL_P (t))
5909 error ("%qD is not a variable in clause %qs", t,
5910 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5911 else
5912 error ("%qE is not a variable in clause %qs", t,
5913 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5914 remove = true;
5916 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
5917 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
5918 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
5920 error ("%qD appears more than once in data clauses", t);
5921 remove = true;
5923 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
5924 && bitmap_bit_p (&map_head, DECL_UID (t)))
5926 error ("%qD appears both in data and map clauses", t);
5927 remove = true;
5929 else
5930 bitmap_set_bit (&generic_head, DECL_UID (t));
5931 if (!field_ok)
5932 break;
5933 handle_field_decl:
5934 if (!remove
5935 && TREE_CODE (t) == FIELD_DECL
5936 && t == OMP_CLAUSE_DECL (c))
5938 OMP_CLAUSE_DECL (c)
5939 = omp_privatize_field (t, (OMP_CLAUSE_CODE (c)
5940 == OMP_CLAUSE_SHARED));
5941 if (OMP_CLAUSE_DECL (c) == error_mark_node)
5942 remove = true;
5944 break;
5946 case OMP_CLAUSE_FIRSTPRIVATE:
5947 t = omp_clause_decl_field (OMP_CLAUSE_DECL (c));
5948 if (t)
5949 omp_note_field_privatization (t, OMP_CLAUSE_DECL (c));
5950 else
5951 t = OMP_CLAUSE_DECL (c);
5952 if (t == current_class_ptr)
5954 error ("%<this%> allowed in OpenMP only in %<declare simd%>"
5955 " clauses");
5956 remove = true;
5957 break;
5959 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL
5960 && (!allow_fields || TREE_CODE (t) != FIELD_DECL))
5962 if (processing_template_decl)
5963 break;
5964 if (DECL_P (t))
5965 error ("%qD is not a variable in clause %<firstprivate%>", t);
5966 else
5967 error ("%qE is not a variable in clause %<firstprivate%>", t);
5968 remove = true;
5970 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
5971 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
5973 error ("%qD appears more than once in data clauses", t);
5974 remove = true;
5976 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
5978 error ("%qD appears both in data and map clauses", t);
5979 remove = true;
5981 else
5982 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
5983 goto handle_field_decl;
5985 case OMP_CLAUSE_LASTPRIVATE:
5986 t = omp_clause_decl_field (OMP_CLAUSE_DECL (c));
5987 if (t)
5988 omp_note_field_privatization (t, OMP_CLAUSE_DECL (c));
5989 else
5990 t = OMP_CLAUSE_DECL (c);
5991 if (t == current_class_ptr)
5993 error ("%<this%> allowed in OpenMP only in %<declare simd%>"
5994 " clauses");
5995 remove = true;
5996 break;
5998 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL
5999 && (!allow_fields || TREE_CODE (t) != FIELD_DECL))
6001 if (processing_template_decl)
6002 break;
6003 if (DECL_P (t))
6004 error ("%qD is not a variable in clause %<lastprivate%>", t);
6005 else
6006 error ("%qE is not a variable in clause %<lastprivate%>", t);
6007 remove = true;
6009 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
6010 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
6012 error ("%qD appears more than once in data clauses", t);
6013 remove = true;
6015 else
6016 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
6017 goto handle_field_decl;
6019 case OMP_CLAUSE_IF:
6020 t = OMP_CLAUSE_IF_EXPR (c);
6021 t = maybe_convert_cond (t);
6022 if (t == error_mark_node)
6023 remove = true;
6024 else if (!processing_template_decl)
6025 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6026 OMP_CLAUSE_IF_EXPR (c) = t;
6027 break;
6029 case OMP_CLAUSE_FINAL:
6030 t = OMP_CLAUSE_FINAL_EXPR (c);
6031 t = maybe_convert_cond (t);
6032 if (t == error_mark_node)
6033 remove = true;
6034 else if (!processing_template_decl)
6035 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6036 OMP_CLAUSE_FINAL_EXPR (c) = t;
6037 break;
6039 case OMP_CLAUSE_GANG:
6040 /* Operand 1 is the gang static: argument. */
6041 t = OMP_CLAUSE_OPERAND (c, 1);
6042 if (t != NULL_TREE)
6044 if (t == error_mark_node)
6045 remove = true;
6046 else if (!type_dependent_expression_p (t)
6047 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6049 error ("%<gang%> static expression must be integral");
6050 remove = true;
6052 else
6054 t = mark_rvalue_use (t);
6055 if (!processing_template_decl)
6057 t = maybe_constant_value (t);
6058 if (TREE_CODE (t) == INTEGER_CST
6059 && tree_int_cst_sgn (t) != 1
6060 && t != integer_minus_one_node)
6062 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6063 "%<gang%> static value must be"
6064 "positive");
6065 t = integer_one_node;
6068 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6070 OMP_CLAUSE_OPERAND (c, 1) = t;
6072 /* Check operand 0, the num argument. */
6074 case OMP_CLAUSE_WORKER:
6075 case OMP_CLAUSE_VECTOR:
6076 if (OMP_CLAUSE_OPERAND (c, 0) == NULL_TREE)
6077 break;
6079 case OMP_CLAUSE_NUM_TASKS:
6080 case OMP_CLAUSE_NUM_TEAMS:
6081 case OMP_CLAUSE_NUM_THREADS:
6082 case OMP_CLAUSE_NUM_GANGS:
6083 case OMP_CLAUSE_NUM_WORKERS:
6084 case OMP_CLAUSE_VECTOR_LENGTH:
6085 t = OMP_CLAUSE_OPERAND (c, 0);
6086 if (t == error_mark_node)
6087 remove = true;
6088 else if (!type_dependent_expression_p (t)
6089 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6091 switch (OMP_CLAUSE_CODE (c))
6093 case OMP_CLAUSE_GANG:
6094 error_at (OMP_CLAUSE_LOCATION (c),
6095 "%<gang%> num expression must be integral"); break;
6096 case OMP_CLAUSE_VECTOR:
6097 error_at (OMP_CLAUSE_LOCATION (c),
6098 "%<vector%> length expression must be integral");
6099 break;
6100 case OMP_CLAUSE_WORKER:
6101 error_at (OMP_CLAUSE_LOCATION (c),
6102 "%<worker%> num expression must be integral");
6103 break;
6104 default:
6105 error_at (OMP_CLAUSE_LOCATION (c),
6106 "%qs expression must be integral",
6107 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6109 remove = true;
6111 else
6113 t = mark_rvalue_use (t);
6114 if (!processing_template_decl)
6116 t = maybe_constant_value (t);
6117 if (TREE_CODE (t) == INTEGER_CST
6118 && tree_int_cst_sgn (t) != 1)
6120 switch (OMP_CLAUSE_CODE (c))
6122 case OMP_CLAUSE_GANG:
6123 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6124 "%<gang%> num value must be positive");
6125 break;
6126 case OMP_CLAUSE_VECTOR:
6127 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6128 "%<vector%> length value must be"
6129 "positive");
6130 break;
6131 case OMP_CLAUSE_WORKER:
6132 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6133 "%<worker%> num value must be"
6134 "positive");
6135 break;
6136 default:
6137 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6138 "%qs value must be positive",
6139 omp_clause_code_name
6140 [OMP_CLAUSE_CODE (c)]);
6142 t = integer_one_node;
6144 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6146 OMP_CLAUSE_OPERAND (c, 0) = t;
6148 break;
6150 case OMP_CLAUSE_SCHEDULE:
6151 if (OMP_CLAUSE_SCHEDULE_KIND (c) & OMP_CLAUSE_SCHEDULE_NONMONOTONIC)
6153 const char *p = NULL;
6154 switch (OMP_CLAUSE_SCHEDULE_KIND (c) & OMP_CLAUSE_SCHEDULE_MASK)
6156 case OMP_CLAUSE_SCHEDULE_STATIC: p = "static"; break;
6157 case OMP_CLAUSE_SCHEDULE_DYNAMIC: break;
6158 case OMP_CLAUSE_SCHEDULE_GUIDED: break;
6159 case OMP_CLAUSE_SCHEDULE_AUTO: p = "auto"; break;
6160 case OMP_CLAUSE_SCHEDULE_RUNTIME: p = "runtime"; break;
6161 default: gcc_unreachable ();
6163 if (p)
6165 error_at (OMP_CLAUSE_LOCATION (c),
6166 "%<nonmonotonic%> modifier specified for %qs "
6167 "schedule kind", p);
6168 OMP_CLAUSE_SCHEDULE_KIND (c)
6169 = (enum omp_clause_schedule_kind)
6170 (OMP_CLAUSE_SCHEDULE_KIND (c)
6171 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
6175 t = OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c);
6176 if (t == NULL)
6178 else if (t == error_mark_node)
6179 remove = true;
6180 else if (!type_dependent_expression_p (t)
6181 && (OMP_CLAUSE_SCHEDULE_KIND (c)
6182 != OMP_CLAUSE_SCHEDULE_CILKFOR)
6183 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6185 error ("schedule chunk size expression must be integral");
6186 remove = true;
6188 else
6190 t = mark_rvalue_use (t);
6191 if (!processing_template_decl)
6193 if (OMP_CLAUSE_SCHEDULE_KIND (c)
6194 == OMP_CLAUSE_SCHEDULE_CILKFOR)
6196 t = convert_to_integer (long_integer_type_node, t);
6197 if (t == error_mark_node)
6199 remove = true;
6200 break;
6203 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6205 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
6207 break;
6209 case OMP_CLAUSE_SIMDLEN:
6210 case OMP_CLAUSE_SAFELEN:
6211 t = OMP_CLAUSE_OPERAND (c, 0);
6212 if (t == error_mark_node)
6213 remove = true;
6214 else if (!type_dependent_expression_p (t)
6215 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6217 error ("%qs length expression must be integral",
6218 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6219 remove = true;
6221 else
6223 t = mark_rvalue_use (t);
6224 t = maybe_constant_value (t);
6225 if (!processing_template_decl)
6227 if (TREE_CODE (t) != INTEGER_CST
6228 || tree_int_cst_sgn (t) != 1)
6230 error ("%qs length expression must be positive constant"
6231 " integer expression",
6232 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6233 remove = true;
6236 OMP_CLAUSE_OPERAND (c, 0) = t;
6237 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SAFELEN)
6238 safelen = c;
6240 break;
6242 case OMP_CLAUSE_ASYNC:
6243 t = OMP_CLAUSE_ASYNC_EXPR (c);
6244 if (t == error_mark_node)
6245 remove = true;
6246 else if (!type_dependent_expression_p (t)
6247 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6249 error ("%<async%> expression must be integral");
6250 remove = true;
6252 else
6254 t = mark_rvalue_use (t);
6255 if (!processing_template_decl)
6256 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6257 OMP_CLAUSE_ASYNC_EXPR (c) = t;
6259 break;
6261 case OMP_CLAUSE_WAIT:
6262 t = OMP_CLAUSE_WAIT_EXPR (c);
6263 if (t == error_mark_node)
6264 remove = true;
6265 else if (!processing_template_decl)
6266 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6267 OMP_CLAUSE_WAIT_EXPR (c) = t;
6268 break;
6270 case OMP_CLAUSE_THREAD_LIMIT:
6271 t = OMP_CLAUSE_THREAD_LIMIT_EXPR (c);
6272 if (t == error_mark_node)
6273 remove = true;
6274 else if (!type_dependent_expression_p (t)
6275 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6277 error ("%<thread_limit%> expression must be integral");
6278 remove = true;
6280 else
6282 t = mark_rvalue_use (t);
6283 if (!processing_template_decl)
6285 t = maybe_constant_value (t);
6286 if (TREE_CODE (t) == INTEGER_CST
6287 && tree_int_cst_sgn (t) != 1)
6289 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6290 "%<thread_limit%> value must be positive");
6291 t = integer_one_node;
6293 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6295 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
6297 break;
6299 case OMP_CLAUSE_DEVICE:
6300 t = OMP_CLAUSE_DEVICE_ID (c);
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 error ("%<device%> id must be integral");
6307 remove = true;
6309 else
6311 t = mark_rvalue_use (t);
6312 if (!processing_template_decl)
6313 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6314 OMP_CLAUSE_DEVICE_ID (c) = t;
6316 break;
6318 case OMP_CLAUSE_DIST_SCHEDULE:
6319 t = OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c);
6320 if (t == NULL)
6322 else if (t == error_mark_node)
6323 remove = true;
6324 else if (!type_dependent_expression_p (t)
6325 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6327 error ("%<dist_schedule%> chunk size expression must be "
6328 "integral");
6329 remove = true;
6331 else
6333 t = mark_rvalue_use (t);
6334 if (!processing_template_decl)
6335 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6336 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
6338 break;
6340 case OMP_CLAUSE_ALIGNED:
6341 t = OMP_CLAUSE_DECL (c);
6342 if (t == current_class_ptr && !declare_simd)
6344 error ("%<this%> allowed in OpenMP only in %<declare simd%>"
6345 " clauses");
6346 remove = true;
6347 break;
6349 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
6351 if (processing_template_decl)
6352 break;
6353 if (DECL_P (t))
6354 error ("%qD is not a variable in %<aligned%> clause", t);
6355 else
6356 error ("%qE is not a variable in %<aligned%> clause", t);
6357 remove = true;
6359 else if (!type_dependent_expression_p (t)
6360 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE
6361 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE
6362 && (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE
6363 || (!POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (t)))
6364 && (TREE_CODE (TREE_TYPE (TREE_TYPE (t)))
6365 != ARRAY_TYPE))))
6367 error_at (OMP_CLAUSE_LOCATION (c),
6368 "%qE in %<aligned%> clause is neither a pointer nor "
6369 "an array nor a reference to pointer or array", t);
6370 remove = true;
6372 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
6374 error ("%qD appears more than once in %<aligned%> clauses", t);
6375 remove = true;
6377 else
6378 bitmap_set_bit (&aligned_head, DECL_UID (t));
6379 t = OMP_CLAUSE_ALIGNED_ALIGNMENT (c);
6380 if (t == error_mark_node)
6381 remove = true;
6382 else if (t == NULL_TREE)
6383 break;
6384 else if (!type_dependent_expression_p (t)
6385 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6387 error ("%<aligned%> clause alignment expression must "
6388 "be integral");
6389 remove = true;
6391 else
6393 t = mark_rvalue_use (t);
6394 t = maybe_constant_value (t);
6395 if (!processing_template_decl)
6397 if (TREE_CODE (t) != INTEGER_CST
6398 || tree_int_cst_sgn (t) != 1)
6400 error ("%<aligned%> clause alignment expression must be "
6401 "positive constant integer expression");
6402 remove = true;
6405 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = t;
6407 break;
6409 case OMP_CLAUSE_DEPEND:
6410 t = OMP_CLAUSE_DECL (c);
6411 if (t == NULL_TREE)
6413 gcc_assert (OMP_CLAUSE_DEPEND_KIND (c)
6414 == OMP_CLAUSE_DEPEND_SOURCE);
6415 break;
6417 if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SINK)
6419 if (cp_finish_omp_clause_depend_sink (c))
6420 remove = true;
6421 break;
6423 if (TREE_CODE (t) == TREE_LIST)
6425 if (handle_omp_array_sections (c, allow_fields))
6426 remove = true;
6427 break;
6429 if (t == error_mark_node)
6430 remove = true;
6431 else if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
6433 if (processing_template_decl)
6434 break;
6435 if (DECL_P (t))
6436 error ("%qD is not a variable in %<depend%> clause", t);
6437 else
6438 error ("%qE is not a variable in %<depend%> clause", t);
6439 remove = true;
6441 else if (t == current_class_ptr)
6443 error ("%<this%> allowed in OpenMP only in %<declare simd%>"
6444 " clauses");
6445 remove = true;
6447 else if (!processing_template_decl
6448 && !cxx_mark_addressable (t))
6449 remove = true;
6450 break;
6452 case OMP_CLAUSE_MAP:
6453 case OMP_CLAUSE_TO:
6454 case OMP_CLAUSE_FROM:
6455 case OMP_CLAUSE__CACHE_:
6456 t = OMP_CLAUSE_DECL (c);
6457 if (TREE_CODE (t) == TREE_LIST)
6459 if (handle_omp_array_sections (c, allow_fields))
6460 remove = true;
6461 else
6463 t = OMP_CLAUSE_DECL (c);
6464 if (TREE_CODE (t) != TREE_LIST
6465 && !type_dependent_expression_p (t)
6466 && !cp_omp_mappable_type (TREE_TYPE (t)))
6468 error_at (OMP_CLAUSE_LOCATION (c),
6469 "array section does not have mappable type "
6470 "in %qs clause",
6471 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6472 remove = true;
6474 while (TREE_CODE (t) == ARRAY_REF)
6475 t = TREE_OPERAND (t, 0);
6476 if (TREE_CODE (t) == COMPONENT_REF
6477 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
6479 while (TREE_CODE (t) == COMPONENT_REF)
6480 t = TREE_OPERAND (t, 0);
6481 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
6482 break;
6483 if (bitmap_bit_p (&map_head, DECL_UID (t)))
6485 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
6486 error ("%qD appears more than once in motion"
6487 " clauses", t);
6488 else
6489 error ("%qD appears more than once in map"
6490 " clauses", t);
6491 remove = true;
6493 else
6495 bitmap_set_bit (&map_head, DECL_UID (t));
6496 bitmap_set_bit (&map_field_head, DECL_UID (t));
6500 break;
6502 if (t == error_mark_node)
6504 remove = true;
6505 break;
6507 if (REFERENCE_REF_P (t)
6508 && TREE_CODE (TREE_OPERAND (t, 0)) == COMPONENT_REF)
6510 t = TREE_OPERAND (t, 0);
6511 OMP_CLAUSE_DECL (c) = t;
6513 if (TREE_CODE (t) == COMPONENT_REF
6514 && allow_fields
6515 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE__CACHE_)
6517 if (type_dependent_expression_p (t))
6518 break;
6519 if (DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
6521 error_at (OMP_CLAUSE_LOCATION (c),
6522 "bit-field %qE in %qs clause",
6523 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6524 remove = true;
6526 else if (!cp_omp_mappable_type (TREE_TYPE (t)))
6528 error_at (OMP_CLAUSE_LOCATION (c),
6529 "%qE does not have a mappable type in %qs clause",
6530 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6531 remove = true;
6533 while (TREE_CODE (t) == COMPONENT_REF)
6535 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0)))
6536 == UNION_TYPE)
6538 error_at (OMP_CLAUSE_LOCATION (c),
6539 "%qE is a member of a union", t);
6540 remove = true;
6541 break;
6543 t = TREE_OPERAND (t, 0);
6545 if (remove)
6546 break;
6547 if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
6549 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
6550 goto handle_map_references;
6553 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
6555 if (processing_template_decl)
6556 break;
6557 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
6558 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
6559 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ALWAYS_POINTER))
6560 break;
6561 if (DECL_P (t))
6562 error ("%qD is not a variable in %qs clause", t,
6563 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6564 else
6565 error ("%qE is not a variable in %qs clause", t,
6566 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6567 remove = true;
6569 else if (VAR_P (t) && CP_DECL_THREAD_LOCAL_P (t))
6571 error ("%qD is threadprivate variable in %qs clause", t,
6572 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6573 remove = true;
6575 else if (t == current_class_ptr)
6577 error ("%<this%> allowed in OpenMP only in %<declare simd%>"
6578 " clauses");
6579 remove = true;
6580 break;
6582 else if (!processing_template_decl
6583 && TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE
6584 && (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
6585 || (OMP_CLAUSE_MAP_KIND (c)
6586 != GOMP_MAP_FIRSTPRIVATE_POINTER))
6587 && !cxx_mark_addressable (t))
6588 remove = true;
6589 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
6590 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
6591 || (OMP_CLAUSE_MAP_KIND (c)
6592 == GOMP_MAP_FIRSTPRIVATE_POINTER)))
6593 && t == OMP_CLAUSE_DECL (c)
6594 && !type_dependent_expression_p (t)
6595 && !cp_omp_mappable_type ((TREE_CODE (TREE_TYPE (t))
6596 == REFERENCE_TYPE)
6597 ? TREE_TYPE (TREE_TYPE (t))
6598 : TREE_TYPE (t)))
6600 error_at (OMP_CLAUSE_LOCATION (c),
6601 "%qD does not have a mappable type in %qs clause", t,
6602 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6603 remove = true;
6605 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
6606 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
6608 if (bitmap_bit_p (&generic_head, DECL_UID (t))
6609 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
6611 error ("%qD appears more than once in data clauses", t);
6612 remove = true;
6614 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
6616 error ("%qD appears both in data and map clauses", t);
6617 remove = true;
6619 else
6620 bitmap_set_bit (&generic_head, DECL_UID (t));
6622 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
6624 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
6625 error ("%qD appears more than once in motion clauses", t);
6626 else
6627 error ("%qD appears more than once in map clauses", t);
6628 remove = true;
6630 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
6631 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
6633 error ("%qD appears both in data and map clauses", t);
6634 remove = true;
6636 else
6638 bitmap_set_bit (&map_head, DECL_UID (t));
6639 if (t != OMP_CLAUSE_DECL (c)
6640 && TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPONENT_REF)
6641 bitmap_set_bit (&map_field_head, DECL_UID (t));
6643 handle_map_references:
6644 if (!remove
6645 && !processing_template_decl
6646 && allow_fields
6647 && TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c))) == REFERENCE_TYPE)
6649 t = OMP_CLAUSE_DECL (c);
6650 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
6652 OMP_CLAUSE_DECL (c) = build_simple_mem_ref (t);
6653 if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
6654 OMP_CLAUSE_SIZE (c)
6655 = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t)));
6657 else if (OMP_CLAUSE_MAP_KIND (c)
6658 != GOMP_MAP_FIRSTPRIVATE_POINTER
6659 && (OMP_CLAUSE_MAP_KIND (c)
6660 != GOMP_MAP_FIRSTPRIVATE_REFERENCE)
6661 && (OMP_CLAUSE_MAP_KIND (c)
6662 != GOMP_MAP_ALWAYS_POINTER))
6664 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
6665 OMP_CLAUSE_MAP);
6666 if (TREE_CODE (t) == COMPONENT_REF)
6667 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ALWAYS_POINTER);
6668 else
6669 OMP_CLAUSE_SET_MAP_KIND (c2,
6670 GOMP_MAP_FIRSTPRIVATE_REFERENCE);
6671 OMP_CLAUSE_DECL (c2) = t;
6672 OMP_CLAUSE_SIZE (c2) = size_zero_node;
6673 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
6674 OMP_CLAUSE_CHAIN (c) = c2;
6675 OMP_CLAUSE_DECL (c) = build_simple_mem_ref (t);
6676 if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
6677 OMP_CLAUSE_SIZE (c)
6678 = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t)));
6679 c = c2;
6682 break;
6684 case OMP_CLAUSE_TO_DECLARE:
6685 case OMP_CLAUSE_LINK:
6686 t = OMP_CLAUSE_DECL (c);
6687 if (TREE_CODE (t) == FUNCTION_DECL
6688 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
6690 else if (!VAR_P (t))
6692 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
6694 if (TREE_CODE (t) == OVERLOAD && OVL_CHAIN (t))
6695 error_at (OMP_CLAUSE_LOCATION (c),
6696 "overloaded function name %qE in clause %qs", t,
6697 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6698 else if (TREE_CODE (t) == TEMPLATE_ID_EXPR)
6699 error_at (OMP_CLAUSE_LOCATION (c),
6700 "template %qE in clause %qs", t,
6701 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6702 else
6703 error_at (OMP_CLAUSE_LOCATION (c),
6704 "%qE is neither a variable nor a function name "
6705 "in clause %qs", t,
6706 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6708 else
6709 error_at (OMP_CLAUSE_LOCATION (c),
6710 "%qE is not a variable in clause %qs", t,
6711 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6712 remove = true;
6714 else if (DECL_THREAD_LOCAL_P (t))
6716 error_at (OMP_CLAUSE_LOCATION (c),
6717 "%qD is threadprivate variable in %qs clause", t,
6718 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6719 remove = true;
6721 else if (!cp_omp_mappable_type (TREE_TYPE (t)))
6723 error_at (OMP_CLAUSE_LOCATION (c),
6724 "%qD does not have a mappable type in %qs clause", t,
6725 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6726 remove = true;
6728 if (remove)
6729 break;
6730 if (bitmap_bit_p (&generic_head, DECL_UID (t)))
6732 error_at (OMP_CLAUSE_LOCATION (c),
6733 "%qE appears more than once on the same "
6734 "%<declare target%> directive", t);
6735 remove = true;
6737 else
6738 bitmap_set_bit (&generic_head, DECL_UID (t));
6739 break;
6741 case OMP_CLAUSE_UNIFORM:
6742 t = OMP_CLAUSE_DECL (c);
6743 if (TREE_CODE (t) != PARM_DECL)
6745 if (processing_template_decl)
6746 break;
6747 if (DECL_P (t))
6748 error ("%qD is not an argument in %<uniform%> clause", t);
6749 else
6750 error ("%qE is not an argument in %<uniform%> clause", t);
6751 remove = true;
6752 break;
6754 /* map_head bitmap is used as uniform_head if declare_simd. */
6755 bitmap_set_bit (&map_head, DECL_UID (t));
6756 goto check_dup_generic;
6758 case OMP_CLAUSE_GRAINSIZE:
6759 t = OMP_CLAUSE_GRAINSIZE_EXPR (c);
6760 if (t == error_mark_node)
6761 remove = true;
6762 else if (!type_dependent_expression_p (t)
6763 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6765 error ("%<grainsize%> expression must be integral");
6766 remove = true;
6768 else
6770 t = mark_rvalue_use (t);
6771 if (!processing_template_decl)
6773 t = maybe_constant_value (t);
6774 if (TREE_CODE (t) == INTEGER_CST
6775 && tree_int_cst_sgn (t) != 1)
6777 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6778 "%<grainsize%> value must be positive");
6779 t = integer_one_node;
6781 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6783 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
6785 break;
6787 case OMP_CLAUSE_PRIORITY:
6788 t = OMP_CLAUSE_PRIORITY_EXPR (c);
6789 if (t == error_mark_node)
6790 remove = true;
6791 else if (!type_dependent_expression_p (t)
6792 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6794 error ("%<priority%> expression must be integral");
6795 remove = true;
6797 else
6799 t = mark_rvalue_use (t);
6800 if (!processing_template_decl)
6802 t = maybe_constant_value (t);
6803 if (TREE_CODE (t) == INTEGER_CST
6804 && tree_int_cst_sgn (t) == -1)
6806 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6807 "%<priority%> value must be non-negative");
6808 t = integer_one_node;
6810 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6812 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
6814 break;
6816 case OMP_CLAUSE_HINT:
6817 t = OMP_CLAUSE_HINT_EXPR (c);
6818 if (t == error_mark_node)
6819 remove = true;
6820 else if (!type_dependent_expression_p (t)
6821 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6823 error ("%<num_tasks%> expression must be integral");
6824 remove = true;
6826 else
6828 t = mark_rvalue_use (t);
6829 if (!processing_template_decl)
6831 t = maybe_constant_value (t);
6832 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6834 OMP_CLAUSE_HINT_EXPR (c) = t;
6836 break;
6838 case OMP_CLAUSE_USE_DEVICE:
6839 case OMP_CLAUSE_IS_DEVICE_PTR:
6840 case OMP_CLAUSE_USE_DEVICE_PTR:
6841 field_ok = allow_fields;
6842 t = OMP_CLAUSE_DECL (c);
6843 if (!type_dependent_expression_p (t))
6845 tree type = TREE_TYPE (t);
6846 if (TREE_CODE (type) != POINTER_TYPE
6847 && TREE_CODE (type) != ARRAY_TYPE
6848 && (TREE_CODE (type) != REFERENCE_TYPE
6849 || (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
6850 && TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE)))
6852 error_at (OMP_CLAUSE_LOCATION (c),
6853 "%qs variable is neither a pointer, nor an array"
6854 "nor reference to pointer or array",
6855 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6856 remove = true;
6859 goto check_dup_generic;
6861 case OMP_CLAUSE_NOWAIT:
6862 case OMP_CLAUSE_DEFAULT:
6863 case OMP_CLAUSE_UNTIED:
6864 case OMP_CLAUSE_COLLAPSE:
6865 case OMP_CLAUSE_MERGEABLE:
6866 case OMP_CLAUSE_PARALLEL:
6867 case OMP_CLAUSE_FOR:
6868 case OMP_CLAUSE_SECTIONS:
6869 case OMP_CLAUSE_TASKGROUP:
6870 case OMP_CLAUSE_PROC_BIND:
6871 case OMP_CLAUSE_NOGROUP:
6872 case OMP_CLAUSE_THREADS:
6873 case OMP_CLAUSE_SIMD:
6874 case OMP_CLAUSE_DEFAULTMAP:
6875 case OMP_CLAUSE__CILK_FOR_COUNT_:
6876 case OMP_CLAUSE_AUTO:
6877 case OMP_CLAUSE_INDEPENDENT:
6878 case OMP_CLAUSE_SEQ:
6879 break;
6881 case OMP_CLAUSE_TILE:
6882 for (tree list = OMP_CLAUSE_TILE_LIST (c); !remove && list;
6883 list = TREE_CHAIN (list))
6885 t = TREE_VALUE (list);
6887 if (t == error_mark_node)
6888 remove = true;
6889 else if (!type_dependent_expression_p (t)
6890 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6892 error ("%<tile%> value must be integral");
6893 remove = true;
6895 else
6897 t = mark_rvalue_use (t);
6898 if (!processing_template_decl)
6900 t = maybe_constant_value (t);
6901 if (TREE_CODE (t) == INTEGER_CST
6902 && tree_int_cst_sgn (t) != 1
6903 && t != integer_minus_one_node)
6905 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6906 "%<tile%> value must be positive");
6907 t = integer_one_node;
6910 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6913 /* Update list item. */
6914 TREE_VALUE (list) = t;
6916 break;
6918 case OMP_CLAUSE_ORDERED:
6919 ordered_seen = true;
6920 break;
6922 case OMP_CLAUSE_INBRANCH:
6923 case OMP_CLAUSE_NOTINBRANCH:
6924 if (branch_seen)
6926 error ("%<inbranch%> clause is incompatible with "
6927 "%<notinbranch%>");
6928 remove = true;
6930 branch_seen = true;
6931 break;
6933 default:
6934 gcc_unreachable ();
6937 if (remove)
6938 *pc = OMP_CLAUSE_CHAIN (c);
6939 else
6940 pc = &OMP_CLAUSE_CHAIN (c);
6943 for (pc = &clauses, c = clauses; c ; c = *pc)
6945 enum omp_clause_code c_kind = OMP_CLAUSE_CODE (c);
6946 bool remove = false;
6947 bool need_complete_type = false;
6948 bool need_default_ctor = false;
6949 bool need_copy_ctor = false;
6950 bool need_copy_assignment = false;
6951 bool need_implicitly_determined = false;
6952 bool need_dtor = false;
6953 tree type, inner_type;
6955 switch (c_kind)
6957 case OMP_CLAUSE_SHARED:
6958 need_implicitly_determined = true;
6959 break;
6960 case OMP_CLAUSE_PRIVATE:
6961 need_complete_type = true;
6962 need_default_ctor = true;
6963 need_dtor = true;
6964 need_implicitly_determined = true;
6965 break;
6966 case OMP_CLAUSE_FIRSTPRIVATE:
6967 need_complete_type = true;
6968 need_copy_ctor = true;
6969 need_dtor = true;
6970 need_implicitly_determined = true;
6971 break;
6972 case OMP_CLAUSE_LASTPRIVATE:
6973 need_complete_type = true;
6974 need_copy_assignment = true;
6975 need_implicitly_determined = true;
6976 break;
6977 case OMP_CLAUSE_REDUCTION:
6978 need_implicitly_determined = true;
6979 break;
6980 case OMP_CLAUSE_LINEAR:
6981 if (!declare_simd)
6982 need_implicitly_determined = true;
6983 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c)
6984 && !bitmap_bit_p (&map_head,
6985 DECL_UID (OMP_CLAUSE_LINEAR_STEP (c))))
6987 error_at (OMP_CLAUSE_LOCATION (c),
6988 "%<linear%> clause step is a parameter %qD not "
6989 "specified in %<uniform%> clause",
6990 OMP_CLAUSE_LINEAR_STEP (c));
6991 *pc = OMP_CLAUSE_CHAIN (c);
6992 continue;
6994 break;
6995 case OMP_CLAUSE_COPYPRIVATE:
6996 need_copy_assignment = true;
6997 break;
6998 case OMP_CLAUSE_COPYIN:
6999 need_copy_assignment = true;
7000 break;
7001 case OMP_CLAUSE_SIMDLEN:
7002 if (safelen
7003 && !processing_template_decl
7004 && tree_int_cst_lt (OMP_CLAUSE_SAFELEN_EXPR (safelen),
7005 OMP_CLAUSE_SIMDLEN_EXPR (c)))
7007 error_at (OMP_CLAUSE_LOCATION (c),
7008 "%<simdlen%> clause value is bigger than "
7009 "%<safelen%> clause value");
7010 OMP_CLAUSE_SIMDLEN_EXPR (c)
7011 = OMP_CLAUSE_SAFELEN_EXPR (safelen);
7013 pc = &OMP_CLAUSE_CHAIN (c);
7014 continue;
7015 case OMP_CLAUSE_SCHEDULE:
7016 if (ordered_seen
7017 && (OMP_CLAUSE_SCHEDULE_KIND (c)
7018 & OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
7020 error_at (OMP_CLAUSE_LOCATION (c),
7021 "%<nonmonotonic%> schedule modifier specified "
7022 "together with %<ordered%> clause");
7023 OMP_CLAUSE_SCHEDULE_KIND (c)
7024 = (enum omp_clause_schedule_kind)
7025 (OMP_CLAUSE_SCHEDULE_KIND (c)
7026 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
7028 pc = &OMP_CLAUSE_CHAIN (c);
7029 continue;
7030 case OMP_CLAUSE_NOWAIT:
7031 if (copyprivate_seen)
7033 error_at (OMP_CLAUSE_LOCATION (c),
7034 "%<nowait%> clause must not be used together "
7035 "with %<copyprivate%>");
7036 *pc = OMP_CLAUSE_CHAIN (c);
7037 continue;
7039 /* FALLTHRU */
7040 default:
7041 pc = &OMP_CLAUSE_CHAIN (c);
7042 continue;
7045 t = OMP_CLAUSE_DECL (c);
7046 if (processing_template_decl
7047 && !VAR_P (t) && TREE_CODE (t) != PARM_DECL)
7049 pc = &OMP_CLAUSE_CHAIN (c);
7050 continue;
7053 switch (c_kind)
7055 case OMP_CLAUSE_LASTPRIVATE:
7056 if (!bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
7058 need_default_ctor = true;
7059 need_dtor = true;
7061 break;
7063 case OMP_CLAUSE_REDUCTION:
7064 if (finish_omp_reduction_clause (c, &need_default_ctor,
7065 &need_dtor))
7066 remove = true;
7067 else
7068 t = OMP_CLAUSE_DECL (c);
7069 break;
7071 case OMP_CLAUSE_COPYIN:
7072 if (!VAR_P (t) || !CP_DECL_THREAD_LOCAL_P (t))
7074 error ("%qE must be %<threadprivate%> for %<copyin%>", t);
7075 remove = true;
7077 break;
7079 default:
7080 break;
7083 if (need_complete_type || need_copy_assignment)
7085 t = require_complete_type (t);
7086 if (t == error_mark_node)
7087 remove = true;
7088 else if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE
7089 && !complete_type_or_else (TREE_TYPE (TREE_TYPE (t)), t))
7090 remove = true;
7092 if (need_implicitly_determined)
7094 const char *share_name = NULL;
7096 if (VAR_P (t) && CP_DECL_THREAD_LOCAL_P (t))
7097 share_name = "threadprivate";
7098 else switch (cxx_omp_predetermined_sharing (t))
7100 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
7101 break;
7102 case OMP_CLAUSE_DEFAULT_SHARED:
7103 /* const vars may be specified in firstprivate clause. */
7104 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
7105 && cxx_omp_const_qual_no_mutable (t))
7106 break;
7107 share_name = "shared";
7108 break;
7109 case OMP_CLAUSE_DEFAULT_PRIVATE:
7110 share_name = "private";
7111 break;
7112 default:
7113 gcc_unreachable ();
7115 if (share_name)
7117 error ("%qE is predetermined %qs for %qs",
7118 omp_clause_printable_decl (t), share_name,
7119 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7120 remove = true;
7124 /* We're interested in the base element, not arrays. */
7125 inner_type = type = TREE_TYPE (t);
7126 if ((need_complete_type
7127 || need_copy_assignment
7128 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
7129 && TREE_CODE (inner_type) == REFERENCE_TYPE)
7130 inner_type = TREE_TYPE (inner_type);
7131 while (TREE_CODE (inner_type) == ARRAY_TYPE)
7132 inner_type = TREE_TYPE (inner_type);
7134 /* Check for special function availability by building a call to one.
7135 Save the results, because later we won't be in the right context
7136 for making these queries. */
7137 if (CLASS_TYPE_P (inner_type)
7138 && COMPLETE_TYPE_P (inner_type)
7139 && (need_default_ctor || need_copy_ctor
7140 || need_copy_assignment || need_dtor)
7141 && !type_dependent_expression_p (t)
7142 && cxx_omp_create_clause_info (c, inner_type, need_default_ctor,
7143 need_copy_ctor, need_copy_assignment,
7144 need_dtor))
7145 remove = true;
7147 if (!remove
7148 && c_kind == OMP_CLAUSE_SHARED
7149 && processing_template_decl)
7151 t = omp_clause_decl_field (OMP_CLAUSE_DECL (c));
7152 if (t)
7153 OMP_CLAUSE_DECL (c) = t;
7156 if (remove)
7157 *pc = OMP_CLAUSE_CHAIN (c);
7158 else
7159 pc = &OMP_CLAUSE_CHAIN (c);
7162 bitmap_obstack_release (NULL);
7163 return clauses;
7166 /* Start processing OpenMP clauses that can include any
7167 privatization clauses for non-static data members. */
7169 tree
7170 push_omp_privatization_clauses (bool ignore_next)
7172 if (omp_private_member_ignore_next)
7174 omp_private_member_ignore_next = ignore_next;
7175 return NULL_TREE;
7177 omp_private_member_ignore_next = ignore_next;
7178 if (omp_private_member_map)
7179 omp_private_member_vec.safe_push (error_mark_node);
7180 return push_stmt_list ();
7183 /* Revert remapping of any non-static data members since
7184 the last push_omp_privatization_clauses () call. */
7186 void
7187 pop_omp_privatization_clauses (tree stmt)
7189 if (stmt == NULL_TREE)
7190 return;
7191 stmt = pop_stmt_list (stmt);
7192 if (omp_private_member_map)
7194 while (!omp_private_member_vec.is_empty ())
7196 tree t = omp_private_member_vec.pop ();
7197 if (t == error_mark_node)
7199 add_stmt (stmt);
7200 return;
7202 bool no_decl_expr = t == integer_zero_node;
7203 if (no_decl_expr)
7204 t = omp_private_member_vec.pop ();
7205 tree *v = omp_private_member_map->get (t);
7206 gcc_assert (v);
7207 if (!no_decl_expr)
7208 add_decl_expr (*v);
7209 omp_private_member_map->remove (t);
7211 delete omp_private_member_map;
7212 omp_private_member_map = NULL;
7214 add_stmt (stmt);
7217 /* Remember OpenMP privatization clauses mapping and clear it.
7218 Used for lambdas. */
7220 void
7221 save_omp_privatization_clauses (vec<tree> &save)
7223 save = vNULL;
7224 if (omp_private_member_ignore_next)
7225 save.safe_push (integer_one_node);
7226 omp_private_member_ignore_next = false;
7227 if (!omp_private_member_map)
7228 return;
7230 while (!omp_private_member_vec.is_empty ())
7232 tree t = omp_private_member_vec.pop ();
7233 if (t == error_mark_node)
7235 save.safe_push (t);
7236 continue;
7238 tree n = t;
7239 if (t == integer_zero_node)
7240 t = omp_private_member_vec.pop ();
7241 tree *v = omp_private_member_map->get (t);
7242 gcc_assert (v);
7243 save.safe_push (*v);
7244 save.safe_push (t);
7245 if (n != t)
7246 save.safe_push (n);
7248 delete omp_private_member_map;
7249 omp_private_member_map = NULL;
7252 /* Restore OpenMP privatization clauses mapping saved by the
7253 above function. */
7255 void
7256 restore_omp_privatization_clauses (vec<tree> &save)
7258 gcc_assert (omp_private_member_vec.is_empty ());
7259 omp_private_member_ignore_next = false;
7260 if (save.is_empty ())
7261 return;
7262 if (save.length () == 1 && save[0] == integer_one_node)
7264 omp_private_member_ignore_next = true;
7265 save.release ();
7266 return;
7269 omp_private_member_map = new hash_map <tree, tree>;
7270 while (!save.is_empty ())
7272 tree t = save.pop ();
7273 tree n = t;
7274 if (t != error_mark_node)
7276 if (t == integer_one_node)
7278 omp_private_member_ignore_next = true;
7279 gcc_assert (save.is_empty ());
7280 break;
7282 if (t == integer_zero_node)
7283 t = save.pop ();
7284 tree &v = omp_private_member_map->get_or_insert (t);
7285 v = save.pop ();
7287 omp_private_member_vec.safe_push (t);
7288 if (n != t)
7289 omp_private_member_vec.safe_push (n);
7291 save.release ();
7294 /* For all variables in the tree_list VARS, mark them as thread local. */
7296 void
7297 finish_omp_threadprivate (tree vars)
7299 tree t;
7301 /* Mark every variable in VARS to be assigned thread local storage. */
7302 for (t = vars; t; t = TREE_CHAIN (t))
7304 tree v = TREE_PURPOSE (t);
7306 if (error_operand_p (v))
7308 else if (!VAR_P (v))
7309 error ("%<threadprivate%> %qD is not file, namespace "
7310 "or block scope variable", v);
7311 /* If V had already been marked threadprivate, it doesn't matter
7312 whether it had been used prior to this point. */
7313 else if (TREE_USED (v)
7314 && (DECL_LANG_SPECIFIC (v) == NULL
7315 || !CP_DECL_THREADPRIVATE_P (v)))
7316 error ("%qE declared %<threadprivate%> after first use", v);
7317 else if (! TREE_STATIC (v) && ! DECL_EXTERNAL (v))
7318 error ("automatic variable %qE cannot be %<threadprivate%>", v);
7319 else if (! COMPLETE_TYPE_P (complete_type (TREE_TYPE (v))))
7320 error ("%<threadprivate%> %qE has incomplete type", v);
7321 else if (TREE_STATIC (v) && TYPE_P (CP_DECL_CONTEXT (v))
7322 && CP_DECL_CONTEXT (v) != current_class_type)
7323 error ("%<threadprivate%> %qE directive not "
7324 "in %qT definition", v, CP_DECL_CONTEXT (v));
7325 else
7327 /* Allocate a LANG_SPECIFIC structure for V, if needed. */
7328 if (DECL_LANG_SPECIFIC (v) == NULL)
7330 retrofit_lang_decl (v);
7332 /* Make sure that DECL_DISCRIMINATOR_P continues to be true
7333 after the allocation of the lang_decl structure. */
7334 if (DECL_DISCRIMINATOR_P (v))
7335 DECL_LANG_SPECIFIC (v)->u.base.u2sel = 1;
7338 if (! CP_DECL_THREAD_LOCAL_P (v))
7340 CP_DECL_THREAD_LOCAL_P (v) = true;
7341 set_decl_tls_model (v, decl_default_tls_model (v));
7342 /* If rtl has been already set for this var, call
7343 make_decl_rtl once again, so that encode_section_info
7344 has a chance to look at the new decl flags. */
7345 if (DECL_RTL_SET_P (v))
7346 make_decl_rtl (v);
7348 CP_DECL_THREADPRIVATE_P (v) = 1;
7353 /* Build an OpenMP structured block. */
7355 tree
7356 begin_omp_structured_block (void)
7358 return do_pushlevel (sk_omp);
7361 tree
7362 finish_omp_structured_block (tree block)
7364 return do_poplevel (block);
7367 /* Similarly, except force the retention of the BLOCK. */
7369 tree
7370 begin_omp_parallel (void)
7372 keep_next_level (true);
7373 return begin_omp_structured_block ();
7376 /* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
7377 statement. */
7379 tree
7380 finish_oacc_data (tree clauses, tree block)
7382 tree stmt;
7384 block = finish_omp_structured_block (block);
7386 stmt = make_node (OACC_DATA);
7387 TREE_TYPE (stmt) = void_type_node;
7388 OACC_DATA_CLAUSES (stmt) = clauses;
7389 OACC_DATA_BODY (stmt) = block;
7391 return add_stmt (stmt);
7394 /* Generate OACC_HOST_DATA, with CLAUSES and BLOCK as its compound
7395 statement. */
7397 tree
7398 finish_oacc_host_data (tree clauses, tree block)
7400 tree stmt;
7402 block = finish_omp_structured_block (block);
7404 stmt = make_node (OACC_HOST_DATA);
7405 TREE_TYPE (stmt) = void_type_node;
7406 OACC_HOST_DATA_CLAUSES (stmt) = clauses;
7407 OACC_HOST_DATA_BODY (stmt) = block;
7409 return add_stmt (stmt);
7412 /* Generate OMP construct CODE, with BODY and CLAUSES as its compound
7413 statement. */
7415 tree
7416 finish_omp_construct (enum tree_code code, tree body, tree clauses)
7418 body = finish_omp_structured_block (body);
7420 tree stmt = make_node (code);
7421 TREE_TYPE (stmt) = void_type_node;
7422 OMP_BODY (stmt) = body;
7423 OMP_CLAUSES (stmt) = clauses;
7425 return add_stmt (stmt);
7428 tree
7429 finish_omp_parallel (tree clauses, tree body)
7431 tree stmt;
7433 body = finish_omp_structured_block (body);
7435 stmt = make_node (OMP_PARALLEL);
7436 TREE_TYPE (stmt) = void_type_node;
7437 OMP_PARALLEL_CLAUSES (stmt) = clauses;
7438 OMP_PARALLEL_BODY (stmt) = body;
7440 return add_stmt (stmt);
7443 tree
7444 begin_omp_task (void)
7446 keep_next_level (true);
7447 return begin_omp_structured_block ();
7450 tree
7451 finish_omp_task (tree clauses, tree body)
7453 tree stmt;
7455 body = finish_omp_structured_block (body);
7457 stmt = make_node (OMP_TASK);
7458 TREE_TYPE (stmt) = void_type_node;
7459 OMP_TASK_CLAUSES (stmt) = clauses;
7460 OMP_TASK_BODY (stmt) = body;
7462 return add_stmt (stmt);
7465 /* Helper function for finish_omp_for. Convert Ith random access iterator
7466 into integral iterator. Return FALSE if successful. */
7468 static bool
7469 handle_omp_for_class_iterator (int i, location_t locus, enum tree_code code,
7470 tree declv, tree orig_declv, tree initv,
7471 tree condv, tree incrv, tree *body,
7472 tree *pre_body, tree &clauses, tree *lastp,
7473 int collapse, int ordered)
7475 tree diff, iter_init, iter_incr = NULL, last;
7476 tree incr_var = NULL, orig_pre_body, orig_body, c;
7477 tree decl = TREE_VEC_ELT (declv, i);
7478 tree init = TREE_VEC_ELT (initv, i);
7479 tree cond = TREE_VEC_ELT (condv, i);
7480 tree incr = TREE_VEC_ELT (incrv, i);
7481 tree iter = decl;
7482 location_t elocus = locus;
7484 if (init && EXPR_HAS_LOCATION (init))
7485 elocus = EXPR_LOCATION (init);
7487 cond = cp_fully_fold (cond);
7488 switch (TREE_CODE (cond))
7490 case GT_EXPR:
7491 case GE_EXPR:
7492 case LT_EXPR:
7493 case LE_EXPR:
7494 case NE_EXPR:
7495 if (TREE_OPERAND (cond, 1) == iter)
7496 cond = build2 (swap_tree_comparison (TREE_CODE (cond)),
7497 TREE_TYPE (cond), iter, TREE_OPERAND (cond, 0));
7498 if (TREE_OPERAND (cond, 0) != iter)
7499 cond = error_mark_node;
7500 else
7502 tree tem = build_x_binary_op (EXPR_LOCATION (cond),
7503 TREE_CODE (cond),
7504 iter, ERROR_MARK,
7505 TREE_OPERAND (cond, 1), ERROR_MARK,
7506 NULL, tf_warning_or_error);
7507 if (error_operand_p (tem))
7508 return true;
7510 break;
7511 default:
7512 cond = error_mark_node;
7513 break;
7515 if (cond == error_mark_node)
7517 error_at (elocus, "invalid controlling predicate");
7518 return true;
7520 diff = build_x_binary_op (elocus, MINUS_EXPR, TREE_OPERAND (cond, 1),
7521 ERROR_MARK, iter, ERROR_MARK, NULL,
7522 tf_warning_or_error);
7523 diff = cp_fully_fold (diff);
7524 if (error_operand_p (diff))
7525 return true;
7526 if (TREE_CODE (TREE_TYPE (diff)) != INTEGER_TYPE)
7528 error_at (elocus, "difference between %qE and %qD does not have integer type",
7529 TREE_OPERAND (cond, 1), iter);
7530 return true;
7532 if (!c_omp_check_loop_iv_exprs (locus, orig_declv,
7533 TREE_VEC_ELT (declv, i), NULL_TREE,
7534 cond, cp_walk_subtrees))
7535 return true;
7537 switch (TREE_CODE (incr))
7539 case PREINCREMENT_EXPR:
7540 case PREDECREMENT_EXPR:
7541 case POSTINCREMENT_EXPR:
7542 case POSTDECREMENT_EXPR:
7543 if (TREE_OPERAND (incr, 0) != iter)
7545 incr = error_mark_node;
7546 break;
7548 iter_incr = build_x_unary_op (EXPR_LOCATION (incr),
7549 TREE_CODE (incr), iter,
7550 tf_warning_or_error);
7551 if (error_operand_p (iter_incr))
7552 return true;
7553 else if (TREE_CODE (incr) == PREINCREMENT_EXPR
7554 || TREE_CODE (incr) == POSTINCREMENT_EXPR)
7555 incr = integer_one_node;
7556 else
7557 incr = integer_minus_one_node;
7558 break;
7559 case MODIFY_EXPR:
7560 if (TREE_OPERAND (incr, 0) != iter)
7561 incr = error_mark_node;
7562 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
7563 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
7565 tree rhs = TREE_OPERAND (incr, 1);
7566 if (TREE_OPERAND (rhs, 0) == iter)
7568 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (rhs, 1)))
7569 != INTEGER_TYPE)
7570 incr = error_mark_node;
7571 else
7573 iter_incr = build_x_modify_expr (EXPR_LOCATION (rhs),
7574 iter, TREE_CODE (rhs),
7575 TREE_OPERAND (rhs, 1),
7576 tf_warning_or_error);
7577 if (error_operand_p (iter_incr))
7578 return true;
7579 incr = TREE_OPERAND (rhs, 1);
7580 incr = cp_convert (TREE_TYPE (diff), incr,
7581 tf_warning_or_error);
7582 if (TREE_CODE (rhs) == MINUS_EXPR)
7584 incr = build1 (NEGATE_EXPR, TREE_TYPE (diff), incr);
7585 incr = fold_simple (incr);
7587 if (TREE_CODE (incr) != INTEGER_CST
7588 && (TREE_CODE (incr) != NOP_EXPR
7589 || (TREE_CODE (TREE_OPERAND (incr, 0))
7590 != INTEGER_CST)))
7591 iter_incr = NULL;
7594 else if (TREE_OPERAND (rhs, 1) == iter)
7596 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (rhs, 0))) != INTEGER_TYPE
7597 || TREE_CODE (rhs) != PLUS_EXPR)
7598 incr = error_mark_node;
7599 else
7601 iter_incr = build_x_binary_op (EXPR_LOCATION (rhs),
7602 PLUS_EXPR,
7603 TREE_OPERAND (rhs, 0),
7604 ERROR_MARK, iter,
7605 ERROR_MARK, NULL,
7606 tf_warning_or_error);
7607 if (error_operand_p (iter_incr))
7608 return true;
7609 iter_incr = build_x_modify_expr (EXPR_LOCATION (rhs),
7610 iter, NOP_EXPR,
7611 iter_incr,
7612 tf_warning_or_error);
7613 if (error_operand_p (iter_incr))
7614 return true;
7615 incr = TREE_OPERAND (rhs, 0);
7616 iter_incr = NULL;
7619 else
7620 incr = error_mark_node;
7622 else
7623 incr = error_mark_node;
7624 break;
7625 default:
7626 incr = error_mark_node;
7627 break;
7630 if (incr == error_mark_node)
7632 error_at (elocus, "invalid increment expression");
7633 return true;
7636 incr = cp_convert (TREE_TYPE (diff), incr, tf_warning_or_error);
7637 bool taskloop_iv_seen = false;
7638 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
7639 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
7640 && OMP_CLAUSE_DECL (c) == iter)
7642 if (code == OMP_TASKLOOP)
7644 taskloop_iv_seen = true;
7645 OMP_CLAUSE_LASTPRIVATE_TASKLOOP_IV (c) = 1;
7647 break;
7649 else if (code == OMP_TASKLOOP
7650 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
7651 && OMP_CLAUSE_DECL (c) == iter)
7653 taskloop_iv_seen = true;
7654 OMP_CLAUSE_PRIVATE_TASKLOOP_IV (c) = 1;
7657 decl = create_temporary_var (TREE_TYPE (diff));
7658 pushdecl (decl);
7659 add_decl_expr (decl);
7660 last = create_temporary_var (TREE_TYPE (diff));
7661 pushdecl (last);
7662 add_decl_expr (last);
7663 if (c && iter_incr == NULL && TREE_CODE (incr) != INTEGER_CST
7664 && (!ordered || (i < collapse && collapse > 1)))
7666 incr_var = create_temporary_var (TREE_TYPE (diff));
7667 pushdecl (incr_var);
7668 add_decl_expr (incr_var);
7670 gcc_assert (stmts_are_full_exprs_p ());
7671 tree diffvar = NULL_TREE;
7672 if (code == OMP_TASKLOOP)
7674 if (!taskloop_iv_seen)
7676 tree ivc = build_omp_clause (locus, OMP_CLAUSE_FIRSTPRIVATE);
7677 OMP_CLAUSE_DECL (ivc) = iter;
7678 cxx_omp_finish_clause (ivc, NULL);
7679 OMP_CLAUSE_CHAIN (ivc) = clauses;
7680 clauses = ivc;
7682 tree lvc = build_omp_clause (locus, OMP_CLAUSE_FIRSTPRIVATE);
7683 OMP_CLAUSE_DECL (lvc) = last;
7684 OMP_CLAUSE_CHAIN (lvc) = clauses;
7685 clauses = lvc;
7686 diffvar = create_temporary_var (TREE_TYPE (diff));
7687 pushdecl (diffvar);
7688 add_decl_expr (diffvar);
7691 orig_pre_body = *pre_body;
7692 *pre_body = push_stmt_list ();
7693 if (orig_pre_body)
7694 add_stmt (orig_pre_body);
7695 if (init != NULL)
7696 finish_expr_stmt (build_x_modify_expr (elocus,
7697 iter, NOP_EXPR, init,
7698 tf_warning_or_error));
7699 init = build_int_cst (TREE_TYPE (diff), 0);
7700 if (c && iter_incr == NULL
7701 && (!ordered || (i < collapse && collapse > 1)))
7703 if (incr_var)
7705 finish_expr_stmt (build_x_modify_expr (elocus,
7706 incr_var, NOP_EXPR,
7707 incr, tf_warning_or_error));
7708 incr = incr_var;
7710 iter_incr = build_x_modify_expr (elocus,
7711 iter, PLUS_EXPR, incr,
7712 tf_warning_or_error);
7714 if (c && ordered && i < collapse && collapse > 1)
7715 iter_incr = incr;
7716 finish_expr_stmt (build_x_modify_expr (elocus,
7717 last, NOP_EXPR, init,
7718 tf_warning_or_error));
7719 if (diffvar)
7721 finish_expr_stmt (build_x_modify_expr (elocus,
7722 diffvar, NOP_EXPR,
7723 diff, tf_warning_or_error));
7724 diff = diffvar;
7726 *pre_body = pop_stmt_list (*pre_body);
7728 cond = cp_build_binary_op (elocus,
7729 TREE_CODE (cond), decl, diff,
7730 tf_warning_or_error);
7731 incr = build_modify_expr (elocus, decl, NULL_TREE, PLUS_EXPR,
7732 elocus, incr, NULL_TREE);
7734 orig_body = *body;
7735 *body = push_stmt_list ();
7736 iter_init = build2 (MINUS_EXPR, TREE_TYPE (diff), decl, last);
7737 iter_init = build_x_modify_expr (elocus,
7738 iter, PLUS_EXPR, iter_init,
7739 tf_warning_or_error);
7740 if (iter_init != error_mark_node)
7741 iter_init = build1 (NOP_EXPR, void_type_node, iter_init);
7742 finish_expr_stmt (iter_init);
7743 finish_expr_stmt (build_x_modify_expr (elocus,
7744 last, NOP_EXPR, decl,
7745 tf_warning_or_error));
7746 add_stmt (orig_body);
7747 *body = pop_stmt_list (*body);
7749 if (c)
7751 OMP_CLAUSE_LASTPRIVATE_STMT (c) = push_stmt_list ();
7752 if (!ordered)
7753 finish_expr_stmt (iter_incr);
7754 else
7756 iter_init = decl;
7757 if (i < collapse && collapse > 1 && !error_operand_p (iter_incr))
7758 iter_init = build2 (PLUS_EXPR, TREE_TYPE (diff),
7759 iter_init, iter_incr);
7760 iter_init = build2 (MINUS_EXPR, TREE_TYPE (diff), iter_init, last);
7761 iter_init = build_x_modify_expr (elocus,
7762 iter, PLUS_EXPR, iter_init,
7763 tf_warning_or_error);
7764 if (iter_init != error_mark_node)
7765 iter_init = build1 (NOP_EXPR, void_type_node, iter_init);
7766 finish_expr_stmt (iter_init);
7768 OMP_CLAUSE_LASTPRIVATE_STMT (c)
7769 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (c));
7772 TREE_VEC_ELT (declv, i) = decl;
7773 TREE_VEC_ELT (initv, i) = init;
7774 TREE_VEC_ELT (condv, i) = cond;
7775 TREE_VEC_ELT (incrv, i) = incr;
7776 *lastp = last;
7778 return false;
7781 /* Build and validate an OMP_FOR statement. CLAUSES, BODY, COND, INCR
7782 are directly for their associated operands in the statement. DECL
7783 and INIT are a combo; if DECL is NULL then INIT ought to be a
7784 MODIFY_EXPR, and the DECL should be extracted. PRE_BODY are
7785 optional statements that need to go before the loop into its
7786 sk_omp scope. */
7788 tree
7789 finish_omp_for (location_t locus, enum tree_code code, tree declv,
7790 tree orig_declv, tree initv, tree condv, tree incrv,
7791 tree body, tree pre_body, vec<tree> *orig_inits, tree clauses)
7793 tree omp_for = NULL, orig_incr = NULL;
7794 tree decl = NULL, init, cond, incr, orig_decl = NULL_TREE, block = NULL_TREE;
7795 tree last = NULL_TREE;
7796 location_t elocus;
7797 int i;
7798 int collapse = 1;
7799 int ordered = 0;
7801 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (initv));
7802 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (condv));
7803 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (incrv));
7804 if (TREE_VEC_LENGTH (declv) > 1)
7806 tree c = find_omp_clause (clauses, OMP_CLAUSE_COLLAPSE);
7807 if (c)
7808 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (c));
7809 if (collapse != TREE_VEC_LENGTH (declv))
7810 ordered = TREE_VEC_LENGTH (declv);
7812 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
7814 decl = TREE_VEC_ELT (declv, i);
7815 init = TREE_VEC_ELT (initv, i);
7816 cond = TREE_VEC_ELT (condv, i);
7817 incr = TREE_VEC_ELT (incrv, i);
7818 elocus = locus;
7820 if (decl == NULL)
7822 if (init != NULL)
7823 switch (TREE_CODE (init))
7825 case MODIFY_EXPR:
7826 decl = TREE_OPERAND (init, 0);
7827 init = TREE_OPERAND (init, 1);
7828 break;
7829 case MODOP_EXPR:
7830 if (TREE_CODE (TREE_OPERAND (init, 1)) == NOP_EXPR)
7832 decl = TREE_OPERAND (init, 0);
7833 init = TREE_OPERAND (init, 2);
7835 break;
7836 default:
7837 break;
7840 if (decl == NULL)
7842 error_at (locus,
7843 "expected iteration declaration or initialization");
7844 return NULL;
7848 if (init && EXPR_HAS_LOCATION (init))
7849 elocus = EXPR_LOCATION (init);
7851 if (cond == NULL)
7853 error_at (elocus, "missing controlling predicate");
7854 return NULL;
7857 if (incr == NULL)
7859 error_at (elocus, "missing increment expression");
7860 return NULL;
7863 TREE_VEC_ELT (declv, i) = decl;
7864 TREE_VEC_ELT (initv, i) = init;
7867 if (orig_inits)
7869 bool fail = false;
7870 tree orig_init;
7871 FOR_EACH_VEC_ELT (*orig_inits, i, orig_init)
7872 if (orig_init
7873 && !c_omp_check_loop_iv_exprs (locus, declv,
7874 TREE_VEC_ELT (declv, i), orig_init,
7875 NULL_TREE, cp_walk_subtrees))
7876 fail = true;
7877 if (fail)
7878 return NULL;
7881 if (dependent_omp_for_p (declv, initv, condv, incrv))
7883 tree stmt;
7885 stmt = make_node (code);
7887 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
7889 /* This is really just a place-holder. We'll be decomposing this
7890 again and going through the cp_build_modify_expr path below when
7891 we instantiate the thing. */
7892 TREE_VEC_ELT (initv, i)
7893 = build2 (MODIFY_EXPR, void_type_node, TREE_VEC_ELT (declv, i),
7894 TREE_VEC_ELT (initv, i));
7897 TREE_TYPE (stmt) = void_type_node;
7898 OMP_FOR_INIT (stmt) = initv;
7899 OMP_FOR_COND (stmt) = condv;
7900 OMP_FOR_INCR (stmt) = incrv;
7901 OMP_FOR_BODY (stmt) = body;
7902 OMP_FOR_PRE_BODY (stmt) = pre_body;
7903 OMP_FOR_CLAUSES (stmt) = clauses;
7905 SET_EXPR_LOCATION (stmt, locus);
7906 return add_stmt (stmt);
7909 if (!orig_declv)
7910 orig_declv = copy_node (declv);
7912 if (processing_template_decl)
7913 orig_incr = make_tree_vec (TREE_VEC_LENGTH (incrv));
7915 for (i = 0; i < TREE_VEC_LENGTH (declv); )
7917 decl = TREE_VEC_ELT (declv, i);
7918 init = TREE_VEC_ELT (initv, i);
7919 cond = TREE_VEC_ELT (condv, i);
7920 incr = TREE_VEC_ELT (incrv, i);
7921 if (orig_incr)
7922 TREE_VEC_ELT (orig_incr, i) = incr;
7923 elocus = locus;
7925 if (init && EXPR_HAS_LOCATION (init))
7926 elocus = EXPR_LOCATION (init);
7928 if (!DECL_P (decl))
7930 error_at (elocus, "expected iteration declaration or initialization");
7931 return NULL;
7934 if (incr && TREE_CODE (incr) == MODOP_EXPR)
7936 if (orig_incr)
7937 TREE_VEC_ELT (orig_incr, i) = incr;
7938 incr = cp_build_modify_expr (TREE_OPERAND (incr, 0),
7939 TREE_CODE (TREE_OPERAND (incr, 1)),
7940 TREE_OPERAND (incr, 2),
7941 tf_warning_or_error);
7944 if (CLASS_TYPE_P (TREE_TYPE (decl)))
7946 if (code == OMP_SIMD)
7948 error_at (elocus, "%<#pragma omp simd%> used with class "
7949 "iteration variable %qE", decl);
7950 return NULL;
7952 if (code == CILK_FOR && i == 0)
7953 orig_decl = decl;
7954 if (handle_omp_for_class_iterator (i, locus, code, declv, orig_declv,
7955 initv, condv, incrv, &body,
7956 &pre_body, clauses, &last,
7957 collapse, ordered))
7958 return NULL;
7959 continue;
7962 if (!INTEGRAL_TYPE_P (TREE_TYPE (decl))
7963 && !TYPE_PTR_P (TREE_TYPE (decl)))
7965 error_at (elocus, "invalid type for iteration variable %qE", decl);
7966 return NULL;
7969 if (!processing_template_decl)
7971 init = fold_build_cleanup_point_expr (TREE_TYPE (init), init);
7972 init = cp_build_modify_expr (decl, NOP_EXPR, init, tf_warning_or_error);
7974 else
7975 init = build2 (MODIFY_EXPR, void_type_node, decl, init);
7976 if (cond
7977 && TREE_SIDE_EFFECTS (cond)
7978 && COMPARISON_CLASS_P (cond)
7979 && !processing_template_decl)
7981 tree t = TREE_OPERAND (cond, 0);
7982 if (TREE_SIDE_EFFECTS (t)
7983 && t != decl
7984 && (TREE_CODE (t) != NOP_EXPR
7985 || TREE_OPERAND (t, 0) != decl))
7986 TREE_OPERAND (cond, 0)
7987 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7989 t = TREE_OPERAND (cond, 1);
7990 if (TREE_SIDE_EFFECTS (t)
7991 && t != decl
7992 && (TREE_CODE (t) != NOP_EXPR
7993 || TREE_OPERAND (t, 0) != decl))
7994 TREE_OPERAND (cond, 1)
7995 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7997 if (decl == error_mark_node || init == error_mark_node)
7998 return NULL;
8000 TREE_VEC_ELT (declv, i) = decl;
8001 TREE_VEC_ELT (initv, i) = init;
8002 TREE_VEC_ELT (condv, i) = cond;
8003 TREE_VEC_ELT (incrv, i) = incr;
8004 i++;
8007 if (IS_EMPTY_STMT (pre_body))
8008 pre_body = NULL;
8010 if (code == CILK_FOR && !processing_template_decl)
8011 block = push_stmt_list ();
8013 omp_for = c_finish_omp_for (locus, code, declv, orig_declv, initv, condv,
8014 incrv, body, pre_body);
8016 /* Check for iterators appearing in lb, b or incr expressions. */
8017 if (omp_for && !c_omp_check_loop_iv (omp_for, orig_declv, cp_walk_subtrees))
8018 omp_for = NULL_TREE;
8020 if (omp_for == NULL)
8022 if (block)
8023 pop_stmt_list (block);
8024 return NULL;
8027 add_stmt (omp_for);
8029 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INCR (omp_for)); i++)
8031 decl = TREE_OPERAND (TREE_VEC_ELT (OMP_FOR_INIT (omp_for), i), 0);
8032 incr = TREE_VEC_ELT (OMP_FOR_INCR (omp_for), i);
8034 if (TREE_CODE (incr) != MODIFY_EXPR)
8035 continue;
8037 if (TREE_SIDE_EFFECTS (TREE_OPERAND (incr, 1))
8038 && BINARY_CLASS_P (TREE_OPERAND (incr, 1))
8039 && !processing_template_decl)
8041 tree t = TREE_OPERAND (TREE_OPERAND (incr, 1), 0);
8042 if (TREE_SIDE_EFFECTS (t)
8043 && t != decl
8044 && (TREE_CODE (t) != NOP_EXPR
8045 || TREE_OPERAND (t, 0) != decl))
8046 TREE_OPERAND (TREE_OPERAND (incr, 1), 0)
8047 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
8049 t = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
8050 if (TREE_SIDE_EFFECTS (t)
8051 && t != decl
8052 && (TREE_CODE (t) != NOP_EXPR
8053 || TREE_OPERAND (t, 0) != decl))
8054 TREE_OPERAND (TREE_OPERAND (incr, 1), 1)
8055 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
8058 if (orig_incr)
8059 TREE_VEC_ELT (OMP_FOR_INCR (omp_for), i) = TREE_VEC_ELT (orig_incr, i);
8061 OMP_FOR_CLAUSES (omp_for) = clauses;
8063 /* For simd loops with non-static data member iterators, we could have added
8064 OMP_CLAUSE_LINEAR clauses without OMP_CLAUSE_LINEAR_STEP. As we know the
8065 step at this point, fill it in. */
8066 if (code == OMP_SIMD && !processing_template_decl
8067 && TREE_VEC_LENGTH (OMP_FOR_INCR (omp_for)) == 1)
8068 for (tree c = find_omp_clause (clauses, OMP_CLAUSE_LINEAR); c;
8069 c = find_omp_clause (OMP_CLAUSE_CHAIN (c), OMP_CLAUSE_LINEAR))
8070 if (OMP_CLAUSE_LINEAR_STEP (c) == NULL_TREE)
8072 decl = TREE_OPERAND (TREE_VEC_ELT (OMP_FOR_INIT (omp_for), 0), 0);
8073 gcc_assert (decl == OMP_CLAUSE_DECL (c));
8074 incr = TREE_VEC_ELT (OMP_FOR_INCR (omp_for), 0);
8075 tree step, stept;
8076 switch (TREE_CODE (incr))
8078 case PREINCREMENT_EXPR:
8079 case POSTINCREMENT_EXPR:
8080 /* c_omp_for_incr_canonicalize_ptr() should have been
8081 called to massage things appropriately. */
8082 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (decl)));
8083 OMP_CLAUSE_LINEAR_STEP (c) = build_int_cst (TREE_TYPE (decl), 1);
8084 break;
8085 case PREDECREMENT_EXPR:
8086 case POSTDECREMENT_EXPR:
8087 /* c_omp_for_incr_canonicalize_ptr() should have been
8088 called to massage things appropriately. */
8089 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (decl)));
8090 OMP_CLAUSE_LINEAR_STEP (c)
8091 = build_int_cst (TREE_TYPE (decl), -1);
8092 break;
8093 case MODIFY_EXPR:
8094 gcc_assert (TREE_OPERAND (incr, 0) == decl);
8095 incr = TREE_OPERAND (incr, 1);
8096 switch (TREE_CODE (incr))
8098 case PLUS_EXPR:
8099 if (TREE_OPERAND (incr, 1) == decl)
8100 step = TREE_OPERAND (incr, 0);
8101 else
8102 step = TREE_OPERAND (incr, 1);
8103 break;
8104 case MINUS_EXPR:
8105 case POINTER_PLUS_EXPR:
8106 gcc_assert (TREE_OPERAND (incr, 0) == decl);
8107 step = TREE_OPERAND (incr, 1);
8108 break;
8109 default:
8110 gcc_unreachable ();
8112 stept = TREE_TYPE (decl);
8113 if (POINTER_TYPE_P (stept))
8114 stept = sizetype;
8115 step = fold_convert (stept, step);
8116 if (TREE_CODE (incr) == MINUS_EXPR)
8117 step = fold_build1 (NEGATE_EXPR, stept, step);
8118 OMP_CLAUSE_LINEAR_STEP (c) = step;
8119 break;
8120 default:
8121 gcc_unreachable ();
8125 if (block)
8127 tree omp_par = make_node (OMP_PARALLEL);
8128 TREE_TYPE (omp_par) = void_type_node;
8129 OMP_PARALLEL_CLAUSES (omp_par) = NULL_TREE;
8130 tree bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
8131 TREE_SIDE_EFFECTS (bind) = 1;
8132 BIND_EXPR_BODY (bind) = pop_stmt_list (block);
8133 OMP_PARALLEL_BODY (omp_par) = bind;
8134 if (OMP_FOR_PRE_BODY (omp_for))
8136 add_stmt (OMP_FOR_PRE_BODY (omp_for));
8137 OMP_FOR_PRE_BODY (omp_for) = NULL_TREE;
8139 init = TREE_VEC_ELT (OMP_FOR_INIT (omp_for), 0);
8140 decl = TREE_OPERAND (init, 0);
8141 cond = TREE_VEC_ELT (OMP_FOR_COND (omp_for), 0);
8142 incr = TREE_VEC_ELT (OMP_FOR_INCR (omp_for), 0);
8143 tree t = TREE_OPERAND (cond, 1), c, clauses, *pc;
8144 clauses = OMP_FOR_CLAUSES (omp_for);
8145 OMP_FOR_CLAUSES (omp_for) = NULL_TREE;
8146 for (pc = &clauses; *pc; )
8147 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_SCHEDULE)
8149 gcc_assert (OMP_FOR_CLAUSES (omp_for) == NULL_TREE);
8150 OMP_FOR_CLAUSES (omp_for) = *pc;
8151 *pc = OMP_CLAUSE_CHAIN (*pc);
8152 OMP_CLAUSE_CHAIN (OMP_FOR_CLAUSES (omp_for)) = NULL_TREE;
8154 else
8156 gcc_assert (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_FIRSTPRIVATE);
8157 pc = &OMP_CLAUSE_CHAIN (*pc);
8159 if (TREE_CODE (t) != INTEGER_CST)
8161 TREE_OPERAND (cond, 1) = get_temp_regvar (TREE_TYPE (t), t);
8162 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
8163 OMP_CLAUSE_DECL (c) = TREE_OPERAND (cond, 1);
8164 OMP_CLAUSE_CHAIN (c) = clauses;
8165 clauses = c;
8167 if (TREE_CODE (incr) == MODIFY_EXPR)
8169 t = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
8170 if (TREE_CODE (t) != INTEGER_CST)
8172 TREE_OPERAND (TREE_OPERAND (incr, 1), 1)
8173 = get_temp_regvar (TREE_TYPE (t), t);
8174 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
8175 OMP_CLAUSE_DECL (c) = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
8176 OMP_CLAUSE_CHAIN (c) = clauses;
8177 clauses = c;
8180 t = TREE_OPERAND (init, 1);
8181 if (TREE_CODE (t) != INTEGER_CST)
8183 TREE_OPERAND (init, 1) = get_temp_regvar (TREE_TYPE (t), t);
8184 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
8185 OMP_CLAUSE_DECL (c) = TREE_OPERAND (init, 1);
8186 OMP_CLAUSE_CHAIN (c) = clauses;
8187 clauses = c;
8189 if (orig_decl && orig_decl != decl)
8191 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
8192 OMP_CLAUSE_DECL (c) = orig_decl;
8193 OMP_CLAUSE_CHAIN (c) = clauses;
8194 clauses = c;
8196 if (last)
8198 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
8199 OMP_CLAUSE_DECL (c) = last;
8200 OMP_CLAUSE_CHAIN (c) = clauses;
8201 clauses = c;
8203 c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
8204 OMP_CLAUSE_DECL (c) = decl;
8205 OMP_CLAUSE_CHAIN (c) = clauses;
8206 clauses = c;
8207 c = build_omp_clause (input_location, OMP_CLAUSE__CILK_FOR_COUNT_);
8208 OMP_CLAUSE_OPERAND (c, 0)
8209 = cilk_for_number_of_iterations (omp_for);
8210 OMP_CLAUSE_CHAIN (c) = clauses;
8211 OMP_PARALLEL_CLAUSES (omp_par) = finish_omp_clauses (c, false);
8212 add_stmt (omp_par);
8213 return omp_par;
8215 else if (code == CILK_FOR && processing_template_decl)
8217 tree c, clauses = OMP_FOR_CLAUSES (omp_for);
8218 if (orig_decl && orig_decl != decl)
8220 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
8221 OMP_CLAUSE_DECL (c) = orig_decl;
8222 OMP_CLAUSE_CHAIN (c) = clauses;
8223 clauses = c;
8225 if (last)
8227 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
8228 OMP_CLAUSE_DECL (c) = last;
8229 OMP_CLAUSE_CHAIN (c) = clauses;
8230 clauses = c;
8232 OMP_FOR_CLAUSES (omp_for) = clauses;
8234 return omp_for;
8237 void
8238 finish_omp_atomic (enum tree_code code, enum tree_code opcode, tree lhs,
8239 tree rhs, tree v, tree lhs1, tree rhs1, bool seq_cst)
8241 tree orig_lhs;
8242 tree orig_rhs;
8243 tree orig_v;
8244 tree orig_lhs1;
8245 tree orig_rhs1;
8246 bool dependent_p;
8247 tree stmt;
8249 orig_lhs = lhs;
8250 orig_rhs = rhs;
8251 orig_v = v;
8252 orig_lhs1 = lhs1;
8253 orig_rhs1 = rhs1;
8254 dependent_p = false;
8255 stmt = NULL_TREE;
8257 /* Even in a template, we can detect invalid uses of the atomic
8258 pragma if neither LHS nor RHS is type-dependent. */
8259 if (processing_template_decl)
8261 dependent_p = (type_dependent_expression_p (lhs)
8262 || (rhs && type_dependent_expression_p (rhs))
8263 || (v && type_dependent_expression_p (v))
8264 || (lhs1 && type_dependent_expression_p (lhs1))
8265 || (rhs1 && type_dependent_expression_p (rhs1)));
8266 if (!dependent_p)
8268 lhs = build_non_dependent_expr (lhs);
8269 if (rhs)
8270 rhs = build_non_dependent_expr (rhs);
8271 if (v)
8272 v = build_non_dependent_expr (v);
8273 if (lhs1)
8274 lhs1 = build_non_dependent_expr (lhs1);
8275 if (rhs1)
8276 rhs1 = build_non_dependent_expr (rhs1);
8279 if (!dependent_p)
8281 bool swapped = false;
8282 if (rhs1 && cp_tree_equal (lhs, rhs))
8284 std::swap (rhs, rhs1);
8285 swapped = !commutative_tree_code (opcode);
8287 if (rhs1 && !cp_tree_equal (lhs, rhs1))
8289 if (code == OMP_ATOMIC)
8290 error ("%<#pragma omp atomic update%> uses two different "
8291 "expressions for memory");
8292 else
8293 error ("%<#pragma omp atomic capture%> uses two different "
8294 "expressions for memory");
8295 return;
8297 if (lhs1 && !cp_tree_equal (lhs, lhs1))
8299 if (code == OMP_ATOMIC)
8300 error ("%<#pragma omp atomic update%> uses two different "
8301 "expressions for memory");
8302 else
8303 error ("%<#pragma omp atomic capture%> uses two different "
8304 "expressions for memory");
8305 return;
8307 stmt = c_finish_omp_atomic (input_location, code, opcode, lhs, rhs,
8308 v, lhs1, rhs1, swapped, seq_cst,
8309 processing_template_decl != 0);
8310 if (stmt == error_mark_node)
8311 return;
8313 if (processing_template_decl)
8315 if (code == OMP_ATOMIC_READ)
8317 stmt = build_min_nt_loc (EXPR_LOCATION (orig_lhs),
8318 OMP_ATOMIC_READ, orig_lhs);
8319 OMP_ATOMIC_SEQ_CST (stmt) = seq_cst;
8320 stmt = build2 (MODIFY_EXPR, void_type_node, orig_v, stmt);
8322 else
8324 if (opcode == NOP_EXPR)
8325 stmt = build2 (MODIFY_EXPR, void_type_node, orig_lhs, orig_rhs);
8326 else
8327 stmt = build2 (opcode, void_type_node, orig_lhs, orig_rhs);
8328 if (orig_rhs1)
8329 stmt = build_min_nt_loc (EXPR_LOCATION (orig_rhs1),
8330 COMPOUND_EXPR, orig_rhs1, stmt);
8331 if (code != OMP_ATOMIC)
8333 stmt = build_min_nt_loc (EXPR_LOCATION (orig_lhs1),
8334 code, orig_lhs1, stmt);
8335 OMP_ATOMIC_SEQ_CST (stmt) = seq_cst;
8336 stmt = build2 (MODIFY_EXPR, void_type_node, orig_v, stmt);
8339 stmt = build2 (OMP_ATOMIC, void_type_node, integer_zero_node, stmt);
8340 OMP_ATOMIC_SEQ_CST (stmt) = seq_cst;
8342 finish_expr_stmt (stmt);
8345 void
8346 finish_omp_barrier (void)
8348 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_BARRIER);
8349 vec<tree, va_gc> *vec = make_tree_vector ();
8350 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
8351 release_tree_vector (vec);
8352 finish_expr_stmt (stmt);
8355 void
8356 finish_omp_flush (void)
8358 tree fn = builtin_decl_explicit (BUILT_IN_SYNC_SYNCHRONIZE);
8359 vec<tree, va_gc> *vec = make_tree_vector ();
8360 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
8361 release_tree_vector (vec);
8362 finish_expr_stmt (stmt);
8365 void
8366 finish_omp_taskwait (void)
8368 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_TASKWAIT);
8369 vec<tree, va_gc> *vec = make_tree_vector ();
8370 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
8371 release_tree_vector (vec);
8372 finish_expr_stmt (stmt);
8375 void
8376 finish_omp_taskyield (void)
8378 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_TASKYIELD);
8379 vec<tree, va_gc> *vec = make_tree_vector ();
8380 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
8381 release_tree_vector (vec);
8382 finish_expr_stmt (stmt);
8385 void
8386 finish_omp_cancel (tree clauses)
8388 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
8389 int mask = 0;
8390 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
8391 mask = 1;
8392 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
8393 mask = 2;
8394 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
8395 mask = 4;
8396 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
8397 mask = 8;
8398 else
8400 error ("%<#pragma omp cancel must specify one of "
8401 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> clauses");
8402 return;
8404 vec<tree, va_gc> *vec = make_tree_vector ();
8405 tree ifc = find_omp_clause (clauses, OMP_CLAUSE_IF);
8406 if (ifc != NULL_TREE)
8408 tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
8409 ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
8410 boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
8411 build_zero_cst (type));
8413 else
8414 ifc = boolean_true_node;
8415 vec->quick_push (build_int_cst (integer_type_node, mask));
8416 vec->quick_push (ifc);
8417 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
8418 release_tree_vector (vec);
8419 finish_expr_stmt (stmt);
8422 void
8423 finish_omp_cancellation_point (tree clauses)
8425 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
8426 int mask = 0;
8427 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
8428 mask = 1;
8429 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
8430 mask = 2;
8431 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
8432 mask = 4;
8433 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
8434 mask = 8;
8435 else
8437 error ("%<#pragma omp cancellation point must specify one of "
8438 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> clauses");
8439 return;
8441 vec<tree, va_gc> *vec
8442 = make_tree_vector_single (build_int_cst (integer_type_node, mask));
8443 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
8444 release_tree_vector (vec);
8445 finish_expr_stmt (stmt);
8448 /* Begin a __transaction_atomic or __transaction_relaxed statement.
8449 If PCOMPOUND is non-null, this is for a function-transaction-block, and we
8450 should create an extra compound stmt. */
8452 tree
8453 begin_transaction_stmt (location_t loc, tree *pcompound, int flags)
8455 tree r;
8457 if (pcompound)
8458 *pcompound = begin_compound_stmt (0);
8460 r = build_stmt (loc, TRANSACTION_EXPR, NULL_TREE);
8462 /* Only add the statement to the function if support enabled. */
8463 if (flag_tm)
8464 add_stmt (r);
8465 else
8466 error_at (loc, ((flags & TM_STMT_ATTR_RELAXED) != 0
8467 ? G_("%<__transaction_relaxed%> without "
8468 "transactional memory support enabled")
8469 : G_("%<__transaction_atomic%> without "
8470 "transactional memory support enabled")));
8472 TRANSACTION_EXPR_BODY (r) = push_stmt_list ();
8473 TREE_SIDE_EFFECTS (r) = 1;
8474 return r;
8477 /* End a __transaction_atomic or __transaction_relaxed statement.
8478 If COMPOUND_STMT is non-null, this is for a function-transaction-block,
8479 and we should end the compound. If NOEX is non-NULL, we wrap the body in
8480 a MUST_NOT_THROW_EXPR with NOEX as condition. */
8482 void
8483 finish_transaction_stmt (tree stmt, tree compound_stmt, int flags, tree noex)
8485 TRANSACTION_EXPR_BODY (stmt) = pop_stmt_list (TRANSACTION_EXPR_BODY (stmt));
8486 TRANSACTION_EXPR_OUTER (stmt) = (flags & TM_STMT_ATTR_OUTER) != 0;
8487 TRANSACTION_EXPR_RELAXED (stmt) = (flags & TM_STMT_ATTR_RELAXED) != 0;
8488 TRANSACTION_EXPR_IS_STMT (stmt) = 1;
8490 /* noexcept specifications are not allowed for function transactions. */
8491 gcc_assert (!(noex && compound_stmt));
8492 if (noex)
8494 tree body = build_must_not_throw_expr (TRANSACTION_EXPR_BODY (stmt),
8495 noex);
8496 protected_set_expr_location
8497 (body, EXPR_LOCATION (TRANSACTION_EXPR_BODY (stmt)));
8498 TREE_SIDE_EFFECTS (body) = 1;
8499 TRANSACTION_EXPR_BODY (stmt) = body;
8502 if (compound_stmt)
8503 finish_compound_stmt (compound_stmt);
8506 /* Build a __transaction_atomic or __transaction_relaxed expression. If
8507 NOEX is non-NULL, we wrap the body in a MUST_NOT_THROW_EXPR with NOEX as
8508 condition. */
8510 tree
8511 build_transaction_expr (location_t loc, tree expr, int flags, tree noex)
8513 tree ret;
8514 if (noex)
8516 expr = build_must_not_throw_expr (expr, noex);
8517 protected_set_expr_location (expr, loc);
8518 TREE_SIDE_EFFECTS (expr) = 1;
8520 ret = build1 (TRANSACTION_EXPR, TREE_TYPE (expr), expr);
8521 if (flags & TM_STMT_ATTR_RELAXED)
8522 TRANSACTION_EXPR_RELAXED (ret) = 1;
8523 TREE_SIDE_EFFECTS (ret) = 1;
8524 SET_EXPR_LOCATION (ret, loc);
8525 return ret;
8528 void
8529 init_cp_semantics (void)
8533 /* Build a STATIC_ASSERT for a static assertion with the condition
8534 CONDITION and the message text MESSAGE. LOCATION is the location
8535 of the static assertion in the source code. When MEMBER_P, this
8536 static assertion is a member of a class. */
8537 void
8538 finish_static_assert (tree condition, tree message, location_t location,
8539 bool member_p)
8541 if (message == NULL_TREE
8542 || message == error_mark_node
8543 || condition == NULL_TREE
8544 || condition == error_mark_node)
8545 return;
8547 if (check_for_bare_parameter_packs (condition))
8548 condition = error_mark_node;
8550 if (type_dependent_expression_p (condition)
8551 || value_dependent_expression_p (condition))
8553 /* We're in a template; build a STATIC_ASSERT and put it in
8554 the right place. */
8555 tree assertion;
8557 assertion = make_node (STATIC_ASSERT);
8558 STATIC_ASSERT_CONDITION (assertion) = condition;
8559 STATIC_ASSERT_MESSAGE (assertion) = message;
8560 STATIC_ASSERT_SOURCE_LOCATION (assertion) = location;
8562 if (member_p)
8563 maybe_add_class_template_decl_list (current_class_type,
8564 assertion,
8565 /*friend_p=*/0);
8566 else
8567 add_stmt (assertion);
8569 return;
8572 /* Fold the expression and convert it to a boolean value. */
8573 condition = instantiate_non_dependent_expr (condition);
8574 condition = cp_convert (boolean_type_node, condition, tf_warning_or_error);
8575 condition = maybe_constant_value (condition);
8577 if (TREE_CODE (condition) == INTEGER_CST && !integer_zerop (condition))
8578 /* Do nothing; the condition is satisfied. */
8580 else
8582 location_t saved_loc = input_location;
8584 input_location = location;
8585 if (TREE_CODE (condition) == INTEGER_CST
8586 && integer_zerop (condition))
8588 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
8589 (TREE_TYPE (TREE_TYPE (message))));
8590 int len = TREE_STRING_LENGTH (message) / sz - 1;
8591 /* Report the error. */
8592 if (len == 0)
8593 error ("static assertion failed");
8594 else
8595 error ("static assertion failed: %s",
8596 TREE_STRING_POINTER (message));
8598 else if (condition && condition != error_mark_node)
8600 error ("non-constant condition for static assertion");
8601 if (require_potential_rvalue_constant_expression (condition))
8602 cxx_constant_value (condition);
8604 input_location = saved_loc;
8608 /* Implements the C++0x decltype keyword. Returns the type of EXPR,
8609 suitable for use as a type-specifier.
8611 ID_EXPRESSION_OR_MEMBER_ACCESS_P is true when EXPR was parsed as an
8612 id-expression or a class member access, FALSE when it was parsed as
8613 a full expression. */
8615 tree
8616 finish_decltype_type (tree expr, bool id_expression_or_member_access_p,
8617 tsubst_flags_t complain)
8619 tree type = NULL_TREE;
8621 if (!expr || error_operand_p (expr))
8622 return error_mark_node;
8624 if (TYPE_P (expr)
8625 || TREE_CODE (expr) == TYPE_DECL
8626 || (TREE_CODE (expr) == BIT_NOT_EXPR
8627 && TYPE_P (TREE_OPERAND (expr, 0))))
8629 if (complain & tf_error)
8630 error ("argument to decltype must be an expression");
8631 return error_mark_node;
8634 /* Depending on the resolution of DR 1172, we may later need to distinguish
8635 instantiation-dependent but not type-dependent expressions so that, say,
8636 A<decltype(sizeof(T))>::U doesn't require 'typename'. */
8637 if (instantiation_dependent_expression_p (expr))
8639 type = cxx_make_type (DECLTYPE_TYPE);
8640 DECLTYPE_TYPE_EXPR (type) = expr;
8641 DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (type)
8642 = id_expression_or_member_access_p;
8643 SET_TYPE_STRUCTURAL_EQUALITY (type);
8645 return type;
8648 /* The type denoted by decltype(e) is defined as follows: */
8650 expr = resolve_nondeduced_context (expr);
8652 if (invalid_nonstatic_memfn_p (input_location, expr, complain))
8653 return error_mark_node;
8655 if (type_unknown_p (expr))
8657 if (complain & tf_error)
8658 error ("decltype cannot resolve address of overloaded function");
8659 return error_mark_node;
8662 /* To get the size of a static data member declared as an array of
8663 unknown bound, we need to instantiate it. */
8664 if (VAR_P (expr)
8665 && VAR_HAD_UNKNOWN_BOUND (expr)
8666 && DECL_TEMPLATE_INSTANTIATION (expr))
8667 instantiate_decl (expr, /*defer_ok*/true, /*expl_inst_mem*/false);
8669 if (id_expression_or_member_access_p)
8671 /* If e is an id-expression or a class member access (5.2.5
8672 [expr.ref]), decltype(e) is defined as the type of the entity
8673 named by e. If there is no such entity, or e names a set of
8674 overloaded functions, the program is ill-formed. */
8675 if (identifier_p (expr))
8676 expr = lookup_name (expr);
8678 if (INDIRECT_REF_P (expr))
8679 /* This can happen when the expression is, e.g., "a.b". Just
8680 look at the underlying operand. */
8681 expr = TREE_OPERAND (expr, 0);
8683 if (TREE_CODE (expr) == OFFSET_REF
8684 || TREE_CODE (expr) == MEMBER_REF
8685 || TREE_CODE (expr) == SCOPE_REF)
8686 /* We're only interested in the field itself. If it is a
8687 BASELINK, we will need to see through it in the next
8688 step. */
8689 expr = TREE_OPERAND (expr, 1);
8691 if (BASELINK_P (expr))
8692 /* See through BASELINK nodes to the underlying function. */
8693 expr = BASELINK_FUNCTIONS (expr);
8695 switch (TREE_CODE (expr))
8697 case FIELD_DECL:
8698 if (DECL_BIT_FIELD_TYPE (expr))
8700 type = DECL_BIT_FIELD_TYPE (expr);
8701 break;
8703 /* Fall through for fields that aren't bitfields. */
8705 case FUNCTION_DECL:
8706 case VAR_DECL:
8707 case CONST_DECL:
8708 case PARM_DECL:
8709 case RESULT_DECL:
8710 case TEMPLATE_PARM_INDEX:
8711 expr = mark_type_use (expr);
8712 type = TREE_TYPE (expr);
8713 break;
8715 case ERROR_MARK:
8716 type = error_mark_node;
8717 break;
8719 case COMPONENT_REF:
8720 case COMPOUND_EXPR:
8721 mark_type_use (expr);
8722 type = is_bitfield_expr_with_lowered_type (expr);
8723 if (!type)
8724 type = TREE_TYPE (TREE_OPERAND (expr, 1));
8725 break;
8727 case BIT_FIELD_REF:
8728 gcc_unreachable ();
8730 case INTEGER_CST:
8731 case PTRMEM_CST:
8732 /* We can get here when the id-expression refers to an
8733 enumerator or non-type template parameter. */
8734 type = TREE_TYPE (expr);
8735 break;
8737 default:
8738 /* Handle instantiated template non-type arguments. */
8739 type = TREE_TYPE (expr);
8740 break;
8743 else
8745 /* Within a lambda-expression:
8747 Every occurrence of decltype((x)) where x is a possibly
8748 parenthesized id-expression that names an entity of
8749 automatic storage duration is treated as if x were
8750 transformed into an access to a corresponding data member
8751 of the closure type that would have been declared if x
8752 were a use of the denoted entity. */
8753 if (outer_automatic_var_p (expr)
8754 && current_function_decl
8755 && LAMBDA_FUNCTION_P (current_function_decl))
8756 type = capture_decltype (expr);
8757 else if (error_operand_p (expr))
8758 type = error_mark_node;
8759 else if (expr == current_class_ptr)
8760 /* If the expression is just "this", we want the
8761 cv-unqualified pointer for the "this" type. */
8762 type = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
8763 else
8765 /* Otherwise, where T is the type of e, if e is an lvalue,
8766 decltype(e) is defined as T&; if an xvalue, T&&; otherwise, T. */
8767 cp_lvalue_kind clk = lvalue_kind (expr);
8768 type = unlowered_expr_type (expr);
8769 gcc_assert (TREE_CODE (type) != REFERENCE_TYPE);
8771 /* For vector types, pick a non-opaque variant. */
8772 if (VECTOR_TYPE_P (type))
8773 type = strip_typedefs (type);
8775 if (clk != clk_none && !(clk & clk_class))
8776 type = cp_build_reference_type (type, (clk & clk_rvalueref));
8780 return type;
8783 /* Called from trait_expr_value to evaluate either __has_nothrow_assign or
8784 __has_nothrow_copy, depending on assign_p. */
8786 static bool
8787 classtype_has_nothrow_assign_or_copy_p (tree type, bool assign_p)
8789 tree fns;
8791 if (assign_p)
8793 int ix;
8794 ix = lookup_fnfields_1 (type, ansi_assopname (NOP_EXPR));
8795 if (ix < 0)
8796 return false;
8797 fns = (*CLASSTYPE_METHOD_VEC (type))[ix];
8799 else if (TYPE_HAS_COPY_CTOR (type))
8801 /* If construction of the copy constructor was postponed, create
8802 it now. */
8803 if (CLASSTYPE_LAZY_COPY_CTOR (type))
8804 lazily_declare_fn (sfk_copy_constructor, type);
8805 if (CLASSTYPE_LAZY_MOVE_CTOR (type))
8806 lazily_declare_fn (sfk_move_constructor, type);
8807 fns = CLASSTYPE_CONSTRUCTORS (type);
8809 else
8810 return false;
8812 for (; fns; fns = OVL_NEXT (fns))
8814 tree fn = OVL_CURRENT (fns);
8816 if (assign_p)
8818 if (copy_fn_p (fn) == 0)
8819 continue;
8821 else if (copy_fn_p (fn) <= 0)
8822 continue;
8824 maybe_instantiate_noexcept (fn);
8825 if (!TYPE_NOTHROW_P (TREE_TYPE (fn)))
8826 return false;
8829 return true;
8832 /* Actually evaluates the trait. */
8834 static bool
8835 trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
8837 enum tree_code type_code1;
8838 tree t;
8840 type_code1 = TREE_CODE (type1);
8842 switch (kind)
8844 case CPTK_HAS_NOTHROW_ASSIGN:
8845 type1 = strip_array_types (type1);
8846 return (!CP_TYPE_CONST_P (type1) && type_code1 != REFERENCE_TYPE
8847 && (trait_expr_value (CPTK_HAS_TRIVIAL_ASSIGN, type1, type2)
8848 || (CLASS_TYPE_P (type1)
8849 && classtype_has_nothrow_assign_or_copy_p (type1,
8850 true))));
8852 case CPTK_HAS_TRIVIAL_ASSIGN:
8853 /* ??? The standard seems to be missing the "or array of such a class
8854 type" wording for this trait. */
8855 type1 = strip_array_types (type1);
8856 return (!CP_TYPE_CONST_P (type1) && type_code1 != REFERENCE_TYPE
8857 && (trivial_type_p (type1)
8858 || (CLASS_TYPE_P (type1)
8859 && TYPE_HAS_TRIVIAL_COPY_ASSIGN (type1))));
8861 case CPTK_HAS_NOTHROW_CONSTRUCTOR:
8862 type1 = strip_array_types (type1);
8863 return (trait_expr_value (CPTK_HAS_TRIVIAL_CONSTRUCTOR, type1, type2)
8864 || (CLASS_TYPE_P (type1)
8865 && (t = locate_ctor (type1))
8866 && (maybe_instantiate_noexcept (t),
8867 TYPE_NOTHROW_P (TREE_TYPE (t)))));
8869 case CPTK_HAS_TRIVIAL_CONSTRUCTOR:
8870 type1 = strip_array_types (type1);
8871 return (trivial_type_p (type1)
8872 || (CLASS_TYPE_P (type1) && TYPE_HAS_TRIVIAL_DFLT (type1)));
8874 case CPTK_HAS_NOTHROW_COPY:
8875 type1 = strip_array_types (type1);
8876 return (trait_expr_value (CPTK_HAS_TRIVIAL_COPY, type1, type2)
8877 || (CLASS_TYPE_P (type1)
8878 && classtype_has_nothrow_assign_or_copy_p (type1, false)));
8880 case CPTK_HAS_TRIVIAL_COPY:
8881 /* ??? The standard seems to be missing the "or array of such a class
8882 type" wording for this trait. */
8883 type1 = strip_array_types (type1);
8884 return (trivial_type_p (type1) || type_code1 == REFERENCE_TYPE
8885 || (CLASS_TYPE_P (type1) && TYPE_HAS_TRIVIAL_COPY_CTOR (type1)));
8887 case CPTK_HAS_TRIVIAL_DESTRUCTOR:
8888 type1 = strip_array_types (type1);
8889 return (trivial_type_p (type1) || type_code1 == REFERENCE_TYPE
8890 || (CLASS_TYPE_P (type1)
8891 && TYPE_HAS_TRIVIAL_DESTRUCTOR (type1)));
8893 case CPTK_HAS_VIRTUAL_DESTRUCTOR:
8894 return type_has_virtual_destructor (type1);
8896 case CPTK_IS_ABSTRACT:
8897 return (ABSTRACT_CLASS_TYPE_P (type1));
8899 case CPTK_IS_BASE_OF:
8900 return (NON_UNION_CLASS_TYPE_P (type1) && NON_UNION_CLASS_TYPE_P (type2)
8901 && (same_type_ignoring_top_level_qualifiers_p (type1, type2)
8902 || DERIVED_FROM_P (type1, type2)));
8904 case CPTK_IS_CLASS:
8905 return (NON_UNION_CLASS_TYPE_P (type1));
8907 case CPTK_IS_EMPTY:
8908 return (NON_UNION_CLASS_TYPE_P (type1) && CLASSTYPE_EMPTY_P (type1));
8910 case CPTK_IS_ENUM:
8911 return (type_code1 == ENUMERAL_TYPE);
8913 case CPTK_IS_FINAL:
8914 return (CLASS_TYPE_P (type1) && CLASSTYPE_FINAL (type1));
8916 case CPTK_IS_LITERAL_TYPE:
8917 return (literal_type_p (type1));
8919 case CPTK_IS_POD:
8920 return (pod_type_p (type1));
8922 case CPTK_IS_POLYMORPHIC:
8923 return (CLASS_TYPE_P (type1) && TYPE_POLYMORPHIC_P (type1));
8925 case CPTK_IS_SAME_AS:
8926 return same_type_p (type1, type2);
8928 case CPTK_IS_STD_LAYOUT:
8929 return (std_layout_type_p (type1));
8931 case CPTK_IS_TRIVIAL:
8932 return (trivial_type_p (type1));
8934 case CPTK_IS_TRIVIALLY_ASSIGNABLE:
8935 return is_trivially_xible (MODIFY_EXPR, type1, type2);
8937 case CPTK_IS_TRIVIALLY_CONSTRUCTIBLE:
8938 return is_trivially_xible (INIT_EXPR, type1, type2);
8940 case CPTK_IS_TRIVIALLY_COPYABLE:
8941 return (trivially_copyable_p (type1));
8943 case CPTK_IS_UNION:
8944 return (type_code1 == UNION_TYPE);
8946 default:
8947 gcc_unreachable ();
8948 return false;
8952 /* If TYPE is an array of unknown bound, or (possibly cv-qualified)
8953 void, or a complete type, returns true, otherwise false. */
8955 static bool
8956 check_trait_type (tree type)
8958 if (type == NULL_TREE)
8959 return true;
8961 if (TREE_CODE (type) == TREE_LIST)
8962 return (check_trait_type (TREE_VALUE (type))
8963 && check_trait_type (TREE_CHAIN (type)));
8965 if (TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type)
8966 && COMPLETE_TYPE_P (TREE_TYPE (type)))
8967 return true;
8969 if (VOID_TYPE_P (type))
8970 return true;
8972 return !!complete_type_or_else (strip_array_types (type), NULL_TREE);
8975 /* Process a trait expression. */
8977 tree
8978 finish_trait_expr (cp_trait_kind kind, tree type1, tree type2)
8980 if (type1 == error_mark_node
8981 || type2 == error_mark_node)
8982 return error_mark_node;
8984 if (processing_template_decl)
8986 tree trait_expr = make_node (TRAIT_EXPR);
8987 TREE_TYPE (trait_expr) = boolean_type_node;
8988 TRAIT_EXPR_TYPE1 (trait_expr) = type1;
8989 TRAIT_EXPR_TYPE2 (trait_expr) = type2;
8990 TRAIT_EXPR_KIND (trait_expr) = kind;
8991 return trait_expr;
8994 switch (kind)
8996 case CPTK_HAS_NOTHROW_ASSIGN:
8997 case CPTK_HAS_TRIVIAL_ASSIGN:
8998 case CPTK_HAS_NOTHROW_CONSTRUCTOR:
8999 case CPTK_HAS_TRIVIAL_CONSTRUCTOR:
9000 case CPTK_HAS_NOTHROW_COPY:
9001 case CPTK_HAS_TRIVIAL_COPY:
9002 case CPTK_HAS_TRIVIAL_DESTRUCTOR:
9003 case CPTK_HAS_VIRTUAL_DESTRUCTOR:
9004 case CPTK_IS_ABSTRACT:
9005 case CPTK_IS_EMPTY:
9006 case CPTK_IS_FINAL:
9007 case CPTK_IS_LITERAL_TYPE:
9008 case CPTK_IS_POD:
9009 case CPTK_IS_POLYMORPHIC:
9010 case CPTK_IS_STD_LAYOUT:
9011 case CPTK_IS_TRIVIAL:
9012 case CPTK_IS_TRIVIALLY_COPYABLE:
9013 if (!check_trait_type (type1))
9014 return error_mark_node;
9015 break;
9017 case CPTK_IS_TRIVIALLY_ASSIGNABLE:
9018 case CPTK_IS_TRIVIALLY_CONSTRUCTIBLE:
9019 if (!check_trait_type (type1)
9020 || !check_trait_type (type2))
9021 return error_mark_node;
9022 break;
9024 case CPTK_IS_BASE_OF:
9025 if (NON_UNION_CLASS_TYPE_P (type1) && NON_UNION_CLASS_TYPE_P (type2)
9026 && !same_type_ignoring_top_level_qualifiers_p (type1, type2)
9027 && !complete_type_or_else (type2, NULL_TREE))
9028 /* We already issued an error. */
9029 return error_mark_node;
9030 break;
9032 case CPTK_IS_CLASS:
9033 case CPTK_IS_ENUM:
9034 case CPTK_IS_UNION:
9035 case CPTK_IS_SAME_AS:
9036 break;
9038 default:
9039 gcc_unreachable ();
9042 return (trait_expr_value (kind, type1, type2)
9043 ? boolean_true_node : boolean_false_node);
9046 /* Do-nothing variants of functions to handle pragma FLOAT_CONST_DECIMAL64,
9047 which is ignored for C++. */
9049 void
9050 set_float_const_decimal64 (void)
9054 void
9055 clear_float_const_decimal64 (void)
9059 bool
9060 float_const_decimal64_p (void)
9062 return 0;
9066 /* Return true if T designates the implied `this' parameter. */
9068 bool
9069 is_this_parameter (tree t)
9071 if (!DECL_P (t) || DECL_NAME (t) != this_identifier)
9072 return false;
9073 gcc_assert (TREE_CODE (t) == PARM_DECL || is_capture_proxy (t));
9074 return true;
9077 /* Insert the deduced return type for an auto function. */
9079 void
9080 apply_deduced_return_type (tree fco, tree return_type)
9082 tree result;
9084 if (return_type == error_mark_node)
9085 return;
9087 if (LAMBDA_FUNCTION_P (fco))
9089 tree lambda = CLASSTYPE_LAMBDA_EXPR (current_class_type);
9090 LAMBDA_EXPR_RETURN_TYPE (lambda) = return_type;
9093 if (DECL_CONV_FN_P (fco))
9094 DECL_NAME (fco) = mangle_conv_op_name_for_type (return_type);
9096 TREE_TYPE (fco) = change_return_type (return_type, TREE_TYPE (fco));
9098 result = DECL_RESULT (fco);
9099 if (result == NULL_TREE)
9100 return;
9101 if (TREE_TYPE (result) == return_type)
9102 return;
9104 /* We already have a DECL_RESULT from start_preparsed_function.
9105 Now we need to redo the work it and allocate_struct_function
9106 did to reflect the new type. */
9107 gcc_assert (current_function_decl == fco);
9108 result = build_decl (input_location, RESULT_DECL, NULL_TREE,
9109 TYPE_MAIN_VARIANT (return_type));
9110 DECL_ARTIFICIAL (result) = 1;
9111 DECL_IGNORED_P (result) = 1;
9112 cp_apply_type_quals_to_decl (cp_type_quals (return_type),
9113 result);
9115 DECL_RESULT (fco) = result;
9117 if (!processing_template_decl)
9119 if (!VOID_TYPE_P (TREE_TYPE (result)))
9120 complete_type_or_else (TREE_TYPE (result), NULL_TREE);
9121 bool aggr = aggregate_value_p (result, fco);
9122 #ifdef PCC_STATIC_STRUCT_RETURN
9123 cfun->returns_pcc_struct = aggr;
9124 #endif
9125 cfun->returns_struct = aggr;
9130 /* DECL is a local variable or parameter from the surrounding scope of a
9131 lambda-expression. Returns the decltype for a use of the capture field
9132 for DECL even if it hasn't been captured yet. */
9134 static tree
9135 capture_decltype (tree decl)
9137 tree lam = CLASSTYPE_LAMBDA_EXPR (DECL_CONTEXT (current_function_decl));
9138 /* FIXME do lookup instead of list walk? */
9139 tree cap = value_member (decl, LAMBDA_EXPR_CAPTURE_LIST (lam));
9140 tree type;
9142 if (cap)
9143 type = TREE_TYPE (TREE_PURPOSE (cap));
9144 else
9145 switch (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lam))
9147 case CPLD_NONE:
9148 error ("%qD is not captured", decl);
9149 return error_mark_node;
9151 case CPLD_COPY:
9152 type = TREE_TYPE (decl);
9153 if (TREE_CODE (type) == REFERENCE_TYPE
9154 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
9155 type = TREE_TYPE (type);
9156 break;
9158 case CPLD_REFERENCE:
9159 type = TREE_TYPE (decl);
9160 if (TREE_CODE (type) != REFERENCE_TYPE)
9161 type = build_reference_type (TREE_TYPE (decl));
9162 break;
9164 default:
9165 gcc_unreachable ();
9168 if (TREE_CODE (type) != REFERENCE_TYPE)
9170 if (!LAMBDA_EXPR_MUTABLE_P (lam))
9171 type = cp_build_qualified_type (type, (cp_type_quals (type)
9172 |TYPE_QUAL_CONST));
9173 type = build_reference_type (type);
9175 return type;
9178 /* Build a unary fold expression of EXPR over OP. If IS_RIGHT is true,
9179 this is a right unary fold. Otherwise it is a left unary fold. */
9181 static tree
9182 finish_unary_fold_expr (tree expr, int op, tree_code dir)
9184 // Build a pack expansion (assuming expr has pack type).
9185 if (!uses_parameter_packs (expr))
9187 error_at (location_of (expr), "operand of fold expression has no "
9188 "unexpanded parameter packs");
9189 return error_mark_node;
9191 tree pack = make_pack_expansion (expr);
9193 // Build the fold expression.
9194 tree code = build_int_cstu (integer_type_node, abs (op));
9195 tree fold = build_min (dir, unknown_type_node, code, pack);
9196 FOLD_EXPR_MODIFY_P (fold) = (op < 0);
9197 return fold;
9200 tree
9201 finish_left_unary_fold_expr (tree expr, int op)
9203 return finish_unary_fold_expr (expr, op, UNARY_LEFT_FOLD_EXPR);
9206 tree
9207 finish_right_unary_fold_expr (tree expr, int op)
9209 return finish_unary_fold_expr (expr, op, UNARY_RIGHT_FOLD_EXPR);
9212 /* Build a binary fold expression over EXPR1 and EXPR2. The
9213 associativity of the fold is determined by EXPR1 and EXPR2 (whichever
9214 has an unexpanded parameter pack). */
9216 tree
9217 finish_binary_fold_expr (tree pack, tree init, int op, tree_code dir)
9219 pack = make_pack_expansion (pack);
9220 tree code = build_int_cstu (integer_type_node, abs (op));
9221 tree fold = build_min (dir, unknown_type_node, code, pack, init);
9222 FOLD_EXPR_MODIFY_P (fold) = (op < 0);
9223 return fold;
9226 tree
9227 finish_binary_fold_expr (tree expr1, tree expr2, int op)
9229 // Determine which expr has an unexpanded parameter pack and
9230 // set the pack and initial term.
9231 bool pack1 = uses_parameter_packs (expr1);
9232 bool pack2 = uses_parameter_packs (expr2);
9233 if (pack1 && !pack2)
9234 return finish_binary_fold_expr (expr1, expr2, op, BINARY_RIGHT_FOLD_EXPR);
9235 else if (pack2 && !pack1)
9236 return finish_binary_fold_expr (expr2, expr1, op, BINARY_LEFT_FOLD_EXPR);
9237 else
9239 if (pack1)
9240 error ("both arguments in binary fold have unexpanded parameter packs");
9241 else
9242 error ("no unexpanded parameter packs in binary fold");
9244 return error_mark_node;
9247 #include "gt-cp-semantics.h"