Merge from trunk.
[official-gcc.git] / gcc / cp / semantics.c
blob589451876c92e55c071ef6ddac2677e2e1a29ef2
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-2014 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 "tree.h"
31 #include "stmt.h"
32 #include "varasm.h"
33 #include "stor-layout.h"
34 #include "stringpool.h"
35 #include "cp-tree.h"
36 #include "c-family/c-common.h"
37 #include "c-family/c-objc.h"
38 #include "tree-inline.h"
39 #include "intl.h"
40 #include "toplev.h"
41 #include "flags.h"
42 #include "timevar.h"
43 #include "diagnostic.h"
44 #include "cgraph.h"
45 #include "tree-iterator.h"
46 #include "target.h"
47 #include "pointer-set.h"
48 #include "hash-table.h"
49 #include "gimplify.h"
50 #include "bitmap.h"
51 #include "omp-low.h"
52 #include "builtins.h"
54 static bool verify_constant (tree, bool, bool *, bool *);
55 #define VERIFY_CONSTANT(X) \
56 do { \
57 if (verify_constant ((X), allow_non_constant, non_constant_p, overflow_p)) \
58 return t; \
59 } while (0)
61 /* There routines provide a modular interface to perform many parsing
62 operations. They may therefore be used during actual parsing, or
63 during template instantiation, which may be regarded as a
64 degenerate form of parsing. */
66 static tree maybe_convert_cond (tree);
67 static tree finalize_nrv_r (tree *, int *, void *);
68 static tree capture_decltype (tree);
71 /* Deferred Access Checking Overview
72 ---------------------------------
74 Most C++ expressions and declarations require access checking
75 to be performed during parsing. However, in several cases,
76 this has to be treated differently.
78 For member declarations, access checking has to be deferred
79 until more information about the declaration is known. For
80 example:
82 class A {
83 typedef int X;
84 public:
85 X f();
88 A::X A::f();
89 A::X g();
91 When we are parsing the function return type `A::X', we don't
92 really know if this is allowed until we parse the function name.
94 Furthermore, some contexts require that access checking is
95 never performed at all. These include class heads, and template
96 instantiations.
98 Typical use of access checking functions is described here:
100 1. When we enter a context that requires certain access checking
101 mode, the function `push_deferring_access_checks' is called with
102 DEFERRING argument specifying the desired mode. Access checking
103 may be performed immediately (dk_no_deferred), deferred
104 (dk_deferred), or not performed (dk_no_check).
106 2. When a declaration such as a type, or a variable, is encountered,
107 the function `perform_or_defer_access_check' is called. It
108 maintains a vector of all deferred checks.
110 3. The global `current_class_type' or `current_function_decl' is then
111 setup by the parser. `enforce_access' relies on these information
112 to check access.
114 4. Upon exiting the context mentioned in step 1,
115 `perform_deferred_access_checks' is called to check all declaration
116 stored in the vector. `pop_deferring_access_checks' is then
117 called to restore the previous access checking mode.
119 In case of parsing error, we simply call `pop_deferring_access_checks'
120 without `perform_deferred_access_checks'. */
122 typedef struct GTY(()) deferred_access {
123 /* A vector representing name-lookups for which we have deferred
124 checking access controls. We cannot check the accessibility of
125 names used in a decl-specifier-seq until we know what is being
126 declared because code like:
128 class A {
129 class B {};
130 B* f();
133 A::B* A::f() { return 0; }
135 is valid, even though `A::B' is not generally accessible. */
136 vec<deferred_access_check, va_gc> * GTY(()) deferred_access_checks;
138 /* The current mode of access checks. */
139 enum deferring_kind deferring_access_checks_kind;
141 } deferred_access;
143 /* Data for deferred access checking. */
144 static GTY(()) vec<deferred_access, va_gc> *deferred_access_stack;
145 static GTY(()) unsigned deferred_access_no_check;
147 /* Save the current deferred access states and start deferred
148 access checking iff DEFER_P is true. */
150 void
151 push_deferring_access_checks (deferring_kind deferring)
153 /* For context like template instantiation, access checking
154 disabling applies to all nested context. */
155 if (deferred_access_no_check || deferring == dk_no_check)
156 deferred_access_no_check++;
157 else
159 deferred_access e = {NULL, deferring};
160 vec_safe_push (deferred_access_stack, e);
164 /* Save the current deferred access states and start deferred access
165 checking, continuing the set of deferred checks in CHECKS. */
167 void
168 reopen_deferring_access_checks (vec<deferred_access_check, va_gc> * checks)
170 push_deferring_access_checks (dk_deferred);
171 if (!deferred_access_no_check)
172 deferred_access_stack->last().deferred_access_checks = checks;
175 /* Resume deferring access checks again after we stopped doing
176 this previously. */
178 void
179 resume_deferring_access_checks (void)
181 if (!deferred_access_no_check)
182 deferred_access_stack->last().deferring_access_checks_kind = dk_deferred;
185 /* Stop deferring access checks. */
187 void
188 stop_deferring_access_checks (void)
190 if (!deferred_access_no_check)
191 deferred_access_stack->last().deferring_access_checks_kind = dk_no_deferred;
194 /* Discard the current deferred access checks and restore the
195 previous states. */
197 void
198 pop_deferring_access_checks (void)
200 if (deferred_access_no_check)
201 deferred_access_no_check--;
202 else
203 deferred_access_stack->pop ();
206 /* Returns a TREE_LIST representing the deferred checks.
207 The TREE_PURPOSE of each node is the type through which the
208 access occurred; the TREE_VALUE is the declaration named.
211 vec<deferred_access_check, va_gc> *
212 get_deferred_access_checks (void)
214 if (deferred_access_no_check)
215 return NULL;
216 else
217 return (deferred_access_stack->last().deferred_access_checks);
220 /* Take current deferred checks and combine with the
221 previous states if we also defer checks previously.
222 Otherwise perform checks now. */
224 void
225 pop_to_parent_deferring_access_checks (void)
227 if (deferred_access_no_check)
228 deferred_access_no_check--;
229 else
231 vec<deferred_access_check, va_gc> *checks;
232 deferred_access *ptr;
234 checks = (deferred_access_stack->last ().deferred_access_checks);
236 deferred_access_stack->pop ();
237 ptr = &deferred_access_stack->last ();
238 if (ptr->deferring_access_checks_kind == dk_no_deferred)
240 /* Check access. */
241 perform_access_checks (checks, tf_warning_or_error);
243 else
245 /* Merge with parent. */
246 int i, j;
247 deferred_access_check *chk, *probe;
249 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
251 FOR_EACH_VEC_SAFE_ELT (ptr->deferred_access_checks, j, probe)
253 if (probe->binfo == chk->binfo &&
254 probe->decl == chk->decl &&
255 probe->diag_decl == chk->diag_decl)
256 goto found;
258 /* Insert into parent's checks. */
259 vec_safe_push (ptr->deferred_access_checks, *chk);
260 found:;
266 /* Perform the access checks in CHECKS. The TREE_PURPOSE of each node
267 is the BINFO indicating the qualifying scope used to access the
268 DECL node stored in the TREE_VALUE of the node. If CHECKS is empty
269 or we aren't in SFINAE context or all the checks succeed return TRUE,
270 otherwise FALSE. */
272 bool
273 perform_access_checks (vec<deferred_access_check, va_gc> *checks,
274 tsubst_flags_t complain)
276 int i;
277 deferred_access_check *chk;
278 location_t loc = input_location;
279 bool ok = true;
281 if (!checks)
282 return true;
284 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
286 input_location = chk->loc;
287 ok &= enforce_access (chk->binfo, chk->decl, chk->diag_decl, complain);
290 input_location = loc;
291 return (complain & tf_error) ? true : ok;
294 /* Perform the deferred access checks.
296 After performing the checks, we still have to keep the list
297 `deferred_access_stack->deferred_access_checks' since we may want
298 to check access for them again later in a different context.
299 For example:
301 class A {
302 typedef int X;
303 static X a;
305 A::X A::a, x; // No error for `A::a', error for `x'
307 We have to perform deferred access of `A::X', first with `A::a',
308 next with `x'. Return value like perform_access_checks above. */
310 bool
311 perform_deferred_access_checks (tsubst_flags_t complain)
313 return perform_access_checks (get_deferred_access_checks (), complain);
316 /* Defer checking the accessibility of DECL, when looked up in
317 BINFO. DIAG_DECL is the declaration to use to print diagnostics.
318 Return value like perform_access_checks above. */
320 bool
321 perform_or_defer_access_check (tree binfo, tree decl, tree diag_decl,
322 tsubst_flags_t complain)
324 int i;
325 deferred_access *ptr;
326 deferred_access_check *chk;
329 /* Exit if we are in a context that no access checking is performed.
331 if (deferred_access_no_check)
332 return true;
334 gcc_assert (TREE_CODE (binfo) == TREE_BINFO);
336 ptr = &deferred_access_stack->last ();
338 /* If we are not supposed to defer access checks, just check now. */
339 if (ptr->deferring_access_checks_kind == dk_no_deferred)
341 bool ok = enforce_access (binfo, decl, diag_decl, complain);
342 return (complain & tf_error) ? true : ok;
345 /* See if we are already going to perform this check. */
346 FOR_EACH_VEC_SAFE_ELT (ptr->deferred_access_checks, i, chk)
348 if (chk->decl == decl && chk->binfo == binfo &&
349 chk->diag_decl == diag_decl)
351 return true;
354 /* If not, record the check. */
355 deferred_access_check new_access = {binfo, decl, diag_decl, input_location};
356 vec_safe_push (ptr->deferred_access_checks, new_access);
358 return true;
361 /* Returns nonzero if the current statement is a full expression,
362 i.e. temporaries created during that statement should be destroyed
363 at the end of the statement. */
366 stmts_are_full_exprs_p (void)
368 return current_stmt_tree ()->stmts_are_full_exprs_p;
371 /* T is a statement. Add it to the statement-tree. This is the C++
372 version. The C/ObjC frontends have a slightly different version of
373 this function. */
375 tree
376 add_stmt (tree t)
378 enum tree_code code = TREE_CODE (t);
380 if (EXPR_P (t) && code != LABEL_EXPR)
382 if (!EXPR_HAS_LOCATION (t))
383 SET_EXPR_LOCATION (t, input_location);
385 /* When we expand a statement-tree, we must know whether or not the
386 statements are full-expressions. We record that fact here. */
387 STMT_IS_FULL_EXPR_P (t) = stmts_are_full_exprs_p ();
390 if (code == LABEL_EXPR || code == CASE_LABEL_EXPR)
391 STATEMENT_LIST_HAS_LABEL (cur_stmt_list) = 1;
393 /* Add T to the statement-tree. Non-side-effect statements need to be
394 recorded during statement expressions. */
395 gcc_checking_assert (!stmt_list_stack->is_empty ());
396 append_to_statement_list_force (t, &cur_stmt_list);
398 return t;
401 /* Returns the stmt_tree to which statements are currently being added. */
403 stmt_tree
404 current_stmt_tree (void)
406 return (cfun
407 ? &cfun->language->base.x_stmt_tree
408 : &scope_chain->x_stmt_tree);
411 /* If statements are full expressions, wrap STMT in a CLEANUP_POINT_EXPR. */
413 static tree
414 maybe_cleanup_point_expr (tree expr)
416 if (!processing_template_decl && stmts_are_full_exprs_p ())
417 expr = fold_build_cleanup_point_expr (TREE_TYPE (expr), expr);
418 return expr;
421 /* Like maybe_cleanup_point_expr except have the type of the new expression be
422 void so we don't need to create a temporary variable to hold the inner
423 expression. The reason why we do this is because the original type might be
424 an aggregate and we cannot create a temporary variable for that type. */
426 tree
427 maybe_cleanup_point_expr_void (tree expr)
429 if (!processing_template_decl && stmts_are_full_exprs_p ())
430 expr = fold_build_cleanup_point_expr (void_type_node, expr);
431 return expr;
436 /* Create a declaration statement for the declaration given by the DECL. */
438 void
439 add_decl_expr (tree decl)
441 tree r = build_stmt (input_location, DECL_EXPR, decl);
442 if (DECL_INITIAL (decl)
443 || (DECL_SIZE (decl) && TREE_SIDE_EFFECTS (DECL_SIZE (decl))))
444 r = maybe_cleanup_point_expr_void (r);
445 add_stmt (r);
448 /* Finish a scope. */
450 tree
451 do_poplevel (tree stmt_list)
453 tree block = NULL;
455 if (stmts_are_full_exprs_p ())
456 block = poplevel (kept_level_p (), 1, 0);
458 stmt_list = pop_stmt_list (stmt_list);
460 if (!processing_template_decl)
462 stmt_list = c_build_bind_expr (input_location, block, stmt_list);
463 /* ??? See c_end_compound_stmt re statement expressions. */
466 return stmt_list;
469 /* Begin a new scope. */
471 static tree
472 do_pushlevel (scope_kind sk)
474 tree ret = push_stmt_list ();
475 if (stmts_are_full_exprs_p ())
476 begin_scope (sk, NULL);
477 return ret;
480 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
481 when the current scope is exited. EH_ONLY is true when this is not
482 meant to apply to normal control flow transfer. */
484 void
485 push_cleanup (tree decl, tree cleanup, bool eh_only)
487 tree stmt = build_stmt (input_location, CLEANUP_STMT, NULL, cleanup, decl);
488 CLEANUP_EH_ONLY (stmt) = eh_only;
489 add_stmt (stmt);
490 CLEANUP_BODY (stmt) = push_stmt_list ();
493 /* Simple infinite loop tracking for -Wreturn-type. We keep a stack of all
494 the current loops, represented by 'NULL_TREE' if we've seen a possible
495 exit, and 'error_mark_node' if not. This is currently used only to
496 suppress the warning about a function with no return statements, and
497 therefore we don't bother noting returns as possible exits. We also
498 don't bother with gotos. */
500 static void
501 begin_maybe_infinite_loop (tree cond)
503 /* Only track this while parsing a function, not during instantiation. */
504 if (!cfun || (DECL_TEMPLATE_INSTANTIATION (current_function_decl)
505 && !processing_template_decl))
506 return;
507 bool maybe_infinite = true;
508 if (cond)
510 cond = fold_non_dependent_expr_sfinae (cond, tf_none);
511 cond = maybe_constant_value (cond);
512 maybe_infinite = integer_nonzerop (cond);
514 vec_safe_push (cp_function_chain->infinite_loops,
515 maybe_infinite ? error_mark_node : NULL_TREE);
519 /* A break is a possible exit for the current loop. */
521 void
522 break_maybe_infinite_loop (void)
524 if (!cfun)
525 return;
526 cp_function_chain->infinite_loops->last() = NULL_TREE;
529 /* If we reach the end of the loop without seeing a possible exit, we have
530 an infinite loop. */
532 static void
533 end_maybe_infinite_loop (tree cond)
535 if (!cfun || (DECL_TEMPLATE_INSTANTIATION (current_function_decl)
536 && !processing_template_decl))
537 return;
538 tree current = cp_function_chain->infinite_loops->pop();
539 if (current != NULL_TREE)
541 cond = fold_non_dependent_expr (cond);
542 cond = maybe_constant_value (cond);
543 if (integer_nonzerop (cond))
544 current_function_infinite_loop = 1;
549 /* Begin a conditional that might contain a declaration. When generating
550 normal code, we want the declaration to appear before the statement
551 containing the conditional. When generating template code, we want the
552 conditional to be rendered as the raw DECL_EXPR. */
554 static void
555 begin_cond (tree *cond_p)
557 if (processing_template_decl)
558 *cond_p = push_stmt_list ();
561 /* Finish such a conditional. */
563 static void
564 finish_cond (tree *cond_p, tree expr)
566 if (processing_template_decl)
568 tree cond = pop_stmt_list (*cond_p);
570 if (expr == NULL_TREE)
571 /* Empty condition in 'for'. */
572 gcc_assert (empty_expr_stmt_p (cond));
573 else if (check_for_bare_parameter_packs (expr))
574 expr = error_mark_node;
575 else if (!empty_expr_stmt_p (cond))
576 expr = build2 (COMPOUND_EXPR, TREE_TYPE (expr), cond, expr);
578 *cond_p = expr;
581 /* If *COND_P specifies a conditional with a declaration, transform the
582 loop such that
583 while (A x = 42) { }
584 for (; A x = 42;) { }
585 becomes
586 while (true) { A x = 42; if (!x) break; }
587 for (;;) { A x = 42; if (!x) break; }
588 The statement list for BODY will be empty if the conditional did
589 not declare anything. */
591 static void
592 simplify_loop_decl_cond (tree *cond_p, tree body)
594 tree cond, if_stmt;
596 if (!TREE_SIDE_EFFECTS (body))
597 return;
599 cond = *cond_p;
600 *cond_p = boolean_true_node;
602 if_stmt = begin_if_stmt ();
603 cond = cp_build_unary_op (TRUTH_NOT_EXPR, cond, 0, tf_warning_or_error);
604 finish_if_stmt_cond (cond, if_stmt);
605 finish_break_stmt ();
606 finish_then_clause (if_stmt);
607 finish_if_stmt (if_stmt);
610 /* Finish a goto-statement. */
612 tree
613 finish_goto_stmt (tree destination)
615 if (identifier_p (destination))
616 destination = lookup_label (destination);
618 /* We warn about unused labels with -Wunused. That means we have to
619 mark the used labels as used. */
620 if (TREE_CODE (destination) == LABEL_DECL)
621 TREE_USED (destination) = 1;
622 else
624 destination = mark_rvalue_use (destination);
625 if (!processing_template_decl)
627 destination = cp_convert (ptr_type_node, destination,
628 tf_warning_or_error);
629 if (error_operand_p (destination))
630 return NULL_TREE;
631 destination
632 = fold_build_cleanup_point_expr (TREE_TYPE (destination),
633 destination);
637 check_goto (destination);
639 return add_stmt (build_stmt (input_location, GOTO_EXPR, destination));
642 /* COND is the condition-expression for an if, while, etc.,
643 statement. Convert it to a boolean value, if appropriate.
644 In addition, verify sequence points if -Wsequence-point is enabled. */
646 static tree
647 maybe_convert_cond (tree cond)
649 /* Empty conditions remain empty. */
650 if (!cond)
651 return NULL_TREE;
653 /* Wait until we instantiate templates before doing conversion. */
654 if (processing_template_decl)
655 return cond;
657 if (warn_sequence_point)
658 verify_sequence_points (cond);
660 /* Do the conversion. */
661 cond = convert_from_reference (cond);
663 if (TREE_CODE (cond) == MODIFY_EXPR
664 && !TREE_NO_WARNING (cond)
665 && warn_parentheses)
667 warning (OPT_Wparentheses,
668 "suggest parentheses around assignment used as truth value");
669 TREE_NO_WARNING (cond) = 1;
672 return condition_conversion (cond);
675 /* Finish an expression-statement, whose EXPRESSION is as indicated. */
677 tree
678 finish_expr_stmt (tree expr)
680 tree r = NULL_TREE;
682 if (expr != NULL_TREE)
684 if (!processing_template_decl)
686 if (warn_sequence_point)
687 verify_sequence_points (expr);
688 expr = convert_to_void (expr, ICV_STATEMENT, tf_warning_or_error);
690 else if (!type_dependent_expression_p (expr))
691 convert_to_void (build_non_dependent_expr (expr), ICV_STATEMENT,
692 tf_warning_or_error);
694 if (check_for_bare_parameter_packs (expr))
695 expr = error_mark_node;
697 /* Simplification of inner statement expressions, compound exprs,
698 etc can result in us already having an EXPR_STMT. */
699 if (TREE_CODE (expr) != CLEANUP_POINT_EXPR)
701 if (TREE_CODE (expr) != EXPR_STMT)
702 expr = build_stmt (input_location, EXPR_STMT, expr);
703 expr = maybe_cleanup_point_expr_void (expr);
706 r = add_stmt (expr);
709 return r;
713 /* Begin an if-statement. Returns a newly created IF_STMT if
714 appropriate. */
716 tree
717 begin_if_stmt (void)
719 tree r, scope;
720 scope = do_pushlevel (sk_cond);
721 r = build_stmt (input_location, IF_STMT, NULL_TREE,
722 NULL_TREE, NULL_TREE, scope);
723 begin_cond (&IF_COND (r));
724 return r;
727 /* Process the COND of an if-statement, which may be given by
728 IF_STMT. */
730 void
731 finish_if_stmt_cond (tree cond, tree if_stmt)
733 finish_cond (&IF_COND (if_stmt), maybe_convert_cond (cond));
734 add_stmt (if_stmt);
735 THEN_CLAUSE (if_stmt) = push_stmt_list ();
738 /* Finish the then-clause of an if-statement, which may be given by
739 IF_STMT. */
741 tree
742 finish_then_clause (tree if_stmt)
744 THEN_CLAUSE (if_stmt) = pop_stmt_list (THEN_CLAUSE (if_stmt));
745 return if_stmt;
748 /* Begin the else-clause of an if-statement. */
750 void
751 begin_else_clause (tree if_stmt)
753 ELSE_CLAUSE (if_stmt) = push_stmt_list ();
756 /* Finish the else-clause of an if-statement, which may be given by
757 IF_STMT. */
759 void
760 finish_else_clause (tree if_stmt)
762 ELSE_CLAUSE (if_stmt) = pop_stmt_list (ELSE_CLAUSE (if_stmt));
765 /* Finish an if-statement. */
767 void
768 finish_if_stmt (tree if_stmt)
770 tree scope = IF_SCOPE (if_stmt);
771 IF_SCOPE (if_stmt) = NULL;
772 add_stmt (do_poplevel (scope));
775 /* Begin a while-statement. Returns a newly created WHILE_STMT if
776 appropriate. */
778 tree
779 begin_while_stmt (void)
781 tree r;
782 r = build_stmt (input_location, WHILE_STMT, NULL_TREE, NULL_TREE);
783 add_stmt (r);
784 WHILE_BODY (r) = do_pushlevel (sk_block);
785 begin_cond (&WHILE_COND (r));
786 return r;
789 /* Process the COND of a while-statement, which may be given by
790 WHILE_STMT. */
792 void
793 finish_while_stmt_cond (tree cond, tree while_stmt, bool ivdep)
795 cond = maybe_convert_cond (cond);
796 finish_cond (&WHILE_COND (while_stmt), cond);
797 begin_maybe_infinite_loop (cond);
798 if (ivdep && cond != error_mark_node)
799 WHILE_COND (while_stmt) = build2 (ANNOTATE_EXPR,
800 TREE_TYPE (WHILE_COND (while_stmt)),
801 WHILE_COND (while_stmt),
802 build_int_cst (integer_type_node,
803 annot_expr_ivdep_kind));
804 simplify_loop_decl_cond (&WHILE_COND (while_stmt), WHILE_BODY (while_stmt));
807 /* Finish a while-statement, which may be given by WHILE_STMT. */
809 void
810 finish_while_stmt (tree while_stmt)
812 end_maybe_infinite_loop (boolean_true_node);
813 WHILE_BODY (while_stmt) = do_poplevel (WHILE_BODY (while_stmt));
816 /* Begin a do-statement. Returns a newly created DO_STMT if
817 appropriate. */
819 tree
820 begin_do_stmt (void)
822 tree r = build_stmt (input_location, DO_STMT, NULL_TREE, NULL_TREE);
823 begin_maybe_infinite_loop (boolean_true_node);
824 add_stmt (r);
825 DO_BODY (r) = push_stmt_list ();
826 return r;
829 /* Finish the body of a do-statement, which may be given by DO_STMT. */
831 void
832 finish_do_body (tree do_stmt)
834 tree body = DO_BODY (do_stmt) = pop_stmt_list (DO_BODY (do_stmt));
836 if (TREE_CODE (body) == STATEMENT_LIST && STATEMENT_LIST_TAIL (body))
837 body = STATEMENT_LIST_TAIL (body)->stmt;
839 if (IS_EMPTY_STMT (body))
840 warning (OPT_Wempty_body,
841 "suggest explicit braces around empty body in %<do%> statement");
844 /* Finish a do-statement, which may be given by DO_STMT, and whose
845 COND is as indicated. */
847 void
848 finish_do_stmt (tree cond, tree do_stmt, bool ivdep)
850 cond = maybe_convert_cond (cond);
851 end_maybe_infinite_loop (cond);
852 if (ivdep && cond != error_mark_node)
853 cond = build2 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
854 build_int_cst (integer_type_node, annot_expr_ivdep_kind));
855 DO_COND (do_stmt) = cond;
858 /* Finish a return-statement. The EXPRESSION returned, if any, is as
859 indicated. */
861 tree
862 finish_return_stmt (tree expr)
864 tree r;
865 bool no_warning;
867 expr = check_return_expr (expr, &no_warning);
869 if (error_operand_p (expr)
870 || (flag_openmp && !check_omp_return ()))
871 return error_mark_node;
872 if (!processing_template_decl)
874 if (warn_sequence_point)
875 verify_sequence_points (expr);
877 if (DECL_DESTRUCTOR_P (current_function_decl)
878 || (DECL_CONSTRUCTOR_P (current_function_decl)
879 && targetm.cxx.cdtor_returns_this ()))
881 /* Similarly, all destructors must run destructors for
882 base-classes before returning. So, all returns in a
883 destructor get sent to the DTOR_LABEL; finish_function emits
884 code to return a value there. */
885 return finish_goto_stmt (cdtor_label);
889 r = build_stmt (input_location, RETURN_EXPR, expr);
890 TREE_NO_WARNING (r) |= no_warning;
891 r = maybe_cleanup_point_expr_void (r);
892 r = add_stmt (r);
894 return r;
897 /* Begin the scope of a for-statement or a range-for-statement.
898 Both the returned trees are to be used in a call to
899 begin_for_stmt or begin_range_for_stmt. */
901 tree
902 begin_for_scope (tree *init)
904 tree scope = NULL_TREE;
905 if (flag_new_for_scope > 0)
906 scope = do_pushlevel (sk_for);
908 if (processing_template_decl)
909 *init = push_stmt_list ();
910 else
911 *init = NULL_TREE;
913 return scope;
916 /* Begin a for-statement. Returns a new FOR_STMT.
917 SCOPE and INIT should be the return of begin_for_scope,
918 or both NULL_TREE */
920 tree
921 begin_for_stmt (tree scope, tree init)
923 tree r;
925 r = build_stmt (input_location, FOR_STMT, NULL_TREE, NULL_TREE,
926 NULL_TREE, NULL_TREE, NULL_TREE);
928 if (scope == NULL_TREE)
930 gcc_assert (!init || !(flag_new_for_scope > 0));
931 if (!init)
932 scope = begin_for_scope (&init);
934 FOR_INIT_STMT (r) = init;
935 FOR_SCOPE (r) = scope;
937 return r;
940 /* Finish the for-init-statement of a for-statement, which may be
941 given by FOR_STMT. */
943 void
944 finish_for_init_stmt (tree for_stmt)
946 if (processing_template_decl)
947 FOR_INIT_STMT (for_stmt) = pop_stmt_list (FOR_INIT_STMT (for_stmt));
948 add_stmt (for_stmt);
949 FOR_BODY (for_stmt) = do_pushlevel (sk_block);
950 begin_cond (&FOR_COND (for_stmt));
953 /* Finish the COND of a for-statement, which may be given by
954 FOR_STMT. */
956 void
957 finish_for_cond (tree cond, tree for_stmt, bool ivdep)
959 cond = maybe_convert_cond (cond);
960 finish_cond (&FOR_COND (for_stmt), cond);
961 begin_maybe_infinite_loop (cond);
962 if (ivdep && cond != error_mark_node)
963 FOR_COND (for_stmt) = build2 (ANNOTATE_EXPR,
964 TREE_TYPE (FOR_COND (for_stmt)),
965 FOR_COND (for_stmt),
966 build_int_cst (integer_type_node,
967 annot_expr_ivdep_kind));
968 simplify_loop_decl_cond (&FOR_COND (for_stmt), FOR_BODY (for_stmt));
971 /* Finish the increment-EXPRESSION in a for-statement, which may be
972 given by FOR_STMT. */
974 void
975 finish_for_expr (tree expr, tree for_stmt)
977 if (!expr)
978 return;
979 /* If EXPR is an overloaded function, issue an error; there is no
980 context available to use to perform overload resolution. */
981 if (type_unknown_p (expr))
983 cxx_incomplete_type_error (expr, TREE_TYPE (expr));
984 expr = error_mark_node;
986 if (!processing_template_decl)
988 if (warn_sequence_point)
989 verify_sequence_points (expr);
990 expr = convert_to_void (expr, ICV_THIRD_IN_FOR,
991 tf_warning_or_error);
993 else if (!type_dependent_expression_p (expr))
994 convert_to_void (build_non_dependent_expr (expr), ICV_THIRD_IN_FOR,
995 tf_warning_or_error);
996 expr = maybe_cleanup_point_expr_void (expr);
997 if (check_for_bare_parameter_packs (expr))
998 expr = error_mark_node;
999 FOR_EXPR (for_stmt) = expr;
1002 /* Finish the body of a for-statement, which may be given by
1003 FOR_STMT. The increment-EXPR for the loop must be
1004 provided.
1005 It can also finish RANGE_FOR_STMT. */
1007 void
1008 finish_for_stmt (tree for_stmt)
1010 end_maybe_infinite_loop (boolean_true_node);
1012 if (TREE_CODE (for_stmt) == RANGE_FOR_STMT)
1013 RANGE_FOR_BODY (for_stmt) = do_poplevel (RANGE_FOR_BODY (for_stmt));
1014 else
1015 FOR_BODY (for_stmt) = do_poplevel (FOR_BODY (for_stmt));
1017 /* Pop the scope for the body of the loop. */
1018 if (flag_new_for_scope > 0)
1020 tree scope;
1021 tree *scope_ptr = (TREE_CODE (for_stmt) == RANGE_FOR_STMT
1022 ? &RANGE_FOR_SCOPE (for_stmt)
1023 : &FOR_SCOPE (for_stmt));
1024 scope = *scope_ptr;
1025 *scope_ptr = NULL;
1026 add_stmt (do_poplevel (scope));
1030 /* Begin a range-for-statement. Returns a new RANGE_FOR_STMT.
1031 SCOPE and INIT should be the return of begin_for_scope,
1032 or both NULL_TREE .
1033 To finish it call finish_for_stmt(). */
1035 tree
1036 begin_range_for_stmt (tree scope, tree init)
1038 tree r;
1040 begin_maybe_infinite_loop (boolean_false_node);
1042 r = build_stmt (input_location, RANGE_FOR_STMT,
1043 NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE);
1045 if (scope == NULL_TREE)
1047 gcc_assert (!init || !(flag_new_for_scope > 0));
1048 if (!init)
1049 scope = begin_for_scope (&init);
1052 /* RANGE_FOR_STMTs do not use nor save the init tree, so we
1053 pop it now. */
1054 if (init)
1055 pop_stmt_list (init);
1056 RANGE_FOR_SCOPE (r) = scope;
1058 return r;
1061 /* Finish the head of a range-based for statement, which may
1062 be given by RANGE_FOR_STMT. DECL must be the declaration
1063 and EXPR must be the loop expression. */
1065 void
1066 finish_range_for_decl (tree range_for_stmt, tree decl, tree expr)
1068 RANGE_FOR_DECL (range_for_stmt) = decl;
1069 RANGE_FOR_EXPR (range_for_stmt) = expr;
1070 add_stmt (range_for_stmt);
1071 RANGE_FOR_BODY (range_for_stmt) = do_pushlevel (sk_block);
1074 /* Finish a break-statement. */
1076 tree
1077 finish_break_stmt (void)
1079 /* In switch statements break is sometimes stylistically used after
1080 a return statement. This can lead to spurious warnings about
1081 control reaching the end of a non-void function when it is
1082 inlined. Note that we are calling block_may_fallthru with
1083 language specific tree nodes; this works because
1084 block_may_fallthru returns true when given something it does not
1085 understand. */
1086 if (!block_may_fallthru (cur_stmt_list))
1087 return void_node;
1088 return add_stmt (build_stmt (input_location, BREAK_STMT));
1091 /* Finish a continue-statement. */
1093 tree
1094 finish_continue_stmt (void)
1096 return add_stmt (build_stmt (input_location, CONTINUE_STMT));
1099 /* Begin a switch-statement. Returns a new SWITCH_STMT if
1100 appropriate. */
1102 tree
1103 begin_switch_stmt (void)
1105 tree r, scope;
1107 scope = do_pushlevel (sk_cond);
1108 r = build_stmt (input_location, SWITCH_STMT, NULL_TREE, NULL_TREE, NULL_TREE, scope);
1110 begin_cond (&SWITCH_STMT_COND (r));
1112 return r;
1115 /* Finish the cond of a switch-statement. */
1117 void
1118 finish_switch_cond (tree cond, tree switch_stmt)
1120 tree orig_type = NULL;
1121 if (!processing_template_decl)
1123 /* Convert the condition to an integer or enumeration type. */
1124 cond = build_expr_type_conversion (WANT_INT | WANT_ENUM, cond, true);
1125 if (cond == NULL_TREE)
1127 error ("switch quantity not an integer");
1128 cond = error_mark_node;
1130 orig_type = TREE_TYPE (cond);
1131 if (cond != error_mark_node)
1133 /* Warn if the condition has boolean value. */
1134 if (TREE_CODE (orig_type) == BOOLEAN_TYPE)
1135 warning_at (input_location, OPT_Wswitch_bool,
1136 "switch condition has type bool");
1138 /* [stmt.switch]
1140 Integral promotions are performed. */
1141 cond = perform_integral_promotions (cond);
1142 cond = maybe_cleanup_point_expr (cond);
1145 if (check_for_bare_parameter_packs (cond))
1146 cond = error_mark_node;
1147 else if (!processing_template_decl && warn_sequence_point)
1148 verify_sequence_points (cond);
1150 finish_cond (&SWITCH_STMT_COND (switch_stmt), cond);
1151 SWITCH_STMT_TYPE (switch_stmt) = orig_type;
1152 add_stmt (switch_stmt);
1153 push_switch (switch_stmt);
1154 SWITCH_STMT_BODY (switch_stmt) = push_stmt_list ();
1157 /* Finish the body of a switch-statement, which may be given by
1158 SWITCH_STMT. The COND to switch on is indicated. */
1160 void
1161 finish_switch_stmt (tree switch_stmt)
1163 tree scope;
1165 SWITCH_STMT_BODY (switch_stmt) =
1166 pop_stmt_list (SWITCH_STMT_BODY (switch_stmt));
1167 pop_switch ();
1169 scope = SWITCH_STMT_SCOPE (switch_stmt);
1170 SWITCH_STMT_SCOPE (switch_stmt) = NULL;
1171 add_stmt (do_poplevel (scope));
1174 /* Begin a try-block. Returns a newly-created TRY_BLOCK if
1175 appropriate. */
1177 tree
1178 begin_try_block (void)
1180 tree r = build_stmt (input_location, TRY_BLOCK, NULL_TREE, NULL_TREE);
1181 add_stmt (r);
1182 TRY_STMTS (r) = push_stmt_list ();
1183 return r;
1186 /* Likewise, for a function-try-block. The block returned in
1187 *COMPOUND_STMT is an artificial outer scope, containing the
1188 function-try-block. */
1190 tree
1191 begin_function_try_block (tree *compound_stmt)
1193 tree r;
1194 /* This outer scope does not exist in the C++ standard, but we need
1195 a place to put __FUNCTION__ and similar variables. */
1196 *compound_stmt = begin_compound_stmt (0);
1197 r = begin_try_block ();
1198 FN_TRY_BLOCK_P (r) = 1;
1199 return r;
1202 /* Finish a try-block, which may be given by TRY_BLOCK. */
1204 void
1205 finish_try_block (tree try_block)
1207 TRY_STMTS (try_block) = pop_stmt_list (TRY_STMTS (try_block));
1208 TRY_HANDLERS (try_block) = push_stmt_list ();
1211 /* Finish the body of a cleanup try-block, which may be given by
1212 TRY_BLOCK. */
1214 void
1215 finish_cleanup_try_block (tree try_block)
1217 TRY_STMTS (try_block) = pop_stmt_list (TRY_STMTS (try_block));
1220 /* Finish an implicitly generated try-block, with a cleanup is given
1221 by CLEANUP. */
1223 void
1224 finish_cleanup (tree cleanup, tree try_block)
1226 TRY_HANDLERS (try_block) = cleanup;
1227 CLEANUP_P (try_block) = 1;
1230 /* Likewise, for a function-try-block. */
1232 void
1233 finish_function_try_block (tree try_block)
1235 finish_try_block (try_block);
1236 /* FIXME : something queer about CTOR_INITIALIZER somehow following
1237 the try block, but moving it inside. */
1238 in_function_try_handler = 1;
1241 /* Finish a handler-sequence for a try-block, which may be given by
1242 TRY_BLOCK. */
1244 void
1245 finish_handler_sequence (tree try_block)
1247 TRY_HANDLERS (try_block) = pop_stmt_list (TRY_HANDLERS (try_block));
1248 check_handlers (TRY_HANDLERS (try_block));
1251 /* Finish the handler-seq for a function-try-block, given by
1252 TRY_BLOCK. COMPOUND_STMT is the outer block created by
1253 begin_function_try_block. */
1255 void
1256 finish_function_handler_sequence (tree try_block, tree compound_stmt)
1258 in_function_try_handler = 0;
1259 finish_handler_sequence (try_block);
1260 finish_compound_stmt (compound_stmt);
1263 /* Begin a handler. Returns a HANDLER if appropriate. */
1265 tree
1266 begin_handler (void)
1268 tree r;
1270 r = build_stmt (input_location, HANDLER, NULL_TREE, NULL_TREE);
1271 add_stmt (r);
1273 /* Create a binding level for the eh_info and the exception object
1274 cleanup. */
1275 HANDLER_BODY (r) = do_pushlevel (sk_catch);
1277 return r;
1280 /* Finish the handler-parameters for a handler, which may be given by
1281 HANDLER. DECL is the declaration for the catch parameter, or NULL
1282 if this is a `catch (...)' clause. */
1284 void
1285 finish_handler_parms (tree decl, tree handler)
1287 tree type = NULL_TREE;
1288 if (processing_template_decl)
1290 if (decl)
1292 decl = pushdecl (decl);
1293 decl = push_template_decl (decl);
1294 HANDLER_PARMS (handler) = decl;
1295 type = TREE_TYPE (decl);
1298 else
1299 type = expand_start_catch_block (decl);
1300 HANDLER_TYPE (handler) = type;
1303 /* Finish a handler, which may be given by HANDLER. The BLOCKs are
1304 the return value from the matching call to finish_handler_parms. */
1306 void
1307 finish_handler (tree handler)
1309 if (!processing_template_decl)
1310 expand_end_catch_block ();
1311 HANDLER_BODY (handler) = do_poplevel (HANDLER_BODY (handler));
1314 /* Begin a compound statement. FLAGS contains some bits that control the
1315 behavior and context. If BCS_NO_SCOPE is set, the compound statement
1316 does not define a scope. If BCS_FN_BODY is set, this is the outermost
1317 block of a function. If BCS_TRY_BLOCK is set, this is the block
1318 created on behalf of a TRY statement. Returns a token to be passed to
1319 finish_compound_stmt. */
1321 tree
1322 begin_compound_stmt (unsigned int flags)
1324 tree r;
1326 if (flags & BCS_NO_SCOPE)
1328 r = push_stmt_list ();
1329 STATEMENT_LIST_NO_SCOPE (r) = 1;
1331 /* Normally, we try hard to keep the BLOCK for a statement-expression.
1332 But, if it's a statement-expression with a scopeless block, there's
1333 nothing to keep, and we don't want to accidentally keep a block
1334 *inside* the scopeless block. */
1335 keep_next_level (false);
1337 else
1338 r = do_pushlevel (flags & BCS_TRY_BLOCK ? sk_try : sk_block);
1340 /* When processing a template, we need to remember where the braces were,
1341 so that we can set up identical scopes when instantiating the template
1342 later. BIND_EXPR is a handy candidate for this.
1343 Note that do_poplevel won't create a BIND_EXPR itself here (and thus
1344 result in nested BIND_EXPRs), since we don't build BLOCK nodes when
1345 processing templates. */
1346 if (processing_template_decl)
1348 r = build3 (BIND_EXPR, NULL, NULL, r, NULL);
1349 BIND_EXPR_TRY_BLOCK (r) = (flags & BCS_TRY_BLOCK) != 0;
1350 BIND_EXPR_BODY_BLOCK (r) = (flags & BCS_FN_BODY) != 0;
1351 TREE_SIDE_EFFECTS (r) = 1;
1354 return r;
1357 /* Finish a compound-statement, which is given by STMT. */
1359 void
1360 finish_compound_stmt (tree stmt)
1362 if (TREE_CODE (stmt) == BIND_EXPR)
1364 tree body = do_poplevel (BIND_EXPR_BODY (stmt));
1365 /* If the STATEMENT_LIST is empty and this BIND_EXPR isn't special,
1366 discard the BIND_EXPR so it can be merged with the containing
1367 STATEMENT_LIST. */
1368 if (TREE_CODE (body) == STATEMENT_LIST
1369 && STATEMENT_LIST_HEAD (body) == NULL
1370 && !BIND_EXPR_BODY_BLOCK (stmt)
1371 && !BIND_EXPR_TRY_BLOCK (stmt))
1372 stmt = body;
1373 else
1374 BIND_EXPR_BODY (stmt) = body;
1376 else if (STATEMENT_LIST_NO_SCOPE (stmt))
1377 stmt = pop_stmt_list (stmt);
1378 else
1380 /* Destroy any ObjC "super" receivers that may have been
1381 created. */
1382 objc_clear_super_receiver ();
1384 stmt = do_poplevel (stmt);
1387 /* ??? See c_end_compound_stmt wrt statement expressions. */
1388 add_stmt (stmt);
1391 /* Finish an asm-statement, whose components are a STRING, some
1392 OUTPUT_OPERANDS, some INPUT_OPERANDS, some CLOBBERS and some
1393 LABELS. Also note whether the asm-statement should be
1394 considered volatile. */
1396 tree
1397 finish_asm_stmt (int volatile_p, tree string, tree output_operands,
1398 tree input_operands, tree clobbers, tree labels)
1400 tree r;
1401 tree t;
1402 int ninputs = list_length (input_operands);
1403 int noutputs = list_length (output_operands);
1405 if (!processing_template_decl)
1407 const char *constraint;
1408 const char **oconstraints;
1409 bool allows_mem, allows_reg, is_inout;
1410 tree operand;
1411 int i;
1413 oconstraints = XALLOCAVEC (const char *, noutputs);
1415 string = resolve_asm_operand_names (string, output_operands,
1416 input_operands, labels);
1418 for (i = 0, t = output_operands; t; t = TREE_CHAIN (t), ++i)
1420 operand = TREE_VALUE (t);
1422 /* ??? Really, this should not be here. Users should be using a
1423 proper lvalue, dammit. But there's a long history of using
1424 casts in the output operands. In cases like longlong.h, this
1425 becomes a primitive form of typechecking -- if the cast can be
1426 removed, then the output operand had a type of the proper width;
1427 otherwise we'll get an error. Gross, but ... */
1428 STRIP_NOPS (operand);
1430 operand = mark_lvalue_use (operand);
1432 if (!lvalue_or_else (operand, lv_asm, tf_warning_or_error))
1433 operand = error_mark_node;
1435 if (operand != error_mark_node
1436 && (TREE_READONLY (operand)
1437 || CP_TYPE_CONST_P (TREE_TYPE (operand))
1438 /* Functions are not modifiable, even though they are
1439 lvalues. */
1440 || TREE_CODE (TREE_TYPE (operand)) == FUNCTION_TYPE
1441 || TREE_CODE (TREE_TYPE (operand)) == METHOD_TYPE
1442 /* If it's an aggregate and any field is const, then it is
1443 effectively const. */
1444 || (CLASS_TYPE_P (TREE_TYPE (operand))
1445 && C_TYPE_FIELDS_READONLY (TREE_TYPE (operand)))))
1446 cxx_readonly_error (operand, lv_asm);
1448 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
1449 oconstraints[i] = constraint;
1451 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
1452 &allows_mem, &allows_reg, &is_inout))
1454 /* If the operand is going to end up in memory,
1455 mark it addressable. */
1456 if (!allows_reg && !cxx_mark_addressable (operand))
1457 operand = error_mark_node;
1459 else
1460 operand = error_mark_node;
1462 TREE_VALUE (t) = operand;
1465 for (i = 0, t = input_operands; t; ++i, t = TREE_CHAIN (t))
1467 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
1468 bool constraint_parsed
1469 = parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
1470 oconstraints, &allows_mem, &allows_reg);
1471 /* If the operand is going to end up in memory, don't call
1472 decay_conversion. */
1473 if (constraint_parsed && !allows_reg && allows_mem)
1474 operand = mark_lvalue_use (TREE_VALUE (t));
1475 else
1476 operand = decay_conversion (TREE_VALUE (t), tf_warning_or_error);
1478 /* If the type of the operand hasn't been determined (e.g.,
1479 because it involves an overloaded function), then issue
1480 an error message. There's no context available to
1481 resolve the overloading. */
1482 if (TREE_TYPE (operand) == unknown_type_node)
1484 error ("type of asm operand %qE could not be determined",
1485 TREE_VALUE (t));
1486 operand = error_mark_node;
1489 if (constraint_parsed)
1491 /* If the operand is going to end up in memory,
1492 mark it addressable. */
1493 if (!allows_reg && allows_mem)
1495 /* Strip the nops as we allow this case. FIXME, this really
1496 should be rejected or made deprecated. */
1497 STRIP_NOPS (operand);
1498 if (!cxx_mark_addressable (operand))
1499 operand = error_mark_node;
1501 else if (!allows_reg && !allows_mem)
1503 /* If constraint allows neither register nor memory,
1504 try harder to get a constant. */
1505 tree constop = maybe_constant_value (operand);
1506 if (TREE_CONSTANT (constop))
1507 operand = constop;
1510 else
1511 operand = error_mark_node;
1513 TREE_VALUE (t) = operand;
1517 r = build_stmt (input_location, ASM_EXPR, string,
1518 output_operands, input_operands,
1519 clobbers, labels);
1520 ASM_VOLATILE_P (r) = volatile_p || noutputs == 0;
1521 r = maybe_cleanup_point_expr_void (r);
1522 return add_stmt (r);
1525 /* Finish a label with the indicated NAME. Returns the new label. */
1527 tree
1528 finish_label_stmt (tree name)
1530 tree decl = define_label (input_location, name);
1532 if (decl == error_mark_node)
1533 return error_mark_node;
1535 add_stmt (build_stmt (input_location, LABEL_EXPR, decl));
1537 return decl;
1540 /* Finish a series of declarations for local labels. G++ allows users
1541 to declare "local" labels, i.e., labels with scope. This extension
1542 is useful when writing code involving statement-expressions. */
1544 void
1545 finish_label_decl (tree name)
1547 if (!at_function_scope_p ())
1549 error ("__label__ declarations are only allowed in function scopes");
1550 return;
1553 add_decl_expr (declare_local_label (name));
1556 /* When DECL goes out of scope, make sure that CLEANUP is executed. */
1558 void
1559 finish_decl_cleanup (tree decl, tree cleanup)
1561 push_cleanup (decl, cleanup, false);
1564 /* If the current scope exits with an exception, run CLEANUP. */
1566 void
1567 finish_eh_cleanup (tree cleanup)
1569 push_cleanup (NULL, cleanup, true);
1572 /* The MEM_INITS is a list of mem-initializers, in reverse of the
1573 order they were written by the user. Each node is as for
1574 emit_mem_initializers. */
1576 void
1577 finish_mem_initializers (tree mem_inits)
1579 /* Reorder the MEM_INITS so that they are in the order they appeared
1580 in the source program. */
1581 mem_inits = nreverse (mem_inits);
1583 if (processing_template_decl)
1585 tree mem;
1587 for (mem = mem_inits; mem; mem = TREE_CHAIN (mem))
1589 /* If the TREE_PURPOSE is a TYPE_PACK_EXPANSION, skip the
1590 check for bare parameter packs in the TREE_VALUE, because
1591 any parameter packs in the TREE_VALUE have already been
1592 bound as part of the TREE_PURPOSE. See
1593 make_pack_expansion for more information. */
1594 if (TREE_CODE (TREE_PURPOSE (mem)) != TYPE_PACK_EXPANSION
1595 && check_for_bare_parameter_packs (TREE_VALUE (mem)))
1596 TREE_VALUE (mem) = error_mark_node;
1599 add_stmt (build_min_nt_loc (UNKNOWN_LOCATION,
1600 CTOR_INITIALIZER, mem_inits));
1602 else
1603 emit_mem_initializers (mem_inits);
1606 /* Obfuscate EXPR if it looks like an id-expression or member access so
1607 that the call to finish_decltype in do_auto_deduction will give the
1608 right result. */
1610 tree
1611 force_paren_expr (tree expr)
1613 /* This is only needed for decltype(auto) in C++14. */
1614 if (cxx_dialect < cxx1y)
1615 return expr;
1617 /* If we're in unevaluated context, we can't be deducing a
1618 return/initializer type, so we don't need to mess with this. */
1619 if (cp_unevaluated_operand)
1620 return expr;
1622 if (!DECL_P (expr) && TREE_CODE (expr) != COMPONENT_REF
1623 && TREE_CODE (expr) != SCOPE_REF)
1624 return expr;
1626 if (TREE_CODE (expr) == COMPONENT_REF)
1627 REF_PARENTHESIZED_P (expr) = true;
1628 else if (type_dependent_expression_p (expr))
1629 expr = build1 (PAREN_EXPR, TREE_TYPE (expr), expr);
1630 else
1632 cp_lvalue_kind kind = lvalue_kind (expr);
1633 if ((kind & ~clk_class) != clk_none)
1635 tree type = unlowered_expr_type (expr);
1636 bool rval = !!(kind & clk_rvalueref);
1637 type = cp_build_reference_type (type, rval);
1638 expr = build_static_cast (type, expr, tf_error);
1642 return expr;
1645 /* Finish a parenthesized expression EXPR. */
1647 tree
1648 finish_parenthesized_expr (tree expr)
1650 if (EXPR_P (expr))
1651 /* This inhibits warnings in c_common_truthvalue_conversion. */
1652 TREE_NO_WARNING (expr) = 1;
1654 if (TREE_CODE (expr) == OFFSET_REF
1655 || TREE_CODE (expr) == SCOPE_REF)
1656 /* [expr.unary.op]/3 The qualified id of a pointer-to-member must not be
1657 enclosed in parentheses. */
1658 PTRMEM_OK_P (expr) = 0;
1660 if (TREE_CODE (expr) == STRING_CST)
1661 PAREN_STRING_LITERAL_P (expr) = 1;
1663 expr = force_paren_expr (expr);
1665 return expr;
1668 /* Finish a reference to a non-static data member (DECL) that is not
1669 preceded by `.' or `->'. */
1671 tree
1672 finish_non_static_data_member (tree decl, tree object, tree qualifying_scope)
1674 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
1676 if (!object)
1678 tree scope = qualifying_scope;
1679 if (scope == NULL_TREE)
1680 scope = context_for_name_lookup (decl);
1681 object = maybe_dummy_object (scope, NULL);
1684 object = maybe_resolve_dummy (object, true);
1685 if (object == error_mark_node)
1686 return error_mark_node;
1688 /* DR 613: Can use non-static data members without an associated
1689 object in sizeof/decltype/alignof. */
1690 if (is_dummy_object (object) && cp_unevaluated_operand == 0
1691 && (!processing_template_decl || !current_class_ref))
1693 if (current_function_decl
1694 && DECL_STATIC_FUNCTION_P (current_function_decl))
1695 error ("invalid use of member %q+D in static member function", decl);
1696 else
1697 error ("invalid use of non-static data member %q+D", decl);
1698 error ("from this location");
1700 return error_mark_node;
1703 if (current_class_ptr)
1704 TREE_USED (current_class_ptr) = 1;
1705 if (processing_template_decl && !qualifying_scope)
1707 tree type = TREE_TYPE (decl);
1709 if (TREE_CODE (type) == REFERENCE_TYPE)
1710 /* Quals on the object don't matter. */;
1711 else if (PACK_EXPANSION_P (type))
1712 /* Don't bother trying to represent this. */
1713 type = NULL_TREE;
1714 else
1716 /* Set the cv qualifiers. */
1717 int quals = cp_type_quals (TREE_TYPE (object));
1719 if (DECL_MUTABLE_P (decl))
1720 quals &= ~TYPE_QUAL_CONST;
1722 quals |= cp_type_quals (TREE_TYPE (decl));
1723 type = cp_build_qualified_type (type, quals);
1726 return (convert_from_reference
1727 (build_min (COMPONENT_REF, type, object, decl, NULL_TREE)));
1729 /* If PROCESSING_TEMPLATE_DECL is nonzero here, then
1730 QUALIFYING_SCOPE is also non-null. Wrap this in a SCOPE_REF
1731 for now. */
1732 else if (processing_template_decl)
1733 return build_qualified_name (TREE_TYPE (decl),
1734 qualifying_scope,
1735 decl,
1736 /*template_p=*/false);
1737 else
1739 tree access_type = TREE_TYPE (object);
1741 perform_or_defer_access_check (TYPE_BINFO (access_type), decl,
1742 decl, tf_warning_or_error);
1744 /* If the data member was named `C::M', convert `*this' to `C'
1745 first. */
1746 if (qualifying_scope)
1748 tree binfo = NULL_TREE;
1749 object = build_scoped_ref (object, qualifying_scope,
1750 &binfo);
1753 return build_class_member_access_expr (object, decl,
1754 /*access_path=*/NULL_TREE,
1755 /*preserve_reference=*/false,
1756 tf_warning_or_error);
1760 /* If we are currently parsing a template and we encountered a typedef
1761 TYPEDEF_DECL that is being accessed though CONTEXT, this function
1762 adds the typedef to a list tied to the current template.
1763 At template instantiation time, that list is walked and access check
1764 performed for each typedef.
1765 LOCATION is the location of the usage point of TYPEDEF_DECL. */
1767 void
1768 add_typedef_to_current_template_for_access_check (tree typedef_decl,
1769 tree context,
1770 location_t location)
1772 tree template_info = NULL;
1773 tree cs = current_scope ();
1775 if (!is_typedef_decl (typedef_decl)
1776 || !context
1777 || !CLASS_TYPE_P (context)
1778 || !cs)
1779 return;
1781 if (CLASS_TYPE_P (cs) || TREE_CODE (cs) == FUNCTION_DECL)
1782 template_info = get_template_info (cs);
1784 if (template_info
1785 && TI_TEMPLATE (template_info)
1786 && !currently_open_class (context))
1787 append_type_to_template_for_access_check (cs, typedef_decl,
1788 context, location);
1791 /* DECL was the declaration to which a qualified-id resolved. Issue
1792 an error message if it is not accessible. If OBJECT_TYPE is
1793 non-NULL, we have just seen `x->' or `x.' and OBJECT_TYPE is the
1794 type of `*x', or `x', respectively. If the DECL was named as
1795 `A::B' then NESTED_NAME_SPECIFIER is `A'. */
1797 void
1798 check_accessibility_of_qualified_id (tree decl,
1799 tree object_type,
1800 tree nested_name_specifier)
1802 tree scope;
1803 tree qualifying_type = NULL_TREE;
1805 /* If we are parsing a template declaration and if decl is a typedef,
1806 add it to a list tied to the template.
1807 At template instantiation time, that list will be walked and
1808 access check performed. */
1809 add_typedef_to_current_template_for_access_check (decl,
1810 nested_name_specifier
1811 ? nested_name_specifier
1812 : DECL_CONTEXT (decl),
1813 input_location);
1815 /* If we're not checking, return immediately. */
1816 if (deferred_access_no_check)
1817 return;
1819 /* Determine the SCOPE of DECL. */
1820 scope = context_for_name_lookup (decl);
1821 /* If the SCOPE is not a type, then DECL is not a member. */
1822 if (!TYPE_P (scope))
1823 return;
1824 /* Compute the scope through which DECL is being accessed. */
1825 if (object_type
1826 /* OBJECT_TYPE might not be a class type; consider:
1828 class A { typedef int I; };
1829 I *p;
1830 p->A::I::~I();
1832 In this case, we will have "A::I" as the DECL, but "I" as the
1833 OBJECT_TYPE. */
1834 && CLASS_TYPE_P (object_type)
1835 && DERIVED_FROM_P (scope, object_type))
1836 /* If we are processing a `->' or `.' expression, use the type of the
1837 left-hand side. */
1838 qualifying_type = object_type;
1839 else if (nested_name_specifier)
1841 /* If the reference is to a non-static member of the
1842 current class, treat it as if it were referenced through
1843 `this'. */
1844 tree ct;
1845 if (DECL_NONSTATIC_MEMBER_P (decl)
1846 && current_class_ptr
1847 && DERIVED_FROM_P (scope, ct = current_nonlambda_class_type ()))
1848 qualifying_type = ct;
1849 /* Otherwise, use the type indicated by the
1850 nested-name-specifier. */
1851 else
1852 qualifying_type = nested_name_specifier;
1854 else
1855 /* Otherwise, the name must be from the current class or one of
1856 its bases. */
1857 qualifying_type = currently_open_derived_class (scope);
1859 if (qualifying_type
1860 /* It is possible for qualifying type to be a TEMPLATE_TYPE_PARM
1861 or similar in a default argument value. */
1862 && CLASS_TYPE_P (qualifying_type)
1863 && !dependent_type_p (qualifying_type))
1864 perform_or_defer_access_check (TYPE_BINFO (qualifying_type), decl,
1865 decl, tf_warning_or_error);
1868 /* EXPR is the result of a qualified-id. The QUALIFYING_CLASS was the
1869 class named to the left of the "::" operator. DONE is true if this
1870 expression is a complete postfix-expression; it is false if this
1871 expression is followed by '->', '[', '(', etc. ADDRESS_P is true
1872 iff this expression is the operand of '&'. TEMPLATE_P is true iff
1873 the qualified-id was of the form "A::template B". TEMPLATE_ARG_P
1874 is true iff this qualified name appears as a template argument. */
1876 tree
1877 finish_qualified_id_expr (tree qualifying_class,
1878 tree expr,
1879 bool done,
1880 bool address_p,
1881 bool template_p,
1882 bool template_arg_p,
1883 tsubst_flags_t complain)
1885 gcc_assert (TYPE_P (qualifying_class));
1887 if (error_operand_p (expr))
1888 return error_mark_node;
1890 if ((DECL_P (expr) || BASELINK_P (expr))
1891 && !mark_used (expr, complain))
1892 return error_mark_node;
1894 if (template_p)
1895 check_template_keyword (expr);
1897 /* If EXPR occurs as the operand of '&', use special handling that
1898 permits a pointer-to-member. */
1899 if (address_p && done)
1901 if (TREE_CODE (expr) == SCOPE_REF)
1902 expr = TREE_OPERAND (expr, 1);
1903 expr = build_offset_ref (qualifying_class, expr,
1904 /*address_p=*/true, complain);
1905 return expr;
1908 /* No need to check access within an enum. */
1909 if (TREE_CODE (qualifying_class) == ENUMERAL_TYPE)
1910 return expr;
1912 /* Within the scope of a class, turn references to non-static
1913 members into expression of the form "this->...". */
1914 if (template_arg_p)
1915 /* But, within a template argument, we do not want make the
1916 transformation, as there is no "this" pointer. */
1918 else if (TREE_CODE (expr) == FIELD_DECL)
1920 push_deferring_access_checks (dk_no_check);
1921 expr = finish_non_static_data_member (expr, NULL_TREE,
1922 qualifying_class);
1923 pop_deferring_access_checks ();
1925 else if (BASELINK_P (expr) && !processing_template_decl)
1927 /* See if any of the functions are non-static members. */
1928 /* If so, the expression may be relative to 'this'. */
1929 if (!shared_member_p (expr)
1930 && current_class_ptr
1931 && DERIVED_FROM_P (qualifying_class,
1932 current_nonlambda_class_type ()))
1933 expr = (build_class_member_access_expr
1934 (maybe_dummy_object (qualifying_class, NULL),
1935 expr,
1936 BASELINK_ACCESS_BINFO (expr),
1937 /*preserve_reference=*/false,
1938 complain));
1939 else if (done)
1940 /* The expression is a qualified name whose address is not
1941 being taken. */
1942 expr = build_offset_ref (qualifying_class, expr, /*address_p=*/false,
1943 complain);
1945 else if (BASELINK_P (expr))
1947 else
1949 /* In a template, return a SCOPE_REF for most qualified-ids
1950 so that we can check access at instantiation time. But if
1951 we're looking at a member of the current instantiation, we
1952 know we have access and building up the SCOPE_REF confuses
1953 non-type template argument handling. */
1954 if (processing_template_decl
1955 && !currently_open_class (qualifying_class))
1956 expr = build_qualified_name (TREE_TYPE (expr),
1957 qualifying_class, expr,
1958 template_p);
1960 expr = convert_from_reference (expr);
1963 return expr;
1966 /* Begin a statement-expression. The value returned must be passed to
1967 finish_stmt_expr. */
1969 tree
1970 begin_stmt_expr (void)
1972 return push_stmt_list ();
1975 /* Process the final expression of a statement expression. EXPR can be
1976 NULL, if the final expression is empty. Return a STATEMENT_LIST
1977 containing all the statements in the statement-expression, or
1978 ERROR_MARK_NODE if there was an error. */
1980 tree
1981 finish_stmt_expr_expr (tree expr, tree stmt_expr)
1983 if (error_operand_p (expr))
1985 /* The type of the statement-expression is the type of the last
1986 expression. */
1987 TREE_TYPE (stmt_expr) = error_mark_node;
1988 return error_mark_node;
1991 /* If the last statement does not have "void" type, then the value
1992 of the last statement is the value of the entire expression. */
1993 if (expr)
1995 tree type = TREE_TYPE (expr);
1997 if (processing_template_decl)
1999 expr = build_stmt (input_location, EXPR_STMT, expr);
2000 expr = add_stmt (expr);
2001 /* Mark the last statement so that we can recognize it as such at
2002 template-instantiation time. */
2003 EXPR_STMT_STMT_EXPR_RESULT (expr) = 1;
2005 else if (VOID_TYPE_P (type))
2007 /* Just treat this like an ordinary statement. */
2008 expr = finish_expr_stmt (expr);
2010 else
2012 /* It actually has a value we need to deal with. First, force it
2013 to be an rvalue so that we won't need to build up a copy
2014 constructor call later when we try to assign it to something. */
2015 expr = force_rvalue (expr, tf_warning_or_error);
2016 if (error_operand_p (expr))
2017 return error_mark_node;
2019 /* Update for array-to-pointer decay. */
2020 type = TREE_TYPE (expr);
2022 /* Wrap it in a CLEANUP_POINT_EXPR and add it to the list like a
2023 normal statement, but don't convert to void or actually add
2024 the EXPR_STMT. */
2025 if (TREE_CODE (expr) != CLEANUP_POINT_EXPR)
2026 expr = maybe_cleanup_point_expr (expr);
2027 add_stmt (expr);
2030 /* The type of the statement-expression is the type of the last
2031 expression. */
2032 TREE_TYPE (stmt_expr) = type;
2035 return stmt_expr;
2038 /* Finish a statement-expression. EXPR should be the value returned
2039 by the previous begin_stmt_expr. Returns an expression
2040 representing the statement-expression. */
2042 tree
2043 finish_stmt_expr (tree stmt_expr, bool has_no_scope)
2045 tree type;
2046 tree result;
2048 if (error_operand_p (stmt_expr))
2050 pop_stmt_list (stmt_expr);
2051 return error_mark_node;
2054 gcc_assert (TREE_CODE (stmt_expr) == STATEMENT_LIST);
2056 type = TREE_TYPE (stmt_expr);
2057 result = pop_stmt_list (stmt_expr);
2058 TREE_TYPE (result) = type;
2060 if (processing_template_decl)
2062 result = build_min (STMT_EXPR, type, result);
2063 TREE_SIDE_EFFECTS (result) = 1;
2064 STMT_EXPR_NO_SCOPE (result) = has_no_scope;
2066 else if (CLASS_TYPE_P (type))
2068 /* Wrap the statement-expression in a TARGET_EXPR so that the
2069 temporary object created by the final expression is destroyed at
2070 the end of the full-expression containing the
2071 statement-expression. */
2072 result = force_target_expr (type, result, tf_warning_or_error);
2075 return result;
2078 /* Returns the expression which provides the value of STMT_EXPR. */
2080 tree
2081 stmt_expr_value_expr (tree stmt_expr)
2083 tree t = STMT_EXPR_STMT (stmt_expr);
2085 if (TREE_CODE (t) == BIND_EXPR)
2086 t = BIND_EXPR_BODY (t);
2088 if (TREE_CODE (t) == STATEMENT_LIST && STATEMENT_LIST_TAIL (t))
2089 t = STATEMENT_LIST_TAIL (t)->stmt;
2091 if (TREE_CODE (t) == EXPR_STMT)
2092 t = EXPR_STMT_EXPR (t);
2094 return t;
2097 /* Return TRUE iff EXPR_STMT is an empty list of
2098 expression statements. */
2100 bool
2101 empty_expr_stmt_p (tree expr_stmt)
2103 tree body = NULL_TREE;
2105 if (expr_stmt == void_node)
2106 return true;
2108 if (expr_stmt)
2110 if (TREE_CODE (expr_stmt) == EXPR_STMT)
2111 body = EXPR_STMT_EXPR (expr_stmt);
2112 else if (TREE_CODE (expr_stmt) == STATEMENT_LIST)
2113 body = expr_stmt;
2116 if (body)
2118 if (TREE_CODE (body) == STATEMENT_LIST)
2119 return tsi_end_p (tsi_start (body));
2120 else
2121 return empty_expr_stmt_p (body);
2123 return false;
2126 /* Perform Koenig lookup. FN is the postfix-expression representing
2127 the function (or functions) to call; ARGS are the arguments to the
2128 call. Returns the functions to be considered by overload resolution. */
2130 tree
2131 perform_koenig_lookup (tree fn, vec<tree, va_gc> *args,
2132 tsubst_flags_t complain)
2134 tree identifier = NULL_TREE;
2135 tree functions = NULL_TREE;
2136 tree tmpl_args = NULL_TREE;
2137 bool template_id = false;
2139 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
2141 /* Use a separate flag to handle null args. */
2142 template_id = true;
2143 tmpl_args = TREE_OPERAND (fn, 1);
2144 fn = TREE_OPERAND (fn, 0);
2147 /* Find the name of the overloaded function. */
2148 if (identifier_p (fn))
2149 identifier = fn;
2150 else if (is_overloaded_fn (fn))
2152 functions = fn;
2153 identifier = DECL_NAME (get_first_fn (functions));
2155 else if (DECL_P (fn))
2157 functions = fn;
2158 identifier = DECL_NAME (fn);
2161 /* A call to a namespace-scope function using an unqualified name.
2163 Do Koenig lookup -- unless any of the arguments are
2164 type-dependent. */
2165 if (!any_type_dependent_arguments_p (args)
2166 && !any_dependent_template_arguments_p (tmpl_args))
2168 fn = lookup_arg_dependent (identifier, functions, args);
2169 if (!fn)
2171 /* The unqualified name could not be resolved. */
2172 if (complain)
2173 fn = unqualified_fn_lookup_error (identifier);
2174 else
2175 fn = identifier;
2179 if (fn && template_id)
2180 fn = build2 (TEMPLATE_ID_EXPR, unknown_type_node, fn, tmpl_args);
2182 return fn;
2185 /* Generate an expression for `FN (ARGS)'. This may change the
2186 contents of ARGS.
2188 If DISALLOW_VIRTUAL is true, the call to FN will be not generated
2189 as a virtual call, even if FN is virtual. (This flag is set when
2190 encountering an expression where the function name is explicitly
2191 qualified. For example a call to `X::f' never generates a virtual
2192 call.)
2194 Returns code for the call. */
2196 tree
2197 finish_call_expr (tree fn, vec<tree, va_gc> **args, bool disallow_virtual,
2198 bool koenig_p, tsubst_flags_t complain)
2200 tree result;
2201 tree orig_fn;
2202 vec<tree, va_gc> *orig_args = NULL;
2204 if (fn == error_mark_node)
2205 return error_mark_node;
2207 gcc_assert (!TYPE_P (fn));
2209 orig_fn = fn;
2211 if (processing_template_decl)
2213 /* If the call expression is dependent, build a CALL_EXPR node
2214 with no type; type_dependent_expression_p recognizes
2215 expressions with no type as being dependent. */
2216 if (type_dependent_expression_p (fn)
2217 || any_type_dependent_arguments_p (*args)
2218 /* For a non-static member function that doesn't have an
2219 explicit object argument, we need to specifically
2220 test the type dependency of the "this" pointer because it
2221 is not included in *ARGS even though it is considered to
2222 be part of the list of arguments. Note that this is
2223 related to CWG issues 515 and 1005. */
2224 || (TREE_CODE (fn) != COMPONENT_REF
2225 && non_static_member_function_p (fn)
2226 && current_class_ref
2227 && type_dependent_expression_p (current_class_ref)))
2229 result = build_nt_call_vec (fn, *args);
2230 SET_EXPR_LOCATION (result, EXPR_LOC_OR_LOC (fn, input_location));
2231 KOENIG_LOOKUP_P (result) = koenig_p;
2232 if (cfun)
2236 tree fndecl = OVL_CURRENT (fn);
2237 if (TREE_CODE (fndecl) != FUNCTION_DECL
2238 || !TREE_THIS_VOLATILE (fndecl))
2239 break;
2240 fn = OVL_NEXT (fn);
2242 while (fn);
2243 if (!fn)
2244 current_function_returns_abnormally = 1;
2246 return result;
2248 orig_args = make_tree_vector_copy (*args);
2249 if (!BASELINK_P (fn)
2250 && TREE_CODE (fn) != PSEUDO_DTOR_EXPR
2251 && TREE_TYPE (fn) != unknown_type_node)
2252 fn = build_non_dependent_expr (fn);
2253 make_args_non_dependent (*args);
2256 if (TREE_CODE (fn) == COMPONENT_REF)
2258 tree member = TREE_OPERAND (fn, 1);
2259 if (BASELINK_P (member))
2261 tree object = TREE_OPERAND (fn, 0);
2262 return build_new_method_call (object, member,
2263 args, NULL_TREE,
2264 (disallow_virtual
2265 ? LOOKUP_NORMAL | LOOKUP_NONVIRTUAL
2266 : LOOKUP_NORMAL),
2267 /*fn_p=*/NULL,
2268 complain);
2272 /* Per 13.3.1.1, '(&f)(...)' is the same as '(f)(...)'. */
2273 if (TREE_CODE (fn) == ADDR_EXPR
2274 && TREE_CODE (TREE_OPERAND (fn, 0)) == OVERLOAD)
2275 fn = TREE_OPERAND (fn, 0);
2277 if (is_overloaded_fn (fn))
2278 fn = baselink_for_fns (fn);
2280 result = NULL_TREE;
2281 if (BASELINK_P (fn))
2283 tree object;
2285 /* A call to a member function. From [over.call.func]:
2287 If the keyword this is in scope and refers to the class of
2288 that member function, or a derived class thereof, then the
2289 function call is transformed into a qualified function call
2290 using (*this) as the postfix-expression to the left of the
2291 . operator.... [Otherwise] a contrived object of type T
2292 becomes the implied object argument.
2294 In this situation:
2296 struct A { void f(); };
2297 struct B : public A {};
2298 struct C : public A { void g() { B::f(); }};
2300 "the class of that member function" refers to `A'. But 11.2
2301 [class.access.base] says that we need to convert 'this' to B* as
2302 part of the access, so we pass 'B' to maybe_dummy_object. */
2304 object = maybe_dummy_object (BINFO_TYPE (BASELINK_ACCESS_BINFO (fn)),
2305 NULL);
2307 if (processing_template_decl)
2309 if (type_dependent_expression_p (object))
2311 tree ret = build_nt_call_vec (orig_fn, orig_args);
2312 release_tree_vector (orig_args);
2313 return ret;
2315 object = build_non_dependent_expr (object);
2318 result = build_new_method_call (object, fn, args, NULL_TREE,
2319 (disallow_virtual
2320 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
2321 : LOOKUP_NORMAL),
2322 /*fn_p=*/NULL,
2323 complain);
2325 else if (is_overloaded_fn (fn))
2327 /* If the function is an overloaded builtin, resolve it. */
2328 if (TREE_CODE (fn) == FUNCTION_DECL
2329 && (DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL
2330 || DECL_BUILT_IN_CLASS (fn) == BUILT_IN_MD))
2331 result = resolve_overloaded_builtin (input_location, fn, *args);
2333 if (!result)
2335 if (warn_sizeof_pointer_memaccess
2336 && !vec_safe_is_empty (*args)
2337 && !processing_template_decl)
2339 location_t sizeof_arg_loc[3];
2340 tree sizeof_arg[3];
2341 unsigned int i;
2342 for (i = 0; i < 3; i++)
2344 tree t;
2346 sizeof_arg_loc[i] = UNKNOWN_LOCATION;
2347 sizeof_arg[i] = NULL_TREE;
2348 if (i >= (*args)->length ())
2349 continue;
2350 t = (**args)[i];
2351 if (TREE_CODE (t) != SIZEOF_EXPR)
2352 continue;
2353 if (SIZEOF_EXPR_TYPE_P (t))
2354 sizeof_arg[i] = TREE_TYPE (TREE_OPERAND (t, 0));
2355 else
2356 sizeof_arg[i] = TREE_OPERAND (t, 0);
2357 sizeof_arg_loc[i] = EXPR_LOCATION (t);
2359 sizeof_pointer_memaccess_warning
2360 (sizeof_arg_loc, fn, *args,
2361 sizeof_arg, same_type_ignoring_top_level_qualifiers_p);
2364 /* A call to a namespace-scope function. */
2365 result = build_new_function_call (fn, args, koenig_p, complain);
2368 else if (TREE_CODE (fn) == PSEUDO_DTOR_EXPR)
2370 if (!vec_safe_is_empty (*args))
2371 error ("arguments to destructor are not allowed");
2372 /* Mark the pseudo-destructor call as having side-effects so
2373 that we do not issue warnings about its use. */
2374 result = build1 (NOP_EXPR,
2375 void_type_node,
2376 TREE_OPERAND (fn, 0));
2377 TREE_SIDE_EFFECTS (result) = 1;
2379 else if (CLASS_TYPE_P (TREE_TYPE (fn)))
2380 /* If the "function" is really an object of class type, it might
2381 have an overloaded `operator ()'. */
2382 result = build_op_call (fn, args, complain);
2384 if (!result)
2385 /* A call where the function is unknown. */
2386 result = cp_build_function_call_vec (fn, args, complain);
2388 if (processing_template_decl && result != error_mark_node)
2390 if (INDIRECT_REF_P (result))
2391 result = TREE_OPERAND (result, 0);
2392 result = build_call_vec (TREE_TYPE (result), orig_fn, orig_args);
2393 SET_EXPR_LOCATION (result, input_location);
2394 KOENIG_LOOKUP_P (result) = koenig_p;
2395 release_tree_vector (orig_args);
2396 result = convert_from_reference (result);
2399 if (koenig_p)
2401 /* Free garbage OVERLOADs from arg-dependent lookup. */
2402 tree next = NULL_TREE;
2403 for (fn = orig_fn;
2404 fn && TREE_CODE (fn) == OVERLOAD && OVL_ARG_DEPENDENT (fn);
2405 fn = next)
2407 if (processing_template_decl)
2408 /* In a template, we'll re-use them at instantiation time. */
2409 OVL_ARG_DEPENDENT (fn) = false;
2410 else
2412 next = OVL_CHAIN (fn);
2413 ggc_free (fn);
2418 return result;
2421 /* Finish a call to a postfix increment or decrement or EXPR. (Which
2422 is indicated by CODE, which should be POSTINCREMENT_EXPR or
2423 POSTDECREMENT_EXPR.) */
2425 tree
2426 finish_increment_expr (tree expr, enum tree_code code)
2428 return build_x_unary_op (input_location, code, expr, tf_warning_or_error);
2431 /* Finish a use of `this'. Returns an expression for `this'. */
2433 tree
2434 finish_this_expr (void)
2436 tree result;
2438 if (current_class_ptr)
2440 tree type = TREE_TYPE (current_class_ref);
2442 /* In a lambda expression, 'this' refers to the captured 'this'. */
2443 if (LAMBDA_TYPE_P (type))
2444 result = lambda_expr_this_capture (CLASSTYPE_LAMBDA_EXPR (type), true);
2445 else
2446 result = current_class_ptr;
2448 else if (current_function_decl
2449 && DECL_STATIC_FUNCTION_P (current_function_decl))
2451 error ("%<this%> is unavailable for static member functions");
2452 result = error_mark_node;
2454 else
2456 if (current_function_decl)
2457 error ("invalid use of %<this%> in non-member function");
2458 else
2459 error ("invalid use of %<this%> at top level");
2460 result = error_mark_node;
2463 /* The keyword 'this' is a prvalue expression. */
2464 result = rvalue (result);
2466 return result;
2469 /* Finish a pseudo-destructor expression. If SCOPE is NULL, the
2470 expression was of the form `OBJECT.~DESTRUCTOR' where DESTRUCTOR is
2471 the TYPE for the type given. If SCOPE is non-NULL, the expression
2472 was of the form `OBJECT.SCOPE::~DESTRUCTOR'. */
2474 tree
2475 finish_pseudo_destructor_expr (tree object, tree scope, tree destructor,
2476 location_t loc)
2478 if (object == error_mark_node || destructor == error_mark_node)
2479 return error_mark_node;
2481 gcc_assert (TYPE_P (destructor));
2483 if (!processing_template_decl)
2485 if (scope == error_mark_node)
2487 error_at (loc, "invalid qualifying scope in pseudo-destructor name");
2488 return error_mark_node;
2490 if (is_auto (destructor))
2491 destructor = TREE_TYPE (object);
2492 if (scope && TYPE_P (scope) && !check_dtor_name (scope, destructor))
2494 error_at (loc,
2495 "qualified type %qT does not match destructor name ~%qT",
2496 scope, destructor);
2497 return error_mark_node;
2501 /* [expr.pseudo] says both:
2503 The type designated by the pseudo-destructor-name shall be
2504 the same as the object type.
2506 and:
2508 The cv-unqualified versions of the object type and of the
2509 type designated by the pseudo-destructor-name shall be the
2510 same type.
2512 We implement the more generous second sentence, since that is
2513 what most other compilers do. */
2514 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (object),
2515 destructor))
2517 error_at (loc, "%qE is not of type %qT", object, destructor);
2518 return error_mark_node;
2522 return build3_loc (loc, PSEUDO_DTOR_EXPR, void_type_node, object,
2523 scope, destructor);
2526 /* Finish an expression of the form CODE EXPR. */
2528 tree
2529 finish_unary_op_expr (location_t loc, enum tree_code code, tree expr,
2530 tsubst_flags_t complain)
2532 tree result = build_x_unary_op (loc, code, expr, complain);
2533 if ((complain & tf_warning)
2534 && TREE_OVERFLOW_P (result) && !TREE_OVERFLOW_P (expr))
2535 overflow_warning (input_location, result);
2537 return result;
2540 /* Finish a compound-literal expression. TYPE is the type to which
2541 the CONSTRUCTOR in COMPOUND_LITERAL is being cast. */
2543 tree
2544 finish_compound_literal (tree type, tree compound_literal,
2545 tsubst_flags_t complain)
2547 if (type == error_mark_node)
2548 return error_mark_node;
2550 if (TREE_CODE (type) == REFERENCE_TYPE)
2552 compound_literal
2553 = finish_compound_literal (TREE_TYPE (type), compound_literal,
2554 complain);
2555 return cp_build_c_cast (type, compound_literal, complain);
2558 if (!TYPE_OBJ_P (type))
2560 if (complain & tf_error)
2561 error ("compound literal of non-object type %qT", type);
2562 return error_mark_node;
2565 if (processing_template_decl)
2567 TREE_TYPE (compound_literal) = type;
2568 /* Mark the expression as a compound literal. */
2569 TREE_HAS_CONSTRUCTOR (compound_literal) = 1;
2570 return compound_literal;
2573 type = complete_type (type);
2575 if (TYPE_NON_AGGREGATE_CLASS (type))
2577 /* Trying to deal with a CONSTRUCTOR instead of a TREE_LIST
2578 everywhere that deals with function arguments would be a pain, so
2579 just wrap it in a TREE_LIST. The parser set a flag so we know
2580 that it came from T{} rather than T({}). */
2581 CONSTRUCTOR_IS_DIRECT_INIT (compound_literal) = 1;
2582 compound_literal = build_tree_list (NULL_TREE, compound_literal);
2583 return build_functional_cast (type, compound_literal, complain);
2586 if (TREE_CODE (type) == ARRAY_TYPE
2587 && check_array_initializer (NULL_TREE, type, compound_literal))
2588 return error_mark_node;
2589 compound_literal = reshape_init (type, compound_literal, complain);
2590 if (SCALAR_TYPE_P (type)
2591 && !BRACE_ENCLOSED_INITIALIZER_P (compound_literal)
2592 && (complain & tf_warning_or_error))
2593 check_narrowing (type, compound_literal);
2594 if (TREE_CODE (type) == ARRAY_TYPE
2595 && TYPE_DOMAIN (type) == NULL_TREE)
2597 cp_complete_array_type_or_error (&type, compound_literal,
2598 false, complain);
2599 if (type == error_mark_node)
2600 return error_mark_node;
2602 compound_literal = digest_init (type, compound_literal, complain);
2603 if (TREE_CODE (compound_literal) == CONSTRUCTOR)
2604 TREE_HAS_CONSTRUCTOR (compound_literal) = true;
2605 /* Put static/constant array temporaries in static variables, but always
2606 represent class temporaries with TARGET_EXPR so we elide copies. */
2607 if ((!at_function_scope_p () || CP_TYPE_CONST_P (type))
2608 && TREE_CODE (type) == ARRAY_TYPE
2609 && !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
2610 && initializer_constant_valid_p (compound_literal, type))
2612 tree decl = create_temporary_var (type);
2613 DECL_INITIAL (decl) = compound_literal;
2614 TREE_STATIC (decl) = 1;
2615 if (literal_type_p (type) && CP_TYPE_CONST_NON_VOLATILE_P (type))
2617 /* 5.19 says that a constant expression can include an
2618 lvalue-rvalue conversion applied to "a glvalue of literal type
2619 that refers to a non-volatile temporary object initialized
2620 with a constant expression". Rather than try to communicate
2621 that this VAR_DECL is a temporary, just mark it constexpr. */
2622 DECL_DECLARED_CONSTEXPR_P (decl) = true;
2623 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
2624 TREE_CONSTANT (decl) = true;
2626 cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
2627 decl = pushdecl_top_level (decl);
2628 DECL_NAME (decl) = make_anon_name ();
2629 SET_DECL_ASSEMBLER_NAME (decl, DECL_NAME (decl));
2630 /* Make sure the destructor is callable. */
2631 tree clean = cxx_maybe_build_cleanup (decl, complain);
2632 if (clean == error_mark_node)
2633 return error_mark_node;
2634 return decl;
2636 else
2637 return get_target_expr_sfinae (compound_literal, complain);
2640 /* Return the declaration for the function-name variable indicated by
2641 ID. */
2643 tree
2644 finish_fname (tree id)
2646 tree decl;
2648 decl = fname_decl (input_location, C_RID_CODE (id), id);
2649 if (processing_template_decl && current_function_decl
2650 && decl != error_mark_node)
2651 decl = DECL_NAME (decl);
2652 return decl;
2655 /* Finish a translation unit. */
2657 void
2658 finish_translation_unit (void)
2660 /* In case there were missing closebraces,
2661 get us back to the global binding level. */
2662 pop_everything ();
2663 while (current_namespace != global_namespace)
2664 pop_namespace ();
2666 /* Do file scope __FUNCTION__ et al. */
2667 finish_fname_decls ();
2670 /* Finish a template type parameter, specified as AGGR IDENTIFIER.
2671 Returns the parameter. */
2673 tree
2674 finish_template_type_parm (tree aggr, tree identifier)
2676 if (aggr != class_type_node)
2678 permerror (input_location, "template type parameters must use the keyword %<class%> or %<typename%>");
2679 aggr = class_type_node;
2682 return build_tree_list (aggr, identifier);
2685 /* Finish a template template parameter, specified as AGGR IDENTIFIER.
2686 Returns the parameter. */
2688 tree
2689 finish_template_template_parm (tree aggr, tree identifier)
2691 tree decl = build_lang_decl_loc (input_location,
2692 TYPE_DECL, identifier, NULL_TREE);
2694 tree tmpl = build_lang_decl (TEMPLATE_DECL, identifier, NULL_TREE);
2695 DECL_TEMPLATE_PARMS (tmpl) = current_template_parms;
2696 DECL_CONSTRAINTS (tmpl) = current_template_reqs;
2697 DECL_TEMPLATE_RESULT (tmpl) = decl;
2698 DECL_ARTIFICIAL (decl) = 1;
2700 end_template_decl ();
2702 gcc_assert (DECL_TEMPLATE_PARMS (tmpl));
2704 check_default_tmpl_args (decl, DECL_TEMPLATE_PARMS (tmpl),
2705 /*is_primary=*/true, /*is_partial=*/false,
2706 /*is_friend=*/0);
2708 return finish_template_type_parm (aggr, tmpl);
2711 /* ARGUMENT is the default-argument value for a template template
2712 parameter. If ARGUMENT is invalid, issue error messages and return
2713 the ERROR_MARK_NODE. Otherwise, ARGUMENT itself is returned. */
2715 tree
2716 check_template_template_default_arg (tree argument)
2718 if (TREE_CODE (argument) != TEMPLATE_DECL
2719 && TREE_CODE (argument) != TEMPLATE_TEMPLATE_PARM
2720 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
2722 if (TREE_CODE (argument) == TYPE_DECL)
2723 error ("invalid use of type %qT as a default value for a template "
2724 "template-parameter", TREE_TYPE (argument));
2725 else
2726 error ("invalid default argument for a template template parameter");
2727 return error_mark_node;
2730 return argument;
2733 /* Begin a class definition, as indicated by T. */
2735 tree
2736 begin_class_definition (tree t)
2738 if (error_operand_p (t) || error_operand_p (TYPE_MAIN_DECL (t)))
2739 return error_mark_node;
2741 if (processing_template_parmlist)
2743 error ("definition of %q#T inside template parameter list", t);
2744 return error_mark_node;
2747 /* According to the C++ ABI, decimal classes defined in ISO/IEC TR 24733
2748 are passed the same as decimal scalar types. */
2749 if (TREE_CODE (t) == RECORD_TYPE
2750 && !processing_template_decl)
2752 tree ns = TYPE_CONTEXT (t);
2753 if (ns && TREE_CODE (ns) == NAMESPACE_DECL
2754 && DECL_CONTEXT (ns) == std_node
2755 && DECL_NAME (ns)
2756 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (ns)), "decimal"))
2758 const char *n = TYPE_NAME_STRING (t);
2759 if ((strcmp (n, "decimal32") == 0)
2760 || (strcmp (n, "decimal64") == 0)
2761 || (strcmp (n, "decimal128") == 0))
2762 TYPE_TRANSPARENT_AGGR (t) = 1;
2766 /* A non-implicit typename comes from code like:
2768 template <typename T> struct A {
2769 template <typename U> struct A<T>::B ...
2771 This is erroneous. */
2772 else if (TREE_CODE (t) == TYPENAME_TYPE)
2774 error ("invalid definition of qualified type %qT", t);
2775 t = error_mark_node;
2778 if (t == error_mark_node || ! MAYBE_CLASS_TYPE_P (t))
2780 t = make_class_type (RECORD_TYPE);
2781 pushtag (make_anon_name (), t, /*tag_scope=*/ts_current);
2784 if (TYPE_BEING_DEFINED (t))
2786 t = make_class_type (TREE_CODE (t));
2787 pushtag (TYPE_IDENTIFIER (t), t, /*tag_scope=*/ts_current);
2789 maybe_process_partial_specialization (t);
2790 pushclass (t);
2791 TYPE_BEING_DEFINED (t) = 1;
2792 class_binding_level->defining_class_p = 1;
2794 if (flag_pack_struct)
2796 tree v;
2797 TYPE_PACKED (t) = 1;
2798 /* Even though the type is being defined for the first time
2799 here, there might have been a forward declaration, so there
2800 might be cv-qualified variants of T. */
2801 for (v = TYPE_NEXT_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v))
2802 TYPE_PACKED (v) = 1;
2804 /* Reset the interface data, at the earliest possible
2805 moment, as it might have been set via a class foo;
2806 before. */
2807 if (! TYPE_ANONYMOUS_P (t))
2809 struct c_fileinfo *finfo = \
2810 get_fileinfo (LOCATION_FILE (input_location));
2811 CLASSTYPE_INTERFACE_ONLY (t) = finfo->interface_only;
2812 SET_CLASSTYPE_INTERFACE_UNKNOWN_X
2813 (t, finfo->interface_unknown);
2815 reset_specialization();
2817 /* Make a declaration for this class in its own scope. */
2818 build_self_reference ();
2820 return t;
2823 /* Finish the member declaration given by DECL. */
2825 void
2826 finish_member_declaration (tree decl)
2828 if (decl == error_mark_node || decl == NULL_TREE)
2829 return;
2831 if (decl == void_type_node)
2832 /* The COMPONENT was a friend, not a member, and so there's
2833 nothing for us to do. */
2834 return;
2836 /* We should see only one DECL at a time. */
2837 gcc_assert (DECL_CHAIN (decl) == NULL_TREE);
2839 /* Set up access control for DECL. */
2840 TREE_PRIVATE (decl)
2841 = (current_access_specifier == access_private_node);
2842 TREE_PROTECTED (decl)
2843 = (current_access_specifier == access_protected_node);
2844 if (TREE_CODE (decl) == TEMPLATE_DECL)
2846 TREE_PRIVATE (DECL_TEMPLATE_RESULT (decl)) = TREE_PRIVATE (decl);
2847 TREE_PROTECTED (DECL_TEMPLATE_RESULT (decl)) = TREE_PROTECTED (decl);
2850 /* Mark the DECL as a member of the current class, unless it's
2851 a member of an enumeration. */
2852 if (TREE_CODE (decl) != CONST_DECL)
2853 DECL_CONTEXT (decl) = current_class_type;
2855 /* Check for bare parameter packs in the member variable declaration. */
2856 if (TREE_CODE (decl) == FIELD_DECL)
2858 if (check_for_bare_parameter_packs (TREE_TYPE (decl)))
2859 TREE_TYPE (decl) = error_mark_node;
2860 if (check_for_bare_parameter_packs (DECL_ATTRIBUTES (decl)))
2861 DECL_ATTRIBUTES (decl) = NULL_TREE;
2864 /* [dcl.link]
2866 A C language linkage is ignored for the names of class members
2867 and the member function type of class member functions. */
2868 if (DECL_LANG_SPECIFIC (decl) && DECL_LANGUAGE (decl) == lang_c)
2869 SET_DECL_LANGUAGE (decl, lang_cplusplus);
2871 /* Put functions on the TYPE_METHODS list and everything else on the
2872 TYPE_FIELDS list. Note that these are built up in reverse order.
2873 We reverse them (to obtain declaration order) in finish_struct. */
2874 if (DECL_DECLARES_FUNCTION_P (decl))
2876 /* We also need to add this function to the
2877 CLASSTYPE_METHOD_VEC. */
2878 if (add_method (current_class_type, decl, NULL_TREE))
2880 DECL_CHAIN (decl) = TYPE_METHODS (current_class_type);
2881 TYPE_METHODS (current_class_type) = decl;
2883 maybe_add_class_template_decl_list (current_class_type, decl,
2884 /*friend_p=*/0);
2887 /* Enter the DECL into the scope of the class, if the class
2888 isn't a closure (whose fields are supposed to be unnamed). */
2889 else if (CLASSTYPE_LAMBDA_EXPR (current_class_type)
2890 || pushdecl_class_level (decl))
2892 if (TREE_CODE (decl) == USING_DECL)
2894 /* For now, ignore class-scope USING_DECLS, so that
2895 debugging backends do not see them. */
2896 DECL_IGNORED_P (decl) = 1;
2899 /* All TYPE_DECLs go at the end of TYPE_FIELDS. Ordinary fields
2900 go at the beginning. The reason is that lookup_field_1
2901 searches the list in order, and we want a field name to
2902 override a type name so that the "struct stat hack" will
2903 work. In particular:
2905 struct S { enum E { }; int E } s;
2906 s.E = 3;
2908 is valid. In addition, the FIELD_DECLs must be maintained in
2909 declaration order so that class layout works as expected.
2910 However, we don't need that order until class layout, so we
2911 save a little time by putting FIELD_DECLs on in reverse order
2912 here, and then reversing them in finish_struct_1. (We could
2913 also keep a pointer to the correct insertion points in the
2914 list.) */
2916 if (TREE_CODE (decl) == TYPE_DECL)
2917 TYPE_FIELDS (current_class_type)
2918 = chainon (TYPE_FIELDS (current_class_type), decl);
2919 else
2921 DECL_CHAIN (decl) = TYPE_FIELDS (current_class_type);
2922 TYPE_FIELDS (current_class_type) = decl;
2925 maybe_add_class_template_decl_list (current_class_type, decl,
2926 /*friend_p=*/0);
2929 if (pch_file)
2930 note_decl_for_pch (decl);
2933 /* DECL has been declared while we are building a PCH file. Perform
2934 actions that we might normally undertake lazily, but which can be
2935 performed now so that they do not have to be performed in
2936 translation units which include the PCH file. */
2938 void
2939 note_decl_for_pch (tree decl)
2941 gcc_assert (pch_file);
2943 /* There's a good chance that we'll have to mangle names at some
2944 point, even if only for emission in debugging information. */
2945 if (VAR_OR_FUNCTION_DECL_P (decl)
2946 && !processing_template_decl)
2947 mangle_decl (decl);
2950 /* Finish processing a complete template declaration. The PARMS are
2951 the template parameters. */
2953 void
2954 finish_template_decl (tree parms)
2956 if (parms)
2957 end_template_decl ();
2958 else
2959 end_specialization ();
2962 // Returns the template type of the class scope being entered. If we're
2963 // entering a constrained class scope. TYPE is the class template
2964 // scope being entered and we may need to match the intended type with
2965 // a constrained specialization. For example:
2967 // template<Object T>
2968 // struct S { void f(); }; #1
2970 // template<Object T>
2971 // void S<T>::f() { } #2
2973 // We check, in #2, that S<T> refers precisely to the type declared by
2974 // #1 (i.e., that the constraints match). Note that the following should
2975 // be an error since there is no specialization of S<T> that is
2976 // unconstrained, but this is not diagnosed here.
2978 // template<typename T>
2979 // void S<T>::f() { }
2981 // We cannot diagnose this problem here since this function also matches
2982 // qualified template names that are not part of a definition. For example:
2984 // template<Integral T, Floating_point U>
2985 // typename pair<T, U>::first_type void f(T, U);
2987 // Here, it is unlikely that there is a partial specialization of
2988 // pair constrained for for Integral and Floating_point arguments.
2990 // The general rule is: if a constrained specialization with matching
2991 // constraints is found return that type. Alos note that if TYPE is not a
2992 // class-type (e.g. a typename type), then no fixup is needed.
2993 static tree
2994 fixup_template_type (tree type)
2996 // Don't try to fix non-class types.
2997 if (!CLASS_TYPE_P (type))
2998 return type;
3000 // Find the template parameter list at the a depth appropriate to
3001 // the scope we're trying to enter.
3002 tree parms = current_template_parms;
3003 int depth = template_class_depth (type);
3004 for (int n = processing_template_decl; n > depth && parms; --n)
3005 parms = TREE_CHAIN (parms);
3006 if (!parms)
3007 return type;
3008 tree cur_constr = TEMPLATE_PARMS_CONSTRAINTS (parms);
3010 // Search for a specialization whose type and constraints match.
3011 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
3012 tree specs = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
3013 while (specs)
3015 tree spec_constr = DECL_CONSTRAINTS (TREE_VALUE (specs));
3017 // If the type and constraints match a specialization, then we
3018 // are entering that type. Note that the type comparison is
3019 // structural since constrained partial specialiations may
3020 // have different canonical types for the same type patterns.
3021 if (comptypes (type, TREE_TYPE (specs), COMPARE_STRUCTURAL)
3022 && equivalent_constraints (cur_constr, spec_constr))
3023 return TREE_TYPE (specs);
3024 specs = TREE_CHAIN (specs);
3027 // If no specialization matches, then must return the type
3028 // previously found.
3029 return type;
3033 /* Finish processing a template-id (which names a type) of the form
3034 NAME < ARGS >. Return the TYPE_DECL for the type named by the
3035 template-id. If ENTERING_SCOPE is nonzero we are about to enter
3036 the scope of template-id indicated. */
3038 tree
3039 finish_template_type (tree name, tree args, int entering_scope)
3041 tree type;
3043 type = lookup_template_class (name, args,
3044 NULL_TREE, NULL_TREE, entering_scope,
3045 tf_warning_or_error | tf_user);
3047 // If entering a scope, correct the lookup to account for constraints.
3048 if (entering_scope)
3049 type = fixup_template_type (type);
3051 if (type == error_mark_node)
3052 return type;
3053 else if (CLASS_TYPE_P (type) && !alias_type_or_template_p (type))
3054 return TYPE_STUB_DECL (type);
3055 else
3056 return TYPE_NAME (type);
3059 /* Finish processing a BASE_CLASS with the indicated ACCESS_SPECIFIER.
3060 Return a TREE_LIST containing the ACCESS_SPECIFIER and the
3061 BASE_CLASS, or NULL_TREE if an error occurred. The
3062 ACCESS_SPECIFIER is one of
3063 access_{default,public,protected_private}_node. For a virtual base
3064 we set TREE_TYPE. */
3066 tree
3067 finish_base_specifier (tree base, tree access, bool virtual_p)
3069 tree result;
3071 if (base == error_mark_node)
3073 error ("invalid base-class specification");
3074 result = NULL_TREE;
3076 else if (! MAYBE_CLASS_TYPE_P (base))
3078 error ("%qT is not a class type", base);
3079 result = NULL_TREE;
3081 else
3083 if (cp_type_quals (base) != 0)
3085 /* DR 484: Can a base-specifier name a cv-qualified
3086 class type? */
3087 base = TYPE_MAIN_VARIANT (base);
3089 result = build_tree_list (access, base);
3090 if (virtual_p)
3091 TREE_TYPE (result) = integer_type_node;
3094 return result;
3097 /* If FNS is a member function, a set of member functions, or a
3098 template-id referring to one or more member functions, return a
3099 BASELINK for FNS, incorporating the current access context.
3100 Otherwise, return FNS unchanged. */
3102 tree
3103 baselink_for_fns (tree fns)
3105 tree scope;
3106 tree cl;
3108 if (BASELINK_P (fns)
3109 || error_operand_p (fns))
3110 return fns;
3112 scope = ovl_scope (fns);
3113 if (!CLASS_TYPE_P (scope))
3114 return fns;
3116 cl = currently_open_derived_class (scope);
3117 if (!cl)
3118 cl = scope;
3119 cl = TYPE_BINFO (cl);
3120 return build_baselink (cl, cl, fns, /*optype=*/NULL_TREE);
3123 /* Returns true iff DECL is a variable from a function outside
3124 the current one. */
3126 static bool
3127 outer_var_p (tree decl)
3129 return ((VAR_P (decl) || TREE_CODE (decl) == PARM_DECL)
3130 && DECL_FUNCTION_SCOPE_P (decl)
3131 && (DECL_CONTEXT (decl) != current_function_decl
3132 || parsing_nsdmi ()));
3135 /* As above, but also checks that DECL is automatic. */
3137 static bool
3138 outer_automatic_var_p (tree decl)
3140 return (outer_var_p (decl)
3141 && !TREE_STATIC (decl));
3144 /* ID_EXPRESSION is a representation of parsed, but unprocessed,
3145 id-expression. (See cp_parser_id_expression for details.) SCOPE,
3146 if non-NULL, is the type or namespace used to explicitly qualify
3147 ID_EXPRESSION. DECL is the entity to which that name has been
3148 resolved.
3150 *CONSTANT_EXPRESSION_P is true if we are presently parsing a
3151 constant-expression. In that case, *NON_CONSTANT_EXPRESSION_P will
3152 be set to true if this expression isn't permitted in a
3153 constant-expression, but it is otherwise not set by this function.
3154 *ALLOW_NON_CONSTANT_EXPRESSION_P is true if we are parsing a
3155 constant-expression, but a non-constant expression is also
3156 permissible.
3158 DONE is true if this expression is a complete postfix-expression;
3159 it is false if this expression is followed by '->', '[', '(', etc.
3160 ADDRESS_P is true iff this expression is the operand of '&'.
3161 TEMPLATE_P is true iff the qualified-id was of the form
3162 "A::template B". TEMPLATE_ARG_P is true iff this qualified name
3163 appears as a template argument.
3165 If an error occurs, and it is the kind of error that might cause
3166 the parser to abort a tentative parse, *ERROR_MSG is filled in. It
3167 is the caller's responsibility to issue the message. *ERROR_MSG
3168 will be a string with static storage duration, so the caller need
3169 not "free" it.
3171 Return an expression for the entity, after issuing appropriate
3172 diagnostics. This function is also responsible for transforming a
3173 reference to a non-static member into a COMPONENT_REF that makes
3174 the use of "this" explicit.
3176 Upon return, *IDK will be filled in appropriately. */
3177 tree
3178 finish_id_expression (tree id_expression,
3179 tree decl,
3180 tree scope,
3181 cp_id_kind *idk,
3182 bool integral_constant_expression_p,
3183 bool allow_non_integral_constant_expression_p,
3184 bool *non_integral_constant_expression_p,
3185 bool template_p,
3186 bool done,
3187 bool address_p,
3188 bool template_arg_p,
3189 const char **error_msg,
3190 location_t location)
3192 decl = strip_using_decl (decl);
3194 /* Initialize the output parameters. */
3195 *idk = CP_ID_KIND_NONE;
3196 *error_msg = NULL;
3198 if (id_expression == error_mark_node)
3199 return error_mark_node;
3200 /* If we have a template-id, then no further lookup is
3201 required. If the template-id was for a template-class, we
3202 will sometimes have a TYPE_DECL at this point. */
3203 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3204 || TREE_CODE (decl) == TYPE_DECL)
3206 /* Look up the name. */
3207 else
3209 if (decl == error_mark_node)
3211 /* Name lookup failed. */
3212 if (scope
3213 && (!TYPE_P (scope)
3214 || (!dependent_type_p (scope)
3215 && !(identifier_p (id_expression)
3216 && IDENTIFIER_TYPENAME_P (id_expression)
3217 && dependent_type_p (TREE_TYPE (id_expression))))))
3219 /* If the qualifying type is non-dependent (and the name
3220 does not name a conversion operator to a dependent
3221 type), issue an error. */
3222 qualified_name_lookup_error (scope, id_expression, decl, location);
3223 return error_mark_node;
3225 else if (!scope)
3227 /* It may be resolved via Koenig lookup. */
3228 *idk = CP_ID_KIND_UNQUALIFIED;
3229 return id_expression;
3231 else
3232 decl = id_expression;
3234 /* If DECL is a variable that would be out of scope under
3235 ANSI/ISO rules, but in scope in the ARM, name lookup
3236 will succeed. Issue a diagnostic here. */
3237 else
3238 decl = check_for_out_of_scope_variable (decl);
3240 /* Remember that the name was used in the definition of
3241 the current class so that we can check later to see if
3242 the meaning would have been different after the class
3243 was entirely defined. */
3244 if (!scope && decl != error_mark_node && identifier_p (id_expression))
3245 maybe_note_name_used_in_class (id_expression, decl);
3247 /* Disallow uses of local variables from containing functions, except
3248 within lambda-expressions. */
3249 if (!outer_var_p (decl))
3250 /* OK */;
3251 else if (TREE_STATIC (decl)
3252 /* It's not a use (3.2) if we're in an unevaluated context. */
3253 || cp_unevaluated_operand)
3254 /* OK */;
3255 else
3257 tree context = DECL_CONTEXT (decl);
3258 tree containing_function = current_function_decl;
3259 tree lambda_stack = NULL_TREE;
3260 tree lambda_expr = NULL_TREE;
3261 tree initializer = convert_from_reference (decl);
3263 /* Mark it as used now even if the use is ill-formed. */
3264 mark_used (decl);
3266 /* Core issue 696: "[At the July 2009 meeting] the CWG expressed
3267 support for an approach in which a reference to a local
3268 [constant] automatic variable in a nested class or lambda body
3269 would enter the expression as an rvalue, which would reduce
3270 the complexity of the problem"
3272 FIXME update for final resolution of core issue 696. */
3273 if (decl_maybe_constant_var_p (decl))
3275 if (processing_template_decl)
3276 /* In a template, the constant value may not be in a usable
3277 form, so wait until instantiation time. */
3278 return decl;
3279 else if (decl_constant_var_p (decl))
3280 return integral_constant_value (decl);
3283 if (parsing_nsdmi ())
3284 containing_function = NULL_TREE;
3285 /* If we are in a lambda function, we can move out until we hit
3286 1. the context,
3287 2. a non-lambda function, or
3288 3. a non-default capturing lambda function. */
3289 else while (context != containing_function
3290 && LAMBDA_FUNCTION_P (containing_function))
3292 lambda_expr = CLASSTYPE_LAMBDA_EXPR
3293 (DECL_CONTEXT (containing_function));
3295 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr)
3296 == CPLD_NONE)
3297 break;
3299 lambda_stack = tree_cons (NULL_TREE,
3300 lambda_expr,
3301 lambda_stack);
3303 containing_function
3304 = decl_function_context (containing_function);
3307 if (lambda_expr && TREE_CODE (decl) == VAR_DECL
3308 && DECL_ANON_UNION_VAR_P (decl))
3310 error ("cannot capture member %qD of anonymous union", decl);
3311 return error_mark_node;
3313 if (context == containing_function)
3315 decl = add_default_capture (lambda_stack,
3316 /*id=*/DECL_NAME (decl),
3317 initializer);
3319 else if (lambda_expr)
3321 error ("%qD is not captured", decl);
3322 return error_mark_node;
3324 else
3326 error (VAR_P (decl)
3327 ? G_("use of local variable with automatic storage from containing function")
3328 : G_("use of parameter from containing function"));
3329 inform (input_location, "%q+#D declared here", decl);
3330 return error_mark_node;
3334 /* Also disallow uses of function parameters outside the function
3335 body, except inside an unevaluated context (i.e. decltype). */
3336 if (TREE_CODE (decl) == PARM_DECL
3337 && DECL_CONTEXT (decl) == NULL_TREE
3338 && !cp_unevaluated_operand)
3340 *error_msg = "use of parameter outside function body";
3341 return error_mark_node;
3345 /* If we didn't find anything, or what we found was a type,
3346 then this wasn't really an id-expression. */
3347 if (TREE_CODE (decl) == TEMPLATE_DECL
3348 && !DECL_FUNCTION_TEMPLATE_P (decl))
3350 *error_msg = "missing template arguments";
3351 return error_mark_node;
3353 else if (TREE_CODE (decl) == TYPE_DECL
3354 || TREE_CODE (decl) == NAMESPACE_DECL)
3356 *error_msg = "expected primary-expression";
3357 return error_mark_node;
3360 /* If the name resolved to a template parameter, there is no
3361 need to look it up again later. */
3362 if ((TREE_CODE (decl) == CONST_DECL && DECL_TEMPLATE_PARM_P (decl))
3363 || TREE_CODE (decl) == TEMPLATE_PARM_INDEX)
3365 tree r;
3367 *idk = CP_ID_KIND_NONE;
3368 if (TREE_CODE (decl) == TEMPLATE_PARM_INDEX)
3369 decl = TEMPLATE_PARM_DECL (decl);
3370 r = convert_from_reference (DECL_INITIAL (decl));
3372 if (integral_constant_expression_p
3373 && !dependent_type_p (TREE_TYPE (decl))
3374 && !(INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (r))))
3376 if (!allow_non_integral_constant_expression_p)
3377 error ("template parameter %qD of type %qT is not allowed in "
3378 "an integral constant expression because it is not of "
3379 "integral or enumeration type", decl, TREE_TYPE (decl));
3380 *non_integral_constant_expression_p = true;
3382 return r;
3384 else
3386 bool dependent_p;
3388 /* If the declaration was explicitly qualified indicate
3389 that. The semantics of `A::f(3)' are different than
3390 `f(3)' if `f' is virtual. */
3391 *idk = (scope
3392 ? CP_ID_KIND_QUALIFIED
3393 : (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3394 ? CP_ID_KIND_TEMPLATE_ID
3395 : CP_ID_KIND_UNQUALIFIED));
3398 /* [temp.dep.expr]
3400 An id-expression is type-dependent if it contains an
3401 identifier that was declared with a dependent type.
3403 The standard is not very specific about an id-expression that
3404 names a set of overloaded functions. What if some of them
3405 have dependent types and some of them do not? Presumably,
3406 such a name should be treated as a dependent name. */
3407 /* Assume the name is not dependent. */
3408 dependent_p = false;
3409 if (!processing_template_decl)
3410 /* No names are dependent outside a template. */
3412 else if (TREE_CODE (decl) == CONST_DECL)
3413 /* We don't want to treat enumerators as dependent. */
3415 /* A template-id where the name of the template was not resolved
3416 is definitely dependent. */
3417 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3418 && (identifier_p (TREE_OPERAND (decl, 0))))
3419 dependent_p = true;
3420 /* For anything except an overloaded function, just check its
3421 type. */
3422 else if (!is_overloaded_fn (decl))
3423 dependent_p
3424 = dependent_type_p (TREE_TYPE (decl));
3425 /* For a set of overloaded functions, check each of the
3426 functions. */
3427 else
3429 tree fns = decl;
3431 if (BASELINK_P (fns))
3432 fns = BASELINK_FUNCTIONS (fns);
3434 /* For a template-id, check to see if the template
3435 arguments are dependent. */
3436 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
3438 tree args = TREE_OPERAND (fns, 1);
3439 dependent_p = any_dependent_template_arguments_p (args);
3440 /* The functions are those referred to by the
3441 template-id. */
3442 fns = TREE_OPERAND (fns, 0);
3445 /* If there are no dependent template arguments, go through
3446 the overloaded functions. */
3447 while (fns && !dependent_p)
3449 tree fn = OVL_CURRENT (fns);
3451 /* Member functions of dependent classes are
3452 dependent. */
3453 if (TREE_CODE (fn) == FUNCTION_DECL
3454 && type_dependent_expression_p (fn))
3455 dependent_p = true;
3456 else if (TREE_CODE (fn) == TEMPLATE_DECL
3457 && dependent_template_p (fn))
3458 dependent_p = true;
3460 fns = OVL_NEXT (fns);
3464 /* If the name was dependent on a template parameter, we will
3465 resolve the name at instantiation time. */
3466 if (dependent_p)
3468 /* Create a SCOPE_REF for qualified names, if the scope is
3469 dependent. */
3470 if (scope)
3472 if (TYPE_P (scope))
3474 if (address_p && done)
3475 decl = finish_qualified_id_expr (scope, decl,
3476 done, address_p,
3477 template_p,
3478 template_arg_p,
3479 tf_warning_or_error);
3480 else
3482 tree type = NULL_TREE;
3483 if (DECL_P (decl) && !dependent_scope_p (scope))
3484 type = TREE_TYPE (decl);
3485 decl = build_qualified_name (type,
3486 scope,
3487 id_expression,
3488 template_p);
3491 if (TREE_TYPE (decl))
3492 decl = convert_from_reference (decl);
3493 return decl;
3495 /* A TEMPLATE_ID already contains all the information we
3496 need. */
3497 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR)
3498 return id_expression;
3499 *idk = CP_ID_KIND_UNQUALIFIED_DEPENDENT;
3500 /* If we found a variable, then name lookup during the
3501 instantiation will always resolve to the same VAR_DECL
3502 (or an instantiation thereof). */
3503 if (VAR_P (decl)
3504 || TREE_CODE (decl) == PARM_DECL)
3506 mark_used (decl);
3507 return convert_from_reference (decl);
3509 /* The same is true for FIELD_DECL, but we also need to
3510 make sure that the syntax is correct. */
3511 else if (TREE_CODE (decl) == FIELD_DECL)
3513 /* Since SCOPE is NULL here, this is an unqualified name.
3514 Access checking has been performed during name lookup
3515 already. Turn off checking to avoid duplicate errors. */
3516 push_deferring_access_checks (dk_no_check);
3517 decl = finish_non_static_data_member
3518 (decl, NULL_TREE,
3519 /*qualifying_scope=*/NULL_TREE);
3520 pop_deferring_access_checks ();
3521 return decl;
3523 return id_expression;
3526 if (TREE_CODE (decl) == NAMESPACE_DECL)
3528 error ("use of namespace %qD as expression", decl);
3529 return error_mark_node;
3531 else if (DECL_CLASS_TEMPLATE_P (decl))
3533 error ("use of class template %qT as expression", decl);
3534 return error_mark_node;
3536 else if (TREE_CODE (decl) == TREE_LIST)
3538 /* Ambiguous reference to base members. */
3539 error ("request for member %qD is ambiguous in "
3540 "multiple inheritance lattice", id_expression);
3541 print_candidates (decl);
3542 return error_mark_node;
3545 /* Mark variable-like entities as used. Functions are similarly
3546 marked either below or after overload resolution. */
3547 if ((VAR_P (decl)
3548 || TREE_CODE (decl) == PARM_DECL
3549 || TREE_CODE (decl) == CONST_DECL
3550 || TREE_CODE (decl) == RESULT_DECL)
3551 && !mark_used (decl))
3552 return error_mark_node;
3554 /* Only certain kinds of names are allowed in constant
3555 expression. Template parameters have already
3556 been handled above. */
3557 if (! error_operand_p (decl)
3558 && integral_constant_expression_p
3559 && ! decl_constant_var_p (decl)
3560 && TREE_CODE (decl) != CONST_DECL
3561 && ! builtin_valid_in_constant_expr_p (decl))
3563 if (!allow_non_integral_constant_expression_p)
3565 error ("%qD cannot appear in a constant-expression", decl);
3566 return error_mark_node;
3568 *non_integral_constant_expression_p = true;
3571 tree wrap;
3572 if (VAR_P (decl)
3573 && !cp_unevaluated_operand
3574 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl))
3575 && 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 (scope)
3584 decl = (adjust_result_of_qualified_name_lookup
3585 (decl, scope, current_nonlambda_class_type()));
3587 if (TREE_CODE (decl) == FUNCTION_DECL)
3588 mark_used (decl);
3590 if (TYPE_P (scope))
3591 decl = finish_qualified_id_expr (scope,
3592 decl,
3593 done,
3594 address_p,
3595 template_p,
3596 template_arg_p,
3597 tf_warning_or_error);
3598 else
3599 decl = convert_from_reference (decl);
3601 else if (TREE_CODE (decl) == FIELD_DECL)
3603 /* Since SCOPE is NULL here, this is an unqualified name.
3604 Access checking has been performed during name lookup
3605 already. Turn off checking to avoid duplicate errors. */
3606 push_deferring_access_checks (dk_no_check);
3607 decl = finish_non_static_data_member (decl, NULL_TREE,
3608 /*qualifying_scope=*/NULL_TREE);
3609 pop_deferring_access_checks ();
3611 else if (is_overloaded_fn (decl))
3613 tree first_fn;
3615 first_fn = get_first_fn (decl);
3616 if (TREE_CODE (first_fn) == TEMPLATE_DECL)
3617 first_fn = DECL_TEMPLATE_RESULT (first_fn);
3619 if (!really_overloaded_fn (decl)
3620 && !mark_used (first_fn))
3621 return error_mark_node;
3623 if (!template_arg_p
3624 && TREE_CODE (first_fn) == FUNCTION_DECL
3625 && DECL_FUNCTION_MEMBER_P (first_fn)
3626 && !shared_member_p (decl))
3628 /* A set of member functions. */
3629 decl = maybe_dummy_object (DECL_CONTEXT (first_fn), 0);
3630 return finish_class_member_access_expr (decl, id_expression,
3631 /*template_p=*/false,
3632 tf_warning_or_error);
3635 decl = baselink_for_fns (decl);
3637 else
3639 if (DECL_P (decl) && DECL_NONLOCAL (decl)
3640 && DECL_CLASS_SCOPE_P (decl))
3642 tree context = context_for_name_lookup (decl);
3643 if (context != current_class_type)
3645 tree path = currently_open_derived_class (context);
3646 perform_or_defer_access_check (TYPE_BINFO (path),
3647 decl, decl,
3648 tf_warning_or_error);
3652 decl = convert_from_reference (decl);
3656 /* Handle references (c++/56130). */
3657 tree t = REFERENCE_REF_P (decl) ? TREE_OPERAND (decl, 0) : decl;
3658 if (TREE_DEPRECATED (t))
3659 warn_deprecated_use (t, NULL_TREE);
3661 return decl;
3664 /* Implement the __typeof keyword: Return the type of EXPR, suitable for
3665 use as a type-specifier. */
3667 tree
3668 finish_typeof (tree expr)
3670 tree type;
3672 if (type_dependent_expression_p (expr))
3674 type = cxx_make_type (TYPEOF_TYPE);
3675 TYPEOF_TYPE_EXPR (type) = expr;
3676 SET_TYPE_STRUCTURAL_EQUALITY (type);
3678 return type;
3681 expr = mark_type_use (expr);
3683 type = unlowered_expr_type (expr);
3685 if (!type || type == unknown_type_node)
3687 error ("type of %qE is unknown", expr);
3688 return error_mark_node;
3691 return type;
3694 /* Implement the __underlying_type keyword: Return the underlying
3695 type of TYPE, suitable for use as a type-specifier. */
3697 tree
3698 finish_underlying_type (tree type)
3700 tree underlying_type;
3702 if (processing_template_decl)
3704 underlying_type = cxx_make_type (UNDERLYING_TYPE);
3705 UNDERLYING_TYPE_TYPE (underlying_type) = type;
3706 SET_TYPE_STRUCTURAL_EQUALITY (underlying_type);
3708 return underlying_type;
3711 complete_type (type);
3713 if (TREE_CODE (type) != ENUMERAL_TYPE)
3715 error ("%qT is not an enumeration type", type);
3716 return error_mark_node;
3719 underlying_type = ENUM_UNDERLYING_TYPE (type);
3721 /* Fixup necessary in this case because ENUM_UNDERLYING_TYPE
3722 includes TYPE_MIN_VALUE and TYPE_MAX_VALUE information.
3723 See finish_enum_value_list for details. */
3724 if (!ENUM_FIXED_UNDERLYING_TYPE_P (type))
3725 underlying_type
3726 = c_common_type_for_mode (TYPE_MODE (underlying_type),
3727 TYPE_UNSIGNED (underlying_type));
3729 return underlying_type;
3732 /* Implement the __direct_bases keyword: Return the direct base classes
3733 of type */
3735 tree
3736 calculate_direct_bases (tree type)
3738 vec<tree, va_gc> *vector = make_tree_vector();
3739 tree bases_vec = NULL_TREE;
3740 vec<tree, va_gc> *base_binfos;
3741 tree binfo;
3742 unsigned i;
3744 complete_type (type);
3746 if (!NON_UNION_CLASS_TYPE_P (type))
3747 return make_tree_vec (0);
3749 base_binfos = BINFO_BASE_BINFOS (TYPE_BINFO (type));
3751 /* Virtual bases are initialized first */
3752 for (i = 0; base_binfos->iterate (i, &binfo); i++)
3754 if (BINFO_VIRTUAL_P (binfo))
3756 vec_safe_push (vector, binfo);
3760 /* Now non-virtuals */
3761 for (i = 0; base_binfos->iterate (i, &binfo); i++)
3763 if (!BINFO_VIRTUAL_P (binfo))
3765 vec_safe_push (vector, binfo);
3770 bases_vec = make_tree_vec (vector->length ());
3772 for (i = 0; i < vector->length (); ++i)
3774 TREE_VEC_ELT (bases_vec, i) = BINFO_TYPE ((*vector)[i]);
3776 return bases_vec;
3779 /* Implement the __bases keyword: Return the base classes
3780 of type */
3782 /* Find morally non-virtual base classes by walking binfo hierarchy */
3783 /* Virtual base classes are handled separately in finish_bases */
3785 static tree
3786 dfs_calculate_bases_pre (tree binfo, void * /*data_*/)
3788 /* Don't walk bases of virtual bases */
3789 return BINFO_VIRTUAL_P (binfo) ? dfs_skip_bases : NULL_TREE;
3792 static tree
3793 dfs_calculate_bases_post (tree binfo, void *data_)
3795 vec<tree, va_gc> **data = ((vec<tree, va_gc> **) data_);
3796 if (!BINFO_VIRTUAL_P (binfo))
3798 vec_safe_push (*data, BINFO_TYPE (binfo));
3800 return NULL_TREE;
3803 /* Calculates the morally non-virtual base classes of a class */
3804 static vec<tree, va_gc> *
3805 calculate_bases_helper (tree type)
3807 vec<tree, va_gc> *vector = make_tree_vector();
3809 /* Now add non-virtual base classes in order of construction */
3810 dfs_walk_all (TYPE_BINFO (type),
3811 dfs_calculate_bases_pre, dfs_calculate_bases_post, &vector);
3812 return vector;
3815 tree
3816 calculate_bases (tree type)
3818 vec<tree, va_gc> *vector = make_tree_vector();
3819 tree bases_vec = NULL_TREE;
3820 unsigned i;
3821 vec<tree, va_gc> *vbases;
3822 vec<tree, va_gc> *nonvbases;
3823 tree binfo;
3825 complete_type (type);
3827 if (!NON_UNION_CLASS_TYPE_P (type))
3828 return make_tree_vec (0);
3830 /* First go through virtual base classes */
3831 for (vbases = CLASSTYPE_VBASECLASSES (type), i = 0;
3832 vec_safe_iterate (vbases, i, &binfo); i++)
3834 vec<tree, va_gc> *vbase_bases;
3835 vbase_bases = calculate_bases_helper (BINFO_TYPE (binfo));
3836 vec_safe_splice (vector, vbase_bases);
3837 release_tree_vector (vbase_bases);
3840 /* Now for the non-virtual bases */
3841 nonvbases = calculate_bases_helper (type);
3842 vec_safe_splice (vector, nonvbases);
3843 release_tree_vector (nonvbases);
3845 /* Last element is entire class, so don't copy */
3846 bases_vec = make_tree_vec (vector->length () - 1);
3848 for (i = 0; i < vector->length () - 1; ++i)
3850 TREE_VEC_ELT (bases_vec, i) = (*vector)[i];
3852 release_tree_vector (vector);
3853 return bases_vec;
3856 tree
3857 finish_bases (tree type, bool direct)
3859 tree bases = NULL_TREE;
3861 if (!processing_template_decl)
3863 /* Parameter packs can only be used in templates */
3864 error ("Parameter pack __bases only valid in template declaration");
3865 return error_mark_node;
3868 bases = cxx_make_type (BASES);
3869 BASES_TYPE (bases) = type;
3870 BASES_DIRECT (bases) = direct;
3871 SET_TYPE_STRUCTURAL_EQUALITY (bases);
3873 return bases;
3876 /* Perform C++-specific checks for __builtin_offsetof before calling
3877 fold_offsetof. */
3879 tree
3880 finish_offsetof (tree expr)
3882 if (TREE_CODE (expr) == PSEUDO_DTOR_EXPR)
3884 error ("cannot apply %<offsetof%> to destructor %<~%T%>",
3885 TREE_OPERAND (expr, 2));
3886 return error_mark_node;
3888 if (TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE
3889 || TREE_CODE (TREE_TYPE (expr)) == METHOD_TYPE
3890 || TREE_TYPE (expr) == unknown_type_node)
3892 if (INDIRECT_REF_P (expr))
3893 error ("second operand of %<offsetof%> is neither a single "
3894 "identifier nor a sequence of member accesses and "
3895 "array references");
3896 else
3898 if (TREE_CODE (expr) == COMPONENT_REF
3899 || TREE_CODE (expr) == COMPOUND_EXPR)
3900 expr = TREE_OPERAND (expr, 1);
3901 error ("cannot apply %<offsetof%> to member function %qD", expr);
3903 return error_mark_node;
3905 if (REFERENCE_REF_P (expr))
3906 expr = TREE_OPERAND (expr, 0);
3907 if (TREE_CODE (expr) == COMPONENT_REF)
3909 tree object = TREE_OPERAND (expr, 0);
3910 if (!complete_type_or_else (TREE_TYPE (object), object))
3911 return error_mark_node;
3913 return fold_offsetof (expr);
3916 /* Replace the AGGR_INIT_EXPR at *TP with an equivalent CALL_EXPR. This
3917 function is broken out from the above for the benefit of the tree-ssa
3918 project. */
3920 void
3921 simplify_aggr_init_expr (tree *tp)
3923 tree aggr_init_expr = *tp;
3925 /* Form an appropriate CALL_EXPR. */
3926 tree fn = AGGR_INIT_EXPR_FN (aggr_init_expr);
3927 tree slot = AGGR_INIT_EXPR_SLOT (aggr_init_expr);
3928 tree type = TREE_TYPE (slot);
3930 tree call_expr;
3931 enum style_t { ctor, arg, pcc } style;
3933 if (AGGR_INIT_VIA_CTOR_P (aggr_init_expr))
3934 style = ctor;
3935 #ifdef PCC_STATIC_STRUCT_RETURN
3936 else if (1)
3937 style = pcc;
3938 #endif
3939 else
3941 gcc_assert (TREE_ADDRESSABLE (type));
3942 style = arg;
3945 call_expr = build_call_array_loc (input_location,
3946 TREE_TYPE (TREE_TYPE (TREE_TYPE (fn))),
3948 aggr_init_expr_nargs (aggr_init_expr),
3949 AGGR_INIT_EXPR_ARGP (aggr_init_expr));
3950 TREE_NOTHROW (call_expr) = TREE_NOTHROW (aggr_init_expr);
3951 CALL_EXPR_LIST_INIT_P (call_expr) = CALL_EXPR_LIST_INIT_P (aggr_init_expr);
3952 tree ret = call_expr;
3954 if (style == ctor)
3956 /* Replace the first argument to the ctor with the address of the
3957 slot. */
3958 cxx_mark_addressable (slot);
3959 CALL_EXPR_ARG (call_expr, 0) =
3960 build1 (ADDR_EXPR, build_pointer_type (type), slot);
3962 else if (style == arg)
3964 /* Just mark it addressable here, and leave the rest to
3965 expand_call{,_inline}. */
3966 cxx_mark_addressable (slot);
3967 CALL_EXPR_RETURN_SLOT_OPT (call_expr) = true;
3968 ret = build2 (INIT_EXPR, TREE_TYPE (ret), slot, ret);
3970 else if (style == pcc)
3972 /* If we're using the non-reentrant PCC calling convention, then we
3973 need to copy the returned value out of the static buffer into the
3974 SLOT. */
3975 push_deferring_access_checks (dk_no_check);
3976 ret = build_aggr_init (slot, ret,
3977 DIRECT_BIND | LOOKUP_ONLYCONVERTING,
3978 tf_warning_or_error);
3979 pop_deferring_access_checks ();
3980 ret = build2 (COMPOUND_EXPR, TREE_TYPE (slot), ret, slot);
3983 if (AGGR_INIT_ZERO_FIRST (aggr_init_expr))
3985 tree init = build_zero_init (type, NULL_TREE,
3986 /*static_storage_p=*/false);
3987 init = build2 (INIT_EXPR, void_type_node, slot, init);
3988 ret = build2 (COMPOUND_EXPR, TREE_TYPE (ret), init, ret);
3991 *tp = ret;
3994 /* Emit all thunks to FN that should be emitted when FN is emitted. */
3996 void
3997 emit_associated_thunks (tree fn)
3999 /* When we use vcall offsets, we emit thunks with the virtual
4000 functions to which they thunk. The whole point of vcall offsets
4001 is so that you can know statically the entire set of thunks that
4002 will ever be needed for a given virtual function, thereby
4003 enabling you to output all the thunks with the function itself. */
4004 if (DECL_VIRTUAL_P (fn)
4005 /* Do not emit thunks for extern template instantiations. */
4006 && ! DECL_REALLY_EXTERN (fn))
4008 tree thunk;
4010 for (thunk = DECL_THUNKS (fn); thunk; thunk = DECL_CHAIN (thunk))
4012 if (!THUNK_ALIAS (thunk))
4014 use_thunk (thunk, /*emit_p=*/1);
4015 if (DECL_RESULT_THUNK_P (thunk))
4017 tree probe;
4019 for (probe = DECL_THUNKS (thunk);
4020 probe; probe = DECL_CHAIN (probe))
4021 use_thunk (probe, /*emit_p=*/1);
4024 else
4025 gcc_assert (!DECL_THUNKS (thunk));
4030 /* Returns true iff FUN is an instantiation of a constexpr function
4031 template. */
4033 static inline bool
4034 is_instantiation_of_constexpr (tree fun)
4036 return (DECL_TEMPLOID_INSTANTIATION (fun)
4037 && DECL_DECLARED_CONSTEXPR_P (DECL_TI_TEMPLATE (fun)));
4040 /* Generate RTL for FN. */
4042 bool
4043 expand_or_defer_fn_1 (tree fn)
4045 /* When the parser calls us after finishing the body of a template
4046 function, we don't really want to expand the body. */
4047 if (processing_template_decl)
4049 /* Normally, collection only occurs in rest_of_compilation. So,
4050 if we don't collect here, we never collect junk generated
4051 during the processing of templates until we hit a
4052 non-template function. It's not safe to do this inside a
4053 nested class, though, as the parser may have local state that
4054 is not a GC root. */
4055 if (!function_depth)
4056 ggc_collect ();
4057 return false;
4060 gcc_assert (DECL_SAVED_TREE (fn));
4062 /* We make a decision about linkage for these functions at the end
4063 of the compilation. Until that point, we do not want the back
4064 end to output them -- but we do want it to see the bodies of
4065 these functions so that it can inline them as appropriate. */
4066 if (DECL_DECLARED_INLINE_P (fn) || DECL_IMPLICIT_INSTANTIATION (fn))
4068 if (DECL_INTERFACE_KNOWN (fn))
4069 /* We've already made a decision as to how this function will
4070 be handled. */;
4071 else if (!at_eof)
4072 tentative_decl_linkage (fn);
4073 else
4074 import_export_decl (fn);
4076 /* If the user wants us to keep all inline functions, then mark
4077 this function as needed so that finish_file will make sure to
4078 output it later. Similarly, all dllexport'd functions must
4079 be emitted; there may be callers in other DLLs. */
4080 if ((flag_keep_inline_functions
4081 && DECL_DECLARED_INLINE_P (fn)
4082 && !DECL_REALLY_EXTERN (fn))
4083 || (flag_keep_inline_dllexport
4084 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (fn))))
4086 mark_needed (fn);
4087 DECL_EXTERNAL (fn) = 0;
4091 /* If this is a constructor or destructor body, we have to clone
4092 it. */
4093 if (maybe_clone_body (fn))
4095 /* We don't want to process FN again, so pretend we've written
4096 it out, even though we haven't. */
4097 TREE_ASM_WRITTEN (fn) = 1;
4098 /* If this is an instantiation of a constexpr function, keep
4099 DECL_SAVED_TREE for explain_invalid_constexpr_fn. */
4100 if (!is_instantiation_of_constexpr (fn))
4101 DECL_SAVED_TREE (fn) = NULL_TREE;
4102 return false;
4105 /* There's no reason to do any of the work here if we're only doing
4106 semantic analysis; this code just generates RTL. */
4107 if (flag_syntax_only)
4108 return false;
4110 return true;
4113 void
4114 expand_or_defer_fn (tree fn)
4116 if (expand_or_defer_fn_1 (fn))
4118 function_depth++;
4120 /* Expand or defer, at the whim of the compilation unit manager. */
4121 cgraph_finalize_function (fn, function_depth > 1);
4122 emit_associated_thunks (fn);
4124 function_depth--;
4128 struct nrv_data
4130 nrv_data () : visited (37) {}
4132 tree var;
4133 tree result;
4134 hash_table<pointer_hash <tree_node> > visited;
4137 /* Helper function for walk_tree, used by finalize_nrv below. */
4139 static tree
4140 finalize_nrv_r (tree* tp, int* walk_subtrees, void* data)
4142 struct nrv_data *dp = (struct nrv_data *)data;
4143 tree_node **slot;
4145 /* No need to walk into types. There wouldn't be any need to walk into
4146 non-statements, except that we have to consider STMT_EXPRs. */
4147 if (TYPE_P (*tp))
4148 *walk_subtrees = 0;
4149 /* Change all returns to just refer to the RESULT_DECL; this is a nop,
4150 but differs from using NULL_TREE in that it indicates that we care
4151 about the value of the RESULT_DECL. */
4152 else if (TREE_CODE (*tp) == RETURN_EXPR)
4153 TREE_OPERAND (*tp, 0) = dp->result;
4154 /* Change all cleanups for the NRV to only run when an exception is
4155 thrown. */
4156 else if (TREE_CODE (*tp) == CLEANUP_STMT
4157 && CLEANUP_DECL (*tp) == dp->var)
4158 CLEANUP_EH_ONLY (*tp) = 1;
4159 /* Replace the DECL_EXPR for the NRV with an initialization of the
4160 RESULT_DECL, if needed. */
4161 else if (TREE_CODE (*tp) == DECL_EXPR
4162 && DECL_EXPR_DECL (*tp) == dp->var)
4164 tree init;
4165 if (DECL_INITIAL (dp->var)
4166 && DECL_INITIAL (dp->var) != error_mark_node)
4167 init = build2 (INIT_EXPR, void_type_node, dp->result,
4168 DECL_INITIAL (dp->var));
4169 else
4170 init = build_empty_stmt (EXPR_LOCATION (*tp));
4171 DECL_INITIAL (dp->var) = NULL_TREE;
4172 SET_EXPR_LOCATION (init, EXPR_LOCATION (*tp));
4173 *tp = init;
4175 /* And replace all uses of the NRV with the RESULT_DECL. */
4176 else if (*tp == dp->var)
4177 *tp = dp->result;
4179 /* Avoid walking into the same tree more than once. Unfortunately, we
4180 can't just use walk_tree_without duplicates because it would only call
4181 us for the first occurrence of dp->var in the function body. */
4182 slot = dp->visited.find_slot (*tp, INSERT);
4183 if (*slot)
4184 *walk_subtrees = 0;
4185 else
4186 *slot = *tp;
4188 /* Keep iterating. */
4189 return NULL_TREE;
4192 /* Called from finish_function to implement the named return value
4193 optimization by overriding all the RETURN_EXPRs and pertinent
4194 CLEANUP_STMTs and replacing all occurrences of VAR with RESULT, the
4195 RESULT_DECL for the function. */
4197 void
4198 finalize_nrv (tree *tp, tree var, tree result)
4200 struct nrv_data data;
4202 /* Copy name from VAR to RESULT. */
4203 DECL_NAME (result) = DECL_NAME (var);
4204 /* Don't forget that we take its address. */
4205 TREE_ADDRESSABLE (result) = TREE_ADDRESSABLE (var);
4206 /* Finally set DECL_VALUE_EXPR to avoid assigning
4207 a stack slot at -O0 for the original var and debug info
4208 uses RESULT location for VAR. */
4209 SET_DECL_VALUE_EXPR (var, result);
4210 DECL_HAS_VALUE_EXPR_P (var) = 1;
4212 data.var = var;
4213 data.result = result;
4214 cp_walk_tree (tp, finalize_nrv_r, &data, 0);
4217 /* Create CP_OMP_CLAUSE_INFO for clause C. Returns true if it is invalid. */
4219 bool
4220 cxx_omp_create_clause_info (tree c, tree type, bool need_default_ctor,
4221 bool need_copy_ctor, bool need_copy_assignment,
4222 bool need_dtor)
4224 int save_errorcount = errorcount;
4225 tree info, t;
4227 /* Always allocate 3 elements for simplicity. These are the
4228 function decls for the ctor, dtor, and assignment op.
4229 This layout is known to the three lang hooks,
4230 cxx_omp_clause_default_init, cxx_omp_clause_copy_init,
4231 and cxx_omp_clause_assign_op. */
4232 info = make_tree_vec (3);
4233 CP_OMP_CLAUSE_INFO (c) = info;
4235 if (need_default_ctor || need_copy_ctor)
4237 if (need_default_ctor)
4238 t = get_default_ctor (type);
4239 else
4240 t = get_copy_ctor (type, tf_warning_or_error);
4242 if (t && !trivial_fn_p (t))
4243 TREE_VEC_ELT (info, 0) = t;
4246 if (need_dtor && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
4247 TREE_VEC_ELT (info, 1) = get_dtor (type, tf_warning_or_error);
4249 if (need_copy_assignment)
4251 t = get_copy_assign (type);
4253 if (t && !trivial_fn_p (t))
4254 TREE_VEC_ELT (info, 2) = t;
4257 return errorcount != save_errorcount;
4260 /* Helper function for handle_omp_array_sections. Called recursively
4261 to handle multiple array-section-subscripts. C is the clause,
4262 T current expression (initially OMP_CLAUSE_DECL), which is either
4263 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
4264 expression if specified, TREE_VALUE length expression if specified,
4265 TREE_CHAIN is what it has been specified after, or some decl.
4266 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
4267 set to true if any of the array-section-subscript could have length
4268 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
4269 first array-section-subscript which is known not to have length
4270 of one. Given say:
4271 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
4272 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
4273 all are or may have length of 1, array-section-subscript [:2] is the
4274 first one knonwn not to have length 1. For array-section-subscript
4275 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
4276 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
4277 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
4278 case though, as some lengths could be zero. */
4280 static tree
4281 handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
4282 bool &maybe_zero_len, unsigned int &first_non_one)
4284 tree ret, low_bound, length, type;
4285 if (TREE_CODE (t) != TREE_LIST)
4287 if (error_operand_p (t))
4288 return error_mark_node;
4289 if (type_dependent_expression_p (t))
4290 return NULL_TREE;
4291 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
4293 if (processing_template_decl)
4294 return NULL_TREE;
4295 if (DECL_P (t))
4296 error_at (OMP_CLAUSE_LOCATION (c),
4297 "%qD is not a variable in %qs clause", t,
4298 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4299 else
4300 error_at (OMP_CLAUSE_LOCATION (c),
4301 "%qE is not a variable in %qs clause", t,
4302 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4303 return error_mark_node;
4305 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
4306 && TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
4308 error_at (OMP_CLAUSE_LOCATION (c),
4309 "%qD is threadprivate variable in %qs clause", t,
4310 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4311 return error_mark_node;
4313 t = convert_from_reference (t);
4314 return t;
4317 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
4318 maybe_zero_len, first_non_one);
4319 if (ret == error_mark_node || ret == NULL_TREE)
4320 return ret;
4322 type = TREE_TYPE (ret);
4323 low_bound = TREE_PURPOSE (t);
4324 length = TREE_VALUE (t);
4325 if ((low_bound && type_dependent_expression_p (low_bound))
4326 || (length && type_dependent_expression_p (length)))
4327 return NULL_TREE;
4329 if (low_bound == error_mark_node || length == error_mark_node)
4330 return error_mark_node;
4332 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
4334 error_at (OMP_CLAUSE_LOCATION (c),
4335 "low bound %qE of array section does not have integral type",
4336 low_bound);
4337 return error_mark_node;
4339 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
4341 error_at (OMP_CLAUSE_LOCATION (c),
4342 "length %qE of array section does not have integral type",
4343 length);
4344 return error_mark_node;
4346 if (low_bound
4347 && TREE_CODE (low_bound) == INTEGER_CST
4348 && TYPE_PRECISION (TREE_TYPE (low_bound))
4349 > TYPE_PRECISION (sizetype))
4350 low_bound = fold_convert (sizetype, low_bound);
4351 if (length
4352 && TREE_CODE (length) == INTEGER_CST
4353 && TYPE_PRECISION (TREE_TYPE (length))
4354 > TYPE_PRECISION (sizetype))
4355 length = fold_convert (sizetype, length);
4356 if (low_bound == NULL_TREE)
4357 low_bound = integer_zero_node;
4359 if (length != NULL_TREE)
4361 if (!integer_nonzerop (length))
4362 maybe_zero_len = true;
4363 if (first_non_one == types.length ()
4364 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
4365 first_non_one++;
4367 if (TREE_CODE (type) == ARRAY_TYPE)
4369 if (length == NULL_TREE
4370 && (TYPE_DOMAIN (type) == NULL_TREE
4371 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
4373 error_at (OMP_CLAUSE_LOCATION (c),
4374 "for unknown bound array type length expression must "
4375 "be specified");
4376 return error_mark_node;
4378 if (TREE_CODE (low_bound) == INTEGER_CST
4379 && tree_int_cst_sgn (low_bound) == -1)
4381 error_at (OMP_CLAUSE_LOCATION (c),
4382 "negative low bound in array section in %qs clause",
4383 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4384 return error_mark_node;
4386 if (length != NULL_TREE
4387 && TREE_CODE (length) == INTEGER_CST
4388 && tree_int_cst_sgn (length) == -1)
4390 error_at (OMP_CLAUSE_LOCATION (c),
4391 "negative length in array section in %qs clause",
4392 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4393 return error_mark_node;
4395 if (TYPE_DOMAIN (type)
4396 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
4397 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
4398 == INTEGER_CST)
4400 tree size = size_binop (PLUS_EXPR,
4401 TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
4402 size_one_node);
4403 if (TREE_CODE (low_bound) == INTEGER_CST)
4405 if (tree_int_cst_lt (size, low_bound))
4407 error_at (OMP_CLAUSE_LOCATION (c),
4408 "low bound %qE above array section size "
4409 "in %qs clause", low_bound,
4410 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4411 return error_mark_node;
4413 if (tree_int_cst_equal (size, low_bound))
4414 maybe_zero_len = true;
4415 else if (length == NULL_TREE
4416 && first_non_one == types.length ()
4417 && tree_int_cst_equal
4418 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
4419 low_bound))
4420 first_non_one++;
4422 else if (length == NULL_TREE)
4424 maybe_zero_len = true;
4425 if (first_non_one == types.length ())
4426 first_non_one++;
4428 if (length && TREE_CODE (length) == INTEGER_CST)
4430 if (tree_int_cst_lt (size, length))
4432 error_at (OMP_CLAUSE_LOCATION (c),
4433 "length %qE above array section size "
4434 "in %qs clause", length,
4435 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4436 return error_mark_node;
4438 if (TREE_CODE (low_bound) == INTEGER_CST)
4440 tree lbpluslen
4441 = size_binop (PLUS_EXPR,
4442 fold_convert (sizetype, low_bound),
4443 fold_convert (sizetype, length));
4444 if (TREE_CODE (lbpluslen) == INTEGER_CST
4445 && tree_int_cst_lt (size, lbpluslen))
4447 error_at (OMP_CLAUSE_LOCATION (c),
4448 "high bound %qE above array section size "
4449 "in %qs clause", lbpluslen,
4450 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4451 return error_mark_node;
4456 else if (length == NULL_TREE)
4458 maybe_zero_len = true;
4459 if (first_non_one == types.length ())
4460 first_non_one++;
4463 /* For [lb:] we will need to evaluate lb more than once. */
4464 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
4466 tree lb = cp_save_expr (low_bound);
4467 if (lb != low_bound)
4469 TREE_PURPOSE (t) = lb;
4470 low_bound = lb;
4474 else if (TREE_CODE (type) == POINTER_TYPE)
4476 if (length == NULL_TREE)
4478 error_at (OMP_CLAUSE_LOCATION (c),
4479 "for pointer type length expression must be specified");
4480 return error_mark_node;
4482 /* If there is a pointer type anywhere but in the very first
4483 array-section-subscript, the array section can't be contiguous. */
4484 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
4485 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
4487 error_at (OMP_CLAUSE_LOCATION (c),
4488 "array section is not contiguous in %qs clause",
4489 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4490 return error_mark_node;
4493 else
4495 error_at (OMP_CLAUSE_LOCATION (c),
4496 "%qE does not have pointer or array type", ret);
4497 return error_mark_node;
4499 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
4500 types.safe_push (TREE_TYPE (ret));
4501 /* We will need to evaluate lb more than once. */
4502 tree lb = cp_save_expr (low_bound);
4503 if (lb != low_bound)
4505 TREE_PURPOSE (t) = lb;
4506 low_bound = lb;
4508 ret = grok_array_decl (OMP_CLAUSE_LOCATION (c), ret, low_bound, false);
4509 return ret;
4512 /* Handle array sections for clause C. */
4514 static bool
4515 handle_omp_array_sections (tree c)
4517 bool maybe_zero_len = false;
4518 unsigned int first_non_one = 0;
4519 auto_vec<tree> types;
4520 tree first = handle_omp_array_sections_1 (c, OMP_CLAUSE_DECL (c), types,
4521 maybe_zero_len, first_non_one);
4522 if (first == error_mark_node)
4523 return true;
4524 if (first == NULL_TREE)
4525 return false;
4526 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
4528 tree t = OMP_CLAUSE_DECL (c);
4529 tree tem = NULL_TREE;
4530 if (processing_template_decl)
4531 return false;
4532 /* Need to evaluate side effects in the length expressions
4533 if any. */
4534 while (TREE_CODE (t) == TREE_LIST)
4536 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
4538 if (tem == NULL_TREE)
4539 tem = TREE_VALUE (t);
4540 else
4541 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
4542 TREE_VALUE (t), tem);
4544 t = TREE_CHAIN (t);
4546 if (tem)
4547 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
4548 OMP_CLAUSE_DECL (c) = first;
4550 else
4552 unsigned int num = types.length (), i;
4553 tree t, side_effects = NULL_TREE, size = NULL_TREE;
4554 tree condition = NULL_TREE;
4556 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
4557 maybe_zero_len = true;
4558 if (processing_template_decl && maybe_zero_len)
4559 return false;
4561 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
4562 t = TREE_CHAIN (t))
4564 tree low_bound = TREE_PURPOSE (t);
4565 tree length = TREE_VALUE (t);
4567 i--;
4568 if (low_bound
4569 && TREE_CODE (low_bound) == INTEGER_CST
4570 && TYPE_PRECISION (TREE_TYPE (low_bound))
4571 > TYPE_PRECISION (sizetype))
4572 low_bound = fold_convert (sizetype, low_bound);
4573 if (length
4574 && TREE_CODE (length) == INTEGER_CST
4575 && TYPE_PRECISION (TREE_TYPE (length))
4576 > TYPE_PRECISION (sizetype))
4577 length = fold_convert (sizetype, length);
4578 if (low_bound == NULL_TREE)
4579 low_bound = integer_zero_node;
4580 if (!maybe_zero_len && i > first_non_one)
4582 if (integer_nonzerop (low_bound))
4583 goto do_warn_noncontiguous;
4584 if (length != NULL_TREE
4585 && TREE_CODE (length) == INTEGER_CST
4586 && TYPE_DOMAIN (types[i])
4587 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
4588 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
4589 == INTEGER_CST)
4591 tree size;
4592 size = size_binop (PLUS_EXPR,
4593 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
4594 size_one_node);
4595 if (!tree_int_cst_equal (length, size))
4597 do_warn_noncontiguous:
4598 error_at (OMP_CLAUSE_LOCATION (c),
4599 "array section is not contiguous in %qs "
4600 "clause",
4601 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4602 return true;
4605 if (!processing_template_decl
4606 && length != NULL_TREE
4607 && TREE_SIDE_EFFECTS (length))
4609 if (side_effects == NULL_TREE)
4610 side_effects = length;
4611 else
4612 side_effects = build2 (COMPOUND_EXPR,
4613 TREE_TYPE (side_effects),
4614 length, side_effects);
4617 else if (processing_template_decl)
4618 continue;
4619 else
4621 tree l;
4623 if (i > first_non_one && length && integer_nonzerop (length))
4624 continue;
4625 if (length)
4626 l = fold_convert (sizetype, length);
4627 else
4629 l = size_binop (PLUS_EXPR,
4630 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
4631 size_one_node);
4632 l = size_binop (MINUS_EXPR, l,
4633 fold_convert (sizetype, low_bound));
4635 if (i > first_non_one)
4637 l = fold_build2 (NE_EXPR, boolean_type_node, l,
4638 size_zero_node);
4639 if (condition == NULL_TREE)
4640 condition = l;
4641 else
4642 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
4643 l, condition);
4645 else if (size == NULL_TREE)
4647 size = size_in_bytes (TREE_TYPE (types[i]));
4648 size = size_binop (MULT_EXPR, size, l);
4649 if (condition)
4650 size = fold_build3 (COND_EXPR, sizetype, condition,
4651 size, size_zero_node);
4653 else
4654 size = size_binop (MULT_EXPR, size, l);
4657 if (!processing_template_decl)
4659 if (side_effects)
4660 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
4661 OMP_CLAUSE_DECL (c) = first;
4662 OMP_CLAUSE_SIZE (c) = size;
4663 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
4664 return false;
4665 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
4666 OMP_CLAUSE_MAP);
4667 OMP_CLAUSE_MAP_KIND (c2) = OMP_CLAUSE_MAP_POINTER;
4668 if (!cxx_mark_addressable (t))
4669 return false;
4670 OMP_CLAUSE_DECL (c2) = t;
4671 t = build_fold_addr_expr (first);
4672 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
4673 ptrdiff_type_node, t);
4674 tree ptr = OMP_CLAUSE_DECL (c2);
4675 ptr = convert_from_reference (ptr);
4676 if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
4677 ptr = build_fold_addr_expr (ptr);
4678 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
4679 ptrdiff_type_node, t,
4680 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
4681 ptrdiff_type_node, ptr));
4682 OMP_CLAUSE_SIZE (c2) = t;
4683 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
4684 OMP_CLAUSE_CHAIN (c) = c2;
4685 ptr = OMP_CLAUSE_DECL (c2);
4686 if (TREE_CODE (TREE_TYPE (ptr)) == REFERENCE_TYPE
4687 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (ptr))))
4689 tree c3 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
4690 OMP_CLAUSE_MAP);
4691 OMP_CLAUSE_MAP_KIND (c3) = OMP_CLAUSE_MAP_POINTER;
4692 OMP_CLAUSE_DECL (c3) = ptr;
4693 OMP_CLAUSE_DECL (c2) = convert_from_reference (ptr);
4694 OMP_CLAUSE_SIZE (c3) = size_zero_node;
4695 OMP_CLAUSE_CHAIN (c3) = OMP_CLAUSE_CHAIN (c2);
4696 OMP_CLAUSE_CHAIN (c2) = c3;
4700 return false;
4703 /* Return identifier to look up for omp declare reduction. */
4705 tree
4706 omp_reduction_id (enum tree_code reduction_code, tree reduction_id, tree type)
4708 const char *p = NULL;
4709 const char *m = NULL;
4710 switch (reduction_code)
4712 case PLUS_EXPR:
4713 case MULT_EXPR:
4714 case MINUS_EXPR:
4715 case BIT_AND_EXPR:
4716 case BIT_XOR_EXPR:
4717 case BIT_IOR_EXPR:
4718 case TRUTH_ANDIF_EXPR:
4719 case TRUTH_ORIF_EXPR:
4720 reduction_id = ansi_opname (reduction_code);
4721 break;
4722 case MIN_EXPR:
4723 p = "min";
4724 break;
4725 case MAX_EXPR:
4726 p = "max";
4727 break;
4728 default:
4729 break;
4732 if (p == NULL)
4734 if (TREE_CODE (reduction_id) != IDENTIFIER_NODE)
4735 return error_mark_node;
4736 p = IDENTIFIER_POINTER (reduction_id);
4739 if (type != NULL_TREE)
4740 m = mangle_type_string (TYPE_MAIN_VARIANT (type));
4742 const char prefix[] = "omp declare reduction ";
4743 size_t lenp = sizeof (prefix);
4744 if (strncmp (p, prefix, lenp - 1) == 0)
4745 lenp = 1;
4746 size_t len = strlen (p);
4747 size_t lenm = m ? strlen (m) + 1 : 0;
4748 char *name = XALLOCAVEC (char, lenp + len + lenm);
4749 if (lenp > 1)
4750 memcpy (name, prefix, lenp - 1);
4751 memcpy (name + lenp - 1, p, len + 1);
4752 if (m)
4754 name[lenp + len - 1] = '~';
4755 memcpy (name + lenp + len, m, lenm);
4757 return get_identifier (name);
4760 /* Lookup OpenMP UDR ID for TYPE, return the corresponding artificial
4761 FUNCTION_DECL or NULL_TREE if not found. */
4763 static tree
4764 omp_reduction_lookup (location_t loc, tree id, tree type, tree *baselinkp,
4765 vec<tree> *ambiguousp)
4767 tree orig_id = id;
4768 tree baselink = NULL_TREE;
4769 if (identifier_p (id))
4771 cp_id_kind idk;
4772 bool nonint_cst_expression_p;
4773 const char *error_msg;
4774 id = omp_reduction_id (ERROR_MARK, id, type);
4775 tree decl = lookup_name (id);
4776 if (decl == NULL_TREE)
4777 decl = error_mark_node;
4778 id = finish_id_expression (id, decl, NULL_TREE, &idk, false, true,
4779 &nonint_cst_expression_p, false, true, false,
4780 false, &error_msg, loc);
4781 if (idk == CP_ID_KIND_UNQUALIFIED
4782 && identifier_p (id))
4784 vec<tree, va_gc> *args = NULL;
4785 vec_safe_push (args, build_reference_type (type));
4786 id = perform_koenig_lookup (id, args, tf_none);
4789 else if (TREE_CODE (id) == SCOPE_REF)
4790 id = lookup_qualified_name (TREE_OPERAND (id, 0),
4791 omp_reduction_id (ERROR_MARK,
4792 TREE_OPERAND (id, 1),
4793 type),
4794 false, false);
4795 tree fns = id;
4796 if (id && is_overloaded_fn (id))
4797 id = get_fns (id);
4798 for (; id; id = OVL_NEXT (id))
4800 tree fndecl = OVL_CURRENT (id);
4801 if (TREE_CODE (fndecl) == FUNCTION_DECL)
4803 tree argtype = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
4804 if (same_type_p (TREE_TYPE (argtype), type))
4805 break;
4808 if (id && BASELINK_P (fns))
4810 if (baselinkp)
4811 *baselinkp = fns;
4812 else
4813 baselink = fns;
4815 if (id == NULL_TREE && CLASS_TYPE_P (type) && TYPE_BINFO (type))
4817 vec<tree> ambiguous = vNULL;
4818 tree binfo = TYPE_BINFO (type), base_binfo, ret = NULL_TREE;
4819 unsigned int ix;
4820 if (ambiguousp == NULL)
4821 ambiguousp = &ambiguous;
4822 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
4824 id = omp_reduction_lookup (loc, orig_id, BINFO_TYPE (base_binfo),
4825 baselinkp ? baselinkp : &baselink,
4826 ambiguousp);
4827 if (id == NULL_TREE)
4828 continue;
4829 if (!ambiguousp->is_empty ())
4830 ambiguousp->safe_push (id);
4831 else if (ret != NULL_TREE)
4833 ambiguousp->safe_push (ret);
4834 ambiguousp->safe_push (id);
4835 ret = NULL_TREE;
4837 else
4838 ret = id;
4840 if (ambiguousp != &ambiguous)
4841 return ret;
4842 if (!ambiguous.is_empty ())
4844 const char *str = _("candidates are:");
4845 unsigned int idx;
4846 tree udr;
4847 error_at (loc, "user defined reduction lookup is ambiguous");
4848 FOR_EACH_VEC_ELT (ambiguous, idx, udr)
4850 inform (DECL_SOURCE_LOCATION (udr), "%s %#D", str, udr);
4851 if (idx == 0)
4852 str = get_spaces (str);
4854 ambiguous.release ();
4855 ret = error_mark_node;
4856 baselink = NULL_TREE;
4858 id = ret;
4860 if (id && baselink)
4861 perform_or_defer_access_check (BASELINK_BINFO (baselink),
4862 id, id, tf_warning_or_error);
4863 return id;
4866 /* Helper function for cp_parser_omp_declare_reduction_exprs
4867 and tsubst_omp_udr.
4868 Remove CLEANUP_STMT for data (omp_priv variable).
4869 Also append INIT_EXPR for DECL_INITIAL of omp_priv after its
4870 DECL_EXPR. */
4872 tree
4873 cp_remove_omp_priv_cleanup_stmt (tree *tp, int *walk_subtrees, void *data)
4875 if (TYPE_P (*tp))
4876 *walk_subtrees = 0;
4877 else if (TREE_CODE (*tp) == CLEANUP_STMT && CLEANUP_DECL (*tp) == (tree) data)
4878 *tp = CLEANUP_BODY (*tp);
4879 else if (TREE_CODE (*tp) == DECL_EXPR)
4881 tree decl = DECL_EXPR_DECL (*tp);
4882 if (!processing_template_decl
4883 && decl == (tree) data
4884 && DECL_INITIAL (decl)
4885 && DECL_INITIAL (decl) != error_mark_node)
4887 tree list = NULL_TREE;
4888 append_to_statement_list_force (*tp, &list);
4889 tree init_expr = build2 (INIT_EXPR, void_type_node,
4890 decl, DECL_INITIAL (decl));
4891 DECL_INITIAL (decl) = NULL_TREE;
4892 append_to_statement_list_force (init_expr, &list);
4893 *tp = list;
4896 return NULL_TREE;
4899 /* Data passed from cp_check_omp_declare_reduction to
4900 cp_check_omp_declare_reduction_r. */
4902 struct cp_check_omp_declare_reduction_data
4904 location_t loc;
4905 tree stmts[7];
4906 bool combiner_p;
4909 /* Helper function for cp_check_omp_declare_reduction, called via
4910 cp_walk_tree. */
4912 static tree
4913 cp_check_omp_declare_reduction_r (tree *tp, int *, void *data)
4915 struct cp_check_omp_declare_reduction_data *udr_data
4916 = (struct cp_check_omp_declare_reduction_data *) data;
4917 if (SSA_VAR_P (*tp)
4918 && !DECL_ARTIFICIAL (*tp)
4919 && *tp != DECL_EXPR_DECL (udr_data->stmts[udr_data->combiner_p ? 0 : 3])
4920 && *tp != DECL_EXPR_DECL (udr_data->stmts[udr_data->combiner_p ? 1 : 4]))
4922 location_t loc = udr_data->loc;
4923 if (udr_data->combiner_p)
4924 error_at (loc, "%<#pragma omp declare reduction%> combiner refers to "
4925 "variable %qD which is not %<omp_out%> nor %<omp_in%>",
4926 *tp);
4927 else
4928 error_at (loc, "%<#pragma omp declare reduction%> initializer refers "
4929 "to variable %qD which is not %<omp_priv%> nor "
4930 "%<omp_orig%>",
4931 *tp);
4932 return *tp;
4934 return NULL_TREE;
4937 /* Diagnose violation of OpenMP #pragma omp declare reduction restrictions. */
4939 void
4940 cp_check_omp_declare_reduction (tree udr)
4942 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (udr)));
4943 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
4944 type = TREE_TYPE (type);
4945 int i;
4946 location_t loc = DECL_SOURCE_LOCATION (udr);
4948 if (type == error_mark_node)
4949 return;
4950 if (ARITHMETIC_TYPE_P (type))
4952 static enum tree_code predef_codes[]
4953 = { PLUS_EXPR, MULT_EXPR, MINUS_EXPR, BIT_AND_EXPR, BIT_XOR_EXPR,
4954 BIT_IOR_EXPR, TRUTH_ANDIF_EXPR, TRUTH_ORIF_EXPR };
4955 for (i = 0; i < 8; i++)
4957 tree id = omp_reduction_id (predef_codes[i], NULL_TREE, NULL_TREE);
4958 const char *n1 = IDENTIFIER_POINTER (DECL_NAME (udr));
4959 const char *n2 = IDENTIFIER_POINTER (id);
4960 if (strncmp (n1, n2, IDENTIFIER_LENGTH (id)) == 0
4961 && (n1[IDENTIFIER_LENGTH (id)] == '~'
4962 || n1[IDENTIFIER_LENGTH (id)] == '\0'))
4963 break;
4966 if (i == 8
4967 && TREE_CODE (type) != COMPLEX_EXPR)
4969 const char prefix_minmax[] = "omp declare reduction m";
4970 size_t prefix_size = sizeof (prefix_minmax) - 1;
4971 const char *n = IDENTIFIER_POINTER (DECL_NAME (udr));
4972 if (strncmp (IDENTIFIER_POINTER (DECL_NAME (udr)),
4973 prefix_minmax, prefix_size) == 0
4974 && ((n[prefix_size] == 'i' && n[prefix_size + 1] == 'n')
4975 || (n[prefix_size] == 'a' && n[prefix_size + 1] == 'x'))
4976 && (n[prefix_size + 2] == '~' || n[prefix_size + 2] == '\0'))
4977 i = 0;
4979 if (i < 8)
4981 error_at (loc, "predeclared arithmetic type %qT in "
4982 "%<#pragma omp declare reduction%>", type);
4983 return;
4986 else if (TREE_CODE (type) == FUNCTION_TYPE
4987 || TREE_CODE (type) == METHOD_TYPE
4988 || TREE_CODE (type) == ARRAY_TYPE)
4990 error_at (loc, "function or array type %qT in "
4991 "%<#pragma omp declare reduction%>", type);
4992 return;
4994 else if (TREE_CODE (type) == REFERENCE_TYPE)
4996 error_at (loc, "reference type %qT in %<#pragma omp declare reduction%>",
4997 type);
4998 return;
5000 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
5002 error_at (loc, "const, volatile or __restrict qualified type %qT in "
5003 "%<#pragma omp declare reduction%>", type);
5004 return;
5007 tree body = DECL_SAVED_TREE (udr);
5008 if (body == NULL_TREE || TREE_CODE (body) != STATEMENT_LIST)
5009 return;
5011 tree_stmt_iterator tsi;
5012 struct cp_check_omp_declare_reduction_data data;
5013 memset (data.stmts, 0, sizeof data.stmts);
5014 for (i = 0, tsi = tsi_start (body);
5015 i < 7 && !tsi_end_p (tsi);
5016 i++, tsi_next (&tsi))
5017 data.stmts[i] = tsi_stmt (tsi);
5018 data.loc = loc;
5019 gcc_assert (tsi_end_p (tsi));
5020 if (i >= 3)
5022 gcc_assert (TREE_CODE (data.stmts[0]) == DECL_EXPR
5023 && TREE_CODE (data.stmts[1]) == DECL_EXPR);
5024 if (TREE_NO_WARNING (DECL_EXPR_DECL (data.stmts[0])))
5025 return;
5026 data.combiner_p = true;
5027 if (cp_walk_tree (&data.stmts[2], cp_check_omp_declare_reduction_r,
5028 &data, NULL))
5029 TREE_NO_WARNING (DECL_EXPR_DECL (data.stmts[0])) = 1;
5031 if (i >= 6)
5033 gcc_assert (TREE_CODE (data.stmts[3]) == DECL_EXPR
5034 && TREE_CODE (data.stmts[4]) == DECL_EXPR);
5035 data.combiner_p = false;
5036 if (cp_walk_tree (&data.stmts[5], cp_check_omp_declare_reduction_r,
5037 &data, NULL)
5038 || cp_walk_tree (&DECL_INITIAL (DECL_EXPR_DECL (data.stmts[3])),
5039 cp_check_omp_declare_reduction_r, &data, NULL))
5040 TREE_NO_WARNING (DECL_EXPR_DECL (data.stmts[0])) = 1;
5041 if (i == 7)
5042 gcc_assert (TREE_CODE (data.stmts[6]) == DECL_EXPR);
5046 /* Helper function of finish_omp_clauses. Clone STMT as if we were making
5047 an inline call. But, remap
5048 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
5049 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
5051 static tree
5052 clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
5053 tree decl, tree placeholder)
5055 copy_body_data id;
5056 struct pointer_map_t *decl_map = pointer_map_create ();
5058 *pointer_map_insert (decl_map, omp_decl1) = placeholder;
5059 *pointer_map_insert (decl_map, omp_decl2) = decl;
5060 memset (&id, 0, sizeof (id));
5061 id.src_fn = DECL_CONTEXT (omp_decl1);
5062 id.dst_fn = current_function_decl;
5063 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
5064 id.decl_map = decl_map;
5066 id.copy_decl = copy_decl_no_change;
5067 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
5068 id.transform_new_cfg = true;
5069 id.transform_return_to_modify = false;
5070 id.transform_lang_insert_block = NULL;
5071 id.eh_lp_nr = 0;
5072 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
5073 pointer_map_destroy (decl_map);
5074 return stmt;
5077 /* Helper function of finish_omp_clauses, called via cp_walk_tree.
5078 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
5080 static tree
5081 find_omp_placeholder_r (tree *tp, int *, void *data)
5083 if (*tp == (tree) data)
5084 return *tp;
5085 return NULL_TREE;
5088 /* Helper function of finish_omp_clauses. Handle OMP_CLAUSE_REDUCTION C.
5089 Return true if there is some error and the clause should be removed. */
5091 static bool
5092 finish_omp_reduction_clause (tree c, bool *need_default_ctor, bool *need_dtor)
5094 tree t = OMP_CLAUSE_DECL (c);
5095 bool predefined = false;
5096 tree type = TREE_TYPE (t);
5097 if (TREE_CODE (type) == REFERENCE_TYPE)
5098 type = TREE_TYPE (type);
5099 if (type == error_mark_node)
5100 return true;
5101 else if (ARITHMETIC_TYPE_P (type))
5102 switch (OMP_CLAUSE_REDUCTION_CODE (c))
5104 case PLUS_EXPR:
5105 case MULT_EXPR:
5106 case MINUS_EXPR:
5107 predefined = true;
5108 break;
5109 case MIN_EXPR:
5110 case MAX_EXPR:
5111 if (TREE_CODE (type) == COMPLEX_TYPE)
5112 break;
5113 predefined = true;
5114 break;
5115 case BIT_AND_EXPR:
5116 case BIT_IOR_EXPR:
5117 case BIT_XOR_EXPR:
5118 if (FLOAT_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
5119 break;
5120 predefined = true;
5121 break;
5122 case TRUTH_ANDIF_EXPR:
5123 case TRUTH_ORIF_EXPR:
5124 if (FLOAT_TYPE_P (type))
5125 break;
5126 predefined = true;
5127 break;
5128 default:
5129 break;
5131 else if (TREE_CODE (type) == ARRAY_TYPE || TYPE_READONLY (type))
5133 error ("%qE has invalid type for %<reduction%>", t);
5134 return true;
5136 else if (!processing_template_decl)
5138 t = require_complete_type (t);
5139 if (t == error_mark_node)
5140 return true;
5141 OMP_CLAUSE_DECL (c) = t;
5144 if (predefined)
5146 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = NULL_TREE;
5147 return false;
5149 else if (processing_template_decl)
5150 return false;
5152 tree id = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
5154 type = TYPE_MAIN_VARIANT (TREE_TYPE (t));
5155 if (TREE_CODE (type) == REFERENCE_TYPE)
5156 type = TREE_TYPE (type);
5157 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = NULL_TREE;
5158 if (id == NULL_TREE)
5159 id = omp_reduction_id (OMP_CLAUSE_REDUCTION_CODE (c),
5160 NULL_TREE, NULL_TREE);
5161 id = omp_reduction_lookup (OMP_CLAUSE_LOCATION (c), id, type, NULL, NULL);
5162 if (id)
5164 if (id == error_mark_node)
5165 return true;
5166 id = OVL_CURRENT (id);
5167 mark_used (id);
5168 tree body = DECL_SAVED_TREE (id);
5169 if (TREE_CODE (body) == STATEMENT_LIST)
5171 tree_stmt_iterator tsi;
5172 tree placeholder = NULL_TREE;
5173 int i;
5174 tree stmts[7];
5175 tree atype = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (id)));
5176 atype = TREE_TYPE (atype);
5177 bool need_static_cast = !same_type_p (type, atype);
5178 memset (stmts, 0, sizeof stmts);
5179 for (i = 0, tsi = tsi_start (body);
5180 i < 7 && !tsi_end_p (tsi);
5181 i++, tsi_next (&tsi))
5182 stmts[i] = tsi_stmt (tsi);
5183 gcc_assert (tsi_end_p (tsi));
5185 if (i >= 3)
5187 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
5188 && TREE_CODE (stmts[1]) == DECL_EXPR);
5189 placeholder = build_lang_decl (VAR_DECL, NULL_TREE, type);
5190 DECL_ARTIFICIAL (placeholder) = 1;
5191 DECL_IGNORED_P (placeholder) = 1;
5192 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
5193 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[0])))
5194 cxx_mark_addressable (placeholder);
5195 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[1]))
5196 && TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c)))
5197 != REFERENCE_TYPE)
5198 cxx_mark_addressable (OMP_CLAUSE_DECL (c));
5199 tree omp_out = placeholder;
5200 tree omp_in = convert_from_reference (OMP_CLAUSE_DECL (c));
5201 if (need_static_cast)
5203 tree rtype = build_reference_type (atype);
5204 omp_out = build_static_cast (rtype, omp_out,
5205 tf_warning_or_error);
5206 omp_in = build_static_cast (rtype, omp_in,
5207 tf_warning_or_error);
5208 if (omp_out == error_mark_node || omp_in == error_mark_node)
5209 return true;
5210 omp_out = convert_from_reference (omp_out);
5211 omp_in = convert_from_reference (omp_in);
5213 OMP_CLAUSE_REDUCTION_MERGE (c)
5214 = clone_omp_udr (stmts[2], DECL_EXPR_DECL (stmts[0]),
5215 DECL_EXPR_DECL (stmts[1]), omp_in, omp_out);
5217 if (i >= 6)
5219 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
5220 && TREE_CODE (stmts[4]) == DECL_EXPR);
5221 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[3])))
5222 cxx_mark_addressable (OMP_CLAUSE_DECL (c));
5223 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[4])))
5224 cxx_mark_addressable (placeholder);
5225 tree omp_priv = convert_from_reference (OMP_CLAUSE_DECL (c));
5226 tree omp_orig = placeholder;
5227 if (need_static_cast)
5229 if (i == 7)
5231 error_at (OMP_CLAUSE_LOCATION (c),
5232 "user defined reduction with constructor "
5233 "initializer for base class %qT", atype);
5234 return true;
5236 tree rtype = build_reference_type (atype);
5237 omp_priv = build_static_cast (rtype, omp_priv,
5238 tf_warning_or_error);
5239 omp_orig = build_static_cast (rtype, omp_orig,
5240 tf_warning_or_error);
5241 if (omp_priv == error_mark_node
5242 || omp_orig == error_mark_node)
5243 return true;
5244 omp_priv = convert_from_reference (omp_priv);
5245 omp_orig = convert_from_reference (omp_orig);
5247 if (i == 6)
5248 *need_default_ctor = true;
5249 OMP_CLAUSE_REDUCTION_INIT (c)
5250 = clone_omp_udr (stmts[5], DECL_EXPR_DECL (stmts[4]),
5251 DECL_EXPR_DECL (stmts[3]),
5252 omp_priv, omp_orig);
5253 if (cp_walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
5254 find_omp_placeholder_r, placeholder, NULL))
5255 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
5257 else if (i >= 3)
5259 if (CLASS_TYPE_P (type) && !pod_type_p (type))
5260 *need_default_ctor = true;
5261 else
5263 tree init;
5264 tree v = convert_from_reference (t);
5265 if (AGGREGATE_TYPE_P (TREE_TYPE (v)))
5266 init = build_constructor (TREE_TYPE (v), NULL);
5267 else
5268 init = fold_convert (TREE_TYPE (v), integer_zero_node);
5269 OMP_CLAUSE_REDUCTION_INIT (c)
5270 = build2 (INIT_EXPR, TREE_TYPE (v), v, init);
5275 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
5276 *need_dtor = true;
5277 else
5279 error ("user defined reduction not found for %qD", t);
5280 return true;
5282 return false;
5285 /* For all elements of CLAUSES, validate them vs OpenMP constraints.
5286 Remove any elements from the list that are invalid. */
5288 tree
5289 finish_omp_clauses (tree clauses)
5291 bitmap_head generic_head, firstprivate_head, lastprivate_head;
5292 bitmap_head aligned_head;
5293 tree c, t, *pc;
5294 bool branch_seen = false;
5295 bool copyprivate_seen = false;
5297 bitmap_obstack_initialize (NULL);
5298 bitmap_initialize (&generic_head, &bitmap_default_obstack);
5299 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
5300 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
5301 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
5303 for (pc = &clauses, c = clauses; c ; c = *pc)
5305 bool remove = false;
5307 switch (OMP_CLAUSE_CODE (c))
5309 case OMP_CLAUSE_SHARED:
5310 goto check_dup_generic;
5311 case OMP_CLAUSE_PRIVATE:
5312 goto check_dup_generic;
5313 case OMP_CLAUSE_REDUCTION:
5314 goto check_dup_generic;
5315 case OMP_CLAUSE_COPYPRIVATE:
5316 copyprivate_seen = true;
5317 goto check_dup_generic;
5318 case OMP_CLAUSE_COPYIN:
5319 goto check_dup_generic;
5320 case OMP_CLAUSE_LINEAR:
5321 t = OMP_CLAUSE_DECL (c);
5322 if (!type_dependent_expression_p (t)
5323 && !INTEGRAL_TYPE_P (TREE_TYPE (t))
5324 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
5326 error ("linear clause applied to non-integral non-pointer "
5327 "variable with %qT type", TREE_TYPE (t));
5328 remove = true;
5329 break;
5331 t = OMP_CLAUSE_LINEAR_STEP (c);
5332 if (t == NULL_TREE)
5333 t = integer_one_node;
5334 if (t == error_mark_node)
5336 remove = true;
5337 break;
5339 else if (!type_dependent_expression_p (t)
5340 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5342 error ("linear step expression must be integral");
5343 remove = true;
5344 break;
5346 else
5348 t = mark_rvalue_use (t);
5349 if (!processing_template_decl)
5351 if (TREE_CODE (OMP_CLAUSE_DECL (c)) == PARM_DECL)
5352 t = maybe_constant_value (t);
5353 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5354 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c)))
5355 == POINTER_TYPE)
5357 t = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
5358 OMP_CLAUSE_DECL (c), t);
5359 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c),
5360 MINUS_EXPR, sizetype, t,
5361 OMP_CLAUSE_DECL (c));
5362 if (t == error_mark_node)
5364 remove = true;
5365 break;
5368 else
5369 t = fold_convert (TREE_TYPE (OMP_CLAUSE_DECL (c)), t);
5371 OMP_CLAUSE_LINEAR_STEP (c) = t;
5373 goto check_dup_generic;
5374 check_dup_generic:
5375 t = OMP_CLAUSE_DECL (c);
5376 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
5378 if (processing_template_decl)
5379 break;
5380 if (DECL_P (t))
5381 error ("%qD is not a variable in clause %qs", t,
5382 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5383 else
5384 error ("%qE is not a variable in clause %qs", t,
5385 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5386 remove = true;
5388 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
5389 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
5390 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
5392 error ("%qD appears more than once in data clauses", t);
5393 remove = true;
5395 else
5396 bitmap_set_bit (&generic_head, DECL_UID (t));
5397 break;
5399 case OMP_CLAUSE_FIRSTPRIVATE:
5400 t = OMP_CLAUSE_DECL (c);
5401 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
5403 if (processing_template_decl)
5404 break;
5405 if (DECL_P (t))
5406 error ("%qD is not a variable in clause %<firstprivate%>", t);
5407 else
5408 error ("%qE is not a variable in clause %<firstprivate%>", t);
5409 remove = true;
5411 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
5412 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
5414 error ("%qD appears more than once in data clauses", t);
5415 remove = true;
5417 else
5418 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
5419 break;
5421 case OMP_CLAUSE_LASTPRIVATE:
5422 t = OMP_CLAUSE_DECL (c);
5423 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
5425 if (processing_template_decl)
5426 break;
5427 if (DECL_P (t))
5428 error ("%qD is not a variable in clause %<lastprivate%>", t);
5429 else
5430 error ("%qE is not a variable in clause %<lastprivate%>", t);
5431 remove = true;
5433 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
5434 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
5436 error ("%qD appears more than once in data clauses", t);
5437 remove = true;
5439 else
5440 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
5441 break;
5443 case OMP_CLAUSE_IF:
5444 t = OMP_CLAUSE_IF_EXPR (c);
5445 t = maybe_convert_cond (t);
5446 if (t == error_mark_node)
5447 remove = true;
5448 else if (!processing_template_decl)
5449 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5450 OMP_CLAUSE_IF_EXPR (c) = t;
5451 break;
5453 case OMP_CLAUSE_FINAL:
5454 t = OMP_CLAUSE_FINAL_EXPR (c);
5455 t = maybe_convert_cond (t);
5456 if (t == error_mark_node)
5457 remove = true;
5458 else if (!processing_template_decl)
5459 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5460 OMP_CLAUSE_FINAL_EXPR (c) = t;
5461 break;
5463 case OMP_CLAUSE_NUM_THREADS:
5464 t = OMP_CLAUSE_NUM_THREADS_EXPR (c);
5465 if (t == error_mark_node)
5466 remove = true;
5467 else if (!type_dependent_expression_p (t)
5468 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5470 error ("num_threads expression must be integral");
5471 remove = true;
5473 else
5475 t = mark_rvalue_use (t);
5476 if (!processing_template_decl)
5477 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5478 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
5480 break;
5482 case OMP_CLAUSE_SCHEDULE:
5483 t = OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c);
5484 if (t == NULL)
5486 else if (t == error_mark_node)
5487 remove = true;
5488 else if (!type_dependent_expression_p (t)
5489 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5491 error ("schedule chunk size expression must be integral");
5492 remove = true;
5494 else
5496 t = mark_rvalue_use (t);
5497 if (!processing_template_decl)
5498 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5499 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
5501 break;
5503 case OMP_CLAUSE_SIMDLEN:
5504 case OMP_CLAUSE_SAFELEN:
5505 t = OMP_CLAUSE_OPERAND (c, 0);
5506 if (t == error_mark_node)
5507 remove = true;
5508 else if (!type_dependent_expression_p (t)
5509 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5511 error ("%qs length expression must be integral",
5512 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5513 remove = true;
5515 else
5517 t = mark_rvalue_use (t);
5518 t = maybe_constant_value (t);
5519 if (!processing_template_decl)
5521 if (TREE_CODE (t) != INTEGER_CST
5522 || tree_int_cst_sgn (t) != 1)
5524 error ("%qs length expression must be positive constant"
5525 " integer expression",
5526 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5527 remove = true;
5530 OMP_CLAUSE_OPERAND (c, 0) = t;
5532 break;
5534 case OMP_CLAUSE_NUM_TEAMS:
5535 t = OMP_CLAUSE_NUM_TEAMS_EXPR (c);
5536 if (t == error_mark_node)
5537 remove = true;
5538 else if (!type_dependent_expression_p (t)
5539 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5541 error ("%<num_teams%> expression must be integral");
5542 remove = true;
5544 else
5546 t = mark_rvalue_use (t);
5547 if (!processing_template_decl)
5548 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5549 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
5551 break;
5553 case OMP_CLAUSE_THREAD_LIMIT:
5554 t = OMP_CLAUSE_THREAD_LIMIT_EXPR (c);
5555 if (t == error_mark_node)
5556 remove = true;
5557 else if (!type_dependent_expression_p (t)
5558 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5560 error ("%<thread_limit%> expression must be integral");
5561 remove = true;
5563 else
5565 t = mark_rvalue_use (t);
5566 if (!processing_template_decl)
5567 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5568 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
5570 break;
5572 case OMP_CLAUSE_DEVICE:
5573 t = OMP_CLAUSE_DEVICE_ID (c);
5574 if (t == error_mark_node)
5575 remove = true;
5576 else if (!type_dependent_expression_p (t)
5577 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5579 error ("%<device%> id must be integral");
5580 remove = true;
5582 else
5584 t = mark_rvalue_use (t);
5585 if (!processing_template_decl)
5586 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5587 OMP_CLAUSE_DEVICE_ID (c) = t;
5589 break;
5591 case OMP_CLAUSE_DIST_SCHEDULE:
5592 t = OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c);
5593 if (t == NULL)
5595 else if (t == error_mark_node)
5596 remove = true;
5597 else if (!type_dependent_expression_p (t)
5598 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5600 error ("%<dist_schedule%> chunk size expression must be "
5601 "integral");
5602 remove = true;
5604 else
5606 t = mark_rvalue_use (t);
5607 if (!processing_template_decl)
5608 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5609 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
5611 break;
5613 case OMP_CLAUSE_ALIGNED:
5614 t = OMP_CLAUSE_DECL (c);
5615 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
5617 if (processing_template_decl)
5618 break;
5619 if (DECL_P (t))
5620 error ("%qD is not a variable in %<aligned%> clause", t);
5621 else
5622 error ("%qE is not a variable in %<aligned%> clause", t);
5623 remove = true;
5625 else if (!type_dependent_expression_p (t)
5626 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE
5627 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE
5628 && (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE
5629 || (!POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (t)))
5630 && (TREE_CODE (TREE_TYPE (TREE_TYPE (t)))
5631 != ARRAY_TYPE))))
5633 error_at (OMP_CLAUSE_LOCATION (c),
5634 "%qE in %<aligned%> clause is neither a pointer nor "
5635 "an array nor a reference to pointer or array", t);
5636 remove = true;
5638 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
5640 error ("%qD appears more than once in %<aligned%> clauses", t);
5641 remove = true;
5643 else
5644 bitmap_set_bit (&aligned_head, DECL_UID (t));
5645 t = OMP_CLAUSE_ALIGNED_ALIGNMENT (c);
5646 if (t == error_mark_node)
5647 remove = true;
5648 else if (t == NULL_TREE)
5649 break;
5650 else if (!type_dependent_expression_p (t)
5651 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5653 error ("%<aligned%> clause alignment expression must "
5654 "be integral");
5655 remove = true;
5657 else
5659 t = mark_rvalue_use (t);
5660 t = maybe_constant_value (t);
5661 if (!processing_template_decl)
5663 if (TREE_CODE (t) != INTEGER_CST
5664 || tree_int_cst_sgn (t) != 1)
5666 error ("%<aligned%> clause alignment expression must be "
5667 "positive constant integer expression");
5668 remove = true;
5671 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = t;
5673 break;
5675 case OMP_CLAUSE_DEPEND:
5676 t = OMP_CLAUSE_DECL (c);
5677 if (TREE_CODE (t) == TREE_LIST)
5679 if (handle_omp_array_sections (c))
5680 remove = true;
5681 break;
5683 if (t == error_mark_node)
5684 remove = true;
5685 else if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
5687 if (processing_template_decl)
5688 break;
5689 if (DECL_P (t))
5690 error ("%qD is not a variable in %<depend%> clause", t);
5691 else
5692 error ("%qE is not a variable in %<depend%> clause", t);
5693 remove = true;
5695 else if (!processing_template_decl
5696 && !cxx_mark_addressable (t))
5697 remove = true;
5698 break;
5700 case OMP_CLAUSE_MAP:
5701 case OMP_CLAUSE_TO:
5702 case OMP_CLAUSE_FROM:
5703 t = OMP_CLAUSE_DECL (c);
5704 if (TREE_CODE (t) == TREE_LIST)
5706 if (handle_omp_array_sections (c))
5707 remove = true;
5708 else
5710 t = OMP_CLAUSE_DECL (c);
5711 if (!cp_omp_mappable_type (TREE_TYPE (t)))
5713 error_at (OMP_CLAUSE_LOCATION (c),
5714 "array section does not have mappable type "
5715 "in %qs clause",
5716 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5717 remove = true;
5720 break;
5722 if (t == error_mark_node)
5723 remove = true;
5724 else if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
5726 if (processing_template_decl)
5727 break;
5728 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
5729 && OMP_CLAUSE_MAP_KIND (c) == OMP_CLAUSE_MAP_POINTER)
5730 break;
5731 if (DECL_P (t))
5732 error ("%qD is not a variable in %qs clause", t,
5733 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5734 else
5735 error ("%qE is not a variable in %qs clause", t,
5736 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5737 remove = true;
5739 else if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
5741 error ("%qD is threadprivate variable in %qs clause", t,
5742 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5743 remove = true;
5745 else if (!processing_template_decl
5746 && TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE
5747 && !cxx_mark_addressable (t))
5748 remove = true;
5749 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
5750 && OMP_CLAUSE_MAP_KIND (c) == OMP_CLAUSE_MAP_POINTER)
5751 && !cp_omp_mappable_type ((TREE_CODE (TREE_TYPE (t))
5752 == REFERENCE_TYPE)
5753 ? TREE_TYPE (TREE_TYPE (t))
5754 : TREE_TYPE (t)))
5756 error_at (OMP_CLAUSE_LOCATION (c),
5757 "%qD does not have a mappable type in %qs clause", t,
5758 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5759 remove = true;
5761 else if (bitmap_bit_p (&generic_head, DECL_UID (t)))
5763 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
5764 error ("%qD appears more than once in motion clauses", t);
5765 else
5766 error ("%qD appears more than once in map clauses", t);
5767 remove = true;
5769 else
5770 bitmap_set_bit (&generic_head, DECL_UID (t));
5771 break;
5773 case OMP_CLAUSE_UNIFORM:
5774 t = OMP_CLAUSE_DECL (c);
5775 if (TREE_CODE (t) != PARM_DECL)
5777 if (processing_template_decl)
5778 break;
5779 if (DECL_P (t))
5780 error ("%qD is not an argument in %<uniform%> clause", t);
5781 else
5782 error ("%qE is not an argument in %<uniform%> clause", t);
5783 remove = true;
5784 break;
5786 goto check_dup_generic;
5788 case OMP_CLAUSE_NOWAIT:
5789 case OMP_CLAUSE_ORDERED:
5790 case OMP_CLAUSE_DEFAULT:
5791 case OMP_CLAUSE_UNTIED:
5792 case OMP_CLAUSE_COLLAPSE:
5793 case OMP_CLAUSE_MERGEABLE:
5794 case OMP_CLAUSE_PARALLEL:
5795 case OMP_CLAUSE_FOR:
5796 case OMP_CLAUSE_SECTIONS:
5797 case OMP_CLAUSE_TASKGROUP:
5798 case OMP_CLAUSE_PROC_BIND:
5799 break;
5801 case OMP_CLAUSE_INBRANCH:
5802 case OMP_CLAUSE_NOTINBRANCH:
5803 if (branch_seen)
5805 error ("%<inbranch%> clause is incompatible with "
5806 "%<notinbranch%>");
5807 remove = true;
5809 branch_seen = true;
5810 break;
5812 default:
5813 gcc_unreachable ();
5816 if (remove)
5817 *pc = OMP_CLAUSE_CHAIN (c);
5818 else
5819 pc = &OMP_CLAUSE_CHAIN (c);
5822 for (pc = &clauses, c = clauses; c ; c = *pc)
5824 enum omp_clause_code c_kind = OMP_CLAUSE_CODE (c);
5825 bool remove = false;
5826 bool need_complete_non_reference = false;
5827 bool need_default_ctor = false;
5828 bool need_copy_ctor = false;
5829 bool need_copy_assignment = false;
5830 bool need_implicitly_determined = false;
5831 bool need_dtor = false;
5832 tree type, inner_type;
5834 switch (c_kind)
5836 case OMP_CLAUSE_SHARED:
5837 need_implicitly_determined = true;
5838 break;
5839 case OMP_CLAUSE_PRIVATE:
5840 need_complete_non_reference = true;
5841 need_default_ctor = true;
5842 need_dtor = true;
5843 need_implicitly_determined = true;
5844 break;
5845 case OMP_CLAUSE_FIRSTPRIVATE:
5846 need_complete_non_reference = true;
5847 need_copy_ctor = true;
5848 need_dtor = true;
5849 need_implicitly_determined = true;
5850 break;
5851 case OMP_CLAUSE_LASTPRIVATE:
5852 need_complete_non_reference = true;
5853 need_copy_assignment = true;
5854 need_implicitly_determined = true;
5855 break;
5856 case OMP_CLAUSE_REDUCTION:
5857 need_implicitly_determined = true;
5858 break;
5859 case OMP_CLAUSE_COPYPRIVATE:
5860 need_copy_assignment = true;
5861 break;
5862 case OMP_CLAUSE_COPYIN:
5863 need_copy_assignment = true;
5864 break;
5865 case OMP_CLAUSE_NOWAIT:
5866 if (copyprivate_seen)
5868 error_at (OMP_CLAUSE_LOCATION (c),
5869 "%<nowait%> clause must not be used together "
5870 "with %<copyprivate%>");
5871 *pc = OMP_CLAUSE_CHAIN (c);
5872 continue;
5874 /* FALLTHRU */
5875 default:
5876 pc = &OMP_CLAUSE_CHAIN (c);
5877 continue;
5880 t = OMP_CLAUSE_DECL (c);
5881 if (processing_template_decl
5882 && !VAR_P (t) && TREE_CODE (t) != PARM_DECL)
5884 pc = &OMP_CLAUSE_CHAIN (c);
5885 continue;
5888 switch (c_kind)
5890 case OMP_CLAUSE_LASTPRIVATE:
5891 if (!bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
5893 need_default_ctor = true;
5894 need_dtor = true;
5896 break;
5898 case OMP_CLAUSE_REDUCTION:
5899 if (finish_omp_reduction_clause (c, &need_default_ctor,
5900 &need_dtor))
5901 remove = true;
5902 else
5903 t = OMP_CLAUSE_DECL (c);
5904 break;
5906 case OMP_CLAUSE_COPYIN:
5907 if (!VAR_P (t) || !DECL_THREAD_LOCAL_P (t))
5909 error ("%qE must be %<threadprivate%> for %<copyin%>", t);
5910 remove = true;
5912 break;
5914 default:
5915 break;
5918 if (need_complete_non_reference || need_copy_assignment)
5920 t = require_complete_type (t);
5921 if (t == error_mark_node)
5922 remove = true;
5923 else if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE
5924 && need_complete_non_reference)
5926 error ("%qE has reference type for %qs", t,
5927 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5928 remove = true;
5931 if (need_implicitly_determined)
5933 const char *share_name = NULL;
5935 if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
5936 share_name = "threadprivate";
5937 else switch (cxx_omp_predetermined_sharing (t))
5939 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
5940 break;
5941 case OMP_CLAUSE_DEFAULT_SHARED:
5942 /* const vars may be specified in firstprivate clause. */
5943 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
5944 && cxx_omp_const_qual_no_mutable (t))
5945 break;
5946 share_name = "shared";
5947 break;
5948 case OMP_CLAUSE_DEFAULT_PRIVATE:
5949 share_name = "private";
5950 break;
5951 default:
5952 gcc_unreachable ();
5954 if (share_name)
5956 error ("%qE is predetermined %qs for %qs",
5957 t, share_name, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5958 remove = true;
5962 /* We're interested in the base element, not arrays. */
5963 inner_type = type = TREE_TYPE (t);
5964 while (TREE_CODE (inner_type) == ARRAY_TYPE)
5965 inner_type = TREE_TYPE (inner_type);
5967 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
5968 && TREE_CODE (inner_type) == REFERENCE_TYPE)
5969 inner_type = TREE_TYPE (inner_type);
5971 /* Check for special function availability by building a call to one.
5972 Save the results, because later we won't be in the right context
5973 for making these queries. */
5974 if (CLASS_TYPE_P (inner_type)
5975 && COMPLETE_TYPE_P (inner_type)
5976 && (need_default_ctor || need_copy_ctor
5977 || need_copy_assignment || need_dtor)
5978 && !type_dependent_expression_p (t)
5979 && cxx_omp_create_clause_info (c, inner_type, need_default_ctor,
5980 need_copy_ctor, need_copy_assignment,
5981 need_dtor))
5982 remove = true;
5984 if (remove)
5985 *pc = OMP_CLAUSE_CHAIN (c);
5986 else
5987 pc = &OMP_CLAUSE_CHAIN (c);
5990 bitmap_obstack_release (NULL);
5991 return clauses;
5994 /* For all variables in the tree_list VARS, mark them as thread local. */
5996 void
5997 finish_omp_threadprivate (tree vars)
5999 tree t;
6001 /* Mark every variable in VARS to be assigned thread local storage. */
6002 for (t = vars; t; t = TREE_CHAIN (t))
6004 tree v = TREE_PURPOSE (t);
6006 if (error_operand_p (v))
6008 else if (!VAR_P (v))
6009 error ("%<threadprivate%> %qD is not file, namespace "
6010 "or block scope variable", v);
6011 /* If V had already been marked threadprivate, it doesn't matter
6012 whether it had been used prior to this point. */
6013 else if (TREE_USED (v)
6014 && (DECL_LANG_SPECIFIC (v) == NULL
6015 || !CP_DECL_THREADPRIVATE_P (v)))
6016 error ("%qE declared %<threadprivate%> after first use", v);
6017 else if (! TREE_STATIC (v) && ! DECL_EXTERNAL (v))
6018 error ("automatic variable %qE cannot be %<threadprivate%>", v);
6019 else if (! COMPLETE_TYPE_P (complete_type (TREE_TYPE (v))))
6020 error ("%<threadprivate%> %qE has incomplete type", v);
6021 else if (TREE_STATIC (v) && TYPE_P (CP_DECL_CONTEXT (v))
6022 && CP_DECL_CONTEXT (v) != current_class_type)
6023 error ("%<threadprivate%> %qE directive not "
6024 "in %qT definition", v, CP_DECL_CONTEXT (v));
6025 else
6027 /* Allocate a LANG_SPECIFIC structure for V, if needed. */
6028 if (DECL_LANG_SPECIFIC (v) == NULL)
6030 retrofit_lang_decl (v);
6032 /* Make sure that DECL_DISCRIMINATOR_P continues to be true
6033 after the allocation of the lang_decl structure. */
6034 if (DECL_DISCRIMINATOR_P (v))
6035 DECL_LANG_SPECIFIC (v)->u.base.u2sel = 1;
6038 if (! DECL_THREAD_LOCAL_P (v))
6040 set_decl_tls_model (v, decl_default_tls_model (v));
6041 /* If rtl has been already set for this var, call
6042 make_decl_rtl once again, so that encode_section_info
6043 has a chance to look at the new decl flags. */
6044 if (DECL_RTL_SET_P (v))
6045 make_decl_rtl (v);
6047 CP_DECL_THREADPRIVATE_P (v) = 1;
6052 /* Build an OpenMP structured block. */
6054 tree
6055 begin_omp_structured_block (void)
6057 return do_pushlevel (sk_omp);
6060 tree
6061 finish_omp_structured_block (tree block)
6063 return do_poplevel (block);
6066 /* Similarly, except force the retention of the BLOCK. */
6068 tree
6069 begin_omp_parallel (void)
6071 keep_next_level (true);
6072 return begin_omp_structured_block ();
6075 tree
6076 finish_omp_parallel (tree clauses, tree body)
6078 tree stmt;
6080 body = finish_omp_structured_block (body);
6082 stmt = make_node (OMP_PARALLEL);
6083 TREE_TYPE (stmt) = void_type_node;
6084 OMP_PARALLEL_CLAUSES (stmt) = clauses;
6085 OMP_PARALLEL_BODY (stmt) = body;
6087 return add_stmt (stmt);
6090 tree
6091 begin_omp_task (void)
6093 keep_next_level (true);
6094 return begin_omp_structured_block ();
6097 tree
6098 finish_omp_task (tree clauses, tree body)
6100 tree stmt;
6102 body = finish_omp_structured_block (body);
6104 stmt = make_node (OMP_TASK);
6105 TREE_TYPE (stmt) = void_type_node;
6106 OMP_TASK_CLAUSES (stmt) = clauses;
6107 OMP_TASK_BODY (stmt) = body;
6109 return add_stmt (stmt);
6112 /* Helper function for finish_omp_for. Convert Ith random access iterator
6113 into integral iterator. Return FALSE if successful. */
6115 static bool
6116 handle_omp_for_class_iterator (int i, location_t locus, tree declv, tree initv,
6117 tree condv, tree incrv, tree *body,
6118 tree *pre_body, tree clauses)
6120 tree diff, iter_init, iter_incr = NULL, last;
6121 tree incr_var = NULL, orig_pre_body, orig_body, c;
6122 tree decl = TREE_VEC_ELT (declv, i);
6123 tree init = TREE_VEC_ELT (initv, i);
6124 tree cond = TREE_VEC_ELT (condv, i);
6125 tree incr = TREE_VEC_ELT (incrv, i);
6126 tree iter = decl;
6127 location_t elocus = locus;
6129 if (init && EXPR_HAS_LOCATION (init))
6130 elocus = EXPR_LOCATION (init);
6132 switch (TREE_CODE (cond))
6134 case GT_EXPR:
6135 case GE_EXPR:
6136 case LT_EXPR:
6137 case LE_EXPR:
6138 if (TREE_OPERAND (cond, 1) == iter)
6139 cond = build2 (swap_tree_comparison (TREE_CODE (cond)),
6140 TREE_TYPE (cond), iter, TREE_OPERAND (cond, 0));
6141 if (TREE_OPERAND (cond, 0) != iter)
6142 cond = error_mark_node;
6143 else
6145 tree tem = build_x_binary_op (EXPR_LOCATION (cond),
6146 TREE_CODE (cond),
6147 iter, ERROR_MARK,
6148 TREE_OPERAND (cond, 1), ERROR_MARK,
6149 NULL, tf_warning_or_error);
6150 if (error_operand_p (tem))
6151 return true;
6153 break;
6154 default:
6155 cond = error_mark_node;
6156 break;
6158 if (cond == error_mark_node)
6160 error_at (elocus, "invalid controlling predicate");
6161 return true;
6163 diff = build_x_binary_op (elocus, MINUS_EXPR, TREE_OPERAND (cond, 1),
6164 ERROR_MARK, iter, ERROR_MARK, NULL,
6165 tf_warning_or_error);
6166 if (error_operand_p (diff))
6167 return true;
6168 if (TREE_CODE (TREE_TYPE (diff)) != INTEGER_TYPE)
6170 error_at (elocus, "difference between %qE and %qD does not have integer type",
6171 TREE_OPERAND (cond, 1), iter);
6172 return true;
6175 switch (TREE_CODE (incr))
6177 case PREINCREMENT_EXPR:
6178 case PREDECREMENT_EXPR:
6179 case POSTINCREMENT_EXPR:
6180 case POSTDECREMENT_EXPR:
6181 if (TREE_OPERAND (incr, 0) != iter)
6183 incr = error_mark_node;
6184 break;
6186 iter_incr = build_x_unary_op (EXPR_LOCATION (incr),
6187 TREE_CODE (incr), iter,
6188 tf_warning_or_error);
6189 if (error_operand_p (iter_incr))
6190 return true;
6191 else if (TREE_CODE (incr) == PREINCREMENT_EXPR
6192 || TREE_CODE (incr) == POSTINCREMENT_EXPR)
6193 incr = integer_one_node;
6194 else
6195 incr = integer_minus_one_node;
6196 break;
6197 case MODIFY_EXPR:
6198 if (TREE_OPERAND (incr, 0) != iter)
6199 incr = error_mark_node;
6200 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
6201 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
6203 tree rhs = TREE_OPERAND (incr, 1);
6204 if (TREE_OPERAND (rhs, 0) == iter)
6206 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (rhs, 1)))
6207 != INTEGER_TYPE)
6208 incr = error_mark_node;
6209 else
6211 iter_incr = build_x_modify_expr (EXPR_LOCATION (rhs),
6212 iter, TREE_CODE (rhs),
6213 TREE_OPERAND (rhs, 1),
6214 tf_warning_or_error);
6215 if (error_operand_p (iter_incr))
6216 return true;
6217 incr = TREE_OPERAND (rhs, 1);
6218 incr = cp_convert (TREE_TYPE (diff), incr,
6219 tf_warning_or_error);
6220 if (TREE_CODE (rhs) == MINUS_EXPR)
6222 incr = build1 (NEGATE_EXPR, TREE_TYPE (diff), incr);
6223 incr = fold_if_not_in_template (incr);
6225 if (TREE_CODE (incr) != INTEGER_CST
6226 && (TREE_CODE (incr) != NOP_EXPR
6227 || (TREE_CODE (TREE_OPERAND (incr, 0))
6228 != INTEGER_CST)))
6229 iter_incr = NULL;
6232 else if (TREE_OPERAND (rhs, 1) == iter)
6234 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (rhs, 0))) != INTEGER_TYPE
6235 || TREE_CODE (rhs) != PLUS_EXPR)
6236 incr = error_mark_node;
6237 else
6239 iter_incr = build_x_binary_op (EXPR_LOCATION (rhs),
6240 PLUS_EXPR,
6241 TREE_OPERAND (rhs, 0),
6242 ERROR_MARK, iter,
6243 ERROR_MARK, NULL,
6244 tf_warning_or_error);
6245 if (error_operand_p (iter_incr))
6246 return true;
6247 iter_incr = build_x_modify_expr (EXPR_LOCATION (rhs),
6248 iter, NOP_EXPR,
6249 iter_incr,
6250 tf_warning_or_error);
6251 if (error_operand_p (iter_incr))
6252 return true;
6253 incr = TREE_OPERAND (rhs, 0);
6254 iter_incr = NULL;
6257 else
6258 incr = error_mark_node;
6260 else
6261 incr = error_mark_node;
6262 break;
6263 default:
6264 incr = error_mark_node;
6265 break;
6268 if (incr == error_mark_node)
6270 error_at (elocus, "invalid increment expression");
6271 return true;
6274 incr = cp_convert (TREE_TYPE (diff), incr, tf_warning_or_error);
6275 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
6276 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
6277 && OMP_CLAUSE_DECL (c) == iter)
6278 break;
6280 decl = create_temporary_var (TREE_TYPE (diff));
6281 pushdecl (decl);
6282 add_decl_expr (decl);
6283 last = create_temporary_var (TREE_TYPE (diff));
6284 pushdecl (last);
6285 add_decl_expr (last);
6286 if (c && iter_incr == NULL)
6288 incr_var = create_temporary_var (TREE_TYPE (diff));
6289 pushdecl (incr_var);
6290 add_decl_expr (incr_var);
6292 gcc_assert (stmts_are_full_exprs_p ());
6294 orig_pre_body = *pre_body;
6295 *pre_body = push_stmt_list ();
6296 if (orig_pre_body)
6297 add_stmt (orig_pre_body);
6298 if (init != NULL)
6299 finish_expr_stmt (build_x_modify_expr (elocus,
6300 iter, NOP_EXPR, init,
6301 tf_warning_or_error));
6302 init = build_int_cst (TREE_TYPE (diff), 0);
6303 if (c && iter_incr == NULL)
6305 finish_expr_stmt (build_x_modify_expr (elocus,
6306 incr_var, NOP_EXPR,
6307 incr, tf_warning_or_error));
6308 incr = incr_var;
6309 iter_incr = build_x_modify_expr (elocus,
6310 iter, PLUS_EXPR, incr,
6311 tf_warning_or_error);
6313 finish_expr_stmt (build_x_modify_expr (elocus,
6314 last, NOP_EXPR, init,
6315 tf_warning_or_error));
6316 *pre_body = pop_stmt_list (*pre_body);
6318 cond = cp_build_binary_op (elocus,
6319 TREE_CODE (cond), decl, diff,
6320 tf_warning_or_error);
6321 incr = build_modify_expr (elocus, decl, NULL_TREE, PLUS_EXPR,
6322 elocus, incr, NULL_TREE);
6324 orig_body = *body;
6325 *body = push_stmt_list ();
6326 iter_init = build2 (MINUS_EXPR, TREE_TYPE (diff), decl, last);
6327 iter_init = build_x_modify_expr (elocus,
6328 iter, PLUS_EXPR, iter_init,
6329 tf_warning_or_error);
6330 iter_init = build1 (NOP_EXPR, void_type_node, iter_init);
6331 finish_expr_stmt (iter_init);
6332 finish_expr_stmt (build_x_modify_expr (elocus,
6333 last, NOP_EXPR, decl,
6334 tf_warning_or_error));
6335 add_stmt (orig_body);
6336 *body = pop_stmt_list (*body);
6338 if (c)
6340 OMP_CLAUSE_LASTPRIVATE_STMT (c) = push_stmt_list ();
6341 finish_expr_stmt (iter_incr);
6342 OMP_CLAUSE_LASTPRIVATE_STMT (c)
6343 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (c));
6346 TREE_VEC_ELT (declv, i) = decl;
6347 TREE_VEC_ELT (initv, i) = init;
6348 TREE_VEC_ELT (condv, i) = cond;
6349 TREE_VEC_ELT (incrv, i) = incr;
6351 return false;
6354 /* Build and validate an OMP_FOR statement. CLAUSES, BODY, COND, INCR
6355 are directly for their associated operands in the statement. DECL
6356 and INIT are a combo; if DECL is NULL then INIT ought to be a
6357 MODIFY_EXPR, and the DECL should be extracted. PRE_BODY are
6358 optional statements that need to go before the loop into its
6359 sk_omp scope. */
6361 tree
6362 finish_omp_for (location_t locus, enum tree_code code, tree declv, tree initv,
6363 tree condv, tree incrv, tree body, tree pre_body, tree clauses)
6365 tree omp_for = NULL, orig_incr = NULL;
6366 tree decl, init, cond, incr;
6367 location_t elocus;
6368 int i;
6370 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (initv));
6371 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (condv));
6372 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (incrv));
6373 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
6375 decl = TREE_VEC_ELT (declv, i);
6376 init = TREE_VEC_ELT (initv, i);
6377 cond = TREE_VEC_ELT (condv, i);
6378 incr = TREE_VEC_ELT (incrv, i);
6379 elocus = locus;
6381 if (decl == NULL)
6383 if (init != NULL)
6384 switch (TREE_CODE (init))
6386 case MODIFY_EXPR:
6387 decl = TREE_OPERAND (init, 0);
6388 init = TREE_OPERAND (init, 1);
6389 break;
6390 case MODOP_EXPR:
6391 if (TREE_CODE (TREE_OPERAND (init, 1)) == NOP_EXPR)
6393 decl = TREE_OPERAND (init, 0);
6394 init = TREE_OPERAND (init, 2);
6396 break;
6397 default:
6398 break;
6401 if (decl == NULL)
6403 error_at (locus,
6404 "expected iteration declaration or initialization");
6405 return NULL;
6409 if (init && EXPR_HAS_LOCATION (init))
6410 elocus = EXPR_LOCATION (init);
6412 if (cond == NULL)
6414 error_at (elocus, "missing controlling predicate");
6415 return NULL;
6418 if (incr == NULL)
6420 error_at (elocus, "missing increment expression");
6421 return NULL;
6424 TREE_VEC_ELT (declv, i) = decl;
6425 TREE_VEC_ELT (initv, i) = init;
6428 if (dependent_omp_for_p (declv, initv, condv, incrv))
6430 tree stmt;
6432 stmt = make_node (code);
6434 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
6436 /* This is really just a place-holder. We'll be decomposing this
6437 again and going through the cp_build_modify_expr path below when
6438 we instantiate the thing. */
6439 TREE_VEC_ELT (initv, i)
6440 = build2 (MODIFY_EXPR, void_type_node, TREE_VEC_ELT (declv, i),
6441 TREE_VEC_ELT (initv, i));
6444 TREE_TYPE (stmt) = void_type_node;
6445 OMP_FOR_INIT (stmt) = initv;
6446 OMP_FOR_COND (stmt) = condv;
6447 OMP_FOR_INCR (stmt) = incrv;
6448 OMP_FOR_BODY (stmt) = body;
6449 OMP_FOR_PRE_BODY (stmt) = pre_body;
6450 OMP_FOR_CLAUSES (stmt) = clauses;
6452 SET_EXPR_LOCATION (stmt, locus);
6453 return add_stmt (stmt);
6456 if (processing_template_decl)
6457 orig_incr = make_tree_vec (TREE_VEC_LENGTH (incrv));
6459 for (i = 0; i < TREE_VEC_LENGTH (declv); )
6461 decl = TREE_VEC_ELT (declv, i);
6462 init = TREE_VEC_ELT (initv, i);
6463 cond = TREE_VEC_ELT (condv, i);
6464 incr = TREE_VEC_ELT (incrv, i);
6465 if (orig_incr)
6466 TREE_VEC_ELT (orig_incr, i) = incr;
6467 elocus = locus;
6469 if (init && EXPR_HAS_LOCATION (init))
6470 elocus = EXPR_LOCATION (init);
6472 if (!DECL_P (decl))
6474 error_at (elocus, "expected iteration declaration or initialization");
6475 return NULL;
6478 if (incr && TREE_CODE (incr) == MODOP_EXPR)
6480 if (orig_incr)
6481 TREE_VEC_ELT (orig_incr, i) = incr;
6482 incr = cp_build_modify_expr (TREE_OPERAND (incr, 0),
6483 TREE_CODE (TREE_OPERAND (incr, 1)),
6484 TREE_OPERAND (incr, 2),
6485 tf_warning_or_error);
6488 if (CLASS_TYPE_P (TREE_TYPE (decl)))
6490 if (code == OMP_SIMD)
6492 error_at (elocus, "%<#pragma omp simd%> used with class "
6493 "iteration variable %qE", decl);
6494 return NULL;
6496 if (handle_omp_for_class_iterator (i, locus, declv, initv, condv,
6497 incrv, &body, &pre_body, clauses))
6498 return NULL;
6499 continue;
6502 if (!INTEGRAL_TYPE_P (TREE_TYPE (decl))
6503 && !TYPE_PTR_P (TREE_TYPE (decl)))
6505 error_at (elocus, "invalid type for iteration variable %qE", decl);
6506 return NULL;
6509 if (!processing_template_decl)
6511 init = fold_build_cleanup_point_expr (TREE_TYPE (init), init);
6512 init = cp_build_modify_expr (decl, NOP_EXPR, init, tf_warning_or_error);
6514 else
6515 init = build2 (MODIFY_EXPR, void_type_node, decl, init);
6516 if (cond
6517 && TREE_SIDE_EFFECTS (cond)
6518 && COMPARISON_CLASS_P (cond)
6519 && !processing_template_decl)
6521 tree t = TREE_OPERAND (cond, 0);
6522 if (TREE_SIDE_EFFECTS (t)
6523 && t != decl
6524 && (TREE_CODE (t) != NOP_EXPR
6525 || TREE_OPERAND (t, 0) != decl))
6526 TREE_OPERAND (cond, 0)
6527 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6529 t = TREE_OPERAND (cond, 1);
6530 if (TREE_SIDE_EFFECTS (t)
6531 && t != decl
6532 && (TREE_CODE (t) != NOP_EXPR
6533 || TREE_OPERAND (t, 0) != decl))
6534 TREE_OPERAND (cond, 1)
6535 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6537 if (decl == error_mark_node || init == error_mark_node)
6538 return NULL;
6540 TREE_VEC_ELT (declv, i) = decl;
6541 TREE_VEC_ELT (initv, i) = init;
6542 TREE_VEC_ELT (condv, i) = cond;
6543 TREE_VEC_ELT (incrv, i) = incr;
6544 i++;
6547 if (IS_EMPTY_STMT (pre_body))
6548 pre_body = NULL;
6550 omp_for = c_finish_omp_for (locus, code, declv, initv, condv, incrv,
6551 body, pre_body);
6553 if (omp_for == NULL)
6554 return NULL;
6556 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INCR (omp_for)); i++)
6558 decl = TREE_OPERAND (TREE_VEC_ELT (OMP_FOR_INIT (omp_for), i), 0);
6559 incr = TREE_VEC_ELT (OMP_FOR_INCR (omp_for), i);
6561 if (TREE_CODE (incr) != MODIFY_EXPR)
6562 continue;
6564 if (TREE_SIDE_EFFECTS (TREE_OPERAND (incr, 1))
6565 && BINARY_CLASS_P (TREE_OPERAND (incr, 1))
6566 && !processing_template_decl)
6568 tree t = TREE_OPERAND (TREE_OPERAND (incr, 1), 0);
6569 if (TREE_SIDE_EFFECTS (t)
6570 && t != decl
6571 && (TREE_CODE (t) != NOP_EXPR
6572 || TREE_OPERAND (t, 0) != decl))
6573 TREE_OPERAND (TREE_OPERAND (incr, 1), 0)
6574 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6576 t = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
6577 if (TREE_SIDE_EFFECTS (t)
6578 && t != decl
6579 && (TREE_CODE (t) != NOP_EXPR
6580 || TREE_OPERAND (t, 0) != decl))
6581 TREE_OPERAND (TREE_OPERAND (incr, 1), 1)
6582 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6585 if (orig_incr)
6586 TREE_VEC_ELT (OMP_FOR_INCR (omp_for), i) = TREE_VEC_ELT (orig_incr, i);
6588 if (omp_for != NULL)
6589 OMP_FOR_CLAUSES (omp_for) = clauses;
6590 return omp_for;
6593 void
6594 finish_omp_atomic (enum tree_code code, enum tree_code opcode, tree lhs,
6595 tree rhs, tree v, tree lhs1, tree rhs1, bool seq_cst)
6597 tree orig_lhs;
6598 tree orig_rhs;
6599 tree orig_v;
6600 tree orig_lhs1;
6601 tree orig_rhs1;
6602 bool dependent_p;
6603 tree stmt;
6605 orig_lhs = lhs;
6606 orig_rhs = rhs;
6607 orig_v = v;
6608 orig_lhs1 = lhs1;
6609 orig_rhs1 = rhs1;
6610 dependent_p = false;
6611 stmt = NULL_TREE;
6613 /* Even in a template, we can detect invalid uses of the atomic
6614 pragma if neither LHS nor RHS is type-dependent. */
6615 if (processing_template_decl)
6617 dependent_p = (type_dependent_expression_p (lhs)
6618 || (rhs && type_dependent_expression_p (rhs))
6619 || (v && type_dependent_expression_p (v))
6620 || (lhs1 && type_dependent_expression_p (lhs1))
6621 || (rhs1 && type_dependent_expression_p (rhs1)));
6622 if (!dependent_p)
6624 lhs = build_non_dependent_expr (lhs);
6625 if (rhs)
6626 rhs = build_non_dependent_expr (rhs);
6627 if (v)
6628 v = build_non_dependent_expr (v);
6629 if (lhs1)
6630 lhs1 = build_non_dependent_expr (lhs1);
6631 if (rhs1)
6632 rhs1 = build_non_dependent_expr (rhs1);
6635 if (!dependent_p)
6637 bool swapped = false;
6638 if (rhs1 && cp_tree_equal (lhs, rhs))
6640 tree tem = rhs;
6641 rhs = rhs1;
6642 rhs1 = tem;
6643 swapped = !commutative_tree_code (opcode);
6645 if (rhs1 && !cp_tree_equal (lhs, rhs1))
6647 if (code == OMP_ATOMIC)
6648 error ("%<#pragma omp atomic update%> uses two different "
6649 "expressions for memory");
6650 else
6651 error ("%<#pragma omp atomic capture%> uses two different "
6652 "expressions for memory");
6653 return;
6655 if (lhs1 && !cp_tree_equal (lhs, lhs1))
6657 if (code == OMP_ATOMIC)
6658 error ("%<#pragma omp atomic update%> uses two different "
6659 "expressions for memory");
6660 else
6661 error ("%<#pragma omp atomic capture%> uses two different "
6662 "expressions for memory");
6663 return;
6665 stmt = c_finish_omp_atomic (input_location, code, opcode, lhs, rhs,
6666 v, lhs1, rhs1, swapped, seq_cst);
6667 if (stmt == error_mark_node)
6668 return;
6670 if (processing_template_decl)
6672 if (code == OMP_ATOMIC_READ)
6674 stmt = build_min_nt_loc (EXPR_LOCATION (orig_lhs),
6675 OMP_ATOMIC_READ, orig_lhs);
6676 OMP_ATOMIC_SEQ_CST (stmt) = seq_cst;
6677 stmt = build2 (MODIFY_EXPR, void_type_node, orig_v, stmt);
6679 else
6681 if (opcode == NOP_EXPR)
6682 stmt = build2 (MODIFY_EXPR, void_type_node, orig_lhs, orig_rhs);
6683 else
6684 stmt = build2 (opcode, void_type_node, orig_lhs, orig_rhs);
6685 if (orig_rhs1)
6686 stmt = build_min_nt_loc (EXPR_LOCATION (orig_rhs1),
6687 COMPOUND_EXPR, orig_rhs1, stmt);
6688 if (code != OMP_ATOMIC)
6690 stmt = build_min_nt_loc (EXPR_LOCATION (orig_lhs1),
6691 code, orig_lhs1, stmt);
6692 OMP_ATOMIC_SEQ_CST (stmt) = seq_cst;
6693 stmt = build2 (MODIFY_EXPR, void_type_node, orig_v, stmt);
6696 stmt = build2 (OMP_ATOMIC, void_type_node, integer_zero_node, stmt);
6697 OMP_ATOMIC_SEQ_CST (stmt) = seq_cst;
6699 finish_expr_stmt (stmt);
6702 void
6703 finish_omp_barrier (void)
6705 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_BARRIER);
6706 vec<tree, va_gc> *vec = make_tree_vector ();
6707 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
6708 release_tree_vector (vec);
6709 finish_expr_stmt (stmt);
6712 void
6713 finish_omp_flush (void)
6715 tree fn = builtin_decl_explicit (BUILT_IN_SYNC_SYNCHRONIZE);
6716 vec<tree, va_gc> *vec = make_tree_vector ();
6717 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
6718 release_tree_vector (vec);
6719 finish_expr_stmt (stmt);
6722 void
6723 finish_omp_taskwait (void)
6725 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_TASKWAIT);
6726 vec<tree, va_gc> *vec = make_tree_vector ();
6727 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
6728 release_tree_vector (vec);
6729 finish_expr_stmt (stmt);
6732 void
6733 finish_omp_taskyield (void)
6735 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_TASKYIELD);
6736 vec<tree, va_gc> *vec = make_tree_vector ();
6737 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
6738 release_tree_vector (vec);
6739 finish_expr_stmt (stmt);
6742 void
6743 finish_omp_cancel (tree clauses)
6745 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
6746 int mask = 0;
6747 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
6748 mask = 1;
6749 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
6750 mask = 2;
6751 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
6752 mask = 4;
6753 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
6754 mask = 8;
6755 else
6757 error ("%<#pragma omp cancel must specify one of "
6758 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> clauses");
6759 return;
6761 vec<tree, va_gc> *vec = make_tree_vector ();
6762 tree ifc = find_omp_clause (clauses, OMP_CLAUSE_IF);
6763 if (ifc != NULL_TREE)
6765 tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
6766 ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
6767 boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
6768 build_zero_cst (type));
6770 else
6771 ifc = boolean_true_node;
6772 vec->quick_push (build_int_cst (integer_type_node, mask));
6773 vec->quick_push (ifc);
6774 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
6775 release_tree_vector (vec);
6776 finish_expr_stmt (stmt);
6779 void
6780 finish_omp_cancellation_point (tree clauses)
6782 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
6783 int mask = 0;
6784 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
6785 mask = 1;
6786 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
6787 mask = 2;
6788 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
6789 mask = 4;
6790 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
6791 mask = 8;
6792 else
6794 error ("%<#pragma omp cancellation point must specify one of "
6795 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> clauses");
6796 return;
6798 vec<tree, va_gc> *vec
6799 = make_tree_vector_single (build_int_cst (integer_type_node, mask));
6800 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
6801 release_tree_vector (vec);
6802 finish_expr_stmt (stmt);
6805 /* Begin a __transaction_atomic or __transaction_relaxed statement.
6806 If PCOMPOUND is non-null, this is for a function-transaction-block, and we
6807 should create an extra compound stmt. */
6809 tree
6810 begin_transaction_stmt (location_t loc, tree *pcompound, int flags)
6812 tree r;
6814 if (pcompound)
6815 *pcompound = begin_compound_stmt (0);
6817 r = build_stmt (loc, TRANSACTION_EXPR, NULL_TREE);
6819 /* Only add the statement to the function if support enabled. */
6820 if (flag_tm)
6821 add_stmt (r);
6822 else
6823 error_at (loc, ((flags & TM_STMT_ATTR_RELAXED) != 0
6824 ? G_("%<__transaction_relaxed%> without "
6825 "transactional memory support enabled")
6826 : G_("%<__transaction_atomic%> without "
6827 "transactional memory support enabled")));
6829 TRANSACTION_EXPR_BODY (r) = push_stmt_list ();
6830 TREE_SIDE_EFFECTS (r) = 1;
6831 return r;
6834 /* End a __transaction_atomic or __transaction_relaxed statement.
6835 If COMPOUND_STMT is non-null, this is for a function-transaction-block,
6836 and we should end the compound. If NOEX is non-NULL, we wrap the body in
6837 a MUST_NOT_THROW_EXPR with NOEX as condition. */
6839 void
6840 finish_transaction_stmt (tree stmt, tree compound_stmt, int flags, tree noex)
6842 TRANSACTION_EXPR_BODY (stmt) = pop_stmt_list (TRANSACTION_EXPR_BODY (stmt));
6843 TRANSACTION_EXPR_OUTER (stmt) = (flags & TM_STMT_ATTR_OUTER) != 0;
6844 TRANSACTION_EXPR_RELAXED (stmt) = (flags & TM_STMT_ATTR_RELAXED) != 0;
6845 TRANSACTION_EXPR_IS_STMT (stmt) = 1;
6847 /* noexcept specifications are not allowed for function transactions. */
6848 gcc_assert (!(noex && compound_stmt));
6849 if (noex)
6851 tree body = build_must_not_throw_expr (TRANSACTION_EXPR_BODY (stmt),
6852 noex);
6853 /* This may not be true when the STATEMENT_LIST is empty. */
6854 if (EXPR_P (body))
6855 SET_EXPR_LOCATION (body, EXPR_LOCATION (TRANSACTION_EXPR_BODY (stmt)));
6856 TREE_SIDE_EFFECTS (body) = 1;
6857 TRANSACTION_EXPR_BODY (stmt) = body;
6860 if (compound_stmt)
6861 finish_compound_stmt (compound_stmt);
6864 /* Build a __transaction_atomic or __transaction_relaxed expression. If
6865 NOEX is non-NULL, we wrap the body in a MUST_NOT_THROW_EXPR with NOEX as
6866 condition. */
6868 tree
6869 build_transaction_expr (location_t loc, tree expr, int flags, tree noex)
6871 tree ret;
6872 if (noex)
6874 expr = build_must_not_throw_expr (expr, noex);
6875 if (EXPR_P (expr))
6876 SET_EXPR_LOCATION (expr, loc);
6877 TREE_SIDE_EFFECTS (expr) = 1;
6879 ret = build1 (TRANSACTION_EXPR, TREE_TYPE (expr), expr);
6880 if (flags & TM_STMT_ATTR_RELAXED)
6881 TRANSACTION_EXPR_RELAXED (ret) = 1;
6882 TREE_SIDE_EFFECTS (ret) = 1;
6883 SET_EXPR_LOCATION (ret, loc);
6884 return ret;
6887 void
6888 init_cp_semantics (void)
6892 /* Build a STATIC_ASSERT for a static assertion with the condition
6893 CONDITION and the message text MESSAGE. LOCATION is the location
6894 of the static assertion in the source code. When MEMBER_P, this
6895 static assertion is a member of a class. */
6896 void
6897 finish_static_assert (tree condition, tree message, location_t location,
6898 bool member_p)
6900 if (message == NULL_TREE
6901 || message == error_mark_node
6902 || condition == NULL_TREE
6903 || condition == error_mark_node)
6904 return;
6906 if (check_for_bare_parameter_packs (condition))
6907 condition = error_mark_node;
6909 if (type_dependent_expression_p (condition)
6910 || value_dependent_expression_p (condition))
6912 /* We're in a template; build a STATIC_ASSERT and put it in
6913 the right place. */
6914 tree assertion;
6916 assertion = make_node (STATIC_ASSERT);
6917 STATIC_ASSERT_CONDITION (assertion) = condition;
6918 STATIC_ASSERT_MESSAGE (assertion) = message;
6919 STATIC_ASSERT_SOURCE_LOCATION (assertion) = location;
6921 if (member_p)
6922 maybe_add_class_template_decl_list (current_class_type,
6923 assertion,
6924 /*friend_p=*/0);
6925 else
6926 add_stmt (assertion);
6928 return;
6931 /* Fold the expression and convert it to a boolean value. */
6932 condition = fold_non_dependent_expr (condition);
6933 condition = cp_convert (boolean_type_node, condition, tf_warning_or_error);
6934 condition = maybe_constant_value (condition);
6936 if (TREE_CODE (condition) == INTEGER_CST && !integer_zerop (condition))
6937 /* Do nothing; the condition is satisfied. */
6939 else
6941 location_t saved_loc = input_location;
6943 input_location = location;
6944 if (TREE_CODE (condition) == INTEGER_CST
6945 && integer_zerop (condition))
6946 /* Report the error. */
6947 error ("static assertion failed: %s", TREE_STRING_POINTER (message));
6948 else if (condition && condition != error_mark_node)
6950 error ("non-constant condition for static assertion");
6951 if (require_potential_rvalue_constant_expression (condition))
6952 cxx_constant_value (condition);
6954 input_location = saved_loc;
6958 /* Implements the C++0x decltype keyword. Returns the type of EXPR,
6959 suitable for use as a type-specifier.
6961 ID_EXPRESSION_OR_MEMBER_ACCESS_P is true when EXPR was parsed as an
6962 id-expression or a class member access, FALSE when it was parsed as
6963 a full expression. */
6965 tree
6966 finish_decltype_type (tree expr, bool id_expression_or_member_access_p,
6967 tsubst_flags_t complain)
6969 tree type = NULL_TREE;
6971 if (!expr || error_operand_p (expr))
6972 return error_mark_node;
6974 if (TYPE_P (expr)
6975 || TREE_CODE (expr) == TYPE_DECL
6976 || (TREE_CODE (expr) == BIT_NOT_EXPR
6977 && TYPE_P (TREE_OPERAND (expr, 0))))
6979 if (complain & tf_error)
6980 error ("argument to decltype must be an expression");
6981 return error_mark_node;
6984 /* Depending on the resolution of DR 1172, we may later need to distinguish
6985 instantiation-dependent but not type-dependent expressions so that, say,
6986 A<decltype(sizeof(T))>::U doesn't require 'typename'. */
6987 if (instantiation_dependent_expression_p (expr))
6989 type = cxx_make_type (DECLTYPE_TYPE);
6990 DECLTYPE_TYPE_EXPR (type) = expr;
6991 DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (type)
6992 = id_expression_or_member_access_p;
6993 SET_TYPE_STRUCTURAL_EQUALITY (type);
6995 return type;
6998 /* The type denoted by decltype(e) is defined as follows: */
7000 expr = resolve_nondeduced_context (expr);
7002 if (invalid_nonstatic_memfn_p (expr, complain))
7003 return error_mark_node;
7005 if (type_unknown_p (expr))
7007 if (complain & tf_error)
7008 error ("decltype cannot resolve address of overloaded function");
7009 return error_mark_node;
7012 /* To get the size of a static data member declared as an array of
7013 unknown bound, we need to instantiate it. */
7014 if (VAR_P (expr)
7015 && VAR_HAD_UNKNOWN_BOUND (expr)
7016 && DECL_TEMPLATE_INSTANTIATION (expr))
7017 instantiate_decl (expr, /*defer_ok*/true, /*expl_inst_mem*/false);
7019 if (id_expression_or_member_access_p)
7021 /* If e is an id-expression or a class member access (5.2.5
7022 [expr.ref]), decltype(e) is defined as the type of the entity
7023 named by e. If there is no such entity, or e names a set of
7024 overloaded functions, the program is ill-formed. */
7025 if (identifier_p (expr))
7026 expr = lookup_name (expr);
7028 if (INDIRECT_REF_P (expr))
7029 /* This can happen when the expression is, e.g., "a.b". Just
7030 look at the underlying operand. */
7031 expr = TREE_OPERAND (expr, 0);
7033 if (TREE_CODE (expr) == OFFSET_REF
7034 || TREE_CODE (expr) == MEMBER_REF
7035 || TREE_CODE (expr) == SCOPE_REF)
7036 /* We're only interested in the field itself. If it is a
7037 BASELINK, we will need to see through it in the next
7038 step. */
7039 expr = TREE_OPERAND (expr, 1);
7041 if (BASELINK_P (expr))
7042 /* See through BASELINK nodes to the underlying function. */
7043 expr = BASELINK_FUNCTIONS (expr);
7045 switch (TREE_CODE (expr))
7047 case FIELD_DECL:
7048 if (DECL_BIT_FIELD_TYPE (expr))
7050 type = DECL_BIT_FIELD_TYPE (expr);
7051 break;
7053 /* Fall through for fields that aren't bitfields. */
7055 case FUNCTION_DECL:
7056 case VAR_DECL:
7057 case CONST_DECL:
7058 case PARM_DECL:
7059 case RESULT_DECL:
7060 case TEMPLATE_PARM_INDEX:
7061 expr = mark_type_use (expr);
7062 type = TREE_TYPE (expr);
7063 break;
7065 case ERROR_MARK:
7066 type = error_mark_node;
7067 break;
7069 case COMPONENT_REF:
7070 case COMPOUND_EXPR:
7071 mark_type_use (expr);
7072 type = is_bitfield_expr_with_lowered_type (expr);
7073 if (!type)
7074 type = TREE_TYPE (TREE_OPERAND (expr, 1));
7075 break;
7077 case BIT_FIELD_REF:
7078 gcc_unreachable ();
7080 case INTEGER_CST:
7081 case PTRMEM_CST:
7082 /* We can get here when the id-expression refers to an
7083 enumerator or non-type template parameter. */
7084 type = TREE_TYPE (expr);
7085 break;
7087 default:
7088 /* Handle instantiated template non-type arguments. */
7089 type = TREE_TYPE (expr);
7090 break;
7093 else
7095 /* Within a lambda-expression:
7097 Every occurrence of decltype((x)) where x is a possibly
7098 parenthesized id-expression that names an entity of
7099 automatic storage duration is treated as if x were
7100 transformed into an access to a corresponding data member
7101 of the closure type that would have been declared if x
7102 were a use of the denoted entity. */
7103 if (outer_automatic_var_p (expr)
7104 && current_function_decl
7105 && LAMBDA_FUNCTION_P (current_function_decl))
7106 type = capture_decltype (expr);
7107 else if (error_operand_p (expr))
7108 type = error_mark_node;
7109 else if (expr == current_class_ptr)
7110 /* If the expression is just "this", we want the
7111 cv-unqualified pointer for the "this" type. */
7112 type = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
7113 else
7115 /* Otherwise, where T is the type of e, if e is an lvalue,
7116 decltype(e) is defined as T&; if an xvalue, T&&; otherwise, T. */
7117 cp_lvalue_kind clk = lvalue_kind (expr);
7118 type = unlowered_expr_type (expr);
7119 gcc_assert (TREE_CODE (type) != REFERENCE_TYPE);
7121 /* For vector types, pick a non-opaque variant. */
7122 if (TREE_CODE (type) == VECTOR_TYPE)
7123 type = strip_typedefs (type);
7125 if (clk != clk_none && !(clk & clk_class))
7126 type = cp_build_reference_type (type, (clk & clk_rvalueref));
7130 if (cxx_dialect >= cxx1y && array_of_runtime_bound_p (type)
7131 && (flag_iso || warn_vla > 0))
7133 if (complain & tf_warning_or_error)
7134 pedwarn (input_location, OPT_Wvla,
7135 "taking decltype of array of runtime bound");
7136 else
7137 return error_mark_node;
7140 return type;
7143 /* Called from trait_expr_value to evaluate either __has_nothrow_assign or
7144 __has_nothrow_copy, depending on assign_p. */
7146 static bool
7147 classtype_has_nothrow_assign_or_copy_p (tree type, bool assign_p)
7149 tree fns;
7151 if (assign_p)
7153 int ix;
7154 ix = lookup_fnfields_1 (type, ansi_assopname (NOP_EXPR));
7155 if (ix < 0)
7156 return false;
7157 fns = (*CLASSTYPE_METHOD_VEC (type))[ix];
7159 else if (TYPE_HAS_COPY_CTOR (type))
7161 /* If construction of the copy constructor was postponed, create
7162 it now. */
7163 if (CLASSTYPE_LAZY_COPY_CTOR (type))
7164 lazily_declare_fn (sfk_copy_constructor, type);
7165 if (CLASSTYPE_LAZY_MOVE_CTOR (type))
7166 lazily_declare_fn (sfk_move_constructor, type);
7167 fns = CLASSTYPE_CONSTRUCTORS (type);
7169 else
7170 return false;
7172 for (; fns; fns = OVL_NEXT (fns))
7174 tree fn = OVL_CURRENT (fns);
7176 if (assign_p)
7178 if (copy_fn_p (fn) == 0)
7179 continue;
7181 else if (copy_fn_p (fn) <= 0)
7182 continue;
7184 maybe_instantiate_noexcept (fn);
7185 if (!TYPE_NOTHROW_P (TREE_TYPE (fn)))
7186 return false;
7189 return true;
7192 // Returns true if K denotes a unary type trait.
7193 inline bool
7194 is_unary_trait (cp_trait_kind k)
7196 if (k == CPTK_IS_CONVERTIBLE_TO || k == CPTK_IS_BASE_OF)
7197 return false;
7198 return true;
7201 // Returns true if K denotes a binary type trait.
7202 bool
7203 is_binary_trait (cp_trait_kind k)
7205 return !is_unary_trait (k);
7208 // Returns a type for T that can be used as an xvalue. For function
7209 // types, this returns an rvalue reference to T. For all other types,
7210 // this simply returns T.
7211 tree
7212 xvalue_result_type (tree t)
7214 if (TREE_CODE (t) == FUNCTION_TYPE)
7215 return cp_build_reference_type(t, true);
7216 else
7217 return t;
7220 /* Actually evaluates the trait. */
7222 static bool
7223 trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
7225 enum tree_code type_code1;
7226 tree t;
7228 type_code1 = TREE_CODE (type1);
7230 switch (kind)
7232 case CPTK_HAS_NOTHROW_ASSIGN:
7233 type1 = strip_array_types (type1);
7234 return (!CP_TYPE_CONST_P (type1) && type_code1 != REFERENCE_TYPE
7235 && (trait_expr_value (CPTK_HAS_TRIVIAL_ASSIGN, type1, type2)
7236 || (CLASS_TYPE_P (type1)
7237 && classtype_has_nothrow_assign_or_copy_p (type1,
7238 true))));
7240 case CPTK_HAS_TRIVIAL_ASSIGN:
7241 /* ??? The standard seems to be missing the "or array of such a class
7242 type" wording for this trait. */
7243 type1 = strip_array_types (type1);
7244 return (!CP_TYPE_CONST_P (type1) && type_code1 != REFERENCE_TYPE
7245 && (trivial_type_p (type1)
7246 || (CLASS_TYPE_P (type1)
7247 && TYPE_HAS_TRIVIAL_COPY_ASSIGN (type1))));
7249 case CPTK_HAS_NOTHROW_CONSTRUCTOR:
7250 type1 = strip_array_types (type1);
7251 return (trait_expr_value (CPTK_HAS_TRIVIAL_CONSTRUCTOR, type1, type2)
7252 || (CLASS_TYPE_P (type1)
7253 && (t = locate_ctor (type1))
7254 && (maybe_instantiate_noexcept (t),
7255 TYPE_NOTHROW_P (TREE_TYPE (t)))));
7257 case CPTK_HAS_TRIVIAL_CONSTRUCTOR:
7258 type1 = strip_array_types (type1);
7259 return (trivial_type_p (type1)
7260 || (CLASS_TYPE_P (type1) && TYPE_HAS_TRIVIAL_DFLT (type1)));
7262 case CPTK_HAS_NOTHROW_COPY:
7263 type1 = strip_array_types (type1);
7264 return (trait_expr_value (CPTK_HAS_TRIVIAL_COPY, type1, type2)
7265 || (CLASS_TYPE_P (type1)
7266 && classtype_has_nothrow_assign_or_copy_p (type1, false)));
7268 case CPTK_HAS_TRIVIAL_COPY:
7269 /* ??? The standard seems to be missing the "or array of such a class
7270 type" wording for this trait. */
7271 type1 = strip_array_types (type1);
7272 return (trivial_type_p (type1) || type_code1 == REFERENCE_TYPE
7273 || (CLASS_TYPE_P (type1) && TYPE_HAS_TRIVIAL_COPY_CTOR (type1)));
7275 case CPTK_HAS_TRIVIAL_DESTRUCTOR:
7276 type1 = strip_array_types (type1);
7277 return (trivial_type_p (type1) || type_code1 == REFERENCE_TYPE
7278 || (CLASS_TYPE_P (type1)
7279 && TYPE_HAS_TRIVIAL_DESTRUCTOR (type1)));
7281 case CPTK_HAS_VIRTUAL_DESTRUCTOR:
7282 return type_has_virtual_destructor (type1);
7284 case CPTK_IS_ABSTRACT:
7285 return (ABSTRACT_CLASS_TYPE_P (type1));
7287 case CPTK_IS_BASE_OF:
7288 return (NON_UNION_CLASS_TYPE_P (type1) && NON_UNION_CLASS_TYPE_P (type2)
7289 && (same_type_ignoring_top_level_qualifiers_p (type1, type2)
7290 || DERIVED_FROM_P (type1, type2)));
7292 case CPTK_IS_CLASS:
7293 return (NON_UNION_CLASS_TYPE_P (type1));
7295 case CPTK_IS_CONVERTIBLE_TO:
7296 return can_convert (type2, xvalue_result_type (type1), tf_none);
7298 case CPTK_IS_EMPTY:
7299 return (NON_UNION_CLASS_TYPE_P (type1) && CLASSTYPE_EMPTY_P (type1));
7301 case CPTK_IS_ENUM:
7302 return (type_code1 == ENUMERAL_TYPE);
7304 case CPTK_IS_FINAL:
7305 return (CLASS_TYPE_P (type1) && CLASSTYPE_FINAL (type1));
7307 case CPTK_IS_LITERAL_TYPE:
7308 return (literal_type_p (type1));
7310 case CPTK_IS_POD:
7311 return (pod_type_p (type1));
7313 case CPTK_IS_POLYMORPHIC:
7314 return (CLASS_TYPE_P (type1) && TYPE_POLYMORPHIC_P (type1));
7316 case CPTK_IS_SAME_AS:
7317 return same_type_p (type1, type2);
7319 case CPTK_IS_STD_LAYOUT:
7320 return (std_layout_type_p (type1));
7322 case CPTK_IS_TRIVIAL:
7323 return (trivial_type_p (type1));
7325 case CPTK_IS_UNION:
7326 return (type_code1 == UNION_TYPE);
7328 default:
7329 gcc_unreachable ();
7330 return false;
7334 /* If TYPE is an array of unknown bound, or (possibly cv-qualified)
7335 void, or a complete type, returns it, otherwise NULL_TREE. */
7337 static tree
7338 check_trait_type (tree type)
7340 if (TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type)
7341 && COMPLETE_TYPE_P (TREE_TYPE (type)))
7342 return type;
7344 if (VOID_TYPE_P (type))
7345 return type;
7347 return complete_type_or_else (strip_array_types (type), NULL_TREE);
7350 /* Process a trait expression. */
7352 tree
7353 finish_trait_expr (cp_trait_kind kind, tree type1, tree type2)
7355 gcc_assert (kind == CPTK_HAS_NOTHROW_ASSIGN
7356 || kind == CPTK_HAS_NOTHROW_CONSTRUCTOR
7357 || kind == CPTK_HAS_NOTHROW_COPY
7358 || kind == CPTK_HAS_TRIVIAL_ASSIGN
7359 || kind == CPTK_HAS_TRIVIAL_CONSTRUCTOR
7360 || kind == CPTK_HAS_TRIVIAL_COPY
7361 || kind == CPTK_HAS_TRIVIAL_DESTRUCTOR
7362 || kind == CPTK_HAS_VIRTUAL_DESTRUCTOR
7363 || kind == CPTK_IS_ABSTRACT
7364 || kind == CPTK_IS_BASE_OF
7365 || kind == CPTK_IS_CLASS
7366 || kind == CPTK_IS_CONVERTIBLE_TO
7367 || kind == CPTK_IS_EMPTY
7368 || kind == CPTK_IS_ENUM
7369 || kind == CPTK_IS_FINAL
7370 || kind == CPTK_IS_LITERAL_TYPE
7371 || kind == CPTK_IS_POD
7372 || kind == CPTK_IS_POLYMORPHIC
7373 || kind == CPTK_IS_SAME_AS
7374 || kind == CPTK_IS_STD_LAYOUT
7375 || kind == CPTK_IS_TRIVIAL
7376 || kind == CPTK_IS_UNION);
7378 if (type1 == error_mark_node
7379 || (is_binary_trait (kind) && type2 == error_mark_node))
7380 return error_mark_node;
7382 if (processing_template_decl)
7384 tree trait_expr = make_node (TRAIT_EXPR);
7385 TREE_TYPE (trait_expr) = boolean_type_node;
7386 TRAIT_EXPR_TYPE1 (trait_expr) = type1;
7387 TRAIT_EXPR_TYPE2 (trait_expr) = type2;
7388 TRAIT_EXPR_KIND (trait_expr) = kind;
7389 return trait_expr;
7392 switch (kind)
7394 case CPTK_HAS_NOTHROW_ASSIGN:
7395 case CPTK_HAS_TRIVIAL_ASSIGN:
7396 case CPTK_HAS_NOTHROW_CONSTRUCTOR:
7397 case CPTK_HAS_TRIVIAL_CONSTRUCTOR:
7398 case CPTK_HAS_NOTHROW_COPY:
7399 case CPTK_HAS_TRIVIAL_COPY:
7400 case CPTK_HAS_TRIVIAL_DESTRUCTOR:
7401 case CPTK_HAS_VIRTUAL_DESTRUCTOR:
7402 case CPTK_IS_ABSTRACT:
7403 case CPTK_IS_EMPTY:
7404 case CPTK_IS_FINAL:
7405 case CPTK_IS_LITERAL_TYPE:
7406 case CPTK_IS_POD:
7407 case CPTK_IS_POLYMORPHIC:
7408 case CPTK_IS_STD_LAYOUT:
7409 case CPTK_IS_TRIVIAL:
7410 if (!check_trait_type (type1))
7411 return error_mark_node;
7412 break;
7414 case CPTK_IS_BASE_OF:
7415 if (NON_UNION_CLASS_TYPE_P (type1) && NON_UNION_CLASS_TYPE_P (type2)
7416 && !same_type_ignoring_top_level_qualifiers_p (type1, type2)
7417 && !complete_type_or_else (type2, NULL_TREE))
7418 /* We already issued an error. */
7419 return error_mark_node;
7420 break;
7422 case CPTK_IS_CLASS:
7423 case CPTK_IS_ENUM:
7424 case CPTK_IS_UNION:
7425 case CPTK_IS_SAME_AS:
7426 break;
7428 case CPTK_IS_CONVERTIBLE_TO:
7429 if (!check_trait_type (type1))
7430 return error_mark_node;
7431 if (!check_trait_type (type2))
7432 return error_mark_node;
7433 break;
7435 default:
7436 gcc_unreachable ();
7439 return (trait_expr_value (kind, type1, type2)
7440 ? boolean_true_node : boolean_false_node);
7443 /* Do-nothing variants of functions to handle pragma FLOAT_CONST_DECIMAL64,
7444 which is ignored for C++. */
7446 void
7447 set_float_const_decimal64 (void)
7451 void
7452 clear_float_const_decimal64 (void)
7456 bool
7457 float_const_decimal64_p (void)
7459 return 0;
7463 /* Return true if T is a literal type. */
7465 bool
7466 literal_type_p (tree t)
7468 if (SCALAR_TYPE_P (t)
7469 || TREE_CODE (t) == VECTOR_TYPE
7470 || TREE_CODE (t) == REFERENCE_TYPE)
7471 return true;
7472 if (CLASS_TYPE_P (t))
7474 t = complete_type (t);
7475 gcc_assert (COMPLETE_TYPE_P (t) || errorcount);
7476 return CLASSTYPE_LITERAL_P (t);
7478 if (TREE_CODE (t) == ARRAY_TYPE)
7479 return literal_type_p (strip_array_types (t));
7480 return false;
7483 /* If DECL is a variable declared `constexpr', require its type
7484 be literal. Return the DECL if OK, otherwise NULL. */
7486 tree
7487 ensure_literal_type_for_constexpr_object (tree decl)
7489 tree type = TREE_TYPE (decl);
7490 if (VAR_P (decl) && DECL_DECLARED_CONSTEXPR_P (decl)
7491 && !processing_template_decl)
7493 tree stype = strip_array_types (type);
7494 if (CLASS_TYPE_P (stype) && !COMPLETE_TYPE_P (complete_type (stype)))
7495 /* Don't complain here, we'll complain about incompleteness
7496 when we try to initialize the variable. */;
7497 else if (!literal_type_p (type))
7499 error ("the type %qT of constexpr variable %qD is not literal",
7500 type, decl);
7501 explain_non_literal_class (type);
7502 return NULL;
7505 return decl;
7508 /* Representation of entries in the constexpr function definition table. */
7510 typedef struct GTY(()) constexpr_fundef {
7511 tree decl;
7512 tree body;
7513 } constexpr_fundef;
7515 /* This table holds all constexpr function definitions seen in
7516 the current translation unit. */
7518 static GTY ((param_is (constexpr_fundef))) htab_t constexpr_fundef_table;
7520 /* Utility function used for managing the constexpr function table.
7521 Return true if the entries pointed to by P and Q are for the
7522 same constexpr function. */
7524 static inline int
7525 constexpr_fundef_equal (const void *p, const void *q)
7527 const constexpr_fundef *lhs = (const constexpr_fundef *) p;
7528 const constexpr_fundef *rhs = (const constexpr_fundef *) q;
7529 return lhs->decl == rhs->decl;
7532 /* Utility function used for managing the constexpr function table.
7533 Return a hash value for the entry pointed to by Q. */
7535 static inline hashval_t
7536 constexpr_fundef_hash (const void *p)
7538 const constexpr_fundef *fundef = (const constexpr_fundef *) p;
7539 return DECL_UID (fundef->decl);
7542 /* Return a previously saved definition of function FUN. */
7544 static constexpr_fundef *
7545 retrieve_constexpr_fundef (tree fun)
7547 constexpr_fundef fundef = { NULL, NULL };
7548 if (constexpr_fundef_table == NULL)
7549 return NULL;
7551 fundef.decl = fun;
7552 return (constexpr_fundef *) htab_find (constexpr_fundef_table, &fundef);
7555 /* Check whether the parameter and return types of FUN are valid for a
7556 constexpr function, and complain if COMPLAIN. */
7558 static bool
7559 is_valid_constexpr_fn (tree fun, bool complain)
7561 bool ret = true;
7563 if (DECL_INHERITED_CTOR_BASE (fun)
7564 && TREE_CODE (fun) == TEMPLATE_DECL)
7566 ret = false;
7567 if (complain)
7568 error ("inherited constructor %qD is not constexpr",
7569 get_inherited_ctor (fun));
7571 else
7573 for (tree parm = FUNCTION_FIRST_USER_PARM (fun);
7574 parm != NULL_TREE; parm = TREE_CHAIN (parm))
7575 if (!literal_type_p (TREE_TYPE (parm)))
7577 ret = false;
7578 if (complain)
7580 error ("invalid type for parameter %d of constexpr "
7581 "function %q+#D", DECL_PARM_INDEX (parm), fun);
7582 explain_non_literal_class (TREE_TYPE (parm));
7587 if (!DECL_CONSTRUCTOR_P (fun))
7589 tree rettype = TREE_TYPE (TREE_TYPE (fun));
7590 if (!literal_type_p (rettype))
7592 ret = false;
7593 if (complain)
7595 error ("invalid return type %qT of constexpr function %q+D",
7596 rettype, fun);
7597 explain_non_literal_class (rettype);
7601 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fun)
7602 && !CLASSTYPE_LITERAL_P (DECL_CONTEXT (fun)))
7604 ret = false;
7605 if (complain)
7607 error ("enclosing class of constexpr non-static member "
7608 "function %q+#D is not a literal type", fun);
7609 explain_non_literal_class (DECL_CONTEXT (fun));
7613 else if (CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fun)))
7615 ret = false;
7616 if (complain)
7617 error ("%q#T has virtual base classes", DECL_CONTEXT (fun));
7620 return ret;
7623 /* Subroutine of build_data_member_initialization. MEMBER is a COMPONENT_REF
7624 for a member of an anonymous aggregate, INIT is the initializer for that
7625 member, and VEC_OUTER is the vector of constructor elements for the class
7626 whose constructor we are processing. Add the initializer to the vector
7627 and return true to indicate success. */
7629 static bool
7630 build_anon_member_initialization (tree member, tree init,
7631 vec<constructor_elt, va_gc> **vec_outer)
7633 /* MEMBER presents the relevant fields from the inside out, but we need
7634 to build up the initializer from the outside in so that we can reuse
7635 previously built CONSTRUCTORs if this is, say, the second field in an
7636 anonymous struct. So we use a vec as a stack. */
7637 auto_vec<tree, 2> fields;
7640 fields.safe_push (TREE_OPERAND (member, 1));
7641 member = TREE_OPERAND (member, 0);
7643 while (ANON_AGGR_TYPE_P (TREE_TYPE (member))
7644 && TREE_CODE (member) == COMPONENT_REF);
7646 /* VEC has the constructor elements vector for the context of FIELD.
7647 If FIELD is an anonymous aggregate, we will push inside it. */
7648 vec<constructor_elt, va_gc> **vec = vec_outer;
7649 tree field;
7650 while (field = fields.pop(),
7651 ANON_AGGR_TYPE_P (TREE_TYPE (field)))
7653 tree ctor;
7654 /* If there is already an outer constructor entry for the anonymous
7655 aggregate FIELD, use it; otherwise, insert one. */
7656 if (vec_safe_is_empty (*vec)
7657 || (*vec)->last().index != field)
7659 ctor = build_constructor (TREE_TYPE (field), NULL);
7660 CONSTRUCTOR_APPEND_ELT (*vec, field, ctor);
7662 else
7663 ctor = (*vec)->last().value;
7664 vec = &CONSTRUCTOR_ELTS (ctor);
7667 /* Now we're at the innermost field, the one that isn't an anonymous
7668 aggregate. Add its initializer to the CONSTRUCTOR and we're done. */
7669 gcc_assert (fields.is_empty());
7670 CONSTRUCTOR_APPEND_ELT (*vec, field, init);
7672 return true;
7675 /* Subroutine of build_constexpr_constructor_member_initializers.
7676 The expression tree T represents a data member initialization
7677 in a (constexpr) constructor definition. Build a pairing of
7678 the data member with its initializer, and prepend that pair
7679 to the existing initialization pair INITS. */
7681 static bool
7682 build_data_member_initialization (tree t, vec<constructor_elt, va_gc> **vec)
7684 tree member, init;
7685 if (TREE_CODE (t) == CLEANUP_POINT_EXPR)
7686 t = TREE_OPERAND (t, 0);
7687 if (TREE_CODE (t) == EXPR_STMT)
7688 t = TREE_OPERAND (t, 0);
7689 if (t == error_mark_node)
7690 return false;
7691 if (TREE_CODE (t) == STATEMENT_LIST)
7693 tree_stmt_iterator i;
7694 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
7696 if (! build_data_member_initialization (tsi_stmt (i), vec))
7697 return false;
7699 return true;
7701 if (TREE_CODE (t) == CLEANUP_STMT)
7703 /* We can't see a CLEANUP_STMT in a constructor for a literal class,
7704 but we can in a constexpr constructor for a non-literal class. Just
7705 ignore it; either all the initialization will be constant, in which
7706 case the cleanup can't run, or it can't be constexpr.
7707 Still recurse into CLEANUP_BODY. */
7708 return build_data_member_initialization (CLEANUP_BODY (t), vec);
7710 if (TREE_CODE (t) == CONVERT_EXPR)
7711 t = TREE_OPERAND (t, 0);
7712 if (TREE_CODE (t) == INIT_EXPR
7713 || TREE_CODE (t) == MODIFY_EXPR)
7715 member = TREE_OPERAND (t, 0);
7716 init = break_out_target_exprs (TREE_OPERAND (t, 1));
7718 else if (TREE_CODE (t) == CALL_EXPR)
7720 member = CALL_EXPR_ARG (t, 0);
7721 /* We don't use build_cplus_new here because it complains about
7722 abstract bases. Leaving the call unwrapped means that it has the
7723 wrong type, but cxx_eval_constant_expression doesn't care. */
7724 init = break_out_target_exprs (t);
7726 else if (TREE_CODE (t) == DECL_EXPR)
7727 /* Declaring a temporary, don't add it to the CONSTRUCTOR. */
7728 return true;
7729 else
7730 gcc_unreachable ();
7731 if (INDIRECT_REF_P (member))
7732 member = TREE_OPERAND (member, 0);
7733 if (TREE_CODE (member) == NOP_EXPR)
7735 tree op = member;
7736 STRIP_NOPS (op);
7737 if (TREE_CODE (op) == ADDR_EXPR)
7739 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7740 (TREE_TYPE (TREE_TYPE (op)),
7741 TREE_TYPE (TREE_TYPE (member))));
7742 /* Initializing a cv-qualified member; we need to look through
7743 the const_cast. */
7744 member = op;
7746 else if (op == current_class_ptr
7747 && (same_type_ignoring_top_level_qualifiers_p
7748 (TREE_TYPE (TREE_TYPE (member)),
7749 current_class_type)))
7750 /* Delegating constructor. */
7751 member = op;
7752 else
7754 /* This is an initializer for an empty base; keep it for now so
7755 we can check it in cxx_eval_bare_aggregate. */
7756 gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (member))));
7759 if (TREE_CODE (member) == ADDR_EXPR)
7760 member = TREE_OPERAND (member, 0);
7761 if (TREE_CODE (member) == COMPONENT_REF)
7763 tree aggr = TREE_OPERAND (member, 0);
7764 if (TREE_CODE (aggr) != COMPONENT_REF)
7765 /* Normal member initialization. */
7766 member = TREE_OPERAND (member, 1);
7767 else if (ANON_AGGR_TYPE_P (TREE_TYPE (aggr)))
7768 /* Initializing a member of an anonymous union. */
7769 return build_anon_member_initialization (member, init, vec);
7770 else
7771 /* We're initializing a vtable pointer in a base. Leave it as
7772 COMPONENT_REF so we remember the path to get to the vfield. */
7773 gcc_assert (TREE_TYPE (member) == vtbl_ptr_type_node);
7776 CONSTRUCTOR_APPEND_ELT (*vec, member, init);
7777 return true;
7780 /* Make sure that there are no statements after LAST in the constructor
7781 body represented by LIST. */
7783 bool
7784 check_constexpr_ctor_body (tree last, tree list)
7786 bool ok = true;
7787 if (TREE_CODE (list) == STATEMENT_LIST)
7789 tree_stmt_iterator i = tsi_last (list);
7790 for (; !tsi_end_p (i); tsi_prev (&i))
7792 tree t = tsi_stmt (i);
7793 if (t == last)
7794 break;
7795 if (TREE_CODE (t) == BIND_EXPR)
7797 if (BIND_EXPR_VARS (t))
7799 ok = false;
7800 break;
7802 if (!check_constexpr_ctor_body (last, BIND_EXPR_BODY (t)))
7803 return false;
7804 else
7805 continue;
7807 /* We currently allow typedefs and static_assert.
7808 FIXME allow them in the standard, too. */
7809 if (TREE_CODE (t) != STATIC_ASSERT)
7811 ok = false;
7812 break;
7816 else if (list != last
7817 && TREE_CODE (list) != STATIC_ASSERT)
7818 ok = false;
7819 if (!ok)
7821 error ("constexpr constructor does not have empty body");
7822 DECL_DECLARED_CONSTEXPR_P (current_function_decl) = false;
7824 return ok;
7827 /* V is a vector of constructor elements built up for the base and member
7828 initializers of a constructor for TYPE. They need to be in increasing
7829 offset order, which they might not be yet if TYPE has a primary base
7830 which is not first in the base-clause or a vptr and at least one base
7831 all of which are non-primary. */
7833 static vec<constructor_elt, va_gc> *
7834 sort_constexpr_mem_initializers (tree type, vec<constructor_elt, va_gc> *v)
7836 tree pri = CLASSTYPE_PRIMARY_BINFO (type);
7837 tree field_type;
7838 unsigned i;
7839 constructor_elt *ce;
7841 if (pri)
7842 field_type = BINFO_TYPE (pri);
7843 else if (TYPE_CONTAINS_VPTR_P (type))
7844 field_type = vtbl_ptr_type_node;
7845 else
7846 return v;
7848 /* Find the element for the primary base or vptr and move it to the
7849 beginning of the vec. */
7850 for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
7851 if (TREE_TYPE (ce->index) == field_type)
7852 break;
7854 if (i > 0 && i < vec_safe_length (v))
7856 vec<constructor_elt, va_gc> &vref = *v;
7857 constructor_elt elt = vref[i];
7858 for (; i > 0; --i)
7859 vref[i] = vref[i-1];
7860 vref[0] = elt;
7863 return v;
7866 /* Build compile-time evalable representations of member-initializer list
7867 for a constexpr constructor. */
7869 static tree
7870 build_constexpr_constructor_member_initializers (tree type, tree body)
7872 vec<constructor_elt, va_gc> *vec = NULL;
7873 bool ok = true;
7874 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR
7875 || TREE_CODE (body) == EH_SPEC_BLOCK)
7876 body = TREE_OPERAND (body, 0);
7877 if (TREE_CODE (body) == STATEMENT_LIST)
7878 body = STATEMENT_LIST_HEAD (body)->stmt;
7879 body = BIND_EXPR_BODY (body);
7880 if (TREE_CODE (body) == CLEANUP_POINT_EXPR)
7882 body = TREE_OPERAND (body, 0);
7883 if (TREE_CODE (body) == EXPR_STMT)
7884 body = TREE_OPERAND (body, 0);
7885 if (TREE_CODE (body) == INIT_EXPR
7886 && (same_type_ignoring_top_level_qualifiers_p
7887 (TREE_TYPE (TREE_OPERAND (body, 0)),
7888 current_class_type)))
7890 /* Trivial copy. */
7891 return TREE_OPERAND (body, 1);
7893 ok = build_data_member_initialization (body, &vec);
7895 else if (TREE_CODE (body) == STATEMENT_LIST)
7897 tree_stmt_iterator i;
7898 for (i = tsi_start (body); !tsi_end_p (i); tsi_next (&i))
7900 ok = build_data_member_initialization (tsi_stmt (i), &vec);
7901 if (!ok)
7902 break;
7905 else if (TREE_CODE (body) == TRY_BLOCK)
7907 error ("body of %<constexpr%> constructor cannot be "
7908 "a function-try-block");
7909 return error_mark_node;
7911 else if (EXPR_P (body))
7912 ok = build_data_member_initialization (body, &vec);
7913 else
7914 gcc_assert (errorcount > 0);
7915 if (ok)
7917 if (vec_safe_length (vec) > 0)
7919 /* In a delegating constructor, return the target. */
7920 constructor_elt *ce = &(*vec)[0];
7921 if (ce->index == current_class_ptr)
7923 body = ce->value;
7924 vec_free (vec);
7925 return body;
7928 vec = sort_constexpr_mem_initializers (type, vec);
7929 return build_constructor (type, vec);
7931 else
7932 return error_mark_node;
7935 /* Subroutine of register_constexpr_fundef. BODY is the body of a function
7936 declared to be constexpr, or a sub-statement thereof. Returns the
7937 return value if suitable, error_mark_node for a statement not allowed in
7938 a constexpr function, or NULL_TREE if no return value was found. */
7940 static tree
7941 constexpr_fn_retval (tree body)
7943 switch (TREE_CODE (body))
7945 case STATEMENT_LIST:
7947 tree_stmt_iterator i;
7948 tree expr = NULL_TREE;
7949 for (i = tsi_start (body); !tsi_end_p (i); tsi_next (&i))
7951 tree s = constexpr_fn_retval (tsi_stmt (i));
7952 if (s == error_mark_node)
7953 return error_mark_node;
7954 else if (s == NULL_TREE)
7955 /* Keep iterating. */;
7956 else if (expr)
7957 /* Multiple return statements. */
7958 return error_mark_node;
7959 else
7960 expr = s;
7962 return expr;
7965 case RETURN_EXPR:
7966 return break_out_target_exprs (TREE_OPERAND (body, 0));
7968 case DECL_EXPR:
7969 if (TREE_CODE (DECL_EXPR_DECL (body)) == USING_DECL)
7970 return NULL_TREE;
7971 return error_mark_node;
7973 case CLEANUP_POINT_EXPR:
7974 return constexpr_fn_retval (TREE_OPERAND (body, 0));
7976 case USING_STMT:
7977 return NULL_TREE;
7979 default:
7980 return error_mark_node;
7984 /* Subroutine of register_constexpr_fundef. BODY is the DECL_SAVED_TREE of
7985 FUN; do the necessary transformations to turn it into a single expression
7986 that we can store in the hash table. */
7988 static tree
7989 massage_constexpr_body (tree fun, tree body)
7991 if (DECL_CONSTRUCTOR_P (fun))
7992 body = build_constexpr_constructor_member_initializers
7993 (DECL_CONTEXT (fun), body);
7994 else
7996 if (TREE_CODE (body) == EH_SPEC_BLOCK)
7997 body = EH_SPEC_STMTS (body);
7998 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
7999 body = TREE_OPERAND (body, 0);
8000 if (TREE_CODE (body) == BIND_EXPR)
8001 body = BIND_EXPR_BODY (body);
8002 body = constexpr_fn_retval (body);
8004 return body;
8007 /* FUN is a constexpr constructor with massaged body BODY. Return true
8008 if some bases/fields are uninitialized, and complain if COMPLAIN. */
8010 static bool
8011 cx_check_missing_mem_inits (tree fun, tree body, bool complain)
8013 bool bad;
8014 tree field;
8015 unsigned i, nelts;
8016 tree ctype;
8018 if (TREE_CODE (body) != CONSTRUCTOR)
8019 return false;
8021 nelts = CONSTRUCTOR_NELTS (body);
8022 ctype = DECL_CONTEXT (fun);
8023 field = TYPE_FIELDS (ctype);
8025 if (TREE_CODE (ctype) == UNION_TYPE)
8027 if (nelts == 0 && next_initializable_field (field))
8029 if (complain)
8030 error ("%<constexpr%> constructor for union %qT must "
8031 "initialize exactly one non-static data member", ctype);
8032 return true;
8034 return false;
8037 bad = false;
8038 for (i = 0; i <= nelts; ++i)
8040 tree index;
8041 if (i == nelts)
8042 index = NULL_TREE;
8043 else
8045 index = CONSTRUCTOR_ELT (body, i)->index;
8046 /* Skip base and vtable inits. */
8047 if (TREE_CODE (index) != FIELD_DECL
8048 || DECL_ARTIFICIAL (index))
8049 continue;
8051 for (; field != index; field = DECL_CHAIN (field))
8053 tree ftype;
8054 if (TREE_CODE (field) != FIELD_DECL
8055 || (DECL_C_BIT_FIELD (field) && !DECL_NAME (field))
8056 || DECL_ARTIFICIAL (field))
8057 continue;
8058 ftype = strip_array_types (TREE_TYPE (field));
8059 if (type_has_constexpr_default_constructor (ftype))
8061 /* It's OK to skip a member with a trivial constexpr ctor.
8062 A constexpr ctor that isn't trivial should have been
8063 added in by now. */
8064 gcc_checking_assert (!TYPE_HAS_COMPLEX_DFLT (ftype)
8065 || errorcount != 0);
8066 continue;
8068 if (!complain)
8069 return true;
8070 error ("uninitialized member %qD in %<constexpr%> constructor",
8071 field);
8072 bad = true;
8074 if (field == NULL_TREE)
8075 break;
8076 field = DECL_CHAIN (field);
8079 return bad;
8082 /* We are processing the definition of the constexpr function FUN.
8083 Check that its BODY fulfills the propriate requirements and
8084 enter it in the constexpr function definition table.
8085 For constructor BODY is actually the TREE_LIST of the
8086 member-initializer list. */
8088 tree
8089 register_constexpr_fundef (tree fun, tree body)
8091 constexpr_fundef entry;
8092 constexpr_fundef **slot;
8094 if (!is_valid_constexpr_fn (fun, !DECL_GENERATED_P (fun)))
8095 return NULL;
8097 body = massage_constexpr_body (fun, body);
8098 if (body == NULL_TREE || body == error_mark_node)
8100 if (!DECL_CONSTRUCTOR_P (fun))
8101 error ("body of constexpr function %qD not a return-statement", fun);
8102 return NULL;
8105 if (!potential_rvalue_constant_expression (body))
8107 if (!DECL_GENERATED_P (fun))
8108 require_potential_rvalue_constant_expression (body);
8109 return NULL;
8112 if (DECL_CONSTRUCTOR_P (fun)
8113 && cx_check_missing_mem_inits (fun, body, !DECL_GENERATED_P (fun)))
8114 return NULL;
8116 /* Create the constexpr function table if necessary. */
8117 if (constexpr_fundef_table == NULL)
8118 constexpr_fundef_table = htab_create_ggc (101,
8119 constexpr_fundef_hash,
8120 constexpr_fundef_equal,
8121 ggc_free);
8122 entry.decl = fun;
8123 entry.body = body;
8124 slot = (constexpr_fundef **)
8125 htab_find_slot (constexpr_fundef_table, &entry, INSERT);
8127 gcc_assert (*slot == NULL);
8128 *slot = ggc_alloc<constexpr_fundef> ();
8129 **slot = entry;
8131 return fun;
8134 /* FUN is a non-constexpr function called in a context that requires a
8135 constant expression. If it comes from a constexpr template, explain why
8136 the instantiation isn't constexpr. */
8138 void
8139 explain_invalid_constexpr_fn (tree fun)
8141 static struct pointer_set_t *diagnosed;
8142 tree body;
8143 location_t save_loc;
8144 /* Only diagnose defaulted functions or instantiations. */
8145 if (!DECL_DEFAULTED_FN (fun)
8146 && !is_instantiation_of_constexpr (fun))
8147 return;
8148 if (diagnosed == NULL)
8149 diagnosed = pointer_set_create ();
8150 if (pointer_set_insert (diagnosed, fun) != 0)
8151 /* Already explained. */
8152 return;
8154 save_loc = input_location;
8155 input_location = DECL_SOURCE_LOCATION (fun);
8156 inform (0, "%q+D is not usable as a constexpr function because:", fun);
8157 /* First check the declaration. */
8158 if (is_valid_constexpr_fn (fun, true))
8160 /* Then if it's OK, the body. */
8161 if (DECL_DEFAULTED_FN (fun))
8162 explain_implicit_non_constexpr (fun);
8163 else
8165 body = massage_constexpr_body (fun, DECL_SAVED_TREE (fun));
8166 require_potential_rvalue_constant_expression (body);
8167 if (DECL_CONSTRUCTOR_P (fun))
8168 cx_check_missing_mem_inits (fun, body, true);
8171 input_location = save_loc;
8174 /* Objects of this type represent calls to constexpr functions
8175 along with the bindings of parameters to their arguments, for
8176 the purpose of compile time evaluation. */
8178 typedef struct GTY(()) constexpr_call {
8179 /* Description of the constexpr function definition. */
8180 constexpr_fundef *fundef;
8181 /* Parameter bindings environment. A TREE_LIST where each TREE_PURPOSE
8182 is a parameter _DECL and the TREE_VALUE is the value of the parameter.
8183 Note: This arrangement is made to accommodate the use of
8184 iterative_hash_template_arg (see pt.c). If you change this
8185 representation, also change the hash calculation in
8186 cxx_eval_call_expression. */
8187 tree bindings;
8188 /* Result of the call.
8189 NULL means the call is being evaluated.
8190 error_mark_node means that the evaluation was erroneous;
8191 otherwise, the actuall value of the call. */
8192 tree result;
8193 /* The hash of this call; we remember it here to avoid having to
8194 recalculate it when expanding the hash table. */
8195 hashval_t hash;
8196 } constexpr_call;
8198 /* A table of all constexpr calls that have been evaluated by the
8199 compiler in this translation unit. */
8201 static GTY ((param_is (constexpr_call))) htab_t constexpr_call_table;
8203 static tree cxx_eval_constant_expression (const constexpr_call *, tree,
8204 bool, bool, bool *, bool *);
8206 /* Compute a hash value for a constexpr call representation. */
8208 static hashval_t
8209 constexpr_call_hash (const void *p)
8211 const constexpr_call *info = (const constexpr_call *) p;
8212 return info->hash;
8215 /* Return 1 if the objects pointed to by P and Q represent calls
8216 to the same constexpr function with the same arguments.
8217 Otherwise, return 0. */
8219 static int
8220 constexpr_call_equal (const void *p, const void *q)
8222 const constexpr_call *lhs = (const constexpr_call *) p;
8223 const constexpr_call *rhs = (const constexpr_call *) q;
8224 tree lhs_bindings;
8225 tree rhs_bindings;
8226 if (lhs == rhs)
8227 return 1;
8228 if (!constexpr_fundef_equal (lhs->fundef, rhs->fundef))
8229 return 0;
8230 lhs_bindings = lhs->bindings;
8231 rhs_bindings = rhs->bindings;
8232 while (lhs_bindings != NULL && rhs_bindings != NULL)
8234 tree lhs_arg = TREE_VALUE (lhs_bindings);
8235 tree rhs_arg = TREE_VALUE (rhs_bindings);
8236 gcc_assert (TREE_TYPE (lhs_arg) == TREE_TYPE (rhs_arg));
8237 if (!cp_tree_equal (lhs_arg, rhs_arg))
8238 return 0;
8239 lhs_bindings = TREE_CHAIN (lhs_bindings);
8240 rhs_bindings = TREE_CHAIN (rhs_bindings);
8242 return lhs_bindings == rhs_bindings;
8245 /* Initialize the constexpr call table, if needed. */
8247 static void
8248 maybe_initialize_constexpr_call_table (void)
8250 if (constexpr_call_table == NULL)
8251 constexpr_call_table = htab_create_ggc (101,
8252 constexpr_call_hash,
8253 constexpr_call_equal,
8254 ggc_free);
8257 /* Return true if T designates the implied `this' parameter. */
8259 bool
8260 is_this_parameter (tree t)
8262 if (!DECL_P (t) || DECL_NAME (t) != this_identifier)
8263 return false;
8264 gcc_assert (TREE_CODE (t) == PARM_DECL || is_capture_proxy (t));
8265 return true;
8268 /* We have an expression tree T that represents a call, either CALL_EXPR
8269 or AGGR_INIT_EXPR. If the call is lexically to a named function,
8270 retrun the _DECL for that function. */
8272 static tree
8273 get_function_named_in_call (tree t)
8275 tree fun = NULL;
8276 switch (TREE_CODE (t))
8278 case CALL_EXPR:
8279 fun = CALL_EXPR_FN (t);
8280 break;
8282 case AGGR_INIT_EXPR:
8283 fun = AGGR_INIT_EXPR_FN (t);
8284 break;
8286 default:
8287 gcc_unreachable();
8288 break;
8290 if (TREE_CODE (fun) == ADDR_EXPR
8291 && TREE_CODE (TREE_OPERAND (fun, 0)) == FUNCTION_DECL)
8292 fun = TREE_OPERAND (fun, 0);
8293 return fun;
8296 /* We have an expression tree T that represents a call, either CALL_EXPR
8297 or AGGR_INIT_EXPR. Return the Nth argument. */
8299 static inline tree
8300 get_nth_callarg (tree t, int n)
8302 switch (TREE_CODE (t))
8304 case CALL_EXPR:
8305 return CALL_EXPR_ARG (t, n);
8307 case AGGR_INIT_EXPR:
8308 return AGGR_INIT_EXPR_ARG (t, n);
8310 default:
8311 gcc_unreachable ();
8312 return NULL;
8316 /* Look up the binding of the function parameter T in a constexpr
8317 function call context CALL. */
8319 static tree
8320 lookup_parameter_binding (const constexpr_call *call, tree t)
8322 tree b = purpose_member (t, call->bindings);
8323 return TREE_VALUE (b);
8326 /* Attempt to evaluate T which represents a call to a builtin function.
8327 We assume here that all builtin functions evaluate to scalar types
8328 represented by _CST nodes. */
8330 static tree
8331 cxx_eval_builtin_function_call (const constexpr_call *call, tree t,
8332 bool allow_non_constant, bool addr,
8333 bool *non_constant_p, bool *overflow_p)
8335 const int nargs = call_expr_nargs (t);
8336 tree *args = (tree *) alloca (nargs * sizeof (tree));
8337 tree new_call;
8338 int i;
8339 for (i = 0; i < nargs; ++i)
8341 args[i] = cxx_eval_constant_expression (call, CALL_EXPR_ARG (t, i),
8342 allow_non_constant, addr,
8343 non_constant_p, overflow_p);
8344 if (allow_non_constant && *non_constant_p)
8345 return t;
8347 if (*non_constant_p)
8348 return t;
8349 new_call = build_call_array_loc (EXPR_LOCATION (t), TREE_TYPE (t),
8350 CALL_EXPR_FN (t), nargs, args);
8351 new_call = fold (new_call);
8352 VERIFY_CONSTANT (new_call);
8353 return new_call;
8356 /* TEMP is the constant value of a temporary object of type TYPE. Adjust
8357 the type of the value to match. */
8359 static tree
8360 adjust_temp_type (tree type, tree temp)
8362 if (TREE_TYPE (temp) == type)
8363 return temp;
8364 /* Avoid wrapping an aggregate value in a NOP_EXPR. */
8365 if (TREE_CODE (temp) == CONSTRUCTOR)
8366 return build_constructor (type, CONSTRUCTOR_ELTS (temp));
8367 gcc_assert (scalarish_type_p (type));
8368 return cp_fold_convert (type, temp);
8371 /* Subroutine of cxx_eval_call_expression.
8372 We are processing a call expression (either CALL_EXPR or
8373 AGGR_INIT_EXPR) in the call context of OLD_CALL. Evaluate
8374 all arguments and bind their values to correspondings
8375 parameters, making up the NEW_CALL context. */
8377 static void
8378 cxx_bind_parameters_in_call (const constexpr_call *old_call, tree t,
8379 constexpr_call *new_call,
8380 bool allow_non_constant,
8381 bool *non_constant_p, bool *overflow_p)
8383 const int nargs = call_expr_nargs (t);
8384 tree fun = new_call->fundef->decl;
8385 tree parms = DECL_ARGUMENTS (fun);
8386 int i;
8387 for (i = 0; i < nargs; ++i)
8389 tree x, arg;
8390 tree type = parms ? TREE_TYPE (parms) : void_type_node;
8391 /* For member function, the first argument is a pointer to the implied
8392 object. And for an object construction, don't bind `this' before
8393 it is fully constructed. */
8394 if (i == 0 && DECL_CONSTRUCTOR_P (fun))
8395 goto next;
8396 x = get_nth_callarg (t, i);
8397 if (parms && DECL_BY_REFERENCE (parms))
8399 /* cp_genericize made this a reference for argument passing, but
8400 we don't want to treat it like one for constexpr evaluation. */
8401 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
8402 gcc_assert (TREE_CODE (TREE_TYPE (x)) == REFERENCE_TYPE);
8403 type = TREE_TYPE (type);
8404 x = convert_from_reference (x);
8406 arg = cxx_eval_constant_expression (old_call, x, allow_non_constant,
8407 TREE_CODE (type) == REFERENCE_TYPE,
8408 non_constant_p, overflow_p);
8409 /* Don't VERIFY_CONSTANT here. */
8410 if (*non_constant_p && allow_non_constant)
8411 return;
8412 /* Just discard ellipsis args after checking their constantitude. */
8413 if (!parms)
8414 continue;
8415 if (*non_constant_p)
8416 /* Don't try to adjust the type of non-constant args. */
8417 goto next;
8419 /* Make sure the binding has the same type as the parm. */
8420 if (TREE_CODE (type) != REFERENCE_TYPE)
8421 arg = adjust_temp_type (type, arg);
8422 new_call->bindings = tree_cons (parms, arg, new_call->bindings);
8423 next:
8424 parms = TREE_CHAIN (parms);
8428 /* Variables and functions to manage constexpr call expansion context.
8429 These do not need to be marked for PCH or GC. */
8431 /* FIXME remember and print actual constant arguments. */
8432 static vec<tree> call_stack = vNULL;
8433 static int call_stack_tick;
8434 static int last_cx_error_tick;
8436 static bool
8437 push_cx_call_context (tree call)
8439 ++call_stack_tick;
8440 if (!EXPR_HAS_LOCATION (call))
8441 SET_EXPR_LOCATION (call, input_location);
8442 call_stack.safe_push (call);
8443 if (call_stack.length () > (unsigned) max_constexpr_depth)
8444 return false;
8445 return true;
8448 static void
8449 pop_cx_call_context (void)
8451 ++call_stack_tick;
8452 call_stack.pop ();
8455 vec<tree>
8456 cx_error_context (void)
8458 vec<tree> r = vNULL;
8459 if (call_stack_tick != last_cx_error_tick
8460 && !call_stack.is_empty ())
8461 r = call_stack;
8462 last_cx_error_tick = call_stack_tick;
8463 return r;
8466 /* Subroutine of cxx_eval_constant_expression.
8467 Evaluate the call expression tree T in the context of OLD_CALL expression
8468 evaluation. */
8470 static tree
8471 cxx_eval_call_expression (const constexpr_call *old_call, tree t,
8472 bool allow_non_constant, bool addr,
8473 bool *non_constant_p, bool *overflow_p)
8475 location_t loc = EXPR_LOC_OR_LOC (t, input_location);
8476 tree fun = get_function_named_in_call (t);
8477 tree result;
8478 constexpr_call new_call = { NULL, NULL, NULL, 0 };
8479 constexpr_call **slot;
8480 constexpr_call *entry;
8481 bool depth_ok;
8483 if (TREE_CODE (fun) != FUNCTION_DECL)
8485 /* Might be a constexpr function pointer. */
8486 fun = cxx_eval_constant_expression (old_call, fun, allow_non_constant,
8487 /*addr*/false, non_constant_p, overflow_p);
8488 if (TREE_CODE (fun) == ADDR_EXPR)
8489 fun = TREE_OPERAND (fun, 0);
8491 if (TREE_CODE (fun) != FUNCTION_DECL)
8493 if (!allow_non_constant && !*non_constant_p)
8494 error_at (loc, "expression %qE does not designate a constexpr "
8495 "function", fun);
8496 *non_constant_p = true;
8497 return t;
8499 if (DECL_CLONED_FUNCTION_P (fun))
8500 fun = DECL_CLONED_FUNCTION (fun);
8501 if (is_builtin_fn (fun))
8502 return cxx_eval_builtin_function_call (old_call, t, allow_non_constant,
8503 addr, non_constant_p, overflow_p);
8504 if (!DECL_DECLARED_CONSTEXPR_P (fun))
8506 if (!allow_non_constant)
8508 error_at (loc, "call to non-constexpr function %qD", fun);
8509 explain_invalid_constexpr_fn (fun);
8511 *non_constant_p = true;
8512 return t;
8515 /* Shortcut trivial constructor/op=. */
8516 if (trivial_fn_p (fun))
8518 if (call_expr_nargs (t) == 2)
8520 tree arg = convert_from_reference (get_nth_callarg (t, 1));
8521 return cxx_eval_constant_expression (old_call, arg, allow_non_constant,
8522 addr, non_constant_p, overflow_p);
8524 else if (TREE_CODE (t) == AGGR_INIT_EXPR
8525 && AGGR_INIT_ZERO_FIRST (t))
8526 return build_zero_init (DECL_CONTEXT (fun), NULL_TREE, false);
8529 /* If in direct recursive call, optimize definition search. */
8530 if (old_call != NULL && old_call->fundef->decl == fun)
8531 new_call.fundef = old_call->fundef;
8532 else
8534 new_call.fundef = retrieve_constexpr_fundef (fun);
8535 if (new_call.fundef == NULL || new_call.fundef->body == NULL)
8537 if (!allow_non_constant)
8539 if (DECL_INITIAL (fun))
8541 /* The definition of fun was somehow unsuitable. */
8542 error_at (loc, "%qD called in a constant expression", fun);
8543 explain_invalid_constexpr_fn (fun);
8545 else
8546 error_at (loc, "%qD used before its definition", fun);
8548 *non_constant_p = true;
8549 return t;
8552 cxx_bind_parameters_in_call (old_call, t, &new_call,
8553 allow_non_constant, non_constant_p, overflow_p);
8554 if (*non_constant_p)
8555 return t;
8557 depth_ok = push_cx_call_context (t);
8559 new_call.hash
8560 = iterative_hash_template_arg (new_call.bindings,
8561 constexpr_fundef_hash (new_call.fundef));
8563 /* If we have seen this call before, we are done. */
8564 maybe_initialize_constexpr_call_table ();
8565 slot = (constexpr_call **)
8566 htab_find_slot (constexpr_call_table, &new_call, INSERT);
8567 entry = *slot;
8568 if (entry == NULL)
8570 /* We need to keep a pointer to the entry, not just the slot, as the
8571 slot can move in the call to cxx_eval_builtin_function_call. */
8572 *slot = entry = ggc_alloc<constexpr_call> ();
8573 *entry = new_call;
8575 /* Calls which are in progress have their result set to NULL
8576 so that we can detect circular dependencies. */
8577 else if (entry->result == NULL)
8579 if (!allow_non_constant)
8580 error ("call has circular dependency");
8581 *non_constant_p = true;
8582 entry->result = result = error_mark_node;
8585 if (!depth_ok)
8587 if (!allow_non_constant)
8588 error ("constexpr evaluation depth exceeds maximum of %d (use "
8589 "-fconstexpr-depth= to increase the maximum)",
8590 max_constexpr_depth);
8591 *non_constant_p = true;
8592 entry->result = result = error_mark_node;
8594 else
8596 result = entry->result;
8597 if (!result || result == error_mark_node)
8598 result = (cxx_eval_constant_expression
8599 (&new_call, new_call.fundef->body,
8600 allow_non_constant, addr,
8601 non_constant_p, overflow_p));
8602 if (result == error_mark_node)
8603 *non_constant_p = true;
8604 if (*non_constant_p)
8605 entry->result = result = error_mark_node;
8606 else
8608 /* If this was a call to initialize an object, set the type of
8609 the CONSTRUCTOR to the type of that object. */
8610 if (DECL_CONSTRUCTOR_P (fun))
8612 tree ob_arg = get_nth_callarg (t, 0);
8613 STRIP_NOPS (ob_arg);
8614 gcc_assert (TYPE_PTR_P (TREE_TYPE (ob_arg))
8615 && CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (ob_arg))));
8616 result = adjust_temp_type (TREE_TYPE (TREE_TYPE (ob_arg)),
8617 result);
8619 entry->result = result;
8623 pop_cx_call_context ();
8624 return unshare_expr (result);
8627 /* FIXME speed this up, it's taking 16% of compile time on sieve testcase. */
8629 bool
8630 reduced_constant_expression_p (tree t)
8632 if (TREE_CODE (t) == PTRMEM_CST)
8633 /* Even if we can't lower this yet, it's constant. */
8634 return true;
8635 /* FIXME are we calling this too much? */
8636 return initializer_constant_valid_p (t, TREE_TYPE (t)) != NULL_TREE;
8639 /* Some expressions may have constant operands but are not constant
8640 themselves, such as 1/0. Call this function (or rather, the macro
8641 following it) to check for that condition.
8643 We only call this in places that require an arithmetic constant, not in
8644 places where we might have a non-constant expression that can be a
8645 component of a constant expression, such as the address of a constexpr
8646 variable that might be dereferenced later. */
8648 static bool
8649 verify_constant (tree t, bool allow_non_constant, bool *non_constant_p,
8650 bool *overflow_p)
8652 if (!*non_constant_p && !reduced_constant_expression_p (t))
8654 if (!allow_non_constant)
8655 error ("%q+E is not a constant expression", t);
8656 *non_constant_p = true;
8658 if (TREE_OVERFLOW_P (t))
8660 if (!allow_non_constant)
8662 permerror (input_location, "overflow in constant expression");
8663 /* If we're being permissive (and are in an enforcing
8664 context), ignore the overflow. */
8665 if (flag_permissive)
8666 return *non_constant_p;
8668 *overflow_p = true;
8670 return *non_constant_p;
8673 /* Subroutine of cxx_eval_constant_expression.
8674 Attempt to reduce the unary expression tree T to a compile time value.
8675 If successful, return the value. Otherwise issue a diagnostic
8676 and return error_mark_node. */
8678 static tree
8679 cxx_eval_unary_expression (const constexpr_call *call, tree t,
8680 bool allow_non_constant, bool addr,
8681 bool *non_constant_p, bool *overflow_p)
8683 tree r;
8684 tree orig_arg = TREE_OPERAND (t, 0);
8685 tree arg = cxx_eval_constant_expression (call, orig_arg, allow_non_constant,
8686 addr, non_constant_p, overflow_p);
8687 VERIFY_CONSTANT (arg);
8688 if (arg == orig_arg)
8689 return t;
8690 r = fold_build1 (TREE_CODE (t), TREE_TYPE (t), arg);
8691 VERIFY_CONSTANT (r);
8692 return r;
8695 /* Subroutine of cxx_eval_constant_expression.
8696 Like cxx_eval_unary_expression, except for binary expressions. */
8698 static tree
8699 cxx_eval_binary_expression (const constexpr_call *call, tree t,
8700 bool allow_non_constant, bool addr,
8701 bool *non_constant_p, bool *overflow_p)
8703 tree r;
8704 tree orig_lhs = TREE_OPERAND (t, 0);
8705 tree orig_rhs = TREE_OPERAND (t, 1);
8706 tree lhs, rhs;
8707 lhs = cxx_eval_constant_expression (call, orig_lhs,
8708 allow_non_constant, addr,
8709 non_constant_p, overflow_p);
8710 VERIFY_CONSTANT (lhs);
8711 rhs = cxx_eval_constant_expression (call, orig_rhs,
8712 allow_non_constant, addr,
8713 non_constant_p, overflow_p);
8714 VERIFY_CONSTANT (rhs);
8715 if (lhs == orig_lhs && rhs == orig_rhs)
8716 return t;
8717 r = fold_build2 (TREE_CODE (t), TREE_TYPE (t), lhs, rhs);
8718 VERIFY_CONSTANT (r);
8719 return r;
8722 /* Subroutine of cxx_eval_constant_expression.
8723 Attempt to evaluate condition expressions. Dead branches are not
8724 looked into. */
8726 static tree
8727 cxx_eval_conditional_expression (const constexpr_call *call, tree t,
8728 bool allow_non_constant, bool addr,
8729 bool *non_constant_p, bool *overflow_p)
8731 tree val = cxx_eval_constant_expression (call, TREE_OPERAND (t, 0),
8732 allow_non_constant, addr,
8733 non_constant_p, overflow_p);
8734 VERIFY_CONSTANT (val);
8735 /* Don't VERIFY_CONSTANT the other operands. */
8736 if (integer_zerop (val))
8737 return cxx_eval_constant_expression (call, TREE_OPERAND (t, 2),
8738 allow_non_constant, addr,
8739 non_constant_p, overflow_p);
8740 return cxx_eval_constant_expression (call, TREE_OPERAND (t, 1),
8741 allow_non_constant, addr,
8742 non_constant_p, overflow_p);
8745 /* Subroutine of cxx_eval_constant_expression.
8746 Attempt to reduce a reference to an array slot. */
8748 static tree
8749 cxx_eval_array_reference (const constexpr_call *call, tree t,
8750 bool allow_non_constant, bool addr,
8751 bool *non_constant_p, bool *overflow_p)
8753 tree oldary = TREE_OPERAND (t, 0);
8754 tree ary = cxx_eval_constant_expression (call, oldary,
8755 allow_non_constant, addr,
8756 non_constant_p, overflow_p);
8757 tree index, oldidx;
8758 HOST_WIDE_INT i;
8759 tree elem_type;
8760 unsigned len, elem_nchars = 1;
8761 if (*non_constant_p)
8762 return t;
8763 oldidx = TREE_OPERAND (t, 1);
8764 index = cxx_eval_constant_expression (call, oldidx,
8765 allow_non_constant, false,
8766 non_constant_p, overflow_p);
8767 VERIFY_CONSTANT (index);
8768 if (addr && ary == oldary && index == oldidx)
8769 return t;
8770 else if (addr)
8771 return build4 (ARRAY_REF, TREE_TYPE (t), ary, index, NULL, NULL);
8772 elem_type = TREE_TYPE (TREE_TYPE (ary));
8773 if (TREE_CODE (ary) == CONSTRUCTOR)
8774 len = CONSTRUCTOR_NELTS (ary);
8775 else if (TREE_CODE (ary) == STRING_CST)
8777 elem_nchars = (TYPE_PRECISION (elem_type)
8778 / TYPE_PRECISION (char_type_node));
8779 len = (unsigned) TREE_STRING_LENGTH (ary) / elem_nchars;
8781 else
8783 /* We can't do anything with other tree codes, so use
8784 VERIFY_CONSTANT to complain and fail. */
8785 VERIFY_CONSTANT (ary);
8786 gcc_unreachable ();
8788 if (compare_tree_int (index, len) >= 0)
8790 if (tree_int_cst_lt (index, array_type_nelts_top (TREE_TYPE (ary))))
8792 /* If it's within the array bounds but doesn't have an explicit
8793 initializer, it's value-initialized. */
8794 tree val = build_value_init (elem_type, tf_warning_or_error);
8795 return cxx_eval_constant_expression (call, val,
8796 allow_non_constant, addr,
8797 non_constant_p, overflow_p);
8800 if (!allow_non_constant)
8801 error ("array subscript out of bound");
8802 *non_constant_p = true;
8803 return t;
8805 else if (tree_int_cst_lt (index, integer_zero_node))
8807 if (!allow_non_constant)
8808 error ("negative array subscript");
8809 *non_constant_p = true;
8810 return t;
8812 i = tree_to_shwi (index);
8813 if (TREE_CODE (ary) == CONSTRUCTOR)
8814 return (*CONSTRUCTOR_ELTS (ary))[i].value;
8815 else if (elem_nchars == 1)
8816 return build_int_cst (cv_unqualified (TREE_TYPE (TREE_TYPE (ary))),
8817 TREE_STRING_POINTER (ary)[i]);
8818 else
8820 tree type = cv_unqualified (TREE_TYPE (TREE_TYPE (ary)));
8821 return native_interpret_expr (type, (const unsigned char *)
8822 TREE_STRING_POINTER (ary)
8823 + i * elem_nchars, elem_nchars);
8825 /* Don't VERIFY_CONSTANT here. */
8828 /* Subroutine of cxx_eval_constant_expression.
8829 Attempt to reduce a field access of a value of class type. */
8831 static tree
8832 cxx_eval_component_reference (const constexpr_call *call, tree t,
8833 bool allow_non_constant, bool addr,
8834 bool *non_constant_p, bool *overflow_p)
8836 unsigned HOST_WIDE_INT i;
8837 tree field;
8838 tree value;
8839 tree part = TREE_OPERAND (t, 1);
8840 tree orig_whole = TREE_OPERAND (t, 0);
8841 tree whole = cxx_eval_constant_expression (call, orig_whole,
8842 allow_non_constant, addr,
8843 non_constant_p, overflow_p);
8844 if (whole == orig_whole)
8845 return t;
8846 if (addr)
8847 return fold_build3 (COMPONENT_REF, TREE_TYPE (t),
8848 whole, part, NULL_TREE);
8849 /* Don't VERIFY_CONSTANT here; we only want to check that we got a
8850 CONSTRUCTOR. */
8851 if (!*non_constant_p && TREE_CODE (whole) != CONSTRUCTOR)
8853 if (!allow_non_constant)
8854 error ("%qE is not a constant expression", orig_whole);
8855 *non_constant_p = true;
8857 if (DECL_MUTABLE_P (part))
8859 if (!allow_non_constant)
8860 error ("mutable %qD is not usable in a constant expression", part);
8861 *non_constant_p = true;
8863 if (*non_constant_p)
8864 return t;
8865 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole), i, field, value)
8867 if (field == part)
8868 return value;
8870 if (TREE_CODE (TREE_TYPE (whole)) == UNION_TYPE
8871 && CONSTRUCTOR_NELTS (whole) > 0)
8873 /* DR 1188 says we don't have to deal with this. */
8874 if (!allow_non_constant)
8875 error ("accessing %qD member instead of initialized %qD member in "
8876 "constant expression", part, CONSTRUCTOR_ELT (whole, 0)->index);
8877 *non_constant_p = true;
8878 return t;
8881 /* If there's no explicit init for this field, it's value-initialized. */
8882 value = build_value_init (TREE_TYPE (t), tf_warning_or_error);
8883 return cxx_eval_constant_expression (call, value,
8884 allow_non_constant, addr,
8885 non_constant_p, overflow_p);
8888 /* Subroutine of cxx_eval_constant_expression.
8889 Attempt to reduce a field access of a value of class type that is
8890 expressed as a BIT_FIELD_REF. */
8892 static tree
8893 cxx_eval_bit_field_ref (const constexpr_call *call, tree t,
8894 bool allow_non_constant, bool addr,
8895 bool *non_constant_p, bool *overflow_p)
8897 tree orig_whole = TREE_OPERAND (t, 0);
8898 tree retval, fldval, utype, mask;
8899 bool fld_seen = false;
8900 HOST_WIDE_INT istart, isize;
8901 tree whole = cxx_eval_constant_expression (call, orig_whole,
8902 allow_non_constant, addr,
8903 non_constant_p, overflow_p);
8904 tree start, field, value;
8905 unsigned HOST_WIDE_INT i;
8907 if (whole == orig_whole)
8908 return t;
8909 /* Don't VERIFY_CONSTANT here; we only want to check that we got a
8910 CONSTRUCTOR. */
8911 if (!*non_constant_p
8912 && TREE_CODE (whole) != VECTOR_CST
8913 && TREE_CODE (whole) != CONSTRUCTOR)
8915 if (!allow_non_constant)
8916 error ("%qE is not a constant expression", orig_whole);
8917 *non_constant_p = true;
8919 if (*non_constant_p)
8920 return t;
8922 if (TREE_CODE (whole) == VECTOR_CST)
8923 return fold_ternary (BIT_FIELD_REF, TREE_TYPE (t), whole,
8924 TREE_OPERAND (t, 1), TREE_OPERAND (t, 2));
8926 start = TREE_OPERAND (t, 2);
8927 istart = tree_to_shwi (start);
8928 isize = tree_to_shwi (TREE_OPERAND (t, 1));
8929 utype = TREE_TYPE (t);
8930 if (!TYPE_UNSIGNED (utype))
8931 utype = build_nonstandard_integer_type (TYPE_PRECISION (utype), 1);
8932 retval = build_int_cst (utype, 0);
8933 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole), i, field, value)
8935 tree bitpos = bit_position (field);
8936 if (bitpos == start && DECL_SIZE (field) == TREE_OPERAND (t, 1))
8937 return value;
8938 if (TREE_CODE (TREE_TYPE (field)) == INTEGER_TYPE
8939 && TREE_CODE (value) == INTEGER_CST
8940 && tree_fits_shwi_p (bitpos)
8941 && tree_fits_shwi_p (DECL_SIZE (field)))
8943 HOST_WIDE_INT bit = tree_to_shwi (bitpos);
8944 HOST_WIDE_INT sz = tree_to_shwi (DECL_SIZE (field));
8945 HOST_WIDE_INT shift;
8946 if (bit >= istart && bit + sz <= istart + isize)
8948 fldval = fold_convert (utype, value);
8949 mask = build_int_cst_type (utype, -1);
8950 mask = fold_build2 (LSHIFT_EXPR, utype, mask,
8951 size_int (TYPE_PRECISION (utype) - sz));
8952 mask = fold_build2 (RSHIFT_EXPR, utype, mask,
8953 size_int (TYPE_PRECISION (utype) - sz));
8954 fldval = fold_build2 (BIT_AND_EXPR, utype, fldval, mask);
8955 shift = bit - istart;
8956 if (BYTES_BIG_ENDIAN)
8957 shift = TYPE_PRECISION (utype) - shift - sz;
8958 fldval = fold_build2 (LSHIFT_EXPR, utype, fldval,
8959 size_int (shift));
8960 retval = fold_build2 (BIT_IOR_EXPR, utype, retval, fldval);
8961 fld_seen = true;
8965 if (fld_seen)
8966 return fold_convert (TREE_TYPE (t), retval);
8967 gcc_unreachable ();
8968 return error_mark_node;
8971 /* Subroutine of cxx_eval_constant_expression.
8972 Evaluate a short-circuited logical expression T in the context
8973 of a given constexpr CALL. BAILOUT_VALUE is the value for
8974 early return. CONTINUE_VALUE is used here purely for
8975 sanity check purposes. */
8977 static tree
8978 cxx_eval_logical_expression (const constexpr_call *call, tree t,
8979 tree bailout_value, tree continue_value,
8980 bool allow_non_constant, bool addr,
8981 bool *non_constant_p, bool *overflow_p)
8983 tree r;
8984 tree lhs = cxx_eval_constant_expression (call, TREE_OPERAND (t, 0),
8985 allow_non_constant, addr,
8986 non_constant_p, overflow_p);
8987 VERIFY_CONSTANT (lhs);
8988 if (tree_int_cst_equal (lhs, bailout_value))
8989 return lhs;
8990 gcc_assert (tree_int_cst_equal (lhs, continue_value));
8991 r = cxx_eval_constant_expression (call, TREE_OPERAND (t, 1),
8992 allow_non_constant, addr, non_constant_p, overflow_p);
8993 VERIFY_CONSTANT (r);
8994 return r;
8997 /* REF is a COMPONENT_REF designating a particular field. V is a vector of
8998 CONSTRUCTOR elements to initialize (part of) an object containing that
8999 field. Return a pointer to the constructor_elt corresponding to the
9000 initialization of the field. */
9002 static constructor_elt *
9003 base_field_constructor_elt (vec<constructor_elt, va_gc> *v, tree ref)
9005 tree aggr = TREE_OPERAND (ref, 0);
9006 tree field = TREE_OPERAND (ref, 1);
9007 HOST_WIDE_INT i;
9008 constructor_elt *ce;
9010 gcc_assert (TREE_CODE (ref) == COMPONENT_REF);
9012 if (TREE_CODE (aggr) == COMPONENT_REF)
9014 constructor_elt *base_ce
9015 = base_field_constructor_elt (v, aggr);
9016 v = CONSTRUCTOR_ELTS (base_ce->value);
9019 for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
9020 if (ce->index == field)
9021 return ce;
9023 gcc_unreachable ();
9024 return NULL;
9027 /* Subroutine of cxx_eval_constant_expression.
9028 The expression tree T denotes a C-style array or a C-style
9029 aggregate. Reduce it to a constant expression. */
9031 static tree
9032 cxx_eval_bare_aggregate (const constexpr_call *call, tree t,
9033 bool allow_non_constant, bool addr,
9034 bool *non_constant_p, bool *overflow_p)
9036 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t);
9037 vec<constructor_elt, va_gc> *n;
9038 vec_alloc (n, vec_safe_length (v));
9039 constructor_elt *ce;
9040 HOST_WIDE_INT i;
9041 bool changed = false;
9042 gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (t));
9043 for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
9045 tree elt = cxx_eval_constant_expression (call, ce->value,
9046 allow_non_constant, addr,
9047 non_constant_p, overflow_p);
9048 /* Don't VERIFY_CONSTANT here. */
9049 if (allow_non_constant && *non_constant_p)
9050 goto fail;
9051 if (elt != ce->value)
9052 changed = true;
9053 if (ce->index && TREE_CODE (ce->index) == COMPONENT_REF)
9055 /* This is an initialization of a vfield inside a base
9056 subaggregate that we already initialized; push this
9057 initialization into the previous initialization. */
9058 constructor_elt *inner = base_field_constructor_elt (n, ce->index);
9059 inner->value = elt;
9061 else if (ce->index && TREE_CODE (ce->index) == NOP_EXPR)
9063 /* This is an initializer for an empty base; now that we've
9064 checked that it's constant, we can ignore it. */
9065 gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (ce->index))));
9067 else
9068 CONSTRUCTOR_APPEND_ELT (n, ce->index, elt);
9070 if (*non_constant_p || !changed)
9072 fail:
9073 vec_free (n);
9074 return t;
9076 t = build_constructor (TREE_TYPE (t), n);
9077 TREE_CONSTANT (t) = true;
9078 if (TREE_CODE (TREE_TYPE (t)) == VECTOR_TYPE)
9079 t = fold (t);
9080 return t;
9083 /* Subroutine of cxx_eval_constant_expression.
9084 The expression tree T is a VEC_INIT_EXPR which denotes the desired
9085 initialization of a non-static data member of array type. Reduce it to a
9086 CONSTRUCTOR.
9088 Note that apart from value-initialization (when VALUE_INIT is true),
9089 this is only intended to support value-initialization and the
9090 initializations done by defaulted constructors for classes with
9091 non-static data members of array type. In this case, VEC_INIT_EXPR_INIT
9092 will either be NULL_TREE for the default constructor, or a COMPONENT_REF
9093 for the copy/move constructor. */
9095 static tree
9096 cxx_eval_vec_init_1 (const constexpr_call *call, tree atype, tree init,
9097 bool value_init, bool allow_non_constant, bool addr,
9098 bool *non_constant_p, bool *overflow_p)
9100 tree elttype = TREE_TYPE (atype);
9101 int max = tree_to_shwi (array_type_nelts (atype));
9102 vec<constructor_elt, va_gc> *n;
9103 vec_alloc (n, max + 1);
9104 bool pre_init = false;
9105 int i;
9107 /* For the default constructor, build up a call to the default
9108 constructor of the element type. We only need to handle class types
9109 here, as for a constructor to be constexpr, all members must be
9110 initialized, which for a defaulted default constructor means they must
9111 be of a class type with a constexpr default constructor. */
9112 if (TREE_CODE (elttype) == ARRAY_TYPE)
9113 /* We only do this at the lowest level. */;
9114 else if (value_init)
9116 init = build_value_init (elttype, tf_warning_or_error);
9117 init = cxx_eval_constant_expression
9118 (call, init, allow_non_constant, addr, non_constant_p, overflow_p);
9119 pre_init = true;
9121 else if (!init)
9123 vec<tree, va_gc> *argvec = make_tree_vector ();
9124 init = build_special_member_call (NULL_TREE, complete_ctor_identifier,
9125 &argvec, elttype, LOOKUP_NORMAL,
9126 tf_warning_or_error);
9127 release_tree_vector (argvec);
9128 init = cxx_eval_constant_expression (call, init, allow_non_constant,
9129 addr, non_constant_p, overflow_p);
9130 pre_init = true;
9133 if (*non_constant_p && !allow_non_constant)
9134 goto fail;
9136 for (i = 0; i <= max; ++i)
9138 tree idx = build_int_cst (size_type_node, i);
9139 tree eltinit;
9140 if (TREE_CODE (elttype) == ARRAY_TYPE)
9142 /* A multidimensional array; recurse. */
9143 if (value_init || init == NULL_TREE)
9144 eltinit = NULL_TREE;
9145 else
9146 eltinit = cp_build_array_ref (input_location, init, idx,
9147 tf_warning_or_error);
9148 eltinit = cxx_eval_vec_init_1 (call, elttype, eltinit, value_init,
9149 allow_non_constant, addr,
9150 non_constant_p, overflow_p);
9152 else if (pre_init)
9154 /* Initializing an element using value or default initialization
9155 we just pre-built above. */
9156 if (i == 0)
9157 eltinit = init;
9158 else
9159 eltinit = unshare_expr (init);
9161 else
9163 /* Copying an element. */
9164 gcc_assert (same_type_ignoring_top_level_qualifiers_p
9165 (atype, TREE_TYPE (init)));
9166 eltinit = cp_build_array_ref (input_location, init, idx,
9167 tf_warning_or_error);
9168 if (!real_lvalue_p (init))
9169 eltinit = move (eltinit);
9170 eltinit = force_rvalue (eltinit, tf_warning_or_error);
9171 eltinit = cxx_eval_constant_expression
9172 (call, eltinit, allow_non_constant, addr, non_constant_p, overflow_p);
9174 if (*non_constant_p && !allow_non_constant)
9175 goto fail;
9176 CONSTRUCTOR_APPEND_ELT (n, idx, eltinit);
9179 if (!*non_constant_p)
9181 init = build_constructor (atype, n);
9182 TREE_CONSTANT (init) = true;
9183 return init;
9186 fail:
9187 vec_free (n);
9188 return init;
9191 static tree
9192 cxx_eval_vec_init (const constexpr_call *call, tree t,
9193 bool allow_non_constant, bool addr,
9194 bool *non_constant_p, bool *overflow_p)
9196 tree atype = TREE_TYPE (t);
9197 tree init = VEC_INIT_EXPR_INIT (t);
9198 tree r = cxx_eval_vec_init_1 (call, atype, init,
9199 VEC_INIT_EXPR_VALUE_INIT (t),
9200 allow_non_constant, addr, non_constant_p, overflow_p);
9201 if (*non_constant_p)
9202 return t;
9203 else
9204 return r;
9207 /* A less strict version of fold_indirect_ref_1, which requires cv-quals to
9208 match. We want to be less strict for simple *& folding; if we have a
9209 non-const temporary that we access through a const pointer, that should
9210 work. We handle this here rather than change fold_indirect_ref_1
9211 because we're dealing with things like ADDR_EXPR of INTEGER_CST which
9212 don't really make sense outside of constant expression evaluation. Also
9213 we want to allow folding to COMPONENT_REF, which could cause trouble
9214 with TBAA in fold_indirect_ref_1.
9216 Try to keep this function synced with fold_indirect_ref_1. */
9218 static tree
9219 cxx_fold_indirect_ref (location_t loc, tree type, tree op0, bool *empty_base)
9221 tree sub, subtype;
9223 sub = op0;
9224 STRIP_NOPS (sub);
9225 subtype = TREE_TYPE (sub);
9226 if (!POINTER_TYPE_P (subtype))
9227 return NULL_TREE;
9229 if (TREE_CODE (sub) == ADDR_EXPR)
9231 tree op = TREE_OPERAND (sub, 0);
9232 tree optype = TREE_TYPE (op);
9234 /* *&CONST_DECL -> to the value of the const decl. */
9235 if (TREE_CODE (op) == CONST_DECL)
9236 return DECL_INITIAL (op);
9237 /* *&p => p; make sure to handle *&"str"[cst] here. */
9238 if (same_type_ignoring_top_level_qualifiers_p (optype, type))
9240 tree fop = fold_read_from_constant_string (op);
9241 if (fop)
9242 return fop;
9243 else
9244 return op;
9246 /* *(foo *)&fooarray => fooarray[0] */
9247 else if (TREE_CODE (optype) == ARRAY_TYPE
9248 && (same_type_ignoring_top_level_qualifiers_p
9249 (type, TREE_TYPE (optype))))
9251 tree type_domain = TYPE_DOMAIN (optype);
9252 tree min_val = size_zero_node;
9253 if (type_domain && TYPE_MIN_VALUE (type_domain))
9254 min_val = TYPE_MIN_VALUE (type_domain);
9255 return build4_loc (loc, ARRAY_REF, type, op, min_val,
9256 NULL_TREE, NULL_TREE);
9258 /* *(foo *)&complexfoo => __real__ complexfoo */
9259 else if (TREE_CODE (optype) == COMPLEX_TYPE
9260 && (same_type_ignoring_top_level_qualifiers_p
9261 (type, TREE_TYPE (optype))))
9262 return fold_build1_loc (loc, REALPART_EXPR, type, op);
9263 /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
9264 else if (TREE_CODE (optype) == VECTOR_TYPE
9265 && (same_type_ignoring_top_level_qualifiers_p
9266 (type, TREE_TYPE (optype))))
9268 tree part_width = TYPE_SIZE (type);
9269 tree index = bitsize_int (0);
9270 return fold_build3_loc (loc, BIT_FIELD_REF, type, op, part_width, index);
9272 /* Also handle conversion to an empty base class, which
9273 is represented with a NOP_EXPR. */
9274 else if (is_empty_class (type)
9275 && CLASS_TYPE_P (optype)
9276 && DERIVED_FROM_P (type, optype))
9278 *empty_base = true;
9279 return op;
9281 /* *(foo *)&struct_with_foo_field => COMPONENT_REF */
9282 else if (RECORD_OR_UNION_TYPE_P (optype))
9284 tree field = TYPE_FIELDS (optype);
9285 for (; field; field = DECL_CHAIN (field))
9286 if (TREE_CODE (field) == FIELD_DECL
9287 && integer_zerop (byte_position (field))
9288 && (same_type_ignoring_top_level_qualifiers_p
9289 (TREE_TYPE (field), type)))
9291 return fold_build3 (COMPONENT_REF, type, op, field, NULL_TREE);
9292 break;
9296 else if (TREE_CODE (sub) == POINTER_PLUS_EXPR
9297 && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST)
9299 tree op00 = TREE_OPERAND (sub, 0);
9300 tree op01 = TREE_OPERAND (sub, 1);
9302 STRIP_NOPS (op00);
9303 if (TREE_CODE (op00) == ADDR_EXPR)
9305 tree op00type;
9306 op00 = TREE_OPERAND (op00, 0);
9307 op00type = TREE_TYPE (op00);
9309 /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
9310 if (TREE_CODE (op00type) == VECTOR_TYPE
9311 && (same_type_ignoring_top_level_qualifiers_p
9312 (type, TREE_TYPE (op00type))))
9314 HOST_WIDE_INT offset = tree_to_shwi (op01);
9315 tree part_width = TYPE_SIZE (type);
9316 unsigned HOST_WIDE_INT part_widthi = tree_to_shwi (part_width)/BITS_PER_UNIT;
9317 unsigned HOST_WIDE_INT indexi = offset * BITS_PER_UNIT;
9318 tree index = bitsize_int (indexi);
9320 if (offset / part_widthi < TYPE_VECTOR_SUBPARTS (op00type))
9321 return fold_build3_loc (loc,
9322 BIT_FIELD_REF, type, op00,
9323 part_width, index);
9326 /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
9327 else if (TREE_CODE (op00type) == COMPLEX_TYPE
9328 && (same_type_ignoring_top_level_qualifiers_p
9329 (type, TREE_TYPE (op00type))))
9331 tree size = TYPE_SIZE_UNIT (type);
9332 if (tree_int_cst_equal (size, op01))
9333 return fold_build1_loc (loc, IMAGPART_EXPR, type, op00);
9335 /* ((foo *)&fooarray)[1] => fooarray[1] */
9336 else if (TREE_CODE (op00type) == ARRAY_TYPE
9337 && (same_type_ignoring_top_level_qualifiers_p
9338 (type, TREE_TYPE (op00type))))
9340 tree type_domain = TYPE_DOMAIN (op00type);
9341 tree min_val = size_zero_node;
9342 if (type_domain && TYPE_MIN_VALUE (type_domain))
9343 min_val = TYPE_MIN_VALUE (type_domain);
9344 op01 = size_binop_loc (loc, EXACT_DIV_EXPR, op01,
9345 TYPE_SIZE_UNIT (type));
9346 op01 = size_binop_loc (loc, PLUS_EXPR, op01, min_val);
9347 return build4_loc (loc, ARRAY_REF, type, op00, op01,
9348 NULL_TREE, NULL_TREE);
9350 /* Also handle conversion to an empty base class, which
9351 is represented with a NOP_EXPR. */
9352 else if (is_empty_class (type)
9353 && CLASS_TYPE_P (op00type)
9354 && DERIVED_FROM_P (type, op00type))
9356 *empty_base = true;
9357 return op00;
9359 /* ((foo *)&struct_with_foo_field)[1] => COMPONENT_REF */
9360 else if (RECORD_OR_UNION_TYPE_P (op00type))
9362 tree field = TYPE_FIELDS (op00type);
9363 for (; field; field = DECL_CHAIN (field))
9364 if (TREE_CODE (field) == FIELD_DECL
9365 && tree_int_cst_equal (byte_position (field), op01)
9366 && (same_type_ignoring_top_level_qualifiers_p
9367 (TREE_TYPE (field), type)))
9369 return fold_build3 (COMPONENT_REF, type, op00,
9370 field, NULL_TREE);
9371 break;
9376 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
9377 else if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
9378 && (same_type_ignoring_top_level_qualifiers_p
9379 (type, TREE_TYPE (TREE_TYPE (subtype)))))
9381 tree type_domain;
9382 tree min_val = size_zero_node;
9383 tree newsub = cxx_fold_indirect_ref (loc, TREE_TYPE (subtype), sub, NULL);
9384 if (newsub)
9385 sub = newsub;
9386 else
9387 sub = build1_loc (loc, INDIRECT_REF, TREE_TYPE (subtype), sub);
9388 type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
9389 if (type_domain && TYPE_MIN_VALUE (type_domain))
9390 min_val = TYPE_MIN_VALUE (type_domain);
9391 return build4_loc (loc, ARRAY_REF, type, sub, min_val, NULL_TREE,
9392 NULL_TREE);
9395 return NULL_TREE;
9398 static tree
9399 cxx_eval_indirect_ref (const constexpr_call *call, tree t,
9400 bool allow_non_constant, bool addr,
9401 bool *non_constant_p, bool *overflow_p)
9403 tree orig_op0 = TREE_OPERAND (t, 0);
9404 tree op0 = cxx_eval_constant_expression (call, orig_op0, allow_non_constant,
9405 /*addr*/false, non_constant_p, overflow_p);
9406 bool empty_base = false;
9407 tree r;
9409 /* Don't VERIFY_CONSTANT here. */
9410 if (*non_constant_p)
9411 return t;
9413 r = cxx_fold_indirect_ref (EXPR_LOCATION (t), TREE_TYPE (t), op0,
9414 &empty_base);
9416 if (r)
9417 r = cxx_eval_constant_expression (call, r, allow_non_constant,
9418 addr, non_constant_p, overflow_p);
9419 else
9421 tree sub = op0;
9422 STRIP_NOPS (sub);
9423 if (TREE_CODE (sub) == ADDR_EXPR)
9425 /* We couldn't fold to a constant value. Make sure it's not
9426 something we should have been able to fold. */
9427 gcc_assert (!same_type_ignoring_top_level_qualifiers_p
9428 (TREE_TYPE (TREE_TYPE (sub)), TREE_TYPE (t)));
9429 /* DR 1188 says we don't have to deal with this. */
9430 if (!allow_non_constant)
9431 error ("accessing value of %qE through a %qT glvalue in a "
9432 "constant expression", build_fold_indirect_ref (sub),
9433 TREE_TYPE (t));
9434 *non_constant_p = true;
9435 return t;
9439 /* If we're pulling out the value of an empty base, make sure
9440 that the whole object is constant and then return an empty
9441 CONSTRUCTOR. */
9442 if (empty_base)
9444 VERIFY_CONSTANT (r);
9445 r = build_constructor (TREE_TYPE (t), NULL);
9446 TREE_CONSTANT (r) = true;
9449 if (r == NULL_TREE)
9451 if (addr && op0 != orig_op0)
9452 return build1 (INDIRECT_REF, TREE_TYPE (t), op0);
9453 if (!addr)
9454 VERIFY_CONSTANT (t);
9455 return t;
9457 return r;
9460 /* Complain about R, a VAR_DECL, not being usable in a constant expression.
9461 Shared between potential_constant_expression and
9462 cxx_eval_constant_expression. */
9464 static void
9465 non_const_var_error (tree r)
9467 tree type = TREE_TYPE (r);
9468 error ("the value of %qD is not usable in a constant "
9469 "expression", r);
9470 /* Avoid error cascade. */
9471 if (DECL_INITIAL (r) == error_mark_node)
9472 return;
9473 if (DECL_DECLARED_CONSTEXPR_P (r))
9474 inform (DECL_SOURCE_LOCATION (r),
9475 "%qD used in its own initializer", r);
9476 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
9478 if (!CP_TYPE_CONST_P (type))
9479 inform (DECL_SOURCE_LOCATION (r),
9480 "%q#D is not const", r);
9481 else if (CP_TYPE_VOLATILE_P (type))
9482 inform (DECL_SOURCE_LOCATION (r),
9483 "%q#D is volatile", r);
9484 else if (!DECL_INITIAL (r)
9485 || !TREE_CONSTANT (DECL_INITIAL (r)))
9486 inform (DECL_SOURCE_LOCATION (r),
9487 "%qD was not initialized with a constant "
9488 "expression", r);
9489 else
9490 gcc_unreachable ();
9492 else
9494 if (cxx_dialect >= cxx11 && !DECL_DECLARED_CONSTEXPR_P (r))
9495 inform (DECL_SOURCE_LOCATION (r),
9496 "%qD was not declared %<constexpr%>", r);
9497 else
9498 inform (DECL_SOURCE_LOCATION (r),
9499 "%qD does not have integral or enumeration type",
9504 /* Subroutine of cxx_eval_constant_expression.
9505 Like cxx_eval_unary_expression, except for trinary expressions. */
9507 static tree
9508 cxx_eval_trinary_expression (const constexpr_call *call, tree t,
9509 bool allow_non_constant, bool addr,
9510 bool *non_constant_p, bool *overflow_p)
9512 int i;
9513 tree args[3];
9514 tree val;
9516 for (i = 0; i < 3; i++)
9518 args[i] = cxx_eval_constant_expression (call, TREE_OPERAND (t, i),
9519 allow_non_constant, addr,
9520 non_constant_p, overflow_p);
9521 VERIFY_CONSTANT (args[i]);
9524 val = fold_ternary_loc (EXPR_LOCATION (t), TREE_CODE (t), TREE_TYPE (t),
9525 args[0], args[1], args[2]);
9526 if (val == NULL_TREE)
9527 return t;
9528 VERIFY_CONSTANT (val);
9529 return val;
9532 /* Attempt to reduce the expression T to a constant value.
9533 On failure, issue diagnostic and return error_mark_node. */
9534 /* FIXME unify with c_fully_fold */
9536 static tree
9537 cxx_eval_constant_expression (const constexpr_call *call, tree t,
9538 bool allow_non_constant, bool addr,
9539 bool *non_constant_p, bool *overflow_p)
9541 tree r = t;
9543 if (t == error_mark_node)
9545 *non_constant_p = true;
9546 return t;
9548 if (CONSTANT_CLASS_P (t))
9550 if (TREE_CODE (t) == PTRMEM_CST)
9551 t = cplus_expand_constant (t);
9552 else if (TREE_OVERFLOW (t) && (!flag_permissive || allow_non_constant))
9553 *overflow_p = true;
9554 return t;
9556 if (TREE_CODE (t) != NOP_EXPR
9557 && reduced_constant_expression_p (t))
9558 return fold (t);
9560 switch (TREE_CODE (t))
9562 case VAR_DECL:
9563 if (addr)
9564 return t;
9565 /* else fall through. */
9566 case CONST_DECL:
9567 r = integral_constant_value (t);
9568 if (TREE_CODE (r) == TARGET_EXPR
9569 && TREE_CODE (TARGET_EXPR_INITIAL (r)) == CONSTRUCTOR)
9570 r = TARGET_EXPR_INITIAL (r);
9571 if (DECL_P (r))
9573 if (!allow_non_constant)
9574 non_const_var_error (r);
9575 *non_constant_p = true;
9577 break;
9579 case FUNCTION_DECL:
9580 case TEMPLATE_DECL:
9581 case LABEL_DECL:
9582 return t;
9584 case PARM_DECL:
9585 if (call && DECL_CONTEXT (t) == call->fundef->decl)
9587 if (DECL_ARTIFICIAL (t) && DECL_CONSTRUCTOR_P (DECL_CONTEXT (t)))
9589 if (!allow_non_constant)
9590 sorry ("use of the value of the object being constructed "
9591 "in a constant expression");
9592 *non_constant_p = true;
9594 else
9595 r = lookup_parameter_binding (call, t);
9597 else if (addr)
9598 /* Defer in case this is only used for its type. */;
9599 else
9601 if (!allow_non_constant)
9602 error ("%qE is not a constant expression", t);
9603 *non_constant_p = true;
9605 break;
9607 case CALL_EXPR:
9608 case AGGR_INIT_EXPR:
9609 r = cxx_eval_call_expression (call, t, allow_non_constant, addr,
9610 non_constant_p, overflow_p);
9611 break;
9613 case TARGET_EXPR:
9614 if (!literal_type_p (TREE_TYPE (t)))
9616 if (!allow_non_constant)
9618 error ("temporary of non-literal type %qT in a "
9619 "constant expression", TREE_TYPE (t));
9620 explain_non_literal_class (TREE_TYPE (t));
9622 *non_constant_p = true;
9623 break;
9625 /* else fall through. */
9626 case INIT_EXPR:
9627 /* Pass false for 'addr' because these codes indicate
9628 initialization of a temporary. */
9629 r = cxx_eval_constant_expression (call, TREE_OPERAND (t, 1),
9630 allow_non_constant, false,
9631 non_constant_p, overflow_p);
9632 if (!*non_constant_p)
9633 /* Adjust the type of the result to the type of the temporary. */
9634 r = adjust_temp_type (TREE_TYPE (t), r);
9635 break;
9637 case SCOPE_REF:
9638 r = cxx_eval_constant_expression (call, TREE_OPERAND (t, 1),
9639 allow_non_constant, addr,
9640 non_constant_p, overflow_p);
9641 break;
9643 case RETURN_EXPR:
9644 case NON_LVALUE_EXPR:
9645 case TRY_CATCH_EXPR:
9646 case CLEANUP_POINT_EXPR:
9647 case MUST_NOT_THROW_EXPR:
9648 case SAVE_EXPR:
9649 r = cxx_eval_constant_expression (call, TREE_OPERAND (t, 0),
9650 allow_non_constant, addr,
9651 non_constant_p, overflow_p);
9652 break;
9654 /* These differ from cxx_eval_unary_expression in that this doesn't
9655 check for a constant operand or result; an address can be
9656 constant without its operand being, and vice versa. */
9657 case INDIRECT_REF:
9658 r = cxx_eval_indirect_ref (call, t, allow_non_constant, addr,
9659 non_constant_p, overflow_p);
9660 break;
9662 case ADDR_EXPR:
9664 tree oldop = TREE_OPERAND (t, 0);
9665 tree op = cxx_eval_constant_expression (call, oldop,
9666 allow_non_constant,
9667 /*addr*/true,
9668 non_constant_p, overflow_p);
9669 /* Don't VERIFY_CONSTANT here. */
9670 if (*non_constant_p)
9671 return t;
9672 /* This function does more aggressive folding than fold itself. */
9673 r = build_fold_addr_expr_with_type (op, TREE_TYPE (t));
9674 if (TREE_CODE (r) == ADDR_EXPR && TREE_OPERAND (r, 0) == oldop)
9675 return t;
9676 break;
9679 case REALPART_EXPR:
9680 case IMAGPART_EXPR:
9681 case CONJ_EXPR:
9682 case FIX_TRUNC_EXPR:
9683 case FLOAT_EXPR:
9684 case NEGATE_EXPR:
9685 case ABS_EXPR:
9686 case BIT_NOT_EXPR:
9687 case TRUTH_NOT_EXPR:
9688 case FIXED_CONVERT_EXPR:
9689 r = cxx_eval_unary_expression (call, t, allow_non_constant, addr,
9690 non_constant_p, overflow_p);
9691 break;
9693 case SIZEOF_EXPR:
9694 if (SIZEOF_EXPR_TYPE_P (t))
9695 r = cxx_sizeof_or_alignof_type (TREE_TYPE (TREE_OPERAND (t, 0)),
9696 SIZEOF_EXPR, false);
9697 else if (TYPE_P (TREE_OPERAND (t, 0)))
9698 r = cxx_sizeof_or_alignof_type (TREE_OPERAND (t, 0), SIZEOF_EXPR,
9699 false);
9700 else
9701 r = cxx_sizeof_or_alignof_expr (TREE_OPERAND (t, 0), SIZEOF_EXPR,
9702 false);
9703 if (r == error_mark_node)
9704 r = size_one_node;
9705 VERIFY_CONSTANT (r);
9706 break;
9708 case COMPOUND_EXPR:
9710 /* check_return_expr sometimes wraps a TARGET_EXPR in a
9711 COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR
9712 introduced by build_call_a. */
9713 tree op0 = TREE_OPERAND (t, 0);
9714 tree op1 = TREE_OPERAND (t, 1);
9715 STRIP_NOPS (op1);
9716 if ((TREE_CODE (op0) == TARGET_EXPR && op1 == TARGET_EXPR_SLOT (op0))
9717 || TREE_CODE (op1) == EMPTY_CLASS_EXPR)
9718 r = cxx_eval_constant_expression (call, op0, allow_non_constant,
9719 addr, non_constant_p, overflow_p);
9720 else
9722 /* Check that the LHS is constant and then discard it. */
9723 cxx_eval_constant_expression (call, op0, allow_non_constant,
9724 false, non_constant_p, overflow_p);
9725 op1 = TREE_OPERAND (t, 1);
9726 r = cxx_eval_constant_expression (call, op1, allow_non_constant,
9727 addr, non_constant_p, overflow_p);
9730 break;
9732 case POINTER_PLUS_EXPR:
9733 case PLUS_EXPR:
9734 case MINUS_EXPR:
9735 case MULT_EXPR:
9736 case TRUNC_DIV_EXPR:
9737 case CEIL_DIV_EXPR:
9738 case FLOOR_DIV_EXPR:
9739 case ROUND_DIV_EXPR:
9740 case TRUNC_MOD_EXPR:
9741 case CEIL_MOD_EXPR:
9742 case ROUND_MOD_EXPR:
9743 case RDIV_EXPR:
9744 case EXACT_DIV_EXPR:
9745 case MIN_EXPR:
9746 case MAX_EXPR:
9747 case LSHIFT_EXPR:
9748 case RSHIFT_EXPR:
9749 case LROTATE_EXPR:
9750 case RROTATE_EXPR:
9751 case BIT_IOR_EXPR:
9752 case BIT_XOR_EXPR:
9753 case BIT_AND_EXPR:
9754 case TRUTH_XOR_EXPR:
9755 case LT_EXPR:
9756 case LE_EXPR:
9757 case GT_EXPR:
9758 case GE_EXPR:
9759 case EQ_EXPR:
9760 case NE_EXPR:
9761 case UNORDERED_EXPR:
9762 case ORDERED_EXPR:
9763 case UNLT_EXPR:
9764 case UNLE_EXPR:
9765 case UNGT_EXPR:
9766 case UNGE_EXPR:
9767 case UNEQ_EXPR:
9768 case LTGT_EXPR:
9769 case RANGE_EXPR:
9770 case COMPLEX_EXPR:
9771 r = cxx_eval_binary_expression (call, t, allow_non_constant, addr,
9772 non_constant_p, overflow_p);
9773 break;
9775 /* fold can introduce non-IF versions of these; still treat them as
9776 short-circuiting. */
9777 case TRUTH_AND_EXPR:
9778 case TRUTH_ANDIF_EXPR:
9779 r = cxx_eval_logical_expression (call, t, boolean_false_node,
9780 boolean_true_node,
9781 allow_non_constant, addr,
9782 non_constant_p, overflow_p);
9783 break;
9785 case TRUTH_OR_EXPR:
9786 case TRUTH_ORIF_EXPR:
9787 r = cxx_eval_logical_expression (call, t, boolean_true_node,
9788 boolean_false_node,
9789 allow_non_constant, addr,
9790 non_constant_p, overflow_p);
9791 break;
9793 case ARRAY_REF:
9794 r = cxx_eval_array_reference (call, t, allow_non_constant, addr,
9795 non_constant_p, overflow_p);
9796 break;
9798 case COMPONENT_REF:
9799 if (is_overloaded_fn (t))
9801 /* We can only get here in checking mode via
9802 build_non_dependent_expr, because any expression that
9803 calls or takes the address of the function will have
9804 pulled a FUNCTION_DECL out of the COMPONENT_REF. */
9805 gcc_checking_assert (allow_non_constant || errorcount);
9806 *non_constant_p = true;
9807 return t;
9809 r = cxx_eval_component_reference (call, t, allow_non_constant, addr,
9810 non_constant_p, overflow_p);
9811 break;
9813 case BIT_FIELD_REF:
9814 r = cxx_eval_bit_field_ref (call, t, allow_non_constant, addr,
9815 non_constant_p, overflow_p);
9816 break;
9818 case COND_EXPR:
9819 case VEC_COND_EXPR:
9820 r = cxx_eval_conditional_expression (call, t, allow_non_constant, addr,
9821 non_constant_p, overflow_p);
9822 break;
9824 case CONSTRUCTOR:
9825 r = cxx_eval_bare_aggregate (call, t, allow_non_constant, addr,
9826 non_constant_p, overflow_p);
9827 break;
9829 case VEC_INIT_EXPR:
9830 /* We can get this in a defaulted constructor for a class with a
9831 non-static data member of array type. Either the initializer will
9832 be NULL, meaning default-initialization, or it will be an lvalue
9833 or xvalue of the same type, meaning direct-initialization from the
9834 corresponding member. */
9835 r = cxx_eval_vec_init (call, t, allow_non_constant, addr,
9836 non_constant_p, overflow_p);
9837 break;
9839 case FMA_EXPR:
9840 case VEC_PERM_EXPR:
9841 r = cxx_eval_trinary_expression (call, t, allow_non_constant, addr,
9842 non_constant_p, overflow_p);
9843 break;
9845 case CONVERT_EXPR:
9846 case VIEW_CONVERT_EXPR:
9847 case NOP_EXPR:
9849 tree oldop = TREE_OPERAND (t, 0);
9850 tree op = cxx_eval_constant_expression (call, oldop,
9851 allow_non_constant, addr,
9852 non_constant_p, overflow_p);
9853 if (*non_constant_p)
9854 return t;
9855 if (POINTER_TYPE_P (TREE_TYPE (t))
9856 && TREE_CODE (op) == INTEGER_CST
9857 && !integer_zerop (op))
9859 if (!allow_non_constant)
9860 error_at (EXPR_LOC_OR_LOC (t, input_location),
9861 "reinterpret_cast from integer to pointer");
9862 *non_constant_p = true;
9863 return t;
9865 if (op == oldop)
9866 /* We didn't fold at the top so we could check for ptr-int
9867 conversion. */
9868 return fold (t);
9869 r = fold_build1 (TREE_CODE (t), TREE_TYPE (t), op);
9870 /* Conversion of an out-of-range value has implementation-defined
9871 behavior; the language considers it different from arithmetic
9872 overflow, which is undefined. */
9873 if (TREE_OVERFLOW_P (r) && !TREE_OVERFLOW_P (op))
9874 TREE_OVERFLOW (r) = false;
9876 break;
9878 case EMPTY_CLASS_EXPR:
9879 /* This is good enough for a function argument that might not get
9880 used, and they can't do anything with it, so just return it. */
9881 return t;
9883 case LAMBDA_EXPR:
9884 case PREINCREMENT_EXPR:
9885 case POSTINCREMENT_EXPR:
9886 case PREDECREMENT_EXPR:
9887 case POSTDECREMENT_EXPR:
9888 case NEW_EXPR:
9889 case VEC_NEW_EXPR:
9890 case DELETE_EXPR:
9891 case VEC_DELETE_EXPR:
9892 case THROW_EXPR:
9893 case MODIFY_EXPR:
9894 case MODOP_EXPR:
9895 /* GCC internal stuff. */
9896 case VA_ARG_EXPR:
9897 case OBJ_TYPE_REF:
9898 case WITH_CLEANUP_EXPR:
9899 case STATEMENT_LIST:
9900 case BIND_EXPR:
9901 case NON_DEPENDENT_EXPR:
9902 case BASELINK:
9903 case EXPR_STMT:
9904 case OFFSET_REF:
9905 if (!allow_non_constant)
9906 error_at (EXPR_LOC_OR_LOC (t, input_location),
9907 "expression %qE is not a constant-expression", t);
9908 *non_constant_p = true;
9909 break;
9911 default:
9912 internal_error ("unexpected expression %qE of kind %s", t,
9913 get_tree_code_name (TREE_CODE (t)));
9914 *non_constant_p = true;
9915 break;
9918 if (r == error_mark_node)
9919 *non_constant_p = true;
9921 if (*non_constant_p)
9922 return t;
9923 else
9924 return r;
9927 static tree
9928 cxx_eval_outermost_constant_expr (tree t, bool allow_non_constant)
9930 bool non_constant_p = false;
9931 bool overflow_p = false;
9932 tree r = cxx_eval_constant_expression (NULL, t, allow_non_constant,
9933 false, &non_constant_p, &overflow_p);
9935 verify_constant (r, allow_non_constant, &non_constant_p, &overflow_p);
9937 if (TREE_CODE (t) != CONSTRUCTOR
9938 && cp_has_mutable_p (TREE_TYPE (t)))
9940 /* We allow a mutable type if the original expression was a
9941 CONSTRUCTOR so that we can do aggregate initialization of
9942 constexpr variables. */
9943 if (!allow_non_constant)
9944 error ("%qT cannot be the type of a complete constant expression "
9945 "because it has mutable sub-objects", TREE_TYPE (t));
9946 non_constant_p = true;
9949 /* Technically we should check this for all subexpressions, but that
9950 runs into problems with our internal representation of pointer
9951 subtraction and the 5.19 rules are still in flux. */
9952 if (CONVERT_EXPR_CODE_P (TREE_CODE (r))
9953 && ARITHMETIC_TYPE_P (TREE_TYPE (r))
9954 && TREE_CODE (TREE_OPERAND (r, 0)) == ADDR_EXPR)
9956 if (!allow_non_constant)
9957 error ("conversion from pointer type %qT "
9958 "to arithmetic type %qT in a constant-expression",
9959 TREE_TYPE (TREE_OPERAND (r, 0)), TREE_TYPE (r));
9960 non_constant_p = true;
9963 if (!non_constant_p && overflow_p)
9964 non_constant_p = true;
9966 if (non_constant_p && !allow_non_constant)
9967 return error_mark_node;
9968 else if (non_constant_p && TREE_CONSTANT (r))
9970 /* This isn't actually constant, so unset TREE_CONSTANT. */
9971 if (EXPR_P (r))
9972 r = copy_node (r);
9973 else if (TREE_CODE (r) == CONSTRUCTOR)
9974 r = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (r), r);
9975 else
9976 r = build_nop (TREE_TYPE (r), r);
9977 TREE_CONSTANT (r) = false;
9979 else if (non_constant_p || r == t)
9980 return t;
9982 if (TREE_CODE (r) == CONSTRUCTOR && CLASS_TYPE_P (TREE_TYPE (r)))
9984 if (TREE_CODE (t) == TARGET_EXPR
9985 && TARGET_EXPR_INITIAL (t) == r)
9986 return t;
9987 else
9989 r = get_target_expr (r);
9990 TREE_CONSTANT (r) = true;
9991 return r;
9994 else
9995 return r;
9998 /* Returns true if T is a valid subexpression of a constant expression,
9999 even if it isn't itself a constant expression. */
10001 bool
10002 is_sub_constant_expr (tree t)
10004 bool non_constant_p = false;
10005 bool overflow_p = false;
10006 cxx_eval_constant_expression (NULL, t, true, false, &non_constant_p,
10007 &overflow_p);
10008 return !non_constant_p && !overflow_p;
10011 /* If T represents a constant expression returns its reduced value.
10012 Otherwise return error_mark_node. If T is dependent, then
10013 return NULL. */
10015 tree
10016 cxx_constant_value (tree t)
10018 return cxx_eval_outermost_constant_expr (t, false);
10021 /* If T is a constant expression, returns its reduced value.
10022 Otherwise, if T does not have TREE_CONSTANT set, returns T.
10023 Otherwise, returns a version of T without TREE_CONSTANT. */
10025 tree
10026 maybe_constant_value (tree t)
10028 tree r;
10030 if (instantiation_dependent_expression_p (t)
10031 || type_unknown_p (t)
10032 || BRACE_ENCLOSED_INITIALIZER_P (t)
10033 || !potential_constant_expression (t))
10035 if (TREE_OVERFLOW_P (t))
10037 t = build_nop (TREE_TYPE (t), t);
10038 TREE_CONSTANT (t) = false;
10040 return t;
10043 r = cxx_eval_outermost_constant_expr (t, true);
10044 #ifdef ENABLE_CHECKING
10045 /* cp_tree_equal looks through NOPs, so allow them. */
10046 gcc_assert (r == t
10047 || CONVERT_EXPR_P (t)
10048 || (TREE_CONSTANT (t) && !TREE_CONSTANT (r))
10049 || !cp_tree_equal (r, t));
10050 #endif
10051 return r;
10054 /* Like maybe_constant_value, but returns a CONSTRUCTOR directly, rather
10055 than wrapped in a TARGET_EXPR. */
10057 tree
10058 maybe_constant_init (tree t)
10060 t = maybe_constant_value (t);
10061 if (TREE_CODE (t) == TARGET_EXPR)
10063 tree init = TARGET_EXPR_INITIAL (t);
10064 if (TREE_CODE (init) == CONSTRUCTOR)
10065 t = init;
10067 return t;
10070 #if 0
10071 /* FIXME see ADDR_EXPR section in potential_constant_expression_1. */
10072 /* Return true if the object referred to by REF has automatic or thread
10073 local storage. */
10075 enum { ck_ok, ck_bad, ck_unknown };
10076 static int
10077 check_automatic_or_tls (tree ref)
10079 enum machine_mode mode;
10080 HOST_WIDE_INT bitsize, bitpos;
10081 tree offset;
10082 int volatilep = 0, unsignedp = 0;
10083 tree decl = get_inner_reference (ref, &bitsize, &bitpos, &offset,
10084 &mode, &unsignedp, &volatilep, false);
10085 duration_kind dk;
10087 /* If there isn't a decl in the middle, we don't know the linkage here,
10088 and this isn't a constant expression anyway. */
10089 if (!DECL_P (decl))
10090 return ck_unknown;
10091 dk = decl_storage_duration (decl);
10092 return (dk == dk_auto || dk == dk_thread) ? ck_bad : ck_ok;
10094 #endif
10096 /* Return true if T denotes a potentially constant expression. Issue
10097 diagnostic as appropriate under control of FLAGS. If WANT_RVAL is true,
10098 an lvalue-rvalue conversion is implied.
10100 C++0x [expr.const] used to say
10102 6 An expression is a potential constant expression if it is
10103 a constant expression where all occurrences of function
10104 parameters are replaced by arbitrary constant expressions
10105 of the appropriate type.
10107 2 A conditional expression is a constant expression unless it
10108 involves one of the following as a potentially evaluated
10109 subexpression (3.2), but subexpressions of logical AND (5.14),
10110 logical OR (5.15), and conditional (5.16) operations that are
10111 not evaluated are not considered. */
10113 static bool
10114 potential_constant_expression_1 (tree t, bool want_rval, tsubst_flags_t flags)
10116 enum { any = false, rval = true };
10117 int i;
10118 tree tmp;
10120 if (t == error_mark_node)
10121 return false;
10122 if (t == NULL_TREE)
10123 return true;
10124 if (TREE_THIS_VOLATILE (t))
10126 if (flags & tf_error)
10127 error ("expression %qE has side-effects", t);
10128 return false;
10130 if (CONSTANT_CLASS_P (t))
10131 return true;
10133 switch (TREE_CODE (t))
10135 case FUNCTION_DECL:
10136 case BASELINK:
10137 case TEMPLATE_DECL:
10138 case OVERLOAD:
10139 case TEMPLATE_ID_EXPR:
10140 case LABEL_DECL:
10141 case LABEL_EXPR:
10142 case CONST_DECL:
10143 case SIZEOF_EXPR:
10144 case ALIGNOF_EXPR:
10145 case OFFSETOF_EXPR:
10146 case NOEXCEPT_EXPR:
10147 case TEMPLATE_PARM_INDEX:
10148 case TRAIT_EXPR:
10149 case IDENTIFIER_NODE:
10150 case USERDEF_LITERAL:
10151 /* We can see a FIELD_DECL in a pointer-to-member expression. */
10152 case FIELD_DECL:
10153 case PARM_DECL:
10154 case USING_DECL:
10155 case REQUIRES_EXPR:
10156 case EXPR_REQ:
10157 case TYPE_REQ:
10158 case NESTED_REQ:
10159 case VALIDEXPR_EXPR:
10160 case VALIDTYPE_EXPR:
10161 case CONSTEXPR_EXPR:
10162 return true;
10164 case AGGR_INIT_EXPR:
10165 case CALL_EXPR:
10166 /* -- an invocation of a function other than a constexpr function
10167 or a constexpr constructor. */
10169 tree fun = get_function_named_in_call (t);
10170 const int nargs = call_expr_nargs (t);
10171 i = 0;
10173 if (is_overloaded_fn (fun))
10175 if (TREE_CODE (fun) == FUNCTION_DECL)
10177 if (builtin_valid_in_constant_expr_p (fun))
10178 return true;
10179 if (!DECL_DECLARED_CONSTEXPR_P (fun)
10180 /* Allow any built-in function; if the expansion
10181 isn't constant, we'll deal with that then. */
10182 && !is_builtin_fn (fun))
10184 if (flags & tf_error)
10186 error_at (EXPR_LOC_OR_LOC (t, input_location),
10187 "call to non-constexpr function %qD", fun);
10188 explain_invalid_constexpr_fn (fun);
10190 return false;
10192 /* A call to a non-static member function takes the address
10193 of the object as the first argument. But in a constant
10194 expression the address will be folded away, so look
10195 through it now. */
10196 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fun)
10197 && !DECL_CONSTRUCTOR_P (fun))
10199 tree x = get_nth_callarg (t, 0);
10200 if (is_this_parameter (x))
10202 if (DECL_CONTEXT (x) == NULL_TREE
10203 || DECL_CONSTRUCTOR_P (DECL_CONTEXT (x)))
10205 if (flags & tf_error)
10206 sorry ("calling a member function of the "
10207 "object being constructed in a constant "
10208 "expression");
10209 return false;
10211 /* Otherwise OK. */;
10213 else if (!potential_constant_expression_1 (x, rval, flags))
10214 return false;
10215 i = 1;
10218 else
10220 if (!potential_constant_expression_1 (fun, true, flags))
10221 return false;
10222 fun = get_first_fn (fun);
10224 /* Skip initial arguments to base constructors. */
10225 if (DECL_BASE_CONSTRUCTOR_P (fun))
10226 i = num_artificial_parms_for (fun);
10227 fun = DECL_ORIGIN (fun);
10229 else
10231 if (potential_constant_expression_1 (fun, rval, flags))
10232 /* Might end up being a constant function pointer. */;
10233 else
10234 return false;
10236 for (; i < nargs; ++i)
10238 tree x = get_nth_callarg (t, i);
10239 if (!potential_constant_expression_1 (x, rval, flags))
10240 return false;
10242 return true;
10245 case NON_LVALUE_EXPR:
10246 /* -- an lvalue-to-rvalue conversion (4.1) unless it is applied to
10247 -- an lvalue of integral type that refers to a non-volatile
10248 const variable or static data member initialized with
10249 constant expressions, or
10251 -- an lvalue of literal type that refers to non-volatile
10252 object defined with constexpr, or that refers to a
10253 sub-object of such an object; */
10254 return potential_constant_expression_1 (TREE_OPERAND (t, 0), rval, flags);
10256 case VAR_DECL:
10257 if (want_rval && !decl_constant_var_p (t)
10258 && !dependent_type_p (TREE_TYPE (t)))
10260 if (flags & tf_error)
10261 non_const_var_error (t);
10262 return false;
10264 return true;
10266 case NOP_EXPR:
10267 case CONVERT_EXPR:
10268 case VIEW_CONVERT_EXPR:
10269 /* -- a reinterpret_cast. FIXME not implemented, and this rule
10270 may change to something more specific to type-punning (DR 1312). */
10272 tree from = TREE_OPERAND (t, 0);
10273 if (POINTER_TYPE_P (TREE_TYPE (t))
10274 && TREE_CODE (from) == INTEGER_CST
10275 && !integer_zerop (from))
10277 if (flags & tf_error)
10278 error_at (EXPR_LOC_OR_LOC (t, input_location),
10279 "reinterpret_cast from integer to pointer");
10280 return false;
10282 return (potential_constant_expression_1
10283 (from, TREE_CODE (t) != VIEW_CONVERT_EXPR, flags));
10286 case ADDR_EXPR:
10287 /* -- a unary operator & that is applied to an lvalue that
10288 designates an object with thread or automatic storage
10289 duration; */
10290 t = TREE_OPERAND (t, 0);
10291 #if 0
10292 /* FIXME adjust when issue 1197 is fully resolved. For now don't do
10293 any checking here, as we might dereference the pointer later. If
10294 we remove this code, also remove check_automatic_or_tls. */
10295 i = check_automatic_or_tls (t);
10296 if (i == ck_ok)
10297 return true;
10298 if (i == ck_bad)
10300 if (flags & tf_error)
10301 error ("address-of an object %qE with thread local or "
10302 "automatic storage is not a constant expression", t);
10303 return false;
10305 #endif
10306 return potential_constant_expression_1 (t, any, flags);
10308 case COMPONENT_REF:
10309 case BIT_FIELD_REF:
10310 case ARROW_EXPR:
10311 case OFFSET_REF:
10312 /* -- a class member access unless its postfix-expression is
10313 of literal type or of pointer to literal type. */
10314 /* This test would be redundant, as it follows from the
10315 postfix-expression being a potential constant expression. */
10316 return potential_constant_expression_1 (TREE_OPERAND (t, 0),
10317 want_rval, flags);
10319 case EXPR_PACK_EXPANSION:
10320 return potential_constant_expression_1 (PACK_EXPANSION_PATTERN (t),
10321 want_rval, flags);
10323 case INDIRECT_REF:
10325 tree x = TREE_OPERAND (t, 0);
10326 STRIP_NOPS (x);
10327 if (is_this_parameter (x))
10329 if (DECL_CONTEXT (x)
10330 && !DECL_DECLARED_CONSTEXPR_P (DECL_CONTEXT (x)))
10332 if (flags & tf_error)
10333 error ("use of %<this%> in a constant expression");
10334 return false;
10336 if (want_rval && DECL_CONTEXT (x)
10337 && DECL_CONSTRUCTOR_P (DECL_CONTEXT (x)))
10339 if (flags & tf_error)
10340 sorry ("use of the value of the object being constructed "
10341 "in a constant expression");
10342 return false;
10344 return true;
10346 return potential_constant_expression_1 (x, rval, flags);
10349 case LAMBDA_EXPR:
10350 case DYNAMIC_CAST_EXPR:
10351 case PSEUDO_DTOR_EXPR:
10352 case PREINCREMENT_EXPR:
10353 case POSTINCREMENT_EXPR:
10354 case PREDECREMENT_EXPR:
10355 case POSTDECREMENT_EXPR:
10356 case NEW_EXPR:
10357 case VEC_NEW_EXPR:
10358 case DELETE_EXPR:
10359 case VEC_DELETE_EXPR:
10360 case THROW_EXPR:
10361 case MODIFY_EXPR:
10362 case MODOP_EXPR:
10363 case OMP_ATOMIC:
10364 case OMP_ATOMIC_READ:
10365 case OMP_ATOMIC_CAPTURE_OLD:
10366 case OMP_ATOMIC_CAPTURE_NEW:
10367 /* GCC internal stuff. */
10368 case VA_ARG_EXPR:
10369 case OBJ_TYPE_REF:
10370 case WITH_CLEANUP_EXPR:
10371 case CLEANUP_POINT_EXPR:
10372 case MUST_NOT_THROW_EXPR:
10373 case TRY_CATCH_EXPR:
10374 case STATEMENT_LIST:
10375 /* Don't bother trying to define a subset of statement-expressions to
10376 be constant-expressions, at least for now. */
10377 case STMT_EXPR:
10378 case EXPR_STMT:
10379 case BIND_EXPR:
10380 case TRANSACTION_EXPR:
10381 case IF_STMT:
10382 case DO_STMT:
10383 case FOR_STMT:
10384 case WHILE_STMT:
10385 case DECL_EXPR:
10386 if (flags & tf_error)
10387 error ("expression %qE is not a constant-expression", t);
10388 return false;
10390 case TYPEID_EXPR:
10391 /* -- a typeid expression whose operand is of polymorphic
10392 class type; */
10394 tree e = TREE_OPERAND (t, 0);
10395 if (!TYPE_P (e) && !type_dependent_expression_p (e)
10396 && TYPE_POLYMORPHIC_P (TREE_TYPE (e)))
10398 if (flags & tf_error)
10399 error ("typeid-expression is not a constant expression "
10400 "because %qE is of polymorphic type", e);
10401 return false;
10403 return true;
10406 case MINUS_EXPR:
10407 /* -- a subtraction where both operands are pointers. */
10408 if (TYPE_PTR_P (TREE_OPERAND (t, 0))
10409 && TYPE_PTR_P (TREE_OPERAND (t, 1)))
10411 if (flags & tf_error)
10412 error ("difference of two pointer expressions is not "
10413 "a constant expression");
10414 return false;
10416 want_rval = true;
10417 goto binary;
10419 case LT_EXPR:
10420 case LE_EXPR:
10421 case GT_EXPR:
10422 case GE_EXPR:
10423 case EQ_EXPR:
10424 case NE_EXPR:
10425 /* -- a relational or equality operator where at least
10426 one of the operands is a pointer. */
10427 if (TYPE_PTR_P (TREE_OPERAND (t, 0))
10428 || TYPE_PTR_P (TREE_OPERAND (t, 1)))
10430 if (flags & tf_error)
10431 error ("pointer comparison expression is not a "
10432 "constant expression");
10433 return false;
10435 want_rval = true;
10436 goto binary;
10438 case BIT_NOT_EXPR:
10439 /* A destructor. */
10440 if (TYPE_P (TREE_OPERAND (t, 0)))
10441 return true;
10442 /* else fall through. */
10444 case REALPART_EXPR:
10445 case IMAGPART_EXPR:
10446 case CONJ_EXPR:
10447 case SAVE_EXPR:
10448 case FIX_TRUNC_EXPR:
10449 case FLOAT_EXPR:
10450 case NEGATE_EXPR:
10451 case ABS_EXPR:
10452 case TRUTH_NOT_EXPR:
10453 case FIXED_CONVERT_EXPR:
10454 case UNARY_PLUS_EXPR:
10455 return potential_constant_expression_1 (TREE_OPERAND (t, 0), rval,
10456 flags);
10458 case CAST_EXPR:
10459 case CONST_CAST_EXPR:
10460 case STATIC_CAST_EXPR:
10461 case REINTERPRET_CAST_EXPR:
10462 case IMPLICIT_CONV_EXPR:
10463 if (cxx_dialect < cxx11
10464 && !dependent_type_p (TREE_TYPE (t))
10465 && !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t)))
10466 /* In C++98, a conversion to non-integral type can't be part of a
10467 constant expression. */
10469 if (flags & tf_error)
10470 error ("cast to non-integral type %qT in a constant expression",
10471 TREE_TYPE (t));
10472 return false;
10475 return (potential_constant_expression_1
10476 (TREE_OPERAND (t, 0),
10477 TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE, flags));
10479 case PAREN_EXPR:
10480 case NON_DEPENDENT_EXPR:
10481 /* For convenience. */
10482 case RETURN_EXPR:
10483 return potential_constant_expression_1 (TREE_OPERAND (t, 0),
10484 want_rval, flags);
10486 case SCOPE_REF:
10487 return potential_constant_expression_1 (TREE_OPERAND (t, 1),
10488 want_rval, flags);
10490 case TARGET_EXPR:
10491 if (!literal_type_p (TREE_TYPE (t)))
10493 if (flags & tf_error)
10495 error ("temporary of non-literal type %qT in a "
10496 "constant expression", TREE_TYPE (t));
10497 explain_non_literal_class (TREE_TYPE (t));
10499 return false;
10501 case INIT_EXPR:
10502 return potential_constant_expression_1 (TREE_OPERAND (t, 1),
10503 rval, flags);
10505 case CONSTRUCTOR:
10507 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t);
10508 constructor_elt *ce;
10509 for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
10510 if (!potential_constant_expression_1 (ce->value, want_rval, flags))
10511 return false;
10512 return true;
10515 case TREE_LIST:
10517 gcc_assert (TREE_PURPOSE (t) == NULL_TREE
10518 || DECL_P (TREE_PURPOSE (t)));
10519 if (!potential_constant_expression_1 (TREE_VALUE (t), want_rval,
10520 flags))
10521 return false;
10522 if (TREE_CHAIN (t) == NULL_TREE)
10523 return true;
10524 return potential_constant_expression_1 (TREE_CHAIN (t), want_rval,
10525 flags);
10528 case TRUNC_DIV_EXPR:
10529 case CEIL_DIV_EXPR:
10530 case FLOOR_DIV_EXPR:
10531 case ROUND_DIV_EXPR:
10532 case TRUNC_MOD_EXPR:
10533 case CEIL_MOD_EXPR:
10534 case ROUND_MOD_EXPR:
10536 tree denom = TREE_OPERAND (t, 1);
10537 if (!potential_constant_expression_1 (denom, rval, flags))
10538 return false;
10539 /* We can't call cxx_eval_outermost_constant_expr on an expression
10540 that hasn't been through fold_non_dependent_expr yet. */
10541 if (!processing_template_decl)
10542 denom = cxx_eval_outermost_constant_expr (denom, true);
10543 if (integer_zerop (denom))
10545 if (flags & tf_error)
10546 error ("division by zero is not a constant-expression");
10547 return false;
10549 else
10551 want_rval = true;
10552 return potential_constant_expression_1 (TREE_OPERAND (t, 0),
10553 want_rval, flags);
10557 case COMPOUND_EXPR:
10559 /* check_return_expr sometimes wraps a TARGET_EXPR in a
10560 COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR
10561 introduced by build_call_a. */
10562 tree op0 = TREE_OPERAND (t, 0);
10563 tree op1 = TREE_OPERAND (t, 1);
10564 STRIP_NOPS (op1);
10565 if ((TREE_CODE (op0) == TARGET_EXPR && op1 == TARGET_EXPR_SLOT (op0))
10566 || TREE_CODE (op1) == EMPTY_CLASS_EXPR)
10567 return potential_constant_expression_1 (op0, want_rval, flags);
10568 else
10569 goto binary;
10572 /* If the first operand is the non-short-circuit constant, look at
10573 the second operand; otherwise we only care about the first one for
10574 potentiality. */
10575 case TRUTH_AND_EXPR:
10576 case TRUTH_ANDIF_EXPR:
10577 tmp = boolean_true_node;
10578 goto truth;
10579 case TRUTH_OR_EXPR:
10580 case TRUTH_ORIF_EXPR:
10581 tmp = boolean_false_node;
10582 truth:
10584 tree op = TREE_OPERAND (t, 0);
10585 if (!potential_constant_expression_1 (op, rval, flags))
10586 return false;
10587 if (!processing_template_decl)
10588 op = cxx_eval_outermost_constant_expr (op, true);
10589 if (tree_int_cst_equal (op, tmp))
10590 return potential_constant_expression_1 (TREE_OPERAND (t, 1), rval, flags);
10591 else
10592 return true;
10595 case PLUS_EXPR:
10596 case MULT_EXPR:
10597 case POINTER_PLUS_EXPR:
10598 case RDIV_EXPR:
10599 case EXACT_DIV_EXPR:
10600 case MIN_EXPR:
10601 case MAX_EXPR:
10602 case LSHIFT_EXPR:
10603 case RSHIFT_EXPR:
10604 case LROTATE_EXPR:
10605 case RROTATE_EXPR:
10606 case BIT_IOR_EXPR:
10607 case BIT_XOR_EXPR:
10608 case BIT_AND_EXPR:
10609 case TRUTH_XOR_EXPR:
10610 case UNORDERED_EXPR:
10611 case ORDERED_EXPR:
10612 case UNLT_EXPR:
10613 case UNLE_EXPR:
10614 case UNGT_EXPR:
10615 case UNGE_EXPR:
10616 case UNEQ_EXPR:
10617 case LTGT_EXPR:
10618 case RANGE_EXPR:
10619 case COMPLEX_EXPR:
10620 want_rval = true;
10621 /* Fall through. */
10622 case ARRAY_REF:
10623 case ARRAY_RANGE_REF:
10624 case MEMBER_REF:
10625 case DOTSTAR_EXPR:
10626 binary:
10627 for (i = 0; i < 2; ++i)
10628 if (!potential_constant_expression_1 (TREE_OPERAND (t, i),
10629 want_rval, flags))
10630 return false;
10631 return true;
10633 case CILK_SYNC_STMT:
10634 case CILK_SPAWN_STMT:
10635 case ARRAY_NOTATION_REF:
10636 return false;
10638 case FMA_EXPR:
10639 case VEC_PERM_EXPR:
10640 for (i = 0; i < 3; ++i)
10641 if (!potential_constant_expression_1 (TREE_OPERAND (t, i),
10642 true, flags))
10643 return false;
10644 return true;
10646 case COND_EXPR:
10647 case VEC_COND_EXPR:
10648 /* If the condition is a known constant, we know which of the legs we
10649 care about; otherwise we only require that the condition and
10650 either of the legs be potentially constant. */
10651 tmp = TREE_OPERAND (t, 0);
10652 if (!potential_constant_expression_1 (tmp, rval, flags))
10653 return false;
10654 if (!processing_template_decl)
10655 tmp = cxx_eval_outermost_constant_expr (tmp, true);
10656 if (integer_zerop (tmp))
10657 return potential_constant_expression_1 (TREE_OPERAND (t, 2),
10658 want_rval, flags);
10659 else if (TREE_CODE (tmp) == INTEGER_CST)
10660 return potential_constant_expression_1 (TREE_OPERAND (t, 1),
10661 want_rval, flags);
10662 for (i = 1; i < 3; ++i)
10663 if (potential_constant_expression_1 (TREE_OPERAND (t, i),
10664 want_rval, tf_none))
10665 return true;
10666 if (flags & tf_error)
10667 error ("expression %qE is not a constant-expression", t);
10668 return false;
10670 case VEC_INIT_EXPR:
10671 if (VEC_INIT_EXPR_IS_CONSTEXPR (t))
10672 return true;
10673 if (flags & tf_error)
10675 error ("non-constant array initialization");
10676 diagnose_non_constexpr_vec_init (t);
10678 return false;
10680 default:
10681 if (objc_is_property_ref (t))
10682 return false;
10684 sorry ("unexpected AST of kind %s", get_tree_code_name (TREE_CODE (t)));
10685 gcc_unreachable();
10686 return false;
10690 /* The main entry point to the above. */
10692 bool
10693 potential_constant_expression (tree t)
10695 return potential_constant_expression_1 (t, false, tf_none);
10698 /* As above, but require a constant rvalue. */
10700 bool
10701 potential_rvalue_constant_expression (tree t)
10703 return potential_constant_expression_1 (t, true, tf_none);
10706 /* Like above, but complain about non-constant expressions. */
10708 bool
10709 require_potential_constant_expression (tree t)
10711 return potential_constant_expression_1 (t, false, tf_warning_or_error);
10714 /* Cross product of the above. */
10716 bool
10717 require_potential_rvalue_constant_expression (tree t)
10719 return potential_constant_expression_1 (t, true, tf_warning_or_error);
10722 /* Insert the deduced return type for an auto function. */
10724 void
10725 apply_deduced_return_type (tree fco, tree return_type)
10727 tree result;
10729 if (return_type == error_mark_node)
10730 return;
10732 if (LAMBDA_FUNCTION_P (fco))
10734 tree lambda = CLASSTYPE_LAMBDA_EXPR (current_class_type);
10735 LAMBDA_EXPR_RETURN_TYPE (lambda) = return_type;
10738 if (DECL_CONV_FN_P (fco))
10739 DECL_NAME (fco) = mangle_conv_op_name_for_type (return_type);
10741 TREE_TYPE (fco) = change_return_type (return_type, TREE_TYPE (fco));
10743 result = DECL_RESULT (fco);
10744 if (result == NULL_TREE)
10745 return;
10746 if (TREE_TYPE (result) == return_type)
10747 return;
10749 /* We already have a DECL_RESULT from start_preparsed_function.
10750 Now we need to redo the work it and allocate_struct_function
10751 did to reflect the new type. */
10752 gcc_assert (current_function_decl == fco);
10753 result = build_decl (input_location, RESULT_DECL, NULL_TREE,
10754 TYPE_MAIN_VARIANT (return_type));
10755 DECL_ARTIFICIAL (result) = 1;
10756 DECL_IGNORED_P (result) = 1;
10757 cp_apply_type_quals_to_decl (cp_type_quals (return_type),
10758 result);
10760 DECL_RESULT (fco) = result;
10762 if (!processing_template_decl)
10764 if (!VOID_TYPE_P (TREE_TYPE (result)))
10765 complete_type_or_else (TREE_TYPE (result), NULL_TREE);
10766 bool aggr = aggregate_value_p (result, fco);
10767 #ifdef PCC_STATIC_STRUCT_RETURN
10768 cfun->returns_pcc_struct = aggr;
10769 #endif
10770 cfun->returns_struct = aggr;
10775 /* DECL is a local variable or parameter from the surrounding scope of a
10776 lambda-expression. Returns the decltype for a use of the capture field
10777 for DECL even if it hasn't been captured yet. */
10779 static tree
10780 capture_decltype (tree decl)
10782 tree lam = CLASSTYPE_LAMBDA_EXPR (DECL_CONTEXT (current_function_decl));
10783 /* FIXME do lookup instead of list walk? */
10784 tree cap = value_member (decl, LAMBDA_EXPR_CAPTURE_LIST (lam));
10785 tree type;
10787 if (cap)
10788 type = TREE_TYPE (TREE_PURPOSE (cap));
10789 else
10790 switch (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lam))
10792 case CPLD_NONE:
10793 error ("%qD is not captured", decl);
10794 return error_mark_node;
10796 case CPLD_COPY:
10797 type = TREE_TYPE (decl);
10798 if (TREE_CODE (type) == REFERENCE_TYPE
10799 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
10800 type = TREE_TYPE (type);
10801 break;
10803 case CPLD_REFERENCE:
10804 type = TREE_TYPE (decl);
10805 if (TREE_CODE (type) != REFERENCE_TYPE)
10806 type = build_reference_type (TREE_TYPE (decl));
10807 break;
10809 default:
10810 gcc_unreachable ();
10813 if (TREE_CODE (type) != REFERENCE_TYPE)
10815 if (!LAMBDA_EXPR_MUTABLE_P (lam))
10816 type = cp_build_qualified_type (type, (cp_type_quals (type)
10817 |TYPE_QUAL_CONST));
10818 type = build_reference_type (type);
10820 return type;
10823 #include "gt-cp-semantics.h"