PR c/81544 - attribute noreturn and warn_unused_result on the same function accepted
[official-gcc.git] / gcc / cp / semantics.c
blobc6726324ae6205a155a3c49bfac81209f494c464
1 /* Perform the semantic phase of parsing, i.e., the process of
2 building tree structure, checking semantic consistency, and
3 building RTL. These routines are used both during actual parsing
4 and during the instantiation of template functions.
6 Copyright (C) 1998-2017 Free Software Foundation, Inc.
7 Written by Mark Mitchell (mmitchell@usa.net) based on code found
8 formerly in parse.y and pt.c.
10 This file is part of GCC.
12 GCC is free software; you can redistribute it and/or modify it
13 under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 3, or (at your option)
15 any later version.
17 GCC is distributed in the hope that it will be useful, but
18 WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with GCC; see the file COPYING3. If not see
24 <http://www.gnu.org/licenses/>. */
26 #include "config.h"
27 #include "system.h"
28 #include "coretypes.h"
29 #include "target.h"
30 #include "bitmap.h"
31 #include "cp-tree.h"
32 #include "stringpool.h"
33 #include "cgraph.h"
34 #include "stmt.h"
35 #include "varasm.h"
36 #include "stor-layout.h"
37 #include "c-family/c-objc.h"
38 #include "tree-inline.h"
39 #include "intl.h"
40 #include "tree-iterator.h"
41 #include "omp-general.h"
42 #include "convert.h"
43 #include "stringpool.h"
44 #include "attribs.h"
45 #include "gomp-constants.h"
46 #include "predict.h"
48 /* There routines provide a modular interface to perform many parsing
49 operations. They may therefore be used during actual parsing, or
50 during template instantiation, which may be regarded as a
51 degenerate form of parsing. */
53 static tree maybe_convert_cond (tree);
54 static tree finalize_nrv_r (tree *, int *, void *);
55 static tree capture_decltype (tree);
57 /* Used for OpenMP non-static data member privatization. */
59 static hash_map<tree, tree> *omp_private_member_map;
60 static vec<tree> omp_private_member_vec;
61 static bool omp_private_member_ignore_next;
64 /* Deferred Access Checking Overview
65 ---------------------------------
67 Most C++ expressions and declarations require access checking
68 to be performed during parsing. However, in several cases,
69 this has to be treated differently.
71 For member declarations, access checking has to be deferred
72 until more information about the declaration is known. For
73 example:
75 class A {
76 typedef int X;
77 public:
78 X f();
81 A::X A::f();
82 A::X g();
84 When we are parsing the function return type `A::X', we don't
85 really know if this is allowed until we parse the function name.
87 Furthermore, some contexts require that access checking is
88 never performed at all. These include class heads, and template
89 instantiations.
91 Typical use of access checking functions is described here:
93 1. When we enter a context that requires certain access checking
94 mode, the function `push_deferring_access_checks' is called with
95 DEFERRING argument specifying the desired mode. Access checking
96 may be performed immediately (dk_no_deferred), deferred
97 (dk_deferred), or not performed (dk_no_check).
99 2. When a declaration such as a type, or a variable, is encountered,
100 the function `perform_or_defer_access_check' is called. It
101 maintains a vector of all deferred checks.
103 3. The global `current_class_type' or `current_function_decl' is then
104 setup by the parser. `enforce_access' relies on these information
105 to check access.
107 4. Upon exiting the context mentioned in step 1,
108 `perform_deferred_access_checks' is called to check all declaration
109 stored in the vector. `pop_deferring_access_checks' is then
110 called to restore the previous access checking mode.
112 In case of parsing error, we simply call `pop_deferring_access_checks'
113 without `perform_deferred_access_checks'. */
115 struct GTY(()) deferred_access {
116 /* A vector representing name-lookups for which we have deferred
117 checking access controls. We cannot check the accessibility of
118 names used in a decl-specifier-seq until we know what is being
119 declared because code like:
121 class A {
122 class B {};
123 B* f();
126 A::B* A::f() { return 0; }
128 is valid, even though `A::B' is not generally accessible. */
129 vec<deferred_access_check, va_gc> * GTY(()) deferred_access_checks;
131 /* The current mode of access checks. */
132 enum deferring_kind deferring_access_checks_kind;
136 /* Data for deferred access checking. */
137 static GTY(()) vec<deferred_access, va_gc> *deferred_access_stack;
138 static GTY(()) unsigned deferred_access_no_check;
140 /* Save the current deferred access states and start deferred
141 access checking iff DEFER_P is true. */
143 void
144 push_deferring_access_checks (deferring_kind deferring)
146 /* For context like template instantiation, access checking
147 disabling applies to all nested context. */
148 if (deferred_access_no_check || deferring == dk_no_check)
149 deferred_access_no_check++;
150 else
152 deferred_access e = {NULL, deferring};
153 vec_safe_push (deferred_access_stack, e);
157 /* Save the current deferred access states and start deferred access
158 checking, continuing the set of deferred checks in CHECKS. */
160 void
161 reopen_deferring_access_checks (vec<deferred_access_check, va_gc> * checks)
163 push_deferring_access_checks (dk_deferred);
164 if (!deferred_access_no_check)
165 deferred_access_stack->last().deferred_access_checks = checks;
168 /* Resume deferring access checks again after we stopped doing
169 this previously. */
171 void
172 resume_deferring_access_checks (void)
174 if (!deferred_access_no_check)
175 deferred_access_stack->last().deferring_access_checks_kind = dk_deferred;
178 /* Stop deferring access checks. */
180 void
181 stop_deferring_access_checks (void)
183 if (!deferred_access_no_check)
184 deferred_access_stack->last().deferring_access_checks_kind = dk_no_deferred;
187 /* Discard the current deferred access checks and restore the
188 previous states. */
190 void
191 pop_deferring_access_checks (void)
193 if (deferred_access_no_check)
194 deferred_access_no_check--;
195 else
196 deferred_access_stack->pop ();
199 /* Returns a TREE_LIST representing the deferred checks.
200 The TREE_PURPOSE of each node is the type through which the
201 access occurred; the TREE_VALUE is the declaration named.
204 vec<deferred_access_check, va_gc> *
205 get_deferred_access_checks (void)
207 if (deferred_access_no_check)
208 return NULL;
209 else
210 return (deferred_access_stack->last().deferred_access_checks);
213 /* Take current deferred checks and combine with the
214 previous states if we also defer checks previously.
215 Otherwise perform checks now. */
217 void
218 pop_to_parent_deferring_access_checks (void)
220 if (deferred_access_no_check)
221 deferred_access_no_check--;
222 else
224 vec<deferred_access_check, va_gc> *checks;
225 deferred_access *ptr;
227 checks = (deferred_access_stack->last ().deferred_access_checks);
229 deferred_access_stack->pop ();
230 ptr = &deferred_access_stack->last ();
231 if (ptr->deferring_access_checks_kind == dk_no_deferred)
233 /* Check access. */
234 perform_access_checks (checks, tf_warning_or_error);
236 else
238 /* Merge with parent. */
239 int i, j;
240 deferred_access_check *chk, *probe;
242 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
244 FOR_EACH_VEC_SAFE_ELT (ptr->deferred_access_checks, j, probe)
246 if (probe->binfo == chk->binfo &&
247 probe->decl == chk->decl &&
248 probe->diag_decl == chk->diag_decl)
249 goto found;
251 /* Insert into parent's checks. */
252 vec_safe_push (ptr->deferred_access_checks, *chk);
253 found:;
259 /* Perform the access checks in CHECKS. The TREE_PURPOSE of each node
260 is the BINFO indicating the qualifying scope used to access the
261 DECL node stored in the TREE_VALUE of the node. If CHECKS is empty
262 or we aren't in SFINAE context or all the checks succeed return TRUE,
263 otherwise FALSE. */
265 bool
266 perform_access_checks (vec<deferred_access_check, va_gc> *checks,
267 tsubst_flags_t complain)
269 int i;
270 deferred_access_check *chk;
271 location_t loc = input_location;
272 bool ok = true;
274 if (!checks)
275 return true;
277 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
279 input_location = chk->loc;
280 ok &= enforce_access (chk->binfo, chk->decl, chk->diag_decl, complain);
283 input_location = loc;
284 return (complain & tf_error) ? true : ok;
287 /* Perform the deferred access checks.
289 After performing the checks, we still have to keep the list
290 `deferred_access_stack->deferred_access_checks' since we may want
291 to check access for them again later in a different context.
292 For example:
294 class A {
295 typedef int X;
296 static X a;
298 A::X A::a, x; // No error for `A::a', error for `x'
300 We have to perform deferred access of `A::X', first with `A::a',
301 next with `x'. Return value like perform_access_checks above. */
303 bool
304 perform_deferred_access_checks (tsubst_flags_t complain)
306 return perform_access_checks (get_deferred_access_checks (), complain);
309 /* Defer checking the accessibility of DECL, when looked up in
310 BINFO. DIAG_DECL is the declaration to use to print diagnostics.
311 Return value like perform_access_checks above.
312 If non-NULL, report failures to AFI. */
314 bool
315 perform_or_defer_access_check (tree binfo, tree decl, tree diag_decl,
316 tsubst_flags_t complain,
317 access_failure_info *afi)
319 int i;
320 deferred_access *ptr;
321 deferred_access_check *chk;
324 /* Exit if we are in a context that no access checking is performed.
326 if (deferred_access_no_check)
327 return true;
329 gcc_assert (TREE_CODE (binfo) == TREE_BINFO);
331 ptr = &deferred_access_stack->last ();
333 /* If we are not supposed to defer access checks, just check now. */
334 if (ptr->deferring_access_checks_kind == dk_no_deferred)
336 bool ok = enforce_access (binfo, decl, diag_decl, complain, afi);
337 return (complain & tf_error) ? true : ok;
340 /* See if we are already going to perform this check. */
341 FOR_EACH_VEC_SAFE_ELT (ptr->deferred_access_checks, i, chk)
343 if (chk->decl == decl && chk->binfo == binfo &&
344 chk->diag_decl == diag_decl)
346 return true;
349 /* If not, record the check. */
350 deferred_access_check new_access = {binfo, decl, diag_decl, input_location};
351 vec_safe_push (ptr->deferred_access_checks, new_access);
353 return true;
356 /* Returns nonzero if the current statement is a full expression,
357 i.e. temporaries created during that statement should be destroyed
358 at the end of the statement. */
361 stmts_are_full_exprs_p (void)
363 return current_stmt_tree ()->stmts_are_full_exprs_p;
366 /* T is a statement. Add it to the statement-tree. This is the C++
367 version. The C/ObjC frontends have a slightly different version of
368 this function. */
370 tree
371 add_stmt (tree t)
373 enum tree_code code = TREE_CODE (t);
375 if (EXPR_P (t) && code != LABEL_EXPR)
377 if (!EXPR_HAS_LOCATION (t))
378 SET_EXPR_LOCATION (t, input_location);
380 /* When we expand a statement-tree, we must know whether or not the
381 statements are full-expressions. We record that fact here. */
382 STMT_IS_FULL_EXPR_P (t) = stmts_are_full_exprs_p ();
385 if (code == LABEL_EXPR || code == CASE_LABEL_EXPR)
386 STATEMENT_LIST_HAS_LABEL (cur_stmt_list) = 1;
388 /* Add T to the statement-tree. Non-side-effect statements need to be
389 recorded during statement expressions. */
390 gcc_checking_assert (!stmt_list_stack->is_empty ());
391 append_to_statement_list_force (t, &cur_stmt_list);
393 return t;
396 /* Returns the stmt_tree to which statements are currently being added. */
398 stmt_tree
399 current_stmt_tree (void)
401 return (cfun
402 ? &cfun->language->base.x_stmt_tree
403 : &scope_chain->x_stmt_tree);
406 /* If statements are full expressions, wrap STMT in a CLEANUP_POINT_EXPR. */
408 static tree
409 maybe_cleanup_point_expr (tree expr)
411 if (!processing_template_decl && stmts_are_full_exprs_p ())
412 expr = fold_build_cleanup_point_expr (TREE_TYPE (expr), expr);
413 return expr;
416 /* Like maybe_cleanup_point_expr except have the type of the new expression be
417 void so we don't need to create a temporary variable to hold the inner
418 expression. The reason why we do this is because the original type might be
419 an aggregate and we cannot create a temporary variable for that type. */
421 tree
422 maybe_cleanup_point_expr_void (tree expr)
424 if (!processing_template_decl && stmts_are_full_exprs_p ())
425 expr = fold_build_cleanup_point_expr (void_type_node, expr);
426 return expr;
431 /* Create a declaration statement for the declaration given by the DECL. */
433 void
434 add_decl_expr (tree decl)
436 tree r = build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl);
437 if (DECL_INITIAL (decl)
438 || (DECL_SIZE (decl) && TREE_SIDE_EFFECTS (DECL_SIZE (decl))))
439 r = maybe_cleanup_point_expr_void (r);
440 add_stmt (r);
443 /* Finish a scope. */
445 tree
446 do_poplevel (tree stmt_list)
448 tree block = NULL;
450 if (stmts_are_full_exprs_p ())
451 block = poplevel (kept_level_p (), 1, 0);
453 stmt_list = pop_stmt_list (stmt_list);
455 if (!processing_template_decl)
457 stmt_list = c_build_bind_expr (input_location, block, stmt_list);
458 /* ??? See c_end_compound_stmt re statement expressions. */
461 return stmt_list;
464 /* Begin a new scope. */
466 static tree
467 do_pushlevel (scope_kind sk)
469 tree ret = push_stmt_list ();
470 if (stmts_are_full_exprs_p ())
471 begin_scope (sk, NULL);
472 return ret;
475 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
476 when the current scope is exited. EH_ONLY is true when this is not
477 meant to apply to normal control flow transfer. */
479 void
480 push_cleanup (tree decl, tree cleanup, bool eh_only)
482 tree stmt = build_stmt (input_location, CLEANUP_STMT, NULL, cleanup, decl);
483 CLEANUP_EH_ONLY (stmt) = eh_only;
484 add_stmt (stmt);
485 CLEANUP_BODY (stmt) = push_stmt_list ();
488 /* Simple infinite loop tracking for -Wreturn-type. We keep a stack of all
489 the current loops, represented by 'NULL_TREE' if we've seen a possible
490 exit, and 'error_mark_node' if not. This is currently used only to
491 suppress the warning about a function with no return statements, and
492 therefore we don't bother noting returns as possible exits. We also
493 don't bother with gotos. */
495 static void
496 begin_maybe_infinite_loop (tree cond)
498 /* Only track this while parsing a function, not during instantiation. */
499 if (!cfun || (DECL_TEMPLATE_INSTANTIATION (current_function_decl)
500 && !processing_template_decl))
501 return;
502 bool maybe_infinite = true;
503 if (cond)
505 cond = fold_non_dependent_expr (cond);
506 maybe_infinite = integer_nonzerop (cond);
508 vec_safe_push (cp_function_chain->infinite_loops,
509 maybe_infinite ? error_mark_node : NULL_TREE);
513 /* A break is a possible exit for the current loop. */
515 void
516 break_maybe_infinite_loop (void)
518 if (!cfun)
519 return;
520 cp_function_chain->infinite_loops->last() = NULL_TREE;
523 /* If we reach the end of the loop without seeing a possible exit, we have
524 an infinite loop. */
526 static void
527 end_maybe_infinite_loop (tree cond)
529 if (!cfun || (DECL_TEMPLATE_INSTANTIATION (current_function_decl)
530 && !processing_template_decl))
531 return;
532 tree current = cp_function_chain->infinite_loops->pop();
533 if (current != NULL_TREE)
535 cond = fold_non_dependent_expr (cond);
536 if (integer_nonzerop (cond))
537 current_function_infinite_loop = 1;
542 /* Begin a conditional that might contain a declaration. When generating
543 normal code, we want the declaration to appear before the statement
544 containing the conditional. When generating template code, we want the
545 conditional to be rendered as the raw DECL_EXPR. */
547 static void
548 begin_cond (tree *cond_p)
550 if (processing_template_decl)
551 *cond_p = push_stmt_list ();
554 /* Finish such a conditional. */
556 static void
557 finish_cond (tree *cond_p, tree expr)
559 if (processing_template_decl)
561 tree cond = pop_stmt_list (*cond_p);
563 if (expr == NULL_TREE)
564 /* Empty condition in 'for'. */
565 gcc_assert (empty_expr_stmt_p (cond));
566 else if (check_for_bare_parameter_packs (expr))
567 expr = error_mark_node;
568 else if (!empty_expr_stmt_p (cond))
569 expr = build2 (COMPOUND_EXPR, TREE_TYPE (expr), cond, expr);
571 *cond_p = expr;
574 /* If *COND_P specifies a conditional with a declaration, transform the
575 loop such that
576 while (A x = 42) { }
577 for (; A x = 42;) { }
578 becomes
579 while (true) { A x = 42; if (!x) break; }
580 for (;;) { A x = 42; if (!x) break; }
581 The statement list for BODY will be empty if the conditional did
582 not declare anything. */
584 static void
585 simplify_loop_decl_cond (tree *cond_p, tree body)
587 tree cond, if_stmt;
589 if (!TREE_SIDE_EFFECTS (body))
590 return;
592 cond = *cond_p;
593 *cond_p = boolean_true_node;
595 if_stmt = begin_if_stmt ();
596 cond = cp_build_unary_op (TRUTH_NOT_EXPR, cond, false, tf_warning_or_error);
597 finish_if_stmt_cond (cond, if_stmt);
598 finish_break_stmt ();
599 finish_then_clause (if_stmt);
600 finish_if_stmt (if_stmt);
603 /* Finish a goto-statement. */
605 tree
606 finish_goto_stmt (tree destination)
608 if (identifier_p (destination))
609 destination = lookup_label (destination);
611 /* We warn about unused labels with -Wunused. That means we have to
612 mark the used labels as used. */
613 if (TREE_CODE (destination) == LABEL_DECL)
614 TREE_USED (destination) = 1;
615 else
617 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 && require_constant_expression (cond)
735 && !value_dependent_expression_p (cond))
737 cond = instantiate_non_dependent_expr (cond);
738 cond = cxx_constant_value (cond, NULL_TREE);
740 finish_cond (&IF_COND (if_stmt), cond);
741 add_stmt (if_stmt);
742 THEN_CLAUSE (if_stmt) = push_stmt_list ();
743 return cond;
746 /* Finish the then-clause of an if-statement, which may be given by
747 IF_STMT. */
749 tree
750 finish_then_clause (tree if_stmt)
752 THEN_CLAUSE (if_stmt) = pop_stmt_list (THEN_CLAUSE (if_stmt));
753 return if_stmt;
756 /* Begin the else-clause of an if-statement. */
758 void
759 begin_else_clause (tree if_stmt)
761 ELSE_CLAUSE (if_stmt) = push_stmt_list ();
764 /* Finish the else-clause of an if-statement, which may be given by
765 IF_STMT. */
767 void
768 finish_else_clause (tree if_stmt)
770 ELSE_CLAUSE (if_stmt) = pop_stmt_list (ELSE_CLAUSE (if_stmt));
773 /* Finish an if-statement. */
775 void
776 finish_if_stmt (tree if_stmt)
778 tree scope = IF_SCOPE (if_stmt);
779 IF_SCOPE (if_stmt) = NULL;
780 add_stmt (do_poplevel (scope));
783 /* Begin a while-statement. Returns a newly created WHILE_STMT if
784 appropriate. */
786 tree
787 begin_while_stmt (void)
789 tree r;
790 r = build_stmt (input_location, WHILE_STMT, NULL_TREE, NULL_TREE);
791 add_stmt (r);
792 WHILE_BODY (r) = do_pushlevel (sk_block);
793 begin_cond (&WHILE_COND (r));
794 return r;
797 /* Process the COND of a while-statement, which may be given by
798 WHILE_STMT. */
800 void
801 finish_while_stmt_cond (tree cond, tree while_stmt, bool ivdep)
803 cond = maybe_convert_cond (cond);
804 finish_cond (&WHILE_COND (while_stmt), cond);
805 begin_maybe_infinite_loop (cond);
806 if (ivdep && cond != error_mark_node)
807 WHILE_COND (while_stmt) = build3 (ANNOTATE_EXPR,
808 TREE_TYPE (WHILE_COND (while_stmt)),
809 WHILE_COND (while_stmt),
810 build_int_cst (integer_type_node,
811 annot_expr_ivdep_kind),
812 integer_zero_node);
813 simplify_loop_decl_cond (&WHILE_COND (while_stmt), WHILE_BODY (while_stmt));
816 /* Finish a while-statement, which may be given by WHILE_STMT. */
818 void
819 finish_while_stmt (tree while_stmt)
821 end_maybe_infinite_loop (boolean_true_node);
822 WHILE_BODY (while_stmt) = do_poplevel (WHILE_BODY (while_stmt));
825 /* Begin a do-statement. Returns a newly created DO_STMT if
826 appropriate. */
828 tree
829 begin_do_stmt (void)
831 tree r = build_stmt (input_location, DO_STMT, NULL_TREE, NULL_TREE);
832 begin_maybe_infinite_loop (boolean_true_node);
833 add_stmt (r);
834 DO_BODY (r) = push_stmt_list ();
835 return r;
838 /* Finish the body of a do-statement, which may be given by DO_STMT. */
840 void
841 finish_do_body (tree do_stmt)
843 tree body = DO_BODY (do_stmt) = pop_stmt_list (DO_BODY (do_stmt));
845 if (TREE_CODE (body) == STATEMENT_LIST && STATEMENT_LIST_TAIL (body))
846 body = STATEMENT_LIST_TAIL (body)->stmt;
848 if (IS_EMPTY_STMT (body))
849 warning (OPT_Wempty_body,
850 "suggest explicit braces around empty body in %<do%> statement");
853 /* Finish a do-statement, which may be given by DO_STMT, and whose
854 COND is as indicated. */
856 void
857 finish_do_stmt (tree cond, tree do_stmt, bool ivdep)
859 cond = maybe_convert_cond (cond);
860 end_maybe_infinite_loop (cond);
861 if (ivdep && cond != error_mark_node)
862 cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
863 build_int_cst (integer_type_node, annot_expr_ivdep_kind),
864 integer_zero_node);
865 DO_COND (do_stmt) = cond;
868 /* Finish a return-statement. The EXPRESSION returned, if any, is as
869 indicated. */
871 tree
872 finish_return_stmt (tree expr)
874 tree r;
875 bool no_warning;
877 expr = check_return_expr (expr, &no_warning);
879 if (error_operand_p (expr)
880 || (flag_openmp && !check_omp_return ()))
882 /* Suppress -Wreturn-type for this function. */
883 if (warn_return_type)
884 TREE_NO_WARNING (current_function_decl) = true;
885 return error_mark_node;
888 if (!processing_template_decl)
890 if (warn_sequence_point)
891 verify_sequence_points (expr);
893 if (DECL_DESTRUCTOR_P (current_function_decl)
894 || (DECL_CONSTRUCTOR_P (current_function_decl)
895 && targetm.cxx.cdtor_returns_this ()))
897 /* Similarly, all destructors must run destructors for
898 base-classes before returning. So, all returns in a
899 destructor get sent to the DTOR_LABEL; finish_function emits
900 code to return a value there. */
901 return finish_goto_stmt (cdtor_label);
905 r = build_stmt (input_location, RETURN_EXPR, expr);
906 TREE_NO_WARNING (r) |= no_warning;
907 r = maybe_cleanup_point_expr_void (r);
908 r = add_stmt (r);
910 return r;
913 /* Begin the scope of a for-statement or a range-for-statement.
914 Both the returned trees are to be used in a call to
915 begin_for_stmt or begin_range_for_stmt. */
917 tree
918 begin_for_scope (tree *init)
920 tree scope = NULL_TREE;
921 if (flag_new_for_scope > 0)
922 scope = do_pushlevel (sk_for);
924 if (processing_template_decl)
925 *init = push_stmt_list ();
926 else
927 *init = NULL_TREE;
929 return scope;
932 /* Begin a for-statement. Returns a new FOR_STMT.
933 SCOPE and INIT should be the return of begin_for_scope,
934 or both NULL_TREE */
936 tree
937 begin_for_stmt (tree scope, tree init)
939 tree r;
941 r = build_stmt (input_location, FOR_STMT, NULL_TREE, NULL_TREE,
942 NULL_TREE, NULL_TREE, NULL_TREE);
944 if (scope == NULL_TREE)
946 gcc_assert (!init || !(flag_new_for_scope > 0));
947 if (!init)
948 scope = begin_for_scope (&init);
950 FOR_INIT_STMT (r) = init;
951 FOR_SCOPE (r) = scope;
953 return r;
956 /* Finish the init-statement of a for-statement, which may be
957 given by FOR_STMT. */
959 void
960 finish_init_stmt (tree for_stmt)
962 if (processing_template_decl)
963 FOR_INIT_STMT (for_stmt) = pop_stmt_list (FOR_INIT_STMT (for_stmt));
964 add_stmt (for_stmt);
965 FOR_BODY (for_stmt) = do_pushlevel (sk_block);
966 begin_cond (&FOR_COND (for_stmt));
969 /* Finish the COND of a for-statement, which may be given by
970 FOR_STMT. */
972 void
973 finish_for_cond (tree cond, tree for_stmt, bool ivdep)
975 cond = maybe_convert_cond (cond);
976 finish_cond (&FOR_COND (for_stmt), cond);
977 begin_maybe_infinite_loop (cond);
978 if (ivdep && cond != error_mark_node)
979 FOR_COND (for_stmt) = build3 (ANNOTATE_EXPR,
980 TREE_TYPE (FOR_COND (for_stmt)),
981 FOR_COND (for_stmt),
982 build_int_cst (integer_type_node,
983 annot_expr_ivdep_kind),
984 integer_zero_node);
985 simplify_loop_decl_cond (&FOR_COND (for_stmt), FOR_BODY (for_stmt));
988 /* Finish the increment-EXPRESSION in a for-statement, which may be
989 given by FOR_STMT. */
991 void
992 finish_for_expr (tree expr, tree for_stmt)
994 if (!expr)
995 return;
996 /* If EXPR is an overloaded function, issue an error; there is no
997 context available to use to perform overload resolution. */
998 if (type_unknown_p (expr))
1000 cxx_incomplete_type_error (expr, TREE_TYPE (expr));
1001 expr = error_mark_node;
1003 if (!processing_template_decl)
1005 if (warn_sequence_point)
1006 verify_sequence_points (expr);
1007 expr = convert_to_void (expr, ICV_THIRD_IN_FOR,
1008 tf_warning_or_error);
1010 else if (!type_dependent_expression_p (expr))
1011 convert_to_void (build_non_dependent_expr (expr), ICV_THIRD_IN_FOR,
1012 tf_warning_or_error);
1013 expr = maybe_cleanup_point_expr_void (expr);
1014 if (check_for_bare_parameter_packs (expr))
1015 expr = error_mark_node;
1016 FOR_EXPR (for_stmt) = expr;
1019 /* Finish the body of a for-statement, which may be given by
1020 FOR_STMT. The increment-EXPR for the loop must be
1021 provided.
1022 It can also finish RANGE_FOR_STMT. */
1024 void
1025 finish_for_stmt (tree for_stmt)
1027 end_maybe_infinite_loop (boolean_true_node);
1029 if (TREE_CODE (for_stmt) == RANGE_FOR_STMT)
1030 RANGE_FOR_BODY (for_stmt) = do_poplevel (RANGE_FOR_BODY (for_stmt));
1031 else
1032 FOR_BODY (for_stmt) = do_poplevel (FOR_BODY (for_stmt));
1034 /* Pop the scope for the body of the loop. */
1035 if (flag_new_for_scope > 0)
1037 tree scope;
1038 tree *scope_ptr = (TREE_CODE (for_stmt) == RANGE_FOR_STMT
1039 ? &RANGE_FOR_SCOPE (for_stmt)
1040 : &FOR_SCOPE (for_stmt));
1041 scope = *scope_ptr;
1042 *scope_ptr = NULL;
1043 add_stmt (do_poplevel (scope));
1047 /* Begin a range-for-statement. Returns a new RANGE_FOR_STMT.
1048 SCOPE and INIT should be the return of begin_for_scope,
1049 or both NULL_TREE .
1050 To finish it call finish_for_stmt(). */
1052 tree
1053 begin_range_for_stmt (tree scope, tree init)
1055 tree r;
1057 begin_maybe_infinite_loop (boolean_false_node);
1059 r = build_stmt (input_location, RANGE_FOR_STMT,
1060 NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE);
1062 if (scope == NULL_TREE)
1064 gcc_assert (!init || !(flag_new_for_scope > 0));
1065 if (!init)
1066 scope = begin_for_scope (&init);
1069 /* RANGE_FOR_STMTs do not use nor save the init tree, so we
1070 pop it now. */
1071 if (init)
1072 pop_stmt_list (init);
1073 RANGE_FOR_SCOPE (r) = scope;
1075 return r;
1078 /* Finish the head of a range-based for statement, which may
1079 be given by RANGE_FOR_STMT. DECL must be the declaration
1080 and EXPR must be the loop expression. */
1082 void
1083 finish_range_for_decl (tree range_for_stmt, tree decl, tree expr)
1085 RANGE_FOR_DECL (range_for_stmt) = decl;
1086 RANGE_FOR_EXPR (range_for_stmt) = expr;
1087 add_stmt (range_for_stmt);
1088 RANGE_FOR_BODY (range_for_stmt) = do_pushlevel (sk_block);
1091 /* Finish a break-statement. */
1093 tree
1094 finish_break_stmt (void)
1096 /* In switch statements break is sometimes stylistically used after
1097 a return statement. This can lead to spurious warnings about
1098 control reaching the end of a non-void function when it is
1099 inlined. Note that we are calling block_may_fallthru with
1100 language specific tree nodes; this works because
1101 block_may_fallthru returns true when given something it does not
1102 understand. */
1103 if (!block_may_fallthru (cur_stmt_list))
1104 return void_node;
1105 note_break_stmt ();
1106 return add_stmt (build_stmt (input_location, BREAK_STMT));
1109 /* Finish a continue-statement. */
1111 tree
1112 finish_continue_stmt (void)
1114 return add_stmt (build_stmt (input_location, CONTINUE_STMT));
1117 /* Begin a switch-statement. Returns a new SWITCH_STMT if
1118 appropriate. */
1120 tree
1121 begin_switch_stmt (void)
1123 tree r, scope;
1125 scope = do_pushlevel (sk_cond);
1126 r = build_stmt (input_location, SWITCH_STMT, NULL_TREE, NULL_TREE, NULL_TREE, scope);
1128 begin_cond (&SWITCH_STMT_COND (r));
1130 return r;
1133 /* Finish the cond of a switch-statement. */
1135 void
1136 finish_switch_cond (tree cond, tree switch_stmt)
1138 tree orig_type = NULL;
1140 if (!processing_template_decl)
1142 /* Convert the condition to an integer or enumeration type. */
1143 cond = build_expr_type_conversion (WANT_INT | WANT_ENUM, cond, true);
1144 if (cond == NULL_TREE)
1146 error ("switch quantity not an integer");
1147 cond = error_mark_node;
1149 /* We want unlowered type here to handle enum bit-fields. */
1150 orig_type = unlowered_expr_type (cond);
1151 if (TREE_CODE (orig_type) != ENUMERAL_TYPE)
1152 orig_type = TREE_TYPE (cond);
1153 if (cond != error_mark_node)
1155 /* [stmt.switch]
1157 Integral promotions are performed. */
1158 cond = perform_integral_promotions (cond);
1159 cond = maybe_cleanup_point_expr (cond);
1162 if (check_for_bare_parameter_packs (cond))
1163 cond = error_mark_node;
1164 else if (!processing_template_decl && warn_sequence_point)
1165 verify_sequence_points (cond);
1167 finish_cond (&SWITCH_STMT_COND (switch_stmt), cond);
1168 SWITCH_STMT_TYPE (switch_stmt) = orig_type;
1169 add_stmt (switch_stmt);
1170 push_switch (switch_stmt);
1171 SWITCH_STMT_BODY (switch_stmt) = push_stmt_list ();
1174 /* Finish the body of a switch-statement, which may be given by
1175 SWITCH_STMT. The COND to switch on is indicated. */
1177 void
1178 finish_switch_stmt (tree switch_stmt)
1180 tree scope;
1182 SWITCH_STMT_BODY (switch_stmt) =
1183 pop_stmt_list (SWITCH_STMT_BODY (switch_stmt));
1184 pop_switch ();
1186 scope = SWITCH_STMT_SCOPE (switch_stmt);
1187 SWITCH_STMT_SCOPE (switch_stmt) = NULL;
1188 add_stmt (do_poplevel (scope));
1191 /* Begin a try-block. Returns a newly-created TRY_BLOCK if
1192 appropriate. */
1194 tree
1195 begin_try_block (void)
1197 tree r = build_stmt (input_location, TRY_BLOCK, NULL_TREE, NULL_TREE);
1198 add_stmt (r);
1199 TRY_STMTS (r) = push_stmt_list ();
1200 return r;
1203 /* Likewise, for a function-try-block. The block returned in
1204 *COMPOUND_STMT is an artificial outer scope, containing the
1205 function-try-block. */
1207 tree
1208 begin_function_try_block (tree *compound_stmt)
1210 tree r;
1211 /* This outer scope does not exist in the C++ standard, but we need
1212 a place to put __FUNCTION__ and similar variables. */
1213 *compound_stmt = begin_compound_stmt (0);
1214 r = begin_try_block ();
1215 FN_TRY_BLOCK_P (r) = 1;
1216 return r;
1219 /* Finish a try-block, which may be given by TRY_BLOCK. */
1221 void
1222 finish_try_block (tree try_block)
1224 TRY_STMTS (try_block) = pop_stmt_list (TRY_STMTS (try_block));
1225 TRY_HANDLERS (try_block) = push_stmt_list ();
1228 /* Finish the body of a cleanup try-block, which may be given by
1229 TRY_BLOCK. */
1231 void
1232 finish_cleanup_try_block (tree try_block)
1234 TRY_STMTS (try_block) = pop_stmt_list (TRY_STMTS (try_block));
1237 /* Finish an implicitly generated try-block, with a cleanup is given
1238 by CLEANUP. */
1240 void
1241 finish_cleanup (tree cleanup, tree try_block)
1243 TRY_HANDLERS (try_block) = cleanup;
1244 CLEANUP_P (try_block) = 1;
1247 /* Likewise, for a function-try-block. */
1249 void
1250 finish_function_try_block (tree try_block)
1252 finish_try_block (try_block);
1253 /* FIXME : something queer about CTOR_INITIALIZER somehow following
1254 the try block, but moving it inside. */
1255 in_function_try_handler = 1;
1258 /* Finish a handler-sequence for a try-block, which may be given by
1259 TRY_BLOCK. */
1261 void
1262 finish_handler_sequence (tree try_block)
1264 TRY_HANDLERS (try_block) = pop_stmt_list (TRY_HANDLERS (try_block));
1265 check_handlers (TRY_HANDLERS (try_block));
1268 /* Finish the handler-seq for a function-try-block, given by
1269 TRY_BLOCK. COMPOUND_STMT is the outer block created by
1270 begin_function_try_block. */
1272 void
1273 finish_function_handler_sequence (tree try_block, tree compound_stmt)
1275 in_function_try_handler = 0;
1276 finish_handler_sequence (try_block);
1277 finish_compound_stmt (compound_stmt);
1280 /* Begin a handler. Returns a HANDLER if appropriate. */
1282 tree
1283 begin_handler (void)
1285 tree r;
1287 r = build_stmt (input_location, HANDLER, NULL_TREE, NULL_TREE);
1288 add_stmt (r);
1290 /* Create a binding level for the eh_info and the exception object
1291 cleanup. */
1292 HANDLER_BODY (r) = do_pushlevel (sk_catch);
1294 return r;
1297 /* Finish the handler-parameters for a handler, which may be given by
1298 HANDLER. DECL is the declaration for the catch parameter, or NULL
1299 if this is a `catch (...)' clause. */
1301 void
1302 finish_handler_parms (tree decl, tree handler)
1304 tree type = NULL_TREE;
1305 if (processing_template_decl)
1307 if (decl)
1309 decl = pushdecl (decl);
1310 decl = push_template_decl (decl);
1311 HANDLER_PARMS (handler) = decl;
1312 type = TREE_TYPE (decl);
1315 else
1317 type = expand_start_catch_block (decl);
1318 if (warn_catch_value
1319 && type != NULL_TREE
1320 && type != error_mark_node
1321 && TREE_CODE (TREE_TYPE (decl)) != REFERENCE_TYPE)
1323 tree orig_type = TREE_TYPE (decl);
1324 if (CLASS_TYPE_P (orig_type))
1326 if (TYPE_POLYMORPHIC_P (orig_type))
1327 warning (OPT_Wcatch_value_,
1328 "catching polymorphic type %q#T by value", orig_type);
1329 else if (warn_catch_value > 1)
1330 warning (OPT_Wcatch_value_,
1331 "catching type %q#T by value", orig_type);
1333 else if (warn_catch_value > 2)
1334 warning (OPT_Wcatch_value_,
1335 "catching non-reference type %q#T", orig_type);
1338 HANDLER_TYPE (handler) = type;
1341 /* Finish a handler, which may be given by HANDLER. The BLOCKs are
1342 the return value from the matching call to finish_handler_parms. */
1344 void
1345 finish_handler (tree handler)
1347 if (!processing_template_decl)
1348 expand_end_catch_block ();
1349 HANDLER_BODY (handler) = do_poplevel (HANDLER_BODY (handler));
1352 /* Begin a compound statement. FLAGS contains some bits that control the
1353 behavior and context. If BCS_NO_SCOPE is set, the compound statement
1354 does not define a scope. If BCS_FN_BODY is set, this is the outermost
1355 block of a function. If BCS_TRY_BLOCK is set, this is the block
1356 created on behalf of a TRY statement. Returns a token to be passed to
1357 finish_compound_stmt. */
1359 tree
1360 begin_compound_stmt (unsigned int flags)
1362 tree r;
1364 if (flags & BCS_NO_SCOPE)
1366 r = push_stmt_list ();
1367 STATEMENT_LIST_NO_SCOPE (r) = 1;
1369 /* Normally, we try hard to keep the BLOCK for a statement-expression.
1370 But, if it's a statement-expression with a scopeless block, there's
1371 nothing to keep, and we don't want to accidentally keep a block
1372 *inside* the scopeless block. */
1373 keep_next_level (false);
1375 else
1377 scope_kind sk = sk_block;
1378 if (flags & BCS_TRY_BLOCK)
1379 sk = sk_try;
1380 else if (flags & BCS_TRANSACTION)
1381 sk = sk_transaction;
1382 r = do_pushlevel (sk);
1385 /* When processing a template, we need to remember where the braces were,
1386 so that we can set up identical scopes when instantiating the template
1387 later. BIND_EXPR is a handy candidate for this.
1388 Note that do_poplevel won't create a BIND_EXPR itself here (and thus
1389 result in nested BIND_EXPRs), since we don't build BLOCK nodes when
1390 processing templates. */
1391 if (processing_template_decl)
1393 r = build3 (BIND_EXPR, NULL, NULL, r, NULL);
1394 BIND_EXPR_TRY_BLOCK (r) = (flags & BCS_TRY_BLOCK) != 0;
1395 BIND_EXPR_BODY_BLOCK (r) = (flags & BCS_FN_BODY) != 0;
1396 TREE_SIDE_EFFECTS (r) = 1;
1399 return r;
1402 /* Finish a compound-statement, which is given by STMT. */
1404 void
1405 finish_compound_stmt (tree stmt)
1407 if (TREE_CODE (stmt) == BIND_EXPR)
1409 tree body = do_poplevel (BIND_EXPR_BODY (stmt));
1410 /* If the STATEMENT_LIST is empty and this BIND_EXPR isn't special,
1411 discard the BIND_EXPR so it can be merged with the containing
1412 STATEMENT_LIST. */
1413 if (TREE_CODE (body) == STATEMENT_LIST
1414 && STATEMENT_LIST_HEAD (body) == NULL
1415 && !BIND_EXPR_BODY_BLOCK (stmt)
1416 && !BIND_EXPR_TRY_BLOCK (stmt))
1417 stmt = body;
1418 else
1419 BIND_EXPR_BODY (stmt) = body;
1421 else if (STATEMENT_LIST_NO_SCOPE (stmt))
1422 stmt = pop_stmt_list (stmt);
1423 else
1425 /* Destroy any ObjC "super" receivers that may have been
1426 created. */
1427 objc_clear_super_receiver ();
1429 stmt = do_poplevel (stmt);
1432 /* ??? See c_end_compound_stmt wrt statement expressions. */
1433 add_stmt (stmt);
1436 /* Finish an asm-statement, whose components are a STRING, some
1437 OUTPUT_OPERANDS, some INPUT_OPERANDS, some CLOBBERS and some
1438 LABELS. Also note whether the asm-statement should be
1439 considered volatile. */
1441 tree
1442 finish_asm_stmt (int volatile_p, tree string, tree output_operands,
1443 tree input_operands, tree clobbers, tree labels)
1445 tree r;
1446 tree t;
1447 int ninputs = list_length (input_operands);
1448 int noutputs = list_length (output_operands);
1450 if (!processing_template_decl)
1452 const char *constraint;
1453 const char **oconstraints;
1454 bool allows_mem, allows_reg, is_inout;
1455 tree operand;
1456 int i;
1458 oconstraints = XALLOCAVEC (const char *, noutputs);
1460 string = resolve_asm_operand_names (string, output_operands,
1461 input_operands, labels);
1463 for (i = 0, t = output_operands; t; t = TREE_CHAIN (t), ++i)
1465 operand = TREE_VALUE (t);
1467 /* ??? Really, this should not be here. Users should be using a
1468 proper lvalue, dammit. But there's a long history of using
1469 casts in the output operands. In cases like longlong.h, this
1470 becomes a primitive form of typechecking -- if the cast can be
1471 removed, then the output operand had a type of the proper width;
1472 otherwise we'll get an error. Gross, but ... */
1473 STRIP_NOPS (operand);
1475 operand = mark_lvalue_use (operand);
1477 if (!lvalue_or_else (operand, lv_asm, tf_warning_or_error))
1478 operand = error_mark_node;
1480 if (operand != error_mark_node
1481 && (TREE_READONLY (operand)
1482 || CP_TYPE_CONST_P (TREE_TYPE (operand))
1483 /* Functions are not modifiable, even though they are
1484 lvalues. */
1485 || TREE_CODE (TREE_TYPE (operand)) == FUNCTION_TYPE
1486 || TREE_CODE (TREE_TYPE (operand)) == METHOD_TYPE
1487 /* If it's an aggregate and any field is const, then it is
1488 effectively const. */
1489 || (CLASS_TYPE_P (TREE_TYPE (operand))
1490 && C_TYPE_FIELDS_READONLY (TREE_TYPE (operand)))))
1491 cxx_readonly_error (operand, lv_asm);
1493 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
1494 oconstraints[i] = constraint;
1496 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
1497 &allows_mem, &allows_reg, &is_inout))
1499 /* If the operand is going to end up in memory,
1500 mark it addressable. */
1501 if (!allows_reg && !cxx_mark_addressable (operand))
1502 operand = error_mark_node;
1504 else
1505 operand = error_mark_node;
1507 TREE_VALUE (t) = operand;
1510 for (i = 0, t = input_operands; t; ++i, t = TREE_CHAIN (t))
1512 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
1513 bool constraint_parsed
1514 = parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
1515 oconstraints, &allows_mem, &allows_reg);
1516 /* If the operand is going to end up in memory, don't call
1517 decay_conversion. */
1518 if (constraint_parsed && !allows_reg && allows_mem)
1519 operand = mark_lvalue_use (TREE_VALUE (t));
1520 else
1521 operand = decay_conversion (TREE_VALUE (t), tf_warning_or_error);
1523 /* If the type of the operand hasn't been determined (e.g.,
1524 because it involves an overloaded function), then issue
1525 an error message. There's no context available to
1526 resolve the overloading. */
1527 if (TREE_TYPE (operand) == unknown_type_node)
1529 error ("type of asm operand %qE could not be determined",
1530 TREE_VALUE (t));
1531 operand = error_mark_node;
1534 if (constraint_parsed)
1536 /* If the operand is going to end up in memory,
1537 mark it addressable. */
1538 if (!allows_reg && allows_mem)
1540 /* Strip the nops as we allow this case. FIXME, this really
1541 should be rejected or made deprecated. */
1542 STRIP_NOPS (operand);
1543 if (!cxx_mark_addressable (operand))
1544 operand = error_mark_node;
1546 else if (!allows_reg && !allows_mem)
1548 /* If constraint allows neither register nor memory,
1549 try harder to get a constant. */
1550 tree constop = maybe_constant_value (operand);
1551 if (TREE_CONSTANT (constop))
1552 operand = constop;
1555 else
1556 operand = error_mark_node;
1558 TREE_VALUE (t) = operand;
1562 r = build_stmt (input_location, ASM_EXPR, string,
1563 output_operands, input_operands,
1564 clobbers, labels);
1565 ASM_VOLATILE_P (r) = volatile_p || noutputs == 0;
1566 r = maybe_cleanup_point_expr_void (r);
1567 return add_stmt (r);
1570 /* Finish a label with the indicated NAME. Returns the new label. */
1572 tree
1573 finish_label_stmt (tree name)
1575 tree decl = define_label (input_location, name);
1577 if (decl == error_mark_node)
1578 return error_mark_node;
1580 add_stmt (build_stmt (input_location, LABEL_EXPR, decl));
1582 return decl;
1585 /* Finish a series of declarations for local labels. G++ allows users
1586 to declare "local" labels, i.e., labels with scope. This extension
1587 is useful when writing code involving statement-expressions. */
1589 void
1590 finish_label_decl (tree name)
1592 if (!at_function_scope_p ())
1594 error ("__label__ declarations are only allowed in function scopes");
1595 return;
1598 add_decl_expr (declare_local_label (name));
1601 /* When DECL goes out of scope, make sure that CLEANUP is executed. */
1603 void
1604 finish_decl_cleanup (tree decl, tree cleanup)
1606 push_cleanup (decl, cleanup, false);
1609 /* If the current scope exits with an exception, run CLEANUP. */
1611 void
1612 finish_eh_cleanup (tree cleanup)
1614 push_cleanup (NULL, cleanup, true);
1617 /* The MEM_INITS is a list of mem-initializers, in reverse of the
1618 order they were written by the user. Each node is as for
1619 emit_mem_initializers. */
1621 void
1622 finish_mem_initializers (tree mem_inits)
1624 /* Reorder the MEM_INITS so that they are in the order they appeared
1625 in the source program. */
1626 mem_inits = nreverse (mem_inits);
1628 if (processing_template_decl)
1630 tree mem;
1632 for (mem = mem_inits; mem; mem = TREE_CHAIN (mem))
1634 /* If the TREE_PURPOSE is a TYPE_PACK_EXPANSION, skip the
1635 check for bare parameter packs in the TREE_VALUE, because
1636 any parameter packs in the TREE_VALUE have already been
1637 bound as part of the TREE_PURPOSE. See
1638 make_pack_expansion for more information. */
1639 if (TREE_CODE (TREE_PURPOSE (mem)) != TYPE_PACK_EXPANSION
1640 && check_for_bare_parameter_packs (TREE_VALUE (mem)))
1641 TREE_VALUE (mem) = error_mark_node;
1644 add_stmt (build_min_nt_loc (UNKNOWN_LOCATION,
1645 CTOR_INITIALIZER, mem_inits));
1647 else
1648 emit_mem_initializers (mem_inits);
1651 /* Obfuscate EXPR if it looks like an id-expression or member access so
1652 that the call to finish_decltype in do_auto_deduction will give the
1653 right result. */
1655 tree
1656 force_paren_expr (tree expr)
1658 /* This is only needed for decltype(auto) in C++14. */
1659 if (cxx_dialect < cxx14)
1660 return expr;
1662 /* If we're in unevaluated context, we can't be deducing a
1663 return/initializer type, so we don't need to mess with this. */
1664 if (cp_unevaluated_operand)
1665 return expr;
1667 if (!DECL_P (expr) && TREE_CODE (expr) != COMPONENT_REF
1668 && TREE_CODE (expr) != SCOPE_REF)
1669 return expr;
1671 if (TREE_CODE (expr) == COMPONENT_REF
1672 || TREE_CODE (expr) == SCOPE_REF)
1673 REF_PARENTHESIZED_P (expr) = true;
1674 else if (type_dependent_expression_p (expr))
1675 expr = build1 (PAREN_EXPR, TREE_TYPE (expr), expr);
1676 else if (VAR_P (expr) && DECL_HARD_REGISTER (expr))
1677 /* We can't bind a hard register variable to a reference. */;
1678 else
1680 cp_lvalue_kind kind = lvalue_kind (expr);
1681 if ((kind & ~clk_class) != clk_none)
1683 tree type = unlowered_expr_type (expr);
1684 bool rval = !!(kind & clk_rvalueref);
1685 type = cp_build_reference_type (type, rval);
1686 /* This inhibits warnings in, eg, cxx_mark_addressable
1687 (c++/60955). */
1688 warning_sentinel s (extra_warnings);
1689 expr = build_static_cast (type, expr, tf_error);
1690 if (expr != error_mark_node)
1691 REF_PARENTHESIZED_P (expr) = true;
1695 return expr;
1698 /* If T is an id-expression obfuscated by force_paren_expr, undo the
1699 obfuscation and return the underlying id-expression. Otherwise
1700 return T. */
1702 tree
1703 maybe_undo_parenthesized_ref (tree t)
1705 if (cxx_dialect >= cxx14
1706 && INDIRECT_REF_P (t)
1707 && REF_PARENTHESIZED_P (t))
1709 t = TREE_OPERAND (t, 0);
1710 while (TREE_CODE (t) == NON_LVALUE_EXPR
1711 || TREE_CODE (t) == NOP_EXPR)
1712 t = TREE_OPERAND (t, 0);
1714 gcc_assert (TREE_CODE (t) == ADDR_EXPR
1715 || TREE_CODE (t) == STATIC_CAST_EXPR);
1716 t = TREE_OPERAND (t, 0);
1719 return t;
1722 /* Finish a parenthesized expression EXPR. */
1724 cp_expr
1725 finish_parenthesized_expr (cp_expr expr)
1727 if (EXPR_P (expr))
1728 /* This inhibits warnings in c_common_truthvalue_conversion. */
1729 TREE_NO_WARNING (expr) = 1;
1731 if (TREE_CODE (expr) == OFFSET_REF
1732 || TREE_CODE (expr) == SCOPE_REF)
1733 /* [expr.unary.op]/3 The qualified id of a pointer-to-member must not be
1734 enclosed in parentheses. */
1735 PTRMEM_OK_P (expr) = 0;
1737 if (TREE_CODE (expr) == STRING_CST)
1738 PAREN_STRING_LITERAL_P (expr) = 1;
1740 expr = cp_expr (force_paren_expr (expr), expr.get_location ());
1742 return expr;
1745 /* Finish a reference to a non-static data member (DECL) that is not
1746 preceded by `.' or `->'. */
1748 tree
1749 finish_non_static_data_member (tree decl, tree object, tree qualifying_scope)
1751 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
1752 bool try_omp_private = !object && omp_private_member_map;
1753 tree ret;
1755 if (!object)
1757 tree scope = qualifying_scope;
1758 if (scope == NULL_TREE)
1759 scope = context_for_name_lookup (decl);
1760 object = maybe_dummy_object (scope, NULL);
1763 object = maybe_resolve_dummy (object, true);
1764 if (object == error_mark_node)
1765 return error_mark_node;
1767 /* DR 613/850: Can use non-static data members without an associated
1768 object in sizeof/decltype/alignof. */
1769 if (is_dummy_object (object) && cp_unevaluated_operand == 0
1770 && (!processing_template_decl || !current_class_ref))
1772 if (current_function_decl
1773 && DECL_STATIC_FUNCTION_P (current_function_decl))
1774 error ("invalid use of member %qD in static member function", decl);
1775 else
1776 error ("invalid use of non-static data member %qD", decl);
1777 inform (DECL_SOURCE_LOCATION (decl), "declared here");
1779 return error_mark_node;
1782 if (current_class_ptr)
1783 TREE_USED (current_class_ptr) = 1;
1784 if (processing_template_decl && !qualifying_scope)
1786 tree type = TREE_TYPE (decl);
1788 if (TREE_CODE (type) == REFERENCE_TYPE)
1789 /* Quals on the object don't matter. */;
1790 else if (PACK_EXPANSION_P (type))
1791 /* Don't bother trying to represent this. */
1792 type = NULL_TREE;
1793 else
1795 /* Set the cv qualifiers. */
1796 int quals = cp_type_quals (TREE_TYPE (object));
1798 if (DECL_MUTABLE_P (decl))
1799 quals &= ~TYPE_QUAL_CONST;
1801 quals |= cp_type_quals (TREE_TYPE (decl));
1802 type = cp_build_qualified_type (type, quals);
1805 ret = (convert_from_reference
1806 (build_min (COMPONENT_REF, type, object, decl, NULL_TREE)));
1808 /* If PROCESSING_TEMPLATE_DECL is nonzero here, then
1809 QUALIFYING_SCOPE is also non-null. Wrap this in a SCOPE_REF
1810 for now. */
1811 else if (processing_template_decl)
1812 ret = build_qualified_name (TREE_TYPE (decl),
1813 qualifying_scope,
1814 decl,
1815 /*template_p=*/false);
1816 else
1818 tree access_type = TREE_TYPE (object);
1820 perform_or_defer_access_check (TYPE_BINFO (access_type), decl,
1821 decl, tf_warning_or_error);
1823 /* If the data member was named `C::M', convert `*this' to `C'
1824 first. */
1825 if (qualifying_scope)
1827 tree binfo = NULL_TREE;
1828 object = build_scoped_ref (object, qualifying_scope,
1829 &binfo);
1832 ret = build_class_member_access_expr (object, decl,
1833 /*access_path=*/NULL_TREE,
1834 /*preserve_reference=*/false,
1835 tf_warning_or_error);
1837 if (try_omp_private)
1839 tree *v = omp_private_member_map->get (decl);
1840 if (v)
1841 ret = convert_from_reference (*v);
1843 return ret;
1846 /* If we are currently parsing a template and we encountered a typedef
1847 TYPEDEF_DECL that is being accessed though CONTEXT, this function
1848 adds the typedef to a list tied to the current template.
1849 At template instantiation time, that list is walked and access check
1850 performed for each typedef.
1851 LOCATION is the location of the usage point of TYPEDEF_DECL. */
1853 void
1854 add_typedef_to_current_template_for_access_check (tree typedef_decl,
1855 tree context,
1856 location_t location)
1858 tree template_info = NULL;
1859 tree cs = current_scope ();
1861 if (!is_typedef_decl (typedef_decl)
1862 || !context
1863 || !CLASS_TYPE_P (context)
1864 || !cs)
1865 return;
1867 if (CLASS_TYPE_P (cs) || TREE_CODE (cs) == FUNCTION_DECL)
1868 template_info = get_template_info (cs);
1870 if (template_info
1871 && TI_TEMPLATE (template_info)
1872 && !currently_open_class (context))
1873 append_type_to_template_for_access_check (cs, typedef_decl,
1874 context, location);
1877 /* DECL was the declaration to which a qualified-id resolved. Issue
1878 an error message if it is not accessible. If OBJECT_TYPE is
1879 non-NULL, we have just seen `x->' or `x.' and OBJECT_TYPE is the
1880 type of `*x', or `x', respectively. If the DECL was named as
1881 `A::B' then NESTED_NAME_SPECIFIER is `A'. */
1883 void
1884 check_accessibility_of_qualified_id (tree decl,
1885 tree object_type,
1886 tree nested_name_specifier)
1888 tree scope;
1889 tree qualifying_type = NULL_TREE;
1891 /* If we are parsing a template declaration and if decl is a typedef,
1892 add it to a list tied to the template.
1893 At template instantiation time, that list will be walked and
1894 access check performed. */
1895 add_typedef_to_current_template_for_access_check (decl,
1896 nested_name_specifier
1897 ? nested_name_specifier
1898 : DECL_CONTEXT (decl),
1899 input_location);
1901 /* If we're not checking, return immediately. */
1902 if (deferred_access_no_check)
1903 return;
1905 /* Determine the SCOPE of DECL. */
1906 scope = context_for_name_lookup (decl);
1907 /* If the SCOPE is not a type, then DECL is not a member. */
1908 if (!TYPE_P (scope))
1909 return;
1910 /* Compute the scope through which DECL is being accessed. */
1911 if (object_type
1912 /* OBJECT_TYPE might not be a class type; consider:
1914 class A { typedef int I; };
1915 I *p;
1916 p->A::I::~I();
1918 In this case, we will have "A::I" as the DECL, but "I" as the
1919 OBJECT_TYPE. */
1920 && CLASS_TYPE_P (object_type)
1921 && DERIVED_FROM_P (scope, object_type))
1922 /* If we are processing a `->' or `.' expression, use the type of the
1923 left-hand side. */
1924 qualifying_type = object_type;
1925 else if (nested_name_specifier)
1927 /* If the reference is to a non-static member of the
1928 current class, treat it as if it were referenced through
1929 `this'. */
1930 tree ct;
1931 if (DECL_NONSTATIC_MEMBER_P (decl)
1932 && current_class_ptr
1933 && DERIVED_FROM_P (scope, ct = current_nonlambda_class_type ()))
1934 qualifying_type = ct;
1935 /* Otherwise, use the type indicated by the
1936 nested-name-specifier. */
1937 else
1938 qualifying_type = nested_name_specifier;
1940 else
1941 /* Otherwise, the name must be from the current class or one of
1942 its bases. */
1943 qualifying_type = currently_open_derived_class (scope);
1945 if (qualifying_type
1946 /* It is possible for qualifying type to be a TEMPLATE_TYPE_PARM
1947 or similar in a default argument value. */
1948 && CLASS_TYPE_P (qualifying_type)
1949 && !dependent_type_p (qualifying_type))
1950 perform_or_defer_access_check (TYPE_BINFO (qualifying_type), decl,
1951 decl, tf_warning_or_error);
1954 /* EXPR is the result of a qualified-id. The QUALIFYING_CLASS was the
1955 class named to the left of the "::" operator. DONE is true if this
1956 expression is a complete postfix-expression; it is false if this
1957 expression is followed by '->', '[', '(', etc. ADDRESS_P is true
1958 iff this expression is the operand of '&'. TEMPLATE_P is true iff
1959 the qualified-id was of the form "A::template B". TEMPLATE_ARG_P
1960 is true iff this qualified name appears as a template argument. */
1962 tree
1963 finish_qualified_id_expr (tree qualifying_class,
1964 tree expr,
1965 bool done,
1966 bool address_p,
1967 bool template_p,
1968 bool template_arg_p,
1969 tsubst_flags_t complain)
1971 gcc_assert (TYPE_P (qualifying_class));
1973 if (error_operand_p (expr))
1974 return error_mark_node;
1976 if ((DECL_P (expr) || BASELINK_P (expr))
1977 && !mark_used (expr, complain))
1978 return error_mark_node;
1980 if (template_p)
1982 if (TREE_CODE (expr) == UNBOUND_CLASS_TEMPLATE)
1983 /* cp_parser_lookup_name thought we were looking for a type,
1984 but we're actually looking for a declaration. */
1985 expr = build_qualified_name (/*type*/NULL_TREE,
1986 TYPE_CONTEXT (expr),
1987 TYPE_IDENTIFIER (expr),
1988 /*template_p*/true);
1989 else
1990 check_template_keyword (expr);
1993 /* If EXPR occurs as the operand of '&', use special handling that
1994 permits a pointer-to-member. */
1995 if (address_p && done)
1997 if (TREE_CODE (expr) == SCOPE_REF)
1998 expr = TREE_OPERAND (expr, 1);
1999 expr = build_offset_ref (qualifying_class, expr,
2000 /*address_p=*/true, complain);
2001 return expr;
2004 /* No need to check access within an enum. */
2005 if (TREE_CODE (qualifying_class) == ENUMERAL_TYPE)
2006 return expr;
2008 /* Within the scope of a class, turn references to non-static
2009 members into expression of the form "this->...". */
2010 if (template_arg_p)
2011 /* But, within a template argument, we do not want make the
2012 transformation, as there is no "this" pointer. */
2014 else if (TREE_CODE (expr) == FIELD_DECL)
2016 push_deferring_access_checks (dk_no_check);
2017 expr = finish_non_static_data_member (expr, NULL_TREE,
2018 qualifying_class);
2019 pop_deferring_access_checks ();
2021 else if (BASELINK_P (expr))
2023 /* See if any of the functions are non-static members. */
2024 /* If so, the expression may be relative to 'this'. */
2025 if (!shared_member_p (expr)
2026 && current_class_ptr
2027 && DERIVED_FROM_P (qualifying_class,
2028 current_nonlambda_class_type ()))
2029 expr = (build_class_member_access_expr
2030 (maybe_dummy_object (qualifying_class, NULL),
2031 expr,
2032 BASELINK_ACCESS_BINFO (expr),
2033 /*preserve_reference=*/false,
2034 complain));
2035 else if (done)
2036 /* The expression is a qualified name whose address is not
2037 being taken. */
2038 expr = build_offset_ref (qualifying_class, expr, /*address_p=*/false,
2039 complain);
2041 else
2043 /* In a template, return a SCOPE_REF for most qualified-ids
2044 so that we can check access at instantiation time. But if
2045 we're looking at a member of the current instantiation, we
2046 know we have access and building up the SCOPE_REF confuses
2047 non-type template argument handling. */
2048 if (processing_template_decl
2049 && (!currently_open_class (qualifying_class)
2050 || TREE_CODE (expr) == BIT_NOT_EXPR))
2051 expr = build_qualified_name (TREE_TYPE (expr),
2052 qualifying_class, expr,
2053 template_p);
2055 expr = convert_from_reference (expr);
2058 return expr;
2061 /* Begin a statement-expression. The value returned must be passed to
2062 finish_stmt_expr. */
2064 tree
2065 begin_stmt_expr (void)
2067 return push_stmt_list ();
2070 /* Process the final expression of a statement expression. EXPR can be
2071 NULL, if the final expression is empty. Return a STATEMENT_LIST
2072 containing all the statements in the statement-expression, or
2073 ERROR_MARK_NODE if there was an error. */
2075 tree
2076 finish_stmt_expr_expr (tree expr, tree stmt_expr)
2078 if (error_operand_p (expr))
2080 /* The type of the statement-expression is the type of the last
2081 expression. */
2082 TREE_TYPE (stmt_expr) = error_mark_node;
2083 return error_mark_node;
2086 /* If the last statement does not have "void" type, then the value
2087 of the last statement is the value of the entire expression. */
2088 if (expr)
2090 tree type = TREE_TYPE (expr);
2092 if (processing_template_decl)
2094 expr = build_stmt (input_location, EXPR_STMT, expr);
2095 expr = add_stmt (expr);
2096 /* Mark the last statement so that we can recognize it as such at
2097 template-instantiation time. */
2098 EXPR_STMT_STMT_EXPR_RESULT (expr) = 1;
2100 else if (VOID_TYPE_P (type))
2102 /* Just treat this like an ordinary statement. */
2103 expr = finish_expr_stmt (expr);
2105 else
2107 /* It actually has a value we need to deal with. First, force it
2108 to be an rvalue so that we won't need to build up a copy
2109 constructor call later when we try to assign it to something. */
2110 expr = force_rvalue (expr, tf_warning_or_error);
2111 if (error_operand_p (expr))
2112 return error_mark_node;
2114 /* Update for array-to-pointer decay. */
2115 type = TREE_TYPE (expr);
2117 /* Wrap it in a CLEANUP_POINT_EXPR and add it to the list like a
2118 normal statement, but don't convert to void or actually add
2119 the EXPR_STMT. */
2120 if (TREE_CODE (expr) != CLEANUP_POINT_EXPR)
2121 expr = maybe_cleanup_point_expr (expr);
2122 add_stmt (expr);
2125 /* The type of the statement-expression is the type of the last
2126 expression. */
2127 TREE_TYPE (stmt_expr) = type;
2130 return stmt_expr;
2133 /* Finish a statement-expression. EXPR should be the value returned
2134 by the previous begin_stmt_expr. Returns an expression
2135 representing the statement-expression. */
2137 tree
2138 finish_stmt_expr (tree stmt_expr, bool has_no_scope)
2140 tree type;
2141 tree result;
2143 if (error_operand_p (stmt_expr))
2145 pop_stmt_list (stmt_expr);
2146 return error_mark_node;
2149 gcc_assert (TREE_CODE (stmt_expr) == STATEMENT_LIST);
2151 type = TREE_TYPE (stmt_expr);
2152 result = pop_stmt_list (stmt_expr);
2153 TREE_TYPE (result) = type;
2155 if (processing_template_decl)
2157 result = build_min (STMT_EXPR, type, result);
2158 TREE_SIDE_EFFECTS (result) = 1;
2159 STMT_EXPR_NO_SCOPE (result) = has_no_scope;
2161 else if (CLASS_TYPE_P (type))
2163 /* Wrap the statement-expression in a TARGET_EXPR so that the
2164 temporary object created by the final expression is destroyed at
2165 the end of the full-expression containing the
2166 statement-expression. */
2167 result = force_target_expr (type, result, tf_warning_or_error);
2170 return result;
2173 /* Returns the expression which provides the value of STMT_EXPR. */
2175 tree
2176 stmt_expr_value_expr (tree stmt_expr)
2178 tree t = STMT_EXPR_STMT (stmt_expr);
2180 if (TREE_CODE (t) == BIND_EXPR)
2181 t = BIND_EXPR_BODY (t);
2183 if (TREE_CODE (t) == STATEMENT_LIST && STATEMENT_LIST_TAIL (t))
2184 t = STATEMENT_LIST_TAIL (t)->stmt;
2186 if (TREE_CODE (t) == EXPR_STMT)
2187 t = EXPR_STMT_EXPR (t);
2189 return t;
2192 /* Return TRUE iff EXPR_STMT is an empty list of
2193 expression statements. */
2195 bool
2196 empty_expr_stmt_p (tree expr_stmt)
2198 tree body = NULL_TREE;
2200 if (expr_stmt == void_node)
2201 return true;
2203 if (expr_stmt)
2205 if (TREE_CODE (expr_stmt) == EXPR_STMT)
2206 body = EXPR_STMT_EXPR (expr_stmt);
2207 else if (TREE_CODE (expr_stmt) == STATEMENT_LIST)
2208 body = expr_stmt;
2211 if (body)
2213 if (TREE_CODE (body) == STATEMENT_LIST)
2214 return tsi_end_p (tsi_start (body));
2215 else
2216 return empty_expr_stmt_p (body);
2218 return false;
2221 /* Perform Koenig lookup. FN is the postfix-expression representing
2222 the function (or functions) to call; ARGS are the arguments to the
2223 call. Returns the functions to be considered by overload resolution. */
2225 cp_expr
2226 perform_koenig_lookup (cp_expr fn, vec<tree, va_gc> *args,
2227 tsubst_flags_t complain)
2229 tree identifier = NULL_TREE;
2230 tree functions = NULL_TREE;
2231 tree tmpl_args = NULL_TREE;
2232 bool template_id = false;
2233 location_t loc = fn.get_location ();
2235 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
2237 /* Use a separate flag to handle null args. */
2238 template_id = true;
2239 tmpl_args = TREE_OPERAND (fn, 1);
2240 fn = TREE_OPERAND (fn, 0);
2243 /* Find the name of the overloaded function. */
2244 if (identifier_p (fn))
2245 identifier = fn;
2246 else
2248 functions = fn;
2249 identifier = OVL_NAME (functions);
2252 /* A call to a namespace-scope function using an unqualified name.
2254 Do Koenig lookup -- unless any of the arguments are
2255 type-dependent. */
2256 if (!any_type_dependent_arguments_p (args)
2257 && !any_dependent_template_arguments_p (tmpl_args))
2259 fn = lookup_arg_dependent (identifier, functions, args);
2260 if (!fn)
2262 /* The unqualified name could not be resolved. */
2263 if (complain & tf_error)
2264 fn = unqualified_fn_lookup_error (cp_expr (identifier, loc));
2265 else
2266 fn = identifier;
2270 if (fn && template_id && fn != error_mark_node)
2271 fn = build2 (TEMPLATE_ID_EXPR, unknown_type_node, fn, tmpl_args);
2273 return fn;
2276 /* Generate an expression for `FN (ARGS)'. This may change the
2277 contents of ARGS.
2279 If DISALLOW_VIRTUAL is true, the call to FN will be not generated
2280 as a virtual call, even if FN is virtual. (This flag is set when
2281 encountering an expression where the function name is explicitly
2282 qualified. For example a call to `X::f' never generates a virtual
2283 call.)
2285 Returns code for the call. */
2287 tree
2288 finish_call_expr (tree fn, vec<tree, va_gc> **args, bool disallow_virtual,
2289 bool koenig_p, tsubst_flags_t complain)
2291 tree result;
2292 tree orig_fn;
2293 vec<tree, va_gc> *orig_args = NULL;
2295 if (fn == error_mark_node)
2296 return error_mark_node;
2298 gcc_assert (!TYPE_P (fn));
2300 /* If FN may be a FUNCTION_DECL obfuscated by force_paren_expr, undo
2301 it so that we can tell this is a call to a known function. */
2302 fn = maybe_undo_parenthesized_ref (fn);
2304 orig_fn = fn;
2306 if (processing_template_decl)
2308 /* If FN is a local extern declaration or set thereof, look them up
2309 again at instantiation time. */
2310 if (is_overloaded_fn (fn))
2312 tree ifn = get_first_fn (fn);
2313 if (TREE_CODE (ifn) == FUNCTION_DECL
2314 && DECL_LOCAL_FUNCTION_P (ifn))
2315 orig_fn = DECL_NAME (ifn);
2318 /* If the call expression is dependent, build a CALL_EXPR node
2319 with no type; type_dependent_expression_p recognizes
2320 expressions with no type as being dependent. */
2321 if (type_dependent_expression_p (fn)
2322 || any_type_dependent_arguments_p (*args))
2324 result = build_min_nt_call_vec (orig_fn, *args);
2325 SET_EXPR_LOCATION (result, EXPR_LOC_OR_LOC (fn, input_location));
2326 KOENIG_LOOKUP_P (result) = koenig_p;
2327 if (is_overloaded_fn (fn))
2329 fn = get_fns (fn);
2330 lookup_keep (fn, true);
2333 if (cfun)
2335 bool abnormal = true;
2336 for (lkp_iterator iter (fn); abnormal && iter; ++iter)
2338 tree fndecl = *iter;
2339 if (TREE_CODE (fndecl) != FUNCTION_DECL
2340 || !TREE_THIS_VOLATILE (fndecl))
2341 abnormal = false;
2343 /* FIXME: Stop warning about falling off end of non-void
2344 function. But this is wrong. Even if we only see
2345 no-return fns at this point, we could select a
2346 future-defined return fn during instantiation. Or
2347 vice-versa. */
2348 if (abnormal)
2349 current_function_returns_abnormally = 1;
2351 return result;
2353 orig_args = make_tree_vector_copy (*args);
2354 if (!BASELINK_P (fn)
2355 && TREE_CODE (fn) != PSEUDO_DTOR_EXPR
2356 && TREE_TYPE (fn) != unknown_type_node)
2357 fn = build_non_dependent_expr (fn);
2358 make_args_non_dependent (*args);
2361 if (TREE_CODE (fn) == COMPONENT_REF)
2363 tree member = TREE_OPERAND (fn, 1);
2364 if (BASELINK_P (member))
2366 tree object = TREE_OPERAND (fn, 0);
2367 return build_new_method_call (object, member,
2368 args, NULL_TREE,
2369 (disallow_virtual
2370 ? LOOKUP_NORMAL | LOOKUP_NONVIRTUAL
2371 : LOOKUP_NORMAL),
2372 /*fn_p=*/NULL,
2373 complain);
2377 /* Per 13.3.1.1, '(&f)(...)' is the same as '(f)(...)'. */
2378 if (TREE_CODE (fn) == ADDR_EXPR
2379 && TREE_CODE (TREE_OPERAND (fn, 0)) == OVERLOAD)
2380 fn = TREE_OPERAND (fn, 0);
2382 if (is_overloaded_fn (fn))
2383 fn = baselink_for_fns (fn);
2385 result = NULL_TREE;
2386 if (BASELINK_P (fn))
2388 tree object;
2390 /* A call to a member function. From [over.call.func]:
2392 If the keyword this is in scope and refers to the class of
2393 that member function, or a derived class thereof, then the
2394 function call is transformed into a qualified function call
2395 using (*this) as the postfix-expression to the left of the
2396 . operator.... [Otherwise] a contrived object of type T
2397 becomes the implied object argument.
2399 In this situation:
2401 struct A { void f(); };
2402 struct B : public A {};
2403 struct C : public A { void g() { B::f(); }};
2405 "the class of that member function" refers to `A'. But 11.2
2406 [class.access.base] says that we need to convert 'this' to B* as
2407 part of the access, so we pass 'B' to maybe_dummy_object. */
2409 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (get_first_fn (fn)))
2411 /* A constructor call always uses a dummy object. (This constructor
2412 call which has the form A::A () is actually invalid and we are
2413 going to reject it later in build_new_method_call.) */
2414 object = build_dummy_object (BINFO_TYPE (BASELINK_ACCESS_BINFO (fn)));
2416 else
2417 object = maybe_dummy_object (BINFO_TYPE (BASELINK_ACCESS_BINFO (fn)),
2418 NULL);
2420 result = build_new_method_call (object, fn, args, NULL_TREE,
2421 (disallow_virtual
2422 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
2423 : LOOKUP_NORMAL),
2424 /*fn_p=*/NULL,
2425 complain);
2427 else if (is_overloaded_fn (fn))
2429 /* If the function is an overloaded builtin, resolve it. */
2430 if (TREE_CODE (fn) == FUNCTION_DECL
2431 && (DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL
2432 || DECL_BUILT_IN_CLASS (fn) == BUILT_IN_MD))
2433 result = resolve_overloaded_builtin (input_location, fn, *args);
2435 if (!result)
2437 if (warn_sizeof_pointer_memaccess
2438 && (complain & tf_warning)
2439 && !vec_safe_is_empty (*args)
2440 && !processing_template_decl)
2442 location_t sizeof_arg_loc[3];
2443 tree sizeof_arg[3];
2444 unsigned int i;
2445 for (i = 0; i < 3; i++)
2447 tree t;
2449 sizeof_arg_loc[i] = UNKNOWN_LOCATION;
2450 sizeof_arg[i] = NULL_TREE;
2451 if (i >= (*args)->length ())
2452 continue;
2453 t = (**args)[i];
2454 if (TREE_CODE (t) != SIZEOF_EXPR)
2455 continue;
2456 if (SIZEOF_EXPR_TYPE_P (t))
2457 sizeof_arg[i] = TREE_TYPE (TREE_OPERAND (t, 0));
2458 else
2459 sizeof_arg[i] = TREE_OPERAND (t, 0);
2460 sizeof_arg_loc[i] = EXPR_LOCATION (t);
2462 sizeof_pointer_memaccess_warning
2463 (sizeof_arg_loc, fn, *args,
2464 sizeof_arg, same_type_ignoring_top_level_qualifiers_p);
2467 /* A call to a namespace-scope function. */
2468 result = build_new_function_call (fn, args, complain);
2471 else if (TREE_CODE (fn) == PSEUDO_DTOR_EXPR)
2473 if (!vec_safe_is_empty (*args))
2474 error ("arguments to destructor are not allowed");
2475 /* Mark the pseudo-destructor call as having side-effects so
2476 that we do not issue warnings about its use. */
2477 result = build1 (NOP_EXPR,
2478 void_type_node,
2479 TREE_OPERAND (fn, 0));
2480 TREE_SIDE_EFFECTS (result) = 1;
2482 else if (CLASS_TYPE_P (TREE_TYPE (fn)))
2483 /* If the "function" is really an object of class type, it might
2484 have an overloaded `operator ()'. */
2485 result = build_op_call (fn, args, complain);
2487 if (!result)
2488 /* A call where the function is unknown. */
2489 result = cp_build_function_call_vec (fn, args, complain);
2491 if (processing_template_decl && result != error_mark_node)
2493 if (INDIRECT_REF_P (result))
2494 result = TREE_OPERAND (result, 0);
2495 result = build_call_vec (TREE_TYPE (result), orig_fn, orig_args);
2496 SET_EXPR_LOCATION (result, input_location);
2497 KOENIG_LOOKUP_P (result) = koenig_p;
2498 release_tree_vector (orig_args);
2499 result = convert_from_reference (result);
2502 /* Free or retain OVERLOADs from lookup. */
2503 if (is_overloaded_fn (orig_fn))
2504 lookup_keep (get_fns (orig_fn), processing_template_decl);
2506 return result;
2509 /* Finish a call to a postfix increment or decrement or EXPR. (Which
2510 is indicated by CODE, which should be POSTINCREMENT_EXPR or
2511 POSTDECREMENT_EXPR.) */
2513 cp_expr
2514 finish_increment_expr (cp_expr expr, enum tree_code code)
2516 /* input_location holds the location of the trailing operator token.
2517 Build a location of the form:
2518 expr++
2519 ~~~~^~
2520 with the caret at the operator token, ranging from the start
2521 of EXPR to the end of the operator token. */
2522 location_t combined_loc = make_location (input_location,
2523 expr.get_start (),
2524 get_finish (input_location));
2525 cp_expr result = build_x_unary_op (combined_loc, code, expr,
2526 tf_warning_or_error);
2527 /* TODO: build_x_unary_op doesn't honor the location, so set it here. */
2528 result.set_location (combined_loc);
2529 return result;
2532 /* Finish a use of `this'. Returns an expression for `this'. */
2534 tree
2535 finish_this_expr (void)
2537 tree result = NULL_TREE;
2539 if (current_class_ptr)
2541 tree type = TREE_TYPE (current_class_ref);
2543 /* In a lambda expression, 'this' refers to the captured 'this'. */
2544 if (LAMBDA_TYPE_P (type))
2545 result = lambda_expr_this_capture (CLASSTYPE_LAMBDA_EXPR (type), true);
2546 else
2547 result = current_class_ptr;
2550 if (result)
2551 /* The keyword 'this' is a prvalue expression. */
2552 return rvalue (result);
2554 tree fn = current_nonlambda_function ();
2555 if (fn && DECL_STATIC_FUNCTION_P (fn))
2556 error ("%<this%> is unavailable for static member functions");
2557 else if (fn)
2558 error ("invalid use of %<this%> in non-member function");
2559 else
2560 error ("invalid use of %<this%> at top level");
2561 return error_mark_node;
2564 /* Finish a pseudo-destructor expression. If SCOPE is NULL, the
2565 expression was of the form `OBJECT.~DESTRUCTOR' where DESTRUCTOR is
2566 the TYPE for the type given. If SCOPE is non-NULL, the expression
2567 was of the form `OBJECT.SCOPE::~DESTRUCTOR'. */
2569 tree
2570 finish_pseudo_destructor_expr (tree object, tree scope, tree destructor,
2571 location_t loc)
2573 if (object == error_mark_node || destructor == error_mark_node)
2574 return error_mark_node;
2576 gcc_assert (TYPE_P (destructor));
2578 if (!processing_template_decl)
2580 if (scope == error_mark_node)
2582 error_at (loc, "invalid qualifying scope in pseudo-destructor name");
2583 return error_mark_node;
2585 if (is_auto (destructor))
2586 destructor = TREE_TYPE (object);
2587 if (scope && TYPE_P (scope) && !check_dtor_name (scope, destructor))
2589 error_at (loc,
2590 "qualified type %qT does not match destructor name ~%qT",
2591 scope, destructor);
2592 return error_mark_node;
2596 /* [expr.pseudo] says both:
2598 The type designated by the pseudo-destructor-name shall be
2599 the same as the object type.
2601 and:
2603 The cv-unqualified versions of the object type and of the
2604 type designated by the pseudo-destructor-name shall be the
2605 same type.
2607 We implement the more generous second sentence, since that is
2608 what most other compilers do. */
2609 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (object),
2610 destructor))
2612 error_at (loc, "%qE is not of type %qT", object, destructor);
2613 return error_mark_node;
2617 return build3_loc (loc, PSEUDO_DTOR_EXPR, void_type_node, object,
2618 scope, destructor);
2621 /* Finish an expression of the form CODE EXPR. */
2623 cp_expr
2624 finish_unary_op_expr (location_t op_loc, enum tree_code code, cp_expr expr,
2625 tsubst_flags_t complain)
2627 /* Build a location of the form:
2628 ++expr
2629 ^~~~~~
2630 with the caret at the operator token, ranging from the start
2631 of the operator token to the end of EXPR. */
2632 location_t combined_loc = make_location (op_loc,
2633 op_loc, expr.get_finish ());
2634 cp_expr result = build_x_unary_op (combined_loc, code, expr, complain);
2635 /* TODO: build_x_unary_op doesn't always honor the location. */
2636 result.set_location (combined_loc);
2638 tree result_ovl, expr_ovl;
2640 if (!(complain & tf_warning))
2641 return result;
2643 result_ovl = result;
2644 expr_ovl = expr;
2646 if (!processing_template_decl)
2647 expr_ovl = cp_fully_fold (expr_ovl);
2649 if (!CONSTANT_CLASS_P (expr_ovl)
2650 || TREE_OVERFLOW_P (expr_ovl))
2651 return result;
2653 if (!processing_template_decl)
2654 result_ovl = cp_fully_fold (result_ovl);
2656 if (CONSTANT_CLASS_P (result_ovl) && TREE_OVERFLOW_P (result_ovl))
2657 overflow_warning (combined_loc, result_ovl);
2659 return result;
2662 /* Finish a compound-literal expression or C++11 functional cast with aggregate
2663 initializer. TYPE is the type to which the CONSTRUCTOR in COMPOUND_LITERAL
2664 is being cast. */
2666 tree
2667 finish_compound_literal (tree type, tree compound_literal,
2668 tsubst_flags_t complain,
2669 fcl_t fcl_context)
2671 if (type == error_mark_node)
2672 return error_mark_node;
2674 if (TREE_CODE (type) == REFERENCE_TYPE)
2676 compound_literal
2677 = finish_compound_literal (TREE_TYPE (type), compound_literal,
2678 complain, fcl_context);
2679 return cp_build_c_cast (type, compound_literal, complain);
2682 if (!TYPE_OBJ_P (type))
2684 if (complain & tf_error)
2685 error ("compound literal of non-object type %qT", type);
2686 return error_mark_node;
2689 if (tree anode = type_uses_auto (type))
2690 if (CLASS_PLACEHOLDER_TEMPLATE (anode))
2692 type = do_auto_deduction (type, compound_literal, anode, complain,
2693 adc_variable_type);
2694 if (type == error_mark_node)
2695 return error_mark_node;
2698 if (processing_template_decl)
2700 TREE_TYPE (compound_literal) = type;
2701 /* Mark the expression as a compound literal. */
2702 TREE_HAS_CONSTRUCTOR (compound_literal) = 1;
2703 if (fcl_context == fcl_c99)
2704 CONSTRUCTOR_C99_COMPOUND_LITERAL (compound_literal) = 1;
2705 return compound_literal;
2708 type = complete_type (type);
2710 if (TYPE_NON_AGGREGATE_CLASS (type))
2712 /* Trying to deal with a CONSTRUCTOR instead of a TREE_LIST
2713 everywhere that deals with function arguments would be a pain, so
2714 just wrap it in a TREE_LIST. The parser set a flag so we know
2715 that it came from T{} rather than T({}). */
2716 CONSTRUCTOR_IS_DIRECT_INIT (compound_literal) = 1;
2717 compound_literal = build_tree_list (NULL_TREE, compound_literal);
2718 return build_functional_cast (type, compound_literal, complain);
2721 if (TREE_CODE (type) == ARRAY_TYPE
2722 && check_array_initializer (NULL_TREE, type, compound_literal))
2723 return error_mark_node;
2724 compound_literal = reshape_init (type, compound_literal, complain);
2725 if (SCALAR_TYPE_P (type)
2726 && !BRACE_ENCLOSED_INITIALIZER_P (compound_literal)
2727 && !check_narrowing (type, compound_literal, complain))
2728 return error_mark_node;
2729 if (TREE_CODE (type) == ARRAY_TYPE
2730 && TYPE_DOMAIN (type) == NULL_TREE)
2732 cp_complete_array_type_or_error (&type, compound_literal,
2733 false, complain);
2734 if (type == error_mark_node)
2735 return error_mark_node;
2737 compound_literal = digest_init_flags (type, compound_literal, LOOKUP_NORMAL,
2738 complain);
2739 if (TREE_CODE (compound_literal) == CONSTRUCTOR)
2741 TREE_HAS_CONSTRUCTOR (compound_literal) = true;
2742 if (fcl_context == fcl_c99)
2743 CONSTRUCTOR_C99_COMPOUND_LITERAL (compound_literal) = 1;
2746 /* Put static/constant array temporaries in static variables. */
2747 /* FIXME all C99 compound literals should be variables rather than C++
2748 temporaries, unless they are used as an aggregate initializer. */
2749 if ((!at_function_scope_p () || CP_TYPE_CONST_P (type))
2750 && fcl_context == fcl_c99
2751 && TREE_CODE (type) == ARRAY_TYPE
2752 && !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
2753 && initializer_constant_valid_p (compound_literal, type))
2755 tree decl = create_temporary_var (type);
2756 DECL_INITIAL (decl) = compound_literal;
2757 TREE_STATIC (decl) = 1;
2758 if (literal_type_p (type) && CP_TYPE_CONST_NON_VOLATILE_P (type))
2760 /* 5.19 says that a constant expression can include an
2761 lvalue-rvalue conversion applied to "a glvalue of literal type
2762 that refers to a non-volatile temporary object initialized
2763 with a constant expression". Rather than try to communicate
2764 that this VAR_DECL is a temporary, just mark it constexpr. */
2765 DECL_DECLARED_CONSTEXPR_P (decl) = true;
2766 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
2767 TREE_CONSTANT (decl) = true;
2769 cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
2770 decl = pushdecl_top_level (decl);
2771 DECL_NAME (decl) = make_anon_name ();
2772 SET_DECL_ASSEMBLER_NAME (decl, DECL_NAME (decl));
2773 /* Make sure the destructor is callable. */
2774 tree clean = cxx_maybe_build_cleanup (decl, complain);
2775 if (clean == error_mark_node)
2776 return error_mark_node;
2777 return decl;
2780 /* Represent other compound literals with TARGET_EXPR so we produce
2781 an lvalue, but can elide copies. */
2782 if (!VECTOR_TYPE_P (type))
2783 compound_literal = get_target_expr_sfinae (compound_literal, complain);
2785 return compound_literal;
2788 /* Return the declaration for the function-name variable indicated by
2789 ID. */
2791 tree
2792 finish_fname (tree id)
2794 tree decl;
2796 decl = fname_decl (input_location, C_RID_CODE (id), id);
2797 if (processing_template_decl && current_function_decl
2798 && decl != error_mark_node)
2799 decl = DECL_NAME (decl);
2800 return decl;
2803 /* Finish a translation unit. */
2805 void
2806 finish_translation_unit (void)
2808 /* In case there were missing closebraces,
2809 get us back to the global binding level. */
2810 pop_everything ();
2811 while (current_namespace != global_namespace)
2812 pop_namespace ();
2814 /* Do file scope __FUNCTION__ et al. */
2815 finish_fname_decls ();
2818 /* Finish a template type parameter, specified as AGGR IDENTIFIER.
2819 Returns the parameter. */
2821 tree
2822 finish_template_type_parm (tree aggr, tree identifier)
2824 if (aggr != class_type_node)
2826 permerror (input_location, "template type parameters must use the keyword %<class%> or %<typename%>");
2827 aggr = class_type_node;
2830 return build_tree_list (aggr, identifier);
2833 /* Finish a template template parameter, specified as AGGR IDENTIFIER.
2834 Returns the parameter. */
2836 tree
2837 finish_template_template_parm (tree aggr, tree identifier)
2839 tree decl = build_decl (input_location,
2840 TYPE_DECL, identifier, NULL_TREE);
2842 tree tmpl = build_lang_decl (TEMPLATE_DECL, identifier, NULL_TREE);
2843 DECL_TEMPLATE_PARMS (tmpl) = current_template_parms;
2844 DECL_TEMPLATE_RESULT (tmpl) = decl;
2845 DECL_ARTIFICIAL (decl) = 1;
2847 // Associate the constraints with the underlying declaration,
2848 // not the template.
2849 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
2850 tree constr = build_constraints (reqs, NULL_TREE);
2851 set_constraints (decl, constr);
2853 end_template_decl ();
2855 gcc_assert (DECL_TEMPLATE_PARMS (tmpl));
2857 check_default_tmpl_args (decl, DECL_TEMPLATE_PARMS (tmpl),
2858 /*is_primary=*/true, /*is_partial=*/false,
2859 /*is_friend=*/0);
2861 return finish_template_type_parm (aggr, tmpl);
2864 /* ARGUMENT is the default-argument value for a template template
2865 parameter. If ARGUMENT is invalid, issue error messages and return
2866 the ERROR_MARK_NODE. Otherwise, ARGUMENT itself is returned. */
2868 tree
2869 check_template_template_default_arg (tree argument)
2871 if (TREE_CODE (argument) != TEMPLATE_DECL
2872 && TREE_CODE (argument) != TEMPLATE_TEMPLATE_PARM
2873 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
2875 if (TREE_CODE (argument) == TYPE_DECL)
2876 error ("invalid use of type %qT as a default value for a template "
2877 "template-parameter", TREE_TYPE (argument));
2878 else
2879 error ("invalid default argument for a template template parameter");
2880 return error_mark_node;
2883 return argument;
2886 /* Begin a class definition, as indicated by T. */
2888 tree
2889 begin_class_definition (tree t)
2891 if (error_operand_p (t) || error_operand_p (TYPE_MAIN_DECL (t)))
2892 return error_mark_node;
2894 if (processing_template_parmlist)
2896 error ("definition of %q#T inside template parameter list", t);
2897 return error_mark_node;
2900 /* According to the C++ ABI, decimal classes defined in ISO/IEC TR 24733
2901 are passed the same as decimal scalar types. */
2902 if (TREE_CODE (t) == RECORD_TYPE
2903 && !processing_template_decl)
2905 tree ns = TYPE_CONTEXT (t);
2906 if (ns && TREE_CODE (ns) == NAMESPACE_DECL
2907 && DECL_CONTEXT (ns) == std_node
2908 && DECL_NAME (ns)
2909 && id_equal (DECL_NAME (ns), "decimal"))
2911 const char *n = TYPE_NAME_STRING (t);
2912 if ((strcmp (n, "decimal32") == 0)
2913 || (strcmp (n, "decimal64") == 0)
2914 || (strcmp (n, "decimal128") == 0))
2915 TYPE_TRANSPARENT_AGGR (t) = 1;
2919 /* A non-implicit typename comes from code like:
2921 template <typename T> struct A {
2922 template <typename U> struct A<T>::B ...
2924 This is erroneous. */
2925 else if (TREE_CODE (t) == TYPENAME_TYPE)
2927 error ("invalid definition of qualified type %qT", t);
2928 t = error_mark_node;
2931 if (t == error_mark_node || ! MAYBE_CLASS_TYPE_P (t))
2933 t = make_class_type (RECORD_TYPE);
2934 pushtag (make_anon_name (), t, /*tag_scope=*/ts_current);
2937 if (TYPE_BEING_DEFINED (t))
2939 t = make_class_type (TREE_CODE (t));
2940 pushtag (TYPE_IDENTIFIER (t), t, /*tag_scope=*/ts_current);
2942 maybe_process_partial_specialization (t);
2943 pushclass (t);
2944 TYPE_BEING_DEFINED (t) = 1;
2945 class_binding_level->defining_class_p = 1;
2947 if (flag_pack_struct)
2949 tree v;
2950 TYPE_PACKED (t) = 1;
2951 /* Even though the type is being defined for the first time
2952 here, there might have been a forward declaration, so there
2953 might be cv-qualified variants of T. */
2954 for (v = TYPE_NEXT_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v))
2955 TYPE_PACKED (v) = 1;
2957 /* Reset the interface data, at the earliest possible
2958 moment, as it might have been set via a class foo;
2959 before. */
2960 if (! TYPE_UNNAMED_P (t))
2962 struct c_fileinfo *finfo = \
2963 get_fileinfo (LOCATION_FILE (input_location));
2964 CLASSTYPE_INTERFACE_ONLY (t) = finfo->interface_only;
2965 SET_CLASSTYPE_INTERFACE_UNKNOWN_X
2966 (t, finfo->interface_unknown);
2968 reset_specialization();
2970 /* Make a declaration for this class in its own scope. */
2971 build_self_reference ();
2973 return t;
2976 /* Finish the member declaration given by DECL. */
2978 void
2979 finish_member_declaration (tree decl)
2981 if (decl == error_mark_node || decl == NULL_TREE)
2982 return;
2984 if (decl == void_type_node)
2985 /* The COMPONENT was a friend, not a member, and so there's
2986 nothing for us to do. */
2987 return;
2989 /* We should see only one DECL at a time. */
2990 gcc_assert (DECL_CHAIN (decl) == NULL_TREE);
2992 /* Don't add decls after definition. */
2993 gcc_assert (TYPE_BEING_DEFINED (current_class_type)
2994 /* We can add lambda types when late parsing default
2995 arguments. */
2996 || LAMBDA_TYPE_P (TREE_TYPE (decl)));
2998 /* Set up access control for DECL. */
2999 TREE_PRIVATE (decl)
3000 = (current_access_specifier == access_private_node);
3001 TREE_PROTECTED (decl)
3002 = (current_access_specifier == access_protected_node);
3003 if (TREE_CODE (decl) == TEMPLATE_DECL)
3005 TREE_PRIVATE (DECL_TEMPLATE_RESULT (decl)) = TREE_PRIVATE (decl);
3006 TREE_PROTECTED (DECL_TEMPLATE_RESULT (decl)) = TREE_PROTECTED (decl);
3009 /* Mark the DECL as a member of the current class, unless it's
3010 a member of an enumeration. */
3011 if (TREE_CODE (decl) != CONST_DECL)
3012 DECL_CONTEXT (decl) = current_class_type;
3014 if (TREE_CODE (decl) == USING_DECL)
3015 /* For now, ignore class-scope USING_DECLS, so that debugging
3016 backends do not see them. */
3017 DECL_IGNORED_P (decl) = 1;
3019 /* Check for bare parameter packs in the non-static data member
3020 declaration. */
3021 if (TREE_CODE (decl) == FIELD_DECL)
3023 if (check_for_bare_parameter_packs (TREE_TYPE (decl)))
3024 TREE_TYPE (decl) = error_mark_node;
3025 if (check_for_bare_parameter_packs (DECL_ATTRIBUTES (decl)))
3026 DECL_ATTRIBUTES (decl) = NULL_TREE;
3029 /* [dcl.link]
3031 A C language linkage is ignored for the names of class members
3032 and the member function type of class member functions. */
3033 if (DECL_LANG_SPECIFIC (decl))
3034 SET_DECL_LANGUAGE (decl, lang_cplusplus);
3036 bool add = false;
3038 /* Functions and non-functions are added differently. */
3039 if (DECL_DECLARES_FUNCTION_P (decl))
3040 add = add_method (current_class_type, decl, false);
3041 /* Enter the DECL into the scope of the class, if the class
3042 isn't a closure (whose fields are supposed to be unnamed). */
3043 else if (CLASSTYPE_LAMBDA_EXPR (current_class_type)
3044 || pushdecl_class_level (decl))
3045 add = true;
3047 if (add)
3049 /* All TYPE_DECLs go at the end of TYPE_FIELDS. Ordinary fields
3050 go at the beginning. The reason is that
3051 legacy_nonfn_member_lookup searches the list in order, and we
3052 want a field name to override a type name so that the "struct
3053 stat hack" will work. In particular:
3055 struct S { enum E { }; static const int E = 5; int ary[S::E]; } s;
3057 is valid. */
3059 if (TREE_CODE (decl) == TYPE_DECL)
3060 TYPE_FIELDS (current_class_type)
3061 = chainon (TYPE_FIELDS (current_class_type), decl);
3062 else
3064 DECL_CHAIN (decl) = TYPE_FIELDS (current_class_type);
3065 TYPE_FIELDS (current_class_type) = decl;
3068 maybe_add_class_template_decl_list (current_class_type, decl,
3069 /*friend_p=*/0);
3073 /* Finish processing a complete template declaration. The PARMS are
3074 the template parameters. */
3076 void
3077 finish_template_decl (tree parms)
3079 if (parms)
3080 end_template_decl ();
3081 else
3082 end_specialization ();
3085 // Returns the template type of the class scope being entered. If we're
3086 // entering a constrained class scope. TYPE is the class template
3087 // scope being entered and we may need to match the intended type with
3088 // a constrained specialization. For example:
3090 // template<Object T>
3091 // struct S { void f(); }; #1
3093 // template<Object T>
3094 // void S<T>::f() { } #2
3096 // We check, in #2, that S<T> refers precisely to the type declared by
3097 // #1 (i.e., that the constraints match). Note that the following should
3098 // be an error since there is no specialization of S<T> that is
3099 // unconstrained, but this is not diagnosed here.
3101 // template<typename T>
3102 // void S<T>::f() { }
3104 // We cannot diagnose this problem here since this function also matches
3105 // qualified template names that are not part of a definition. For example:
3107 // template<Integral T, Floating_point U>
3108 // typename pair<T, U>::first_type void f(T, U);
3110 // Here, it is unlikely that there is a partial specialization of
3111 // pair constrained for for Integral and Floating_point arguments.
3113 // The general rule is: if a constrained specialization with matching
3114 // constraints is found return that type. Also note that if TYPE is not a
3115 // class-type (e.g. a typename type), then no fixup is needed.
3117 static tree
3118 fixup_template_type (tree type)
3120 // Find the template parameter list at the a depth appropriate to
3121 // the scope we're trying to enter.
3122 tree parms = current_template_parms;
3123 int depth = template_class_depth (type);
3124 for (int n = processing_template_decl; n > depth && parms; --n)
3125 parms = TREE_CHAIN (parms);
3126 if (!parms)
3127 return type;
3128 tree cur_reqs = TEMPLATE_PARMS_CONSTRAINTS (parms);
3129 tree cur_constr = build_constraints (cur_reqs, NULL_TREE);
3131 // Search for a specialization whose type and constraints match.
3132 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
3133 tree specs = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
3134 while (specs)
3136 tree spec_constr = get_constraints (TREE_VALUE (specs));
3138 // If the type and constraints match a specialization, then we
3139 // are entering that type.
3140 if (same_type_p (type, TREE_TYPE (specs))
3141 && equivalent_constraints (cur_constr, spec_constr))
3142 return TREE_TYPE (specs);
3143 specs = TREE_CHAIN (specs);
3146 // If no specialization matches, then must return the type
3147 // previously found.
3148 return type;
3151 /* Finish processing a template-id (which names a type) of the form
3152 NAME < ARGS >. Return the TYPE_DECL for the type named by the
3153 template-id. If ENTERING_SCOPE is nonzero we are about to enter
3154 the scope of template-id indicated. */
3156 tree
3157 finish_template_type (tree name, tree args, int entering_scope)
3159 tree type;
3161 type = lookup_template_class (name, args,
3162 NULL_TREE, NULL_TREE, entering_scope,
3163 tf_warning_or_error | tf_user);
3165 /* If we might be entering the scope of a partial specialization,
3166 find the one with the right constraints. */
3167 if (flag_concepts
3168 && entering_scope
3169 && CLASS_TYPE_P (type)
3170 && CLASSTYPE_TEMPLATE_INFO (type)
3171 && dependent_type_p (type)
3172 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
3173 type = fixup_template_type (type);
3175 if (type == error_mark_node)
3176 return type;
3177 else if (CLASS_TYPE_P (type) && !alias_type_or_template_p (type))
3178 return TYPE_STUB_DECL (type);
3179 else
3180 return TYPE_NAME (type);
3183 /* Finish processing a BASE_CLASS with the indicated ACCESS_SPECIFIER.
3184 Return a TREE_LIST containing the ACCESS_SPECIFIER and the
3185 BASE_CLASS, or NULL_TREE if an error occurred. The
3186 ACCESS_SPECIFIER is one of
3187 access_{default,public,protected_private}_node. For a virtual base
3188 we set TREE_TYPE. */
3190 tree
3191 finish_base_specifier (tree base, tree access, bool virtual_p)
3193 tree result;
3195 if (base == error_mark_node)
3197 error ("invalid base-class specification");
3198 result = NULL_TREE;
3200 else if (! MAYBE_CLASS_TYPE_P (base))
3202 error ("%qT is not a class type", base);
3203 result = NULL_TREE;
3205 else
3207 if (cp_type_quals (base) != 0)
3209 /* DR 484: Can a base-specifier name a cv-qualified
3210 class type? */
3211 base = TYPE_MAIN_VARIANT (base);
3213 result = build_tree_list (access, base);
3214 if (virtual_p)
3215 TREE_TYPE (result) = integer_type_node;
3218 return result;
3221 /* If FNS is a member function, a set of member functions, or a
3222 template-id referring to one or more member functions, return a
3223 BASELINK for FNS, incorporating the current access context.
3224 Otherwise, return FNS unchanged. */
3226 tree
3227 baselink_for_fns (tree fns)
3229 tree scope;
3230 tree cl;
3232 if (BASELINK_P (fns)
3233 || error_operand_p (fns))
3234 return fns;
3236 scope = ovl_scope (fns);
3237 if (!CLASS_TYPE_P (scope))
3238 return fns;
3240 cl = currently_open_derived_class (scope);
3241 if (!cl)
3242 cl = scope;
3243 cl = TYPE_BINFO (cl);
3244 return build_baselink (cl, cl, fns, /*optype=*/NULL_TREE);
3247 /* Returns true iff DECL is a variable from a function outside
3248 the current one. */
3250 static bool
3251 outer_var_p (tree decl)
3253 return ((VAR_P (decl) || TREE_CODE (decl) == PARM_DECL)
3254 && DECL_FUNCTION_SCOPE_P (decl)
3255 /* Don't get confused by temporaries. */
3256 && DECL_NAME (decl)
3257 && (DECL_CONTEXT (decl) != current_function_decl
3258 || parsing_nsdmi ()));
3261 /* As above, but also checks that DECL is automatic. */
3263 bool
3264 outer_automatic_var_p (tree decl)
3266 return (outer_var_p (decl)
3267 && !TREE_STATIC (decl));
3270 /* DECL satisfies outer_automatic_var_p. Possibly complain about it or
3271 rewrite it for lambda capture.
3273 If ODR_USE is true, we're being called from mark_use, and we complain about
3274 use of constant variables. If ODR_USE is false, we're being called for the
3275 id-expression, and we do lambda capture. */
3277 tree
3278 process_outer_var_ref (tree decl, tsubst_flags_t complain, bool odr_use)
3280 if (cp_unevaluated_operand)
3281 /* It's not a use (3.2) if we're in an unevaluated context. */
3282 return decl;
3283 if (decl == error_mark_node)
3284 return decl;
3286 tree context = DECL_CONTEXT (decl);
3287 tree containing_function = current_function_decl;
3288 tree lambda_stack = NULL_TREE;
3289 tree lambda_expr = NULL_TREE;
3290 tree initializer = convert_from_reference (decl);
3292 /* Mark it as used now even if the use is ill-formed. */
3293 if (!mark_used (decl, complain))
3294 return error_mark_node;
3296 if (parsing_nsdmi ())
3297 containing_function = NULL_TREE;
3299 if (containing_function && LAMBDA_FUNCTION_P (containing_function))
3301 /* Check whether we've already built a proxy. */
3302 tree var = decl;
3303 while (is_normal_capture_proxy (var))
3304 var = DECL_CAPTURED_VARIABLE (var);
3305 tree d = retrieve_local_specialization (var);
3307 if (d && d != decl && is_capture_proxy (d))
3309 if (DECL_CONTEXT (d) == containing_function)
3310 /* We already have an inner proxy. */
3311 return d;
3312 else
3313 /* We need to capture an outer proxy. */
3314 return process_outer_var_ref (d, complain, odr_use);
3318 /* If we are in a lambda function, we can move out until we hit
3319 1. the context,
3320 2. a non-lambda function, or
3321 3. a non-default capturing lambda function. */
3322 while (context != containing_function
3323 /* containing_function can be null with invalid generic lambdas. */
3324 && containing_function
3325 && LAMBDA_FUNCTION_P (containing_function))
3327 tree closure = DECL_CONTEXT (containing_function);
3328 lambda_expr = CLASSTYPE_LAMBDA_EXPR (closure);
3330 if (TYPE_CLASS_SCOPE_P (closure))
3331 /* A lambda in an NSDMI (c++/64496). */
3332 break;
3334 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr)
3335 == CPLD_NONE)
3336 break;
3338 lambda_stack = tree_cons (NULL_TREE,
3339 lambda_expr,
3340 lambda_stack);
3342 containing_function
3343 = decl_function_context (containing_function);
3346 /* In a lambda within a template, wait until instantiation
3347 time to implicitly capture. */
3348 if (context == containing_function
3349 && DECL_TEMPLATE_INFO (containing_function)
3350 && uses_template_parms (DECL_TI_ARGS (containing_function)))
3351 return decl;
3353 if (lambda_expr && VAR_P (decl)
3354 && DECL_ANON_UNION_VAR_P (decl))
3356 if (complain & tf_error)
3357 error ("cannot capture member %qD of anonymous union", decl);
3358 return error_mark_node;
3360 /* Do lambda capture when processing the id-expression, not when
3361 odr-using a variable. */
3362 if (!odr_use && context == containing_function)
3364 decl = add_default_capture (lambda_stack,
3365 /*id=*/DECL_NAME (decl),
3366 initializer);
3368 /* Only an odr-use of an outer automatic variable causes an
3369 error, and a constant variable can decay to a prvalue
3370 constant without odr-use. So don't complain yet. */
3371 else if (!odr_use && decl_constant_var_p (decl))
3372 return decl;
3373 else if (lambda_expr)
3375 if (complain & tf_error)
3377 error ("%qD is not captured", decl);
3378 tree closure = LAMBDA_EXPR_CLOSURE (lambda_expr);
3379 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr)
3380 == CPLD_NONE)
3381 inform (location_of (closure),
3382 "the lambda has no capture-default");
3383 else if (TYPE_CLASS_SCOPE_P (closure))
3384 inform (UNKNOWN_LOCATION, "lambda in local class %q+T cannot "
3385 "capture variables from the enclosing context",
3386 TYPE_CONTEXT (closure));
3387 inform (DECL_SOURCE_LOCATION (decl), "%q#D declared here", decl);
3389 return error_mark_node;
3391 else
3393 if (complain & tf_error)
3395 error (VAR_P (decl)
3396 ? G_("use of local variable with automatic storage from "
3397 "containing function")
3398 : G_("use of parameter from containing function"));
3399 inform (DECL_SOURCE_LOCATION (decl), "%q#D declared here", decl);
3401 return error_mark_node;
3403 return decl;
3406 /* ID_EXPRESSION is a representation of parsed, but unprocessed,
3407 id-expression. (See cp_parser_id_expression for details.) SCOPE,
3408 if non-NULL, is the type or namespace used to explicitly qualify
3409 ID_EXPRESSION. DECL is the entity to which that name has been
3410 resolved.
3412 *CONSTANT_EXPRESSION_P is true if we are presently parsing a
3413 constant-expression. In that case, *NON_CONSTANT_EXPRESSION_P will
3414 be set to true if this expression isn't permitted in a
3415 constant-expression, but it is otherwise not set by this function.
3416 *ALLOW_NON_CONSTANT_EXPRESSION_P is true if we are parsing a
3417 constant-expression, but a non-constant expression is also
3418 permissible.
3420 DONE is true if this expression is a complete postfix-expression;
3421 it is false if this expression is followed by '->', '[', '(', etc.
3422 ADDRESS_P is true iff this expression is the operand of '&'.
3423 TEMPLATE_P is true iff the qualified-id was of the form
3424 "A::template B". TEMPLATE_ARG_P is true iff this qualified name
3425 appears as a template argument.
3427 If an error occurs, and it is the kind of error that might cause
3428 the parser to abort a tentative parse, *ERROR_MSG is filled in. It
3429 is the caller's responsibility to issue the message. *ERROR_MSG
3430 will be a string with static storage duration, so the caller need
3431 not "free" it.
3433 Return an expression for the entity, after issuing appropriate
3434 diagnostics. This function is also responsible for transforming a
3435 reference to a non-static member into a COMPONENT_REF that makes
3436 the use of "this" explicit.
3438 Upon return, *IDK will be filled in appropriately. */
3439 cp_expr
3440 finish_id_expression (tree id_expression,
3441 tree decl,
3442 tree scope,
3443 cp_id_kind *idk,
3444 bool integral_constant_expression_p,
3445 bool allow_non_integral_constant_expression_p,
3446 bool *non_integral_constant_expression_p,
3447 bool template_p,
3448 bool done,
3449 bool address_p,
3450 bool template_arg_p,
3451 const char **error_msg,
3452 location_t location)
3454 decl = strip_using_decl (decl);
3456 /* Initialize the output parameters. */
3457 *idk = CP_ID_KIND_NONE;
3458 *error_msg = NULL;
3460 if (id_expression == error_mark_node)
3461 return error_mark_node;
3462 /* If we have a template-id, then no further lookup is
3463 required. If the template-id was for a template-class, we
3464 will sometimes have a TYPE_DECL at this point. */
3465 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3466 || TREE_CODE (decl) == TYPE_DECL)
3468 /* Look up the name. */
3469 else
3471 if (decl == error_mark_node)
3473 /* Name lookup failed. */
3474 if (scope
3475 && (!TYPE_P (scope)
3476 || (!dependent_type_p (scope)
3477 && !(identifier_p (id_expression)
3478 && IDENTIFIER_CONV_OP_P (id_expression)
3479 && dependent_type_p (TREE_TYPE (id_expression))))))
3481 /* If the qualifying type is non-dependent (and the name
3482 does not name a conversion operator to a dependent
3483 type), issue an error. */
3484 qualified_name_lookup_error (scope, id_expression, decl, location);
3485 return error_mark_node;
3487 else if (!scope)
3489 /* It may be resolved via Koenig lookup. */
3490 *idk = CP_ID_KIND_UNQUALIFIED;
3491 return id_expression;
3493 else
3494 decl = id_expression;
3496 /* If DECL is a variable that would be out of scope under
3497 ANSI/ISO rules, but in scope in the ARM, name lookup
3498 will succeed. Issue a diagnostic here. */
3499 else
3500 decl = check_for_out_of_scope_variable (decl);
3502 /* Remember that the name was used in the definition of
3503 the current class so that we can check later to see if
3504 the meaning would have been different after the class
3505 was entirely defined. */
3506 if (!scope && decl != error_mark_node && identifier_p (id_expression))
3507 maybe_note_name_used_in_class (id_expression, decl);
3509 /* A use in unevaluated operand might not be instantiated appropriately
3510 if tsubst_copy builds a dummy parm, or if we never instantiate a
3511 generic lambda, so mark it now. */
3512 if (processing_template_decl && cp_unevaluated_operand)
3513 mark_type_use (decl);
3515 /* Disallow uses of local variables from containing functions, except
3516 within lambda-expressions. */
3517 if (outer_automatic_var_p (decl))
3519 decl = process_outer_var_ref (decl, tf_warning_or_error);
3520 if (decl == error_mark_node)
3521 return error_mark_node;
3524 /* Also disallow uses of function parameters outside the function
3525 body, except inside an unevaluated context (i.e. decltype). */
3526 if (TREE_CODE (decl) == PARM_DECL
3527 && DECL_CONTEXT (decl) == NULL_TREE
3528 && !cp_unevaluated_operand)
3530 *error_msg = G_("use of parameter outside function body");
3531 return error_mark_node;
3535 /* If we didn't find anything, or what we found was a type,
3536 then this wasn't really an id-expression. */
3537 if (TREE_CODE (decl) == TEMPLATE_DECL
3538 && !DECL_FUNCTION_TEMPLATE_P (decl))
3540 *error_msg = G_("missing template arguments");
3541 return error_mark_node;
3543 else if (TREE_CODE (decl) == TYPE_DECL
3544 || TREE_CODE (decl) == NAMESPACE_DECL)
3546 *error_msg = G_("expected primary-expression");
3547 return error_mark_node;
3550 /* If the name resolved to a template parameter, there is no
3551 need to look it up again later. */
3552 if ((TREE_CODE (decl) == CONST_DECL && DECL_TEMPLATE_PARM_P (decl))
3553 || TREE_CODE (decl) == TEMPLATE_PARM_INDEX)
3555 tree r;
3557 *idk = CP_ID_KIND_NONE;
3558 if (TREE_CODE (decl) == TEMPLATE_PARM_INDEX)
3559 decl = TEMPLATE_PARM_DECL (decl);
3560 r = convert_from_reference (DECL_INITIAL (decl));
3562 if (integral_constant_expression_p
3563 && !dependent_type_p (TREE_TYPE (decl))
3564 && !(INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (r))))
3566 if (!allow_non_integral_constant_expression_p)
3567 error ("template parameter %qD of type %qT is not allowed in "
3568 "an integral constant expression because it is not of "
3569 "integral or enumeration type", decl, TREE_TYPE (decl));
3570 *non_integral_constant_expression_p = true;
3572 return r;
3574 else
3576 bool dependent_p = type_dependent_expression_p (decl);
3578 /* If the declaration was explicitly qualified indicate
3579 that. The semantics of `A::f(3)' are different than
3580 `f(3)' if `f' is virtual. */
3581 *idk = (scope
3582 ? CP_ID_KIND_QUALIFIED
3583 : (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3584 ? CP_ID_KIND_TEMPLATE_ID
3585 : (dependent_p
3586 ? CP_ID_KIND_UNQUALIFIED_DEPENDENT
3587 : CP_ID_KIND_UNQUALIFIED)));
3589 if (dependent_p
3590 && DECL_P (decl)
3591 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (decl)))
3592 /* Dependent type attributes on the decl mean that the TREE_TYPE is
3593 wrong, so just return the identifier. */
3594 return id_expression;
3596 if (TREE_CODE (decl) == NAMESPACE_DECL)
3598 error ("use of namespace %qD as expression", decl);
3599 return error_mark_node;
3601 else if (DECL_CLASS_TEMPLATE_P (decl))
3603 error ("use of class template %qT as expression", decl);
3604 return error_mark_node;
3606 else if (TREE_CODE (decl) == TREE_LIST)
3608 /* Ambiguous reference to base members. */
3609 error ("request for member %qD is ambiguous in "
3610 "multiple inheritance lattice", id_expression);
3611 print_candidates (decl);
3612 return error_mark_node;
3615 /* Mark variable-like entities as used. Functions are similarly
3616 marked either below or after overload resolution. */
3617 if ((VAR_P (decl)
3618 || TREE_CODE (decl) == PARM_DECL
3619 || TREE_CODE (decl) == CONST_DECL
3620 || TREE_CODE (decl) == RESULT_DECL)
3621 && !mark_used (decl))
3622 return error_mark_node;
3624 /* Only certain kinds of names are allowed in constant
3625 expression. Template parameters have already
3626 been handled above. */
3627 if (! error_operand_p (decl)
3628 && !dependent_p
3629 && integral_constant_expression_p
3630 && ! decl_constant_var_p (decl)
3631 && TREE_CODE (decl) != CONST_DECL
3632 && ! builtin_valid_in_constant_expr_p (decl))
3634 if (!allow_non_integral_constant_expression_p)
3636 error ("%qD cannot appear in a constant-expression", decl);
3637 return error_mark_node;
3639 *non_integral_constant_expression_p = true;
3642 tree wrap;
3643 if (VAR_P (decl)
3644 && !cp_unevaluated_operand
3645 && !processing_template_decl
3646 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl))
3647 && CP_DECL_THREAD_LOCAL_P (decl)
3648 && (wrap = get_tls_wrapper_fn (decl)))
3650 /* Replace an evaluated use of the thread_local variable with
3651 a call to its wrapper. */
3652 decl = build_cxx_call (wrap, 0, NULL, tf_warning_or_error);
3654 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3655 && !dependent_p
3656 && variable_template_p (TREE_OPERAND (decl, 0)))
3658 decl = finish_template_variable (decl);
3659 mark_used (decl);
3660 decl = convert_from_reference (decl);
3662 else if (scope)
3664 if (TREE_CODE (decl) == SCOPE_REF)
3666 gcc_assert (same_type_p (scope, TREE_OPERAND (decl, 0)));
3667 decl = TREE_OPERAND (decl, 1);
3670 decl = (adjust_result_of_qualified_name_lookup
3671 (decl, scope, current_nonlambda_class_type()));
3673 if (TREE_CODE (decl) == FUNCTION_DECL)
3674 mark_used (decl);
3676 if (TYPE_P (scope))
3677 decl = finish_qualified_id_expr (scope,
3678 decl,
3679 done,
3680 address_p,
3681 template_p,
3682 template_arg_p,
3683 tf_warning_or_error);
3684 else
3685 decl = convert_from_reference (decl);
3687 else if (TREE_CODE (decl) == FIELD_DECL)
3689 /* Since SCOPE is NULL here, this is an unqualified name.
3690 Access checking has been performed during name lookup
3691 already. Turn off checking to avoid duplicate errors. */
3692 push_deferring_access_checks (dk_no_check);
3693 decl = finish_non_static_data_member (decl, NULL_TREE,
3694 /*qualifying_scope=*/NULL_TREE);
3695 pop_deferring_access_checks ();
3697 else if (is_overloaded_fn (decl))
3699 tree first_fn = get_first_fn (decl);
3701 if (TREE_CODE (first_fn) == TEMPLATE_DECL)
3702 first_fn = DECL_TEMPLATE_RESULT (first_fn);
3704 /* [basic.def.odr]: "A function whose name appears as a
3705 potentially-evaluated expression is odr-used if it is the unique
3706 lookup result".
3708 But only mark it if it's a complete postfix-expression; in a call,
3709 ADL might select a different function, and we'll call mark_used in
3710 build_over_call. */
3711 if (done
3712 && !really_overloaded_fn (decl)
3713 && !mark_used (first_fn))
3714 return error_mark_node;
3716 if (!template_arg_p
3717 && TREE_CODE (first_fn) == FUNCTION_DECL
3718 && DECL_FUNCTION_MEMBER_P (first_fn)
3719 && !shared_member_p (decl))
3721 /* A set of member functions. */
3722 decl = maybe_dummy_object (DECL_CONTEXT (first_fn), 0);
3723 return finish_class_member_access_expr (decl, id_expression,
3724 /*template_p=*/false,
3725 tf_warning_or_error);
3728 decl = baselink_for_fns (decl);
3730 else
3732 if (DECL_P (decl) && DECL_NONLOCAL (decl)
3733 && DECL_CLASS_SCOPE_P (decl))
3735 tree context = context_for_name_lookup (decl);
3736 if (context != current_class_type)
3738 tree path = currently_open_derived_class (context);
3739 perform_or_defer_access_check (TYPE_BINFO (path),
3740 decl, decl,
3741 tf_warning_or_error);
3745 decl = convert_from_reference (decl);
3749 return cp_expr (decl, location);
3752 /* Implement the __typeof keyword: Return the type of EXPR, suitable for
3753 use as a type-specifier. */
3755 tree
3756 finish_typeof (tree expr)
3758 tree type;
3760 if (type_dependent_expression_p (expr))
3762 type = cxx_make_type (TYPEOF_TYPE);
3763 TYPEOF_TYPE_EXPR (type) = expr;
3764 SET_TYPE_STRUCTURAL_EQUALITY (type);
3766 return type;
3769 expr = mark_type_use (expr);
3771 type = unlowered_expr_type (expr);
3773 if (!type || type == unknown_type_node)
3775 error ("type of %qE is unknown", expr);
3776 return error_mark_node;
3779 return type;
3782 /* Implement the __underlying_type keyword: Return the underlying
3783 type of TYPE, suitable for use as a type-specifier. */
3785 tree
3786 finish_underlying_type (tree type)
3788 tree underlying_type;
3790 if (processing_template_decl)
3792 underlying_type = cxx_make_type (UNDERLYING_TYPE);
3793 UNDERLYING_TYPE_TYPE (underlying_type) = type;
3794 SET_TYPE_STRUCTURAL_EQUALITY (underlying_type);
3796 return underlying_type;
3799 if (!complete_type_or_else (type, NULL_TREE))
3800 return error_mark_node;
3802 if (TREE_CODE (type) != ENUMERAL_TYPE)
3804 error ("%qT is not an enumeration type", type);
3805 return error_mark_node;
3808 underlying_type = ENUM_UNDERLYING_TYPE (type);
3810 /* Fixup necessary in this case because ENUM_UNDERLYING_TYPE
3811 includes TYPE_MIN_VALUE and TYPE_MAX_VALUE information.
3812 See finish_enum_value_list for details. */
3813 if (!ENUM_FIXED_UNDERLYING_TYPE_P (type))
3814 underlying_type
3815 = c_common_type_for_mode (TYPE_MODE (underlying_type),
3816 TYPE_UNSIGNED (underlying_type));
3818 return underlying_type;
3821 /* Implement the __direct_bases keyword: Return the direct base classes
3822 of type */
3824 tree
3825 calculate_direct_bases (tree type)
3827 vec<tree, va_gc> *vector = make_tree_vector();
3828 tree bases_vec = NULL_TREE;
3829 vec<tree, va_gc> *base_binfos;
3830 tree binfo;
3831 unsigned i;
3833 complete_type (type);
3835 if (!NON_UNION_CLASS_TYPE_P (type))
3836 return make_tree_vec (0);
3838 base_binfos = BINFO_BASE_BINFOS (TYPE_BINFO (type));
3840 /* Virtual bases are initialized first */
3841 for (i = 0; base_binfos->iterate (i, &binfo); i++)
3843 if (BINFO_VIRTUAL_P (binfo))
3845 vec_safe_push (vector, binfo);
3849 /* Now non-virtuals */
3850 for (i = 0; base_binfos->iterate (i, &binfo); i++)
3852 if (!BINFO_VIRTUAL_P (binfo))
3854 vec_safe_push (vector, binfo);
3859 bases_vec = make_tree_vec (vector->length ());
3861 for (i = 0; i < vector->length (); ++i)
3863 TREE_VEC_ELT (bases_vec, i) = BINFO_TYPE ((*vector)[i]);
3865 return bases_vec;
3868 /* Implement the __bases keyword: Return the base classes
3869 of type */
3871 /* Find morally non-virtual base classes by walking binfo hierarchy */
3872 /* Virtual base classes are handled separately in finish_bases */
3874 static tree
3875 dfs_calculate_bases_pre (tree binfo, void * /*data_*/)
3877 /* Don't walk bases of virtual bases */
3878 return BINFO_VIRTUAL_P (binfo) ? dfs_skip_bases : NULL_TREE;
3881 static tree
3882 dfs_calculate_bases_post (tree binfo, void *data_)
3884 vec<tree, va_gc> **data = ((vec<tree, va_gc> **) data_);
3885 if (!BINFO_VIRTUAL_P (binfo))
3887 vec_safe_push (*data, BINFO_TYPE (binfo));
3889 return NULL_TREE;
3892 /* Calculates the morally non-virtual base classes of a class */
3893 static vec<tree, va_gc> *
3894 calculate_bases_helper (tree type)
3896 vec<tree, va_gc> *vector = make_tree_vector();
3898 /* Now add non-virtual base classes in order of construction */
3899 if (TYPE_BINFO (type))
3900 dfs_walk_all (TYPE_BINFO (type),
3901 dfs_calculate_bases_pre, dfs_calculate_bases_post, &vector);
3902 return vector;
3905 tree
3906 calculate_bases (tree type)
3908 vec<tree, va_gc> *vector = make_tree_vector();
3909 tree bases_vec = NULL_TREE;
3910 unsigned i;
3911 vec<tree, va_gc> *vbases;
3912 vec<tree, va_gc> *nonvbases;
3913 tree binfo;
3915 complete_type (type);
3917 if (!NON_UNION_CLASS_TYPE_P (type))
3918 return make_tree_vec (0);
3920 /* First go through virtual base classes */
3921 for (vbases = CLASSTYPE_VBASECLASSES (type), i = 0;
3922 vec_safe_iterate (vbases, i, &binfo); i++)
3924 vec<tree, va_gc> *vbase_bases;
3925 vbase_bases = calculate_bases_helper (BINFO_TYPE (binfo));
3926 vec_safe_splice (vector, vbase_bases);
3927 release_tree_vector (vbase_bases);
3930 /* Now for the non-virtual bases */
3931 nonvbases = calculate_bases_helper (type);
3932 vec_safe_splice (vector, nonvbases);
3933 release_tree_vector (nonvbases);
3935 /* Note that during error recovery vector->length can even be zero. */
3936 if (vector->length () > 1)
3938 /* Last element is entire class, so don't copy */
3939 bases_vec = make_tree_vec (vector->length() - 1);
3941 for (i = 0; i < vector->length () - 1; ++i)
3942 TREE_VEC_ELT (bases_vec, i) = (*vector)[i];
3944 else
3945 bases_vec = make_tree_vec (0);
3947 release_tree_vector (vector);
3948 return bases_vec;
3951 tree
3952 finish_bases (tree type, bool direct)
3954 tree bases = NULL_TREE;
3956 if (!processing_template_decl)
3958 /* Parameter packs can only be used in templates */
3959 error ("Parameter pack __bases only valid in template declaration");
3960 return error_mark_node;
3963 bases = cxx_make_type (BASES);
3964 BASES_TYPE (bases) = type;
3965 BASES_DIRECT (bases) = direct;
3966 SET_TYPE_STRUCTURAL_EQUALITY (bases);
3968 return bases;
3971 /* Perform C++-specific checks for __builtin_offsetof before calling
3972 fold_offsetof. */
3974 tree
3975 finish_offsetof (tree object_ptr, tree expr, location_t loc)
3977 /* If we're processing a template, we can't finish the semantics yet.
3978 Otherwise we can fold the entire expression now. */
3979 if (processing_template_decl)
3981 expr = build2 (OFFSETOF_EXPR, size_type_node, expr, object_ptr);
3982 SET_EXPR_LOCATION (expr, loc);
3983 return expr;
3986 if (TREE_CODE (expr) == PSEUDO_DTOR_EXPR)
3988 error ("cannot apply %<offsetof%> to destructor %<~%T%>",
3989 TREE_OPERAND (expr, 2));
3990 return error_mark_node;
3992 if (TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE
3993 || TREE_CODE (TREE_TYPE (expr)) == METHOD_TYPE
3994 || TREE_TYPE (expr) == unknown_type_node)
3996 if (INDIRECT_REF_P (expr))
3997 error ("second operand of %<offsetof%> is neither a single "
3998 "identifier nor a sequence of member accesses and "
3999 "array references");
4000 else
4002 if (TREE_CODE (expr) == COMPONENT_REF
4003 || TREE_CODE (expr) == COMPOUND_EXPR)
4004 expr = TREE_OPERAND (expr, 1);
4005 error ("cannot apply %<offsetof%> to member function %qD", expr);
4007 return error_mark_node;
4009 if (REFERENCE_REF_P (expr))
4010 expr = TREE_OPERAND (expr, 0);
4011 if (!complete_type_or_else (TREE_TYPE (TREE_TYPE (object_ptr)), object_ptr))
4012 return error_mark_node;
4013 if (warn_invalid_offsetof
4014 && CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (object_ptr)))
4015 && CLASSTYPE_NON_STD_LAYOUT (TREE_TYPE (TREE_TYPE (object_ptr)))
4016 && cp_unevaluated_operand == 0)
4017 pedwarn (loc, OPT_Winvalid_offsetof,
4018 "offsetof within non-standard-layout type %qT is undefined",
4019 TREE_TYPE (TREE_TYPE (object_ptr)));
4020 return fold_offsetof (expr);
4023 /* Replace the AGGR_INIT_EXPR at *TP with an equivalent CALL_EXPR. This
4024 function is broken out from the above for the benefit of the tree-ssa
4025 project. */
4027 void
4028 simplify_aggr_init_expr (tree *tp)
4030 tree aggr_init_expr = *tp;
4032 /* Form an appropriate CALL_EXPR. */
4033 tree fn = AGGR_INIT_EXPR_FN (aggr_init_expr);
4034 tree slot = AGGR_INIT_EXPR_SLOT (aggr_init_expr);
4035 tree type = TREE_TYPE (slot);
4037 tree call_expr;
4038 enum style_t { ctor, arg, pcc } style;
4040 if (AGGR_INIT_VIA_CTOR_P (aggr_init_expr))
4041 style = ctor;
4042 #ifdef PCC_STATIC_STRUCT_RETURN
4043 else if (1)
4044 style = pcc;
4045 #endif
4046 else
4048 gcc_assert (TREE_ADDRESSABLE (type));
4049 style = arg;
4052 call_expr = build_call_array_loc (input_location,
4053 TREE_TYPE (TREE_TYPE (TREE_TYPE (fn))),
4055 aggr_init_expr_nargs (aggr_init_expr),
4056 AGGR_INIT_EXPR_ARGP (aggr_init_expr));
4057 TREE_NOTHROW (call_expr) = TREE_NOTHROW (aggr_init_expr);
4058 CALL_FROM_THUNK_P (call_expr) = AGGR_INIT_FROM_THUNK_P (aggr_init_expr);
4059 CALL_EXPR_OPERATOR_SYNTAX (call_expr)
4060 = CALL_EXPR_OPERATOR_SYNTAX (aggr_init_expr);
4061 CALL_EXPR_ORDERED_ARGS (call_expr) = CALL_EXPR_ORDERED_ARGS (aggr_init_expr);
4062 CALL_EXPR_REVERSE_ARGS (call_expr) = CALL_EXPR_REVERSE_ARGS (aggr_init_expr);
4064 if (style == ctor)
4066 /* Replace the first argument to the ctor with the address of the
4067 slot. */
4068 cxx_mark_addressable (slot);
4069 CALL_EXPR_ARG (call_expr, 0) =
4070 build1 (ADDR_EXPR, build_pointer_type (type), slot);
4072 else if (style == arg)
4074 /* Just mark it addressable here, and leave the rest to
4075 expand_call{,_inline}. */
4076 cxx_mark_addressable (slot);
4077 CALL_EXPR_RETURN_SLOT_OPT (call_expr) = true;
4078 call_expr = build2 (INIT_EXPR, TREE_TYPE (call_expr), slot, call_expr);
4080 else if (style == pcc)
4082 /* If we're using the non-reentrant PCC calling convention, then we
4083 need to copy the returned value out of the static buffer into the
4084 SLOT. */
4085 push_deferring_access_checks (dk_no_check);
4086 call_expr = build_aggr_init (slot, call_expr,
4087 DIRECT_BIND | LOOKUP_ONLYCONVERTING,
4088 tf_warning_or_error);
4089 pop_deferring_access_checks ();
4090 call_expr = build2 (COMPOUND_EXPR, TREE_TYPE (slot), call_expr, slot);
4093 if (AGGR_INIT_ZERO_FIRST (aggr_init_expr))
4095 tree init = build_zero_init (type, NULL_TREE,
4096 /*static_storage_p=*/false);
4097 init = build2 (INIT_EXPR, void_type_node, slot, init);
4098 call_expr = build2 (COMPOUND_EXPR, TREE_TYPE (call_expr),
4099 init, call_expr);
4102 *tp = call_expr;
4105 /* Emit all thunks to FN that should be emitted when FN is emitted. */
4107 void
4108 emit_associated_thunks (tree fn)
4110 /* When we use vcall offsets, we emit thunks with the virtual
4111 functions to which they thunk. The whole point of vcall offsets
4112 is so that you can know statically the entire set of thunks that
4113 will ever be needed for a given virtual function, thereby
4114 enabling you to output all the thunks with the function itself. */
4115 if (DECL_VIRTUAL_P (fn)
4116 /* Do not emit thunks for extern template instantiations. */
4117 && ! DECL_REALLY_EXTERN (fn))
4119 tree thunk;
4121 for (thunk = DECL_THUNKS (fn); thunk; thunk = DECL_CHAIN (thunk))
4123 if (!THUNK_ALIAS (thunk))
4125 use_thunk (thunk, /*emit_p=*/1);
4126 if (DECL_RESULT_THUNK_P (thunk))
4128 tree probe;
4130 for (probe = DECL_THUNKS (thunk);
4131 probe; probe = DECL_CHAIN (probe))
4132 use_thunk (probe, /*emit_p=*/1);
4135 else
4136 gcc_assert (!DECL_THUNKS (thunk));
4141 /* Generate RTL for FN. */
4143 bool
4144 expand_or_defer_fn_1 (tree fn)
4146 /* When the parser calls us after finishing the body of a template
4147 function, we don't really want to expand the body. */
4148 if (processing_template_decl)
4150 /* Normally, collection only occurs in rest_of_compilation. So,
4151 if we don't collect here, we never collect junk generated
4152 during the processing of templates until we hit a
4153 non-template function. It's not safe to do this inside a
4154 nested class, though, as the parser may have local state that
4155 is not a GC root. */
4156 if (!function_depth)
4157 ggc_collect ();
4158 return false;
4161 gcc_assert (DECL_SAVED_TREE (fn));
4163 /* We make a decision about linkage for these functions at the end
4164 of the compilation. Until that point, we do not want the back
4165 end to output them -- but we do want it to see the bodies of
4166 these functions so that it can inline them as appropriate. */
4167 if (DECL_DECLARED_INLINE_P (fn) || DECL_IMPLICIT_INSTANTIATION (fn))
4169 if (DECL_INTERFACE_KNOWN (fn))
4170 /* We've already made a decision as to how this function will
4171 be handled. */;
4172 else if (!at_eof)
4173 tentative_decl_linkage (fn);
4174 else
4175 import_export_decl (fn);
4177 /* If the user wants us to keep all inline functions, then mark
4178 this function as needed so that finish_file will make sure to
4179 output it later. Similarly, all dllexport'd functions must
4180 be emitted; there may be callers in other DLLs. */
4181 if (DECL_DECLARED_INLINE_P (fn)
4182 && !DECL_REALLY_EXTERN (fn)
4183 && (flag_keep_inline_functions
4184 || (flag_keep_inline_dllexport
4185 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (fn)))))
4187 mark_needed (fn);
4188 DECL_EXTERNAL (fn) = 0;
4192 /* If this is a constructor or destructor body, we have to clone
4193 it. */
4194 if (maybe_clone_body (fn))
4196 /* We don't want to process FN again, so pretend we've written
4197 it out, even though we haven't. */
4198 TREE_ASM_WRITTEN (fn) = 1;
4199 /* If this is a constexpr function, keep DECL_SAVED_TREE. */
4200 if (!DECL_DECLARED_CONSTEXPR_P (fn))
4201 DECL_SAVED_TREE (fn) = NULL_TREE;
4202 return false;
4205 /* There's no reason to do any of the work here if we're only doing
4206 semantic analysis; this code just generates RTL. */
4207 if (flag_syntax_only)
4208 return false;
4210 return true;
4213 void
4214 expand_or_defer_fn (tree fn)
4216 if (expand_or_defer_fn_1 (fn))
4218 function_depth++;
4220 /* Expand or defer, at the whim of the compilation unit manager. */
4221 cgraph_node::finalize_function (fn, function_depth > 1);
4222 emit_associated_thunks (fn);
4224 function_depth--;
4228 struct nrv_data
4230 nrv_data () : visited (37) {}
4232 tree var;
4233 tree result;
4234 hash_table<nofree_ptr_hash <tree_node> > visited;
4237 /* Helper function for walk_tree, used by finalize_nrv below. */
4239 static tree
4240 finalize_nrv_r (tree* tp, int* walk_subtrees, void* data)
4242 struct nrv_data *dp = (struct nrv_data *)data;
4243 tree_node **slot;
4245 /* No need to walk into types. There wouldn't be any need to walk into
4246 non-statements, except that we have to consider STMT_EXPRs. */
4247 if (TYPE_P (*tp))
4248 *walk_subtrees = 0;
4249 /* Change all returns to just refer to the RESULT_DECL; this is a nop,
4250 but differs from using NULL_TREE in that it indicates that we care
4251 about the value of the RESULT_DECL. */
4252 else if (TREE_CODE (*tp) == RETURN_EXPR)
4253 TREE_OPERAND (*tp, 0) = dp->result;
4254 /* Change all cleanups for the NRV to only run when an exception is
4255 thrown. */
4256 else if (TREE_CODE (*tp) == CLEANUP_STMT
4257 && CLEANUP_DECL (*tp) == dp->var)
4258 CLEANUP_EH_ONLY (*tp) = 1;
4259 /* Replace the DECL_EXPR for the NRV with an initialization of the
4260 RESULT_DECL, if needed. */
4261 else if (TREE_CODE (*tp) == DECL_EXPR
4262 && DECL_EXPR_DECL (*tp) == dp->var)
4264 tree init;
4265 if (DECL_INITIAL (dp->var)
4266 && DECL_INITIAL (dp->var) != error_mark_node)
4267 init = build2 (INIT_EXPR, void_type_node, dp->result,
4268 DECL_INITIAL (dp->var));
4269 else
4270 init = build_empty_stmt (EXPR_LOCATION (*tp));
4271 DECL_INITIAL (dp->var) = NULL_TREE;
4272 SET_EXPR_LOCATION (init, EXPR_LOCATION (*tp));
4273 *tp = init;
4275 /* And replace all uses of the NRV with the RESULT_DECL. */
4276 else if (*tp == dp->var)
4277 *tp = dp->result;
4279 /* Avoid walking into the same tree more than once. Unfortunately, we
4280 can't just use walk_tree_without duplicates because it would only call
4281 us for the first occurrence of dp->var in the function body. */
4282 slot = dp->visited.find_slot (*tp, INSERT);
4283 if (*slot)
4284 *walk_subtrees = 0;
4285 else
4286 *slot = *tp;
4288 /* Keep iterating. */
4289 return NULL_TREE;
4292 /* Called from finish_function to implement the named return value
4293 optimization by overriding all the RETURN_EXPRs and pertinent
4294 CLEANUP_STMTs and replacing all occurrences of VAR with RESULT, the
4295 RESULT_DECL for the function. */
4297 void
4298 finalize_nrv (tree *tp, tree var, tree result)
4300 struct nrv_data data;
4302 /* Copy name from VAR to RESULT. */
4303 DECL_NAME (result) = DECL_NAME (var);
4304 /* Don't forget that we take its address. */
4305 TREE_ADDRESSABLE (result) = TREE_ADDRESSABLE (var);
4306 /* Finally set DECL_VALUE_EXPR to avoid assigning
4307 a stack slot at -O0 for the original var and debug info
4308 uses RESULT location for VAR. */
4309 SET_DECL_VALUE_EXPR (var, result);
4310 DECL_HAS_VALUE_EXPR_P (var) = 1;
4312 data.var = var;
4313 data.result = result;
4314 cp_walk_tree (tp, finalize_nrv_r, &data, 0);
4317 /* Create CP_OMP_CLAUSE_INFO for clause C. Returns true if it is invalid. */
4319 bool
4320 cxx_omp_create_clause_info (tree c, tree type, bool need_default_ctor,
4321 bool need_copy_ctor, bool need_copy_assignment,
4322 bool need_dtor)
4324 int save_errorcount = errorcount;
4325 tree info, t;
4327 /* Always allocate 3 elements for simplicity. These are the
4328 function decls for the ctor, dtor, and assignment op.
4329 This layout is known to the three lang hooks,
4330 cxx_omp_clause_default_init, cxx_omp_clause_copy_init,
4331 and cxx_omp_clause_assign_op. */
4332 info = make_tree_vec (3);
4333 CP_OMP_CLAUSE_INFO (c) = info;
4335 if (need_default_ctor || need_copy_ctor)
4337 if (need_default_ctor)
4338 t = get_default_ctor (type);
4339 else
4340 t = get_copy_ctor (type, tf_warning_or_error);
4342 if (t && !trivial_fn_p (t))
4343 TREE_VEC_ELT (info, 0) = t;
4346 if (need_dtor && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
4347 TREE_VEC_ELT (info, 1) = get_dtor (type, tf_warning_or_error);
4349 if (need_copy_assignment)
4351 t = get_copy_assign (type);
4353 if (t && !trivial_fn_p (t))
4354 TREE_VEC_ELT (info, 2) = t;
4357 return errorcount != save_errorcount;
4360 /* If DECL is DECL_OMP_PRIVATIZED_MEMBER, return corresponding
4361 FIELD_DECL, otherwise return DECL itself. */
4363 static tree
4364 omp_clause_decl_field (tree decl)
4366 if (VAR_P (decl)
4367 && DECL_HAS_VALUE_EXPR_P (decl)
4368 && DECL_ARTIFICIAL (decl)
4369 && DECL_LANG_SPECIFIC (decl)
4370 && DECL_OMP_PRIVATIZED_MEMBER (decl))
4372 tree f = DECL_VALUE_EXPR (decl);
4373 if (TREE_CODE (f) == INDIRECT_REF)
4374 f = TREE_OPERAND (f, 0);
4375 if (TREE_CODE (f) == COMPONENT_REF)
4377 f = TREE_OPERAND (f, 1);
4378 gcc_assert (TREE_CODE (f) == FIELD_DECL);
4379 return f;
4382 return NULL_TREE;
4385 /* Adjust DECL if needed for printing using %qE. */
4387 static tree
4388 omp_clause_printable_decl (tree decl)
4390 tree t = omp_clause_decl_field (decl);
4391 if (t)
4392 return t;
4393 return decl;
4396 /* For a FIELD_DECL F and corresponding DECL_OMP_PRIVATIZED_MEMBER
4397 VAR_DECL T that doesn't need a DECL_EXPR added, record it for
4398 privatization. */
4400 static void
4401 omp_note_field_privatization (tree f, tree t)
4403 if (!omp_private_member_map)
4404 omp_private_member_map = new hash_map<tree, tree>;
4405 tree &v = omp_private_member_map->get_or_insert (f);
4406 if (v == NULL_TREE)
4408 v = t;
4409 omp_private_member_vec.safe_push (f);
4410 /* Signal that we don't want to create DECL_EXPR for this dummy var. */
4411 omp_private_member_vec.safe_push (integer_zero_node);
4415 /* Privatize FIELD_DECL T, return corresponding DECL_OMP_PRIVATIZED_MEMBER
4416 dummy VAR_DECL. */
4418 tree
4419 omp_privatize_field (tree t, bool shared)
4421 tree m = finish_non_static_data_member (t, NULL_TREE, NULL_TREE);
4422 if (m == error_mark_node)
4423 return error_mark_node;
4424 if (!omp_private_member_map && !shared)
4425 omp_private_member_map = new hash_map<tree, tree>;
4426 if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE)
4428 gcc_assert (TREE_CODE (m) == INDIRECT_REF);
4429 m = TREE_OPERAND (m, 0);
4431 tree vb = NULL_TREE;
4432 tree &v = shared ? vb : omp_private_member_map->get_or_insert (t);
4433 if (v == NULL_TREE)
4435 v = create_temporary_var (TREE_TYPE (m));
4436 retrofit_lang_decl (v);
4437 DECL_OMP_PRIVATIZED_MEMBER (v) = 1;
4438 SET_DECL_VALUE_EXPR (v, m);
4439 DECL_HAS_VALUE_EXPR_P (v) = 1;
4440 if (!shared)
4441 omp_private_member_vec.safe_push (t);
4443 return v;
4446 /* Helper function for handle_omp_array_sections. Called recursively
4447 to handle multiple array-section-subscripts. C is the clause,
4448 T current expression (initially OMP_CLAUSE_DECL), which is either
4449 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
4450 expression if specified, TREE_VALUE length expression if specified,
4451 TREE_CHAIN is what it has been specified after, or some decl.
4452 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
4453 set to true if any of the array-section-subscript could have length
4454 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
4455 first array-section-subscript which is known not to have length
4456 of one. Given say:
4457 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
4458 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
4459 all are or may have length of 1, array-section-subscript [:2] is the
4460 first one known not to have length 1. For array-section-subscript
4461 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
4462 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
4463 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
4464 case though, as some lengths could be zero. */
4466 static tree
4467 handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
4468 bool &maybe_zero_len, unsigned int &first_non_one,
4469 enum c_omp_region_type ort)
4471 tree ret, low_bound, length, type;
4472 if (TREE_CODE (t) != TREE_LIST)
4474 if (error_operand_p (t))
4475 return error_mark_node;
4476 if (REFERENCE_REF_P (t)
4477 && TREE_CODE (TREE_OPERAND (t, 0)) == COMPONENT_REF)
4478 t = TREE_OPERAND (t, 0);
4479 ret = t;
4480 if (TREE_CODE (t) == COMPONENT_REF
4481 && ort == C_ORT_OMP
4482 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
4483 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO
4484 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FROM)
4485 && !type_dependent_expression_p (t))
4487 if (TREE_CODE (TREE_OPERAND (t, 1)) == FIELD_DECL
4488 && DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
4490 error_at (OMP_CLAUSE_LOCATION (c),
4491 "bit-field %qE in %qs clause",
4492 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4493 return error_mark_node;
4495 while (TREE_CODE (t) == COMPONENT_REF)
4497 if (TREE_TYPE (TREE_OPERAND (t, 0))
4498 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == UNION_TYPE)
4500 error_at (OMP_CLAUSE_LOCATION (c),
4501 "%qE is a member of a union", t);
4502 return error_mark_node;
4504 t = TREE_OPERAND (t, 0);
4506 if (REFERENCE_REF_P (t))
4507 t = TREE_OPERAND (t, 0);
4509 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
4511 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
4512 return NULL_TREE;
4513 if (DECL_P (t))
4514 error_at (OMP_CLAUSE_LOCATION (c),
4515 "%qD is not a variable in %qs clause", t,
4516 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4517 else
4518 error_at (OMP_CLAUSE_LOCATION (c),
4519 "%qE is not a variable in %qs clause", t,
4520 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4521 return error_mark_node;
4523 else if (TREE_CODE (t) == PARM_DECL
4524 && DECL_ARTIFICIAL (t)
4525 && DECL_NAME (t) == this_identifier)
4527 error_at (OMP_CLAUSE_LOCATION (c),
4528 "%<this%> allowed in OpenMP only in %<declare simd%>"
4529 " clauses");
4530 return error_mark_node;
4532 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
4533 && VAR_P (t) && CP_DECL_THREAD_LOCAL_P (t))
4535 error_at (OMP_CLAUSE_LOCATION (c),
4536 "%qD is threadprivate variable in %qs clause", t,
4537 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4538 return error_mark_node;
4540 if (type_dependent_expression_p (ret))
4541 return NULL_TREE;
4542 ret = convert_from_reference (ret);
4543 return ret;
4546 if (ort == C_ORT_OMP
4547 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
4548 && TREE_CODE (TREE_CHAIN (t)) == FIELD_DECL)
4549 TREE_CHAIN (t) = omp_privatize_field (TREE_CHAIN (t), false);
4550 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
4551 maybe_zero_len, first_non_one, ort);
4552 if (ret == error_mark_node || ret == NULL_TREE)
4553 return ret;
4555 type = TREE_TYPE (ret);
4556 low_bound = TREE_PURPOSE (t);
4557 length = TREE_VALUE (t);
4558 if ((low_bound && type_dependent_expression_p (low_bound))
4559 || (length && type_dependent_expression_p (length)))
4560 return NULL_TREE;
4562 if (low_bound == error_mark_node || length == error_mark_node)
4563 return error_mark_node;
4565 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
4567 error_at (OMP_CLAUSE_LOCATION (c),
4568 "low bound %qE of array section does not have integral type",
4569 low_bound);
4570 return error_mark_node;
4572 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
4574 error_at (OMP_CLAUSE_LOCATION (c),
4575 "length %qE of array section does not have integral type",
4576 length);
4577 return error_mark_node;
4579 if (low_bound)
4580 low_bound = mark_rvalue_use (low_bound);
4581 if (length)
4582 length = mark_rvalue_use (length);
4583 /* We need to reduce to real constant-values for checks below. */
4584 if (length)
4585 length = fold_simple (length);
4586 if (low_bound)
4587 low_bound = fold_simple (low_bound);
4588 if (low_bound
4589 && TREE_CODE (low_bound) == INTEGER_CST
4590 && TYPE_PRECISION (TREE_TYPE (low_bound))
4591 > TYPE_PRECISION (sizetype))
4592 low_bound = fold_convert (sizetype, low_bound);
4593 if (length
4594 && TREE_CODE (length) == INTEGER_CST
4595 && TYPE_PRECISION (TREE_TYPE (length))
4596 > TYPE_PRECISION (sizetype))
4597 length = fold_convert (sizetype, length);
4598 if (low_bound == NULL_TREE)
4599 low_bound = integer_zero_node;
4601 if (length != NULL_TREE)
4603 if (!integer_nonzerop (length))
4605 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
4606 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
4608 if (integer_zerop (length))
4610 error_at (OMP_CLAUSE_LOCATION (c),
4611 "zero length array section in %qs clause",
4612 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4613 return error_mark_node;
4616 else
4617 maybe_zero_len = true;
4619 if (first_non_one == types.length ()
4620 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
4621 first_non_one++;
4623 if (TREE_CODE (type) == ARRAY_TYPE)
4625 if (length == NULL_TREE
4626 && (TYPE_DOMAIN (type) == NULL_TREE
4627 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
4629 error_at (OMP_CLAUSE_LOCATION (c),
4630 "for unknown bound array type length expression must "
4631 "be specified");
4632 return error_mark_node;
4634 if (TREE_CODE (low_bound) == INTEGER_CST
4635 && tree_int_cst_sgn (low_bound) == -1)
4637 error_at (OMP_CLAUSE_LOCATION (c),
4638 "negative low bound in array section in %qs clause",
4639 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4640 return error_mark_node;
4642 if (length != NULL_TREE
4643 && TREE_CODE (length) == INTEGER_CST
4644 && tree_int_cst_sgn (length) == -1)
4646 error_at (OMP_CLAUSE_LOCATION (c),
4647 "negative length in array section in %qs clause",
4648 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4649 return error_mark_node;
4651 if (TYPE_DOMAIN (type)
4652 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
4653 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
4654 == INTEGER_CST)
4656 tree size
4657 = fold_convert (sizetype, TYPE_MAX_VALUE (TYPE_DOMAIN (type)));
4658 size = size_binop (PLUS_EXPR, size, size_one_node);
4659 if (TREE_CODE (low_bound) == INTEGER_CST)
4661 if (tree_int_cst_lt (size, low_bound))
4663 error_at (OMP_CLAUSE_LOCATION (c),
4664 "low bound %qE above array section size "
4665 "in %qs clause", low_bound,
4666 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4667 return error_mark_node;
4669 if (tree_int_cst_equal (size, low_bound))
4671 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
4672 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
4674 error_at (OMP_CLAUSE_LOCATION (c),
4675 "zero length array section in %qs clause",
4676 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4677 return error_mark_node;
4679 maybe_zero_len = true;
4681 else if (length == NULL_TREE
4682 && first_non_one == types.length ()
4683 && tree_int_cst_equal
4684 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
4685 low_bound))
4686 first_non_one++;
4688 else if (length == NULL_TREE)
4690 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
4691 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION)
4692 maybe_zero_len = true;
4693 if (first_non_one == types.length ())
4694 first_non_one++;
4696 if (length && TREE_CODE (length) == INTEGER_CST)
4698 if (tree_int_cst_lt (size, length))
4700 error_at (OMP_CLAUSE_LOCATION (c),
4701 "length %qE above array section size "
4702 "in %qs clause", length,
4703 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4704 return error_mark_node;
4706 if (TREE_CODE (low_bound) == INTEGER_CST)
4708 tree lbpluslen
4709 = size_binop (PLUS_EXPR,
4710 fold_convert (sizetype, low_bound),
4711 fold_convert (sizetype, length));
4712 if (TREE_CODE (lbpluslen) == INTEGER_CST
4713 && tree_int_cst_lt (size, lbpluslen))
4715 error_at (OMP_CLAUSE_LOCATION (c),
4716 "high bound %qE above array section size "
4717 "in %qs clause", lbpluslen,
4718 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4719 return error_mark_node;
4724 else if (length == NULL_TREE)
4726 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
4727 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION)
4728 maybe_zero_len = true;
4729 if (first_non_one == types.length ())
4730 first_non_one++;
4733 /* For [lb:] we will need to evaluate lb more than once. */
4734 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
4736 tree lb = cp_save_expr (low_bound);
4737 if (lb != low_bound)
4739 TREE_PURPOSE (t) = lb;
4740 low_bound = lb;
4744 else if (TREE_CODE (type) == POINTER_TYPE)
4746 if (length == NULL_TREE)
4748 error_at (OMP_CLAUSE_LOCATION (c),
4749 "for pointer type length expression must be specified");
4750 return error_mark_node;
4752 if (length != NULL_TREE
4753 && TREE_CODE (length) == INTEGER_CST
4754 && tree_int_cst_sgn (length) == -1)
4756 error_at (OMP_CLAUSE_LOCATION (c),
4757 "negative length in array section in %qs clause",
4758 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4759 return error_mark_node;
4761 /* If there is a pointer type anywhere but in the very first
4762 array-section-subscript, the array section can't be contiguous. */
4763 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
4764 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
4766 error_at (OMP_CLAUSE_LOCATION (c),
4767 "array section is not contiguous in %qs clause",
4768 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4769 return error_mark_node;
4772 else
4774 error_at (OMP_CLAUSE_LOCATION (c),
4775 "%qE does not have pointer or array type", ret);
4776 return error_mark_node;
4778 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
4779 types.safe_push (TREE_TYPE (ret));
4780 /* We will need to evaluate lb more than once. */
4781 tree lb = cp_save_expr (low_bound);
4782 if (lb != low_bound)
4784 TREE_PURPOSE (t) = lb;
4785 low_bound = lb;
4787 ret = grok_array_decl (OMP_CLAUSE_LOCATION (c), ret, low_bound, false);
4788 return ret;
4791 /* Handle array sections for clause C. */
4793 static bool
4794 handle_omp_array_sections (tree c, enum c_omp_region_type ort)
4796 bool maybe_zero_len = false;
4797 unsigned int first_non_one = 0;
4798 auto_vec<tree, 10> types;
4799 tree first = handle_omp_array_sections_1 (c, OMP_CLAUSE_DECL (c), types,
4800 maybe_zero_len, first_non_one,
4801 ort);
4802 if (first == error_mark_node)
4803 return true;
4804 if (first == NULL_TREE)
4805 return false;
4806 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
4808 tree t = OMP_CLAUSE_DECL (c);
4809 tree tem = NULL_TREE;
4810 if (processing_template_decl)
4811 return false;
4812 /* Need to evaluate side effects in the length expressions
4813 if any. */
4814 while (TREE_CODE (t) == TREE_LIST)
4816 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
4818 if (tem == NULL_TREE)
4819 tem = TREE_VALUE (t);
4820 else
4821 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
4822 TREE_VALUE (t), tem);
4824 t = TREE_CHAIN (t);
4826 if (tem)
4827 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
4828 OMP_CLAUSE_DECL (c) = first;
4830 else
4832 unsigned int num = types.length (), i;
4833 tree t, side_effects = NULL_TREE, size = NULL_TREE;
4834 tree condition = NULL_TREE;
4836 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
4837 maybe_zero_len = true;
4838 if (processing_template_decl && maybe_zero_len)
4839 return false;
4841 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
4842 t = TREE_CHAIN (t))
4844 tree low_bound = TREE_PURPOSE (t);
4845 tree length = TREE_VALUE (t);
4847 i--;
4848 if (low_bound
4849 && TREE_CODE (low_bound) == INTEGER_CST
4850 && TYPE_PRECISION (TREE_TYPE (low_bound))
4851 > TYPE_PRECISION (sizetype))
4852 low_bound = fold_convert (sizetype, low_bound);
4853 if (length
4854 && TREE_CODE (length) == INTEGER_CST
4855 && TYPE_PRECISION (TREE_TYPE (length))
4856 > TYPE_PRECISION (sizetype))
4857 length = fold_convert (sizetype, length);
4858 if (low_bound == NULL_TREE)
4859 low_bound = integer_zero_node;
4860 if (!maybe_zero_len && i > first_non_one)
4862 if (integer_nonzerop (low_bound))
4863 goto do_warn_noncontiguous;
4864 if (length != NULL_TREE
4865 && TREE_CODE (length) == INTEGER_CST
4866 && TYPE_DOMAIN (types[i])
4867 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
4868 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
4869 == INTEGER_CST)
4871 tree size;
4872 size = size_binop (PLUS_EXPR,
4873 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
4874 size_one_node);
4875 if (!tree_int_cst_equal (length, size))
4877 do_warn_noncontiguous:
4878 error_at (OMP_CLAUSE_LOCATION (c),
4879 "array section is not contiguous in %qs "
4880 "clause",
4881 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4882 return true;
4885 if (!processing_template_decl
4886 && length != NULL_TREE
4887 && TREE_SIDE_EFFECTS (length))
4889 if (side_effects == NULL_TREE)
4890 side_effects = length;
4891 else
4892 side_effects = build2 (COMPOUND_EXPR,
4893 TREE_TYPE (side_effects),
4894 length, side_effects);
4897 else if (processing_template_decl)
4898 continue;
4899 else
4901 tree l;
4903 if (i > first_non_one
4904 && ((length && integer_nonzerop (length))
4905 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION))
4906 continue;
4907 if (length)
4908 l = fold_convert (sizetype, length);
4909 else
4911 l = size_binop (PLUS_EXPR,
4912 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
4913 size_one_node);
4914 l = size_binop (MINUS_EXPR, l,
4915 fold_convert (sizetype, low_bound));
4917 if (i > first_non_one)
4919 l = fold_build2 (NE_EXPR, boolean_type_node, l,
4920 size_zero_node);
4921 if (condition == NULL_TREE)
4922 condition = l;
4923 else
4924 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
4925 l, condition);
4927 else if (size == NULL_TREE)
4929 size = size_in_bytes (TREE_TYPE (types[i]));
4930 tree eltype = TREE_TYPE (types[num - 1]);
4931 while (TREE_CODE (eltype) == ARRAY_TYPE)
4932 eltype = TREE_TYPE (eltype);
4933 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
4934 size = size_binop (EXACT_DIV_EXPR, size,
4935 size_in_bytes (eltype));
4936 size = size_binop (MULT_EXPR, size, l);
4937 if (condition)
4938 size = fold_build3 (COND_EXPR, sizetype, condition,
4939 size, size_zero_node);
4941 else
4942 size = size_binop (MULT_EXPR, size, l);
4945 if (!processing_template_decl)
4947 if (side_effects)
4948 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
4949 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
4951 size = size_binop (MINUS_EXPR, size, size_one_node);
4952 tree index_type = build_index_type (size);
4953 tree eltype = TREE_TYPE (first);
4954 while (TREE_CODE (eltype) == ARRAY_TYPE)
4955 eltype = TREE_TYPE (eltype);
4956 tree type = build_array_type (eltype, index_type);
4957 tree ptype = build_pointer_type (eltype);
4958 if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE
4959 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (t))))
4960 t = convert_from_reference (t);
4961 else if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
4962 t = build_fold_addr_expr (t);
4963 tree t2 = build_fold_addr_expr (first);
4964 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
4965 ptrdiff_type_node, t2);
4966 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
4967 ptrdiff_type_node, t2,
4968 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
4969 ptrdiff_type_node, t));
4970 if (tree_fits_shwi_p (t2))
4971 t = build2 (MEM_REF, type, t,
4972 build_int_cst (ptype, tree_to_shwi (t2)));
4973 else
4975 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
4976 sizetype, t2);
4977 t = build2_loc (OMP_CLAUSE_LOCATION (c), POINTER_PLUS_EXPR,
4978 TREE_TYPE (t), t, t2);
4979 t = build2 (MEM_REF, type, t, build_int_cst (ptype, 0));
4981 OMP_CLAUSE_DECL (c) = t;
4982 return false;
4984 OMP_CLAUSE_DECL (c) = first;
4985 OMP_CLAUSE_SIZE (c) = size;
4986 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
4987 || (TREE_CODE (t) == COMPONENT_REF
4988 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE))
4989 return false;
4990 if (ort == C_ORT_OMP || ort == C_ORT_ACC)
4991 switch (OMP_CLAUSE_MAP_KIND (c))
4993 case GOMP_MAP_ALLOC:
4994 case GOMP_MAP_TO:
4995 case GOMP_MAP_FROM:
4996 case GOMP_MAP_TOFROM:
4997 case GOMP_MAP_ALWAYS_TO:
4998 case GOMP_MAP_ALWAYS_FROM:
4999 case GOMP_MAP_ALWAYS_TOFROM:
5000 case GOMP_MAP_RELEASE:
5001 case GOMP_MAP_DELETE:
5002 case GOMP_MAP_FORCE_TO:
5003 case GOMP_MAP_FORCE_FROM:
5004 case GOMP_MAP_FORCE_TOFROM:
5005 case GOMP_MAP_FORCE_PRESENT:
5006 OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (c) = 1;
5007 break;
5008 default:
5009 break;
5011 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
5012 OMP_CLAUSE_MAP);
5013 if ((ort & C_ORT_OMP_DECLARE_SIMD) != C_ORT_OMP && ort != C_ORT_ACC)
5014 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_POINTER);
5015 else if (TREE_CODE (t) == COMPONENT_REF)
5016 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ALWAYS_POINTER);
5017 else if (REFERENCE_REF_P (t)
5018 && TREE_CODE (TREE_OPERAND (t, 0)) == COMPONENT_REF)
5020 t = TREE_OPERAND (t, 0);
5021 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ALWAYS_POINTER);
5023 else
5024 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_FIRSTPRIVATE_POINTER);
5025 if (OMP_CLAUSE_MAP_KIND (c2) != GOMP_MAP_FIRSTPRIVATE_POINTER
5026 && !cxx_mark_addressable (t))
5027 return false;
5028 OMP_CLAUSE_DECL (c2) = t;
5029 t = build_fold_addr_expr (first);
5030 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
5031 ptrdiff_type_node, t);
5032 tree ptr = OMP_CLAUSE_DECL (c2);
5033 ptr = convert_from_reference (ptr);
5034 if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
5035 ptr = build_fold_addr_expr (ptr);
5036 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
5037 ptrdiff_type_node, t,
5038 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
5039 ptrdiff_type_node, ptr));
5040 OMP_CLAUSE_SIZE (c2) = t;
5041 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
5042 OMP_CLAUSE_CHAIN (c) = c2;
5043 ptr = OMP_CLAUSE_DECL (c2);
5044 if (OMP_CLAUSE_MAP_KIND (c2) != GOMP_MAP_FIRSTPRIVATE_POINTER
5045 && TREE_CODE (TREE_TYPE (ptr)) == REFERENCE_TYPE
5046 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (ptr))))
5048 tree c3 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
5049 OMP_CLAUSE_MAP);
5050 OMP_CLAUSE_SET_MAP_KIND (c3, OMP_CLAUSE_MAP_KIND (c2));
5051 OMP_CLAUSE_DECL (c3) = ptr;
5052 if (OMP_CLAUSE_MAP_KIND (c2) == GOMP_MAP_ALWAYS_POINTER)
5053 OMP_CLAUSE_DECL (c2) = build_simple_mem_ref (ptr);
5054 else
5055 OMP_CLAUSE_DECL (c2) = convert_from_reference (ptr);
5056 OMP_CLAUSE_SIZE (c3) = size_zero_node;
5057 OMP_CLAUSE_CHAIN (c3) = OMP_CLAUSE_CHAIN (c2);
5058 OMP_CLAUSE_CHAIN (c2) = c3;
5062 return false;
5065 /* Return identifier to look up for omp declare reduction. */
5067 tree
5068 omp_reduction_id (enum tree_code reduction_code, tree reduction_id, tree type)
5070 const char *p = NULL;
5071 const char *m = NULL;
5072 switch (reduction_code)
5074 case PLUS_EXPR:
5075 case MULT_EXPR:
5076 case MINUS_EXPR:
5077 case BIT_AND_EXPR:
5078 case BIT_XOR_EXPR:
5079 case BIT_IOR_EXPR:
5080 case TRUTH_ANDIF_EXPR:
5081 case TRUTH_ORIF_EXPR:
5082 reduction_id = ovl_op_identifier (false, reduction_code);
5083 break;
5084 case MIN_EXPR:
5085 p = "min";
5086 break;
5087 case MAX_EXPR:
5088 p = "max";
5089 break;
5090 default:
5091 break;
5094 if (p == NULL)
5096 if (TREE_CODE (reduction_id) != IDENTIFIER_NODE)
5097 return error_mark_node;
5098 p = IDENTIFIER_POINTER (reduction_id);
5101 if (type != NULL_TREE)
5102 m = mangle_type_string (TYPE_MAIN_VARIANT (type));
5104 const char prefix[] = "omp declare reduction ";
5105 size_t lenp = sizeof (prefix);
5106 if (strncmp (p, prefix, lenp - 1) == 0)
5107 lenp = 1;
5108 size_t len = strlen (p);
5109 size_t lenm = m ? strlen (m) + 1 : 0;
5110 char *name = XALLOCAVEC (char, lenp + len + lenm);
5111 if (lenp > 1)
5112 memcpy (name, prefix, lenp - 1);
5113 memcpy (name + lenp - 1, p, len + 1);
5114 if (m)
5116 name[lenp + len - 1] = '~';
5117 memcpy (name + lenp + len, m, lenm);
5119 return get_identifier (name);
5122 /* Lookup OpenMP UDR ID for TYPE, return the corresponding artificial
5123 FUNCTION_DECL or NULL_TREE if not found. */
5125 static tree
5126 omp_reduction_lookup (location_t loc, tree id, tree type, tree *baselinkp,
5127 vec<tree> *ambiguousp)
5129 tree orig_id = id;
5130 tree baselink = NULL_TREE;
5131 if (identifier_p (id))
5133 cp_id_kind idk;
5134 bool nonint_cst_expression_p;
5135 const char *error_msg;
5136 id = omp_reduction_id (ERROR_MARK, id, type);
5137 tree decl = lookup_name (id);
5138 if (decl == NULL_TREE)
5139 decl = error_mark_node;
5140 id = finish_id_expression (id, decl, NULL_TREE, &idk, false, true,
5141 &nonint_cst_expression_p, false, true, false,
5142 false, &error_msg, loc);
5143 if (idk == CP_ID_KIND_UNQUALIFIED
5144 && identifier_p (id))
5146 vec<tree, va_gc> *args = NULL;
5147 vec_safe_push (args, build_reference_type (type));
5148 id = perform_koenig_lookup (id, args, tf_none);
5151 else if (TREE_CODE (id) == SCOPE_REF)
5152 id = lookup_qualified_name (TREE_OPERAND (id, 0),
5153 omp_reduction_id (ERROR_MARK,
5154 TREE_OPERAND (id, 1),
5155 type),
5156 false, false);
5157 tree fns = id;
5158 id = NULL_TREE;
5159 if (fns && is_overloaded_fn (fns))
5161 for (lkp_iterator iter (get_fns (fns)); iter; ++iter)
5163 tree fndecl = *iter;
5164 if (TREE_CODE (fndecl) == FUNCTION_DECL)
5166 tree argtype = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
5167 if (same_type_p (TREE_TYPE (argtype), type))
5169 id = fndecl;
5170 break;
5175 if (id && BASELINK_P (fns))
5177 if (baselinkp)
5178 *baselinkp = fns;
5179 else
5180 baselink = fns;
5184 if (!id && CLASS_TYPE_P (type) && TYPE_BINFO (type))
5186 vec<tree> ambiguous = vNULL;
5187 tree binfo = TYPE_BINFO (type), base_binfo, ret = NULL_TREE;
5188 unsigned int ix;
5189 if (ambiguousp == NULL)
5190 ambiguousp = &ambiguous;
5191 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
5193 id = omp_reduction_lookup (loc, orig_id, BINFO_TYPE (base_binfo),
5194 baselinkp ? baselinkp : &baselink,
5195 ambiguousp);
5196 if (id == NULL_TREE)
5197 continue;
5198 if (!ambiguousp->is_empty ())
5199 ambiguousp->safe_push (id);
5200 else if (ret != NULL_TREE)
5202 ambiguousp->safe_push (ret);
5203 ambiguousp->safe_push (id);
5204 ret = NULL_TREE;
5206 else
5207 ret = id;
5209 if (ambiguousp != &ambiguous)
5210 return ret;
5211 if (!ambiguous.is_empty ())
5213 const char *str = _("candidates are:");
5214 unsigned int idx;
5215 tree udr;
5216 error_at (loc, "user defined reduction lookup is ambiguous");
5217 FOR_EACH_VEC_ELT (ambiguous, idx, udr)
5219 inform (DECL_SOURCE_LOCATION (udr), "%s %#qD", str, udr);
5220 if (idx == 0)
5221 str = get_spaces (str);
5223 ambiguous.release ();
5224 ret = error_mark_node;
5225 baselink = NULL_TREE;
5227 id = ret;
5229 if (id && baselink)
5230 perform_or_defer_access_check (BASELINK_BINFO (baselink),
5231 id, id, tf_warning_or_error);
5232 return id;
5235 /* Helper function for cp_parser_omp_declare_reduction_exprs
5236 and tsubst_omp_udr.
5237 Remove CLEANUP_STMT for data (omp_priv variable).
5238 Also append INIT_EXPR for DECL_INITIAL of omp_priv after its
5239 DECL_EXPR. */
5241 tree
5242 cp_remove_omp_priv_cleanup_stmt (tree *tp, int *walk_subtrees, void *data)
5244 if (TYPE_P (*tp))
5245 *walk_subtrees = 0;
5246 else if (TREE_CODE (*tp) == CLEANUP_STMT && CLEANUP_DECL (*tp) == (tree) data)
5247 *tp = CLEANUP_BODY (*tp);
5248 else if (TREE_CODE (*tp) == DECL_EXPR)
5250 tree decl = DECL_EXPR_DECL (*tp);
5251 if (!processing_template_decl
5252 && decl == (tree) data
5253 && DECL_INITIAL (decl)
5254 && DECL_INITIAL (decl) != error_mark_node)
5256 tree list = NULL_TREE;
5257 append_to_statement_list_force (*tp, &list);
5258 tree init_expr = build2 (INIT_EXPR, void_type_node,
5259 decl, DECL_INITIAL (decl));
5260 DECL_INITIAL (decl) = NULL_TREE;
5261 append_to_statement_list_force (init_expr, &list);
5262 *tp = list;
5265 return NULL_TREE;
5268 /* Data passed from cp_check_omp_declare_reduction to
5269 cp_check_omp_declare_reduction_r. */
5271 struct cp_check_omp_declare_reduction_data
5273 location_t loc;
5274 tree stmts[7];
5275 bool combiner_p;
5278 /* Helper function for cp_check_omp_declare_reduction, called via
5279 cp_walk_tree. */
5281 static tree
5282 cp_check_omp_declare_reduction_r (tree *tp, int *, void *data)
5284 struct cp_check_omp_declare_reduction_data *udr_data
5285 = (struct cp_check_omp_declare_reduction_data *) data;
5286 if (SSA_VAR_P (*tp)
5287 && !DECL_ARTIFICIAL (*tp)
5288 && *tp != DECL_EXPR_DECL (udr_data->stmts[udr_data->combiner_p ? 0 : 3])
5289 && *tp != DECL_EXPR_DECL (udr_data->stmts[udr_data->combiner_p ? 1 : 4]))
5291 location_t loc = udr_data->loc;
5292 if (udr_data->combiner_p)
5293 error_at (loc, "%<#pragma omp declare reduction%> combiner refers to "
5294 "variable %qD which is not %<omp_out%> nor %<omp_in%>",
5295 *tp);
5296 else
5297 error_at (loc, "%<#pragma omp declare reduction%> initializer refers "
5298 "to variable %qD which is not %<omp_priv%> nor "
5299 "%<omp_orig%>",
5300 *tp);
5301 return *tp;
5303 return NULL_TREE;
5306 /* Diagnose violation of OpenMP #pragma omp declare reduction restrictions. */
5308 void
5309 cp_check_omp_declare_reduction (tree udr)
5311 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (udr)));
5312 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
5313 type = TREE_TYPE (type);
5314 int i;
5315 location_t loc = DECL_SOURCE_LOCATION (udr);
5317 if (type == error_mark_node)
5318 return;
5319 if (ARITHMETIC_TYPE_P (type))
5321 static enum tree_code predef_codes[]
5322 = { PLUS_EXPR, MULT_EXPR, MINUS_EXPR, BIT_AND_EXPR, BIT_XOR_EXPR,
5323 BIT_IOR_EXPR, TRUTH_ANDIF_EXPR, TRUTH_ORIF_EXPR };
5324 for (i = 0; i < 8; i++)
5326 tree id = omp_reduction_id (predef_codes[i], NULL_TREE, NULL_TREE);
5327 const char *n1 = IDENTIFIER_POINTER (DECL_NAME (udr));
5328 const char *n2 = IDENTIFIER_POINTER (id);
5329 if (strncmp (n1, n2, IDENTIFIER_LENGTH (id)) == 0
5330 && (n1[IDENTIFIER_LENGTH (id)] == '~'
5331 || n1[IDENTIFIER_LENGTH (id)] == '\0'))
5332 break;
5335 if (i == 8
5336 && TREE_CODE (type) != COMPLEX_EXPR)
5338 const char prefix_minmax[] = "omp declare reduction m";
5339 size_t prefix_size = sizeof (prefix_minmax) - 1;
5340 const char *n = IDENTIFIER_POINTER (DECL_NAME (udr));
5341 if (strncmp (IDENTIFIER_POINTER (DECL_NAME (udr)),
5342 prefix_minmax, prefix_size) == 0
5343 && ((n[prefix_size] == 'i' && n[prefix_size + 1] == 'n')
5344 || (n[prefix_size] == 'a' && n[prefix_size + 1] == 'x'))
5345 && (n[prefix_size + 2] == '~' || n[prefix_size + 2] == '\0'))
5346 i = 0;
5348 if (i < 8)
5350 error_at (loc, "predeclared arithmetic type %qT in "
5351 "%<#pragma omp declare reduction%>", type);
5352 return;
5355 else if (TREE_CODE (type) == FUNCTION_TYPE
5356 || TREE_CODE (type) == METHOD_TYPE
5357 || TREE_CODE (type) == ARRAY_TYPE)
5359 error_at (loc, "function or array type %qT in "
5360 "%<#pragma omp declare reduction%>", type);
5361 return;
5363 else if (TREE_CODE (type) == REFERENCE_TYPE)
5365 error_at (loc, "reference type %qT in %<#pragma omp declare reduction%>",
5366 type);
5367 return;
5369 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
5371 error_at (loc, "const, volatile or __restrict qualified type %qT in "
5372 "%<#pragma omp declare reduction%>", type);
5373 return;
5376 tree body = DECL_SAVED_TREE (udr);
5377 if (body == NULL_TREE || TREE_CODE (body) != STATEMENT_LIST)
5378 return;
5380 tree_stmt_iterator tsi;
5381 struct cp_check_omp_declare_reduction_data data;
5382 memset (data.stmts, 0, sizeof data.stmts);
5383 for (i = 0, tsi = tsi_start (body);
5384 i < 7 && !tsi_end_p (tsi);
5385 i++, tsi_next (&tsi))
5386 data.stmts[i] = tsi_stmt (tsi);
5387 data.loc = loc;
5388 gcc_assert (tsi_end_p (tsi));
5389 if (i >= 3)
5391 gcc_assert (TREE_CODE (data.stmts[0]) == DECL_EXPR
5392 && TREE_CODE (data.stmts[1]) == DECL_EXPR);
5393 if (TREE_NO_WARNING (DECL_EXPR_DECL (data.stmts[0])))
5394 return;
5395 data.combiner_p = true;
5396 if (cp_walk_tree (&data.stmts[2], cp_check_omp_declare_reduction_r,
5397 &data, NULL))
5398 TREE_NO_WARNING (DECL_EXPR_DECL (data.stmts[0])) = 1;
5400 if (i >= 6)
5402 gcc_assert (TREE_CODE (data.stmts[3]) == DECL_EXPR
5403 && TREE_CODE (data.stmts[4]) == DECL_EXPR);
5404 data.combiner_p = false;
5405 if (cp_walk_tree (&data.stmts[5], cp_check_omp_declare_reduction_r,
5406 &data, NULL)
5407 || cp_walk_tree (&DECL_INITIAL (DECL_EXPR_DECL (data.stmts[3])),
5408 cp_check_omp_declare_reduction_r, &data, NULL))
5409 TREE_NO_WARNING (DECL_EXPR_DECL (data.stmts[0])) = 1;
5410 if (i == 7)
5411 gcc_assert (TREE_CODE (data.stmts[6]) == DECL_EXPR);
5415 /* Helper function of finish_omp_clauses. Clone STMT as if we were making
5416 an inline call. But, remap
5417 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
5418 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
5420 static tree
5421 clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
5422 tree decl, tree placeholder)
5424 copy_body_data id;
5425 hash_map<tree, tree> decl_map;
5427 decl_map.put (omp_decl1, placeholder);
5428 decl_map.put (omp_decl2, decl);
5429 memset (&id, 0, sizeof (id));
5430 id.src_fn = DECL_CONTEXT (omp_decl1);
5431 id.dst_fn = current_function_decl;
5432 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
5433 id.decl_map = &decl_map;
5435 id.copy_decl = copy_decl_no_change;
5436 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
5437 id.transform_new_cfg = true;
5438 id.transform_return_to_modify = false;
5439 id.transform_lang_insert_block = NULL;
5440 id.eh_lp_nr = 0;
5441 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
5442 return stmt;
5445 /* Helper function of finish_omp_clauses, called via cp_walk_tree.
5446 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
5448 static tree
5449 find_omp_placeholder_r (tree *tp, int *, void *data)
5451 if (*tp == (tree) data)
5452 return *tp;
5453 return NULL_TREE;
5456 /* Helper function of finish_omp_clauses. Handle OMP_CLAUSE_REDUCTION C.
5457 Return true if there is some error and the clause should be removed. */
5459 static bool
5460 finish_omp_reduction_clause (tree c, bool *need_default_ctor, bool *need_dtor)
5462 tree t = OMP_CLAUSE_DECL (c);
5463 bool predefined = false;
5464 if (TREE_CODE (t) == TREE_LIST)
5466 gcc_assert (processing_template_decl);
5467 return false;
5469 tree type = TREE_TYPE (t);
5470 if (TREE_CODE (t) == MEM_REF)
5471 type = TREE_TYPE (type);
5472 if (TREE_CODE (type) == REFERENCE_TYPE)
5473 type = TREE_TYPE (type);
5474 if (TREE_CODE (type) == ARRAY_TYPE)
5476 tree oatype = type;
5477 gcc_assert (TREE_CODE (t) != MEM_REF);
5478 while (TREE_CODE (type) == ARRAY_TYPE)
5479 type = TREE_TYPE (type);
5480 if (!processing_template_decl)
5482 t = require_complete_type (t);
5483 if (t == error_mark_node)
5484 return true;
5485 tree size = size_binop (EXACT_DIV_EXPR, TYPE_SIZE_UNIT (oatype),
5486 TYPE_SIZE_UNIT (type));
5487 if (integer_zerop (size))
5489 error ("%qE in %<reduction%> clause is a zero size array",
5490 omp_clause_printable_decl (t));
5491 return true;
5493 size = size_binop (MINUS_EXPR, size, size_one_node);
5494 tree index_type = build_index_type (size);
5495 tree atype = build_array_type (type, index_type);
5496 tree ptype = build_pointer_type (type);
5497 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
5498 t = build_fold_addr_expr (t);
5499 t = build2 (MEM_REF, atype, t, build_int_cst (ptype, 0));
5500 OMP_CLAUSE_DECL (c) = t;
5503 if (type == error_mark_node)
5504 return true;
5505 else if (ARITHMETIC_TYPE_P (type))
5506 switch (OMP_CLAUSE_REDUCTION_CODE (c))
5508 case PLUS_EXPR:
5509 case MULT_EXPR:
5510 case MINUS_EXPR:
5511 predefined = true;
5512 break;
5513 case MIN_EXPR:
5514 case MAX_EXPR:
5515 if (TREE_CODE (type) == COMPLEX_TYPE)
5516 break;
5517 predefined = true;
5518 break;
5519 case BIT_AND_EXPR:
5520 case BIT_IOR_EXPR:
5521 case BIT_XOR_EXPR:
5522 if (FLOAT_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
5523 break;
5524 predefined = true;
5525 break;
5526 case TRUTH_ANDIF_EXPR:
5527 case TRUTH_ORIF_EXPR:
5528 if (FLOAT_TYPE_P (type))
5529 break;
5530 predefined = true;
5531 break;
5532 default:
5533 break;
5535 else if (TYPE_READONLY (type))
5537 error ("%qE has const type for %<reduction%>",
5538 omp_clause_printable_decl (t));
5539 return true;
5541 else if (!processing_template_decl)
5543 t = require_complete_type (t);
5544 if (t == error_mark_node)
5545 return true;
5546 OMP_CLAUSE_DECL (c) = t;
5549 if (predefined)
5551 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = NULL_TREE;
5552 return false;
5554 else if (processing_template_decl)
5555 return false;
5557 tree id = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
5559 type = TYPE_MAIN_VARIANT (type);
5560 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = NULL_TREE;
5561 if (id == NULL_TREE)
5562 id = omp_reduction_id (OMP_CLAUSE_REDUCTION_CODE (c),
5563 NULL_TREE, NULL_TREE);
5564 id = omp_reduction_lookup (OMP_CLAUSE_LOCATION (c), id, type, NULL, NULL);
5565 if (id)
5567 if (id == error_mark_node)
5568 return true;
5569 mark_used (id);
5570 tree body = DECL_SAVED_TREE (id);
5571 if (!body)
5572 return true;
5573 if (TREE_CODE (body) == STATEMENT_LIST)
5575 tree_stmt_iterator tsi;
5576 tree placeholder = NULL_TREE, decl_placeholder = NULL_TREE;
5577 int i;
5578 tree stmts[7];
5579 tree atype = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (id)));
5580 atype = TREE_TYPE (atype);
5581 bool need_static_cast = !same_type_p (type, atype);
5582 memset (stmts, 0, sizeof stmts);
5583 for (i = 0, tsi = tsi_start (body);
5584 i < 7 && !tsi_end_p (tsi);
5585 i++, tsi_next (&tsi))
5586 stmts[i] = tsi_stmt (tsi);
5587 gcc_assert (tsi_end_p (tsi));
5589 if (i >= 3)
5591 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
5592 && TREE_CODE (stmts[1]) == DECL_EXPR);
5593 placeholder = build_lang_decl (VAR_DECL, NULL_TREE, type);
5594 DECL_ARTIFICIAL (placeholder) = 1;
5595 DECL_IGNORED_P (placeholder) = 1;
5596 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
5597 if (TREE_CODE (t) == MEM_REF)
5599 decl_placeholder = build_lang_decl (VAR_DECL, NULL_TREE,
5600 type);
5601 DECL_ARTIFICIAL (decl_placeholder) = 1;
5602 DECL_IGNORED_P (decl_placeholder) = 1;
5603 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c) = decl_placeholder;
5605 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[0])))
5606 cxx_mark_addressable (placeholder);
5607 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[1]))
5608 && TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c)))
5609 != REFERENCE_TYPE)
5610 cxx_mark_addressable (decl_placeholder ? decl_placeholder
5611 : OMP_CLAUSE_DECL (c));
5612 tree omp_out = placeholder;
5613 tree omp_in = decl_placeholder ? decl_placeholder
5614 : convert_from_reference (OMP_CLAUSE_DECL (c));
5615 if (need_static_cast)
5617 tree rtype = build_reference_type (atype);
5618 omp_out = build_static_cast (rtype, omp_out,
5619 tf_warning_or_error);
5620 omp_in = build_static_cast (rtype, omp_in,
5621 tf_warning_or_error);
5622 if (omp_out == error_mark_node || omp_in == error_mark_node)
5623 return true;
5624 omp_out = convert_from_reference (omp_out);
5625 omp_in = convert_from_reference (omp_in);
5627 OMP_CLAUSE_REDUCTION_MERGE (c)
5628 = clone_omp_udr (stmts[2], DECL_EXPR_DECL (stmts[0]),
5629 DECL_EXPR_DECL (stmts[1]), omp_in, omp_out);
5631 if (i >= 6)
5633 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
5634 && TREE_CODE (stmts[4]) == DECL_EXPR);
5635 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[3])))
5636 cxx_mark_addressable (decl_placeholder ? decl_placeholder
5637 : OMP_CLAUSE_DECL (c));
5638 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[4])))
5639 cxx_mark_addressable (placeholder);
5640 tree omp_priv = decl_placeholder ? decl_placeholder
5641 : convert_from_reference (OMP_CLAUSE_DECL (c));
5642 tree omp_orig = placeholder;
5643 if (need_static_cast)
5645 if (i == 7)
5647 error_at (OMP_CLAUSE_LOCATION (c),
5648 "user defined reduction with constructor "
5649 "initializer for base class %qT", atype);
5650 return true;
5652 tree rtype = build_reference_type (atype);
5653 omp_priv = build_static_cast (rtype, omp_priv,
5654 tf_warning_or_error);
5655 omp_orig = build_static_cast (rtype, omp_orig,
5656 tf_warning_or_error);
5657 if (omp_priv == error_mark_node
5658 || omp_orig == error_mark_node)
5659 return true;
5660 omp_priv = convert_from_reference (omp_priv);
5661 omp_orig = convert_from_reference (omp_orig);
5663 if (i == 6)
5664 *need_default_ctor = true;
5665 OMP_CLAUSE_REDUCTION_INIT (c)
5666 = clone_omp_udr (stmts[5], DECL_EXPR_DECL (stmts[4]),
5667 DECL_EXPR_DECL (stmts[3]),
5668 omp_priv, omp_orig);
5669 if (cp_walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
5670 find_omp_placeholder_r, placeholder, NULL))
5671 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
5673 else if (i >= 3)
5675 if (CLASS_TYPE_P (type) && !pod_type_p (type))
5676 *need_default_ctor = true;
5677 else
5679 tree init;
5680 tree v = decl_placeholder ? decl_placeholder
5681 : convert_from_reference (t);
5682 if (AGGREGATE_TYPE_P (TREE_TYPE (v)))
5683 init = build_constructor (TREE_TYPE (v), NULL);
5684 else
5685 init = fold_convert (TREE_TYPE (v), integer_zero_node);
5686 OMP_CLAUSE_REDUCTION_INIT (c)
5687 = build2 (INIT_EXPR, TREE_TYPE (v), v, init);
5692 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
5693 *need_dtor = true;
5694 else
5696 error ("user defined reduction not found for %qE",
5697 omp_clause_printable_decl (t));
5698 return true;
5700 if (TREE_CODE (OMP_CLAUSE_DECL (c)) == MEM_REF)
5701 gcc_assert (TYPE_SIZE_UNIT (type)
5702 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST);
5703 return false;
5706 /* Called from finish_struct_1. linear(this) or linear(this:step)
5707 clauses might not be finalized yet because the class has been incomplete
5708 when parsing #pragma omp declare simd methods. Fix those up now. */
5710 void
5711 finish_omp_declare_simd_methods (tree t)
5713 if (processing_template_decl)
5714 return;
5716 for (tree x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
5718 if (TREE_CODE (TREE_TYPE (x)) != METHOD_TYPE)
5719 continue;
5720 tree ods = lookup_attribute ("omp declare simd", DECL_ATTRIBUTES (x));
5721 if (!ods || !TREE_VALUE (ods))
5722 continue;
5723 for (tree c = TREE_VALUE (TREE_VALUE (ods)); c; c = OMP_CLAUSE_CHAIN (c))
5724 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
5725 && integer_zerop (OMP_CLAUSE_DECL (c))
5726 && OMP_CLAUSE_LINEAR_STEP (c)
5727 && TREE_CODE (TREE_TYPE (OMP_CLAUSE_LINEAR_STEP (c)))
5728 == POINTER_TYPE)
5730 tree s = OMP_CLAUSE_LINEAR_STEP (c);
5731 s = fold_convert_loc (OMP_CLAUSE_LOCATION (c), sizetype, s);
5732 s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MULT_EXPR,
5733 sizetype, s, TYPE_SIZE_UNIT (t));
5734 OMP_CLAUSE_LINEAR_STEP (c) = s;
5739 /* Adjust sink depend clause to take into account pointer offsets.
5741 Return TRUE if there was a problem processing the offset, and the
5742 whole clause should be removed. */
5744 static bool
5745 cp_finish_omp_clause_depend_sink (tree sink_clause)
5747 tree t = OMP_CLAUSE_DECL (sink_clause);
5748 gcc_assert (TREE_CODE (t) == TREE_LIST);
5750 /* Make sure we don't adjust things twice for templates. */
5751 if (processing_template_decl)
5752 return false;
5754 for (; t; t = TREE_CHAIN (t))
5756 tree decl = TREE_VALUE (t);
5757 if (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
5759 tree offset = TREE_PURPOSE (t);
5760 bool neg = wi::neg_p (wi::to_wide (offset));
5761 offset = fold_unary (ABS_EXPR, TREE_TYPE (offset), offset);
5762 decl = mark_rvalue_use (decl);
5763 decl = convert_from_reference (decl);
5764 tree t2 = pointer_int_sum (OMP_CLAUSE_LOCATION (sink_clause),
5765 neg ? MINUS_EXPR : PLUS_EXPR,
5766 decl, offset);
5767 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (sink_clause),
5768 MINUS_EXPR, sizetype,
5769 fold_convert (sizetype, t2),
5770 fold_convert (sizetype, decl));
5771 if (t2 == error_mark_node)
5772 return true;
5773 TREE_PURPOSE (t) = t2;
5776 return false;
5779 /* For all elements of CLAUSES, validate them vs OpenMP constraints.
5780 Remove any elements from the list that are invalid. */
5782 tree
5783 finish_omp_clauses (tree clauses, enum c_omp_region_type ort)
5785 bitmap_head generic_head, firstprivate_head, lastprivate_head;
5786 bitmap_head aligned_head, map_head, map_field_head, oacc_reduction_head;
5787 tree c, t, *pc;
5788 tree safelen = NULL_TREE;
5789 bool branch_seen = false;
5790 bool copyprivate_seen = false;
5791 bool ordered_seen = false;
5792 bool oacc_async = false;
5794 bitmap_obstack_initialize (NULL);
5795 bitmap_initialize (&generic_head, &bitmap_default_obstack);
5796 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
5797 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
5798 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
5799 bitmap_initialize (&map_head, &bitmap_default_obstack);
5800 bitmap_initialize (&map_field_head, &bitmap_default_obstack);
5801 bitmap_initialize (&oacc_reduction_head, &bitmap_default_obstack);
5803 if (ort & C_ORT_ACC)
5804 for (c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
5805 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ASYNC)
5807 oacc_async = true;
5808 break;
5811 for (pc = &clauses, c = clauses; c ; c = *pc)
5813 bool remove = false;
5814 bool field_ok = false;
5816 switch (OMP_CLAUSE_CODE (c))
5818 case OMP_CLAUSE_SHARED:
5819 field_ok = ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP);
5820 goto check_dup_generic;
5821 case OMP_CLAUSE_PRIVATE:
5822 field_ok = ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP);
5823 goto check_dup_generic;
5824 case OMP_CLAUSE_REDUCTION:
5825 field_ok = ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP);
5826 t = OMP_CLAUSE_DECL (c);
5827 if (TREE_CODE (t) == TREE_LIST)
5829 if (handle_omp_array_sections (c, ort))
5831 remove = true;
5832 break;
5834 if (TREE_CODE (t) == TREE_LIST)
5836 while (TREE_CODE (t) == TREE_LIST)
5837 t = TREE_CHAIN (t);
5839 else
5841 gcc_assert (TREE_CODE (t) == MEM_REF);
5842 t = TREE_OPERAND (t, 0);
5843 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
5844 t = TREE_OPERAND (t, 0);
5845 if (TREE_CODE (t) == ADDR_EXPR
5846 || TREE_CODE (t) == INDIRECT_REF)
5847 t = TREE_OPERAND (t, 0);
5849 tree n = omp_clause_decl_field (t);
5850 if (n)
5851 t = n;
5852 goto check_dup_generic_t;
5854 if (oacc_async)
5855 cxx_mark_addressable (t);
5856 goto check_dup_generic;
5857 case OMP_CLAUSE_COPYPRIVATE:
5858 copyprivate_seen = true;
5859 field_ok = ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP);
5860 goto check_dup_generic;
5861 case OMP_CLAUSE_COPYIN:
5862 goto check_dup_generic;
5863 case OMP_CLAUSE_LINEAR:
5864 field_ok = ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP);
5865 t = OMP_CLAUSE_DECL (c);
5866 if (ort != C_ORT_OMP_DECLARE_SIMD
5867 && OMP_CLAUSE_LINEAR_KIND (c) != OMP_CLAUSE_LINEAR_DEFAULT)
5869 error_at (OMP_CLAUSE_LOCATION (c),
5870 "modifier should not be specified in %<linear%> "
5871 "clause on %<simd%> or %<for%> constructs");
5872 OMP_CLAUSE_LINEAR_KIND (c) = OMP_CLAUSE_LINEAR_DEFAULT;
5874 if ((VAR_P (t) || TREE_CODE (t) == PARM_DECL)
5875 && !type_dependent_expression_p (t))
5877 tree type = TREE_TYPE (t);
5878 if ((OMP_CLAUSE_LINEAR_KIND (c) == OMP_CLAUSE_LINEAR_REF
5879 || OMP_CLAUSE_LINEAR_KIND (c) == OMP_CLAUSE_LINEAR_UVAL)
5880 && TREE_CODE (type) != REFERENCE_TYPE)
5882 error ("linear clause with %qs modifier applied to "
5883 "non-reference variable with %qT type",
5884 OMP_CLAUSE_LINEAR_KIND (c) == OMP_CLAUSE_LINEAR_REF
5885 ? "ref" : "uval", TREE_TYPE (t));
5886 remove = true;
5887 break;
5889 if (TREE_CODE (type) == REFERENCE_TYPE)
5890 type = TREE_TYPE (type);
5891 if (OMP_CLAUSE_LINEAR_KIND (c) != OMP_CLAUSE_LINEAR_REF)
5893 if (!INTEGRAL_TYPE_P (type)
5894 && TREE_CODE (type) != POINTER_TYPE)
5896 error ("linear clause applied to non-integral non-pointer"
5897 " variable with %qT type", TREE_TYPE (t));
5898 remove = true;
5899 break;
5903 t = OMP_CLAUSE_LINEAR_STEP (c);
5904 if (t == NULL_TREE)
5905 t = integer_one_node;
5906 if (t == error_mark_node)
5908 remove = true;
5909 break;
5911 else if (!type_dependent_expression_p (t)
5912 && !INTEGRAL_TYPE_P (TREE_TYPE (t))
5913 && (ort != C_ORT_OMP_DECLARE_SIMD
5914 || TREE_CODE (t) != PARM_DECL
5915 || TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE
5916 || !INTEGRAL_TYPE_P (TREE_TYPE (TREE_TYPE (t)))))
5918 error ("linear step expression must be integral");
5919 remove = true;
5920 break;
5922 else
5924 t = mark_rvalue_use (t);
5925 if (ort == C_ORT_OMP_DECLARE_SIMD && TREE_CODE (t) == PARM_DECL)
5927 OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c) = 1;
5928 goto check_dup_generic;
5930 if (!processing_template_decl
5931 && (VAR_P (OMP_CLAUSE_DECL (c))
5932 || TREE_CODE (OMP_CLAUSE_DECL (c)) == PARM_DECL))
5934 if (ort == C_ORT_OMP_DECLARE_SIMD)
5936 t = maybe_constant_value (t);
5937 if (TREE_CODE (t) != INTEGER_CST)
5939 error_at (OMP_CLAUSE_LOCATION (c),
5940 "%<linear%> clause step %qE is neither "
5941 "constant nor a parameter", t);
5942 remove = true;
5943 break;
5946 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5947 tree type = TREE_TYPE (OMP_CLAUSE_DECL (c));
5948 if (TREE_CODE (type) == REFERENCE_TYPE)
5949 type = TREE_TYPE (type);
5950 if (OMP_CLAUSE_LINEAR_KIND (c) == OMP_CLAUSE_LINEAR_REF)
5952 type = build_pointer_type (type);
5953 tree d = fold_convert (type, OMP_CLAUSE_DECL (c));
5954 t = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
5955 d, t);
5956 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c),
5957 MINUS_EXPR, sizetype,
5958 fold_convert (sizetype, t),
5959 fold_convert (sizetype, d));
5960 if (t == error_mark_node)
5962 remove = true;
5963 break;
5966 else if (TREE_CODE (type) == POINTER_TYPE
5967 /* Can't multiply the step yet if *this
5968 is still incomplete type. */
5969 && (ort != C_ORT_OMP_DECLARE_SIMD
5970 || TREE_CODE (OMP_CLAUSE_DECL (c)) != PARM_DECL
5971 || !DECL_ARTIFICIAL (OMP_CLAUSE_DECL (c))
5972 || DECL_NAME (OMP_CLAUSE_DECL (c))
5973 != this_identifier
5974 || !TYPE_BEING_DEFINED (TREE_TYPE (type))))
5976 tree d = convert_from_reference (OMP_CLAUSE_DECL (c));
5977 t = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
5978 d, t);
5979 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c),
5980 MINUS_EXPR, sizetype,
5981 fold_convert (sizetype, t),
5982 fold_convert (sizetype, d));
5983 if (t == error_mark_node)
5985 remove = true;
5986 break;
5989 else
5990 t = fold_convert (type, t);
5992 OMP_CLAUSE_LINEAR_STEP (c) = t;
5994 goto check_dup_generic;
5995 check_dup_generic:
5996 t = omp_clause_decl_field (OMP_CLAUSE_DECL (c));
5997 if (t)
5999 if (!remove && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_SHARED)
6000 omp_note_field_privatization (t, OMP_CLAUSE_DECL (c));
6002 else
6003 t = OMP_CLAUSE_DECL (c);
6004 check_dup_generic_t:
6005 if (t == current_class_ptr
6006 && (ort != C_ORT_OMP_DECLARE_SIMD
6007 || (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_LINEAR
6008 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_UNIFORM)))
6010 error ("%<this%> allowed in OpenMP only in %<declare simd%>"
6011 " clauses");
6012 remove = true;
6013 break;
6015 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL
6016 && (!field_ok || TREE_CODE (t) != FIELD_DECL))
6018 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
6019 break;
6020 if (DECL_P (t))
6021 error ("%qD is not a variable in clause %qs", t,
6022 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6023 else
6024 error ("%qE is not a variable in clause %qs", t,
6025 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6026 remove = true;
6028 else if (ort == C_ORT_ACC
6029 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
6031 if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
6033 error ("%qD appears more than once in reduction clauses", t);
6034 remove = true;
6036 else
6037 bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
6039 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
6040 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
6041 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
6043 error ("%qD appears more than once in data clauses", t);
6044 remove = true;
6046 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
6047 && bitmap_bit_p (&map_head, DECL_UID (t)))
6049 if (ort == C_ORT_ACC)
6050 error ("%qD appears more than once in data clauses", t);
6051 else
6052 error ("%qD appears both in data and map clauses", t);
6053 remove = true;
6055 else
6056 bitmap_set_bit (&generic_head, DECL_UID (t));
6057 if (!field_ok)
6058 break;
6059 handle_field_decl:
6060 if (!remove
6061 && TREE_CODE (t) == FIELD_DECL
6062 && t == OMP_CLAUSE_DECL (c)
6063 && ort != C_ORT_ACC)
6065 OMP_CLAUSE_DECL (c)
6066 = omp_privatize_field (t, (OMP_CLAUSE_CODE (c)
6067 == OMP_CLAUSE_SHARED));
6068 if (OMP_CLAUSE_DECL (c) == error_mark_node)
6069 remove = true;
6071 break;
6073 case OMP_CLAUSE_FIRSTPRIVATE:
6074 t = omp_clause_decl_field (OMP_CLAUSE_DECL (c));
6075 if (t)
6076 omp_note_field_privatization (t, OMP_CLAUSE_DECL (c));
6077 else
6078 t = OMP_CLAUSE_DECL (c);
6079 if (ort != C_ORT_ACC && t == current_class_ptr)
6081 error ("%<this%> allowed in OpenMP only in %<declare simd%>"
6082 " clauses");
6083 remove = true;
6084 break;
6086 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL
6087 && ((ort & C_ORT_OMP_DECLARE_SIMD) != C_ORT_OMP
6088 || TREE_CODE (t) != FIELD_DECL))
6090 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
6091 break;
6092 if (DECL_P (t))
6093 error ("%qD is not a variable in clause %<firstprivate%>", t);
6094 else
6095 error ("%qE is not a variable in clause %<firstprivate%>", t);
6096 remove = true;
6098 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
6099 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
6101 error ("%qD appears more than once in data clauses", t);
6102 remove = true;
6104 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
6106 if (ort == C_ORT_ACC)
6107 error ("%qD appears more than once in data clauses", t);
6108 else
6109 error ("%qD appears both in data and map clauses", t);
6110 remove = true;
6112 else
6113 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
6114 goto handle_field_decl;
6116 case OMP_CLAUSE_LASTPRIVATE:
6117 t = omp_clause_decl_field (OMP_CLAUSE_DECL (c));
6118 if (t)
6119 omp_note_field_privatization (t, OMP_CLAUSE_DECL (c));
6120 else
6121 t = OMP_CLAUSE_DECL (c);
6122 if (t == current_class_ptr)
6124 error ("%<this%> allowed in OpenMP only in %<declare simd%>"
6125 " clauses");
6126 remove = true;
6127 break;
6129 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL
6130 && ((ort & C_ORT_OMP_DECLARE_SIMD) != C_ORT_OMP
6131 || TREE_CODE (t) != FIELD_DECL))
6133 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
6134 break;
6135 if (DECL_P (t))
6136 error ("%qD is not a variable in clause %<lastprivate%>", t);
6137 else
6138 error ("%qE is not a variable in clause %<lastprivate%>", t);
6139 remove = true;
6141 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
6142 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
6144 error ("%qD appears more than once in data clauses", t);
6145 remove = true;
6147 else
6148 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
6149 goto handle_field_decl;
6151 case OMP_CLAUSE_IF:
6152 t = OMP_CLAUSE_IF_EXPR (c);
6153 t = maybe_convert_cond (t);
6154 if (t == error_mark_node)
6155 remove = true;
6156 else if (!processing_template_decl)
6157 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6158 OMP_CLAUSE_IF_EXPR (c) = t;
6159 break;
6161 case OMP_CLAUSE_FINAL:
6162 t = OMP_CLAUSE_FINAL_EXPR (c);
6163 t = maybe_convert_cond (t);
6164 if (t == error_mark_node)
6165 remove = true;
6166 else if (!processing_template_decl)
6167 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6168 OMP_CLAUSE_FINAL_EXPR (c) = t;
6169 break;
6171 case OMP_CLAUSE_GANG:
6172 /* Operand 1 is the gang static: argument. */
6173 t = OMP_CLAUSE_OPERAND (c, 1);
6174 if (t != NULL_TREE)
6176 if (t == error_mark_node)
6177 remove = true;
6178 else if (!type_dependent_expression_p (t)
6179 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6181 error ("%<gang%> static expression must be integral");
6182 remove = true;
6184 else
6186 t = mark_rvalue_use (t);
6187 if (!processing_template_decl)
6189 t = maybe_constant_value (t);
6190 if (TREE_CODE (t) == INTEGER_CST
6191 && tree_int_cst_sgn (t) != 1
6192 && t != integer_minus_one_node)
6194 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6195 "%<gang%> static value must be "
6196 "positive");
6197 t = integer_one_node;
6199 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6202 OMP_CLAUSE_OPERAND (c, 1) = t;
6204 /* Check operand 0, the num argument. */
6205 /* FALLTHRU */
6207 case OMP_CLAUSE_WORKER:
6208 case OMP_CLAUSE_VECTOR:
6209 if (OMP_CLAUSE_OPERAND (c, 0) == NULL_TREE)
6210 break;
6211 /* FALLTHRU */
6213 case OMP_CLAUSE_NUM_TASKS:
6214 case OMP_CLAUSE_NUM_TEAMS:
6215 case OMP_CLAUSE_NUM_THREADS:
6216 case OMP_CLAUSE_NUM_GANGS:
6217 case OMP_CLAUSE_NUM_WORKERS:
6218 case OMP_CLAUSE_VECTOR_LENGTH:
6219 t = OMP_CLAUSE_OPERAND (c, 0);
6220 if (t == error_mark_node)
6221 remove = true;
6222 else if (!type_dependent_expression_p (t)
6223 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6225 switch (OMP_CLAUSE_CODE (c))
6227 case OMP_CLAUSE_GANG:
6228 error_at (OMP_CLAUSE_LOCATION (c),
6229 "%<gang%> num expression must be integral"); break;
6230 case OMP_CLAUSE_VECTOR:
6231 error_at (OMP_CLAUSE_LOCATION (c),
6232 "%<vector%> length expression must be integral");
6233 break;
6234 case OMP_CLAUSE_WORKER:
6235 error_at (OMP_CLAUSE_LOCATION (c),
6236 "%<worker%> num expression must be integral");
6237 break;
6238 default:
6239 error_at (OMP_CLAUSE_LOCATION (c),
6240 "%qs expression must be integral",
6241 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6243 remove = true;
6245 else
6247 t = mark_rvalue_use (t);
6248 if (!processing_template_decl)
6250 t = maybe_constant_value (t);
6251 if (TREE_CODE (t) == INTEGER_CST
6252 && tree_int_cst_sgn (t) != 1)
6254 switch (OMP_CLAUSE_CODE (c))
6256 case OMP_CLAUSE_GANG:
6257 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6258 "%<gang%> num value must be positive");
6259 break;
6260 case OMP_CLAUSE_VECTOR:
6261 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6262 "%<vector%> length value must be "
6263 "positive");
6264 break;
6265 case OMP_CLAUSE_WORKER:
6266 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6267 "%<worker%> num value must be "
6268 "positive");
6269 break;
6270 default:
6271 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6272 "%qs value must be positive",
6273 omp_clause_code_name
6274 [OMP_CLAUSE_CODE (c)]);
6276 t = integer_one_node;
6278 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6280 OMP_CLAUSE_OPERAND (c, 0) = t;
6282 break;
6284 case OMP_CLAUSE_SCHEDULE:
6285 if (OMP_CLAUSE_SCHEDULE_KIND (c) & OMP_CLAUSE_SCHEDULE_NONMONOTONIC)
6287 const char *p = NULL;
6288 switch (OMP_CLAUSE_SCHEDULE_KIND (c) & OMP_CLAUSE_SCHEDULE_MASK)
6290 case OMP_CLAUSE_SCHEDULE_STATIC: p = "static"; break;
6291 case OMP_CLAUSE_SCHEDULE_DYNAMIC: break;
6292 case OMP_CLAUSE_SCHEDULE_GUIDED: break;
6293 case OMP_CLAUSE_SCHEDULE_AUTO: p = "auto"; break;
6294 case OMP_CLAUSE_SCHEDULE_RUNTIME: p = "runtime"; break;
6295 default: gcc_unreachable ();
6297 if (p)
6299 error_at (OMP_CLAUSE_LOCATION (c),
6300 "%<nonmonotonic%> modifier specified for %qs "
6301 "schedule kind", p);
6302 OMP_CLAUSE_SCHEDULE_KIND (c)
6303 = (enum omp_clause_schedule_kind)
6304 (OMP_CLAUSE_SCHEDULE_KIND (c)
6305 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
6309 t = OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c);
6310 if (t == NULL)
6312 else if (t == error_mark_node)
6313 remove = true;
6314 else if (!type_dependent_expression_p (t)
6315 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6317 error ("schedule chunk size expression must be integral");
6318 remove = true;
6320 else
6322 t = mark_rvalue_use (t);
6323 if (!processing_template_decl)
6325 t = maybe_constant_value (t);
6326 if (TREE_CODE (t) == INTEGER_CST
6327 && tree_int_cst_sgn (t) != 1)
6329 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6330 "chunk size value must be positive");
6331 t = integer_one_node;
6333 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6335 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
6337 break;
6339 case OMP_CLAUSE_SIMDLEN:
6340 case OMP_CLAUSE_SAFELEN:
6341 t = OMP_CLAUSE_OPERAND (c, 0);
6342 if (t == error_mark_node)
6343 remove = true;
6344 else if (!type_dependent_expression_p (t)
6345 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6347 error ("%qs length expression must be integral",
6348 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6349 remove = true;
6351 else
6353 t = mark_rvalue_use (t);
6354 if (!processing_template_decl)
6356 t = maybe_constant_value (t);
6357 if (TREE_CODE (t) != INTEGER_CST
6358 || tree_int_cst_sgn (t) != 1)
6360 error ("%qs length expression must be positive constant"
6361 " integer expression",
6362 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6363 remove = true;
6366 OMP_CLAUSE_OPERAND (c, 0) = t;
6367 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SAFELEN)
6368 safelen = c;
6370 break;
6372 case OMP_CLAUSE_ASYNC:
6373 t = OMP_CLAUSE_ASYNC_EXPR (c);
6374 if (t == error_mark_node)
6375 remove = true;
6376 else if (!type_dependent_expression_p (t)
6377 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6379 error ("%<async%> expression must be integral");
6380 remove = true;
6382 else
6384 t = mark_rvalue_use (t);
6385 if (!processing_template_decl)
6386 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6387 OMP_CLAUSE_ASYNC_EXPR (c) = t;
6389 break;
6391 case OMP_CLAUSE_WAIT:
6392 t = OMP_CLAUSE_WAIT_EXPR (c);
6393 if (t == error_mark_node)
6394 remove = true;
6395 else if (!processing_template_decl)
6396 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6397 OMP_CLAUSE_WAIT_EXPR (c) = t;
6398 break;
6400 case OMP_CLAUSE_THREAD_LIMIT:
6401 t = OMP_CLAUSE_THREAD_LIMIT_EXPR (c);
6402 if (t == error_mark_node)
6403 remove = true;
6404 else if (!type_dependent_expression_p (t)
6405 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6407 error ("%<thread_limit%> expression must be integral");
6408 remove = true;
6410 else
6412 t = mark_rvalue_use (t);
6413 if (!processing_template_decl)
6415 t = maybe_constant_value (t);
6416 if (TREE_CODE (t) == INTEGER_CST
6417 && tree_int_cst_sgn (t) != 1)
6419 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6420 "%<thread_limit%> value must be positive");
6421 t = integer_one_node;
6423 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6425 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
6427 break;
6429 case OMP_CLAUSE_DEVICE:
6430 t = OMP_CLAUSE_DEVICE_ID (c);
6431 if (t == error_mark_node)
6432 remove = true;
6433 else if (!type_dependent_expression_p (t)
6434 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6436 error ("%<device%> id must be integral");
6437 remove = true;
6439 else
6441 t = mark_rvalue_use (t);
6442 if (!processing_template_decl)
6443 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6444 OMP_CLAUSE_DEVICE_ID (c) = t;
6446 break;
6448 case OMP_CLAUSE_DIST_SCHEDULE:
6449 t = OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c);
6450 if (t == NULL)
6452 else if (t == error_mark_node)
6453 remove = true;
6454 else if (!type_dependent_expression_p (t)
6455 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6457 error ("%<dist_schedule%> chunk size expression must be "
6458 "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_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
6468 break;
6470 case OMP_CLAUSE_ALIGNED:
6471 t = OMP_CLAUSE_DECL (c);
6472 if (t == current_class_ptr && ort != C_ORT_OMP_DECLARE_SIMD)
6474 error ("%<this%> allowed in OpenMP only in %<declare simd%>"
6475 " clauses");
6476 remove = true;
6477 break;
6479 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
6481 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
6482 break;
6483 if (DECL_P (t))
6484 error ("%qD is not a variable in %<aligned%> clause", t);
6485 else
6486 error ("%qE is not a variable in %<aligned%> clause", t);
6487 remove = true;
6489 else if (!type_dependent_expression_p (t)
6490 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE
6491 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE
6492 && (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE
6493 || (!POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (t)))
6494 && (TREE_CODE (TREE_TYPE (TREE_TYPE (t)))
6495 != ARRAY_TYPE))))
6497 error_at (OMP_CLAUSE_LOCATION (c),
6498 "%qE in %<aligned%> clause is neither a pointer nor "
6499 "an array nor a reference to pointer or array", t);
6500 remove = true;
6502 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
6504 error ("%qD appears more than once in %<aligned%> clauses", t);
6505 remove = true;
6507 else
6508 bitmap_set_bit (&aligned_head, DECL_UID (t));
6509 t = OMP_CLAUSE_ALIGNED_ALIGNMENT (c);
6510 if (t == error_mark_node)
6511 remove = true;
6512 else if (t == NULL_TREE)
6513 break;
6514 else if (!type_dependent_expression_p (t)
6515 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6517 error ("%<aligned%> clause alignment expression must "
6518 "be integral");
6519 remove = true;
6521 else
6523 t = mark_rvalue_use (t);
6524 if (!processing_template_decl)
6526 t = maybe_constant_value (t);
6527 if (TREE_CODE (t) != INTEGER_CST
6528 || tree_int_cst_sgn (t) != 1)
6530 error ("%<aligned%> clause alignment expression must be "
6531 "positive constant integer expression");
6532 remove = true;
6535 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = t;
6537 break;
6539 case OMP_CLAUSE_DEPEND:
6540 t = OMP_CLAUSE_DECL (c);
6541 if (t == NULL_TREE)
6543 gcc_assert (OMP_CLAUSE_DEPEND_KIND (c)
6544 == OMP_CLAUSE_DEPEND_SOURCE);
6545 break;
6547 if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SINK)
6549 if (cp_finish_omp_clause_depend_sink (c))
6550 remove = true;
6551 break;
6553 if (TREE_CODE (t) == TREE_LIST)
6555 if (handle_omp_array_sections (c, ort))
6556 remove = true;
6557 break;
6559 if (t == error_mark_node)
6560 remove = true;
6561 else if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
6563 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
6564 break;
6565 if (DECL_P (t))
6566 error ("%qD is not a variable in %<depend%> clause", t);
6567 else
6568 error ("%qE is not a variable in %<depend%> clause", t);
6569 remove = true;
6571 else if (t == current_class_ptr)
6573 error ("%<this%> allowed in OpenMP only in %<declare simd%>"
6574 " clauses");
6575 remove = true;
6577 else if (!processing_template_decl
6578 && !cxx_mark_addressable (t))
6579 remove = true;
6580 break;
6582 case OMP_CLAUSE_MAP:
6583 case OMP_CLAUSE_TO:
6584 case OMP_CLAUSE_FROM:
6585 case OMP_CLAUSE__CACHE_:
6586 t = OMP_CLAUSE_DECL (c);
6587 if (TREE_CODE (t) == TREE_LIST)
6589 if (handle_omp_array_sections (c, ort))
6590 remove = true;
6591 else
6593 t = OMP_CLAUSE_DECL (c);
6594 if (TREE_CODE (t) != TREE_LIST
6595 && !type_dependent_expression_p (t)
6596 && !cp_omp_mappable_type (TREE_TYPE (t)))
6598 error_at (OMP_CLAUSE_LOCATION (c),
6599 "array section does not have mappable type "
6600 "in %qs clause",
6601 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6602 remove = true;
6604 while (TREE_CODE (t) == ARRAY_REF)
6605 t = TREE_OPERAND (t, 0);
6606 if (TREE_CODE (t) == COMPONENT_REF
6607 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
6609 while (TREE_CODE (t) == COMPONENT_REF)
6610 t = TREE_OPERAND (t, 0);
6611 if (REFERENCE_REF_P (t))
6612 t = TREE_OPERAND (t, 0);
6613 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
6614 break;
6615 if (bitmap_bit_p (&map_head, DECL_UID (t)))
6617 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
6618 error ("%qD appears more than once in motion"
6619 " clauses", t);
6620 else if (ort == C_ORT_ACC)
6621 error ("%qD appears more than once in data"
6622 " clauses", t);
6623 else
6624 error ("%qD appears more than once in map"
6625 " clauses", t);
6626 remove = true;
6628 else
6630 bitmap_set_bit (&map_head, DECL_UID (t));
6631 bitmap_set_bit (&map_field_head, DECL_UID (t));
6635 break;
6637 if (t == error_mark_node)
6639 remove = true;
6640 break;
6642 if (REFERENCE_REF_P (t)
6643 && TREE_CODE (TREE_OPERAND (t, 0)) == COMPONENT_REF)
6645 t = TREE_OPERAND (t, 0);
6646 OMP_CLAUSE_DECL (c) = t;
6648 if (TREE_CODE (t) == COMPONENT_REF
6649 && (ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP
6650 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE__CACHE_)
6652 if (type_dependent_expression_p (t))
6653 break;
6654 if (TREE_CODE (TREE_OPERAND (t, 1)) == FIELD_DECL
6655 && DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
6657 error_at (OMP_CLAUSE_LOCATION (c),
6658 "bit-field %qE in %qs clause",
6659 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6660 remove = true;
6662 else if (!cp_omp_mappable_type (TREE_TYPE (t)))
6664 error_at (OMP_CLAUSE_LOCATION (c),
6665 "%qE does not have a mappable type in %qs clause",
6666 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6667 remove = true;
6669 while (TREE_CODE (t) == COMPONENT_REF)
6671 if (TREE_TYPE (TREE_OPERAND (t, 0))
6672 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0)))
6673 == UNION_TYPE))
6675 error_at (OMP_CLAUSE_LOCATION (c),
6676 "%qE is a member of a union", t);
6677 remove = true;
6678 break;
6680 t = TREE_OPERAND (t, 0);
6682 if (remove)
6683 break;
6684 if (REFERENCE_REF_P (t))
6685 t = TREE_OPERAND (t, 0);
6686 if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
6688 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
6689 goto handle_map_references;
6692 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
6694 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
6695 break;
6696 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
6697 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
6698 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ALWAYS_POINTER))
6699 break;
6700 if (DECL_P (t))
6701 error ("%qD is not a variable in %qs clause", t,
6702 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6703 else
6704 error ("%qE is not a variable in %qs clause", t,
6705 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6706 remove = true;
6708 else if (VAR_P (t) && CP_DECL_THREAD_LOCAL_P (t))
6710 error ("%qD is threadprivate variable in %qs clause", t,
6711 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6712 remove = true;
6714 else if (ort != C_ORT_ACC && t == current_class_ptr)
6716 error ("%<this%> allowed in OpenMP only in %<declare simd%>"
6717 " clauses");
6718 remove = true;
6719 break;
6721 else if (!processing_template_decl
6722 && TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE
6723 && (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
6724 || (OMP_CLAUSE_MAP_KIND (c)
6725 != GOMP_MAP_FIRSTPRIVATE_POINTER))
6726 && !cxx_mark_addressable (t))
6727 remove = true;
6728 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
6729 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
6730 || (OMP_CLAUSE_MAP_KIND (c)
6731 == GOMP_MAP_FIRSTPRIVATE_POINTER)))
6732 && t == OMP_CLAUSE_DECL (c)
6733 && !type_dependent_expression_p (t)
6734 && !cp_omp_mappable_type ((TREE_CODE (TREE_TYPE (t))
6735 == REFERENCE_TYPE)
6736 ? TREE_TYPE (TREE_TYPE (t))
6737 : TREE_TYPE (t)))
6739 error_at (OMP_CLAUSE_LOCATION (c),
6740 "%qD does not have a mappable type in %qs clause", t,
6741 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6742 remove = true;
6744 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
6745 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FORCE_DEVICEPTR
6746 && !type_dependent_expression_p (t)
6747 && !POINTER_TYPE_P (TREE_TYPE (t)))
6749 error ("%qD is not a pointer variable", t);
6750 remove = true;
6752 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
6753 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
6755 if (bitmap_bit_p (&generic_head, DECL_UID (t))
6756 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
6758 error ("%qD appears more than once in data clauses", t);
6759 remove = true;
6761 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
6763 if (ort == C_ORT_ACC)
6764 error ("%qD appears more than once in data clauses", t);
6765 else
6766 error ("%qD appears both in data and map clauses", t);
6767 remove = true;
6769 else
6770 bitmap_set_bit (&generic_head, DECL_UID (t));
6772 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
6774 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
6775 error ("%qD appears more than once in motion clauses", t);
6776 if (ort == C_ORT_ACC)
6777 error ("%qD appears more than once in data clauses", t);
6778 else
6779 error ("%qD appears more than once in map clauses", t);
6780 remove = true;
6782 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
6783 || bitmap_bit_p (&firstprivate_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
6793 bitmap_set_bit (&map_head, DECL_UID (t));
6794 if (t != OMP_CLAUSE_DECL (c)
6795 && TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPONENT_REF)
6796 bitmap_set_bit (&map_field_head, DECL_UID (t));
6798 handle_map_references:
6799 if (!remove
6800 && !processing_template_decl
6801 && (ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP
6802 && TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c))) == REFERENCE_TYPE)
6804 t = OMP_CLAUSE_DECL (c);
6805 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
6807 OMP_CLAUSE_DECL (c) = build_simple_mem_ref (t);
6808 if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
6809 OMP_CLAUSE_SIZE (c)
6810 = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t)));
6812 else if (OMP_CLAUSE_MAP_KIND (c)
6813 != GOMP_MAP_FIRSTPRIVATE_POINTER
6814 && (OMP_CLAUSE_MAP_KIND (c)
6815 != GOMP_MAP_FIRSTPRIVATE_REFERENCE)
6816 && (OMP_CLAUSE_MAP_KIND (c)
6817 != GOMP_MAP_ALWAYS_POINTER))
6819 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
6820 OMP_CLAUSE_MAP);
6821 if (TREE_CODE (t) == COMPONENT_REF)
6822 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ALWAYS_POINTER);
6823 else
6824 OMP_CLAUSE_SET_MAP_KIND (c2,
6825 GOMP_MAP_FIRSTPRIVATE_REFERENCE);
6826 OMP_CLAUSE_DECL (c2) = t;
6827 OMP_CLAUSE_SIZE (c2) = size_zero_node;
6828 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
6829 OMP_CLAUSE_CHAIN (c) = c2;
6830 OMP_CLAUSE_DECL (c) = build_simple_mem_ref (t);
6831 if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
6832 OMP_CLAUSE_SIZE (c)
6833 = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t)));
6834 c = c2;
6837 break;
6839 case OMP_CLAUSE_TO_DECLARE:
6840 case OMP_CLAUSE_LINK:
6841 t = OMP_CLAUSE_DECL (c);
6842 if (TREE_CODE (t) == FUNCTION_DECL
6843 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
6845 else if (!VAR_P (t))
6847 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
6849 if (TREE_CODE (t) == TEMPLATE_ID_EXPR)
6850 error_at (OMP_CLAUSE_LOCATION (c),
6851 "template %qE in clause %qs", t,
6852 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6853 else if (really_overloaded_fn (t))
6854 error_at (OMP_CLAUSE_LOCATION (c),
6855 "overloaded function name %qE in clause %qs", t,
6856 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6857 else
6858 error_at (OMP_CLAUSE_LOCATION (c),
6859 "%qE is neither a variable nor a function name "
6860 "in clause %qs", t,
6861 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6863 else
6864 error_at (OMP_CLAUSE_LOCATION (c),
6865 "%qE is not a variable in clause %qs", t,
6866 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6867 remove = true;
6869 else if (DECL_THREAD_LOCAL_P (t))
6871 error_at (OMP_CLAUSE_LOCATION (c),
6872 "%qD is threadprivate variable in %qs clause", t,
6873 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6874 remove = true;
6876 else if (!cp_omp_mappable_type (TREE_TYPE (t)))
6878 error_at (OMP_CLAUSE_LOCATION (c),
6879 "%qD does not have a mappable type in %qs clause", t,
6880 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6881 remove = true;
6883 if (remove)
6884 break;
6885 if (bitmap_bit_p (&generic_head, DECL_UID (t)))
6887 error_at (OMP_CLAUSE_LOCATION (c),
6888 "%qE appears more than once on the same "
6889 "%<declare target%> directive", t);
6890 remove = true;
6892 else
6893 bitmap_set_bit (&generic_head, DECL_UID (t));
6894 break;
6896 case OMP_CLAUSE_UNIFORM:
6897 t = OMP_CLAUSE_DECL (c);
6898 if (TREE_CODE (t) != PARM_DECL)
6900 if (processing_template_decl)
6901 break;
6902 if (DECL_P (t))
6903 error ("%qD is not an argument in %<uniform%> clause", t);
6904 else
6905 error ("%qE is not an argument in %<uniform%> clause", t);
6906 remove = true;
6907 break;
6909 /* map_head bitmap is used as uniform_head if declare_simd. */
6910 bitmap_set_bit (&map_head, DECL_UID (t));
6911 goto check_dup_generic;
6913 case OMP_CLAUSE_GRAINSIZE:
6914 t = OMP_CLAUSE_GRAINSIZE_EXPR (c);
6915 if (t == error_mark_node)
6916 remove = true;
6917 else if (!type_dependent_expression_p (t)
6918 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6920 error ("%<grainsize%> expression must be integral");
6921 remove = true;
6923 else
6925 t = mark_rvalue_use (t);
6926 if (!processing_template_decl)
6928 t = maybe_constant_value (t);
6929 if (TREE_CODE (t) == INTEGER_CST
6930 && tree_int_cst_sgn (t) != 1)
6932 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6933 "%<grainsize%> value must be positive");
6934 t = integer_one_node;
6936 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6938 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
6940 break;
6942 case OMP_CLAUSE_PRIORITY:
6943 t = OMP_CLAUSE_PRIORITY_EXPR (c);
6944 if (t == error_mark_node)
6945 remove = true;
6946 else if (!type_dependent_expression_p (t)
6947 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6949 error ("%<priority%> expression must be integral");
6950 remove = true;
6952 else
6954 t = mark_rvalue_use (t);
6955 if (!processing_template_decl)
6957 t = maybe_constant_value (t);
6958 if (TREE_CODE (t) == INTEGER_CST
6959 && tree_int_cst_sgn (t) == -1)
6961 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6962 "%<priority%> value must be non-negative");
6963 t = integer_one_node;
6965 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6967 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
6969 break;
6971 case OMP_CLAUSE_HINT:
6972 t = OMP_CLAUSE_HINT_EXPR (c);
6973 if (t == error_mark_node)
6974 remove = true;
6975 else if (!type_dependent_expression_p (t)
6976 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6978 error ("%<num_tasks%> expression must be integral");
6979 remove = true;
6981 else
6983 t = mark_rvalue_use (t);
6984 if (!processing_template_decl)
6986 t = maybe_constant_value (t);
6987 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6989 OMP_CLAUSE_HINT_EXPR (c) = t;
6991 break;
6993 case OMP_CLAUSE_IS_DEVICE_PTR:
6994 case OMP_CLAUSE_USE_DEVICE_PTR:
6995 field_ok = (ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP;
6996 t = OMP_CLAUSE_DECL (c);
6997 if (!type_dependent_expression_p (t))
6999 tree type = TREE_TYPE (t);
7000 if (TREE_CODE (type) != POINTER_TYPE
7001 && TREE_CODE (type) != ARRAY_TYPE
7002 && (TREE_CODE (type) != REFERENCE_TYPE
7003 || (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
7004 && TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE)))
7006 error_at (OMP_CLAUSE_LOCATION (c),
7007 "%qs variable is neither a pointer, nor an array "
7008 "nor reference to pointer or array",
7009 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7010 remove = true;
7013 goto check_dup_generic;
7015 case OMP_CLAUSE_NOWAIT:
7016 case OMP_CLAUSE_DEFAULT:
7017 case OMP_CLAUSE_UNTIED:
7018 case OMP_CLAUSE_COLLAPSE:
7019 case OMP_CLAUSE_MERGEABLE:
7020 case OMP_CLAUSE_PARALLEL:
7021 case OMP_CLAUSE_FOR:
7022 case OMP_CLAUSE_SECTIONS:
7023 case OMP_CLAUSE_TASKGROUP:
7024 case OMP_CLAUSE_PROC_BIND:
7025 case OMP_CLAUSE_NOGROUP:
7026 case OMP_CLAUSE_THREADS:
7027 case OMP_CLAUSE_SIMD:
7028 case OMP_CLAUSE_DEFAULTMAP:
7029 case OMP_CLAUSE_AUTO:
7030 case OMP_CLAUSE_INDEPENDENT:
7031 case OMP_CLAUSE_SEQ:
7032 break;
7034 case OMP_CLAUSE_TILE:
7035 for (tree list = OMP_CLAUSE_TILE_LIST (c); !remove && list;
7036 list = TREE_CHAIN (list))
7038 t = TREE_VALUE (list);
7040 if (t == error_mark_node)
7041 remove = true;
7042 else if (!type_dependent_expression_p (t)
7043 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
7045 error_at (OMP_CLAUSE_LOCATION (c),
7046 "%<tile%> argument needs integral type");
7047 remove = true;
7049 else
7051 t = mark_rvalue_use (t);
7052 if (!processing_template_decl)
7054 /* Zero is used to indicate '*', we permit you
7055 to get there via an ICE of value zero. */
7056 t = maybe_constant_value (t);
7057 if (!tree_fits_shwi_p (t)
7058 || tree_to_shwi (t) < 0)
7060 error_at (OMP_CLAUSE_LOCATION (c),
7061 "%<tile%> argument needs positive "
7062 "integral constant");
7063 remove = true;
7065 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7069 /* Update list item. */
7070 TREE_VALUE (list) = t;
7072 break;
7074 case OMP_CLAUSE_ORDERED:
7075 ordered_seen = true;
7076 break;
7078 case OMP_CLAUSE_INBRANCH:
7079 case OMP_CLAUSE_NOTINBRANCH:
7080 if (branch_seen)
7082 error ("%<inbranch%> clause is incompatible with "
7083 "%<notinbranch%>");
7084 remove = true;
7086 branch_seen = true;
7087 break;
7089 default:
7090 gcc_unreachable ();
7093 if (remove)
7094 *pc = OMP_CLAUSE_CHAIN (c);
7095 else
7096 pc = &OMP_CLAUSE_CHAIN (c);
7099 for (pc = &clauses, c = clauses; c ; c = *pc)
7101 enum omp_clause_code c_kind = OMP_CLAUSE_CODE (c);
7102 bool remove = false;
7103 bool need_complete_type = false;
7104 bool need_default_ctor = false;
7105 bool need_copy_ctor = false;
7106 bool need_copy_assignment = false;
7107 bool need_implicitly_determined = false;
7108 bool need_dtor = false;
7109 tree type, inner_type;
7111 switch (c_kind)
7113 case OMP_CLAUSE_SHARED:
7114 need_implicitly_determined = true;
7115 break;
7116 case OMP_CLAUSE_PRIVATE:
7117 need_complete_type = true;
7118 need_default_ctor = true;
7119 need_dtor = true;
7120 need_implicitly_determined = true;
7121 break;
7122 case OMP_CLAUSE_FIRSTPRIVATE:
7123 need_complete_type = true;
7124 need_copy_ctor = true;
7125 need_dtor = true;
7126 need_implicitly_determined = true;
7127 break;
7128 case OMP_CLAUSE_LASTPRIVATE:
7129 need_complete_type = true;
7130 need_copy_assignment = true;
7131 need_implicitly_determined = true;
7132 break;
7133 case OMP_CLAUSE_REDUCTION:
7134 need_implicitly_determined = true;
7135 break;
7136 case OMP_CLAUSE_LINEAR:
7137 if (ort != C_ORT_OMP_DECLARE_SIMD)
7138 need_implicitly_determined = true;
7139 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c)
7140 && !bitmap_bit_p (&map_head,
7141 DECL_UID (OMP_CLAUSE_LINEAR_STEP (c))))
7143 error_at (OMP_CLAUSE_LOCATION (c),
7144 "%<linear%> clause step is a parameter %qD not "
7145 "specified in %<uniform%> clause",
7146 OMP_CLAUSE_LINEAR_STEP (c));
7147 *pc = OMP_CLAUSE_CHAIN (c);
7148 continue;
7150 break;
7151 case OMP_CLAUSE_COPYPRIVATE:
7152 need_copy_assignment = true;
7153 break;
7154 case OMP_CLAUSE_COPYIN:
7155 need_copy_assignment = true;
7156 break;
7157 case OMP_CLAUSE_SIMDLEN:
7158 if (safelen
7159 && !processing_template_decl
7160 && tree_int_cst_lt (OMP_CLAUSE_SAFELEN_EXPR (safelen),
7161 OMP_CLAUSE_SIMDLEN_EXPR (c)))
7163 error_at (OMP_CLAUSE_LOCATION (c),
7164 "%<simdlen%> clause value is bigger than "
7165 "%<safelen%> clause value");
7166 OMP_CLAUSE_SIMDLEN_EXPR (c)
7167 = OMP_CLAUSE_SAFELEN_EXPR (safelen);
7169 pc = &OMP_CLAUSE_CHAIN (c);
7170 continue;
7171 case OMP_CLAUSE_SCHEDULE:
7172 if (ordered_seen
7173 && (OMP_CLAUSE_SCHEDULE_KIND (c)
7174 & OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
7176 error_at (OMP_CLAUSE_LOCATION (c),
7177 "%<nonmonotonic%> schedule modifier specified "
7178 "together with %<ordered%> clause");
7179 OMP_CLAUSE_SCHEDULE_KIND (c)
7180 = (enum omp_clause_schedule_kind)
7181 (OMP_CLAUSE_SCHEDULE_KIND (c)
7182 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
7184 pc = &OMP_CLAUSE_CHAIN (c);
7185 continue;
7186 case OMP_CLAUSE_NOWAIT:
7187 if (copyprivate_seen)
7189 error_at (OMP_CLAUSE_LOCATION (c),
7190 "%<nowait%> clause must not be used together "
7191 "with %<copyprivate%>");
7192 *pc = OMP_CLAUSE_CHAIN (c);
7193 continue;
7195 /* FALLTHRU */
7196 default:
7197 pc = &OMP_CLAUSE_CHAIN (c);
7198 continue;
7201 t = OMP_CLAUSE_DECL (c);
7202 if (processing_template_decl
7203 && !VAR_P (t) && TREE_CODE (t) != PARM_DECL)
7205 pc = &OMP_CLAUSE_CHAIN (c);
7206 continue;
7209 switch (c_kind)
7211 case OMP_CLAUSE_LASTPRIVATE:
7212 if (!bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
7214 need_default_ctor = true;
7215 need_dtor = true;
7217 break;
7219 case OMP_CLAUSE_REDUCTION:
7220 if (finish_omp_reduction_clause (c, &need_default_ctor,
7221 &need_dtor))
7222 remove = true;
7223 else
7224 t = OMP_CLAUSE_DECL (c);
7225 break;
7227 case OMP_CLAUSE_COPYIN:
7228 if (!VAR_P (t) || !CP_DECL_THREAD_LOCAL_P (t))
7230 error ("%qE must be %<threadprivate%> for %<copyin%>", t);
7231 remove = true;
7233 break;
7235 default:
7236 break;
7239 if (need_complete_type || need_copy_assignment)
7241 t = require_complete_type (t);
7242 if (t == error_mark_node)
7243 remove = true;
7244 else if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE
7245 && !complete_type_or_else (TREE_TYPE (TREE_TYPE (t)), t))
7246 remove = true;
7248 if (need_implicitly_determined)
7250 const char *share_name = NULL;
7252 if (VAR_P (t) && CP_DECL_THREAD_LOCAL_P (t))
7253 share_name = "threadprivate";
7254 else switch (cxx_omp_predetermined_sharing (t))
7256 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
7257 break;
7258 case OMP_CLAUSE_DEFAULT_SHARED:
7259 /* const vars may be specified in firstprivate clause. */
7260 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
7261 && cxx_omp_const_qual_no_mutable (t))
7262 break;
7263 share_name = "shared";
7264 break;
7265 case OMP_CLAUSE_DEFAULT_PRIVATE:
7266 share_name = "private";
7267 break;
7268 default:
7269 gcc_unreachable ();
7271 if (share_name)
7273 error ("%qE is predetermined %qs for %qs",
7274 omp_clause_printable_decl (t), share_name,
7275 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7276 remove = true;
7280 /* We're interested in the base element, not arrays. */
7281 inner_type = type = TREE_TYPE (t);
7282 if ((need_complete_type
7283 || need_copy_assignment
7284 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
7285 && TREE_CODE (inner_type) == REFERENCE_TYPE)
7286 inner_type = TREE_TYPE (inner_type);
7287 while (TREE_CODE (inner_type) == ARRAY_TYPE)
7288 inner_type = TREE_TYPE (inner_type);
7290 /* Check for special function availability by building a call to one.
7291 Save the results, because later we won't be in the right context
7292 for making these queries. */
7293 if (CLASS_TYPE_P (inner_type)
7294 && COMPLETE_TYPE_P (inner_type)
7295 && (need_default_ctor || need_copy_ctor
7296 || need_copy_assignment || need_dtor)
7297 && !type_dependent_expression_p (t)
7298 && cxx_omp_create_clause_info (c, inner_type, need_default_ctor,
7299 need_copy_ctor, need_copy_assignment,
7300 need_dtor))
7301 remove = true;
7303 if (!remove
7304 && c_kind == OMP_CLAUSE_SHARED
7305 && processing_template_decl)
7307 t = omp_clause_decl_field (OMP_CLAUSE_DECL (c));
7308 if (t)
7309 OMP_CLAUSE_DECL (c) = t;
7312 if (remove)
7313 *pc = OMP_CLAUSE_CHAIN (c);
7314 else
7315 pc = &OMP_CLAUSE_CHAIN (c);
7318 bitmap_obstack_release (NULL);
7319 return clauses;
7322 /* Start processing OpenMP clauses that can include any
7323 privatization clauses for non-static data members. */
7325 tree
7326 push_omp_privatization_clauses (bool ignore_next)
7328 if (omp_private_member_ignore_next)
7330 omp_private_member_ignore_next = ignore_next;
7331 return NULL_TREE;
7333 omp_private_member_ignore_next = ignore_next;
7334 if (omp_private_member_map)
7335 omp_private_member_vec.safe_push (error_mark_node);
7336 return push_stmt_list ();
7339 /* Revert remapping of any non-static data members since
7340 the last push_omp_privatization_clauses () call. */
7342 void
7343 pop_omp_privatization_clauses (tree stmt)
7345 if (stmt == NULL_TREE)
7346 return;
7347 stmt = pop_stmt_list (stmt);
7348 if (omp_private_member_map)
7350 while (!omp_private_member_vec.is_empty ())
7352 tree t = omp_private_member_vec.pop ();
7353 if (t == error_mark_node)
7355 add_stmt (stmt);
7356 return;
7358 bool no_decl_expr = t == integer_zero_node;
7359 if (no_decl_expr)
7360 t = omp_private_member_vec.pop ();
7361 tree *v = omp_private_member_map->get (t);
7362 gcc_assert (v);
7363 if (!no_decl_expr)
7364 add_decl_expr (*v);
7365 omp_private_member_map->remove (t);
7367 delete omp_private_member_map;
7368 omp_private_member_map = NULL;
7370 add_stmt (stmt);
7373 /* Remember OpenMP privatization clauses mapping and clear it.
7374 Used for lambdas. */
7376 void
7377 save_omp_privatization_clauses (vec<tree> &save)
7379 save = vNULL;
7380 if (omp_private_member_ignore_next)
7381 save.safe_push (integer_one_node);
7382 omp_private_member_ignore_next = false;
7383 if (!omp_private_member_map)
7384 return;
7386 while (!omp_private_member_vec.is_empty ())
7388 tree t = omp_private_member_vec.pop ();
7389 if (t == error_mark_node)
7391 save.safe_push (t);
7392 continue;
7394 tree n = t;
7395 if (t == integer_zero_node)
7396 t = omp_private_member_vec.pop ();
7397 tree *v = omp_private_member_map->get (t);
7398 gcc_assert (v);
7399 save.safe_push (*v);
7400 save.safe_push (t);
7401 if (n != t)
7402 save.safe_push (n);
7404 delete omp_private_member_map;
7405 omp_private_member_map = NULL;
7408 /* Restore OpenMP privatization clauses mapping saved by the
7409 above function. */
7411 void
7412 restore_omp_privatization_clauses (vec<tree> &save)
7414 gcc_assert (omp_private_member_vec.is_empty ());
7415 omp_private_member_ignore_next = false;
7416 if (save.is_empty ())
7417 return;
7418 if (save.length () == 1 && save[0] == integer_one_node)
7420 omp_private_member_ignore_next = true;
7421 save.release ();
7422 return;
7425 omp_private_member_map = new hash_map <tree, tree>;
7426 while (!save.is_empty ())
7428 tree t = save.pop ();
7429 tree n = t;
7430 if (t != error_mark_node)
7432 if (t == integer_one_node)
7434 omp_private_member_ignore_next = true;
7435 gcc_assert (save.is_empty ());
7436 break;
7438 if (t == integer_zero_node)
7439 t = save.pop ();
7440 tree &v = omp_private_member_map->get_or_insert (t);
7441 v = save.pop ();
7443 omp_private_member_vec.safe_push (t);
7444 if (n != t)
7445 omp_private_member_vec.safe_push (n);
7447 save.release ();
7450 /* For all variables in the tree_list VARS, mark them as thread local. */
7452 void
7453 finish_omp_threadprivate (tree vars)
7455 tree t;
7457 /* Mark every variable in VARS to be assigned thread local storage. */
7458 for (t = vars; t; t = TREE_CHAIN (t))
7460 tree v = TREE_PURPOSE (t);
7462 if (error_operand_p (v))
7464 else if (!VAR_P (v))
7465 error ("%<threadprivate%> %qD is not file, namespace "
7466 "or block scope variable", v);
7467 /* If V had already been marked threadprivate, it doesn't matter
7468 whether it had been used prior to this point. */
7469 else if (TREE_USED (v)
7470 && (DECL_LANG_SPECIFIC (v) == NULL
7471 || !CP_DECL_THREADPRIVATE_P (v)))
7472 error ("%qE declared %<threadprivate%> after first use", v);
7473 else if (! TREE_STATIC (v) && ! DECL_EXTERNAL (v))
7474 error ("automatic variable %qE cannot be %<threadprivate%>", v);
7475 else if (! COMPLETE_TYPE_P (complete_type (TREE_TYPE (v))))
7476 error ("%<threadprivate%> %qE has incomplete type", v);
7477 else if (TREE_STATIC (v) && TYPE_P (CP_DECL_CONTEXT (v))
7478 && CP_DECL_CONTEXT (v) != current_class_type)
7479 error ("%<threadprivate%> %qE directive not "
7480 "in %qT definition", v, CP_DECL_CONTEXT (v));
7481 else
7483 /* Allocate a LANG_SPECIFIC structure for V, if needed. */
7484 if (DECL_LANG_SPECIFIC (v) == NULL)
7486 retrofit_lang_decl (v);
7488 /* Make sure that DECL_DISCRIMINATOR_P continues to be true
7489 after the allocation of the lang_decl structure. */
7490 if (DECL_DISCRIMINATOR_P (v))
7491 DECL_LANG_SPECIFIC (v)->u.base.u2sel = 1;
7494 if (! CP_DECL_THREAD_LOCAL_P (v))
7496 CP_DECL_THREAD_LOCAL_P (v) = true;
7497 set_decl_tls_model (v, decl_default_tls_model (v));
7498 /* If rtl has been already set for this var, call
7499 make_decl_rtl once again, so that encode_section_info
7500 has a chance to look at the new decl flags. */
7501 if (DECL_RTL_SET_P (v))
7502 make_decl_rtl (v);
7504 CP_DECL_THREADPRIVATE_P (v) = 1;
7509 /* Build an OpenMP structured block. */
7511 tree
7512 begin_omp_structured_block (void)
7514 return do_pushlevel (sk_omp);
7517 tree
7518 finish_omp_structured_block (tree block)
7520 return do_poplevel (block);
7523 /* Similarly, except force the retention of the BLOCK. */
7525 tree
7526 begin_omp_parallel (void)
7528 keep_next_level (true);
7529 return begin_omp_structured_block ();
7532 /* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
7533 statement. */
7535 tree
7536 finish_oacc_data (tree clauses, tree block)
7538 tree stmt;
7540 block = finish_omp_structured_block (block);
7542 stmt = make_node (OACC_DATA);
7543 TREE_TYPE (stmt) = void_type_node;
7544 OACC_DATA_CLAUSES (stmt) = clauses;
7545 OACC_DATA_BODY (stmt) = block;
7547 return add_stmt (stmt);
7550 /* Generate OACC_HOST_DATA, with CLAUSES and BLOCK as its compound
7551 statement. */
7553 tree
7554 finish_oacc_host_data (tree clauses, tree block)
7556 tree stmt;
7558 block = finish_omp_structured_block (block);
7560 stmt = make_node (OACC_HOST_DATA);
7561 TREE_TYPE (stmt) = void_type_node;
7562 OACC_HOST_DATA_CLAUSES (stmt) = clauses;
7563 OACC_HOST_DATA_BODY (stmt) = block;
7565 return add_stmt (stmt);
7568 /* Generate OMP construct CODE, with BODY and CLAUSES as its compound
7569 statement. */
7571 tree
7572 finish_omp_construct (enum tree_code code, tree body, tree clauses)
7574 body = finish_omp_structured_block (body);
7576 tree stmt = make_node (code);
7577 TREE_TYPE (stmt) = void_type_node;
7578 OMP_BODY (stmt) = body;
7579 OMP_CLAUSES (stmt) = clauses;
7581 return add_stmt (stmt);
7584 tree
7585 finish_omp_parallel (tree clauses, tree body)
7587 tree stmt;
7589 body = finish_omp_structured_block (body);
7591 stmt = make_node (OMP_PARALLEL);
7592 TREE_TYPE (stmt) = void_type_node;
7593 OMP_PARALLEL_CLAUSES (stmt) = clauses;
7594 OMP_PARALLEL_BODY (stmt) = body;
7596 return add_stmt (stmt);
7599 tree
7600 begin_omp_task (void)
7602 keep_next_level (true);
7603 return begin_omp_structured_block ();
7606 tree
7607 finish_omp_task (tree clauses, tree body)
7609 tree stmt;
7611 body = finish_omp_structured_block (body);
7613 stmt = make_node (OMP_TASK);
7614 TREE_TYPE (stmt) = void_type_node;
7615 OMP_TASK_CLAUSES (stmt) = clauses;
7616 OMP_TASK_BODY (stmt) = body;
7618 return add_stmt (stmt);
7621 /* Helper function for finish_omp_for. Convert Ith random access iterator
7622 into integral iterator. Return FALSE if successful. */
7624 static bool
7625 handle_omp_for_class_iterator (int i, location_t locus, enum tree_code code,
7626 tree declv, tree orig_declv, tree initv,
7627 tree condv, tree incrv, tree *body,
7628 tree *pre_body, tree &clauses, tree *lastp,
7629 int collapse, int ordered)
7631 tree diff, iter_init, iter_incr = NULL, last;
7632 tree incr_var = NULL, orig_pre_body, orig_body, c;
7633 tree decl = TREE_VEC_ELT (declv, i);
7634 tree init = TREE_VEC_ELT (initv, i);
7635 tree cond = TREE_VEC_ELT (condv, i);
7636 tree incr = TREE_VEC_ELT (incrv, i);
7637 tree iter = decl;
7638 location_t elocus = locus;
7640 if (init && EXPR_HAS_LOCATION (init))
7641 elocus = EXPR_LOCATION (init);
7643 cond = cp_fully_fold (cond);
7644 switch (TREE_CODE (cond))
7646 case GT_EXPR:
7647 case GE_EXPR:
7648 case LT_EXPR:
7649 case LE_EXPR:
7650 case NE_EXPR:
7651 if (TREE_OPERAND (cond, 1) == iter)
7652 cond = build2 (swap_tree_comparison (TREE_CODE (cond)),
7653 TREE_TYPE (cond), iter, TREE_OPERAND (cond, 0));
7654 if (TREE_OPERAND (cond, 0) != iter)
7655 cond = error_mark_node;
7656 else
7658 tree tem = build_x_binary_op (EXPR_LOCATION (cond),
7659 TREE_CODE (cond),
7660 iter, ERROR_MARK,
7661 TREE_OPERAND (cond, 1), ERROR_MARK,
7662 NULL, tf_warning_or_error);
7663 if (error_operand_p (tem))
7664 return true;
7666 break;
7667 default:
7668 cond = error_mark_node;
7669 break;
7671 if (cond == error_mark_node)
7673 error_at (elocus, "invalid controlling predicate");
7674 return true;
7676 diff = build_x_binary_op (elocus, MINUS_EXPR, TREE_OPERAND (cond, 1),
7677 ERROR_MARK, iter, ERROR_MARK, NULL,
7678 tf_warning_or_error);
7679 diff = cp_fully_fold (diff);
7680 if (error_operand_p (diff))
7681 return true;
7682 if (TREE_CODE (TREE_TYPE (diff)) != INTEGER_TYPE)
7684 error_at (elocus, "difference between %qE and %qD does not have integer type",
7685 TREE_OPERAND (cond, 1), iter);
7686 return true;
7688 if (!c_omp_check_loop_iv_exprs (locus, orig_declv,
7689 TREE_VEC_ELT (declv, i), NULL_TREE,
7690 cond, cp_walk_subtrees))
7691 return true;
7693 switch (TREE_CODE (incr))
7695 case PREINCREMENT_EXPR:
7696 case PREDECREMENT_EXPR:
7697 case POSTINCREMENT_EXPR:
7698 case POSTDECREMENT_EXPR:
7699 if (TREE_OPERAND (incr, 0) != iter)
7701 incr = error_mark_node;
7702 break;
7704 iter_incr = build_x_unary_op (EXPR_LOCATION (incr),
7705 TREE_CODE (incr), iter,
7706 tf_warning_or_error);
7707 if (error_operand_p (iter_incr))
7708 return true;
7709 else if (TREE_CODE (incr) == PREINCREMENT_EXPR
7710 || TREE_CODE (incr) == POSTINCREMENT_EXPR)
7711 incr = integer_one_node;
7712 else
7713 incr = integer_minus_one_node;
7714 break;
7715 case MODIFY_EXPR:
7716 if (TREE_OPERAND (incr, 0) != iter)
7717 incr = error_mark_node;
7718 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
7719 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
7721 tree rhs = TREE_OPERAND (incr, 1);
7722 if (TREE_OPERAND (rhs, 0) == iter)
7724 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (rhs, 1)))
7725 != INTEGER_TYPE)
7726 incr = error_mark_node;
7727 else
7729 iter_incr = build_x_modify_expr (EXPR_LOCATION (rhs),
7730 iter, TREE_CODE (rhs),
7731 TREE_OPERAND (rhs, 1),
7732 tf_warning_or_error);
7733 if (error_operand_p (iter_incr))
7734 return true;
7735 incr = TREE_OPERAND (rhs, 1);
7736 incr = cp_convert (TREE_TYPE (diff), incr,
7737 tf_warning_or_error);
7738 if (TREE_CODE (rhs) == MINUS_EXPR)
7740 incr = build1 (NEGATE_EXPR, TREE_TYPE (diff), incr);
7741 incr = fold_simple (incr);
7743 if (TREE_CODE (incr) != INTEGER_CST
7744 && (TREE_CODE (incr) != NOP_EXPR
7745 || (TREE_CODE (TREE_OPERAND (incr, 0))
7746 != INTEGER_CST)))
7747 iter_incr = NULL;
7750 else if (TREE_OPERAND (rhs, 1) == iter)
7752 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (rhs, 0))) != INTEGER_TYPE
7753 || TREE_CODE (rhs) != PLUS_EXPR)
7754 incr = error_mark_node;
7755 else
7757 iter_incr = build_x_binary_op (EXPR_LOCATION (rhs),
7758 PLUS_EXPR,
7759 TREE_OPERAND (rhs, 0),
7760 ERROR_MARK, iter,
7761 ERROR_MARK, NULL,
7762 tf_warning_or_error);
7763 if (error_operand_p (iter_incr))
7764 return true;
7765 iter_incr = build_x_modify_expr (EXPR_LOCATION (rhs),
7766 iter, NOP_EXPR,
7767 iter_incr,
7768 tf_warning_or_error);
7769 if (error_operand_p (iter_incr))
7770 return true;
7771 incr = TREE_OPERAND (rhs, 0);
7772 iter_incr = NULL;
7775 else
7776 incr = error_mark_node;
7778 else
7779 incr = error_mark_node;
7780 break;
7781 default:
7782 incr = error_mark_node;
7783 break;
7786 if (incr == error_mark_node)
7788 error_at (elocus, "invalid increment expression");
7789 return true;
7792 incr = cp_convert (TREE_TYPE (diff), incr, tf_warning_or_error);
7793 bool taskloop_iv_seen = false;
7794 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
7795 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
7796 && OMP_CLAUSE_DECL (c) == iter)
7798 if (code == OMP_TASKLOOP)
7800 taskloop_iv_seen = true;
7801 OMP_CLAUSE_LASTPRIVATE_TASKLOOP_IV (c) = 1;
7803 break;
7805 else if (code == OMP_TASKLOOP
7806 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
7807 && OMP_CLAUSE_DECL (c) == iter)
7809 taskloop_iv_seen = true;
7810 OMP_CLAUSE_PRIVATE_TASKLOOP_IV (c) = 1;
7813 decl = create_temporary_var (TREE_TYPE (diff));
7814 pushdecl (decl);
7815 add_decl_expr (decl);
7816 last = create_temporary_var (TREE_TYPE (diff));
7817 pushdecl (last);
7818 add_decl_expr (last);
7819 if (c && iter_incr == NULL && TREE_CODE (incr) != INTEGER_CST
7820 && (!ordered || (i < collapse && collapse > 1)))
7822 incr_var = create_temporary_var (TREE_TYPE (diff));
7823 pushdecl (incr_var);
7824 add_decl_expr (incr_var);
7826 gcc_assert (stmts_are_full_exprs_p ());
7827 tree diffvar = NULL_TREE;
7828 if (code == OMP_TASKLOOP)
7830 if (!taskloop_iv_seen)
7832 tree ivc = build_omp_clause (locus, OMP_CLAUSE_FIRSTPRIVATE);
7833 OMP_CLAUSE_DECL (ivc) = iter;
7834 cxx_omp_finish_clause (ivc, NULL);
7835 OMP_CLAUSE_CHAIN (ivc) = clauses;
7836 clauses = ivc;
7838 tree lvc = build_omp_clause (locus, OMP_CLAUSE_FIRSTPRIVATE);
7839 OMP_CLAUSE_DECL (lvc) = last;
7840 OMP_CLAUSE_CHAIN (lvc) = clauses;
7841 clauses = lvc;
7842 diffvar = create_temporary_var (TREE_TYPE (diff));
7843 pushdecl (diffvar);
7844 add_decl_expr (diffvar);
7847 orig_pre_body = *pre_body;
7848 *pre_body = push_stmt_list ();
7849 if (orig_pre_body)
7850 add_stmt (orig_pre_body);
7851 if (init != NULL)
7852 finish_expr_stmt (build_x_modify_expr (elocus,
7853 iter, NOP_EXPR, init,
7854 tf_warning_or_error));
7855 init = build_int_cst (TREE_TYPE (diff), 0);
7856 if (c && iter_incr == NULL
7857 && (!ordered || (i < collapse && collapse > 1)))
7859 if (incr_var)
7861 finish_expr_stmt (build_x_modify_expr (elocus,
7862 incr_var, NOP_EXPR,
7863 incr, tf_warning_or_error));
7864 incr = incr_var;
7866 iter_incr = build_x_modify_expr (elocus,
7867 iter, PLUS_EXPR, incr,
7868 tf_warning_or_error);
7870 if (c && ordered && i < collapse && collapse > 1)
7871 iter_incr = incr;
7872 finish_expr_stmt (build_x_modify_expr (elocus,
7873 last, NOP_EXPR, init,
7874 tf_warning_or_error));
7875 if (diffvar)
7877 finish_expr_stmt (build_x_modify_expr (elocus,
7878 diffvar, NOP_EXPR,
7879 diff, tf_warning_or_error));
7880 diff = diffvar;
7882 *pre_body = pop_stmt_list (*pre_body);
7884 cond = cp_build_binary_op (elocus,
7885 TREE_CODE (cond), decl, diff,
7886 tf_warning_or_error);
7887 incr = build_modify_expr (elocus, decl, NULL_TREE, PLUS_EXPR,
7888 elocus, incr, NULL_TREE);
7890 orig_body = *body;
7891 *body = push_stmt_list ();
7892 iter_init = build2 (MINUS_EXPR, TREE_TYPE (diff), decl, last);
7893 iter_init = build_x_modify_expr (elocus,
7894 iter, PLUS_EXPR, iter_init,
7895 tf_warning_or_error);
7896 if (iter_init != error_mark_node)
7897 iter_init = build1 (NOP_EXPR, void_type_node, iter_init);
7898 finish_expr_stmt (iter_init);
7899 finish_expr_stmt (build_x_modify_expr (elocus,
7900 last, NOP_EXPR, decl,
7901 tf_warning_or_error));
7902 add_stmt (orig_body);
7903 *body = pop_stmt_list (*body);
7905 if (c)
7907 OMP_CLAUSE_LASTPRIVATE_STMT (c) = push_stmt_list ();
7908 if (!ordered)
7909 finish_expr_stmt (iter_incr);
7910 else
7912 iter_init = decl;
7913 if (i < collapse && collapse > 1 && !error_operand_p (iter_incr))
7914 iter_init = build2 (PLUS_EXPR, TREE_TYPE (diff),
7915 iter_init, iter_incr);
7916 iter_init = build2 (MINUS_EXPR, TREE_TYPE (diff), iter_init, last);
7917 iter_init = build_x_modify_expr (elocus,
7918 iter, PLUS_EXPR, iter_init,
7919 tf_warning_or_error);
7920 if (iter_init != error_mark_node)
7921 iter_init = build1 (NOP_EXPR, void_type_node, iter_init);
7922 finish_expr_stmt (iter_init);
7924 OMP_CLAUSE_LASTPRIVATE_STMT (c)
7925 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (c));
7928 TREE_VEC_ELT (declv, i) = decl;
7929 TREE_VEC_ELT (initv, i) = init;
7930 TREE_VEC_ELT (condv, i) = cond;
7931 TREE_VEC_ELT (incrv, i) = incr;
7932 *lastp = last;
7934 return false;
7937 /* Build and validate an OMP_FOR statement. CLAUSES, BODY, COND, INCR
7938 are directly for their associated operands in the statement. DECL
7939 and INIT are a combo; if DECL is NULL then INIT ought to be a
7940 MODIFY_EXPR, and the DECL should be extracted. PRE_BODY are
7941 optional statements that need to go before the loop into its
7942 sk_omp scope. */
7944 tree
7945 finish_omp_for (location_t locus, enum tree_code code, tree declv,
7946 tree orig_declv, tree initv, tree condv, tree incrv,
7947 tree body, tree pre_body, vec<tree> *orig_inits, tree clauses)
7949 tree omp_for = NULL, orig_incr = NULL;
7950 tree decl = NULL, init, cond, incr;
7951 tree last = NULL_TREE;
7952 location_t elocus;
7953 int i;
7954 int collapse = 1;
7955 int ordered = 0;
7957 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (initv));
7958 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (condv));
7959 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (incrv));
7960 if (TREE_VEC_LENGTH (declv) > 1)
7962 tree c;
7964 c = omp_find_clause (clauses, OMP_CLAUSE_TILE);
7965 if (c)
7966 collapse = list_length (OMP_CLAUSE_TILE_LIST (c));
7967 else
7969 c = omp_find_clause (clauses, OMP_CLAUSE_COLLAPSE);
7970 if (c)
7971 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (c));
7972 if (collapse != TREE_VEC_LENGTH (declv))
7973 ordered = TREE_VEC_LENGTH (declv);
7976 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
7978 decl = TREE_VEC_ELT (declv, i);
7979 init = TREE_VEC_ELT (initv, i);
7980 cond = TREE_VEC_ELT (condv, i);
7981 incr = TREE_VEC_ELT (incrv, i);
7982 elocus = locus;
7984 if (decl == NULL)
7986 if (init != NULL)
7987 switch (TREE_CODE (init))
7989 case MODIFY_EXPR:
7990 decl = TREE_OPERAND (init, 0);
7991 init = TREE_OPERAND (init, 1);
7992 break;
7993 case MODOP_EXPR:
7994 if (TREE_CODE (TREE_OPERAND (init, 1)) == NOP_EXPR)
7996 decl = TREE_OPERAND (init, 0);
7997 init = TREE_OPERAND (init, 2);
7999 break;
8000 default:
8001 break;
8004 if (decl == NULL)
8006 error_at (locus,
8007 "expected iteration declaration or initialization");
8008 return NULL;
8012 if (init && EXPR_HAS_LOCATION (init))
8013 elocus = EXPR_LOCATION (init);
8015 if (cond == NULL)
8017 error_at (elocus, "missing controlling predicate");
8018 return NULL;
8021 if (incr == NULL)
8023 error_at (elocus, "missing increment expression");
8024 return NULL;
8027 TREE_VEC_ELT (declv, i) = decl;
8028 TREE_VEC_ELT (initv, i) = init;
8031 if (orig_inits)
8033 bool fail = false;
8034 tree orig_init;
8035 FOR_EACH_VEC_ELT (*orig_inits, i, orig_init)
8036 if (orig_init
8037 && !c_omp_check_loop_iv_exprs (locus, declv,
8038 TREE_VEC_ELT (declv, i), orig_init,
8039 NULL_TREE, cp_walk_subtrees))
8040 fail = true;
8041 if (fail)
8042 return NULL;
8045 if (dependent_omp_for_p (declv, initv, condv, incrv))
8047 tree stmt;
8049 stmt = make_node (code);
8051 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
8053 /* This is really just a place-holder. We'll be decomposing this
8054 again and going through the cp_build_modify_expr path below when
8055 we instantiate the thing. */
8056 TREE_VEC_ELT (initv, i)
8057 = build2 (MODIFY_EXPR, void_type_node, TREE_VEC_ELT (declv, i),
8058 TREE_VEC_ELT (initv, i));
8061 TREE_TYPE (stmt) = void_type_node;
8062 OMP_FOR_INIT (stmt) = initv;
8063 OMP_FOR_COND (stmt) = condv;
8064 OMP_FOR_INCR (stmt) = incrv;
8065 OMP_FOR_BODY (stmt) = body;
8066 OMP_FOR_PRE_BODY (stmt) = pre_body;
8067 OMP_FOR_CLAUSES (stmt) = clauses;
8069 SET_EXPR_LOCATION (stmt, locus);
8070 return add_stmt (stmt);
8073 if (!orig_declv)
8074 orig_declv = copy_node (declv);
8076 if (processing_template_decl)
8077 orig_incr = make_tree_vec (TREE_VEC_LENGTH (incrv));
8079 for (i = 0; i < TREE_VEC_LENGTH (declv); )
8081 decl = TREE_VEC_ELT (declv, i);
8082 init = TREE_VEC_ELT (initv, i);
8083 cond = TREE_VEC_ELT (condv, i);
8084 incr = TREE_VEC_ELT (incrv, i);
8085 if (orig_incr)
8086 TREE_VEC_ELT (orig_incr, i) = incr;
8087 elocus = locus;
8089 if (init && EXPR_HAS_LOCATION (init))
8090 elocus = EXPR_LOCATION (init);
8092 if (!DECL_P (decl))
8094 error_at (elocus, "expected iteration declaration or initialization");
8095 return NULL;
8098 if (incr && TREE_CODE (incr) == MODOP_EXPR)
8100 if (orig_incr)
8101 TREE_VEC_ELT (orig_incr, i) = incr;
8102 incr = cp_build_modify_expr (elocus, TREE_OPERAND (incr, 0),
8103 TREE_CODE (TREE_OPERAND (incr, 1)),
8104 TREE_OPERAND (incr, 2),
8105 tf_warning_or_error);
8108 if (CLASS_TYPE_P (TREE_TYPE (decl)))
8110 if (code == OMP_SIMD)
8112 error_at (elocus, "%<#pragma omp simd%> used with class "
8113 "iteration variable %qE", decl);
8114 return NULL;
8116 if (handle_omp_for_class_iterator (i, locus, code, declv, orig_declv,
8117 initv, condv, incrv, &body,
8118 &pre_body, clauses, &last,
8119 collapse, ordered))
8120 return NULL;
8121 continue;
8124 if (!INTEGRAL_TYPE_P (TREE_TYPE (decl))
8125 && !TYPE_PTR_P (TREE_TYPE (decl)))
8127 error_at (elocus, "invalid type for iteration variable %qE", decl);
8128 return NULL;
8131 if (!processing_template_decl)
8133 init = fold_build_cleanup_point_expr (TREE_TYPE (init), init);
8134 init = cp_build_modify_expr (elocus, decl, NOP_EXPR, init,
8135 tf_warning_or_error);
8137 else
8138 init = build2 (MODIFY_EXPR, void_type_node, decl, init);
8139 if (cond
8140 && TREE_SIDE_EFFECTS (cond)
8141 && COMPARISON_CLASS_P (cond)
8142 && !processing_template_decl)
8144 tree t = TREE_OPERAND (cond, 0);
8145 if (TREE_SIDE_EFFECTS (t)
8146 && t != decl
8147 && (TREE_CODE (t) != NOP_EXPR
8148 || TREE_OPERAND (t, 0) != decl))
8149 TREE_OPERAND (cond, 0)
8150 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
8152 t = TREE_OPERAND (cond, 1);
8153 if (TREE_SIDE_EFFECTS (t)
8154 && t != decl
8155 && (TREE_CODE (t) != NOP_EXPR
8156 || TREE_OPERAND (t, 0) != decl))
8157 TREE_OPERAND (cond, 1)
8158 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
8160 if (decl == error_mark_node || init == error_mark_node)
8161 return NULL;
8163 TREE_VEC_ELT (declv, i) = decl;
8164 TREE_VEC_ELT (initv, i) = init;
8165 TREE_VEC_ELT (condv, i) = cond;
8166 TREE_VEC_ELT (incrv, i) = incr;
8167 i++;
8170 if (IS_EMPTY_STMT (pre_body))
8171 pre_body = NULL;
8173 omp_for = c_finish_omp_for (locus, code, declv, orig_declv, initv, condv,
8174 incrv, body, pre_body);
8176 /* Check for iterators appearing in lb, b or incr expressions. */
8177 if (omp_for && !c_omp_check_loop_iv (omp_for, orig_declv, cp_walk_subtrees))
8178 omp_for = NULL_TREE;
8180 if (omp_for == NULL)
8182 return NULL;
8185 add_stmt (omp_for);
8187 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INCR (omp_for)); i++)
8189 decl = TREE_OPERAND (TREE_VEC_ELT (OMP_FOR_INIT (omp_for), i), 0);
8190 incr = TREE_VEC_ELT (OMP_FOR_INCR (omp_for), i);
8192 if (TREE_CODE (incr) != MODIFY_EXPR)
8193 continue;
8195 if (TREE_SIDE_EFFECTS (TREE_OPERAND (incr, 1))
8196 && BINARY_CLASS_P (TREE_OPERAND (incr, 1))
8197 && !processing_template_decl)
8199 tree t = TREE_OPERAND (TREE_OPERAND (incr, 1), 0);
8200 if (TREE_SIDE_EFFECTS (t)
8201 && t != decl
8202 && (TREE_CODE (t) != NOP_EXPR
8203 || TREE_OPERAND (t, 0) != decl))
8204 TREE_OPERAND (TREE_OPERAND (incr, 1), 0)
8205 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
8207 t = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
8208 if (TREE_SIDE_EFFECTS (t)
8209 && t != decl
8210 && (TREE_CODE (t) != NOP_EXPR
8211 || TREE_OPERAND (t, 0) != decl))
8212 TREE_OPERAND (TREE_OPERAND (incr, 1), 1)
8213 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
8216 if (orig_incr)
8217 TREE_VEC_ELT (OMP_FOR_INCR (omp_for), i) = TREE_VEC_ELT (orig_incr, i);
8219 OMP_FOR_CLAUSES (omp_for) = clauses;
8221 /* For simd loops with non-static data member iterators, we could have added
8222 OMP_CLAUSE_LINEAR clauses without OMP_CLAUSE_LINEAR_STEP. As we know the
8223 step at this point, fill it in. */
8224 if (code == OMP_SIMD && !processing_template_decl
8225 && TREE_VEC_LENGTH (OMP_FOR_INCR (omp_for)) == 1)
8226 for (tree c = omp_find_clause (clauses, OMP_CLAUSE_LINEAR); c;
8227 c = omp_find_clause (OMP_CLAUSE_CHAIN (c), OMP_CLAUSE_LINEAR))
8228 if (OMP_CLAUSE_LINEAR_STEP (c) == NULL_TREE)
8230 decl = TREE_OPERAND (TREE_VEC_ELT (OMP_FOR_INIT (omp_for), 0), 0);
8231 gcc_assert (decl == OMP_CLAUSE_DECL (c));
8232 incr = TREE_VEC_ELT (OMP_FOR_INCR (omp_for), 0);
8233 tree step, stept;
8234 switch (TREE_CODE (incr))
8236 case PREINCREMENT_EXPR:
8237 case POSTINCREMENT_EXPR:
8238 /* c_omp_for_incr_canonicalize_ptr() should have been
8239 called to massage things appropriately. */
8240 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (decl)));
8241 OMP_CLAUSE_LINEAR_STEP (c) = build_int_cst (TREE_TYPE (decl), 1);
8242 break;
8243 case PREDECREMENT_EXPR:
8244 case POSTDECREMENT_EXPR:
8245 /* c_omp_for_incr_canonicalize_ptr() should have been
8246 called to massage things appropriately. */
8247 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (decl)));
8248 OMP_CLAUSE_LINEAR_STEP (c)
8249 = build_int_cst (TREE_TYPE (decl), -1);
8250 break;
8251 case MODIFY_EXPR:
8252 gcc_assert (TREE_OPERAND (incr, 0) == decl);
8253 incr = TREE_OPERAND (incr, 1);
8254 switch (TREE_CODE (incr))
8256 case PLUS_EXPR:
8257 if (TREE_OPERAND (incr, 1) == decl)
8258 step = TREE_OPERAND (incr, 0);
8259 else
8260 step = TREE_OPERAND (incr, 1);
8261 break;
8262 case MINUS_EXPR:
8263 case POINTER_PLUS_EXPR:
8264 gcc_assert (TREE_OPERAND (incr, 0) == decl);
8265 step = TREE_OPERAND (incr, 1);
8266 break;
8267 default:
8268 gcc_unreachable ();
8270 stept = TREE_TYPE (decl);
8271 if (POINTER_TYPE_P (stept))
8272 stept = sizetype;
8273 step = fold_convert (stept, step);
8274 if (TREE_CODE (incr) == MINUS_EXPR)
8275 step = fold_build1 (NEGATE_EXPR, stept, step);
8276 OMP_CLAUSE_LINEAR_STEP (c) = step;
8277 break;
8278 default:
8279 gcc_unreachable ();
8283 return omp_for;
8286 void
8287 finish_omp_atomic (enum tree_code code, enum tree_code opcode, tree lhs,
8288 tree rhs, tree v, tree lhs1, tree rhs1, bool seq_cst)
8290 tree orig_lhs;
8291 tree orig_rhs;
8292 tree orig_v;
8293 tree orig_lhs1;
8294 tree orig_rhs1;
8295 bool dependent_p;
8296 tree stmt;
8298 orig_lhs = lhs;
8299 orig_rhs = rhs;
8300 orig_v = v;
8301 orig_lhs1 = lhs1;
8302 orig_rhs1 = rhs1;
8303 dependent_p = false;
8304 stmt = NULL_TREE;
8306 /* Even in a template, we can detect invalid uses of the atomic
8307 pragma if neither LHS nor RHS is type-dependent. */
8308 if (processing_template_decl)
8310 dependent_p = (type_dependent_expression_p (lhs)
8311 || (rhs && type_dependent_expression_p (rhs))
8312 || (v && type_dependent_expression_p (v))
8313 || (lhs1 && type_dependent_expression_p (lhs1))
8314 || (rhs1 && type_dependent_expression_p (rhs1)));
8315 if (!dependent_p)
8317 lhs = build_non_dependent_expr (lhs);
8318 if (rhs)
8319 rhs = build_non_dependent_expr (rhs);
8320 if (v)
8321 v = build_non_dependent_expr (v);
8322 if (lhs1)
8323 lhs1 = build_non_dependent_expr (lhs1);
8324 if (rhs1)
8325 rhs1 = build_non_dependent_expr (rhs1);
8328 if (!dependent_p)
8330 bool swapped = false;
8331 if (rhs1 && cp_tree_equal (lhs, rhs))
8333 std::swap (rhs, rhs1);
8334 swapped = !commutative_tree_code (opcode);
8336 if (rhs1 && !cp_tree_equal (lhs, rhs1))
8338 if (code == OMP_ATOMIC)
8339 error ("%<#pragma omp atomic update%> uses two different "
8340 "expressions for memory");
8341 else
8342 error ("%<#pragma omp atomic capture%> uses two different "
8343 "expressions for memory");
8344 return;
8346 if (lhs1 && !cp_tree_equal (lhs, lhs1))
8348 if (code == OMP_ATOMIC)
8349 error ("%<#pragma omp atomic update%> uses two different "
8350 "expressions for memory");
8351 else
8352 error ("%<#pragma omp atomic capture%> uses two different "
8353 "expressions for memory");
8354 return;
8356 stmt = c_finish_omp_atomic (input_location, code, opcode, lhs, rhs,
8357 v, lhs1, rhs1, swapped, seq_cst,
8358 processing_template_decl != 0);
8359 if (stmt == error_mark_node)
8360 return;
8362 if (processing_template_decl)
8364 if (code == OMP_ATOMIC_READ)
8366 stmt = build_min_nt_loc (EXPR_LOCATION (orig_lhs),
8367 OMP_ATOMIC_READ, orig_lhs);
8368 OMP_ATOMIC_SEQ_CST (stmt) = seq_cst;
8369 stmt = build2 (MODIFY_EXPR, void_type_node, orig_v, stmt);
8371 else
8373 if (opcode == NOP_EXPR)
8374 stmt = build2 (MODIFY_EXPR, void_type_node, orig_lhs, orig_rhs);
8375 else
8376 stmt = build2 (opcode, void_type_node, orig_lhs, orig_rhs);
8377 if (orig_rhs1)
8378 stmt = build_min_nt_loc (EXPR_LOCATION (orig_rhs1),
8379 COMPOUND_EXPR, orig_rhs1, stmt);
8380 if (code != OMP_ATOMIC)
8382 stmt = build_min_nt_loc (EXPR_LOCATION (orig_lhs1),
8383 code, orig_lhs1, stmt);
8384 OMP_ATOMIC_SEQ_CST (stmt) = seq_cst;
8385 stmt = build2 (MODIFY_EXPR, void_type_node, orig_v, stmt);
8388 stmt = build2 (OMP_ATOMIC, void_type_node, integer_zero_node, stmt);
8389 OMP_ATOMIC_SEQ_CST (stmt) = seq_cst;
8391 finish_expr_stmt (stmt);
8394 void
8395 finish_omp_barrier (void)
8397 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_BARRIER);
8398 vec<tree, va_gc> *vec = make_tree_vector ();
8399 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
8400 release_tree_vector (vec);
8401 finish_expr_stmt (stmt);
8404 void
8405 finish_omp_flush (void)
8407 tree fn = builtin_decl_explicit (BUILT_IN_SYNC_SYNCHRONIZE);
8408 vec<tree, va_gc> *vec = make_tree_vector ();
8409 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
8410 release_tree_vector (vec);
8411 finish_expr_stmt (stmt);
8414 void
8415 finish_omp_taskwait (void)
8417 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_TASKWAIT);
8418 vec<tree, va_gc> *vec = make_tree_vector ();
8419 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
8420 release_tree_vector (vec);
8421 finish_expr_stmt (stmt);
8424 void
8425 finish_omp_taskyield (void)
8427 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_TASKYIELD);
8428 vec<tree, va_gc> *vec = make_tree_vector ();
8429 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
8430 release_tree_vector (vec);
8431 finish_expr_stmt (stmt);
8434 void
8435 finish_omp_cancel (tree clauses)
8437 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
8438 int mask = 0;
8439 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
8440 mask = 1;
8441 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
8442 mask = 2;
8443 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
8444 mask = 4;
8445 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
8446 mask = 8;
8447 else
8449 error ("%<#pragma omp cancel%> must specify one of "
8450 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> clauses");
8451 return;
8453 vec<tree, va_gc> *vec = make_tree_vector ();
8454 tree ifc = omp_find_clause (clauses, OMP_CLAUSE_IF);
8455 if (ifc != NULL_TREE)
8457 tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
8458 ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
8459 boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
8460 build_zero_cst (type));
8462 else
8463 ifc = boolean_true_node;
8464 vec->quick_push (build_int_cst (integer_type_node, mask));
8465 vec->quick_push (ifc);
8466 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
8467 release_tree_vector (vec);
8468 finish_expr_stmt (stmt);
8471 void
8472 finish_omp_cancellation_point (tree clauses)
8474 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
8475 int mask = 0;
8476 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
8477 mask = 1;
8478 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
8479 mask = 2;
8480 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
8481 mask = 4;
8482 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
8483 mask = 8;
8484 else
8486 error ("%<#pragma omp cancellation point%> must specify one of "
8487 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> clauses");
8488 return;
8490 vec<tree, va_gc> *vec
8491 = make_tree_vector_single (build_int_cst (integer_type_node, mask));
8492 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
8493 release_tree_vector (vec);
8494 finish_expr_stmt (stmt);
8497 /* Begin a __transaction_atomic or __transaction_relaxed statement.
8498 If PCOMPOUND is non-null, this is for a function-transaction-block, and we
8499 should create an extra compound stmt. */
8501 tree
8502 begin_transaction_stmt (location_t loc, tree *pcompound, int flags)
8504 tree r;
8506 if (pcompound)
8507 *pcompound = begin_compound_stmt (0);
8509 r = build_stmt (loc, TRANSACTION_EXPR, NULL_TREE);
8511 /* Only add the statement to the function if support enabled. */
8512 if (flag_tm)
8513 add_stmt (r);
8514 else
8515 error_at (loc, ((flags & TM_STMT_ATTR_RELAXED) != 0
8516 ? G_("%<__transaction_relaxed%> without "
8517 "transactional memory support enabled")
8518 : G_("%<__transaction_atomic%> without "
8519 "transactional memory support enabled")));
8521 TRANSACTION_EXPR_BODY (r) = push_stmt_list ();
8522 TREE_SIDE_EFFECTS (r) = 1;
8523 return r;
8526 /* End a __transaction_atomic or __transaction_relaxed statement.
8527 If COMPOUND_STMT is non-null, this is for a function-transaction-block,
8528 and we should end the compound. If NOEX is non-NULL, we wrap the body in
8529 a MUST_NOT_THROW_EXPR with NOEX as condition. */
8531 void
8532 finish_transaction_stmt (tree stmt, tree compound_stmt, int flags, tree noex)
8534 TRANSACTION_EXPR_BODY (stmt) = pop_stmt_list (TRANSACTION_EXPR_BODY (stmt));
8535 TRANSACTION_EXPR_OUTER (stmt) = (flags & TM_STMT_ATTR_OUTER) != 0;
8536 TRANSACTION_EXPR_RELAXED (stmt) = (flags & TM_STMT_ATTR_RELAXED) != 0;
8537 TRANSACTION_EXPR_IS_STMT (stmt) = 1;
8539 /* noexcept specifications are not allowed for function transactions. */
8540 gcc_assert (!(noex && compound_stmt));
8541 if (noex)
8543 tree body = build_must_not_throw_expr (TRANSACTION_EXPR_BODY (stmt),
8544 noex);
8545 protected_set_expr_location
8546 (body, EXPR_LOCATION (TRANSACTION_EXPR_BODY (stmt)));
8547 TREE_SIDE_EFFECTS (body) = 1;
8548 TRANSACTION_EXPR_BODY (stmt) = body;
8551 if (compound_stmt)
8552 finish_compound_stmt (compound_stmt);
8555 /* Build a __transaction_atomic or __transaction_relaxed expression. If
8556 NOEX is non-NULL, we wrap the body in a MUST_NOT_THROW_EXPR with NOEX as
8557 condition. */
8559 tree
8560 build_transaction_expr (location_t loc, tree expr, int flags, tree noex)
8562 tree ret;
8563 if (noex)
8565 expr = build_must_not_throw_expr (expr, noex);
8566 protected_set_expr_location (expr, loc);
8567 TREE_SIDE_EFFECTS (expr) = 1;
8569 ret = build1 (TRANSACTION_EXPR, TREE_TYPE (expr), expr);
8570 if (flags & TM_STMT_ATTR_RELAXED)
8571 TRANSACTION_EXPR_RELAXED (ret) = 1;
8572 TREE_SIDE_EFFECTS (ret) = 1;
8573 SET_EXPR_LOCATION (ret, loc);
8574 return ret;
8577 void
8578 init_cp_semantics (void)
8582 /* Build a STATIC_ASSERT for a static assertion with the condition
8583 CONDITION and the message text MESSAGE. LOCATION is the location
8584 of the static assertion in the source code. When MEMBER_P, this
8585 static assertion is a member of a class. */
8586 void
8587 finish_static_assert (tree condition, tree message, location_t location,
8588 bool member_p)
8590 if (message == NULL_TREE
8591 || message == error_mark_node
8592 || condition == NULL_TREE
8593 || condition == error_mark_node)
8594 return;
8596 if (check_for_bare_parameter_packs (condition))
8597 condition = error_mark_node;
8599 if (type_dependent_expression_p (condition)
8600 || value_dependent_expression_p (condition))
8602 /* We're in a template; build a STATIC_ASSERT and put it in
8603 the right place. */
8604 tree assertion;
8606 assertion = make_node (STATIC_ASSERT);
8607 STATIC_ASSERT_CONDITION (assertion) = condition;
8608 STATIC_ASSERT_MESSAGE (assertion) = message;
8609 STATIC_ASSERT_SOURCE_LOCATION (assertion) = location;
8611 if (member_p)
8612 maybe_add_class_template_decl_list (current_class_type,
8613 assertion,
8614 /*friend_p=*/0);
8615 else
8616 add_stmt (assertion);
8618 return;
8621 /* Fold the expression and convert it to a boolean value. */
8622 condition = instantiate_non_dependent_expr (condition);
8623 condition = cp_convert (boolean_type_node, condition, tf_warning_or_error);
8624 condition = maybe_constant_value (condition);
8626 if (TREE_CODE (condition) == INTEGER_CST && !integer_zerop (condition))
8627 /* Do nothing; the condition is satisfied. */
8629 else
8631 location_t saved_loc = input_location;
8633 input_location = location;
8634 if (TREE_CODE (condition) == INTEGER_CST
8635 && integer_zerop (condition))
8637 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
8638 (TREE_TYPE (TREE_TYPE (message))));
8639 int len = TREE_STRING_LENGTH (message) / sz - 1;
8640 /* Report the error. */
8641 if (len == 0)
8642 error ("static assertion failed");
8643 else
8644 error ("static assertion failed: %s",
8645 TREE_STRING_POINTER (message));
8647 else if (condition && condition != error_mark_node)
8649 error ("non-constant condition for static assertion");
8650 if (require_potential_rvalue_constant_expression (condition))
8651 cxx_constant_value (condition);
8653 input_location = saved_loc;
8657 /* Implements the C++0x decltype keyword. Returns the type of EXPR,
8658 suitable for use as a type-specifier.
8660 ID_EXPRESSION_OR_MEMBER_ACCESS_P is true when EXPR was parsed as an
8661 id-expression or a class member access, FALSE when it was parsed as
8662 a full expression. */
8664 tree
8665 finish_decltype_type (tree expr, bool id_expression_or_member_access_p,
8666 tsubst_flags_t complain)
8668 tree type = NULL_TREE;
8670 if (!expr || error_operand_p (expr))
8671 return error_mark_node;
8673 if (TYPE_P (expr)
8674 || TREE_CODE (expr) == TYPE_DECL
8675 || (TREE_CODE (expr) == BIT_NOT_EXPR
8676 && TYPE_P (TREE_OPERAND (expr, 0))))
8678 if (complain & tf_error)
8679 error ("argument to decltype must be an expression");
8680 return error_mark_node;
8683 /* Depending on the resolution of DR 1172, we may later need to distinguish
8684 instantiation-dependent but not type-dependent expressions so that, say,
8685 A<decltype(sizeof(T))>::U doesn't require 'typename'. */
8686 if (instantiation_dependent_uneval_expression_p (expr))
8688 type = cxx_make_type (DECLTYPE_TYPE);
8689 DECLTYPE_TYPE_EXPR (type) = expr;
8690 DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (type)
8691 = id_expression_or_member_access_p;
8692 SET_TYPE_STRUCTURAL_EQUALITY (type);
8694 return type;
8697 /* The type denoted by decltype(e) is defined as follows: */
8699 expr = resolve_nondeduced_context (expr, complain);
8701 if (invalid_nonstatic_memfn_p (input_location, expr, complain))
8702 return error_mark_node;
8704 if (type_unknown_p (expr))
8706 if (complain & tf_error)
8707 error ("decltype cannot resolve address of overloaded function");
8708 return error_mark_node;
8711 /* To get the size of a static data member declared as an array of
8712 unknown bound, we need to instantiate it. */
8713 if (VAR_P (expr)
8714 && VAR_HAD_UNKNOWN_BOUND (expr)
8715 && DECL_TEMPLATE_INSTANTIATION (expr))
8716 instantiate_decl (expr, /*defer_ok*/true, /*expl_inst_mem*/false);
8718 if (id_expression_or_member_access_p)
8720 /* If e is an id-expression or a class member access (5.2.5
8721 [expr.ref]), decltype(e) is defined as the type of the entity
8722 named by e. If there is no such entity, or e names a set of
8723 overloaded functions, the program is ill-formed. */
8724 if (identifier_p (expr))
8725 expr = lookup_name (expr);
8727 if (INDIRECT_REF_P (expr))
8728 /* This can happen when the expression is, e.g., "a.b". Just
8729 look at the underlying operand. */
8730 expr = TREE_OPERAND (expr, 0);
8732 if (TREE_CODE (expr) == OFFSET_REF
8733 || TREE_CODE (expr) == MEMBER_REF
8734 || TREE_CODE (expr) == SCOPE_REF)
8735 /* We're only interested in the field itself. If it is a
8736 BASELINK, we will need to see through it in the next
8737 step. */
8738 expr = TREE_OPERAND (expr, 1);
8740 if (BASELINK_P (expr))
8741 /* See through BASELINK nodes to the underlying function. */
8742 expr = BASELINK_FUNCTIONS (expr);
8744 /* decltype of a decomposition name drops references in the tuple case
8745 (unlike decltype of a normal variable) and keeps cv-qualifiers from
8746 the containing object in the other cases (unlike decltype of a member
8747 access expression). */
8748 if (DECL_DECOMPOSITION_P (expr))
8750 if (DECL_HAS_VALUE_EXPR_P (expr))
8751 /* Expr is an array or struct subobject proxy, handle
8752 bit-fields properly. */
8753 return unlowered_expr_type (expr);
8754 else
8755 /* Expr is a reference variable for the tuple case. */
8756 return lookup_decomp_type (expr);
8759 switch (TREE_CODE (expr))
8761 case FIELD_DECL:
8762 if (DECL_BIT_FIELD_TYPE (expr))
8764 type = DECL_BIT_FIELD_TYPE (expr);
8765 break;
8767 /* Fall through for fields that aren't bitfields. */
8768 gcc_fallthrough ();
8770 case FUNCTION_DECL:
8771 case VAR_DECL:
8772 case CONST_DECL:
8773 case PARM_DECL:
8774 case RESULT_DECL:
8775 case TEMPLATE_PARM_INDEX:
8776 expr = mark_type_use (expr);
8777 type = TREE_TYPE (expr);
8778 break;
8780 case ERROR_MARK:
8781 type = error_mark_node;
8782 break;
8784 case COMPONENT_REF:
8785 case COMPOUND_EXPR:
8786 mark_type_use (expr);
8787 type = is_bitfield_expr_with_lowered_type (expr);
8788 if (!type)
8789 type = TREE_TYPE (TREE_OPERAND (expr, 1));
8790 break;
8792 case BIT_FIELD_REF:
8793 gcc_unreachable ();
8795 case INTEGER_CST:
8796 case PTRMEM_CST:
8797 /* We can get here when the id-expression refers to an
8798 enumerator or non-type template parameter. */
8799 type = TREE_TYPE (expr);
8800 break;
8802 default:
8803 /* Handle instantiated template non-type arguments. */
8804 type = TREE_TYPE (expr);
8805 break;
8808 else
8810 /* Within a lambda-expression:
8812 Every occurrence of decltype((x)) where x is a possibly
8813 parenthesized id-expression that names an entity of
8814 automatic storage duration is treated as if x were
8815 transformed into an access to a corresponding data member
8816 of the closure type that would have been declared if x
8817 were a use of the denoted entity. */
8818 if (outer_automatic_var_p (expr)
8819 && current_function_decl
8820 && LAMBDA_FUNCTION_P (current_function_decl))
8821 type = capture_decltype (expr);
8822 else if (error_operand_p (expr))
8823 type = error_mark_node;
8824 else if (expr == current_class_ptr)
8825 /* If the expression is just "this", we want the
8826 cv-unqualified pointer for the "this" type. */
8827 type = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
8828 else
8830 /* Otherwise, where T is the type of e, if e is an lvalue,
8831 decltype(e) is defined as T&; if an xvalue, T&&; otherwise, T. */
8832 cp_lvalue_kind clk = lvalue_kind (expr);
8833 type = unlowered_expr_type (expr);
8834 gcc_assert (TREE_CODE (type) != REFERENCE_TYPE);
8836 /* For vector types, pick a non-opaque variant. */
8837 if (VECTOR_TYPE_P (type))
8838 type = strip_typedefs (type);
8840 if (clk != clk_none && !(clk & clk_class))
8841 type = cp_build_reference_type (type, (clk & clk_rvalueref));
8845 return type;
8848 /* Called from trait_expr_value to evaluate either __has_nothrow_assign or
8849 __has_nothrow_copy, depending on assign_p. Returns true iff all
8850 the copy {ctor,assign} fns are nothrow. */
8852 static bool
8853 classtype_has_nothrow_assign_or_copy_p (tree type, bool assign_p)
8855 tree fns = NULL_TREE;
8857 if (assign_p || TYPE_HAS_COPY_CTOR (type))
8858 fns = get_class_binding (type, assign_p ? assign_op_identifier
8859 : ctor_identifier);
8861 bool saw_copy = false;
8862 for (ovl_iterator iter (fns); iter; ++iter)
8864 tree fn = *iter;
8866 if (copy_fn_p (fn) > 0)
8868 saw_copy = true;
8869 maybe_instantiate_noexcept (fn);
8870 if (!TYPE_NOTHROW_P (TREE_TYPE (fn)))
8871 return false;
8875 return saw_copy;
8878 /* Actually evaluates the trait. */
8880 static bool
8881 trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
8883 enum tree_code type_code1;
8884 tree t;
8886 type_code1 = TREE_CODE (type1);
8888 switch (kind)
8890 case CPTK_HAS_NOTHROW_ASSIGN:
8891 type1 = strip_array_types (type1);
8892 return (!CP_TYPE_CONST_P (type1) && type_code1 != REFERENCE_TYPE
8893 && (trait_expr_value (CPTK_HAS_TRIVIAL_ASSIGN, type1, type2)
8894 || (CLASS_TYPE_P (type1)
8895 && classtype_has_nothrow_assign_or_copy_p (type1,
8896 true))));
8898 case CPTK_HAS_TRIVIAL_ASSIGN:
8899 /* ??? The standard seems to be missing the "or array of such a class
8900 type" wording for this trait. */
8901 type1 = strip_array_types (type1);
8902 return (!CP_TYPE_CONST_P (type1) && type_code1 != REFERENCE_TYPE
8903 && (trivial_type_p (type1)
8904 || (CLASS_TYPE_P (type1)
8905 && TYPE_HAS_TRIVIAL_COPY_ASSIGN (type1))));
8907 case CPTK_HAS_NOTHROW_CONSTRUCTOR:
8908 type1 = strip_array_types (type1);
8909 return (trait_expr_value (CPTK_HAS_TRIVIAL_CONSTRUCTOR, type1, type2)
8910 || (CLASS_TYPE_P (type1)
8911 && (t = locate_ctor (type1))
8912 && (maybe_instantiate_noexcept (t),
8913 TYPE_NOTHROW_P (TREE_TYPE (t)))));
8915 case CPTK_HAS_TRIVIAL_CONSTRUCTOR:
8916 type1 = strip_array_types (type1);
8917 return (trivial_type_p (type1)
8918 || (CLASS_TYPE_P (type1) && TYPE_HAS_TRIVIAL_DFLT (type1)));
8920 case CPTK_HAS_NOTHROW_COPY:
8921 type1 = strip_array_types (type1);
8922 return (trait_expr_value (CPTK_HAS_TRIVIAL_COPY, type1, type2)
8923 || (CLASS_TYPE_P (type1)
8924 && classtype_has_nothrow_assign_or_copy_p (type1, false)));
8926 case CPTK_HAS_TRIVIAL_COPY:
8927 /* ??? The standard seems to be missing the "or array of such a class
8928 type" wording for this trait. */
8929 type1 = strip_array_types (type1);
8930 return (trivial_type_p (type1) || type_code1 == REFERENCE_TYPE
8931 || (CLASS_TYPE_P (type1) && TYPE_HAS_TRIVIAL_COPY_CTOR (type1)));
8933 case CPTK_HAS_TRIVIAL_DESTRUCTOR:
8934 type1 = strip_array_types (type1);
8935 return (trivial_type_p (type1) || type_code1 == REFERENCE_TYPE
8936 || (CLASS_TYPE_P (type1)
8937 && TYPE_HAS_TRIVIAL_DESTRUCTOR (type1)));
8939 case CPTK_HAS_VIRTUAL_DESTRUCTOR:
8940 return type_has_virtual_destructor (type1);
8942 case CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS:
8943 return type_has_unique_obj_representations (type1);
8945 case CPTK_IS_ABSTRACT:
8946 return ABSTRACT_CLASS_TYPE_P (type1);
8948 case CPTK_IS_AGGREGATE:
8949 return CP_AGGREGATE_TYPE_P (type1);
8951 case CPTK_IS_BASE_OF:
8952 return (NON_UNION_CLASS_TYPE_P (type1) && NON_UNION_CLASS_TYPE_P (type2)
8953 && (same_type_ignoring_top_level_qualifiers_p (type1, type2)
8954 || DERIVED_FROM_P (type1, type2)));
8956 case CPTK_IS_CLASS:
8957 return NON_UNION_CLASS_TYPE_P (type1);
8959 case CPTK_IS_EMPTY:
8960 return NON_UNION_CLASS_TYPE_P (type1) && CLASSTYPE_EMPTY_P (type1);
8962 case CPTK_IS_ENUM:
8963 return type_code1 == ENUMERAL_TYPE;
8965 case CPTK_IS_FINAL:
8966 return CLASS_TYPE_P (type1) && CLASSTYPE_FINAL (type1);
8968 case CPTK_IS_LITERAL_TYPE:
8969 return literal_type_p (type1);
8971 case CPTK_IS_POD:
8972 return pod_type_p (type1);
8974 case CPTK_IS_POLYMORPHIC:
8975 return CLASS_TYPE_P (type1) && TYPE_POLYMORPHIC_P (type1);
8977 case CPTK_IS_SAME_AS:
8978 return same_type_p (type1, type2);
8980 case CPTK_IS_STD_LAYOUT:
8981 return std_layout_type_p (type1);
8983 case CPTK_IS_TRIVIAL:
8984 return trivial_type_p (type1);
8986 case CPTK_IS_TRIVIALLY_ASSIGNABLE:
8987 return is_trivially_xible (MODIFY_EXPR, type1, type2);
8989 case CPTK_IS_TRIVIALLY_CONSTRUCTIBLE:
8990 return is_trivially_xible (INIT_EXPR, type1, type2);
8992 case CPTK_IS_TRIVIALLY_COPYABLE:
8993 return trivially_copyable_p (type1);
8995 case CPTK_IS_UNION:
8996 return type_code1 == UNION_TYPE;
8998 case CPTK_IS_ASSIGNABLE:
8999 return is_xible (MODIFY_EXPR, type1, type2);
9001 case CPTK_IS_CONSTRUCTIBLE:
9002 return is_xible (INIT_EXPR, type1, type2);
9004 default:
9005 gcc_unreachable ();
9006 return false;
9010 /* If TYPE is an array of unknown bound, or (possibly cv-qualified)
9011 void, or a complete type, returns true, otherwise false. */
9013 static bool
9014 check_trait_type (tree type)
9016 if (type == NULL_TREE)
9017 return true;
9019 if (TREE_CODE (type) == TREE_LIST)
9020 return (check_trait_type (TREE_VALUE (type))
9021 && check_trait_type (TREE_CHAIN (type)));
9023 if (TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type)
9024 && COMPLETE_TYPE_P (TREE_TYPE (type)))
9025 return true;
9027 if (VOID_TYPE_P (type))
9028 return true;
9030 return !!complete_type_or_else (strip_array_types (type), NULL_TREE);
9033 /* Process a trait expression. */
9035 tree
9036 finish_trait_expr (cp_trait_kind kind, tree type1, tree type2)
9038 if (type1 == error_mark_node
9039 || type2 == error_mark_node)
9040 return error_mark_node;
9042 if (processing_template_decl)
9044 tree trait_expr = make_node (TRAIT_EXPR);
9045 TREE_TYPE (trait_expr) = boolean_type_node;
9046 TRAIT_EXPR_TYPE1 (trait_expr) = type1;
9047 TRAIT_EXPR_TYPE2 (trait_expr) = type2;
9048 TRAIT_EXPR_KIND (trait_expr) = kind;
9049 return trait_expr;
9052 switch (kind)
9054 case CPTK_HAS_NOTHROW_ASSIGN:
9055 case CPTK_HAS_TRIVIAL_ASSIGN:
9056 case CPTK_HAS_NOTHROW_CONSTRUCTOR:
9057 case CPTK_HAS_TRIVIAL_CONSTRUCTOR:
9058 case CPTK_HAS_NOTHROW_COPY:
9059 case CPTK_HAS_TRIVIAL_COPY:
9060 case CPTK_HAS_TRIVIAL_DESTRUCTOR:
9061 case CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS:
9062 case CPTK_HAS_VIRTUAL_DESTRUCTOR:
9063 case CPTK_IS_ABSTRACT:
9064 case CPTK_IS_AGGREGATE:
9065 case CPTK_IS_EMPTY:
9066 case CPTK_IS_FINAL:
9067 case CPTK_IS_LITERAL_TYPE:
9068 case CPTK_IS_POD:
9069 case CPTK_IS_POLYMORPHIC:
9070 case CPTK_IS_STD_LAYOUT:
9071 case CPTK_IS_TRIVIAL:
9072 case CPTK_IS_TRIVIALLY_COPYABLE:
9073 if (!check_trait_type (type1))
9074 return error_mark_node;
9075 break;
9077 case CPTK_IS_ASSIGNABLE:
9078 case CPTK_IS_CONSTRUCTIBLE:
9079 break;
9081 case CPTK_IS_TRIVIALLY_ASSIGNABLE:
9082 case CPTK_IS_TRIVIALLY_CONSTRUCTIBLE:
9083 if (!check_trait_type (type1)
9084 || !check_trait_type (type2))
9085 return error_mark_node;
9086 break;
9088 case CPTK_IS_BASE_OF:
9089 if (NON_UNION_CLASS_TYPE_P (type1) && NON_UNION_CLASS_TYPE_P (type2)
9090 && !same_type_ignoring_top_level_qualifiers_p (type1, type2)
9091 && !complete_type_or_else (type2, NULL_TREE))
9092 /* We already issued an error. */
9093 return error_mark_node;
9094 break;
9096 case CPTK_IS_CLASS:
9097 case CPTK_IS_ENUM:
9098 case CPTK_IS_UNION:
9099 case CPTK_IS_SAME_AS:
9100 break;
9102 default:
9103 gcc_unreachable ();
9106 return (trait_expr_value (kind, type1, type2)
9107 ? boolean_true_node : boolean_false_node);
9110 /* Do-nothing variants of functions to handle pragma FLOAT_CONST_DECIMAL64,
9111 which is ignored for C++. */
9113 void
9114 set_float_const_decimal64 (void)
9118 void
9119 clear_float_const_decimal64 (void)
9123 bool
9124 float_const_decimal64_p (void)
9126 return 0;
9130 /* Return true if T designates the implied `this' parameter. */
9132 bool
9133 is_this_parameter (tree t)
9135 if (!DECL_P (t) || DECL_NAME (t) != this_identifier)
9136 return false;
9137 gcc_assert (TREE_CODE (t) == PARM_DECL || is_capture_proxy (t)
9138 || (cp_binding_oracle && TREE_CODE (t) == VAR_DECL));
9139 return true;
9142 /* Insert the deduced return type for an auto function. */
9144 void
9145 apply_deduced_return_type (tree fco, tree return_type)
9147 tree result;
9149 if (return_type == error_mark_node)
9150 return;
9152 if (DECL_CONV_FN_P (fco))
9153 DECL_NAME (fco) = make_conv_op_name (return_type);
9155 TREE_TYPE (fco) = change_return_type (return_type, TREE_TYPE (fco));
9157 result = DECL_RESULT (fco);
9158 if (result == NULL_TREE)
9159 return;
9160 if (TREE_TYPE (result) == return_type)
9161 return;
9163 if (!processing_template_decl && !VOID_TYPE_P (return_type)
9164 && !complete_type_or_else (return_type, NULL_TREE))
9165 return;
9167 /* We already have a DECL_RESULT from start_preparsed_function.
9168 Now we need to redo the work it and allocate_struct_function
9169 did to reflect the new type. */
9170 gcc_assert (current_function_decl == fco);
9171 result = build_decl (input_location, RESULT_DECL, NULL_TREE,
9172 TYPE_MAIN_VARIANT (return_type));
9173 DECL_ARTIFICIAL (result) = 1;
9174 DECL_IGNORED_P (result) = 1;
9175 cp_apply_type_quals_to_decl (cp_type_quals (return_type),
9176 result);
9178 DECL_RESULT (fco) = result;
9180 if (!processing_template_decl)
9182 bool aggr = aggregate_value_p (result, fco);
9183 #ifdef PCC_STATIC_STRUCT_RETURN
9184 cfun->returns_pcc_struct = aggr;
9185 #endif
9186 cfun->returns_struct = aggr;
9191 /* DECL is a local variable or parameter from the surrounding scope of a
9192 lambda-expression. Returns the decltype for a use of the capture field
9193 for DECL even if it hasn't been captured yet. */
9195 static tree
9196 capture_decltype (tree decl)
9198 tree lam = CLASSTYPE_LAMBDA_EXPR (DECL_CONTEXT (current_function_decl));
9199 /* FIXME do lookup instead of list walk? */
9200 tree cap = value_member (decl, LAMBDA_EXPR_CAPTURE_LIST (lam));
9201 tree type;
9203 if (cap)
9204 type = TREE_TYPE (TREE_PURPOSE (cap));
9205 else
9206 switch (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lam))
9208 case CPLD_NONE:
9209 error ("%qD is not captured", decl);
9210 return error_mark_node;
9212 case CPLD_COPY:
9213 type = TREE_TYPE (decl);
9214 if (TREE_CODE (type) == REFERENCE_TYPE
9215 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
9216 type = TREE_TYPE (type);
9217 break;
9219 case CPLD_REFERENCE:
9220 type = TREE_TYPE (decl);
9221 if (TREE_CODE (type) != REFERENCE_TYPE)
9222 type = build_reference_type (TREE_TYPE (decl));
9223 break;
9225 default:
9226 gcc_unreachable ();
9229 if (TREE_CODE (type) != REFERENCE_TYPE)
9231 if (!LAMBDA_EXPR_MUTABLE_P (lam))
9232 type = cp_build_qualified_type (type, (cp_type_quals (type)
9233 |TYPE_QUAL_CONST));
9234 type = build_reference_type (type);
9236 return type;
9239 /* Build a unary fold expression of EXPR over OP. If IS_RIGHT is true,
9240 this is a right unary fold. Otherwise it is a left unary fold. */
9242 static tree
9243 finish_unary_fold_expr (tree expr, int op, tree_code dir)
9245 // Build a pack expansion (assuming expr has pack type).
9246 if (!uses_parameter_packs (expr))
9248 error_at (location_of (expr), "operand of fold expression has no "
9249 "unexpanded parameter packs");
9250 return error_mark_node;
9252 tree pack = make_pack_expansion (expr);
9254 // Build the fold expression.
9255 tree code = build_int_cstu (integer_type_node, abs (op));
9256 tree fold = build_min_nt_loc (UNKNOWN_LOCATION, dir, code, pack);
9257 FOLD_EXPR_MODIFY_P (fold) = (op < 0);
9258 return fold;
9261 tree
9262 finish_left_unary_fold_expr (tree expr, int op)
9264 return finish_unary_fold_expr (expr, op, UNARY_LEFT_FOLD_EXPR);
9267 tree
9268 finish_right_unary_fold_expr (tree expr, int op)
9270 return finish_unary_fold_expr (expr, op, UNARY_RIGHT_FOLD_EXPR);
9273 /* Build a binary fold expression over EXPR1 and EXPR2. The
9274 associativity of the fold is determined by EXPR1 and EXPR2 (whichever
9275 has an unexpanded parameter pack). */
9277 tree
9278 finish_binary_fold_expr (tree pack, tree init, int op, tree_code dir)
9280 pack = make_pack_expansion (pack);
9281 tree code = build_int_cstu (integer_type_node, abs (op));
9282 tree fold = build_min_nt_loc (UNKNOWN_LOCATION, dir, code, pack, init);
9283 FOLD_EXPR_MODIFY_P (fold) = (op < 0);
9284 return fold;
9287 tree
9288 finish_binary_fold_expr (tree expr1, tree expr2, int op)
9290 // Determine which expr has an unexpanded parameter pack and
9291 // set the pack and initial term.
9292 bool pack1 = uses_parameter_packs (expr1);
9293 bool pack2 = uses_parameter_packs (expr2);
9294 if (pack1 && !pack2)
9295 return finish_binary_fold_expr (expr1, expr2, op, BINARY_RIGHT_FOLD_EXPR);
9296 else if (pack2 && !pack1)
9297 return finish_binary_fold_expr (expr2, expr1, op, BINARY_LEFT_FOLD_EXPR);
9298 else
9300 if (pack1)
9301 error ("both arguments in binary fold have unexpanded parameter packs");
9302 else
9303 error ("no unexpanded parameter packs in binary fold");
9305 return error_mark_node;
9308 /* Finish __builtin_launder (arg). */
9310 tree
9311 finish_builtin_launder (location_t loc, tree arg, tsubst_flags_t complain)
9313 tree orig_arg = arg;
9314 if (!type_dependent_expression_p (arg))
9315 arg = decay_conversion (arg, complain);
9316 if (error_operand_p (arg))
9317 return error_mark_node;
9318 if (!type_dependent_expression_p (arg)
9319 && TREE_CODE (TREE_TYPE (arg)) != POINTER_TYPE)
9321 error_at (loc, "non-pointer argument to %<__builtin_launder%>");
9322 return error_mark_node;
9324 if (processing_template_decl)
9325 arg = orig_arg;
9326 return build_call_expr_internal_loc (loc, IFN_LAUNDER,
9327 TREE_TYPE (arg), 1, arg);
9330 #include "gt-cp-semantics.h"