Daily bump.
[official-gcc.git] / gcc / cp / semantics.c
blobf0cee68e46f454dd667e83d3e1867ff2c6c68420
1 /* Perform the semantic phase of parsing, i.e., the process of
2 building tree structure, checking semantic consistency, and
3 building RTL. These routines are used both during actual parsing
4 and during the instantiation of template functions.
6 Copyright (C) 1998-2018 Free Software Foundation, Inc.
7 Written by Mark Mitchell (mmitchell@usa.net) based on code found
8 formerly in parse.y and pt.c.
10 This file is part of GCC.
12 GCC is free software; you can redistribute it and/or modify it
13 under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 3, or (at your option)
15 any later version.
17 GCC is distributed in the hope that it will be useful, but
18 WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with GCC; see the file COPYING3. If not see
24 <http://www.gnu.org/licenses/>. */
26 #include "config.h"
27 #include "system.h"
28 #include "coretypes.h"
29 #include "target.h"
30 #include "bitmap.h"
31 #include "cp-tree.h"
32 #include "stringpool.h"
33 #include "cgraph.h"
34 #include "stmt.h"
35 #include "varasm.h"
36 #include "stor-layout.h"
37 #include "c-family/c-objc.h"
38 #include "tree-inline.h"
39 #include "intl.h"
40 #include "tree-iterator.h"
41 #include "omp-general.h"
42 #include "convert.h"
43 #include "stringpool.h"
44 #include "attribs.h"
45 #include "gomp-constants.h"
46 #include "predict.h"
48 /* There routines provide a modular interface to perform many parsing
49 operations. They may therefore be used during actual parsing, or
50 during template instantiation, which may be regarded as a
51 degenerate form of parsing. */
53 static tree maybe_convert_cond (tree);
54 static tree finalize_nrv_r (tree *, int *, void *);
55 static tree capture_decltype (tree);
57 /* Used for OpenMP non-static data member privatization. */
59 static hash_map<tree, tree> *omp_private_member_map;
60 static vec<tree> omp_private_member_vec;
61 static bool omp_private_member_ignore_next;
64 /* Deferred Access Checking Overview
65 ---------------------------------
67 Most C++ expressions and declarations require access checking
68 to be performed during parsing. However, in several cases,
69 this has to be treated differently.
71 For member declarations, access checking has to be deferred
72 until more information about the declaration is known. For
73 example:
75 class A {
76 typedef int X;
77 public:
78 X f();
81 A::X A::f();
82 A::X g();
84 When we are parsing the function return type `A::X', we don't
85 really know if this is allowed until we parse the function name.
87 Furthermore, some contexts require that access checking is
88 never performed at all. These include class heads, and template
89 instantiations.
91 Typical use of access checking functions is described here:
93 1. When we enter a context that requires certain access checking
94 mode, the function `push_deferring_access_checks' is called with
95 DEFERRING argument specifying the desired mode. Access checking
96 may be performed immediately (dk_no_deferred), deferred
97 (dk_deferred), or not performed (dk_no_check).
99 2. When a declaration such as a type, or a variable, is encountered,
100 the function `perform_or_defer_access_check' is called. It
101 maintains a vector of all deferred checks.
103 3. The global `current_class_type' or `current_function_decl' is then
104 setup by the parser. `enforce_access' relies on these information
105 to check access.
107 4. Upon exiting the context mentioned in step 1,
108 `perform_deferred_access_checks' is called to check all declaration
109 stored in the vector. `pop_deferring_access_checks' is then
110 called to restore the previous access checking mode.
112 In case of parsing error, we simply call `pop_deferring_access_checks'
113 without `perform_deferred_access_checks'. */
115 struct GTY(()) deferred_access {
116 /* A vector representing name-lookups for which we have deferred
117 checking access controls. We cannot check the accessibility of
118 names used in a decl-specifier-seq until we know what is being
119 declared because code like:
121 class A {
122 class B {};
123 B* f();
126 A::B* A::f() { return 0; }
128 is valid, even though `A::B' is not generally accessible. */
129 vec<deferred_access_check, va_gc> * GTY(()) deferred_access_checks;
131 /* The current mode of access checks. */
132 enum deferring_kind deferring_access_checks_kind;
136 /* Data for deferred access checking. */
137 static GTY(()) vec<deferred_access, va_gc> *deferred_access_stack;
138 static GTY(()) unsigned deferred_access_no_check;
140 /* Save the current deferred access states and start deferred
141 access checking iff DEFER_P is true. */
143 void
144 push_deferring_access_checks (deferring_kind deferring)
146 /* For context like template instantiation, access checking
147 disabling applies to all nested context. */
148 if (deferred_access_no_check || deferring == dk_no_check)
149 deferred_access_no_check++;
150 else
152 deferred_access e = {NULL, deferring};
153 vec_safe_push (deferred_access_stack, e);
157 /* Save the current deferred access states and start deferred access
158 checking, continuing the set of deferred checks in CHECKS. */
160 void
161 reopen_deferring_access_checks (vec<deferred_access_check, va_gc> * checks)
163 push_deferring_access_checks (dk_deferred);
164 if (!deferred_access_no_check)
165 deferred_access_stack->last().deferred_access_checks = checks;
168 /* Resume deferring access checks again after we stopped doing
169 this previously. */
171 void
172 resume_deferring_access_checks (void)
174 if (!deferred_access_no_check)
175 deferred_access_stack->last().deferring_access_checks_kind = dk_deferred;
178 /* Stop deferring access checks. */
180 void
181 stop_deferring_access_checks (void)
183 if (!deferred_access_no_check)
184 deferred_access_stack->last().deferring_access_checks_kind = dk_no_deferred;
187 /* Discard the current deferred access checks and restore the
188 previous states. */
190 void
191 pop_deferring_access_checks (void)
193 if (deferred_access_no_check)
194 deferred_access_no_check--;
195 else
196 deferred_access_stack->pop ();
199 /* Returns a TREE_LIST representing the deferred checks.
200 The TREE_PURPOSE of each node is the type through which the
201 access occurred; the TREE_VALUE is the declaration named.
204 vec<deferred_access_check, va_gc> *
205 get_deferred_access_checks (void)
207 if (deferred_access_no_check)
208 return NULL;
209 else
210 return (deferred_access_stack->last().deferred_access_checks);
213 /* Take current deferred checks and combine with the
214 previous states if we also defer checks previously.
215 Otherwise perform checks now. */
217 void
218 pop_to_parent_deferring_access_checks (void)
220 if (deferred_access_no_check)
221 deferred_access_no_check--;
222 else
224 vec<deferred_access_check, va_gc> *checks;
225 deferred_access *ptr;
227 checks = (deferred_access_stack->last ().deferred_access_checks);
229 deferred_access_stack->pop ();
230 ptr = &deferred_access_stack->last ();
231 if (ptr->deferring_access_checks_kind == dk_no_deferred)
233 /* Check access. */
234 perform_access_checks (checks, tf_warning_or_error);
236 else
238 /* Merge with parent. */
239 int i, j;
240 deferred_access_check *chk, *probe;
242 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
244 FOR_EACH_VEC_SAFE_ELT (ptr->deferred_access_checks, j, probe)
246 if (probe->binfo == chk->binfo &&
247 probe->decl == chk->decl &&
248 probe->diag_decl == chk->diag_decl)
249 goto found;
251 /* Insert into parent's checks. */
252 vec_safe_push (ptr->deferred_access_checks, *chk);
253 found:;
259 /* Perform the access checks in CHECKS. The TREE_PURPOSE of each node
260 is the BINFO indicating the qualifying scope used to access the
261 DECL node stored in the TREE_VALUE of the node. If CHECKS is empty
262 or we aren't in SFINAE context or all the checks succeed return TRUE,
263 otherwise FALSE. */
265 bool
266 perform_access_checks (vec<deferred_access_check, va_gc> *checks,
267 tsubst_flags_t complain)
269 int i;
270 deferred_access_check *chk;
271 location_t loc = input_location;
272 bool ok = true;
274 if (!checks)
275 return true;
277 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
279 input_location = chk->loc;
280 ok &= enforce_access (chk->binfo, chk->decl, chk->diag_decl, complain);
283 input_location = loc;
284 return (complain & tf_error) ? true : ok;
287 /* Perform the deferred access checks.
289 After performing the checks, we still have to keep the list
290 `deferred_access_stack->deferred_access_checks' since we may want
291 to check access for them again later in a different context.
292 For example:
294 class A {
295 typedef int X;
296 static X a;
298 A::X A::a, x; // No error for `A::a', error for `x'
300 We have to perform deferred access of `A::X', first with `A::a',
301 next with `x'. Return value like perform_access_checks above. */
303 bool
304 perform_deferred_access_checks (tsubst_flags_t complain)
306 return perform_access_checks (get_deferred_access_checks (), complain);
309 /* Defer checking the accessibility of DECL, when looked up in
310 BINFO. DIAG_DECL is the declaration to use to print diagnostics.
311 Return value like perform_access_checks above.
312 If non-NULL, report failures to AFI. */
314 bool
315 perform_or_defer_access_check (tree binfo, tree decl, tree diag_decl,
316 tsubst_flags_t complain,
317 access_failure_info *afi)
319 int i;
320 deferred_access *ptr;
321 deferred_access_check *chk;
324 /* Exit if we are in a context that no access checking is performed.
326 if (deferred_access_no_check)
327 return true;
329 gcc_assert (TREE_CODE (binfo) == TREE_BINFO);
331 ptr = &deferred_access_stack->last ();
333 /* If we are not supposed to defer access checks, just check now. */
334 if (ptr->deferring_access_checks_kind == dk_no_deferred)
336 bool ok = enforce_access (binfo, decl, diag_decl, complain, afi);
337 return (complain & tf_error) ? true : ok;
340 /* See if we are already going to perform this check. */
341 FOR_EACH_VEC_SAFE_ELT (ptr->deferred_access_checks, i, chk)
343 if (chk->decl == decl && chk->binfo == binfo &&
344 chk->diag_decl == diag_decl)
346 return true;
349 /* If not, record the check. */
350 deferred_access_check new_access = {binfo, decl, diag_decl, input_location};
351 vec_safe_push (ptr->deferred_access_checks, new_access);
353 return true;
356 /* Returns nonzero if the current statement is a full expression,
357 i.e. temporaries created during that statement should be destroyed
358 at the end of the statement. */
361 stmts_are_full_exprs_p (void)
363 return current_stmt_tree ()->stmts_are_full_exprs_p;
366 /* T is a statement. Add it to the statement-tree. This is the C++
367 version. The C/ObjC frontends have a slightly different version of
368 this function. */
370 tree
371 add_stmt (tree t)
373 enum tree_code code = TREE_CODE (t);
375 if (EXPR_P (t) && code != LABEL_EXPR)
377 if (!EXPR_HAS_LOCATION (t))
378 SET_EXPR_LOCATION (t, input_location);
380 /* When we expand a statement-tree, we must know whether or not the
381 statements are full-expressions. We record that fact here. */
382 STMT_IS_FULL_EXPR_P (t) = stmts_are_full_exprs_p ();
385 if (code == LABEL_EXPR || code == CASE_LABEL_EXPR)
386 STATEMENT_LIST_HAS_LABEL (cur_stmt_list) = 1;
388 /* Add T to the statement-tree. Non-side-effect statements need to be
389 recorded during statement expressions. */
390 gcc_checking_assert (!stmt_list_stack->is_empty ());
391 append_to_statement_list_force (t, &cur_stmt_list);
393 return t;
396 /* Returns the stmt_tree to which statements are currently being added. */
398 stmt_tree
399 current_stmt_tree (void)
401 return (cfun
402 ? &cfun->language->base.x_stmt_tree
403 : &scope_chain->x_stmt_tree);
406 /* If statements are full expressions, wrap STMT in a CLEANUP_POINT_EXPR. */
408 static tree
409 maybe_cleanup_point_expr (tree expr)
411 if (!processing_template_decl && stmts_are_full_exprs_p ())
412 expr = fold_build_cleanup_point_expr (TREE_TYPE (expr), expr);
413 return expr;
416 /* Like maybe_cleanup_point_expr except have the type of the new expression be
417 void so we don't need to create a temporary variable to hold the inner
418 expression. The reason why we do this is because the original type might be
419 an aggregate and we cannot create a temporary variable for that type. */
421 tree
422 maybe_cleanup_point_expr_void (tree expr)
424 if (!processing_template_decl && stmts_are_full_exprs_p ())
425 expr = fold_build_cleanup_point_expr (void_type_node, expr);
426 return expr;
431 /* Create a declaration statement for the declaration given by the DECL. */
433 void
434 add_decl_expr (tree decl)
436 tree r = build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl);
437 if (DECL_INITIAL (decl)
438 || (DECL_SIZE (decl) && TREE_SIDE_EFFECTS (DECL_SIZE (decl))))
439 r = maybe_cleanup_point_expr_void (r);
440 add_stmt (r);
443 /* Finish a scope. */
445 tree
446 do_poplevel (tree stmt_list)
448 tree block = NULL;
450 if (stmts_are_full_exprs_p ())
451 block = poplevel (kept_level_p (), 1, 0);
453 stmt_list = pop_stmt_list (stmt_list);
455 if (!processing_template_decl)
457 stmt_list = c_build_bind_expr (input_location, block, stmt_list);
458 /* ??? See c_end_compound_stmt re statement expressions. */
461 return stmt_list;
464 /* Begin a new scope. */
466 static tree
467 do_pushlevel (scope_kind sk)
469 tree ret = push_stmt_list ();
470 if (stmts_are_full_exprs_p ())
471 begin_scope (sk, NULL);
472 return ret;
475 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
476 when the current scope is exited. EH_ONLY is true when this is not
477 meant to apply to normal control flow transfer. */
479 void
480 push_cleanup (tree decl, tree cleanup, bool eh_only)
482 tree stmt = build_stmt (input_location, CLEANUP_STMT, NULL, cleanup, decl);
483 CLEANUP_EH_ONLY (stmt) = eh_only;
484 add_stmt (stmt);
485 CLEANUP_BODY (stmt) = push_stmt_list ();
488 /* Simple infinite loop tracking for -Wreturn-type. We keep a stack of all
489 the current loops, represented by 'NULL_TREE' if we've seen a possible
490 exit, and 'error_mark_node' if not. This is currently used only to
491 suppress the warning about a function with no return statements, and
492 therefore we don't bother noting returns as possible exits. We also
493 don't bother with gotos. */
495 static void
496 begin_maybe_infinite_loop (tree cond)
498 /* Only track this while parsing a function, not during instantiation. */
499 if (!cfun || (DECL_TEMPLATE_INSTANTIATION (current_function_decl)
500 && !processing_template_decl))
501 return;
502 bool maybe_infinite = true;
503 if (cond)
505 cond = fold_non_dependent_expr (cond);
506 maybe_infinite = integer_nonzerop (cond);
508 vec_safe_push (cp_function_chain->infinite_loops,
509 maybe_infinite ? error_mark_node : NULL_TREE);
513 /* A break is a possible exit for the current loop. */
515 void
516 break_maybe_infinite_loop (void)
518 if (!cfun)
519 return;
520 cp_function_chain->infinite_loops->last() = NULL_TREE;
523 /* If we reach the end of the loop without seeing a possible exit, we have
524 an infinite loop. */
526 static void
527 end_maybe_infinite_loop (tree cond)
529 if (!cfun || (DECL_TEMPLATE_INSTANTIATION (current_function_decl)
530 && !processing_template_decl))
531 return;
532 tree current = cp_function_chain->infinite_loops->pop();
533 if (current != NULL_TREE)
535 cond = fold_non_dependent_expr (cond);
536 if (integer_nonzerop (cond))
537 current_function_infinite_loop = 1;
542 /* Begin a conditional that might contain a declaration. When generating
543 normal code, we want the declaration to appear before the statement
544 containing the conditional. When generating template code, we want the
545 conditional to be rendered as the raw DECL_EXPR. */
547 static void
548 begin_cond (tree *cond_p)
550 if (processing_template_decl)
551 *cond_p = push_stmt_list ();
554 /* Finish such a conditional. */
556 static void
557 finish_cond (tree *cond_p, tree expr)
559 if (processing_template_decl)
561 tree cond = pop_stmt_list (*cond_p);
563 if (expr == NULL_TREE)
564 /* Empty condition in 'for'. */
565 gcc_assert (empty_expr_stmt_p (cond));
566 else if (check_for_bare_parameter_packs (expr))
567 expr = error_mark_node;
568 else if (!empty_expr_stmt_p (cond))
569 expr = build2 (COMPOUND_EXPR, TREE_TYPE (expr), cond, expr);
571 *cond_p = expr;
574 /* If *COND_P specifies a conditional with a declaration, transform the
575 loop such that
576 while (A x = 42) { }
577 for (; A x = 42;) { }
578 becomes
579 while (true) { A x = 42; if (!x) break; }
580 for (;;) { A x = 42; if (!x) break; }
581 The statement list for BODY will be empty if the conditional did
582 not declare anything. */
584 static void
585 simplify_loop_decl_cond (tree *cond_p, tree body)
587 tree cond, if_stmt;
589 if (!TREE_SIDE_EFFECTS (body))
590 return;
592 cond = *cond_p;
593 *cond_p = boolean_true_node;
595 if_stmt = begin_if_stmt ();
596 cond = cp_build_unary_op (TRUTH_NOT_EXPR, cond, false, tf_warning_or_error);
597 finish_if_stmt_cond (cond, if_stmt);
598 finish_break_stmt ();
599 finish_then_clause (if_stmt);
600 finish_if_stmt (if_stmt);
603 /* Finish a goto-statement. */
605 tree
606 finish_goto_stmt (tree destination)
608 if (identifier_p (destination))
609 destination = lookup_label (destination);
611 /* We warn about unused labels with -Wunused. That means we have to
612 mark the used labels as used. */
613 if (TREE_CODE (destination) == LABEL_DECL)
614 TREE_USED (destination) = 1;
615 else
617 destination = mark_rvalue_use (destination);
618 if (!processing_template_decl)
620 destination = cp_convert (ptr_type_node, destination,
621 tf_warning_or_error);
622 if (error_operand_p (destination))
623 return NULL_TREE;
624 destination
625 = fold_build_cleanup_point_expr (TREE_TYPE (destination),
626 destination);
630 check_goto (destination);
632 add_stmt (build_predict_expr (PRED_GOTO, NOT_TAKEN));
633 return add_stmt (build_stmt (input_location, GOTO_EXPR, destination));
636 /* COND is the condition-expression for an if, while, etc.,
637 statement. Convert it to a boolean value, if appropriate.
638 In addition, verify sequence points if -Wsequence-point is enabled. */
640 static tree
641 maybe_convert_cond (tree cond)
643 /* Empty conditions remain empty. */
644 if (!cond)
645 return NULL_TREE;
647 /* Wait until we instantiate templates before doing conversion. */
648 if (processing_template_decl)
649 return cond;
651 if (warn_sequence_point)
652 verify_sequence_points (cond);
654 /* Do the conversion. */
655 cond = convert_from_reference (cond);
657 if (TREE_CODE (cond) == MODIFY_EXPR
658 && !TREE_NO_WARNING (cond)
659 && warn_parentheses)
661 warning_at (EXPR_LOC_OR_LOC (cond, input_location), OPT_Wparentheses,
662 "suggest parentheses around assignment used as truth value");
663 TREE_NO_WARNING (cond) = 1;
666 return condition_conversion (cond);
669 /* Finish an expression-statement, whose EXPRESSION is as indicated. */
671 tree
672 finish_expr_stmt (tree expr)
674 tree r = NULL_TREE;
675 location_t loc = EXPR_LOCATION (expr);
677 if (expr != NULL_TREE)
679 /* If we ran into a problem, make sure we complained. */
680 gcc_assert (expr != error_mark_node || seen_error ());
682 if (!processing_template_decl)
684 if (warn_sequence_point)
685 verify_sequence_points (expr);
686 expr = convert_to_void (expr, ICV_STATEMENT, tf_warning_or_error);
688 else if (!type_dependent_expression_p (expr))
689 convert_to_void (build_non_dependent_expr (expr), ICV_STATEMENT,
690 tf_warning_or_error);
692 if (check_for_bare_parameter_packs (expr))
693 expr = error_mark_node;
695 /* Simplification of inner statement expressions, compound exprs,
696 etc can result in us already having an EXPR_STMT. */
697 if (TREE_CODE (expr) != CLEANUP_POINT_EXPR)
699 if (TREE_CODE (expr) != EXPR_STMT)
700 expr = build_stmt (loc, EXPR_STMT, expr);
701 expr = maybe_cleanup_point_expr_void (expr);
704 r = add_stmt (expr);
707 return r;
711 /* Begin an if-statement. Returns a newly created IF_STMT if
712 appropriate. */
714 tree
715 begin_if_stmt (void)
717 tree r, scope;
718 scope = do_pushlevel (sk_cond);
719 r = build_stmt (input_location, IF_STMT, NULL_TREE,
720 NULL_TREE, NULL_TREE, scope);
721 current_binding_level->this_entity = r;
722 begin_cond (&IF_COND (r));
723 return r;
726 /* Process the COND of an if-statement, which may be given by
727 IF_STMT. */
729 tree
730 finish_if_stmt_cond (tree cond, tree if_stmt)
732 cond = maybe_convert_cond (cond);
733 if (IF_STMT_CONSTEXPR_P (if_stmt)
734 && 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,
802 unsigned short unroll)
804 cond = maybe_convert_cond (cond);
805 finish_cond (&WHILE_COND (while_stmt), cond);
806 begin_maybe_infinite_loop (cond);
807 if (ivdep && cond != error_mark_node)
808 WHILE_COND (while_stmt) = build3 (ANNOTATE_EXPR,
809 TREE_TYPE (WHILE_COND (while_stmt)),
810 WHILE_COND (while_stmt),
811 build_int_cst (integer_type_node,
812 annot_expr_ivdep_kind),
813 integer_zero_node);
814 if (unroll && cond != error_mark_node)
815 WHILE_COND (while_stmt) = build3 (ANNOTATE_EXPR,
816 TREE_TYPE (WHILE_COND (while_stmt)),
817 WHILE_COND (while_stmt),
818 build_int_cst (integer_type_node,
819 annot_expr_unroll_kind),
820 build_int_cst (integer_type_node,
821 unroll));
822 simplify_loop_decl_cond (&WHILE_COND (while_stmt), WHILE_BODY (while_stmt));
825 /* Finish a while-statement, which may be given by WHILE_STMT. */
827 void
828 finish_while_stmt (tree while_stmt)
830 end_maybe_infinite_loop (boolean_true_node);
831 WHILE_BODY (while_stmt) = do_poplevel (WHILE_BODY (while_stmt));
834 /* Begin a do-statement. Returns a newly created DO_STMT if
835 appropriate. */
837 tree
838 begin_do_stmt (void)
840 tree r = build_stmt (input_location, DO_STMT, NULL_TREE, NULL_TREE);
841 begin_maybe_infinite_loop (boolean_true_node);
842 add_stmt (r);
843 DO_BODY (r) = push_stmt_list ();
844 return r;
847 /* Finish the body of a do-statement, which may be given by DO_STMT. */
849 void
850 finish_do_body (tree do_stmt)
852 tree body = DO_BODY (do_stmt) = pop_stmt_list (DO_BODY (do_stmt));
854 if (TREE_CODE (body) == STATEMENT_LIST && STATEMENT_LIST_TAIL (body))
855 body = STATEMENT_LIST_TAIL (body)->stmt;
857 if (IS_EMPTY_STMT (body))
858 warning (OPT_Wempty_body,
859 "suggest explicit braces around empty body in %<do%> statement");
862 /* Finish a do-statement, which may be given by DO_STMT, and whose
863 COND is as indicated. */
865 void
866 finish_do_stmt (tree cond, tree do_stmt, bool ivdep, unsigned short unroll)
868 cond = maybe_convert_cond (cond);
869 end_maybe_infinite_loop (cond);
870 if (ivdep && cond != error_mark_node)
871 cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
872 build_int_cst (integer_type_node, annot_expr_ivdep_kind),
873 integer_zero_node);
874 if (unroll && cond != error_mark_node)
875 cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
876 build_int_cst (integer_type_node, annot_expr_unroll_kind),
877 build_int_cst (integer_type_node, unroll));
878 DO_COND (do_stmt) = cond;
881 /* Finish a return-statement. The EXPRESSION returned, if any, is as
882 indicated. */
884 tree
885 finish_return_stmt (tree expr)
887 tree r;
888 bool no_warning;
890 expr = check_return_expr (expr, &no_warning);
892 if (error_operand_p (expr)
893 || (flag_openmp && !check_omp_return ()))
895 /* Suppress -Wreturn-type for this function. */
896 if (warn_return_type)
897 TREE_NO_WARNING (current_function_decl) = true;
898 return error_mark_node;
901 if (!processing_template_decl)
903 if (warn_sequence_point)
904 verify_sequence_points (expr);
906 if (DECL_DESTRUCTOR_P (current_function_decl)
907 || (DECL_CONSTRUCTOR_P (current_function_decl)
908 && targetm.cxx.cdtor_returns_this ()))
910 /* Similarly, all destructors must run destructors for
911 base-classes before returning. So, all returns in a
912 destructor get sent to the DTOR_LABEL; finish_function emits
913 code to return a value there. */
914 return finish_goto_stmt (cdtor_label);
918 r = build_stmt (input_location, RETURN_EXPR, expr);
919 TREE_NO_WARNING (r) |= no_warning;
920 r = maybe_cleanup_point_expr_void (r);
921 r = add_stmt (r);
923 return r;
926 /* Begin the scope of a for-statement or a range-for-statement.
927 Both the returned trees are to be used in a call to
928 begin_for_stmt or begin_range_for_stmt. */
930 tree
931 begin_for_scope (tree *init)
933 tree scope = NULL_TREE;
934 if (flag_new_for_scope)
935 scope = do_pushlevel (sk_for);
937 if (processing_template_decl)
938 *init = push_stmt_list ();
939 else
940 *init = NULL_TREE;
942 return scope;
945 /* Begin a for-statement. Returns a new FOR_STMT.
946 SCOPE and INIT should be the return of begin_for_scope,
947 or both NULL_TREE */
949 tree
950 begin_for_stmt (tree scope, tree init)
952 tree r;
954 r = build_stmt (input_location, FOR_STMT, NULL_TREE, NULL_TREE,
955 NULL_TREE, NULL_TREE, NULL_TREE);
957 if (scope == NULL_TREE)
959 gcc_assert (!init || !flag_new_for_scope);
960 if (!init)
961 scope = begin_for_scope (&init);
963 FOR_INIT_STMT (r) = init;
964 FOR_SCOPE (r) = scope;
966 return r;
969 /* Finish the init-statement of a for-statement, which may be
970 given by FOR_STMT. */
972 void
973 finish_init_stmt (tree for_stmt)
975 if (processing_template_decl)
976 FOR_INIT_STMT (for_stmt) = pop_stmt_list (FOR_INIT_STMT (for_stmt));
977 add_stmt (for_stmt);
978 FOR_BODY (for_stmt) = do_pushlevel (sk_block);
979 begin_cond (&FOR_COND (for_stmt));
982 /* Finish the COND of a for-statement, which may be given by
983 FOR_STMT. */
985 void
986 finish_for_cond (tree cond, tree for_stmt, bool ivdep, unsigned short unroll)
988 cond = maybe_convert_cond (cond);
989 finish_cond (&FOR_COND (for_stmt), cond);
990 begin_maybe_infinite_loop (cond);
991 if (ivdep && cond != error_mark_node)
992 FOR_COND (for_stmt) = build3 (ANNOTATE_EXPR,
993 TREE_TYPE (FOR_COND (for_stmt)),
994 FOR_COND (for_stmt),
995 build_int_cst (integer_type_node,
996 annot_expr_ivdep_kind),
997 integer_zero_node);
998 if (unroll && cond != error_mark_node)
999 FOR_COND (for_stmt) = build3 (ANNOTATE_EXPR,
1000 TREE_TYPE (FOR_COND (for_stmt)),
1001 FOR_COND (for_stmt),
1002 build_int_cst (integer_type_node,
1003 annot_expr_unroll_kind),
1004 build_int_cst (integer_type_node,
1005 unroll));
1006 simplify_loop_decl_cond (&FOR_COND (for_stmt), FOR_BODY (for_stmt));
1009 /* Finish the increment-EXPRESSION in a for-statement, which may be
1010 given by FOR_STMT. */
1012 void
1013 finish_for_expr (tree expr, tree for_stmt)
1015 if (!expr)
1016 return;
1017 /* If EXPR is an overloaded function, issue an error; there is no
1018 context available to use to perform overload resolution. */
1019 if (type_unknown_p (expr))
1021 cxx_incomplete_type_error (expr, TREE_TYPE (expr));
1022 expr = error_mark_node;
1024 if (!processing_template_decl)
1026 if (warn_sequence_point)
1027 verify_sequence_points (expr);
1028 expr = convert_to_void (expr, ICV_THIRD_IN_FOR,
1029 tf_warning_or_error);
1031 else if (!type_dependent_expression_p (expr))
1032 convert_to_void (build_non_dependent_expr (expr), ICV_THIRD_IN_FOR,
1033 tf_warning_or_error);
1034 expr = maybe_cleanup_point_expr_void (expr);
1035 if (check_for_bare_parameter_packs (expr))
1036 expr = error_mark_node;
1037 FOR_EXPR (for_stmt) = expr;
1040 /* Finish the body of a for-statement, which may be given by
1041 FOR_STMT. The increment-EXPR for the loop must be
1042 provided.
1043 It can also finish RANGE_FOR_STMT. */
1045 void
1046 finish_for_stmt (tree for_stmt)
1048 end_maybe_infinite_loop (boolean_true_node);
1050 if (TREE_CODE (for_stmt) == RANGE_FOR_STMT)
1051 RANGE_FOR_BODY (for_stmt) = do_poplevel (RANGE_FOR_BODY (for_stmt));
1052 else
1053 FOR_BODY (for_stmt) = do_poplevel (FOR_BODY (for_stmt));
1055 /* Pop the scope for the body of the loop. */
1056 if (flag_new_for_scope)
1058 tree scope;
1059 tree *scope_ptr = (TREE_CODE (for_stmt) == RANGE_FOR_STMT
1060 ? &RANGE_FOR_SCOPE (for_stmt)
1061 : &FOR_SCOPE (for_stmt));
1062 scope = *scope_ptr;
1063 *scope_ptr = NULL;
1064 add_stmt (do_poplevel (scope));
1068 /* Begin a range-for-statement. Returns a new RANGE_FOR_STMT.
1069 SCOPE and INIT should be the return of begin_for_scope,
1070 or both NULL_TREE .
1071 To finish it call finish_for_stmt(). */
1073 tree
1074 begin_range_for_stmt (tree scope, tree init)
1076 tree r;
1078 begin_maybe_infinite_loop (boolean_false_node);
1080 r = build_stmt (input_location, RANGE_FOR_STMT,
1081 NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE);
1083 if (scope == NULL_TREE)
1085 gcc_assert (!init || !flag_new_for_scope);
1086 if (!init)
1087 scope = begin_for_scope (&init);
1090 /* RANGE_FOR_STMTs do not use nor save the init tree, so we
1091 pop it now. */
1092 if (init)
1093 pop_stmt_list (init);
1094 RANGE_FOR_SCOPE (r) = scope;
1096 return r;
1099 /* Finish the head of a range-based for statement, which may
1100 be given by RANGE_FOR_STMT. DECL must be the declaration
1101 and EXPR must be the loop expression. */
1103 void
1104 finish_range_for_decl (tree range_for_stmt, tree decl, tree expr)
1106 RANGE_FOR_DECL (range_for_stmt) = decl;
1107 RANGE_FOR_EXPR (range_for_stmt) = expr;
1108 add_stmt (range_for_stmt);
1109 RANGE_FOR_BODY (range_for_stmt) = do_pushlevel (sk_block);
1112 /* Finish a break-statement. */
1114 tree
1115 finish_break_stmt (void)
1117 /* In switch statements break is sometimes stylistically used after
1118 a return statement. This can lead to spurious warnings about
1119 control reaching the end of a non-void function when it is
1120 inlined. Note that we are calling block_may_fallthru with
1121 language specific tree nodes; this works because
1122 block_may_fallthru returns true when given something it does not
1123 understand. */
1124 if (!block_may_fallthru (cur_stmt_list))
1125 return void_node;
1126 note_break_stmt ();
1127 return add_stmt (build_stmt (input_location, BREAK_STMT));
1130 /* Finish a continue-statement. */
1132 tree
1133 finish_continue_stmt (void)
1135 return add_stmt (build_stmt (input_location, CONTINUE_STMT));
1138 /* Begin a switch-statement. Returns a new SWITCH_STMT if
1139 appropriate. */
1141 tree
1142 begin_switch_stmt (void)
1144 tree r, scope;
1146 scope = do_pushlevel (sk_cond);
1147 r = build_stmt (input_location, SWITCH_STMT, NULL_TREE, NULL_TREE, NULL_TREE, scope);
1149 begin_cond (&SWITCH_STMT_COND (r));
1151 return r;
1154 /* Finish the cond of a switch-statement. */
1156 void
1157 finish_switch_cond (tree cond, tree switch_stmt)
1159 tree orig_type = NULL;
1161 if (!processing_template_decl)
1163 /* Convert the condition to an integer or enumeration type. */
1164 cond = build_expr_type_conversion (WANT_INT | WANT_ENUM, cond, true);
1165 if (cond == NULL_TREE)
1167 error ("switch quantity not an integer");
1168 cond = error_mark_node;
1170 /* We want unlowered type here to handle enum bit-fields. */
1171 orig_type = unlowered_expr_type (cond);
1172 if (TREE_CODE (orig_type) != ENUMERAL_TYPE)
1173 orig_type = TREE_TYPE (cond);
1174 if (cond != error_mark_node)
1176 /* [stmt.switch]
1178 Integral promotions are performed. */
1179 cond = perform_integral_promotions (cond);
1180 cond = maybe_cleanup_point_expr (cond);
1183 if (check_for_bare_parameter_packs (cond))
1184 cond = error_mark_node;
1185 else if (!processing_template_decl && warn_sequence_point)
1186 verify_sequence_points (cond);
1188 finish_cond (&SWITCH_STMT_COND (switch_stmt), cond);
1189 SWITCH_STMT_TYPE (switch_stmt) = orig_type;
1190 add_stmt (switch_stmt);
1191 push_switch (switch_stmt);
1192 SWITCH_STMT_BODY (switch_stmt) = push_stmt_list ();
1195 /* Finish the body of a switch-statement, which may be given by
1196 SWITCH_STMT. The COND to switch on is indicated. */
1198 void
1199 finish_switch_stmt (tree switch_stmt)
1201 tree scope;
1203 SWITCH_STMT_BODY (switch_stmt) =
1204 pop_stmt_list (SWITCH_STMT_BODY (switch_stmt));
1205 pop_switch ();
1207 scope = SWITCH_STMT_SCOPE (switch_stmt);
1208 SWITCH_STMT_SCOPE (switch_stmt) = NULL;
1209 add_stmt (do_poplevel (scope));
1212 /* Begin a try-block. Returns a newly-created TRY_BLOCK if
1213 appropriate. */
1215 tree
1216 begin_try_block (void)
1218 tree r = build_stmt (input_location, TRY_BLOCK, NULL_TREE, NULL_TREE);
1219 add_stmt (r);
1220 TRY_STMTS (r) = push_stmt_list ();
1221 return r;
1224 /* Likewise, for a function-try-block. The block returned in
1225 *COMPOUND_STMT is an artificial outer scope, containing the
1226 function-try-block. */
1228 tree
1229 begin_function_try_block (tree *compound_stmt)
1231 tree r;
1232 /* This outer scope does not exist in the C++ standard, but we need
1233 a place to put __FUNCTION__ and similar variables. */
1234 *compound_stmt = begin_compound_stmt (0);
1235 r = begin_try_block ();
1236 FN_TRY_BLOCK_P (r) = 1;
1237 return r;
1240 /* Finish a try-block, which may be given by TRY_BLOCK. */
1242 void
1243 finish_try_block (tree try_block)
1245 TRY_STMTS (try_block) = pop_stmt_list (TRY_STMTS (try_block));
1246 TRY_HANDLERS (try_block) = push_stmt_list ();
1249 /* Finish the body of a cleanup try-block, which may be given by
1250 TRY_BLOCK. */
1252 void
1253 finish_cleanup_try_block (tree try_block)
1255 TRY_STMTS (try_block) = pop_stmt_list (TRY_STMTS (try_block));
1258 /* Finish an implicitly generated try-block, with a cleanup is given
1259 by CLEANUP. */
1261 void
1262 finish_cleanup (tree cleanup, tree try_block)
1264 TRY_HANDLERS (try_block) = cleanup;
1265 CLEANUP_P (try_block) = 1;
1268 /* Likewise, for a function-try-block. */
1270 void
1271 finish_function_try_block (tree try_block)
1273 finish_try_block (try_block);
1274 /* FIXME : something queer about CTOR_INITIALIZER somehow following
1275 the try block, but moving it inside. */
1276 in_function_try_handler = 1;
1279 /* Finish a handler-sequence for a try-block, which may be given by
1280 TRY_BLOCK. */
1282 void
1283 finish_handler_sequence (tree try_block)
1285 TRY_HANDLERS (try_block) = pop_stmt_list (TRY_HANDLERS (try_block));
1286 check_handlers (TRY_HANDLERS (try_block));
1289 /* Finish the handler-seq for a function-try-block, given by
1290 TRY_BLOCK. COMPOUND_STMT is the outer block created by
1291 begin_function_try_block. */
1293 void
1294 finish_function_handler_sequence (tree try_block, tree compound_stmt)
1296 in_function_try_handler = 0;
1297 finish_handler_sequence (try_block);
1298 finish_compound_stmt (compound_stmt);
1301 /* Begin a handler. Returns a HANDLER if appropriate. */
1303 tree
1304 begin_handler (void)
1306 tree r;
1308 r = build_stmt (input_location, HANDLER, NULL_TREE, NULL_TREE);
1309 add_stmt (r);
1311 /* Create a binding level for the eh_info and the exception object
1312 cleanup. */
1313 HANDLER_BODY (r) = do_pushlevel (sk_catch);
1315 return r;
1318 /* Finish the handler-parameters for a handler, which may be given by
1319 HANDLER. DECL is the declaration for the catch parameter, or NULL
1320 if this is a `catch (...)' clause. */
1322 void
1323 finish_handler_parms (tree decl, tree handler)
1325 tree type = NULL_TREE;
1326 if (processing_template_decl)
1328 if (decl)
1330 decl = pushdecl (decl);
1331 decl = push_template_decl (decl);
1332 HANDLER_PARMS (handler) = decl;
1333 type = TREE_TYPE (decl);
1336 else
1338 type = expand_start_catch_block (decl);
1339 if (warn_catch_value
1340 && type != NULL_TREE
1341 && type != error_mark_node
1342 && TREE_CODE (TREE_TYPE (decl)) != REFERENCE_TYPE)
1344 tree orig_type = TREE_TYPE (decl);
1345 if (CLASS_TYPE_P (orig_type))
1347 if (TYPE_POLYMORPHIC_P (orig_type))
1348 warning (OPT_Wcatch_value_,
1349 "catching polymorphic type %q#T by value", orig_type);
1350 else if (warn_catch_value > 1)
1351 warning (OPT_Wcatch_value_,
1352 "catching type %q#T by value", orig_type);
1354 else if (warn_catch_value > 2)
1355 warning (OPT_Wcatch_value_,
1356 "catching non-reference type %q#T", orig_type);
1359 HANDLER_TYPE (handler) = type;
1362 /* Finish a handler, which may be given by HANDLER. The BLOCKs are
1363 the return value from the matching call to finish_handler_parms. */
1365 void
1366 finish_handler (tree handler)
1368 if (!processing_template_decl)
1369 expand_end_catch_block ();
1370 HANDLER_BODY (handler) = do_poplevel (HANDLER_BODY (handler));
1373 /* Begin a compound statement. FLAGS contains some bits that control the
1374 behavior and context. If BCS_NO_SCOPE is set, the compound statement
1375 does not define a scope. If BCS_FN_BODY is set, this is the outermost
1376 block of a function. If BCS_TRY_BLOCK is set, this is the block
1377 created on behalf of a TRY statement. Returns a token to be passed to
1378 finish_compound_stmt. */
1380 tree
1381 begin_compound_stmt (unsigned int flags)
1383 tree r;
1385 if (flags & BCS_NO_SCOPE)
1387 r = push_stmt_list ();
1388 STATEMENT_LIST_NO_SCOPE (r) = 1;
1390 /* Normally, we try hard to keep the BLOCK for a statement-expression.
1391 But, if it's a statement-expression with a scopeless block, there's
1392 nothing to keep, and we don't want to accidentally keep a block
1393 *inside* the scopeless block. */
1394 keep_next_level (false);
1396 else
1398 scope_kind sk = sk_block;
1399 if (flags & BCS_TRY_BLOCK)
1400 sk = sk_try;
1401 else if (flags & BCS_TRANSACTION)
1402 sk = sk_transaction;
1403 r = do_pushlevel (sk);
1406 /* When processing a template, we need to remember where the braces were,
1407 so that we can set up identical scopes when instantiating the template
1408 later. BIND_EXPR is a handy candidate for this.
1409 Note that do_poplevel won't create a BIND_EXPR itself here (and thus
1410 result in nested BIND_EXPRs), since we don't build BLOCK nodes when
1411 processing templates. */
1412 if (processing_template_decl)
1414 r = build3 (BIND_EXPR, NULL, NULL, r, NULL);
1415 BIND_EXPR_TRY_BLOCK (r) = (flags & BCS_TRY_BLOCK) != 0;
1416 BIND_EXPR_BODY_BLOCK (r) = (flags & BCS_FN_BODY) != 0;
1417 TREE_SIDE_EFFECTS (r) = 1;
1420 return r;
1423 /* Finish a compound-statement, which is given by STMT. */
1425 void
1426 finish_compound_stmt (tree stmt)
1428 if (TREE_CODE (stmt) == BIND_EXPR)
1430 tree body = do_poplevel (BIND_EXPR_BODY (stmt));
1431 /* If the STATEMENT_LIST is empty and this BIND_EXPR isn't special,
1432 discard the BIND_EXPR so it can be merged with the containing
1433 STATEMENT_LIST. */
1434 if (TREE_CODE (body) == STATEMENT_LIST
1435 && STATEMENT_LIST_HEAD (body) == NULL
1436 && !BIND_EXPR_BODY_BLOCK (stmt)
1437 && !BIND_EXPR_TRY_BLOCK (stmt))
1438 stmt = body;
1439 else
1440 BIND_EXPR_BODY (stmt) = body;
1442 else if (STATEMENT_LIST_NO_SCOPE (stmt))
1443 stmt = pop_stmt_list (stmt);
1444 else
1446 /* Destroy any ObjC "super" receivers that may have been
1447 created. */
1448 objc_clear_super_receiver ();
1450 stmt = do_poplevel (stmt);
1453 /* ??? See c_end_compound_stmt wrt statement expressions. */
1454 add_stmt (stmt);
1457 /* Finish an asm-statement, whose components are a STRING, some
1458 OUTPUT_OPERANDS, some INPUT_OPERANDS, some CLOBBERS and some
1459 LABELS. Also note whether the asm-statement should be
1460 considered volatile. */
1462 tree
1463 finish_asm_stmt (int volatile_p, tree string, tree output_operands,
1464 tree input_operands, tree clobbers, tree labels)
1466 tree r;
1467 tree t;
1468 int ninputs = list_length (input_operands);
1469 int noutputs = list_length (output_operands);
1471 if (!processing_template_decl)
1473 const char *constraint;
1474 const char **oconstraints;
1475 bool allows_mem, allows_reg, is_inout;
1476 tree operand;
1477 int i;
1479 oconstraints = XALLOCAVEC (const char *, noutputs);
1481 string = resolve_asm_operand_names (string, output_operands,
1482 input_operands, labels);
1484 for (i = 0, t = output_operands; t; t = TREE_CHAIN (t), ++i)
1486 operand = TREE_VALUE (t);
1488 /* ??? Really, this should not be here. Users should be using a
1489 proper lvalue, dammit. But there's a long history of using
1490 casts in the output operands. In cases like longlong.h, this
1491 becomes a primitive form of typechecking -- if the cast can be
1492 removed, then the output operand had a type of the proper width;
1493 otherwise we'll get an error. Gross, but ... */
1494 STRIP_NOPS (operand);
1496 operand = mark_lvalue_use (operand);
1498 if (!lvalue_or_else (operand, lv_asm, tf_warning_or_error))
1499 operand = error_mark_node;
1501 if (operand != error_mark_node
1502 && (TREE_READONLY (operand)
1503 || CP_TYPE_CONST_P (TREE_TYPE (operand))
1504 /* Functions are not modifiable, even though they are
1505 lvalues. */
1506 || TREE_CODE (TREE_TYPE (operand)) == FUNCTION_TYPE
1507 || TREE_CODE (TREE_TYPE (operand)) == METHOD_TYPE
1508 /* If it's an aggregate and any field is const, then it is
1509 effectively const. */
1510 || (CLASS_TYPE_P (TREE_TYPE (operand))
1511 && C_TYPE_FIELDS_READONLY (TREE_TYPE (operand)))))
1512 cxx_readonly_error (operand, lv_asm);
1514 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
1515 oconstraints[i] = constraint;
1517 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
1518 &allows_mem, &allows_reg, &is_inout))
1520 /* If the operand is going to end up in memory,
1521 mark it addressable. */
1522 if (!allows_reg && !cxx_mark_addressable (operand))
1523 operand = error_mark_node;
1525 else
1526 operand = error_mark_node;
1528 TREE_VALUE (t) = operand;
1531 for (i = 0, t = input_operands; t; ++i, t = TREE_CHAIN (t))
1533 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
1534 bool constraint_parsed
1535 = parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
1536 oconstraints, &allows_mem, &allows_reg);
1537 /* If the operand is going to end up in memory, don't call
1538 decay_conversion. */
1539 if (constraint_parsed && !allows_reg && allows_mem)
1540 operand = mark_lvalue_use (TREE_VALUE (t));
1541 else
1542 operand = decay_conversion (TREE_VALUE (t), tf_warning_or_error);
1544 /* If the type of the operand hasn't been determined (e.g.,
1545 because it involves an overloaded function), then issue
1546 an error message. There's no context available to
1547 resolve the overloading. */
1548 if (TREE_TYPE (operand) == unknown_type_node)
1550 error ("type of asm operand %qE could not be determined",
1551 TREE_VALUE (t));
1552 operand = error_mark_node;
1555 if (constraint_parsed)
1557 /* If the operand is going to end up in memory,
1558 mark it addressable. */
1559 if (!allows_reg && allows_mem)
1561 /* Strip the nops as we allow this case. FIXME, this really
1562 should be rejected or made deprecated. */
1563 STRIP_NOPS (operand);
1564 if (!cxx_mark_addressable (operand))
1565 operand = error_mark_node;
1567 else if (!allows_reg && !allows_mem)
1569 /* If constraint allows neither register nor memory,
1570 try harder to get a constant. */
1571 tree constop = maybe_constant_value (operand);
1572 if (TREE_CONSTANT (constop))
1573 operand = constop;
1576 else
1577 operand = error_mark_node;
1579 TREE_VALUE (t) = operand;
1583 r = build_stmt (input_location, ASM_EXPR, string,
1584 output_operands, input_operands,
1585 clobbers, labels);
1586 ASM_VOLATILE_P (r) = volatile_p || noutputs == 0;
1587 r = maybe_cleanup_point_expr_void (r);
1588 return add_stmt (r);
1591 /* Finish a label with the indicated NAME. Returns the new label. */
1593 tree
1594 finish_label_stmt (tree name)
1596 tree decl = define_label (input_location, name);
1598 if (decl == error_mark_node)
1599 return error_mark_node;
1601 add_stmt (build_stmt (input_location, LABEL_EXPR, decl));
1603 return decl;
1606 /* Finish a series of declarations for local labels. G++ allows users
1607 to declare "local" labels, i.e., labels with scope. This extension
1608 is useful when writing code involving statement-expressions. */
1610 void
1611 finish_label_decl (tree name)
1613 if (!at_function_scope_p ())
1615 error ("__label__ declarations are only allowed in function scopes");
1616 return;
1619 add_decl_expr (declare_local_label (name));
1622 /* When DECL goes out of scope, make sure that CLEANUP is executed. */
1624 void
1625 finish_decl_cleanup (tree decl, tree cleanup)
1627 push_cleanup (decl, cleanup, false);
1630 /* If the current scope exits with an exception, run CLEANUP. */
1632 void
1633 finish_eh_cleanup (tree cleanup)
1635 push_cleanup (NULL, cleanup, true);
1638 /* The MEM_INITS is a list of mem-initializers, in reverse of the
1639 order they were written by the user. Each node is as for
1640 emit_mem_initializers. */
1642 void
1643 finish_mem_initializers (tree mem_inits)
1645 /* Reorder the MEM_INITS so that they are in the order they appeared
1646 in the source program. */
1647 mem_inits = nreverse (mem_inits);
1649 if (processing_template_decl)
1651 tree mem;
1653 for (mem = mem_inits; mem; mem = TREE_CHAIN (mem))
1655 /* If the TREE_PURPOSE is a TYPE_PACK_EXPANSION, skip the
1656 check for bare parameter packs in the TREE_VALUE, because
1657 any parameter packs in the TREE_VALUE have already been
1658 bound as part of the TREE_PURPOSE. See
1659 make_pack_expansion for more information. */
1660 if (TREE_CODE (TREE_PURPOSE (mem)) != TYPE_PACK_EXPANSION
1661 && check_for_bare_parameter_packs (TREE_VALUE (mem)))
1662 TREE_VALUE (mem) = error_mark_node;
1665 add_stmt (build_min_nt_loc (UNKNOWN_LOCATION,
1666 CTOR_INITIALIZER, mem_inits));
1668 else
1669 emit_mem_initializers (mem_inits);
1672 /* Obfuscate EXPR if it looks like an id-expression or member access so
1673 that the call to finish_decltype in do_auto_deduction will give the
1674 right result. */
1676 tree
1677 force_paren_expr (tree expr)
1679 /* This is only needed for decltype(auto) in C++14. */
1680 if (cxx_dialect < cxx14)
1681 return expr;
1683 /* If we're in unevaluated context, we can't be deducing a
1684 return/initializer type, so we don't need to mess with this. */
1685 if (cp_unevaluated_operand)
1686 return expr;
1688 if (!DECL_P (expr) && TREE_CODE (expr) != COMPONENT_REF
1689 && TREE_CODE (expr) != SCOPE_REF)
1690 return expr;
1692 if (TREE_CODE (expr) == COMPONENT_REF
1693 || TREE_CODE (expr) == SCOPE_REF)
1694 REF_PARENTHESIZED_P (expr) = true;
1695 else if (type_dependent_expression_p (expr))
1696 expr = build1 (PAREN_EXPR, TREE_TYPE (expr), expr);
1697 else if (VAR_P (expr) && DECL_HARD_REGISTER (expr))
1698 /* We can't bind a hard register variable to a reference. */;
1699 else
1701 cp_lvalue_kind kind = lvalue_kind (expr);
1702 if ((kind & ~clk_class) != clk_none)
1704 tree type = unlowered_expr_type (expr);
1705 bool rval = !!(kind & clk_rvalueref);
1706 type = cp_build_reference_type (type, rval);
1707 /* This inhibits warnings in, eg, cxx_mark_addressable
1708 (c++/60955). */
1709 warning_sentinel s (extra_warnings);
1710 expr = build_static_cast (type, expr, tf_error);
1711 if (expr != error_mark_node)
1712 REF_PARENTHESIZED_P (expr) = true;
1716 return expr;
1719 /* If T is an id-expression obfuscated by force_paren_expr, undo the
1720 obfuscation and return the underlying id-expression. Otherwise
1721 return T. */
1723 tree
1724 maybe_undo_parenthesized_ref (tree t)
1726 if (cxx_dialect >= cxx14
1727 && INDIRECT_REF_P (t)
1728 && REF_PARENTHESIZED_P (t))
1730 t = TREE_OPERAND (t, 0);
1731 while (TREE_CODE (t) == NON_LVALUE_EXPR
1732 || TREE_CODE (t) == NOP_EXPR)
1733 t = TREE_OPERAND (t, 0);
1735 gcc_assert (TREE_CODE (t) == ADDR_EXPR
1736 || TREE_CODE (t) == STATIC_CAST_EXPR);
1737 t = TREE_OPERAND (t, 0);
1740 return t;
1743 /* Finish a parenthesized expression EXPR. */
1745 cp_expr
1746 finish_parenthesized_expr (cp_expr expr)
1748 if (EXPR_P (expr))
1749 /* This inhibits warnings in c_common_truthvalue_conversion. */
1750 TREE_NO_WARNING (expr) = 1;
1752 if (TREE_CODE (expr) == OFFSET_REF
1753 || TREE_CODE (expr) == SCOPE_REF)
1754 /* [expr.unary.op]/3 The qualified id of a pointer-to-member must not be
1755 enclosed in parentheses. */
1756 PTRMEM_OK_P (expr) = 0;
1758 if (TREE_CODE (expr) == STRING_CST)
1759 PAREN_STRING_LITERAL_P (expr) = 1;
1761 expr = cp_expr (force_paren_expr (expr), expr.get_location ());
1763 return expr;
1766 /* Finish a reference to a non-static data member (DECL) that is not
1767 preceded by `.' or `->'. */
1769 tree
1770 finish_non_static_data_member (tree decl, tree object, tree qualifying_scope)
1772 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
1773 bool try_omp_private = !object && omp_private_member_map;
1774 tree ret;
1776 if (!object)
1778 tree scope = qualifying_scope;
1779 if (scope == NULL_TREE)
1780 scope = context_for_name_lookup (decl);
1781 object = maybe_dummy_object (scope, NULL);
1784 object = maybe_resolve_dummy (object, true);
1785 if (object == error_mark_node)
1786 return error_mark_node;
1788 /* DR 613/850: Can use non-static data members without an associated
1789 object in sizeof/decltype/alignof. */
1790 if (is_dummy_object (object) && cp_unevaluated_operand == 0
1791 && (!processing_template_decl || !current_class_ref))
1793 if (current_function_decl
1794 && DECL_STATIC_FUNCTION_P (current_function_decl))
1795 error ("invalid use of member %qD in static member function", decl);
1796 else
1797 error ("invalid use of non-static data member %qD", decl);
1798 inform (DECL_SOURCE_LOCATION (decl), "declared here");
1800 return error_mark_node;
1803 if (current_class_ptr)
1804 TREE_USED (current_class_ptr) = 1;
1805 if (processing_template_decl && !qualifying_scope)
1807 tree type = TREE_TYPE (decl);
1809 if (TREE_CODE (type) == REFERENCE_TYPE)
1810 /* Quals on the object don't matter. */;
1811 else if (PACK_EXPANSION_P (type))
1812 /* Don't bother trying to represent this. */
1813 type = NULL_TREE;
1814 else
1816 /* Set the cv qualifiers. */
1817 int quals = cp_type_quals (TREE_TYPE (object));
1819 if (DECL_MUTABLE_P (decl))
1820 quals &= ~TYPE_QUAL_CONST;
1822 quals |= cp_type_quals (TREE_TYPE (decl));
1823 type = cp_build_qualified_type (type, quals);
1826 ret = (convert_from_reference
1827 (build_min (COMPONENT_REF, type, object, decl, NULL_TREE)));
1829 /* If PROCESSING_TEMPLATE_DECL is nonzero here, then
1830 QUALIFYING_SCOPE is also non-null. Wrap this in a SCOPE_REF
1831 for now. */
1832 else if (processing_template_decl)
1833 ret = build_qualified_name (TREE_TYPE (decl),
1834 qualifying_scope,
1835 decl,
1836 /*template_p=*/false);
1837 else
1839 tree access_type = TREE_TYPE (object);
1841 perform_or_defer_access_check (TYPE_BINFO (access_type), decl,
1842 decl, tf_warning_or_error);
1844 /* If the data member was named `C::M', convert `*this' to `C'
1845 first. */
1846 if (qualifying_scope)
1848 tree binfo = NULL_TREE;
1849 object = build_scoped_ref (object, qualifying_scope,
1850 &binfo);
1853 ret = build_class_member_access_expr (object, decl,
1854 /*access_path=*/NULL_TREE,
1855 /*preserve_reference=*/false,
1856 tf_warning_or_error);
1858 if (try_omp_private)
1860 tree *v = omp_private_member_map->get (decl);
1861 if (v)
1862 ret = convert_from_reference (*v);
1864 return ret;
1867 /* If we are currently parsing a template and we encountered a typedef
1868 TYPEDEF_DECL that is being accessed though CONTEXT, this function
1869 adds the typedef to a list tied to the current template.
1870 At template instantiation time, that list is walked and access check
1871 performed for each typedef.
1872 LOCATION is the location of the usage point of TYPEDEF_DECL. */
1874 void
1875 add_typedef_to_current_template_for_access_check (tree typedef_decl,
1876 tree context,
1877 location_t location)
1879 tree template_info = NULL;
1880 tree cs = current_scope ();
1882 if (!is_typedef_decl (typedef_decl)
1883 || !context
1884 || !CLASS_TYPE_P (context)
1885 || !cs)
1886 return;
1888 if (CLASS_TYPE_P (cs) || TREE_CODE (cs) == FUNCTION_DECL)
1889 template_info = get_template_info (cs);
1891 if (template_info
1892 && TI_TEMPLATE (template_info)
1893 && !currently_open_class (context))
1894 append_type_to_template_for_access_check (cs, typedef_decl,
1895 context, location);
1898 /* DECL was the declaration to which a qualified-id resolved. Issue
1899 an error message if it is not accessible. If OBJECT_TYPE is
1900 non-NULL, we have just seen `x->' or `x.' and OBJECT_TYPE is the
1901 type of `*x', or `x', respectively. If the DECL was named as
1902 `A::B' then NESTED_NAME_SPECIFIER is `A'. */
1904 void
1905 check_accessibility_of_qualified_id (tree decl,
1906 tree object_type,
1907 tree nested_name_specifier)
1909 tree scope;
1910 tree qualifying_type = NULL_TREE;
1912 /* If we are parsing a template declaration and if decl is a typedef,
1913 add it to a list tied to the template.
1914 At template instantiation time, that list will be walked and
1915 access check performed. */
1916 add_typedef_to_current_template_for_access_check (decl,
1917 nested_name_specifier
1918 ? nested_name_specifier
1919 : DECL_CONTEXT (decl),
1920 input_location);
1922 /* If we're not checking, return immediately. */
1923 if (deferred_access_no_check)
1924 return;
1926 /* Determine the SCOPE of DECL. */
1927 scope = context_for_name_lookup (decl);
1928 /* If the SCOPE is not a type, then DECL is not a member. */
1929 if (!TYPE_P (scope))
1930 return;
1931 /* Compute the scope through which DECL is being accessed. */
1932 if (object_type
1933 /* OBJECT_TYPE might not be a class type; consider:
1935 class A { typedef int I; };
1936 I *p;
1937 p->A::I::~I();
1939 In this case, we will have "A::I" as the DECL, but "I" as the
1940 OBJECT_TYPE. */
1941 && CLASS_TYPE_P (object_type)
1942 && DERIVED_FROM_P (scope, object_type))
1943 /* If we are processing a `->' or `.' expression, use the type of the
1944 left-hand side. */
1945 qualifying_type = object_type;
1946 else if (nested_name_specifier)
1948 /* If the reference is to a non-static member of the
1949 current class, treat it as if it were referenced through
1950 `this'. */
1951 tree ct;
1952 if (DECL_NONSTATIC_MEMBER_P (decl)
1953 && current_class_ptr
1954 && DERIVED_FROM_P (scope, ct = current_nonlambda_class_type ()))
1955 qualifying_type = ct;
1956 /* Otherwise, use the type indicated by the
1957 nested-name-specifier. */
1958 else
1959 qualifying_type = nested_name_specifier;
1961 else
1962 /* Otherwise, the name must be from the current class or one of
1963 its bases. */
1964 qualifying_type = currently_open_derived_class (scope);
1966 if (qualifying_type
1967 /* It is possible for qualifying type to be a TEMPLATE_TYPE_PARM
1968 or similar in a default argument value. */
1969 && CLASS_TYPE_P (qualifying_type)
1970 && !dependent_type_p (qualifying_type))
1971 perform_or_defer_access_check (TYPE_BINFO (qualifying_type), decl,
1972 decl, tf_warning_or_error);
1975 /* EXPR is the result of a qualified-id. The QUALIFYING_CLASS was the
1976 class named to the left of the "::" operator. DONE is true if this
1977 expression is a complete postfix-expression; it is false if this
1978 expression is followed by '->', '[', '(', etc. ADDRESS_P is true
1979 iff this expression is the operand of '&'. TEMPLATE_P is true iff
1980 the qualified-id was of the form "A::template B". TEMPLATE_ARG_P
1981 is true iff this qualified name appears as a template argument. */
1983 tree
1984 finish_qualified_id_expr (tree qualifying_class,
1985 tree expr,
1986 bool done,
1987 bool address_p,
1988 bool template_p,
1989 bool template_arg_p,
1990 tsubst_flags_t complain)
1992 gcc_assert (TYPE_P (qualifying_class));
1994 if (error_operand_p (expr))
1995 return error_mark_node;
1997 if ((DECL_P (expr) || BASELINK_P (expr))
1998 && !mark_used (expr, complain))
1999 return error_mark_node;
2001 if (template_p)
2003 if (TREE_CODE (expr) == UNBOUND_CLASS_TEMPLATE)
2005 /* cp_parser_lookup_name thought we were looking for a type,
2006 but we're actually looking for a declaration. */
2007 qualifying_class = TYPE_CONTEXT (expr);
2008 expr = TYPE_IDENTIFIER (expr);
2010 else
2011 check_template_keyword (expr);
2014 /* If EXPR occurs as the operand of '&', use special handling that
2015 permits a pointer-to-member. */
2016 if (address_p && done)
2018 if (TREE_CODE (expr) == SCOPE_REF)
2019 expr = TREE_OPERAND (expr, 1);
2020 expr = build_offset_ref (qualifying_class, expr,
2021 /*address_p=*/true, complain);
2022 return expr;
2025 /* No need to check access within an enum. */
2026 if (TREE_CODE (qualifying_class) == ENUMERAL_TYPE)
2027 return expr;
2029 /* Within the scope of a class, turn references to non-static
2030 members into expression of the form "this->...". */
2031 if (template_arg_p)
2032 /* But, within a template argument, we do not want make the
2033 transformation, as there is no "this" pointer. */
2035 else if (TREE_CODE (expr) == FIELD_DECL)
2037 push_deferring_access_checks (dk_no_check);
2038 expr = finish_non_static_data_member (expr, NULL_TREE,
2039 qualifying_class);
2040 pop_deferring_access_checks ();
2042 else if (BASELINK_P (expr))
2044 /* See if any of the functions are non-static members. */
2045 /* If so, the expression may be relative to 'this'. */
2046 if (!shared_member_p (expr)
2047 && current_class_ptr
2048 && DERIVED_FROM_P (qualifying_class,
2049 current_nonlambda_class_type ()))
2050 expr = (build_class_member_access_expr
2051 (maybe_dummy_object (qualifying_class, NULL),
2052 expr,
2053 BASELINK_ACCESS_BINFO (expr),
2054 /*preserve_reference=*/false,
2055 complain));
2056 else if (done)
2057 /* The expression is a qualified name whose address is not
2058 being taken. */
2059 expr = build_offset_ref (qualifying_class, expr, /*address_p=*/false,
2060 complain);
2062 else
2064 /* In a template, return a SCOPE_REF for most qualified-ids
2065 so that we can check access at instantiation time. But if
2066 we're looking at a member of the current instantiation, we
2067 know we have access and building up the SCOPE_REF confuses
2068 non-type template argument handling. */
2069 if (processing_template_decl
2070 && (!currently_open_class (qualifying_class)
2071 || TREE_CODE (expr) == BIT_NOT_EXPR))
2072 expr = build_qualified_name (TREE_TYPE (expr),
2073 qualifying_class, expr,
2074 template_p);
2076 expr = convert_from_reference (expr);
2079 return expr;
2082 /* Begin a statement-expression. The value returned must be passed to
2083 finish_stmt_expr. */
2085 tree
2086 begin_stmt_expr (void)
2088 return push_stmt_list ();
2091 /* Process the final expression of a statement expression. EXPR can be
2092 NULL, if the final expression is empty. Return a STATEMENT_LIST
2093 containing all the statements in the statement-expression, or
2094 ERROR_MARK_NODE if there was an error. */
2096 tree
2097 finish_stmt_expr_expr (tree expr, tree stmt_expr)
2099 if (error_operand_p (expr))
2101 /* The type of the statement-expression is the type of the last
2102 expression. */
2103 TREE_TYPE (stmt_expr) = error_mark_node;
2104 return error_mark_node;
2107 /* If the last statement does not have "void" type, then the value
2108 of the last statement is the value of the entire expression. */
2109 if (expr)
2111 tree type = TREE_TYPE (expr);
2113 if (processing_template_decl)
2115 expr = build_stmt (input_location, EXPR_STMT, expr);
2116 expr = add_stmt (expr);
2117 /* Mark the last statement so that we can recognize it as such at
2118 template-instantiation time. */
2119 EXPR_STMT_STMT_EXPR_RESULT (expr) = 1;
2121 else if (VOID_TYPE_P (type))
2123 /* Just treat this like an ordinary statement. */
2124 expr = finish_expr_stmt (expr);
2126 else
2128 /* It actually has a value we need to deal with. First, force it
2129 to be an rvalue so that we won't need to build up a copy
2130 constructor call later when we try to assign it to something. */
2131 expr = force_rvalue (expr, tf_warning_or_error);
2132 if (error_operand_p (expr))
2133 return error_mark_node;
2135 /* Update for array-to-pointer decay. */
2136 type = TREE_TYPE (expr);
2138 /* Wrap it in a CLEANUP_POINT_EXPR and add it to the list like a
2139 normal statement, but don't convert to void or actually add
2140 the EXPR_STMT. */
2141 if (TREE_CODE (expr) != CLEANUP_POINT_EXPR)
2142 expr = maybe_cleanup_point_expr (expr);
2143 add_stmt (expr);
2146 /* The type of the statement-expression is the type of the last
2147 expression. */
2148 TREE_TYPE (stmt_expr) = type;
2151 return stmt_expr;
2154 /* Finish a statement-expression. EXPR should be the value returned
2155 by the previous begin_stmt_expr. Returns an expression
2156 representing the statement-expression. */
2158 tree
2159 finish_stmt_expr (tree stmt_expr, bool has_no_scope)
2161 tree type;
2162 tree result;
2164 if (error_operand_p (stmt_expr))
2166 pop_stmt_list (stmt_expr);
2167 return error_mark_node;
2170 gcc_assert (TREE_CODE (stmt_expr) == STATEMENT_LIST);
2172 type = TREE_TYPE (stmt_expr);
2173 result = pop_stmt_list (stmt_expr);
2174 TREE_TYPE (result) = type;
2176 if (processing_template_decl)
2178 result = build_min (STMT_EXPR, type, result);
2179 TREE_SIDE_EFFECTS (result) = 1;
2180 STMT_EXPR_NO_SCOPE (result) = has_no_scope;
2182 else if (CLASS_TYPE_P (type))
2184 /* Wrap the statement-expression in a TARGET_EXPR so that the
2185 temporary object created by the final expression is destroyed at
2186 the end of the full-expression containing the
2187 statement-expression. */
2188 result = force_target_expr (type, result, tf_warning_or_error);
2191 return result;
2194 /* Returns the expression which provides the value of STMT_EXPR. */
2196 tree
2197 stmt_expr_value_expr (tree stmt_expr)
2199 tree t = STMT_EXPR_STMT (stmt_expr);
2201 if (TREE_CODE (t) == BIND_EXPR)
2202 t = BIND_EXPR_BODY (t);
2204 if (TREE_CODE (t) == STATEMENT_LIST && STATEMENT_LIST_TAIL (t))
2205 t = STATEMENT_LIST_TAIL (t)->stmt;
2207 if (TREE_CODE (t) == EXPR_STMT)
2208 t = EXPR_STMT_EXPR (t);
2210 return t;
2213 /* Return TRUE iff EXPR_STMT is an empty list of
2214 expression statements. */
2216 bool
2217 empty_expr_stmt_p (tree expr_stmt)
2219 tree body = NULL_TREE;
2221 if (expr_stmt == void_node)
2222 return true;
2224 if (expr_stmt)
2226 if (TREE_CODE (expr_stmt) == EXPR_STMT)
2227 body = EXPR_STMT_EXPR (expr_stmt);
2228 else if (TREE_CODE (expr_stmt) == STATEMENT_LIST)
2229 body = expr_stmt;
2232 if (body)
2234 if (TREE_CODE (body) == STATEMENT_LIST)
2235 return tsi_end_p (tsi_start (body));
2236 else
2237 return empty_expr_stmt_p (body);
2239 return false;
2242 /* Perform Koenig lookup. FN is the postfix-expression representing
2243 the function (or functions) to call; ARGS are the arguments to the
2244 call. Returns the functions to be considered by overload resolution. */
2246 cp_expr
2247 perform_koenig_lookup (cp_expr fn, vec<tree, va_gc> *args,
2248 tsubst_flags_t complain)
2250 tree identifier = NULL_TREE;
2251 tree functions = NULL_TREE;
2252 tree tmpl_args = NULL_TREE;
2253 bool template_id = false;
2254 location_t loc = fn.get_location ();
2256 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
2258 /* Use a separate flag to handle null args. */
2259 template_id = true;
2260 tmpl_args = TREE_OPERAND (fn, 1);
2261 fn = TREE_OPERAND (fn, 0);
2264 /* Find the name of the overloaded function. */
2265 if (identifier_p (fn))
2266 identifier = fn;
2267 else
2269 functions = fn;
2270 identifier = OVL_NAME (functions);
2273 /* A call to a namespace-scope function using an unqualified name.
2275 Do Koenig lookup -- unless any of the arguments are
2276 type-dependent. */
2277 if (!any_type_dependent_arguments_p (args)
2278 && !any_dependent_template_arguments_p (tmpl_args))
2280 fn = lookup_arg_dependent (identifier, functions, args);
2281 if (!fn)
2283 /* The unqualified name could not be resolved. */
2284 if (complain & tf_error)
2285 fn = unqualified_fn_lookup_error (cp_expr (identifier, loc));
2286 else
2287 fn = identifier;
2291 if (fn && template_id && fn != error_mark_node)
2292 fn = build2 (TEMPLATE_ID_EXPR, unknown_type_node, fn, tmpl_args);
2294 return fn;
2297 /* Generate an expression for `FN (ARGS)'. This may change the
2298 contents of ARGS.
2300 If DISALLOW_VIRTUAL is true, the call to FN will be not generated
2301 as a virtual call, even if FN is virtual. (This flag is set when
2302 encountering an expression where the function name is explicitly
2303 qualified. For example a call to `X::f' never generates a virtual
2304 call.)
2306 Returns code for the call. */
2308 tree
2309 finish_call_expr (tree fn, vec<tree, va_gc> **args, bool disallow_virtual,
2310 bool koenig_p, tsubst_flags_t complain)
2312 tree result;
2313 tree orig_fn;
2314 vec<tree, va_gc> *orig_args = NULL;
2316 if (fn == error_mark_node)
2317 return error_mark_node;
2319 gcc_assert (!TYPE_P (fn));
2321 /* If FN may be a FUNCTION_DECL obfuscated by force_paren_expr, undo
2322 it so that we can tell this is a call to a known function. */
2323 fn = maybe_undo_parenthesized_ref (fn);
2325 orig_fn = fn;
2327 if (processing_template_decl)
2329 /* If FN is a local extern declaration or set thereof, look them up
2330 again at instantiation time. */
2331 if (is_overloaded_fn (fn))
2333 tree ifn = get_first_fn (fn);
2334 if (TREE_CODE (ifn) == FUNCTION_DECL
2335 && DECL_LOCAL_FUNCTION_P (ifn))
2336 orig_fn = DECL_NAME (ifn);
2339 /* If the call expression is dependent, build a CALL_EXPR node
2340 with no type; type_dependent_expression_p recognizes
2341 expressions with no type as being dependent. */
2342 if (type_dependent_expression_p (fn)
2343 || any_type_dependent_arguments_p (*args))
2345 result = build_min_nt_call_vec (orig_fn, *args);
2346 SET_EXPR_LOCATION (result, EXPR_LOC_OR_LOC (fn, input_location));
2347 KOENIG_LOOKUP_P (result) = koenig_p;
2348 if (is_overloaded_fn (fn))
2350 fn = get_fns (fn);
2351 lookup_keep (fn, true);
2354 if (cfun)
2356 bool abnormal = true;
2357 for (lkp_iterator iter (fn); abnormal && iter; ++iter)
2359 tree fndecl = *iter;
2360 if (TREE_CODE (fndecl) != FUNCTION_DECL
2361 || !TREE_THIS_VOLATILE (fndecl))
2362 abnormal = false;
2364 /* FIXME: Stop warning about falling off end of non-void
2365 function. But this is wrong. Even if we only see
2366 no-return fns at this point, we could select a
2367 future-defined return fn during instantiation. Or
2368 vice-versa. */
2369 if (abnormal)
2370 current_function_returns_abnormally = 1;
2372 return result;
2374 orig_args = make_tree_vector_copy (*args);
2375 if (!BASELINK_P (fn)
2376 && TREE_CODE (fn) != PSEUDO_DTOR_EXPR
2377 && TREE_TYPE (fn) != unknown_type_node)
2378 fn = build_non_dependent_expr (fn);
2379 make_args_non_dependent (*args);
2382 if (TREE_CODE (fn) == COMPONENT_REF)
2384 tree member = TREE_OPERAND (fn, 1);
2385 if (BASELINK_P (member))
2387 tree object = TREE_OPERAND (fn, 0);
2388 return build_new_method_call (object, member,
2389 args, NULL_TREE,
2390 (disallow_virtual
2391 ? LOOKUP_NORMAL | LOOKUP_NONVIRTUAL
2392 : LOOKUP_NORMAL),
2393 /*fn_p=*/NULL,
2394 complain);
2398 /* Per 13.3.1.1, '(&f)(...)' is the same as '(f)(...)'. */
2399 if (TREE_CODE (fn) == ADDR_EXPR
2400 && TREE_CODE (TREE_OPERAND (fn, 0)) == OVERLOAD)
2401 fn = TREE_OPERAND (fn, 0);
2403 if (is_overloaded_fn (fn))
2404 fn = baselink_for_fns (fn);
2406 result = NULL_TREE;
2407 if (BASELINK_P (fn))
2409 tree object;
2411 /* A call to a member function. From [over.call.func]:
2413 If the keyword this is in scope and refers to the class of
2414 that member function, or a derived class thereof, then the
2415 function call is transformed into a qualified function call
2416 using (*this) as the postfix-expression to the left of the
2417 . operator.... [Otherwise] a contrived object of type T
2418 becomes the implied object argument.
2420 In this situation:
2422 struct A { void f(); };
2423 struct B : public A {};
2424 struct C : public A { void g() { B::f(); }};
2426 "the class of that member function" refers to `A'. But 11.2
2427 [class.access.base] says that we need to convert 'this' to B* as
2428 part of the access, so we pass 'B' to maybe_dummy_object. */
2430 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (get_first_fn (fn)))
2432 /* A constructor call always uses a dummy object. (This constructor
2433 call which has the form A::A () is actually invalid and we are
2434 going to reject it later in build_new_method_call.) */
2435 object = build_dummy_object (BINFO_TYPE (BASELINK_ACCESS_BINFO (fn)));
2437 else
2438 object = maybe_dummy_object (BINFO_TYPE (BASELINK_ACCESS_BINFO (fn)),
2439 NULL);
2441 result = build_new_method_call (object, fn, args, NULL_TREE,
2442 (disallow_virtual
2443 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
2444 : LOOKUP_NORMAL),
2445 /*fn_p=*/NULL,
2446 complain);
2448 else if (is_overloaded_fn (fn))
2450 /* If the function is an overloaded builtin, resolve it. */
2451 if (TREE_CODE (fn) == FUNCTION_DECL
2452 && (DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL
2453 || DECL_BUILT_IN_CLASS (fn) == BUILT_IN_MD))
2454 result = resolve_overloaded_builtin (input_location, fn, *args);
2456 if (!result)
2458 if (warn_sizeof_pointer_memaccess
2459 && (complain & tf_warning)
2460 && !vec_safe_is_empty (*args)
2461 && !processing_template_decl)
2463 location_t sizeof_arg_loc[3];
2464 tree sizeof_arg[3];
2465 unsigned int i;
2466 for (i = 0; i < 3; i++)
2468 tree t;
2470 sizeof_arg_loc[i] = UNKNOWN_LOCATION;
2471 sizeof_arg[i] = NULL_TREE;
2472 if (i >= (*args)->length ())
2473 continue;
2474 t = (**args)[i];
2475 if (TREE_CODE (t) != SIZEOF_EXPR)
2476 continue;
2477 if (SIZEOF_EXPR_TYPE_P (t))
2478 sizeof_arg[i] = TREE_TYPE (TREE_OPERAND (t, 0));
2479 else
2480 sizeof_arg[i] = TREE_OPERAND (t, 0);
2481 sizeof_arg_loc[i] = EXPR_LOCATION (t);
2483 sizeof_pointer_memaccess_warning
2484 (sizeof_arg_loc, fn, *args,
2485 sizeof_arg, same_type_ignoring_top_level_qualifiers_p);
2488 /* A call to a namespace-scope function. */
2489 result = build_new_function_call (fn, args, complain);
2492 else if (TREE_CODE (fn) == PSEUDO_DTOR_EXPR)
2494 if (!vec_safe_is_empty (*args))
2495 error ("arguments to destructor are not allowed");
2496 /* Mark the pseudo-destructor call as having side-effects so
2497 that we do not issue warnings about its use. */
2498 result = build1 (NOP_EXPR,
2499 void_type_node,
2500 TREE_OPERAND (fn, 0));
2501 TREE_SIDE_EFFECTS (result) = 1;
2503 else if (CLASS_TYPE_P (TREE_TYPE (fn)))
2504 /* If the "function" is really an object of class type, it might
2505 have an overloaded `operator ()'. */
2506 result = build_op_call (fn, args, complain);
2508 if (!result)
2509 /* A call where the function is unknown. */
2510 result = cp_build_function_call_vec (fn, args, complain);
2512 if (processing_template_decl && result != error_mark_node)
2514 if (INDIRECT_REF_P (result))
2515 result = TREE_OPERAND (result, 0);
2516 result = build_call_vec (TREE_TYPE (result), orig_fn, orig_args);
2517 SET_EXPR_LOCATION (result, input_location);
2518 KOENIG_LOOKUP_P (result) = koenig_p;
2519 release_tree_vector (orig_args);
2520 result = convert_from_reference (result);
2523 /* Free or retain OVERLOADs from lookup. */
2524 if (is_overloaded_fn (orig_fn))
2525 lookup_keep (get_fns (orig_fn), processing_template_decl);
2527 return result;
2530 /* Finish a call to a postfix increment or decrement or EXPR. (Which
2531 is indicated by CODE, which should be POSTINCREMENT_EXPR or
2532 POSTDECREMENT_EXPR.) */
2534 cp_expr
2535 finish_increment_expr (cp_expr expr, enum tree_code code)
2537 /* input_location holds the location of the trailing operator token.
2538 Build a location of the form:
2539 expr++
2540 ~~~~^~
2541 with the caret at the operator token, ranging from the start
2542 of EXPR to the end of the operator token. */
2543 location_t combined_loc = make_location (input_location,
2544 expr.get_start (),
2545 get_finish (input_location));
2546 cp_expr result = build_x_unary_op (combined_loc, code, expr,
2547 tf_warning_or_error);
2548 /* TODO: build_x_unary_op doesn't honor the location, so set it here. */
2549 result.set_location (combined_loc);
2550 return result;
2553 /* Finish a use of `this'. Returns an expression for `this'. */
2555 tree
2556 finish_this_expr (void)
2558 tree result = NULL_TREE;
2560 if (current_class_ptr)
2562 tree type = TREE_TYPE (current_class_ref);
2564 /* In a lambda expression, 'this' refers to the captured 'this'. */
2565 if (LAMBDA_TYPE_P (type))
2566 result = lambda_expr_this_capture (CLASSTYPE_LAMBDA_EXPR (type), true);
2567 else
2568 result = current_class_ptr;
2571 if (result)
2572 /* The keyword 'this' is a prvalue expression. */
2573 return rvalue (result);
2575 tree fn = current_nonlambda_function ();
2576 if (fn && DECL_STATIC_FUNCTION_P (fn))
2577 error ("%<this%> is unavailable for static member functions");
2578 else if (fn)
2579 error ("invalid use of %<this%> in non-member function");
2580 else
2581 error ("invalid use of %<this%> at top level");
2582 return error_mark_node;
2585 /* Finish a pseudo-destructor expression. If SCOPE is NULL, the
2586 expression was of the form `OBJECT.~DESTRUCTOR' where DESTRUCTOR is
2587 the TYPE for the type given. If SCOPE is non-NULL, the expression
2588 was of the form `OBJECT.SCOPE::~DESTRUCTOR'. */
2590 tree
2591 finish_pseudo_destructor_expr (tree object, tree scope, tree destructor,
2592 location_t loc)
2594 if (object == error_mark_node || destructor == error_mark_node)
2595 return error_mark_node;
2597 gcc_assert (TYPE_P (destructor));
2599 if (!processing_template_decl)
2601 if (scope == error_mark_node)
2603 error_at (loc, "invalid qualifying scope in pseudo-destructor name");
2604 return error_mark_node;
2606 if (is_auto (destructor))
2607 destructor = TREE_TYPE (object);
2608 if (scope && TYPE_P (scope) && !check_dtor_name (scope, destructor))
2610 error_at (loc,
2611 "qualified type %qT does not match destructor name ~%qT",
2612 scope, destructor);
2613 return error_mark_node;
2617 /* [expr.pseudo] says both:
2619 The type designated by the pseudo-destructor-name shall be
2620 the same as the object type.
2622 and:
2624 The cv-unqualified versions of the object type and of the
2625 type designated by the pseudo-destructor-name shall be the
2626 same type.
2628 We implement the more generous second sentence, since that is
2629 what most other compilers do. */
2630 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (object),
2631 destructor))
2633 error_at (loc, "%qE is not of type %qT", object, destructor);
2634 return error_mark_node;
2638 return build3_loc (loc, PSEUDO_DTOR_EXPR, void_type_node, object,
2639 scope, destructor);
2642 /* Finish an expression of the form CODE EXPR. */
2644 cp_expr
2645 finish_unary_op_expr (location_t op_loc, enum tree_code code, cp_expr expr,
2646 tsubst_flags_t complain)
2648 /* Build a location of the form:
2649 ++expr
2650 ^~~~~~
2651 with the caret at the operator token, ranging from the start
2652 of the operator token to the end of EXPR. */
2653 location_t combined_loc = make_location (op_loc,
2654 op_loc, expr.get_finish ());
2655 cp_expr result = build_x_unary_op (combined_loc, code, expr, complain);
2656 /* TODO: build_x_unary_op doesn't always honor the location. */
2657 result.set_location (combined_loc);
2659 tree result_ovl, expr_ovl;
2661 if (!(complain & tf_warning))
2662 return result;
2664 result_ovl = result;
2665 expr_ovl = expr;
2667 if (!processing_template_decl)
2668 expr_ovl = cp_fully_fold (expr_ovl);
2670 if (!CONSTANT_CLASS_P (expr_ovl)
2671 || TREE_OVERFLOW_P (expr_ovl))
2672 return result;
2674 if (!processing_template_decl)
2675 result_ovl = cp_fully_fold (result_ovl);
2677 if (CONSTANT_CLASS_P (result_ovl) && TREE_OVERFLOW_P (result_ovl))
2678 overflow_warning (combined_loc, result_ovl);
2680 return result;
2683 /* Finish a compound-literal expression or C++11 functional cast with aggregate
2684 initializer. TYPE is the type to which the CONSTRUCTOR in COMPOUND_LITERAL
2685 is being cast. */
2687 tree
2688 finish_compound_literal (tree type, tree compound_literal,
2689 tsubst_flags_t complain,
2690 fcl_t fcl_context)
2692 if (type == error_mark_node)
2693 return error_mark_node;
2695 if (TREE_CODE (type) == REFERENCE_TYPE)
2697 compound_literal
2698 = finish_compound_literal (TREE_TYPE (type), compound_literal,
2699 complain, fcl_context);
2700 return cp_build_c_cast (type, compound_literal, complain);
2703 if (!TYPE_OBJ_P (type))
2705 if (complain & tf_error)
2706 error ("compound literal of non-object type %qT", type);
2707 return error_mark_node;
2710 if (tree anode = type_uses_auto (type))
2711 if (CLASS_PLACEHOLDER_TEMPLATE (anode))
2713 type = do_auto_deduction (type, compound_literal, anode, complain,
2714 adc_variable_type);
2715 if (type == error_mark_node)
2716 return error_mark_node;
2719 if (processing_template_decl)
2721 TREE_TYPE (compound_literal) = type;
2722 /* Mark the expression as a compound literal. */
2723 TREE_HAS_CONSTRUCTOR (compound_literal) = 1;
2724 if (fcl_context == fcl_c99)
2725 CONSTRUCTOR_C99_COMPOUND_LITERAL (compound_literal) = 1;
2726 return compound_literal;
2729 type = complete_type (type);
2731 if (TYPE_NON_AGGREGATE_CLASS (type))
2733 /* Trying to deal with a CONSTRUCTOR instead of a TREE_LIST
2734 everywhere that deals with function arguments would be a pain, so
2735 just wrap it in a TREE_LIST. The parser set a flag so we know
2736 that it came from T{} rather than T({}). */
2737 CONSTRUCTOR_IS_DIRECT_INIT (compound_literal) = 1;
2738 compound_literal = build_tree_list (NULL_TREE, compound_literal);
2739 return build_functional_cast (type, compound_literal, complain);
2742 if (TREE_CODE (type) == ARRAY_TYPE
2743 && check_array_initializer (NULL_TREE, type, compound_literal))
2744 return error_mark_node;
2745 compound_literal = reshape_init (type, compound_literal, complain);
2746 if (SCALAR_TYPE_P (type)
2747 && !BRACE_ENCLOSED_INITIALIZER_P (compound_literal)
2748 && !check_narrowing (type, compound_literal, complain))
2749 return error_mark_node;
2750 if (TREE_CODE (type) == ARRAY_TYPE
2751 && TYPE_DOMAIN (type) == NULL_TREE)
2753 cp_complete_array_type_or_error (&type, compound_literal,
2754 false, complain);
2755 if (type == error_mark_node)
2756 return error_mark_node;
2758 compound_literal = digest_init_flags (type, compound_literal, LOOKUP_NORMAL,
2759 complain);
2760 if (TREE_CODE (compound_literal) == CONSTRUCTOR)
2762 TREE_HAS_CONSTRUCTOR (compound_literal) = true;
2763 if (fcl_context == fcl_c99)
2764 CONSTRUCTOR_C99_COMPOUND_LITERAL (compound_literal) = 1;
2767 /* Put static/constant array temporaries in static variables. */
2768 /* FIXME all C99 compound literals should be variables rather than C++
2769 temporaries, unless they are used as an aggregate initializer. */
2770 if ((!at_function_scope_p () || CP_TYPE_CONST_P (type))
2771 && fcl_context == fcl_c99
2772 && TREE_CODE (type) == ARRAY_TYPE
2773 && !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
2774 && initializer_constant_valid_p (compound_literal, type))
2776 tree decl = create_temporary_var (type);
2777 DECL_INITIAL (decl) = compound_literal;
2778 TREE_STATIC (decl) = 1;
2779 if (literal_type_p (type) && CP_TYPE_CONST_NON_VOLATILE_P (type))
2781 /* 5.19 says that a constant expression can include an
2782 lvalue-rvalue conversion applied to "a glvalue of literal type
2783 that refers to a non-volatile temporary object initialized
2784 with a constant expression". Rather than try to communicate
2785 that this VAR_DECL is a temporary, just mark it constexpr. */
2786 DECL_DECLARED_CONSTEXPR_P (decl) = true;
2787 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
2788 TREE_CONSTANT (decl) = true;
2790 cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
2791 decl = pushdecl_top_level (decl);
2792 DECL_NAME (decl) = make_anon_name ();
2793 SET_DECL_ASSEMBLER_NAME (decl, DECL_NAME (decl));
2794 /* Make sure the destructor is callable. */
2795 tree clean = cxx_maybe_build_cleanup (decl, complain);
2796 if (clean == error_mark_node)
2797 return error_mark_node;
2798 return decl;
2801 /* Represent other compound literals with TARGET_EXPR so we produce
2802 an lvalue, but can elide copies. */
2803 if (!VECTOR_TYPE_P (type))
2804 compound_literal = get_target_expr_sfinae (compound_literal, complain);
2806 return compound_literal;
2809 /* Return the declaration for the function-name variable indicated by
2810 ID. */
2812 tree
2813 finish_fname (tree id)
2815 tree decl;
2817 decl = fname_decl (input_location, C_RID_CODE (id), id);
2818 if (processing_template_decl && current_function_decl
2819 && decl != error_mark_node)
2820 decl = DECL_NAME (decl);
2821 return decl;
2824 /* Finish a translation unit. */
2826 void
2827 finish_translation_unit (void)
2829 /* In case there were missing closebraces,
2830 get us back to the global binding level. */
2831 pop_everything ();
2832 while (current_namespace != global_namespace)
2833 pop_namespace ();
2835 /* Do file scope __FUNCTION__ et al. */
2836 finish_fname_decls ();
2839 /* Finish a template type parameter, specified as AGGR IDENTIFIER.
2840 Returns the parameter. */
2842 tree
2843 finish_template_type_parm (tree aggr, tree identifier)
2845 if (aggr != class_type_node)
2847 permerror (input_location, "template type parameters must use the keyword %<class%> or %<typename%>");
2848 aggr = class_type_node;
2851 return build_tree_list (aggr, identifier);
2854 /* Finish a template template parameter, specified as AGGR IDENTIFIER.
2855 Returns the parameter. */
2857 tree
2858 finish_template_template_parm (tree aggr, tree identifier)
2860 tree decl = build_decl (input_location,
2861 TYPE_DECL, identifier, NULL_TREE);
2863 tree tmpl = build_lang_decl (TEMPLATE_DECL, identifier, NULL_TREE);
2864 DECL_TEMPLATE_PARMS (tmpl) = current_template_parms;
2865 DECL_TEMPLATE_RESULT (tmpl) = decl;
2866 DECL_ARTIFICIAL (decl) = 1;
2868 // Associate the constraints with the underlying declaration,
2869 // not the template.
2870 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
2871 tree constr = build_constraints (reqs, NULL_TREE);
2872 set_constraints (decl, constr);
2874 end_template_decl ();
2876 gcc_assert (DECL_TEMPLATE_PARMS (tmpl));
2878 check_default_tmpl_args (decl, DECL_TEMPLATE_PARMS (tmpl),
2879 /*is_primary=*/true, /*is_partial=*/false,
2880 /*is_friend=*/0);
2882 return finish_template_type_parm (aggr, tmpl);
2885 /* ARGUMENT is the default-argument value for a template template
2886 parameter. If ARGUMENT is invalid, issue error messages and return
2887 the ERROR_MARK_NODE. Otherwise, ARGUMENT itself is returned. */
2889 tree
2890 check_template_template_default_arg (tree argument)
2892 if (TREE_CODE (argument) != TEMPLATE_DECL
2893 && TREE_CODE (argument) != TEMPLATE_TEMPLATE_PARM
2894 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
2896 if (TREE_CODE (argument) == TYPE_DECL)
2897 error ("invalid use of type %qT as a default value for a template "
2898 "template-parameter", TREE_TYPE (argument));
2899 else
2900 error ("invalid default argument for a template template parameter");
2901 return error_mark_node;
2904 return argument;
2907 /* Begin a class definition, as indicated by T. */
2909 tree
2910 begin_class_definition (tree t)
2912 if (error_operand_p (t) || error_operand_p (TYPE_MAIN_DECL (t)))
2913 return error_mark_node;
2915 if (processing_template_parmlist)
2917 error ("definition of %q#T inside template parameter list", t);
2918 return error_mark_node;
2921 /* According to the C++ ABI, decimal classes defined in ISO/IEC TR 24733
2922 are passed the same as decimal scalar types. */
2923 if (TREE_CODE (t) == RECORD_TYPE
2924 && !processing_template_decl)
2926 tree ns = TYPE_CONTEXT (t);
2927 if (ns && TREE_CODE (ns) == NAMESPACE_DECL
2928 && DECL_CONTEXT (ns) == std_node
2929 && DECL_NAME (ns)
2930 && id_equal (DECL_NAME (ns), "decimal"))
2932 const char *n = TYPE_NAME_STRING (t);
2933 if ((strcmp (n, "decimal32") == 0)
2934 || (strcmp (n, "decimal64") == 0)
2935 || (strcmp (n, "decimal128") == 0))
2936 TYPE_TRANSPARENT_AGGR (t) = 1;
2940 /* A non-implicit typename comes from code like:
2942 template <typename T> struct A {
2943 template <typename U> struct A<T>::B ...
2945 This is erroneous. */
2946 else if (TREE_CODE (t) == TYPENAME_TYPE)
2948 error ("invalid definition of qualified type %qT", t);
2949 t = error_mark_node;
2952 if (t == error_mark_node || ! MAYBE_CLASS_TYPE_P (t))
2954 t = make_class_type (RECORD_TYPE);
2955 pushtag (make_anon_name (), t, /*tag_scope=*/ts_current);
2958 if (TYPE_BEING_DEFINED (t))
2960 t = make_class_type (TREE_CODE (t));
2961 pushtag (TYPE_IDENTIFIER (t), t, /*tag_scope=*/ts_current);
2963 maybe_process_partial_specialization (t);
2964 pushclass (t);
2965 TYPE_BEING_DEFINED (t) = 1;
2966 class_binding_level->defining_class_p = 1;
2968 if (flag_pack_struct)
2970 tree v;
2971 TYPE_PACKED (t) = 1;
2972 /* Even though the type is being defined for the first time
2973 here, there might have been a forward declaration, so there
2974 might be cv-qualified variants of T. */
2975 for (v = TYPE_NEXT_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v))
2976 TYPE_PACKED (v) = 1;
2978 /* Reset the interface data, at the earliest possible
2979 moment, as it might have been set via a class foo;
2980 before. */
2981 if (! TYPE_UNNAMED_P (t))
2983 struct c_fileinfo *finfo = \
2984 get_fileinfo (LOCATION_FILE (input_location));
2985 CLASSTYPE_INTERFACE_ONLY (t) = finfo->interface_only;
2986 SET_CLASSTYPE_INTERFACE_UNKNOWN_X
2987 (t, finfo->interface_unknown);
2989 reset_specialization();
2991 /* Make a declaration for this class in its own scope. */
2992 build_self_reference ();
2994 return t;
2997 /* Finish the member declaration given by DECL. */
2999 void
3000 finish_member_declaration (tree decl)
3002 if (decl == error_mark_node || decl == NULL_TREE)
3003 return;
3005 if (decl == void_type_node)
3006 /* The COMPONENT was a friend, not a member, and so there's
3007 nothing for us to do. */
3008 return;
3010 /* We should see only one DECL at a time. */
3011 gcc_assert (DECL_CHAIN (decl) == NULL_TREE);
3013 /* Don't add decls after definition. */
3014 gcc_assert (TYPE_BEING_DEFINED (current_class_type)
3015 /* We can add lambda types when late parsing default
3016 arguments. */
3017 || LAMBDA_TYPE_P (TREE_TYPE (decl)));
3019 /* Set up access control for DECL. */
3020 TREE_PRIVATE (decl)
3021 = (current_access_specifier == access_private_node);
3022 TREE_PROTECTED (decl)
3023 = (current_access_specifier == access_protected_node);
3024 if (TREE_CODE (decl) == TEMPLATE_DECL)
3026 TREE_PRIVATE (DECL_TEMPLATE_RESULT (decl)) = TREE_PRIVATE (decl);
3027 TREE_PROTECTED (DECL_TEMPLATE_RESULT (decl)) = TREE_PROTECTED (decl);
3030 /* Mark the DECL as a member of the current class, unless it's
3031 a member of an enumeration. */
3032 if (TREE_CODE (decl) != CONST_DECL)
3033 DECL_CONTEXT (decl) = current_class_type;
3035 if (TREE_CODE (decl) == USING_DECL)
3036 /* For now, ignore class-scope USING_DECLS, so that debugging
3037 backends do not see them. */
3038 DECL_IGNORED_P (decl) = 1;
3040 /* Check for bare parameter packs in the non-static data member
3041 declaration. */
3042 if (TREE_CODE (decl) == FIELD_DECL)
3044 if (check_for_bare_parameter_packs (TREE_TYPE (decl)))
3045 TREE_TYPE (decl) = error_mark_node;
3046 if (check_for_bare_parameter_packs (DECL_ATTRIBUTES (decl)))
3047 DECL_ATTRIBUTES (decl) = NULL_TREE;
3050 /* [dcl.link]
3052 A C language linkage is ignored for the names of class members
3053 and the member function type of class member functions. */
3054 if (DECL_LANG_SPECIFIC (decl))
3055 SET_DECL_LANGUAGE (decl, lang_cplusplus);
3057 bool add = false;
3059 /* Functions and non-functions are added differently. */
3060 if (DECL_DECLARES_FUNCTION_P (decl))
3061 add = add_method (current_class_type, decl, false);
3062 /* Enter the DECL into the scope of the class, if the class
3063 isn't a closure (whose fields are supposed to be unnamed). */
3064 else if (CLASSTYPE_LAMBDA_EXPR (current_class_type)
3065 || pushdecl_class_level (decl))
3066 add = true;
3068 if (add)
3070 /* All TYPE_DECLs go at the end of TYPE_FIELDS. Ordinary fields
3071 go at the beginning. The reason is that
3072 legacy_nonfn_member_lookup searches the list in order, and we
3073 want a field name to override a type name so that the "struct
3074 stat hack" will work. In particular:
3076 struct S { enum E { }; static const int E = 5; int ary[S::E]; } s;
3078 is valid. */
3080 if (TREE_CODE (decl) == TYPE_DECL)
3081 TYPE_FIELDS (current_class_type)
3082 = chainon (TYPE_FIELDS (current_class_type), decl);
3083 else
3085 DECL_CHAIN (decl) = TYPE_FIELDS (current_class_type);
3086 TYPE_FIELDS (current_class_type) = decl;
3089 maybe_add_class_template_decl_list (current_class_type, decl,
3090 /*friend_p=*/0);
3094 /* Finish processing a complete template declaration. The PARMS are
3095 the template parameters. */
3097 void
3098 finish_template_decl (tree parms)
3100 if (parms)
3101 end_template_decl ();
3102 else
3103 end_specialization ();
3106 // Returns the template type of the class scope being entered. If we're
3107 // entering a constrained class scope. TYPE is the class template
3108 // scope being entered and we may need to match the intended type with
3109 // a constrained specialization. For example:
3111 // template<Object T>
3112 // struct S { void f(); }; #1
3114 // template<Object T>
3115 // void S<T>::f() { } #2
3117 // We check, in #2, that S<T> refers precisely to the type declared by
3118 // #1 (i.e., that the constraints match). Note that the following should
3119 // be an error since there is no specialization of S<T> that is
3120 // unconstrained, but this is not diagnosed here.
3122 // template<typename T>
3123 // void S<T>::f() { }
3125 // We cannot diagnose this problem here since this function also matches
3126 // qualified template names that are not part of a definition. For example:
3128 // template<Integral T, Floating_point U>
3129 // typename pair<T, U>::first_type void f(T, U);
3131 // Here, it is unlikely that there is a partial specialization of
3132 // pair constrained for for Integral and Floating_point arguments.
3134 // The general rule is: if a constrained specialization with matching
3135 // constraints is found return that type. Also note that if TYPE is not a
3136 // class-type (e.g. a typename type), then no fixup is needed.
3138 static tree
3139 fixup_template_type (tree type)
3141 // Find the template parameter list at the a depth appropriate to
3142 // the scope we're trying to enter.
3143 tree parms = current_template_parms;
3144 int depth = template_class_depth (type);
3145 for (int n = processing_template_decl; n > depth && parms; --n)
3146 parms = TREE_CHAIN (parms);
3147 if (!parms)
3148 return type;
3149 tree cur_reqs = TEMPLATE_PARMS_CONSTRAINTS (parms);
3150 tree cur_constr = build_constraints (cur_reqs, NULL_TREE);
3152 // Search for a specialization whose type and constraints match.
3153 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
3154 tree specs = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
3155 while (specs)
3157 tree spec_constr = get_constraints (TREE_VALUE (specs));
3159 // If the type and constraints match a specialization, then we
3160 // are entering that type.
3161 if (same_type_p (type, TREE_TYPE (specs))
3162 && equivalent_constraints (cur_constr, spec_constr))
3163 return TREE_TYPE (specs);
3164 specs = TREE_CHAIN (specs);
3167 // If no specialization matches, then must return the type
3168 // previously found.
3169 return type;
3172 /* Finish processing a template-id (which names a type) of the form
3173 NAME < ARGS >. Return the TYPE_DECL for the type named by the
3174 template-id. If ENTERING_SCOPE is nonzero we are about to enter
3175 the scope of template-id indicated. */
3177 tree
3178 finish_template_type (tree name, tree args, int entering_scope)
3180 tree type;
3182 type = lookup_template_class (name, args,
3183 NULL_TREE, NULL_TREE, entering_scope,
3184 tf_warning_or_error | tf_user);
3186 /* If we might be entering the scope of a partial specialization,
3187 find the one with the right constraints. */
3188 if (flag_concepts
3189 && entering_scope
3190 && CLASS_TYPE_P (type)
3191 && CLASSTYPE_TEMPLATE_INFO (type)
3192 && dependent_type_p (type)
3193 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
3194 type = fixup_template_type (type);
3196 if (type == error_mark_node)
3197 return type;
3198 else if (CLASS_TYPE_P (type) && !alias_type_or_template_p (type))
3199 return TYPE_STUB_DECL (type);
3200 else
3201 return TYPE_NAME (type);
3204 /* Finish processing a BASE_CLASS with the indicated ACCESS_SPECIFIER.
3205 Return a TREE_LIST containing the ACCESS_SPECIFIER and the
3206 BASE_CLASS, or NULL_TREE if an error occurred. The
3207 ACCESS_SPECIFIER is one of
3208 access_{default,public,protected_private}_node. For a virtual base
3209 we set TREE_TYPE. */
3211 tree
3212 finish_base_specifier (tree base, tree access, bool virtual_p)
3214 tree result;
3216 if (base == error_mark_node)
3218 error ("invalid base-class specification");
3219 result = NULL_TREE;
3221 else if (! MAYBE_CLASS_TYPE_P (base))
3223 error ("%qT is not a class type", base);
3224 result = NULL_TREE;
3226 else
3228 if (cp_type_quals (base) != 0)
3230 /* DR 484: Can a base-specifier name a cv-qualified
3231 class type? */
3232 base = TYPE_MAIN_VARIANT (base);
3234 result = build_tree_list (access, base);
3235 if (virtual_p)
3236 TREE_TYPE (result) = integer_type_node;
3239 return result;
3242 /* If FNS is a member function, a set of member functions, or a
3243 template-id referring to one or more member functions, return a
3244 BASELINK for FNS, incorporating the current access context.
3245 Otherwise, return FNS unchanged. */
3247 tree
3248 baselink_for_fns (tree fns)
3250 tree scope;
3251 tree cl;
3253 if (BASELINK_P (fns)
3254 || error_operand_p (fns))
3255 return fns;
3257 scope = ovl_scope (fns);
3258 if (!CLASS_TYPE_P (scope))
3259 return fns;
3261 cl = currently_open_derived_class (scope);
3262 if (!cl)
3263 cl = scope;
3264 cl = TYPE_BINFO (cl);
3265 return build_baselink (cl, cl, fns, /*optype=*/NULL_TREE);
3268 /* Returns true iff DECL is a variable from a function outside
3269 the current one. */
3271 static bool
3272 outer_var_p (tree decl)
3274 return ((VAR_P (decl) || TREE_CODE (decl) == PARM_DECL)
3275 && DECL_FUNCTION_SCOPE_P (decl)
3276 /* Don't get confused by temporaries. */
3277 && DECL_NAME (decl)
3278 && (DECL_CONTEXT (decl) != current_function_decl
3279 || parsing_nsdmi ()));
3282 /* As above, but also checks that DECL is automatic. */
3284 bool
3285 outer_automatic_var_p (tree decl)
3287 return (outer_var_p (decl)
3288 && !TREE_STATIC (decl));
3291 /* DECL satisfies outer_automatic_var_p. Possibly complain about it or
3292 rewrite it for lambda capture.
3294 If ODR_USE is true, we're being called from mark_use, and we complain about
3295 use of constant variables. If ODR_USE is false, we're being called for the
3296 id-expression, and we do lambda capture. */
3298 tree
3299 process_outer_var_ref (tree decl, tsubst_flags_t complain, bool odr_use)
3301 if (cp_unevaluated_operand)
3302 /* It's not a use (3.2) if we're in an unevaluated context. */
3303 return decl;
3304 if (decl == error_mark_node)
3305 return decl;
3307 tree context = DECL_CONTEXT (decl);
3308 tree containing_function = current_function_decl;
3309 tree lambda_stack = NULL_TREE;
3310 tree lambda_expr = NULL_TREE;
3311 tree initializer = convert_from_reference (decl);
3313 /* Mark it as used now even if the use is ill-formed. */
3314 if (!mark_used (decl, complain))
3315 return error_mark_node;
3317 if (parsing_nsdmi ())
3318 containing_function = NULL_TREE;
3320 if (containing_function && LAMBDA_FUNCTION_P (containing_function))
3322 /* Check whether we've already built a proxy. */
3323 tree var = decl;
3324 while (is_capture_proxy_with_ref (var))
3325 var = DECL_CAPTURED_VARIABLE (var);
3326 tree d = retrieve_local_specialization (var);
3328 if (d && d != decl && is_capture_proxy (d))
3330 if (DECL_CONTEXT (d) == containing_function)
3331 /* We already have an inner proxy. */
3332 return d;
3333 else
3334 /* We need to capture an outer proxy. */
3335 return process_outer_var_ref (d, complain, odr_use);
3339 /* If we are in a lambda function, we can move out until we hit
3340 1. the context,
3341 2. a non-lambda function, or
3342 3. a non-default capturing lambda function. */
3343 while (context != containing_function
3344 /* containing_function can be null with invalid generic lambdas. */
3345 && containing_function
3346 && LAMBDA_FUNCTION_P (containing_function))
3348 tree closure = DECL_CONTEXT (containing_function);
3349 lambda_expr = CLASSTYPE_LAMBDA_EXPR (closure);
3351 if (TYPE_CLASS_SCOPE_P (closure))
3352 /* A lambda in an NSDMI (c++/64496). */
3353 break;
3355 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr)
3356 == CPLD_NONE)
3357 break;
3359 lambda_stack = tree_cons (NULL_TREE,
3360 lambda_expr,
3361 lambda_stack);
3363 containing_function
3364 = decl_function_context (containing_function);
3367 /* In a lambda within a template, wait until instantiation
3368 time to implicitly capture. */
3369 if (context == containing_function
3370 && DECL_TEMPLATE_INFO (containing_function)
3371 && uses_template_parms (DECL_TI_ARGS (containing_function)))
3372 return decl;
3374 if (lambda_expr && VAR_P (decl)
3375 && DECL_ANON_UNION_VAR_P (decl))
3377 if (complain & tf_error)
3378 error ("cannot capture member %qD of anonymous union", decl);
3379 return error_mark_node;
3381 /* Do lambda capture when processing the id-expression, not when
3382 odr-using a variable. */
3383 if (!odr_use && context == containing_function)
3385 decl = add_default_capture (lambda_stack,
3386 /*id=*/DECL_NAME (decl),
3387 initializer);
3389 /* Only an odr-use of an outer automatic variable causes an
3390 error, and a constant variable can decay to a prvalue
3391 constant without odr-use. So don't complain yet. */
3392 else if (!odr_use && decl_constant_var_p (decl))
3393 return decl;
3394 else if (lambda_expr)
3396 if (complain & tf_error)
3398 error ("%qD is not captured", decl);
3399 tree closure = LAMBDA_EXPR_CLOSURE (lambda_expr);
3400 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr)
3401 == CPLD_NONE)
3402 inform (location_of (closure),
3403 "the lambda has no capture-default");
3404 else if (TYPE_CLASS_SCOPE_P (closure))
3405 inform (UNKNOWN_LOCATION, "lambda in local class %q+T cannot "
3406 "capture variables from the enclosing context",
3407 TYPE_CONTEXT (closure));
3408 inform (DECL_SOURCE_LOCATION (decl), "%q#D declared here", decl);
3410 return error_mark_node;
3412 else
3414 if (complain & tf_error)
3416 error (VAR_P (decl)
3417 ? G_("use of local variable with automatic storage from "
3418 "containing function")
3419 : G_("use of parameter from containing function"));
3420 inform (DECL_SOURCE_LOCATION (decl), "%q#D declared here", decl);
3422 return error_mark_node;
3424 return decl;
3427 /* ID_EXPRESSION is a representation of parsed, but unprocessed,
3428 id-expression. (See cp_parser_id_expression for details.) SCOPE,
3429 if non-NULL, is the type or namespace used to explicitly qualify
3430 ID_EXPRESSION. DECL is the entity to which that name has been
3431 resolved.
3433 *CONSTANT_EXPRESSION_P is true if we are presently parsing a
3434 constant-expression. In that case, *NON_CONSTANT_EXPRESSION_P will
3435 be set to true if this expression isn't permitted in a
3436 constant-expression, but it is otherwise not set by this function.
3437 *ALLOW_NON_CONSTANT_EXPRESSION_P is true if we are parsing a
3438 constant-expression, but a non-constant expression is also
3439 permissible.
3441 DONE is true if this expression is a complete postfix-expression;
3442 it is false if this expression is followed by '->', '[', '(', etc.
3443 ADDRESS_P is true iff this expression is the operand of '&'.
3444 TEMPLATE_P is true iff the qualified-id was of the form
3445 "A::template B". TEMPLATE_ARG_P is true iff this qualified name
3446 appears as a template argument.
3448 If an error occurs, and it is the kind of error that might cause
3449 the parser to abort a tentative parse, *ERROR_MSG is filled in. It
3450 is the caller's responsibility to issue the message. *ERROR_MSG
3451 will be a string with static storage duration, so the caller need
3452 not "free" it.
3454 Return an expression for the entity, after issuing appropriate
3455 diagnostics. This function is also responsible for transforming a
3456 reference to a non-static member into a COMPONENT_REF that makes
3457 the use of "this" explicit.
3459 Upon return, *IDK will be filled in appropriately. */
3460 cp_expr
3461 finish_id_expression (tree id_expression,
3462 tree decl,
3463 tree scope,
3464 cp_id_kind *idk,
3465 bool integral_constant_expression_p,
3466 bool allow_non_integral_constant_expression_p,
3467 bool *non_integral_constant_expression_p,
3468 bool template_p,
3469 bool done,
3470 bool address_p,
3471 bool template_arg_p,
3472 const char **error_msg,
3473 location_t location)
3475 decl = strip_using_decl (decl);
3477 /* Initialize the output parameters. */
3478 *idk = CP_ID_KIND_NONE;
3479 *error_msg = NULL;
3481 if (id_expression == error_mark_node)
3482 return error_mark_node;
3483 /* If we have a template-id, then no further lookup is
3484 required. If the template-id was for a template-class, we
3485 will sometimes have a TYPE_DECL at this point. */
3486 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3487 || TREE_CODE (decl) == TYPE_DECL)
3489 /* Look up the name. */
3490 else
3492 if (decl == error_mark_node)
3494 /* Name lookup failed. */
3495 if (scope
3496 && (!TYPE_P (scope)
3497 || (!dependent_type_p (scope)
3498 && !(identifier_p (id_expression)
3499 && IDENTIFIER_CONV_OP_P (id_expression)
3500 && dependent_type_p (TREE_TYPE (id_expression))))))
3502 /* If the qualifying type is non-dependent (and the name
3503 does not name a conversion operator to a dependent
3504 type), issue an error. */
3505 qualified_name_lookup_error (scope, id_expression, decl, location);
3506 return error_mark_node;
3508 else if (!scope)
3510 /* It may be resolved via Koenig lookup. */
3511 *idk = CP_ID_KIND_UNQUALIFIED;
3512 return id_expression;
3514 else
3515 decl = id_expression;
3517 /* If DECL is a variable that would be out of scope under
3518 ANSI/ISO rules, but in scope in the ARM, name lookup
3519 will succeed. Issue a diagnostic here. */
3520 else
3521 decl = check_for_out_of_scope_variable (decl);
3523 /* Remember that the name was used in the definition of
3524 the current class so that we can check later to see if
3525 the meaning would have been different after the class
3526 was entirely defined. */
3527 if (!scope && decl != error_mark_node && identifier_p (id_expression))
3528 maybe_note_name_used_in_class (id_expression, decl);
3530 /* A use in unevaluated operand might not be instantiated appropriately
3531 if tsubst_copy builds a dummy parm, or if we never instantiate a
3532 generic lambda, so mark it now. */
3533 if (processing_template_decl && cp_unevaluated_operand)
3534 mark_type_use (decl);
3536 /* Disallow uses of local variables from containing functions, except
3537 within lambda-expressions. */
3538 if (outer_automatic_var_p (decl))
3540 decl = process_outer_var_ref (decl, tf_warning_or_error);
3541 if (decl == error_mark_node)
3542 return error_mark_node;
3545 /* Also disallow uses of function parameters outside the function
3546 body, except inside an unevaluated context (i.e. decltype). */
3547 if (TREE_CODE (decl) == PARM_DECL
3548 && DECL_CONTEXT (decl) == NULL_TREE
3549 && !cp_unevaluated_operand)
3551 *error_msg = G_("use of parameter outside function body");
3552 return error_mark_node;
3556 /* If we didn't find anything, or what we found was a type,
3557 then this wasn't really an id-expression. */
3558 if (TREE_CODE (decl) == TEMPLATE_DECL
3559 && !DECL_FUNCTION_TEMPLATE_P (decl))
3561 *error_msg = G_("missing template arguments");
3562 return error_mark_node;
3564 else if (TREE_CODE (decl) == TYPE_DECL
3565 || TREE_CODE (decl) == NAMESPACE_DECL)
3567 *error_msg = G_("expected primary-expression");
3568 return error_mark_node;
3571 /* If the name resolved to a template parameter, there is no
3572 need to look it up again later. */
3573 if ((TREE_CODE (decl) == CONST_DECL && DECL_TEMPLATE_PARM_P (decl))
3574 || TREE_CODE (decl) == TEMPLATE_PARM_INDEX)
3576 tree r;
3578 *idk = CP_ID_KIND_NONE;
3579 if (TREE_CODE (decl) == TEMPLATE_PARM_INDEX)
3580 decl = TEMPLATE_PARM_DECL (decl);
3581 r = convert_from_reference (DECL_INITIAL (decl));
3583 if (integral_constant_expression_p
3584 && !dependent_type_p (TREE_TYPE (decl))
3585 && !(INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (r))))
3587 if (!allow_non_integral_constant_expression_p)
3588 error ("template parameter %qD of type %qT is not allowed in "
3589 "an integral constant expression because it is not of "
3590 "integral or enumeration type", decl, TREE_TYPE (decl));
3591 *non_integral_constant_expression_p = true;
3593 return r;
3595 else
3597 bool dependent_p = type_dependent_expression_p (decl);
3599 /* If the declaration was explicitly qualified indicate
3600 that. The semantics of `A::f(3)' are different than
3601 `f(3)' if `f' is virtual. */
3602 *idk = (scope
3603 ? CP_ID_KIND_QUALIFIED
3604 : (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3605 ? CP_ID_KIND_TEMPLATE_ID
3606 : (dependent_p
3607 ? CP_ID_KIND_UNQUALIFIED_DEPENDENT
3608 : CP_ID_KIND_UNQUALIFIED)));
3610 if (dependent_p
3611 && DECL_P (decl)
3612 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (decl)))
3613 /* Dependent type attributes on the decl mean that the TREE_TYPE is
3614 wrong, so just return the identifier. */
3615 return id_expression;
3617 if (TREE_CODE (decl) == NAMESPACE_DECL)
3619 error ("use of namespace %qD as expression", decl);
3620 return error_mark_node;
3622 else if (DECL_CLASS_TEMPLATE_P (decl))
3624 error ("use of class template %qT as expression", decl);
3625 return error_mark_node;
3627 else if (TREE_CODE (decl) == TREE_LIST)
3629 /* Ambiguous reference to base members. */
3630 error ("request for member %qD is ambiguous in "
3631 "multiple inheritance lattice", id_expression);
3632 print_candidates (decl);
3633 return error_mark_node;
3636 /* Mark variable-like entities as used. Functions are similarly
3637 marked either below or after overload resolution. */
3638 if ((VAR_P (decl)
3639 || TREE_CODE (decl) == PARM_DECL
3640 || TREE_CODE (decl) == CONST_DECL
3641 || TREE_CODE (decl) == RESULT_DECL)
3642 && !mark_used (decl))
3643 return error_mark_node;
3645 /* Only certain kinds of names are allowed in constant
3646 expression. Template parameters have already
3647 been handled above. */
3648 if (! error_operand_p (decl)
3649 && !dependent_p
3650 && integral_constant_expression_p
3651 && ! decl_constant_var_p (decl)
3652 && TREE_CODE (decl) != CONST_DECL
3653 && ! builtin_valid_in_constant_expr_p (decl))
3655 if (!allow_non_integral_constant_expression_p)
3657 error ("%qD cannot appear in a constant-expression", decl);
3658 return error_mark_node;
3660 *non_integral_constant_expression_p = true;
3663 tree wrap;
3664 if (VAR_P (decl)
3665 && !cp_unevaluated_operand
3666 && !processing_template_decl
3667 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl))
3668 && CP_DECL_THREAD_LOCAL_P (decl)
3669 && (wrap = get_tls_wrapper_fn (decl)))
3671 /* Replace an evaluated use of the thread_local variable with
3672 a call to its wrapper. */
3673 decl = build_cxx_call (wrap, 0, NULL, tf_warning_or_error);
3675 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3676 && !dependent_p
3677 && variable_template_p (TREE_OPERAND (decl, 0)))
3679 decl = finish_template_variable (decl);
3680 mark_used (decl);
3681 decl = convert_from_reference (decl);
3683 else if (scope)
3685 if (TREE_CODE (decl) == SCOPE_REF)
3687 gcc_assert (same_type_p (scope, TREE_OPERAND (decl, 0)));
3688 decl = TREE_OPERAND (decl, 1);
3691 decl = (adjust_result_of_qualified_name_lookup
3692 (decl, scope, current_nonlambda_class_type()));
3694 if (TREE_CODE (decl) == FUNCTION_DECL)
3695 mark_used (decl);
3697 if (TYPE_P (scope))
3698 decl = finish_qualified_id_expr (scope,
3699 decl,
3700 done,
3701 address_p,
3702 template_p,
3703 template_arg_p,
3704 tf_warning_or_error);
3705 else
3706 decl = convert_from_reference (decl);
3708 else if (TREE_CODE (decl) == FIELD_DECL)
3710 /* Since SCOPE is NULL here, this is an unqualified name.
3711 Access checking has been performed during name lookup
3712 already. Turn off checking to avoid duplicate errors. */
3713 push_deferring_access_checks (dk_no_check);
3714 decl = finish_non_static_data_member (decl, NULL_TREE,
3715 /*qualifying_scope=*/NULL_TREE);
3716 pop_deferring_access_checks ();
3718 else if (is_overloaded_fn (decl))
3720 tree first_fn = get_first_fn (decl);
3722 if (TREE_CODE (first_fn) == TEMPLATE_DECL)
3723 first_fn = DECL_TEMPLATE_RESULT (first_fn);
3725 /* [basic.def.odr]: "A function whose name appears as a
3726 potentially-evaluated expression is odr-used if it is the unique
3727 lookup result".
3729 But only mark it if it's a complete postfix-expression; in a call,
3730 ADL might select a different function, and we'll call mark_used in
3731 build_over_call. */
3732 if (done
3733 && !really_overloaded_fn (decl)
3734 && !mark_used (first_fn))
3735 return error_mark_node;
3737 if (!template_arg_p
3738 && TREE_CODE (first_fn) == FUNCTION_DECL
3739 && DECL_FUNCTION_MEMBER_P (first_fn)
3740 && !shared_member_p (decl))
3742 /* A set of member functions. */
3743 decl = maybe_dummy_object (DECL_CONTEXT (first_fn), 0);
3744 return finish_class_member_access_expr (decl, id_expression,
3745 /*template_p=*/false,
3746 tf_warning_or_error);
3749 decl = baselink_for_fns (decl);
3751 else
3753 if (DECL_P (decl) && DECL_NONLOCAL (decl)
3754 && DECL_CLASS_SCOPE_P (decl))
3756 tree context = context_for_name_lookup (decl);
3757 if (context != current_class_type)
3759 tree path = currently_open_derived_class (context);
3760 perform_or_defer_access_check (TYPE_BINFO (path),
3761 decl, decl,
3762 tf_warning_or_error);
3766 decl = convert_from_reference (decl);
3770 return cp_expr (decl, location);
3773 /* Implement the __typeof keyword: Return the type of EXPR, suitable for
3774 use as a type-specifier. */
3776 tree
3777 finish_typeof (tree expr)
3779 tree type;
3781 if (type_dependent_expression_p (expr))
3783 type = cxx_make_type (TYPEOF_TYPE);
3784 TYPEOF_TYPE_EXPR (type) = expr;
3785 SET_TYPE_STRUCTURAL_EQUALITY (type);
3787 return type;
3790 expr = mark_type_use (expr);
3792 type = unlowered_expr_type (expr);
3794 if (!type || type == unknown_type_node)
3796 error ("type of %qE is unknown", expr);
3797 return error_mark_node;
3800 return type;
3803 /* Implement the __underlying_type keyword: Return the underlying
3804 type of TYPE, suitable for use as a type-specifier. */
3806 tree
3807 finish_underlying_type (tree type)
3809 tree underlying_type;
3811 if (processing_template_decl)
3813 underlying_type = cxx_make_type (UNDERLYING_TYPE);
3814 UNDERLYING_TYPE_TYPE (underlying_type) = type;
3815 SET_TYPE_STRUCTURAL_EQUALITY (underlying_type);
3817 return underlying_type;
3820 if (!complete_type_or_else (type, NULL_TREE))
3821 return error_mark_node;
3823 if (TREE_CODE (type) != ENUMERAL_TYPE)
3825 error ("%qT is not an enumeration type", type);
3826 return error_mark_node;
3829 underlying_type = ENUM_UNDERLYING_TYPE (type);
3831 /* Fixup necessary in this case because ENUM_UNDERLYING_TYPE
3832 includes TYPE_MIN_VALUE and TYPE_MAX_VALUE information.
3833 See finish_enum_value_list for details. */
3834 if (!ENUM_FIXED_UNDERLYING_TYPE_P (type))
3835 underlying_type
3836 = c_common_type_for_mode (TYPE_MODE (underlying_type),
3837 TYPE_UNSIGNED (underlying_type));
3839 return underlying_type;
3842 /* Implement the __direct_bases keyword: Return the direct base classes
3843 of type */
3845 tree
3846 calculate_direct_bases (tree type)
3848 vec<tree, va_gc> *vector = make_tree_vector();
3849 tree bases_vec = NULL_TREE;
3850 vec<tree, va_gc> *base_binfos;
3851 tree binfo;
3852 unsigned i;
3854 complete_type (type);
3856 if (!NON_UNION_CLASS_TYPE_P (type))
3857 return make_tree_vec (0);
3859 base_binfos = BINFO_BASE_BINFOS (TYPE_BINFO (type));
3861 /* Virtual bases are initialized first */
3862 for (i = 0; base_binfos->iterate (i, &binfo); i++)
3864 if (BINFO_VIRTUAL_P (binfo))
3866 vec_safe_push (vector, binfo);
3870 /* Now non-virtuals */
3871 for (i = 0; base_binfos->iterate (i, &binfo); i++)
3873 if (!BINFO_VIRTUAL_P (binfo))
3875 vec_safe_push (vector, binfo);
3880 bases_vec = make_tree_vec (vector->length ());
3882 for (i = 0; i < vector->length (); ++i)
3884 TREE_VEC_ELT (bases_vec, i) = BINFO_TYPE ((*vector)[i]);
3886 return bases_vec;
3889 /* Implement the __bases keyword: Return the base classes
3890 of type */
3892 /* Find morally non-virtual base classes by walking binfo hierarchy */
3893 /* Virtual base classes are handled separately in finish_bases */
3895 static tree
3896 dfs_calculate_bases_pre (tree binfo, void * /*data_*/)
3898 /* Don't walk bases of virtual bases */
3899 return BINFO_VIRTUAL_P (binfo) ? dfs_skip_bases : NULL_TREE;
3902 static tree
3903 dfs_calculate_bases_post (tree binfo, void *data_)
3905 vec<tree, va_gc> **data = ((vec<tree, va_gc> **) data_);
3906 if (!BINFO_VIRTUAL_P (binfo))
3908 vec_safe_push (*data, BINFO_TYPE (binfo));
3910 return NULL_TREE;
3913 /* Calculates the morally non-virtual base classes of a class */
3914 static vec<tree, va_gc> *
3915 calculate_bases_helper (tree type)
3917 vec<tree, va_gc> *vector = make_tree_vector();
3919 /* Now add non-virtual base classes in order of construction */
3920 if (TYPE_BINFO (type))
3921 dfs_walk_all (TYPE_BINFO (type),
3922 dfs_calculate_bases_pre, dfs_calculate_bases_post, &vector);
3923 return vector;
3926 tree
3927 calculate_bases (tree type)
3929 vec<tree, va_gc> *vector = make_tree_vector();
3930 tree bases_vec = NULL_TREE;
3931 unsigned i;
3932 vec<tree, va_gc> *vbases;
3933 vec<tree, va_gc> *nonvbases;
3934 tree binfo;
3936 complete_type (type);
3938 if (!NON_UNION_CLASS_TYPE_P (type))
3939 return make_tree_vec (0);
3941 /* First go through virtual base classes */
3942 for (vbases = CLASSTYPE_VBASECLASSES (type), i = 0;
3943 vec_safe_iterate (vbases, i, &binfo); i++)
3945 vec<tree, va_gc> *vbase_bases;
3946 vbase_bases = calculate_bases_helper (BINFO_TYPE (binfo));
3947 vec_safe_splice (vector, vbase_bases);
3948 release_tree_vector (vbase_bases);
3951 /* Now for the non-virtual bases */
3952 nonvbases = calculate_bases_helper (type);
3953 vec_safe_splice (vector, nonvbases);
3954 release_tree_vector (nonvbases);
3956 /* Note that during error recovery vector->length can even be zero. */
3957 if (vector->length () > 1)
3959 /* Last element is entire class, so don't copy */
3960 bases_vec = make_tree_vec (vector->length() - 1);
3962 for (i = 0; i < vector->length () - 1; ++i)
3963 TREE_VEC_ELT (bases_vec, i) = (*vector)[i];
3965 else
3966 bases_vec = make_tree_vec (0);
3968 release_tree_vector (vector);
3969 return bases_vec;
3972 tree
3973 finish_bases (tree type, bool direct)
3975 tree bases = NULL_TREE;
3977 if (!processing_template_decl)
3979 /* Parameter packs can only be used in templates */
3980 error ("Parameter pack __bases only valid in template declaration");
3981 return error_mark_node;
3984 bases = cxx_make_type (BASES);
3985 BASES_TYPE (bases) = type;
3986 BASES_DIRECT (bases) = direct;
3987 SET_TYPE_STRUCTURAL_EQUALITY (bases);
3989 return bases;
3992 /* Perform C++-specific checks for __builtin_offsetof before calling
3993 fold_offsetof. */
3995 tree
3996 finish_offsetof (tree object_ptr, tree expr, location_t loc)
3998 /* If we're processing a template, we can't finish the semantics yet.
3999 Otherwise we can fold the entire expression now. */
4000 if (processing_template_decl)
4002 expr = build2 (OFFSETOF_EXPR, size_type_node, expr, object_ptr);
4003 SET_EXPR_LOCATION (expr, loc);
4004 return expr;
4007 if (TREE_CODE (expr) == PSEUDO_DTOR_EXPR)
4009 error ("cannot apply %<offsetof%> to destructor %<~%T%>",
4010 TREE_OPERAND (expr, 2));
4011 return error_mark_node;
4013 if (TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE
4014 || TREE_CODE (TREE_TYPE (expr)) == METHOD_TYPE
4015 || TREE_TYPE (expr) == unknown_type_node)
4017 if (INDIRECT_REF_P (expr))
4018 error ("second operand of %<offsetof%> is neither a single "
4019 "identifier nor a sequence of member accesses and "
4020 "array references");
4021 else
4023 if (TREE_CODE (expr) == COMPONENT_REF
4024 || TREE_CODE (expr) == COMPOUND_EXPR)
4025 expr = TREE_OPERAND (expr, 1);
4026 error ("cannot apply %<offsetof%> to member function %qD", expr);
4028 return error_mark_node;
4030 if (REFERENCE_REF_P (expr))
4031 expr = TREE_OPERAND (expr, 0);
4032 if (!complete_type_or_else (TREE_TYPE (TREE_TYPE (object_ptr)), object_ptr))
4033 return error_mark_node;
4034 if (warn_invalid_offsetof
4035 && CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (object_ptr)))
4036 && CLASSTYPE_NON_STD_LAYOUT (TREE_TYPE (TREE_TYPE (object_ptr)))
4037 && cp_unevaluated_operand == 0)
4038 pedwarn (loc, OPT_Winvalid_offsetof,
4039 "offsetof within non-standard-layout type %qT is undefined",
4040 TREE_TYPE (TREE_TYPE (object_ptr)));
4041 return fold_offsetof (expr);
4044 /* Replace the AGGR_INIT_EXPR at *TP with an equivalent CALL_EXPR. This
4045 function is broken out from the above for the benefit of the tree-ssa
4046 project. */
4048 void
4049 simplify_aggr_init_expr (tree *tp)
4051 tree aggr_init_expr = *tp;
4053 /* Form an appropriate CALL_EXPR. */
4054 tree fn = AGGR_INIT_EXPR_FN (aggr_init_expr);
4055 tree slot = AGGR_INIT_EXPR_SLOT (aggr_init_expr);
4056 tree type = TREE_TYPE (slot);
4058 tree call_expr;
4059 enum style_t { ctor, arg, pcc } style;
4061 if (AGGR_INIT_VIA_CTOR_P (aggr_init_expr))
4062 style = ctor;
4063 #ifdef PCC_STATIC_STRUCT_RETURN
4064 else if (1)
4065 style = pcc;
4066 #endif
4067 else
4069 gcc_assert (TREE_ADDRESSABLE (type));
4070 style = arg;
4073 call_expr = build_call_array_loc (input_location,
4074 TREE_TYPE (TREE_TYPE (TREE_TYPE (fn))),
4076 aggr_init_expr_nargs (aggr_init_expr),
4077 AGGR_INIT_EXPR_ARGP (aggr_init_expr));
4078 TREE_NOTHROW (call_expr) = TREE_NOTHROW (aggr_init_expr);
4079 CALL_FROM_THUNK_P (call_expr) = AGGR_INIT_FROM_THUNK_P (aggr_init_expr);
4080 CALL_EXPR_OPERATOR_SYNTAX (call_expr)
4081 = CALL_EXPR_OPERATOR_SYNTAX (aggr_init_expr);
4082 CALL_EXPR_ORDERED_ARGS (call_expr) = CALL_EXPR_ORDERED_ARGS (aggr_init_expr);
4083 CALL_EXPR_REVERSE_ARGS (call_expr) = CALL_EXPR_REVERSE_ARGS (aggr_init_expr);
4085 if (style == ctor)
4087 /* Replace the first argument to the ctor with the address of the
4088 slot. */
4089 cxx_mark_addressable (slot);
4090 CALL_EXPR_ARG (call_expr, 0) =
4091 build1 (ADDR_EXPR, build_pointer_type (type), slot);
4093 else if (style == arg)
4095 /* Just mark it addressable here, and leave the rest to
4096 expand_call{,_inline}. */
4097 cxx_mark_addressable (slot);
4098 CALL_EXPR_RETURN_SLOT_OPT (call_expr) = true;
4099 call_expr = build2 (INIT_EXPR, TREE_TYPE (call_expr), slot, call_expr);
4101 else if (style == pcc)
4103 /* If we're using the non-reentrant PCC calling convention, then we
4104 need to copy the returned value out of the static buffer into the
4105 SLOT. */
4106 push_deferring_access_checks (dk_no_check);
4107 call_expr = build_aggr_init (slot, call_expr,
4108 DIRECT_BIND | LOOKUP_ONLYCONVERTING,
4109 tf_warning_or_error);
4110 pop_deferring_access_checks ();
4111 call_expr = build2 (COMPOUND_EXPR, TREE_TYPE (slot), call_expr, slot);
4114 if (AGGR_INIT_ZERO_FIRST (aggr_init_expr))
4116 tree init = build_zero_init (type, NULL_TREE,
4117 /*static_storage_p=*/false);
4118 init = build2 (INIT_EXPR, void_type_node, slot, init);
4119 call_expr = build2 (COMPOUND_EXPR, TREE_TYPE (call_expr),
4120 init, call_expr);
4123 *tp = call_expr;
4126 /* Emit all thunks to FN that should be emitted when FN is emitted. */
4128 void
4129 emit_associated_thunks (tree fn)
4131 /* When we use vcall offsets, we emit thunks with the virtual
4132 functions to which they thunk. The whole point of vcall offsets
4133 is so that you can know statically the entire set of thunks that
4134 will ever be needed for a given virtual function, thereby
4135 enabling you to output all the thunks with the function itself. */
4136 if (DECL_VIRTUAL_P (fn)
4137 /* Do not emit thunks for extern template instantiations. */
4138 && ! DECL_REALLY_EXTERN (fn))
4140 tree thunk;
4142 for (thunk = DECL_THUNKS (fn); thunk; thunk = DECL_CHAIN (thunk))
4144 if (!THUNK_ALIAS (thunk))
4146 use_thunk (thunk, /*emit_p=*/1);
4147 if (DECL_RESULT_THUNK_P (thunk))
4149 tree probe;
4151 for (probe = DECL_THUNKS (thunk);
4152 probe; probe = DECL_CHAIN (probe))
4153 use_thunk (probe, /*emit_p=*/1);
4156 else
4157 gcc_assert (!DECL_THUNKS (thunk));
4162 /* Generate RTL for FN. */
4164 bool
4165 expand_or_defer_fn_1 (tree fn)
4167 /* When the parser calls us after finishing the body of a template
4168 function, we don't really want to expand the body. */
4169 if (processing_template_decl)
4171 /* Normally, collection only occurs in rest_of_compilation. So,
4172 if we don't collect here, we never collect junk generated
4173 during the processing of templates until we hit a
4174 non-template function. It's not safe to do this inside a
4175 nested class, though, as the parser may have local state that
4176 is not a GC root. */
4177 if (!function_depth)
4178 ggc_collect ();
4179 return false;
4182 gcc_assert (DECL_SAVED_TREE (fn));
4184 /* We make a decision about linkage for these functions at the end
4185 of the compilation. Until that point, we do not want the back
4186 end to output them -- but we do want it to see the bodies of
4187 these functions so that it can inline them as appropriate. */
4188 if (DECL_DECLARED_INLINE_P (fn) || DECL_IMPLICIT_INSTANTIATION (fn))
4190 if (DECL_INTERFACE_KNOWN (fn))
4191 /* We've already made a decision as to how this function will
4192 be handled. */;
4193 else if (!at_eof)
4194 tentative_decl_linkage (fn);
4195 else
4196 import_export_decl (fn);
4198 /* If the user wants us to keep all inline functions, then mark
4199 this function as needed so that finish_file will make sure to
4200 output it later. Similarly, all dllexport'd functions must
4201 be emitted; there may be callers in other DLLs. */
4202 if (DECL_DECLARED_INLINE_P (fn)
4203 && !DECL_REALLY_EXTERN (fn)
4204 && (flag_keep_inline_functions
4205 || (flag_keep_inline_dllexport
4206 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (fn)))))
4208 mark_needed (fn);
4209 DECL_EXTERNAL (fn) = 0;
4213 /* If this is a constructor or destructor body, we have to clone
4214 it. */
4215 if (maybe_clone_body (fn))
4217 /* We don't want to process FN again, so pretend we've written
4218 it out, even though we haven't. */
4219 TREE_ASM_WRITTEN (fn) = 1;
4220 /* If this is a constexpr function, keep DECL_SAVED_TREE. */
4221 if (!DECL_DECLARED_CONSTEXPR_P (fn))
4222 DECL_SAVED_TREE (fn) = NULL_TREE;
4223 return false;
4226 /* There's no reason to do any of the work here if we're only doing
4227 semantic analysis; this code just generates RTL. */
4228 if (flag_syntax_only)
4229 return false;
4231 return true;
4234 void
4235 expand_or_defer_fn (tree fn)
4237 if (expand_or_defer_fn_1 (fn))
4239 function_depth++;
4241 /* Expand or defer, at the whim of the compilation unit manager. */
4242 cgraph_node::finalize_function (fn, function_depth > 1);
4243 emit_associated_thunks (fn);
4245 function_depth--;
4249 struct nrv_data
4251 nrv_data () : visited (37) {}
4253 tree var;
4254 tree result;
4255 hash_table<nofree_ptr_hash <tree_node> > visited;
4258 /* Helper function for walk_tree, used by finalize_nrv below. */
4260 static tree
4261 finalize_nrv_r (tree* tp, int* walk_subtrees, void* data)
4263 struct nrv_data *dp = (struct nrv_data *)data;
4264 tree_node **slot;
4266 /* No need to walk into types. There wouldn't be any need to walk into
4267 non-statements, except that we have to consider STMT_EXPRs. */
4268 if (TYPE_P (*tp))
4269 *walk_subtrees = 0;
4270 /* Change all returns to just refer to the RESULT_DECL; this is a nop,
4271 but differs from using NULL_TREE in that it indicates that we care
4272 about the value of the RESULT_DECL. */
4273 else if (TREE_CODE (*tp) == RETURN_EXPR)
4274 TREE_OPERAND (*tp, 0) = dp->result;
4275 /* Change all cleanups for the NRV to only run when an exception is
4276 thrown. */
4277 else if (TREE_CODE (*tp) == CLEANUP_STMT
4278 && CLEANUP_DECL (*tp) == dp->var)
4279 CLEANUP_EH_ONLY (*tp) = 1;
4280 /* Replace the DECL_EXPR for the NRV with an initialization of the
4281 RESULT_DECL, if needed. */
4282 else if (TREE_CODE (*tp) == DECL_EXPR
4283 && DECL_EXPR_DECL (*tp) == dp->var)
4285 tree init;
4286 if (DECL_INITIAL (dp->var)
4287 && DECL_INITIAL (dp->var) != error_mark_node)
4288 init = build2 (INIT_EXPR, void_type_node, dp->result,
4289 DECL_INITIAL (dp->var));
4290 else
4291 init = build_empty_stmt (EXPR_LOCATION (*tp));
4292 DECL_INITIAL (dp->var) = NULL_TREE;
4293 SET_EXPR_LOCATION (init, EXPR_LOCATION (*tp));
4294 *tp = init;
4296 /* And replace all uses of the NRV with the RESULT_DECL. */
4297 else if (*tp == dp->var)
4298 *tp = dp->result;
4300 /* Avoid walking into the same tree more than once. Unfortunately, we
4301 can't just use walk_tree_without duplicates because it would only call
4302 us for the first occurrence of dp->var in the function body. */
4303 slot = dp->visited.find_slot (*tp, INSERT);
4304 if (*slot)
4305 *walk_subtrees = 0;
4306 else
4307 *slot = *tp;
4309 /* Keep iterating. */
4310 return NULL_TREE;
4313 /* Called from finish_function to implement the named return value
4314 optimization by overriding all the RETURN_EXPRs and pertinent
4315 CLEANUP_STMTs and replacing all occurrences of VAR with RESULT, the
4316 RESULT_DECL for the function. */
4318 void
4319 finalize_nrv (tree *tp, tree var, tree result)
4321 struct nrv_data data;
4323 /* Copy name from VAR to RESULT. */
4324 DECL_NAME (result) = DECL_NAME (var);
4325 /* Don't forget that we take its address. */
4326 TREE_ADDRESSABLE (result) = TREE_ADDRESSABLE (var);
4327 /* Finally set DECL_VALUE_EXPR to avoid assigning
4328 a stack slot at -O0 for the original var and debug info
4329 uses RESULT location for VAR. */
4330 SET_DECL_VALUE_EXPR (var, result);
4331 DECL_HAS_VALUE_EXPR_P (var) = 1;
4333 data.var = var;
4334 data.result = result;
4335 cp_walk_tree (tp, finalize_nrv_r, &data, 0);
4338 /* Create CP_OMP_CLAUSE_INFO for clause C. Returns true if it is invalid. */
4340 bool
4341 cxx_omp_create_clause_info (tree c, tree type, bool need_default_ctor,
4342 bool need_copy_ctor, bool need_copy_assignment,
4343 bool need_dtor)
4345 int save_errorcount = errorcount;
4346 tree info, t;
4348 /* Always allocate 3 elements for simplicity. These are the
4349 function decls for the ctor, dtor, and assignment op.
4350 This layout is known to the three lang hooks,
4351 cxx_omp_clause_default_init, cxx_omp_clause_copy_init,
4352 and cxx_omp_clause_assign_op. */
4353 info = make_tree_vec (3);
4354 CP_OMP_CLAUSE_INFO (c) = info;
4356 if (need_default_ctor || need_copy_ctor)
4358 if (need_default_ctor)
4359 t = get_default_ctor (type);
4360 else
4361 t = get_copy_ctor (type, tf_warning_or_error);
4363 if (t && !trivial_fn_p (t))
4364 TREE_VEC_ELT (info, 0) = t;
4367 if (need_dtor && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
4368 TREE_VEC_ELT (info, 1) = get_dtor (type, tf_warning_or_error);
4370 if (need_copy_assignment)
4372 t = get_copy_assign (type);
4374 if (t && !trivial_fn_p (t))
4375 TREE_VEC_ELT (info, 2) = t;
4378 return errorcount != save_errorcount;
4381 /* If DECL is DECL_OMP_PRIVATIZED_MEMBER, return corresponding
4382 FIELD_DECL, otherwise return DECL itself. */
4384 static tree
4385 omp_clause_decl_field (tree decl)
4387 if (VAR_P (decl)
4388 && DECL_HAS_VALUE_EXPR_P (decl)
4389 && DECL_ARTIFICIAL (decl)
4390 && DECL_LANG_SPECIFIC (decl)
4391 && DECL_OMP_PRIVATIZED_MEMBER (decl))
4393 tree f = DECL_VALUE_EXPR (decl);
4394 if (INDIRECT_REF_P (f))
4395 f = TREE_OPERAND (f, 0);
4396 if (TREE_CODE (f) == COMPONENT_REF)
4398 f = TREE_OPERAND (f, 1);
4399 gcc_assert (TREE_CODE (f) == FIELD_DECL);
4400 return f;
4403 return NULL_TREE;
4406 /* Adjust DECL if needed for printing using %qE. */
4408 static tree
4409 omp_clause_printable_decl (tree decl)
4411 tree t = omp_clause_decl_field (decl);
4412 if (t)
4413 return t;
4414 return decl;
4417 /* For a FIELD_DECL F and corresponding DECL_OMP_PRIVATIZED_MEMBER
4418 VAR_DECL T that doesn't need a DECL_EXPR added, record it for
4419 privatization. */
4421 static void
4422 omp_note_field_privatization (tree f, tree t)
4424 if (!omp_private_member_map)
4425 omp_private_member_map = new hash_map<tree, tree>;
4426 tree &v = omp_private_member_map->get_or_insert (f);
4427 if (v == NULL_TREE)
4429 v = t;
4430 omp_private_member_vec.safe_push (f);
4431 /* Signal that we don't want to create DECL_EXPR for this dummy var. */
4432 omp_private_member_vec.safe_push (integer_zero_node);
4436 /* Privatize FIELD_DECL T, return corresponding DECL_OMP_PRIVATIZED_MEMBER
4437 dummy VAR_DECL. */
4439 tree
4440 omp_privatize_field (tree t, bool shared)
4442 tree m = finish_non_static_data_member (t, NULL_TREE, NULL_TREE);
4443 if (m == error_mark_node)
4444 return error_mark_node;
4445 if (!omp_private_member_map && !shared)
4446 omp_private_member_map = new hash_map<tree, tree>;
4447 if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE)
4449 gcc_assert (INDIRECT_REF_P (m));
4450 m = TREE_OPERAND (m, 0);
4452 tree vb = NULL_TREE;
4453 tree &v = shared ? vb : omp_private_member_map->get_or_insert (t);
4454 if (v == NULL_TREE)
4456 v = create_temporary_var (TREE_TYPE (m));
4457 retrofit_lang_decl (v);
4458 DECL_OMP_PRIVATIZED_MEMBER (v) = 1;
4459 SET_DECL_VALUE_EXPR (v, m);
4460 DECL_HAS_VALUE_EXPR_P (v) = 1;
4461 if (!shared)
4462 omp_private_member_vec.safe_push (t);
4464 return v;
4467 /* Helper function for handle_omp_array_sections. Called recursively
4468 to handle multiple array-section-subscripts. C is the clause,
4469 T current expression (initially OMP_CLAUSE_DECL), which is either
4470 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
4471 expression if specified, TREE_VALUE length expression if specified,
4472 TREE_CHAIN is what it has been specified after, or some decl.
4473 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
4474 set to true if any of the array-section-subscript could have length
4475 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
4476 first array-section-subscript which is known not to have length
4477 of one. Given say:
4478 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
4479 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
4480 all are or may have length of 1, array-section-subscript [:2] is the
4481 first one known not to have length 1. For array-section-subscript
4482 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
4483 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
4484 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
4485 case though, as some lengths could be zero. */
4487 static tree
4488 handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
4489 bool &maybe_zero_len, unsigned int &first_non_one,
4490 enum c_omp_region_type ort)
4492 tree ret, low_bound, length, type;
4493 if (TREE_CODE (t) != TREE_LIST)
4495 if (error_operand_p (t))
4496 return error_mark_node;
4497 if (REFERENCE_REF_P (t)
4498 && TREE_CODE (TREE_OPERAND (t, 0)) == COMPONENT_REF)
4499 t = TREE_OPERAND (t, 0);
4500 ret = t;
4501 if (TREE_CODE (t) == COMPONENT_REF
4502 && ort == C_ORT_OMP
4503 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
4504 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO
4505 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FROM)
4506 && !type_dependent_expression_p (t))
4508 if (TREE_CODE (TREE_OPERAND (t, 1)) == FIELD_DECL
4509 && DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
4511 error_at (OMP_CLAUSE_LOCATION (c),
4512 "bit-field %qE in %qs clause",
4513 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4514 return error_mark_node;
4516 while (TREE_CODE (t) == COMPONENT_REF)
4518 if (TREE_TYPE (TREE_OPERAND (t, 0))
4519 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == UNION_TYPE)
4521 error_at (OMP_CLAUSE_LOCATION (c),
4522 "%qE is a member of a union", t);
4523 return error_mark_node;
4525 t = TREE_OPERAND (t, 0);
4527 if (REFERENCE_REF_P (t))
4528 t = TREE_OPERAND (t, 0);
4530 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
4532 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
4533 return NULL_TREE;
4534 if (DECL_P (t))
4535 error_at (OMP_CLAUSE_LOCATION (c),
4536 "%qD is not a variable in %qs clause", t,
4537 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4538 else
4539 error_at (OMP_CLAUSE_LOCATION (c),
4540 "%qE is not a variable in %qs clause", t,
4541 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4542 return error_mark_node;
4544 else if (TREE_CODE (t) == PARM_DECL
4545 && DECL_ARTIFICIAL (t)
4546 && DECL_NAME (t) == this_identifier)
4548 error_at (OMP_CLAUSE_LOCATION (c),
4549 "%<this%> allowed in OpenMP only in %<declare simd%>"
4550 " clauses");
4551 return error_mark_node;
4553 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
4554 && VAR_P (t) && CP_DECL_THREAD_LOCAL_P (t))
4556 error_at (OMP_CLAUSE_LOCATION (c),
4557 "%qD is threadprivate variable in %qs clause", t,
4558 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4559 return error_mark_node;
4561 if (type_dependent_expression_p (ret))
4562 return NULL_TREE;
4563 ret = convert_from_reference (ret);
4564 return ret;
4567 if (ort == C_ORT_OMP
4568 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
4569 && TREE_CODE (TREE_CHAIN (t)) == FIELD_DECL)
4570 TREE_CHAIN (t) = omp_privatize_field (TREE_CHAIN (t), false);
4571 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
4572 maybe_zero_len, first_non_one, ort);
4573 if (ret == error_mark_node || ret == NULL_TREE)
4574 return ret;
4576 type = TREE_TYPE (ret);
4577 low_bound = TREE_PURPOSE (t);
4578 length = TREE_VALUE (t);
4579 if ((low_bound && type_dependent_expression_p (low_bound))
4580 || (length && type_dependent_expression_p (length)))
4581 return NULL_TREE;
4583 if (low_bound == error_mark_node || length == error_mark_node)
4584 return error_mark_node;
4586 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
4588 error_at (OMP_CLAUSE_LOCATION (c),
4589 "low bound %qE of array section does not have integral type",
4590 low_bound);
4591 return error_mark_node;
4593 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
4595 error_at (OMP_CLAUSE_LOCATION (c),
4596 "length %qE of array section does not have integral type",
4597 length);
4598 return error_mark_node;
4600 if (low_bound)
4601 low_bound = mark_rvalue_use (low_bound);
4602 if (length)
4603 length = mark_rvalue_use (length);
4604 /* We need to reduce to real constant-values for checks below. */
4605 if (length)
4606 length = fold_simple (length);
4607 if (low_bound)
4608 low_bound = fold_simple (low_bound);
4609 if (low_bound
4610 && TREE_CODE (low_bound) == INTEGER_CST
4611 && TYPE_PRECISION (TREE_TYPE (low_bound))
4612 > TYPE_PRECISION (sizetype))
4613 low_bound = fold_convert (sizetype, low_bound);
4614 if (length
4615 && TREE_CODE (length) == INTEGER_CST
4616 && TYPE_PRECISION (TREE_TYPE (length))
4617 > TYPE_PRECISION (sizetype))
4618 length = fold_convert (sizetype, length);
4619 if (low_bound == NULL_TREE)
4620 low_bound = integer_zero_node;
4622 if (length != NULL_TREE)
4624 if (!integer_nonzerop (length))
4626 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
4627 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
4629 if (integer_zerop (length))
4631 error_at (OMP_CLAUSE_LOCATION (c),
4632 "zero length array section in %qs clause",
4633 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4634 return error_mark_node;
4637 else
4638 maybe_zero_len = true;
4640 if (first_non_one == types.length ()
4641 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
4642 first_non_one++;
4644 if (TREE_CODE (type) == ARRAY_TYPE)
4646 if (length == NULL_TREE
4647 && (TYPE_DOMAIN (type) == NULL_TREE
4648 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
4650 error_at (OMP_CLAUSE_LOCATION (c),
4651 "for unknown bound array type length expression must "
4652 "be specified");
4653 return error_mark_node;
4655 if (TREE_CODE (low_bound) == INTEGER_CST
4656 && tree_int_cst_sgn (low_bound) == -1)
4658 error_at (OMP_CLAUSE_LOCATION (c),
4659 "negative low bound in array section in %qs clause",
4660 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4661 return error_mark_node;
4663 if (length != NULL_TREE
4664 && TREE_CODE (length) == INTEGER_CST
4665 && tree_int_cst_sgn (length) == -1)
4667 error_at (OMP_CLAUSE_LOCATION (c),
4668 "negative length in array section in %qs clause",
4669 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4670 return error_mark_node;
4672 if (TYPE_DOMAIN (type)
4673 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
4674 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
4675 == INTEGER_CST)
4677 tree size
4678 = fold_convert (sizetype, TYPE_MAX_VALUE (TYPE_DOMAIN (type)));
4679 size = size_binop (PLUS_EXPR, size, size_one_node);
4680 if (TREE_CODE (low_bound) == INTEGER_CST)
4682 if (tree_int_cst_lt (size, low_bound))
4684 error_at (OMP_CLAUSE_LOCATION (c),
4685 "low bound %qE above array section size "
4686 "in %qs clause", low_bound,
4687 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4688 return error_mark_node;
4690 if (tree_int_cst_equal (size, low_bound))
4692 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
4693 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
4695 error_at (OMP_CLAUSE_LOCATION (c),
4696 "zero length array section in %qs clause",
4697 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4698 return error_mark_node;
4700 maybe_zero_len = true;
4702 else if (length == NULL_TREE
4703 && first_non_one == types.length ()
4704 && tree_int_cst_equal
4705 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
4706 low_bound))
4707 first_non_one++;
4709 else if (length == NULL_TREE)
4711 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
4712 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION)
4713 maybe_zero_len = true;
4714 if (first_non_one == types.length ())
4715 first_non_one++;
4717 if (length && TREE_CODE (length) == INTEGER_CST)
4719 if (tree_int_cst_lt (size, length))
4721 error_at (OMP_CLAUSE_LOCATION (c),
4722 "length %qE above array section size "
4723 "in %qs clause", length,
4724 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4725 return error_mark_node;
4727 if (TREE_CODE (low_bound) == INTEGER_CST)
4729 tree lbpluslen
4730 = size_binop (PLUS_EXPR,
4731 fold_convert (sizetype, low_bound),
4732 fold_convert (sizetype, length));
4733 if (TREE_CODE (lbpluslen) == INTEGER_CST
4734 && tree_int_cst_lt (size, lbpluslen))
4736 error_at (OMP_CLAUSE_LOCATION (c),
4737 "high bound %qE above array section size "
4738 "in %qs clause", lbpluslen,
4739 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4740 return error_mark_node;
4745 else if (length == NULL_TREE)
4747 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
4748 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION)
4749 maybe_zero_len = true;
4750 if (first_non_one == types.length ())
4751 first_non_one++;
4754 /* For [lb:] we will need to evaluate lb more than once. */
4755 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
4757 tree lb = cp_save_expr (low_bound);
4758 if (lb != low_bound)
4760 TREE_PURPOSE (t) = lb;
4761 low_bound = lb;
4765 else if (TREE_CODE (type) == POINTER_TYPE)
4767 if (length == NULL_TREE)
4769 error_at (OMP_CLAUSE_LOCATION (c),
4770 "for pointer type length expression must be specified");
4771 return error_mark_node;
4773 if (length != NULL_TREE
4774 && TREE_CODE (length) == INTEGER_CST
4775 && tree_int_cst_sgn (length) == -1)
4777 error_at (OMP_CLAUSE_LOCATION (c),
4778 "negative length in array section in %qs clause",
4779 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4780 return error_mark_node;
4782 /* If there is a pointer type anywhere but in the very first
4783 array-section-subscript, the array section can't be contiguous. */
4784 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
4785 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
4787 error_at (OMP_CLAUSE_LOCATION (c),
4788 "array section is not contiguous in %qs clause",
4789 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4790 return error_mark_node;
4793 else
4795 error_at (OMP_CLAUSE_LOCATION (c),
4796 "%qE does not have pointer or array type", ret);
4797 return error_mark_node;
4799 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
4800 types.safe_push (TREE_TYPE (ret));
4801 /* We will need to evaluate lb more than once. */
4802 tree lb = cp_save_expr (low_bound);
4803 if (lb != low_bound)
4805 TREE_PURPOSE (t) = lb;
4806 low_bound = lb;
4808 ret = grok_array_decl (OMP_CLAUSE_LOCATION (c), ret, low_bound, false);
4809 return ret;
4812 /* Handle array sections for clause C. */
4814 static bool
4815 handle_omp_array_sections (tree c, enum c_omp_region_type ort)
4817 bool maybe_zero_len = false;
4818 unsigned int first_non_one = 0;
4819 auto_vec<tree, 10> types;
4820 tree first = handle_omp_array_sections_1 (c, OMP_CLAUSE_DECL (c), types,
4821 maybe_zero_len, first_non_one,
4822 ort);
4823 if (first == error_mark_node)
4824 return true;
4825 if (first == NULL_TREE)
4826 return false;
4827 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
4829 tree t = OMP_CLAUSE_DECL (c);
4830 tree tem = NULL_TREE;
4831 if (processing_template_decl)
4832 return false;
4833 /* Need to evaluate side effects in the length expressions
4834 if any. */
4835 while (TREE_CODE (t) == TREE_LIST)
4837 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
4839 if (tem == NULL_TREE)
4840 tem = TREE_VALUE (t);
4841 else
4842 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
4843 TREE_VALUE (t), tem);
4845 t = TREE_CHAIN (t);
4847 if (tem)
4848 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
4849 OMP_CLAUSE_DECL (c) = first;
4851 else
4853 unsigned int num = types.length (), i;
4854 tree t, side_effects = NULL_TREE, size = NULL_TREE;
4855 tree condition = NULL_TREE;
4857 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
4858 maybe_zero_len = true;
4859 if (processing_template_decl && maybe_zero_len)
4860 return false;
4862 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
4863 t = TREE_CHAIN (t))
4865 tree low_bound = TREE_PURPOSE (t);
4866 tree length = TREE_VALUE (t);
4868 i--;
4869 if (low_bound
4870 && TREE_CODE (low_bound) == INTEGER_CST
4871 && TYPE_PRECISION (TREE_TYPE (low_bound))
4872 > TYPE_PRECISION (sizetype))
4873 low_bound = fold_convert (sizetype, low_bound);
4874 if (length
4875 && TREE_CODE (length) == INTEGER_CST
4876 && TYPE_PRECISION (TREE_TYPE (length))
4877 > TYPE_PRECISION (sizetype))
4878 length = fold_convert (sizetype, length);
4879 if (low_bound == NULL_TREE)
4880 low_bound = integer_zero_node;
4881 if (!maybe_zero_len && i > first_non_one)
4883 if (integer_nonzerop (low_bound))
4884 goto do_warn_noncontiguous;
4885 if (length != NULL_TREE
4886 && TREE_CODE (length) == INTEGER_CST
4887 && TYPE_DOMAIN (types[i])
4888 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
4889 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
4890 == INTEGER_CST)
4892 tree size;
4893 size = size_binop (PLUS_EXPR,
4894 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
4895 size_one_node);
4896 if (!tree_int_cst_equal (length, size))
4898 do_warn_noncontiguous:
4899 error_at (OMP_CLAUSE_LOCATION (c),
4900 "array section is not contiguous in %qs "
4901 "clause",
4902 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4903 return true;
4906 if (!processing_template_decl
4907 && length != NULL_TREE
4908 && TREE_SIDE_EFFECTS (length))
4910 if (side_effects == NULL_TREE)
4911 side_effects = length;
4912 else
4913 side_effects = build2 (COMPOUND_EXPR,
4914 TREE_TYPE (side_effects),
4915 length, side_effects);
4918 else if (processing_template_decl)
4919 continue;
4920 else
4922 tree l;
4924 if (i > first_non_one
4925 && ((length && integer_nonzerop (length))
4926 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION))
4927 continue;
4928 if (length)
4929 l = fold_convert (sizetype, length);
4930 else
4932 l = size_binop (PLUS_EXPR,
4933 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
4934 size_one_node);
4935 l = size_binop (MINUS_EXPR, l,
4936 fold_convert (sizetype, low_bound));
4938 if (i > first_non_one)
4940 l = fold_build2 (NE_EXPR, boolean_type_node, l,
4941 size_zero_node);
4942 if (condition == NULL_TREE)
4943 condition = l;
4944 else
4945 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
4946 l, condition);
4948 else if (size == NULL_TREE)
4950 size = size_in_bytes (TREE_TYPE (types[i]));
4951 tree eltype = TREE_TYPE (types[num - 1]);
4952 while (TREE_CODE (eltype) == ARRAY_TYPE)
4953 eltype = TREE_TYPE (eltype);
4954 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
4955 size = size_binop (EXACT_DIV_EXPR, size,
4956 size_in_bytes (eltype));
4957 size = size_binop (MULT_EXPR, size, l);
4958 if (condition)
4959 size = fold_build3 (COND_EXPR, sizetype, condition,
4960 size, size_zero_node);
4962 else
4963 size = size_binop (MULT_EXPR, size, l);
4966 if (!processing_template_decl)
4968 if (side_effects)
4969 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
4970 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
4972 size = size_binop (MINUS_EXPR, size, size_one_node);
4973 tree index_type = build_index_type (size);
4974 tree eltype = TREE_TYPE (first);
4975 while (TREE_CODE (eltype) == ARRAY_TYPE)
4976 eltype = TREE_TYPE (eltype);
4977 tree type = build_array_type (eltype, index_type);
4978 tree ptype = build_pointer_type (eltype);
4979 if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE
4980 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (t))))
4981 t = convert_from_reference (t);
4982 else if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
4983 t = build_fold_addr_expr (t);
4984 tree t2 = build_fold_addr_expr (first);
4985 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
4986 ptrdiff_type_node, t2);
4987 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
4988 ptrdiff_type_node, t2,
4989 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
4990 ptrdiff_type_node, t));
4991 if (tree_fits_shwi_p (t2))
4992 t = build2 (MEM_REF, type, t,
4993 build_int_cst (ptype, tree_to_shwi (t2)));
4994 else
4996 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
4997 sizetype, t2);
4998 t = build2_loc (OMP_CLAUSE_LOCATION (c), POINTER_PLUS_EXPR,
4999 TREE_TYPE (t), t, t2);
5000 t = build2 (MEM_REF, type, t, build_int_cst (ptype, 0));
5002 OMP_CLAUSE_DECL (c) = t;
5003 return false;
5005 OMP_CLAUSE_DECL (c) = first;
5006 OMP_CLAUSE_SIZE (c) = size;
5007 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
5008 || (TREE_CODE (t) == COMPONENT_REF
5009 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE))
5010 return false;
5011 if (ort == C_ORT_OMP || ort == C_ORT_ACC)
5012 switch (OMP_CLAUSE_MAP_KIND (c))
5014 case GOMP_MAP_ALLOC:
5015 case GOMP_MAP_TO:
5016 case GOMP_MAP_FROM:
5017 case GOMP_MAP_TOFROM:
5018 case GOMP_MAP_ALWAYS_TO:
5019 case GOMP_MAP_ALWAYS_FROM:
5020 case GOMP_MAP_ALWAYS_TOFROM:
5021 case GOMP_MAP_RELEASE:
5022 case GOMP_MAP_DELETE:
5023 case GOMP_MAP_FORCE_TO:
5024 case GOMP_MAP_FORCE_FROM:
5025 case GOMP_MAP_FORCE_TOFROM:
5026 case GOMP_MAP_FORCE_PRESENT:
5027 OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (c) = 1;
5028 break;
5029 default:
5030 break;
5032 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
5033 OMP_CLAUSE_MAP);
5034 if ((ort & C_ORT_OMP_DECLARE_SIMD) != C_ORT_OMP && ort != C_ORT_ACC)
5035 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_POINTER);
5036 else if (TREE_CODE (t) == COMPONENT_REF)
5037 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ALWAYS_POINTER);
5038 else if (REFERENCE_REF_P (t)
5039 && TREE_CODE (TREE_OPERAND (t, 0)) == COMPONENT_REF)
5041 t = TREE_OPERAND (t, 0);
5042 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ALWAYS_POINTER);
5044 else
5045 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_FIRSTPRIVATE_POINTER);
5046 if (OMP_CLAUSE_MAP_KIND (c2) != GOMP_MAP_FIRSTPRIVATE_POINTER
5047 && !cxx_mark_addressable (t))
5048 return false;
5049 OMP_CLAUSE_DECL (c2) = t;
5050 t = build_fold_addr_expr (first);
5051 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
5052 ptrdiff_type_node, t);
5053 tree ptr = OMP_CLAUSE_DECL (c2);
5054 ptr = convert_from_reference (ptr);
5055 if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
5056 ptr = build_fold_addr_expr (ptr);
5057 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
5058 ptrdiff_type_node, t,
5059 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
5060 ptrdiff_type_node, ptr));
5061 OMP_CLAUSE_SIZE (c2) = t;
5062 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
5063 OMP_CLAUSE_CHAIN (c) = c2;
5064 ptr = OMP_CLAUSE_DECL (c2);
5065 if (OMP_CLAUSE_MAP_KIND (c2) != GOMP_MAP_FIRSTPRIVATE_POINTER
5066 && TREE_CODE (TREE_TYPE (ptr)) == REFERENCE_TYPE
5067 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (ptr))))
5069 tree c3 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
5070 OMP_CLAUSE_MAP);
5071 OMP_CLAUSE_SET_MAP_KIND (c3, OMP_CLAUSE_MAP_KIND (c2));
5072 OMP_CLAUSE_DECL (c3) = ptr;
5073 if (OMP_CLAUSE_MAP_KIND (c2) == GOMP_MAP_ALWAYS_POINTER)
5074 OMP_CLAUSE_DECL (c2) = build_simple_mem_ref (ptr);
5075 else
5076 OMP_CLAUSE_DECL (c2) = convert_from_reference (ptr);
5077 OMP_CLAUSE_SIZE (c3) = size_zero_node;
5078 OMP_CLAUSE_CHAIN (c3) = OMP_CLAUSE_CHAIN (c2);
5079 OMP_CLAUSE_CHAIN (c2) = c3;
5083 return false;
5086 /* Return identifier to look up for omp declare reduction. */
5088 tree
5089 omp_reduction_id (enum tree_code reduction_code, tree reduction_id, tree type)
5091 const char *p = NULL;
5092 const char *m = NULL;
5093 switch (reduction_code)
5095 case PLUS_EXPR:
5096 case MULT_EXPR:
5097 case MINUS_EXPR:
5098 case BIT_AND_EXPR:
5099 case BIT_XOR_EXPR:
5100 case BIT_IOR_EXPR:
5101 case TRUTH_ANDIF_EXPR:
5102 case TRUTH_ORIF_EXPR:
5103 reduction_id = ovl_op_identifier (false, reduction_code);
5104 break;
5105 case MIN_EXPR:
5106 p = "min";
5107 break;
5108 case MAX_EXPR:
5109 p = "max";
5110 break;
5111 default:
5112 break;
5115 if (p == NULL)
5117 if (TREE_CODE (reduction_id) != IDENTIFIER_NODE)
5118 return error_mark_node;
5119 p = IDENTIFIER_POINTER (reduction_id);
5122 if (type != NULL_TREE)
5123 m = mangle_type_string (TYPE_MAIN_VARIANT (type));
5125 const char prefix[] = "omp declare reduction ";
5126 size_t lenp = sizeof (prefix);
5127 if (strncmp (p, prefix, lenp - 1) == 0)
5128 lenp = 1;
5129 size_t len = strlen (p);
5130 size_t lenm = m ? strlen (m) + 1 : 0;
5131 char *name = XALLOCAVEC (char, lenp + len + lenm);
5132 if (lenp > 1)
5133 memcpy (name, prefix, lenp - 1);
5134 memcpy (name + lenp - 1, p, len + 1);
5135 if (m)
5137 name[lenp + len - 1] = '~';
5138 memcpy (name + lenp + len, m, lenm);
5140 return get_identifier (name);
5143 /* Lookup OpenMP UDR ID for TYPE, return the corresponding artificial
5144 FUNCTION_DECL or NULL_TREE if not found. */
5146 static tree
5147 omp_reduction_lookup (location_t loc, tree id, tree type, tree *baselinkp,
5148 vec<tree> *ambiguousp)
5150 tree orig_id = id;
5151 tree baselink = NULL_TREE;
5152 if (identifier_p (id))
5154 cp_id_kind idk;
5155 bool nonint_cst_expression_p;
5156 const char *error_msg;
5157 id = omp_reduction_id (ERROR_MARK, id, type);
5158 tree decl = lookup_name (id);
5159 if (decl == NULL_TREE)
5160 decl = error_mark_node;
5161 id = finish_id_expression (id, decl, NULL_TREE, &idk, false, true,
5162 &nonint_cst_expression_p, false, true, false,
5163 false, &error_msg, loc);
5164 if (idk == CP_ID_KIND_UNQUALIFIED
5165 && identifier_p (id))
5167 vec<tree, va_gc> *args = NULL;
5168 vec_safe_push (args, build_reference_type (type));
5169 id = perform_koenig_lookup (id, args, tf_none);
5172 else if (TREE_CODE (id) == SCOPE_REF)
5173 id = lookup_qualified_name (TREE_OPERAND (id, 0),
5174 omp_reduction_id (ERROR_MARK,
5175 TREE_OPERAND (id, 1),
5176 type),
5177 false, false);
5178 tree fns = id;
5179 id = NULL_TREE;
5180 if (fns && is_overloaded_fn (fns))
5182 for (lkp_iterator iter (get_fns (fns)); iter; ++iter)
5184 tree fndecl = *iter;
5185 if (TREE_CODE (fndecl) == FUNCTION_DECL)
5187 tree argtype = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
5188 if (same_type_p (TREE_TYPE (argtype), type))
5190 id = fndecl;
5191 break;
5196 if (id && BASELINK_P (fns))
5198 if (baselinkp)
5199 *baselinkp = fns;
5200 else
5201 baselink = fns;
5205 if (!id && CLASS_TYPE_P (type) && TYPE_BINFO (type))
5207 vec<tree> ambiguous = vNULL;
5208 tree binfo = TYPE_BINFO (type), base_binfo, ret = NULL_TREE;
5209 unsigned int ix;
5210 if (ambiguousp == NULL)
5211 ambiguousp = &ambiguous;
5212 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
5214 id = omp_reduction_lookup (loc, orig_id, BINFO_TYPE (base_binfo),
5215 baselinkp ? baselinkp : &baselink,
5216 ambiguousp);
5217 if (id == NULL_TREE)
5218 continue;
5219 if (!ambiguousp->is_empty ())
5220 ambiguousp->safe_push (id);
5221 else if (ret != NULL_TREE)
5223 ambiguousp->safe_push (ret);
5224 ambiguousp->safe_push (id);
5225 ret = NULL_TREE;
5227 else
5228 ret = id;
5230 if (ambiguousp != &ambiguous)
5231 return ret;
5232 if (!ambiguous.is_empty ())
5234 const char *str = _("candidates are:");
5235 unsigned int idx;
5236 tree udr;
5237 error_at (loc, "user defined reduction lookup is ambiguous");
5238 FOR_EACH_VEC_ELT (ambiguous, idx, udr)
5240 inform (DECL_SOURCE_LOCATION (udr), "%s %#qD", str, udr);
5241 if (idx == 0)
5242 str = get_spaces (str);
5244 ambiguous.release ();
5245 ret = error_mark_node;
5246 baselink = NULL_TREE;
5248 id = ret;
5250 if (id && baselink)
5251 perform_or_defer_access_check (BASELINK_BINFO (baselink),
5252 id, id, tf_warning_or_error);
5253 return id;
5256 /* Helper function for cp_parser_omp_declare_reduction_exprs
5257 and tsubst_omp_udr.
5258 Remove CLEANUP_STMT for data (omp_priv variable).
5259 Also append INIT_EXPR for DECL_INITIAL of omp_priv after its
5260 DECL_EXPR. */
5262 tree
5263 cp_remove_omp_priv_cleanup_stmt (tree *tp, int *walk_subtrees, void *data)
5265 if (TYPE_P (*tp))
5266 *walk_subtrees = 0;
5267 else if (TREE_CODE (*tp) == CLEANUP_STMT && CLEANUP_DECL (*tp) == (tree) data)
5268 *tp = CLEANUP_BODY (*tp);
5269 else if (TREE_CODE (*tp) == DECL_EXPR)
5271 tree decl = DECL_EXPR_DECL (*tp);
5272 if (!processing_template_decl
5273 && decl == (tree) data
5274 && DECL_INITIAL (decl)
5275 && DECL_INITIAL (decl) != error_mark_node)
5277 tree list = NULL_TREE;
5278 append_to_statement_list_force (*tp, &list);
5279 tree init_expr = build2 (INIT_EXPR, void_type_node,
5280 decl, DECL_INITIAL (decl));
5281 DECL_INITIAL (decl) = NULL_TREE;
5282 append_to_statement_list_force (init_expr, &list);
5283 *tp = list;
5286 return NULL_TREE;
5289 /* Data passed from cp_check_omp_declare_reduction to
5290 cp_check_omp_declare_reduction_r. */
5292 struct cp_check_omp_declare_reduction_data
5294 location_t loc;
5295 tree stmts[7];
5296 bool combiner_p;
5299 /* Helper function for cp_check_omp_declare_reduction, called via
5300 cp_walk_tree. */
5302 static tree
5303 cp_check_omp_declare_reduction_r (tree *tp, int *, void *data)
5305 struct cp_check_omp_declare_reduction_data *udr_data
5306 = (struct cp_check_omp_declare_reduction_data *) data;
5307 if (SSA_VAR_P (*tp)
5308 && !DECL_ARTIFICIAL (*tp)
5309 && *tp != DECL_EXPR_DECL (udr_data->stmts[udr_data->combiner_p ? 0 : 3])
5310 && *tp != DECL_EXPR_DECL (udr_data->stmts[udr_data->combiner_p ? 1 : 4]))
5312 location_t loc = udr_data->loc;
5313 if (udr_data->combiner_p)
5314 error_at (loc, "%<#pragma omp declare reduction%> combiner refers to "
5315 "variable %qD which is not %<omp_out%> nor %<omp_in%>",
5316 *tp);
5317 else
5318 error_at (loc, "%<#pragma omp declare reduction%> initializer refers "
5319 "to variable %qD which is not %<omp_priv%> nor "
5320 "%<omp_orig%>",
5321 *tp);
5322 return *tp;
5324 return NULL_TREE;
5327 /* Diagnose violation of OpenMP #pragma omp declare reduction restrictions. */
5329 void
5330 cp_check_omp_declare_reduction (tree udr)
5332 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (udr)));
5333 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
5334 type = TREE_TYPE (type);
5335 int i;
5336 location_t loc = DECL_SOURCE_LOCATION (udr);
5338 if (type == error_mark_node)
5339 return;
5340 if (ARITHMETIC_TYPE_P (type))
5342 static enum tree_code predef_codes[]
5343 = { PLUS_EXPR, MULT_EXPR, MINUS_EXPR, BIT_AND_EXPR, BIT_XOR_EXPR,
5344 BIT_IOR_EXPR, TRUTH_ANDIF_EXPR, TRUTH_ORIF_EXPR };
5345 for (i = 0; i < 8; i++)
5347 tree id = omp_reduction_id (predef_codes[i], NULL_TREE, NULL_TREE);
5348 const char *n1 = IDENTIFIER_POINTER (DECL_NAME (udr));
5349 const char *n2 = IDENTIFIER_POINTER (id);
5350 if (strncmp (n1, n2, IDENTIFIER_LENGTH (id)) == 0
5351 && (n1[IDENTIFIER_LENGTH (id)] == '~'
5352 || n1[IDENTIFIER_LENGTH (id)] == '\0'))
5353 break;
5356 if (i == 8
5357 && TREE_CODE (type) != COMPLEX_EXPR)
5359 const char prefix_minmax[] = "omp declare reduction m";
5360 size_t prefix_size = sizeof (prefix_minmax) - 1;
5361 const char *n = IDENTIFIER_POINTER (DECL_NAME (udr));
5362 if (strncmp (IDENTIFIER_POINTER (DECL_NAME (udr)),
5363 prefix_minmax, prefix_size) == 0
5364 && ((n[prefix_size] == 'i' && n[prefix_size + 1] == 'n')
5365 || (n[prefix_size] == 'a' && n[prefix_size + 1] == 'x'))
5366 && (n[prefix_size + 2] == '~' || n[prefix_size + 2] == '\0'))
5367 i = 0;
5369 if (i < 8)
5371 error_at (loc, "predeclared arithmetic type %qT in "
5372 "%<#pragma omp declare reduction%>", type);
5373 return;
5376 else if (TREE_CODE (type) == FUNCTION_TYPE
5377 || TREE_CODE (type) == METHOD_TYPE
5378 || TREE_CODE (type) == ARRAY_TYPE)
5380 error_at (loc, "function or array type %qT in "
5381 "%<#pragma omp declare reduction%>", type);
5382 return;
5384 else if (TREE_CODE (type) == REFERENCE_TYPE)
5386 error_at (loc, "reference type %qT in %<#pragma omp declare reduction%>",
5387 type);
5388 return;
5390 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
5392 error_at (loc, "const, volatile or __restrict qualified type %qT in "
5393 "%<#pragma omp declare reduction%>", type);
5394 return;
5397 tree body = DECL_SAVED_TREE (udr);
5398 if (body == NULL_TREE || TREE_CODE (body) != STATEMENT_LIST)
5399 return;
5401 tree_stmt_iterator tsi;
5402 struct cp_check_omp_declare_reduction_data data;
5403 memset (data.stmts, 0, sizeof data.stmts);
5404 for (i = 0, tsi = tsi_start (body);
5405 i < 7 && !tsi_end_p (tsi);
5406 i++, tsi_next (&tsi))
5407 data.stmts[i] = tsi_stmt (tsi);
5408 data.loc = loc;
5409 gcc_assert (tsi_end_p (tsi));
5410 if (i >= 3)
5412 gcc_assert (TREE_CODE (data.stmts[0]) == DECL_EXPR
5413 && TREE_CODE (data.stmts[1]) == DECL_EXPR);
5414 if (TREE_NO_WARNING (DECL_EXPR_DECL (data.stmts[0])))
5415 return;
5416 data.combiner_p = true;
5417 if (cp_walk_tree (&data.stmts[2], cp_check_omp_declare_reduction_r,
5418 &data, NULL))
5419 TREE_NO_WARNING (DECL_EXPR_DECL (data.stmts[0])) = 1;
5421 if (i >= 6)
5423 gcc_assert (TREE_CODE (data.stmts[3]) == DECL_EXPR
5424 && TREE_CODE (data.stmts[4]) == DECL_EXPR);
5425 data.combiner_p = false;
5426 if (cp_walk_tree (&data.stmts[5], cp_check_omp_declare_reduction_r,
5427 &data, NULL)
5428 || cp_walk_tree (&DECL_INITIAL (DECL_EXPR_DECL (data.stmts[3])),
5429 cp_check_omp_declare_reduction_r, &data, NULL))
5430 TREE_NO_WARNING (DECL_EXPR_DECL (data.stmts[0])) = 1;
5431 if (i == 7)
5432 gcc_assert (TREE_CODE (data.stmts[6]) == DECL_EXPR);
5436 /* Helper function of finish_omp_clauses. Clone STMT as if we were making
5437 an inline call. But, remap
5438 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
5439 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
5441 static tree
5442 clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
5443 tree decl, tree placeholder)
5445 copy_body_data id;
5446 hash_map<tree, tree> decl_map;
5448 decl_map.put (omp_decl1, placeholder);
5449 decl_map.put (omp_decl2, decl);
5450 memset (&id, 0, sizeof (id));
5451 id.src_fn = DECL_CONTEXT (omp_decl1);
5452 id.dst_fn = current_function_decl;
5453 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
5454 id.decl_map = &decl_map;
5456 id.copy_decl = copy_decl_no_change;
5457 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
5458 id.transform_new_cfg = true;
5459 id.transform_return_to_modify = false;
5460 id.transform_lang_insert_block = NULL;
5461 id.eh_lp_nr = 0;
5462 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
5463 return stmt;
5466 /* Helper function of finish_omp_clauses, called via cp_walk_tree.
5467 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
5469 static tree
5470 find_omp_placeholder_r (tree *tp, int *, void *data)
5472 if (*tp == (tree) data)
5473 return *tp;
5474 return NULL_TREE;
5477 /* Helper function of finish_omp_clauses. Handle OMP_CLAUSE_REDUCTION C.
5478 Return true if there is some error and the clause should be removed. */
5480 static bool
5481 finish_omp_reduction_clause (tree c, bool *need_default_ctor, bool *need_dtor)
5483 tree t = OMP_CLAUSE_DECL (c);
5484 bool predefined = false;
5485 if (TREE_CODE (t) == TREE_LIST)
5487 gcc_assert (processing_template_decl);
5488 return false;
5490 tree type = TREE_TYPE (t);
5491 if (TREE_CODE (t) == MEM_REF)
5492 type = TREE_TYPE (type);
5493 if (TREE_CODE (type) == REFERENCE_TYPE)
5494 type = TREE_TYPE (type);
5495 if (TREE_CODE (type) == ARRAY_TYPE)
5497 tree oatype = type;
5498 gcc_assert (TREE_CODE (t) != MEM_REF);
5499 while (TREE_CODE (type) == ARRAY_TYPE)
5500 type = TREE_TYPE (type);
5501 if (!processing_template_decl)
5503 t = require_complete_type (t);
5504 if (t == error_mark_node)
5505 return true;
5506 tree size = size_binop (EXACT_DIV_EXPR, TYPE_SIZE_UNIT (oatype),
5507 TYPE_SIZE_UNIT (type));
5508 if (integer_zerop (size))
5510 error ("%qE in %<reduction%> clause is a zero size array",
5511 omp_clause_printable_decl (t));
5512 return true;
5514 size = size_binop (MINUS_EXPR, size, size_one_node);
5515 tree index_type = build_index_type (size);
5516 tree atype = build_array_type (type, index_type);
5517 tree ptype = build_pointer_type (type);
5518 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
5519 t = build_fold_addr_expr (t);
5520 t = build2 (MEM_REF, atype, t, build_int_cst (ptype, 0));
5521 OMP_CLAUSE_DECL (c) = t;
5524 if (type == error_mark_node)
5525 return true;
5526 else if (ARITHMETIC_TYPE_P (type))
5527 switch (OMP_CLAUSE_REDUCTION_CODE (c))
5529 case PLUS_EXPR:
5530 case MULT_EXPR:
5531 case MINUS_EXPR:
5532 predefined = true;
5533 break;
5534 case MIN_EXPR:
5535 case MAX_EXPR:
5536 if (TREE_CODE (type) == COMPLEX_TYPE)
5537 break;
5538 predefined = true;
5539 break;
5540 case BIT_AND_EXPR:
5541 case BIT_IOR_EXPR:
5542 case BIT_XOR_EXPR:
5543 if (FLOAT_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
5544 break;
5545 predefined = true;
5546 break;
5547 case TRUTH_ANDIF_EXPR:
5548 case TRUTH_ORIF_EXPR:
5549 if (FLOAT_TYPE_P (type))
5550 break;
5551 predefined = true;
5552 break;
5553 default:
5554 break;
5556 else if (TYPE_READONLY (type))
5558 error ("%qE has const type for %<reduction%>",
5559 omp_clause_printable_decl (t));
5560 return true;
5562 else if (!processing_template_decl)
5564 t = require_complete_type (t);
5565 if (t == error_mark_node)
5566 return true;
5567 OMP_CLAUSE_DECL (c) = t;
5570 if (predefined)
5572 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = NULL_TREE;
5573 return false;
5575 else if (processing_template_decl)
5576 return false;
5578 tree id = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
5580 type = TYPE_MAIN_VARIANT (type);
5581 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = NULL_TREE;
5582 if (id == NULL_TREE)
5583 id = omp_reduction_id (OMP_CLAUSE_REDUCTION_CODE (c),
5584 NULL_TREE, NULL_TREE);
5585 id = omp_reduction_lookup (OMP_CLAUSE_LOCATION (c), id, type, NULL, NULL);
5586 if (id)
5588 if (id == error_mark_node)
5589 return true;
5590 mark_used (id);
5591 tree body = DECL_SAVED_TREE (id);
5592 if (!body)
5593 return true;
5594 if (TREE_CODE (body) == STATEMENT_LIST)
5596 tree_stmt_iterator tsi;
5597 tree placeholder = NULL_TREE, decl_placeholder = NULL_TREE;
5598 int i;
5599 tree stmts[7];
5600 tree atype = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (id)));
5601 atype = TREE_TYPE (atype);
5602 bool need_static_cast = !same_type_p (type, atype);
5603 memset (stmts, 0, sizeof stmts);
5604 for (i = 0, tsi = tsi_start (body);
5605 i < 7 && !tsi_end_p (tsi);
5606 i++, tsi_next (&tsi))
5607 stmts[i] = tsi_stmt (tsi);
5608 gcc_assert (tsi_end_p (tsi));
5610 if (i >= 3)
5612 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
5613 && TREE_CODE (stmts[1]) == DECL_EXPR);
5614 placeholder = build_lang_decl (VAR_DECL, NULL_TREE, type);
5615 DECL_ARTIFICIAL (placeholder) = 1;
5616 DECL_IGNORED_P (placeholder) = 1;
5617 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
5618 if (TREE_CODE (t) == MEM_REF)
5620 decl_placeholder = build_lang_decl (VAR_DECL, NULL_TREE,
5621 type);
5622 DECL_ARTIFICIAL (decl_placeholder) = 1;
5623 DECL_IGNORED_P (decl_placeholder) = 1;
5624 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c) = decl_placeholder;
5626 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[0])))
5627 cxx_mark_addressable (placeholder);
5628 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[1]))
5629 && TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c)))
5630 != REFERENCE_TYPE)
5631 cxx_mark_addressable (decl_placeholder ? decl_placeholder
5632 : OMP_CLAUSE_DECL (c));
5633 tree omp_out = placeholder;
5634 tree omp_in = decl_placeholder ? decl_placeholder
5635 : convert_from_reference (OMP_CLAUSE_DECL (c));
5636 if (need_static_cast)
5638 tree rtype = build_reference_type (atype);
5639 omp_out = build_static_cast (rtype, omp_out,
5640 tf_warning_or_error);
5641 omp_in = build_static_cast (rtype, omp_in,
5642 tf_warning_or_error);
5643 if (omp_out == error_mark_node || omp_in == error_mark_node)
5644 return true;
5645 omp_out = convert_from_reference (omp_out);
5646 omp_in = convert_from_reference (omp_in);
5648 OMP_CLAUSE_REDUCTION_MERGE (c)
5649 = clone_omp_udr (stmts[2], DECL_EXPR_DECL (stmts[0]),
5650 DECL_EXPR_DECL (stmts[1]), omp_in, omp_out);
5652 if (i >= 6)
5654 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
5655 && TREE_CODE (stmts[4]) == DECL_EXPR);
5656 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[3])))
5657 cxx_mark_addressable (decl_placeholder ? decl_placeholder
5658 : OMP_CLAUSE_DECL (c));
5659 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[4])))
5660 cxx_mark_addressable (placeholder);
5661 tree omp_priv = decl_placeholder ? decl_placeholder
5662 : convert_from_reference (OMP_CLAUSE_DECL (c));
5663 tree omp_orig = placeholder;
5664 if (need_static_cast)
5666 if (i == 7)
5668 error_at (OMP_CLAUSE_LOCATION (c),
5669 "user defined reduction with constructor "
5670 "initializer for base class %qT", atype);
5671 return true;
5673 tree rtype = build_reference_type (atype);
5674 omp_priv = build_static_cast (rtype, omp_priv,
5675 tf_warning_or_error);
5676 omp_orig = build_static_cast (rtype, omp_orig,
5677 tf_warning_or_error);
5678 if (omp_priv == error_mark_node
5679 || omp_orig == error_mark_node)
5680 return true;
5681 omp_priv = convert_from_reference (omp_priv);
5682 omp_orig = convert_from_reference (omp_orig);
5684 if (i == 6)
5685 *need_default_ctor = true;
5686 OMP_CLAUSE_REDUCTION_INIT (c)
5687 = clone_omp_udr (stmts[5], DECL_EXPR_DECL (stmts[4]),
5688 DECL_EXPR_DECL (stmts[3]),
5689 omp_priv, omp_orig);
5690 if (cp_walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
5691 find_omp_placeholder_r, placeholder, NULL))
5692 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
5694 else if (i >= 3)
5696 if (CLASS_TYPE_P (type) && !pod_type_p (type))
5697 *need_default_ctor = true;
5698 else
5700 tree init;
5701 tree v = decl_placeholder ? decl_placeholder
5702 : convert_from_reference (t);
5703 if (AGGREGATE_TYPE_P (TREE_TYPE (v)))
5704 init = build_constructor (TREE_TYPE (v), NULL);
5705 else
5706 init = fold_convert (TREE_TYPE (v), integer_zero_node);
5707 OMP_CLAUSE_REDUCTION_INIT (c)
5708 = build2 (INIT_EXPR, TREE_TYPE (v), v, init);
5713 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
5714 *need_dtor = true;
5715 else
5717 error ("user defined reduction not found for %qE",
5718 omp_clause_printable_decl (t));
5719 return true;
5721 if (TREE_CODE (OMP_CLAUSE_DECL (c)) == MEM_REF)
5722 gcc_assert (TYPE_SIZE_UNIT (type)
5723 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST);
5724 return false;
5727 /* Called from finish_struct_1. linear(this) or linear(this:step)
5728 clauses might not be finalized yet because the class has been incomplete
5729 when parsing #pragma omp declare simd methods. Fix those up now. */
5731 void
5732 finish_omp_declare_simd_methods (tree t)
5734 if (processing_template_decl)
5735 return;
5737 for (tree x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
5739 if (TREE_CODE (TREE_TYPE (x)) != METHOD_TYPE)
5740 continue;
5741 tree ods = lookup_attribute ("omp declare simd", DECL_ATTRIBUTES (x));
5742 if (!ods || !TREE_VALUE (ods))
5743 continue;
5744 for (tree c = TREE_VALUE (TREE_VALUE (ods)); c; c = OMP_CLAUSE_CHAIN (c))
5745 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
5746 && integer_zerop (OMP_CLAUSE_DECL (c))
5747 && OMP_CLAUSE_LINEAR_STEP (c)
5748 && TREE_CODE (TREE_TYPE (OMP_CLAUSE_LINEAR_STEP (c)))
5749 == POINTER_TYPE)
5751 tree s = OMP_CLAUSE_LINEAR_STEP (c);
5752 s = fold_convert_loc (OMP_CLAUSE_LOCATION (c), sizetype, s);
5753 s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MULT_EXPR,
5754 sizetype, s, TYPE_SIZE_UNIT (t));
5755 OMP_CLAUSE_LINEAR_STEP (c) = s;
5760 /* Adjust sink depend clause to take into account pointer offsets.
5762 Return TRUE if there was a problem processing the offset, and the
5763 whole clause should be removed. */
5765 static bool
5766 cp_finish_omp_clause_depend_sink (tree sink_clause)
5768 tree t = OMP_CLAUSE_DECL (sink_clause);
5769 gcc_assert (TREE_CODE (t) == TREE_LIST);
5771 /* Make sure we don't adjust things twice for templates. */
5772 if (processing_template_decl)
5773 return false;
5775 for (; t; t = TREE_CHAIN (t))
5777 tree decl = TREE_VALUE (t);
5778 if (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
5780 tree offset = TREE_PURPOSE (t);
5781 bool neg = wi::neg_p (wi::to_wide (offset));
5782 offset = fold_unary (ABS_EXPR, TREE_TYPE (offset), offset);
5783 decl = mark_rvalue_use (decl);
5784 decl = convert_from_reference (decl);
5785 tree t2 = pointer_int_sum (OMP_CLAUSE_LOCATION (sink_clause),
5786 neg ? MINUS_EXPR : PLUS_EXPR,
5787 decl, offset);
5788 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (sink_clause),
5789 MINUS_EXPR, sizetype,
5790 fold_convert (sizetype, t2),
5791 fold_convert (sizetype, decl));
5792 if (t2 == error_mark_node)
5793 return true;
5794 TREE_PURPOSE (t) = t2;
5797 return false;
5800 /* For all elements of CLAUSES, validate them vs OpenMP constraints.
5801 Remove any elements from the list that are invalid. */
5803 tree
5804 finish_omp_clauses (tree clauses, enum c_omp_region_type ort)
5806 bitmap_head generic_head, firstprivate_head, lastprivate_head;
5807 bitmap_head aligned_head, map_head, map_field_head, oacc_reduction_head;
5808 tree c, t, *pc;
5809 tree safelen = NULL_TREE;
5810 bool branch_seen = false;
5811 bool copyprivate_seen = false;
5812 bool ordered_seen = false;
5813 bool oacc_async = false;
5815 bitmap_obstack_initialize (NULL);
5816 bitmap_initialize (&generic_head, &bitmap_default_obstack);
5817 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
5818 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
5819 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
5820 bitmap_initialize (&map_head, &bitmap_default_obstack);
5821 bitmap_initialize (&map_field_head, &bitmap_default_obstack);
5822 bitmap_initialize (&oacc_reduction_head, &bitmap_default_obstack);
5824 if (ort & C_ORT_ACC)
5825 for (c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
5826 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ASYNC)
5828 oacc_async = true;
5829 break;
5832 for (pc = &clauses, c = clauses; c ; c = *pc)
5834 bool remove = false;
5835 bool field_ok = false;
5837 switch (OMP_CLAUSE_CODE (c))
5839 case OMP_CLAUSE_SHARED:
5840 field_ok = ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP);
5841 goto check_dup_generic;
5842 case OMP_CLAUSE_PRIVATE:
5843 field_ok = ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP);
5844 goto check_dup_generic;
5845 case OMP_CLAUSE_REDUCTION:
5846 field_ok = ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP);
5847 t = OMP_CLAUSE_DECL (c);
5848 if (TREE_CODE (t) == TREE_LIST)
5850 if (handle_omp_array_sections (c, ort))
5852 remove = true;
5853 break;
5855 if (TREE_CODE (t) == TREE_LIST)
5857 while (TREE_CODE (t) == TREE_LIST)
5858 t = TREE_CHAIN (t);
5860 else
5862 gcc_assert (TREE_CODE (t) == MEM_REF);
5863 t = TREE_OPERAND (t, 0);
5864 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
5865 t = TREE_OPERAND (t, 0);
5866 if (TREE_CODE (t) == ADDR_EXPR
5867 || INDIRECT_REF_P (t))
5868 t = TREE_OPERAND (t, 0);
5870 tree n = omp_clause_decl_field (t);
5871 if (n)
5872 t = n;
5873 goto check_dup_generic_t;
5875 if (oacc_async)
5876 cxx_mark_addressable (t);
5877 goto check_dup_generic;
5878 case OMP_CLAUSE_COPYPRIVATE:
5879 copyprivate_seen = true;
5880 field_ok = ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP);
5881 goto check_dup_generic;
5882 case OMP_CLAUSE_COPYIN:
5883 goto check_dup_generic;
5884 case OMP_CLAUSE_LINEAR:
5885 field_ok = ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP);
5886 t = OMP_CLAUSE_DECL (c);
5887 if (ort != C_ORT_OMP_DECLARE_SIMD
5888 && OMP_CLAUSE_LINEAR_KIND (c) != OMP_CLAUSE_LINEAR_DEFAULT)
5890 error_at (OMP_CLAUSE_LOCATION (c),
5891 "modifier should not be specified in %<linear%> "
5892 "clause on %<simd%> or %<for%> constructs");
5893 OMP_CLAUSE_LINEAR_KIND (c) = OMP_CLAUSE_LINEAR_DEFAULT;
5895 if ((VAR_P (t) || TREE_CODE (t) == PARM_DECL)
5896 && !type_dependent_expression_p (t))
5898 tree type = TREE_TYPE (t);
5899 if ((OMP_CLAUSE_LINEAR_KIND (c) == OMP_CLAUSE_LINEAR_REF
5900 || OMP_CLAUSE_LINEAR_KIND (c) == OMP_CLAUSE_LINEAR_UVAL)
5901 && TREE_CODE (type) != REFERENCE_TYPE)
5903 error ("linear clause with %qs modifier applied to "
5904 "non-reference variable with %qT type",
5905 OMP_CLAUSE_LINEAR_KIND (c) == OMP_CLAUSE_LINEAR_REF
5906 ? "ref" : "uval", TREE_TYPE (t));
5907 remove = true;
5908 break;
5910 if (TREE_CODE (type) == REFERENCE_TYPE)
5911 type = TREE_TYPE (type);
5912 if (OMP_CLAUSE_LINEAR_KIND (c) != OMP_CLAUSE_LINEAR_REF)
5914 if (!INTEGRAL_TYPE_P (type)
5915 && TREE_CODE (type) != POINTER_TYPE)
5917 error ("linear clause applied to non-integral non-pointer"
5918 " variable with %qT type", TREE_TYPE (t));
5919 remove = true;
5920 break;
5924 t = OMP_CLAUSE_LINEAR_STEP (c);
5925 if (t == NULL_TREE)
5926 t = integer_one_node;
5927 if (t == error_mark_node)
5929 remove = true;
5930 break;
5932 else if (!type_dependent_expression_p (t)
5933 && !INTEGRAL_TYPE_P (TREE_TYPE (t))
5934 && (ort != C_ORT_OMP_DECLARE_SIMD
5935 || TREE_CODE (t) != PARM_DECL
5936 || TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE
5937 || !INTEGRAL_TYPE_P (TREE_TYPE (TREE_TYPE (t)))))
5939 error ("linear step expression must be integral");
5940 remove = true;
5941 break;
5943 else
5945 t = mark_rvalue_use (t);
5946 if (ort == C_ORT_OMP_DECLARE_SIMD && TREE_CODE (t) == PARM_DECL)
5948 OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c) = 1;
5949 goto check_dup_generic;
5951 if (!processing_template_decl
5952 && (VAR_P (OMP_CLAUSE_DECL (c))
5953 || TREE_CODE (OMP_CLAUSE_DECL (c)) == PARM_DECL))
5955 if (ort == C_ORT_OMP_DECLARE_SIMD)
5957 t = maybe_constant_value (t);
5958 if (TREE_CODE (t) != INTEGER_CST)
5960 error_at (OMP_CLAUSE_LOCATION (c),
5961 "%<linear%> clause step %qE is neither "
5962 "constant nor a parameter", t);
5963 remove = true;
5964 break;
5967 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5968 tree type = TREE_TYPE (OMP_CLAUSE_DECL (c));
5969 if (TREE_CODE (type) == REFERENCE_TYPE)
5970 type = TREE_TYPE (type);
5971 if (OMP_CLAUSE_LINEAR_KIND (c) == OMP_CLAUSE_LINEAR_REF)
5973 type = build_pointer_type (type);
5974 tree d = fold_convert (type, OMP_CLAUSE_DECL (c));
5975 t = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
5976 d, t);
5977 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c),
5978 MINUS_EXPR, sizetype,
5979 fold_convert (sizetype, t),
5980 fold_convert (sizetype, d));
5981 if (t == error_mark_node)
5983 remove = true;
5984 break;
5987 else if (TREE_CODE (type) == POINTER_TYPE
5988 /* Can't multiply the step yet if *this
5989 is still incomplete type. */
5990 && (ort != C_ORT_OMP_DECLARE_SIMD
5991 || TREE_CODE (OMP_CLAUSE_DECL (c)) != PARM_DECL
5992 || !DECL_ARTIFICIAL (OMP_CLAUSE_DECL (c))
5993 || DECL_NAME (OMP_CLAUSE_DECL (c))
5994 != this_identifier
5995 || !TYPE_BEING_DEFINED (TREE_TYPE (type))))
5997 tree d = convert_from_reference (OMP_CLAUSE_DECL (c));
5998 t = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
5999 d, t);
6000 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c),
6001 MINUS_EXPR, sizetype,
6002 fold_convert (sizetype, t),
6003 fold_convert (sizetype, d));
6004 if (t == error_mark_node)
6006 remove = true;
6007 break;
6010 else
6011 t = fold_convert (type, t);
6013 OMP_CLAUSE_LINEAR_STEP (c) = t;
6015 goto check_dup_generic;
6016 check_dup_generic:
6017 t = omp_clause_decl_field (OMP_CLAUSE_DECL (c));
6018 if (t)
6020 if (!remove && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_SHARED)
6021 omp_note_field_privatization (t, OMP_CLAUSE_DECL (c));
6023 else
6024 t = OMP_CLAUSE_DECL (c);
6025 check_dup_generic_t:
6026 if (t == current_class_ptr
6027 && (ort != C_ORT_OMP_DECLARE_SIMD
6028 || (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_LINEAR
6029 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_UNIFORM)))
6031 error ("%<this%> allowed in OpenMP only in %<declare simd%>"
6032 " clauses");
6033 remove = true;
6034 break;
6036 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL
6037 && (!field_ok || TREE_CODE (t) != FIELD_DECL))
6039 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
6040 break;
6041 if (DECL_P (t))
6042 error ("%qD is not a variable in clause %qs", t,
6043 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6044 else
6045 error ("%qE is not a variable in clause %qs", t,
6046 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6047 remove = true;
6049 else if (ort == C_ORT_ACC
6050 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
6052 if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
6054 error ("%qD appears more than once in reduction clauses", t);
6055 remove = true;
6057 else
6058 bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
6060 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
6061 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
6062 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
6064 error ("%qD appears more than once in data clauses", t);
6065 remove = true;
6067 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
6068 && bitmap_bit_p (&map_head, DECL_UID (t)))
6070 if (ort == C_ORT_ACC)
6071 error ("%qD appears more than once in data clauses", t);
6072 else
6073 error ("%qD appears both in data and map clauses", t);
6074 remove = true;
6076 else
6077 bitmap_set_bit (&generic_head, DECL_UID (t));
6078 if (!field_ok)
6079 break;
6080 handle_field_decl:
6081 if (!remove
6082 && TREE_CODE (t) == FIELD_DECL
6083 && t == OMP_CLAUSE_DECL (c)
6084 && ort != C_ORT_ACC)
6086 OMP_CLAUSE_DECL (c)
6087 = omp_privatize_field (t, (OMP_CLAUSE_CODE (c)
6088 == OMP_CLAUSE_SHARED));
6089 if (OMP_CLAUSE_DECL (c) == error_mark_node)
6090 remove = true;
6092 break;
6094 case OMP_CLAUSE_FIRSTPRIVATE:
6095 t = omp_clause_decl_field (OMP_CLAUSE_DECL (c));
6096 if (t)
6097 omp_note_field_privatization (t, OMP_CLAUSE_DECL (c));
6098 else
6099 t = OMP_CLAUSE_DECL (c);
6100 if (ort != C_ORT_ACC && t == current_class_ptr)
6102 error ("%<this%> allowed in OpenMP only in %<declare simd%>"
6103 " clauses");
6104 remove = true;
6105 break;
6107 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL
6108 && ((ort & C_ORT_OMP_DECLARE_SIMD) != C_ORT_OMP
6109 || TREE_CODE (t) != FIELD_DECL))
6111 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
6112 break;
6113 if (DECL_P (t))
6114 error ("%qD is not a variable in clause %<firstprivate%>", t);
6115 else
6116 error ("%qE is not a variable in clause %<firstprivate%>", t);
6117 remove = true;
6119 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
6120 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
6122 error ("%qD appears more than once in data clauses", t);
6123 remove = true;
6125 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
6127 if (ort == C_ORT_ACC)
6128 error ("%qD appears more than once in data clauses", t);
6129 else
6130 error ("%qD appears both in data and map clauses", t);
6131 remove = true;
6133 else
6134 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
6135 goto handle_field_decl;
6137 case OMP_CLAUSE_LASTPRIVATE:
6138 t = omp_clause_decl_field (OMP_CLAUSE_DECL (c));
6139 if (t)
6140 omp_note_field_privatization (t, OMP_CLAUSE_DECL (c));
6141 else
6142 t = OMP_CLAUSE_DECL (c);
6143 if (t == current_class_ptr)
6145 error ("%<this%> allowed in OpenMP only in %<declare simd%>"
6146 " clauses");
6147 remove = true;
6148 break;
6150 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL
6151 && ((ort & C_ORT_OMP_DECLARE_SIMD) != C_ORT_OMP
6152 || TREE_CODE (t) != FIELD_DECL))
6154 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
6155 break;
6156 if (DECL_P (t))
6157 error ("%qD is not a variable in clause %<lastprivate%>", t);
6158 else
6159 error ("%qE is not a variable in clause %<lastprivate%>", t);
6160 remove = true;
6162 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
6163 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
6165 error ("%qD appears more than once in data clauses", t);
6166 remove = true;
6168 else
6169 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
6170 goto handle_field_decl;
6172 case OMP_CLAUSE_IF:
6173 t = OMP_CLAUSE_IF_EXPR (c);
6174 t = maybe_convert_cond (t);
6175 if (t == error_mark_node)
6176 remove = true;
6177 else if (!processing_template_decl)
6178 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6179 OMP_CLAUSE_IF_EXPR (c) = t;
6180 break;
6182 case OMP_CLAUSE_FINAL:
6183 t = OMP_CLAUSE_FINAL_EXPR (c);
6184 t = maybe_convert_cond (t);
6185 if (t == error_mark_node)
6186 remove = true;
6187 else if (!processing_template_decl)
6188 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6189 OMP_CLAUSE_FINAL_EXPR (c) = t;
6190 break;
6192 case OMP_CLAUSE_GANG:
6193 /* Operand 1 is the gang static: argument. */
6194 t = OMP_CLAUSE_OPERAND (c, 1);
6195 if (t != NULL_TREE)
6197 if (t == error_mark_node)
6198 remove = true;
6199 else if (!type_dependent_expression_p (t)
6200 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6202 error ("%<gang%> static expression must be integral");
6203 remove = true;
6205 else
6207 t = mark_rvalue_use (t);
6208 if (!processing_template_decl)
6210 t = maybe_constant_value (t);
6211 if (TREE_CODE (t) == INTEGER_CST
6212 && tree_int_cst_sgn (t) != 1
6213 && t != integer_minus_one_node)
6215 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6216 "%<gang%> static value must be "
6217 "positive");
6218 t = integer_one_node;
6220 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6223 OMP_CLAUSE_OPERAND (c, 1) = t;
6225 /* Check operand 0, the num argument. */
6226 /* FALLTHRU */
6228 case OMP_CLAUSE_WORKER:
6229 case OMP_CLAUSE_VECTOR:
6230 if (OMP_CLAUSE_OPERAND (c, 0) == NULL_TREE)
6231 break;
6232 /* FALLTHRU */
6234 case OMP_CLAUSE_NUM_TASKS:
6235 case OMP_CLAUSE_NUM_TEAMS:
6236 case OMP_CLAUSE_NUM_THREADS:
6237 case OMP_CLAUSE_NUM_GANGS:
6238 case OMP_CLAUSE_NUM_WORKERS:
6239 case OMP_CLAUSE_VECTOR_LENGTH:
6240 t = OMP_CLAUSE_OPERAND (c, 0);
6241 if (t == error_mark_node)
6242 remove = true;
6243 else if (!type_dependent_expression_p (t)
6244 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6246 switch (OMP_CLAUSE_CODE (c))
6248 case OMP_CLAUSE_GANG:
6249 error_at (OMP_CLAUSE_LOCATION (c),
6250 "%<gang%> num expression must be integral"); break;
6251 case OMP_CLAUSE_VECTOR:
6252 error_at (OMP_CLAUSE_LOCATION (c),
6253 "%<vector%> length expression must be integral");
6254 break;
6255 case OMP_CLAUSE_WORKER:
6256 error_at (OMP_CLAUSE_LOCATION (c),
6257 "%<worker%> num expression must be integral");
6258 break;
6259 default:
6260 error_at (OMP_CLAUSE_LOCATION (c),
6261 "%qs expression must be integral",
6262 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6264 remove = true;
6266 else
6268 t = mark_rvalue_use (t);
6269 if (!processing_template_decl)
6271 t = maybe_constant_value (t);
6272 if (TREE_CODE (t) == INTEGER_CST
6273 && tree_int_cst_sgn (t) != 1)
6275 switch (OMP_CLAUSE_CODE (c))
6277 case OMP_CLAUSE_GANG:
6278 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6279 "%<gang%> num value must be positive");
6280 break;
6281 case OMP_CLAUSE_VECTOR:
6282 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6283 "%<vector%> length value must be "
6284 "positive");
6285 break;
6286 case OMP_CLAUSE_WORKER:
6287 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6288 "%<worker%> num value must be "
6289 "positive");
6290 break;
6291 default:
6292 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6293 "%qs value must be positive",
6294 omp_clause_code_name
6295 [OMP_CLAUSE_CODE (c)]);
6297 t = integer_one_node;
6299 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6301 OMP_CLAUSE_OPERAND (c, 0) = t;
6303 break;
6305 case OMP_CLAUSE_SCHEDULE:
6306 if (OMP_CLAUSE_SCHEDULE_KIND (c) & OMP_CLAUSE_SCHEDULE_NONMONOTONIC)
6308 const char *p = NULL;
6309 switch (OMP_CLAUSE_SCHEDULE_KIND (c) & OMP_CLAUSE_SCHEDULE_MASK)
6311 case OMP_CLAUSE_SCHEDULE_STATIC: p = "static"; break;
6312 case OMP_CLAUSE_SCHEDULE_DYNAMIC: break;
6313 case OMP_CLAUSE_SCHEDULE_GUIDED: break;
6314 case OMP_CLAUSE_SCHEDULE_AUTO: p = "auto"; break;
6315 case OMP_CLAUSE_SCHEDULE_RUNTIME: p = "runtime"; break;
6316 default: gcc_unreachable ();
6318 if (p)
6320 error_at (OMP_CLAUSE_LOCATION (c),
6321 "%<nonmonotonic%> modifier specified for %qs "
6322 "schedule kind", p);
6323 OMP_CLAUSE_SCHEDULE_KIND (c)
6324 = (enum omp_clause_schedule_kind)
6325 (OMP_CLAUSE_SCHEDULE_KIND (c)
6326 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
6330 t = OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c);
6331 if (t == NULL)
6333 else if (t == error_mark_node)
6334 remove = true;
6335 else if (!type_dependent_expression_p (t)
6336 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6338 error ("schedule chunk size expression must be integral");
6339 remove = true;
6341 else
6343 t = mark_rvalue_use (t);
6344 if (!processing_template_decl)
6346 t = maybe_constant_value (t);
6347 if (TREE_CODE (t) == INTEGER_CST
6348 && tree_int_cst_sgn (t) != 1)
6350 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6351 "chunk size value must be positive");
6352 t = integer_one_node;
6354 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6356 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
6358 break;
6360 case OMP_CLAUSE_SIMDLEN:
6361 case OMP_CLAUSE_SAFELEN:
6362 t = OMP_CLAUSE_OPERAND (c, 0);
6363 if (t == error_mark_node)
6364 remove = true;
6365 else if (!type_dependent_expression_p (t)
6366 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6368 error ("%qs length expression must be integral",
6369 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6370 remove = true;
6372 else
6374 t = mark_rvalue_use (t);
6375 if (!processing_template_decl)
6377 t = maybe_constant_value (t);
6378 if (TREE_CODE (t) != INTEGER_CST
6379 || tree_int_cst_sgn (t) != 1)
6381 error ("%qs length expression must be positive constant"
6382 " integer expression",
6383 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6384 remove = true;
6387 OMP_CLAUSE_OPERAND (c, 0) = t;
6388 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SAFELEN)
6389 safelen = c;
6391 break;
6393 case OMP_CLAUSE_ASYNC:
6394 t = OMP_CLAUSE_ASYNC_EXPR (c);
6395 if (t == error_mark_node)
6396 remove = true;
6397 else if (!type_dependent_expression_p (t)
6398 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6400 error ("%<async%> expression must be integral");
6401 remove = true;
6403 else
6405 t = mark_rvalue_use (t);
6406 if (!processing_template_decl)
6407 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6408 OMP_CLAUSE_ASYNC_EXPR (c) = t;
6410 break;
6412 case OMP_CLAUSE_WAIT:
6413 t = OMP_CLAUSE_WAIT_EXPR (c);
6414 if (t == error_mark_node)
6415 remove = true;
6416 else if (!processing_template_decl)
6417 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6418 OMP_CLAUSE_WAIT_EXPR (c) = t;
6419 break;
6421 case OMP_CLAUSE_THREAD_LIMIT:
6422 t = OMP_CLAUSE_THREAD_LIMIT_EXPR (c);
6423 if (t == error_mark_node)
6424 remove = true;
6425 else if (!type_dependent_expression_p (t)
6426 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6428 error ("%<thread_limit%> expression must be integral");
6429 remove = true;
6431 else
6433 t = mark_rvalue_use (t);
6434 if (!processing_template_decl)
6436 t = maybe_constant_value (t);
6437 if (TREE_CODE (t) == INTEGER_CST
6438 && tree_int_cst_sgn (t) != 1)
6440 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6441 "%<thread_limit%> value must be positive");
6442 t = integer_one_node;
6444 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6446 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
6448 break;
6450 case OMP_CLAUSE_DEVICE:
6451 t = OMP_CLAUSE_DEVICE_ID (c);
6452 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 ("%<device%> id must be integral");
6458 remove = true;
6460 else
6462 t = mark_rvalue_use (t);
6463 if (!processing_template_decl)
6464 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6465 OMP_CLAUSE_DEVICE_ID (c) = t;
6467 break;
6469 case OMP_CLAUSE_DIST_SCHEDULE:
6470 t = OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c);
6471 if (t == NULL)
6473 else if (t == error_mark_node)
6474 remove = true;
6475 else if (!type_dependent_expression_p (t)
6476 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6478 error ("%<dist_schedule%> chunk size expression must be "
6479 "integral");
6480 remove = true;
6482 else
6484 t = mark_rvalue_use (t);
6485 if (!processing_template_decl)
6486 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6487 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
6489 break;
6491 case OMP_CLAUSE_ALIGNED:
6492 t = OMP_CLAUSE_DECL (c);
6493 if (t == current_class_ptr && ort != C_ORT_OMP_DECLARE_SIMD)
6495 error ("%<this%> allowed in OpenMP only in %<declare simd%>"
6496 " clauses");
6497 remove = true;
6498 break;
6500 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
6502 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
6503 break;
6504 if (DECL_P (t))
6505 error ("%qD is not a variable in %<aligned%> clause", t);
6506 else
6507 error ("%qE is not a variable in %<aligned%> clause", t);
6508 remove = true;
6510 else if (!type_dependent_expression_p (t)
6511 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE
6512 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE
6513 && (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE
6514 || (!POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (t)))
6515 && (TREE_CODE (TREE_TYPE (TREE_TYPE (t)))
6516 != ARRAY_TYPE))))
6518 error_at (OMP_CLAUSE_LOCATION (c),
6519 "%qE in %<aligned%> clause is neither a pointer nor "
6520 "an array nor a reference to pointer or array", t);
6521 remove = true;
6523 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
6525 error ("%qD appears more than once in %<aligned%> clauses", t);
6526 remove = true;
6528 else
6529 bitmap_set_bit (&aligned_head, DECL_UID (t));
6530 t = OMP_CLAUSE_ALIGNED_ALIGNMENT (c);
6531 if (t == error_mark_node)
6532 remove = true;
6533 else if (t == NULL_TREE)
6534 break;
6535 else if (!type_dependent_expression_p (t)
6536 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6538 error ("%<aligned%> clause alignment expression must "
6539 "be integral");
6540 remove = true;
6542 else
6544 t = mark_rvalue_use (t);
6545 if (!processing_template_decl)
6547 t = maybe_constant_value (t);
6548 if (TREE_CODE (t) != INTEGER_CST
6549 || tree_int_cst_sgn (t) != 1)
6551 error ("%<aligned%> clause alignment expression must be "
6552 "positive constant integer expression");
6553 remove = true;
6556 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = t;
6558 break;
6560 case OMP_CLAUSE_DEPEND:
6561 t = OMP_CLAUSE_DECL (c);
6562 if (t == NULL_TREE)
6564 gcc_assert (OMP_CLAUSE_DEPEND_KIND (c)
6565 == OMP_CLAUSE_DEPEND_SOURCE);
6566 break;
6568 if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SINK)
6570 if (cp_finish_omp_clause_depend_sink (c))
6571 remove = true;
6572 break;
6574 if (TREE_CODE (t) == TREE_LIST)
6576 if (handle_omp_array_sections (c, ort))
6577 remove = true;
6578 break;
6580 if (t == error_mark_node)
6581 remove = true;
6582 else if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
6584 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
6585 break;
6586 if (DECL_P (t))
6587 error ("%qD is not a variable in %<depend%> clause", t);
6588 else
6589 error ("%qE is not a variable in %<depend%> clause", t);
6590 remove = true;
6592 else if (t == current_class_ptr)
6594 error ("%<this%> allowed in OpenMP only in %<declare simd%>"
6595 " clauses");
6596 remove = true;
6598 else if (!processing_template_decl
6599 && !cxx_mark_addressable (t))
6600 remove = true;
6601 break;
6603 case OMP_CLAUSE_MAP:
6604 case OMP_CLAUSE_TO:
6605 case OMP_CLAUSE_FROM:
6606 case OMP_CLAUSE__CACHE_:
6607 t = OMP_CLAUSE_DECL (c);
6608 if (TREE_CODE (t) == TREE_LIST)
6610 if (handle_omp_array_sections (c, ort))
6611 remove = true;
6612 else
6614 t = OMP_CLAUSE_DECL (c);
6615 if (TREE_CODE (t) != TREE_LIST
6616 && !type_dependent_expression_p (t)
6617 && !cp_omp_mappable_type (TREE_TYPE (t)))
6619 error_at (OMP_CLAUSE_LOCATION (c),
6620 "array section does not have mappable type "
6621 "in %qs clause",
6622 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6623 remove = true;
6625 while (TREE_CODE (t) == ARRAY_REF)
6626 t = TREE_OPERAND (t, 0);
6627 if (TREE_CODE (t) == COMPONENT_REF
6628 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
6630 while (TREE_CODE (t) == COMPONENT_REF)
6631 t = TREE_OPERAND (t, 0);
6632 if (REFERENCE_REF_P (t))
6633 t = TREE_OPERAND (t, 0);
6634 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
6635 break;
6636 if (bitmap_bit_p (&map_head, DECL_UID (t)))
6638 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
6639 error ("%qD appears more than once in motion"
6640 " clauses", t);
6641 else if (ort == C_ORT_ACC)
6642 error ("%qD appears more than once in data"
6643 " clauses", t);
6644 else
6645 error ("%qD appears more than once in map"
6646 " clauses", t);
6647 remove = true;
6649 else
6651 bitmap_set_bit (&map_head, DECL_UID (t));
6652 bitmap_set_bit (&map_field_head, DECL_UID (t));
6656 break;
6658 if (t == error_mark_node)
6660 remove = true;
6661 break;
6663 if (REFERENCE_REF_P (t)
6664 && TREE_CODE (TREE_OPERAND (t, 0)) == COMPONENT_REF)
6666 t = TREE_OPERAND (t, 0);
6667 OMP_CLAUSE_DECL (c) = t;
6669 if (TREE_CODE (t) == COMPONENT_REF
6670 && (ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP
6671 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE__CACHE_)
6673 if (type_dependent_expression_p (t))
6674 break;
6675 if (TREE_CODE (TREE_OPERAND (t, 1)) == FIELD_DECL
6676 && DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
6678 error_at (OMP_CLAUSE_LOCATION (c),
6679 "bit-field %qE in %qs clause",
6680 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6681 remove = true;
6683 else if (!cp_omp_mappable_type (TREE_TYPE (t)))
6685 error_at (OMP_CLAUSE_LOCATION (c),
6686 "%qE does not have a mappable type in %qs clause",
6687 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6688 remove = true;
6690 while (TREE_CODE (t) == COMPONENT_REF)
6692 if (TREE_TYPE (TREE_OPERAND (t, 0))
6693 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0)))
6694 == UNION_TYPE))
6696 error_at (OMP_CLAUSE_LOCATION (c),
6697 "%qE is a member of a union", t);
6698 remove = true;
6699 break;
6701 t = TREE_OPERAND (t, 0);
6703 if (remove)
6704 break;
6705 if (REFERENCE_REF_P (t))
6706 t = TREE_OPERAND (t, 0);
6707 if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
6709 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
6710 goto handle_map_references;
6713 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
6715 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
6716 break;
6717 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
6718 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
6719 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ALWAYS_POINTER))
6720 break;
6721 if (DECL_P (t))
6722 error ("%qD is not a variable in %qs clause", t,
6723 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6724 else
6725 error ("%qE is not a variable in %qs clause", t,
6726 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6727 remove = true;
6729 else if (VAR_P (t) && CP_DECL_THREAD_LOCAL_P (t))
6731 error ("%qD is threadprivate variable in %qs clause", t,
6732 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6733 remove = true;
6735 else if (ort != C_ORT_ACC && t == current_class_ptr)
6737 error ("%<this%> allowed in OpenMP only in %<declare simd%>"
6738 " clauses");
6739 remove = true;
6740 break;
6742 else if (!processing_template_decl
6743 && TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE
6744 && (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
6745 || (OMP_CLAUSE_MAP_KIND (c)
6746 != GOMP_MAP_FIRSTPRIVATE_POINTER))
6747 && !cxx_mark_addressable (t))
6748 remove = true;
6749 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
6750 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
6751 || (OMP_CLAUSE_MAP_KIND (c)
6752 == GOMP_MAP_FIRSTPRIVATE_POINTER)))
6753 && t == OMP_CLAUSE_DECL (c)
6754 && !type_dependent_expression_p (t)
6755 && !cp_omp_mappable_type ((TREE_CODE (TREE_TYPE (t))
6756 == REFERENCE_TYPE)
6757 ? TREE_TYPE (TREE_TYPE (t))
6758 : TREE_TYPE (t)))
6760 error_at (OMP_CLAUSE_LOCATION (c),
6761 "%qD does not have a mappable type in %qs clause", t,
6762 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6763 remove = true;
6765 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
6766 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FORCE_DEVICEPTR
6767 && !type_dependent_expression_p (t)
6768 && !POINTER_TYPE_P (TREE_TYPE (t)))
6770 error ("%qD is not a pointer variable", t);
6771 remove = true;
6773 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
6774 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
6776 if (bitmap_bit_p (&generic_head, DECL_UID (t))
6777 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
6779 error ("%qD appears more than once in data clauses", t);
6780 remove = true;
6782 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
6784 if (ort == C_ORT_ACC)
6785 error ("%qD appears more than once in data clauses", t);
6786 else
6787 error ("%qD appears both in data and map clauses", t);
6788 remove = true;
6790 else
6791 bitmap_set_bit (&generic_head, DECL_UID (t));
6793 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
6795 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
6796 error ("%qD appears more than once in motion clauses", t);
6797 if (ort == C_ORT_ACC)
6798 error ("%qD appears more than once in data clauses", t);
6799 else
6800 error ("%qD appears more than once in map clauses", t);
6801 remove = true;
6803 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
6804 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
6806 if (ort == C_ORT_ACC)
6807 error ("%qD appears more than once in data clauses", t);
6808 else
6809 error ("%qD appears both in data and map clauses", t);
6810 remove = true;
6812 else
6814 bitmap_set_bit (&map_head, DECL_UID (t));
6815 if (t != OMP_CLAUSE_DECL (c)
6816 && TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPONENT_REF)
6817 bitmap_set_bit (&map_field_head, DECL_UID (t));
6819 handle_map_references:
6820 if (!remove
6821 && !processing_template_decl
6822 && (ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP
6823 && TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c))) == REFERENCE_TYPE)
6825 t = OMP_CLAUSE_DECL (c);
6826 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
6828 OMP_CLAUSE_DECL (c) = build_simple_mem_ref (t);
6829 if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
6830 OMP_CLAUSE_SIZE (c)
6831 = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t)));
6833 else if (OMP_CLAUSE_MAP_KIND (c)
6834 != GOMP_MAP_FIRSTPRIVATE_POINTER
6835 && (OMP_CLAUSE_MAP_KIND (c)
6836 != GOMP_MAP_FIRSTPRIVATE_REFERENCE)
6837 && (OMP_CLAUSE_MAP_KIND (c)
6838 != GOMP_MAP_ALWAYS_POINTER))
6840 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
6841 OMP_CLAUSE_MAP);
6842 if (TREE_CODE (t) == COMPONENT_REF)
6843 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ALWAYS_POINTER);
6844 else
6845 OMP_CLAUSE_SET_MAP_KIND (c2,
6846 GOMP_MAP_FIRSTPRIVATE_REFERENCE);
6847 OMP_CLAUSE_DECL (c2) = t;
6848 OMP_CLAUSE_SIZE (c2) = size_zero_node;
6849 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
6850 OMP_CLAUSE_CHAIN (c) = c2;
6851 OMP_CLAUSE_DECL (c) = build_simple_mem_ref (t);
6852 if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
6853 OMP_CLAUSE_SIZE (c)
6854 = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t)));
6855 c = c2;
6858 break;
6860 case OMP_CLAUSE_TO_DECLARE:
6861 case OMP_CLAUSE_LINK:
6862 t = OMP_CLAUSE_DECL (c);
6863 if (TREE_CODE (t) == FUNCTION_DECL
6864 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
6866 else if (!VAR_P (t))
6868 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
6870 if (TREE_CODE (t) == TEMPLATE_ID_EXPR)
6871 error_at (OMP_CLAUSE_LOCATION (c),
6872 "template %qE in clause %qs", t,
6873 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6874 else if (really_overloaded_fn (t))
6875 error_at (OMP_CLAUSE_LOCATION (c),
6876 "overloaded function name %qE in clause %qs", t,
6877 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6878 else
6879 error_at (OMP_CLAUSE_LOCATION (c),
6880 "%qE is neither a variable nor a function name "
6881 "in clause %qs", t,
6882 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6884 else
6885 error_at (OMP_CLAUSE_LOCATION (c),
6886 "%qE is not a variable in clause %qs", t,
6887 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6888 remove = true;
6890 else if (DECL_THREAD_LOCAL_P (t))
6892 error_at (OMP_CLAUSE_LOCATION (c),
6893 "%qD is threadprivate variable in %qs clause", t,
6894 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6895 remove = true;
6897 else if (!cp_omp_mappable_type (TREE_TYPE (t)))
6899 error_at (OMP_CLAUSE_LOCATION (c),
6900 "%qD does not have a mappable type in %qs clause", t,
6901 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6902 remove = true;
6904 if (remove)
6905 break;
6906 if (bitmap_bit_p (&generic_head, DECL_UID (t)))
6908 error_at (OMP_CLAUSE_LOCATION (c),
6909 "%qE appears more than once on the same "
6910 "%<declare target%> directive", t);
6911 remove = true;
6913 else
6914 bitmap_set_bit (&generic_head, DECL_UID (t));
6915 break;
6917 case OMP_CLAUSE_UNIFORM:
6918 t = OMP_CLAUSE_DECL (c);
6919 if (TREE_CODE (t) != PARM_DECL)
6921 if (processing_template_decl)
6922 break;
6923 if (DECL_P (t))
6924 error ("%qD is not an argument in %<uniform%> clause", t);
6925 else
6926 error ("%qE is not an argument in %<uniform%> clause", t);
6927 remove = true;
6928 break;
6930 /* map_head bitmap is used as uniform_head if declare_simd. */
6931 bitmap_set_bit (&map_head, DECL_UID (t));
6932 goto check_dup_generic;
6934 case OMP_CLAUSE_GRAINSIZE:
6935 t = OMP_CLAUSE_GRAINSIZE_EXPR (c);
6936 if (t == error_mark_node)
6937 remove = true;
6938 else if (!type_dependent_expression_p (t)
6939 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6941 error ("%<grainsize%> expression must be integral");
6942 remove = true;
6944 else
6946 t = mark_rvalue_use (t);
6947 if (!processing_template_decl)
6949 t = maybe_constant_value (t);
6950 if (TREE_CODE (t) == INTEGER_CST
6951 && tree_int_cst_sgn (t) != 1)
6953 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6954 "%<grainsize%> value must be positive");
6955 t = integer_one_node;
6957 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6959 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
6961 break;
6963 case OMP_CLAUSE_PRIORITY:
6964 t = OMP_CLAUSE_PRIORITY_EXPR (c);
6965 if (t == error_mark_node)
6966 remove = true;
6967 else if (!type_dependent_expression_p (t)
6968 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6970 error ("%<priority%> expression must be integral");
6971 remove = true;
6973 else
6975 t = mark_rvalue_use (t);
6976 if (!processing_template_decl)
6978 t = maybe_constant_value (t);
6979 if (TREE_CODE (t) == INTEGER_CST
6980 && tree_int_cst_sgn (t) == -1)
6982 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6983 "%<priority%> value must be non-negative");
6984 t = integer_one_node;
6986 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6988 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
6990 break;
6992 case OMP_CLAUSE_HINT:
6993 t = OMP_CLAUSE_HINT_EXPR (c);
6994 if (t == error_mark_node)
6995 remove = true;
6996 else if (!type_dependent_expression_p (t)
6997 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6999 error ("%<num_tasks%> expression must be integral");
7000 remove = true;
7002 else
7004 t = mark_rvalue_use (t);
7005 if (!processing_template_decl)
7007 t = maybe_constant_value (t);
7008 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7010 OMP_CLAUSE_HINT_EXPR (c) = t;
7012 break;
7014 case OMP_CLAUSE_IS_DEVICE_PTR:
7015 case OMP_CLAUSE_USE_DEVICE_PTR:
7016 field_ok = (ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP;
7017 t = OMP_CLAUSE_DECL (c);
7018 if (!type_dependent_expression_p (t))
7020 tree type = TREE_TYPE (t);
7021 if (TREE_CODE (type) != POINTER_TYPE
7022 && TREE_CODE (type) != ARRAY_TYPE
7023 && (TREE_CODE (type) != REFERENCE_TYPE
7024 || (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
7025 && TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE)))
7027 error_at (OMP_CLAUSE_LOCATION (c),
7028 "%qs variable is neither a pointer, nor an array "
7029 "nor reference to pointer or array",
7030 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7031 remove = true;
7034 goto check_dup_generic;
7036 case OMP_CLAUSE_NOWAIT:
7037 case OMP_CLAUSE_DEFAULT:
7038 case OMP_CLAUSE_UNTIED:
7039 case OMP_CLAUSE_COLLAPSE:
7040 case OMP_CLAUSE_MERGEABLE:
7041 case OMP_CLAUSE_PARALLEL:
7042 case OMP_CLAUSE_FOR:
7043 case OMP_CLAUSE_SECTIONS:
7044 case OMP_CLAUSE_TASKGROUP:
7045 case OMP_CLAUSE_PROC_BIND:
7046 case OMP_CLAUSE_NOGROUP:
7047 case OMP_CLAUSE_THREADS:
7048 case OMP_CLAUSE_SIMD:
7049 case OMP_CLAUSE_DEFAULTMAP:
7050 case OMP_CLAUSE_AUTO:
7051 case OMP_CLAUSE_INDEPENDENT:
7052 case OMP_CLAUSE_SEQ:
7053 break;
7055 case OMP_CLAUSE_TILE:
7056 for (tree list = OMP_CLAUSE_TILE_LIST (c); !remove && list;
7057 list = TREE_CHAIN (list))
7059 t = TREE_VALUE (list);
7061 if (t == error_mark_node)
7062 remove = true;
7063 else if (!type_dependent_expression_p (t)
7064 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
7066 error_at (OMP_CLAUSE_LOCATION (c),
7067 "%<tile%> argument needs integral type");
7068 remove = true;
7070 else
7072 t = mark_rvalue_use (t);
7073 if (!processing_template_decl)
7075 /* Zero is used to indicate '*', we permit you
7076 to get there via an ICE of value zero. */
7077 t = maybe_constant_value (t);
7078 if (!tree_fits_shwi_p (t)
7079 || tree_to_shwi (t) < 0)
7081 error_at (OMP_CLAUSE_LOCATION (c),
7082 "%<tile%> argument needs positive "
7083 "integral constant");
7084 remove = true;
7086 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7090 /* Update list item. */
7091 TREE_VALUE (list) = t;
7093 break;
7095 case OMP_CLAUSE_ORDERED:
7096 ordered_seen = true;
7097 break;
7099 case OMP_CLAUSE_INBRANCH:
7100 case OMP_CLAUSE_NOTINBRANCH:
7101 if (branch_seen)
7103 error ("%<inbranch%> clause is incompatible with "
7104 "%<notinbranch%>");
7105 remove = true;
7107 branch_seen = true;
7108 break;
7110 default:
7111 gcc_unreachable ();
7114 if (remove)
7115 *pc = OMP_CLAUSE_CHAIN (c);
7116 else
7117 pc = &OMP_CLAUSE_CHAIN (c);
7120 for (pc = &clauses, c = clauses; c ; c = *pc)
7122 enum omp_clause_code c_kind = OMP_CLAUSE_CODE (c);
7123 bool remove = false;
7124 bool need_complete_type = false;
7125 bool need_default_ctor = false;
7126 bool need_copy_ctor = false;
7127 bool need_copy_assignment = false;
7128 bool need_implicitly_determined = false;
7129 bool need_dtor = false;
7130 tree type, inner_type;
7132 switch (c_kind)
7134 case OMP_CLAUSE_SHARED:
7135 need_implicitly_determined = true;
7136 break;
7137 case OMP_CLAUSE_PRIVATE:
7138 need_complete_type = true;
7139 need_default_ctor = true;
7140 need_dtor = true;
7141 need_implicitly_determined = true;
7142 break;
7143 case OMP_CLAUSE_FIRSTPRIVATE:
7144 need_complete_type = true;
7145 need_copy_ctor = true;
7146 need_dtor = true;
7147 need_implicitly_determined = true;
7148 break;
7149 case OMP_CLAUSE_LASTPRIVATE:
7150 need_complete_type = true;
7151 need_copy_assignment = true;
7152 need_implicitly_determined = true;
7153 break;
7154 case OMP_CLAUSE_REDUCTION:
7155 need_implicitly_determined = true;
7156 break;
7157 case OMP_CLAUSE_LINEAR:
7158 if (ort != C_ORT_OMP_DECLARE_SIMD)
7159 need_implicitly_determined = true;
7160 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c)
7161 && !bitmap_bit_p (&map_head,
7162 DECL_UID (OMP_CLAUSE_LINEAR_STEP (c))))
7164 error_at (OMP_CLAUSE_LOCATION (c),
7165 "%<linear%> clause step is a parameter %qD not "
7166 "specified in %<uniform%> clause",
7167 OMP_CLAUSE_LINEAR_STEP (c));
7168 *pc = OMP_CLAUSE_CHAIN (c);
7169 continue;
7171 break;
7172 case OMP_CLAUSE_COPYPRIVATE:
7173 need_copy_assignment = true;
7174 break;
7175 case OMP_CLAUSE_COPYIN:
7176 need_copy_assignment = true;
7177 break;
7178 case OMP_CLAUSE_SIMDLEN:
7179 if (safelen
7180 && !processing_template_decl
7181 && tree_int_cst_lt (OMP_CLAUSE_SAFELEN_EXPR (safelen),
7182 OMP_CLAUSE_SIMDLEN_EXPR (c)))
7184 error_at (OMP_CLAUSE_LOCATION (c),
7185 "%<simdlen%> clause value is bigger than "
7186 "%<safelen%> clause value");
7187 OMP_CLAUSE_SIMDLEN_EXPR (c)
7188 = OMP_CLAUSE_SAFELEN_EXPR (safelen);
7190 pc = &OMP_CLAUSE_CHAIN (c);
7191 continue;
7192 case OMP_CLAUSE_SCHEDULE:
7193 if (ordered_seen
7194 && (OMP_CLAUSE_SCHEDULE_KIND (c)
7195 & OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
7197 error_at (OMP_CLAUSE_LOCATION (c),
7198 "%<nonmonotonic%> schedule modifier specified "
7199 "together with %<ordered%> clause");
7200 OMP_CLAUSE_SCHEDULE_KIND (c)
7201 = (enum omp_clause_schedule_kind)
7202 (OMP_CLAUSE_SCHEDULE_KIND (c)
7203 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
7205 pc = &OMP_CLAUSE_CHAIN (c);
7206 continue;
7207 case OMP_CLAUSE_NOWAIT:
7208 if (copyprivate_seen)
7210 error_at (OMP_CLAUSE_LOCATION (c),
7211 "%<nowait%> clause must not be used together "
7212 "with %<copyprivate%>");
7213 *pc = OMP_CLAUSE_CHAIN (c);
7214 continue;
7216 /* FALLTHRU */
7217 default:
7218 pc = &OMP_CLAUSE_CHAIN (c);
7219 continue;
7222 t = OMP_CLAUSE_DECL (c);
7223 if (processing_template_decl
7224 && !VAR_P (t) && TREE_CODE (t) != PARM_DECL)
7226 pc = &OMP_CLAUSE_CHAIN (c);
7227 continue;
7230 switch (c_kind)
7232 case OMP_CLAUSE_LASTPRIVATE:
7233 if (!bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
7235 need_default_ctor = true;
7236 need_dtor = true;
7238 break;
7240 case OMP_CLAUSE_REDUCTION:
7241 if (finish_omp_reduction_clause (c, &need_default_ctor,
7242 &need_dtor))
7243 remove = true;
7244 else
7245 t = OMP_CLAUSE_DECL (c);
7246 break;
7248 case OMP_CLAUSE_COPYIN:
7249 if (!VAR_P (t) || !CP_DECL_THREAD_LOCAL_P (t))
7251 error ("%qE must be %<threadprivate%> for %<copyin%>", t);
7252 remove = true;
7254 break;
7256 default:
7257 break;
7260 if (need_complete_type || need_copy_assignment)
7262 t = require_complete_type (t);
7263 if (t == error_mark_node)
7264 remove = true;
7265 else if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE
7266 && !complete_type_or_else (TREE_TYPE (TREE_TYPE (t)), t))
7267 remove = true;
7269 if (need_implicitly_determined)
7271 const char *share_name = NULL;
7273 if (VAR_P (t) && CP_DECL_THREAD_LOCAL_P (t))
7274 share_name = "threadprivate";
7275 else switch (cxx_omp_predetermined_sharing (t))
7277 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
7278 break;
7279 case OMP_CLAUSE_DEFAULT_SHARED:
7280 /* const vars may be specified in firstprivate clause. */
7281 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
7282 && cxx_omp_const_qual_no_mutable (t))
7283 break;
7284 share_name = "shared";
7285 break;
7286 case OMP_CLAUSE_DEFAULT_PRIVATE:
7287 share_name = "private";
7288 break;
7289 default:
7290 gcc_unreachable ();
7292 if (share_name)
7294 error ("%qE is predetermined %qs for %qs",
7295 omp_clause_printable_decl (t), share_name,
7296 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7297 remove = true;
7301 /* We're interested in the base element, not arrays. */
7302 inner_type = type = TREE_TYPE (t);
7303 if ((need_complete_type
7304 || need_copy_assignment
7305 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
7306 && TREE_CODE (inner_type) == REFERENCE_TYPE)
7307 inner_type = TREE_TYPE (inner_type);
7308 while (TREE_CODE (inner_type) == ARRAY_TYPE)
7309 inner_type = TREE_TYPE (inner_type);
7311 /* Check for special function availability by building a call to one.
7312 Save the results, because later we won't be in the right context
7313 for making these queries. */
7314 if (CLASS_TYPE_P (inner_type)
7315 && COMPLETE_TYPE_P (inner_type)
7316 && (need_default_ctor || need_copy_ctor
7317 || need_copy_assignment || need_dtor)
7318 && !type_dependent_expression_p (t)
7319 && cxx_omp_create_clause_info (c, inner_type, need_default_ctor,
7320 need_copy_ctor, need_copy_assignment,
7321 need_dtor))
7322 remove = true;
7324 if (!remove
7325 && c_kind == OMP_CLAUSE_SHARED
7326 && processing_template_decl)
7328 t = omp_clause_decl_field (OMP_CLAUSE_DECL (c));
7329 if (t)
7330 OMP_CLAUSE_DECL (c) = t;
7333 if (remove)
7334 *pc = OMP_CLAUSE_CHAIN (c);
7335 else
7336 pc = &OMP_CLAUSE_CHAIN (c);
7339 bitmap_obstack_release (NULL);
7340 return clauses;
7343 /* Start processing OpenMP clauses that can include any
7344 privatization clauses for non-static data members. */
7346 tree
7347 push_omp_privatization_clauses (bool ignore_next)
7349 if (omp_private_member_ignore_next)
7351 omp_private_member_ignore_next = ignore_next;
7352 return NULL_TREE;
7354 omp_private_member_ignore_next = ignore_next;
7355 if (omp_private_member_map)
7356 omp_private_member_vec.safe_push (error_mark_node);
7357 return push_stmt_list ();
7360 /* Revert remapping of any non-static data members since
7361 the last push_omp_privatization_clauses () call. */
7363 void
7364 pop_omp_privatization_clauses (tree stmt)
7366 if (stmt == NULL_TREE)
7367 return;
7368 stmt = pop_stmt_list (stmt);
7369 if (omp_private_member_map)
7371 while (!omp_private_member_vec.is_empty ())
7373 tree t = omp_private_member_vec.pop ();
7374 if (t == error_mark_node)
7376 add_stmt (stmt);
7377 return;
7379 bool no_decl_expr = t == integer_zero_node;
7380 if (no_decl_expr)
7381 t = omp_private_member_vec.pop ();
7382 tree *v = omp_private_member_map->get (t);
7383 gcc_assert (v);
7384 if (!no_decl_expr)
7385 add_decl_expr (*v);
7386 omp_private_member_map->remove (t);
7388 delete omp_private_member_map;
7389 omp_private_member_map = NULL;
7391 add_stmt (stmt);
7394 /* Remember OpenMP privatization clauses mapping and clear it.
7395 Used for lambdas. */
7397 void
7398 save_omp_privatization_clauses (vec<tree> &save)
7400 save = vNULL;
7401 if (omp_private_member_ignore_next)
7402 save.safe_push (integer_one_node);
7403 omp_private_member_ignore_next = false;
7404 if (!omp_private_member_map)
7405 return;
7407 while (!omp_private_member_vec.is_empty ())
7409 tree t = omp_private_member_vec.pop ();
7410 if (t == error_mark_node)
7412 save.safe_push (t);
7413 continue;
7415 tree n = t;
7416 if (t == integer_zero_node)
7417 t = omp_private_member_vec.pop ();
7418 tree *v = omp_private_member_map->get (t);
7419 gcc_assert (v);
7420 save.safe_push (*v);
7421 save.safe_push (t);
7422 if (n != t)
7423 save.safe_push (n);
7425 delete omp_private_member_map;
7426 omp_private_member_map = NULL;
7429 /* Restore OpenMP privatization clauses mapping saved by the
7430 above function. */
7432 void
7433 restore_omp_privatization_clauses (vec<tree> &save)
7435 gcc_assert (omp_private_member_vec.is_empty ());
7436 omp_private_member_ignore_next = false;
7437 if (save.is_empty ())
7438 return;
7439 if (save.length () == 1 && save[0] == integer_one_node)
7441 omp_private_member_ignore_next = true;
7442 save.release ();
7443 return;
7446 omp_private_member_map = new hash_map <tree, tree>;
7447 while (!save.is_empty ())
7449 tree t = save.pop ();
7450 tree n = t;
7451 if (t != error_mark_node)
7453 if (t == integer_one_node)
7455 omp_private_member_ignore_next = true;
7456 gcc_assert (save.is_empty ());
7457 break;
7459 if (t == integer_zero_node)
7460 t = save.pop ();
7461 tree &v = omp_private_member_map->get_or_insert (t);
7462 v = save.pop ();
7464 omp_private_member_vec.safe_push (t);
7465 if (n != t)
7466 omp_private_member_vec.safe_push (n);
7468 save.release ();
7471 /* For all variables in the tree_list VARS, mark them as thread local. */
7473 void
7474 finish_omp_threadprivate (tree vars)
7476 tree t;
7478 /* Mark every variable in VARS to be assigned thread local storage. */
7479 for (t = vars; t; t = TREE_CHAIN (t))
7481 tree v = TREE_PURPOSE (t);
7483 if (error_operand_p (v))
7485 else if (!VAR_P (v))
7486 error ("%<threadprivate%> %qD is not file, namespace "
7487 "or block scope variable", v);
7488 /* If V had already been marked threadprivate, it doesn't matter
7489 whether it had been used prior to this point. */
7490 else if (TREE_USED (v)
7491 && (DECL_LANG_SPECIFIC (v) == NULL
7492 || !CP_DECL_THREADPRIVATE_P (v)))
7493 error ("%qE declared %<threadprivate%> after first use", v);
7494 else if (! TREE_STATIC (v) && ! DECL_EXTERNAL (v))
7495 error ("automatic variable %qE cannot be %<threadprivate%>", v);
7496 else if (! COMPLETE_TYPE_P (complete_type (TREE_TYPE (v))))
7497 error ("%<threadprivate%> %qE has incomplete type", v);
7498 else if (TREE_STATIC (v) && TYPE_P (CP_DECL_CONTEXT (v))
7499 && CP_DECL_CONTEXT (v) != current_class_type)
7500 error ("%<threadprivate%> %qE directive not "
7501 "in %qT definition", v, CP_DECL_CONTEXT (v));
7502 else
7504 /* Allocate a LANG_SPECIFIC structure for V, if needed. */
7505 if (DECL_LANG_SPECIFIC (v) == NULL)
7507 retrofit_lang_decl (v);
7509 /* Make sure that DECL_DISCRIMINATOR_P continues to be true
7510 after the allocation of the lang_decl structure. */
7511 if (DECL_DISCRIMINATOR_P (v))
7512 DECL_LANG_SPECIFIC (v)->u.base.u2sel = 1;
7515 if (! CP_DECL_THREAD_LOCAL_P (v))
7517 CP_DECL_THREAD_LOCAL_P (v) = true;
7518 set_decl_tls_model (v, decl_default_tls_model (v));
7519 /* If rtl has been already set for this var, call
7520 make_decl_rtl once again, so that encode_section_info
7521 has a chance to look at the new decl flags. */
7522 if (DECL_RTL_SET_P (v))
7523 make_decl_rtl (v);
7525 CP_DECL_THREADPRIVATE_P (v) = 1;
7530 /* Build an OpenMP structured block. */
7532 tree
7533 begin_omp_structured_block (void)
7535 return do_pushlevel (sk_omp);
7538 tree
7539 finish_omp_structured_block (tree block)
7541 return do_poplevel (block);
7544 /* Similarly, except force the retention of the BLOCK. */
7546 tree
7547 begin_omp_parallel (void)
7549 keep_next_level (true);
7550 return begin_omp_structured_block ();
7553 /* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
7554 statement. */
7556 tree
7557 finish_oacc_data (tree clauses, tree block)
7559 tree stmt;
7561 block = finish_omp_structured_block (block);
7563 stmt = make_node (OACC_DATA);
7564 TREE_TYPE (stmt) = void_type_node;
7565 OACC_DATA_CLAUSES (stmt) = clauses;
7566 OACC_DATA_BODY (stmt) = block;
7568 return add_stmt (stmt);
7571 /* Generate OACC_HOST_DATA, with CLAUSES and BLOCK as its compound
7572 statement. */
7574 tree
7575 finish_oacc_host_data (tree clauses, tree block)
7577 tree stmt;
7579 block = finish_omp_structured_block (block);
7581 stmt = make_node (OACC_HOST_DATA);
7582 TREE_TYPE (stmt) = void_type_node;
7583 OACC_HOST_DATA_CLAUSES (stmt) = clauses;
7584 OACC_HOST_DATA_BODY (stmt) = block;
7586 return add_stmt (stmt);
7589 /* Generate OMP construct CODE, with BODY and CLAUSES as its compound
7590 statement. */
7592 tree
7593 finish_omp_construct (enum tree_code code, tree body, tree clauses)
7595 body = finish_omp_structured_block (body);
7597 tree stmt = make_node (code);
7598 TREE_TYPE (stmt) = void_type_node;
7599 OMP_BODY (stmt) = body;
7600 OMP_CLAUSES (stmt) = clauses;
7602 return add_stmt (stmt);
7605 tree
7606 finish_omp_parallel (tree clauses, tree body)
7608 tree stmt;
7610 body = finish_omp_structured_block (body);
7612 stmt = make_node (OMP_PARALLEL);
7613 TREE_TYPE (stmt) = void_type_node;
7614 OMP_PARALLEL_CLAUSES (stmt) = clauses;
7615 OMP_PARALLEL_BODY (stmt) = body;
7617 return add_stmt (stmt);
7620 tree
7621 begin_omp_task (void)
7623 keep_next_level (true);
7624 return begin_omp_structured_block ();
7627 tree
7628 finish_omp_task (tree clauses, tree body)
7630 tree stmt;
7632 body = finish_omp_structured_block (body);
7634 stmt = make_node (OMP_TASK);
7635 TREE_TYPE (stmt) = void_type_node;
7636 OMP_TASK_CLAUSES (stmt) = clauses;
7637 OMP_TASK_BODY (stmt) = body;
7639 return add_stmt (stmt);
7642 /* Helper function for finish_omp_for. Convert Ith random access iterator
7643 into integral iterator. Return FALSE if successful. */
7645 static bool
7646 handle_omp_for_class_iterator (int i, location_t locus, enum tree_code code,
7647 tree declv, tree orig_declv, tree initv,
7648 tree condv, tree incrv, tree *body,
7649 tree *pre_body, tree &clauses, tree *lastp,
7650 int collapse, int ordered)
7652 tree diff, iter_init, iter_incr = NULL, last;
7653 tree incr_var = NULL, orig_pre_body, orig_body, c;
7654 tree decl = TREE_VEC_ELT (declv, i);
7655 tree init = TREE_VEC_ELT (initv, i);
7656 tree cond = TREE_VEC_ELT (condv, i);
7657 tree incr = TREE_VEC_ELT (incrv, i);
7658 tree iter = decl;
7659 location_t elocus = locus;
7661 if (init && EXPR_HAS_LOCATION (init))
7662 elocus = EXPR_LOCATION (init);
7664 cond = cp_fully_fold (cond);
7665 switch (TREE_CODE (cond))
7667 case GT_EXPR:
7668 case GE_EXPR:
7669 case LT_EXPR:
7670 case LE_EXPR:
7671 case NE_EXPR:
7672 if (TREE_OPERAND (cond, 1) == iter)
7673 cond = build2 (swap_tree_comparison (TREE_CODE (cond)),
7674 TREE_TYPE (cond), iter, TREE_OPERAND (cond, 0));
7675 if (TREE_OPERAND (cond, 0) != iter)
7676 cond = error_mark_node;
7677 else
7679 tree tem = build_x_binary_op (EXPR_LOCATION (cond),
7680 TREE_CODE (cond),
7681 iter, ERROR_MARK,
7682 TREE_OPERAND (cond, 1), ERROR_MARK,
7683 NULL, tf_warning_or_error);
7684 if (error_operand_p (tem))
7685 return true;
7687 break;
7688 default:
7689 cond = error_mark_node;
7690 break;
7692 if (cond == error_mark_node)
7694 error_at (elocus, "invalid controlling predicate");
7695 return true;
7697 diff = build_x_binary_op (elocus, MINUS_EXPR, TREE_OPERAND (cond, 1),
7698 ERROR_MARK, iter, ERROR_MARK, NULL,
7699 tf_warning_or_error);
7700 diff = cp_fully_fold (diff);
7701 if (error_operand_p (diff))
7702 return true;
7703 if (TREE_CODE (TREE_TYPE (diff)) != INTEGER_TYPE)
7705 error_at (elocus, "difference between %qE and %qD does not have integer type",
7706 TREE_OPERAND (cond, 1), iter);
7707 return true;
7709 if (!c_omp_check_loop_iv_exprs (locus, orig_declv,
7710 TREE_VEC_ELT (declv, i), NULL_TREE,
7711 cond, cp_walk_subtrees))
7712 return true;
7714 switch (TREE_CODE (incr))
7716 case PREINCREMENT_EXPR:
7717 case PREDECREMENT_EXPR:
7718 case POSTINCREMENT_EXPR:
7719 case POSTDECREMENT_EXPR:
7720 if (TREE_OPERAND (incr, 0) != iter)
7722 incr = error_mark_node;
7723 break;
7725 iter_incr = build_x_unary_op (EXPR_LOCATION (incr),
7726 TREE_CODE (incr), iter,
7727 tf_warning_or_error);
7728 if (error_operand_p (iter_incr))
7729 return true;
7730 else if (TREE_CODE (incr) == PREINCREMENT_EXPR
7731 || TREE_CODE (incr) == POSTINCREMENT_EXPR)
7732 incr = integer_one_node;
7733 else
7734 incr = integer_minus_one_node;
7735 break;
7736 case MODIFY_EXPR:
7737 if (TREE_OPERAND (incr, 0) != iter)
7738 incr = error_mark_node;
7739 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
7740 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
7742 tree rhs = TREE_OPERAND (incr, 1);
7743 if (TREE_OPERAND (rhs, 0) == iter)
7745 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (rhs, 1)))
7746 != INTEGER_TYPE)
7747 incr = error_mark_node;
7748 else
7750 iter_incr = build_x_modify_expr (EXPR_LOCATION (rhs),
7751 iter, TREE_CODE (rhs),
7752 TREE_OPERAND (rhs, 1),
7753 tf_warning_or_error);
7754 if (error_operand_p (iter_incr))
7755 return true;
7756 incr = TREE_OPERAND (rhs, 1);
7757 incr = cp_convert (TREE_TYPE (diff), incr,
7758 tf_warning_or_error);
7759 if (TREE_CODE (rhs) == MINUS_EXPR)
7761 incr = build1 (NEGATE_EXPR, TREE_TYPE (diff), incr);
7762 incr = fold_simple (incr);
7764 if (TREE_CODE (incr) != INTEGER_CST
7765 && (TREE_CODE (incr) != NOP_EXPR
7766 || (TREE_CODE (TREE_OPERAND (incr, 0))
7767 != INTEGER_CST)))
7768 iter_incr = NULL;
7771 else if (TREE_OPERAND (rhs, 1) == iter)
7773 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (rhs, 0))) != INTEGER_TYPE
7774 || TREE_CODE (rhs) != PLUS_EXPR)
7775 incr = error_mark_node;
7776 else
7778 iter_incr = build_x_binary_op (EXPR_LOCATION (rhs),
7779 PLUS_EXPR,
7780 TREE_OPERAND (rhs, 0),
7781 ERROR_MARK, iter,
7782 ERROR_MARK, NULL,
7783 tf_warning_or_error);
7784 if (error_operand_p (iter_incr))
7785 return true;
7786 iter_incr = build_x_modify_expr (EXPR_LOCATION (rhs),
7787 iter, NOP_EXPR,
7788 iter_incr,
7789 tf_warning_or_error);
7790 if (error_operand_p (iter_incr))
7791 return true;
7792 incr = TREE_OPERAND (rhs, 0);
7793 iter_incr = NULL;
7796 else
7797 incr = error_mark_node;
7799 else
7800 incr = error_mark_node;
7801 break;
7802 default:
7803 incr = error_mark_node;
7804 break;
7807 if (incr == error_mark_node)
7809 error_at (elocus, "invalid increment expression");
7810 return true;
7813 incr = cp_convert (TREE_TYPE (diff), incr, tf_warning_or_error);
7814 bool taskloop_iv_seen = false;
7815 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
7816 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
7817 && OMP_CLAUSE_DECL (c) == iter)
7819 if (code == OMP_TASKLOOP)
7821 taskloop_iv_seen = true;
7822 OMP_CLAUSE_LASTPRIVATE_TASKLOOP_IV (c) = 1;
7824 break;
7826 else if (code == OMP_TASKLOOP
7827 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
7828 && OMP_CLAUSE_DECL (c) == iter)
7830 taskloop_iv_seen = true;
7831 OMP_CLAUSE_PRIVATE_TASKLOOP_IV (c) = 1;
7834 decl = create_temporary_var (TREE_TYPE (diff));
7835 pushdecl (decl);
7836 add_decl_expr (decl);
7837 last = create_temporary_var (TREE_TYPE (diff));
7838 pushdecl (last);
7839 add_decl_expr (last);
7840 if (c && iter_incr == NULL && TREE_CODE (incr) != INTEGER_CST
7841 && (!ordered || (i < collapse && collapse > 1)))
7843 incr_var = create_temporary_var (TREE_TYPE (diff));
7844 pushdecl (incr_var);
7845 add_decl_expr (incr_var);
7847 gcc_assert (stmts_are_full_exprs_p ());
7848 tree diffvar = NULL_TREE;
7849 if (code == OMP_TASKLOOP)
7851 if (!taskloop_iv_seen)
7853 tree ivc = build_omp_clause (locus, OMP_CLAUSE_FIRSTPRIVATE);
7854 OMP_CLAUSE_DECL (ivc) = iter;
7855 cxx_omp_finish_clause (ivc, NULL);
7856 OMP_CLAUSE_CHAIN (ivc) = clauses;
7857 clauses = ivc;
7859 tree lvc = build_omp_clause (locus, OMP_CLAUSE_FIRSTPRIVATE);
7860 OMP_CLAUSE_DECL (lvc) = last;
7861 OMP_CLAUSE_CHAIN (lvc) = clauses;
7862 clauses = lvc;
7863 diffvar = create_temporary_var (TREE_TYPE (diff));
7864 pushdecl (diffvar);
7865 add_decl_expr (diffvar);
7868 orig_pre_body = *pre_body;
7869 *pre_body = push_stmt_list ();
7870 if (orig_pre_body)
7871 add_stmt (orig_pre_body);
7872 if (init != NULL)
7873 finish_expr_stmt (build_x_modify_expr (elocus,
7874 iter, NOP_EXPR, init,
7875 tf_warning_or_error));
7876 init = build_int_cst (TREE_TYPE (diff), 0);
7877 if (c && iter_incr == NULL
7878 && (!ordered || (i < collapse && collapse > 1)))
7880 if (incr_var)
7882 finish_expr_stmt (build_x_modify_expr (elocus,
7883 incr_var, NOP_EXPR,
7884 incr, tf_warning_or_error));
7885 incr = incr_var;
7887 iter_incr = build_x_modify_expr (elocus,
7888 iter, PLUS_EXPR, incr,
7889 tf_warning_or_error);
7891 if (c && ordered && i < collapse && collapse > 1)
7892 iter_incr = incr;
7893 finish_expr_stmt (build_x_modify_expr (elocus,
7894 last, NOP_EXPR, init,
7895 tf_warning_or_error));
7896 if (diffvar)
7898 finish_expr_stmt (build_x_modify_expr (elocus,
7899 diffvar, NOP_EXPR,
7900 diff, tf_warning_or_error));
7901 diff = diffvar;
7903 *pre_body = pop_stmt_list (*pre_body);
7905 cond = cp_build_binary_op (elocus,
7906 TREE_CODE (cond), decl, diff,
7907 tf_warning_or_error);
7908 incr = build_modify_expr (elocus, decl, NULL_TREE, PLUS_EXPR,
7909 elocus, incr, NULL_TREE);
7911 orig_body = *body;
7912 *body = push_stmt_list ();
7913 iter_init = build2 (MINUS_EXPR, TREE_TYPE (diff), decl, last);
7914 iter_init = build_x_modify_expr (elocus,
7915 iter, PLUS_EXPR, iter_init,
7916 tf_warning_or_error);
7917 if (iter_init != error_mark_node)
7918 iter_init = build1 (NOP_EXPR, void_type_node, iter_init);
7919 finish_expr_stmt (iter_init);
7920 finish_expr_stmt (build_x_modify_expr (elocus,
7921 last, NOP_EXPR, decl,
7922 tf_warning_or_error));
7923 add_stmt (orig_body);
7924 *body = pop_stmt_list (*body);
7926 if (c)
7928 OMP_CLAUSE_LASTPRIVATE_STMT (c) = push_stmt_list ();
7929 if (!ordered)
7930 finish_expr_stmt (iter_incr);
7931 else
7933 iter_init = decl;
7934 if (i < collapse && collapse > 1 && !error_operand_p (iter_incr))
7935 iter_init = build2 (PLUS_EXPR, TREE_TYPE (diff),
7936 iter_init, iter_incr);
7937 iter_init = build2 (MINUS_EXPR, TREE_TYPE (diff), iter_init, last);
7938 iter_init = build_x_modify_expr (elocus,
7939 iter, PLUS_EXPR, iter_init,
7940 tf_warning_or_error);
7941 if (iter_init != error_mark_node)
7942 iter_init = build1 (NOP_EXPR, void_type_node, iter_init);
7943 finish_expr_stmt (iter_init);
7945 OMP_CLAUSE_LASTPRIVATE_STMT (c)
7946 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (c));
7949 TREE_VEC_ELT (declv, i) = decl;
7950 TREE_VEC_ELT (initv, i) = init;
7951 TREE_VEC_ELT (condv, i) = cond;
7952 TREE_VEC_ELT (incrv, i) = incr;
7953 *lastp = last;
7955 return false;
7958 /* Build and validate an OMP_FOR statement. CLAUSES, BODY, COND, INCR
7959 are directly for their associated operands in the statement. DECL
7960 and INIT are a combo; if DECL is NULL then INIT ought to be a
7961 MODIFY_EXPR, and the DECL should be extracted. PRE_BODY are
7962 optional statements that need to go before the loop into its
7963 sk_omp scope. */
7965 tree
7966 finish_omp_for (location_t locus, enum tree_code code, tree declv,
7967 tree orig_declv, tree initv, tree condv, tree incrv,
7968 tree body, tree pre_body, vec<tree> *orig_inits, tree clauses)
7970 tree omp_for = NULL, orig_incr = NULL;
7971 tree decl = NULL, init, cond, incr;
7972 tree last = NULL_TREE;
7973 location_t elocus;
7974 int i;
7975 int collapse = 1;
7976 int ordered = 0;
7978 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (initv));
7979 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (condv));
7980 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (incrv));
7981 if (TREE_VEC_LENGTH (declv) > 1)
7983 tree c;
7985 c = omp_find_clause (clauses, OMP_CLAUSE_TILE);
7986 if (c)
7987 collapse = list_length (OMP_CLAUSE_TILE_LIST (c));
7988 else
7990 c = omp_find_clause (clauses, OMP_CLAUSE_COLLAPSE);
7991 if (c)
7992 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (c));
7993 if (collapse != TREE_VEC_LENGTH (declv))
7994 ordered = TREE_VEC_LENGTH (declv);
7997 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
7999 decl = TREE_VEC_ELT (declv, i);
8000 init = TREE_VEC_ELT (initv, i);
8001 cond = TREE_VEC_ELT (condv, i);
8002 incr = TREE_VEC_ELT (incrv, i);
8003 elocus = locus;
8005 if (decl == NULL)
8007 if (init != NULL)
8008 switch (TREE_CODE (init))
8010 case MODIFY_EXPR:
8011 decl = TREE_OPERAND (init, 0);
8012 init = TREE_OPERAND (init, 1);
8013 break;
8014 case MODOP_EXPR:
8015 if (TREE_CODE (TREE_OPERAND (init, 1)) == NOP_EXPR)
8017 decl = TREE_OPERAND (init, 0);
8018 init = TREE_OPERAND (init, 2);
8020 break;
8021 default:
8022 break;
8025 if (decl == NULL)
8027 error_at (locus,
8028 "expected iteration declaration or initialization");
8029 return NULL;
8033 if (init && EXPR_HAS_LOCATION (init))
8034 elocus = EXPR_LOCATION (init);
8036 if (cond == NULL)
8038 error_at (elocus, "missing controlling predicate");
8039 return NULL;
8042 if (incr == NULL)
8044 error_at (elocus, "missing increment expression");
8045 return NULL;
8048 TREE_VEC_ELT (declv, i) = decl;
8049 TREE_VEC_ELT (initv, i) = init;
8052 if (orig_inits)
8054 bool fail = false;
8055 tree orig_init;
8056 FOR_EACH_VEC_ELT (*orig_inits, i, orig_init)
8057 if (orig_init
8058 && !c_omp_check_loop_iv_exprs (locus, declv,
8059 TREE_VEC_ELT (declv, i), orig_init,
8060 NULL_TREE, cp_walk_subtrees))
8061 fail = true;
8062 if (fail)
8063 return NULL;
8066 if (dependent_omp_for_p (declv, initv, condv, incrv))
8068 tree stmt;
8070 stmt = make_node (code);
8072 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
8074 /* This is really just a place-holder. We'll be decomposing this
8075 again and going through the cp_build_modify_expr path below when
8076 we instantiate the thing. */
8077 TREE_VEC_ELT (initv, i)
8078 = build2 (MODIFY_EXPR, void_type_node, TREE_VEC_ELT (declv, i),
8079 TREE_VEC_ELT (initv, i));
8082 TREE_TYPE (stmt) = void_type_node;
8083 OMP_FOR_INIT (stmt) = initv;
8084 OMP_FOR_COND (stmt) = condv;
8085 OMP_FOR_INCR (stmt) = incrv;
8086 OMP_FOR_BODY (stmt) = body;
8087 OMP_FOR_PRE_BODY (stmt) = pre_body;
8088 OMP_FOR_CLAUSES (stmt) = clauses;
8090 SET_EXPR_LOCATION (stmt, locus);
8091 return add_stmt (stmt);
8094 if (!orig_declv)
8095 orig_declv = copy_node (declv);
8097 if (processing_template_decl)
8098 orig_incr = make_tree_vec (TREE_VEC_LENGTH (incrv));
8100 for (i = 0; i < TREE_VEC_LENGTH (declv); )
8102 decl = TREE_VEC_ELT (declv, i);
8103 init = TREE_VEC_ELT (initv, i);
8104 cond = TREE_VEC_ELT (condv, i);
8105 incr = TREE_VEC_ELT (incrv, i);
8106 if (orig_incr)
8107 TREE_VEC_ELT (orig_incr, i) = incr;
8108 elocus = locus;
8110 if (init && EXPR_HAS_LOCATION (init))
8111 elocus = EXPR_LOCATION (init);
8113 if (!DECL_P (decl))
8115 error_at (elocus, "expected iteration declaration or initialization");
8116 return NULL;
8119 if (incr && TREE_CODE (incr) == MODOP_EXPR)
8121 if (orig_incr)
8122 TREE_VEC_ELT (orig_incr, i) = incr;
8123 incr = cp_build_modify_expr (elocus, TREE_OPERAND (incr, 0),
8124 TREE_CODE (TREE_OPERAND (incr, 1)),
8125 TREE_OPERAND (incr, 2),
8126 tf_warning_or_error);
8129 if (CLASS_TYPE_P (TREE_TYPE (decl)))
8131 if (code == OMP_SIMD)
8133 error_at (elocus, "%<#pragma omp simd%> used with class "
8134 "iteration variable %qE", decl);
8135 return NULL;
8137 if (handle_omp_for_class_iterator (i, locus, code, declv, orig_declv,
8138 initv, condv, incrv, &body,
8139 &pre_body, clauses, &last,
8140 collapse, ordered))
8141 return NULL;
8142 continue;
8145 if (!INTEGRAL_TYPE_P (TREE_TYPE (decl))
8146 && !TYPE_PTR_P (TREE_TYPE (decl)))
8148 error_at (elocus, "invalid type for iteration variable %qE", decl);
8149 return NULL;
8152 if (!processing_template_decl)
8154 init = fold_build_cleanup_point_expr (TREE_TYPE (init), init);
8155 init = cp_build_modify_expr (elocus, decl, NOP_EXPR, init,
8156 tf_warning_or_error);
8158 else
8159 init = build2 (MODIFY_EXPR, void_type_node, decl, init);
8160 if (cond
8161 && TREE_SIDE_EFFECTS (cond)
8162 && COMPARISON_CLASS_P (cond)
8163 && !processing_template_decl)
8165 tree t = TREE_OPERAND (cond, 0);
8166 if (TREE_SIDE_EFFECTS (t)
8167 && t != decl
8168 && (TREE_CODE (t) != NOP_EXPR
8169 || TREE_OPERAND (t, 0) != decl))
8170 TREE_OPERAND (cond, 0)
8171 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
8173 t = TREE_OPERAND (cond, 1);
8174 if (TREE_SIDE_EFFECTS (t)
8175 && t != decl
8176 && (TREE_CODE (t) != NOP_EXPR
8177 || TREE_OPERAND (t, 0) != decl))
8178 TREE_OPERAND (cond, 1)
8179 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
8181 if (decl == error_mark_node || init == error_mark_node)
8182 return NULL;
8184 TREE_VEC_ELT (declv, i) = decl;
8185 TREE_VEC_ELT (initv, i) = init;
8186 TREE_VEC_ELT (condv, i) = cond;
8187 TREE_VEC_ELT (incrv, i) = incr;
8188 i++;
8191 if (IS_EMPTY_STMT (pre_body))
8192 pre_body = NULL;
8194 omp_for = c_finish_omp_for (locus, code, declv, orig_declv, initv, condv,
8195 incrv, body, pre_body);
8197 /* Check for iterators appearing in lb, b or incr expressions. */
8198 if (omp_for && !c_omp_check_loop_iv (omp_for, orig_declv, cp_walk_subtrees))
8199 omp_for = NULL_TREE;
8201 if (omp_for == NULL)
8203 return NULL;
8206 add_stmt (omp_for);
8208 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INCR (omp_for)); i++)
8210 decl = TREE_OPERAND (TREE_VEC_ELT (OMP_FOR_INIT (omp_for), i), 0);
8211 incr = TREE_VEC_ELT (OMP_FOR_INCR (omp_for), i);
8213 if (TREE_CODE (incr) != MODIFY_EXPR)
8214 continue;
8216 if (TREE_SIDE_EFFECTS (TREE_OPERAND (incr, 1))
8217 && BINARY_CLASS_P (TREE_OPERAND (incr, 1))
8218 && !processing_template_decl)
8220 tree t = TREE_OPERAND (TREE_OPERAND (incr, 1), 0);
8221 if (TREE_SIDE_EFFECTS (t)
8222 && t != decl
8223 && (TREE_CODE (t) != NOP_EXPR
8224 || TREE_OPERAND (t, 0) != decl))
8225 TREE_OPERAND (TREE_OPERAND (incr, 1), 0)
8226 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
8228 t = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
8229 if (TREE_SIDE_EFFECTS (t)
8230 && t != decl
8231 && (TREE_CODE (t) != NOP_EXPR
8232 || TREE_OPERAND (t, 0) != decl))
8233 TREE_OPERAND (TREE_OPERAND (incr, 1), 1)
8234 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
8237 if (orig_incr)
8238 TREE_VEC_ELT (OMP_FOR_INCR (omp_for), i) = TREE_VEC_ELT (orig_incr, i);
8240 OMP_FOR_CLAUSES (omp_for) = clauses;
8242 /* For simd loops with non-static data member iterators, we could have added
8243 OMP_CLAUSE_LINEAR clauses without OMP_CLAUSE_LINEAR_STEP. As we know the
8244 step at this point, fill it in. */
8245 if (code == OMP_SIMD && !processing_template_decl
8246 && TREE_VEC_LENGTH (OMP_FOR_INCR (omp_for)) == 1)
8247 for (tree c = omp_find_clause (clauses, OMP_CLAUSE_LINEAR); c;
8248 c = omp_find_clause (OMP_CLAUSE_CHAIN (c), OMP_CLAUSE_LINEAR))
8249 if (OMP_CLAUSE_LINEAR_STEP (c) == NULL_TREE)
8251 decl = TREE_OPERAND (TREE_VEC_ELT (OMP_FOR_INIT (omp_for), 0), 0);
8252 gcc_assert (decl == OMP_CLAUSE_DECL (c));
8253 incr = TREE_VEC_ELT (OMP_FOR_INCR (omp_for), 0);
8254 tree step, stept;
8255 switch (TREE_CODE (incr))
8257 case PREINCREMENT_EXPR:
8258 case POSTINCREMENT_EXPR:
8259 /* c_omp_for_incr_canonicalize_ptr() should have been
8260 called to massage things appropriately. */
8261 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (decl)));
8262 OMP_CLAUSE_LINEAR_STEP (c) = build_int_cst (TREE_TYPE (decl), 1);
8263 break;
8264 case PREDECREMENT_EXPR:
8265 case POSTDECREMENT_EXPR:
8266 /* c_omp_for_incr_canonicalize_ptr() should have been
8267 called to massage things appropriately. */
8268 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (decl)));
8269 OMP_CLAUSE_LINEAR_STEP (c)
8270 = build_int_cst (TREE_TYPE (decl), -1);
8271 break;
8272 case MODIFY_EXPR:
8273 gcc_assert (TREE_OPERAND (incr, 0) == decl);
8274 incr = TREE_OPERAND (incr, 1);
8275 switch (TREE_CODE (incr))
8277 case PLUS_EXPR:
8278 if (TREE_OPERAND (incr, 1) == decl)
8279 step = TREE_OPERAND (incr, 0);
8280 else
8281 step = TREE_OPERAND (incr, 1);
8282 break;
8283 case MINUS_EXPR:
8284 case POINTER_PLUS_EXPR:
8285 gcc_assert (TREE_OPERAND (incr, 0) == decl);
8286 step = TREE_OPERAND (incr, 1);
8287 break;
8288 default:
8289 gcc_unreachable ();
8291 stept = TREE_TYPE (decl);
8292 if (POINTER_TYPE_P (stept))
8293 stept = sizetype;
8294 step = fold_convert (stept, step);
8295 if (TREE_CODE (incr) == MINUS_EXPR)
8296 step = fold_build1 (NEGATE_EXPR, stept, step);
8297 OMP_CLAUSE_LINEAR_STEP (c) = step;
8298 break;
8299 default:
8300 gcc_unreachable ();
8304 return omp_for;
8307 void
8308 finish_omp_atomic (enum tree_code code, enum tree_code opcode, tree lhs,
8309 tree rhs, tree v, tree lhs1, tree rhs1, bool seq_cst)
8311 tree orig_lhs;
8312 tree orig_rhs;
8313 tree orig_v;
8314 tree orig_lhs1;
8315 tree orig_rhs1;
8316 bool dependent_p;
8317 tree stmt;
8319 orig_lhs = lhs;
8320 orig_rhs = rhs;
8321 orig_v = v;
8322 orig_lhs1 = lhs1;
8323 orig_rhs1 = rhs1;
8324 dependent_p = false;
8325 stmt = NULL_TREE;
8327 /* Even in a template, we can detect invalid uses of the atomic
8328 pragma if neither LHS nor RHS is type-dependent. */
8329 if (processing_template_decl)
8331 dependent_p = (type_dependent_expression_p (lhs)
8332 || (rhs && type_dependent_expression_p (rhs))
8333 || (v && type_dependent_expression_p (v))
8334 || (lhs1 && type_dependent_expression_p (lhs1))
8335 || (rhs1 && type_dependent_expression_p (rhs1)));
8336 if (!dependent_p)
8338 lhs = build_non_dependent_expr (lhs);
8339 if (rhs)
8340 rhs = build_non_dependent_expr (rhs);
8341 if (v)
8342 v = build_non_dependent_expr (v);
8343 if (lhs1)
8344 lhs1 = build_non_dependent_expr (lhs1);
8345 if (rhs1)
8346 rhs1 = build_non_dependent_expr (rhs1);
8349 if (!dependent_p)
8351 bool swapped = false;
8352 if (rhs1 && cp_tree_equal (lhs, rhs))
8354 std::swap (rhs, rhs1);
8355 swapped = !commutative_tree_code (opcode);
8357 if (rhs1 && !cp_tree_equal (lhs, rhs1))
8359 if (code == OMP_ATOMIC)
8360 error ("%<#pragma omp atomic update%> uses two different "
8361 "expressions for memory");
8362 else
8363 error ("%<#pragma omp atomic capture%> uses two different "
8364 "expressions for memory");
8365 return;
8367 if (lhs1 && !cp_tree_equal (lhs, lhs1))
8369 if (code == OMP_ATOMIC)
8370 error ("%<#pragma omp atomic update%> uses two different "
8371 "expressions for memory");
8372 else
8373 error ("%<#pragma omp atomic capture%> uses two different "
8374 "expressions for memory");
8375 return;
8377 stmt = c_finish_omp_atomic (input_location, code, opcode, lhs, rhs,
8378 v, lhs1, rhs1, swapped, seq_cst,
8379 processing_template_decl != 0);
8380 if (stmt == error_mark_node)
8381 return;
8383 if (processing_template_decl)
8385 if (code == OMP_ATOMIC_READ)
8387 stmt = build_min_nt_loc (EXPR_LOCATION (orig_lhs),
8388 OMP_ATOMIC_READ, orig_lhs);
8389 OMP_ATOMIC_SEQ_CST (stmt) = seq_cst;
8390 stmt = build2 (MODIFY_EXPR, void_type_node, orig_v, stmt);
8392 else
8394 if (opcode == NOP_EXPR)
8395 stmt = build2 (MODIFY_EXPR, void_type_node, orig_lhs, orig_rhs);
8396 else
8397 stmt = build2 (opcode, void_type_node, orig_lhs, orig_rhs);
8398 if (orig_rhs1)
8399 stmt = build_min_nt_loc (EXPR_LOCATION (orig_rhs1),
8400 COMPOUND_EXPR, orig_rhs1, stmt);
8401 if (code != OMP_ATOMIC)
8403 stmt = build_min_nt_loc (EXPR_LOCATION (orig_lhs1),
8404 code, orig_lhs1, stmt);
8405 OMP_ATOMIC_SEQ_CST (stmt) = seq_cst;
8406 stmt = build2 (MODIFY_EXPR, void_type_node, orig_v, stmt);
8409 stmt = build2 (OMP_ATOMIC, void_type_node, integer_zero_node, stmt);
8410 OMP_ATOMIC_SEQ_CST (stmt) = seq_cst;
8412 finish_expr_stmt (stmt);
8415 void
8416 finish_omp_barrier (void)
8418 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_BARRIER);
8419 vec<tree, va_gc> *vec = make_tree_vector ();
8420 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
8421 release_tree_vector (vec);
8422 finish_expr_stmt (stmt);
8425 void
8426 finish_omp_flush (void)
8428 tree fn = builtin_decl_explicit (BUILT_IN_SYNC_SYNCHRONIZE);
8429 vec<tree, va_gc> *vec = make_tree_vector ();
8430 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
8431 release_tree_vector (vec);
8432 finish_expr_stmt (stmt);
8435 void
8436 finish_omp_taskwait (void)
8438 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_TASKWAIT);
8439 vec<tree, va_gc> *vec = make_tree_vector ();
8440 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
8441 release_tree_vector (vec);
8442 finish_expr_stmt (stmt);
8445 void
8446 finish_omp_taskyield (void)
8448 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_TASKYIELD);
8449 vec<tree, va_gc> *vec = make_tree_vector ();
8450 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
8451 release_tree_vector (vec);
8452 finish_expr_stmt (stmt);
8455 void
8456 finish_omp_cancel (tree clauses)
8458 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
8459 int mask = 0;
8460 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
8461 mask = 1;
8462 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
8463 mask = 2;
8464 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
8465 mask = 4;
8466 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
8467 mask = 8;
8468 else
8470 error ("%<#pragma omp cancel%> must specify one of "
8471 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> clauses");
8472 return;
8474 vec<tree, va_gc> *vec = make_tree_vector ();
8475 tree ifc = omp_find_clause (clauses, OMP_CLAUSE_IF);
8476 if (ifc != NULL_TREE)
8478 tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
8479 ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
8480 boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
8481 build_zero_cst (type));
8483 else
8484 ifc = boolean_true_node;
8485 vec->quick_push (build_int_cst (integer_type_node, mask));
8486 vec->quick_push (ifc);
8487 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
8488 release_tree_vector (vec);
8489 finish_expr_stmt (stmt);
8492 void
8493 finish_omp_cancellation_point (tree clauses)
8495 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
8496 int mask = 0;
8497 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
8498 mask = 1;
8499 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
8500 mask = 2;
8501 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
8502 mask = 4;
8503 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
8504 mask = 8;
8505 else
8507 error ("%<#pragma omp cancellation point%> must specify one of "
8508 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> clauses");
8509 return;
8511 vec<tree, va_gc> *vec
8512 = make_tree_vector_single (build_int_cst (integer_type_node, mask));
8513 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
8514 release_tree_vector (vec);
8515 finish_expr_stmt (stmt);
8518 /* Begin a __transaction_atomic or __transaction_relaxed statement.
8519 If PCOMPOUND is non-null, this is for a function-transaction-block, and we
8520 should create an extra compound stmt. */
8522 tree
8523 begin_transaction_stmt (location_t loc, tree *pcompound, int flags)
8525 tree r;
8527 if (pcompound)
8528 *pcompound = begin_compound_stmt (0);
8530 r = build_stmt (loc, TRANSACTION_EXPR, NULL_TREE);
8532 /* Only add the statement to the function if support enabled. */
8533 if (flag_tm)
8534 add_stmt (r);
8535 else
8536 error_at (loc, ((flags & TM_STMT_ATTR_RELAXED) != 0
8537 ? G_("%<__transaction_relaxed%> without "
8538 "transactional memory support enabled")
8539 : G_("%<__transaction_atomic%> without "
8540 "transactional memory support enabled")));
8542 TRANSACTION_EXPR_BODY (r) = push_stmt_list ();
8543 TREE_SIDE_EFFECTS (r) = 1;
8544 return r;
8547 /* End a __transaction_atomic or __transaction_relaxed statement.
8548 If COMPOUND_STMT is non-null, this is for a function-transaction-block,
8549 and we should end the compound. If NOEX is non-NULL, we wrap the body in
8550 a MUST_NOT_THROW_EXPR with NOEX as condition. */
8552 void
8553 finish_transaction_stmt (tree stmt, tree compound_stmt, int flags, tree noex)
8555 TRANSACTION_EXPR_BODY (stmt) = pop_stmt_list (TRANSACTION_EXPR_BODY (stmt));
8556 TRANSACTION_EXPR_OUTER (stmt) = (flags & TM_STMT_ATTR_OUTER) != 0;
8557 TRANSACTION_EXPR_RELAXED (stmt) = (flags & TM_STMT_ATTR_RELAXED) != 0;
8558 TRANSACTION_EXPR_IS_STMT (stmt) = 1;
8560 /* noexcept specifications are not allowed for function transactions. */
8561 gcc_assert (!(noex && compound_stmt));
8562 if (noex)
8564 tree body = build_must_not_throw_expr (TRANSACTION_EXPR_BODY (stmt),
8565 noex);
8566 protected_set_expr_location
8567 (body, EXPR_LOCATION (TRANSACTION_EXPR_BODY (stmt)));
8568 TREE_SIDE_EFFECTS (body) = 1;
8569 TRANSACTION_EXPR_BODY (stmt) = body;
8572 if (compound_stmt)
8573 finish_compound_stmt (compound_stmt);
8576 /* Build a __transaction_atomic or __transaction_relaxed expression. If
8577 NOEX is non-NULL, we wrap the body in a MUST_NOT_THROW_EXPR with NOEX as
8578 condition. */
8580 tree
8581 build_transaction_expr (location_t loc, tree expr, int flags, tree noex)
8583 tree ret;
8584 if (noex)
8586 expr = build_must_not_throw_expr (expr, noex);
8587 protected_set_expr_location (expr, loc);
8588 TREE_SIDE_EFFECTS (expr) = 1;
8590 ret = build1 (TRANSACTION_EXPR, TREE_TYPE (expr), expr);
8591 if (flags & TM_STMT_ATTR_RELAXED)
8592 TRANSACTION_EXPR_RELAXED (ret) = 1;
8593 TREE_SIDE_EFFECTS (ret) = 1;
8594 SET_EXPR_LOCATION (ret, loc);
8595 return ret;
8598 void
8599 init_cp_semantics (void)
8603 /* Build a STATIC_ASSERT for a static assertion with the condition
8604 CONDITION and the message text MESSAGE. LOCATION is the location
8605 of the static assertion in the source code. When MEMBER_P, this
8606 static assertion is a member of a class. */
8607 void
8608 finish_static_assert (tree condition, tree message, location_t location,
8609 bool member_p)
8611 tsubst_flags_t complain = tf_warning_or_error;
8613 if (message == NULL_TREE
8614 || message == error_mark_node
8615 || condition == NULL_TREE
8616 || condition == error_mark_node)
8617 return;
8619 if (check_for_bare_parameter_packs (condition))
8620 condition = error_mark_node;
8622 if (type_dependent_expression_p (condition)
8623 || value_dependent_expression_p (condition))
8625 /* We're in a template; build a STATIC_ASSERT and put it in
8626 the right place. */
8627 tree assertion;
8629 assertion = make_node (STATIC_ASSERT);
8630 STATIC_ASSERT_CONDITION (assertion) = condition;
8631 STATIC_ASSERT_MESSAGE (assertion) = message;
8632 STATIC_ASSERT_SOURCE_LOCATION (assertion) = location;
8634 if (member_p)
8635 maybe_add_class_template_decl_list (current_class_type,
8636 assertion,
8637 /*friend_p=*/0);
8638 else
8639 add_stmt (assertion);
8641 return;
8644 /* Fold the expression and convert it to a boolean value. */
8645 condition = perform_implicit_conversion_flags (boolean_type_node, condition,
8646 complain, LOOKUP_NORMAL);
8647 condition = fold_non_dependent_expr (condition);
8649 if (TREE_CODE (condition) == INTEGER_CST && !integer_zerop (condition))
8650 /* Do nothing; the condition is satisfied. */
8652 else
8654 location_t saved_loc = input_location;
8656 input_location = location;
8657 if (TREE_CODE (condition) == INTEGER_CST
8658 && integer_zerop (condition))
8660 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
8661 (TREE_TYPE (TREE_TYPE (message))));
8662 int len = TREE_STRING_LENGTH (message) / sz - 1;
8663 /* Report the error. */
8664 if (len == 0)
8665 error ("static assertion failed");
8666 else
8667 error ("static assertion failed: %s",
8668 TREE_STRING_POINTER (message));
8670 else if (condition && condition != error_mark_node)
8672 error ("non-constant condition for static assertion");
8673 if (require_potential_rvalue_constant_expression (condition))
8674 cxx_constant_value (condition);
8676 input_location = saved_loc;
8680 /* Implements the C++0x decltype keyword. Returns the type of EXPR,
8681 suitable for use as a type-specifier.
8683 ID_EXPRESSION_OR_MEMBER_ACCESS_P is true when EXPR was parsed as an
8684 id-expression or a class member access, FALSE when it was parsed as
8685 a full expression. */
8687 tree
8688 finish_decltype_type (tree expr, bool id_expression_or_member_access_p,
8689 tsubst_flags_t complain)
8691 tree type = NULL_TREE;
8693 if (!expr || error_operand_p (expr))
8694 return error_mark_node;
8696 if (TYPE_P (expr)
8697 || TREE_CODE (expr) == TYPE_DECL
8698 || (TREE_CODE (expr) == BIT_NOT_EXPR
8699 && TYPE_P (TREE_OPERAND (expr, 0))))
8701 if (complain & tf_error)
8702 error ("argument to decltype must be an expression");
8703 return error_mark_node;
8706 /* Depending on the resolution of DR 1172, we may later need to distinguish
8707 instantiation-dependent but not type-dependent expressions so that, say,
8708 A<decltype(sizeof(T))>::U doesn't require 'typename'. */
8709 if (instantiation_dependent_uneval_expression_p (expr))
8711 type = cxx_make_type (DECLTYPE_TYPE);
8712 DECLTYPE_TYPE_EXPR (type) = expr;
8713 DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (type)
8714 = id_expression_or_member_access_p;
8715 SET_TYPE_STRUCTURAL_EQUALITY (type);
8717 return type;
8720 /* The type denoted by decltype(e) is defined as follows: */
8722 expr = resolve_nondeduced_context (expr, complain);
8724 if (invalid_nonstatic_memfn_p (input_location, expr, complain))
8725 return error_mark_node;
8727 if (type_unknown_p (expr))
8729 if (complain & tf_error)
8730 error ("decltype cannot resolve address of overloaded function");
8731 return error_mark_node;
8734 /* To get the size of a static data member declared as an array of
8735 unknown bound, we need to instantiate it. */
8736 if (VAR_P (expr)
8737 && VAR_HAD_UNKNOWN_BOUND (expr)
8738 && DECL_TEMPLATE_INSTANTIATION (expr))
8739 instantiate_decl (expr, /*defer_ok*/true, /*expl_inst_mem*/false);
8741 if (id_expression_or_member_access_p)
8743 /* If e is an id-expression or a class member access (5.2.5
8744 [expr.ref]), decltype(e) is defined as the type of the entity
8745 named by e. If there is no such entity, or e names a set of
8746 overloaded functions, the program is ill-formed. */
8747 if (identifier_p (expr))
8748 expr = lookup_name (expr);
8750 if (INDIRECT_REF_P (expr))
8751 /* This can happen when the expression is, e.g., "a.b". Just
8752 look at the underlying operand. */
8753 expr = TREE_OPERAND (expr, 0);
8755 if (TREE_CODE (expr) == OFFSET_REF
8756 || TREE_CODE (expr) == MEMBER_REF
8757 || TREE_CODE (expr) == SCOPE_REF)
8758 /* We're only interested in the field itself. If it is a
8759 BASELINK, we will need to see through it in the next
8760 step. */
8761 expr = TREE_OPERAND (expr, 1);
8763 if (BASELINK_P (expr))
8764 /* See through BASELINK nodes to the underlying function. */
8765 expr = BASELINK_FUNCTIONS (expr);
8767 /* decltype of a decomposition name drops references in the tuple case
8768 (unlike decltype of a normal variable) and keeps cv-qualifiers from
8769 the containing object in the other cases (unlike decltype of a member
8770 access expression). */
8771 if (DECL_DECOMPOSITION_P (expr))
8773 if (DECL_HAS_VALUE_EXPR_P (expr))
8774 /* Expr is an array or struct subobject proxy, handle
8775 bit-fields properly. */
8776 return unlowered_expr_type (expr);
8777 else
8778 /* Expr is a reference variable for the tuple case. */
8779 return lookup_decomp_type (expr);
8782 switch (TREE_CODE (expr))
8784 case FIELD_DECL:
8785 if (DECL_BIT_FIELD_TYPE (expr))
8787 type = DECL_BIT_FIELD_TYPE (expr);
8788 break;
8790 /* Fall through for fields that aren't bitfields. */
8791 gcc_fallthrough ();
8793 case FUNCTION_DECL:
8794 case VAR_DECL:
8795 case CONST_DECL:
8796 case PARM_DECL:
8797 case RESULT_DECL:
8798 case TEMPLATE_PARM_INDEX:
8799 expr = mark_type_use (expr);
8800 type = TREE_TYPE (expr);
8801 break;
8803 case ERROR_MARK:
8804 type = error_mark_node;
8805 break;
8807 case COMPONENT_REF:
8808 case COMPOUND_EXPR:
8809 mark_type_use (expr);
8810 type = is_bitfield_expr_with_lowered_type (expr);
8811 if (!type)
8812 type = TREE_TYPE (TREE_OPERAND (expr, 1));
8813 break;
8815 case BIT_FIELD_REF:
8816 gcc_unreachable ();
8818 case INTEGER_CST:
8819 case PTRMEM_CST:
8820 /* We can get here when the id-expression refers to an
8821 enumerator or non-type template parameter. */
8822 type = TREE_TYPE (expr);
8823 break;
8825 default:
8826 /* Handle instantiated template non-type arguments. */
8827 type = TREE_TYPE (expr);
8828 break;
8831 else
8833 /* Within a lambda-expression:
8835 Every occurrence of decltype((x)) where x is a possibly
8836 parenthesized id-expression that names an entity of
8837 automatic storage duration is treated as if x were
8838 transformed into an access to a corresponding data member
8839 of the closure type that would have been declared if x
8840 were a use of the denoted entity. */
8841 if (outer_automatic_var_p (expr)
8842 && current_function_decl
8843 && LAMBDA_FUNCTION_P (current_function_decl))
8844 type = capture_decltype (expr);
8845 else if (error_operand_p (expr))
8846 type = error_mark_node;
8847 else if (expr == current_class_ptr)
8848 /* If the expression is just "this", we want the
8849 cv-unqualified pointer for the "this" type. */
8850 type = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
8851 else
8853 /* Otherwise, where T is the type of e, if e is an lvalue,
8854 decltype(e) is defined as T&; if an xvalue, T&&; otherwise, T. */
8855 cp_lvalue_kind clk = lvalue_kind (expr);
8856 type = unlowered_expr_type (expr);
8857 gcc_assert (TREE_CODE (type) != REFERENCE_TYPE);
8859 /* For vector types, pick a non-opaque variant. */
8860 if (VECTOR_TYPE_P (type))
8861 type = strip_typedefs (type);
8863 if (clk != clk_none && !(clk & clk_class))
8864 type = cp_build_reference_type (type, (clk & clk_rvalueref));
8868 return type;
8871 /* Called from trait_expr_value to evaluate either __has_nothrow_assign or
8872 __has_nothrow_copy, depending on assign_p. Returns true iff all
8873 the copy {ctor,assign} fns are nothrow. */
8875 static bool
8876 classtype_has_nothrow_assign_or_copy_p (tree type, bool assign_p)
8878 tree fns = NULL_TREE;
8880 if (assign_p || TYPE_HAS_COPY_CTOR (type))
8881 fns = get_class_binding (type, assign_p ? assign_op_identifier
8882 : ctor_identifier);
8884 bool saw_copy = false;
8885 for (ovl_iterator iter (fns); iter; ++iter)
8887 tree fn = *iter;
8889 if (copy_fn_p (fn) > 0)
8891 saw_copy = true;
8892 maybe_instantiate_noexcept (fn);
8893 if (!TYPE_NOTHROW_P (TREE_TYPE (fn)))
8894 return false;
8898 return saw_copy;
8901 /* Actually evaluates the trait. */
8903 static bool
8904 trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
8906 enum tree_code type_code1;
8907 tree t;
8909 type_code1 = TREE_CODE (type1);
8911 switch (kind)
8913 case CPTK_HAS_NOTHROW_ASSIGN:
8914 type1 = strip_array_types (type1);
8915 return (!CP_TYPE_CONST_P (type1) && type_code1 != REFERENCE_TYPE
8916 && (trait_expr_value (CPTK_HAS_TRIVIAL_ASSIGN, type1, type2)
8917 || (CLASS_TYPE_P (type1)
8918 && classtype_has_nothrow_assign_or_copy_p (type1,
8919 true))));
8921 case CPTK_HAS_TRIVIAL_ASSIGN:
8922 /* ??? The standard seems to be missing the "or array of such a class
8923 type" wording for this trait. */
8924 type1 = strip_array_types (type1);
8925 return (!CP_TYPE_CONST_P (type1) && type_code1 != REFERENCE_TYPE
8926 && (trivial_type_p (type1)
8927 || (CLASS_TYPE_P (type1)
8928 && TYPE_HAS_TRIVIAL_COPY_ASSIGN (type1))));
8930 case CPTK_HAS_NOTHROW_CONSTRUCTOR:
8931 type1 = strip_array_types (type1);
8932 return (trait_expr_value (CPTK_HAS_TRIVIAL_CONSTRUCTOR, type1, type2)
8933 || (CLASS_TYPE_P (type1)
8934 && (t = locate_ctor (type1))
8935 && (maybe_instantiate_noexcept (t),
8936 TYPE_NOTHROW_P (TREE_TYPE (t)))));
8938 case CPTK_HAS_TRIVIAL_CONSTRUCTOR:
8939 type1 = strip_array_types (type1);
8940 return (trivial_type_p (type1)
8941 || (CLASS_TYPE_P (type1) && TYPE_HAS_TRIVIAL_DFLT (type1)));
8943 case CPTK_HAS_NOTHROW_COPY:
8944 type1 = strip_array_types (type1);
8945 return (trait_expr_value (CPTK_HAS_TRIVIAL_COPY, type1, type2)
8946 || (CLASS_TYPE_P (type1)
8947 && classtype_has_nothrow_assign_or_copy_p (type1, false)));
8949 case CPTK_HAS_TRIVIAL_COPY:
8950 /* ??? The standard seems to be missing the "or array of such a class
8951 type" wording for this trait. */
8952 type1 = strip_array_types (type1);
8953 return (trivial_type_p (type1) || type_code1 == REFERENCE_TYPE
8954 || (CLASS_TYPE_P (type1) && TYPE_HAS_TRIVIAL_COPY_CTOR (type1)));
8956 case CPTK_HAS_TRIVIAL_DESTRUCTOR:
8957 type1 = strip_array_types (type1);
8958 return (trivial_type_p (type1) || type_code1 == REFERENCE_TYPE
8959 || (CLASS_TYPE_P (type1)
8960 && TYPE_HAS_TRIVIAL_DESTRUCTOR (type1)));
8962 case CPTK_HAS_VIRTUAL_DESTRUCTOR:
8963 return type_has_virtual_destructor (type1);
8965 case CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS:
8966 return type_has_unique_obj_representations (type1);
8968 case CPTK_IS_ABSTRACT:
8969 return ABSTRACT_CLASS_TYPE_P (type1);
8971 case CPTK_IS_AGGREGATE:
8972 return CP_AGGREGATE_TYPE_P (type1);
8974 case CPTK_IS_BASE_OF:
8975 return (NON_UNION_CLASS_TYPE_P (type1) && NON_UNION_CLASS_TYPE_P (type2)
8976 && (same_type_ignoring_top_level_qualifiers_p (type1, type2)
8977 || DERIVED_FROM_P (type1, type2)));
8979 case CPTK_IS_CLASS:
8980 return NON_UNION_CLASS_TYPE_P (type1);
8982 case CPTK_IS_EMPTY:
8983 return NON_UNION_CLASS_TYPE_P (type1) && CLASSTYPE_EMPTY_P (type1);
8985 case CPTK_IS_ENUM:
8986 return type_code1 == ENUMERAL_TYPE;
8988 case CPTK_IS_FINAL:
8989 return CLASS_TYPE_P (type1) && CLASSTYPE_FINAL (type1);
8991 case CPTK_IS_LITERAL_TYPE:
8992 return literal_type_p (type1);
8994 case CPTK_IS_POD:
8995 return pod_type_p (type1);
8997 case CPTK_IS_POLYMORPHIC:
8998 return CLASS_TYPE_P (type1) && TYPE_POLYMORPHIC_P (type1);
9000 case CPTK_IS_SAME_AS:
9001 return same_type_p (type1, type2);
9003 case CPTK_IS_STD_LAYOUT:
9004 return std_layout_type_p (type1);
9006 case CPTK_IS_TRIVIAL:
9007 return trivial_type_p (type1);
9009 case CPTK_IS_TRIVIALLY_ASSIGNABLE:
9010 return is_trivially_xible (MODIFY_EXPR, type1, type2);
9012 case CPTK_IS_TRIVIALLY_CONSTRUCTIBLE:
9013 return is_trivially_xible (INIT_EXPR, type1, type2);
9015 case CPTK_IS_TRIVIALLY_COPYABLE:
9016 return trivially_copyable_p (type1);
9018 case CPTK_IS_UNION:
9019 return type_code1 == UNION_TYPE;
9021 case CPTK_IS_ASSIGNABLE:
9022 return is_xible (MODIFY_EXPR, type1, type2);
9024 case CPTK_IS_CONSTRUCTIBLE:
9025 return is_xible (INIT_EXPR, type1, type2);
9027 default:
9028 gcc_unreachable ();
9029 return false;
9033 /* If TYPE is an array of unknown bound, or (possibly cv-qualified)
9034 void, or a complete type, returns true, otherwise false. */
9036 static bool
9037 check_trait_type (tree type)
9039 if (type == NULL_TREE)
9040 return true;
9042 if (TREE_CODE (type) == TREE_LIST)
9043 return (check_trait_type (TREE_VALUE (type))
9044 && check_trait_type (TREE_CHAIN (type)));
9046 if (TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type)
9047 && COMPLETE_TYPE_P (TREE_TYPE (type)))
9048 return true;
9050 if (VOID_TYPE_P (type))
9051 return true;
9053 return !!complete_type_or_else (strip_array_types (type), NULL_TREE);
9056 /* Process a trait expression. */
9058 tree
9059 finish_trait_expr (cp_trait_kind kind, tree type1, tree type2)
9061 if (type1 == error_mark_node
9062 || type2 == error_mark_node)
9063 return error_mark_node;
9065 if (processing_template_decl)
9067 tree trait_expr = make_node (TRAIT_EXPR);
9068 TREE_TYPE (trait_expr) = boolean_type_node;
9069 TRAIT_EXPR_TYPE1 (trait_expr) = type1;
9070 TRAIT_EXPR_TYPE2 (trait_expr) = type2;
9071 TRAIT_EXPR_KIND (trait_expr) = kind;
9072 return trait_expr;
9075 switch (kind)
9077 case CPTK_HAS_NOTHROW_ASSIGN:
9078 case CPTK_HAS_TRIVIAL_ASSIGN:
9079 case CPTK_HAS_NOTHROW_CONSTRUCTOR:
9080 case CPTK_HAS_TRIVIAL_CONSTRUCTOR:
9081 case CPTK_HAS_NOTHROW_COPY:
9082 case CPTK_HAS_TRIVIAL_COPY:
9083 case CPTK_HAS_TRIVIAL_DESTRUCTOR:
9084 case CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS:
9085 case CPTK_HAS_VIRTUAL_DESTRUCTOR:
9086 case CPTK_IS_ABSTRACT:
9087 case CPTK_IS_AGGREGATE:
9088 case CPTK_IS_EMPTY:
9089 case CPTK_IS_FINAL:
9090 case CPTK_IS_LITERAL_TYPE:
9091 case CPTK_IS_POD:
9092 case CPTK_IS_POLYMORPHIC:
9093 case CPTK_IS_STD_LAYOUT:
9094 case CPTK_IS_TRIVIAL:
9095 case CPTK_IS_TRIVIALLY_COPYABLE:
9096 if (!check_trait_type (type1))
9097 return error_mark_node;
9098 break;
9100 case CPTK_IS_ASSIGNABLE:
9101 case CPTK_IS_CONSTRUCTIBLE:
9102 break;
9104 case CPTK_IS_TRIVIALLY_ASSIGNABLE:
9105 case CPTK_IS_TRIVIALLY_CONSTRUCTIBLE:
9106 if (!check_trait_type (type1)
9107 || !check_trait_type (type2))
9108 return error_mark_node;
9109 break;
9111 case CPTK_IS_BASE_OF:
9112 if (NON_UNION_CLASS_TYPE_P (type1) && NON_UNION_CLASS_TYPE_P (type2)
9113 && !same_type_ignoring_top_level_qualifiers_p (type1, type2)
9114 && !complete_type_or_else (type2, NULL_TREE))
9115 /* We already issued an error. */
9116 return error_mark_node;
9117 break;
9119 case CPTK_IS_CLASS:
9120 case CPTK_IS_ENUM:
9121 case CPTK_IS_UNION:
9122 case CPTK_IS_SAME_AS:
9123 break;
9125 default:
9126 gcc_unreachable ();
9129 return (trait_expr_value (kind, type1, type2)
9130 ? boolean_true_node : boolean_false_node);
9133 /* Do-nothing variants of functions to handle pragma FLOAT_CONST_DECIMAL64,
9134 which is ignored for C++. */
9136 void
9137 set_float_const_decimal64 (void)
9141 void
9142 clear_float_const_decimal64 (void)
9146 bool
9147 float_const_decimal64_p (void)
9149 return 0;
9153 /* Return true if T designates the implied `this' parameter. */
9155 bool
9156 is_this_parameter (tree t)
9158 if (!DECL_P (t) || DECL_NAME (t) != this_identifier)
9159 return false;
9160 gcc_assert (TREE_CODE (t) == PARM_DECL || is_capture_proxy (t)
9161 || (cp_binding_oracle && TREE_CODE (t) == VAR_DECL));
9162 return true;
9165 /* Insert the deduced return type for an auto function. */
9167 void
9168 apply_deduced_return_type (tree fco, tree return_type)
9170 tree result;
9172 if (return_type == error_mark_node)
9173 return;
9175 if (DECL_CONV_FN_P (fco))
9176 DECL_NAME (fco) = make_conv_op_name (return_type);
9178 TREE_TYPE (fco) = change_return_type (return_type, TREE_TYPE (fco));
9180 result = DECL_RESULT (fco);
9181 if (result == NULL_TREE)
9182 return;
9183 if (TREE_TYPE (result) == return_type)
9184 return;
9186 if (!processing_template_decl && !VOID_TYPE_P (return_type)
9187 && !complete_type_or_else (return_type, NULL_TREE))
9188 return;
9190 /* We already have a DECL_RESULT from start_preparsed_function.
9191 Now we need to redo the work it and allocate_struct_function
9192 did to reflect the new type. */
9193 gcc_assert (current_function_decl == fco);
9194 result = build_decl (input_location, RESULT_DECL, NULL_TREE,
9195 TYPE_MAIN_VARIANT (return_type));
9196 DECL_ARTIFICIAL (result) = 1;
9197 DECL_IGNORED_P (result) = 1;
9198 cp_apply_type_quals_to_decl (cp_type_quals (return_type),
9199 result);
9201 DECL_RESULT (fco) = result;
9203 if (!processing_template_decl)
9205 bool aggr = aggregate_value_p (result, fco);
9206 #ifdef PCC_STATIC_STRUCT_RETURN
9207 cfun->returns_pcc_struct = aggr;
9208 #endif
9209 cfun->returns_struct = aggr;
9214 /* DECL is a local variable or parameter from the surrounding scope of a
9215 lambda-expression. Returns the decltype for a use of the capture field
9216 for DECL even if it hasn't been captured yet. */
9218 static tree
9219 capture_decltype (tree decl)
9221 tree lam = CLASSTYPE_LAMBDA_EXPR (DECL_CONTEXT (current_function_decl));
9222 /* FIXME do lookup instead of list walk? */
9223 tree cap = value_member (decl, LAMBDA_EXPR_CAPTURE_LIST (lam));
9224 tree type;
9226 if (cap)
9227 type = TREE_TYPE (TREE_PURPOSE (cap));
9228 else
9229 switch (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lam))
9231 case CPLD_NONE:
9232 error ("%qD is not captured", decl);
9233 return error_mark_node;
9235 case CPLD_COPY:
9236 type = TREE_TYPE (decl);
9237 if (TREE_CODE (type) == REFERENCE_TYPE
9238 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
9239 type = TREE_TYPE (type);
9240 break;
9242 case CPLD_REFERENCE:
9243 type = TREE_TYPE (decl);
9244 if (TREE_CODE (type) != REFERENCE_TYPE)
9245 type = build_reference_type (TREE_TYPE (decl));
9246 break;
9248 default:
9249 gcc_unreachable ();
9252 if (TREE_CODE (type) != REFERENCE_TYPE)
9254 if (!LAMBDA_EXPR_MUTABLE_P (lam))
9255 type = cp_build_qualified_type (type, (cp_type_quals (type)
9256 |TYPE_QUAL_CONST));
9257 type = build_reference_type (type);
9259 return type;
9262 /* Build a unary fold expression of EXPR over OP. If IS_RIGHT is true,
9263 this is a right unary fold. Otherwise it is a left unary fold. */
9265 static tree
9266 finish_unary_fold_expr (tree expr, int op, tree_code dir)
9268 // Build a pack expansion (assuming expr has pack type).
9269 if (!uses_parameter_packs (expr))
9271 error_at (location_of (expr), "operand of fold expression has no "
9272 "unexpanded parameter packs");
9273 return error_mark_node;
9275 tree pack = make_pack_expansion (expr);
9277 // Build the fold expression.
9278 tree code = build_int_cstu (integer_type_node, abs (op));
9279 tree fold = build_min_nt_loc (UNKNOWN_LOCATION, dir, code, pack);
9280 FOLD_EXPR_MODIFY_P (fold) = (op < 0);
9281 return fold;
9284 tree
9285 finish_left_unary_fold_expr (tree expr, int op)
9287 return finish_unary_fold_expr (expr, op, UNARY_LEFT_FOLD_EXPR);
9290 tree
9291 finish_right_unary_fold_expr (tree expr, int op)
9293 return finish_unary_fold_expr (expr, op, UNARY_RIGHT_FOLD_EXPR);
9296 /* Build a binary fold expression over EXPR1 and EXPR2. The
9297 associativity of the fold is determined by EXPR1 and EXPR2 (whichever
9298 has an unexpanded parameter pack). */
9300 tree
9301 finish_binary_fold_expr (tree pack, tree init, int op, tree_code dir)
9303 pack = make_pack_expansion (pack);
9304 tree code = build_int_cstu (integer_type_node, abs (op));
9305 tree fold = build_min_nt_loc (UNKNOWN_LOCATION, dir, code, pack, init);
9306 FOLD_EXPR_MODIFY_P (fold) = (op < 0);
9307 return fold;
9310 tree
9311 finish_binary_fold_expr (tree expr1, tree expr2, int op)
9313 // Determine which expr has an unexpanded parameter pack and
9314 // set the pack and initial term.
9315 bool pack1 = uses_parameter_packs (expr1);
9316 bool pack2 = uses_parameter_packs (expr2);
9317 if (pack1 && !pack2)
9318 return finish_binary_fold_expr (expr1, expr2, op, BINARY_RIGHT_FOLD_EXPR);
9319 else if (pack2 && !pack1)
9320 return finish_binary_fold_expr (expr2, expr1, op, BINARY_LEFT_FOLD_EXPR);
9321 else
9323 if (pack1)
9324 error ("both arguments in binary fold have unexpanded parameter packs");
9325 else
9326 error ("no unexpanded parameter packs in binary fold");
9328 return error_mark_node;
9331 /* Finish __builtin_launder (arg). */
9333 tree
9334 finish_builtin_launder (location_t loc, tree arg, tsubst_flags_t complain)
9336 tree orig_arg = arg;
9337 if (!type_dependent_expression_p (arg))
9338 arg = decay_conversion (arg, complain);
9339 if (error_operand_p (arg))
9340 return error_mark_node;
9341 if (!type_dependent_expression_p (arg)
9342 && TREE_CODE (TREE_TYPE (arg)) != POINTER_TYPE)
9344 error_at (loc, "non-pointer argument to %<__builtin_launder%>");
9345 return error_mark_node;
9347 if (processing_template_decl)
9348 arg = orig_arg;
9349 return build_call_expr_internal_loc (loc, IFN_LAUNDER,
9350 TREE_TYPE (arg), 1, arg);
9353 #include "gt-cp-semantics.h"