PR c++/84421 - type-dependent if constexpr
[official-gcc.git] / gcc / cp / semantics.c
blob35569d0cb0d8e4ec0c616751d03a9cd11edc7574
1 /* Perform the semantic phase of parsing, i.e., the process of
2 building tree structure, checking semantic consistency, and
3 building RTL. These routines are used both during actual parsing
4 and during the instantiation of template functions.
6 Copyright (C) 1998-2018 Free Software Foundation, Inc.
7 Written by Mark Mitchell (mmitchell@usa.net) based on code found
8 formerly in parse.y and pt.c.
10 This file is part of GCC.
12 GCC is free software; you can redistribute it and/or modify it
13 under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 3, or (at your option)
15 any later version.
17 GCC is distributed in the hope that it will be useful, but
18 WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with GCC; see the file COPYING3. If not see
24 <http://www.gnu.org/licenses/>. */
26 #include "config.h"
27 #include "system.h"
28 #include "coretypes.h"
29 #include "target.h"
30 #include "bitmap.h"
31 #include "cp-tree.h"
32 #include "stringpool.h"
33 #include "cgraph.h"
34 #include "stmt.h"
35 #include "varasm.h"
36 #include "stor-layout.h"
37 #include "c-family/c-objc.h"
38 #include "tree-inline.h"
39 #include "intl.h"
40 #include "tree-iterator.h"
41 #include "omp-general.h"
42 #include "convert.h"
43 #include "stringpool.h"
44 #include "attribs.h"
45 #include "gomp-constants.h"
46 #include "predict.h"
48 /* There routines provide a modular interface to perform many parsing
49 operations. They may therefore be used during actual parsing, or
50 during template instantiation, which may be regarded as a
51 degenerate form of parsing. */
53 static tree maybe_convert_cond (tree);
54 static tree finalize_nrv_r (tree *, int *, void *);
55 static tree capture_decltype (tree);
57 /* Used for OpenMP non-static data member privatization. */
59 static hash_map<tree, tree> *omp_private_member_map;
60 static vec<tree> omp_private_member_vec;
61 static bool omp_private_member_ignore_next;
64 /* Deferred Access Checking Overview
65 ---------------------------------
67 Most C++ expressions and declarations require access checking
68 to be performed during parsing. However, in several cases,
69 this has to be treated differently.
71 For member declarations, access checking has to be deferred
72 until more information about the declaration is known. For
73 example:
75 class A {
76 typedef int X;
77 public:
78 X f();
81 A::X A::f();
82 A::X g();
84 When we are parsing the function return type `A::X', we don't
85 really know if this is allowed until we parse the function name.
87 Furthermore, some contexts require that access checking is
88 never performed at all. These include class heads, and template
89 instantiations.
91 Typical use of access checking functions is described here:
93 1. When we enter a context that requires certain access checking
94 mode, the function `push_deferring_access_checks' is called with
95 DEFERRING argument specifying the desired mode. Access checking
96 may be performed immediately (dk_no_deferred), deferred
97 (dk_deferred), or not performed (dk_no_check).
99 2. When a declaration such as a type, or a variable, is encountered,
100 the function `perform_or_defer_access_check' is called. It
101 maintains a vector of all deferred checks.
103 3. The global `current_class_type' or `current_function_decl' is then
104 setup by the parser. `enforce_access' relies on these information
105 to check access.
107 4. Upon exiting the context mentioned in step 1,
108 `perform_deferred_access_checks' is called to check all declaration
109 stored in the vector. `pop_deferring_access_checks' is then
110 called to restore the previous access checking mode.
112 In case of parsing error, we simply call `pop_deferring_access_checks'
113 without `perform_deferred_access_checks'. */
115 struct GTY(()) deferred_access {
116 /* A vector representing name-lookups for which we have deferred
117 checking access controls. We cannot check the accessibility of
118 names used in a decl-specifier-seq until we know what is being
119 declared because code like:
121 class A {
122 class B {};
123 B* f();
126 A::B* A::f() { return 0; }
128 is valid, even though `A::B' is not generally accessible. */
129 vec<deferred_access_check, va_gc> * GTY(()) deferred_access_checks;
131 /* The current mode of access checks. */
132 enum deferring_kind deferring_access_checks_kind;
136 /* Data for deferred access checking. */
137 static GTY(()) vec<deferred_access, va_gc> *deferred_access_stack;
138 static GTY(()) unsigned deferred_access_no_check;
140 /* Save the current deferred access states and start deferred
141 access checking iff DEFER_P is true. */
143 void
144 push_deferring_access_checks (deferring_kind deferring)
146 /* For context like template instantiation, access checking
147 disabling applies to all nested context. */
148 if (deferred_access_no_check || deferring == dk_no_check)
149 deferred_access_no_check++;
150 else
152 deferred_access e = {NULL, deferring};
153 vec_safe_push (deferred_access_stack, e);
157 /* Save the current deferred access states and start deferred access
158 checking, continuing the set of deferred checks in CHECKS. */
160 void
161 reopen_deferring_access_checks (vec<deferred_access_check, va_gc> * checks)
163 push_deferring_access_checks (dk_deferred);
164 if (!deferred_access_no_check)
165 deferred_access_stack->last().deferred_access_checks = checks;
168 /* Resume deferring access checks again after we stopped doing
169 this previously. */
171 void
172 resume_deferring_access_checks (void)
174 if (!deferred_access_no_check)
175 deferred_access_stack->last().deferring_access_checks_kind = dk_deferred;
178 /* Stop deferring access checks. */
180 void
181 stop_deferring_access_checks (void)
183 if (!deferred_access_no_check)
184 deferred_access_stack->last().deferring_access_checks_kind = dk_no_deferred;
187 /* Discard the current deferred access checks and restore the
188 previous states. */
190 void
191 pop_deferring_access_checks (void)
193 if (deferred_access_no_check)
194 deferred_access_no_check--;
195 else
196 deferred_access_stack->pop ();
199 /* Returns a TREE_LIST representing the deferred checks.
200 The TREE_PURPOSE of each node is the type through which the
201 access occurred; the TREE_VALUE is the declaration named.
204 vec<deferred_access_check, va_gc> *
205 get_deferred_access_checks (void)
207 if (deferred_access_no_check)
208 return NULL;
209 else
210 return (deferred_access_stack->last().deferred_access_checks);
213 /* Take current deferred checks and combine with the
214 previous states if we also defer checks previously.
215 Otherwise perform checks now. */
217 void
218 pop_to_parent_deferring_access_checks (void)
220 if (deferred_access_no_check)
221 deferred_access_no_check--;
222 else
224 vec<deferred_access_check, va_gc> *checks;
225 deferred_access *ptr;
227 checks = (deferred_access_stack->last ().deferred_access_checks);
229 deferred_access_stack->pop ();
230 ptr = &deferred_access_stack->last ();
231 if (ptr->deferring_access_checks_kind == dk_no_deferred)
233 /* Check access. */
234 perform_access_checks (checks, tf_warning_or_error);
236 else
238 /* Merge with parent. */
239 int i, j;
240 deferred_access_check *chk, *probe;
242 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
244 FOR_EACH_VEC_SAFE_ELT (ptr->deferred_access_checks, j, probe)
246 if (probe->binfo == chk->binfo &&
247 probe->decl == chk->decl &&
248 probe->diag_decl == chk->diag_decl)
249 goto found;
251 /* Insert into parent's checks. */
252 vec_safe_push (ptr->deferred_access_checks, *chk);
253 found:;
259 /* Perform the access checks in CHECKS. The TREE_PURPOSE of each node
260 is the BINFO indicating the qualifying scope used to access the
261 DECL node stored in the TREE_VALUE of the node. If CHECKS is empty
262 or we aren't in SFINAE context or all the checks succeed return TRUE,
263 otherwise FALSE. */
265 bool
266 perform_access_checks (vec<deferred_access_check, va_gc> *checks,
267 tsubst_flags_t complain)
269 int i;
270 deferred_access_check *chk;
271 location_t loc = input_location;
272 bool ok = true;
274 if (!checks)
275 return true;
277 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
279 input_location = chk->loc;
280 ok &= enforce_access (chk->binfo, chk->decl, chk->diag_decl, complain);
283 input_location = loc;
284 return (complain & tf_error) ? true : ok;
287 /* Perform the deferred access checks.
289 After performing the checks, we still have to keep the list
290 `deferred_access_stack->deferred_access_checks' since we may want
291 to check access for them again later in a different context.
292 For example:
294 class A {
295 typedef int X;
296 static X a;
298 A::X A::a, x; // No error for `A::a', error for `x'
300 We have to perform deferred access of `A::X', first with `A::a',
301 next with `x'. Return value like perform_access_checks above. */
303 bool
304 perform_deferred_access_checks (tsubst_flags_t complain)
306 return perform_access_checks (get_deferred_access_checks (), complain);
309 /* Defer checking the accessibility of DECL, when looked up in
310 BINFO. DIAG_DECL is the declaration to use to print diagnostics.
311 Return value like perform_access_checks above.
312 If non-NULL, report failures to AFI. */
314 bool
315 perform_or_defer_access_check (tree binfo, tree decl, tree diag_decl,
316 tsubst_flags_t complain,
317 access_failure_info *afi)
319 int i;
320 deferred_access *ptr;
321 deferred_access_check *chk;
324 /* Exit if we are in a context that no access checking is performed.
326 if (deferred_access_no_check)
327 return true;
329 gcc_assert (TREE_CODE (binfo) == TREE_BINFO);
331 ptr = &deferred_access_stack->last ();
333 /* If we are not supposed to defer access checks, just check now. */
334 if (ptr->deferring_access_checks_kind == dk_no_deferred)
336 bool ok = enforce_access (binfo, decl, diag_decl, complain, afi);
337 return (complain & tf_error) ? true : ok;
340 /* See if we are already going to perform this check. */
341 FOR_EACH_VEC_SAFE_ELT (ptr->deferred_access_checks, i, chk)
343 if (chk->decl == decl && chk->binfo == binfo &&
344 chk->diag_decl == diag_decl)
346 return true;
349 /* If not, record the check. */
350 deferred_access_check new_access = {binfo, decl, diag_decl, input_location};
351 vec_safe_push (ptr->deferred_access_checks, new_access);
353 return true;
356 /* Returns nonzero if the current statement is a full expression,
357 i.e. temporaries created during that statement should be destroyed
358 at the end of the statement. */
361 stmts_are_full_exprs_p (void)
363 return current_stmt_tree ()->stmts_are_full_exprs_p;
366 /* T is a statement. Add it to the statement-tree. This is the C++
367 version. The C/ObjC frontends have a slightly different version of
368 this function. */
370 tree
371 add_stmt (tree t)
373 enum tree_code code = TREE_CODE (t);
375 if (EXPR_P (t) && code != LABEL_EXPR)
377 if (!EXPR_HAS_LOCATION (t))
378 SET_EXPR_LOCATION (t, input_location);
380 /* When we expand a statement-tree, we must know whether or not the
381 statements are full-expressions. We record that fact here. */
382 STMT_IS_FULL_EXPR_P (t) = stmts_are_full_exprs_p ();
385 if (code == LABEL_EXPR || code == CASE_LABEL_EXPR)
386 STATEMENT_LIST_HAS_LABEL (cur_stmt_list) = 1;
388 /* Add T to the statement-tree. Non-side-effect statements need to be
389 recorded during statement expressions. */
390 gcc_checking_assert (!stmt_list_stack->is_empty ());
391 append_to_statement_list_force (t, &cur_stmt_list);
393 return t;
396 /* Returns the stmt_tree to which statements are currently being added. */
398 stmt_tree
399 current_stmt_tree (void)
401 return (cfun
402 ? &cfun->language->base.x_stmt_tree
403 : &scope_chain->x_stmt_tree);
406 /* If statements are full expressions, wrap STMT in a CLEANUP_POINT_EXPR. */
408 static tree
409 maybe_cleanup_point_expr (tree expr)
411 if (!processing_template_decl && stmts_are_full_exprs_p ())
412 expr = fold_build_cleanup_point_expr (TREE_TYPE (expr), expr);
413 return expr;
416 /* Like maybe_cleanup_point_expr except have the type of the new expression be
417 void so we don't need to create a temporary variable to hold the inner
418 expression. The reason why we do this is because the original type might be
419 an aggregate and we cannot create a temporary variable for that type. */
421 tree
422 maybe_cleanup_point_expr_void (tree expr)
424 if (!processing_template_decl && stmts_are_full_exprs_p ())
425 expr = fold_build_cleanup_point_expr (void_type_node, expr);
426 return expr;
431 /* Create a declaration statement for the declaration given by the DECL. */
433 void
434 add_decl_expr (tree decl)
436 tree r = build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl);
437 if (DECL_INITIAL (decl)
438 || (DECL_SIZE (decl) && TREE_SIDE_EFFECTS (DECL_SIZE (decl))))
439 r = maybe_cleanup_point_expr_void (r);
440 add_stmt (r);
443 /* Finish a scope. */
445 tree
446 do_poplevel (tree stmt_list)
448 tree block = NULL;
450 if (stmts_are_full_exprs_p ())
451 block = poplevel (kept_level_p (), 1, 0);
453 stmt_list = pop_stmt_list (stmt_list);
455 if (!processing_template_decl)
457 stmt_list = c_build_bind_expr (input_location, block, stmt_list);
458 /* ??? See c_end_compound_stmt re statement expressions. */
461 return stmt_list;
464 /* Begin a new scope. */
466 static tree
467 do_pushlevel (scope_kind sk)
469 tree ret = push_stmt_list ();
470 if (stmts_are_full_exprs_p ())
471 begin_scope (sk, NULL);
472 return ret;
475 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
476 when the current scope is exited. EH_ONLY is true when this is not
477 meant to apply to normal control flow transfer. */
479 void
480 push_cleanup (tree decl, tree cleanup, bool eh_only)
482 tree stmt = build_stmt (input_location, CLEANUP_STMT, NULL, cleanup, decl);
483 CLEANUP_EH_ONLY (stmt) = eh_only;
484 add_stmt (stmt);
485 CLEANUP_BODY (stmt) = push_stmt_list ();
488 /* Simple infinite loop tracking for -Wreturn-type. We keep a stack of all
489 the current loops, represented by 'NULL_TREE' if we've seen a possible
490 exit, and 'error_mark_node' if not. This is currently used only to
491 suppress the warning about a function with no return statements, and
492 therefore we don't bother noting returns as possible exits. We also
493 don't bother with gotos. */
495 static void
496 begin_maybe_infinite_loop (tree cond)
498 /* Only track this while parsing a function, not during instantiation. */
499 if (!cfun || (DECL_TEMPLATE_INSTANTIATION (current_function_decl)
500 && !processing_template_decl))
501 return;
502 bool maybe_infinite = true;
503 if (cond)
505 cond = fold_non_dependent_expr (cond);
506 maybe_infinite = integer_nonzerop (cond);
508 vec_safe_push (cp_function_chain->infinite_loops,
509 maybe_infinite ? error_mark_node : NULL_TREE);
513 /* A break is a possible exit for the current loop. */
515 void
516 break_maybe_infinite_loop (void)
518 if (!cfun)
519 return;
520 cp_function_chain->infinite_loops->last() = NULL_TREE;
523 /* If we reach the end of the loop without seeing a possible exit, we have
524 an infinite loop. */
526 static void
527 end_maybe_infinite_loop (tree cond)
529 if (!cfun || (DECL_TEMPLATE_INSTANTIATION (current_function_decl)
530 && !processing_template_decl))
531 return;
532 tree current = cp_function_chain->infinite_loops->pop();
533 if (current != NULL_TREE)
535 cond = fold_non_dependent_expr (cond);
536 if (integer_nonzerop (cond))
537 current_function_infinite_loop = 1;
542 /* Begin a conditional that might contain a declaration. When generating
543 normal code, we want the declaration to appear before the statement
544 containing the conditional. When generating template code, we want the
545 conditional to be rendered as the raw DECL_EXPR. */
547 static void
548 begin_cond (tree *cond_p)
550 if (processing_template_decl)
551 *cond_p = push_stmt_list ();
554 /* Finish such a conditional. */
556 static void
557 finish_cond (tree *cond_p, tree expr)
559 if (processing_template_decl)
561 tree cond = pop_stmt_list (*cond_p);
563 if (expr == NULL_TREE)
564 /* Empty condition in 'for'. */
565 gcc_assert (empty_expr_stmt_p (cond));
566 else if (check_for_bare_parameter_packs (expr))
567 expr = error_mark_node;
568 else if (!empty_expr_stmt_p (cond))
569 expr = build2 (COMPOUND_EXPR, TREE_TYPE (expr), cond, expr);
571 *cond_p = expr;
574 /* If *COND_P specifies a conditional with a declaration, transform the
575 loop such that
576 while (A x = 42) { }
577 for (; A x = 42;) { }
578 becomes
579 while (true) { A x = 42; if (!x) break; }
580 for (;;) { A x = 42; if (!x) break; }
581 The statement list for BODY will be empty if the conditional did
582 not declare anything. */
584 static void
585 simplify_loop_decl_cond (tree *cond_p, tree body)
587 tree cond, if_stmt;
589 if (!TREE_SIDE_EFFECTS (body))
590 return;
592 cond = *cond_p;
593 *cond_p = boolean_true_node;
595 if_stmt = begin_if_stmt ();
596 cond = cp_build_unary_op (TRUTH_NOT_EXPR, cond, false, tf_warning_or_error);
597 finish_if_stmt_cond (cond, if_stmt);
598 finish_break_stmt ();
599 finish_then_clause (if_stmt);
600 finish_if_stmt (if_stmt);
603 /* Finish a goto-statement. */
605 tree
606 finish_goto_stmt (tree destination)
608 if (identifier_p (destination))
609 destination = lookup_label (destination);
611 /* We warn about unused labels with -Wunused. That means we have to
612 mark the used labels as used. */
613 if (TREE_CODE (destination) == LABEL_DECL)
614 TREE_USED (destination) = 1;
615 else
617 destination = mark_rvalue_use (destination);
618 if (!processing_template_decl)
620 destination = cp_convert (ptr_type_node, destination,
621 tf_warning_or_error);
622 if (error_operand_p (destination))
623 return NULL_TREE;
624 destination
625 = fold_build_cleanup_point_expr (TREE_TYPE (destination),
626 destination);
630 check_goto (destination);
632 add_stmt (build_predict_expr (PRED_GOTO, NOT_TAKEN));
633 return add_stmt (build_stmt (input_location, GOTO_EXPR, destination));
636 /* COND is the condition-expression for an if, while, etc.,
637 statement. Convert it to a boolean value, if appropriate.
638 In addition, verify sequence points if -Wsequence-point is enabled. */
640 static tree
641 maybe_convert_cond (tree cond)
643 /* Empty conditions remain empty. */
644 if (!cond)
645 return NULL_TREE;
647 /* Wait until we instantiate templates before doing conversion. */
648 if (processing_template_decl)
649 return cond;
651 if (warn_sequence_point)
652 verify_sequence_points (cond);
654 /* Do the conversion. */
655 cond = convert_from_reference (cond);
657 if (TREE_CODE (cond) == MODIFY_EXPR
658 && !TREE_NO_WARNING (cond)
659 && warn_parentheses)
661 warning_at (EXPR_LOC_OR_LOC (cond, input_location), OPT_Wparentheses,
662 "suggest parentheses around assignment used as truth value");
663 TREE_NO_WARNING (cond) = 1;
666 return condition_conversion (cond);
669 /* Finish an expression-statement, whose EXPRESSION is as indicated. */
671 tree
672 finish_expr_stmt (tree expr)
674 tree r = NULL_TREE;
675 location_t loc = EXPR_LOCATION (expr);
677 if (expr != NULL_TREE)
679 /* If we ran into a problem, make sure we complained. */
680 gcc_assert (expr != error_mark_node || seen_error ());
682 if (!processing_template_decl)
684 if (warn_sequence_point)
685 verify_sequence_points (expr);
686 expr = convert_to_void (expr, ICV_STATEMENT, tf_warning_or_error);
688 else if (!type_dependent_expression_p (expr))
689 convert_to_void (build_non_dependent_expr (expr), ICV_STATEMENT,
690 tf_warning_or_error);
692 if (check_for_bare_parameter_packs (expr))
693 expr = error_mark_node;
695 /* Simplification of inner statement expressions, compound exprs,
696 etc can result in us already having an EXPR_STMT. */
697 if (TREE_CODE (expr) != CLEANUP_POINT_EXPR)
699 if (TREE_CODE (expr) != EXPR_STMT)
700 expr = build_stmt (loc, EXPR_STMT, expr);
701 expr = maybe_cleanup_point_expr_void (expr);
704 r = add_stmt (expr);
707 return r;
711 /* Begin an if-statement. Returns a newly created IF_STMT if
712 appropriate. */
714 tree
715 begin_if_stmt (void)
717 tree r, scope;
718 scope = do_pushlevel (sk_cond);
719 r = build_stmt (input_location, IF_STMT, NULL_TREE,
720 NULL_TREE, NULL_TREE, scope);
721 current_binding_level->this_entity = r;
722 begin_cond (&IF_COND (r));
723 return r;
726 /* Process the COND of an if-statement, which may be given by
727 IF_STMT. */
729 tree
730 finish_if_stmt_cond (tree cond, tree if_stmt)
732 cond = maybe_convert_cond (cond);
733 if (IF_STMT_CONSTEXPR_P (if_stmt)
734 && !type_dependent_expression_p (cond)
735 && require_constant_expression (cond)
736 && !value_dependent_expression_p (cond))
738 cond = instantiate_non_dependent_expr (cond);
739 cond = cxx_constant_value (cond, NULL_TREE);
741 finish_cond (&IF_COND (if_stmt), cond);
742 add_stmt (if_stmt);
743 THEN_CLAUSE (if_stmt) = push_stmt_list ();
744 return cond;
747 /* Finish the then-clause of an if-statement, which may be given by
748 IF_STMT. */
750 tree
751 finish_then_clause (tree if_stmt)
753 THEN_CLAUSE (if_stmt) = pop_stmt_list (THEN_CLAUSE (if_stmt));
754 return if_stmt;
757 /* Begin the else-clause of an if-statement. */
759 void
760 begin_else_clause (tree if_stmt)
762 ELSE_CLAUSE (if_stmt) = push_stmt_list ();
765 /* Finish the else-clause of an if-statement, which may be given by
766 IF_STMT. */
768 void
769 finish_else_clause (tree if_stmt)
771 ELSE_CLAUSE (if_stmt) = pop_stmt_list (ELSE_CLAUSE (if_stmt));
774 /* Finish an if-statement. */
776 void
777 finish_if_stmt (tree if_stmt)
779 tree scope = IF_SCOPE (if_stmt);
780 IF_SCOPE (if_stmt) = NULL;
781 add_stmt (do_poplevel (scope));
784 /* Begin a while-statement. Returns a newly created WHILE_STMT if
785 appropriate. */
787 tree
788 begin_while_stmt (void)
790 tree r;
791 r = build_stmt (input_location, WHILE_STMT, NULL_TREE, NULL_TREE);
792 add_stmt (r);
793 WHILE_BODY (r) = do_pushlevel (sk_block);
794 begin_cond (&WHILE_COND (r));
795 return r;
798 /* Process the COND of a while-statement, which may be given by
799 WHILE_STMT. */
801 void
802 finish_while_stmt_cond (tree cond, tree while_stmt, bool ivdep,
803 unsigned short unroll)
805 cond = maybe_convert_cond (cond);
806 finish_cond (&WHILE_COND (while_stmt), cond);
807 begin_maybe_infinite_loop (cond);
808 if (ivdep && cond != error_mark_node)
809 WHILE_COND (while_stmt) = build3 (ANNOTATE_EXPR,
810 TREE_TYPE (WHILE_COND (while_stmt)),
811 WHILE_COND (while_stmt),
812 build_int_cst (integer_type_node,
813 annot_expr_ivdep_kind),
814 integer_zero_node);
815 if (unroll && cond != error_mark_node)
816 WHILE_COND (while_stmt) = build3 (ANNOTATE_EXPR,
817 TREE_TYPE (WHILE_COND (while_stmt)),
818 WHILE_COND (while_stmt),
819 build_int_cst (integer_type_node,
820 annot_expr_unroll_kind),
821 build_int_cst (integer_type_node,
822 unroll));
823 simplify_loop_decl_cond (&WHILE_COND (while_stmt), WHILE_BODY (while_stmt));
826 /* Finish a while-statement, which may be given by WHILE_STMT. */
828 void
829 finish_while_stmt (tree while_stmt)
831 end_maybe_infinite_loop (boolean_true_node);
832 WHILE_BODY (while_stmt) = do_poplevel (WHILE_BODY (while_stmt));
835 /* Begin a do-statement. Returns a newly created DO_STMT if
836 appropriate. */
838 tree
839 begin_do_stmt (void)
841 tree r = build_stmt (input_location, DO_STMT, NULL_TREE, NULL_TREE);
842 begin_maybe_infinite_loop (boolean_true_node);
843 add_stmt (r);
844 DO_BODY (r) = push_stmt_list ();
845 return r;
848 /* Finish the body of a do-statement, which may be given by DO_STMT. */
850 void
851 finish_do_body (tree do_stmt)
853 tree body = DO_BODY (do_stmt) = pop_stmt_list (DO_BODY (do_stmt));
855 if (TREE_CODE (body) == STATEMENT_LIST && STATEMENT_LIST_TAIL (body))
856 body = STATEMENT_LIST_TAIL (body)->stmt;
858 if (IS_EMPTY_STMT (body))
859 warning (OPT_Wempty_body,
860 "suggest explicit braces around empty body in %<do%> statement");
863 /* Finish a do-statement, which may be given by DO_STMT, and whose
864 COND is as indicated. */
866 void
867 finish_do_stmt (tree cond, tree do_stmt, bool ivdep, unsigned short unroll)
869 cond = maybe_convert_cond (cond);
870 end_maybe_infinite_loop (cond);
871 if (ivdep && cond != error_mark_node)
872 cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
873 build_int_cst (integer_type_node, annot_expr_ivdep_kind),
874 integer_zero_node);
875 if (unroll && cond != error_mark_node)
876 cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
877 build_int_cst (integer_type_node, annot_expr_unroll_kind),
878 build_int_cst (integer_type_node, unroll));
879 DO_COND (do_stmt) = cond;
882 /* Finish a return-statement. The EXPRESSION returned, if any, is as
883 indicated. */
885 tree
886 finish_return_stmt (tree expr)
888 tree r;
889 bool no_warning;
891 expr = check_return_expr (expr, &no_warning);
893 if (error_operand_p (expr)
894 || (flag_openmp && !check_omp_return ()))
896 /* Suppress -Wreturn-type for this function. */
897 if (warn_return_type)
898 TREE_NO_WARNING (current_function_decl) = true;
899 return error_mark_node;
902 if (!processing_template_decl)
904 if (warn_sequence_point)
905 verify_sequence_points (expr);
907 if (DECL_DESTRUCTOR_P (current_function_decl)
908 || (DECL_CONSTRUCTOR_P (current_function_decl)
909 && targetm.cxx.cdtor_returns_this ()))
911 /* Similarly, all destructors must run destructors for
912 base-classes before returning. So, all returns in a
913 destructor get sent to the DTOR_LABEL; finish_function emits
914 code to return a value there. */
915 return finish_goto_stmt (cdtor_label);
919 r = build_stmt (input_location, RETURN_EXPR, expr);
920 TREE_NO_WARNING (r) |= no_warning;
921 r = maybe_cleanup_point_expr_void (r);
922 r = add_stmt (r);
924 return r;
927 /* Begin the scope of a for-statement or a range-for-statement.
928 Both the returned trees are to be used in a call to
929 begin_for_stmt or begin_range_for_stmt. */
931 tree
932 begin_for_scope (tree *init)
934 tree scope = NULL_TREE;
935 if (flag_new_for_scope)
936 scope = do_pushlevel (sk_for);
938 if (processing_template_decl)
939 *init = push_stmt_list ();
940 else
941 *init = NULL_TREE;
943 return scope;
946 /* Begin a for-statement. Returns a new FOR_STMT.
947 SCOPE and INIT should be the return of begin_for_scope,
948 or both NULL_TREE */
950 tree
951 begin_for_stmt (tree scope, tree init)
953 tree r;
955 r = build_stmt (input_location, FOR_STMT, NULL_TREE, NULL_TREE,
956 NULL_TREE, NULL_TREE, NULL_TREE);
958 if (scope == NULL_TREE)
960 gcc_assert (!init || !flag_new_for_scope);
961 if (!init)
962 scope = begin_for_scope (&init);
964 FOR_INIT_STMT (r) = init;
965 FOR_SCOPE (r) = scope;
967 return r;
970 /* Finish the init-statement of a for-statement, which may be
971 given by FOR_STMT. */
973 void
974 finish_init_stmt (tree for_stmt)
976 if (processing_template_decl)
977 FOR_INIT_STMT (for_stmt) = pop_stmt_list (FOR_INIT_STMT (for_stmt));
978 add_stmt (for_stmt);
979 FOR_BODY (for_stmt) = do_pushlevel (sk_block);
980 begin_cond (&FOR_COND (for_stmt));
983 /* Finish the COND of a for-statement, which may be given by
984 FOR_STMT. */
986 void
987 finish_for_cond (tree cond, tree for_stmt, bool ivdep, unsigned short unroll)
989 cond = maybe_convert_cond (cond);
990 finish_cond (&FOR_COND (for_stmt), cond);
991 begin_maybe_infinite_loop (cond);
992 if (ivdep && cond != error_mark_node)
993 FOR_COND (for_stmt) = build3 (ANNOTATE_EXPR,
994 TREE_TYPE (FOR_COND (for_stmt)),
995 FOR_COND (for_stmt),
996 build_int_cst (integer_type_node,
997 annot_expr_ivdep_kind),
998 integer_zero_node);
999 if (unroll && cond != error_mark_node)
1000 FOR_COND (for_stmt) = build3 (ANNOTATE_EXPR,
1001 TREE_TYPE (FOR_COND (for_stmt)),
1002 FOR_COND (for_stmt),
1003 build_int_cst (integer_type_node,
1004 annot_expr_unroll_kind),
1005 build_int_cst (integer_type_node,
1006 unroll));
1007 simplify_loop_decl_cond (&FOR_COND (for_stmt), FOR_BODY (for_stmt));
1010 /* Finish the increment-EXPRESSION in a for-statement, which may be
1011 given by FOR_STMT. */
1013 void
1014 finish_for_expr (tree expr, tree for_stmt)
1016 if (!expr)
1017 return;
1018 /* If EXPR is an overloaded function, issue an error; there is no
1019 context available to use to perform overload resolution. */
1020 if (type_unknown_p (expr))
1022 cxx_incomplete_type_error (expr, TREE_TYPE (expr));
1023 expr = error_mark_node;
1025 if (!processing_template_decl)
1027 if (warn_sequence_point)
1028 verify_sequence_points (expr);
1029 expr = convert_to_void (expr, ICV_THIRD_IN_FOR,
1030 tf_warning_or_error);
1032 else if (!type_dependent_expression_p (expr))
1033 convert_to_void (build_non_dependent_expr (expr), ICV_THIRD_IN_FOR,
1034 tf_warning_or_error);
1035 expr = maybe_cleanup_point_expr_void (expr);
1036 if (check_for_bare_parameter_packs (expr))
1037 expr = error_mark_node;
1038 FOR_EXPR (for_stmt) = expr;
1041 /* Finish the body of a for-statement, which may be given by
1042 FOR_STMT. The increment-EXPR for the loop must be
1043 provided.
1044 It can also finish RANGE_FOR_STMT. */
1046 void
1047 finish_for_stmt (tree for_stmt)
1049 end_maybe_infinite_loop (boolean_true_node);
1051 if (TREE_CODE (for_stmt) == RANGE_FOR_STMT)
1052 RANGE_FOR_BODY (for_stmt) = do_poplevel (RANGE_FOR_BODY (for_stmt));
1053 else
1054 FOR_BODY (for_stmt) = do_poplevel (FOR_BODY (for_stmt));
1056 /* Pop the scope for the body of the loop. */
1057 if (flag_new_for_scope)
1059 tree scope;
1060 tree *scope_ptr = (TREE_CODE (for_stmt) == RANGE_FOR_STMT
1061 ? &RANGE_FOR_SCOPE (for_stmt)
1062 : &FOR_SCOPE (for_stmt));
1063 scope = *scope_ptr;
1064 *scope_ptr = NULL;
1065 add_stmt (do_poplevel (scope));
1069 /* Begin a range-for-statement. Returns a new RANGE_FOR_STMT.
1070 SCOPE and INIT should be the return of begin_for_scope,
1071 or both NULL_TREE .
1072 To finish it call finish_for_stmt(). */
1074 tree
1075 begin_range_for_stmt (tree scope, tree init)
1077 tree r;
1079 begin_maybe_infinite_loop (boolean_false_node);
1081 r = build_stmt (input_location, RANGE_FOR_STMT,
1082 NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE);
1084 if (scope == NULL_TREE)
1086 gcc_assert (!init || !flag_new_for_scope);
1087 if (!init)
1088 scope = begin_for_scope (&init);
1091 /* RANGE_FOR_STMTs do not use nor save the init tree, so we
1092 pop it now. */
1093 if (init)
1094 pop_stmt_list (init);
1095 RANGE_FOR_SCOPE (r) = scope;
1097 return r;
1100 /* Finish the head of a range-based for statement, which may
1101 be given by RANGE_FOR_STMT. DECL must be the declaration
1102 and EXPR must be the loop expression. */
1104 void
1105 finish_range_for_decl (tree range_for_stmt, tree decl, tree expr)
1107 RANGE_FOR_DECL (range_for_stmt) = decl;
1108 RANGE_FOR_EXPR (range_for_stmt) = expr;
1109 add_stmt (range_for_stmt);
1110 RANGE_FOR_BODY (range_for_stmt) = do_pushlevel (sk_block);
1113 /* Finish a break-statement. */
1115 tree
1116 finish_break_stmt (void)
1118 /* In switch statements break is sometimes stylistically used after
1119 a return statement. This can lead to spurious warnings about
1120 control reaching the end of a non-void function when it is
1121 inlined. Note that we are calling block_may_fallthru with
1122 language specific tree nodes; this works because
1123 block_may_fallthru returns true when given something it does not
1124 understand. */
1125 if (!block_may_fallthru (cur_stmt_list))
1126 return void_node;
1127 note_break_stmt ();
1128 return add_stmt (build_stmt (input_location, BREAK_STMT));
1131 /* Finish a continue-statement. */
1133 tree
1134 finish_continue_stmt (void)
1136 return add_stmt (build_stmt (input_location, CONTINUE_STMT));
1139 /* Begin a switch-statement. Returns a new SWITCH_STMT if
1140 appropriate. */
1142 tree
1143 begin_switch_stmt (void)
1145 tree r, scope;
1147 scope = do_pushlevel (sk_cond);
1148 r = build_stmt (input_location, SWITCH_STMT, NULL_TREE, NULL_TREE, NULL_TREE, scope);
1150 begin_cond (&SWITCH_STMT_COND (r));
1152 return r;
1155 /* Finish the cond of a switch-statement. */
1157 void
1158 finish_switch_cond (tree cond, tree switch_stmt)
1160 tree orig_type = NULL;
1162 if (!processing_template_decl)
1164 /* Convert the condition to an integer or enumeration type. */
1165 cond = build_expr_type_conversion (WANT_INT | WANT_ENUM, cond, true);
1166 if (cond == NULL_TREE)
1168 error ("switch quantity not an integer");
1169 cond = error_mark_node;
1171 /* We want unlowered type here to handle enum bit-fields. */
1172 orig_type = unlowered_expr_type (cond);
1173 if (TREE_CODE (orig_type) != ENUMERAL_TYPE)
1174 orig_type = TREE_TYPE (cond);
1175 if (cond != error_mark_node)
1177 /* [stmt.switch]
1179 Integral promotions are performed. */
1180 cond = perform_integral_promotions (cond);
1181 cond = maybe_cleanup_point_expr (cond);
1184 if (check_for_bare_parameter_packs (cond))
1185 cond = error_mark_node;
1186 else if (!processing_template_decl && warn_sequence_point)
1187 verify_sequence_points (cond);
1189 finish_cond (&SWITCH_STMT_COND (switch_stmt), cond);
1190 SWITCH_STMT_TYPE (switch_stmt) = orig_type;
1191 add_stmt (switch_stmt);
1192 push_switch (switch_stmt);
1193 SWITCH_STMT_BODY (switch_stmt) = push_stmt_list ();
1196 /* Finish the body of a switch-statement, which may be given by
1197 SWITCH_STMT. The COND to switch on is indicated. */
1199 void
1200 finish_switch_stmt (tree switch_stmt)
1202 tree scope;
1204 SWITCH_STMT_BODY (switch_stmt) =
1205 pop_stmt_list (SWITCH_STMT_BODY (switch_stmt));
1206 pop_switch ();
1208 scope = SWITCH_STMT_SCOPE (switch_stmt);
1209 SWITCH_STMT_SCOPE (switch_stmt) = NULL;
1210 add_stmt (do_poplevel (scope));
1213 /* Begin a try-block. Returns a newly-created TRY_BLOCK if
1214 appropriate. */
1216 tree
1217 begin_try_block (void)
1219 tree r = build_stmt (input_location, TRY_BLOCK, NULL_TREE, NULL_TREE);
1220 add_stmt (r);
1221 TRY_STMTS (r) = push_stmt_list ();
1222 return r;
1225 /* Likewise, for a function-try-block. The block returned in
1226 *COMPOUND_STMT is an artificial outer scope, containing the
1227 function-try-block. */
1229 tree
1230 begin_function_try_block (tree *compound_stmt)
1232 tree r;
1233 /* This outer scope does not exist in the C++ standard, but we need
1234 a place to put __FUNCTION__ and similar variables. */
1235 *compound_stmt = begin_compound_stmt (0);
1236 r = begin_try_block ();
1237 FN_TRY_BLOCK_P (r) = 1;
1238 return r;
1241 /* Finish a try-block, which may be given by TRY_BLOCK. */
1243 void
1244 finish_try_block (tree try_block)
1246 TRY_STMTS (try_block) = pop_stmt_list (TRY_STMTS (try_block));
1247 TRY_HANDLERS (try_block) = push_stmt_list ();
1250 /* Finish the body of a cleanup try-block, which may be given by
1251 TRY_BLOCK. */
1253 void
1254 finish_cleanup_try_block (tree try_block)
1256 TRY_STMTS (try_block) = pop_stmt_list (TRY_STMTS (try_block));
1259 /* Finish an implicitly generated try-block, with a cleanup is given
1260 by CLEANUP. */
1262 void
1263 finish_cleanup (tree cleanup, tree try_block)
1265 TRY_HANDLERS (try_block) = cleanup;
1266 CLEANUP_P (try_block) = 1;
1269 /* Likewise, for a function-try-block. */
1271 void
1272 finish_function_try_block (tree try_block)
1274 finish_try_block (try_block);
1275 /* FIXME : something queer about CTOR_INITIALIZER somehow following
1276 the try block, but moving it inside. */
1277 in_function_try_handler = 1;
1280 /* Finish a handler-sequence for a try-block, which may be given by
1281 TRY_BLOCK. */
1283 void
1284 finish_handler_sequence (tree try_block)
1286 TRY_HANDLERS (try_block) = pop_stmt_list (TRY_HANDLERS (try_block));
1287 check_handlers (TRY_HANDLERS (try_block));
1290 /* Finish the handler-seq for a function-try-block, given by
1291 TRY_BLOCK. COMPOUND_STMT is the outer block created by
1292 begin_function_try_block. */
1294 void
1295 finish_function_handler_sequence (tree try_block, tree compound_stmt)
1297 in_function_try_handler = 0;
1298 finish_handler_sequence (try_block);
1299 finish_compound_stmt (compound_stmt);
1302 /* Begin a handler. Returns a HANDLER if appropriate. */
1304 tree
1305 begin_handler (void)
1307 tree r;
1309 r = build_stmt (input_location, HANDLER, NULL_TREE, NULL_TREE);
1310 add_stmt (r);
1312 /* Create a binding level for the eh_info and the exception object
1313 cleanup. */
1314 HANDLER_BODY (r) = do_pushlevel (sk_catch);
1316 return r;
1319 /* Finish the handler-parameters for a handler, which may be given by
1320 HANDLER. DECL is the declaration for the catch parameter, or NULL
1321 if this is a `catch (...)' clause. */
1323 void
1324 finish_handler_parms (tree decl, tree handler)
1326 tree type = NULL_TREE;
1327 if (processing_template_decl)
1329 if (decl)
1331 decl = pushdecl (decl);
1332 decl = push_template_decl (decl);
1333 HANDLER_PARMS (handler) = decl;
1334 type = TREE_TYPE (decl);
1337 else
1339 type = expand_start_catch_block (decl);
1340 if (warn_catch_value
1341 && type != NULL_TREE
1342 && type != error_mark_node
1343 && TREE_CODE (TREE_TYPE (decl)) != REFERENCE_TYPE)
1345 tree orig_type = TREE_TYPE (decl);
1346 if (CLASS_TYPE_P (orig_type))
1348 if (TYPE_POLYMORPHIC_P (orig_type))
1349 warning (OPT_Wcatch_value_,
1350 "catching polymorphic type %q#T by value", orig_type);
1351 else if (warn_catch_value > 1)
1352 warning (OPT_Wcatch_value_,
1353 "catching type %q#T by value", orig_type);
1355 else if (warn_catch_value > 2)
1356 warning (OPT_Wcatch_value_,
1357 "catching non-reference type %q#T", orig_type);
1360 HANDLER_TYPE (handler) = type;
1363 /* Finish a handler, which may be given by HANDLER. The BLOCKs are
1364 the return value from the matching call to finish_handler_parms. */
1366 void
1367 finish_handler (tree handler)
1369 if (!processing_template_decl)
1370 expand_end_catch_block ();
1371 HANDLER_BODY (handler) = do_poplevel (HANDLER_BODY (handler));
1374 /* Begin a compound statement. FLAGS contains some bits that control the
1375 behavior and context. If BCS_NO_SCOPE is set, the compound statement
1376 does not define a scope. If BCS_FN_BODY is set, this is the outermost
1377 block of a function. If BCS_TRY_BLOCK is set, this is the block
1378 created on behalf of a TRY statement. Returns a token to be passed to
1379 finish_compound_stmt. */
1381 tree
1382 begin_compound_stmt (unsigned int flags)
1384 tree r;
1386 if (flags & BCS_NO_SCOPE)
1388 r = push_stmt_list ();
1389 STATEMENT_LIST_NO_SCOPE (r) = 1;
1391 /* Normally, we try hard to keep the BLOCK for a statement-expression.
1392 But, if it's a statement-expression with a scopeless block, there's
1393 nothing to keep, and we don't want to accidentally keep a block
1394 *inside* the scopeless block. */
1395 keep_next_level (false);
1397 else
1399 scope_kind sk = sk_block;
1400 if (flags & BCS_TRY_BLOCK)
1401 sk = sk_try;
1402 else if (flags & BCS_TRANSACTION)
1403 sk = sk_transaction;
1404 r = do_pushlevel (sk);
1407 /* When processing a template, we need to remember where the braces were,
1408 so that we can set up identical scopes when instantiating the template
1409 later. BIND_EXPR is a handy candidate for this.
1410 Note that do_poplevel won't create a BIND_EXPR itself here (and thus
1411 result in nested BIND_EXPRs), since we don't build BLOCK nodes when
1412 processing templates. */
1413 if (processing_template_decl)
1415 r = build3 (BIND_EXPR, NULL, NULL, r, NULL);
1416 BIND_EXPR_TRY_BLOCK (r) = (flags & BCS_TRY_BLOCK) != 0;
1417 BIND_EXPR_BODY_BLOCK (r) = (flags & BCS_FN_BODY) != 0;
1418 TREE_SIDE_EFFECTS (r) = 1;
1421 return r;
1424 /* Finish a compound-statement, which is given by STMT. */
1426 void
1427 finish_compound_stmt (tree stmt)
1429 if (TREE_CODE (stmt) == BIND_EXPR)
1431 tree body = do_poplevel (BIND_EXPR_BODY (stmt));
1432 /* If the STATEMENT_LIST is empty and this BIND_EXPR isn't special,
1433 discard the BIND_EXPR so it can be merged with the containing
1434 STATEMENT_LIST. */
1435 if (TREE_CODE (body) == STATEMENT_LIST
1436 && STATEMENT_LIST_HEAD (body) == NULL
1437 && !BIND_EXPR_BODY_BLOCK (stmt)
1438 && !BIND_EXPR_TRY_BLOCK (stmt))
1439 stmt = body;
1440 else
1441 BIND_EXPR_BODY (stmt) = body;
1443 else if (STATEMENT_LIST_NO_SCOPE (stmt))
1444 stmt = pop_stmt_list (stmt);
1445 else
1447 /* Destroy any ObjC "super" receivers that may have been
1448 created. */
1449 objc_clear_super_receiver ();
1451 stmt = do_poplevel (stmt);
1454 /* ??? See c_end_compound_stmt wrt statement expressions. */
1455 add_stmt (stmt);
1458 /* Finish an asm-statement, whose components are a STRING, some
1459 OUTPUT_OPERANDS, some INPUT_OPERANDS, some CLOBBERS and some
1460 LABELS. Also note whether the asm-statement should be
1461 considered volatile. */
1463 tree
1464 finish_asm_stmt (int volatile_p, tree string, tree output_operands,
1465 tree input_operands, tree clobbers, tree labels)
1467 tree r;
1468 tree t;
1469 int ninputs = list_length (input_operands);
1470 int noutputs = list_length (output_operands);
1472 if (!processing_template_decl)
1474 const char *constraint;
1475 const char **oconstraints;
1476 bool allows_mem, allows_reg, is_inout;
1477 tree operand;
1478 int i;
1480 oconstraints = XALLOCAVEC (const char *, noutputs);
1482 string = resolve_asm_operand_names (string, output_operands,
1483 input_operands, labels);
1485 for (i = 0, t = output_operands; t; t = TREE_CHAIN (t), ++i)
1487 operand = TREE_VALUE (t);
1489 /* ??? Really, this should not be here. Users should be using a
1490 proper lvalue, dammit. But there's a long history of using
1491 casts in the output operands. In cases like longlong.h, this
1492 becomes a primitive form of typechecking -- if the cast can be
1493 removed, then the output operand had a type of the proper width;
1494 otherwise we'll get an error. Gross, but ... */
1495 STRIP_NOPS (operand);
1497 operand = mark_lvalue_use (operand);
1499 if (!lvalue_or_else (operand, lv_asm, tf_warning_or_error))
1500 operand = error_mark_node;
1502 if (operand != error_mark_node
1503 && (TREE_READONLY (operand)
1504 || CP_TYPE_CONST_P (TREE_TYPE (operand))
1505 /* Functions are not modifiable, even though they are
1506 lvalues. */
1507 || TREE_CODE (TREE_TYPE (operand)) == FUNCTION_TYPE
1508 || TREE_CODE (TREE_TYPE (operand)) == METHOD_TYPE
1509 /* If it's an aggregate and any field is const, then it is
1510 effectively const. */
1511 || (CLASS_TYPE_P (TREE_TYPE (operand))
1512 && C_TYPE_FIELDS_READONLY (TREE_TYPE (operand)))))
1513 cxx_readonly_error (operand, lv_asm);
1515 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
1516 oconstraints[i] = constraint;
1518 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
1519 &allows_mem, &allows_reg, &is_inout))
1521 /* If the operand is going to end up in memory,
1522 mark it addressable. */
1523 if (!allows_reg && !cxx_mark_addressable (operand))
1524 operand = error_mark_node;
1526 else
1527 operand = error_mark_node;
1529 TREE_VALUE (t) = operand;
1532 for (i = 0, t = input_operands; t; ++i, t = TREE_CHAIN (t))
1534 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
1535 bool constraint_parsed
1536 = parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
1537 oconstraints, &allows_mem, &allows_reg);
1538 /* If the operand is going to end up in memory, don't call
1539 decay_conversion. */
1540 if (constraint_parsed && !allows_reg && allows_mem)
1541 operand = mark_lvalue_use (TREE_VALUE (t));
1542 else
1543 operand = decay_conversion (TREE_VALUE (t), tf_warning_or_error);
1545 /* If the type of the operand hasn't been determined (e.g.,
1546 because it involves an overloaded function), then issue
1547 an error message. There's no context available to
1548 resolve the overloading. */
1549 if (TREE_TYPE (operand) == unknown_type_node)
1551 error ("type of asm operand %qE could not be determined",
1552 TREE_VALUE (t));
1553 operand = error_mark_node;
1556 if (constraint_parsed)
1558 /* If the operand is going to end up in memory,
1559 mark it addressable. */
1560 if (!allows_reg && allows_mem)
1562 /* Strip the nops as we allow this case. FIXME, this really
1563 should be rejected or made deprecated. */
1564 STRIP_NOPS (operand);
1565 if (!cxx_mark_addressable (operand))
1566 operand = error_mark_node;
1568 else if (!allows_reg && !allows_mem)
1570 /* If constraint allows neither register nor memory,
1571 try harder to get a constant. */
1572 tree constop = maybe_constant_value (operand);
1573 if (TREE_CONSTANT (constop))
1574 operand = constop;
1577 else
1578 operand = error_mark_node;
1580 TREE_VALUE (t) = operand;
1584 r = build_stmt (input_location, ASM_EXPR, string,
1585 output_operands, input_operands,
1586 clobbers, labels);
1587 ASM_VOLATILE_P (r) = volatile_p || noutputs == 0;
1588 r = maybe_cleanup_point_expr_void (r);
1589 return add_stmt (r);
1592 /* Finish a label with the indicated NAME. Returns the new label. */
1594 tree
1595 finish_label_stmt (tree name)
1597 tree decl = define_label (input_location, name);
1599 if (decl == error_mark_node)
1600 return error_mark_node;
1602 add_stmt (build_stmt (input_location, LABEL_EXPR, decl));
1604 return decl;
1607 /* Finish a series of declarations for local labels. G++ allows users
1608 to declare "local" labels, i.e., labels with scope. This extension
1609 is useful when writing code involving statement-expressions. */
1611 void
1612 finish_label_decl (tree name)
1614 if (!at_function_scope_p ())
1616 error ("__label__ declarations are only allowed in function scopes");
1617 return;
1620 add_decl_expr (declare_local_label (name));
1623 /* When DECL goes out of scope, make sure that CLEANUP is executed. */
1625 void
1626 finish_decl_cleanup (tree decl, tree cleanup)
1628 push_cleanup (decl, cleanup, false);
1631 /* If the current scope exits with an exception, run CLEANUP. */
1633 void
1634 finish_eh_cleanup (tree cleanup)
1636 push_cleanup (NULL, cleanup, true);
1639 /* The MEM_INITS is a list of mem-initializers, in reverse of the
1640 order they were written by the user. Each node is as for
1641 emit_mem_initializers. */
1643 void
1644 finish_mem_initializers (tree mem_inits)
1646 /* Reorder the MEM_INITS so that they are in the order they appeared
1647 in the source program. */
1648 mem_inits = nreverse (mem_inits);
1650 if (processing_template_decl)
1652 tree mem;
1654 for (mem = mem_inits; mem; mem = TREE_CHAIN (mem))
1656 /* If the TREE_PURPOSE is a TYPE_PACK_EXPANSION, skip the
1657 check for bare parameter packs in the TREE_VALUE, because
1658 any parameter packs in the TREE_VALUE have already been
1659 bound as part of the TREE_PURPOSE. See
1660 make_pack_expansion for more information. */
1661 if (TREE_CODE (TREE_PURPOSE (mem)) != TYPE_PACK_EXPANSION
1662 && check_for_bare_parameter_packs (TREE_VALUE (mem)))
1663 TREE_VALUE (mem) = error_mark_node;
1666 add_stmt (build_min_nt_loc (UNKNOWN_LOCATION,
1667 CTOR_INITIALIZER, mem_inits));
1669 else
1670 emit_mem_initializers (mem_inits);
1673 /* Obfuscate EXPR if it looks like an id-expression or member access so
1674 that the call to finish_decltype in do_auto_deduction will give the
1675 right result. */
1677 tree
1678 force_paren_expr (tree expr)
1680 /* This is only needed for decltype(auto) in C++14. */
1681 if (cxx_dialect < cxx14)
1682 return expr;
1684 /* If we're in unevaluated context, we can't be deducing a
1685 return/initializer type, so we don't need to mess with this. */
1686 if (cp_unevaluated_operand)
1687 return expr;
1689 if (!DECL_P (expr) && TREE_CODE (expr) != COMPONENT_REF
1690 && TREE_CODE (expr) != SCOPE_REF)
1691 return expr;
1693 if (TREE_CODE (expr) == COMPONENT_REF
1694 || TREE_CODE (expr) == SCOPE_REF)
1695 REF_PARENTHESIZED_P (expr) = true;
1696 else if (type_dependent_expression_p (expr))
1697 expr = build1 (PAREN_EXPR, TREE_TYPE (expr), expr);
1698 else if (VAR_P (expr) && DECL_HARD_REGISTER (expr))
1699 /* We can't bind a hard register variable to a reference. */;
1700 else
1702 cp_lvalue_kind kind = lvalue_kind (expr);
1703 if ((kind & ~clk_class) != clk_none)
1705 tree type = unlowered_expr_type (expr);
1706 bool rval = !!(kind & clk_rvalueref);
1707 type = cp_build_reference_type (type, rval);
1708 /* This inhibits warnings in, eg, cxx_mark_addressable
1709 (c++/60955). */
1710 warning_sentinel s (extra_warnings);
1711 expr = build_static_cast (type, expr, tf_error);
1712 if (expr != error_mark_node)
1713 REF_PARENTHESIZED_P (expr) = true;
1717 return expr;
1720 /* If T is an id-expression obfuscated by force_paren_expr, undo the
1721 obfuscation and return the underlying id-expression. Otherwise
1722 return T. */
1724 tree
1725 maybe_undo_parenthesized_ref (tree t)
1727 if (cxx_dialect >= cxx14
1728 && INDIRECT_REF_P (t)
1729 && REF_PARENTHESIZED_P (t))
1731 t = TREE_OPERAND (t, 0);
1732 while (TREE_CODE (t) == NON_LVALUE_EXPR
1733 || TREE_CODE (t) == NOP_EXPR)
1734 t = TREE_OPERAND (t, 0);
1736 gcc_assert (TREE_CODE (t) == ADDR_EXPR
1737 || TREE_CODE (t) == STATIC_CAST_EXPR);
1738 t = TREE_OPERAND (t, 0);
1741 return t;
1744 /* Finish a parenthesized expression EXPR. */
1746 cp_expr
1747 finish_parenthesized_expr (cp_expr expr)
1749 if (EXPR_P (expr))
1750 /* This inhibits warnings in c_common_truthvalue_conversion. */
1751 TREE_NO_WARNING (expr) = 1;
1753 if (TREE_CODE (expr) == OFFSET_REF
1754 || TREE_CODE (expr) == SCOPE_REF)
1755 /* [expr.unary.op]/3 The qualified id of a pointer-to-member must not be
1756 enclosed in parentheses. */
1757 PTRMEM_OK_P (expr) = 0;
1759 if (TREE_CODE (expr) == STRING_CST)
1760 PAREN_STRING_LITERAL_P (expr) = 1;
1762 expr = cp_expr (force_paren_expr (expr), expr.get_location ());
1764 return expr;
1767 /* Finish a reference to a non-static data member (DECL) that is not
1768 preceded by `.' or `->'. */
1770 tree
1771 finish_non_static_data_member (tree decl, tree object, tree qualifying_scope)
1773 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
1774 bool try_omp_private = !object && omp_private_member_map;
1775 tree ret;
1777 if (!object)
1779 tree scope = qualifying_scope;
1780 if (scope == NULL_TREE)
1781 scope = context_for_name_lookup (decl);
1782 object = maybe_dummy_object (scope, NULL);
1785 object = maybe_resolve_dummy (object, true);
1786 if (object == error_mark_node)
1787 return error_mark_node;
1789 /* DR 613/850: Can use non-static data members without an associated
1790 object in sizeof/decltype/alignof. */
1791 if (is_dummy_object (object) && cp_unevaluated_operand == 0
1792 && (!processing_template_decl || !current_class_ref))
1794 if (current_function_decl
1795 && DECL_STATIC_FUNCTION_P (current_function_decl))
1796 error ("invalid use of member %qD in static member function", decl);
1797 else
1798 error ("invalid use of non-static data member %qD", decl);
1799 inform (DECL_SOURCE_LOCATION (decl), "declared here");
1801 return error_mark_node;
1804 if (current_class_ptr)
1805 TREE_USED (current_class_ptr) = 1;
1806 if (processing_template_decl && !qualifying_scope)
1808 tree type = TREE_TYPE (decl);
1810 if (TREE_CODE (type) == REFERENCE_TYPE)
1811 /* Quals on the object don't matter. */;
1812 else if (PACK_EXPANSION_P (type))
1813 /* Don't bother trying to represent this. */
1814 type = NULL_TREE;
1815 else
1817 /* Set the cv qualifiers. */
1818 int quals = cp_type_quals (TREE_TYPE (object));
1820 if (DECL_MUTABLE_P (decl))
1821 quals &= ~TYPE_QUAL_CONST;
1823 quals |= cp_type_quals (TREE_TYPE (decl));
1824 type = cp_build_qualified_type (type, quals);
1827 ret = (convert_from_reference
1828 (build_min (COMPONENT_REF, type, object, decl, NULL_TREE)));
1830 /* If PROCESSING_TEMPLATE_DECL is nonzero here, then
1831 QUALIFYING_SCOPE is also non-null. Wrap this in a SCOPE_REF
1832 for now. */
1833 else if (processing_template_decl)
1834 ret = build_qualified_name (TREE_TYPE (decl),
1835 qualifying_scope,
1836 decl,
1837 /*template_p=*/false);
1838 else
1840 tree access_type = TREE_TYPE (object);
1842 perform_or_defer_access_check (TYPE_BINFO (access_type), decl,
1843 decl, tf_warning_or_error);
1845 /* If the data member was named `C::M', convert `*this' to `C'
1846 first. */
1847 if (qualifying_scope)
1849 tree binfo = NULL_TREE;
1850 object = build_scoped_ref (object, qualifying_scope,
1851 &binfo);
1854 ret = build_class_member_access_expr (object, decl,
1855 /*access_path=*/NULL_TREE,
1856 /*preserve_reference=*/false,
1857 tf_warning_or_error);
1859 if (try_omp_private)
1861 tree *v = omp_private_member_map->get (decl);
1862 if (v)
1863 ret = convert_from_reference (*v);
1865 return ret;
1868 /* If we are currently parsing a template and we encountered a typedef
1869 TYPEDEF_DECL that is being accessed though CONTEXT, this function
1870 adds the typedef to a list tied to the current template.
1871 At template instantiation time, that list is walked and access check
1872 performed for each typedef.
1873 LOCATION is the location of the usage point of TYPEDEF_DECL. */
1875 void
1876 add_typedef_to_current_template_for_access_check (tree typedef_decl,
1877 tree context,
1878 location_t location)
1880 tree template_info = NULL;
1881 tree cs = current_scope ();
1883 if (!is_typedef_decl (typedef_decl)
1884 || !context
1885 || !CLASS_TYPE_P (context)
1886 || !cs)
1887 return;
1889 if (CLASS_TYPE_P (cs) || TREE_CODE (cs) == FUNCTION_DECL)
1890 template_info = get_template_info (cs);
1892 if (template_info
1893 && TI_TEMPLATE (template_info)
1894 && !currently_open_class (context))
1895 append_type_to_template_for_access_check (cs, typedef_decl,
1896 context, location);
1899 /* DECL was the declaration to which a qualified-id resolved. Issue
1900 an error message if it is not accessible. If OBJECT_TYPE is
1901 non-NULL, we have just seen `x->' or `x.' and OBJECT_TYPE is the
1902 type of `*x', or `x', respectively. If the DECL was named as
1903 `A::B' then NESTED_NAME_SPECIFIER is `A'. */
1905 void
1906 check_accessibility_of_qualified_id (tree decl,
1907 tree object_type,
1908 tree nested_name_specifier)
1910 tree scope;
1911 tree qualifying_type = NULL_TREE;
1913 /* If we are parsing a template declaration and if decl is a typedef,
1914 add it to a list tied to the template.
1915 At template instantiation time, that list will be walked and
1916 access check performed. */
1917 add_typedef_to_current_template_for_access_check (decl,
1918 nested_name_specifier
1919 ? nested_name_specifier
1920 : DECL_CONTEXT (decl),
1921 input_location);
1923 /* If we're not checking, return immediately. */
1924 if (deferred_access_no_check)
1925 return;
1927 /* Determine the SCOPE of DECL. */
1928 scope = context_for_name_lookup (decl);
1929 /* If the SCOPE is not a type, then DECL is not a member. */
1930 if (!TYPE_P (scope))
1931 return;
1932 /* Compute the scope through which DECL is being accessed. */
1933 if (object_type
1934 /* OBJECT_TYPE might not be a class type; consider:
1936 class A { typedef int I; };
1937 I *p;
1938 p->A::I::~I();
1940 In this case, we will have "A::I" as the DECL, but "I" as the
1941 OBJECT_TYPE. */
1942 && CLASS_TYPE_P (object_type)
1943 && DERIVED_FROM_P (scope, object_type))
1944 /* If we are processing a `->' or `.' expression, use the type of the
1945 left-hand side. */
1946 qualifying_type = object_type;
1947 else if (nested_name_specifier)
1949 /* If the reference is to a non-static member of the
1950 current class, treat it as if it were referenced through
1951 `this'. */
1952 tree ct;
1953 if (DECL_NONSTATIC_MEMBER_P (decl)
1954 && current_class_ptr
1955 && DERIVED_FROM_P (scope, ct = current_nonlambda_class_type ()))
1956 qualifying_type = ct;
1957 /* Otherwise, use the type indicated by the
1958 nested-name-specifier. */
1959 else
1960 qualifying_type = nested_name_specifier;
1962 else
1963 /* Otherwise, the name must be from the current class or one of
1964 its bases. */
1965 qualifying_type = currently_open_derived_class (scope);
1967 if (qualifying_type
1968 /* It is possible for qualifying type to be a TEMPLATE_TYPE_PARM
1969 or similar in a default argument value. */
1970 && CLASS_TYPE_P (qualifying_type)
1971 && !dependent_type_p (qualifying_type))
1972 perform_or_defer_access_check (TYPE_BINFO (qualifying_type), decl,
1973 decl, tf_warning_or_error);
1976 /* EXPR is the result of a qualified-id. The QUALIFYING_CLASS was the
1977 class named to the left of the "::" operator. DONE is true if this
1978 expression is a complete postfix-expression; it is false if this
1979 expression is followed by '->', '[', '(', etc. ADDRESS_P is true
1980 iff this expression is the operand of '&'. TEMPLATE_P is true iff
1981 the qualified-id was of the form "A::template B". TEMPLATE_ARG_P
1982 is true iff this qualified name appears as a template argument. */
1984 tree
1985 finish_qualified_id_expr (tree qualifying_class,
1986 tree expr,
1987 bool done,
1988 bool address_p,
1989 bool template_p,
1990 bool template_arg_p,
1991 tsubst_flags_t complain)
1993 gcc_assert (TYPE_P (qualifying_class));
1995 if (error_operand_p (expr))
1996 return error_mark_node;
1998 if ((DECL_P (expr) || BASELINK_P (expr))
1999 && !mark_used (expr, complain))
2000 return error_mark_node;
2002 if (template_p)
2004 if (TREE_CODE (expr) == UNBOUND_CLASS_TEMPLATE)
2006 /* cp_parser_lookup_name thought we were looking for a type,
2007 but we're actually looking for a declaration. */
2008 qualifying_class = TYPE_CONTEXT (expr);
2009 expr = TYPE_IDENTIFIER (expr);
2011 else
2012 check_template_keyword (expr);
2015 /* If EXPR occurs as the operand of '&', use special handling that
2016 permits a pointer-to-member. */
2017 if (address_p && done)
2019 if (TREE_CODE (expr) == SCOPE_REF)
2020 expr = TREE_OPERAND (expr, 1);
2021 expr = build_offset_ref (qualifying_class, expr,
2022 /*address_p=*/true, complain);
2023 return expr;
2026 /* No need to check access within an enum. */
2027 if (TREE_CODE (qualifying_class) == ENUMERAL_TYPE)
2028 return expr;
2030 /* Within the scope of a class, turn references to non-static
2031 members into expression of the form "this->...". */
2032 if (template_arg_p)
2033 /* But, within a template argument, we do not want make the
2034 transformation, as there is no "this" pointer. */
2036 else if (TREE_CODE (expr) == FIELD_DECL)
2038 push_deferring_access_checks (dk_no_check);
2039 expr = finish_non_static_data_member (expr, NULL_TREE,
2040 qualifying_class);
2041 pop_deferring_access_checks ();
2043 else if (BASELINK_P (expr))
2045 /* See if any of the functions are non-static members. */
2046 /* If so, the expression may be relative to 'this'. */
2047 if (!shared_member_p (expr)
2048 && current_class_ptr
2049 && DERIVED_FROM_P (qualifying_class,
2050 current_nonlambda_class_type ()))
2051 expr = (build_class_member_access_expr
2052 (maybe_dummy_object (qualifying_class, NULL),
2053 expr,
2054 BASELINK_ACCESS_BINFO (expr),
2055 /*preserve_reference=*/false,
2056 complain));
2057 else if (done)
2058 /* The expression is a qualified name whose address is not
2059 being taken. */
2060 expr = build_offset_ref (qualifying_class, expr, /*address_p=*/false,
2061 complain);
2063 else
2065 /* In a template, return a SCOPE_REF for most qualified-ids
2066 so that we can check access at instantiation time. But if
2067 we're looking at a member of the current instantiation, we
2068 know we have access and building up the SCOPE_REF confuses
2069 non-type template argument handling. */
2070 if (processing_template_decl
2071 && (!currently_open_class (qualifying_class)
2072 || TREE_CODE (expr) == BIT_NOT_EXPR))
2073 expr = build_qualified_name (TREE_TYPE (expr),
2074 qualifying_class, expr,
2075 template_p);
2077 expr = convert_from_reference (expr);
2080 return expr;
2083 /* Begin a statement-expression. The value returned must be passed to
2084 finish_stmt_expr. */
2086 tree
2087 begin_stmt_expr (void)
2089 return push_stmt_list ();
2092 /* Process the final expression of a statement expression. EXPR can be
2093 NULL, if the final expression is empty. Return a STATEMENT_LIST
2094 containing all the statements in the statement-expression, or
2095 ERROR_MARK_NODE if there was an error. */
2097 tree
2098 finish_stmt_expr_expr (tree expr, tree stmt_expr)
2100 if (error_operand_p (expr))
2102 /* The type of the statement-expression is the type of the last
2103 expression. */
2104 TREE_TYPE (stmt_expr) = error_mark_node;
2105 return error_mark_node;
2108 /* If the last statement does not have "void" type, then the value
2109 of the last statement is the value of the entire expression. */
2110 if (expr)
2112 tree type = TREE_TYPE (expr);
2114 if (processing_template_decl)
2116 expr = build_stmt (input_location, EXPR_STMT, expr);
2117 expr = add_stmt (expr);
2118 /* Mark the last statement so that we can recognize it as such at
2119 template-instantiation time. */
2120 EXPR_STMT_STMT_EXPR_RESULT (expr) = 1;
2122 else if (VOID_TYPE_P (type))
2124 /* Just treat this like an ordinary statement. */
2125 expr = finish_expr_stmt (expr);
2127 else
2129 /* It actually has a value we need to deal with. First, force it
2130 to be an rvalue so that we won't need to build up a copy
2131 constructor call later when we try to assign it to something. */
2132 expr = force_rvalue (expr, tf_warning_or_error);
2133 if (error_operand_p (expr))
2134 return error_mark_node;
2136 /* Update for array-to-pointer decay. */
2137 type = TREE_TYPE (expr);
2139 /* Wrap it in a CLEANUP_POINT_EXPR and add it to the list like a
2140 normal statement, but don't convert to void or actually add
2141 the EXPR_STMT. */
2142 if (TREE_CODE (expr) != CLEANUP_POINT_EXPR)
2143 expr = maybe_cleanup_point_expr (expr);
2144 add_stmt (expr);
2147 /* The type of the statement-expression is the type of the last
2148 expression. */
2149 TREE_TYPE (stmt_expr) = type;
2152 return stmt_expr;
2155 /* Finish a statement-expression. EXPR should be the value returned
2156 by the previous begin_stmt_expr. Returns an expression
2157 representing the statement-expression. */
2159 tree
2160 finish_stmt_expr (tree stmt_expr, bool has_no_scope)
2162 tree type;
2163 tree result;
2165 if (error_operand_p (stmt_expr))
2167 pop_stmt_list (stmt_expr);
2168 return error_mark_node;
2171 gcc_assert (TREE_CODE (stmt_expr) == STATEMENT_LIST);
2173 type = TREE_TYPE (stmt_expr);
2174 result = pop_stmt_list (stmt_expr);
2175 TREE_TYPE (result) = type;
2177 if (processing_template_decl)
2179 result = build_min (STMT_EXPR, type, result);
2180 TREE_SIDE_EFFECTS (result) = 1;
2181 STMT_EXPR_NO_SCOPE (result) = has_no_scope;
2183 else if (CLASS_TYPE_P (type))
2185 /* Wrap the statement-expression in a TARGET_EXPR so that the
2186 temporary object created by the final expression is destroyed at
2187 the end of the full-expression containing the
2188 statement-expression. */
2189 result = force_target_expr (type, result, tf_warning_or_error);
2192 return result;
2195 /* Returns the expression which provides the value of STMT_EXPR. */
2197 tree
2198 stmt_expr_value_expr (tree stmt_expr)
2200 tree t = STMT_EXPR_STMT (stmt_expr);
2202 if (TREE_CODE (t) == BIND_EXPR)
2203 t = BIND_EXPR_BODY (t);
2205 if (TREE_CODE (t) == STATEMENT_LIST && STATEMENT_LIST_TAIL (t))
2206 t = STATEMENT_LIST_TAIL (t)->stmt;
2208 if (TREE_CODE (t) == EXPR_STMT)
2209 t = EXPR_STMT_EXPR (t);
2211 return t;
2214 /* Return TRUE iff EXPR_STMT is an empty list of
2215 expression statements. */
2217 bool
2218 empty_expr_stmt_p (tree expr_stmt)
2220 tree body = NULL_TREE;
2222 if (expr_stmt == void_node)
2223 return true;
2225 if (expr_stmt)
2227 if (TREE_CODE (expr_stmt) == EXPR_STMT)
2228 body = EXPR_STMT_EXPR (expr_stmt);
2229 else if (TREE_CODE (expr_stmt) == STATEMENT_LIST)
2230 body = expr_stmt;
2233 if (body)
2235 if (TREE_CODE (body) == STATEMENT_LIST)
2236 return tsi_end_p (tsi_start (body));
2237 else
2238 return empty_expr_stmt_p (body);
2240 return false;
2243 /* Perform Koenig lookup. FN is the postfix-expression representing
2244 the function (or functions) to call; ARGS are the arguments to the
2245 call. Returns the functions to be considered by overload resolution. */
2247 cp_expr
2248 perform_koenig_lookup (cp_expr fn, vec<tree, va_gc> *args,
2249 tsubst_flags_t complain)
2251 tree identifier = NULL_TREE;
2252 tree functions = NULL_TREE;
2253 tree tmpl_args = NULL_TREE;
2254 bool template_id = false;
2255 location_t loc = fn.get_location ();
2257 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
2259 /* Use a separate flag to handle null args. */
2260 template_id = true;
2261 tmpl_args = TREE_OPERAND (fn, 1);
2262 fn = TREE_OPERAND (fn, 0);
2265 /* Find the name of the overloaded function. */
2266 if (identifier_p (fn))
2267 identifier = fn;
2268 else
2270 functions = fn;
2271 identifier = OVL_NAME (functions);
2274 /* A call to a namespace-scope function using an unqualified name.
2276 Do Koenig lookup -- unless any of the arguments are
2277 type-dependent. */
2278 if (!any_type_dependent_arguments_p (args)
2279 && !any_dependent_template_arguments_p (tmpl_args))
2281 fn = lookup_arg_dependent (identifier, functions, args);
2282 if (!fn)
2284 /* The unqualified name could not be resolved. */
2285 if (complain & tf_error)
2286 fn = unqualified_fn_lookup_error (cp_expr (identifier, loc));
2287 else
2288 fn = identifier;
2292 if (fn && template_id && fn != error_mark_node)
2293 fn = build2 (TEMPLATE_ID_EXPR, unknown_type_node, fn, tmpl_args);
2295 return fn;
2298 /* Generate an expression for `FN (ARGS)'. This may change the
2299 contents of ARGS.
2301 If DISALLOW_VIRTUAL is true, the call to FN will be not generated
2302 as a virtual call, even if FN is virtual. (This flag is set when
2303 encountering an expression where the function name is explicitly
2304 qualified. For example a call to `X::f' never generates a virtual
2305 call.)
2307 Returns code for the call. */
2309 tree
2310 finish_call_expr (tree fn, vec<tree, va_gc> **args, bool disallow_virtual,
2311 bool koenig_p, tsubst_flags_t complain)
2313 tree result;
2314 tree orig_fn;
2315 vec<tree, va_gc> *orig_args = NULL;
2317 if (fn == error_mark_node)
2318 return error_mark_node;
2320 gcc_assert (!TYPE_P (fn));
2322 /* If FN may be a FUNCTION_DECL obfuscated by force_paren_expr, undo
2323 it so that we can tell this is a call to a known function. */
2324 fn = maybe_undo_parenthesized_ref (fn);
2326 orig_fn = fn;
2328 if (processing_template_decl)
2330 /* If FN is a local extern declaration or set thereof, look them up
2331 again at instantiation time. */
2332 if (is_overloaded_fn (fn))
2334 tree ifn = get_first_fn (fn);
2335 if (TREE_CODE (ifn) == FUNCTION_DECL
2336 && DECL_LOCAL_FUNCTION_P (ifn))
2337 orig_fn = DECL_NAME (ifn);
2340 /* If the call expression is dependent, build a CALL_EXPR node
2341 with no type; type_dependent_expression_p recognizes
2342 expressions with no type as being dependent. */
2343 if (type_dependent_expression_p (fn)
2344 || any_type_dependent_arguments_p (*args))
2346 result = build_min_nt_call_vec (orig_fn, *args);
2347 SET_EXPR_LOCATION (result, EXPR_LOC_OR_LOC (fn, input_location));
2348 KOENIG_LOOKUP_P (result) = koenig_p;
2349 if (is_overloaded_fn (fn))
2351 fn = get_fns (fn);
2352 lookup_keep (fn, true);
2355 if (cfun)
2357 bool abnormal = true;
2358 for (lkp_iterator iter (fn); abnormal && iter; ++iter)
2360 tree fndecl = *iter;
2361 if (TREE_CODE (fndecl) != FUNCTION_DECL
2362 || !TREE_THIS_VOLATILE (fndecl))
2363 abnormal = false;
2365 /* FIXME: Stop warning about falling off end of non-void
2366 function. But this is wrong. Even if we only see
2367 no-return fns at this point, we could select a
2368 future-defined return fn during instantiation. Or
2369 vice-versa. */
2370 if (abnormal)
2371 current_function_returns_abnormally = 1;
2373 return result;
2375 orig_args = make_tree_vector_copy (*args);
2376 if (!BASELINK_P (fn)
2377 && TREE_CODE (fn) != PSEUDO_DTOR_EXPR
2378 && TREE_TYPE (fn) != unknown_type_node)
2379 fn = build_non_dependent_expr (fn);
2380 make_args_non_dependent (*args);
2383 if (TREE_CODE (fn) == COMPONENT_REF)
2385 tree member = TREE_OPERAND (fn, 1);
2386 if (BASELINK_P (member))
2388 tree object = TREE_OPERAND (fn, 0);
2389 return build_new_method_call (object, member,
2390 args, NULL_TREE,
2391 (disallow_virtual
2392 ? LOOKUP_NORMAL | LOOKUP_NONVIRTUAL
2393 : LOOKUP_NORMAL),
2394 /*fn_p=*/NULL,
2395 complain);
2399 /* Per 13.3.1.1, '(&f)(...)' is the same as '(f)(...)'. */
2400 if (TREE_CODE (fn) == ADDR_EXPR
2401 && TREE_CODE (TREE_OPERAND (fn, 0)) == OVERLOAD)
2402 fn = TREE_OPERAND (fn, 0);
2404 if (is_overloaded_fn (fn))
2405 fn = baselink_for_fns (fn);
2407 result = NULL_TREE;
2408 if (BASELINK_P (fn))
2410 tree object;
2412 /* A call to a member function. From [over.call.func]:
2414 If the keyword this is in scope and refers to the class of
2415 that member function, or a derived class thereof, then the
2416 function call is transformed into a qualified function call
2417 using (*this) as the postfix-expression to the left of the
2418 . operator.... [Otherwise] a contrived object of type T
2419 becomes the implied object argument.
2421 In this situation:
2423 struct A { void f(); };
2424 struct B : public A {};
2425 struct C : public A { void g() { B::f(); }};
2427 "the class of that member function" refers to `A'. But 11.2
2428 [class.access.base] says that we need to convert 'this' to B* as
2429 part of the access, so we pass 'B' to maybe_dummy_object. */
2431 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (get_first_fn (fn)))
2433 /* A constructor call always uses a dummy object. (This constructor
2434 call which has the form A::A () is actually invalid and we are
2435 going to reject it later in build_new_method_call.) */
2436 object = build_dummy_object (BINFO_TYPE (BASELINK_ACCESS_BINFO (fn)));
2438 else
2439 object = maybe_dummy_object (BINFO_TYPE (BASELINK_ACCESS_BINFO (fn)),
2440 NULL);
2442 result = build_new_method_call (object, fn, args, NULL_TREE,
2443 (disallow_virtual
2444 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
2445 : LOOKUP_NORMAL),
2446 /*fn_p=*/NULL,
2447 complain);
2449 else if (is_overloaded_fn (fn))
2451 /* If the function is an overloaded builtin, resolve it. */
2452 if (TREE_CODE (fn) == FUNCTION_DECL
2453 && (DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL
2454 || DECL_BUILT_IN_CLASS (fn) == BUILT_IN_MD))
2455 result = resolve_overloaded_builtin (input_location, fn, *args);
2457 if (!result)
2459 if (warn_sizeof_pointer_memaccess
2460 && (complain & tf_warning)
2461 && !vec_safe_is_empty (*args)
2462 && !processing_template_decl)
2464 location_t sizeof_arg_loc[3];
2465 tree sizeof_arg[3];
2466 unsigned int i;
2467 for (i = 0; i < 3; i++)
2469 tree t;
2471 sizeof_arg_loc[i] = UNKNOWN_LOCATION;
2472 sizeof_arg[i] = NULL_TREE;
2473 if (i >= (*args)->length ())
2474 continue;
2475 t = (**args)[i];
2476 if (TREE_CODE (t) != SIZEOF_EXPR)
2477 continue;
2478 if (SIZEOF_EXPR_TYPE_P (t))
2479 sizeof_arg[i] = TREE_TYPE (TREE_OPERAND (t, 0));
2480 else
2481 sizeof_arg[i] = TREE_OPERAND (t, 0);
2482 sizeof_arg_loc[i] = EXPR_LOCATION (t);
2484 sizeof_pointer_memaccess_warning
2485 (sizeof_arg_loc, fn, *args,
2486 sizeof_arg, same_type_ignoring_top_level_qualifiers_p);
2489 /* A call to a namespace-scope function. */
2490 result = build_new_function_call (fn, args, complain);
2493 else if (TREE_CODE (fn) == PSEUDO_DTOR_EXPR)
2495 if (!vec_safe_is_empty (*args))
2496 error ("arguments to destructor are not allowed");
2497 /* Mark the pseudo-destructor call as having side-effects so
2498 that we do not issue warnings about its use. */
2499 result = build1 (NOP_EXPR,
2500 void_type_node,
2501 TREE_OPERAND (fn, 0));
2502 TREE_SIDE_EFFECTS (result) = 1;
2504 else if (CLASS_TYPE_P (TREE_TYPE (fn)))
2505 /* If the "function" is really an object of class type, it might
2506 have an overloaded `operator ()'. */
2507 result = build_op_call (fn, args, complain);
2509 if (!result)
2510 /* A call where the function is unknown. */
2511 result = cp_build_function_call_vec (fn, args, complain);
2513 if (processing_template_decl && result != error_mark_node)
2515 if (INDIRECT_REF_P (result))
2516 result = TREE_OPERAND (result, 0);
2517 result = build_call_vec (TREE_TYPE (result), orig_fn, orig_args);
2518 SET_EXPR_LOCATION (result, input_location);
2519 KOENIG_LOOKUP_P (result) = koenig_p;
2520 release_tree_vector (orig_args);
2521 result = convert_from_reference (result);
2524 /* Free or retain OVERLOADs from lookup. */
2525 if (is_overloaded_fn (orig_fn))
2526 lookup_keep (get_fns (orig_fn), processing_template_decl);
2528 return result;
2531 /* Finish a call to a postfix increment or decrement or EXPR. (Which
2532 is indicated by CODE, which should be POSTINCREMENT_EXPR or
2533 POSTDECREMENT_EXPR.) */
2535 cp_expr
2536 finish_increment_expr (cp_expr expr, enum tree_code code)
2538 /* input_location holds the location of the trailing operator token.
2539 Build a location of the form:
2540 expr++
2541 ~~~~^~
2542 with the caret at the operator token, ranging from the start
2543 of EXPR to the end of the operator token. */
2544 location_t combined_loc = make_location (input_location,
2545 expr.get_start (),
2546 get_finish (input_location));
2547 cp_expr result = build_x_unary_op (combined_loc, code, expr,
2548 tf_warning_or_error);
2549 /* TODO: build_x_unary_op doesn't honor the location, so set it here. */
2550 result.set_location (combined_loc);
2551 return result;
2554 /* Finish a use of `this'. Returns an expression for `this'. */
2556 tree
2557 finish_this_expr (void)
2559 tree result = NULL_TREE;
2561 if (current_class_ptr)
2563 tree type = TREE_TYPE (current_class_ref);
2565 /* In a lambda expression, 'this' refers to the captured 'this'. */
2566 if (LAMBDA_TYPE_P (type))
2567 result = lambda_expr_this_capture (CLASSTYPE_LAMBDA_EXPR (type), true);
2568 else
2569 result = current_class_ptr;
2572 if (result)
2573 /* The keyword 'this' is a prvalue expression. */
2574 return rvalue (result);
2576 tree fn = current_nonlambda_function ();
2577 if (fn && DECL_STATIC_FUNCTION_P (fn))
2578 error ("%<this%> is unavailable for static member functions");
2579 else if (fn)
2580 error ("invalid use of %<this%> in non-member function");
2581 else
2582 error ("invalid use of %<this%> at top level");
2583 return error_mark_node;
2586 /* Finish a pseudo-destructor expression. If SCOPE is NULL, the
2587 expression was of the form `OBJECT.~DESTRUCTOR' where DESTRUCTOR is
2588 the TYPE for the type given. If SCOPE is non-NULL, the expression
2589 was of the form `OBJECT.SCOPE::~DESTRUCTOR'. */
2591 tree
2592 finish_pseudo_destructor_expr (tree object, tree scope, tree destructor,
2593 location_t loc)
2595 if (object == error_mark_node || destructor == error_mark_node)
2596 return error_mark_node;
2598 gcc_assert (TYPE_P (destructor));
2600 if (!processing_template_decl)
2602 if (scope == error_mark_node)
2604 error_at (loc, "invalid qualifying scope in pseudo-destructor name");
2605 return error_mark_node;
2607 if (is_auto (destructor))
2608 destructor = TREE_TYPE (object);
2609 if (scope && TYPE_P (scope) && !check_dtor_name (scope, destructor))
2611 error_at (loc,
2612 "qualified type %qT does not match destructor name ~%qT",
2613 scope, destructor);
2614 return error_mark_node;
2618 /* [expr.pseudo] says both:
2620 The type designated by the pseudo-destructor-name shall be
2621 the same as the object type.
2623 and:
2625 The cv-unqualified versions of the object type and of the
2626 type designated by the pseudo-destructor-name shall be the
2627 same type.
2629 We implement the more generous second sentence, since that is
2630 what most other compilers do. */
2631 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (object),
2632 destructor))
2634 error_at (loc, "%qE is not of type %qT", object, destructor);
2635 return error_mark_node;
2639 return build3_loc (loc, PSEUDO_DTOR_EXPR, void_type_node, object,
2640 scope, destructor);
2643 /* Finish an expression of the form CODE EXPR. */
2645 cp_expr
2646 finish_unary_op_expr (location_t op_loc, enum tree_code code, cp_expr expr,
2647 tsubst_flags_t complain)
2649 /* Build a location of the form:
2650 ++expr
2651 ^~~~~~
2652 with the caret at the operator token, ranging from the start
2653 of the operator token to the end of EXPR. */
2654 location_t combined_loc = make_location (op_loc,
2655 op_loc, expr.get_finish ());
2656 cp_expr result = build_x_unary_op (combined_loc, code, expr, complain);
2657 /* TODO: build_x_unary_op doesn't always honor the location. */
2658 result.set_location (combined_loc);
2660 tree result_ovl, expr_ovl;
2662 if (!(complain & tf_warning))
2663 return result;
2665 result_ovl = result;
2666 expr_ovl = expr;
2668 if (!processing_template_decl)
2669 expr_ovl = cp_fully_fold (expr_ovl);
2671 if (!CONSTANT_CLASS_P (expr_ovl)
2672 || TREE_OVERFLOW_P (expr_ovl))
2673 return result;
2675 if (!processing_template_decl)
2676 result_ovl = cp_fully_fold (result_ovl);
2678 if (CONSTANT_CLASS_P (result_ovl) && TREE_OVERFLOW_P (result_ovl))
2679 overflow_warning (combined_loc, result_ovl);
2681 return result;
2684 /* Finish a compound-literal expression or C++11 functional cast with aggregate
2685 initializer. TYPE is the type to which the CONSTRUCTOR in COMPOUND_LITERAL
2686 is being cast. */
2688 tree
2689 finish_compound_literal (tree type, tree compound_literal,
2690 tsubst_flags_t complain,
2691 fcl_t fcl_context)
2693 if (type == error_mark_node)
2694 return error_mark_node;
2696 if (TREE_CODE (type) == REFERENCE_TYPE)
2698 compound_literal
2699 = finish_compound_literal (TREE_TYPE (type), compound_literal,
2700 complain, fcl_context);
2701 return cp_build_c_cast (type, compound_literal, complain);
2704 if (!TYPE_OBJ_P (type))
2706 if (complain & tf_error)
2707 error ("compound literal of non-object type %qT", type);
2708 return error_mark_node;
2711 if (tree anode = type_uses_auto (type))
2712 if (CLASS_PLACEHOLDER_TEMPLATE (anode))
2714 type = do_auto_deduction (type, compound_literal, anode, complain,
2715 adc_variable_type);
2716 if (type == error_mark_node)
2717 return error_mark_node;
2720 if (processing_template_decl)
2722 TREE_TYPE (compound_literal) = type;
2723 /* Mark the expression as a compound literal. */
2724 TREE_HAS_CONSTRUCTOR (compound_literal) = 1;
2725 if (fcl_context == fcl_c99)
2726 CONSTRUCTOR_C99_COMPOUND_LITERAL (compound_literal) = 1;
2727 return compound_literal;
2730 type = complete_type (type);
2732 if (TYPE_NON_AGGREGATE_CLASS (type))
2734 /* Trying to deal with a CONSTRUCTOR instead of a TREE_LIST
2735 everywhere that deals with function arguments would be a pain, so
2736 just wrap it in a TREE_LIST. The parser set a flag so we know
2737 that it came from T{} rather than T({}). */
2738 CONSTRUCTOR_IS_DIRECT_INIT (compound_literal) = 1;
2739 compound_literal = build_tree_list (NULL_TREE, compound_literal);
2740 return build_functional_cast (type, compound_literal, complain);
2743 if (TREE_CODE (type) == ARRAY_TYPE
2744 && check_array_initializer (NULL_TREE, type, compound_literal))
2745 return error_mark_node;
2746 compound_literal = reshape_init (type, compound_literal, complain);
2747 if (SCALAR_TYPE_P (type)
2748 && !BRACE_ENCLOSED_INITIALIZER_P (compound_literal)
2749 && !check_narrowing (type, compound_literal, complain))
2750 return error_mark_node;
2751 if (TREE_CODE (type) == ARRAY_TYPE
2752 && TYPE_DOMAIN (type) == NULL_TREE)
2754 cp_complete_array_type_or_error (&type, compound_literal,
2755 false, complain);
2756 if (type == error_mark_node)
2757 return error_mark_node;
2759 compound_literal = digest_init_flags (type, compound_literal, LOOKUP_NORMAL,
2760 complain);
2761 if (TREE_CODE (compound_literal) == CONSTRUCTOR)
2763 TREE_HAS_CONSTRUCTOR (compound_literal) = true;
2764 if (fcl_context == fcl_c99)
2765 CONSTRUCTOR_C99_COMPOUND_LITERAL (compound_literal) = 1;
2768 /* Put static/constant array temporaries in static variables. */
2769 /* FIXME all C99 compound literals should be variables rather than C++
2770 temporaries, unless they are used as an aggregate initializer. */
2771 if ((!at_function_scope_p () || CP_TYPE_CONST_P (type))
2772 && fcl_context == fcl_c99
2773 && TREE_CODE (type) == ARRAY_TYPE
2774 && !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
2775 && initializer_constant_valid_p (compound_literal, type))
2777 tree decl = create_temporary_var (type);
2778 DECL_INITIAL (decl) = compound_literal;
2779 TREE_STATIC (decl) = 1;
2780 if (literal_type_p (type) && CP_TYPE_CONST_NON_VOLATILE_P (type))
2782 /* 5.19 says that a constant expression can include an
2783 lvalue-rvalue conversion applied to "a glvalue of literal type
2784 that refers to a non-volatile temporary object initialized
2785 with a constant expression". Rather than try to communicate
2786 that this VAR_DECL is a temporary, just mark it constexpr. */
2787 DECL_DECLARED_CONSTEXPR_P (decl) = true;
2788 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
2789 TREE_CONSTANT (decl) = true;
2791 cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
2792 decl = pushdecl_top_level (decl);
2793 DECL_NAME (decl) = make_anon_name ();
2794 SET_DECL_ASSEMBLER_NAME (decl, DECL_NAME (decl));
2795 /* Make sure the destructor is callable. */
2796 tree clean = cxx_maybe_build_cleanup (decl, complain);
2797 if (clean == error_mark_node)
2798 return error_mark_node;
2799 return decl;
2802 /* Represent other compound literals with TARGET_EXPR so we produce
2803 an lvalue, but can elide copies. */
2804 if (!VECTOR_TYPE_P (type))
2805 compound_literal = get_target_expr_sfinae (compound_literal, complain);
2807 return compound_literal;
2810 /* Return the declaration for the function-name variable indicated by
2811 ID. */
2813 tree
2814 finish_fname (tree id)
2816 tree decl;
2818 decl = fname_decl (input_location, C_RID_CODE (id), id);
2819 if (processing_template_decl && current_function_decl
2820 && decl != error_mark_node)
2821 decl = DECL_NAME (decl);
2822 return decl;
2825 /* Finish a translation unit. */
2827 void
2828 finish_translation_unit (void)
2830 /* In case there were missing closebraces,
2831 get us back to the global binding level. */
2832 pop_everything ();
2833 while (current_namespace != global_namespace)
2834 pop_namespace ();
2836 /* Do file scope __FUNCTION__ et al. */
2837 finish_fname_decls ();
2840 /* Finish a template type parameter, specified as AGGR IDENTIFIER.
2841 Returns the parameter. */
2843 tree
2844 finish_template_type_parm (tree aggr, tree identifier)
2846 if (aggr != class_type_node)
2848 permerror (input_location, "template type parameters must use the keyword %<class%> or %<typename%>");
2849 aggr = class_type_node;
2852 return build_tree_list (aggr, identifier);
2855 /* Finish a template template parameter, specified as AGGR IDENTIFIER.
2856 Returns the parameter. */
2858 tree
2859 finish_template_template_parm (tree aggr, tree identifier)
2861 tree decl = build_decl (input_location,
2862 TYPE_DECL, identifier, NULL_TREE);
2864 tree tmpl = build_lang_decl (TEMPLATE_DECL, identifier, NULL_TREE);
2865 DECL_TEMPLATE_PARMS (tmpl) = current_template_parms;
2866 DECL_TEMPLATE_RESULT (tmpl) = decl;
2867 DECL_ARTIFICIAL (decl) = 1;
2869 // Associate the constraints with the underlying declaration,
2870 // not the template.
2871 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
2872 tree constr = build_constraints (reqs, NULL_TREE);
2873 set_constraints (decl, constr);
2875 end_template_decl ();
2877 gcc_assert (DECL_TEMPLATE_PARMS (tmpl));
2879 check_default_tmpl_args (decl, DECL_TEMPLATE_PARMS (tmpl),
2880 /*is_primary=*/true, /*is_partial=*/false,
2881 /*is_friend=*/0);
2883 return finish_template_type_parm (aggr, tmpl);
2886 /* ARGUMENT is the default-argument value for a template template
2887 parameter. If ARGUMENT is invalid, issue error messages and return
2888 the ERROR_MARK_NODE. Otherwise, ARGUMENT itself is returned. */
2890 tree
2891 check_template_template_default_arg (tree argument)
2893 if (TREE_CODE (argument) != TEMPLATE_DECL
2894 && TREE_CODE (argument) != TEMPLATE_TEMPLATE_PARM
2895 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
2897 if (TREE_CODE (argument) == TYPE_DECL)
2898 error ("invalid use of type %qT as a default value for a template "
2899 "template-parameter", TREE_TYPE (argument));
2900 else
2901 error ("invalid default argument for a template template parameter");
2902 return error_mark_node;
2905 return argument;
2908 /* Begin a class definition, as indicated by T. */
2910 tree
2911 begin_class_definition (tree t)
2913 if (error_operand_p (t) || error_operand_p (TYPE_MAIN_DECL (t)))
2914 return error_mark_node;
2916 if (processing_template_parmlist)
2918 error ("definition of %q#T inside template parameter list", t);
2919 return error_mark_node;
2922 /* According to the C++ ABI, decimal classes defined in ISO/IEC TR 24733
2923 are passed the same as decimal scalar types. */
2924 if (TREE_CODE (t) == RECORD_TYPE
2925 && !processing_template_decl)
2927 tree ns = TYPE_CONTEXT (t);
2928 if (ns && TREE_CODE (ns) == NAMESPACE_DECL
2929 && DECL_CONTEXT (ns) == std_node
2930 && DECL_NAME (ns)
2931 && id_equal (DECL_NAME (ns), "decimal"))
2933 const char *n = TYPE_NAME_STRING (t);
2934 if ((strcmp (n, "decimal32") == 0)
2935 || (strcmp (n, "decimal64") == 0)
2936 || (strcmp (n, "decimal128") == 0))
2937 TYPE_TRANSPARENT_AGGR (t) = 1;
2941 /* A non-implicit typename comes from code like:
2943 template <typename T> struct A {
2944 template <typename U> struct A<T>::B ...
2946 This is erroneous. */
2947 else if (TREE_CODE (t) == TYPENAME_TYPE)
2949 error ("invalid definition of qualified type %qT", t);
2950 t = error_mark_node;
2953 if (t == error_mark_node || ! MAYBE_CLASS_TYPE_P (t))
2955 t = make_class_type (RECORD_TYPE);
2956 pushtag (make_anon_name (), t, /*tag_scope=*/ts_current);
2959 if (TYPE_BEING_DEFINED (t))
2961 t = make_class_type (TREE_CODE (t));
2962 pushtag (TYPE_IDENTIFIER (t), t, /*tag_scope=*/ts_current);
2964 maybe_process_partial_specialization (t);
2965 pushclass (t);
2966 TYPE_BEING_DEFINED (t) = 1;
2967 class_binding_level->defining_class_p = 1;
2969 if (flag_pack_struct)
2971 tree v;
2972 TYPE_PACKED (t) = 1;
2973 /* Even though the type is being defined for the first time
2974 here, there might have been a forward declaration, so there
2975 might be cv-qualified variants of T. */
2976 for (v = TYPE_NEXT_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v))
2977 TYPE_PACKED (v) = 1;
2979 /* Reset the interface data, at the earliest possible
2980 moment, as it might have been set via a class foo;
2981 before. */
2982 if (! TYPE_UNNAMED_P (t))
2984 struct c_fileinfo *finfo = \
2985 get_fileinfo (LOCATION_FILE (input_location));
2986 CLASSTYPE_INTERFACE_ONLY (t) = finfo->interface_only;
2987 SET_CLASSTYPE_INTERFACE_UNKNOWN_X
2988 (t, finfo->interface_unknown);
2990 reset_specialization();
2992 /* Make a declaration for this class in its own scope. */
2993 build_self_reference ();
2995 return t;
2998 /* Finish the member declaration given by DECL. */
3000 void
3001 finish_member_declaration (tree decl)
3003 if (decl == error_mark_node || decl == NULL_TREE)
3004 return;
3006 if (decl == void_type_node)
3007 /* The COMPONENT was a friend, not a member, and so there's
3008 nothing for us to do. */
3009 return;
3011 /* We should see only one DECL at a time. */
3012 gcc_assert (DECL_CHAIN (decl) == NULL_TREE);
3014 /* Don't add decls after definition. */
3015 gcc_assert (TYPE_BEING_DEFINED (current_class_type)
3016 /* We can add lambda types when late parsing default
3017 arguments. */
3018 || LAMBDA_TYPE_P (TREE_TYPE (decl)));
3020 /* Set up access control for DECL. */
3021 TREE_PRIVATE (decl)
3022 = (current_access_specifier == access_private_node);
3023 TREE_PROTECTED (decl)
3024 = (current_access_specifier == access_protected_node);
3025 if (TREE_CODE (decl) == TEMPLATE_DECL)
3027 TREE_PRIVATE (DECL_TEMPLATE_RESULT (decl)) = TREE_PRIVATE (decl);
3028 TREE_PROTECTED (DECL_TEMPLATE_RESULT (decl)) = TREE_PROTECTED (decl);
3031 /* Mark the DECL as a member of the current class, unless it's
3032 a member of an enumeration. */
3033 if (TREE_CODE (decl) != CONST_DECL)
3034 DECL_CONTEXT (decl) = current_class_type;
3036 if (TREE_CODE (decl) == USING_DECL)
3037 /* For now, ignore class-scope USING_DECLS, so that debugging
3038 backends do not see them. */
3039 DECL_IGNORED_P (decl) = 1;
3041 /* Check for bare parameter packs in the non-static data member
3042 declaration. */
3043 if (TREE_CODE (decl) == FIELD_DECL)
3045 if (check_for_bare_parameter_packs (TREE_TYPE (decl)))
3046 TREE_TYPE (decl) = error_mark_node;
3047 if (check_for_bare_parameter_packs (DECL_ATTRIBUTES (decl)))
3048 DECL_ATTRIBUTES (decl) = NULL_TREE;
3051 /* [dcl.link]
3053 A C language linkage is ignored for the names of class members
3054 and the member function type of class member functions. */
3055 if (DECL_LANG_SPECIFIC (decl))
3056 SET_DECL_LANGUAGE (decl, lang_cplusplus);
3058 bool add = false;
3060 /* Functions and non-functions are added differently. */
3061 if (DECL_DECLARES_FUNCTION_P (decl))
3062 add = add_method (current_class_type, decl, false);
3063 /* Enter the DECL into the scope of the class, if the class
3064 isn't a closure (whose fields are supposed to be unnamed). */
3065 else if (CLASSTYPE_LAMBDA_EXPR (current_class_type)
3066 || pushdecl_class_level (decl))
3067 add = true;
3069 if (add)
3071 /* All TYPE_DECLs go at the end of TYPE_FIELDS. Ordinary fields
3072 go at the beginning. The reason is that
3073 legacy_nonfn_member_lookup searches the list in order, and we
3074 want a field name to override a type name so that the "struct
3075 stat hack" will work. In particular:
3077 struct S { enum E { }; static const int E = 5; int ary[S::E]; } s;
3079 is valid. */
3081 if (TREE_CODE (decl) == TYPE_DECL)
3082 TYPE_FIELDS (current_class_type)
3083 = chainon (TYPE_FIELDS (current_class_type), decl);
3084 else
3086 DECL_CHAIN (decl) = TYPE_FIELDS (current_class_type);
3087 TYPE_FIELDS (current_class_type) = decl;
3090 maybe_add_class_template_decl_list (current_class_type, decl,
3091 /*friend_p=*/0);
3095 /* Finish processing a complete template declaration. The PARMS are
3096 the template parameters. */
3098 void
3099 finish_template_decl (tree parms)
3101 if (parms)
3102 end_template_decl ();
3103 else
3104 end_specialization ();
3107 // Returns the template type of the class scope being entered. If we're
3108 // entering a constrained class scope. TYPE is the class template
3109 // scope being entered and we may need to match the intended type with
3110 // a constrained specialization. For example:
3112 // template<Object T>
3113 // struct S { void f(); }; #1
3115 // template<Object T>
3116 // void S<T>::f() { } #2
3118 // We check, in #2, that S<T> refers precisely to the type declared by
3119 // #1 (i.e., that the constraints match). Note that the following should
3120 // be an error since there is no specialization of S<T> that is
3121 // unconstrained, but this is not diagnosed here.
3123 // template<typename T>
3124 // void S<T>::f() { }
3126 // We cannot diagnose this problem here since this function also matches
3127 // qualified template names that are not part of a definition. For example:
3129 // template<Integral T, Floating_point U>
3130 // typename pair<T, U>::first_type void f(T, U);
3132 // Here, it is unlikely that there is a partial specialization of
3133 // pair constrained for for Integral and Floating_point arguments.
3135 // The general rule is: if a constrained specialization with matching
3136 // constraints is found return that type. Also note that if TYPE is not a
3137 // class-type (e.g. a typename type), then no fixup is needed.
3139 static tree
3140 fixup_template_type (tree type)
3142 // Find the template parameter list at the a depth appropriate to
3143 // the scope we're trying to enter.
3144 tree parms = current_template_parms;
3145 int depth = template_class_depth (type);
3146 for (int n = processing_template_decl; n > depth && parms; --n)
3147 parms = TREE_CHAIN (parms);
3148 if (!parms)
3149 return type;
3150 tree cur_reqs = TEMPLATE_PARMS_CONSTRAINTS (parms);
3151 tree cur_constr = build_constraints (cur_reqs, NULL_TREE);
3153 // Search for a specialization whose type and constraints match.
3154 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
3155 tree specs = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
3156 while (specs)
3158 tree spec_constr = get_constraints (TREE_VALUE (specs));
3160 // If the type and constraints match a specialization, then we
3161 // are entering that type.
3162 if (same_type_p (type, TREE_TYPE (specs))
3163 && equivalent_constraints (cur_constr, spec_constr))
3164 return TREE_TYPE (specs);
3165 specs = TREE_CHAIN (specs);
3168 // If no specialization matches, then must return the type
3169 // previously found.
3170 return type;
3173 /* Finish processing a template-id (which names a type) of the form
3174 NAME < ARGS >. Return the TYPE_DECL for the type named by the
3175 template-id. If ENTERING_SCOPE is nonzero we are about to enter
3176 the scope of template-id indicated. */
3178 tree
3179 finish_template_type (tree name, tree args, int entering_scope)
3181 tree type;
3183 type = lookup_template_class (name, args,
3184 NULL_TREE, NULL_TREE, entering_scope,
3185 tf_warning_or_error | tf_user);
3187 /* If we might be entering the scope of a partial specialization,
3188 find the one with the right constraints. */
3189 if (flag_concepts
3190 && entering_scope
3191 && CLASS_TYPE_P (type)
3192 && CLASSTYPE_TEMPLATE_INFO (type)
3193 && dependent_type_p (type)
3194 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
3195 type = fixup_template_type (type);
3197 if (type == error_mark_node)
3198 return type;
3199 else if (CLASS_TYPE_P (type) && !alias_type_or_template_p (type))
3200 return TYPE_STUB_DECL (type);
3201 else
3202 return TYPE_NAME (type);
3205 /* Finish processing a BASE_CLASS with the indicated ACCESS_SPECIFIER.
3206 Return a TREE_LIST containing the ACCESS_SPECIFIER and the
3207 BASE_CLASS, or NULL_TREE if an error occurred. The
3208 ACCESS_SPECIFIER is one of
3209 access_{default,public,protected_private}_node. For a virtual base
3210 we set TREE_TYPE. */
3212 tree
3213 finish_base_specifier (tree base, tree access, bool virtual_p)
3215 tree result;
3217 if (base == error_mark_node)
3219 error ("invalid base-class specification");
3220 result = NULL_TREE;
3222 else if (! MAYBE_CLASS_TYPE_P (base))
3224 error ("%qT is not a class type", base);
3225 result = NULL_TREE;
3227 else
3229 if (cp_type_quals (base) != 0)
3231 /* DR 484: Can a base-specifier name a cv-qualified
3232 class type? */
3233 base = TYPE_MAIN_VARIANT (base);
3235 result = build_tree_list (access, base);
3236 if (virtual_p)
3237 TREE_TYPE (result) = integer_type_node;
3240 return result;
3243 /* If FNS is a member function, a set of member functions, or a
3244 template-id referring to one or more member functions, return a
3245 BASELINK for FNS, incorporating the current access context.
3246 Otherwise, return FNS unchanged. */
3248 tree
3249 baselink_for_fns (tree fns)
3251 tree scope;
3252 tree cl;
3254 if (BASELINK_P (fns)
3255 || error_operand_p (fns))
3256 return fns;
3258 scope = ovl_scope (fns);
3259 if (!CLASS_TYPE_P (scope))
3260 return fns;
3262 cl = currently_open_derived_class (scope);
3263 if (!cl)
3264 cl = scope;
3265 cl = TYPE_BINFO (cl);
3266 return build_baselink (cl, cl, fns, /*optype=*/NULL_TREE);
3269 /* Returns true iff DECL is a variable from a function outside
3270 the current one. */
3272 static bool
3273 outer_var_p (tree decl)
3275 return ((VAR_P (decl) || TREE_CODE (decl) == PARM_DECL)
3276 && DECL_FUNCTION_SCOPE_P (decl)
3277 /* Don't get confused by temporaries. */
3278 && DECL_NAME (decl)
3279 && (DECL_CONTEXT (decl) != current_function_decl
3280 || parsing_nsdmi ()));
3283 /* As above, but also checks that DECL is automatic. */
3285 bool
3286 outer_automatic_var_p (tree decl)
3288 return (outer_var_p (decl)
3289 && !TREE_STATIC (decl));
3292 /* DECL satisfies outer_automatic_var_p. Possibly complain about it or
3293 rewrite it for lambda capture.
3295 If ODR_USE is true, we're being called from mark_use, and we complain about
3296 use of constant variables. If ODR_USE is false, we're being called for the
3297 id-expression, and we do lambda capture. */
3299 tree
3300 process_outer_var_ref (tree decl, tsubst_flags_t complain, bool odr_use)
3302 if (cp_unevaluated_operand)
3303 /* It's not a use (3.2) if we're in an unevaluated context. */
3304 return decl;
3305 if (decl == error_mark_node)
3306 return decl;
3308 tree context = DECL_CONTEXT (decl);
3309 tree containing_function = current_function_decl;
3310 tree lambda_stack = NULL_TREE;
3311 tree lambda_expr = NULL_TREE;
3312 tree initializer = convert_from_reference (decl);
3314 /* Mark it as used now even if the use is ill-formed. */
3315 if (!mark_used (decl, complain))
3316 return error_mark_node;
3318 if (parsing_nsdmi ())
3319 containing_function = NULL_TREE;
3321 if (containing_function && LAMBDA_FUNCTION_P (containing_function))
3323 /* Check whether we've already built a proxy. */
3324 tree var = decl;
3325 while (is_capture_proxy_with_ref (var))
3326 var = DECL_CAPTURED_VARIABLE (var);
3327 tree d = retrieve_local_specialization (var);
3329 if (d && d != decl && is_capture_proxy (d))
3331 if (DECL_CONTEXT (d) == containing_function)
3332 /* We already have an inner proxy. */
3333 return d;
3334 else
3335 /* We need to capture an outer proxy. */
3336 return process_outer_var_ref (d, complain, odr_use);
3340 /* If we are in a lambda function, we can move out until we hit
3341 1. the context,
3342 2. a non-lambda function, or
3343 3. a non-default capturing lambda function. */
3344 while (context != containing_function
3345 /* containing_function can be null with invalid generic lambdas. */
3346 && containing_function
3347 && LAMBDA_FUNCTION_P (containing_function))
3349 tree closure = DECL_CONTEXT (containing_function);
3350 lambda_expr = CLASSTYPE_LAMBDA_EXPR (closure);
3352 if (TYPE_CLASS_SCOPE_P (closure))
3353 /* A lambda in an NSDMI (c++/64496). */
3354 break;
3356 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr)
3357 == CPLD_NONE)
3358 break;
3360 lambda_stack = tree_cons (NULL_TREE,
3361 lambda_expr,
3362 lambda_stack);
3364 containing_function
3365 = decl_function_context (containing_function);
3368 /* In a lambda within a template, wait until instantiation
3369 time to implicitly capture. */
3370 if (context == containing_function
3371 && DECL_TEMPLATE_INFO (containing_function)
3372 && uses_template_parms (DECL_TI_ARGS (containing_function)))
3373 return decl;
3375 if (lambda_expr && VAR_P (decl)
3376 && DECL_ANON_UNION_VAR_P (decl))
3378 if (complain & tf_error)
3379 error ("cannot capture member %qD of anonymous union", decl);
3380 return error_mark_node;
3382 /* Do lambda capture when processing the id-expression, not when
3383 odr-using a variable. */
3384 if (!odr_use && context == containing_function)
3386 decl = add_default_capture (lambda_stack,
3387 /*id=*/DECL_NAME (decl),
3388 initializer);
3390 /* Only an odr-use of an outer automatic variable causes an
3391 error, and a constant variable can decay to a prvalue
3392 constant without odr-use. So don't complain yet. */
3393 else if (!odr_use && decl_constant_var_p (decl))
3394 return decl;
3395 else if (lambda_expr)
3397 if (complain & tf_error)
3399 error ("%qD is not captured", decl);
3400 tree closure = LAMBDA_EXPR_CLOSURE (lambda_expr);
3401 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr)
3402 == CPLD_NONE)
3403 inform (location_of (closure),
3404 "the lambda has no capture-default");
3405 else if (TYPE_CLASS_SCOPE_P (closure))
3406 inform (UNKNOWN_LOCATION, "lambda in local class %q+T cannot "
3407 "capture variables from the enclosing context",
3408 TYPE_CONTEXT (closure));
3409 inform (DECL_SOURCE_LOCATION (decl), "%q#D declared here", decl);
3411 return error_mark_node;
3413 else
3415 if (complain & tf_error)
3417 error (VAR_P (decl)
3418 ? G_("use of local variable with automatic storage from "
3419 "containing function")
3420 : G_("use of parameter from containing function"));
3421 inform (DECL_SOURCE_LOCATION (decl), "%q#D declared here", decl);
3423 return error_mark_node;
3425 return decl;
3428 /* ID_EXPRESSION is a representation of parsed, but unprocessed,
3429 id-expression. (See cp_parser_id_expression for details.) SCOPE,
3430 if non-NULL, is the type or namespace used to explicitly qualify
3431 ID_EXPRESSION. DECL is the entity to which that name has been
3432 resolved.
3434 *CONSTANT_EXPRESSION_P is true if we are presently parsing a
3435 constant-expression. In that case, *NON_CONSTANT_EXPRESSION_P will
3436 be set to true if this expression isn't permitted in a
3437 constant-expression, but it is otherwise not set by this function.
3438 *ALLOW_NON_CONSTANT_EXPRESSION_P is true if we are parsing a
3439 constant-expression, but a non-constant expression is also
3440 permissible.
3442 DONE is true if this expression is a complete postfix-expression;
3443 it is false if this expression is followed by '->', '[', '(', etc.
3444 ADDRESS_P is true iff this expression is the operand of '&'.
3445 TEMPLATE_P is true iff the qualified-id was of the form
3446 "A::template B". TEMPLATE_ARG_P is true iff this qualified name
3447 appears as a template argument.
3449 If an error occurs, and it is the kind of error that might cause
3450 the parser to abort a tentative parse, *ERROR_MSG is filled in. It
3451 is the caller's responsibility to issue the message. *ERROR_MSG
3452 will be a string with static storage duration, so the caller need
3453 not "free" it.
3455 Return an expression for the entity, after issuing appropriate
3456 diagnostics. This function is also responsible for transforming a
3457 reference to a non-static member into a COMPONENT_REF that makes
3458 the use of "this" explicit.
3460 Upon return, *IDK will be filled in appropriately. */
3461 cp_expr
3462 finish_id_expression (tree id_expression,
3463 tree decl,
3464 tree scope,
3465 cp_id_kind *idk,
3466 bool integral_constant_expression_p,
3467 bool allow_non_integral_constant_expression_p,
3468 bool *non_integral_constant_expression_p,
3469 bool template_p,
3470 bool done,
3471 bool address_p,
3472 bool template_arg_p,
3473 const char **error_msg,
3474 location_t location)
3476 decl = strip_using_decl (decl);
3478 /* Initialize the output parameters. */
3479 *idk = CP_ID_KIND_NONE;
3480 *error_msg = NULL;
3482 if (id_expression == error_mark_node)
3483 return error_mark_node;
3484 /* If we have a template-id, then no further lookup is
3485 required. If the template-id was for a template-class, we
3486 will sometimes have a TYPE_DECL at this point. */
3487 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3488 || TREE_CODE (decl) == TYPE_DECL)
3490 /* Look up the name. */
3491 else
3493 if (decl == error_mark_node)
3495 /* Name lookup failed. */
3496 if (scope
3497 && (!TYPE_P (scope)
3498 || (!dependent_type_p (scope)
3499 && !(identifier_p (id_expression)
3500 && IDENTIFIER_CONV_OP_P (id_expression)
3501 && dependent_type_p (TREE_TYPE (id_expression))))))
3503 /* If the qualifying type is non-dependent (and the name
3504 does not name a conversion operator to a dependent
3505 type), issue an error. */
3506 qualified_name_lookup_error (scope, id_expression, decl, location);
3507 return error_mark_node;
3509 else if (!scope)
3511 /* It may be resolved via Koenig lookup. */
3512 *idk = CP_ID_KIND_UNQUALIFIED;
3513 return id_expression;
3515 else
3516 decl = id_expression;
3518 /* If DECL is a variable that would be out of scope under
3519 ANSI/ISO rules, but in scope in the ARM, name lookup
3520 will succeed. Issue a diagnostic here. */
3521 else
3522 decl = check_for_out_of_scope_variable (decl);
3524 /* Remember that the name was used in the definition of
3525 the current class so that we can check later to see if
3526 the meaning would have been different after the class
3527 was entirely defined. */
3528 if (!scope && decl != error_mark_node && identifier_p (id_expression))
3529 maybe_note_name_used_in_class (id_expression, decl);
3531 /* A use in unevaluated operand might not be instantiated appropriately
3532 if tsubst_copy builds a dummy parm, or if we never instantiate a
3533 generic lambda, so mark it now. */
3534 if (processing_template_decl && cp_unevaluated_operand)
3535 mark_type_use (decl);
3537 /* Disallow uses of local variables from containing functions, except
3538 within lambda-expressions. */
3539 if (outer_automatic_var_p (decl))
3541 decl = process_outer_var_ref (decl, tf_warning_or_error);
3542 if (decl == error_mark_node)
3543 return error_mark_node;
3546 /* Also disallow uses of function parameters outside the function
3547 body, except inside an unevaluated context (i.e. decltype). */
3548 if (TREE_CODE (decl) == PARM_DECL
3549 && DECL_CONTEXT (decl) == NULL_TREE
3550 && !cp_unevaluated_operand)
3552 *error_msg = G_("use of parameter outside function body");
3553 return error_mark_node;
3557 /* If we didn't find anything, or what we found was a type,
3558 then this wasn't really an id-expression. */
3559 if (TREE_CODE (decl) == TEMPLATE_DECL
3560 && !DECL_FUNCTION_TEMPLATE_P (decl))
3562 *error_msg = G_("missing template arguments");
3563 return error_mark_node;
3565 else if (TREE_CODE (decl) == TYPE_DECL
3566 || TREE_CODE (decl) == NAMESPACE_DECL)
3568 *error_msg = G_("expected primary-expression");
3569 return error_mark_node;
3572 /* If the name resolved to a template parameter, there is no
3573 need to look it up again later. */
3574 if ((TREE_CODE (decl) == CONST_DECL && DECL_TEMPLATE_PARM_P (decl))
3575 || TREE_CODE (decl) == TEMPLATE_PARM_INDEX)
3577 tree r;
3579 *idk = CP_ID_KIND_NONE;
3580 if (TREE_CODE (decl) == TEMPLATE_PARM_INDEX)
3581 decl = TEMPLATE_PARM_DECL (decl);
3582 r = convert_from_reference (DECL_INITIAL (decl));
3584 if (integral_constant_expression_p
3585 && !dependent_type_p (TREE_TYPE (decl))
3586 && !(INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (r))))
3588 if (!allow_non_integral_constant_expression_p)
3589 error ("template parameter %qD of type %qT is not allowed in "
3590 "an integral constant expression because it is not of "
3591 "integral or enumeration type", decl, TREE_TYPE (decl));
3592 *non_integral_constant_expression_p = true;
3594 return r;
3596 else
3598 bool dependent_p = type_dependent_expression_p (decl);
3600 /* If the declaration was explicitly qualified indicate
3601 that. The semantics of `A::f(3)' are different than
3602 `f(3)' if `f' is virtual. */
3603 *idk = (scope
3604 ? CP_ID_KIND_QUALIFIED
3605 : (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3606 ? CP_ID_KIND_TEMPLATE_ID
3607 : (dependent_p
3608 ? CP_ID_KIND_UNQUALIFIED_DEPENDENT
3609 : CP_ID_KIND_UNQUALIFIED)));
3611 if (dependent_p
3612 && DECL_P (decl)
3613 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (decl)))
3614 /* Dependent type attributes on the decl mean that the TREE_TYPE is
3615 wrong, so just return the identifier. */
3616 return id_expression;
3618 if (TREE_CODE (decl) == NAMESPACE_DECL)
3620 error ("use of namespace %qD as expression", decl);
3621 return error_mark_node;
3623 else if (DECL_CLASS_TEMPLATE_P (decl))
3625 error ("use of class template %qT as expression", decl);
3626 return error_mark_node;
3628 else if (TREE_CODE (decl) == TREE_LIST)
3630 /* Ambiguous reference to base members. */
3631 error ("request for member %qD is ambiguous in "
3632 "multiple inheritance lattice", id_expression);
3633 print_candidates (decl);
3634 return error_mark_node;
3637 /* Mark variable-like entities as used. Functions are similarly
3638 marked either below or after overload resolution. */
3639 if ((VAR_P (decl)
3640 || TREE_CODE (decl) == PARM_DECL
3641 || TREE_CODE (decl) == CONST_DECL
3642 || TREE_CODE (decl) == RESULT_DECL)
3643 && !mark_used (decl))
3644 return error_mark_node;
3646 /* Only certain kinds of names are allowed in constant
3647 expression. Template parameters have already
3648 been handled above. */
3649 if (! error_operand_p (decl)
3650 && !dependent_p
3651 && integral_constant_expression_p
3652 && ! decl_constant_var_p (decl)
3653 && TREE_CODE (decl) != CONST_DECL
3654 && ! builtin_valid_in_constant_expr_p (decl))
3656 if (!allow_non_integral_constant_expression_p)
3658 error ("%qD cannot appear in a constant-expression", decl);
3659 return error_mark_node;
3661 *non_integral_constant_expression_p = true;
3664 tree wrap;
3665 if (VAR_P (decl)
3666 && !cp_unevaluated_operand
3667 && !processing_template_decl
3668 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl))
3669 && CP_DECL_THREAD_LOCAL_P (decl)
3670 && (wrap = get_tls_wrapper_fn (decl)))
3672 /* Replace an evaluated use of the thread_local variable with
3673 a call to its wrapper. */
3674 decl = build_cxx_call (wrap, 0, NULL, tf_warning_or_error);
3676 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3677 && !dependent_p
3678 && variable_template_p (TREE_OPERAND (decl, 0)))
3680 decl = finish_template_variable (decl);
3681 mark_used (decl);
3682 decl = convert_from_reference (decl);
3684 else if (scope)
3686 if (TREE_CODE (decl) == SCOPE_REF)
3688 gcc_assert (same_type_p (scope, TREE_OPERAND (decl, 0)));
3689 decl = TREE_OPERAND (decl, 1);
3692 decl = (adjust_result_of_qualified_name_lookup
3693 (decl, scope, current_nonlambda_class_type()));
3695 if (TREE_CODE (decl) == FUNCTION_DECL)
3696 mark_used (decl);
3698 if (TYPE_P (scope))
3699 decl = finish_qualified_id_expr (scope,
3700 decl,
3701 done,
3702 address_p,
3703 template_p,
3704 template_arg_p,
3705 tf_warning_or_error);
3706 else
3707 decl = convert_from_reference (decl);
3709 else if (TREE_CODE (decl) == FIELD_DECL)
3711 /* Since SCOPE is NULL here, this is an unqualified name.
3712 Access checking has been performed during name lookup
3713 already. Turn off checking to avoid duplicate errors. */
3714 push_deferring_access_checks (dk_no_check);
3715 decl = finish_non_static_data_member (decl, NULL_TREE,
3716 /*qualifying_scope=*/NULL_TREE);
3717 pop_deferring_access_checks ();
3719 else if (is_overloaded_fn (decl))
3721 tree first_fn = get_first_fn (decl);
3723 if (TREE_CODE (first_fn) == TEMPLATE_DECL)
3724 first_fn = DECL_TEMPLATE_RESULT (first_fn);
3726 /* [basic.def.odr]: "A function whose name appears as a
3727 potentially-evaluated expression is odr-used if it is the unique
3728 lookup result".
3730 But only mark it if it's a complete postfix-expression; in a call,
3731 ADL might select a different function, and we'll call mark_used in
3732 build_over_call. */
3733 if (done
3734 && !really_overloaded_fn (decl)
3735 && !mark_used (first_fn))
3736 return error_mark_node;
3738 if (!template_arg_p
3739 && TREE_CODE (first_fn) == FUNCTION_DECL
3740 && DECL_FUNCTION_MEMBER_P (first_fn)
3741 && !shared_member_p (decl))
3743 /* A set of member functions. */
3744 decl = maybe_dummy_object (DECL_CONTEXT (first_fn), 0);
3745 return finish_class_member_access_expr (decl, id_expression,
3746 /*template_p=*/false,
3747 tf_warning_or_error);
3750 decl = baselink_for_fns (decl);
3752 else
3754 if (DECL_P (decl) && DECL_NONLOCAL (decl)
3755 && DECL_CLASS_SCOPE_P (decl))
3757 tree context = context_for_name_lookup (decl);
3758 if (context != current_class_type)
3760 tree path = currently_open_derived_class (context);
3761 perform_or_defer_access_check (TYPE_BINFO (path),
3762 decl, decl,
3763 tf_warning_or_error);
3767 decl = convert_from_reference (decl);
3771 return cp_expr (decl, location);
3774 /* Implement the __typeof keyword: Return the type of EXPR, suitable for
3775 use as a type-specifier. */
3777 tree
3778 finish_typeof (tree expr)
3780 tree type;
3782 if (type_dependent_expression_p (expr))
3784 type = cxx_make_type (TYPEOF_TYPE);
3785 TYPEOF_TYPE_EXPR (type) = expr;
3786 SET_TYPE_STRUCTURAL_EQUALITY (type);
3788 return type;
3791 expr = mark_type_use (expr);
3793 type = unlowered_expr_type (expr);
3795 if (!type || type == unknown_type_node)
3797 error ("type of %qE is unknown", expr);
3798 return error_mark_node;
3801 return type;
3804 /* Implement the __underlying_type keyword: Return the underlying
3805 type of TYPE, suitable for use as a type-specifier. */
3807 tree
3808 finish_underlying_type (tree type)
3810 tree underlying_type;
3812 if (processing_template_decl)
3814 underlying_type = cxx_make_type (UNDERLYING_TYPE);
3815 UNDERLYING_TYPE_TYPE (underlying_type) = type;
3816 SET_TYPE_STRUCTURAL_EQUALITY (underlying_type);
3818 return underlying_type;
3821 if (!complete_type_or_else (type, NULL_TREE))
3822 return error_mark_node;
3824 if (TREE_CODE (type) != ENUMERAL_TYPE)
3826 error ("%qT is not an enumeration type", type);
3827 return error_mark_node;
3830 underlying_type = ENUM_UNDERLYING_TYPE (type);
3832 /* Fixup necessary in this case because ENUM_UNDERLYING_TYPE
3833 includes TYPE_MIN_VALUE and TYPE_MAX_VALUE information.
3834 See finish_enum_value_list for details. */
3835 if (!ENUM_FIXED_UNDERLYING_TYPE_P (type))
3836 underlying_type
3837 = c_common_type_for_mode (TYPE_MODE (underlying_type),
3838 TYPE_UNSIGNED (underlying_type));
3840 return underlying_type;
3843 /* Implement the __direct_bases keyword: Return the direct base classes
3844 of type */
3846 tree
3847 calculate_direct_bases (tree type)
3849 vec<tree, va_gc> *vector = make_tree_vector();
3850 tree bases_vec = NULL_TREE;
3851 vec<tree, va_gc> *base_binfos;
3852 tree binfo;
3853 unsigned i;
3855 complete_type (type);
3857 if (!NON_UNION_CLASS_TYPE_P (type))
3858 return make_tree_vec (0);
3860 base_binfos = BINFO_BASE_BINFOS (TYPE_BINFO (type));
3862 /* Virtual bases are initialized first */
3863 for (i = 0; base_binfos->iterate (i, &binfo); i++)
3865 if (BINFO_VIRTUAL_P (binfo))
3867 vec_safe_push (vector, binfo);
3871 /* Now non-virtuals */
3872 for (i = 0; base_binfos->iterate (i, &binfo); i++)
3874 if (!BINFO_VIRTUAL_P (binfo))
3876 vec_safe_push (vector, binfo);
3881 bases_vec = make_tree_vec (vector->length ());
3883 for (i = 0; i < vector->length (); ++i)
3885 TREE_VEC_ELT (bases_vec, i) = BINFO_TYPE ((*vector)[i]);
3887 return bases_vec;
3890 /* Implement the __bases keyword: Return the base classes
3891 of type */
3893 /* Find morally non-virtual base classes by walking binfo hierarchy */
3894 /* Virtual base classes are handled separately in finish_bases */
3896 static tree
3897 dfs_calculate_bases_pre (tree binfo, void * /*data_*/)
3899 /* Don't walk bases of virtual bases */
3900 return BINFO_VIRTUAL_P (binfo) ? dfs_skip_bases : NULL_TREE;
3903 static tree
3904 dfs_calculate_bases_post (tree binfo, void *data_)
3906 vec<tree, va_gc> **data = ((vec<tree, va_gc> **) data_);
3907 if (!BINFO_VIRTUAL_P (binfo))
3909 vec_safe_push (*data, BINFO_TYPE (binfo));
3911 return NULL_TREE;
3914 /* Calculates the morally non-virtual base classes of a class */
3915 static vec<tree, va_gc> *
3916 calculate_bases_helper (tree type)
3918 vec<tree, va_gc> *vector = make_tree_vector();
3920 /* Now add non-virtual base classes in order of construction */
3921 if (TYPE_BINFO (type))
3922 dfs_walk_all (TYPE_BINFO (type),
3923 dfs_calculate_bases_pre, dfs_calculate_bases_post, &vector);
3924 return vector;
3927 tree
3928 calculate_bases (tree type)
3930 vec<tree, va_gc> *vector = make_tree_vector();
3931 tree bases_vec = NULL_TREE;
3932 unsigned i;
3933 vec<tree, va_gc> *vbases;
3934 vec<tree, va_gc> *nonvbases;
3935 tree binfo;
3937 complete_type (type);
3939 if (!NON_UNION_CLASS_TYPE_P (type))
3940 return make_tree_vec (0);
3942 /* First go through virtual base classes */
3943 for (vbases = CLASSTYPE_VBASECLASSES (type), i = 0;
3944 vec_safe_iterate (vbases, i, &binfo); i++)
3946 vec<tree, va_gc> *vbase_bases;
3947 vbase_bases = calculate_bases_helper (BINFO_TYPE (binfo));
3948 vec_safe_splice (vector, vbase_bases);
3949 release_tree_vector (vbase_bases);
3952 /* Now for the non-virtual bases */
3953 nonvbases = calculate_bases_helper (type);
3954 vec_safe_splice (vector, nonvbases);
3955 release_tree_vector (nonvbases);
3957 /* Note that during error recovery vector->length can even be zero. */
3958 if (vector->length () > 1)
3960 /* Last element is entire class, so don't copy */
3961 bases_vec = make_tree_vec (vector->length() - 1);
3963 for (i = 0; i < vector->length () - 1; ++i)
3964 TREE_VEC_ELT (bases_vec, i) = (*vector)[i];
3966 else
3967 bases_vec = make_tree_vec (0);
3969 release_tree_vector (vector);
3970 return bases_vec;
3973 tree
3974 finish_bases (tree type, bool direct)
3976 tree bases = NULL_TREE;
3978 if (!processing_template_decl)
3980 /* Parameter packs can only be used in templates */
3981 error ("Parameter pack __bases only valid in template declaration");
3982 return error_mark_node;
3985 bases = cxx_make_type (BASES);
3986 BASES_TYPE (bases) = type;
3987 BASES_DIRECT (bases) = direct;
3988 SET_TYPE_STRUCTURAL_EQUALITY (bases);
3990 return bases;
3993 /* Perform C++-specific checks for __builtin_offsetof before calling
3994 fold_offsetof. */
3996 tree
3997 finish_offsetof (tree object_ptr, tree expr, location_t loc)
3999 /* If we're processing a template, we can't finish the semantics yet.
4000 Otherwise we can fold the entire expression now. */
4001 if (processing_template_decl)
4003 expr = build2 (OFFSETOF_EXPR, size_type_node, expr, object_ptr);
4004 SET_EXPR_LOCATION (expr, loc);
4005 return expr;
4008 if (TREE_CODE (expr) == PSEUDO_DTOR_EXPR)
4010 error ("cannot apply %<offsetof%> to destructor %<~%T%>",
4011 TREE_OPERAND (expr, 2));
4012 return error_mark_node;
4014 if (TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE
4015 || TREE_CODE (TREE_TYPE (expr)) == METHOD_TYPE
4016 || TREE_TYPE (expr) == unknown_type_node)
4018 if (INDIRECT_REF_P (expr))
4019 error ("second operand of %<offsetof%> is neither a single "
4020 "identifier nor a sequence of member accesses and "
4021 "array references");
4022 else
4024 if (TREE_CODE (expr) == COMPONENT_REF
4025 || TREE_CODE (expr) == COMPOUND_EXPR)
4026 expr = TREE_OPERAND (expr, 1);
4027 error ("cannot apply %<offsetof%> to member function %qD", expr);
4029 return error_mark_node;
4031 if (REFERENCE_REF_P (expr))
4032 expr = TREE_OPERAND (expr, 0);
4033 if (!complete_type_or_else (TREE_TYPE (TREE_TYPE (object_ptr)), object_ptr))
4034 return error_mark_node;
4035 if (warn_invalid_offsetof
4036 && CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (object_ptr)))
4037 && CLASSTYPE_NON_STD_LAYOUT (TREE_TYPE (TREE_TYPE (object_ptr)))
4038 && cp_unevaluated_operand == 0)
4039 pedwarn (loc, OPT_Winvalid_offsetof,
4040 "offsetof within non-standard-layout type %qT is undefined",
4041 TREE_TYPE (TREE_TYPE (object_ptr)));
4042 return fold_offsetof (expr);
4045 /* Replace the AGGR_INIT_EXPR at *TP with an equivalent CALL_EXPR. This
4046 function is broken out from the above for the benefit of the tree-ssa
4047 project. */
4049 void
4050 simplify_aggr_init_expr (tree *tp)
4052 tree aggr_init_expr = *tp;
4054 /* Form an appropriate CALL_EXPR. */
4055 tree fn = AGGR_INIT_EXPR_FN (aggr_init_expr);
4056 tree slot = AGGR_INIT_EXPR_SLOT (aggr_init_expr);
4057 tree type = TREE_TYPE (slot);
4059 tree call_expr;
4060 enum style_t { ctor, arg, pcc } style;
4062 if (AGGR_INIT_VIA_CTOR_P (aggr_init_expr))
4063 style = ctor;
4064 #ifdef PCC_STATIC_STRUCT_RETURN
4065 else if (1)
4066 style = pcc;
4067 #endif
4068 else
4070 gcc_assert (TREE_ADDRESSABLE (type));
4071 style = arg;
4074 call_expr = build_call_array_loc (input_location,
4075 TREE_TYPE (TREE_TYPE (TREE_TYPE (fn))),
4077 aggr_init_expr_nargs (aggr_init_expr),
4078 AGGR_INIT_EXPR_ARGP (aggr_init_expr));
4079 TREE_NOTHROW (call_expr) = TREE_NOTHROW (aggr_init_expr);
4080 CALL_FROM_THUNK_P (call_expr) = AGGR_INIT_FROM_THUNK_P (aggr_init_expr);
4081 CALL_EXPR_OPERATOR_SYNTAX (call_expr)
4082 = CALL_EXPR_OPERATOR_SYNTAX (aggr_init_expr);
4083 CALL_EXPR_ORDERED_ARGS (call_expr) = CALL_EXPR_ORDERED_ARGS (aggr_init_expr);
4084 CALL_EXPR_REVERSE_ARGS (call_expr) = CALL_EXPR_REVERSE_ARGS (aggr_init_expr);
4086 if (style == ctor)
4088 /* Replace the first argument to the ctor with the address of the
4089 slot. */
4090 cxx_mark_addressable (slot);
4091 CALL_EXPR_ARG (call_expr, 0) =
4092 build1 (ADDR_EXPR, build_pointer_type (type), slot);
4094 else if (style == arg)
4096 /* Just mark it addressable here, and leave the rest to
4097 expand_call{,_inline}. */
4098 cxx_mark_addressable (slot);
4099 CALL_EXPR_RETURN_SLOT_OPT (call_expr) = true;
4100 call_expr = build2 (INIT_EXPR, TREE_TYPE (call_expr), slot, call_expr);
4102 else if (style == pcc)
4104 /* If we're using the non-reentrant PCC calling convention, then we
4105 need to copy the returned value out of the static buffer into the
4106 SLOT. */
4107 push_deferring_access_checks (dk_no_check);
4108 call_expr = build_aggr_init (slot, call_expr,
4109 DIRECT_BIND | LOOKUP_ONLYCONVERTING,
4110 tf_warning_or_error);
4111 pop_deferring_access_checks ();
4112 call_expr = build2 (COMPOUND_EXPR, TREE_TYPE (slot), call_expr, slot);
4115 if (AGGR_INIT_ZERO_FIRST (aggr_init_expr))
4117 tree init = build_zero_init (type, NULL_TREE,
4118 /*static_storage_p=*/false);
4119 init = build2 (INIT_EXPR, void_type_node, slot, init);
4120 call_expr = build2 (COMPOUND_EXPR, TREE_TYPE (call_expr),
4121 init, call_expr);
4124 *tp = call_expr;
4127 /* Emit all thunks to FN that should be emitted when FN is emitted. */
4129 void
4130 emit_associated_thunks (tree fn)
4132 /* When we use vcall offsets, we emit thunks with the virtual
4133 functions to which they thunk. The whole point of vcall offsets
4134 is so that you can know statically the entire set of thunks that
4135 will ever be needed for a given virtual function, thereby
4136 enabling you to output all the thunks with the function itself. */
4137 if (DECL_VIRTUAL_P (fn)
4138 /* Do not emit thunks for extern template instantiations. */
4139 && ! DECL_REALLY_EXTERN (fn))
4141 tree thunk;
4143 for (thunk = DECL_THUNKS (fn); thunk; thunk = DECL_CHAIN (thunk))
4145 if (!THUNK_ALIAS (thunk))
4147 use_thunk (thunk, /*emit_p=*/1);
4148 if (DECL_RESULT_THUNK_P (thunk))
4150 tree probe;
4152 for (probe = DECL_THUNKS (thunk);
4153 probe; probe = DECL_CHAIN (probe))
4154 use_thunk (probe, /*emit_p=*/1);
4157 else
4158 gcc_assert (!DECL_THUNKS (thunk));
4163 /* Generate RTL for FN. */
4165 bool
4166 expand_or_defer_fn_1 (tree fn)
4168 /* When the parser calls us after finishing the body of a template
4169 function, we don't really want to expand the body. */
4170 if (processing_template_decl)
4172 /* Normally, collection only occurs in rest_of_compilation. So,
4173 if we don't collect here, we never collect junk generated
4174 during the processing of templates until we hit a
4175 non-template function. It's not safe to do this inside a
4176 nested class, though, as the parser may have local state that
4177 is not a GC root. */
4178 if (!function_depth)
4179 ggc_collect ();
4180 return false;
4183 gcc_assert (DECL_SAVED_TREE (fn));
4185 /* We make a decision about linkage for these functions at the end
4186 of the compilation. Until that point, we do not want the back
4187 end to output them -- but we do want it to see the bodies of
4188 these functions so that it can inline them as appropriate. */
4189 if (DECL_DECLARED_INLINE_P (fn) || DECL_IMPLICIT_INSTANTIATION (fn))
4191 if (DECL_INTERFACE_KNOWN (fn))
4192 /* We've already made a decision as to how this function will
4193 be handled. */;
4194 else if (!at_eof)
4195 tentative_decl_linkage (fn);
4196 else
4197 import_export_decl (fn);
4199 /* If the user wants us to keep all inline functions, then mark
4200 this function as needed so that finish_file will make sure to
4201 output it later. Similarly, all dllexport'd functions must
4202 be emitted; there may be callers in other DLLs. */
4203 if (DECL_DECLARED_INLINE_P (fn)
4204 && !DECL_REALLY_EXTERN (fn)
4205 && (flag_keep_inline_functions
4206 || (flag_keep_inline_dllexport
4207 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (fn)))))
4209 mark_needed (fn);
4210 DECL_EXTERNAL (fn) = 0;
4214 /* If this is a constructor or destructor body, we have to clone
4215 it. */
4216 if (maybe_clone_body (fn))
4218 /* We don't want to process FN again, so pretend we've written
4219 it out, even though we haven't. */
4220 TREE_ASM_WRITTEN (fn) = 1;
4221 /* If this is a constexpr function, keep DECL_SAVED_TREE. */
4222 if (!DECL_DECLARED_CONSTEXPR_P (fn))
4223 DECL_SAVED_TREE (fn) = NULL_TREE;
4224 return false;
4227 /* There's no reason to do any of the work here if we're only doing
4228 semantic analysis; this code just generates RTL. */
4229 if (flag_syntax_only)
4230 return false;
4232 return true;
4235 void
4236 expand_or_defer_fn (tree fn)
4238 if (expand_or_defer_fn_1 (fn))
4240 function_depth++;
4242 /* Expand or defer, at the whim of the compilation unit manager. */
4243 cgraph_node::finalize_function (fn, function_depth > 1);
4244 emit_associated_thunks (fn);
4246 function_depth--;
4250 struct nrv_data
4252 nrv_data () : visited (37) {}
4254 tree var;
4255 tree result;
4256 hash_table<nofree_ptr_hash <tree_node> > visited;
4259 /* Helper function for walk_tree, used by finalize_nrv below. */
4261 static tree
4262 finalize_nrv_r (tree* tp, int* walk_subtrees, void* data)
4264 struct nrv_data *dp = (struct nrv_data *)data;
4265 tree_node **slot;
4267 /* No need to walk into types. There wouldn't be any need to walk into
4268 non-statements, except that we have to consider STMT_EXPRs. */
4269 if (TYPE_P (*tp))
4270 *walk_subtrees = 0;
4271 /* Change all returns to just refer to the RESULT_DECL; this is a nop,
4272 but differs from using NULL_TREE in that it indicates that we care
4273 about the value of the RESULT_DECL. */
4274 else if (TREE_CODE (*tp) == RETURN_EXPR)
4275 TREE_OPERAND (*tp, 0) = dp->result;
4276 /* Change all cleanups for the NRV to only run when an exception is
4277 thrown. */
4278 else if (TREE_CODE (*tp) == CLEANUP_STMT
4279 && CLEANUP_DECL (*tp) == dp->var)
4280 CLEANUP_EH_ONLY (*tp) = 1;
4281 /* Replace the DECL_EXPR for the NRV with an initialization of the
4282 RESULT_DECL, if needed. */
4283 else if (TREE_CODE (*tp) == DECL_EXPR
4284 && DECL_EXPR_DECL (*tp) == dp->var)
4286 tree init;
4287 if (DECL_INITIAL (dp->var)
4288 && DECL_INITIAL (dp->var) != error_mark_node)
4289 init = build2 (INIT_EXPR, void_type_node, dp->result,
4290 DECL_INITIAL (dp->var));
4291 else
4292 init = build_empty_stmt (EXPR_LOCATION (*tp));
4293 DECL_INITIAL (dp->var) = NULL_TREE;
4294 SET_EXPR_LOCATION (init, EXPR_LOCATION (*tp));
4295 *tp = init;
4297 /* And replace all uses of the NRV with the RESULT_DECL. */
4298 else if (*tp == dp->var)
4299 *tp = dp->result;
4301 /* Avoid walking into the same tree more than once. Unfortunately, we
4302 can't just use walk_tree_without duplicates because it would only call
4303 us for the first occurrence of dp->var in the function body. */
4304 slot = dp->visited.find_slot (*tp, INSERT);
4305 if (*slot)
4306 *walk_subtrees = 0;
4307 else
4308 *slot = *tp;
4310 /* Keep iterating. */
4311 return NULL_TREE;
4314 /* Called from finish_function to implement the named return value
4315 optimization by overriding all the RETURN_EXPRs and pertinent
4316 CLEANUP_STMTs and replacing all occurrences of VAR with RESULT, the
4317 RESULT_DECL for the function. */
4319 void
4320 finalize_nrv (tree *tp, tree var, tree result)
4322 struct nrv_data data;
4324 /* Copy name from VAR to RESULT. */
4325 DECL_NAME (result) = DECL_NAME (var);
4326 /* Don't forget that we take its address. */
4327 TREE_ADDRESSABLE (result) = TREE_ADDRESSABLE (var);
4328 /* Finally set DECL_VALUE_EXPR to avoid assigning
4329 a stack slot at -O0 for the original var and debug info
4330 uses RESULT location for VAR. */
4331 SET_DECL_VALUE_EXPR (var, result);
4332 DECL_HAS_VALUE_EXPR_P (var) = 1;
4334 data.var = var;
4335 data.result = result;
4336 cp_walk_tree (tp, finalize_nrv_r, &data, 0);
4339 /* Create CP_OMP_CLAUSE_INFO for clause C. Returns true if it is invalid. */
4341 bool
4342 cxx_omp_create_clause_info (tree c, tree type, bool need_default_ctor,
4343 bool need_copy_ctor, bool need_copy_assignment,
4344 bool need_dtor)
4346 int save_errorcount = errorcount;
4347 tree info, t;
4349 /* Always allocate 3 elements for simplicity. These are the
4350 function decls for the ctor, dtor, and assignment op.
4351 This layout is known to the three lang hooks,
4352 cxx_omp_clause_default_init, cxx_omp_clause_copy_init,
4353 and cxx_omp_clause_assign_op. */
4354 info = make_tree_vec (3);
4355 CP_OMP_CLAUSE_INFO (c) = info;
4357 if (need_default_ctor || need_copy_ctor)
4359 if (need_default_ctor)
4360 t = get_default_ctor (type);
4361 else
4362 t = get_copy_ctor (type, tf_warning_or_error);
4364 if (t && !trivial_fn_p (t))
4365 TREE_VEC_ELT (info, 0) = t;
4368 if (need_dtor && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
4369 TREE_VEC_ELT (info, 1) = get_dtor (type, tf_warning_or_error);
4371 if (need_copy_assignment)
4373 t = get_copy_assign (type);
4375 if (t && !trivial_fn_p (t))
4376 TREE_VEC_ELT (info, 2) = t;
4379 return errorcount != save_errorcount;
4382 /* If DECL is DECL_OMP_PRIVATIZED_MEMBER, return corresponding
4383 FIELD_DECL, otherwise return DECL itself. */
4385 static tree
4386 omp_clause_decl_field (tree decl)
4388 if (VAR_P (decl)
4389 && DECL_HAS_VALUE_EXPR_P (decl)
4390 && DECL_ARTIFICIAL (decl)
4391 && DECL_LANG_SPECIFIC (decl)
4392 && DECL_OMP_PRIVATIZED_MEMBER (decl))
4394 tree f = DECL_VALUE_EXPR (decl);
4395 if (INDIRECT_REF_P (f))
4396 f = TREE_OPERAND (f, 0);
4397 if (TREE_CODE (f) == COMPONENT_REF)
4399 f = TREE_OPERAND (f, 1);
4400 gcc_assert (TREE_CODE (f) == FIELD_DECL);
4401 return f;
4404 return NULL_TREE;
4407 /* Adjust DECL if needed for printing using %qE. */
4409 static tree
4410 omp_clause_printable_decl (tree decl)
4412 tree t = omp_clause_decl_field (decl);
4413 if (t)
4414 return t;
4415 return decl;
4418 /* For a FIELD_DECL F and corresponding DECL_OMP_PRIVATIZED_MEMBER
4419 VAR_DECL T that doesn't need a DECL_EXPR added, record it for
4420 privatization. */
4422 static void
4423 omp_note_field_privatization (tree f, tree t)
4425 if (!omp_private_member_map)
4426 omp_private_member_map = new hash_map<tree, tree>;
4427 tree &v = omp_private_member_map->get_or_insert (f);
4428 if (v == NULL_TREE)
4430 v = t;
4431 omp_private_member_vec.safe_push (f);
4432 /* Signal that we don't want to create DECL_EXPR for this dummy var. */
4433 omp_private_member_vec.safe_push (integer_zero_node);
4437 /* Privatize FIELD_DECL T, return corresponding DECL_OMP_PRIVATIZED_MEMBER
4438 dummy VAR_DECL. */
4440 tree
4441 omp_privatize_field (tree t, bool shared)
4443 tree m = finish_non_static_data_member (t, NULL_TREE, NULL_TREE);
4444 if (m == error_mark_node)
4445 return error_mark_node;
4446 if (!omp_private_member_map && !shared)
4447 omp_private_member_map = new hash_map<tree, tree>;
4448 if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE)
4450 gcc_assert (INDIRECT_REF_P (m));
4451 m = TREE_OPERAND (m, 0);
4453 tree vb = NULL_TREE;
4454 tree &v = shared ? vb : omp_private_member_map->get_or_insert (t);
4455 if (v == NULL_TREE)
4457 v = create_temporary_var (TREE_TYPE (m));
4458 retrofit_lang_decl (v);
4459 DECL_OMP_PRIVATIZED_MEMBER (v) = 1;
4460 SET_DECL_VALUE_EXPR (v, m);
4461 DECL_HAS_VALUE_EXPR_P (v) = 1;
4462 if (!shared)
4463 omp_private_member_vec.safe_push (t);
4465 return v;
4468 /* Helper function for handle_omp_array_sections. Called recursively
4469 to handle multiple array-section-subscripts. C is the clause,
4470 T current expression (initially OMP_CLAUSE_DECL), which is either
4471 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
4472 expression if specified, TREE_VALUE length expression if specified,
4473 TREE_CHAIN is what it has been specified after, or some decl.
4474 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
4475 set to true if any of the array-section-subscript could have length
4476 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
4477 first array-section-subscript which is known not to have length
4478 of one. Given say:
4479 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
4480 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
4481 all are or may have length of 1, array-section-subscript [:2] is the
4482 first one known not to have length 1. For array-section-subscript
4483 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
4484 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
4485 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
4486 case though, as some lengths could be zero. */
4488 static tree
4489 handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
4490 bool &maybe_zero_len, unsigned int &first_non_one,
4491 enum c_omp_region_type ort)
4493 tree ret, low_bound, length, type;
4494 if (TREE_CODE (t) != TREE_LIST)
4496 if (error_operand_p (t))
4497 return error_mark_node;
4498 if (REFERENCE_REF_P (t)
4499 && TREE_CODE (TREE_OPERAND (t, 0)) == COMPONENT_REF)
4500 t = TREE_OPERAND (t, 0);
4501 ret = t;
4502 if (TREE_CODE (t) == COMPONENT_REF
4503 && ort == C_ORT_OMP
4504 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
4505 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO
4506 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FROM)
4507 && !type_dependent_expression_p (t))
4509 if (TREE_CODE (TREE_OPERAND (t, 1)) == FIELD_DECL
4510 && DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
4512 error_at (OMP_CLAUSE_LOCATION (c),
4513 "bit-field %qE in %qs clause",
4514 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4515 return error_mark_node;
4517 while (TREE_CODE (t) == COMPONENT_REF)
4519 if (TREE_TYPE (TREE_OPERAND (t, 0))
4520 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == UNION_TYPE)
4522 error_at (OMP_CLAUSE_LOCATION (c),
4523 "%qE is a member of a union", t);
4524 return error_mark_node;
4526 t = TREE_OPERAND (t, 0);
4528 if (REFERENCE_REF_P (t))
4529 t = TREE_OPERAND (t, 0);
4531 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
4533 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
4534 return NULL_TREE;
4535 if (DECL_P (t))
4536 error_at (OMP_CLAUSE_LOCATION (c),
4537 "%qD is not a variable in %qs clause", t,
4538 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4539 else
4540 error_at (OMP_CLAUSE_LOCATION (c),
4541 "%qE is not a variable in %qs clause", t,
4542 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4543 return error_mark_node;
4545 else if (TREE_CODE (t) == PARM_DECL
4546 && DECL_ARTIFICIAL (t)
4547 && DECL_NAME (t) == this_identifier)
4549 error_at (OMP_CLAUSE_LOCATION (c),
4550 "%<this%> allowed in OpenMP only in %<declare simd%>"
4551 " clauses");
4552 return error_mark_node;
4554 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
4555 && VAR_P (t) && CP_DECL_THREAD_LOCAL_P (t))
4557 error_at (OMP_CLAUSE_LOCATION (c),
4558 "%qD is threadprivate variable in %qs clause", t,
4559 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4560 return error_mark_node;
4562 if (type_dependent_expression_p (ret))
4563 return NULL_TREE;
4564 ret = convert_from_reference (ret);
4565 return ret;
4568 if (ort == C_ORT_OMP
4569 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
4570 && TREE_CODE (TREE_CHAIN (t)) == FIELD_DECL)
4571 TREE_CHAIN (t) = omp_privatize_field (TREE_CHAIN (t), false);
4572 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
4573 maybe_zero_len, first_non_one, ort);
4574 if (ret == error_mark_node || ret == NULL_TREE)
4575 return ret;
4577 type = TREE_TYPE (ret);
4578 low_bound = TREE_PURPOSE (t);
4579 length = TREE_VALUE (t);
4580 if ((low_bound && type_dependent_expression_p (low_bound))
4581 || (length && type_dependent_expression_p (length)))
4582 return NULL_TREE;
4584 if (low_bound == error_mark_node || length == error_mark_node)
4585 return error_mark_node;
4587 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
4589 error_at (OMP_CLAUSE_LOCATION (c),
4590 "low bound %qE of array section does not have integral type",
4591 low_bound);
4592 return error_mark_node;
4594 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
4596 error_at (OMP_CLAUSE_LOCATION (c),
4597 "length %qE of array section does not have integral type",
4598 length);
4599 return error_mark_node;
4601 if (low_bound)
4602 low_bound = mark_rvalue_use (low_bound);
4603 if (length)
4604 length = mark_rvalue_use (length);
4605 /* We need to reduce to real constant-values for checks below. */
4606 if (length)
4607 length = fold_simple (length);
4608 if (low_bound)
4609 low_bound = fold_simple (low_bound);
4610 if (low_bound
4611 && TREE_CODE (low_bound) == INTEGER_CST
4612 && TYPE_PRECISION (TREE_TYPE (low_bound))
4613 > TYPE_PRECISION (sizetype))
4614 low_bound = fold_convert (sizetype, low_bound);
4615 if (length
4616 && TREE_CODE (length) == INTEGER_CST
4617 && TYPE_PRECISION (TREE_TYPE (length))
4618 > TYPE_PRECISION (sizetype))
4619 length = fold_convert (sizetype, length);
4620 if (low_bound == NULL_TREE)
4621 low_bound = integer_zero_node;
4623 if (length != NULL_TREE)
4625 if (!integer_nonzerop (length))
4627 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
4628 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
4630 if (integer_zerop (length))
4632 error_at (OMP_CLAUSE_LOCATION (c),
4633 "zero length array section in %qs clause",
4634 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4635 return error_mark_node;
4638 else
4639 maybe_zero_len = true;
4641 if (first_non_one == types.length ()
4642 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
4643 first_non_one++;
4645 if (TREE_CODE (type) == ARRAY_TYPE)
4647 if (length == NULL_TREE
4648 && (TYPE_DOMAIN (type) == NULL_TREE
4649 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
4651 error_at (OMP_CLAUSE_LOCATION (c),
4652 "for unknown bound array type length expression must "
4653 "be specified");
4654 return error_mark_node;
4656 if (TREE_CODE (low_bound) == INTEGER_CST
4657 && tree_int_cst_sgn (low_bound) == -1)
4659 error_at (OMP_CLAUSE_LOCATION (c),
4660 "negative low bound in array section in %qs clause",
4661 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4662 return error_mark_node;
4664 if (length != NULL_TREE
4665 && TREE_CODE (length) == INTEGER_CST
4666 && tree_int_cst_sgn (length) == -1)
4668 error_at (OMP_CLAUSE_LOCATION (c),
4669 "negative length in array section in %qs clause",
4670 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4671 return error_mark_node;
4673 if (TYPE_DOMAIN (type)
4674 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
4675 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
4676 == INTEGER_CST)
4678 tree size
4679 = fold_convert (sizetype, TYPE_MAX_VALUE (TYPE_DOMAIN (type)));
4680 size = size_binop (PLUS_EXPR, size, size_one_node);
4681 if (TREE_CODE (low_bound) == INTEGER_CST)
4683 if (tree_int_cst_lt (size, low_bound))
4685 error_at (OMP_CLAUSE_LOCATION (c),
4686 "low bound %qE above array section size "
4687 "in %qs clause", low_bound,
4688 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4689 return error_mark_node;
4691 if (tree_int_cst_equal (size, low_bound))
4693 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
4694 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
4696 error_at (OMP_CLAUSE_LOCATION (c),
4697 "zero length array section in %qs clause",
4698 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4699 return error_mark_node;
4701 maybe_zero_len = true;
4703 else if (length == NULL_TREE
4704 && first_non_one == types.length ()
4705 && tree_int_cst_equal
4706 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
4707 low_bound))
4708 first_non_one++;
4710 else if (length == NULL_TREE)
4712 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
4713 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION)
4714 maybe_zero_len = true;
4715 if (first_non_one == types.length ())
4716 first_non_one++;
4718 if (length && TREE_CODE (length) == INTEGER_CST)
4720 if (tree_int_cst_lt (size, length))
4722 error_at (OMP_CLAUSE_LOCATION (c),
4723 "length %qE above array section size "
4724 "in %qs clause", length,
4725 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4726 return error_mark_node;
4728 if (TREE_CODE (low_bound) == INTEGER_CST)
4730 tree lbpluslen
4731 = size_binop (PLUS_EXPR,
4732 fold_convert (sizetype, low_bound),
4733 fold_convert (sizetype, length));
4734 if (TREE_CODE (lbpluslen) == INTEGER_CST
4735 && tree_int_cst_lt (size, lbpluslen))
4737 error_at (OMP_CLAUSE_LOCATION (c),
4738 "high bound %qE above array section size "
4739 "in %qs clause", lbpluslen,
4740 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4741 return error_mark_node;
4746 else if (length == NULL_TREE)
4748 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
4749 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION)
4750 maybe_zero_len = true;
4751 if (first_non_one == types.length ())
4752 first_non_one++;
4755 /* For [lb:] we will need to evaluate lb more than once. */
4756 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
4758 tree lb = cp_save_expr (low_bound);
4759 if (lb != low_bound)
4761 TREE_PURPOSE (t) = lb;
4762 low_bound = lb;
4766 else if (TREE_CODE (type) == POINTER_TYPE)
4768 if (length == NULL_TREE)
4770 error_at (OMP_CLAUSE_LOCATION (c),
4771 "for pointer type length expression must be specified");
4772 return error_mark_node;
4774 if (length != NULL_TREE
4775 && TREE_CODE (length) == INTEGER_CST
4776 && tree_int_cst_sgn (length) == -1)
4778 error_at (OMP_CLAUSE_LOCATION (c),
4779 "negative length in array section in %qs clause",
4780 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4781 return error_mark_node;
4783 /* If there is a pointer type anywhere but in the very first
4784 array-section-subscript, the array section can't be contiguous. */
4785 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
4786 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
4788 error_at (OMP_CLAUSE_LOCATION (c),
4789 "array section is not contiguous in %qs clause",
4790 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4791 return error_mark_node;
4794 else
4796 error_at (OMP_CLAUSE_LOCATION (c),
4797 "%qE does not have pointer or array type", ret);
4798 return error_mark_node;
4800 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
4801 types.safe_push (TREE_TYPE (ret));
4802 /* We will need to evaluate lb more than once. */
4803 tree lb = cp_save_expr (low_bound);
4804 if (lb != low_bound)
4806 TREE_PURPOSE (t) = lb;
4807 low_bound = lb;
4809 ret = grok_array_decl (OMP_CLAUSE_LOCATION (c), ret, low_bound, false);
4810 return ret;
4813 /* Handle array sections for clause C. */
4815 static bool
4816 handle_omp_array_sections (tree c, enum c_omp_region_type ort)
4818 bool maybe_zero_len = false;
4819 unsigned int first_non_one = 0;
4820 auto_vec<tree, 10> types;
4821 tree first = handle_omp_array_sections_1 (c, OMP_CLAUSE_DECL (c), types,
4822 maybe_zero_len, first_non_one,
4823 ort);
4824 if (first == error_mark_node)
4825 return true;
4826 if (first == NULL_TREE)
4827 return false;
4828 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
4830 tree t = OMP_CLAUSE_DECL (c);
4831 tree tem = NULL_TREE;
4832 if (processing_template_decl)
4833 return false;
4834 /* Need to evaluate side effects in the length expressions
4835 if any. */
4836 while (TREE_CODE (t) == TREE_LIST)
4838 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
4840 if (tem == NULL_TREE)
4841 tem = TREE_VALUE (t);
4842 else
4843 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
4844 TREE_VALUE (t), tem);
4846 t = TREE_CHAIN (t);
4848 if (tem)
4849 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
4850 OMP_CLAUSE_DECL (c) = first;
4852 else
4854 unsigned int num = types.length (), i;
4855 tree t, side_effects = NULL_TREE, size = NULL_TREE;
4856 tree condition = NULL_TREE;
4858 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
4859 maybe_zero_len = true;
4860 if (processing_template_decl && maybe_zero_len)
4861 return false;
4863 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
4864 t = TREE_CHAIN (t))
4866 tree low_bound = TREE_PURPOSE (t);
4867 tree length = TREE_VALUE (t);
4869 i--;
4870 if (low_bound
4871 && TREE_CODE (low_bound) == INTEGER_CST
4872 && TYPE_PRECISION (TREE_TYPE (low_bound))
4873 > TYPE_PRECISION (sizetype))
4874 low_bound = fold_convert (sizetype, low_bound);
4875 if (length
4876 && TREE_CODE (length) == INTEGER_CST
4877 && TYPE_PRECISION (TREE_TYPE (length))
4878 > TYPE_PRECISION (sizetype))
4879 length = fold_convert (sizetype, length);
4880 if (low_bound == NULL_TREE)
4881 low_bound = integer_zero_node;
4882 if (!maybe_zero_len && i > first_non_one)
4884 if (integer_nonzerop (low_bound))
4885 goto do_warn_noncontiguous;
4886 if (length != NULL_TREE
4887 && TREE_CODE (length) == INTEGER_CST
4888 && TYPE_DOMAIN (types[i])
4889 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
4890 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
4891 == INTEGER_CST)
4893 tree size;
4894 size = size_binop (PLUS_EXPR,
4895 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
4896 size_one_node);
4897 if (!tree_int_cst_equal (length, size))
4899 do_warn_noncontiguous:
4900 error_at (OMP_CLAUSE_LOCATION (c),
4901 "array section is not contiguous in %qs "
4902 "clause",
4903 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4904 return true;
4907 if (!processing_template_decl
4908 && length != NULL_TREE
4909 && TREE_SIDE_EFFECTS (length))
4911 if (side_effects == NULL_TREE)
4912 side_effects = length;
4913 else
4914 side_effects = build2 (COMPOUND_EXPR,
4915 TREE_TYPE (side_effects),
4916 length, side_effects);
4919 else if (processing_template_decl)
4920 continue;
4921 else
4923 tree l;
4925 if (i > first_non_one
4926 && ((length && integer_nonzerop (length))
4927 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION))
4928 continue;
4929 if (length)
4930 l = fold_convert (sizetype, length);
4931 else
4933 l = size_binop (PLUS_EXPR,
4934 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
4935 size_one_node);
4936 l = size_binop (MINUS_EXPR, l,
4937 fold_convert (sizetype, low_bound));
4939 if (i > first_non_one)
4941 l = fold_build2 (NE_EXPR, boolean_type_node, l,
4942 size_zero_node);
4943 if (condition == NULL_TREE)
4944 condition = l;
4945 else
4946 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
4947 l, condition);
4949 else if (size == NULL_TREE)
4951 size = size_in_bytes (TREE_TYPE (types[i]));
4952 tree eltype = TREE_TYPE (types[num - 1]);
4953 while (TREE_CODE (eltype) == ARRAY_TYPE)
4954 eltype = TREE_TYPE (eltype);
4955 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
4956 size = size_binop (EXACT_DIV_EXPR, size,
4957 size_in_bytes (eltype));
4958 size = size_binop (MULT_EXPR, size, l);
4959 if (condition)
4960 size = fold_build3 (COND_EXPR, sizetype, condition,
4961 size, size_zero_node);
4963 else
4964 size = size_binop (MULT_EXPR, size, l);
4967 if (!processing_template_decl)
4969 if (side_effects)
4970 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
4971 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
4973 size = size_binop (MINUS_EXPR, size, size_one_node);
4974 tree index_type = build_index_type (size);
4975 tree eltype = TREE_TYPE (first);
4976 while (TREE_CODE (eltype) == ARRAY_TYPE)
4977 eltype = TREE_TYPE (eltype);
4978 tree type = build_array_type (eltype, index_type);
4979 tree ptype = build_pointer_type (eltype);
4980 if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE
4981 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (t))))
4982 t = convert_from_reference (t);
4983 else if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
4984 t = build_fold_addr_expr (t);
4985 tree t2 = build_fold_addr_expr (first);
4986 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
4987 ptrdiff_type_node, t2);
4988 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
4989 ptrdiff_type_node, t2,
4990 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
4991 ptrdiff_type_node, t));
4992 if (tree_fits_shwi_p (t2))
4993 t = build2 (MEM_REF, type, t,
4994 build_int_cst (ptype, tree_to_shwi (t2)));
4995 else
4997 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
4998 sizetype, t2);
4999 t = build2_loc (OMP_CLAUSE_LOCATION (c), POINTER_PLUS_EXPR,
5000 TREE_TYPE (t), t, t2);
5001 t = build2 (MEM_REF, type, t, build_int_cst (ptype, 0));
5003 OMP_CLAUSE_DECL (c) = t;
5004 return false;
5006 OMP_CLAUSE_DECL (c) = first;
5007 OMP_CLAUSE_SIZE (c) = size;
5008 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
5009 || (TREE_CODE (t) == COMPONENT_REF
5010 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE))
5011 return false;
5012 if (ort == C_ORT_OMP || ort == C_ORT_ACC)
5013 switch (OMP_CLAUSE_MAP_KIND (c))
5015 case GOMP_MAP_ALLOC:
5016 case GOMP_MAP_TO:
5017 case GOMP_MAP_FROM:
5018 case GOMP_MAP_TOFROM:
5019 case GOMP_MAP_ALWAYS_TO:
5020 case GOMP_MAP_ALWAYS_FROM:
5021 case GOMP_MAP_ALWAYS_TOFROM:
5022 case GOMP_MAP_RELEASE:
5023 case GOMP_MAP_DELETE:
5024 case GOMP_MAP_FORCE_TO:
5025 case GOMP_MAP_FORCE_FROM:
5026 case GOMP_MAP_FORCE_TOFROM:
5027 case GOMP_MAP_FORCE_PRESENT:
5028 OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (c) = 1;
5029 break;
5030 default:
5031 break;
5033 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
5034 OMP_CLAUSE_MAP);
5035 if ((ort & C_ORT_OMP_DECLARE_SIMD) != C_ORT_OMP && ort != C_ORT_ACC)
5036 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_POINTER);
5037 else if (TREE_CODE (t) == COMPONENT_REF)
5038 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ALWAYS_POINTER);
5039 else if (REFERENCE_REF_P (t)
5040 && TREE_CODE (TREE_OPERAND (t, 0)) == COMPONENT_REF)
5042 t = TREE_OPERAND (t, 0);
5043 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ALWAYS_POINTER);
5045 else
5046 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_FIRSTPRIVATE_POINTER);
5047 if (OMP_CLAUSE_MAP_KIND (c2) != GOMP_MAP_FIRSTPRIVATE_POINTER
5048 && !cxx_mark_addressable (t))
5049 return false;
5050 OMP_CLAUSE_DECL (c2) = t;
5051 t = build_fold_addr_expr (first);
5052 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
5053 ptrdiff_type_node, t);
5054 tree ptr = OMP_CLAUSE_DECL (c2);
5055 ptr = convert_from_reference (ptr);
5056 if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
5057 ptr = build_fold_addr_expr (ptr);
5058 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
5059 ptrdiff_type_node, t,
5060 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
5061 ptrdiff_type_node, ptr));
5062 OMP_CLAUSE_SIZE (c2) = t;
5063 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
5064 OMP_CLAUSE_CHAIN (c) = c2;
5065 ptr = OMP_CLAUSE_DECL (c2);
5066 if (OMP_CLAUSE_MAP_KIND (c2) != GOMP_MAP_FIRSTPRIVATE_POINTER
5067 && TREE_CODE (TREE_TYPE (ptr)) == REFERENCE_TYPE
5068 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (ptr))))
5070 tree c3 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
5071 OMP_CLAUSE_MAP);
5072 OMP_CLAUSE_SET_MAP_KIND (c3, OMP_CLAUSE_MAP_KIND (c2));
5073 OMP_CLAUSE_DECL (c3) = ptr;
5074 if (OMP_CLAUSE_MAP_KIND (c2) == GOMP_MAP_ALWAYS_POINTER)
5075 OMP_CLAUSE_DECL (c2) = build_simple_mem_ref (ptr);
5076 else
5077 OMP_CLAUSE_DECL (c2) = convert_from_reference (ptr);
5078 OMP_CLAUSE_SIZE (c3) = size_zero_node;
5079 OMP_CLAUSE_CHAIN (c3) = OMP_CLAUSE_CHAIN (c2);
5080 OMP_CLAUSE_CHAIN (c2) = c3;
5084 return false;
5087 /* Return identifier to look up for omp declare reduction. */
5089 tree
5090 omp_reduction_id (enum tree_code reduction_code, tree reduction_id, tree type)
5092 const char *p = NULL;
5093 const char *m = NULL;
5094 switch (reduction_code)
5096 case PLUS_EXPR:
5097 case MULT_EXPR:
5098 case MINUS_EXPR:
5099 case BIT_AND_EXPR:
5100 case BIT_XOR_EXPR:
5101 case BIT_IOR_EXPR:
5102 case TRUTH_ANDIF_EXPR:
5103 case TRUTH_ORIF_EXPR:
5104 reduction_id = ovl_op_identifier (false, reduction_code);
5105 break;
5106 case MIN_EXPR:
5107 p = "min";
5108 break;
5109 case MAX_EXPR:
5110 p = "max";
5111 break;
5112 default:
5113 break;
5116 if (p == NULL)
5118 if (TREE_CODE (reduction_id) != IDENTIFIER_NODE)
5119 return error_mark_node;
5120 p = IDENTIFIER_POINTER (reduction_id);
5123 if (type != NULL_TREE)
5124 m = mangle_type_string (TYPE_MAIN_VARIANT (type));
5126 const char prefix[] = "omp declare reduction ";
5127 size_t lenp = sizeof (prefix);
5128 if (strncmp (p, prefix, lenp - 1) == 0)
5129 lenp = 1;
5130 size_t len = strlen (p);
5131 size_t lenm = m ? strlen (m) + 1 : 0;
5132 char *name = XALLOCAVEC (char, lenp + len + lenm);
5133 if (lenp > 1)
5134 memcpy (name, prefix, lenp - 1);
5135 memcpy (name + lenp - 1, p, len + 1);
5136 if (m)
5138 name[lenp + len - 1] = '~';
5139 memcpy (name + lenp + len, m, lenm);
5141 return get_identifier (name);
5144 /* Lookup OpenMP UDR ID for TYPE, return the corresponding artificial
5145 FUNCTION_DECL or NULL_TREE if not found. */
5147 static tree
5148 omp_reduction_lookup (location_t loc, tree id, tree type, tree *baselinkp,
5149 vec<tree> *ambiguousp)
5151 tree orig_id = id;
5152 tree baselink = NULL_TREE;
5153 if (identifier_p (id))
5155 cp_id_kind idk;
5156 bool nonint_cst_expression_p;
5157 const char *error_msg;
5158 id = omp_reduction_id (ERROR_MARK, id, type);
5159 tree decl = lookup_name (id);
5160 if (decl == NULL_TREE)
5161 decl = error_mark_node;
5162 id = finish_id_expression (id, decl, NULL_TREE, &idk, false, true,
5163 &nonint_cst_expression_p, false, true, false,
5164 false, &error_msg, loc);
5165 if (idk == CP_ID_KIND_UNQUALIFIED
5166 && identifier_p (id))
5168 vec<tree, va_gc> *args = NULL;
5169 vec_safe_push (args, build_reference_type (type));
5170 id = perform_koenig_lookup (id, args, tf_none);
5173 else if (TREE_CODE (id) == SCOPE_REF)
5174 id = lookup_qualified_name (TREE_OPERAND (id, 0),
5175 omp_reduction_id (ERROR_MARK,
5176 TREE_OPERAND (id, 1),
5177 type),
5178 false, false);
5179 tree fns = id;
5180 id = NULL_TREE;
5181 if (fns && is_overloaded_fn (fns))
5183 for (lkp_iterator iter (get_fns (fns)); iter; ++iter)
5185 tree fndecl = *iter;
5186 if (TREE_CODE (fndecl) == FUNCTION_DECL)
5188 tree argtype = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
5189 if (same_type_p (TREE_TYPE (argtype), type))
5191 id = fndecl;
5192 break;
5197 if (id && BASELINK_P (fns))
5199 if (baselinkp)
5200 *baselinkp = fns;
5201 else
5202 baselink = fns;
5206 if (!id && CLASS_TYPE_P (type) && TYPE_BINFO (type))
5208 vec<tree> ambiguous = vNULL;
5209 tree binfo = TYPE_BINFO (type), base_binfo, ret = NULL_TREE;
5210 unsigned int ix;
5211 if (ambiguousp == NULL)
5212 ambiguousp = &ambiguous;
5213 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
5215 id = omp_reduction_lookup (loc, orig_id, BINFO_TYPE (base_binfo),
5216 baselinkp ? baselinkp : &baselink,
5217 ambiguousp);
5218 if (id == NULL_TREE)
5219 continue;
5220 if (!ambiguousp->is_empty ())
5221 ambiguousp->safe_push (id);
5222 else if (ret != NULL_TREE)
5224 ambiguousp->safe_push (ret);
5225 ambiguousp->safe_push (id);
5226 ret = NULL_TREE;
5228 else
5229 ret = id;
5231 if (ambiguousp != &ambiguous)
5232 return ret;
5233 if (!ambiguous.is_empty ())
5235 const char *str = _("candidates are:");
5236 unsigned int idx;
5237 tree udr;
5238 error_at (loc, "user defined reduction lookup is ambiguous");
5239 FOR_EACH_VEC_ELT (ambiguous, idx, udr)
5241 inform (DECL_SOURCE_LOCATION (udr), "%s %#qD", str, udr);
5242 if (idx == 0)
5243 str = get_spaces (str);
5245 ambiguous.release ();
5246 ret = error_mark_node;
5247 baselink = NULL_TREE;
5249 id = ret;
5251 if (id && baselink)
5252 perform_or_defer_access_check (BASELINK_BINFO (baselink),
5253 id, id, tf_warning_or_error);
5254 return id;
5257 /* Helper function for cp_parser_omp_declare_reduction_exprs
5258 and tsubst_omp_udr.
5259 Remove CLEANUP_STMT for data (omp_priv variable).
5260 Also append INIT_EXPR for DECL_INITIAL of omp_priv after its
5261 DECL_EXPR. */
5263 tree
5264 cp_remove_omp_priv_cleanup_stmt (tree *tp, int *walk_subtrees, void *data)
5266 if (TYPE_P (*tp))
5267 *walk_subtrees = 0;
5268 else if (TREE_CODE (*tp) == CLEANUP_STMT && CLEANUP_DECL (*tp) == (tree) data)
5269 *tp = CLEANUP_BODY (*tp);
5270 else if (TREE_CODE (*tp) == DECL_EXPR)
5272 tree decl = DECL_EXPR_DECL (*tp);
5273 if (!processing_template_decl
5274 && decl == (tree) data
5275 && DECL_INITIAL (decl)
5276 && DECL_INITIAL (decl) != error_mark_node)
5278 tree list = NULL_TREE;
5279 append_to_statement_list_force (*tp, &list);
5280 tree init_expr = build2 (INIT_EXPR, void_type_node,
5281 decl, DECL_INITIAL (decl));
5282 DECL_INITIAL (decl) = NULL_TREE;
5283 append_to_statement_list_force (init_expr, &list);
5284 *tp = list;
5287 return NULL_TREE;
5290 /* Data passed from cp_check_omp_declare_reduction to
5291 cp_check_omp_declare_reduction_r. */
5293 struct cp_check_omp_declare_reduction_data
5295 location_t loc;
5296 tree stmts[7];
5297 bool combiner_p;
5300 /* Helper function for cp_check_omp_declare_reduction, called via
5301 cp_walk_tree. */
5303 static tree
5304 cp_check_omp_declare_reduction_r (tree *tp, int *, void *data)
5306 struct cp_check_omp_declare_reduction_data *udr_data
5307 = (struct cp_check_omp_declare_reduction_data *) data;
5308 if (SSA_VAR_P (*tp)
5309 && !DECL_ARTIFICIAL (*tp)
5310 && *tp != DECL_EXPR_DECL (udr_data->stmts[udr_data->combiner_p ? 0 : 3])
5311 && *tp != DECL_EXPR_DECL (udr_data->stmts[udr_data->combiner_p ? 1 : 4]))
5313 location_t loc = udr_data->loc;
5314 if (udr_data->combiner_p)
5315 error_at (loc, "%<#pragma omp declare reduction%> combiner refers to "
5316 "variable %qD which is not %<omp_out%> nor %<omp_in%>",
5317 *tp);
5318 else
5319 error_at (loc, "%<#pragma omp declare reduction%> initializer refers "
5320 "to variable %qD which is not %<omp_priv%> nor "
5321 "%<omp_orig%>",
5322 *tp);
5323 return *tp;
5325 return NULL_TREE;
5328 /* Diagnose violation of OpenMP #pragma omp declare reduction restrictions. */
5330 void
5331 cp_check_omp_declare_reduction (tree udr)
5333 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (udr)));
5334 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
5335 type = TREE_TYPE (type);
5336 int i;
5337 location_t loc = DECL_SOURCE_LOCATION (udr);
5339 if (type == error_mark_node)
5340 return;
5341 if (ARITHMETIC_TYPE_P (type))
5343 static enum tree_code predef_codes[]
5344 = { PLUS_EXPR, MULT_EXPR, MINUS_EXPR, BIT_AND_EXPR, BIT_XOR_EXPR,
5345 BIT_IOR_EXPR, TRUTH_ANDIF_EXPR, TRUTH_ORIF_EXPR };
5346 for (i = 0; i < 8; i++)
5348 tree id = omp_reduction_id (predef_codes[i], NULL_TREE, NULL_TREE);
5349 const char *n1 = IDENTIFIER_POINTER (DECL_NAME (udr));
5350 const char *n2 = IDENTIFIER_POINTER (id);
5351 if (strncmp (n1, n2, IDENTIFIER_LENGTH (id)) == 0
5352 && (n1[IDENTIFIER_LENGTH (id)] == '~'
5353 || n1[IDENTIFIER_LENGTH (id)] == '\0'))
5354 break;
5357 if (i == 8
5358 && TREE_CODE (type) != COMPLEX_EXPR)
5360 const char prefix_minmax[] = "omp declare reduction m";
5361 size_t prefix_size = sizeof (prefix_minmax) - 1;
5362 const char *n = IDENTIFIER_POINTER (DECL_NAME (udr));
5363 if (strncmp (IDENTIFIER_POINTER (DECL_NAME (udr)),
5364 prefix_minmax, prefix_size) == 0
5365 && ((n[prefix_size] == 'i' && n[prefix_size + 1] == 'n')
5366 || (n[prefix_size] == 'a' && n[prefix_size + 1] == 'x'))
5367 && (n[prefix_size + 2] == '~' || n[prefix_size + 2] == '\0'))
5368 i = 0;
5370 if (i < 8)
5372 error_at (loc, "predeclared arithmetic type %qT in "
5373 "%<#pragma omp declare reduction%>", type);
5374 return;
5377 else if (TREE_CODE (type) == FUNCTION_TYPE
5378 || TREE_CODE (type) == METHOD_TYPE
5379 || TREE_CODE (type) == ARRAY_TYPE)
5381 error_at (loc, "function or array type %qT in "
5382 "%<#pragma omp declare reduction%>", type);
5383 return;
5385 else if (TREE_CODE (type) == REFERENCE_TYPE)
5387 error_at (loc, "reference type %qT in %<#pragma omp declare reduction%>",
5388 type);
5389 return;
5391 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
5393 error_at (loc, "const, volatile or __restrict qualified type %qT in "
5394 "%<#pragma omp declare reduction%>", type);
5395 return;
5398 tree body = DECL_SAVED_TREE (udr);
5399 if (body == NULL_TREE || TREE_CODE (body) != STATEMENT_LIST)
5400 return;
5402 tree_stmt_iterator tsi;
5403 struct cp_check_omp_declare_reduction_data data;
5404 memset (data.stmts, 0, sizeof data.stmts);
5405 for (i = 0, tsi = tsi_start (body);
5406 i < 7 && !tsi_end_p (tsi);
5407 i++, tsi_next (&tsi))
5408 data.stmts[i] = tsi_stmt (tsi);
5409 data.loc = loc;
5410 gcc_assert (tsi_end_p (tsi));
5411 if (i >= 3)
5413 gcc_assert (TREE_CODE (data.stmts[0]) == DECL_EXPR
5414 && TREE_CODE (data.stmts[1]) == DECL_EXPR);
5415 if (TREE_NO_WARNING (DECL_EXPR_DECL (data.stmts[0])))
5416 return;
5417 data.combiner_p = true;
5418 if (cp_walk_tree (&data.stmts[2], cp_check_omp_declare_reduction_r,
5419 &data, NULL))
5420 TREE_NO_WARNING (DECL_EXPR_DECL (data.stmts[0])) = 1;
5422 if (i >= 6)
5424 gcc_assert (TREE_CODE (data.stmts[3]) == DECL_EXPR
5425 && TREE_CODE (data.stmts[4]) == DECL_EXPR);
5426 data.combiner_p = false;
5427 if (cp_walk_tree (&data.stmts[5], cp_check_omp_declare_reduction_r,
5428 &data, NULL)
5429 || cp_walk_tree (&DECL_INITIAL (DECL_EXPR_DECL (data.stmts[3])),
5430 cp_check_omp_declare_reduction_r, &data, NULL))
5431 TREE_NO_WARNING (DECL_EXPR_DECL (data.stmts[0])) = 1;
5432 if (i == 7)
5433 gcc_assert (TREE_CODE (data.stmts[6]) == DECL_EXPR);
5437 /* Helper function of finish_omp_clauses. Clone STMT as if we were making
5438 an inline call. But, remap
5439 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
5440 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
5442 static tree
5443 clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
5444 tree decl, tree placeholder)
5446 copy_body_data id;
5447 hash_map<tree, tree> decl_map;
5449 decl_map.put (omp_decl1, placeholder);
5450 decl_map.put (omp_decl2, decl);
5451 memset (&id, 0, sizeof (id));
5452 id.src_fn = DECL_CONTEXT (omp_decl1);
5453 id.dst_fn = current_function_decl;
5454 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
5455 id.decl_map = &decl_map;
5457 id.copy_decl = copy_decl_no_change;
5458 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
5459 id.transform_new_cfg = true;
5460 id.transform_return_to_modify = false;
5461 id.transform_lang_insert_block = NULL;
5462 id.eh_lp_nr = 0;
5463 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
5464 return stmt;
5467 /* Helper function of finish_omp_clauses, called via cp_walk_tree.
5468 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
5470 static tree
5471 find_omp_placeholder_r (tree *tp, int *, void *data)
5473 if (*tp == (tree) data)
5474 return *tp;
5475 return NULL_TREE;
5478 /* Helper function of finish_omp_clauses. Handle OMP_CLAUSE_REDUCTION C.
5479 Return true if there is some error and the clause should be removed. */
5481 static bool
5482 finish_omp_reduction_clause (tree c, bool *need_default_ctor, bool *need_dtor)
5484 tree t = OMP_CLAUSE_DECL (c);
5485 bool predefined = false;
5486 if (TREE_CODE (t) == TREE_LIST)
5488 gcc_assert (processing_template_decl);
5489 return false;
5491 tree type = TREE_TYPE (t);
5492 if (TREE_CODE (t) == MEM_REF)
5493 type = TREE_TYPE (type);
5494 if (TREE_CODE (type) == REFERENCE_TYPE)
5495 type = TREE_TYPE (type);
5496 if (TREE_CODE (type) == ARRAY_TYPE)
5498 tree oatype = type;
5499 gcc_assert (TREE_CODE (t) != MEM_REF);
5500 while (TREE_CODE (type) == ARRAY_TYPE)
5501 type = TREE_TYPE (type);
5502 if (!processing_template_decl)
5504 t = require_complete_type (t);
5505 if (t == error_mark_node)
5506 return true;
5507 tree size = size_binop (EXACT_DIV_EXPR, TYPE_SIZE_UNIT (oatype),
5508 TYPE_SIZE_UNIT (type));
5509 if (integer_zerop (size))
5511 error ("%qE in %<reduction%> clause is a zero size array",
5512 omp_clause_printable_decl (t));
5513 return true;
5515 size = size_binop (MINUS_EXPR, size, size_one_node);
5516 tree index_type = build_index_type (size);
5517 tree atype = build_array_type (type, index_type);
5518 tree ptype = build_pointer_type (type);
5519 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
5520 t = build_fold_addr_expr (t);
5521 t = build2 (MEM_REF, atype, t, build_int_cst (ptype, 0));
5522 OMP_CLAUSE_DECL (c) = t;
5525 if (type == error_mark_node)
5526 return true;
5527 else if (ARITHMETIC_TYPE_P (type))
5528 switch (OMP_CLAUSE_REDUCTION_CODE (c))
5530 case PLUS_EXPR:
5531 case MULT_EXPR:
5532 case MINUS_EXPR:
5533 predefined = true;
5534 break;
5535 case MIN_EXPR:
5536 case MAX_EXPR:
5537 if (TREE_CODE (type) == COMPLEX_TYPE)
5538 break;
5539 predefined = true;
5540 break;
5541 case BIT_AND_EXPR:
5542 case BIT_IOR_EXPR:
5543 case BIT_XOR_EXPR:
5544 if (FLOAT_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
5545 break;
5546 predefined = true;
5547 break;
5548 case TRUTH_ANDIF_EXPR:
5549 case TRUTH_ORIF_EXPR:
5550 if (FLOAT_TYPE_P (type))
5551 break;
5552 predefined = true;
5553 break;
5554 default:
5555 break;
5557 else if (TYPE_READONLY (type))
5559 error ("%qE has const type for %<reduction%>",
5560 omp_clause_printable_decl (t));
5561 return true;
5563 else if (!processing_template_decl)
5565 t = require_complete_type (t);
5566 if (t == error_mark_node)
5567 return true;
5568 OMP_CLAUSE_DECL (c) = t;
5571 if (predefined)
5573 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = NULL_TREE;
5574 return false;
5576 else if (processing_template_decl)
5577 return false;
5579 tree id = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
5581 type = TYPE_MAIN_VARIANT (type);
5582 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = NULL_TREE;
5583 if (id == NULL_TREE)
5584 id = omp_reduction_id (OMP_CLAUSE_REDUCTION_CODE (c),
5585 NULL_TREE, NULL_TREE);
5586 id = omp_reduction_lookup (OMP_CLAUSE_LOCATION (c), id, type, NULL, NULL);
5587 if (id)
5589 if (id == error_mark_node)
5590 return true;
5591 mark_used (id);
5592 tree body = DECL_SAVED_TREE (id);
5593 if (!body)
5594 return true;
5595 if (TREE_CODE (body) == STATEMENT_LIST)
5597 tree_stmt_iterator tsi;
5598 tree placeholder = NULL_TREE, decl_placeholder = NULL_TREE;
5599 int i;
5600 tree stmts[7];
5601 tree atype = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (id)));
5602 atype = TREE_TYPE (atype);
5603 bool need_static_cast = !same_type_p (type, atype);
5604 memset (stmts, 0, sizeof stmts);
5605 for (i = 0, tsi = tsi_start (body);
5606 i < 7 && !tsi_end_p (tsi);
5607 i++, tsi_next (&tsi))
5608 stmts[i] = tsi_stmt (tsi);
5609 gcc_assert (tsi_end_p (tsi));
5611 if (i >= 3)
5613 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
5614 && TREE_CODE (stmts[1]) == DECL_EXPR);
5615 placeholder = build_lang_decl (VAR_DECL, NULL_TREE, type);
5616 DECL_ARTIFICIAL (placeholder) = 1;
5617 DECL_IGNORED_P (placeholder) = 1;
5618 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
5619 if (TREE_CODE (t) == MEM_REF)
5621 decl_placeholder = build_lang_decl (VAR_DECL, NULL_TREE,
5622 type);
5623 DECL_ARTIFICIAL (decl_placeholder) = 1;
5624 DECL_IGNORED_P (decl_placeholder) = 1;
5625 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c) = decl_placeholder;
5627 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[0])))
5628 cxx_mark_addressable (placeholder);
5629 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[1]))
5630 && TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c)))
5631 != REFERENCE_TYPE)
5632 cxx_mark_addressable (decl_placeholder ? decl_placeholder
5633 : OMP_CLAUSE_DECL (c));
5634 tree omp_out = placeholder;
5635 tree omp_in = decl_placeholder ? decl_placeholder
5636 : convert_from_reference (OMP_CLAUSE_DECL (c));
5637 if (need_static_cast)
5639 tree rtype = build_reference_type (atype);
5640 omp_out = build_static_cast (rtype, omp_out,
5641 tf_warning_or_error);
5642 omp_in = build_static_cast (rtype, omp_in,
5643 tf_warning_or_error);
5644 if (omp_out == error_mark_node || omp_in == error_mark_node)
5645 return true;
5646 omp_out = convert_from_reference (omp_out);
5647 omp_in = convert_from_reference (omp_in);
5649 OMP_CLAUSE_REDUCTION_MERGE (c)
5650 = clone_omp_udr (stmts[2], DECL_EXPR_DECL (stmts[0]),
5651 DECL_EXPR_DECL (stmts[1]), omp_in, omp_out);
5653 if (i >= 6)
5655 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
5656 && TREE_CODE (stmts[4]) == DECL_EXPR);
5657 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[3])))
5658 cxx_mark_addressable (decl_placeholder ? decl_placeholder
5659 : OMP_CLAUSE_DECL (c));
5660 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[4])))
5661 cxx_mark_addressable (placeholder);
5662 tree omp_priv = decl_placeholder ? decl_placeholder
5663 : convert_from_reference (OMP_CLAUSE_DECL (c));
5664 tree omp_orig = placeholder;
5665 if (need_static_cast)
5667 if (i == 7)
5669 error_at (OMP_CLAUSE_LOCATION (c),
5670 "user defined reduction with constructor "
5671 "initializer for base class %qT", atype);
5672 return true;
5674 tree rtype = build_reference_type (atype);
5675 omp_priv = build_static_cast (rtype, omp_priv,
5676 tf_warning_or_error);
5677 omp_orig = build_static_cast (rtype, omp_orig,
5678 tf_warning_or_error);
5679 if (omp_priv == error_mark_node
5680 || omp_orig == error_mark_node)
5681 return true;
5682 omp_priv = convert_from_reference (omp_priv);
5683 omp_orig = convert_from_reference (omp_orig);
5685 if (i == 6)
5686 *need_default_ctor = true;
5687 OMP_CLAUSE_REDUCTION_INIT (c)
5688 = clone_omp_udr (stmts[5], DECL_EXPR_DECL (stmts[4]),
5689 DECL_EXPR_DECL (stmts[3]),
5690 omp_priv, omp_orig);
5691 if (cp_walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
5692 find_omp_placeholder_r, placeholder, NULL))
5693 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
5695 else if (i >= 3)
5697 if (CLASS_TYPE_P (type) && !pod_type_p (type))
5698 *need_default_ctor = true;
5699 else
5701 tree init;
5702 tree v = decl_placeholder ? decl_placeholder
5703 : convert_from_reference (t);
5704 if (AGGREGATE_TYPE_P (TREE_TYPE (v)))
5705 init = build_constructor (TREE_TYPE (v), NULL);
5706 else
5707 init = fold_convert (TREE_TYPE (v), integer_zero_node);
5708 OMP_CLAUSE_REDUCTION_INIT (c)
5709 = build2 (INIT_EXPR, TREE_TYPE (v), v, init);
5714 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
5715 *need_dtor = true;
5716 else
5718 error ("user defined reduction not found for %qE",
5719 omp_clause_printable_decl (t));
5720 return true;
5722 if (TREE_CODE (OMP_CLAUSE_DECL (c)) == MEM_REF)
5723 gcc_assert (TYPE_SIZE_UNIT (type)
5724 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST);
5725 return false;
5728 /* Called from finish_struct_1. linear(this) or linear(this:step)
5729 clauses might not be finalized yet because the class has been incomplete
5730 when parsing #pragma omp declare simd methods. Fix those up now. */
5732 void
5733 finish_omp_declare_simd_methods (tree t)
5735 if (processing_template_decl)
5736 return;
5738 for (tree x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
5740 if (TREE_CODE (TREE_TYPE (x)) != METHOD_TYPE)
5741 continue;
5742 tree ods = lookup_attribute ("omp declare simd", DECL_ATTRIBUTES (x));
5743 if (!ods || !TREE_VALUE (ods))
5744 continue;
5745 for (tree c = TREE_VALUE (TREE_VALUE (ods)); c; c = OMP_CLAUSE_CHAIN (c))
5746 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
5747 && integer_zerop (OMP_CLAUSE_DECL (c))
5748 && OMP_CLAUSE_LINEAR_STEP (c)
5749 && TREE_CODE (TREE_TYPE (OMP_CLAUSE_LINEAR_STEP (c)))
5750 == POINTER_TYPE)
5752 tree s = OMP_CLAUSE_LINEAR_STEP (c);
5753 s = fold_convert_loc (OMP_CLAUSE_LOCATION (c), sizetype, s);
5754 s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MULT_EXPR,
5755 sizetype, s, TYPE_SIZE_UNIT (t));
5756 OMP_CLAUSE_LINEAR_STEP (c) = s;
5761 /* Adjust sink depend clause to take into account pointer offsets.
5763 Return TRUE if there was a problem processing the offset, and the
5764 whole clause should be removed. */
5766 static bool
5767 cp_finish_omp_clause_depend_sink (tree sink_clause)
5769 tree t = OMP_CLAUSE_DECL (sink_clause);
5770 gcc_assert (TREE_CODE (t) == TREE_LIST);
5772 /* Make sure we don't adjust things twice for templates. */
5773 if (processing_template_decl)
5774 return false;
5776 for (; t; t = TREE_CHAIN (t))
5778 tree decl = TREE_VALUE (t);
5779 if (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
5781 tree offset = TREE_PURPOSE (t);
5782 bool neg = wi::neg_p (wi::to_wide (offset));
5783 offset = fold_unary (ABS_EXPR, TREE_TYPE (offset), offset);
5784 decl = mark_rvalue_use (decl);
5785 decl = convert_from_reference (decl);
5786 tree t2 = pointer_int_sum (OMP_CLAUSE_LOCATION (sink_clause),
5787 neg ? MINUS_EXPR : PLUS_EXPR,
5788 decl, offset);
5789 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (sink_clause),
5790 MINUS_EXPR, sizetype,
5791 fold_convert (sizetype, t2),
5792 fold_convert (sizetype, decl));
5793 if (t2 == error_mark_node)
5794 return true;
5795 TREE_PURPOSE (t) = t2;
5798 return false;
5801 /* For all elements of CLAUSES, validate them vs OpenMP constraints.
5802 Remove any elements from the list that are invalid. */
5804 tree
5805 finish_omp_clauses (tree clauses, enum c_omp_region_type ort)
5807 bitmap_head generic_head, firstprivate_head, lastprivate_head;
5808 bitmap_head aligned_head, map_head, map_field_head, oacc_reduction_head;
5809 tree c, t, *pc;
5810 tree safelen = NULL_TREE;
5811 bool branch_seen = false;
5812 bool copyprivate_seen = false;
5813 bool ordered_seen = false;
5814 bool oacc_async = false;
5816 bitmap_obstack_initialize (NULL);
5817 bitmap_initialize (&generic_head, &bitmap_default_obstack);
5818 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
5819 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
5820 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
5821 bitmap_initialize (&map_head, &bitmap_default_obstack);
5822 bitmap_initialize (&map_field_head, &bitmap_default_obstack);
5823 bitmap_initialize (&oacc_reduction_head, &bitmap_default_obstack);
5825 if (ort & C_ORT_ACC)
5826 for (c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
5827 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ASYNC)
5829 oacc_async = true;
5830 break;
5833 for (pc = &clauses, c = clauses; c ; c = *pc)
5835 bool remove = false;
5836 bool field_ok = false;
5838 switch (OMP_CLAUSE_CODE (c))
5840 case OMP_CLAUSE_SHARED:
5841 field_ok = ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP);
5842 goto check_dup_generic;
5843 case OMP_CLAUSE_PRIVATE:
5844 field_ok = ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP);
5845 goto check_dup_generic;
5846 case OMP_CLAUSE_REDUCTION:
5847 field_ok = ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP);
5848 t = OMP_CLAUSE_DECL (c);
5849 if (TREE_CODE (t) == TREE_LIST)
5851 if (handle_omp_array_sections (c, ort))
5853 remove = true;
5854 break;
5856 if (TREE_CODE (t) == TREE_LIST)
5858 while (TREE_CODE (t) == TREE_LIST)
5859 t = TREE_CHAIN (t);
5861 else
5863 gcc_assert (TREE_CODE (t) == MEM_REF);
5864 t = TREE_OPERAND (t, 0);
5865 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
5866 t = TREE_OPERAND (t, 0);
5867 if (TREE_CODE (t) == ADDR_EXPR
5868 || INDIRECT_REF_P (t))
5869 t = TREE_OPERAND (t, 0);
5871 tree n = omp_clause_decl_field (t);
5872 if (n)
5873 t = n;
5874 goto check_dup_generic_t;
5876 if (oacc_async)
5877 cxx_mark_addressable (t);
5878 goto check_dup_generic;
5879 case OMP_CLAUSE_COPYPRIVATE:
5880 copyprivate_seen = true;
5881 field_ok = ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP);
5882 goto check_dup_generic;
5883 case OMP_CLAUSE_COPYIN:
5884 goto check_dup_generic;
5885 case OMP_CLAUSE_LINEAR:
5886 field_ok = ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP);
5887 t = OMP_CLAUSE_DECL (c);
5888 if (ort != C_ORT_OMP_DECLARE_SIMD
5889 && OMP_CLAUSE_LINEAR_KIND (c) != OMP_CLAUSE_LINEAR_DEFAULT)
5891 error_at (OMP_CLAUSE_LOCATION (c),
5892 "modifier should not be specified in %<linear%> "
5893 "clause on %<simd%> or %<for%> constructs");
5894 OMP_CLAUSE_LINEAR_KIND (c) = OMP_CLAUSE_LINEAR_DEFAULT;
5896 if ((VAR_P (t) || TREE_CODE (t) == PARM_DECL)
5897 && !type_dependent_expression_p (t))
5899 tree type = TREE_TYPE (t);
5900 if ((OMP_CLAUSE_LINEAR_KIND (c) == OMP_CLAUSE_LINEAR_REF
5901 || OMP_CLAUSE_LINEAR_KIND (c) == OMP_CLAUSE_LINEAR_UVAL)
5902 && TREE_CODE (type) != REFERENCE_TYPE)
5904 error ("linear clause with %qs modifier applied to "
5905 "non-reference variable with %qT type",
5906 OMP_CLAUSE_LINEAR_KIND (c) == OMP_CLAUSE_LINEAR_REF
5907 ? "ref" : "uval", TREE_TYPE (t));
5908 remove = true;
5909 break;
5911 if (TREE_CODE (type) == REFERENCE_TYPE)
5912 type = TREE_TYPE (type);
5913 if (OMP_CLAUSE_LINEAR_KIND (c) != OMP_CLAUSE_LINEAR_REF)
5915 if (!INTEGRAL_TYPE_P (type)
5916 && TREE_CODE (type) != POINTER_TYPE)
5918 error ("linear clause applied to non-integral non-pointer"
5919 " variable with %qT type", TREE_TYPE (t));
5920 remove = true;
5921 break;
5925 t = OMP_CLAUSE_LINEAR_STEP (c);
5926 if (t == NULL_TREE)
5927 t = integer_one_node;
5928 if (t == error_mark_node)
5930 remove = true;
5931 break;
5933 else if (!type_dependent_expression_p (t)
5934 && !INTEGRAL_TYPE_P (TREE_TYPE (t))
5935 && (ort != C_ORT_OMP_DECLARE_SIMD
5936 || TREE_CODE (t) != PARM_DECL
5937 || TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE
5938 || !INTEGRAL_TYPE_P (TREE_TYPE (TREE_TYPE (t)))))
5940 error ("linear step expression must be integral");
5941 remove = true;
5942 break;
5944 else
5946 t = mark_rvalue_use (t);
5947 if (ort == C_ORT_OMP_DECLARE_SIMD && TREE_CODE (t) == PARM_DECL)
5949 OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c) = 1;
5950 goto check_dup_generic;
5952 if (!processing_template_decl
5953 && (VAR_P (OMP_CLAUSE_DECL (c))
5954 || TREE_CODE (OMP_CLAUSE_DECL (c)) == PARM_DECL))
5956 if (ort == C_ORT_OMP_DECLARE_SIMD)
5958 t = maybe_constant_value (t);
5959 if (TREE_CODE (t) != INTEGER_CST)
5961 error_at (OMP_CLAUSE_LOCATION (c),
5962 "%<linear%> clause step %qE is neither "
5963 "constant nor a parameter", t);
5964 remove = true;
5965 break;
5968 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5969 tree type = TREE_TYPE (OMP_CLAUSE_DECL (c));
5970 if (TREE_CODE (type) == REFERENCE_TYPE)
5971 type = TREE_TYPE (type);
5972 if (OMP_CLAUSE_LINEAR_KIND (c) == OMP_CLAUSE_LINEAR_REF)
5974 type = build_pointer_type (type);
5975 tree d = fold_convert (type, OMP_CLAUSE_DECL (c));
5976 t = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
5977 d, t);
5978 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c),
5979 MINUS_EXPR, sizetype,
5980 fold_convert (sizetype, t),
5981 fold_convert (sizetype, d));
5982 if (t == error_mark_node)
5984 remove = true;
5985 break;
5988 else if (TREE_CODE (type) == POINTER_TYPE
5989 /* Can't multiply the step yet if *this
5990 is still incomplete type. */
5991 && (ort != C_ORT_OMP_DECLARE_SIMD
5992 || TREE_CODE (OMP_CLAUSE_DECL (c)) != PARM_DECL
5993 || !DECL_ARTIFICIAL (OMP_CLAUSE_DECL (c))
5994 || DECL_NAME (OMP_CLAUSE_DECL (c))
5995 != this_identifier
5996 || !TYPE_BEING_DEFINED (TREE_TYPE (type))))
5998 tree d = convert_from_reference (OMP_CLAUSE_DECL (c));
5999 t = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
6000 d, t);
6001 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c),
6002 MINUS_EXPR, sizetype,
6003 fold_convert (sizetype, t),
6004 fold_convert (sizetype, d));
6005 if (t == error_mark_node)
6007 remove = true;
6008 break;
6011 else
6012 t = fold_convert (type, t);
6014 OMP_CLAUSE_LINEAR_STEP (c) = t;
6016 goto check_dup_generic;
6017 check_dup_generic:
6018 t = omp_clause_decl_field (OMP_CLAUSE_DECL (c));
6019 if (t)
6021 if (!remove && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_SHARED)
6022 omp_note_field_privatization (t, OMP_CLAUSE_DECL (c));
6024 else
6025 t = OMP_CLAUSE_DECL (c);
6026 check_dup_generic_t:
6027 if (t == current_class_ptr
6028 && (ort != C_ORT_OMP_DECLARE_SIMD
6029 || (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_LINEAR
6030 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_UNIFORM)))
6032 error ("%<this%> allowed in OpenMP only in %<declare simd%>"
6033 " clauses");
6034 remove = true;
6035 break;
6037 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL
6038 && (!field_ok || TREE_CODE (t) != FIELD_DECL))
6040 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
6041 break;
6042 if (DECL_P (t))
6043 error ("%qD is not a variable in clause %qs", t,
6044 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6045 else
6046 error ("%qE is not a variable in clause %qs", t,
6047 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6048 remove = true;
6050 else if (ort == C_ORT_ACC
6051 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
6053 if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
6055 error ("%qD appears more than once in reduction clauses", t);
6056 remove = true;
6058 else
6059 bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
6061 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
6062 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
6063 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
6065 error ("%qD appears more than once in data clauses", t);
6066 remove = true;
6068 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
6069 && bitmap_bit_p (&map_head, DECL_UID (t)))
6071 if (ort == C_ORT_ACC)
6072 error ("%qD appears more than once in data clauses", t);
6073 else
6074 error ("%qD appears both in data and map clauses", t);
6075 remove = true;
6077 else
6078 bitmap_set_bit (&generic_head, DECL_UID (t));
6079 if (!field_ok)
6080 break;
6081 handle_field_decl:
6082 if (!remove
6083 && TREE_CODE (t) == FIELD_DECL
6084 && t == OMP_CLAUSE_DECL (c)
6085 && ort != C_ORT_ACC)
6087 OMP_CLAUSE_DECL (c)
6088 = omp_privatize_field (t, (OMP_CLAUSE_CODE (c)
6089 == OMP_CLAUSE_SHARED));
6090 if (OMP_CLAUSE_DECL (c) == error_mark_node)
6091 remove = true;
6093 break;
6095 case OMP_CLAUSE_FIRSTPRIVATE:
6096 t = omp_clause_decl_field (OMP_CLAUSE_DECL (c));
6097 if (t)
6098 omp_note_field_privatization (t, OMP_CLAUSE_DECL (c));
6099 else
6100 t = OMP_CLAUSE_DECL (c);
6101 if (ort != C_ORT_ACC && t == current_class_ptr)
6103 error ("%<this%> allowed in OpenMP only in %<declare simd%>"
6104 " clauses");
6105 remove = true;
6106 break;
6108 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL
6109 && ((ort & C_ORT_OMP_DECLARE_SIMD) != C_ORT_OMP
6110 || TREE_CODE (t) != FIELD_DECL))
6112 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
6113 break;
6114 if (DECL_P (t))
6115 error ("%qD is not a variable in clause %<firstprivate%>", t);
6116 else
6117 error ("%qE is not a variable in clause %<firstprivate%>", t);
6118 remove = true;
6120 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
6121 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
6123 error ("%qD appears more than once in data clauses", t);
6124 remove = true;
6126 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
6128 if (ort == C_ORT_ACC)
6129 error ("%qD appears more than once in data clauses", t);
6130 else
6131 error ("%qD appears both in data and map clauses", t);
6132 remove = true;
6134 else
6135 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
6136 goto handle_field_decl;
6138 case OMP_CLAUSE_LASTPRIVATE:
6139 t = omp_clause_decl_field (OMP_CLAUSE_DECL (c));
6140 if (t)
6141 omp_note_field_privatization (t, OMP_CLAUSE_DECL (c));
6142 else
6143 t = OMP_CLAUSE_DECL (c);
6144 if (t == current_class_ptr)
6146 error ("%<this%> allowed in OpenMP only in %<declare simd%>"
6147 " clauses");
6148 remove = true;
6149 break;
6151 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL
6152 && ((ort & C_ORT_OMP_DECLARE_SIMD) != C_ORT_OMP
6153 || TREE_CODE (t) != FIELD_DECL))
6155 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
6156 break;
6157 if (DECL_P (t))
6158 error ("%qD is not a variable in clause %<lastprivate%>", t);
6159 else
6160 error ("%qE is not a variable in clause %<lastprivate%>", t);
6161 remove = true;
6163 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
6164 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
6166 error ("%qD appears more than once in data clauses", t);
6167 remove = true;
6169 else
6170 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
6171 goto handle_field_decl;
6173 case OMP_CLAUSE_IF:
6174 t = OMP_CLAUSE_IF_EXPR (c);
6175 t = maybe_convert_cond (t);
6176 if (t == error_mark_node)
6177 remove = true;
6178 else if (!processing_template_decl)
6179 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6180 OMP_CLAUSE_IF_EXPR (c) = t;
6181 break;
6183 case OMP_CLAUSE_FINAL:
6184 t = OMP_CLAUSE_FINAL_EXPR (c);
6185 t = maybe_convert_cond (t);
6186 if (t == error_mark_node)
6187 remove = true;
6188 else if (!processing_template_decl)
6189 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6190 OMP_CLAUSE_FINAL_EXPR (c) = t;
6191 break;
6193 case OMP_CLAUSE_GANG:
6194 /* Operand 1 is the gang static: argument. */
6195 t = OMP_CLAUSE_OPERAND (c, 1);
6196 if (t != NULL_TREE)
6198 if (t == error_mark_node)
6199 remove = true;
6200 else if (!type_dependent_expression_p (t)
6201 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6203 error ("%<gang%> static expression must be integral");
6204 remove = true;
6206 else
6208 t = mark_rvalue_use (t);
6209 if (!processing_template_decl)
6211 t = maybe_constant_value (t);
6212 if (TREE_CODE (t) == INTEGER_CST
6213 && tree_int_cst_sgn (t) != 1
6214 && t != integer_minus_one_node)
6216 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6217 "%<gang%> static value must be "
6218 "positive");
6219 t = integer_one_node;
6221 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6224 OMP_CLAUSE_OPERAND (c, 1) = t;
6226 /* Check operand 0, the num argument. */
6227 /* FALLTHRU */
6229 case OMP_CLAUSE_WORKER:
6230 case OMP_CLAUSE_VECTOR:
6231 if (OMP_CLAUSE_OPERAND (c, 0) == NULL_TREE)
6232 break;
6233 /* FALLTHRU */
6235 case OMP_CLAUSE_NUM_TASKS:
6236 case OMP_CLAUSE_NUM_TEAMS:
6237 case OMP_CLAUSE_NUM_THREADS:
6238 case OMP_CLAUSE_NUM_GANGS:
6239 case OMP_CLAUSE_NUM_WORKERS:
6240 case OMP_CLAUSE_VECTOR_LENGTH:
6241 t = OMP_CLAUSE_OPERAND (c, 0);
6242 if (t == error_mark_node)
6243 remove = true;
6244 else if (!type_dependent_expression_p (t)
6245 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6247 switch (OMP_CLAUSE_CODE (c))
6249 case OMP_CLAUSE_GANG:
6250 error_at (OMP_CLAUSE_LOCATION (c),
6251 "%<gang%> num expression must be integral"); break;
6252 case OMP_CLAUSE_VECTOR:
6253 error_at (OMP_CLAUSE_LOCATION (c),
6254 "%<vector%> length expression must be integral");
6255 break;
6256 case OMP_CLAUSE_WORKER:
6257 error_at (OMP_CLAUSE_LOCATION (c),
6258 "%<worker%> num expression must be integral");
6259 break;
6260 default:
6261 error_at (OMP_CLAUSE_LOCATION (c),
6262 "%qs expression must be integral",
6263 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6265 remove = true;
6267 else
6269 t = mark_rvalue_use (t);
6270 if (!processing_template_decl)
6272 t = maybe_constant_value (t);
6273 if (TREE_CODE (t) == INTEGER_CST
6274 && tree_int_cst_sgn (t) != 1)
6276 switch (OMP_CLAUSE_CODE (c))
6278 case OMP_CLAUSE_GANG:
6279 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6280 "%<gang%> num value must be positive");
6281 break;
6282 case OMP_CLAUSE_VECTOR:
6283 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6284 "%<vector%> length value must be "
6285 "positive");
6286 break;
6287 case OMP_CLAUSE_WORKER:
6288 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6289 "%<worker%> num value must be "
6290 "positive");
6291 break;
6292 default:
6293 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6294 "%qs value must be positive",
6295 omp_clause_code_name
6296 [OMP_CLAUSE_CODE (c)]);
6298 t = integer_one_node;
6300 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6302 OMP_CLAUSE_OPERAND (c, 0) = t;
6304 break;
6306 case OMP_CLAUSE_SCHEDULE:
6307 if (OMP_CLAUSE_SCHEDULE_KIND (c) & OMP_CLAUSE_SCHEDULE_NONMONOTONIC)
6309 const char *p = NULL;
6310 switch (OMP_CLAUSE_SCHEDULE_KIND (c) & OMP_CLAUSE_SCHEDULE_MASK)
6312 case OMP_CLAUSE_SCHEDULE_STATIC: p = "static"; break;
6313 case OMP_CLAUSE_SCHEDULE_DYNAMIC: break;
6314 case OMP_CLAUSE_SCHEDULE_GUIDED: break;
6315 case OMP_CLAUSE_SCHEDULE_AUTO: p = "auto"; break;
6316 case OMP_CLAUSE_SCHEDULE_RUNTIME: p = "runtime"; break;
6317 default: gcc_unreachable ();
6319 if (p)
6321 error_at (OMP_CLAUSE_LOCATION (c),
6322 "%<nonmonotonic%> modifier specified for %qs "
6323 "schedule kind", p);
6324 OMP_CLAUSE_SCHEDULE_KIND (c)
6325 = (enum omp_clause_schedule_kind)
6326 (OMP_CLAUSE_SCHEDULE_KIND (c)
6327 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
6331 t = OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c);
6332 if (t == NULL)
6334 else if (t == error_mark_node)
6335 remove = true;
6336 else if (!type_dependent_expression_p (t)
6337 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6339 error ("schedule chunk size expression must be integral");
6340 remove = true;
6342 else
6344 t = mark_rvalue_use (t);
6345 if (!processing_template_decl)
6347 t = maybe_constant_value (t);
6348 if (TREE_CODE (t) == INTEGER_CST
6349 && tree_int_cst_sgn (t) != 1)
6351 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6352 "chunk size value must be positive");
6353 t = integer_one_node;
6355 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6357 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
6359 break;
6361 case OMP_CLAUSE_SIMDLEN:
6362 case OMP_CLAUSE_SAFELEN:
6363 t = OMP_CLAUSE_OPERAND (c, 0);
6364 if (t == error_mark_node)
6365 remove = true;
6366 else if (!type_dependent_expression_p (t)
6367 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6369 error ("%qs length expression must be integral",
6370 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6371 remove = true;
6373 else
6375 t = mark_rvalue_use (t);
6376 if (!processing_template_decl)
6378 t = maybe_constant_value (t);
6379 if (TREE_CODE (t) != INTEGER_CST
6380 || tree_int_cst_sgn (t) != 1)
6382 error ("%qs length expression must be positive constant"
6383 " integer expression",
6384 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6385 remove = true;
6388 OMP_CLAUSE_OPERAND (c, 0) = t;
6389 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SAFELEN)
6390 safelen = c;
6392 break;
6394 case OMP_CLAUSE_ASYNC:
6395 t = OMP_CLAUSE_ASYNC_EXPR (c);
6396 if (t == error_mark_node)
6397 remove = true;
6398 else if (!type_dependent_expression_p (t)
6399 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6401 error ("%<async%> expression must be integral");
6402 remove = true;
6404 else
6406 t = mark_rvalue_use (t);
6407 if (!processing_template_decl)
6408 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6409 OMP_CLAUSE_ASYNC_EXPR (c) = t;
6411 break;
6413 case OMP_CLAUSE_WAIT:
6414 t = OMP_CLAUSE_WAIT_EXPR (c);
6415 if (t == error_mark_node)
6416 remove = true;
6417 else if (!processing_template_decl)
6418 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6419 OMP_CLAUSE_WAIT_EXPR (c) = t;
6420 break;
6422 case OMP_CLAUSE_THREAD_LIMIT:
6423 t = OMP_CLAUSE_THREAD_LIMIT_EXPR (c);
6424 if (t == error_mark_node)
6425 remove = true;
6426 else if (!type_dependent_expression_p (t)
6427 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6429 error ("%<thread_limit%> expression must be integral");
6430 remove = true;
6432 else
6434 t = mark_rvalue_use (t);
6435 if (!processing_template_decl)
6437 t = maybe_constant_value (t);
6438 if (TREE_CODE (t) == INTEGER_CST
6439 && tree_int_cst_sgn (t) != 1)
6441 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6442 "%<thread_limit%> value must be positive");
6443 t = integer_one_node;
6445 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6447 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
6449 break;
6451 case OMP_CLAUSE_DEVICE:
6452 t = OMP_CLAUSE_DEVICE_ID (c);
6453 if (t == error_mark_node)
6454 remove = true;
6455 else if (!type_dependent_expression_p (t)
6456 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6458 error ("%<device%> id must be integral");
6459 remove = true;
6461 else
6463 t = mark_rvalue_use (t);
6464 if (!processing_template_decl)
6465 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6466 OMP_CLAUSE_DEVICE_ID (c) = t;
6468 break;
6470 case OMP_CLAUSE_DIST_SCHEDULE:
6471 t = OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c);
6472 if (t == NULL)
6474 else if (t == error_mark_node)
6475 remove = true;
6476 else if (!type_dependent_expression_p (t)
6477 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6479 error ("%<dist_schedule%> chunk size expression must be "
6480 "integral");
6481 remove = true;
6483 else
6485 t = mark_rvalue_use (t);
6486 if (!processing_template_decl)
6487 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6488 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
6490 break;
6492 case OMP_CLAUSE_ALIGNED:
6493 t = OMP_CLAUSE_DECL (c);
6494 if (t == current_class_ptr && ort != C_ORT_OMP_DECLARE_SIMD)
6496 error ("%<this%> allowed in OpenMP only in %<declare simd%>"
6497 " clauses");
6498 remove = true;
6499 break;
6501 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
6503 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
6504 break;
6505 if (DECL_P (t))
6506 error ("%qD is not a variable in %<aligned%> clause", t);
6507 else
6508 error ("%qE is not a variable in %<aligned%> clause", t);
6509 remove = true;
6511 else if (!type_dependent_expression_p (t)
6512 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE
6513 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE
6514 && (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE
6515 || (!POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (t)))
6516 && (TREE_CODE (TREE_TYPE (TREE_TYPE (t)))
6517 != ARRAY_TYPE))))
6519 error_at (OMP_CLAUSE_LOCATION (c),
6520 "%qE in %<aligned%> clause is neither a pointer nor "
6521 "an array nor a reference to pointer or array", t);
6522 remove = true;
6524 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
6526 error ("%qD appears more than once in %<aligned%> clauses", t);
6527 remove = true;
6529 else
6530 bitmap_set_bit (&aligned_head, DECL_UID (t));
6531 t = OMP_CLAUSE_ALIGNED_ALIGNMENT (c);
6532 if (t == error_mark_node)
6533 remove = true;
6534 else if (t == NULL_TREE)
6535 break;
6536 else if (!type_dependent_expression_p (t)
6537 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6539 error ("%<aligned%> clause alignment expression must "
6540 "be integral");
6541 remove = true;
6543 else
6545 t = mark_rvalue_use (t);
6546 if (!processing_template_decl)
6548 t = maybe_constant_value (t);
6549 if (TREE_CODE (t) != INTEGER_CST
6550 || tree_int_cst_sgn (t) != 1)
6552 error ("%<aligned%> clause alignment expression must be "
6553 "positive constant integer expression");
6554 remove = true;
6557 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = t;
6559 break;
6561 case OMP_CLAUSE_DEPEND:
6562 t = OMP_CLAUSE_DECL (c);
6563 if (t == NULL_TREE)
6565 gcc_assert (OMP_CLAUSE_DEPEND_KIND (c)
6566 == OMP_CLAUSE_DEPEND_SOURCE);
6567 break;
6569 if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SINK)
6571 if (cp_finish_omp_clause_depend_sink (c))
6572 remove = true;
6573 break;
6575 if (TREE_CODE (t) == TREE_LIST)
6577 if (handle_omp_array_sections (c, ort))
6578 remove = true;
6579 break;
6581 if (t == error_mark_node)
6582 remove = true;
6583 else if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
6585 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
6586 break;
6587 if (DECL_P (t))
6588 error ("%qD is not a variable in %<depend%> clause", t);
6589 else
6590 error ("%qE is not a variable in %<depend%> clause", t);
6591 remove = true;
6593 else if (t == current_class_ptr)
6595 error ("%<this%> allowed in OpenMP only in %<declare simd%>"
6596 " clauses");
6597 remove = true;
6599 else if (!processing_template_decl
6600 && !cxx_mark_addressable (t))
6601 remove = true;
6602 break;
6604 case OMP_CLAUSE_MAP:
6605 case OMP_CLAUSE_TO:
6606 case OMP_CLAUSE_FROM:
6607 case OMP_CLAUSE__CACHE_:
6608 t = OMP_CLAUSE_DECL (c);
6609 if (TREE_CODE (t) == TREE_LIST)
6611 if (handle_omp_array_sections (c, ort))
6612 remove = true;
6613 else
6615 t = OMP_CLAUSE_DECL (c);
6616 if (TREE_CODE (t) != TREE_LIST
6617 && !type_dependent_expression_p (t)
6618 && !cp_omp_mappable_type (TREE_TYPE (t)))
6620 error_at (OMP_CLAUSE_LOCATION (c),
6621 "array section does not have mappable type "
6622 "in %qs clause",
6623 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6624 remove = true;
6626 while (TREE_CODE (t) == ARRAY_REF)
6627 t = TREE_OPERAND (t, 0);
6628 if (TREE_CODE (t) == COMPONENT_REF
6629 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
6631 while (TREE_CODE (t) == COMPONENT_REF)
6632 t = TREE_OPERAND (t, 0);
6633 if (REFERENCE_REF_P (t))
6634 t = TREE_OPERAND (t, 0);
6635 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
6636 break;
6637 if (bitmap_bit_p (&map_head, DECL_UID (t)))
6639 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
6640 error ("%qD appears more than once in motion"
6641 " clauses", t);
6642 else if (ort == C_ORT_ACC)
6643 error ("%qD appears more than once in data"
6644 " clauses", t);
6645 else
6646 error ("%qD appears more than once in map"
6647 " clauses", t);
6648 remove = true;
6650 else
6652 bitmap_set_bit (&map_head, DECL_UID (t));
6653 bitmap_set_bit (&map_field_head, DECL_UID (t));
6657 break;
6659 if (t == error_mark_node)
6661 remove = true;
6662 break;
6664 if (REFERENCE_REF_P (t)
6665 && TREE_CODE (TREE_OPERAND (t, 0)) == COMPONENT_REF)
6667 t = TREE_OPERAND (t, 0);
6668 OMP_CLAUSE_DECL (c) = t;
6670 if (TREE_CODE (t) == COMPONENT_REF
6671 && (ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP
6672 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE__CACHE_)
6674 if (type_dependent_expression_p (t))
6675 break;
6676 if (TREE_CODE (TREE_OPERAND (t, 1)) == FIELD_DECL
6677 && DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
6679 error_at (OMP_CLAUSE_LOCATION (c),
6680 "bit-field %qE in %qs clause",
6681 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6682 remove = true;
6684 else if (!cp_omp_mappable_type (TREE_TYPE (t)))
6686 error_at (OMP_CLAUSE_LOCATION (c),
6687 "%qE does not have a mappable type in %qs clause",
6688 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6689 remove = true;
6691 while (TREE_CODE (t) == COMPONENT_REF)
6693 if (TREE_TYPE (TREE_OPERAND (t, 0))
6694 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0)))
6695 == UNION_TYPE))
6697 error_at (OMP_CLAUSE_LOCATION (c),
6698 "%qE is a member of a union", t);
6699 remove = true;
6700 break;
6702 t = TREE_OPERAND (t, 0);
6704 if (remove)
6705 break;
6706 if (REFERENCE_REF_P (t))
6707 t = TREE_OPERAND (t, 0);
6708 if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
6710 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
6711 goto handle_map_references;
6714 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
6716 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
6717 break;
6718 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
6719 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
6720 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ALWAYS_POINTER))
6721 break;
6722 if (DECL_P (t))
6723 error ("%qD is not a variable in %qs clause", t,
6724 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6725 else
6726 error ("%qE is not a variable in %qs clause", t,
6727 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6728 remove = true;
6730 else if (VAR_P (t) && CP_DECL_THREAD_LOCAL_P (t))
6732 error ("%qD is threadprivate variable in %qs clause", t,
6733 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6734 remove = true;
6736 else if (ort != C_ORT_ACC && t == current_class_ptr)
6738 error ("%<this%> allowed in OpenMP only in %<declare simd%>"
6739 " clauses");
6740 remove = true;
6741 break;
6743 else if (!processing_template_decl
6744 && TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE
6745 && (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
6746 || (OMP_CLAUSE_MAP_KIND (c)
6747 != GOMP_MAP_FIRSTPRIVATE_POINTER))
6748 && !cxx_mark_addressable (t))
6749 remove = true;
6750 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
6751 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
6752 || (OMP_CLAUSE_MAP_KIND (c)
6753 == GOMP_MAP_FIRSTPRIVATE_POINTER)))
6754 && t == OMP_CLAUSE_DECL (c)
6755 && !type_dependent_expression_p (t)
6756 && !cp_omp_mappable_type ((TREE_CODE (TREE_TYPE (t))
6757 == REFERENCE_TYPE)
6758 ? TREE_TYPE (TREE_TYPE (t))
6759 : TREE_TYPE (t)))
6761 error_at (OMP_CLAUSE_LOCATION (c),
6762 "%qD does not have a mappable type in %qs clause", t,
6763 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6764 remove = true;
6766 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
6767 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FORCE_DEVICEPTR
6768 && !type_dependent_expression_p (t)
6769 && !POINTER_TYPE_P (TREE_TYPE (t)))
6771 error ("%qD is not a pointer variable", t);
6772 remove = true;
6774 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
6775 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
6777 if (bitmap_bit_p (&generic_head, DECL_UID (t))
6778 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
6780 error ("%qD appears more than once in data clauses", t);
6781 remove = true;
6783 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
6785 if (ort == C_ORT_ACC)
6786 error ("%qD appears more than once in data clauses", t);
6787 else
6788 error ("%qD appears both in data and map clauses", t);
6789 remove = true;
6791 else
6792 bitmap_set_bit (&generic_head, DECL_UID (t));
6794 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
6796 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
6797 error ("%qD appears more than once in motion clauses", t);
6798 if (ort == C_ORT_ACC)
6799 error ("%qD appears more than once in data clauses", t);
6800 else
6801 error ("%qD appears more than once in map clauses", t);
6802 remove = true;
6804 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
6805 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
6807 if (ort == C_ORT_ACC)
6808 error ("%qD appears more than once in data clauses", t);
6809 else
6810 error ("%qD appears both in data and map clauses", t);
6811 remove = true;
6813 else
6815 bitmap_set_bit (&map_head, DECL_UID (t));
6816 if (t != OMP_CLAUSE_DECL (c)
6817 && TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPONENT_REF)
6818 bitmap_set_bit (&map_field_head, DECL_UID (t));
6820 handle_map_references:
6821 if (!remove
6822 && !processing_template_decl
6823 && (ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP
6824 && TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c))) == REFERENCE_TYPE)
6826 t = OMP_CLAUSE_DECL (c);
6827 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
6829 OMP_CLAUSE_DECL (c) = build_simple_mem_ref (t);
6830 if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
6831 OMP_CLAUSE_SIZE (c)
6832 = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t)));
6834 else if (OMP_CLAUSE_MAP_KIND (c)
6835 != GOMP_MAP_FIRSTPRIVATE_POINTER
6836 && (OMP_CLAUSE_MAP_KIND (c)
6837 != GOMP_MAP_FIRSTPRIVATE_REFERENCE)
6838 && (OMP_CLAUSE_MAP_KIND (c)
6839 != GOMP_MAP_ALWAYS_POINTER))
6841 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
6842 OMP_CLAUSE_MAP);
6843 if (TREE_CODE (t) == COMPONENT_REF)
6844 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ALWAYS_POINTER);
6845 else
6846 OMP_CLAUSE_SET_MAP_KIND (c2,
6847 GOMP_MAP_FIRSTPRIVATE_REFERENCE);
6848 OMP_CLAUSE_DECL (c2) = t;
6849 OMP_CLAUSE_SIZE (c2) = size_zero_node;
6850 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
6851 OMP_CLAUSE_CHAIN (c) = c2;
6852 OMP_CLAUSE_DECL (c) = build_simple_mem_ref (t);
6853 if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
6854 OMP_CLAUSE_SIZE (c)
6855 = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t)));
6856 c = c2;
6859 break;
6861 case OMP_CLAUSE_TO_DECLARE:
6862 case OMP_CLAUSE_LINK:
6863 t = OMP_CLAUSE_DECL (c);
6864 if (TREE_CODE (t) == FUNCTION_DECL
6865 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
6867 else if (!VAR_P (t))
6869 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
6871 if (TREE_CODE (t) == TEMPLATE_ID_EXPR)
6872 error_at (OMP_CLAUSE_LOCATION (c),
6873 "template %qE in clause %qs", t,
6874 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6875 else if (really_overloaded_fn (t))
6876 error_at (OMP_CLAUSE_LOCATION (c),
6877 "overloaded function name %qE in clause %qs", t,
6878 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6879 else
6880 error_at (OMP_CLAUSE_LOCATION (c),
6881 "%qE is neither a variable nor a function name "
6882 "in clause %qs", t,
6883 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6885 else
6886 error_at (OMP_CLAUSE_LOCATION (c),
6887 "%qE is not a variable in clause %qs", t,
6888 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6889 remove = true;
6891 else if (DECL_THREAD_LOCAL_P (t))
6893 error_at (OMP_CLAUSE_LOCATION (c),
6894 "%qD is threadprivate variable in %qs clause", t,
6895 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6896 remove = true;
6898 else if (!cp_omp_mappable_type (TREE_TYPE (t)))
6900 error_at (OMP_CLAUSE_LOCATION (c),
6901 "%qD does not have a mappable type in %qs clause", t,
6902 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6903 remove = true;
6905 if (remove)
6906 break;
6907 if (bitmap_bit_p (&generic_head, DECL_UID (t)))
6909 error_at (OMP_CLAUSE_LOCATION (c),
6910 "%qE appears more than once on the same "
6911 "%<declare target%> directive", t);
6912 remove = true;
6914 else
6915 bitmap_set_bit (&generic_head, DECL_UID (t));
6916 break;
6918 case OMP_CLAUSE_UNIFORM:
6919 t = OMP_CLAUSE_DECL (c);
6920 if (TREE_CODE (t) != PARM_DECL)
6922 if (processing_template_decl)
6923 break;
6924 if (DECL_P (t))
6925 error ("%qD is not an argument in %<uniform%> clause", t);
6926 else
6927 error ("%qE is not an argument in %<uniform%> clause", t);
6928 remove = true;
6929 break;
6931 /* map_head bitmap is used as uniform_head if declare_simd. */
6932 bitmap_set_bit (&map_head, DECL_UID (t));
6933 goto check_dup_generic;
6935 case OMP_CLAUSE_GRAINSIZE:
6936 t = OMP_CLAUSE_GRAINSIZE_EXPR (c);
6937 if (t == error_mark_node)
6938 remove = true;
6939 else if (!type_dependent_expression_p (t)
6940 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6942 error ("%<grainsize%> expression must be integral");
6943 remove = true;
6945 else
6947 t = mark_rvalue_use (t);
6948 if (!processing_template_decl)
6950 t = maybe_constant_value (t);
6951 if (TREE_CODE (t) == INTEGER_CST
6952 && tree_int_cst_sgn (t) != 1)
6954 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6955 "%<grainsize%> value must be positive");
6956 t = integer_one_node;
6958 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6960 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
6962 break;
6964 case OMP_CLAUSE_PRIORITY:
6965 t = OMP_CLAUSE_PRIORITY_EXPR (c);
6966 if (t == error_mark_node)
6967 remove = true;
6968 else if (!type_dependent_expression_p (t)
6969 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6971 error ("%<priority%> expression must be integral");
6972 remove = true;
6974 else
6976 t = mark_rvalue_use (t);
6977 if (!processing_template_decl)
6979 t = maybe_constant_value (t);
6980 if (TREE_CODE (t) == INTEGER_CST
6981 && tree_int_cst_sgn (t) == -1)
6983 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6984 "%<priority%> value must be non-negative");
6985 t = integer_one_node;
6987 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6989 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
6991 break;
6993 case OMP_CLAUSE_HINT:
6994 t = OMP_CLAUSE_HINT_EXPR (c);
6995 if (t == error_mark_node)
6996 remove = true;
6997 else if (!type_dependent_expression_p (t)
6998 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
7000 error ("%<num_tasks%> expression must be integral");
7001 remove = true;
7003 else
7005 t = mark_rvalue_use (t);
7006 if (!processing_template_decl)
7008 t = maybe_constant_value (t);
7009 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7011 OMP_CLAUSE_HINT_EXPR (c) = t;
7013 break;
7015 case OMP_CLAUSE_IS_DEVICE_PTR:
7016 case OMP_CLAUSE_USE_DEVICE_PTR:
7017 field_ok = (ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP;
7018 t = OMP_CLAUSE_DECL (c);
7019 if (!type_dependent_expression_p (t))
7021 tree type = TREE_TYPE (t);
7022 if (TREE_CODE (type) != POINTER_TYPE
7023 && TREE_CODE (type) != ARRAY_TYPE
7024 && (TREE_CODE (type) != REFERENCE_TYPE
7025 || (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
7026 && TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE)))
7028 error_at (OMP_CLAUSE_LOCATION (c),
7029 "%qs variable is neither a pointer, nor an array "
7030 "nor reference to pointer or array",
7031 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7032 remove = true;
7035 goto check_dup_generic;
7037 case OMP_CLAUSE_NOWAIT:
7038 case OMP_CLAUSE_DEFAULT:
7039 case OMP_CLAUSE_UNTIED:
7040 case OMP_CLAUSE_COLLAPSE:
7041 case OMP_CLAUSE_MERGEABLE:
7042 case OMP_CLAUSE_PARALLEL:
7043 case OMP_CLAUSE_FOR:
7044 case OMP_CLAUSE_SECTIONS:
7045 case OMP_CLAUSE_TASKGROUP:
7046 case OMP_CLAUSE_PROC_BIND:
7047 case OMP_CLAUSE_NOGROUP:
7048 case OMP_CLAUSE_THREADS:
7049 case OMP_CLAUSE_SIMD:
7050 case OMP_CLAUSE_DEFAULTMAP:
7051 case OMP_CLAUSE_AUTO:
7052 case OMP_CLAUSE_INDEPENDENT:
7053 case OMP_CLAUSE_SEQ:
7054 break;
7056 case OMP_CLAUSE_TILE:
7057 for (tree list = OMP_CLAUSE_TILE_LIST (c); !remove && list;
7058 list = TREE_CHAIN (list))
7060 t = TREE_VALUE (list);
7062 if (t == error_mark_node)
7063 remove = true;
7064 else if (!type_dependent_expression_p (t)
7065 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
7067 error_at (OMP_CLAUSE_LOCATION (c),
7068 "%<tile%> argument needs integral type");
7069 remove = true;
7071 else
7073 t = mark_rvalue_use (t);
7074 if (!processing_template_decl)
7076 /* Zero is used to indicate '*', we permit you
7077 to get there via an ICE of value zero. */
7078 t = maybe_constant_value (t);
7079 if (!tree_fits_shwi_p (t)
7080 || tree_to_shwi (t) < 0)
7082 error_at (OMP_CLAUSE_LOCATION (c),
7083 "%<tile%> argument needs positive "
7084 "integral constant");
7085 remove = true;
7087 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7091 /* Update list item. */
7092 TREE_VALUE (list) = t;
7094 break;
7096 case OMP_CLAUSE_ORDERED:
7097 ordered_seen = true;
7098 break;
7100 case OMP_CLAUSE_INBRANCH:
7101 case OMP_CLAUSE_NOTINBRANCH:
7102 if (branch_seen)
7104 error ("%<inbranch%> clause is incompatible with "
7105 "%<notinbranch%>");
7106 remove = true;
7108 branch_seen = true;
7109 break;
7111 default:
7112 gcc_unreachable ();
7115 if (remove)
7116 *pc = OMP_CLAUSE_CHAIN (c);
7117 else
7118 pc = &OMP_CLAUSE_CHAIN (c);
7121 for (pc = &clauses, c = clauses; c ; c = *pc)
7123 enum omp_clause_code c_kind = OMP_CLAUSE_CODE (c);
7124 bool remove = false;
7125 bool need_complete_type = false;
7126 bool need_default_ctor = false;
7127 bool need_copy_ctor = false;
7128 bool need_copy_assignment = false;
7129 bool need_implicitly_determined = false;
7130 bool need_dtor = false;
7131 tree type, inner_type;
7133 switch (c_kind)
7135 case OMP_CLAUSE_SHARED:
7136 need_implicitly_determined = true;
7137 break;
7138 case OMP_CLAUSE_PRIVATE:
7139 need_complete_type = true;
7140 need_default_ctor = true;
7141 need_dtor = true;
7142 need_implicitly_determined = true;
7143 break;
7144 case OMP_CLAUSE_FIRSTPRIVATE:
7145 need_complete_type = true;
7146 need_copy_ctor = true;
7147 need_dtor = true;
7148 need_implicitly_determined = true;
7149 break;
7150 case OMP_CLAUSE_LASTPRIVATE:
7151 need_complete_type = true;
7152 need_copy_assignment = true;
7153 need_implicitly_determined = true;
7154 break;
7155 case OMP_CLAUSE_REDUCTION:
7156 need_implicitly_determined = true;
7157 break;
7158 case OMP_CLAUSE_LINEAR:
7159 if (ort != C_ORT_OMP_DECLARE_SIMD)
7160 need_implicitly_determined = true;
7161 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c)
7162 && !bitmap_bit_p (&map_head,
7163 DECL_UID (OMP_CLAUSE_LINEAR_STEP (c))))
7165 error_at (OMP_CLAUSE_LOCATION (c),
7166 "%<linear%> clause step is a parameter %qD not "
7167 "specified in %<uniform%> clause",
7168 OMP_CLAUSE_LINEAR_STEP (c));
7169 *pc = OMP_CLAUSE_CHAIN (c);
7170 continue;
7172 break;
7173 case OMP_CLAUSE_COPYPRIVATE:
7174 need_copy_assignment = true;
7175 break;
7176 case OMP_CLAUSE_COPYIN:
7177 need_copy_assignment = true;
7178 break;
7179 case OMP_CLAUSE_SIMDLEN:
7180 if (safelen
7181 && !processing_template_decl
7182 && tree_int_cst_lt (OMP_CLAUSE_SAFELEN_EXPR (safelen),
7183 OMP_CLAUSE_SIMDLEN_EXPR (c)))
7185 error_at (OMP_CLAUSE_LOCATION (c),
7186 "%<simdlen%> clause value is bigger than "
7187 "%<safelen%> clause value");
7188 OMP_CLAUSE_SIMDLEN_EXPR (c)
7189 = OMP_CLAUSE_SAFELEN_EXPR (safelen);
7191 pc = &OMP_CLAUSE_CHAIN (c);
7192 continue;
7193 case OMP_CLAUSE_SCHEDULE:
7194 if (ordered_seen
7195 && (OMP_CLAUSE_SCHEDULE_KIND (c)
7196 & OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
7198 error_at (OMP_CLAUSE_LOCATION (c),
7199 "%<nonmonotonic%> schedule modifier specified "
7200 "together with %<ordered%> clause");
7201 OMP_CLAUSE_SCHEDULE_KIND (c)
7202 = (enum omp_clause_schedule_kind)
7203 (OMP_CLAUSE_SCHEDULE_KIND (c)
7204 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
7206 pc = &OMP_CLAUSE_CHAIN (c);
7207 continue;
7208 case OMP_CLAUSE_NOWAIT:
7209 if (copyprivate_seen)
7211 error_at (OMP_CLAUSE_LOCATION (c),
7212 "%<nowait%> clause must not be used together "
7213 "with %<copyprivate%>");
7214 *pc = OMP_CLAUSE_CHAIN (c);
7215 continue;
7217 /* FALLTHRU */
7218 default:
7219 pc = &OMP_CLAUSE_CHAIN (c);
7220 continue;
7223 t = OMP_CLAUSE_DECL (c);
7224 if (processing_template_decl
7225 && !VAR_P (t) && TREE_CODE (t) != PARM_DECL)
7227 pc = &OMP_CLAUSE_CHAIN (c);
7228 continue;
7231 switch (c_kind)
7233 case OMP_CLAUSE_LASTPRIVATE:
7234 if (!bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
7236 need_default_ctor = true;
7237 need_dtor = true;
7239 break;
7241 case OMP_CLAUSE_REDUCTION:
7242 if (finish_omp_reduction_clause (c, &need_default_ctor,
7243 &need_dtor))
7244 remove = true;
7245 else
7246 t = OMP_CLAUSE_DECL (c);
7247 break;
7249 case OMP_CLAUSE_COPYIN:
7250 if (!VAR_P (t) || !CP_DECL_THREAD_LOCAL_P (t))
7252 error ("%qE must be %<threadprivate%> for %<copyin%>", t);
7253 remove = true;
7255 break;
7257 default:
7258 break;
7261 if (need_complete_type || need_copy_assignment)
7263 t = require_complete_type (t);
7264 if (t == error_mark_node)
7265 remove = true;
7266 else if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE
7267 && !complete_type_or_else (TREE_TYPE (TREE_TYPE (t)), t))
7268 remove = true;
7270 if (need_implicitly_determined)
7272 const char *share_name = NULL;
7274 if (VAR_P (t) && CP_DECL_THREAD_LOCAL_P (t))
7275 share_name = "threadprivate";
7276 else switch (cxx_omp_predetermined_sharing (t))
7278 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
7279 break;
7280 case OMP_CLAUSE_DEFAULT_SHARED:
7281 /* const vars may be specified in firstprivate clause. */
7282 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
7283 && cxx_omp_const_qual_no_mutable (t))
7284 break;
7285 share_name = "shared";
7286 break;
7287 case OMP_CLAUSE_DEFAULT_PRIVATE:
7288 share_name = "private";
7289 break;
7290 default:
7291 gcc_unreachable ();
7293 if (share_name)
7295 error ("%qE is predetermined %qs for %qs",
7296 omp_clause_printable_decl (t), share_name,
7297 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7298 remove = true;
7302 /* We're interested in the base element, not arrays. */
7303 inner_type = type = TREE_TYPE (t);
7304 if ((need_complete_type
7305 || need_copy_assignment
7306 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
7307 && TREE_CODE (inner_type) == REFERENCE_TYPE)
7308 inner_type = TREE_TYPE (inner_type);
7309 while (TREE_CODE (inner_type) == ARRAY_TYPE)
7310 inner_type = TREE_TYPE (inner_type);
7312 /* Check for special function availability by building a call to one.
7313 Save the results, because later we won't be in the right context
7314 for making these queries. */
7315 if (CLASS_TYPE_P (inner_type)
7316 && COMPLETE_TYPE_P (inner_type)
7317 && (need_default_ctor || need_copy_ctor
7318 || need_copy_assignment || need_dtor)
7319 && !type_dependent_expression_p (t)
7320 && cxx_omp_create_clause_info (c, inner_type, need_default_ctor,
7321 need_copy_ctor, need_copy_assignment,
7322 need_dtor))
7323 remove = true;
7325 if (!remove
7326 && c_kind == OMP_CLAUSE_SHARED
7327 && processing_template_decl)
7329 t = omp_clause_decl_field (OMP_CLAUSE_DECL (c));
7330 if (t)
7331 OMP_CLAUSE_DECL (c) = t;
7334 if (remove)
7335 *pc = OMP_CLAUSE_CHAIN (c);
7336 else
7337 pc = &OMP_CLAUSE_CHAIN (c);
7340 bitmap_obstack_release (NULL);
7341 return clauses;
7344 /* Start processing OpenMP clauses that can include any
7345 privatization clauses for non-static data members. */
7347 tree
7348 push_omp_privatization_clauses (bool ignore_next)
7350 if (omp_private_member_ignore_next)
7352 omp_private_member_ignore_next = ignore_next;
7353 return NULL_TREE;
7355 omp_private_member_ignore_next = ignore_next;
7356 if (omp_private_member_map)
7357 omp_private_member_vec.safe_push (error_mark_node);
7358 return push_stmt_list ();
7361 /* Revert remapping of any non-static data members since
7362 the last push_omp_privatization_clauses () call. */
7364 void
7365 pop_omp_privatization_clauses (tree stmt)
7367 if (stmt == NULL_TREE)
7368 return;
7369 stmt = pop_stmt_list (stmt);
7370 if (omp_private_member_map)
7372 while (!omp_private_member_vec.is_empty ())
7374 tree t = omp_private_member_vec.pop ();
7375 if (t == error_mark_node)
7377 add_stmt (stmt);
7378 return;
7380 bool no_decl_expr = t == integer_zero_node;
7381 if (no_decl_expr)
7382 t = omp_private_member_vec.pop ();
7383 tree *v = omp_private_member_map->get (t);
7384 gcc_assert (v);
7385 if (!no_decl_expr)
7386 add_decl_expr (*v);
7387 omp_private_member_map->remove (t);
7389 delete omp_private_member_map;
7390 omp_private_member_map = NULL;
7392 add_stmt (stmt);
7395 /* Remember OpenMP privatization clauses mapping and clear it.
7396 Used for lambdas. */
7398 void
7399 save_omp_privatization_clauses (vec<tree> &save)
7401 save = vNULL;
7402 if (omp_private_member_ignore_next)
7403 save.safe_push (integer_one_node);
7404 omp_private_member_ignore_next = false;
7405 if (!omp_private_member_map)
7406 return;
7408 while (!omp_private_member_vec.is_empty ())
7410 tree t = omp_private_member_vec.pop ();
7411 if (t == error_mark_node)
7413 save.safe_push (t);
7414 continue;
7416 tree n = t;
7417 if (t == integer_zero_node)
7418 t = omp_private_member_vec.pop ();
7419 tree *v = omp_private_member_map->get (t);
7420 gcc_assert (v);
7421 save.safe_push (*v);
7422 save.safe_push (t);
7423 if (n != t)
7424 save.safe_push (n);
7426 delete omp_private_member_map;
7427 omp_private_member_map = NULL;
7430 /* Restore OpenMP privatization clauses mapping saved by the
7431 above function. */
7433 void
7434 restore_omp_privatization_clauses (vec<tree> &save)
7436 gcc_assert (omp_private_member_vec.is_empty ());
7437 omp_private_member_ignore_next = false;
7438 if (save.is_empty ())
7439 return;
7440 if (save.length () == 1 && save[0] == integer_one_node)
7442 omp_private_member_ignore_next = true;
7443 save.release ();
7444 return;
7447 omp_private_member_map = new hash_map <tree, tree>;
7448 while (!save.is_empty ())
7450 tree t = save.pop ();
7451 tree n = t;
7452 if (t != error_mark_node)
7454 if (t == integer_one_node)
7456 omp_private_member_ignore_next = true;
7457 gcc_assert (save.is_empty ());
7458 break;
7460 if (t == integer_zero_node)
7461 t = save.pop ();
7462 tree &v = omp_private_member_map->get_or_insert (t);
7463 v = save.pop ();
7465 omp_private_member_vec.safe_push (t);
7466 if (n != t)
7467 omp_private_member_vec.safe_push (n);
7469 save.release ();
7472 /* For all variables in the tree_list VARS, mark them as thread local. */
7474 void
7475 finish_omp_threadprivate (tree vars)
7477 tree t;
7479 /* Mark every variable in VARS to be assigned thread local storage. */
7480 for (t = vars; t; t = TREE_CHAIN (t))
7482 tree v = TREE_PURPOSE (t);
7484 if (error_operand_p (v))
7486 else if (!VAR_P (v))
7487 error ("%<threadprivate%> %qD is not file, namespace "
7488 "or block scope variable", v);
7489 /* If V had already been marked threadprivate, it doesn't matter
7490 whether it had been used prior to this point. */
7491 else if (TREE_USED (v)
7492 && (DECL_LANG_SPECIFIC (v) == NULL
7493 || !CP_DECL_THREADPRIVATE_P (v)))
7494 error ("%qE declared %<threadprivate%> after first use", v);
7495 else if (! TREE_STATIC (v) && ! DECL_EXTERNAL (v))
7496 error ("automatic variable %qE cannot be %<threadprivate%>", v);
7497 else if (! COMPLETE_TYPE_P (complete_type (TREE_TYPE (v))))
7498 error ("%<threadprivate%> %qE has incomplete type", v);
7499 else if (TREE_STATIC (v) && TYPE_P (CP_DECL_CONTEXT (v))
7500 && CP_DECL_CONTEXT (v) != current_class_type)
7501 error ("%<threadprivate%> %qE directive not "
7502 "in %qT definition", v, CP_DECL_CONTEXT (v));
7503 else
7505 /* Allocate a LANG_SPECIFIC structure for V, if needed. */
7506 if (DECL_LANG_SPECIFIC (v) == NULL)
7508 retrofit_lang_decl (v);
7510 /* Make sure that DECL_DISCRIMINATOR_P continues to be true
7511 after the allocation of the lang_decl structure. */
7512 if (DECL_DISCRIMINATOR_P (v))
7513 DECL_LANG_SPECIFIC (v)->u.base.u2sel = 1;
7516 if (! CP_DECL_THREAD_LOCAL_P (v))
7518 CP_DECL_THREAD_LOCAL_P (v) = true;
7519 set_decl_tls_model (v, decl_default_tls_model (v));
7520 /* If rtl has been already set for this var, call
7521 make_decl_rtl once again, so that encode_section_info
7522 has a chance to look at the new decl flags. */
7523 if (DECL_RTL_SET_P (v))
7524 make_decl_rtl (v);
7526 CP_DECL_THREADPRIVATE_P (v) = 1;
7531 /* Build an OpenMP structured block. */
7533 tree
7534 begin_omp_structured_block (void)
7536 return do_pushlevel (sk_omp);
7539 tree
7540 finish_omp_structured_block (tree block)
7542 return do_poplevel (block);
7545 /* Similarly, except force the retention of the BLOCK. */
7547 tree
7548 begin_omp_parallel (void)
7550 keep_next_level (true);
7551 return begin_omp_structured_block ();
7554 /* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
7555 statement. */
7557 tree
7558 finish_oacc_data (tree clauses, tree block)
7560 tree stmt;
7562 block = finish_omp_structured_block (block);
7564 stmt = make_node (OACC_DATA);
7565 TREE_TYPE (stmt) = void_type_node;
7566 OACC_DATA_CLAUSES (stmt) = clauses;
7567 OACC_DATA_BODY (stmt) = block;
7569 return add_stmt (stmt);
7572 /* Generate OACC_HOST_DATA, with CLAUSES and BLOCK as its compound
7573 statement. */
7575 tree
7576 finish_oacc_host_data (tree clauses, tree block)
7578 tree stmt;
7580 block = finish_omp_structured_block (block);
7582 stmt = make_node (OACC_HOST_DATA);
7583 TREE_TYPE (stmt) = void_type_node;
7584 OACC_HOST_DATA_CLAUSES (stmt) = clauses;
7585 OACC_HOST_DATA_BODY (stmt) = block;
7587 return add_stmt (stmt);
7590 /* Generate OMP construct CODE, with BODY and CLAUSES as its compound
7591 statement. */
7593 tree
7594 finish_omp_construct (enum tree_code code, tree body, tree clauses)
7596 body = finish_omp_structured_block (body);
7598 tree stmt = make_node (code);
7599 TREE_TYPE (stmt) = void_type_node;
7600 OMP_BODY (stmt) = body;
7601 OMP_CLAUSES (stmt) = clauses;
7603 return add_stmt (stmt);
7606 tree
7607 finish_omp_parallel (tree clauses, tree body)
7609 tree stmt;
7611 body = finish_omp_structured_block (body);
7613 stmt = make_node (OMP_PARALLEL);
7614 TREE_TYPE (stmt) = void_type_node;
7615 OMP_PARALLEL_CLAUSES (stmt) = clauses;
7616 OMP_PARALLEL_BODY (stmt) = body;
7618 return add_stmt (stmt);
7621 tree
7622 begin_omp_task (void)
7624 keep_next_level (true);
7625 return begin_omp_structured_block ();
7628 tree
7629 finish_omp_task (tree clauses, tree body)
7631 tree stmt;
7633 body = finish_omp_structured_block (body);
7635 stmt = make_node (OMP_TASK);
7636 TREE_TYPE (stmt) = void_type_node;
7637 OMP_TASK_CLAUSES (stmt) = clauses;
7638 OMP_TASK_BODY (stmt) = body;
7640 return add_stmt (stmt);
7643 /* Helper function for finish_omp_for. Convert Ith random access iterator
7644 into integral iterator. Return FALSE if successful. */
7646 static bool
7647 handle_omp_for_class_iterator (int i, location_t locus, enum tree_code code,
7648 tree declv, tree orig_declv, tree initv,
7649 tree condv, tree incrv, tree *body,
7650 tree *pre_body, tree &clauses, tree *lastp,
7651 int collapse, int ordered)
7653 tree diff, iter_init, iter_incr = NULL, last;
7654 tree incr_var = NULL, orig_pre_body, orig_body, c;
7655 tree decl = TREE_VEC_ELT (declv, i);
7656 tree init = TREE_VEC_ELT (initv, i);
7657 tree cond = TREE_VEC_ELT (condv, i);
7658 tree incr = TREE_VEC_ELT (incrv, i);
7659 tree iter = decl;
7660 location_t elocus = locus;
7662 if (init && EXPR_HAS_LOCATION (init))
7663 elocus = EXPR_LOCATION (init);
7665 cond = cp_fully_fold (cond);
7666 switch (TREE_CODE (cond))
7668 case GT_EXPR:
7669 case GE_EXPR:
7670 case LT_EXPR:
7671 case LE_EXPR:
7672 case NE_EXPR:
7673 if (TREE_OPERAND (cond, 1) == iter)
7674 cond = build2 (swap_tree_comparison (TREE_CODE (cond)),
7675 TREE_TYPE (cond), iter, TREE_OPERAND (cond, 0));
7676 if (TREE_OPERAND (cond, 0) != iter)
7677 cond = error_mark_node;
7678 else
7680 tree tem = build_x_binary_op (EXPR_LOCATION (cond),
7681 TREE_CODE (cond),
7682 iter, ERROR_MARK,
7683 TREE_OPERAND (cond, 1), ERROR_MARK,
7684 NULL, tf_warning_or_error);
7685 if (error_operand_p (tem))
7686 return true;
7688 break;
7689 default:
7690 cond = error_mark_node;
7691 break;
7693 if (cond == error_mark_node)
7695 error_at (elocus, "invalid controlling predicate");
7696 return true;
7698 diff = build_x_binary_op (elocus, MINUS_EXPR, TREE_OPERAND (cond, 1),
7699 ERROR_MARK, iter, ERROR_MARK, NULL,
7700 tf_warning_or_error);
7701 diff = cp_fully_fold (diff);
7702 if (error_operand_p (diff))
7703 return true;
7704 if (TREE_CODE (TREE_TYPE (diff)) != INTEGER_TYPE)
7706 error_at (elocus, "difference between %qE and %qD does not have integer type",
7707 TREE_OPERAND (cond, 1), iter);
7708 return true;
7710 if (!c_omp_check_loop_iv_exprs (locus, orig_declv,
7711 TREE_VEC_ELT (declv, i), NULL_TREE,
7712 cond, cp_walk_subtrees))
7713 return true;
7715 switch (TREE_CODE (incr))
7717 case PREINCREMENT_EXPR:
7718 case PREDECREMENT_EXPR:
7719 case POSTINCREMENT_EXPR:
7720 case POSTDECREMENT_EXPR:
7721 if (TREE_OPERAND (incr, 0) != iter)
7723 incr = error_mark_node;
7724 break;
7726 iter_incr = build_x_unary_op (EXPR_LOCATION (incr),
7727 TREE_CODE (incr), iter,
7728 tf_warning_or_error);
7729 if (error_operand_p (iter_incr))
7730 return true;
7731 else if (TREE_CODE (incr) == PREINCREMENT_EXPR
7732 || TREE_CODE (incr) == POSTINCREMENT_EXPR)
7733 incr = integer_one_node;
7734 else
7735 incr = integer_minus_one_node;
7736 break;
7737 case MODIFY_EXPR:
7738 if (TREE_OPERAND (incr, 0) != iter)
7739 incr = error_mark_node;
7740 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
7741 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
7743 tree rhs = TREE_OPERAND (incr, 1);
7744 if (TREE_OPERAND (rhs, 0) == iter)
7746 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (rhs, 1)))
7747 != INTEGER_TYPE)
7748 incr = error_mark_node;
7749 else
7751 iter_incr = build_x_modify_expr (EXPR_LOCATION (rhs),
7752 iter, TREE_CODE (rhs),
7753 TREE_OPERAND (rhs, 1),
7754 tf_warning_or_error);
7755 if (error_operand_p (iter_incr))
7756 return true;
7757 incr = TREE_OPERAND (rhs, 1);
7758 incr = cp_convert (TREE_TYPE (diff), incr,
7759 tf_warning_or_error);
7760 if (TREE_CODE (rhs) == MINUS_EXPR)
7762 incr = build1 (NEGATE_EXPR, TREE_TYPE (diff), incr);
7763 incr = fold_simple (incr);
7765 if (TREE_CODE (incr) != INTEGER_CST
7766 && (TREE_CODE (incr) != NOP_EXPR
7767 || (TREE_CODE (TREE_OPERAND (incr, 0))
7768 != INTEGER_CST)))
7769 iter_incr = NULL;
7772 else if (TREE_OPERAND (rhs, 1) == iter)
7774 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (rhs, 0))) != INTEGER_TYPE
7775 || TREE_CODE (rhs) != PLUS_EXPR)
7776 incr = error_mark_node;
7777 else
7779 iter_incr = build_x_binary_op (EXPR_LOCATION (rhs),
7780 PLUS_EXPR,
7781 TREE_OPERAND (rhs, 0),
7782 ERROR_MARK, iter,
7783 ERROR_MARK, NULL,
7784 tf_warning_or_error);
7785 if (error_operand_p (iter_incr))
7786 return true;
7787 iter_incr = build_x_modify_expr (EXPR_LOCATION (rhs),
7788 iter, NOP_EXPR,
7789 iter_incr,
7790 tf_warning_or_error);
7791 if (error_operand_p (iter_incr))
7792 return true;
7793 incr = TREE_OPERAND (rhs, 0);
7794 iter_incr = NULL;
7797 else
7798 incr = error_mark_node;
7800 else
7801 incr = error_mark_node;
7802 break;
7803 default:
7804 incr = error_mark_node;
7805 break;
7808 if (incr == error_mark_node)
7810 error_at (elocus, "invalid increment expression");
7811 return true;
7814 incr = cp_convert (TREE_TYPE (diff), incr, tf_warning_or_error);
7815 bool taskloop_iv_seen = false;
7816 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
7817 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
7818 && OMP_CLAUSE_DECL (c) == iter)
7820 if (code == OMP_TASKLOOP)
7822 taskloop_iv_seen = true;
7823 OMP_CLAUSE_LASTPRIVATE_TASKLOOP_IV (c) = 1;
7825 break;
7827 else if (code == OMP_TASKLOOP
7828 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
7829 && OMP_CLAUSE_DECL (c) == iter)
7831 taskloop_iv_seen = true;
7832 OMP_CLAUSE_PRIVATE_TASKLOOP_IV (c) = 1;
7835 decl = create_temporary_var (TREE_TYPE (diff));
7836 pushdecl (decl);
7837 add_decl_expr (decl);
7838 last = create_temporary_var (TREE_TYPE (diff));
7839 pushdecl (last);
7840 add_decl_expr (last);
7841 if (c && iter_incr == NULL && TREE_CODE (incr) != INTEGER_CST
7842 && (!ordered || (i < collapse && collapse > 1)))
7844 incr_var = create_temporary_var (TREE_TYPE (diff));
7845 pushdecl (incr_var);
7846 add_decl_expr (incr_var);
7848 gcc_assert (stmts_are_full_exprs_p ());
7849 tree diffvar = NULL_TREE;
7850 if (code == OMP_TASKLOOP)
7852 if (!taskloop_iv_seen)
7854 tree ivc = build_omp_clause (locus, OMP_CLAUSE_FIRSTPRIVATE);
7855 OMP_CLAUSE_DECL (ivc) = iter;
7856 cxx_omp_finish_clause (ivc, NULL);
7857 OMP_CLAUSE_CHAIN (ivc) = clauses;
7858 clauses = ivc;
7860 tree lvc = build_omp_clause (locus, OMP_CLAUSE_FIRSTPRIVATE);
7861 OMP_CLAUSE_DECL (lvc) = last;
7862 OMP_CLAUSE_CHAIN (lvc) = clauses;
7863 clauses = lvc;
7864 diffvar = create_temporary_var (TREE_TYPE (diff));
7865 pushdecl (diffvar);
7866 add_decl_expr (diffvar);
7869 orig_pre_body = *pre_body;
7870 *pre_body = push_stmt_list ();
7871 if (orig_pre_body)
7872 add_stmt (orig_pre_body);
7873 if (init != NULL)
7874 finish_expr_stmt (build_x_modify_expr (elocus,
7875 iter, NOP_EXPR, init,
7876 tf_warning_or_error));
7877 init = build_int_cst (TREE_TYPE (diff), 0);
7878 if (c && iter_incr == NULL
7879 && (!ordered || (i < collapse && collapse > 1)))
7881 if (incr_var)
7883 finish_expr_stmt (build_x_modify_expr (elocus,
7884 incr_var, NOP_EXPR,
7885 incr, tf_warning_or_error));
7886 incr = incr_var;
7888 iter_incr = build_x_modify_expr (elocus,
7889 iter, PLUS_EXPR, incr,
7890 tf_warning_or_error);
7892 if (c && ordered && i < collapse && collapse > 1)
7893 iter_incr = incr;
7894 finish_expr_stmt (build_x_modify_expr (elocus,
7895 last, NOP_EXPR, init,
7896 tf_warning_or_error));
7897 if (diffvar)
7899 finish_expr_stmt (build_x_modify_expr (elocus,
7900 diffvar, NOP_EXPR,
7901 diff, tf_warning_or_error));
7902 diff = diffvar;
7904 *pre_body = pop_stmt_list (*pre_body);
7906 cond = cp_build_binary_op (elocus,
7907 TREE_CODE (cond), decl, diff,
7908 tf_warning_or_error);
7909 incr = build_modify_expr (elocus, decl, NULL_TREE, PLUS_EXPR,
7910 elocus, incr, NULL_TREE);
7912 orig_body = *body;
7913 *body = push_stmt_list ();
7914 iter_init = build2 (MINUS_EXPR, TREE_TYPE (diff), decl, last);
7915 iter_init = build_x_modify_expr (elocus,
7916 iter, PLUS_EXPR, iter_init,
7917 tf_warning_or_error);
7918 if (iter_init != error_mark_node)
7919 iter_init = build1 (NOP_EXPR, void_type_node, iter_init);
7920 finish_expr_stmt (iter_init);
7921 finish_expr_stmt (build_x_modify_expr (elocus,
7922 last, NOP_EXPR, decl,
7923 tf_warning_or_error));
7924 add_stmt (orig_body);
7925 *body = pop_stmt_list (*body);
7927 if (c)
7929 OMP_CLAUSE_LASTPRIVATE_STMT (c) = push_stmt_list ();
7930 if (!ordered)
7931 finish_expr_stmt (iter_incr);
7932 else
7934 iter_init = decl;
7935 if (i < collapse && collapse > 1 && !error_operand_p (iter_incr))
7936 iter_init = build2 (PLUS_EXPR, TREE_TYPE (diff),
7937 iter_init, iter_incr);
7938 iter_init = build2 (MINUS_EXPR, TREE_TYPE (diff), iter_init, last);
7939 iter_init = build_x_modify_expr (elocus,
7940 iter, PLUS_EXPR, iter_init,
7941 tf_warning_or_error);
7942 if (iter_init != error_mark_node)
7943 iter_init = build1 (NOP_EXPR, void_type_node, iter_init);
7944 finish_expr_stmt (iter_init);
7946 OMP_CLAUSE_LASTPRIVATE_STMT (c)
7947 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (c));
7950 TREE_VEC_ELT (declv, i) = decl;
7951 TREE_VEC_ELT (initv, i) = init;
7952 TREE_VEC_ELT (condv, i) = cond;
7953 TREE_VEC_ELT (incrv, i) = incr;
7954 *lastp = last;
7956 return false;
7959 /* Build and validate an OMP_FOR statement. CLAUSES, BODY, COND, INCR
7960 are directly for their associated operands in the statement. DECL
7961 and INIT are a combo; if DECL is NULL then INIT ought to be a
7962 MODIFY_EXPR, and the DECL should be extracted. PRE_BODY are
7963 optional statements that need to go before the loop into its
7964 sk_omp scope. */
7966 tree
7967 finish_omp_for (location_t locus, enum tree_code code, tree declv,
7968 tree orig_declv, tree initv, tree condv, tree incrv,
7969 tree body, tree pre_body, vec<tree> *orig_inits, tree clauses)
7971 tree omp_for = NULL, orig_incr = NULL;
7972 tree decl = NULL, init, cond, incr;
7973 tree last = NULL_TREE;
7974 location_t elocus;
7975 int i;
7976 int collapse = 1;
7977 int ordered = 0;
7979 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (initv));
7980 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (condv));
7981 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (incrv));
7982 if (TREE_VEC_LENGTH (declv) > 1)
7984 tree c;
7986 c = omp_find_clause (clauses, OMP_CLAUSE_TILE);
7987 if (c)
7988 collapse = list_length (OMP_CLAUSE_TILE_LIST (c));
7989 else
7991 c = omp_find_clause (clauses, OMP_CLAUSE_COLLAPSE);
7992 if (c)
7993 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (c));
7994 if (collapse != TREE_VEC_LENGTH (declv))
7995 ordered = TREE_VEC_LENGTH (declv);
7998 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
8000 decl = TREE_VEC_ELT (declv, i);
8001 init = TREE_VEC_ELT (initv, i);
8002 cond = TREE_VEC_ELT (condv, i);
8003 incr = TREE_VEC_ELT (incrv, i);
8004 elocus = locus;
8006 if (decl == NULL)
8008 if (init != NULL)
8009 switch (TREE_CODE (init))
8011 case MODIFY_EXPR:
8012 decl = TREE_OPERAND (init, 0);
8013 init = TREE_OPERAND (init, 1);
8014 break;
8015 case MODOP_EXPR:
8016 if (TREE_CODE (TREE_OPERAND (init, 1)) == NOP_EXPR)
8018 decl = TREE_OPERAND (init, 0);
8019 init = TREE_OPERAND (init, 2);
8021 break;
8022 default:
8023 break;
8026 if (decl == NULL)
8028 error_at (locus,
8029 "expected iteration declaration or initialization");
8030 return NULL;
8034 if (init && EXPR_HAS_LOCATION (init))
8035 elocus = EXPR_LOCATION (init);
8037 if (cond == NULL)
8039 error_at (elocus, "missing controlling predicate");
8040 return NULL;
8043 if (incr == NULL)
8045 error_at (elocus, "missing increment expression");
8046 return NULL;
8049 TREE_VEC_ELT (declv, i) = decl;
8050 TREE_VEC_ELT (initv, i) = init;
8053 if (orig_inits)
8055 bool fail = false;
8056 tree orig_init;
8057 FOR_EACH_VEC_ELT (*orig_inits, i, orig_init)
8058 if (orig_init
8059 && !c_omp_check_loop_iv_exprs (locus, declv,
8060 TREE_VEC_ELT (declv, i), orig_init,
8061 NULL_TREE, cp_walk_subtrees))
8062 fail = true;
8063 if (fail)
8064 return NULL;
8067 if (dependent_omp_for_p (declv, initv, condv, incrv))
8069 tree stmt;
8071 stmt = make_node (code);
8073 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
8075 /* This is really just a place-holder. We'll be decomposing this
8076 again and going through the cp_build_modify_expr path below when
8077 we instantiate the thing. */
8078 TREE_VEC_ELT (initv, i)
8079 = build2 (MODIFY_EXPR, void_type_node, TREE_VEC_ELT (declv, i),
8080 TREE_VEC_ELT (initv, i));
8083 TREE_TYPE (stmt) = void_type_node;
8084 OMP_FOR_INIT (stmt) = initv;
8085 OMP_FOR_COND (stmt) = condv;
8086 OMP_FOR_INCR (stmt) = incrv;
8087 OMP_FOR_BODY (stmt) = body;
8088 OMP_FOR_PRE_BODY (stmt) = pre_body;
8089 OMP_FOR_CLAUSES (stmt) = clauses;
8091 SET_EXPR_LOCATION (stmt, locus);
8092 return add_stmt (stmt);
8095 if (!orig_declv)
8096 orig_declv = copy_node (declv);
8098 if (processing_template_decl)
8099 orig_incr = make_tree_vec (TREE_VEC_LENGTH (incrv));
8101 for (i = 0; i < TREE_VEC_LENGTH (declv); )
8103 decl = TREE_VEC_ELT (declv, i);
8104 init = TREE_VEC_ELT (initv, i);
8105 cond = TREE_VEC_ELT (condv, i);
8106 incr = TREE_VEC_ELT (incrv, i);
8107 if (orig_incr)
8108 TREE_VEC_ELT (orig_incr, i) = incr;
8109 elocus = locus;
8111 if (init && EXPR_HAS_LOCATION (init))
8112 elocus = EXPR_LOCATION (init);
8114 if (!DECL_P (decl))
8116 error_at (elocus, "expected iteration declaration or initialization");
8117 return NULL;
8120 if (incr && TREE_CODE (incr) == MODOP_EXPR)
8122 if (orig_incr)
8123 TREE_VEC_ELT (orig_incr, i) = incr;
8124 incr = cp_build_modify_expr (elocus, TREE_OPERAND (incr, 0),
8125 TREE_CODE (TREE_OPERAND (incr, 1)),
8126 TREE_OPERAND (incr, 2),
8127 tf_warning_or_error);
8130 if (CLASS_TYPE_P (TREE_TYPE (decl)))
8132 if (code == OMP_SIMD)
8134 error_at (elocus, "%<#pragma omp simd%> used with class "
8135 "iteration variable %qE", decl);
8136 return NULL;
8138 if (handle_omp_for_class_iterator (i, locus, code, declv, orig_declv,
8139 initv, condv, incrv, &body,
8140 &pre_body, clauses, &last,
8141 collapse, ordered))
8142 return NULL;
8143 continue;
8146 if (!INTEGRAL_TYPE_P (TREE_TYPE (decl))
8147 && !TYPE_PTR_P (TREE_TYPE (decl)))
8149 error_at (elocus, "invalid type for iteration variable %qE", decl);
8150 return NULL;
8153 if (!processing_template_decl)
8155 init = fold_build_cleanup_point_expr (TREE_TYPE (init), init);
8156 init = cp_build_modify_expr (elocus, decl, NOP_EXPR, init,
8157 tf_warning_or_error);
8159 else
8160 init = build2 (MODIFY_EXPR, void_type_node, decl, init);
8161 if (cond
8162 && TREE_SIDE_EFFECTS (cond)
8163 && COMPARISON_CLASS_P (cond)
8164 && !processing_template_decl)
8166 tree t = TREE_OPERAND (cond, 0);
8167 if (TREE_SIDE_EFFECTS (t)
8168 && t != decl
8169 && (TREE_CODE (t) != NOP_EXPR
8170 || TREE_OPERAND (t, 0) != decl))
8171 TREE_OPERAND (cond, 0)
8172 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
8174 t = TREE_OPERAND (cond, 1);
8175 if (TREE_SIDE_EFFECTS (t)
8176 && t != decl
8177 && (TREE_CODE (t) != NOP_EXPR
8178 || TREE_OPERAND (t, 0) != decl))
8179 TREE_OPERAND (cond, 1)
8180 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
8182 if (decl == error_mark_node || init == error_mark_node)
8183 return NULL;
8185 TREE_VEC_ELT (declv, i) = decl;
8186 TREE_VEC_ELT (initv, i) = init;
8187 TREE_VEC_ELT (condv, i) = cond;
8188 TREE_VEC_ELT (incrv, i) = incr;
8189 i++;
8192 if (IS_EMPTY_STMT (pre_body))
8193 pre_body = NULL;
8195 omp_for = c_finish_omp_for (locus, code, declv, orig_declv, initv, condv,
8196 incrv, body, pre_body);
8198 /* Check for iterators appearing in lb, b or incr expressions. */
8199 if (omp_for && !c_omp_check_loop_iv (omp_for, orig_declv, cp_walk_subtrees))
8200 omp_for = NULL_TREE;
8202 if (omp_for == NULL)
8204 return NULL;
8207 add_stmt (omp_for);
8209 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INCR (omp_for)); i++)
8211 decl = TREE_OPERAND (TREE_VEC_ELT (OMP_FOR_INIT (omp_for), i), 0);
8212 incr = TREE_VEC_ELT (OMP_FOR_INCR (omp_for), i);
8214 if (TREE_CODE (incr) != MODIFY_EXPR)
8215 continue;
8217 if (TREE_SIDE_EFFECTS (TREE_OPERAND (incr, 1))
8218 && BINARY_CLASS_P (TREE_OPERAND (incr, 1))
8219 && !processing_template_decl)
8221 tree t = TREE_OPERAND (TREE_OPERAND (incr, 1), 0);
8222 if (TREE_SIDE_EFFECTS (t)
8223 && t != decl
8224 && (TREE_CODE (t) != NOP_EXPR
8225 || TREE_OPERAND (t, 0) != decl))
8226 TREE_OPERAND (TREE_OPERAND (incr, 1), 0)
8227 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
8229 t = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
8230 if (TREE_SIDE_EFFECTS (t)
8231 && t != decl
8232 && (TREE_CODE (t) != NOP_EXPR
8233 || TREE_OPERAND (t, 0) != decl))
8234 TREE_OPERAND (TREE_OPERAND (incr, 1), 1)
8235 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
8238 if (orig_incr)
8239 TREE_VEC_ELT (OMP_FOR_INCR (omp_for), i) = TREE_VEC_ELT (orig_incr, i);
8241 OMP_FOR_CLAUSES (omp_for) = clauses;
8243 /* For simd loops with non-static data member iterators, we could have added
8244 OMP_CLAUSE_LINEAR clauses without OMP_CLAUSE_LINEAR_STEP. As we know the
8245 step at this point, fill it in. */
8246 if (code == OMP_SIMD && !processing_template_decl
8247 && TREE_VEC_LENGTH (OMP_FOR_INCR (omp_for)) == 1)
8248 for (tree c = omp_find_clause (clauses, OMP_CLAUSE_LINEAR); c;
8249 c = omp_find_clause (OMP_CLAUSE_CHAIN (c), OMP_CLAUSE_LINEAR))
8250 if (OMP_CLAUSE_LINEAR_STEP (c) == NULL_TREE)
8252 decl = TREE_OPERAND (TREE_VEC_ELT (OMP_FOR_INIT (omp_for), 0), 0);
8253 gcc_assert (decl == OMP_CLAUSE_DECL (c));
8254 incr = TREE_VEC_ELT (OMP_FOR_INCR (omp_for), 0);
8255 tree step, stept;
8256 switch (TREE_CODE (incr))
8258 case PREINCREMENT_EXPR:
8259 case POSTINCREMENT_EXPR:
8260 /* c_omp_for_incr_canonicalize_ptr() should have been
8261 called to massage things appropriately. */
8262 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (decl)));
8263 OMP_CLAUSE_LINEAR_STEP (c) = build_int_cst (TREE_TYPE (decl), 1);
8264 break;
8265 case PREDECREMENT_EXPR:
8266 case POSTDECREMENT_EXPR:
8267 /* c_omp_for_incr_canonicalize_ptr() should have been
8268 called to massage things appropriately. */
8269 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (decl)));
8270 OMP_CLAUSE_LINEAR_STEP (c)
8271 = build_int_cst (TREE_TYPE (decl), -1);
8272 break;
8273 case MODIFY_EXPR:
8274 gcc_assert (TREE_OPERAND (incr, 0) == decl);
8275 incr = TREE_OPERAND (incr, 1);
8276 switch (TREE_CODE (incr))
8278 case PLUS_EXPR:
8279 if (TREE_OPERAND (incr, 1) == decl)
8280 step = TREE_OPERAND (incr, 0);
8281 else
8282 step = TREE_OPERAND (incr, 1);
8283 break;
8284 case MINUS_EXPR:
8285 case POINTER_PLUS_EXPR:
8286 gcc_assert (TREE_OPERAND (incr, 0) == decl);
8287 step = TREE_OPERAND (incr, 1);
8288 break;
8289 default:
8290 gcc_unreachable ();
8292 stept = TREE_TYPE (decl);
8293 if (POINTER_TYPE_P (stept))
8294 stept = sizetype;
8295 step = fold_convert (stept, step);
8296 if (TREE_CODE (incr) == MINUS_EXPR)
8297 step = fold_build1 (NEGATE_EXPR, stept, step);
8298 OMP_CLAUSE_LINEAR_STEP (c) = step;
8299 break;
8300 default:
8301 gcc_unreachable ();
8305 return omp_for;
8308 void
8309 finish_omp_atomic (enum tree_code code, enum tree_code opcode, tree lhs,
8310 tree rhs, tree v, tree lhs1, tree rhs1, bool seq_cst)
8312 tree orig_lhs;
8313 tree orig_rhs;
8314 tree orig_v;
8315 tree orig_lhs1;
8316 tree orig_rhs1;
8317 bool dependent_p;
8318 tree stmt;
8320 orig_lhs = lhs;
8321 orig_rhs = rhs;
8322 orig_v = v;
8323 orig_lhs1 = lhs1;
8324 orig_rhs1 = rhs1;
8325 dependent_p = false;
8326 stmt = NULL_TREE;
8328 /* Even in a template, we can detect invalid uses of the atomic
8329 pragma if neither LHS nor RHS is type-dependent. */
8330 if (processing_template_decl)
8332 dependent_p = (type_dependent_expression_p (lhs)
8333 || (rhs && type_dependent_expression_p (rhs))
8334 || (v && type_dependent_expression_p (v))
8335 || (lhs1 && type_dependent_expression_p (lhs1))
8336 || (rhs1 && type_dependent_expression_p (rhs1)));
8337 if (!dependent_p)
8339 lhs = build_non_dependent_expr (lhs);
8340 if (rhs)
8341 rhs = build_non_dependent_expr (rhs);
8342 if (v)
8343 v = build_non_dependent_expr (v);
8344 if (lhs1)
8345 lhs1 = build_non_dependent_expr (lhs1);
8346 if (rhs1)
8347 rhs1 = build_non_dependent_expr (rhs1);
8350 if (!dependent_p)
8352 bool swapped = false;
8353 if (rhs1 && cp_tree_equal (lhs, rhs))
8355 std::swap (rhs, rhs1);
8356 swapped = !commutative_tree_code (opcode);
8358 if (rhs1 && !cp_tree_equal (lhs, rhs1))
8360 if (code == OMP_ATOMIC)
8361 error ("%<#pragma omp atomic update%> uses two different "
8362 "expressions for memory");
8363 else
8364 error ("%<#pragma omp atomic capture%> uses two different "
8365 "expressions for memory");
8366 return;
8368 if (lhs1 && !cp_tree_equal (lhs, lhs1))
8370 if (code == OMP_ATOMIC)
8371 error ("%<#pragma omp atomic update%> uses two different "
8372 "expressions for memory");
8373 else
8374 error ("%<#pragma omp atomic capture%> uses two different "
8375 "expressions for memory");
8376 return;
8378 stmt = c_finish_omp_atomic (input_location, code, opcode, lhs, rhs,
8379 v, lhs1, rhs1, swapped, seq_cst,
8380 processing_template_decl != 0);
8381 if (stmt == error_mark_node)
8382 return;
8384 if (processing_template_decl)
8386 if (code == OMP_ATOMIC_READ)
8388 stmt = build_min_nt_loc (EXPR_LOCATION (orig_lhs),
8389 OMP_ATOMIC_READ, orig_lhs);
8390 OMP_ATOMIC_SEQ_CST (stmt) = seq_cst;
8391 stmt = build2 (MODIFY_EXPR, void_type_node, orig_v, stmt);
8393 else
8395 if (opcode == NOP_EXPR)
8396 stmt = build2 (MODIFY_EXPR, void_type_node, orig_lhs, orig_rhs);
8397 else
8398 stmt = build2 (opcode, void_type_node, orig_lhs, orig_rhs);
8399 if (orig_rhs1)
8400 stmt = build_min_nt_loc (EXPR_LOCATION (orig_rhs1),
8401 COMPOUND_EXPR, orig_rhs1, stmt);
8402 if (code != OMP_ATOMIC)
8404 stmt = build_min_nt_loc (EXPR_LOCATION (orig_lhs1),
8405 code, orig_lhs1, stmt);
8406 OMP_ATOMIC_SEQ_CST (stmt) = seq_cst;
8407 stmt = build2 (MODIFY_EXPR, void_type_node, orig_v, stmt);
8410 stmt = build2 (OMP_ATOMIC, void_type_node, integer_zero_node, stmt);
8411 OMP_ATOMIC_SEQ_CST (stmt) = seq_cst;
8413 finish_expr_stmt (stmt);
8416 void
8417 finish_omp_barrier (void)
8419 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_BARRIER);
8420 vec<tree, va_gc> *vec = make_tree_vector ();
8421 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
8422 release_tree_vector (vec);
8423 finish_expr_stmt (stmt);
8426 void
8427 finish_omp_flush (void)
8429 tree fn = builtin_decl_explicit (BUILT_IN_SYNC_SYNCHRONIZE);
8430 vec<tree, va_gc> *vec = make_tree_vector ();
8431 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
8432 release_tree_vector (vec);
8433 finish_expr_stmt (stmt);
8436 void
8437 finish_omp_taskwait (void)
8439 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_TASKWAIT);
8440 vec<tree, va_gc> *vec = make_tree_vector ();
8441 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
8442 release_tree_vector (vec);
8443 finish_expr_stmt (stmt);
8446 void
8447 finish_omp_taskyield (void)
8449 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_TASKYIELD);
8450 vec<tree, va_gc> *vec = make_tree_vector ();
8451 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
8452 release_tree_vector (vec);
8453 finish_expr_stmt (stmt);
8456 void
8457 finish_omp_cancel (tree clauses)
8459 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
8460 int mask = 0;
8461 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
8462 mask = 1;
8463 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
8464 mask = 2;
8465 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
8466 mask = 4;
8467 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
8468 mask = 8;
8469 else
8471 error ("%<#pragma omp cancel%> must specify one of "
8472 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> clauses");
8473 return;
8475 vec<tree, va_gc> *vec = make_tree_vector ();
8476 tree ifc = omp_find_clause (clauses, OMP_CLAUSE_IF);
8477 if (ifc != NULL_TREE)
8479 tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
8480 ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
8481 boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
8482 build_zero_cst (type));
8484 else
8485 ifc = boolean_true_node;
8486 vec->quick_push (build_int_cst (integer_type_node, mask));
8487 vec->quick_push (ifc);
8488 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
8489 release_tree_vector (vec);
8490 finish_expr_stmt (stmt);
8493 void
8494 finish_omp_cancellation_point (tree clauses)
8496 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
8497 int mask = 0;
8498 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
8499 mask = 1;
8500 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
8501 mask = 2;
8502 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
8503 mask = 4;
8504 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
8505 mask = 8;
8506 else
8508 error ("%<#pragma omp cancellation point%> must specify one of "
8509 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> clauses");
8510 return;
8512 vec<tree, va_gc> *vec
8513 = make_tree_vector_single (build_int_cst (integer_type_node, mask));
8514 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
8515 release_tree_vector (vec);
8516 finish_expr_stmt (stmt);
8519 /* Begin a __transaction_atomic or __transaction_relaxed statement.
8520 If PCOMPOUND is non-null, this is for a function-transaction-block, and we
8521 should create an extra compound stmt. */
8523 tree
8524 begin_transaction_stmt (location_t loc, tree *pcompound, int flags)
8526 tree r;
8528 if (pcompound)
8529 *pcompound = begin_compound_stmt (0);
8531 r = build_stmt (loc, TRANSACTION_EXPR, NULL_TREE);
8533 /* Only add the statement to the function if support enabled. */
8534 if (flag_tm)
8535 add_stmt (r);
8536 else
8537 error_at (loc, ((flags & TM_STMT_ATTR_RELAXED) != 0
8538 ? G_("%<__transaction_relaxed%> without "
8539 "transactional memory support enabled")
8540 : G_("%<__transaction_atomic%> without "
8541 "transactional memory support enabled")));
8543 TRANSACTION_EXPR_BODY (r) = push_stmt_list ();
8544 TREE_SIDE_EFFECTS (r) = 1;
8545 return r;
8548 /* End a __transaction_atomic or __transaction_relaxed statement.
8549 If COMPOUND_STMT is non-null, this is for a function-transaction-block,
8550 and we should end the compound. If NOEX is non-NULL, we wrap the body in
8551 a MUST_NOT_THROW_EXPR with NOEX as condition. */
8553 void
8554 finish_transaction_stmt (tree stmt, tree compound_stmt, int flags, tree noex)
8556 TRANSACTION_EXPR_BODY (stmt) = pop_stmt_list (TRANSACTION_EXPR_BODY (stmt));
8557 TRANSACTION_EXPR_OUTER (stmt) = (flags & TM_STMT_ATTR_OUTER) != 0;
8558 TRANSACTION_EXPR_RELAXED (stmt) = (flags & TM_STMT_ATTR_RELAXED) != 0;
8559 TRANSACTION_EXPR_IS_STMT (stmt) = 1;
8561 /* noexcept specifications are not allowed for function transactions. */
8562 gcc_assert (!(noex && compound_stmt));
8563 if (noex)
8565 tree body = build_must_not_throw_expr (TRANSACTION_EXPR_BODY (stmt),
8566 noex);
8567 protected_set_expr_location
8568 (body, EXPR_LOCATION (TRANSACTION_EXPR_BODY (stmt)));
8569 TREE_SIDE_EFFECTS (body) = 1;
8570 TRANSACTION_EXPR_BODY (stmt) = body;
8573 if (compound_stmt)
8574 finish_compound_stmt (compound_stmt);
8577 /* Build a __transaction_atomic or __transaction_relaxed expression. If
8578 NOEX is non-NULL, we wrap the body in a MUST_NOT_THROW_EXPR with NOEX as
8579 condition. */
8581 tree
8582 build_transaction_expr (location_t loc, tree expr, int flags, tree noex)
8584 tree ret;
8585 if (noex)
8587 expr = build_must_not_throw_expr (expr, noex);
8588 protected_set_expr_location (expr, loc);
8589 TREE_SIDE_EFFECTS (expr) = 1;
8591 ret = build1 (TRANSACTION_EXPR, TREE_TYPE (expr), expr);
8592 if (flags & TM_STMT_ATTR_RELAXED)
8593 TRANSACTION_EXPR_RELAXED (ret) = 1;
8594 TREE_SIDE_EFFECTS (ret) = 1;
8595 SET_EXPR_LOCATION (ret, loc);
8596 return ret;
8599 void
8600 init_cp_semantics (void)
8604 /* Build a STATIC_ASSERT for a static assertion with the condition
8605 CONDITION and the message text MESSAGE. LOCATION is the location
8606 of the static assertion in the source code. When MEMBER_P, this
8607 static assertion is a member of a class. */
8608 void
8609 finish_static_assert (tree condition, tree message, location_t location,
8610 bool member_p)
8612 tsubst_flags_t complain = tf_warning_or_error;
8614 if (message == NULL_TREE
8615 || message == error_mark_node
8616 || condition == NULL_TREE
8617 || condition == error_mark_node)
8618 return;
8620 if (check_for_bare_parameter_packs (condition))
8621 condition = error_mark_node;
8623 if (type_dependent_expression_p (condition)
8624 || value_dependent_expression_p (condition))
8626 /* We're in a template; build a STATIC_ASSERT and put it in
8627 the right place. */
8628 tree assertion;
8630 assertion = make_node (STATIC_ASSERT);
8631 STATIC_ASSERT_CONDITION (assertion) = condition;
8632 STATIC_ASSERT_MESSAGE (assertion) = message;
8633 STATIC_ASSERT_SOURCE_LOCATION (assertion) = location;
8635 if (member_p)
8636 maybe_add_class_template_decl_list (current_class_type,
8637 assertion,
8638 /*friend_p=*/0);
8639 else
8640 add_stmt (assertion);
8642 return;
8645 /* Fold the expression and convert it to a boolean value. */
8646 condition = perform_implicit_conversion_flags (boolean_type_node, condition,
8647 complain, LOOKUP_NORMAL);
8648 condition = fold_non_dependent_expr (condition);
8650 if (TREE_CODE (condition) == INTEGER_CST && !integer_zerop (condition))
8651 /* Do nothing; the condition is satisfied. */
8653 else
8655 location_t saved_loc = input_location;
8657 input_location = location;
8658 if (TREE_CODE (condition) == INTEGER_CST
8659 && integer_zerop (condition))
8661 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
8662 (TREE_TYPE (TREE_TYPE (message))));
8663 int len = TREE_STRING_LENGTH (message) / sz - 1;
8664 /* Report the error. */
8665 if (len == 0)
8666 error ("static assertion failed");
8667 else
8668 error ("static assertion failed: %s",
8669 TREE_STRING_POINTER (message));
8671 else if (condition && condition != error_mark_node)
8673 error ("non-constant condition for static assertion");
8674 if (require_potential_rvalue_constant_expression (condition))
8675 cxx_constant_value (condition);
8677 input_location = saved_loc;
8681 /* Implements the C++0x decltype keyword. Returns the type of EXPR,
8682 suitable for use as a type-specifier.
8684 ID_EXPRESSION_OR_MEMBER_ACCESS_P is true when EXPR was parsed as an
8685 id-expression or a class member access, FALSE when it was parsed as
8686 a full expression. */
8688 tree
8689 finish_decltype_type (tree expr, bool id_expression_or_member_access_p,
8690 tsubst_flags_t complain)
8692 tree type = NULL_TREE;
8694 if (!expr || error_operand_p (expr))
8695 return error_mark_node;
8697 if (TYPE_P (expr)
8698 || TREE_CODE (expr) == TYPE_DECL
8699 || (TREE_CODE (expr) == BIT_NOT_EXPR
8700 && TYPE_P (TREE_OPERAND (expr, 0))))
8702 if (complain & tf_error)
8703 error ("argument to decltype must be an expression");
8704 return error_mark_node;
8707 /* Depending on the resolution of DR 1172, we may later need to distinguish
8708 instantiation-dependent but not type-dependent expressions so that, say,
8709 A<decltype(sizeof(T))>::U doesn't require 'typename'. */
8710 if (instantiation_dependent_uneval_expression_p (expr))
8712 type = cxx_make_type (DECLTYPE_TYPE);
8713 DECLTYPE_TYPE_EXPR (type) = expr;
8714 DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (type)
8715 = id_expression_or_member_access_p;
8716 SET_TYPE_STRUCTURAL_EQUALITY (type);
8718 return type;
8721 /* The type denoted by decltype(e) is defined as follows: */
8723 expr = resolve_nondeduced_context (expr, complain);
8725 if (invalid_nonstatic_memfn_p (input_location, expr, complain))
8726 return error_mark_node;
8728 if (type_unknown_p (expr))
8730 if (complain & tf_error)
8731 error ("decltype cannot resolve address of overloaded function");
8732 return error_mark_node;
8735 /* To get the size of a static data member declared as an array of
8736 unknown bound, we need to instantiate it. */
8737 if (VAR_P (expr)
8738 && VAR_HAD_UNKNOWN_BOUND (expr)
8739 && DECL_TEMPLATE_INSTANTIATION (expr))
8740 instantiate_decl (expr, /*defer_ok*/true, /*expl_inst_mem*/false);
8742 if (id_expression_or_member_access_p)
8744 /* If e is an id-expression or a class member access (5.2.5
8745 [expr.ref]), decltype(e) is defined as the type of the entity
8746 named by e. If there is no such entity, or e names a set of
8747 overloaded functions, the program is ill-formed. */
8748 if (identifier_p (expr))
8749 expr = lookup_name (expr);
8751 if (INDIRECT_REF_P (expr))
8752 /* This can happen when the expression is, e.g., "a.b". Just
8753 look at the underlying operand. */
8754 expr = TREE_OPERAND (expr, 0);
8756 if (TREE_CODE (expr) == OFFSET_REF
8757 || TREE_CODE (expr) == MEMBER_REF
8758 || TREE_CODE (expr) == SCOPE_REF)
8759 /* We're only interested in the field itself. If it is a
8760 BASELINK, we will need to see through it in the next
8761 step. */
8762 expr = TREE_OPERAND (expr, 1);
8764 if (BASELINK_P (expr))
8765 /* See through BASELINK nodes to the underlying function. */
8766 expr = BASELINK_FUNCTIONS (expr);
8768 /* decltype of a decomposition name drops references in the tuple case
8769 (unlike decltype of a normal variable) and keeps cv-qualifiers from
8770 the containing object in the other cases (unlike decltype of a member
8771 access expression). */
8772 if (DECL_DECOMPOSITION_P (expr))
8774 if (DECL_HAS_VALUE_EXPR_P (expr))
8775 /* Expr is an array or struct subobject proxy, handle
8776 bit-fields properly. */
8777 return unlowered_expr_type (expr);
8778 else
8779 /* Expr is a reference variable for the tuple case. */
8780 return lookup_decomp_type (expr);
8783 switch (TREE_CODE (expr))
8785 case FIELD_DECL:
8786 if (DECL_BIT_FIELD_TYPE (expr))
8788 type = DECL_BIT_FIELD_TYPE (expr);
8789 break;
8791 /* Fall through for fields that aren't bitfields. */
8792 gcc_fallthrough ();
8794 case FUNCTION_DECL:
8795 case VAR_DECL:
8796 case CONST_DECL:
8797 case PARM_DECL:
8798 case RESULT_DECL:
8799 case TEMPLATE_PARM_INDEX:
8800 expr = mark_type_use (expr);
8801 type = TREE_TYPE (expr);
8802 break;
8804 case ERROR_MARK:
8805 type = error_mark_node;
8806 break;
8808 case COMPONENT_REF:
8809 case COMPOUND_EXPR:
8810 mark_type_use (expr);
8811 type = is_bitfield_expr_with_lowered_type (expr);
8812 if (!type)
8813 type = TREE_TYPE (TREE_OPERAND (expr, 1));
8814 break;
8816 case BIT_FIELD_REF:
8817 gcc_unreachable ();
8819 case INTEGER_CST:
8820 case PTRMEM_CST:
8821 /* We can get here when the id-expression refers to an
8822 enumerator or non-type template parameter. */
8823 type = TREE_TYPE (expr);
8824 break;
8826 default:
8827 /* Handle instantiated template non-type arguments. */
8828 type = TREE_TYPE (expr);
8829 break;
8832 else
8834 /* Within a lambda-expression:
8836 Every occurrence of decltype((x)) where x is a possibly
8837 parenthesized id-expression that names an entity of
8838 automatic storage duration is treated as if x were
8839 transformed into an access to a corresponding data member
8840 of the closure type that would have been declared if x
8841 were a use of the denoted entity. */
8842 if (outer_automatic_var_p (expr)
8843 && current_function_decl
8844 && LAMBDA_FUNCTION_P (current_function_decl))
8845 type = capture_decltype (expr);
8846 else if (error_operand_p (expr))
8847 type = error_mark_node;
8848 else if (expr == current_class_ptr)
8849 /* If the expression is just "this", we want the
8850 cv-unqualified pointer for the "this" type. */
8851 type = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
8852 else
8854 /* Otherwise, where T is the type of e, if e is an lvalue,
8855 decltype(e) is defined as T&; if an xvalue, T&&; otherwise, T. */
8856 cp_lvalue_kind clk = lvalue_kind (expr);
8857 type = unlowered_expr_type (expr);
8858 gcc_assert (TREE_CODE (type) != REFERENCE_TYPE);
8860 /* For vector types, pick a non-opaque variant. */
8861 if (VECTOR_TYPE_P (type))
8862 type = strip_typedefs (type);
8864 if (clk != clk_none && !(clk & clk_class))
8865 type = cp_build_reference_type (type, (clk & clk_rvalueref));
8869 return type;
8872 /* Called from trait_expr_value to evaluate either __has_nothrow_assign or
8873 __has_nothrow_copy, depending on assign_p. Returns true iff all
8874 the copy {ctor,assign} fns are nothrow. */
8876 static bool
8877 classtype_has_nothrow_assign_or_copy_p (tree type, bool assign_p)
8879 tree fns = NULL_TREE;
8881 if (assign_p || TYPE_HAS_COPY_CTOR (type))
8882 fns = get_class_binding (type, assign_p ? assign_op_identifier
8883 : ctor_identifier);
8885 bool saw_copy = false;
8886 for (ovl_iterator iter (fns); iter; ++iter)
8888 tree fn = *iter;
8890 if (copy_fn_p (fn) > 0)
8892 saw_copy = true;
8893 maybe_instantiate_noexcept (fn);
8894 if (!TYPE_NOTHROW_P (TREE_TYPE (fn)))
8895 return false;
8899 return saw_copy;
8902 /* Actually evaluates the trait. */
8904 static bool
8905 trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
8907 enum tree_code type_code1;
8908 tree t;
8910 type_code1 = TREE_CODE (type1);
8912 switch (kind)
8914 case CPTK_HAS_NOTHROW_ASSIGN:
8915 type1 = strip_array_types (type1);
8916 return (!CP_TYPE_CONST_P (type1) && type_code1 != REFERENCE_TYPE
8917 && (trait_expr_value (CPTK_HAS_TRIVIAL_ASSIGN, type1, type2)
8918 || (CLASS_TYPE_P (type1)
8919 && classtype_has_nothrow_assign_or_copy_p (type1,
8920 true))));
8922 case CPTK_HAS_TRIVIAL_ASSIGN:
8923 /* ??? The standard seems to be missing the "or array of such a class
8924 type" wording for this trait. */
8925 type1 = strip_array_types (type1);
8926 return (!CP_TYPE_CONST_P (type1) && type_code1 != REFERENCE_TYPE
8927 && (trivial_type_p (type1)
8928 || (CLASS_TYPE_P (type1)
8929 && TYPE_HAS_TRIVIAL_COPY_ASSIGN (type1))));
8931 case CPTK_HAS_NOTHROW_CONSTRUCTOR:
8932 type1 = strip_array_types (type1);
8933 return (trait_expr_value (CPTK_HAS_TRIVIAL_CONSTRUCTOR, type1, type2)
8934 || (CLASS_TYPE_P (type1)
8935 && (t = locate_ctor (type1))
8936 && (maybe_instantiate_noexcept (t),
8937 TYPE_NOTHROW_P (TREE_TYPE (t)))));
8939 case CPTK_HAS_TRIVIAL_CONSTRUCTOR:
8940 type1 = strip_array_types (type1);
8941 return (trivial_type_p (type1)
8942 || (CLASS_TYPE_P (type1) && TYPE_HAS_TRIVIAL_DFLT (type1)));
8944 case CPTK_HAS_NOTHROW_COPY:
8945 type1 = strip_array_types (type1);
8946 return (trait_expr_value (CPTK_HAS_TRIVIAL_COPY, type1, type2)
8947 || (CLASS_TYPE_P (type1)
8948 && classtype_has_nothrow_assign_or_copy_p (type1, false)));
8950 case CPTK_HAS_TRIVIAL_COPY:
8951 /* ??? The standard seems to be missing the "or array of such a class
8952 type" wording for this trait. */
8953 type1 = strip_array_types (type1);
8954 return (trivial_type_p (type1) || type_code1 == REFERENCE_TYPE
8955 || (CLASS_TYPE_P (type1) && TYPE_HAS_TRIVIAL_COPY_CTOR (type1)));
8957 case CPTK_HAS_TRIVIAL_DESTRUCTOR:
8958 type1 = strip_array_types (type1);
8959 return (trivial_type_p (type1) || type_code1 == REFERENCE_TYPE
8960 || (CLASS_TYPE_P (type1)
8961 && TYPE_HAS_TRIVIAL_DESTRUCTOR (type1)));
8963 case CPTK_HAS_VIRTUAL_DESTRUCTOR:
8964 return type_has_virtual_destructor (type1);
8966 case CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS:
8967 return type_has_unique_obj_representations (type1);
8969 case CPTK_IS_ABSTRACT:
8970 return ABSTRACT_CLASS_TYPE_P (type1);
8972 case CPTK_IS_AGGREGATE:
8973 return CP_AGGREGATE_TYPE_P (type1);
8975 case CPTK_IS_BASE_OF:
8976 return (NON_UNION_CLASS_TYPE_P (type1) && NON_UNION_CLASS_TYPE_P (type2)
8977 && (same_type_ignoring_top_level_qualifiers_p (type1, type2)
8978 || DERIVED_FROM_P (type1, type2)));
8980 case CPTK_IS_CLASS:
8981 return NON_UNION_CLASS_TYPE_P (type1);
8983 case CPTK_IS_EMPTY:
8984 return NON_UNION_CLASS_TYPE_P (type1) && CLASSTYPE_EMPTY_P (type1);
8986 case CPTK_IS_ENUM:
8987 return type_code1 == ENUMERAL_TYPE;
8989 case CPTK_IS_FINAL:
8990 return CLASS_TYPE_P (type1) && CLASSTYPE_FINAL (type1);
8992 case CPTK_IS_LITERAL_TYPE:
8993 return literal_type_p (type1);
8995 case CPTK_IS_POD:
8996 return pod_type_p (type1);
8998 case CPTK_IS_POLYMORPHIC:
8999 return CLASS_TYPE_P (type1) && TYPE_POLYMORPHIC_P (type1);
9001 case CPTK_IS_SAME_AS:
9002 return same_type_p (type1, type2);
9004 case CPTK_IS_STD_LAYOUT:
9005 return std_layout_type_p (type1);
9007 case CPTK_IS_TRIVIAL:
9008 return trivial_type_p (type1);
9010 case CPTK_IS_TRIVIALLY_ASSIGNABLE:
9011 return is_trivially_xible (MODIFY_EXPR, type1, type2);
9013 case CPTK_IS_TRIVIALLY_CONSTRUCTIBLE:
9014 return is_trivially_xible (INIT_EXPR, type1, type2);
9016 case CPTK_IS_TRIVIALLY_COPYABLE:
9017 return trivially_copyable_p (type1);
9019 case CPTK_IS_UNION:
9020 return type_code1 == UNION_TYPE;
9022 case CPTK_IS_ASSIGNABLE:
9023 return is_xible (MODIFY_EXPR, type1, type2);
9025 case CPTK_IS_CONSTRUCTIBLE:
9026 return is_xible (INIT_EXPR, type1, type2);
9028 default:
9029 gcc_unreachable ();
9030 return false;
9034 /* If TYPE is an array of unknown bound, or (possibly cv-qualified)
9035 void, or a complete type, returns true, otherwise false. */
9037 static bool
9038 check_trait_type (tree type)
9040 if (type == NULL_TREE)
9041 return true;
9043 if (TREE_CODE (type) == TREE_LIST)
9044 return (check_trait_type (TREE_VALUE (type))
9045 && check_trait_type (TREE_CHAIN (type)));
9047 if (TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type)
9048 && COMPLETE_TYPE_P (TREE_TYPE (type)))
9049 return true;
9051 if (VOID_TYPE_P (type))
9052 return true;
9054 return !!complete_type_or_else (strip_array_types (type), NULL_TREE);
9057 /* Process a trait expression. */
9059 tree
9060 finish_trait_expr (cp_trait_kind kind, tree type1, tree type2)
9062 if (type1 == error_mark_node
9063 || type2 == error_mark_node)
9064 return error_mark_node;
9066 if (processing_template_decl)
9068 tree trait_expr = make_node (TRAIT_EXPR);
9069 TREE_TYPE (trait_expr) = boolean_type_node;
9070 TRAIT_EXPR_TYPE1 (trait_expr) = type1;
9071 TRAIT_EXPR_TYPE2 (trait_expr) = type2;
9072 TRAIT_EXPR_KIND (trait_expr) = kind;
9073 return trait_expr;
9076 switch (kind)
9078 case CPTK_HAS_NOTHROW_ASSIGN:
9079 case CPTK_HAS_TRIVIAL_ASSIGN:
9080 case CPTK_HAS_NOTHROW_CONSTRUCTOR:
9081 case CPTK_HAS_TRIVIAL_CONSTRUCTOR:
9082 case CPTK_HAS_NOTHROW_COPY:
9083 case CPTK_HAS_TRIVIAL_COPY:
9084 case CPTK_HAS_TRIVIAL_DESTRUCTOR:
9085 case CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS:
9086 case CPTK_HAS_VIRTUAL_DESTRUCTOR:
9087 case CPTK_IS_ABSTRACT:
9088 case CPTK_IS_AGGREGATE:
9089 case CPTK_IS_EMPTY:
9090 case CPTK_IS_FINAL:
9091 case CPTK_IS_LITERAL_TYPE:
9092 case CPTK_IS_POD:
9093 case CPTK_IS_POLYMORPHIC:
9094 case CPTK_IS_STD_LAYOUT:
9095 case CPTK_IS_TRIVIAL:
9096 case CPTK_IS_TRIVIALLY_COPYABLE:
9097 if (!check_trait_type (type1))
9098 return error_mark_node;
9099 break;
9101 case CPTK_IS_ASSIGNABLE:
9102 case CPTK_IS_CONSTRUCTIBLE:
9103 break;
9105 case CPTK_IS_TRIVIALLY_ASSIGNABLE:
9106 case CPTK_IS_TRIVIALLY_CONSTRUCTIBLE:
9107 if (!check_trait_type (type1)
9108 || !check_trait_type (type2))
9109 return error_mark_node;
9110 break;
9112 case CPTK_IS_BASE_OF:
9113 if (NON_UNION_CLASS_TYPE_P (type1) && NON_UNION_CLASS_TYPE_P (type2)
9114 && !same_type_ignoring_top_level_qualifiers_p (type1, type2)
9115 && !complete_type_or_else (type2, NULL_TREE))
9116 /* We already issued an error. */
9117 return error_mark_node;
9118 break;
9120 case CPTK_IS_CLASS:
9121 case CPTK_IS_ENUM:
9122 case CPTK_IS_UNION:
9123 case CPTK_IS_SAME_AS:
9124 break;
9126 default:
9127 gcc_unreachable ();
9130 return (trait_expr_value (kind, type1, type2)
9131 ? boolean_true_node : boolean_false_node);
9134 /* Do-nothing variants of functions to handle pragma FLOAT_CONST_DECIMAL64,
9135 which is ignored for C++. */
9137 void
9138 set_float_const_decimal64 (void)
9142 void
9143 clear_float_const_decimal64 (void)
9147 bool
9148 float_const_decimal64_p (void)
9150 return 0;
9154 /* Return true if T designates the implied `this' parameter. */
9156 bool
9157 is_this_parameter (tree t)
9159 if (!DECL_P (t) || DECL_NAME (t) != this_identifier)
9160 return false;
9161 gcc_assert (TREE_CODE (t) == PARM_DECL || is_capture_proxy (t)
9162 || (cp_binding_oracle && TREE_CODE (t) == VAR_DECL));
9163 return true;
9166 /* Insert the deduced return type for an auto function. */
9168 void
9169 apply_deduced_return_type (tree fco, tree return_type)
9171 tree result;
9173 if (return_type == error_mark_node)
9174 return;
9176 if (DECL_CONV_FN_P (fco))
9177 DECL_NAME (fco) = make_conv_op_name (return_type);
9179 TREE_TYPE (fco) = change_return_type (return_type, TREE_TYPE (fco));
9181 result = DECL_RESULT (fco);
9182 if (result == NULL_TREE)
9183 return;
9184 if (TREE_TYPE (result) == return_type)
9185 return;
9187 if (!processing_template_decl && !VOID_TYPE_P (return_type)
9188 && !complete_type_or_else (return_type, NULL_TREE))
9189 return;
9191 /* We already have a DECL_RESULT from start_preparsed_function.
9192 Now we need to redo the work it and allocate_struct_function
9193 did to reflect the new type. */
9194 gcc_assert (current_function_decl == fco);
9195 result = build_decl (input_location, RESULT_DECL, NULL_TREE,
9196 TYPE_MAIN_VARIANT (return_type));
9197 DECL_ARTIFICIAL (result) = 1;
9198 DECL_IGNORED_P (result) = 1;
9199 cp_apply_type_quals_to_decl (cp_type_quals (return_type),
9200 result);
9202 DECL_RESULT (fco) = result;
9204 if (!processing_template_decl)
9206 bool aggr = aggregate_value_p (result, fco);
9207 #ifdef PCC_STATIC_STRUCT_RETURN
9208 cfun->returns_pcc_struct = aggr;
9209 #endif
9210 cfun->returns_struct = aggr;
9215 /* DECL is a local variable or parameter from the surrounding scope of a
9216 lambda-expression. Returns the decltype for a use of the capture field
9217 for DECL even if it hasn't been captured yet. */
9219 static tree
9220 capture_decltype (tree decl)
9222 tree lam = CLASSTYPE_LAMBDA_EXPR (DECL_CONTEXT (current_function_decl));
9223 /* FIXME do lookup instead of list walk? */
9224 tree cap = value_member (decl, LAMBDA_EXPR_CAPTURE_LIST (lam));
9225 tree type;
9227 if (cap)
9228 type = TREE_TYPE (TREE_PURPOSE (cap));
9229 else
9230 switch (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lam))
9232 case CPLD_NONE:
9233 error ("%qD is not captured", decl);
9234 return error_mark_node;
9236 case CPLD_COPY:
9237 type = TREE_TYPE (decl);
9238 if (TREE_CODE (type) == REFERENCE_TYPE
9239 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
9240 type = TREE_TYPE (type);
9241 break;
9243 case CPLD_REFERENCE:
9244 type = TREE_TYPE (decl);
9245 if (TREE_CODE (type) != REFERENCE_TYPE)
9246 type = build_reference_type (TREE_TYPE (decl));
9247 break;
9249 default:
9250 gcc_unreachable ();
9253 if (TREE_CODE (type) != REFERENCE_TYPE)
9255 if (!LAMBDA_EXPR_MUTABLE_P (lam))
9256 type = cp_build_qualified_type (type, (cp_type_quals (type)
9257 |TYPE_QUAL_CONST));
9258 type = build_reference_type (type);
9260 return type;
9263 /* Build a unary fold expression of EXPR over OP. If IS_RIGHT is true,
9264 this is a right unary fold. Otherwise it is a left unary fold. */
9266 static tree
9267 finish_unary_fold_expr (tree expr, int op, tree_code dir)
9269 // Build a pack expansion (assuming expr has pack type).
9270 if (!uses_parameter_packs (expr))
9272 error_at (location_of (expr), "operand of fold expression has no "
9273 "unexpanded parameter packs");
9274 return error_mark_node;
9276 tree pack = make_pack_expansion (expr);
9278 // Build the fold expression.
9279 tree code = build_int_cstu (integer_type_node, abs (op));
9280 tree fold = build_min_nt_loc (UNKNOWN_LOCATION, dir, code, pack);
9281 FOLD_EXPR_MODIFY_P (fold) = (op < 0);
9282 return fold;
9285 tree
9286 finish_left_unary_fold_expr (tree expr, int op)
9288 return finish_unary_fold_expr (expr, op, UNARY_LEFT_FOLD_EXPR);
9291 tree
9292 finish_right_unary_fold_expr (tree expr, int op)
9294 return finish_unary_fold_expr (expr, op, UNARY_RIGHT_FOLD_EXPR);
9297 /* Build a binary fold expression over EXPR1 and EXPR2. The
9298 associativity of the fold is determined by EXPR1 and EXPR2 (whichever
9299 has an unexpanded parameter pack). */
9301 tree
9302 finish_binary_fold_expr (tree pack, tree init, int op, tree_code dir)
9304 pack = make_pack_expansion (pack);
9305 tree code = build_int_cstu (integer_type_node, abs (op));
9306 tree fold = build_min_nt_loc (UNKNOWN_LOCATION, dir, code, pack, init);
9307 FOLD_EXPR_MODIFY_P (fold) = (op < 0);
9308 return fold;
9311 tree
9312 finish_binary_fold_expr (tree expr1, tree expr2, int op)
9314 // Determine which expr has an unexpanded parameter pack and
9315 // set the pack and initial term.
9316 bool pack1 = uses_parameter_packs (expr1);
9317 bool pack2 = uses_parameter_packs (expr2);
9318 if (pack1 && !pack2)
9319 return finish_binary_fold_expr (expr1, expr2, op, BINARY_RIGHT_FOLD_EXPR);
9320 else if (pack2 && !pack1)
9321 return finish_binary_fold_expr (expr2, expr1, op, BINARY_LEFT_FOLD_EXPR);
9322 else
9324 if (pack1)
9325 error ("both arguments in binary fold have unexpanded parameter packs");
9326 else
9327 error ("no unexpanded parameter packs in binary fold");
9329 return error_mark_node;
9332 /* Finish __builtin_launder (arg). */
9334 tree
9335 finish_builtin_launder (location_t loc, tree arg, tsubst_flags_t complain)
9337 tree orig_arg = arg;
9338 if (!type_dependent_expression_p (arg))
9339 arg = decay_conversion (arg, complain);
9340 if (error_operand_p (arg))
9341 return error_mark_node;
9342 if (!type_dependent_expression_p (arg)
9343 && TREE_CODE (TREE_TYPE (arg)) != POINTER_TYPE)
9345 error_at (loc, "non-pointer argument to %<__builtin_launder%>");
9346 return error_mark_node;
9348 if (processing_template_decl)
9349 arg = orig_arg;
9350 return build_call_expr_internal_loc (loc, IFN_LAUNDER,
9351 TREE_TYPE (arg), 1, arg);
9354 #include "gt-cp-semantics.h"