tree-ssa-threadupdate.c: Include tree-cfg.h and tree-pass.h
[official-gcc.git] / gcc / cp / semantics.c
blobcd5a91d2c60c3ed8f90d1d820a496e2820205e9f
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-2013 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 "vec.h"
47 #include "target.h"
48 #include "gimple.h"
49 #include "gimplify.h"
50 #include "bitmap.h"
51 #include "hash-table.h"
52 #include "omp-low.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 /* Add T to the statement-tree. Non-side-effect statements need to be
391 recorded during statement expressions. */
392 gcc_checking_assert (!stmt_list_stack->is_empty ());
393 append_to_statement_list_force (t, &cur_stmt_list);
395 return t;
398 /* Returns the stmt_tree to which statements are currently being added. */
400 stmt_tree
401 current_stmt_tree (void)
403 return (cfun
404 ? &cfun->language->base.x_stmt_tree
405 : &scope_chain->x_stmt_tree);
408 /* If statements are full expressions, wrap STMT in a CLEANUP_POINT_EXPR. */
410 static tree
411 maybe_cleanup_point_expr (tree expr)
413 if (!processing_template_decl && stmts_are_full_exprs_p ())
414 expr = fold_build_cleanup_point_expr (TREE_TYPE (expr), expr);
415 return expr;
418 /* Like maybe_cleanup_point_expr except have the type of the new expression be
419 void so we don't need to create a temporary variable to hold the inner
420 expression. The reason why we do this is because the original type might be
421 an aggregate and we cannot create a temporary variable for that type. */
423 tree
424 maybe_cleanup_point_expr_void (tree expr)
426 if (!processing_template_decl && stmts_are_full_exprs_p ())
427 expr = fold_build_cleanup_point_expr (void_type_node, expr);
428 return expr;
433 /* Create a declaration statement for the declaration given by the DECL. */
435 void
436 add_decl_expr (tree decl)
438 tree r = build_stmt (input_location, DECL_EXPR, decl);
439 if (DECL_INITIAL (decl)
440 || (DECL_SIZE (decl) && TREE_SIDE_EFFECTS (DECL_SIZE (decl))))
441 r = maybe_cleanup_point_expr_void (r);
442 add_stmt (r);
445 /* Finish a scope. */
447 tree
448 do_poplevel (tree stmt_list)
450 tree block = NULL;
452 if (stmts_are_full_exprs_p ())
453 block = poplevel (kept_level_p (), 1, 0);
455 stmt_list = pop_stmt_list (stmt_list);
457 if (!processing_template_decl)
459 stmt_list = c_build_bind_expr (input_location, block, stmt_list);
460 /* ??? See c_end_compound_stmt re statement expressions. */
463 return stmt_list;
466 /* Begin a new scope. */
468 static tree
469 do_pushlevel (scope_kind sk)
471 tree ret = push_stmt_list ();
472 if (stmts_are_full_exprs_p ())
473 begin_scope (sk, NULL);
474 return ret;
477 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
478 when the current scope is exited. EH_ONLY is true when this is not
479 meant to apply to normal control flow transfer. */
481 void
482 push_cleanup (tree decl, tree cleanup, bool eh_only)
484 tree stmt = build_stmt (input_location, CLEANUP_STMT, NULL, cleanup, decl);
485 CLEANUP_EH_ONLY (stmt) = eh_only;
486 add_stmt (stmt);
487 CLEANUP_BODY (stmt) = push_stmt_list ();
490 /* Begin a conditional that might contain a declaration. When generating
491 normal code, we want the declaration to appear before the statement
492 containing the conditional. When generating template code, we want the
493 conditional to be rendered as the raw DECL_EXPR. */
495 static void
496 begin_cond (tree *cond_p)
498 if (processing_template_decl)
499 *cond_p = push_stmt_list ();
502 /* Finish such a conditional. */
504 static void
505 finish_cond (tree *cond_p, tree expr)
507 if (processing_template_decl)
509 tree cond = pop_stmt_list (*cond_p);
511 if (expr == NULL_TREE)
512 /* Empty condition in 'for'. */
513 gcc_assert (empty_expr_stmt_p (cond));
514 else if (check_for_bare_parameter_packs (expr))
515 expr = error_mark_node;
516 else if (!empty_expr_stmt_p (cond))
517 expr = build2 (COMPOUND_EXPR, TREE_TYPE (expr), cond, expr);
519 *cond_p = expr;
522 /* If *COND_P specifies a conditional with a declaration, transform the
523 loop such that
524 while (A x = 42) { }
525 for (; A x = 42;) { }
526 becomes
527 while (true) { A x = 42; if (!x) break; }
528 for (;;) { A x = 42; if (!x) break; }
529 The statement list for BODY will be empty if the conditional did
530 not declare anything. */
532 static void
533 simplify_loop_decl_cond (tree *cond_p, tree body)
535 tree cond, if_stmt;
537 if (!TREE_SIDE_EFFECTS (body))
538 return;
540 cond = *cond_p;
541 *cond_p = boolean_true_node;
543 if_stmt = begin_if_stmt ();
544 cond = cp_build_unary_op (TRUTH_NOT_EXPR, cond, 0, tf_warning_or_error);
545 finish_if_stmt_cond (cond, if_stmt);
546 finish_break_stmt ();
547 finish_then_clause (if_stmt);
548 finish_if_stmt (if_stmt);
551 /* Finish a goto-statement. */
553 tree
554 finish_goto_stmt (tree destination)
556 if (identifier_p (destination))
557 destination = lookup_label (destination);
559 /* We warn about unused labels with -Wunused. That means we have to
560 mark the used labels as used. */
561 if (TREE_CODE (destination) == LABEL_DECL)
562 TREE_USED (destination) = 1;
563 else
565 destination = mark_rvalue_use (destination);
566 if (!processing_template_decl)
568 destination = cp_convert (ptr_type_node, destination,
569 tf_warning_or_error);
570 if (error_operand_p (destination))
571 return NULL_TREE;
572 destination
573 = fold_build_cleanup_point_expr (TREE_TYPE (destination),
574 destination);
578 check_goto (destination);
580 return add_stmt (build_stmt (input_location, GOTO_EXPR, destination));
583 /* COND is the condition-expression for an if, while, etc.,
584 statement. Convert it to a boolean value, if appropriate.
585 In addition, verify sequence points if -Wsequence-point is enabled. */
587 static tree
588 maybe_convert_cond (tree cond)
590 /* Empty conditions remain empty. */
591 if (!cond)
592 return NULL_TREE;
594 /* Wait until we instantiate templates before doing conversion. */
595 if (processing_template_decl)
596 return cond;
598 if (warn_sequence_point)
599 verify_sequence_points (cond);
601 /* Do the conversion. */
602 cond = convert_from_reference (cond);
604 if (TREE_CODE (cond) == MODIFY_EXPR
605 && !TREE_NO_WARNING (cond)
606 && warn_parentheses)
608 warning (OPT_Wparentheses,
609 "suggest parentheses around assignment used as truth value");
610 TREE_NO_WARNING (cond) = 1;
613 return condition_conversion (cond);
616 /* Finish an expression-statement, whose EXPRESSION is as indicated. */
618 tree
619 finish_expr_stmt (tree expr)
621 tree r = NULL_TREE;
623 if (expr != NULL_TREE)
625 if (!processing_template_decl)
627 if (warn_sequence_point)
628 verify_sequence_points (expr);
629 expr = convert_to_void (expr, ICV_STATEMENT, tf_warning_or_error);
631 else if (!type_dependent_expression_p (expr))
632 convert_to_void (build_non_dependent_expr (expr), ICV_STATEMENT,
633 tf_warning_or_error);
635 if (check_for_bare_parameter_packs (expr))
636 expr = error_mark_node;
638 /* Simplification of inner statement expressions, compound exprs,
639 etc can result in us already having an EXPR_STMT. */
640 if (TREE_CODE (expr) != CLEANUP_POINT_EXPR)
642 if (TREE_CODE (expr) != EXPR_STMT)
643 expr = build_stmt (input_location, EXPR_STMT, expr);
644 expr = maybe_cleanup_point_expr_void (expr);
647 r = add_stmt (expr);
650 return r;
654 /* Begin an if-statement. Returns a newly created IF_STMT if
655 appropriate. */
657 tree
658 begin_if_stmt (void)
660 tree r, scope;
661 scope = do_pushlevel (sk_cond);
662 r = build_stmt (input_location, IF_STMT, NULL_TREE,
663 NULL_TREE, NULL_TREE, scope);
664 begin_cond (&IF_COND (r));
665 return r;
668 /* Process the COND of an if-statement, which may be given by
669 IF_STMT. */
671 void
672 finish_if_stmt_cond (tree cond, tree if_stmt)
674 finish_cond (&IF_COND (if_stmt), maybe_convert_cond (cond));
675 add_stmt (if_stmt);
676 THEN_CLAUSE (if_stmt) = push_stmt_list ();
679 /* Finish the then-clause of an if-statement, which may be given by
680 IF_STMT. */
682 tree
683 finish_then_clause (tree if_stmt)
685 THEN_CLAUSE (if_stmt) = pop_stmt_list (THEN_CLAUSE (if_stmt));
686 return if_stmt;
689 /* Begin the else-clause of an if-statement. */
691 void
692 begin_else_clause (tree if_stmt)
694 ELSE_CLAUSE (if_stmt) = push_stmt_list ();
697 /* Finish the else-clause of an if-statement, which may be given by
698 IF_STMT. */
700 void
701 finish_else_clause (tree if_stmt)
703 ELSE_CLAUSE (if_stmt) = pop_stmt_list (ELSE_CLAUSE (if_stmt));
706 /* Finish an if-statement. */
708 void
709 finish_if_stmt (tree if_stmt)
711 tree scope = IF_SCOPE (if_stmt);
712 IF_SCOPE (if_stmt) = NULL;
713 add_stmt (do_poplevel (scope));
716 /* Begin a while-statement. Returns a newly created WHILE_STMT if
717 appropriate. */
719 tree
720 begin_while_stmt (void)
722 tree r;
723 r = build_stmt (input_location, WHILE_STMT, NULL_TREE, NULL_TREE);
724 add_stmt (r);
725 WHILE_BODY (r) = do_pushlevel (sk_block);
726 begin_cond (&WHILE_COND (r));
727 return r;
730 /* Process the COND of a while-statement, which may be given by
731 WHILE_STMT. */
733 void
734 finish_while_stmt_cond (tree cond, tree while_stmt, bool ivdep)
736 finish_cond (&WHILE_COND (while_stmt), maybe_convert_cond (cond));
737 if (ivdep && cond != error_mark_node)
738 WHILE_COND (while_stmt) = build2 (ANNOTATE_EXPR,
739 TREE_TYPE (WHILE_COND (while_stmt)),
740 WHILE_COND (while_stmt),
741 build_int_cst (integer_type_node,
742 annot_expr_ivdep_kind));
743 simplify_loop_decl_cond (&WHILE_COND (while_stmt), WHILE_BODY (while_stmt));
746 /* Finish a while-statement, which may be given by WHILE_STMT. */
748 void
749 finish_while_stmt (tree while_stmt)
751 WHILE_BODY (while_stmt) = do_poplevel (WHILE_BODY (while_stmt));
754 /* Begin a do-statement. Returns a newly created DO_STMT if
755 appropriate. */
757 tree
758 begin_do_stmt (void)
760 tree r = build_stmt (input_location, DO_STMT, NULL_TREE, NULL_TREE);
761 add_stmt (r);
762 DO_BODY (r) = push_stmt_list ();
763 return r;
766 /* Finish the body of a do-statement, which may be given by DO_STMT. */
768 void
769 finish_do_body (tree do_stmt)
771 tree body = DO_BODY (do_stmt) = pop_stmt_list (DO_BODY (do_stmt));
773 if (TREE_CODE (body) == STATEMENT_LIST && STATEMENT_LIST_TAIL (body))
774 body = STATEMENT_LIST_TAIL (body)->stmt;
776 if (IS_EMPTY_STMT (body))
777 warning (OPT_Wempty_body,
778 "suggest explicit braces around empty body in %<do%> statement");
781 /* Finish a do-statement, which may be given by DO_STMT, and whose
782 COND is as indicated. */
784 void
785 finish_do_stmt (tree cond, tree do_stmt, bool ivdep)
787 cond = maybe_convert_cond (cond);
788 if (ivdep && cond != error_mark_node)
789 cond = build2 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
790 build_int_cst (integer_type_node, annot_expr_ivdep_kind));
791 DO_COND (do_stmt) = cond;
794 /* Finish a return-statement. The EXPRESSION returned, if any, is as
795 indicated. */
797 tree
798 finish_return_stmt (tree expr)
800 tree r;
801 bool no_warning;
803 expr = check_return_expr (expr, &no_warning);
805 if (error_operand_p (expr)
806 || (flag_openmp && !check_omp_return ()))
807 return error_mark_node;
808 if (!processing_template_decl)
810 if (warn_sequence_point)
811 verify_sequence_points (expr);
813 if (DECL_DESTRUCTOR_P (current_function_decl)
814 || (DECL_CONSTRUCTOR_P (current_function_decl)
815 && targetm.cxx.cdtor_returns_this ()))
817 /* Similarly, all destructors must run destructors for
818 base-classes before returning. So, all returns in a
819 destructor get sent to the DTOR_LABEL; finish_function emits
820 code to return a value there. */
821 return finish_goto_stmt (cdtor_label);
825 r = build_stmt (input_location, RETURN_EXPR, expr);
826 TREE_NO_WARNING (r) |= no_warning;
827 r = maybe_cleanup_point_expr_void (r);
828 r = add_stmt (r);
830 return r;
833 /* Begin the scope of a for-statement or a range-for-statement.
834 Both the returned trees are to be used in a call to
835 begin_for_stmt or begin_range_for_stmt. */
837 tree
838 begin_for_scope (tree *init)
840 tree scope = NULL_TREE;
841 if (flag_new_for_scope > 0)
842 scope = do_pushlevel (sk_for);
844 if (processing_template_decl)
845 *init = push_stmt_list ();
846 else
847 *init = NULL_TREE;
849 return scope;
852 /* Begin a for-statement. Returns a new FOR_STMT.
853 SCOPE and INIT should be the return of begin_for_scope,
854 or both NULL_TREE */
856 tree
857 begin_for_stmt (tree scope, tree init)
859 tree r;
861 r = build_stmt (input_location, FOR_STMT, NULL_TREE, NULL_TREE,
862 NULL_TREE, NULL_TREE, NULL_TREE);
864 if (scope == NULL_TREE)
866 gcc_assert (!init || !(flag_new_for_scope > 0));
867 if (!init)
868 scope = begin_for_scope (&init);
870 FOR_INIT_STMT (r) = init;
871 FOR_SCOPE (r) = scope;
873 return r;
876 /* Finish the for-init-statement of a for-statement, which may be
877 given by FOR_STMT. */
879 void
880 finish_for_init_stmt (tree for_stmt)
882 if (processing_template_decl)
883 FOR_INIT_STMT (for_stmt) = pop_stmt_list (FOR_INIT_STMT (for_stmt));
884 add_stmt (for_stmt);
885 FOR_BODY (for_stmt) = do_pushlevel (sk_block);
886 begin_cond (&FOR_COND (for_stmt));
889 /* Finish the COND of a for-statement, which may be given by
890 FOR_STMT. */
892 void
893 finish_for_cond (tree cond, tree for_stmt, bool ivdep)
895 finish_cond (&FOR_COND (for_stmt), maybe_convert_cond (cond));
896 if (ivdep && cond != error_mark_node)
897 FOR_COND (for_stmt) = build2 (ANNOTATE_EXPR,
898 TREE_TYPE (FOR_COND (for_stmt)),
899 FOR_COND (for_stmt),
900 build_int_cst (integer_type_node,
901 annot_expr_ivdep_kind));
902 simplify_loop_decl_cond (&FOR_COND (for_stmt), FOR_BODY (for_stmt));
905 /* Finish the increment-EXPRESSION in a for-statement, which may be
906 given by FOR_STMT. */
908 void
909 finish_for_expr (tree expr, tree for_stmt)
911 if (!expr)
912 return;
913 /* If EXPR is an overloaded function, issue an error; there is no
914 context available to use to perform overload resolution. */
915 if (type_unknown_p (expr))
917 cxx_incomplete_type_error (expr, TREE_TYPE (expr));
918 expr = error_mark_node;
920 if (!processing_template_decl)
922 if (warn_sequence_point)
923 verify_sequence_points (expr);
924 expr = convert_to_void (expr, ICV_THIRD_IN_FOR,
925 tf_warning_or_error);
927 else if (!type_dependent_expression_p (expr))
928 convert_to_void (build_non_dependent_expr (expr), ICV_THIRD_IN_FOR,
929 tf_warning_or_error);
930 expr = maybe_cleanup_point_expr_void (expr);
931 if (check_for_bare_parameter_packs (expr))
932 expr = error_mark_node;
933 FOR_EXPR (for_stmt) = expr;
936 /* Finish the body of a for-statement, which may be given by
937 FOR_STMT. The increment-EXPR for the loop must be
938 provided.
939 It can also finish RANGE_FOR_STMT. */
941 void
942 finish_for_stmt (tree for_stmt)
944 if (TREE_CODE (for_stmt) == RANGE_FOR_STMT)
945 RANGE_FOR_BODY (for_stmt) = do_poplevel (RANGE_FOR_BODY (for_stmt));
946 else
947 FOR_BODY (for_stmt) = do_poplevel (FOR_BODY (for_stmt));
949 /* Pop the scope for the body of the loop. */
950 if (flag_new_for_scope > 0)
952 tree scope;
953 tree *scope_ptr = (TREE_CODE (for_stmt) == RANGE_FOR_STMT
954 ? &RANGE_FOR_SCOPE (for_stmt)
955 : &FOR_SCOPE (for_stmt));
956 scope = *scope_ptr;
957 *scope_ptr = NULL;
958 add_stmt (do_poplevel (scope));
962 /* Begin a range-for-statement. Returns a new RANGE_FOR_STMT.
963 SCOPE and INIT should be the return of begin_for_scope,
964 or both NULL_TREE .
965 To finish it call finish_for_stmt(). */
967 tree
968 begin_range_for_stmt (tree scope, tree init)
970 tree r;
972 r = build_stmt (input_location, RANGE_FOR_STMT,
973 NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE);
975 if (scope == NULL_TREE)
977 gcc_assert (!init || !(flag_new_for_scope > 0));
978 if (!init)
979 scope = begin_for_scope (&init);
982 /* RANGE_FOR_STMTs do not use nor save the init tree, so we
983 pop it now. */
984 if (init)
985 pop_stmt_list (init);
986 RANGE_FOR_SCOPE (r) = scope;
988 return r;
991 /* Finish the head of a range-based for statement, which may
992 be given by RANGE_FOR_STMT. DECL must be the declaration
993 and EXPR must be the loop expression. */
995 void
996 finish_range_for_decl (tree range_for_stmt, tree decl, tree expr)
998 RANGE_FOR_DECL (range_for_stmt) = decl;
999 RANGE_FOR_EXPR (range_for_stmt) = expr;
1000 add_stmt (range_for_stmt);
1001 RANGE_FOR_BODY (range_for_stmt) = do_pushlevel (sk_block);
1004 /* Finish a break-statement. */
1006 tree
1007 finish_break_stmt (void)
1009 /* In switch statements break is sometimes stylistically used after
1010 a return statement. This can lead to spurious warnings about
1011 control reaching the end of a non-void function when it is
1012 inlined. Note that we are calling block_may_fallthru with
1013 language specific tree nodes; this works because
1014 block_may_fallthru returns true when given something it does not
1015 understand. */
1016 if (!block_may_fallthru (cur_stmt_list))
1017 return void_zero_node;
1018 return add_stmt (build_stmt (input_location, BREAK_STMT));
1021 /* Finish a continue-statement. */
1023 tree
1024 finish_continue_stmt (void)
1026 return add_stmt (build_stmt (input_location, CONTINUE_STMT));
1029 /* Begin a switch-statement. Returns a new SWITCH_STMT if
1030 appropriate. */
1032 tree
1033 begin_switch_stmt (void)
1035 tree r, scope;
1037 scope = do_pushlevel (sk_cond);
1038 r = build_stmt (input_location, SWITCH_STMT, NULL_TREE, NULL_TREE, NULL_TREE, scope);
1040 begin_cond (&SWITCH_STMT_COND (r));
1042 return r;
1045 /* Finish the cond of a switch-statement. */
1047 void
1048 finish_switch_cond (tree cond, tree switch_stmt)
1050 tree orig_type = NULL;
1051 if (!processing_template_decl)
1053 /* Convert the condition to an integer or enumeration type. */
1054 cond = build_expr_type_conversion (WANT_INT | WANT_ENUM, cond, true);
1055 if (cond == NULL_TREE)
1057 error ("switch quantity not an integer");
1058 cond = error_mark_node;
1060 orig_type = TREE_TYPE (cond);
1061 if (cond != error_mark_node)
1063 /* [stmt.switch]
1065 Integral promotions are performed. */
1066 cond = perform_integral_promotions (cond);
1067 cond = maybe_cleanup_point_expr (cond);
1070 if (check_for_bare_parameter_packs (cond))
1071 cond = error_mark_node;
1072 else if (!processing_template_decl && warn_sequence_point)
1073 verify_sequence_points (cond);
1075 finish_cond (&SWITCH_STMT_COND (switch_stmt), cond);
1076 SWITCH_STMT_TYPE (switch_stmt) = orig_type;
1077 add_stmt (switch_stmt);
1078 push_switch (switch_stmt);
1079 SWITCH_STMT_BODY (switch_stmt) = push_stmt_list ();
1082 /* Finish the body of a switch-statement, which may be given by
1083 SWITCH_STMT. The COND to switch on is indicated. */
1085 void
1086 finish_switch_stmt (tree switch_stmt)
1088 tree scope;
1090 SWITCH_STMT_BODY (switch_stmt) =
1091 pop_stmt_list (SWITCH_STMT_BODY (switch_stmt));
1092 pop_switch ();
1094 scope = SWITCH_STMT_SCOPE (switch_stmt);
1095 SWITCH_STMT_SCOPE (switch_stmt) = NULL;
1096 add_stmt (do_poplevel (scope));
1099 /* Begin a try-block. Returns a newly-created TRY_BLOCK if
1100 appropriate. */
1102 tree
1103 begin_try_block (void)
1105 tree r = build_stmt (input_location, TRY_BLOCK, NULL_TREE, NULL_TREE);
1106 add_stmt (r);
1107 TRY_STMTS (r) = push_stmt_list ();
1108 return r;
1111 /* Likewise, for a function-try-block. The block returned in
1112 *COMPOUND_STMT is an artificial outer scope, containing the
1113 function-try-block. */
1115 tree
1116 begin_function_try_block (tree *compound_stmt)
1118 tree r;
1119 /* This outer scope does not exist in the C++ standard, but we need
1120 a place to put __FUNCTION__ and similar variables. */
1121 *compound_stmt = begin_compound_stmt (0);
1122 r = begin_try_block ();
1123 FN_TRY_BLOCK_P (r) = 1;
1124 return r;
1127 /* Finish a try-block, which may be given by TRY_BLOCK. */
1129 void
1130 finish_try_block (tree try_block)
1132 TRY_STMTS (try_block) = pop_stmt_list (TRY_STMTS (try_block));
1133 TRY_HANDLERS (try_block) = push_stmt_list ();
1136 /* Finish the body of a cleanup try-block, which may be given by
1137 TRY_BLOCK. */
1139 void
1140 finish_cleanup_try_block (tree try_block)
1142 TRY_STMTS (try_block) = pop_stmt_list (TRY_STMTS (try_block));
1145 /* Finish an implicitly generated try-block, with a cleanup is given
1146 by CLEANUP. */
1148 void
1149 finish_cleanup (tree cleanup, tree try_block)
1151 TRY_HANDLERS (try_block) = cleanup;
1152 CLEANUP_P (try_block) = 1;
1155 /* Likewise, for a function-try-block. */
1157 void
1158 finish_function_try_block (tree try_block)
1160 finish_try_block (try_block);
1161 /* FIXME : something queer about CTOR_INITIALIZER somehow following
1162 the try block, but moving it inside. */
1163 in_function_try_handler = 1;
1166 /* Finish a handler-sequence for a try-block, which may be given by
1167 TRY_BLOCK. */
1169 void
1170 finish_handler_sequence (tree try_block)
1172 TRY_HANDLERS (try_block) = pop_stmt_list (TRY_HANDLERS (try_block));
1173 check_handlers (TRY_HANDLERS (try_block));
1176 /* Finish the handler-seq for a function-try-block, given by
1177 TRY_BLOCK. COMPOUND_STMT is the outer block created by
1178 begin_function_try_block. */
1180 void
1181 finish_function_handler_sequence (tree try_block, tree compound_stmt)
1183 in_function_try_handler = 0;
1184 finish_handler_sequence (try_block);
1185 finish_compound_stmt (compound_stmt);
1188 /* Begin a handler. Returns a HANDLER if appropriate. */
1190 tree
1191 begin_handler (void)
1193 tree r;
1195 r = build_stmt (input_location, HANDLER, NULL_TREE, NULL_TREE);
1196 add_stmt (r);
1198 /* Create a binding level for the eh_info and the exception object
1199 cleanup. */
1200 HANDLER_BODY (r) = do_pushlevel (sk_catch);
1202 return r;
1205 /* Finish the handler-parameters for a handler, which may be given by
1206 HANDLER. DECL is the declaration for the catch parameter, or NULL
1207 if this is a `catch (...)' clause. */
1209 void
1210 finish_handler_parms (tree decl, tree handler)
1212 tree type = NULL_TREE;
1213 if (processing_template_decl)
1215 if (decl)
1217 decl = pushdecl (decl);
1218 decl = push_template_decl (decl);
1219 HANDLER_PARMS (handler) = decl;
1220 type = TREE_TYPE (decl);
1223 else
1224 type = expand_start_catch_block (decl);
1225 HANDLER_TYPE (handler) = type;
1228 /* Finish a handler, which may be given by HANDLER. The BLOCKs are
1229 the return value from the matching call to finish_handler_parms. */
1231 void
1232 finish_handler (tree handler)
1234 if (!processing_template_decl)
1235 expand_end_catch_block ();
1236 HANDLER_BODY (handler) = do_poplevel (HANDLER_BODY (handler));
1239 /* Begin a compound statement. FLAGS contains some bits that control the
1240 behavior and context. If BCS_NO_SCOPE is set, the compound statement
1241 does not define a scope. If BCS_FN_BODY is set, this is the outermost
1242 block of a function. If BCS_TRY_BLOCK is set, this is the block
1243 created on behalf of a TRY statement. Returns a token to be passed to
1244 finish_compound_stmt. */
1246 tree
1247 begin_compound_stmt (unsigned int flags)
1249 tree r;
1251 if (flags & BCS_NO_SCOPE)
1253 r = push_stmt_list ();
1254 STATEMENT_LIST_NO_SCOPE (r) = 1;
1256 /* Normally, we try hard to keep the BLOCK for a statement-expression.
1257 But, if it's a statement-expression with a scopeless block, there's
1258 nothing to keep, and we don't want to accidentally keep a block
1259 *inside* the scopeless block. */
1260 keep_next_level (false);
1262 else
1263 r = do_pushlevel (flags & BCS_TRY_BLOCK ? sk_try : sk_block);
1265 /* When processing a template, we need to remember where the braces were,
1266 so that we can set up identical scopes when instantiating the template
1267 later. BIND_EXPR is a handy candidate for this.
1268 Note that do_poplevel won't create a BIND_EXPR itself here (and thus
1269 result in nested BIND_EXPRs), since we don't build BLOCK nodes when
1270 processing templates. */
1271 if (processing_template_decl)
1273 r = build3 (BIND_EXPR, NULL, NULL, r, NULL);
1274 BIND_EXPR_TRY_BLOCK (r) = (flags & BCS_TRY_BLOCK) != 0;
1275 BIND_EXPR_BODY_BLOCK (r) = (flags & BCS_FN_BODY) != 0;
1276 TREE_SIDE_EFFECTS (r) = 1;
1279 return r;
1282 /* Finish a compound-statement, which is given by STMT. */
1284 void
1285 finish_compound_stmt (tree stmt)
1287 if (TREE_CODE (stmt) == BIND_EXPR)
1289 tree body = do_poplevel (BIND_EXPR_BODY (stmt));
1290 /* If the STATEMENT_LIST is empty and this BIND_EXPR isn't special,
1291 discard the BIND_EXPR so it can be merged with the containing
1292 STATEMENT_LIST. */
1293 if (TREE_CODE (body) == STATEMENT_LIST
1294 && STATEMENT_LIST_HEAD (body) == NULL
1295 && !BIND_EXPR_BODY_BLOCK (stmt)
1296 && !BIND_EXPR_TRY_BLOCK (stmt))
1297 stmt = body;
1298 else
1299 BIND_EXPR_BODY (stmt) = body;
1301 else if (STATEMENT_LIST_NO_SCOPE (stmt))
1302 stmt = pop_stmt_list (stmt);
1303 else
1305 /* Destroy any ObjC "super" receivers that may have been
1306 created. */
1307 objc_clear_super_receiver ();
1309 stmt = do_poplevel (stmt);
1312 /* ??? See c_end_compound_stmt wrt statement expressions. */
1313 add_stmt (stmt);
1316 /* Finish an asm-statement, whose components are a STRING, some
1317 OUTPUT_OPERANDS, some INPUT_OPERANDS, some CLOBBERS and some
1318 LABELS. Also note whether the asm-statement should be
1319 considered volatile. */
1321 tree
1322 finish_asm_stmt (int volatile_p, tree string, tree output_operands,
1323 tree input_operands, tree clobbers, tree labels)
1325 tree r;
1326 tree t;
1327 int ninputs = list_length (input_operands);
1328 int noutputs = list_length (output_operands);
1330 if (!processing_template_decl)
1332 const char *constraint;
1333 const char **oconstraints;
1334 bool allows_mem, allows_reg, is_inout;
1335 tree operand;
1336 int i;
1338 oconstraints = XALLOCAVEC (const char *, noutputs);
1340 string = resolve_asm_operand_names (string, output_operands,
1341 input_operands, labels);
1343 for (i = 0, t = output_operands; t; t = TREE_CHAIN (t), ++i)
1345 operand = TREE_VALUE (t);
1347 /* ??? Really, this should not be here. Users should be using a
1348 proper lvalue, dammit. But there's a long history of using
1349 casts in the output operands. In cases like longlong.h, this
1350 becomes a primitive form of typechecking -- if the cast can be
1351 removed, then the output operand had a type of the proper width;
1352 otherwise we'll get an error. Gross, but ... */
1353 STRIP_NOPS (operand);
1355 operand = mark_lvalue_use (operand);
1357 if (!lvalue_or_else (operand, lv_asm, tf_warning_or_error))
1358 operand = error_mark_node;
1360 if (operand != error_mark_node
1361 && (TREE_READONLY (operand)
1362 || CP_TYPE_CONST_P (TREE_TYPE (operand))
1363 /* Functions are not modifiable, even though they are
1364 lvalues. */
1365 || TREE_CODE (TREE_TYPE (operand)) == FUNCTION_TYPE
1366 || TREE_CODE (TREE_TYPE (operand)) == METHOD_TYPE
1367 /* If it's an aggregate and any field is const, then it is
1368 effectively const. */
1369 || (CLASS_TYPE_P (TREE_TYPE (operand))
1370 && C_TYPE_FIELDS_READONLY (TREE_TYPE (operand)))))
1371 cxx_readonly_error (operand, lv_asm);
1373 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
1374 oconstraints[i] = constraint;
1376 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
1377 &allows_mem, &allows_reg, &is_inout))
1379 /* If the operand is going to end up in memory,
1380 mark it addressable. */
1381 if (!allows_reg && !cxx_mark_addressable (operand))
1382 operand = error_mark_node;
1384 else
1385 operand = error_mark_node;
1387 TREE_VALUE (t) = operand;
1390 for (i = 0, t = input_operands; t; ++i, t = TREE_CHAIN (t))
1392 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
1393 bool constraint_parsed
1394 = parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
1395 oconstraints, &allows_mem, &allows_reg);
1396 /* If the operand is going to end up in memory, don't call
1397 decay_conversion. */
1398 if (constraint_parsed && !allows_reg && allows_mem)
1399 operand = mark_lvalue_use (TREE_VALUE (t));
1400 else
1401 operand = decay_conversion (TREE_VALUE (t), tf_warning_or_error);
1403 /* If the type of the operand hasn't been determined (e.g.,
1404 because it involves an overloaded function), then issue
1405 an error message. There's no context available to
1406 resolve the overloading. */
1407 if (TREE_TYPE (operand) == unknown_type_node)
1409 error ("type of asm operand %qE could not be determined",
1410 TREE_VALUE (t));
1411 operand = error_mark_node;
1414 if (constraint_parsed)
1416 /* If the operand is going to end up in memory,
1417 mark it addressable. */
1418 if (!allows_reg && allows_mem)
1420 /* Strip the nops as we allow this case. FIXME, this really
1421 should be rejected or made deprecated. */
1422 STRIP_NOPS (operand);
1423 if (!cxx_mark_addressable (operand))
1424 operand = error_mark_node;
1426 else if (!allows_reg && !allows_mem)
1428 /* If constraint allows neither register nor memory,
1429 try harder to get a constant. */
1430 tree constop = maybe_constant_value (operand);
1431 if (TREE_CONSTANT (constop))
1432 operand = constop;
1435 else
1436 operand = error_mark_node;
1438 TREE_VALUE (t) = operand;
1442 r = build_stmt (input_location, ASM_EXPR, string,
1443 output_operands, input_operands,
1444 clobbers, labels);
1445 ASM_VOLATILE_P (r) = volatile_p || noutputs == 0;
1446 r = maybe_cleanup_point_expr_void (r);
1447 return add_stmt (r);
1450 /* Finish a label with the indicated NAME. Returns the new label. */
1452 tree
1453 finish_label_stmt (tree name)
1455 tree decl = define_label (input_location, name);
1457 if (decl == error_mark_node)
1458 return error_mark_node;
1460 add_stmt (build_stmt (input_location, LABEL_EXPR, decl));
1462 return decl;
1465 /* Finish a series of declarations for local labels. G++ allows users
1466 to declare "local" labels, i.e., labels with scope. This extension
1467 is useful when writing code involving statement-expressions. */
1469 void
1470 finish_label_decl (tree name)
1472 if (!at_function_scope_p ())
1474 error ("__label__ declarations are only allowed in function scopes");
1475 return;
1478 add_decl_expr (declare_local_label (name));
1481 /* When DECL goes out of scope, make sure that CLEANUP is executed. */
1483 void
1484 finish_decl_cleanup (tree decl, tree cleanup)
1486 push_cleanup (decl, cleanup, false);
1489 /* If the current scope exits with an exception, run CLEANUP. */
1491 void
1492 finish_eh_cleanup (tree cleanup)
1494 push_cleanup (NULL, cleanup, true);
1497 /* The MEM_INITS is a list of mem-initializers, in reverse of the
1498 order they were written by the user. Each node is as for
1499 emit_mem_initializers. */
1501 void
1502 finish_mem_initializers (tree mem_inits)
1504 /* Reorder the MEM_INITS so that they are in the order they appeared
1505 in the source program. */
1506 mem_inits = nreverse (mem_inits);
1508 if (processing_template_decl)
1510 tree mem;
1512 for (mem = mem_inits; mem; mem = TREE_CHAIN (mem))
1514 /* If the TREE_PURPOSE is a TYPE_PACK_EXPANSION, skip the
1515 check for bare parameter packs in the TREE_VALUE, because
1516 any parameter packs in the TREE_VALUE have already been
1517 bound as part of the TREE_PURPOSE. See
1518 make_pack_expansion for more information. */
1519 if (TREE_CODE (TREE_PURPOSE (mem)) != TYPE_PACK_EXPANSION
1520 && check_for_bare_parameter_packs (TREE_VALUE (mem)))
1521 TREE_VALUE (mem) = error_mark_node;
1524 add_stmt (build_min_nt_loc (UNKNOWN_LOCATION,
1525 CTOR_INITIALIZER, mem_inits));
1527 else
1528 emit_mem_initializers (mem_inits);
1531 /* Obfuscate EXPR if it looks like an id-expression or member access so
1532 that the call to finish_decltype in do_auto_deduction will give the
1533 right result. */
1535 tree
1536 force_paren_expr (tree expr)
1538 /* This is only needed for decltype(auto) in C++14. */
1539 if (cxx_dialect < cxx1y)
1540 return expr;
1542 if (!DECL_P (expr) && TREE_CODE (expr) != COMPONENT_REF
1543 && TREE_CODE (expr) != SCOPE_REF)
1544 return expr;
1546 if (processing_template_decl)
1547 expr = build1 (PAREN_EXPR, TREE_TYPE (expr), expr);
1548 else
1550 cp_lvalue_kind kind = lvalue_kind (expr);
1551 if ((kind & ~clk_class) != clk_none)
1553 tree type = unlowered_expr_type (expr);
1554 bool rval = !!(kind & clk_rvalueref);
1555 type = cp_build_reference_type (type, rval);
1556 expr = build_static_cast (type, expr, tf_warning_or_error);
1560 return expr;
1563 /* Finish a parenthesized expression EXPR. */
1565 tree
1566 finish_parenthesized_expr (tree expr)
1568 if (EXPR_P (expr))
1569 /* This inhibits warnings in c_common_truthvalue_conversion. */
1570 TREE_NO_WARNING (expr) = 1;
1572 if (TREE_CODE (expr) == OFFSET_REF
1573 || TREE_CODE (expr) == SCOPE_REF)
1574 /* [expr.unary.op]/3 The qualified id of a pointer-to-member must not be
1575 enclosed in parentheses. */
1576 PTRMEM_OK_P (expr) = 0;
1578 if (TREE_CODE (expr) == STRING_CST)
1579 PAREN_STRING_LITERAL_P (expr) = 1;
1581 expr = force_paren_expr (expr);
1583 return expr;
1586 /* Finish a reference to a non-static data member (DECL) that is not
1587 preceded by `.' or `->'. */
1589 tree
1590 finish_non_static_data_member (tree decl, tree object, tree qualifying_scope)
1592 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
1594 if (!object)
1596 tree scope = qualifying_scope;
1597 if (scope == NULL_TREE)
1598 scope = context_for_name_lookup (decl);
1599 object = maybe_dummy_object (scope, NULL);
1602 object = maybe_resolve_dummy (object);
1603 if (object == error_mark_node)
1604 return error_mark_node;
1606 /* DR 613: Can use non-static data members without an associated
1607 object in sizeof/decltype/alignof. */
1608 if (is_dummy_object (object) && cp_unevaluated_operand == 0
1609 && (!processing_template_decl || !current_class_ref))
1611 if (current_function_decl
1612 && DECL_STATIC_FUNCTION_P (current_function_decl))
1613 error ("invalid use of member %q+D in static member function", decl);
1614 else
1615 error ("invalid use of non-static data member %q+D", decl);
1616 error ("from this location");
1618 return error_mark_node;
1621 if (current_class_ptr)
1622 TREE_USED (current_class_ptr) = 1;
1623 if (processing_template_decl && !qualifying_scope)
1625 tree type = TREE_TYPE (decl);
1627 if (TREE_CODE (type) == REFERENCE_TYPE)
1628 /* Quals on the object don't matter. */;
1629 else if (PACK_EXPANSION_P (type))
1630 /* Don't bother trying to represent this. */
1631 type = NULL_TREE;
1632 else
1634 /* Set the cv qualifiers. */
1635 int quals = cp_type_quals (TREE_TYPE (object));
1637 if (DECL_MUTABLE_P (decl))
1638 quals &= ~TYPE_QUAL_CONST;
1640 quals |= cp_type_quals (TREE_TYPE (decl));
1641 type = cp_build_qualified_type (type, quals);
1644 return (convert_from_reference
1645 (build_min (COMPONENT_REF, type, object, decl, NULL_TREE)));
1647 /* If PROCESSING_TEMPLATE_DECL is nonzero here, then
1648 QUALIFYING_SCOPE is also non-null. Wrap this in a SCOPE_REF
1649 for now. */
1650 else if (processing_template_decl)
1651 return build_qualified_name (TREE_TYPE (decl),
1652 qualifying_scope,
1653 decl,
1654 /*template_p=*/false);
1655 else
1657 tree access_type = TREE_TYPE (object);
1659 perform_or_defer_access_check (TYPE_BINFO (access_type), decl,
1660 decl, tf_warning_or_error);
1662 /* If the data member was named `C::M', convert `*this' to `C'
1663 first. */
1664 if (qualifying_scope)
1666 tree binfo = NULL_TREE;
1667 object = build_scoped_ref (object, qualifying_scope,
1668 &binfo);
1671 return build_class_member_access_expr (object, decl,
1672 /*access_path=*/NULL_TREE,
1673 /*preserve_reference=*/false,
1674 tf_warning_or_error);
1678 /* If we are currently parsing a template and we encountered a typedef
1679 TYPEDEF_DECL that is being accessed though CONTEXT, this function
1680 adds the typedef to a list tied to the current template.
1681 At template instantiation time, that list is walked and access check
1682 performed for each typedef.
1683 LOCATION is the location of the usage point of TYPEDEF_DECL. */
1685 void
1686 add_typedef_to_current_template_for_access_check (tree typedef_decl,
1687 tree context,
1688 location_t location)
1690 tree template_info = NULL;
1691 tree cs = current_scope ();
1693 if (!is_typedef_decl (typedef_decl)
1694 || !context
1695 || !CLASS_TYPE_P (context)
1696 || !cs)
1697 return;
1699 if (CLASS_TYPE_P (cs) || TREE_CODE (cs) == FUNCTION_DECL)
1700 template_info = get_template_info (cs);
1702 if (template_info
1703 && TI_TEMPLATE (template_info)
1704 && !currently_open_class (context))
1705 append_type_to_template_for_access_check (cs, typedef_decl,
1706 context, location);
1709 /* DECL was the declaration to which a qualified-id resolved. Issue
1710 an error message if it is not accessible. If OBJECT_TYPE is
1711 non-NULL, we have just seen `x->' or `x.' and OBJECT_TYPE is the
1712 type of `*x', or `x', respectively. If the DECL was named as
1713 `A::B' then NESTED_NAME_SPECIFIER is `A'. */
1715 void
1716 check_accessibility_of_qualified_id (tree decl,
1717 tree object_type,
1718 tree nested_name_specifier)
1720 tree scope;
1721 tree qualifying_type = NULL_TREE;
1723 /* If we are parsing a template declaration and if decl is a typedef,
1724 add it to a list tied to the template.
1725 At template instantiation time, that list will be walked and
1726 access check performed. */
1727 add_typedef_to_current_template_for_access_check (decl,
1728 nested_name_specifier
1729 ? nested_name_specifier
1730 : DECL_CONTEXT (decl),
1731 input_location);
1733 /* If we're not checking, return immediately. */
1734 if (deferred_access_no_check)
1735 return;
1737 /* Determine the SCOPE of DECL. */
1738 scope = context_for_name_lookup (decl);
1739 /* If the SCOPE is not a type, then DECL is not a member. */
1740 if (!TYPE_P (scope))
1741 return;
1742 /* Compute the scope through which DECL is being accessed. */
1743 if (object_type
1744 /* OBJECT_TYPE might not be a class type; consider:
1746 class A { typedef int I; };
1747 I *p;
1748 p->A::I::~I();
1750 In this case, we will have "A::I" as the DECL, but "I" as the
1751 OBJECT_TYPE. */
1752 && CLASS_TYPE_P (object_type)
1753 && DERIVED_FROM_P (scope, object_type))
1754 /* If we are processing a `->' or `.' expression, use the type of the
1755 left-hand side. */
1756 qualifying_type = object_type;
1757 else if (nested_name_specifier)
1759 /* If the reference is to a non-static member of the
1760 current class, treat it as if it were referenced through
1761 `this'. */
1762 if (DECL_NONSTATIC_MEMBER_P (decl)
1763 && current_class_ptr
1764 && DERIVED_FROM_P (scope, current_class_type))
1765 qualifying_type = current_class_type;
1766 /* Otherwise, use the type indicated by the
1767 nested-name-specifier. */
1768 else
1769 qualifying_type = nested_name_specifier;
1771 else
1772 /* Otherwise, the name must be from the current class or one of
1773 its bases. */
1774 qualifying_type = currently_open_derived_class (scope);
1776 if (qualifying_type
1777 /* It is possible for qualifying type to be a TEMPLATE_TYPE_PARM
1778 or similar in a default argument value. */
1779 && CLASS_TYPE_P (qualifying_type)
1780 && !dependent_type_p (qualifying_type))
1781 perform_or_defer_access_check (TYPE_BINFO (qualifying_type), decl,
1782 decl, tf_warning_or_error);
1785 /* EXPR is the result of a qualified-id. The QUALIFYING_CLASS was the
1786 class named to the left of the "::" operator. DONE is true if this
1787 expression is a complete postfix-expression; it is false if this
1788 expression is followed by '->', '[', '(', etc. ADDRESS_P is true
1789 iff this expression is the operand of '&'. TEMPLATE_P is true iff
1790 the qualified-id was of the form "A::template B". TEMPLATE_ARG_P
1791 is true iff this qualified name appears as a template argument. */
1793 tree
1794 finish_qualified_id_expr (tree qualifying_class,
1795 tree expr,
1796 bool done,
1797 bool address_p,
1798 bool template_p,
1799 bool template_arg_p,
1800 tsubst_flags_t complain)
1802 gcc_assert (TYPE_P (qualifying_class));
1804 if (error_operand_p (expr))
1805 return error_mark_node;
1807 if ((DECL_P (expr) || BASELINK_P (expr))
1808 && !mark_used (expr, complain))
1809 return error_mark_node;
1811 if (template_p)
1812 check_template_keyword (expr);
1814 /* If EXPR occurs as the operand of '&', use special handling that
1815 permits a pointer-to-member. */
1816 if (address_p && done)
1818 if (TREE_CODE (expr) == SCOPE_REF)
1819 expr = TREE_OPERAND (expr, 1);
1820 expr = build_offset_ref (qualifying_class, expr,
1821 /*address_p=*/true, complain);
1822 return expr;
1825 /* No need to check access within an enum. */
1826 if (TREE_CODE (qualifying_class) == ENUMERAL_TYPE)
1827 return expr;
1829 /* Within the scope of a class, turn references to non-static
1830 members into expression of the form "this->...". */
1831 if (template_arg_p)
1832 /* But, within a template argument, we do not want make the
1833 transformation, as there is no "this" pointer. */
1835 else if (TREE_CODE (expr) == FIELD_DECL)
1837 push_deferring_access_checks (dk_no_check);
1838 expr = finish_non_static_data_member (expr, NULL_TREE,
1839 qualifying_class);
1840 pop_deferring_access_checks ();
1842 else if (BASELINK_P (expr) && !processing_template_decl)
1844 /* See if any of the functions are non-static members. */
1845 /* If so, the expression may be relative to 'this'. */
1846 if (!shared_member_p (expr)
1847 && current_class_ptr
1848 && DERIVED_FROM_P (qualifying_class,
1849 current_nonlambda_class_type ()))
1850 expr = (build_class_member_access_expr
1851 (maybe_dummy_object (qualifying_class, NULL),
1852 expr,
1853 BASELINK_ACCESS_BINFO (expr),
1854 /*preserve_reference=*/false,
1855 complain));
1856 else if (done)
1857 /* The expression is a qualified name whose address is not
1858 being taken. */
1859 expr = build_offset_ref (qualifying_class, expr, /*address_p=*/false,
1860 complain);
1862 else if (BASELINK_P (expr))
1864 else
1866 /* In a template, return a SCOPE_REF for most qualified-ids
1867 so that we can check access at instantiation time. But if
1868 we're looking at a member of the current instantiation, we
1869 know we have access and building up the SCOPE_REF confuses
1870 non-type template argument handling. */
1871 if (processing_template_decl
1872 && !currently_open_class (qualifying_class))
1873 expr = build_qualified_name (TREE_TYPE (expr),
1874 qualifying_class, expr,
1875 template_p);
1877 expr = convert_from_reference (expr);
1880 return expr;
1883 /* Begin a statement-expression. The value returned must be passed to
1884 finish_stmt_expr. */
1886 tree
1887 begin_stmt_expr (void)
1889 return push_stmt_list ();
1892 /* Process the final expression of a statement expression. EXPR can be
1893 NULL, if the final expression is empty. Return a STATEMENT_LIST
1894 containing all the statements in the statement-expression, or
1895 ERROR_MARK_NODE if there was an error. */
1897 tree
1898 finish_stmt_expr_expr (tree expr, tree stmt_expr)
1900 if (error_operand_p (expr))
1902 /* The type of the statement-expression is the type of the last
1903 expression. */
1904 TREE_TYPE (stmt_expr) = error_mark_node;
1905 return error_mark_node;
1908 /* If the last statement does not have "void" type, then the value
1909 of the last statement is the value of the entire expression. */
1910 if (expr)
1912 tree type = TREE_TYPE (expr);
1914 if (processing_template_decl)
1916 expr = build_stmt (input_location, EXPR_STMT, expr);
1917 expr = add_stmt (expr);
1918 /* Mark the last statement so that we can recognize it as such at
1919 template-instantiation time. */
1920 EXPR_STMT_STMT_EXPR_RESULT (expr) = 1;
1922 else if (VOID_TYPE_P (type))
1924 /* Just treat this like an ordinary statement. */
1925 expr = finish_expr_stmt (expr);
1927 else
1929 /* It actually has a value we need to deal with. First, force it
1930 to be an rvalue so that we won't need to build up a copy
1931 constructor call later when we try to assign it to something. */
1932 expr = force_rvalue (expr, tf_warning_or_error);
1933 if (error_operand_p (expr))
1934 return error_mark_node;
1936 /* Update for array-to-pointer decay. */
1937 type = TREE_TYPE (expr);
1939 /* Wrap it in a CLEANUP_POINT_EXPR and add it to the list like a
1940 normal statement, but don't convert to void or actually add
1941 the EXPR_STMT. */
1942 if (TREE_CODE (expr) != CLEANUP_POINT_EXPR)
1943 expr = maybe_cleanup_point_expr (expr);
1944 add_stmt (expr);
1947 /* The type of the statement-expression is the type of the last
1948 expression. */
1949 TREE_TYPE (stmt_expr) = type;
1952 return stmt_expr;
1955 /* Finish a statement-expression. EXPR should be the value returned
1956 by the previous begin_stmt_expr. Returns an expression
1957 representing the statement-expression. */
1959 tree
1960 finish_stmt_expr (tree stmt_expr, bool has_no_scope)
1962 tree type;
1963 tree result;
1965 if (error_operand_p (stmt_expr))
1967 pop_stmt_list (stmt_expr);
1968 return error_mark_node;
1971 gcc_assert (TREE_CODE (stmt_expr) == STATEMENT_LIST);
1973 type = TREE_TYPE (stmt_expr);
1974 result = pop_stmt_list (stmt_expr);
1975 TREE_TYPE (result) = type;
1977 if (processing_template_decl)
1979 result = build_min (STMT_EXPR, type, result);
1980 TREE_SIDE_EFFECTS (result) = 1;
1981 STMT_EXPR_NO_SCOPE (result) = has_no_scope;
1983 else if (CLASS_TYPE_P (type))
1985 /* Wrap the statement-expression in a TARGET_EXPR so that the
1986 temporary object created by the final expression is destroyed at
1987 the end of the full-expression containing the
1988 statement-expression. */
1989 result = force_target_expr (type, result, tf_warning_or_error);
1992 return result;
1995 /* Returns the expression which provides the value of STMT_EXPR. */
1997 tree
1998 stmt_expr_value_expr (tree stmt_expr)
2000 tree t = STMT_EXPR_STMT (stmt_expr);
2002 if (TREE_CODE (t) == BIND_EXPR)
2003 t = BIND_EXPR_BODY (t);
2005 if (TREE_CODE (t) == STATEMENT_LIST && STATEMENT_LIST_TAIL (t))
2006 t = STATEMENT_LIST_TAIL (t)->stmt;
2008 if (TREE_CODE (t) == EXPR_STMT)
2009 t = EXPR_STMT_EXPR (t);
2011 return t;
2014 /* Return TRUE iff EXPR_STMT is an empty list of
2015 expression statements. */
2017 bool
2018 empty_expr_stmt_p (tree expr_stmt)
2020 tree body = NULL_TREE;
2022 if (expr_stmt == void_zero_node)
2023 return true;
2025 if (expr_stmt)
2027 if (TREE_CODE (expr_stmt) == EXPR_STMT)
2028 body = EXPR_STMT_EXPR (expr_stmt);
2029 else if (TREE_CODE (expr_stmt) == STATEMENT_LIST)
2030 body = expr_stmt;
2033 if (body)
2035 if (TREE_CODE (body) == STATEMENT_LIST)
2036 return tsi_end_p (tsi_start (body));
2037 else
2038 return empty_expr_stmt_p (body);
2040 return false;
2043 /* Perform Koenig lookup. FN is the postfix-expression representing
2044 the function (or functions) to call; ARGS are the arguments to the
2045 call; if INCLUDE_STD then the `std' namespace is automatically
2046 considered an associated namespace (used in range-based for loops).
2047 Returns the functions to be considered by overload resolution. */
2049 tree
2050 perform_koenig_lookup (tree fn, vec<tree, va_gc> *args, bool include_std,
2051 tsubst_flags_t complain)
2053 tree identifier = NULL_TREE;
2054 tree functions = NULL_TREE;
2055 tree tmpl_args = NULL_TREE;
2056 bool template_id = false;
2058 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
2060 /* Use a separate flag to handle null args. */
2061 template_id = true;
2062 tmpl_args = TREE_OPERAND (fn, 1);
2063 fn = TREE_OPERAND (fn, 0);
2066 /* Find the name of the overloaded function. */
2067 if (identifier_p (fn))
2068 identifier = fn;
2069 else if (is_overloaded_fn (fn))
2071 functions = fn;
2072 identifier = DECL_NAME (get_first_fn (functions));
2074 else if (DECL_P (fn))
2076 functions = fn;
2077 identifier = DECL_NAME (fn);
2080 /* A call to a namespace-scope function using an unqualified name.
2082 Do Koenig lookup -- unless any of the arguments are
2083 type-dependent. */
2084 if (!any_type_dependent_arguments_p (args)
2085 && !any_dependent_template_arguments_p (tmpl_args))
2087 fn = lookup_arg_dependent (identifier, functions, args, include_std);
2088 if (!fn)
2090 /* The unqualified name could not be resolved. */
2091 if (complain)
2092 fn = unqualified_fn_lookup_error (identifier);
2093 else
2094 fn = identifier;
2098 if (fn && template_id)
2099 fn = build2 (TEMPLATE_ID_EXPR, unknown_type_node, fn, tmpl_args);
2101 return fn;
2104 /* Generate an expression for `FN (ARGS)'. This may change the
2105 contents of ARGS.
2107 If DISALLOW_VIRTUAL is true, the call to FN will be not generated
2108 as a virtual call, even if FN is virtual. (This flag is set when
2109 encountering an expression where the function name is explicitly
2110 qualified. For example a call to `X::f' never generates a virtual
2111 call.)
2113 Returns code for the call. */
2115 tree
2116 finish_call_expr (tree fn, vec<tree, va_gc> **args, bool disallow_virtual,
2117 bool koenig_p, tsubst_flags_t complain)
2119 tree result;
2120 tree orig_fn;
2121 vec<tree, va_gc> *orig_args = NULL;
2123 if (fn == error_mark_node)
2124 return error_mark_node;
2126 gcc_assert (!TYPE_P (fn));
2128 orig_fn = fn;
2130 if (processing_template_decl)
2132 /* If the call expression is dependent, build a CALL_EXPR node
2133 with no type; type_dependent_expression_p recognizes
2134 expressions with no type as being dependent. */
2135 if (type_dependent_expression_p (fn)
2136 || any_type_dependent_arguments_p (*args)
2137 /* For a non-static member function that doesn't have an
2138 explicit object argument, we need to specifically
2139 test the type dependency of the "this" pointer because it
2140 is not included in *ARGS even though it is considered to
2141 be part of the list of arguments. Note that this is
2142 related to CWG issues 515 and 1005. */
2143 || (TREE_CODE (fn) != COMPONENT_REF
2144 && non_static_member_function_p (fn)
2145 && current_class_ref
2146 && type_dependent_expression_p (current_class_ref)))
2148 result = build_nt_call_vec (fn, *args);
2149 SET_EXPR_LOCATION (result, EXPR_LOC_OR_HERE (fn));
2150 KOENIG_LOOKUP_P (result) = koenig_p;
2151 if (cfun)
2155 tree fndecl = OVL_CURRENT (fn);
2156 if (TREE_CODE (fndecl) != FUNCTION_DECL
2157 || !TREE_THIS_VOLATILE (fndecl))
2158 break;
2159 fn = OVL_NEXT (fn);
2161 while (fn);
2162 if (!fn)
2163 current_function_returns_abnormally = 1;
2165 return result;
2167 orig_args = make_tree_vector_copy (*args);
2168 if (!BASELINK_P (fn)
2169 && TREE_CODE (fn) != PSEUDO_DTOR_EXPR
2170 && TREE_TYPE (fn) != unknown_type_node)
2171 fn = build_non_dependent_expr (fn);
2172 make_args_non_dependent (*args);
2175 if (TREE_CODE (fn) == COMPONENT_REF)
2177 tree member = TREE_OPERAND (fn, 1);
2178 if (BASELINK_P (member))
2180 tree object = TREE_OPERAND (fn, 0);
2181 return build_new_method_call (object, member,
2182 args, NULL_TREE,
2183 (disallow_virtual
2184 ? LOOKUP_NORMAL | LOOKUP_NONVIRTUAL
2185 : LOOKUP_NORMAL),
2186 /*fn_p=*/NULL,
2187 complain);
2191 /* Per 13.3.1.1, '(&f)(...)' is the same as '(f)(...)'. */
2192 if (TREE_CODE (fn) == ADDR_EXPR
2193 && TREE_CODE (TREE_OPERAND (fn, 0)) == OVERLOAD)
2194 fn = TREE_OPERAND (fn, 0);
2196 if (is_overloaded_fn (fn))
2197 fn = baselink_for_fns (fn);
2199 result = NULL_TREE;
2200 if (BASELINK_P (fn))
2202 tree object;
2204 /* A call to a member function. From [over.call.func]:
2206 If the keyword this is in scope and refers to the class of
2207 that member function, or a derived class thereof, then the
2208 function call is transformed into a qualified function call
2209 using (*this) as the postfix-expression to the left of the
2210 . operator.... [Otherwise] a contrived object of type T
2211 becomes the implied object argument.
2213 In this situation:
2215 struct A { void f(); };
2216 struct B : public A {};
2217 struct C : public A { void g() { B::f(); }};
2219 "the class of that member function" refers to `A'. But 11.2
2220 [class.access.base] says that we need to convert 'this' to B* as
2221 part of the access, so we pass 'B' to maybe_dummy_object. */
2223 object = maybe_dummy_object (BINFO_TYPE (BASELINK_ACCESS_BINFO (fn)),
2224 NULL);
2226 if (processing_template_decl)
2228 if (type_dependent_expression_p (object))
2230 tree ret = build_nt_call_vec (orig_fn, orig_args);
2231 release_tree_vector (orig_args);
2232 return ret;
2234 object = build_non_dependent_expr (object);
2237 result = build_new_method_call (object, fn, args, NULL_TREE,
2238 (disallow_virtual
2239 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
2240 : LOOKUP_NORMAL),
2241 /*fn_p=*/NULL,
2242 complain);
2244 else if (is_overloaded_fn (fn))
2246 /* If the function is an overloaded builtin, resolve it. */
2247 if (TREE_CODE (fn) == FUNCTION_DECL
2248 && (DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL
2249 || DECL_BUILT_IN_CLASS (fn) == BUILT_IN_MD))
2250 result = resolve_overloaded_builtin (input_location, fn, *args);
2252 if (!result)
2254 if (warn_sizeof_pointer_memaccess
2255 && !vec_safe_is_empty (*args)
2256 && !processing_template_decl)
2258 location_t sizeof_arg_loc[3];
2259 tree sizeof_arg[3];
2260 unsigned int i;
2261 for (i = 0; i < 3; i++)
2263 tree t;
2265 sizeof_arg_loc[i] = UNKNOWN_LOCATION;
2266 sizeof_arg[i] = NULL_TREE;
2267 if (i >= (*args)->length ())
2268 continue;
2269 t = (**args)[i];
2270 if (TREE_CODE (t) != SIZEOF_EXPR)
2271 continue;
2272 if (SIZEOF_EXPR_TYPE_P (t))
2273 sizeof_arg[i] = TREE_TYPE (TREE_OPERAND (t, 0));
2274 else
2275 sizeof_arg[i] = TREE_OPERAND (t, 0);
2276 sizeof_arg_loc[i] = EXPR_LOCATION (t);
2278 sizeof_pointer_memaccess_warning
2279 (sizeof_arg_loc, fn, *args,
2280 sizeof_arg, same_type_ignoring_top_level_qualifiers_p);
2283 /* A call to a namespace-scope function. */
2284 result = build_new_function_call (fn, args, koenig_p, complain);
2287 else if (TREE_CODE (fn) == PSEUDO_DTOR_EXPR)
2289 if (!vec_safe_is_empty (*args))
2290 error ("arguments to destructor are not allowed");
2291 /* Mark the pseudo-destructor call as having side-effects so
2292 that we do not issue warnings about its use. */
2293 result = build1 (NOP_EXPR,
2294 void_type_node,
2295 TREE_OPERAND (fn, 0));
2296 TREE_SIDE_EFFECTS (result) = 1;
2298 else if (CLASS_TYPE_P (TREE_TYPE (fn)))
2299 /* If the "function" is really an object of class type, it might
2300 have an overloaded `operator ()'. */
2301 result = build_op_call (fn, args, complain);
2303 if (!result)
2304 /* A call where the function is unknown. */
2305 result = cp_build_function_call_vec (fn, args, complain);
2307 if (processing_template_decl && result != error_mark_node)
2309 if (INDIRECT_REF_P (result))
2310 result = TREE_OPERAND (result, 0);
2311 result = build_call_vec (TREE_TYPE (result), orig_fn, orig_args);
2312 SET_EXPR_LOCATION (result, input_location);
2313 KOENIG_LOOKUP_P (result) = koenig_p;
2314 release_tree_vector (orig_args);
2315 result = convert_from_reference (result);
2318 if (koenig_p)
2320 /* Free garbage OVERLOADs from arg-dependent lookup. */
2321 tree next = NULL_TREE;
2322 for (fn = orig_fn;
2323 fn && TREE_CODE (fn) == OVERLOAD && OVL_ARG_DEPENDENT (fn);
2324 fn = next)
2326 if (processing_template_decl)
2327 /* In a template, we'll re-use them at instantiation time. */
2328 OVL_ARG_DEPENDENT (fn) = false;
2329 else
2331 next = OVL_CHAIN (fn);
2332 ggc_free (fn);
2337 return result;
2340 /* Finish a call to a postfix increment or decrement or EXPR. (Which
2341 is indicated by CODE, which should be POSTINCREMENT_EXPR or
2342 POSTDECREMENT_EXPR.) */
2344 tree
2345 finish_increment_expr (tree expr, enum tree_code code)
2347 return build_x_unary_op (input_location, code, expr, tf_warning_or_error);
2350 /* Finish a use of `this'. Returns an expression for `this'. */
2352 tree
2353 finish_this_expr (void)
2355 tree result;
2357 if (current_class_ptr)
2359 tree type = TREE_TYPE (current_class_ref);
2361 /* In a lambda expression, 'this' refers to the captured 'this'. */
2362 if (LAMBDA_TYPE_P (type))
2363 result = lambda_expr_this_capture (CLASSTYPE_LAMBDA_EXPR (type));
2364 else
2365 result = current_class_ptr;
2367 else if (current_function_decl
2368 && DECL_STATIC_FUNCTION_P (current_function_decl))
2370 error ("%<this%> is unavailable for static member functions");
2371 result = error_mark_node;
2373 else
2375 if (current_function_decl)
2376 error ("invalid use of %<this%> in non-member function");
2377 else
2378 error ("invalid use of %<this%> at top level");
2379 result = error_mark_node;
2382 /* The keyword 'this' is a prvalue expression. */
2383 result = rvalue (result);
2385 return result;
2388 /* Finish a pseudo-destructor expression. If SCOPE is NULL, the
2389 expression was of the form `OBJECT.~DESTRUCTOR' where DESTRUCTOR is
2390 the TYPE for the type given. If SCOPE is non-NULL, the expression
2391 was of the form `OBJECT.SCOPE::~DESTRUCTOR'. */
2393 tree
2394 finish_pseudo_destructor_expr (tree object, tree scope, tree destructor,
2395 location_t loc)
2397 if (object == error_mark_node || destructor == error_mark_node)
2398 return error_mark_node;
2400 gcc_assert (TYPE_P (destructor));
2402 if (!processing_template_decl)
2404 if (scope == error_mark_node)
2406 error_at (loc, "invalid qualifying scope in pseudo-destructor name");
2407 return error_mark_node;
2409 if (is_auto (destructor))
2410 destructor = TREE_TYPE (object);
2411 if (scope && TYPE_P (scope) && !check_dtor_name (scope, destructor))
2413 error_at (loc,
2414 "qualified type %qT does not match destructor name ~%qT",
2415 scope, destructor);
2416 return error_mark_node;
2420 /* [expr.pseudo] says both:
2422 The type designated by the pseudo-destructor-name shall be
2423 the same as the object type.
2425 and:
2427 The cv-unqualified versions of the object type and of the
2428 type designated by the pseudo-destructor-name shall be the
2429 same type.
2431 We implement the more generous second sentence, since that is
2432 what most other compilers do. */
2433 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (object),
2434 destructor))
2436 error_at (loc, "%qE is not of type %qT", object, destructor);
2437 return error_mark_node;
2441 return build3_loc (loc, PSEUDO_DTOR_EXPR, void_type_node, object,
2442 scope, destructor);
2445 /* Finish an expression of the form CODE EXPR. */
2447 tree
2448 finish_unary_op_expr (location_t loc, enum tree_code code, tree expr,
2449 tsubst_flags_t complain)
2451 tree result = build_x_unary_op (loc, code, expr, complain);
2452 if ((complain & tf_warning)
2453 && TREE_OVERFLOW_P (result) && !TREE_OVERFLOW_P (expr))
2454 overflow_warning (input_location, result);
2456 return result;
2459 /* Finish a compound-literal expression. TYPE is the type to which
2460 the CONSTRUCTOR in COMPOUND_LITERAL is being cast. */
2462 tree
2463 finish_compound_literal (tree type, tree compound_literal,
2464 tsubst_flags_t complain)
2466 if (type == error_mark_node)
2467 return error_mark_node;
2469 if (TREE_CODE (type) == REFERENCE_TYPE)
2471 compound_literal
2472 = finish_compound_literal (TREE_TYPE (type), compound_literal,
2473 complain);
2474 return cp_build_c_cast (type, compound_literal, complain);
2477 if (!TYPE_OBJ_P (type))
2479 if (complain & tf_error)
2480 error ("compound literal of non-object type %qT", type);
2481 return error_mark_node;
2484 if (processing_template_decl)
2486 TREE_TYPE (compound_literal) = type;
2487 /* Mark the expression as a compound literal. */
2488 TREE_HAS_CONSTRUCTOR (compound_literal) = 1;
2489 return compound_literal;
2492 type = complete_type (type);
2494 if (TYPE_NON_AGGREGATE_CLASS (type))
2496 /* Trying to deal with a CONSTRUCTOR instead of a TREE_LIST
2497 everywhere that deals with function arguments would be a pain, so
2498 just wrap it in a TREE_LIST. The parser set a flag so we know
2499 that it came from T{} rather than T({}). */
2500 CONSTRUCTOR_IS_DIRECT_INIT (compound_literal) = 1;
2501 compound_literal = build_tree_list (NULL_TREE, compound_literal);
2502 return build_functional_cast (type, compound_literal, complain);
2505 if (TREE_CODE (type) == ARRAY_TYPE
2506 && check_array_initializer (NULL_TREE, type, compound_literal))
2507 return error_mark_node;
2508 compound_literal = reshape_init (type, compound_literal, complain);
2509 if (SCALAR_TYPE_P (type)
2510 && !BRACE_ENCLOSED_INITIALIZER_P (compound_literal)
2511 && (complain & tf_warning_or_error))
2512 check_narrowing (type, compound_literal);
2513 if (TREE_CODE (type) == ARRAY_TYPE
2514 && TYPE_DOMAIN (type) == NULL_TREE)
2516 cp_complete_array_type_or_error (&type, compound_literal,
2517 false, complain);
2518 if (type == error_mark_node)
2519 return error_mark_node;
2521 compound_literal = digest_init (type, compound_literal, complain);
2522 if (TREE_CODE (compound_literal) == CONSTRUCTOR)
2523 TREE_HAS_CONSTRUCTOR (compound_literal) = true;
2524 /* Put static/constant array temporaries in static variables, but always
2525 represent class temporaries with TARGET_EXPR so we elide copies. */
2526 if ((!at_function_scope_p () || CP_TYPE_CONST_P (type))
2527 && TREE_CODE (type) == ARRAY_TYPE
2528 && !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
2529 && !cp_unevaluated_operand
2530 && initializer_constant_valid_p (compound_literal, type))
2532 tree decl = create_temporary_var (type);
2533 DECL_INITIAL (decl) = compound_literal;
2534 TREE_STATIC (decl) = 1;
2535 if (literal_type_p (type) && CP_TYPE_CONST_NON_VOLATILE_P (type))
2537 /* 5.19 says that a constant expression can include an
2538 lvalue-rvalue conversion applied to "a glvalue of literal type
2539 that refers to a non-volatile temporary object initialized
2540 with a constant expression". Rather than try to communicate
2541 that this VAR_DECL is a temporary, just mark it constexpr. */
2542 DECL_DECLARED_CONSTEXPR_P (decl) = true;
2543 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
2544 TREE_CONSTANT (decl) = true;
2546 cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
2547 decl = pushdecl_top_level (decl);
2548 DECL_NAME (decl) = make_anon_name ();
2549 SET_DECL_ASSEMBLER_NAME (decl, DECL_NAME (decl));
2550 /* Make sure the destructor is callable. */
2551 tree clean = cxx_maybe_build_cleanup (decl, complain);
2552 if (clean == error_mark_node)
2553 return error_mark_node;
2554 return decl;
2556 else
2557 return get_target_expr_sfinae (compound_literal, complain);
2560 /* Return the declaration for the function-name variable indicated by
2561 ID. */
2563 tree
2564 finish_fname (tree id)
2566 tree decl;
2568 decl = fname_decl (input_location, C_RID_CODE (id), id);
2569 if (processing_template_decl && current_function_decl)
2570 decl = DECL_NAME (decl);
2571 return decl;
2574 /* Finish a translation unit. */
2576 void
2577 finish_translation_unit (void)
2579 /* In case there were missing closebraces,
2580 get us back to the global binding level. */
2581 pop_everything ();
2582 while (current_namespace != global_namespace)
2583 pop_namespace ();
2585 /* Do file scope __FUNCTION__ et al. */
2586 finish_fname_decls ();
2589 /* Finish a template type parameter, specified as AGGR IDENTIFIER.
2590 Returns the parameter. */
2592 tree
2593 finish_template_type_parm (tree aggr, tree identifier)
2595 if (aggr != class_type_node)
2597 permerror (input_location, "template type parameters must use the keyword %<class%> or %<typename%>");
2598 aggr = class_type_node;
2601 return build_tree_list (aggr, identifier);
2604 /* Finish a template template parameter, specified as AGGR IDENTIFIER.
2605 Returns the parameter. */
2607 tree
2608 finish_template_template_parm (tree aggr, tree identifier)
2610 tree decl = build_decl (input_location,
2611 TYPE_DECL, identifier, NULL_TREE);
2612 tree tmpl = build_lang_decl (TEMPLATE_DECL, identifier, NULL_TREE);
2613 DECL_TEMPLATE_PARMS (tmpl) = current_template_parms;
2614 DECL_TEMPLATE_RESULT (tmpl) = decl;
2615 DECL_ARTIFICIAL (decl) = 1;
2616 end_template_decl ();
2618 gcc_assert (DECL_TEMPLATE_PARMS (tmpl));
2620 check_default_tmpl_args (decl, DECL_TEMPLATE_PARMS (tmpl),
2621 /*is_primary=*/true, /*is_partial=*/false,
2622 /*is_friend=*/0);
2624 return finish_template_type_parm (aggr, tmpl);
2627 /* ARGUMENT is the default-argument value for a template template
2628 parameter. If ARGUMENT is invalid, issue error messages and return
2629 the ERROR_MARK_NODE. Otherwise, ARGUMENT itself is returned. */
2631 tree
2632 check_template_template_default_arg (tree argument)
2634 if (TREE_CODE (argument) != TEMPLATE_DECL
2635 && TREE_CODE (argument) != TEMPLATE_TEMPLATE_PARM
2636 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
2638 if (TREE_CODE (argument) == TYPE_DECL)
2639 error ("invalid use of type %qT as a default value for a template "
2640 "template-parameter", TREE_TYPE (argument));
2641 else
2642 error ("invalid default argument for a template template parameter");
2643 return error_mark_node;
2646 return argument;
2649 /* Begin a class definition, as indicated by T. */
2651 tree
2652 begin_class_definition (tree t)
2654 if (error_operand_p (t) || error_operand_p (TYPE_MAIN_DECL (t)))
2655 return error_mark_node;
2657 if (processing_template_parmlist)
2659 error ("definition of %q#T inside template parameter list", t);
2660 return error_mark_node;
2663 /* According to the C++ ABI, decimal classes defined in ISO/IEC TR 24733
2664 are passed the same as decimal scalar types. */
2665 if (TREE_CODE (t) == RECORD_TYPE
2666 && !processing_template_decl)
2668 tree ns = TYPE_CONTEXT (t);
2669 if (ns && TREE_CODE (ns) == NAMESPACE_DECL
2670 && DECL_CONTEXT (ns) == std_node
2671 && DECL_NAME (ns)
2672 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (ns)), "decimal"))
2674 const char *n = TYPE_NAME_STRING (t);
2675 if ((strcmp (n, "decimal32") == 0)
2676 || (strcmp (n, "decimal64") == 0)
2677 || (strcmp (n, "decimal128") == 0))
2678 TYPE_TRANSPARENT_AGGR (t) = 1;
2682 /* A non-implicit typename comes from code like:
2684 template <typename T> struct A {
2685 template <typename U> struct A<T>::B ...
2687 This is erroneous. */
2688 else if (TREE_CODE (t) == TYPENAME_TYPE)
2690 error ("invalid definition of qualified type %qT", t);
2691 t = error_mark_node;
2694 if (t == error_mark_node || ! MAYBE_CLASS_TYPE_P (t))
2696 t = make_class_type (RECORD_TYPE);
2697 pushtag (make_anon_name (), t, /*tag_scope=*/ts_current);
2700 if (TYPE_BEING_DEFINED (t))
2702 t = make_class_type (TREE_CODE (t));
2703 pushtag (TYPE_IDENTIFIER (t), t, /*tag_scope=*/ts_current);
2705 maybe_process_partial_specialization (t);
2706 pushclass (t);
2707 TYPE_BEING_DEFINED (t) = 1;
2709 if (flag_pack_struct)
2711 tree v;
2712 TYPE_PACKED (t) = 1;
2713 /* Even though the type is being defined for the first time
2714 here, there might have been a forward declaration, so there
2715 might be cv-qualified variants of T. */
2716 for (v = TYPE_NEXT_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v))
2717 TYPE_PACKED (v) = 1;
2719 /* Reset the interface data, at the earliest possible
2720 moment, as it might have been set via a class foo;
2721 before. */
2722 if (! TYPE_ANONYMOUS_P (t))
2724 struct c_fileinfo *finfo = get_fileinfo (input_filename);
2725 CLASSTYPE_INTERFACE_ONLY (t) = finfo->interface_only;
2726 SET_CLASSTYPE_INTERFACE_UNKNOWN_X
2727 (t, finfo->interface_unknown);
2729 reset_specialization();
2731 /* Make a declaration for this class in its own scope. */
2732 build_self_reference ();
2734 return t;
2737 /* Finish the member declaration given by DECL. */
2739 void
2740 finish_member_declaration (tree decl)
2742 if (decl == error_mark_node || decl == NULL_TREE)
2743 return;
2745 if (decl == void_type_node)
2746 /* The COMPONENT was a friend, not a member, and so there's
2747 nothing for us to do. */
2748 return;
2750 /* We should see only one DECL at a time. */
2751 gcc_assert (DECL_CHAIN (decl) == NULL_TREE);
2753 /* Set up access control for DECL. */
2754 TREE_PRIVATE (decl)
2755 = (current_access_specifier == access_private_node);
2756 TREE_PROTECTED (decl)
2757 = (current_access_specifier == access_protected_node);
2758 if (TREE_CODE (decl) == TEMPLATE_DECL)
2760 TREE_PRIVATE (DECL_TEMPLATE_RESULT (decl)) = TREE_PRIVATE (decl);
2761 TREE_PROTECTED (DECL_TEMPLATE_RESULT (decl)) = TREE_PROTECTED (decl);
2764 /* Mark the DECL as a member of the current class, unless it's
2765 a member of an enumeration. */
2766 if (TREE_CODE (decl) != CONST_DECL)
2767 DECL_CONTEXT (decl) = current_class_type;
2769 /* Check for bare parameter packs in the member variable declaration. */
2770 if (TREE_CODE (decl) == FIELD_DECL)
2772 if (check_for_bare_parameter_packs (TREE_TYPE (decl)))
2773 TREE_TYPE (decl) = error_mark_node;
2774 if (check_for_bare_parameter_packs (DECL_ATTRIBUTES (decl)))
2775 DECL_ATTRIBUTES (decl) = NULL_TREE;
2778 /* [dcl.link]
2780 A C language linkage is ignored for the names of class members
2781 and the member function type of class member functions. */
2782 if (DECL_LANG_SPECIFIC (decl) && DECL_LANGUAGE (decl) == lang_c)
2783 SET_DECL_LANGUAGE (decl, lang_cplusplus);
2785 /* Put functions on the TYPE_METHODS list and everything else on the
2786 TYPE_FIELDS list. Note that these are built up in reverse order.
2787 We reverse them (to obtain declaration order) in finish_struct. */
2788 if (DECL_DECLARES_FUNCTION_P (decl))
2790 /* We also need to add this function to the
2791 CLASSTYPE_METHOD_VEC. */
2792 if (add_method (current_class_type, decl, NULL_TREE))
2794 DECL_CHAIN (decl) = TYPE_METHODS (current_class_type);
2795 TYPE_METHODS (current_class_type) = decl;
2797 maybe_add_class_template_decl_list (current_class_type, decl,
2798 /*friend_p=*/0);
2801 /* Enter the DECL into the scope of the class, if the class
2802 isn't a closure (whose fields are supposed to be unnamed). */
2803 else if (CLASSTYPE_LAMBDA_EXPR (current_class_type)
2804 || pushdecl_class_level (decl))
2806 if (TREE_CODE (decl) == USING_DECL)
2808 /* For now, ignore class-scope USING_DECLS, so that
2809 debugging backends do not see them. */
2810 DECL_IGNORED_P (decl) = 1;
2813 /* All TYPE_DECLs go at the end of TYPE_FIELDS. Ordinary fields
2814 go at the beginning. The reason is that lookup_field_1
2815 searches the list in order, and we want a field name to
2816 override a type name so that the "struct stat hack" will
2817 work. In particular:
2819 struct S { enum E { }; int E } s;
2820 s.E = 3;
2822 is valid. In addition, the FIELD_DECLs must be maintained in
2823 declaration order so that class layout works as expected.
2824 However, we don't need that order until class layout, so we
2825 save a little time by putting FIELD_DECLs on in reverse order
2826 here, and then reversing them in finish_struct_1. (We could
2827 also keep a pointer to the correct insertion points in the
2828 list.) */
2830 if (TREE_CODE (decl) == TYPE_DECL)
2831 TYPE_FIELDS (current_class_type)
2832 = chainon (TYPE_FIELDS (current_class_type), decl);
2833 else
2835 DECL_CHAIN (decl) = TYPE_FIELDS (current_class_type);
2836 TYPE_FIELDS (current_class_type) = decl;
2839 maybe_add_class_template_decl_list (current_class_type, decl,
2840 /*friend_p=*/0);
2843 if (pch_file)
2844 note_decl_for_pch (decl);
2847 /* DECL has been declared while we are building a PCH file. Perform
2848 actions that we might normally undertake lazily, but which can be
2849 performed now so that they do not have to be performed in
2850 translation units which include the PCH file. */
2852 void
2853 note_decl_for_pch (tree decl)
2855 gcc_assert (pch_file);
2857 /* There's a good chance that we'll have to mangle names at some
2858 point, even if only for emission in debugging information. */
2859 if (VAR_OR_FUNCTION_DECL_P (decl)
2860 && !processing_template_decl)
2861 mangle_decl (decl);
2864 /* Finish processing a complete template declaration. The PARMS are
2865 the template parameters. */
2867 void
2868 finish_template_decl (tree parms)
2870 if (parms)
2871 end_template_decl ();
2872 else
2873 end_specialization ();
2876 /* Finish processing a template-id (which names a type) of the form
2877 NAME < ARGS >. Return the TYPE_DECL for the type named by the
2878 template-id. If ENTERING_SCOPE is nonzero we are about to enter
2879 the scope of template-id indicated. */
2881 tree
2882 finish_template_type (tree name, tree args, int entering_scope)
2884 tree type;
2886 type = lookup_template_class (name, args,
2887 NULL_TREE, NULL_TREE, entering_scope,
2888 tf_warning_or_error | tf_user);
2889 if (type == error_mark_node)
2890 return type;
2891 else if (CLASS_TYPE_P (type) && !alias_type_or_template_p (type))
2892 return TYPE_STUB_DECL (type);
2893 else
2894 return TYPE_NAME (type);
2897 /* Finish processing a BASE_CLASS with the indicated ACCESS_SPECIFIER.
2898 Return a TREE_LIST containing the ACCESS_SPECIFIER and the
2899 BASE_CLASS, or NULL_TREE if an error occurred. The
2900 ACCESS_SPECIFIER is one of
2901 access_{default,public,protected_private}_node. For a virtual base
2902 we set TREE_TYPE. */
2904 tree
2905 finish_base_specifier (tree base, tree access, bool virtual_p)
2907 tree result;
2909 if (base == error_mark_node)
2911 error ("invalid base-class specification");
2912 result = NULL_TREE;
2914 else if (! MAYBE_CLASS_TYPE_P (base))
2916 error ("%qT is not a class type", base);
2917 result = NULL_TREE;
2919 else
2921 if (cp_type_quals (base) != 0)
2923 /* DR 484: Can a base-specifier name a cv-qualified
2924 class type? */
2925 base = TYPE_MAIN_VARIANT (base);
2927 result = build_tree_list (access, base);
2928 if (virtual_p)
2929 TREE_TYPE (result) = integer_type_node;
2932 return result;
2935 /* If FNS is a member function, a set of member functions, or a
2936 template-id referring to one or more member functions, return a
2937 BASELINK for FNS, incorporating the current access context.
2938 Otherwise, return FNS unchanged. */
2940 tree
2941 baselink_for_fns (tree fns)
2943 tree scope;
2944 tree cl;
2946 if (BASELINK_P (fns)
2947 || error_operand_p (fns))
2948 return fns;
2950 scope = ovl_scope (fns);
2951 if (!CLASS_TYPE_P (scope))
2952 return fns;
2954 cl = currently_open_derived_class (scope);
2955 if (!cl)
2956 cl = scope;
2957 cl = TYPE_BINFO (cl);
2958 return build_baselink (cl, cl, fns, /*optype=*/NULL_TREE);
2961 /* Returns true iff DECL is a variable from a function outside
2962 the current one. */
2964 static bool
2965 outer_var_p (tree decl)
2967 return ((VAR_P (decl) || TREE_CODE (decl) == PARM_DECL)
2968 && DECL_FUNCTION_SCOPE_P (decl)
2969 && (DECL_CONTEXT (decl) != current_function_decl
2970 || parsing_nsdmi ()));
2973 /* As above, but also checks that DECL is automatic. */
2975 static bool
2976 outer_automatic_var_p (tree decl)
2978 return (outer_var_p (decl)
2979 && !TREE_STATIC (decl));
2982 /* ID_EXPRESSION is a representation of parsed, but unprocessed,
2983 id-expression. (See cp_parser_id_expression for details.) SCOPE,
2984 if non-NULL, is the type or namespace used to explicitly qualify
2985 ID_EXPRESSION. DECL is the entity to which that name has been
2986 resolved.
2988 *CONSTANT_EXPRESSION_P is true if we are presently parsing a
2989 constant-expression. In that case, *NON_CONSTANT_EXPRESSION_P will
2990 be set to true if this expression isn't permitted in a
2991 constant-expression, but it is otherwise not set by this function.
2992 *ALLOW_NON_CONSTANT_EXPRESSION_P is true if we are parsing a
2993 constant-expression, but a non-constant expression is also
2994 permissible.
2996 DONE is true if this expression is a complete postfix-expression;
2997 it is false if this expression is followed by '->', '[', '(', etc.
2998 ADDRESS_P is true iff this expression is the operand of '&'.
2999 TEMPLATE_P is true iff the qualified-id was of the form
3000 "A::template B". TEMPLATE_ARG_P is true iff this qualified name
3001 appears as a template argument.
3003 If an error occurs, and it is the kind of error that might cause
3004 the parser to abort a tentative parse, *ERROR_MSG is filled in. It
3005 is the caller's responsibility to issue the message. *ERROR_MSG
3006 will be a string with static storage duration, so the caller need
3007 not "free" it.
3009 Return an expression for the entity, after issuing appropriate
3010 diagnostics. This function is also responsible for transforming a
3011 reference to a non-static member into a COMPONENT_REF that makes
3012 the use of "this" explicit.
3014 Upon return, *IDK will be filled in appropriately. */
3015 tree
3016 finish_id_expression (tree id_expression,
3017 tree decl,
3018 tree scope,
3019 cp_id_kind *idk,
3020 bool integral_constant_expression_p,
3021 bool allow_non_integral_constant_expression_p,
3022 bool *non_integral_constant_expression_p,
3023 bool template_p,
3024 bool done,
3025 bool address_p,
3026 bool template_arg_p,
3027 const char **error_msg,
3028 location_t location)
3030 decl = strip_using_decl (decl);
3032 /* Initialize the output parameters. */
3033 *idk = CP_ID_KIND_NONE;
3034 *error_msg = NULL;
3036 if (id_expression == error_mark_node)
3037 return error_mark_node;
3038 /* If we have a template-id, then no further lookup is
3039 required. If the template-id was for a template-class, we
3040 will sometimes have a TYPE_DECL at this point. */
3041 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3042 || TREE_CODE (decl) == TYPE_DECL)
3044 /* Look up the name. */
3045 else
3047 if (decl == error_mark_node)
3049 /* Name lookup failed. */
3050 if (scope
3051 && (!TYPE_P (scope)
3052 || (!dependent_type_p (scope)
3053 && !(identifier_p (id_expression)
3054 && IDENTIFIER_TYPENAME_P (id_expression)
3055 && dependent_type_p (TREE_TYPE (id_expression))))))
3057 /* If the qualifying type is non-dependent (and the name
3058 does not name a conversion operator to a dependent
3059 type), issue an error. */
3060 qualified_name_lookup_error (scope, id_expression, decl, location);
3061 return error_mark_node;
3063 else if (!scope)
3065 /* It may be resolved via Koenig lookup. */
3066 *idk = CP_ID_KIND_UNQUALIFIED;
3067 return id_expression;
3069 else
3070 decl = id_expression;
3072 /* If DECL is a variable that would be out of scope under
3073 ANSI/ISO rules, but in scope in the ARM, name lookup
3074 will succeed. Issue a diagnostic here. */
3075 else
3076 decl = check_for_out_of_scope_variable (decl);
3078 /* Remember that the name was used in the definition of
3079 the current class so that we can check later to see if
3080 the meaning would have been different after the class
3081 was entirely defined. */
3082 if (!scope && decl != error_mark_node && identifier_p (id_expression))
3083 maybe_note_name_used_in_class (id_expression, decl);
3085 /* Disallow uses of local variables from containing functions, except
3086 within lambda-expressions. */
3087 if (!outer_var_p (decl))
3088 /* OK */;
3089 else if (TREE_STATIC (decl)
3090 /* It's not a use (3.2) if we're in an unevaluated context. */
3091 || cp_unevaluated_operand)
3093 if (processing_template_decl)
3094 /* For a use of an outer static/unevaluated var, return the id
3095 so that we'll look it up again in the instantiation. */
3096 return id_expression;
3098 else
3100 tree context = DECL_CONTEXT (decl);
3101 tree containing_function = current_function_decl;
3102 tree lambda_stack = NULL_TREE;
3103 tree lambda_expr = NULL_TREE;
3104 tree initializer = convert_from_reference (decl);
3106 /* Mark it as used now even if the use is ill-formed. */
3107 mark_used (decl);
3109 /* Core issue 696: "[At the July 2009 meeting] the CWG expressed
3110 support for an approach in which a reference to a local
3111 [constant] automatic variable in a nested class or lambda body
3112 would enter the expression as an rvalue, which would reduce
3113 the complexity of the problem"
3115 FIXME update for final resolution of core issue 696. */
3116 if (decl_constant_var_p (decl))
3118 if (processing_template_decl)
3119 /* In a template, the constant value may not be in a usable
3120 form, so look it up again at instantiation time. */
3121 return id_expression;
3122 else
3123 return integral_constant_value (decl);
3126 if (parsing_nsdmi ())
3127 containing_function = NULL_TREE;
3128 /* If we are in a lambda function, we can move out until we hit
3129 1. the context,
3130 2. a non-lambda function, or
3131 3. a non-default capturing lambda function. */
3132 else while (context != containing_function
3133 && LAMBDA_FUNCTION_P (containing_function))
3135 lambda_expr = CLASSTYPE_LAMBDA_EXPR
3136 (DECL_CONTEXT (containing_function));
3138 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr)
3139 == CPLD_NONE)
3140 break;
3142 lambda_stack = tree_cons (NULL_TREE,
3143 lambda_expr,
3144 lambda_stack);
3146 containing_function
3147 = decl_function_context (containing_function);
3150 if (lambda_expr && TREE_CODE (decl) == VAR_DECL
3151 && DECL_ANON_UNION_VAR_P (decl))
3153 error ("cannot capture member %qD of anonymous union", decl);
3154 return error_mark_node;
3156 if (context == containing_function)
3158 decl = add_default_capture (lambda_stack,
3159 /*id=*/DECL_NAME (decl),
3160 initializer);
3162 else if (lambda_expr)
3164 error ("%qD is not captured", decl);
3165 return error_mark_node;
3167 else
3169 error (VAR_P (decl)
3170 ? G_("use of local variable with automatic storage from containing function")
3171 : G_("use of parameter from containing function"));
3172 inform (input_location, "%q+#D declared here", decl);
3173 return error_mark_node;
3177 /* Also disallow uses of function parameters outside the function
3178 body, except inside an unevaluated context (i.e. decltype). */
3179 if (TREE_CODE (decl) == PARM_DECL
3180 && DECL_CONTEXT (decl) == NULL_TREE
3181 && !cp_unevaluated_operand)
3183 error ("use of parameter %qD outside function body", decl);
3184 return error_mark_node;
3188 /* If we didn't find anything, or what we found was a type,
3189 then this wasn't really an id-expression. */
3190 if (TREE_CODE (decl) == TEMPLATE_DECL
3191 && !DECL_FUNCTION_TEMPLATE_P (decl))
3193 *error_msg = "missing template arguments";
3194 return error_mark_node;
3196 else if (TREE_CODE (decl) == TYPE_DECL
3197 || TREE_CODE (decl) == NAMESPACE_DECL)
3199 *error_msg = "expected primary-expression";
3200 return error_mark_node;
3203 /* If the name resolved to a template parameter, there is no
3204 need to look it up again later. */
3205 if ((TREE_CODE (decl) == CONST_DECL && DECL_TEMPLATE_PARM_P (decl))
3206 || TREE_CODE (decl) == TEMPLATE_PARM_INDEX)
3208 tree r;
3210 *idk = CP_ID_KIND_NONE;
3211 if (TREE_CODE (decl) == TEMPLATE_PARM_INDEX)
3212 decl = TEMPLATE_PARM_DECL (decl);
3213 r = convert_from_reference (DECL_INITIAL (decl));
3215 if (integral_constant_expression_p
3216 && !dependent_type_p (TREE_TYPE (decl))
3217 && !(INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (r))))
3219 if (!allow_non_integral_constant_expression_p)
3220 error ("template parameter %qD of type %qT is not allowed in "
3221 "an integral constant expression because it is not of "
3222 "integral or enumeration type", decl, TREE_TYPE (decl));
3223 *non_integral_constant_expression_p = true;
3225 return r;
3227 else
3229 bool dependent_p;
3231 /* If the declaration was explicitly qualified indicate
3232 that. The semantics of `A::f(3)' are different than
3233 `f(3)' if `f' is virtual. */
3234 *idk = (scope
3235 ? CP_ID_KIND_QUALIFIED
3236 : (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3237 ? CP_ID_KIND_TEMPLATE_ID
3238 : CP_ID_KIND_UNQUALIFIED));
3241 /* [temp.dep.expr]
3243 An id-expression is type-dependent if it contains an
3244 identifier that was declared with a dependent type.
3246 The standard is not very specific about an id-expression that
3247 names a set of overloaded functions. What if some of them
3248 have dependent types and some of them do not? Presumably,
3249 such a name should be treated as a dependent name. */
3250 /* Assume the name is not dependent. */
3251 dependent_p = false;
3252 if (!processing_template_decl)
3253 /* No names are dependent outside a template. */
3255 else if (TREE_CODE (decl) == CONST_DECL)
3256 /* We don't want to treat enumerators as dependent. */
3258 /* A template-id where the name of the template was not resolved
3259 is definitely dependent. */
3260 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3261 && (identifier_p (TREE_OPERAND (decl, 0))))
3262 dependent_p = true;
3263 /* For anything except an overloaded function, just check its
3264 type. */
3265 else if (!is_overloaded_fn (decl))
3266 dependent_p
3267 = dependent_type_p (TREE_TYPE (decl));
3268 /* For a set of overloaded functions, check each of the
3269 functions. */
3270 else
3272 tree fns = decl;
3274 if (BASELINK_P (fns))
3275 fns = BASELINK_FUNCTIONS (fns);
3277 /* For a template-id, check to see if the template
3278 arguments are dependent. */
3279 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
3281 tree args = TREE_OPERAND (fns, 1);
3282 dependent_p = any_dependent_template_arguments_p (args);
3283 /* The functions are those referred to by the
3284 template-id. */
3285 fns = TREE_OPERAND (fns, 0);
3288 /* If there are no dependent template arguments, go through
3289 the overloaded functions. */
3290 while (fns && !dependent_p)
3292 tree fn = OVL_CURRENT (fns);
3294 /* Member functions of dependent classes are
3295 dependent. */
3296 if (TREE_CODE (fn) == FUNCTION_DECL
3297 && type_dependent_expression_p (fn))
3298 dependent_p = true;
3299 else if (TREE_CODE (fn) == TEMPLATE_DECL
3300 && dependent_template_p (fn))
3301 dependent_p = true;
3303 fns = OVL_NEXT (fns);
3307 /* If the name was dependent on a template parameter, we will
3308 resolve the name at instantiation time. */
3309 if (dependent_p)
3311 /* Create a SCOPE_REF for qualified names, if the scope is
3312 dependent. */
3313 if (scope)
3315 if (TYPE_P (scope))
3317 if (address_p && done)
3318 decl = finish_qualified_id_expr (scope, decl,
3319 done, address_p,
3320 template_p,
3321 template_arg_p,
3322 tf_warning_or_error);
3323 else
3325 tree type = NULL_TREE;
3326 if (DECL_P (decl) && !dependent_scope_p (scope))
3327 type = TREE_TYPE (decl);
3328 decl = build_qualified_name (type,
3329 scope,
3330 id_expression,
3331 template_p);
3334 if (TREE_TYPE (decl))
3335 decl = convert_from_reference (decl);
3336 return decl;
3338 /* A TEMPLATE_ID already contains all the information we
3339 need. */
3340 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR)
3341 return id_expression;
3342 *idk = CP_ID_KIND_UNQUALIFIED_DEPENDENT;
3343 /* If we found a variable, then name lookup during the
3344 instantiation will always resolve to the same VAR_DECL
3345 (or an instantiation thereof). */
3346 if (VAR_P (decl)
3347 || TREE_CODE (decl) == PARM_DECL)
3349 mark_used (decl);
3350 return convert_from_reference (decl);
3352 /* The same is true for FIELD_DECL, but we also need to
3353 make sure that the syntax is correct. */
3354 else if (TREE_CODE (decl) == FIELD_DECL)
3356 /* Since SCOPE is NULL here, this is an unqualified name.
3357 Access checking has been performed during name lookup
3358 already. Turn off checking to avoid duplicate errors. */
3359 push_deferring_access_checks (dk_no_check);
3360 decl = finish_non_static_data_member
3361 (decl, NULL_TREE,
3362 /*qualifying_scope=*/NULL_TREE);
3363 pop_deferring_access_checks ();
3364 return decl;
3366 return id_expression;
3369 if (TREE_CODE (decl) == NAMESPACE_DECL)
3371 error ("use of namespace %qD as expression", decl);
3372 return error_mark_node;
3374 else if (DECL_CLASS_TEMPLATE_P (decl))
3376 error ("use of class template %qT as expression", decl);
3377 return error_mark_node;
3379 else if (TREE_CODE (decl) == TREE_LIST)
3381 /* Ambiguous reference to base members. */
3382 error ("request for member %qD is ambiguous in "
3383 "multiple inheritance lattice", id_expression);
3384 print_candidates (decl);
3385 return error_mark_node;
3388 /* Mark variable-like entities as used. Functions are similarly
3389 marked either below or after overload resolution. */
3390 if ((VAR_P (decl)
3391 || TREE_CODE (decl) == PARM_DECL
3392 || TREE_CODE (decl) == CONST_DECL
3393 || TREE_CODE (decl) == RESULT_DECL)
3394 && !mark_used (decl))
3395 return error_mark_node;
3397 /* Only certain kinds of names are allowed in constant
3398 expression. Template parameters have already
3399 been handled above. */
3400 if (! error_operand_p (decl)
3401 && integral_constant_expression_p
3402 && ! decl_constant_var_p (decl)
3403 && TREE_CODE (decl) != CONST_DECL
3404 && ! builtin_valid_in_constant_expr_p (decl))
3406 if (!allow_non_integral_constant_expression_p)
3408 error ("%qD cannot appear in a constant-expression", decl);
3409 return error_mark_node;
3411 *non_integral_constant_expression_p = true;
3414 tree wrap;
3415 if (VAR_P (decl)
3416 && !cp_unevaluated_operand
3417 && DECL_THREAD_LOCAL_P (decl)
3418 && (wrap = get_tls_wrapper_fn (decl)))
3420 /* Replace an evaluated use of the thread_local variable with
3421 a call to its wrapper. */
3422 decl = build_cxx_call (wrap, 0, NULL, tf_warning_or_error);
3424 else if (scope)
3426 decl = (adjust_result_of_qualified_name_lookup
3427 (decl, scope, current_nonlambda_class_type()));
3429 if (TREE_CODE (decl) == FUNCTION_DECL)
3430 mark_used (decl);
3432 if (TYPE_P (scope))
3433 decl = finish_qualified_id_expr (scope,
3434 decl,
3435 done,
3436 address_p,
3437 template_p,
3438 template_arg_p,
3439 tf_warning_or_error);
3440 else
3441 decl = convert_from_reference (decl);
3443 else if (TREE_CODE (decl) == FIELD_DECL)
3445 /* Since SCOPE is NULL here, this is an unqualified name.
3446 Access checking has been performed during name lookup
3447 already. Turn off checking to avoid duplicate errors. */
3448 push_deferring_access_checks (dk_no_check);
3449 decl = finish_non_static_data_member (decl, NULL_TREE,
3450 /*qualifying_scope=*/NULL_TREE);
3451 pop_deferring_access_checks ();
3453 else if (is_overloaded_fn (decl))
3455 tree first_fn;
3457 first_fn = get_first_fn (decl);
3458 if (TREE_CODE (first_fn) == TEMPLATE_DECL)
3459 first_fn = DECL_TEMPLATE_RESULT (first_fn);
3461 if (!really_overloaded_fn (decl)
3462 && !mark_used (first_fn))
3463 return error_mark_node;
3465 if (!template_arg_p
3466 && TREE_CODE (first_fn) == FUNCTION_DECL
3467 && DECL_FUNCTION_MEMBER_P (first_fn)
3468 && !shared_member_p (decl))
3470 /* A set of member functions. */
3471 decl = maybe_dummy_object (DECL_CONTEXT (first_fn), 0);
3472 return finish_class_member_access_expr (decl, id_expression,
3473 /*template_p=*/false,
3474 tf_warning_or_error);
3477 decl = baselink_for_fns (decl);
3479 else
3481 if (DECL_P (decl) && DECL_NONLOCAL (decl)
3482 && DECL_CLASS_SCOPE_P (decl))
3484 tree context = context_for_name_lookup (decl);
3485 if (context != current_class_type)
3487 tree path = currently_open_derived_class (context);
3488 perform_or_defer_access_check (TYPE_BINFO (path),
3489 decl, decl,
3490 tf_warning_or_error);
3494 decl = convert_from_reference (decl);
3498 /* Handle references (c++/56130). */
3499 tree t = REFERENCE_REF_P (decl) ? TREE_OPERAND (decl, 0) : decl;
3500 if (TREE_DEPRECATED (t))
3501 warn_deprecated_use (t, NULL_TREE);
3503 return decl;
3506 /* Implement the __typeof keyword: Return the type of EXPR, suitable for
3507 use as a type-specifier. */
3509 tree
3510 finish_typeof (tree expr)
3512 tree type;
3514 if (type_dependent_expression_p (expr))
3516 type = cxx_make_type (TYPEOF_TYPE);
3517 TYPEOF_TYPE_EXPR (type) = expr;
3518 SET_TYPE_STRUCTURAL_EQUALITY (type);
3520 return type;
3523 expr = mark_type_use (expr);
3525 type = unlowered_expr_type (expr);
3527 if (!type || type == unknown_type_node)
3529 error ("type of %qE is unknown", expr);
3530 return error_mark_node;
3533 return type;
3536 /* Implement the __underlying_type keyword: Return the underlying
3537 type of TYPE, suitable for use as a type-specifier. */
3539 tree
3540 finish_underlying_type (tree type)
3542 tree underlying_type;
3544 if (processing_template_decl)
3546 underlying_type = cxx_make_type (UNDERLYING_TYPE);
3547 UNDERLYING_TYPE_TYPE (underlying_type) = type;
3548 SET_TYPE_STRUCTURAL_EQUALITY (underlying_type);
3550 return underlying_type;
3553 complete_type (type);
3555 if (TREE_CODE (type) != ENUMERAL_TYPE)
3557 error ("%qT is not an enumeration type", type);
3558 return error_mark_node;
3561 underlying_type = ENUM_UNDERLYING_TYPE (type);
3563 /* Fixup necessary in this case because ENUM_UNDERLYING_TYPE
3564 includes TYPE_MIN_VALUE and TYPE_MAX_VALUE information.
3565 See finish_enum_value_list for details. */
3566 if (!ENUM_FIXED_UNDERLYING_TYPE_P (type))
3567 underlying_type
3568 = c_common_type_for_mode (TYPE_MODE (underlying_type),
3569 TYPE_UNSIGNED (underlying_type));
3571 return underlying_type;
3574 /* Implement the __direct_bases keyword: Return the direct base classes
3575 of type */
3577 tree
3578 calculate_direct_bases (tree type)
3580 vec<tree, va_gc> *vector = make_tree_vector();
3581 tree bases_vec = NULL_TREE;
3582 vec<tree, va_gc> *base_binfos;
3583 tree binfo;
3584 unsigned i;
3586 complete_type (type);
3588 if (!NON_UNION_CLASS_TYPE_P (type))
3589 return make_tree_vec (0);
3591 base_binfos = BINFO_BASE_BINFOS (TYPE_BINFO (type));
3593 /* Virtual bases are initialized first */
3594 for (i = 0; base_binfos->iterate (i, &binfo); i++)
3596 if (BINFO_VIRTUAL_P (binfo))
3598 vec_safe_push (vector, binfo);
3602 /* Now non-virtuals */
3603 for (i = 0; base_binfos->iterate (i, &binfo); i++)
3605 if (!BINFO_VIRTUAL_P (binfo))
3607 vec_safe_push (vector, binfo);
3612 bases_vec = make_tree_vec (vector->length ());
3614 for (i = 0; i < vector->length (); ++i)
3616 TREE_VEC_ELT (bases_vec, i) = BINFO_TYPE ((*vector)[i]);
3618 return bases_vec;
3621 /* Implement the __bases keyword: Return the base classes
3622 of type */
3624 /* Find morally non-virtual base classes by walking binfo hierarchy */
3625 /* Virtual base classes are handled separately in finish_bases */
3627 static tree
3628 dfs_calculate_bases_pre (tree binfo, void * /*data_*/)
3630 /* Don't walk bases of virtual bases */
3631 return BINFO_VIRTUAL_P (binfo) ? dfs_skip_bases : NULL_TREE;
3634 static tree
3635 dfs_calculate_bases_post (tree binfo, void *data_)
3637 vec<tree, va_gc> **data = ((vec<tree, va_gc> **) data_);
3638 if (!BINFO_VIRTUAL_P (binfo))
3640 vec_safe_push (*data, BINFO_TYPE (binfo));
3642 return NULL_TREE;
3645 /* Calculates the morally non-virtual base classes of a class */
3646 static vec<tree, va_gc> *
3647 calculate_bases_helper (tree type)
3649 vec<tree, va_gc> *vector = make_tree_vector();
3651 /* Now add non-virtual base classes in order of construction */
3652 dfs_walk_all (TYPE_BINFO (type),
3653 dfs_calculate_bases_pre, dfs_calculate_bases_post, &vector);
3654 return vector;
3657 tree
3658 calculate_bases (tree type)
3660 vec<tree, va_gc> *vector = make_tree_vector();
3661 tree bases_vec = NULL_TREE;
3662 unsigned i;
3663 vec<tree, va_gc> *vbases;
3664 vec<tree, va_gc> *nonvbases;
3665 tree binfo;
3667 complete_type (type);
3669 if (!NON_UNION_CLASS_TYPE_P (type))
3670 return make_tree_vec (0);
3672 /* First go through virtual base classes */
3673 for (vbases = CLASSTYPE_VBASECLASSES (type), i = 0;
3674 vec_safe_iterate (vbases, i, &binfo); i++)
3676 vec<tree, va_gc> *vbase_bases;
3677 vbase_bases = calculate_bases_helper (BINFO_TYPE (binfo));
3678 vec_safe_splice (vector, vbase_bases);
3679 release_tree_vector (vbase_bases);
3682 /* Now for the non-virtual bases */
3683 nonvbases = calculate_bases_helper (type);
3684 vec_safe_splice (vector, nonvbases);
3685 release_tree_vector (nonvbases);
3687 /* Last element is entire class, so don't copy */
3688 bases_vec = make_tree_vec (vector->length () - 1);
3690 for (i = 0; i < vector->length () - 1; ++i)
3692 TREE_VEC_ELT (bases_vec, i) = (*vector)[i];
3694 release_tree_vector (vector);
3695 return bases_vec;
3698 tree
3699 finish_bases (tree type, bool direct)
3701 tree bases = NULL_TREE;
3703 if (!processing_template_decl)
3705 /* Parameter packs can only be used in templates */
3706 error ("Parameter pack __bases only valid in template declaration");
3707 return error_mark_node;
3710 bases = cxx_make_type (BASES);
3711 BASES_TYPE (bases) = type;
3712 BASES_DIRECT (bases) = direct;
3713 SET_TYPE_STRUCTURAL_EQUALITY (bases);
3715 return bases;
3718 /* Perform C++-specific checks for __builtin_offsetof before calling
3719 fold_offsetof. */
3721 tree
3722 finish_offsetof (tree expr)
3724 if (TREE_CODE (expr) == PSEUDO_DTOR_EXPR)
3726 error ("cannot apply %<offsetof%> to destructor %<~%T%>",
3727 TREE_OPERAND (expr, 2));
3728 return error_mark_node;
3730 if (TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE
3731 || TREE_CODE (TREE_TYPE (expr)) == METHOD_TYPE
3732 || TREE_TYPE (expr) == unknown_type_node)
3734 if (INDIRECT_REF_P (expr))
3735 error ("second operand of %<offsetof%> is neither a single "
3736 "identifier nor a sequence of member accesses and "
3737 "array references");
3738 else
3740 if (TREE_CODE (expr) == COMPONENT_REF
3741 || TREE_CODE (expr) == COMPOUND_EXPR)
3742 expr = TREE_OPERAND (expr, 1);
3743 error ("cannot apply %<offsetof%> to member function %qD", expr);
3745 return error_mark_node;
3747 if (REFERENCE_REF_P (expr))
3748 expr = TREE_OPERAND (expr, 0);
3749 if (TREE_CODE (expr) == COMPONENT_REF)
3751 tree object = TREE_OPERAND (expr, 0);
3752 if (!complete_type_or_else (TREE_TYPE (object), object))
3753 return error_mark_node;
3755 return fold_offsetof (expr);
3758 /* Replace the AGGR_INIT_EXPR at *TP with an equivalent CALL_EXPR. This
3759 function is broken out from the above for the benefit of the tree-ssa
3760 project. */
3762 void
3763 simplify_aggr_init_expr (tree *tp)
3765 tree aggr_init_expr = *tp;
3767 /* Form an appropriate CALL_EXPR. */
3768 tree fn = AGGR_INIT_EXPR_FN (aggr_init_expr);
3769 tree slot = AGGR_INIT_EXPR_SLOT (aggr_init_expr);
3770 tree type = TREE_TYPE (slot);
3772 tree call_expr;
3773 enum style_t { ctor, arg, pcc } style;
3775 if (AGGR_INIT_VIA_CTOR_P (aggr_init_expr))
3776 style = ctor;
3777 #ifdef PCC_STATIC_STRUCT_RETURN
3778 else if (1)
3779 style = pcc;
3780 #endif
3781 else
3783 gcc_assert (TREE_ADDRESSABLE (type));
3784 style = arg;
3787 call_expr = build_call_array_loc (input_location,
3788 TREE_TYPE (TREE_TYPE (TREE_TYPE (fn))),
3790 aggr_init_expr_nargs (aggr_init_expr),
3791 AGGR_INIT_EXPR_ARGP (aggr_init_expr));
3792 TREE_NOTHROW (call_expr) = TREE_NOTHROW (aggr_init_expr);
3794 if (style == ctor)
3796 /* Replace the first argument to the ctor with the address of the
3797 slot. */
3798 cxx_mark_addressable (slot);
3799 CALL_EXPR_ARG (call_expr, 0) =
3800 build1 (ADDR_EXPR, build_pointer_type (type), slot);
3802 else if (style == arg)
3804 /* Just mark it addressable here, and leave the rest to
3805 expand_call{,_inline}. */
3806 cxx_mark_addressable (slot);
3807 CALL_EXPR_RETURN_SLOT_OPT (call_expr) = true;
3808 call_expr = build2 (INIT_EXPR, TREE_TYPE (call_expr), slot, call_expr);
3810 else if (style == pcc)
3812 /* If we're using the non-reentrant PCC calling convention, then we
3813 need to copy the returned value out of the static buffer into the
3814 SLOT. */
3815 push_deferring_access_checks (dk_no_check);
3816 call_expr = build_aggr_init (slot, call_expr,
3817 DIRECT_BIND | LOOKUP_ONLYCONVERTING,
3818 tf_warning_or_error);
3819 pop_deferring_access_checks ();
3820 call_expr = build2 (COMPOUND_EXPR, TREE_TYPE (slot), call_expr, slot);
3823 if (AGGR_INIT_ZERO_FIRST (aggr_init_expr))
3825 tree init = build_zero_init (type, NULL_TREE,
3826 /*static_storage_p=*/false);
3827 init = build2 (INIT_EXPR, void_type_node, slot, init);
3828 call_expr = build2 (COMPOUND_EXPR, TREE_TYPE (call_expr),
3829 init, call_expr);
3832 *tp = call_expr;
3835 /* Emit all thunks to FN that should be emitted when FN is emitted. */
3837 void
3838 emit_associated_thunks (tree fn)
3840 /* When we use vcall offsets, we emit thunks with the virtual
3841 functions to which they thunk. The whole point of vcall offsets
3842 is so that you can know statically the entire set of thunks that
3843 will ever be needed for a given virtual function, thereby
3844 enabling you to output all the thunks with the function itself. */
3845 if (DECL_VIRTUAL_P (fn)
3846 /* Do not emit thunks for extern template instantiations. */
3847 && ! DECL_REALLY_EXTERN (fn))
3849 tree thunk;
3851 for (thunk = DECL_THUNKS (fn); thunk; thunk = DECL_CHAIN (thunk))
3853 if (!THUNK_ALIAS (thunk))
3855 use_thunk (thunk, /*emit_p=*/1);
3856 if (DECL_RESULT_THUNK_P (thunk))
3858 tree probe;
3860 for (probe = DECL_THUNKS (thunk);
3861 probe; probe = DECL_CHAIN (probe))
3862 use_thunk (probe, /*emit_p=*/1);
3865 else
3866 gcc_assert (!DECL_THUNKS (thunk));
3871 /* Returns true iff FUN is an instantiation of a constexpr function
3872 template. */
3874 static inline bool
3875 is_instantiation_of_constexpr (tree fun)
3877 return (DECL_TEMPLOID_INSTANTIATION (fun)
3878 && DECL_DECLARED_CONSTEXPR_P (DECL_TEMPLATE_RESULT
3879 (DECL_TI_TEMPLATE (fun))));
3882 /* Generate RTL for FN. */
3884 bool
3885 expand_or_defer_fn_1 (tree fn)
3887 /* When the parser calls us after finishing the body of a template
3888 function, we don't really want to expand the body. */
3889 if (processing_template_decl)
3891 /* Normally, collection only occurs in rest_of_compilation. So,
3892 if we don't collect here, we never collect junk generated
3893 during the processing of templates until we hit a
3894 non-template function. It's not safe to do this inside a
3895 nested class, though, as the parser may have local state that
3896 is not a GC root. */
3897 if (!function_depth)
3898 ggc_collect ();
3899 return false;
3902 gcc_assert (DECL_SAVED_TREE (fn));
3904 /* If this is a constructor or destructor body, we have to clone
3905 it. */
3906 if (maybe_clone_body (fn))
3908 /* We don't want to process FN again, so pretend we've written
3909 it out, even though we haven't. */
3910 TREE_ASM_WRITTEN (fn) = 1;
3911 /* If this is an instantiation of a constexpr function, keep
3912 DECL_SAVED_TREE for explain_invalid_constexpr_fn. */
3913 if (!is_instantiation_of_constexpr (fn))
3914 DECL_SAVED_TREE (fn) = NULL_TREE;
3915 return false;
3918 /* We make a decision about linkage for these functions at the end
3919 of the compilation. Until that point, we do not want the back
3920 end to output them -- but we do want it to see the bodies of
3921 these functions so that it can inline them as appropriate. */
3922 if (DECL_DECLARED_INLINE_P (fn) || DECL_IMPLICIT_INSTANTIATION (fn))
3924 if (DECL_INTERFACE_KNOWN (fn))
3925 /* We've already made a decision as to how this function will
3926 be handled. */;
3927 else if (!at_eof)
3929 DECL_EXTERNAL (fn) = 1;
3930 DECL_NOT_REALLY_EXTERN (fn) = 1;
3931 note_vague_linkage_fn (fn);
3932 /* A non-template inline function with external linkage will
3933 always be COMDAT. As we must eventually determine the
3934 linkage of all functions, and as that causes writes to
3935 the data mapped in from the PCH file, it's advantageous
3936 to mark the functions at this point. */
3937 if (!DECL_IMPLICIT_INSTANTIATION (fn))
3939 /* This function must have external linkage, as
3940 otherwise DECL_INTERFACE_KNOWN would have been
3941 set. */
3942 gcc_assert (TREE_PUBLIC (fn));
3943 comdat_linkage (fn);
3944 DECL_INTERFACE_KNOWN (fn) = 1;
3947 else
3948 import_export_decl (fn);
3950 /* If the user wants us to keep all inline functions, then mark
3951 this function as needed so that finish_file will make sure to
3952 output it later. Similarly, all dllexport'd functions must
3953 be emitted; there may be callers in other DLLs. */
3954 if ((flag_keep_inline_functions
3955 && DECL_DECLARED_INLINE_P (fn)
3956 && !DECL_REALLY_EXTERN (fn))
3957 || (flag_keep_inline_dllexport
3958 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (fn))))
3960 mark_needed (fn);
3961 DECL_EXTERNAL (fn) = 0;
3965 /* There's no reason to do any of the work here if we're only doing
3966 semantic analysis; this code just generates RTL. */
3967 if (flag_syntax_only)
3968 return false;
3970 return true;
3973 void
3974 expand_or_defer_fn (tree fn)
3976 if (expand_or_defer_fn_1 (fn))
3978 function_depth++;
3980 /* Expand or defer, at the whim of the compilation unit manager. */
3981 cgraph_finalize_function (fn, function_depth > 1);
3982 emit_associated_thunks (fn);
3984 function_depth--;
3988 struct nrv_data
3990 tree var;
3991 tree result;
3992 hash_table <pointer_hash <tree_node> > visited;
3995 /* Helper function for walk_tree, used by finalize_nrv below. */
3997 static tree
3998 finalize_nrv_r (tree* tp, int* walk_subtrees, void* data)
4000 struct nrv_data *dp = (struct nrv_data *)data;
4001 tree_node **slot;
4003 /* No need to walk into types. There wouldn't be any need to walk into
4004 non-statements, except that we have to consider STMT_EXPRs. */
4005 if (TYPE_P (*tp))
4006 *walk_subtrees = 0;
4007 /* Change all returns to just refer to the RESULT_DECL; this is a nop,
4008 but differs from using NULL_TREE in that it indicates that we care
4009 about the value of the RESULT_DECL. */
4010 else if (TREE_CODE (*tp) == RETURN_EXPR)
4011 TREE_OPERAND (*tp, 0) = dp->result;
4012 /* Change all cleanups for the NRV to only run when an exception is
4013 thrown. */
4014 else if (TREE_CODE (*tp) == CLEANUP_STMT
4015 && CLEANUP_DECL (*tp) == dp->var)
4016 CLEANUP_EH_ONLY (*tp) = 1;
4017 /* Replace the DECL_EXPR for the NRV with an initialization of the
4018 RESULT_DECL, if needed. */
4019 else if (TREE_CODE (*tp) == DECL_EXPR
4020 && DECL_EXPR_DECL (*tp) == dp->var)
4022 tree init;
4023 if (DECL_INITIAL (dp->var)
4024 && DECL_INITIAL (dp->var) != error_mark_node)
4025 init = build2 (INIT_EXPR, void_type_node, dp->result,
4026 DECL_INITIAL (dp->var));
4027 else
4028 init = build_empty_stmt (EXPR_LOCATION (*tp));
4029 DECL_INITIAL (dp->var) = NULL_TREE;
4030 SET_EXPR_LOCATION (init, EXPR_LOCATION (*tp));
4031 *tp = init;
4033 /* And replace all uses of the NRV with the RESULT_DECL. */
4034 else if (*tp == dp->var)
4035 *tp = dp->result;
4037 /* Avoid walking into the same tree more than once. Unfortunately, we
4038 can't just use walk_tree_without duplicates because it would only call
4039 us for the first occurrence of dp->var in the function body. */
4040 slot = dp->visited.find_slot (*tp, INSERT);
4041 if (*slot)
4042 *walk_subtrees = 0;
4043 else
4044 *slot = *tp;
4046 /* Keep iterating. */
4047 return NULL_TREE;
4050 /* Called from finish_function to implement the named return value
4051 optimization by overriding all the RETURN_EXPRs and pertinent
4052 CLEANUP_STMTs and replacing all occurrences of VAR with RESULT, the
4053 RESULT_DECL for the function. */
4055 void
4056 finalize_nrv (tree *tp, tree var, tree result)
4058 struct nrv_data data;
4060 /* Copy name from VAR to RESULT. */
4061 DECL_NAME (result) = DECL_NAME (var);
4062 /* Don't forget that we take its address. */
4063 TREE_ADDRESSABLE (result) = TREE_ADDRESSABLE (var);
4064 /* Finally set DECL_VALUE_EXPR to avoid assigning
4065 a stack slot at -O0 for the original var and debug info
4066 uses RESULT location for VAR. */
4067 SET_DECL_VALUE_EXPR (var, result);
4068 DECL_HAS_VALUE_EXPR_P (var) = 1;
4070 data.var = var;
4071 data.result = result;
4072 data.visited.create (37);
4073 cp_walk_tree (tp, finalize_nrv_r, &data, 0);
4074 data.visited.dispose ();
4077 /* Create CP_OMP_CLAUSE_INFO for clause C. Returns true if it is invalid. */
4079 bool
4080 cxx_omp_create_clause_info (tree c, tree type, bool need_default_ctor,
4081 bool need_copy_ctor, bool need_copy_assignment,
4082 bool need_dtor)
4084 int save_errorcount = errorcount;
4085 tree info, t;
4087 /* Always allocate 3 elements for simplicity. These are the
4088 function decls for the ctor, dtor, and assignment op.
4089 This layout is known to the three lang hooks,
4090 cxx_omp_clause_default_init, cxx_omp_clause_copy_init,
4091 and cxx_omp_clause_assign_op. */
4092 info = make_tree_vec (3);
4093 CP_OMP_CLAUSE_INFO (c) = info;
4095 if (need_default_ctor || need_copy_ctor)
4097 if (need_default_ctor)
4098 t = get_default_ctor (type);
4099 else
4100 t = get_copy_ctor (type, tf_warning_or_error);
4102 if (t && !trivial_fn_p (t))
4103 TREE_VEC_ELT (info, 0) = t;
4106 if (need_dtor && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
4107 TREE_VEC_ELT (info, 1) = get_dtor (type, tf_warning_or_error);
4109 if (need_copy_assignment)
4111 t = get_copy_assign (type);
4113 if (t && !trivial_fn_p (t))
4114 TREE_VEC_ELT (info, 2) = t;
4117 return errorcount != save_errorcount;
4120 /* Helper function for handle_omp_array_sections. Called recursively
4121 to handle multiple array-section-subscripts. C is the clause,
4122 T current expression (initially OMP_CLAUSE_DECL), which is either
4123 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
4124 expression if specified, TREE_VALUE length expression if specified,
4125 TREE_CHAIN is what it has been specified after, or some decl.
4126 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
4127 set to true if any of the array-section-subscript could have length
4128 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
4129 first array-section-subscript which is known not to have length
4130 of one. Given say:
4131 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
4132 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
4133 all are or may have length of 1, array-section-subscript [:2] is the
4134 first one knonwn not to have length 1. For array-section-subscript
4135 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
4136 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
4137 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
4138 case though, as some lengths could be zero. */
4140 static tree
4141 handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
4142 bool &maybe_zero_len, unsigned int &first_non_one)
4144 tree ret, low_bound, length, type;
4145 if (TREE_CODE (t) != TREE_LIST)
4147 if (error_operand_p (t))
4148 return error_mark_node;
4149 if (type_dependent_expression_p (t))
4150 return NULL_TREE;
4151 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
4153 if (processing_template_decl)
4154 return NULL_TREE;
4155 if (DECL_P (t))
4156 error_at (OMP_CLAUSE_LOCATION (c),
4157 "%qD is not a variable in %qs clause", t,
4158 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4159 else
4160 error_at (OMP_CLAUSE_LOCATION (c),
4161 "%qE is not a variable in %qs clause", t,
4162 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4163 return error_mark_node;
4165 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
4166 && TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
4168 error_at (OMP_CLAUSE_LOCATION (c),
4169 "%qD is threadprivate variable in %qs clause", t,
4170 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4171 return error_mark_node;
4173 t = convert_from_reference (t);
4174 return t;
4177 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
4178 maybe_zero_len, first_non_one);
4179 if (ret == error_mark_node || ret == NULL_TREE)
4180 return ret;
4182 type = TREE_TYPE (ret);
4183 low_bound = TREE_PURPOSE (t);
4184 length = TREE_VALUE (t);
4185 if ((low_bound && type_dependent_expression_p (low_bound))
4186 || (length && type_dependent_expression_p (length)))
4187 return NULL_TREE;
4189 if (low_bound == error_mark_node || length == error_mark_node)
4190 return error_mark_node;
4192 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
4194 error_at (OMP_CLAUSE_LOCATION (c),
4195 "low bound %qE of array section does not have integral type",
4196 low_bound);
4197 return error_mark_node;
4199 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
4201 error_at (OMP_CLAUSE_LOCATION (c),
4202 "length %qE of array section does not have integral type",
4203 length);
4204 return error_mark_node;
4206 if (low_bound
4207 && TREE_CODE (low_bound) == INTEGER_CST
4208 && TYPE_PRECISION (TREE_TYPE (low_bound))
4209 > TYPE_PRECISION (sizetype))
4210 low_bound = fold_convert (sizetype, low_bound);
4211 if (length
4212 && TREE_CODE (length) == INTEGER_CST
4213 && TYPE_PRECISION (TREE_TYPE (length))
4214 > TYPE_PRECISION (sizetype))
4215 length = fold_convert (sizetype, length);
4216 if (low_bound == NULL_TREE)
4217 low_bound = integer_zero_node;
4219 if (length != NULL_TREE)
4221 if (!integer_nonzerop (length))
4222 maybe_zero_len = true;
4223 if (first_non_one == types.length ()
4224 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
4225 first_non_one++;
4227 if (TREE_CODE (type) == ARRAY_TYPE)
4229 if (length == NULL_TREE
4230 && (TYPE_DOMAIN (type) == NULL_TREE
4231 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
4233 error_at (OMP_CLAUSE_LOCATION (c),
4234 "for unknown bound array type length expression must "
4235 "be specified");
4236 return error_mark_node;
4238 if (TREE_CODE (low_bound) == INTEGER_CST
4239 && tree_int_cst_sgn (low_bound) == -1)
4241 error_at (OMP_CLAUSE_LOCATION (c),
4242 "negative low bound in array section in %qs clause",
4243 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4244 return error_mark_node;
4246 if (length != NULL_TREE
4247 && TREE_CODE (length) == INTEGER_CST
4248 && tree_int_cst_sgn (length) == -1)
4250 error_at (OMP_CLAUSE_LOCATION (c),
4251 "negative length in array section in %qs clause",
4252 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4253 return error_mark_node;
4255 if (TYPE_DOMAIN (type)
4256 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
4257 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
4258 == INTEGER_CST)
4260 tree size = size_binop (PLUS_EXPR,
4261 TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
4262 size_one_node);
4263 if (TREE_CODE (low_bound) == INTEGER_CST)
4265 if (tree_int_cst_lt (size, low_bound))
4267 error_at (OMP_CLAUSE_LOCATION (c),
4268 "low bound %qE above array section size "
4269 "in %qs clause", low_bound,
4270 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4271 return error_mark_node;
4273 if (tree_int_cst_equal (size, low_bound))
4274 maybe_zero_len = true;
4275 else if (length == NULL_TREE
4276 && first_non_one == types.length ()
4277 && tree_int_cst_equal
4278 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
4279 low_bound))
4280 first_non_one++;
4282 else if (length == NULL_TREE)
4284 maybe_zero_len = true;
4285 if (first_non_one == types.length ())
4286 first_non_one++;
4288 if (length && TREE_CODE (length) == INTEGER_CST)
4290 if (tree_int_cst_lt (size, length))
4292 error_at (OMP_CLAUSE_LOCATION (c),
4293 "length %qE above array section size "
4294 "in %qs clause", length,
4295 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4296 return error_mark_node;
4298 if (TREE_CODE (low_bound) == INTEGER_CST)
4300 tree lbpluslen
4301 = size_binop (PLUS_EXPR,
4302 fold_convert (sizetype, low_bound),
4303 fold_convert (sizetype, length));
4304 if (TREE_CODE (lbpluslen) == INTEGER_CST
4305 && tree_int_cst_lt (size, lbpluslen))
4307 error_at (OMP_CLAUSE_LOCATION (c),
4308 "high bound %qE above array section size "
4309 "in %qs clause", lbpluslen,
4310 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4311 return error_mark_node;
4316 else if (length == NULL_TREE)
4318 maybe_zero_len = true;
4319 if (first_non_one == types.length ())
4320 first_non_one++;
4323 /* For [lb:] we will need to evaluate lb more than once. */
4324 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
4326 tree lb = cp_save_expr (low_bound);
4327 if (lb != low_bound)
4329 TREE_PURPOSE (t) = lb;
4330 low_bound = lb;
4334 else if (TREE_CODE (type) == POINTER_TYPE)
4336 if (length == NULL_TREE)
4338 error_at (OMP_CLAUSE_LOCATION (c),
4339 "for pointer type length expression must be specified");
4340 return error_mark_node;
4342 /* If there is a pointer type anywhere but in the very first
4343 array-section-subscript, the array section can't be contiguous. */
4344 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
4345 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
4347 error_at (OMP_CLAUSE_LOCATION (c),
4348 "array section is not contiguous in %qs clause",
4349 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4350 return error_mark_node;
4353 else
4355 error_at (OMP_CLAUSE_LOCATION (c),
4356 "%qE does not have pointer or array type", ret);
4357 return error_mark_node;
4359 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
4360 types.safe_push (TREE_TYPE (ret));
4361 /* We will need to evaluate lb more than once. */
4362 tree lb = cp_save_expr (low_bound);
4363 if (lb != low_bound)
4365 TREE_PURPOSE (t) = lb;
4366 low_bound = lb;
4368 ret = grok_array_decl (OMP_CLAUSE_LOCATION (c), ret, low_bound, false);
4369 return ret;
4372 /* Handle array sections for clause C. */
4374 static bool
4375 handle_omp_array_sections (tree c)
4377 bool maybe_zero_len = false;
4378 unsigned int first_non_one = 0;
4379 auto_vec<tree> types;
4380 tree first = handle_omp_array_sections_1 (c, OMP_CLAUSE_DECL (c), types,
4381 maybe_zero_len, first_non_one);
4382 if (first == error_mark_node)
4383 return true;
4384 if (first == NULL_TREE)
4385 return false;
4386 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
4388 tree t = OMP_CLAUSE_DECL (c);
4389 tree tem = NULL_TREE;
4390 if (processing_template_decl)
4391 return false;
4392 /* Need to evaluate side effects in the length expressions
4393 if any. */
4394 while (TREE_CODE (t) == TREE_LIST)
4396 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
4398 if (tem == NULL_TREE)
4399 tem = TREE_VALUE (t);
4400 else
4401 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
4402 TREE_VALUE (t), tem);
4404 t = TREE_CHAIN (t);
4406 if (tem)
4407 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
4408 OMP_CLAUSE_DECL (c) = first;
4410 else
4412 unsigned int num = types.length (), i;
4413 tree t, side_effects = NULL_TREE, size = NULL_TREE;
4414 tree condition = NULL_TREE;
4416 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
4417 maybe_zero_len = true;
4418 if (processing_template_decl && maybe_zero_len)
4419 return false;
4421 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
4422 t = TREE_CHAIN (t))
4424 tree low_bound = TREE_PURPOSE (t);
4425 tree length = TREE_VALUE (t);
4427 i--;
4428 if (low_bound
4429 && TREE_CODE (low_bound) == INTEGER_CST
4430 && TYPE_PRECISION (TREE_TYPE (low_bound))
4431 > TYPE_PRECISION (sizetype))
4432 low_bound = fold_convert (sizetype, low_bound);
4433 if (length
4434 && TREE_CODE (length) == INTEGER_CST
4435 && TYPE_PRECISION (TREE_TYPE (length))
4436 > TYPE_PRECISION (sizetype))
4437 length = fold_convert (sizetype, length);
4438 if (low_bound == NULL_TREE)
4439 low_bound = integer_zero_node;
4440 if (!maybe_zero_len && i > first_non_one)
4442 if (integer_nonzerop (low_bound))
4443 goto do_warn_noncontiguous;
4444 if (length != NULL_TREE
4445 && TREE_CODE (length) == INTEGER_CST
4446 && TYPE_DOMAIN (types[i])
4447 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
4448 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
4449 == INTEGER_CST)
4451 tree size;
4452 size = size_binop (PLUS_EXPR,
4453 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
4454 size_one_node);
4455 if (!tree_int_cst_equal (length, size))
4457 do_warn_noncontiguous:
4458 error_at (OMP_CLAUSE_LOCATION (c),
4459 "array section is not contiguous in %qs "
4460 "clause",
4461 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4462 return true;
4465 if (!processing_template_decl
4466 && length != NULL_TREE
4467 && TREE_SIDE_EFFECTS (length))
4469 if (side_effects == NULL_TREE)
4470 side_effects = length;
4471 else
4472 side_effects = build2 (COMPOUND_EXPR,
4473 TREE_TYPE (side_effects),
4474 length, side_effects);
4477 else if (processing_template_decl)
4478 continue;
4479 else
4481 tree l;
4483 if (i > first_non_one && length && integer_nonzerop (length))
4484 continue;
4485 if (length)
4486 l = fold_convert (sizetype, length);
4487 else
4489 l = size_binop (PLUS_EXPR,
4490 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
4491 size_one_node);
4492 l = size_binop (MINUS_EXPR, l,
4493 fold_convert (sizetype, low_bound));
4495 if (i > first_non_one)
4497 l = fold_build2 (NE_EXPR, boolean_type_node, l,
4498 size_zero_node);
4499 if (condition == NULL_TREE)
4500 condition = l;
4501 else
4502 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
4503 l, condition);
4505 else if (size == NULL_TREE)
4507 size = size_in_bytes (TREE_TYPE (types[i]));
4508 size = size_binop (MULT_EXPR, size, l);
4509 if (condition)
4510 size = fold_build3 (COND_EXPR, sizetype, condition,
4511 size, size_zero_node);
4513 else
4514 size = size_binop (MULT_EXPR, size, l);
4517 if (!processing_template_decl)
4519 if (side_effects)
4520 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
4521 OMP_CLAUSE_DECL (c) = first;
4522 OMP_CLAUSE_SIZE (c) = size;
4523 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
4524 return false;
4525 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
4526 OMP_CLAUSE_MAP);
4527 OMP_CLAUSE_MAP_KIND (c2) = OMP_CLAUSE_MAP_POINTER;
4528 if (!cxx_mark_addressable (t))
4529 return false;
4530 OMP_CLAUSE_DECL (c2) = t;
4531 t = build_fold_addr_expr (first);
4532 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
4533 ptrdiff_type_node, t);
4534 tree ptr = OMP_CLAUSE_DECL (c2);
4535 ptr = convert_from_reference (ptr);
4536 if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
4537 ptr = build_fold_addr_expr (ptr);
4538 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
4539 ptrdiff_type_node, t,
4540 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
4541 ptrdiff_type_node, ptr));
4542 OMP_CLAUSE_SIZE (c2) = t;
4543 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
4544 OMP_CLAUSE_CHAIN (c) = c2;
4545 ptr = OMP_CLAUSE_DECL (c2);
4546 if (TREE_CODE (TREE_TYPE (ptr)) == REFERENCE_TYPE
4547 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (ptr))))
4549 tree c3 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
4550 OMP_CLAUSE_MAP);
4551 OMP_CLAUSE_MAP_KIND (c3) = OMP_CLAUSE_MAP_POINTER;
4552 OMP_CLAUSE_DECL (c3) = ptr;
4553 OMP_CLAUSE_DECL (c2) = convert_from_reference (ptr);
4554 OMP_CLAUSE_SIZE (c3) = size_zero_node;
4555 OMP_CLAUSE_CHAIN (c3) = OMP_CLAUSE_CHAIN (c2);
4556 OMP_CLAUSE_CHAIN (c2) = c3;
4560 return false;
4563 /* Return identifier to look up for omp declare reduction. */
4565 tree
4566 omp_reduction_id (enum tree_code reduction_code, tree reduction_id, tree type)
4568 const char *p = NULL;
4569 const char *m = NULL;
4570 switch (reduction_code)
4572 case PLUS_EXPR:
4573 case MULT_EXPR:
4574 case MINUS_EXPR:
4575 case BIT_AND_EXPR:
4576 case BIT_XOR_EXPR:
4577 case BIT_IOR_EXPR:
4578 case TRUTH_ANDIF_EXPR:
4579 case TRUTH_ORIF_EXPR:
4580 reduction_id = ansi_opname (reduction_code);
4581 break;
4582 case MIN_EXPR:
4583 p = "min";
4584 break;
4585 case MAX_EXPR:
4586 p = "max";
4587 break;
4588 default:
4589 break;
4592 if (p == NULL)
4594 if (TREE_CODE (reduction_id) != IDENTIFIER_NODE)
4595 return error_mark_node;
4596 p = IDENTIFIER_POINTER (reduction_id);
4599 if (type != NULL_TREE)
4600 m = mangle_type_string (TYPE_MAIN_VARIANT (type));
4602 const char prefix[] = "omp declare reduction ";
4603 size_t lenp = sizeof (prefix);
4604 if (strncmp (p, prefix, lenp - 1) == 0)
4605 lenp = 1;
4606 size_t len = strlen (p);
4607 size_t lenm = m ? strlen (m) + 1 : 0;
4608 char *name = XALLOCAVEC (char, lenp + len + lenm);
4609 if (lenp > 1)
4610 memcpy (name, prefix, lenp - 1);
4611 memcpy (name + lenp - 1, p, len + 1);
4612 if (m)
4614 name[lenp + len - 1] = '~';
4615 memcpy (name + lenp + len, m, lenm);
4617 return get_identifier (name);
4620 /* Lookup OpenMP UDR ID for TYPE, return the corresponding artificial
4621 FUNCTION_DECL or NULL_TREE if not found. */
4623 static tree
4624 omp_reduction_lookup (location_t loc, tree id, tree type, tree *baselinkp,
4625 vec<tree> *ambiguousp)
4627 tree orig_id = id;
4628 tree baselink = NULL_TREE;
4629 if (identifier_p (id))
4631 cp_id_kind idk;
4632 bool nonint_cst_expression_p;
4633 const char *error_msg;
4634 id = omp_reduction_id (ERROR_MARK, id, type);
4635 tree decl = lookup_name (id);
4636 if (decl == NULL_TREE)
4637 decl = error_mark_node;
4638 id = finish_id_expression (id, decl, NULL_TREE, &idk, false, true,
4639 &nonint_cst_expression_p, false, true, false,
4640 false, &error_msg, loc);
4641 if (idk == CP_ID_KIND_UNQUALIFIED
4642 && identifier_p (id))
4644 vec<tree, va_gc> *args = NULL;
4645 vec_safe_push (args, build_reference_type (type));
4646 id = perform_koenig_lookup (id, args, false, tf_none);
4649 else if (TREE_CODE (id) == SCOPE_REF)
4650 id = lookup_qualified_name (TREE_OPERAND (id, 0),
4651 omp_reduction_id (ERROR_MARK,
4652 TREE_OPERAND (id, 1),
4653 type),
4654 false, false);
4655 tree fns = id;
4656 if (id && is_overloaded_fn (id))
4657 id = get_fns (id);
4658 for (; id; id = OVL_NEXT (id))
4660 tree fndecl = OVL_CURRENT (id);
4661 if (TREE_CODE (fndecl) == FUNCTION_DECL)
4663 tree argtype = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
4664 if (same_type_p (TREE_TYPE (argtype), type))
4665 break;
4668 if (id && BASELINK_P (fns))
4670 if (baselinkp)
4671 *baselinkp = fns;
4672 else
4673 baselink = fns;
4675 if (id == NULL_TREE && CLASS_TYPE_P (type) && TYPE_BINFO (type))
4677 vec<tree> ambiguous = vNULL;
4678 tree binfo = TYPE_BINFO (type), base_binfo, ret = NULL_TREE;
4679 unsigned int ix;
4680 if (ambiguousp == NULL)
4681 ambiguousp = &ambiguous;
4682 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
4684 id = omp_reduction_lookup (loc, orig_id, BINFO_TYPE (base_binfo),
4685 baselinkp ? baselinkp : &baselink,
4686 ambiguousp);
4687 if (id == NULL_TREE)
4688 continue;
4689 if (!ambiguousp->is_empty ())
4690 ambiguousp->safe_push (id);
4691 else if (ret != NULL_TREE)
4693 ambiguousp->safe_push (ret);
4694 ambiguousp->safe_push (id);
4695 ret = NULL_TREE;
4697 else
4698 ret = id;
4700 if (ambiguousp != &ambiguous)
4701 return ret;
4702 if (!ambiguous.is_empty ())
4704 const char *str = _("candidates are:");
4705 unsigned int idx;
4706 tree udr;
4707 error_at (loc, "user defined reduction lookup is ambiguous");
4708 FOR_EACH_VEC_ELT (ambiguous, idx, udr)
4710 inform (DECL_SOURCE_LOCATION (udr), "%s %#D", str, udr);
4711 if (idx == 0)
4712 str = get_spaces (str);
4714 ambiguous.release ();
4715 ret = error_mark_node;
4716 baselink = NULL_TREE;
4718 id = ret;
4720 if (id && baselink)
4721 perform_or_defer_access_check (BASELINK_BINFO (baselink),
4722 id, id, tf_warning_or_error);
4723 return id;
4726 /* Helper function for cp_parser_omp_declare_reduction_exprs
4727 and tsubst_omp_udr.
4728 Remove CLEANUP_STMT for data (omp_priv variable).
4729 Also append INIT_EXPR for DECL_INITIAL of omp_priv after its
4730 DECL_EXPR. */
4732 tree
4733 cp_remove_omp_priv_cleanup_stmt (tree *tp, int *walk_subtrees, void *data)
4735 if (TYPE_P (*tp))
4736 *walk_subtrees = 0;
4737 else if (TREE_CODE (*tp) == CLEANUP_STMT && CLEANUP_DECL (*tp) == (tree) data)
4738 *tp = CLEANUP_BODY (*tp);
4739 else if (TREE_CODE (*tp) == DECL_EXPR)
4741 tree decl = DECL_EXPR_DECL (*tp);
4742 if (!processing_template_decl
4743 && decl == (tree) data
4744 && DECL_INITIAL (decl)
4745 && DECL_INITIAL (decl) != error_mark_node)
4747 tree list = NULL_TREE;
4748 append_to_statement_list_force (*tp, &list);
4749 tree init_expr = build2 (INIT_EXPR, void_type_node,
4750 decl, DECL_INITIAL (decl));
4751 DECL_INITIAL (decl) = NULL_TREE;
4752 append_to_statement_list_force (init_expr, &list);
4753 *tp = list;
4756 return NULL_TREE;
4759 /* Data passed from cp_check_omp_declare_reduction to
4760 cp_check_omp_declare_reduction_r. */
4762 struct cp_check_omp_declare_reduction_data
4764 location_t loc;
4765 tree stmts[7];
4766 bool combiner_p;
4769 /* Helper function for cp_check_omp_declare_reduction, called via
4770 cp_walk_tree. */
4772 static tree
4773 cp_check_omp_declare_reduction_r (tree *tp, int *, void *data)
4775 struct cp_check_omp_declare_reduction_data *udr_data
4776 = (struct cp_check_omp_declare_reduction_data *) data;
4777 if (SSA_VAR_P (*tp)
4778 && !DECL_ARTIFICIAL (*tp)
4779 && *tp != DECL_EXPR_DECL (udr_data->stmts[udr_data->combiner_p ? 0 : 3])
4780 && *tp != DECL_EXPR_DECL (udr_data->stmts[udr_data->combiner_p ? 1 : 4]))
4782 location_t loc = udr_data->loc;
4783 if (udr_data->combiner_p)
4784 error_at (loc, "%<#pragma omp declare reduction%> combiner refers to "
4785 "variable %qD which is not %<omp_out%> nor %<omp_in%>",
4786 *tp);
4787 else
4788 error_at (loc, "%<#pragma omp declare reduction%> initializer refers "
4789 "to variable %qD which is not %<omp_priv%> nor "
4790 "%<omp_orig%>",
4791 *tp);
4792 return *tp;
4794 return NULL_TREE;
4797 /* Diagnose violation of OpenMP #pragma omp declare reduction restrictions. */
4799 void
4800 cp_check_omp_declare_reduction (tree udr)
4802 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (udr)));
4803 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
4804 type = TREE_TYPE (type);
4805 int i;
4806 location_t loc = DECL_SOURCE_LOCATION (udr);
4808 if (type == error_mark_node)
4809 return;
4810 if (ARITHMETIC_TYPE_P (type))
4812 static enum tree_code predef_codes[]
4813 = { PLUS_EXPR, MULT_EXPR, MINUS_EXPR, BIT_AND_EXPR, BIT_XOR_EXPR,
4814 BIT_IOR_EXPR, TRUTH_ANDIF_EXPR, TRUTH_ORIF_EXPR };
4815 for (i = 0; i < 8; i++)
4817 tree id = omp_reduction_id (predef_codes[i], NULL_TREE, NULL_TREE);
4818 const char *n1 = IDENTIFIER_POINTER (DECL_NAME (udr));
4819 const char *n2 = IDENTIFIER_POINTER (id);
4820 if (strncmp (n1, n2, IDENTIFIER_LENGTH (id)) == 0
4821 && (n1[IDENTIFIER_LENGTH (id)] == '~'
4822 || n1[IDENTIFIER_LENGTH (id)] == '\0'))
4823 break;
4826 if (i == 8
4827 && TREE_CODE (type) != COMPLEX_EXPR)
4829 const char prefix_minmax[] = "omp declare reduction m";
4830 size_t prefix_size = sizeof (prefix_minmax) - 1;
4831 const char *n = IDENTIFIER_POINTER (DECL_NAME (udr));
4832 if (strncmp (IDENTIFIER_POINTER (DECL_NAME (udr)),
4833 prefix_minmax, prefix_size) == 0
4834 && ((n[prefix_size] == 'i' && n[prefix_size + 1] == 'n')
4835 || (n[prefix_size] == 'a' && n[prefix_size + 1] == 'x'))
4836 && (n[prefix_size + 2] == '~' || n[prefix_size + 2] == '\0'))
4837 i = 0;
4839 if (i < 8)
4841 error_at (loc, "predeclared arithmetic type %qT in "
4842 "%<#pragma omp declare reduction%>", type);
4843 return;
4846 else if (TREE_CODE (type) == FUNCTION_TYPE
4847 || TREE_CODE (type) == METHOD_TYPE
4848 || TREE_CODE (type) == ARRAY_TYPE)
4850 error_at (loc, "function or array type %qT in "
4851 "%<#pragma omp declare reduction%>", type);
4852 return;
4854 else if (TREE_CODE (type) == REFERENCE_TYPE)
4856 error_at (loc, "reference type %qT in %<#pragma omp declare reduction%>",
4857 type);
4858 return;
4860 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
4862 error_at (loc, "const, volatile or __restrict qualified type %qT in "
4863 "%<#pragma omp declare reduction%>", type);
4864 return;
4867 tree body = DECL_SAVED_TREE (udr);
4868 if (body == NULL_TREE || TREE_CODE (body) != STATEMENT_LIST)
4869 return;
4871 tree_stmt_iterator tsi;
4872 struct cp_check_omp_declare_reduction_data data;
4873 memset (data.stmts, 0, sizeof data.stmts);
4874 for (i = 0, tsi = tsi_start (body);
4875 i < 7 && !tsi_end_p (tsi);
4876 i++, tsi_next (&tsi))
4877 data.stmts[i] = tsi_stmt (tsi);
4878 data.loc = loc;
4879 gcc_assert (tsi_end_p (tsi));
4880 if (i >= 3)
4882 gcc_assert (TREE_CODE (data.stmts[0]) == DECL_EXPR
4883 && TREE_CODE (data.stmts[1]) == DECL_EXPR);
4884 if (TREE_NO_WARNING (DECL_EXPR_DECL (data.stmts[0])))
4885 return;
4886 data.combiner_p = true;
4887 if (cp_walk_tree (&data.stmts[2], cp_check_omp_declare_reduction_r,
4888 &data, NULL))
4889 TREE_NO_WARNING (DECL_EXPR_DECL (data.stmts[0])) = 1;
4891 if (i >= 6)
4893 gcc_assert (TREE_CODE (data.stmts[3]) == DECL_EXPR
4894 && TREE_CODE (data.stmts[4]) == DECL_EXPR);
4895 data.combiner_p = false;
4896 if (cp_walk_tree (&data.stmts[5], cp_check_omp_declare_reduction_r,
4897 &data, NULL)
4898 || cp_walk_tree (&DECL_INITIAL (DECL_EXPR_DECL (data.stmts[3])),
4899 cp_check_omp_declare_reduction_r, &data, NULL))
4900 TREE_NO_WARNING (DECL_EXPR_DECL (data.stmts[0])) = 1;
4901 if (i == 7)
4902 gcc_assert (TREE_CODE (data.stmts[6]) == DECL_EXPR);
4906 /* Helper function of finish_omp_clauses. Clone STMT as if we were making
4907 an inline call. But, remap
4908 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
4909 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
4911 static tree
4912 clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
4913 tree decl, tree placeholder)
4915 copy_body_data id;
4916 struct pointer_map_t *decl_map = pointer_map_create ();
4918 *pointer_map_insert (decl_map, omp_decl1) = placeholder;
4919 *pointer_map_insert (decl_map, omp_decl2) = decl;
4920 memset (&id, 0, sizeof (id));
4921 id.src_fn = DECL_CONTEXT (omp_decl1);
4922 id.dst_fn = current_function_decl;
4923 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
4924 id.decl_map = decl_map;
4926 id.copy_decl = copy_decl_no_change;
4927 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
4928 id.transform_new_cfg = true;
4929 id.transform_return_to_modify = false;
4930 id.transform_lang_insert_block = NULL;
4931 id.eh_lp_nr = 0;
4932 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
4933 pointer_map_destroy (decl_map);
4934 return stmt;
4937 /* Helper function of finish_omp_clauses, called via cp_walk_tree.
4938 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
4940 static tree
4941 find_omp_placeholder_r (tree *tp, int *, void *data)
4943 if (*tp == (tree) data)
4944 return *tp;
4945 return NULL_TREE;
4948 /* Helper function of finish_omp_clauses. Handle OMP_CLAUSE_REDUCTION C.
4949 Return true if there is some error and the clause should be removed. */
4951 static bool
4952 finish_omp_reduction_clause (tree c, bool *need_default_ctor, bool *need_dtor)
4954 tree t = OMP_CLAUSE_DECL (c);
4955 bool predefined = false;
4956 tree type = TREE_TYPE (t);
4957 if (TREE_CODE (type) == REFERENCE_TYPE)
4958 type = TREE_TYPE (type);
4959 if (ARITHMETIC_TYPE_P (type))
4960 switch (OMP_CLAUSE_REDUCTION_CODE (c))
4962 case PLUS_EXPR:
4963 case MULT_EXPR:
4964 case MINUS_EXPR:
4965 predefined = true;
4966 break;
4967 case MIN_EXPR:
4968 case MAX_EXPR:
4969 if (TREE_CODE (type) == COMPLEX_TYPE)
4970 break;
4971 predefined = true;
4972 break;
4973 case BIT_AND_EXPR:
4974 case BIT_IOR_EXPR:
4975 case BIT_XOR_EXPR:
4976 case TRUTH_ANDIF_EXPR:
4977 case TRUTH_ORIF_EXPR:
4978 if (FLOAT_TYPE_P (type))
4979 break;
4980 predefined = true;
4981 break;
4982 default:
4983 break;
4985 else if (TREE_CODE (type) == ARRAY_TYPE || TYPE_READONLY (type))
4987 error ("%qE has invalid type for %<reduction%>", t);
4988 return true;
4990 else if (!processing_template_decl)
4992 t = require_complete_type (t);
4993 if (t == error_mark_node)
4994 return true;
4995 OMP_CLAUSE_DECL (c) = t;
4998 if (predefined)
5000 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = NULL_TREE;
5001 return false;
5003 else if (processing_template_decl)
5004 return false;
5006 tree id = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
5008 type = TYPE_MAIN_VARIANT (TREE_TYPE (t));
5009 if (TREE_CODE (type) == REFERENCE_TYPE)
5010 type = TREE_TYPE (type);
5011 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = NULL_TREE;
5012 if (id == NULL_TREE)
5013 id = omp_reduction_id (OMP_CLAUSE_REDUCTION_CODE (c),
5014 NULL_TREE, NULL_TREE);
5015 id = omp_reduction_lookup (OMP_CLAUSE_LOCATION (c), id, type, NULL, NULL);
5016 if (id)
5018 if (id == error_mark_node)
5019 return true;
5020 id = OVL_CURRENT (id);
5021 mark_used (id);
5022 tree body = DECL_SAVED_TREE (id);
5023 if (TREE_CODE (body) == STATEMENT_LIST)
5025 tree_stmt_iterator tsi;
5026 tree placeholder = NULL_TREE;
5027 int i;
5028 tree stmts[7];
5029 tree atype = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (id)));
5030 atype = TREE_TYPE (atype);
5031 bool need_static_cast = !same_type_p (type, atype);
5032 memset (stmts, 0, sizeof stmts);
5033 for (i = 0, tsi = tsi_start (body);
5034 i < 7 && !tsi_end_p (tsi);
5035 i++, tsi_next (&tsi))
5036 stmts[i] = tsi_stmt (tsi);
5037 gcc_assert (tsi_end_p (tsi));
5039 if (i >= 3)
5041 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
5042 && TREE_CODE (stmts[1]) == DECL_EXPR);
5043 placeholder = build_lang_decl (VAR_DECL, NULL_TREE, type);
5044 DECL_ARTIFICIAL (placeholder) = 1;
5045 DECL_IGNORED_P (placeholder) = 1;
5046 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
5047 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[0])))
5048 cxx_mark_addressable (placeholder);
5049 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[1]))
5050 && TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c)))
5051 != REFERENCE_TYPE)
5052 cxx_mark_addressable (OMP_CLAUSE_DECL (c));
5053 tree omp_out = placeholder;
5054 tree omp_in = convert_from_reference (OMP_CLAUSE_DECL (c));
5055 if (need_static_cast)
5057 tree rtype = build_reference_type (atype);
5058 omp_out = build_static_cast (rtype, omp_out,
5059 tf_warning_or_error);
5060 omp_in = build_static_cast (rtype, omp_in,
5061 tf_warning_or_error);
5062 if (omp_out == error_mark_node || omp_in == error_mark_node)
5063 return true;
5064 omp_out = convert_from_reference (omp_out);
5065 omp_in = convert_from_reference (omp_in);
5067 OMP_CLAUSE_REDUCTION_MERGE (c)
5068 = clone_omp_udr (stmts[2], DECL_EXPR_DECL (stmts[0]),
5069 DECL_EXPR_DECL (stmts[1]), omp_in, omp_out);
5071 if (i >= 6)
5073 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
5074 && TREE_CODE (stmts[4]) == DECL_EXPR);
5075 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[3])))
5076 cxx_mark_addressable (OMP_CLAUSE_DECL (c));
5077 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[4])))
5078 cxx_mark_addressable (placeholder);
5079 tree omp_priv = convert_from_reference (OMP_CLAUSE_DECL (c));
5080 tree omp_orig = placeholder;
5081 if (need_static_cast)
5083 if (i == 7)
5085 error_at (OMP_CLAUSE_LOCATION (c),
5086 "user defined reduction with constructor "
5087 "initializer for base class %qT", atype);
5088 return true;
5090 tree rtype = build_reference_type (atype);
5091 omp_priv = build_static_cast (rtype, omp_priv,
5092 tf_warning_or_error);
5093 omp_orig = build_static_cast (rtype, omp_orig,
5094 tf_warning_or_error);
5095 if (omp_priv == error_mark_node
5096 || omp_orig == error_mark_node)
5097 return true;
5098 omp_priv = convert_from_reference (omp_priv);
5099 omp_orig = convert_from_reference (omp_orig);
5101 if (i == 6)
5102 *need_default_ctor = true;
5103 OMP_CLAUSE_REDUCTION_INIT (c)
5104 = clone_omp_udr (stmts[5], DECL_EXPR_DECL (stmts[4]),
5105 DECL_EXPR_DECL (stmts[3]),
5106 omp_priv, omp_orig);
5107 if (cp_walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
5108 find_omp_placeholder_r, placeholder, NULL))
5109 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
5111 else if (i >= 3)
5113 if (CLASS_TYPE_P (type) && !pod_type_p (type))
5114 *need_default_ctor = true;
5115 else
5117 tree init;
5118 tree v = convert_from_reference (t);
5119 if (AGGREGATE_TYPE_P (TREE_TYPE (v)))
5120 init = build_constructor (TREE_TYPE (v), NULL);
5121 else
5122 init = fold_convert (TREE_TYPE (v), integer_zero_node);
5123 OMP_CLAUSE_REDUCTION_INIT (c)
5124 = build2 (INIT_EXPR, TREE_TYPE (v), v, init);
5129 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
5130 *need_dtor = true;
5131 else
5133 error ("user defined reduction not found for %qD", t);
5134 return true;
5136 return false;
5139 /* For all elements of CLAUSES, validate them vs OpenMP constraints.
5140 Remove any elements from the list that are invalid. */
5142 tree
5143 finish_omp_clauses (tree clauses)
5145 bitmap_head generic_head, firstprivate_head, lastprivate_head;
5146 bitmap_head aligned_head;
5147 tree c, t, *pc = &clauses;
5148 bool branch_seen = false;
5149 bool copyprivate_seen = false;
5151 bitmap_obstack_initialize (NULL);
5152 bitmap_initialize (&generic_head, &bitmap_default_obstack);
5153 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
5154 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
5155 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
5157 for (pc = &clauses, c = clauses; c ; c = *pc)
5159 bool remove = false;
5161 switch (OMP_CLAUSE_CODE (c))
5163 case OMP_CLAUSE_SHARED:
5164 goto check_dup_generic;
5165 case OMP_CLAUSE_PRIVATE:
5166 goto check_dup_generic;
5167 case OMP_CLAUSE_REDUCTION:
5168 goto check_dup_generic;
5169 case OMP_CLAUSE_COPYPRIVATE:
5170 copyprivate_seen = true;
5171 goto check_dup_generic;
5172 case OMP_CLAUSE_COPYIN:
5173 goto check_dup_generic;
5174 case OMP_CLAUSE_LINEAR:
5175 t = OMP_CLAUSE_DECL (c);
5176 if (!type_dependent_expression_p (t)
5177 && !INTEGRAL_TYPE_P (TREE_TYPE (t))
5178 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
5180 error ("linear clause applied to non-integral non-pointer "
5181 "variable with %qT type", TREE_TYPE (t));
5182 remove = true;
5183 break;
5185 t = OMP_CLAUSE_LINEAR_STEP (c);
5186 if (t == NULL_TREE)
5187 t = integer_one_node;
5188 if (t == error_mark_node)
5190 remove = true;
5191 break;
5193 else if (!type_dependent_expression_p (t)
5194 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5196 error ("linear step expression must be integral");
5197 remove = true;
5198 break;
5200 else
5202 t = mark_rvalue_use (t);
5203 if (!processing_template_decl)
5205 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5206 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c)))
5207 == POINTER_TYPE)
5209 t = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
5210 OMP_CLAUSE_DECL (c), t);
5211 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c),
5212 MINUS_EXPR, sizetype, t,
5213 OMP_CLAUSE_DECL (c));
5214 if (t == error_mark_node)
5216 remove = true;
5217 break;
5221 OMP_CLAUSE_LINEAR_STEP (c) = t;
5223 goto check_dup_generic;
5224 check_dup_generic:
5225 t = OMP_CLAUSE_DECL (c);
5226 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
5228 if (processing_template_decl)
5229 break;
5230 if (DECL_P (t))
5231 error ("%qD is not a variable in clause %qs", t,
5232 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5233 else
5234 error ("%qE is not a variable in clause %qs", t,
5235 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5236 remove = true;
5238 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
5239 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
5240 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
5242 error ("%qD appears more than once in data clauses", t);
5243 remove = true;
5245 else
5246 bitmap_set_bit (&generic_head, DECL_UID (t));
5247 break;
5249 case OMP_CLAUSE_FIRSTPRIVATE:
5250 t = OMP_CLAUSE_DECL (c);
5251 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
5253 if (processing_template_decl)
5254 break;
5255 if (DECL_P (t))
5256 error ("%qD is not a variable in clause %<firstprivate%>", t);
5257 else
5258 error ("%qE is not a variable in clause %<firstprivate%>", t);
5259 remove = true;
5261 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
5262 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
5264 error ("%qD appears more than once in data clauses", t);
5265 remove = true;
5267 else
5268 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
5269 break;
5271 case OMP_CLAUSE_LASTPRIVATE:
5272 t = OMP_CLAUSE_DECL (c);
5273 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
5275 if (processing_template_decl)
5276 break;
5277 if (DECL_P (t))
5278 error ("%qD is not a variable in clause %<lastprivate%>", t);
5279 else
5280 error ("%qE is not a variable in clause %<lastprivate%>", t);
5281 remove = true;
5283 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
5284 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
5286 error ("%qD appears more than once in data clauses", t);
5287 remove = true;
5289 else
5290 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
5291 break;
5293 case OMP_CLAUSE_IF:
5294 t = OMP_CLAUSE_IF_EXPR (c);
5295 t = maybe_convert_cond (t);
5296 if (t == error_mark_node)
5297 remove = true;
5298 else if (!processing_template_decl)
5299 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5300 OMP_CLAUSE_IF_EXPR (c) = t;
5301 break;
5303 case OMP_CLAUSE_FINAL:
5304 t = OMP_CLAUSE_FINAL_EXPR (c);
5305 t = maybe_convert_cond (t);
5306 if (t == error_mark_node)
5307 remove = true;
5308 else if (!processing_template_decl)
5309 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5310 OMP_CLAUSE_FINAL_EXPR (c) = t;
5311 break;
5313 case OMP_CLAUSE_NUM_THREADS:
5314 t = OMP_CLAUSE_NUM_THREADS_EXPR (c);
5315 if (t == error_mark_node)
5316 remove = true;
5317 else if (!type_dependent_expression_p (t)
5318 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5320 error ("num_threads expression must be integral");
5321 remove = true;
5323 else
5325 t = mark_rvalue_use (t);
5326 if (!processing_template_decl)
5327 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5328 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
5330 break;
5332 case OMP_CLAUSE_SCHEDULE:
5333 t = OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c);
5334 if (t == NULL)
5336 else if (t == error_mark_node)
5337 remove = true;
5338 else if (!type_dependent_expression_p (t)
5339 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5341 error ("schedule chunk size expression must be integral");
5342 remove = true;
5344 else
5346 t = mark_rvalue_use (t);
5347 if (!processing_template_decl)
5348 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5349 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
5351 break;
5353 case OMP_CLAUSE_SIMDLEN:
5354 case OMP_CLAUSE_SAFELEN:
5355 t = OMP_CLAUSE_OPERAND (c, 0);
5356 if (t == error_mark_node)
5357 remove = true;
5358 else if (!type_dependent_expression_p (t)
5359 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5361 error ("%qs length expression must be integral",
5362 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5363 remove = true;
5365 else
5367 t = mark_rvalue_use (t);
5368 t = maybe_constant_value (t);
5369 if (!processing_template_decl)
5371 if (TREE_CODE (t) != INTEGER_CST
5372 || tree_int_cst_sgn (t) != 1)
5374 error ("%qs length expression must be positive constant"
5375 " integer expression",
5376 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5377 remove = true;
5380 OMP_CLAUSE_OPERAND (c, 0) = t;
5382 break;
5384 case OMP_CLAUSE_NUM_TEAMS:
5385 t = OMP_CLAUSE_NUM_TEAMS_EXPR (c);
5386 if (t == error_mark_node)
5387 remove = true;
5388 else if (!type_dependent_expression_p (t)
5389 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5391 error ("%<num_teams%> expression must be integral");
5392 remove = true;
5394 else
5396 t = mark_rvalue_use (t);
5397 if (!processing_template_decl)
5398 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5399 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
5401 break;
5403 case OMP_CLAUSE_THREAD_LIMIT:
5404 t = OMP_CLAUSE_THREAD_LIMIT_EXPR (c);
5405 if (t == error_mark_node)
5406 remove = true;
5407 else if (!type_dependent_expression_p (t)
5408 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5410 error ("%<thread_limit%> expression must be integral");
5411 remove = true;
5413 else
5415 t = mark_rvalue_use (t);
5416 if (!processing_template_decl)
5417 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5418 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
5420 break;
5422 case OMP_CLAUSE_DEVICE:
5423 t = OMP_CLAUSE_DEVICE_ID (c);
5424 if (t == error_mark_node)
5425 remove = true;
5426 else if (!type_dependent_expression_p (t)
5427 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5429 error ("%<device%> id must be integral");
5430 remove = true;
5432 else
5434 t = mark_rvalue_use (t);
5435 if (!processing_template_decl)
5436 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5437 OMP_CLAUSE_DEVICE_ID (c) = t;
5439 break;
5441 case OMP_CLAUSE_DIST_SCHEDULE:
5442 t = OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c);
5443 if (t == NULL)
5445 else if (t == error_mark_node)
5446 remove = true;
5447 else if (!type_dependent_expression_p (t)
5448 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5450 error ("%<dist_schedule%> chunk size expression must be "
5451 "integral");
5452 remove = true;
5454 else
5456 t = mark_rvalue_use (t);
5457 if (!processing_template_decl)
5458 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5459 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
5461 break;
5463 case OMP_CLAUSE_ALIGNED:
5464 t = OMP_CLAUSE_DECL (c);
5465 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
5467 if (processing_template_decl)
5468 break;
5469 if (DECL_P (t))
5470 error ("%qD is not a variable in %<aligned%> clause", t);
5471 else
5472 error ("%qE is not a variable in %<aligned%> clause", t);
5473 remove = true;
5475 else if (!type_dependent_expression_p (t)
5476 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE
5477 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE
5478 && (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE
5479 || (!POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (t)))
5480 && (TREE_CODE (TREE_TYPE (TREE_TYPE (t)))
5481 != ARRAY_TYPE))))
5483 error_at (OMP_CLAUSE_LOCATION (c),
5484 "%qE in %<aligned%> clause is neither a pointer nor "
5485 "an array nor a reference to pointer or array", t);
5486 remove = true;
5488 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
5490 error ("%qD appears more than once in %<aligned%> clauses", t);
5491 remove = true;
5493 else
5494 bitmap_set_bit (&aligned_head, DECL_UID (t));
5495 t = OMP_CLAUSE_ALIGNED_ALIGNMENT (c);
5496 if (t == error_mark_node)
5497 remove = true;
5498 else if (t == NULL_TREE)
5499 break;
5500 else if (!type_dependent_expression_p (t)
5501 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5503 error ("%<aligned%> clause alignment expression must "
5504 "be integral");
5505 remove = true;
5507 else
5509 t = mark_rvalue_use (t);
5510 t = maybe_constant_value (t);
5511 if (!processing_template_decl)
5513 if (TREE_CODE (t) != INTEGER_CST
5514 || tree_int_cst_sgn (t) != 1)
5516 error ("%<aligned%> clause alignment expression must be "
5517 "positive constant integer expression");
5518 remove = true;
5521 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = t;
5523 break;
5525 case OMP_CLAUSE_DEPEND:
5526 t = OMP_CLAUSE_DECL (c);
5527 if (TREE_CODE (t) == TREE_LIST)
5529 if (handle_omp_array_sections (c))
5530 remove = true;
5531 break;
5533 if (t == error_mark_node)
5534 remove = true;
5535 else if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
5537 if (processing_template_decl)
5538 break;
5539 if (DECL_P (t))
5540 error ("%qD is not a variable in %<depend%> clause", t);
5541 else
5542 error ("%qE is not a variable in %<depend%> clause", t);
5543 remove = true;
5545 else if (!processing_template_decl
5546 && !cxx_mark_addressable (t))
5547 remove = true;
5548 break;
5550 case OMP_CLAUSE_MAP:
5551 case OMP_CLAUSE_TO:
5552 case OMP_CLAUSE_FROM:
5553 t = OMP_CLAUSE_DECL (c);
5554 if (TREE_CODE (t) == TREE_LIST)
5556 if (handle_omp_array_sections (c))
5557 remove = true;
5558 else
5560 t = OMP_CLAUSE_DECL (c);
5561 if (!cp_omp_mappable_type (TREE_TYPE (t)))
5563 error_at (OMP_CLAUSE_LOCATION (c),
5564 "array section does not have mappable type "
5565 "in %qs clause",
5566 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5567 remove = true;
5570 break;
5572 if (t == error_mark_node)
5573 remove = true;
5574 else if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
5576 if (processing_template_decl)
5577 break;
5578 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
5579 && OMP_CLAUSE_MAP_KIND (c) == OMP_CLAUSE_MAP_POINTER)
5580 break;
5581 if (DECL_P (t))
5582 error ("%qD is not a variable in %qs clause", t,
5583 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5584 else
5585 error ("%qE is not a variable in %qs clause", t,
5586 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5587 remove = true;
5589 else if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
5591 error ("%qD is threadprivate variable in %qs clause", t,
5592 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5593 remove = true;
5595 else if (!processing_template_decl
5596 && TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE
5597 && !cxx_mark_addressable (t))
5598 remove = true;
5599 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
5600 && OMP_CLAUSE_MAP_KIND (c) == OMP_CLAUSE_MAP_POINTER)
5601 && !cp_omp_mappable_type ((TREE_CODE (TREE_TYPE (t))
5602 == REFERENCE_TYPE)
5603 ? TREE_TYPE (TREE_TYPE (t))
5604 : TREE_TYPE (t)))
5606 error_at (OMP_CLAUSE_LOCATION (c),
5607 "%qD does not have a mappable type in %qs clause", t,
5608 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5609 remove = true;
5611 else if (bitmap_bit_p (&generic_head, DECL_UID (t)))
5613 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
5614 error ("%qD appears more than once in motion clauses", t);
5615 else
5616 error ("%qD appears more than once in map clauses", t);
5617 remove = true;
5619 else
5620 bitmap_set_bit (&generic_head, DECL_UID (t));
5621 break;
5623 case OMP_CLAUSE_UNIFORM:
5624 t = OMP_CLAUSE_DECL (c);
5625 if (TREE_CODE (t) != PARM_DECL)
5627 if (processing_template_decl)
5628 break;
5629 if (DECL_P (t))
5630 error ("%qD is not an argument in %<uniform%> clause", t);
5631 else
5632 error ("%qE is not an argument in %<uniform%> clause", t);
5633 remove = true;
5634 break;
5636 goto check_dup_generic;
5638 case OMP_CLAUSE_NOWAIT:
5639 case OMP_CLAUSE_ORDERED:
5640 case OMP_CLAUSE_DEFAULT:
5641 case OMP_CLAUSE_UNTIED:
5642 case OMP_CLAUSE_COLLAPSE:
5643 case OMP_CLAUSE_MERGEABLE:
5644 case OMP_CLAUSE_PARALLEL:
5645 case OMP_CLAUSE_FOR:
5646 case OMP_CLAUSE_SECTIONS:
5647 case OMP_CLAUSE_TASKGROUP:
5648 case OMP_CLAUSE_PROC_BIND:
5649 break;
5651 case OMP_CLAUSE_INBRANCH:
5652 case OMP_CLAUSE_NOTINBRANCH:
5653 if (branch_seen)
5655 error ("%<inbranch%> clause is incompatible with "
5656 "%<notinbranch%>");
5657 remove = true;
5659 branch_seen = true;
5660 break;
5662 default:
5663 gcc_unreachable ();
5666 if (remove)
5667 *pc = OMP_CLAUSE_CHAIN (c);
5668 else
5669 pc = &OMP_CLAUSE_CHAIN (c);
5672 for (pc = &clauses, c = clauses; c ; c = *pc)
5674 enum omp_clause_code c_kind = OMP_CLAUSE_CODE (c);
5675 bool remove = false;
5676 bool need_complete_non_reference = false;
5677 bool need_default_ctor = false;
5678 bool need_copy_ctor = false;
5679 bool need_copy_assignment = false;
5680 bool need_implicitly_determined = false;
5681 bool need_dtor = false;
5682 tree type, inner_type;
5684 switch (c_kind)
5686 case OMP_CLAUSE_SHARED:
5687 need_implicitly_determined = true;
5688 break;
5689 case OMP_CLAUSE_PRIVATE:
5690 need_complete_non_reference = true;
5691 need_default_ctor = true;
5692 need_dtor = true;
5693 need_implicitly_determined = true;
5694 break;
5695 case OMP_CLAUSE_FIRSTPRIVATE:
5696 need_complete_non_reference = true;
5697 need_copy_ctor = true;
5698 need_dtor = true;
5699 need_implicitly_determined = true;
5700 break;
5701 case OMP_CLAUSE_LASTPRIVATE:
5702 need_complete_non_reference = true;
5703 need_copy_assignment = true;
5704 need_implicitly_determined = true;
5705 break;
5706 case OMP_CLAUSE_REDUCTION:
5707 need_implicitly_determined = true;
5708 break;
5709 case OMP_CLAUSE_COPYPRIVATE:
5710 need_copy_assignment = true;
5711 break;
5712 case OMP_CLAUSE_COPYIN:
5713 need_copy_assignment = true;
5714 break;
5715 case OMP_CLAUSE_NOWAIT:
5716 if (copyprivate_seen)
5718 error_at (OMP_CLAUSE_LOCATION (c),
5719 "%<nowait%> clause must not be used together "
5720 "with %<copyprivate%>");
5721 *pc = OMP_CLAUSE_CHAIN (c);
5722 continue;
5724 /* FALLTHRU */
5725 default:
5726 pc = &OMP_CLAUSE_CHAIN (c);
5727 continue;
5730 t = OMP_CLAUSE_DECL (c);
5731 if (processing_template_decl
5732 && !VAR_P (t) && TREE_CODE (t) != PARM_DECL)
5734 pc = &OMP_CLAUSE_CHAIN (c);
5735 continue;
5738 switch (c_kind)
5740 case OMP_CLAUSE_LASTPRIVATE:
5741 if (!bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
5743 need_default_ctor = true;
5744 need_dtor = true;
5746 break;
5748 case OMP_CLAUSE_REDUCTION:
5749 if (finish_omp_reduction_clause (c, &need_default_ctor,
5750 &need_dtor))
5751 remove = true;
5752 else
5753 t = OMP_CLAUSE_DECL (c);
5754 break;
5756 case OMP_CLAUSE_COPYIN:
5757 if (!VAR_P (t) || !DECL_THREAD_LOCAL_P (t))
5759 error ("%qE must be %<threadprivate%> for %<copyin%>", t);
5760 remove = true;
5762 break;
5764 default:
5765 break;
5768 if (need_complete_non_reference || need_copy_assignment)
5770 t = require_complete_type (t);
5771 if (t == error_mark_node)
5772 remove = true;
5773 else if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE
5774 && need_complete_non_reference)
5776 error ("%qE has reference type for %qs", t,
5777 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5778 remove = true;
5781 if (need_implicitly_determined)
5783 const char *share_name = NULL;
5785 if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
5786 share_name = "threadprivate";
5787 else switch (cxx_omp_predetermined_sharing (t))
5789 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
5790 break;
5791 case OMP_CLAUSE_DEFAULT_SHARED:
5792 /* const vars may be specified in firstprivate clause. */
5793 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
5794 && cxx_omp_const_qual_no_mutable (t))
5795 break;
5796 share_name = "shared";
5797 break;
5798 case OMP_CLAUSE_DEFAULT_PRIVATE:
5799 share_name = "private";
5800 break;
5801 default:
5802 gcc_unreachable ();
5804 if (share_name)
5806 error ("%qE is predetermined %qs for %qs",
5807 t, share_name, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5808 remove = true;
5812 /* We're interested in the base element, not arrays. */
5813 inner_type = type = TREE_TYPE (t);
5814 while (TREE_CODE (inner_type) == ARRAY_TYPE)
5815 inner_type = TREE_TYPE (inner_type);
5817 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
5818 && TREE_CODE (inner_type) == REFERENCE_TYPE)
5819 inner_type = TREE_TYPE (inner_type);
5821 /* Check for special function availability by building a call to one.
5822 Save the results, because later we won't be in the right context
5823 for making these queries. */
5824 if (CLASS_TYPE_P (inner_type)
5825 && COMPLETE_TYPE_P (inner_type)
5826 && (need_default_ctor || need_copy_ctor
5827 || need_copy_assignment || need_dtor)
5828 && !type_dependent_expression_p (t)
5829 && cxx_omp_create_clause_info (c, inner_type, need_default_ctor,
5830 need_copy_ctor, need_copy_assignment,
5831 need_dtor))
5832 remove = true;
5834 if (remove)
5835 *pc = OMP_CLAUSE_CHAIN (c);
5836 else
5837 pc = &OMP_CLAUSE_CHAIN (c);
5840 bitmap_obstack_release (NULL);
5841 return clauses;
5844 /* For all variables in the tree_list VARS, mark them as thread local. */
5846 void
5847 finish_omp_threadprivate (tree vars)
5849 tree t;
5851 /* Mark every variable in VARS to be assigned thread local storage. */
5852 for (t = vars; t; t = TREE_CHAIN (t))
5854 tree v = TREE_PURPOSE (t);
5856 if (error_operand_p (v))
5858 else if (!VAR_P (v))
5859 error ("%<threadprivate%> %qD is not file, namespace "
5860 "or block scope variable", v);
5861 /* If V had already been marked threadprivate, it doesn't matter
5862 whether it had been used prior to this point. */
5863 else if (TREE_USED (v)
5864 && (DECL_LANG_SPECIFIC (v) == NULL
5865 || !CP_DECL_THREADPRIVATE_P (v)))
5866 error ("%qE declared %<threadprivate%> after first use", v);
5867 else if (! TREE_STATIC (v) && ! DECL_EXTERNAL (v))
5868 error ("automatic variable %qE cannot be %<threadprivate%>", v);
5869 else if (! COMPLETE_TYPE_P (complete_type (TREE_TYPE (v))))
5870 error ("%<threadprivate%> %qE has incomplete type", v);
5871 else if (TREE_STATIC (v) && TYPE_P (CP_DECL_CONTEXT (v))
5872 && CP_DECL_CONTEXT (v) != current_class_type)
5873 error ("%<threadprivate%> %qE directive not "
5874 "in %qT definition", v, CP_DECL_CONTEXT (v));
5875 else
5877 /* Allocate a LANG_SPECIFIC structure for V, if needed. */
5878 if (DECL_LANG_SPECIFIC (v) == NULL)
5880 retrofit_lang_decl (v);
5882 /* Make sure that DECL_DISCRIMINATOR_P continues to be true
5883 after the allocation of the lang_decl structure. */
5884 if (DECL_DISCRIMINATOR_P (v))
5885 DECL_LANG_SPECIFIC (v)->u.base.u2sel = 1;
5888 if (! DECL_THREAD_LOCAL_P (v))
5890 DECL_TLS_MODEL (v) = decl_default_tls_model (v);
5891 /* If rtl has been already set for this var, call
5892 make_decl_rtl once again, so that encode_section_info
5893 has a chance to look at the new decl flags. */
5894 if (DECL_RTL_SET_P (v))
5895 make_decl_rtl (v);
5897 CP_DECL_THREADPRIVATE_P (v) = 1;
5902 /* Build an OpenMP structured block. */
5904 tree
5905 begin_omp_structured_block (void)
5907 return do_pushlevel (sk_omp);
5910 tree
5911 finish_omp_structured_block (tree block)
5913 return do_poplevel (block);
5916 /* Similarly, except force the retention of the BLOCK. */
5918 tree
5919 begin_omp_parallel (void)
5921 keep_next_level (true);
5922 return begin_omp_structured_block ();
5925 tree
5926 finish_omp_parallel (tree clauses, tree body)
5928 tree stmt;
5930 body = finish_omp_structured_block (body);
5932 stmt = make_node (OMP_PARALLEL);
5933 TREE_TYPE (stmt) = void_type_node;
5934 OMP_PARALLEL_CLAUSES (stmt) = clauses;
5935 OMP_PARALLEL_BODY (stmt) = body;
5937 return add_stmt (stmt);
5940 tree
5941 begin_omp_task (void)
5943 keep_next_level (true);
5944 return begin_omp_structured_block ();
5947 tree
5948 finish_omp_task (tree clauses, tree body)
5950 tree stmt;
5952 body = finish_omp_structured_block (body);
5954 stmt = make_node (OMP_TASK);
5955 TREE_TYPE (stmt) = void_type_node;
5956 OMP_TASK_CLAUSES (stmt) = clauses;
5957 OMP_TASK_BODY (stmt) = body;
5959 return add_stmt (stmt);
5962 /* Helper function for finish_omp_for. Convert Ith random access iterator
5963 into integral iterator. Return FALSE if successful. */
5965 static bool
5966 handle_omp_for_class_iterator (int i, location_t locus, tree declv, tree initv,
5967 tree condv, tree incrv, tree *body,
5968 tree *pre_body, tree clauses)
5970 tree diff, iter_init, iter_incr = NULL, last;
5971 tree incr_var = NULL, orig_pre_body, orig_body, c;
5972 tree decl = TREE_VEC_ELT (declv, i);
5973 tree init = TREE_VEC_ELT (initv, i);
5974 tree cond = TREE_VEC_ELT (condv, i);
5975 tree incr = TREE_VEC_ELT (incrv, i);
5976 tree iter = decl;
5977 location_t elocus = locus;
5979 if (init && EXPR_HAS_LOCATION (init))
5980 elocus = EXPR_LOCATION (init);
5982 switch (TREE_CODE (cond))
5984 case GT_EXPR:
5985 case GE_EXPR:
5986 case LT_EXPR:
5987 case LE_EXPR:
5988 if (TREE_OPERAND (cond, 1) == iter)
5989 cond = build2 (swap_tree_comparison (TREE_CODE (cond)),
5990 TREE_TYPE (cond), iter, TREE_OPERAND (cond, 0));
5991 if (TREE_OPERAND (cond, 0) != iter)
5992 cond = error_mark_node;
5993 else
5995 tree tem = build_x_binary_op (EXPR_LOCATION (cond),
5996 TREE_CODE (cond),
5997 iter, ERROR_MARK,
5998 TREE_OPERAND (cond, 1), ERROR_MARK,
5999 NULL, tf_warning_or_error);
6000 if (error_operand_p (tem))
6001 return true;
6003 break;
6004 default:
6005 cond = error_mark_node;
6006 break;
6008 if (cond == error_mark_node)
6010 error_at (elocus, "invalid controlling predicate");
6011 return true;
6013 diff = build_x_binary_op (elocus, MINUS_EXPR, TREE_OPERAND (cond, 1),
6014 ERROR_MARK, iter, ERROR_MARK, NULL,
6015 tf_warning_or_error);
6016 if (error_operand_p (diff))
6017 return true;
6018 if (TREE_CODE (TREE_TYPE (diff)) != INTEGER_TYPE)
6020 error_at (elocus, "difference between %qE and %qD does not have integer type",
6021 TREE_OPERAND (cond, 1), iter);
6022 return true;
6025 switch (TREE_CODE (incr))
6027 case PREINCREMENT_EXPR:
6028 case PREDECREMENT_EXPR:
6029 case POSTINCREMENT_EXPR:
6030 case POSTDECREMENT_EXPR:
6031 if (TREE_OPERAND (incr, 0) != iter)
6033 incr = error_mark_node;
6034 break;
6036 iter_incr = build_x_unary_op (EXPR_LOCATION (incr),
6037 TREE_CODE (incr), iter,
6038 tf_warning_or_error);
6039 if (error_operand_p (iter_incr))
6040 return true;
6041 else if (TREE_CODE (incr) == PREINCREMENT_EXPR
6042 || TREE_CODE (incr) == POSTINCREMENT_EXPR)
6043 incr = integer_one_node;
6044 else
6045 incr = integer_minus_one_node;
6046 break;
6047 case MODIFY_EXPR:
6048 if (TREE_OPERAND (incr, 0) != iter)
6049 incr = error_mark_node;
6050 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
6051 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
6053 tree rhs = TREE_OPERAND (incr, 1);
6054 if (TREE_OPERAND (rhs, 0) == iter)
6056 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (rhs, 1)))
6057 != INTEGER_TYPE)
6058 incr = error_mark_node;
6059 else
6061 iter_incr = build_x_modify_expr (EXPR_LOCATION (rhs),
6062 iter, TREE_CODE (rhs),
6063 TREE_OPERAND (rhs, 1),
6064 tf_warning_or_error);
6065 if (error_operand_p (iter_incr))
6066 return true;
6067 incr = TREE_OPERAND (rhs, 1);
6068 incr = cp_convert (TREE_TYPE (diff), incr,
6069 tf_warning_or_error);
6070 if (TREE_CODE (rhs) == MINUS_EXPR)
6072 incr = build1 (NEGATE_EXPR, TREE_TYPE (diff), incr);
6073 incr = fold_if_not_in_template (incr);
6075 if (TREE_CODE (incr) != INTEGER_CST
6076 && (TREE_CODE (incr) != NOP_EXPR
6077 || (TREE_CODE (TREE_OPERAND (incr, 0))
6078 != INTEGER_CST)))
6079 iter_incr = NULL;
6082 else if (TREE_OPERAND (rhs, 1) == iter)
6084 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (rhs, 0))) != INTEGER_TYPE
6085 || TREE_CODE (rhs) != PLUS_EXPR)
6086 incr = error_mark_node;
6087 else
6089 iter_incr = build_x_binary_op (EXPR_LOCATION (rhs),
6090 PLUS_EXPR,
6091 TREE_OPERAND (rhs, 0),
6092 ERROR_MARK, iter,
6093 ERROR_MARK, NULL,
6094 tf_warning_or_error);
6095 if (error_operand_p (iter_incr))
6096 return true;
6097 iter_incr = build_x_modify_expr (EXPR_LOCATION (rhs),
6098 iter, NOP_EXPR,
6099 iter_incr,
6100 tf_warning_or_error);
6101 if (error_operand_p (iter_incr))
6102 return true;
6103 incr = TREE_OPERAND (rhs, 0);
6104 iter_incr = NULL;
6107 else
6108 incr = error_mark_node;
6110 else
6111 incr = error_mark_node;
6112 break;
6113 default:
6114 incr = error_mark_node;
6115 break;
6118 if (incr == error_mark_node)
6120 error_at (elocus, "invalid increment expression");
6121 return true;
6124 incr = cp_convert (TREE_TYPE (diff), incr, tf_warning_or_error);
6125 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
6126 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
6127 && OMP_CLAUSE_DECL (c) == iter)
6128 break;
6130 decl = create_temporary_var (TREE_TYPE (diff));
6131 pushdecl (decl);
6132 add_decl_expr (decl);
6133 last = create_temporary_var (TREE_TYPE (diff));
6134 pushdecl (last);
6135 add_decl_expr (last);
6136 if (c && iter_incr == NULL)
6138 incr_var = create_temporary_var (TREE_TYPE (diff));
6139 pushdecl (incr_var);
6140 add_decl_expr (incr_var);
6142 gcc_assert (stmts_are_full_exprs_p ());
6144 orig_pre_body = *pre_body;
6145 *pre_body = push_stmt_list ();
6146 if (orig_pre_body)
6147 add_stmt (orig_pre_body);
6148 if (init != NULL)
6149 finish_expr_stmt (build_x_modify_expr (elocus,
6150 iter, NOP_EXPR, init,
6151 tf_warning_or_error));
6152 init = build_int_cst (TREE_TYPE (diff), 0);
6153 if (c && iter_incr == NULL)
6155 finish_expr_stmt (build_x_modify_expr (elocus,
6156 incr_var, NOP_EXPR,
6157 incr, tf_warning_or_error));
6158 incr = incr_var;
6159 iter_incr = build_x_modify_expr (elocus,
6160 iter, PLUS_EXPR, incr,
6161 tf_warning_or_error);
6163 finish_expr_stmt (build_x_modify_expr (elocus,
6164 last, NOP_EXPR, init,
6165 tf_warning_or_error));
6166 *pre_body = pop_stmt_list (*pre_body);
6168 cond = cp_build_binary_op (elocus,
6169 TREE_CODE (cond), decl, diff,
6170 tf_warning_or_error);
6171 incr = build_modify_expr (elocus, decl, NULL_TREE, PLUS_EXPR,
6172 elocus, incr, NULL_TREE);
6174 orig_body = *body;
6175 *body = push_stmt_list ();
6176 iter_init = build2 (MINUS_EXPR, TREE_TYPE (diff), decl, last);
6177 iter_init = build_x_modify_expr (elocus,
6178 iter, PLUS_EXPR, iter_init,
6179 tf_warning_or_error);
6180 iter_init = build1 (NOP_EXPR, void_type_node, iter_init);
6181 finish_expr_stmt (iter_init);
6182 finish_expr_stmt (build_x_modify_expr (elocus,
6183 last, NOP_EXPR, decl,
6184 tf_warning_or_error));
6185 add_stmt (orig_body);
6186 *body = pop_stmt_list (*body);
6188 if (c)
6190 OMP_CLAUSE_LASTPRIVATE_STMT (c) = push_stmt_list ();
6191 finish_expr_stmt (iter_incr);
6192 OMP_CLAUSE_LASTPRIVATE_STMT (c)
6193 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (c));
6196 TREE_VEC_ELT (declv, i) = decl;
6197 TREE_VEC_ELT (initv, i) = init;
6198 TREE_VEC_ELT (condv, i) = cond;
6199 TREE_VEC_ELT (incrv, i) = incr;
6201 return false;
6204 /* Build and validate an OMP_FOR statement. CLAUSES, BODY, COND, INCR
6205 are directly for their associated operands in the statement. DECL
6206 and INIT are a combo; if DECL is NULL then INIT ought to be a
6207 MODIFY_EXPR, and the DECL should be extracted. PRE_BODY are
6208 optional statements that need to go before the loop into its
6209 sk_omp scope. */
6211 tree
6212 finish_omp_for (location_t locus, enum tree_code code, tree declv, tree initv,
6213 tree condv, tree incrv, tree body, tree pre_body, tree clauses)
6215 tree omp_for = NULL, orig_incr = NULL;
6216 tree decl, init, cond, incr;
6217 location_t elocus;
6218 int i;
6220 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (initv));
6221 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (condv));
6222 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (incrv));
6223 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
6225 decl = TREE_VEC_ELT (declv, i);
6226 init = TREE_VEC_ELT (initv, i);
6227 cond = TREE_VEC_ELT (condv, i);
6228 incr = TREE_VEC_ELT (incrv, i);
6229 elocus = locus;
6231 if (decl == NULL)
6233 if (init != NULL)
6234 switch (TREE_CODE (init))
6236 case MODIFY_EXPR:
6237 decl = TREE_OPERAND (init, 0);
6238 init = TREE_OPERAND (init, 1);
6239 break;
6240 case MODOP_EXPR:
6241 if (TREE_CODE (TREE_OPERAND (init, 1)) == NOP_EXPR)
6243 decl = TREE_OPERAND (init, 0);
6244 init = TREE_OPERAND (init, 2);
6246 break;
6247 default:
6248 break;
6251 if (decl == NULL)
6253 error_at (locus,
6254 "expected iteration declaration or initialization");
6255 return NULL;
6259 if (init && EXPR_HAS_LOCATION (init))
6260 elocus = EXPR_LOCATION (init);
6262 if (cond == NULL)
6264 error_at (elocus, "missing controlling predicate");
6265 return NULL;
6268 if (incr == NULL)
6270 error_at (elocus, "missing increment expression");
6271 return NULL;
6274 TREE_VEC_ELT (declv, i) = decl;
6275 TREE_VEC_ELT (initv, i) = init;
6278 if (dependent_omp_for_p (declv, initv, condv, incrv))
6280 tree stmt;
6282 stmt = make_node (code);
6284 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
6286 /* This is really just a place-holder. We'll be decomposing this
6287 again and going through the cp_build_modify_expr path below when
6288 we instantiate the thing. */
6289 TREE_VEC_ELT (initv, i)
6290 = build2 (MODIFY_EXPR, void_type_node, TREE_VEC_ELT (declv, i),
6291 TREE_VEC_ELT (initv, i));
6294 TREE_TYPE (stmt) = void_type_node;
6295 OMP_FOR_INIT (stmt) = initv;
6296 OMP_FOR_COND (stmt) = condv;
6297 OMP_FOR_INCR (stmt) = incrv;
6298 OMP_FOR_BODY (stmt) = body;
6299 OMP_FOR_PRE_BODY (stmt) = pre_body;
6300 OMP_FOR_CLAUSES (stmt) = clauses;
6302 SET_EXPR_LOCATION (stmt, locus);
6303 return add_stmt (stmt);
6306 if (processing_template_decl)
6307 orig_incr = make_tree_vec (TREE_VEC_LENGTH (incrv));
6309 for (i = 0; i < TREE_VEC_LENGTH (declv); )
6311 decl = TREE_VEC_ELT (declv, i);
6312 init = TREE_VEC_ELT (initv, i);
6313 cond = TREE_VEC_ELT (condv, i);
6314 incr = TREE_VEC_ELT (incrv, i);
6315 if (orig_incr)
6316 TREE_VEC_ELT (orig_incr, i) = incr;
6317 elocus = locus;
6319 if (init && EXPR_HAS_LOCATION (init))
6320 elocus = EXPR_LOCATION (init);
6322 if (!DECL_P (decl))
6324 error_at (elocus, "expected iteration declaration or initialization");
6325 return NULL;
6328 if (incr && TREE_CODE (incr) == MODOP_EXPR)
6330 if (orig_incr)
6331 TREE_VEC_ELT (orig_incr, i) = incr;
6332 incr = cp_build_modify_expr (TREE_OPERAND (incr, 0),
6333 TREE_CODE (TREE_OPERAND (incr, 1)),
6334 TREE_OPERAND (incr, 2),
6335 tf_warning_or_error);
6338 if (CLASS_TYPE_P (TREE_TYPE (decl)))
6340 if (code == OMP_SIMD)
6342 error_at (elocus, "%<#pragma omp simd%> used with class "
6343 "iteration variable %qE", decl);
6344 return NULL;
6346 if (handle_omp_for_class_iterator (i, locus, declv, initv, condv,
6347 incrv, &body, &pre_body, clauses))
6348 return NULL;
6349 continue;
6352 if (!INTEGRAL_TYPE_P (TREE_TYPE (decl))
6353 && !TYPE_PTR_P (TREE_TYPE (decl)))
6355 error_at (elocus, "invalid type for iteration variable %qE", decl);
6356 return NULL;
6359 if (!processing_template_decl)
6361 init = fold_build_cleanup_point_expr (TREE_TYPE (init), init);
6362 init = cp_build_modify_expr (decl, NOP_EXPR, init, tf_warning_or_error);
6364 else
6365 init = build2 (MODIFY_EXPR, void_type_node, decl, init);
6366 if (cond
6367 && TREE_SIDE_EFFECTS (cond)
6368 && COMPARISON_CLASS_P (cond)
6369 && !processing_template_decl)
6371 tree t = TREE_OPERAND (cond, 0);
6372 if (TREE_SIDE_EFFECTS (t)
6373 && t != decl
6374 && (TREE_CODE (t) != NOP_EXPR
6375 || TREE_OPERAND (t, 0) != decl))
6376 TREE_OPERAND (cond, 0)
6377 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6379 t = TREE_OPERAND (cond, 1);
6380 if (TREE_SIDE_EFFECTS (t)
6381 && t != decl
6382 && (TREE_CODE (t) != NOP_EXPR
6383 || TREE_OPERAND (t, 0) != decl))
6384 TREE_OPERAND (cond, 1)
6385 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6387 if (decl == error_mark_node || init == error_mark_node)
6388 return NULL;
6390 TREE_VEC_ELT (declv, i) = decl;
6391 TREE_VEC_ELT (initv, i) = init;
6392 TREE_VEC_ELT (condv, i) = cond;
6393 TREE_VEC_ELT (incrv, i) = incr;
6394 i++;
6397 if (IS_EMPTY_STMT (pre_body))
6398 pre_body = NULL;
6400 omp_for = c_finish_omp_for (locus, code, declv, initv, condv, incrv,
6401 body, pre_body);
6403 if (omp_for == NULL)
6404 return NULL;
6406 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INCR (omp_for)); i++)
6408 decl = TREE_OPERAND (TREE_VEC_ELT (OMP_FOR_INIT (omp_for), i), 0);
6409 incr = TREE_VEC_ELT (OMP_FOR_INCR (omp_for), i);
6411 if (TREE_CODE (incr) != MODIFY_EXPR)
6412 continue;
6414 if (TREE_SIDE_EFFECTS (TREE_OPERAND (incr, 1))
6415 && BINARY_CLASS_P (TREE_OPERAND (incr, 1))
6416 && !processing_template_decl)
6418 tree t = TREE_OPERAND (TREE_OPERAND (incr, 1), 0);
6419 if (TREE_SIDE_EFFECTS (t)
6420 && t != decl
6421 && (TREE_CODE (t) != NOP_EXPR
6422 || TREE_OPERAND (t, 0) != decl))
6423 TREE_OPERAND (TREE_OPERAND (incr, 1), 0)
6424 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6426 t = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
6427 if (TREE_SIDE_EFFECTS (t)
6428 && t != decl
6429 && (TREE_CODE (t) != NOP_EXPR
6430 || TREE_OPERAND (t, 0) != decl))
6431 TREE_OPERAND (TREE_OPERAND (incr, 1), 1)
6432 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6435 if (orig_incr)
6436 TREE_VEC_ELT (OMP_FOR_INCR (omp_for), i) = TREE_VEC_ELT (orig_incr, i);
6438 if (omp_for != NULL)
6439 OMP_FOR_CLAUSES (omp_for) = clauses;
6440 return omp_for;
6443 void
6444 finish_omp_atomic (enum tree_code code, enum tree_code opcode, tree lhs,
6445 tree rhs, tree v, tree lhs1, tree rhs1, bool seq_cst)
6447 tree orig_lhs;
6448 tree orig_rhs;
6449 tree orig_v;
6450 tree orig_lhs1;
6451 tree orig_rhs1;
6452 bool dependent_p;
6453 tree stmt;
6455 orig_lhs = lhs;
6456 orig_rhs = rhs;
6457 orig_v = v;
6458 orig_lhs1 = lhs1;
6459 orig_rhs1 = rhs1;
6460 dependent_p = false;
6461 stmt = NULL_TREE;
6463 /* Even in a template, we can detect invalid uses of the atomic
6464 pragma if neither LHS nor RHS is type-dependent. */
6465 if (processing_template_decl)
6467 dependent_p = (type_dependent_expression_p (lhs)
6468 || (rhs && type_dependent_expression_p (rhs))
6469 || (v && type_dependent_expression_p (v))
6470 || (lhs1 && type_dependent_expression_p (lhs1))
6471 || (rhs1 && type_dependent_expression_p (rhs1)));
6472 if (!dependent_p)
6474 lhs = build_non_dependent_expr (lhs);
6475 if (rhs)
6476 rhs = build_non_dependent_expr (rhs);
6477 if (v)
6478 v = build_non_dependent_expr (v);
6479 if (lhs1)
6480 lhs1 = build_non_dependent_expr (lhs1);
6481 if (rhs1)
6482 rhs1 = build_non_dependent_expr (rhs1);
6485 if (!dependent_p)
6487 bool swapped = false;
6488 if (rhs1 && cp_tree_equal (lhs, rhs))
6490 tree tem = rhs;
6491 rhs = rhs1;
6492 rhs1 = tem;
6493 swapped = !commutative_tree_code (opcode);
6495 if (rhs1 && !cp_tree_equal (lhs, rhs1))
6497 if (code == OMP_ATOMIC)
6498 error ("%<#pragma omp atomic update%> uses two different "
6499 "expressions for memory");
6500 else
6501 error ("%<#pragma omp atomic capture%> uses two different "
6502 "expressions for memory");
6503 return;
6505 if (lhs1 && !cp_tree_equal (lhs, lhs1))
6507 if (code == OMP_ATOMIC)
6508 error ("%<#pragma omp atomic update%> uses two different "
6509 "expressions for memory");
6510 else
6511 error ("%<#pragma omp atomic capture%> uses two different "
6512 "expressions for memory");
6513 return;
6515 stmt = c_finish_omp_atomic (input_location, code, opcode, lhs, rhs,
6516 v, lhs1, rhs1, swapped, seq_cst);
6517 if (stmt == error_mark_node)
6518 return;
6520 if (processing_template_decl)
6522 if (code == OMP_ATOMIC_READ)
6524 stmt = build_min_nt_loc (EXPR_LOCATION (orig_lhs),
6525 OMP_ATOMIC_READ, orig_lhs);
6526 OMP_ATOMIC_SEQ_CST (stmt) = seq_cst;
6527 stmt = build2 (MODIFY_EXPR, void_type_node, orig_v, stmt);
6529 else
6531 if (opcode == NOP_EXPR)
6532 stmt = build2 (MODIFY_EXPR, void_type_node, orig_lhs, orig_rhs);
6533 else
6534 stmt = build2 (opcode, void_type_node, orig_lhs, orig_rhs);
6535 if (orig_rhs1)
6536 stmt = build_min_nt_loc (EXPR_LOCATION (orig_rhs1),
6537 COMPOUND_EXPR, orig_rhs1, stmt);
6538 if (code != OMP_ATOMIC)
6540 stmt = build_min_nt_loc (EXPR_LOCATION (orig_lhs1),
6541 code, orig_lhs1, stmt);
6542 OMP_ATOMIC_SEQ_CST (stmt) = seq_cst;
6543 stmt = build2 (MODIFY_EXPR, void_type_node, orig_v, stmt);
6546 stmt = build2 (OMP_ATOMIC, void_type_node, integer_zero_node, stmt);
6547 OMP_ATOMIC_SEQ_CST (stmt) = seq_cst;
6549 add_stmt (stmt);
6552 void
6553 finish_omp_barrier (void)
6555 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_BARRIER);
6556 vec<tree, va_gc> *vec = make_tree_vector ();
6557 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
6558 release_tree_vector (vec);
6559 finish_expr_stmt (stmt);
6562 void
6563 finish_omp_flush (void)
6565 tree fn = builtin_decl_explicit (BUILT_IN_SYNC_SYNCHRONIZE);
6566 vec<tree, va_gc> *vec = make_tree_vector ();
6567 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
6568 release_tree_vector (vec);
6569 finish_expr_stmt (stmt);
6572 void
6573 finish_omp_taskwait (void)
6575 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_TASKWAIT);
6576 vec<tree, va_gc> *vec = make_tree_vector ();
6577 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
6578 release_tree_vector (vec);
6579 finish_expr_stmt (stmt);
6582 void
6583 finish_omp_taskyield (void)
6585 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_TASKYIELD);
6586 vec<tree, va_gc> *vec = make_tree_vector ();
6587 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
6588 release_tree_vector (vec);
6589 finish_expr_stmt (stmt);
6592 void
6593 finish_omp_cancel (tree clauses)
6595 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
6596 int mask = 0;
6597 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
6598 mask = 1;
6599 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
6600 mask = 2;
6601 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
6602 mask = 4;
6603 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
6604 mask = 8;
6605 else
6607 error ("%<#pragma omp cancel must specify one of "
6608 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> clauses");
6609 return;
6611 vec<tree, va_gc> *vec = make_tree_vector ();
6612 tree ifc = find_omp_clause (clauses, OMP_CLAUSE_IF);
6613 if (ifc != NULL_TREE)
6615 tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
6616 ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
6617 boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
6618 build_zero_cst (type));
6620 else
6621 ifc = boolean_true_node;
6622 vec->quick_push (build_int_cst (integer_type_node, mask));
6623 vec->quick_push (ifc);
6624 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
6625 release_tree_vector (vec);
6626 finish_expr_stmt (stmt);
6629 void
6630 finish_omp_cancellation_point (tree clauses)
6632 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
6633 int mask = 0;
6634 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
6635 mask = 1;
6636 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
6637 mask = 2;
6638 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
6639 mask = 4;
6640 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
6641 mask = 8;
6642 else
6644 error ("%<#pragma omp cancellation point must specify one of "
6645 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> clauses");
6646 return;
6648 vec<tree, va_gc> *vec
6649 = make_tree_vector_single (build_int_cst (integer_type_node, mask));
6650 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
6651 release_tree_vector (vec);
6652 finish_expr_stmt (stmt);
6655 /* Begin a __transaction_atomic or __transaction_relaxed statement.
6656 If PCOMPOUND is non-null, this is for a function-transaction-block, and we
6657 should create an extra compound stmt. */
6659 tree
6660 begin_transaction_stmt (location_t loc, tree *pcompound, int flags)
6662 tree r;
6664 if (pcompound)
6665 *pcompound = begin_compound_stmt (0);
6667 r = build_stmt (loc, TRANSACTION_EXPR, NULL_TREE);
6669 /* Only add the statement to the function if support enabled. */
6670 if (flag_tm)
6671 add_stmt (r);
6672 else
6673 error_at (loc, ((flags & TM_STMT_ATTR_RELAXED) != 0
6674 ? G_("%<__transaction_relaxed%> without "
6675 "transactional memory support enabled")
6676 : G_("%<__transaction_atomic%> without "
6677 "transactional memory support enabled")));
6679 TRANSACTION_EXPR_BODY (r) = push_stmt_list ();
6680 TREE_SIDE_EFFECTS (r) = 1;
6681 return r;
6684 /* End a __transaction_atomic or __transaction_relaxed statement.
6685 If COMPOUND_STMT is non-null, this is for a function-transaction-block,
6686 and we should end the compound. If NOEX is non-NULL, we wrap the body in
6687 a MUST_NOT_THROW_EXPR with NOEX as condition. */
6689 void
6690 finish_transaction_stmt (tree stmt, tree compound_stmt, int flags, tree noex)
6692 TRANSACTION_EXPR_BODY (stmt) = pop_stmt_list (TRANSACTION_EXPR_BODY (stmt));
6693 TRANSACTION_EXPR_OUTER (stmt) = (flags & TM_STMT_ATTR_OUTER) != 0;
6694 TRANSACTION_EXPR_RELAXED (stmt) = (flags & TM_STMT_ATTR_RELAXED) != 0;
6695 TRANSACTION_EXPR_IS_STMT (stmt) = 1;
6697 /* noexcept specifications are not allowed for function transactions. */
6698 gcc_assert (!(noex && compound_stmt));
6699 if (noex)
6701 tree body = build_must_not_throw_expr (TRANSACTION_EXPR_BODY (stmt),
6702 noex);
6703 /* This may not be true when the STATEMENT_LIST is empty. */
6704 if (EXPR_P (body))
6705 SET_EXPR_LOCATION (body, EXPR_LOCATION (TRANSACTION_EXPR_BODY (stmt)));
6706 TREE_SIDE_EFFECTS (body) = 1;
6707 TRANSACTION_EXPR_BODY (stmt) = body;
6710 if (compound_stmt)
6711 finish_compound_stmt (compound_stmt);
6714 /* Build a __transaction_atomic or __transaction_relaxed expression. If
6715 NOEX is non-NULL, we wrap the body in a MUST_NOT_THROW_EXPR with NOEX as
6716 condition. */
6718 tree
6719 build_transaction_expr (location_t loc, tree expr, int flags, tree noex)
6721 tree ret;
6722 if (noex)
6724 expr = build_must_not_throw_expr (expr, noex);
6725 if (EXPR_P (expr))
6726 SET_EXPR_LOCATION (expr, loc);
6727 TREE_SIDE_EFFECTS (expr) = 1;
6729 ret = build1 (TRANSACTION_EXPR, TREE_TYPE (expr), expr);
6730 if (flags & TM_STMT_ATTR_RELAXED)
6731 TRANSACTION_EXPR_RELAXED (ret) = 1;
6732 TREE_SIDE_EFFECTS (ret) = 1;
6733 SET_EXPR_LOCATION (ret, loc);
6734 return ret;
6737 void
6738 init_cp_semantics (void)
6742 /* Build a STATIC_ASSERT for a static assertion with the condition
6743 CONDITION and the message text MESSAGE. LOCATION is the location
6744 of the static assertion in the source code. When MEMBER_P, this
6745 static assertion is a member of a class. */
6746 void
6747 finish_static_assert (tree condition, tree message, location_t location,
6748 bool member_p)
6750 if (message == NULL_TREE
6751 || message == error_mark_node
6752 || condition == NULL_TREE
6753 || condition == error_mark_node)
6754 return;
6756 if (check_for_bare_parameter_packs (condition))
6757 condition = error_mark_node;
6759 if (type_dependent_expression_p (condition)
6760 || value_dependent_expression_p (condition))
6762 /* We're in a template; build a STATIC_ASSERT and put it in
6763 the right place. */
6764 tree assertion;
6766 assertion = make_node (STATIC_ASSERT);
6767 STATIC_ASSERT_CONDITION (assertion) = condition;
6768 STATIC_ASSERT_MESSAGE (assertion) = message;
6769 STATIC_ASSERT_SOURCE_LOCATION (assertion) = location;
6771 if (member_p)
6772 maybe_add_class_template_decl_list (current_class_type,
6773 assertion,
6774 /*friend_p=*/0);
6775 else
6776 add_stmt (assertion);
6778 return;
6781 /* Fold the expression and convert it to a boolean value. */
6782 condition = fold_non_dependent_expr (condition);
6783 condition = cp_convert (boolean_type_node, condition, tf_warning_or_error);
6784 condition = maybe_constant_value (condition);
6786 if (TREE_CODE (condition) == INTEGER_CST && !integer_zerop (condition))
6787 /* Do nothing; the condition is satisfied. */
6789 else
6791 location_t saved_loc = input_location;
6793 input_location = location;
6794 if (TREE_CODE (condition) == INTEGER_CST
6795 && integer_zerop (condition))
6796 /* Report the error. */
6797 error ("static assertion failed: %s", TREE_STRING_POINTER (message));
6798 else if (condition && condition != error_mark_node)
6800 error ("non-constant condition for static assertion");
6801 cxx_constant_value (condition);
6803 input_location = saved_loc;
6807 /* Implements the C++0x decltype keyword. Returns the type of EXPR,
6808 suitable for use as a type-specifier.
6810 ID_EXPRESSION_OR_MEMBER_ACCESS_P is true when EXPR was parsed as an
6811 id-expression or a class member access, FALSE when it was parsed as
6812 a full expression. */
6814 tree
6815 finish_decltype_type (tree expr, bool id_expression_or_member_access_p,
6816 tsubst_flags_t complain)
6818 tree type = NULL_TREE;
6820 if (!expr || error_operand_p (expr))
6821 return error_mark_node;
6823 if (TYPE_P (expr)
6824 || TREE_CODE (expr) == TYPE_DECL
6825 || (TREE_CODE (expr) == BIT_NOT_EXPR
6826 && TYPE_P (TREE_OPERAND (expr, 0))))
6828 if (complain & tf_error)
6829 error ("argument to decltype must be an expression");
6830 return error_mark_node;
6833 /* Depending on the resolution of DR 1172, we may later need to distinguish
6834 instantiation-dependent but not type-dependent expressions so that, say,
6835 A<decltype(sizeof(T))>::U doesn't require 'typename'. */
6836 if (instantiation_dependent_expression_p (expr))
6838 type = cxx_make_type (DECLTYPE_TYPE);
6839 DECLTYPE_TYPE_EXPR (type) = expr;
6840 DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (type)
6841 = id_expression_or_member_access_p;
6842 SET_TYPE_STRUCTURAL_EQUALITY (type);
6844 return type;
6847 /* The type denoted by decltype(e) is defined as follows: */
6849 expr = resolve_nondeduced_context (expr);
6851 if (invalid_nonstatic_memfn_p (expr, complain))
6852 return error_mark_node;
6854 if (type_unknown_p (expr))
6856 if (complain & tf_error)
6857 error ("decltype cannot resolve address of overloaded function");
6858 return error_mark_node;
6861 /* To get the size of a static data member declared as an array of
6862 unknown bound, we need to instantiate it. */
6863 if (VAR_P (expr)
6864 && VAR_HAD_UNKNOWN_BOUND (expr)
6865 && DECL_TEMPLATE_INSTANTIATION (expr))
6866 instantiate_decl (expr, /*defer_ok*/true, /*expl_inst_mem*/false);
6868 if (id_expression_or_member_access_p)
6870 /* If e is an id-expression or a class member access (5.2.5
6871 [expr.ref]), decltype(e) is defined as the type of the entity
6872 named by e. If there is no such entity, or e names a set of
6873 overloaded functions, the program is ill-formed. */
6874 if (identifier_p (expr))
6875 expr = lookup_name (expr);
6877 if (INDIRECT_REF_P (expr))
6878 /* This can happen when the expression is, e.g., "a.b". Just
6879 look at the underlying operand. */
6880 expr = TREE_OPERAND (expr, 0);
6882 if (TREE_CODE (expr) == OFFSET_REF
6883 || TREE_CODE (expr) == MEMBER_REF
6884 || TREE_CODE (expr) == SCOPE_REF)
6885 /* We're only interested in the field itself. If it is a
6886 BASELINK, we will need to see through it in the next
6887 step. */
6888 expr = TREE_OPERAND (expr, 1);
6890 if (BASELINK_P (expr))
6891 /* See through BASELINK nodes to the underlying function. */
6892 expr = BASELINK_FUNCTIONS (expr);
6894 switch (TREE_CODE (expr))
6896 case FIELD_DECL:
6897 if (DECL_BIT_FIELD_TYPE (expr))
6899 type = DECL_BIT_FIELD_TYPE (expr);
6900 break;
6902 /* Fall through for fields that aren't bitfields. */
6904 case FUNCTION_DECL:
6905 case VAR_DECL:
6906 case CONST_DECL:
6907 case PARM_DECL:
6908 case RESULT_DECL:
6909 case TEMPLATE_PARM_INDEX:
6910 expr = mark_type_use (expr);
6911 type = TREE_TYPE (expr);
6912 break;
6914 case ERROR_MARK:
6915 type = error_mark_node;
6916 break;
6918 case COMPONENT_REF:
6919 case COMPOUND_EXPR:
6920 mark_type_use (expr);
6921 type = is_bitfield_expr_with_lowered_type (expr);
6922 if (!type)
6923 type = TREE_TYPE (TREE_OPERAND (expr, 1));
6924 break;
6926 case BIT_FIELD_REF:
6927 gcc_unreachable ();
6929 case INTEGER_CST:
6930 case PTRMEM_CST:
6931 /* We can get here when the id-expression refers to an
6932 enumerator or non-type template parameter. */
6933 type = TREE_TYPE (expr);
6934 break;
6936 default:
6937 /* Handle instantiated template non-type arguments. */
6938 type = TREE_TYPE (expr);
6939 break;
6942 else
6944 /* Within a lambda-expression:
6946 Every occurrence of decltype((x)) where x is a possibly
6947 parenthesized id-expression that names an entity of
6948 automatic storage duration is treated as if x were
6949 transformed into an access to a corresponding data member
6950 of the closure type that would have been declared if x
6951 were a use of the denoted entity. */
6952 if (outer_automatic_var_p (expr)
6953 && current_function_decl
6954 && LAMBDA_FUNCTION_P (current_function_decl))
6955 type = capture_decltype (expr);
6956 else if (error_operand_p (expr))
6957 type = error_mark_node;
6958 else if (expr == current_class_ptr)
6959 /* If the expression is just "this", we want the
6960 cv-unqualified pointer for the "this" type. */
6961 type = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
6962 else
6964 /* Otherwise, where T is the type of e, if e is an lvalue,
6965 decltype(e) is defined as T&; if an xvalue, T&&; otherwise, T. */
6966 cp_lvalue_kind clk = lvalue_kind (expr);
6967 type = unlowered_expr_type (expr);
6968 gcc_assert (TREE_CODE (type) != REFERENCE_TYPE);
6970 /* For vector types, pick a non-opaque variant. */
6971 if (TREE_CODE (type) == VECTOR_TYPE)
6972 type = strip_typedefs (type);
6974 if (clk != clk_none && !(clk & clk_class))
6975 type = cp_build_reference_type (type, (clk & clk_rvalueref));
6979 if (cxx_dialect >= cxx1y && array_of_runtime_bound_p (type))
6981 if (complain & tf_warning_or_error)
6982 pedwarn (input_location, OPT_Wvla,
6983 "taking decltype of array of runtime bound");
6984 else
6985 return error_mark_node;
6988 return type;
6991 /* Called from trait_expr_value to evaluate either __has_nothrow_assign or
6992 __has_nothrow_copy, depending on assign_p. */
6994 static bool
6995 classtype_has_nothrow_assign_or_copy_p (tree type, bool assign_p)
6997 tree fns;
6999 if (assign_p)
7001 int ix;
7002 ix = lookup_fnfields_1 (type, ansi_assopname (NOP_EXPR));
7003 if (ix < 0)
7004 return false;
7005 fns = (*CLASSTYPE_METHOD_VEC (type))[ix];
7007 else if (TYPE_HAS_COPY_CTOR (type))
7009 /* If construction of the copy constructor was postponed, create
7010 it now. */
7011 if (CLASSTYPE_LAZY_COPY_CTOR (type))
7012 lazily_declare_fn (sfk_copy_constructor, type);
7013 if (CLASSTYPE_LAZY_MOVE_CTOR (type))
7014 lazily_declare_fn (sfk_move_constructor, type);
7015 fns = CLASSTYPE_CONSTRUCTORS (type);
7017 else
7018 return false;
7020 for (; fns; fns = OVL_NEXT (fns))
7022 tree fn = OVL_CURRENT (fns);
7024 if (assign_p)
7026 if (copy_fn_p (fn) == 0)
7027 continue;
7029 else if (copy_fn_p (fn) <= 0)
7030 continue;
7032 maybe_instantiate_noexcept (fn);
7033 if (!TYPE_NOTHROW_P (TREE_TYPE (fn)))
7034 return false;
7037 return true;
7040 /* Actually evaluates the trait. */
7042 static bool
7043 trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
7045 enum tree_code type_code1;
7046 tree t;
7048 type_code1 = TREE_CODE (type1);
7050 switch (kind)
7052 case CPTK_HAS_NOTHROW_ASSIGN:
7053 type1 = strip_array_types (type1);
7054 return (!CP_TYPE_CONST_P (type1) && type_code1 != REFERENCE_TYPE
7055 && (trait_expr_value (CPTK_HAS_TRIVIAL_ASSIGN, type1, type2)
7056 || (CLASS_TYPE_P (type1)
7057 && classtype_has_nothrow_assign_or_copy_p (type1,
7058 true))));
7060 case CPTK_HAS_TRIVIAL_ASSIGN:
7061 /* ??? The standard seems to be missing the "or array of such a class
7062 type" wording for this trait. */
7063 type1 = strip_array_types (type1);
7064 return (!CP_TYPE_CONST_P (type1) && type_code1 != REFERENCE_TYPE
7065 && (trivial_type_p (type1)
7066 || (CLASS_TYPE_P (type1)
7067 && TYPE_HAS_TRIVIAL_COPY_ASSIGN (type1))));
7069 case CPTK_HAS_NOTHROW_CONSTRUCTOR:
7070 type1 = strip_array_types (type1);
7071 return (trait_expr_value (CPTK_HAS_TRIVIAL_CONSTRUCTOR, type1, type2)
7072 || (CLASS_TYPE_P (type1)
7073 && (t = locate_ctor (type1))
7074 && (maybe_instantiate_noexcept (t),
7075 TYPE_NOTHROW_P (TREE_TYPE (t)))));
7077 case CPTK_HAS_TRIVIAL_CONSTRUCTOR:
7078 type1 = strip_array_types (type1);
7079 return (trivial_type_p (type1)
7080 || (CLASS_TYPE_P (type1) && TYPE_HAS_TRIVIAL_DFLT (type1)));
7082 case CPTK_HAS_NOTHROW_COPY:
7083 type1 = strip_array_types (type1);
7084 return (trait_expr_value (CPTK_HAS_TRIVIAL_COPY, type1, type2)
7085 || (CLASS_TYPE_P (type1)
7086 && classtype_has_nothrow_assign_or_copy_p (type1, false)));
7088 case CPTK_HAS_TRIVIAL_COPY:
7089 /* ??? The standard seems to be missing the "or array of such a class
7090 type" wording for this trait. */
7091 type1 = strip_array_types (type1);
7092 return (trivial_type_p (type1) || type_code1 == REFERENCE_TYPE
7093 || (CLASS_TYPE_P (type1) && TYPE_HAS_TRIVIAL_COPY_CTOR (type1)));
7095 case CPTK_HAS_TRIVIAL_DESTRUCTOR:
7096 type1 = strip_array_types (type1);
7097 return (trivial_type_p (type1) || type_code1 == REFERENCE_TYPE
7098 || (CLASS_TYPE_P (type1)
7099 && TYPE_HAS_TRIVIAL_DESTRUCTOR (type1)));
7101 case CPTK_HAS_VIRTUAL_DESTRUCTOR:
7102 return type_has_virtual_destructor (type1);
7104 case CPTK_IS_ABSTRACT:
7105 return (ABSTRACT_CLASS_TYPE_P (type1));
7107 case CPTK_IS_BASE_OF:
7108 return (NON_UNION_CLASS_TYPE_P (type1) && NON_UNION_CLASS_TYPE_P (type2)
7109 && DERIVED_FROM_P (type1, type2));
7111 case CPTK_IS_CLASS:
7112 return (NON_UNION_CLASS_TYPE_P (type1));
7114 case CPTK_IS_CONVERTIBLE_TO:
7115 /* TODO */
7116 return false;
7118 case CPTK_IS_EMPTY:
7119 return (NON_UNION_CLASS_TYPE_P (type1) && CLASSTYPE_EMPTY_P (type1));
7121 case CPTK_IS_ENUM:
7122 return (type_code1 == ENUMERAL_TYPE);
7124 case CPTK_IS_FINAL:
7125 return (CLASS_TYPE_P (type1) && CLASSTYPE_FINAL (type1));
7127 case CPTK_IS_LITERAL_TYPE:
7128 return (literal_type_p (type1));
7130 case CPTK_IS_POD:
7131 return (pod_type_p (type1));
7133 case CPTK_IS_POLYMORPHIC:
7134 return (CLASS_TYPE_P (type1) && TYPE_POLYMORPHIC_P (type1));
7136 case CPTK_IS_STD_LAYOUT:
7137 return (std_layout_type_p (type1));
7139 case CPTK_IS_TRIVIAL:
7140 return (trivial_type_p (type1));
7142 case CPTK_IS_UNION:
7143 return (type_code1 == UNION_TYPE);
7145 default:
7146 gcc_unreachable ();
7147 return false;
7151 /* If TYPE is an array of unknown bound, or (possibly cv-qualified)
7152 void, or a complete type, returns it, otherwise NULL_TREE. */
7154 static tree
7155 check_trait_type (tree type)
7157 if (TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type)
7158 && COMPLETE_TYPE_P (TREE_TYPE (type)))
7159 return type;
7161 if (VOID_TYPE_P (type))
7162 return type;
7164 return complete_type_or_else (strip_array_types (type), NULL_TREE);
7167 /* Process a trait expression. */
7169 tree
7170 finish_trait_expr (cp_trait_kind kind, tree type1, tree type2)
7172 gcc_assert (kind == CPTK_HAS_NOTHROW_ASSIGN
7173 || kind == CPTK_HAS_NOTHROW_CONSTRUCTOR
7174 || kind == CPTK_HAS_NOTHROW_COPY
7175 || kind == CPTK_HAS_TRIVIAL_ASSIGN
7176 || kind == CPTK_HAS_TRIVIAL_CONSTRUCTOR
7177 || kind == CPTK_HAS_TRIVIAL_COPY
7178 || kind == CPTK_HAS_TRIVIAL_DESTRUCTOR
7179 || kind == CPTK_HAS_VIRTUAL_DESTRUCTOR
7180 || kind == CPTK_IS_ABSTRACT
7181 || kind == CPTK_IS_BASE_OF
7182 || kind == CPTK_IS_CLASS
7183 || kind == CPTK_IS_CONVERTIBLE_TO
7184 || kind == CPTK_IS_EMPTY
7185 || kind == CPTK_IS_ENUM
7186 || kind == CPTK_IS_FINAL
7187 || kind == CPTK_IS_LITERAL_TYPE
7188 || kind == CPTK_IS_POD
7189 || kind == CPTK_IS_POLYMORPHIC
7190 || kind == CPTK_IS_STD_LAYOUT
7191 || kind == CPTK_IS_TRIVIAL
7192 || kind == CPTK_IS_UNION);
7194 if (kind == CPTK_IS_CONVERTIBLE_TO)
7196 sorry ("__is_convertible_to");
7197 return error_mark_node;
7200 if (type1 == error_mark_node
7201 || ((kind == CPTK_IS_BASE_OF || kind == CPTK_IS_CONVERTIBLE_TO)
7202 && type2 == error_mark_node))
7203 return error_mark_node;
7205 if (processing_template_decl)
7207 tree trait_expr = make_node (TRAIT_EXPR);
7208 TREE_TYPE (trait_expr) = boolean_type_node;
7209 TRAIT_EXPR_TYPE1 (trait_expr) = type1;
7210 TRAIT_EXPR_TYPE2 (trait_expr) = type2;
7211 TRAIT_EXPR_KIND (trait_expr) = kind;
7212 return trait_expr;
7215 switch (kind)
7217 case CPTK_HAS_NOTHROW_ASSIGN:
7218 case CPTK_HAS_TRIVIAL_ASSIGN:
7219 case CPTK_HAS_NOTHROW_CONSTRUCTOR:
7220 case CPTK_HAS_TRIVIAL_CONSTRUCTOR:
7221 case CPTK_HAS_NOTHROW_COPY:
7222 case CPTK_HAS_TRIVIAL_COPY:
7223 case CPTK_HAS_TRIVIAL_DESTRUCTOR:
7224 case CPTK_HAS_VIRTUAL_DESTRUCTOR:
7225 case CPTK_IS_ABSTRACT:
7226 case CPTK_IS_EMPTY:
7227 case CPTK_IS_FINAL:
7228 case CPTK_IS_LITERAL_TYPE:
7229 case CPTK_IS_POD:
7230 case CPTK_IS_POLYMORPHIC:
7231 case CPTK_IS_STD_LAYOUT:
7232 case CPTK_IS_TRIVIAL:
7233 if (!check_trait_type (type1))
7234 return error_mark_node;
7235 break;
7237 case CPTK_IS_BASE_OF:
7238 if (NON_UNION_CLASS_TYPE_P (type1) && NON_UNION_CLASS_TYPE_P (type2)
7239 && !same_type_ignoring_top_level_qualifiers_p (type1, type2)
7240 && !complete_type_or_else (type2, NULL_TREE))
7241 /* We already issued an error. */
7242 return error_mark_node;
7243 break;
7245 case CPTK_IS_CLASS:
7246 case CPTK_IS_ENUM:
7247 case CPTK_IS_UNION:
7248 break;
7250 case CPTK_IS_CONVERTIBLE_TO:
7251 default:
7252 gcc_unreachable ();
7255 return (trait_expr_value (kind, type1, type2)
7256 ? boolean_true_node : boolean_false_node);
7259 /* Do-nothing variants of functions to handle pragma FLOAT_CONST_DECIMAL64,
7260 which is ignored for C++. */
7262 void
7263 set_float_const_decimal64 (void)
7267 void
7268 clear_float_const_decimal64 (void)
7272 bool
7273 float_const_decimal64_p (void)
7275 return 0;
7279 /* Return true if T is a literal type. */
7281 bool
7282 literal_type_p (tree t)
7284 if (SCALAR_TYPE_P (t)
7285 || TREE_CODE (t) == VECTOR_TYPE
7286 || TREE_CODE (t) == REFERENCE_TYPE)
7287 return true;
7288 if (CLASS_TYPE_P (t))
7290 t = complete_type (t);
7291 gcc_assert (COMPLETE_TYPE_P (t) || errorcount);
7292 return CLASSTYPE_LITERAL_P (t);
7294 if (TREE_CODE (t) == ARRAY_TYPE)
7295 return literal_type_p (strip_array_types (t));
7296 return false;
7299 /* If DECL is a variable declared `constexpr', require its type
7300 be literal. Return the DECL if OK, otherwise NULL. */
7302 tree
7303 ensure_literal_type_for_constexpr_object (tree decl)
7305 tree type = TREE_TYPE (decl);
7306 if (VAR_P (decl) && DECL_DECLARED_CONSTEXPR_P (decl)
7307 && !processing_template_decl)
7309 if (CLASS_TYPE_P (type) && !COMPLETE_TYPE_P (complete_type (type)))
7310 /* Don't complain here, we'll complain about incompleteness
7311 when we try to initialize the variable. */;
7312 else if (!literal_type_p (type))
7314 error ("the type %qT of constexpr variable %qD is not literal",
7315 type, decl);
7316 explain_non_literal_class (type);
7317 return NULL;
7320 return decl;
7323 /* Representation of entries in the constexpr function definition table. */
7325 typedef struct GTY(()) constexpr_fundef {
7326 tree decl;
7327 tree body;
7328 } constexpr_fundef;
7330 /* This table holds all constexpr function definitions seen in
7331 the current translation unit. */
7333 static GTY ((param_is (constexpr_fundef))) htab_t constexpr_fundef_table;
7335 /* Utility function used for managing the constexpr function table.
7336 Return true if the entries pointed to by P and Q are for the
7337 same constexpr function. */
7339 static inline int
7340 constexpr_fundef_equal (const void *p, const void *q)
7342 const constexpr_fundef *lhs = (const constexpr_fundef *) p;
7343 const constexpr_fundef *rhs = (const constexpr_fundef *) q;
7344 return lhs->decl == rhs->decl;
7347 /* Utility function used for managing the constexpr function table.
7348 Return a hash value for the entry pointed to by Q. */
7350 static inline hashval_t
7351 constexpr_fundef_hash (const void *p)
7353 const constexpr_fundef *fundef = (const constexpr_fundef *) p;
7354 return DECL_UID (fundef->decl);
7357 /* Return a previously saved definition of function FUN. */
7359 static constexpr_fundef *
7360 retrieve_constexpr_fundef (tree fun)
7362 constexpr_fundef fundef = { NULL, NULL };
7363 if (constexpr_fundef_table == NULL)
7364 return NULL;
7366 fundef.decl = fun;
7367 return (constexpr_fundef *) htab_find (constexpr_fundef_table, &fundef);
7370 /* Check whether the parameter and return types of FUN are valid for a
7371 constexpr function, and complain if COMPLAIN. */
7373 static bool
7374 is_valid_constexpr_fn (tree fun, bool complain)
7376 tree parm = FUNCTION_FIRST_USER_PARM (fun);
7377 bool ret = true;
7378 for (; parm != NULL; parm = TREE_CHAIN (parm))
7379 if (!literal_type_p (TREE_TYPE (parm)))
7381 ret = false;
7382 if (complain)
7384 error ("invalid type for parameter %d of constexpr "
7385 "function %q+#D", DECL_PARM_INDEX (parm), fun);
7386 explain_non_literal_class (TREE_TYPE (parm));
7390 if (!DECL_CONSTRUCTOR_P (fun))
7392 tree rettype = TREE_TYPE (TREE_TYPE (fun));
7393 if (!literal_type_p (rettype))
7395 ret = false;
7396 if (complain)
7398 error ("invalid return type %qT of constexpr function %q+D",
7399 rettype, fun);
7400 explain_non_literal_class (rettype);
7404 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fun)
7405 && !CLASSTYPE_LITERAL_P (DECL_CONTEXT (fun)))
7407 ret = false;
7408 if (complain)
7410 error ("enclosing class of constexpr non-static member "
7411 "function %q+#D is not a literal type", fun);
7412 explain_non_literal_class (DECL_CONTEXT (fun));
7416 else if (CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fun)))
7418 ret = false;
7419 if (complain)
7420 error ("%q#T has virtual base classes", DECL_CONTEXT (fun));
7423 return ret;
7426 /* Subroutine of build_data_member_initialization. MEMBER is a COMPONENT_REF
7427 for a member of an anonymous aggregate, INIT is the initializer for that
7428 member, and VEC_OUTER is the vector of constructor elements for the class
7429 whose constructor we are processing. Add the initializer to the vector
7430 and return true to indicate success. */
7432 static bool
7433 build_anon_member_initialization (tree member, tree init,
7434 vec<constructor_elt, va_gc> **vec_outer)
7436 /* MEMBER presents the relevant fields from the inside out, but we need
7437 to build up the initializer from the outside in so that we can reuse
7438 previously built CONSTRUCTORs if this is, say, the second field in an
7439 anonymous struct. So we use a vec as a stack. */
7440 stack_vec<tree, 2> fields;
7443 fields.safe_push (TREE_OPERAND (member, 1));
7444 member = TREE_OPERAND (member, 0);
7446 while (ANON_AGGR_TYPE_P (TREE_TYPE (member)));
7448 /* VEC has the constructor elements vector for the context of FIELD.
7449 If FIELD is an anonymous aggregate, we will push inside it. */
7450 vec<constructor_elt, va_gc> **vec = vec_outer;
7451 tree field;
7452 while (field = fields.pop(),
7453 ANON_AGGR_TYPE_P (TREE_TYPE (field)))
7455 tree ctor;
7456 /* If there is already an outer constructor entry for the anonymous
7457 aggregate FIELD, use it; otherwise, insert one. */
7458 if (vec_safe_is_empty (*vec)
7459 || (*vec)->last().index != field)
7461 ctor = build_constructor (TREE_TYPE (field), NULL);
7462 CONSTRUCTOR_APPEND_ELT (*vec, field, ctor);
7464 else
7465 ctor = (*vec)->last().value;
7466 vec = &CONSTRUCTOR_ELTS (ctor);
7469 /* Now we're at the innermost field, the one that isn't an anonymous
7470 aggregate. Add its initializer to the CONSTRUCTOR and we're done. */
7471 gcc_assert (fields.is_empty());
7472 CONSTRUCTOR_APPEND_ELT (*vec, field, init);
7474 return true;
7477 /* Subroutine of build_constexpr_constructor_member_initializers.
7478 The expression tree T represents a data member initialization
7479 in a (constexpr) constructor definition. Build a pairing of
7480 the data member with its initializer, and prepend that pair
7481 to the existing initialization pair INITS. */
7483 static bool
7484 build_data_member_initialization (tree t, vec<constructor_elt, va_gc> **vec)
7486 tree member, init;
7487 if (TREE_CODE (t) == CLEANUP_POINT_EXPR)
7488 t = TREE_OPERAND (t, 0);
7489 if (TREE_CODE (t) == EXPR_STMT)
7490 t = TREE_OPERAND (t, 0);
7491 if (t == error_mark_node)
7492 return false;
7493 if (TREE_CODE (t) == STATEMENT_LIST)
7495 tree_stmt_iterator i;
7496 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
7498 if (! build_data_member_initialization (tsi_stmt (i), vec))
7499 return false;
7501 return true;
7503 if (TREE_CODE (t) == CLEANUP_STMT)
7505 /* We can't see a CLEANUP_STMT in a constructor for a literal class,
7506 but we can in a constexpr constructor for a non-literal class. Just
7507 ignore it; either all the initialization will be constant, in which
7508 case the cleanup can't run, or it can't be constexpr.
7509 Still recurse into CLEANUP_BODY. */
7510 return build_data_member_initialization (CLEANUP_BODY (t), vec);
7512 if (TREE_CODE (t) == CONVERT_EXPR)
7513 t = TREE_OPERAND (t, 0);
7514 if (TREE_CODE (t) == INIT_EXPR
7515 || TREE_CODE (t) == MODIFY_EXPR)
7517 member = TREE_OPERAND (t, 0);
7518 init = break_out_target_exprs (TREE_OPERAND (t, 1));
7520 else if (TREE_CODE (t) == CALL_EXPR)
7522 member = CALL_EXPR_ARG (t, 0);
7523 /* We don't use build_cplus_new here because it complains about
7524 abstract bases. Leaving the call unwrapped means that it has the
7525 wrong type, but cxx_eval_constant_expression doesn't care. */
7526 init = break_out_target_exprs (t);
7528 else if (TREE_CODE (t) == DECL_EXPR)
7529 /* Declaring a temporary, don't add it to the CONSTRUCTOR. */
7530 return true;
7531 else
7532 gcc_unreachable ();
7533 if (INDIRECT_REF_P (member))
7534 member = TREE_OPERAND (member, 0);
7535 if (TREE_CODE (member) == NOP_EXPR)
7537 tree op = member;
7538 STRIP_NOPS (op);
7539 if (TREE_CODE (op) == ADDR_EXPR)
7541 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7542 (TREE_TYPE (TREE_TYPE (op)),
7543 TREE_TYPE (TREE_TYPE (member))));
7544 /* Initializing a cv-qualified member; we need to look through
7545 the const_cast. */
7546 member = op;
7548 else if (op == current_class_ptr
7549 && (same_type_ignoring_top_level_qualifiers_p
7550 (TREE_TYPE (TREE_TYPE (member)),
7551 current_class_type)))
7552 /* Delegating constructor. */
7553 member = op;
7554 else
7556 /* This is an initializer for an empty base; keep it for now so
7557 we can check it in cxx_eval_bare_aggregate. */
7558 gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (member))));
7561 if (TREE_CODE (member) == ADDR_EXPR)
7562 member = TREE_OPERAND (member, 0);
7563 if (TREE_CODE (member) == COMPONENT_REF)
7565 tree aggr = TREE_OPERAND (member, 0);
7566 if (TREE_CODE (aggr) != COMPONENT_REF)
7567 /* Normal member initialization. */
7568 member = TREE_OPERAND (member, 1);
7569 else if (ANON_AGGR_TYPE_P (TREE_TYPE (aggr)))
7570 /* Initializing a member of an anonymous union. */
7571 return build_anon_member_initialization (member, init, vec);
7572 else
7573 /* We're initializing a vtable pointer in a base. Leave it as
7574 COMPONENT_REF so we remember the path to get to the vfield. */
7575 gcc_assert (TREE_TYPE (member) == vtbl_ptr_type_node);
7578 CONSTRUCTOR_APPEND_ELT (*vec, member, init);
7579 return true;
7582 /* Make sure that there are no statements after LAST in the constructor
7583 body represented by LIST. */
7585 bool
7586 check_constexpr_ctor_body (tree last, tree list)
7588 bool ok = true;
7589 if (TREE_CODE (list) == STATEMENT_LIST)
7591 tree_stmt_iterator i = tsi_last (list);
7592 for (; !tsi_end_p (i); tsi_prev (&i))
7594 tree t = tsi_stmt (i);
7595 if (t == last)
7596 break;
7597 if (TREE_CODE (t) == BIND_EXPR)
7599 if (!check_constexpr_ctor_body (last, BIND_EXPR_BODY (t)))
7600 return false;
7601 else
7602 continue;
7604 /* We currently allow typedefs and static_assert.
7605 FIXME allow them in the standard, too. */
7606 if (TREE_CODE (t) != STATIC_ASSERT)
7608 ok = false;
7609 break;
7613 else if (list != last
7614 && TREE_CODE (list) != STATIC_ASSERT)
7615 ok = false;
7616 if (!ok)
7618 error ("constexpr constructor does not have empty body");
7619 DECL_DECLARED_CONSTEXPR_P (current_function_decl) = false;
7621 return ok;
7624 /* V is a vector of constructor elements built up for the base and member
7625 initializers of a constructor for TYPE. They need to be in increasing
7626 offset order, which they might not be yet if TYPE has a primary base
7627 which is not first in the base-clause or a vptr and at least one base
7628 all of which are non-primary. */
7630 static vec<constructor_elt, va_gc> *
7631 sort_constexpr_mem_initializers (tree type, vec<constructor_elt, va_gc> *v)
7633 tree pri = CLASSTYPE_PRIMARY_BINFO (type);
7634 tree field_type;
7635 constructor_elt elt;
7636 int i;
7638 if (pri)
7639 field_type = BINFO_TYPE (pri);
7640 else if (TYPE_CONTAINS_VPTR_P (type))
7641 field_type = vtbl_ptr_type_node;
7642 else
7643 return v;
7645 /* Find the element for the primary base or vptr and move it to the
7646 beginning of the vec. */
7647 vec<constructor_elt, va_gc> &vref = *v;
7648 for (i = 0; ; ++i)
7649 if (TREE_TYPE (vref[i].index) == field_type)
7650 break;
7652 if (i > 0)
7654 elt = vref[i];
7655 for (; i > 0; --i)
7656 vref[i] = vref[i-1];
7657 vref[0] = elt;
7660 return v;
7663 /* Build compile-time evalable representations of member-initializer list
7664 for a constexpr constructor. */
7666 static tree
7667 build_constexpr_constructor_member_initializers (tree type, tree body)
7669 vec<constructor_elt, va_gc> *vec = NULL;
7670 bool ok = true;
7671 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR
7672 || TREE_CODE (body) == EH_SPEC_BLOCK)
7673 body = TREE_OPERAND (body, 0);
7674 if (TREE_CODE (body) == STATEMENT_LIST)
7675 body = STATEMENT_LIST_HEAD (body)->stmt;
7676 body = BIND_EXPR_BODY (body);
7677 if (TREE_CODE (body) == CLEANUP_POINT_EXPR)
7679 body = TREE_OPERAND (body, 0);
7680 if (TREE_CODE (body) == EXPR_STMT)
7681 body = TREE_OPERAND (body, 0);
7682 if (TREE_CODE (body) == INIT_EXPR
7683 && (same_type_ignoring_top_level_qualifiers_p
7684 (TREE_TYPE (TREE_OPERAND (body, 0)),
7685 current_class_type)))
7687 /* Trivial copy. */
7688 return TREE_OPERAND (body, 1);
7690 ok = build_data_member_initialization (body, &vec);
7692 else if (TREE_CODE (body) == STATEMENT_LIST)
7694 tree_stmt_iterator i;
7695 for (i = tsi_start (body); !tsi_end_p (i); tsi_next (&i))
7697 ok = build_data_member_initialization (tsi_stmt (i), &vec);
7698 if (!ok)
7699 break;
7702 else if (TREE_CODE (body) == TRY_BLOCK)
7704 error ("body of %<constexpr%> constructor cannot be "
7705 "a function-try-block");
7706 return error_mark_node;
7708 else if (EXPR_P (body))
7709 ok = build_data_member_initialization (body, &vec);
7710 else
7711 gcc_assert (errorcount > 0);
7712 if (ok)
7714 if (vec_safe_length (vec) > 0)
7716 /* In a delegating constructor, return the target. */
7717 constructor_elt *ce = &(*vec)[0];
7718 if (ce->index == current_class_ptr)
7720 body = ce->value;
7721 vec_free (vec);
7722 return body;
7725 vec = sort_constexpr_mem_initializers (type, vec);
7726 return build_constructor (type, vec);
7728 else
7729 return error_mark_node;
7732 /* Subroutine of register_constexpr_fundef. BODY is the body of a function
7733 declared to be constexpr, or a sub-statement thereof. Returns the
7734 return value if suitable, error_mark_node for a statement not allowed in
7735 a constexpr function, or NULL_TREE if no return value was found. */
7737 static tree
7738 constexpr_fn_retval (tree body)
7740 switch (TREE_CODE (body))
7742 case STATEMENT_LIST:
7744 tree_stmt_iterator i;
7745 tree expr = NULL_TREE;
7746 for (i = tsi_start (body); !tsi_end_p (i); tsi_next (&i))
7748 tree s = constexpr_fn_retval (tsi_stmt (i));
7749 if (s == error_mark_node)
7750 return error_mark_node;
7751 else if (s == NULL_TREE)
7752 /* Keep iterating. */;
7753 else if (expr)
7754 /* Multiple return statements. */
7755 return error_mark_node;
7756 else
7757 expr = s;
7759 return expr;
7762 case RETURN_EXPR:
7763 return break_out_target_exprs (TREE_OPERAND (body, 0));
7765 case DECL_EXPR:
7766 if (TREE_CODE (DECL_EXPR_DECL (body)) == USING_DECL)
7767 return NULL_TREE;
7768 return error_mark_node;
7770 case CLEANUP_POINT_EXPR:
7771 return constexpr_fn_retval (TREE_OPERAND (body, 0));
7773 case USING_STMT:
7774 return NULL_TREE;
7776 default:
7777 return error_mark_node;
7781 /* Subroutine of register_constexpr_fundef. BODY is the DECL_SAVED_TREE of
7782 FUN; do the necessary transformations to turn it into a single expression
7783 that we can store in the hash table. */
7785 static tree
7786 massage_constexpr_body (tree fun, tree body)
7788 if (DECL_CONSTRUCTOR_P (fun))
7789 body = build_constexpr_constructor_member_initializers
7790 (DECL_CONTEXT (fun), body);
7791 else
7793 if (TREE_CODE (body) == EH_SPEC_BLOCK)
7794 body = EH_SPEC_STMTS (body);
7795 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
7796 body = TREE_OPERAND (body, 0);
7797 if (TREE_CODE (body) == BIND_EXPR)
7798 body = BIND_EXPR_BODY (body);
7799 body = constexpr_fn_retval (body);
7801 return body;
7804 /* FUN is a constexpr constructor with massaged body BODY. Return true
7805 if some bases/fields are uninitialized, and complain if COMPLAIN. */
7807 static bool
7808 cx_check_missing_mem_inits (tree fun, tree body, bool complain)
7810 bool bad;
7811 tree field;
7812 unsigned i, nelts;
7813 tree ctype;
7815 if (TREE_CODE (body) != CONSTRUCTOR)
7816 return false;
7818 nelts = CONSTRUCTOR_NELTS (body);
7819 ctype = DECL_CONTEXT (fun);
7820 field = TYPE_FIELDS (ctype);
7822 if (TREE_CODE (ctype) == UNION_TYPE)
7824 if (nelts == 0 && next_initializable_field (field))
7826 if (complain)
7827 error ("%<constexpr%> constructor for union %qT must "
7828 "initialize exactly one non-static data member", ctype);
7829 return true;
7831 return false;
7834 bad = false;
7835 for (i = 0; i <= nelts; ++i)
7837 tree index;
7838 if (i == nelts)
7839 index = NULL_TREE;
7840 else
7842 index = CONSTRUCTOR_ELT (body, i)->index;
7843 /* Skip base and vtable inits. */
7844 if (TREE_CODE (index) != FIELD_DECL
7845 || DECL_ARTIFICIAL (index))
7846 continue;
7848 for (; field != index; field = DECL_CHAIN (field))
7850 tree ftype;
7851 if (TREE_CODE (field) != FIELD_DECL
7852 || (DECL_C_BIT_FIELD (field) && !DECL_NAME (field))
7853 || DECL_ARTIFICIAL (field))
7854 continue;
7855 ftype = strip_array_types (TREE_TYPE (field));
7856 if (type_has_constexpr_default_constructor (ftype))
7858 /* It's OK to skip a member with a trivial constexpr ctor.
7859 A constexpr ctor that isn't trivial should have been
7860 added in by now. */
7861 gcc_checking_assert (!TYPE_HAS_COMPLEX_DFLT (ftype)
7862 || errorcount != 0);
7863 continue;
7865 if (!complain)
7866 return true;
7867 error ("uninitialized member %qD in %<constexpr%> constructor",
7868 field);
7869 bad = true;
7871 if (field == NULL_TREE)
7872 break;
7873 field = DECL_CHAIN (field);
7876 return bad;
7879 /* We are processing the definition of the constexpr function FUN.
7880 Check that its BODY fulfills the propriate requirements and
7881 enter it in the constexpr function definition table.
7882 For constructor BODY is actually the TREE_LIST of the
7883 member-initializer list. */
7885 tree
7886 register_constexpr_fundef (tree fun, tree body)
7888 constexpr_fundef entry;
7889 constexpr_fundef **slot;
7891 if (!is_valid_constexpr_fn (fun, !DECL_GENERATED_P (fun)))
7892 return NULL;
7894 body = massage_constexpr_body (fun, body);
7895 if (body == NULL_TREE || body == error_mark_node)
7897 if (!DECL_CONSTRUCTOR_P (fun))
7898 error ("body of constexpr function %qD not a return-statement", fun);
7899 return NULL;
7902 if (!potential_rvalue_constant_expression (body))
7904 if (!DECL_GENERATED_P (fun))
7905 require_potential_rvalue_constant_expression (body);
7906 return NULL;
7909 if (DECL_CONSTRUCTOR_P (fun)
7910 && cx_check_missing_mem_inits (fun, body, !DECL_GENERATED_P (fun)))
7911 return NULL;
7913 /* Create the constexpr function table if necessary. */
7914 if (constexpr_fundef_table == NULL)
7915 constexpr_fundef_table = htab_create_ggc (101,
7916 constexpr_fundef_hash,
7917 constexpr_fundef_equal,
7918 ggc_free);
7919 entry.decl = fun;
7920 entry.body = body;
7921 slot = (constexpr_fundef **)
7922 htab_find_slot (constexpr_fundef_table, &entry, INSERT);
7924 gcc_assert (*slot == NULL);
7925 *slot = ggc_alloc_constexpr_fundef ();
7926 **slot = entry;
7928 return fun;
7931 /* FUN is a non-constexpr function called in a context that requires a
7932 constant expression. If it comes from a constexpr template, explain why
7933 the instantiation isn't constexpr. */
7935 void
7936 explain_invalid_constexpr_fn (tree fun)
7938 static struct pointer_set_t *diagnosed;
7939 tree body;
7940 location_t save_loc;
7941 /* Only diagnose defaulted functions or instantiations. */
7942 if (!DECL_DEFAULTED_FN (fun)
7943 && !is_instantiation_of_constexpr (fun))
7944 return;
7945 if (diagnosed == NULL)
7946 diagnosed = pointer_set_create ();
7947 if (pointer_set_insert (diagnosed, fun) != 0)
7948 /* Already explained. */
7949 return;
7951 save_loc = input_location;
7952 input_location = DECL_SOURCE_LOCATION (fun);
7953 inform (0, "%q+D is not usable as a constexpr function because:", fun);
7954 /* First check the declaration. */
7955 if (is_valid_constexpr_fn (fun, true))
7957 /* Then if it's OK, the body. */
7958 if (DECL_DEFAULTED_FN (fun))
7959 explain_implicit_non_constexpr (fun);
7960 else
7962 body = massage_constexpr_body (fun, DECL_SAVED_TREE (fun));
7963 require_potential_rvalue_constant_expression (body);
7964 if (DECL_CONSTRUCTOR_P (fun))
7965 cx_check_missing_mem_inits (fun, body, true);
7968 input_location = save_loc;
7971 /* Objects of this type represent calls to constexpr functions
7972 along with the bindings of parameters to their arguments, for
7973 the purpose of compile time evaluation. */
7975 typedef struct GTY(()) constexpr_call {
7976 /* Description of the constexpr function definition. */
7977 constexpr_fundef *fundef;
7978 /* Parameter bindings environment. A TREE_LIST where each TREE_PURPOSE
7979 is a parameter _DECL and the TREE_VALUE is the value of the parameter.
7980 Note: This arrangement is made to accommodate the use of
7981 iterative_hash_template_arg (see pt.c). If you change this
7982 representation, also change the hash calculation in
7983 cxx_eval_call_expression. */
7984 tree bindings;
7985 /* Result of the call.
7986 NULL means the call is being evaluated.
7987 error_mark_node means that the evaluation was erroneous;
7988 otherwise, the actuall value of the call. */
7989 tree result;
7990 /* The hash of this call; we remember it here to avoid having to
7991 recalculate it when expanding the hash table. */
7992 hashval_t hash;
7993 } constexpr_call;
7995 /* A table of all constexpr calls that have been evaluated by the
7996 compiler in this translation unit. */
7998 static GTY ((param_is (constexpr_call))) htab_t constexpr_call_table;
8000 static tree cxx_eval_constant_expression (const constexpr_call *, tree,
8001 bool, bool, bool *, bool *);
8003 /* Compute a hash value for a constexpr call representation. */
8005 static hashval_t
8006 constexpr_call_hash (const void *p)
8008 const constexpr_call *info = (const constexpr_call *) p;
8009 return info->hash;
8012 /* Return 1 if the objects pointed to by P and Q represent calls
8013 to the same constexpr function with the same arguments.
8014 Otherwise, return 0. */
8016 static int
8017 constexpr_call_equal (const void *p, const void *q)
8019 const constexpr_call *lhs = (const constexpr_call *) p;
8020 const constexpr_call *rhs = (const constexpr_call *) q;
8021 tree lhs_bindings;
8022 tree rhs_bindings;
8023 if (lhs == rhs)
8024 return 1;
8025 if (!constexpr_fundef_equal (lhs->fundef, rhs->fundef))
8026 return 0;
8027 lhs_bindings = lhs->bindings;
8028 rhs_bindings = rhs->bindings;
8029 while (lhs_bindings != NULL && rhs_bindings != NULL)
8031 tree lhs_arg = TREE_VALUE (lhs_bindings);
8032 tree rhs_arg = TREE_VALUE (rhs_bindings);
8033 gcc_assert (TREE_TYPE (lhs_arg) == TREE_TYPE (rhs_arg));
8034 if (!cp_tree_equal (lhs_arg, rhs_arg))
8035 return 0;
8036 lhs_bindings = TREE_CHAIN (lhs_bindings);
8037 rhs_bindings = TREE_CHAIN (rhs_bindings);
8039 return lhs_bindings == rhs_bindings;
8042 /* Initialize the constexpr call table, if needed. */
8044 static void
8045 maybe_initialize_constexpr_call_table (void)
8047 if (constexpr_call_table == NULL)
8048 constexpr_call_table = htab_create_ggc (101,
8049 constexpr_call_hash,
8050 constexpr_call_equal,
8051 ggc_free);
8054 /* Return true if T designates the implied `this' parameter. */
8056 static inline bool
8057 is_this_parameter (tree t)
8059 return t == current_class_ptr;
8062 /* We have an expression tree T that represents a call, either CALL_EXPR
8063 or AGGR_INIT_EXPR. If the call is lexically to a named function,
8064 retrun the _DECL for that function. */
8066 static tree
8067 get_function_named_in_call (tree t)
8069 tree fun = NULL;
8070 switch (TREE_CODE (t))
8072 case CALL_EXPR:
8073 fun = CALL_EXPR_FN (t);
8074 break;
8076 case AGGR_INIT_EXPR:
8077 fun = AGGR_INIT_EXPR_FN (t);
8078 break;
8080 default:
8081 gcc_unreachable();
8082 break;
8084 if (TREE_CODE (fun) == ADDR_EXPR
8085 && TREE_CODE (TREE_OPERAND (fun, 0)) == FUNCTION_DECL)
8086 fun = TREE_OPERAND (fun, 0);
8087 return fun;
8090 /* We have an expression tree T that represents a call, either CALL_EXPR
8091 or AGGR_INIT_EXPR. Return the Nth argument. */
8093 static inline tree
8094 get_nth_callarg (tree t, int n)
8096 switch (TREE_CODE (t))
8098 case CALL_EXPR:
8099 return CALL_EXPR_ARG (t, n);
8101 case AGGR_INIT_EXPR:
8102 return AGGR_INIT_EXPR_ARG (t, n);
8104 default:
8105 gcc_unreachable ();
8106 return NULL;
8110 /* Look up the binding of the function parameter T in a constexpr
8111 function call context CALL. */
8113 static tree
8114 lookup_parameter_binding (const constexpr_call *call, tree t)
8116 tree b = purpose_member (t, call->bindings);
8117 return TREE_VALUE (b);
8120 /* Attempt to evaluate T which represents a call to a builtin function.
8121 We assume here that all builtin functions evaluate to scalar types
8122 represented by _CST nodes. */
8124 static tree
8125 cxx_eval_builtin_function_call (const constexpr_call *call, tree t,
8126 bool allow_non_constant, bool addr,
8127 bool *non_constant_p, bool *overflow_p)
8129 const int nargs = call_expr_nargs (t);
8130 tree *args = (tree *) alloca (nargs * sizeof (tree));
8131 tree new_call;
8132 int i;
8133 for (i = 0; i < nargs; ++i)
8135 args[i] = cxx_eval_constant_expression (call, CALL_EXPR_ARG (t, i),
8136 allow_non_constant, addr,
8137 non_constant_p, overflow_p);
8138 if (allow_non_constant && *non_constant_p)
8139 return t;
8141 if (*non_constant_p)
8142 return t;
8143 new_call = build_call_array_loc (EXPR_LOCATION (t), TREE_TYPE (t),
8144 CALL_EXPR_FN (t), nargs, args);
8145 new_call = fold (new_call);
8146 VERIFY_CONSTANT (new_call);
8147 return new_call;
8150 /* TEMP is the constant value of a temporary object of type TYPE. Adjust
8151 the type of the value to match. */
8153 static tree
8154 adjust_temp_type (tree type, tree temp)
8156 if (TREE_TYPE (temp) == type)
8157 return temp;
8158 /* Avoid wrapping an aggregate value in a NOP_EXPR. */
8159 if (TREE_CODE (temp) == CONSTRUCTOR)
8160 return build_constructor (type, CONSTRUCTOR_ELTS (temp));
8161 gcc_assert (scalarish_type_p (type));
8162 return cp_fold_convert (type, temp);
8165 /* Subroutine of cxx_eval_call_expression.
8166 We are processing a call expression (either CALL_EXPR or
8167 AGGR_INIT_EXPR) in the call context of OLD_CALL. Evaluate
8168 all arguments and bind their values to correspondings
8169 parameters, making up the NEW_CALL context. */
8171 static void
8172 cxx_bind_parameters_in_call (const constexpr_call *old_call, tree t,
8173 constexpr_call *new_call,
8174 bool allow_non_constant,
8175 bool *non_constant_p, bool *overflow_p)
8177 const int nargs = call_expr_nargs (t);
8178 tree fun = new_call->fundef->decl;
8179 tree parms = DECL_ARGUMENTS (fun);
8180 int i;
8181 for (i = 0; i < nargs; ++i)
8183 tree x, arg;
8184 tree type = parms ? TREE_TYPE (parms) : void_type_node;
8185 /* For member function, the first argument is a pointer to the implied
8186 object. And for an object construction, don't bind `this' before
8187 it is fully constructed. */
8188 if (i == 0 && DECL_CONSTRUCTOR_P (fun))
8189 goto next;
8190 x = get_nth_callarg (t, i);
8191 if (parms && DECL_BY_REFERENCE (parms))
8193 /* cp_genericize made this a reference for argument passing, but
8194 we don't want to treat it like one for constexpr evaluation. */
8195 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
8196 gcc_assert (TREE_CODE (TREE_TYPE (x)) == REFERENCE_TYPE);
8197 type = TREE_TYPE (type);
8198 x = convert_from_reference (x);
8200 arg = cxx_eval_constant_expression (old_call, x, allow_non_constant,
8201 TREE_CODE (type) == REFERENCE_TYPE,
8202 non_constant_p, overflow_p);
8203 /* Don't VERIFY_CONSTANT here. */
8204 if (*non_constant_p && allow_non_constant)
8205 return;
8206 /* Just discard ellipsis args after checking their constantitude. */
8207 if (!parms)
8208 continue;
8209 if (*non_constant_p)
8210 /* Don't try to adjust the type of non-constant args. */
8211 goto next;
8213 /* Make sure the binding has the same type as the parm. */
8214 if (TREE_CODE (type) != REFERENCE_TYPE)
8215 arg = adjust_temp_type (type, arg);
8216 new_call->bindings = tree_cons (parms, arg, new_call->bindings);
8217 next:
8218 parms = TREE_CHAIN (parms);
8222 /* Variables and functions to manage constexpr call expansion context.
8223 These do not need to be marked for PCH or GC. */
8225 /* FIXME remember and print actual constant arguments. */
8226 static vec<tree> call_stack = vNULL;
8227 static int call_stack_tick;
8228 static int last_cx_error_tick;
8230 static bool
8231 push_cx_call_context (tree call)
8233 ++call_stack_tick;
8234 if (!EXPR_HAS_LOCATION (call))
8235 SET_EXPR_LOCATION (call, input_location);
8236 call_stack.safe_push (call);
8237 if (call_stack.length () > (unsigned) max_constexpr_depth)
8238 return false;
8239 return true;
8242 static void
8243 pop_cx_call_context (void)
8245 ++call_stack_tick;
8246 call_stack.pop ();
8249 vec<tree>
8250 cx_error_context (void)
8252 vec<tree> r = vNULL;
8253 if (call_stack_tick != last_cx_error_tick
8254 && !call_stack.is_empty ())
8255 r = call_stack;
8256 last_cx_error_tick = call_stack_tick;
8257 return r;
8260 /* Subroutine of cxx_eval_constant_expression.
8261 Evaluate the call expression tree T in the context of OLD_CALL expression
8262 evaluation. */
8264 static tree
8265 cxx_eval_call_expression (const constexpr_call *old_call, tree t,
8266 bool allow_non_constant, bool addr,
8267 bool *non_constant_p, bool *overflow_p)
8269 location_t loc = EXPR_LOC_OR_HERE (t);
8270 tree fun = get_function_named_in_call (t);
8271 tree result;
8272 constexpr_call new_call = { NULL, NULL, NULL, 0 };
8273 constexpr_call **slot;
8274 constexpr_call *entry;
8275 bool depth_ok;
8277 if (TREE_CODE (fun) != FUNCTION_DECL)
8279 /* Might be a constexpr function pointer. */
8280 fun = cxx_eval_constant_expression (old_call, fun, allow_non_constant,
8281 /*addr*/false, non_constant_p, overflow_p);
8282 if (TREE_CODE (fun) == ADDR_EXPR)
8283 fun = TREE_OPERAND (fun, 0);
8285 if (TREE_CODE (fun) != FUNCTION_DECL)
8287 if (!allow_non_constant && !*non_constant_p)
8288 error_at (loc, "expression %qE does not designate a constexpr "
8289 "function", fun);
8290 *non_constant_p = true;
8291 return t;
8293 if (DECL_CLONED_FUNCTION_P (fun))
8294 fun = DECL_CLONED_FUNCTION (fun);
8295 if (is_builtin_fn (fun))
8296 return cxx_eval_builtin_function_call (old_call, t, allow_non_constant,
8297 addr, non_constant_p, overflow_p);
8298 if (!DECL_DECLARED_CONSTEXPR_P (fun))
8300 if (!allow_non_constant)
8302 error_at (loc, "call to non-constexpr function %qD", fun);
8303 explain_invalid_constexpr_fn (fun);
8305 *non_constant_p = true;
8306 return t;
8309 /* Shortcut trivial constructor/op=. */
8310 if (trivial_fn_p (fun))
8312 if (call_expr_nargs (t) == 2)
8314 tree arg = convert_from_reference (get_nth_callarg (t, 1));
8315 return cxx_eval_constant_expression (old_call, arg, allow_non_constant,
8316 addr, non_constant_p, overflow_p);
8318 else if (TREE_CODE (t) == AGGR_INIT_EXPR
8319 && AGGR_INIT_ZERO_FIRST (t))
8320 return build_zero_init (DECL_CONTEXT (fun), NULL_TREE, false);
8323 /* If in direct recursive call, optimize definition search. */
8324 if (old_call != NULL && old_call->fundef->decl == fun)
8325 new_call.fundef = old_call->fundef;
8326 else
8328 new_call.fundef = retrieve_constexpr_fundef (fun);
8329 if (new_call.fundef == NULL || new_call.fundef->body == NULL)
8331 if (!allow_non_constant)
8333 if (DECL_INITIAL (fun))
8335 /* The definition of fun was somehow unsuitable. */
8336 error_at (loc, "%qD called in a constant expression", fun);
8337 explain_invalid_constexpr_fn (fun);
8339 else
8340 error_at (loc, "%qD used before its definition", fun);
8342 *non_constant_p = true;
8343 return t;
8346 cxx_bind_parameters_in_call (old_call, t, &new_call,
8347 allow_non_constant, non_constant_p, overflow_p);
8348 if (*non_constant_p)
8349 return t;
8351 depth_ok = push_cx_call_context (t);
8353 new_call.hash
8354 = iterative_hash_template_arg (new_call.bindings,
8355 constexpr_fundef_hash (new_call.fundef));
8357 /* If we have seen this call before, we are done. */
8358 maybe_initialize_constexpr_call_table ();
8359 slot = (constexpr_call **)
8360 htab_find_slot (constexpr_call_table, &new_call, INSERT);
8361 entry = *slot;
8362 if (entry == NULL)
8364 /* We need to keep a pointer to the entry, not just the slot, as the
8365 slot can move in the call to cxx_eval_builtin_function_call. */
8366 *slot = entry = ggc_alloc_constexpr_call ();
8367 *entry = new_call;
8369 /* Calls which are in progress have their result set to NULL
8370 so that we can detect circular dependencies. */
8371 else if (entry->result == NULL)
8373 if (!allow_non_constant)
8374 error ("call has circular dependency");
8375 *non_constant_p = true;
8376 entry->result = result = error_mark_node;
8379 if (!depth_ok)
8381 if (!allow_non_constant)
8382 error ("constexpr evaluation depth exceeds maximum of %d (use "
8383 "-fconstexpr-depth= to increase the maximum)",
8384 max_constexpr_depth);
8385 *non_constant_p = true;
8386 entry->result = result = error_mark_node;
8388 else
8390 result = entry->result;
8391 if (!result || result == error_mark_node)
8392 result = (cxx_eval_constant_expression
8393 (&new_call, new_call.fundef->body,
8394 allow_non_constant, addr,
8395 non_constant_p, overflow_p));
8396 if (result == error_mark_node)
8397 *non_constant_p = true;
8398 if (*non_constant_p)
8399 entry->result = result = error_mark_node;
8400 else
8402 /* If this was a call to initialize an object, set the type of
8403 the CONSTRUCTOR to the type of that object. */
8404 if (DECL_CONSTRUCTOR_P (fun))
8406 tree ob_arg = get_nth_callarg (t, 0);
8407 STRIP_NOPS (ob_arg);
8408 gcc_assert (TYPE_PTR_P (TREE_TYPE (ob_arg))
8409 && CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (ob_arg))));
8410 result = adjust_temp_type (TREE_TYPE (TREE_TYPE (ob_arg)),
8411 result);
8413 entry->result = result;
8417 pop_cx_call_context ();
8418 return unshare_expr (result);
8421 /* FIXME speed this up, it's taking 16% of compile time on sieve testcase. */
8423 bool
8424 reduced_constant_expression_p (tree t)
8426 if (TREE_CODE (t) == PTRMEM_CST)
8427 /* Even if we can't lower this yet, it's constant. */
8428 return true;
8429 /* FIXME are we calling this too much? */
8430 return initializer_constant_valid_p (t, TREE_TYPE (t)) != NULL_TREE;
8433 /* Some expressions may have constant operands but are not constant
8434 themselves, such as 1/0. Call this function (or rather, the macro
8435 following it) to check for that condition.
8437 We only call this in places that require an arithmetic constant, not in
8438 places where we might have a non-constant expression that can be a
8439 component of a constant expression, such as the address of a constexpr
8440 variable that might be dereferenced later. */
8442 static bool
8443 verify_constant (tree t, bool allow_non_constant, bool *non_constant_p,
8444 bool *overflow_p)
8446 if (!*non_constant_p && !reduced_constant_expression_p (t))
8448 if (!allow_non_constant)
8449 error ("%q+E is not a constant expression", t);
8450 *non_constant_p = true;
8452 if (TREE_OVERFLOW_P (t))
8454 if (!allow_non_constant)
8456 permerror (input_location, "overflow in constant expression");
8457 /* If we're being permissive (and are in an enforcing
8458 context), ignore the overflow. */
8459 if (flag_permissive)
8460 return *non_constant_p;
8462 *overflow_p = true;
8464 return *non_constant_p;
8467 /* Subroutine of cxx_eval_constant_expression.
8468 Attempt to reduce the unary expression tree T to a compile time value.
8469 If successful, return the value. Otherwise issue a diagnostic
8470 and return error_mark_node. */
8472 static tree
8473 cxx_eval_unary_expression (const constexpr_call *call, tree t,
8474 bool allow_non_constant, bool addr,
8475 bool *non_constant_p, bool *overflow_p)
8477 tree r;
8478 tree orig_arg = TREE_OPERAND (t, 0);
8479 tree arg = cxx_eval_constant_expression (call, orig_arg, allow_non_constant,
8480 addr, non_constant_p, overflow_p);
8481 VERIFY_CONSTANT (arg);
8482 if (arg == orig_arg)
8483 return t;
8484 r = fold_build1 (TREE_CODE (t), TREE_TYPE (t), arg);
8485 VERIFY_CONSTANT (r);
8486 return r;
8489 /* Subroutine of cxx_eval_constant_expression.
8490 Like cxx_eval_unary_expression, except for binary expressions. */
8492 static tree
8493 cxx_eval_binary_expression (const constexpr_call *call, tree t,
8494 bool allow_non_constant, bool addr,
8495 bool *non_constant_p, bool *overflow_p)
8497 tree r;
8498 tree orig_lhs = TREE_OPERAND (t, 0);
8499 tree orig_rhs = TREE_OPERAND (t, 1);
8500 tree lhs, rhs;
8501 lhs = cxx_eval_constant_expression (call, orig_lhs,
8502 allow_non_constant, addr,
8503 non_constant_p, overflow_p);
8504 VERIFY_CONSTANT (lhs);
8505 rhs = cxx_eval_constant_expression (call, orig_rhs,
8506 allow_non_constant, addr,
8507 non_constant_p, overflow_p);
8508 VERIFY_CONSTANT (rhs);
8509 if (lhs == orig_lhs && rhs == orig_rhs)
8510 return t;
8511 r = fold_build2 (TREE_CODE (t), TREE_TYPE (t), lhs, rhs);
8512 VERIFY_CONSTANT (r);
8513 return r;
8516 /* Subroutine of cxx_eval_constant_expression.
8517 Attempt to evaluate condition expressions. Dead branches are not
8518 looked into. */
8520 static tree
8521 cxx_eval_conditional_expression (const constexpr_call *call, tree t,
8522 bool allow_non_constant, bool addr,
8523 bool *non_constant_p, bool *overflow_p)
8525 tree val = cxx_eval_constant_expression (call, TREE_OPERAND (t, 0),
8526 allow_non_constant, addr,
8527 non_constant_p, overflow_p);
8528 VERIFY_CONSTANT (val);
8529 /* Don't VERIFY_CONSTANT the other operands. */
8530 if (integer_zerop (val))
8531 return cxx_eval_constant_expression (call, TREE_OPERAND (t, 2),
8532 allow_non_constant, addr,
8533 non_constant_p, overflow_p);
8534 return cxx_eval_constant_expression (call, TREE_OPERAND (t, 1),
8535 allow_non_constant, addr,
8536 non_constant_p, overflow_p);
8539 /* Subroutine of cxx_eval_constant_expression.
8540 Attempt to reduce a reference to an array slot. */
8542 static tree
8543 cxx_eval_array_reference (const constexpr_call *call, tree t,
8544 bool allow_non_constant, bool addr,
8545 bool *non_constant_p, bool *overflow_p)
8547 tree oldary = TREE_OPERAND (t, 0);
8548 tree ary = cxx_eval_constant_expression (call, oldary,
8549 allow_non_constant, addr,
8550 non_constant_p, overflow_p);
8551 tree index, oldidx;
8552 HOST_WIDE_INT i;
8553 tree elem_type;
8554 unsigned len, elem_nchars = 1;
8555 if (*non_constant_p)
8556 return t;
8557 oldidx = TREE_OPERAND (t, 1);
8558 index = cxx_eval_constant_expression (call, oldidx,
8559 allow_non_constant, false,
8560 non_constant_p, overflow_p);
8561 VERIFY_CONSTANT (index);
8562 if (addr && ary == oldary && index == oldidx)
8563 return t;
8564 else if (addr)
8565 return build4 (ARRAY_REF, TREE_TYPE (t), ary, index, NULL, NULL);
8566 elem_type = TREE_TYPE (TREE_TYPE (ary));
8567 if (TREE_CODE (ary) == CONSTRUCTOR)
8568 len = CONSTRUCTOR_NELTS (ary);
8569 else if (TREE_CODE (ary) == STRING_CST)
8571 elem_nchars = (TYPE_PRECISION (elem_type)
8572 / TYPE_PRECISION (char_type_node));
8573 len = (unsigned) TREE_STRING_LENGTH (ary) / elem_nchars;
8575 else
8577 /* We can't do anything with other tree codes, so use
8578 VERIFY_CONSTANT to complain and fail. */
8579 VERIFY_CONSTANT (ary);
8580 gcc_unreachable ();
8582 if (compare_tree_int (index, len) >= 0)
8584 if (tree_int_cst_lt (index, array_type_nelts_top (TREE_TYPE (ary))))
8586 /* If it's within the array bounds but doesn't have an explicit
8587 initializer, it's value-initialized. */
8588 tree val = build_value_init (elem_type, tf_warning_or_error);
8589 return cxx_eval_constant_expression (call, val,
8590 allow_non_constant, addr,
8591 non_constant_p, overflow_p);
8594 if (!allow_non_constant)
8595 error ("array subscript out of bound");
8596 *non_constant_p = true;
8597 return t;
8599 else if (tree_int_cst_lt (index, integer_zero_node))
8601 if (!allow_non_constant)
8602 error ("negative array subscript");
8603 *non_constant_p = true;
8604 return t;
8606 i = tree_to_shwi (index);
8607 if (TREE_CODE (ary) == CONSTRUCTOR)
8608 return (*CONSTRUCTOR_ELTS (ary))[i].value;
8609 else if (elem_nchars == 1)
8610 return build_int_cst (cv_unqualified (TREE_TYPE (TREE_TYPE (ary))),
8611 TREE_STRING_POINTER (ary)[i]);
8612 else
8614 tree type = cv_unqualified (TREE_TYPE (TREE_TYPE (ary)));
8615 return native_interpret_expr (type, (const unsigned char *)
8616 TREE_STRING_POINTER (ary)
8617 + i * elem_nchars, elem_nchars);
8619 /* Don't VERIFY_CONSTANT here. */
8622 /* Subroutine of cxx_eval_constant_expression.
8623 Attempt to reduce a field access of a value of class type. */
8625 static tree
8626 cxx_eval_component_reference (const constexpr_call *call, tree t,
8627 bool allow_non_constant, bool addr,
8628 bool *non_constant_p, bool *overflow_p)
8630 unsigned HOST_WIDE_INT i;
8631 tree field;
8632 tree value;
8633 tree part = TREE_OPERAND (t, 1);
8634 tree orig_whole = TREE_OPERAND (t, 0);
8635 tree whole = cxx_eval_constant_expression (call, orig_whole,
8636 allow_non_constant, addr,
8637 non_constant_p, overflow_p);
8638 if (whole == orig_whole)
8639 return t;
8640 if (addr)
8641 return fold_build3 (COMPONENT_REF, TREE_TYPE (t),
8642 whole, part, NULL_TREE);
8643 /* Don't VERIFY_CONSTANT here; we only want to check that we got a
8644 CONSTRUCTOR. */
8645 if (!*non_constant_p && TREE_CODE (whole) != CONSTRUCTOR)
8647 if (!allow_non_constant)
8648 error ("%qE is not a constant expression", orig_whole);
8649 *non_constant_p = true;
8651 if (DECL_MUTABLE_P (part))
8653 if (!allow_non_constant)
8654 error ("mutable %qD is not usable in a constant expression", part);
8655 *non_constant_p = true;
8657 if (*non_constant_p)
8658 return t;
8659 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole), i, field, value)
8661 if (field == part)
8662 return value;
8664 if (TREE_CODE (TREE_TYPE (whole)) == UNION_TYPE
8665 && CONSTRUCTOR_NELTS (whole) > 0)
8667 /* DR 1188 says we don't have to deal with this. */
8668 if (!allow_non_constant)
8669 error ("accessing %qD member instead of initialized %qD member in "
8670 "constant expression", part, CONSTRUCTOR_ELT (whole, 0)->index);
8671 *non_constant_p = true;
8672 return t;
8675 /* If there's no explicit init for this field, it's value-initialized. */
8676 value = build_value_init (TREE_TYPE (t), tf_warning_or_error);
8677 return cxx_eval_constant_expression (call, value,
8678 allow_non_constant, addr,
8679 non_constant_p, overflow_p);
8682 /* Subroutine of cxx_eval_constant_expression.
8683 Attempt to reduce a field access of a value of class type that is
8684 expressed as a BIT_FIELD_REF. */
8686 static tree
8687 cxx_eval_bit_field_ref (const constexpr_call *call, tree t,
8688 bool allow_non_constant, bool addr,
8689 bool *non_constant_p, bool *overflow_p)
8691 tree orig_whole = TREE_OPERAND (t, 0);
8692 tree retval, fldval, utype, mask;
8693 bool fld_seen = false;
8694 HOST_WIDE_INT istart, isize;
8695 tree whole = cxx_eval_constant_expression (call, orig_whole,
8696 allow_non_constant, addr,
8697 non_constant_p, overflow_p);
8698 tree start, field, value;
8699 unsigned HOST_WIDE_INT i;
8701 if (whole == orig_whole)
8702 return t;
8703 /* Don't VERIFY_CONSTANT here; we only want to check that we got a
8704 CONSTRUCTOR. */
8705 if (!*non_constant_p
8706 && TREE_CODE (whole) != VECTOR_CST
8707 && TREE_CODE (whole) != CONSTRUCTOR)
8709 if (!allow_non_constant)
8710 error ("%qE is not a constant expression", orig_whole);
8711 *non_constant_p = true;
8713 if (*non_constant_p)
8714 return t;
8716 if (TREE_CODE (whole) == VECTOR_CST)
8717 return fold_ternary (BIT_FIELD_REF, TREE_TYPE (t), whole,
8718 TREE_OPERAND (t, 1), TREE_OPERAND (t, 2));
8720 start = TREE_OPERAND (t, 2);
8721 istart = tree_to_shwi (start);
8722 isize = tree_to_shwi (TREE_OPERAND (t, 1));
8723 utype = TREE_TYPE (t);
8724 if (!TYPE_UNSIGNED (utype))
8725 utype = build_nonstandard_integer_type (TYPE_PRECISION (utype), 1);
8726 retval = build_int_cst (utype, 0);
8727 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole), i, field, value)
8729 tree bitpos = bit_position (field);
8730 if (bitpos == start && DECL_SIZE (field) == TREE_OPERAND (t, 1))
8731 return value;
8732 if (TREE_CODE (TREE_TYPE (field)) == INTEGER_TYPE
8733 && TREE_CODE (value) == INTEGER_CST
8734 && tree_fits_shwi_p (bitpos)
8735 && tree_fits_shwi_p (DECL_SIZE (field)))
8737 HOST_WIDE_INT bit = tree_to_shwi (bitpos);
8738 HOST_WIDE_INT sz = tree_to_shwi (DECL_SIZE (field));
8739 HOST_WIDE_INT shift;
8740 if (bit >= istart && bit + sz <= istart + isize)
8742 fldval = fold_convert (utype, value);
8743 mask = build_int_cst_type (utype, -1);
8744 mask = fold_build2 (LSHIFT_EXPR, utype, mask,
8745 size_int (TYPE_PRECISION (utype) - sz));
8746 mask = fold_build2 (RSHIFT_EXPR, utype, mask,
8747 size_int (TYPE_PRECISION (utype) - sz));
8748 fldval = fold_build2 (BIT_AND_EXPR, utype, fldval, mask);
8749 shift = bit - istart;
8750 if (BYTES_BIG_ENDIAN)
8751 shift = TYPE_PRECISION (utype) - shift - sz;
8752 fldval = fold_build2 (LSHIFT_EXPR, utype, fldval,
8753 size_int (shift));
8754 retval = fold_build2 (BIT_IOR_EXPR, utype, retval, fldval);
8755 fld_seen = true;
8759 if (fld_seen)
8760 return fold_convert (TREE_TYPE (t), retval);
8761 gcc_unreachable ();
8762 return error_mark_node;
8765 /* Subroutine of cxx_eval_constant_expression.
8766 Evaluate a short-circuited logical expression T in the context
8767 of a given constexpr CALL. BAILOUT_VALUE is the value for
8768 early return. CONTINUE_VALUE is used here purely for
8769 sanity check purposes. */
8771 static tree
8772 cxx_eval_logical_expression (const constexpr_call *call, tree t,
8773 tree bailout_value, tree continue_value,
8774 bool allow_non_constant, bool addr,
8775 bool *non_constant_p, bool *overflow_p)
8777 tree r;
8778 tree lhs = cxx_eval_constant_expression (call, TREE_OPERAND (t, 0),
8779 allow_non_constant, addr,
8780 non_constant_p, overflow_p);
8781 VERIFY_CONSTANT (lhs);
8782 if (tree_int_cst_equal (lhs, bailout_value))
8783 return lhs;
8784 gcc_assert (tree_int_cst_equal (lhs, continue_value));
8785 r = cxx_eval_constant_expression (call, TREE_OPERAND (t, 1),
8786 allow_non_constant, addr, non_constant_p, overflow_p);
8787 VERIFY_CONSTANT (r);
8788 return r;
8791 /* REF is a COMPONENT_REF designating a particular field. V is a vector of
8792 CONSTRUCTOR elements to initialize (part of) an object containing that
8793 field. Return a pointer to the constructor_elt corresponding to the
8794 initialization of the field. */
8796 static constructor_elt *
8797 base_field_constructor_elt (vec<constructor_elt, va_gc> *v, tree ref)
8799 tree aggr = TREE_OPERAND (ref, 0);
8800 tree field = TREE_OPERAND (ref, 1);
8801 HOST_WIDE_INT i;
8802 constructor_elt *ce;
8804 gcc_assert (TREE_CODE (ref) == COMPONENT_REF);
8806 if (TREE_CODE (aggr) == COMPONENT_REF)
8808 constructor_elt *base_ce
8809 = base_field_constructor_elt (v, aggr);
8810 v = CONSTRUCTOR_ELTS (base_ce->value);
8813 for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
8814 if (ce->index == field)
8815 return ce;
8817 gcc_unreachable ();
8818 return NULL;
8821 /* Subroutine of cxx_eval_constant_expression.
8822 The expression tree T denotes a C-style array or a C-style
8823 aggregate. Reduce it to a constant expression. */
8825 static tree
8826 cxx_eval_bare_aggregate (const constexpr_call *call, tree t,
8827 bool allow_non_constant, bool addr,
8828 bool *non_constant_p, bool *overflow_p)
8830 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t);
8831 vec<constructor_elt, va_gc> *n;
8832 vec_alloc (n, vec_safe_length (v));
8833 constructor_elt *ce;
8834 HOST_WIDE_INT i;
8835 bool changed = false;
8836 gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (t));
8837 for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
8839 tree elt = cxx_eval_constant_expression (call, ce->value,
8840 allow_non_constant, addr,
8841 non_constant_p, overflow_p);
8842 /* Don't VERIFY_CONSTANT here. */
8843 if (allow_non_constant && *non_constant_p)
8844 goto fail;
8845 if (elt != ce->value)
8846 changed = true;
8847 if (ce->index && TREE_CODE (ce->index) == COMPONENT_REF)
8849 /* This is an initialization of a vfield inside a base
8850 subaggregate that we already initialized; push this
8851 initialization into the previous initialization. */
8852 constructor_elt *inner = base_field_constructor_elt (n, ce->index);
8853 inner->value = elt;
8855 else if (ce->index && TREE_CODE (ce->index) == NOP_EXPR)
8857 /* This is an initializer for an empty base; now that we've
8858 checked that it's constant, we can ignore it. */
8859 gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (ce->index))));
8861 else
8862 CONSTRUCTOR_APPEND_ELT (n, ce->index, elt);
8864 if (*non_constant_p || !changed)
8866 fail:
8867 vec_free (n);
8868 return t;
8870 t = build_constructor (TREE_TYPE (t), n);
8871 TREE_CONSTANT (t) = true;
8872 if (TREE_CODE (TREE_TYPE (t)) == VECTOR_TYPE)
8873 t = fold (t);
8874 return t;
8877 /* Subroutine of cxx_eval_constant_expression.
8878 The expression tree T is a VEC_INIT_EXPR which denotes the desired
8879 initialization of a non-static data member of array type. Reduce it to a
8880 CONSTRUCTOR.
8882 Note that apart from value-initialization (when VALUE_INIT is true),
8883 this is only intended to support value-initialization and the
8884 initializations done by defaulted constructors for classes with
8885 non-static data members of array type. In this case, VEC_INIT_EXPR_INIT
8886 will either be NULL_TREE for the default constructor, or a COMPONENT_REF
8887 for the copy/move constructor. */
8889 static tree
8890 cxx_eval_vec_init_1 (const constexpr_call *call, tree atype, tree init,
8891 bool value_init, bool allow_non_constant, bool addr,
8892 bool *non_constant_p, bool *overflow_p)
8894 tree elttype = TREE_TYPE (atype);
8895 int max = tree_to_shwi (array_type_nelts (atype));
8896 vec<constructor_elt, va_gc> *n;
8897 vec_alloc (n, max + 1);
8898 bool pre_init = false;
8899 int i;
8901 /* For the default constructor, build up a call to the default
8902 constructor of the element type. We only need to handle class types
8903 here, as for a constructor to be constexpr, all members must be
8904 initialized, which for a defaulted default constructor means they must
8905 be of a class type with a constexpr default constructor. */
8906 if (TREE_CODE (elttype) == ARRAY_TYPE)
8907 /* We only do this at the lowest level. */;
8908 else if (value_init)
8910 init = build_value_init (elttype, tf_warning_or_error);
8911 init = cxx_eval_constant_expression
8912 (call, init, allow_non_constant, addr, non_constant_p, overflow_p);
8913 pre_init = true;
8915 else if (!init)
8917 vec<tree, va_gc> *argvec = make_tree_vector ();
8918 init = build_special_member_call (NULL_TREE, complete_ctor_identifier,
8919 &argvec, elttype, LOOKUP_NORMAL,
8920 tf_warning_or_error);
8921 release_tree_vector (argvec);
8922 init = cxx_eval_constant_expression (call, init, allow_non_constant,
8923 addr, non_constant_p, overflow_p);
8924 pre_init = true;
8927 if (*non_constant_p && !allow_non_constant)
8928 goto fail;
8930 for (i = 0; i <= max; ++i)
8932 tree idx = build_int_cst (size_type_node, i);
8933 tree eltinit;
8934 if (TREE_CODE (elttype) == ARRAY_TYPE)
8936 /* A multidimensional array; recurse. */
8937 if (value_init || init == NULL_TREE)
8938 eltinit = NULL_TREE;
8939 else
8940 eltinit = cp_build_array_ref (input_location, init, idx,
8941 tf_warning_or_error);
8942 eltinit = cxx_eval_vec_init_1 (call, elttype, eltinit, value_init,
8943 allow_non_constant, addr,
8944 non_constant_p, overflow_p);
8946 else if (pre_init)
8948 /* Initializing an element using value or default initialization
8949 we just pre-built above. */
8950 if (i == 0)
8951 eltinit = init;
8952 else
8953 eltinit = unshare_expr (init);
8955 else
8957 /* Copying an element. */
8958 vec<tree, va_gc> *argvec;
8959 gcc_assert (same_type_ignoring_top_level_qualifiers_p
8960 (atype, TREE_TYPE (init)));
8961 eltinit = cp_build_array_ref (input_location, init, idx,
8962 tf_warning_or_error);
8963 if (!real_lvalue_p (init))
8964 eltinit = move (eltinit);
8965 argvec = make_tree_vector ();
8966 argvec->quick_push (eltinit);
8967 eltinit = (build_special_member_call
8968 (NULL_TREE, complete_ctor_identifier, &argvec,
8969 elttype, LOOKUP_NORMAL, tf_warning_or_error));
8970 release_tree_vector (argvec);
8971 eltinit = cxx_eval_constant_expression
8972 (call, eltinit, allow_non_constant, addr, non_constant_p, overflow_p);
8974 if (*non_constant_p && !allow_non_constant)
8975 goto fail;
8976 CONSTRUCTOR_APPEND_ELT (n, idx, eltinit);
8979 if (!*non_constant_p)
8981 init = build_constructor (atype, n);
8982 TREE_CONSTANT (init) = true;
8983 return init;
8986 fail:
8987 vec_free (n);
8988 return init;
8991 static tree
8992 cxx_eval_vec_init (const constexpr_call *call, tree t,
8993 bool allow_non_constant, bool addr,
8994 bool *non_constant_p, bool *overflow_p)
8996 tree atype = TREE_TYPE (t);
8997 tree init = VEC_INIT_EXPR_INIT (t);
8998 tree r = cxx_eval_vec_init_1 (call, atype, init,
8999 VEC_INIT_EXPR_VALUE_INIT (t),
9000 allow_non_constant, addr, non_constant_p, overflow_p);
9001 if (*non_constant_p)
9002 return t;
9003 else
9004 return r;
9007 /* A less strict version of fold_indirect_ref_1, which requires cv-quals to
9008 match. We want to be less strict for simple *& folding; if we have a
9009 non-const temporary that we access through a const pointer, that should
9010 work. We handle this here rather than change fold_indirect_ref_1
9011 because we're dealing with things like ADDR_EXPR of INTEGER_CST which
9012 don't really make sense outside of constant expression evaluation. Also
9013 we want to allow folding to COMPONENT_REF, which could cause trouble
9014 with TBAA in fold_indirect_ref_1.
9016 Try to keep this function synced with fold_indirect_ref_1. */
9018 static tree
9019 cxx_fold_indirect_ref (location_t loc, tree type, tree op0, bool *empty_base)
9021 tree sub, subtype;
9023 sub = op0;
9024 STRIP_NOPS (sub);
9025 subtype = TREE_TYPE (sub);
9026 if (!POINTER_TYPE_P (subtype))
9027 return NULL_TREE;
9029 if (TREE_CODE (sub) == ADDR_EXPR)
9031 tree op = TREE_OPERAND (sub, 0);
9032 tree optype = TREE_TYPE (op);
9034 /* *&CONST_DECL -> to the value of the const decl. */
9035 if (TREE_CODE (op) == CONST_DECL)
9036 return DECL_INITIAL (op);
9037 /* *&p => p; make sure to handle *&"str"[cst] here. */
9038 if (same_type_ignoring_top_level_qualifiers_p (optype, type))
9040 tree fop = fold_read_from_constant_string (op);
9041 if (fop)
9042 return fop;
9043 else
9044 return op;
9046 /* *(foo *)&fooarray => fooarray[0] */
9047 else if (TREE_CODE (optype) == ARRAY_TYPE
9048 && (same_type_ignoring_top_level_qualifiers_p
9049 (type, TREE_TYPE (optype))))
9051 tree type_domain = TYPE_DOMAIN (optype);
9052 tree min_val = size_zero_node;
9053 if (type_domain && TYPE_MIN_VALUE (type_domain))
9054 min_val = TYPE_MIN_VALUE (type_domain);
9055 return build4_loc (loc, ARRAY_REF, type, op, min_val,
9056 NULL_TREE, NULL_TREE);
9058 /* *(foo *)&complexfoo => __real__ complexfoo */
9059 else if (TREE_CODE (optype) == COMPLEX_TYPE
9060 && (same_type_ignoring_top_level_qualifiers_p
9061 (type, TREE_TYPE (optype))))
9062 return fold_build1_loc (loc, REALPART_EXPR, type, op);
9063 /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
9064 else if (TREE_CODE (optype) == VECTOR_TYPE
9065 && (same_type_ignoring_top_level_qualifiers_p
9066 (type, TREE_TYPE (optype))))
9068 tree part_width = TYPE_SIZE (type);
9069 tree index = bitsize_int (0);
9070 return fold_build3_loc (loc, BIT_FIELD_REF, type, op, part_width, index);
9072 /* Also handle conversion to an empty base class, which
9073 is represented with a NOP_EXPR. */
9074 else if (is_empty_class (type)
9075 && CLASS_TYPE_P (optype)
9076 && DERIVED_FROM_P (type, optype))
9078 *empty_base = true;
9079 return op;
9081 /* *(foo *)&struct_with_foo_field => COMPONENT_REF */
9082 else if (RECORD_OR_UNION_TYPE_P (optype))
9084 tree field = TYPE_FIELDS (optype);
9085 for (; field; field = DECL_CHAIN (field))
9086 if (TREE_CODE (field) == FIELD_DECL
9087 && integer_zerop (byte_position (field))
9088 && (same_type_ignoring_top_level_qualifiers_p
9089 (TREE_TYPE (field), type)))
9091 return fold_build3 (COMPONENT_REF, type, op, field, NULL_TREE);
9092 break;
9096 else if (TREE_CODE (sub) == POINTER_PLUS_EXPR
9097 && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST)
9099 tree op00 = TREE_OPERAND (sub, 0);
9100 tree op01 = TREE_OPERAND (sub, 1);
9102 STRIP_NOPS (op00);
9103 if (TREE_CODE (op00) == ADDR_EXPR)
9105 tree op00type;
9106 op00 = TREE_OPERAND (op00, 0);
9107 op00type = TREE_TYPE (op00);
9109 /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
9110 if (TREE_CODE (op00type) == VECTOR_TYPE
9111 && (same_type_ignoring_top_level_qualifiers_p
9112 (type, TREE_TYPE (op00type))))
9114 HOST_WIDE_INT offset = tree_to_shwi (op01);
9115 tree part_width = TYPE_SIZE (type);
9116 unsigned HOST_WIDE_INT part_widthi = tree_to_shwi (part_width)/BITS_PER_UNIT;
9117 unsigned HOST_WIDE_INT indexi = offset * BITS_PER_UNIT;
9118 tree index = bitsize_int (indexi);
9120 if (offset/part_widthi <= TYPE_VECTOR_SUBPARTS (op00type))
9121 return fold_build3_loc (loc,
9122 BIT_FIELD_REF, type, op00,
9123 part_width, index);
9126 /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
9127 else if (TREE_CODE (op00type) == COMPLEX_TYPE
9128 && (same_type_ignoring_top_level_qualifiers_p
9129 (type, TREE_TYPE (op00type))))
9131 tree size = TYPE_SIZE_UNIT (type);
9132 if (tree_int_cst_equal (size, op01))
9133 return fold_build1_loc (loc, IMAGPART_EXPR, type, op00);
9135 /* ((foo *)&fooarray)[1] => fooarray[1] */
9136 else if (TREE_CODE (op00type) == ARRAY_TYPE
9137 && (same_type_ignoring_top_level_qualifiers_p
9138 (type, TREE_TYPE (op00type))))
9140 tree type_domain = TYPE_DOMAIN (op00type);
9141 tree min_val = size_zero_node;
9142 if (type_domain && TYPE_MIN_VALUE (type_domain))
9143 min_val = TYPE_MIN_VALUE (type_domain);
9144 op01 = size_binop_loc (loc, EXACT_DIV_EXPR, op01,
9145 TYPE_SIZE_UNIT (type));
9146 op01 = size_binop_loc (loc, PLUS_EXPR, op01, min_val);
9147 return build4_loc (loc, ARRAY_REF, type, op00, op01,
9148 NULL_TREE, NULL_TREE);
9150 /* Also handle conversion to an empty base class, which
9151 is represented with a NOP_EXPR. */
9152 else if (is_empty_class (type)
9153 && CLASS_TYPE_P (op00type)
9154 && DERIVED_FROM_P (type, op00type))
9156 *empty_base = true;
9157 return op00;
9159 /* ((foo *)&struct_with_foo_field)[1] => COMPONENT_REF */
9160 else if (RECORD_OR_UNION_TYPE_P (op00type))
9162 tree field = TYPE_FIELDS (op00type);
9163 for (; field; field = DECL_CHAIN (field))
9164 if (TREE_CODE (field) == FIELD_DECL
9165 && tree_int_cst_equal (byte_position (field), op01)
9166 && (same_type_ignoring_top_level_qualifiers_p
9167 (TREE_TYPE (field), type)))
9169 return fold_build3 (COMPONENT_REF, type, op00,
9170 field, NULL_TREE);
9171 break;
9176 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
9177 else if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
9178 && (same_type_ignoring_top_level_qualifiers_p
9179 (type, TREE_TYPE (TREE_TYPE (subtype)))))
9181 tree type_domain;
9182 tree min_val = size_zero_node;
9183 tree newsub = cxx_fold_indirect_ref (loc, TREE_TYPE (subtype), sub, NULL);
9184 if (newsub)
9185 sub = newsub;
9186 else
9187 sub = build1_loc (loc, INDIRECT_REF, TREE_TYPE (subtype), sub);
9188 type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
9189 if (type_domain && TYPE_MIN_VALUE (type_domain))
9190 min_val = TYPE_MIN_VALUE (type_domain);
9191 return build4_loc (loc, ARRAY_REF, type, sub, min_val, NULL_TREE,
9192 NULL_TREE);
9195 return NULL_TREE;
9198 static tree
9199 cxx_eval_indirect_ref (const constexpr_call *call, tree t,
9200 bool allow_non_constant, bool addr,
9201 bool *non_constant_p, bool *overflow_p)
9203 tree orig_op0 = TREE_OPERAND (t, 0);
9204 tree op0 = cxx_eval_constant_expression (call, orig_op0, allow_non_constant,
9205 /*addr*/false, non_constant_p, overflow_p);
9206 bool empty_base = false;
9207 tree r;
9209 /* Don't VERIFY_CONSTANT here. */
9210 if (*non_constant_p)
9211 return t;
9213 r = cxx_fold_indirect_ref (EXPR_LOCATION (t), TREE_TYPE (t), op0,
9214 &empty_base);
9216 if (r)
9217 r = cxx_eval_constant_expression (call, r, allow_non_constant,
9218 addr, non_constant_p, overflow_p);
9219 else
9221 tree sub = op0;
9222 STRIP_NOPS (sub);
9223 if (TREE_CODE (sub) == ADDR_EXPR)
9225 /* We couldn't fold to a constant value. Make sure it's not
9226 something we should have been able to fold. */
9227 gcc_assert (!same_type_ignoring_top_level_qualifiers_p
9228 (TREE_TYPE (TREE_TYPE (sub)), TREE_TYPE (t)));
9229 /* DR 1188 says we don't have to deal with this. */
9230 if (!allow_non_constant)
9231 error ("accessing value of %qE through a %qT glvalue in a "
9232 "constant expression", build_fold_indirect_ref (sub),
9233 TREE_TYPE (t));
9234 *non_constant_p = true;
9235 return t;
9239 /* If we're pulling out the value of an empty base, make sure
9240 that the whole object is constant and then return an empty
9241 CONSTRUCTOR. */
9242 if (empty_base)
9244 VERIFY_CONSTANT (r);
9245 r = build_constructor (TREE_TYPE (t), NULL);
9246 TREE_CONSTANT (r) = true;
9249 if (r == NULL_TREE)
9251 if (addr && op0 != orig_op0)
9252 return build1 (INDIRECT_REF, TREE_TYPE (t), op0);
9253 if (!addr)
9254 VERIFY_CONSTANT (t);
9255 return t;
9257 return r;
9260 /* Complain about R, a VAR_DECL, not being usable in a constant expression.
9261 Shared between potential_constant_expression and
9262 cxx_eval_constant_expression. */
9264 static void
9265 non_const_var_error (tree r)
9267 tree type = TREE_TYPE (r);
9268 error ("the value of %qD is not usable in a constant "
9269 "expression", r);
9270 /* Avoid error cascade. */
9271 if (DECL_INITIAL (r) == error_mark_node)
9272 return;
9273 if (DECL_DECLARED_CONSTEXPR_P (r))
9274 inform (DECL_SOURCE_LOCATION (r),
9275 "%qD used in its own initializer", r);
9276 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
9278 if (!CP_TYPE_CONST_P (type))
9279 inform (DECL_SOURCE_LOCATION (r),
9280 "%q#D is not const", r);
9281 else if (CP_TYPE_VOLATILE_P (type))
9282 inform (DECL_SOURCE_LOCATION (r),
9283 "%q#D is volatile", r);
9284 else if (!DECL_INITIAL (r)
9285 || !TREE_CONSTANT (DECL_INITIAL (r)))
9286 inform (DECL_SOURCE_LOCATION (r),
9287 "%qD was not initialized with a constant "
9288 "expression", r);
9289 else
9290 gcc_unreachable ();
9292 else
9294 if (cxx_dialect >= cxx11 && !DECL_DECLARED_CONSTEXPR_P (r))
9295 inform (DECL_SOURCE_LOCATION (r),
9296 "%qD was not declared %<constexpr%>", r);
9297 else
9298 inform (DECL_SOURCE_LOCATION (r),
9299 "%qD does not have integral or enumeration type",
9304 /* Subroutine of cxx_eval_constant_expression.
9305 Like cxx_eval_unary_expression, except for trinary expressions. */
9307 static tree
9308 cxx_eval_trinary_expression (const constexpr_call *call, tree t,
9309 bool allow_non_constant, bool addr,
9310 bool *non_constant_p, bool *overflow_p)
9312 int i;
9313 tree args[3];
9314 tree val;
9316 for (i = 0; i < 3; i++)
9318 args[i] = cxx_eval_constant_expression (call, TREE_OPERAND (t, i),
9319 allow_non_constant, addr,
9320 non_constant_p, overflow_p);
9321 VERIFY_CONSTANT (args[i]);
9324 val = fold_ternary_loc (EXPR_LOCATION (t), TREE_CODE (t), TREE_TYPE (t),
9325 args[0], args[1], args[2]);
9326 if (val == NULL_TREE)
9327 return t;
9328 VERIFY_CONSTANT (val);
9329 return val;
9332 /* Attempt to reduce the expression T to a constant value.
9333 On failure, issue diagnostic and return error_mark_node. */
9334 /* FIXME unify with c_fully_fold */
9336 static tree
9337 cxx_eval_constant_expression (const constexpr_call *call, tree t,
9338 bool allow_non_constant, bool addr,
9339 bool *non_constant_p, bool *overflow_p)
9341 tree r = t;
9343 if (t == error_mark_node)
9345 *non_constant_p = true;
9346 return t;
9348 if (CONSTANT_CLASS_P (t))
9350 if (TREE_CODE (t) == PTRMEM_CST)
9351 t = cplus_expand_constant (t);
9352 else if (TREE_OVERFLOW (t) && (!flag_permissive || allow_non_constant))
9353 *overflow_p = true;
9354 return t;
9356 if (TREE_CODE (t) != NOP_EXPR
9357 && reduced_constant_expression_p (t))
9358 return fold (t);
9360 switch (TREE_CODE (t))
9362 case VAR_DECL:
9363 if (addr)
9364 return t;
9365 /* else fall through. */
9366 case CONST_DECL:
9367 r = integral_constant_value (t);
9368 if (TREE_CODE (r) == TARGET_EXPR
9369 && TREE_CODE (TARGET_EXPR_INITIAL (r)) == CONSTRUCTOR)
9370 r = TARGET_EXPR_INITIAL (r);
9371 if (DECL_P (r))
9373 if (!allow_non_constant)
9374 non_const_var_error (r);
9375 *non_constant_p = true;
9377 break;
9379 case FUNCTION_DECL:
9380 case TEMPLATE_DECL:
9381 case LABEL_DECL:
9382 return t;
9384 case PARM_DECL:
9385 if (call && DECL_CONTEXT (t) == call->fundef->decl)
9387 if (DECL_ARTIFICIAL (t) && DECL_CONSTRUCTOR_P (DECL_CONTEXT (t)))
9389 if (!allow_non_constant)
9390 sorry ("use of the value of the object being constructed "
9391 "in a constant expression");
9392 *non_constant_p = true;
9394 else
9395 r = lookup_parameter_binding (call, t);
9397 else if (addr)
9398 /* Defer in case this is only used for its type. */;
9399 else
9401 if (!allow_non_constant)
9402 error ("%qE is not a constant expression", t);
9403 *non_constant_p = true;
9405 break;
9407 case CALL_EXPR:
9408 case AGGR_INIT_EXPR:
9409 r = cxx_eval_call_expression (call, t, allow_non_constant, addr,
9410 non_constant_p, overflow_p);
9411 break;
9413 case TARGET_EXPR:
9414 if (!literal_type_p (TREE_TYPE (t)))
9416 if (!allow_non_constant)
9418 error ("temporary of non-literal type %qT in a "
9419 "constant expression", TREE_TYPE (t));
9420 explain_non_literal_class (TREE_TYPE (t));
9422 *non_constant_p = true;
9423 break;
9425 /* else fall through. */
9426 case INIT_EXPR:
9427 /* Pass false for 'addr' because these codes indicate
9428 initialization of a temporary. */
9429 r = cxx_eval_constant_expression (call, TREE_OPERAND (t, 1),
9430 allow_non_constant, false,
9431 non_constant_p, overflow_p);
9432 if (!*non_constant_p)
9433 /* Adjust the type of the result to the type of the temporary. */
9434 r = adjust_temp_type (TREE_TYPE (t), r);
9435 break;
9437 case SCOPE_REF:
9438 r = cxx_eval_constant_expression (call, TREE_OPERAND (t, 1),
9439 allow_non_constant, addr,
9440 non_constant_p, overflow_p);
9441 break;
9443 case RETURN_EXPR:
9444 case NON_LVALUE_EXPR:
9445 case TRY_CATCH_EXPR:
9446 case CLEANUP_POINT_EXPR:
9447 case MUST_NOT_THROW_EXPR:
9448 case SAVE_EXPR:
9449 r = cxx_eval_constant_expression (call, TREE_OPERAND (t, 0),
9450 allow_non_constant, addr,
9451 non_constant_p, overflow_p);
9452 break;
9454 /* These differ from cxx_eval_unary_expression in that this doesn't
9455 check for a constant operand or result; an address can be
9456 constant without its operand being, and vice versa. */
9457 case INDIRECT_REF:
9458 r = cxx_eval_indirect_ref (call, t, allow_non_constant, addr,
9459 non_constant_p, overflow_p);
9460 break;
9462 case ADDR_EXPR:
9464 tree oldop = TREE_OPERAND (t, 0);
9465 tree op = cxx_eval_constant_expression (call, oldop,
9466 allow_non_constant,
9467 /*addr*/true,
9468 non_constant_p, overflow_p);
9469 /* Don't VERIFY_CONSTANT here. */
9470 if (*non_constant_p)
9471 return t;
9472 /* This function does more aggressive folding than fold itself. */
9473 r = build_fold_addr_expr_with_type (op, TREE_TYPE (t));
9474 if (TREE_CODE (r) == ADDR_EXPR && TREE_OPERAND (r, 0) == oldop)
9475 return t;
9476 break;
9479 case REALPART_EXPR:
9480 case IMAGPART_EXPR:
9481 case CONJ_EXPR:
9482 case FIX_TRUNC_EXPR:
9483 case FLOAT_EXPR:
9484 case NEGATE_EXPR:
9485 case ABS_EXPR:
9486 case BIT_NOT_EXPR:
9487 case TRUTH_NOT_EXPR:
9488 case FIXED_CONVERT_EXPR:
9489 r = cxx_eval_unary_expression (call, t, allow_non_constant, addr,
9490 non_constant_p, overflow_p);
9491 break;
9493 case SIZEOF_EXPR:
9494 if (SIZEOF_EXPR_TYPE_P (t))
9495 r = cxx_sizeof_or_alignof_type (TREE_TYPE (TREE_OPERAND (t, 0)),
9496 SIZEOF_EXPR, false);
9497 else if (TYPE_P (TREE_OPERAND (t, 0)))
9498 r = cxx_sizeof_or_alignof_type (TREE_OPERAND (t, 0), SIZEOF_EXPR,
9499 false);
9500 else
9501 r = cxx_sizeof_or_alignof_expr (TREE_OPERAND (t, 0), SIZEOF_EXPR,
9502 false);
9503 if (r == error_mark_node)
9504 r = size_one_node;
9505 VERIFY_CONSTANT (r);
9506 break;
9508 case COMPOUND_EXPR:
9510 /* check_return_expr sometimes wraps a TARGET_EXPR in a
9511 COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR
9512 introduced by build_call_a. */
9513 tree op0 = TREE_OPERAND (t, 0);
9514 tree op1 = TREE_OPERAND (t, 1);
9515 STRIP_NOPS (op1);
9516 if ((TREE_CODE (op0) == TARGET_EXPR && op1 == TARGET_EXPR_SLOT (op0))
9517 || TREE_CODE (op1) == EMPTY_CLASS_EXPR)
9518 r = cxx_eval_constant_expression (call, op0, allow_non_constant,
9519 addr, non_constant_p, overflow_p);
9520 else
9522 /* Check that the LHS is constant and then discard it. */
9523 cxx_eval_constant_expression (call, op0, allow_non_constant,
9524 false, non_constant_p, overflow_p);
9525 op1 = TREE_OPERAND (t, 1);
9526 r = cxx_eval_constant_expression (call, op1, allow_non_constant,
9527 addr, non_constant_p, overflow_p);
9530 break;
9532 case POINTER_PLUS_EXPR:
9533 case PLUS_EXPR:
9534 case MINUS_EXPR:
9535 case MULT_EXPR:
9536 case TRUNC_DIV_EXPR:
9537 case CEIL_DIV_EXPR:
9538 case FLOOR_DIV_EXPR:
9539 case ROUND_DIV_EXPR:
9540 case TRUNC_MOD_EXPR:
9541 case CEIL_MOD_EXPR:
9542 case ROUND_MOD_EXPR:
9543 case RDIV_EXPR:
9544 case EXACT_DIV_EXPR:
9545 case MIN_EXPR:
9546 case MAX_EXPR:
9547 case LSHIFT_EXPR:
9548 case RSHIFT_EXPR:
9549 case LROTATE_EXPR:
9550 case RROTATE_EXPR:
9551 case BIT_IOR_EXPR:
9552 case BIT_XOR_EXPR:
9553 case BIT_AND_EXPR:
9554 case TRUTH_XOR_EXPR:
9555 case LT_EXPR:
9556 case LE_EXPR:
9557 case GT_EXPR:
9558 case GE_EXPR:
9559 case EQ_EXPR:
9560 case NE_EXPR:
9561 case UNORDERED_EXPR:
9562 case ORDERED_EXPR:
9563 case UNLT_EXPR:
9564 case UNLE_EXPR:
9565 case UNGT_EXPR:
9566 case UNGE_EXPR:
9567 case UNEQ_EXPR:
9568 case LTGT_EXPR:
9569 case RANGE_EXPR:
9570 case COMPLEX_EXPR:
9571 r = cxx_eval_binary_expression (call, t, allow_non_constant, addr,
9572 non_constant_p, overflow_p);
9573 break;
9575 /* fold can introduce non-IF versions of these; still treat them as
9576 short-circuiting. */
9577 case TRUTH_AND_EXPR:
9578 case TRUTH_ANDIF_EXPR:
9579 r = cxx_eval_logical_expression (call, t, boolean_false_node,
9580 boolean_true_node,
9581 allow_non_constant, addr,
9582 non_constant_p, overflow_p);
9583 break;
9585 case TRUTH_OR_EXPR:
9586 case TRUTH_ORIF_EXPR:
9587 r = cxx_eval_logical_expression (call, t, boolean_true_node,
9588 boolean_false_node,
9589 allow_non_constant, addr,
9590 non_constant_p, overflow_p);
9591 break;
9593 case ARRAY_REF:
9594 r = cxx_eval_array_reference (call, t, allow_non_constant, addr,
9595 non_constant_p, overflow_p);
9596 break;
9598 case COMPONENT_REF:
9599 r = cxx_eval_component_reference (call, t, allow_non_constant, addr,
9600 non_constant_p, overflow_p);
9601 break;
9603 case BIT_FIELD_REF:
9604 r = cxx_eval_bit_field_ref (call, t, allow_non_constant, addr,
9605 non_constant_p, overflow_p);
9606 break;
9608 case COND_EXPR:
9609 case VEC_COND_EXPR:
9610 r = cxx_eval_conditional_expression (call, t, allow_non_constant, addr,
9611 non_constant_p, overflow_p);
9612 break;
9614 case CONSTRUCTOR:
9615 r = cxx_eval_bare_aggregate (call, t, allow_non_constant, addr,
9616 non_constant_p, overflow_p);
9617 break;
9619 case VEC_INIT_EXPR:
9620 /* We can get this in a defaulted constructor for a class with a
9621 non-static data member of array type. Either the initializer will
9622 be NULL, meaning default-initialization, or it will be an lvalue
9623 or xvalue of the same type, meaning direct-initialization from the
9624 corresponding member. */
9625 r = cxx_eval_vec_init (call, t, allow_non_constant, addr,
9626 non_constant_p, overflow_p);
9627 break;
9629 case FMA_EXPR:
9630 case VEC_PERM_EXPR:
9631 r = cxx_eval_trinary_expression (call, t, allow_non_constant, addr,
9632 non_constant_p, overflow_p);
9633 break;
9635 case CONVERT_EXPR:
9636 case VIEW_CONVERT_EXPR:
9637 case NOP_EXPR:
9639 tree oldop = TREE_OPERAND (t, 0);
9640 tree op = cxx_eval_constant_expression (call, oldop,
9641 allow_non_constant, addr,
9642 non_constant_p, overflow_p);
9643 if (*non_constant_p)
9644 return t;
9645 if (POINTER_TYPE_P (TREE_TYPE (t))
9646 && TREE_CODE (op) == INTEGER_CST
9647 && !integer_zerop (op))
9649 if (!allow_non_constant)
9650 error_at (EXPR_LOC_OR_HERE (t),
9651 "reinterpret_cast from integer to pointer");
9652 *non_constant_p = true;
9653 return t;
9655 if (op == oldop)
9656 /* We didn't fold at the top so we could check for ptr-int
9657 conversion. */
9658 return fold (t);
9659 r = fold_build1 (TREE_CODE (t), TREE_TYPE (t), op);
9660 /* Conversion of an out-of-range value has implementation-defined
9661 behavior; the language considers it different from arithmetic
9662 overflow, which is undefined. */
9663 if (TREE_OVERFLOW_P (r) && !TREE_OVERFLOW_P (op))
9664 TREE_OVERFLOW (r) = false;
9666 break;
9668 case EMPTY_CLASS_EXPR:
9669 /* This is good enough for a function argument that might not get
9670 used, and they can't do anything with it, so just return it. */
9671 return t;
9673 case LAMBDA_EXPR:
9674 case PREINCREMENT_EXPR:
9675 case POSTINCREMENT_EXPR:
9676 case PREDECREMENT_EXPR:
9677 case POSTDECREMENT_EXPR:
9678 case NEW_EXPR:
9679 case VEC_NEW_EXPR:
9680 case DELETE_EXPR:
9681 case VEC_DELETE_EXPR:
9682 case THROW_EXPR:
9683 case MODIFY_EXPR:
9684 case MODOP_EXPR:
9685 /* GCC internal stuff. */
9686 case VA_ARG_EXPR:
9687 case OBJ_TYPE_REF:
9688 case WITH_CLEANUP_EXPR:
9689 case STATEMENT_LIST:
9690 case BIND_EXPR:
9691 case NON_DEPENDENT_EXPR:
9692 case BASELINK:
9693 case EXPR_STMT:
9694 case OFFSET_REF:
9695 if (!allow_non_constant)
9696 error_at (EXPR_LOC_OR_HERE (t),
9697 "expression %qE is not a constant-expression", t);
9698 *non_constant_p = true;
9699 break;
9701 default:
9702 internal_error ("unexpected expression %qE of kind %s", t,
9703 get_tree_code_name (TREE_CODE (t)));
9704 *non_constant_p = true;
9705 break;
9708 if (r == error_mark_node)
9709 *non_constant_p = true;
9711 if (*non_constant_p)
9712 return t;
9713 else
9714 return r;
9717 static tree
9718 cxx_eval_outermost_constant_expr (tree t, bool allow_non_constant)
9720 bool non_constant_p = false;
9721 bool overflow_p = false;
9722 tree r = cxx_eval_constant_expression (NULL, t, allow_non_constant,
9723 false, &non_constant_p, &overflow_p);
9725 verify_constant (r, allow_non_constant, &non_constant_p, &overflow_p);
9727 if (TREE_CODE (t) != CONSTRUCTOR
9728 && cp_has_mutable_p (TREE_TYPE (t)))
9730 /* We allow a mutable type if the original expression was a
9731 CONSTRUCTOR so that we can do aggregate initialization of
9732 constexpr variables. */
9733 if (!allow_non_constant)
9734 error ("%qT cannot be the type of a complete constant expression "
9735 "because it has mutable sub-objects", TREE_TYPE (t));
9736 non_constant_p = true;
9739 /* Technically we should check this for all subexpressions, but that
9740 runs into problems with our internal representation of pointer
9741 subtraction and the 5.19 rules are still in flux. */
9742 if (CONVERT_EXPR_CODE_P (TREE_CODE (r))
9743 && ARITHMETIC_TYPE_P (TREE_TYPE (r))
9744 && TREE_CODE (TREE_OPERAND (r, 0)) == ADDR_EXPR)
9746 if (!allow_non_constant)
9747 error ("conversion from pointer type %qT "
9748 "to arithmetic type %qT in a constant-expression",
9749 TREE_TYPE (TREE_OPERAND (r, 0)), TREE_TYPE (r));
9750 non_constant_p = true;
9753 if (!non_constant_p && overflow_p)
9754 non_constant_p = true;
9756 if (non_constant_p && !allow_non_constant)
9757 return error_mark_node;
9758 else if (non_constant_p && TREE_CONSTANT (r))
9760 /* This isn't actually constant, so unset TREE_CONSTANT. */
9761 if (EXPR_P (r))
9762 r = copy_node (r);
9763 else if (TREE_CODE (r) == CONSTRUCTOR)
9764 r = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (r), r);
9765 else
9766 r = build_nop (TREE_TYPE (r), r);
9767 TREE_CONSTANT (r) = false;
9769 else if (non_constant_p || r == t)
9770 return t;
9772 if (TREE_CODE (r) == CONSTRUCTOR && CLASS_TYPE_P (TREE_TYPE (r)))
9774 if (TREE_CODE (t) == TARGET_EXPR
9775 && TARGET_EXPR_INITIAL (t) == r)
9776 return t;
9777 else
9779 r = get_target_expr (r);
9780 TREE_CONSTANT (r) = true;
9781 return r;
9784 else
9785 return r;
9788 /* Returns true if T is a valid subexpression of a constant expression,
9789 even if it isn't itself a constant expression. */
9791 bool
9792 is_sub_constant_expr (tree t)
9794 bool non_constant_p = false;
9795 bool overflow_p = false;
9796 cxx_eval_constant_expression (NULL, t, true, false, &non_constant_p,
9797 &overflow_p);
9798 return !non_constant_p && !overflow_p;
9801 /* If T represents a constant expression returns its reduced value.
9802 Otherwise return error_mark_node. If T is dependent, then
9803 return NULL. */
9805 tree
9806 cxx_constant_value (tree t)
9808 return cxx_eval_outermost_constant_expr (t, false);
9811 /* If T is a constant expression, returns its reduced value.
9812 Otherwise, if T does not have TREE_CONSTANT set, returns T.
9813 Otherwise, returns a version of T without TREE_CONSTANT. */
9815 tree
9816 maybe_constant_value (tree t)
9818 tree r;
9820 if (instantiation_dependent_expression_p (t)
9821 || type_unknown_p (t)
9822 || BRACE_ENCLOSED_INITIALIZER_P (t)
9823 || !potential_constant_expression (t))
9825 if (TREE_OVERFLOW_P (t))
9827 t = build_nop (TREE_TYPE (t), t);
9828 TREE_CONSTANT (t) = false;
9830 return t;
9833 r = cxx_eval_outermost_constant_expr (t, true);
9834 #ifdef ENABLE_CHECKING
9835 /* cp_tree_equal looks through NOPs, so allow them. */
9836 gcc_assert (r == t
9837 || CONVERT_EXPR_P (t)
9838 || (TREE_CONSTANT (t) && !TREE_CONSTANT (r))
9839 || !cp_tree_equal (r, t));
9840 #endif
9841 return r;
9844 /* Like maybe_constant_value, but returns a CONSTRUCTOR directly, rather
9845 than wrapped in a TARGET_EXPR. */
9847 tree
9848 maybe_constant_init (tree t)
9850 t = maybe_constant_value (t);
9851 if (TREE_CODE (t) == TARGET_EXPR)
9853 tree init = TARGET_EXPR_INITIAL (t);
9854 if (TREE_CODE (init) == CONSTRUCTOR)
9855 t = init;
9857 return t;
9860 #if 0
9861 /* FIXME see ADDR_EXPR section in potential_constant_expression_1. */
9862 /* Return true if the object referred to by REF has automatic or thread
9863 local storage. */
9865 enum { ck_ok, ck_bad, ck_unknown };
9866 static int
9867 check_automatic_or_tls (tree ref)
9869 enum machine_mode mode;
9870 HOST_WIDE_INT bitsize, bitpos;
9871 tree offset;
9872 int volatilep = 0, unsignedp = 0;
9873 tree decl = get_inner_reference (ref, &bitsize, &bitpos, &offset,
9874 &mode, &unsignedp, &volatilep, false);
9875 duration_kind dk;
9877 /* If there isn't a decl in the middle, we don't know the linkage here,
9878 and this isn't a constant expression anyway. */
9879 if (!DECL_P (decl))
9880 return ck_unknown;
9881 dk = decl_storage_duration (decl);
9882 return (dk == dk_auto || dk == dk_thread) ? ck_bad : ck_ok;
9884 #endif
9886 /* Return true if T denotes a potentially constant expression. Issue
9887 diagnostic as appropriate under control of FLAGS. If WANT_RVAL is true,
9888 an lvalue-rvalue conversion is implied.
9890 C++0x [expr.const] used to say
9892 6 An expression is a potential constant expression if it is
9893 a constant expression where all occurrences of function
9894 parameters are replaced by arbitrary constant expressions
9895 of the appropriate type.
9897 2 A conditional expression is a constant expression unless it
9898 involves one of the following as a potentially evaluated
9899 subexpression (3.2), but subexpressions of logical AND (5.14),
9900 logical OR (5.15), and conditional (5.16) operations that are
9901 not evaluated are not considered. */
9903 static bool
9904 potential_constant_expression_1 (tree t, bool want_rval, tsubst_flags_t flags)
9906 enum { any = false, rval = true };
9907 int i;
9908 tree tmp;
9910 if (t == error_mark_node)
9911 return false;
9912 if (t == NULL_TREE)
9913 return true;
9914 if (TREE_THIS_VOLATILE (t))
9916 if (flags & tf_error)
9917 error ("expression %qE has side-effects", t);
9918 return false;
9920 if (CONSTANT_CLASS_P (t))
9921 return true;
9923 switch (TREE_CODE (t))
9925 case FUNCTION_DECL:
9926 case BASELINK:
9927 case TEMPLATE_DECL:
9928 case OVERLOAD:
9929 case TEMPLATE_ID_EXPR:
9930 case LABEL_DECL:
9931 case LABEL_EXPR:
9932 case CONST_DECL:
9933 case SIZEOF_EXPR:
9934 case ALIGNOF_EXPR:
9935 case OFFSETOF_EXPR:
9936 case NOEXCEPT_EXPR:
9937 case TEMPLATE_PARM_INDEX:
9938 case TRAIT_EXPR:
9939 case IDENTIFIER_NODE:
9940 case USERDEF_LITERAL:
9941 /* We can see a FIELD_DECL in a pointer-to-member expression. */
9942 case FIELD_DECL:
9943 case PARM_DECL:
9944 case USING_DECL:
9945 return true;
9947 case AGGR_INIT_EXPR:
9948 case CALL_EXPR:
9949 /* -- an invocation of a function other than a constexpr function
9950 or a constexpr constructor. */
9952 tree fun = get_function_named_in_call (t);
9953 const int nargs = call_expr_nargs (t);
9954 i = 0;
9956 if (is_overloaded_fn (fun))
9958 if (TREE_CODE (fun) == FUNCTION_DECL)
9960 if (builtin_valid_in_constant_expr_p (fun))
9961 return true;
9962 if (!DECL_DECLARED_CONSTEXPR_P (fun)
9963 /* Allow any built-in function; if the expansion
9964 isn't constant, we'll deal with that then. */
9965 && !is_builtin_fn (fun))
9967 if (flags & tf_error)
9969 error_at (EXPR_LOC_OR_HERE (t),
9970 "call to non-constexpr function %qD", fun);
9971 explain_invalid_constexpr_fn (fun);
9973 return false;
9975 /* A call to a non-static member function takes the address
9976 of the object as the first argument. But in a constant
9977 expression the address will be folded away, so look
9978 through it now. */
9979 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fun)
9980 && !DECL_CONSTRUCTOR_P (fun))
9982 tree x = get_nth_callarg (t, 0);
9983 if (is_this_parameter (x))
9985 if (DECL_CONTEXT (x) == NULL_TREE
9986 || DECL_CONSTRUCTOR_P (DECL_CONTEXT (x)))
9988 if (flags & tf_error)
9989 sorry ("calling a member function of the "
9990 "object being constructed in a constant "
9991 "expression");
9992 return false;
9994 /* Otherwise OK. */;
9996 else if (!potential_constant_expression_1 (x, rval, flags))
9997 return false;
9998 i = 1;
10001 else
10003 if (!potential_constant_expression_1 (fun, true, flags))
10004 return false;
10005 fun = get_first_fn (fun);
10007 /* Skip initial arguments to base constructors. */
10008 if (DECL_BASE_CONSTRUCTOR_P (fun))
10009 i = num_artificial_parms_for (fun);
10010 fun = DECL_ORIGIN (fun);
10012 else
10014 if (potential_constant_expression_1 (fun, rval, flags))
10015 /* Might end up being a constant function pointer. */;
10016 else
10017 return false;
10019 for (; i < nargs; ++i)
10021 tree x = get_nth_callarg (t, i);
10022 if (!potential_constant_expression_1 (x, rval, flags))
10023 return false;
10025 return true;
10028 case NON_LVALUE_EXPR:
10029 /* -- an lvalue-to-rvalue conversion (4.1) unless it is applied to
10030 -- an lvalue of integral type that refers to a non-volatile
10031 const variable or static data member initialized with
10032 constant expressions, or
10034 -- an lvalue of literal type that refers to non-volatile
10035 object defined with constexpr, or that refers to a
10036 sub-object of such an object; */
10037 return potential_constant_expression_1 (TREE_OPERAND (t, 0), rval, flags);
10039 case VAR_DECL:
10040 if (want_rval && !decl_constant_var_p (t)
10041 && !dependent_type_p (TREE_TYPE (t)))
10043 if (flags & tf_error)
10044 non_const_var_error (t);
10045 return false;
10047 return true;
10049 case NOP_EXPR:
10050 case CONVERT_EXPR:
10051 case VIEW_CONVERT_EXPR:
10052 /* -- a reinterpret_cast. FIXME not implemented, and this rule
10053 may change to something more specific to type-punning (DR 1312). */
10055 tree from = TREE_OPERAND (t, 0);
10056 if (POINTER_TYPE_P (TREE_TYPE (t))
10057 && TREE_CODE (from) == INTEGER_CST
10058 && !integer_zerop (from))
10060 if (flags & tf_error)
10061 error_at (EXPR_LOC_OR_HERE (t),
10062 "reinterpret_cast from integer to pointer");
10063 return false;
10065 return (potential_constant_expression_1
10066 (from, TREE_CODE (t) != VIEW_CONVERT_EXPR, flags));
10069 case ADDR_EXPR:
10070 /* -- a unary operator & that is applied to an lvalue that
10071 designates an object with thread or automatic storage
10072 duration; */
10073 t = TREE_OPERAND (t, 0);
10074 #if 0
10075 /* FIXME adjust when issue 1197 is fully resolved. For now don't do
10076 any checking here, as we might dereference the pointer later. If
10077 we remove this code, also remove check_automatic_or_tls. */
10078 i = check_automatic_or_tls (t);
10079 if (i == ck_ok)
10080 return true;
10081 if (i == ck_bad)
10083 if (flags & tf_error)
10084 error ("address-of an object %qE with thread local or "
10085 "automatic storage is not a constant expression", t);
10086 return false;
10088 #endif
10089 return potential_constant_expression_1 (t, any, flags);
10091 case COMPONENT_REF:
10092 case BIT_FIELD_REF:
10093 case ARROW_EXPR:
10094 case OFFSET_REF:
10095 /* -- a class member access unless its postfix-expression is
10096 of literal type or of pointer to literal type. */
10097 /* This test would be redundant, as it follows from the
10098 postfix-expression being a potential constant expression. */
10099 return potential_constant_expression_1 (TREE_OPERAND (t, 0),
10100 want_rval, flags);
10102 case EXPR_PACK_EXPANSION:
10103 return potential_constant_expression_1 (PACK_EXPANSION_PATTERN (t),
10104 want_rval, flags);
10106 case INDIRECT_REF:
10108 tree x = TREE_OPERAND (t, 0);
10109 STRIP_NOPS (x);
10110 if (is_this_parameter (x))
10112 if (DECL_CONTEXT (x)
10113 && !DECL_DECLARED_CONSTEXPR_P (DECL_CONTEXT (x)))
10115 if (flags & tf_error)
10116 error ("use of %<this%> in a constant expression");
10117 return false;
10119 if (want_rval && DECL_CONTEXT (x)
10120 && DECL_CONSTRUCTOR_P (DECL_CONTEXT (x)))
10122 if (flags & tf_error)
10123 sorry ("use of the value of the object being constructed "
10124 "in a constant expression");
10125 return false;
10127 return true;
10129 return potential_constant_expression_1 (x, rval, flags);
10132 case LAMBDA_EXPR:
10133 case DYNAMIC_CAST_EXPR:
10134 case PSEUDO_DTOR_EXPR:
10135 case PREINCREMENT_EXPR:
10136 case POSTINCREMENT_EXPR:
10137 case PREDECREMENT_EXPR:
10138 case POSTDECREMENT_EXPR:
10139 case NEW_EXPR:
10140 case VEC_NEW_EXPR:
10141 case DELETE_EXPR:
10142 case VEC_DELETE_EXPR:
10143 case THROW_EXPR:
10144 case MODIFY_EXPR:
10145 case MODOP_EXPR:
10146 case OMP_ATOMIC:
10147 case OMP_ATOMIC_READ:
10148 case OMP_ATOMIC_CAPTURE_OLD:
10149 case OMP_ATOMIC_CAPTURE_NEW:
10150 /* GCC internal stuff. */
10151 case VA_ARG_EXPR:
10152 case OBJ_TYPE_REF:
10153 case WITH_CLEANUP_EXPR:
10154 case CLEANUP_POINT_EXPR:
10155 case MUST_NOT_THROW_EXPR:
10156 case TRY_CATCH_EXPR:
10157 case STATEMENT_LIST:
10158 /* Don't bother trying to define a subset of statement-expressions to
10159 be constant-expressions, at least for now. */
10160 case STMT_EXPR:
10161 case EXPR_STMT:
10162 case BIND_EXPR:
10163 case TRANSACTION_EXPR:
10164 case IF_STMT:
10165 case DO_STMT:
10166 case FOR_STMT:
10167 case WHILE_STMT:
10168 if (flags & tf_error)
10169 error ("expression %qE is not a constant-expression", t);
10170 return false;
10172 case TYPEID_EXPR:
10173 /* -- a typeid expression whose operand is of polymorphic
10174 class type; */
10176 tree e = TREE_OPERAND (t, 0);
10177 if (!TYPE_P (e) && !type_dependent_expression_p (e)
10178 && TYPE_POLYMORPHIC_P (TREE_TYPE (e)))
10180 if (flags & tf_error)
10181 error ("typeid-expression is not a constant expression "
10182 "because %qE is of polymorphic type", e);
10183 return false;
10185 return true;
10188 case MINUS_EXPR:
10189 /* -- a subtraction where both operands are pointers. */
10190 if (TYPE_PTR_P (TREE_OPERAND (t, 0))
10191 && TYPE_PTR_P (TREE_OPERAND (t, 1)))
10193 if (flags & tf_error)
10194 error ("difference of two pointer expressions is not "
10195 "a constant expression");
10196 return false;
10198 want_rval = true;
10199 goto binary;
10201 case LT_EXPR:
10202 case LE_EXPR:
10203 case GT_EXPR:
10204 case GE_EXPR:
10205 case EQ_EXPR:
10206 case NE_EXPR:
10207 /* -- a relational or equality operator where at least
10208 one of the operands is a pointer. */
10209 if (TYPE_PTR_P (TREE_OPERAND (t, 0))
10210 || TYPE_PTR_P (TREE_OPERAND (t, 1)))
10212 if (flags & tf_error)
10213 error ("pointer comparison expression is not a "
10214 "constant expression");
10215 return false;
10217 want_rval = true;
10218 goto binary;
10220 case BIT_NOT_EXPR:
10221 /* A destructor. */
10222 if (TYPE_P (TREE_OPERAND (t, 0)))
10223 return true;
10224 /* else fall through. */
10226 case REALPART_EXPR:
10227 case IMAGPART_EXPR:
10228 case CONJ_EXPR:
10229 case SAVE_EXPR:
10230 case FIX_TRUNC_EXPR:
10231 case FLOAT_EXPR:
10232 case NEGATE_EXPR:
10233 case ABS_EXPR:
10234 case TRUTH_NOT_EXPR:
10235 case FIXED_CONVERT_EXPR:
10236 case UNARY_PLUS_EXPR:
10237 return potential_constant_expression_1 (TREE_OPERAND (t, 0), rval,
10238 flags);
10240 case CAST_EXPR:
10241 case CONST_CAST_EXPR:
10242 case STATIC_CAST_EXPR:
10243 case REINTERPRET_CAST_EXPR:
10244 case IMPLICIT_CONV_EXPR:
10245 if (cxx_dialect < cxx11
10246 && !dependent_type_p (TREE_TYPE (t))
10247 && !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t)))
10248 /* In C++98, a conversion to non-integral type can't be part of a
10249 constant expression. */
10251 if (flags & tf_error)
10252 error ("cast to non-integral type %qT in a constant expression",
10253 TREE_TYPE (t));
10254 return false;
10257 return (potential_constant_expression_1
10258 (TREE_OPERAND (t, 0),
10259 TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE, flags));
10261 case PAREN_EXPR:
10262 case NON_DEPENDENT_EXPR:
10263 /* For convenience. */
10264 case RETURN_EXPR:
10265 return potential_constant_expression_1 (TREE_OPERAND (t, 0),
10266 want_rval, flags);
10268 case SCOPE_REF:
10269 return potential_constant_expression_1 (TREE_OPERAND (t, 1),
10270 want_rval, flags);
10272 case TARGET_EXPR:
10273 if (!literal_type_p (TREE_TYPE (t)))
10275 if (flags & tf_error)
10277 error ("temporary of non-literal type %qT in a "
10278 "constant expression", TREE_TYPE (t));
10279 explain_non_literal_class (TREE_TYPE (t));
10281 return false;
10283 case INIT_EXPR:
10284 return potential_constant_expression_1 (TREE_OPERAND (t, 1),
10285 rval, flags);
10287 case CONSTRUCTOR:
10289 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t);
10290 constructor_elt *ce;
10291 for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
10292 if (!potential_constant_expression_1 (ce->value, want_rval, flags))
10293 return false;
10294 return true;
10297 case TREE_LIST:
10299 gcc_assert (TREE_PURPOSE (t) == NULL_TREE
10300 || DECL_P (TREE_PURPOSE (t)));
10301 if (!potential_constant_expression_1 (TREE_VALUE (t), want_rval,
10302 flags))
10303 return false;
10304 if (TREE_CHAIN (t) == NULL_TREE)
10305 return true;
10306 return potential_constant_expression_1 (TREE_CHAIN (t), want_rval,
10307 flags);
10310 case TRUNC_DIV_EXPR:
10311 case CEIL_DIV_EXPR:
10312 case FLOOR_DIV_EXPR:
10313 case ROUND_DIV_EXPR:
10314 case TRUNC_MOD_EXPR:
10315 case CEIL_MOD_EXPR:
10316 case ROUND_MOD_EXPR:
10318 tree denom = TREE_OPERAND (t, 1);
10319 if (!potential_constant_expression_1 (denom, rval, flags))
10320 return false;
10321 /* We can't call cxx_eval_outermost_constant_expr on an expression
10322 that hasn't been through fold_non_dependent_expr yet. */
10323 if (!processing_template_decl)
10324 denom = cxx_eval_outermost_constant_expr (denom, true);
10325 if (integer_zerop (denom))
10327 if (flags & tf_error)
10328 error ("division by zero is not a constant-expression");
10329 return false;
10331 else
10333 want_rval = true;
10334 return potential_constant_expression_1 (TREE_OPERAND (t, 0),
10335 want_rval, flags);
10339 case COMPOUND_EXPR:
10341 /* check_return_expr sometimes wraps a TARGET_EXPR in a
10342 COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR
10343 introduced by build_call_a. */
10344 tree op0 = TREE_OPERAND (t, 0);
10345 tree op1 = TREE_OPERAND (t, 1);
10346 STRIP_NOPS (op1);
10347 if ((TREE_CODE (op0) == TARGET_EXPR && op1 == TARGET_EXPR_SLOT (op0))
10348 || TREE_CODE (op1) == EMPTY_CLASS_EXPR)
10349 return potential_constant_expression_1 (op0, want_rval, flags);
10350 else
10351 goto binary;
10354 /* If the first operand is the non-short-circuit constant, look at
10355 the second operand; otherwise we only care about the first one for
10356 potentiality. */
10357 case TRUTH_AND_EXPR:
10358 case TRUTH_ANDIF_EXPR:
10359 tmp = boolean_true_node;
10360 goto truth;
10361 case TRUTH_OR_EXPR:
10362 case TRUTH_ORIF_EXPR:
10363 tmp = boolean_false_node;
10364 truth:
10366 tree op = TREE_OPERAND (t, 0);
10367 if (!potential_constant_expression_1 (op, rval, flags))
10368 return false;
10369 if (!processing_template_decl)
10370 op = cxx_eval_outermost_constant_expr (op, true);
10371 if (tree_int_cst_equal (op, tmp))
10372 return potential_constant_expression_1 (TREE_OPERAND (t, 1), rval, flags);
10373 else
10374 return true;
10377 case PLUS_EXPR:
10378 case MULT_EXPR:
10379 case POINTER_PLUS_EXPR:
10380 case RDIV_EXPR:
10381 case EXACT_DIV_EXPR:
10382 case MIN_EXPR:
10383 case MAX_EXPR:
10384 case LSHIFT_EXPR:
10385 case RSHIFT_EXPR:
10386 case LROTATE_EXPR:
10387 case RROTATE_EXPR:
10388 case BIT_IOR_EXPR:
10389 case BIT_XOR_EXPR:
10390 case BIT_AND_EXPR:
10391 case TRUTH_XOR_EXPR:
10392 case UNORDERED_EXPR:
10393 case ORDERED_EXPR:
10394 case UNLT_EXPR:
10395 case UNLE_EXPR:
10396 case UNGT_EXPR:
10397 case UNGE_EXPR:
10398 case UNEQ_EXPR:
10399 case LTGT_EXPR:
10400 case RANGE_EXPR:
10401 case COMPLEX_EXPR:
10402 want_rval = true;
10403 /* Fall through. */
10404 case ARRAY_REF:
10405 case ARRAY_RANGE_REF:
10406 case MEMBER_REF:
10407 case DOTSTAR_EXPR:
10408 binary:
10409 for (i = 0; i < 2; ++i)
10410 if (!potential_constant_expression_1 (TREE_OPERAND (t, i),
10411 want_rval, flags))
10412 return false;
10413 return true;
10415 case ARRAY_NOTATION_REF:
10416 return false;
10418 case FMA_EXPR:
10419 case VEC_PERM_EXPR:
10420 for (i = 0; i < 3; ++i)
10421 if (!potential_constant_expression_1 (TREE_OPERAND (t, i),
10422 true, flags))
10423 return false;
10424 return true;
10426 case COND_EXPR:
10427 case VEC_COND_EXPR:
10428 /* If the condition is a known constant, we know which of the legs we
10429 care about; otherwise we only require that the condition and
10430 either of the legs be potentially constant. */
10431 tmp = TREE_OPERAND (t, 0);
10432 if (!potential_constant_expression_1 (tmp, rval, flags))
10433 return false;
10434 if (!processing_template_decl)
10435 tmp = cxx_eval_outermost_constant_expr (tmp, true);
10436 if (integer_zerop (tmp))
10437 return potential_constant_expression_1 (TREE_OPERAND (t, 2),
10438 want_rval, flags);
10439 else if (TREE_CODE (tmp) == INTEGER_CST)
10440 return potential_constant_expression_1 (TREE_OPERAND (t, 1),
10441 want_rval, flags);
10442 for (i = 1; i < 3; ++i)
10443 if (potential_constant_expression_1 (TREE_OPERAND (t, i),
10444 want_rval, tf_none))
10445 return true;
10446 if (flags & tf_error)
10447 error ("expression %qE is not a constant-expression", t);
10448 return false;
10450 case VEC_INIT_EXPR:
10451 if (VEC_INIT_EXPR_IS_CONSTEXPR (t))
10452 return true;
10453 if (flags & tf_error)
10455 error ("non-constant array initialization");
10456 diagnose_non_constexpr_vec_init (t);
10458 return false;
10460 default:
10461 if (objc_is_property_ref (t))
10462 return false;
10464 sorry ("unexpected AST of kind %s", get_tree_code_name (TREE_CODE (t)));
10465 gcc_unreachable();
10466 return false;
10470 /* The main entry point to the above. */
10472 bool
10473 potential_constant_expression (tree t)
10475 return potential_constant_expression_1 (t, false, tf_none);
10478 /* As above, but require a constant rvalue. */
10480 bool
10481 potential_rvalue_constant_expression (tree t)
10483 return potential_constant_expression_1 (t, true, tf_none);
10486 /* Like above, but complain about non-constant expressions. */
10488 bool
10489 require_potential_constant_expression (tree t)
10491 return potential_constant_expression_1 (t, false, tf_warning_or_error);
10494 /* Cross product of the above. */
10496 bool
10497 require_potential_rvalue_constant_expression (tree t)
10499 return potential_constant_expression_1 (t, true, tf_warning_or_error);
10502 /* Insert the deduced return type for an auto function. */
10504 void
10505 apply_deduced_return_type (tree fco, tree return_type)
10507 tree result;
10509 if (return_type == error_mark_node)
10510 return;
10512 if (LAMBDA_FUNCTION_P (fco))
10514 tree lambda = CLASSTYPE_LAMBDA_EXPR (current_class_type);
10515 LAMBDA_EXPR_RETURN_TYPE (lambda) = return_type;
10518 if (DECL_CONV_FN_P (fco))
10519 DECL_NAME (fco) = mangle_conv_op_name_for_type (return_type);
10521 TREE_TYPE (fco) = change_return_type (return_type, TREE_TYPE (fco));
10523 result = DECL_RESULT (fco);
10524 if (result == NULL_TREE)
10525 return;
10526 if (TREE_TYPE (result) == return_type)
10527 return;
10529 /* We already have a DECL_RESULT from start_preparsed_function.
10530 Now we need to redo the work it and allocate_struct_function
10531 did to reflect the new type. */
10532 gcc_assert (current_function_decl == fco);
10533 result = build_decl (input_location, RESULT_DECL, NULL_TREE,
10534 TYPE_MAIN_VARIANT (return_type));
10535 DECL_ARTIFICIAL (result) = 1;
10536 DECL_IGNORED_P (result) = 1;
10537 cp_apply_type_quals_to_decl (cp_type_quals (return_type),
10538 result);
10540 DECL_RESULT (fco) = result;
10542 if (!processing_template_decl)
10544 bool aggr = aggregate_value_p (result, fco);
10545 #ifdef PCC_STATIC_STRUCT_RETURN
10546 cfun->returns_pcc_struct = aggr;
10547 #endif
10548 cfun->returns_struct = aggr;
10553 /* DECL is a local variable or parameter from the surrounding scope of a
10554 lambda-expression. Returns the decltype for a use of the capture field
10555 for DECL even if it hasn't been captured yet. */
10557 static tree
10558 capture_decltype (tree decl)
10560 tree lam = CLASSTYPE_LAMBDA_EXPR (DECL_CONTEXT (current_function_decl));
10561 /* FIXME do lookup instead of list walk? */
10562 tree cap = value_member (decl, LAMBDA_EXPR_CAPTURE_LIST (lam));
10563 tree type;
10565 if (cap)
10566 type = TREE_TYPE (TREE_PURPOSE (cap));
10567 else
10568 switch (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lam))
10570 case CPLD_NONE:
10571 error ("%qD is not captured", decl);
10572 return error_mark_node;
10574 case CPLD_COPY:
10575 type = TREE_TYPE (decl);
10576 if (TREE_CODE (type) == REFERENCE_TYPE
10577 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
10578 type = TREE_TYPE (type);
10579 break;
10581 case CPLD_REFERENCE:
10582 type = TREE_TYPE (decl);
10583 if (TREE_CODE (type) != REFERENCE_TYPE)
10584 type = build_reference_type (TREE_TYPE (decl));
10585 break;
10587 default:
10588 gcc_unreachable ();
10591 if (TREE_CODE (type) != REFERENCE_TYPE)
10593 if (!LAMBDA_EXPR_MUTABLE_P (lam))
10594 type = cp_build_qualified_type (type, (cp_type_quals (type)
10595 |TYPE_QUAL_CONST));
10596 type = build_reference_type (type);
10598 return type;
10601 #include "gt-cp-semantics.h"