PR c++/84707
[official-gcc.git] / gcc / cp / semantics.c
blobbf5b41e08798268bde426d5cdfc21a63ce98fd73
1 /* Perform the semantic phase of parsing, i.e., the process of
2 building tree structure, checking semantic consistency, and
3 building RTL. These routines are used both during actual parsing
4 and during the instantiation of template functions.
6 Copyright (C) 1998-2018 Free Software Foundation, Inc.
7 Written by Mark Mitchell (mmitchell@usa.net) based on code found
8 formerly in parse.y and pt.c.
10 This file is part of GCC.
12 GCC is free software; you can redistribute it and/or modify it
13 under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 3, or (at your option)
15 any later version.
17 GCC is distributed in the hope that it will be useful, but
18 WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with GCC; see the file COPYING3. If not see
24 <http://www.gnu.org/licenses/>. */
26 #include "config.h"
27 #include "system.h"
28 #include "coretypes.h"
29 #include "target.h"
30 #include "bitmap.h"
31 #include "cp-tree.h"
32 #include "stringpool.h"
33 #include "cgraph.h"
34 #include "stmt.h"
35 #include "varasm.h"
36 #include "stor-layout.h"
37 #include "c-family/c-objc.h"
38 #include "tree-inline.h"
39 #include "intl.h"
40 #include "tree-iterator.h"
41 #include "omp-general.h"
42 #include "convert.h"
43 #include "stringpool.h"
44 #include "attribs.h"
45 #include "gomp-constants.h"
46 #include "predict.h"
48 /* There routines provide a modular interface to perform many parsing
49 operations. They may therefore be used during actual parsing, or
50 during template instantiation, which may be regarded as a
51 degenerate form of parsing. */
53 static tree maybe_convert_cond (tree);
54 static tree finalize_nrv_r (tree *, int *, void *);
55 static tree capture_decltype (tree);
57 /* Used for OpenMP non-static data member privatization. */
59 static hash_map<tree, tree> *omp_private_member_map;
60 static vec<tree> omp_private_member_vec;
61 static bool omp_private_member_ignore_next;
64 /* Deferred Access Checking Overview
65 ---------------------------------
67 Most C++ expressions and declarations require access checking
68 to be performed during parsing. However, in several cases,
69 this has to be treated differently.
71 For member declarations, access checking has to be deferred
72 until more information about the declaration is known. For
73 example:
75 class A {
76 typedef int X;
77 public:
78 X f();
81 A::X A::f();
82 A::X g();
84 When we are parsing the function return type `A::X', we don't
85 really know if this is allowed until we parse the function name.
87 Furthermore, some contexts require that access checking is
88 never performed at all. These include class heads, and template
89 instantiations.
91 Typical use of access checking functions is described here:
93 1. When we enter a context that requires certain access checking
94 mode, the function `push_deferring_access_checks' is called with
95 DEFERRING argument specifying the desired mode. Access checking
96 may be performed immediately (dk_no_deferred), deferred
97 (dk_deferred), or not performed (dk_no_check).
99 2. When a declaration such as a type, or a variable, is encountered,
100 the function `perform_or_defer_access_check' is called. It
101 maintains a vector of all deferred checks.
103 3. The global `current_class_type' or `current_function_decl' is then
104 setup by the parser. `enforce_access' relies on these information
105 to check access.
107 4. Upon exiting the context mentioned in step 1,
108 `perform_deferred_access_checks' is called to check all declaration
109 stored in the vector. `pop_deferring_access_checks' is then
110 called to restore the previous access checking mode.
112 In case of parsing error, we simply call `pop_deferring_access_checks'
113 without `perform_deferred_access_checks'. */
115 struct GTY(()) deferred_access {
116 /* A vector representing name-lookups for which we have deferred
117 checking access controls. We cannot check the accessibility of
118 names used in a decl-specifier-seq until we know what is being
119 declared because code like:
121 class A {
122 class B {};
123 B* f();
126 A::B* A::f() { return 0; }
128 is valid, even though `A::B' is not generally accessible. */
129 vec<deferred_access_check, va_gc> * GTY(()) deferred_access_checks;
131 /* The current mode of access checks. */
132 enum deferring_kind deferring_access_checks_kind;
136 /* Data for deferred access checking. */
137 static GTY(()) vec<deferred_access, va_gc> *deferred_access_stack;
138 static GTY(()) unsigned deferred_access_no_check;
140 /* Save the current deferred access states and start deferred
141 access checking iff DEFER_P is true. */
143 void
144 push_deferring_access_checks (deferring_kind deferring)
146 /* For context like template instantiation, access checking
147 disabling applies to all nested context. */
148 if (deferred_access_no_check || deferring == dk_no_check)
149 deferred_access_no_check++;
150 else
152 deferred_access e = {NULL, deferring};
153 vec_safe_push (deferred_access_stack, e);
157 /* Save the current deferred access states and start deferred access
158 checking, continuing the set of deferred checks in CHECKS. */
160 void
161 reopen_deferring_access_checks (vec<deferred_access_check, va_gc> * checks)
163 push_deferring_access_checks (dk_deferred);
164 if (!deferred_access_no_check)
165 deferred_access_stack->last().deferred_access_checks = checks;
168 /* Resume deferring access checks again after we stopped doing
169 this previously. */
171 void
172 resume_deferring_access_checks (void)
174 if (!deferred_access_no_check)
175 deferred_access_stack->last().deferring_access_checks_kind = dk_deferred;
178 /* Stop deferring access checks. */
180 void
181 stop_deferring_access_checks (void)
183 if (!deferred_access_no_check)
184 deferred_access_stack->last().deferring_access_checks_kind = dk_no_deferred;
187 /* Discard the current deferred access checks and restore the
188 previous states. */
190 void
191 pop_deferring_access_checks (void)
193 if (deferred_access_no_check)
194 deferred_access_no_check--;
195 else
196 deferred_access_stack->pop ();
199 /* Returns a TREE_LIST representing the deferred checks.
200 The TREE_PURPOSE of each node is the type through which the
201 access occurred; the TREE_VALUE is the declaration named.
204 vec<deferred_access_check, va_gc> *
205 get_deferred_access_checks (void)
207 if (deferred_access_no_check)
208 return NULL;
209 else
210 return (deferred_access_stack->last().deferred_access_checks);
213 /* Take current deferred checks and combine with the
214 previous states if we also defer checks previously.
215 Otherwise perform checks now. */
217 void
218 pop_to_parent_deferring_access_checks (void)
220 if (deferred_access_no_check)
221 deferred_access_no_check--;
222 else
224 vec<deferred_access_check, va_gc> *checks;
225 deferred_access *ptr;
227 checks = (deferred_access_stack->last ().deferred_access_checks);
229 deferred_access_stack->pop ();
230 ptr = &deferred_access_stack->last ();
231 if (ptr->deferring_access_checks_kind == dk_no_deferred)
233 /* Check access. */
234 perform_access_checks (checks, tf_warning_or_error);
236 else
238 /* Merge with parent. */
239 int i, j;
240 deferred_access_check *chk, *probe;
242 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
244 FOR_EACH_VEC_SAFE_ELT (ptr->deferred_access_checks, j, probe)
246 if (probe->binfo == chk->binfo &&
247 probe->decl == chk->decl &&
248 probe->diag_decl == chk->diag_decl)
249 goto found;
251 /* Insert into parent's checks. */
252 vec_safe_push (ptr->deferred_access_checks, *chk);
253 found:;
259 /* Perform the access checks in CHECKS. The TREE_PURPOSE of each node
260 is the BINFO indicating the qualifying scope used to access the
261 DECL node stored in the TREE_VALUE of the node. If CHECKS is empty
262 or we aren't in SFINAE context or all the checks succeed return TRUE,
263 otherwise FALSE. */
265 bool
266 perform_access_checks (vec<deferred_access_check, va_gc> *checks,
267 tsubst_flags_t complain)
269 int i;
270 deferred_access_check *chk;
271 location_t loc = input_location;
272 bool ok = true;
274 if (!checks)
275 return true;
277 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
279 input_location = chk->loc;
280 ok &= enforce_access (chk->binfo, chk->decl, chk->diag_decl, complain);
283 input_location = loc;
284 return (complain & tf_error) ? true : ok;
287 /* Perform the deferred access checks.
289 After performing the checks, we still have to keep the list
290 `deferred_access_stack->deferred_access_checks' since we may want
291 to check access for them again later in a different context.
292 For example:
294 class A {
295 typedef int X;
296 static X a;
298 A::X A::a, x; // No error for `A::a', error for `x'
300 We have to perform deferred access of `A::X', first with `A::a',
301 next with `x'. Return value like perform_access_checks above. */
303 bool
304 perform_deferred_access_checks (tsubst_flags_t complain)
306 return perform_access_checks (get_deferred_access_checks (), complain);
309 /* Defer checking the accessibility of DECL, when looked up in
310 BINFO. DIAG_DECL is the declaration to use to print diagnostics.
311 Return value like perform_access_checks above.
312 If non-NULL, report failures to AFI. */
314 bool
315 perform_or_defer_access_check (tree binfo, tree decl, tree diag_decl,
316 tsubst_flags_t complain,
317 access_failure_info *afi)
319 int i;
320 deferred_access *ptr;
321 deferred_access_check *chk;
324 /* Exit if we are in a context that no access checking is performed.
326 if (deferred_access_no_check)
327 return true;
329 gcc_assert (TREE_CODE (binfo) == TREE_BINFO);
331 ptr = &deferred_access_stack->last ();
333 /* If we are not supposed to defer access checks, just check now. */
334 if (ptr->deferring_access_checks_kind == dk_no_deferred)
336 bool ok = enforce_access (binfo, decl, diag_decl, complain, afi);
337 return (complain & tf_error) ? true : ok;
340 /* See if we are already going to perform this check. */
341 FOR_EACH_VEC_SAFE_ELT (ptr->deferred_access_checks, i, chk)
343 if (chk->decl == decl && chk->binfo == binfo &&
344 chk->diag_decl == diag_decl)
346 return true;
349 /* If not, record the check. */
350 deferred_access_check new_access = {binfo, decl, diag_decl, input_location};
351 vec_safe_push (ptr->deferred_access_checks, new_access);
353 return true;
356 /* Returns nonzero if the current statement is a full expression,
357 i.e. temporaries created during that statement should be destroyed
358 at the end of the statement. */
361 stmts_are_full_exprs_p (void)
363 return current_stmt_tree ()->stmts_are_full_exprs_p;
366 /* T is a statement. Add it to the statement-tree. This is the C++
367 version. The C/ObjC frontends have a slightly different version of
368 this function. */
370 tree
371 add_stmt (tree t)
373 enum tree_code code = TREE_CODE (t);
375 if (EXPR_P (t) && code != LABEL_EXPR)
377 if (!EXPR_HAS_LOCATION (t))
378 SET_EXPR_LOCATION (t, input_location);
380 /* When we expand a statement-tree, we must know whether or not the
381 statements are full-expressions. We record that fact here. */
382 STMT_IS_FULL_EXPR_P (t) = stmts_are_full_exprs_p ();
385 if (code == LABEL_EXPR || code == CASE_LABEL_EXPR)
386 STATEMENT_LIST_HAS_LABEL (cur_stmt_list) = 1;
388 /* Add T to the statement-tree. Non-side-effect statements need to be
389 recorded during statement expressions. */
390 gcc_checking_assert (!stmt_list_stack->is_empty ());
391 append_to_statement_list_force (t, &cur_stmt_list);
393 return t;
396 /* Returns the stmt_tree to which statements are currently being added. */
398 stmt_tree
399 current_stmt_tree (void)
401 return (cfun
402 ? &cfun->language->base.x_stmt_tree
403 : &scope_chain->x_stmt_tree);
406 /* If statements are full expressions, wrap STMT in a CLEANUP_POINT_EXPR. */
408 static tree
409 maybe_cleanup_point_expr (tree expr)
411 if (!processing_template_decl && stmts_are_full_exprs_p ())
412 expr = fold_build_cleanup_point_expr (TREE_TYPE (expr), expr);
413 return expr;
416 /* Like maybe_cleanup_point_expr except have the type of the new expression be
417 void so we don't need to create a temporary variable to hold the inner
418 expression. The reason why we do this is because the original type might be
419 an aggregate and we cannot create a temporary variable for that type. */
421 tree
422 maybe_cleanup_point_expr_void (tree expr)
424 if (!processing_template_decl && stmts_are_full_exprs_p ())
425 expr = fold_build_cleanup_point_expr (void_type_node, expr);
426 return expr;
431 /* Create a declaration statement for the declaration given by the DECL. */
433 void
434 add_decl_expr (tree decl)
436 tree r = build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl);
437 if (DECL_INITIAL (decl)
438 || (DECL_SIZE (decl) && TREE_SIDE_EFFECTS (DECL_SIZE (decl))))
439 r = maybe_cleanup_point_expr_void (r);
440 add_stmt (r);
443 /* Finish a scope. */
445 tree
446 do_poplevel (tree stmt_list)
448 tree block = NULL;
450 if (stmts_are_full_exprs_p ())
451 block = poplevel (kept_level_p (), 1, 0);
453 stmt_list = pop_stmt_list (stmt_list);
455 if (!processing_template_decl)
457 stmt_list = c_build_bind_expr (input_location, block, stmt_list);
458 /* ??? See c_end_compound_stmt re statement expressions. */
461 return stmt_list;
464 /* Begin a new scope. */
466 static tree
467 do_pushlevel (scope_kind sk)
469 tree ret = push_stmt_list ();
470 if (stmts_are_full_exprs_p ())
471 begin_scope (sk, NULL);
472 return ret;
475 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
476 when the current scope is exited. EH_ONLY is true when this is not
477 meant to apply to normal control flow transfer. */
479 void
480 push_cleanup (tree decl, tree cleanup, bool eh_only)
482 tree stmt = build_stmt (input_location, CLEANUP_STMT, NULL, cleanup, decl);
483 CLEANUP_EH_ONLY (stmt) = eh_only;
484 add_stmt (stmt);
485 CLEANUP_BODY (stmt) = push_stmt_list ();
488 /* Simple infinite loop tracking for -Wreturn-type. We keep a stack of all
489 the current loops, represented by 'NULL_TREE' if we've seen a possible
490 exit, and 'error_mark_node' if not. This is currently used only to
491 suppress the warning about a function with no return statements, and
492 therefore we don't bother noting returns as possible exits. We also
493 don't bother with gotos. */
495 static void
496 begin_maybe_infinite_loop (tree cond)
498 /* Only track this while parsing a function, not during instantiation. */
499 if (!cfun || (DECL_TEMPLATE_INSTANTIATION (current_function_decl)
500 && !processing_template_decl))
501 return;
502 bool maybe_infinite = true;
503 if (cond)
505 cond = fold_non_dependent_expr (cond);
506 maybe_infinite = integer_nonzerop (cond);
508 vec_safe_push (cp_function_chain->infinite_loops,
509 maybe_infinite ? error_mark_node : NULL_TREE);
513 /* A break is a possible exit for the current loop. */
515 void
516 break_maybe_infinite_loop (void)
518 if (!cfun)
519 return;
520 cp_function_chain->infinite_loops->last() = NULL_TREE;
523 /* If we reach the end of the loop without seeing a possible exit, we have
524 an infinite loop. */
526 static void
527 end_maybe_infinite_loop (tree cond)
529 if (!cfun || (DECL_TEMPLATE_INSTANTIATION (current_function_decl)
530 && !processing_template_decl))
531 return;
532 tree current = cp_function_chain->infinite_loops->pop();
533 if (current != NULL_TREE)
535 cond = fold_non_dependent_expr (cond);
536 if (integer_nonzerop (cond))
537 current_function_infinite_loop = 1;
542 /* Begin a conditional that might contain a declaration. When generating
543 normal code, we want the declaration to appear before the statement
544 containing the conditional. When generating template code, we want the
545 conditional to be rendered as the raw DECL_EXPR. */
547 static void
548 begin_cond (tree *cond_p)
550 if (processing_template_decl)
551 *cond_p = push_stmt_list ();
554 /* Finish such a conditional. */
556 static void
557 finish_cond (tree *cond_p, tree expr)
559 if (processing_template_decl)
561 tree cond = pop_stmt_list (*cond_p);
563 if (expr == NULL_TREE)
564 /* Empty condition in 'for'. */
565 gcc_assert (empty_expr_stmt_p (cond));
566 else if (check_for_bare_parameter_packs (expr))
567 expr = error_mark_node;
568 else if (!empty_expr_stmt_p (cond))
569 expr = build2 (COMPOUND_EXPR, TREE_TYPE (expr), cond, expr);
571 *cond_p = expr;
574 /* If *COND_P specifies a conditional with a declaration, transform the
575 loop such that
576 while (A x = 42) { }
577 for (; A x = 42;) { }
578 becomes
579 while (true) { A x = 42; if (!x) break; }
580 for (;;) { A x = 42; if (!x) break; }
581 The statement list for BODY will be empty if the conditional did
582 not declare anything. */
584 static void
585 simplify_loop_decl_cond (tree *cond_p, tree body)
587 tree cond, if_stmt;
589 if (!TREE_SIDE_EFFECTS (body))
590 return;
592 cond = *cond_p;
593 *cond_p = boolean_true_node;
595 if_stmt = begin_if_stmt ();
596 cond = cp_build_unary_op (TRUTH_NOT_EXPR, cond, false, tf_warning_or_error);
597 finish_if_stmt_cond (cond, if_stmt);
598 finish_break_stmt ();
599 finish_then_clause (if_stmt);
600 finish_if_stmt (if_stmt);
603 /* Finish a goto-statement. */
605 tree
606 finish_goto_stmt (tree destination)
608 if (identifier_p (destination))
609 destination = lookup_label (destination);
611 /* We warn about unused labels with -Wunused. That means we have to
612 mark the used labels as used. */
613 if (TREE_CODE (destination) == LABEL_DECL)
614 TREE_USED (destination) = 1;
615 else
617 destination = mark_rvalue_use (destination);
618 if (!processing_template_decl)
620 destination = cp_convert (ptr_type_node, destination,
621 tf_warning_or_error);
622 if (error_operand_p (destination))
623 return NULL_TREE;
624 destination
625 = fold_build_cleanup_point_expr (TREE_TYPE (destination),
626 destination);
630 check_goto (destination);
632 add_stmt (build_predict_expr (PRED_GOTO, NOT_TAKEN));
633 return add_stmt (build_stmt (input_location, GOTO_EXPR, destination));
636 /* COND is the condition-expression for an if, while, etc.,
637 statement. Convert it to a boolean value, if appropriate.
638 In addition, verify sequence points if -Wsequence-point is enabled. */
640 static tree
641 maybe_convert_cond (tree cond)
643 /* Empty conditions remain empty. */
644 if (!cond)
645 return NULL_TREE;
647 /* Wait until we instantiate templates before doing conversion. */
648 if (processing_template_decl)
649 return cond;
651 if (warn_sequence_point)
652 verify_sequence_points (cond);
654 /* Do the conversion. */
655 cond = convert_from_reference (cond);
657 if (TREE_CODE (cond) == MODIFY_EXPR
658 && !TREE_NO_WARNING (cond)
659 && warn_parentheses)
661 warning_at (EXPR_LOC_OR_LOC (cond, input_location), OPT_Wparentheses,
662 "suggest parentheses around assignment used as truth value");
663 TREE_NO_WARNING (cond) = 1;
666 return condition_conversion (cond);
669 /* Finish an expression-statement, whose EXPRESSION is as indicated. */
671 tree
672 finish_expr_stmt (tree expr)
674 tree r = NULL_TREE;
675 location_t loc = EXPR_LOCATION (expr);
677 if (expr != NULL_TREE)
679 /* If we ran into a problem, make sure we complained. */
680 gcc_assert (expr != error_mark_node || seen_error ());
682 if (!processing_template_decl)
684 if (warn_sequence_point)
685 verify_sequence_points (expr);
686 expr = convert_to_void (expr, ICV_STATEMENT, tf_warning_or_error);
688 else if (!type_dependent_expression_p (expr))
689 convert_to_void (build_non_dependent_expr (expr), ICV_STATEMENT,
690 tf_warning_or_error);
692 if (check_for_bare_parameter_packs (expr))
693 expr = error_mark_node;
695 /* Simplification of inner statement expressions, compound exprs,
696 etc can result in us already having an EXPR_STMT. */
697 if (TREE_CODE (expr) != CLEANUP_POINT_EXPR)
699 if (TREE_CODE (expr) != EXPR_STMT)
700 expr = build_stmt (loc, EXPR_STMT, expr);
701 expr = maybe_cleanup_point_expr_void (expr);
704 r = add_stmt (expr);
707 return r;
711 /* Begin an if-statement. Returns a newly created IF_STMT if
712 appropriate. */
714 tree
715 begin_if_stmt (void)
717 tree r, scope;
718 scope = do_pushlevel (sk_cond);
719 r = build_stmt (input_location, IF_STMT, NULL_TREE,
720 NULL_TREE, NULL_TREE, scope);
721 current_binding_level->this_entity = r;
722 begin_cond (&IF_COND (r));
723 return r;
726 /* Process the COND of an if-statement, which may be given by
727 IF_STMT. */
729 tree
730 finish_if_stmt_cond (tree cond, tree if_stmt)
732 cond = maybe_convert_cond (cond);
733 if (IF_STMT_CONSTEXPR_P (if_stmt)
734 && !type_dependent_expression_p (cond)
735 && require_constant_expression (cond)
736 && !value_dependent_expression_p (cond))
738 cond = instantiate_non_dependent_expr (cond);
739 cond = cxx_constant_value (cond, NULL_TREE);
741 finish_cond (&IF_COND (if_stmt), cond);
742 add_stmt (if_stmt);
743 THEN_CLAUSE (if_stmt) = push_stmt_list ();
744 return cond;
747 /* Finish the then-clause of an if-statement, which may be given by
748 IF_STMT. */
750 tree
751 finish_then_clause (tree if_stmt)
753 THEN_CLAUSE (if_stmt) = pop_stmt_list (THEN_CLAUSE (if_stmt));
754 return if_stmt;
757 /* Begin the else-clause of an if-statement. */
759 void
760 begin_else_clause (tree if_stmt)
762 ELSE_CLAUSE (if_stmt) = push_stmt_list ();
765 /* Finish the else-clause of an if-statement, which may be given by
766 IF_STMT. */
768 void
769 finish_else_clause (tree if_stmt)
771 ELSE_CLAUSE (if_stmt) = pop_stmt_list (ELSE_CLAUSE (if_stmt));
774 /* Finish an if-statement. */
776 void
777 finish_if_stmt (tree if_stmt)
779 tree scope = IF_SCOPE (if_stmt);
780 IF_SCOPE (if_stmt) = NULL;
781 add_stmt (do_poplevel (scope));
784 /* Begin a while-statement. Returns a newly created WHILE_STMT if
785 appropriate. */
787 tree
788 begin_while_stmt (void)
790 tree r;
791 r = build_stmt (input_location, WHILE_STMT, NULL_TREE, NULL_TREE);
792 add_stmt (r);
793 WHILE_BODY (r) = do_pushlevel (sk_block);
794 begin_cond (&WHILE_COND (r));
795 return r;
798 /* Process the COND of a while-statement, which may be given by
799 WHILE_STMT. */
801 void
802 finish_while_stmt_cond (tree cond, tree while_stmt, bool ivdep,
803 unsigned short unroll)
805 cond = maybe_convert_cond (cond);
806 finish_cond (&WHILE_COND (while_stmt), cond);
807 begin_maybe_infinite_loop (cond);
808 if (ivdep && cond != error_mark_node)
809 WHILE_COND (while_stmt) = build3 (ANNOTATE_EXPR,
810 TREE_TYPE (WHILE_COND (while_stmt)),
811 WHILE_COND (while_stmt),
812 build_int_cst (integer_type_node,
813 annot_expr_ivdep_kind),
814 integer_zero_node);
815 if (unroll && cond != error_mark_node)
816 WHILE_COND (while_stmt) = build3 (ANNOTATE_EXPR,
817 TREE_TYPE (WHILE_COND (while_stmt)),
818 WHILE_COND (while_stmt),
819 build_int_cst (integer_type_node,
820 annot_expr_unroll_kind),
821 build_int_cst (integer_type_node,
822 unroll));
823 simplify_loop_decl_cond (&WHILE_COND (while_stmt), WHILE_BODY (while_stmt));
826 /* Finish a while-statement, which may be given by WHILE_STMT. */
828 void
829 finish_while_stmt (tree while_stmt)
831 end_maybe_infinite_loop (boolean_true_node);
832 WHILE_BODY (while_stmt) = do_poplevel (WHILE_BODY (while_stmt));
835 /* Begin a do-statement. Returns a newly created DO_STMT if
836 appropriate. */
838 tree
839 begin_do_stmt (void)
841 tree r = build_stmt (input_location, DO_STMT, NULL_TREE, NULL_TREE);
842 begin_maybe_infinite_loop (boolean_true_node);
843 add_stmt (r);
844 DO_BODY (r) = push_stmt_list ();
845 return r;
848 /* Finish the body of a do-statement, which may be given by DO_STMT. */
850 void
851 finish_do_body (tree do_stmt)
853 tree body = DO_BODY (do_stmt) = pop_stmt_list (DO_BODY (do_stmt));
855 if (TREE_CODE (body) == STATEMENT_LIST && STATEMENT_LIST_TAIL (body))
856 body = STATEMENT_LIST_TAIL (body)->stmt;
858 if (IS_EMPTY_STMT (body))
859 warning (OPT_Wempty_body,
860 "suggest explicit braces around empty body in %<do%> statement");
863 /* Finish a do-statement, which may be given by DO_STMT, and whose
864 COND is as indicated. */
866 void
867 finish_do_stmt (tree cond, tree do_stmt, bool ivdep, unsigned short unroll)
869 cond = maybe_convert_cond (cond);
870 end_maybe_infinite_loop (cond);
871 if (ivdep && cond != error_mark_node)
872 cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
873 build_int_cst (integer_type_node, annot_expr_ivdep_kind),
874 integer_zero_node);
875 if (unroll && cond != error_mark_node)
876 cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
877 build_int_cst (integer_type_node, annot_expr_unroll_kind),
878 build_int_cst (integer_type_node, unroll));
879 DO_COND (do_stmt) = cond;
882 /* Finish a return-statement. The EXPRESSION returned, if any, is as
883 indicated. */
885 tree
886 finish_return_stmt (tree expr)
888 tree r;
889 bool no_warning;
891 expr = check_return_expr (expr, &no_warning);
893 if (error_operand_p (expr)
894 || (flag_openmp && !check_omp_return ()))
896 /* Suppress -Wreturn-type for this function. */
897 if (warn_return_type)
898 TREE_NO_WARNING (current_function_decl) = true;
899 return error_mark_node;
902 if (!processing_template_decl)
904 if (warn_sequence_point)
905 verify_sequence_points (expr);
907 if (DECL_DESTRUCTOR_P (current_function_decl)
908 || (DECL_CONSTRUCTOR_P (current_function_decl)
909 && targetm.cxx.cdtor_returns_this ()))
911 /* Similarly, all destructors must run destructors for
912 base-classes before returning. So, all returns in a
913 destructor get sent to the DTOR_LABEL; finish_function emits
914 code to return a value there. */
915 return finish_goto_stmt (cdtor_label);
919 r = build_stmt (input_location, RETURN_EXPR, expr);
920 TREE_NO_WARNING (r) |= no_warning;
921 r = maybe_cleanup_point_expr_void (r);
922 r = add_stmt (r);
924 return r;
927 /* Begin the scope of a for-statement or a range-for-statement.
928 Both the returned trees are to be used in a call to
929 begin_for_stmt or begin_range_for_stmt. */
931 tree
932 begin_for_scope (tree *init)
934 tree scope = NULL_TREE;
935 if (flag_new_for_scope)
936 scope = do_pushlevel (sk_for);
938 if (processing_template_decl)
939 *init = push_stmt_list ();
940 else
941 *init = NULL_TREE;
943 return scope;
946 /* Begin a for-statement. Returns a new FOR_STMT.
947 SCOPE and INIT should be the return of begin_for_scope,
948 or both NULL_TREE */
950 tree
951 begin_for_stmt (tree scope, tree init)
953 tree r;
955 r = build_stmt (input_location, FOR_STMT, NULL_TREE, NULL_TREE,
956 NULL_TREE, NULL_TREE, NULL_TREE);
958 if (scope == NULL_TREE)
960 gcc_assert (!init || !flag_new_for_scope);
961 if (!init)
962 scope = begin_for_scope (&init);
964 FOR_INIT_STMT (r) = init;
965 FOR_SCOPE (r) = scope;
967 return r;
970 /* Finish the init-statement of a for-statement, which may be
971 given by FOR_STMT. */
973 void
974 finish_init_stmt (tree for_stmt)
976 if (processing_template_decl)
977 FOR_INIT_STMT (for_stmt) = pop_stmt_list (FOR_INIT_STMT (for_stmt));
978 add_stmt (for_stmt);
979 FOR_BODY (for_stmt) = do_pushlevel (sk_block);
980 begin_cond (&FOR_COND (for_stmt));
983 /* Finish the COND of a for-statement, which may be given by
984 FOR_STMT. */
986 void
987 finish_for_cond (tree cond, tree for_stmt, bool ivdep, unsigned short unroll)
989 cond = maybe_convert_cond (cond);
990 finish_cond (&FOR_COND (for_stmt), cond);
991 begin_maybe_infinite_loop (cond);
992 if (ivdep && cond != error_mark_node)
993 FOR_COND (for_stmt) = build3 (ANNOTATE_EXPR,
994 TREE_TYPE (FOR_COND (for_stmt)),
995 FOR_COND (for_stmt),
996 build_int_cst (integer_type_node,
997 annot_expr_ivdep_kind),
998 integer_zero_node);
999 if (unroll && cond != error_mark_node)
1000 FOR_COND (for_stmt) = build3 (ANNOTATE_EXPR,
1001 TREE_TYPE (FOR_COND (for_stmt)),
1002 FOR_COND (for_stmt),
1003 build_int_cst (integer_type_node,
1004 annot_expr_unroll_kind),
1005 build_int_cst (integer_type_node,
1006 unroll));
1007 simplify_loop_decl_cond (&FOR_COND (for_stmt), FOR_BODY (for_stmt));
1010 /* Finish the increment-EXPRESSION in a for-statement, which may be
1011 given by FOR_STMT. */
1013 void
1014 finish_for_expr (tree expr, tree for_stmt)
1016 if (!expr)
1017 return;
1018 /* If EXPR is an overloaded function, issue an error; there is no
1019 context available to use to perform overload resolution. */
1020 if (type_unknown_p (expr))
1022 cxx_incomplete_type_error (expr, TREE_TYPE (expr));
1023 expr = error_mark_node;
1025 if (!processing_template_decl)
1027 if (warn_sequence_point)
1028 verify_sequence_points (expr);
1029 expr = convert_to_void (expr, ICV_THIRD_IN_FOR,
1030 tf_warning_or_error);
1032 else if (!type_dependent_expression_p (expr))
1033 convert_to_void (build_non_dependent_expr (expr), ICV_THIRD_IN_FOR,
1034 tf_warning_or_error);
1035 expr = maybe_cleanup_point_expr_void (expr);
1036 if (check_for_bare_parameter_packs (expr))
1037 expr = error_mark_node;
1038 FOR_EXPR (for_stmt) = expr;
1041 /* Finish the body of a for-statement, which may be given by
1042 FOR_STMT. The increment-EXPR for the loop must be
1043 provided.
1044 It can also finish RANGE_FOR_STMT. */
1046 void
1047 finish_for_stmt (tree for_stmt)
1049 end_maybe_infinite_loop (boolean_true_node);
1051 if (TREE_CODE (for_stmt) == RANGE_FOR_STMT)
1052 RANGE_FOR_BODY (for_stmt) = do_poplevel (RANGE_FOR_BODY (for_stmt));
1053 else
1054 FOR_BODY (for_stmt) = do_poplevel (FOR_BODY (for_stmt));
1056 /* Pop the scope for the body of the loop. */
1057 if (flag_new_for_scope)
1059 tree scope;
1060 tree *scope_ptr = (TREE_CODE (for_stmt) == RANGE_FOR_STMT
1061 ? &RANGE_FOR_SCOPE (for_stmt)
1062 : &FOR_SCOPE (for_stmt));
1063 scope = *scope_ptr;
1064 *scope_ptr = NULL;
1065 add_stmt (do_poplevel (scope));
1069 /* Begin a range-for-statement. Returns a new RANGE_FOR_STMT.
1070 SCOPE and INIT should be the return of begin_for_scope,
1071 or both NULL_TREE .
1072 To finish it call finish_for_stmt(). */
1074 tree
1075 begin_range_for_stmt (tree scope, tree init)
1077 tree r;
1079 begin_maybe_infinite_loop (boolean_false_node);
1081 r = build_stmt (input_location, RANGE_FOR_STMT,
1082 NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE);
1084 if (scope == NULL_TREE)
1086 gcc_assert (!init || !flag_new_for_scope);
1087 if (!init)
1088 scope = begin_for_scope (&init);
1091 /* RANGE_FOR_STMTs do not use nor save the init tree, so we
1092 pop it now. */
1093 if (init)
1094 pop_stmt_list (init);
1095 RANGE_FOR_SCOPE (r) = scope;
1097 return r;
1100 /* Finish the head of a range-based for statement, which may
1101 be given by RANGE_FOR_STMT. DECL must be the declaration
1102 and EXPR must be the loop expression. */
1104 void
1105 finish_range_for_decl (tree range_for_stmt, tree decl, tree expr)
1107 RANGE_FOR_DECL (range_for_stmt) = decl;
1108 RANGE_FOR_EXPR (range_for_stmt) = expr;
1109 add_stmt (range_for_stmt);
1110 RANGE_FOR_BODY (range_for_stmt) = do_pushlevel (sk_block);
1113 /* Finish a break-statement. */
1115 tree
1116 finish_break_stmt (void)
1118 /* In switch statements break is sometimes stylistically used after
1119 a return statement. This can lead to spurious warnings about
1120 control reaching the end of a non-void function when it is
1121 inlined. Note that we are calling block_may_fallthru with
1122 language specific tree nodes; this works because
1123 block_may_fallthru returns true when given something it does not
1124 understand. */
1125 if (!block_may_fallthru (cur_stmt_list))
1126 return void_node;
1127 note_break_stmt ();
1128 return add_stmt (build_stmt (input_location, BREAK_STMT));
1131 /* Finish a continue-statement. */
1133 tree
1134 finish_continue_stmt (void)
1136 return add_stmt (build_stmt (input_location, CONTINUE_STMT));
1139 /* Begin a switch-statement. Returns a new SWITCH_STMT if
1140 appropriate. */
1142 tree
1143 begin_switch_stmt (void)
1145 tree r, scope;
1147 scope = do_pushlevel (sk_cond);
1148 r = build_stmt (input_location, SWITCH_STMT, NULL_TREE, NULL_TREE, NULL_TREE, scope);
1150 begin_cond (&SWITCH_STMT_COND (r));
1152 return r;
1155 /* Finish the cond of a switch-statement. */
1157 void
1158 finish_switch_cond (tree cond, tree switch_stmt)
1160 tree orig_type = NULL;
1162 if (!processing_template_decl)
1164 /* Convert the condition to an integer or enumeration type. */
1165 cond = build_expr_type_conversion (WANT_INT | WANT_ENUM, cond, true);
1166 if (cond == NULL_TREE)
1168 error ("switch quantity not an integer");
1169 cond = error_mark_node;
1171 /* We want unlowered type here to handle enum bit-fields. */
1172 orig_type = unlowered_expr_type (cond);
1173 if (TREE_CODE (orig_type) != ENUMERAL_TYPE)
1174 orig_type = TREE_TYPE (cond);
1175 if (cond != error_mark_node)
1177 /* [stmt.switch]
1179 Integral promotions are performed. */
1180 cond = perform_integral_promotions (cond);
1181 cond = maybe_cleanup_point_expr (cond);
1184 if (check_for_bare_parameter_packs (cond))
1185 cond = error_mark_node;
1186 else if (!processing_template_decl && warn_sequence_point)
1187 verify_sequence_points (cond);
1189 finish_cond (&SWITCH_STMT_COND (switch_stmt), cond);
1190 SWITCH_STMT_TYPE (switch_stmt) = orig_type;
1191 add_stmt (switch_stmt);
1192 push_switch (switch_stmt);
1193 SWITCH_STMT_BODY (switch_stmt) = push_stmt_list ();
1196 /* Finish the body of a switch-statement, which may be given by
1197 SWITCH_STMT. The COND to switch on is indicated. */
1199 void
1200 finish_switch_stmt (tree switch_stmt)
1202 tree scope;
1204 SWITCH_STMT_BODY (switch_stmt) =
1205 pop_stmt_list (SWITCH_STMT_BODY (switch_stmt));
1206 pop_switch ();
1208 scope = SWITCH_STMT_SCOPE (switch_stmt);
1209 SWITCH_STMT_SCOPE (switch_stmt) = NULL;
1210 add_stmt (do_poplevel (scope));
1213 /* Begin a try-block. Returns a newly-created TRY_BLOCK if
1214 appropriate. */
1216 tree
1217 begin_try_block (void)
1219 tree r = build_stmt (input_location, TRY_BLOCK, NULL_TREE, NULL_TREE);
1220 add_stmt (r);
1221 TRY_STMTS (r) = push_stmt_list ();
1222 return r;
1225 /* Likewise, for a function-try-block. The block returned in
1226 *COMPOUND_STMT is an artificial outer scope, containing the
1227 function-try-block. */
1229 tree
1230 begin_function_try_block (tree *compound_stmt)
1232 tree r;
1233 /* This outer scope does not exist in the C++ standard, but we need
1234 a place to put __FUNCTION__ and similar variables. */
1235 *compound_stmt = begin_compound_stmt (0);
1236 r = begin_try_block ();
1237 FN_TRY_BLOCK_P (r) = 1;
1238 return r;
1241 /* Finish a try-block, which may be given by TRY_BLOCK. */
1243 void
1244 finish_try_block (tree try_block)
1246 TRY_STMTS (try_block) = pop_stmt_list (TRY_STMTS (try_block));
1247 TRY_HANDLERS (try_block) = push_stmt_list ();
1250 /* Finish the body of a cleanup try-block, which may be given by
1251 TRY_BLOCK. */
1253 void
1254 finish_cleanup_try_block (tree try_block)
1256 TRY_STMTS (try_block) = pop_stmt_list (TRY_STMTS (try_block));
1259 /* Finish an implicitly generated try-block, with a cleanup is given
1260 by CLEANUP. */
1262 void
1263 finish_cleanup (tree cleanup, tree try_block)
1265 TRY_HANDLERS (try_block) = cleanup;
1266 CLEANUP_P (try_block) = 1;
1269 /* Likewise, for a function-try-block. */
1271 void
1272 finish_function_try_block (tree try_block)
1274 finish_try_block (try_block);
1275 /* FIXME : something queer about CTOR_INITIALIZER somehow following
1276 the try block, but moving it inside. */
1277 in_function_try_handler = 1;
1280 /* Finish a handler-sequence for a try-block, which may be given by
1281 TRY_BLOCK. */
1283 void
1284 finish_handler_sequence (tree try_block)
1286 TRY_HANDLERS (try_block) = pop_stmt_list (TRY_HANDLERS (try_block));
1287 check_handlers (TRY_HANDLERS (try_block));
1290 /* Finish the handler-seq for a function-try-block, given by
1291 TRY_BLOCK. COMPOUND_STMT is the outer block created by
1292 begin_function_try_block. */
1294 void
1295 finish_function_handler_sequence (tree try_block, tree compound_stmt)
1297 in_function_try_handler = 0;
1298 finish_handler_sequence (try_block);
1299 finish_compound_stmt (compound_stmt);
1302 /* Begin a handler. Returns a HANDLER if appropriate. */
1304 tree
1305 begin_handler (void)
1307 tree r;
1309 r = build_stmt (input_location, HANDLER, NULL_TREE, NULL_TREE);
1310 add_stmt (r);
1312 /* Create a binding level for the eh_info and the exception object
1313 cleanup. */
1314 HANDLER_BODY (r) = do_pushlevel (sk_catch);
1316 return r;
1319 /* Finish the handler-parameters for a handler, which may be given by
1320 HANDLER. DECL is the declaration for the catch parameter, or NULL
1321 if this is a `catch (...)' clause. */
1323 void
1324 finish_handler_parms (tree decl, tree handler)
1326 tree type = NULL_TREE;
1327 if (processing_template_decl)
1329 if (decl)
1331 decl = pushdecl (decl);
1332 decl = push_template_decl (decl);
1333 HANDLER_PARMS (handler) = decl;
1334 type = TREE_TYPE (decl);
1337 else
1339 type = expand_start_catch_block (decl);
1340 if (warn_catch_value
1341 && type != NULL_TREE
1342 && type != error_mark_node
1343 && TREE_CODE (TREE_TYPE (decl)) != REFERENCE_TYPE)
1345 tree orig_type = TREE_TYPE (decl);
1346 if (CLASS_TYPE_P (orig_type))
1348 if (TYPE_POLYMORPHIC_P (orig_type))
1349 warning (OPT_Wcatch_value_,
1350 "catching polymorphic type %q#T by value", orig_type);
1351 else if (warn_catch_value > 1)
1352 warning (OPT_Wcatch_value_,
1353 "catching type %q#T by value", orig_type);
1355 else if (warn_catch_value > 2)
1356 warning (OPT_Wcatch_value_,
1357 "catching non-reference type %q#T", orig_type);
1360 HANDLER_TYPE (handler) = type;
1363 /* Finish a handler, which may be given by HANDLER. The BLOCKs are
1364 the return value from the matching call to finish_handler_parms. */
1366 void
1367 finish_handler (tree handler)
1369 if (!processing_template_decl)
1370 expand_end_catch_block ();
1371 HANDLER_BODY (handler) = do_poplevel (HANDLER_BODY (handler));
1374 /* Begin a compound statement. FLAGS contains some bits that control the
1375 behavior and context. If BCS_NO_SCOPE is set, the compound statement
1376 does not define a scope. If BCS_FN_BODY is set, this is the outermost
1377 block of a function. If BCS_TRY_BLOCK is set, this is the block
1378 created on behalf of a TRY statement. Returns a token to be passed to
1379 finish_compound_stmt. */
1381 tree
1382 begin_compound_stmt (unsigned int flags)
1384 tree r;
1386 if (flags & BCS_NO_SCOPE)
1388 r = push_stmt_list ();
1389 STATEMENT_LIST_NO_SCOPE (r) = 1;
1391 /* Normally, we try hard to keep the BLOCK for a statement-expression.
1392 But, if it's a statement-expression with a scopeless block, there's
1393 nothing to keep, and we don't want to accidentally keep a block
1394 *inside* the scopeless block. */
1395 keep_next_level (false);
1397 else
1399 scope_kind sk = sk_block;
1400 if (flags & BCS_TRY_BLOCK)
1401 sk = sk_try;
1402 else if (flags & BCS_TRANSACTION)
1403 sk = sk_transaction;
1404 r = do_pushlevel (sk);
1407 /* When processing a template, we need to remember where the braces were,
1408 so that we can set up identical scopes when instantiating the template
1409 later. BIND_EXPR is a handy candidate for this.
1410 Note that do_poplevel won't create a BIND_EXPR itself here (and thus
1411 result in nested BIND_EXPRs), since we don't build BLOCK nodes when
1412 processing templates. */
1413 if (processing_template_decl)
1415 r = build3 (BIND_EXPR, NULL, NULL, r, NULL);
1416 BIND_EXPR_TRY_BLOCK (r) = (flags & BCS_TRY_BLOCK) != 0;
1417 BIND_EXPR_BODY_BLOCK (r) = (flags & BCS_FN_BODY) != 0;
1418 TREE_SIDE_EFFECTS (r) = 1;
1421 return r;
1424 /* Finish a compound-statement, which is given by STMT. */
1426 void
1427 finish_compound_stmt (tree stmt)
1429 if (TREE_CODE (stmt) == BIND_EXPR)
1431 tree body = do_poplevel (BIND_EXPR_BODY (stmt));
1432 /* If the STATEMENT_LIST is empty and this BIND_EXPR isn't special,
1433 discard the BIND_EXPR so it can be merged with the containing
1434 STATEMENT_LIST. */
1435 if (TREE_CODE (body) == STATEMENT_LIST
1436 && STATEMENT_LIST_HEAD (body) == NULL
1437 && !BIND_EXPR_BODY_BLOCK (stmt)
1438 && !BIND_EXPR_TRY_BLOCK (stmt))
1439 stmt = body;
1440 else
1441 BIND_EXPR_BODY (stmt) = body;
1443 else if (STATEMENT_LIST_NO_SCOPE (stmt))
1444 stmt = pop_stmt_list (stmt);
1445 else
1447 /* Destroy any ObjC "super" receivers that may have been
1448 created. */
1449 objc_clear_super_receiver ();
1451 stmt = do_poplevel (stmt);
1454 /* ??? See c_end_compound_stmt wrt statement expressions. */
1455 add_stmt (stmt);
1458 /* Finish an asm-statement, whose components are a STRING, some
1459 OUTPUT_OPERANDS, some INPUT_OPERANDS, some CLOBBERS and some
1460 LABELS. Also note whether the asm-statement should be
1461 considered volatile. */
1463 tree
1464 finish_asm_stmt (int volatile_p, tree string, tree output_operands,
1465 tree input_operands, tree clobbers, tree labels)
1467 tree r;
1468 tree t;
1469 int ninputs = list_length (input_operands);
1470 int noutputs = list_length (output_operands);
1472 if (!processing_template_decl)
1474 const char *constraint;
1475 const char **oconstraints;
1476 bool allows_mem, allows_reg, is_inout;
1477 tree operand;
1478 int i;
1480 oconstraints = XALLOCAVEC (const char *, noutputs);
1482 string = resolve_asm_operand_names (string, output_operands,
1483 input_operands, labels);
1485 for (i = 0, t = output_operands; t; t = TREE_CHAIN (t), ++i)
1487 operand = TREE_VALUE (t);
1489 /* ??? Really, this should not be here. Users should be using a
1490 proper lvalue, dammit. But there's a long history of using
1491 casts in the output operands. In cases like longlong.h, this
1492 becomes a primitive form of typechecking -- if the cast can be
1493 removed, then the output operand had a type of the proper width;
1494 otherwise we'll get an error. Gross, but ... */
1495 STRIP_NOPS (operand);
1497 operand = mark_lvalue_use (operand);
1499 if (!lvalue_or_else (operand, lv_asm, tf_warning_or_error))
1500 operand = error_mark_node;
1502 if (operand != error_mark_node
1503 && (TREE_READONLY (operand)
1504 || CP_TYPE_CONST_P (TREE_TYPE (operand))
1505 /* Functions are not modifiable, even though they are
1506 lvalues. */
1507 || TREE_CODE (TREE_TYPE (operand)) == FUNCTION_TYPE
1508 || TREE_CODE (TREE_TYPE (operand)) == METHOD_TYPE
1509 /* If it's an aggregate and any field is const, then it is
1510 effectively const. */
1511 || (CLASS_TYPE_P (TREE_TYPE (operand))
1512 && C_TYPE_FIELDS_READONLY (TREE_TYPE (operand)))))
1513 cxx_readonly_error (operand, lv_asm);
1515 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
1516 oconstraints[i] = constraint;
1518 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
1519 &allows_mem, &allows_reg, &is_inout))
1521 /* If the operand is going to end up in memory,
1522 mark it addressable. */
1523 if (!allows_reg && !cxx_mark_addressable (operand))
1524 operand = error_mark_node;
1526 else
1527 operand = error_mark_node;
1529 TREE_VALUE (t) = operand;
1532 for (i = 0, t = input_operands; t; ++i, t = TREE_CHAIN (t))
1534 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
1535 bool constraint_parsed
1536 = parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
1537 oconstraints, &allows_mem, &allows_reg);
1538 /* If the operand is going to end up in memory, don't call
1539 decay_conversion. */
1540 if (constraint_parsed && !allows_reg && allows_mem)
1541 operand = mark_lvalue_use (TREE_VALUE (t));
1542 else
1543 operand = decay_conversion (TREE_VALUE (t), tf_warning_or_error);
1545 /* If the type of the operand hasn't been determined (e.g.,
1546 because it involves an overloaded function), then issue
1547 an error message. There's no context available to
1548 resolve the overloading. */
1549 if (TREE_TYPE (operand) == unknown_type_node)
1551 error ("type of asm operand %qE could not be determined",
1552 TREE_VALUE (t));
1553 operand = error_mark_node;
1556 if (constraint_parsed)
1558 /* If the operand is going to end up in memory,
1559 mark it addressable. */
1560 if (!allows_reg && allows_mem)
1562 /* Strip the nops as we allow this case. FIXME, this really
1563 should be rejected or made deprecated. */
1564 STRIP_NOPS (operand);
1565 if (!cxx_mark_addressable (operand))
1566 operand = error_mark_node;
1568 else if (!allows_reg && !allows_mem)
1570 /* If constraint allows neither register nor memory,
1571 try harder to get a constant. */
1572 tree constop = maybe_constant_value (operand);
1573 if (TREE_CONSTANT (constop))
1574 operand = constop;
1577 else
1578 operand = error_mark_node;
1580 TREE_VALUE (t) = operand;
1584 r = build_stmt (input_location, ASM_EXPR, string,
1585 output_operands, input_operands,
1586 clobbers, labels);
1587 ASM_VOLATILE_P (r) = volatile_p || noutputs == 0;
1588 r = maybe_cleanup_point_expr_void (r);
1589 return add_stmt (r);
1592 /* Finish a label with the indicated NAME. Returns the new label. */
1594 tree
1595 finish_label_stmt (tree name)
1597 tree decl = define_label (input_location, name);
1599 if (decl == error_mark_node)
1600 return error_mark_node;
1602 add_stmt (build_stmt (input_location, LABEL_EXPR, decl));
1604 return decl;
1607 /* Finish a series of declarations for local labels. G++ allows users
1608 to declare "local" labels, i.e., labels with scope. This extension
1609 is useful when writing code involving statement-expressions. */
1611 void
1612 finish_label_decl (tree name)
1614 if (!at_function_scope_p ())
1616 error ("__label__ declarations are only allowed in function scopes");
1617 return;
1620 add_decl_expr (declare_local_label (name));
1623 /* When DECL goes out of scope, make sure that CLEANUP is executed. */
1625 void
1626 finish_decl_cleanup (tree decl, tree cleanup)
1628 push_cleanup (decl, cleanup, false);
1631 /* If the current scope exits with an exception, run CLEANUP. */
1633 void
1634 finish_eh_cleanup (tree cleanup)
1636 push_cleanup (NULL, cleanup, true);
1639 /* The MEM_INITS is a list of mem-initializers, in reverse of the
1640 order they were written by the user. Each node is as for
1641 emit_mem_initializers. */
1643 void
1644 finish_mem_initializers (tree mem_inits)
1646 /* Reorder the MEM_INITS so that they are in the order they appeared
1647 in the source program. */
1648 mem_inits = nreverse (mem_inits);
1650 if (processing_template_decl)
1652 tree mem;
1654 for (mem = mem_inits; mem; mem = TREE_CHAIN (mem))
1656 /* If the TREE_PURPOSE is a TYPE_PACK_EXPANSION, skip the
1657 check for bare parameter packs in the TREE_VALUE, because
1658 any parameter packs in the TREE_VALUE have already been
1659 bound as part of the TREE_PURPOSE. See
1660 make_pack_expansion for more information. */
1661 if (TREE_CODE (TREE_PURPOSE (mem)) != TYPE_PACK_EXPANSION
1662 && check_for_bare_parameter_packs (TREE_VALUE (mem)))
1663 TREE_VALUE (mem) = error_mark_node;
1666 add_stmt (build_min_nt_loc (UNKNOWN_LOCATION,
1667 CTOR_INITIALIZER, mem_inits));
1669 else
1670 emit_mem_initializers (mem_inits);
1673 /* Obfuscate EXPR if it looks like an id-expression or member access so
1674 that the call to finish_decltype in do_auto_deduction will give the
1675 right result. */
1677 tree
1678 force_paren_expr (tree expr)
1680 /* This is only needed for decltype(auto) in C++14. */
1681 if (cxx_dialect < cxx14)
1682 return expr;
1684 /* If we're in unevaluated context, we can't be deducing a
1685 return/initializer type, so we don't need to mess with this. */
1686 if (cp_unevaluated_operand)
1687 return expr;
1689 if (!DECL_P (expr) && TREE_CODE (expr) != COMPONENT_REF
1690 && TREE_CODE (expr) != SCOPE_REF)
1691 return expr;
1693 if (TREE_CODE (expr) == COMPONENT_REF
1694 || TREE_CODE (expr) == SCOPE_REF)
1695 REF_PARENTHESIZED_P (expr) = true;
1696 else if (processing_template_decl)
1697 expr = build1 (PAREN_EXPR, TREE_TYPE (expr), expr);
1698 else if (VAR_P (expr) && DECL_HARD_REGISTER (expr))
1699 /* We can't bind a hard register variable to a reference. */;
1700 else
1702 cp_lvalue_kind kind = lvalue_kind (expr);
1703 if ((kind & ~clk_class) != clk_none)
1705 tree type = unlowered_expr_type (expr);
1706 bool rval = !!(kind & clk_rvalueref);
1707 type = cp_build_reference_type (type, rval);
1708 /* This inhibits warnings in, eg, cxx_mark_addressable
1709 (c++/60955). */
1710 warning_sentinel s (extra_warnings);
1711 expr = build_static_cast (type, expr, tf_error);
1712 if (expr != error_mark_node)
1713 REF_PARENTHESIZED_P (expr) = true;
1717 return expr;
1720 /* If T is an id-expression obfuscated by force_paren_expr, undo the
1721 obfuscation and return the underlying id-expression. Otherwise
1722 return T. */
1724 tree
1725 maybe_undo_parenthesized_ref (tree t)
1727 if (cxx_dialect < cxx14)
1728 return t;
1730 if (INDIRECT_REF_P (t) && REF_PARENTHESIZED_P (t))
1732 t = TREE_OPERAND (t, 0);
1733 while (TREE_CODE (t) == NON_LVALUE_EXPR
1734 || TREE_CODE (t) == NOP_EXPR)
1735 t = TREE_OPERAND (t, 0);
1737 gcc_assert (TREE_CODE (t) == ADDR_EXPR
1738 || TREE_CODE (t) == STATIC_CAST_EXPR);
1739 t = TREE_OPERAND (t, 0);
1741 else if (TREE_CODE (t) == PAREN_EXPR)
1742 t = TREE_OPERAND (t, 0);
1744 return t;
1747 /* Finish a parenthesized expression EXPR. */
1749 cp_expr
1750 finish_parenthesized_expr (cp_expr expr)
1752 if (EXPR_P (expr))
1753 /* This inhibits warnings in c_common_truthvalue_conversion. */
1754 TREE_NO_WARNING (expr) = 1;
1756 if (TREE_CODE (expr) == OFFSET_REF
1757 || TREE_CODE (expr) == SCOPE_REF)
1758 /* [expr.unary.op]/3 The qualified id of a pointer-to-member must not be
1759 enclosed in parentheses. */
1760 PTRMEM_OK_P (expr) = 0;
1762 if (TREE_CODE (expr) == STRING_CST)
1763 PAREN_STRING_LITERAL_P (expr) = 1;
1765 expr = cp_expr (force_paren_expr (expr), expr.get_location ());
1767 return expr;
1770 /* Finish a reference to a non-static data member (DECL) that is not
1771 preceded by `.' or `->'. */
1773 tree
1774 finish_non_static_data_member (tree decl, tree object, tree qualifying_scope)
1776 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
1777 bool try_omp_private = !object && omp_private_member_map;
1778 tree ret;
1780 if (!object)
1782 tree scope = qualifying_scope;
1783 if (scope == NULL_TREE)
1784 scope = context_for_name_lookup (decl);
1785 object = maybe_dummy_object (scope, NULL);
1788 object = maybe_resolve_dummy (object, true);
1789 if (object == error_mark_node)
1790 return error_mark_node;
1792 /* DR 613/850: Can use non-static data members without an associated
1793 object in sizeof/decltype/alignof. */
1794 if (is_dummy_object (object) && cp_unevaluated_operand == 0
1795 && (!processing_template_decl || !current_class_ref))
1797 if (current_function_decl
1798 && DECL_STATIC_FUNCTION_P (current_function_decl))
1799 error ("invalid use of member %qD in static member function", decl);
1800 else
1801 error ("invalid use of non-static data member %qD", decl);
1802 inform (DECL_SOURCE_LOCATION (decl), "declared here");
1804 return error_mark_node;
1807 if (current_class_ptr)
1808 TREE_USED (current_class_ptr) = 1;
1809 if (processing_template_decl && !qualifying_scope)
1811 tree type = TREE_TYPE (decl);
1813 if (TREE_CODE (type) == REFERENCE_TYPE)
1814 /* Quals on the object don't matter. */;
1815 else if (PACK_EXPANSION_P (type))
1816 /* Don't bother trying to represent this. */
1817 type = NULL_TREE;
1818 else
1820 /* Set the cv qualifiers. */
1821 int quals = cp_type_quals (TREE_TYPE (object));
1823 if (DECL_MUTABLE_P (decl))
1824 quals &= ~TYPE_QUAL_CONST;
1826 quals |= cp_type_quals (TREE_TYPE (decl));
1827 type = cp_build_qualified_type (type, quals);
1830 ret = (convert_from_reference
1831 (build_min (COMPONENT_REF, type, object, decl, NULL_TREE)));
1833 /* If PROCESSING_TEMPLATE_DECL is nonzero here, then
1834 QUALIFYING_SCOPE is also non-null. Wrap this in a SCOPE_REF
1835 for now. */
1836 else if (processing_template_decl)
1837 ret = build_qualified_name (TREE_TYPE (decl),
1838 qualifying_scope,
1839 decl,
1840 /*template_p=*/false);
1841 else
1843 tree access_type = TREE_TYPE (object);
1845 perform_or_defer_access_check (TYPE_BINFO (access_type), decl,
1846 decl, tf_warning_or_error);
1848 /* If the data member was named `C::M', convert `*this' to `C'
1849 first. */
1850 if (qualifying_scope)
1852 tree binfo = NULL_TREE;
1853 object = build_scoped_ref (object, qualifying_scope,
1854 &binfo);
1857 ret = build_class_member_access_expr (object, decl,
1858 /*access_path=*/NULL_TREE,
1859 /*preserve_reference=*/false,
1860 tf_warning_or_error);
1862 if (try_omp_private)
1864 tree *v = omp_private_member_map->get (decl);
1865 if (v)
1866 ret = convert_from_reference (*v);
1868 return ret;
1871 /* If we are currently parsing a template and we encountered a typedef
1872 TYPEDEF_DECL that is being accessed though CONTEXT, this function
1873 adds the typedef to a list tied to the current template.
1874 At template instantiation time, that list is walked and access check
1875 performed for each typedef.
1876 LOCATION is the location of the usage point of TYPEDEF_DECL. */
1878 void
1879 add_typedef_to_current_template_for_access_check (tree typedef_decl,
1880 tree context,
1881 location_t location)
1883 tree template_info = NULL;
1884 tree cs = current_scope ();
1886 if (!is_typedef_decl (typedef_decl)
1887 || !context
1888 || !CLASS_TYPE_P (context)
1889 || !cs)
1890 return;
1892 if (CLASS_TYPE_P (cs) || TREE_CODE (cs) == FUNCTION_DECL)
1893 template_info = get_template_info (cs);
1895 if (template_info
1896 && TI_TEMPLATE (template_info)
1897 && !currently_open_class (context))
1898 append_type_to_template_for_access_check (cs, typedef_decl,
1899 context, location);
1902 /* DECL was the declaration to which a qualified-id resolved. Issue
1903 an error message if it is not accessible. If OBJECT_TYPE is
1904 non-NULL, we have just seen `x->' or `x.' and OBJECT_TYPE is the
1905 type of `*x', or `x', respectively. If the DECL was named as
1906 `A::B' then NESTED_NAME_SPECIFIER is `A'. */
1908 void
1909 check_accessibility_of_qualified_id (tree decl,
1910 tree object_type,
1911 tree nested_name_specifier)
1913 tree scope;
1914 tree qualifying_type = NULL_TREE;
1916 /* If we are parsing a template declaration and if decl is a typedef,
1917 add it to a list tied to the template.
1918 At template instantiation time, that list will be walked and
1919 access check performed. */
1920 add_typedef_to_current_template_for_access_check (decl,
1921 nested_name_specifier
1922 ? nested_name_specifier
1923 : DECL_CONTEXT (decl),
1924 input_location);
1926 /* If we're not checking, return immediately. */
1927 if (deferred_access_no_check)
1928 return;
1930 /* Determine the SCOPE of DECL. */
1931 scope = context_for_name_lookup (decl);
1932 /* If the SCOPE is not a type, then DECL is not a member. */
1933 if (!TYPE_P (scope))
1934 return;
1935 /* Compute the scope through which DECL is being accessed. */
1936 if (object_type
1937 /* OBJECT_TYPE might not be a class type; consider:
1939 class A { typedef int I; };
1940 I *p;
1941 p->A::I::~I();
1943 In this case, we will have "A::I" as the DECL, but "I" as the
1944 OBJECT_TYPE. */
1945 && CLASS_TYPE_P (object_type)
1946 && DERIVED_FROM_P (scope, object_type))
1947 /* If we are processing a `->' or `.' expression, use the type of the
1948 left-hand side. */
1949 qualifying_type = object_type;
1950 else if (nested_name_specifier)
1952 /* If the reference is to a non-static member of the
1953 current class, treat it as if it were referenced through
1954 `this'. */
1955 tree ct;
1956 if (DECL_NONSTATIC_MEMBER_P (decl)
1957 && current_class_ptr
1958 && DERIVED_FROM_P (scope, ct = current_nonlambda_class_type ()))
1959 qualifying_type = ct;
1960 /* Otherwise, use the type indicated by the
1961 nested-name-specifier. */
1962 else
1963 qualifying_type = nested_name_specifier;
1965 else
1966 /* Otherwise, the name must be from the current class or one of
1967 its bases. */
1968 qualifying_type = currently_open_derived_class (scope);
1970 if (qualifying_type
1971 /* It is possible for qualifying type to be a TEMPLATE_TYPE_PARM
1972 or similar in a default argument value. */
1973 && CLASS_TYPE_P (qualifying_type)
1974 && !dependent_type_p (qualifying_type))
1975 perform_or_defer_access_check (TYPE_BINFO (qualifying_type), decl,
1976 decl, tf_warning_or_error);
1979 /* EXPR is the result of a qualified-id. The QUALIFYING_CLASS was the
1980 class named to the left of the "::" operator. DONE is true if this
1981 expression is a complete postfix-expression; it is false if this
1982 expression is followed by '->', '[', '(', etc. ADDRESS_P is true
1983 iff this expression is the operand of '&'. TEMPLATE_P is true iff
1984 the qualified-id was of the form "A::template B". TEMPLATE_ARG_P
1985 is true iff this qualified name appears as a template argument. */
1987 tree
1988 finish_qualified_id_expr (tree qualifying_class,
1989 tree expr,
1990 bool done,
1991 bool address_p,
1992 bool template_p,
1993 bool template_arg_p,
1994 tsubst_flags_t complain)
1996 gcc_assert (TYPE_P (qualifying_class));
1998 if (error_operand_p (expr))
1999 return error_mark_node;
2001 if ((DECL_P (expr) || BASELINK_P (expr))
2002 && !mark_used (expr, complain))
2003 return error_mark_node;
2005 if (template_p)
2007 if (TREE_CODE (expr) == UNBOUND_CLASS_TEMPLATE)
2009 /* cp_parser_lookup_name thought we were looking for a type,
2010 but we're actually looking for a declaration. */
2011 qualifying_class = TYPE_CONTEXT (expr);
2012 expr = TYPE_IDENTIFIER (expr);
2014 else
2015 check_template_keyword (expr);
2018 /* If EXPR occurs as the operand of '&', use special handling that
2019 permits a pointer-to-member. */
2020 if (address_p && done)
2022 if (TREE_CODE (expr) == SCOPE_REF)
2023 expr = TREE_OPERAND (expr, 1);
2024 expr = build_offset_ref (qualifying_class, expr,
2025 /*address_p=*/true, complain);
2026 return expr;
2029 /* No need to check access within an enum. */
2030 if (TREE_CODE (qualifying_class) == ENUMERAL_TYPE)
2031 return expr;
2033 /* Within the scope of a class, turn references to non-static
2034 members into expression of the form "this->...". */
2035 if (template_arg_p)
2036 /* But, within a template argument, we do not want make the
2037 transformation, as there is no "this" pointer. */
2039 else if (TREE_CODE (expr) == FIELD_DECL)
2041 push_deferring_access_checks (dk_no_check);
2042 expr = finish_non_static_data_member (expr, NULL_TREE,
2043 qualifying_class);
2044 pop_deferring_access_checks ();
2046 else if (BASELINK_P (expr))
2048 /* See if any of the functions are non-static members. */
2049 /* If so, the expression may be relative to 'this'. */
2050 if (!shared_member_p (expr)
2051 && current_class_ptr
2052 && DERIVED_FROM_P (qualifying_class,
2053 current_nonlambda_class_type ()))
2054 expr = (build_class_member_access_expr
2055 (maybe_dummy_object (qualifying_class, NULL),
2056 expr,
2057 BASELINK_ACCESS_BINFO (expr),
2058 /*preserve_reference=*/false,
2059 complain));
2060 else if (done)
2061 /* The expression is a qualified name whose address is not
2062 being taken. */
2063 expr = build_offset_ref (qualifying_class, expr, /*address_p=*/false,
2064 complain);
2066 else
2068 /* In a template, return a SCOPE_REF for most qualified-ids
2069 so that we can check access at instantiation time. But if
2070 we're looking at a member of the current instantiation, we
2071 know we have access and building up the SCOPE_REF confuses
2072 non-type template argument handling. */
2073 if (processing_template_decl
2074 && (!currently_open_class (qualifying_class)
2075 || TREE_CODE (expr) == BIT_NOT_EXPR))
2076 expr = build_qualified_name (TREE_TYPE (expr),
2077 qualifying_class, expr,
2078 template_p);
2080 expr = convert_from_reference (expr);
2083 return expr;
2086 /* Begin a statement-expression. The value returned must be passed to
2087 finish_stmt_expr. */
2089 tree
2090 begin_stmt_expr (void)
2092 return push_stmt_list ();
2095 /* Process the final expression of a statement expression. EXPR can be
2096 NULL, if the final expression is empty. Return a STATEMENT_LIST
2097 containing all the statements in the statement-expression, or
2098 ERROR_MARK_NODE if there was an error. */
2100 tree
2101 finish_stmt_expr_expr (tree expr, tree stmt_expr)
2103 if (error_operand_p (expr))
2105 /* The type of the statement-expression is the type of the last
2106 expression. */
2107 TREE_TYPE (stmt_expr) = error_mark_node;
2108 return error_mark_node;
2111 /* If the last statement does not have "void" type, then the value
2112 of the last statement is the value of the entire expression. */
2113 if (expr)
2115 tree type = TREE_TYPE (expr);
2117 if (processing_template_decl)
2119 expr = build_stmt (input_location, EXPR_STMT, expr);
2120 expr = add_stmt (expr);
2121 /* Mark the last statement so that we can recognize it as such at
2122 template-instantiation time. */
2123 EXPR_STMT_STMT_EXPR_RESULT (expr) = 1;
2125 else if (VOID_TYPE_P (type))
2127 /* Just treat this like an ordinary statement. */
2128 expr = finish_expr_stmt (expr);
2130 else
2132 /* It actually has a value we need to deal with. First, force it
2133 to be an rvalue so that we won't need to build up a copy
2134 constructor call later when we try to assign it to something. */
2135 expr = force_rvalue (expr, tf_warning_or_error);
2136 if (error_operand_p (expr))
2137 return error_mark_node;
2139 /* Update for array-to-pointer decay. */
2140 type = TREE_TYPE (expr);
2142 /* Wrap it in a CLEANUP_POINT_EXPR and add it to the list like a
2143 normal statement, but don't convert to void or actually add
2144 the EXPR_STMT. */
2145 if (TREE_CODE (expr) != CLEANUP_POINT_EXPR)
2146 expr = maybe_cleanup_point_expr (expr);
2147 add_stmt (expr);
2150 /* The type of the statement-expression is the type of the last
2151 expression. */
2152 TREE_TYPE (stmt_expr) = type;
2155 return stmt_expr;
2158 /* Finish a statement-expression. EXPR should be the value returned
2159 by the previous begin_stmt_expr. Returns an expression
2160 representing the statement-expression. */
2162 tree
2163 finish_stmt_expr (tree stmt_expr, bool has_no_scope)
2165 tree type;
2166 tree result;
2168 if (error_operand_p (stmt_expr))
2170 pop_stmt_list (stmt_expr);
2171 return error_mark_node;
2174 gcc_assert (TREE_CODE (stmt_expr) == STATEMENT_LIST);
2176 type = TREE_TYPE (stmt_expr);
2177 result = pop_stmt_list (stmt_expr);
2178 TREE_TYPE (result) = type;
2180 if (processing_template_decl)
2182 result = build_min (STMT_EXPR, type, result);
2183 TREE_SIDE_EFFECTS (result) = 1;
2184 STMT_EXPR_NO_SCOPE (result) = has_no_scope;
2186 else if (CLASS_TYPE_P (type))
2188 /* Wrap the statement-expression in a TARGET_EXPR so that the
2189 temporary object created by the final expression is destroyed at
2190 the end of the full-expression containing the
2191 statement-expression. */
2192 result = force_target_expr (type, result, tf_warning_or_error);
2195 return result;
2198 /* Returns the expression which provides the value of STMT_EXPR. */
2200 tree
2201 stmt_expr_value_expr (tree stmt_expr)
2203 tree t = STMT_EXPR_STMT (stmt_expr);
2205 if (TREE_CODE (t) == BIND_EXPR)
2206 t = BIND_EXPR_BODY (t);
2208 if (TREE_CODE (t) == STATEMENT_LIST && STATEMENT_LIST_TAIL (t))
2209 t = STATEMENT_LIST_TAIL (t)->stmt;
2211 if (TREE_CODE (t) == EXPR_STMT)
2212 t = EXPR_STMT_EXPR (t);
2214 return t;
2217 /* Return TRUE iff EXPR_STMT is an empty list of
2218 expression statements. */
2220 bool
2221 empty_expr_stmt_p (tree expr_stmt)
2223 tree body = NULL_TREE;
2225 if (expr_stmt == void_node)
2226 return true;
2228 if (expr_stmt)
2230 if (TREE_CODE (expr_stmt) == EXPR_STMT)
2231 body = EXPR_STMT_EXPR (expr_stmt);
2232 else if (TREE_CODE (expr_stmt) == STATEMENT_LIST)
2233 body = expr_stmt;
2236 if (body)
2238 if (TREE_CODE (body) == STATEMENT_LIST)
2239 return tsi_end_p (tsi_start (body));
2240 else
2241 return empty_expr_stmt_p (body);
2243 return false;
2246 /* Perform Koenig lookup. FN is the postfix-expression representing
2247 the function (or functions) to call; ARGS are the arguments to the
2248 call. Returns the functions to be considered by overload resolution. */
2250 cp_expr
2251 perform_koenig_lookup (cp_expr fn, vec<tree, va_gc> *args,
2252 tsubst_flags_t complain)
2254 tree identifier = NULL_TREE;
2255 tree functions = NULL_TREE;
2256 tree tmpl_args = NULL_TREE;
2257 bool template_id = false;
2258 location_t loc = fn.get_location ();
2260 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
2262 /* Use a separate flag to handle null args. */
2263 template_id = true;
2264 tmpl_args = TREE_OPERAND (fn, 1);
2265 fn = TREE_OPERAND (fn, 0);
2268 /* Find the name of the overloaded function. */
2269 if (identifier_p (fn))
2270 identifier = fn;
2271 else
2273 functions = fn;
2274 identifier = OVL_NAME (functions);
2277 /* A call to a namespace-scope function using an unqualified name.
2279 Do Koenig lookup -- unless any of the arguments are
2280 type-dependent. */
2281 if (!any_type_dependent_arguments_p (args)
2282 && !any_dependent_template_arguments_p (tmpl_args))
2284 fn = lookup_arg_dependent (identifier, functions, args);
2285 if (!fn)
2287 /* The unqualified name could not be resolved. */
2288 if (complain & tf_error)
2289 fn = unqualified_fn_lookup_error (cp_expr (identifier, loc));
2290 else
2291 fn = identifier;
2295 if (fn && template_id && fn != error_mark_node)
2296 fn = build2 (TEMPLATE_ID_EXPR, unknown_type_node, fn, tmpl_args);
2298 return fn;
2301 /* Generate an expression for `FN (ARGS)'. This may change the
2302 contents of ARGS.
2304 If DISALLOW_VIRTUAL is true, the call to FN will be not generated
2305 as a virtual call, even if FN is virtual. (This flag is set when
2306 encountering an expression where the function name is explicitly
2307 qualified. For example a call to `X::f' never generates a virtual
2308 call.)
2310 Returns code for the call. */
2312 tree
2313 finish_call_expr (tree fn, vec<tree, va_gc> **args, bool disallow_virtual,
2314 bool koenig_p, tsubst_flags_t complain)
2316 tree result;
2317 tree orig_fn;
2318 vec<tree, va_gc> *orig_args = NULL;
2320 if (fn == error_mark_node)
2321 return error_mark_node;
2323 gcc_assert (!TYPE_P (fn));
2325 /* If FN may be a FUNCTION_DECL obfuscated by force_paren_expr, undo
2326 it so that we can tell this is a call to a known function. */
2327 fn = maybe_undo_parenthesized_ref (fn);
2329 orig_fn = fn;
2331 if (processing_template_decl)
2333 /* If FN is a local extern declaration or set thereof, look them up
2334 again at instantiation time. */
2335 if (is_overloaded_fn (fn))
2337 tree ifn = get_first_fn (fn);
2338 if (TREE_CODE (ifn) == FUNCTION_DECL
2339 && DECL_LOCAL_FUNCTION_P (ifn))
2340 orig_fn = DECL_NAME (ifn);
2343 /* If the call expression is dependent, build a CALL_EXPR node
2344 with no type; type_dependent_expression_p recognizes
2345 expressions with no type as being dependent. */
2346 if (type_dependent_expression_p (fn)
2347 || any_type_dependent_arguments_p (*args))
2349 result = build_min_nt_call_vec (orig_fn, *args);
2350 SET_EXPR_LOCATION (result, EXPR_LOC_OR_LOC (fn, input_location));
2351 KOENIG_LOOKUP_P (result) = koenig_p;
2352 if (is_overloaded_fn (fn))
2354 fn = get_fns (fn);
2355 lookup_keep (fn, true);
2358 if (cfun)
2360 bool abnormal = true;
2361 for (lkp_iterator iter (fn); abnormal && iter; ++iter)
2363 tree fndecl = *iter;
2364 if (TREE_CODE (fndecl) != FUNCTION_DECL
2365 || !TREE_THIS_VOLATILE (fndecl))
2366 abnormal = false;
2368 /* FIXME: Stop warning about falling off end of non-void
2369 function. But this is wrong. Even if we only see
2370 no-return fns at this point, we could select a
2371 future-defined return fn during instantiation. Or
2372 vice-versa. */
2373 if (abnormal)
2374 current_function_returns_abnormally = 1;
2376 return result;
2378 orig_args = make_tree_vector_copy (*args);
2379 if (!BASELINK_P (fn)
2380 && TREE_CODE (fn) != PSEUDO_DTOR_EXPR
2381 && TREE_TYPE (fn) != unknown_type_node)
2382 fn = build_non_dependent_expr (fn);
2383 make_args_non_dependent (*args);
2386 if (TREE_CODE (fn) == COMPONENT_REF)
2388 tree member = TREE_OPERAND (fn, 1);
2389 if (BASELINK_P (member))
2391 tree object = TREE_OPERAND (fn, 0);
2392 return build_new_method_call (object, member,
2393 args, NULL_TREE,
2394 (disallow_virtual
2395 ? LOOKUP_NORMAL | LOOKUP_NONVIRTUAL
2396 : LOOKUP_NORMAL),
2397 /*fn_p=*/NULL,
2398 complain);
2402 /* Per 13.3.1.1, '(&f)(...)' is the same as '(f)(...)'. */
2403 if (TREE_CODE (fn) == ADDR_EXPR
2404 && TREE_CODE (TREE_OPERAND (fn, 0)) == OVERLOAD)
2405 fn = TREE_OPERAND (fn, 0);
2407 if (is_overloaded_fn (fn))
2408 fn = baselink_for_fns (fn);
2410 result = NULL_TREE;
2411 if (BASELINK_P (fn))
2413 tree object;
2415 /* A call to a member function. From [over.call.func]:
2417 If the keyword this is in scope and refers to the class of
2418 that member function, or a derived class thereof, then the
2419 function call is transformed into a qualified function call
2420 using (*this) as the postfix-expression to the left of the
2421 . operator.... [Otherwise] a contrived object of type T
2422 becomes the implied object argument.
2424 In this situation:
2426 struct A { void f(); };
2427 struct B : public A {};
2428 struct C : public A { void g() { B::f(); }};
2430 "the class of that member function" refers to `A'. But 11.2
2431 [class.access.base] says that we need to convert 'this' to B* as
2432 part of the access, so we pass 'B' to maybe_dummy_object. */
2434 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (get_first_fn (fn)))
2436 /* A constructor call always uses a dummy object. (This constructor
2437 call which has the form A::A () is actually invalid and we are
2438 going to reject it later in build_new_method_call.) */
2439 object = build_dummy_object (BINFO_TYPE (BASELINK_ACCESS_BINFO (fn)));
2441 else
2442 object = maybe_dummy_object (BINFO_TYPE (BASELINK_ACCESS_BINFO (fn)),
2443 NULL);
2445 result = build_new_method_call (object, fn, args, NULL_TREE,
2446 (disallow_virtual
2447 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
2448 : LOOKUP_NORMAL),
2449 /*fn_p=*/NULL,
2450 complain);
2452 else if (is_overloaded_fn (fn))
2454 /* If the function is an overloaded builtin, resolve it. */
2455 if (TREE_CODE (fn) == FUNCTION_DECL
2456 && (DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL
2457 || DECL_BUILT_IN_CLASS (fn) == BUILT_IN_MD))
2458 result = resolve_overloaded_builtin (input_location, fn, *args);
2460 if (!result)
2462 if (warn_sizeof_pointer_memaccess
2463 && (complain & tf_warning)
2464 && !vec_safe_is_empty (*args)
2465 && !processing_template_decl)
2467 location_t sizeof_arg_loc[3];
2468 tree sizeof_arg[3];
2469 unsigned int i;
2470 for (i = 0; i < 3; i++)
2472 tree t;
2474 sizeof_arg_loc[i] = UNKNOWN_LOCATION;
2475 sizeof_arg[i] = NULL_TREE;
2476 if (i >= (*args)->length ())
2477 continue;
2478 t = (**args)[i];
2479 if (TREE_CODE (t) != SIZEOF_EXPR)
2480 continue;
2481 if (SIZEOF_EXPR_TYPE_P (t))
2482 sizeof_arg[i] = TREE_TYPE (TREE_OPERAND (t, 0));
2483 else
2484 sizeof_arg[i] = TREE_OPERAND (t, 0);
2485 sizeof_arg_loc[i] = EXPR_LOCATION (t);
2487 sizeof_pointer_memaccess_warning
2488 (sizeof_arg_loc, fn, *args,
2489 sizeof_arg, same_type_ignoring_top_level_qualifiers_p);
2492 /* A call to a namespace-scope function. */
2493 result = build_new_function_call (fn, args, complain);
2496 else if (TREE_CODE (fn) == PSEUDO_DTOR_EXPR)
2498 if (!vec_safe_is_empty (*args))
2499 error ("arguments to destructor are not allowed");
2500 /* Mark the pseudo-destructor call as having side-effects so
2501 that we do not issue warnings about its use. */
2502 result = build1 (NOP_EXPR,
2503 void_type_node,
2504 TREE_OPERAND (fn, 0));
2505 TREE_SIDE_EFFECTS (result) = 1;
2507 else if (CLASS_TYPE_P (TREE_TYPE (fn)))
2508 /* If the "function" is really an object of class type, it might
2509 have an overloaded `operator ()'. */
2510 result = build_op_call (fn, args, complain);
2512 if (!result)
2513 /* A call where the function is unknown. */
2514 result = cp_build_function_call_vec (fn, args, complain);
2516 if (processing_template_decl && result != error_mark_node)
2518 if (INDIRECT_REF_P (result))
2519 result = TREE_OPERAND (result, 0);
2520 result = build_call_vec (TREE_TYPE (result), orig_fn, orig_args);
2521 SET_EXPR_LOCATION (result, input_location);
2522 KOENIG_LOOKUP_P (result) = koenig_p;
2523 release_tree_vector (orig_args);
2524 result = convert_from_reference (result);
2527 /* Free or retain OVERLOADs from lookup. */
2528 if (is_overloaded_fn (orig_fn))
2529 lookup_keep (get_fns (orig_fn), processing_template_decl);
2531 return result;
2534 /* Finish a call to a postfix increment or decrement or EXPR. (Which
2535 is indicated by CODE, which should be POSTINCREMENT_EXPR or
2536 POSTDECREMENT_EXPR.) */
2538 cp_expr
2539 finish_increment_expr (cp_expr expr, enum tree_code code)
2541 /* input_location holds the location of the trailing operator token.
2542 Build a location of the form:
2543 expr++
2544 ~~~~^~
2545 with the caret at the operator token, ranging from the start
2546 of EXPR to the end of the operator token. */
2547 location_t combined_loc = make_location (input_location,
2548 expr.get_start (),
2549 get_finish (input_location));
2550 cp_expr result = build_x_unary_op (combined_loc, code, expr,
2551 tf_warning_or_error);
2552 /* TODO: build_x_unary_op doesn't honor the location, so set it here. */
2553 result.set_location (combined_loc);
2554 return result;
2557 /* Finish a use of `this'. Returns an expression for `this'. */
2559 tree
2560 finish_this_expr (void)
2562 tree result = NULL_TREE;
2564 if (current_class_ptr)
2566 tree type = TREE_TYPE (current_class_ref);
2568 /* In a lambda expression, 'this' refers to the captured 'this'. */
2569 if (LAMBDA_TYPE_P (type))
2570 result = lambda_expr_this_capture (CLASSTYPE_LAMBDA_EXPR (type), true);
2571 else
2572 result = current_class_ptr;
2575 if (result)
2576 /* The keyword 'this' is a prvalue expression. */
2577 return rvalue (result);
2579 tree fn = current_nonlambda_function ();
2580 if (fn && DECL_STATIC_FUNCTION_P (fn))
2581 error ("%<this%> is unavailable for static member functions");
2582 else if (fn)
2583 error ("invalid use of %<this%> in non-member function");
2584 else
2585 error ("invalid use of %<this%> at top level");
2586 return error_mark_node;
2589 /* Finish a pseudo-destructor expression. If SCOPE is NULL, the
2590 expression was of the form `OBJECT.~DESTRUCTOR' where DESTRUCTOR is
2591 the TYPE for the type given. If SCOPE is non-NULL, the expression
2592 was of the form `OBJECT.SCOPE::~DESTRUCTOR'. */
2594 tree
2595 finish_pseudo_destructor_expr (tree object, tree scope, tree destructor,
2596 location_t loc)
2598 if (object == error_mark_node || destructor == error_mark_node)
2599 return error_mark_node;
2601 gcc_assert (TYPE_P (destructor));
2603 if (!processing_template_decl)
2605 if (scope == error_mark_node)
2607 error_at (loc, "invalid qualifying scope in pseudo-destructor name");
2608 return error_mark_node;
2610 if (is_auto (destructor))
2611 destructor = TREE_TYPE (object);
2612 if (scope && TYPE_P (scope) && !check_dtor_name (scope, destructor))
2614 error_at (loc,
2615 "qualified type %qT does not match destructor name ~%qT",
2616 scope, destructor);
2617 return error_mark_node;
2621 /* [expr.pseudo] says both:
2623 The type designated by the pseudo-destructor-name shall be
2624 the same as the object type.
2626 and:
2628 The cv-unqualified versions of the object type and of the
2629 type designated by the pseudo-destructor-name shall be the
2630 same type.
2632 We implement the more generous second sentence, since that is
2633 what most other compilers do. */
2634 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (object),
2635 destructor))
2637 error_at (loc, "%qE is not of type %qT", object, destructor);
2638 return error_mark_node;
2642 return build3_loc (loc, PSEUDO_DTOR_EXPR, void_type_node, object,
2643 scope, destructor);
2646 /* Finish an expression of the form CODE EXPR. */
2648 cp_expr
2649 finish_unary_op_expr (location_t op_loc, enum tree_code code, cp_expr expr,
2650 tsubst_flags_t complain)
2652 /* Build a location of the form:
2653 ++expr
2654 ^~~~~~
2655 with the caret at the operator token, ranging from the start
2656 of the operator token to the end of EXPR. */
2657 location_t combined_loc = make_location (op_loc,
2658 op_loc, expr.get_finish ());
2659 cp_expr result = build_x_unary_op (combined_loc, code, expr, complain);
2660 /* TODO: build_x_unary_op doesn't always honor the location. */
2661 result.set_location (combined_loc);
2663 tree result_ovl, expr_ovl;
2665 if (!(complain & tf_warning))
2666 return result;
2668 result_ovl = result;
2669 expr_ovl = expr;
2671 if (!processing_template_decl)
2672 expr_ovl = cp_fully_fold (expr_ovl);
2674 if (!CONSTANT_CLASS_P (expr_ovl)
2675 || TREE_OVERFLOW_P (expr_ovl))
2676 return result;
2678 if (!processing_template_decl)
2679 result_ovl = cp_fully_fold (result_ovl);
2681 if (CONSTANT_CLASS_P (result_ovl) && TREE_OVERFLOW_P (result_ovl))
2682 overflow_warning (combined_loc, result_ovl);
2684 return result;
2687 /* Finish a compound-literal expression or C++11 functional cast with aggregate
2688 initializer. TYPE is the type to which the CONSTRUCTOR in COMPOUND_LITERAL
2689 is being cast. */
2691 tree
2692 finish_compound_literal (tree type, tree compound_literal,
2693 tsubst_flags_t complain,
2694 fcl_t fcl_context)
2696 if (type == error_mark_node)
2697 return error_mark_node;
2699 if (TREE_CODE (type) == REFERENCE_TYPE)
2701 compound_literal
2702 = finish_compound_literal (TREE_TYPE (type), compound_literal,
2703 complain, fcl_context);
2704 return cp_build_c_cast (type, compound_literal, complain);
2707 if (!TYPE_OBJ_P (type))
2709 if (complain & tf_error)
2710 error ("compound literal of non-object type %qT", type);
2711 return error_mark_node;
2714 if (tree anode = type_uses_auto (type))
2715 if (CLASS_PLACEHOLDER_TEMPLATE (anode))
2717 type = do_auto_deduction (type, compound_literal, anode, complain,
2718 adc_variable_type);
2719 if (type == error_mark_node)
2720 return error_mark_node;
2723 if (processing_template_decl)
2725 TREE_TYPE (compound_literal) = type;
2726 /* Mark the expression as a compound literal. */
2727 TREE_HAS_CONSTRUCTOR (compound_literal) = 1;
2728 if (fcl_context == fcl_c99)
2729 CONSTRUCTOR_C99_COMPOUND_LITERAL (compound_literal) = 1;
2730 return compound_literal;
2733 type = complete_type (type);
2735 if (TYPE_NON_AGGREGATE_CLASS (type))
2737 /* Trying to deal with a CONSTRUCTOR instead of a TREE_LIST
2738 everywhere that deals with function arguments would be a pain, so
2739 just wrap it in a TREE_LIST. The parser set a flag so we know
2740 that it came from T{} rather than T({}). */
2741 CONSTRUCTOR_IS_DIRECT_INIT (compound_literal) = 1;
2742 compound_literal = build_tree_list (NULL_TREE, compound_literal);
2743 return build_functional_cast (type, compound_literal, complain);
2746 if (TREE_CODE (type) == ARRAY_TYPE
2747 && check_array_initializer (NULL_TREE, type, compound_literal))
2748 return error_mark_node;
2749 compound_literal = reshape_init (type, compound_literal, complain);
2750 if (SCALAR_TYPE_P (type)
2751 && !BRACE_ENCLOSED_INITIALIZER_P (compound_literal)
2752 && !check_narrowing (type, compound_literal, complain))
2753 return error_mark_node;
2754 if (TREE_CODE (type) == ARRAY_TYPE
2755 && TYPE_DOMAIN (type) == NULL_TREE)
2757 cp_complete_array_type_or_error (&type, compound_literal,
2758 false, complain);
2759 if (type == error_mark_node)
2760 return error_mark_node;
2762 compound_literal = digest_init_flags (type, compound_literal, LOOKUP_NORMAL,
2763 complain);
2764 if (TREE_CODE (compound_literal) == CONSTRUCTOR)
2766 TREE_HAS_CONSTRUCTOR (compound_literal) = true;
2767 if (fcl_context == fcl_c99)
2768 CONSTRUCTOR_C99_COMPOUND_LITERAL (compound_literal) = 1;
2771 /* Put static/constant array temporaries in static variables. */
2772 /* FIXME all C99 compound literals should be variables rather than C++
2773 temporaries, unless they are used as an aggregate initializer. */
2774 if ((!at_function_scope_p () || CP_TYPE_CONST_P (type))
2775 && fcl_context == fcl_c99
2776 && TREE_CODE (type) == ARRAY_TYPE
2777 && !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
2778 && initializer_constant_valid_p (compound_literal, type))
2780 tree decl = create_temporary_var (type);
2781 DECL_INITIAL (decl) = compound_literal;
2782 TREE_STATIC (decl) = 1;
2783 if (literal_type_p (type) && CP_TYPE_CONST_NON_VOLATILE_P (type))
2785 /* 5.19 says that a constant expression can include an
2786 lvalue-rvalue conversion applied to "a glvalue of literal type
2787 that refers to a non-volatile temporary object initialized
2788 with a constant expression". Rather than try to communicate
2789 that this VAR_DECL is a temporary, just mark it constexpr. */
2790 DECL_DECLARED_CONSTEXPR_P (decl) = true;
2791 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
2792 TREE_CONSTANT (decl) = true;
2794 cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
2795 decl = pushdecl_top_level (decl);
2796 DECL_NAME (decl) = make_anon_name ();
2797 SET_DECL_ASSEMBLER_NAME (decl, DECL_NAME (decl));
2798 /* Make sure the destructor is callable. */
2799 tree clean = cxx_maybe_build_cleanup (decl, complain);
2800 if (clean == error_mark_node)
2801 return error_mark_node;
2802 return decl;
2805 /* Represent other compound literals with TARGET_EXPR so we produce
2806 an lvalue, but can elide copies. */
2807 if (!VECTOR_TYPE_P (type))
2808 compound_literal = get_target_expr_sfinae (compound_literal, complain);
2810 return compound_literal;
2813 /* Return the declaration for the function-name variable indicated by
2814 ID. */
2816 tree
2817 finish_fname (tree id)
2819 tree decl;
2821 decl = fname_decl (input_location, C_RID_CODE (id), id);
2822 if (processing_template_decl && current_function_decl
2823 && decl != error_mark_node)
2824 decl = DECL_NAME (decl);
2825 return decl;
2828 /* Finish a translation unit. */
2830 void
2831 finish_translation_unit (void)
2833 /* In case there were missing closebraces,
2834 get us back to the global binding level. */
2835 pop_everything ();
2836 while (current_namespace != global_namespace)
2837 pop_namespace ();
2839 /* Do file scope __FUNCTION__ et al. */
2840 finish_fname_decls ();
2843 /* Finish a template type parameter, specified as AGGR IDENTIFIER.
2844 Returns the parameter. */
2846 tree
2847 finish_template_type_parm (tree aggr, tree identifier)
2849 if (aggr != class_type_node)
2851 permerror (input_location, "template type parameters must use the keyword %<class%> or %<typename%>");
2852 aggr = class_type_node;
2855 return build_tree_list (aggr, identifier);
2858 /* Finish a template template parameter, specified as AGGR IDENTIFIER.
2859 Returns the parameter. */
2861 tree
2862 finish_template_template_parm (tree aggr, tree identifier)
2864 tree decl = build_decl (input_location,
2865 TYPE_DECL, identifier, NULL_TREE);
2867 tree tmpl = build_lang_decl (TEMPLATE_DECL, identifier, NULL_TREE);
2868 DECL_TEMPLATE_PARMS (tmpl) = current_template_parms;
2869 DECL_TEMPLATE_RESULT (tmpl) = decl;
2870 DECL_ARTIFICIAL (decl) = 1;
2872 // Associate the constraints with the underlying declaration,
2873 // not the template.
2874 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
2875 tree constr = build_constraints (reqs, NULL_TREE);
2876 set_constraints (decl, constr);
2878 end_template_decl ();
2880 gcc_assert (DECL_TEMPLATE_PARMS (tmpl));
2882 check_default_tmpl_args (decl, DECL_TEMPLATE_PARMS (tmpl),
2883 /*is_primary=*/true, /*is_partial=*/false,
2884 /*is_friend=*/0);
2886 return finish_template_type_parm (aggr, tmpl);
2889 /* ARGUMENT is the default-argument value for a template template
2890 parameter. If ARGUMENT is invalid, issue error messages and return
2891 the ERROR_MARK_NODE. Otherwise, ARGUMENT itself is returned. */
2893 tree
2894 check_template_template_default_arg (tree argument)
2896 if (TREE_CODE (argument) != TEMPLATE_DECL
2897 && TREE_CODE (argument) != TEMPLATE_TEMPLATE_PARM
2898 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
2900 if (TREE_CODE (argument) == TYPE_DECL)
2901 error ("invalid use of type %qT as a default value for a template "
2902 "template-parameter", TREE_TYPE (argument));
2903 else
2904 error ("invalid default argument for a template template parameter");
2905 return error_mark_node;
2908 return argument;
2911 /* Begin a class definition, as indicated by T. */
2913 tree
2914 begin_class_definition (tree t)
2916 if (error_operand_p (t) || error_operand_p (TYPE_MAIN_DECL (t)))
2917 return error_mark_node;
2919 if (processing_template_parmlist)
2921 error ("definition of %q#T inside template parameter list", t);
2922 return error_mark_node;
2925 /* According to the C++ ABI, decimal classes defined in ISO/IEC TR 24733
2926 are passed the same as decimal scalar types. */
2927 if (TREE_CODE (t) == RECORD_TYPE
2928 && !processing_template_decl)
2930 tree ns = TYPE_CONTEXT (t);
2931 if (ns && TREE_CODE (ns) == NAMESPACE_DECL
2932 && DECL_CONTEXT (ns) == std_node
2933 && DECL_NAME (ns)
2934 && id_equal (DECL_NAME (ns), "decimal"))
2936 const char *n = TYPE_NAME_STRING (t);
2937 if ((strcmp (n, "decimal32") == 0)
2938 || (strcmp (n, "decimal64") == 0)
2939 || (strcmp (n, "decimal128") == 0))
2940 TYPE_TRANSPARENT_AGGR (t) = 1;
2944 /* A non-implicit typename comes from code like:
2946 template <typename T> struct A {
2947 template <typename U> struct A<T>::B ...
2949 This is erroneous. */
2950 else if (TREE_CODE (t) == TYPENAME_TYPE)
2952 error ("invalid definition of qualified type %qT", t);
2953 t = error_mark_node;
2956 if (t == error_mark_node || ! MAYBE_CLASS_TYPE_P (t))
2958 t = make_class_type (RECORD_TYPE);
2959 pushtag (make_anon_name (), t, /*tag_scope=*/ts_current);
2962 if (TYPE_BEING_DEFINED (t))
2964 t = make_class_type (TREE_CODE (t));
2965 pushtag (TYPE_IDENTIFIER (t), t, /*tag_scope=*/ts_current);
2967 maybe_process_partial_specialization (t);
2968 pushclass (t);
2969 TYPE_BEING_DEFINED (t) = 1;
2970 class_binding_level->defining_class_p = 1;
2972 if (flag_pack_struct)
2974 tree v;
2975 TYPE_PACKED (t) = 1;
2976 /* Even though the type is being defined for the first time
2977 here, there might have been a forward declaration, so there
2978 might be cv-qualified variants of T. */
2979 for (v = TYPE_NEXT_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v))
2980 TYPE_PACKED (v) = 1;
2982 /* Reset the interface data, at the earliest possible
2983 moment, as it might have been set via a class foo;
2984 before. */
2985 if (! TYPE_UNNAMED_P (t))
2987 struct c_fileinfo *finfo = \
2988 get_fileinfo (LOCATION_FILE (input_location));
2989 CLASSTYPE_INTERFACE_ONLY (t) = finfo->interface_only;
2990 SET_CLASSTYPE_INTERFACE_UNKNOWN_X
2991 (t, finfo->interface_unknown);
2993 reset_specialization();
2995 /* Make a declaration for this class in its own scope. */
2996 build_self_reference ();
2998 return t;
3001 /* Finish the member declaration given by DECL. */
3003 void
3004 finish_member_declaration (tree decl)
3006 if (decl == error_mark_node || decl == NULL_TREE)
3007 return;
3009 if (decl == void_type_node)
3010 /* The COMPONENT was a friend, not a member, and so there's
3011 nothing for us to do. */
3012 return;
3014 /* We should see only one DECL at a time. */
3015 gcc_assert (DECL_CHAIN (decl) == NULL_TREE);
3017 /* Don't add decls after definition. */
3018 gcc_assert (TYPE_BEING_DEFINED (current_class_type)
3019 /* We can add lambda types when late parsing default
3020 arguments. */
3021 || LAMBDA_TYPE_P (TREE_TYPE (decl)));
3023 /* Set up access control for DECL. */
3024 TREE_PRIVATE (decl)
3025 = (current_access_specifier == access_private_node);
3026 TREE_PROTECTED (decl)
3027 = (current_access_specifier == access_protected_node);
3028 if (TREE_CODE (decl) == TEMPLATE_DECL)
3030 TREE_PRIVATE (DECL_TEMPLATE_RESULT (decl)) = TREE_PRIVATE (decl);
3031 TREE_PROTECTED (DECL_TEMPLATE_RESULT (decl)) = TREE_PROTECTED (decl);
3034 /* Mark the DECL as a member of the current class, unless it's
3035 a member of an enumeration. */
3036 if (TREE_CODE (decl) != CONST_DECL)
3037 DECL_CONTEXT (decl) = current_class_type;
3039 if (TREE_CODE (decl) == USING_DECL)
3040 /* For now, ignore class-scope USING_DECLS, so that debugging
3041 backends do not see them. */
3042 DECL_IGNORED_P (decl) = 1;
3044 /* Check for bare parameter packs in the non-static data member
3045 declaration. */
3046 if (TREE_CODE (decl) == FIELD_DECL)
3048 if (check_for_bare_parameter_packs (TREE_TYPE (decl)))
3049 TREE_TYPE (decl) = error_mark_node;
3050 if (check_for_bare_parameter_packs (DECL_ATTRIBUTES (decl)))
3051 DECL_ATTRIBUTES (decl) = NULL_TREE;
3054 /* [dcl.link]
3056 A C language linkage is ignored for the names of class members
3057 and the member function type of class member functions. */
3058 if (DECL_LANG_SPECIFIC (decl))
3059 SET_DECL_LANGUAGE (decl, lang_cplusplus);
3061 bool add = false;
3063 /* Functions and non-functions are added differently. */
3064 if (DECL_DECLARES_FUNCTION_P (decl))
3065 add = add_method (current_class_type, decl, false);
3066 /* Enter the DECL into the scope of the class, if the class
3067 isn't a closure (whose fields are supposed to be unnamed). */
3068 else if (CLASSTYPE_LAMBDA_EXPR (current_class_type)
3069 || pushdecl_class_level (decl))
3070 add = true;
3072 if (add)
3074 /* All TYPE_DECLs go at the end of TYPE_FIELDS. Ordinary fields
3075 go at the beginning. The reason is that
3076 legacy_nonfn_member_lookup searches the list in order, and we
3077 want a field name to override a type name so that the "struct
3078 stat hack" will work. In particular:
3080 struct S { enum E { }; static const int E = 5; int ary[S::E]; } s;
3082 is valid. */
3084 if (TREE_CODE (decl) == TYPE_DECL)
3085 TYPE_FIELDS (current_class_type)
3086 = chainon (TYPE_FIELDS (current_class_type), decl);
3087 else
3089 DECL_CHAIN (decl) = TYPE_FIELDS (current_class_type);
3090 TYPE_FIELDS (current_class_type) = decl;
3093 maybe_add_class_template_decl_list (current_class_type, decl,
3094 /*friend_p=*/0);
3098 /* Finish processing a complete template declaration. The PARMS are
3099 the template parameters. */
3101 void
3102 finish_template_decl (tree parms)
3104 if (parms)
3105 end_template_decl ();
3106 else
3107 end_specialization ();
3110 // Returns the template type of the class scope being entered. If we're
3111 // entering a constrained class scope. TYPE is the class template
3112 // scope being entered and we may need to match the intended type with
3113 // a constrained specialization. For example:
3115 // template<Object T>
3116 // struct S { void f(); }; #1
3118 // template<Object T>
3119 // void S<T>::f() { } #2
3121 // We check, in #2, that S<T> refers precisely to the type declared by
3122 // #1 (i.e., that the constraints match). Note that the following should
3123 // be an error since there is no specialization of S<T> that is
3124 // unconstrained, but this is not diagnosed here.
3126 // template<typename T>
3127 // void S<T>::f() { }
3129 // We cannot diagnose this problem here since this function also matches
3130 // qualified template names that are not part of a definition. For example:
3132 // template<Integral T, Floating_point U>
3133 // typename pair<T, U>::first_type void f(T, U);
3135 // Here, it is unlikely that there is a partial specialization of
3136 // pair constrained for for Integral and Floating_point arguments.
3138 // The general rule is: if a constrained specialization with matching
3139 // constraints is found return that type. Also note that if TYPE is not a
3140 // class-type (e.g. a typename type), then no fixup is needed.
3142 static tree
3143 fixup_template_type (tree type)
3145 // Find the template parameter list at the a depth appropriate to
3146 // the scope we're trying to enter.
3147 tree parms = current_template_parms;
3148 int depth = template_class_depth (type);
3149 for (int n = processing_template_decl; n > depth && parms; --n)
3150 parms = TREE_CHAIN (parms);
3151 if (!parms)
3152 return type;
3153 tree cur_reqs = TEMPLATE_PARMS_CONSTRAINTS (parms);
3154 tree cur_constr = build_constraints (cur_reqs, NULL_TREE);
3156 // Search for a specialization whose type and constraints match.
3157 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
3158 tree specs = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
3159 while (specs)
3161 tree spec_constr = get_constraints (TREE_VALUE (specs));
3163 // If the type and constraints match a specialization, then we
3164 // are entering that type.
3165 if (same_type_p (type, TREE_TYPE (specs))
3166 && equivalent_constraints (cur_constr, spec_constr))
3167 return TREE_TYPE (specs);
3168 specs = TREE_CHAIN (specs);
3171 // If no specialization matches, then must return the type
3172 // previously found.
3173 return type;
3176 /* Finish processing a template-id (which names a type) of the form
3177 NAME < ARGS >. Return the TYPE_DECL for the type named by the
3178 template-id. If ENTERING_SCOPE is nonzero we are about to enter
3179 the scope of template-id indicated. */
3181 tree
3182 finish_template_type (tree name, tree args, int entering_scope)
3184 tree type;
3186 type = lookup_template_class (name, args,
3187 NULL_TREE, NULL_TREE, entering_scope,
3188 tf_warning_or_error | tf_user);
3190 /* If we might be entering the scope of a partial specialization,
3191 find the one with the right constraints. */
3192 if (flag_concepts
3193 && entering_scope
3194 && CLASS_TYPE_P (type)
3195 && CLASSTYPE_TEMPLATE_INFO (type)
3196 && dependent_type_p (type)
3197 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
3198 type = fixup_template_type (type);
3200 if (type == error_mark_node)
3201 return type;
3202 else if (CLASS_TYPE_P (type) && !alias_type_or_template_p (type))
3203 return TYPE_STUB_DECL (type);
3204 else
3205 return TYPE_NAME (type);
3208 /* Finish processing a BASE_CLASS with the indicated ACCESS_SPECIFIER.
3209 Return a TREE_LIST containing the ACCESS_SPECIFIER and the
3210 BASE_CLASS, or NULL_TREE if an error occurred. The
3211 ACCESS_SPECIFIER is one of
3212 access_{default,public,protected_private}_node. For a virtual base
3213 we set TREE_TYPE. */
3215 tree
3216 finish_base_specifier (tree base, tree access, bool virtual_p)
3218 tree result;
3220 if (base == error_mark_node)
3222 error ("invalid base-class specification");
3223 result = NULL_TREE;
3225 else if (! MAYBE_CLASS_TYPE_P (base))
3227 error ("%qT is not a class type", base);
3228 result = NULL_TREE;
3230 else
3232 if (cp_type_quals (base) != 0)
3234 /* DR 484: Can a base-specifier name a cv-qualified
3235 class type? */
3236 base = TYPE_MAIN_VARIANT (base);
3238 result = build_tree_list (access, base);
3239 if (virtual_p)
3240 TREE_TYPE (result) = integer_type_node;
3243 return result;
3246 /* If FNS is a member function, a set of member functions, or a
3247 template-id referring to one or more member functions, return a
3248 BASELINK for FNS, incorporating the current access context.
3249 Otherwise, return FNS unchanged. */
3251 tree
3252 baselink_for_fns (tree fns)
3254 tree scope;
3255 tree cl;
3257 if (BASELINK_P (fns)
3258 || error_operand_p (fns))
3259 return fns;
3261 scope = ovl_scope (fns);
3262 if (!CLASS_TYPE_P (scope))
3263 return fns;
3265 cl = currently_open_derived_class (scope);
3266 if (!cl)
3267 cl = scope;
3268 cl = TYPE_BINFO (cl);
3269 return build_baselink (cl, cl, fns, /*optype=*/NULL_TREE);
3272 /* Returns true iff DECL is a variable from a function outside
3273 the current one. */
3275 static bool
3276 outer_var_p (tree decl)
3278 return ((VAR_P (decl) || TREE_CODE (decl) == PARM_DECL)
3279 && DECL_FUNCTION_SCOPE_P (decl)
3280 /* Don't get confused by temporaries. */
3281 && DECL_NAME (decl)
3282 && (DECL_CONTEXT (decl) != current_function_decl
3283 || parsing_nsdmi ()));
3286 /* As above, but also checks that DECL is automatic. */
3288 bool
3289 outer_automatic_var_p (tree decl)
3291 return (outer_var_p (decl)
3292 && !TREE_STATIC (decl));
3295 /* DECL satisfies outer_automatic_var_p. Possibly complain about it or
3296 rewrite it for lambda capture.
3298 If ODR_USE is true, we're being called from mark_use, and we complain about
3299 use of constant variables. If ODR_USE is false, we're being called for the
3300 id-expression, and we do lambda capture. */
3302 tree
3303 process_outer_var_ref (tree decl, tsubst_flags_t complain, bool odr_use)
3305 if (cp_unevaluated_operand)
3306 /* It's not a use (3.2) if we're in an unevaluated context. */
3307 return decl;
3308 if (decl == error_mark_node)
3309 return decl;
3311 tree context = DECL_CONTEXT (decl);
3312 tree containing_function = current_function_decl;
3313 tree lambda_stack = NULL_TREE;
3314 tree lambda_expr = NULL_TREE;
3315 tree initializer = convert_from_reference (decl);
3317 /* Mark it as used now even if the use is ill-formed. */
3318 if (!mark_used (decl, complain))
3319 return error_mark_node;
3321 if (parsing_nsdmi ())
3322 containing_function = NULL_TREE;
3324 if (containing_function && LAMBDA_FUNCTION_P (containing_function))
3326 /* Check whether we've already built a proxy. */
3327 tree var = decl;
3328 while (is_capture_proxy_with_ref (var))
3329 var = DECL_CAPTURED_VARIABLE (var);
3330 tree d = retrieve_local_specialization (var);
3332 if (d && d != decl && is_capture_proxy (d))
3334 if (DECL_CONTEXT (d) == containing_function)
3335 /* We already have an inner proxy. */
3336 return d;
3337 else
3338 /* We need to capture an outer proxy. */
3339 return process_outer_var_ref (d, complain, odr_use);
3343 /* If we are in a lambda function, we can move out until we hit
3344 1. the context,
3345 2. a non-lambda function, or
3346 3. a non-default capturing lambda function. */
3347 while (context != containing_function
3348 /* containing_function can be null with invalid generic lambdas. */
3349 && containing_function
3350 && LAMBDA_FUNCTION_P (containing_function))
3352 tree closure = DECL_CONTEXT (containing_function);
3353 lambda_expr = CLASSTYPE_LAMBDA_EXPR (closure);
3355 if (TYPE_CLASS_SCOPE_P (closure))
3356 /* A lambda in an NSDMI (c++/64496). */
3357 break;
3359 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr)
3360 == CPLD_NONE)
3361 break;
3363 lambda_stack = tree_cons (NULL_TREE,
3364 lambda_expr,
3365 lambda_stack);
3367 containing_function
3368 = decl_function_context (containing_function);
3371 /* In a lambda within a template, wait until instantiation
3372 time to implicitly capture. */
3373 if (context == containing_function
3374 && DECL_TEMPLATE_INFO (containing_function)
3375 && uses_template_parms (DECL_TI_ARGS (containing_function)))
3376 return decl;
3378 if (lambda_expr && VAR_P (decl)
3379 && DECL_ANON_UNION_VAR_P (decl))
3381 if (complain & tf_error)
3382 error ("cannot capture member %qD of anonymous union", decl);
3383 return error_mark_node;
3385 /* Do lambda capture when processing the id-expression, not when
3386 odr-using a variable. */
3387 if (!odr_use && context == containing_function)
3389 decl = add_default_capture (lambda_stack,
3390 /*id=*/DECL_NAME (decl),
3391 initializer);
3393 /* Only an odr-use of an outer automatic variable causes an
3394 error, and a constant variable can decay to a prvalue
3395 constant without odr-use. So don't complain yet. */
3396 else if (!odr_use && decl_constant_var_p (decl))
3397 return decl;
3398 else if (lambda_expr)
3400 if (complain & tf_error)
3402 error ("%qD is not captured", decl);
3403 tree closure = LAMBDA_EXPR_CLOSURE (lambda_expr);
3404 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr)
3405 == CPLD_NONE)
3406 inform (location_of (closure),
3407 "the lambda has no capture-default");
3408 else if (TYPE_CLASS_SCOPE_P (closure))
3409 inform (UNKNOWN_LOCATION, "lambda in local class %q+T cannot "
3410 "capture variables from the enclosing context",
3411 TYPE_CONTEXT (closure));
3412 inform (DECL_SOURCE_LOCATION (decl), "%q#D declared here", decl);
3414 return error_mark_node;
3416 else
3418 if (complain & tf_error)
3420 error (VAR_P (decl)
3421 ? G_("use of local variable with automatic storage from "
3422 "containing function")
3423 : G_("use of parameter from containing function"));
3424 inform (DECL_SOURCE_LOCATION (decl), "%q#D declared here", decl);
3426 return error_mark_node;
3428 return decl;
3431 /* ID_EXPRESSION is a representation of parsed, but unprocessed,
3432 id-expression. (See cp_parser_id_expression for details.) SCOPE,
3433 if non-NULL, is the type or namespace used to explicitly qualify
3434 ID_EXPRESSION. DECL is the entity to which that name has been
3435 resolved.
3437 *CONSTANT_EXPRESSION_P is true if we are presently parsing a
3438 constant-expression. In that case, *NON_CONSTANT_EXPRESSION_P will
3439 be set to true if this expression isn't permitted in a
3440 constant-expression, but it is otherwise not set by this function.
3441 *ALLOW_NON_CONSTANT_EXPRESSION_P is true if we are parsing a
3442 constant-expression, but a non-constant expression is also
3443 permissible.
3445 DONE is true if this expression is a complete postfix-expression;
3446 it is false if this expression is followed by '->', '[', '(', etc.
3447 ADDRESS_P is true iff this expression is the operand of '&'.
3448 TEMPLATE_P is true iff the qualified-id was of the form
3449 "A::template B". TEMPLATE_ARG_P is true iff this qualified name
3450 appears as a template argument.
3452 If an error occurs, and it is the kind of error that might cause
3453 the parser to abort a tentative parse, *ERROR_MSG is filled in. It
3454 is the caller's responsibility to issue the message. *ERROR_MSG
3455 will be a string with static storage duration, so the caller need
3456 not "free" it.
3458 Return an expression for the entity, after issuing appropriate
3459 diagnostics. This function is also responsible for transforming a
3460 reference to a non-static member into a COMPONENT_REF that makes
3461 the use of "this" explicit.
3463 Upon return, *IDK will be filled in appropriately. */
3464 cp_expr
3465 finish_id_expression (tree id_expression,
3466 tree decl,
3467 tree scope,
3468 cp_id_kind *idk,
3469 bool integral_constant_expression_p,
3470 bool allow_non_integral_constant_expression_p,
3471 bool *non_integral_constant_expression_p,
3472 bool template_p,
3473 bool done,
3474 bool address_p,
3475 bool template_arg_p,
3476 const char **error_msg,
3477 location_t location)
3479 decl = strip_using_decl (decl);
3481 /* Initialize the output parameters. */
3482 *idk = CP_ID_KIND_NONE;
3483 *error_msg = NULL;
3485 if (id_expression == error_mark_node)
3486 return error_mark_node;
3487 /* If we have a template-id, then no further lookup is
3488 required. If the template-id was for a template-class, we
3489 will sometimes have a TYPE_DECL at this point. */
3490 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3491 || TREE_CODE (decl) == TYPE_DECL)
3493 /* Look up the name. */
3494 else
3496 if (decl == error_mark_node)
3498 /* Name lookup failed. */
3499 if (scope
3500 && (!TYPE_P (scope)
3501 || (!dependent_type_p (scope)
3502 && !(identifier_p (id_expression)
3503 && IDENTIFIER_CONV_OP_P (id_expression)
3504 && dependent_type_p (TREE_TYPE (id_expression))))))
3506 /* If the qualifying type is non-dependent (and the name
3507 does not name a conversion operator to a dependent
3508 type), issue an error. */
3509 qualified_name_lookup_error (scope, id_expression, decl, location);
3510 return error_mark_node;
3512 else if (!scope)
3514 /* It may be resolved via Koenig lookup. */
3515 *idk = CP_ID_KIND_UNQUALIFIED;
3516 return id_expression;
3518 else
3519 decl = id_expression;
3521 /* If DECL is a variable that would be out of scope under
3522 ANSI/ISO rules, but in scope in the ARM, name lookup
3523 will succeed. Issue a diagnostic here. */
3524 else
3525 decl = check_for_out_of_scope_variable (decl);
3527 /* Remember that the name was used in the definition of
3528 the current class so that we can check later to see if
3529 the meaning would have been different after the class
3530 was entirely defined. */
3531 if (!scope && decl != error_mark_node && identifier_p (id_expression))
3532 maybe_note_name_used_in_class (id_expression, decl);
3534 /* A use in unevaluated operand might not be instantiated appropriately
3535 if tsubst_copy builds a dummy parm, or if we never instantiate a
3536 generic lambda, so mark it now. */
3537 if (processing_template_decl && cp_unevaluated_operand)
3538 mark_type_use (decl);
3540 /* Disallow uses of local variables from containing functions, except
3541 within lambda-expressions. */
3542 if (outer_automatic_var_p (decl))
3544 decl = process_outer_var_ref (decl, tf_warning_or_error);
3545 if (decl == error_mark_node)
3546 return error_mark_node;
3549 /* Also disallow uses of function parameters outside the function
3550 body, except inside an unevaluated context (i.e. decltype). */
3551 if (TREE_CODE (decl) == PARM_DECL
3552 && DECL_CONTEXT (decl) == NULL_TREE
3553 && !cp_unevaluated_operand)
3555 *error_msg = G_("use of parameter outside function body");
3556 return error_mark_node;
3560 /* If we didn't find anything, or what we found was a type,
3561 then this wasn't really an id-expression. */
3562 if (TREE_CODE (decl) == TEMPLATE_DECL
3563 && !DECL_FUNCTION_TEMPLATE_P (decl))
3565 *error_msg = G_("missing template arguments");
3566 return error_mark_node;
3568 else if (TREE_CODE (decl) == TYPE_DECL
3569 || TREE_CODE (decl) == NAMESPACE_DECL)
3571 *error_msg = G_("expected primary-expression");
3572 return error_mark_node;
3575 /* If the name resolved to a template parameter, there is no
3576 need to look it up again later. */
3577 if ((TREE_CODE (decl) == CONST_DECL && DECL_TEMPLATE_PARM_P (decl))
3578 || TREE_CODE (decl) == TEMPLATE_PARM_INDEX)
3580 tree r;
3582 *idk = CP_ID_KIND_NONE;
3583 if (TREE_CODE (decl) == TEMPLATE_PARM_INDEX)
3584 decl = TEMPLATE_PARM_DECL (decl);
3585 r = convert_from_reference (DECL_INITIAL (decl));
3587 if (integral_constant_expression_p
3588 && !dependent_type_p (TREE_TYPE (decl))
3589 && !(INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (r))))
3591 if (!allow_non_integral_constant_expression_p)
3592 error ("template parameter %qD of type %qT is not allowed in "
3593 "an integral constant expression because it is not of "
3594 "integral or enumeration type", decl, TREE_TYPE (decl));
3595 *non_integral_constant_expression_p = true;
3597 return r;
3599 else
3601 bool dependent_p = type_dependent_expression_p (decl);
3603 /* If the declaration was explicitly qualified indicate
3604 that. The semantics of `A::f(3)' are different than
3605 `f(3)' if `f' is virtual. */
3606 *idk = (scope
3607 ? CP_ID_KIND_QUALIFIED
3608 : (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3609 ? CP_ID_KIND_TEMPLATE_ID
3610 : (dependent_p
3611 ? CP_ID_KIND_UNQUALIFIED_DEPENDENT
3612 : CP_ID_KIND_UNQUALIFIED)));
3614 if (dependent_p
3615 && DECL_P (decl)
3616 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (decl)))
3617 /* Dependent type attributes on the decl mean that the TREE_TYPE is
3618 wrong, so just return the identifier. */
3619 return id_expression;
3621 if (TREE_CODE (decl) == NAMESPACE_DECL)
3623 error ("use of namespace %qD as expression", decl);
3624 return error_mark_node;
3626 else if (DECL_CLASS_TEMPLATE_P (decl))
3628 error ("use of class template %qT as expression", decl);
3629 return error_mark_node;
3631 else if (TREE_CODE (decl) == TREE_LIST)
3633 /* Ambiguous reference to base members. */
3634 error ("request for member %qD is ambiguous in "
3635 "multiple inheritance lattice", id_expression);
3636 print_candidates (decl);
3637 return error_mark_node;
3640 /* Mark variable-like entities as used. Functions are similarly
3641 marked either below or after overload resolution. */
3642 if ((VAR_P (decl)
3643 || TREE_CODE (decl) == PARM_DECL
3644 || TREE_CODE (decl) == CONST_DECL
3645 || TREE_CODE (decl) == RESULT_DECL)
3646 && !mark_used (decl))
3647 return error_mark_node;
3649 /* Only certain kinds of names are allowed in constant
3650 expression. Template parameters have already
3651 been handled above. */
3652 if (! error_operand_p (decl)
3653 && !dependent_p
3654 && integral_constant_expression_p
3655 && ! decl_constant_var_p (decl)
3656 && TREE_CODE (decl) != CONST_DECL
3657 && ! builtin_valid_in_constant_expr_p (decl))
3659 if (!allow_non_integral_constant_expression_p)
3661 error ("%qD cannot appear in a constant-expression", decl);
3662 return error_mark_node;
3664 *non_integral_constant_expression_p = true;
3667 tree wrap;
3668 if (VAR_P (decl)
3669 && !cp_unevaluated_operand
3670 && !processing_template_decl
3671 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl))
3672 && CP_DECL_THREAD_LOCAL_P (decl)
3673 && (wrap = get_tls_wrapper_fn (decl)))
3675 /* Replace an evaluated use of the thread_local variable with
3676 a call to its wrapper. */
3677 decl = build_cxx_call (wrap, 0, NULL, tf_warning_or_error);
3679 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3680 && !dependent_p
3681 && variable_template_p (TREE_OPERAND (decl, 0)))
3683 decl = finish_template_variable (decl);
3684 mark_used (decl);
3685 decl = convert_from_reference (decl);
3687 else if (scope)
3689 if (TREE_CODE (decl) == SCOPE_REF)
3691 gcc_assert (same_type_p (scope, TREE_OPERAND (decl, 0)));
3692 decl = TREE_OPERAND (decl, 1);
3695 decl = (adjust_result_of_qualified_name_lookup
3696 (decl, scope, current_nonlambda_class_type()));
3698 if (TREE_CODE (decl) == FUNCTION_DECL)
3699 mark_used (decl);
3701 if (TYPE_P (scope))
3702 decl = finish_qualified_id_expr (scope,
3703 decl,
3704 done,
3705 address_p,
3706 template_p,
3707 template_arg_p,
3708 tf_warning_or_error);
3709 else
3710 decl = convert_from_reference (decl);
3712 else if (TREE_CODE (decl) == FIELD_DECL)
3714 /* Since SCOPE is NULL here, this is an unqualified name.
3715 Access checking has been performed during name lookup
3716 already. Turn off checking to avoid duplicate errors. */
3717 push_deferring_access_checks (dk_no_check);
3718 decl = finish_non_static_data_member (decl, NULL_TREE,
3719 /*qualifying_scope=*/NULL_TREE);
3720 pop_deferring_access_checks ();
3722 else if (is_overloaded_fn (decl))
3724 tree first_fn = get_first_fn (decl);
3726 if (TREE_CODE (first_fn) == TEMPLATE_DECL)
3727 first_fn = DECL_TEMPLATE_RESULT (first_fn);
3729 /* [basic.def.odr]: "A function whose name appears as a
3730 potentially-evaluated expression is odr-used if it is the unique
3731 lookup result".
3733 But only mark it if it's a complete postfix-expression; in a call,
3734 ADL might select a different function, and we'll call mark_used in
3735 build_over_call. */
3736 if (done
3737 && !really_overloaded_fn (decl)
3738 && !mark_used (first_fn))
3739 return error_mark_node;
3741 if (!template_arg_p
3742 && TREE_CODE (first_fn) == FUNCTION_DECL
3743 && DECL_FUNCTION_MEMBER_P (first_fn)
3744 && !shared_member_p (decl))
3746 /* A set of member functions. */
3747 decl = maybe_dummy_object (DECL_CONTEXT (first_fn), 0);
3748 return finish_class_member_access_expr (decl, id_expression,
3749 /*template_p=*/false,
3750 tf_warning_or_error);
3753 decl = baselink_for_fns (decl);
3755 else
3757 if (DECL_P (decl) && DECL_NONLOCAL (decl)
3758 && DECL_CLASS_SCOPE_P (decl))
3760 tree context = context_for_name_lookup (decl);
3761 if (context != current_class_type)
3763 tree path = currently_open_derived_class (context);
3764 perform_or_defer_access_check (TYPE_BINFO (path),
3765 decl, decl,
3766 tf_warning_or_error);
3770 decl = convert_from_reference (decl);
3774 return cp_expr (decl, location);
3777 /* Implement the __typeof keyword: Return the type of EXPR, suitable for
3778 use as a type-specifier. */
3780 tree
3781 finish_typeof (tree expr)
3783 tree type;
3785 if (type_dependent_expression_p (expr))
3787 type = cxx_make_type (TYPEOF_TYPE);
3788 TYPEOF_TYPE_EXPR (type) = expr;
3789 SET_TYPE_STRUCTURAL_EQUALITY (type);
3791 return type;
3794 expr = mark_type_use (expr);
3796 type = unlowered_expr_type (expr);
3798 if (!type || type == unknown_type_node)
3800 error ("type of %qE is unknown", expr);
3801 return error_mark_node;
3804 return type;
3807 /* Implement the __underlying_type keyword: Return the underlying
3808 type of TYPE, suitable for use as a type-specifier. */
3810 tree
3811 finish_underlying_type (tree type)
3813 tree underlying_type;
3815 if (processing_template_decl)
3817 underlying_type = cxx_make_type (UNDERLYING_TYPE);
3818 UNDERLYING_TYPE_TYPE (underlying_type) = type;
3819 SET_TYPE_STRUCTURAL_EQUALITY (underlying_type);
3821 return underlying_type;
3824 if (!complete_type_or_else (type, NULL_TREE))
3825 return error_mark_node;
3827 if (TREE_CODE (type) != ENUMERAL_TYPE)
3829 error ("%qT is not an enumeration type", type);
3830 return error_mark_node;
3833 underlying_type = ENUM_UNDERLYING_TYPE (type);
3835 /* Fixup necessary in this case because ENUM_UNDERLYING_TYPE
3836 includes TYPE_MIN_VALUE and TYPE_MAX_VALUE information.
3837 See finish_enum_value_list for details. */
3838 if (!ENUM_FIXED_UNDERLYING_TYPE_P (type))
3839 underlying_type
3840 = c_common_type_for_mode (TYPE_MODE (underlying_type),
3841 TYPE_UNSIGNED (underlying_type));
3843 return underlying_type;
3846 /* Implement the __direct_bases keyword: Return the direct base classes
3847 of type */
3849 tree
3850 calculate_direct_bases (tree type)
3852 vec<tree, va_gc> *vector = make_tree_vector();
3853 tree bases_vec = NULL_TREE;
3854 vec<tree, va_gc> *base_binfos;
3855 tree binfo;
3856 unsigned i;
3858 complete_type (type);
3860 if (!NON_UNION_CLASS_TYPE_P (type))
3861 return make_tree_vec (0);
3863 base_binfos = BINFO_BASE_BINFOS (TYPE_BINFO (type));
3865 /* Virtual bases are initialized first */
3866 for (i = 0; base_binfos->iterate (i, &binfo); i++)
3868 if (BINFO_VIRTUAL_P (binfo))
3870 vec_safe_push (vector, binfo);
3874 /* Now non-virtuals */
3875 for (i = 0; base_binfos->iterate (i, &binfo); i++)
3877 if (!BINFO_VIRTUAL_P (binfo))
3879 vec_safe_push (vector, binfo);
3884 bases_vec = make_tree_vec (vector->length ());
3886 for (i = 0; i < vector->length (); ++i)
3888 TREE_VEC_ELT (bases_vec, i) = BINFO_TYPE ((*vector)[i]);
3890 return bases_vec;
3893 /* Implement the __bases keyword: Return the base classes
3894 of type */
3896 /* Find morally non-virtual base classes by walking binfo hierarchy */
3897 /* Virtual base classes are handled separately in finish_bases */
3899 static tree
3900 dfs_calculate_bases_pre (tree binfo, void * /*data_*/)
3902 /* Don't walk bases of virtual bases */
3903 return BINFO_VIRTUAL_P (binfo) ? dfs_skip_bases : NULL_TREE;
3906 static tree
3907 dfs_calculate_bases_post (tree binfo, void *data_)
3909 vec<tree, va_gc> **data = ((vec<tree, va_gc> **) data_);
3910 if (!BINFO_VIRTUAL_P (binfo))
3912 vec_safe_push (*data, BINFO_TYPE (binfo));
3914 return NULL_TREE;
3917 /* Calculates the morally non-virtual base classes of a class */
3918 static vec<tree, va_gc> *
3919 calculate_bases_helper (tree type)
3921 vec<tree, va_gc> *vector = make_tree_vector();
3923 /* Now add non-virtual base classes in order of construction */
3924 if (TYPE_BINFO (type))
3925 dfs_walk_all (TYPE_BINFO (type),
3926 dfs_calculate_bases_pre, dfs_calculate_bases_post, &vector);
3927 return vector;
3930 tree
3931 calculate_bases (tree type)
3933 vec<tree, va_gc> *vector = make_tree_vector();
3934 tree bases_vec = NULL_TREE;
3935 unsigned i;
3936 vec<tree, va_gc> *vbases;
3937 vec<tree, va_gc> *nonvbases;
3938 tree binfo;
3940 complete_type (type);
3942 if (!NON_UNION_CLASS_TYPE_P (type))
3943 return make_tree_vec (0);
3945 /* First go through virtual base classes */
3946 for (vbases = CLASSTYPE_VBASECLASSES (type), i = 0;
3947 vec_safe_iterate (vbases, i, &binfo); i++)
3949 vec<tree, va_gc> *vbase_bases;
3950 vbase_bases = calculate_bases_helper (BINFO_TYPE (binfo));
3951 vec_safe_splice (vector, vbase_bases);
3952 release_tree_vector (vbase_bases);
3955 /* Now for the non-virtual bases */
3956 nonvbases = calculate_bases_helper (type);
3957 vec_safe_splice (vector, nonvbases);
3958 release_tree_vector (nonvbases);
3960 /* Note that during error recovery vector->length can even be zero. */
3961 if (vector->length () > 1)
3963 /* Last element is entire class, so don't copy */
3964 bases_vec = make_tree_vec (vector->length() - 1);
3966 for (i = 0; i < vector->length () - 1; ++i)
3967 TREE_VEC_ELT (bases_vec, i) = (*vector)[i];
3969 else
3970 bases_vec = make_tree_vec (0);
3972 release_tree_vector (vector);
3973 return bases_vec;
3976 tree
3977 finish_bases (tree type, bool direct)
3979 tree bases = NULL_TREE;
3981 if (!processing_template_decl)
3983 /* Parameter packs can only be used in templates */
3984 error ("Parameter pack __bases only valid in template declaration");
3985 return error_mark_node;
3988 bases = cxx_make_type (BASES);
3989 BASES_TYPE (bases) = type;
3990 BASES_DIRECT (bases) = direct;
3991 SET_TYPE_STRUCTURAL_EQUALITY (bases);
3993 return bases;
3996 /* Perform C++-specific checks for __builtin_offsetof before calling
3997 fold_offsetof. */
3999 tree
4000 finish_offsetof (tree object_ptr, tree expr, location_t loc)
4002 /* If we're processing a template, we can't finish the semantics yet.
4003 Otherwise we can fold the entire expression now. */
4004 if (processing_template_decl)
4006 expr = build2 (OFFSETOF_EXPR, size_type_node, expr, object_ptr);
4007 SET_EXPR_LOCATION (expr, loc);
4008 return expr;
4011 if (TREE_CODE (expr) == PSEUDO_DTOR_EXPR)
4013 error ("cannot apply %<offsetof%> to destructor %<~%T%>",
4014 TREE_OPERAND (expr, 2));
4015 return error_mark_node;
4017 if (TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE
4018 || TREE_CODE (TREE_TYPE (expr)) == METHOD_TYPE
4019 || TREE_TYPE (expr) == unknown_type_node)
4021 if (INDIRECT_REF_P (expr))
4022 error ("second operand of %<offsetof%> is neither a single "
4023 "identifier nor a sequence of member accesses and "
4024 "array references");
4025 else
4027 if (TREE_CODE (expr) == COMPONENT_REF
4028 || TREE_CODE (expr) == COMPOUND_EXPR)
4029 expr = TREE_OPERAND (expr, 1);
4030 error ("cannot apply %<offsetof%> to member function %qD", expr);
4032 return error_mark_node;
4034 if (REFERENCE_REF_P (expr))
4035 expr = TREE_OPERAND (expr, 0);
4036 if (!complete_type_or_else (TREE_TYPE (TREE_TYPE (object_ptr)), object_ptr))
4037 return error_mark_node;
4038 if (warn_invalid_offsetof
4039 && CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (object_ptr)))
4040 && CLASSTYPE_NON_STD_LAYOUT (TREE_TYPE (TREE_TYPE (object_ptr)))
4041 && cp_unevaluated_operand == 0)
4042 pedwarn (loc, OPT_Winvalid_offsetof,
4043 "offsetof within non-standard-layout type %qT is undefined",
4044 TREE_TYPE (TREE_TYPE (object_ptr)));
4045 return fold_offsetof (expr);
4048 /* Replace the AGGR_INIT_EXPR at *TP with an equivalent CALL_EXPR. This
4049 function is broken out from the above for the benefit of the tree-ssa
4050 project. */
4052 void
4053 simplify_aggr_init_expr (tree *tp)
4055 tree aggr_init_expr = *tp;
4057 /* Form an appropriate CALL_EXPR. */
4058 tree fn = AGGR_INIT_EXPR_FN (aggr_init_expr);
4059 tree slot = AGGR_INIT_EXPR_SLOT (aggr_init_expr);
4060 tree type = TREE_TYPE (slot);
4062 tree call_expr;
4063 enum style_t { ctor, arg, pcc } style;
4065 if (AGGR_INIT_VIA_CTOR_P (aggr_init_expr))
4066 style = ctor;
4067 #ifdef PCC_STATIC_STRUCT_RETURN
4068 else if (1)
4069 style = pcc;
4070 #endif
4071 else
4073 gcc_assert (TREE_ADDRESSABLE (type));
4074 style = arg;
4077 call_expr = build_call_array_loc (input_location,
4078 TREE_TYPE (TREE_TYPE (TREE_TYPE (fn))),
4080 aggr_init_expr_nargs (aggr_init_expr),
4081 AGGR_INIT_EXPR_ARGP (aggr_init_expr));
4082 TREE_NOTHROW (call_expr) = TREE_NOTHROW (aggr_init_expr);
4083 CALL_FROM_THUNK_P (call_expr) = AGGR_INIT_FROM_THUNK_P (aggr_init_expr);
4084 CALL_EXPR_OPERATOR_SYNTAX (call_expr)
4085 = CALL_EXPR_OPERATOR_SYNTAX (aggr_init_expr);
4086 CALL_EXPR_ORDERED_ARGS (call_expr) = CALL_EXPR_ORDERED_ARGS (aggr_init_expr);
4087 CALL_EXPR_REVERSE_ARGS (call_expr) = CALL_EXPR_REVERSE_ARGS (aggr_init_expr);
4089 if (style == ctor)
4091 /* Replace the first argument to the ctor with the address of the
4092 slot. */
4093 cxx_mark_addressable (slot);
4094 CALL_EXPR_ARG (call_expr, 0) =
4095 build1 (ADDR_EXPR, build_pointer_type (type), slot);
4097 else if (style == arg)
4099 /* Just mark it addressable here, and leave the rest to
4100 expand_call{,_inline}. */
4101 cxx_mark_addressable (slot);
4102 CALL_EXPR_RETURN_SLOT_OPT (call_expr) = true;
4103 call_expr = build2 (INIT_EXPR, TREE_TYPE (call_expr), slot, call_expr);
4105 else if (style == pcc)
4107 /* If we're using the non-reentrant PCC calling convention, then we
4108 need to copy the returned value out of the static buffer into the
4109 SLOT. */
4110 push_deferring_access_checks (dk_no_check);
4111 call_expr = build_aggr_init (slot, call_expr,
4112 DIRECT_BIND | LOOKUP_ONLYCONVERTING,
4113 tf_warning_or_error);
4114 pop_deferring_access_checks ();
4115 call_expr = build2 (COMPOUND_EXPR, TREE_TYPE (slot), call_expr, slot);
4118 if (AGGR_INIT_ZERO_FIRST (aggr_init_expr))
4120 tree init = build_zero_init (type, NULL_TREE,
4121 /*static_storage_p=*/false);
4122 init = build2 (INIT_EXPR, void_type_node, slot, init);
4123 call_expr = build2 (COMPOUND_EXPR, TREE_TYPE (call_expr),
4124 init, call_expr);
4127 *tp = call_expr;
4130 /* Emit all thunks to FN that should be emitted when FN is emitted. */
4132 void
4133 emit_associated_thunks (tree fn)
4135 /* When we use vcall offsets, we emit thunks with the virtual
4136 functions to which they thunk. The whole point of vcall offsets
4137 is so that you can know statically the entire set of thunks that
4138 will ever be needed for a given virtual function, thereby
4139 enabling you to output all the thunks with the function itself. */
4140 if (DECL_VIRTUAL_P (fn)
4141 /* Do not emit thunks for extern template instantiations. */
4142 && ! DECL_REALLY_EXTERN (fn))
4144 tree thunk;
4146 for (thunk = DECL_THUNKS (fn); thunk; thunk = DECL_CHAIN (thunk))
4148 if (!THUNK_ALIAS (thunk))
4150 use_thunk (thunk, /*emit_p=*/1);
4151 if (DECL_RESULT_THUNK_P (thunk))
4153 tree probe;
4155 for (probe = DECL_THUNKS (thunk);
4156 probe; probe = DECL_CHAIN (probe))
4157 use_thunk (probe, /*emit_p=*/1);
4160 else
4161 gcc_assert (!DECL_THUNKS (thunk));
4166 /* Generate RTL for FN. */
4168 bool
4169 expand_or_defer_fn_1 (tree fn)
4171 /* When the parser calls us after finishing the body of a template
4172 function, we don't really want to expand the body. */
4173 if (processing_template_decl)
4175 /* Normally, collection only occurs in rest_of_compilation. So,
4176 if we don't collect here, we never collect junk generated
4177 during the processing of templates until we hit a
4178 non-template function. It's not safe to do this inside a
4179 nested class, though, as the parser may have local state that
4180 is not a GC root. */
4181 if (!function_depth)
4182 ggc_collect ();
4183 return false;
4186 gcc_assert (DECL_SAVED_TREE (fn));
4188 /* We make a decision about linkage for these functions at the end
4189 of the compilation. Until that point, we do not want the back
4190 end to output them -- but we do want it to see the bodies of
4191 these functions so that it can inline them as appropriate. */
4192 if (DECL_DECLARED_INLINE_P (fn) || DECL_IMPLICIT_INSTANTIATION (fn))
4194 if (DECL_INTERFACE_KNOWN (fn))
4195 /* We've already made a decision as to how this function will
4196 be handled. */;
4197 else if (!at_eof)
4198 tentative_decl_linkage (fn);
4199 else
4200 import_export_decl (fn);
4202 /* If the user wants us to keep all inline functions, then mark
4203 this function as needed so that finish_file will make sure to
4204 output it later. Similarly, all dllexport'd functions must
4205 be emitted; there may be callers in other DLLs. */
4206 if (DECL_DECLARED_INLINE_P (fn)
4207 && !DECL_REALLY_EXTERN (fn)
4208 && (flag_keep_inline_functions
4209 || (flag_keep_inline_dllexport
4210 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (fn)))))
4212 mark_needed (fn);
4213 DECL_EXTERNAL (fn) = 0;
4217 /* If this is a constructor or destructor body, we have to clone
4218 it. */
4219 if (maybe_clone_body (fn))
4221 /* We don't want to process FN again, so pretend we've written
4222 it out, even though we haven't. */
4223 TREE_ASM_WRITTEN (fn) = 1;
4224 /* If this is a constexpr function, keep DECL_SAVED_TREE. */
4225 if (!DECL_DECLARED_CONSTEXPR_P (fn))
4226 DECL_SAVED_TREE (fn) = NULL_TREE;
4227 return false;
4230 /* There's no reason to do any of the work here if we're only doing
4231 semantic analysis; this code just generates RTL. */
4232 if (flag_syntax_only)
4233 return false;
4235 return true;
4238 void
4239 expand_or_defer_fn (tree fn)
4241 if (expand_or_defer_fn_1 (fn))
4243 function_depth++;
4245 /* Expand or defer, at the whim of the compilation unit manager. */
4246 cgraph_node::finalize_function (fn, function_depth > 1);
4247 emit_associated_thunks (fn);
4249 function_depth--;
4253 struct nrv_data
4255 nrv_data () : visited (37) {}
4257 tree var;
4258 tree result;
4259 hash_table<nofree_ptr_hash <tree_node> > visited;
4262 /* Helper function for walk_tree, used by finalize_nrv below. */
4264 static tree
4265 finalize_nrv_r (tree* tp, int* walk_subtrees, void* data)
4267 struct nrv_data *dp = (struct nrv_data *)data;
4268 tree_node **slot;
4270 /* No need to walk into types. There wouldn't be any need to walk into
4271 non-statements, except that we have to consider STMT_EXPRs. */
4272 if (TYPE_P (*tp))
4273 *walk_subtrees = 0;
4274 /* Change all returns to just refer to the RESULT_DECL; this is a nop,
4275 but differs from using NULL_TREE in that it indicates that we care
4276 about the value of the RESULT_DECL. */
4277 else if (TREE_CODE (*tp) == RETURN_EXPR)
4278 TREE_OPERAND (*tp, 0) = dp->result;
4279 /* Change all cleanups for the NRV to only run when an exception is
4280 thrown. */
4281 else if (TREE_CODE (*tp) == CLEANUP_STMT
4282 && CLEANUP_DECL (*tp) == dp->var)
4283 CLEANUP_EH_ONLY (*tp) = 1;
4284 /* Replace the DECL_EXPR for the NRV with an initialization of the
4285 RESULT_DECL, if needed. */
4286 else if (TREE_CODE (*tp) == DECL_EXPR
4287 && DECL_EXPR_DECL (*tp) == dp->var)
4289 tree init;
4290 if (DECL_INITIAL (dp->var)
4291 && DECL_INITIAL (dp->var) != error_mark_node)
4292 init = build2 (INIT_EXPR, void_type_node, dp->result,
4293 DECL_INITIAL (dp->var));
4294 else
4295 init = build_empty_stmt (EXPR_LOCATION (*tp));
4296 DECL_INITIAL (dp->var) = NULL_TREE;
4297 SET_EXPR_LOCATION (init, EXPR_LOCATION (*tp));
4298 *tp = init;
4300 /* And replace all uses of the NRV with the RESULT_DECL. */
4301 else if (*tp == dp->var)
4302 *tp = dp->result;
4304 /* Avoid walking into the same tree more than once. Unfortunately, we
4305 can't just use walk_tree_without duplicates because it would only call
4306 us for the first occurrence of dp->var in the function body. */
4307 slot = dp->visited.find_slot (*tp, INSERT);
4308 if (*slot)
4309 *walk_subtrees = 0;
4310 else
4311 *slot = *tp;
4313 /* Keep iterating. */
4314 return NULL_TREE;
4317 /* Called from finish_function to implement the named return value
4318 optimization by overriding all the RETURN_EXPRs and pertinent
4319 CLEANUP_STMTs and replacing all occurrences of VAR with RESULT, the
4320 RESULT_DECL for the function. */
4322 void
4323 finalize_nrv (tree *tp, tree var, tree result)
4325 struct nrv_data data;
4327 /* Copy name from VAR to RESULT. */
4328 DECL_NAME (result) = DECL_NAME (var);
4329 /* Don't forget that we take its address. */
4330 TREE_ADDRESSABLE (result) = TREE_ADDRESSABLE (var);
4331 /* Finally set DECL_VALUE_EXPR to avoid assigning
4332 a stack slot at -O0 for the original var and debug info
4333 uses RESULT location for VAR. */
4334 SET_DECL_VALUE_EXPR (var, result);
4335 DECL_HAS_VALUE_EXPR_P (var) = 1;
4337 data.var = var;
4338 data.result = result;
4339 cp_walk_tree (tp, finalize_nrv_r, &data, 0);
4342 /* Create CP_OMP_CLAUSE_INFO for clause C. Returns true if it is invalid. */
4344 bool
4345 cxx_omp_create_clause_info (tree c, tree type, bool need_default_ctor,
4346 bool need_copy_ctor, bool need_copy_assignment,
4347 bool need_dtor)
4349 int save_errorcount = errorcount;
4350 tree info, t;
4352 /* Always allocate 3 elements for simplicity. These are the
4353 function decls for the ctor, dtor, and assignment op.
4354 This layout is known to the three lang hooks,
4355 cxx_omp_clause_default_init, cxx_omp_clause_copy_init,
4356 and cxx_omp_clause_assign_op. */
4357 info = make_tree_vec (3);
4358 CP_OMP_CLAUSE_INFO (c) = info;
4360 if (need_default_ctor || need_copy_ctor)
4362 if (need_default_ctor)
4363 t = get_default_ctor (type);
4364 else
4365 t = get_copy_ctor (type, tf_warning_or_error);
4367 if (t && !trivial_fn_p (t))
4368 TREE_VEC_ELT (info, 0) = t;
4371 if (need_dtor && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
4372 TREE_VEC_ELT (info, 1) = get_dtor (type, tf_warning_or_error);
4374 if (need_copy_assignment)
4376 t = get_copy_assign (type);
4378 if (t && !trivial_fn_p (t))
4379 TREE_VEC_ELT (info, 2) = t;
4382 return errorcount != save_errorcount;
4385 /* If DECL is DECL_OMP_PRIVATIZED_MEMBER, return corresponding
4386 FIELD_DECL, otherwise return DECL itself. */
4388 static tree
4389 omp_clause_decl_field (tree decl)
4391 if (VAR_P (decl)
4392 && DECL_HAS_VALUE_EXPR_P (decl)
4393 && DECL_ARTIFICIAL (decl)
4394 && DECL_LANG_SPECIFIC (decl)
4395 && DECL_OMP_PRIVATIZED_MEMBER (decl))
4397 tree f = DECL_VALUE_EXPR (decl);
4398 if (INDIRECT_REF_P (f))
4399 f = TREE_OPERAND (f, 0);
4400 if (TREE_CODE (f) == COMPONENT_REF)
4402 f = TREE_OPERAND (f, 1);
4403 gcc_assert (TREE_CODE (f) == FIELD_DECL);
4404 return f;
4407 return NULL_TREE;
4410 /* Adjust DECL if needed for printing using %qE. */
4412 static tree
4413 omp_clause_printable_decl (tree decl)
4415 tree t = omp_clause_decl_field (decl);
4416 if (t)
4417 return t;
4418 return decl;
4421 /* For a FIELD_DECL F and corresponding DECL_OMP_PRIVATIZED_MEMBER
4422 VAR_DECL T that doesn't need a DECL_EXPR added, record it for
4423 privatization. */
4425 static void
4426 omp_note_field_privatization (tree f, tree t)
4428 if (!omp_private_member_map)
4429 omp_private_member_map = new hash_map<tree, tree>;
4430 tree &v = omp_private_member_map->get_or_insert (f);
4431 if (v == NULL_TREE)
4433 v = t;
4434 omp_private_member_vec.safe_push (f);
4435 /* Signal that we don't want to create DECL_EXPR for this dummy var. */
4436 omp_private_member_vec.safe_push (integer_zero_node);
4440 /* Privatize FIELD_DECL T, return corresponding DECL_OMP_PRIVATIZED_MEMBER
4441 dummy VAR_DECL. */
4443 tree
4444 omp_privatize_field (tree t, bool shared)
4446 tree m = finish_non_static_data_member (t, NULL_TREE, NULL_TREE);
4447 if (m == error_mark_node)
4448 return error_mark_node;
4449 if (!omp_private_member_map && !shared)
4450 omp_private_member_map = new hash_map<tree, tree>;
4451 if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE)
4453 gcc_assert (INDIRECT_REF_P (m));
4454 m = TREE_OPERAND (m, 0);
4456 tree vb = NULL_TREE;
4457 tree &v = shared ? vb : omp_private_member_map->get_or_insert (t);
4458 if (v == NULL_TREE)
4460 v = create_temporary_var (TREE_TYPE (m));
4461 retrofit_lang_decl (v);
4462 DECL_OMP_PRIVATIZED_MEMBER (v) = 1;
4463 SET_DECL_VALUE_EXPR (v, m);
4464 DECL_HAS_VALUE_EXPR_P (v) = 1;
4465 if (!shared)
4466 omp_private_member_vec.safe_push (t);
4468 return v;
4471 /* Helper function for handle_omp_array_sections. Called recursively
4472 to handle multiple array-section-subscripts. C is the clause,
4473 T current expression (initially OMP_CLAUSE_DECL), which is either
4474 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
4475 expression if specified, TREE_VALUE length expression if specified,
4476 TREE_CHAIN is what it has been specified after, or some decl.
4477 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
4478 set to true if any of the array-section-subscript could have length
4479 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
4480 first array-section-subscript which is known not to have length
4481 of one. Given say:
4482 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
4483 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
4484 all are or may have length of 1, array-section-subscript [:2] is the
4485 first one known not to have length 1. For array-section-subscript
4486 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
4487 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
4488 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
4489 case though, as some lengths could be zero. */
4491 static tree
4492 handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
4493 bool &maybe_zero_len, unsigned int &first_non_one,
4494 enum c_omp_region_type ort)
4496 tree ret, low_bound, length, type;
4497 if (TREE_CODE (t) != TREE_LIST)
4499 if (error_operand_p (t))
4500 return error_mark_node;
4501 if (REFERENCE_REF_P (t)
4502 && TREE_CODE (TREE_OPERAND (t, 0)) == COMPONENT_REF)
4503 t = TREE_OPERAND (t, 0);
4504 ret = t;
4505 if (TREE_CODE (t) == COMPONENT_REF
4506 && ort == C_ORT_OMP
4507 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
4508 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO
4509 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FROM)
4510 && !type_dependent_expression_p (t))
4512 if (TREE_CODE (TREE_OPERAND (t, 1)) == FIELD_DECL
4513 && DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
4515 error_at (OMP_CLAUSE_LOCATION (c),
4516 "bit-field %qE in %qs clause",
4517 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4518 return error_mark_node;
4520 while (TREE_CODE (t) == COMPONENT_REF)
4522 if (TREE_TYPE (TREE_OPERAND (t, 0))
4523 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == UNION_TYPE)
4525 error_at (OMP_CLAUSE_LOCATION (c),
4526 "%qE is a member of a union", t);
4527 return error_mark_node;
4529 t = TREE_OPERAND (t, 0);
4531 if (REFERENCE_REF_P (t))
4532 t = TREE_OPERAND (t, 0);
4534 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
4536 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
4537 return NULL_TREE;
4538 if (DECL_P (t))
4539 error_at (OMP_CLAUSE_LOCATION (c),
4540 "%qD is not a variable in %qs clause", t,
4541 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4542 else
4543 error_at (OMP_CLAUSE_LOCATION (c),
4544 "%qE is not a variable in %qs clause", t,
4545 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4546 return error_mark_node;
4548 else if (TREE_CODE (t) == PARM_DECL
4549 && DECL_ARTIFICIAL (t)
4550 && DECL_NAME (t) == this_identifier)
4552 error_at (OMP_CLAUSE_LOCATION (c),
4553 "%<this%> allowed in OpenMP only in %<declare simd%>"
4554 " clauses");
4555 return error_mark_node;
4557 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
4558 && VAR_P (t) && CP_DECL_THREAD_LOCAL_P (t))
4560 error_at (OMP_CLAUSE_LOCATION (c),
4561 "%qD is threadprivate variable in %qs clause", t,
4562 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4563 return error_mark_node;
4565 if (type_dependent_expression_p (ret))
4566 return NULL_TREE;
4567 ret = convert_from_reference (ret);
4568 return ret;
4571 if (ort == C_ORT_OMP
4572 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
4573 && TREE_CODE (TREE_CHAIN (t)) == FIELD_DECL)
4574 TREE_CHAIN (t) = omp_privatize_field (TREE_CHAIN (t), false);
4575 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
4576 maybe_zero_len, first_non_one, ort);
4577 if (ret == error_mark_node || ret == NULL_TREE)
4578 return ret;
4580 type = TREE_TYPE (ret);
4581 low_bound = TREE_PURPOSE (t);
4582 length = TREE_VALUE (t);
4583 if ((low_bound && type_dependent_expression_p (low_bound))
4584 || (length && type_dependent_expression_p (length)))
4585 return NULL_TREE;
4587 if (low_bound == error_mark_node || length == error_mark_node)
4588 return error_mark_node;
4590 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
4592 error_at (OMP_CLAUSE_LOCATION (c),
4593 "low bound %qE of array section does not have integral type",
4594 low_bound);
4595 return error_mark_node;
4597 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
4599 error_at (OMP_CLAUSE_LOCATION (c),
4600 "length %qE of array section does not have integral type",
4601 length);
4602 return error_mark_node;
4604 if (low_bound)
4605 low_bound = mark_rvalue_use (low_bound);
4606 if (length)
4607 length = mark_rvalue_use (length);
4608 /* We need to reduce to real constant-values for checks below. */
4609 if (length)
4610 length = fold_simple (length);
4611 if (low_bound)
4612 low_bound = fold_simple (low_bound);
4613 if (low_bound
4614 && TREE_CODE (low_bound) == INTEGER_CST
4615 && TYPE_PRECISION (TREE_TYPE (low_bound))
4616 > TYPE_PRECISION (sizetype))
4617 low_bound = fold_convert (sizetype, low_bound);
4618 if (length
4619 && TREE_CODE (length) == INTEGER_CST
4620 && TYPE_PRECISION (TREE_TYPE (length))
4621 > TYPE_PRECISION (sizetype))
4622 length = fold_convert (sizetype, length);
4623 if (low_bound == NULL_TREE)
4624 low_bound = integer_zero_node;
4626 if (length != NULL_TREE)
4628 if (!integer_nonzerop (length))
4630 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
4631 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
4633 if (integer_zerop (length))
4635 error_at (OMP_CLAUSE_LOCATION (c),
4636 "zero length array section in %qs clause",
4637 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4638 return error_mark_node;
4641 else
4642 maybe_zero_len = true;
4644 if (first_non_one == types.length ()
4645 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
4646 first_non_one++;
4648 if (TREE_CODE (type) == ARRAY_TYPE)
4650 if (length == NULL_TREE
4651 && (TYPE_DOMAIN (type) == NULL_TREE
4652 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
4654 error_at (OMP_CLAUSE_LOCATION (c),
4655 "for unknown bound array type length expression must "
4656 "be specified");
4657 return error_mark_node;
4659 if (TREE_CODE (low_bound) == INTEGER_CST
4660 && tree_int_cst_sgn (low_bound) == -1)
4662 error_at (OMP_CLAUSE_LOCATION (c),
4663 "negative low bound in array section in %qs clause",
4664 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4665 return error_mark_node;
4667 if (length != NULL_TREE
4668 && TREE_CODE (length) == INTEGER_CST
4669 && tree_int_cst_sgn (length) == -1)
4671 error_at (OMP_CLAUSE_LOCATION (c),
4672 "negative length in array section in %qs clause",
4673 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4674 return error_mark_node;
4676 if (TYPE_DOMAIN (type)
4677 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
4678 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
4679 == INTEGER_CST)
4681 tree size
4682 = fold_convert (sizetype, TYPE_MAX_VALUE (TYPE_DOMAIN (type)));
4683 size = size_binop (PLUS_EXPR, size, size_one_node);
4684 if (TREE_CODE (low_bound) == INTEGER_CST)
4686 if (tree_int_cst_lt (size, low_bound))
4688 error_at (OMP_CLAUSE_LOCATION (c),
4689 "low bound %qE above array section size "
4690 "in %qs clause", low_bound,
4691 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4692 return error_mark_node;
4694 if (tree_int_cst_equal (size, low_bound))
4696 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
4697 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
4699 error_at (OMP_CLAUSE_LOCATION (c),
4700 "zero length array section in %qs clause",
4701 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4702 return error_mark_node;
4704 maybe_zero_len = true;
4706 else if (length == NULL_TREE
4707 && first_non_one == types.length ()
4708 && tree_int_cst_equal
4709 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
4710 low_bound))
4711 first_non_one++;
4713 else if (length == NULL_TREE)
4715 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
4716 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION)
4717 maybe_zero_len = true;
4718 if (first_non_one == types.length ())
4719 first_non_one++;
4721 if (length && TREE_CODE (length) == INTEGER_CST)
4723 if (tree_int_cst_lt (size, length))
4725 error_at (OMP_CLAUSE_LOCATION (c),
4726 "length %qE above array section size "
4727 "in %qs clause", length,
4728 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4729 return error_mark_node;
4731 if (TREE_CODE (low_bound) == INTEGER_CST)
4733 tree lbpluslen
4734 = size_binop (PLUS_EXPR,
4735 fold_convert (sizetype, low_bound),
4736 fold_convert (sizetype, length));
4737 if (TREE_CODE (lbpluslen) == INTEGER_CST
4738 && tree_int_cst_lt (size, lbpluslen))
4740 error_at (OMP_CLAUSE_LOCATION (c),
4741 "high bound %qE above array section size "
4742 "in %qs clause", lbpluslen,
4743 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4744 return error_mark_node;
4749 else if (length == NULL_TREE)
4751 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
4752 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION)
4753 maybe_zero_len = true;
4754 if (first_non_one == types.length ())
4755 first_non_one++;
4758 /* For [lb:] we will need to evaluate lb more than once. */
4759 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
4761 tree lb = cp_save_expr (low_bound);
4762 if (lb != low_bound)
4764 TREE_PURPOSE (t) = lb;
4765 low_bound = lb;
4769 else if (TREE_CODE (type) == POINTER_TYPE)
4771 if (length == NULL_TREE)
4773 error_at (OMP_CLAUSE_LOCATION (c),
4774 "for pointer type length expression must be specified");
4775 return error_mark_node;
4777 if (length != NULL_TREE
4778 && TREE_CODE (length) == INTEGER_CST
4779 && tree_int_cst_sgn (length) == -1)
4781 error_at (OMP_CLAUSE_LOCATION (c),
4782 "negative length in array section in %qs clause",
4783 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4784 return error_mark_node;
4786 /* If there is a pointer type anywhere but in the very first
4787 array-section-subscript, the array section can't be contiguous. */
4788 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
4789 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
4791 error_at (OMP_CLAUSE_LOCATION (c),
4792 "array section is not contiguous in %qs clause",
4793 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4794 return error_mark_node;
4797 else
4799 error_at (OMP_CLAUSE_LOCATION (c),
4800 "%qE does not have pointer or array type", ret);
4801 return error_mark_node;
4803 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
4804 types.safe_push (TREE_TYPE (ret));
4805 /* We will need to evaluate lb more than once. */
4806 tree lb = cp_save_expr (low_bound);
4807 if (lb != low_bound)
4809 TREE_PURPOSE (t) = lb;
4810 low_bound = lb;
4812 ret = grok_array_decl (OMP_CLAUSE_LOCATION (c), ret, low_bound, false);
4813 return ret;
4816 /* Handle array sections for clause C. */
4818 static bool
4819 handle_omp_array_sections (tree c, enum c_omp_region_type ort)
4821 bool maybe_zero_len = false;
4822 unsigned int first_non_one = 0;
4823 auto_vec<tree, 10> types;
4824 tree first = handle_omp_array_sections_1 (c, OMP_CLAUSE_DECL (c), types,
4825 maybe_zero_len, first_non_one,
4826 ort);
4827 if (first == error_mark_node)
4828 return true;
4829 if (first == NULL_TREE)
4830 return false;
4831 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
4833 tree t = OMP_CLAUSE_DECL (c);
4834 tree tem = NULL_TREE;
4835 if (processing_template_decl)
4836 return false;
4837 /* Need to evaluate side effects in the length expressions
4838 if any. */
4839 while (TREE_CODE (t) == TREE_LIST)
4841 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
4843 if (tem == NULL_TREE)
4844 tem = TREE_VALUE (t);
4845 else
4846 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
4847 TREE_VALUE (t), tem);
4849 t = TREE_CHAIN (t);
4851 if (tem)
4852 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
4853 OMP_CLAUSE_DECL (c) = first;
4855 else
4857 unsigned int num = types.length (), i;
4858 tree t, side_effects = NULL_TREE, size = NULL_TREE;
4859 tree condition = NULL_TREE;
4861 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
4862 maybe_zero_len = true;
4863 if (processing_template_decl && maybe_zero_len)
4864 return false;
4866 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
4867 t = TREE_CHAIN (t))
4869 tree low_bound = TREE_PURPOSE (t);
4870 tree length = TREE_VALUE (t);
4872 i--;
4873 if (low_bound
4874 && TREE_CODE (low_bound) == INTEGER_CST
4875 && TYPE_PRECISION (TREE_TYPE (low_bound))
4876 > TYPE_PRECISION (sizetype))
4877 low_bound = fold_convert (sizetype, low_bound);
4878 if (length
4879 && TREE_CODE (length) == INTEGER_CST
4880 && TYPE_PRECISION (TREE_TYPE (length))
4881 > TYPE_PRECISION (sizetype))
4882 length = fold_convert (sizetype, length);
4883 if (low_bound == NULL_TREE)
4884 low_bound = integer_zero_node;
4885 if (!maybe_zero_len && i > first_non_one)
4887 if (integer_nonzerop (low_bound))
4888 goto do_warn_noncontiguous;
4889 if (length != NULL_TREE
4890 && TREE_CODE (length) == INTEGER_CST
4891 && TYPE_DOMAIN (types[i])
4892 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
4893 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
4894 == INTEGER_CST)
4896 tree size;
4897 size = size_binop (PLUS_EXPR,
4898 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
4899 size_one_node);
4900 if (!tree_int_cst_equal (length, size))
4902 do_warn_noncontiguous:
4903 error_at (OMP_CLAUSE_LOCATION (c),
4904 "array section is not contiguous in %qs "
4905 "clause",
4906 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4907 return true;
4910 if (!processing_template_decl
4911 && length != NULL_TREE
4912 && TREE_SIDE_EFFECTS (length))
4914 if (side_effects == NULL_TREE)
4915 side_effects = length;
4916 else
4917 side_effects = build2 (COMPOUND_EXPR,
4918 TREE_TYPE (side_effects),
4919 length, side_effects);
4922 else if (processing_template_decl)
4923 continue;
4924 else
4926 tree l;
4928 if (i > first_non_one
4929 && ((length && integer_nonzerop (length))
4930 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION))
4931 continue;
4932 if (length)
4933 l = fold_convert (sizetype, length);
4934 else
4936 l = size_binop (PLUS_EXPR,
4937 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
4938 size_one_node);
4939 l = size_binop (MINUS_EXPR, l,
4940 fold_convert (sizetype, low_bound));
4942 if (i > first_non_one)
4944 l = fold_build2 (NE_EXPR, boolean_type_node, l,
4945 size_zero_node);
4946 if (condition == NULL_TREE)
4947 condition = l;
4948 else
4949 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
4950 l, condition);
4952 else if (size == NULL_TREE)
4954 size = size_in_bytes (TREE_TYPE (types[i]));
4955 tree eltype = TREE_TYPE (types[num - 1]);
4956 while (TREE_CODE (eltype) == ARRAY_TYPE)
4957 eltype = TREE_TYPE (eltype);
4958 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
4959 size = size_binop (EXACT_DIV_EXPR, size,
4960 size_in_bytes (eltype));
4961 size = size_binop (MULT_EXPR, size, l);
4962 if (condition)
4963 size = fold_build3 (COND_EXPR, sizetype, condition,
4964 size, size_zero_node);
4966 else
4967 size = size_binop (MULT_EXPR, size, l);
4970 if (!processing_template_decl)
4972 if (side_effects)
4973 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
4974 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
4976 size = size_binop (MINUS_EXPR, size, size_one_node);
4977 tree index_type = build_index_type (size);
4978 tree eltype = TREE_TYPE (first);
4979 while (TREE_CODE (eltype) == ARRAY_TYPE)
4980 eltype = TREE_TYPE (eltype);
4981 tree type = build_array_type (eltype, index_type);
4982 tree ptype = build_pointer_type (eltype);
4983 if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE
4984 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (t))))
4985 t = convert_from_reference (t);
4986 else if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
4987 t = build_fold_addr_expr (t);
4988 tree t2 = build_fold_addr_expr (first);
4989 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
4990 ptrdiff_type_node, t2);
4991 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
4992 ptrdiff_type_node, t2,
4993 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
4994 ptrdiff_type_node, t));
4995 if (tree_fits_shwi_p (t2))
4996 t = build2 (MEM_REF, type, t,
4997 build_int_cst (ptype, tree_to_shwi (t2)));
4998 else
5000 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
5001 sizetype, t2);
5002 t = build2_loc (OMP_CLAUSE_LOCATION (c), POINTER_PLUS_EXPR,
5003 TREE_TYPE (t), t, t2);
5004 t = build2 (MEM_REF, type, t, build_int_cst (ptype, 0));
5006 OMP_CLAUSE_DECL (c) = t;
5007 return false;
5009 OMP_CLAUSE_DECL (c) = first;
5010 OMP_CLAUSE_SIZE (c) = size;
5011 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
5012 || (TREE_CODE (t) == COMPONENT_REF
5013 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE))
5014 return false;
5015 if (ort == C_ORT_OMP || ort == C_ORT_ACC)
5016 switch (OMP_CLAUSE_MAP_KIND (c))
5018 case GOMP_MAP_ALLOC:
5019 case GOMP_MAP_TO:
5020 case GOMP_MAP_FROM:
5021 case GOMP_MAP_TOFROM:
5022 case GOMP_MAP_ALWAYS_TO:
5023 case GOMP_MAP_ALWAYS_FROM:
5024 case GOMP_MAP_ALWAYS_TOFROM:
5025 case GOMP_MAP_RELEASE:
5026 case GOMP_MAP_DELETE:
5027 case GOMP_MAP_FORCE_TO:
5028 case GOMP_MAP_FORCE_FROM:
5029 case GOMP_MAP_FORCE_TOFROM:
5030 case GOMP_MAP_FORCE_PRESENT:
5031 OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (c) = 1;
5032 break;
5033 default:
5034 break;
5036 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
5037 OMP_CLAUSE_MAP);
5038 if ((ort & C_ORT_OMP_DECLARE_SIMD) != C_ORT_OMP && ort != C_ORT_ACC)
5039 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_POINTER);
5040 else if (TREE_CODE (t) == COMPONENT_REF)
5041 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ALWAYS_POINTER);
5042 else if (REFERENCE_REF_P (t)
5043 && TREE_CODE (TREE_OPERAND (t, 0)) == COMPONENT_REF)
5045 t = TREE_OPERAND (t, 0);
5046 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ALWAYS_POINTER);
5048 else
5049 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_FIRSTPRIVATE_POINTER);
5050 if (OMP_CLAUSE_MAP_KIND (c2) != GOMP_MAP_FIRSTPRIVATE_POINTER
5051 && !cxx_mark_addressable (t))
5052 return false;
5053 OMP_CLAUSE_DECL (c2) = t;
5054 t = build_fold_addr_expr (first);
5055 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
5056 ptrdiff_type_node, t);
5057 tree ptr = OMP_CLAUSE_DECL (c2);
5058 ptr = convert_from_reference (ptr);
5059 if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
5060 ptr = build_fold_addr_expr (ptr);
5061 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
5062 ptrdiff_type_node, t,
5063 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
5064 ptrdiff_type_node, ptr));
5065 OMP_CLAUSE_SIZE (c2) = t;
5066 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
5067 OMP_CLAUSE_CHAIN (c) = c2;
5068 ptr = OMP_CLAUSE_DECL (c2);
5069 if (OMP_CLAUSE_MAP_KIND (c2) != GOMP_MAP_FIRSTPRIVATE_POINTER
5070 && TREE_CODE (TREE_TYPE (ptr)) == REFERENCE_TYPE
5071 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (ptr))))
5073 tree c3 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
5074 OMP_CLAUSE_MAP);
5075 OMP_CLAUSE_SET_MAP_KIND (c3, OMP_CLAUSE_MAP_KIND (c2));
5076 OMP_CLAUSE_DECL (c3) = ptr;
5077 if (OMP_CLAUSE_MAP_KIND (c2) == GOMP_MAP_ALWAYS_POINTER)
5078 OMP_CLAUSE_DECL (c2) = build_simple_mem_ref (ptr);
5079 else
5080 OMP_CLAUSE_DECL (c2) = convert_from_reference (ptr);
5081 OMP_CLAUSE_SIZE (c3) = size_zero_node;
5082 OMP_CLAUSE_CHAIN (c3) = OMP_CLAUSE_CHAIN (c2);
5083 OMP_CLAUSE_CHAIN (c2) = c3;
5087 return false;
5090 /* Return identifier to look up for omp declare reduction. */
5092 tree
5093 omp_reduction_id (enum tree_code reduction_code, tree reduction_id, tree type)
5095 const char *p = NULL;
5096 const char *m = NULL;
5097 switch (reduction_code)
5099 case PLUS_EXPR:
5100 case MULT_EXPR:
5101 case MINUS_EXPR:
5102 case BIT_AND_EXPR:
5103 case BIT_XOR_EXPR:
5104 case BIT_IOR_EXPR:
5105 case TRUTH_ANDIF_EXPR:
5106 case TRUTH_ORIF_EXPR:
5107 reduction_id = ovl_op_identifier (false, reduction_code);
5108 break;
5109 case MIN_EXPR:
5110 p = "min";
5111 break;
5112 case MAX_EXPR:
5113 p = "max";
5114 break;
5115 default:
5116 break;
5119 if (p == NULL)
5121 if (TREE_CODE (reduction_id) != IDENTIFIER_NODE)
5122 return error_mark_node;
5123 p = IDENTIFIER_POINTER (reduction_id);
5126 if (type != NULL_TREE)
5127 m = mangle_type_string (TYPE_MAIN_VARIANT (type));
5129 const char prefix[] = "omp declare reduction ";
5130 size_t lenp = sizeof (prefix);
5131 if (strncmp (p, prefix, lenp - 1) == 0)
5132 lenp = 1;
5133 size_t len = strlen (p);
5134 size_t lenm = m ? strlen (m) + 1 : 0;
5135 char *name = XALLOCAVEC (char, lenp + len + lenm);
5136 if (lenp > 1)
5137 memcpy (name, prefix, lenp - 1);
5138 memcpy (name + lenp - 1, p, len + 1);
5139 if (m)
5141 name[lenp + len - 1] = '~';
5142 memcpy (name + lenp + len, m, lenm);
5144 return get_identifier (name);
5147 /* Lookup OpenMP UDR ID for TYPE, return the corresponding artificial
5148 FUNCTION_DECL or NULL_TREE if not found. */
5150 static tree
5151 omp_reduction_lookup (location_t loc, tree id, tree type, tree *baselinkp,
5152 vec<tree> *ambiguousp)
5154 tree orig_id = id;
5155 tree baselink = NULL_TREE;
5156 if (identifier_p (id))
5158 cp_id_kind idk;
5159 bool nonint_cst_expression_p;
5160 const char *error_msg;
5161 id = omp_reduction_id (ERROR_MARK, id, type);
5162 tree decl = lookup_name (id);
5163 if (decl == NULL_TREE)
5164 decl = error_mark_node;
5165 id = finish_id_expression (id, decl, NULL_TREE, &idk, false, true,
5166 &nonint_cst_expression_p, false, true, false,
5167 false, &error_msg, loc);
5168 if (idk == CP_ID_KIND_UNQUALIFIED
5169 && identifier_p (id))
5171 vec<tree, va_gc> *args = NULL;
5172 vec_safe_push (args, build_reference_type (type));
5173 id = perform_koenig_lookup (id, args, tf_none);
5176 else if (TREE_CODE (id) == SCOPE_REF)
5177 id = lookup_qualified_name (TREE_OPERAND (id, 0),
5178 omp_reduction_id (ERROR_MARK,
5179 TREE_OPERAND (id, 1),
5180 type),
5181 false, false);
5182 tree fns = id;
5183 id = NULL_TREE;
5184 if (fns && is_overloaded_fn (fns))
5186 for (lkp_iterator iter (get_fns (fns)); iter; ++iter)
5188 tree fndecl = *iter;
5189 if (TREE_CODE (fndecl) == FUNCTION_DECL)
5191 tree argtype = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
5192 if (same_type_p (TREE_TYPE (argtype), type))
5194 id = fndecl;
5195 break;
5200 if (id && BASELINK_P (fns))
5202 if (baselinkp)
5203 *baselinkp = fns;
5204 else
5205 baselink = fns;
5209 if (!id && CLASS_TYPE_P (type) && TYPE_BINFO (type))
5211 vec<tree> ambiguous = vNULL;
5212 tree binfo = TYPE_BINFO (type), base_binfo, ret = NULL_TREE;
5213 unsigned int ix;
5214 if (ambiguousp == NULL)
5215 ambiguousp = &ambiguous;
5216 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
5218 id = omp_reduction_lookup (loc, orig_id, BINFO_TYPE (base_binfo),
5219 baselinkp ? baselinkp : &baselink,
5220 ambiguousp);
5221 if (id == NULL_TREE)
5222 continue;
5223 if (!ambiguousp->is_empty ())
5224 ambiguousp->safe_push (id);
5225 else if (ret != NULL_TREE)
5227 ambiguousp->safe_push (ret);
5228 ambiguousp->safe_push (id);
5229 ret = NULL_TREE;
5231 else
5232 ret = id;
5234 if (ambiguousp != &ambiguous)
5235 return ret;
5236 if (!ambiguous.is_empty ())
5238 const char *str = _("candidates are:");
5239 unsigned int idx;
5240 tree udr;
5241 error_at (loc, "user defined reduction lookup is ambiguous");
5242 FOR_EACH_VEC_ELT (ambiguous, idx, udr)
5244 inform (DECL_SOURCE_LOCATION (udr), "%s %#qD", str, udr);
5245 if (idx == 0)
5246 str = get_spaces (str);
5248 ambiguous.release ();
5249 ret = error_mark_node;
5250 baselink = NULL_TREE;
5252 id = ret;
5254 if (id && baselink)
5255 perform_or_defer_access_check (BASELINK_BINFO (baselink),
5256 id, id, tf_warning_or_error);
5257 return id;
5260 /* Helper function for cp_parser_omp_declare_reduction_exprs
5261 and tsubst_omp_udr.
5262 Remove CLEANUP_STMT for data (omp_priv variable).
5263 Also append INIT_EXPR for DECL_INITIAL of omp_priv after its
5264 DECL_EXPR. */
5266 tree
5267 cp_remove_omp_priv_cleanup_stmt (tree *tp, int *walk_subtrees, void *data)
5269 if (TYPE_P (*tp))
5270 *walk_subtrees = 0;
5271 else if (TREE_CODE (*tp) == CLEANUP_STMT && CLEANUP_DECL (*tp) == (tree) data)
5272 *tp = CLEANUP_BODY (*tp);
5273 else if (TREE_CODE (*tp) == DECL_EXPR)
5275 tree decl = DECL_EXPR_DECL (*tp);
5276 if (!processing_template_decl
5277 && decl == (tree) data
5278 && DECL_INITIAL (decl)
5279 && DECL_INITIAL (decl) != error_mark_node)
5281 tree list = NULL_TREE;
5282 append_to_statement_list_force (*tp, &list);
5283 tree init_expr = build2 (INIT_EXPR, void_type_node,
5284 decl, DECL_INITIAL (decl));
5285 DECL_INITIAL (decl) = NULL_TREE;
5286 append_to_statement_list_force (init_expr, &list);
5287 *tp = list;
5290 return NULL_TREE;
5293 /* Data passed from cp_check_omp_declare_reduction to
5294 cp_check_omp_declare_reduction_r. */
5296 struct cp_check_omp_declare_reduction_data
5298 location_t loc;
5299 tree stmts[7];
5300 bool combiner_p;
5303 /* Helper function for cp_check_omp_declare_reduction, called via
5304 cp_walk_tree. */
5306 static tree
5307 cp_check_omp_declare_reduction_r (tree *tp, int *, void *data)
5309 struct cp_check_omp_declare_reduction_data *udr_data
5310 = (struct cp_check_omp_declare_reduction_data *) data;
5311 if (SSA_VAR_P (*tp)
5312 && !DECL_ARTIFICIAL (*tp)
5313 && *tp != DECL_EXPR_DECL (udr_data->stmts[udr_data->combiner_p ? 0 : 3])
5314 && *tp != DECL_EXPR_DECL (udr_data->stmts[udr_data->combiner_p ? 1 : 4]))
5316 location_t loc = udr_data->loc;
5317 if (udr_data->combiner_p)
5318 error_at (loc, "%<#pragma omp declare reduction%> combiner refers to "
5319 "variable %qD which is not %<omp_out%> nor %<omp_in%>",
5320 *tp);
5321 else
5322 error_at (loc, "%<#pragma omp declare reduction%> initializer refers "
5323 "to variable %qD which is not %<omp_priv%> nor "
5324 "%<omp_orig%>",
5325 *tp);
5326 return *tp;
5328 return NULL_TREE;
5331 /* Diagnose violation of OpenMP #pragma omp declare reduction restrictions. */
5333 void
5334 cp_check_omp_declare_reduction (tree udr)
5336 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (udr)));
5337 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
5338 type = TREE_TYPE (type);
5339 int i;
5340 location_t loc = DECL_SOURCE_LOCATION (udr);
5342 if (type == error_mark_node)
5343 return;
5344 if (ARITHMETIC_TYPE_P (type))
5346 static enum tree_code predef_codes[]
5347 = { PLUS_EXPR, MULT_EXPR, MINUS_EXPR, BIT_AND_EXPR, BIT_XOR_EXPR,
5348 BIT_IOR_EXPR, TRUTH_ANDIF_EXPR, TRUTH_ORIF_EXPR };
5349 for (i = 0; i < 8; i++)
5351 tree id = omp_reduction_id (predef_codes[i], NULL_TREE, NULL_TREE);
5352 const char *n1 = IDENTIFIER_POINTER (DECL_NAME (udr));
5353 const char *n2 = IDENTIFIER_POINTER (id);
5354 if (strncmp (n1, n2, IDENTIFIER_LENGTH (id)) == 0
5355 && (n1[IDENTIFIER_LENGTH (id)] == '~'
5356 || n1[IDENTIFIER_LENGTH (id)] == '\0'))
5357 break;
5360 if (i == 8
5361 && TREE_CODE (type) != COMPLEX_EXPR)
5363 const char prefix_minmax[] = "omp declare reduction m";
5364 size_t prefix_size = sizeof (prefix_minmax) - 1;
5365 const char *n = IDENTIFIER_POINTER (DECL_NAME (udr));
5366 if (strncmp (IDENTIFIER_POINTER (DECL_NAME (udr)),
5367 prefix_minmax, prefix_size) == 0
5368 && ((n[prefix_size] == 'i' && n[prefix_size + 1] == 'n')
5369 || (n[prefix_size] == 'a' && n[prefix_size + 1] == 'x'))
5370 && (n[prefix_size + 2] == '~' || n[prefix_size + 2] == '\0'))
5371 i = 0;
5373 if (i < 8)
5375 error_at (loc, "predeclared arithmetic type %qT in "
5376 "%<#pragma omp declare reduction%>", type);
5377 return;
5380 else if (TREE_CODE (type) == FUNCTION_TYPE
5381 || TREE_CODE (type) == METHOD_TYPE
5382 || TREE_CODE (type) == ARRAY_TYPE)
5384 error_at (loc, "function or array type %qT in "
5385 "%<#pragma omp declare reduction%>", type);
5386 return;
5388 else if (TREE_CODE (type) == REFERENCE_TYPE)
5390 error_at (loc, "reference type %qT in %<#pragma omp declare reduction%>",
5391 type);
5392 return;
5394 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
5396 error_at (loc, "const, volatile or __restrict qualified type %qT in "
5397 "%<#pragma omp declare reduction%>", type);
5398 return;
5401 tree body = DECL_SAVED_TREE (udr);
5402 if (body == NULL_TREE || TREE_CODE (body) != STATEMENT_LIST)
5403 return;
5405 tree_stmt_iterator tsi;
5406 struct cp_check_omp_declare_reduction_data data;
5407 memset (data.stmts, 0, sizeof data.stmts);
5408 for (i = 0, tsi = tsi_start (body);
5409 i < 7 && !tsi_end_p (tsi);
5410 i++, tsi_next (&tsi))
5411 data.stmts[i] = tsi_stmt (tsi);
5412 data.loc = loc;
5413 gcc_assert (tsi_end_p (tsi));
5414 if (i >= 3)
5416 gcc_assert (TREE_CODE (data.stmts[0]) == DECL_EXPR
5417 && TREE_CODE (data.stmts[1]) == DECL_EXPR);
5418 if (TREE_NO_WARNING (DECL_EXPR_DECL (data.stmts[0])))
5419 return;
5420 data.combiner_p = true;
5421 if (cp_walk_tree (&data.stmts[2], cp_check_omp_declare_reduction_r,
5422 &data, NULL))
5423 TREE_NO_WARNING (DECL_EXPR_DECL (data.stmts[0])) = 1;
5425 if (i >= 6)
5427 gcc_assert (TREE_CODE (data.stmts[3]) == DECL_EXPR
5428 && TREE_CODE (data.stmts[4]) == DECL_EXPR);
5429 data.combiner_p = false;
5430 if (cp_walk_tree (&data.stmts[5], cp_check_omp_declare_reduction_r,
5431 &data, NULL)
5432 || cp_walk_tree (&DECL_INITIAL (DECL_EXPR_DECL (data.stmts[3])),
5433 cp_check_omp_declare_reduction_r, &data, NULL))
5434 TREE_NO_WARNING (DECL_EXPR_DECL (data.stmts[0])) = 1;
5435 if (i == 7)
5436 gcc_assert (TREE_CODE (data.stmts[6]) == DECL_EXPR);
5440 /* Helper function of finish_omp_clauses. Clone STMT as if we were making
5441 an inline call. But, remap
5442 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
5443 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
5445 static tree
5446 clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
5447 tree decl, tree placeholder)
5449 copy_body_data id;
5450 hash_map<tree, tree> decl_map;
5452 decl_map.put (omp_decl1, placeholder);
5453 decl_map.put (omp_decl2, decl);
5454 memset (&id, 0, sizeof (id));
5455 id.src_fn = DECL_CONTEXT (omp_decl1);
5456 id.dst_fn = current_function_decl;
5457 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
5458 id.decl_map = &decl_map;
5460 id.copy_decl = copy_decl_no_change;
5461 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
5462 id.transform_new_cfg = true;
5463 id.transform_return_to_modify = false;
5464 id.transform_lang_insert_block = NULL;
5465 id.eh_lp_nr = 0;
5466 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
5467 return stmt;
5470 /* Helper function of finish_omp_clauses, called via cp_walk_tree.
5471 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
5473 static tree
5474 find_omp_placeholder_r (tree *tp, int *, void *data)
5476 if (*tp == (tree) data)
5477 return *tp;
5478 return NULL_TREE;
5481 /* Helper function of finish_omp_clauses. Handle OMP_CLAUSE_REDUCTION C.
5482 Return true if there is some error and the clause should be removed. */
5484 static bool
5485 finish_omp_reduction_clause (tree c, bool *need_default_ctor, bool *need_dtor)
5487 tree t = OMP_CLAUSE_DECL (c);
5488 bool predefined = false;
5489 if (TREE_CODE (t) == TREE_LIST)
5491 gcc_assert (processing_template_decl);
5492 return false;
5494 tree type = TREE_TYPE (t);
5495 if (TREE_CODE (t) == MEM_REF)
5496 type = TREE_TYPE (type);
5497 if (TREE_CODE (type) == REFERENCE_TYPE)
5498 type = TREE_TYPE (type);
5499 if (TREE_CODE (type) == ARRAY_TYPE)
5501 tree oatype = type;
5502 gcc_assert (TREE_CODE (t) != MEM_REF);
5503 while (TREE_CODE (type) == ARRAY_TYPE)
5504 type = TREE_TYPE (type);
5505 if (!processing_template_decl)
5507 t = require_complete_type (t);
5508 if (t == error_mark_node)
5509 return true;
5510 tree size = size_binop (EXACT_DIV_EXPR, TYPE_SIZE_UNIT (oatype),
5511 TYPE_SIZE_UNIT (type));
5512 if (integer_zerop (size))
5514 error ("%qE in %<reduction%> clause is a zero size array",
5515 omp_clause_printable_decl (t));
5516 return true;
5518 size = size_binop (MINUS_EXPR, size, size_one_node);
5519 tree index_type = build_index_type (size);
5520 tree atype = build_array_type (type, index_type);
5521 tree ptype = build_pointer_type (type);
5522 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
5523 t = build_fold_addr_expr (t);
5524 t = build2 (MEM_REF, atype, t, build_int_cst (ptype, 0));
5525 OMP_CLAUSE_DECL (c) = t;
5528 if (type == error_mark_node)
5529 return true;
5530 else if (ARITHMETIC_TYPE_P (type))
5531 switch (OMP_CLAUSE_REDUCTION_CODE (c))
5533 case PLUS_EXPR:
5534 case MULT_EXPR:
5535 case MINUS_EXPR:
5536 predefined = true;
5537 break;
5538 case MIN_EXPR:
5539 case MAX_EXPR:
5540 if (TREE_CODE (type) == COMPLEX_TYPE)
5541 break;
5542 predefined = true;
5543 break;
5544 case BIT_AND_EXPR:
5545 case BIT_IOR_EXPR:
5546 case BIT_XOR_EXPR:
5547 if (FLOAT_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
5548 break;
5549 predefined = true;
5550 break;
5551 case TRUTH_ANDIF_EXPR:
5552 case TRUTH_ORIF_EXPR:
5553 if (FLOAT_TYPE_P (type))
5554 break;
5555 predefined = true;
5556 break;
5557 default:
5558 break;
5560 else if (TYPE_READONLY (type))
5562 error ("%qE has const type for %<reduction%>",
5563 omp_clause_printable_decl (t));
5564 return true;
5566 else if (!processing_template_decl)
5568 t = require_complete_type (t);
5569 if (t == error_mark_node)
5570 return true;
5571 OMP_CLAUSE_DECL (c) = t;
5574 if (predefined)
5576 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = NULL_TREE;
5577 return false;
5579 else if (processing_template_decl)
5580 return false;
5582 tree id = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
5584 type = TYPE_MAIN_VARIANT (type);
5585 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = NULL_TREE;
5586 if (id == NULL_TREE)
5587 id = omp_reduction_id (OMP_CLAUSE_REDUCTION_CODE (c),
5588 NULL_TREE, NULL_TREE);
5589 id = omp_reduction_lookup (OMP_CLAUSE_LOCATION (c), id, type, NULL, NULL);
5590 if (id)
5592 if (id == error_mark_node)
5593 return true;
5594 mark_used (id);
5595 tree body = DECL_SAVED_TREE (id);
5596 if (!body)
5597 return true;
5598 if (TREE_CODE (body) == STATEMENT_LIST)
5600 tree_stmt_iterator tsi;
5601 tree placeholder = NULL_TREE, decl_placeholder = NULL_TREE;
5602 int i;
5603 tree stmts[7];
5604 tree atype = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (id)));
5605 atype = TREE_TYPE (atype);
5606 bool need_static_cast = !same_type_p (type, atype);
5607 memset (stmts, 0, sizeof stmts);
5608 for (i = 0, tsi = tsi_start (body);
5609 i < 7 && !tsi_end_p (tsi);
5610 i++, tsi_next (&tsi))
5611 stmts[i] = tsi_stmt (tsi);
5612 gcc_assert (tsi_end_p (tsi));
5614 if (i >= 3)
5616 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
5617 && TREE_CODE (stmts[1]) == DECL_EXPR);
5618 placeholder = build_lang_decl (VAR_DECL, NULL_TREE, type);
5619 DECL_ARTIFICIAL (placeholder) = 1;
5620 DECL_IGNORED_P (placeholder) = 1;
5621 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
5622 if (TREE_CODE (t) == MEM_REF)
5624 decl_placeholder = build_lang_decl (VAR_DECL, NULL_TREE,
5625 type);
5626 DECL_ARTIFICIAL (decl_placeholder) = 1;
5627 DECL_IGNORED_P (decl_placeholder) = 1;
5628 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c) = decl_placeholder;
5630 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[0])))
5631 cxx_mark_addressable (placeholder);
5632 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[1]))
5633 && TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c)))
5634 != REFERENCE_TYPE)
5635 cxx_mark_addressable (decl_placeholder ? decl_placeholder
5636 : OMP_CLAUSE_DECL (c));
5637 tree omp_out = placeholder;
5638 tree omp_in = decl_placeholder ? decl_placeholder
5639 : convert_from_reference (OMP_CLAUSE_DECL (c));
5640 if (need_static_cast)
5642 tree rtype = build_reference_type (atype);
5643 omp_out = build_static_cast (rtype, omp_out,
5644 tf_warning_or_error);
5645 omp_in = build_static_cast (rtype, omp_in,
5646 tf_warning_or_error);
5647 if (omp_out == error_mark_node || omp_in == error_mark_node)
5648 return true;
5649 omp_out = convert_from_reference (omp_out);
5650 omp_in = convert_from_reference (omp_in);
5652 OMP_CLAUSE_REDUCTION_MERGE (c)
5653 = clone_omp_udr (stmts[2], DECL_EXPR_DECL (stmts[0]),
5654 DECL_EXPR_DECL (stmts[1]), omp_in, omp_out);
5656 if (i >= 6)
5658 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
5659 && TREE_CODE (stmts[4]) == DECL_EXPR);
5660 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[3])))
5661 cxx_mark_addressable (decl_placeholder ? decl_placeholder
5662 : OMP_CLAUSE_DECL (c));
5663 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[4])))
5664 cxx_mark_addressable (placeholder);
5665 tree omp_priv = decl_placeholder ? decl_placeholder
5666 : convert_from_reference (OMP_CLAUSE_DECL (c));
5667 tree omp_orig = placeholder;
5668 if (need_static_cast)
5670 if (i == 7)
5672 error_at (OMP_CLAUSE_LOCATION (c),
5673 "user defined reduction with constructor "
5674 "initializer for base class %qT", atype);
5675 return true;
5677 tree rtype = build_reference_type (atype);
5678 omp_priv = build_static_cast (rtype, omp_priv,
5679 tf_warning_or_error);
5680 omp_orig = build_static_cast (rtype, omp_orig,
5681 tf_warning_or_error);
5682 if (omp_priv == error_mark_node
5683 || omp_orig == error_mark_node)
5684 return true;
5685 omp_priv = convert_from_reference (omp_priv);
5686 omp_orig = convert_from_reference (omp_orig);
5688 if (i == 6)
5689 *need_default_ctor = true;
5690 OMP_CLAUSE_REDUCTION_INIT (c)
5691 = clone_omp_udr (stmts[5], DECL_EXPR_DECL (stmts[4]),
5692 DECL_EXPR_DECL (stmts[3]),
5693 omp_priv, omp_orig);
5694 if (cp_walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
5695 find_omp_placeholder_r, placeholder, NULL))
5696 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
5698 else if (i >= 3)
5700 if (CLASS_TYPE_P (type) && !pod_type_p (type))
5701 *need_default_ctor = true;
5702 else
5704 tree init;
5705 tree v = decl_placeholder ? decl_placeholder
5706 : convert_from_reference (t);
5707 if (AGGREGATE_TYPE_P (TREE_TYPE (v)))
5708 init = build_constructor (TREE_TYPE (v), NULL);
5709 else
5710 init = fold_convert (TREE_TYPE (v), integer_zero_node);
5711 OMP_CLAUSE_REDUCTION_INIT (c)
5712 = build2 (INIT_EXPR, TREE_TYPE (v), v, init);
5717 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
5718 *need_dtor = true;
5719 else
5721 error ("user defined reduction not found for %qE",
5722 omp_clause_printable_decl (t));
5723 return true;
5725 if (TREE_CODE (OMP_CLAUSE_DECL (c)) == MEM_REF)
5726 gcc_assert (TYPE_SIZE_UNIT (type)
5727 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST);
5728 return false;
5731 /* Called from finish_struct_1. linear(this) or linear(this:step)
5732 clauses might not be finalized yet because the class has been incomplete
5733 when parsing #pragma omp declare simd methods. Fix those up now. */
5735 void
5736 finish_omp_declare_simd_methods (tree t)
5738 if (processing_template_decl)
5739 return;
5741 for (tree x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
5743 if (TREE_CODE (TREE_TYPE (x)) != METHOD_TYPE)
5744 continue;
5745 tree ods = lookup_attribute ("omp declare simd", DECL_ATTRIBUTES (x));
5746 if (!ods || !TREE_VALUE (ods))
5747 continue;
5748 for (tree c = TREE_VALUE (TREE_VALUE (ods)); c; c = OMP_CLAUSE_CHAIN (c))
5749 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
5750 && integer_zerop (OMP_CLAUSE_DECL (c))
5751 && OMP_CLAUSE_LINEAR_STEP (c)
5752 && TREE_CODE (TREE_TYPE (OMP_CLAUSE_LINEAR_STEP (c)))
5753 == POINTER_TYPE)
5755 tree s = OMP_CLAUSE_LINEAR_STEP (c);
5756 s = fold_convert_loc (OMP_CLAUSE_LOCATION (c), sizetype, s);
5757 s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MULT_EXPR,
5758 sizetype, s, TYPE_SIZE_UNIT (t));
5759 OMP_CLAUSE_LINEAR_STEP (c) = s;
5764 /* Adjust sink depend clause to take into account pointer offsets.
5766 Return TRUE if there was a problem processing the offset, and the
5767 whole clause should be removed. */
5769 static bool
5770 cp_finish_omp_clause_depend_sink (tree sink_clause)
5772 tree t = OMP_CLAUSE_DECL (sink_clause);
5773 gcc_assert (TREE_CODE (t) == TREE_LIST);
5775 /* Make sure we don't adjust things twice for templates. */
5776 if (processing_template_decl)
5777 return false;
5779 for (; t; t = TREE_CHAIN (t))
5781 tree decl = TREE_VALUE (t);
5782 if (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
5784 tree offset = TREE_PURPOSE (t);
5785 bool neg = wi::neg_p (wi::to_wide (offset));
5786 offset = fold_unary (ABS_EXPR, TREE_TYPE (offset), offset);
5787 decl = mark_rvalue_use (decl);
5788 decl = convert_from_reference (decl);
5789 tree t2 = pointer_int_sum (OMP_CLAUSE_LOCATION (sink_clause),
5790 neg ? MINUS_EXPR : PLUS_EXPR,
5791 decl, offset);
5792 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (sink_clause),
5793 MINUS_EXPR, sizetype,
5794 fold_convert (sizetype, t2),
5795 fold_convert (sizetype, decl));
5796 if (t2 == error_mark_node)
5797 return true;
5798 TREE_PURPOSE (t) = t2;
5801 return false;
5804 /* For all elements of CLAUSES, validate them vs OpenMP constraints.
5805 Remove any elements from the list that are invalid. */
5807 tree
5808 finish_omp_clauses (tree clauses, enum c_omp_region_type ort)
5810 bitmap_head generic_head, firstprivate_head, lastprivate_head;
5811 bitmap_head aligned_head, map_head, map_field_head, oacc_reduction_head;
5812 tree c, t, *pc;
5813 tree safelen = NULL_TREE;
5814 bool branch_seen = false;
5815 bool copyprivate_seen = false;
5816 bool ordered_seen = false;
5817 bool oacc_async = false;
5819 bitmap_obstack_initialize (NULL);
5820 bitmap_initialize (&generic_head, &bitmap_default_obstack);
5821 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
5822 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
5823 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
5824 bitmap_initialize (&map_head, &bitmap_default_obstack);
5825 bitmap_initialize (&map_field_head, &bitmap_default_obstack);
5826 bitmap_initialize (&oacc_reduction_head, &bitmap_default_obstack);
5828 if (ort & C_ORT_ACC)
5829 for (c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
5830 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ASYNC)
5832 oacc_async = true;
5833 break;
5836 for (pc = &clauses, c = clauses; c ; c = *pc)
5838 bool remove = false;
5839 bool field_ok = false;
5841 switch (OMP_CLAUSE_CODE (c))
5843 case OMP_CLAUSE_SHARED:
5844 field_ok = ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP);
5845 goto check_dup_generic;
5846 case OMP_CLAUSE_PRIVATE:
5847 field_ok = ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP);
5848 goto check_dup_generic;
5849 case OMP_CLAUSE_REDUCTION:
5850 field_ok = ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP);
5851 t = OMP_CLAUSE_DECL (c);
5852 if (TREE_CODE (t) == TREE_LIST)
5854 if (handle_omp_array_sections (c, ort))
5856 remove = true;
5857 break;
5859 if (TREE_CODE (t) == TREE_LIST)
5861 while (TREE_CODE (t) == TREE_LIST)
5862 t = TREE_CHAIN (t);
5864 else
5866 gcc_assert (TREE_CODE (t) == MEM_REF);
5867 t = TREE_OPERAND (t, 0);
5868 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
5869 t = TREE_OPERAND (t, 0);
5870 if (TREE_CODE (t) == ADDR_EXPR
5871 || INDIRECT_REF_P (t))
5872 t = TREE_OPERAND (t, 0);
5874 tree n = omp_clause_decl_field (t);
5875 if (n)
5876 t = n;
5877 goto check_dup_generic_t;
5879 if (oacc_async)
5880 cxx_mark_addressable (t);
5881 goto check_dup_generic;
5882 case OMP_CLAUSE_COPYPRIVATE:
5883 copyprivate_seen = true;
5884 field_ok = ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP);
5885 goto check_dup_generic;
5886 case OMP_CLAUSE_COPYIN:
5887 goto check_dup_generic;
5888 case OMP_CLAUSE_LINEAR:
5889 field_ok = ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP);
5890 t = OMP_CLAUSE_DECL (c);
5891 if (ort != C_ORT_OMP_DECLARE_SIMD
5892 && OMP_CLAUSE_LINEAR_KIND (c) != OMP_CLAUSE_LINEAR_DEFAULT)
5894 error_at (OMP_CLAUSE_LOCATION (c),
5895 "modifier should not be specified in %<linear%> "
5896 "clause on %<simd%> or %<for%> constructs");
5897 OMP_CLAUSE_LINEAR_KIND (c) = OMP_CLAUSE_LINEAR_DEFAULT;
5899 if ((VAR_P (t) || TREE_CODE (t) == PARM_DECL)
5900 && !type_dependent_expression_p (t))
5902 tree type = TREE_TYPE (t);
5903 if ((OMP_CLAUSE_LINEAR_KIND (c) == OMP_CLAUSE_LINEAR_REF
5904 || OMP_CLAUSE_LINEAR_KIND (c) == OMP_CLAUSE_LINEAR_UVAL)
5905 && TREE_CODE (type) != REFERENCE_TYPE)
5907 error ("linear clause with %qs modifier applied to "
5908 "non-reference variable with %qT type",
5909 OMP_CLAUSE_LINEAR_KIND (c) == OMP_CLAUSE_LINEAR_REF
5910 ? "ref" : "uval", TREE_TYPE (t));
5911 remove = true;
5912 break;
5914 if (TREE_CODE (type) == REFERENCE_TYPE)
5915 type = TREE_TYPE (type);
5916 if (OMP_CLAUSE_LINEAR_KIND (c) != OMP_CLAUSE_LINEAR_REF)
5918 if (!INTEGRAL_TYPE_P (type)
5919 && TREE_CODE (type) != POINTER_TYPE)
5921 error ("linear clause applied to non-integral non-pointer"
5922 " variable with %qT type", TREE_TYPE (t));
5923 remove = true;
5924 break;
5928 t = OMP_CLAUSE_LINEAR_STEP (c);
5929 if (t == NULL_TREE)
5930 t = integer_one_node;
5931 if (t == error_mark_node)
5933 remove = true;
5934 break;
5936 else if (!type_dependent_expression_p (t)
5937 && !INTEGRAL_TYPE_P (TREE_TYPE (t))
5938 && (ort != C_ORT_OMP_DECLARE_SIMD
5939 || TREE_CODE (t) != PARM_DECL
5940 || TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE
5941 || !INTEGRAL_TYPE_P (TREE_TYPE (TREE_TYPE (t)))))
5943 error ("linear step expression must be integral");
5944 remove = true;
5945 break;
5947 else
5949 t = mark_rvalue_use (t);
5950 if (ort == C_ORT_OMP_DECLARE_SIMD && TREE_CODE (t) == PARM_DECL)
5952 OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c) = 1;
5953 goto check_dup_generic;
5955 if (!processing_template_decl
5956 && (VAR_P (OMP_CLAUSE_DECL (c))
5957 || TREE_CODE (OMP_CLAUSE_DECL (c)) == PARM_DECL))
5959 if (ort == C_ORT_OMP_DECLARE_SIMD)
5961 t = maybe_constant_value (t);
5962 if (TREE_CODE (t) != INTEGER_CST)
5964 error_at (OMP_CLAUSE_LOCATION (c),
5965 "%<linear%> clause step %qE is neither "
5966 "constant nor a parameter", t);
5967 remove = true;
5968 break;
5971 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5972 tree type = TREE_TYPE (OMP_CLAUSE_DECL (c));
5973 if (TREE_CODE (type) == REFERENCE_TYPE)
5974 type = TREE_TYPE (type);
5975 if (OMP_CLAUSE_LINEAR_KIND (c) == OMP_CLAUSE_LINEAR_REF)
5977 type = build_pointer_type (type);
5978 tree d = fold_convert (type, OMP_CLAUSE_DECL (c));
5979 t = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
5980 d, t);
5981 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c),
5982 MINUS_EXPR, sizetype,
5983 fold_convert (sizetype, t),
5984 fold_convert (sizetype, d));
5985 if (t == error_mark_node)
5987 remove = true;
5988 break;
5991 else if (TREE_CODE (type) == POINTER_TYPE
5992 /* Can't multiply the step yet if *this
5993 is still incomplete type. */
5994 && (ort != C_ORT_OMP_DECLARE_SIMD
5995 || TREE_CODE (OMP_CLAUSE_DECL (c)) != PARM_DECL
5996 || !DECL_ARTIFICIAL (OMP_CLAUSE_DECL (c))
5997 || DECL_NAME (OMP_CLAUSE_DECL (c))
5998 != this_identifier
5999 || !TYPE_BEING_DEFINED (TREE_TYPE (type))))
6001 tree d = convert_from_reference (OMP_CLAUSE_DECL (c));
6002 t = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
6003 d, t);
6004 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c),
6005 MINUS_EXPR, sizetype,
6006 fold_convert (sizetype, t),
6007 fold_convert (sizetype, d));
6008 if (t == error_mark_node)
6010 remove = true;
6011 break;
6014 else
6015 t = fold_convert (type, t);
6017 OMP_CLAUSE_LINEAR_STEP (c) = t;
6019 goto check_dup_generic;
6020 check_dup_generic:
6021 t = omp_clause_decl_field (OMP_CLAUSE_DECL (c));
6022 if (t)
6024 if (!remove && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_SHARED)
6025 omp_note_field_privatization (t, OMP_CLAUSE_DECL (c));
6027 else
6028 t = OMP_CLAUSE_DECL (c);
6029 check_dup_generic_t:
6030 if (t == current_class_ptr
6031 && (ort != C_ORT_OMP_DECLARE_SIMD
6032 || (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_LINEAR
6033 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_UNIFORM)))
6035 error ("%<this%> allowed in OpenMP only in %<declare simd%>"
6036 " clauses");
6037 remove = true;
6038 break;
6040 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL
6041 && (!field_ok || TREE_CODE (t) != FIELD_DECL))
6043 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
6044 break;
6045 if (DECL_P (t))
6046 error ("%qD is not a variable in clause %qs", t,
6047 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6048 else
6049 error ("%qE is not a variable in clause %qs", t,
6050 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6051 remove = true;
6053 else if (ort == C_ORT_ACC
6054 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
6056 if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
6058 error ("%qD appears more than once in reduction clauses", t);
6059 remove = true;
6061 else
6062 bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
6064 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
6065 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
6066 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
6068 error ("%qD appears more than once in data clauses", t);
6069 remove = true;
6071 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
6072 && bitmap_bit_p (&map_head, DECL_UID (t)))
6074 if (ort == C_ORT_ACC)
6075 error ("%qD appears more than once in data clauses", t);
6076 else
6077 error ("%qD appears both in data and map clauses", t);
6078 remove = true;
6080 else
6081 bitmap_set_bit (&generic_head, DECL_UID (t));
6082 if (!field_ok)
6083 break;
6084 handle_field_decl:
6085 if (!remove
6086 && TREE_CODE (t) == FIELD_DECL
6087 && t == OMP_CLAUSE_DECL (c)
6088 && ort != C_ORT_ACC)
6090 OMP_CLAUSE_DECL (c)
6091 = omp_privatize_field (t, (OMP_CLAUSE_CODE (c)
6092 == OMP_CLAUSE_SHARED));
6093 if (OMP_CLAUSE_DECL (c) == error_mark_node)
6094 remove = true;
6096 break;
6098 case OMP_CLAUSE_FIRSTPRIVATE:
6099 t = omp_clause_decl_field (OMP_CLAUSE_DECL (c));
6100 if (t)
6101 omp_note_field_privatization (t, OMP_CLAUSE_DECL (c));
6102 else
6103 t = OMP_CLAUSE_DECL (c);
6104 if (ort != C_ORT_ACC && t == current_class_ptr)
6106 error ("%<this%> allowed in OpenMP only in %<declare simd%>"
6107 " clauses");
6108 remove = true;
6109 break;
6111 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL
6112 && ((ort & C_ORT_OMP_DECLARE_SIMD) != C_ORT_OMP
6113 || TREE_CODE (t) != FIELD_DECL))
6115 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
6116 break;
6117 if (DECL_P (t))
6118 error ("%qD is not a variable in clause %<firstprivate%>", t);
6119 else
6120 error ("%qE is not a variable in clause %<firstprivate%>", t);
6121 remove = true;
6123 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
6124 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
6126 error ("%qD appears more than once in data clauses", t);
6127 remove = true;
6129 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
6131 if (ort == C_ORT_ACC)
6132 error ("%qD appears more than once in data clauses", t);
6133 else
6134 error ("%qD appears both in data and map clauses", t);
6135 remove = true;
6137 else
6138 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
6139 goto handle_field_decl;
6141 case OMP_CLAUSE_LASTPRIVATE:
6142 t = omp_clause_decl_field (OMP_CLAUSE_DECL (c));
6143 if (t)
6144 omp_note_field_privatization (t, OMP_CLAUSE_DECL (c));
6145 else
6146 t = OMP_CLAUSE_DECL (c);
6147 if (t == current_class_ptr)
6149 error ("%<this%> allowed in OpenMP only in %<declare simd%>"
6150 " clauses");
6151 remove = true;
6152 break;
6154 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL
6155 && ((ort & C_ORT_OMP_DECLARE_SIMD) != C_ORT_OMP
6156 || TREE_CODE (t) != FIELD_DECL))
6158 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
6159 break;
6160 if (DECL_P (t))
6161 error ("%qD is not a variable in clause %<lastprivate%>", t);
6162 else
6163 error ("%qE is not a variable in clause %<lastprivate%>", t);
6164 remove = true;
6166 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
6167 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
6169 error ("%qD appears more than once in data clauses", t);
6170 remove = true;
6172 else
6173 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
6174 goto handle_field_decl;
6176 case OMP_CLAUSE_IF:
6177 t = OMP_CLAUSE_IF_EXPR (c);
6178 t = maybe_convert_cond (t);
6179 if (t == error_mark_node)
6180 remove = true;
6181 else if (!processing_template_decl)
6182 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6183 OMP_CLAUSE_IF_EXPR (c) = t;
6184 break;
6186 case OMP_CLAUSE_FINAL:
6187 t = OMP_CLAUSE_FINAL_EXPR (c);
6188 t = maybe_convert_cond (t);
6189 if (t == error_mark_node)
6190 remove = true;
6191 else if (!processing_template_decl)
6192 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6193 OMP_CLAUSE_FINAL_EXPR (c) = t;
6194 break;
6196 case OMP_CLAUSE_GANG:
6197 /* Operand 1 is the gang static: argument. */
6198 t = OMP_CLAUSE_OPERAND (c, 1);
6199 if (t != NULL_TREE)
6201 if (t == error_mark_node)
6202 remove = true;
6203 else if (!type_dependent_expression_p (t)
6204 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6206 error ("%<gang%> static expression must be integral");
6207 remove = true;
6209 else
6211 t = mark_rvalue_use (t);
6212 if (!processing_template_decl)
6214 t = maybe_constant_value (t);
6215 if (TREE_CODE (t) == INTEGER_CST
6216 && tree_int_cst_sgn (t) != 1
6217 && t != integer_minus_one_node)
6219 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6220 "%<gang%> static value must be "
6221 "positive");
6222 t = integer_one_node;
6224 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6227 OMP_CLAUSE_OPERAND (c, 1) = t;
6229 /* Check operand 0, the num argument. */
6230 /* FALLTHRU */
6232 case OMP_CLAUSE_WORKER:
6233 case OMP_CLAUSE_VECTOR:
6234 if (OMP_CLAUSE_OPERAND (c, 0) == NULL_TREE)
6235 break;
6236 /* FALLTHRU */
6238 case OMP_CLAUSE_NUM_TASKS:
6239 case OMP_CLAUSE_NUM_TEAMS:
6240 case OMP_CLAUSE_NUM_THREADS:
6241 case OMP_CLAUSE_NUM_GANGS:
6242 case OMP_CLAUSE_NUM_WORKERS:
6243 case OMP_CLAUSE_VECTOR_LENGTH:
6244 t = OMP_CLAUSE_OPERAND (c, 0);
6245 if (t == error_mark_node)
6246 remove = true;
6247 else if (!type_dependent_expression_p (t)
6248 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6250 switch (OMP_CLAUSE_CODE (c))
6252 case OMP_CLAUSE_GANG:
6253 error_at (OMP_CLAUSE_LOCATION (c),
6254 "%<gang%> num expression must be integral"); break;
6255 case OMP_CLAUSE_VECTOR:
6256 error_at (OMP_CLAUSE_LOCATION (c),
6257 "%<vector%> length expression must be integral");
6258 break;
6259 case OMP_CLAUSE_WORKER:
6260 error_at (OMP_CLAUSE_LOCATION (c),
6261 "%<worker%> num expression must be integral");
6262 break;
6263 default:
6264 error_at (OMP_CLAUSE_LOCATION (c),
6265 "%qs expression must be integral",
6266 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6268 remove = true;
6270 else
6272 t = mark_rvalue_use (t);
6273 if (!processing_template_decl)
6275 t = maybe_constant_value (t);
6276 if (TREE_CODE (t) == INTEGER_CST
6277 && tree_int_cst_sgn (t) != 1)
6279 switch (OMP_CLAUSE_CODE (c))
6281 case OMP_CLAUSE_GANG:
6282 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6283 "%<gang%> num value must be positive");
6284 break;
6285 case OMP_CLAUSE_VECTOR:
6286 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6287 "%<vector%> length value must be "
6288 "positive");
6289 break;
6290 case OMP_CLAUSE_WORKER:
6291 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6292 "%<worker%> num value must be "
6293 "positive");
6294 break;
6295 default:
6296 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6297 "%qs value must be positive",
6298 omp_clause_code_name
6299 [OMP_CLAUSE_CODE (c)]);
6301 t = integer_one_node;
6303 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6305 OMP_CLAUSE_OPERAND (c, 0) = t;
6307 break;
6309 case OMP_CLAUSE_SCHEDULE:
6310 if (OMP_CLAUSE_SCHEDULE_KIND (c) & OMP_CLAUSE_SCHEDULE_NONMONOTONIC)
6312 const char *p = NULL;
6313 switch (OMP_CLAUSE_SCHEDULE_KIND (c) & OMP_CLAUSE_SCHEDULE_MASK)
6315 case OMP_CLAUSE_SCHEDULE_STATIC: p = "static"; break;
6316 case OMP_CLAUSE_SCHEDULE_DYNAMIC: break;
6317 case OMP_CLAUSE_SCHEDULE_GUIDED: break;
6318 case OMP_CLAUSE_SCHEDULE_AUTO: p = "auto"; break;
6319 case OMP_CLAUSE_SCHEDULE_RUNTIME: p = "runtime"; break;
6320 default: gcc_unreachable ();
6322 if (p)
6324 error_at (OMP_CLAUSE_LOCATION (c),
6325 "%<nonmonotonic%> modifier specified for %qs "
6326 "schedule kind", p);
6327 OMP_CLAUSE_SCHEDULE_KIND (c)
6328 = (enum omp_clause_schedule_kind)
6329 (OMP_CLAUSE_SCHEDULE_KIND (c)
6330 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
6334 t = OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c);
6335 if (t == NULL)
6337 else if (t == error_mark_node)
6338 remove = true;
6339 else if (!type_dependent_expression_p (t)
6340 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6342 error ("schedule chunk size expression must be integral");
6343 remove = true;
6345 else
6347 t = mark_rvalue_use (t);
6348 if (!processing_template_decl)
6350 t = maybe_constant_value (t);
6351 if (TREE_CODE (t) == INTEGER_CST
6352 && tree_int_cst_sgn (t) != 1)
6354 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6355 "chunk size value must be positive");
6356 t = integer_one_node;
6358 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6360 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
6362 break;
6364 case OMP_CLAUSE_SIMDLEN:
6365 case OMP_CLAUSE_SAFELEN:
6366 t = OMP_CLAUSE_OPERAND (c, 0);
6367 if (t == error_mark_node)
6368 remove = true;
6369 else if (!type_dependent_expression_p (t)
6370 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6372 error ("%qs length expression must be integral",
6373 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6374 remove = true;
6376 else
6378 t = mark_rvalue_use (t);
6379 if (!processing_template_decl)
6381 t = maybe_constant_value (t);
6382 if (TREE_CODE (t) != INTEGER_CST
6383 || tree_int_cst_sgn (t) != 1)
6385 error ("%qs length expression must be positive constant"
6386 " integer expression",
6387 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6388 remove = true;
6391 OMP_CLAUSE_OPERAND (c, 0) = t;
6392 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SAFELEN)
6393 safelen = c;
6395 break;
6397 case OMP_CLAUSE_ASYNC:
6398 t = OMP_CLAUSE_ASYNC_EXPR (c);
6399 if (t == error_mark_node)
6400 remove = true;
6401 else if (!type_dependent_expression_p (t)
6402 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6404 error ("%<async%> expression must be integral");
6405 remove = true;
6407 else
6409 t = mark_rvalue_use (t);
6410 if (!processing_template_decl)
6411 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6412 OMP_CLAUSE_ASYNC_EXPR (c) = t;
6414 break;
6416 case OMP_CLAUSE_WAIT:
6417 t = OMP_CLAUSE_WAIT_EXPR (c);
6418 if (t == error_mark_node)
6419 remove = true;
6420 else if (!processing_template_decl)
6421 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6422 OMP_CLAUSE_WAIT_EXPR (c) = t;
6423 break;
6425 case OMP_CLAUSE_THREAD_LIMIT:
6426 t = OMP_CLAUSE_THREAD_LIMIT_EXPR (c);
6427 if (t == error_mark_node)
6428 remove = true;
6429 else if (!type_dependent_expression_p (t)
6430 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6432 error ("%<thread_limit%> expression must be integral");
6433 remove = true;
6435 else
6437 t = mark_rvalue_use (t);
6438 if (!processing_template_decl)
6440 t = maybe_constant_value (t);
6441 if (TREE_CODE (t) == INTEGER_CST
6442 && tree_int_cst_sgn (t) != 1)
6444 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6445 "%<thread_limit%> value must be positive");
6446 t = integer_one_node;
6448 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6450 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
6452 break;
6454 case OMP_CLAUSE_DEVICE:
6455 t = OMP_CLAUSE_DEVICE_ID (c);
6456 if (t == error_mark_node)
6457 remove = true;
6458 else if (!type_dependent_expression_p (t)
6459 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6461 error ("%<device%> id must be integral");
6462 remove = true;
6464 else
6466 t = mark_rvalue_use (t);
6467 if (!processing_template_decl)
6468 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6469 OMP_CLAUSE_DEVICE_ID (c) = t;
6471 break;
6473 case OMP_CLAUSE_DIST_SCHEDULE:
6474 t = OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c);
6475 if (t == NULL)
6477 else if (t == error_mark_node)
6478 remove = true;
6479 else if (!type_dependent_expression_p (t)
6480 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6482 error ("%<dist_schedule%> chunk size expression must be "
6483 "integral");
6484 remove = true;
6486 else
6488 t = mark_rvalue_use (t);
6489 if (!processing_template_decl)
6490 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6491 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
6493 break;
6495 case OMP_CLAUSE_ALIGNED:
6496 t = OMP_CLAUSE_DECL (c);
6497 if (t == current_class_ptr && ort != C_ORT_OMP_DECLARE_SIMD)
6499 error ("%<this%> allowed in OpenMP only in %<declare simd%>"
6500 " clauses");
6501 remove = true;
6502 break;
6504 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
6506 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
6507 break;
6508 if (DECL_P (t))
6509 error ("%qD is not a variable in %<aligned%> clause", t);
6510 else
6511 error ("%qE is not a variable in %<aligned%> clause", t);
6512 remove = true;
6514 else if (!type_dependent_expression_p (t)
6515 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE
6516 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE
6517 && (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE
6518 || (!POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (t)))
6519 && (TREE_CODE (TREE_TYPE (TREE_TYPE (t)))
6520 != ARRAY_TYPE))))
6522 error_at (OMP_CLAUSE_LOCATION (c),
6523 "%qE in %<aligned%> clause is neither a pointer nor "
6524 "an array nor a reference to pointer or array", t);
6525 remove = true;
6527 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
6529 error ("%qD appears more than once in %<aligned%> clauses", t);
6530 remove = true;
6532 else
6533 bitmap_set_bit (&aligned_head, DECL_UID (t));
6534 t = OMP_CLAUSE_ALIGNED_ALIGNMENT (c);
6535 if (t == error_mark_node)
6536 remove = true;
6537 else if (t == NULL_TREE)
6538 break;
6539 else if (!type_dependent_expression_p (t)
6540 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6542 error ("%<aligned%> clause alignment expression must "
6543 "be integral");
6544 remove = true;
6546 else
6548 t = mark_rvalue_use (t);
6549 if (!processing_template_decl)
6551 t = maybe_constant_value (t);
6552 if (TREE_CODE (t) != INTEGER_CST
6553 || tree_int_cst_sgn (t) != 1)
6555 error ("%<aligned%> clause alignment expression must be "
6556 "positive constant integer expression");
6557 remove = true;
6560 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = t;
6562 break;
6564 case OMP_CLAUSE_DEPEND:
6565 t = OMP_CLAUSE_DECL (c);
6566 if (t == NULL_TREE)
6568 gcc_assert (OMP_CLAUSE_DEPEND_KIND (c)
6569 == OMP_CLAUSE_DEPEND_SOURCE);
6570 break;
6572 if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SINK)
6574 if (cp_finish_omp_clause_depend_sink (c))
6575 remove = true;
6576 break;
6578 if (TREE_CODE (t) == TREE_LIST)
6580 if (handle_omp_array_sections (c, ort))
6581 remove = true;
6582 break;
6584 if (t == error_mark_node)
6585 remove = true;
6586 else if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
6588 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
6589 break;
6590 if (DECL_P (t))
6591 error ("%qD is not a variable in %<depend%> clause", t);
6592 else
6593 error ("%qE is not a variable in %<depend%> clause", t);
6594 remove = true;
6596 else if (t == current_class_ptr)
6598 error ("%<this%> allowed in OpenMP only in %<declare simd%>"
6599 " clauses");
6600 remove = true;
6602 else if (!processing_template_decl
6603 && !cxx_mark_addressable (t))
6604 remove = true;
6605 break;
6607 case OMP_CLAUSE_MAP:
6608 case OMP_CLAUSE_TO:
6609 case OMP_CLAUSE_FROM:
6610 case OMP_CLAUSE__CACHE_:
6611 t = OMP_CLAUSE_DECL (c);
6612 if (TREE_CODE (t) == TREE_LIST)
6614 if (handle_omp_array_sections (c, ort))
6615 remove = true;
6616 else
6618 t = OMP_CLAUSE_DECL (c);
6619 if (TREE_CODE (t) != TREE_LIST
6620 && !type_dependent_expression_p (t)
6621 && !cp_omp_mappable_type (TREE_TYPE (t)))
6623 error_at (OMP_CLAUSE_LOCATION (c),
6624 "array section does not have mappable type "
6625 "in %qs clause",
6626 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6627 remove = true;
6629 while (TREE_CODE (t) == ARRAY_REF)
6630 t = TREE_OPERAND (t, 0);
6631 if (TREE_CODE (t) == COMPONENT_REF
6632 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
6634 while (TREE_CODE (t) == COMPONENT_REF)
6635 t = TREE_OPERAND (t, 0);
6636 if (REFERENCE_REF_P (t))
6637 t = TREE_OPERAND (t, 0);
6638 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
6639 break;
6640 if (bitmap_bit_p (&map_head, DECL_UID (t)))
6642 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
6643 error ("%qD appears more than once in motion"
6644 " clauses", t);
6645 else if (ort == C_ORT_ACC)
6646 error ("%qD appears more than once in data"
6647 " clauses", t);
6648 else
6649 error ("%qD appears more than once in map"
6650 " clauses", t);
6651 remove = true;
6653 else
6655 bitmap_set_bit (&map_head, DECL_UID (t));
6656 bitmap_set_bit (&map_field_head, DECL_UID (t));
6660 break;
6662 if (t == error_mark_node)
6664 remove = true;
6665 break;
6667 if (REFERENCE_REF_P (t)
6668 && TREE_CODE (TREE_OPERAND (t, 0)) == COMPONENT_REF)
6670 t = TREE_OPERAND (t, 0);
6671 OMP_CLAUSE_DECL (c) = t;
6673 if (TREE_CODE (t) == COMPONENT_REF
6674 && (ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP
6675 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE__CACHE_)
6677 if (type_dependent_expression_p (t))
6678 break;
6679 if (TREE_CODE (TREE_OPERAND (t, 1)) == FIELD_DECL
6680 && DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
6682 error_at (OMP_CLAUSE_LOCATION (c),
6683 "bit-field %qE in %qs clause",
6684 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6685 remove = true;
6687 else if (!cp_omp_mappable_type (TREE_TYPE (t)))
6689 error_at (OMP_CLAUSE_LOCATION (c),
6690 "%qE does not have a mappable type in %qs clause",
6691 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6692 remove = true;
6694 while (TREE_CODE (t) == COMPONENT_REF)
6696 if (TREE_TYPE (TREE_OPERAND (t, 0))
6697 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0)))
6698 == UNION_TYPE))
6700 error_at (OMP_CLAUSE_LOCATION (c),
6701 "%qE is a member of a union", t);
6702 remove = true;
6703 break;
6705 t = TREE_OPERAND (t, 0);
6707 if (remove)
6708 break;
6709 if (REFERENCE_REF_P (t))
6710 t = TREE_OPERAND (t, 0);
6711 if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
6713 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
6714 goto handle_map_references;
6717 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
6719 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
6720 break;
6721 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
6722 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
6723 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ALWAYS_POINTER))
6724 break;
6725 if (DECL_P (t))
6726 error ("%qD is not a variable in %qs clause", t,
6727 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6728 else
6729 error ("%qE is not a variable in %qs clause", t,
6730 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6731 remove = true;
6733 else if (VAR_P (t) && CP_DECL_THREAD_LOCAL_P (t))
6735 error ("%qD is threadprivate variable in %qs clause", t,
6736 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6737 remove = true;
6739 else if (ort != C_ORT_ACC && t == current_class_ptr)
6741 error ("%<this%> allowed in OpenMP only in %<declare simd%>"
6742 " clauses");
6743 remove = true;
6744 break;
6746 else if (!processing_template_decl
6747 && TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE
6748 && (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
6749 || (OMP_CLAUSE_MAP_KIND (c)
6750 != GOMP_MAP_FIRSTPRIVATE_POINTER))
6751 && !cxx_mark_addressable (t))
6752 remove = true;
6753 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
6754 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
6755 || (OMP_CLAUSE_MAP_KIND (c)
6756 == GOMP_MAP_FIRSTPRIVATE_POINTER)))
6757 && t == OMP_CLAUSE_DECL (c)
6758 && !type_dependent_expression_p (t)
6759 && !cp_omp_mappable_type ((TREE_CODE (TREE_TYPE (t))
6760 == REFERENCE_TYPE)
6761 ? TREE_TYPE (TREE_TYPE (t))
6762 : TREE_TYPE (t)))
6764 error_at (OMP_CLAUSE_LOCATION (c),
6765 "%qD does not have a mappable type in %qs clause", t,
6766 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6767 remove = true;
6769 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
6770 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FORCE_DEVICEPTR
6771 && !type_dependent_expression_p (t)
6772 && !POINTER_TYPE_P (TREE_TYPE (t)))
6774 error ("%qD is not a pointer variable", t);
6775 remove = true;
6777 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
6778 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
6780 if (bitmap_bit_p (&generic_head, DECL_UID (t))
6781 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
6783 error ("%qD appears more than once in data clauses", t);
6784 remove = true;
6786 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
6788 if (ort == C_ORT_ACC)
6789 error ("%qD appears more than once in data clauses", t);
6790 else
6791 error ("%qD appears both in data and map clauses", t);
6792 remove = true;
6794 else
6795 bitmap_set_bit (&generic_head, DECL_UID (t));
6797 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
6799 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
6800 error ("%qD appears more than once in motion clauses", t);
6801 if (ort == C_ORT_ACC)
6802 error ("%qD appears more than once in data clauses", t);
6803 else
6804 error ("%qD appears more than once in map clauses", t);
6805 remove = true;
6807 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
6808 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
6810 if (ort == C_ORT_ACC)
6811 error ("%qD appears more than once in data clauses", t);
6812 else
6813 error ("%qD appears both in data and map clauses", t);
6814 remove = true;
6816 else
6818 bitmap_set_bit (&map_head, DECL_UID (t));
6819 if (t != OMP_CLAUSE_DECL (c)
6820 && TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPONENT_REF)
6821 bitmap_set_bit (&map_field_head, DECL_UID (t));
6823 handle_map_references:
6824 if (!remove
6825 && !processing_template_decl
6826 && (ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP
6827 && TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c))) == REFERENCE_TYPE)
6829 t = OMP_CLAUSE_DECL (c);
6830 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
6832 OMP_CLAUSE_DECL (c) = build_simple_mem_ref (t);
6833 if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
6834 OMP_CLAUSE_SIZE (c)
6835 = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t)));
6837 else if (OMP_CLAUSE_MAP_KIND (c)
6838 != GOMP_MAP_FIRSTPRIVATE_POINTER
6839 && (OMP_CLAUSE_MAP_KIND (c)
6840 != GOMP_MAP_FIRSTPRIVATE_REFERENCE)
6841 && (OMP_CLAUSE_MAP_KIND (c)
6842 != GOMP_MAP_ALWAYS_POINTER))
6844 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
6845 OMP_CLAUSE_MAP);
6846 if (TREE_CODE (t) == COMPONENT_REF)
6847 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ALWAYS_POINTER);
6848 else
6849 OMP_CLAUSE_SET_MAP_KIND (c2,
6850 GOMP_MAP_FIRSTPRIVATE_REFERENCE);
6851 OMP_CLAUSE_DECL (c2) = t;
6852 OMP_CLAUSE_SIZE (c2) = size_zero_node;
6853 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
6854 OMP_CLAUSE_CHAIN (c) = c2;
6855 OMP_CLAUSE_DECL (c) = build_simple_mem_ref (t);
6856 if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
6857 OMP_CLAUSE_SIZE (c)
6858 = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t)));
6859 c = c2;
6862 break;
6864 case OMP_CLAUSE_TO_DECLARE:
6865 case OMP_CLAUSE_LINK:
6866 t = OMP_CLAUSE_DECL (c);
6867 if (TREE_CODE (t) == FUNCTION_DECL
6868 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
6870 else if (!VAR_P (t))
6872 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
6874 if (TREE_CODE (t) == TEMPLATE_ID_EXPR)
6875 error_at (OMP_CLAUSE_LOCATION (c),
6876 "template %qE in clause %qs", t,
6877 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6878 else if (really_overloaded_fn (t))
6879 error_at (OMP_CLAUSE_LOCATION (c),
6880 "overloaded function name %qE in clause %qs", t,
6881 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6882 else
6883 error_at (OMP_CLAUSE_LOCATION (c),
6884 "%qE is neither a variable nor a function name "
6885 "in clause %qs", t,
6886 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6888 else
6889 error_at (OMP_CLAUSE_LOCATION (c),
6890 "%qE is not a variable in clause %qs", t,
6891 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6892 remove = true;
6894 else if (DECL_THREAD_LOCAL_P (t))
6896 error_at (OMP_CLAUSE_LOCATION (c),
6897 "%qD is threadprivate variable in %qs clause", t,
6898 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6899 remove = true;
6901 else if (!cp_omp_mappable_type (TREE_TYPE (t)))
6903 error_at (OMP_CLAUSE_LOCATION (c),
6904 "%qD does not have a mappable type in %qs clause", t,
6905 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6906 remove = true;
6908 if (remove)
6909 break;
6910 if (bitmap_bit_p (&generic_head, DECL_UID (t)))
6912 error_at (OMP_CLAUSE_LOCATION (c),
6913 "%qE appears more than once on the same "
6914 "%<declare target%> directive", t);
6915 remove = true;
6917 else
6918 bitmap_set_bit (&generic_head, DECL_UID (t));
6919 break;
6921 case OMP_CLAUSE_UNIFORM:
6922 t = OMP_CLAUSE_DECL (c);
6923 if (TREE_CODE (t) != PARM_DECL)
6925 if (processing_template_decl)
6926 break;
6927 if (DECL_P (t))
6928 error ("%qD is not an argument in %<uniform%> clause", t);
6929 else
6930 error ("%qE is not an argument in %<uniform%> clause", t);
6931 remove = true;
6932 break;
6934 /* map_head bitmap is used as uniform_head if declare_simd. */
6935 bitmap_set_bit (&map_head, DECL_UID (t));
6936 goto check_dup_generic;
6938 case OMP_CLAUSE_GRAINSIZE:
6939 t = OMP_CLAUSE_GRAINSIZE_EXPR (c);
6940 if (t == error_mark_node)
6941 remove = true;
6942 else if (!type_dependent_expression_p (t)
6943 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6945 error ("%<grainsize%> expression must be integral");
6946 remove = true;
6948 else
6950 t = mark_rvalue_use (t);
6951 if (!processing_template_decl)
6953 t = maybe_constant_value (t);
6954 if (TREE_CODE (t) == INTEGER_CST
6955 && tree_int_cst_sgn (t) != 1)
6957 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6958 "%<grainsize%> value must be positive");
6959 t = integer_one_node;
6961 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6963 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
6965 break;
6967 case OMP_CLAUSE_PRIORITY:
6968 t = OMP_CLAUSE_PRIORITY_EXPR (c);
6969 if (t == error_mark_node)
6970 remove = true;
6971 else if (!type_dependent_expression_p (t)
6972 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6974 error ("%<priority%> expression must be integral");
6975 remove = true;
6977 else
6979 t = mark_rvalue_use (t);
6980 if (!processing_template_decl)
6982 t = maybe_constant_value (t);
6983 if (TREE_CODE (t) == INTEGER_CST
6984 && tree_int_cst_sgn (t) == -1)
6986 warning_at (OMP_CLAUSE_LOCATION (c), 0,
6987 "%<priority%> value must be non-negative");
6988 t = integer_one_node;
6990 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6992 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
6994 break;
6996 case OMP_CLAUSE_HINT:
6997 t = OMP_CLAUSE_HINT_EXPR (c);
6998 if (t == error_mark_node)
6999 remove = true;
7000 else if (!type_dependent_expression_p (t)
7001 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
7003 error ("%<num_tasks%> expression must be integral");
7004 remove = true;
7006 else
7008 t = mark_rvalue_use (t);
7009 if (!processing_template_decl)
7011 t = maybe_constant_value (t);
7012 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7014 OMP_CLAUSE_HINT_EXPR (c) = t;
7016 break;
7018 case OMP_CLAUSE_IS_DEVICE_PTR:
7019 case OMP_CLAUSE_USE_DEVICE_PTR:
7020 field_ok = (ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP;
7021 t = OMP_CLAUSE_DECL (c);
7022 if (!type_dependent_expression_p (t))
7024 tree type = TREE_TYPE (t);
7025 if (TREE_CODE (type) != POINTER_TYPE
7026 && TREE_CODE (type) != ARRAY_TYPE
7027 && (TREE_CODE (type) != REFERENCE_TYPE
7028 || (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
7029 && TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE)))
7031 error_at (OMP_CLAUSE_LOCATION (c),
7032 "%qs variable is neither a pointer, nor an array "
7033 "nor reference to pointer or array",
7034 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7035 remove = true;
7038 goto check_dup_generic;
7040 case OMP_CLAUSE_NOWAIT:
7041 case OMP_CLAUSE_DEFAULT:
7042 case OMP_CLAUSE_UNTIED:
7043 case OMP_CLAUSE_COLLAPSE:
7044 case OMP_CLAUSE_MERGEABLE:
7045 case OMP_CLAUSE_PARALLEL:
7046 case OMP_CLAUSE_FOR:
7047 case OMP_CLAUSE_SECTIONS:
7048 case OMP_CLAUSE_TASKGROUP:
7049 case OMP_CLAUSE_PROC_BIND:
7050 case OMP_CLAUSE_NOGROUP:
7051 case OMP_CLAUSE_THREADS:
7052 case OMP_CLAUSE_SIMD:
7053 case OMP_CLAUSE_DEFAULTMAP:
7054 case OMP_CLAUSE_AUTO:
7055 case OMP_CLAUSE_INDEPENDENT:
7056 case OMP_CLAUSE_SEQ:
7057 break;
7059 case OMP_CLAUSE_TILE:
7060 for (tree list = OMP_CLAUSE_TILE_LIST (c); !remove && list;
7061 list = TREE_CHAIN (list))
7063 t = TREE_VALUE (list);
7065 if (t == error_mark_node)
7066 remove = true;
7067 else if (!type_dependent_expression_p (t)
7068 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
7070 error_at (OMP_CLAUSE_LOCATION (c),
7071 "%<tile%> argument needs integral type");
7072 remove = true;
7074 else
7076 t = mark_rvalue_use (t);
7077 if (!processing_template_decl)
7079 /* Zero is used to indicate '*', we permit you
7080 to get there via an ICE of value zero. */
7081 t = maybe_constant_value (t);
7082 if (!tree_fits_shwi_p (t)
7083 || tree_to_shwi (t) < 0)
7085 error_at (OMP_CLAUSE_LOCATION (c),
7086 "%<tile%> argument needs positive "
7087 "integral constant");
7088 remove = true;
7090 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7094 /* Update list item. */
7095 TREE_VALUE (list) = t;
7097 break;
7099 case OMP_CLAUSE_ORDERED:
7100 ordered_seen = true;
7101 break;
7103 case OMP_CLAUSE_INBRANCH:
7104 case OMP_CLAUSE_NOTINBRANCH:
7105 if (branch_seen)
7107 error ("%<inbranch%> clause is incompatible with "
7108 "%<notinbranch%>");
7109 remove = true;
7111 branch_seen = true;
7112 break;
7114 default:
7115 gcc_unreachable ();
7118 if (remove)
7119 *pc = OMP_CLAUSE_CHAIN (c);
7120 else
7121 pc = &OMP_CLAUSE_CHAIN (c);
7124 for (pc = &clauses, c = clauses; c ; c = *pc)
7126 enum omp_clause_code c_kind = OMP_CLAUSE_CODE (c);
7127 bool remove = false;
7128 bool need_complete_type = false;
7129 bool need_default_ctor = false;
7130 bool need_copy_ctor = false;
7131 bool need_copy_assignment = false;
7132 bool need_implicitly_determined = false;
7133 bool need_dtor = false;
7134 tree type, inner_type;
7136 switch (c_kind)
7138 case OMP_CLAUSE_SHARED:
7139 need_implicitly_determined = true;
7140 break;
7141 case OMP_CLAUSE_PRIVATE:
7142 need_complete_type = true;
7143 need_default_ctor = true;
7144 need_dtor = true;
7145 need_implicitly_determined = true;
7146 break;
7147 case OMP_CLAUSE_FIRSTPRIVATE:
7148 need_complete_type = true;
7149 need_copy_ctor = true;
7150 need_dtor = true;
7151 need_implicitly_determined = true;
7152 break;
7153 case OMP_CLAUSE_LASTPRIVATE:
7154 need_complete_type = true;
7155 need_copy_assignment = true;
7156 need_implicitly_determined = true;
7157 break;
7158 case OMP_CLAUSE_REDUCTION:
7159 need_implicitly_determined = true;
7160 break;
7161 case OMP_CLAUSE_LINEAR:
7162 if (ort != C_ORT_OMP_DECLARE_SIMD)
7163 need_implicitly_determined = true;
7164 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c)
7165 && !bitmap_bit_p (&map_head,
7166 DECL_UID (OMP_CLAUSE_LINEAR_STEP (c))))
7168 error_at (OMP_CLAUSE_LOCATION (c),
7169 "%<linear%> clause step is a parameter %qD not "
7170 "specified in %<uniform%> clause",
7171 OMP_CLAUSE_LINEAR_STEP (c));
7172 *pc = OMP_CLAUSE_CHAIN (c);
7173 continue;
7175 break;
7176 case OMP_CLAUSE_COPYPRIVATE:
7177 need_copy_assignment = true;
7178 break;
7179 case OMP_CLAUSE_COPYIN:
7180 need_copy_assignment = true;
7181 break;
7182 case OMP_CLAUSE_SIMDLEN:
7183 if (safelen
7184 && !processing_template_decl
7185 && tree_int_cst_lt (OMP_CLAUSE_SAFELEN_EXPR (safelen),
7186 OMP_CLAUSE_SIMDLEN_EXPR (c)))
7188 error_at (OMP_CLAUSE_LOCATION (c),
7189 "%<simdlen%> clause value is bigger than "
7190 "%<safelen%> clause value");
7191 OMP_CLAUSE_SIMDLEN_EXPR (c)
7192 = OMP_CLAUSE_SAFELEN_EXPR (safelen);
7194 pc = &OMP_CLAUSE_CHAIN (c);
7195 continue;
7196 case OMP_CLAUSE_SCHEDULE:
7197 if (ordered_seen
7198 && (OMP_CLAUSE_SCHEDULE_KIND (c)
7199 & OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
7201 error_at (OMP_CLAUSE_LOCATION (c),
7202 "%<nonmonotonic%> schedule modifier specified "
7203 "together with %<ordered%> clause");
7204 OMP_CLAUSE_SCHEDULE_KIND (c)
7205 = (enum omp_clause_schedule_kind)
7206 (OMP_CLAUSE_SCHEDULE_KIND (c)
7207 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
7209 pc = &OMP_CLAUSE_CHAIN (c);
7210 continue;
7211 case OMP_CLAUSE_NOWAIT:
7212 if (copyprivate_seen)
7214 error_at (OMP_CLAUSE_LOCATION (c),
7215 "%<nowait%> clause must not be used together "
7216 "with %<copyprivate%>");
7217 *pc = OMP_CLAUSE_CHAIN (c);
7218 continue;
7220 /* FALLTHRU */
7221 default:
7222 pc = &OMP_CLAUSE_CHAIN (c);
7223 continue;
7226 t = OMP_CLAUSE_DECL (c);
7227 if (processing_template_decl
7228 && !VAR_P (t) && TREE_CODE (t) != PARM_DECL)
7230 pc = &OMP_CLAUSE_CHAIN (c);
7231 continue;
7234 switch (c_kind)
7236 case OMP_CLAUSE_LASTPRIVATE:
7237 if (!bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
7239 need_default_ctor = true;
7240 need_dtor = true;
7242 break;
7244 case OMP_CLAUSE_REDUCTION:
7245 if (finish_omp_reduction_clause (c, &need_default_ctor,
7246 &need_dtor))
7247 remove = true;
7248 else
7249 t = OMP_CLAUSE_DECL (c);
7250 break;
7252 case OMP_CLAUSE_COPYIN:
7253 if (!VAR_P (t) || !CP_DECL_THREAD_LOCAL_P (t))
7255 error ("%qE must be %<threadprivate%> for %<copyin%>", t);
7256 remove = true;
7258 break;
7260 default:
7261 break;
7264 if (need_complete_type || need_copy_assignment)
7266 t = require_complete_type (t);
7267 if (t == error_mark_node)
7268 remove = true;
7269 else if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE
7270 && !complete_type_or_else (TREE_TYPE (TREE_TYPE (t)), t))
7271 remove = true;
7273 if (need_implicitly_determined)
7275 const char *share_name = NULL;
7277 if (VAR_P (t) && CP_DECL_THREAD_LOCAL_P (t))
7278 share_name = "threadprivate";
7279 else switch (cxx_omp_predetermined_sharing (t))
7281 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
7282 break;
7283 case OMP_CLAUSE_DEFAULT_SHARED:
7284 /* const vars may be specified in firstprivate clause. */
7285 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
7286 && cxx_omp_const_qual_no_mutable (t))
7287 break;
7288 share_name = "shared";
7289 break;
7290 case OMP_CLAUSE_DEFAULT_PRIVATE:
7291 share_name = "private";
7292 break;
7293 default:
7294 gcc_unreachable ();
7296 if (share_name)
7298 error ("%qE is predetermined %qs for %qs",
7299 omp_clause_printable_decl (t), share_name,
7300 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7301 remove = true;
7305 /* We're interested in the base element, not arrays. */
7306 inner_type = type = TREE_TYPE (t);
7307 if ((need_complete_type
7308 || need_copy_assignment
7309 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
7310 && TREE_CODE (inner_type) == REFERENCE_TYPE)
7311 inner_type = TREE_TYPE (inner_type);
7312 while (TREE_CODE (inner_type) == ARRAY_TYPE)
7313 inner_type = TREE_TYPE (inner_type);
7315 /* Check for special function availability by building a call to one.
7316 Save the results, because later we won't be in the right context
7317 for making these queries. */
7318 if (CLASS_TYPE_P (inner_type)
7319 && COMPLETE_TYPE_P (inner_type)
7320 && (need_default_ctor || need_copy_ctor
7321 || need_copy_assignment || need_dtor)
7322 && !type_dependent_expression_p (t)
7323 && cxx_omp_create_clause_info (c, inner_type, need_default_ctor,
7324 need_copy_ctor, need_copy_assignment,
7325 need_dtor))
7326 remove = true;
7328 if (!remove
7329 && c_kind == OMP_CLAUSE_SHARED
7330 && processing_template_decl)
7332 t = omp_clause_decl_field (OMP_CLAUSE_DECL (c));
7333 if (t)
7334 OMP_CLAUSE_DECL (c) = t;
7337 if (remove)
7338 *pc = OMP_CLAUSE_CHAIN (c);
7339 else
7340 pc = &OMP_CLAUSE_CHAIN (c);
7343 bitmap_obstack_release (NULL);
7344 return clauses;
7347 /* Start processing OpenMP clauses that can include any
7348 privatization clauses for non-static data members. */
7350 tree
7351 push_omp_privatization_clauses (bool ignore_next)
7353 if (omp_private_member_ignore_next)
7355 omp_private_member_ignore_next = ignore_next;
7356 return NULL_TREE;
7358 omp_private_member_ignore_next = ignore_next;
7359 if (omp_private_member_map)
7360 omp_private_member_vec.safe_push (error_mark_node);
7361 return push_stmt_list ();
7364 /* Revert remapping of any non-static data members since
7365 the last push_omp_privatization_clauses () call. */
7367 void
7368 pop_omp_privatization_clauses (tree stmt)
7370 if (stmt == NULL_TREE)
7371 return;
7372 stmt = pop_stmt_list (stmt);
7373 if (omp_private_member_map)
7375 while (!omp_private_member_vec.is_empty ())
7377 tree t = omp_private_member_vec.pop ();
7378 if (t == error_mark_node)
7380 add_stmt (stmt);
7381 return;
7383 bool no_decl_expr = t == integer_zero_node;
7384 if (no_decl_expr)
7385 t = omp_private_member_vec.pop ();
7386 tree *v = omp_private_member_map->get (t);
7387 gcc_assert (v);
7388 if (!no_decl_expr)
7389 add_decl_expr (*v);
7390 omp_private_member_map->remove (t);
7392 delete omp_private_member_map;
7393 omp_private_member_map = NULL;
7395 add_stmt (stmt);
7398 /* Remember OpenMP privatization clauses mapping and clear it.
7399 Used for lambdas. */
7401 void
7402 save_omp_privatization_clauses (vec<tree> &save)
7404 save = vNULL;
7405 if (omp_private_member_ignore_next)
7406 save.safe_push (integer_one_node);
7407 omp_private_member_ignore_next = false;
7408 if (!omp_private_member_map)
7409 return;
7411 while (!omp_private_member_vec.is_empty ())
7413 tree t = omp_private_member_vec.pop ();
7414 if (t == error_mark_node)
7416 save.safe_push (t);
7417 continue;
7419 tree n = t;
7420 if (t == integer_zero_node)
7421 t = omp_private_member_vec.pop ();
7422 tree *v = omp_private_member_map->get (t);
7423 gcc_assert (v);
7424 save.safe_push (*v);
7425 save.safe_push (t);
7426 if (n != t)
7427 save.safe_push (n);
7429 delete omp_private_member_map;
7430 omp_private_member_map = NULL;
7433 /* Restore OpenMP privatization clauses mapping saved by the
7434 above function. */
7436 void
7437 restore_omp_privatization_clauses (vec<tree> &save)
7439 gcc_assert (omp_private_member_vec.is_empty ());
7440 omp_private_member_ignore_next = false;
7441 if (save.is_empty ())
7442 return;
7443 if (save.length () == 1 && save[0] == integer_one_node)
7445 omp_private_member_ignore_next = true;
7446 save.release ();
7447 return;
7450 omp_private_member_map = new hash_map <tree, tree>;
7451 while (!save.is_empty ())
7453 tree t = save.pop ();
7454 tree n = t;
7455 if (t != error_mark_node)
7457 if (t == integer_one_node)
7459 omp_private_member_ignore_next = true;
7460 gcc_assert (save.is_empty ());
7461 break;
7463 if (t == integer_zero_node)
7464 t = save.pop ();
7465 tree &v = omp_private_member_map->get_or_insert (t);
7466 v = save.pop ();
7468 omp_private_member_vec.safe_push (t);
7469 if (n != t)
7470 omp_private_member_vec.safe_push (n);
7472 save.release ();
7475 /* For all variables in the tree_list VARS, mark them as thread local. */
7477 void
7478 finish_omp_threadprivate (tree vars)
7480 tree t;
7482 /* Mark every variable in VARS to be assigned thread local storage. */
7483 for (t = vars; t; t = TREE_CHAIN (t))
7485 tree v = TREE_PURPOSE (t);
7487 if (error_operand_p (v))
7489 else if (!VAR_P (v))
7490 error ("%<threadprivate%> %qD is not file, namespace "
7491 "or block scope variable", v);
7492 /* If V had already been marked threadprivate, it doesn't matter
7493 whether it had been used prior to this point. */
7494 else if (TREE_USED (v)
7495 && (DECL_LANG_SPECIFIC (v) == NULL
7496 || !CP_DECL_THREADPRIVATE_P (v)))
7497 error ("%qE declared %<threadprivate%> after first use", v);
7498 else if (! TREE_STATIC (v) && ! DECL_EXTERNAL (v))
7499 error ("automatic variable %qE cannot be %<threadprivate%>", v);
7500 else if (! COMPLETE_TYPE_P (complete_type (TREE_TYPE (v))))
7501 error ("%<threadprivate%> %qE has incomplete type", v);
7502 else if (TREE_STATIC (v) && TYPE_P (CP_DECL_CONTEXT (v))
7503 && CP_DECL_CONTEXT (v) != current_class_type)
7504 error ("%<threadprivate%> %qE directive not "
7505 "in %qT definition", v, CP_DECL_CONTEXT (v));
7506 else
7508 /* Allocate a LANG_SPECIFIC structure for V, if needed. */
7509 if (DECL_LANG_SPECIFIC (v) == NULL)
7511 retrofit_lang_decl (v);
7513 /* Make sure that DECL_DISCRIMINATOR_P continues to be true
7514 after the allocation of the lang_decl structure. */
7515 if (DECL_DISCRIMINATOR_P (v))
7516 DECL_LANG_SPECIFIC (v)->u.base.u2sel = 1;
7519 if (! CP_DECL_THREAD_LOCAL_P (v))
7521 CP_DECL_THREAD_LOCAL_P (v) = true;
7522 set_decl_tls_model (v, decl_default_tls_model (v));
7523 /* If rtl has been already set for this var, call
7524 make_decl_rtl once again, so that encode_section_info
7525 has a chance to look at the new decl flags. */
7526 if (DECL_RTL_SET_P (v))
7527 make_decl_rtl (v);
7529 CP_DECL_THREADPRIVATE_P (v) = 1;
7534 /* Build an OpenMP structured block. */
7536 tree
7537 begin_omp_structured_block (void)
7539 return do_pushlevel (sk_omp);
7542 tree
7543 finish_omp_structured_block (tree block)
7545 return do_poplevel (block);
7548 /* Similarly, except force the retention of the BLOCK. */
7550 tree
7551 begin_omp_parallel (void)
7553 keep_next_level (true);
7554 return begin_omp_structured_block ();
7557 /* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
7558 statement. */
7560 tree
7561 finish_oacc_data (tree clauses, tree block)
7563 tree stmt;
7565 block = finish_omp_structured_block (block);
7567 stmt = make_node (OACC_DATA);
7568 TREE_TYPE (stmt) = void_type_node;
7569 OACC_DATA_CLAUSES (stmt) = clauses;
7570 OACC_DATA_BODY (stmt) = block;
7572 return add_stmt (stmt);
7575 /* Generate OACC_HOST_DATA, with CLAUSES and BLOCK as its compound
7576 statement. */
7578 tree
7579 finish_oacc_host_data (tree clauses, tree block)
7581 tree stmt;
7583 block = finish_omp_structured_block (block);
7585 stmt = make_node (OACC_HOST_DATA);
7586 TREE_TYPE (stmt) = void_type_node;
7587 OACC_HOST_DATA_CLAUSES (stmt) = clauses;
7588 OACC_HOST_DATA_BODY (stmt) = block;
7590 return add_stmt (stmt);
7593 /* Generate OMP construct CODE, with BODY and CLAUSES as its compound
7594 statement. */
7596 tree
7597 finish_omp_construct (enum tree_code code, tree body, tree clauses)
7599 body = finish_omp_structured_block (body);
7601 tree stmt = make_node (code);
7602 TREE_TYPE (stmt) = void_type_node;
7603 OMP_BODY (stmt) = body;
7604 OMP_CLAUSES (stmt) = clauses;
7606 return add_stmt (stmt);
7609 tree
7610 finish_omp_parallel (tree clauses, tree body)
7612 tree stmt;
7614 body = finish_omp_structured_block (body);
7616 stmt = make_node (OMP_PARALLEL);
7617 TREE_TYPE (stmt) = void_type_node;
7618 OMP_PARALLEL_CLAUSES (stmt) = clauses;
7619 OMP_PARALLEL_BODY (stmt) = body;
7621 return add_stmt (stmt);
7624 tree
7625 begin_omp_task (void)
7627 keep_next_level (true);
7628 return begin_omp_structured_block ();
7631 tree
7632 finish_omp_task (tree clauses, tree body)
7634 tree stmt;
7636 body = finish_omp_structured_block (body);
7638 stmt = make_node (OMP_TASK);
7639 TREE_TYPE (stmt) = void_type_node;
7640 OMP_TASK_CLAUSES (stmt) = clauses;
7641 OMP_TASK_BODY (stmt) = body;
7643 return add_stmt (stmt);
7646 /* Helper function for finish_omp_for. Convert Ith random access iterator
7647 into integral iterator. Return FALSE if successful. */
7649 static bool
7650 handle_omp_for_class_iterator (int i, location_t locus, enum tree_code code,
7651 tree declv, tree orig_declv, tree initv,
7652 tree condv, tree incrv, tree *body,
7653 tree *pre_body, tree &clauses, tree *lastp,
7654 int collapse, int ordered)
7656 tree diff, iter_init, iter_incr = NULL, last;
7657 tree incr_var = NULL, orig_pre_body, orig_body, c;
7658 tree decl = TREE_VEC_ELT (declv, i);
7659 tree init = TREE_VEC_ELT (initv, i);
7660 tree cond = TREE_VEC_ELT (condv, i);
7661 tree incr = TREE_VEC_ELT (incrv, i);
7662 tree iter = decl;
7663 location_t elocus = locus;
7665 if (init && EXPR_HAS_LOCATION (init))
7666 elocus = EXPR_LOCATION (init);
7668 cond = cp_fully_fold (cond);
7669 switch (TREE_CODE (cond))
7671 case GT_EXPR:
7672 case GE_EXPR:
7673 case LT_EXPR:
7674 case LE_EXPR:
7675 case NE_EXPR:
7676 if (TREE_OPERAND (cond, 1) == iter)
7677 cond = build2 (swap_tree_comparison (TREE_CODE (cond)),
7678 TREE_TYPE (cond), iter, TREE_OPERAND (cond, 0));
7679 if (TREE_OPERAND (cond, 0) != iter)
7680 cond = error_mark_node;
7681 else
7683 tree tem = build_x_binary_op (EXPR_LOCATION (cond),
7684 TREE_CODE (cond),
7685 iter, ERROR_MARK,
7686 TREE_OPERAND (cond, 1), ERROR_MARK,
7687 NULL, tf_warning_or_error);
7688 if (error_operand_p (tem))
7689 return true;
7691 break;
7692 default:
7693 cond = error_mark_node;
7694 break;
7696 if (cond == error_mark_node)
7698 error_at (elocus, "invalid controlling predicate");
7699 return true;
7701 diff = build_x_binary_op (elocus, MINUS_EXPR, TREE_OPERAND (cond, 1),
7702 ERROR_MARK, iter, ERROR_MARK, NULL,
7703 tf_warning_or_error);
7704 diff = cp_fully_fold (diff);
7705 if (error_operand_p (diff))
7706 return true;
7707 if (TREE_CODE (TREE_TYPE (diff)) != INTEGER_TYPE)
7709 error_at (elocus, "difference between %qE and %qD does not have integer type",
7710 TREE_OPERAND (cond, 1), iter);
7711 return true;
7713 if (!c_omp_check_loop_iv_exprs (locus, orig_declv,
7714 TREE_VEC_ELT (declv, i), NULL_TREE,
7715 cond, cp_walk_subtrees))
7716 return true;
7718 switch (TREE_CODE (incr))
7720 case PREINCREMENT_EXPR:
7721 case PREDECREMENT_EXPR:
7722 case POSTINCREMENT_EXPR:
7723 case POSTDECREMENT_EXPR:
7724 if (TREE_OPERAND (incr, 0) != iter)
7726 incr = error_mark_node;
7727 break;
7729 iter_incr = build_x_unary_op (EXPR_LOCATION (incr),
7730 TREE_CODE (incr), iter,
7731 tf_warning_or_error);
7732 if (error_operand_p (iter_incr))
7733 return true;
7734 else if (TREE_CODE (incr) == PREINCREMENT_EXPR
7735 || TREE_CODE (incr) == POSTINCREMENT_EXPR)
7736 incr = integer_one_node;
7737 else
7738 incr = integer_minus_one_node;
7739 break;
7740 case MODIFY_EXPR:
7741 if (TREE_OPERAND (incr, 0) != iter)
7742 incr = error_mark_node;
7743 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
7744 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
7746 tree rhs = TREE_OPERAND (incr, 1);
7747 if (TREE_OPERAND (rhs, 0) == iter)
7749 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (rhs, 1)))
7750 != INTEGER_TYPE)
7751 incr = error_mark_node;
7752 else
7754 iter_incr = build_x_modify_expr (EXPR_LOCATION (rhs),
7755 iter, TREE_CODE (rhs),
7756 TREE_OPERAND (rhs, 1),
7757 tf_warning_or_error);
7758 if (error_operand_p (iter_incr))
7759 return true;
7760 incr = TREE_OPERAND (rhs, 1);
7761 incr = cp_convert (TREE_TYPE (diff), incr,
7762 tf_warning_or_error);
7763 if (TREE_CODE (rhs) == MINUS_EXPR)
7765 incr = build1 (NEGATE_EXPR, TREE_TYPE (diff), incr);
7766 incr = fold_simple (incr);
7768 if (TREE_CODE (incr) != INTEGER_CST
7769 && (TREE_CODE (incr) != NOP_EXPR
7770 || (TREE_CODE (TREE_OPERAND (incr, 0))
7771 != INTEGER_CST)))
7772 iter_incr = NULL;
7775 else if (TREE_OPERAND (rhs, 1) == iter)
7777 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (rhs, 0))) != INTEGER_TYPE
7778 || TREE_CODE (rhs) != PLUS_EXPR)
7779 incr = error_mark_node;
7780 else
7782 iter_incr = build_x_binary_op (EXPR_LOCATION (rhs),
7783 PLUS_EXPR,
7784 TREE_OPERAND (rhs, 0),
7785 ERROR_MARK, iter,
7786 ERROR_MARK, NULL,
7787 tf_warning_or_error);
7788 if (error_operand_p (iter_incr))
7789 return true;
7790 iter_incr = build_x_modify_expr (EXPR_LOCATION (rhs),
7791 iter, NOP_EXPR,
7792 iter_incr,
7793 tf_warning_or_error);
7794 if (error_operand_p (iter_incr))
7795 return true;
7796 incr = TREE_OPERAND (rhs, 0);
7797 iter_incr = NULL;
7800 else
7801 incr = error_mark_node;
7803 else
7804 incr = error_mark_node;
7805 break;
7806 default:
7807 incr = error_mark_node;
7808 break;
7811 if (incr == error_mark_node)
7813 error_at (elocus, "invalid increment expression");
7814 return true;
7817 incr = cp_convert (TREE_TYPE (diff), incr, tf_warning_or_error);
7818 bool taskloop_iv_seen = false;
7819 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
7820 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
7821 && OMP_CLAUSE_DECL (c) == iter)
7823 if (code == OMP_TASKLOOP)
7825 taskloop_iv_seen = true;
7826 OMP_CLAUSE_LASTPRIVATE_TASKLOOP_IV (c) = 1;
7828 break;
7830 else if (code == OMP_TASKLOOP
7831 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
7832 && OMP_CLAUSE_DECL (c) == iter)
7834 taskloop_iv_seen = true;
7835 OMP_CLAUSE_PRIVATE_TASKLOOP_IV (c) = 1;
7838 decl = create_temporary_var (TREE_TYPE (diff));
7839 pushdecl (decl);
7840 add_decl_expr (decl);
7841 last = create_temporary_var (TREE_TYPE (diff));
7842 pushdecl (last);
7843 add_decl_expr (last);
7844 if (c && iter_incr == NULL && TREE_CODE (incr) != INTEGER_CST
7845 && (!ordered || (i < collapse && collapse > 1)))
7847 incr_var = create_temporary_var (TREE_TYPE (diff));
7848 pushdecl (incr_var);
7849 add_decl_expr (incr_var);
7851 gcc_assert (stmts_are_full_exprs_p ());
7852 tree diffvar = NULL_TREE;
7853 if (code == OMP_TASKLOOP)
7855 if (!taskloop_iv_seen)
7857 tree ivc = build_omp_clause (locus, OMP_CLAUSE_FIRSTPRIVATE);
7858 OMP_CLAUSE_DECL (ivc) = iter;
7859 cxx_omp_finish_clause (ivc, NULL);
7860 OMP_CLAUSE_CHAIN (ivc) = clauses;
7861 clauses = ivc;
7863 tree lvc = build_omp_clause (locus, OMP_CLAUSE_FIRSTPRIVATE);
7864 OMP_CLAUSE_DECL (lvc) = last;
7865 OMP_CLAUSE_CHAIN (lvc) = clauses;
7866 clauses = lvc;
7867 diffvar = create_temporary_var (TREE_TYPE (diff));
7868 pushdecl (diffvar);
7869 add_decl_expr (diffvar);
7872 orig_pre_body = *pre_body;
7873 *pre_body = push_stmt_list ();
7874 if (orig_pre_body)
7875 add_stmt (orig_pre_body);
7876 if (init != NULL)
7877 finish_expr_stmt (build_x_modify_expr (elocus,
7878 iter, NOP_EXPR, init,
7879 tf_warning_or_error));
7880 init = build_int_cst (TREE_TYPE (diff), 0);
7881 if (c && iter_incr == NULL
7882 && (!ordered || (i < collapse && collapse > 1)))
7884 if (incr_var)
7886 finish_expr_stmt (build_x_modify_expr (elocus,
7887 incr_var, NOP_EXPR,
7888 incr, tf_warning_or_error));
7889 incr = incr_var;
7891 iter_incr = build_x_modify_expr (elocus,
7892 iter, PLUS_EXPR, incr,
7893 tf_warning_or_error);
7895 if (c && ordered && i < collapse && collapse > 1)
7896 iter_incr = incr;
7897 finish_expr_stmt (build_x_modify_expr (elocus,
7898 last, NOP_EXPR, init,
7899 tf_warning_or_error));
7900 if (diffvar)
7902 finish_expr_stmt (build_x_modify_expr (elocus,
7903 diffvar, NOP_EXPR,
7904 diff, tf_warning_or_error));
7905 diff = diffvar;
7907 *pre_body = pop_stmt_list (*pre_body);
7909 cond = cp_build_binary_op (elocus,
7910 TREE_CODE (cond), decl, diff,
7911 tf_warning_or_error);
7912 incr = build_modify_expr (elocus, decl, NULL_TREE, PLUS_EXPR,
7913 elocus, incr, NULL_TREE);
7915 orig_body = *body;
7916 *body = push_stmt_list ();
7917 iter_init = build2 (MINUS_EXPR, TREE_TYPE (diff), decl, last);
7918 iter_init = build_x_modify_expr (elocus,
7919 iter, PLUS_EXPR, iter_init,
7920 tf_warning_or_error);
7921 if (iter_init != error_mark_node)
7922 iter_init = build1 (NOP_EXPR, void_type_node, iter_init);
7923 finish_expr_stmt (iter_init);
7924 finish_expr_stmt (build_x_modify_expr (elocus,
7925 last, NOP_EXPR, decl,
7926 tf_warning_or_error));
7927 add_stmt (orig_body);
7928 *body = pop_stmt_list (*body);
7930 if (c)
7932 OMP_CLAUSE_LASTPRIVATE_STMT (c) = push_stmt_list ();
7933 if (!ordered)
7934 finish_expr_stmt (iter_incr);
7935 else
7937 iter_init = decl;
7938 if (i < collapse && collapse > 1 && !error_operand_p (iter_incr))
7939 iter_init = build2 (PLUS_EXPR, TREE_TYPE (diff),
7940 iter_init, iter_incr);
7941 iter_init = build2 (MINUS_EXPR, TREE_TYPE (diff), iter_init, last);
7942 iter_init = build_x_modify_expr (elocus,
7943 iter, PLUS_EXPR, iter_init,
7944 tf_warning_or_error);
7945 if (iter_init != error_mark_node)
7946 iter_init = build1 (NOP_EXPR, void_type_node, iter_init);
7947 finish_expr_stmt (iter_init);
7949 OMP_CLAUSE_LASTPRIVATE_STMT (c)
7950 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (c));
7953 TREE_VEC_ELT (declv, i) = decl;
7954 TREE_VEC_ELT (initv, i) = init;
7955 TREE_VEC_ELT (condv, i) = cond;
7956 TREE_VEC_ELT (incrv, i) = incr;
7957 *lastp = last;
7959 return false;
7962 /* Build and validate an OMP_FOR statement. CLAUSES, BODY, COND, INCR
7963 are directly for their associated operands in the statement. DECL
7964 and INIT are a combo; if DECL is NULL then INIT ought to be a
7965 MODIFY_EXPR, and the DECL should be extracted. PRE_BODY are
7966 optional statements that need to go before the loop into its
7967 sk_omp scope. */
7969 tree
7970 finish_omp_for (location_t locus, enum tree_code code, tree declv,
7971 tree orig_declv, tree initv, tree condv, tree incrv,
7972 tree body, tree pre_body, vec<tree> *orig_inits, tree clauses)
7974 tree omp_for = NULL, orig_incr = NULL;
7975 tree decl = NULL, init, cond, incr;
7976 tree last = NULL_TREE;
7977 location_t elocus;
7978 int i;
7979 int collapse = 1;
7980 int ordered = 0;
7982 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (initv));
7983 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (condv));
7984 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (incrv));
7985 if (TREE_VEC_LENGTH (declv) > 1)
7987 tree c;
7989 c = omp_find_clause (clauses, OMP_CLAUSE_TILE);
7990 if (c)
7991 collapse = list_length (OMP_CLAUSE_TILE_LIST (c));
7992 else
7994 c = omp_find_clause (clauses, OMP_CLAUSE_COLLAPSE);
7995 if (c)
7996 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (c));
7997 if (collapse != TREE_VEC_LENGTH (declv))
7998 ordered = TREE_VEC_LENGTH (declv);
8001 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
8003 decl = TREE_VEC_ELT (declv, i);
8004 init = TREE_VEC_ELT (initv, i);
8005 cond = TREE_VEC_ELT (condv, i);
8006 incr = TREE_VEC_ELT (incrv, i);
8007 elocus = locus;
8009 if (decl == NULL)
8011 if (init != NULL)
8012 switch (TREE_CODE (init))
8014 case MODIFY_EXPR:
8015 decl = TREE_OPERAND (init, 0);
8016 init = TREE_OPERAND (init, 1);
8017 break;
8018 case MODOP_EXPR:
8019 if (TREE_CODE (TREE_OPERAND (init, 1)) == NOP_EXPR)
8021 decl = TREE_OPERAND (init, 0);
8022 init = TREE_OPERAND (init, 2);
8024 break;
8025 default:
8026 break;
8029 if (decl == NULL)
8031 error_at (locus,
8032 "expected iteration declaration or initialization");
8033 return NULL;
8037 if (init && EXPR_HAS_LOCATION (init))
8038 elocus = EXPR_LOCATION (init);
8040 if (cond == NULL)
8042 error_at (elocus, "missing controlling predicate");
8043 return NULL;
8046 if (incr == NULL)
8048 error_at (elocus, "missing increment expression");
8049 return NULL;
8052 TREE_VEC_ELT (declv, i) = decl;
8053 TREE_VEC_ELT (initv, i) = init;
8056 if (orig_inits)
8058 bool fail = false;
8059 tree orig_init;
8060 FOR_EACH_VEC_ELT (*orig_inits, i, orig_init)
8061 if (orig_init
8062 && !c_omp_check_loop_iv_exprs (locus, declv,
8063 TREE_VEC_ELT (declv, i), orig_init,
8064 NULL_TREE, cp_walk_subtrees))
8065 fail = true;
8066 if (fail)
8067 return NULL;
8070 if (dependent_omp_for_p (declv, initv, condv, incrv))
8072 tree stmt;
8074 stmt = make_node (code);
8076 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
8078 /* This is really just a place-holder. We'll be decomposing this
8079 again and going through the cp_build_modify_expr path below when
8080 we instantiate the thing. */
8081 TREE_VEC_ELT (initv, i)
8082 = build2 (MODIFY_EXPR, void_type_node, TREE_VEC_ELT (declv, i),
8083 TREE_VEC_ELT (initv, i));
8086 TREE_TYPE (stmt) = void_type_node;
8087 OMP_FOR_INIT (stmt) = initv;
8088 OMP_FOR_COND (stmt) = condv;
8089 OMP_FOR_INCR (stmt) = incrv;
8090 OMP_FOR_BODY (stmt) = body;
8091 OMP_FOR_PRE_BODY (stmt) = pre_body;
8092 OMP_FOR_CLAUSES (stmt) = clauses;
8094 SET_EXPR_LOCATION (stmt, locus);
8095 return add_stmt (stmt);
8098 if (!orig_declv)
8099 orig_declv = copy_node (declv);
8101 if (processing_template_decl)
8102 orig_incr = make_tree_vec (TREE_VEC_LENGTH (incrv));
8104 for (i = 0; i < TREE_VEC_LENGTH (declv); )
8106 decl = TREE_VEC_ELT (declv, i);
8107 init = TREE_VEC_ELT (initv, i);
8108 cond = TREE_VEC_ELT (condv, i);
8109 incr = TREE_VEC_ELT (incrv, i);
8110 if (orig_incr)
8111 TREE_VEC_ELT (orig_incr, i) = incr;
8112 elocus = locus;
8114 if (init && EXPR_HAS_LOCATION (init))
8115 elocus = EXPR_LOCATION (init);
8117 if (!DECL_P (decl))
8119 error_at (elocus, "expected iteration declaration or initialization");
8120 return NULL;
8123 if (incr && TREE_CODE (incr) == MODOP_EXPR)
8125 if (orig_incr)
8126 TREE_VEC_ELT (orig_incr, i) = incr;
8127 incr = cp_build_modify_expr (elocus, TREE_OPERAND (incr, 0),
8128 TREE_CODE (TREE_OPERAND (incr, 1)),
8129 TREE_OPERAND (incr, 2),
8130 tf_warning_or_error);
8133 if (CLASS_TYPE_P (TREE_TYPE (decl)))
8135 if (code == OMP_SIMD)
8137 error_at (elocus, "%<#pragma omp simd%> used with class "
8138 "iteration variable %qE", decl);
8139 return NULL;
8141 if (handle_omp_for_class_iterator (i, locus, code, declv, orig_declv,
8142 initv, condv, incrv, &body,
8143 &pre_body, clauses, &last,
8144 collapse, ordered))
8145 return NULL;
8146 continue;
8149 if (!INTEGRAL_TYPE_P (TREE_TYPE (decl))
8150 && !TYPE_PTR_P (TREE_TYPE (decl)))
8152 error_at (elocus, "invalid type for iteration variable %qE", decl);
8153 return NULL;
8156 if (!processing_template_decl)
8158 init = fold_build_cleanup_point_expr (TREE_TYPE (init), init);
8159 init = cp_build_modify_expr (elocus, decl, NOP_EXPR, init,
8160 tf_warning_or_error);
8162 else
8163 init = build2 (MODIFY_EXPR, void_type_node, decl, init);
8164 if (cond
8165 && TREE_SIDE_EFFECTS (cond)
8166 && COMPARISON_CLASS_P (cond)
8167 && !processing_template_decl)
8169 tree t = TREE_OPERAND (cond, 0);
8170 if (TREE_SIDE_EFFECTS (t)
8171 && t != decl
8172 && (TREE_CODE (t) != NOP_EXPR
8173 || TREE_OPERAND (t, 0) != decl))
8174 TREE_OPERAND (cond, 0)
8175 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
8177 t = TREE_OPERAND (cond, 1);
8178 if (TREE_SIDE_EFFECTS (t)
8179 && t != decl
8180 && (TREE_CODE (t) != NOP_EXPR
8181 || TREE_OPERAND (t, 0) != decl))
8182 TREE_OPERAND (cond, 1)
8183 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
8185 if (decl == error_mark_node || init == error_mark_node)
8186 return NULL;
8188 TREE_VEC_ELT (declv, i) = decl;
8189 TREE_VEC_ELT (initv, i) = init;
8190 TREE_VEC_ELT (condv, i) = cond;
8191 TREE_VEC_ELT (incrv, i) = incr;
8192 i++;
8195 if (IS_EMPTY_STMT (pre_body))
8196 pre_body = NULL;
8198 omp_for = c_finish_omp_for (locus, code, declv, orig_declv, initv, condv,
8199 incrv, body, pre_body);
8201 /* Check for iterators appearing in lb, b or incr expressions. */
8202 if (omp_for && !c_omp_check_loop_iv (omp_for, orig_declv, cp_walk_subtrees))
8203 omp_for = NULL_TREE;
8205 if (omp_for == NULL)
8207 return NULL;
8210 add_stmt (omp_for);
8212 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INCR (omp_for)); i++)
8214 decl = TREE_OPERAND (TREE_VEC_ELT (OMP_FOR_INIT (omp_for), i), 0);
8215 incr = TREE_VEC_ELT (OMP_FOR_INCR (omp_for), i);
8217 if (TREE_CODE (incr) != MODIFY_EXPR)
8218 continue;
8220 if (TREE_SIDE_EFFECTS (TREE_OPERAND (incr, 1))
8221 && BINARY_CLASS_P (TREE_OPERAND (incr, 1))
8222 && !processing_template_decl)
8224 tree t = TREE_OPERAND (TREE_OPERAND (incr, 1), 0);
8225 if (TREE_SIDE_EFFECTS (t)
8226 && t != decl
8227 && (TREE_CODE (t) != NOP_EXPR
8228 || TREE_OPERAND (t, 0) != decl))
8229 TREE_OPERAND (TREE_OPERAND (incr, 1), 0)
8230 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
8232 t = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
8233 if (TREE_SIDE_EFFECTS (t)
8234 && t != decl
8235 && (TREE_CODE (t) != NOP_EXPR
8236 || TREE_OPERAND (t, 0) != decl))
8237 TREE_OPERAND (TREE_OPERAND (incr, 1), 1)
8238 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
8241 if (orig_incr)
8242 TREE_VEC_ELT (OMP_FOR_INCR (omp_for), i) = TREE_VEC_ELT (orig_incr, i);
8244 OMP_FOR_CLAUSES (omp_for) = clauses;
8246 /* For simd loops with non-static data member iterators, we could have added
8247 OMP_CLAUSE_LINEAR clauses without OMP_CLAUSE_LINEAR_STEP. As we know the
8248 step at this point, fill it in. */
8249 if (code == OMP_SIMD && !processing_template_decl
8250 && TREE_VEC_LENGTH (OMP_FOR_INCR (omp_for)) == 1)
8251 for (tree c = omp_find_clause (clauses, OMP_CLAUSE_LINEAR); c;
8252 c = omp_find_clause (OMP_CLAUSE_CHAIN (c), OMP_CLAUSE_LINEAR))
8253 if (OMP_CLAUSE_LINEAR_STEP (c) == NULL_TREE)
8255 decl = TREE_OPERAND (TREE_VEC_ELT (OMP_FOR_INIT (omp_for), 0), 0);
8256 gcc_assert (decl == OMP_CLAUSE_DECL (c));
8257 incr = TREE_VEC_ELT (OMP_FOR_INCR (omp_for), 0);
8258 tree step, stept;
8259 switch (TREE_CODE (incr))
8261 case PREINCREMENT_EXPR:
8262 case POSTINCREMENT_EXPR:
8263 /* c_omp_for_incr_canonicalize_ptr() should have been
8264 called to massage things appropriately. */
8265 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (decl)));
8266 OMP_CLAUSE_LINEAR_STEP (c) = build_int_cst (TREE_TYPE (decl), 1);
8267 break;
8268 case PREDECREMENT_EXPR:
8269 case POSTDECREMENT_EXPR:
8270 /* c_omp_for_incr_canonicalize_ptr() should have been
8271 called to massage things appropriately. */
8272 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (decl)));
8273 OMP_CLAUSE_LINEAR_STEP (c)
8274 = build_int_cst (TREE_TYPE (decl), -1);
8275 break;
8276 case MODIFY_EXPR:
8277 gcc_assert (TREE_OPERAND (incr, 0) == decl);
8278 incr = TREE_OPERAND (incr, 1);
8279 switch (TREE_CODE (incr))
8281 case PLUS_EXPR:
8282 if (TREE_OPERAND (incr, 1) == decl)
8283 step = TREE_OPERAND (incr, 0);
8284 else
8285 step = TREE_OPERAND (incr, 1);
8286 break;
8287 case MINUS_EXPR:
8288 case POINTER_PLUS_EXPR:
8289 gcc_assert (TREE_OPERAND (incr, 0) == decl);
8290 step = TREE_OPERAND (incr, 1);
8291 break;
8292 default:
8293 gcc_unreachable ();
8295 stept = TREE_TYPE (decl);
8296 if (POINTER_TYPE_P (stept))
8297 stept = sizetype;
8298 step = fold_convert (stept, step);
8299 if (TREE_CODE (incr) == MINUS_EXPR)
8300 step = fold_build1 (NEGATE_EXPR, stept, step);
8301 OMP_CLAUSE_LINEAR_STEP (c) = step;
8302 break;
8303 default:
8304 gcc_unreachable ();
8308 return omp_for;
8311 void
8312 finish_omp_atomic (enum tree_code code, enum tree_code opcode, tree lhs,
8313 tree rhs, tree v, tree lhs1, tree rhs1, bool seq_cst)
8315 tree orig_lhs;
8316 tree orig_rhs;
8317 tree orig_v;
8318 tree orig_lhs1;
8319 tree orig_rhs1;
8320 bool dependent_p;
8321 tree stmt;
8323 orig_lhs = lhs;
8324 orig_rhs = rhs;
8325 orig_v = v;
8326 orig_lhs1 = lhs1;
8327 orig_rhs1 = rhs1;
8328 dependent_p = false;
8329 stmt = NULL_TREE;
8331 /* Even in a template, we can detect invalid uses of the atomic
8332 pragma if neither LHS nor RHS is type-dependent. */
8333 if (processing_template_decl)
8335 dependent_p = (type_dependent_expression_p (lhs)
8336 || (rhs && type_dependent_expression_p (rhs))
8337 || (v && type_dependent_expression_p (v))
8338 || (lhs1 && type_dependent_expression_p (lhs1))
8339 || (rhs1 && type_dependent_expression_p (rhs1)));
8340 if (!dependent_p)
8342 lhs = build_non_dependent_expr (lhs);
8343 if (rhs)
8344 rhs = build_non_dependent_expr (rhs);
8345 if (v)
8346 v = build_non_dependent_expr (v);
8347 if (lhs1)
8348 lhs1 = build_non_dependent_expr (lhs1);
8349 if (rhs1)
8350 rhs1 = build_non_dependent_expr (rhs1);
8353 if (!dependent_p)
8355 bool swapped = false;
8356 if (rhs1 && cp_tree_equal (lhs, rhs))
8358 std::swap (rhs, rhs1);
8359 swapped = !commutative_tree_code (opcode);
8361 if (rhs1 && !cp_tree_equal (lhs, rhs1))
8363 if (code == OMP_ATOMIC)
8364 error ("%<#pragma omp atomic update%> uses two different "
8365 "expressions for memory");
8366 else
8367 error ("%<#pragma omp atomic capture%> uses two different "
8368 "expressions for memory");
8369 return;
8371 if (lhs1 && !cp_tree_equal (lhs, lhs1))
8373 if (code == OMP_ATOMIC)
8374 error ("%<#pragma omp atomic update%> uses two different "
8375 "expressions for memory");
8376 else
8377 error ("%<#pragma omp atomic capture%> uses two different "
8378 "expressions for memory");
8379 return;
8381 stmt = c_finish_omp_atomic (input_location, code, opcode, lhs, rhs,
8382 v, lhs1, rhs1, swapped, seq_cst,
8383 processing_template_decl != 0);
8384 if (stmt == error_mark_node)
8385 return;
8387 if (processing_template_decl)
8389 if (code == OMP_ATOMIC_READ)
8391 stmt = build_min_nt_loc (EXPR_LOCATION (orig_lhs),
8392 OMP_ATOMIC_READ, orig_lhs);
8393 OMP_ATOMIC_SEQ_CST (stmt) = seq_cst;
8394 stmt = build2 (MODIFY_EXPR, void_type_node, orig_v, stmt);
8396 else
8398 if (opcode == NOP_EXPR)
8399 stmt = build2 (MODIFY_EXPR, void_type_node, orig_lhs, orig_rhs);
8400 else
8401 stmt = build2 (opcode, void_type_node, orig_lhs, orig_rhs);
8402 if (orig_rhs1)
8403 stmt = build_min_nt_loc (EXPR_LOCATION (orig_rhs1),
8404 COMPOUND_EXPR, orig_rhs1, stmt);
8405 if (code != OMP_ATOMIC)
8407 stmt = build_min_nt_loc (EXPR_LOCATION (orig_lhs1),
8408 code, orig_lhs1, stmt);
8409 OMP_ATOMIC_SEQ_CST (stmt) = seq_cst;
8410 stmt = build2 (MODIFY_EXPR, void_type_node, orig_v, stmt);
8413 stmt = build2 (OMP_ATOMIC, void_type_node, integer_zero_node, stmt);
8414 OMP_ATOMIC_SEQ_CST (stmt) = seq_cst;
8416 finish_expr_stmt (stmt);
8419 void
8420 finish_omp_barrier (void)
8422 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_BARRIER);
8423 vec<tree, va_gc> *vec = make_tree_vector ();
8424 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
8425 release_tree_vector (vec);
8426 finish_expr_stmt (stmt);
8429 void
8430 finish_omp_flush (void)
8432 tree fn = builtin_decl_explicit (BUILT_IN_SYNC_SYNCHRONIZE);
8433 vec<tree, va_gc> *vec = make_tree_vector ();
8434 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
8435 release_tree_vector (vec);
8436 finish_expr_stmt (stmt);
8439 void
8440 finish_omp_taskwait (void)
8442 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_TASKWAIT);
8443 vec<tree, va_gc> *vec = make_tree_vector ();
8444 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
8445 release_tree_vector (vec);
8446 finish_expr_stmt (stmt);
8449 void
8450 finish_omp_taskyield (void)
8452 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_TASKYIELD);
8453 vec<tree, va_gc> *vec = make_tree_vector ();
8454 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
8455 release_tree_vector (vec);
8456 finish_expr_stmt (stmt);
8459 void
8460 finish_omp_cancel (tree clauses)
8462 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
8463 int mask = 0;
8464 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
8465 mask = 1;
8466 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
8467 mask = 2;
8468 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
8469 mask = 4;
8470 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
8471 mask = 8;
8472 else
8474 error ("%<#pragma omp cancel%> must specify one of "
8475 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> clauses");
8476 return;
8478 vec<tree, va_gc> *vec = make_tree_vector ();
8479 tree ifc = omp_find_clause (clauses, OMP_CLAUSE_IF);
8480 if (ifc != NULL_TREE)
8482 tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
8483 ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
8484 boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
8485 build_zero_cst (type));
8487 else
8488 ifc = boolean_true_node;
8489 vec->quick_push (build_int_cst (integer_type_node, mask));
8490 vec->quick_push (ifc);
8491 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
8492 release_tree_vector (vec);
8493 finish_expr_stmt (stmt);
8496 void
8497 finish_omp_cancellation_point (tree clauses)
8499 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
8500 int mask = 0;
8501 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
8502 mask = 1;
8503 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
8504 mask = 2;
8505 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
8506 mask = 4;
8507 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
8508 mask = 8;
8509 else
8511 error ("%<#pragma omp cancellation point%> must specify one of "
8512 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> clauses");
8513 return;
8515 vec<tree, va_gc> *vec
8516 = make_tree_vector_single (build_int_cst (integer_type_node, mask));
8517 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
8518 release_tree_vector (vec);
8519 finish_expr_stmt (stmt);
8522 /* Begin a __transaction_atomic or __transaction_relaxed statement.
8523 If PCOMPOUND is non-null, this is for a function-transaction-block, and we
8524 should create an extra compound stmt. */
8526 tree
8527 begin_transaction_stmt (location_t loc, tree *pcompound, int flags)
8529 tree r;
8531 if (pcompound)
8532 *pcompound = begin_compound_stmt (0);
8534 r = build_stmt (loc, TRANSACTION_EXPR, NULL_TREE);
8536 /* Only add the statement to the function if support enabled. */
8537 if (flag_tm)
8538 add_stmt (r);
8539 else
8540 error_at (loc, ((flags & TM_STMT_ATTR_RELAXED) != 0
8541 ? G_("%<__transaction_relaxed%> without "
8542 "transactional memory support enabled")
8543 : G_("%<__transaction_atomic%> without "
8544 "transactional memory support enabled")));
8546 TRANSACTION_EXPR_BODY (r) = push_stmt_list ();
8547 TREE_SIDE_EFFECTS (r) = 1;
8548 return r;
8551 /* End a __transaction_atomic or __transaction_relaxed statement.
8552 If COMPOUND_STMT is non-null, this is for a function-transaction-block,
8553 and we should end the compound. If NOEX is non-NULL, we wrap the body in
8554 a MUST_NOT_THROW_EXPR with NOEX as condition. */
8556 void
8557 finish_transaction_stmt (tree stmt, tree compound_stmt, int flags, tree noex)
8559 TRANSACTION_EXPR_BODY (stmt) = pop_stmt_list (TRANSACTION_EXPR_BODY (stmt));
8560 TRANSACTION_EXPR_OUTER (stmt) = (flags & TM_STMT_ATTR_OUTER) != 0;
8561 TRANSACTION_EXPR_RELAXED (stmt) = (flags & TM_STMT_ATTR_RELAXED) != 0;
8562 TRANSACTION_EXPR_IS_STMT (stmt) = 1;
8564 /* noexcept specifications are not allowed for function transactions. */
8565 gcc_assert (!(noex && compound_stmt));
8566 if (noex)
8568 tree body = build_must_not_throw_expr (TRANSACTION_EXPR_BODY (stmt),
8569 noex);
8570 protected_set_expr_location
8571 (body, EXPR_LOCATION (TRANSACTION_EXPR_BODY (stmt)));
8572 TREE_SIDE_EFFECTS (body) = 1;
8573 TRANSACTION_EXPR_BODY (stmt) = body;
8576 if (compound_stmt)
8577 finish_compound_stmt (compound_stmt);
8580 /* Build a __transaction_atomic or __transaction_relaxed expression. If
8581 NOEX is non-NULL, we wrap the body in a MUST_NOT_THROW_EXPR with NOEX as
8582 condition. */
8584 tree
8585 build_transaction_expr (location_t loc, tree expr, int flags, tree noex)
8587 tree ret;
8588 if (noex)
8590 expr = build_must_not_throw_expr (expr, noex);
8591 protected_set_expr_location (expr, loc);
8592 TREE_SIDE_EFFECTS (expr) = 1;
8594 ret = build1 (TRANSACTION_EXPR, TREE_TYPE (expr), expr);
8595 if (flags & TM_STMT_ATTR_RELAXED)
8596 TRANSACTION_EXPR_RELAXED (ret) = 1;
8597 TREE_SIDE_EFFECTS (ret) = 1;
8598 SET_EXPR_LOCATION (ret, loc);
8599 return ret;
8602 void
8603 init_cp_semantics (void)
8607 /* Build a STATIC_ASSERT for a static assertion with the condition
8608 CONDITION and the message text MESSAGE. LOCATION is the location
8609 of the static assertion in the source code. When MEMBER_P, this
8610 static assertion is a member of a class. */
8611 void
8612 finish_static_assert (tree condition, tree message, location_t location,
8613 bool member_p)
8615 tsubst_flags_t complain = tf_warning_or_error;
8617 if (message == NULL_TREE
8618 || message == error_mark_node
8619 || condition == NULL_TREE
8620 || condition == error_mark_node)
8621 return;
8623 if (check_for_bare_parameter_packs (condition))
8624 condition = error_mark_node;
8626 if (type_dependent_expression_p (condition)
8627 || value_dependent_expression_p (condition))
8629 /* We're in a template; build a STATIC_ASSERT and put it in
8630 the right place. */
8631 tree assertion;
8633 assertion = make_node (STATIC_ASSERT);
8634 STATIC_ASSERT_CONDITION (assertion) = condition;
8635 STATIC_ASSERT_MESSAGE (assertion) = message;
8636 STATIC_ASSERT_SOURCE_LOCATION (assertion) = location;
8638 if (member_p)
8639 maybe_add_class_template_decl_list (current_class_type,
8640 assertion,
8641 /*friend_p=*/0);
8642 else
8643 add_stmt (assertion);
8645 return;
8648 /* Fold the expression and convert it to a boolean value. */
8649 condition = perform_implicit_conversion_flags (boolean_type_node, condition,
8650 complain, LOOKUP_NORMAL);
8651 condition = fold_non_dependent_expr (condition);
8653 if (TREE_CODE (condition) == INTEGER_CST && !integer_zerop (condition))
8654 /* Do nothing; the condition is satisfied. */
8656 else
8658 location_t saved_loc = input_location;
8660 input_location = location;
8661 if (TREE_CODE (condition) == INTEGER_CST
8662 && integer_zerop (condition))
8664 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
8665 (TREE_TYPE (TREE_TYPE (message))));
8666 int len = TREE_STRING_LENGTH (message) / sz - 1;
8667 /* Report the error. */
8668 if (len == 0)
8669 error ("static assertion failed");
8670 else
8671 error ("static assertion failed: %s",
8672 TREE_STRING_POINTER (message));
8674 else if (condition && condition != error_mark_node)
8676 error ("non-constant condition for static assertion");
8677 if (require_rvalue_constant_expression (condition))
8678 cxx_constant_value (condition);
8680 input_location = saved_loc;
8684 /* Implements the C++0x decltype keyword. Returns the type of EXPR,
8685 suitable for use as a type-specifier.
8687 ID_EXPRESSION_OR_MEMBER_ACCESS_P is true when EXPR was parsed as an
8688 id-expression or a class member access, FALSE when it was parsed as
8689 a full expression. */
8691 tree
8692 finish_decltype_type (tree expr, bool id_expression_or_member_access_p,
8693 tsubst_flags_t complain)
8695 tree type = NULL_TREE;
8697 if (!expr || error_operand_p (expr))
8698 return error_mark_node;
8700 if (TYPE_P (expr)
8701 || TREE_CODE (expr) == TYPE_DECL
8702 || (TREE_CODE (expr) == BIT_NOT_EXPR
8703 && TYPE_P (TREE_OPERAND (expr, 0))))
8705 if (complain & tf_error)
8706 error ("argument to decltype must be an expression");
8707 return error_mark_node;
8710 /* Depending on the resolution of DR 1172, we may later need to distinguish
8711 instantiation-dependent but not type-dependent expressions so that, say,
8712 A<decltype(sizeof(T))>::U doesn't require 'typename'. */
8713 if (instantiation_dependent_uneval_expression_p (expr))
8715 type = cxx_make_type (DECLTYPE_TYPE);
8716 DECLTYPE_TYPE_EXPR (type) = expr;
8717 DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (type)
8718 = id_expression_or_member_access_p;
8719 SET_TYPE_STRUCTURAL_EQUALITY (type);
8721 return type;
8724 /* The type denoted by decltype(e) is defined as follows: */
8726 expr = resolve_nondeduced_context (expr, complain);
8728 if (invalid_nonstatic_memfn_p (input_location, expr, complain))
8729 return error_mark_node;
8731 if (type_unknown_p (expr))
8733 if (complain & tf_error)
8734 error ("decltype cannot resolve address of overloaded function");
8735 return error_mark_node;
8738 /* To get the size of a static data member declared as an array of
8739 unknown bound, we need to instantiate it. */
8740 if (VAR_P (expr)
8741 && VAR_HAD_UNKNOWN_BOUND (expr)
8742 && DECL_TEMPLATE_INSTANTIATION (expr))
8743 instantiate_decl (expr, /*defer_ok*/true, /*expl_inst_mem*/false);
8745 if (id_expression_or_member_access_p)
8747 /* If e is an id-expression or a class member access (5.2.5
8748 [expr.ref]), decltype(e) is defined as the type of the entity
8749 named by e. If there is no such entity, or e names a set of
8750 overloaded functions, the program is ill-formed. */
8751 if (identifier_p (expr))
8752 expr = lookup_name (expr);
8754 if (INDIRECT_REF_P (expr))
8755 /* This can happen when the expression is, e.g., "a.b". Just
8756 look at the underlying operand. */
8757 expr = TREE_OPERAND (expr, 0);
8759 if (TREE_CODE (expr) == OFFSET_REF
8760 || TREE_CODE (expr) == MEMBER_REF
8761 || TREE_CODE (expr) == SCOPE_REF)
8762 /* We're only interested in the field itself. If it is a
8763 BASELINK, we will need to see through it in the next
8764 step. */
8765 expr = TREE_OPERAND (expr, 1);
8767 if (BASELINK_P (expr))
8768 /* See through BASELINK nodes to the underlying function. */
8769 expr = BASELINK_FUNCTIONS (expr);
8771 /* decltype of a decomposition name drops references in the tuple case
8772 (unlike decltype of a normal variable) and keeps cv-qualifiers from
8773 the containing object in the other cases (unlike decltype of a member
8774 access expression). */
8775 if (DECL_DECOMPOSITION_P (expr))
8777 if (DECL_HAS_VALUE_EXPR_P (expr))
8778 /* Expr is an array or struct subobject proxy, handle
8779 bit-fields properly. */
8780 return unlowered_expr_type (expr);
8781 else
8782 /* Expr is a reference variable for the tuple case. */
8783 return lookup_decomp_type (expr);
8786 switch (TREE_CODE (expr))
8788 case FIELD_DECL:
8789 if (DECL_BIT_FIELD_TYPE (expr))
8791 type = DECL_BIT_FIELD_TYPE (expr);
8792 break;
8794 /* Fall through for fields that aren't bitfields. */
8795 gcc_fallthrough ();
8797 case FUNCTION_DECL:
8798 case VAR_DECL:
8799 case CONST_DECL:
8800 case PARM_DECL:
8801 case RESULT_DECL:
8802 case TEMPLATE_PARM_INDEX:
8803 expr = mark_type_use (expr);
8804 type = TREE_TYPE (expr);
8805 break;
8807 case ERROR_MARK:
8808 type = error_mark_node;
8809 break;
8811 case COMPONENT_REF:
8812 case COMPOUND_EXPR:
8813 mark_type_use (expr);
8814 type = is_bitfield_expr_with_lowered_type (expr);
8815 if (!type)
8816 type = TREE_TYPE (TREE_OPERAND (expr, 1));
8817 break;
8819 case BIT_FIELD_REF:
8820 gcc_unreachable ();
8822 case INTEGER_CST:
8823 case PTRMEM_CST:
8824 /* We can get here when the id-expression refers to an
8825 enumerator or non-type template parameter. */
8826 type = TREE_TYPE (expr);
8827 break;
8829 default:
8830 /* Handle instantiated template non-type arguments. */
8831 type = TREE_TYPE (expr);
8832 break;
8835 else
8837 /* Within a lambda-expression:
8839 Every occurrence of decltype((x)) where x is a possibly
8840 parenthesized id-expression that names an entity of
8841 automatic storage duration is treated as if x were
8842 transformed into an access to a corresponding data member
8843 of the closure type that would have been declared if x
8844 were a use of the denoted entity. */
8845 if (outer_automatic_var_p (expr)
8846 && current_function_decl
8847 && LAMBDA_FUNCTION_P (current_function_decl))
8848 type = capture_decltype (expr);
8849 else if (error_operand_p (expr))
8850 type = error_mark_node;
8851 else if (expr == current_class_ptr)
8852 /* If the expression is just "this", we want the
8853 cv-unqualified pointer for the "this" type. */
8854 type = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
8855 else
8857 /* Otherwise, where T is the type of e, if e is an lvalue,
8858 decltype(e) is defined as T&; if an xvalue, T&&; otherwise, T. */
8859 cp_lvalue_kind clk = lvalue_kind (expr);
8860 type = unlowered_expr_type (expr);
8861 gcc_assert (TREE_CODE (type) != REFERENCE_TYPE);
8863 /* For vector types, pick a non-opaque variant. */
8864 if (VECTOR_TYPE_P (type))
8865 type = strip_typedefs (type);
8867 if (clk != clk_none && !(clk & clk_class))
8868 type = cp_build_reference_type (type, (clk & clk_rvalueref));
8872 return type;
8875 /* Called from trait_expr_value to evaluate either __has_nothrow_assign or
8876 __has_nothrow_copy, depending on assign_p. Returns true iff all
8877 the copy {ctor,assign} fns are nothrow. */
8879 static bool
8880 classtype_has_nothrow_assign_or_copy_p (tree type, bool assign_p)
8882 tree fns = NULL_TREE;
8884 if (assign_p || TYPE_HAS_COPY_CTOR (type))
8885 fns = get_class_binding (type, assign_p ? assign_op_identifier
8886 : ctor_identifier);
8888 bool saw_copy = false;
8889 for (ovl_iterator iter (fns); iter; ++iter)
8891 tree fn = *iter;
8893 if (copy_fn_p (fn) > 0)
8895 saw_copy = true;
8896 maybe_instantiate_noexcept (fn);
8897 if (!TYPE_NOTHROW_P (TREE_TYPE (fn)))
8898 return false;
8902 return saw_copy;
8905 /* Actually evaluates the trait. */
8907 static bool
8908 trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
8910 enum tree_code type_code1;
8911 tree t;
8913 type_code1 = TREE_CODE (type1);
8915 switch (kind)
8917 case CPTK_HAS_NOTHROW_ASSIGN:
8918 type1 = strip_array_types (type1);
8919 return (!CP_TYPE_CONST_P (type1) && type_code1 != REFERENCE_TYPE
8920 && (trait_expr_value (CPTK_HAS_TRIVIAL_ASSIGN, type1, type2)
8921 || (CLASS_TYPE_P (type1)
8922 && classtype_has_nothrow_assign_or_copy_p (type1,
8923 true))));
8925 case CPTK_HAS_TRIVIAL_ASSIGN:
8926 /* ??? The standard seems to be missing the "or array of such a class
8927 type" wording for this trait. */
8928 type1 = strip_array_types (type1);
8929 return (!CP_TYPE_CONST_P (type1) && type_code1 != REFERENCE_TYPE
8930 && (trivial_type_p (type1)
8931 || (CLASS_TYPE_P (type1)
8932 && TYPE_HAS_TRIVIAL_COPY_ASSIGN (type1))));
8934 case CPTK_HAS_NOTHROW_CONSTRUCTOR:
8935 type1 = strip_array_types (type1);
8936 return (trait_expr_value (CPTK_HAS_TRIVIAL_CONSTRUCTOR, type1, type2)
8937 || (CLASS_TYPE_P (type1)
8938 && (t = locate_ctor (type1))
8939 && (maybe_instantiate_noexcept (t),
8940 TYPE_NOTHROW_P (TREE_TYPE (t)))));
8942 case CPTK_HAS_TRIVIAL_CONSTRUCTOR:
8943 type1 = strip_array_types (type1);
8944 return (trivial_type_p (type1)
8945 || (CLASS_TYPE_P (type1) && TYPE_HAS_TRIVIAL_DFLT (type1)));
8947 case CPTK_HAS_NOTHROW_COPY:
8948 type1 = strip_array_types (type1);
8949 return (trait_expr_value (CPTK_HAS_TRIVIAL_COPY, type1, type2)
8950 || (CLASS_TYPE_P (type1)
8951 && classtype_has_nothrow_assign_or_copy_p (type1, false)));
8953 case CPTK_HAS_TRIVIAL_COPY:
8954 /* ??? The standard seems to be missing the "or array of such a class
8955 type" wording for this trait. */
8956 type1 = strip_array_types (type1);
8957 return (trivial_type_p (type1) || type_code1 == REFERENCE_TYPE
8958 || (CLASS_TYPE_P (type1) && TYPE_HAS_TRIVIAL_COPY_CTOR (type1)));
8960 case CPTK_HAS_TRIVIAL_DESTRUCTOR:
8961 type1 = strip_array_types (type1);
8962 return (trivial_type_p (type1) || type_code1 == REFERENCE_TYPE
8963 || (CLASS_TYPE_P (type1)
8964 && TYPE_HAS_TRIVIAL_DESTRUCTOR (type1)));
8966 case CPTK_HAS_VIRTUAL_DESTRUCTOR:
8967 return type_has_virtual_destructor (type1);
8969 case CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS:
8970 return type_has_unique_obj_representations (type1);
8972 case CPTK_IS_ABSTRACT:
8973 return ABSTRACT_CLASS_TYPE_P (type1);
8975 case CPTK_IS_AGGREGATE:
8976 return CP_AGGREGATE_TYPE_P (type1);
8978 case CPTK_IS_BASE_OF:
8979 return (NON_UNION_CLASS_TYPE_P (type1) && NON_UNION_CLASS_TYPE_P (type2)
8980 && (same_type_ignoring_top_level_qualifiers_p (type1, type2)
8981 || DERIVED_FROM_P (type1, type2)));
8983 case CPTK_IS_CLASS:
8984 return NON_UNION_CLASS_TYPE_P (type1);
8986 case CPTK_IS_EMPTY:
8987 return NON_UNION_CLASS_TYPE_P (type1) && CLASSTYPE_EMPTY_P (type1);
8989 case CPTK_IS_ENUM:
8990 return type_code1 == ENUMERAL_TYPE;
8992 case CPTK_IS_FINAL:
8993 return CLASS_TYPE_P (type1) && CLASSTYPE_FINAL (type1);
8995 case CPTK_IS_LITERAL_TYPE:
8996 return literal_type_p (type1);
8998 case CPTK_IS_POD:
8999 return pod_type_p (type1);
9001 case CPTK_IS_POLYMORPHIC:
9002 return CLASS_TYPE_P (type1) && TYPE_POLYMORPHIC_P (type1);
9004 case CPTK_IS_SAME_AS:
9005 return same_type_p (type1, type2);
9007 case CPTK_IS_STD_LAYOUT:
9008 return std_layout_type_p (type1);
9010 case CPTK_IS_TRIVIAL:
9011 return trivial_type_p (type1);
9013 case CPTK_IS_TRIVIALLY_ASSIGNABLE:
9014 return is_trivially_xible (MODIFY_EXPR, type1, type2);
9016 case CPTK_IS_TRIVIALLY_CONSTRUCTIBLE:
9017 return is_trivially_xible (INIT_EXPR, type1, type2);
9019 case CPTK_IS_TRIVIALLY_COPYABLE:
9020 return trivially_copyable_p (type1);
9022 case CPTK_IS_UNION:
9023 return type_code1 == UNION_TYPE;
9025 case CPTK_IS_ASSIGNABLE:
9026 return is_xible (MODIFY_EXPR, type1, type2);
9028 case CPTK_IS_CONSTRUCTIBLE:
9029 return is_xible (INIT_EXPR, type1, type2);
9031 default:
9032 gcc_unreachable ();
9033 return false;
9037 /* If TYPE is an array of unknown bound, or (possibly cv-qualified)
9038 void, or a complete type, returns true, otherwise false. */
9040 static bool
9041 check_trait_type (tree type)
9043 if (type == NULL_TREE)
9044 return true;
9046 if (TREE_CODE (type) == TREE_LIST)
9047 return (check_trait_type (TREE_VALUE (type))
9048 && check_trait_type (TREE_CHAIN (type)));
9050 if (TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type)
9051 && COMPLETE_TYPE_P (TREE_TYPE (type)))
9052 return true;
9054 if (VOID_TYPE_P (type))
9055 return true;
9057 return !!complete_type_or_else (strip_array_types (type), NULL_TREE);
9060 /* Process a trait expression. */
9062 tree
9063 finish_trait_expr (cp_trait_kind kind, tree type1, tree type2)
9065 if (type1 == error_mark_node
9066 || type2 == error_mark_node)
9067 return error_mark_node;
9069 if (processing_template_decl)
9071 tree trait_expr = make_node (TRAIT_EXPR);
9072 TREE_TYPE (trait_expr) = boolean_type_node;
9073 TRAIT_EXPR_TYPE1 (trait_expr) = type1;
9074 TRAIT_EXPR_TYPE2 (trait_expr) = type2;
9075 TRAIT_EXPR_KIND (trait_expr) = kind;
9076 return trait_expr;
9079 switch (kind)
9081 case CPTK_HAS_NOTHROW_ASSIGN:
9082 case CPTK_HAS_TRIVIAL_ASSIGN:
9083 case CPTK_HAS_NOTHROW_CONSTRUCTOR:
9084 case CPTK_HAS_TRIVIAL_CONSTRUCTOR:
9085 case CPTK_HAS_NOTHROW_COPY:
9086 case CPTK_HAS_TRIVIAL_COPY:
9087 case CPTK_HAS_TRIVIAL_DESTRUCTOR:
9088 case CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS:
9089 case CPTK_HAS_VIRTUAL_DESTRUCTOR:
9090 case CPTK_IS_ABSTRACT:
9091 case CPTK_IS_AGGREGATE:
9092 case CPTK_IS_EMPTY:
9093 case CPTK_IS_FINAL:
9094 case CPTK_IS_LITERAL_TYPE:
9095 case CPTK_IS_POD:
9096 case CPTK_IS_POLYMORPHIC:
9097 case CPTK_IS_STD_LAYOUT:
9098 case CPTK_IS_TRIVIAL:
9099 case CPTK_IS_TRIVIALLY_COPYABLE:
9100 if (!check_trait_type (type1))
9101 return error_mark_node;
9102 break;
9104 case CPTK_IS_ASSIGNABLE:
9105 case CPTK_IS_CONSTRUCTIBLE:
9106 break;
9108 case CPTK_IS_TRIVIALLY_ASSIGNABLE:
9109 case CPTK_IS_TRIVIALLY_CONSTRUCTIBLE:
9110 if (!check_trait_type (type1)
9111 || !check_trait_type (type2))
9112 return error_mark_node;
9113 break;
9115 case CPTK_IS_BASE_OF:
9116 if (NON_UNION_CLASS_TYPE_P (type1) && NON_UNION_CLASS_TYPE_P (type2)
9117 && !same_type_ignoring_top_level_qualifiers_p (type1, type2)
9118 && !complete_type_or_else (type2, NULL_TREE))
9119 /* We already issued an error. */
9120 return error_mark_node;
9121 break;
9123 case CPTK_IS_CLASS:
9124 case CPTK_IS_ENUM:
9125 case CPTK_IS_UNION:
9126 case CPTK_IS_SAME_AS:
9127 break;
9129 default:
9130 gcc_unreachable ();
9133 return (trait_expr_value (kind, type1, type2)
9134 ? boolean_true_node : boolean_false_node);
9137 /* Do-nothing variants of functions to handle pragma FLOAT_CONST_DECIMAL64,
9138 which is ignored for C++. */
9140 void
9141 set_float_const_decimal64 (void)
9145 void
9146 clear_float_const_decimal64 (void)
9150 bool
9151 float_const_decimal64_p (void)
9153 return 0;
9157 /* Return true if T designates the implied `this' parameter. */
9159 bool
9160 is_this_parameter (tree t)
9162 if (!DECL_P (t) || DECL_NAME (t) != this_identifier)
9163 return false;
9164 gcc_assert (TREE_CODE (t) == PARM_DECL || is_capture_proxy (t)
9165 || (cp_binding_oracle && TREE_CODE (t) == VAR_DECL));
9166 return true;
9169 /* Insert the deduced return type for an auto function. */
9171 void
9172 apply_deduced_return_type (tree fco, tree return_type)
9174 tree result;
9176 if (return_type == error_mark_node)
9177 return;
9179 if (DECL_CONV_FN_P (fco))
9180 DECL_NAME (fco) = make_conv_op_name (return_type);
9182 TREE_TYPE (fco) = change_return_type (return_type, TREE_TYPE (fco));
9184 result = DECL_RESULT (fco);
9185 if (result == NULL_TREE)
9186 return;
9187 if (TREE_TYPE (result) == return_type)
9188 return;
9190 if (!processing_template_decl && !VOID_TYPE_P (return_type)
9191 && !complete_type_or_else (return_type, NULL_TREE))
9192 return;
9194 /* We already have a DECL_RESULT from start_preparsed_function.
9195 Now we need to redo the work it and allocate_struct_function
9196 did to reflect the new type. */
9197 gcc_assert (current_function_decl == fco);
9198 result = build_decl (input_location, RESULT_DECL, NULL_TREE,
9199 TYPE_MAIN_VARIANT (return_type));
9200 DECL_ARTIFICIAL (result) = 1;
9201 DECL_IGNORED_P (result) = 1;
9202 cp_apply_type_quals_to_decl (cp_type_quals (return_type),
9203 result);
9205 DECL_RESULT (fco) = result;
9207 if (!processing_template_decl)
9209 bool aggr = aggregate_value_p (result, fco);
9210 #ifdef PCC_STATIC_STRUCT_RETURN
9211 cfun->returns_pcc_struct = aggr;
9212 #endif
9213 cfun->returns_struct = aggr;
9218 /* DECL is a local variable or parameter from the surrounding scope of a
9219 lambda-expression. Returns the decltype for a use of the capture field
9220 for DECL even if it hasn't been captured yet. */
9222 static tree
9223 capture_decltype (tree decl)
9225 tree lam = CLASSTYPE_LAMBDA_EXPR (DECL_CONTEXT (current_function_decl));
9226 /* FIXME do lookup instead of list walk? */
9227 tree cap = value_member (decl, LAMBDA_EXPR_CAPTURE_LIST (lam));
9228 tree type;
9230 if (cap)
9231 type = TREE_TYPE (TREE_PURPOSE (cap));
9232 else
9233 switch (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lam))
9235 case CPLD_NONE:
9236 error ("%qD is not captured", decl);
9237 return error_mark_node;
9239 case CPLD_COPY:
9240 type = TREE_TYPE (decl);
9241 if (TREE_CODE (type) == REFERENCE_TYPE
9242 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
9243 type = TREE_TYPE (type);
9244 break;
9246 case CPLD_REFERENCE:
9247 type = TREE_TYPE (decl);
9248 if (TREE_CODE (type) != REFERENCE_TYPE)
9249 type = build_reference_type (TREE_TYPE (decl));
9250 break;
9252 default:
9253 gcc_unreachable ();
9256 if (TREE_CODE (type) != REFERENCE_TYPE)
9258 if (!LAMBDA_EXPR_MUTABLE_P (lam))
9259 type = cp_build_qualified_type (type, (cp_type_quals (type)
9260 |TYPE_QUAL_CONST));
9261 type = build_reference_type (type);
9263 return type;
9266 /* Build a unary fold expression of EXPR over OP. If IS_RIGHT is true,
9267 this is a right unary fold. Otherwise it is a left unary fold. */
9269 static tree
9270 finish_unary_fold_expr (tree expr, int op, tree_code dir)
9272 // Build a pack expansion (assuming expr has pack type).
9273 if (!uses_parameter_packs (expr))
9275 error_at (location_of (expr), "operand of fold expression has no "
9276 "unexpanded parameter packs");
9277 return error_mark_node;
9279 tree pack = make_pack_expansion (expr);
9281 // Build the fold expression.
9282 tree code = build_int_cstu (integer_type_node, abs (op));
9283 tree fold = build_min_nt_loc (UNKNOWN_LOCATION, dir, code, pack);
9284 FOLD_EXPR_MODIFY_P (fold) = (op < 0);
9285 return fold;
9288 tree
9289 finish_left_unary_fold_expr (tree expr, int op)
9291 return finish_unary_fold_expr (expr, op, UNARY_LEFT_FOLD_EXPR);
9294 tree
9295 finish_right_unary_fold_expr (tree expr, int op)
9297 return finish_unary_fold_expr (expr, op, UNARY_RIGHT_FOLD_EXPR);
9300 /* Build a binary fold expression over EXPR1 and EXPR2. The
9301 associativity of the fold is determined by EXPR1 and EXPR2 (whichever
9302 has an unexpanded parameter pack). */
9304 tree
9305 finish_binary_fold_expr (tree pack, tree init, int op, tree_code dir)
9307 pack = make_pack_expansion (pack);
9308 tree code = build_int_cstu (integer_type_node, abs (op));
9309 tree fold = build_min_nt_loc (UNKNOWN_LOCATION, dir, code, pack, init);
9310 FOLD_EXPR_MODIFY_P (fold) = (op < 0);
9311 return fold;
9314 tree
9315 finish_binary_fold_expr (tree expr1, tree expr2, int op)
9317 // Determine which expr has an unexpanded parameter pack and
9318 // set the pack and initial term.
9319 bool pack1 = uses_parameter_packs (expr1);
9320 bool pack2 = uses_parameter_packs (expr2);
9321 if (pack1 && !pack2)
9322 return finish_binary_fold_expr (expr1, expr2, op, BINARY_RIGHT_FOLD_EXPR);
9323 else if (pack2 && !pack1)
9324 return finish_binary_fold_expr (expr2, expr1, op, BINARY_LEFT_FOLD_EXPR);
9325 else
9327 if (pack1)
9328 error ("both arguments in binary fold have unexpanded parameter packs");
9329 else
9330 error ("no unexpanded parameter packs in binary fold");
9332 return error_mark_node;
9335 /* Finish __builtin_launder (arg). */
9337 tree
9338 finish_builtin_launder (location_t loc, tree arg, tsubst_flags_t complain)
9340 tree orig_arg = arg;
9341 if (!type_dependent_expression_p (arg))
9342 arg = decay_conversion (arg, complain);
9343 if (error_operand_p (arg))
9344 return error_mark_node;
9345 if (!type_dependent_expression_p (arg)
9346 && TREE_CODE (TREE_TYPE (arg)) != POINTER_TYPE)
9348 error_at (loc, "non-pointer argument to %<__builtin_launder%>");
9349 return error_mark_node;
9351 if (processing_template_decl)
9352 arg = orig_arg;
9353 return build_call_expr_internal_loc (loc, IFN_LAUNDER,
9354 TREE_TYPE (arg), 1, arg);
9357 #include "gt-cp-semantics.h"