gcc/ChangeLog
[official-gcc.git] / gcc / cp / semantics.c
blob2a69ab0863fa882d87bc27dfce38946d8b59e7c3
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-2015 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 "tm.h"
30 #include "alias.h"
31 #include "tree.h"
32 #include "stmt.h"
33 #include "varasm.h"
34 #include "stor-layout.h"
35 #include "stringpool.h"
36 #include "cp-tree.h"
37 #include "c-family/c-common.h"
38 #include "c-family/c-objc.h"
39 #include "tree-inline.h"
40 #include "intl.h"
41 #include "toplev.h"
42 #include "flags.h"
43 #include "timevar.h"
44 #include "diagnostic.h"
45 #include "hard-reg-set.h"
46 #include "function.h"
47 #include "cgraph.h"
48 #include "tree-iterator.h"
49 #include "target.h"
50 #include "gimplify.h"
51 #include "bitmap.h"
52 #include "omp-low.h"
53 #include "builtins.h"
54 #include "convert.h"
55 #include "gomp-constants.h"
57 /* There routines provide a modular interface to perform many parsing
58 operations. They may therefore be used during actual parsing, or
59 during template instantiation, which may be regarded as a
60 degenerate form of parsing. */
62 static tree maybe_convert_cond (tree);
63 static tree finalize_nrv_r (tree *, int *, void *);
64 static tree capture_decltype (tree);
67 /* Deferred Access Checking Overview
68 ---------------------------------
70 Most C++ expressions and declarations require access checking
71 to be performed during parsing. However, in several cases,
72 this has to be treated differently.
74 For member declarations, access checking has to be deferred
75 until more information about the declaration is known. For
76 example:
78 class A {
79 typedef int X;
80 public:
81 X f();
84 A::X A::f();
85 A::X g();
87 When we are parsing the function return type `A::X', we don't
88 really know if this is allowed until we parse the function name.
90 Furthermore, some contexts require that access checking is
91 never performed at all. These include class heads, and template
92 instantiations.
94 Typical use of access checking functions is described here:
96 1. When we enter a context that requires certain access checking
97 mode, the function `push_deferring_access_checks' is called with
98 DEFERRING argument specifying the desired mode. Access checking
99 may be performed immediately (dk_no_deferred), deferred
100 (dk_deferred), or not performed (dk_no_check).
102 2. When a declaration such as a type, or a variable, is encountered,
103 the function `perform_or_defer_access_check' is called. It
104 maintains a vector of all deferred checks.
106 3. The global `current_class_type' or `current_function_decl' is then
107 setup by the parser. `enforce_access' relies on these information
108 to check access.
110 4. Upon exiting the context mentioned in step 1,
111 `perform_deferred_access_checks' is called to check all declaration
112 stored in the vector. `pop_deferring_access_checks' is then
113 called to restore the previous access checking mode.
115 In case of parsing error, we simply call `pop_deferring_access_checks'
116 without `perform_deferred_access_checks'. */
118 struct GTY(()) deferred_access {
119 /* A vector representing name-lookups for which we have deferred
120 checking access controls. We cannot check the accessibility of
121 names used in a decl-specifier-seq until we know what is being
122 declared because code like:
124 class A {
125 class B {};
126 B* f();
129 A::B* A::f() { return 0; }
131 is valid, even though `A::B' is not generally accessible. */
132 vec<deferred_access_check, va_gc> * GTY(()) deferred_access_checks;
134 /* The current mode of access checks. */
135 enum deferring_kind deferring_access_checks_kind;
139 /* Data for deferred access checking. */
140 static GTY(()) vec<deferred_access, va_gc> *deferred_access_stack;
141 static GTY(()) unsigned deferred_access_no_check;
143 /* Save the current deferred access states and start deferred
144 access checking iff DEFER_P is true. */
146 void
147 push_deferring_access_checks (deferring_kind deferring)
149 /* For context like template instantiation, access checking
150 disabling applies to all nested context. */
151 if (deferred_access_no_check || deferring == dk_no_check)
152 deferred_access_no_check++;
153 else
155 deferred_access e = {NULL, deferring};
156 vec_safe_push (deferred_access_stack, e);
160 /* Save the current deferred access states and start deferred access
161 checking, continuing the set of deferred checks in CHECKS. */
163 void
164 reopen_deferring_access_checks (vec<deferred_access_check, va_gc> * checks)
166 push_deferring_access_checks (dk_deferred);
167 if (!deferred_access_no_check)
168 deferred_access_stack->last().deferred_access_checks = checks;
171 /* Resume deferring access checks again after we stopped doing
172 this previously. */
174 void
175 resume_deferring_access_checks (void)
177 if (!deferred_access_no_check)
178 deferred_access_stack->last().deferring_access_checks_kind = dk_deferred;
181 /* Stop deferring access checks. */
183 void
184 stop_deferring_access_checks (void)
186 if (!deferred_access_no_check)
187 deferred_access_stack->last().deferring_access_checks_kind = dk_no_deferred;
190 /* Discard the current deferred access checks and restore the
191 previous states. */
193 void
194 pop_deferring_access_checks (void)
196 if (deferred_access_no_check)
197 deferred_access_no_check--;
198 else
199 deferred_access_stack->pop ();
202 /* Returns a TREE_LIST representing the deferred checks.
203 The TREE_PURPOSE of each node is the type through which the
204 access occurred; the TREE_VALUE is the declaration named.
207 vec<deferred_access_check, va_gc> *
208 get_deferred_access_checks (void)
210 if (deferred_access_no_check)
211 return NULL;
212 else
213 return (deferred_access_stack->last().deferred_access_checks);
216 /* Take current deferred checks and combine with the
217 previous states if we also defer checks previously.
218 Otherwise perform checks now. */
220 void
221 pop_to_parent_deferring_access_checks (void)
223 if (deferred_access_no_check)
224 deferred_access_no_check--;
225 else
227 vec<deferred_access_check, va_gc> *checks;
228 deferred_access *ptr;
230 checks = (deferred_access_stack->last ().deferred_access_checks);
232 deferred_access_stack->pop ();
233 ptr = &deferred_access_stack->last ();
234 if (ptr->deferring_access_checks_kind == dk_no_deferred)
236 /* Check access. */
237 perform_access_checks (checks, tf_warning_or_error);
239 else
241 /* Merge with parent. */
242 int i, j;
243 deferred_access_check *chk, *probe;
245 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
247 FOR_EACH_VEC_SAFE_ELT (ptr->deferred_access_checks, j, probe)
249 if (probe->binfo == chk->binfo &&
250 probe->decl == chk->decl &&
251 probe->diag_decl == chk->diag_decl)
252 goto found;
254 /* Insert into parent's checks. */
255 vec_safe_push (ptr->deferred_access_checks, *chk);
256 found:;
262 /* Perform the access checks in CHECKS. The TREE_PURPOSE of each node
263 is the BINFO indicating the qualifying scope used to access the
264 DECL node stored in the TREE_VALUE of the node. If CHECKS is empty
265 or we aren't in SFINAE context or all the checks succeed return TRUE,
266 otherwise FALSE. */
268 bool
269 perform_access_checks (vec<deferred_access_check, va_gc> *checks,
270 tsubst_flags_t complain)
272 int i;
273 deferred_access_check *chk;
274 location_t loc = input_location;
275 bool ok = true;
277 if (!checks)
278 return true;
280 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
282 input_location = chk->loc;
283 ok &= enforce_access (chk->binfo, chk->decl, chk->diag_decl, complain);
286 input_location = loc;
287 return (complain & tf_error) ? true : ok;
290 /* Perform the deferred access checks.
292 After performing the checks, we still have to keep the list
293 `deferred_access_stack->deferred_access_checks' since we may want
294 to check access for them again later in a different context.
295 For example:
297 class A {
298 typedef int X;
299 static X a;
301 A::X A::a, x; // No error for `A::a', error for `x'
303 We have to perform deferred access of `A::X', first with `A::a',
304 next with `x'. Return value like perform_access_checks above. */
306 bool
307 perform_deferred_access_checks (tsubst_flags_t complain)
309 return perform_access_checks (get_deferred_access_checks (), complain);
312 /* Defer checking the accessibility of DECL, when looked up in
313 BINFO. DIAG_DECL is the declaration to use to print diagnostics.
314 Return value like perform_access_checks above. */
316 bool
317 perform_or_defer_access_check (tree binfo, tree decl, tree diag_decl,
318 tsubst_flags_t complain)
320 int i;
321 deferred_access *ptr;
322 deferred_access_check *chk;
325 /* Exit if we are in a context that no access checking is performed.
327 if (deferred_access_no_check)
328 return true;
330 gcc_assert (TREE_CODE (binfo) == TREE_BINFO);
332 ptr = &deferred_access_stack->last ();
334 /* If we are not supposed to defer access checks, just check now. */
335 if (ptr->deferring_access_checks_kind == dk_no_deferred)
337 bool ok = enforce_access (binfo, decl, diag_decl, complain);
338 return (complain & tf_error) ? true : ok;
341 /* See if we are already going to perform this check. */
342 FOR_EACH_VEC_SAFE_ELT (ptr->deferred_access_checks, i, chk)
344 if (chk->decl == decl && chk->binfo == binfo &&
345 chk->diag_decl == diag_decl)
347 return true;
350 /* If not, record the check. */
351 deferred_access_check new_access = {binfo, decl, diag_decl, input_location};
352 vec_safe_push (ptr->deferred_access_checks, new_access);
354 return true;
357 /* Returns nonzero if the current statement is a full expression,
358 i.e. temporaries created during that statement should be destroyed
359 at the end of the statement. */
362 stmts_are_full_exprs_p (void)
364 return current_stmt_tree ()->stmts_are_full_exprs_p;
367 /* T is a statement. Add it to the statement-tree. This is the C++
368 version. The C/ObjC frontends have a slightly different version of
369 this function. */
371 tree
372 add_stmt (tree t)
374 enum tree_code code = TREE_CODE (t);
376 if (EXPR_P (t) && code != LABEL_EXPR)
378 if (!EXPR_HAS_LOCATION (t))
379 SET_EXPR_LOCATION (t, input_location);
381 /* When we expand a statement-tree, we must know whether or not the
382 statements are full-expressions. We record that fact here. */
383 STMT_IS_FULL_EXPR_P (t) = stmts_are_full_exprs_p ();
386 if (code == LABEL_EXPR || code == CASE_LABEL_EXPR)
387 STATEMENT_LIST_HAS_LABEL (cur_stmt_list) = 1;
389 /* Add T to the statement-tree. Non-side-effect statements need to be
390 recorded during statement expressions. */
391 gcc_checking_assert (!stmt_list_stack->is_empty ());
392 append_to_statement_list_force (t, &cur_stmt_list);
394 return t;
397 /* Returns the stmt_tree to which statements are currently being added. */
399 stmt_tree
400 current_stmt_tree (void)
402 return (cfun
403 ? &cfun->language->base.x_stmt_tree
404 : &scope_chain->x_stmt_tree);
407 /* If statements are full expressions, wrap STMT in a CLEANUP_POINT_EXPR. */
409 static tree
410 maybe_cleanup_point_expr (tree expr)
412 if (!processing_template_decl && stmts_are_full_exprs_p ())
413 expr = fold_build_cleanup_point_expr (TREE_TYPE (expr), expr);
414 return expr;
417 /* Like maybe_cleanup_point_expr except have the type of the new expression be
418 void so we don't need to create a temporary variable to hold the inner
419 expression. The reason why we do this is because the original type might be
420 an aggregate and we cannot create a temporary variable for that type. */
422 tree
423 maybe_cleanup_point_expr_void (tree expr)
425 if (!processing_template_decl && stmts_are_full_exprs_p ())
426 expr = fold_build_cleanup_point_expr (void_type_node, expr);
427 return expr;
432 /* Create a declaration statement for the declaration given by the DECL. */
434 void
435 add_decl_expr (tree decl)
437 tree r = build_stmt (input_location, DECL_EXPR, decl);
438 if (DECL_INITIAL (decl)
439 || (DECL_SIZE (decl) && TREE_SIDE_EFFECTS (DECL_SIZE (decl))))
440 r = maybe_cleanup_point_expr_void (r);
441 add_stmt (r);
444 /* Finish a scope. */
446 tree
447 do_poplevel (tree stmt_list)
449 tree block = NULL;
451 if (stmts_are_full_exprs_p ())
452 block = poplevel (kept_level_p (), 1, 0);
454 stmt_list = pop_stmt_list (stmt_list);
456 if (!processing_template_decl)
458 stmt_list = c_build_bind_expr (input_location, block, stmt_list);
459 /* ??? See c_end_compound_stmt re statement expressions. */
462 return stmt_list;
465 /* Begin a new scope. */
467 static tree
468 do_pushlevel (scope_kind sk)
470 tree ret = push_stmt_list ();
471 if (stmts_are_full_exprs_p ())
472 begin_scope (sk, NULL);
473 return ret;
476 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
477 when the current scope is exited. EH_ONLY is true when this is not
478 meant to apply to normal control flow transfer. */
480 void
481 push_cleanup (tree decl, tree cleanup, bool eh_only)
483 tree stmt = build_stmt (input_location, CLEANUP_STMT, NULL, cleanup, decl);
484 CLEANUP_EH_ONLY (stmt) = eh_only;
485 add_stmt (stmt);
486 CLEANUP_BODY (stmt) = push_stmt_list ();
489 /* Simple infinite loop tracking for -Wreturn-type. We keep a stack of all
490 the current loops, represented by 'NULL_TREE' if we've seen a possible
491 exit, and 'error_mark_node' if not. This is currently used only to
492 suppress the warning about a function with no return statements, and
493 therefore we don't bother noting returns as possible exits. We also
494 don't bother with gotos. */
496 static void
497 begin_maybe_infinite_loop (tree cond)
499 /* Only track this while parsing a function, not during instantiation. */
500 if (!cfun || (DECL_TEMPLATE_INSTANTIATION (current_function_decl)
501 && !processing_template_decl))
502 return;
503 bool maybe_infinite = true;
504 if (cond)
506 cond = fold_non_dependent_expr (cond);
507 maybe_infinite = integer_nonzerop (cond);
509 vec_safe_push (cp_function_chain->infinite_loops,
510 maybe_infinite ? error_mark_node : NULL_TREE);
514 /* A break is a possible exit for the current loop. */
516 void
517 break_maybe_infinite_loop (void)
519 if (!cfun)
520 return;
521 cp_function_chain->infinite_loops->last() = NULL_TREE;
524 /* If we reach the end of the loop without seeing a possible exit, we have
525 an infinite loop. */
527 static void
528 end_maybe_infinite_loop (tree cond)
530 if (!cfun || (DECL_TEMPLATE_INSTANTIATION (current_function_decl)
531 && !processing_template_decl))
532 return;
533 tree current = cp_function_chain->infinite_loops->pop();
534 if (current != NULL_TREE)
536 cond = fold_non_dependent_expr (cond);
537 if (integer_nonzerop (cond))
538 current_function_infinite_loop = 1;
543 /* Begin a conditional that might contain a declaration. When generating
544 normal code, we want the declaration to appear before the statement
545 containing the conditional. When generating template code, we want the
546 conditional to be rendered as the raw DECL_EXPR. */
548 static void
549 begin_cond (tree *cond_p)
551 if (processing_template_decl)
552 *cond_p = push_stmt_list ();
555 /* Finish such a conditional. */
557 static void
558 finish_cond (tree *cond_p, tree expr)
560 if (processing_template_decl)
562 tree cond = pop_stmt_list (*cond_p);
564 if (expr == NULL_TREE)
565 /* Empty condition in 'for'. */
566 gcc_assert (empty_expr_stmt_p (cond));
567 else if (check_for_bare_parameter_packs (expr))
568 expr = error_mark_node;
569 else if (!empty_expr_stmt_p (cond))
570 expr = build2 (COMPOUND_EXPR, TREE_TYPE (expr), cond, expr);
572 *cond_p = expr;
575 /* If *COND_P specifies a conditional with a declaration, transform the
576 loop such that
577 while (A x = 42) { }
578 for (; A x = 42;) { }
579 becomes
580 while (true) { A x = 42; if (!x) break; }
581 for (;;) { A x = 42; if (!x) break; }
582 The statement list for BODY will be empty if the conditional did
583 not declare anything. */
585 static void
586 simplify_loop_decl_cond (tree *cond_p, tree body)
588 tree cond, if_stmt;
590 if (!TREE_SIDE_EFFECTS (body))
591 return;
593 cond = *cond_p;
594 *cond_p = boolean_true_node;
596 if_stmt = begin_if_stmt ();
597 cond = cp_build_unary_op (TRUTH_NOT_EXPR, cond, 0, tf_warning_or_error);
598 finish_if_stmt_cond (cond, if_stmt);
599 finish_break_stmt ();
600 finish_then_clause (if_stmt);
601 finish_if_stmt (if_stmt);
604 /* Finish a goto-statement. */
606 tree
607 finish_goto_stmt (tree destination)
609 if (identifier_p (destination))
610 destination = lookup_label (destination);
612 /* We warn about unused labels with -Wunused. That means we have to
613 mark the used labels as used. */
614 if (TREE_CODE (destination) == LABEL_DECL)
615 TREE_USED (destination) = 1;
616 else
618 if (check_no_cilk (destination,
619 "Cilk array notation cannot be used as a computed goto expression",
620 "%<_Cilk_spawn%> statement cannot be used as a computed goto expression"))
621 destination = error_mark_node;
622 destination = mark_rvalue_use (destination);
623 if (!processing_template_decl)
625 destination = cp_convert (ptr_type_node, destination,
626 tf_warning_or_error);
627 if (error_operand_p (destination))
628 return NULL_TREE;
629 destination
630 = fold_build_cleanup_point_expr (TREE_TYPE (destination),
631 destination);
635 check_goto (destination);
637 return add_stmt (build_stmt (input_location, GOTO_EXPR, destination));
640 /* COND is the condition-expression for an if, while, etc.,
641 statement. Convert it to a boolean value, if appropriate.
642 In addition, verify sequence points if -Wsequence-point is enabled. */
644 static tree
645 maybe_convert_cond (tree cond)
647 /* Empty conditions remain empty. */
648 if (!cond)
649 return NULL_TREE;
651 /* Wait until we instantiate templates before doing conversion. */
652 if (processing_template_decl)
653 return cond;
655 if (warn_sequence_point)
656 verify_sequence_points (cond);
658 /* Do the conversion. */
659 cond = convert_from_reference (cond);
661 if (TREE_CODE (cond) == MODIFY_EXPR
662 && !TREE_NO_WARNING (cond)
663 && warn_parentheses)
665 warning (OPT_Wparentheses,
666 "suggest parentheses around assignment used as truth value");
667 TREE_NO_WARNING (cond) = 1;
670 return condition_conversion (cond);
673 /* Finish an expression-statement, whose EXPRESSION is as indicated. */
675 tree
676 finish_expr_stmt (tree expr)
678 tree r = NULL_TREE;
680 if (expr != NULL_TREE)
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 (input_location, 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 begin_cond (&IF_COND (r));
722 return r;
725 /* Process the COND of an if-statement, which may be given by
726 IF_STMT. */
728 void
729 finish_if_stmt_cond (tree cond, tree if_stmt)
731 finish_cond (&IF_COND (if_stmt), maybe_convert_cond (cond));
732 add_stmt (if_stmt);
733 THEN_CLAUSE (if_stmt) = push_stmt_list ();
736 /* Finish the then-clause of an if-statement, which may be given by
737 IF_STMT. */
739 tree
740 finish_then_clause (tree if_stmt)
742 THEN_CLAUSE (if_stmt) = pop_stmt_list (THEN_CLAUSE (if_stmt));
743 return if_stmt;
746 /* Begin the else-clause of an if-statement. */
748 void
749 begin_else_clause (tree if_stmt)
751 ELSE_CLAUSE (if_stmt) = push_stmt_list ();
754 /* Finish the else-clause of an if-statement, which may be given by
755 IF_STMT. */
757 void
758 finish_else_clause (tree if_stmt)
760 ELSE_CLAUSE (if_stmt) = pop_stmt_list (ELSE_CLAUSE (if_stmt));
763 /* Finish an if-statement. */
765 void
766 finish_if_stmt (tree if_stmt)
768 tree scope = IF_SCOPE (if_stmt);
769 IF_SCOPE (if_stmt) = NULL;
770 add_stmt (do_poplevel (scope));
773 /* Begin a while-statement. Returns a newly created WHILE_STMT if
774 appropriate. */
776 tree
777 begin_while_stmt (void)
779 tree r;
780 r = build_stmt (input_location, WHILE_STMT, NULL_TREE, NULL_TREE);
781 add_stmt (r);
782 WHILE_BODY (r) = do_pushlevel (sk_block);
783 begin_cond (&WHILE_COND (r));
784 return r;
787 /* Process the COND of a while-statement, which may be given by
788 WHILE_STMT. */
790 void
791 finish_while_stmt_cond (tree cond, tree while_stmt, bool ivdep)
793 if (check_no_cilk (cond,
794 "Cilk array notation cannot be used as a condition for while statement",
795 "%<_Cilk_spawn%> statement cannot be used as a condition for while statement"))
796 cond = error_mark_node;
797 cond = maybe_convert_cond (cond);
798 finish_cond (&WHILE_COND (while_stmt), cond);
799 begin_maybe_infinite_loop (cond);
800 if (ivdep && cond != error_mark_node)
801 WHILE_COND (while_stmt) = build2 (ANNOTATE_EXPR,
802 TREE_TYPE (WHILE_COND (while_stmt)),
803 WHILE_COND (while_stmt),
804 build_int_cst (integer_type_node,
805 annot_expr_ivdep_kind));
806 simplify_loop_decl_cond (&WHILE_COND (while_stmt), WHILE_BODY (while_stmt));
809 /* Finish a while-statement, which may be given by WHILE_STMT. */
811 void
812 finish_while_stmt (tree while_stmt)
814 end_maybe_infinite_loop (boolean_true_node);
815 WHILE_BODY (while_stmt) = do_poplevel (WHILE_BODY (while_stmt));
818 /* Begin a do-statement. Returns a newly created DO_STMT if
819 appropriate. */
821 tree
822 begin_do_stmt (void)
824 tree r = build_stmt (input_location, DO_STMT, NULL_TREE, NULL_TREE);
825 begin_maybe_infinite_loop (boolean_true_node);
826 add_stmt (r);
827 DO_BODY (r) = push_stmt_list ();
828 return r;
831 /* Finish the body of a do-statement, which may be given by DO_STMT. */
833 void
834 finish_do_body (tree do_stmt)
836 tree body = DO_BODY (do_stmt) = pop_stmt_list (DO_BODY (do_stmt));
838 if (TREE_CODE (body) == STATEMENT_LIST && STATEMENT_LIST_TAIL (body))
839 body = STATEMENT_LIST_TAIL (body)->stmt;
841 if (IS_EMPTY_STMT (body))
842 warning (OPT_Wempty_body,
843 "suggest explicit braces around empty body in %<do%> statement");
846 /* Finish a do-statement, which may be given by DO_STMT, and whose
847 COND is as indicated. */
849 void
850 finish_do_stmt (tree cond, tree do_stmt, bool ivdep)
852 if (check_no_cilk (cond,
853 "Cilk array notation cannot be used as a condition for a do-while statement",
854 "%<_Cilk_spawn%> statement cannot be used as a condition for a do-while statement"))
855 cond = error_mark_node;
856 cond = maybe_convert_cond (cond);
857 end_maybe_infinite_loop (cond);
858 if (ivdep && cond != error_mark_node)
859 cond = build2 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
860 build_int_cst (integer_type_node, annot_expr_ivdep_kind));
861 DO_COND (do_stmt) = cond;
864 /* Finish a return-statement. The EXPRESSION returned, if any, is as
865 indicated. */
867 tree
868 finish_return_stmt (tree expr)
870 tree r;
871 bool no_warning;
873 expr = check_return_expr (expr, &no_warning);
875 if (error_operand_p (expr)
876 || (flag_openmp && !check_omp_return ()))
878 /* Suppress -Wreturn-type for this function. */
879 if (warn_return_type)
880 TREE_NO_WARNING (current_function_decl) = true;
881 return error_mark_node;
884 if (!processing_template_decl)
886 if (warn_sequence_point)
887 verify_sequence_points (expr);
889 if (DECL_DESTRUCTOR_P (current_function_decl)
890 || (DECL_CONSTRUCTOR_P (current_function_decl)
891 && targetm.cxx.cdtor_returns_this ()))
893 /* Similarly, all destructors must run destructors for
894 base-classes before returning. So, all returns in a
895 destructor get sent to the DTOR_LABEL; finish_function emits
896 code to return a value there. */
897 return finish_goto_stmt (cdtor_label);
901 r = build_stmt (input_location, RETURN_EXPR, expr);
902 TREE_NO_WARNING (r) |= no_warning;
903 r = maybe_cleanup_point_expr_void (r);
904 r = add_stmt (r);
906 return r;
909 /* Begin the scope of a for-statement or a range-for-statement.
910 Both the returned trees are to be used in a call to
911 begin_for_stmt or begin_range_for_stmt. */
913 tree
914 begin_for_scope (tree *init)
916 tree scope = NULL_TREE;
917 if (flag_new_for_scope > 0)
918 scope = do_pushlevel (sk_for);
920 if (processing_template_decl)
921 *init = push_stmt_list ();
922 else
923 *init = NULL_TREE;
925 return scope;
928 /* Begin a for-statement. Returns a new FOR_STMT.
929 SCOPE and INIT should be the return of begin_for_scope,
930 or both NULL_TREE */
932 tree
933 begin_for_stmt (tree scope, tree init)
935 tree r;
937 r = build_stmt (input_location, FOR_STMT, NULL_TREE, NULL_TREE,
938 NULL_TREE, NULL_TREE, NULL_TREE);
940 if (scope == NULL_TREE)
942 gcc_assert (!init || !(flag_new_for_scope > 0));
943 if (!init)
944 scope = begin_for_scope (&init);
946 FOR_INIT_STMT (r) = init;
947 FOR_SCOPE (r) = scope;
949 return r;
952 /* Finish the for-init-statement of a for-statement, which may be
953 given by FOR_STMT. */
955 void
956 finish_for_init_stmt (tree for_stmt)
958 if (processing_template_decl)
959 FOR_INIT_STMT (for_stmt) = pop_stmt_list (FOR_INIT_STMT (for_stmt));
960 add_stmt (for_stmt);
961 FOR_BODY (for_stmt) = do_pushlevel (sk_block);
962 begin_cond (&FOR_COND (for_stmt));
965 /* Finish the COND of a for-statement, which may be given by
966 FOR_STMT. */
968 void
969 finish_for_cond (tree cond, tree for_stmt, bool ivdep)
971 if (check_no_cilk (cond,
972 "Cilk array notation cannot be used in a condition for a for-loop",
973 "%<_Cilk_spawn%> statement cannot be used in a condition for a for-loop"))
974 cond = error_mark_node;
975 cond = maybe_convert_cond (cond);
976 finish_cond (&FOR_COND (for_stmt), cond);
977 begin_maybe_infinite_loop (cond);
978 if (ivdep && cond != error_mark_node)
979 FOR_COND (for_stmt) = build2 (ANNOTATE_EXPR,
980 TREE_TYPE (FOR_COND (for_stmt)),
981 FOR_COND (for_stmt),
982 build_int_cst (integer_type_node,
983 annot_expr_ivdep_kind));
984 simplify_loop_decl_cond (&FOR_COND (for_stmt), FOR_BODY (for_stmt));
987 /* Finish the increment-EXPRESSION in a for-statement, which may be
988 given by FOR_STMT. */
990 void
991 finish_for_expr (tree expr, tree for_stmt)
993 if (!expr)
994 return;
995 /* If EXPR is an overloaded function, issue an error; there is no
996 context available to use to perform overload resolution. */
997 if (type_unknown_p (expr))
999 cxx_incomplete_type_error (expr, TREE_TYPE (expr));
1000 expr = error_mark_node;
1002 if (!processing_template_decl)
1004 if (warn_sequence_point)
1005 verify_sequence_points (expr);
1006 expr = convert_to_void (expr, ICV_THIRD_IN_FOR,
1007 tf_warning_or_error);
1009 else if (!type_dependent_expression_p (expr))
1010 convert_to_void (build_non_dependent_expr (expr), ICV_THIRD_IN_FOR,
1011 tf_warning_or_error);
1012 expr = maybe_cleanup_point_expr_void (expr);
1013 if (check_for_bare_parameter_packs (expr))
1014 expr = error_mark_node;
1015 FOR_EXPR (for_stmt) = expr;
1018 /* Finish the body of a for-statement, which may be given by
1019 FOR_STMT. The increment-EXPR for the loop must be
1020 provided.
1021 It can also finish RANGE_FOR_STMT. */
1023 void
1024 finish_for_stmt (tree for_stmt)
1026 end_maybe_infinite_loop (boolean_true_node);
1028 if (TREE_CODE (for_stmt) == RANGE_FOR_STMT)
1029 RANGE_FOR_BODY (for_stmt) = do_poplevel (RANGE_FOR_BODY (for_stmt));
1030 else
1031 FOR_BODY (for_stmt) = do_poplevel (FOR_BODY (for_stmt));
1033 /* Pop the scope for the body of the loop. */
1034 if (flag_new_for_scope > 0)
1036 tree scope;
1037 tree *scope_ptr = (TREE_CODE (for_stmt) == RANGE_FOR_STMT
1038 ? &RANGE_FOR_SCOPE (for_stmt)
1039 : &FOR_SCOPE (for_stmt));
1040 scope = *scope_ptr;
1041 *scope_ptr = NULL;
1042 add_stmt (do_poplevel (scope));
1046 /* Begin a range-for-statement. Returns a new RANGE_FOR_STMT.
1047 SCOPE and INIT should be the return of begin_for_scope,
1048 or both NULL_TREE .
1049 To finish it call finish_for_stmt(). */
1051 tree
1052 begin_range_for_stmt (tree scope, tree init)
1054 tree r;
1056 begin_maybe_infinite_loop (boolean_false_node);
1058 r = build_stmt (input_location, RANGE_FOR_STMT,
1059 NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE);
1061 if (scope == NULL_TREE)
1063 gcc_assert (!init || !(flag_new_for_scope > 0));
1064 if (!init)
1065 scope = begin_for_scope (&init);
1068 /* RANGE_FOR_STMTs do not use nor save the init tree, so we
1069 pop it now. */
1070 if (init)
1071 pop_stmt_list (init);
1072 RANGE_FOR_SCOPE (r) = scope;
1074 return r;
1077 /* Finish the head of a range-based for statement, which may
1078 be given by RANGE_FOR_STMT. DECL must be the declaration
1079 and EXPR must be the loop expression. */
1081 void
1082 finish_range_for_decl (tree range_for_stmt, tree decl, tree expr)
1084 RANGE_FOR_DECL (range_for_stmt) = decl;
1085 RANGE_FOR_EXPR (range_for_stmt) = expr;
1086 add_stmt (range_for_stmt);
1087 RANGE_FOR_BODY (range_for_stmt) = do_pushlevel (sk_block);
1090 /* Finish a break-statement. */
1092 tree
1093 finish_break_stmt (void)
1095 /* In switch statements break is sometimes stylistically used after
1096 a return statement. This can lead to spurious warnings about
1097 control reaching the end of a non-void function when it is
1098 inlined. Note that we are calling block_may_fallthru with
1099 language specific tree nodes; this works because
1100 block_may_fallthru returns true when given something it does not
1101 understand. */
1102 if (!block_may_fallthru (cur_stmt_list))
1103 return void_node;
1104 return add_stmt (build_stmt (input_location, BREAK_STMT));
1107 /* Finish a continue-statement. */
1109 tree
1110 finish_continue_stmt (void)
1112 return add_stmt (build_stmt (input_location, CONTINUE_STMT));
1115 /* Begin a switch-statement. Returns a new SWITCH_STMT if
1116 appropriate. */
1118 tree
1119 begin_switch_stmt (void)
1121 tree r, scope;
1123 scope = do_pushlevel (sk_cond);
1124 r = build_stmt (input_location, SWITCH_STMT, NULL_TREE, NULL_TREE, NULL_TREE, scope);
1126 begin_cond (&SWITCH_STMT_COND (r));
1128 return r;
1131 /* Finish the cond of a switch-statement. */
1133 void
1134 finish_switch_cond (tree cond, tree switch_stmt)
1136 tree orig_type = NULL;
1138 if (check_no_cilk (cond,
1139 "Cilk array notation cannot be used as a condition for switch statement",
1140 "%<_Cilk_spawn%> statement cannot be used as a condition for switch statement"))
1141 cond = error_mark_node;
1143 if (!processing_template_decl)
1145 /* Convert the condition to an integer or enumeration type. */
1146 cond = build_expr_type_conversion (WANT_INT | WANT_ENUM, cond, true);
1147 if (cond == NULL_TREE)
1149 error ("switch quantity not an integer");
1150 cond = error_mark_node;
1152 /* We want unlowered type here to handle enum bit-fields. */
1153 orig_type = unlowered_expr_type (cond);
1154 if (TREE_CODE (orig_type) != ENUMERAL_TYPE)
1155 orig_type = TREE_TYPE (cond);
1156 if (cond != error_mark_node)
1158 /* [stmt.switch]
1160 Integral promotions are performed. */
1161 cond = perform_integral_promotions (cond);
1162 cond = maybe_cleanup_point_expr (cond);
1165 if (check_for_bare_parameter_packs (cond))
1166 cond = error_mark_node;
1167 else if (!processing_template_decl && warn_sequence_point)
1168 verify_sequence_points (cond);
1170 finish_cond (&SWITCH_STMT_COND (switch_stmt), cond);
1171 SWITCH_STMT_TYPE (switch_stmt) = orig_type;
1172 add_stmt (switch_stmt);
1173 push_switch (switch_stmt);
1174 SWITCH_STMT_BODY (switch_stmt) = push_stmt_list ();
1177 /* Finish the body of a switch-statement, which may be given by
1178 SWITCH_STMT. The COND to switch on is indicated. */
1180 void
1181 finish_switch_stmt (tree switch_stmt)
1183 tree scope;
1185 SWITCH_STMT_BODY (switch_stmt) =
1186 pop_stmt_list (SWITCH_STMT_BODY (switch_stmt));
1187 pop_switch ();
1189 scope = SWITCH_STMT_SCOPE (switch_stmt);
1190 SWITCH_STMT_SCOPE (switch_stmt) = NULL;
1191 add_stmt (do_poplevel (scope));
1194 /* Begin a try-block. Returns a newly-created TRY_BLOCK if
1195 appropriate. */
1197 tree
1198 begin_try_block (void)
1200 tree r = build_stmt (input_location, TRY_BLOCK, NULL_TREE, NULL_TREE);
1201 add_stmt (r);
1202 TRY_STMTS (r) = push_stmt_list ();
1203 return r;
1206 /* Likewise, for a function-try-block. The block returned in
1207 *COMPOUND_STMT is an artificial outer scope, containing the
1208 function-try-block. */
1210 tree
1211 begin_function_try_block (tree *compound_stmt)
1213 tree r;
1214 /* This outer scope does not exist in the C++ standard, but we need
1215 a place to put __FUNCTION__ and similar variables. */
1216 *compound_stmt = begin_compound_stmt (0);
1217 r = begin_try_block ();
1218 FN_TRY_BLOCK_P (r) = 1;
1219 return r;
1222 /* Finish a try-block, which may be given by TRY_BLOCK. */
1224 void
1225 finish_try_block (tree try_block)
1227 TRY_STMTS (try_block) = pop_stmt_list (TRY_STMTS (try_block));
1228 TRY_HANDLERS (try_block) = push_stmt_list ();
1231 /* Finish the body of a cleanup try-block, which may be given by
1232 TRY_BLOCK. */
1234 void
1235 finish_cleanup_try_block (tree try_block)
1237 TRY_STMTS (try_block) = pop_stmt_list (TRY_STMTS (try_block));
1240 /* Finish an implicitly generated try-block, with a cleanup is given
1241 by CLEANUP. */
1243 void
1244 finish_cleanup (tree cleanup, tree try_block)
1246 TRY_HANDLERS (try_block) = cleanup;
1247 CLEANUP_P (try_block) = 1;
1250 /* Likewise, for a function-try-block. */
1252 void
1253 finish_function_try_block (tree try_block)
1255 finish_try_block (try_block);
1256 /* FIXME : something queer about CTOR_INITIALIZER somehow following
1257 the try block, but moving it inside. */
1258 in_function_try_handler = 1;
1261 /* Finish a handler-sequence for a try-block, which may be given by
1262 TRY_BLOCK. */
1264 void
1265 finish_handler_sequence (tree try_block)
1267 TRY_HANDLERS (try_block) = pop_stmt_list (TRY_HANDLERS (try_block));
1268 check_handlers (TRY_HANDLERS (try_block));
1271 /* Finish the handler-seq for a function-try-block, given by
1272 TRY_BLOCK. COMPOUND_STMT is the outer block created by
1273 begin_function_try_block. */
1275 void
1276 finish_function_handler_sequence (tree try_block, tree compound_stmt)
1278 in_function_try_handler = 0;
1279 finish_handler_sequence (try_block);
1280 finish_compound_stmt (compound_stmt);
1283 /* Begin a handler. Returns a HANDLER if appropriate. */
1285 tree
1286 begin_handler (void)
1288 tree r;
1290 r = build_stmt (input_location, HANDLER, NULL_TREE, NULL_TREE);
1291 add_stmt (r);
1293 /* Create a binding level for the eh_info and the exception object
1294 cleanup. */
1295 HANDLER_BODY (r) = do_pushlevel (sk_catch);
1297 return r;
1300 /* Finish the handler-parameters for a handler, which may be given by
1301 HANDLER. DECL is the declaration for the catch parameter, or NULL
1302 if this is a `catch (...)' clause. */
1304 void
1305 finish_handler_parms (tree decl, tree handler)
1307 tree type = NULL_TREE;
1308 if (processing_template_decl)
1310 if (decl)
1312 decl = pushdecl (decl);
1313 decl = push_template_decl (decl);
1314 HANDLER_PARMS (handler) = decl;
1315 type = TREE_TYPE (decl);
1318 else
1319 type = expand_start_catch_block (decl);
1320 HANDLER_TYPE (handler) = type;
1323 /* Finish a handler, which may be given by HANDLER. The BLOCKs are
1324 the return value from the matching call to finish_handler_parms. */
1326 void
1327 finish_handler (tree handler)
1329 if (!processing_template_decl)
1330 expand_end_catch_block ();
1331 HANDLER_BODY (handler) = do_poplevel (HANDLER_BODY (handler));
1334 /* Begin a compound statement. FLAGS contains some bits that control the
1335 behavior and context. If BCS_NO_SCOPE is set, the compound statement
1336 does not define a scope. If BCS_FN_BODY is set, this is the outermost
1337 block of a function. If BCS_TRY_BLOCK is set, this is the block
1338 created on behalf of a TRY statement. Returns a token to be passed to
1339 finish_compound_stmt. */
1341 tree
1342 begin_compound_stmt (unsigned int flags)
1344 tree r;
1346 if (flags & BCS_NO_SCOPE)
1348 r = push_stmt_list ();
1349 STATEMENT_LIST_NO_SCOPE (r) = 1;
1351 /* Normally, we try hard to keep the BLOCK for a statement-expression.
1352 But, if it's a statement-expression with a scopeless block, there's
1353 nothing to keep, and we don't want to accidentally keep a block
1354 *inside* the scopeless block. */
1355 keep_next_level (false);
1357 else
1358 r = do_pushlevel (flags & BCS_TRY_BLOCK ? sk_try : sk_block);
1360 /* When processing a template, we need to remember where the braces were,
1361 so that we can set up identical scopes when instantiating the template
1362 later. BIND_EXPR is a handy candidate for this.
1363 Note that do_poplevel won't create a BIND_EXPR itself here (and thus
1364 result in nested BIND_EXPRs), since we don't build BLOCK nodes when
1365 processing templates. */
1366 if (processing_template_decl)
1368 r = build3 (BIND_EXPR, NULL, NULL, r, NULL);
1369 BIND_EXPR_TRY_BLOCK (r) = (flags & BCS_TRY_BLOCK) != 0;
1370 BIND_EXPR_BODY_BLOCK (r) = (flags & BCS_FN_BODY) != 0;
1371 TREE_SIDE_EFFECTS (r) = 1;
1374 return r;
1377 /* Finish a compound-statement, which is given by STMT. */
1379 void
1380 finish_compound_stmt (tree stmt)
1382 if (TREE_CODE (stmt) == BIND_EXPR)
1384 tree body = do_poplevel (BIND_EXPR_BODY (stmt));
1385 /* If the STATEMENT_LIST is empty and this BIND_EXPR isn't special,
1386 discard the BIND_EXPR so it can be merged with the containing
1387 STATEMENT_LIST. */
1388 if (TREE_CODE (body) == STATEMENT_LIST
1389 && STATEMENT_LIST_HEAD (body) == NULL
1390 && !BIND_EXPR_BODY_BLOCK (stmt)
1391 && !BIND_EXPR_TRY_BLOCK (stmt))
1392 stmt = body;
1393 else
1394 BIND_EXPR_BODY (stmt) = body;
1396 else if (STATEMENT_LIST_NO_SCOPE (stmt))
1397 stmt = pop_stmt_list (stmt);
1398 else
1400 /* Destroy any ObjC "super" receivers that may have been
1401 created. */
1402 objc_clear_super_receiver ();
1404 stmt = do_poplevel (stmt);
1407 /* ??? See c_end_compound_stmt wrt statement expressions. */
1408 add_stmt (stmt);
1411 /* Finish an asm-statement, whose components are a STRING, some
1412 OUTPUT_OPERANDS, some INPUT_OPERANDS, some CLOBBERS and some
1413 LABELS. Also note whether the asm-statement should be
1414 considered volatile. */
1416 tree
1417 finish_asm_stmt (int volatile_p, tree string, tree output_operands,
1418 tree input_operands, tree clobbers, tree labels)
1420 tree r;
1421 tree t;
1422 int ninputs = list_length (input_operands);
1423 int noutputs = list_length (output_operands);
1425 if (!processing_template_decl)
1427 const char *constraint;
1428 const char **oconstraints;
1429 bool allows_mem, allows_reg, is_inout;
1430 tree operand;
1431 int i;
1433 oconstraints = XALLOCAVEC (const char *, noutputs);
1435 string = resolve_asm_operand_names (string, output_operands,
1436 input_operands, labels);
1438 for (i = 0, t = output_operands; t; t = TREE_CHAIN (t), ++i)
1440 operand = TREE_VALUE (t);
1442 /* ??? Really, this should not be here. Users should be using a
1443 proper lvalue, dammit. But there's a long history of using
1444 casts in the output operands. In cases like longlong.h, this
1445 becomes a primitive form of typechecking -- if the cast can be
1446 removed, then the output operand had a type of the proper width;
1447 otherwise we'll get an error. Gross, but ... */
1448 STRIP_NOPS (operand);
1450 operand = mark_lvalue_use (operand);
1452 if (!lvalue_or_else (operand, lv_asm, tf_warning_or_error))
1453 operand = error_mark_node;
1455 if (operand != error_mark_node
1456 && (TREE_READONLY (operand)
1457 || CP_TYPE_CONST_P (TREE_TYPE (operand))
1458 /* Functions are not modifiable, even though they are
1459 lvalues. */
1460 || TREE_CODE (TREE_TYPE (operand)) == FUNCTION_TYPE
1461 || TREE_CODE (TREE_TYPE (operand)) == METHOD_TYPE
1462 /* If it's an aggregate and any field is const, then it is
1463 effectively const. */
1464 || (CLASS_TYPE_P (TREE_TYPE (operand))
1465 && C_TYPE_FIELDS_READONLY (TREE_TYPE (operand)))))
1466 cxx_readonly_error (operand, lv_asm);
1468 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
1469 oconstraints[i] = constraint;
1471 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
1472 &allows_mem, &allows_reg, &is_inout))
1474 /* If the operand is going to end up in memory,
1475 mark it addressable. */
1476 if (!allows_reg && !cxx_mark_addressable (operand))
1477 operand = error_mark_node;
1479 else
1480 operand = error_mark_node;
1482 TREE_VALUE (t) = operand;
1485 for (i = 0, t = input_operands; t; ++i, t = TREE_CHAIN (t))
1487 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
1488 bool constraint_parsed
1489 = parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
1490 oconstraints, &allows_mem, &allows_reg);
1491 /* If the operand is going to end up in memory, don't call
1492 decay_conversion. */
1493 if (constraint_parsed && !allows_reg && allows_mem)
1494 operand = mark_lvalue_use (TREE_VALUE (t));
1495 else
1496 operand = decay_conversion (TREE_VALUE (t), tf_warning_or_error);
1498 /* If the type of the operand hasn't been determined (e.g.,
1499 because it involves an overloaded function), then issue
1500 an error message. There's no context available to
1501 resolve the overloading. */
1502 if (TREE_TYPE (operand) == unknown_type_node)
1504 error ("type of asm operand %qE could not be determined",
1505 TREE_VALUE (t));
1506 operand = error_mark_node;
1509 if (constraint_parsed)
1511 /* If the operand is going to end up in memory,
1512 mark it addressable. */
1513 if (!allows_reg && allows_mem)
1515 /* Strip the nops as we allow this case. FIXME, this really
1516 should be rejected or made deprecated. */
1517 STRIP_NOPS (operand);
1518 if (!cxx_mark_addressable (operand))
1519 operand = error_mark_node;
1521 else if (!allows_reg && !allows_mem)
1523 /* If constraint allows neither register nor memory,
1524 try harder to get a constant. */
1525 tree constop = maybe_constant_value (operand);
1526 if (TREE_CONSTANT (constop))
1527 operand = constop;
1530 else
1531 operand = error_mark_node;
1533 TREE_VALUE (t) = operand;
1537 r = build_stmt (input_location, ASM_EXPR, string,
1538 output_operands, input_operands,
1539 clobbers, labels);
1540 ASM_VOLATILE_P (r) = volatile_p || noutputs == 0;
1541 r = maybe_cleanup_point_expr_void (r);
1542 return add_stmt (r);
1545 /* Finish a label with the indicated NAME. Returns the new label. */
1547 tree
1548 finish_label_stmt (tree name)
1550 tree decl = define_label (input_location, name);
1552 if (decl == error_mark_node)
1553 return error_mark_node;
1555 add_stmt (build_stmt (input_location, LABEL_EXPR, decl));
1557 return decl;
1560 /* Finish a series of declarations for local labels. G++ allows users
1561 to declare "local" labels, i.e., labels with scope. This extension
1562 is useful when writing code involving statement-expressions. */
1564 void
1565 finish_label_decl (tree name)
1567 if (!at_function_scope_p ())
1569 error ("__label__ declarations are only allowed in function scopes");
1570 return;
1573 add_decl_expr (declare_local_label (name));
1576 /* When DECL goes out of scope, make sure that CLEANUP is executed. */
1578 void
1579 finish_decl_cleanup (tree decl, tree cleanup)
1581 push_cleanup (decl, cleanup, false);
1584 /* If the current scope exits with an exception, run CLEANUP. */
1586 void
1587 finish_eh_cleanup (tree cleanup)
1589 push_cleanup (NULL, cleanup, true);
1592 /* The MEM_INITS is a list of mem-initializers, in reverse of the
1593 order they were written by the user. Each node is as for
1594 emit_mem_initializers. */
1596 void
1597 finish_mem_initializers (tree mem_inits)
1599 /* Reorder the MEM_INITS so that they are in the order they appeared
1600 in the source program. */
1601 mem_inits = nreverse (mem_inits);
1603 if (processing_template_decl)
1605 tree mem;
1607 for (mem = mem_inits; mem; mem = TREE_CHAIN (mem))
1609 /* If the TREE_PURPOSE is a TYPE_PACK_EXPANSION, skip the
1610 check for bare parameter packs in the TREE_VALUE, because
1611 any parameter packs in the TREE_VALUE have already been
1612 bound as part of the TREE_PURPOSE. See
1613 make_pack_expansion for more information. */
1614 if (TREE_CODE (TREE_PURPOSE (mem)) != TYPE_PACK_EXPANSION
1615 && check_for_bare_parameter_packs (TREE_VALUE (mem)))
1616 TREE_VALUE (mem) = error_mark_node;
1619 add_stmt (build_min_nt_loc (UNKNOWN_LOCATION,
1620 CTOR_INITIALIZER, mem_inits));
1622 else
1623 emit_mem_initializers (mem_inits);
1626 /* Obfuscate EXPR if it looks like an id-expression or member access so
1627 that the call to finish_decltype in do_auto_deduction will give the
1628 right result. */
1630 tree
1631 force_paren_expr (tree expr)
1633 /* This is only needed for decltype(auto) in C++14. */
1634 if (cxx_dialect < cxx14)
1635 return expr;
1637 /* If we're in unevaluated context, we can't be deducing a
1638 return/initializer type, so we don't need to mess with this. */
1639 if (cp_unevaluated_operand)
1640 return expr;
1642 if (!DECL_P (expr) && TREE_CODE (expr) != COMPONENT_REF
1643 && TREE_CODE (expr) != SCOPE_REF)
1644 return expr;
1646 if (TREE_CODE (expr) == COMPONENT_REF)
1647 REF_PARENTHESIZED_P (expr) = true;
1648 else if (type_dependent_expression_p (expr))
1649 expr = build1 (PAREN_EXPR, TREE_TYPE (expr), expr);
1650 else
1652 cp_lvalue_kind kind = lvalue_kind (expr);
1653 if ((kind & ~clk_class) != clk_none)
1655 tree type = unlowered_expr_type (expr);
1656 bool rval = !!(kind & clk_rvalueref);
1657 type = cp_build_reference_type (type, rval);
1658 /* This inhibits warnings in, eg, cxx_mark_addressable
1659 (c++/60955). */
1660 warning_sentinel s (extra_warnings);
1661 expr = build_static_cast (type, expr, tf_error);
1662 if (expr != error_mark_node)
1663 REF_PARENTHESIZED_P (expr) = true;
1667 return expr;
1670 /* Finish a parenthesized expression EXPR. */
1672 tree
1673 finish_parenthesized_expr (tree expr)
1675 if (EXPR_P (expr))
1676 /* This inhibits warnings in c_common_truthvalue_conversion. */
1677 TREE_NO_WARNING (expr) = 1;
1679 if (TREE_CODE (expr) == OFFSET_REF
1680 || TREE_CODE (expr) == SCOPE_REF)
1681 /* [expr.unary.op]/3 The qualified id of a pointer-to-member must not be
1682 enclosed in parentheses. */
1683 PTRMEM_OK_P (expr) = 0;
1685 if (TREE_CODE (expr) == STRING_CST)
1686 PAREN_STRING_LITERAL_P (expr) = 1;
1688 expr = force_paren_expr (expr);
1690 return expr;
1693 /* Finish a reference to a non-static data member (DECL) that is not
1694 preceded by `.' or `->'. */
1696 tree
1697 finish_non_static_data_member (tree decl, tree object, tree qualifying_scope)
1699 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
1701 if (!object)
1703 tree scope = qualifying_scope;
1704 if (scope == NULL_TREE)
1705 scope = context_for_name_lookup (decl);
1706 object = maybe_dummy_object (scope, NULL);
1709 object = maybe_resolve_dummy (object, true);
1710 if (object == error_mark_node)
1711 return error_mark_node;
1713 /* DR 613/850: Can use non-static data members without an associated
1714 object in sizeof/decltype/alignof. */
1715 if (is_dummy_object (object) && cp_unevaluated_operand == 0
1716 && (!processing_template_decl || !current_class_ref))
1718 if (current_function_decl
1719 && DECL_STATIC_FUNCTION_P (current_function_decl))
1720 error ("invalid use of member %qD in static member function", decl);
1721 else
1722 error ("invalid use of non-static data member %qD", decl);
1723 inform (DECL_SOURCE_LOCATION (decl), "declared here");
1725 return error_mark_node;
1728 if (current_class_ptr)
1729 TREE_USED (current_class_ptr) = 1;
1730 if (processing_template_decl && !qualifying_scope)
1732 tree type = TREE_TYPE (decl);
1734 if (TREE_CODE (type) == REFERENCE_TYPE)
1735 /* Quals on the object don't matter. */;
1736 else if (PACK_EXPANSION_P (type))
1737 /* Don't bother trying to represent this. */
1738 type = NULL_TREE;
1739 else
1741 /* Set the cv qualifiers. */
1742 int quals = cp_type_quals (TREE_TYPE (object));
1744 if (DECL_MUTABLE_P (decl))
1745 quals &= ~TYPE_QUAL_CONST;
1747 quals |= cp_type_quals (TREE_TYPE (decl));
1748 type = cp_build_qualified_type (type, quals);
1751 return (convert_from_reference
1752 (build_min (COMPONENT_REF, type, object, decl, NULL_TREE)));
1754 /* If PROCESSING_TEMPLATE_DECL is nonzero here, then
1755 QUALIFYING_SCOPE is also non-null. Wrap this in a SCOPE_REF
1756 for now. */
1757 else if (processing_template_decl)
1758 return build_qualified_name (TREE_TYPE (decl),
1759 qualifying_scope,
1760 decl,
1761 /*template_p=*/false);
1762 else
1764 tree access_type = TREE_TYPE (object);
1766 perform_or_defer_access_check (TYPE_BINFO (access_type), decl,
1767 decl, tf_warning_or_error);
1769 /* If the data member was named `C::M', convert `*this' to `C'
1770 first. */
1771 if (qualifying_scope)
1773 tree binfo = NULL_TREE;
1774 object = build_scoped_ref (object, qualifying_scope,
1775 &binfo);
1778 return build_class_member_access_expr (object, decl,
1779 /*access_path=*/NULL_TREE,
1780 /*preserve_reference=*/false,
1781 tf_warning_or_error);
1785 /* If we are currently parsing a template and we encountered a typedef
1786 TYPEDEF_DECL that is being accessed though CONTEXT, this function
1787 adds the typedef to a list tied to the current template.
1788 At template instantiation time, that list is walked and access check
1789 performed for each typedef.
1790 LOCATION is the location of the usage point of TYPEDEF_DECL. */
1792 void
1793 add_typedef_to_current_template_for_access_check (tree typedef_decl,
1794 tree context,
1795 location_t location)
1797 tree template_info = NULL;
1798 tree cs = current_scope ();
1800 if (!is_typedef_decl (typedef_decl)
1801 || !context
1802 || !CLASS_TYPE_P (context)
1803 || !cs)
1804 return;
1806 if (CLASS_TYPE_P (cs) || TREE_CODE (cs) == FUNCTION_DECL)
1807 template_info = get_template_info (cs);
1809 if (template_info
1810 && TI_TEMPLATE (template_info)
1811 && !currently_open_class (context))
1812 append_type_to_template_for_access_check (cs, typedef_decl,
1813 context, location);
1816 /* DECL was the declaration to which a qualified-id resolved. Issue
1817 an error message if it is not accessible. If OBJECT_TYPE is
1818 non-NULL, we have just seen `x->' or `x.' and OBJECT_TYPE is the
1819 type of `*x', or `x', respectively. If the DECL was named as
1820 `A::B' then NESTED_NAME_SPECIFIER is `A'. */
1822 void
1823 check_accessibility_of_qualified_id (tree decl,
1824 tree object_type,
1825 tree nested_name_specifier)
1827 tree scope;
1828 tree qualifying_type = NULL_TREE;
1830 /* If we are parsing a template declaration and if decl is a typedef,
1831 add it to a list tied to the template.
1832 At template instantiation time, that list will be walked and
1833 access check performed. */
1834 add_typedef_to_current_template_for_access_check (decl,
1835 nested_name_specifier
1836 ? nested_name_specifier
1837 : DECL_CONTEXT (decl),
1838 input_location);
1840 /* If we're not checking, return immediately. */
1841 if (deferred_access_no_check)
1842 return;
1844 /* Determine the SCOPE of DECL. */
1845 scope = context_for_name_lookup (decl);
1846 /* If the SCOPE is not a type, then DECL is not a member. */
1847 if (!TYPE_P (scope))
1848 return;
1849 /* Compute the scope through which DECL is being accessed. */
1850 if (object_type
1851 /* OBJECT_TYPE might not be a class type; consider:
1853 class A { typedef int I; };
1854 I *p;
1855 p->A::I::~I();
1857 In this case, we will have "A::I" as the DECL, but "I" as the
1858 OBJECT_TYPE. */
1859 && CLASS_TYPE_P (object_type)
1860 && DERIVED_FROM_P (scope, object_type))
1861 /* If we are processing a `->' or `.' expression, use the type of the
1862 left-hand side. */
1863 qualifying_type = object_type;
1864 else if (nested_name_specifier)
1866 /* If the reference is to a non-static member of the
1867 current class, treat it as if it were referenced through
1868 `this'. */
1869 tree ct;
1870 if (DECL_NONSTATIC_MEMBER_P (decl)
1871 && current_class_ptr
1872 && DERIVED_FROM_P (scope, ct = current_nonlambda_class_type ()))
1873 qualifying_type = ct;
1874 /* Otherwise, use the type indicated by the
1875 nested-name-specifier. */
1876 else
1877 qualifying_type = nested_name_specifier;
1879 else
1880 /* Otherwise, the name must be from the current class or one of
1881 its bases. */
1882 qualifying_type = currently_open_derived_class (scope);
1884 if (qualifying_type
1885 /* It is possible for qualifying type to be a TEMPLATE_TYPE_PARM
1886 or similar in a default argument value. */
1887 && CLASS_TYPE_P (qualifying_type)
1888 && !dependent_type_p (qualifying_type))
1889 perform_or_defer_access_check (TYPE_BINFO (qualifying_type), decl,
1890 decl, tf_warning_or_error);
1893 /* EXPR is the result of a qualified-id. The QUALIFYING_CLASS was the
1894 class named to the left of the "::" operator. DONE is true if this
1895 expression is a complete postfix-expression; it is false if this
1896 expression is followed by '->', '[', '(', etc. ADDRESS_P is true
1897 iff this expression is the operand of '&'. TEMPLATE_P is true iff
1898 the qualified-id was of the form "A::template B". TEMPLATE_ARG_P
1899 is true iff this qualified name appears as a template argument. */
1901 tree
1902 finish_qualified_id_expr (tree qualifying_class,
1903 tree expr,
1904 bool done,
1905 bool address_p,
1906 bool template_p,
1907 bool template_arg_p,
1908 tsubst_flags_t complain)
1910 gcc_assert (TYPE_P (qualifying_class));
1912 if (error_operand_p (expr))
1913 return error_mark_node;
1915 if ((DECL_P (expr) || BASELINK_P (expr))
1916 && !mark_used (expr, complain))
1917 return error_mark_node;
1919 if (template_p)
1920 check_template_keyword (expr);
1922 /* If EXPR occurs as the operand of '&', use special handling that
1923 permits a pointer-to-member. */
1924 if (address_p && done)
1926 if (TREE_CODE (expr) == SCOPE_REF)
1927 expr = TREE_OPERAND (expr, 1);
1928 expr = build_offset_ref (qualifying_class, expr,
1929 /*address_p=*/true, complain);
1930 return expr;
1933 /* No need to check access within an enum. */
1934 if (TREE_CODE (qualifying_class) == ENUMERAL_TYPE)
1935 return expr;
1937 /* Within the scope of a class, turn references to non-static
1938 members into expression of the form "this->...". */
1939 if (template_arg_p)
1940 /* But, within a template argument, we do not want make the
1941 transformation, as there is no "this" pointer. */
1943 else if (TREE_CODE (expr) == FIELD_DECL)
1945 push_deferring_access_checks (dk_no_check);
1946 expr = finish_non_static_data_member (expr, NULL_TREE,
1947 qualifying_class);
1948 pop_deferring_access_checks ();
1950 else if (BASELINK_P (expr) && !processing_template_decl)
1952 /* See if any of the functions are non-static members. */
1953 /* If so, the expression may be relative to 'this'. */
1954 if (!shared_member_p (expr)
1955 && current_class_ptr
1956 && DERIVED_FROM_P (qualifying_class,
1957 current_nonlambda_class_type ()))
1958 expr = (build_class_member_access_expr
1959 (maybe_dummy_object (qualifying_class, NULL),
1960 expr,
1961 BASELINK_ACCESS_BINFO (expr),
1962 /*preserve_reference=*/false,
1963 complain));
1964 else if (done)
1965 /* The expression is a qualified name whose address is not
1966 being taken. */
1967 expr = build_offset_ref (qualifying_class, expr, /*address_p=*/false,
1968 complain);
1970 else if (BASELINK_P (expr))
1972 else
1974 /* In a template, return a SCOPE_REF for most qualified-ids
1975 so that we can check access at instantiation time. But if
1976 we're looking at a member of the current instantiation, we
1977 know we have access and building up the SCOPE_REF confuses
1978 non-type template argument handling. */
1979 if (processing_template_decl
1980 && !currently_open_class (qualifying_class))
1981 expr = build_qualified_name (TREE_TYPE (expr),
1982 qualifying_class, expr,
1983 template_p);
1985 expr = convert_from_reference (expr);
1988 return expr;
1991 /* Begin a statement-expression. The value returned must be passed to
1992 finish_stmt_expr. */
1994 tree
1995 begin_stmt_expr (void)
1997 return push_stmt_list ();
2000 /* Process the final expression of a statement expression. EXPR can be
2001 NULL, if the final expression is empty. Return a STATEMENT_LIST
2002 containing all the statements in the statement-expression, or
2003 ERROR_MARK_NODE if there was an error. */
2005 tree
2006 finish_stmt_expr_expr (tree expr, tree stmt_expr)
2008 if (error_operand_p (expr))
2010 /* The type of the statement-expression is the type of the last
2011 expression. */
2012 TREE_TYPE (stmt_expr) = error_mark_node;
2013 return error_mark_node;
2016 /* If the last statement does not have "void" type, then the value
2017 of the last statement is the value of the entire expression. */
2018 if (expr)
2020 tree type = TREE_TYPE (expr);
2022 if (processing_template_decl)
2024 expr = build_stmt (input_location, EXPR_STMT, expr);
2025 expr = add_stmt (expr);
2026 /* Mark the last statement so that we can recognize it as such at
2027 template-instantiation time. */
2028 EXPR_STMT_STMT_EXPR_RESULT (expr) = 1;
2030 else if (VOID_TYPE_P (type))
2032 /* Just treat this like an ordinary statement. */
2033 expr = finish_expr_stmt (expr);
2035 else
2037 /* It actually has a value we need to deal with. First, force it
2038 to be an rvalue so that we won't need to build up a copy
2039 constructor call later when we try to assign it to something. */
2040 expr = force_rvalue (expr, tf_warning_or_error);
2041 if (error_operand_p (expr))
2042 return error_mark_node;
2044 /* Update for array-to-pointer decay. */
2045 type = TREE_TYPE (expr);
2047 /* Wrap it in a CLEANUP_POINT_EXPR and add it to the list like a
2048 normal statement, but don't convert to void or actually add
2049 the EXPR_STMT. */
2050 if (TREE_CODE (expr) != CLEANUP_POINT_EXPR)
2051 expr = maybe_cleanup_point_expr (expr);
2052 add_stmt (expr);
2055 /* The type of the statement-expression is the type of the last
2056 expression. */
2057 TREE_TYPE (stmt_expr) = type;
2060 return stmt_expr;
2063 /* Finish a statement-expression. EXPR should be the value returned
2064 by the previous begin_stmt_expr. Returns an expression
2065 representing the statement-expression. */
2067 tree
2068 finish_stmt_expr (tree stmt_expr, bool has_no_scope)
2070 tree type;
2071 tree result;
2073 if (error_operand_p (stmt_expr))
2075 pop_stmt_list (stmt_expr);
2076 return error_mark_node;
2079 gcc_assert (TREE_CODE (stmt_expr) == STATEMENT_LIST);
2081 type = TREE_TYPE (stmt_expr);
2082 result = pop_stmt_list (stmt_expr);
2083 TREE_TYPE (result) = type;
2085 if (processing_template_decl)
2087 result = build_min (STMT_EXPR, type, result);
2088 TREE_SIDE_EFFECTS (result) = 1;
2089 STMT_EXPR_NO_SCOPE (result) = has_no_scope;
2091 else if (CLASS_TYPE_P (type))
2093 /* Wrap the statement-expression in a TARGET_EXPR so that the
2094 temporary object created by the final expression is destroyed at
2095 the end of the full-expression containing the
2096 statement-expression. */
2097 result = force_target_expr (type, result, tf_warning_or_error);
2100 return result;
2103 /* Returns the expression which provides the value of STMT_EXPR. */
2105 tree
2106 stmt_expr_value_expr (tree stmt_expr)
2108 tree t = STMT_EXPR_STMT (stmt_expr);
2110 if (TREE_CODE (t) == BIND_EXPR)
2111 t = BIND_EXPR_BODY (t);
2113 if (TREE_CODE (t) == STATEMENT_LIST && STATEMENT_LIST_TAIL (t))
2114 t = STATEMENT_LIST_TAIL (t)->stmt;
2116 if (TREE_CODE (t) == EXPR_STMT)
2117 t = EXPR_STMT_EXPR (t);
2119 return t;
2122 /* Return TRUE iff EXPR_STMT is an empty list of
2123 expression statements. */
2125 bool
2126 empty_expr_stmt_p (tree expr_stmt)
2128 tree body = NULL_TREE;
2130 if (expr_stmt == void_node)
2131 return true;
2133 if (expr_stmt)
2135 if (TREE_CODE (expr_stmt) == EXPR_STMT)
2136 body = EXPR_STMT_EXPR (expr_stmt);
2137 else if (TREE_CODE (expr_stmt) == STATEMENT_LIST)
2138 body = expr_stmt;
2141 if (body)
2143 if (TREE_CODE (body) == STATEMENT_LIST)
2144 return tsi_end_p (tsi_start (body));
2145 else
2146 return empty_expr_stmt_p (body);
2148 return false;
2151 /* Perform Koenig lookup. FN is the postfix-expression representing
2152 the function (or functions) to call; ARGS are the arguments to the
2153 call. Returns the functions to be considered by overload resolution. */
2155 tree
2156 perform_koenig_lookup (tree fn, vec<tree, va_gc> *args,
2157 tsubst_flags_t complain)
2159 tree identifier = NULL_TREE;
2160 tree functions = NULL_TREE;
2161 tree tmpl_args = NULL_TREE;
2162 bool template_id = false;
2164 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
2166 /* Use a separate flag to handle null args. */
2167 template_id = true;
2168 tmpl_args = TREE_OPERAND (fn, 1);
2169 fn = TREE_OPERAND (fn, 0);
2172 /* Find the name of the overloaded function. */
2173 if (identifier_p (fn))
2174 identifier = fn;
2175 else if (is_overloaded_fn (fn))
2177 functions = fn;
2178 identifier = DECL_NAME (get_first_fn (functions));
2180 else if (DECL_P (fn))
2182 functions = fn;
2183 identifier = DECL_NAME (fn);
2186 /* A call to a namespace-scope function using an unqualified name.
2188 Do Koenig lookup -- unless any of the arguments are
2189 type-dependent. */
2190 if (!any_type_dependent_arguments_p (args)
2191 && !any_dependent_template_arguments_p (tmpl_args))
2193 fn = lookup_arg_dependent (identifier, functions, args);
2194 if (!fn)
2196 /* The unqualified name could not be resolved. */
2197 if (complain)
2198 fn = unqualified_fn_lookup_error (identifier);
2199 else
2200 fn = identifier;
2204 if (fn && template_id)
2205 fn = build2 (TEMPLATE_ID_EXPR, unknown_type_node, fn, tmpl_args);
2207 return fn;
2210 /* Generate an expression for `FN (ARGS)'. This may change the
2211 contents of ARGS.
2213 If DISALLOW_VIRTUAL is true, the call to FN will be not generated
2214 as a virtual call, even if FN is virtual. (This flag is set when
2215 encountering an expression where the function name is explicitly
2216 qualified. For example a call to `X::f' never generates a virtual
2217 call.)
2219 Returns code for the call. */
2221 tree
2222 finish_call_expr (tree fn, vec<tree, va_gc> **args, bool disallow_virtual,
2223 bool koenig_p, tsubst_flags_t complain)
2225 tree result;
2226 tree orig_fn;
2227 vec<tree, va_gc> *orig_args = NULL;
2229 if (fn == error_mark_node)
2230 return error_mark_node;
2232 gcc_assert (!TYPE_P (fn));
2234 orig_fn = fn;
2236 if (processing_template_decl)
2238 /* If the call expression is dependent, build a CALL_EXPR node
2239 with no type; type_dependent_expression_p recognizes
2240 expressions with no type as being dependent. */
2241 if (type_dependent_expression_p (fn)
2242 || any_type_dependent_arguments_p (*args)
2243 /* For a non-static member function that doesn't have an
2244 explicit object argument, we need to specifically
2245 test the type dependency of the "this" pointer because it
2246 is not included in *ARGS even though it is considered to
2247 be part of the list of arguments. Note that this is
2248 related to CWG issues 515 and 1005. */
2249 || (TREE_CODE (fn) != COMPONENT_REF
2250 && non_static_member_function_p (fn)
2251 && current_class_ref
2252 && type_dependent_expression_p (current_class_ref)))
2254 result = build_nt_call_vec (fn, *args);
2255 SET_EXPR_LOCATION (result, EXPR_LOC_OR_LOC (fn, input_location));
2256 KOENIG_LOOKUP_P (result) = koenig_p;
2257 if (cfun)
2261 tree fndecl = OVL_CURRENT (fn);
2262 if (TREE_CODE (fndecl) != FUNCTION_DECL
2263 || !TREE_THIS_VOLATILE (fndecl))
2264 break;
2265 fn = OVL_NEXT (fn);
2267 while (fn);
2268 if (!fn)
2269 current_function_returns_abnormally = 1;
2271 return result;
2273 orig_args = make_tree_vector_copy (*args);
2274 if (!BASELINK_P (fn)
2275 && TREE_CODE (fn) != PSEUDO_DTOR_EXPR
2276 && TREE_TYPE (fn) != unknown_type_node)
2277 fn = build_non_dependent_expr (fn);
2278 make_args_non_dependent (*args);
2281 if (TREE_CODE (fn) == COMPONENT_REF)
2283 tree member = TREE_OPERAND (fn, 1);
2284 if (BASELINK_P (member))
2286 tree object = TREE_OPERAND (fn, 0);
2287 return build_new_method_call (object, member,
2288 args, NULL_TREE,
2289 (disallow_virtual
2290 ? LOOKUP_NORMAL | LOOKUP_NONVIRTUAL
2291 : LOOKUP_NORMAL),
2292 /*fn_p=*/NULL,
2293 complain);
2297 /* Per 13.3.1.1, '(&f)(...)' is the same as '(f)(...)'. */
2298 if (TREE_CODE (fn) == ADDR_EXPR
2299 && TREE_CODE (TREE_OPERAND (fn, 0)) == OVERLOAD)
2300 fn = TREE_OPERAND (fn, 0);
2302 if (is_overloaded_fn (fn))
2303 fn = baselink_for_fns (fn);
2305 result = NULL_TREE;
2306 if (BASELINK_P (fn))
2308 tree object;
2310 /* A call to a member function. From [over.call.func]:
2312 If the keyword this is in scope and refers to the class of
2313 that member function, or a derived class thereof, then the
2314 function call is transformed into a qualified function call
2315 using (*this) as the postfix-expression to the left of the
2316 . operator.... [Otherwise] a contrived object of type T
2317 becomes the implied object argument.
2319 In this situation:
2321 struct A { void f(); };
2322 struct B : public A {};
2323 struct C : public A { void g() { B::f(); }};
2325 "the class of that member function" refers to `A'. But 11.2
2326 [class.access.base] says that we need to convert 'this' to B* as
2327 part of the access, so we pass 'B' to maybe_dummy_object. */
2329 object = maybe_dummy_object (BINFO_TYPE (BASELINK_ACCESS_BINFO (fn)),
2330 NULL);
2332 if (processing_template_decl)
2334 if (type_dependent_expression_p (object))
2336 tree ret = build_nt_call_vec (orig_fn, orig_args);
2337 release_tree_vector (orig_args);
2338 return ret;
2340 object = build_non_dependent_expr (object);
2343 result = build_new_method_call (object, fn, args, NULL_TREE,
2344 (disallow_virtual
2345 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
2346 : LOOKUP_NORMAL),
2347 /*fn_p=*/NULL,
2348 complain);
2350 else if (is_overloaded_fn (fn))
2352 /* If the function is an overloaded builtin, resolve it. */
2353 if (TREE_CODE (fn) == FUNCTION_DECL
2354 && (DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL
2355 || DECL_BUILT_IN_CLASS (fn) == BUILT_IN_MD))
2356 result = resolve_overloaded_builtin (input_location, fn, *args);
2358 if (!result)
2360 if (warn_sizeof_pointer_memaccess
2361 && (complain & tf_warning)
2362 && !vec_safe_is_empty (*args)
2363 && !processing_template_decl)
2365 location_t sizeof_arg_loc[3];
2366 tree sizeof_arg[3];
2367 unsigned int i;
2368 for (i = 0; i < 3; i++)
2370 tree t;
2372 sizeof_arg_loc[i] = UNKNOWN_LOCATION;
2373 sizeof_arg[i] = NULL_TREE;
2374 if (i >= (*args)->length ())
2375 continue;
2376 t = (**args)[i];
2377 if (TREE_CODE (t) != SIZEOF_EXPR)
2378 continue;
2379 if (SIZEOF_EXPR_TYPE_P (t))
2380 sizeof_arg[i] = TREE_TYPE (TREE_OPERAND (t, 0));
2381 else
2382 sizeof_arg[i] = TREE_OPERAND (t, 0);
2383 sizeof_arg_loc[i] = EXPR_LOCATION (t);
2385 sizeof_pointer_memaccess_warning
2386 (sizeof_arg_loc, fn, *args,
2387 sizeof_arg, same_type_ignoring_top_level_qualifiers_p);
2390 /* A call to a namespace-scope function. */
2391 result = build_new_function_call (fn, args, koenig_p, complain);
2394 else if (TREE_CODE (fn) == PSEUDO_DTOR_EXPR)
2396 if (!vec_safe_is_empty (*args))
2397 error ("arguments to destructor are not allowed");
2398 /* Mark the pseudo-destructor call as having side-effects so
2399 that we do not issue warnings about its use. */
2400 result = build1 (NOP_EXPR,
2401 void_type_node,
2402 TREE_OPERAND (fn, 0));
2403 TREE_SIDE_EFFECTS (result) = 1;
2405 else if (CLASS_TYPE_P (TREE_TYPE (fn)))
2406 /* If the "function" is really an object of class type, it might
2407 have an overloaded `operator ()'. */
2408 result = build_op_call (fn, args, complain);
2410 if (!result)
2411 /* A call where the function is unknown. */
2412 result = cp_build_function_call_vec (fn, args, complain);
2414 if (processing_template_decl && result != error_mark_node)
2416 if (INDIRECT_REF_P (result))
2417 result = TREE_OPERAND (result, 0);
2418 result = build_call_vec (TREE_TYPE (result), orig_fn, orig_args);
2419 SET_EXPR_LOCATION (result, input_location);
2420 KOENIG_LOOKUP_P (result) = koenig_p;
2421 release_tree_vector (orig_args);
2422 result = convert_from_reference (result);
2425 if (koenig_p)
2427 /* Free garbage OVERLOADs from arg-dependent lookup. */
2428 tree next = NULL_TREE;
2429 for (fn = orig_fn;
2430 fn && TREE_CODE (fn) == OVERLOAD && OVL_ARG_DEPENDENT (fn);
2431 fn = next)
2433 if (processing_template_decl)
2434 /* In a template, we'll re-use them at instantiation time. */
2435 OVL_ARG_DEPENDENT (fn) = false;
2436 else
2438 next = OVL_CHAIN (fn);
2439 ggc_free (fn);
2444 return result;
2447 /* Finish a call to a postfix increment or decrement or EXPR. (Which
2448 is indicated by CODE, which should be POSTINCREMENT_EXPR or
2449 POSTDECREMENT_EXPR.) */
2451 tree
2452 finish_increment_expr (tree expr, enum tree_code code)
2454 return build_x_unary_op (input_location, code, expr, tf_warning_or_error);
2457 /* Finish a use of `this'. Returns an expression for `this'. */
2459 tree
2460 finish_this_expr (void)
2462 tree result = NULL_TREE;
2464 if (current_class_ptr)
2466 tree type = TREE_TYPE (current_class_ref);
2468 /* In a lambda expression, 'this' refers to the captured 'this'. */
2469 if (LAMBDA_TYPE_P (type))
2470 result = lambda_expr_this_capture (CLASSTYPE_LAMBDA_EXPR (type), true);
2471 else
2472 result = current_class_ptr;
2475 if (result)
2476 /* The keyword 'this' is a prvalue expression. */
2477 return rvalue (result);
2479 tree fn = current_nonlambda_function ();
2480 if (fn && DECL_STATIC_FUNCTION_P (fn))
2481 error ("%<this%> is unavailable for static member functions");
2482 else if (fn)
2483 error ("invalid use of %<this%> in non-member function");
2484 else
2485 error ("invalid use of %<this%> at top level");
2486 return error_mark_node;
2489 /* Finish a pseudo-destructor expression. If SCOPE is NULL, the
2490 expression was of the form `OBJECT.~DESTRUCTOR' where DESTRUCTOR is
2491 the TYPE for the type given. If SCOPE is non-NULL, the expression
2492 was of the form `OBJECT.SCOPE::~DESTRUCTOR'. */
2494 tree
2495 finish_pseudo_destructor_expr (tree object, tree scope, tree destructor,
2496 location_t loc)
2498 if (object == error_mark_node || destructor == error_mark_node)
2499 return error_mark_node;
2501 gcc_assert (TYPE_P (destructor));
2503 if (!processing_template_decl)
2505 if (scope == error_mark_node)
2507 error_at (loc, "invalid qualifying scope in pseudo-destructor name");
2508 return error_mark_node;
2510 if (is_auto (destructor))
2511 destructor = TREE_TYPE (object);
2512 if (scope && TYPE_P (scope) && !check_dtor_name (scope, destructor))
2514 error_at (loc,
2515 "qualified type %qT does not match destructor name ~%qT",
2516 scope, destructor);
2517 return error_mark_node;
2521 /* [expr.pseudo] says both:
2523 The type designated by the pseudo-destructor-name shall be
2524 the same as the object type.
2526 and:
2528 The cv-unqualified versions of the object type and of the
2529 type designated by the pseudo-destructor-name shall be the
2530 same type.
2532 We implement the more generous second sentence, since that is
2533 what most other compilers do. */
2534 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (object),
2535 destructor))
2537 error_at (loc, "%qE is not of type %qT", object, destructor);
2538 return error_mark_node;
2542 return build3_loc (loc, PSEUDO_DTOR_EXPR, void_type_node, object,
2543 scope, destructor);
2546 /* Finish an expression of the form CODE EXPR. */
2548 tree
2549 finish_unary_op_expr (location_t loc, enum tree_code code, tree expr,
2550 tsubst_flags_t complain)
2552 tree result = build_x_unary_op (loc, code, expr, complain);
2553 if ((complain & tf_warning)
2554 && TREE_OVERFLOW_P (result) && !TREE_OVERFLOW_P (expr))
2555 overflow_warning (input_location, result);
2557 return result;
2560 /* Finish a compound-literal expression. TYPE is the type to which
2561 the CONSTRUCTOR in COMPOUND_LITERAL is being cast. */
2563 tree
2564 finish_compound_literal (tree type, tree compound_literal,
2565 tsubst_flags_t complain)
2567 if (type == error_mark_node)
2568 return error_mark_node;
2570 if (TREE_CODE (type) == REFERENCE_TYPE)
2572 compound_literal
2573 = finish_compound_literal (TREE_TYPE (type), compound_literal,
2574 complain);
2575 return cp_build_c_cast (type, compound_literal, complain);
2578 if (!TYPE_OBJ_P (type))
2580 if (complain & tf_error)
2581 error ("compound literal of non-object type %qT", type);
2582 return error_mark_node;
2585 if (processing_template_decl)
2587 TREE_TYPE (compound_literal) = type;
2588 /* Mark the expression as a compound literal. */
2589 TREE_HAS_CONSTRUCTOR (compound_literal) = 1;
2590 return compound_literal;
2593 type = complete_type (type);
2595 if (TYPE_NON_AGGREGATE_CLASS (type))
2597 /* Trying to deal with a CONSTRUCTOR instead of a TREE_LIST
2598 everywhere that deals with function arguments would be a pain, so
2599 just wrap it in a TREE_LIST. The parser set a flag so we know
2600 that it came from T{} rather than T({}). */
2601 CONSTRUCTOR_IS_DIRECT_INIT (compound_literal) = 1;
2602 compound_literal = build_tree_list (NULL_TREE, compound_literal);
2603 return build_functional_cast (type, compound_literal, complain);
2606 if (TREE_CODE (type) == ARRAY_TYPE
2607 && check_array_initializer (NULL_TREE, type, compound_literal))
2608 return error_mark_node;
2609 compound_literal = reshape_init (type, compound_literal, complain);
2610 if (SCALAR_TYPE_P (type)
2611 && !BRACE_ENCLOSED_INITIALIZER_P (compound_literal)
2612 && !check_narrowing (type, compound_literal, complain))
2613 return error_mark_node;
2614 if (TREE_CODE (type) == ARRAY_TYPE
2615 && TYPE_DOMAIN (type) == NULL_TREE)
2617 cp_complete_array_type_or_error (&type, compound_literal,
2618 false, complain);
2619 if (type == error_mark_node)
2620 return error_mark_node;
2622 compound_literal = digest_init (type, compound_literal, complain);
2623 if (TREE_CODE (compound_literal) == CONSTRUCTOR)
2624 TREE_HAS_CONSTRUCTOR (compound_literal) = true;
2625 /* Put static/constant array temporaries in static variables, but always
2626 represent class temporaries with TARGET_EXPR so we elide copies. */
2627 if ((!at_function_scope_p () || CP_TYPE_CONST_P (type))
2628 && TREE_CODE (type) == ARRAY_TYPE
2629 && !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
2630 && initializer_constant_valid_p (compound_literal, type))
2632 tree decl = create_temporary_var (type);
2633 DECL_INITIAL (decl) = compound_literal;
2634 TREE_STATIC (decl) = 1;
2635 if (literal_type_p (type) && CP_TYPE_CONST_NON_VOLATILE_P (type))
2637 /* 5.19 says that a constant expression can include an
2638 lvalue-rvalue conversion applied to "a glvalue of literal type
2639 that refers to a non-volatile temporary object initialized
2640 with a constant expression". Rather than try to communicate
2641 that this VAR_DECL is a temporary, just mark it constexpr. */
2642 DECL_DECLARED_CONSTEXPR_P (decl) = true;
2643 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
2644 TREE_CONSTANT (decl) = true;
2646 cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
2647 decl = pushdecl_top_level (decl);
2648 DECL_NAME (decl) = make_anon_name ();
2649 SET_DECL_ASSEMBLER_NAME (decl, DECL_NAME (decl));
2650 /* Make sure the destructor is callable. */
2651 tree clean = cxx_maybe_build_cleanup (decl, complain);
2652 if (clean == error_mark_node)
2653 return error_mark_node;
2654 return decl;
2656 else
2657 return get_target_expr_sfinae (compound_literal, complain);
2660 /* Return the declaration for the function-name variable indicated by
2661 ID. */
2663 tree
2664 finish_fname (tree id)
2666 tree decl;
2668 decl = fname_decl (input_location, C_RID_CODE (id), id);
2669 if (processing_template_decl && current_function_decl
2670 && decl != error_mark_node)
2671 decl = DECL_NAME (decl);
2672 return decl;
2675 /* Finish a translation unit. */
2677 void
2678 finish_translation_unit (void)
2680 /* In case there were missing closebraces,
2681 get us back to the global binding level. */
2682 pop_everything ();
2683 while (current_namespace != global_namespace)
2684 pop_namespace ();
2686 /* Do file scope __FUNCTION__ et al. */
2687 finish_fname_decls ();
2690 /* Finish a template type parameter, specified as AGGR IDENTIFIER.
2691 Returns the parameter. */
2693 tree
2694 finish_template_type_parm (tree aggr, tree identifier)
2696 if (aggr != class_type_node)
2698 permerror (input_location, "template type parameters must use the keyword %<class%> or %<typename%>");
2699 aggr = class_type_node;
2702 return build_tree_list (aggr, identifier);
2705 /* Finish a template template parameter, specified as AGGR IDENTIFIER.
2706 Returns the parameter. */
2708 tree
2709 finish_template_template_parm (tree aggr, tree identifier)
2711 tree decl = build_decl (input_location,
2712 TYPE_DECL, identifier, NULL_TREE);
2714 tree tmpl = build_lang_decl (TEMPLATE_DECL, identifier, NULL_TREE);
2715 DECL_TEMPLATE_PARMS (tmpl) = current_template_parms;
2716 DECL_TEMPLATE_RESULT (tmpl) = decl;
2717 DECL_ARTIFICIAL (decl) = 1;
2719 // Associate the constraints with the underlying declaration,
2720 // not the template.
2721 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
2722 tree constr = build_constraints (reqs, NULL_TREE);
2723 set_constraints (decl, constr);
2725 end_template_decl ();
2727 gcc_assert (DECL_TEMPLATE_PARMS (tmpl));
2729 check_default_tmpl_args (decl, DECL_TEMPLATE_PARMS (tmpl),
2730 /*is_primary=*/true, /*is_partial=*/false,
2731 /*is_friend=*/0);
2733 return finish_template_type_parm (aggr, tmpl);
2736 /* ARGUMENT is the default-argument value for a template template
2737 parameter. If ARGUMENT is invalid, issue error messages and return
2738 the ERROR_MARK_NODE. Otherwise, ARGUMENT itself is returned. */
2740 tree
2741 check_template_template_default_arg (tree argument)
2743 if (TREE_CODE (argument) != TEMPLATE_DECL
2744 && TREE_CODE (argument) != TEMPLATE_TEMPLATE_PARM
2745 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
2747 if (TREE_CODE (argument) == TYPE_DECL)
2748 error ("invalid use of type %qT as a default value for a template "
2749 "template-parameter", TREE_TYPE (argument));
2750 else
2751 error ("invalid default argument for a template template parameter");
2752 return error_mark_node;
2755 return argument;
2758 /* Begin a class definition, as indicated by T. */
2760 tree
2761 begin_class_definition (tree t)
2763 if (error_operand_p (t) || error_operand_p (TYPE_MAIN_DECL (t)))
2764 return error_mark_node;
2766 if (processing_template_parmlist)
2768 error ("definition of %q#T inside template parameter list", t);
2769 return error_mark_node;
2772 /* According to the C++ ABI, decimal classes defined in ISO/IEC TR 24733
2773 are passed the same as decimal scalar types. */
2774 if (TREE_CODE (t) == RECORD_TYPE
2775 && !processing_template_decl)
2777 tree ns = TYPE_CONTEXT (t);
2778 if (ns && TREE_CODE (ns) == NAMESPACE_DECL
2779 && DECL_CONTEXT (ns) == std_node
2780 && DECL_NAME (ns)
2781 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (ns)), "decimal"))
2783 const char *n = TYPE_NAME_STRING (t);
2784 if ((strcmp (n, "decimal32") == 0)
2785 || (strcmp (n, "decimal64") == 0)
2786 || (strcmp (n, "decimal128") == 0))
2787 TYPE_TRANSPARENT_AGGR (t) = 1;
2791 /* A non-implicit typename comes from code like:
2793 template <typename T> struct A {
2794 template <typename U> struct A<T>::B ...
2796 This is erroneous. */
2797 else if (TREE_CODE (t) == TYPENAME_TYPE)
2799 error ("invalid definition of qualified type %qT", t);
2800 t = error_mark_node;
2803 if (t == error_mark_node || ! MAYBE_CLASS_TYPE_P (t))
2805 t = make_class_type (RECORD_TYPE);
2806 pushtag (make_anon_name (), t, /*tag_scope=*/ts_current);
2809 if (TYPE_BEING_DEFINED (t))
2811 t = make_class_type (TREE_CODE (t));
2812 pushtag (TYPE_IDENTIFIER (t), t, /*tag_scope=*/ts_current);
2814 maybe_process_partial_specialization (t);
2815 pushclass (t);
2816 TYPE_BEING_DEFINED (t) = 1;
2817 class_binding_level->defining_class_p = 1;
2819 if (flag_pack_struct)
2821 tree v;
2822 TYPE_PACKED (t) = 1;
2823 /* Even though the type is being defined for the first time
2824 here, there might have been a forward declaration, so there
2825 might be cv-qualified variants of T. */
2826 for (v = TYPE_NEXT_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v))
2827 TYPE_PACKED (v) = 1;
2829 /* Reset the interface data, at the earliest possible
2830 moment, as it might have been set via a class foo;
2831 before. */
2832 if (! TYPE_ANONYMOUS_P (t))
2834 struct c_fileinfo *finfo = \
2835 get_fileinfo (LOCATION_FILE (input_location));
2836 CLASSTYPE_INTERFACE_ONLY (t) = finfo->interface_only;
2837 SET_CLASSTYPE_INTERFACE_UNKNOWN_X
2838 (t, finfo->interface_unknown);
2840 reset_specialization();
2842 /* Make a declaration for this class in its own scope. */
2843 build_self_reference ();
2845 return t;
2848 /* Finish the member declaration given by DECL. */
2850 void
2851 finish_member_declaration (tree decl)
2853 if (decl == error_mark_node || decl == NULL_TREE)
2854 return;
2856 if (decl == void_type_node)
2857 /* The COMPONENT was a friend, not a member, and so there's
2858 nothing for us to do. */
2859 return;
2861 /* We should see only one DECL at a time. */
2862 gcc_assert (DECL_CHAIN (decl) == NULL_TREE);
2864 /* Set up access control for DECL. */
2865 TREE_PRIVATE (decl)
2866 = (current_access_specifier == access_private_node);
2867 TREE_PROTECTED (decl)
2868 = (current_access_specifier == access_protected_node);
2869 if (TREE_CODE (decl) == TEMPLATE_DECL)
2871 TREE_PRIVATE (DECL_TEMPLATE_RESULT (decl)) = TREE_PRIVATE (decl);
2872 TREE_PROTECTED (DECL_TEMPLATE_RESULT (decl)) = TREE_PROTECTED (decl);
2875 /* Mark the DECL as a member of the current class, unless it's
2876 a member of an enumeration. */
2877 if (TREE_CODE (decl) != CONST_DECL)
2878 DECL_CONTEXT (decl) = current_class_type;
2880 /* Check for bare parameter packs in the member variable declaration. */
2881 if (TREE_CODE (decl) == FIELD_DECL)
2883 if (check_for_bare_parameter_packs (TREE_TYPE (decl)))
2884 TREE_TYPE (decl) = error_mark_node;
2885 if (check_for_bare_parameter_packs (DECL_ATTRIBUTES (decl)))
2886 DECL_ATTRIBUTES (decl) = NULL_TREE;
2889 /* [dcl.link]
2891 A C language linkage is ignored for the names of class members
2892 and the member function type of class member functions. */
2893 if (DECL_LANG_SPECIFIC (decl) && DECL_LANGUAGE (decl) == lang_c)
2894 SET_DECL_LANGUAGE (decl, lang_cplusplus);
2896 /* Put functions on the TYPE_METHODS list and everything else on the
2897 TYPE_FIELDS list. Note that these are built up in reverse order.
2898 We reverse them (to obtain declaration order) in finish_struct. */
2899 if (DECL_DECLARES_FUNCTION_P (decl))
2901 /* We also need to add this function to the
2902 CLASSTYPE_METHOD_VEC. */
2903 if (add_method (current_class_type, decl, NULL_TREE))
2905 gcc_assert (TYPE_MAIN_VARIANT (current_class_type) == current_class_type);
2906 DECL_CHAIN (decl) = TYPE_METHODS (current_class_type);
2907 TYPE_METHODS (current_class_type) = decl;
2909 maybe_add_class_template_decl_list (current_class_type, decl,
2910 /*friend_p=*/0);
2913 /* Enter the DECL into the scope of the class, if the class
2914 isn't a closure (whose fields are supposed to be unnamed). */
2915 else if (CLASSTYPE_LAMBDA_EXPR (current_class_type)
2916 || pushdecl_class_level (decl))
2918 if (TREE_CODE (decl) == USING_DECL)
2920 /* For now, ignore class-scope USING_DECLS, so that
2921 debugging backends do not see them. */
2922 DECL_IGNORED_P (decl) = 1;
2925 /* All TYPE_DECLs go at the end of TYPE_FIELDS. Ordinary fields
2926 go at the beginning. The reason is that lookup_field_1
2927 searches the list in order, and we want a field name to
2928 override a type name so that the "struct stat hack" will
2929 work. In particular:
2931 struct S { enum E { }; int E } s;
2932 s.E = 3;
2934 is valid. In addition, the FIELD_DECLs must be maintained in
2935 declaration order so that class layout works as expected.
2936 However, we don't need that order until class layout, so we
2937 save a little time by putting FIELD_DECLs on in reverse order
2938 here, and then reversing them in finish_struct_1. (We could
2939 also keep a pointer to the correct insertion points in the
2940 list.) */
2942 if (TREE_CODE (decl) == TYPE_DECL)
2943 TYPE_FIELDS (current_class_type)
2944 = chainon (TYPE_FIELDS (current_class_type), decl);
2945 else
2947 DECL_CHAIN (decl) = TYPE_FIELDS (current_class_type);
2948 TYPE_FIELDS (current_class_type) = decl;
2951 maybe_add_class_template_decl_list (current_class_type, decl,
2952 /*friend_p=*/0);
2955 if (pch_file)
2956 note_decl_for_pch (decl);
2959 /* DECL has been declared while we are building a PCH file. Perform
2960 actions that we might normally undertake lazily, but which can be
2961 performed now so that they do not have to be performed in
2962 translation units which include the PCH file. */
2964 void
2965 note_decl_for_pch (tree decl)
2967 gcc_assert (pch_file);
2969 /* There's a good chance that we'll have to mangle names at some
2970 point, even if only for emission in debugging information. */
2971 if (VAR_OR_FUNCTION_DECL_P (decl)
2972 && !processing_template_decl)
2973 mangle_decl (decl);
2976 /* Finish processing a complete template declaration. The PARMS are
2977 the template parameters. */
2979 void
2980 finish_template_decl (tree parms)
2982 if (parms)
2983 end_template_decl ();
2984 else
2985 end_specialization ();
2988 // Returns the template type of the class scope being entered. If we're
2989 // entering a constrained class scope. TYPE is the class template
2990 // scope being entered and we may need to match the intended type with
2991 // a constrained specialization. For example:
2993 // template<Object T>
2994 // struct S { void f(); }; #1
2996 // template<Object T>
2997 // void S<T>::f() { } #2
2999 // We check, in #2, that S<T> refers precisely to the type declared by
3000 // #1 (i.e., that the constraints match). Note that the following should
3001 // be an error since there is no specialization of S<T> that is
3002 // unconstrained, but this is not diagnosed here.
3004 // template<typename T>
3005 // void S<T>::f() { }
3007 // We cannot diagnose this problem here since this function also matches
3008 // qualified template names that are not part of a definition. For example:
3010 // template<Integral T, Floating_point U>
3011 // typename pair<T, U>::first_type void f(T, U);
3013 // Here, it is unlikely that there is a partial specialization of
3014 // pair constrained for for Integral and Floating_point arguments.
3016 // The general rule is: if a constrained specialization with matching
3017 // constraints is found return that type. Also note that if TYPE is not a
3018 // class-type (e.g. a typename type), then no fixup is needed.
3020 static tree
3021 fixup_template_type (tree type)
3023 // Find the template parameter list at the a depth appropriate to
3024 // the scope we're trying to enter.
3025 tree parms = current_template_parms;
3026 int depth = template_class_depth (type);
3027 for (int n = processing_template_decl; n > depth && parms; --n)
3028 parms = TREE_CHAIN (parms);
3029 if (!parms)
3030 return type;
3031 tree cur_reqs = TEMPLATE_PARMS_CONSTRAINTS (parms);
3032 tree cur_constr = build_constraints (cur_reqs, NULL_TREE);
3034 // Search for a specialization whose type and constraints match.
3035 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
3036 tree specs = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
3037 while (specs)
3039 tree spec_constr = get_constraints (TREE_VALUE (specs));
3041 // If the type and constraints match a specialization, then we
3042 // are entering that type.
3043 if (same_type_p (type, TREE_TYPE (specs))
3044 && equivalent_constraints (cur_constr, spec_constr))
3045 return TREE_TYPE (specs);
3046 specs = TREE_CHAIN (specs);
3049 // If no specialization matches, then must return the type
3050 // previously found.
3051 return type;
3054 /* Finish processing a template-id (which names a type) of the form
3055 NAME < ARGS >. Return the TYPE_DECL for the type named by the
3056 template-id. If ENTERING_SCOPE is nonzero we are about to enter
3057 the scope of template-id indicated. */
3059 tree
3060 finish_template_type (tree name, tree args, int entering_scope)
3062 tree type;
3064 type = lookup_template_class (name, args,
3065 NULL_TREE, NULL_TREE, entering_scope,
3066 tf_warning_or_error | tf_user);
3068 /* If we might be entering the scope of a partial specialization,
3069 find the one with the right constraints. */
3070 if (flag_concepts
3071 && entering_scope
3072 && CLASS_TYPE_P (type)
3073 && dependent_type_p (type)
3074 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
3075 type = fixup_template_type (type);
3077 if (type == error_mark_node)
3078 return type;
3079 else if (CLASS_TYPE_P (type) && !alias_type_or_template_p (type))
3080 return TYPE_STUB_DECL (type);
3081 else
3082 return TYPE_NAME (type);
3085 /* Finish processing a BASE_CLASS with the indicated ACCESS_SPECIFIER.
3086 Return a TREE_LIST containing the ACCESS_SPECIFIER and the
3087 BASE_CLASS, or NULL_TREE if an error occurred. The
3088 ACCESS_SPECIFIER is one of
3089 access_{default,public,protected_private}_node. For a virtual base
3090 we set TREE_TYPE. */
3092 tree
3093 finish_base_specifier (tree base, tree access, bool virtual_p)
3095 tree result;
3097 if (base == error_mark_node)
3099 error ("invalid base-class specification");
3100 result = NULL_TREE;
3102 else if (! MAYBE_CLASS_TYPE_P (base))
3104 error ("%qT is not a class type", base);
3105 result = NULL_TREE;
3107 else
3109 if (cp_type_quals (base) != 0)
3111 /* DR 484: Can a base-specifier name a cv-qualified
3112 class type? */
3113 base = TYPE_MAIN_VARIANT (base);
3115 result = build_tree_list (access, base);
3116 if (virtual_p)
3117 TREE_TYPE (result) = integer_type_node;
3120 return result;
3123 /* If FNS is a member function, a set of member functions, or a
3124 template-id referring to one or more member functions, return a
3125 BASELINK for FNS, incorporating the current access context.
3126 Otherwise, return FNS unchanged. */
3128 tree
3129 baselink_for_fns (tree fns)
3131 tree scope;
3132 tree cl;
3134 if (BASELINK_P (fns)
3135 || error_operand_p (fns))
3136 return fns;
3138 scope = ovl_scope (fns);
3139 if (!CLASS_TYPE_P (scope))
3140 return fns;
3142 cl = currently_open_derived_class (scope);
3143 if (!cl)
3144 cl = scope;
3145 cl = TYPE_BINFO (cl);
3146 return build_baselink (cl, cl, fns, /*optype=*/NULL_TREE);
3149 /* Returns true iff DECL is a variable from a function outside
3150 the current one. */
3152 static bool
3153 outer_var_p (tree decl)
3155 return ((VAR_P (decl) || TREE_CODE (decl) == PARM_DECL)
3156 && DECL_FUNCTION_SCOPE_P (decl)
3157 && (DECL_CONTEXT (decl) != current_function_decl
3158 || parsing_nsdmi ()));
3161 /* As above, but also checks that DECL is automatic. */
3163 bool
3164 outer_automatic_var_p (tree decl)
3166 return (outer_var_p (decl)
3167 && !TREE_STATIC (decl));
3170 /* DECL satisfies outer_automatic_var_p. Possibly complain about it or
3171 rewrite it for lambda capture. */
3173 tree
3174 process_outer_var_ref (tree decl, tsubst_flags_t complain)
3176 if (cp_unevaluated_operand)
3177 /* It's not a use (3.2) if we're in an unevaluated context. */
3178 return decl;
3179 if (decl == error_mark_node)
3180 return decl;
3182 tree context = DECL_CONTEXT (decl);
3183 tree containing_function = current_function_decl;
3184 tree lambda_stack = NULL_TREE;
3185 tree lambda_expr = NULL_TREE;
3186 tree initializer = convert_from_reference (decl);
3188 /* Mark it as used now even if the use is ill-formed. */
3189 if (!mark_used (decl, complain) && !(complain & tf_error))
3190 return error_mark_node;
3192 /* Core issue 696: "[At the July 2009 meeting] the CWG expressed
3193 support for an approach in which a reference to a local
3194 [constant] automatic variable in a nested class or lambda body
3195 would enter the expression as an rvalue, which would reduce
3196 the complexity of the problem"
3198 FIXME update for final resolution of core issue 696. */
3199 if (decl_maybe_constant_var_p (decl))
3201 if (processing_template_decl)
3202 /* In a template, the constant value may not be in a usable
3203 form, so wait until instantiation time. */
3204 return decl;
3205 else if (decl_constant_var_p (decl))
3207 tree t = maybe_constant_value (convert_from_reference (decl));
3208 if (TREE_CONSTANT (t))
3209 return t;
3213 if (parsing_nsdmi ())
3214 containing_function = NULL_TREE;
3215 else
3216 /* If we are in a lambda function, we can move out until we hit
3217 1. the context,
3218 2. a non-lambda function, or
3219 3. a non-default capturing lambda function. */
3220 while (context != containing_function
3221 && LAMBDA_FUNCTION_P (containing_function))
3223 tree closure = DECL_CONTEXT (containing_function);
3224 lambda_expr = CLASSTYPE_LAMBDA_EXPR (closure);
3226 if (TYPE_CLASS_SCOPE_P (closure))
3227 /* A lambda in an NSDMI (c++/64496). */
3228 break;
3230 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr)
3231 == CPLD_NONE)
3232 break;
3234 lambda_stack = tree_cons (NULL_TREE,
3235 lambda_expr,
3236 lambda_stack);
3238 containing_function
3239 = decl_function_context (containing_function);
3242 if (lambda_expr && VAR_P (decl)
3243 && DECL_ANON_UNION_VAR_P (decl))
3245 if (complain & tf_error)
3246 error ("cannot capture member %qD of anonymous union", decl);
3247 return error_mark_node;
3249 if (context == containing_function)
3251 decl = add_default_capture (lambda_stack,
3252 /*id=*/DECL_NAME (decl),
3253 initializer);
3255 else if (lambda_expr)
3257 if (complain & tf_error)
3259 error ("%qD is not captured", decl);
3260 tree closure = LAMBDA_EXPR_CLOSURE (lambda_expr);
3261 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr)
3262 == CPLD_NONE)
3263 inform (location_of (closure),
3264 "the lambda has no capture-default");
3265 else if (TYPE_CLASS_SCOPE_P (closure))
3266 inform (0, "lambda in local class %q+T cannot "
3267 "capture variables from the enclosing context",
3268 TYPE_CONTEXT (closure));
3269 inform (DECL_SOURCE_LOCATION (decl), "%q#D declared here", decl);
3271 return error_mark_node;
3273 else
3275 if (complain & tf_error)
3276 error (VAR_P (decl)
3277 ? G_("use of local variable with automatic storage from containing function")
3278 : G_("use of parameter from containing function"));
3279 inform (DECL_SOURCE_LOCATION (decl), "%q#D declared here", decl);
3280 return error_mark_node;
3282 return decl;
3285 /* ID_EXPRESSION is a representation of parsed, but unprocessed,
3286 id-expression. (See cp_parser_id_expression for details.) SCOPE,
3287 if non-NULL, is the type or namespace used to explicitly qualify
3288 ID_EXPRESSION. DECL is the entity to which that name has been
3289 resolved.
3291 *CONSTANT_EXPRESSION_P is true if we are presently parsing a
3292 constant-expression. In that case, *NON_CONSTANT_EXPRESSION_P will
3293 be set to true if this expression isn't permitted in a
3294 constant-expression, but it is otherwise not set by this function.
3295 *ALLOW_NON_CONSTANT_EXPRESSION_P is true if we are parsing a
3296 constant-expression, but a non-constant expression is also
3297 permissible.
3299 DONE is true if this expression is a complete postfix-expression;
3300 it is false if this expression is followed by '->', '[', '(', etc.
3301 ADDRESS_P is true iff this expression is the operand of '&'.
3302 TEMPLATE_P is true iff the qualified-id was of the form
3303 "A::template B". TEMPLATE_ARG_P is true iff this qualified name
3304 appears as a template argument.
3306 If an error occurs, and it is the kind of error that might cause
3307 the parser to abort a tentative parse, *ERROR_MSG is filled in. It
3308 is the caller's responsibility to issue the message. *ERROR_MSG
3309 will be a string with static storage duration, so the caller need
3310 not "free" it.
3312 Return an expression for the entity, after issuing appropriate
3313 diagnostics. This function is also responsible for transforming a
3314 reference to a non-static member into a COMPONENT_REF that makes
3315 the use of "this" explicit.
3317 Upon return, *IDK will be filled in appropriately. */
3318 tree
3319 finish_id_expression (tree id_expression,
3320 tree decl,
3321 tree scope,
3322 cp_id_kind *idk,
3323 bool integral_constant_expression_p,
3324 bool allow_non_integral_constant_expression_p,
3325 bool *non_integral_constant_expression_p,
3326 bool template_p,
3327 bool done,
3328 bool address_p,
3329 bool template_arg_p,
3330 const char **error_msg,
3331 location_t location)
3333 decl = strip_using_decl (decl);
3335 /* Initialize the output parameters. */
3336 *idk = CP_ID_KIND_NONE;
3337 *error_msg = NULL;
3339 if (id_expression == error_mark_node)
3340 return error_mark_node;
3341 /* If we have a template-id, then no further lookup is
3342 required. If the template-id was for a template-class, we
3343 will sometimes have a TYPE_DECL at this point. */
3344 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3345 || TREE_CODE (decl) == TYPE_DECL)
3347 /* Look up the name. */
3348 else
3350 if (decl == error_mark_node)
3352 /* Name lookup failed. */
3353 if (scope
3354 && (!TYPE_P (scope)
3355 || (!dependent_type_p (scope)
3356 && !(identifier_p (id_expression)
3357 && IDENTIFIER_TYPENAME_P (id_expression)
3358 && dependent_type_p (TREE_TYPE (id_expression))))))
3360 /* If the qualifying type is non-dependent (and the name
3361 does not name a conversion operator to a dependent
3362 type), issue an error. */
3363 qualified_name_lookup_error (scope, id_expression, decl, location);
3364 return error_mark_node;
3366 else if (!scope)
3368 /* It may be resolved via Koenig lookup. */
3369 *idk = CP_ID_KIND_UNQUALIFIED;
3370 return id_expression;
3372 else
3373 decl = id_expression;
3375 /* If DECL is a variable that would be out of scope under
3376 ANSI/ISO rules, but in scope in the ARM, name lookup
3377 will succeed. Issue a diagnostic here. */
3378 else
3379 decl = check_for_out_of_scope_variable (decl);
3381 /* Remember that the name was used in the definition of
3382 the current class so that we can check later to see if
3383 the meaning would have been different after the class
3384 was entirely defined. */
3385 if (!scope && decl != error_mark_node && identifier_p (id_expression))
3386 maybe_note_name_used_in_class (id_expression, decl);
3388 /* Disallow uses of local variables from containing functions, except
3389 within lambda-expressions. */
3390 if (outer_automatic_var_p (decl))
3392 decl = process_outer_var_ref (decl, tf_warning_or_error);
3393 if (decl == error_mark_node)
3394 return error_mark_node;
3397 /* Also disallow uses of function parameters outside the function
3398 body, except inside an unevaluated context (i.e. decltype). */
3399 if (TREE_CODE (decl) == PARM_DECL
3400 && DECL_CONTEXT (decl) == NULL_TREE
3401 && !cp_unevaluated_operand)
3403 *error_msg = "use of parameter outside function body";
3404 return error_mark_node;
3408 /* If we didn't find anything, or what we found was a type,
3409 then this wasn't really an id-expression. */
3410 if (TREE_CODE (decl) == TEMPLATE_DECL
3411 && !DECL_FUNCTION_TEMPLATE_P (decl))
3413 *error_msg = "missing template arguments";
3414 return error_mark_node;
3416 else if (TREE_CODE (decl) == TYPE_DECL
3417 || TREE_CODE (decl) == NAMESPACE_DECL)
3419 *error_msg = "expected primary-expression";
3420 return error_mark_node;
3423 /* If the name resolved to a template parameter, there is no
3424 need to look it up again later. */
3425 if ((TREE_CODE (decl) == CONST_DECL && DECL_TEMPLATE_PARM_P (decl))
3426 || TREE_CODE (decl) == TEMPLATE_PARM_INDEX)
3428 tree r;
3430 *idk = CP_ID_KIND_NONE;
3431 if (TREE_CODE (decl) == TEMPLATE_PARM_INDEX)
3432 decl = TEMPLATE_PARM_DECL (decl);
3433 r = convert_from_reference (DECL_INITIAL (decl));
3435 if (integral_constant_expression_p
3436 && !dependent_type_p (TREE_TYPE (decl))
3437 && !(INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (r))))
3439 if (!allow_non_integral_constant_expression_p)
3440 error ("template parameter %qD of type %qT is not allowed in "
3441 "an integral constant expression because it is not of "
3442 "integral or enumeration type", decl, TREE_TYPE (decl));
3443 *non_integral_constant_expression_p = true;
3445 return r;
3447 else
3449 bool dependent_p = type_dependent_expression_p (decl);
3451 /* If the declaration was explicitly qualified indicate
3452 that. The semantics of `A::f(3)' are different than
3453 `f(3)' if `f' is virtual. */
3454 *idk = (scope
3455 ? CP_ID_KIND_QUALIFIED
3456 : (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3457 ? CP_ID_KIND_TEMPLATE_ID
3458 : (dependent_p
3459 ? CP_ID_KIND_UNQUALIFIED_DEPENDENT
3460 : CP_ID_KIND_UNQUALIFIED)));
3462 /* If the name was dependent on a template parameter, we will
3463 resolve the name at instantiation time. */
3464 if (dependent_p)
3466 /* If we found a variable, then name lookup during the
3467 instantiation will always resolve to the same VAR_DECL
3468 (or an instantiation thereof). */
3469 if (VAR_P (decl)
3470 || TREE_CODE (decl) == CONST_DECL
3471 || TREE_CODE (decl) == PARM_DECL)
3473 mark_used (decl);
3474 return convert_from_reference (decl);
3477 /* Create a SCOPE_REF for qualified names, if the scope is
3478 dependent. */
3479 if (scope)
3481 if (TYPE_P (scope))
3483 if (address_p && done)
3484 decl = finish_qualified_id_expr (scope, decl,
3485 done, address_p,
3486 template_p,
3487 template_arg_p,
3488 tf_warning_or_error);
3489 else
3491 tree type = NULL_TREE;
3492 if (DECL_P (decl) && !dependent_scope_p (scope))
3493 type = TREE_TYPE (decl);
3494 decl = build_qualified_name (type,
3495 scope,
3496 id_expression,
3497 template_p);
3500 if (TREE_TYPE (decl))
3501 decl = convert_from_reference (decl);
3502 return decl;
3504 /* A TEMPLATE_ID already contains all the information we
3505 need. */
3506 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR)
3507 return id_expression;
3508 /* The same is true for FIELD_DECL, but we also need to
3509 make sure that the syntax is correct. */
3510 else if (TREE_CODE (decl) == FIELD_DECL)
3512 /* Since SCOPE is NULL here, this is an unqualified name.
3513 Access checking has been performed during name lookup
3514 already. Turn off checking to avoid duplicate errors. */
3515 push_deferring_access_checks (dk_no_check);
3516 decl = finish_non_static_data_member
3517 (decl, NULL_TREE,
3518 /*qualifying_scope=*/NULL_TREE);
3519 pop_deferring_access_checks ();
3520 return decl;
3522 return id_expression;
3525 if (TREE_CODE (decl) == NAMESPACE_DECL)
3527 error ("use of namespace %qD as expression", decl);
3528 return error_mark_node;
3530 else if (DECL_CLASS_TEMPLATE_P (decl))
3532 error ("use of class template %qT as expression", decl);
3533 return error_mark_node;
3535 else if (TREE_CODE (decl) == TREE_LIST)
3537 /* Ambiguous reference to base members. */
3538 error ("request for member %qD is ambiguous in "
3539 "multiple inheritance lattice", id_expression);
3540 print_candidates (decl);
3541 return error_mark_node;
3544 /* Mark variable-like entities as used. Functions are similarly
3545 marked either below or after overload resolution. */
3546 if ((VAR_P (decl)
3547 || TREE_CODE (decl) == PARM_DECL
3548 || TREE_CODE (decl) == CONST_DECL
3549 || TREE_CODE (decl) == RESULT_DECL)
3550 && !mark_used (decl))
3551 return error_mark_node;
3553 /* Only certain kinds of names are allowed in constant
3554 expression. Template parameters have already
3555 been handled above. */
3556 if (! error_operand_p (decl)
3557 && integral_constant_expression_p
3558 && ! decl_constant_var_p (decl)
3559 && TREE_CODE (decl) != CONST_DECL
3560 && ! builtin_valid_in_constant_expr_p (decl))
3562 if (!allow_non_integral_constant_expression_p)
3564 error ("%qD cannot appear in a constant-expression", decl);
3565 return error_mark_node;
3567 *non_integral_constant_expression_p = true;
3570 tree wrap;
3571 if (VAR_P (decl)
3572 && !cp_unevaluated_operand
3573 && !processing_template_decl
3574 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl))
3575 && CP_DECL_THREAD_LOCAL_P (decl)
3576 && (wrap = get_tls_wrapper_fn (decl)))
3578 /* Replace an evaluated use of the thread_local variable with
3579 a call to its wrapper. */
3580 decl = build_cxx_call (wrap, 0, NULL, tf_warning_or_error);
3582 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3583 && variable_template_p (TREE_OPERAND (decl, 0)))
3585 decl = finish_template_variable (decl);
3586 mark_used (decl);
3587 decl = convert_from_reference (decl);
3589 else if (scope)
3591 decl = (adjust_result_of_qualified_name_lookup
3592 (decl, scope, current_nonlambda_class_type()));
3594 if (TREE_CODE (decl) == FUNCTION_DECL)
3595 mark_used (decl);
3597 if (TYPE_P (scope))
3598 decl = finish_qualified_id_expr (scope,
3599 decl,
3600 done,
3601 address_p,
3602 template_p,
3603 template_arg_p,
3604 tf_warning_or_error);
3605 else
3606 decl = convert_from_reference (decl);
3608 else if (TREE_CODE (decl) == FIELD_DECL)
3610 /* Since SCOPE is NULL here, this is an unqualified name.
3611 Access checking has been performed during name lookup
3612 already. Turn off checking to avoid duplicate errors. */
3613 push_deferring_access_checks (dk_no_check);
3614 decl = finish_non_static_data_member (decl, NULL_TREE,
3615 /*qualifying_scope=*/NULL_TREE);
3616 pop_deferring_access_checks ();
3618 else if (is_overloaded_fn (decl))
3620 tree first_fn;
3622 first_fn = get_first_fn (decl);
3623 if (TREE_CODE (first_fn) == TEMPLATE_DECL)
3624 first_fn = DECL_TEMPLATE_RESULT (first_fn);
3626 if (!really_overloaded_fn (decl)
3627 && !mark_used (first_fn))
3628 return error_mark_node;
3630 if (!template_arg_p
3631 && TREE_CODE (first_fn) == FUNCTION_DECL
3632 && DECL_FUNCTION_MEMBER_P (first_fn)
3633 && !shared_member_p (decl))
3635 /* A set of member functions. */
3636 decl = maybe_dummy_object (DECL_CONTEXT (first_fn), 0);
3637 return finish_class_member_access_expr (decl, id_expression,
3638 /*template_p=*/false,
3639 tf_warning_or_error);
3642 decl = baselink_for_fns (decl);
3644 else
3646 if (DECL_P (decl) && DECL_NONLOCAL (decl)
3647 && DECL_CLASS_SCOPE_P (decl))
3649 tree context = context_for_name_lookup (decl);
3650 if (context != current_class_type)
3652 tree path = currently_open_derived_class (context);
3653 perform_or_defer_access_check (TYPE_BINFO (path),
3654 decl, decl,
3655 tf_warning_or_error);
3659 decl = convert_from_reference (decl);
3663 return decl;
3666 /* Implement the __typeof keyword: Return the type of EXPR, suitable for
3667 use as a type-specifier. */
3669 tree
3670 finish_typeof (tree expr)
3672 tree type;
3674 if (type_dependent_expression_p (expr))
3676 type = cxx_make_type (TYPEOF_TYPE);
3677 TYPEOF_TYPE_EXPR (type) = expr;
3678 SET_TYPE_STRUCTURAL_EQUALITY (type);
3680 return type;
3683 expr = mark_type_use (expr);
3685 type = unlowered_expr_type (expr);
3687 if (!type || type == unknown_type_node)
3689 error ("type of %qE is unknown", expr);
3690 return error_mark_node;
3693 return type;
3696 /* Implement the __underlying_type keyword: Return the underlying
3697 type of TYPE, suitable for use as a type-specifier. */
3699 tree
3700 finish_underlying_type (tree type)
3702 tree underlying_type;
3704 if (processing_template_decl)
3706 underlying_type = cxx_make_type (UNDERLYING_TYPE);
3707 UNDERLYING_TYPE_TYPE (underlying_type) = type;
3708 SET_TYPE_STRUCTURAL_EQUALITY (underlying_type);
3710 return underlying_type;
3713 complete_type (type);
3715 if (TREE_CODE (type) != ENUMERAL_TYPE)
3717 error ("%qT is not an enumeration type", type);
3718 return error_mark_node;
3721 underlying_type = ENUM_UNDERLYING_TYPE (type);
3723 /* Fixup necessary in this case because ENUM_UNDERLYING_TYPE
3724 includes TYPE_MIN_VALUE and TYPE_MAX_VALUE information.
3725 See finish_enum_value_list for details. */
3726 if (!ENUM_FIXED_UNDERLYING_TYPE_P (type))
3727 underlying_type
3728 = c_common_type_for_mode (TYPE_MODE (underlying_type),
3729 TYPE_UNSIGNED (underlying_type));
3731 return underlying_type;
3734 /* Implement the __direct_bases keyword: Return the direct base classes
3735 of type */
3737 tree
3738 calculate_direct_bases (tree type)
3740 vec<tree, va_gc> *vector = make_tree_vector();
3741 tree bases_vec = NULL_TREE;
3742 vec<tree, va_gc> *base_binfos;
3743 tree binfo;
3744 unsigned i;
3746 complete_type (type);
3748 if (!NON_UNION_CLASS_TYPE_P (type))
3749 return make_tree_vec (0);
3751 base_binfos = BINFO_BASE_BINFOS (TYPE_BINFO (type));
3753 /* Virtual bases are initialized first */
3754 for (i = 0; base_binfos->iterate (i, &binfo); i++)
3756 if (BINFO_VIRTUAL_P (binfo))
3758 vec_safe_push (vector, binfo);
3762 /* Now non-virtuals */
3763 for (i = 0; base_binfos->iterate (i, &binfo); i++)
3765 if (!BINFO_VIRTUAL_P (binfo))
3767 vec_safe_push (vector, binfo);
3772 bases_vec = make_tree_vec (vector->length ());
3774 for (i = 0; i < vector->length (); ++i)
3776 TREE_VEC_ELT (bases_vec, i) = BINFO_TYPE ((*vector)[i]);
3778 return bases_vec;
3781 /* Implement the __bases keyword: Return the base classes
3782 of type */
3784 /* Find morally non-virtual base classes by walking binfo hierarchy */
3785 /* Virtual base classes are handled separately in finish_bases */
3787 static tree
3788 dfs_calculate_bases_pre (tree binfo, void * /*data_*/)
3790 /* Don't walk bases of virtual bases */
3791 return BINFO_VIRTUAL_P (binfo) ? dfs_skip_bases : NULL_TREE;
3794 static tree
3795 dfs_calculate_bases_post (tree binfo, void *data_)
3797 vec<tree, va_gc> **data = ((vec<tree, va_gc> **) data_);
3798 if (!BINFO_VIRTUAL_P (binfo))
3800 vec_safe_push (*data, BINFO_TYPE (binfo));
3802 return NULL_TREE;
3805 /* Calculates the morally non-virtual base classes of a class */
3806 static vec<tree, va_gc> *
3807 calculate_bases_helper (tree type)
3809 vec<tree, va_gc> *vector = make_tree_vector();
3811 /* Now add non-virtual base classes in order of construction */
3812 dfs_walk_all (TYPE_BINFO (type),
3813 dfs_calculate_bases_pre, dfs_calculate_bases_post, &vector);
3814 return vector;
3817 tree
3818 calculate_bases (tree type)
3820 vec<tree, va_gc> *vector = make_tree_vector();
3821 tree bases_vec = NULL_TREE;
3822 unsigned i;
3823 vec<tree, va_gc> *vbases;
3824 vec<tree, va_gc> *nonvbases;
3825 tree binfo;
3827 complete_type (type);
3829 if (!NON_UNION_CLASS_TYPE_P (type))
3830 return make_tree_vec (0);
3832 /* First go through virtual base classes */
3833 for (vbases = CLASSTYPE_VBASECLASSES (type), i = 0;
3834 vec_safe_iterate (vbases, i, &binfo); i++)
3836 vec<tree, va_gc> *vbase_bases;
3837 vbase_bases = calculate_bases_helper (BINFO_TYPE (binfo));
3838 vec_safe_splice (vector, vbase_bases);
3839 release_tree_vector (vbase_bases);
3842 /* Now for the non-virtual bases */
3843 nonvbases = calculate_bases_helper (type);
3844 vec_safe_splice (vector, nonvbases);
3845 release_tree_vector (nonvbases);
3847 /* Last element is entire class, so don't copy */
3848 bases_vec = make_tree_vec (vector->length () - 1);
3850 for (i = 0; i < vector->length () - 1; ++i)
3852 TREE_VEC_ELT (bases_vec, i) = (*vector)[i];
3854 release_tree_vector (vector);
3855 return bases_vec;
3858 tree
3859 finish_bases (tree type, bool direct)
3861 tree bases = NULL_TREE;
3863 if (!processing_template_decl)
3865 /* Parameter packs can only be used in templates */
3866 error ("Parameter pack __bases only valid in template declaration");
3867 return error_mark_node;
3870 bases = cxx_make_type (BASES);
3871 BASES_TYPE (bases) = type;
3872 BASES_DIRECT (bases) = direct;
3873 SET_TYPE_STRUCTURAL_EQUALITY (bases);
3875 return bases;
3878 /* Perform C++-specific checks for __builtin_offsetof before calling
3879 fold_offsetof. */
3881 tree
3882 finish_offsetof (tree expr, location_t loc)
3884 /* If we're processing a template, we can't finish the semantics yet.
3885 Otherwise we can fold the entire expression now. */
3886 if (processing_template_decl)
3888 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
3889 SET_EXPR_LOCATION (expr, loc);
3890 return expr;
3893 if (TREE_CODE (expr) == PSEUDO_DTOR_EXPR)
3895 error ("cannot apply %<offsetof%> to destructor %<~%T%>",
3896 TREE_OPERAND (expr, 2));
3897 return error_mark_node;
3899 if (TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE
3900 || TREE_CODE (TREE_TYPE (expr)) == METHOD_TYPE
3901 || TREE_TYPE (expr) == unknown_type_node)
3903 if (INDIRECT_REF_P (expr))
3904 error ("second operand of %<offsetof%> is neither a single "
3905 "identifier nor a sequence of member accesses and "
3906 "array references");
3907 else
3909 if (TREE_CODE (expr) == COMPONENT_REF
3910 || TREE_CODE (expr) == COMPOUND_EXPR)
3911 expr = TREE_OPERAND (expr, 1);
3912 error ("cannot apply %<offsetof%> to member function %qD", expr);
3914 return error_mark_node;
3916 if (REFERENCE_REF_P (expr))
3917 expr = TREE_OPERAND (expr, 0);
3918 if (TREE_CODE (expr) == COMPONENT_REF)
3920 tree object = TREE_OPERAND (expr, 0);
3921 if (!complete_type_or_else (TREE_TYPE (object), object))
3922 return error_mark_node;
3923 if (warn_invalid_offsetof
3924 && CLASS_TYPE_P (TREE_TYPE (object))
3925 && CLASSTYPE_NON_STD_LAYOUT (TREE_TYPE (object))
3926 && cp_unevaluated_operand == 0)
3927 pedwarn (loc, OPT_Winvalid_offsetof,
3928 "offsetof within non-standard-layout type %qT is undefined",
3929 TREE_TYPE (object));
3931 return fold_offsetof (expr);
3934 /* Replace the AGGR_INIT_EXPR at *TP with an equivalent CALL_EXPR. This
3935 function is broken out from the above for the benefit of the tree-ssa
3936 project. */
3938 void
3939 simplify_aggr_init_expr (tree *tp)
3941 tree aggr_init_expr = *tp;
3943 /* Form an appropriate CALL_EXPR. */
3944 tree fn = AGGR_INIT_EXPR_FN (aggr_init_expr);
3945 tree slot = AGGR_INIT_EXPR_SLOT (aggr_init_expr);
3946 tree type = TREE_TYPE (slot);
3948 tree call_expr;
3949 enum style_t { ctor, arg, pcc } style;
3951 if (AGGR_INIT_VIA_CTOR_P (aggr_init_expr))
3952 style = ctor;
3953 #ifdef PCC_STATIC_STRUCT_RETURN
3954 else if (1)
3955 style = pcc;
3956 #endif
3957 else
3959 gcc_assert (TREE_ADDRESSABLE (type));
3960 style = arg;
3963 call_expr = build_call_array_loc (input_location,
3964 TREE_TYPE (TREE_TYPE (TREE_TYPE (fn))),
3966 aggr_init_expr_nargs (aggr_init_expr),
3967 AGGR_INIT_EXPR_ARGP (aggr_init_expr));
3968 TREE_NOTHROW (call_expr) = TREE_NOTHROW (aggr_init_expr);
3969 CALL_EXPR_LIST_INIT_P (call_expr) = CALL_EXPR_LIST_INIT_P (aggr_init_expr);
3971 if (style == ctor)
3973 /* Replace the first argument to the ctor with the address of the
3974 slot. */
3975 cxx_mark_addressable (slot);
3976 CALL_EXPR_ARG (call_expr, 0) =
3977 build1 (ADDR_EXPR, build_pointer_type (type), slot);
3979 else if (style == arg)
3981 /* Just mark it addressable here, and leave the rest to
3982 expand_call{,_inline}. */
3983 cxx_mark_addressable (slot);
3984 CALL_EXPR_RETURN_SLOT_OPT (call_expr) = true;
3985 call_expr = build2 (INIT_EXPR, TREE_TYPE (call_expr), slot, call_expr);
3987 else if (style == pcc)
3989 /* If we're using the non-reentrant PCC calling convention, then we
3990 need to copy the returned value out of the static buffer into the
3991 SLOT. */
3992 push_deferring_access_checks (dk_no_check);
3993 call_expr = build_aggr_init (slot, call_expr,
3994 DIRECT_BIND | LOOKUP_ONLYCONVERTING,
3995 tf_warning_or_error);
3996 pop_deferring_access_checks ();
3997 call_expr = build2 (COMPOUND_EXPR, TREE_TYPE (slot), call_expr, slot);
4000 if (AGGR_INIT_ZERO_FIRST (aggr_init_expr))
4002 tree init = build_zero_init (type, NULL_TREE,
4003 /*static_storage_p=*/false);
4004 init = build2 (INIT_EXPR, void_type_node, slot, init);
4005 call_expr = build2 (COMPOUND_EXPR, TREE_TYPE (call_expr),
4006 init, call_expr);
4009 *tp = call_expr;
4012 /* Emit all thunks to FN that should be emitted when FN is emitted. */
4014 void
4015 emit_associated_thunks (tree fn)
4017 /* When we use vcall offsets, we emit thunks with the virtual
4018 functions to which they thunk. The whole point of vcall offsets
4019 is so that you can know statically the entire set of thunks that
4020 will ever be needed for a given virtual function, thereby
4021 enabling you to output all the thunks with the function itself. */
4022 if (DECL_VIRTUAL_P (fn)
4023 /* Do not emit thunks for extern template instantiations. */
4024 && ! DECL_REALLY_EXTERN (fn))
4026 tree thunk;
4028 for (thunk = DECL_THUNKS (fn); thunk; thunk = DECL_CHAIN (thunk))
4030 if (!THUNK_ALIAS (thunk))
4032 use_thunk (thunk, /*emit_p=*/1);
4033 if (DECL_RESULT_THUNK_P (thunk))
4035 tree probe;
4037 for (probe = DECL_THUNKS (thunk);
4038 probe; probe = DECL_CHAIN (probe))
4039 use_thunk (probe, /*emit_p=*/1);
4042 else
4043 gcc_assert (!DECL_THUNKS (thunk));
4048 /* Generate RTL for FN. */
4050 bool
4051 expand_or_defer_fn_1 (tree fn)
4053 /* When the parser calls us after finishing the body of a template
4054 function, we don't really want to expand the body. */
4055 if (processing_template_decl)
4057 /* Normally, collection only occurs in rest_of_compilation. So,
4058 if we don't collect here, we never collect junk generated
4059 during the processing of templates until we hit a
4060 non-template function. It's not safe to do this inside a
4061 nested class, though, as the parser may have local state that
4062 is not a GC root. */
4063 if (!function_depth)
4064 ggc_collect ();
4065 return false;
4068 gcc_assert (DECL_SAVED_TREE (fn));
4070 /* We make a decision about linkage for these functions at the end
4071 of the compilation. Until that point, we do not want the back
4072 end to output them -- but we do want it to see the bodies of
4073 these functions so that it can inline them as appropriate. */
4074 if (DECL_DECLARED_INLINE_P (fn) || DECL_IMPLICIT_INSTANTIATION (fn))
4076 if (DECL_INTERFACE_KNOWN (fn))
4077 /* We've already made a decision as to how this function will
4078 be handled. */;
4079 else if (!at_eof)
4080 tentative_decl_linkage (fn);
4081 else
4082 import_export_decl (fn);
4084 /* If the user wants us to keep all inline functions, then mark
4085 this function as needed so that finish_file will make sure to
4086 output it later. Similarly, all dllexport'd functions must
4087 be emitted; there may be callers in other DLLs. */
4088 if (DECL_DECLARED_INLINE_P (fn)
4089 && !DECL_REALLY_EXTERN (fn)
4090 && (flag_keep_inline_functions
4091 || (flag_keep_inline_dllexport
4092 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (fn)))))
4094 mark_needed (fn);
4095 DECL_EXTERNAL (fn) = 0;
4099 /* If this is a constructor or destructor body, we have to clone
4100 it. */
4101 if (maybe_clone_body (fn))
4103 /* We don't want to process FN again, so pretend we've written
4104 it out, even though we haven't. */
4105 TREE_ASM_WRITTEN (fn) = 1;
4106 /* If this is an instantiation of a constexpr function, keep
4107 DECL_SAVED_TREE for explain_invalid_constexpr_fn. */
4108 if (!is_instantiation_of_constexpr (fn))
4109 DECL_SAVED_TREE (fn) = NULL_TREE;
4110 return false;
4113 /* There's no reason to do any of the work here if we're only doing
4114 semantic analysis; this code just generates RTL. */
4115 if (flag_syntax_only)
4116 return false;
4118 return true;
4121 void
4122 expand_or_defer_fn (tree fn)
4124 if (expand_or_defer_fn_1 (fn))
4126 function_depth++;
4128 /* Expand or defer, at the whim of the compilation unit manager. */
4129 cgraph_node::finalize_function (fn, function_depth > 1);
4130 emit_associated_thunks (fn);
4132 function_depth--;
4136 struct nrv_data
4138 nrv_data () : visited (37) {}
4140 tree var;
4141 tree result;
4142 hash_table<nofree_ptr_hash <tree_node> > visited;
4145 /* Helper function for walk_tree, used by finalize_nrv below. */
4147 static tree
4148 finalize_nrv_r (tree* tp, int* walk_subtrees, void* data)
4150 struct nrv_data *dp = (struct nrv_data *)data;
4151 tree_node **slot;
4153 /* No need to walk into types. There wouldn't be any need to walk into
4154 non-statements, except that we have to consider STMT_EXPRs. */
4155 if (TYPE_P (*tp))
4156 *walk_subtrees = 0;
4157 /* Change all returns to just refer to the RESULT_DECL; this is a nop,
4158 but differs from using NULL_TREE in that it indicates that we care
4159 about the value of the RESULT_DECL. */
4160 else if (TREE_CODE (*tp) == RETURN_EXPR)
4161 TREE_OPERAND (*tp, 0) = dp->result;
4162 /* Change all cleanups for the NRV to only run when an exception is
4163 thrown. */
4164 else if (TREE_CODE (*tp) == CLEANUP_STMT
4165 && CLEANUP_DECL (*tp) == dp->var)
4166 CLEANUP_EH_ONLY (*tp) = 1;
4167 /* Replace the DECL_EXPR for the NRV with an initialization of the
4168 RESULT_DECL, if needed. */
4169 else if (TREE_CODE (*tp) == DECL_EXPR
4170 && DECL_EXPR_DECL (*tp) == dp->var)
4172 tree init;
4173 if (DECL_INITIAL (dp->var)
4174 && DECL_INITIAL (dp->var) != error_mark_node)
4175 init = build2 (INIT_EXPR, void_type_node, dp->result,
4176 DECL_INITIAL (dp->var));
4177 else
4178 init = build_empty_stmt (EXPR_LOCATION (*tp));
4179 DECL_INITIAL (dp->var) = NULL_TREE;
4180 SET_EXPR_LOCATION (init, EXPR_LOCATION (*tp));
4181 *tp = init;
4183 /* And replace all uses of the NRV with the RESULT_DECL. */
4184 else if (*tp == dp->var)
4185 *tp = dp->result;
4187 /* Avoid walking into the same tree more than once. Unfortunately, we
4188 can't just use walk_tree_without duplicates because it would only call
4189 us for the first occurrence of dp->var in the function body. */
4190 slot = dp->visited.find_slot (*tp, INSERT);
4191 if (*slot)
4192 *walk_subtrees = 0;
4193 else
4194 *slot = *tp;
4196 /* Keep iterating. */
4197 return NULL_TREE;
4200 /* Called from finish_function to implement the named return value
4201 optimization by overriding all the RETURN_EXPRs and pertinent
4202 CLEANUP_STMTs and replacing all occurrences of VAR with RESULT, the
4203 RESULT_DECL for the function. */
4205 void
4206 finalize_nrv (tree *tp, tree var, tree result)
4208 struct nrv_data data;
4210 /* Copy name from VAR to RESULT. */
4211 DECL_NAME (result) = DECL_NAME (var);
4212 /* Don't forget that we take its address. */
4213 TREE_ADDRESSABLE (result) = TREE_ADDRESSABLE (var);
4214 /* Finally set DECL_VALUE_EXPR to avoid assigning
4215 a stack slot at -O0 for the original var and debug info
4216 uses RESULT location for VAR. */
4217 SET_DECL_VALUE_EXPR (var, result);
4218 DECL_HAS_VALUE_EXPR_P (var) = 1;
4220 data.var = var;
4221 data.result = result;
4222 cp_walk_tree (tp, finalize_nrv_r, &data, 0);
4225 /* Create CP_OMP_CLAUSE_INFO for clause C. Returns true if it is invalid. */
4227 bool
4228 cxx_omp_create_clause_info (tree c, tree type, bool need_default_ctor,
4229 bool need_copy_ctor, bool need_copy_assignment,
4230 bool need_dtor)
4232 int save_errorcount = errorcount;
4233 tree info, t;
4235 /* Always allocate 3 elements for simplicity. These are the
4236 function decls for the ctor, dtor, and assignment op.
4237 This layout is known to the three lang hooks,
4238 cxx_omp_clause_default_init, cxx_omp_clause_copy_init,
4239 and cxx_omp_clause_assign_op. */
4240 info = make_tree_vec (3);
4241 CP_OMP_CLAUSE_INFO (c) = info;
4243 if (need_default_ctor || need_copy_ctor)
4245 if (need_default_ctor)
4246 t = get_default_ctor (type);
4247 else
4248 t = get_copy_ctor (type, tf_warning_or_error);
4250 if (t && !trivial_fn_p (t))
4251 TREE_VEC_ELT (info, 0) = t;
4254 if (need_dtor && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
4255 TREE_VEC_ELT (info, 1) = get_dtor (type, tf_warning_or_error);
4257 if (need_copy_assignment)
4259 t = get_copy_assign (type);
4261 if (t && !trivial_fn_p (t))
4262 TREE_VEC_ELT (info, 2) = t;
4265 return errorcount != save_errorcount;
4268 /* Helper function for handle_omp_array_sections. Called recursively
4269 to handle multiple array-section-subscripts. C is the clause,
4270 T current expression (initially OMP_CLAUSE_DECL), which is either
4271 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
4272 expression if specified, TREE_VALUE length expression if specified,
4273 TREE_CHAIN is what it has been specified after, or some decl.
4274 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
4275 set to true if any of the array-section-subscript could have length
4276 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
4277 first array-section-subscript which is known not to have length
4278 of one. Given say:
4279 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
4280 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
4281 all are or may have length of 1, array-section-subscript [:2] is the
4282 first one knonwn not to have length 1. For array-section-subscript
4283 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
4284 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
4285 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
4286 case though, as some lengths could be zero. */
4288 static tree
4289 handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
4290 bool &maybe_zero_len, unsigned int &first_non_one)
4292 tree ret, low_bound, length, type;
4293 if (TREE_CODE (t) != TREE_LIST)
4295 if (error_operand_p (t))
4296 return error_mark_node;
4297 if (type_dependent_expression_p (t))
4298 return NULL_TREE;
4299 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
4301 if (processing_template_decl)
4302 return NULL_TREE;
4303 if (DECL_P (t))
4304 error_at (OMP_CLAUSE_LOCATION (c),
4305 "%qD is not a variable in %qs clause", t,
4306 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4307 else
4308 error_at (OMP_CLAUSE_LOCATION (c),
4309 "%qE is not a variable in %qs clause", t,
4310 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4311 return error_mark_node;
4313 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
4314 && VAR_P (t) && CP_DECL_THREAD_LOCAL_P (t))
4316 error_at (OMP_CLAUSE_LOCATION (c),
4317 "%qD is threadprivate variable in %qs clause", t,
4318 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4319 return error_mark_node;
4321 t = convert_from_reference (t);
4322 return t;
4325 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
4326 maybe_zero_len, first_non_one);
4327 if (ret == error_mark_node || ret == NULL_TREE)
4328 return ret;
4330 type = TREE_TYPE (ret);
4331 low_bound = TREE_PURPOSE (t);
4332 length = TREE_VALUE (t);
4333 if ((low_bound && type_dependent_expression_p (low_bound))
4334 || (length && type_dependent_expression_p (length)))
4335 return NULL_TREE;
4337 if (low_bound == error_mark_node || length == error_mark_node)
4338 return error_mark_node;
4340 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
4342 error_at (OMP_CLAUSE_LOCATION (c),
4343 "low bound %qE of array section does not have integral type",
4344 low_bound);
4345 return error_mark_node;
4347 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
4349 error_at (OMP_CLAUSE_LOCATION (c),
4350 "length %qE of array section does not have integral type",
4351 length);
4352 return error_mark_node;
4354 if (low_bound)
4355 low_bound = mark_rvalue_use (low_bound);
4356 if (length)
4357 length = mark_rvalue_use (length);
4358 if (low_bound
4359 && TREE_CODE (low_bound) == INTEGER_CST
4360 && TYPE_PRECISION (TREE_TYPE (low_bound))
4361 > TYPE_PRECISION (sizetype))
4362 low_bound = fold_convert (sizetype, low_bound);
4363 if (length
4364 && TREE_CODE (length) == INTEGER_CST
4365 && TYPE_PRECISION (TREE_TYPE (length))
4366 > TYPE_PRECISION (sizetype))
4367 length = fold_convert (sizetype, length);
4368 if (low_bound == NULL_TREE)
4369 low_bound = integer_zero_node;
4371 if (length != NULL_TREE)
4373 if (!integer_nonzerop (length))
4374 maybe_zero_len = true;
4375 if (first_non_one == types.length ()
4376 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
4377 first_non_one++;
4379 if (TREE_CODE (type) == ARRAY_TYPE)
4381 if (length == NULL_TREE
4382 && (TYPE_DOMAIN (type) == NULL_TREE
4383 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
4385 error_at (OMP_CLAUSE_LOCATION (c),
4386 "for unknown bound array type length expression must "
4387 "be specified");
4388 return error_mark_node;
4390 if (TREE_CODE (low_bound) == INTEGER_CST
4391 && tree_int_cst_sgn (low_bound) == -1)
4393 error_at (OMP_CLAUSE_LOCATION (c),
4394 "negative low bound in array section in %qs clause",
4395 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4396 return error_mark_node;
4398 if (length != NULL_TREE
4399 && TREE_CODE (length) == INTEGER_CST
4400 && tree_int_cst_sgn (length) == -1)
4402 error_at (OMP_CLAUSE_LOCATION (c),
4403 "negative length in array section in %qs clause",
4404 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4405 return error_mark_node;
4407 if (TYPE_DOMAIN (type)
4408 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
4409 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
4410 == INTEGER_CST)
4412 tree size = size_binop (PLUS_EXPR,
4413 TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
4414 size_one_node);
4415 if (TREE_CODE (low_bound) == INTEGER_CST)
4417 if (tree_int_cst_lt (size, low_bound))
4419 error_at (OMP_CLAUSE_LOCATION (c),
4420 "low bound %qE above array section size "
4421 "in %qs clause", low_bound,
4422 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4423 return error_mark_node;
4425 if (tree_int_cst_equal (size, low_bound))
4426 maybe_zero_len = true;
4427 else if (length == NULL_TREE
4428 && first_non_one == types.length ()
4429 && tree_int_cst_equal
4430 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
4431 low_bound))
4432 first_non_one++;
4434 else if (length == NULL_TREE)
4436 maybe_zero_len = true;
4437 if (first_non_one == types.length ())
4438 first_non_one++;
4440 if (length && TREE_CODE (length) == INTEGER_CST)
4442 if (tree_int_cst_lt (size, length))
4444 error_at (OMP_CLAUSE_LOCATION (c),
4445 "length %qE above array section size "
4446 "in %qs clause", length,
4447 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4448 return error_mark_node;
4450 if (TREE_CODE (low_bound) == INTEGER_CST)
4452 tree lbpluslen
4453 = size_binop (PLUS_EXPR,
4454 fold_convert (sizetype, low_bound),
4455 fold_convert (sizetype, length));
4456 if (TREE_CODE (lbpluslen) == INTEGER_CST
4457 && tree_int_cst_lt (size, lbpluslen))
4459 error_at (OMP_CLAUSE_LOCATION (c),
4460 "high bound %qE above array section size "
4461 "in %qs clause", lbpluslen,
4462 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4463 return error_mark_node;
4468 else if (length == NULL_TREE)
4470 maybe_zero_len = true;
4471 if (first_non_one == types.length ())
4472 first_non_one++;
4475 /* For [lb:] we will need to evaluate lb more than once. */
4476 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
4478 tree lb = cp_save_expr (low_bound);
4479 if (lb != low_bound)
4481 TREE_PURPOSE (t) = lb;
4482 low_bound = lb;
4486 else if (TREE_CODE (type) == POINTER_TYPE)
4488 if (length == NULL_TREE)
4490 error_at (OMP_CLAUSE_LOCATION (c),
4491 "for pointer type length expression must be specified");
4492 return error_mark_node;
4494 /* If there is a pointer type anywhere but in the very first
4495 array-section-subscript, the array section can't be contiguous. */
4496 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
4497 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
4499 error_at (OMP_CLAUSE_LOCATION (c),
4500 "array section is not contiguous in %qs clause",
4501 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4502 return error_mark_node;
4505 else
4507 error_at (OMP_CLAUSE_LOCATION (c),
4508 "%qE does not have pointer or array type", ret);
4509 return error_mark_node;
4511 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
4512 types.safe_push (TREE_TYPE (ret));
4513 /* We will need to evaluate lb more than once. */
4514 tree lb = cp_save_expr (low_bound);
4515 if (lb != low_bound)
4517 TREE_PURPOSE (t) = lb;
4518 low_bound = lb;
4520 ret = grok_array_decl (OMP_CLAUSE_LOCATION (c), ret, low_bound, false);
4521 return ret;
4524 /* Handle array sections for clause C. */
4526 static bool
4527 handle_omp_array_sections (tree c)
4529 bool maybe_zero_len = false;
4530 unsigned int first_non_one = 0;
4531 auto_vec<tree> types;
4532 tree first = handle_omp_array_sections_1 (c, OMP_CLAUSE_DECL (c), types,
4533 maybe_zero_len, first_non_one);
4534 if (first == error_mark_node)
4535 return true;
4536 if (first == NULL_TREE)
4537 return false;
4538 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
4540 tree t = OMP_CLAUSE_DECL (c);
4541 tree tem = NULL_TREE;
4542 if (processing_template_decl)
4543 return false;
4544 /* Need to evaluate side effects in the length expressions
4545 if any. */
4546 while (TREE_CODE (t) == TREE_LIST)
4548 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
4550 if (tem == NULL_TREE)
4551 tem = TREE_VALUE (t);
4552 else
4553 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
4554 TREE_VALUE (t), tem);
4556 t = TREE_CHAIN (t);
4558 if (tem)
4559 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
4560 OMP_CLAUSE_DECL (c) = first;
4562 else
4564 unsigned int num = types.length (), i;
4565 tree t, side_effects = NULL_TREE, size = NULL_TREE;
4566 tree condition = NULL_TREE;
4568 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
4569 maybe_zero_len = true;
4570 if (processing_template_decl && maybe_zero_len)
4571 return false;
4573 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
4574 t = TREE_CHAIN (t))
4576 tree low_bound = TREE_PURPOSE (t);
4577 tree length = TREE_VALUE (t);
4579 i--;
4580 if (low_bound
4581 && TREE_CODE (low_bound) == INTEGER_CST
4582 && TYPE_PRECISION (TREE_TYPE (low_bound))
4583 > TYPE_PRECISION (sizetype))
4584 low_bound = fold_convert (sizetype, low_bound);
4585 if (length
4586 && TREE_CODE (length) == INTEGER_CST
4587 && TYPE_PRECISION (TREE_TYPE (length))
4588 > TYPE_PRECISION (sizetype))
4589 length = fold_convert (sizetype, length);
4590 if (low_bound == NULL_TREE)
4591 low_bound = integer_zero_node;
4592 if (!maybe_zero_len && i > first_non_one)
4594 if (integer_nonzerop (low_bound))
4595 goto do_warn_noncontiguous;
4596 if (length != NULL_TREE
4597 && TREE_CODE (length) == INTEGER_CST
4598 && TYPE_DOMAIN (types[i])
4599 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
4600 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
4601 == INTEGER_CST)
4603 tree size;
4604 size = size_binop (PLUS_EXPR,
4605 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
4606 size_one_node);
4607 if (!tree_int_cst_equal (length, size))
4609 do_warn_noncontiguous:
4610 error_at (OMP_CLAUSE_LOCATION (c),
4611 "array section is not contiguous in %qs "
4612 "clause",
4613 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4614 return true;
4617 if (!processing_template_decl
4618 && length != NULL_TREE
4619 && TREE_SIDE_EFFECTS (length))
4621 if (side_effects == NULL_TREE)
4622 side_effects = length;
4623 else
4624 side_effects = build2 (COMPOUND_EXPR,
4625 TREE_TYPE (side_effects),
4626 length, side_effects);
4629 else if (processing_template_decl)
4630 continue;
4631 else
4633 tree l;
4635 if (i > first_non_one && length && integer_nonzerop (length))
4636 continue;
4637 if (length)
4638 l = fold_convert (sizetype, length);
4639 else
4641 l = size_binop (PLUS_EXPR,
4642 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
4643 size_one_node);
4644 l = size_binop (MINUS_EXPR, l,
4645 fold_convert (sizetype, low_bound));
4647 if (i > first_non_one)
4649 l = fold_build2 (NE_EXPR, boolean_type_node, l,
4650 size_zero_node);
4651 if (condition == NULL_TREE)
4652 condition = l;
4653 else
4654 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
4655 l, condition);
4657 else if (size == NULL_TREE)
4659 size = size_in_bytes (TREE_TYPE (types[i]));
4660 size = size_binop (MULT_EXPR, size, l);
4661 if (condition)
4662 size = fold_build3 (COND_EXPR, sizetype, condition,
4663 size, size_zero_node);
4665 else
4666 size = size_binop (MULT_EXPR, size, l);
4669 if (!processing_template_decl)
4671 if (side_effects)
4672 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
4673 OMP_CLAUSE_DECL (c) = first;
4674 OMP_CLAUSE_SIZE (c) = size;
4675 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
4676 return false;
4677 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
4678 OMP_CLAUSE_MAP);
4679 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_POINTER);
4680 if (!cxx_mark_addressable (t))
4681 return false;
4682 OMP_CLAUSE_DECL (c2) = t;
4683 t = build_fold_addr_expr (first);
4684 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
4685 ptrdiff_type_node, t);
4686 tree ptr = OMP_CLAUSE_DECL (c2);
4687 ptr = convert_from_reference (ptr);
4688 if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
4689 ptr = build_fold_addr_expr (ptr);
4690 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
4691 ptrdiff_type_node, t,
4692 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
4693 ptrdiff_type_node, ptr));
4694 OMP_CLAUSE_SIZE (c2) = t;
4695 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
4696 OMP_CLAUSE_CHAIN (c) = c2;
4697 ptr = OMP_CLAUSE_DECL (c2);
4698 if (TREE_CODE (TREE_TYPE (ptr)) == REFERENCE_TYPE
4699 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (ptr))))
4701 tree c3 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
4702 OMP_CLAUSE_MAP);
4703 OMP_CLAUSE_SET_MAP_KIND (c3, GOMP_MAP_POINTER);
4704 OMP_CLAUSE_DECL (c3) = ptr;
4705 OMP_CLAUSE_DECL (c2) = convert_from_reference (ptr);
4706 OMP_CLAUSE_SIZE (c3) = size_zero_node;
4707 OMP_CLAUSE_CHAIN (c3) = OMP_CLAUSE_CHAIN (c2);
4708 OMP_CLAUSE_CHAIN (c2) = c3;
4712 return false;
4715 /* Return identifier to look up for omp declare reduction. */
4717 tree
4718 omp_reduction_id (enum tree_code reduction_code, tree reduction_id, tree type)
4720 const char *p = NULL;
4721 const char *m = NULL;
4722 switch (reduction_code)
4724 case PLUS_EXPR:
4725 case MULT_EXPR:
4726 case MINUS_EXPR:
4727 case BIT_AND_EXPR:
4728 case BIT_XOR_EXPR:
4729 case BIT_IOR_EXPR:
4730 case TRUTH_ANDIF_EXPR:
4731 case TRUTH_ORIF_EXPR:
4732 reduction_id = ansi_opname (reduction_code);
4733 break;
4734 case MIN_EXPR:
4735 p = "min";
4736 break;
4737 case MAX_EXPR:
4738 p = "max";
4739 break;
4740 default:
4741 break;
4744 if (p == NULL)
4746 if (TREE_CODE (reduction_id) != IDENTIFIER_NODE)
4747 return error_mark_node;
4748 p = IDENTIFIER_POINTER (reduction_id);
4751 if (type != NULL_TREE)
4752 m = mangle_type_string (TYPE_MAIN_VARIANT (type));
4754 const char prefix[] = "omp declare reduction ";
4755 size_t lenp = sizeof (prefix);
4756 if (strncmp (p, prefix, lenp - 1) == 0)
4757 lenp = 1;
4758 size_t len = strlen (p);
4759 size_t lenm = m ? strlen (m) + 1 : 0;
4760 char *name = XALLOCAVEC (char, lenp + len + lenm);
4761 if (lenp > 1)
4762 memcpy (name, prefix, lenp - 1);
4763 memcpy (name + lenp - 1, p, len + 1);
4764 if (m)
4766 name[lenp + len - 1] = '~';
4767 memcpy (name + lenp + len, m, lenm);
4769 return get_identifier (name);
4772 /* Lookup OpenMP UDR ID for TYPE, return the corresponding artificial
4773 FUNCTION_DECL or NULL_TREE if not found. */
4775 static tree
4776 omp_reduction_lookup (location_t loc, tree id, tree type, tree *baselinkp,
4777 vec<tree> *ambiguousp)
4779 tree orig_id = id;
4780 tree baselink = NULL_TREE;
4781 if (identifier_p (id))
4783 cp_id_kind idk;
4784 bool nonint_cst_expression_p;
4785 const char *error_msg;
4786 id = omp_reduction_id (ERROR_MARK, id, type);
4787 tree decl = lookup_name (id);
4788 if (decl == NULL_TREE)
4789 decl = error_mark_node;
4790 id = finish_id_expression (id, decl, NULL_TREE, &idk, false, true,
4791 &nonint_cst_expression_p, false, true, false,
4792 false, &error_msg, loc);
4793 if (idk == CP_ID_KIND_UNQUALIFIED
4794 && identifier_p (id))
4796 vec<tree, va_gc> *args = NULL;
4797 vec_safe_push (args, build_reference_type (type));
4798 id = perform_koenig_lookup (id, args, tf_none);
4801 else if (TREE_CODE (id) == SCOPE_REF)
4802 id = lookup_qualified_name (TREE_OPERAND (id, 0),
4803 omp_reduction_id (ERROR_MARK,
4804 TREE_OPERAND (id, 1),
4805 type),
4806 false, false);
4807 tree fns = id;
4808 if (id && is_overloaded_fn (id))
4809 id = get_fns (id);
4810 for (; id; id = OVL_NEXT (id))
4812 tree fndecl = OVL_CURRENT (id);
4813 if (TREE_CODE (fndecl) == FUNCTION_DECL)
4815 tree argtype = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
4816 if (same_type_p (TREE_TYPE (argtype), type))
4817 break;
4820 if (id && BASELINK_P (fns))
4822 if (baselinkp)
4823 *baselinkp = fns;
4824 else
4825 baselink = fns;
4827 if (id == NULL_TREE && CLASS_TYPE_P (type) && TYPE_BINFO (type))
4829 vec<tree> ambiguous = vNULL;
4830 tree binfo = TYPE_BINFO (type), base_binfo, ret = NULL_TREE;
4831 unsigned int ix;
4832 if (ambiguousp == NULL)
4833 ambiguousp = &ambiguous;
4834 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
4836 id = omp_reduction_lookup (loc, orig_id, BINFO_TYPE (base_binfo),
4837 baselinkp ? baselinkp : &baselink,
4838 ambiguousp);
4839 if (id == NULL_TREE)
4840 continue;
4841 if (!ambiguousp->is_empty ())
4842 ambiguousp->safe_push (id);
4843 else if (ret != NULL_TREE)
4845 ambiguousp->safe_push (ret);
4846 ambiguousp->safe_push (id);
4847 ret = NULL_TREE;
4849 else
4850 ret = id;
4852 if (ambiguousp != &ambiguous)
4853 return ret;
4854 if (!ambiguous.is_empty ())
4856 const char *str = _("candidates are:");
4857 unsigned int idx;
4858 tree udr;
4859 error_at (loc, "user defined reduction lookup is ambiguous");
4860 FOR_EACH_VEC_ELT (ambiguous, idx, udr)
4862 inform (DECL_SOURCE_LOCATION (udr), "%s %#D", str, udr);
4863 if (idx == 0)
4864 str = get_spaces (str);
4866 ambiguous.release ();
4867 ret = error_mark_node;
4868 baselink = NULL_TREE;
4870 id = ret;
4872 if (id && baselink)
4873 perform_or_defer_access_check (BASELINK_BINFO (baselink),
4874 id, id, tf_warning_or_error);
4875 return id;
4878 /* Helper function for cp_parser_omp_declare_reduction_exprs
4879 and tsubst_omp_udr.
4880 Remove CLEANUP_STMT for data (omp_priv variable).
4881 Also append INIT_EXPR for DECL_INITIAL of omp_priv after its
4882 DECL_EXPR. */
4884 tree
4885 cp_remove_omp_priv_cleanup_stmt (tree *tp, int *walk_subtrees, void *data)
4887 if (TYPE_P (*tp))
4888 *walk_subtrees = 0;
4889 else if (TREE_CODE (*tp) == CLEANUP_STMT && CLEANUP_DECL (*tp) == (tree) data)
4890 *tp = CLEANUP_BODY (*tp);
4891 else if (TREE_CODE (*tp) == DECL_EXPR)
4893 tree decl = DECL_EXPR_DECL (*tp);
4894 if (!processing_template_decl
4895 && decl == (tree) data
4896 && DECL_INITIAL (decl)
4897 && DECL_INITIAL (decl) != error_mark_node)
4899 tree list = NULL_TREE;
4900 append_to_statement_list_force (*tp, &list);
4901 tree init_expr = build2 (INIT_EXPR, void_type_node,
4902 decl, DECL_INITIAL (decl));
4903 DECL_INITIAL (decl) = NULL_TREE;
4904 append_to_statement_list_force (init_expr, &list);
4905 *tp = list;
4908 return NULL_TREE;
4911 /* Data passed from cp_check_omp_declare_reduction to
4912 cp_check_omp_declare_reduction_r. */
4914 struct cp_check_omp_declare_reduction_data
4916 location_t loc;
4917 tree stmts[7];
4918 bool combiner_p;
4921 /* Helper function for cp_check_omp_declare_reduction, called via
4922 cp_walk_tree. */
4924 static tree
4925 cp_check_omp_declare_reduction_r (tree *tp, int *, void *data)
4927 struct cp_check_omp_declare_reduction_data *udr_data
4928 = (struct cp_check_omp_declare_reduction_data *) data;
4929 if (SSA_VAR_P (*tp)
4930 && !DECL_ARTIFICIAL (*tp)
4931 && *tp != DECL_EXPR_DECL (udr_data->stmts[udr_data->combiner_p ? 0 : 3])
4932 && *tp != DECL_EXPR_DECL (udr_data->stmts[udr_data->combiner_p ? 1 : 4]))
4934 location_t loc = udr_data->loc;
4935 if (udr_data->combiner_p)
4936 error_at (loc, "%<#pragma omp declare reduction%> combiner refers to "
4937 "variable %qD which is not %<omp_out%> nor %<omp_in%>",
4938 *tp);
4939 else
4940 error_at (loc, "%<#pragma omp declare reduction%> initializer refers "
4941 "to variable %qD which is not %<omp_priv%> nor "
4942 "%<omp_orig%>",
4943 *tp);
4944 return *tp;
4946 return NULL_TREE;
4949 /* Diagnose violation of OpenMP #pragma omp declare reduction restrictions. */
4951 void
4952 cp_check_omp_declare_reduction (tree udr)
4954 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (udr)));
4955 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
4956 type = TREE_TYPE (type);
4957 int i;
4958 location_t loc = DECL_SOURCE_LOCATION (udr);
4960 if (type == error_mark_node)
4961 return;
4962 if (ARITHMETIC_TYPE_P (type))
4964 static enum tree_code predef_codes[]
4965 = { PLUS_EXPR, MULT_EXPR, MINUS_EXPR, BIT_AND_EXPR, BIT_XOR_EXPR,
4966 BIT_IOR_EXPR, TRUTH_ANDIF_EXPR, TRUTH_ORIF_EXPR };
4967 for (i = 0; i < 8; i++)
4969 tree id = omp_reduction_id (predef_codes[i], NULL_TREE, NULL_TREE);
4970 const char *n1 = IDENTIFIER_POINTER (DECL_NAME (udr));
4971 const char *n2 = IDENTIFIER_POINTER (id);
4972 if (strncmp (n1, n2, IDENTIFIER_LENGTH (id)) == 0
4973 && (n1[IDENTIFIER_LENGTH (id)] == '~'
4974 || n1[IDENTIFIER_LENGTH (id)] == '\0'))
4975 break;
4978 if (i == 8
4979 && TREE_CODE (type) != COMPLEX_EXPR)
4981 const char prefix_minmax[] = "omp declare reduction m";
4982 size_t prefix_size = sizeof (prefix_minmax) - 1;
4983 const char *n = IDENTIFIER_POINTER (DECL_NAME (udr));
4984 if (strncmp (IDENTIFIER_POINTER (DECL_NAME (udr)),
4985 prefix_minmax, prefix_size) == 0
4986 && ((n[prefix_size] == 'i' && n[prefix_size + 1] == 'n')
4987 || (n[prefix_size] == 'a' && n[prefix_size + 1] == 'x'))
4988 && (n[prefix_size + 2] == '~' || n[prefix_size + 2] == '\0'))
4989 i = 0;
4991 if (i < 8)
4993 error_at (loc, "predeclared arithmetic type %qT in "
4994 "%<#pragma omp declare reduction%>", type);
4995 return;
4998 else if (TREE_CODE (type) == FUNCTION_TYPE
4999 || TREE_CODE (type) == METHOD_TYPE
5000 || TREE_CODE (type) == ARRAY_TYPE)
5002 error_at (loc, "function or array type %qT in "
5003 "%<#pragma omp declare reduction%>", type);
5004 return;
5006 else if (TREE_CODE (type) == REFERENCE_TYPE)
5008 error_at (loc, "reference type %qT in %<#pragma omp declare reduction%>",
5009 type);
5010 return;
5012 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
5014 error_at (loc, "const, volatile or __restrict qualified type %qT in "
5015 "%<#pragma omp declare reduction%>", type);
5016 return;
5019 tree body = DECL_SAVED_TREE (udr);
5020 if (body == NULL_TREE || TREE_CODE (body) != STATEMENT_LIST)
5021 return;
5023 tree_stmt_iterator tsi;
5024 struct cp_check_omp_declare_reduction_data data;
5025 memset (data.stmts, 0, sizeof data.stmts);
5026 for (i = 0, tsi = tsi_start (body);
5027 i < 7 && !tsi_end_p (tsi);
5028 i++, tsi_next (&tsi))
5029 data.stmts[i] = tsi_stmt (tsi);
5030 data.loc = loc;
5031 gcc_assert (tsi_end_p (tsi));
5032 if (i >= 3)
5034 gcc_assert (TREE_CODE (data.stmts[0]) == DECL_EXPR
5035 && TREE_CODE (data.stmts[1]) == DECL_EXPR);
5036 if (TREE_NO_WARNING (DECL_EXPR_DECL (data.stmts[0])))
5037 return;
5038 data.combiner_p = true;
5039 if (cp_walk_tree (&data.stmts[2], cp_check_omp_declare_reduction_r,
5040 &data, NULL))
5041 TREE_NO_WARNING (DECL_EXPR_DECL (data.stmts[0])) = 1;
5043 if (i >= 6)
5045 gcc_assert (TREE_CODE (data.stmts[3]) == DECL_EXPR
5046 && TREE_CODE (data.stmts[4]) == DECL_EXPR);
5047 data.combiner_p = false;
5048 if (cp_walk_tree (&data.stmts[5], cp_check_omp_declare_reduction_r,
5049 &data, NULL)
5050 || cp_walk_tree (&DECL_INITIAL (DECL_EXPR_DECL (data.stmts[3])),
5051 cp_check_omp_declare_reduction_r, &data, NULL))
5052 TREE_NO_WARNING (DECL_EXPR_DECL (data.stmts[0])) = 1;
5053 if (i == 7)
5054 gcc_assert (TREE_CODE (data.stmts[6]) == DECL_EXPR);
5058 /* Helper function of finish_omp_clauses. Clone STMT as if we were making
5059 an inline call. But, remap
5060 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
5061 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
5063 static tree
5064 clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
5065 tree decl, tree placeholder)
5067 copy_body_data id;
5068 hash_map<tree, tree> decl_map;
5070 decl_map.put (omp_decl1, placeholder);
5071 decl_map.put (omp_decl2, decl);
5072 memset (&id, 0, sizeof (id));
5073 id.src_fn = DECL_CONTEXT (omp_decl1);
5074 id.dst_fn = current_function_decl;
5075 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
5076 id.decl_map = &decl_map;
5078 id.copy_decl = copy_decl_no_change;
5079 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
5080 id.transform_new_cfg = true;
5081 id.transform_return_to_modify = false;
5082 id.transform_lang_insert_block = NULL;
5083 id.eh_lp_nr = 0;
5084 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
5085 return stmt;
5088 /* Helper function of finish_omp_clauses, called via cp_walk_tree.
5089 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
5091 static tree
5092 find_omp_placeholder_r (tree *tp, int *, void *data)
5094 if (*tp == (tree) data)
5095 return *tp;
5096 return NULL_TREE;
5099 /* Helper function of finish_omp_clauses. Handle OMP_CLAUSE_REDUCTION C.
5100 Return true if there is some error and the clause should be removed. */
5102 static bool
5103 finish_omp_reduction_clause (tree c, bool *need_default_ctor, bool *need_dtor)
5105 tree t = OMP_CLAUSE_DECL (c);
5106 bool predefined = false;
5107 tree type = TREE_TYPE (t);
5108 if (TREE_CODE (type) == REFERENCE_TYPE)
5109 type = TREE_TYPE (type);
5110 if (type == error_mark_node)
5111 return true;
5112 else if (ARITHMETIC_TYPE_P (type))
5113 switch (OMP_CLAUSE_REDUCTION_CODE (c))
5115 case PLUS_EXPR:
5116 case MULT_EXPR:
5117 case MINUS_EXPR:
5118 predefined = true;
5119 break;
5120 case MIN_EXPR:
5121 case MAX_EXPR:
5122 if (TREE_CODE (type) == COMPLEX_TYPE)
5123 break;
5124 predefined = true;
5125 break;
5126 case BIT_AND_EXPR:
5127 case BIT_IOR_EXPR:
5128 case BIT_XOR_EXPR:
5129 if (FLOAT_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
5130 break;
5131 predefined = true;
5132 break;
5133 case TRUTH_ANDIF_EXPR:
5134 case TRUTH_ORIF_EXPR:
5135 if (FLOAT_TYPE_P (type))
5136 break;
5137 predefined = true;
5138 break;
5139 default:
5140 break;
5142 else if (TREE_CODE (type) == ARRAY_TYPE || TYPE_READONLY (type))
5144 error ("%qE has invalid type for %<reduction%>", t);
5145 return true;
5147 else if (!processing_template_decl)
5149 t = require_complete_type (t);
5150 if (t == error_mark_node)
5151 return true;
5152 OMP_CLAUSE_DECL (c) = t;
5155 if (predefined)
5157 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = NULL_TREE;
5158 return false;
5160 else if (processing_template_decl)
5161 return false;
5163 tree id = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
5165 type = TYPE_MAIN_VARIANT (TREE_TYPE (t));
5166 if (TREE_CODE (type) == REFERENCE_TYPE)
5167 type = TREE_TYPE (type);
5168 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = NULL_TREE;
5169 if (id == NULL_TREE)
5170 id = omp_reduction_id (OMP_CLAUSE_REDUCTION_CODE (c),
5171 NULL_TREE, NULL_TREE);
5172 id = omp_reduction_lookup (OMP_CLAUSE_LOCATION (c), id, type, NULL, NULL);
5173 if (id)
5175 if (id == error_mark_node)
5176 return true;
5177 id = OVL_CURRENT (id);
5178 mark_used (id);
5179 tree body = DECL_SAVED_TREE (id);
5180 if (!body)
5181 return true;
5182 if (TREE_CODE (body) == STATEMENT_LIST)
5184 tree_stmt_iterator tsi;
5185 tree placeholder = NULL_TREE;
5186 int i;
5187 tree stmts[7];
5188 tree atype = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (id)));
5189 atype = TREE_TYPE (atype);
5190 bool need_static_cast = !same_type_p (type, atype);
5191 memset (stmts, 0, sizeof stmts);
5192 for (i = 0, tsi = tsi_start (body);
5193 i < 7 && !tsi_end_p (tsi);
5194 i++, tsi_next (&tsi))
5195 stmts[i] = tsi_stmt (tsi);
5196 gcc_assert (tsi_end_p (tsi));
5198 if (i >= 3)
5200 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
5201 && TREE_CODE (stmts[1]) == DECL_EXPR);
5202 placeholder = build_lang_decl (VAR_DECL, NULL_TREE, type);
5203 DECL_ARTIFICIAL (placeholder) = 1;
5204 DECL_IGNORED_P (placeholder) = 1;
5205 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
5206 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[0])))
5207 cxx_mark_addressable (placeholder);
5208 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[1]))
5209 && TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c)))
5210 != REFERENCE_TYPE)
5211 cxx_mark_addressable (OMP_CLAUSE_DECL (c));
5212 tree omp_out = placeholder;
5213 tree omp_in = convert_from_reference (OMP_CLAUSE_DECL (c));
5214 if (need_static_cast)
5216 tree rtype = build_reference_type (atype);
5217 omp_out = build_static_cast (rtype, omp_out,
5218 tf_warning_or_error);
5219 omp_in = build_static_cast (rtype, omp_in,
5220 tf_warning_or_error);
5221 if (omp_out == error_mark_node || omp_in == error_mark_node)
5222 return true;
5223 omp_out = convert_from_reference (omp_out);
5224 omp_in = convert_from_reference (omp_in);
5226 OMP_CLAUSE_REDUCTION_MERGE (c)
5227 = clone_omp_udr (stmts[2], DECL_EXPR_DECL (stmts[0]),
5228 DECL_EXPR_DECL (stmts[1]), omp_in, omp_out);
5230 if (i >= 6)
5232 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
5233 && TREE_CODE (stmts[4]) == DECL_EXPR);
5234 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[3])))
5235 cxx_mark_addressable (OMP_CLAUSE_DECL (c));
5236 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[4])))
5237 cxx_mark_addressable (placeholder);
5238 tree omp_priv = convert_from_reference (OMP_CLAUSE_DECL (c));
5239 tree omp_orig = placeholder;
5240 if (need_static_cast)
5242 if (i == 7)
5244 error_at (OMP_CLAUSE_LOCATION (c),
5245 "user defined reduction with constructor "
5246 "initializer for base class %qT", atype);
5247 return true;
5249 tree rtype = build_reference_type (atype);
5250 omp_priv = build_static_cast (rtype, omp_priv,
5251 tf_warning_or_error);
5252 omp_orig = build_static_cast (rtype, omp_orig,
5253 tf_warning_or_error);
5254 if (omp_priv == error_mark_node
5255 || omp_orig == error_mark_node)
5256 return true;
5257 omp_priv = convert_from_reference (omp_priv);
5258 omp_orig = convert_from_reference (omp_orig);
5260 if (i == 6)
5261 *need_default_ctor = true;
5262 OMP_CLAUSE_REDUCTION_INIT (c)
5263 = clone_omp_udr (stmts[5], DECL_EXPR_DECL (stmts[4]),
5264 DECL_EXPR_DECL (stmts[3]),
5265 omp_priv, omp_orig);
5266 if (cp_walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
5267 find_omp_placeholder_r, placeholder, NULL))
5268 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
5270 else if (i >= 3)
5272 if (CLASS_TYPE_P (type) && !pod_type_p (type))
5273 *need_default_ctor = true;
5274 else
5276 tree init;
5277 tree v = convert_from_reference (t);
5278 if (AGGREGATE_TYPE_P (TREE_TYPE (v)))
5279 init = build_constructor (TREE_TYPE (v), NULL);
5280 else
5281 init = fold_convert (TREE_TYPE (v), integer_zero_node);
5282 OMP_CLAUSE_REDUCTION_INIT (c)
5283 = build2 (INIT_EXPR, TREE_TYPE (v), v, init);
5288 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
5289 *need_dtor = true;
5290 else
5292 error ("user defined reduction not found for %qD", t);
5293 return true;
5295 return false;
5298 /* For all elements of CLAUSES, validate them vs OpenMP constraints.
5299 Remove any elements from the list that are invalid. */
5301 tree
5302 finish_omp_clauses (tree clauses)
5304 bitmap_head generic_head, firstprivate_head, lastprivate_head;
5305 bitmap_head aligned_head;
5306 tree c, t, *pc;
5307 bool branch_seen = false;
5308 bool copyprivate_seen = false;
5310 bitmap_obstack_initialize (NULL);
5311 bitmap_initialize (&generic_head, &bitmap_default_obstack);
5312 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
5313 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
5314 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
5316 for (pc = &clauses, c = clauses; c ; c = *pc)
5318 bool remove = false;
5320 switch (OMP_CLAUSE_CODE (c))
5322 case OMP_CLAUSE_SHARED:
5323 goto check_dup_generic;
5324 case OMP_CLAUSE_PRIVATE:
5325 goto check_dup_generic;
5326 case OMP_CLAUSE_REDUCTION:
5327 goto check_dup_generic;
5328 case OMP_CLAUSE_COPYPRIVATE:
5329 copyprivate_seen = true;
5330 goto check_dup_generic;
5331 case OMP_CLAUSE_COPYIN:
5332 goto check_dup_generic;
5333 case OMP_CLAUSE_LINEAR:
5334 t = OMP_CLAUSE_DECL (c);
5335 if (!type_dependent_expression_p (t)
5336 && !INTEGRAL_TYPE_P (TREE_TYPE (t))
5337 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
5339 error ("linear clause applied to non-integral non-pointer "
5340 "variable with %qT type", TREE_TYPE (t));
5341 remove = true;
5342 break;
5344 t = OMP_CLAUSE_LINEAR_STEP (c);
5345 if (t == NULL_TREE)
5346 t = integer_one_node;
5347 if (t == error_mark_node)
5349 remove = true;
5350 break;
5352 else if (!type_dependent_expression_p (t)
5353 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5355 error ("linear step expression must be integral");
5356 remove = true;
5357 break;
5359 else
5361 t = mark_rvalue_use (t);
5362 if (!processing_template_decl)
5364 if (TREE_CODE (OMP_CLAUSE_DECL (c)) == PARM_DECL)
5365 t = maybe_constant_value (t);
5366 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5367 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c)))
5368 == POINTER_TYPE)
5370 t = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
5371 OMP_CLAUSE_DECL (c), t);
5372 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c),
5373 MINUS_EXPR, sizetype, t,
5374 OMP_CLAUSE_DECL (c));
5375 if (t == error_mark_node)
5377 remove = true;
5378 break;
5381 else
5382 t = fold_convert (TREE_TYPE (OMP_CLAUSE_DECL (c)), t);
5384 OMP_CLAUSE_LINEAR_STEP (c) = t;
5386 goto check_dup_generic;
5387 check_dup_generic:
5388 t = OMP_CLAUSE_DECL (c);
5389 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
5391 if (processing_template_decl)
5392 break;
5393 if (DECL_P (t))
5394 error ("%qD is not a variable in clause %qs", t,
5395 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5396 else
5397 error ("%qE is not a variable in clause %qs", t,
5398 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5399 remove = true;
5401 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
5402 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
5403 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
5405 error ("%qD appears more than once in data clauses", t);
5406 remove = true;
5408 else
5409 bitmap_set_bit (&generic_head, DECL_UID (t));
5410 break;
5412 case OMP_CLAUSE_FIRSTPRIVATE:
5413 t = OMP_CLAUSE_DECL (c);
5414 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
5416 if (processing_template_decl)
5417 break;
5418 if (DECL_P (t))
5419 error ("%qD is not a variable in clause %<firstprivate%>", t);
5420 else
5421 error ("%qE is not a variable in clause %<firstprivate%>", t);
5422 remove = true;
5424 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
5425 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
5427 error ("%qD appears more than once in data clauses", t);
5428 remove = true;
5430 else
5431 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
5432 break;
5434 case OMP_CLAUSE_LASTPRIVATE:
5435 t = OMP_CLAUSE_DECL (c);
5436 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
5438 if (processing_template_decl)
5439 break;
5440 if (DECL_P (t))
5441 error ("%qD is not a variable in clause %<lastprivate%>", t);
5442 else
5443 error ("%qE is not a variable in clause %<lastprivate%>", t);
5444 remove = true;
5446 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
5447 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
5449 error ("%qD appears more than once in data clauses", t);
5450 remove = true;
5452 else
5453 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
5454 break;
5456 case OMP_CLAUSE_IF:
5457 t = OMP_CLAUSE_IF_EXPR (c);
5458 t = maybe_convert_cond (t);
5459 if (t == error_mark_node)
5460 remove = true;
5461 else if (!processing_template_decl)
5462 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5463 OMP_CLAUSE_IF_EXPR (c) = t;
5464 break;
5466 case OMP_CLAUSE_FINAL:
5467 t = OMP_CLAUSE_FINAL_EXPR (c);
5468 t = maybe_convert_cond (t);
5469 if (t == error_mark_node)
5470 remove = true;
5471 else if (!processing_template_decl)
5472 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5473 OMP_CLAUSE_FINAL_EXPR (c) = t;
5474 break;
5476 case OMP_CLAUSE_NUM_THREADS:
5477 t = OMP_CLAUSE_NUM_THREADS_EXPR (c);
5478 if (t == error_mark_node)
5479 remove = true;
5480 else if (!type_dependent_expression_p (t)
5481 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5483 error ("num_threads expression must be integral");
5484 remove = true;
5486 else
5488 t = mark_rvalue_use (t);
5489 if (!processing_template_decl)
5490 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5491 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
5493 break;
5495 case OMP_CLAUSE_SCHEDULE:
5496 t = OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c);
5497 if (t == NULL)
5499 else if (t == error_mark_node)
5500 remove = true;
5501 else if (!type_dependent_expression_p (t)
5502 && (OMP_CLAUSE_SCHEDULE_KIND (c)
5503 != OMP_CLAUSE_SCHEDULE_CILKFOR)
5504 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5506 error ("schedule chunk size expression must be integral");
5507 remove = true;
5509 else
5511 t = mark_rvalue_use (t);
5512 if (!processing_template_decl)
5514 if (OMP_CLAUSE_SCHEDULE_KIND (c)
5515 == OMP_CLAUSE_SCHEDULE_CILKFOR)
5517 t = convert_to_integer (long_integer_type_node, t);
5518 if (t == error_mark_node)
5520 remove = true;
5521 break;
5524 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5526 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
5528 break;
5530 case OMP_CLAUSE_SIMDLEN:
5531 case OMP_CLAUSE_SAFELEN:
5532 t = OMP_CLAUSE_OPERAND (c, 0);
5533 if (t == error_mark_node)
5534 remove = true;
5535 else if (!type_dependent_expression_p (t)
5536 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5538 error ("%qs length expression must be integral",
5539 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5540 remove = true;
5542 else
5544 t = mark_rvalue_use (t);
5545 t = maybe_constant_value (t);
5546 if (!processing_template_decl)
5548 if (TREE_CODE (t) != INTEGER_CST
5549 || tree_int_cst_sgn (t) != 1)
5551 error ("%qs length expression must be positive constant"
5552 " integer expression",
5553 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5554 remove = true;
5557 OMP_CLAUSE_OPERAND (c, 0) = t;
5559 break;
5561 case OMP_CLAUSE_NUM_TEAMS:
5562 t = OMP_CLAUSE_NUM_TEAMS_EXPR (c);
5563 if (t == error_mark_node)
5564 remove = true;
5565 else if (!type_dependent_expression_p (t)
5566 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5568 error ("%<num_teams%> expression must be integral");
5569 remove = true;
5571 else
5573 t = mark_rvalue_use (t);
5574 if (!processing_template_decl)
5575 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5576 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
5578 break;
5580 case OMP_CLAUSE_ASYNC:
5581 t = OMP_CLAUSE_ASYNC_EXPR (c);
5582 if (t == error_mark_node)
5583 remove = true;
5584 else if (!type_dependent_expression_p (t)
5585 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5587 error ("%<async%> expression must be integral");
5588 remove = true;
5590 else
5592 t = mark_rvalue_use (t);
5593 if (!processing_template_decl)
5594 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5595 OMP_CLAUSE_ASYNC_EXPR (c) = t;
5597 break;
5599 case OMP_CLAUSE_VECTOR_LENGTH:
5600 t = OMP_CLAUSE_VECTOR_LENGTH_EXPR (c);
5601 t = maybe_convert_cond (t);
5602 if (t == error_mark_node)
5603 remove = true;
5604 else if (!processing_template_decl)
5605 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5606 OMP_CLAUSE_VECTOR_LENGTH_EXPR (c) = t;
5607 break;
5609 case OMP_CLAUSE_WAIT:
5610 t = OMP_CLAUSE_WAIT_EXPR (c);
5611 if (t == error_mark_node)
5612 remove = true;
5613 else if (!processing_template_decl)
5614 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5615 OMP_CLAUSE_WAIT_EXPR (c) = t;
5616 break;
5618 case OMP_CLAUSE_THREAD_LIMIT:
5619 t = OMP_CLAUSE_THREAD_LIMIT_EXPR (c);
5620 if (t == error_mark_node)
5621 remove = true;
5622 else if (!type_dependent_expression_p (t)
5623 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5625 error ("%<thread_limit%> expression must be integral");
5626 remove = true;
5628 else
5630 t = mark_rvalue_use (t);
5631 if (!processing_template_decl)
5632 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5633 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
5635 break;
5637 case OMP_CLAUSE_DEVICE:
5638 t = OMP_CLAUSE_DEVICE_ID (c);
5639 if (t == error_mark_node)
5640 remove = true;
5641 else if (!type_dependent_expression_p (t)
5642 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5644 error ("%<device%> id must be integral");
5645 remove = true;
5647 else
5649 t = mark_rvalue_use (t);
5650 if (!processing_template_decl)
5651 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5652 OMP_CLAUSE_DEVICE_ID (c) = t;
5654 break;
5656 case OMP_CLAUSE_DIST_SCHEDULE:
5657 t = OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c);
5658 if (t == NULL)
5660 else if (t == error_mark_node)
5661 remove = true;
5662 else if (!type_dependent_expression_p (t)
5663 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5665 error ("%<dist_schedule%> chunk size expression must be "
5666 "integral");
5667 remove = true;
5669 else
5671 t = mark_rvalue_use (t);
5672 if (!processing_template_decl)
5673 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5674 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
5676 break;
5678 case OMP_CLAUSE_ALIGNED:
5679 t = OMP_CLAUSE_DECL (c);
5680 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
5682 if (processing_template_decl)
5683 break;
5684 if (DECL_P (t))
5685 error ("%qD is not a variable in %<aligned%> clause", t);
5686 else
5687 error ("%qE is not a variable in %<aligned%> clause", t);
5688 remove = true;
5690 else if (!type_dependent_expression_p (t)
5691 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE
5692 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE
5693 && (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE
5694 || (!POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (t)))
5695 && (TREE_CODE (TREE_TYPE (TREE_TYPE (t)))
5696 != ARRAY_TYPE))))
5698 error_at (OMP_CLAUSE_LOCATION (c),
5699 "%qE in %<aligned%> clause is neither a pointer nor "
5700 "an array nor a reference to pointer or array", t);
5701 remove = true;
5703 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
5705 error ("%qD appears more than once in %<aligned%> clauses", t);
5706 remove = true;
5708 else
5709 bitmap_set_bit (&aligned_head, DECL_UID (t));
5710 t = OMP_CLAUSE_ALIGNED_ALIGNMENT (c);
5711 if (t == error_mark_node)
5712 remove = true;
5713 else if (t == NULL_TREE)
5714 break;
5715 else if (!type_dependent_expression_p (t)
5716 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5718 error ("%<aligned%> clause alignment expression must "
5719 "be integral");
5720 remove = true;
5722 else
5724 t = mark_rvalue_use (t);
5725 t = maybe_constant_value (t);
5726 if (!processing_template_decl)
5728 if (TREE_CODE (t) != INTEGER_CST
5729 || tree_int_cst_sgn (t) != 1)
5731 error ("%<aligned%> clause alignment expression must be "
5732 "positive constant integer expression");
5733 remove = true;
5736 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = t;
5738 break;
5740 case OMP_CLAUSE_DEPEND:
5741 t = OMP_CLAUSE_DECL (c);
5742 if (TREE_CODE (t) == TREE_LIST)
5744 if (handle_omp_array_sections (c))
5745 remove = true;
5746 break;
5748 if (t == error_mark_node)
5749 remove = true;
5750 else if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
5752 if (processing_template_decl)
5753 break;
5754 if (DECL_P (t))
5755 error ("%qD is not a variable in %<depend%> clause", t);
5756 else
5757 error ("%qE is not a variable in %<depend%> clause", t);
5758 remove = true;
5760 else if (!processing_template_decl
5761 && !cxx_mark_addressable (t))
5762 remove = true;
5763 break;
5765 case OMP_CLAUSE_MAP:
5766 case OMP_CLAUSE_TO:
5767 case OMP_CLAUSE_FROM:
5768 case OMP_CLAUSE__CACHE_:
5769 t = OMP_CLAUSE_DECL (c);
5770 if (TREE_CODE (t) == TREE_LIST)
5772 if (handle_omp_array_sections (c))
5773 remove = true;
5774 else
5776 t = OMP_CLAUSE_DECL (c);
5777 if (TREE_CODE (t) != TREE_LIST
5778 && !type_dependent_expression_p (t)
5779 && !cp_omp_mappable_type (TREE_TYPE (t)))
5781 error_at (OMP_CLAUSE_LOCATION (c),
5782 "array section does not have mappable type "
5783 "in %qs clause",
5784 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5785 remove = true;
5788 break;
5790 if (t == error_mark_node)
5791 remove = true;
5792 else if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
5794 if (processing_template_decl)
5795 break;
5796 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
5797 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER)
5798 break;
5799 if (DECL_P (t))
5800 error ("%qD is not a variable in %qs clause", t,
5801 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5802 else
5803 error ("%qE is not a variable in %qs clause", t,
5804 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5805 remove = true;
5807 else if (VAR_P (t) && CP_DECL_THREAD_LOCAL_P (t))
5809 error ("%qD is threadprivate variable in %qs clause", t,
5810 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5811 remove = true;
5813 else if (!processing_template_decl
5814 && TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE
5815 && !cxx_mark_addressable (t))
5816 remove = true;
5817 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
5818 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER)
5819 && !type_dependent_expression_p (t)
5820 && !cp_omp_mappable_type ((TREE_CODE (TREE_TYPE (t))
5821 == REFERENCE_TYPE)
5822 ? TREE_TYPE (TREE_TYPE (t))
5823 : TREE_TYPE (t)))
5825 error_at (OMP_CLAUSE_LOCATION (c),
5826 "%qD does not have a mappable type in %qs clause", t,
5827 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5828 remove = true;
5830 else if (bitmap_bit_p (&generic_head, DECL_UID (t)))
5832 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
5833 error ("%qD appears more than once in motion clauses", t);
5834 else
5835 error ("%qD appears more than once in map clauses", t);
5836 remove = true;
5838 else
5839 bitmap_set_bit (&generic_head, DECL_UID (t));
5840 break;
5842 case OMP_CLAUSE_UNIFORM:
5843 t = OMP_CLAUSE_DECL (c);
5844 if (TREE_CODE (t) != PARM_DECL)
5846 if (processing_template_decl)
5847 break;
5848 if (DECL_P (t))
5849 error ("%qD is not an argument in %<uniform%> clause", t);
5850 else
5851 error ("%qE is not an argument in %<uniform%> clause", t);
5852 remove = true;
5853 break;
5855 goto check_dup_generic;
5857 case OMP_CLAUSE_NOWAIT:
5858 case OMP_CLAUSE_ORDERED:
5859 case OMP_CLAUSE_DEFAULT:
5860 case OMP_CLAUSE_UNTIED:
5861 case OMP_CLAUSE_COLLAPSE:
5862 case OMP_CLAUSE_MERGEABLE:
5863 case OMP_CLAUSE_PARALLEL:
5864 case OMP_CLAUSE_FOR:
5865 case OMP_CLAUSE_SECTIONS:
5866 case OMP_CLAUSE_TASKGROUP:
5867 case OMP_CLAUSE_PROC_BIND:
5868 case OMP_CLAUSE__CILK_FOR_COUNT_:
5869 break;
5871 case OMP_CLAUSE_INBRANCH:
5872 case OMP_CLAUSE_NOTINBRANCH:
5873 if (branch_seen)
5875 error ("%<inbranch%> clause is incompatible with "
5876 "%<notinbranch%>");
5877 remove = true;
5879 branch_seen = true;
5880 break;
5882 default:
5883 gcc_unreachable ();
5886 if (remove)
5887 *pc = OMP_CLAUSE_CHAIN (c);
5888 else
5889 pc = &OMP_CLAUSE_CHAIN (c);
5892 for (pc = &clauses, c = clauses; c ; c = *pc)
5894 enum omp_clause_code c_kind = OMP_CLAUSE_CODE (c);
5895 bool remove = false;
5896 bool need_complete_non_reference = false;
5897 bool need_default_ctor = false;
5898 bool need_copy_ctor = false;
5899 bool need_copy_assignment = false;
5900 bool need_implicitly_determined = false;
5901 bool need_dtor = false;
5902 tree type, inner_type;
5904 switch (c_kind)
5906 case OMP_CLAUSE_SHARED:
5907 need_implicitly_determined = true;
5908 break;
5909 case OMP_CLAUSE_PRIVATE:
5910 need_complete_non_reference = true;
5911 need_default_ctor = true;
5912 need_dtor = true;
5913 need_implicitly_determined = true;
5914 break;
5915 case OMP_CLAUSE_FIRSTPRIVATE:
5916 need_complete_non_reference = true;
5917 need_copy_ctor = true;
5918 need_dtor = true;
5919 need_implicitly_determined = true;
5920 break;
5921 case OMP_CLAUSE_LASTPRIVATE:
5922 need_complete_non_reference = true;
5923 need_copy_assignment = true;
5924 need_implicitly_determined = true;
5925 break;
5926 case OMP_CLAUSE_REDUCTION:
5927 need_implicitly_determined = true;
5928 break;
5929 case OMP_CLAUSE_COPYPRIVATE:
5930 need_copy_assignment = true;
5931 break;
5932 case OMP_CLAUSE_COPYIN:
5933 need_copy_assignment = true;
5934 break;
5935 case OMP_CLAUSE_NOWAIT:
5936 if (copyprivate_seen)
5938 error_at (OMP_CLAUSE_LOCATION (c),
5939 "%<nowait%> clause must not be used together "
5940 "with %<copyprivate%>");
5941 *pc = OMP_CLAUSE_CHAIN (c);
5942 continue;
5944 /* FALLTHRU */
5945 default:
5946 pc = &OMP_CLAUSE_CHAIN (c);
5947 continue;
5950 t = OMP_CLAUSE_DECL (c);
5951 if (processing_template_decl
5952 && !VAR_P (t) && TREE_CODE (t) != PARM_DECL)
5954 pc = &OMP_CLAUSE_CHAIN (c);
5955 continue;
5958 switch (c_kind)
5960 case OMP_CLAUSE_LASTPRIVATE:
5961 if (!bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
5963 need_default_ctor = true;
5964 need_dtor = true;
5966 break;
5968 case OMP_CLAUSE_REDUCTION:
5969 if (finish_omp_reduction_clause (c, &need_default_ctor,
5970 &need_dtor))
5971 remove = true;
5972 else
5973 t = OMP_CLAUSE_DECL (c);
5974 break;
5976 case OMP_CLAUSE_COPYIN:
5977 if (!VAR_P (t) || !CP_DECL_THREAD_LOCAL_P (t))
5979 error ("%qE must be %<threadprivate%> for %<copyin%>", t);
5980 remove = true;
5982 break;
5984 default:
5985 break;
5988 if (need_complete_non_reference || need_copy_assignment)
5990 t = require_complete_type (t);
5991 if (t == error_mark_node)
5992 remove = true;
5993 else if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE
5994 && need_complete_non_reference)
5996 error ("%qE has reference type for %qs", t,
5997 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5998 remove = true;
6001 if (need_implicitly_determined)
6003 const char *share_name = NULL;
6005 if (VAR_P (t) && CP_DECL_THREAD_LOCAL_P (t))
6006 share_name = "threadprivate";
6007 else switch (cxx_omp_predetermined_sharing (t))
6009 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
6010 break;
6011 case OMP_CLAUSE_DEFAULT_SHARED:
6012 /* const vars may be specified in firstprivate clause. */
6013 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
6014 && cxx_omp_const_qual_no_mutable (t))
6015 break;
6016 share_name = "shared";
6017 break;
6018 case OMP_CLAUSE_DEFAULT_PRIVATE:
6019 share_name = "private";
6020 break;
6021 default:
6022 gcc_unreachable ();
6024 if (share_name)
6026 error ("%qE is predetermined %qs for %qs",
6027 t, share_name, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6028 remove = true;
6032 /* We're interested in the base element, not arrays. */
6033 inner_type = type = TREE_TYPE (t);
6034 while (TREE_CODE (inner_type) == ARRAY_TYPE)
6035 inner_type = TREE_TYPE (inner_type);
6037 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
6038 && TREE_CODE (inner_type) == REFERENCE_TYPE)
6039 inner_type = TREE_TYPE (inner_type);
6041 /* Check for special function availability by building a call to one.
6042 Save the results, because later we won't be in the right context
6043 for making these queries. */
6044 if (CLASS_TYPE_P (inner_type)
6045 && COMPLETE_TYPE_P (inner_type)
6046 && (need_default_ctor || need_copy_ctor
6047 || need_copy_assignment || need_dtor)
6048 && !type_dependent_expression_p (t)
6049 && cxx_omp_create_clause_info (c, inner_type, need_default_ctor,
6050 need_copy_ctor, need_copy_assignment,
6051 need_dtor))
6052 remove = true;
6054 if (remove)
6055 *pc = OMP_CLAUSE_CHAIN (c);
6056 else
6057 pc = &OMP_CLAUSE_CHAIN (c);
6060 bitmap_obstack_release (NULL);
6061 return clauses;
6064 /* For all variables in the tree_list VARS, mark them as thread local. */
6066 void
6067 finish_omp_threadprivate (tree vars)
6069 tree t;
6071 /* Mark every variable in VARS to be assigned thread local storage. */
6072 for (t = vars; t; t = TREE_CHAIN (t))
6074 tree v = TREE_PURPOSE (t);
6076 if (error_operand_p (v))
6078 else if (!VAR_P (v))
6079 error ("%<threadprivate%> %qD is not file, namespace "
6080 "or block scope variable", v);
6081 /* If V had already been marked threadprivate, it doesn't matter
6082 whether it had been used prior to this point. */
6083 else if (TREE_USED (v)
6084 && (DECL_LANG_SPECIFIC (v) == NULL
6085 || !CP_DECL_THREADPRIVATE_P (v)))
6086 error ("%qE declared %<threadprivate%> after first use", v);
6087 else if (! TREE_STATIC (v) && ! DECL_EXTERNAL (v))
6088 error ("automatic variable %qE cannot be %<threadprivate%>", v);
6089 else if (! COMPLETE_TYPE_P (complete_type (TREE_TYPE (v))))
6090 error ("%<threadprivate%> %qE has incomplete type", v);
6091 else if (TREE_STATIC (v) && TYPE_P (CP_DECL_CONTEXT (v))
6092 && CP_DECL_CONTEXT (v) != current_class_type)
6093 error ("%<threadprivate%> %qE directive not "
6094 "in %qT definition", v, CP_DECL_CONTEXT (v));
6095 else
6097 /* Allocate a LANG_SPECIFIC structure for V, if needed. */
6098 if (DECL_LANG_SPECIFIC (v) == NULL)
6100 retrofit_lang_decl (v);
6102 /* Make sure that DECL_DISCRIMINATOR_P continues to be true
6103 after the allocation of the lang_decl structure. */
6104 if (DECL_DISCRIMINATOR_P (v))
6105 DECL_LANG_SPECIFIC (v)->u.base.u2sel = 1;
6108 if (! CP_DECL_THREAD_LOCAL_P (v))
6110 CP_DECL_THREAD_LOCAL_P (v) = true;
6111 set_decl_tls_model (v, decl_default_tls_model (v));
6112 /* If rtl has been already set for this var, call
6113 make_decl_rtl once again, so that encode_section_info
6114 has a chance to look at the new decl flags. */
6115 if (DECL_RTL_SET_P (v))
6116 make_decl_rtl (v);
6118 CP_DECL_THREADPRIVATE_P (v) = 1;
6123 /* Build an OpenMP structured block. */
6125 tree
6126 begin_omp_structured_block (void)
6128 return do_pushlevel (sk_omp);
6131 tree
6132 finish_omp_structured_block (tree block)
6134 return do_poplevel (block);
6137 /* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
6138 statement. LOC is the location of the OACC_DATA. */
6140 tree
6141 finish_oacc_data (tree clauses, tree block)
6143 tree stmt;
6145 block = finish_omp_structured_block (block);
6147 stmt = make_node (OACC_DATA);
6148 TREE_TYPE (stmt) = void_type_node;
6149 OACC_DATA_CLAUSES (stmt) = clauses;
6150 OACC_DATA_BODY (stmt) = block;
6152 return add_stmt (stmt);
6155 /* Generate OACC_KERNELS, with CLAUSES and BLOCK as its compound
6156 statement. LOC is the location of the OACC_KERNELS. */
6158 tree
6159 finish_oacc_kernels (tree clauses, tree block)
6161 tree stmt;
6163 block = finish_omp_structured_block (block);
6165 stmt = make_node (OACC_KERNELS);
6166 TREE_TYPE (stmt) = void_type_node;
6167 OACC_KERNELS_CLAUSES (stmt) = clauses;
6168 OACC_KERNELS_BODY (stmt) = block;
6170 return add_stmt (stmt);
6173 /* Generate OACC_PARALLEL, with CLAUSES and BLOCK as its compound
6174 statement. LOC is the location of the OACC_PARALLEL. */
6176 tree
6177 finish_oacc_parallel (tree clauses, tree block)
6179 tree stmt;
6181 block = finish_omp_structured_block (block);
6183 stmt = make_node (OACC_PARALLEL);
6184 TREE_TYPE (stmt) = void_type_node;
6185 OACC_PARALLEL_CLAUSES (stmt) = clauses;
6186 OACC_PARALLEL_BODY (stmt) = block;
6188 return add_stmt (stmt);
6191 /* Similarly, except force the retention of the BLOCK. */
6193 tree
6194 begin_omp_parallel (void)
6196 keep_next_level (true);
6197 return begin_omp_structured_block ();
6200 tree
6201 finish_omp_parallel (tree clauses, tree body)
6203 tree stmt;
6205 body = finish_omp_structured_block (body);
6207 stmt = make_node (OMP_PARALLEL);
6208 TREE_TYPE (stmt) = void_type_node;
6209 OMP_PARALLEL_CLAUSES (stmt) = clauses;
6210 OMP_PARALLEL_BODY (stmt) = body;
6212 return add_stmt (stmt);
6215 tree
6216 begin_omp_task (void)
6218 keep_next_level (true);
6219 return begin_omp_structured_block ();
6222 tree
6223 finish_omp_task (tree clauses, tree body)
6225 tree stmt;
6227 body = finish_omp_structured_block (body);
6229 stmt = make_node (OMP_TASK);
6230 TREE_TYPE (stmt) = void_type_node;
6231 OMP_TASK_CLAUSES (stmt) = clauses;
6232 OMP_TASK_BODY (stmt) = body;
6234 return add_stmt (stmt);
6237 /* Helper function for finish_omp_for. Convert Ith random access iterator
6238 into integral iterator. Return FALSE if successful. */
6240 static bool
6241 handle_omp_for_class_iterator (int i, location_t locus, tree declv, tree initv,
6242 tree condv, tree incrv, tree *body,
6243 tree *pre_body, tree clauses, tree *lastp)
6245 tree diff, iter_init, iter_incr = NULL, last;
6246 tree incr_var = NULL, orig_pre_body, orig_body, c;
6247 tree decl = TREE_VEC_ELT (declv, i);
6248 tree init = TREE_VEC_ELT (initv, i);
6249 tree cond = TREE_VEC_ELT (condv, i);
6250 tree incr = TREE_VEC_ELT (incrv, i);
6251 tree iter = decl;
6252 location_t elocus = locus;
6254 if (init && EXPR_HAS_LOCATION (init))
6255 elocus = EXPR_LOCATION (init);
6257 switch (TREE_CODE (cond))
6259 case GT_EXPR:
6260 case GE_EXPR:
6261 case LT_EXPR:
6262 case LE_EXPR:
6263 case NE_EXPR:
6264 if (TREE_OPERAND (cond, 1) == iter)
6265 cond = build2 (swap_tree_comparison (TREE_CODE (cond)),
6266 TREE_TYPE (cond), iter, TREE_OPERAND (cond, 0));
6267 if (TREE_OPERAND (cond, 0) != iter)
6268 cond = error_mark_node;
6269 else
6271 tree tem = build_x_binary_op (EXPR_LOCATION (cond),
6272 TREE_CODE (cond),
6273 iter, ERROR_MARK,
6274 TREE_OPERAND (cond, 1), ERROR_MARK,
6275 NULL, tf_warning_or_error);
6276 if (error_operand_p (tem))
6277 return true;
6279 break;
6280 default:
6281 cond = error_mark_node;
6282 break;
6284 if (cond == error_mark_node)
6286 error_at (elocus, "invalid controlling predicate");
6287 return true;
6289 diff = build_x_binary_op (elocus, MINUS_EXPR, TREE_OPERAND (cond, 1),
6290 ERROR_MARK, iter, ERROR_MARK, NULL,
6291 tf_warning_or_error);
6292 if (error_operand_p (diff))
6293 return true;
6294 if (TREE_CODE (TREE_TYPE (diff)) != INTEGER_TYPE)
6296 error_at (elocus, "difference between %qE and %qD does not have integer type",
6297 TREE_OPERAND (cond, 1), iter);
6298 return true;
6301 switch (TREE_CODE (incr))
6303 case PREINCREMENT_EXPR:
6304 case PREDECREMENT_EXPR:
6305 case POSTINCREMENT_EXPR:
6306 case POSTDECREMENT_EXPR:
6307 if (TREE_OPERAND (incr, 0) != iter)
6309 incr = error_mark_node;
6310 break;
6312 iter_incr = build_x_unary_op (EXPR_LOCATION (incr),
6313 TREE_CODE (incr), iter,
6314 tf_warning_or_error);
6315 if (error_operand_p (iter_incr))
6316 return true;
6317 else if (TREE_CODE (incr) == PREINCREMENT_EXPR
6318 || TREE_CODE (incr) == POSTINCREMENT_EXPR)
6319 incr = integer_one_node;
6320 else
6321 incr = integer_minus_one_node;
6322 break;
6323 case MODIFY_EXPR:
6324 if (TREE_OPERAND (incr, 0) != iter)
6325 incr = error_mark_node;
6326 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
6327 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
6329 tree rhs = TREE_OPERAND (incr, 1);
6330 if (TREE_OPERAND (rhs, 0) == iter)
6332 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (rhs, 1)))
6333 != INTEGER_TYPE)
6334 incr = error_mark_node;
6335 else
6337 iter_incr = build_x_modify_expr (EXPR_LOCATION (rhs),
6338 iter, TREE_CODE (rhs),
6339 TREE_OPERAND (rhs, 1),
6340 tf_warning_or_error);
6341 if (error_operand_p (iter_incr))
6342 return true;
6343 incr = TREE_OPERAND (rhs, 1);
6344 incr = cp_convert (TREE_TYPE (diff), incr,
6345 tf_warning_or_error);
6346 if (TREE_CODE (rhs) == MINUS_EXPR)
6348 incr = build1 (NEGATE_EXPR, TREE_TYPE (diff), incr);
6349 incr = fold_if_not_in_template (incr);
6351 if (TREE_CODE (incr) != INTEGER_CST
6352 && (TREE_CODE (incr) != NOP_EXPR
6353 || (TREE_CODE (TREE_OPERAND (incr, 0))
6354 != INTEGER_CST)))
6355 iter_incr = NULL;
6358 else if (TREE_OPERAND (rhs, 1) == iter)
6360 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (rhs, 0))) != INTEGER_TYPE
6361 || TREE_CODE (rhs) != PLUS_EXPR)
6362 incr = error_mark_node;
6363 else
6365 iter_incr = build_x_binary_op (EXPR_LOCATION (rhs),
6366 PLUS_EXPR,
6367 TREE_OPERAND (rhs, 0),
6368 ERROR_MARK, iter,
6369 ERROR_MARK, NULL,
6370 tf_warning_or_error);
6371 if (error_operand_p (iter_incr))
6372 return true;
6373 iter_incr = build_x_modify_expr (EXPR_LOCATION (rhs),
6374 iter, NOP_EXPR,
6375 iter_incr,
6376 tf_warning_or_error);
6377 if (error_operand_p (iter_incr))
6378 return true;
6379 incr = TREE_OPERAND (rhs, 0);
6380 iter_incr = NULL;
6383 else
6384 incr = error_mark_node;
6386 else
6387 incr = error_mark_node;
6388 break;
6389 default:
6390 incr = error_mark_node;
6391 break;
6394 if (incr == error_mark_node)
6396 error_at (elocus, "invalid increment expression");
6397 return true;
6400 incr = cp_convert (TREE_TYPE (diff), incr, tf_warning_or_error);
6401 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
6402 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
6403 && OMP_CLAUSE_DECL (c) == iter)
6404 break;
6406 decl = create_temporary_var (TREE_TYPE (diff));
6407 pushdecl (decl);
6408 add_decl_expr (decl);
6409 last = create_temporary_var (TREE_TYPE (diff));
6410 pushdecl (last);
6411 add_decl_expr (last);
6412 if (c && iter_incr == NULL)
6414 incr_var = create_temporary_var (TREE_TYPE (diff));
6415 pushdecl (incr_var);
6416 add_decl_expr (incr_var);
6418 gcc_assert (stmts_are_full_exprs_p ());
6420 orig_pre_body = *pre_body;
6421 *pre_body = push_stmt_list ();
6422 if (orig_pre_body)
6423 add_stmt (orig_pre_body);
6424 if (init != NULL)
6425 finish_expr_stmt (build_x_modify_expr (elocus,
6426 iter, NOP_EXPR, init,
6427 tf_warning_or_error));
6428 init = build_int_cst (TREE_TYPE (diff), 0);
6429 if (c && iter_incr == NULL)
6431 finish_expr_stmt (build_x_modify_expr (elocus,
6432 incr_var, NOP_EXPR,
6433 incr, tf_warning_or_error));
6434 incr = incr_var;
6435 iter_incr = build_x_modify_expr (elocus,
6436 iter, PLUS_EXPR, incr,
6437 tf_warning_or_error);
6439 finish_expr_stmt (build_x_modify_expr (elocus,
6440 last, NOP_EXPR, init,
6441 tf_warning_or_error));
6442 *pre_body = pop_stmt_list (*pre_body);
6444 cond = cp_build_binary_op (elocus,
6445 TREE_CODE (cond), decl, diff,
6446 tf_warning_or_error);
6447 incr = build_modify_expr (elocus, decl, NULL_TREE, PLUS_EXPR,
6448 elocus, incr, NULL_TREE);
6450 orig_body = *body;
6451 *body = push_stmt_list ();
6452 iter_init = build2 (MINUS_EXPR, TREE_TYPE (diff), decl, last);
6453 iter_init = build_x_modify_expr (elocus,
6454 iter, PLUS_EXPR, iter_init,
6455 tf_warning_or_error);
6456 iter_init = build1 (NOP_EXPR, void_type_node, iter_init);
6457 finish_expr_stmt (iter_init);
6458 finish_expr_stmt (build_x_modify_expr (elocus,
6459 last, NOP_EXPR, decl,
6460 tf_warning_or_error));
6461 add_stmt (orig_body);
6462 *body = pop_stmt_list (*body);
6464 if (c)
6466 OMP_CLAUSE_LASTPRIVATE_STMT (c) = push_stmt_list ();
6467 finish_expr_stmt (iter_incr);
6468 OMP_CLAUSE_LASTPRIVATE_STMT (c)
6469 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (c));
6472 TREE_VEC_ELT (declv, i) = decl;
6473 TREE_VEC_ELT (initv, i) = init;
6474 TREE_VEC_ELT (condv, i) = cond;
6475 TREE_VEC_ELT (incrv, i) = incr;
6476 *lastp = last;
6478 return false;
6481 /* Build and validate an OMP_FOR statement. CLAUSES, BODY, COND, INCR
6482 are directly for their associated operands in the statement. DECL
6483 and INIT are a combo; if DECL is NULL then INIT ought to be a
6484 MODIFY_EXPR, and the DECL should be extracted. PRE_BODY are
6485 optional statements that need to go before the loop into its
6486 sk_omp scope. */
6488 tree
6489 finish_omp_for (location_t locus, enum tree_code code, tree declv, tree initv,
6490 tree condv, tree incrv, tree body, tree pre_body, tree clauses)
6492 tree omp_for = NULL, orig_incr = NULL;
6493 tree decl = NULL, init, cond, incr, orig_decl = NULL_TREE, block = NULL_TREE;
6494 tree last = NULL_TREE;
6495 location_t elocus;
6496 int i;
6498 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (initv));
6499 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (condv));
6500 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (incrv));
6501 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
6503 decl = TREE_VEC_ELT (declv, i);
6504 init = TREE_VEC_ELT (initv, i);
6505 cond = TREE_VEC_ELT (condv, i);
6506 incr = TREE_VEC_ELT (incrv, i);
6507 elocus = locus;
6509 if (decl == NULL)
6511 if (init != NULL)
6512 switch (TREE_CODE (init))
6514 case MODIFY_EXPR:
6515 decl = TREE_OPERAND (init, 0);
6516 init = TREE_OPERAND (init, 1);
6517 break;
6518 case MODOP_EXPR:
6519 if (TREE_CODE (TREE_OPERAND (init, 1)) == NOP_EXPR)
6521 decl = TREE_OPERAND (init, 0);
6522 init = TREE_OPERAND (init, 2);
6524 break;
6525 default:
6526 break;
6529 if (decl == NULL)
6531 error_at (locus,
6532 "expected iteration declaration or initialization");
6533 return NULL;
6537 if (init && EXPR_HAS_LOCATION (init))
6538 elocus = EXPR_LOCATION (init);
6540 if (cond == NULL)
6542 error_at (elocus, "missing controlling predicate");
6543 return NULL;
6546 if (incr == NULL)
6548 error_at (elocus, "missing increment expression");
6549 return NULL;
6552 TREE_VEC_ELT (declv, i) = decl;
6553 TREE_VEC_ELT (initv, i) = init;
6556 if (dependent_omp_for_p (declv, initv, condv, incrv))
6558 tree stmt;
6560 stmt = make_node (code);
6562 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
6564 /* This is really just a place-holder. We'll be decomposing this
6565 again and going through the cp_build_modify_expr path below when
6566 we instantiate the thing. */
6567 TREE_VEC_ELT (initv, i)
6568 = build2 (MODIFY_EXPR, void_type_node, TREE_VEC_ELT (declv, i),
6569 TREE_VEC_ELT (initv, i));
6572 TREE_TYPE (stmt) = void_type_node;
6573 OMP_FOR_INIT (stmt) = initv;
6574 OMP_FOR_COND (stmt) = condv;
6575 OMP_FOR_INCR (stmt) = incrv;
6576 OMP_FOR_BODY (stmt) = body;
6577 OMP_FOR_PRE_BODY (stmt) = pre_body;
6578 OMP_FOR_CLAUSES (stmt) = clauses;
6580 SET_EXPR_LOCATION (stmt, locus);
6581 return add_stmt (stmt);
6584 if (processing_template_decl)
6585 orig_incr = make_tree_vec (TREE_VEC_LENGTH (incrv));
6587 for (i = 0; i < TREE_VEC_LENGTH (declv); )
6589 decl = TREE_VEC_ELT (declv, i);
6590 init = TREE_VEC_ELT (initv, i);
6591 cond = TREE_VEC_ELT (condv, i);
6592 incr = TREE_VEC_ELT (incrv, i);
6593 if (orig_incr)
6594 TREE_VEC_ELT (orig_incr, i) = incr;
6595 elocus = locus;
6597 if (init && EXPR_HAS_LOCATION (init))
6598 elocus = EXPR_LOCATION (init);
6600 if (!DECL_P (decl))
6602 error_at (elocus, "expected iteration declaration or initialization");
6603 return NULL;
6606 if (incr && TREE_CODE (incr) == MODOP_EXPR)
6608 if (orig_incr)
6609 TREE_VEC_ELT (orig_incr, i) = incr;
6610 incr = cp_build_modify_expr (TREE_OPERAND (incr, 0),
6611 TREE_CODE (TREE_OPERAND (incr, 1)),
6612 TREE_OPERAND (incr, 2),
6613 tf_warning_or_error);
6616 if (CLASS_TYPE_P (TREE_TYPE (decl)))
6618 if (code == OMP_SIMD)
6620 error_at (elocus, "%<#pragma omp simd%> used with class "
6621 "iteration variable %qE", decl);
6622 return NULL;
6624 if (code == CILK_FOR && i == 0)
6625 orig_decl = decl;
6626 if (handle_omp_for_class_iterator (i, locus, declv, initv, condv,
6627 incrv, &body, &pre_body,
6628 clauses, &last))
6629 return NULL;
6630 continue;
6633 if (!INTEGRAL_TYPE_P (TREE_TYPE (decl))
6634 && !TYPE_PTR_P (TREE_TYPE (decl)))
6636 error_at (elocus, "invalid type for iteration variable %qE", decl);
6637 return NULL;
6640 if (!processing_template_decl)
6642 init = fold_build_cleanup_point_expr (TREE_TYPE (init), init);
6643 init = cp_build_modify_expr (decl, NOP_EXPR, init, tf_warning_or_error);
6645 else
6646 init = build2 (MODIFY_EXPR, void_type_node, decl, init);
6647 if (cond
6648 && TREE_SIDE_EFFECTS (cond)
6649 && COMPARISON_CLASS_P (cond)
6650 && !processing_template_decl)
6652 tree t = TREE_OPERAND (cond, 0);
6653 if (TREE_SIDE_EFFECTS (t)
6654 && t != decl
6655 && (TREE_CODE (t) != NOP_EXPR
6656 || TREE_OPERAND (t, 0) != decl))
6657 TREE_OPERAND (cond, 0)
6658 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6660 t = TREE_OPERAND (cond, 1);
6661 if (TREE_SIDE_EFFECTS (t)
6662 && t != decl
6663 && (TREE_CODE (t) != NOP_EXPR
6664 || TREE_OPERAND (t, 0) != decl))
6665 TREE_OPERAND (cond, 1)
6666 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6668 if (decl == error_mark_node || init == error_mark_node)
6669 return NULL;
6671 TREE_VEC_ELT (declv, i) = decl;
6672 TREE_VEC_ELT (initv, i) = init;
6673 TREE_VEC_ELT (condv, i) = cond;
6674 TREE_VEC_ELT (incrv, i) = incr;
6675 i++;
6678 if (IS_EMPTY_STMT (pre_body))
6679 pre_body = NULL;
6681 if (code == CILK_FOR && !processing_template_decl)
6682 block = push_stmt_list ();
6684 omp_for = c_finish_omp_for (locus, code, declv, initv, condv, incrv,
6685 body, pre_body);
6687 if (omp_for == NULL)
6689 if (block)
6690 pop_stmt_list (block);
6691 return NULL;
6694 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INCR (omp_for)); i++)
6696 decl = TREE_OPERAND (TREE_VEC_ELT (OMP_FOR_INIT (omp_for), i), 0);
6697 incr = TREE_VEC_ELT (OMP_FOR_INCR (omp_for), i);
6699 if (TREE_CODE (incr) != MODIFY_EXPR)
6700 continue;
6702 if (TREE_SIDE_EFFECTS (TREE_OPERAND (incr, 1))
6703 && BINARY_CLASS_P (TREE_OPERAND (incr, 1))
6704 && !processing_template_decl)
6706 tree t = TREE_OPERAND (TREE_OPERAND (incr, 1), 0);
6707 if (TREE_SIDE_EFFECTS (t)
6708 && t != decl
6709 && (TREE_CODE (t) != NOP_EXPR
6710 || TREE_OPERAND (t, 0) != decl))
6711 TREE_OPERAND (TREE_OPERAND (incr, 1), 0)
6712 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6714 t = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
6715 if (TREE_SIDE_EFFECTS (t)
6716 && t != decl
6717 && (TREE_CODE (t) != NOP_EXPR
6718 || TREE_OPERAND (t, 0) != decl))
6719 TREE_OPERAND (TREE_OPERAND (incr, 1), 1)
6720 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6723 if (orig_incr)
6724 TREE_VEC_ELT (OMP_FOR_INCR (omp_for), i) = TREE_VEC_ELT (orig_incr, i);
6726 OMP_FOR_CLAUSES (omp_for) = clauses;
6728 if (block)
6730 tree omp_par = make_node (OMP_PARALLEL);
6731 TREE_TYPE (omp_par) = void_type_node;
6732 OMP_PARALLEL_CLAUSES (omp_par) = NULL_TREE;
6733 tree bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
6734 TREE_SIDE_EFFECTS (bind) = 1;
6735 BIND_EXPR_BODY (bind) = pop_stmt_list (block);
6736 OMP_PARALLEL_BODY (omp_par) = bind;
6737 if (OMP_FOR_PRE_BODY (omp_for))
6739 add_stmt (OMP_FOR_PRE_BODY (omp_for));
6740 OMP_FOR_PRE_BODY (omp_for) = NULL_TREE;
6742 init = TREE_VEC_ELT (OMP_FOR_INIT (omp_for), 0);
6743 decl = TREE_OPERAND (init, 0);
6744 cond = TREE_VEC_ELT (OMP_FOR_COND (omp_for), 0);
6745 incr = TREE_VEC_ELT (OMP_FOR_INCR (omp_for), 0);
6746 tree t = TREE_OPERAND (cond, 1), c, clauses, *pc;
6747 clauses = OMP_FOR_CLAUSES (omp_for);
6748 OMP_FOR_CLAUSES (omp_for) = NULL_TREE;
6749 for (pc = &clauses; *pc; )
6750 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_SCHEDULE)
6752 gcc_assert (OMP_FOR_CLAUSES (omp_for) == NULL_TREE);
6753 OMP_FOR_CLAUSES (omp_for) = *pc;
6754 *pc = OMP_CLAUSE_CHAIN (*pc);
6755 OMP_CLAUSE_CHAIN (OMP_FOR_CLAUSES (omp_for)) = NULL_TREE;
6757 else
6759 gcc_assert (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_FIRSTPRIVATE);
6760 pc = &OMP_CLAUSE_CHAIN (*pc);
6762 if (TREE_CODE (t) != INTEGER_CST)
6764 TREE_OPERAND (cond, 1) = get_temp_regvar (TREE_TYPE (t), t);
6765 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
6766 OMP_CLAUSE_DECL (c) = TREE_OPERAND (cond, 1);
6767 OMP_CLAUSE_CHAIN (c) = clauses;
6768 clauses = c;
6770 if (TREE_CODE (incr) == MODIFY_EXPR)
6772 t = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
6773 if (TREE_CODE (t) != INTEGER_CST)
6775 TREE_OPERAND (TREE_OPERAND (incr, 1), 1)
6776 = get_temp_regvar (TREE_TYPE (t), t);
6777 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
6778 OMP_CLAUSE_DECL (c) = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
6779 OMP_CLAUSE_CHAIN (c) = clauses;
6780 clauses = c;
6783 t = TREE_OPERAND (init, 1);
6784 if (TREE_CODE (t) != INTEGER_CST)
6786 TREE_OPERAND (init, 1) = get_temp_regvar (TREE_TYPE (t), t);
6787 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
6788 OMP_CLAUSE_DECL (c) = TREE_OPERAND (init, 1);
6789 OMP_CLAUSE_CHAIN (c) = clauses;
6790 clauses = c;
6792 if (orig_decl && orig_decl != decl)
6794 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
6795 OMP_CLAUSE_DECL (c) = orig_decl;
6796 OMP_CLAUSE_CHAIN (c) = clauses;
6797 clauses = c;
6799 if (last)
6801 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
6802 OMP_CLAUSE_DECL (c) = last;
6803 OMP_CLAUSE_CHAIN (c) = clauses;
6804 clauses = c;
6806 c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
6807 OMP_CLAUSE_DECL (c) = decl;
6808 OMP_CLAUSE_CHAIN (c) = clauses;
6809 clauses = c;
6810 c = build_omp_clause (input_location, OMP_CLAUSE__CILK_FOR_COUNT_);
6811 OMP_CLAUSE_OPERAND (c, 0)
6812 = cilk_for_number_of_iterations (omp_for);
6813 OMP_CLAUSE_CHAIN (c) = clauses;
6814 OMP_PARALLEL_CLAUSES (omp_par) = finish_omp_clauses (c);
6815 add_stmt (omp_par);
6816 return omp_par;
6818 else if (code == CILK_FOR && processing_template_decl)
6820 tree c, clauses = OMP_FOR_CLAUSES (omp_for);
6821 if (orig_decl && orig_decl != decl)
6823 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
6824 OMP_CLAUSE_DECL (c) = orig_decl;
6825 OMP_CLAUSE_CHAIN (c) = clauses;
6826 clauses = c;
6828 if (last)
6830 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
6831 OMP_CLAUSE_DECL (c) = last;
6832 OMP_CLAUSE_CHAIN (c) = clauses;
6833 clauses = c;
6835 OMP_FOR_CLAUSES (omp_for) = clauses;
6837 return omp_for;
6840 void
6841 finish_omp_atomic (enum tree_code code, enum tree_code opcode, tree lhs,
6842 tree rhs, tree v, tree lhs1, tree rhs1, bool seq_cst)
6844 tree orig_lhs;
6845 tree orig_rhs;
6846 tree orig_v;
6847 tree orig_lhs1;
6848 tree orig_rhs1;
6849 bool dependent_p;
6850 tree stmt;
6852 orig_lhs = lhs;
6853 orig_rhs = rhs;
6854 orig_v = v;
6855 orig_lhs1 = lhs1;
6856 orig_rhs1 = rhs1;
6857 dependent_p = false;
6858 stmt = NULL_TREE;
6860 /* Even in a template, we can detect invalid uses of the atomic
6861 pragma if neither LHS nor RHS is type-dependent. */
6862 if (processing_template_decl)
6864 dependent_p = (type_dependent_expression_p (lhs)
6865 || (rhs && type_dependent_expression_p (rhs))
6866 || (v && type_dependent_expression_p (v))
6867 || (lhs1 && type_dependent_expression_p (lhs1))
6868 || (rhs1 && type_dependent_expression_p (rhs1)));
6869 if (!dependent_p)
6871 lhs = build_non_dependent_expr (lhs);
6872 if (rhs)
6873 rhs = build_non_dependent_expr (rhs);
6874 if (v)
6875 v = build_non_dependent_expr (v);
6876 if (lhs1)
6877 lhs1 = build_non_dependent_expr (lhs1);
6878 if (rhs1)
6879 rhs1 = build_non_dependent_expr (rhs1);
6882 if (!dependent_p)
6884 bool swapped = false;
6885 if (rhs1 && cp_tree_equal (lhs, rhs))
6887 std::swap (rhs, rhs1);
6888 swapped = !commutative_tree_code (opcode);
6890 if (rhs1 && !cp_tree_equal (lhs, rhs1))
6892 if (code == OMP_ATOMIC)
6893 error ("%<#pragma omp atomic update%> uses two different "
6894 "expressions for memory");
6895 else
6896 error ("%<#pragma omp atomic capture%> uses two different "
6897 "expressions for memory");
6898 return;
6900 if (lhs1 && !cp_tree_equal (lhs, lhs1))
6902 if (code == OMP_ATOMIC)
6903 error ("%<#pragma omp atomic update%> uses two different "
6904 "expressions for memory");
6905 else
6906 error ("%<#pragma omp atomic capture%> uses two different "
6907 "expressions for memory");
6908 return;
6910 stmt = c_finish_omp_atomic (input_location, code, opcode, lhs, rhs,
6911 v, lhs1, rhs1, swapped, seq_cst);
6912 if (stmt == error_mark_node)
6913 return;
6915 if (processing_template_decl)
6917 if (code == OMP_ATOMIC_READ)
6919 stmt = build_min_nt_loc (EXPR_LOCATION (orig_lhs),
6920 OMP_ATOMIC_READ, orig_lhs);
6921 OMP_ATOMIC_SEQ_CST (stmt) = seq_cst;
6922 stmt = build2 (MODIFY_EXPR, void_type_node, orig_v, stmt);
6924 else
6926 if (opcode == NOP_EXPR)
6927 stmt = build2 (MODIFY_EXPR, void_type_node, orig_lhs, orig_rhs);
6928 else
6929 stmt = build2 (opcode, void_type_node, orig_lhs, orig_rhs);
6930 if (orig_rhs1)
6931 stmt = build_min_nt_loc (EXPR_LOCATION (orig_rhs1),
6932 COMPOUND_EXPR, orig_rhs1, stmt);
6933 if (code != OMP_ATOMIC)
6935 stmt = build_min_nt_loc (EXPR_LOCATION (orig_lhs1),
6936 code, orig_lhs1, stmt);
6937 OMP_ATOMIC_SEQ_CST (stmt) = seq_cst;
6938 stmt = build2 (MODIFY_EXPR, void_type_node, orig_v, stmt);
6941 stmt = build2 (OMP_ATOMIC, void_type_node, integer_zero_node, stmt);
6942 OMP_ATOMIC_SEQ_CST (stmt) = seq_cst;
6944 finish_expr_stmt (stmt);
6947 void
6948 finish_omp_barrier (void)
6950 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_BARRIER);
6951 vec<tree, va_gc> *vec = make_tree_vector ();
6952 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
6953 release_tree_vector (vec);
6954 finish_expr_stmt (stmt);
6957 void
6958 finish_omp_flush (void)
6960 tree fn = builtin_decl_explicit (BUILT_IN_SYNC_SYNCHRONIZE);
6961 vec<tree, va_gc> *vec = make_tree_vector ();
6962 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
6963 release_tree_vector (vec);
6964 finish_expr_stmt (stmt);
6967 void
6968 finish_omp_taskwait (void)
6970 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_TASKWAIT);
6971 vec<tree, va_gc> *vec = make_tree_vector ();
6972 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
6973 release_tree_vector (vec);
6974 finish_expr_stmt (stmt);
6977 void
6978 finish_omp_taskyield (void)
6980 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_TASKYIELD);
6981 vec<tree, va_gc> *vec = make_tree_vector ();
6982 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
6983 release_tree_vector (vec);
6984 finish_expr_stmt (stmt);
6987 void
6988 finish_omp_cancel (tree clauses)
6990 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
6991 int mask = 0;
6992 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
6993 mask = 1;
6994 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
6995 mask = 2;
6996 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
6997 mask = 4;
6998 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
6999 mask = 8;
7000 else
7002 error ("%<#pragma omp cancel must specify one of "
7003 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> clauses");
7004 return;
7006 vec<tree, va_gc> *vec = make_tree_vector ();
7007 tree ifc = find_omp_clause (clauses, OMP_CLAUSE_IF);
7008 if (ifc != NULL_TREE)
7010 tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
7011 ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
7012 boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
7013 build_zero_cst (type));
7015 else
7016 ifc = boolean_true_node;
7017 vec->quick_push (build_int_cst (integer_type_node, mask));
7018 vec->quick_push (ifc);
7019 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
7020 release_tree_vector (vec);
7021 finish_expr_stmt (stmt);
7024 void
7025 finish_omp_cancellation_point (tree clauses)
7027 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
7028 int mask = 0;
7029 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
7030 mask = 1;
7031 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
7032 mask = 2;
7033 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
7034 mask = 4;
7035 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
7036 mask = 8;
7037 else
7039 error ("%<#pragma omp cancellation point must specify one of "
7040 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> clauses");
7041 return;
7043 vec<tree, va_gc> *vec
7044 = make_tree_vector_single (build_int_cst (integer_type_node, mask));
7045 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
7046 release_tree_vector (vec);
7047 finish_expr_stmt (stmt);
7050 /* Begin a __transaction_atomic or __transaction_relaxed statement.
7051 If PCOMPOUND is non-null, this is for a function-transaction-block, and we
7052 should create an extra compound stmt. */
7054 tree
7055 begin_transaction_stmt (location_t loc, tree *pcompound, int flags)
7057 tree r;
7059 if (pcompound)
7060 *pcompound = begin_compound_stmt (0);
7062 r = build_stmt (loc, TRANSACTION_EXPR, NULL_TREE);
7064 /* Only add the statement to the function if support enabled. */
7065 if (flag_tm)
7066 add_stmt (r);
7067 else
7068 error_at (loc, ((flags & TM_STMT_ATTR_RELAXED) != 0
7069 ? G_("%<__transaction_relaxed%> without "
7070 "transactional memory support enabled")
7071 : G_("%<__transaction_atomic%> without "
7072 "transactional memory support enabled")));
7074 TRANSACTION_EXPR_BODY (r) = push_stmt_list ();
7075 TREE_SIDE_EFFECTS (r) = 1;
7076 return r;
7079 /* End a __transaction_atomic or __transaction_relaxed statement.
7080 If COMPOUND_STMT is non-null, this is for a function-transaction-block,
7081 and we should end the compound. If NOEX is non-NULL, we wrap the body in
7082 a MUST_NOT_THROW_EXPR with NOEX as condition. */
7084 void
7085 finish_transaction_stmt (tree stmt, tree compound_stmt, int flags, tree noex)
7087 TRANSACTION_EXPR_BODY (stmt) = pop_stmt_list (TRANSACTION_EXPR_BODY (stmt));
7088 TRANSACTION_EXPR_OUTER (stmt) = (flags & TM_STMT_ATTR_OUTER) != 0;
7089 TRANSACTION_EXPR_RELAXED (stmt) = (flags & TM_STMT_ATTR_RELAXED) != 0;
7090 TRANSACTION_EXPR_IS_STMT (stmt) = 1;
7092 /* noexcept specifications are not allowed for function transactions. */
7093 gcc_assert (!(noex && compound_stmt));
7094 if (noex)
7096 tree body = build_must_not_throw_expr (TRANSACTION_EXPR_BODY (stmt),
7097 noex);
7098 /* This may not be true when the STATEMENT_LIST is empty. */
7099 if (EXPR_P (body))
7100 SET_EXPR_LOCATION (body, EXPR_LOCATION (TRANSACTION_EXPR_BODY (stmt)));
7101 TREE_SIDE_EFFECTS (body) = 1;
7102 TRANSACTION_EXPR_BODY (stmt) = body;
7105 if (compound_stmt)
7106 finish_compound_stmt (compound_stmt);
7109 /* Build a __transaction_atomic or __transaction_relaxed expression. If
7110 NOEX is non-NULL, we wrap the body in a MUST_NOT_THROW_EXPR with NOEX as
7111 condition. */
7113 tree
7114 build_transaction_expr (location_t loc, tree expr, int flags, tree noex)
7116 tree ret;
7117 if (noex)
7119 expr = build_must_not_throw_expr (expr, noex);
7120 if (EXPR_P (expr))
7121 SET_EXPR_LOCATION (expr, loc);
7122 TREE_SIDE_EFFECTS (expr) = 1;
7124 ret = build1 (TRANSACTION_EXPR, TREE_TYPE (expr), expr);
7125 if (flags & TM_STMT_ATTR_RELAXED)
7126 TRANSACTION_EXPR_RELAXED (ret) = 1;
7127 TREE_SIDE_EFFECTS (ret) = 1;
7128 SET_EXPR_LOCATION (ret, loc);
7129 return ret;
7132 void
7133 init_cp_semantics (void)
7137 /* Build a STATIC_ASSERT for a static assertion with the condition
7138 CONDITION and the message text MESSAGE. LOCATION is the location
7139 of the static assertion in the source code. When MEMBER_P, this
7140 static assertion is a member of a class. */
7141 void
7142 finish_static_assert (tree condition, tree message, location_t location,
7143 bool member_p)
7145 if (message == NULL_TREE
7146 || message == error_mark_node
7147 || condition == NULL_TREE
7148 || condition == error_mark_node)
7149 return;
7151 if (check_for_bare_parameter_packs (condition))
7152 condition = error_mark_node;
7154 if (type_dependent_expression_p (condition)
7155 || value_dependent_expression_p (condition))
7157 /* We're in a template; build a STATIC_ASSERT and put it in
7158 the right place. */
7159 tree assertion;
7161 assertion = make_node (STATIC_ASSERT);
7162 STATIC_ASSERT_CONDITION (assertion) = condition;
7163 STATIC_ASSERT_MESSAGE (assertion) = message;
7164 STATIC_ASSERT_SOURCE_LOCATION (assertion) = location;
7166 if (member_p)
7167 maybe_add_class_template_decl_list (current_class_type,
7168 assertion,
7169 /*friend_p=*/0);
7170 else
7171 add_stmt (assertion);
7173 return;
7176 /* Fold the expression and convert it to a boolean value. */
7177 condition = instantiate_non_dependent_expr (condition);
7178 condition = cp_convert (boolean_type_node, condition, tf_warning_or_error);
7179 condition = maybe_constant_value (condition);
7181 if (TREE_CODE (condition) == INTEGER_CST && !integer_zerop (condition))
7182 /* Do nothing; the condition is satisfied. */
7184 else
7186 location_t saved_loc = input_location;
7188 input_location = location;
7189 if (TREE_CODE (condition) == INTEGER_CST
7190 && integer_zerop (condition))
7192 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
7193 (TREE_TYPE (TREE_TYPE (message))));
7194 int len = TREE_STRING_LENGTH (message) / sz - 1;
7195 /* Report the error. */
7196 if (len == 0)
7197 error ("static assertion failed");
7198 else
7199 error ("static assertion failed: %s",
7200 TREE_STRING_POINTER (message));
7202 else if (condition && condition != error_mark_node)
7204 error ("non-constant condition for static assertion");
7205 if (require_potential_rvalue_constant_expression (condition))
7206 cxx_constant_value (condition);
7208 input_location = saved_loc;
7212 /* Implements the C++0x decltype keyword. Returns the type of EXPR,
7213 suitable for use as a type-specifier.
7215 ID_EXPRESSION_OR_MEMBER_ACCESS_P is true when EXPR was parsed as an
7216 id-expression or a class member access, FALSE when it was parsed as
7217 a full expression. */
7219 tree
7220 finish_decltype_type (tree expr, bool id_expression_or_member_access_p,
7221 tsubst_flags_t complain)
7223 tree type = NULL_TREE;
7225 if (!expr || error_operand_p (expr))
7226 return error_mark_node;
7228 if (TYPE_P (expr)
7229 || TREE_CODE (expr) == TYPE_DECL
7230 || (TREE_CODE (expr) == BIT_NOT_EXPR
7231 && TYPE_P (TREE_OPERAND (expr, 0))))
7233 if (complain & tf_error)
7234 error ("argument to decltype must be an expression");
7235 return error_mark_node;
7238 /* Depending on the resolution of DR 1172, we may later need to distinguish
7239 instantiation-dependent but not type-dependent expressions so that, say,
7240 A<decltype(sizeof(T))>::U doesn't require 'typename'. */
7241 if (instantiation_dependent_expression_p (expr))
7243 type = cxx_make_type (DECLTYPE_TYPE);
7244 DECLTYPE_TYPE_EXPR (type) = expr;
7245 DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (type)
7246 = id_expression_or_member_access_p;
7247 SET_TYPE_STRUCTURAL_EQUALITY (type);
7249 return type;
7252 /* The type denoted by decltype(e) is defined as follows: */
7254 expr = resolve_nondeduced_context (expr);
7256 if (invalid_nonstatic_memfn_p (input_location, expr, complain))
7257 return error_mark_node;
7259 if (type_unknown_p (expr))
7261 if (complain & tf_error)
7262 error ("decltype cannot resolve address of overloaded function");
7263 return error_mark_node;
7266 /* To get the size of a static data member declared as an array of
7267 unknown bound, we need to instantiate it. */
7268 if (VAR_P (expr)
7269 && VAR_HAD_UNKNOWN_BOUND (expr)
7270 && DECL_TEMPLATE_INSTANTIATION (expr))
7271 instantiate_decl (expr, /*defer_ok*/true, /*expl_inst_mem*/false);
7273 if (id_expression_or_member_access_p)
7275 /* If e is an id-expression or a class member access (5.2.5
7276 [expr.ref]), decltype(e) is defined as the type of the entity
7277 named by e. If there is no such entity, or e names a set of
7278 overloaded functions, the program is ill-formed. */
7279 if (identifier_p (expr))
7280 expr = lookup_name (expr);
7282 if (INDIRECT_REF_P (expr))
7283 /* This can happen when the expression is, e.g., "a.b". Just
7284 look at the underlying operand. */
7285 expr = TREE_OPERAND (expr, 0);
7287 if (TREE_CODE (expr) == OFFSET_REF
7288 || TREE_CODE (expr) == MEMBER_REF
7289 || TREE_CODE (expr) == SCOPE_REF)
7290 /* We're only interested in the field itself. If it is a
7291 BASELINK, we will need to see through it in the next
7292 step. */
7293 expr = TREE_OPERAND (expr, 1);
7295 if (BASELINK_P (expr))
7296 /* See through BASELINK nodes to the underlying function. */
7297 expr = BASELINK_FUNCTIONS (expr);
7299 switch (TREE_CODE (expr))
7301 case FIELD_DECL:
7302 if (DECL_BIT_FIELD_TYPE (expr))
7304 type = DECL_BIT_FIELD_TYPE (expr);
7305 break;
7307 /* Fall through for fields that aren't bitfields. */
7309 case FUNCTION_DECL:
7310 case VAR_DECL:
7311 case CONST_DECL:
7312 case PARM_DECL:
7313 case RESULT_DECL:
7314 case TEMPLATE_PARM_INDEX:
7315 expr = mark_type_use (expr);
7316 type = TREE_TYPE (expr);
7317 break;
7319 case ERROR_MARK:
7320 type = error_mark_node;
7321 break;
7323 case COMPONENT_REF:
7324 case COMPOUND_EXPR:
7325 mark_type_use (expr);
7326 type = is_bitfield_expr_with_lowered_type (expr);
7327 if (!type)
7328 type = TREE_TYPE (TREE_OPERAND (expr, 1));
7329 break;
7331 case BIT_FIELD_REF:
7332 gcc_unreachable ();
7334 case INTEGER_CST:
7335 case PTRMEM_CST:
7336 /* We can get here when the id-expression refers to an
7337 enumerator or non-type template parameter. */
7338 type = TREE_TYPE (expr);
7339 break;
7341 default:
7342 /* Handle instantiated template non-type arguments. */
7343 type = TREE_TYPE (expr);
7344 break;
7347 else
7349 /* Within a lambda-expression:
7351 Every occurrence of decltype((x)) where x is a possibly
7352 parenthesized id-expression that names an entity of
7353 automatic storage duration is treated as if x were
7354 transformed into an access to a corresponding data member
7355 of the closure type that would have been declared if x
7356 were a use of the denoted entity. */
7357 if (outer_automatic_var_p (expr)
7358 && current_function_decl
7359 && LAMBDA_FUNCTION_P (current_function_decl))
7360 type = capture_decltype (expr);
7361 else if (error_operand_p (expr))
7362 type = error_mark_node;
7363 else if (expr == current_class_ptr)
7364 /* If the expression is just "this", we want the
7365 cv-unqualified pointer for the "this" type. */
7366 type = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
7367 else
7369 /* Otherwise, where T is the type of e, if e is an lvalue,
7370 decltype(e) is defined as T&; if an xvalue, T&&; otherwise, T. */
7371 cp_lvalue_kind clk = lvalue_kind (expr);
7372 type = unlowered_expr_type (expr);
7373 gcc_assert (TREE_CODE (type) != REFERENCE_TYPE);
7375 /* For vector types, pick a non-opaque variant. */
7376 if (VECTOR_TYPE_P (type))
7377 type = strip_typedefs (type);
7379 if (clk != clk_none && !(clk & clk_class))
7380 type = cp_build_reference_type (type, (clk & clk_rvalueref));
7384 return type;
7387 /* Called from trait_expr_value to evaluate either __has_nothrow_assign or
7388 __has_nothrow_copy, depending on assign_p. */
7390 static bool
7391 classtype_has_nothrow_assign_or_copy_p (tree type, bool assign_p)
7393 tree fns;
7395 if (assign_p)
7397 int ix;
7398 ix = lookup_fnfields_1 (type, ansi_assopname (NOP_EXPR));
7399 if (ix < 0)
7400 return false;
7401 fns = (*CLASSTYPE_METHOD_VEC (type))[ix];
7403 else if (TYPE_HAS_COPY_CTOR (type))
7405 /* If construction of the copy constructor was postponed, create
7406 it now. */
7407 if (CLASSTYPE_LAZY_COPY_CTOR (type))
7408 lazily_declare_fn (sfk_copy_constructor, type);
7409 if (CLASSTYPE_LAZY_MOVE_CTOR (type))
7410 lazily_declare_fn (sfk_move_constructor, type);
7411 fns = CLASSTYPE_CONSTRUCTORS (type);
7413 else
7414 return false;
7416 for (; fns; fns = OVL_NEXT (fns))
7418 tree fn = OVL_CURRENT (fns);
7420 if (assign_p)
7422 if (copy_fn_p (fn) == 0)
7423 continue;
7425 else if (copy_fn_p (fn) <= 0)
7426 continue;
7428 maybe_instantiate_noexcept (fn);
7429 if (!TYPE_NOTHROW_P (TREE_TYPE (fn)))
7430 return false;
7433 return true;
7436 /* Actually evaluates the trait. */
7438 static bool
7439 trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
7441 enum tree_code type_code1;
7442 tree t;
7444 type_code1 = TREE_CODE (type1);
7446 switch (kind)
7448 case CPTK_HAS_NOTHROW_ASSIGN:
7449 type1 = strip_array_types (type1);
7450 return (!CP_TYPE_CONST_P (type1) && type_code1 != REFERENCE_TYPE
7451 && (trait_expr_value (CPTK_HAS_TRIVIAL_ASSIGN, type1, type2)
7452 || (CLASS_TYPE_P (type1)
7453 && classtype_has_nothrow_assign_or_copy_p (type1,
7454 true))));
7456 case CPTK_HAS_TRIVIAL_ASSIGN:
7457 /* ??? The standard seems to be missing the "or array of such a class
7458 type" wording for this trait. */
7459 type1 = strip_array_types (type1);
7460 return (!CP_TYPE_CONST_P (type1) && type_code1 != REFERENCE_TYPE
7461 && (trivial_type_p (type1)
7462 || (CLASS_TYPE_P (type1)
7463 && TYPE_HAS_TRIVIAL_COPY_ASSIGN (type1))));
7465 case CPTK_HAS_NOTHROW_CONSTRUCTOR:
7466 type1 = strip_array_types (type1);
7467 return (trait_expr_value (CPTK_HAS_TRIVIAL_CONSTRUCTOR, type1, type2)
7468 || (CLASS_TYPE_P (type1)
7469 && (t = locate_ctor (type1))
7470 && (maybe_instantiate_noexcept (t),
7471 TYPE_NOTHROW_P (TREE_TYPE (t)))));
7473 case CPTK_HAS_TRIVIAL_CONSTRUCTOR:
7474 type1 = strip_array_types (type1);
7475 return (trivial_type_p (type1)
7476 || (CLASS_TYPE_P (type1) && TYPE_HAS_TRIVIAL_DFLT (type1)));
7478 case CPTK_HAS_NOTHROW_COPY:
7479 type1 = strip_array_types (type1);
7480 return (trait_expr_value (CPTK_HAS_TRIVIAL_COPY, type1, type2)
7481 || (CLASS_TYPE_P (type1)
7482 && classtype_has_nothrow_assign_or_copy_p (type1, false)));
7484 case CPTK_HAS_TRIVIAL_COPY:
7485 /* ??? The standard seems to be missing the "or array of such a class
7486 type" wording for this trait. */
7487 type1 = strip_array_types (type1);
7488 return (trivial_type_p (type1) || type_code1 == REFERENCE_TYPE
7489 || (CLASS_TYPE_P (type1) && TYPE_HAS_TRIVIAL_COPY_CTOR (type1)));
7491 case CPTK_HAS_TRIVIAL_DESTRUCTOR:
7492 type1 = strip_array_types (type1);
7493 return (trivial_type_p (type1) || type_code1 == REFERENCE_TYPE
7494 || (CLASS_TYPE_P (type1)
7495 && TYPE_HAS_TRIVIAL_DESTRUCTOR (type1)));
7497 case CPTK_HAS_VIRTUAL_DESTRUCTOR:
7498 return type_has_virtual_destructor (type1);
7500 case CPTK_IS_ABSTRACT:
7501 return (ABSTRACT_CLASS_TYPE_P (type1));
7503 case CPTK_IS_BASE_OF:
7504 return (NON_UNION_CLASS_TYPE_P (type1) && NON_UNION_CLASS_TYPE_P (type2)
7505 && (same_type_ignoring_top_level_qualifiers_p (type1, type2)
7506 || DERIVED_FROM_P (type1, type2)));
7508 case CPTK_IS_CLASS:
7509 return (NON_UNION_CLASS_TYPE_P (type1));
7511 case CPTK_IS_EMPTY:
7512 return (NON_UNION_CLASS_TYPE_P (type1) && CLASSTYPE_EMPTY_P (type1));
7514 case CPTK_IS_ENUM:
7515 return (type_code1 == ENUMERAL_TYPE);
7517 case CPTK_IS_FINAL:
7518 return (CLASS_TYPE_P (type1) && CLASSTYPE_FINAL (type1));
7520 case CPTK_IS_LITERAL_TYPE:
7521 return (literal_type_p (type1));
7523 case CPTK_IS_POD:
7524 return (pod_type_p (type1));
7526 case CPTK_IS_POLYMORPHIC:
7527 return (CLASS_TYPE_P (type1) && TYPE_POLYMORPHIC_P (type1));
7529 case CPTK_IS_SAME_AS:
7530 return same_type_p (type1, type2);
7532 case CPTK_IS_STD_LAYOUT:
7533 return (std_layout_type_p (type1));
7535 case CPTK_IS_TRIVIAL:
7536 return (trivial_type_p (type1));
7538 case CPTK_IS_TRIVIALLY_ASSIGNABLE:
7539 return is_trivially_xible (MODIFY_EXPR, type1, type2);
7541 case CPTK_IS_TRIVIALLY_CONSTRUCTIBLE:
7542 return is_trivially_xible (INIT_EXPR, type1, type2);
7544 case CPTK_IS_TRIVIALLY_COPYABLE:
7545 return (trivially_copyable_p (type1));
7547 case CPTK_IS_UNION:
7548 return (type_code1 == UNION_TYPE);
7550 default:
7551 gcc_unreachable ();
7552 return false;
7556 /* If TYPE is an array of unknown bound, or (possibly cv-qualified)
7557 void, or a complete type, returns true, otherwise false. */
7559 static bool
7560 check_trait_type (tree type)
7562 if (type == NULL_TREE)
7563 return true;
7565 if (TREE_CODE (type) == TREE_LIST)
7566 return (check_trait_type (TREE_VALUE (type))
7567 && check_trait_type (TREE_CHAIN (type)));
7569 if (TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type)
7570 && COMPLETE_TYPE_P (TREE_TYPE (type)))
7571 return true;
7573 if (VOID_TYPE_P (type))
7574 return true;
7576 return !!complete_type_or_else (strip_array_types (type), NULL_TREE);
7579 /* Process a trait expression. */
7581 tree
7582 finish_trait_expr (cp_trait_kind kind, tree type1, tree type2)
7584 if (type1 == error_mark_node
7585 || type2 == error_mark_node)
7586 return error_mark_node;
7588 if (processing_template_decl)
7590 tree trait_expr = make_node (TRAIT_EXPR);
7591 TREE_TYPE (trait_expr) = boolean_type_node;
7592 TRAIT_EXPR_TYPE1 (trait_expr) = type1;
7593 TRAIT_EXPR_TYPE2 (trait_expr) = type2;
7594 TRAIT_EXPR_KIND (trait_expr) = kind;
7595 return trait_expr;
7598 switch (kind)
7600 case CPTK_HAS_NOTHROW_ASSIGN:
7601 case CPTK_HAS_TRIVIAL_ASSIGN:
7602 case CPTK_HAS_NOTHROW_CONSTRUCTOR:
7603 case CPTK_HAS_TRIVIAL_CONSTRUCTOR:
7604 case CPTK_HAS_NOTHROW_COPY:
7605 case CPTK_HAS_TRIVIAL_COPY:
7606 case CPTK_HAS_TRIVIAL_DESTRUCTOR:
7607 case CPTK_HAS_VIRTUAL_DESTRUCTOR:
7608 case CPTK_IS_ABSTRACT:
7609 case CPTK_IS_EMPTY:
7610 case CPTK_IS_FINAL:
7611 case CPTK_IS_LITERAL_TYPE:
7612 case CPTK_IS_POD:
7613 case CPTK_IS_POLYMORPHIC:
7614 case CPTK_IS_STD_LAYOUT:
7615 case CPTK_IS_TRIVIAL:
7616 case CPTK_IS_TRIVIALLY_COPYABLE:
7617 if (!check_trait_type (type1))
7618 return error_mark_node;
7619 break;
7621 case CPTK_IS_TRIVIALLY_ASSIGNABLE:
7622 case CPTK_IS_TRIVIALLY_CONSTRUCTIBLE:
7623 if (!check_trait_type (type1)
7624 || !check_trait_type (type2))
7625 return error_mark_node;
7626 break;
7628 case CPTK_IS_BASE_OF:
7629 if (NON_UNION_CLASS_TYPE_P (type1) && NON_UNION_CLASS_TYPE_P (type2)
7630 && !same_type_ignoring_top_level_qualifiers_p (type1, type2)
7631 && !complete_type_or_else (type2, NULL_TREE))
7632 /* We already issued an error. */
7633 return error_mark_node;
7634 break;
7636 case CPTK_IS_CLASS:
7637 case CPTK_IS_ENUM:
7638 case CPTK_IS_UNION:
7639 case CPTK_IS_SAME_AS:
7640 break;
7642 default:
7643 gcc_unreachable ();
7646 return (trait_expr_value (kind, type1, type2)
7647 ? boolean_true_node : boolean_false_node);
7650 /* Do-nothing variants of functions to handle pragma FLOAT_CONST_DECIMAL64,
7651 which is ignored for C++. */
7653 void
7654 set_float_const_decimal64 (void)
7658 void
7659 clear_float_const_decimal64 (void)
7663 bool
7664 float_const_decimal64_p (void)
7666 return 0;
7670 /* Return true if T designates the implied `this' parameter. */
7672 bool
7673 is_this_parameter (tree t)
7675 if (!DECL_P (t) || DECL_NAME (t) != this_identifier)
7676 return false;
7677 gcc_assert (TREE_CODE (t) == PARM_DECL || is_capture_proxy (t));
7678 return true;
7681 /* Insert the deduced return type for an auto function. */
7683 void
7684 apply_deduced_return_type (tree fco, tree return_type)
7686 tree result;
7688 if (return_type == error_mark_node)
7689 return;
7691 if (LAMBDA_FUNCTION_P (fco))
7693 tree lambda = CLASSTYPE_LAMBDA_EXPR (current_class_type);
7694 LAMBDA_EXPR_RETURN_TYPE (lambda) = return_type;
7697 if (DECL_CONV_FN_P (fco))
7698 DECL_NAME (fco) = mangle_conv_op_name_for_type (return_type);
7700 TREE_TYPE (fco) = change_return_type (return_type, TREE_TYPE (fco));
7702 result = DECL_RESULT (fco);
7703 if (result == NULL_TREE)
7704 return;
7705 if (TREE_TYPE (result) == return_type)
7706 return;
7708 /* We already have a DECL_RESULT from start_preparsed_function.
7709 Now we need to redo the work it and allocate_struct_function
7710 did to reflect the new type. */
7711 gcc_assert (current_function_decl == fco);
7712 result = build_decl (input_location, RESULT_DECL, NULL_TREE,
7713 TYPE_MAIN_VARIANT (return_type));
7714 DECL_ARTIFICIAL (result) = 1;
7715 DECL_IGNORED_P (result) = 1;
7716 cp_apply_type_quals_to_decl (cp_type_quals (return_type),
7717 result);
7719 DECL_RESULT (fco) = result;
7721 if (!processing_template_decl)
7723 if (!VOID_TYPE_P (TREE_TYPE (result)))
7724 complete_type_or_else (TREE_TYPE (result), NULL_TREE);
7725 bool aggr = aggregate_value_p (result, fco);
7726 #ifdef PCC_STATIC_STRUCT_RETURN
7727 cfun->returns_pcc_struct = aggr;
7728 #endif
7729 cfun->returns_struct = aggr;
7734 /* DECL is a local variable or parameter from the surrounding scope of a
7735 lambda-expression. Returns the decltype for a use of the capture field
7736 for DECL even if it hasn't been captured yet. */
7738 static tree
7739 capture_decltype (tree decl)
7741 tree lam = CLASSTYPE_LAMBDA_EXPR (DECL_CONTEXT (current_function_decl));
7742 /* FIXME do lookup instead of list walk? */
7743 tree cap = value_member (decl, LAMBDA_EXPR_CAPTURE_LIST (lam));
7744 tree type;
7746 if (cap)
7747 type = TREE_TYPE (TREE_PURPOSE (cap));
7748 else
7749 switch (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lam))
7751 case CPLD_NONE:
7752 error ("%qD is not captured", decl);
7753 return error_mark_node;
7755 case CPLD_COPY:
7756 type = TREE_TYPE (decl);
7757 if (TREE_CODE (type) == REFERENCE_TYPE
7758 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
7759 type = TREE_TYPE (type);
7760 break;
7762 case CPLD_REFERENCE:
7763 type = TREE_TYPE (decl);
7764 if (TREE_CODE (type) != REFERENCE_TYPE)
7765 type = build_reference_type (TREE_TYPE (decl));
7766 break;
7768 default:
7769 gcc_unreachable ();
7772 if (TREE_CODE (type) != REFERENCE_TYPE)
7774 if (!LAMBDA_EXPR_MUTABLE_P (lam))
7775 type = cp_build_qualified_type (type, (cp_type_quals (type)
7776 |TYPE_QUAL_CONST));
7777 type = build_reference_type (type);
7779 return type;
7782 #include "gt-cp-semantics.h"