More #include suggestions (PR c++/84269)
[official-gcc.git] / gcc / cp / semantics.c
blob035e3951574894ead12719b0d3054de031512879
1 /* Perform the semantic phase of parsing, i.e., the process of
2 building tree structure, checking semantic consistency, and
3 building RTL. These routines are used both during actual parsing
4 and during the instantiation of template functions.
6 Copyright (C) 1998-2018 Free Software Foundation, Inc.
7 Written by Mark Mitchell (mmitchell@usa.net) based on code found
8 formerly in parse.y and pt.c.
10 This file is part of GCC.
12 GCC is free software; you can redistribute it and/or modify it
13 under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 3, or (at your option)
15 any later version.
17 GCC is distributed in the hope that it will be useful, but
18 WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with GCC; see the file COPYING3. If not see
24 <http://www.gnu.org/licenses/>. */
26 #include "config.h"
27 #include "system.h"
28 #include "coretypes.h"
29 #include "target.h"
30 #include "bitmap.h"
31 #include "cp-tree.h"
32 #include "stringpool.h"
33 #include "cgraph.h"
34 #include "stmt.h"
35 #include "varasm.h"
36 #include "stor-layout.h"
37 #include "c-family/c-objc.h"
38 #include "tree-inline.h"
39 #include "intl.h"
40 #include "tree-iterator.h"
41 #include "omp-general.h"
42 #include "convert.h"
43 #include "stringpool.h"
44 #include "attribs.h"
45 #include "gomp-constants.h"
46 #include "predict.h"
48 /* There routines provide a modular interface to perform many parsing
49 operations. They may therefore be used during actual parsing, or
50 during template instantiation, which may be regarded as a
51 degenerate form of parsing. */
53 static tree maybe_convert_cond (tree);
54 static tree finalize_nrv_r (tree *, int *, void *);
55 static tree capture_decltype (tree);
57 /* Used for OpenMP non-static data member privatization. */
59 static hash_map<tree, tree> *omp_private_member_map;
60 static vec<tree> omp_private_member_vec;
61 static bool omp_private_member_ignore_next;
64 /* Deferred Access Checking Overview
65 ---------------------------------
67 Most C++ expressions and declarations require access checking
68 to be performed during parsing. However, in several cases,
69 this has to be treated differently.
71 For member declarations, access checking has to be deferred
72 until more information about the declaration is known. For
73 example:
75 class A {
76 typedef int X;
77 public:
78 X f();
81 A::X A::f();
82 A::X g();
84 When we are parsing the function return type `A::X', we don't
85 really know if this is allowed until we parse the function name.
87 Furthermore, some contexts require that access checking is
88 never performed at all. These include class heads, and template
89 instantiations.
91 Typical use of access checking functions is described here:
93 1. When we enter a context that requires certain access checking
94 mode, the function `push_deferring_access_checks' is called with
95 DEFERRING argument specifying the desired mode. Access checking
96 may be performed immediately (dk_no_deferred), deferred
97 (dk_deferred), or not performed (dk_no_check).
99 2. When a declaration such as a type, or a variable, is encountered,
100 the function `perform_or_defer_access_check' is called. It
101 maintains a vector of all deferred checks.
103 3. The global `current_class_type' or `current_function_decl' is then
104 setup by the parser. `enforce_access' relies on these information
105 to check access.
107 4. Upon exiting the context mentioned in step 1,
108 `perform_deferred_access_checks' is called to check all declaration
109 stored in the vector. `pop_deferring_access_checks' is then
110 called to restore the previous access checking mode.
112 In case of parsing error, we simply call `pop_deferring_access_checks'
113 without `perform_deferred_access_checks'. */
115 struct GTY(()) deferred_access {
116 /* A vector representing name-lookups for which we have deferred
117 checking access controls. We cannot check the accessibility of
118 names used in a decl-specifier-seq until we know what is being
119 declared because code like:
121 class A {
122 class B {};
123 B* f();
126 A::B* A::f() { return 0; }
128 is valid, even though `A::B' is not generally accessible. */
129 vec<deferred_access_check, va_gc> * GTY(()) deferred_access_checks;
131 /* The current mode of access checks. */
132 enum deferring_kind deferring_access_checks_kind;
136 /* Data for deferred access checking. */
137 static GTY(()) vec<deferred_access, va_gc> *deferred_access_stack;
138 static GTY(()) unsigned deferred_access_no_check;
140 /* Save the current deferred access states and start deferred
141 access checking iff DEFER_P is true. */
143 void
144 push_deferring_access_checks (deferring_kind deferring)
146 /* For context like template instantiation, access checking
147 disabling applies to all nested context. */
148 if (deferred_access_no_check || deferring == dk_no_check)
149 deferred_access_no_check++;
150 else
152 deferred_access e = {NULL, deferring};
153 vec_safe_push (deferred_access_stack, e);
157 /* Save the current deferred access states and start deferred access
158 checking, continuing the set of deferred checks in CHECKS. */
160 void
161 reopen_deferring_access_checks (vec<deferred_access_check, va_gc> * checks)
163 push_deferring_access_checks (dk_deferred);
164 if (!deferred_access_no_check)
165 deferred_access_stack->last().deferred_access_checks = checks;
168 /* Resume deferring access checks again after we stopped doing
169 this previously. */
171 void
172 resume_deferring_access_checks (void)
174 if (!deferred_access_no_check)
175 deferred_access_stack->last().deferring_access_checks_kind = dk_deferred;
178 /* Stop deferring access checks. */
180 void
181 stop_deferring_access_checks (void)
183 if (!deferred_access_no_check)
184 deferred_access_stack->last().deferring_access_checks_kind = dk_no_deferred;
187 /* Discard the current deferred access checks and restore the
188 previous states. */
190 void
191 pop_deferring_access_checks (void)
193 if (deferred_access_no_check)
194 deferred_access_no_check--;
195 else
196 deferred_access_stack->pop ();
199 /* Returns a TREE_LIST representing the deferred checks.
200 The TREE_PURPOSE of each node is the type through which the
201 access occurred; the TREE_VALUE is the declaration named.
204 vec<deferred_access_check, va_gc> *
205 get_deferred_access_checks (void)
207 if (deferred_access_no_check)
208 return NULL;
209 else
210 return (deferred_access_stack->last().deferred_access_checks);
213 /* Take current deferred checks and combine with the
214 previous states if we also defer checks previously.
215 Otherwise perform checks now. */
217 void
218 pop_to_parent_deferring_access_checks (void)
220 if (deferred_access_no_check)
221 deferred_access_no_check--;
222 else
224 vec<deferred_access_check, va_gc> *checks;
225 deferred_access *ptr;
227 checks = (deferred_access_stack->last ().deferred_access_checks);
229 deferred_access_stack->pop ();
230 ptr = &deferred_access_stack->last ();
231 if (ptr->deferring_access_checks_kind == dk_no_deferred)
233 /* Check access. */
234 perform_access_checks (checks, tf_warning_or_error);
236 else
238 /* Merge with parent. */
239 int i, j;
240 deferred_access_check *chk, *probe;
242 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
244 FOR_EACH_VEC_SAFE_ELT (ptr->deferred_access_checks, j, probe)
246 if (probe->binfo == chk->binfo &&
247 probe->decl == chk->decl &&
248 probe->diag_decl == chk->diag_decl)
249 goto found;
251 /* Insert into parent's checks. */
252 vec_safe_push (ptr->deferred_access_checks, *chk);
253 found:;
259 /* Perform the access checks in CHECKS. The TREE_PURPOSE of each node
260 is the BINFO indicating the qualifying scope used to access the
261 DECL node stored in the TREE_VALUE of the node. If CHECKS is empty
262 or we aren't in SFINAE context or all the checks succeed return TRUE,
263 otherwise FALSE. */
265 bool
266 perform_access_checks (vec<deferred_access_check, va_gc> *checks,
267 tsubst_flags_t complain)
269 int i;
270 deferred_access_check *chk;
271 location_t loc = input_location;
272 bool ok = true;
274 if (!checks)
275 return true;
277 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
279 input_location = chk->loc;
280 ok &= enforce_access (chk->binfo, chk->decl, chk->diag_decl, complain);
283 input_location = loc;
284 return (complain & tf_error) ? true : ok;
287 /* Perform the deferred access checks.
289 After performing the checks, we still have to keep the list
290 `deferred_access_stack->deferred_access_checks' since we may want
291 to check access for them again later in a different context.
292 For example:
294 class A {
295 typedef int X;
296 static X a;
298 A::X A::a, x; // No error for `A::a', error for `x'
300 We have to perform deferred access of `A::X', first with `A::a',
301 next with `x'. Return value like perform_access_checks above. */
303 bool
304 perform_deferred_access_checks (tsubst_flags_t complain)
306 return perform_access_checks (get_deferred_access_checks (), complain);
309 /* Defer checking the accessibility of DECL, when looked up in
310 BINFO. DIAG_DECL is the declaration to use to print diagnostics.
311 Return value like perform_access_checks above.
312 If non-NULL, report failures to AFI. */
314 bool
315 perform_or_defer_access_check (tree binfo, tree decl, tree diag_decl,
316 tsubst_flags_t complain,
317 access_failure_info *afi)
319 int i;
320 deferred_access *ptr;
321 deferred_access_check *chk;
324 /* Exit if we are in a context that no access checking is performed.
326 if (deferred_access_no_check)
327 return true;
329 gcc_assert (TREE_CODE (binfo) == TREE_BINFO);
331 ptr = &deferred_access_stack->last ();
333 /* If we are not supposed to defer access checks, just check now. */
334 if (ptr->deferring_access_checks_kind == dk_no_deferred)
336 bool ok = enforce_access (binfo, decl, diag_decl, complain, afi);
337 return (complain & tf_error) ? true : ok;
340 /* See if we are already going to perform this check. */
341 FOR_EACH_VEC_SAFE_ELT (ptr->deferred_access_checks, i, chk)
343 if (chk->decl == decl && chk->binfo == binfo &&
344 chk->diag_decl == diag_decl)
346 return true;
349 /* If not, record the check. */
350 deferred_access_check new_access = {binfo, decl, diag_decl, input_location};
351 vec_safe_push (ptr->deferred_access_checks, new_access);
353 return true;
356 /* Returns nonzero if the current statement is a full expression,
357 i.e. temporaries created during that statement should be destroyed
358 at the end of the statement. */
361 stmts_are_full_exprs_p (void)
363 return current_stmt_tree ()->stmts_are_full_exprs_p;
366 /* T is a statement. Add it to the statement-tree. This is the C++
367 version. The C/ObjC frontends have a slightly different version of
368 this function. */
370 tree
371 add_stmt (tree t)
373 enum tree_code code = TREE_CODE (t);
375 if (EXPR_P (t) && code != LABEL_EXPR)
377 if (!EXPR_HAS_LOCATION (t))
378 SET_EXPR_LOCATION (t, input_location);
380 /* When we expand a statement-tree, we must know whether or not the
381 statements are full-expressions. We record that fact here. */
382 STMT_IS_FULL_EXPR_P (t) = stmts_are_full_exprs_p ();
385 if (code == LABEL_EXPR || code == CASE_LABEL_EXPR)
386 STATEMENT_LIST_HAS_LABEL (cur_stmt_list) = 1;
388 /* Add T to the statement-tree. Non-side-effect statements need to be
389 recorded during statement expressions. */
390 gcc_checking_assert (!stmt_list_stack->is_empty ());
391 append_to_statement_list_force (t, &cur_stmt_list);
393 return t;
396 /* Returns the stmt_tree to which statements are currently being added. */
398 stmt_tree
399 current_stmt_tree (void)
401 return (cfun
402 ? &cfun->language->base.x_stmt_tree
403 : &scope_chain->x_stmt_tree);
406 /* If statements are full expressions, wrap STMT in a CLEANUP_POINT_EXPR. */
408 static tree
409 maybe_cleanup_point_expr (tree expr)
411 if (!processing_template_decl && stmts_are_full_exprs_p ())
412 expr = fold_build_cleanup_point_expr (TREE_TYPE (expr), expr);
413 return expr;
416 /* Like maybe_cleanup_point_expr except have the type of the new expression be
417 void so we don't need to create a temporary variable to hold the inner
418 expression. The reason why we do this is because the original type might be
419 an aggregate and we cannot create a temporary variable for that type. */
421 tree
422 maybe_cleanup_point_expr_void (tree expr)
424 if (!processing_template_decl && stmts_are_full_exprs_p ())
425 expr = fold_build_cleanup_point_expr (void_type_node, expr);
426 return expr;
431 /* Create a declaration statement for the declaration given by the DECL. */
433 void
434 add_decl_expr (tree decl)
436 tree r = build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl);
437 if (DECL_INITIAL (decl)
438 || (DECL_SIZE (decl) && TREE_SIDE_EFFECTS (DECL_SIZE (decl))))
439 r = maybe_cleanup_point_expr_void (r);
440 add_stmt (r);
443 /* Finish a scope. */
445 tree
446 do_poplevel (tree stmt_list)
448 tree block = NULL;
450 if (stmts_are_full_exprs_p ())
451 block = poplevel (kept_level_p (), 1, 0);
453 stmt_list = pop_stmt_list (stmt_list);
455 if (!processing_template_decl)
457 stmt_list = c_build_bind_expr (input_location, block, stmt_list);
458 /* ??? See c_end_compound_stmt re statement expressions. */
461 return stmt_list;
464 /* Begin a new scope. */
466 static tree
467 do_pushlevel (scope_kind sk)
469 tree ret = push_stmt_list ();
470 if (stmts_are_full_exprs_p ())
471 begin_scope (sk, NULL);
472 return ret;
475 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
476 when the current scope is exited. EH_ONLY is true when this is not
477 meant to apply to normal control flow transfer. */
479 void
480 push_cleanup (tree decl, tree cleanup, bool eh_only)
482 tree stmt = build_stmt (input_location, CLEANUP_STMT, NULL, cleanup, decl);
483 CLEANUP_EH_ONLY (stmt) = eh_only;
484 add_stmt (stmt);
485 CLEANUP_BODY (stmt) = push_stmt_list ();
488 /* Simple infinite loop tracking for -Wreturn-type. We keep a stack of all
489 the current loops, represented by 'NULL_TREE' if we've seen a possible
490 exit, and 'error_mark_node' if not. This is currently used only to
491 suppress the warning about a function with no return statements, and
492 therefore we don't bother noting returns as possible exits. We also
493 don't bother with gotos. */
495 static void
496 begin_maybe_infinite_loop (tree cond)
498 /* Only track this while parsing a function, not during instantiation. */
499 if (!cfun || (DECL_TEMPLATE_INSTANTIATION (current_function_decl)
500 && !processing_template_decl))
501 return;
502 bool maybe_infinite = true;
503 if (cond)
505 cond = fold_non_dependent_expr (cond);
506 maybe_infinite = integer_nonzerop (cond);
508 vec_safe_push (cp_function_chain->infinite_loops,
509 maybe_infinite ? error_mark_node : NULL_TREE);
513 /* A break is a possible exit for the current loop. */
515 void
516 break_maybe_infinite_loop (void)
518 if (!cfun)
519 return;
520 cp_function_chain->infinite_loops->last() = NULL_TREE;
523 /* If we reach the end of the loop without seeing a possible exit, we have
524 an infinite loop. */
526 static void
527 end_maybe_infinite_loop (tree cond)
529 if (!cfun || (DECL_TEMPLATE_INSTANTIATION (current_function_decl)
530 && !processing_template_decl))
531 return;
532 tree current = cp_function_chain->infinite_loops->pop();
533 if (current != NULL_TREE)
535 cond = fold_non_dependent_expr (cond);
536 if (integer_nonzerop (cond))
537 current_function_infinite_loop = 1;
542 /* Begin a conditional that might contain a declaration. When generating
543 normal code, we want the declaration to appear before the statement
544 containing the conditional. When generating template code, we want the
545 conditional to be rendered as the raw DECL_EXPR. */
547 static void
548 begin_cond (tree *cond_p)
550 if (processing_template_decl)
551 *cond_p = push_stmt_list ();
554 /* Finish such a conditional. */
556 static void
557 finish_cond (tree *cond_p, tree expr)
559 if (processing_template_decl)
561 tree cond = pop_stmt_list (*cond_p);
563 if (expr == NULL_TREE)
564 /* Empty condition in 'for'. */
565 gcc_assert (empty_expr_stmt_p (cond));
566 else if (check_for_bare_parameter_packs (expr))
567 expr = error_mark_node;
568 else if (!empty_expr_stmt_p (cond))
569 expr = build2 (COMPOUND_EXPR, TREE_TYPE (expr), cond, expr);
571 *cond_p = expr;
574 /* If *COND_P specifies a conditional with a declaration, transform the
575 loop such that
576 while (A x = 42) { }
577 for (; A x = 42;) { }
578 becomes
579 while (true) { A x = 42; if (!x) break; }
580 for (;;) { A x = 42; if (!x) break; }
581 The statement list for BODY will be empty if the conditional did
582 not declare anything. */
584 static void
585 simplify_loop_decl_cond (tree *cond_p, tree body)
587 tree cond, if_stmt;
589 if (!TREE_SIDE_EFFECTS (body))
590 return;
592 cond = *cond_p;
593 *cond_p = boolean_true_node;
595 if_stmt = begin_if_stmt ();
596 cond = cp_build_unary_op (TRUTH_NOT_EXPR, cond, false, tf_warning_or_error);
597 finish_if_stmt_cond (cond, if_stmt);
598 finish_break_stmt ();
599 finish_then_clause (if_stmt);
600 finish_if_stmt (if_stmt);
603 /* Finish a goto-statement. */
605 tree
606 finish_goto_stmt (tree destination)
608 if (identifier_p (destination))
609 destination = lookup_label (destination);
611 /* We warn about unused labels with -Wunused. That means we have to
612 mark the used labels as used. */
613 if (TREE_CODE (destination) == LABEL_DECL)
614 TREE_USED (destination) = 1;
615 else
617 destination = mark_rvalue_use (destination);
618 if (!processing_template_decl)
620 destination = cp_convert (ptr_type_node, destination,
621 tf_warning_or_error);
622 if (error_operand_p (destination))
623 return NULL_TREE;
624 destination
625 = fold_build_cleanup_point_expr (TREE_TYPE (destination),
626 destination);
630 check_goto (destination);
632 add_stmt (build_predict_expr (PRED_GOTO, NOT_TAKEN));
633 return add_stmt (build_stmt (input_location, GOTO_EXPR, destination));
636 /* COND is the condition-expression for an if, while, etc.,
637 statement. Convert it to a boolean value, if appropriate.
638 In addition, verify sequence points if -Wsequence-point is enabled. */
640 static tree
641 maybe_convert_cond (tree cond)
643 /* Empty conditions remain empty. */
644 if (!cond)
645 return NULL_TREE;
647 /* Wait until we instantiate templates before doing conversion. */
648 if (processing_template_decl)
649 return cond;
651 if (warn_sequence_point)
652 verify_sequence_points (cond);
654 /* Do the conversion. */
655 cond = convert_from_reference (cond);
657 if (TREE_CODE (cond) == MODIFY_EXPR
658 && !TREE_NO_WARNING (cond)
659 && warn_parentheses)
661 warning_at (EXPR_LOC_OR_LOC (cond, input_location), OPT_Wparentheses,
662 "suggest parentheses around assignment used as truth value");
663 TREE_NO_WARNING (cond) = 1;
666 return condition_conversion (cond);
669 /* Finish an expression-statement, whose EXPRESSION is as indicated. */
671 tree
672 finish_expr_stmt (tree expr)
674 tree r = NULL_TREE;
675 location_t loc = EXPR_LOCATION (expr);
677 if (expr != NULL_TREE)
679 /* If we ran into a problem, make sure we complained. */
680 gcc_assert (expr != error_mark_node || seen_error ());
682 if (!processing_template_decl)
684 if (warn_sequence_point)
685 verify_sequence_points (expr);
686 expr = convert_to_void (expr, ICV_STATEMENT, tf_warning_or_error);
688 else if (!type_dependent_expression_p (expr))
689 convert_to_void (build_non_dependent_expr (expr), ICV_STATEMENT,
690 tf_warning_or_error);
692 if (check_for_bare_parameter_packs (expr))
693 expr = error_mark_node;
695 /* Simplification of inner statement expressions, compound exprs,
696 etc can result in us already having an EXPR_STMT. */
697 if (TREE_CODE (expr) != CLEANUP_POINT_EXPR)
699 if (TREE_CODE (expr) != EXPR_STMT)
700 expr = build_stmt (loc, EXPR_STMT, expr);
701 expr = maybe_cleanup_point_expr_void (expr);
704 r = add_stmt (expr);
707 return r;
711 /* Begin an if-statement. Returns a newly created IF_STMT if
712 appropriate. */
714 tree
715 begin_if_stmt (void)
717 tree r, scope;
718 scope = do_pushlevel (sk_cond);
719 r = build_stmt (input_location, IF_STMT, NULL_TREE,
720 NULL_TREE, NULL_TREE, scope);
721 current_binding_level->this_entity = r;
722 begin_cond (&IF_COND (r));
723 return r;
726 /* Process the COND of an if-statement, which may be given by
727 IF_STMT. */
729 tree
730 finish_if_stmt_cond (tree cond, tree if_stmt)
732 cond = maybe_convert_cond (cond);
733 if (IF_STMT_CONSTEXPR_P (if_stmt)
734 && !type_dependent_expression_p (cond)
735 && require_constant_expression (cond)
736 && !value_dependent_expression_p (cond)
737 /* Wait until instantiation time, since only then COND has been
738 converted to bool. */
739 && TREE_TYPE (cond) == boolean_type_node)
741 cond = instantiate_non_dependent_expr (cond);
742 cond = cxx_constant_value (cond, NULL_TREE);
744 finish_cond (&IF_COND (if_stmt), cond);
745 add_stmt (if_stmt);
746 THEN_CLAUSE (if_stmt) = push_stmt_list ();
747 return cond;
750 /* Finish the then-clause of an if-statement, which may be given by
751 IF_STMT. */
753 tree
754 finish_then_clause (tree if_stmt)
756 THEN_CLAUSE (if_stmt) = pop_stmt_list (THEN_CLAUSE (if_stmt));
757 return if_stmt;
760 /* Begin the else-clause of an if-statement. */
762 void
763 begin_else_clause (tree if_stmt)
765 ELSE_CLAUSE (if_stmt) = push_stmt_list ();
768 /* Finish the else-clause of an if-statement, which may be given by
769 IF_STMT. */
771 void
772 finish_else_clause (tree if_stmt)
774 ELSE_CLAUSE (if_stmt) = pop_stmt_list (ELSE_CLAUSE (if_stmt));
777 /* Finish an if-statement. */
779 void
780 finish_if_stmt (tree if_stmt)
782 tree scope = IF_SCOPE (if_stmt);
783 IF_SCOPE (if_stmt) = NULL;
784 add_stmt (do_poplevel (scope));
787 /* Begin a while-statement. Returns a newly created WHILE_STMT if
788 appropriate. */
790 tree
791 begin_while_stmt (void)
793 tree r;
794 r = build_stmt (input_location, WHILE_STMT, NULL_TREE, NULL_TREE);
795 add_stmt (r);
796 WHILE_BODY (r) = do_pushlevel (sk_block);
797 begin_cond (&WHILE_COND (r));
798 return r;
801 /* Process the COND of a while-statement, which may be given by
802 WHILE_STMT. */
804 void
805 finish_while_stmt_cond (tree cond, tree while_stmt, bool ivdep,
806 unsigned short unroll)
808 cond = maybe_convert_cond (cond);
809 finish_cond (&WHILE_COND (while_stmt), cond);
810 begin_maybe_infinite_loop (cond);
811 if (ivdep && cond != error_mark_node)
812 WHILE_COND (while_stmt) = build3 (ANNOTATE_EXPR,
813 TREE_TYPE (WHILE_COND (while_stmt)),
814 WHILE_COND (while_stmt),
815 build_int_cst (integer_type_node,
816 annot_expr_ivdep_kind),
817 integer_zero_node);
818 if (unroll && cond != error_mark_node)
819 WHILE_COND (while_stmt) = build3 (ANNOTATE_EXPR,
820 TREE_TYPE (WHILE_COND (while_stmt)),
821 WHILE_COND (while_stmt),
822 build_int_cst (integer_type_node,
823 annot_expr_unroll_kind),
824 build_int_cst (integer_type_node,
825 unroll));
826 simplify_loop_decl_cond (&WHILE_COND (while_stmt), WHILE_BODY (while_stmt));
829 /* Finish a while-statement, which may be given by WHILE_STMT. */
831 void
832 finish_while_stmt (tree while_stmt)
834 end_maybe_infinite_loop (boolean_true_node);
835 WHILE_BODY (while_stmt) = do_poplevel (WHILE_BODY (while_stmt));
838 /* Begin a do-statement. Returns a newly created DO_STMT if
839 appropriate. */
841 tree
842 begin_do_stmt (void)
844 tree r = build_stmt (input_location, DO_STMT, NULL_TREE, NULL_TREE);
845 begin_maybe_infinite_loop (boolean_true_node);
846 add_stmt (r);
847 DO_BODY (r) = push_stmt_list ();
848 return r;
851 /* Finish the body of a do-statement, which may be given by DO_STMT. */
853 void
854 finish_do_body (tree do_stmt)
856 tree body = DO_BODY (do_stmt) = pop_stmt_list (DO_BODY (do_stmt));
858 if (TREE_CODE (body) == STATEMENT_LIST && STATEMENT_LIST_TAIL (body))
859 body = STATEMENT_LIST_TAIL (body)->stmt;
861 if (IS_EMPTY_STMT (body))
862 warning (OPT_Wempty_body,
863 "suggest explicit braces around empty body in %<do%> statement");
866 /* Finish a do-statement, which may be given by DO_STMT, and whose
867 COND is as indicated. */
869 void
870 finish_do_stmt (tree cond, tree do_stmt, bool ivdep, unsigned short unroll)
872 cond = maybe_convert_cond (cond);
873 end_maybe_infinite_loop (cond);
874 if (ivdep && cond != error_mark_node)
875 cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
876 build_int_cst (integer_type_node, annot_expr_ivdep_kind),
877 integer_zero_node);
878 if (unroll && cond != error_mark_node)
879 cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
880 build_int_cst (integer_type_node, annot_expr_unroll_kind),
881 build_int_cst (integer_type_node, unroll));
882 DO_COND (do_stmt) = cond;
885 /* Finish a return-statement. The EXPRESSION returned, if any, is as
886 indicated. */
888 tree
889 finish_return_stmt (tree expr)
891 tree r;
892 bool no_warning;
894 expr = check_return_expr (expr, &no_warning);
896 if (error_operand_p (expr)
897 || (flag_openmp && !check_omp_return ()))
899 /* Suppress -Wreturn-type for this function. */
900 if (warn_return_type)
901 TREE_NO_WARNING (current_function_decl) = true;
902 return error_mark_node;
905 if (!processing_template_decl)
907 if (warn_sequence_point)
908 verify_sequence_points (expr);
910 if (DECL_DESTRUCTOR_P (current_function_decl)
911 || (DECL_CONSTRUCTOR_P (current_function_decl)
912 && targetm.cxx.cdtor_returns_this ()))
914 /* Similarly, all destructors must run destructors for
915 base-classes before returning. So, all returns in a
916 destructor get sent to the DTOR_LABEL; finish_function emits
917 code to return a value there. */
918 return finish_goto_stmt (cdtor_label);
922 r = build_stmt (input_location, RETURN_EXPR, expr);
923 TREE_NO_WARNING (r) |= no_warning;
924 r = maybe_cleanup_point_expr_void (r);
925 r = add_stmt (r);
927 return r;
930 /* Begin the scope of a for-statement or a range-for-statement.
931 Both the returned trees are to be used in a call to
932 begin_for_stmt or begin_range_for_stmt. */
934 tree
935 begin_for_scope (tree *init)
937 tree scope = NULL_TREE;
938 if (flag_new_for_scope)
939 scope = do_pushlevel (sk_for);
941 if (processing_template_decl)
942 *init = push_stmt_list ();
943 else
944 *init = NULL_TREE;
946 return scope;
949 /* Begin a for-statement. Returns a new FOR_STMT.
950 SCOPE and INIT should be the return of begin_for_scope,
951 or both NULL_TREE */
953 tree
954 begin_for_stmt (tree scope, tree init)
956 tree r;
958 r = build_stmt (input_location, FOR_STMT, NULL_TREE, NULL_TREE,
959 NULL_TREE, NULL_TREE, NULL_TREE);
961 if (scope == NULL_TREE)
963 gcc_assert (!init || !flag_new_for_scope);
964 if (!init)
965 scope = begin_for_scope (&init);
967 FOR_INIT_STMT (r) = init;
968 FOR_SCOPE (r) = scope;
970 return r;
973 /* Finish the init-statement of a for-statement, which may be
974 given by FOR_STMT. */
976 void
977 finish_init_stmt (tree for_stmt)
979 if (processing_template_decl)
980 FOR_INIT_STMT (for_stmt) = pop_stmt_list (FOR_INIT_STMT (for_stmt));
981 add_stmt (for_stmt);
982 FOR_BODY (for_stmt) = do_pushlevel (sk_block);
983 begin_cond (&FOR_COND (for_stmt));
986 /* Finish the COND of a for-statement, which may be given by
987 FOR_STMT. */
989 void
990 finish_for_cond (tree cond, tree for_stmt, bool ivdep, unsigned short unroll)
992 cond = maybe_convert_cond (cond);
993 finish_cond (&FOR_COND (for_stmt), cond);
994 begin_maybe_infinite_loop (cond);
995 if (ivdep && cond != error_mark_node)
996 FOR_COND (for_stmt) = build3 (ANNOTATE_EXPR,
997 TREE_TYPE (FOR_COND (for_stmt)),
998 FOR_COND (for_stmt),
999 build_int_cst (integer_type_node,
1000 annot_expr_ivdep_kind),
1001 integer_zero_node);
1002 if (unroll && cond != error_mark_node)
1003 FOR_COND (for_stmt) = build3 (ANNOTATE_EXPR,
1004 TREE_TYPE (FOR_COND (for_stmt)),
1005 FOR_COND (for_stmt),
1006 build_int_cst (integer_type_node,
1007 annot_expr_unroll_kind),
1008 build_int_cst (integer_type_node,
1009 unroll));
1010 simplify_loop_decl_cond (&FOR_COND (for_stmt), FOR_BODY (for_stmt));
1013 /* Finish the increment-EXPRESSION in a for-statement, which may be
1014 given by FOR_STMT. */
1016 void
1017 finish_for_expr (tree expr, tree for_stmt)
1019 if (!expr)
1020 return;
1021 /* If EXPR is an overloaded function, issue an error; there is no
1022 context available to use to perform overload resolution. */
1023 if (type_unknown_p (expr))
1025 cxx_incomplete_type_error (expr, TREE_TYPE (expr));
1026 expr = error_mark_node;
1028 if (!processing_template_decl)
1030 if (warn_sequence_point)
1031 verify_sequence_points (expr);
1032 expr = convert_to_void (expr, ICV_THIRD_IN_FOR,
1033 tf_warning_or_error);
1035 else if (!type_dependent_expression_p (expr))
1036 convert_to_void (build_non_dependent_expr (expr), ICV_THIRD_IN_FOR,
1037 tf_warning_or_error);
1038 expr = maybe_cleanup_point_expr_void (expr);
1039 if (check_for_bare_parameter_packs (expr))
1040 expr = error_mark_node;
1041 FOR_EXPR (for_stmt) = expr;
1044 /* Finish the body of a for-statement, which may be given by
1045 FOR_STMT. The increment-EXPR for the loop must be
1046 provided.
1047 It can also finish RANGE_FOR_STMT. */
1049 void
1050 finish_for_stmt (tree for_stmt)
1052 end_maybe_infinite_loop (boolean_true_node);
1054 if (TREE_CODE (for_stmt) == RANGE_FOR_STMT)
1055 RANGE_FOR_BODY (for_stmt) = do_poplevel (RANGE_FOR_BODY (for_stmt));
1056 else
1057 FOR_BODY (for_stmt) = do_poplevel (FOR_BODY (for_stmt));
1059 /* Pop the scope for the body of the loop. */
1060 if (flag_new_for_scope)
1062 tree scope;
1063 tree *scope_ptr = (TREE_CODE (for_stmt) == RANGE_FOR_STMT
1064 ? &RANGE_FOR_SCOPE (for_stmt)
1065 : &FOR_SCOPE (for_stmt));
1066 scope = *scope_ptr;
1067 *scope_ptr = NULL;
1068 add_stmt (do_poplevel (scope));
1072 /* Begin a range-for-statement. Returns a new RANGE_FOR_STMT.
1073 SCOPE and INIT should be the return of begin_for_scope,
1074 or both NULL_TREE .
1075 To finish it call finish_for_stmt(). */
1077 tree
1078 begin_range_for_stmt (tree scope, tree init)
1080 tree r;
1082 begin_maybe_infinite_loop (boolean_false_node);
1084 r = build_stmt (input_location, RANGE_FOR_STMT,
1085 NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE);
1087 if (scope == NULL_TREE)
1089 gcc_assert (!init || !flag_new_for_scope);
1090 if (!init)
1091 scope = begin_for_scope (&init);
1094 /* RANGE_FOR_STMTs do not use nor save the init tree, so we
1095 pop it now. */
1096 if (init)
1097 pop_stmt_list (init);
1098 RANGE_FOR_SCOPE (r) = scope;
1100 return r;
1103 /* Finish the head of a range-based for statement, which may
1104 be given by RANGE_FOR_STMT. DECL must be the declaration
1105 and EXPR must be the loop expression. */
1107 void
1108 finish_range_for_decl (tree range_for_stmt, tree decl, tree expr)
1110 RANGE_FOR_DECL (range_for_stmt) = decl;
1111 RANGE_FOR_EXPR (range_for_stmt) = expr;
1112 add_stmt (range_for_stmt);
1113 RANGE_FOR_BODY (range_for_stmt) = do_pushlevel (sk_block);
1116 /* Finish a break-statement. */
1118 tree
1119 finish_break_stmt (void)
1121 /* In switch statements break is sometimes stylistically used after
1122 a return statement. This can lead to spurious warnings about
1123 control reaching the end of a non-void function when it is
1124 inlined. Note that we are calling block_may_fallthru with
1125 language specific tree nodes; this works because
1126 block_may_fallthru returns true when given something it does not
1127 understand. */
1128 if (!block_may_fallthru (cur_stmt_list))
1129 return void_node;
1130 note_break_stmt ();
1131 return add_stmt (build_stmt (input_location, BREAK_STMT));
1134 /* Finish a continue-statement. */
1136 tree
1137 finish_continue_stmt (void)
1139 return add_stmt (build_stmt (input_location, CONTINUE_STMT));
1142 /* Begin a switch-statement. Returns a new SWITCH_STMT if
1143 appropriate. */
1145 tree
1146 begin_switch_stmt (void)
1148 tree r, scope;
1150 scope = do_pushlevel (sk_cond);
1151 r = build_stmt (input_location, SWITCH_STMT, NULL_TREE, NULL_TREE, NULL_TREE, scope);
1153 begin_cond (&SWITCH_STMT_COND (r));
1155 return r;
1158 /* Finish the cond of a switch-statement. */
1160 void
1161 finish_switch_cond (tree cond, tree switch_stmt)
1163 tree orig_type = NULL;
1165 if (!processing_template_decl)
1167 /* Convert the condition to an integer or enumeration type. */
1168 cond = build_expr_type_conversion (WANT_INT | WANT_ENUM, cond, true);
1169 if (cond == NULL_TREE)
1171 error ("switch quantity not an integer");
1172 cond = error_mark_node;
1174 /* We want unlowered type here to handle enum bit-fields. */
1175 orig_type = unlowered_expr_type (cond);
1176 if (TREE_CODE (orig_type) != ENUMERAL_TYPE)
1177 orig_type = TREE_TYPE (cond);
1178 if (cond != error_mark_node)
1180 /* [stmt.switch]
1182 Integral promotions are performed. */
1183 cond = perform_integral_promotions (cond);
1184 cond = maybe_cleanup_point_expr (cond);
1187 if (check_for_bare_parameter_packs (cond))
1188 cond = error_mark_node;
1189 else if (!processing_template_decl && warn_sequence_point)
1190 verify_sequence_points (cond);
1192 finish_cond (&SWITCH_STMT_COND (switch_stmt), cond);
1193 SWITCH_STMT_TYPE (switch_stmt) = orig_type;
1194 add_stmt (switch_stmt);
1195 push_switch (switch_stmt);
1196 SWITCH_STMT_BODY (switch_stmt) = push_stmt_list ();
1199 /* Finish the body of a switch-statement, which may be given by
1200 SWITCH_STMT. The COND to switch on is indicated. */
1202 void
1203 finish_switch_stmt (tree switch_stmt)
1205 tree scope;
1207 SWITCH_STMT_BODY (switch_stmt) =
1208 pop_stmt_list (SWITCH_STMT_BODY (switch_stmt));
1209 pop_switch ();
1211 scope = SWITCH_STMT_SCOPE (switch_stmt);
1212 SWITCH_STMT_SCOPE (switch_stmt) = NULL;
1213 add_stmt (do_poplevel (scope));
1216 /* Begin a try-block. Returns a newly-created TRY_BLOCK if
1217 appropriate. */
1219 tree
1220 begin_try_block (void)
1222 tree r = build_stmt (input_location, TRY_BLOCK, NULL_TREE, NULL_TREE);
1223 add_stmt (r);
1224 TRY_STMTS (r) = push_stmt_list ();
1225 return r;
1228 /* Likewise, for a function-try-block. The block returned in
1229 *COMPOUND_STMT is an artificial outer scope, containing the
1230 function-try-block. */
1232 tree
1233 begin_function_try_block (tree *compound_stmt)
1235 tree r;
1236 /* This outer scope does not exist in the C++ standard, but we need
1237 a place to put __FUNCTION__ and similar variables. */
1238 *compound_stmt = begin_compound_stmt (0);
1239 r = begin_try_block ();
1240 FN_TRY_BLOCK_P (r) = 1;
1241 return r;
1244 /* Finish a try-block, which may be given by TRY_BLOCK. */
1246 void
1247 finish_try_block (tree try_block)
1249 TRY_STMTS (try_block) = pop_stmt_list (TRY_STMTS (try_block));
1250 TRY_HANDLERS (try_block) = push_stmt_list ();
1253 /* Finish the body of a cleanup try-block, which may be given by
1254 TRY_BLOCK. */
1256 void
1257 finish_cleanup_try_block (tree try_block)
1259 TRY_STMTS (try_block) = pop_stmt_list (TRY_STMTS (try_block));
1262 /* Finish an implicitly generated try-block, with a cleanup is given
1263 by CLEANUP. */
1265 void
1266 finish_cleanup (tree cleanup, tree try_block)
1268 TRY_HANDLERS (try_block) = cleanup;
1269 CLEANUP_P (try_block) = 1;
1272 /* Likewise, for a function-try-block. */
1274 void
1275 finish_function_try_block (tree try_block)
1277 finish_try_block (try_block);
1278 /* FIXME : something queer about CTOR_INITIALIZER somehow following
1279 the try block, but moving it inside. */
1280 in_function_try_handler = 1;
1283 /* Finish a handler-sequence for a try-block, which may be given by
1284 TRY_BLOCK. */
1286 void
1287 finish_handler_sequence (tree try_block)
1289 TRY_HANDLERS (try_block) = pop_stmt_list (TRY_HANDLERS (try_block));
1290 check_handlers (TRY_HANDLERS (try_block));
1293 /* Finish the handler-seq for a function-try-block, given by
1294 TRY_BLOCK. COMPOUND_STMT is the outer block created by
1295 begin_function_try_block. */
1297 void
1298 finish_function_handler_sequence (tree try_block, tree compound_stmt)
1300 in_function_try_handler = 0;
1301 finish_handler_sequence (try_block);
1302 finish_compound_stmt (compound_stmt);
1305 /* Begin a handler. Returns a HANDLER if appropriate. */
1307 tree
1308 begin_handler (void)
1310 tree r;
1312 r = build_stmt (input_location, HANDLER, NULL_TREE, NULL_TREE);
1313 add_stmt (r);
1315 /* Create a binding level for the eh_info and the exception object
1316 cleanup. */
1317 HANDLER_BODY (r) = do_pushlevel (sk_catch);
1319 return r;
1322 /* Finish the handler-parameters for a handler, which may be given by
1323 HANDLER. DECL is the declaration for the catch parameter, or NULL
1324 if this is a `catch (...)' clause. */
1326 void
1327 finish_handler_parms (tree decl, tree handler)
1329 tree type = NULL_TREE;
1330 if (processing_template_decl)
1332 if (decl)
1334 decl = pushdecl (decl);
1335 decl = push_template_decl (decl);
1336 HANDLER_PARMS (handler) = decl;
1337 type = TREE_TYPE (decl);
1340 else
1342 type = expand_start_catch_block (decl);
1343 if (warn_catch_value
1344 && type != NULL_TREE
1345 && type != error_mark_node
1346 && TREE_CODE (TREE_TYPE (decl)) != REFERENCE_TYPE)
1348 tree orig_type = TREE_TYPE (decl);
1349 if (CLASS_TYPE_P (orig_type))
1351 if (TYPE_POLYMORPHIC_P (orig_type))
1352 warning (OPT_Wcatch_value_,
1353 "catching polymorphic type %q#T by value", orig_type);
1354 else if (warn_catch_value > 1)
1355 warning (OPT_Wcatch_value_,
1356 "catching type %q#T by value", orig_type);
1358 else if (warn_catch_value > 2)
1359 warning (OPT_Wcatch_value_,
1360 "catching non-reference type %q#T", orig_type);
1363 HANDLER_TYPE (handler) = type;
1366 /* Finish a handler, which may be given by HANDLER. The BLOCKs are
1367 the return value from the matching call to finish_handler_parms. */
1369 void
1370 finish_handler (tree handler)
1372 if (!processing_template_decl)
1373 expand_end_catch_block ();
1374 HANDLER_BODY (handler) = do_poplevel (HANDLER_BODY (handler));
1377 /* Begin a compound statement. FLAGS contains some bits that control the
1378 behavior and context. If BCS_NO_SCOPE is set, the compound statement
1379 does not define a scope. If BCS_FN_BODY is set, this is the outermost
1380 block of a function. If BCS_TRY_BLOCK is set, this is the block
1381 created on behalf of a TRY statement. Returns a token to be passed to
1382 finish_compound_stmt. */
1384 tree
1385 begin_compound_stmt (unsigned int flags)
1387 tree r;
1389 if (flags & BCS_NO_SCOPE)
1391 r = push_stmt_list ();
1392 STATEMENT_LIST_NO_SCOPE (r) = 1;
1394 /* Normally, we try hard to keep the BLOCK for a statement-expression.
1395 But, if it's a statement-expression with a scopeless block, there's
1396 nothing to keep, and we don't want to accidentally keep a block
1397 *inside* the scopeless block. */
1398 keep_next_level (false);
1400 else
1402 scope_kind sk = sk_block;
1403 if (flags & BCS_TRY_BLOCK)
1404 sk = sk_try;
1405 else if (flags & BCS_TRANSACTION)
1406 sk = sk_transaction;
1407 r = do_pushlevel (sk);
1410 /* When processing a template, we need to remember where the braces were,
1411 so that we can set up identical scopes when instantiating the template
1412 later. BIND_EXPR is a handy candidate for this.
1413 Note that do_poplevel won't create a BIND_EXPR itself here (and thus
1414 result in nested BIND_EXPRs), since we don't build BLOCK nodes when
1415 processing templates. */
1416 if (processing_template_decl)
1418 r = build3 (BIND_EXPR, NULL, NULL, r, NULL);
1419 BIND_EXPR_TRY_BLOCK (r) = (flags & BCS_TRY_BLOCK) != 0;
1420 BIND_EXPR_BODY_BLOCK (r) = (flags & BCS_FN_BODY) != 0;
1421 TREE_SIDE_EFFECTS (r) = 1;
1424 return r;
1427 /* Finish a compound-statement, which is given by STMT. */
1429 void
1430 finish_compound_stmt (tree stmt)
1432 if (TREE_CODE (stmt) == BIND_EXPR)
1434 tree body = do_poplevel (BIND_EXPR_BODY (stmt));
1435 /* If the STATEMENT_LIST is empty and this BIND_EXPR isn't special,
1436 discard the BIND_EXPR so it can be merged with the containing
1437 STATEMENT_LIST. */
1438 if (TREE_CODE (body) == STATEMENT_LIST
1439 && STATEMENT_LIST_HEAD (body) == NULL
1440 && !BIND_EXPR_BODY_BLOCK (stmt)
1441 && !BIND_EXPR_TRY_BLOCK (stmt))
1442 stmt = body;
1443 else
1444 BIND_EXPR_BODY (stmt) = body;
1446 else if (STATEMENT_LIST_NO_SCOPE (stmt))
1447 stmt = pop_stmt_list (stmt);
1448 else
1450 /* Destroy any ObjC "super" receivers that may have been
1451 created. */
1452 objc_clear_super_receiver ();
1454 stmt = do_poplevel (stmt);
1457 /* ??? See c_end_compound_stmt wrt statement expressions. */
1458 add_stmt (stmt);
1461 /* Finish an asm-statement, whose components are a STRING, some
1462 OUTPUT_OPERANDS, some INPUT_OPERANDS, some CLOBBERS and some
1463 LABELS. Also note whether the asm-statement should be
1464 considered volatile. */
1466 tree
1467 finish_asm_stmt (int volatile_p, tree string, tree output_operands,
1468 tree input_operands, tree clobbers, tree labels)
1470 tree r;
1471 tree t;
1472 int ninputs = list_length (input_operands);
1473 int noutputs = list_length (output_operands);
1475 if (!processing_template_decl)
1477 const char *constraint;
1478 const char **oconstraints;
1479 bool allows_mem, allows_reg, is_inout;
1480 tree operand;
1481 int i;
1483 oconstraints = XALLOCAVEC (const char *, noutputs);
1485 string = resolve_asm_operand_names (string, output_operands,
1486 input_operands, labels);
1488 for (i = 0, t = output_operands; t; t = TREE_CHAIN (t), ++i)
1490 operand = TREE_VALUE (t);
1492 /* ??? Really, this should not be here. Users should be using a
1493 proper lvalue, dammit. But there's a long history of using
1494 casts in the output operands. In cases like longlong.h, this
1495 becomes a primitive form of typechecking -- if the cast can be
1496 removed, then the output operand had a type of the proper width;
1497 otherwise we'll get an error. Gross, but ... */
1498 STRIP_NOPS (operand);
1500 operand = mark_lvalue_use (operand);
1502 if (!lvalue_or_else (operand, lv_asm, tf_warning_or_error))
1503 operand = error_mark_node;
1505 if (operand != error_mark_node
1506 && (TREE_READONLY (operand)
1507 || CP_TYPE_CONST_P (TREE_TYPE (operand))
1508 /* Functions are not modifiable, even though they are
1509 lvalues. */
1510 || TREE_CODE (TREE_TYPE (operand)) == FUNCTION_TYPE
1511 || TREE_CODE (TREE_TYPE (operand)) == METHOD_TYPE
1512 /* If it's an aggregate and any field is const, then it is
1513 effectively const. */
1514 || (CLASS_TYPE_P (TREE_TYPE (operand))
1515 && C_TYPE_FIELDS_READONLY (TREE_TYPE (operand)))))
1516 cxx_readonly_error (operand, lv_asm);
1518 tree *op = &operand;
1519 while (TREE_CODE (*op) == COMPOUND_EXPR)
1520 op = &TREE_OPERAND (*op, 1);
1521 switch (TREE_CODE (*op))
1523 case PREINCREMENT_EXPR:
1524 case PREDECREMENT_EXPR:
1525 case MODIFY_EXPR:
1526 *op = genericize_compound_lvalue (*op);
1527 op = &TREE_OPERAND (*op, 1);
1528 break;
1529 default:
1530 break;
1533 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
1534 oconstraints[i] = constraint;
1536 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
1537 &allows_mem, &allows_reg, &is_inout))
1539 /* If the operand is going to end up in memory,
1540 mark it addressable. */
1541 if (!allows_reg && !cxx_mark_addressable (*op))
1542 operand = error_mark_node;
1544 else
1545 operand = error_mark_node;
1547 TREE_VALUE (t) = operand;
1550 for (i = 0, t = input_operands; t; ++i, t = TREE_CHAIN (t))
1552 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
1553 bool constraint_parsed
1554 = parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
1555 oconstraints, &allows_mem, &allows_reg);
1556 /* If the operand is going to end up in memory, don't call
1557 decay_conversion. */
1558 if (constraint_parsed && !allows_reg && allows_mem)
1559 operand = mark_lvalue_use (TREE_VALUE (t));
1560 else
1561 operand = decay_conversion (TREE_VALUE (t), tf_warning_or_error);
1563 /* If the type of the operand hasn't been determined (e.g.,
1564 because it involves an overloaded function), then issue
1565 an error message. There's no context available to
1566 resolve the overloading. */
1567 if (TREE_TYPE (operand) == unknown_type_node)
1569 error ("type of asm operand %qE could not be determined",
1570 TREE_VALUE (t));
1571 operand = error_mark_node;
1574 if (constraint_parsed)
1576 /* If the operand is going to end up in memory,
1577 mark it addressable. */
1578 if (!allows_reg && allows_mem)
1580 /* Strip the nops as we allow this case. FIXME, this really
1581 should be rejected or made deprecated. */
1582 STRIP_NOPS (operand);
1584 tree *op = &operand;
1585 while (TREE_CODE (*op) == COMPOUND_EXPR)
1586 op = &TREE_OPERAND (*op, 1);
1587 switch (TREE_CODE (*op))
1589 case PREINCREMENT_EXPR:
1590 case PREDECREMENT_EXPR:
1591 case MODIFY_EXPR:
1592 *op = genericize_compound_lvalue (*op);
1593 op = &TREE_OPERAND (*op, 1);
1594 break;
1595 default:
1596 break;
1599 if (!cxx_mark_addressable (*op))
1600 operand = error_mark_node;
1602 else if (!allows_reg && !allows_mem)
1604 /* If constraint allows neither register nor memory,
1605 try harder to get a constant. */
1606 tree constop = maybe_constant_value (operand);
1607 if (TREE_CONSTANT (constop))
1608 operand = constop;
1611 else
1612 operand = error_mark_node;
1614 TREE_VALUE (t) = operand;
1618 r = build_stmt (input_location, ASM_EXPR, string,
1619 output_operands, input_operands,
1620 clobbers, labels);
1621 ASM_VOLATILE_P (r) = volatile_p || noutputs == 0;
1622 r = maybe_cleanup_point_expr_void (r);
1623 return add_stmt (r);
1626 /* Finish a label with the indicated NAME. Returns the new label. */
1628 tree
1629 finish_label_stmt (tree name)
1631 tree decl = define_label (input_location, name);
1633 if (decl == error_mark_node)
1634 return error_mark_node;
1636 add_stmt (build_stmt (input_location, LABEL_EXPR, decl));
1638 return decl;
1641 /* Finish a series of declarations for local labels. G++ allows users
1642 to declare "local" labels, i.e., labels with scope. This extension
1643 is useful when writing code involving statement-expressions. */
1645 void
1646 finish_label_decl (tree name)
1648 if (!at_function_scope_p ())
1650 error ("__label__ declarations are only allowed in function scopes");
1651 return;
1654 add_decl_expr (declare_local_label (name));
1657 /* When DECL goes out of scope, make sure that CLEANUP is executed. */
1659 void
1660 finish_decl_cleanup (tree decl, tree cleanup)
1662 push_cleanup (decl, cleanup, false);
1665 /* If the current scope exits with an exception, run CLEANUP. */
1667 void
1668 finish_eh_cleanup (tree cleanup)
1670 push_cleanup (NULL, cleanup, true);
1673 /* The MEM_INITS is a list of mem-initializers, in reverse of the
1674 order they were written by the user. Each node is as for
1675 emit_mem_initializers. */
1677 void
1678 finish_mem_initializers (tree mem_inits)
1680 /* Reorder the MEM_INITS so that they are in the order they appeared
1681 in the source program. */
1682 mem_inits = nreverse (mem_inits);
1684 if (processing_template_decl)
1686 tree mem;
1688 for (mem = mem_inits; mem; mem = TREE_CHAIN (mem))
1690 /* If the TREE_PURPOSE is a TYPE_PACK_EXPANSION, skip the
1691 check for bare parameter packs in the TREE_VALUE, because
1692 any parameter packs in the TREE_VALUE have already been
1693 bound as part of the TREE_PURPOSE. See
1694 make_pack_expansion for more information. */
1695 if (TREE_CODE (TREE_PURPOSE (mem)) != TYPE_PACK_EXPANSION
1696 && check_for_bare_parameter_packs (TREE_VALUE (mem)))
1697 TREE_VALUE (mem) = error_mark_node;
1700 add_stmt (build_min_nt_loc (UNKNOWN_LOCATION,
1701 CTOR_INITIALIZER, mem_inits));
1703 else
1704 emit_mem_initializers (mem_inits);
1707 /* Obfuscate EXPR if it looks like an id-expression or member access so
1708 that the call to finish_decltype in do_auto_deduction will give the
1709 right result. */
1711 tree
1712 force_paren_expr (tree expr)
1714 /* This is only needed for decltype(auto) in C++14. */
1715 if (cxx_dialect < cxx14)
1716 return expr;
1718 /* If we're in unevaluated context, we can't be deducing a
1719 return/initializer type, so we don't need to mess with this. */
1720 if (cp_unevaluated_operand)
1721 return expr;
1723 if (!DECL_P (expr) && TREE_CODE (expr) != COMPONENT_REF
1724 && TREE_CODE (expr) != SCOPE_REF)
1725 return expr;
1727 if (TREE_CODE (expr) == COMPONENT_REF
1728 || TREE_CODE (expr) == SCOPE_REF)
1729 REF_PARENTHESIZED_P (expr) = true;
1730 else if (processing_template_decl)
1731 expr = build1 (PAREN_EXPR, TREE_TYPE (expr), expr);
1732 else if (VAR_P (expr) && DECL_HARD_REGISTER (expr))
1733 /* We can't bind a hard register variable to a reference. */;
1734 else
1736 cp_lvalue_kind kind = lvalue_kind (expr);
1737 if ((kind & ~clk_class) != clk_none)
1739 tree type = unlowered_expr_type (expr);
1740 bool rval = !!(kind & clk_rvalueref);
1741 type = cp_build_reference_type (type, rval);
1742 /* This inhibits warnings in, eg, cxx_mark_addressable
1743 (c++/60955). */
1744 warning_sentinel s (extra_warnings);
1745 expr = build_static_cast (type, expr, tf_error);
1746 if (expr != error_mark_node)
1747 REF_PARENTHESIZED_P (expr) = true;
1751 return expr;
1754 /* If T is an id-expression obfuscated by force_paren_expr, undo the
1755 obfuscation and return the underlying id-expression. Otherwise
1756 return T. */
1758 tree
1759 maybe_undo_parenthesized_ref (tree t)
1761 if (cxx_dialect < cxx14)
1762 return t;
1764 if (INDIRECT_REF_P (t) && REF_PARENTHESIZED_P (t))
1766 t = TREE_OPERAND (t, 0);
1767 while (TREE_CODE (t) == NON_LVALUE_EXPR
1768 || TREE_CODE (t) == NOP_EXPR)
1769 t = TREE_OPERAND (t, 0);
1771 gcc_assert (TREE_CODE (t) == ADDR_EXPR
1772 || TREE_CODE (t) == STATIC_CAST_EXPR);
1773 t = TREE_OPERAND (t, 0);
1775 else if (TREE_CODE (t) == PAREN_EXPR)
1776 t = TREE_OPERAND (t, 0);
1778 return t;
1781 /* Finish a parenthesized expression EXPR. */
1783 cp_expr
1784 finish_parenthesized_expr (cp_expr expr)
1786 if (EXPR_P (expr))
1787 /* This inhibits warnings in c_common_truthvalue_conversion. */
1788 TREE_NO_WARNING (expr) = 1;
1790 if (TREE_CODE (expr) == OFFSET_REF
1791 || TREE_CODE (expr) == SCOPE_REF)
1792 /* [expr.unary.op]/3 The qualified id of a pointer-to-member must not be
1793 enclosed in parentheses. */
1794 PTRMEM_OK_P (expr) = 0;
1796 if (TREE_CODE (expr) == STRING_CST)
1797 PAREN_STRING_LITERAL_P (expr) = 1;
1799 expr = cp_expr (force_paren_expr (expr), expr.get_location ());
1801 return expr;
1804 /* Finish a reference to a non-static data member (DECL) that is not
1805 preceded by `.' or `->'. */
1807 tree
1808 finish_non_static_data_member (tree decl, tree object, tree qualifying_scope)
1810 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
1811 bool try_omp_private = !object && omp_private_member_map;
1812 tree ret;
1814 if (!object)
1816 tree scope = qualifying_scope;
1817 if (scope == NULL_TREE)
1818 scope = context_for_name_lookup (decl);
1819 object = maybe_dummy_object (scope, NULL);
1822 object = maybe_resolve_dummy (object, true);
1823 if (object == error_mark_node)
1824 return error_mark_node;
1826 /* DR 613/850: Can use non-static data members without an associated
1827 object in sizeof/decltype/alignof. */
1828 if (is_dummy_object (object) && cp_unevaluated_operand == 0
1829 && (!processing_template_decl || !current_class_ref))
1831 if (current_function_decl
1832 && DECL_STATIC_FUNCTION_P (current_function_decl))
1833 error ("invalid use of member %qD in static member function", decl);
1834 else
1835 error ("invalid use of non-static data member %qD", decl);
1836 inform (DECL_SOURCE_LOCATION (decl), "declared here");
1838 return error_mark_node;
1841 if (current_class_ptr)
1842 TREE_USED (current_class_ptr) = 1;
1843 if (processing_template_decl && !qualifying_scope)
1845 tree type = TREE_TYPE (decl);
1847 if (TREE_CODE (type) == REFERENCE_TYPE)
1848 /* Quals on the object don't matter. */;
1849 else if (PACK_EXPANSION_P (type))
1850 /* Don't bother trying to represent this. */
1851 type = NULL_TREE;
1852 else
1854 /* Set the cv qualifiers. */
1855 int quals = cp_type_quals (TREE_TYPE (object));
1857 if (DECL_MUTABLE_P (decl))
1858 quals &= ~TYPE_QUAL_CONST;
1860 quals |= cp_type_quals (TREE_TYPE (decl));
1861 type = cp_build_qualified_type (type, quals);
1864 ret = (convert_from_reference
1865 (build_min (COMPONENT_REF, type, object, decl, NULL_TREE)));
1867 /* If PROCESSING_TEMPLATE_DECL is nonzero here, then
1868 QUALIFYING_SCOPE is also non-null. Wrap this in a SCOPE_REF
1869 for now. */
1870 else if (processing_template_decl)
1871 ret = build_qualified_name (TREE_TYPE (decl),
1872 qualifying_scope,
1873 decl,
1874 /*template_p=*/false);
1875 else
1877 tree access_type = TREE_TYPE (object);
1879 perform_or_defer_access_check (TYPE_BINFO (access_type), decl,
1880 decl, tf_warning_or_error);
1882 /* If the data member was named `C::M', convert `*this' to `C'
1883 first. */
1884 if (qualifying_scope)
1886 tree binfo = NULL_TREE;
1887 object = build_scoped_ref (object, qualifying_scope,
1888 &binfo);
1891 ret = build_class_member_access_expr (object, decl,
1892 /*access_path=*/NULL_TREE,
1893 /*preserve_reference=*/false,
1894 tf_warning_or_error);
1896 if (try_omp_private)
1898 tree *v = omp_private_member_map->get (decl);
1899 if (v)
1900 ret = convert_from_reference (*v);
1902 return ret;
1905 /* If we are currently parsing a template and we encountered a typedef
1906 TYPEDEF_DECL that is being accessed though CONTEXT, this function
1907 adds the typedef to a list tied to the current template.
1908 At template instantiation time, that list is walked and access check
1909 performed for each typedef.
1910 LOCATION is the location of the usage point of TYPEDEF_DECL. */
1912 void
1913 add_typedef_to_current_template_for_access_check (tree typedef_decl,
1914 tree context,
1915 location_t location)
1917 tree template_info = NULL;
1918 tree cs = current_scope ();
1920 if (!is_typedef_decl (typedef_decl)
1921 || !context
1922 || !CLASS_TYPE_P (context)
1923 || !cs)
1924 return;
1926 if (CLASS_TYPE_P (cs) || TREE_CODE (cs) == FUNCTION_DECL)
1927 template_info = get_template_info (cs);
1929 if (template_info
1930 && TI_TEMPLATE (template_info)
1931 && !currently_open_class (context))
1932 append_type_to_template_for_access_check (cs, typedef_decl,
1933 context, location);
1936 /* DECL was the declaration to which a qualified-id resolved. Issue
1937 an error message if it is not accessible. If OBJECT_TYPE is
1938 non-NULL, we have just seen `x->' or `x.' and OBJECT_TYPE is the
1939 type of `*x', or `x', respectively. If the DECL was named as
1940 `A::B' then NESTED_NAME_SPECIFIER is `A'. */
1942 void
1943 check_accessibility_of_qualified_id (tree decl,
1944 tree object_type,
1945 tree nested_name_specifier)
1947 tree scope;
1948 tree qualifying_type = NULL_TREE;
1950 /* If we are parsing a template declaration and if decl is a typedef,
1951 add it to a list tied to the template.
1952 At template instantiation time, that list will be walked and
1953 access check performed. */
1954 add_typedef_to_current_template_for_access_check (decl,
1955 nested_name_specifier
1956 ? nested_name_specifier
1957 : DECL_CONTEXT (decl),
1958 input_location);
1960 /* If we're not checking, return immediately. */
1961 if (deferred_access_no_check)
1962 return;
1964 /* Determine the SCOPE of DECL. */
1965 scope = context_for_name_lookup (decl);
1966 /* If the SCOPE is not a type, then DECL is not a member. */
1967 if (!TYPE_P (scope))
1968 return;
1969 /* Compute the scope through which DECL is being accessed. */
1970 if (object_type
1971 /* OBJECT_TYPE might not be a class type; consider:
1973 class A { typedef int I; };
1974 I *p;
1975 p->A::I::~I();
1977 In this case, we will have "A::I" as the DECL, but "I" as the
1978 OBJECT_TYPE. */
1979 && CLASS_TYPE_P (object_type)
1980 && DERIVED_FROM_P (scope, object_type))
1981 /* If we are processing a `->' or `.' expression, use the type of the
1982 left-hand side. */
1983 qualifying_type = object_type;
1984 else if (nested_name_specifier)
1986 /* If the reference is to a non-static member of the
1987 current class, treat it as if it were referenced through
1988 `this'. */
1989 tree ct;
1990 if (DECL_NONSTATIC_MEMBER_P (decl)
1991 && current_class_ptr
1992 && DERIVED_FROM_P (scope, ct = current_nonlambda_class_type ()))
1993 qualifying_type = ct;
1994 /* Otherwise, use the type indicated by the
1995 nested-name-specifier. */
1996 else
1997 qualifying_type = nested_name_specifier;
1999 else
2000 /* Otherwise, the name must be from the current class or one of
2001 its bases. */
2002 qualifying_type = currently_open_derived_class (scope);
2004 if (qualifying_type
2005 /* It is possible for qualifying type to be a TEMPLATE_TYPE_PARM
2006 or similar in a default argument value. */
2007 && CLASS_TYPE_P (qualifying_type)
2008 && !dependent_type_p (qualifying_type))
2009 perform_or_defer_access_check (TYPE_BINFO (qualifying_type), decl,
2010 decl, tf_warning_or_error);
2013 /* EXPR is the result of a qualified-id. The QUALIFYING_CLASS was the
2014 class named to the left of the "::" operator. DONE is true if this
2015 expression is a complete postfix-expression; it is false if this
2016 expression is followed by '->', '[', '(', etc. ADDRESS_P is true
2017 iff this expression is the operand of '&'. TEMPLATE_P is true iff
2018 the qualified-id was of the form "A::template B". TEMPLATE_ARG_P
2019 is true iff this qualified name appears as a template argument. */
2021 tree
2022 finish_qualified_id_expr (tree qualifying_class,
2023 tree expr,
2024 bool done,
2025 bool address_p,
2026 bool template_p,
2027 bool template_arg_p,
2028 tsubst_flags_t complain)
2030 gcc_assert (TYPE_P (qualifying_class));
2032 if (error_operand_p (expr))
2033 return error_mark_node;
2035 if ((DECL_P (expr) || BASELINK_P (expr))
2036 && !mark_used (expr, complain))
2037 return error_mark_node;
2039 if (template_p)
2041 if (TREE_CODE (expr) == UNBOUND_CLASS_TEMPLATE)
2043 /* cp_parser_lookup_name thought we were looking for a type,
2044 but we're actually looking for a declaration. */
2045 qualifying_class = TYPE_CONTEXT (expr);
2046 expr = TYPE_IDENTIFIER (expr);
2048 else
2049 check_template_keyword (expr);
2052 /* If EXPR occurs as the operand of '&', use special handling that
2053 permits a pointer-to-member. */
2054 if (address_p && done)
2056 if (TREE_CODE (expr) == SCOPE_REF)
2057 expr = TREE_OPERAND (expr, 1);
2058 expr = build_offset_ref (qualifying_class, expr,
2059 /*address_p=*/true, complain);
2060 return expr;
2063 /* No need to check access within an enum. */
2064 if (TREE_CODE (qualifying_class) == ENUMERAL_TYPE)
2065 return expr;
2067 /* Within the scope of a class, turn references to non-static
2068 members into expression of the form "this->...". */
2069 if (template_arg_p)
2070 /* But, within a template argument, we do not want make the
2071 transformation, as there is no "this" pointer. */
2073 else if (TREE_CODE (expr) == FIELD_DECL)
2075 push_deferring_access_checks (dk_no_check);
2076 expr = finish_non_static_data_member (expr, NULL_TREE,
2077 qualifying_class);
2078 pop_deferring_access_checks ();
2080 else if (BASELINK_P (expr))
2082 /* See if any of the functions are non-static members. */
2083 /* If so, the expression may be relative to 'this'. */
2084 if (!shared_member_p (expr)
2085 && current_class_ptr
2086 && DERIVED_FROM_P (qualifying_class,
2087 current_nonlambda_class_type ()))
2088 expr = (build_class_member_access_expr
2089 (maybe_dummy_object (qualifying_class, NULL),
2090 expr,
2091 BASELINK_ACCESS_BINFO (expr),
2092 /*preserve_reference=*/false,
2093 complain));
2094 else if (done)
2095 /* The expression is a qualified name whose address is not
2096 being taken. */
2097 expr = build_offset_ref (qualifying_class, expr, /*address_p=*/false,
2098 complain);
2100 else
2102 /* In a template, return a SCOPE_REF for most qualified-ids
2103 so that we can check access at instantiation time. But if
2104 we're looking at a member of the current instantiation, we
2105 know we have access and building up the SCOPE_REF confuses
2106 non-type template argument handling. */
2107 if (processing_template_decl
2108 && (!currently_open_class (qualifying_class)
2109 || TREE_CODE (expr) == BIT_NOT_EXPR))
2110 expr = build_qualified_name (TREE_TYPE (expr),
2111 qualifying_class, expr,
2112 template_p);
2114 expr = convert_from_reference (expr);
2117 return expr;
2120 /* Begin a statement-expression. The value returned must be passed to
2121 finish_stmt_expr. */
2123 tree
2124 begin_stmt_expr (void)
2126 return push_stmt_list ();
2129 /* Process the final expression of a statement expression. EXPR can be
2130 NULL, if the final expression is empty. Return a STATEMENT_LIST
2131 containing all the statements in the statement-expression, or
2132 ERROR_MARK_NODE if there was an error. */
2134 tree
2135 finish_stmt_expr_expr (tree expr, tree stmt_expr)
2137 if (error_operand_p (expr))
2139 /* The type of the statement-expression is the type of the last
2140 expression. */
2141 TREE_TYPE (stmt_expr) = error_mark_node;
2142 return error_mark_node;
2145 /* If the last statement does not have "void" type, then the value
2146 of the last statement is the value of the entire expression. */
2147 if (expr)
2149 tree type = TREE_TYPE (expr);
2151 if (type && type_unknown_p (type))
2153 error ("a statement expression is an insufficient context"
2154 " for overload resolution");
2155 TREE_TYPE (stmt_expr) = error_mark_node;
2156 return error_mark_node;
2158 else if (processing_template_decl)
2160 expr = build_stmt (input_location, EXPR_STMT, expr);
2161 expr = add_stmt (expr);
2162 /* Mark the last statement so that we can recognize it as such at
2163 template-instantiation time. */
2164 EXPR_STMT_STMT_EXPR_RESULT (expr) = 1;
2166 else if (VOID_TYPE_P (type))
2168 /* Just treat this like an ordinary statement. */
2169 expr = finish_expr_stmt (expr);
2171 else
2173 /* It actually has a value we need to deal with. First, force it
2174 to be an rvalue so that we won't need to build up a copy
2175 constructor call later when we try to assign it to something. */
2176 expr = force_rvalue (expr, tf_warning_or_error);
2177 if (error_operand_p (expr))
2178 return error_mark_node;
2180 /* Update for array-to-pointer decay. */
2181 type = TREE_TYPE (expr);
2183 /* Wrap it in a CLEANUP_POINT_EXPR and add it to the list like a
2184 normal statement, but don't convert to void or actually add
2185 the EXPR_STMT. */
2186 if (TREE_CODE (expr) != CLEANUP_POINT_EXPR)
2187 expr = maybe_cleanup_point_expr (expr);
2188 add_stmt (expr);
2191 /* The type of the statement-expression is the type of the last
2192 expression. */
2193 TREE_TYPE (stmt_expr) = type;
2196 return stmt_expr;
2199 /* Finish a statement-expression. EXPR should be the value returned
2200 by the previous begin_stmt_expr. Returns an expression
2201 representing the statement-expression. */
2203 tree
2204 finish_stmt_expr (tree stmt_expr, bool has_no_scope)
2206 tree type;
2207 tree result;
2209 if (error_operand_p (stmt_expr))
2211 pop_stmt_list (stmt_expr);
2212 return error_mark_node;
2215 gcc_assert (TREE_CODE (stmt_expr) == STATEMENT_LIST);
2217 type = TREE_TYPE (stmt_expr);
2218 result = pop_stmt_list (stmt_expr);
2219 TREE_TYPE (result) = type;
2221 if (processing_template_decl)
2223 result = build_min (STMT_EXPR, type, result);
2224 TREE_SIDE_EFFECTS (result) = 1;
2225 STMT_EXPR_NO_SCOPE (result) = has_no_scope;
2227 else if (CLASS_TYPE_P (type))
2229 /* Wrap the statement-expression in a TARGET_EXPR so that the
2230 temporary object created by the final expression is destroyed at
2231 the end of the full-expression containing the
2232 statement-expression. */
2233 result = force_target_expr (type, result, tf_warning_or_error);
2236 return result;
2239 /* Returns the expression which provides the value of STMT_EXPR. */
2241 tree
2242 stmt_expr_value_expr (tree stmt_expr)
2244 tree t = STMT_EXPR_STMT (stmt_expr);
2246 if (TREE_CODE (t) == BIND_EXPR)
2247 t = BIND_EXPR_BODY (t);
2249 if (TREE_CODE (t) == STATEMENT_LIST && STATEMENT_LIST_TAIL (t))
2250 t = STATEMENT_LIST_TAIL (t)->stmt;
2252 if (TREE_CODE (t) == EXPR_STMT)
2253 t = EXPR_STMT_EXPR (t);
2255 return t;
2258 /* Return TRUE iff EXPR_STMT is an empty list of
2259 expression statements. */
2261 bool
2262 empty_expr_stmt_p (tree expr_stmt)
2264 tree body = NULL_TREE;
2266 if (expr_stmt == void_node)
2267 return true;
2269 if (expr_stmt)
2271 if (TREE_CODE (expr_stmt) == EXPR_STMT)
2272 body = EXPR_STMT_EXPR (expr_stmt);
2273 else if (TREE_CODE (expr_stmt) == STATEMENT_LIST)
2274 body = expr_stmt;
2277 if (body)
2279 if (TREE_CODE (body) == STATEMENT_LIST)
2280 return tsi_end_p (tsi_start (body));
2281 else
2282 return empty_expr_stmt_p (body);
2284 return false;
2287 /* Perform Koenig lookup. FN is the postfix-expression representing
2288 the function (or functions) to call; ARGS are the arguments to the
2289 call. Returns the functions to be considered by overload resolution. */
2291 cp_expr
2292 perform_koenig_lookup (cp_expr fn, vec<tree, va_gc> *args,
2293 tsubst_flags_t complain)
2295 tree identifier = NULL_TREE;
2296 tree functions = NULL_TREE;
2297 tree tmpl_args = NULL_TREE;
2298 bool template_id = false;
2299 location_t loc = fn.get_location ();
2301 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
2303 /* Use a separate flag to handle null args. */
2304 template_id = true;
2305 tmpl_args = TREE_OPERAND (fn, 1);
2306 fn = TREE_OPERAND (fn, 0);
2309 /* Find the name of the overloaded function. */
2310 if (identifier_p (fn))
2311 identifier = fn;
2312 else
2314 functions = fn;
2315 identifier = OVL_NAME (functions);
2318 /* A call to a namespace-scope function using an unqualified name.
2320 Do Koenig lookup -- unless any of the arguments are
2321 type-dependent. */
2322 if (!any_type_dependent_arguments_p (args)
2323 && !any_dependent_template_arguments_p (tmpl_args))
2325 fn = lookup_arg_dependent (identifier, functions, args);
2326 if (!fn)
2328 /* The unqualified name could not be resolved. */
2329 if (complain & tf_error)
2330 fn = unqualified_fn_lookup_error (cp_expr (identifier, loc));
2331 else
2332 fn = identifier;
2336 if (fn && template_id && fn != error_mark_node)
2337 fn = build2 (TEMPLATE_ID_EXPR, unknown_type_node, fn, tmpl_args);
2339 return fn;
2342 /* Generate an expression for `FN (ARGS)'. This may change the
2343 contents of ARGS.
2345 If DISALLOW_VIRTUAL is true, the call to FN will be not generated
2346 as a virtual call, even if FN is virtual. (This flag is set when
2347 encountering an expression where the function name is explicitly
2348 qualified. For example a call to `X::f' never generates a virtual
2349 call.)
2351 Returns code for the call. */
2353 tree
2354 finish_call_expr (tree fn, vec<tree, va_gc> **args, bool disallow_virtual,
2355 bool koenig_p, tsubst_flags_t complain)
2357 tree result;
2358 tree orig_fn;
2359 vec<tree, va_gc> *orig_args = NULL;
2361 if (fn == error_mark_node)
2362 return error_mark_node;
2364 gcc_assert (!TYPE_P (fn));
2366 /* If FN may be a FUNCTION_DECL obfuscated by force_paren_expr, undo
2367 it so that we can tell this is a call to a known function. */
2368 fn = maybe_undo_parenthesized_ref (fn);
2370 orig_fn = fn;
2372 if (processing_template_decl)
2374 /* If FN is a local extern declaration or set thereof, look them up
2375 again at instantiation time. */
2376 if (is_overloaded_fn (fn))
2378 tree ifn = get_first_fn (fn);
2379 if (TREE_CODE (ifn) == FUNCTION_DECL
2380 && DECL_LOCAL_FUNCTION_P (ifn))
2381 orig_fn = DECL_NAME (ifn);
2384 /* If the call expression is dependent, build a CALL_EXPR node
2385 with no type; type_dependent_expression_p recognizes
2386 expressions with no type as being dependent. */
2387 if (type_dependent_expression_p (fn)
2388 || any_type_dependent_arguments_p (*args))
2390 result = build_min_nt_call_vec (orig_fn, *args);
2391 SET_EXPR_LOCATION (result, EXPR_LOC_OR_LOC (fn, input_location));
2392 KOENIG_LOOKUP_P (result) = koenig_p;
2393 if (is_overloaded_fn (fn))
2395 fn = get_fns (fn);
2396 lookup_keep (fn, true);
2399 if (cfun)
2401 bool abnormal = true;
2402 for (lkp_iterator iter (fn); abnormal && iter; ++iter)
2404 tree fndecl = *iter;
2405 if (TREE_CODE (fndecl) != FUNCTION_DECL
2406 || !TREE_THIS_VOLATILE (fndecl))
2407 abnormal = false;
2409 /* FIXME: Stop warning about falling off end of non-void
2410 function. But this is wrong. Even if we only see
2411 no-return fns at this point, we could select a
2412 future-defined return fn during instantiation. Or
2413 vice-versa. */
2414 if (abnormal)
2415 current_function_returns_abnormally = 1;
2417 return result;
2419 orig_args = make_tree_vector_copy (*args);
2420 if (!BASELINK_P (fn)
2421 && TREE_CODE (fn) != PSEUDO_DTOR_EXPR
2422 && TREE_TYPE (fn) != unknown_type_node)
2423 fn = build_non_dependent_expr (fn);
2424 make_args_non_dependent (*args);
2427 if (TREE_CODE (fn) == COMPONENT_REF)
2429 tree member = TREE_OPERAND (fn, 1);
2430 if (BASELINK_P (member))
2432 tree object = TREE_OPERAND (fn, 0);
2433 return build_new_method_call (object, member,
2434 args, NULL_TREE,
2435 (disallow_virtual
2436 ? LOOKUP_NORMAL | LOOKUP_NONVIRTUAL
2437 : LOOKUP_NORMAL),
2438 /*fn_p=*/NULL,
2439 complain);
2443 /* Per 13.3.1.1, '(&f)(...)' is the same as '(f)(...)'. */
2444 if (TREE_CODE (fn) == ADDR_EXPR
2445 && TREE_CODE (TREE_OPERAND (fn, 0)) == OVERLOAD)
2446 fn = TREE_OPERAND (fn, 0);
2448 if (is_overloaded_fn (fn))
2449 fn = baselink_for_fns (fn);
2451 result = NULL_TREE;
2452 if (BASELINK_P (fn))
2454 tree object;
2456 /* A call to a member function. From [over.call.func]:
2458 If the keyword this is in scope and refers to the class of
2459 that member function, or a derived class thereof, then the
2460 function call is transformed into a qualified function call
2461 using (*this) as the postfix-expression to the left of the
2462 . operator.... [Otherwise] a contrived object of type T
2463 becomes the implied object argument.
2465 In this situation:
2467 struct A { void f(); };
2468 struct B : public A {};
2469 struct C : public A { void g() { B::f(); }};
2471 "the class of that member function" refers to `A'. But 11.2
2472 [class.access.base] says that we need to convert 'this' to B* as
2473 part of the access, so we pass 'B' to maybe_dummy_object. */
2475 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (get_first_fn (fn)))
2477 /* A constructor call always uses a dummy object. (This constructor
2478 call which has the form A::A () is actually invalid and we are
2479 going to reject it later in build_new_method_call.) */
2480 object = build_dummy_object (BINFO_TYPE (BASELINK_ACCESS_BINFO (fn)));
2482 else
2483 object = maybe_dummy_object (BINFO_TYPE (BASELINK_ACCESS_BINFO (fn)),
2484 NULL);
2486 result = build_new_method_call (object, fn, args, NULL_TREE,
2487 (disallow_virtual
2488 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
2489 : LOOKUP_NORMAL),
2490 /*fn_p=*/NULL,
2491 complain);
2493 else if (is_overloaded_fn (fn))
2495 /* If the function is an overloaded builtin, resolve it. */
2496 if (TREE_CODE (fn) == FUNCTION_DECL
2497 && (DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL
2498 || DECL_BUILT_IN_CLASS (fn) == BUILT_IN_MD))
2499 result = resolve_overloaded_builtin (input_location, fn, *args);
2501 if (!result)
2503 if (warn_sizeof_pointer_memaccess
2504 && (complain & tf_warning)
2505 && !vec_safe_is_empty (*args)
2506 && !processing_template_decl)
2508 location_t sizeof_arg_loc[3];
2509 tree sizeof_arg[3];
2510 unsigned int i;
2511 for (i = 0; i < 3; i++)
2513 tree t;
2515 sizeof_arg_loc[i] = UNKNOWN_LOCATION;
2516 sizeof_arg[i] = NULL_TREE;
2517 if (i >= (*args)->length ())
2518 continue;
2519 t = (**args)[i];
2520 if (TREE_CODE (t) != SIZEOF_EXPR)
2521 continue;
2522 if (SIZEOF_EXPR_TYPE_P (t))
2523 sizeof_arg[i] = TREE_TYPE (TREE_OPERAND (t, 0));
2524 else
2525 sizeof_arg[i] = TREE_OPERAND (t, 0);
2526 sizeof_arg_loc[i] = EXPR_LOCATION (t);
2528 sizeof_pointer_memaccess_warning
2529 (sizeof_arg_loc, fn, *args,
2530 sizeof_arg, same_type_ignoring_top_level_qualifiers_p);
2533 /* A call to a namespace-scope function. */
2534 result = build_new_function_call (fn, args, complain);
2537 else if (TREE_CODE (fn) == PSEUDO_DTOR_EXPR)
2539 if (!vec_safe_is_empty (*args))
2540 error ("arguments to destructor are not allowed");
2541 /* Mark the pseudo-destructor call as having side-effects so
2542 that we do not issue warnings about its use. */
2543 result = build1 (NOP_EXPR,
2544 void_type_node,
2545 TREE_OPERAND (fn, 0));
2546 TREE_SIDE_EFFECTS (result) = 1;
2548 else if (CLASS_TYPE_P (TREE_TYPE (fn)))
2549 /* If the "function" is really an object of class type, it might
2550 have an overloaded `operator ()'. */
2551 result = build_op_call (fn, args, complain);
2553 if (!result)
2554 /* A call where the function is unknown. */
2555 result = cp_build_function_call_vec (fn, args, complain);
2557 if (processing_template_decl && result != error_mark_node)
2559 if (INDIRECT_REF_P (result))
2560 result = TREE_OPERAND (result, 0);
2561 result = build_call_vec (TREE_TYPE (result), orig_fn, orig_args);
2562 SET_EXPR_LOCATION (result, input_location);
2563 KOENIG_LOOKUP_P (result) = koenig_p;
2564 release_tree_vector (orig_args);
2565 result = convert_from_reference (result);
2568 /* Free or retain OVERLOADs from lookup. */
2569 if (is_overloaded_fn (orig_fn))
2570 lookup_keep (get_fns (orig_fn), processing_template_decl);
2572 return result;
2575 /* Finish a call to a postfix increment or decrement or EXPR. (Which
2576 is indicated by CODE, which should be POSTINCREMENT_EXPR or
2577 POSTDECREMENT_EXPR.) */
2579 cp_expr
2580 finish_increment_expr (cp_expr expr, enum tree_code code)
2582 /* input_location holds the location of the trailing operator token.
2583 Build a location of the form:
2584 expr++
2585 ~~~~^~
2586 with the caret at the operator token, ranging from the start
2587 of EXPR to the end of the operator token. */
2588 location_t combined_loc = make_location (input_location,
2589 expr.get_start (),
2590 get_finish (input_location));
2591 cp_expr result = build_x_unary_op (combined_loc, code, expr,
2592 tf_warning_or_error);
2593 /* TODO: build_x_unary_op doesn't honor the location, so set it here. */
2594 result.set_location (combined_loc);
2595 return result;
2598 /* Finish a use of `this'. Returns an expression for `this'. */
2600 tree
2601 finish_this_expr (void)
2603 tree result = NULL_TREE;
2605 if (current_class_ptr)
2607 tree type = TREE_TYPE (current_class_ref);
2609 /* In a lambda expression, 'this' refers to the captured 'this'. */
2610 if (LAMBDA_TYPE_P (type))
2611 result = lambda_expr_this_capture (CLASSTYPE_LAMBDA_EXPR (type), true);
2612 else
2613 result = current_class_ptr;
2616 if (result)
2617 /* The keyword 'this' is a prvalue expression. */
2618 return rvalue (result);
2620 tree fn = current_nonlambda_function ();
2621 if (fn && DECL_STATIC_FUNCTION_P (fn))
2622 error ("%<this%> is unavailable for static member functions");
2623 else if (fn)
2624 error ("invalid use of %<this%> in non-member function");
2625 else
2626 error ("invalid use of %<this%> at top level");
2627 return error_mark_node;
2630 /* Finish a pseudo-destructor expression. If SCOPE is NULL, the
2631 expression was of the form `OBJECT.~DESTRUCTOR' where DESTRUCTOR is
2632 the TYPE for the type given. If SCOPE is non-NULL, the expression
2633 was of the form `OBJECT.SCOPE::~DESTRUCTOR'. */
2635 tree
2636 finish_pseudo_destructor_expr (tree object, tree scope, tree destructor,
2637 location_t loc)
2639 if (object == error_mark_node || destructor == error_mark_node)
2640 return error_mark_node;
2642 gcc_assert (TYPE_P (destructor));
2644 if (!processing_template_decl)
2646 if (scope == error_mark_node)
2648 error_at (loc, "invalid qualifying scope in pseudo-destructor name");
2649 return error_mark_node;
2651 if (is_auto (destructor))
2652 destructor = TREE_TYPE (object);
2653 if (scope && TYPE_P (scope) && !check_dtor_name (scope, destructor))
2655 error_at (loc,
2656 "qualified type %qT does not match destructor name ~%qT",
2657 scope, destructor);
2658 return error_mark_node;
2662 /* [expr.pseudo] says both:
2664 The type designated by the pseudo-destructor-name shall be
2665 the same as the object type.
2667 and:
2669 The cv-unqualified versions of the object type and of the
2670 type designated by the pseudo-destructor-name shall be the
2671 same type.
2673 We implement the more generous second sentence, since that is
2674 what most other compilers do. */
2675 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (object),
2676 destructor))
2678 error_at (loc, "%qE is not of type %qT", object, destructor);
2679 return error_mark_node;
2683 return build3_loc (loc, PSEUDO_DTOR_EXPR, void_type_node, object,
2684 scope, destructor);
2687 /* Finish an expression of the form CODE EXPR. */
2689 cp_expr
2690 finish_unary_op_expr (location_t op_loc, enum tree_code code, cp_expr expr,
2691 tsubst_flags_t complain)
2693 /* Build a location of the form:
2694 ++expr
2695 ^~~~~~
2696 with the caret at the operator token, ranging from the start
2697 of the operator token to the end of EXPR. */
2698 location_t combined_loc = make_location (op_loc,
2699 op_loc, expr.get_finish ());
2700 cp_expr result = build_x_unary_op (combined_loc, code, expr, complain);
2701 /* TODO: build_x_unary_op doesn't always honor the location. */
2702 result.set_location (combined_loc);
2704 tree result_ovl, expr_ovl;
2706 if (!(complain & tf_warning))
2707 return result;
2709 result_ovl = result;
2710 expr_ovl = expr;
2712 if (!processing_template_decl)
2713 expr_ovl = cp_fully_fold (expr_ovl);
2715 if (!CONSTANT_CLASS_P (expr_ovl)
2716 || TREE_OVERFLOW_P (expr_ovl))
2717 return result;
2719 if (!processing_template_decl)
2720 result_ovl = cp_fully_fold (result_ovl);
2722 if (CONSTANT_CLASS_P (result_ovl) && TREE_OVERFLOW_P (result_ovl))
2723 overflow_warning (combined_loc, result_ovl);
2725 return result;
2728 /* Finish a compound-literal expression or C++11 functional cast with aggregate
2729 initializer. TYPE is the type to which the CONSTRUCTOR in COMPOUND_LITERAL
2730 is being cast. */
2732 tree
2733 finish_compound_literal (tree type, tree compound_literal,
2734 tsubst_flags_t complain,
2735 fcl_t fcl_context)
2737 if (type == error_mark_node)
2738 return error_mark_node;
2740 if (TREE_CODE (type) == REFERENCE_TYPE)
2742 compound_literal
2743 = finish_compound_literal (TREE_TYPE (type), compound_literal,
2744 complain, fcl_context);
2745 return cp_build_c_cast (type, compound_literal, complain);
2748 if (!TYPE_OBJ_P (type))
2750 if (complain & tf_error)
2751 error ("compound literal of non-object type %qT", type);
2752 return error_mark_node;
2755 if (tree anode = type_uses_auto (type))
2756 if (CLASS_PLACEHOLDER_TEMPLATE (anode))
2758 type = do_auto_deduction (type, compound_literal, anode, complain,
2759 adc_variable_type);
2760 if (type == error_mark_node)
2761 return error_mark_node;
2764 if (processing_template_decl)
2766 TREE_TYPE (compound_literal) = type;
2767 /* Mark the expression as a compound literal. */
2768 TREE_HAS_CONSTRUCTOR (compound_literal) = 1;
2769 if (fcl_context == fcl_c99)
2770 CONSTRUCTOR_C99_COMPOUND_LITERAL (compound_literal) = 1;
2771 return compound_literal;
2774 type = complete_type (type);
2776 if (TYPE_NON_AGGREGATE_CLASS (type))
2778 /* Trying to deal with a CONSTRUCTOR instead of a TREE_LIST
2779 everywhere that deals with function arguments would be a pain, so
2780 just wrap it in a TREE_LIST. The parser set a flag so we know
2781 that it came from T{} rather than T({}). */
2782 CONSTRUCTOR_IS_DIRECT_INIT (compound_literal) = 1;
2783 compound_literal = build_tree_list (NULL_TREE, compound_literal);
2784 return build_functional_cast (type, compound_literal, complain);
2787 if (TREE_CODE (type) == ARRAY_TYPE
2788 && check_array_initializer (NULL_TREE, type, compound_literal))
2789 return error_mark_node;
2790 compound_literal = reshape_init (type, compound_literal, complain);
2791 if (SCALAR_TYPE_P (type)
2792 && !BRACE_ENCLOSED_INITIALIZER_P (compound_literal)
2793 && !check_narrowing (type, compound_literal, complain))
2794 return error_mark_node;
2795 if (TREE_CODE (type) == ARRAY_TYPE
2796 && TYPE_DOMAIN (type) == NULL_TREE)
2798 cp_complete_array_type_or_error (&type, compound_literal,
2799 false, complain);
2800 if (type == error_mark_node)
2801 return error_mark_node;
2803 compound_literal = digest_init_flags (type, compound_literal, LOOKUP_NORMAL,
2804 complain);
2805 if (TREE_CODE (compound_literal) == CONSTRUCTOR)
2807 TREE_HAS_CONSTRUCTOR (compound_literal) = true;
2808 if (fcl_context == fcl_c99)
2809 CONSTRUCTOR_C99_COMPOUND_LITERAL (compound_literal) = 1;
2812 /* Put static/constant array temporaries in static variables. */
2813 /* FIXME all C99 compound literals should be variables rather than C++
2814 temporaries, unless they are used as an aggregate initializer. */
2815 if ((!at_function_scope_p () || CP_TYPE_CONST_P (type))
2816 && fcl_context == fcl_c99
2817 && TREE_CODE (type) == ARRAY_TYPE
2818 && !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
2819 && initializer_constant_valid_p (compound_literal, type))
2821 tree decl = create_temporary_var (type);
2822 DECL_INITIAL (decl) = compound_literal;
2823 TREE_STATIC (decl) = 1;
2824 if (literal_type_p (type) && CP_TYPE_CONST_NON_VOLATILE_P (type))
2826 /* 5.19 says that a constant expression can include an
2827 lvalue-rvalue conversion applied to "a glvalue of literal type
2828 that refers to a non-volatile temporary object initialized
2829 with a constant expression". Rather than try to communicate
2830 that this VAR_DECL is a temporary, just mark it constexpr. */
2831 DECL_DECLARED_CONSTEXPR_P (decl) = true;
2832 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
2833 TREE_CONSTANT (decl) = true;
2835 cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
2836 decl = pushdecl_top_level (decl);
2837 DECL_NAME (decl) = make_anon_name ();
2838 SET_DECL_ASSEMBLER_NAME (decl, DECL_NAME (decl));
2839 /* Make sure the destructor is callable. */
2840 tree clean = cxx_maybe_build_cleanup (decl, complain);
2841 if (clean == error_mark_node)
2842 return error_mark_node;
2843 return decl;
2846 /* Represent other compound literals with TARGET_EXPR so we produce
2847 an lvalue, but can elide copies. */
2848 if (!VECTOR_TYPE_P (type))
2849 compound_literal = get_target_expr_sfinae (compound_literal, complain);
2851 return compound_literal;
2854 /* Return the declaration for the function-name variable indicated by
2855 ID. */
2857 tree
2858 finish_fname (tree id)
2860 tree decl;
2862 decl = fname_decl (input_location, C_RID_CODE (id), id);
2863 if (processing_template_decl && current_function_decl
2864 && decl != error_mark_node)
2865 decl = DECL_NAME (decl);
2866 return decl;
2869 /* Finish a translation unit. */
2871 void
2872 finish_translation_unit (void)
2874 /* In case there were missing closebraces,
2875 get us back to the global binding level. */
2876 pop_everything ();
2877 while (current_namespace != global_namespace)
2878 pop_namespace ();
2880 /* Do file scope __FUNCTION__ et al. */
2881 finish_fname_decls ();
2884 /* Finish a template type parameter, specified as AGGR IDENTIFIER.
2885 Returns the parameter. */
2887 tree
2888 finish_template_type_parm (tree aggr, tree identifier)
2890 if (aggr != class_type_node)
2892 permerror (input_location, "template type parameters must use the keyword %<class%> or %<typename%>");
2893 aggr = class_type_node;
2896 return build_tree_list (aggr, identifier);
2899 /* Finish a template template parameter, specified as AGGR IDENTIFIER.
2900 Returns the parameter. */
2902 tree
2903 finish_template_template_parm (tree aggr, tree identifier)
2905 tree decl = build_decl (input_location,
2906 TYPE_DECL, identifier, NULL_TREE);
2908 tree tmpl = build_lang_decl (TEMPLATE_DECL, identifier, NULL_TREE);
2909 DECL_TEMPLATE_PARMS (tmpl) = current_template_parms;
2910 DECL_TEMPLATE_RESULT (tmpl) = decl;
2911 DECL_ARTIFICIAL (decl) = 1;
2913 // Associate the constraints with the underlying declaration,
2914 // not the template.
2915 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
2916 tree constr = build_constraints (reqs, NULL_TREE);
2917 set_constraints (decl, constr);
2919 end_template_decl ();
2921 gcc_assert (DECL_TEMPLATE_PARMS (tmpl));
2923 check_default_tmpl_args (decl, DECL_TEMPLATE_PARMS (tmpl),
2924 /*is_primary=*/true, /*is_partial=*/false,
2925 /*is_friend=*/0);
2927 return finish_template_type_parm (aggr, tmpl);
2930 /* ARGUMENT is the default-argument value for a template template
2931 parameter. If ARGUMENT is invalid, issue error messages and return
2932 the ERROR_MARK_NODE. Otherwise, ARGUMENT itself is returned. */
2934 tree
2935 check_template_template_default_arg (tree argument)
2937 if (TREE_CODE (argument) != TEMPLATE_DECL
2938 && TREE_CODE (argument) != TEMPLATE_TEMPLATE_PARM
2939 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
2941 if (TREE_CODE (argument) == TYPE_DECL)
2942 error ("invalid use of type %qT as a default value for a template "
2943 "template-parameter", TREE_TYPE (argument));
2944 else
2945 error ("invalid default argument for a template template parameter");
2946 return error_mark_node;
2949 return argument;
2952 /* Begin a class definition, as indicated by T. */
2954 tree
2955 begin_class_definition (tree t)
2957 if (error_operand_p (t) || error_operand_p (TYPE_MAIN_DECL (t)))
2958 return error_mark_node;
2960 if (processing_template_parmlist)
2962 error ("definition of %q#T inside template parameter list", t);
2963 return error_mark_node;
2966 /* According to the C++ ABI, decimal classes defined in ISO/IEC TR 24733
2967 are passed the same as decimal scalar types. */
2968 if (TREE_CODE (t) == RECORD_TYPE
2969 && !processing_template_decl)
2971 tree ns = TYPE_CONTEXT (t);
2972 if (ns && TREE_CODE (ns) == NAMESPACE_DECL
2973 && DECL_CONTEXT (ns) == std_node
2974 && DECL_NAME (ns)
2975 && id_equal (DECL_NAME (ns), "decimal"))
2977 const char *n = TYPE_NAME_STRING (t);
2978 if ((strcmp (n, "decimal32") == 0)
2979 || (strcmp (n, "decimal64") == 0)
2980 || (strcmp (n, "decimal128") == 0))
2981 TYPE_TRANSPARENT_AGGR (t) = 1;
2985 /* A non-implicit typename comes from code like:
2987 template <typename T> struct A {
2988 template <typename U> struct A<T>::B ...
2990 This is erroneous. */
2991 else if (TREE_CODE (t) == TYPENAME_TYPE)
2993 error ("invalid definition of qualified type %qT", t);
2994 t = error_mark_node;
2997 if (t == error_mark_node || ! MAYBE_CLASS_TYPE_P (t))
2999 t = make_class_type (RECORD_TYPE);
3000 pushtag (make_anon_name (), t, /*tag_scope=*/ts_current);
3003 if (TYPE_BEING_DEFINED (t))
3005 t = make_class_type (TREE_CODE (t));
3006 pushtag (TYPE_IDENTIFIER (t), t, /*tag_scope=*/ts_current);
3008 maybe_process_partial_specialization (t);
3009 pushclass (t);
3010 TYPE_BEING_DEFINED (t) = 1;
3011 class_binding_level->defining_class_p = 1;
3013 if (flag_pack_struct)
3015 tree v;
3016 TYPE_PACKED (t) = 1;
3017 /* Even though the type is being defined for the first time
3018 here, there might have been a forward declaration, so there
3019 might be cv-qualified variants of T. */
3020 for (v = TYPE_NEXT_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v))
3021 TYPE_PACKED (v) = 1;
3023 /* Reset the interface data, at the earliest possible
3024 moment, as it might have been set via a class foo;
3025 before. */
3026 if (! TYPE_UNNAMED_P (t))
3028 struct c_fileinfo *finfo = \
3029 get_fileinfo (LOCATION_FILE (input_location));
3030 CLASSTYPE_INTERFACE_ONLY (t) = finfo->interface_only;
3031 SET_CLASSTYPE_INTERFACE_UNKNOWN_X
3032 (t, finfo->interface_unknown);
3034 reset_specialization();
3036 /* Make a declaration for this class in its own scope. */
3037 build_self_reference ();
3039 return t;
3042 /* Finish the member declaration given by DECL. */
3044 void
3045 finish_member_declaration (tree decl)
3047 if (decl == error_mark_node || decl == NULL_TREE)
3048 return;
3050 if (decl == void_type_node)
3051 /* The COMPONENT was a friend, not a member, and so there's
3052 nothing for us to do. */
3053 return;
3055 /* We should see only one DECL at a time. */
3056 gcc_assert (DECL_CHAIN (decl) == NULL_TREE);
3058 /* Don't add decls after definition. */
3059 gcc_assert (TYPE_BEING_DEFINED (current_class_type)
3060 /* We can add lambda types when late parsing default
3061 arguments. */
3062 || LAMBDA_TYPE_P (TREE_TYPE (decl)));
3064 /* Set up access control for DECL. */
3065 TREE_PRIVATE (decl)
3066 = (current_access_specifier == access_private_node);
3067 TREE_PROTECTED (decl)
3068 = (current_access_specifier == access_protected_node);
3069 if (TREE_CODE (decl) == TEMPLATE_DECL)
3071 TREE_PRIVATE (DECL_TEMPLATE_RESULT (decl)) = TREE_PRIVATE (decl);
3072 TREE_PROTECTED (DECL_TEMPLATE_RESULT (decl)) = TREE_PROTECTED (decl);
3075 /* Mark the DECL as a member of the current class, unless it's
3076 a member of an enumeration. */
3077 if (TREE_CODE (decl) != CONST_DECL)
3078 DECL_CONTEXT (decl) = current_class_type;
3080 if (TREE_CODE (decl) == USING_DECL)
3081 /* For now, ignore class-scope USING_DECLS, so that debugging
3082 backends do not see them. */
3083 DECL_IGNORED_P (decl) = 1;
3085 /* Check for bare parameter packs in the non-static data member
3086 declaration. */
3087 if (TREE_CODE (decl) == FIELD_DECL)
3089 if (check_for_bare_parameter_packs (TREE_TYPE (decl)))
3090 TREE_TYPE (decl) = error_mark_node;
3091 if (check_for_bare_parameter_packs (DECL_ATTRIBUTES (decl)))
3092 DECL_ATTRIBUTES (decl) = NULL_TREE;
3095 /* [dcl.link]
3097 A C language linkage is ignored for the names of class members
3098 and the member function type of class member functions. */
3099 if (DECL_LANG_SPECIFIC (decl))
3100 SET_DECL_LANGUAGE (decl, lang_cplusplus);
3102 bool add = false;
3104 /* Functions and non-functions are added differently. */
3105 if (DECL_DECLARES_FUNCTION_P (decl))
3106 add = add_method (current_class_type, decl, false);
3107 /* Enter the DECL into the scope of the class, if the class
3108 isn't a closure (whose fields are supposed to be unnamed). */
3109 else if (CLASSTYPE_LAMBDA_EXPR (current_class_type)
3110 || pushdecl_class_level (decl))
3111 add = true;
3113 if (add)
3115 /* All TYPE_DECLs go at the end of TYPE_FIELDS. Ordinary fields
3116 go at the beginning. The reason is that
3117 legacy_nonfn_member_lookup searches the list in order, and we
3118 want a field name to override a type name so that the "struct
3119 stat hack" will work. In particular:
3121 struct S { enum E { }; static const int E = 5; int ary[S::E]; } s;
3123 is valid. */
3125 if (TREE_CODE (decl) == TYPE_DECL)
3126 TYPE_FIELDS (current_class_type)
3127 = chainon (TYPE_FIELDS (current_class_type), decl);
3128 else
3130 DECL_CHAIN (decl) = TYPE_FIELDS (current_class_type);
3131 TYPE_FIELDS (current_class_type) = decl;
3134 maybe_add_class_template_decl_list (current_class_type, decl,
3135 /*friend_p=*/0);
3139 /* Finish processing a complete template declaration. The PARMS are
3140 the template parameters. */
3142 void
3143 finish_template_decl (tree parms)
3145 if (parms)
3146 end_template_decl ();
3147 else
3148 end_specialization ();
3151 // Returns the template type of the class scope being entered. If we're
3152 // entering a constrained class scope. TYPE is the class template
3153 // scope being entered and we may need to match the intended type with
3154 // a constrained specialization. For example:
3156 // template<Object T>
3157 // struct S { void f(); }; #1
3159 // template<Object T>
3160 // void S<T>::f() { } #2
3162 // We check, in #2, that S<T> refers precisely to the type declared by
3163 // #1 (i.e., that the constraints match). Note that the following should
3164 // be an error since there is no specialization of S<T> that is
3165 // unconstrained, but this is not diagnosed here.
3167 // template<typename T>
3168 // void S<T>::f() { }
3170 // We cannot diagnose this problem here since this function also matches
3171 // qualified template names that are not part of a definition. For example:
3173 // template<Integral T, Floating_point U>
3174 // typename pair<T, U>::first_type void f(T, U);
3176 // Here, it is unlikely that there is a partial specialization of
3177 // pair constrained for for Integral and Floating_point arguments.
3179 // The general rule is: if a constrained specialization with matching
3180 // constraints is found return that type. Also note that if TYPE is not a
3181 // class-type (e.g. a typename type), then no fixup is needed.
3183 static tree
3184 fixup_template_type (tree type)
3186 // Find the template parameter list at the a depth appropriate to
3187 // the scope we're trying to enter.
3188 tree parms = current_template_parms;
3189 int depth = template_class_depth (type);
3190 for (int n = processing_template_decl; n > depth && parms; --n)
3191 parms = TREE_CHAIN (parms);
3192 if (!parms)
3193 return type;
3194 tree cur_reqs = TEMPLATE_PARMS_CONSTRAINTS (parms);
3195 tree cur_constr = build_constraints (cur_reqs, NULL_TREE);
3197 // Search for a specialization whose type and constraints match.
3198 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
3199 tree specs = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
3200 while (specs)
3202 tree spec_constr = get_constraints (TREE_VALUE (specs));
3204 // If the type and constraints match a specialization, then we
3205 // are entering that type.
3206 if (same_type_p (type, TREE_TYPE (specs))
3207 && equivalent_constraints (cur_constr, spec_constr))
3208 return TREE_TYPE (specs);
3209 specs = TREE_CHAIN (specs);
3212 // If no specialization matches, then must return the type
3213 // previously found.
3214 return type;
3217 /* Finish processing a template-id (which names a type) of the form
3218 NAME < ARGS >. Return the TYPE_DECL for the type named by the
3219 template-id. If ENTERING_SCOPE is nonzero we are about to enter
3220 the scope of template-id indicated. */
3222 tree
3223 finish_template_type (tree name, tree args, int entering_scope)
3225 tree type;
3227 type = lookup_template_class (name, args,
3228 NULL_TREE, NULL_TREE, entering_scope,
3229 tf_warning_or_error | tf_user);
3231 /* If we might be entering the scope of a partial specialization,
3232 find the one with the right constraints. */
3233 if (flag_concepts
3234 && entering_scope
3235 && CLASS_TYPE_P (type)
3236 && CLASSTYPE_TEMPLATE_INFO (type)
3237 && dependent_type_p (type)
3238 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
3239 type = fixup_template_type (type);
3241 if (type == error_mark_node)
3242 return type;
3243 else if (CLASS_TYPE_P (type) && !alias_type_or_template_p (type))
3244 return TYPE_STUB_DECL (type);
3245 else
3246 return TYPE_NAME (type);
3249 /* Finish processing a BASE_CLASS with the indicated ACCESS_SPECIFIER.
3250 Return a TREE_LIST containing the ACCESS_SPECIFIER and the
3251 BASE_CLASS, or NULL_TREE if an error occurred. The
3252 ACCESS_SPECIFIER is one of
3253 access_{default,public,protected_private}_node. For a virtual base
3254 we set TREE_TYPE. */
3256 tree
3257 finish_base_specifier (tree base, tree access, bool virtual_p)
3259 tree result;
3261 if (base == error_mark_node)
3263 error ("invalid base-class specification");
3264 result = NULL_TREE;
3266 else if (! MAYBE_CLASS_TYPE_P (base))
3268 error ("%qT is not a class type", base);
3269 result = NULL_TREE;
3271 else
3273 if (cp_type_quals (base) != 0)
3275 /* DR 484: Can a base-specifier name a cv-qualified
3276 class type? */
3277 base = TYPE_MAIN_VARIANT (base);
3279 result = build_tree_list (access, base);
3280 if (virtual_p)
3281 TREE_TYPE (result) = integer_type_node;
3284 return result;
3287 /* If FNS is a member function, a set of member functions, or a
3288 template-id referring to one or more member functions, return a
3289 BASELINK for FNS, incorporating the current access context.
3290 Otherwise, return FNS unchanged. */
3292 tree
3293 baselink_for_fns (tree fns)
3295 tree scope;
3296 tree cl;
3298 if (BASELINK_P (fns)
3299 || error_operand_p (fns))
3300 return fns;
3302 scope = ovl_scope (fns);
3303 if (!CLASS_TYPE_P (scope))
3304 return fns;
3306 cl = currently_open_derived_class (scope);
3307 if (!cl)
3308 cl = scope;
3309 cl = TYPE_BINFO (cl);
3310 return build_baselink (cl, cl, fns, /*optype=*/NULL_TREE);
3313 /* Returns true iff DECL is a variable from a function outside
3314 the current one. */
3316 static bool
3317 outer_var_p (tree decl)
3319 return ((VAR_P (decl) || TREE_CODE (decl) == PARM_DECL)
3320 && DECL_FUNCTION_SCOPE_P (decl)
3321 /* Don't get confused by temporaries. */
3322 && DECL_NAME (decl)
3323 && (DECL_CONTEXT (decl) != current_function_decl
3324 || parsing_nsdmi ()));
3327 /* As above, but also checks that DECL is automatic. */
3329 bool
3330 outer_automatic_var_p (tree decl)
3332 return (outer_var_p (decl)
3333 && !TREE_STATIC (decl));
3336 /* DECL satisfies outer_automatic_var_p. Possibly complain about it or
3337 rewrite it for lambda capture.
3339 If ODR_USE is true, we're being called from mark_use, and we complain about
3340 use of constant variables. If ODR_USE is false, we're being called for the
3341 id-expression, and we do lambda capture. */
3343 tree
3344 process_outer_var_ref (tree decl, tsubst_flags_t complain, bool odr_use)
3346 if (cp_unevaluated_operand)
3347 /* It's not a use (3.2) if we're in an unevaluated context. */
3348 return decl;
3349 if (decl == error_mark_node)
3350 return decl;
3352 tree context = DECL_CONTEXT (decl);
3353 tree containing_function = current_function_decl;
3354 tree lambda_stack = NULL_TREE;
3355 tree lambda_expr = NULL_TREE;
3356 tree initializer = convert_from_reference (decl);
3358 /* Mark it as used now even if the use is ill-formed. */
3359 if (!mark_used (decl, complain))
3360 return error_mark_node;
3362 if (parsing_nsdmi ())
3363 containing_function = NULL_TREE;
3365 if (containing_function && LAMBDA_FUNCTION_P (containing_function))
3367 /* Check whether we've already built a proxy. */
3368 tree var = decl;
3369 while (is_normal_capture_proxy (var))
3370 var = DECL_CAPTURED_VARIABLE (var);
3371 tree d = retrieve_local_specialization (var);
3373 if (d && d != decl && is_capture_proxy (d))
3375 if (DECL_CONTEXT (d) == containing_function)
3376 /* We already have an inner proxy. */
3377 return d;
3378 else
3379 /* We need to capture an outer proxy. */
3380 return process_outer_var_ref (d, complain, odr_use);
3384 /* If we are in a lambda function, we can move out until we hit
3385 1. the context,
3386 2. a non-lambda function, or
3387 3. a non-default capturing lambda function. */
3388 while (context != containing_function
3389 /* containing_function can be null with invalid generic lambdas. */
3390 && containing_function
3391 && LAMBDA_FUNCTION_P (containing_function))
3393 tree closure = DECL_CONTEXT (containing_function);
3394 lambda_expr = CLASSTYPE_LAMBDA_EXPR (closure);
3396 if (TYPE_CLASS_SCOPE_P (closure))
3397 /* A lambda in an NSDMI (c++/64496). */
3398 break;
3400 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr)
3401 == CPLD_NONE)
3402 break;
3404 lambda_stack = tree_cons (NULL_TREE,
3405 lambda_expr,
3406 lambda_stack);
3408 containing_function
3409 = decl_function_context (containing_function);
3412 /* In a lambda within a template, wait until instantiation
3413 time to implicitly capture. */
3414 if (context == containing_function
3415 && DECL_TEMPLATE_INFO (containing_function)
3416 && uses_template_parms (DECL_TI_ARGS (containing_function)))
3417 return decl;
3419 if (lambda_expr && VAR_P (decl)
3420 && DECL_ANON_UNION_VAR_P (decl))
3422 if (complain & tf_error)
3423 error ("cannot capture member %qD of anonymous union", decl);
3424 return error_mark_node;
3426 /* Do lambda capture when processing the id-expression, not when
3427 odr-using a variable. */
3428 if (!odr_use && context == containing_function)
3430 decl = add_default_capture (lambda_stack,
3431 /*id=*/DECL_NAME (decl),
3432 initializer);
3434 /* Only an odr-use of an outer automatic variable causes an
3435 error, and a constant variable can decay to a prvalue
3436 constant without odr-use. So don't complain yet. */
3437 else if (!odr_use && decl_constant_var_p (decl))
3438 return decl;
3439 else if (lambda_expr)
3441 if (complain & tf_error)
3443 error ("%qD is not captured", decl);
3444 tree closure = LAMBDA_EXPR_CLOSURE (lambda_expr);
3445 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr)
3446 == CPLD_NONE)
3447 inform (location_of (closure),
3448 "the lambda has no capture-default");
3449 else if (TYPE_CLASS_SCOPE_P (closure))
3450 inform (UNKNOWN_LOCATION, "lambda in local class %q+T cannot "
3451 "capture variables from the enclosing context",
3452 TYPE_CONTEXT (closure));
3453 inform (DECL_SOURCE_LOCATION (decl), "%q#D declared here", decl);
3455 return error_mark_node;
3457 else
3459 if (complain & tf_error)
3461 error (VAR_P (decl)
3462 ? G_("use of local variable with automatic storage from "
3463 "containing function")
3464 : G_("use of parameter from containing function"));
3465 inform (DECL_SOURCE_LOCATION (decl), "%q#D declared here", decl);
3467 return error_mark_node;
3469 return decl;
3472 /* ID_EXPRESSION is a representation of parsed, but unprocessed,
3473 id-expression. (See cp_parser_id_expression for details.) SCOPE,
3474 if non-NULL, is the type or namespace used to explicitly qualify
3475 ID_EXPRESSION. DECL is the entity to which that name has been
3476 resolved.
3478 *CONSTANT_EXPRESSION_P is true if we are presently parsing a
3479 constant-expression. In that case, *NON_CONSTANT_EXPRESSION_P will
3480 be set to true if this expression isn't permitted in a
3481 constant-expression, but it is otherwise not set by this function.
3482 *ALLOW_NON_CONSTANT_EXPRESSION_P is true if we are parsing a
3483 constant-expression, but a non-constant expression is also
3484 permissible.
3486 DONE is true if this expression is a complete postfix-expression;
3487 it is false if this expression is followed by '->', '[', '(', etc.
3488 ADDRESS_P is true iff this expression is the operand of '&'.
3489 TEMPLATE_P is true iff the qualified-id was of the form
3490 "A::template B". TEMPLATE_ARG_P is true iff this qualified name
3491 appears as a template argument.
3493 If an error occurs, and it is the kind of error that might cause
3494 the parser to abort a tentative parse, *ERROR_MSG is filled in. It
3495 is the caller's responsibility to issue the message. *ERROR_MSG
3496 will be a string with static storage duration, so the caller need
3497 not "free" it.
3499 Return an expression for the entity, after issuing appropriate
3500 diagnostics. This function is also responsible for transforming a
3501 reference to a non-static member into a COMPONENT_REF that makes
3502 the use of "this" explicit.
3504 Upon return, *IDK will be filled in appropriately. */
3505 cp_expr
3506 finish_id_expression (tree id_expression,
3507 tree decl,
3508 tree scope,
3509 cp_id_kind *idk,
3510 bool integral_constant_expression_p,
3511 bool allow_non_integral_constant_expression_p,
3512 bool *non_integral_constant_expression_p,
3513 bool template_p,
3514 bool done,
3515 bool address_p,
3516 bool template_arg_p,
3517 const char **error_msg,
3518 location_t location)
3520 decl = strip_using_decl (decl);
3522 /* Initialize the output parameters. */
3523 *idk = CP_ID_KIND_NONE;
3524 *error_msg = NULL;
3526 if (id_expression == error_mark_node)
3527 return error_mark_node;
3528 /* If we have a template-id, then no further lookup is
3529 required. If the template-id was for a template-class, we
3530 will sometimes have a TYPE_DECL at this point. */
3531 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3532 || TREE_CODE (decl) == TYPE_DECL)
3534 /* Look up the name. */
3535 else
3537 if (decl == error_mark_node)
3539 /* Name lookup failed. */
3540 if (scope
3541 && (!TYPE_P (scope)
3542 || (!dependent_type_p (scope)
3543 && !(identifier_p (id_expression)
3544 && IDENTIFIER_CONV_OP_P (id_expression)
3545 && dependent_type_p (TREE_TYPE (id_expression))))))
3547 /* If the qualifying type is non-dependent (and the name
3548 does not name a conversion operator to a dependent
3549 type), issue an error. */
3550 qualified_name_lookup_error (scope, id_expression, decl, location);
3551 return error_mark_node;
3553 else if (!scope)
3555 /* It may be resolved via Koenig lookup. */
3556 *idk = CP_ID_KIND_UNQUALIFIED;
3557 return id_expression;
3559 else
3560 decl = id_expression;
3562 /* If DECL is a variable that would be out of scope under
3563 ANSI/ISO rules, but in scope in the ARM, name lookup
3564 will succeed. Issue a diagnostic here. */
3565 else
3566 decl = check_for_out_of_scope_variable (decl);
3568 /* Remember that the name was used in the definition of
3569 the current class so that we can check later to see if
3570 the meaning would have been different after the class
3571 was entirely defined. */
3572 if (!scope && decl != error_mark_node && identifier_p (id_expression))
3573 maybe_note_name_used_in_class (id_expression, decl);
3575 /* A use in unevaluated operand might not be instantiated appropriately
3576 if tsubst_copy builds a dummy parm, or if we never instantiate a
3577 generic lambda, so mark it now. */
3578 if (processing_template_decl && cp_unevaluated_operand)
3579 mark_type_use (decl);
3581 /* Disallow uses of local variables from containing functions, except
3582 within lambda-expressions. */
3583 if (outer_automatic_var_p (decl))
3585 decl = process_outer_var_ref (decl, tf_warning_or_error);
3586 if (decl == error_mark_node)
3587 return error_mark_node;
3590 /* Also disallow uses of function parameters outside the function
3591 body, except inside an unevaluated context (i.e. decltype). */
3592 if (TREE_CODE (decl) == PARM_DECL
3593 && DECL_CONTEXT (decl) == NULL_TREE
3594 && !cp_unevaluated_operand)
3596 *error_msg = G_("use of parameter outside function body");
3597 return error_mark_node;
3601 /* If we didn't find anything, or what we found was a type,
3602 then this wasn't really an id-expression. */
3603 if (TREE_CODE (decl) == TEMPLATE_DECL
3604 && !DECL_FUNCTION_TEMPLATE_P (decl))
3606 *error_msg = G_("missing template arguments");
3607 return error_mark_node;
3609 else if (TREE_CODE (decl) == TYPE_DECL
3610 || TREE_CODE (decl) == NAMESPACE_DECL)
3612 *error_msg = G_("expected primary-expression");
3613 return error_mark_node;
3616 /* If the name resolved to a template parameter, there is no
3617 need to look it up again later. */
3618 if ((TREE_CODE (decl) == CONST_DECL && DECL_TEMPLATE_PARM_P (decl))
3619 || TREE_CODE (decl) == TEMPLATE_PARM_INDEX)
3621 tree r;
3623 *idk = CP_ID_KIND_NONE;
3624 if (TREE_CODE (decl) == TEMPLATE_PARM_INDEX)
3625 decl = TEMPLATE_PARM_DECL (decl);
3626 r = convert_from_reference (DECL_INITIAL (decl));
3628 if (integral_constant_expression_p
3629 && !dependent_type_p (TREE_TYPE (decl))
3630 && !(INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (r))))
3632 if (!allow_non_integral_constant_expression_p)
3633 error ("template parameter %qD of type %qT is not allowed in "
3634 "an integral constant expression because it is not of "
3635 "integral or enumeration type", decl, TREE_TYPE (decl));
3636 *non_integral_constant_expression_p = true;
3638 return r;
3640 else
3642 bool dependent_p = type_dependent_expression_p (decl);
3644 /* If the declaration was explicitly qualified indicate
3645 that. The semantics of `A::f(3)' are different than
3646 `f(3)' if `f' is virtual. */
3647 *idk = (scope
3648 ? CP_ID_KIND_QUALIFIED
3649 : (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3650 ? CP_ID_KIND_TEMPLATE_ID
3651 : (dependent_p
3652 ? CP_ID_KIND_UNQUALIFIED_DEPENDENT
3653 : CP_ID_KIND_UNQUALIFIED)));
3655 if (dependent_p
3656 && DECL_P (decl)
3657 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (decl)))
3658 /* Dependent type attributes on the decl mean that the TREE_TYPE is
3659 wrong, so just return the identifier. */
3660 return id_expression;
3662 if (TREE_CODE (decl) == NAMESPACE_DECL)
3664 error ("use of namespace %qD as expression", decl);
3665 return error_mark_node;
3667 else if (DECL_CLASS_TEMPLATE_P (decl))
3669 error ("use of class template %qT as expression", decl);
3670 return error_mark_node;
3672 else if (TREE_CODE (decl) == TREE_LIST)
3674 /* Ambiguous reference to base members. */
3675 error ("request for member %qD is ambiguous in "
3676 "multiple inheritance lattice", id_expression);
3677 print_candidates (decl);
3678 return error_mark_node;
3681 /* Mark variable-like entities as used. Functions are similarly
3682 marked either below or after overload resolution. */
3683 if ((VAR_P (decl)
3684 || TREE_CODE (decl) == PARM_DECL
3685 || TREE_CODE (decl) == CONST_DECL
3686 || TREE_CODE (decl) == RESULT_DECL)
3687 && !mark_used (decl))
3688 return error_mark_node;
3690 /* Only certain kinds of names are allowed in constant
3691 expression. Template parameters have already
3692 been handled above. */
3693 if (! error_operand_p (decl)
3694 && !dependent_p
3695 && integral_constant_expression_p
3696 && ! decl_constant_var_p (decl)
3697 && TREE_CODE (decl) != CONST_DECL
3698 && ! builtin_valid_in_constant_expr_p (decl))
3700 if (!allow_non_integral_constant_expression_p)
3702 error ("%qD cannot appear in a constant-expression", decl);
3703 return error_mark_node;
3705 *non_integral_constant_expression_p = true;
3708 tree wrap;
3709 if (VAR_P (decl)
3710 && !cp_unevaluated_operand
3711 && !processing_template_decl
3712 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl))
3713 && CP_DECL_THREAD_LOCAL_P (decl)
3714 && (wrap = get_tls_wrapper_fn (decl)))
3716 /* Replace an evaluated use of the thread_local variable with
3717 a call to its wrapper. */
3718 decl = build_cxx_call (wrap, 0, NULL, tf_warning_or_error);
3720 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3721 && !dependent_p
3722 && variable_template_p (TREE_OPERAND (decl, 0)))
3724 decl = finish_template_variable (decl);
3725 mark_used (decl);
3726 decl = convert_from_reference (decl);
3728 else if (scope)
3730 if (TREE_CODE (decl) == SCOPE_REF)
3732 gcc_assert (same_type_p (scope, TREE_OPERAND (decl, 0)));
3733 decl = TREE_OPERAND (decl, 1);
3736 decl = (adjust_result_of_qualified_name_lookup
3737 (decl, scope, current_nonlambda_class_type()));
3739 if (TREE_CODE (decl) == FUNCTION_DECL)
3740 mark_used (decl);
3742 if (TYPE_P (scope))
3743 decl = finish_qualified_id_expr (scope,
3744 decl,
3745 done,
3746 address_p,
3747 template_p,
3748 template_arg_p,
3749 tf_warning_or_error);
3750 else
3751 decl = convert_from_reference (decl);
3753 else if (TREE_CODE (decl) == FIELD_DECL)
3755 /* Since SCOPE is NULL here, this is an unqualified name.
3756 Access checking has been performed during name lookup
3757 already. Turn off checking to avoid duplicate errors. */
3758 push_deferring_access_checks (dk_no_check);
3759 decl = finish_non_static_data_member (decl, NULL_TREE,
3760 /*qualifying_scope=*/NULL_TREE);
3761 pop_deferring_access_checks ();
3763 else if (is_overloaded_fn (decl))
3765 tree first_fn = get_first_fn (decl);
3767 if (TREE_CODE (first_fn) == TEMPLATE_DECL)
3768 first_fn = DECL_TEMPLATE_RESULT (first_fn);
3770 /* [basic.def.odr]: "A function whose name appears as a
3771 potentially-evaluated expression is odr-used if it is the unique
3772 lookup result".
3774 But only mark it if it's a complete postfix-expression; in a call,
3775 ADL might select a different function, and we'll call mark_used in
3776 build_over_call. */
3777 if (done
3778 && !really_overloaded_fn (decl)
3779 && !mark_used (first_fn))
3780 return error_mark_node;
3782 if (!template_arg_p
3783 && TREE_CODE (first_fn) == FUNCTION_DECL
3784 && DECL_FUNCTION_MEMBER_P (first_fn)
3785 && !shared_member_p (decl))
3787 /* A set of member functions. */
3788 decl = maybe_dummy_object (DECL_CONTEXT (first_fn), 0);
3789 return finish_class_member_access_expr (decl, id_expression,
3790 /*template_p=*/false,
3791 tf_warning_or_error);
3794 decl = baselink_for_fns (decl);
3796 else
3798 if (DECL_P (decl) && DECL_NONLOCAL (decl)
3799 && DECL_CLASS_SCOPE_P (decl))
3801 tree context = context_for_name_lookup (decl);
3802 if (context != current_class_type)
3804 tree path = currently_open_derived_class (context);
3805 perform_or_defer_access_check (TYPE_BINFO (path),
3806 decl, decl,
3807 tf_warning_or_error);
3811 decl = convert_from_reference (decl);
3815 return cp_expr (decl, location);
3818 /* Implement the __typeof keyword: Return the type of EXPR, suitable for
3819 use as a type-specifier. */
3821 tree
3822 finish_typeof (tree expr)
3824 tree type;
3826 if (type_dependent_expression_p (expr))
3828 type = cxx_make_type (TYPEOF_TYPE);
3829 TYPEOF_TYPE_EXPR (type) = expr;
3830 SET_TYPE_STRUCTURAL_EQUALITY (type);
3832 return type;
3835 expr = mark_type_use (expr);
3837 type = unlowered_expr_type (expr);
3839 if (!type || type == unknown_type_node)
3841 error ("type of %qE is unknown", expr);
3842 return error_mark_node;
3845 return type;
3848 /* Implement the __underlying_type keyword: Return the underlying
3849 type of TYPE, suitable for use as a type-specifier. */
3851 tree
3852 finish_underlying_type (tree type)
3854 tree underlying_type;
3856 if (processing_template_decl)
3858 underlying_type = cxx_make_type (UNDERLYING_TYPE);
3859 UNDERLYING_TYPE_TYPE (underlying_type) = type;
3860 SET_TYPE_STRUCTURAL_EQUALITY (underlying_type);
3862 return underlying_type;
3865 if (!complete_type_or_else (type, NULL_TREE))
3866 return error_mark_node;
3868 if (TREE_CODE (type) != ENUMERAL_TYPE)
3870 error ("%qT is not an enumeration type", type);
3871 return error_mark_node;
3874 underlying_type = ENUM_UNDERLYING_TYPE (type);
3876 /* Fixup necessary in this case because ENUM_UNDERLYING_TYPE
3877 includes TYPE_MIN_VALUE and TYPE_MAX_VALUE information.
3878 See finish_enum_value_list for details. */
3879 if (!ENUM_FIXED_UNDERLYING_TYPE_P (type))
3880 underlying_type
3881 = c_common_type_for_mode (TYPE_MODE (underlying_type),
3882 TYPE_UNSIGNED (underlying_type));
3884 return underlying_type;
3887 /* Implement the __direct_bases keyword: Return the direct base classes
3888 of type */
3890 tree
3891 calculate_direct_bases (tree type)
3893 vec<tree, va_gc> *vector = make_tree_vector();
3894 tree bases_vec = NULL_TREE;
3895 vec<tree, va_gc> *base_binfos;
3896 tree binfo;
3897 unsigned i;
3899 complete_type (type);
3901 if (!NON_UNION_CLASS_TYPE_P (type))
3902 return make_tree_vec (0);
3904 base_binfos = BINFO_BASE_BINFOS (TYPE_BINFO (type));
3906 /* Virtual bases are initialized first */
3907 for (i = 0; base_binfos->iterate (i, &binfo); i++)
3909 if (BINFO_VIRTUAL_P (binfo))
3911 vec_safe_push (vector, binfo);
3915 /* Now non-virtuals */
3916 for (i = 0; base_binfos->iterate (i, &binfo); i++)
3918 if (!BINFO_VIRTUAL_P (binfo))
3920 vec_safe_push (vector, binfo);
3925 bases_vec = make_tree_vec (vector->length ());
3927 for (i = 0; i < vector->length (); ++i)
3929 TREE_VEC_ELT (bases_vec, i) = BINFO_TYPE ((*vector)[i]);
3931 return bases_vec;
3934 /* Implement the __bases keyword: Return the base classes
3935 of type */
3937 /* Find morally non-virtual base classes by walking binfo hierarchy */
3938 /* Virtual base classes are handled separately in finish_bases */
3940 static tree
3941 dfs_calculate_bases_pre (tree binfo, void * /*data_*/)
3943 /* Don't walk bases of virtual bases */
3944 return BINFO_VIRTUAL_P (binfo) ? dfs_skip_bases : NULL_TREE;
3947 static tree
3948 dfs_calculate_bases_post (tree binfo, void *data_)
3950 vec<tree, va_gc> **data = ((vec<tree, va_gc> **) data_);
3951 if (!BINFO_VIRTUAL_P (binfo))
3953 vec_safe_push (*data, BINFO_TYPE (binfo));
3955 return NULL_TREE;
3958 /* Calculates the morally non-virtual base classes of a class */
3959 static vec<tree, va_gc> *
3960 calculate_bases_helper (tree type)
3962 vec<tree, va_gc> *vector = make_tree_vector();
3964 /* Now add non-virtual base classes in order of construction */
3965 if (TYPE_BINFO (type))
3966 dfs_walk_all (TYPE_BINFO (type),
3967 dfs_calculate_bases_pre, dfs_calculate_bases_post, &vector);
3968 return vector;
3971 tree
3972 calculate_bases (tree type)
3974 vec<tree, va_gc> *vector = make_tree_vector();
3975 tree bases_vec = NULL_TREE;
3976 unsigned i;
3977 vec<tree, va_gc> *vbases;
3978 vec<tree, va_gc> *nonvbases;
3979 tree binfo;
3981 complete_type (type);
3983 if (!NON_UNION_CLASS_TYPE_P (type))
3984 return make_tree_vec (0);
3986 /* First go through virtual base classes */
3987 for (vbases = CLASSTYPE_VBASECLASSES (type), i = 0;
3988 vec_safe_iterate (vbases, i, &binfo); i++)
3990 vec<tree, va_gc> *vbase_bases;
3991 vbase_bases = calculate_bases_helper (BINFO_TYPE (binfo));
3992 vec_safe_splice (vector, vbase_bases);
3993 release_tree_vector (vbase_bases);
3996 /* Now for the non-virtual bases */
3997 nonvbases = calculate_bases_helper (type);
3998 vec_safe_splice (vector, nonvbases);
3999 release_tree_vector (nonvbases);
4001 /* Note that during error recovery vector->length can even be zero. */
4002 if (vector->length () > 1)
4004 /* Last element is entire class, so don't copy */
4005 bases_vec = make_tree_vec (vector->length() - 1);
4007 for (i = 0; i < vector->length () - 1; ++i)
4008 TREE_VEC_ELT (bases_vec, i) = (*vector)[i];
4010 else
4011 bases_vec = make_tree_vec (0);
4013 release_tree_vector (vector);
4014 return bases_vec;
4017 tree
4018 finish_bases (tree type, bool direct)
4020 tree bases = NULL_TREE;
4022 if (!processing_template_decl)
4024 /* Parameter packs can only be used in templates */
4025 error ("Parameter pack __bases only valid in template declaration");
4026 return error_mark_node;
4029 bases = cxx_make_type (BASES);
4030 BASES_TYPE (bases) = type;
4031 BASES_DIRECT (bases) = direct;
4032 SET_TYPE_STRUCTURAL_EQUALITY (bases);
4034 return bases;
4037 /* Perform C++-specific checks for __builtin_offsetof before calling
4038 fold_offsetof. */
4040 tree
4041 finish_offsetof (tree object_ptr, tree expr, location_t loc)
4043 /* If we're processing a template, we can't finish the semantics yet.
4044 Otherwise we can fold the entire expression now. */
4045 if (processing_template_decl)
4047 expr = build2 (OFFSETOF_EXPR, size_type_node, expr, object_ptr);
4048 SET_EXPR_LOCATION (expr, loc);
4049 return expr;
4052 if (TREE_CODE (expr) == PSEUDO_DTOR_EXPR)
4054 error ("cannot apply %<offsetof%> to destructor %<~%T%>",
4055 TREE_OPERAND (expr, 2));
4056 return error_mark_node;
4058 if (TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE
4059 || TREE_CODE (TREE_TYPE (expr)) == METHOD_TYPE
4060 || TREE_TYPE (expr) == unknown_type_node)
4062 if (INDIRECT_REF_P (expr))
4063 error ("second operand of %<offsetof%> is neither a single "
4064 "identifier nor a sequence of member accesses and "
4065 "array references");
4066 else
4068 if (TREE_CODE (expr) == COMPONENT_REF
4069 || TREE_CODE (expr) == COMPOUND_EXPR)
4070 expr = TREE_OPERAND (expr, 1);
4071 error ("cannot apply %<offsetof%> to member function %qD", expr);
4073 return error_mark_node;
4075 if (TREE_CODE (expr) == CONST_DECL)
4077 error ("cannot apply %<offsetof%> to an enumerator %qD", expr);
4078 return error_mark_node;
4080 if (REFERENCE_REF_P (expr))
4081 expr = TREE_OPERAND (expr, 0);
4082 if (!complete_type_or_else (TREE_TYPE (TREE_TYPE (object_ptr)), object_ptr))
4083 return error_mark_node;
4084 if (warn_invalid_offsetof
4085 && CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (object_ptr)))
4086 && CLASSTYPE_NON_STD_LAYOUT (TREE_TYPE (TREE_TYPE (object_ptr)))
4087 && cp_unevaluated_operand == 0)
4088 pedwarn (loc, OPT_Winvalid_offsetof,
4089 "offsetof within non-standard-layout type %qT is undefined",
4090 TREE_TYPE (TREE_TYPE (object_ptr)));
4091 return fold_offsetof (expr);
4094 /* Replace the AGGR_INIT_EXPR at *TP with an equivalent CALL_EXPR. This
4095 function is broken out from the above for the benefit of the tree-ssa
4096 project. */
4098 void
4099 simplify_aggr_init_expr (tree *tp)
4101 tree aggr_init_expr = *tp;
4103 /* Form an appropriate CALL_EXPR. */
4104 tree fn = AGGR_INIT_EXPR_FN (aggr_init_expr);
4105 tree slot = AGGR_INIT_EXPR_SLOT (aggr_init_expr);
4106 tree type = TREE_TYPE (slot);
4108 tree call_expr;
4109 enum style_t { ctor, arg, pcc } style;
4111 if (AGGR_INIT_VIA_CTOR_P (aggr_init_expr))
4112 style = ctor;
4113 #ifdef PCC_STATIC_STRUCT_RETURN
4114 else if (1)
4115 style = pcc;
4116 #endif
4117 else
4119 gcc_assert (TREE_ADDRESSABLE (type));
4120 style = arg;
4123 call_expr = build_call_array_loc (input_location,
4124 TREE_TYPE (TREE_TYPE (TREE_TYPE (fn))),
4126 aggr_init_expr_nargs (aggr_init_expr),
4127 AGGR_INIT_EXPR_ARGP (aggr_init_expr));
4128 TREE_NOTHROW (call_expr) = TREE_NOTHROW (aggr_init_expr);
4129 CALL_FROM_THUNK_P (call_expr) = AGGR_INIT_FROM_THUNK_P (aggr_init_expr);
4130 CALL_EXPR_OPERATOR_SYNTAX (call_expr)
4131 = CALL_EXPR_OPERATOR_SYNTAX (aggr_init_expr);
4132 CALL_EXPR_ORDERED_ARGS (call_expr) = CALL_EXPR_ORDERED_ARGS (aggr_init_expr);
4133 CALL_EXPR_REVERSE_ARGS (call_expr) = CALL_EXPR_REVERSE_ARGS (aggr_init_expr);
4135 if (style == ctor)
4137 /* Replace the first argument to the ctor with the address of the
4138 slot. */
4139 cxx_mark_addressable (slot);
4140 CALL_EXPR_ARG (call_expr, 0) =
4141 build1 (ADDR_EXPR, build_pointer_type (type), slot);
4143 else if (style == arg)
4145 /* Just mark it addressable here, and leave the rest to
4146 expand_call{,_inline}. */
4147 cxx_mark_addressable (slot);
4148 CALL_EXPR_RETURN_SLOT_OPT (call_expr) = true;
4149 call_expr = build2 (INIT_EXPR, TREE_TYPE (call_expr), slot, call_expr);
4151 else if (style == pcc)
4153 /* If we're using the non-reentrant PCC calling convention, then we
4154 need to copy the returned value out of the static buffer into the
4155 SLOT. */
4156 push_deferring_access_checks (dk_no_check);
4157 call_expr = build_aggr_init (slot, call_expr,
4158 DIRECT_BIND | LOOKUP_ONLYCONVERTING,
4159 tf_warning_or_error);
4160 pop_deferring_access_checks ();
4161 call_expr = build2 (COMPOUND_EXPR, TREE_TYPE (slot), call_expr, slot);
4164 if (AGGR_INIT_ZERO_FIRST (aggr_init_expr))
4166 tree init = build_zero_init (type, NULL_TREE,
4167 /*static_storage_p=*/false);
4168 init = build2 (INIT_EXPR, void_type_node, slot, init);
4169 call_expr = build2 (COMPOUND_EXPR, TREE_TYPE (call_expr),
4170 init, call_expr);
4173 *tp = call_expr;
4176 /* Emit all thunks to FN that should be emitted when FN is emitted. */
4178 void
4179 emit_associated_thunks (tree fn)
4181 /* When we use vcall offsets, we emit thunks with the virtual
4182 functions to which they thunk. The whole point of vcall offsets
4183 is so that you can know statically the entire set of thunks that
4184 will ever be needed for a given virtual function, thereby
4185 enabling you to output all the thunks with the function itself. */
4186 if (DECL_VIRTUAL_P (fn)
4187 /* Do not emit thunks for extern template instantiations. */
4188 && ! DECL_REALLY_EXTERN (fn))
4190 tree thunk;
4192 for (thunk = DECL_THUNKS (fn); thunk; thunk = DECL_CHAIN (thunk))
4194 if (!THUNK_ALIAS (thunk))
4196 use_thunk (thunk, /*emit_p=*/1);
4197 if (DECL_RESULT_THUNK_P (thunk))
4199 tree probe;
4201 for (probe = DECL_THUNKS (thunk);
4202 probe; probe = DECL_CHAIN (probe))
4203 use_thunk (probe, /*emit_p=*/1);
4206 else
4207 gcc_assert (!DECL_THUNKS (thunk));
4212 /* Generate RTL for FN. */
4214 bool
4215 expand_or_defer_fn_1 (tree fn)
4217 /* When the parser calls us after finishing the body of a template
4218 function, we don't really want to expand the body. */
4219 if (processing_template_decl)
4221 /* Normally, collection only occurs in rest_of_compilation. So,
4222 if we don't collect here, we never collect junk generated
4223 during the processing of templates until we hit a
4224 non-template function. It's not safe to do this inside a
4225 nested class, though, as the parser may have local state that
4226 is not a GC root. */
4227 if (!function_depth)
4228 ggc_collect ();
4229 return false;
4232 gcc_assert (DECL_SAVED_TREE (fn));
4234 /* We make a decision about linkage for these functions at the end
4235 of the compilation. Until that point, we do not want the back
4236 end to output them -- but we do want it to see the bodies of
4237 these functions so that it can inline them as appropriate. */
4238 if (DECL_DECLARED_INLINE_P (fn) || DECL_IMPLICIT_INSTANTIATION (fn))
4240 if (DECL_INTERFACE_KNOWN (fn))
4241 /* We've already made a decision as to how this function will
4242 be handled. */;
4243 else if (!at_eof)
4244 tentative_decl_linkage (fn);
4245 else
4246 import_export_decl (fn);
4248 /* If the user wants us to keep all inline functions, then mark
4249 this function as needed so that finish_file will make sure to
4250 output it later. Similarly, all dllexport'd functions must
4251 be emitted; there may be callers in other DLLs. */
4252 if (DECL_DECLARED_INLINE_P (fn)
4253 && !DECL_REALLY_EXTERN (fn)
4254 && (flag_keep_inline_functions
4255 || (flag_keep_inline_dllexport
4256 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (fn)))))
4258 mark_needed (fn);
4259 DECL_EXTERNAL (fn) = 0;
4263 /* If this is a constructor or destructor body, we have to clone
4264 it. */
4265 if (maybe_clone_body (fn))
4267 /* We don't want to process FN again, so pretend we've written
4268 it out, even though we haven't. */
4269 TREE_ASM_WRITTEN (fn) = 1;
4270 /* If this is a constexpr function, keep DECL_SAVED_TREE. */
4271 if (!DECL_DECLARED_CONSTEXPR_P (fn))
4272 DECL_SAVED_TREE (fn) = NULL_TREE;
4273 return false;
4276 /* There's no reason to do any of the work here if we're only doing
4277 semantic analysis; this code just generates RTL. */
4278 if (flag_syntax_only)
4279 return false;
4281 return true;
4284 void
4285 expand_or_defer_fn (tree fn)
4287 if (expand_or_defer_fn_1 (fn))
4289 function_depth++;
4291 /* Expand or defer, at the whim of the compilation unit manager. */
4292 cgraph_node::finalize_function (fn, function_depth > 1);
4293 emit_associated_thunks (fn);
4295 function_depth--;
4299 struct nrv_data
4301 nrv_data () : visited (37) {}
4303 tree var;
4304 tree result;
4305 hash_table<nofree_ptr_hash <tree_node> > visited;
4308 /* Helper function for walk_tree, used by finalize_nrv below. */
4310 static tree
4311 finalize_nrv_r (tree* tp, int* walk_subtrees, void* data)
4313 struct nrv_data *dp = (struct nrv_data *)data;
4314 tree_node **slot;
4316 /* No need to walk into types. There wouldn't be any need to walk into
4317 non-statements, except that we have to consider STMT_EXPRs. */
4318 if (TYPE_P (*tp))
4319 *walk_subtrees = 0;
4320 /* Change all returns to just refer to the RESULT_DECL; this is a nop,
4321 but differs from using NULL_TREE in that it indicates that we care
4322 about the value of the RESULT_DECL. */
4323 else if (TREE_CODE (*tp) == RETURN_EXPR)
4324 TREE_OPERAND (*tp, 0) = dp->result;
4325 /* Change all cleanups for the NRV to only run when an exception is
4326 thrown. */
4327 else if (TREE_CODE (*tp) == CLEANUP_STMT
4328 && CLEANUP_DECL (*tp) == dp->var)
4329 CLEANUP_EH_ONLY (*tp) = 1;
4330 /* Replace the DECL_EXPR for the NRV with an initialization of the
4331 RESULT_DECL, if needed. */
4332 else if (TREE_CODE (*tp) == DECL_EXPR
4333 && DECL_EXPR_DECL (*tp) == dp->var)
4335 tree init;
4336 if (DECL_INITIAL (dp->var)
4337 && DECL_INITIAL (dp->var) != error_mark_node)
4338 init = build2 (INIT_EXPR, void_type_node, dp->result,
4339 DECL_INITIAL (dp->var));
4340 else
4341 init = build_empty_stmt (EXPR_LOCATION (*tp));
4342 DECL_INITIAL (dp->var) = NULL_TREE;
4343 SET_EXPR_LOCATION (init, EXPR_LOCATION (*tp));
4344 *tp = init;
4346 /* And replace all uses of the NRV with the RESULT_DECL. */
4347 else if (*tp == dp->var)
4348 *tp = dp->result;
4350 /* Avoid walking into the same tree more than once. Unfortunately, we
4351 can't just use walk_tree_without duplicates because it would only call
4352 us for the first occurrence of dp->var in the function body. */
4353 slot = dp->visited.find_slot (*tp, INSERT);
4354 if (*slot)
4355 *walk_subtrees = 0;
4356 else
4357 *slot = *tp;
4359 /* Keep iterating. */
4360 return NULL_TREE;
4363 /* Called from finish_function to implement the named return value
4364 optimization by overriding all the RETURN_EXPRs and pertinent
4365 CLEANUP_STMTs and replacing all occurrences of VAR with RESULT, the
4366 RESULT_DECL for the function. */
4368 void
4369 finalize_nrv (tree *tp, tree var, tree result)
4371 struct nrv_data data;
4373 /* Copy name from VAR to RESULT. */
4374 DECL_NAME (result) = DECL_NAME (var);
4375 /* Don't forget that we take its address. */
4376 TREE_ADDRESSABLE (result) = TREE_ADDRESSABLE (var);
4377 /* Finally set DECL_VALUE_EXPR to avoid assigning
4378 a stack slot at -O0 for the original var and debug info
4379 uses RESULT location for VAR. */
4380 SET_DECL_VALUE_EXPR (var, result);
4381 DECL_HAS_VALUE_EXPR_P (var) = 1;
4383 data.var = var;
4384 data.result = result;
4385 cp_walk_tree (tp, finalize_nrv_r, &data, 0);
4388 /* Create CP_OMP_CLAUSE_INFO for clause C. Returns true if it is invalid. */
4390 bool
4391 cxx_omp_create_clause_info (tree c, tree type, bool need_default_ctor,
4392 bool need_copy_ctor, bool need_copy_assignment,
4393 bool need_dtor)
4395 int save_errorcount = errorcount;
4396 tree info, t;
4398 /* Always allocate 3 elements for simplicity. These are the
4399 function decls for the ctor, dtor, and assignment op.
4400 This layout is known to the three lang hooks,
4401 cxx_omp_clause_default_init, cxx_omp_clause_copy_init,
4402 and cxx_omp_clause_assign_op. */
4403 info = make_tree_vec (3);
4404 CP_OMP_CLAUSE_INFO (c) = info;
4406 if (need_default_ctor || need_copy_ctor)
4408 if (need_default_ctor)
4409 t = get_default_ctor (type);
4410 else
4411 t = get_copy_ctor (type, tf_warning_or_error);
4413 if (t && !trivial_fn_p (t))
4414 TREE_VEC_ELT (info, 0) = t;
4417 if (need_dtor && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
4418 TREE_VEC_ELT (info, 1) = get_dtor (type, tf_warning_or_error);
4420 if (need_copy_assignment)
4422 t = get_copy_assign (type);
4424 if (t && !trivial_fn_p (t))
4425 TREE_VEC_ELT (info, 2) = t;
4428 return errorcount != save_errorcount;
4431 /* If DECL is DECL_OMP_PRIVATIZED_MEMBER, return corresponding
4432 FIELD_DECL, otherwise return DECL itself. */
4434 static tree
4435 omp_clause_decl_field (tree decl)
4437 if (VAR_P (decl)
4438 && DECL_HAS_VALUE_EXPR_P (decl)
4439 && DECL_ARTIFICIAL (decl)
4440 && DECL_LANG_SPECIFIC (decl)
4441 && DECL_OMP_PRIVATIZED_MEMBER (decl))
4443 tree f = DECL_VALUE_EXPR (decl);
4444 if (INDIRECT_REF_P (f))
4445 f = TREE_OPERAND (f, 0);
4446 if (TREE_CODE (f) == COMPONENT_REF)
4448 f = TREE_OPERAND (f, 1);
4449 gcc_assert (TREE_CODE (f) == FIELD_DECL);
4450 return f;
4453 return NULL_TREE;
4456 /* Adjust DECL if needed for printing using %qE. */
4458 static tree
4459 omp_clause_printable_decl (tree decl)
4461 tree t = omp_clause_decl_field (decl);
4462 if (t)
4463 return t;
4464 return decl;
4467 /* For a FIELD_DECL F and corresponding DECL_OMP_PRIVATIZED_MEMBER
4468 VAR_DECL T that doesn't need a DECL_EXPR added, record it for
4469 privatization. */
4471 static void
4472 omp_note_field_privatization (tree f, tree t)
4474 if (!omp_private_member_map)
4475 omp_private_member_map = new hash_map<tree, tree>;
4476 tree &v = omp_private_member_map->get_or_insert (f);
4477 if (v == NULL_TREE)
4479 v = t;
4480 omp_private_member_vec.safe_push (f);
4481 /* Signal that we don't want to create DECL_EXPR for this dummy var. */
4482 omp_private_member_vec.safe_push (integer_zero_node);
4486 /* Privatize FIELD_DECL T, return corresponding DECL_OMP_PRIVATIZED_MEMBER
4487 dummy VAR_DECL. */
4489 tree
4490 omp_privatize_field (tree t, bool shared)
4492 tree m = finish_non_static_data_member (t, NULL_TREE, NULL_TREE);
4493 if (m == error_mark_node)
4494 return error_mark_node;
4495 if (!omp_private_member_map && !shared)
4496 omp_private_member_map = new hash_map<tree, tree>;
4497 if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE)
4499 gcc_assert (INDIRECT_REF_P (m));
4500 m = TREE_OPERAND (m, 0);
4502 tree vb = NULL_TREE;
4503 tree &v = shared ? vb : omp_private_member_map->get_or_insert (t);
4504 if (v == NULL_TREE)
4506 v = create_temporary_var (TREE_TYPE (m));
4507 retrofit_lang_decl (v);
4508 DECL_OMP_PRIVATIZED_MEMBER (v) = 1;
4509 SET_DECL_VALUE_EXPR (v, m);
4510 DECL_HAS_VALUE_EXPR_P (v) = 1;
4511 if (!shared)
4512 omp_private_member_vec.safe_push (t);
4514 return v;
4517 /* Helper function for handle_omp_array_sections. Called recursively
4518 to handle multiple array-section-subscripts. C is the clause,
4519 T current expression (initially OMP_CLAUSE_DECL), which is either
4520 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
4521 expression if specified, TREE_VALUE length expression if specified,
4522 TREE_CHAIN is what it has been specified after, or some decl.
4523 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
4524 set to true if any of the array-section-subscript could have length
4525 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
4526 first array-section-subscript which is known not to have length
4527 of one. Given say:
4528 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
4529 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
4530 all are or may have length of 1, array-section-subscript [:2] is the
4531 first one known not to have length 1. For array-section-subscript
4532 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
4533 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
4534 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
4535 case though, as some lengths could be zero. */
4537 static tree
4538 handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
4539 bool &maybe_zero_len, unsigned int &first_non_one,
4540 enum c_omp_region_type ort)
4542 tree ret, low_bound, length, type;
4543 if (TREE_CODE (t) != TREE_LIST)
4545 if (error_operand_p (t))
4546 return error_mark_node;
4547 if (REFERENCE_REF_P (t)
4548 && TREE_CODE (TREE_OPERAND (t, 0)) == COMPONENT_REF)
4549 t = TREE_OPERAND (t, 0);
4550 ret = t;
4551 if (TREE_CODE (t) == COMPONENT_REF
4552 && ort == C_ORT_OMP
4553 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
4554 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO
4555 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FROM)
4556 && !type_dependent_expression_p (t))
4558 if (TREE_CODE (TREE_OPERAND (t, 1)) == FIELD_DECL
4559 && DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
4561 error_at (OMP_CLAUSE_LOCATION (c),
4562 "bit-field %qE in %qs clause",
4563 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4564 return error_mark_node;
4566 while (TREE_CODE (t) == COMPONENT_REF)
4568 if (TREE_TYPE (TREE_OPERAND (t, 0))
4569 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == UNION_TYPE)
4571 error_at (OMP_CLAUSE_LOCATION (c),
4572 "%qE is a member of a union", t);
4573 return error_mark_node;
4575 t = TREE_OPERAND (t, 0);
4577 if (REFERENCE_REF_P (t))
4578 t = TREE_OPERAND (t, 0);
4580 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
4582 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
4583 return NULL_TREE;
4584 if (DECL_P (t))
4585 error_at (OMP_CLAUSE_LOCATION (c),
4586 "%qD is not a variable in %qs clause", t,
4587 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4588 else
4589 error_at (OMP_CLAUSE_LOCATION (c),
4590 "%qE is not a variable in %qs clause", t,
4591 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4592 return error_mark_node;
4594 else if (TREE_CODE (t) == PARM_DECL
4595 && DECL_ARTIFICIAL (t)
4596 && DECL_NAME (t) == this_identifier)
4598 error_at (OMP_CLAUSE_LOCATION (c),
4599 "%<this%> allowed in OpenMP only in %<declare simd%>"
4600 " clauses");
4601 return error_mark_node;
4603 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
4604 && VAR_P (t) && CP_DECL_THREAD_LOCAL_P (t))
4606 error_at (OMP_CLAUSE_LOCATION (c),
4607 "%qD is threadprivate variable in %qs clause", t,
4608 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4609 return error_mark_node;
4611 if (type_dependent_expression_p (ret))
4612 return NULL_TREE;
4613 ret = convert_from_reference (ret);
4614 return ret;
4617 if (ort == C_ORT_OMP
4618 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
4619 && TREE_CODE (TREE_CHAIN (t)) == FIELD_DECL)
4620 TREE_CHAIN (t) = omp_privatize_field (TREE_CHAIN (t), false);
4621 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
4622 maybe_zero_len, first_non_one, ort);
4623 if (ret == error_mark_node || ret == NULL_TREE)
4624 return ret;
4626 type = TREE_TYPE (ret);
4627 low_bound = TREE_PURPOSE (t);
4628 length = TREE_VALUE (t);
4629 if ((low_bound && type_dependent_expression_p (low_bound))
4630 || (length && type_dependent_expression_p (length)))
4631 return NULL_TREE;
4633 if (low_bound == error_mark_node || length == error_mark_node)
4634 return error_mark_node;
4636 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
4638 error_at (OMP_CLAUSE_LOCATION (c),
4639 "low bound %qE of array section does not have integral type",
4640 low_bound);
4641 return error_mark_node;
4643 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
4645 error_at (OMP_CLAUSE_LOCATION (c),
4646 "length %qE of array section does not have integral type",
4647 length);
4648 return error_mark_node;
4650 if (low_bound)
4651 low_bound = mark_rvalue_use (low_bound);
4652 if (length)
4653 length = mark_rvalue_use (length);
4654 /* We need to reduce to real constant-values for checks below. */
4655 if (length)
4656 length = fold_simple (length);
4657 if (low_bound)
4658 low_bound = fold_simple (low_bound);
4659 if (low_bound
4660 && TREE_CODE (low_bound) == INTEGER_CST
4661 && TYPE_PRECISION (TREE_TYPE (low_bound))
4662 > TYPE_PRECISION (sizetype))
4663 low_bound = fold_convert (sizetype, low_bound);
4664 if (length
4665 && TREE_CODE (length) == INTEGER_CST
4666 && TYPE_PRECISION (TREE_TYPE (length))
4667 > TYPE_PRECISION (sizetype))
4668 length = fold_convert (sizetype, length);
4669 if (low_bound == NULL_TREE)
4670 low_bound = integer_zero_node;
4672 if (length != NULL_TREE)
4674 if (!integer_nonzerop (length))
4676 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
4677 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
4679 if (integer_zerop (length))
4681 error_at (OMP_CLAUSE_LOCATION (c),
4682 "zero length array section in %qs clause",
4683 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4684 return error_mark_node;
4687 else
4688 maybe_zero_len = true;
4690 if (first_non_one == types.length ()
4691 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
4692 first_non_one++;
4694 if (TREE_CODE (type) == ARRAY_TYPE)
4696 if (length == NULL_TREE
4697 && (TYPE_DOMAIN (type) == NULL_TREE
4698 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
4700 error_at (OMP_CLAUSE_LOCATION (c),
4701 "for unknown bound array type length expression must "
4702 "be specified");
4703 return error_mark_node;
4705 if (TREE_CODE (low_bound) == INTEGER_CST
4706 && tree_int_cst_sgn (low_bound) == -1)
4708 error_at (OMP_CLAUSE_LOCATION (c),
4709 "negative low bound in array section in %qs clause",
4710 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4711 return error_mark_node;
4713 if (length != NULL_TREE
4714 && TREE_CODE (length) == INTEGER_CST
4715 && tree_int_cst_sgn (length) == -1)
4717 error_at (OMP_CLAUSE_LOCATION (c),
4718 "negative length in array section in %qs clause",
4719 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4720 return error_mark_node;
4722 if (TYPE_DOMAIN (type)
4723 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
4724 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
4725 == INTEGER_CST)
4727 tree size
4728 = fold_convert (sizetype, TYPE_MAX_VALUE (TYPE_DOMAIN (type)));
4729 size = size_binop (PLUS_EXPR, size, size_one_node);
4730 if (TREE_CODE (low_bound) == INTEGER_CST)
4732 if (tree_int_cst_lt (size, low_bound))
4734 error_at (OMP_CLAUSE_LOCATION (c),
4735 "low bound %qE above array section size "
4736 "in %qs clause", low_bound,
4737 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4738 return error_mark_node;
4740 if (tree_int_cst_equal (size, low_bound))
4742 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
4743 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
4745 error_at (OMP_CLAUSE_LOCATION (c),
4746 "zero length array section in %qs clause",
4747 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4748 return error_mark_node;
4750 maybe_zero_len = true;
4752 else if (length == NULL_TREE
4753 && first_non_one == types.length ()
4754 && tree_int_cst_equal
4755 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
4756 low_bound))
4757 first_non_one++;
4759 else if (length == NULL_TREE)
4761 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
4762 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION)
4763 maybe_zero_len = true;
4764 if (first_non_one == types.length ())
4765 first_non_one++;
4767 if (length && TREE_CODE (length) == INTEGER_CST)
4769 if (tree_int_cst_lt (size, length))
4771 error_at (OMP_CLAUSE_LOCATION (c),
4772 "length %qE above array section size "
4773 "in %qs clause", length,
4774 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4775 return error_mark_node;
4777 if (TREE_CODE (low_bound) == INTEGER_CST)
4779 tree lbpluslen
4780 = size_binop (PLUS_EXPR,
4781 fold_convert (sizetype, low_bound),
4782 fold_convert (sizetype, length));
4783 if (TREE_CODE (lbpluslen) == INTEGER_CST
4784 && tree_int_cst_lt (size, lbpluslen))
4786 error_at (OMP_CLAUSE_LOCATION (c),
4787 "high bound %qE above array section size "
4788 "in %qs clause", lbpluslen,
4789 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4790 return error_mark_node;
4795 else if (length == NULL_TREE)
4797 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
4798 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION)
4799 maybe_zero_len = true;
4800 if (first_non_one == types.length ())
4801 first_non_one++;
4804 /* For [lb:] we will need to evaluate lb more than once. */
4805 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
4807 tree lb = cp_save_expr (low_bound);
4808 if (lb != low_bound)
4810 TREE_PURPOSE (t) = lb;
4811 low_bound = lb;
4815 else if (TREE_CODE (type) == POINTER_TYPE)
4817 if (length == NULL_TREE)
4819 error_at (OMP_CLAUSE_LOCATION (c),
4820 "for pointer type length expression must be specified");
4821 return error_mark_node;
4823 if (length != NULL_TREE
4824 && TREE_CODE (length) == INTEGER_CST
4825 && tree_int_cst_sgn (length) == -1)
4827 error_at (OMP_CLAUSE_LOCATION (c),
4828 "negative length in array section in %qs clause",
4829 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4830 return error_mark_node;
4832 /* If there is a pointer type anywhere but in the very first
4833 array-section-subscript, the array section can't be contiguous. */
4834 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
4835 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
4837 error_at (OMP_CLAUSE_LOCATION (c),
4838 "array section is not contiguous in %qs clause",
4839 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4840 return error_mark_node;
4843 else
4845 error_at (OMP_CLAUSE_LOCATION (c),
4846 "%qE does not have pointer or array type", ret);
4847 return error_mark_node;
4849 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
4850 types.safe_push (TREE_TYPE (ret));
4851 /* We will need to evaluate lb more than once. */
4852 tree lb = cp_save_expr (low_bound);
4853 if (lb != low_bound)
4855 TREE_PURPOSE (t) = lb;
4856 low_bound = lb;
4858 ret = grok_array_decl (OMP_CLAUSE_LOCATION (c), ret, low_bound, false);
4859 return ret;
4862 /* Handle array sections for clause C. */
4864 static bool
4865 handle_omp_array_sections (tree c, enum c_omp_region_type ort)
4867 bool maybe_zero_len = false;
4868 unsigned int first_non_one = 0;
4869 auto_vec<tree, 10> types;
4870 tree first = handle_omp_array_sections_1 (c, OMP_CLAUSE_DECL (c), types,
4871 maybe_zero_len, first_non_one,
4872 ort);
4873 if (first == error_mark_node)
4874 return true;
4875 if (first == NULL_TREE)
4876 return false;
4877 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
4879 tree t = OMP_CLAUSE_DECL (c);
4880 tree tem = NULL_TREE;
4881 if (processing_template_decl)
4882 return false;
4883 /* Need to evaluate side effects in the length expressions
4884 if any. */
4885 while (TREE_CODE (t) == TREE_LIST)
4887 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
4889 if (tem == NULL_TREE)
4890 tem = TREE_VALUE (t);
4891 else
4892 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
4893 TREE_VALUE (t), tem);
4895 t = TREE_CHAIN (t);
4897 if (tem)
4898 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
4899 OMP_CLAUSE_DECL (c) = first;
4901 else
4903 unsigned int num = types.length (), i;
4904 tree t, side_effects = NULL_TREE, size = NULL_TREE;
4905 tree condition = NULL_TREE;
4907 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
4908 maybe_zero_len = true;
4909 if (processing_template_decl && maybe_zero_len)
4910 return false;
4912 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
4913 t = TREE_CHAIN (t))
4915 tree low_bound = TREE_PURPOSE (t);
4916 tree length = TREE_VALUE (t);
4918 i--;
4919 if (low_bound
4920 && TREE_CODE (low_bound) == INTEGER_CST
4921 && TYPE_PRECISION (TREE_TYPE (low_bound))
4922 > TYPE_PRECISION (sizetype))
4923 low_bound = fold_convert (sizetype, low_bound);
4924 if (length
4925 && TREE_CODE (length) == INTEGER_CST
4926 && TYPE_PRECISION (TREE_TYPE (length))
4927 > TYPE_PRECISION (sizetype))
4928 length = fold_convert (sizetype, length);
4929 if (low_bound == NULL_TREE)
4930 low_bound = integer_zero_node;
4931 if (!maybe_zero_len && i > first_non_one)
4933 if (integer_nonzerop (low_bound))
4934 goto do_warn_noncontiguous;
4935 if (length != NULL_TREE
4936 && TREE_CODE (length) == INTEGER_CST
4937 && TYPE_DOMAIN (types[i])
4938 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
4939 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
4940 == INTEGER_CST)
4942 tree size;
4943 size = size_binop (PLUS_EXPR,
4944 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
4945 size_one_node);
4946 if (!tree_int_cst_equal (length, size))
4948 do_warn_noncontiguous:
4949 error_at (OMP_CLAUSE_LOCATION (c),
4950 "array section is not contiguous in %qs "
4951 "clause",
4952 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4953 return true;
4956 if (!processing_template_decl
4957 && length != NULL_TREE
4958 && TREE_SIDE_EFFECTS (length))
4960 if (side_effects == NULL_TREE)
4961 side_effects = length;
4962 else
4963 side_effects = build2 (COMPOUND_EXPR,
4964 TREE_TYPE (side_effects),
4965 length, side_effects);
4968 else if (processing_template_decl)
4969 continue;
4970 else
4972 tree l;
4974 if (i > first_non_one
4975 && ((length && integer_nonzerop (length))
4976 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION))
4977 continue;
4978 if (length)
4979 l = fold_convert (sizetype, length);
4980 else
4982 l = size_binop (PLUS_EXPR,
4983 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
4984 size_one_node);
4985 l = size_binop (MINUS_EXPR, l,
4986 fold_convert (sizetype, low_bound));
4988 if (i > first_non_one)
4990 l = fold_build2 (NE_EXPR, boolean_type_node, l,
4991 size_zero_node);
4992 if (condition == NULL_TREE)
4993 condition = l;
4994 else
4995 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
4996 l, condition);
4998 else if (size == NULL_TREE)
5000 size = size_in_bytes (TREE_TYPE (types[i]));
5001 tree eltype = TREE_TYPE (types[num - 1]);
5002 while (TREE_CODE (eltype) == ARRAY_TYPE)
5003 eltype = TREE_TYPE (eltype);
5004 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
5005 size = size_binop (EXACT_DIV_EXPR, size,
5006 size_in_bytes (eltype));
5007 size = size_binop (MULT_EXPR, size, l);
5008 if (condition)
5009 size = fold_build3 (COND_EXPR, sizetype, condition,
5010 size, size_zero_node);
5012 else
5013 size = size_binop (MULT_EXPR, size, l);
5016 if (!processing_template_decl)
5018 if (side_effects)
5019 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
5020 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
5022 size = size_binop (MINUS_EXPR, size, size_one_node);
5023 tree index_type = build_index_type (size);
5024 tree eltype = TREE_TYPE (first);
5025 while (TREE_CODE (eltype) == ARRAY_TYPE)
5026 eltype = TREE_TYPE (eltype);
5027 tree type = build_array_type (eltype, index_type);
5028 tree ptype = build_pointer_type (eltype);
5029 if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE
5030 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (t))))
5031 t = convert_from_reference (t);
5032 else if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
5033 t = build_fold_addr_expr (t);
5034 tree t2 = build_fold_addr_expr (first);
5035 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
5036 ptrdiff_type_node, t2);
5037 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
5038 ptrdiff_type_node, t2,
5039 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
5040 ptrdiff_type_node, t));
5041 if (tree_fits_shwi_p (t2))
5042 t = build2 (MEM_REF, type, t,
5043 build_int_cst (ptype, tree_to_shwi (t2)));
5044 else
5046 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
5047 sizetype, t2);
5048 t = build2_loc (OMP_CLAUSE_LOCATION (c), POINTER_PLUS_EXPR,
5049 TREE_TYPE (t), t, t2);
5050 t = build2 (MEM_REF, type, t, build_int_cst (ptype, 0));
5052 OMP_CLAUSE_DECL (c) = t;
5053 return false;
5055 OMP_CLAUSE_DECL (c) = first;
5056 OMP_CLAUSE_SIZE (c) = size;
5057 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
5058 || (TREE_CODE (t) == COMPONENT_REF
5059 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE))
5060 return false;
5061 if (ort == C_ORT_OMP || ort == C_ORT_ACC)
5062 switch (OMP_CLAUSE_MAP_KIND (c))
5064 case GOMP_MAP_ALLOC:
5065 case GOMP_MAP_TO:
5066 case GOMP_MAP_FROM:
5067 case GOMP_MAP_TOFROM:
5068 case GOMP_MAP_ALWAYS_TO:
5069 case GOMP_MAP_ALWAYS_FROM:
5070 case GOMP_MAP_ALWAYS_TOFROM:
5071 case GOMP_MAP_RELEASE:
5072 case GOMP_MAP_DELETE:
5073 case GOMP_MAP_FORCE_TO:
5074 case GOMP_MAP_FORCE_FROM:
5075 case GOMP_MAP_FORCE_TOFROM:
5076 case GOMP_MAP_FORCE_PRESENT:
5077 OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (c) = 1;
5078 break;
5079 default:
5080 break;
5082 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
5083 OMP_CLAUSE_MAP);
5084 if ((ort & C_ORT_OMP_DECLARE_SIMD) != C_ORT_OMP && ort != C_ORT_ACC)
5085 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_POINTER);
5086 else if (TREE_CODE (t) == COMPONENT_REF)
5087 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ALWAYS_POINTER);
5088 else if (REFERENCE_REF_P (t)
5089 && TREE_CODE (TREE_OPERAND (t, 0)) == COMPONENT_REF)
5091 t = TREE_OPERAND (t, 0);
5092 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ALWAYS_POINTER);
5094 else
5095 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_FIRSTPRIVATE_POINTER);
5096 if (OMP_CLAUSE_MAP_KIND (c2) != GOMP_MAP_FIRSTPRIVATE_POINTER
5097 && !cxx_mark_addressable (t))
5098 return false;
5099 OMP_CLAUSE_DECL (c2) = t;
5100 t = build_fold_addr_expr (first);
5101 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
5102 ptrdiff_type_node, t);
5103 tree ptr = OMP_CLAUSE_DECL (c2);
5104 ptr = convert_from_reference (ptr);
5105 if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
5106 ptr = build_fold_addr_expr (ptr);
5107 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
5108 ptrdiff_type_node, t,
5109 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
5110 ptrdiff_type_node, ptr));
5111 OMP_CLAUSE_SIZE (c2) = t;
5112 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
5113 OMP_CLAUSE_CHAIN (c) = c2;
5114 ptr = OMP_CLAUSE_DECL (c2);
5115 if (OMP_CLAUSE_MAP_KIND (c2) != GOMP_MAP_FIRSTPRIVATE_POINTER
5116 && TREE_CODE (TREE_TYPE (ptr)) == REFERENCE_TYPE
5117 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (ptr))))
5119 tree c3 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
5120 OMP_CLAUSE_MAP);
5121 OMP_CLAUSE_SET_MAP_KIND (c3, OMP_CLAUSE_MAP_KIND (c2));
5122 OMP_CLAUSE_DECL (c3) = ptr;
5123 if (OMP_CLAUSE_MAP_KIND (c2) == GOMP_MAP_ALWAYS_POINTER)
5124 OMP_CLAUSE_DECL (c2) = build_simple_mem_ref (ptr);
5125 else
5126 OMP_CLAUSE_DECL (c2) = convert_from_reference (ptr);
5127 OMP_CLAUSE_SIZE (c3) = size_zero_node;
5128 OMP_CLAUSE_CHAIN (c3) = OMP_CLAUSE_CHAIN (c2);
5129 OMP_CLAUSE_CHAIN (c2) = c3;
5133 return false;
5136 /* Return identifier to look up for omp declare reduction. */
5138 tree
5139 omp_reduction_id (enum tree_code reduction_code, tree reduction_id, tree type)
5141 const char *p = NULL;
5142 const char *m = NULL;
5143 switch (reduction_code)
5145 case PLUS_EXPR:
5146 case MULT_EXPR:
5147 case MINUS_EXPR:
5148 case BIT_AND_EXPR:
5149 case BIT_XOR_EXPR:
5150 case BIT_IOR_EXPR:
5151 case TRUTH_ANDIF_EXPR:
5152 case TRUTH_ORIF_EXPR:
5153 reduction_id = ovl_op_identifier (false, reduction_code);
5154 break;
5155 case MIN_EXPR:
5156 p = "min";
5157 break;
5158 case MAX_EXPR:
5159 p = "max";
5160 break;
5161 default:
5162 break;
5165 if (p == NULL)
5167 if (TREE_CODE (reduction_id) != IDENTIFIER_NODE)
5168 return error_mark_node;
5169 p = IDENTIFIER_POINTER (reduction_id);
5172 if (type != NULL_TREE)
5173 m = mangle_type_string (TYPE_MAIN_VARIANT (type));
5175 const char prefix[] = "omp declare reduction ";
5176 size_t lenp = sizeof (prefix);
5177 if (strncmp (p, prefix, lenp - 1) == 0)
5178 lenp = 1;
5179 size_t len = strlen (p);
5180 size_t lenm = m ? strlen (m) + 1 : 0;
5181 char *name = XALLOCAVEC (char, lenp + len + lenm);
5182 if (lenp > 1)
5183 memcpy (name, prefix, lenp - 1);
5184 memcpy (name + lenp - 1, p, len + 1);
5185 if (m)
5187 name[lenp + len - 1] = '~';
5188 memcpy (name + lenp + len, m, lenm);
5190 return get_identifier (name);
5193 /* Lookup OpenMP UDR ID for TYPE, return the corresponding artificial
5194 FUNCTION_DECL or NULL_TREE if not found. */
5196 static tree
5197 omp_reduction_lookup (location_t loc, tree id, tree type, tree *baselinkp,
5198 vec<tree> *ambiguousp)
5200 tree orig_id = id;
5201 tree baselink = NULL_TREE;
5202 if (identifier_p (id))
5204 cp_id_kind idk;
5205 bool nonint_cst_expression_p;
5206 const char *error_msg;
5207 id = omp_reduction_id (ERROR_MARK, id, type);
5208 tree decl = lookup_name (id);
5209 if (decl == NULL_TREE)
5210 decl = error_mark_node;
5211 id = finish_id_expression (id, decl, NULL_TREE, &idk, false, true,
5212 &nonint_cst_expression_p, false, true, false,
5213 false, &error_msg, loc);
5214 if (idk == CP_ID_KIND_UNQUALIFIED
5215 && identifier_p (id))
5217 vec<tree, va_gc> *args = NULL;
5218 vec_safe_push (args, build_reference_type (type));
5219 id = perform_koenig_lookup (id, args, tf_none);
5222 else if (TREE_CODE (id) == SCOPE_REF)
5223 id = lookup_qualified_name (TREE_OPERAND (id, 0),
5224 omp_reduction_id (ERROR_MARK,
5225 TREE_OPERAND (id, 1),
5226 type),
5227 false, false);
5228 tree fns = id;
5229 id = NULL_TREE;
5230 if (fns && is_overloaded_fn (fns))
5232 for (lkp_iterator iter (get_fns (fns)); iter; ++iter)
5234 tree fndecl = *iter;
5235 if (TREE_CODE (fndecl) == FUNCTION_DECL)
5237 tree argtype = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
5238 if (same_type_p (TREE_TYPE (argtype), type))
5240 id = fndecl;
5241 break;
5246 if (id && BASELINK_P (fns))
5248 if (baselinkp)
5249 *baselinkp = fns;
5250 else
5251 baselink = fns;
5255 if (!id && CLASS_TYPE_P (type) && TYPE_BINFO (type))
5257 vec<tree> ambiguous = vNULL;
5258 tree binfo = TYPE_BINFO (type), base_binfo, ret = NULL_TREE;
5259 unsigned int ix;
5260 if (ambiguousp == NULL)
5261 ambiguousp = &ambiguous;
5262 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
5264 id = omp_reduction_lookup (loc, orig_id, BINFO_TYPE (base_binfo),
5265 baselinkp ? baselinkp : &baselink,
5266 ambiguousp);
5267 if (id == NULL_TREE)
5268 continue;
5269 if (!ambiguousp->is_empty ())
5270 ambiguousp->safe_push (id);
5271 else if (ret != NULL_TREE)
5273 ambiguousp->safe_push (ret);
5274 ambiguousp->safe_push (id);
5275 ret = NULL_TREE;
5277 else
5278 ret = id;
5280 if (ambiguousp != &ambiguous)
5281 return ret;
5282 if (!ambiguous.is_empty ())
5284 const char *str = _("candidates are:");
5285 unsigned int idx;
5286 tree udr;
5287 error_at (loc, "user defined reduction lookup is ambiguous");
5288 FOR_EACH_VEC_ELT (ambiguous, idx, udr)
5290 inform (DECL_SOURCE_LOCATION (udr), "%s %#qD", str, udr);
5291 if (idx == 0)
5292 str = get_spaces (str);
5294 ambiguous.release ();
5295 ret = error_mark_node;
5296 baselink = NULL_TREE;
5298 id = ret;
5300 if (id && baselink)
5301 perform_or_defer_access_check (BASELINK_BINFO (baselink),
5302 id, id, tf_warning_or_error);
5303 return id;
5306 /* Helper function for cp_parser_omp_declare_reduction_exprs
5307 and tsubst_omp_udr.
5308 Remove CLEANUP_STMT for data (omp_priv variable).
5309 Also append INIT_EXPR for DECL_INITIAL of omp_priv after its
5310 DECL_EXPR. */
5312 tree
5313 cp_remove_omp_priv_cleanup_stmt (tree *tp, int *walk_subtrees, void *data)
5315 if (TYPE_P (*tp))
5316 *walk_subtrees = 0;
5317 else if (TREE_CODE (*tp) == CLEANUP_STMT && CLEANUP_DECL (*tp) == (tree) data)
5318 *tp = CLEANUP_BODY (*tp);
5319 else if (TREE_CODE (*tp) == DECL_EXPR)
5321 tree decl = DECL_EXPR_DECL (*tp);
5322 if (!processing_template_decl
5323 && decl == (tree) data
5324 && DECL_INITIAL (decl)
5325 && DECL_INITIAL (decl) != error_mark_node)
5327 tree list = NULL_TREE;
5328 append_to_statement_list_force (*tp, &list);
5329 tree init_expr = build2 (INIT_EXPR, void_type_node,
5330 decl, DECL_INITIAL (decl));
5331 DECL_INITIAL (decl) = NULL_TREE;
5332 append_to_statement_list_force (init_expr, &list);
5333 *tp = list;
5336 return NULL_TREE;
5339 /* Data passed from cp_check_omp_declare_reduction to
5340 cp_check_omp_declare_reduction_r. */
5342 struct cp_check_omp_declare_reduction_data
5344 location_t loc;
5345 tree stmts[7];
5346 bool combiner_p;
5349 /* Helper function for cp_check_omp_declare_reduction, called via
5350 cp_walk_tree. */
5352 static tree
5353 cp_check_omp_declare_reduction_r (tree *tp, int *, void *data)
5355 struct cp_check_omp_declare_reduction_data *udr_data
5356 = (struct cp_check_omp_declare_reduction_data *) data;
5357 if (SSA_VAR_P (*tp)
5358 && !DECL_ARTIFICIAL (*tp)
5359 && *tp != DECL_EXPR_DECL (udr_data->stmts[udr_data->combiner_p ? 0 : 3])
5360 && *tp != DECL_EXPR_DECL (udr_data->stmts[udr_data->combiner_p ? 1 : 4]))
5362 location_t loc = udr_data->loc;
5363 if (udr_data->combiner_p)
5364 error_at (loc, "%<#pragma omp declare reduction%> combiner refers to "
5365 "variable %qD which is not %<omp_out%> nor %<omp_in%>",
5366 *tp);
5367 else
5368 error_at (loc, "%<#pragma omp declare reduction%> initializer refers "
5369 "to variable %qD which is not %<omp_priv%> nor "
5370 "%<omp_orig%>",
5371 *tp);
5372 return *tp;
5374 return NULL_TREE;
5377 /* Diagnose violation of OpenMP #pragma omp declare reduction restrictions. */
5379 void
5380 cp_check_omp_declare_reduction (tree udr)
5382 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (udr)));
5383 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
5384 type = TREE_TYPE (type);
5385 int i;
5386 location_t loc = DECL_SOURCE_LOCATION (udr);
5388 if (type == error_mark_node)
5389 return;
5390 if (ARITHMETIC_TYPE_P (type))
5392 static enum tree_code predef_codes[]
5393 = { PLUS_EXPR, MULT_EXPR, MINUS_EXPR, BIT_AND_EXPR, BIT_XOR_EXPR,
5394 BIT_IOR_EXPR, TRUTH_ANDIF_EXPR, TRUTH_ORIF_EXPR };
5395 for (i = 0; i < 8; i++)
5397 tree id = omp_reduction_id (predef_codes[i], NULL_TREE, NULL_TREE);
5398 const char *n1 = IDENTIFIER_POINTER (DECL_NAME (udr));
5399 const char *n2 = IDENTIFIER_POINTER (id);
5400 if (strncmp (n1, n2, IDENTIFIER_LENGTH (id)) == 0
5401 && (n1[IDENTIFIER_LENGTH (id)] == '~'
5402 || n1[IDENTIFIER_LENGTH (id)] == '\0'))
5403 break;
5406 if (i == 8
5407 && TREE_CODE (type) != COMPLEX_EXPR)
5409 const char prefix_minmax[] = "omp declare reduction m";
5410 size_t prefix_size = sizeof (prefix_minmax) - 1;
5411 const char *n = IDENTIFIER_POINTER (DECL_NAME (udr));
5412 if (strncmp (IDENTIFIER_POINTER (DECL_NAME (udr)),
5413 prefix_minmax, prefix_size) == 0
5414 && ((n[prefix_size] == 'i' && n[prefix_size + 1] == 'n')
5415 || (n[prefix_size] == 'a' && n[prefix_size + 1] == 'x'))
5416 && (n[prefix_size + 2] == '~' || n[prefix_size + 2] == '\0'))
5417 i = 0;
5419 if (i < 8)
5421 error_at (loc, "predeclared arithmetic type %qT in "
5422 "%<#pragma omp declare reduction%>", type);
5423 return;
5426 else if (TREE_CODE (type) == FUNCTION_TYPE
5427 || TREE_CODE (type) == METHOD_TYPE
5428 || TREE_CODE (type) == ARRAY_TYPE)
5430 error_at (loc, "function or array type %qT in "
5431 "%<#pragma omp declare reduction%>", type);
5432 return;
5434 else if (TREE_CODE (type) == REFERENCE_TYPE)
5436 error_at (loc, "reference type %qT in %<#pragma omp declare reduction%>",
5437 type);
5438 return;
5440 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
5442 error_at (loc, "const, volatile or __restrict qualified type %qT in "
5443 "%<#pragma omp declare reduction%>", type);
5444 return;
5447 tree body = DECL_SAVED_TREE (udr);
5448 if (body == NULL_TREE || TREE_CODE (body) != STATEMENT_LIST)
5449 return;
5451 tree_stmt_iterator tsi;
5452 struct cp_check_omp_declare_reduction_data data;
5453 memset (data.stmts, 0, sizeof data.stmts);
5454 for (i = 0, tsi = tsi_start (body);
5455 i < 7 && !tsi_end_p (tsi);
5456 i++, tsi_next (&tsi))
5457 data.stmts[i] = tsi_stmt (tsi);
5458 data.loc = loc;
5459 gcc_assert (tsi_end_p (tsi));
5460 if (i >= 3)
5462 gcc_assert (TREE_CODE (data.stmts[0]) == DECL_EXPR
5463 && TREE_CODE (data.stmts[1]) == DECL_EXPR);
5464 if (TREE_NO_WARNING (DECL_EXPR_DECL (data.stmts[0])))
5465 return;
5466 data.combiner_p = true;
5467 if (cp_walk_tree (&data.stmts[2], cp_check_omp_declare_reduction_r,
5468 &data, NULL))
5469 TREE_NO_WARNING (DECL_EXPR_DECL (data.stmts[0])) = 1;
5471 if (i >= 6)
5473 gcc_assert (TREE_CODE (data.stmts[3]) == DECL_EXPR
5474 && TREE_CODE (data.stmts[4]) == DECL_EXPR);
5475 data.combiner_p = false;
5476 if (cp_walk_tree (&data.stmts[5], cp_check_omp_declare_reduction_r,
5477 &data, NULL)
5478 || cp_walk_tree (&DECL_INITIAL (DECL_EXPR_DECL (data.stmts[3])),
5479 cp_check_omp_declare_reduction_r, &data, NULL))
5480 TREE_NO_WARNING (DECL_EXPR_DECL (data.stmts[0])) = 1;
5481 if (i == 7)
5482 gcc_assert (TREE_CODE (data.stmts[6]) == DECL_EXPR);
5486 /* Helper function of finish_omp_clauses. Clone STMT as if we were making
5487 an inline call. But, remap
5488 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
5489 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
5491 static tree
5492 clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
5493 tree decl, tree placeholder)
5495 copy_body_data id;
5496 hash_map<tree, tree> decl_map;
5498 decl_map.put (omp_decl1, placeholder);
5499 decl_map.put (omp_decl2, decl);
5500 memset (&id, 0, sizeof (id));
5501 id.src_fn = DECL_CONTEXT (omp_decl1);
5502 id.dst_fn = current_function_decl;
5503 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
5504 id.decl_map = &decl_map;
5506 id.copy_decl = copy_decl_no_change;
5507 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
5508 id.transform_new_cfg = true;
5509 id.transform_return_to_modify = false;
5510 id.transform_lang_insert_block = NULL;
5511 id.eh_lp_nr = 0;
5512 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
5513 return stmt;
5516 /* Helper function of finish_omp_clauses, called via cp_walk_tree.
5517 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
5519 static tree
5520 find_omp_placeholder_r (tree *tp, int *, void *data)
5522 if (*tp == (tree) data)
5523 return *tp;
5524 return NULL_TREE;
5527 /* Helper function of finish_omp_clauses. Handle OMP_CLAUSE_REDUCTION C.
5528 Return true if there is some error and the clause should be removed. */
5530 static bool
5531 finish_omp_reduction_clause (tree c, bool *need_default_ctor, bool *need_dtor)
5533 tree t = OMP_CLAUSE_DECL (c);
5534 bool predefined = false;
5535 if (TREE_CODE (t) == TREE_LIST)
5537 gcc_assert (processing_template_decl);
5538 return false;
5540 tree type = TREE_TYPE (t);
5541 if (TREE_CODE (t) == MEM_REF)
5542 type = TREE_TYPE (type);
5543 if (TREE_CODE (type) == REFERENCE_TYPE)
5544 type = TREE_TYPE (type);
5545 if (TREE_CODE (type) == ARRAY_TYPE)
5547 tree oatype = type;
5548 gcc_assert (TREE_CODE (t) != MEM_REF);
5549 while (TREE_CODE (type) == ARRAY_TYPE)
5550 type = TREE_TYPE (type);
5551 if (!processing_template_decl)
5553 t = require_complete_type (t);
5554 if (t == error_mark_node)
5555 return true;
5556 tree size = size_binop (EXACT_DIV_EXPR, TYPE_SIZE_UNIT (oatype),
5557 TYPE_SIZE_UNIT (type));
5558 if (integer_zerop (size))
5560 error ("%qE in %<reduction%> clause is a zero size array",
5561 omp_clause_printable_decl (t));
5562 return true;
5564 size = size_binop (MINUS_EXPR, size, size_one_node);
5565 tree index_type = build_index_type (size);
5566 tree atype = build_array_type (type, index_type);
5567 tree ptype = build_pointer_type (type);
5568 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
5569 t = build_fold_addr_expr (t);
5570 t = build2 (MEM_REF, atype, t, build_int_cst (ptype, 0));
5571 OMP_CLAUSE_DECL (c) = t;
5574 if (type == error_mark_node)
5575 return true;
5576 else if (ARITHMETIC_TYPE_P (type))
5577 switch (OMP_CLAUSE_REDUCTION_CODE (c))
5579 case PLUS_EXPR:
5580 case MULT_EXPR:
5581 case MINUS_EXPR:
5582 predefined = true;
5583 break;
5584 case MIN_EXPR:
5585 case MAX_EXPR:
5586 if (TREE_CODE (type) == COMPLEX_TYPE)
5587 break;
5588 predefined = true;
5589 break;
5590 case BIT_AND_EXPR:
5591 case BIT_IOR_EXPR:
5592 case BIT_XOR_EXPR:
5593 if (FLOAT_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
5594 break;
5595 predefined = true;
5596 break;
5597 case TRUTH_ANDIF_EXPR:
5598 case TRUTH_ORIF_EXPR:
5599 if (FLOAT_TYPE_P (type))
5600 break;
5601 predefined = true;
5602 break;
5603 default:
5604 break;
5606 else if (TYPE_READONLY (type))
5608 error ("%qE has const type for %<reduction%>",
5609 omp_clause_printable_decl (t));
5610 return true;
5612 else if (!processing_template_decl)
5614 t = require_complete_type (t);
5615 if (t == error_mark_node)
5616 return true;
5617 OMP_CLAUSE_DECL (c) = t;
5620 if (predefined)
5622 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = NULL_TREE;
5623 return false;
5625 else if (processing_template_decl)
5626 return false;
5628 tree id = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
5630 type = TYPE_MAIN_VARIANT (type);
5631 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = NULL_TREE;
5632 if (id == NULL_TREE)
5633 id = omp_reduction_id (OMP_CLAUSE_REDUCTION_CODE (c),
5634 NULL_TREE, NULL_TREE);
5635 id = omp_reduction_lookup (OMP_CLAUSE_LOCATION (c), id, type, NULL, NULL);
5636 if (id)
5638 if (id == error_mark_node)
5639 return true;
5640 mark_used (id);
5641 tree body = DECL_SAVED_TREE (id);
5642 if (!body)
5643 return true;
5644 if (TREE_CODE (body) == STATEMENT_LIST)
5646 tree_stmt_iterator tsi;
5647 tree placeholder = NULL_TREE, decl_placeholder = NULL_TREE;
5648 int i;
5649 tree stmts[7];
5650 tree atype = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (id)));
5651 atype = TREE_TYPE (atype);
5652 bool need_static_cast = !same_type_p (type, atype);
5653 memset (stmts, 0, sizeof stmts);
5654 for (i = 0, tsi = tsi_start (body);
5655 i < 7 && !tsi_end_p (tsi);
5656 i++, tsi_next (&tsi))
5657 stmts[i] = tsi_stmt (tsi);
5658 gcc_assert (tsi_end_p (tsi));
5660 if (i >= 3)
5662 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
5663 && TREE_CODE (stmts[1]) == DECL_EXPR);
5664 placeholder = build_lang_decl (VAR_DECL, NULL_TREE, type);
5665 DECL_ARTIFICIAL (placeholder) = 1;
5666 DECL_IGNORED_P (placeholder) = 1;
5667 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
5668 if (TREE_CODE (t) == MEM_REF)
5670 decl_placeholder = build_lang_decl (VAR_DECL, NULL_TREE,
5671 type);
5672 DECL_ARTIFICIAL (decl_placeholder) = 1;
5673 DECL_IGNORED_P (decl_placeholder) = 1;
5674 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c) = decl_placeholder;
5676 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[0])))
5677 cxx_mark_addressable (placeholder);
5678 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[1]))
5679 && TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c)))
5680 != REFERENCE_TYPE)
5681 cxx_mark_addressable (decl_placeholder ? decl_placeholder
5682 : OMP_CLAUSE_DECL (c));
5683 tree omp_out = placeholder;
5684 tree omp_in = decl_placeholder ? decl_placeholder
5685 : convert_from_reference (OMP_CLAUSE_DECL (c));
5686 if (need_static_cast)
5688 tree rtype = build_reference_type (atype);
5689 omp_out = build_static_cast (rtype, omp_out,
5690 tf_warning_or_error);
5691 omp_in = build_static_cast (rtype, omp_in,
5692 tf_warning_or_error);
5693 if (omp_out == error_mark_node || omp_in == error_mark_node)
5694 return true;
5695 omp_out = convert_from_reference (omp_out);
5696 omp_in = convert_from_reference (omp_in);
5698 OMP_CLAUSE_REDUCTION_MERGE (c)
5699 = clone_omp_udr (stmts[2], DECL_EXPR_DECL (stmts[0]),
5700 DECL_EXPR_DECL (stmts[1]), omp_in, omp_out);
5702 if (i >= 6)
5704 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
5705 && TREE_CODE (stmts[4]) == DECL_EXPR);
5706 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[3])))
5707 cxx_mark_addressable (decl_placeholder ? decl_placeholder
5708 : OMP_CLAUSE_DECL (c));
5709 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[4])))
5710 cxx_mark_addressable (placeholder);
5711 tree omp_priv = decl_placeholder ? decl_placeholder
5712 : convert_from_reference (OMP_CLAUSE_DECL (c));
5713 tree omp_orig = placeholder;
5714 if (need_static_cast)
5716 if (i == 7)
5718 error_at (OMP_CLAUSE_LOCATION (c),
5719 "user defined reduction with constructor "
5720 "initializer for base class %qT", atype);
5721 return true;
5723 tree rtype = build_reference_type (atype);
5724 omp_priv = build_static_cast (rtype, omp_priv,
5725 tf_warning_or_error);
5726 omp_orig = build_static_cast (rtype, omp_orig,
5727 tf_warning_or_error);
5728 if (omp_priv == error_mark_node
5729 || omp_orig == error_mark_node)
5730 return true;
5731 omp_priv = convert_from_reference (omp_priv);
5732 omp_orig = convert_from_reference (omp_orig);
5734 if (i == 6)
5735 *need_default_ctor = true;
5736 OMP_CLAUSE_REDUCTION_INIT (c)
5737 = clone_omp_udr (stmts[5], DECL_EXPR_DECL (stmts[4]),
5738 DECL_EXPR_DECL (stmts[3]),
5739 omp_priv, omp_orig);
5740 if (cp_walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
5741 find_omp_placeholder_r, placeholder, NULL))
5742 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
5744 else if (i >= 3)
5746 if (CLASS_TYPE_P (type) && !pod_type_p (type))
5747 *need_default_ctor = true;
5748 else
5750 tree init;
5751 tree v = decl_placeholder ? decl_placeholder
5752 : convert_from_reference (t);
5753 if (AGGREGATE_TYPE_P (TREE_TYPE (v)))
5754 init = build_constructor (TREE_TYPE (v), NULL);
5755 else
5756 init = fold_convert (TREE_TYPE (v), integer_zero_node);
5757 OMP_CLAUSE_REDUCTION_INIT (c)
5758 = build2 (INIT_EXPR, TREE_TYPE (v), v, init);
5763 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
5764 *need_dtor = true;
5765 else
5767 error ("user defined reduction not found for %qE",
5768 omp_clause_printable_decl (t));
5769 return true;
5771 if (TREE_CODE (OMP_CLAUSE_DECL (c)) == MEM_REF)
5772 gcc_assert (TYPE_SIZE_UNIT (type)
5773 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST);
5774 return false;
5777 /* Called from finish_struct_1. linear(this) or linear(this:step)
5778 clauses might not be finalized yet because the class has been incomplete
5779 when parsing #pragma omp declare simd methods. Fix those up now. */
5781 void
5782 finish_omp_declare_simd_methods (tree t)
5784 if (processing_template_decl)
5785 return;
5787 for (tree x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
5789 if (TREE_CODE (TREE_TYPE (x)) != METHOD_TYPE)
5790 continue;
5791 tree ods = lookup_attribute ("omp declare simd", DECL_ATTRIBUTES (x));
5792 if (!ods || !TREE_VALUE (ods))
5793 continue;
5794 for (tree c = TREE_VALUE (TREE_VALUE (ods)); c; c = OMP_CLAUSE_CHAIN (c))
5795 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
5796 && integer_zerop (OMP_CLAUSE_DECL (c))
5797 && OMP_CLAUSE_LINEAR_STEP (c)
5798 && TREE_CODE (TREE_TYPE (OMP_CLAUSE_LINEAR_STEP (c)))
5799 == POINTER_TYPE)
5801 tree s = OMP_CLAUSE_LINEAR_STEP (c);
5802 s = fold_convert_loc (OMP_CLAUSE_LOCATION (c), sizetype, s);
5803 s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MULT_EXPR,
5804 sizetype, s, TYPE_SIZE_UNIT (t));
5805 OMP_CLAUSE_LINEAR_STEP (c) = s;
5810 /* Adjust sink depend clause to take into account pointer offsets.
5812 Return TRUE if there was a problem processing the offset, and the
5813 whole clause should be removed. */
5815 static bool
5816 cp_finish_omp_clause_depend_sink (tree sink_clause)
5818 tree t = OMP_CLAUSE_DECL (sink_clause);
5819 gcc_assert (TREE_CODE (t) == TREE_LIST);
5821 /* Make sure we don't adjust things twice for templates. */
5822 if (processing_template_decl)
5823 return false;
5825 for (; t; t = TREE_CHAIN (t))
5827 tree decl = TREE_VALUE (t);
5828 if (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
5830 tree offset = TREE_PURPOSE (t);
5831 bool neg = wi::neg_p (wi::to_wide (offset));
5832 offset = fold_unary (ABS_EXPR, TREE_TYPE (offset), offset);
5833 decl = mark_rvalue_use (decl);
5834 decl = convert_from_reference (decl);
5835 tree t2 = pointer_int_sum (OMP_CLAUSE_LOCATION (sink_clause),
5836 neg ? MINUS_EXPR : PLUS_EXPR,
5837 decl, offset);
5838 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (sink_clause),
5839 MINUS_EXPR, sizetype,
5840 fold_convert (sizetype, t2),
5841 fold_convert (sizetype, decl));
5842 if (t2 == error_mark_node)
5843 return true;
5844 TREE_PURPOSE (t) = t2;
5847 return false;
5850 /* For all elements of CLAUSES, validate them vs OpenMP constraints.
5851 Remove any elements from the list that are invalid. */
5853 tree
5854 finish_omp_clauses (tree clauses, enum c_omp_region_type ort)
5856 bitmap_head generic_head, firstprivate_head, lastprivate_head;
5857 bitmap_head aligned_head, map_head, map_field_head, oacc_reduction_head;
5858 tree c, t, *pc;
5859 tree safelen = NULL_TREE;
5860 bool branch_seen = false;
5861 bool copyprivate_seen = false;
5862 bool ordered_seen = false;
5863 bool oacc_async = false;
5865 bitmap_obstack_initialize (NULL);
5866 bitmap_initialize (&generic_head, &bitmap_default_obstack);
5867 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
5868 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
5869 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
5870 bitmap_initialize (&map_head, &bitmap_default_obstack);
5871 bitmap_initialize (&map_field_head, &bitmap_default_obstack);
5872 bitmap_initialize (&oacc_reduction_head, &bitmap_default_obstack);
5874 if (ort & C_ORT_ACC)
5875 for (c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
5876 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ASYNC)
5878 oacc_async = true;
5879 break;
5882 for (pc = &clauses, c = clauses; c ; c = *pc)
5884 bool remove = false;
5885 bool field_ok = false;
5887 switch (OMP_CLAUSE_CODE (c))
5889 case OMP_CLAUSE_SHARED:
5890 field_ok = ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP);
5891 goto check_dup_generic;
5892 case OMP_CLAUSE_PRIVATE:
5893 field_ok = ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP);
5894 goto check_dup_generic;
5895 case OMP_CLAUSE_REDUCTION:
5896 field_ok = ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP);
5897 t = OMP_CLAUSE_DECL (c);
5898 if (TREE_CODE (t) == TREE_LIST)
5900 if (handle_omp_array_sections (c, ort))
5902 remove = true;
5903 break;
5905 if (TREE_CODE (t) == TREE_LIST)
5907 while (TREE_CODE (t) == TREE_LIST)
5908 t = TREE_CHAIN (t);
5910 else
5912 gcc_assert (TREE_CODE (t) == MEM_REF);
5913 t = TREE_OPERAND (t, 0);
5914 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
5915 t = TREE_OPERAND (t, 0);
5916 if (TREE_CODE (t) == ADDR_EXPR
5917 || INDIRECT_REF_P (t))
5918 t = TREE_OPERAND (t, 0);
5920 tree n = omp_clause_decl_field (t);
5921 if (n)
5922 t = n;
5923 goto check_dup_generic_t;
5925 if (oacc_async)
5926 cxx_mark_addressable (t);
5927 goto check_dup_generic;
5928 case OMP_CLAUSE_COPYPRIVATE:
5929 copyprivate_seen = true;
5930 field_ok = ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP);
5931 goto check_dup_generic;
5932 case OMP_CLAUSE_COPYIN:
5933 goto check_dup_generic;
5934 case OMP_CLAUSE_LINEAR:
5935 field_ok = ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP);
5936 t = OMP_CLAUSE_DECL (c);
5937 if (ort != C_ORT_OMP_DECLARE_SIMD
5938 && OMP_CLAUSE_LINEAR_KIND (c) != OMP_CLAUSE_LINEAR_DEFAULT)
5940 error_at (OMP_CLAUSE_LOCATION (c),
5941 "modifier should not be specified in %<linear%> "
5942 "clause on %<simd%> or %<for%> constructs");
5943 OMP_CLAUSE_LINEAR_KIND (c) = OMP_CLAUSE_LINEAR_DEFAULT;
5945 if ((VAR_P (t) || TREE_CODE (t) == PARM_DECL)
5946 && !type_dependent_expression_p (t))
5948 tree type = TREE_TYPE (t);
5949 if ((OMP_CLAUSE_LINEAR_KIND (c) == OMP_CLAUSE_LINEAR_REF
5950 || OMP_CLAUSE_LINEAR_KIND (c) == OMP_CLAUSE_LINEAR_UVAL)
5951 && TREE_CODE (type) != REFERENCE_TYPE)
5953 error ("linear clause with %qs modifier applied to "
5954 "non-reference variable with %qT type",
5955 OMP_CLAUSE_LINEAR_KIND (c) == OMP_CLAUSE_LINEAR_REF
5956 ? "ref" : "uval", TREE_TYPE (t));
5957 remove = true;
5958 break;
5960 if (TREE_CODE (type) == REFERENCE_TYPE)
5961 type = TREE_TYPE (type);
5962 if (OMP_CLAUSE_LINEAR_KIND (c) != OMP_CLAUSE_LINEAR_REF)
5964 if (!INTEGRAL_TYPE_P (type)
5965 && TREE_CODE (type) != POINTER_TYPE)
5967 error ("linear clause applied to non-integral non-pointer"
5968 " variable with %qT type", TREE_TYPE (t));
5969 remove = true;
5970 break;
5974 t = OMP_CLAUSE_LINEAR_STEP (c);
5975 if (t == NULL_TREE)
5976 t = integer_one_node;
5977 if (t == error_mark_node)
5979 remove = true;
5980 break;
5982 else if (!type_dependent_expression_p (t)
5983 && !INTEGRAL_TYPE_P (TREE_TYPE (t))
5984 && (ort != C_ORT_OMP_DECLARE_SIMD
5985 || TREE_CODE (t) != PARM_DECL
5986 || TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE
5987 || !INTEGRAL_TYPE_P (TREE_TYPE (TREE_TYPE (t)))))
5989 error ("linear step expression must be integral");
5990 remove = true;
5991 break;
5993 else
5995 t = mark_rvalue_use (t);
5996 if (ort == C_ORT_OMP_DECLARE_SIMD && TREE_CODE (t) == PARM_DECL)
5998 OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c) = 1;
5999 goto check_dup_generic;
6001 if (!processing_template_decl
6002 && (VAR_P (OMP_CLAUSE_DECL (c))
6003 || TREE_CODE (OMP_CLAUSE_DECL (c)) == PARM_DECL))
6005 if (ort == C_ORT_OMP_DECLARE_SIMD)
6007 t = maybe_constant_value (t);
6008 if (TREE_CODE (t) != INTEGER_CST)
6010 error_at (OMP_CLAUSE_LOCATION (c),
6011 "%<linear%> clause step %qE is neither "
6012 "constant nor a parameter", t);
6013 remove = true;
6014 break;
6017 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6018 tree type = TREE_TYPE (OMP_CLAUSE_DECL (c));
6019 if (TREE_CODE (type) == REFERENCE_TYPE)
6020 type = TREE_TYPE (type);
6021 if (OMP_CLAUSE_LINEAR_KIND (c) == OMP_CLAUSE_LINEAR_REF)
6023 type = build_pointer_type (type);
6024 tree d = fold_convert (type, OMP_CLAUSE_DECL (c));
6025 t = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
6026 d, t);
6027 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c),
6028 MINUS_EXPR, sizetype,
6029 fold_convert (sizetype, t),
6030 fold_convert (sizetype, d));
6031 if (t == error_mark_node)
6033 remove = true;
6034 break;
6037 else if (TREE_CODE (type) == POINTER_TYPE
6038 /* Can't multiply the step yet if *this
6039 is still incomplete type. */
6040 && (ort != C_ORT_OMP_DECLARE_SIMD
6041 || TREE_CODE (OMP_CLAUSE_DECL (c)) != PARM_DECL
6042 || !DECL_ARTIFICIAL (OMP_CLAUSE_DECL (c))
6043 || DECL_NAME (OMP_CLAUSE_DECL (c))
6044 != this_identifier
6045 || !TYPE_BEING_DEFINED (TREE_TYPE (type))))
6047 tree d = convert_from_reference (OMP_CLAUSE_DECL (c));
6048 t = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
6049 d, t);
6050 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c),
6051 MINUS_EXPR, sizetype,
6052 fold_convert (sizetype, t),
6053 fold_convert (sizetype, d));
6054 if (t == error_mark_node)
6056 remove = true;
6057 break;
6060 else
6061 t = fold_convert (type, t);
6063 OMP_CLAUSE_LINEAR_STEP (c) = t;
6065 goto check_dup_generic;
6066 check_dup_generic:
6067 t = omp_clause_decl_field (OMP_CLAUSE_DECL (c));
6068 if (t)
6070 if (!remove && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_SHARED)
6071 omp_note_field_privatization (t, OMP_CLAUSE_DECL (c));
6073 else
6074 t = OMP_CLAUSE_DECL (c);
6075 check_dup_generic_t:
6076 if (t == current_class_ptr
6077 && (ort != C_ORT_OMP_DECLARE_SIMD
6078 || (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_LINEAR
6079 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_UNIFORM)))
6081 error ("%<this%> allowed in OpenMP only in %<declare simd%>"
6082 " clauses");
6083 remove = true;
6084 break;
6086 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL
6087 && (!field_ok || TREE_CODE (t) != FIELD_DECL))
6089 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
6090 break;
6091 if (DECL_P (t))
6092 error ("%qD is not a variable in clause %qs", t,
6093 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6094 else
6095 error ("%qE is not a variable in clause %qs", t,
6096 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6097 remove = true;
6099 else if (ort == C_ORT_ACC
6100 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
6102 if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
6104 error ("%qD appears more than once in reduction clauses", t);
6105 remove = true;
6107 else
6108 bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
6110 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
6111 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
6112 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
6114 error ("%qD appears more than once in data clauses", t);
6115 remove = true;
6117 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
6118 && bitmap_bit_p (&map_head, DECL_UID (t)))
6120 if (ort == C_ORT_ACC)
6121 error ("%qD appears more than once in data clauses", t);
6122 else
6123 error ("%qD appears both in data and map clauses", t);
6124 remove = true;
6126 else
6127 bitmap_set_bit (&generic_head, DECL_UID (t));
6128 if (!field_ok)
6129 break;
6130 handle_field_decl:
6131 if (!remove
6132 && TREE_CODE (t) == FIELD_DECL
6133 && t == OMP_CLAUSE_DECL (c)
6134 && ort != C_ORT_ACC)
6136 OMP_CLAUSE_DECL (c)
6137 = omp_privatize_field (t, (OMP_CLAUSE_CODE (c)
6138 == OMP_CLAUSE_SHARED));
6139 if (OMP_CLAUSE_DECL (c) == error_mark_node)
6140 remove = true;
6142 break;
6144 case OMP_CLAUSE_FIRSTPRIVATE:
6145 t = omp_clause_decl_field (OMP_CLAUSE_DECL (c));
6146 if (t)
6147 omp_note_field_privatization (t, OMP_CLAUSE_DECL (c));
6148 else
6149 t = OMP_CLAUSE_DECL (c);
6150 if (ort != C_ORT_ACC && t == current_class_ptr)
6152 error ("%<this%> allowed in OpenMP only in %<declare simd%>"
6153 " clauses");
6154 remove = true;
6155 break;
6157 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL
6158 && ((ort & C_ORT_OMP_DECLARE_SIMD) != C_ORT_OMP
6159 || TREE_CODE (t) != FIELD_DECL))
6161 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
6162 break;
6163 if (DECL_P (t))
6164 error ("%qD is not a variable in clause %<firstprivate%>", t);
6165 else
6166 error ("%qE is not a variable in clause %<firstprivate%>", t);
6167 remove = true;
6169 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
6170 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
6172 error ("%qD appears more than once in data clauses", t);
6173 remove = true;
6175 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
6177 if (ort == C_ORT_ACC)
6178 error ("%qD appears more than once in data clauses", t);
6179 else
6180 error ("%qD appears both in data and map clauses", t);
6181 remove = true;
6183 else
6184 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
6185 goto handle_field_decl;
6187 case OMP_CLAUSE_LASTPRIVATE:
6188 t = omp_clause_decl_field (OMP_CLAUSE_DECL (c));
6189 if (t)
6190 omp_note_field_privatization (t, OMP_CLAUSE_DECL (c));
6191 else
6192 t = OMP_CLAUSE_DECL (c);
6193 if (t == current_class_ptr)
6195 error ("%<this%> allowed in OpenMP only in %<declare simd%>"
6196 " clauses");
6197 remove = true;
6198 break;
6200 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL
6201 && ((ort & C_ORT_OMP_DECLARE_SIMD) != C_ORT_OMP
6202 || TREE_CODE (t) != FIELD_DECL))
6204 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
6205 break;
6206 if (DECL_P (t))
6207 error ("%qD is not a variable in clause %<lastprivate%>", t);
6208 else
6209 error ("%qE is not a variable in clause %<lastprivate%>", t);
6210 remove = true;
6212 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
6213 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
6215 error ("%qD appears more than once in data clauses", t);
6216 remove = true;
6218 else
6219 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
6220 goto handle_field_decl;
6222 case OMP_CLAUSE_IF:
6223 t = OMP_CLAUSE_IF_EXPR (c);
6224 t = maybe_convert_cond (t);
6225 if (t == error_mark_node)
6226 remove = true;
6227 else if (!processing_template_decl)
6228 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6229 OMP_CLAUSE_IF_EXPR (c) = t;
6230 break;
6232 case OMP_CLAUSE_FINAL:
6233 t = OMP_CLAUSE_FINAL_EXPR (c);
6234 t = maybe_convert_cond (t);
6235 if (t == error_mark_node)
6236 remove = true;
6237 else if (!processing_template_decl)
6238 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6239 OMP_CLAUSE_FINAL_EXPR (c) = t;
6240 break;
6242 case OMP_CLAUSE_GANG:
6243 /* Operand 1 is the gang static: argument. */
6244 t = OMP_CLAUSE_OPERAND (c, 1);
6245 if (t != NULL_TREE)
6247 if (t == error_mark_node)
6248 remove = true;
6249 else if (!type_dependent_expression_p (t)
6250 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6252 error ("%<gang%> static expression must be integral");
6253 remove = true;
6255 else
6257 t = mark_rvalue_use (t);
6258 if (!processing_template_decl)
6260 t = maybe_constant_value (t);
6261 if (TREE_CODE (t) == INTEGER_CST
6262 && tree_int_cst_sgn (t) != 1
6263 && t != integer_minus_one_node)
6265 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6266 "%<gang%> static value must be "
6267 "positive");
6268 t = integer_one_node;
6270 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6273 OMP_CLAUSE_OPERAND (c, 1) = t;
6275 /* Check operand 0, the num argument. */
6276 /* FALLTHRU */
6278 case OMP_CLAUSE_WORKER:
6279 case OMP_CLAUSE_VECTOR:
6280 if (OMP_CLAUSE_OPERAND (c, 0) == NULL_TREE)
6281 break;
6282 /* FALLTHRU */
6284 case OMP_CLAUSE_NUM_TASKS:
6285 case OMP_CLAUSE_NUM_TEAMS:
6286 case OMP_CLAUSE_NUM_THREADS:
6287 case OMP_CLAUSE_NUM_GANGS:
6288 case OMP_CLAUSE_NUM_WORKERS:
6289 case OMP_CLAUSE_VECTOR_LENGTH:
6290 t = OMP_CLAUSE_OPERAND (c, 0);
6291 if (t == error_mark_node)
6292 remove = true;
6293 else if (!type_dependent_expression_p (t)
6294 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6296 switch (OMP_CLAUSE_CODE (c))
6298 case OMP_CLAUSE_GANG:
6299 error_at (OMP_CLAUSE_LOCATION (c),
6300 "%<gang%> num expression must be integral"); break;
6301 case OMP_CLAUSE_VECTOR:
6302 error_at (OMP_CLAUSE_LOCATION (c),
6303 "%<vector%> length expression must be integral");
6304 break;
6305 case OMP_CLAUSE_WORKER:
6306 error_at (OMP_CLAUSE_LOCATION (c),
6307 "%<worker%> num expression must be integral");
6308 break;
6309 default:
6310 error_at (OMP_CLAUSE_LOCATION (c),
6311 "%qs expression must be integral",
6312 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6314 remove = true;
6316 else
6318 t = mark_rvalue_use (t);
6319 if (!processing_template_decl)
6321 t = maybe_constant_value (t);
6322 if (TREE_CODE (t) == INTEGER_CST
6323 && tree_int_cst_sgn (t) != 1)
6325 switch (OMP_CLAUSE_CODE (c))
6327 case OMP_CLAUSE_GANG:
6328 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6329 "%<gang%> num value must be positive");
6330 break;
6331 case OMP_CLAUSE_VECTOR:
6332 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6333 "%<vector%> length value must be "
6334 "positive");
6335 break;
6336 case OMP_CLAUSE_WORKER:
6337 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6338 "%<worker%> num value must be "
6339 "positive");
6340 break;
6341 default:
6342 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6343 "%qs value must be positive",
6344 omp_clause_code_name
6345 [OMP_CLAUSE_CODE (c)]);
6347 t = integer_one_node;
6349 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6351 OMP_CLAUSE_OPERAND (c, 0) = t;
6353 break;
6355 case OMP_CLAUSE_SCHEDULE:
6356 if (OMP_CLAUSE_SCHEDULE_KIND (c) & OMP_CLAUSE_SCHEDULE_NONMONOTONIC)
6358 const char *p = NULL;
6359 switch (OMP_CLAUSE_SCHEDULE_KIND (c) & OMP_CLAUSE_SCHEDULE_MASK)
6361 case OMP_CLAUSE_SCHEDULE_STATIC: p = "static"; break;
6362 case OMP_CLAUSE_SCHEDULE_DYNAMIC: break;
6363 case OMP_CLAUSE_SCHEDULE_GUIDED: break;
6364 case OMP_CLAUSE_SCHEDULE_AUTO: p = "auto"; break;
6365 case OMP_CLAUSE_SCHEDULE_RUNTIME: p = "runtime"; break;
6366 default: gcc_unreachable ();
6368 if (p)
6370 error_at (OMP_CLAUSE_LOCATION (c),
6371 "%<nonmonotonic%> modifier specified for %qs "
6372 "schedule kind", p);
6373 OMP_CLAUSE_SCHEDULE_KIND (c)
6374 = (enum omp_clause_schedule_kind)
6375 (OMP_CLAUSE_SCHEDULE_KIND (c)
6376 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
6380 t = OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c);
6381 if (t == NULL)
6383 else if (t == error_mark_node)
6384 remove = true;
6385 else if (!type_dependent_expression_p (t)
6386 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6388 error ("schedule chunk size expression must be integral");
6389 remove = true;
6391 else
6393 t = mark_rvalue_use (t);
6394 if (!processing_template_decl)
6396 t = maybe_constant_value (t);
6397 if (TREE_CODE (t) == INTEGER_CST
6398 && tree_int_cst_sgn (t) != 1)
6400 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6401 "chunk size value must be positive");
6402 t = integer_one_node;
6404 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6406 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
6408 break;
6410 case OMP_CLAUSE_SIMDLEN:
6411 case OMP_CLAUSE_SAFELEN:
6412 t = OMP_CLAUSE_OPERAND (c, 0);
6413 if (t == error_mark_node)
6414 remove = true;
6415 else if (!type_dependent_expression_p (t)
6416 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6418 error ("%qs length expression must be integral",
6419 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6420 remove = true;
6422 else
6424 t = mark_rvalue_use (t);
6425 if (!processing_template_decl)
6427 t = maybe_constant_value (t);
6428 if (TREE_CODE (t) != INTEGER_CST
6429 || tree_int_cst_sgn (t) != 1)
6431 error ("%qs length expression must be positive constant"
6432 " integer expression",
6433 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6434 remove = true;
6437 OMP_CLAUSE_OPERAND (c, 0) = t;
6438 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SAFELEN)
6439 safelen = c;
6441 break;
6443 case OMP_CLAUSE_ASYNC:
6444 t = OMP_CLAUSE_ASYNC_EXPR (c);
6445 if (t == error_mark_node)
6446 remove = true;
6447 else if (!type_dependent_expression_p (t)
6448 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6450 error ("%<async%> expression must be integral");
6451 remove = true;
6453 else
6455 t = mark_rvalue_use (t);
6456 if (!processing_template_decl)
6457 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6458 OMP_CLAUSE_ASYNC_EXPR (c) = t;
6460 break;
6462 case OMP_CLAUSE_WAIT:
6463 t = OMP_CLAUSE_WAIT_EXPR (c);
6464 if (t == error_mark_node)
6465 remove = true;
6466 else if (!processing_template_decl)
6467 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6468 OMP_CLAUSE_WAIT_EXPR (c) = t;
6469 break;
6471 case OMP_CLAUSE_THREAD_LIMIT:
6472 t = OMP_CLAUSE_THREAD_LIMIT_EXPR (c);
6473 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 ("%<thread_limit%> expression must be integral");
6479 remove = true;
6481 else
6483 t = mark_rvalue_use (t);
6484 if (!processing_template_decl)
6486 t = maybe_constant_value (t);
6487 if (TREE_CODE (t) == INTEGER_CST
6488 && tree_int_cst_sgn (t) != 1)
6490 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6491 "%<thread_limit%> value must be positive");
6492 t = integer_one_node;
6494 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6496 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
6498 break;
6500 case OMP_CLAUSE_DEVICE:
6501 t = OMP_CLAUSE_DEVICE_ID (c);
6502 if (t == error_mark_node)
6503 remove = true;
6504 else if (!type_dependent_expression_p (t)
6505 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6507 error ("%<device%> id must be integral");
6508 remove = true;
6510 else
6512 t = mark_rvalue_use (t);
6513 if (!processing_template_decl)
6514 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6515 OMP_CLAUSE_DEVICE_ID (c) = t;
6517 break;
6519 case OMP_CLAUSE_DIST_SCHEDULE:
6520 t = OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c);
6521 if (t == NULL)
6523 else if (t == error_mark_node)
6524 remove = true;
6525 else if (!type_dependent_expression_p (t)
6526 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6528 error ("%<dist_schedule%> chunk size expression must be "
6529 "integral");
6530 remove = true;
6532 else
6534 t = mark_rvalue_use (t);
6535 if (!processing_template_decl)
6536 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6537 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
6539 break;
6541 case OMP_CLAUSE_ALIGNED:
6542 t = OMP_CLAUSE_DECL (c);
6543 if (t == current_class_ptr && ort != C_ORT_OMP_DECLARE_SIMD)
6545 error ("%<this%> allowed in OpenMP only in %<declare simd%>"
6546 " clauses");
6547 remove = true;
6548 break;
6550 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
6552 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
6553 break;
6554 if (DECL_P (t))
6555 error ("%qD is not a variable in %<aligned%> clause", t);
6556 else
6557 error ("%qE is not a variable in %<aligned%> clause", t);
6558 remove = true;
6560 else if (!type_dependent_expression_p (t)
6561 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE
6562 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE
6563 && (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE
6564 || (!POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (t)))
6565 && (TREE_CODE (TREE_TYPE (TREE_TYPE (t)))
6566 != ARRAY_TYPE))))
6568 error_at (OMP_CLAUSE_LOCATION (c),
6569 "%qE in %<aligned%> clause is neither a pointer nor "
6570 "an array nor a reference to pointer or array", t);
6571 remove = true;
6573 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
6575 error ("%qD appears more than once in %<aligned%> clauses", t);
6576 remove = true;
6578 else
6579 bitmap_set_bit (&aligned_head, DECL_UID (t));
6580 t = OMP_CLAUSE_ALIGNED_ALIGNMENT (c);
6581 if (t == error_mark_node)
6582 remove = true;
6583 else if (t == NULL_TREE)
6584 break;
6585 else if (!type_dependent_expression_p (t)
6586 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6588 error ("%<aligned%> clause alignment expression must "
6589 "be integral");
6590 remove = true;
6592 else
6594 t = mark_rvalue_use (t);
6595 if (!processing_template_decl)
6597 t = maybe_constant_value (t);
6598 if (TREE_CODE (t) != INTEGER_CST
6599 || tree_int_cst_sgn (t) != 1)
6601 error ("%<aligned%> clause alignment expression must be "
6602 "positive constant integer expression");
6603 remove = true;
6606 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = t;
6608 break;
6610 case OMP_CLAUSE_DEPEND:
6611 t = OMP_CLAUSE_DECL (c);
6612 if (t == NULL_TREE)
6614 gcc_assert (OMP_CLAUSE_DEPEND_KIND (c)
6615 == OMP_CLAUSE_DEPEND_SOURCE);
6616 break;
6618 if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SINK)
6620 if (cp_finish_omp_clause_depend_sink (c))
6621 remove = true;
6622 break;
6624 if (TREE_CODE (t) == TREE_LIST)
6626 if (handle_omp_array_sections (c, ort))
6627 remove = true;
6628 break;
6630 if (t == error_mark_node)
6631 remove = true;
6632 else if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
6634 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
6635 break;
6636 if (DECL_P (t))
6637 error ("%qD is not a variable in %<depend%> clause", t);
6638 else
6639 error ("%qE is not a variable in %<depend%> clause", t);
6640 remove = true;
6642 else if (t == current_class_ptr)
6644 error ("%<this%> allowed in OpenMP only in %<declare simd%>"
6645 " clauses");
6646 remove = true;
6648 else if (!processing_template_decl
6649 && !cxx_mark_addressable (t))
6650 remove = true;
6651 break;
6653 case OMP_CLAUSE_MAP:
6654 case OMP_CLAUSE_TO:
6655 case OMP_CLAUSE_FROM:
6656 case OMP_CLAUSE__CACHE_:
6657 t = OMP_CLAUSE_DECL (c);
6658 if (TREE_CODE (t) == TREE_LIST)
6660 if (handle_omp_array_sections (c, ort))
6661 remove = true;
6662 else
6664 t = OMP_CLAUSE_DECL (c);
6665 if (TREE_CODE (t) != TREE_LIST
6666 && !type_dependent_expression_p (t)
6667 && !cp_omp_mappable_type (TREE_TYPE (t)))
6669 error_at (OMP_CLAUSE_LOCATION (c),
6670 "array section does not have mappable type "
6671 "in %qs clause",
6672 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6673 remove = true;
6675 while (TREE_CODE (t) == ARRAY_REF)
6676 t = TREE_OPERAND (t, 0);
6677 if (TREE_CODE (t) == COMPONENT_REF
6678 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
6680 while (TREE_CODE (t) == COMPONENT_REF)
6681 t = TREE_OPERAND (t, 0);
6682 if (REFERENCE_REF_P (t))
6683 t = TREE_OPERAND (t, 0);
6684 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
6685 break;
6686 if (bitmap_bit_p (&map_head, DECL_UID (t)))
6688 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
6689 error ("%qD appears more than once in motion"
6690 " clauses", t);
6691 else if (ort == C_ORT_ACC)
6692 error ("%qD appears more than once in data"
6693 " clauses", t);
6694 else
6695 error ("%qD appears more than once in map"
6696 " clauses", t);
6697 remove = true;
6699 else
6701 bitmap_set_bit (&map_head, DECL_UID (t));
6702 bitmap_set_bit (&map_field_head, DECL_UID (t));
6706 break;
6708 if (t == error_mark_node)
6710 remove = true;
6711 break;
6713 if (REFERENCE_REF_P (t)
6714 && TREE_CODE (TREE_OPERAND (t, 0)) == COMPONENT_REF)
6716 t = TREE_OPERAND (t, 0);
6717 OMP_CLAUSE_DECL (c) = t;
6719 if (TREE_CODE (t) == COMPONENT_REF
6720 && (ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP
6721 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE__CACHE_)
6723 if (type_dependent_expression_p (t))
6724 break;
6725 if (TREE_CODE (TREE_OPERAND (t, 1)) == FIELD_DECL
6726 && DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
6728 error_at (OMP_CLAUSE_LOCATION (c),
6729 "bit-field %qE in %qs clause",
6730 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6731 remove = true;
6733 else if (!cp_omp_mappable_type (TREE_TYPE (t)))
6735 error_at (OMP_CLAUSE_LOCATION (c),
6736 "%qE does not have a mappable type in %qs clause",
6737 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6738 remove = true;
6740 while (TREE_CODE (t) == COMPONENT_REF)
6742 if (TREE_TYPE (TREE_OPERAND (t, 0))
6743 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0)))
6744 == UNION_TYPE))
6746 error_at (OMP_CLAUSE_LOCATION (c),
6747 "%qE is a member of a union", t);
6748 remove = true;
6749 break;
6751 t = TREE_OPERAND (t, 0);
6753 if (remove)
6754 break;
6755 if (REFERENCE_REF_P (t))
6756 t = TREE_OPERAND (t, 0);
6757 if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
6759 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
6760 goto handle_map_references;
6763 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
6765 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
6766 break;
6767 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
6768 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
6769 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ALWAYS_POINTER))
6770 break;
6771 if (DECL_P (t))
6772 error ("%qD is not a variable in %qs clause", t,
6773 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6774 else
6775 error ("%qE is not a variable in %qs clause", t,
6776 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6777 remove = true;
6779 else if (VAR_P (t) && CP_DECL_THREAD_LOCAL_P (t))
6781 error ("%qD is threadprivate variable in %qs clause", t,
6782 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6783 remove = true;
6785 else if (ort != C_ORT_ACC && t == current_class_ptr)
6787 error ("%<this%> allowed in OpenMP only in %<declare simd%>"
6788 " clauses");
6789 remove = true;
6790 break;
6792 else if (!processing_template_decl
6793 && TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE
6794 && (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
6795 || (OMP_CLAUSE_MAP_KIND (c)
6796 != GOMP_MAP_FIRSTPRIVATE_POINTER))
6797 && !cxx_mark_addressable (t))
6798 remove = true;
6799 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
6800 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
6801 || (OMP_CLAUSE_MAP_KIND (c)
6802 == GOMP_MAP_FIRSTPRIVATE_POINTER)))
6803 && t == OMP_CLAUSE_DECL (c)
6804 && !type_dependent_expression_p (t)
6805 && !cp_omp_mappable_type ((TREE_CODE (TREE_TYPE (t))
6806 == REFERENCE_TYPE)
6807 ? TREE_TYPE (TREE_TYPE (t))
6808 : TREE_TYPE (t)))
6810 error_at (OMP_CLAUSE_LOCATION (c),
6811 "%qD does not have a mappable type in %qs clause", t,
6812 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6813 remove = true;
6815 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
6816 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FORCE_DEVICEPTR
6817 && !type_dependent_expression_p (t)
6818 && !POINTER_TYPE_P (TREE_TYPE (t)))
6820 error ("%qD is not a pointer variable", t);
6821 remove = true;
6823 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
6824 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
6826 if (bitmap_bit_p (&generic_head, DECL_UID (t))
6827 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
6829 error ("%qD appears more than once in data clauses", t);
6830 remove = true;
6832 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
6834 if (ort == C_ORT_ACC)
6835 error ("%qD appears more than once in data clauses", t);
6836 else
6837 error ("%qD appears both in data and map clauses", t);
6838 remove = true;
6840 else
6841 bitmap_set_bit (&generic_head, DECL_UID (t));
6843 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
6845 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
6846 error ("%qD appears more than once in motion clauses", t);
6847 if (ort == C_ORT_ACC)
6848 error ("%qD appears more than once in data clauses", t);
6849 else
6850 error ("%qD appears more than once in map clauses", t);
6851 remove = true;
6853 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
6854 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
6856 if (ort == C_ORT_ACC)
6857 error ("%qD appears more than once in data clauses", t);
6858 else
6859 error ("%qD appears both in data and map clauses", t);
6860 remove = true;
6862 else
6864 bitmap_set_bit (&map_head, DECL_UID (t));
6865 if (t != OMP_CLAUSE_DECL (c)
6866 && TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPONENT_REF)
6867 bitmap_set_bit (&map_field_head, DECL_UID (t));
6869 handle_map_references:
6870 if (!remove
6871 && !processing_template_decl
6872 && (ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP
6873 && TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c))) == REFERENCE_TYPE)
6875 t = OMP_CLAUSE_DECL (c);
6876 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
6878 OMP_CLAUSE_DECL (c) = build_simple_mem_ref (t);
6879 if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
6880 OMP_CLAUSE_SIZE (c)
6881 = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t)));
6883 else if (OMP_CLAUSE_MAP_KIND (c)
6884 != GOMP_MAP_FIRSTPRIVATE_POINTER
6885 && (OMP_CLAUSE_MAP_KIND (c)
6886 != GOMP_MAP_FIRSTPRIVATE_REFERENCE)
6887 && (OMP_CLAUSE_MAP_KIND (c)
6888 != GOMP_MAP_ALWAYS_POINTER))
6890 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
6891 OMP_CLAUSE_MAP);
6892 if (TREE_CODE (t) == COMPONENT_REF)
6893 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ALWAYS_POINTER);
6894 else
6895 OMP_CLAUSE_SET_MAP_KIND (c2,
6896 GOMP_MAP_FIRSTPRIVATE_REFERENCE);
6897 OMP_CLAUSE_DECL (c2) = t;
6898 OMP_CLAUSE_SIZE (c2) = size_zero_node;
6899 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
6900 OMP_CLAUSE_CHAIN (c) = c2;
6901 OMP_CLAUSE_DECL (c) = build_simple_mem_ref (t);
6902 if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
6903 OMP_CLAUSE_SIZE (c)
6904 = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t)));
6905 c = c2;
6908 break;
6910 case OMP_CLAUSE_TO_DECLARE:
6911 case OMP_CLAUSE_LINK:
6912 t = OMP_CLAUSE_DECL (c);
6913 if (TREE_CODE (t) == FUNCTION_DECL
6914 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
6916 else if (!VAR_P (t))
6918 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
6920 if (TREE_CODE (t) == TEMPLATE_ID_EXPR)
6921 error_at (OMP_CLAUSE_LOCATION (c),
6922 "template %qE in clause %qs", t,
6923 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6924 else if (really_overloaded_fn (t))
6925 error_at (OMP_CLAUSE_LOCATION (c),
6926 "overloaded function name %qE in clause %qs", t,
6927 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6928 else
6929 error_at (OMP_CLAUSE_LOCATION (c),
6930 "%qE is neither a variable nor a function name "
6931 "in clause %qs", t,
6932 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6934 else
6935 error_at (OMP_CLAUSE_LOCATION (c),
6936 "%qE is not a variable in clause %qs", t,
6937 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6938 remove = true;
6940 else if (DECL_THREAD_LOCAL_P (t))
6942 error_at (OMP_CLAUSE_LOCATION (c),
6943 "%qD is threadprivate variable in %qs clause", t,
6944 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6945 remove = true;
6947 else if (!cp_omp_mappable_type (TREE_TYPE (t)))
6949 error_at (OMP_CLAUSE_LOCATION (c),
6950 "%qD does not have a mappable type in %qs clause", t,
6951 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6952 remove = true;
6954 if (remove)
6955 break;
6956 if (bitmap_bit_p (&generic_head, DECL_UID (t)))
6958 error_at (OMP_CLAUSE_LOCATION (c),
6959 "%qE appears more than once on the same "
6960 "%<declare target%> directive", t);
6961 remove = true;
6963 else
6964 bitmap_set_bit (&generic_head, DECL_UID (t));
6965 break;
6967 case OMP_CLAUSE_UNIFORM:
6968 t = OMP_CLAUSE_DECL (c);
6969 if (TREE_CODE (t) != PARM_DECL)
6971 if (processing_template_decl)
6972 break;
6973 if (DECL_P (t))
6974 error ("%qD is not an argument in %<uniform%> clause", t);
6975 else
6976 error ("%qE is not an argument in %<uniform%> clause", t);
6977 remove = true;
6978 break;
6980 /* map_head bitmap is used as uniform_head if declare_simd. */
6981 bitmap_set_bit (&map_head, DECL_UID (t));
6982 goto check_dup_generic;
6984 case OMP_CLAUSE_GRAINSIZE:
6985 t = OMP_CLAUSE_GRAINSIZE_EXPR (c);
6986 if (t == error_mark_node)
6987 remove = true;
6988 else if (!type_dependent_expression_p (t)
6989 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6991 error ("%<grainsize%> expression must be integral");
6992 remove = true;
6994 else
6996 t = mark_rvalue_use (t);
6997 if (!processing_template_decl)
6999 t = maybe_constant_value (t);
7000 if (TREE_CODE (t) == INTEGER_CST
7001 && tree_int_cst_sgn (t) != 1)
7003 warning_at (OMP_CLAUSE_LOCATION (c), 0,
7004 "%<grainsize%> value must be positive");
7005 t = integer_one_node;
7007 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7009 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
7011 break;
7013 case OMP_CLAUSE_PRIORITY:
7014 t = OMP_CLAUSE_PRIORITY_EXPR (c);
7015 if (t == error_mark_node)
7016 remove = true;
7017 else if (!type_dependent_expression_p (t)
7018 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
7020 error ("%<priority%> expression must be integral");
7021 remove = true;
7023 else
7025 t = mark_rvalue_use (t);
7026 if (!processing_template_decl)
7028 t = maybe_constant_value (t);
7029 if (TREE_CODE (t) == INTEGER_CST
7030 && tree_int_cst_sgn (t) == -1)
7032 warning_at (OMP_CLAUSE_LOCATION (c), 0,
7033 "%<priority%> value must be non-negative");
7034 t = integer_one_node;
7036 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7038 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
7040 break;
7042 case OMP_CLAUSE_HINT:
7043 t = OMP_CLAUSE_HINT_EXPR (c);
7044 if (t == error_mark_node)
7045 remove = true;
7046 else if (!type_dependent_expression_p (t)
7047 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
7049 error ("%<num_tasks%> expression must be integral");
7050 remove = true;
7052 else
7054 t = mark_rvalue_use (t);
7055 if (!processing_template_decl)
7057 t = maybe_constant_value (t);
7058 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7060 OMP_CLAUSE_HINT_EXPR (c) = t;
7062 break;
7064 case OMP_CLAUSE_IS_DEVICE_PTR:
7065 case OMP_CLAUSE_USE_DEVICE_PTR:
7066 field_ok = (ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP;
7067 t = OMP_CLAUSE_DECL (c);
7068 if (!type_dependent_expression_p (t))
7070 tree type = TREE_TYPE (t);
7071 if (TREE_CODE (type) != POINTER_TYPE
7072 && TREE_CODE (type) != ARRAY_TYPE
7073 && (TREE_CODE (type) != REFERENCE_TYPE
7074 || (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
7075 && TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE)))
7077 error_at (OMP_CLAUSE_LOCATION (c),
7078 "%qs variable is neither a pointer, nor an array "
7079 "nor reference to pointer or array",
7080 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7081 remove = true;
7084 goto check_dup_generic;
7086 case OMP_CLAUSE_NOWAIT:
7087 case OMP_CLAUSE_DEFAULT:
7088 case OMP_CLAUSE_UNTIED:
7089 case OMP_CLAUSE_COLLAPSE:
7090 case OMP_CLAUSE_MERGEABLE:
7091 case OMP_CLAUSE_PARALLEL:
7092 case OMP_CLAUSE_FOR:
7093 case OMP_CLAUSE_SECTIONS:
7094 case OMP_CLAUSE_TASKGROUP:
7095 case OMP_CLAUSE_PROC_BIND:
7096 case OMP_CLAUSE_NOGROUP:
7097 case OMP_CLAUSE_THREADS:
7098 case OMP_CLAUSE_SIMD:
7099 case OMP_CLAUSE_DEFAULTMAP:
7100 case OMP_CLAUSE_AUTO:
7101 case OMP_CLAUSE_INDEPENDENT:
7102 case OMP_CLAUSE_SEQ:
7103 break;
7105 case OMP_CLAUSE_TILE:
7106 for (tree list = OMP_CLAUSE_TILE_LIST (c); !remove && list;
7107 list = TREE_CHAIN (list))
7109 t = TREE_VALUE (list);
7111 if (t == error_mark_node)
7112 remove = true;
7113 else if (!type_dependent_expression_p (t)
7114 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
7116 error_at (OMP_CLAUSE_LOCATION (c),
7117 "%<tile%> argument needs integral type");
7118 remove = true;
7120 else
7122 t = mark_rvalue_use (t);
7123 if (!processing_template_decl)
7125 /* Zero is used to indicate '*', we permit you
7126 to get there via an ICE of value zero. */
7127 t = maybe_constant_value (t);
7128 if (!tree_fits_shwi_p (t)
7129 || tree_to_shwi (t) < 0)
7131 error_at (OMP_CLAUSE_LOCATION (c),
7132 "%<tile%> argument needs positive "
7133 "integral constant");
7134 remove = true;
7136 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7140 /* Update list item. */
7141 TREE_VALUE (list) = t;
7143 break;
7145 case OMP_CLAUSE_ORDERED:
7146 ordered_seen = true;
7147 break;
7149 case OMP_CLAUSE_INBRANCH:
7150 case OMP_CLAUSE_NOTINBRANCH:
7151 if (branch_seen)
7153 error ("%<inbranch%> clause is incompatible with "
7154 "%<notinbranch%>");
7155 remove = true;
7157 branch_seen = true;
7158 break;
7160 default:
7161 gcc_unreachable ();
7164 if (remove)
7165 *pc = OMP_CLAUSE_CHAIN (c);
7166 else
7167 pc = &OMP_CLAUSE_CHAIN (c);
7170 for (pc = &clauses, c = clauses; c ; c = *pc)
7172 enum omp_clause_code c_kind = OMP_CLAUSE_CODE (c);
7173 bool remove = false;
7174 bool need_complete_type = false;
7175 bool need_default_ctor = false;
7176 bool need_copy_ctor = false;
7177 bool need_copy_assignment = false;
7178 bool need_implicitly_determined = false;
7179 bool need_dtor = false;
7180 tree type, inner_type;
7182 switch (c_kind)
7184 case OMP_CLAUSE_SHARED:
7185 need_implicitly_determined = true;
7186 break;
7187 case OMP_CLAUSE_PRIVATE:
7188 need_complete_type = true;
7189 need_default_ctor = true;
7190 need_dtor = true;
7191 need_implicitly_determined = true;
7192 break;
7193 case OMP_CLAUSE_FIRSTPRIVATE:
7194 need_complete_type = true;
7195 need_copy_ctor = true;
7196 need_dtor = true;
7197 need_implicitly_determined = true;
7198 break;
7199 case OMP_CLAUSE_LASTPRIVATE:
7200 need_complete_type = true;
7201 need_copy_assignment = true;
7202 need_implicitly_determined = true;
7203 break;
7204 case OMP_CLAUSE_REDUCTION:
7205 need_implicitly_determined = true;
7206 break;
7207 case OMP_CLAUSE_LINEAR:
7208 if (ort != C_ORT_OMP_DECLARE_SIMD)
7209 need_implicitly_determined = true;
7210 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c)
7211 && !bitmap_bit_p (&map_head,
7212 DECL_UID (OMP_CLAUSE_LINEAR_STEP (c))))
7214 error_at (OMP_CLAUSE_LOCATION (c),
7215 "%<linear%> clause step is a parameter %qD not "
7216 "specified in %<uniform%> clause",
7217 OMP_CLAUSE_LINEAR_STEP (c));
7218 *pc = OMP_CLAUSE_CHAIN (c);
7219 continue;
7221 break;
7222 case OMP_CLAUSE_COPYPRIVATE:
7223 need_copy_assignment = true;
7224 break;
7225 case OMP_CLAUSE_COPYIN:
7226 need_copy_assignment = true;
7227 break;
7228 case OMP_CLAUSE_SIMDLEN:
7229 if (safelen
7230 && !processing_template_decl
7231 && tree_int_cst_lt (OMP_CLAUSE_SAFELEN_EXPR (safelen),
7232 OMP_CLAUSE_SIMDLEN_EXPR (c)))
7234 error_at (OMP_CLAUSE_LOCATION (c),
7235 "%<simdlen%> clause value is bigger than "
7236 "%<safelen%> clause value");
7237 OMP_CLAUSE_SIMDLEN_EXPR (c)
7238 = OMP_CLAUSE_SAFELEN_EXPR (safelen);
7240 pc = &OMP_CLAUSE_CHAIN (c);
7241 continue;
7242 case OMP_CLAUSE_SCHEDULE:
7243 if (ordered_seen
7244 && (OMP_CLAUSE_SCHEDULE_KIND (c)
7245 & OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
7247 error_at (OMP_CLAUSE_LOCATION (c),
7248 "%<nonmonotonic%> schedule modifier specified "
7249 "together with %<ordered%> clause");
7250 OMP_CLAUSE_SCHEDULE_KIND (c)
7251 = (enum omp_clause_schedule_kind)
7252 (OMP_CLAUSE_SCHEDULE_KIND (c)
7253 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
7255 pc = &OMP_CLAUSE_CHAIN (c);
7256 continue;
7257 case OMP_CLAUSE_NOWAIT:
7258 if (copyprivate_seen)
7260 error_at (OMP_CLAUSE_LOCATION (c),
7261 "%<nowait%> clause must not be used together "
7262 "with %<copyprivate%>");
7263 *pc = OMP_CLAUSE_CHAIN (c);
7264 continue;
7266 /* FALLTHRU */
7267 default:
7268 pc = &OMP_CLAUSE_CHAIN (c);
7269 continue;
7272 t = OMP_CLAUSE_DECL (c);
7273 if (processing_template_decl
7274 && !VAR_P (t) && TREE_CODE (t) != PARM_DECL)
7276 pc = &OMP_CLAUSE_CHAIN (c);
7277 continue;
7280 switch (c_kind)
7282 case OMP_CLAUSE_LASTPRIVATE:
7283 if (!bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
7285 need_default_ctor = true;
7286 need_dtor = true;
7288 break;
7290 case OMP_CLAUSE_REDUCTION:
7291 if (finish_omp_reduction_clause (c, &need_default_ctor,
7292 &need_dtor))
7293 remove = true;
7294 else
7295 t = OMP_CLAUSE_DECL (c);
7296 break;
7298 case OMP_CLAUSE_COPYIN:
7299 if (!VAR_P (t) || !CP_DECL_THREAD_LOCAL_P (t))
7301 error ("%qE must be %<threadprivate%> for %<copyin%>", t);
7302 remove = true;
7304 break;
7306 default:
7307 break;
7310 if (need_complete_type || need_copy_assignment)
7312 t = require_complete_type (t);
7313 if (t == error_mark_node)
7314 remove = true;
7315 else if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE
7316 && !complete_type_or_else (TREE_TYPE (TREE_TYPE (t)), t))
7317 remove = true;
7319 if (need_implicitly_determined)
7321 const char *share_name = NULL;
7323 if (VAR_P (t) && CP_DECL_THREAD_LOCAL_P (t))
7324 share_name = "threadprivate";
7325 else switch (cxx_omp_predetermined_sharing (t))
7327 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
7328 break;
7329 case OMP_CLAUSE_DEFAULT_SHARED:
7330 /* const vars may be specified in firstprivate clause. */
7331 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
7332 && cxx_omp_const_qual_no_mutable (t))
7333 break;
7334 share_name = "shared";
7335 break;
7336 case OMP_CLAUSE_DEFAULT_PRIVATE:
7337 share_name = "private";
7338 break;
7339 default:
7340 gcc_unreachable ();
7342 if (share_name)
7344 error ("%qE is predetermined %qs for %qs",
7345 omp_clause_printable_decl (t), share_name,
7346 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7347 remove = true;
7351 /* We're interested in the base element, not arrays. */
7352 inner_type = type = TREE_TYPE (t);
7353 if ((need_complete_type
7354 || need_copy_assignment
7355 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
7356 && TREE_CODE (inner_type) == REFERENCE_TYPE)
7357 inner_type = TREE_TYPE (inner_type);
7358 while (TREE_CODE (inner_type) == ARRAY_TYPE)
7359 inner_type = TREE_TYPE (inner_type);
7361 /* Check for special function availability by building a call to one.
7362 Save the results, because later we won't be in the right context
7363 for making these queries. */
7364 if (CLASS_TYPE_P (inner_type)
7365 && COMPLETE_TYPE_P (inner_type)
7366 && (need_default_ctor || need_copy_ctor
7367 || need_copy_assignment || need_dtor)
7368 && !type_dependent_expression_p (t)
7369 && cxx_omp_create_clause_info (c, inner_type, need_default_ctor,
7370 need_copy_ctor, need_copy_assignment,
7371 need_dtor))
7372 remove = true;
7374 if (!remove
7375 && c_kind == OMP_CLAUSE_SHARED
7376 && processing_template_decl)
7378 t = omp_clause_decl_field (OMP_CLAUSE_DECL (c));
7379 if (t)
7380 OMP_CLAUSE_DECL (c) = t;
7383 if (remove)
7384 *pc = OMP_CLAUSE_CHAIN (c);
7385 else
7386 pc = &OMP_CLAUSE_CHAIN (c);
7389 bitmap_obstack_release (NULL);
7390 return clauses;
7393 /* Start processing OpenMP clauses that can include any
7394 privatization clauses for non-static data members. */
7396 tree
7397 push_omp_privatization_clauses (bool ignore_next)
7399 if (omp_private_member_ignore_next)
7401 omp_private_member_ignore_next = ignore_next;
7402 return NULL_TREE;
7404 omp_private_member_ignore_next = ignore_next;
7405 if (omp_private_member_map)
7406 omp_private_member_vec.safe_push (error_mark_node);
7407 return push_stmt_list ();
7410 /* Revert remapping of any non-static data members since
7411 the last push_omp_privatization_clauses () call. */
7413 void
7414 pop_omp_privatization_clauses (tree stmt)
7416 if (stmt == NULL_TREE)
7417 return;
7418 stmt = pop_stmt_list (stmt);
7419 if (omp_private_member_map)
7421 while (!omp_private_member_vec.is_empty ())
7423 tree t = omp_private_member_vec.pop ();
7424 if (t == error_mark_node)
7426 add_stmt (stmt);
7427 return;
7429 bool no_decl_expr = t == integer_zero_node;
7430 if (no_decl_expr)
7431 t = omp_private_member_vec.pop ();
7432 tree *v = omp_private_member_map->get (t);
7433 gcc_assert (v);
7434 if (!no_decl_expr)
7435 add_decl_expr (*v);
7436 omp_private_member_map->remove (t);
7438 delete omp_private_member_map;
7439 omp_private_member_map = NULL;
7441 add_stmt (stmt);
7444 /* Remember OpenMP privatization clauses mapping and clear it.
7445 Used for lambdas. */
7447 void
7448 save_omp_privatization_clauses (vec<tree> &save)
7450 save = vNULL;
7451 if (omp_private_member_ignore_next)
7452 save.safe_push (integer_one_node);
7453 omp_private_member_ignore_next = false;
7454 if (!omp_private_member_map)
7455 return;
7457 while (!omp_private_member_vec.is_empty ())
7459 tree t = omp_private_member_vec.pop ();
7460 if (t == error_mark_node)
7462 save.safe_push (t);
7463 continue;
7465 tree n = t;
7466 if (t == integer_zero_node)
7467 t = omp_private_member_vec.pop ();
7468 tree *v = omp_private_member_map->get (t);
7469 gcc_assert (v);
7470 save.safe_push (*v);
7471 save.safe_push (t);
7472 if (n != t)
7473 save.safe_push (n);
7475 delete omp_private_member_map;
7476 omp_private_member_map = NULL;
7479 /* Restore OpenMP privatization clauses mapping saved by the
7480 above function. */
7482 void
7483 restore_omp_privatization_clauses (vec<tree> &save)
7485 gcc_assert (omp_private_member_vec.is_empty ());
7486 omp_private_member_ignore_next = false;
7487 if (save.is_empty ())
7488 return;
7489 if (save.length () == 1 && save[0] == integer_one_node)
7491 omp_private_member_ignore_next = true;
7492 save.release ();
7493 return;
7496 omp_private_member_map = new hash_map <tree, tree>;
7497 while (!save.is_empty ())
7499 tree t = save.pop ();
7500 tree n = t;
7501 if (t != error_mark_node)
7503 if (t == integer_one_node)
7505 omp_private_member_ignore_next = true;
7506 gcc_assert (save.is_empty ());
7507 break;
7509 if (t == integer_zero_node)
7510 t = save.pop ();
7511 tree &v = omp_private_member_map->get_or_insert (t);
7512 v = save.pop ();
7514 omp_private_member_vec.safe_push (t);
7515 if (n != t)
7516 omp_private_member_vec.safe_push (n);
7518 save.release ();
7521 /* For all variables in the tree_list VARS, mark them as thread local. */
7523 void
7524 finish_omp_threadprivate (tree vars)
7526 tree t;
7528 /* Mark every variable in VARS to be assigned thread local storage. */
7529 for (t = vars; t; t = TREE_CHAIN (t))
7531 tree v = TREE_PURPOSE (t);
7533 if (error_operand_p (v))
7535 else if (!VAR_P (v))
7536 error ("%<threadprivate%> %qD is not file, namespace "
7537 "or block scope variable", v);
7538 /* If V had already been marked threadprivate, it doesn't matter
7539 whether it had been used prior to this point. */
7540 else if (TREE_USED (v)
7541 && (DECL_LANG_SPECIFIC (v) == NULL
7542 || !CP_DECL_THREADPRIVATE_P (v)))
7543 error ("%qE declared %<threadprivate%> after first use", v);
7544 else if (! TREE_STATIC (v) && ! DECL_EXTERNAL (v))
7545 error ("automatic variable %qE cannot be %<threadprivate%>", v);
7546 else if (! COMPLETE_TYPE_P (complete_type (TREE_TYPE (v))))
7547 error ("%<threadprivate%> %qE has incomplete type", v);
7548 else if (TREE_STATIC (v) && TYPE_P (CP_DECL_CONTEXT (v))
7549 && CP_DECL_CONTEXT (v) != current_class_type)
7550 error ("%<threadprivate%> %qE directive not "
7551 "in %qT definition", v, CP_DECL_CONTEXT (v));
7552 else
7554 /* Allocate a LANG_SPECIFIC structure for V, if needed. */
7555 if (DECL_LANG_SPECIFIC (v) == NULL)
7557 retrofit_lang_decl (v);
7559 /* Make sure that DECL_DISCRIMINATOR_P continues to be true
7560 after the allocation of the lang_decl structure. */
7561 if (DECL_DISCRIMINATOR_P (v))
7562 DECL_LANG_SPECIFIC (v)->u.base.u2sel = 1;
7565 if (! CP_DECL_THREAD_LOCAL_P (v))
7567 CP_DECL_THREAD_LOCAL_P (v) = true;
7568 set_decl_tls_model (v, decl_default_tls_model (v));
7569 /* If rtl has been already set for this var, call
7570 make_decl_rtl once again, so that encode_section_info
7571 has a chance to look at the new decl flags. */
7572 if (DECL_RTL_SET_P (v))
7573 make_decl_rtl (v);
7575 CP_DECL_THREADPRIVATE_P (v) = 1;
7580 /* Build an OpenMP structured block. */
7582 tree
7583 begin_omp_structured_block (void)
7585 return do_pushlevel (sk_omp);
7588 tree
7589 finish_omp_structured_block (tree block)
7591 return do_poplevel (block);
7594 /* Similarly, except force the retention of the BLOCK. */
7596 tree
7597 begin_omp_parallel (void)
7599 keep_next_level (true);
7600 return begin_omp_structured_block ();
7603 /* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
7604 statement. */
7606 tree
7607 finish_oacc_data (tree clauses, tree block)
7609 tree stmt;
7611 block = finish_omp_structured_block (block);
7613 stmt = make_node (OACC_DATA);
7614 TREE_TYPE (stmt) = void_type_node;
7615 OACC_DATA_CLAUSES (stmt) = clauses;
7616 OACC_DATA_BODY (stmt) = block;
7618 return add_stmt (stmt);
7621 /* Generate OACC_HOST_DATA, with CLAUSES and BLOCK as its compound
7622 statement. */
7624 tree
7625 finish_oacc_host_data (tree clauses, tree block)
7627 tree stmt;
7629 block = finish_omp_structured_block (block);
7631 stmt = make_node (OACC_HOST_DATA);
7632 TREE_TYPE (stmt) = void_type_node;
7633 OACC_HOST_DATA_CLAUSES (stmt) = clauses;
7634 OACC_HOST_DATA_BODY (stmt) = block;
7636 return add_stmt (stmt);
7639 /* Generate OMP construct CODE, with BODY and CLAUSES as its compound
7640 statement. */
7642 tree
7643 finish_omp_construct (enum tree_code code, tree body, tree clauses)
7645 body = finish_omp_structured_block (body);
7647 tree stmt = make_node (code);
7648 TREE_TYPE (stmt) = void_type_node;
7649 OMP_BODY (stmt) = body;
7650 OMP_CLAUSES (stmt) = clauses;
7652 return add_stmt (stmt);
7655 tree
7656 finish_omp_parallel (tree clauses, tree body)
7658 tree stmt;
7660 body = finish_omp_structured_block (body);
7662 stmt = make_node (OMP_PARALLEL);
7663 TREE_TYPE (stmt) = void_type_node;
7664 OMP_PARALLEL_CLAUSES (stmt) = clauses;
7665 OMP_PARALLEL_BODY (stmt) = body;
7667 return add_stmt (stmt);
7670 tree
7671 begin_omp_task (void)
7673 keep_next_level (true);
7674 return begin_omp_structured_block ();
7677 tree
7678 finish_omp_task (tree clauses, tree body)
7680 tree stmt;
7682 body = finish_omp_structured_block (body);
7684 stmt = make_node (OMP_TASK);
7685 TREE_TYPE (stmt) = void_type_node;
7686 OMP_TASK_CLAUSES (stmt) = clauses;
7687 OMP_TASK_BODY (stmt) = body;
7689 return add_stmt (stmt);
7692 /* Helper function for finish_omp_for. Convert Ith random access iterator
7693 into integral iterator. Return FALSE if successful. */
7695 static bool
7696 handle_omp_for_class_iterator (int i, location_t locus, enum tree_code code,
7697 tree declv, tree orig_declv, tree initv,
7698 tree condv, tree incrv, tree *body,
7699 tree *pre_body, tree &clauses, tree *lastp,
7700 int collapse, int ordered)
7702 tree diff, iter_init, iter_incr = NULL, last;
7703 tree incr_var = NULL, orig_pre_body, orig_body, c;
7704 tree decl = TREE_VEC_ELT (declv, i);
7705 tree init = TREE_VEC_ELT (initv, i);
7706 tree cond = TREE_VEC_ELT (condv, i);
7707 tree incr = TREE_VEC_ELT (incrv, i);
7708 tree iter = decl;
7709 location_t elocus = locus;
7711 if (init && EXPR_HAS_LOCATION (init))
7712 elocus = EXPR_LOCATION (init);
7714 cond = cp_fully_fold (cond);
7715 switch (TREE_CODE (cond))
7717 case GT_EXPR:
7718 case GE_EXPR:
7719 case LT_EXPR:
7720 case LE_EXPR:
7721 case NE_EXPR:
7722 if (TREE_OPERAND (cond, 1) == iter)
7723 cond = build2 (swap_tree_comparison (TREE_CODE (cond)),
7724 TREE_TYPE (cond), iter, TREE_OPERAND (cond, 0));
7725 if (TREE_OPERAND (cond, 0) != iter)
7726 cond = error_mark_node;
7727 else
7729 tree tem = build_x_binary_op (EXPR_LOCATION (cond),
7730 TREE_CODE (cond),
7731 iter, ERROR_MARK,
7732 TREE_OPERAND (cond, 1), ERROR_MARK,
7733 NULL, tf_warning_or_error);
7734 if (error_operand_p (tem))
7735 return true;
7737 break;
7738 default:
7739 cond = error_mark_node;
7740 break;
7742 if (cond == error_mark_node)
7744 error_at (elocus, "invalid controlling predicate");
7745 return true;
7747 diff = build_x_binary_op (elocus, MINUS_EXPR, TREE_OPERAND (cond, 1),
7748 ERROR_MARK, iter, ERROR_MARK, NULL,
7749 tf_warning_or_error);
7750 diff = cp_fully_fold (diff);
7751 if (error_operand_p (diff))
7752 return true;
7753 if (TREE_CODE (TREE_TYPE (diff)) != INTEGER_TYPE)
7755 error_at (elocus, "difference between %qE and %qD does not have integer type",
7756 TREE_OPERAND (cond, 1), iter);
7757 return true;
7759 if (!c_omp_check_loop_iv_exprs (locus, orig_declv,
7760 TREE_VEC_ELT (declv, i), NULL_TREE,
7761 cond, cp_walk_subtrees))
7762 return true;
7764 switch (TREE_CODE (incr))
7766 case PREINCREMENT_EXPR:
7767 case PREDECREMENT_EXPR:
7768 case POSTINCREMENT_EXPR:
7769 case POSTDECREMENT_EXPR:
7770 if (TREE_OPERAND (incr, 0) != iter)
7772 incr = error_mark_node;
7773 break;
7775 iter_incr = build_x_unary_op (EXPR_LOCATION (incr),
7776 TREE_CODE (incr), iter,
7777 tf_warning_or_error);
7778 if (error_operand_p (iter_incr))
7779 return true;
7780 else if (TREE_CODE (incr) == PREINCREMENT_EXPR
7781 || TREE_CODE (incr) == POSTINCREMENT_EXPR)
7782 incr = integer_one_node;
7783 else
7784 incr = integer_minus_one_node;
7785 break;
7786 case MODIFY_EXPR:
7787 if (TREE_OPERAND (incr, 0) != iter)
7788 incr = error_mark_node;
7789 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
7790 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
7792 tree rhs = TREE_OPERAND (incr, 1);
7793 if (TREE_OPERAND (rhs, 0) == iter)
7795 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (rhs, 1)))
7796 != INTEGER_TYPE)
7797 incr = error_mark_node;
7798 else
7800 iter_incr = build_x_modify_expr (EXPR_LOCATION (rhs),
7801 iter, TREE_CODE (rhs),
7802 TREE_OPERAND (rhs, 1),
7803 tf_warning_or_error);
7804 if (error_operand_p (iter_incr))
7805 return true;
7806 incr = TREE_OPERAND (rhs, 1);
7807 incr = cp_convert (TREE_TYPE (diff), incr,
7808 tf_warning_or_error);
7809 if (TREE_CODE (rhs) == MINUS_EXPR)
7811 incr = build1 (NEGATE_EXPR, TREE_TYPE (diff), incr);
7812 incr = fold_simple (incr);
7814 if (TREE_CODE (incr) != INTEGER_CST
7815 && (TREE_CODE (incr) != NOP_EXPR
7816 || (TREE_CODE (TREE_OPERAND (incr, 0))
7817 != INTEGER_CST)))
7818 iter_incr = NULL;
7821 else if (TREE_OPERAND (rhs, 1) == iter)
7823 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (rhs, 0))) != INTEGER_TYPE
7824 || TREE_CODE (rhs) != PLUS_EXPR)
7825 incr = error_mark_node;
7826 else
7828 iter_incr = build_x_binary_op (EXPR_LOCATION (rhs),
7829 PLUS_EXPR,
7830 TREE_OPERAND (rhs, 0),
7831 ERROR_MARK, iter,
7832 ERROR_MARK, NULL,
7833 tf_warning_or_error);
7834 if (error_operand_p (iter_incr))
7835 return true;
7836 iter_incr = build_x_modify_expr (EXPR_LOCATION (rhs),
7837 iter, NOP_EXPR,
7838 iter_incr,
7839 tf_warning_or_error);
7840 if (error_operand_p (iter_incr))
7841 return true;
7842 incr = TREE_OPERAND (rhs, 0);
7843 iter_incr = NULL;
7846 else
7847 incr = error_mark_node;
7849 else
7850 incr = error_mark_node;
7851 break;
7852 default:
7853 incr = error_mark_node;
7854 break;
7857 if (incr == error_mark_node)
7859 error_at (elocus, "invalid increment expression");
7860 return true;
7863 incr = cp_convert (TREE_TYPE (diff), incr, tf_warning_or_error);
7864 bool taskloop_iv_seen = false;
7865 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
7866 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
7867 && OMP_CLAUSE_DECL (c) == iter)
7869 if (code == OMP_TASKLOOP)
7871 taskloop_iv_seen = true;
7872 OMP_CLAUSE_LASTPRIVATE_TASKLOOP_IV (c) = 1;
7874 break;
7876 else if (code == OMP_TASKLOOP
7877 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
7878 && OMP_CLAUSE_DECL (c) == iter)
7880 taskloop_iv_seen = true;
7881 OMP_CLAUSE_PRIVATE_TASKLOOP_IV (c) = 1;
7884 decl = create_temporary_var (TREE_TYPE (diff));
7885 pushdecl (decl);
7886 add_decl_expr (decl);
7887 last = create_temporary_var (TREE_TYPE (diff));
7888 pushdecl (last);
7889 add_decl_expr (last);
7890 if (c && iter_incr == NULL && TREE_CODE (incr) != INTEGER_CST
7891 && (!ordered || (i < collapse && collapse > 1)))
7893 incr_var = create_temporary_var (TREE_TYPE (diff));
7894 pushdecl (incr_var);
7895 add_decl_expr (incr_var);
7897 gcc_assert (stmts_are_full_exprs_p ());
7898 tree diffvar = NULL_TREE;
7899 if (code == OMP_TASKLOOP)
7901 if (!taskloop_iv_seen)
7903 tree ivc = build_omp_clause (locus, OMP_CLAUSE_FIRSTPRIVATE);
7904 OMP_CLAUSE_DECL (ivc) = iter;
7905 cxx_omp_finish_clause (ivc, NULL);
7906 OMP_CLAUSE_CHAIN (ivc) = clauses;
7907 clauses = ivc;
7909 tree lvc = build_omp_clause (locus, OMP_CLAUSE_FIRSTPRIVATE);
7910 OMP_CLAUSE_DECL (lvc) = last;
7911 OMP_CLAUSE_CHAIN (lvc) = clauses;
7912 clauses = lvc;
7913 diffvar = create_temporary_var (TREE_TYPE (diff));
7914 pushdecl (diffvar);
7915 add_decl_expr (diffvar);
7918 orig_pre_body = *pre_body;
7919 *pre_body = push_stmt_list ();
7920 if (orig_pre_body)
7921 add_stmt (orig_pre_body);
7922 if (init != NULL)
7923 finish_expr_stmt (build_x_modify_expr (elocus,
7924 iter, NOP_EXPR, init,
7925 tf_warning_or_error));
7926 init = build_int_cst (TREE_TYPE (diff), 0);
7927 if (c && iter_incr == NULL
7928 && (!ordered || (i < collapse && collapse > 1)))
7930 if (incr_var)
7932 finish_expr_stmt (build_x_modify_expr (elocus,
7933 incr_var, NOP_EXPR,
7934 incr, tf_warning_or_error));
7935 incr = incr_var;
7937 iter_incr = build_x_modify_expr (elocus,
7938 iter, PLUS_EXPR, incr,
7939 tf_warning_or_error);
7941 if (c && ordered && i < collapse && collapse > 1)
7942 iter_incr = incr;
7943 finish_expr_stmt (build_x_modify_expr (elocus,
7944 last, NOP_EXPR, init,
7945 tf_warning_or_error));
7946 if (diffvar)
7948 finish_expr_stmt (build_x_modify_expr (elocus,
7949 diffvar, NOP_EXPR,
7950 diff, tf_warning_or_error));
7951 diff = diffvar;
7953 *pre_body = pop_stmt_list (*pre_body);
7955 cond = cp_build_binary_op (elocus,
7956 TREE_CODE (cond), decl, diff,
7957 tf_warning_or_error);
7958 incr = build_modify_expr (elocus, decl, NULL_TREE, PLUS_EXPR,
7959 elocus, incr, NULL_TREE);
7961 orig_body = *body;
7962 *body = push_stmt_list ();
7963 iter_init = build2 (MINUS_EXPR, TREE_TYPE (diff), decl, last);
7964 iter_init = build_x_modify_expr (elocus,
7965 iter, PLUS_EXPR, iter_init,
7966 tf_warning_or_error);
7967 if (iter_init != error_mark_node)
7968 iter_init = build1 (NOP_EXPR, void_type_node, iter_init);
7969 finish_expr_stmt (iter_init);
7970 finish_expr_stmt (build_x_modify_expr (elocus,
7971 last, NOP_EXPR, decl,
7972 tf_warning_or_error));
7973 add_stmt (orig_body);
7974 *body = pop_stmt_list (*body);
7976 if (c)
7978 OMP_CLAUSE_LASTPRIVATE_STMT (c) = push_stmt_list ();
7979 if (!ordered)
7980 finish_expr_stmt (iter_incr);
7981 else
7983 iter_init = decl;
7984 if (i < collapse && collapse > 1 && !error_operand_p (iter_incr))
7985 iter_init = build2 (PLUS_EXPR, TREE_TYPE (diff),
7986 iter_init, iter_incr);
7987 iter_init = build2 (MINUS_EXPR, TREE_TYPE (diff), iter_init, last);
7988 iter_init = build_x_modify_expr (elocus,
7989 iter, PLUS_EXPR, iter_init,
7990 tf_warning_or_error);
7991 if (iter_init != error_mark_node)
7992 iter_init = build1 (NOP_EXPR, void_type_node, iter_init);
7993 finish_expr_stmt (iter_init);
7995 OMP_CLAUSE_LASTPRIVATE_STMT (c)
7996 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (c));
7999 TREE_VEC_ELT (declv, i) = decl;
8000 TREE_VEC_ELT (initv, i) = init;
8001 TREE_VEC_ELT (condv, i) = cond;
8002 TREE_VEC_ELT (incrv, i) = incr;
8003 *lastp = last;
8005 return false;
8008 /* Build and validate an OMP_FOR statement. CLAUSES, BODY, COND, INCR
8009 are directly for their associated operands in the statement. DECL
8010 and INIT are a combo; if DECL is NULL then INIT ought to be a
8011 MODIFY_EXPR, and the DECL should be extracted. PRE_BODY are
8012 optional statements that need to go before the loop into its
8013 sk_omp scope. */
8015 tree
8016 finish_omp_for (location_t locus, enum tree_code code, tree declv,
8017 tree orig_declv, tree initv, tree condv, tree incrv,
8018 tree body, tree pre_body, vec<tree> *orig_inits, tree clauses)
8020 tree omp_for = NULL, orig_incr = NULL;
8021 tree decl = NULL, init, cond, incr;
8022 tree last = NULL_TREE;
8023 location_t elocus;
8024 int i;
8025 int collapse = 1;
8026 int ordered = 0;
8028 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (initv));
8029 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (condv));
8030 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (incrv));
8031 if (TREE_VEC_LENGTH (declv) > 1)
8033 tree c;
8035 c = omp_find_clause (clauses, OMP_CLAUSE_TILE);
8036 if (c)
8037 collapse = list_length (OMP_CLAUSE_TILE_LIST (c));
8038 else
8040 c = omp_find_clause (clauses, OMP_CLAUSE_COLLAPSE);
8041 if (c)
8042 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (c));
8043 if (collapse != TREE_VEC_LENGTH (declv))
8044 ordered = TREE_VEC_LENGTH (declv);
8047 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
8049 decl = TREE_VEC_ELT (declv, i);
8050 init = TREE_VEC_ELT (initv, i);
8051 cond = TREE_VEC_ELT (condv, i);
8052 incr = TREE_VEC_ELT (incrv, i);
8053 elocus = locus;
8055 if (decl == NULL)
8057 if (init != NULL)
8058 switch (TREE_CODE (init))
8060 case MODIFY_EXPR:
8061 decl = TREE_OPERAND (init, 0);
8062 init = TREE_OPERAND (init, 1);
8063 break;
8064 case MODOP_EXPR:
8065 if (TREE_CODE (TREE_OPERAND (init, 1)) == NOP_EXPR)
8067 decl = TREE_OPERAND (init, 0);
8068 init = TREE_OPERAND (init, 2);
8070 break;
8071 default:
8072 break;
8075 if (decl == NULL)
8077 error_at (locus,
8078 "expected iteration declaration or initialization");
8079 return NULL;
8083 if (init && EXPR_HAS_LOCATION (init))
8084 elocus = EXPR_LOCATION (init);
8086 if (cond == NULL)
8088 error_at (elocus, "missing controlling predicate");
8089 return NULL;
8092 if (incr == NULL)
8094 error_at (elocus, "missing increment expression");
8095 return NULL;
8098 TREE_VEC_ELT (declv, i) = decl;
8099 TREE_VEC_ELT (initv, i) = init;
8102 if (orig_inits)
8104 bool fail = false;
8105 tree orig_init;
8106 FOR_EACH_VEC_ELT (*orig_inits, i, orig_init)
8107 if (orig_init
8108 && !c_omp_check_loop_iv_exprs (locus, declv,
8109 TREE_VEC_ELT (declv, i), orig_init,
8110 NULL_TREE, cp_walk_subtrees))
8111 fail = true;
8112 if (fail)
8113 return NULL;
8116 if (dependent_omp_for_p (declv, initv, condv, incrv))
8118 tree stmt;
8120 stmt = make_node (code);
8122 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
8124 /* This is really just a place-holder. We'll be decomposing this
8125 again and going through the cp_build_modify_expr path below when
8126 we instantiate the thing. */
8127 TREE_VEC_ELT (initv, i)
8128 = build2 (MODIFY_EXPR, void_type_node, TREE_VEC_ELT (declv, i),
8129 TREE_VEC_ELT (initv, i));
8132 TREE_TYPE (stmt) = void_type_node;
8133 OMP_FOR_INIT (stmt) = initv;
8134 OMP_FOR_COND (stmt) = condv;
8135 OMP_FOR_INCR (stmt) = incrv;
8136 OMP_FOR_BODY (stmt) = body;
8137 OMP_FOR_PRE_BODY (stmt) = pre_body;
8138 OMP_FOR_CLAUSES (stmt) = clauses;
8140 SET_EXPR_LOCATION (stmt, locus);
8141 return add_stmt (stmt);
8144 if (!orig_declv)
8145 orig_declv = copy_node (declv);
8147 if (processing_template_decl)
8148 orig_incr = make_tree_vec (TREE_VEC_LENGTH (incrv));
8150 for (i = 0; i < TREE_VEC_LENGTH (declv); )
8152 decl = TREE_VEC_ELT (declv, i);
8153 init = TREE_VEC_ELT (initv, i);
8154 cond = TREE_VEC_ELT (condv, i);
8155 incr = TREE_VEC_ELT (incrv, i);
8156 if (orig_incr)
8157 TREE_VEC_ELT (orig_incr, i) = incr;
8158 elocus = locus;
8160 if (init && EXPR_HAS_LOCATION (init))
8161 elocus = EXPR_LOCATION (init);
8163 if (!DECL_P (decl))
8165 error_at (elocus, "expected iteration declaration or initialization");
8166 return NULL;
8169 if (incr && TREE_CODE (incr) == MODOP_EXPR)
8171 if (orig_incr)
8172 TREE_VEC_ELT (orig_incr, i) = incr;
8173 incr = cp_build_modify_expr (elocus, TREE_OPERAND (incr, 0),
8174 TREE_CODE (TREE_OPERAND (incr, 1)),
8175 TREE_OPERAND (incr, 2),
8176 tf_warning_or_error);
8179 if (CLASS_TYPE_P (TREE_TYPE (decl)))
8181 if (code == OMP_SIMD)
8183 error_at (elocus, "%<#pragma omp simd%> used with class "
8184 "iteration variable %qE", decl);
8185 return NULL;
8187 if (handle_omp_for_class_iterator (i, locus, code, declv, orig_declv,
8188 initv, condv, incrv, &body,
8189 &pre_body, clauses, &last,
8190 collapse, ordered))
8191 return NULL;
8192 continue;
8195 if (!INTEGRAL_TYPE_P (TREE_TYPE (decl))
8196 && !TYPE_PTR_P (TREE_TYPE (decl)))
8198 error_at (elocus, "invalid type for iteration variable %qE", decl);
8199 return NULL;
8202 if (!processing_template_decl)
8204 init = fold_build_cleanup_point_expr (TREE_TYPE (init), init);
8205 init = cp_build_modify_expr (elocus, decl, NOP_EXPR, init,
8206 tf_warning_or_error);
8208 else
8209 init = build2 (MODIFY_EXPR, void_type_node, decl, init);
8210 if (cond
8211 && TREE_SIDE_EFFECTS (cond)
8212 && COMPARISON_CLASS_P (cond)
8213 && !processing_template_decl)
8215 tree t = TREE_OPERAND (cond, 0);
8216 if (TREE_SIDE_EFFECTS (t)
8217 && t != decl
8218 && (TREE_CODE (t) != NOP_EXPR
8219 || TREE_OPERAND (t, 0) != decl))
8220 TREE_OPERAND (cond, 0)
8221 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
8223 t = TREE_OPERAND (cond, 1);
8224 if (TREE_SIDE_EFFECTS (t)
8225 && t != decl
8226 && (TREE_CODE (t) != NOP_EXPR
8227 || TREE_OPERAND (t, 0) != decl))
8228 TREE_OPERAND (cond, 1)
8229 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
8231 if (decl == error_mark_node || init == error_mark_node)
8232 return NULL;
8234 TREE_VEC_ELT (declv, i) = decl;
8235 TREE_VEC_ELT (initv, i) = init;
8236 TREE_VEC_ELT (condv, i) = cond;
8237 TREE_VEC_ELT (incrv, i) = incr;
8238 i++;
8241 if (IS_EMPTY_STMT (pre_body))
8242 pre_body = NULL;
8244 omp_for = c_finish_omp_for (locus, code, declv, orig_declv, initv, condv,
8245 incrv, body, pre_body);
8247 /* Check for iterators appearing in lb, b or incr expressions. */
8248 if (omp_for && !c_omp_check_loop_iv (omp_for, orig_declv, cp_walk_subtrees))
8249 omp_for = NULL_TREE;
8251 if (omp_for == NULL)
8253 return NULL;
8256 add_stmt (omp_for);
8258 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INCR (omp_for)); i++)
8260 decl = TREE_OPERAND (TREE_VEC_ELT (OMP_FOR_INIT (omp_for), i), 0);
8261 incr = TREE_VEC_ELT (OMP_FOR_INCR (omp_for), i);
8263 if (TREE_CODE (incr) != MODIFY_EXPR)
8264 continue;
8266 if (TREE_SIDE_EFFECTS (TREE_OPERAND (incr, 1))
8267 && BINARY_CLASS_P (TREE_OPERAND (incr, 1))
8268 && !processing_template_decl)
8270 tree t = TREE_OPERAND (TREE_OPERAND (incr, 1), 0);
8271 if (TREE_SIDE_EFFECTS (t)
8272 && t != decl
8273 && (TREE_CODE (t) != NOP_EXPR
8274 || TREE_OPERAND (t, 0) != decl))
8275 TREE_OPERAND (TREE_OPERAND (incr, 1), 0)
8276 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
8278 t = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
8279 if (TREE_SIDE_EFFECTS (t)
8280 && t != decl
8281 && (TREE_CODE (t) != NOP_EXPR
8282 || TREE_OPERAND (t, 0) != decl))
8283 TREE_OPERAND (TREE_OPERAND (incr, 1), 1)
8284 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
8287 if (orig_incr)
8288 TREE_VEC_ELT (OMP_FOR_INCR (omp_for), i) = TREE_VEC_ELT (orig_incr, i);
8290 OMP_FOR_CLAUSES (omp_for) = clauses;
8292 /* For simd loops with non-static data member iterators, we could have added
8293 OMP_CLAUSE_LINEAR clauses without OMP_CLAUSE_LINEAR_STEP. As we know the
8294 step at this point, fill it in. */
8295 if (code == OMP_SIMD && !processing_template_decl
8296 && TREE_VEC_LENGTH (OMP_FOR_INCR (omp_for)) == 1)
8297 for (tree c = omp_find_clause (clauses, OMP_CLAUSE_LINEAR); c;
8298 c = omp_find_clause (OMP_CLAUSE_CHAIN (c), OMP_CLAUSE_LINEAR))
8299 if (OMP_CLAUSE_LINEAR_STEP (c) == NULL_TREE)
8301 decl = TREE_OPERAND (TREE_VEC_ELT (OMP_FOR_INIT (omp_for), 0), 0);
8302 gcc_assert (decl == OMP_CLAUSE_DECL (c));
8303 incr = TREE_VEC_ELT (OMP_FOR_INCR (omp_for), 0);
8304 tree step, stept;
8305 switch (TREE_CODE (incr))
8307 case PREINCREMENT_EXPR:
8308 case POSTINCREMENT_EXPR:
8309 /* c_omp_for_incr_canonicalize_ptr() should have been
8310 called to massage things appropriately. */
8311 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (decl)));
8312 OMP_CLAUSE_LINEAR_STEP (c) = build_int_cst (TREE_TYPE (decl), 1);
8313 break;
8314 case PREDECREMENT_EXPR:
8315 case POSTDECREMENT_EXPR:
8316 /* c_omp_for_incr_canonicalize_ptr() should have been
8317 called to massage things appropriately. */
8318 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (decl)));
8319 OMP_CLAUSE_LINEAR_STEP (c)
8320 = build_int_cst (TREE_TYPE (decl), -1);
8321 break;
8322 case MODIFY_EXPR:
8323 gcc_assert (TREE_OPERAND (incr, 0) == decl);
8324 incr = TREE_OPERAND (incr, 1);
8325 switch (TREE_CODE (incr))
8327 case PLUS_EXPR:
8328 if (TREE_OPERAND (incr, 1) == decl)
8329 step = TREE_OPERAND (incr, 0);
8330 else
8331 step = TREE_OPERAND (incr, 1);
8332 break;
8333 case MINUS_EXPR:
8334 case POINTER_PLUS_EXPR:
8335 gcc_assert (TREE_OPERAND (incr, 0) == decl);
8336 step = TREE_OPERAND (incr, 1);
8337 break;
8338 default:
8339 gcc_unreachable ();
8341 stept = TREE_TYPE (decl);
8342 if (POINTER_TYPE_P (stept))
8343 stept = sizetype;
8344 step = fold_convert (stept, step);
8345 if (TREE_CODE (incr) == MINUS_EXPR)
8346 step = fold_build1 (NEGATE_EXPR, stept, step);
8347 OMP_CLAUSE_LINEAR_STEP (c) = step;
8348 break;
8349 default:
8350 gcc_unreachable ();
8354 return omp_for;
8357 void
8358 finish_omp_atomic (enum tree_code code, enum tree_code opcode, tree lhs,
8359 tree rhs, tree v, tree lhs1, tree rhs1, bool seq_cst)
8361 tree orig_lhs;
8362 tree orig_rhs;
8363 tree orig_v;
8364 tree orig_lhs1;
8365 tree orig_rhs1;
8366 bool dependent_p;
8367 tree stmt;
8369 orig_lhs = lhs;
8370 orig_rhs = rhs;
8371 orig_v = v;
8372 orig_lhs1 = lhs1;
8373 orig_rhs1 = rhs1;
8374 dependent_p = false;
8375 stmt = NULL_TREE;
8377 /* Even in a template, we can detect invalid uses of the atomic
8378 pragma if neither LHS nor RHS is type-dependent. */
8379 if (processing_template_decl)
8381 dependent_p = (type_dependent_expression_p (lhs)
8382 || (rhs && type_dependent_expression_p (rhs))
8383 || (v && type_dependent_expression_p (v))
8384 || (lhs1 && type_dependent_expression_p (lhs1))
8385 || (rhs1 && type_dependent_expression_p (rhs1)));
8386 if (!dependent_p)
8388 lhs = build_non_dependent_expr (lhs);
8389 if (rhs)
8390 rhs = build_non_dependent_expr (rhs);
8391 if (v)
8392 v = build_non_dependent_expr (v);
8393 if (lhs1)
8394 lhs1 = build_non_dependent_expr (lhs1);
8395 if (rhs1)
8396 rhs1 = build_non_dependent_expr (rhs1);
8399 if (!dependent_p)
8401 bool swapped = false;
8402 if (rhs1 && cp_tree_equal (lhs, rhs))
8404 std::swap (rhs, rhs1);
8405 swapped = !commutative_tree_code (opcode);
8407 if (rhs1 && !cp_tree_equal (lhs, rhs1))
8409 if (code == OMP_ATOMIC)
8410 error ("%<#pragma omp atomic update%> uses two different "
8411 "expressions for memory");
8412 else
8413 error ("%<#pragma omp atomic capture%> uses two different "
8414 "expressions for memory");
8415 return;
8417 if (lhs1 && !cp_tree_equal (lhs, lhs1))
8419 if (code == OMP_ATOMIC)
8420 error ("%<#pragma omp atomic update%> uses two different "
8421 "expressions for memory");
8422 else
8423 error ("%<#pragma omp atomic capture%> uses two different "
8424 "expressions for memory");
8425 return;
8427 stmt = c_finish_omp_atomic (input_location, code, opcode, lhs, rhs,
8428 v, lhs1, rhs1, swapped, seq_cst,
8429 processing_template_decl != 0);
8430 if (stmt == error_mark_node)
8431 return;
8433 if (processing_template_decl)
8435 if (code == OMP_ATOMIC_READ)
8437 stmt = build_min_nt_loc (EXPR_LOCATION (orig_lhs),
8438 OMP_ATOMIC_READ, orig_lhs);
8439 OMP_ATOMIC_SEQ_CST (stmt) = seq_cst;
8440 stmt = build2 (MODIFY_EXPR, void_type_node, orig_v, stmt);
8442 else
8444 if (opcode == NOP_EXPR)
8445 stmt = build2 (MODIFY_EXPR, void_type_node, orig_lhs, orig_rhs);
8446 else
8447 stmt = build2 (opcode, void_type_node, orig_lhs, orig_rhs);
8448 if (orig_rhs1)
8449 stmt = build_min_nt_loc (EXPR_LOCATION (orig_rhs1),
8450 COMPOUND_EXPR, orig_rhs1, stmt);
8451 if (code != OMP_ATOMIC)
8453 stmt = build_min_nt_loc (EXPR_LOCATION (orig_lhs1),
8454 code, orig_lhs1, stmt);
8455 OMP_ATOMIC_SEQ_CST (stmt) = seq_cst;
8456 stmt = build2 (MODIFY_EXPR, void_type_node, orig_v, stmt);
8459 stmt = build2 (OMP_ATOMIC, void_type_node, integer_zero_node, stmt);
8460 OMP_ATOMIC_SEQ_CST (stmt) = seq_cst;
8462 finish_expr_stmt (stmt);
8465 void
8466 finish_omp_barrier (void)
8468 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_BARRIER);
8469 vec<tree, va_gc> *vec = make_tree_vector ();
8470 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
8471 release_tree_vector (vec);
8472 finish_expr_stmt (stmt);
8475 void
8476 finish_omp_flush (void)
8478 tree fn = builtin_decl_explicit (BUILT_IN_SYNC_SYNCHRONIZE);
8479 vec<tree, va_gc> *vec = make_tree_vector ();
8480 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
8481 release_tree_vector (vec);
8482 finish_expr_stmt (stmt);
8485 void
8486 finish_omp_taskwait (void)
8488 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_TASKWAIT);
8489 vec<tree, va_gc> *vec = make_tree_vector ();
8490 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
8491 release_tree_vector (vec);
8492 finish_expr_stmt (stmt);
8495 void
8496 finish_omp_taskyield (void)
8498 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_TASKYIELD);
8499 vec<tree, va_gc> *vec = make_tree_vector ();
8500 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
8501 release_tree_vector (vec);
8502 finish_expr_stmt (stmt);
8505 void
8506 finish_omp_cancel (tree clauses)
8508 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
8509 int mask = 0;
8510 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
8511 mask = 1;
8512 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
8513 mask = 2;
8514 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
8515 mask = 4;
8516 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
8517 mask = 8;
8518 else
8520 error ("%<#pragma omp cancel%> must specify one of "
8521 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> clauses");
8522 return;
8524 vec<tree, va_gc> *vec = make_tree_vector ();
8525 tree ifc = omp_find_clause (clauses, OMP_CLAUSE_IF);
8526 if (ifc != NULL_TREE)
8528 tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
8529 ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
8530 boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
8531 build_zero_cst (type));
8533 else
8534 ifc = boolean_true_node;
8535 vec->quick_push (build_int_cst (integer_type_node, mask));
8536 vec->quick_push (ifc);
8537 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
8538 release_tree_vector (vec);
8539 finish_expr_stmt (stmt);
8542 void
8543 finish_omp_cancellation_point (tree clauses)
8545 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
8546 int mask = 0;
8547 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
8548 mask = 1;
8549 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
8550 mask = 2;
8551 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
8552 mask = 4;
8553 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
8554 mask = 8;
8555 else
8557 error ("%<#pragma omp cancellation point%> must specify one of "
8558 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> clauses");
8559 return;
8561 vec<tree, va_gc> *vec
8562 = make_tree_vector_single (build_int_cst (integer_type_node, mask));
8563 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
8564 release_tree_vector (vec);
8565 finish_expr_stmt (stmt);
8568 /* Begin a __transaction_atomic or __transaction_relaxed statement.
8569 If PCOMPOUND is non-null, this is for a function-transaction-block, and we
8570 should create an extra compound stmt. */
8572 tree
8573 begin_transaction_stmt (location_t loc, tree *pcompound, int flags)
8575 tree r;
8577 if (pcompound)
8578 *pcompound = begin_compound_stmt (0);
8580 r = build_stmt (loc, TRANSACTION_EXPR, NULL_TREE);
8582 /* Only add the statement to the function if support enabled. */
8583 if (flag_tm)
8584 add_stmt (r);
8585 else
8586 error_at (loc, ((flags & TM_STMT_ATTR_RELAXED) != 0
8587 ? G_("%<__transaction_relaxed%> without "
8588 "transactional memory support enabled")
8589 : G_("%<__transaction_atomic%> without "
8590 "transactional memory support enabled")));
8592 TRANSACTION_EXPR_BODY (r) = push_stmt_list ();
8593 TREE_SIDE_EFFECTS (r) = 1;
8594 return r;
8597 /* End a __transaction_atomic or __transaction_relaxed statement.
8598 If COMPOUND_STMT is non-null, this is for a function-transaction-block,
8599 and we should end the compound. If NOEX is non-NULL, we wrap the body in
8600 a MUST_NOT_THROW_EXPR with NOEX as condition. */
8602 void
8603 finish_transaction_stmt (tree stmt, tree compound_stmt, int flags, tree noex)
8605 TRANSACTION_EXPR_BODY (stmt) = pop_stmt_list (TRANSACTION_EXPR_BODY (stmt));
8606 TRANSACTION_EXPR_OUTER (stmt) = (flags & TM_STMT_ATTR_OUTER) != 0;
8607 TRANSACTION_EXPR_RELAXED (stmt) = (flags & TM_STMT_ATTR_RELAXED) != 0;
8608 TRANSACTION_EXPR_IS_STMT (stmt) = 1;
8610 /* noexcept specifications are not allowed for function transactions. */
8611 gcc_assert (!(noex && compound_stmt));
8612 if (noex)
8614 tree body = build_must_not_throw_expr (TRANSACTION_EXPR_BODY (stmt),
8615 noex);
8616 protected_set_expr_location
8617 (body, EXPR_LOCATION (TRANSACTION_EXPR_BODY (stmt)));
8618 TREE_SIDE_EFFECTS (body) = 1;
8619 TRANSACTION_EXPR_BODY (stmt) = body;
8622 if (compound_stmt)
8623 finish_compound_stmt (compound_stmt);
8626 /* Build a __transaction_atomic or __transaction_relaxed expression. If
8627 NOEX is non-NULL, we wrap the body in a MUST_NOT_THROW_EXPR with NOEX as
8628 condition. */
8630 tree
8631 build_transaction_expr (location_t loc, tree expr, int flags, tree noex)
8633 tree ret;
8634 if (noex)
8636 expr = build_must_not_throw_expr (expr, noex);
8637 protected_set_expr_location (expr, loc);
8638 TREE_SIDE_EFFECTS (expr) = 1;
8640 ret = build1 (TRANSACTION_EXPR, TREE_TYPE (expr), expr);
8641 if (flags & TM_STMT_ATTR_RELAXED)
8642 TRANSACTION_EXPR_RELAXED (ret) = 1;
8643 TREE_SIDE_EFFECTS (ret) = 1;
8644 SET_EXPR_LOCATION (ret, loc);
8645 return ret;
8648 void
8649 init_cp_semantics (void)
8653 /* Build a STATIC_ASSERT for a static assertion with the condition
8654 CONDITION and the message text MESSAGE. LOCATION is the location
8655 of the static assertion in the source code. When MEMBER_P, this
8656 static assertion is a member of a class. */
8657 void
8658 finish_static_assert (tree condition, tree message, location_t location,
8659 bool member_p)
8661 tsubst_flags_t complain = tf_warning_or_error;
8663 if (message == NULL_TREE
8664 || message == error_mark_node
8665 || condition == NULL_TREE
8666 || condition == error_mark_node)
8667 return;
8669 if (check_for_bare_parameter_packs (condition))
8670 condition = error_mark_node;
8672 if (instantiation_dependent_expression_p (condition))
8674 /* We're in a template; build a STATIC_ASSERT and put it in
8675 the right place. */
8676 tree assertion;
8678 assertion = make_node (STATIC_ASSERT);
8679 STATIC_ASSERT_CONDITION (assertion) = condition;
8680 STATIC_ASSERT_MESSAGE (assertion) = message;
8681 STATIC_ASSERT_SOURCE_LOCATION (assertion) = location;
8683 if (member_p)
8684 maybe_add_class_template_decl_list (current_class_type,
8685 assertion,
8686 /*friend_p=*/0);
8687 else
8688 add_stmt (assertion);
8690 return;
8693 /* Fold the expression and convert it to a boolean value. */
8694 condition = perform_implicit_conversion_flags (boolean_type_node, condition,
8695 complain, LOOKUP_NORMAL);
8696 condition = fold_non_dependent_expr (condition);
8698 if (TREE_CODE (condition) == INTEGER_CST && !integer_zerop (condition))
8699 /* Do nothing; the condition is satisfied. */
8701 else
8703 location_t saved_loc = input_location;
8705 input_location = location;
8706 if (TREE_CODE (condition) == INTEGER_CST
8707 && integer_zerop (condition))
8709 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
8710 (TREE_TYPE (TREE_TYPE (message))));
8711 int len = TREE_STRING_LENGTH (message) / sz - 1;
8712 /* Report the error. */
8713 if (len == 0)
8714 error ("static assertion failed");
8715 else
8716 error ("static assertion failed: %s",
8717 TREE_STRING_POINTER (message));
8719 else if (condition && condition != error_mark_node)
8721 error ("non-constant condition for static assertion");
8722 if (require_rvalue_constant_expression (condition))
8723 cxx_constant_value (condition);
8725 input_location = saved_loc;
8729 /* Implements the C++0x decltype keyword. Returns the type of EXPR,
8730 suitable for use as a type-specifier.
8732 ID_EXPRESSION_OR_MEMBER_ACCESS_P is true when EXPR was parsed as an
8733 id-expression or a class member access, FALSE when it was parsed as
8734 a full expression. */
8736 tree
8737 finish_decltype_type (tree expr, bool id_expression_or_member_access_p,
8738 tsubst_flags_t complain)
8740 tree type = NULL_TREE;
8742 if (!expr || error_operand_p (expr))
8743 return error_mark_node;
8745 if (TYPE_P (expr)
8746 || TREE_CODE (expr) == TYPE_DECL
8747 || (TREE_CODE (expr) == BIT_NOT_EXPR
8748 && TYPE_P (TREE_OPERAND (expr, 0))))
8750 if (complain & tf_error)
8751 error ("argument to decltype must be an expression");
8752 return error_mark_node;
8755 /* Depending on the resolution of DR 1172, we may later need to distinguish
8756 instantiation-dependent but not type-dependent expressions so that, say,
8757 A<decltype(sizeof(T))>::U doesn't require 'typename'. */
8758 if (instantiation_dependent_uneval_expression_p (expr))
8760 type = cxx_make_type (DECLTYPE_TYPE);
8761 DECLTYPE_TYPE_EXPR (type) = expr;
8762 DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (type)
8763 = id_expression_or_member_access_p;
8764 SET_TYPE_STRUCTURAL_EQUALITY (type);
8766 return type;
8769 /* The type denoted by decltype(e) is defined as follows: */
8771 expr = resolve_nondeduced_context (expr, complain);
8773 if (invalid_nonstatic_memfn_p (input_location, expr, complain))
8774 return error_mark_node;
8776 if (type_unknown_p (expr))
8778 if (complain & tf_error)
8779 error ("decltype cannot resolve address of overloaded function");
8780 return error_mark_node;
8783 /* To get the size of a static data member declared as an array of
8784 unknown bound, we need to instantiate it. */
8785 if (VAR_P (expr)
8786 && VAR_HAD_UNKNOWN_BOUND (expr)
8787 && DECL_TEMPLATE_INSTANTIATION (expr))
8788 instantiate_decl (expr, /*defer_ok*/true, /*expl_inst_mem*/false);
8790 if (id_expression_or_member_access_p)
8792 /* If e is an id-expression or a class member access (5.2.5
8793 [expr.ref]), decltype(e) is defined as the type of the entity
8794 named by e. If there is no such entity, or e names a set of
8795 overloaded functions, the program is ill-formed. */
8796 if (identifier_p (expr))
8797 expr = lookup_name (expr);
8799 if (INDIRECT_REF_P (expr))
8800 /* This can happen when the expression is, e.g., "a.b". Just
8801 look at the underlying operand. */
8802 expr = TREE_OPERAND (expr, 0);
8804 if (TREE_CODE (expr) == OFFSET_REF
8805 || TREE_CODE (expr) == MEMBER_REF
8806 || TREE_CODE (expr) == SCOPE_REF)
8807 /* We're only interested in the field itself. If it is a
8808 BASELINK, we will need to see through it in the next
8809 step. */
8810 expr = TREE_OPERAND (expr, 1);
8812 if (BASELINK_P (expr))
8813 /* See through BASELINK nodes to the underlying function. */
8814 expr = BASELINK_FUNCTIONS (expr);
8816 /* decltype of a decomposition name drops references in the tuple case
8817 (unlike decltype of a normal variable) and keeps cv-qualifiers from
8818 the containing object in the other cases (unlike decltype of a member
8819 access expression). */
8820 if (DECL_DECOMPOSITION_P (expr))
8822 if (DECL_HAS_VALUE_EXPR_P (expr))
8823 /* Expr is an array or struct subobject proxy, handle
8824 bit-fields properly. */
8825 return unlowered_expr_type (expr);
8826 else
8827 /* Expr is a reference variable for the tuple case. */
8828 return lookup_decomp_type (expr);
8831 switch (TREE_CODE (expr))
8833 case FIELD_DECL:
8834 if (DECL_BIT_FIELD_TYPE (expr))
8836 type = DECL_BIT_FIELD_TYPE (expr);
8837 break;
8839 /* Fall through for fields that aren't bitfields. */
8840 gcc_fallthrough ();
8842 case FUNCTION_DECL:
8843 case VAR_DECL:
8844 case CONST_DECL:
8845 case PARM_DECL:
8846 case RESULT_DECL:
8847 case TEMPLATE_PARM_INDEX:
8848 expr = mark_type_use (expr);
8849 type = TREE_TYPE (expr);
8850 break;
8852 case ERROR_MARK:
8853 type = error_mark_node;
8854 break;
8856 case COMPONENT_REF:
8857 case COMPOUND_EXPR:
8858 mark_type_use (expr);
8859 type = is_bitfield_expr_with_lowered_type (expr);
8860 if (!type)
8861 type = TREE_TYPE (TREE_OPERAND (expr, 1));
8862 break;
8864 case BIT_FIELD_REF:
8865 gcc_unreachable ();
8867 case INTEGER_CST:
8868 case PTRMEM_CST:
8869 /* We can get here when the id-expression refers to an
8870 enumerator or non-type template parameter. */
8871 type = TREE_TYPE (expr);
8872 break;
8874 default:
8875 /* Handle instantiated template non-type arguments. */
8876 type = TREE_TYPE (expr);
8877 break;
8880 else
8882 /* Within a lambda-expression:
8884 Every occurrence of decltype((x)) where x is a possibly
8885 parenthesized id-expression that names an entity of
8886 automatic storage duration is treated as if x were
8887 transformed into an access to a corresponding data member
8888 of the closure type that would have been declared if x
8889 were a use of the denoted entity. */
8890 if (outer_automatic_var_p (expr)
8891 && current_function_decl
8892 && LAMBDA_FUNCTION_P (current_function_decl))
8893 type = capture_decltype (expr);
8894 else if (error_operand_p (expr))
8895 type = error_mark_node;
8896 else if (expr == current_class_ptr)
8897 /* If the expression is just "this", we want the
8898 cv-unqualified pointer for the "this" type. */
8899 type = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
8900 else
8902 /* Otherwise, where T is the type of e, if e is an lvalue,
8903 decltype(e) is defined as T&; if an xvalue, T&&; otherwise, T. */
8904 cp_lvalue_kind clk = lvalue_kind (expr);
8905 type = unlowered_expr_type (expr);
8906 gcc_assert (TREE_CODE (type) != REFERENCE_TYPE);
8908 /* For vector types, pick a non-opaque variant. */
8909 if (VECTOR_TYPE_P (type))
8910 type = strip_typedefs (type);
8912 if (clk != clk_none && !(clk & clk_class))
8913 type = cp_build_reference_type (type, (clk & clk_rvalueref));
8917 return type;
8920 /* Called from trait_expr_value to evaluate either __has_nothrow_assign or
8921 __has_nothrow_copy, depending on assign_p. Returns true iff all
8922 the copy {ctor,assign} fns are nothrow. */
8924 static bool
8925 classtype_has_nothrow_assign_or_copy_p (tree type, bool assign_p)
8927 tree fns = NULL_TREE;
8929 if (assign_p || TYPE_HAS_COPY_CTOR (type))
8930 fns = get_class_binding (type, assign_p ? assign_op_identifier
8931 : ctor_identifier);
8933 bool saw_copy = false;
8934 for (ovl_iterator iter (fns); iter; ++iter)
8936 tree fn = *iter;
8938 if (copy_fn_p (fn) > 0)
8940 saw_copy = true;
8941 maybe_instantiate_noexcept (fn);
8942 if (!TYPE_NOTHROW_P (TREE_TYPE (fn)))
8943 return false;
8947 return saw_copy;
8950 /* Actually evaluates the trait. */
8952 static bool
8953 trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
8955 enum tree_code type_code1;
8956 tree t;
8958 type_code1 = TREE_CODE (type1);
8960 switch (kind)
8962 case CPTK_HAS_NOTHROW_ASSIGN:
8963 type1 = strip_array_types (type1);
8964 return (!CP_TYPE_CONST_P (type1) && type_code1 != REFERENCE_TYPE
8965 && (trait_expr_value (CPTK_HAS_TRIVIAL_ASSIGN, type1, type2)
8966 || (CLASS_TYPE_P (type1)
8967 && classtype_has_nothrow_assign_or_copy_p (type1,
8968 true))));
8970 case CPTK_HAS_TRIVIAL_ASSIGN:
8971 /* ??? The standard seems to be missing the "or array of such a class
8972 type" wording for this trait. */
8973 type1 = strip_array_types (type1);
8974 return (!CP_TYPE_CONST_P (type1) && type_code1 != REFERENCE_TYPE
8975 && (trivial_type_p (type1)
8976 || (CLASS_TYPE_P (type1)
8977 && TYPE_HAS_TRIVIAL_COPY_ASSIGN (type1))));
8979 case CPTK_HAS_NOTHROW_CONSTRUCTOR:
8980 type1 = strip_array_types (type1);
8981 return (trait_expr_value (CPTK_HAS_TRIVIAL_CONSTRUCTOR, type1, type2)
8982 || (CLASS_TYPE_P (type1)
8983 && (t = locate_ctor (type1))
8984 && (maybe_instantiate_noexcept (t),
8985 TYPE_NOTHROW_P (TREE_TYPE (t)))));
8987 case CPTK_HAS_TRIVIAL_CONSTRUCTOR:
8988 type1 = strip_array_types (type1);
8989 return (trivial_type_p (type1)
8990 || (CLASS_TYPE_P (type1) && TYPE_HAS_TRIVIAL_DFLT (type1)));
8992 case CPTK_HAS_NOTHROW_COPY:
8993 type1 = strip_array_types (type1);
8994 return (trait_expr_value (CPTK_HAS_TRIVIAL_COPY, type1, type2)
8995 || (CLASS_TYPE_P (type1)
8996 && classtype_has_nothrow_assign_or_copy_p (type1, false)));
8998 case CPTK_HAS_TRIVIAL_COPY:
8999 /* ??? The standard seems to be missing the "or array of such a class
9000 type" wording for this trait. */
9001 type1 = strip_array_types (type1);
9002 return (trivial_type_p (type1) || type_code1 == REFERENCE_TYPE
9003 || (CLASS_TYPE_P (type1) && TYPE_HAS_TRIVIAL_COPY_CTOR (type1)));
9005 case CPTK_HAS_TRIVIAL_DESTRUCTOR:
9006 type1 = strip_array_types (type1);
9007 return (trivial_type_p (type1) || type_code1 == REFERENCE_TYPE
9008 || (CLASS_TYPE_P (type1)
9009 && TYPE_HAS_TRIVIAL_DESTRUCTOR (type1)));
9011 case CPTK_HAS_VIRTUAL_DESTRUCTOR:
9012 return type_has_virtual_destructor (type1);
9014 case CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS:
9015 return type_has_unique_obj_representations (type1);
9017 case CPTK_IS_ABSTRACT:
9018 return ABSTRACT_CLASS_TYPE_P (type1);
9020 case CPTK_IS_AGGREGATE:
9021 return CP_AGGREGATE_TYPE_P (type1);
9023 case CPTK_IS_BASE_OF:
9024 return (NON_UNION_CLASS_TYPE_P (type1) && NON_UNION_CLASS_TYPE_P (type2)
9025 && (same_type_ignoring_top_level_qualifiers_p (type1, type2)
9026 || DERIVED_FROM_P (type1, type2)));
9028 case CPTK_IS_CLASS:
9029 return NON_UNION_CLASS_TYPE_P (type1);
9031 case CPTK_IS_EMPTY:
9032 return NON_UNION_CLASS_TYPE_P (type1) && CLASSTYPE_EMPTY_P (type1);
9034 case CPTK_IS_ENUM:
9035 return type_code1 == ENUMERAL_TYPE;
9037 case CPTK_IS_FINAL:
9038 return CLASS_TYPE_P (type1) && CLASSTYPE_FINAL (type1);
9040 case CPTK_IS_LITERAL_TYPE:
9041 return literal_type_p (type1);
9043 case CPTK_IS_POD:
9044 return pod_type_p (type1);
9046 case CPTK_IS_POLYMORPHIC:
9047 return CLASS_TYPE_P (type1) && TYPE_POLYMORPHIC_P (type1);
9049 case CPTK_IS_SAME_AS:
9050 return same_type_p (type1, type2);
9052 case CPTK_IS_STD_LAYOUT:
9053 return std_layout_type_p (type1);
9055 case CPTK_IS_TRIVIAL:
9056 return trivial_type_p (type1);
9058 case CPTK_IS_TRIVIALLY_ASSIGNABLE:
9059 return is_trivially_xible (MODIFY_EXPR, type1, type2);
9061 case CPTK_IS_TRIVIALLY_CONSTRUCTIBLE:
9062 return is_trivially_xible (INIT_EXPR, type1, type2);
9064 case CPTK_IS_TRIVIALLY_COPYABLE:
9065 return trivially_copyable_p (type1);
9067 case CPTK_IS_UNION:
9068 return type_code1 == UNION_TYPE;
9070 case CPTK_IS_ASSIGNABLE:
9071 return is_xible (MODIFY_EXPR, type1, type2);
9073 case CPTK_IS_CONSTRUCTIBLE:
9074 return is_xible (INIT_EXPR, type1, type2);
9076 default:
9077 gcc_unreachable ();
9078 return false;
9082 /* If TYPE is an array of unknown bound, or (possibly cv-qualified)
9083 void, or a complete type, returns true, otherwise false. */
9085 static bool
9086 check_trait_type (tree type)
9088 if (type == NULL_TREE)
9089 return true;
9091 if (TREE_CODE (type) == TREE_LIST)
9092 return (check_trait_type (TREE_VALUE (type))
9093 && check_trait_type (TREE_CHAIN (type)));
9095 if (TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type)
9096 && COMPLETE_TYPE_P (TREE_TYPE (type)))
9097 return true;
9099 if (VOID_TYPE_P (type))
9100 return true;
9102 return !!complete_type_or_else (strip_array_types (type), NULL_TREE);
9105 /* Process a trait expression. */
9107 tree
9108 finish_trait_expr (cp_trait_kind kind, tree type1, tree type2)
9110 if (type1 == error_mark_node
9111 || type2 == error_mark_node)
9112 return error_mark_node;
9114 if (processing_template_decl)
9116 tree trait_expr = make_node (TRAIT_EXPR);
9117 TREE_TYPE (trait_expr) = boolean_type_node;
9118 TRAIT_EXPR_TYPE1 (trait_expr) = type1;
9119 TRAIT_EXPR_TYPE2 (trait_expr) = type2;
9120 TRAIT_EXPR_KIND (trait_expr) = kind;
9121 return trait_expr;
9124 switch (kind)
9126 case CPTK_HAS_NOTHROW_ASSIGN:
9127 case CPTK_HAS_TRIVIAL_ASSIGN:
9128 case CPTK_HAS_NOTHROW_CONSTRUCTOR:
9129 case CPTK_HAS_TRIVIAL_CONSTRUCTOR:
9130 case CPTK_HAS_NOTHROW_COPY:
9131 case CPTK_HAS_TRIVIAL_COPY:
9132 case CPTK_HAS_TRIVIAL_DESTRUCTOR:
9133 case CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS:
9134 case CPTK_HAS_VIRTUAL_DESTRUCTOR:
9135 case CPTK_IS_ABSTRACT:
9136 case CPTK_IS_AGGREGATE:
9137 case CPTK_IS_EMPTY:
9138 case CPTK_IS_FINAL:
9139 case CPTK_IS_LITERAL_TYPE:
9140 case CPTK_IS_POD:
9141 case CPTK_IS_POLYMORPHIC:
9142 case CPTK_IS_STD_LAYOUT:
9143 case CPTK_IS_TRIVIAL:
9144 case CPTK_IS_TRIVIALLY_COPYABLE:
9145 if (!check_trait_type (type1))
9146 return error_mark_node;
9147 break;
9149 case CPTK_IS_ASSIGNABLE:
9150 case CPTK_IS_CONSTRUCTIBLE:
9151 break;
9153 case CPTK_IS_TRIVIALLY_ASSIGNABLE:
9154 case CPTK_IS_TRIVIALLY_CONSTRUCTIBLE:
9155 if (!check_trait_type (type1)
9156 || !check_trait_type (type2))
9157 return error_mark_node;
9158 break;
9160 case CPTK_IS_BASE_OF:
9161 if (NON_UNION_CLASS_TYPE_P (type1) && NON_UNION_CLASS_TYPE_P (type2)
9162 && !same_type_ignoring_top_level_qualifiers_p (type1, type2)
9163 && !complete_type_or_else (type2, NULL_TREE))
9164 /* We already issued an error. */
9165 return error_mark_node;
9166 break;
9168 case CPTK_IS_CLASS:
9169 case CPTK_IS_ENUM:
9170 case CPTK_IS_UNION:
9171 case CPTK_IS_SAME_AS:
9172 break;
9174 default:
9175 gcc_unreachable ();
9178 return (trait_expr_value (kind, type1, type2)
9179 ? boolean_true_node : boolean_false_node);
9182 /* Do-nothing variants of functions to handle pragma FLOAT_CONST_DECIMAL64,
9183 which is ignored for C++. */
9185 void
9186 set_float_const_decimal64 (void)
9190 void
9191 clear_float_const_decimal64 (void)
9195 bool
9196 float_const_decimal64_p (void)
9198 return 0;
9202 /* Return true if T designates the implied `this' parameter. */
9204 bool
9205 is_this_parameter (tree t)
9207 if (!DECL_P (t) || DECL_NAME (t) != this_identifier)
9208 return false;
9209 gcc_assert (TREE_CODE (t) == PARM_DECL || is_capture_proxy (t)
9210 || (cp_binding_oracle && TREE_CODE (t) == VAR_DECL));
9211 return true;
9214 /* Insert the deduced return type for an auto function. */
9216 void
9217 apply_deduced_return_type (tree fco, tree return_type)
9219 tree result;
9221 if (return_type == error_mark_node)
9222 return;
9224 if (DECL_CONV_FN_P (fco))
9225 DECL_NAME (fco) = make_conv_op_name (return_type);
9227 TREE_TYPE (fco) = change_return_type (return_type, TREE_TYPE (fco));
9229 result = DECL_RESULT (fco);
9230 if (result == NULL_TREE)
9231 return;
9232 if (TREE_TYPE (result) == return_type)
9233 return;
9235 if (!processing_template_decl && !VOID_TYPE_P (return_type)
9236 && !complete_type_or_else (return_type, NULL_TREE))
9237 return;
9239 /* We already have a DECL_RESULT from start_preparsed_function.
9240 Now we need to redo the work it and allocate_struct_function
9241 did to reflect the new type. */
9242 gcc_assert (current_function_decl == fco);
9243 result = build_decl (input_location, RESULT_DECL, NULL_TREE,
9244 TYPE_MAIN_VARIANT (return_type));
9245 DECL_ARTIFICIAL (result) = 1;
9246 DECL_IGNORED_P (result) = 1;
9247 cp_apply_type_quals_to_decl (cp_type_quals (return_type),
9248 result);
9250 DECL_RESULT (fco) = result;
9252 if (!processing_template_decl)
9254 bool aggr = aggregate_value_p (result, fco);
9255 #ifdef PCC_STATIC_STRUCT_RETURN
9256 cfun->returns_pcc_struct = aggr;
9257 #endif
9258 cfun->returns_struct = aggr;
9263 /* DECL is a local variable or parameter from the surrounding scope of a
9264 lambda-expression. Returns the decltype for a use of the capture field
9265 for DECL even if it hasn't been captured yet. */
9267 static tree
9268 capture_decltype (tree decl)
9270 tree lam = CLASSTYPE_LAMBDA_EXPR (DECL_CONTEXT (current_function_decl));
9271 /* FIXME do lookup instead of list walk? */
9272 tree cap = value_member (decl, LAMBDA_EXPR_CAPTURE_LIST (lam));
9273 tree type;
9275 if (cap)
9276 type = TREE_TYPE (TREE_PURPOSE (cap));
9277 else
9278 switch (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lam))
9280 case CPLD_NONE:
9281 error ("%qD is not captured", decl);
9282 return error_mark_node;
9284 case CPLD_COPY:
9285 type = TREE_TYPE (decl);
9286 if (TREE_CODE (type) == REFERENCE_TYPE
9287 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
9288 type = TREE_TYPE (type);
9289 break;
9291 case CPLD_REFERENCE:
9292 type = TREE_TYPE (decl);
9293 if (TREE_CODE (type) != REFERENCE_TYPE)
9294 type = build_reference_type (TREE_TYPE (decl));
9295 break;
9297 default:
9298 gcc_unreachable ();
9301 if (TREE_CODE (type) != REFERENCE_TYPE)
9303 if (!LAMBDA_EXPR_MUTABLE_P (lam))
9304 type = cp_build_qualified_type (type, (cp_type_quals (type)
9305 |TYPE_QUAL_CONST));
9306 type = build_reference_type (type);
9308 return type;
9311 /* Build a unary fold expression of EXPR over OP. If IS_RIGHT is true,
9312 this is a right unary fold. Otherwise it is a left unary fold. */
9314 static tree
9315 finish_unary_fold_expr (tree expr, int op, tree_code dir)
9317 // Build a pack expansion (assuming expr has pack type).
9318 if (!uses_parameter_packs (expr))
9320 error_at (location_of (expr), "operand of fold expression has no "
9321 "unexpanded parameter packs");
9322 return error_mark_node;
9324 tree pack = make_pack_expansion (expr);
9326 // Build the fold expression.
9327 tree code = build_int_cstu (integer_type_node, abs (op));
9328 tree fold = build_min_nt_loc (UNKNOWN_LOCATION, dir, code, pack);
9329 FOLD_EXPR_MODIFY_P (fold) = (op < 0);
9330 return fold;
9333 tree
9334 finish_left_unary_fold_expr (tree expr, int op)
9336 return finish_unary_fold_expr (expr, op, UNARY_LEFT_FOLD_EXPR);
9339 tree
9340 finish_right_unary_fold_expr (tree expr, int op)
9342 return finish_unary_fold_expr (expr, op, UNARY_RIGHT_FOLD_EXPR);
9345 /* Build a binary fold expression over EXPR1 and EXPR2. The
9346 associativity of the fold is determined by EXPR1 and EXPR2 (whichever
9347 has an unexpanded parameter pack). */
9349 tree
9350 finish_binary_fold_expr (tree pack, tree init, int op, tree_code dir)
9352 pack = make_pack_expansion (pack);
9353 tree code = build_int_cstu (integer_type_node, abs (op));
9354 tree fold = build_min_nt_loc (UNKNOWN_LOCATION, dir, code, pack, init);
9355 FOLD_EXPR_MODIFY_P (fold) = (op < 0);
9356 return fold;
9359 tree
9360 finish_binary_fold_expr (tree expr1, tree expr2, int op)
9362 // Determine which expr has an unexpanded parameter pack and
9363 // set the pack and initial term.
9364 bool pack1 = uses_parameter_packs (expr1);
9365 bool pack2 = uses_parameter_packs (expr2);
9366 if (pack1 && !pack2)
9367 return finish_binary_fold_expr (expr1, expr2, op, BINARY_RIGHT_FOLD_EXPR);
9368 else if (pack2 && !pack1)
9369 return finish_binary_fold_expr (expr2, expr1, op, BINARY_LEFT_FOLD_EXPR);
9370 else
9372 if (pack1)
9373 error ("both arguments in binary fold have unexpanded parameter packs");
9374 else
9375 error ("no unexpanded parameter packs in binary fold");
9377 return error_mark_node;
9380 /* Finish __builtin_launder (arg). */
9382 tree
9383 finish_builtin_launder (location_t loc, tree arg, tsubst_flags_t complain)
9385 tree orig_arg = arg;
9386 if (!type_dependent_expression_p (arg))
9387 arg = decay_conversion (arg, complain);
9388 if (error_operand_p (arg))
9389 return error_mark_node;
9390 if (!type_dependent_expression_p (arg)
9391 && TREE_CODE (TREE_TYPE (arg)) != POINTER_TYPE)
9393 error_at (loc, "non-pointer argument to %<__builtin_launder%>");
9394 return error_mark_node;
9396 if (processing_template_decl)
9397 arg = orig_arg;
9398 return build_call_expr_internal_loc (loc, IFN_LAUNDER,
9399 TREE_TYPE (arg), 1, arg);
9402 #include "gt-cp-semantics.h"