* config/darwin.c (darwin_assemble_visibility): Treat
[official-gcc.git] / gcc / cp / semantics.c
blob68cbb4ba8c408ac978cbded38a1b975fdc44fbf7
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, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
7 2008, 2009, 2010, 2011, 2012 Free Software Foundation, Inc.
8 Written by Mark Mitchell (mmitchell@usa.net) based on code found
9 formerly in parse.y and pt.c.
11 This file is part of GCC.
13 GCC is free software; you can redistribute it and/or modify it
14 under the terms of the GNU General Public License as published by
15 the Free Software Foundation; either version 3, or (at your option)
16 any later version.
18 GCC is distributed in the hope that it will be useful, but
19 WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with GCC; see the file COPYING3. If not see
25 <http://www.gnu.org/licenses/>. */
27 #include "config.h"
28 #include "system.h"
29 #include "coretypes.h"
30 #include "tm.h"
31 #include "tree.h"
32 #include "cp-tree.h"
33 #include "c-family/c-common.h"
34 #include "c-family/c-objc.h"
35 #include "tree-inline.h"
36 #include "intl.h"
37 #include "toplev.h"
38 #include "flags.h"
39 #include "timevar.h"
40 #include "diagnostic.h"
41 #include "cgraph.h"
42 #include "tree-iterator.h"
43 #include "vec.h"
44 #include "target.h"
45 #include "gimple.h"
46 #include "bitmap.h"
48 /* There routines provide a modular interface to perform many parsing
49 operations. They may therefore be used during actual parsing, or
50 during template instantiation, which may be regarded as a
51 degenerate form of parsing. */
53 static tree maybe_convert_cond (tree);
54 static tree finalize_nrv_r (tree *, int *, void *);
55 static tree capture_decltype (tree);
58 /* Deferred Access Checking Overview
59 ---------------------------------
61 Most C++ expressions and declarations require access checking
62 to be performed during parsing. However, in several cases,
63 this has to be treated differently.
65 For member declarations, access checking has to be deferred
66 until more information about the declaration is known. For
67 example:
69 class A {
70 typedef int X;
71 public:
72 X f();
75 A::X A::f();
76 A::X g();
78 When we are parsing the function return type `A::X', we don't
79 really know if this is allowed until we parse the function name.
81 Furthermore, some contexts require that access checking is
82 never performed at all. These include class heads, and template
83 instantiations.
85 Typical use of access checking functions is described here:
87 1. When we enter a context that requires certain access checking
88 mode, the function `push_deferring_access_checks' is called with
89 DEFERRING argument specifying the desired mode. Access checking
90 may be performed immediately (dk_no_deferred), deferred
91 (dk_deferred), or not performed (dk_no_check).
93 2. When a declaration such as a type, or a variable, is encountered,
94 the function `perform_or_defer_access_check' is called. It
95 maintains a VEC of all deferred checks.
97 3. The global `current_class_type' or `current_function_decl' is then
98 setup by the parser. `enforce_access' relies on these information
99 to check access.
101 4. Upon exiting the context mentioned in step 1,
102 `perform_deferred_access_checks' is called to check all declaration
103 stored in the VEC. `pop_deferring_access_checks' is then
104 called to restore the previous access checking mode.
106 In case of parsing error, we simply call `pop_deferring_access_checks'
107 without `perform_deferred_access_checks'. */
109 typedef struct GTY(()) deferred_access {
110 /* A VEC representing name-lookups for which we have deferred
111 checking access controls. We cannot check the accessibility of
112 names used in a decl-specifier-seq until we know what is being
113 declared because code like:
115 class A {
116 class B {};
117 B* f();
120 A::B* A::f() { return 0; }
122 is valid, even though `A::B' is not generally accessible. */
123 VEC (deferred_access_check,gc)* GTY(()) deferred_access_checks;
125 /* The current mode of access checks. */
126 enum deferring_kind deferring_access_checks_kind;
128 } deferred_access;
129 DEF_VEC_O (deferred_access);
130 DEF_VEC_ALLOC_O (deferred_access,gc);
132 /* Data for deferred access checking. */
133 static GTY(()) VEC(deferred_access,gc) *deferred_access_stack;
134 static GTY(()) unsigned deferred_access_no_check;
136 /* Save the current deferred access states and start deferred
137 access checking iff DEFER_P is true. */
139 void
140 push_deferring_access_checks (deferring_kind deferring)
142 /* For context like template instantiation, access checking
143 disabling applies to all nested context. */
144 if (deferred_access_no_check || deferring == dk_no_check)
145 deferred_access_no_check++;
146 else
148 deferred_access e = {NULL, deferring};
149 VEC_safe_push (deferred_access, gc, deferred_access_stack, e);
153 /* Resume deferring access checks again after we stopped doing
154 this previously. */
156 void
157 resume_deferring_access_checks (void)
159 if (!deferred_access_no_check)
160 VEC_last (deferred_access, deferred_access_stack)
161 .deferring_access_checks_kind = dk_deferred;
164 /* Stop deferring access checks. */
166 void
167 stop_deferring_access_checks (void)
169 if (!deferred_access_no_check)
170 VEC_last (deferred_access, deferred_access_stack)
171 .deferring_access_checks_kind = dk_no_deferred;
174 /* Discard the current deferred access checks and restore the
175 previous states. */
177 void
178 pop_deferring_access_checks (void)
180 if (deferred_access_no_check)
181 deferred_access_no_check--;
182 else
183 VEC_pop (deferred_access, deferred_access_stack);
186 /* Returns a TREE_LIST representing the deferred checks.
187 The TREE_PURPOSE of each node is the type through which the
188 access occurred; the TREE_VALUE is the declaration named.
191 VEC (deferred_access_check,gc)*
192 get_deferred_access_checks (void)
194 if (deferred_access_no_check)
195 return NULL;
196 else
197 return (VEC_last (deferred_access, deferred_access_stack)
198 .deferred_access_checks);
201 /* Take current deferred checks and combine with the
202 previous states if we also defer checks previously.
203 Otherwise perform checks now. */
205 void
206 pop_to_parent_deferring_access_checks (void)
208 if (deferred_access_no_check)
209 deferred_access_no_check--;
210 else
212 VEC (deferred_access_check,gc) *checks;
213 deferred_access *ptr;
215 checks = (VEC_last (deferred_access, deferred_access_stack)
216 .deferred_access_checks);
218 VEC_pop (deferred_access, deferred_access_stack);
219 ptr = &VEC_last (deferred_access, deferred_access_stack);
220 if (ptr->deferring_access_checks_kind == dk_no_deferred)
222 /* Check access. */
223 perform_access_checks (checks, tf_warning_or_error);
225 else
227 /* Merge with parent. */
228 int i, j;
229 deferred_access_check *chk, *probe;
231 FOR_EACH_VEC_ELT (deferred_access_check, checks, i, chk)
233 FOR_EACH_VEC_ELT (deferred_access_check,
234 ptr->deferred_access_checks, j, probe)
236 if (probe->binfo == chk->binfo &&
237 probe->decl == chk->decl &&
238 probe->diag_decl == chk->diag_decl)
239 goto found;
241 /* Insert into parent's checks. */
242 VEC_safe_push (deferred_access_check, gc,
243 ptr->deferred_access_checks, *chk);
244 found:;
250 /* Perform the access checks in CHECKS. The TREE_PURPOSE of each node
251 is the BINFO indicating the qualifying scope used to access the
252 DECL node stored in the TREE_VALUE of the node. If CHECKS is empty
253 or we aren't in SFINAE context or all the checks succeed return TRUE,
254 otherwise FALSE. */
256 bool
257 perform_access_checks (VEC (deferred_access_check,gc)* checks,
258 tsubst_flags_t complain)
260 int i;
261 deferred_access_check *chk;
262 location_t loc = input_location;
263 bool ok = true;
265 if (!checks)
266 return true;
268 FOR_EACH_VEC_ELT (deferred_access_check, checks, i, chk)
270 input_location = chk->loc;
271 ok &= enforce_access (chk->binfo, chk->decl, chk->diag_decl, complain);
274 input_location = loc;
275 return (complain & tf_error) ? true : ok;
278 /* Perform the deferred access checks.
280 After performing the checks, we still have to keep the list
281 `deferred_access_stack->deferred_access_checks' since we may want
282 to check access for them again later in a different context.
283 For example:
285 class A {
286 typedef int X;
287 static X a;
289 A::X A::a, x; // No error for `A::a', error for `x'
291 We have to perform deferred access of `A::X', first with `A::a',
292 next with `x'. Return value like perform_access_checks above. */
294 bool
295 perform_deferred_access_checks (tsubst_flags_t complain)
297 return perform_access_checks (get_deferred_access_checks (), complain);
300 /* Defer checking the accessibility of DECL, when looked up in
301 BINFO. DIAG_DECL is the declaration to use to print diagnostics.
302 Return value like perform_access_checks above. */
304 bool
305 perform_or_defer_access_check (tree binfo, tree decl, tree diag_decl,
306 tsubst_flags_t complain)
308 int i;
309 deferred_access *ptr;
310 deferred_access_check *chk;
313 /* Exit if we are in a context that no access checking is performed.
315 if (deferred_access_no_check)
316 return true;
318 gcc_assert (TREE_CODE (binfo) == TREE_BINFO);
320 ptr = &VEC_last (deferred_access, deferred_access_stack);
322 /* If we are not supposed to defer access checks, just check now. */
323 if (ptr->deferring_access_checks_kind == dk_no_deferred)
325 bool ok = enforce_access (binfo, decl, diag_decl, complain);
326 return (complain & tf_error) ? true : ok;
329 /* See if we are already going to perform this check. */
330 FOR_EACH_VEC_ELT (deferred_access_check,
331 ptr->deferred_access_checks, i, chk)
333 if (chk->decl == decl && chk->binfo == binfo &&
334 chk->diag_decl == diag_decl)
336 return true;
339 /* If not, record the check. */
340 deferred_access_check new_access = {binfo, decl, diag_decl, input_location};
341 VEC_safe_push (deferred_access_check, gc, ptr->deferred_access_checks,
342 new_access);
344 return true;
347 /* Returns nonzero if the current statement is a full expression,
348 i.e. temporaries created during that statement should be destroyed
349 at the end of the statement. */
352 stmts_are_full_exprs_p (void)
354 return current_stmt_tree ()->stmts_are_full_exprs_p;
357 /* T is a statement. Add it to the statement-tree. This is the C++
358 version. The C/ObjC frontends have a slightly different version of
359 this function. */
361 tree
362 add_stmt (tree t)
364 enum tree_code code = TREE_CODE (t);
366 if (EXPR_P (t) && code != LABEL_EXPR)
368 if (!EXPR_HAS_LOCATION (t))
369 SET_EXPR_LOCATION (t, input_location);
371 /* When we expand a statement-tree, we must know whether or not the
372 statements are full-expressions. We record that fact here. */
373 STMT_IS_FULL_EXPR_P (t) = stmts_are_full_exprs_p ();
376 /* Add T to the statement-tree. Non-side-effect statements need to be
377 recorded during statement expressions. */
378 gcc_checking_assert (!VEC_empty (tree, stmt_list_stack));
379 append_to_statement_list_force (t, &cur_stmt_list);
381 return t;
384 /* Returns the stmt_tree to which statements are currently being added. */
386 stmt_tree
387 current_stmt_tree (void)
389 return (cfun
390 ? &cfun->language->base.x_stmt_tree
391 : &scope_chain->x_stmt_tree);
394 /* If statements are full expressions, wrap STMT in a CLEANUP_POINT_EXPR. */
396 static tree
397 maybe_cleanup_point_expr (tree expr)
399 if (!processing_template_decl && stmts_are_full_exprs_p ())
400 expr = fold_build_cleanup_point_expr (TREE_TYPE (expr), expr);
401 return expr;
404 /* Like maybe_cleanup_point_expr except have the type of the new expression be
405 void so we don't need to create a temporary variable to hold the inner
406 expression. The reason why we do this is because the original type might be
407 an aggregate and we cannot create a temporary variable for that type. */
409 tree
410 maybe_cleanup_point_expr_void (tree expr)
412 if (!processing_template_decl && stmts_are_full_exprs_p ())
413 expr = fold_build_cleanup_point_expr (void_type_node, expr);
414 return expr;
419 /* Create a declaration statement for the declaration given by the DECL. */
421 void
422 add_decl_expr (tree decl)
424 tree r = build_stmt (input_location, DECL_EXPR, decl);
425 if (DECL_INITIAL (decl)
426 || (DECL_SIZE (decl) && TREE_SIDE_EFFECTS (DECL_SIZE (decl))))
427 r = maybe_cleanup_point_expr_void (r);
428 add_stmt (r);
431 /* Finish a scope. */
433 tree
434 do_poplevel (tree stmt_list)
436 tree block = NULL;
438 if (stmts_are_full_exprs_p ())
439 block = poplevel (kept_level_p (), 1, 0);
441 stmt_list = pop_stmt_list (stmt_list);
443 if (!processing_template_decl)
445 stmt_list = c_build_bind_expr (input_location, block, stmt_list);
446 /* ??? See c_end_compound_stmt re statement expressions. */
449 return stmt_list;
452 /* Begin a new scope. */
454 static tree
455 do_pushlevel (scope_kind sk)
457 tree ret = push_stmt_list ();
458 if (stmts_are_full_exprs_p ())
459 begin_scope (sk, NULL);
460 return ret;
463 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
464 when the current scope is exited. EH_ONLY is true when this is not
465 meant to apply to normal control flow transfer. */
467 void
468 push_cleanup (tree decl, tree cleanup, bool eh_only)
470 tree stmt = build_stmt (input_location, CLEANUP_STMT, NULL, cleanup, decl);
471 CLEANUP_EH_ONLY (stmt) = eh_only;
472 add_stmt (stmt);
473 CLEANUP_BODY (stmt) = push_stmt_list ();
476 /* Begin a conditional that might contain a declaration. When generating
477 normal code, we want the declaration to appear before the statement
478 containing the conditional. When generating template code, we want the
479 conditional to be rendered as the raw DECL_EXPR. */
481 static void
482 begin_cond (tree *cond_p)
484 if (processing_template_decl)
485 *cond_p = push_stmt_list ();
488 /* Finish such a conditional. */
490 static void
491 finish_cond (tree *cond_p, tree expr)
493 if (processing_template_decl)
495 tree cond = pop_stmt_list (*cond_p);
497 if (expr == NULL_TREE)
498 /* Empty condition in 'for'. */
499 gcc_assert (empty_expr_stmt_p (cond));
500 else if (check_for_bare_parameter_packs (expr))
501 expr = error_mark_node;
502 else if (!empty_expr_stmt_p (cond))
503 expr = build2 (COMPOUND_EXPR, TREE_TYPE (expr), cond, expr);
505 *cond_p = expr;
508 /* If *COND_P specifies a conditional with a declaration, transform the
509 loop such that
510 while (A x = 42) { }
511 for (; A x = 42;) { }
512 becomes
513 while (true) { A x = 42; if (!x) break; }
514 for (;;) { A x = 42; if (!x) break; }
515 The statement list for BODY will be empty if the conditional did
516 not declare anything. */
518 static void
519 simplify_loop_decl_cond (tree *cond_p, tree body)
521 tree cond, if_stmt;
523 if (!TREE_SIDE_EFFECTS (body))
524 return;
526 cond = *cond_p;
527 *cond_p = boolean_true_node;
529 if_stmt = begin_if_stmt ();
530 cond = cp_build_unary_op (TRUTH_NOT_EXPR, cond, 0, tf_warning_or_error);
531 finish_if_stmt_cond (cond, if_stmt);
532 finish_break_stmt ();
533 finish_then_clause (if_stmt);
534 finish_if_stmt (if_stmt);
537 /* Finish a goto-statement. */
539 tree
540 finish_goto_stmt (tree destination)
542 if (TREE_CODE (destination) == IDENTIFIER_NODE)
543 destination = lookup_label (destination);
545 /* We warn about unused labels with -Wunused. That means we have to
546 mark the used labels as used. */
547 if (TREE_CODE (destination) == LABEL_DECL)
548 TREE_USED (destination) = 1;
549 else
551 destination = mark_rvalue_use (destination);
552 if (!processing_template_decl)
554 destination = cp_convert (ptr_type_node, destination,
555 tf_warning_or_error);
556 if (error_operand_p (destination))
557 return NULL_TREE;
558 destination
559 = fold_build_cleanup_point_expr (TREE_TYPE (destination),
560 destination);
564 check_goto (destination);
566 return add_stmt (build_stmt (input_location, GOTO_EXPR, destination));
569 /* COND is the condition-expression for an if, while, etc.,
570 statement. Convert it to a boolean value, if appropriate.
571 In addition, verify sequence points if -Wsequence-point is enabled. */
573 static tree
574 maybe_convert_cond (tree cond)
576 /* Empty conditions remain empty. */
577 if (!cond)
578 return NULL_TREE;
580 /* Wait until we instantiate templates before doing conversion. */
581 if (processing_template_decl)
582 return cond;
584 if (warn_sequence_point)
585 verify_sequence_points (cond);
587 /* Do the conversion. */
588 cond = convert_from_reference (cond);
590 if (TREE_CODE (cond) == MODIFY_EXPR
591 && !TREE_NO_WARNING (cond)
592 && warn_parentheses)
594 warning (OPT_Wparentheses,
595 "suggest parentheses around assignment used as truth value");
596 TREE_NO_WARNING (cond) = 1;
599 return condition_conversion (cond);
602 /* Finish an expression-statement, whose EXPRESSION is as indicated. */
604 tree
605 finish_expr_stmt (tree expr)
607 tree r = NULL_TREE;
609 if (expr != NULL_TREE)
611 if (!processing_template_decl)
613 if (warn_sequence_point)
614 verify_sequence_points (expr);
615 expr = convert_to_void (expr, ICV_STATEMENT, tf_warning_or_error);
617 else if (!type_dependent_expression_p (expr))
618 convert_to_void (build_non_dependent_expr (expr), ICV_STATEMENT,
619 tf_warning_or_error);
621 if (check_for_bare_parameter_packs (expr))
622 expr = error_mark_node;
624 /* Simplification of inner statement expressions, compound exprs,
625 etc can result in us already having an EXPR_STMT. */
626 if (TREE_CODE (expr) != CLEANUP_POINT_EXPR)
628 if (TREE_CODE (expr) != EXPR_STMT)
629 expr = build_stmt (input_location, EXPR_STMT, expr);
630 expr = maybe_cleanup_point_expr_void (expr);
633 r = add_stmt (expr);
636 finish_stmt ();
638 return r;
642 /* Begin an if-statement. Returns a newly created IF_STMT if
643 appropriate. */
645 tree
646 begin_if_stmt (void)
648 tree r, scope;
649 scope = do_pushlevel (sk_cond);
650 r = build_stmt (input_location, IF_STMT, NULL_TREE,
651 NULL_TREE, NULL_TREE, scope);
652 begin_cond (&IF_COND (r));
653 return r;
656 /* Process the COND of an if-statement, which may be given by
657 IF_STMT. */
659 void
660 finish_if_stmt_cond (tree cond, tree if_stmt)
662 finish_cond (&IF_COND (if_stmt), maybe_convert_cond (cond));
663 add_stmt (if_stmt);
664 THEN_CLAUSE (if_stmt) = push_stmt_list ();
667 /* Finish the then-clause of an if-statement, which may be given by
668 IF_STMT. */
670 tree
671 finish_then_clause (tree if_stmt)
673 THEN_CLAUSE (if_stmt) = pop_stmt_list (THEN_CLAUSE (if_stmt));
674 return if_stmt;
677 /* Begin the else-clause of an if-statement. */
679 void
680 begin_else_clause (tree if_stmt)
682 ELSE_CLAUSE (if_stmt) = push_stmt_list ();
685 /* Finish the else-clause of an if-statement, which may be given by
686 IF_STMT. */
688 void
689 finish_else_clause (tree if_stmt)
691 ELSE_CLAUSE (if_stmt) = pop_stmt_list (ELSE_CLAUSE (if_stmt));
694 /* Finish an if-statement. */
696 void
697 finish_if_stmt (tree if_stmt)
699 tree scope = IF_SCOPE (if_stmt);
700 IF_SCOPE (if_stmt) = NULL;
701 add_stmt (do_poplevel (scope));
702 finish_stmt ();
705 /* Begin a while-statement. Returns a newly created WHILE_STMT if
706 appropriate. */
708 tree
709 begin_while_stmt (void)
711 tree r;
712 r = build_stmt (input_location, WHILE_STMT, NULL_TREE, NULL_TREE);
713 add_stmt (r);
714 WHILE_BODY (r) = do_pushlevel (sk_block);
715 begin_cond (&WHILE_COND (r));
716 return r;
719 /* Process the COND of a while-statement, which may be given by
720 WHILE_STMT. */
722 void
723 finish_while_stmt_cond (tree cond, tree while_stmt)
725 finish_cond (&WHILE_COND (while_stmt), maybe_convert_cond (cond));
726 simplify_loop_decl_cond (&WHILE_COND (while_stmt), WHILE_BODY (while_stmt));
729 /* Finish a while-statement, which may be given by WHILE_STMT. */
731 void
732 finish_while_stmt (tree while_stmt)
734 WHILE_BODY (while_stmt) = do_poplevel (WHILE_BODY (while_stmt));
735 finish_stmt ();
738 /* Begin a do-statement. Returns a newly created DO_STMT if
739 appropriate. */
741 tree
742 begin_do_stmt (void)
744 tree r = build_stmt (input_location, DO_STMT, NULL_TREE, NULL_TREE);
745 add_stmt (r);
746 DO_BODY (r) = push_stmt_list ();
747 return r;
750 /* Finish the body of a do-statement, which may be given by DO_STMT. */
752 void
753 finish_do_body (tree do_stmt)
755 tree body = DO_BODY (do_stmt) = pop_stmt_list (DO_BODY (do_stmt));
757 if (TREE_CODE (body) == STATEMENT_LIST && STATEMENT_LIST_TAIL (body))
758 body = STATEMENT_LIST_TAIL (body)->stmt;
760 if (IS_EMPTY_STMT (body))
761 warning (OPT_Wempty_body,
762 "suggest explicit braces around empty body in %<do%> statement");
765 /* Finish a do-statement, which may be given by DO_STMT, and whose
766 COND is as indicated. */
768 void
769 finish_do_stmt (tree cond, tree do_stmt)
771 cond = maybe_convert_cond (cond);
772 DO_COND (do_stmt) = cond;
773 finish_stmt ();
776 /* Finish a return-statement. The EXPRESSION returned, if any, is as
777 indicated. */
779 tree
780 finish_return_stmt (tree expr)
782 tree r;
783 bool no_warning;
785 expr = check_return_expr (expr, &no_warning);
787 if (flag_openmp && !check_omp_return ())
788 return error_mark_node;
789 if (!processing_template_decl)
791 if (warn_sequence_point)
792 verify_sequence_points (expr);
794 if (DECL_DESTRUCTOR_P (current_function_decl)
795 || (DECL_CONSTRUCTOR_P (current_function_decl)
796 && targetm.cxx.cdtor_returns_this ()))
798 /* Similarly, all destructors must run destructors for
799 base-classes before returning. So, all returns in a
800 destructor get sent to the DTOR_LABEL; finish_function emits
801 code to return a value there. */
802 return finish_goto_stmt (cdtor_label);
806 r = build_stmt (input_location, RETURN_EXPR, expr);
807 TREE_NO_WARNING (r) |= no_warning;
808 r = maybe_cleanup_point_expr_void (r);
809 r = add_stmt (r);
810 finish_stmt ();
812 return r;
815 /* Begin the scope of a for-statement or a range-for-statement.
816 Both the returned trees are to be used in a call to
817 begin_for_stmt or begin_range_for_stmt. */
819 tree
820 begin_for_scope (tree *init)
822 tree scope = NULL_TREE;
823 if (flag_new_for_scope > 0)
824 scope = do_pushlevel (sk_for);
826 if (processing_template_decl)
827 *init = push_stmt_list ();
828 else
829 *init = NULL_TREE;
831 return scope;
834 /* Begin a for-statement. Returns a new FOR_STMT.
835 SCOPE and INIT should be the return of begin_for_scope,
836 or both NULL_TREE */
838 tree
839 begin_for_stmt (tree scope, tree init)
841 tree r;
843 r = build_stmt (input_location, FOR_STMT, NULL_TREE, NULL_TREE,
844 NULL_TREE, NULL_TREE, NULL_TREE);
846 if (scope == NULL_TREE)
848 gcc_assert (!init || !(flag_new_for_scope > 0));
849 if (!init)
850 scope = begin_for_scope (&init);
852 FOR_INIT_STMT (r) = init;
853 FOR_SCOPE (r) = scope;
855 return r;
858 /* Finish the for-init-statement of a for-statement, which may be
859 given by FOR_STMT. */
861 void
862 finish_for_init_stmt (tree for_stmt)
864 if (processing_template_decl)
865 FOR_INIT_STMT (for_stmt) = pop_stmt_list (FOR_INIT_STMT (for_stmt));
866 add_stmt (for_stmt);
867 FOR_BODY (for_stmt) = do_pushlevel (sk_block);
868 begin_cond (&FOR_COND (for_stmt));
871 /* Finish the COND of a for-statement, which may be given by
872 FOR_STMT. */
874 void
875 finish_for_cond (tree cond, tree for_stmt)
877 finish_cond (&FOR_COND (for_stmt), maybe_convert_cond (cond));
878 simplify_loop_decl_cond (&FOR_COND (for_stmt), FOR_BODY (for_stmt));
881 /* Finish the increment-EXPRESSION in a for-statement, which may be
882 given by FOR_STMT. */
884 void
885 finish_for_expr (tree expr, tree for_stmt)
887 if (!expr)
888 return;
889 /* If EXPR is an overloaded function, issue an error; there is no
890 context available to use to perform overload resolution. */
891 if (type_unknown_p (expr))
893 cxx_incomplete_type_error (expr, TREE_TYPE (expr));
894 expr = error_mark_node;
896 if (!processing_template_decl)
898 if (warn_sequence_point)
899 verify_sequence_points (expr);
900 expr = convert_to_void (expr, ICV_THIRD_IN_FOR,
901 tf_warning_or_error);
903 else if (!type_dependent_expression_p (expr))
904 convert_to_void (build_non_dependent_expr (expr), ICV_THIRD_IN_FOR,
905 tf_warning_or_error);
906 expr = maybe_cleanup_point_expr_void (expr);
907 if (check_for_bare_parameter_packs (expr))
908 expr = error_mark_node;
909 FOR_EXPR (for_stmt) = expr;
912 /* Finish the body of a for-statement, which may be given by
913 FOR_STMT. The increment-EXPR for the loop must be
914 provided.
915 It can also finish RANGE_FOR_STMT. */
917 void
918 finish_for_stmt (tree for_stmt)
920 if (TREE_CODE (for_stmt) == RANGE_FOR_STMT)
921 RANGE_FOR_BODY (for_stmt) = do_poplevel (RANGE_FOR_BODY (for_stmt));
922 else
923 FOR_BODY (for_stmt) = do_poplevel (FOR_BODY (for_stmt));
925 /* Pop the scope for the body of the loop. */
926 if (flag_new_for_scope > 0)
928 tree scope;
929 tree *scope_ptr = (TREE_CODE (for_stmt) == RANGE_FOR_STMT
930 ? &RANGE_FOR_SCOPE (for_stmt)
931 : &FOR_SCOPE (for_stmt));
932 scope = *scope_ptr;
933 *scope_ptr = NULL;
934 add_stmt (do_poplevel (scope));
937 finish_stmt ();
940 /* Begin a range-for-statement. Returns a new RANGE_FOR_STMT.
941 SCOPE and INIT should be the return of begin_for_scope,
942 or both NULL_TREE .
943 To finish it call finish_for_stmt(). */
945 tree
946 begin_range_for_stmt (tree scope, tree init)
948 tree r;
950 r = build_stmt (input_location, RANGE_FOR_STMT,
951 NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE);
953 if (scope == NULL_TREE)
955 gcc_assert (!init || !(flag_new_for_scope > 0));
956 if (!init)
957 scope = begin_for_scope (&init);
960 /* RANGE_FOR_STMTs do not use nor save the init tree, so we
961 pop it now. */
962 if (init)
963 pop_stmt_list (init);
964 RANGE_FOR_SCOPE (r) = scope;
966 return r;
969 /* Finish the head of a range-based for statement, which may
970 be given by RANGE_FOR_STMT. DECL must be the declaration
971 and EXPR must be the loop expression. */
973 void
974 finish_range_for_decl (tree range_for_stmt, tree decl, tree expr)
976 RANGE_FOR_DECL (range_for_stmt) = decl;
977 RANGE_FOR_EXPR (range_for_stmt) = expr;
978 add_stmt (range_for_stmt);
979 RANGE_FOR_BODY (range_for_stmt) = do_pushlevel (sk_block);
982 /* Finish a break-statement. */
984 tree
985 finish_break_stmt (void)
987 /* In switch statements break is sometimes stylistically used after
988 a return statement. This can lead to spurious warnings about
989 control reaching the end of a non-void function when it is
990 inlined. Note that we are calling block_may_fallthru with
991 language specific tree nodes; this works because
992 block_may_fallthru returns true when given something it does not
993 understand. */
994 if (!block_may_fallthru (cur_stmt_list))
995 return void_zero_node;
996 return add_stmt (build_stmt (input_location, BREAK_STMT));
999 /* Finish a continue-statement. */
1001 tree
1002 finish_continue_stmt (void)
1004 return add_stmt (build_stmt (input_location, CONTINUE_STMT));
1007 /* Begin a switch-statement. Returns a new SWITCH_STMT if
1008 appropriate. */
1010 tree
1011 begin_switch_stmt (void)
1013 tree r, scope;
1015 scope = do_pushlevel (sk_cond);
1016 r = build_stmt (input_location, SWITCH_STMT, NULL_TREE, NULL_TREE, NULL_TREE, scope);
1018 begin_cond (&SWITCH_STMT_COND (r));
1020 return r;
1023 /* Finish the cond of a switch-statement. */
1025 void
1026 finish_switch_cond (tree cond, tree switch_stmt)
1028 tree orig_type = NULL;
1029 if (!processing_template_decl)
1031 /* Convert the condition to an integer or enumeration type. */
1032 cond = build_expr_type_conversion (WANT_INT | WANT_ENUM, cond, true);
1033 if (cond == NULL_TREE)
1035 error ("switch quantity not an integer");
1036 cond = error_mark_node;
1038 orig_type = TREE_TYPE (cond);
1039 if (cond != error_mark_node)
1041 /* [stmt.switch]
1043 Integral promotions are performed. */
1044 cond = perform_integral_promotions (cond);
1045 cond = maybe_cleanup_point_expr (cond);
1048 if (check_for_bare_parameter_packs (cond))
1049 cond = error_mark_node;
1050 else if (!processing_template_decl && warn_sequence_point)
1051 verify_sequence_points (cond);
1053 finish_cond (&SWITCH_STMT_COND (switch_stmt), cond);
1054 SWITCH_STMT_TYPE (switch_stmt) = orig_type;
1055 add_stmt (switch_stmt);
1056 push_switch (switch_stmt);
1057 SWITCH_STMT_BODY (switch_stmt) = push_stmt_list ();
1060 /* Finish the body of a switch-statement, which may be given by
1061 SWITCH_STMT. The COND to switch on is indicated. */
1063 void
1064 finish_switch_stmt (tree switch_stmt)
1066 tree scope;
1068 SWITCH_STMT_BODY (switch_stmt) =
1069 pop_stmt_list (SWITCH_STMT_BODY (switch_stmt));
1070 pop_switch ();
1071 finish_stmt ();
1073 scope = SWITCH_STMT_SCOPE (switch_stmt);
1074 SWITCH_STMT_SCOPE (switch_stmt) = NULL;
1075 add_stmt (do_poplevel (scope));
1078 /* Begin a try-block. Returns a newly-created TRY_BLOCK if
1079 appropriate. */
1081 tree
1082 begin_try_block (void)
1084 tree r = build_stmt (input_location, TRY_BLOCK, NULL_TREE, NULL_TREE);
1085 add_stmt (r);
1086 TRY_STMTS (r) = push_stmt_list ();
1087 return r;
1090 /* Likewise, for a function-try-block. The block returned in
1091 *COMPOUND_STMT is an artificial outer scope, containing the
1092 function-try-block. */
1094 tree
1095 begin_function_try_block (tree *compound_stmt)
1097 tree r;
1098 /* This outer scope does not exist in the C++ standard, but we need
1099 a place to put __FUNCTION__ and similar variables. */
1100 *compound_stmt = begin_compound_stmt (0);
1101 r = begin_try_block ();
1102 FN_TRY_BLOCK_P (r) = 1;
1103 return r;
1106 /* Finish a try-block, which may be given by TRY_BLOCK. */
1108 void
1109 finish_try_block (tree try_block)
1111 TRY_STMTS (try_block) = pop_stmt_list (TRY_STMTS (try_block));
1112 TRY_HANDLERS (try_block) = push_stmt_list ();
1115 /* Finish the body of a cleanup try-block, which may be given by
1116 TRY_BLOCK. */
1118 void
1119 finish_cleanup_try_block (tree try_block)
1121 TRY_STMTS (try_block) = pop_stmt_list (TRY_STMTS (try_block));
1124 /* Finish an implicitly generated try-block, with a cleanup is given
1125 by CLEANUP. */
1127 void
1128 finish_cleanup (tree cleanup, tree try_block)
1130 TRY_HANDLERS (try_block) = cleanup;
1131 CLEANUP_P (try_block) = 1;
1134 /* Likewise, for a function-try-block. */
1136 void
1137 finish_function_try_block (tree try_block)
1139 finish_try_block (try_block);
1140 /* FIXME : something queer about CTOR_INITIALIZER somehow following
1141 the try block, but moving it inside. */
1142 in_function_try_handler = 1;
1145 /* Finish a handler-sequence for a try-block, which may be given by
1146 TRY_BLOCK. */
1148 void
1149 finish_handler_sequence (tree try_block)
1151 TRY_HANDLERS (try_block) = pop_stmt_list (TRY_HANDLERS (try_block));
1152 check_handlers (TRY_HANDLERS (try_block));
1155 /* Finish the handler-seq for a function-try-block, given by
1156 TRY_BLOCK. COMPOUND_STMT is the outer block created by
1157 begin_function_try_block. */
1159 void
1160 finish_function_handler_sequence (tree try_block, tree compound_stmt)
1162 in_function_try_handler = 0;
1163 finish_handler_sequence (try_block);
1164 finish_compound_stmt (compound_stmt);
1167 /* Begin a handler. Returns a HANDLER if appropriate. */
1169 tree
1170 begin_handler (void)
1172 tree r;
1174 r = build_stmt (input_location, HANDLER, NULL_TREE, NULL_TREE);
1175 add_stmt (r);
1177 /* Create a binding level for the eh_info and the exception object
1178 cleanup. */
1179 HANDLER_BODY (r) = do_pushlevel (sk_catch);
1181 return r;
1184 /* Finish the handler-parameters for a handler, which may be given by
1185 HANDLER. DECL is the declaration for the catch parameter, or NULL
1186 if this is a `catch (...)' clause. */
1188 void
1189 finish_handler_parms (tree decl, tree handler)
1191 tree type = NULL_TREE;
1192 if (processing_template_decl)
1194 if (decl)
1196 decl = pushdecl (decl);
1197 decl = push_template_decl (decl);
1198 HANDLER_PARMS (handler) = decl;
1199 type = TREE_TYPE (decl);
1202 else
1203 type = expand_start_catch_block (decl);
1204 HANDLER_TYPE (handler) = type;
1205 if (!processing_template_decl && type)
1206 mark_used (eh_type_info (type));
1209 /* Finish a handler, which may be given by HANDLER. The BLOCKs are
1210 the return value from the matching call to finish_handler_parms. */
1212 void
1213 finish_handler (tree handler)
1215 if (!processing_template_decl)
1216 expand_end_catch_block ();
1217 HANDLER_BODY (handler) = do_poplevel (HANDLER_BODY (handler));
1220 /* Begin a compound statement. FLAGS contains some bits that control the
1221 behavior and context. If BCS_NO_SCOPE is set, the compound statement
1222 does not define a scope. If BCS_FN_BODY is set, this is the outermost
1223 block of a function. If BCS_TRY_BLOCK is set, this is the block
1224 created on behalf of a TRY statement. Returns a token to be passed to
1225 finish_compound_stmt. */
1227 tree
1228 begin_compound_stmt (unsigned int flags)
1230 tree r;
1232 if (flags & BCS_NO_SCOPE)
1234 r = push_stmt_list ();
1235 STATEMENT_LIST_NO_SCOPE (r) = 1;
1237 /* Normally, we try hard to keep the BLOCK for a statement-expression.
1238 But, if it's a statement-expression with a scopeless block, there's
1239 nothing to keep, and we don't want to accidentally keep a block
1240 *inside* the scopeless block. */
1241 keep_next_level (false);
1243 else
1244 r = do_pushlevel (flags & BCS_TRY_BLOCK ? sk_try : sk_block);
1246 /* When processing a template, we need to remember where the braces were,
1247 so that we can set up identical scopes when instantiating the template
1248 later. BIND_EXPR is a handy candidate for this.
1249 Note that do_poplevel won't create a BIND_EXPR itself here (and thus
1250 result in nested BIND_EXPRs), since we don't build BLOCK nodes when
1251 processing templates. */
1252 if (processing_template_decl)
1254 r = build3 (BIND_EXPR, NULL, NULL, r, NULL);
1255 BIND_EXPR_TRY_BLOCK (r) = (flags & BCS_TRY_BLOCK) != 0;
1256 BIND_EXPR_BODY_BLOCK (r) = (flags & BCS_FN_BODY) != 0;
1257 TREE_SIDE_EFFECTS (r) = 1;
1260 return r;
1263 /* Finish a compound-statement, which is given by STMT. */
1265 void
1266 finish_compound_stmt (tree stmt)
1268 if (TREE_CODE (stmt) == BIND_EXPR)
1270 tree body = do_poplevel (BIND_EXPR_BODY (stmt));
1271 /* If the STATEMENT_LIST is empty and this BIND_EXPR isn't special,
1272 discard the BIND_EXPR so it can be merged with the containing
1273 STATEMENT_LIST. */
1274 if (TREE_CODE (body) == STATEMENT_LIST
1275 && STATEMENT_LIST_HEAD (body) == NULL
1276 && !BIND_EXPR_BODY_BLOCK (stmt)
1277 && !BIND_EXPR_TRY_BLOCK (stmt))
1278 stmt = body;
1279 else
1280 BIND_EXPR_BODY (stmt) = body;
1282 else if (STATEMENT_LIST_NO_SCOPE (stmt))
1283 stmt = pop_stmt_list (stmt);
1284 else
1286 /* Destroy any ObjC "super" receivers that may have been
1287 created. */
1288 objc_clear_super_receiver ();
1290 stmt = do_poplevel (stmt);
1293 /* ??? See c_end_compound_stmt wrt statement expressions. */
1294 add_stmt (stmt);
1295 finish_stmt ();
1298 /* Finish an asm-statement, whose components are a STRING, some
1299 OUTPUT_OPERANDS, some INPUT_OPERANDS, some CLOBBERS and some
1300 LABELS. Also note whether the asm-statement should be
1301 considered volatile. */
1303 tree
1304 finish_asm_stmt (int volatile_p, tree string, tree output_operands,
1305 tree input_operands, tree clobbers, tree labels)
1307 tree r;
1308 tree t;
1309 int ninputs = list_length (input_operands);
1310 int noutputs = list_length (output_operands);
1312 if (!processing_template_decl)
1314 const char *constraint;
1315 const char **oconstraints;
1316 bool allows_mem, allows_reg, is_inout;
1317 tree operand;
1318 int i;
1320 oconstraints = XALLOCAVEC (const char *, noutputs);
1322 string = resolve_asm_operand_names (string, output_operands,
1323 input_operands, labels);
1325 for (i = 0, t = output_operands; t; t = TREE_CHAIN (t), ++i)
1327 operand = TREE_VALUE (t);
1329 /* ??? Really, this should not be here. Users should be using a
1330 proper lvalue, dammit. But there's a long history of using
1331 casts in the output operands. In cases like longlong.h, this
1332 becomes a primitive form of typechecking -- if the cast can be
1333 removed, then the output operand had a type of the proper width;
1334 otherwise we'll get an error. Gross, but ... */
1335 STRIP_NOPS (operand);
1337 operand = mark_lvalue_use (operand);
1339 if (!lvalue_or_else (operand, lv_asm, tf_warning_or_error))
1340 operand = error_mark_node;
1342 if (operand != error_mark_node
1343 && (TREE_READONLY (operand)
1344 || CP_TYPE_CONST_P (TREE_TYPE (operand))
1345 /* Functions are not modifiable, even though they are
1346 lvalues. */
1347 || TREE_CODE (TREE_TYPE (operand)) == FUNCTION_TYPE
1348 || TREE_CODE (TREE_TYPE (operand)) == METHOD_TYPE
1349 /* If it's an aggregate and any field is const, then it is
1350 effectively const. */
1351 || (CLASS_TYPE_P (TREE_TYPE (operand))
1352 && C_TYPE_FIELDS_READONLY (TREE_TYPE (operand)))))
1353 cxx_readonly_error (operand, lv_asm);
1355 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
1356 oconstraints[i] = constraint;
1358 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
1359 &allows_mem, &allows_reg, &is_inout))
1361 /* If the operand is going to end up in memory,
1362 mark it addressable. */
1363 if (!allows_reg && !cxx_mark_addressable (operand))
1364 operand = error_mark_node;
1366 else
1367 operand = error_mark_node;
1369 TREE_VALUE (t) = operand;
1372 for (i = 0, t = input_operands; t; ++i, t = TREE_CHAIN (t))
1374 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
1375 operand = decay_conversion (TREE_VALUE (t), tf_warning_or_error);
1377 /* If the type of the operand hasn't been determined (e.g.,
1378 because it involves an overloaded function), then issue
1379 an error message. There's no context available to
1380 resolve the overloading. */
1381 if (TREE_TYPE (operand) == unknown_type_node)
1383 error ("type of asm operand %qE could not be determined",
1384 TREE_VALUE (t));
1385 operand = error_mark_node;
1388 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
1389 oconstraints, &allows_mem, &allows_reg))
1391 /* If the operand is going to end up in memory,
1392 mark it addressable. */
1393 if (!allows_reg && allows_mem)
1395 /* Strip the nops as we allow this case. FIXME, this really
1396 should be rejected or made deprecated. */
1397 STRIP_NOPS (operand);
1398 if (!cxx_mark_addressable (operand))
1399 operand = error_mark_node;
1402 else
1403 operand = error_mark_node;
1405 TREE_VALUE (t) = operand;
1409 r = build_stmt (input_location, ASM_EXPR, string,
1410 output_operands, input_operands,
1411 clobbers, labels);
1412 ASM_VOLATILE_P (r) = volatile_p || noutputs == 0;
1413 r = maybe_cleanup_point_expr_void (r);
1414 return add_stmt (r);
1417 /* Finish a label with the indicated NAME. Returns the new label. */
1419 tree
1420 finish_label_stmt (tree name)
1422 tree decl = define_label (input_location, name);
1424 if (decl == error_mark_node)
1425 return error_mark_node;
1427 add_stmt (build_stmt (input_location, LABEL_EXPR, decl));
1429 return decl;
1432 /* Finish a series of declarations for local labels. G++ allows users
1433 to declare "local" labels, i.e., labels with scope. This extension
1434 is useful when writing code involving statement-expressions. */
1436 void
1437 finish_label_decl (tree name)
1439 if (!at_function_scope_p ())
1441 error ("__label__ declarations are only allowed in function scopes");
1442 return;
1445 add_decl_expr (declare_local_label (name));
1448 /* When DECL goes out of scope, make sure that CLEANUP is executed. */
1450 void
1451 finish_decl_cleanup (tree decl, tree cleanup)
1453 push_cleanup (decl, cleanup, false);
1456 /* If the current scope exits with an exception, run CLEANUP. */
1458 void
1459 finish_eh_cleanup (tree cleanup)
1461 push_cleanup (NULL, cleanup, true);
1464 /* The MEM_INITS is a list of mem-initializers, in reverse of the
1465 order they were written by the user. Each node is as for
1466 emit_mem_initializers. */
1468 void
1469 finish_mem_initializers (tree mem_inits)
1471 /* Reorder the MEM_INITS so that they are in the order they appeared
1472 in the source program. */
1473 mem_inits = nreverse (mem_inits);
1475 if (processing_template_decl)
1477 tree mem;
1479 for (mem = mem_inits; mem; mem = TREE_CHAIN (mem))
1481 /* If the TREE_PURPOSE is a TYPE_PACK_EXPANSION, skip the
1482 check for bare parameter packs in the TREE_VALUE, because
1483 any parameter packs in the TREE_VALUE have already been
1484 bound as part of the TREE_PURPOSE. See
1485 make_pack_expansion for more information. */
1486 if (TREE_CODE (TREE_PURPOSE (mem)) != TYPE_PACK_EXPANSION
1487 && check_for_bare_parameter_packs (TREE_VALUE (mem)))
1488 TREE_VALUE (mem) = error_mark_node;
1491 add_stmt (build_min_nt_loc (UNKNOWN_LOCATION,
1492 CTOR_INITIALIZER, mem_inits));
1494 else
1495 emit_mem_initializers (mem_inits);
1498 /* Finish a parenthesized expression EXPR. */
1500 tree
1501 finish_parenthesized_expr (tree expr)
1503 if (EXPR_P (expr))
1504 /* This inhibits warnings in c_common_truthvalue_conversion. */
1505 TREE_NO_WARNING (expr) = 1;
1507 if (TREE_CODE (expr) == OFFSET_REF
1508 || TREE_CODE (expr) == SCOPE_REF)
1509 /* [expr.unary.op]/3 The qualified id of a pointer-to-member must not be
1510 enclosed in parentheses. */
1511 PTRMEM_OK_P (expr) = 0;
1513 if (TREE_CODE (expr) == STRING_CST)
1514 PAREN_STRING_LITERAL_P (expr) = 1;
1516 return expr;
1519 /* Finish a reference to a non-static data member (DECL) that is not
1520 preceded by `.' or `->'. */
1522 tree
1523 finish_non_static_data_member (tree decl, tree object, tree qualifying_scope)
1525 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
1527 if (!object)
1529 tree scope = qualifying_scope;
1530 if (scope == NULL_TREE)
1531 scope = context_for_name_lookup (decl);
1532 object = maybe_dummy_object (scope, NULL);
1535 if (object == error_mark_node)
1536 return error_mark_node;
1538 /* DR 613: Can use non-static data members without an associated
1539 object in sizeof/decltype/alignof. */
1540 if (is_dummy_object (object) && cp_unevaluated_operand == 0
1541 && (!processing_template_decl || !current_class_ref))
1543 if (current_function_decl
1544 && DECL_STATIC_FUNCTION_P (current_function_decl))
1545 error ("invalid use of member %q+D in static member function", decl);
1546 else
1547 error ("invalid use of non-static data member %q+D", decl);
1548 error ("from this location");
1550 return error_mark_node;
1553 if (current_class_ptr)
1554 TREE_USED (current_class_ptr) = 1;
1555 if (processing_template_decl && !qualifying_scope)
1557 tree type = TREE_TYPE (decl);
1559 if (TREE_CODE (type) == REFERENCE_TYPE)
1560 /* Quals on the object don't matter. */;
1561 else
1563 /* Set the cv qualifiers. */
1564 int quals = (current_class_ref
1565 ? cp_type_quals (TREE_TYPE (current_class_ref))
1566 : TYPE_UNQUALIFIED);
1568 if (DECL_MUTABLE_P (decl))
1569 quals &= ~TYPE_QUAL_CONST;
1571 quals |= cp_type_quals (TREE_TYPE (decl));
1572 type = cp_build_qualified_type (type, quals);
1575 return (convert_from_reference
1576 (build_min (COMPONENT_REF, type, object, decl, NULL_TREE)));
1578 /* If PROCESSING_TEMPLATE_DECL is nonzero here, then
1579 QUALIFYING_SCOPE is also non-null. Wrap this in a SCOPE_REF
1580 for now. */
1581 else if (processing_template_decl)
1582 return build_qualified_name (TREE_TYPE (decl),
1583 qualifying_scope,
1584 decl,
1585 /*template_p=*/false);
1586 else
1588 tree access_type = TREE_TYPE (object);
1590 perform_or_defer_access_check (TYPE_BINFO (access_type), decl,
1591 decl, tf_warning_or_error);
1593 /* If the data member was named `C::M', convert `*this' to `C'
1594 first. */
1595 if (qualifying_scope)
1597 tree binfo = NULL_TREE;
1598 object = build_scoped_ref (object, qualifying_scope,
1599 &binfo);
1602 return build_class_member_access_expr (object, decl,
1603 /*access_path=*/NULL_TREE,
1604 /*preserve_reference=*/false,
1605 tf_warning_or_error);
1609 /* If we are currently parsing a template and we encountered a typedef
1610 TYPEDEF_DECL that is being accessed though CONTEXT, this function
1611 adds the typedef to a list tied to the current template.
1612 At template instantiation time, that list is walked and access check
1613 performed for each typedef.
1614 LOCATION is the location of the usage point of TYPEDEF_DECL. */
1616 void
1617 add_typedef_to_current_template_for_access_check (tree typedef_decl,
1618 tree context,
1619 location_t location)
1621 tree template_info = NULL;
1622 tree cs = current_scope ();
1624 if (!is_typedef_decl (typedef_decl)
1625 || !context
1626 || !CLASS_TYPE_P (context)
1627 || !cs)
1628 return;
1630 if (CLASS_TYPE_P (cs) || TREE_CODE (cs) == FUNCTION_DECL)
1631 template_info = get_template_info (cs);
1633 if (template_info
1634 && TI_TEMPLATE (template_info)
1635 && !currently_open_class (context))
1636 append_type_to_template_for_access_check (cs, typedef_decl,
1637 context, location);
1640 /* DECL was the declaration to which a qualified-id resolved. Issue
1641 an error message if it is not accessible. If OBJECT_TYPE is
1642 non-NULL, we have just seen `x->' or `x.' and OBJECT_TYPE is the
1643 type of `*x', or `x', respectively. If the DECL was named as
1644 `A::B' then NESTED_NAME_SPECIFIER is `A'. */
1646 void
1647 check_accessibility_of_qualified_id (tree decl,
1648 tree object_type,
1649 tree nested_name_specifier)
1651 tree scope;
1652 tree qualifying_type = NULL_TREE;
1654 /* If we are parsing a template declaration and if decl is a typedef,
1655 add it to a list tied to the template.
1656 At template instantiation time, that list will be walked and
1657 access check performed. */
1658 add_typedef_to_current_template_for_access_check (decl,
1659 nested_name_specifier
1660 ? nested_name_specifier
1661 : DECL_CONTEXT (decl),
1662 input_location);
1664 /* If we're not checking, return immediately. */
1665 if (deferred_access_no_check)
1666 return;
1668 /* Determine the SCOPE of DECL. */
1669 scope = context_for_name_lookup (decl);
1670 /* If the SCOPE is not a type, then DECL is not a member. */
1671 if (!TYPE_P (scope))
1672 return;
1673 /* Compute the scope through which DECL is being accessed. */
1674 if (object_type
1675 /* OBJECT_TYPE might not be a class type; consider:
1677 class A { typedef int I; };
1678 I *p;
1679 p->A::I::~I();
1681 In this case, we will have "A::I" as the DECL, but "I" as the
1682 OBJECT_TYPE. */
1683 && CLASS_TYPE_P (object_type)
1684 && DERIVED_FROM_P (scope, object_type))
1685 /* If we are processing a `->' or `.' expression, use the type of the
1686 left-hand side. */
1687 qualifying_type = object_type;
1688 else if (nested_name_specifier)
1690 /* If the reference is to a non-static member of the
1691 current class, treat it as if it were referenced through
1692 `this'. */
1693 if (DECL_NONSTATIC_MEMBER_P (decl)
1694 && current_class_ptr
1695 && DERIVED_FROM_P (scope, current_class_type))
1696 qualifying_type = current_class_type;
1697 /* Otherwise, use the type indicated by the
1698 nested-name-specifier. */
1699 else
1700 qualifying_type = nested_name_specifier;
1702 else
1703 /* Otherwise, the name must be from the current class or one of
1704 its bases. */
1705 qualifying_type = currently_open_derived_class (scope);
1707 if (qualifying_type
1708 /* It is possible for qualifying type to be a TEMPLATE_TYPE_PARM
1709 or similar in a default argument value. */
1710 && CLASS_TYPE_P (qualifying_type)
1711 && !dependent_type_p (qualifying_type))
1712 perform_or_defer_access_check (TYPE_BINFO (qualifying_type), decl,
1713 decl, tf_warning_or_error);
1716 /* EXPR is the result of a qualified-id. The QUALIFYING_CLASS was the
1717 class named to the left of the "::" operator. DONE is true if this
1718 expression is a complete postfix-expression; it is false if this
1719 expression is followed by '->', '[', '(', etc. ADDRESS_P is true
1720 iff this expression is the operand of '&'. TEMPLATE_P is true iff
1721 the qualified-id was of the form "A::template B". TEMPLATE_ARG_P
1722 is true iff this qualified name appears as a template argument. */
1724 tree
1725 finish_qualified_id_expr (tree qualifying_class,
1726 tree expr,
1727 bool done,
1728 bool address_p,
1729 bool template_p,
1730 bool template_arg_p)
1732 gcc_assert (TYPE_P (qualifying_class));
1734 if (error_operand_p (expr))
1735 return error_mark_node;
1737 if (DECL_P (expr) || BASELINK_P (expr))
1738 mark_used (expr);
1740 if (template_p)
1741 check_template_keyword (expr);
1743 /* If EXPR occurs as the operand of '&', use special handling that
1744 permits a pointer-to-member. */
1745 if (address_p && done)
1747 if (TREE_CODE (expr) == SCOPE_REF)
1748 expr = TREE_OPERAND (expr, 1);
1749 expr = build_offset_ref (qualifying_class, expr,
1750 /*address_p=*/true);
1751 return expr;
1754 /* Within the scope of a class, turn references to non-static
1755 members into expression of the form "this->...". */
1756 if (template_arg_p)
1757 /* But, within a template argument, we do not want make the
1758 transformation, as there is no "this" pointer. */
1760 else if (TREE_CODE (expr) == FIELD_DECL)
1762 push_deferring_access_checks (dk_no_check);
1763 expr = finish_non_static_data_member (expr, NULL_TREE,
1764 qualifying_class);
1765 pop_deferring_access_checks ();
1767 else if (BASELINK_P (expr) && !processing_template_decl)
1769 tree ob;
1771 /* See if any of the functions are non-static members. */
1772 /* If so, the expression may be relative to 'this'. */
1773 if (!shared_member_p (expr)
1774 && (ob = maybe_dummy_object (qualifying_class, NULL),
1775 !is_dummy_object (ob)))
1776 expr = (build_class_member_access_expr
1777 (ob,
1778 expr,
1779 BASELINK_ACCESS_BINFO (expr),
1780 /*preserve_reference=*/false,
1781 tf_warning_or_error));
1782 else if (done)
1783 /* The expression is a qualified name whose address is not
1784 being taken. */
1785 expr = build_offset_ref (qualifying_class, expr, /*address_p=*/false);
1787 else if (BASELINK_P (expr))
1789 else
1791 expr = convert_from_reference (expr);
1793 /* In a template, return a SCOPE_REF for most qualified-ids
1794 so that we can check access at instantiation time. But if
1795 we're looking at a member of the current instantiation, we
1796 know we have access and building up the SCOPE_REF confuses
1797 non-type template argument handling. */
1798 if (processing_template_decl
1799 && !currently_open_class (qualifying_class))
1800 expr = build_qualified_name (TREE_TYPE (expr),
1801 qualifying_class, expr,
1802 template_p);
1805 return expr;
1808 /* Begin a statement-expression. The value returned must be passed to
1809 finish_stmt_expr. */
1811 tree
1812 begin_stmt_expr (void)
1814 return push_stmt_list ();
1817 /* Process the final expression of a statement expression. EXPR can be
1818 NULL, if the final expression is empty. Return a STATEMENT_LIST
1819 containing all the statements in the statement-expression, or
1820 ERROR_MARK_NODE if there was an error. */
1822 tree
1823 finish_stmt_expr_expr (tree expr, tree stmt_expr)
1825 if (error_operand_p (expr))
1827 /* The type of the statement-expression is the type of the last
1828 expression. */
1829 TREE_TYPE (stmt_expr) = error_mark_node;
1830 return error_mark_node;
1833 /* If the last statement does not have "void" type, then the value
1834 of the last statement is the value of the entire expression. */
1835 if (expr)
1837 tree type = TREE_TYPE (expr);
1839 if (processing_template_decl)
1841 expr = build_stmt (input_location, EXPR_STMT, expr);
1842 expr = add_stmt (expr);
1843 /* Mark the last statement so that we can recognize it as such at
1844 template-instantiation time. */
1845 EXPR_STMT_STMT_EXPR_RESULT (expr) = 1;
1847 else if (VOID_TYPE_P (type))
1849 /* Just treat this like an ordinary statement. */
1850 expr = finish_expr_stmt (expr);
1852 else
1854 /* It actually has a value we need to deal with. First, force it
1855 to be an rvalue so that we won't need to build up a copy
1856 constructor call later when we try to assign it to something. */
1857 expr = force_rvalue (expr, tf_warning_or_error);
1858 if (error_operand_p (expr))
1859 return error_mark_node;
1861 /* Update for array-to-pointer decay. */
1862 type = TREE_TYPE (expr);
1864 /* Wrap it in a CLEANUP_POINT_EXPR and add it to the list like a
1865 normal statement, but don't convert to void or actually add
1866 the EXPR_STMT. */
1867 if (TREE_CODE (expr) != CLEANUP_POINT_EXPR)
1868 expr = maybe_cleanup_point_expr (expr);
1869 add_stmt (expr);
1872 /* The type of the statement-expression is the type of the last
1873 expression. */
1874 TREE_TYPE (stmt_expr) = type;
1877 return stmt_expr;
1880 /* Finish a statement-expression. EXPR should be the value returned
1881 by the previous begin_stmt_expr. Returns an expression
1882 representing the statement-expression. */
1884 tree
1885 finish_stmt_expr (tree stmt_expr, bool has_no_scope)
1887 tree type;
1888 tree result;
1890 if (error_operand_p (stmt_expr))
1892 pop_stmt_list (stmt_expr);
1893 return error_mark_node;
1896 gcc_assert (TREE_CODE (stmt_expr) == STATEMENT_LIST);
1898 type = TREE_TYPE (stmt_expr);
1899 result = pop_stmt_list (stmt_expr);
1900 TREE_TYPE (result) = type;
1902 if (processing_template_decl)
1904 result = build_min (STMT_EXPR, type, result);
1905 TREE_SIDE_EFFECTS (result) = 1;
1906 STMT_EXPR_NO_SCOPE (result) = has_no_scope;
1908 else if (CLASS_TYPE_P (type))
1910 /* Wrap the statement-expression in a TARGET_EXPR so that the
1911 temporary object created by the final expression is destroyed at
1912 the end of the full-expression containing the
1913 statement-expression. */
1914 result = force_target_expr (type, result, tf_warning_or_error);
1917 return result;
1920 /* Returns the expression which provides the value of STMT_EXPR. */
1922 tree
1923 stmt_expr_value_expr (tree stmt_expr)
1925 tree t = STMT_EXPR_STMT (stmt_expr);
1927 if (TREE_CODE (t) == BIND_EXPR)
1928 t = BIND_EXPR_BODY (t);
1930 if (TREE_CODE (t) == STATEMENT_LIST && STATEMENT_LIST_TAIL (t))
1931 t = STATEMENT_LIST_TAIL (t)->stmt;
1933 if (TREE_CODE (t) == EXPR_STMT)
1934 t = EXPR_STMT_EXPR (t);
1936 return t;
1939 /* Return TRUE iff EXPR_STMT is an empty list of
1940 expression statements. */
1942 bool
1943 empty_expr_stmt_p (tree expr_stmt)
1945 tree body = NULL_TREE;
1947 if (expr_stmt == void_zero_node)
1948 return true;
1950 if (expr_stmt)
1952 if (TREE_CODE (expr_stmt) == EXPR_STMT)
1953 body = EXPR_STMT_EXPR (expr_stmt);
1954 else if (TREE_CODE (expr_stmt) == STATEMENT_LIST)
1955 body = expr_stmt;
1958 if (body)
1960 if (TREE_CODE (body) == STATEMENT_LIST)
1961 return tsi_end_p (tsi_start (body));
1962 else
1963 return empty_expr_stmt_p (body);
1965 return false;
1968 /* Perform Koenig lookup. FN is the postfix-expression representing
1969 the function (or functions) to call; ARGS are the arguments to the
1970 call; if INCLUDE_STD then the `std' namespace is automatically
1971 considered an associated namespace (used in range-based for loops).
1972 Returns the functions to be considered by overload resolution. */
1974 tree
1975 perform_koenig_lookup (tree fn, VEC(tree,gc) *args, bool include_std,
1976 tsubst_flags_t complain)
1978 tree identifier = NULL_TREE;
1979 tree functions = NULL_TREE;
1980 tree tmpl_args = NULL_TREE;
1981 bool template_id = false;
1983 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
1985 /* Use a separate flag to handle null args. */
1986 template_id = true;
1987 tmpl_args = TREE_OPERAND (fn, 1);
1988 fn = TREE_OPERAND (fn, 0);
1991 /* Find the name of the overloaded function. */
1992 if (TREE_CODE (fn) == IDENTIFIER_NODE)
1993 identifier = fn;
1994 else if (is_overloaded_fn (fn))
1996 functions = fn;
1997 identifier = DECL_NAME (get_first_fn (functions));
1999 else if (DECL_P (fn))
2001 functions = fn;
2002 identifier = DECL_NAME (fn);
2005 /* A call to a namespace-scope function using an unqualified name.
2007 Do Koenig lookup -- unless any of the arguments are
2008 type-dependent. */
2009 if (!any_type_dependent_arguments_p (args)
2010 && !any_dependent_template_arguments_p (tmpl_args))
2012 fn = lookup_arg_dependent (identifier, functions, args, include_std);
2013 if (!fn)
2015 /* The unqualified name could not be resolved. */
2016 if (complain)
2017 fn = unqualified_fn_lookup_error (identifier);
2018 else
2019 fn = identifier;
2023 if (fn && template_id)
2024 fn = build2 (TEMPLATE_ID_EXPR, unknown_type_node, fn, tmpl_args);
2026 return fn;
2029 /* Generate an expression for `FN (ARGS)'. This may change the
2030 contents of ARGS.
2032 If DISALLOW_VIRTUAL is true, the call to FN will be not generated
2033 as a virtual call, even if FN is virtual. (This flag is set when
2034 encountering an expression where the function name is explicitly
2035 qualified. For example a call to `X::f' never generates a virtual
2036 call.)
2038 Returns code for the call. */
2040 tree
2041 finish_call_expr (tree fn, VEC(tree,gc) **args, bool disallow_virtual,
2042 bool koenig_p, tsubst_flags_t complain)
2044 tree result;
2045 tree orig_fn;
2046 VEC(tree,gc) *orig_args = NULL;
2048 if (fn == error_mark_node)
2049 return error_mark_node;
2051 gcc_assert (!TYPE_P (fn));
2053 orig_fn = fn;
2055 if (processing_template_decl)
2057 /* If the call expression is dependent, build a CALL_EXPR node
2058 with no type; type_dependent_expression_p recognizes
2059 expressions with no type as being dependent. */
2060 if (type_dependent_expression_p (fn)
2061 || any_type_dependent_arguments_p (*args)
2062 /* For a non-static member function that doesn't have an
2063 explicit object argument, we need to specifically
2064 test the type dependency of the "this" pointer because it
2065 is not included in *ARGS even though it is considered to
2066 be part of the list of arguments. Note that this is
2067 related to CWG issues 515 and 1005. */
2068 || (TREE_CODE (fn) != COMPONENT_REF
2069 && non_static_member_function_p (fn)
2070 && current_class_ref
2071 && type_dependent_expression_p (current_class_ref)))
2073 result = build_nt_call_vec (fn, *args);
2074 SET_EXPR_LOCATION (result, EXPR_LOC_OR_HERE (fn));
2075 KOENIG_LOOKUP_P (result) = koenig_p;
2076 if (cfun)
2080 tree fndecl = OVL_CURRENT (fn);
2081 if (TREE_CODE (fndecl) != FUNCTION_DECL
2082 || !TREE_THIS_VOLATILE (fndecl))
2083 break;
2084 fn = OVL_NEXT (fn);
2086 while (fn);
2087 if (!fn)
2088 current_function_returns_abnormally = 1;
2090 return result;
2092 orig_args = make_tree_vector_copy (*args);
2093 if (!BASELINK_P (fn)
2094 && TREE_CODE (fn) != PSEUDO_DTOR_EXPR
2095 && TREE_TYPE (fn) != unknown_type_node)
2096 fn = build_non_dependent_expr (fn);
2097 make_args_non_dependent (*args);
2100 if (TREE_CODE (fn) == COMPONENT_REF)
2102 tree member = TREE_OPERAND (fn, 1);
2103 if (BASELINK_P (member))
2105 tree object = TREE_OPERAND (fn, 0);
2106 return build_new_method_call (object, member,
2107 args, NULL_TREE,
2108 (disallow_virtual
2109 ? LOOKUP_NORMAL | LOOKUP_NONVIRTUAL
2110 : LOOKUP_NORMAL),
2111 /*fn_p=*/NULL,
2112 complain);
2116 if (is_overloaded_fn (fn))
2117 fn = baselink_for_fns (fn);
2119 result = NULL_TREE;
2120 if (BASELINK_P (fn))
2122 tree object;
2124 /* A call to a member function. From [over.call.func]:
2126 If the keyword this is in scope and refers to the class of
2127 that member function, or a derived class thereof, then the
2128 function call is transformed into a qualified function call
2129 using (*this) as the postfix-expression to the left of the
2130 . operator.... [Otherwise] a contrived object of type T
2131 becomes the implied object argument.
2133 In this situation:
2135 struct A { void f(); };
2136 struct B : public A {};
2137 struct C : public A { void g() { B::f(); }};
2139 "the class of that member function" refers to `A'. But 11.2
2140 [class.access.base] says that we need to convert 'this' to B* as
2141 part of the access, so we pass 'B' to maybe_dummy_object. */
2143 object = maybe_dummy_object (BINFO_TYPE (BASELINK_ACCESS_BINFO (fn)),
2144 NULL);
2146 if (processing_template_decl)
2148 if (type_dependent_expression_p (object))
2150 tree ret = build_nt_call_vec (orig_fn, orig_args);
2151 release_tree_vector (orig_args);
2152 return ret;
2154 object = build_non_dependent_expr (object);
2157 result = build_new_method_call (object, fn, args, NULL_TREE,
2158 (disallow_virtual
2159 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
2160 : LOOKUP_NORMAL),
2161 /*fn_p=*/NULL,
2162 complain);
2164 else if (is_overloaded_fn (fn))
2166 /* If the function is an overloaded builtin, resolve it. */
2167 if (TREE_CODE (fn) == FUNCTION_DECL
2168 && (DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL
2169 || DECL_BUILT_IN_CLASS (fn) == BUILT_IN_MD))
2170 result = resolve_overloaded_builtin (input_location, fn, *args);
2172 if (!result)
2173 /* A call to a namespace-scope function. */
2174 result = build_new_function_call (fn, args, koenig_p, complain);
2176 else if (TREE_CODE (fn) == PSEUDO_DTOR_EXPR)
2178 if (!VEC_empty (tree, *args))
2179 error ("arguments to destructor are not allowed");
2180 /* Mark the pseudo-destructor call as having side-effects so
2181 that we do not issue warnings about its use. */
2182 result = build1 (NOP_EXPR,
2183 void_type_node,
2184 TREE_OPERAND (fn, 0));
2185 TREE_SIDE_EFFECTS (result) = 1;
2187 else if (CLASS_TYPE_P (TREE_TYPE (fn)))
2188 /* If the "function" is really an object of class type, it might
2189 have an overloaded `operator ()'. */
2190 result = build_op_call (fn, args, complain);
2192 if (!result)
2193 /* A call where the function is unknown. */
2194 result = cp_build_function_call_vec (fn, args, complain);
2196 if (processing_template_decl && result != error_mark_node)
2198 if (TREE_CODE (result) == INDIRECT_REF)
2199 result = TREE_OPERAND (result, 0);
2200 result = build_call_vec (TREE_TYPE (result), orig_fn, orig_args);
2201 SET_EXPR_LOCATION (result, input_location);
2202 KOENIG_LOOKUP_P (result) = koenig_p;
2203 release_tree_vector (orig_args);
2204 result = convert_from_reference (result);
2207 if (koenig_p)
2209 /* Free garbage OVERLOADs from arg-dependent lookup. */
2210 tree next = NULL_TREE;
2211 for (fn = orig_fn;
2212 fn && TREE_CODE (fn) == OVERLOAD && OVL_ARG_DEPENDENT (fn);
2213 fn = next)
2215 if (processing_template_decl)
2216 /* In a template, we'll re-use them at instantiation time. */
2217 OVL_ARG_DEPENDENT (fn) = false;
2218 else
2220 next = OVL_CHAIN (fn);
2221 ggc_free (fn);
2226 return result;
2229 /* Finish a call to a postfix increment or decrement or EXPR. (Which
2230 is indicated by CODE, which should be POSTINCREMENT_EXPR or
2231 POSTDECREMENT_EXPR.) */
2233 tree
2234 finish_increment_expr (tree expr, enum tree_code code)
2236 return build_x_unary_op (input_location, code, expr, tf_warning_or_error);
2239 /* Finish a use of `this'. Returns an expression for `this'. */
2241 tree
2242 finish_this_expr (void)
2244 tree result;
2246 if (current_class_ptr)
2248 tree type = TREE_TYPE (current_class_ref);
2250 /* In a lambda expression, 'this' refers to the captured 'this'. */
2251 if (LAMBDA_TYPE_P (type))
2252 result = lambda_expr_this_capture (CLASSTYPE_LAMBDA_EXPR (type));
2253 else
2254 result = current_class_ptr;
2257 else if (current_function_decl
2258 && DECL_STATIC_FUNCTION_P (current_function_decl))
2260 error ("%<this%> is unavailable for static member functions");
2261 result = error_mark_node;
2263 else
2265 if (current_function_decl)
2266 error ("invalid use of %<this%> in non-member function");
2267 else
2268 error ("invalid use of %<this%> at top level");
2269 result = error_mark_node;
2272 return result;
2275 /* Finish a pseudo-destructor expression. If SCOPE is NULL, the
2276 expression was of the form `OBJECT.~DESTRUCTOR' where DESTRUCTOR is
2277 the TYPE for the type given. If SCOPE is non-NULL, the expression
2278 was of the form `OBJECT.SCOPE::~DESTRUCTOR'. */
2280 tree
2281 finish_pseudo_destructor_expr (tree object, tree scope, tree destructor)
2283 if (object == error_mark_node || destructor == error_mark_node)
2284 return error_mark_node;
2286 gcc_assert (TYPE_P (destructor));
2288 if (!processing_template_decl)
2290 if (scope == error_mark_node)
2292 error ("invalid qualifying scope in pseudo-destructor name");
2293 return error_mark_node;
2295 if (scope && TYPE_P (scope) && !check_dtor_name (scope, destructor))
2297 error ("qualified type %qT does not match destructor name ~%qT",
2298 scope, destructor);
2299 return error_mark_node;
2303 /* [expr.pseudo] says both:
2305 The type designated by the pseudo-destructor-name shall be
2306 the same as the object type.
2308 and:
2310 The cv-unqualified versions of the object type and of the
2311 type designated by the pseudo-destructor-name shall be the
2312 same type.
2314 We implement the more generous second sentence, since that is
2315 what most other compilers do. */
2316 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (object),
2317 destructor))
2319 error ("%qE is not of type %qT", object, destructor);
2320 return error_mark_node;
2324 return build3 (PSEUDO_DTOR_EXPR, void_type_node, object, scope, destructor);
2327 /* Finish an expression of the form CODE EXPR. */
2329 tree
2330 finish_unary_op_expr (location_t loc, enum tree_code code, tree expr)
2332 tree result = build_x_unary_op (loc, code, expr, tf_warning_or_error);
2333 if (TREE_OVERFLOW_P (result) && !TREE_OVERFLOW_P (expr))
2334 overflow_warning (input_location, result);
2336 return result;
2339 /* Finish a compound-literal expression. TYPE is the type to which
2340 the CONSTRUCTOR in COMPOUND_LITERAL is being cast. */
2342 tree
2343 finish_compound_literal (tree type, tree compound_literal,
2344 tsubst_flags_t complain)
2346 if (type == error_mark_node)
2347 return error_mark_node;
2349 if (TREE_CODE (type) == REFERENCE_TYPE)
2351 compound_literal
2352 = finish_compound_literal (TREE_TYPE (type), compound_literal,
2353 complain);
2354 return cp_build_c_cast (type, compound_literal, complain);
2357 if (!TYPE_OBJ_P (type))
2359 if (complain & tf_error)
2360 error ("compound literal of non-object type %qT", type);
2361 return error_mark_node;
2364 if (processing_template_decl)
2366 TREE_TYPE (compound_literal) = type;
2367 /* Mark the expression as a compound literal. */
2368 TREE_HAS_CONSTRUCTOR (compound_literal) = 1;
2369 return compound_literal;
2372 type = complete_type (type);
2374 if (TYPE_NON_AGGREGATE_CLASS (type))
2376 /* Trying to deal with a CONSTRUCTOR instead of a TREE_LIST
2377 everywhere that deals with function arguments would be a pain, so
2378 just wrap it in a TREE_LIST. The parser set a flag so we know
2379 that it came from T{} rather than T({}). */
2380 CONSTRUCTOR_IS_DIRECT_INIT (compound_literal) = 1;
2381 compound_literal = build_tree_list (NULL_TREE, compound_literal);
2382 return build_functional_cast (type, compound_literal, complain);
2385 if (TREE_CODE (type) == ARRAY_TYPE
2386 && check_array_initializer (NULL_TREE, type, compound_literal))
2387 return error_mark_node;
2388 compound_literal = reshape_init (type, compound_literal, complain);
2389 if (SCALAR_TYPE_P (type)
2390 && !BRACE_ENCLOSED_INITIALIZER_P (compound_literal)
2391 && (complain & tf_warning_or_error))
2392 check_narrowing (type, compound_literal);
2393 if (TREE_CODE (type) == ARRAY_TYPE
2394 && TYPE_DOMAIN (type) == NULL_TREE)
2396 cp_complete_array_type_or_error (&type, compound_literal,
2397 false, complain);
2398 if (type == error_mark_node)
2399 return error_mark_node;
2401 compound_literal = digest_init (type, compound_literal, complain);
2402 if (TREE_CODE (compound_literal) == CONSTRUCTOR)
2403 TREE_HAS_CONSTRUCTOR (compound_literal) = true;
2404 /* Put static/constant array temporaries in static variables, but always
2405 represent class temporaries with TARGET_EXPR so we elide copies. */
2406 if ((!at_function_scope_p () || CP_TYPE_CONST_P (type))
2407 && TREE_CODE (type) == ARRAY_TYPE
2408 && !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
2409 && initializer_constant_valid_p (compound_literal, type))
2411 tree decl = create_temporary_var (type);
2412 DECL_INITIAL (decl) = compound_literal;
2413 TREE_STATIC (decl) = 1;
2414 if (literal_type_p (type) && CP_TYPE_CONST_NON_VOLATILE_P (type))
2416 /* 5.19 says that a constant expression can include an
2417 lvalue-rvalue conversion applied to "a glvalue of literal type
2418 that refers to a non-volatile temporary object initialized
2419 with a constant expression". Rather than try to communicate
2420 that this VAR_DECL is a temporary, just mark it constexpr. */
2421 DECL_DECLARED_CONSTEXPR_P (decl) = true;
2422 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
2423 TREE_CONSTANT (decl) = true;
2425 cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
2426 decl = pushdecl_top_level (decl);
2427 DECL_NAME (decl) = make_anon_name ();
2428 SET_DECL_ASSEMBLER_NAME (decl, DECL_NAME (decl));
2429 return decl;
2431 else
2432 return get_target_expr_sfinae (compound_literal, complain);
2435 /* Return the declaration for the function-name variable indicated by
2436 ID. */
2438 tree
2439 finish_fname (tree id)
2441 tree decl;
2443 decl = fname_decl (input_location, C_RID_CODE (id), id);
2444 if (processing_template_decl && current_function_decl)
2445 decl = DECL_NAME (decl);
2446 return decl;
2449 /* Finish a translation unit. */
2451 void
2452 finish_translation_unit (void)
2454 /* In case there were missing closebraces,
2455 get us back to the global binding level. */
2456 pop_everything ();
2457 while (current_namespace != global_namespace)
2458 pop_namespace ();
2460 /* Do file scope __FUNCTION__ et al. */
2461 finish_fname_decls ();
2464 /* Finish a template type parameter, specified as AGGR IDENTIFIER.
2465 Returns the parameter. */
2467 tree
2468 finish_template_type_parm (tree aggr, tree identifier)
2470 if (aggr != class_type_node)
2472 permerror (input_location, "template type parameters must use the keyword %<class%> or %<typename%>");
2473 aggr = class_type_node;
2476 return build_tree_list (aggr, identifier);
2479 /* Finish a template template parameter, specified as AGGR IDENTIFIER.
2480 Returns the parameter. */
2482 tree
2483 finish_template_template_parm (tree aggr, tree identifier)
2485 tree decl = build_decl (input_location,
2486 TYPE_DECL, identifier, NULL_TREE);
2487 tree tmpl = build_lang_decl (TEMPLATE_DECL, identifier, NULL_TREE);
2488 DECL_TEMPLATE_PARMS (tmpl) = current_template_parms;
2489 DECL_TEMPLATE_RESULT (tmpl) = decl;
2490 DECL_ARTIFICIAL (decl) = 1;
2491 end_template_decl ();
2493 gcc_assert (DECL_TEMPLATE_PARMS (tmpl));
2495 check_default_tmpl_args (decl, DECL_TEMPLATE_PARMS (tmpl),
2496 /*is_primary=*/true, /*is_partial=*/false,
2497 /*is_friend=*/0);
2499 return finish_template_type_parm (aggr, tmpl);
2502 /* ARGUMENT is the default-argument value for a template template
2503 parameter. If ARGUMENT is invalid, issue error messages and return
2504 the ERROR_MARK_NODE. Otherwise, ARGUMENT itself is returned. */
2506 tree
2507 check_template_template_default_arg (tree argument)
2509 if (TREE_CODE (argument) != TEMPLATE_DECL
2510 && TREE_CODE (argument) != TEMPLATE_TEMPLATE_PARM
2511 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
2513 if (TREE_CODE (argument) == TYPE_DECL)
2514 error ("invalid use of type %qT as a default value for a template "
2515 "template-parameter", TREE_TYPE (argument));
2516 else
2517 error ("invalid default argument for a template template parameter");
2518 return error_mark_node;
2521 return argument;
2524 /* Begin a class definition, as indicated by T. */
2526 tree
2527 begin_class_definition (tree t)
2529 if (error_operand_p (t) || error_operand_p (TYPE_MAIN_DECL (t)))
2530 return error_mark_node;
2532 if (processing_template_parmlist)
2534 error ("definition of %q#T inside template parameter list", t);
2535 return error_mark_node;
2538 /* According to the C++ ABI, decimal classes defined in ISO/IEC TR 24733
2539 are passed the same as decimal scalar types. */
2540 if (TREE_CODE (t) == RECORD_TYPE
2541 && !processing_template_decl)
2543 tree ns = TYPE_CONTEXT (t);
2544 if (ns && TREE_CODE (ns) == NAMESPACE_DECL
2545 && DECL_CONTEXT (ns) == std_node
2546 && DECL_NAME (ns)
2547 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (ns)), "decimal"))
2549 const char *n = TYPE_NAME_STRING (t);
2550 if ((strcmp (n, "decimal32") == 0)
2551 || (strcmp (n, "decimal64") == 0)
2552 || (strcmp (n, "decimal128") == 0))
2553 TYPE_TRANSPARENT_AGGR (t) = 1;
2557 /* A non-implicit typename comes from code like:
2559 template <typename T> struct A {
2560 template <typename U> struct A<T>::B ...
2562 This is erroneous. */
2563 else if (TREE_CODE (t) == TYPENAME_TYPE)
2565 error ("invalid definition of qualified type %qT", t);
2566 t = error_mark_node;
2569 if (t == error_mark_node || ! MAYBE_CLASS_TYPE_P (t))
2571 t = make_class_type (RECORD_TYPE);
2572 pushtag (make_anon_name (), t, /*tag_scope=*/ts_current);
2575 if (TYPE_BEING_DEFINED (t))
2577 t = make_class_type (TREE_CODE (t));
2578 pushtag (TYPE_IDENTIFIER (t), t, /*tag_scope=*/ts_current);
2580 maybe_process_partial_specialization (t);
2581 pushclass (t);
2582 TYPE_BEING_DEFINED (t) = 1;
2584 if (flag_pack_struct)
2586 tree v;
2587 TYPE_PACKED (t) = 1;
2588 /* Even though the type is being defined for the first time
2589 here, there might have been a forward declaration, so there
2590 might be cv-qualified variants of T. */
2591 for (v = TYPE_NEXT_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v))
2592 TYPE_PACKED (v) = 1;
2594 /* Reset the interface data, at the earliest possible
2595 moment, as it might have been set via a class foo;
2596 before. */
2597 if (! TYPE_ANONYMOUS_P (t))
2599 struct c_fileinfo *finfo = get_fileinfo (input_filename);
2600 CLASSTYPE_INTERFACE_ONLY (t) = finfo->interface_only;
2601 SET_CLASSTYPE_INTERFACE_UNKNOWN_X
2602 (t, finfo->interface_unknown);
2604 reset_specialization();
2606 /* Make a declaration for this class in its own scope. */
2607 build_self_reference ();
2609 return t;
2612 /* Finish the member declaration given by DECL. */
2614 void
2615 finish_member_declaration (tree decl)
2617 if (decl == error_mark_node || decl == NULL_TREE)
2618 return;
2620 if (decl == void_type_node)
2621 /* The COMPONENT was a friend, not a member, and so there's
2622 nothing for us to do. */
2623 return;
2625 /* We should see only one DECL at a time. */
2626 gcc_assert (DECL_CHAIN (decl) == NULL_TREE);
2628 /* Set up access control for DECL. */
2629 TREE_PRIVATE (decl)
2630 = (current_access_specifier == access_private_node);
2631 TREE_PROTECTED (decl)
2632 = (current_access_specifier == access_protected_node);
2633 if (TREE_CODE (decl) == TEMPLATE_DECL)
2635 TREE_PRIVATE (DECL_TEMPLATE_RESULT (decl)) = TREE_PRIVATE (decl);
2636 TREE_PROTECTED (DECL_TEMPLATE_RESULT (decl)) = TREE_PROTECTED (decl);
2639 /* Mark the DECL as a member of the current class, unless it's
2640 a member of an enumeration. */
2641 if (TREE_CODE (decl) != CONST_DECL)
2642 DECL_CONTEXT (decl) = current_class_type;
2644 /* Check for bare parameter packs in the member variable declaration. */
2645 if (TREE_CODE (decl) == FIELD_DECL)
2647 if (check_for_bare_parameter_packs (TREE_TYPE (decl)))
2648 TREE_TYPE (decl) = error_mark_node;
2649 if (check_for_bare_parameter_packs (DECL_ATTRIBUTES (decl)))
2650 DECL_ATTRIBUTES (decl) = NULL_TREE;
2653 /* [dcl.link]
2655 A C language linkage is ignored for the names of class members
2656 and the member function type of class member functions. */
2657 if (DECL_LANG_SPECIFIC (decl) && DECL_LANGUAGE (decl) == lang_c)
2658 SET_DECL_LANGUAGE (decl, lang_cplusplus);
2660 /* Put functions on the TYPE_METHODS list and everything else on the
2661 TYPE_FIELDS list. Note that these are built up in reverse order.
2662 We reverse them (to obtain declaration order) in finish_struct. */
2663 if (TREE_CODE (decl) == FUNCTION_DECL
2664 || DECL_FUNCTION_TEMPLATE_P (decl))
2666 /* We also need to add this function to the
2667 CLASSTYPE_METHOD_VEC. */
2668 if (add_method (current_class_type, decl, NULL_TREE))
2670 DECL_CHAIN (decl) = TYPE_METHODS (current_class_type);
2671 TYPE_METHODS (current_class_type) = decl;
2673 maybe_add_class_template_decl_list (current_class_type, decl,
2674 /*friend_p=*/0);
2677 /* Enter the DECL into the scope of the class. */
2678 else if (pushdecl_class_level (decl))
2680 if (TREE_CODE (decl) == USING_DECL)
2682 /* For now, ignore class-scope USING_DECLS, so that
2683 debugging backends do not see them. */
2684 DECL_IGNORED_P (decl) = 1;
2687 /* All TYPE_DECLs go at the end of TYPE_FIELDS. Ordinary fields
2688 go at the beginning. The reason is that lookup_field_1
2689 searches the list in order, and we want a field name to
2690 override a type name so that the "struct stat hack" will
2691 work. In particular:
2693 struct S { enum E { }; int E } s;
2694 s.E = 3;
2696 is valid. In addition, the FIELD_DECLs must be maintained in
2697 declaration order so that class layout works as expected.
2698 However, we don't need that order until class layout, so we
2699 save a little time by putting FIELD_DECLs on in reverse order
2700 here, and then reversing them in finish_struct_1. (We could
2701 also keep a pointer to the correct insertion points in the
2702 list.) */
2704 if (TREE_CODE (decl) == TYPE_DECL)
2705 TYPE_FIELDS (current_class_type)
2706 = chainon (TYPE_FIELDS (current_class_type), decl);
2707 else
2709 DECL_CHAIN (decl) = TYPE_FIELDS (current_class_type);
2710 TYPE_FIELDS (current_class_type) = decl;
2713 maybe_add_class_template_decl_list (current_class_type, decl,
2714 /*friend_p=*/0);
2717 if (pch_file)
2718 note_decl_for_pch (decl);
2721 /* DECL has been declared while we are building a PCH file. Perform
2722 actions that we might normally undertake lazily, but which can be
2723 performed now so that they do not have to be performed in
2724 translation units which include the PCH file. */
2726 void
2727 note_decl_for_pch (tree decl)
2729 gcc_assert (pch_file);
2731 /* There's a good chance that we'll have to mangle names at some
2732 point, even if only for emission in debugging information. */
2733 if ((TREE_CODE (decl) == VAR_DECL
2734 || TREE_CODE (decl) == FUNCTION_DECL)
2735 && !processing_template_decl)
2736 mangle_decl (decl);
2739 /* Finish processing a complete template declaration. The PARMS are
2740 the template parameters. */
2742 void
2743 finish_template_decl (tree parms)
2745 if (parms)
2746 end_template_decl ();
2747 else
2748 end_specialization ();
2751 /* Finish processing a template-id (which names a type) of the form
2752 NAME < ARGS >. Return the TYPE_DECL for the type named by the
2753 template-id. If ENTERING_SCOPE is nonzero we are about to enter
2754 the scope of template-id indicated. */
2756 tree
2757 finish_template_type (tree name, tree args, int entering_scope)
2759 tree type;
2761 type = lookup_template_class (name, args,
2762 NULL_TREE, NULL_TREE, entering_scope,
2763 tf_warning_or_error | tf_user);
2764 if (type == error_mark_node)
2765 return type;
2766 else if (CLASS_TYPE_P (type) && !alias_type_or_template_p (type))
2767 return TYPE_STUB_DECL (type);
2768 else
2769 return TYPE_NAME (type);
2772 /* Finish processing a BASE_CLASS with the indicated ACCESS_SPECIFIER.
2773 Return a TREE_LIST containing the ACCESS_SPECIFIER and the
2774 BASE_CLASS, or NULL_TREE if an error occurred. The
2775 ACCESS_SPECIFIER is one of
2776 access_{default,public,protected_private}_node. For a virtual base
2777 we set TREE_TYPE. */
2779 tree
2780 finish_base_specifier (tree base, tree access, bool virtual_p)
2782 tree result;
2784 if (base == error_mark_node)
2786 error ("invalid base-class specification");
2787 result = NULL_TREE;
2789 else if (! MAYBE_CLASS_TYPE_P (base))
2791 error ("%qT is not a class type", base);
2792 result = NULL_TREE;
2794 else
2796 if (cp_type_quals (base) != 0)
2798 /* DR 484: Can a base-specifier name a cv-qualified
2799 class type? */
2800 base = TYPE_MAIN_VARIANT (base);
2802 result = build_tree_list (access, base);
2803 if (virtual_p)
2804 TREE_TYPE (result) = integer_type_node;
2807 return result;
2810 /* If FNS is a member function, a set of member functions, or a
2811 template-id referring to one or more member functions, return a
2812 BASELINK for FNS, incorporating the current access context.
2813 Otherwise, return FNS unchanged. */
2815 tree
2816 baselink_for_fns (tree fns)
2818 tree scope;
2819 tree cl;
2821 if (BASELINK_P (fns)
2822 || error_operand_p (fns))
2823 return fns;
2825 scope = ovl_scope (fns);
2826 if (!CLASS_TYPE_P (scope))
2827 return fns;
2829 cl = currently_open_derived_class (scope);
2830 if (!cl)
2831 cl = scope;
2832 cl = TYPE_BINFO (cl);
2833 return build_baselink (cl, cl, fns, /*optype=*/NULL_TREE);
2836 /* Returns true iff DECL is an automatic variable from a function outside
2837 the current one. */
2839 static bool
2840 outer_automatic_var_p (tree decl)
2842 return ((TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL)
2843 && DECL_FUNCTION_SCOPE_P (decl)
2844 && !TREE_STATIC (decl)
2845 && DECL_CONTEXT (decl) != current_function_decl);
2848 /* ID_EXPRESSION is a representation of parsed, but unprocessed,
2849 id-expression. (See cp_parser_id_expression for details.) SCOPE,
2850 if non-NULL, is the type or namespace used to explicitly qualify
2851 ID_EXPRESSION. DECL is the entity to which that name has been
2852 resolved.
2854 *CONSTANT_EXPRESSION_P is true if we are presently parsing a
2855 constant-expression. In that case, *NON_CONSTANT_EXPRESSION_P will
2856 be set to true if this expression isn't permitted in a
2857 constant-expression, but it is otherwise not set by this function.
2858 *ALLOW_NON_CONSTANT_EXPRESSION_P is true if we are parsing a
2859 constant-expression, but a non-constant expression is also
2860 permissible.
2862 DONE is true if this expression is a complete postfix-expression;
2863 it is false if this expression is followed by '->', '[', '(', etc.
2864 ADDRESS_P is true iff this expression is the operand of '&'.
2865 TEMPLATE_P is true iff the qualified-id was of the form
2866 "A::template B". TEMPLATE_ARG_P is true iff this qualified name
2867 appears as a template argument.
2869 If an error occurs, and it is the kind of error that might cause
2870 the parser to abort a tentative parse, *ERROR_MSG is filled in. It
2871 is the caller's responsibility to issue the message. *ERROR_MSG
2872 will be a string with static storage duration, so the caller need
2873 not "free" it.
2875 Return an expression for the entity, after issuing appropriate
2876 diagnostics. This function is also responsible for transforming a
2877 reference to a non-static member into a COMPONENT_REF that makes
2878 the use of "this" explicit.
2880 Upon return, *IDK will be filled in appropriately. */
2881 tree
2882 finish_id_expression (tree id_expression,
2883 tree decl,
2884 tree scope,
2885 cp_id_kind *idk,
2886 bool integral_constant_expression_p,
2887 bool allow_non_integral_constant_expression_p,
2888 bool *non_integral_constant_expression_p,
2889 bool template_p,
2890 bool done,
2891 bool address_p,
2892 bool template_arg_p,
2893 const char **error_msg,
2894 location_t location)
2896 decl = strip_using_decl (decl);
2898 /* Initialize the output parameters. */
2899 *idk = CP_ID_KIND_NONE;
2900 *error_msg = NULL;
2902 if (id_expression == error_mark_node)
2903 return error_mark_node;
2904 /* If we have a template-id, then no further lookup is
2905 required. If the template-id was for a template-class, we
2906 will sometimes have a TYPE_DECL at this point. */
2907 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
2908 || TREE_CODE (decl) == TYPE_DECL)
2910 /* Look up the name. */
2911 else
2913 if (decl == error_mark_node)
2915 /* Name lookup failed. */
2916 if (scope
2917 && (!TYPE_P (scope)
2918 || (!dependent_type_p (scope)
2919 && !(TREE_CODE (id_expression) == IDENTIFIER_NODE
2920 && IDENTIFIER_TYPENAME_P (id_expression)
2921 && dependent_type_p (TREE_TYPE (id_expression))))))
2923 /* If the qualifying type is non-dependent (and the name
2924 does not name a conversion operator to a dependent
2925 type), issue an error. */
2926 qualified_name_lookup_error (scope, id_expression, decl, location);
2927 return error_mark_node;
2929 else if (!scope)
2931 /* It may be resolved via Koenig lookup. */
2932 *idk = CP_ID_KIND_UNQUALIFIED;
2933 return id_expression;
2935 else
2936 decl = id_expression;
2938 /* If DECL is a variable that would be out of scope under
2939 ANSI/ISO rules, but in scope in the ARM, name lookup
2940 will succeed. Issue a diagnostic here. */
2941 else
2942 decl = check_for_out_of_scope_variable (decl);
2944 /* Remember that the name was used in the definition of
2945 the current class so that we can check later to see if
2946 the meaning would have been different after the class
2947 was entirely defined. */
2948 if (!scope && decl != error_mark_node
2949 && TREE_CODE (id_expression) == IDENTIFIER_NODE)
2950 maybe_note_name_used_in_class (id_expression, decl);
2952 /* Disallow uses of local variables from containing functions, except
2953 within lambda-expressions. */
2954 if (outer_automatic_var_p (decl)
2955 /* It's not a use (3.2) if we're in an unevaluated context. */
2956 && !cp_unevaluated_operand)
2958 tree context = DECL_CONTEXT (decl);
2959 tree containing_function = current_function_decl;
2960 tree lambda_stack = NULL_TREE;
2961 tree lambda_expr = NULL_TREE;
2962 tree initializer = convert_from_reference (decl);
2964 /* Mark it as used now even if the use is ill-formed. */
2965 mark_used (decl);
2967 /* Core issue 696: "[At the July 2009 meeting] the CWG expressed
2968 support for an approach in which a reference to a local
2969 [constant] automatic variable in a nested class or lambda body
2970 would enter the expression as an rvalue, which would reduce
2971 the complexity of the problem"
2973 FIXME update for final resolution of core issue 696. */
2974 if (decl_constant_var_p (decl))
2975 return integral_constant_value (decl);
2977 /* If we are in a lambda function, we can move out until we hit
2978 1. the context,
2979 2. a non-lambda function, or
2980 3. a non-default capturing lambda function. */
2981 while (context != containing_function
2982 && LAMBDA_FUNCTION_P (containing_function))
2984 lambda_expr = CLASSTYPE_LAMBDA_EXPR
2985 (DECL_CONTEXT (containing_function));
2987 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr)
2988 == CPLD_NONE)
2989 break;
2991 lambda_stack = tree_cons (NULL_TREE,
2992 lambda_expr,
2993 lambda_stack);
2995 containing_function
2996 = decl_function_context (containing_function);
2999 if (context == containing_function)
3001 decl = add_default_capture (lambda_stack,
3002 /*id=*/DECL_NAME (decl),
3003 initializer);
3005 else if (lambda_expr)
3007 error ("%qD is not captured", decl);
3008 return error_mark_node;
3010 else
3012 error (TREE_CODE (decl) == VAR_DECL
3013 ? G_("use of %<auto%> variable from containing function")
3014 : G_("use of parameter from containing function"));
3015 error (" %q+#D declared here", decl);
3016 return error_mark_node;
3020 /* Also disallow uses of function parameters outside the function
3021 body, except inside an unevaluated context (i.e. decltype). */
3022 if (TREE_CODE (decl) == PARM_DECL
3023 && DECL_CONTEXT (decl) == NULL_TREE
3024 && !cp_unevaluated_operand)
3026 error ("use of parameter %qD outside function body", decl);
3027 return error_mark_node;
3031 /* If we didn't find anything, or what we found was a type,
3032 then this wasn't really an id-expression. */
3033 if (TREE_CODE (decl) == TEMPLATE_DECL
3034 && !DECL_FUNCTION_TEMPLATE_P (decl))
3036 *error_msg = "missing template arguments";
3037 return error_mark_node;
3039 else if (TREE_CODE (decl) == TYPE_DECL
3040 || TREE_CODE (decl) == NAMESPACE_DECL)
3042 *error_msg = "expected primary-expression";
3043 return error_mark_node;
3046 /* If the name resolved to a template parameter, there is no
3047 need to look it up again later. */
3048 if ((TREE_CODE (decl) == CONST_DECL && DECL_TEMPLATE_PARM_P (decl))
3049 || TREE_CODE (decl) == TEMPLATE_PARM_INDEX)
3051 tree r;
3053 *idk = CP_ID_KIND_NONE;
3054 if (TREE_CODE (decl) == TEMPLATE_PARM_INDEX)
3055 decl = TEMPLATE_PARM_DECL (decl);
3056 r = convert_from_reference (DECL_INITIAL (decl));
3058 if (integral_constant_expression_p
3059 && !dependent_type_p (TREE_TYPE (decl))
3060 && !(INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (r))))
3062 if (!allow_non_integral_constant_expression_p)
3063 error ("template parameter %qD of type %qT is not allowed in "
3064 "an integral constant expression because it is not of "
3065 "integral or enumeration type", decl, TREE_TYPE (decl));
3066 *non_integral_constant_expression_p = true;
3068 return r;
3070 else
3072 bool dependent_p;
3074 /* If the declaration was explicitly qualified indicate
3075 that. The semantics of `A::f(3)' are different than
3076 `f(3)' if `f' is virtual. */
3077 *idk = (scope
3078 ? CP_ID_KIND_QUALIFIED
3079 : (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3080 ? CP_ID_KIND_TEMPLATE_ID
3081 : CP_ID_KIND_UNQUALIFIED));
3084 /* [temp.dep.expr]
3086 An id-expression is type-dependent if it contains an
3087 identifier that was declared with a dependent type.
3089 The standard is not very specific about an id-expression that
3090 names a set of overloaded functions. What if some of them
3091 have dependent types and some of them do not? Presumably,
3092 such a name should be treated as a dependent name. */
3093 /* Assume the name is not dependent. */
3094 dependent_p = false;
3095 if (!processing_template_decl)
3096 /* No names are dependent outside a template. */
3098 else if (TREE_CODE (decl) == CONST_DECL)
3099 /* We don't want to treat enumerators as dependent. */
3101 /* A template-id where the name of the template was not resolved
3102 is definitely dependent. */
3103 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3104 && (TREE_CODE (TREE_OPERAND (decl, 0))
3105 == IDENTIFIER_NODE))
3106 dependent_p = true;
3107 /* For anything except an overloaded function, just check its
3108 type. */
3109 else if (!is_overloaded_fn (decl))
3110 dependent_p
3111 = dependent_type_p (TREE_TYPE (decl));
3112 /* For a set of overloaded functions, check each of the
3113 functions. */
3114 else
3116 tree fns = decl;
3118 if (BASELINK_P (fns))
3119 fns = BASELINK_FUNCTIONS (fns);
3121 /* For a template-id, check to see if the template
3122 arguments are dependent. */
3123 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
3125 tree args = TREE_OPERAND (fns, 1);
3126 dependent_p = any_dependent_template_arguments_p (args);
3127 /* The functions are those referred to by the
3128 template-id. */
3129 fns = TREE_OPERAND (fns, 0);
3132 /* If there are no dependent template arguments, go through
3133 the overloaded functions. */
3134 while (fns && !dependent_p)
3136 tree fn = OVL_CURRENT (fns);
3138 /* Member functions of dependent classes are
3139 dependent. */
3140 if (TREE_CODE (fn) == FUNCTION_DECL
3141 && type_dependent_expression_p (fn))
3142 dependent_p = true;
3143 else if (TREE_CODE (fn) == TEMPLATE_DECL
3144 && dependent_template_p (fn))
3145 dependent_p = true;
3147 fns = OVL_NEXT (fns);
3151 /* If the name was dependent on a template parameter, we will
3152 resolve the name at instantiation time. */
3153 if (dependent_p)
3155 /* Create a SCOPE_REF for qualified names, if the scope is
3156 dependent. */
3157 if (scope)
3159 if (TYPE_P (scope))
3161 if (address_p && done)
3162 decl = finish_qualified_id_expr (scope, decl,
3163 done, address_p,
3164 template_p,
3165 template_arg_p);
3166 else
3168 tree type = NULL_TREE;
3169 if (DECL_P (decl) && !dependent_scope_p (scope))
3170 type = TREE_TYPE (decl);
3171 decl = build_qualified_name (type,
3172 scope,
3173 id_expression,
3174 template_p);
3177 if (TREE_TYPE (decl))
3178 decl = convert_from_reference (decl);
3179 return decl;
3181 /* A TEMPLATE_ID already contains all the information we
3182 need. */
3183 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR)
3184 return id_expression;
3185 *idk = CP_ID_KIND_UNQUALIFIED_DEPENDENT;
3186 /* If we found a variable, then name lookup during the
3187 instantiation will always resolve to the same VAR_DECL
3188 (or an instantiation thereof). */
3189 if (TREE_CODE (decl) == VAR_DECL
3190 || TREE_CODE (decl) == PARM_DECL)
3192 mark_used (decl);
3193 return convert_from_reference (decl);
3195 /* The same is true for FIELD_DECL, but we also need to
3196 make sure that the syntax is correct. */
3197 else if (TREE_CODE (decl) == FIELD_DECL)
3199 /* Since SCOPE is NULL here, this is an unqualified name.
3200 Access checking has been performed during name lookup
3201 already. Turn off checking to avoid duplicate errors. */
3202 push_deferring_access_checks (dk_no_check);
3203 decl = finish_non_static_data_member
3204 (decl, NULL_TREE,
3205 /*qualifying_scope=*/NULL_TREE);
3206 pop_deferring_access_checks ();
3207 return decl;
3209 return id_expression;
3212 if (TREE_CODE (decl) == NAMESPACE_DECL)
3214 error ("use of namespace %qD as expression", decl);
3215 return error_mark_node;
3217 else if (DECL_CLASS_TEMPLATE_P (decl))
3219 error ("use of class template %qT as expression", decl);
3220 return error_mark_node;
3222 else if (TREE_CODE (decl) == TREE_LIST)
3224 /* Ambiguous reference to base members. */
3225 error ("request for member %qD is ambiguous in "
3226 "multiple inheritance lattice", id_expression);
3227 print_candidates (decl);
3228 return error_mark_node;
3231 /* Mark variable-like entities as used. Functions are similarly
3232 marked either below or after overload resolution. */
3233 if ((TREE_CODE (decl) == VAR_DECL
3234 || TREE_CODE (decl) == PARM_DECL
3235 || TREE_CODE (decl) == CONST_DECL
3236 || TREE_CODE (decl) == RESULT_DECL)
3237 && !mark_used (decl))
3238 return error_mark_node;
3240 /* Only certain kinds of names are allowed in constant
3241 expression. Template parameters have already
3242 been handled above. */
3243 if (! error_operand_p (decl)
3244 && integral_constant_expression_p
3245 && ! decl_constant_var_p (decl)
3246 && TREE_CODE (decl) != CONST_DECL
3247 && ! builtin_valid_in_constant_expr_p (decl))
3249 if (!allow_non_integral_constant_expression_p)
3251 error ("%qD cannot appear in a constant-expression", decl);
3252 return error_mark_node;
3254 *non_integral_constant_expression_p = true;
3257 if (scope)
3259 decl = (adjust_result_of_qualified_name_lookup
3260 (decl, scope, current_nonlambda_class_type()));
3262 if (TREE_CODE (decl) == FUNCTION_DECL)
3263 mark_used (decl);
3265 if (TYPE_P (scope))
3266 decl = finish_qualified_id_expr (scope,
3267 decl,
3268 done,
3269 address_p,
3270 template_p,
3271 template_arg_p);
3272 else
3273 decl = convert_from_reference (decl);
3275 else if (TREE_CODE (decl) == FIELD_DECL)
3277 /* Since SCOPE is NULL here, this is an unqualified name.
3278 Access checking has been performed during name lookup
3279 already. Turn off checking to avoid duplicate errors. */
3280 push_deferring_access_checks (dk_no_check);
3281 decl = finish_non_static_data_member (decl, NULL_TREE,
3282 /*qualifying_scope=*/NULL_TREE);
3283 pop_deferring_access_checks ();
3285 else if (is_overloaded_fn (decl))
3287 tree first_fn;
3289 first_fn = get_first_fn (decl);
3290 if (TREE_CODE (first_fn) == TEMPLATE_DECL)
3291 first_fn = DECL_TEMPLATE_RESULT (first_fn);
3293 if (!really_overloaded_fn (decl)
3294 && !mark_used (first_fn))
3295 return error_mark_node;
3297 if (!template_arg_p
3298 && TREE_CODE (first_fn) == FUNCTION_DECL
3299 && DECL_FUNCTION_MEMBER_P (first_fn)
3300 && !shared_member_p (decl))
3302 /* A set of member functions. */
3303 decl = maybe_dummy_object (DECL_CONTEXT (first_fn), 0);
3304 return finish_class_member_access_expr (decl, id_expression,
3305 /*template_p=*/false,
3306 tf_warning_or_error);
3309 decl = baselink_for_fns (decl);
3311 else
3313 if (DECL_P (decl) && DECL_NONLOCAL (decl)
3314 && DECL_CLASS_SCOPE_P (decl))
3316 tree context = context_for_name_lookup (decl);
3317 if (context != current_class_type)
3319 tree path = currently_open_derived_class (context);
3320 perform_or_defer_access_check (TYPE_BINFO (path),
3321 decl, decl,
3322 tf_warning_or_error);
3326 decl = convert_from_reference (decl);
3330 if (TREE_DEPRECATED (decl))
3331 warn_deprecated_use (decl, NULL_TREE);
3333 return decl;
3336 /* Implement the __typeof keyword: Return the type of EXPR, suitable for
3337 use as a type-specifier. */
3339 tree
3340 finish_typeof (tree expr)
3342 tree type;
3344 if (type_dependent_expression_p (expr))
3346 type = cxx_make_type (TYPEOF_TYPE);
3347 TYPEOF_TYPE_EXPR (type) = expr;
3348 SET_TYPE_STRUCTURAL_EQUALITY (type);
3350 return type;
3353 expr = mark_type_use (expr);
3355 type = unlowered_expr_type (expr);
3357 if (!type || type == unknown_type_node)
3359 error ("type of %qE is unknown", expr);
3360 return error_mark_node;
3363 return type;
3366 /* Implement the __underlying_type keyword: Return the underlying
3367 type of TYPE, suitable for use as a type-specifier. */
3369 tree
3370 finish_underlying_type (tree type)
3372 tree underlying_type;
3374 if (processing_template_decl)
3376 underlying_type = cxx_make_type (UNDERLYING_TYPE);
3377 UNDERLYING_TYPE_TYPE (underlying_type) = type;
3378 SET_TYPE_STRUCTURAL_EQUALITY (underlying_type);
3380 return underlying_type;
3383 complete_type (type);
3385 if (TREE_CODE (type) != ENUMERAL_TYPE)
3387 error ("%qT is not an enumeration type", type);
3388 return error_mark_node;
3391 underlying_type = ENUM_UNDERLYING_TYPE (type);
3393 /* Fixup necessary in this case because ENUM_UNDERLYING_TYPE
3394 includes TYPE_MIN_VALUE and TYPE_MAX_VALUE information.
3395 See finish_enum_value_list for details. */
3396 if (!ENUM_FIXED_UNDERLYING_TYPE_P (type))
3397 underlying_type
3398 = c_common_type_for_mode (TYPE_MODE (underlying_type),
3399 TYPE_UNSIGNED (underlying_type));
3401 return underlying_type;
3404 /* Implement the __direct_bases keyword: Return the direct base classes
3405 of type */
3407 tree
3408 calculate_direct_bases (tree type)
3410 VEC(tree, gc) *vector = make_tree_vector();
3411 tree bases_vec = NULL_TREE;
3412 VEC(tree, none) *base_binfos;
3413 tree binfo;
3414 unsigned i;
3416 complete_type (type);
3418 if (!NON_UNION_CLASS_TYPE_P (type))
3419 return make_tree_vec (0);
3421 base_binfos = BINFO_BASE_BINFOS (TYPE_BINFO (type));
3423 /* Virtual bases are initialized first */
3424 for (i = 0; VEC_iterate (tree, base_binfos, i, binfo); i++)
3426 if (BINFO_VIRTUAL_P (binfo))
3428 VEC_safe_push (tree, gc, vector, binfo);
3432 /* Now non-virtuals */
3433 for (i = 0; VEC_iterate (tree, base_binfos, i, binfo); i++)
3435 if (!BINFO_VIRTUAL_P (binfo))
3437 VEC_safe_push (tree, gc, vector, binfo);
3442 bases_vec = make_tree_vec (VEC_length (tree, vector));
3444 for (i = 0; i < VEC_length (tree, vector); ++i)
3446 TREE_VEC_ELT (bases_vec, i) = BINFO_TYPE (VEC_index (tree, vector, i));
3448 return bases_vec;
3451 /* Implement the __bases keyword: Return the base classes
3452 of type */
3454 /* Find morally non-virtual base classes by walking binfo hierarchy */
3455 /* Virtual base classes are handled separately in finish_bases */
3457 static tree
3458 dfs_calculate_bases_pre (tree binfo, void * /*data_*/)
3460 /* Don't walk bases of virtual bases */
3461 return BINFO_VIRTUAL_P (binfo) ? dfs_skip_bases : NULL_TREE;
3464 static tree
3465 dfs_calculate_bases_post (tree binfo, void *data_)
3467 VEC(tree, gc) **data = (VEC(tree, gc) **) data_;
3468 if (!BINFO_VIRTUAL_P (binfo))
3470 VEC_safe_push (tree, gc, *data, BINFO_TYPE (binfo));
3472 return NULL_TREE;
3475 /* Calculates the morally non-virtual base classes of a class */
3476 static VEC(tree, gc) *
3477 calculate_bases_helper (tree type)
3479 VEC(tree, gc) *vector = make_tree_vector();
3481 /* Now add non-virtual base classes in order of construction */
3482 dfs_walk_all (TYPE_BINFO (type),
3483 dfs_calculate_bases_pre, dfs_calculate_bases_post, &vector);
3484 return vector;
3487 tree
3488 calculate_bases (tree type)
3490 VEC(tree, gc) *vector = make_tree_vector();
3491 tree bases_vec = NULL_TREE;
3492 unsigned i;
3493 VEC(tree, gc) *vbases;
3494 VEC(tree, gc) *nonvbases;
3495 tree binfo;
3497 complete_type (type);
3499 if (!NON_UNION_CLASS_TYPE_P (type))
3500 return make_tree_vec (0);
3502 /* First go through virtual base classes */
3503 for (vbases = CLASSTYPE_VBASECLASSES (type), i = 0;
3504 VEC_iterate (tree, vbases, i, binfo); i++)
3506 VEC(tree, gc) *vbase_bases = calculate_bases_helper (BINFO_TYPE (binfo));
3507 VEC_safe_splice (tree, gc, vector, vbase_bases);
3508 release_tree_vector (vbase_bases);
3511 /* Now for the non-virtual bases */
3512 nonvbases = calculate_bases_helper (type);
3513 VEC_safe_splice (tree, gc, vector, nonvbases);
3514 release_tree_vector (nonvbases);
3516 /* Last element is entire class, so don't copy */
3517 bases_vec = make_tree_vec (VEC_length (tree, vector) - 1);
3519 for (i = 0; i < VEC_length (tree, vector) - 1; ++i)
3521 TREE_VEC_ELT (bases_vec, i) = VEC_index (tree, vector, i);
3523 release_tree_vector (vector);
3524 return bases_vec;
3527 tree
3528 finish_bases (tree type, bool direct)
3530 tree bases = NULL_TREE;
3532 if (!processing_template_decl)
3534 /* Parameter packs can only be used in templates */
3535 error ("Parameter pack __bases only valid in template declaration");
3536 return error_mark_node;
3539 bases = cxx_make_type (BASES);
3540 BASES_TYPE (bases) = type;
3541 BASES_DIRECT (bases) = direct;
3542 SET_TYPE_STRUCTURAL_EQUALITY (bases);
3544 return bases;
3547 /* Perform C++-specific checks for __builtin_offsetof before calling
3548 fold_offsetof. */
3550 tree
3551 finish_offsetof (tree expr)
3553 if (TREE_CODE (expr) == PSEUDO_DTOR_EXPR)
3555 error ("cannot apply %<offsetof%> to destructor %<~%T%>",
3556 TREE_OPERAND (expr, 2));
3557 return error_mark_node;
3559 if (TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE
3560 || TREE_CODE (TREE_TYPE (expr)) == METHOD_TYPE
3561 || TREE_TYPE (expr) == unknown_type_node)
3563 if (TREE_CODE (expr) == COMPONENT_REF
3564 || TREE_CODE (expr) == COMPOUND_EXPR)
3565 expr = TREE_OPERAND (expr, 1);
3566 error ("cannot apply %<offsetof%> to member function %qD", expr);
3567 return error_mark_node;
3569 if (REFERENCE_REF_P (expr))
3570 expr = TREE_OPERAND (expr, 0);
3571 if (TREE_CODE (expr) == COMPONENT_REF)
3573 tree object = TREE_OPERAND (expr, 0);
3574 if (!complete_type_or_else (TREE_TYPE (object), object))
3575 return error_mark_node;
3577 return fold_offsetof (expr);
3580 /* Replace the AGGR_INIT_EXPR at *TP with an equivalent CALL_EXPR. This
3581 function is broken out from the above for the benefit of the tree-ssa
3582 project. */
3584 void
3585 simplify_aggr_init_expr (tree *tp)
3587 tree aggr_init_expr = *tp;
3589 /* Form an appropriate CALL_EXPR. */
3590 tree fn = AGGR_INIT_EXPR_FN (aggr_init_expr);
3591 tree slot = AGGR_INIT_EXPR_SLOT (aggr_init_expr);
3592 tree type = TREE_TYPE (slot);
3594 tree call_expr;
3595 enum style_t { ctor, arg, pcc } style;
3597 if (AGGR_INIT_VIA_CTOR_P (aggr_init_expr))
3598 style = ctor;
3599 #ifdef PCC_STATIC_STRUCT_RETURN
3600 else if (1)
3601 style = pcc;
3602 #endif
3603 else
3605 gcc_assert (TREE_ADDRESSABLE (type));
3606 style = arg;
3609 call_expr = build_call_array_loc (input_location,
3610 TREE_TYPE (TREE_TYPE (TREE_TYPE (fn))),
3612 aggr_init_expr_nargs (aggr_init_expr),
3613 AGGR_INIT_EXPR_ARGP (aggr_init_expr));
3614 TREE_NOTHROW (call_expr) = TREE_NOTHROW (aggr_init_expr);
3616 if (style == ctor)
3618 /* Replace the first argument to the ctor with the address of the
3619 slot. */
3620 cxx_mark_addressable (slot);
3621 CALL_EXPR_ARG (call_expr, 0) =
3622 build1 (ADDR_EXPR, build_pointer_type (type), slot);
3624 else if (style == arg)
3626 /* Just mark it addressable here, and leave the rest to
3627 expand_call{,_inline}. */
3628 cxx_mark_addressable (slot);
3629 CALL_EXPR_RETURN_SLOT_OPT (call_expr) = true;
3630 call_expr = build2 (INIT_EXPR, TREE_TYPE (call_expr), slot, call_expr);
3632 else if (style == pcc)
3634 /* If we're using the non-reentrant PCC calling convention, then we
3635 need to copy the returned value out of the static buffer into the
3636 SLOT. */
3637 push_deferring_access_checks (dk_no_check);
3638 call_expr = build_aggr_init (slot, call_expr,
3639 DIRECT_BIND | LOOKUP_ONLYCONVERTING,
3640 tf_warning_or_error);
3641 pop_deferring_access_checks ();
3642 call_expr = build2 (COMPOUND_EXPR, TREE_TYPE (slot), call_expr, slot);
3645 if (AGGR_INIT_ZERO_FIRST (aggr_init_expr))
3647 tree init = build_zero_init (type, NULL_TREE,
3648 /*static_storage_p=*/false);
3649 init = build2 (INIT_EXPR, void_type_node, slot, init);
3650 call_expr = build2 (COMPOUND_EXPR, TREE_TYPE (call_expr),
3651 init, call_expr);
3654 *tp = call_expr;
3657 /* Emit all thunks to FN that should be emitted when FN is emitted. */
3659 void
3660 emit_associated_thunks (tree fn)
3662 /* When we use vcall offsets, we emit thunks with the virtual
3663 functions to which they thunk. The whole point of vcall offsets
3664 is so that you can know statically the entire set of thunks that
3665 will ever be needed for a given virtual function, thereby
3666 enabling you to output all the thunks with the function itself. */
3667 if (DECL_VIRTUAL_P (fn)
3668 /* Do not emit thunks for extern template instantiations. */
3669 && ! DECL_REALLY_EXTERN (fn))
3671 tree thunk;
3673 for (thunk = DECL_THUNKS (fn); thunk; thunk = DECL_CHAIN (thunk))
3675 if (!THUNK_ALIAS (thunk))
3677 use_thunk (thunk, /*emit_p=*/1);
3678 if (DECL_RESULT_THUNK_P (thunk))
3680 tree probe;
3682 for (probe = DECL_THUNKS (thunk);
3683 probe; probe = DECL_CHAIN (probe))
3684 use_thunk (probe, /*emit_p=*/1);
3687 else
3688 gcc_assert (!DECL_THUNKS (thunk));
3693 /* Returns true iff FUN is an instantiation of a constexpr function
3694 template. */
3696 static inline bool
3697 is_instantiation_of_constexpr (tree fun)
3699 return (DECL_TEMPLOID_INSTANTIATION (fun)
3700 && DECL_DECLARED_CONSTEXPR_P (DECL_TEMPLATE_RESULT
3701 (DECL_TI_TEMPLATE (fun))));
3704 /* Generate RTL for FN. */
3706 bool
3707 expand_or_defer_fn_1 (tree fn)
3709 /* When the parser calls us after finishing the body of a template
3710 function, we don't really want to expand the body. */
3711 if (processing_template_decl)
3713 /* Normally, collection only occurs in rest_of_compilation. So,
3714 if we don't collect here, we never collect junk generated
3715 during the processing of templates until we hit a
3716 non-template function. It's not safe to do this inside a
3717 nested class, though, as the parser may have local state that
3718 is not a GC root. */
3719 if (!function_depth)
3720 ggc_collect ();
3721 return false;
3724 gcc_assert (DECL_SAVED_TREE (fn));
3726 /* If this is a constructor or destructor body, we have to clone
3727 it. */
3728 if (maybe_clone_body (fn))
3730 /* We don't want to process FN again, so pretend we've written
3731 it out, even though we haven't. */
3732 TREE_ASM_WRITTEN (fn) = 1;
3733 /* If this is an instantiation of a constexpr function, keep
3734 DECL_SAVED_TREE for explain_invalid_constexpr_fn. */
3735 if (!is_instantiation_of_constexpr (fn))
3736 DECL_SAVED_TREE (fn) = NULL_TREE;
3737 return false;
3740 /* We make a decision about linkage for these functions at the end
3741 of the compilation. Until that point, we do not want the back
3742 end to output them -- but we do want it to see the bodies of
3743 these functions so that it can inline them as appropriate. */
3744 if (DECL_DECLARED_INLINE_P (fn) || DECL_IMPLICIT_INSTANTIATION (fn))
3746 if (DECL_INTERFACE_KNOWN (fn))
3747 /* We've already made a decision as to how this function will
3748 be handled. */;
3749 else if (!at_eof)
3751 DECL_EXTERNAL (fn) = 1;
3752 DECL_NOT_REALLY_EXTERN (fn) = 1;
3753 note_vague_linkage_fn (fn);
3754 /* A non-template inline function with external linkage will
3755 always be COMDAT. As we must eventually determine the
3756 linkage of all functions, and as that causes writes to
3757 the data mapped in from the PCH file, it's advantageous
3758 to mark the functions at this point. */
3759 if (!DECL_IMPLICIT_INSTANTIATION (fn))
3761 /* This function must have external linkage, as
3762 otherwise DECL_INTERFACE_KNOWN would have been
3763 set. */
3764 gcc_assert (TREE_PUBLIC (fn));
3765 comdat_linkage (fn);
3766 DECL_INTERFACE_KNOWN (fn) = 1;
3769 else
3770 import_export_decl (fn);
3772 /* If the user wants us to keep all inline functions, then mark
3773 this function as needed so that finish_file will make sure to
3774 output it later. Similarly, all dllexport'd functions must
3775 be emitted; there may be callers in other DLLs. */
3776 if ((flag_keep_inline_functions
3777 && DECL_DECLARED_INLINE_P (fn)
3778 && !DECL_REALLY_EXTERN (fn))
3779 || (flag_keep_inline_dllexport
3780 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (fn))))
3782 mark_needed (fn);
3783 DECL_EXTERNAL (fn) = 0;
3787 /* There's no reason to do any of the work here if we're only doing
3788 semantic analysis; this code just generates RTL. */
3789 if (flag_syntax_only)
3790 return false;
3792 return true;
3795 void
3796 expand_or_defer_fn (tree fn)
3798 if (expand_or_defer_fn_1 (fn))
3800 function_depth++;
3802 /* Expand or defer, at the whim of the compilation unit manager. */
3803 cgraph_finalize_function (fn, function_depth > 1);
3804 emit_associated_thunks (fn);
3806 function_depth--;
3810 struct nrv_data
3812 tree var;
3813 tree result;
3814 htab_t visited;
3817 /* Helper function for walk_tree, used by finalize_nrv below. */
3819 static tree
3820 finalize_nrv_r (tree* tp, int* walk_subtrees, void* data)
3822 struct nrv_data *dp = (struct nrv_data *)data;
3823 void **slot;
3825 /* No need to walk into types. There wouldn't be any need to walk into
3826 non-statements, except that we have to consider STMT_EXPRs. */
3827 if (TYPE_P (*tp))
3828 *walk_subtrees = 0;
3829 /* Change all returns to just refer to the RESULT_DECL; this is a nop,
3830 but differs from using NULL_TREE in that it indicates that we care
3831 about the value of the RESULT_DECL. */
3832 else if (TREE_CODE (*tp) == RETURN_EXPR)
3833 TREE_OPERAND (*tp, 0) = dp->result;
3834 /* Change all cleanups for the NRV to only run when an exception is
3835 thrown. */
3836 else if (TREE_CODE (*tp) == CLEANUP_STMT
3837 && CLEANUP_DECL (*tp) == dp->var)
3838 CLEANUP_EH_ONLY (*tp) = 1;
3839 /* Replace the DECL_EXPR for the NRV with an initialization of the
3840 RESULT_DECL, if needed. */
3841 else if (TREE_CODE (*tp) == DECL_EXPR
3842 && DECL_EXPR_DECL (*tp) == dp->var)
3844 tree init;
3845 if (DECL_INITIAL (dp->var)
3846 && DECL_INITIAL (dp->var) != error_mark_node)
3847 init = build2 (INIT_EXPR, void_type_node, dp->result,
3848 DECL_INITIAL (dp->var));
3849 else
3850 init = build_empty_stmt (EXPR_LOCATION (*tp));
3851 DECL_INITIAL (dp->var) = NULL_TREE;
3852 SET_EXPR_LOCATION (init, EXPR_LOCATION (*tp));
3853 *tp = init;
3855 /* And replace all uses of the NRV with the RESULT_DECL. */
3856 else if (*tp == dp->var)
3857 *tp = dp->result;
3859 /* Avoid walking into the same tree more than once. Unfortunately, we
3860 can't just use walk_tree_without duplicates because it would only call
3861 us for the first occurrence of dp->var in the function body. */
3862 slot = htab_find_slot (dp->visited, *tp, INSERT);
3863 if (*slot)
3864 *walk_subtrees = 0;
3865 else
3866 *slot = *tp;
3868 /* Keep iterating. */
3869 return NULL_TREE;
3872 /* Called from finish_function to implement the named return value
3873 optimization by overriding all the RETURN_EXPRs and pertinent
3874 CLEANUP_STMTs and replacing all occurrences of VAR with RESULT, the
3875 RESULT_DECL for the function. */
3877 void
3878 finalize_nrv (tree *tp, tree var, tree result)
3880 struct nrv_data data;
3882 /* Copy name from VAR to RESULT. */
3883 DECL_NAME (result) = DECL_NAME (var);
3884 /* Don't forget that we take its address. */
3885 TREE_ADDRESSABLE (result) = TREE_ADDRESSABLE (var);
3886 /* Finally set DECL_VALUE_EXPR to avoid assigning
3887 a stack slot at -O0 for the original var and debug info
3888 uses RESULT location for VAR. */
3889 SET_DECL_VALUE_EXPR (var, result);
3890 DECL_HAS_VALUE_EXPR_P (var) = 1;
3892 data.var = var;
3893 data.result = result;
3894 data.visited = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL);
3895 cp_walk_tree (tp, finalize_nrv_r, &data, 0);
3896 htab_delete (data.visited);
3899 /* Create CP_OMP_CLAUSE_INFO for clause C. Returns true if it is invalid. */
3901 bool
3902 cxx_omp_create_clause_info (tree c, tree type, bool need_default_ctor,
3903 bool need_copy_ctor, bool need_copy_assignment)
3905 int save_errorcount = errorcount;
3906 tree info, t;
3908 /* Always allocate 3 elements for simplicity. These are the
3909 function decls for the ctor, dtor, and assignment op.
3910 This layout is known to the three lang hooks,
3911 cxx_omp_clause_default_init, cxx_omp_clause_copy_init,
3912 and cxx_omp_clause_assign_op. */
3913 info = make_tree_vec (3);
3914 CP_OMP_CLAUSE_INFO (c) = info;
3916 if (need_default_ctor || need_copy_ctor)
3918 if (need_default_ctor)
3919 t = get_default_ctor (type);
3920 else
3921 t = get_copy_ctor (type, tf_warning_or_error);
3923 if (t && !trivial_fn_p (t))
3924 TREE_VEC_ELT (info, 0) = t;
3927 if ((need_default_ctor || need_copy_ctor)
3928 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
3929 TREE_VEC_ELT (info, 1) = get_dtor (type, tf_warning_or_error);
3931 if (need_copy_assignment)
3933 t = get_copy_assign (type);
3935 if (t && !trivial_fn_p (t))
3936 TREE_VEC_ELT (info, 2) = t;
3939 return errorcount != save_errorcount;
3942 /* For all elements of CLAUSES, validate them vs OpenMP constraints.
3943 Remove any elements from the list that are invalid. */
3945 tree
3946 finish_omp_clauses (tree clauses)
3948 bitmap_head generic_head, firstprivate_head, lastprivate_head;
3949 tree c, t, *pc = &clauses;
3950 const char *name;
3952 bitmap_obstack_initialize (NULL);
3953 bitmap_initialize (&generic_head, &bitmap_default_obstack);
3954 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
3955 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
3957 for (pc = &clauses, c = clauses; c ; c = *pc)
3959 bool remove = false;
3961 switch (OMP_CLAUSE_CODE (c))
3963 case OMP_CLAUSE_SHARED:
3964 name = "shared";
3965 goto check_dup_generic;
3966 case OMP_CLAUSE_PRIVATE:
3967 name = "private";
3968 goto check_dup_generic;
3969 case OMP_CLAUSE_REDUCTION:
3970 name = "reduction";
3971 goto check_dup_generic;
3972 case OMP_CLAUSE_COPYPRIVATE:
3973 name = "copyprivate";
3974 goto check_dup_generic;
3975 case OMP_CLAUSE_COPYIN:
3976 name = "copyin";
3977 goto check_dup_generic;
3978 check_dup_generic:
3979 t = OMP_CLAUSE_DECL (c);
3980 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
3982 if (processing_template_decl)
3983 break;
3984 if (DECL_P (t))
3985 error ("%qD is not a variable in clause %qs", t, name);
3986 else
3987 error ("%qE is not a variable in clause %qs", t, name);
3988 remove = true;
3990 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
3991 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
3992 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
3994 error ("%qD appears more than once in data clauses", t);
3995 remove = true;
3997 else
3998 bitmap_set_bit (&generic_head, DECL_UID (t));
3999 break;
4001 case OMP_CLAUSE_FIRSTPRIVATE:
4002 t = OMP_CLAUSE_DECL (c);
4003 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
4005 if (processing_template_decl)
4006 break;
4007 if (DECL_P (t))
4008 error ("%qD is not a variable in clause %<firstprivate%>", t);
4009 else
4010 error ("%qE is not a variable in clause %<firstprivate%>", t);
4011 remove = true;
4013 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
4014 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
4016 error ("%qD appears more than once in data clauses", t);
4017 remove = true;
4019 else
4020 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
4021 break;
4023 case OMP_CLAUSE_LASTPRIVATE:
4024 t = OMP_CLAUSE_DECL (c);
4025 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
4027 if (processing_template_decl)
4028 break;
4029 if (DECL_P (t))
4030 error ("%qD is not a variable in clause %<lastprivate%>", t);
4031 else
4032 error ("%qE is not a variable in clause %<lastprivate%>", t);
4033 remove = true;
4035 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
4036 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
4038 error ("%qD appears more than once in data clauses", t);
4039 remove = true;
4041 else
4042 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
4043 break;
4045 case OMP_CLAUSE_IF:
4046 t = OMP_CLAUSE_IF_EXPR (c);
4047 t = maybe_convert_cond (t);
4048 if (t == error_mark_node)
4049 remove = true;
4050 else if (!processing_template_decl)
4051 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
4052 OMP_CLAUSE_IF_EXPR (c) = t;
4053 break;
4055 case OMP_CLAUSE_FINAL:
4056 t = OMP_CLAUSE_FINAL_EXPR (c);
4057 t = maybe_convert_cond (t);
4058 if (t == error_mark_node)
4059 remove = true;
4060 else if (!processing_template_decl)
4061 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
4062 OMP_CLAUSE_FINAL_EXPR (c) = t;
4063 break;
4065 case OMP_CLAUSE_NUM_THREADS:
4066 t = OMP_CLAUSE_NUM_THREADS_EXPR (c);
4067 if (t == error_mark_node)
4068 remove = true;
4069 else if (!type_dependent_expression_p (t)
4070 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
4072 error ("num_threads expression must be integral");
4073 remove = true;
4075 else
4077 t = mark_rvalue_use (t);
4078 if (!processing_template_decl)
4079 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
4080 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
4082 break;
4084 case OMP_CLAUSE_SCHEDULE:
4085 t = OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c);
4086 if (t == NULL)
4088 else if (t == error_mark_node)
4089 remove = true;
4090 else if (!type_dependent_expression_p (t)
4091 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
4093 error ("schedule chunk size expression must be integral");
4094 remove = true;
4096 else
4098 t = mark_rvalue_use (t);
4099 if (!processing_template_decl)
4100 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
4101 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
4103 break;
4105 case OMP_CLAUSE_NOWAIT:
4106 case OMP_CLAUSE_ORDERED:
4107 case OMP_CLAUSE_DEFAULT:
4108 case OMP_CLAUSE_UNTIED:
4109 case OMP_CLAUSE_COLLAPSE:
4110 case OMP_CLAUSE_MERGEABLE:
4111 break;
4113 default:
4114 gcc_unreachable ();
4117 if (remove)
4118 *pc = OMP_CLAUSE_CHAIN (c);
4119 else
4120 pc = &OMP_CLAUSE_CHAIN (c);
4123 for (pc = &clauses, c = clauses; c ; c = *pc)
4125 enum omp_clause_code c_kind = OMP_CLAUSE_CODE (c);
4126 bool remove = false;
4127 bool need_complete_non_reference = false;
4128 bool need_default_ctor = false;
4129 bool need_copy_ctor = false;
4130 bool need_copy_assignment = false;
4131 bool need_implicitly_determined = false;
4132 tree type, inner_type;
4134 switch (c_kind)
4136 case OMP_CLAUSE_SHARED:
4137 name = "shared";
4138 need_implicitly_determined = true;
4139 break;
4140 case OMP_CLAUSE_PRIVATE:
4141 name = "private";
4142 need_complete_non_reference = true;
4143 need_default_ctor = true;
4144 need_implicitly_determined = true;
4145 break;
4146 case OMP_CLAUSE_FIRSTPRIVATE:
4147 name = "firstprivate";
4148 need_complete_non_reference = true;
4149 need_copy_ctor = true;
4150 need_implicitly_determined = true;
4151 break;
4152 case OMP_CLAUSE_LASTPRIVATE:
4153 name = "lastprivate";
4154 need_complete_non_reference = true;
4155 need_copy_assignment = true;
4156 need_implicitly_determined = true;
4157 break;
4158 case OMP_CLAUSE_REDUCTION:
4159 name = "reduction";
4160 need_implicitly_determined = true;
4161 break;
4162 case OMP_CLAUSE_COPYPRIVATE:
4163 name = "copyprivate";
4164 need_copy_assignment = true;
4165 break;
4166 case OMP_CLAUSE_COPYIN:
4167 name = "copyin";
4168 need_copy_assignment = true;
4169 break;
4170 default:
4171 pc = &OMP_CLAUSE_CHAIN (c);
4172 continue;
4175 t = OMP_CLAUSE_DECL (c);
4176 if (processing_template_decl
4177 && TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
4179 pc = &OMP_CLAUSE_CHAIN (c);
4180 continue;
4183 switch (c_kind)
4185 case OMP_CLAUSE_LASTPRIVATE:
4186 if (!bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
4187 need_default_ctor = true;
4188 break;
4190 case OMP_CLAUSE_REDUCTION:
4191 if (AGGREGATE_TYPE_P (TREE_TYPE (t))
4192 || POINTER_TYPE_P (TREE_TYPE (t)))
4194 error ("%qE has invalid type for %<reduction%>", t);
4195 remove = true;
4197 else if (FLOAT_TYPE_P (TREE_TYPE (t)))
4199 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
4200 switch (r_code)
4202 case PLUS_EXPR:
4203 case MULT_EXPR:
4204 case MINUS_EXPR:
4205 case MIN_EXPR:
4206 case MAX_EXPR:
4207 break;
4208 default:
4209 error ("%qE has invalid type for %<reduction(%s)%>",
4210 t, operator_name_info[r_code].name);
4211 remove = true;
4214 break;
4216 case OMP_CLAUSE_COPYIN:
4217 if (TREE_CODE (t) != VAR_DECL || !DECL_THREAD_LOCAL_P (t))
4219 error ("%qE must be %<threadprivate%> for %<copyin%>", t);
4220 remove = true;
4222 break;
4224 default:
4225 break;
4228 if (need_complete_non_reference || need_copy_assignment)
4230 t = require_complete_type (t);
4231 if (t == error_mark_node)
4232 remove = true;
4233 else if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE
4234 && need_complete_non_reference)
4236 error ("%qE has reference type for %qs", t, name);
4237 remove = true;
4240 if (need_implicitly_determined)
4242 const char *share_name = NULL;
4244 if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
4245 share_name = "threadprivate";
4246 else switch (cxx_omp_predetermined_sharing (t))
4248 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
4249 break;
4250 case OMP_CLAUSE_DEFAULT_SHARED:
4251 /* const vars may be specified in firstprivate clause. */
4252 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
4253 && cxx_omp_const_qual_no_mutable (t))
4254 break;
4255 share_name = "shared";
4256 break;
4257 case OMP_CLAUSE_DEFAULT_PRIVATE:
4258 share_name = "private";
4259 break;
4260 default:
4261 gcc_unreachable ();
4263 if (share_name)
4265 error ("%qE is predetermined %qs for %qs",
4266 t, share_name, name);
4267 remove = true;
4271 /* We're interested in the base element, not arrays. */
4272 inner_type = type = TREE_TYPE (t);
4273 while (TREE_CODE (inner_type) == ARRAY_TYPE)
4274 inner_type = TREE_TYPE (inner_type);
4276 /* Check for special function availability by building a call to one.
4277 Save the results, because later we won't be in the right context
4278 for making these queries. */
4279 if (CLASS_TYPE_P (inner_type)
4280 && COMPLETE_TYPE_P (inner_type)
4281 && (need_default_ctor || need_copy_ctor || need_copy_assignment)
4282 && !type_dependent_expression_p (t)
4283 && cxx_omp_create_clause_info (c, inner_type, need_default_ctor,
4284 need_copy_ctor, need_copy_assignment))
4285 remove = true;
4287 if (remove)
4288 *pc = OMP_CLAUSE_CHAIN (c);
4289 else
4290 pc = &OMP_CLAUSE_CHAIN (c);
4293 bitmap_obstack_release (NULL);
4294 return clauses;
4297 /* For all variables in the tree_list VARS, mark them as thread local. */
4299 void
4300 finish_omp_threadprivate (tree vars)
4302 tree t;
4304 /* Mark every variable in VARS to be assigned thread local storage. */
4305 for (t = vars; t; t = TREE_CHAIN (t))
4307 tree v = TREE_PURPOSE (t);
4309 if (error_operand_p (v))
4311 else if (TREE_CODE (v) != VAR_DECL)
4312 error ("%<threadprivate%> %qD is not file, namespace "
4313 "or block scope variable", v);
4314 /* If V had already been marked threadprivate, it doesn't matter
4315 whether it had been used prior to this point. */
4316 else if (TREE_USED (v)
4317 && (DECL_LANG_SPECIFIC (v) == NULL
4318 || !CP_DECL_THREADPRIVATE_P (v)))
4319 error ("%qE declared %<threadprivate%> after first use", v);
4320 else if (! TREE_STATIC (v) && ! DECL_EXTERNAL (v))
4321 error ("automatic variable %qE cannot be %<threadprivate%>", v);
4322 else if (! COMPLETE_TYPE_P (TREE_TYPE (v)))
4323 error ("%<threadprivate%> %qE has incomplete type", v);
4324 else if (TREE_STATIC (v) && TYPE_P (CP_DECL_CONTEXT (v))
4325 && CP_DECL_CONTEXT (v) != current_class_type)
4326 error ("%<threadprivate%> %qE directive not "
4327 "in %qT definition", v, CP_DECL_CONTEXT (v));
4328 else
4330 /* Allocate a LANG_SPECIFIC structure for V, if needed. */
4331 if (DECL_LANG_SPECIFIC (v) == NULL)
4333 retrofit_lang_decl (v);
4335 /* Make sure that DECL_DISCRIMINATOR_P continues to be true
4336 after the allocation of the lang_decl structure. */
4337 if (DECL_DISCRIMINATOR_P (v))
4338 DECL_LANG_SPECIFIC (v)->u.base.u2sel = 1;
4341 if (! DECL_THREAD_LOCAL_P (v))
4343 DECL_TLS_MODEL (v) = decl_default_tls_model (v);
4344 /* If rtl has been already set for this var, call
4345 make_decl_rtl once again, so that encode_section_info
4346 has a chance to look at the new decl flags. */
4347 if (DECL_RTL_SET_P (v))
4348 make_decl_rtl (v);
4350 CP_DECL_THREADPRIVATE_P (v) = 1;
4355 /* Build an OpenMP structured block. */
4357 tree
4358 begin_omp_structured_block (void)
4360 return do_pushlevel (sk_omp);
4363 tree
4364 finish_omp_structured_block (tree block)
4366 return do_poplevel (block);
4369 /* Similarly, except force the retention of the BLOCK. */
4371 tree
4372 begin_omp_parallel (void)
4374 keep_next_level (true);
4375 return begin_omp_structured_block ();
4378 tree
4379 finish_omp_parallel (tree clauses, tree body)
4381 tree stmt;
4383 body = finish_omp_structured_block (body);
4385 stmt = make_node (OMP_PARALLEL);
4386 TREE_TYPE (stmt) = void_type_node;
4387 OMP_PARALLEL_CLAUSES (stmt) = clauses;
4388 OMP_PARALLEL_BODY (stmt) = body;
4390 return add_stmt (stmt);
4393 tree
4394 begin_omp_task (void)
4396 keep_next_level (true);
4397 return begin_omp_structured_block ();
4400 tree
4401 finish_omp_task (tree clauses, tree body)
4403 tree stmt;
4405 body = finish_omp_structured_block (body);
4407 stmt = make_node (OMP_TASK);
4408 TREE_TYPE (stmt) = void_type_node;
4409 OMP_TASK_CLAUSES (stmt) = clauses;
4410 OMP_TASK_BODY (stmt) = body;
4412 return add_stmt (stmt);
4415 /* Helper function for finish_omp_for. Convert Ith random access iterator
4416 into integral iterator. Return FALSE if successful. */
4418 static bool
4419 handle_omp_for_class_iterator (int i, location_t locus, tree declv, tree initv,
4420 tree condv, tree incrv, tree *body,
4421 tree *pre_body, tree clauses)
4423 tree diff, iter_init, iter_incr = NULL, last;
4424 tree incr_var = NULL, orig_pre_body, orig_body, c;
4425 tree decl = TREE_VEC_ELT (declv, i);
4426 tree init = TREE_VEC_ELT (initv, i);
4427 tree cond = TREE_VEC_ELT (condv, i);
4428 tree incr = TREE_VEC_ELT (incrv, i);
4429 tree iter = decl;
4430 location_t elocus = locus;
4432 if (init && EXPR_HAS_LOCATION (init))
4433 elocus = EXPR_LOCATION (init);
4435 switch (TREE_CODE (cond))
4437 case GT_EXPR:
4438 case GE_EXPR:
4439 case LT_EXPR:
4440 case LE_EXPR:
4441 if (TREE_OPERAND (cond, 1) == iter)
4442 cond = build2 (swap_tree_comparison (TREE_CODE (cond)),
4443 TREE_TYPE (cond), iter, TREE_OPERAND (cond, 0));
4444 if (TREE_OPERAND (cond, 0) != iter)
4445 cond = error_mark_node;
4446 else
4448 tree tem = build_x_binary_op (EXPR_LOCATION (cond),
4449 TREE_CODE (cond),
4450 iter, ERROR_MARK,
4451 TREE_OPERAND (cond, 1), ERROR_MARK,
4452 NULL, tf_warning_or_error);
4453 if (error_operand_p (tem))
4454 return true;
4456 break;
4457 default:
4458 cond = error_mark_node;
4459 break;
4461 if (cond == error_mark_node)
4463 error_at (elocus, "invalid controlling predicate");
4464 return true;
4466 diff = build_x_binary_op (elocus, MINUS_EXPR, TREE_OPERAND (cond, 1),
4467 ERROR_MARK, iter, ERROR_MARK, NULL,
4468 tf_warning_or_error);
4469 if (error_operand_p (diff))
4470 return true;
4471 if (TREE_CODE (TREE_TYPE (diff)) != INTEGER_TYPE)
4473 error_at (elocus, "difference between %qE and %qD does not have integer type",
4474 TREE_OPERAND (cond, 1), iter);
4475 return true;
4478 switch (TREE_CODE (incr))
4480 case PREINCREMENT_EXPR:
4481 case PREDECREMENT_EXPR:
4482 case POSTINCREMENT_EXPR:
4483 case POSTDECREMENT_EXPR:
4484 if (TREE_OPERAND (incr, 0) != iter)
4486 incr = error_mark_node;
4487 break;
4489 iter_incr = build_x_unary_op (EXPR_LOCATION (incr),
4490 TREE_CODE (incr), iter,
4491 tf_warning_or_error);
4492 if (error_operand_p (iter_incr))
4493 return true;
4494 else if (TREE_CODE (incr) == PREINCREMENT_EXPR
4495 || TREE_CODE (incr) == POSTINCREMENT_EXPR)
4496 incr = integer_one_node;
4497 else
4498 incr = integer_minus_one_node;
4499 break;
4500 case MODIFY_EXPR:
4501 if (TREE_OPERAND (incr, 0) != iter)
4502 incr = error_mark_node;
4503 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
4504 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
4506 tree rhs = TREE_OPERAND (incr, 1);
4507 if (TREE_OPERAND (rhs, 0) == iter)
4509 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (rhs, 1)))
4510 != INTEGER_TYPE)
4511 incr = error_mark_node;
4512 else
4514 iter_incr = build_x_modify_expr (EXPR_LOCATION (rhs),
4515 iter, TREE_CODE (rhs),
4516 TREE_OPERAND (rhs, 1),
4517 tf_warning_or_error);
4518 if (error_operand_p (iter_incr))
4519 return true;
4520 incr = TREE_OPERAND (rhs, 1);
4521 incr = cp_convert (TREE_TYPE (diff), incr,
4522 tf_warning_or_error);
4523 if (TREE_CODE (rhs) == MINUS_EXPR)
4525 incr = build1 (NEGATE_EXPR, TREE_TYPE (diff), incr);
4526 incr = fold_if_not_in_template (incr);
4528 if (TREE_CODE (incr) != INTEGER_CST
4529 && (TREE_CODE (incr) != NOP_EXPR
4530 || (TREE_CODE (TREE_OPERAND (incr, 0))
4531 != INTEGER_CST)))
4532 iter_incr = NULL;
4535 else if (TREE_OPERAND (rhs, 1) == iter)
4537 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (rhs, 0))) != INTEGER_TYPE
4538 || TREE_CODE (rhs) != PLUS_EXPR)
4539 incr = error_mark_node;
4540 else
4542 iter_incr = build_x_binary_op (EXPR_LOCATION (rhs),
4543 PLUS_EXPR,
4544 TREE_OPERAND (rhs, 0),
4545 ERROR_MARK, iter,
4546 ERROR_MARK, NULL,
4547 tf_warning_or_error);
4548 if (error_operand_p (iter_incr))
4549 return true;
4550 iter_incr = build_x_modify_expr (EXPR_LOCATION (rhs),
4551 iter, NOP_EXPR,
4552 iter_incr,
4553 tf_warning_or_error);
4554 if (error_operand_p (iter_incr))
4555 return true;
4556 incr = TREE_OPERAND (rhs, 0);
4557 iter_incr = NULL;
4560 else
4561 incr = error_mark_node;
4563 else
4564 incr = error_mark_node;
4565 break;
4566 default:
4567 incr = error_mark_node;
4568 break;
4571 if (incr == error_mark_node)
4573 error_at (elocus, "invalid increment expression");
4574 return true;
4577 incr = cp_convert (TREE_TYPE (diff), incr, tf_warning_or_error);
4578 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
4579 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
4580 && OMP_CLAUSE_DECL (c) == iter)
4581 break;
4583 decl = create_temporary_var (TREE_TYPE (diff));
4584 pushdecl (decl);
4585 add_decl_expr (decl);
4586 last = create_temporary_var (TREE_TYPE (diff));
4587 pushdecl (last);
4588 add_decl_expr (last);
4589 if (c && iter_incr == NULL)
4591 incr_var = create_temporary_var (TREE_TYPE (diff));
4592 pushdecl (incr_var);
4593 add_decl_expr (incr_var);
4595 gcc_assert (stmts_are_full_exprs_p ());
4597 orig_pre_body = *pre_body;
4598 *pre_body = push_stmt_list ();
4599 if (orig_pre_body)
4600 add_stmt (orig_pre_body);
4601 if (init != NULL)
4602 finish_expr_stmt (build_x_modify_expr (elocus,
4603 iter, NOP_EXPR, init,
4604 tf_warning_or_error));
4605 init = build_int_cst (TREE_TYPE (diff), 0);
4606 if (c && iter_incr == NULL)
4608 finish_expr_stmt (build_x_modify_expr (elocus,
4609 incr_var, NOP_EXPR,
4610 incr, tf_warning_or_error));
4611 incr = incr_var;
4612 iter_incr = build_x_modify_expr (elocus,
4613 iter, PLUS_EXPR, incr,
4614 tf_warning_or_error);
4616 finish_expr_stmt (build_x_modify_expr (elocus,
4617 last, NOP_EXPR, init,
4618 tf_warning_or_error));
4619 *pre_body = pop_stmt_list (*pre_body);
4621 cond = cp_build_binary_op (elocus,
4622 TREE_CODE (cond), decl, diff,
4623 tf_warning_or_error);
4624 incr = build_modify_expr (elocus, decl, NULL_TREE, PLUS_EXPR,
4625 elocus, incr, NULL_TREE);
4627 orig_body = *body;
4628 *body = push_stmt_list ();
4629 iter_init = build2 (MINUS_EXPR, TREE_TYPE (diff), decl, last);
4630 iter_init = build_x_modify_expr (elocus,
4631 iter, PLUS_EXPR, iter_init,
4632 tf_warning_or_error);
4633 iter_init = build1 (NOP_EXPR, void_type_node, iter_init);
4634 finish_expr_stmt (iter_init);
4635 finish_expr_stmt (build_x_modify_expr (elocus,
4636 last, NOP_EXPR, decl,
4637 tf_warning_or_error));
4638 add_stmt (orig_body);
4639 *body = pop_stmt_list (*body);
4641 if (c)
4643 OMP_CLAUSE_LASTPRIVATE_STMT (c) = push_stmt_list ();
4644 finish_expr_stmt (iter_incr);
4645 OMP_CLAUSE_LASTPRIVATE_STMT (c)
4646 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (c));
4649 TREE_VEC_ELT (declv, i) = decl;
4650 TREE_VEC_ELT (initv, i) = init;
4651 TREE_VEC_ELT (condv, i) = cond;
4652 TREE_VEC_ELT (incrv, i) = incr;
4654 return false;
4657 /* Build and validate an OMP_FOR statement. CLAUSES, BODY, COND, INCR
4658 are directly for their associated operands in the statement. DECL
4659 and INIT are a combo; if DECL is NULL then INIT ought to be a
4660 MODIFY_EXPR, and the DECL should be extracted. PRE_BODY are
4661 optional statements that need to go before the loop into its
4662 sk_omp scope. */
4664 tree
4665 finish_omp_for (location_t locus, tree declv, tree initv, tree condv,
4666 tree incrv, tree body, tree pre_body, tree clauses)
4668 tree omp_for = NULL, orig_incr = NULL;
4669 tree decl, init, cond, incr;
4670 location_t elocus;
4671 int i;
4673 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (initv));
4674 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (condv));
4675 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (incrv));
4676 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
4678 decl = TREE_VEC_ELT (declv, i);
4679 init = TREE_VEC_ELT (initv, i);
4680 cond = TREE_VEC_ELT (condv, i);
4681 incr = TREE_VEC_ELT (incrv, i);
4682 elocus = locus;
4684 if (decl == NULL)
4686 if (init != NULL)
4687 switch (TREE_CODE (init))
4689 case MODIFY_EXPR:
4690 decl = TREE_OPERAND (init, 0);
4691 init = TREE_OPERAND (init, 1);
4692 break;
4693 case MODOP_EXPR:
4694 if (TREE_CODE (TREE_OPERAND (init, 1)) == NOP_EXPR)
4696 decl = TREE_OPERAND (init, 0);
4697 init = TREE_OPERAND (init, 2);
4699 break;
4700 default:
4701 break;
4704 if (decl == NULL)
4706 error_at (locus,
4707 "expected iteration declaration or initialization");
4708 return NULL;
4712 if (init && EXPR_HAS_LOCATION (init))
4713 elocus = EXPR_LOCATION (init);
4715 if (cond == NULL)
4717 error_at (elocus, "missing controlling predicate");
4718 return NULL;
4721 if (incr == NULL)
4723 error_at (elocus, "missing increment expression");
4724 return NULL;
4727 TREE_VEC_ELT (declv, i) = decl;
4728 TREE_VEC_ELT (initv, i) = init;
4731 if (dependent_omp_for_p (declv, initv, condv, incrv))
4733 tree stmt;
4735 stmt = make_node (OMP_FOR);
4737 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
4739 /* This is really just a place-holder. We'll be decomposing this
4740 again and going through the cp_build_modify_expr path below when
4741 we instantiate the thing. */
4742 TREE_VEC_ELT (initv, i)
4743 = build2 (MODIFY_EXPR, void_type_node, TREE_VEC_ELT (declv, i),
4744 TREE_VEC_ELT (initv, i));
4747 TREE_TYPE (stmt) = void_type_node;
4748 OMP_FOR_INIT (stmt) = initv;
4749 OMP_FOR_COND (stmt) = condv;
4750 OMP_FOR_INCR (stmt) = incrv;
4751 OMP_FOR_BODY (stmt) = body;
4752 OMP_FOR_PRE_BODY (stmt) = pre_body;
4753 OMP_FOR_CLAUSES (stmt) = clauses;
4755 SET_EXPR_LOCATION (stmt, locus);
4756 return add_stmt (stmt);
4759 if (processing_template_decl)
4760 orig_incr = make_tree_vec (TREE_VEC_LENGTH (incrv));
4762 for (i = 0; i < TREE_VEC_LENGTH (declv); )
4764 decl = TREE_VEC_ELT (declv, i);
4765 init = TREE_VEC_ELT (initv, i);
4766 cond = TREE_VEC_ELT (condv, i);
4767 incr = TREE_VEC_ELT (incrv, i);
4768 if (orig_incr)
4769 TREE_VEC_ELT (orig_incr, i) = incr;
4770 elocus = locus;
4772 if (init && EXPR_HAS_LOCATION (init))
4773 elocus = EXPR_LOCATION (init);
4775 if (!DECL_P (decl))
4777 error_at (elocus, "expected iteration declaration or initialization");
4778 return NULL;
4781 if (incr && TREE_CODE (incr) == MODOP_EXPR)
4783 if (orig_incr)
4784 TREE_VEC_ELT (orig_incr, i) = incr;
4785 incr = cp_build_modify_expr (TREE_OPERAND (incr, 0),
4786 TREE_CODE (TREE_OPERAND (incr, 1)),
4787 TREE_OPERAND (incr, 2),
4788 tf_warning_or_error);
4791 if (CLASS_TYPE_P (TREE_TYPE (decl)))
4793 if (handle_omp_for_class_iterator (i, locus, declv, initv, condv,
4794 incrv, &body, &pre_body, clauses))
4795 return NULL;
4796 continue;
4799 if (!INTEGRAL_TYPE_P (TREE_TYPE (decl))
4800 && TREE_CODE (TREE_TYPE (decl)) != POINTER_TYPE)
4802 error_at (elocus, "invalid type for iteration variable %qE", decl);
4803 return NULL;
4806 if (!processing_template_decl)
4808 init = fold_build_cleanup_point_expr (TREE_TYPE (init), init);
4809 init = cp_build_modify_expr (decl, NOP_EXPR, init, tf_warning_or_error);
4811 else
4812 init = build2 (MODIFY_EXPR, void_type_node, decl, init);
4813 if (cond
4814 && TREE_SIDE_EFFECTS (cond)
4815 && COMPARISON_CLASS_P (cond)
4816 && !processing_template_decl)
4818 tree t = TREE_OPERAND (cond, 0);
4819 if (TREE_SIDE_EFFECTS (t)
4820 && t != decl
4821 && (TREE_CODE (t) != NOP_EXPR
4822 || TREE_OPERAND (t, 0) != decl))
4823 TREE_OPERAND (cond, 0)
4824 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
4826 t = TREE_OPERAND (cond, 1);
4827 if (TREE_SIDE_EFFECTS (t)
4828 && t != decl
4829 && (TREE_CODE (t) != NOP_EXPR
4830 || TREE_OPERAND (t, 0) != decl))
4831 TREE_OPERAND (cond, 1)
4832 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
4834 if (decl == error_mark_node || init == error_mark_node)
4835 return NULL;
4837 TREE_VEC_ELT (declv, i) = decl;
4838 TREE_VEC_ELT (initv, i) = init;
4839 TREE_VEC_ELT (condv, i) = cond;
4840 TREE_VEC_ELT (incrv, i) = incr;
4841 i++;
4844 if (IS_EMPTY_STMT (pre_body))
4845 pre_body = NULL;
4847 omp_for = c_finish_omp_for (locus, declv, initv, condv, incrv,
4848 body, pre_body);
4850 if (omp_for == NULL)
4851 return NULL;
4853 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INCR (omp_for)); i++)
4855 decl = TREE_OPERAND (TREE_VEC_ELT (OMP_FOR_INIT (omp_for), i), 0);
4856 incr = TREE_VEC_ELT (OMP_FOR_INCR (omp_for), i);
4858 if (TREE_CODE (incr) != MODIFY_EXPR)
4859 continue;
4861 if (TREE_SIDE_EFFECTS (TREE_OPERAND (incr, 1))
4862 && BINARY_CLASS_P (TREE_OPERAND (incr, 1))
4863 && !processing_template_decl)
4865 tree t = TREE_OPERAND (TREE_OPERAND (incr, 1), 0);
4866 if (TREE_SIDE_EFFECTS (t)
4867 && t != decl
4868 && (TREE_CODE (t) != NOP_EXPR
4869 || TREE_OPERAND (t, 0) != decl))
4870 TREE_OPERAND (TREE_OPERAND (incr, 1), 0)
4871 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
4873 t = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
4874 if (TREE_SIDE_EFFECTS (t)
4875 && t != decl
4876 && (TREE_CODE (t) != NOP_EXPR
4877 || TREE_OPERAND (t, 0) != decl))
4878 TREE_OPERAND (TREE_OPERAND (incr, 1), 1)
4879 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
4882 if (orig_incr)
4883 TREE_VEC_ELT (OMP_FOR_INCR (omp_for), i) = TREE_VEC_ELT (orig_incr, i);
4885 if (omp_for != NULL)
4886 OMP_FOR_CLAUSES (omp_for) = clauses;
4887 return omp_for;
4890 void
4891 finish_omp_atomic (enum tree_code code, enum tree_code opcode, tree lhs,
4892 tree rhs, tree v, tree lhs1, tree rhs1)
4894 tree orig_lhs;
4895 tree orig_rhs;
4896 tree orig_v;
4897 tree orig_lhs1;
4898 tree orig_rhs1;
4899 bool dependent_p;
4900 tree stmt;
4902 orig_lhs = lhs;
4903 orig_rhs = rhs;
4904 orig_v = v;
4905 orig_lhs1 = lhs1;
4906 orig_rhs1 = rhs1;
4907 dependent_p = false;
4908 stmt = NULL_TREE;
4910 /* Even in a template, we can detect invalid uses of the atomic
4911 pragma if neither LHS nor RHS is type-dependent. */
4912 if (processing_template_decl)
4914 dependent_p = (type_dependent_expression_p (lhs)
4915 || (rhs && type_dependent_expression_p (rhs))
4916 || (v && type_dependent_expression_p (v))
4917 || (lhs1 && type_dependent_expression_p (lhs1))
4918 || (rhs1 && type_dependent_expression_p (rhs1)));
4919 if (!dependent_p)
4921 lhs = build_non_dependent_expr (lhs);
4922 if (rhs)
4923 rhs = build_non_dependent_expr (rhs);
4924 if (v)
4925 v = build_non_dependent_expr (v);
4926 if (lhs1)
4927 lhs1 = build_non_dependent_expr (lhs1);
4928 if (rhs1)
4929 rhs1 = build_non_dependent_expr (rhs1);
4932 if (!dependent_p)
4934 stmt = c_finish_omp_atomic (input_location, code, opcode, lhs, rhs,
4935 v, lhs1, rhs1);
4936 if (stmt == error_mark_node)
4937 return;
4939 if (processing_template_decl)
4941 if (code == OMP_ATOMIC_READ)
4943 stmt = build_min_nt_loc (EXPR_LOCATION (orig_lhs),
4944 OMP_ATOMIC_READ, orig_lhs);
4945 stmt = build2 (MODIFY_EXPR, void_type_node, orig_v, stmt);
4947 else
4949 if (opcode == NOP_EXPR)
4950 stmt = build2 (MODIFY_EXPR, void_type_node, orig_lhs, orig_rhs);
4951 else
4952 stmt = build2 (opcode, void_type_node, orig_lhs, orig_rhs);
4953 if (orig_rhs1)
4954 stmt = build_min_nt_loc (EXPR_LOCATION (orig_rhs1),
4955 COMPOUND_EXPR, orig_rhs1, stmt);
4956 if (code != OMP_ATOMIC)
4958 stmt = build_min_nt_loc (EXPR_LOCATION (orig_lhs1),
4959 code, orig_lhs1, stmt);
4960 stmt = build2 (MODIFY_EXPR, void_type_node, orig_v, stmt);
4963 stmt = build2 (OMP_ATOMIC, void_type_node, integer_zero_node, stmt);
4965 add_stmt (stmt);
4968 void
4969 finish_omp_barrier (void)
4971 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_BARRIER);
4972 VEC(tree,gc) *vec = make_tree_vector ();
4973 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
4974 release_tree_vector (vec);
4975 finish_expr_stmt (stmt);
4978 void
4979 finish_omp_flush (void)
4981 tree fn = builtin_decl_explicit (BUILT_IN_SYNC_SYNCHRONIZE);
4982 VEC(tree,gc) *vec = make_tree_vector ();
4983 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
4984 release_tree_vector (vec);
4985 finish_expr_stmt (stmt);
4988 void
4989 finish_omp_taskwait (void)
4991 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_TASKWAIT);
4992 VEC(tree,gc) *vec = make_tree_vector ();
4993 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
4994 release_tree_vector (vec);
4995 finish_expr_stmt (stmt);
4998 void
4999 finish_omp_taskyield (void)
5001 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_TASKYIELD);
5002 VEC(tree,gc) *vec = make_tree_vector ();
5003 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
5004 release_tree_vector (vec);
5005 finish_expr_stmt (stmt);
5008 /* Begin a __transaction_atomic or __transaction_relaxed statement.
5009 If PCOMPOUND is non-null, this is for a function-transaction-block, and we
5010 should create an extra compound stmt. */
5012 tree
5013 begin_transaction_stmt (location_t loc, tree *pcompound, int flags)
5015 tree r;
5017 if (pcompound)
5018 *pcompound = begin_compound_stmt (0);
5020 r = build_stmt (loc, TRANSACTION_EXPR, NULL_TREE);
5022 /* Only add the statement to the function if support enabled. */
5023 if (flag_tm)
5024 add_stmt (r);
5025 else
5026 error_at (loc, ((flags & TM_STMT_ATTR_RELAXED) != 0
5027 ? G_("%<__transaction_relaxed%> without "
5028 "transactional memory support enabled")
5029 : G_("%<__transaction_atomic%> without "
5030 "transactional memory support enabled")));
5032 TRANSACTION_EXPR_BODY (r) = push_stmt_list ();
5033 return r;
5036 /* End a __transaction_atomic or __transaction_relaxed statement.
5037 If COMPOUND_STMT is non-null, this is for a function-transaction-block,
5038 and we should end the compound. If NOEX is non-NULL, we wrap the body in
5039 a MUST_NOT_THROW_EXPR with NOEX as condition. */
5041 void
5042 finish_transaction_stmt (tree stmt, tree compound_stmt, int flags, tree noex)
5044 TRANSACTION_EXPR_BODY (stmt) = pop_stmt_list (TRANSACTION_EXPR_BODY (stmt));
5045 TRANSACTION_EXPR_OUTER (stmt) = (flags & TM_STMT_ATTR_OUTER) != 0;
5046 TRANSACTION_EXPR_RELAXED (stmt) = (flags & TM_STMT_ATTR_RELAXED) != 0;
5047 TRANSACTION_EXPR_IS_STMT (stmt) = 1;
5049 /* noexcept specifications are not allowed for function transactions. */
5050 gcc_assert (!(noex && compound_stmt));
5051 if (noex)
5053 tree body = build_must_not_throw_expr (TRANSACTION_EXPR_BODY (stmt),
5054 noex);
5055 SET_EXPR_LOCATION (body, EXPR_LOCATION (TRANSACTION_EXPR_BODY (stmt)));
5056 TREE_SIDE_EFFECTS (body) = 1;
5057 TRANSACTION_EXPR_BODY (stmt) = body;
5060 if (compound_stmt)
5061 finish_compound_stmt (compound_stmt);
5062 finish_stmt ();
5065 /* Build a __transaction_atomic or __transaction_relaxed expression. If
5066 NOEX is non-NULL, we wrap the body in a MUST_NOT_THROW_EXPR with NOEX as
5067 condition. */
5069 tree
5070 build_transaction_expr (location_t loc, tree expr, int flags, tree noex)
5072 tree ret;
5073 if (noex)
5075 expr = build_must_not_throw_expr (expr, noex);
5076 SET_EXPR_LOCATION (expr, loc);
5077 TREE_SIDE_EFFECTS (expr) = 1;
5079 ret = build1 (TRANSACTION_EXPR, TREE_TYPE (expr), expr);
5080 if (flags & TM_STMT_ATTR_RELAXED)
5081 TRANSACTION_EXPR_RELAXED (ret) = 1;
5082 SET_EXPR_LOCATION (ret, loc);
5083 return ret;
5086 void
5087 init_cp_semantics (void)
5091 /* Build a STATIC_ASSERT for a static assertion with the condition
5092 CONDITION and the message text MESSAGE. LOCATION is the location
5093 of the static assertion in the source code. When MEMBER_P, this
5094 static assertion is a member of a class. */
5095 void
5096 finish_static_assert (tree condition, tree message, location_t location,
5097 bool member_p)
5099 if (message == NULL_TREE
5100 || message == error_mark_node
5101 || condition == NULL_TREE
5102 || condition == error_mark_node)
5103 return;
5105 if (check_for_bare_parameter_packs (condition))
5106 condition = error_mark_node;
5108 if (type_dependent_expression_p (condition)
5109 || value_dependent_expression_p (condition))
5111 /* We're in a template; build a STATIC_ASSERT and put it in
5112 the right place. */
5113 tree assertion;
5115 assertion = make_node (STATIC_ASSERT);
5116 STATIC_ASSERT_CONDITION (assertion) = condition;
5117 STATIC_ASSERT_MESSAGE (assertion) = message;
5118 STATIC_ASSERT_SOURCE_LOCATION (assertion) = location;
5120 if (member_p)
5121 maybe_add_class_template_decl_list (current_class_type,
5122 assertion,
5123 /*friend_p=*/0);
5124 else
5125 add_stmt (assertion);
5127 return;
5130 /* Fold the expression and convert it to a boolean value. */
5131 condition = fold_non_dependent_expr (condition);
5132 condition = cp_convert (boolean_type_node, condition, tf_warning_or_error);
5133 condition = maybe_constant_value (condition);
5135 if (TREE_CODE (condition) == INTEGER_CST && !integer_zerop (condition))
5136 /* Do nothing; the condition is satisfied. */
5138 else
5140 location_t saved_loc = input_location;
5142 input_location = location;
5143 if (TREE_CODE (condition) == INTEGER_CST
5144 && integer_zerop (condition))
5145 /* Report the error. */
5146 error ("static assertion failed: %s", TREE_STRING_POINTER (message));
5147 else if (condition && condition != error_mark_node)
5149 error ("non-constant condition for static assertion");
5150 cxx_constant_value (condition);
5152 input_location = saved_loc;
5156 /* Implements the C++0x decltype keyword. Returns the type of EXPR,
5157 suitable for use as a type-specifier.
5159 ID_EXPRESSION_OR_MEMBER_ACCESS_P is true when EXPR was parsed as an
5160 id-expression or a class member access, FALSE when it was parsed as
5161 a full expression. */
5163 tree
5164 finish_decltype_type (tree expr, bool id_expression_or_member_access_p,
5165 tsubst_flags_t complain)
5167 tree type = NULL_TREE;
5169 if (!expr || error_operand_p (expr))
5170 return error_mark_node;
5172 if (TYPE_P (expr)
5173 || TREE_CODE (expr) == TYPE_DECL
5174 || (TREE_CODE (expr) == BIT_NOT_EXPR
5175 && TYPE_P (TREE_OPERAND (expr, 0))))
5177 if (complain & tf_error)
5178 error ("argument to decltype must be an expression");
5179 return error_mark_node;
5182 /* Depending on the resolution of DR 1172, we may later need to distinguish
5183 instantiation-dependent but not type-dependent expressions so that, say,
5184 A<decltype(sizeof(T))>::U doesn't require 'typename'. */
5185 if (instantiation_dependent_expression_p (expr))
5187 type = cxx_make_type (DECLTYPE_TYPE);
5188 DECLTYPE_TYPE_EXPR (type) = expr;
5189 DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (type)
5190 = id_expression_or_member_access_p;
5191 SET_TYPE_STRUCTURAL_EQUALITY (type);
5193 return type;
5196 /* The type denoted by decltype(e) is defined as follows: */
5198 expr = resolve_nondeduced_context (expr);
5200 if (type_unknown_p (expr))
5202 if (complain & tf_error)
5203 error ("decltype cannot resolve address of overloaded function");
5204 return error_mark_node;
5207 if (invalid_nonstatic_memfn_p (expr, complain))
5208 return error_mark_node;
5210 /* To get the size of a static data member declared as an array of
5211 unknown bound, we need to instantiate it. */
5212 if (TREE_CODE (expr) == VAR_DECL
5213 && VAR_HAD_UNKNOWN_BOUND (expr)
5214 && DECL_TEMPLATE_INSTANTIATION (expr))
5215 instantiate_decl (expr, /*defer_ok*/true, /*expl_inst_mem*/false);
5217 if (id_expression_or_member_access_p)
5219 /* If e is an id-expression or a class member access (5.2.5
5220 [expr.ref]), decltype(e) is defined as the type of the entity
5221 named by e. If there is no such entity, or e names a set of
5222 overloaded functions, the program is ill-formed. */
5223 if (TREE_CODE (expr) == IDENTIFIER_NODE)
5224 expr = lookup_name (expr);
5226 if (TREE_CODE (expr) == INDIRECT_REF)
5227 /* This can happen when the expression is, e.g., "a.b". Just
5228 look at the underlying operand. */
5229 expr = TREE_OPERAND (expr, 0);
5231 if (TREE_CODE (expr) == OFFSET_REF
5232 || TREE_CODE (expr) == MEMBER_REF)
5233 /* We're only interested in the field itself. If it is a
5234 BASELINK, we will need to see through it in the next
5235 step. */
5236 expr = TREE_OPERAND (expr, 1);
5238 if (BASELINK_P (expr))
5239 /* See through BASELINK nodes to the underlying function. */
5240 expr = BASELINK_FUNCTIONS (expr);
5242 switch (TREE_CODE (expr))
5244 case FIELD_DECL:
5245 if (DECL_BIT_FIELD_TYPE (expr))
5247 type = DECL_BIT_FIELD_TYPE (expr);
5248 break;
5250 /* Fall through for fields that aren't bitfields. */
5252 case FUNCTION_DECL:
5253 case VAR_DECL:
5254 case CONST_DECL:
5255 case PARM_DECL:
5256 case RESULT_DECL:
5257 case TEMPLATE_PARM_INDEX:
5258 expr = mark_type_use (expr);
5259 type = TREE_TYPE (expr);
5260 break;
5262 case ERROR_MARK:
5263 type = error_mark_node;
5264 break;
5266 case COMPONENT_REF:
5267 mark_type_use (expr);
5268 type = is_bitfield_expr_with_lowered_type (expr);
5269 if (!type)
5270 type = TREE_TYPE (TREE_OPERAND (expr, 1));
5271 break;
5273 case BIT_FIELD_REF:
5274 gcc_unreachable ();
5276 case INTEGER_CST:
5277 case PTRMEM_CST:
5278 /* We can get here when the id-expression refers to an
5279 enumerator or non-type template parameter. */
5280 type = TREE_TYPE (expr);
5281 break;
5283 default:
5284 gcc_unreachable ();
5285 return error_mark_node;
5288 else
5290 /* Within a lambda-expression:
5292 Every occurrence of decltype((x)) where x is a possibly
5293 parenthesized id-expression that names an entity of
5294 automatic storage duration is treated as if x were
5295 transformed into an access to a corresponding data member
5296 of the closure type that would have been declared if x
5297 were a use of the denoted entity. */
5298 if (outer_automatic_var_p (expr)
5299 && current_function_decl
5300 && LAMBDA_FUNCTION_P (current_function_decl))
5301 type = capture_decltype (expr);
5302 else if (error_operand_p (expr))
5303 type = error_mark_node;
5304 else if (expr == current_class_ptr)
5305 /* If the expression is just "this", we want the
5306 cv-unqualified pointer for the "this" type. */
5307 type = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
5308 else
5310 /* Otherwise, where T is the type of e, if e is an lvalue,
5311 decltype(e) is defined as T&; if an xvalue, T&&; otherwise, T. */
5312 cp_lvalue_kind clk = lvalue_kind (expr);
5313 type = unlowered_expr_type (expr);
5314 gcc_assert (TREE_CODE (type) != REFERENCE_TYPE);
5316 /* For vector types, pick a non-opaque variant. */
5317 if (TREE_CODE (type) == VECTOR_TYPE)
5318 type = strip_typedefs (type);
5320 if (clk != clk_none && !(clk & clk_class))
5321 type = cp_build_reference_type (type, (clk & clk_rvalueref));
5325 return type;
5328 /* Called from trait_expr_value to evaluate either __has_nothrow_assign or
5329 __has_nothrow_copy, depending on assign_p. */
5331 static bool
5332 classtype_has_nothrow_assign_or_copy_p (tree type, bool assign_p)
5334 tree fns;
5336 if (assign_p)
5338 int ix;
5339 ix = lookup_fnfields_1 (type, ansi_assopname (NOP_EXPR));
5340 if (ix < 0)
5341 return false;
5342 fns = VEC_index (tree, CLASSTYPE_METHOD_VEC (type), ix);
5344 else if (TYPE_HAS_COPY_CTOR (type))
5346 /* If construction of the copy constructor was postponed, create
5347 it now. */
5348 if (CLASSTYPE_LAZY_COPY_CTOR (type))
5349 lazily_declare_fn (sfk_copy_constructor, type);
5350 if (CLASSTYPE_LAZY_MOVE_CTOR (type))
5351 lazily_declare_fn (sfk_move_constructor, type);
5352 fns = CLASSTYPE_CONSTRUCTORS (type);
5354 else
5355 return false;
5357 for (; fns; fns = OVL_NEXT (fns))
5359 tree fn = OVL_CURRENT (fns);
5361 if (assign_p)
5363 if (copy_fn_p (fn) == 0)
5364 continue;
5366 else if (copy_fn_p (fn) <= 0)
5367 continue;
5369 if (!TYPE_NOTHROW_P (TREE_TYPE (fn)))
5370 return false;
5373 return true;
5376 /* Actually evaluates the trait. */
5378 static bool
5379 trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
5381 enum tree_code type_code1;
5382 tree t;
5384 type_code1 = TREE_CODE (type1);
5386 switch (kind)
5388 case CPTK_HAS_NOTHROW_ASSIGN:
5389 type1 = strip_array_types (type1);
5390 return (!CP_TYPE_CONST_P (type1) && type_code1 != REFERENCE_TYPE
5391 && (trait_expr_value (CPTK_HAS_TRIVIAL_ASSIGN, type1, type2)
5392 || (CLASS_TYPE_P (type1)
5393 && classtype_has_nothrow_assign_or_copy_p (type1,
5394 true))));
5396 case CPTK_HAS_TRIVIAL_ASSIGN:
5397 /* ??? The standard seems to be missing the "or array of such a class
5398 type" wording for this trait. */
5399 type1 = strip_array_types (type1);
5400 return (!CP_TYPE_CONST_P (type1) && type_code1 != REFERENCE_TYPE
5401 && (trivial_type_p (type1)
5402 || (CLASS_TYPE_P (type1)
5403 && TYPE_HAS_TRIVIAL_COPY_ASSIGN (type1))));
5405 case CPTK_HAS_NOTHROW_CONSTRUCTOR:
5406 type1 = strip_array_types (type1);
5407 return (trait_expr_value (CPTK_HAS_TRIVIAL_CONSTRUCTOR, type1, type2)
5408 || (CLASS_TYPE_P (type1)
5409 && (t = locate_ctor (type1))
5410 && TYPE_NOTHROW_P (TREE_TYPE (t))));
5412 case CPTK_HAS_TRIVIAL_CONSTRUCTOR:
5413 type1 = strip_array_types (type1);
5414 return (trivial_type_p (type1)
5415 || (CLASS_TYPE_P (type1) && TYPE_HAS_TRIVIAL_DFLT (type1)));
5417 case CPTK_HAS_NOTHROW_COPY:
5418 type1 = strip_array_types (type1);
5419 return (trait_expr_value (CPTK_HAS_TRIVIAL_COPY, type1, type2)
5420 || (CLASS_TYPE_P (type1)
5421 && classtype_has_nothrow_assign_or_copy_p (type1, false)));
5423 case CPTK_HAS_TRIVIAL_COPY:
5424 /* ??? The standard seems to be missing the "or array of such a class
5425 type" wording for this trait. */
5426 type1 = strip_array_types (type1);
5427 return (trivial_type_p (type1) || type_code1 == REFERENCE_TYPE
5428 || (CLASS_TYPE_P (type1) && TYPE_HAS_TRIVIAL_COPY_CTOR (type1)));
5430 case CPTK_HAS_TRIVIAL_DESTRUCTOR:
5431 type1 = strip_array_types (type1);
5432 return (trivial_type_p (type1) || type_code1 == REFERENCE_TYPE
5433 || (CLASS_TYPE_P (type1)
5434 && TYPE_HAS_TRIVIAL_DESTRUCTOR (type1)));
5436 case CPTK_HAS_VIRTUAL_DESTRUCTOR:
5437 return type_has_virtual_destructor (type1);
5439 case CPTK_IS_ABSTRACT:
5440 return (ABSTRACT_CLASS_TYPE_P (type1));
5442 case CPTK_IS_BASE_OF:
5443 return (NON_UNION_CLASS_TYPE_P (type1) && NON_UNION_CLASS_TYPE_P (type2)
5444 && DERIVED_FROM_P (type1, type2));
5446 case CPTK_IS_CLASS:
5447 return (NON_UNION_CLASS_TYPE_P (type1));
5449 case CPTK_IS_CONVERTIBLE_TO:
5450 /* TODO */
5451 return false;
5453 case CPTK_IS_EMPTY:
5454 return (NON_UNION_CLASS_TYPE_P (type1) && CLASSTYPE_EMPTY_P (type1));
5456 case CPTK_IS_ENUM:
5457 return (type_code1 == ENUMERAL_TYPE);
5459 case CPTK_IS_FINAL:
5460 return (CLASS_TYPE_P (type1) && CLASSTYPE_FINAL (type1));
5462 case CPTK_IS_LITERAL_TYPE:
5463 return (literal_type_p (type1));
5465 case CPTK_IS_POD:
5466 return (pod_type_p (type1));
5468 case CPTK_IS_POLYMORPHIC:
5469 return (CLASS_TYPE_P (type1) && TYPE_POLYMORPHIC_P (type1));
5471 case CPTK_IS_STD_LAYOUT:
5472 return (std_layout_type_p (type1));
5474 case CPTK_IS_TRIVIAL:
5475 return (trivial_type_p (type1));
5477 case CPTK_IS_UNION:
5478 return (type_code1 == UNION_TYPE);
5480 default:
5481 gcc_unreachable ();
5482 return false;
5486 /* If TYPE is an array of unknown bound, or (possibly cv-qualified)
5487 void, or a complete type, returns it, otherwise NULL_TREE. */
5489 static tree
5490 check_trait_type (tree type)
5492 if (TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type)
5493 && COMPLETE_TYPE_P (TREE_TYPE (type)))
5494 return type;
5496 if (VOID_TYPE_P (type))
5497 return type;
5499 return complete_type_or_else (strip_array_types (type), NULL_TREE);
5502 /* Process a trait expression. */
5504 tree
5505 finish_trait_expr (cp_trait_kind kind, tree type1, tree type2)
5507 gcc_assert (kind == CPTK_HAS_NOTHROW_ASSIGN
5508 || kind == CPTK_HAS_NOTHROW_CONSTRUCTOR
5509 || kind == CPTK_HAS_NOTHROW_COPY
5510 || kind == CPTK_HAS_TRIVIAL_ASSIGN
5511 || kind == CPTK_HAS_TRIVIAL_CONSTRUCTOR
5512 || kind == CPTK_HAS_TRIVIAL_COPY
5513 || kind == CPTK_HAS_TRIVIAL_DESTRUCTOR
5514 || kind == CPTK_HAS_VIRTUAL_DESTRUCTOR
5515 || kind == CPTK_IS_ABSTRACT
5516 || kind == CPTK_IS_BASE_OF
5517 || kind == CPTK_IS_CLASS
5518 || kind == CPTK_IS_CONVERTIBLE_TO
5519 || kind == CPTK_IS_EMPTY
5520 || kind == CPTK_IS_ENUM
5521 || kind == CPTK_IS_FINAL
5522 || kind == CPTK_IS_LITERAL_TYPE
5523 || kind == CPTK_IS_POD
5524 || kind == CPTK_IS_POLYMORPHIC
5525 || kind == CPTK_IS_STD_LAYOUT
5526 || kind == CPTK_IS_TRIVIAL
5527 || kind == CPTK_IS_UNION);
5529 if (kind == CPTK_IS_CONVERTIBLE_TO)
5531 sorry ("__is_convertible_to");
5532 return error_mark_node;
5535 if (type1 == error_mark_node
5536 || ((kind == CPTK_IS_BASE_OF || kind == CPTK_IS_CONVERTIBLE_TO)
5537 && type2 == error_mark_node))
5538 return error_mark_node;
5540 if (processing_template_decl)
5542 tree trait_expr = make_node (TRAIT_EXPR);
5543 TREE_TYPE (trait_expr) = boolean_type_node;
5544 TRAIT_EXPR_TYPE1 (trait_expr) = type1;
5545 TRAIT_EXPR_TYPE2 (trait_expr) = type2;
5546 TRAIT_EXPR_KIND (trait_expr) = kind;
5547 return trait_expr;
5550 switch (kind)
5552 case CPTK_HAS_NOTHROW_ASSIGN:
5553 case CPTK_HAS_TRIVIAL_ASSIGN:
5554 case CPTK_HAS_NOTHROW_CONSTRUCTOR:
5555 case CPTK_HAS_TRIVIAL_CONSTRUCTOR:
5556 case CPTK_HAS_NOTHROW_COPY:
5557 case CPTK_HAS_TRIVIAL_COPY:
5558 case CPTK_HAS_TRIVIAL_DESTRUCTOR:
5559 case CPTK_HAS_VIRTUAL_DESTRUCTOR:
5560 case CPTK_IS_ABSTRACT:
5561 case CPTK_IS_EMPTY:
5562 case CPTK_IS_FINAL:
5563 case CPTK_IS_LITERAL_TYPE:
5564 case CPTK_IS_POD:
5565 case CPTK_IS_POLYMORPHIC:
5566 case CPTK_IS_STD_LAYOUT:
5567 case CPTK_IS_TRIVIAL:
5568 if (!check_trait_type (type1))
5569 return error_mark_node;
5570 break;
5572 case CPTK_IS_BASE_OF:
5573 if (NON_UNION_CLASS_TYPE_P (type1) && NON_UNION_CLASS_TYPE_P (type2)
5574 && !same_type_ignoring_top_level_qualifiers_p (type1, type2)
5575 && !complete_type_or_else (type2, NULL_TREE))
5576 /* We already issued an error. */
5577 return error_mark_node;
5578 break;
5580 case CPTK_IS_CLASS:
5581 case CPTK_IS_ENUM:
5582 case CPTK_IS_UNION:
5583 break;
5585 case CPTK_IS_CONVERTIBLE_TO:
5586 default:
5587 gcc_unreachable ();
5590 return (trait_expr_value (kind, type1, type2)
5591 ? boolean_true_node : boolean_false_node);
5594 /* Do-nothing variants of functions to handle pragma FLOAT_CONST_DECIMAL64,
5595 which is ignored for C++. */
5597 void
5598 set_float_const_decimal64 (void)
5602 void
5603 clear_float_const_decimal64 (void)
5607 bool
5608 float_const_decimal64_p (void)
5610 return 0;
5614 /* Return true if T is a literal type. */
5616 bool
5617 literal_type_p (tree t)
5619 if (SCALAR_TYPE_P (t)
5620 || TREE_CODE (t) == VECTOR_TYPE
5621 || TREE_CODE (t) == REFERENCE_TYPE)
5622 return true;
5623 if (CLASS_TYPE_P (t))
5625 t = complete_type (t);
5626 gcc_assert (COMPLETE_TYPE_P (t) || errorcount);
5627 return CLASSTYPE_LITERAL_P (t);
5629 if (TREE_CODE (t) == ARRAY_TYPE)
5630 return literal_type_p (strip_array_types (t));
5631 return false;
5634 /* If DECL is a variable declared `constexpr', require its type
5635 be literal. Return the DECL if OK, otherwise NULL. */
5637 tree
5638 ensure_literal_type_for_constexpr_object (tree decl)
5640 tree type = TREE_TYPE (decl);
5641 if (TREE_CODE (decl) == VAR_DECL && DECL_DECLARED_CONSTEXPR_P (decl)
5642 && !processing_template_decl)
5644 if (CLASS_TYPE_P (type) && !COMPLETE_TYPE_P (complete_type (type)))
5645 /* Don't complain here, we'll complain about incompleteness
5646 when we try to initialize the variable. */;
5647 else if (!literal_type_p (type))
5649 error ("the type %qT of constexpr variable %qD is not literal",
5650 type, decl);
5651 explain_non_literal_class (type);
5652 return NULL;
5655 return decl;
5658 /* Representation of entries in the constexpr function definition table. */
5660 typedef struct GTY(()) constexpr_fundef {
5661 tree decl;
5662 tree body;
5663 } constexpr_fundef;
5665 /* This table holds all constexpr function definitions seen in
5666 the current translation unit. */
5668 static GTY ((param_is (constexpr_fundef))) htab_t constexpr_fundef_table;
5670 /* Utility function used for managing the constexpr function table.
5671 Return true if the entries pointed to by P and Q are for the
5672 same constexpr function. */
5674 static inline int
5675 constexpr_fundef_equal (const void *p, const void *q)
5677 const constexpr_fundef *lhs = (const constexpr_fundef *) p;
5678 const constexpr_fundef *rhs = (const constexpr_fundef *) q;
5679 return lhs->decl == rhs->decl;
5682 /* Utility function used for managing the constexpr function table.
5683 Return a hash value for the entry pointed to by Q. */
5685 static inline hashval_t
5686 constexpr_fundef_hash (const void *p)
5688 const constexpr_fundef *fundef = (const constexpr_fundef *) p;
5689 return DECL_UID (fundef->decl);
5692 /* Return a previously saved definition of function FUN. */
5694 static constexpr_fundef *
5695 retrieve_constexpr_fundef (tree fun)
5697 constexpr_fundef fundef = { NULL, NULL };
5698 if (constexpr_fundef_table == NULL)
5699 return NULL;
5701 fundef.decl = fun;
5702 return (constexpr_fundef *) htab_find (constexpr_fundef_table, &fundef);
5705 /* Check whether the parameter and return types of FUN are valid for a
5706 constexpr function, and complain if COMPLAIN. */
5708 static bool
5709 is_valid_constexpr_fn (tree fun, bool complain)
5711 tree parm = FUNCTION_FIRST_USER_PARM (fun);
5712 bool ret = true;
5713 for (; parm != NULL; parm = TREE_CHAIN (parm))
5714 if (!literal_type_p (TREE_TYPE (parm)))
5716 ret = false;
5717 if (complain)
5719 error ("invalid type for parameter %d of constexpr "
5720 "function %q+#D", DECL_PARM_INDEX (parm), fun);
5721 explain_non_literal_class (TREE_TYPE (parm));
5725 if (!DECL_CONSTRUCTOR_P (fun))
5727 tree rettype = TREE_TYPE (TREE_TYPE (fun));
5728 if (!literal_type_p (rettype))
5730 ret = false;
5731 if (complain)
5733 error ("invalid return type %qT of constexpr function %q+D",
5734 rettype, fun);
5735 explain_non_literal_class (rettype);
5739 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fun)
5740 && !CLASSTYPE_LITERAL_P (DECL_CONTEXT (fun)))
5742 ret = false;
5743 if (complain)
5745 error ("enclosing class of constexpr non-static member "
5746 "function %q+#D is not a literal type", fun);
5747 explain_non_literal_class (DECL_CONTEXT (fun));
5751 else if (CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fun)))
5753 ret = false;
5754 if (complain)
5755 error ("%q#T has virtual base classes", DECL_CONTEXT (fun));
5758 return ret;
5761 /* Subroutine of build_constexpr_constructor_member_initializers.
5762 The expression tree T represents a data member initialization
5763 in a (constexpr) constructor definition. Build a pairing of
5764 the data member with its initializer, and prepend that pair
5765 to the existing initialization pair INITS. */
5767 static bool
5768 build_data_member_initialization (tree t, VEC(constructor_elt,gc) **vec)
5770 tree member, init;
5771 if (TREE_CODE (t) == CLEANUP_POINT_EXPR)
5772 t = TREE_OPERAND (t, 0);
5773 if (TREE_CODE (t) == EXPR_STMT)
5774 t = TREE_OPERAND (t, 0);
5775 if (t == error_mark_node)
5776 return false;
5777 if (TREE_CODE (t) == STATEMENT_LIST)
5779 tree_stmt_iterator i;
5780 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
5782 if (! build_data_member_initialization (tsi_stmt (i), vec))
5783 return false;
5785 return true;
5787 if (TREE_CODE (t) == CLEANUP_STMT)
5789 /* We can't see a CLEANUP_STMT in a constructor for a literal class,
5790 but we can in a constexpr constructor for a non-literal class. Just
5791 ignore it; either all the initialization will be constant, in which
5792 case the cleanup can't run, or it can't be constexpr.
5793 Still recurse into CLEANUP_BODY. */
5794 return build_data_member_initialization (CLEANUP_BODY (t), vec);
5796 if (TREE_CODE (t) == CONVERT_EXPR)
5797 t = TREE_OPERAND (t, 0);
5798 if (TREE_CODE (t) == INIT_EXPR
5799 || TREE_CODE (t) == MODIFY_EXPR)
5801 member = TREE_OPERAND (t, 0);
5802 init = unshare_expr (TREE_OPERAND (t, 1));
5804 else
5806 gcc_assert (TREE_CODE (t) == CALL_EXPR);
5807 member = CALL_EXPR_ARG (t, 0);
5808 /* We don't use build_cplus_new here because it complains about
5809 abstract bases. Leaving the call unwrapped means that it has the
5810 wrong type, but cxx_eval_constant_expression doesn't care. */
5811 init = unshare_expr (t);
5813 if (TREE_CODE (member) == INDIRECT_REF)
5814 member = TREE_OPERAND (member, 0);
5815 if (TREE_CODE (member) == NOP_EXPR)
5817 tree op = member;
5818 STRIP_NOPS (op);
5819 if (TREE_CODE (op) == ADDR_EXPR)
5821 gcc_assert (same_type_ignoring_top_level_qualifiers_p
5822 (TREE_TYPE (TREE_TYPE (op)),
5823 TREE_TYPE (TREE_TYPE (member))));
5824 /* Initializing a cv-qualified member; we need to look through
5825 the const_cast. */
5826 member = op;
5828 else if (op == current_class_ptr
5829 && (same_type_ignoring_top_level_qualifiers_p
5830 (TREE_TYPE (TREE_TYPE (member)),
5831 current_class_type)))
5832 /* Delegating constructor. */
5833 member = op;
5834 else
5836 /* This is an initializer for an empty base; keep it for now so
5837 we can check it in cxx_eval_bare_aggregate. */
5838 gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (member))));
5841 if (TREE_CODE (member) == ADDR_EXPR)
5842 member = TREE_OPERAND (member, 0);
5843 if (TREE_CODE (member) == COMPONENT_REF
5844 /* If we're initializing a member of a subaggregate, it's a vtable
5845 pointer. Leave it as COMPONENT_REF so we remember the path to get
5846 to the vfield. */
5847 && TREE_CODE (TREE_OPERAND (member, 0)) != COMPONENT_REF)
5848 member = TREE_OPERAND (member, 1);
5849 CONSTRUCTOR_APPEND_ELT (*vec, member, init);
5850 return true;
5853 /* Make sure that there are no statements after LAST in the constructor
5854 body represented by LIST. */
5856 bool
5857 check_constexpr_ctor_body (tree last, tree list)
5859 bool ok = true;
5860 if (TREE_CODE (list) == STATEMENT_LIST)
5862 tree_stmt_iterator i = tsi_last (list);
5863 for (; !tsi_end_p (i); tsi_prev (&i))
5865 tree t = tsi_stmt (i);
5866 if (t == last)
5867 break;
5868 if (TREE_CODE (t) == BIND_EXPR)
5870 if (!check_constexpr_ctor_body (last, BIND_EXPR_BODY (t)))
5871 return false;
5872 else
5873 continue;
5875 /* We currently allow typedefs and static_assert.
5876 FIXME allow them in the standard, too. */
5877 if (TREE_CODE (t) != STATIC_ASSERT)
5879 ok = false;
5880 break;
5884 else if (list != last
5885 && TREE_CODE (list) != STATIC_ASSERT)
5886 ok = false;
5887 if (!ok)
5889 error ("constexpr constructor does not have empty body");
5890 DECL_DECLARED_CONSTEXPR_P (current_function_decl) = false;
5892 return ok;
5895 /* VEC is a vector of constructor elements built up for the base and member
5896 initializers of a constructor for TYPE. They need to be in increasing
5897 offset order, which they might not be yet if TYPE has a primary base
5898 which is not first in the base-clause. */
5900 static VEC(constructor_elt,gc) *
5901 sort_constexpr_mem_initializers (tree type, VEC(constructor_elt,gc) *vec)
5903 tree pri = CLASSTYPE_PRIMARY_BINFO (type);
5904 constructor_elt elt;
5905 int i;
5907 if (pri == NULL_TREE
5908 || pri == BINFO_BASE_BINFO (TYPE_BINFO (type), 0))
5909 return vec;
5911 /* Find the element for the primary base and move it to the beginning of
5912 the vec. */
5913 VEC(constructor_elt,gc) &v = *vec;
5914 pri = BINFO_TYPE (pri);
5915 for (i = 1; ; ++i)
5916 if (TREE_TYPE (v[i].index) == pri)
5917 break;
5919 elt = v[i];
5920 for (; i > 0; --i)
5921 v[i] = v[i-1];
5922 v[0] = elt;
5923 return vec;
5926 /* Build compile-time evalable representations of member-initializer list
5927 for a constexpr constructor. */
5929 static tree
5930 build_constexpr_constructor_member_initializers (tree type, tree body)
5932 VEC(constructor_elt,gc) *vec = NULL;
5933 bool ok = true;
5934 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR
5935 || TREE_CODE (body) == EH_SPEC_BLOCK)
5936 body = TREE_OPERAND (body, 0);
5937 if (TREE_CODE (body) == STATEMENT_LIST)
5938 body = STATEMENT_LIST_HEAD (body)->stmt;
5939 body = BIND_EXPR_BODY (body);
5940 if (TREE_CODE (body) == CLEANUP_POINT_EXPR)
5942 body = TREE_OPERAND (body, 0);
5943 if (TREE_CODE (body) == EXPR_STMT)
5944 body = TREE_OPERAND (body, 0);
5945 if (TREE_CODE (body) == INIT_EXPR
5946 && (same_type_ignoring_top_level_qualifiers_p
5947 (TREE_TYPE (TREE_OPERAND (body, 0)),
5948 current_class_type)))
5950 /* Trivial copy. */
5951 return TREE_OPERAND (body, 1);
5953 ok = build_data_member_initialization (body, &vec);
5955 else if (TREE_CODE (body) == STATEMENT_LIST)
5957 tree_stmt_iterator i;
5958 for (i = tsi_start (body); !tsi_end_p (i); tsi_next (&i))
5960 ok = build_data_member_initialization (tsi_stmt (i), &vec);
5961 if (!ok)
5962 break;
5965 else if (TREE_CODE (body) == TRY_BLOCK)
5967 error ("body of %<constexpr%> constructor cannot be "
5968 "a function-try-block");
5969 return error_mark_node;
5971 else if (EXPR_P (body))
5972 ok = build_data_member_initialization (body, &vec);
5973 else
5974 gcc_assert (errorcount > 0);
5975 if (ok)
5977 if (VEC_length (constructor_elt, vec) > 0)
5979 /* In a delegating constructor, return the target. */
5980 constructor_elt *ce = &VEC_index (constructor_elt, vec, 0);
5981 if (ce->index == current_class_ptr)
5983 body = ce->value;
5984 VEC_free (constructor_elt, gc, vec);
5985 return body;
5988 vec = sort_constexpr_mem_initializers (type, vec);
5989 return build_constructor (type, vec);
5991 else
5992 return error_mark_node;
5995 /* Subroutine of register_constexpr_fundef. BODY is the body of a function
5996 declared to be constexpr, or a sub-statement thereof. Returns the
5997 return value if suitable, error_mark_node for a statement not allowed in
5998 a constexpr function, or NULL_TREE if no return value was found. */
6000 static tree
6001 constexpr_fn_retval (tree body)
6003 switch (TREE_CODE (body))
6005 case STATEMENT_LIST:
6007 tree_stmt_iterator i;
6008 tree expr = NULL_TREE;
6009 for (i = tsi_start (body); !tsi_end_p (i); tsi_next (&i))
6011 tree s = constexpr_fn_retval (tsi_stmt (i));
6012 if (s == error_mark_node)
6013 return error_mark_node;
6014 else if (s == NULL_TREE)
6015 /* Keep iterating. */;
6016 else if (expr)
6017 /* Multiple return statements. */
6018 return error_mark_node;
6019 else
6020 expr = s;
6022 return expr;
6025 case RETURN_EXPR:
6026 return unshare_expr (TREE_OPERAND (body, 0));
6028 case DECL_EXPR:
6029 if (TREE_CODE (DECL_EXPR_DECL (body)) == USING_DECL)
6030 return NULL_TREE;
6031 return error_mark_node;
6033 case CLEANUP_POINT_EXPR:
6034 return constexpr_fn_retval (TREE_OPERAND (body, 0));
6036 case USING_STMT:
6037 return NULL_TREE;
6039 default:
6040 return error_mark_node;
6044 /* Subroutine of register_constexpr_fundef. BODY is the DECL_SAVED_TREE of
6045 FUN; do the necessary transformations to turn it into a single expression
6046 that we can store in the hash table. */
6048 static tree
6049 massage_constexpr_body (tree fun, tree body)
6051 if (DECL_CONSTRUCTOR_P (fun))
6052 body = build_constexpr_constructor_member_initializers
6053 (DECL_CONTEXT (fun), body);
6054 else
6056 if (TREE_CODE (body) == EH_SPEC_BLOCK)
6057 body = EH_SPEC_STMTS (body);
6058 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
6059 body = TREE_OPERAND (body, 0);
6060 if (TREE_CODE (body) == BIND_EXPR)
6061 body = BIND_EXPR_BODY (body);
6062 body = constexpr_fn_retval (body);
6064 return body;
6067 /* FUN is a constexpr constructor with massaged body BODY. Return true
6068 if some bases/fields are uninitialized, and complain if COMPLAIN. */
6070 static bool
6071 cx_check_missing_mem_inits (tree fun, tree body, bool complain)
6073 bool bad;
6074 tree field;
6075 unsigned i, nelts;
6076 tree ctype;
6078 if (TREE_CODE (body) != CONSTRUCTOR)
6079 return false;
6081 nelts = CONSTRUCTOR_NELTS (body);
6082 ctype = DECL_CONTEXT (fun);
6083 field = TYPE_FIELDS (ctype);
6085 if (TREE_CODE (ctype) == UNION_TYPE)
6087 if (nelts == 0 && next_initializable_field (field))
6089 if (complain)
6090 error ("%<constexpr%> constructor for union %qT must "
6091 "initialize exactly one non-static data member", ctype);
6092 return true;
6094 return false;
6097 bad = false;
6098 for (i = 0; i <= nelts; ++i)
6100 tree index;
6101 if (i == nelts)
6102 index = NULL_TREE;
6103 else
6105 index = CONSTRUCTOR_ELT (body, i)->index;
6106 /* Skip base and vtable inits. */
6107 if (TREE_CODE (index) != FIELD_DECL
6108 || DECL_ARTIFICIAL (index))
6109 continue;
6111 for (; field != index; field = DECL_CHAIN (field))
6113 tree ftype;
6114 if (TREE_CODE (field) != FIELD_DECL
6115 || (DECL_C_BIT_FIELD (field) && !DECL_NAME (field))
6116 || DECL_ARTIFICIAL (field))
6117 continue;
6118 ftype = strip_array_types (TREE_TYPE (field));
6119 if (type_has_constexpr_default_constructor (ftype))
6121 /* It's OK to skip a member with a trivial constexpr ctor.
6122 A constexpr ctor that isn't trivial should have been
6123 added in by now. */
6124 gcc_checking_assert (!TYPE_HAS_COMPLEX_DFLT (ftype)
6125 || errorcount != 0);
6126 continue;
6128 if (!complain)
6129 return true;
6130 error ("uninitialized member %qD in %<constexpr%> constructor",
6131 field);
6132 bad = true;
6134 if (field == NULL_TREE)
6135 break;
6136 field = DECL_CHAIN (field);
6139 return bad;
6142 /* We are processing the definition of the constexpr function FUN.
6143 Check that its BODY fulfills the propriate requirements and
6144 enter it in the constexpr function definition table.
6145 For constructor BODY is actually the TREE_LIST of the
6146 member-initializer list. */
6148 tree
6149 register_constexpr_fundef (tree fun, tree body)
6151 constexpr_fundef entry;
6152 constexpr_fundef **slot;
6154 if (!is_valid_constexpr_fn (fun, !DECL_GENERATED_P (fun)))
6155 return NULL;
6157 body = massage_constexpr_body (fun, body);
6158 if (body == NULL_TREE || body == error_mark_node)
6160 if (!DECL_CONSTRUCTOR_P (fun))
6161 error ("body of constexpr function %qD not a return-statement", fun);
6162 return NULL;
6165 if (!potential_rvalue_constant_expression (body))
6167 if (!DECL_GENERATED_P (fun))
6168 require_potential_rvalue_constant_expression (body);
6169 return NULL;
6172 if (DECL_CONSTRUCTOR_P (fun)
6173 && cx_check_missing_mem_inits (fun, body, !DECL_GENERATED_P (fun)))
6174 return NULL;
6176 /* Create the constexpr function table if necessary. */
6177 if (constexpr_fundef_table == NULL)
6178 constexpr_fundef_table = htab_create_ggc (101,
6179 constexpr_fundef_hash,
6180 constexpr_fundef_equal,
6181 ggc_free);
6182 entry.decl = fun;
6183 entry.body = body;
6184 slot = (constexpr_fundef **)
6185 htab_find_slot (constexpr_fundef_table, &entry, INSERT);
6187 gcc_assert (*slot == NULL);
6188 *slot = ggc_alloc_constexpr_fundef ();
6189 **slot = entry;
6191 return fun;
6194 /* FUN is a non-constexpr function called in a context that requires a
6195 constant expression. If it comes from a constexpr template, explain why
6196 the instantiation isn't constexpr. */
6198 void
6199 explain_invalid_constexpr_fn (tree fun)
6201 static struct pointer_set_t *diagnosed;
6202 tree body;
6203 location_t save_loc;
6204 /* Only diagnose defaulted functions or instantiations. */
6205 if (!DECL_DEFAULTED_FN (fun)
6206 && !is_instantiation_of_constexpr (fun))
6207 return;
6208 if (diagnosed == NULL)
6209 diagnosed = pointer_set_create ();
6210 if (pointer_set_insert (diagnosed, fun) != 0)
6211 /* Already explained. */
6212 return;
6214 save_loc = input_location;
6215 input_location = DECL_SOURCE_LOCATION (fun);
6216 inform (0, "%q+D is not usable as a constexpr function because:", fun);
6217 /* First check the declaration. */
6218 if (is_valid_constexpr_fn (fun, true))
6220 /* Then if it's OK, the body. */
6221 if (DECL_DEFAULTED_FN (fun))
6222 explain_implicit_non_constexpr (fun);
6223 else
6225 body = massage_constexpr_body (fun, DECL_SAVED_TREE (fun));
6226 require_potential_rvalue_constant_expression (body);
6227 if (DECL_CONSTRUCTOR_P (fun))
6228 cx_check_missing_mem_inits (fun, body, true);
6231 input_location = save_loc;
6234 /* Objects of this type represent calls to constexpr functions
6235 along with the bindings of parameters to their arguments, for
6236 the purpose of compile time evaluation. */
6238 typedef struct GTY(()) constexpr_call {
6239 /* Description of the constexpr function definition. */
6240 constexpr_fundef *fundef;
6241 /* Parameter bindings environment. A TREE_LIST where each TREE_PURPOSE
6242 is a parameter _DECL and the TREE_VALUE is the value of the parameter.
6243 Note: This arrangement is made to accomodate the use of
6244 iterative_hash_template_arg (see pt.c). If you change this
6245 representation, also change the hash calculation in
6246 cxx_eval_call_expression. */
6247 tree bindings;
6248 /* Result of the call.
6249 NULL means the call is being evaluated.
6250 error_mark_node means that the evaluation was erroneous;
6251 otherwise, the actuall value of the call. */
6252 tree result;
6253 /* The hash of this call; we remember it here to avoid having to
6254 recalculate it when expanding the hash table. */
6255 hashval_t hash;
6256 } constexpr_call;
6258 /* A table of all constexpr calls that have been evaluated by the
6259 compiler in this translation unit. */
6261 static GTY ((param_is (constexpr_call))) htab_t constexpr_call_table;
6263 static tree cxx_eval_constant_expression (const constexpr_call *, tree,
6264 bool, bool, bool *);
6265 static tree cxx_eval_vec_perm_expr (const constexpr_call *, tree, bool, bool,
6266 bool *);
6269 /* Compute a hash value for a constexpr call representation. */
6271 static hashval_t
6272 constexpr_call_hash (const void *p)
6274 const constexpr_call *info = (const constexpr_call *) p;
6275 return info->hash;
6278 /* Return 1 if the objects pointed to by P and Q represent calls
6279 to the same constexpr function with the same arguments.
6280 Otherwise, return 0. */
6282 static int
6283 constexpr_call_equal (const void *p, const void *q)
6285 const constexpr_call *lhs = (const constexpr_call *) p;
6286 const constexpr_call *rhs = (const constexpr_call *) q;
6287 tree lhs_bindings;
6288 tree rhs_bindings;
6289 if (lhs == rhs)
6290 return 1;
6291 if (!constexpr_fundef_equal (lhs->fundef, rhs->fundef))
6292 return 0;
6293 lhs_bindings = lhs->bindings;
6294 rhs_bindings = rhs->bindings;
6295 while (lhs_bindings != NULL && rhs_bindings != NULL)
6297 tree lhs_arg = TREE_VALUE (lhs_bindings);
6298 tree rhs_arg = TREE_VALUE (rhs_bindings);
6299 gcc_assert (TREE_TYPE (lhs_arg) == TREE_TYPE (rhs_arg));
6300 if (!cp_tree_equal (lhs_arg, rhs_arg))
6301 return 0;
6302 lhs_bindings = TREE_CHAIN (lhs_bindings);
6303 rhs_bindings = TREE_CHAIN (rhs_bindings);
6305 return lhs_bindings == rhs_bindings;
6308 /* Initialize the constexpr call table, if needed. */
6310 static void
6311 maybe_initialize_constexpr_call_table (void)
6313 if (constexpr_call_table == NULL)
6314 constexpr_call_table = htab_create_ggc (101,
6315 constexpr_call_hash,
6316 constexpr_call_equal,
6317 ggc_free);
6320 /* Return true if T designates the implied `this' parameter. */
6322 static inline bool
6323 is_this_parameter (tree t)
6325 return t == current_class_ptr;
6328 /* We have an expression tree T that represents a call, either CALL_EXPR
6329 or AGGR_INIT_EXPR. If the call is lexically to a named function,
6330 retrun the _DECL for that function. */
6332 static tree
6333 get_function_named_in_call (tree t)
6335 tree fun = NULL;
6336 switch (TREE_CODE (t))
6338 case CALL_EXPR:
6339 fun = CALL_EXPR_FN (t);
6340 break;
6342 case AGGR_INIT_EXPR:
6343 fun = AGGR_INIT_EXPR_FN (t);
6344 break;
6346 default:
6347 gcc_unreachable();
6348 break;
6350 if (TREE_CODE (fun) == ADDR_EXPR
6351 && TREE_CODE (TREE_OPERAND (fun, 0)) == FUNCTION_DECL)
6352 fun = TREE_OPERAND (fun, 0);
6353 return fun;
6356 /* We have an expression tree T that represents a call, either CALL_EXPR
6357 or AGGR_INIT_EXPR. Return the Nth argument. */
6359 static inline tree
6360 get_nth_callarg (tree t, int n)
6362 switch (TREE_CODE (t))
6364 case CALL_EXPR:
6365 return CALL_EXPR_ARG (t, n);
6367 case AGGR_INIT_EXPR:
6368 return AGGR_INIT_EXPR_ARG (t, n);
6370 default:
6371 gcc_unreachable ();
6372 return NULL;
6376 /* Look up the binding of the function parameter T in a constexpr
6377 function call context CALL. */
6379 static tree
6380 lookup_parameter_binding (const constexpr_call *call, tree t)
6382 tree b = purpose_member (t, call->bindings);
6383 return TREE_VALUE (b);
6386 /* Attempt to evaluate T which represents a call to a builtin function.
6387 We assume here that all builtin functions evaluate to scalar types
6388 represented by _CST nodes. */
6390 static tree
6391 cxx_eval_builtin_function_call (const constexpr_call *call, tree t,
6392 bool allow_non_constant, bool addr,
6393 bool *non_constant_p)
6395 const int nargs = call_expr_nargs (t);
6396 tree *args = (tree *) alloca (nargs * sizeof (tree));
6397 tree new_call;
6398 int i;
6399 for (i = 0; i < nargs; ++i)
6401 args[i] = cxx_eval_constant_expression (call, CALL_EXPR_ARG (t, i),
6402 allow_non_constant, addr,
6403 non_constant_p);
6404 if (allow_non_constant && *non_constant_p)
6405 return t;
6407 if (*non_constant_p)
6408 return t;
6409 new_call = build_call_array_loc (EXPR_LOCATION (t), TREE_TYPE (t),
6410 CALL_EXPR_FN (t), nargs, args);
6411 return fold (new_call);
6414 /* TEMP is the constant value of a temporary object of type TYPE. Adjust
6415 the type of the value to match. */
6417 static tree
6418 adjust_temp_type (tree type, tree temp)
6420 if (TREE_TYPE (temp) == type)
6421 return temp;
6422 /* Avoid wrapping an aggregate value in a NOP_EXPR. */
6423 if (TREE_CODE (temp) == CONSTRUCTOR)
6424 return build_constructor (type, CONSTRUCTOR_ELTS (temp));
6425 gcc_assert (SCALAR_TYPE_P (type));
6426 return cp_fold_convert (type, temp);
6429 /* Subroutine of cxx_eval_call_expression.
6430 We are processing a call expression (either CALL_EXPR or
6431 AGGR_INIT_EXPR) in the call context of OLD_CALL. Evaluate
6432 all arguments and bind their values to correspondings
6433 parameters, making up the NEW_CALL context. */
6435 static void
6436 cxx_bind_parameters_in_call (const constexpr_call *old_call, tree t,
6437 constexpr_call *new_call,
6438 bool allow_non_constant,
6439 bool *non_constant_p)
6441 const int nargs = call_expr_nargs (t);
6442 tree fun = new_call->fundef->decl;
6443 tree parms = DECL_ARGUMENTS (fun);
6444 int i;
6445 for (i = 0; i < nargs; ++i)
6447 tree x, arg;
6448 tree type = parms ? TREE_TYPE (parms) : void_type_node;
6449 /* For member function, the first argument is a pointer to the implied
6450 object. And for an object contruction, don't bind `this' before
6451 it is fully constructed. */
6452 if (i == 0 && DECL_CONSTRUCTOR_P (fun))
6453 goto next;
6454 x = get_nth_callarg (t, i);
6455 arg = cxx_eval_constant_expression (old_call, x, allow_non_constant,
6456 TREE_CODE (type) == REFERENCE_TYPE,
6457 non_constant_p);
6458 /* Don't VERIFY_CONSTANT here. */
6459 if (*non_constant_p && allow_non_constant)
6460 return;
6461 /* Just discard ellipsis args after checking their constantitude. */
6462 if (!parms)
6463 continue;
6464 if (*non_constant_p)
6465 /* Don't try to adjust the type of non-constant args. */
6466 goto next;
6468 /* Make sure the binding has the same type as the parm. */
6469 if (TREE_CODE (type) != REFERENCE_TYPE)
6470 arg = adjust_temp_type (type, arg);
6471 new_call->bindings = tree_cons (parms, arg, new_call->bindings);
6472 next:
6473 parms = TREE_CHAIN (parms);
6477 /* Variables and functions to manage constexpr call expansion context.
6478 These do not need to be marked for PCH or GC. */
6480 /* FIXME remember and print actual constant arguments. */
6481 static VEC(tree,heap) *call_stack = NULL;
6482 static int call_stack_tick;
6483 static int last_cx_error_tick;
6485 static bool
6486 push_cx_call_context (tree call)
6488 ++call_stack_tick;
6489 if (!EXPR_HAS_LOCATION (call))
6490 SET_EXPR_LOCATION (call, input_location);
6491 VEC_safe_push (tree, heap, call_stack, call);
6492 if (VEC_length (tree, call_stack) > (unsigned) max_constexpr_depth)
6493 return false;
6494 return true;
6497 static void
6498 pop_cx_call_context (void)
6500 ++call_stack_tick;
6501 VEC_pop (tree, call_stack);
6504 VEC(tree,heap) *
6505 cx_error_context (void)
6507 VEC(tree,heap) *r = NULL;
6508 if (call_stack_tick != last_cx_error_tick
6509 && !VEC_empty (tree, call_stack))
6510 r = call_stack;
6511 last_cx_error_tick = call_stack_tick;
6512 return r;
6515 /* Subroutine of cxx_eval_constant_expression.
6516 Evaluate the call expression tree T in the context of OLD_CALL expression
6517 evaluation. */
6519 static tree
6520 cxx_eval_call_expression (const constexpr_call *old_call, tree t,
6521 bool allow_non_constant, bool addr,
6522 bool *non_constant_p)
6524 location_t loc = EXPR_LOC_OR_HERE (t);
6525 tree fun = get_function_named_in_call (t);
6526 tree result;
6527 constexpr_call new_call = { NULL, NULL, NULL, 0 };
6528 constexpr_call **slot;
6529 constexpr_call *entry;
6530 bool depth_ok;
6532 if (TREE_CODE (fun) != FUNCTION_DECL)
6534 /* Might be a constexpr function pointer. */
6535 fun = cxx_eval_constant_expression (old_call, fun, allow_non_constant,
6536 /*addr*/false, non_constant_p);
6537 if (TREE_CODE (fun) == ADDR_EXPR)
6538 fun = TREE_OPERAND (fun, 0);
6540 if (TREE_CODE (fun) != FUNCTION_DECL)
6542 if (!allow_non_constant && !*non_constant_p)
6543 error_at (loc, "expression %qE does not designate a constexpr "
6544 "function", fun);
6545 *non_constant_p = true;
6546 return t;
6548 if (DECL_CLONED_FUNCTION_P (fun))
6549 fun = DECL_CLONED_FUNCTION (fun);
6550 if (is_builtin_fn (fun))
6551 return cxx_eval_builtin_function_call (old_call, t, allow_non_constant,
6552 addr, non_constant_p);
6553 if (!DECL_DECLARED_CONSTEXPR_P (fun))
6555 if (!allow_non_constant)
6557 error_at (loc, "call to non-constexpr function %qD", fun);
6558 explain_invalid_constexpr_fn (fun);
6560 *non_constant_p = true;
6561 return t;
6564 /* Shortcut trivial copy constructor/op=. */
6565 if (call_expr_nargs (t) == 2 && trivial_fn_p (fun))
6567 tree arg = convert_from_reference (get_nth_callarg (t, 1));
6568 return cxx_eval_constant_expression (old_call, arg, allow_non_constant,
6569 addr, non_constant_p);
6572 /* If in direct recursive call, optimize definition search. */
6573 if (old_call != NULL && old_call->fundef->decl == fun)
6574 new_call.fundef = old_call->fundef;
6575 else
6577 new_call.fundef = retrieve_constexpr_fundef (fun);
6578 if (new_call.fundef == NULL || new_call.fundef->body == NULL)
6580 if (!allow_non_constant)
6582 if (DECL_INITIAL (fun))
6584 /* The definition of fun was somehow unsuitable. */
6585 error_at (loc, "%qD called in a constant expression", fun);
6586 explain_invalid_constexpr_fn (fun);
6588 else
6589 error_at (loc, "%qD used before its definition", fun);
6591 *non_constant_p = true;
6592 return t;
6595 cxx_bind_parameters_in_call (old_call, t, &new_call,
6596 allow_non_constant, non_constant_p);
6597 if (*non_constant_p)
6598 return t;
6600 depth_ok = push_cx_call_context (t);
6602 new_call.hash
6603 = iterative_hash_template_arg (new_call.bindings,
6604 constexpr_fundef_hash (new_call.fundef));
6606 /* If we have seen this call before, we are done. */
6607 maybe_initialize_constexpr_call_table ();
6608 slot = (constexpr_call **)
6609 htab_find_slot (constexpr_call_table, &new_call, INSERT);
6610 entry = *slot;
6611 if (entry == NULL)
6613 /* We need to keep a pointer to the entry, not just the slot, as the
6614 slot can move in the call to cxx_eval_builtin_function_call. */
6615 *slot = entry = ggc_alloc_constexpr_call ();
6616 *entry = new_call;
6618 /* Calls which are in progress have their result set to NULL
6619 so that we can detect circular dependencies. */
6620 else if (entry->result == NULL)
6622 if (!allow_non_constant)
6623 error ("call has circular dependency");
6624 *non_constant_p = true;
6625 entry->result = result = error_mark_node;
6628 if (!depth_ok)
6630 if (!allow_non_constant)
6631 error ("constexpr evaluation depth exceeds maximum of %d (use "
6632 "-fconstexpr-depth= to increase the maximum)",
6633 max_constexpr_depth);
6634 *non_constant_p = true;
6635 entry->result = result = error_mark_node;
6637 else
6639 result = entry->result;
6640 if (!result || result == error_mark_node)
6641 result = (cxx_eval_constant_expression
6642 (&new_call, new_call.fundef->body,
6643 allow_non_constant, addr,
6644 non_constant_p));
6645 if (result == error_mark_node)
6646 *non_constant_p = true;
6647 if (*non_constant_p)
6648 entry->result = result = error_mark_node;
6649 else
6651 /* If this was a call to initialize an object, set the type of
6652 the CONSTRUCTOR to the type of that object. */
6653 if (DECL_CONSTRUCTOR_P (fun))
6655 tree ob_arg = get_nth_callarg (t, 0);
6656 STRIP_NOPS (ob_arg);
6657 gcc_assert (TREE_CODE (TREE_TYPE (ob_arg)) == POINTER_TYPE
6658 && CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (ob_arg))));
6659 result = adjust_temp_type (TREE_TYPE (TREE_TYPE (ob_arg)),
6660 result);
6662 entry->result = result;
6666 pop_cx_call_context ();
6667 return unshare_expr (result);
6670 /* FIXME speed this up, it's taking 16% of compile time on sieve testcase. */
6672 bool
6673 reduced_constant_expression_p (tree t)
6675 if (TREE_OVERFLOW_P (t))
6676 /* Integer overflow makes this not a constant expression. */
6677 return false;
6678 /* FIXME are we calling this too much? */
6679 return initializer_constant_valid_p (t, TREE_TYPE (t)) != NULL_TREE;
6682 /* Some expressions may have constant operands but are not constant
6683 themselves, such as 1/0. Call this function (or rather, the macro
6684 following it) to check for that condition.
6686 We only call this in places that require an arithmetic constant, not in
6687 places where we might have a non-constant expression that can be a
6688 component of a constant expression, such as the address of a constexpr
6689 variable that might be dereferenced later. */
6691 static bool
6692 verify_constant (tree t, bool allow_non_constant, bool *non_constant_p)
6694 if (!*non_constant_p && !reduced_constant_expression_p (t))
6696 if (!allow_non_constant)
6698 /* If T was already folded to a _CST with TREE_OVERFLOW set,
6699 printing the folded constant isn't helpful. */
6700 if (TREE_OVERFLOW_P (t))
6702 permerror (input_location, "overflow in constant expression");
6703 /* If we're being permissive (and are in an enforcing
6704 context), consider this constant. */
6705 if (flag_permissive)
6706 return false;
6708 else
6709 error ("%q+E is not a constant expression", t);
6711 *non_constant_p = true;
6713 return *non_constant_p;
6715 #define VERIFY_CONSTANT(X) \
6716 do { \
6717 if (verify_constant ((X), allow_non_constant, non_constant_p)) \
6718 return t; \
6719 } while (0)
6721 /* Subroutine of cxx_eval_constant_expression.
6722 Attempt to reduce the unary expression tree T to a compile time value.
6723 If successful, return the value. Otherwise issue a diagnostic
6724 and return error_mark_node. */
6726 static tree
6727 cxx_eval_unary_expression (const constexpr_call *call, tree t,
6728 bool allow_non_constant, bool addr,
6729 bool *non_constant_p)
6731 tree r;
6732 tree orig_arg = TREE_OPERAND (t, 0);
6733 tree arg = cxx_eval_constant_expression (call, orig_arg, allow_non_constant,
6734 addr, non_constant_p);
6735 VERIFY_CONSTANT (arg);
6736 if (arg == orig_arg)
6737 return t;
6738 r = fold_build1 (TREE_CODE (t), TREE_TYPE (t), arg);
6739 VERIFY_CONSTANT (r);
6740 return r;
6743 /* Subroutine of cxx_eval_constant_expression.
6744 Like cxx_eval_unary_expression, except for binary expressions. */
6746 static tree
6747 cxx_eval_binary_expression (const constexpr_call *call, tree t,
6748 bool allow_non_constant, bool addr,
6749 bool *non_constant_p)
6751 tree r;
6752 tree orig_lhs = TREE_OPERAND (t, 0);
6753 tree orig_rhs = TREE_OPERAND (t, 1);
6754 tree lhs, rhs;
6755 lhs = cxx_eval_constant_expression (call, orig_lhs,
6756 allow_non_constant, addr,
6757 non_constant_p);
6758 VERIFY_CONSTANT (lhs);
6759 rhs = cxx_eval_constant_expression (call, orig_rhs,
6760 allow_non_constant, addr,
6761 non_constant_p);
6762 VERIFY_CONSTANT (rhs);
6763 if (lhs == orig_lhs && rhs == orig_rhs)
6764 return t;
6765 r = fold_build2 (TREE_CODE (t), TREE_TYPE (t), lhs, rhs);
6766 VERIFY_CONSTANT (r);
6767 return r;
6770 /* Subroutine of cxx_eval_constant_expression.
6771 Attempt to evaluate condition expressions. Dead branches are not
6772 looked into. */
6774 static tree
6775 cxx_eval_conditional_expression (const constexpr_call *call, tree t,
6776 bool allow_non_constant, bool addr,
6777 bool *non_constant_p)
6779 tree val = cxx_eval_constant_expression (call, TREE_OPERAND (t, 0),
6780 allow_non_constant, addr,
6781 non_constant_p);
6782 VERIFY_CONSTANT (val);
6783 /* Don't VERIFY_CONSTANT the other operands. */
6784 if (integer_zerop (val))
6785 return cxx_eval_constant_expression (call, TREE_OPERAND (t, 2),
6786 allow_non_constant, addr,
6787 non_constant_p);
6788 return cxx_eval_constant_expression (call, TREE_OPERAND (t, 1),
6789 allow_non_constant, addr,
6790 non_constant_p);
6793 /* Subroutine of cxx_eval_constant_expression.
6794 Attempt to reduce a reference to an array slot. */
6796 static tree
6797 cxx_eval_array_reference (const constexpr_call *call, tree t,
6798 bool allow_non_constant, bool addr,
6799 bool *non_constant_p)
6801 tree oldary = TREE_OPERAND (t, 0);
6802 tree ary = cxx_eval_constant_expression (call, oldary,
6803 allow_non_constant, addr,
6804 non_constant_p);
6805 tree index, oldidx;
6806 HOST_WIDE_INT i;
6807 tree elem_type;
6808 unsigned len, elem_nchars = 1;
6809 if (*non_constant_p)
6810 return t;
6811 oldidx = TREE_OPERAND (t, 1);
6812 index = cxx_eval_constant_expression (call, oldidx,
6813 allow_non_constant, false,
6814 non_constant_p);
6815 VERIFY_CONSTANT (index);
6816 if (addr && ary == oldary && index == oldidx)
6817 return t;
6818 else if (addr)
6819 return build4 (ARRAY_REF, TREE_TYPE (t), ary, index, NULL, NULL);
6820 elem_type = TREE_TYPE (TREE_TYPE (ary));
6821 if (TREE_CODE (ary) == CONSTRUCTOR)
6822 len = CONSTRUCTOR_NELTS (ary);
6823 else if (TREE_CODE (ary) == STRING_CST)
6825 elem_nchars = (TYPE_PRECISION (elem_type)
6826 / TYPE_PRECISION (char_type_node));
6827 len = (unsigned) TREE_STRING_LENGTH (ary) / elem_nchars;
6829 else
6831 /* We can't do anything with other tree codes, so use
6832 VERIFY_CONSTANT to complain and fail. */
6833 VERIFY_CONSTANT (ary);
6834 gcc_unreachable ();
6836 if (compare_tree_int (index, len) >= 0)
6838 if (tree_int_cst_lt (index, array_type_nelts_top (TREE_TYPE (ary))))
6840 /* If it's within the array bounds but doesn't have an explicit
6841 initializer, it's value-initialized. */
6842 tree val = build_value_init (elem_type, tf_warning_or_error);
6843 return cxx_eval_constant_expression (call, val,
6844 allow_non_constant, addr,
6845 non_constant_p);
6848 if (!allow_non_constant)
6849 error ("array subscript out of bound");
6850 *non_constant_p = true;
6851 return t;
6853 i = tree_low_cst (index, 0);
6854 if (TREE_CODE (ary) == CONSTRUCTOR)
6855 return VEC_index (constructor_elt, CONSTRUCTOR_ELTS (ary), i).value;
6856 else if (elem_nchars == 1)
6857 return build_int_cst (cv_unqualified (TREE_TYPE (TREE_TYPE (ary))),
6858 TREE_STRING_POINTER (ary)[i]);
6859 else
6861 tree type = cv_unqualified (TREE_TYPE (TREE_TYPE (ary)));
6862 return native_interpret_expr (type, (const unsigned char *)
6863 TREE_STRING_POINTER (ary)
6864 + i * elem_nchars, elem_nchars);
6866 /* Don't VERIFY_CONSTANT here. */
6869 /* Subroutine of cxx_eval_constant_expression.
6870 Attempt to reduce a field access of a value of class type. */
6872 static tree
6873 cxx_eval_component_reference (const constexpr_call *call, tree t,
6874 bool allow_non_constant, bool addr,
6875 bool *non_constant_p)
6877 unsigned HOST_WIDE_INT i;
6878 tree field;
6879 tree value;
6880 tree part = TREE_OPERAND (t, 1);
6881 tree orig_whole = TREE_OPERAND (t, 0);
6882 tree whole = cxx_eval_constant_expression (call, orig_whole,
6883 allow_non_constant, addr,
6884 non_constant_p);
6885 if (whole == orig_whole)
6886 return t;
6887 if (addr)
6888 return fold_build3 (COMPONENT_REF, TREE_TYPE (t),
6889 whole, part, NULL_TREE);
6890 /* Don't VERIFY_CONSTANT here; we only want to check that we got a
6891 CONSTRUCTOR. */
6892 if (!*non_constant_p && TREE_CODE (whole) != CONSTRUCTOR)
6894 if (!allow_non_constant)
6895 error ("%qE is not a constant expression", orig_whole);
6896 *non_constant_p = true;
6898 if (DECL_MUTABLE_P (part))
6900 if (!allow_non_constant)
6901 error ("mutable %qD is not usable in a constant expression", part);
6902 *non_constant_p = true;
6904 if (*non_constant_p)
6905 return t;
6906 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole), i, field, value)
6908 if (field == part)
6909 return value;
6911 if (TREE_CODE (TREE_TYPE (whole)) == UNION_TYPE
6912 && CONSTRUCTOR_NELTS (whole) > 0)
6914 /* DR 1188 says we don't have to deal with this. */
6915 if (!allow_non_constant)
6916 error ("accessing %qD member instead of initialized %qD member in "
6917 "constant expression", part, CONSTRUCTOR_ELT (whole, 0)->index);
6918 *non_constant_p = true;
6919 return t;
6922 /* If there's no explicit init for this field, it's value-initialized. */
6923 value = build_value_init (TREE_TYPE (t), tf_warning_or_error);
6924 return cxx_eval_constant_expression (call, value,
6925 allow_non_constant, addr,
6926 non_constant_p);
6929 /* Subroutine of cxx_eval_constant_expression.
6930 Attempt to reduce a field access of a value of class type that is
6931 expressed as a BIT_FIELD_REF. */
6933 static tree
6934 cxx_eval_bit_field_ref (const constexpr_call *call, tree t,
6935 bool allow_non_constant, bool addr,
6936 bool *non_constant_p)
6938 tree orig_whole = TREE_OPERAND (t, 0);
6939 tree retval, fldval, utype, mask;
6940 bool fld_seen = false;
6941 HOST_WIDE_INT istart, isize;
6942 tree whole = cxx_eval_constant_expression (call, orig_whole,
6943 allow_non_constant, addr,
6944 non_constant_p);
6945 tree start, field, value;
6946 unsigned HOST_WIDE_INT i;
6948 if (whole == orig_whole)
6949 return t;
6950 /* Don't VERIFY_CONSTANT here; we only want to check that we got a
6951 CONSTRUCTOR. */
6952 if (!*non_constant_p && TREE_CODE (whole) != CONSTRUCTOR)
6954 if (!allow_non_constant)
6955 error ("%qE is not a constant expression", orig_whole);
6956 *non_constant_p = true;
6958 if (*non_constant_p)
6959 return t;
6961 start = TREE_OPERAND (t, 2);
6962 istart = tree_low_cst (start, 0);
6963 isize = tree_low_cst (TREE_OPERAND (t, 1), 0);
6964 utype = TREE_TYPE (t);
6965 if (!TYPE_UNSIGNED (utype))
6966 utype = build_nonstandard_integer_type (TYPE_PRECISION (utype), 1);
6967 retval = build_int_cst (utype, 0);
6968 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole), i, field, value)
6970 tree bitpos = bit_position (field);
6971 if (bitpos == start && DECL_SIZE (field) == TREE_OPERAND (t, 1))
6972 return value;
6973 if (TREE_CODE (TREE_TYPE (field)) == INTEGER_TYPE
6974 && TREE_CODE (value) == INTEGER_CST
6975 && host_integerp (bitpos, 0)
6976 && host_integerp (DECL_SIZE (field), 0))
6978 HOST_WIDE_INT bit = tree_low_cst (bitpos, 0);
6979 HOST_WIDE_INT sz = tree_low_cst (DECL_SIZE (field), 0);
6980 HOST_WIDE_INT shift;
6981 if (bit >= istart && bit + sz <= istart + isize)
6983 fldval = fold_convert (utype, value);
6984 mask = build_int_cst_type (utype, -1);
6985 mask = fold_build2 (LSHIFT_EXPR, utype, mask,
6986 size_int (TYPE_PRECISION (utype) - sz));
6987 mask = fold_build2 (RSHIFT_EXPR, utype, mask,
6988 size_int (TYPE_PRECISION (utype) - sz));
6989 fldval = fold_build2 (BIT_AND_EXPR, utype, fldval, mask);
6990 shift = bit - istart;
6991 if (BYTES_BIG_ENDIAN)
6992 shift = TYPE_PRECISION (utype) - shift - sz;
6993 fldval = fold_build2 (LSHIFT_EXPR, utype, fldval,
6994 size_int (shift));
6995 retval = fold_build2 (BIT_IOR_EXPR, utype, retval, fldval);
6996 fld_seen = true;
7000 if (fld_seen)
7001 return fold_convert (TREE_TYPE (t), retval);
7002 gcc_unreachable ();
7003 return error_mark_node;
7006 /* Subroutine of cxx_eval_constant_expression.
7007 Evaluate a short-circuited logical expression T in the context
7008 of a given constexpr CALL. BAILOUT_VALUE is the value for
7009 early return. CONTINUE_VALUE is used here purely for
7010 sanity check purposes. */
7012 static tree
7013 cxx_eval_logical_expression (const constexpr_call *call, tree t,
7014 tree bailout_value, tree continue_value,
7015 bool allow_non_constant, bool addr,
7016 bool *non_constant_p)
7018 tree r;
7019 tree lhs = cxx_eval_constant_expression (call, TREE_OPERAND (t, 0),
7020 allow_non_constant, addr,
7021 non_constant_p);
7022 VERIFY_CONSTANT (lhs);
7023 if (tree_int_cst_equal (lhs, bailout_value))
7024 return lhs;
7025 gcc_assert (tree_int_cst_equal (lhs, continue_value));
7026 r = cxx_eval_constant_expression (call, TREE_OPERAND (t, 1),
7027 allow_non_constant, addr, non_constant_p);
7028 VERIFY_CONSTANT (r);
7029 return r;
7032 /* REF is a COMPONENT_REF designating a particular field. V is a vector of
7033 CONSTRUCTOR elements to initialize (part of) an object containing that
7034 field. Return a pointer to the constructor_elt corresponding to the
7035 initialization of the field. */
7037 static constructor_elt *
7038 base_field_constructor_elt (VEC(constructor_elt,gc) *v, tree ref)
7040 tree aggr = TREE_OPERAND (ref, 0);
7041 tree field = TREE_OPERAND (ref, 1);
7042 HOST_WIDE_INT i;
7043 constructor_elt *ce;
7045 gcc_assert (TREE_CODE (ref) == COMPONENT_REF);
7047 if (TREE_CODE (aggr) == COMPONENT_REF)
7049 constructor_elt *base_ce
7050 = base_field_constructor_elt (v, aggr);
7051 v = CONSTRUCTOR_ELTS (base_ce->value);
7054 for (i = 0; VEC_iterate (constructor_elt, v, i, ce); ++i)
7055 if (ce->index == field)
7056 return ce;
7058 gcc_unreachable ();
7059 return NULL;
7062 /* Subroutine of cxx_eval_constant_expression.
7063 The expression tree T denotes a C-style array or a C-style
7064 aggregate. Reduce it to a constant expression. */
7066 static tree
7067 cxx_eval_bare_aggregate (const constexpr_call *call, tree t,
7068 bool allow_non_constant, bool addr,
7069 bool *non_constant_p)
7071 VEC(constructor_elt,gc) *v = CONSTRUCTOR_ELTS (t);
7072 VEC(constructor_elt,gc) *n = VEC_alloc (constructor_elt, gc,
7073 VEC_length (constructor_elt, v));
7074 constructor_elt *ce;
7075 HOST_WIDE_INT i;
7076 bool changed = false;
7077 gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (t));
7078 for (i = 0; VEC_iterate (constructor_elt, v, i, ce); ++i)
7080 tree elt = cxx_eval_constant_expression (call, ce->value,
7081 allow_non_constant, addr,
7082 non_constant_p);
7083 /* Don't VERIFY_CONSTANT here. */
7084 if (allow_non_constant && *non_constant_p)
7085 goto fail;
7086 if (elt != ce->value)
7087 changed = true;
7088 if (TREE_CODE (ce->index) == COMPONENT_REF)
7090 /* This is an initialization of a vfield inside a base
7091 subaggregate that we already initialized; push this
7092 initialization into the previous initialization. */
7093 constructor_elt *inner = base_field_constructor_elt (n, ce->index);
7094 inner->value = elt;
7096 else if (TREE_CODE (ce->index) == NOP_EXPR)
7098 /* This is an initializer for an empty base; now that we've
7099 checked that it's constant, we can ignore it. */
7100 gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (ce->index))));
7102 else
7103 CONSTRUCTOR_APPEND_ELT (n, ce->index, elt);
7105 if (*non_constant_p || !changed)
7107 fail:
7108 VEC_free (constructor_elt, gc, n);
7109 return t;
7111 t = build_constructor (TREE_TYPE (t), n);
7112 TREE_CONSTANT (t) = true;
7113 return t;
7116 /* Subroutine of cxx_eval_constant_expression.
7117 The expression tree T is a VEC_INIT_EXPR which denotes the desired
7118 initialization of a non-static data member of array type. Reduce it to a
7119 CONSTRUCTOR.
7121 Note that apart from value-initialization (when VALUE_INIT is true),
7122 this is only intended to support value-initialization and the
7123 initializations done by defaulted constructors for classes with
7124 non-static data members of array type. In this case, VEC_INIT_EXPR_INIT
7125 will either be NULL_TREE for the default constructor, or a COMPONENT_REF
7126 for the copy/move constructor. */
7128 static tree
7129 cxx_eval_vec_init_1 (const constexpr_call *call, tree atype, tree init,
7130 bool value_init, bool allow_non_constant, bool addr,
7131 bool *non_constant_p)
7133 tree elttype = TREE_TYPE (atype);
7134 int max = tree_low_cst (array_type_nelts (atype), 0);
7135 VEC(constructor_elt,gc) *n = VEC_alloc (constructor_elt, gc, max + 1);
7136 bool pre_init = false;
7137 int i;
7139 /* For the default constructor, build up a call to the default
7140 constructor of the element type. We only need to handle class types
7141 here, as for a constructor to be constexpr, all members must be
7142 initialized, which for a defaulted default constructor means they must
7143 be of a class type with a constexpr default constructor. */
7144 if (TREE_CODE (elttype) == ARRAY_TYPE)
7145 /* We only do this at the lowest level. */;
7146 else if (value_init)
7148 init = build_value_init (elttype, tf_warning_or_error);
7149 init = cxx_eval_constant_expression
7150 (call, init, allow_non_constant, addr, non_constant_p);
7151 pre_init = true;
7153 else if (!init)
7155 VEC(tree,gc) *argvec = make_tree_vector ();
7156 init = build_special_member_call (NULL_TREE, complete_ctor_identifier,
7157 &argvec, elttype, LOOKUP_NORMAL,
7158 tf_warning_or_error);
7159 release_tree_vector (argvec);
7160 init = cxx_eval_constant_expression (call, init, allow_non_constant,
7161 addr, non_constant_p);
7162 pre_init = true;
7165 if (*non_constant_p && !allow_non_constant)
7166 goto fail;
7168 for (i = 0; i <= max; ++i)
7170 tree idx = build_int_cst (size_type_node, i);
7171 tree eltinit;
7172 if (TREE_CODE (elttype) == ARRAY_TYPE)
7174 /* A multidimensional array; recurse. */
7175 if (value_init || init == NULL_TREE)
7176 eltinit = NULL_TREE;
7177 else
7178 eltinit = cp_build_array_ref (input_location, init, idx,
7179 tf_warning_or_error);
7180 eltinit = cxx_eval_vec_init_1 (call, elttype, eltinit, value_init,
7181 allow_non_constant, addr,
7182 non_constant_p);
7184 else if (pre_init)
7186 /* Initializing an element using value or default initialization
7187 we just pre-built above. */
7188 if (i == 0)
7189 eltinit = init;
7190 else
7191 eltinit = unshare_expr (init);
7193 else
7195 /* Copying an element. */
7196 VEC(tree,gc) *argvec;
7197 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7198 (atype, TREE_TYPE (init)));
7199 eltinit = cp_build_array_ref (input_location, init, idx,
7200 tf_warning_or_error);
7201 if (!real_lvalue_p (init))
7202 eltinit = move (eltinit);
7203 argvec = make_tree_vector ();
7204 VEC_quick_push (tree, argvec, eltinit);
7205 eltinit = (build_special_member_call
7206 (NULL_TREE, complete_ctor_identifier, &argvec,
7207 elttype, LOOKUP_NORMAL, tf_warning_or_error));
7208 release_tree_vector (argvec);
7209 eltinit = cxx_eval_constant_expression
7210 (call, eltinit, allow_non_constant, addr, non_constant_p);
7212 if (*non_constant_p && !allow_non_constant)
7213 goto fail;
7214 CONSTRUCTOR_APPEND_ELT (n, idx, eltinit);
7217 if (!*non_constant_p)
7219 init = build_constructor (atype, n);
7220 TREE_CONSTANT (init) = true;
7221 return init;
7224 fail:
7225 VEC_free (constructor_elt, gc, n);
7226 return init;
7229 static tree
7230 cxx_eval_vec_init (const constexpr_call *call, tree t,
7231 bool allow_non_constant, bool addr,
7232 bool *non_constant_p)
7234 tree atype = TREE_TYPE (t);
7235 tree init = VEC_INIT_EXPR_INIT (t);
7236 tree r = cxx_eval_vec_init_1 (call, atype, init,
7237 VEC_INIT_EXPR_VALUE_INIT (t),
7238 allow_non_constant, addr, non_constant_p);
7239 if (*non_constant_p)
7240 return t;
7241 else
7242 return r;
7245 /* A less strict version of fold_indirect_ref_1, which requires cv-quals to
7246 match. We want to be less strict for simple *& folding; if we have a
7247 non-const temporary that we access through a const pointer, that should
7248 work. We handle this here rather than change fold_indirect_ref_1
7249 because we're dealing with things like ADDR_EXPR of INTEGER_CST which
7250 don't really make sense outside of constant expression evaluation. Also
7251 we want to allow folding to COMPONENT_REF, which could cause trouble
7252 with TBAA in fold_indirect_ref_1.
7254 Try to keep this function synced with fold_indirect_ref_1. */
7256 static tree
7257 cxx_fold_indirect_ref (location_t loc, tree type, tree op0, bool *empty_base)
7259 tree sub, subtype;
7261 sub = op0;
7262 STRIP_NOPS (sub);
7263 subtype = TREE_TYPE (sub);
7264 if (!POINTER_TYPE_P (subtype))
7265 return NULL_TREE;
7267 if (TREE_CODE (sub) == ADDR_EXPR)
7269 tree op = TREE_OPERAND (sub, 0);
7270 tree optype = TREE_TYPE (op);
7272 /* *&CONST_DECL -> to the value of the const decl. */
7273 if (TREE_CODE (op) == CONST_DECL)
7274 return DECL_INITIAL (op);
7275 /* *&p => p; make sure to handle *&"str"[cst] here. */
7276 if (same_type_ignoring_top_level_qualifiers_p (optype, type))
7278 tree fop = fold_read_from_constant_string (op);
7279 if (fop)
7280 return fop;
7281 else
7282 return op;
7284 /* *(foo *)&fooarray => fooarray[0] */
7285 else if (TREE_CODE (optype) == ARRAY_TYPE
7286 && (same_type_ignoring_top_level_qualifiers_p
7287 (type, TREE_TYPE (optype))))
7289 tree type_domain = TYPE_DOMAIN (optype);
7290 tree min_val = size_zero_node;
7291 if (type_domain && TYPE_MIN_VALUE (type_domain))
7292 min_val = TYPE_MIN_VALUE (type_domain);
7293 return build4_loc (loc, ARRAY_REF, type, op, min_val,
7294 NULL_TREE, NULL_TREE);
7296 /* *(foo *)&complexfoo => __real__ complexfoo */
7297 else if (TREE_CODE (optype) == COMPLEX_TYPE
7298 && (same_type_ignoring_top_level_qualifiers_p
7299 (type, TREE_TYPE (optype))))
7300 return fold_build1_loc (loc, REALPART_EXPR, type, op);
7301 /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
7302 else if (TREE_CODE (optype) == VECTOR_TYPE
7303 && (same_type_ignoring_top_level_qualifiers_p
7304 (type, TREE_TYPE (optype))))
7306 tree part_width = TYPE_SIZE (type);
7307 tree index = bitsize_int (0);
7308 return fold_build3_loc (loc, BIT_FIELD_REF, type, op, part_width, index);
7310 /* Also handle conversion to an empty base class, which
7311 is represented with a NOP_EXPR. */
7312 else if (is_empty_class (type)
7313 && CLASS_TYPE_P (optype)
7314 && DERIVED_FROM_P (type, optype))
7316 *empty_base = true;
7317 return op;
7319 /* *(foo *)&struct_with_foo_field => COMPONENT_REF */
7320 else if (RECORD_OR_UNION_TYPE_P (optype))
7322 tree field = TYPE_FIELDS (optype);
7323 for (; field; field = DECL_CHAIN (field))
7324 if (TREE_CODE (field) == FIELD_DECL
7325 && integer_zerop (byte_position (field))
7326 && (same_type_ignoring_top_level_qualifiers_p
7327 (TREE_TYPE (field), type)))
7329 return fold_build3 (COMPONENT_REF, type, op, field, NULL_TREE);
7330 break;
7334 else if (TREE_CODE (sub) == POINTER_PLUS_EXPR
7335 && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST)
7337 tree op00 = TREE_OPERAND (sub, 0);
7338 tree op01 = TREE_OPERAND (sub, 1);
7340 STRIP_NOPS (op00);
7341 if (TREE_CODE (op00) == ADDR_EXPR)
7343 tree op00type;
7344 op00 = TREE_OPERAND (op00, 0);
7345 op00type = TREE_TYPE (op00);
7347 /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
7348 if (TREE_CODE (op00type) == VECTOR_TYPE
7349 && (same_type_ignoring_top_level_qualifiers_p
7350 (type, TREE_TYPE (op00type))))
7352 HOST_WIDE_INT offset = tree_low_cst (op01, 0);
7353 tree part_width = TYPE_SIZE (type);
7354 unsigned HOST_WIDE_INT part_widthi = tree_low_cst (part_width, 0)/BITS_PER_UNIT;
7355 unsigned HOST_WIDE_INT indexi = offset * BITS_PER_UNIT;
7356 tree index = bitsize_int (indexi);
7358 if (offset/part_widthi <= TYPE_VECTOR_SUBPARTS (op00type))
7359 return fold_build3_loc (loc,
7360 BIT_FIELD_REF, type, op00,
7361 part_width, index);
7364 /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
7365 else if (TREE_CODE (op00type) == COMPLEX_TYPE
7366 && (same_type_ignoring_top_level_qualifiers_p
7367 (type, TREE_TYPE (op00type))))
7369 tree size = TYPE_SIZE_UNIT (type);
7370 if (tree_int_cst_equal (size, op01))
7371 return fold_build1_loc (loc, IMAGPART_EXPR, type, op00);
7373 /* ((foo *)&fooarray)[1] => fooarray[1] */
7374 else if (TREE_CODE (op00type) == ARRAY_TYPE
7375 && (same_type_ignoring_top_level_qualifiers_p
7376 (type, TREE_TYPE (op00type))))
7378 tree type_domain = TYPE_DOMAIN (op00type);
7379 tree min_val = size_zero_node;
7380 if (type_domain && TYPE_MIN_VALUE (type_domain))
7381 min_val = TYPE_MIN_VALUE (type_domain);
7382 op01 = size_binop_loc (loc, EXACT_DIV_EXPR, op01,
7383 TYPE_SIZE_UNIT (type));
7384 op01 = size_binop_loc (loc, PLUS_EXPR, op01, min_val);
7385 return build4_loc (loc, ARRAY_REF, type, op00, op01,
7386 NULL_TREE, NULL_TREE);
7388 /* ((foo *)&struct_with_foo_field)[1] => COMPONENT_REF */
7389 else if (RECORD_OR_UNION_TYPE_P (op00type))
7391 tree field = TYPE_FIELDS (op00type);
7392 for (; field; field = DECL_CHAIN (field))
7393 if (TREE_CODE (field) == FIELD_DECL
7394 && tree_int_cst_equal (byte_position (field), op01)
7395 && (same_type_ignoring_top_level_qualifiers_p
7396 (TREE_TYPE (field), type)))
7398 return fold_build3 (COMPONENT_REF, type, op00,
7399 field, NULL_TREE);
7400 break;
7405 /* *(foo *)fooarrptreturn> (*fooarrptr)[0] */
7406 else if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
7407 && (same_type_ignoring_top_level_qualifiers_p
7408 (type, TREE_TYPE (TREE_TYPE (subtype)))))
7410 tree type_domain;
7411 tree min_val = size_zero_node;
7412 sub = cxx_fold_indirect_ref (loc, TREE_TYPE (subtype), sub, NULL);
7413 if (!sub)
7414 sub = build1_loc (loc, INDIRECT_REF, TREE_TYPE (subtype), sub);
7415 type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
7416 if (type_domain && TYPE_MIN_VALUE (type_domain))
7417 min_val = TYPE_MIN_VALUE (type_domain);
7418 return build4_loc (loc, ARRAY_REF, type, sub, min_val, NULL_TREE,
7419 NULL_TREE);
7422 return NULL_TREE;
7425 static tree
7426 cxx_eval_indirect_ref (const constexpr_call *call, tree t,
7427 bool allow_non_constant, bool addr,
7428 bool *non_constant_p)
7430 tree orig_op0 = TREE_OPERAND (t, 0);
7431 tree op0 = cxx_eval_constant_expression (call, orig_op0, allow_non_constant,
7432 /*addr*/false, non_constant_p);
7433 bool empty_base = false;
7434 tree r;
7436 /* Don't VERIFY_CONSTANT here. */
7437 if (*non_constant_p)
7438 return t;
7440 r = cxx_fold_indirect_ref (EXPR_LOCATION (t), TREE_TYPE (t), op0,
7441 &empty_base);
7443 if (r)
7444 r = cxx_eval_constant_expression (call, r, allow_non_constant,
7445 addr, non_constant_p);
7446 else
7448 tree sub = op0;
7449 STRIP_NOPS (sub);
7450 if (TREE_CODE (sub) == POINTER_PLUS_EXPR)
7452 sub = TREE_OPERAND (sub, 0);
7453 STRIP_NOPS (sub);
7455 if (TREE_CODE (sub) == ADDR_EXPR)
7457 /* We couldn't fold to a constant value. Make sure it's not
7458 something we should have been able to fold. */
7459 gcc_assert (!same_type_ignoring_top_level_qualifiers_p
7460 (TREE_TYPE (TREE_TYPE (sub)), TREE_TYPE (t)));
7461 /* DR 1188 says we don't have to deal with this. */
7462 if (!allow_non_constant)
7463 error ("accessing value of %qE through a %qT glvalue in a "
7464 "constant expression", build_fold_indirect_ref (sub),
7465 TREE_TYPE (t));
7466 *non_constant_p = true;
7467 return t;
7471 /* If we're pulling out the value of an empty base, make sure
7472 that the whole object is constant and then return an empty
7473 CONSTRUCTOR. */
7474 if (empty_base)
7476 VERIFY_CONSTANT (r);
7477 r = build_constructor (TREE_TYPE (t), NULL);
7478 TREE_CONSTANT (r) = true;
7481 if (r == NULL_TREE)
7483 if (!addr)
7484 VERIFY_CONSTANT (t);
7485 return t;
7487 return r;
7490 /* Complain about R, a VAR_DECL, not being usable in a constant expression.
7491 Shared between potential_constant_expression and
7492 cxx_eval_constant_expression. */
7494 static void
7495 non_const_var_error (tree r)
7497 tree type = TREE_TYPE (r);
7498 error ("the value of %qD is not usable in a constant "
7499 "expression", r);
7500 /* Avoid error cascade. */
7501 if (DECL_INITIAL (r) == error_mark_node)
7502 return;
7503 if (DECL_DECLARED_CONSTEXPR_P (r))
7504 inform (DECL_SOURCE_LOCATION (r),
7505 "%qD used in its own initializer", r);
7506 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
7508 if (!CP_TYPE_CONST_P (type))
7509 inform (DECL_SOURCE_LOCATION (r),
7510 "%q#D is not const", r);
7511 else if (CP_TYPE_VOLATILE_P (type))
7512 inform (DECL_SOURCE_LOCATION (r),
7513 "%q#D is volatile", r);
7514 else if (!DECL_INITIAL (r)
7515 || !TREE_CONSTANT (DECL_INITIAL (r)))
7516 inform (DECL_SOURCE_LOCATION (r),
7517 "%qD was not initialized with a constant "
7518 "expression", r);
7519 else
7520 gcc_unreachable ();
7522 else
7524 if (cxx_dialect >= cxx0x && !DECL_DECLARED_CONSTEXPR_P (r))
7525 inform (DECL_SOURCE_LOCATION (r),
7526 "%qD was not declared %<constexpr%>", r);
7527 else
7528 inform (DECL_SOURCE_LOCATION (r),
7529 "%qD does not have integral or enumeration type",
7534 /* Evaluate VEC_PERM_EXPR (v1, v2, mask). */
7535 static tree
7536 cxx_eval_vec_perm_expr (const constexpr_call *call, tree t,
7537 bool allow_non_constant, bool addr,
7538 bool * non_constant_p)
7540 int i;
7541 tree args[3];
7542 tree val;
7543 tree elttype = TREE_TYPE (t);
7545 for (i = 0; i < 3; i++)
7547 args[i] = cxx_eval_constant_expression (call, TREE_OPERAND (t, i),
7548 allow_non_constant, addr,
7549 non_constant_p);
7550 if (*non_constant_p)
7551 goto fail;
7554 gcc_assert (TREE_CODE (TREE_TYPE (args[0])) == VECTOR_TYPE);
7555 gcc_assert (TREE_CODE (TREE_TYPE (args[1])) == VECTOR_TYPE);
7556 gcc_assert (TREE_CODE (TREE_TYPE (args[2])) == VECTOR_TYPE);
7558 val = fold_ternary_loc (EXPR_LOCATION (t), VEC_PERM_EXPR, elttype,
7559 args[0], args[1], args[2]);
7560 if (val != NULL_TREE)
7561 return val;
7563 fail:
7564 return t;
7567 /* Attempt to reduce the expression T to a constant value.
7568 On failure, issue diagnostic and return error_mark_node. */
7569 /* FIXME unify with c_fully_fold */
7571 static tree
7572 cxx_eval_constant_expression (const constexpr_call *call, tree t,
7573 bool allow_non_constant, bool addr,
7574 bool *non_constant_p)
7576 tree r = t;
7578 if (t == error_mark_node)
7580 *non_constant_p = true;
7581 return t;
7583 if (CONSTANT_CLASS_P (t))
7585 if (TREE_CODE (t) == PTRMEM_CST)
7586 t = cplus_expand_constant (t);
7587 return t;
7589 if (TREE_CODE (t) != NOP_EXPR
7590 && reduced_constant_expression_p (t))
7591 return fold (t);
7593 switch (TREE_CODE (t))
7595 case VAR_DECL:
7596 if (addr)
7597 return t;
7598 /* else fall through. */
7599 case CONST_DECL:
7600 r = integral_constant_value (t);
7601 if (TREE_CODE (r) == TARGET_EXPR
7602 && TREE_CODE (TARGET_EXPR_INITIAL (r)) == CONSTRUCTOR)
7603 r = TARGET_EXPR_INITIAL (r);
7604 if (DECL_P (r))
7606 if (!allow_non_constant)
7607 non_const_var_error (r);
7608 *non_constant_p = true;
7610 break;
7612 case FUNCTION_DECL:
7613 case TEMPLATE_DECL:
7614 case LABEL_DECL:
7615 return t;
7617 case PARM_DECL:
7618 if (call && DECL_CONTEXT (t) == call->fundef->decl)
7620 if (DECL_ARTIFICIAL (t) && DECL_CONSTRUCTOR_P (DECL_CONTEXT (t)))
7622 if (!allow_non_constant)
7623 sorry ("use of the value of the object being constructed "
7624 "in a constant expression");
7625 *non_constant_p = true;
7627 else
7628 r = lookup_parameter_binding (call, t);
7630 else if (addr)
7631 /* Defer in case this is only used for its type. */;
7632 else
7634 if (!allow_non_constant)
7635 error ("%qE is not a constant expression", t);
7636 *non_constant_p = true;
7638 break;
7640 case CALL_EXPR:
7641 case AGGR_INIT_EXPR:
7642 r = cxx_eval_call_expression (call, t, allow_non_constant, addr,
7643 non_constant_p);
7644 break;
7646 case TARGET_EXPR:
7647 if (!literal_type_p (TREE_TYPE (t)))
7649 if (!allow_non_constant)
7651 error ("temporary of non-literal type %qT in a "
7652 "constant expression", TREE_TYPE (t));
7653 explain_non_literal_class (TREE_TYPE (t));
7655 *non_constant_p = true;
7656 break;
7658 /* else fall through. */
7659 case INIT_EXPR:
7660 /* Pass false for 'addr' because these codes indicate
7661 initialization of a temporary. */
7662 r = cxx_eval_constant_expression (call, TREE_OPERAND (t, 1),
7663 allow_non_constant, false,
7664 non_constant_p);
7665 if (!*non_constant_p)
7666 /* Adjust the type of the result to the type of the temporary. */
7667 r = adjust_temp_type (TREE_TYPE (t), r);
7668 break;
7670 case SCOPE_REF:
7671 r = cxx_eval_constant_expression (call, TREE_OPERAND (t, 1),
7672 allow_non_constant, addr,
7673 non_constant_p);
7674 break;
7676 case RETURN_EXPR:
7677 case NON_LVALUE_EXPR:
7678 case TRY_CATCH_EXPR:
7679 case CLEANUP_POINT_EXPR:
7680 case MUST_NOT_THROW_EXPR:
7681 case SAVE_EXPR:
7682 r = cxx_eval_constant_expression (call, TREE_OPERAND (t, 0),
7683 allow_non_constant, addr,
7684 non_constant_p);
7685 break;
7687 /* These differ from cxx_eval_unary_expression in that this doesn't
7688 check for a constant operand or result; an address can be
7689 constant without its operand being, and vice versa. */
7690 case INDIRECT_REF:
7691 r = cxx_eval_indirect_ref (call, t, allow_non_constant, addr,
7692 non_constant_p);
7693 break;
7695 case ADDR_EXPR:
7697 tree oldop = TREE_OPERAND (t, 0);
7698 tree op = cxx_eval_constant_expression (call, oldop,
7699 allow_non_constant,
7700 /*addr*/true,
7701 non_constant_p);
7702 /* Don't VERIFY_CONSTANT here. */
7703 if (*non_constant_p)
7704 return t;
7705 /* This function does more aggressive folding than fold itself. */
7706 r = build_fold_addr_expr_with_type (op, TREE_TYPE (t));
7707 if (TREE_CODE (r) == ADDR_EXPR && TREE_OPERAND (r, 0) == oldop)
7708 return t;
7709 break;
7712 case REALPART_EXPR:
7713 case IMAGPART_EXPR:
7714 case CONJ_EXPR:
7715 case FIX_TRUNC_EXPR:
7716 case FLOAT_EXPR:
7717 case NEGATE_EXPR:
7718 case ABS_EXPR:
7719 case BIT_NOT_EXPR:
7720 case TRUTH_NOT_EXPR:
7721 case FIXED_CONVERT_EXPR:
7722 r = cxx_eval_unary_expression (call, t, allow_non_constant, addr,
7723 non_constant_p);
7724 break;
7726 case COMPOUND_EXPR:
7728 /* check_return_expr sometimes wraps a TARGET_EXPR in a
7729 COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR
7730 introduced by build_call_a. */
7731 tree op0 = TREE_OPERAND (t, 0);
7732 tree op1 = TREE_OPERAND (t, 1);
7733 STRIP_NOPS (op1);
7734 if ((TREE_CODE (op0) == TARGET_EXPR && op1 == TARGET_EXPR_SLOT (op0))
7735 || TREE_CODE (op1) == EMPTY_CLASS_EXPR)
7736 r = cxx_eval_constant_expression (call, op0, allow_non_constant,
7737 addr, non_constant_p);
7738 else
7740 /* Check that the LHS is constant and then discard it. */
7741 cxx_eval_constant_expression (call, op0, allow_non_constant,
7742 false, non_constant_p);
7743 op1 = TREE_OPERAND (t, 1);
7744 r = cxx_eval_constant_expression (call, op1, allow_non_constant,
7745 addr, non_constant_p);
7748 break;
7750 case POINTER_PLUS_EXPR:
7751 case PLUS_EXPR:
7752 case MINUS_EXPR:
7753 case MULT_EXPR:
7754 case TRUNC_DIV_EXPR:
7755 case CEIL_DIV_EXPR:
7756 case FLOOR_DIV_EXPR:
7757 case ROUND_DIV_EXPR:
7758 case TRUNC_MOD_EXPR:
7759 case CEIL_MOD_EXPR:
7760 case ROUND_MOD_EXPR:
7761 case RDIV_EXPR:
7762 case EXACT_DIV_EXPR:
7763 case MIN_EXPR:
7764 case MAX_EXPR:
7765 case LSHIFT_EXPR:
7766 case RSHIFT_EXPR:
7767 case LROTATE_EXPR:
7768 case RROTATE_EXPR:
7769 case BIT_IOR_EXPR:
7770 case BIT_XOR_EXPR:
7771 case BIT_AND_EXPR:
7772 case TRUTH_XOR_EXPR:
7773 case LT_EXPR:
7774 case LE_EXPR:
7775 case GT_EXPR:
7776 case GE_EXPR:
7777 case EQ_EXPR:
7778 case NE_EXPR:
7779 case UNORDERED_EXPR:
7780 case ORDERED_EXPR:
7781 case UNLT_EXPR:
7782 case UNLE_EXPR:
7783 case UNGT_EXPR:
7784 case UNGE_EXPR:
7785 case UNEQ_EXPR:
7786 case RANGE_EXPR:
7787 case COMPLEX_EXPR:
7788 r = cxx_eval_binary_expression (call, t, allow_non_constant, addr,
7789 non_constant_p);
7790 break;
7792 /* fold can introduce non-IF versions of these; still treat them as
7793 short-circuiting. */
7794 case TRUTH_AND_EXPR:
7795 case TRUTH_ANDIF_EXPR:
7796 r = cxx_eval_logical_expression (call, t, boolean_false_node,
7797 boolean_true_node,
7798 allow_non_constant, addr,
7799 non_constant_p);
7800 break;
7802 case TRUTH_OR_EXPR:
7803 case TRUTH_ORIF_EXPR:
7804 r = cxx_eval_logical_expression (call, t, boolean_true_node,
7805 boolean_false_node,
7806 allow_non_constant, addr,
7807 non_constant_p);
7808 break;
7810 case ARRAY_REF:
7811 r = cxx_eval_array_reference (call, t, allow_non_constant, addr,
7812 non_constant_p);
7813 break;
7815 case COMPONENT_REF:
7816 r = cxx_eval_component_reference (call, t, allow_non_constant, addr,
7817 non_constant_p);
7818 break;
7820 case BIT_FIELD_REF:
7821 r = cxx_eval_bit_field_ref (call, t, allow_non_constant, addr,
7822 non_constant_p);
7823 break;
7825 case COND_EXPR:
7826 case VEC_COND_EXPR:
7827 r = cxx_eval_conditional_expression (call, t, allow_non_constant, addr,
7828 non_constant_p);
7829 break;
7831 case CONSTRUCTOR:
7832 r = cxx_eval_bare_aggregate (call, t, allow_non_constant, addr,
7833 non_constant_p);
7834 break;
7836 case VEC_INIT_EXPR:
7837 /* We can get this in a defaulted constructor for a class with a
7838 non-static data member of array type. Either the initializer will
7839 be NULL, meaning default-initialization, or it will be an lvalue
7840 or xvalue of the same type, meaning direct-initialization from the
7841 corresponding member. */
7842 r = cxx_eval_vec_init (call, t, allow_non_constant, addr,
7843 non_constant_p);
7844 break;
7846 case VEC_PERM_EXPR:
7847 r = cxx_eval_vec_perm_expr (call, t, allow_non_constant, addr,
7848 non_constant_p);
7849 break;
7851 case CONVERT_EXPR:
7852 case VIEW_CONVERT_EXPR:
7853 case NOP_EXPR:
7855 tree oldop = TREE_OPERAND (t, 0);
7856 tree op = cxx_eval_constant_expression (call, oldop,
7857 allow_non_constant, addr,
7858 non_constant_p);
7859 if (*non_constant_p)
7860 return t;
7861 if (op == oldop)
7862 /* We didn't fold at the top so we could check for ptr-int
7863 conversion. */
7864 return fold (t);
7865 r = fold_build1 (TREE_CODE (t), TREE_TYPE (t), op);
7866 /* Conversion of an out-of-range value has implementation-defined
7867 behavior; the language considers it different from arithmetic
7868 overflow, which is undefined. */
7869 if (TREE_OVERFLOW_P (r) && !TREE_OVERFLOW_P (op))
7870 TREE_OVERFLOW (r) = false;
7872 break;
7874 case EMPTY_CLASS_EXPR:
7875 /* This is good enough for a function argument that might not get
7876 used, and they can't do anything with it, so just return it. */
7877 return t;
7879 case LAMBDA_EXPR:
7880 case PREINCREMENT_EXPR:
7881 case POSTINCREMENT_EXPR:
7882 case PREDECREMENT_EXPR:
7883 case POSTDECREMENT_EXPR:
7884 case NEW_EXPR:
7885 case VEC_NEW_EXPR:
7886 case DELETE_EXPR:
7887 case VEC_DELETE_EXPR:
7888 case THROW_EXPR:
7889 case MODIFY_EXPR:
7890 case MODOP_EXPR:
7891 /* GCC internal stuff. */
7892 case VA_ARG_EXPR:
7893 case OBJ_TYPE_REF:
7894 case WITH_CLEANUP_EXPR:
7895 case STATEMENT_LIST:
7896 case BIND_EXPR:
7897 case NON_DEPENDENT_EXPR:
7898 case BASELINK:
7899 case EXPR_STMT:
7900 case OFFSET_REF:
7901 if (!allow_non_constant)
7902 error_at (EXPR_LOC_OR_HERE (t),
7903 "expression %qE is not a constant-expression", t);
7904 *non_constant_p = true;
7905 break;
7907 default:
7908 internal_error ("unexpected expression %qE of kind %s", t,
7909 tree_code_name[TREE_CODE (t)]);
7910 *non_constant_p = true;
7911 break;
7914 if (r == error_mark_node)
7915 *non_constant_p = true;
7917 if (*non_constant_p)
7918 return t;
7919 else
7920 return r;
7923 static tree
7924 cxx_eval_outermost_constant_expr (tree t, bool allow_non_constant)
7926 bool non_constant_p = false;
7927 tree r = cxx_eval_constant_expression (NULL, t, allow_non_constant,
7928 false, &non_constant_p);
7930 verify_constant (r, allow_non_constant, &non_constant_p);
7932 if (TREE_CODE (t) != CONSTRUCTOR
7933 && cp_has_mutable_p (TREE_TYPE (t)))
7935 /* We allow a mutable type if the original expression was a
7936 CONSTRUCTOR so that we can do aggregate initialization of
7937 constexpr variables. */
7938 if (!allow_non_constant)
7939 error ("%qT cannot be the type of a complete constant expression "
7940 "because it has mutable sub-objects", TREE_TYPE (t));
7941 non_constant_p = true;
7944 /* Technically we should check this for all subexpressions, but that
7945 runs into problems with our internal representation of pointer
7946 subtraction and the 5.19 rules are still in flux. */
7947 if (CONVERT_EXPR_CODE_P (TREE_CODE (r))
7948 && ARITHMETIC_TYPE_P (TREE_TYPE (r))
7949 && TREE_CODE (TREE_OPERAND (r, 0)) == ADDR_EXPR)
7951 if (!allow_non_constant)
7952 error ("conversion from pointer type %qT "
7953 "to arithmetic type %qT in a constant-expression",
7954 TREE_TYPE (TREE_OPERAND (r, 0)), TREE_TYPE (r));
7955 non_constant_p = true;
7958 if (non_constant_p && !allow_non_constant)
7959 return error_mark_node;
7960 else if (non_constant_p && TREE_CONSTANT (t))
7962 /* This isn't actually constant, so unset TREE_CONSTANT. */
7963 if (EXPR_P (t) || TREE_CODE (t) == CONSTRUCTOR)
7964 r = copy_node (t);
7965 else
7966 r = build_nop (TREE_TYPE (t), t);
7967 TREE_CONSTANT (r) = false;
7968 return r;
7970 else if (non_constant_p || r == t)
7971 return t;
7972 else if (TREE_CODE (r) == CONSTRUCTOR && CLASS_TYPE_P (TREE_TYPE (r)))
7974 if (TREE_CODE (t) == TARGET_EXPR
7975 && TARGET_EXPR_INITIAL (t) == r)
7976 return t;
7977 else
7979 r = get_target_expr (r);
7980 TREE_CONSTANT (r) = true;
7981 return r;
7984 else
7985 return r;
7988 /* Returns true if T is a valid subexpression of a constant expression,
7989 even if it isn't itself a constant expression. */
7991 bool
7992 is_sub_constant_expr (tree t)
7994 bool non_constant_p = false;
7995 cxx_eval_constant_expression (NULL, t, true, false, &non_constant_p);
7996 return !non_constant_p;
7999 /* If T represents a constant expression returns its reduced value.
8000 Otherwise return error_mark_node. If T is dependent, then
8001 return NULL. */
8003 tree
8004 cxx_constant_value (tree t)
8006 return cxx_eval_outermost_constant_expr (t, false);
8009 /* If T is a constant expression, returns its reduced value.
8010 Otherwise, if T does not have TREE_CONSTANT set, returns T.
8011 Otherwise, returns a version of T without TREE_CONSTANT. */
8013 tree
8014 maybe_constant_value (tree t)
8016 tree r;
8018 if (type_dependent_expression_p (t)
8019 || type_unknown_p (t)
8020 || BRACE_ENCLOSED_INITIALIZER_P (t)
8021 || !potential_constant_expression (t)
8022 || value_dependent_expression_p (t))
8024 if (TREE_OVERFLOW_P (t))
8026 t = build_nop (TREE_TYPE (t), t);
8027 TREE_CONSTANT (t) = false;
8029 return t;
8032 r = cxx_eval_outermost_constant_expr (t, true);
8033 #ifdef ENABLE_CHECKING
8034 /* cp_tree_equal looks through NOPs, so allow them. */
8035 gcc_assert (r == t
8036 || CONVERT_EXPR_P (t)
8037 || (TREE_CONSTANT (t) && !TREE_CONSTANT (r))
8038 || !cp_tree_equal (r, t));
8039 #endif
8040 return r;
8043 /* Like maybe_constant_value, but returns a CONSTRUCTOR directly, rather
8044 than wrapped in a TARGET_EXPR. */
8046 tree
8047 maybe_constant_init (tree t)
8049 t = maybe_constant_value (t);
8050 if (TREE_CODE (t) == TARGET_EXPR)
8052 tree init = TARGET_EXPR_INITIAL (t);
8053 if (TREE_CODE (init) == CONSTRUCTOR
8054 && TREE_CONSTANT (init))
8055 t = init;
8057 return t;
8060 #if 0
8061 /* FIXME see ADDR_EXPR section in potential_constant_expression_1. */
8062 /* Return true if the object referred to by REF has automatic or thread
8063 local storage. */
8065 enum { ck_ok, ck_bad, ck_unknown };
8066 static int
8067 check_automatic_or_tls (tree ref)
8069 enum machine_mode mode;
8070 HOST_WIDE_INT bitsize, bitpos;
8071 tree offset;
8072 int volatilep = 0, unsignedp = 0;
8073 tree decl = get_inner_reference (ref, &bitsize, &bitpos, &offset,
8074 &mode, &unsignedp, &volatilep, false);
8075 duration_kind dk;
8077 /* If there isn't a decl in the middle, we don't know the linkage here,
8078 and this isn't a constant expression anyway. */
8079 if (!DECL_P (decl))
8080 return ck_unknown;
8081 dk = decl_storage_duration (decl);
8082 return (dk == dk_auto || dk == dk_thread) ? ck_bad : ck_ok;
8084 #endif
8086 /* Return true if T denotes a potentially constant expression. Issue
8087 diagnostic as appropriate under control of FLAGS. If WANT_RVAL is true,
8088 an lvalue-rvalue conversion is implied.
8090 C++0x [expr.const] used to say
8092 6 An expression is a potential constant expression if it is
8093 a constant expression where all occurences of function
8094 parameters are replaced by arbitrary constant expressions
8095 of the appropriate type.
8097 2 A conditional expression is a constant expression unless it
8098 involves one of the following as a potentially evaluated
8099 subexpression (3.2), but subexpressions of logical AND (5.14),
8100 logical OR (5.15), and conditional (5.16) operations that are
8101 not evaluated are not considered. */
8103 static bool
8104 potential_constant_expression_1 (tree t, bool want_rval, tsubst_flags_t flags)
8106 enum { any = false, rval = true };
8107 int i;
8108 tree tmp;
8110 /* C++98 has different rules for the form of a constant expression that
8111 are enforced in the parser, so we can assume that anything that gets
8112 this far is suitable. */
8113 if (cxx_dialect < cxx0x)
8114 return true;
8116 if (t == error_mark_node)
8117 return false;
8118 if (t == NULL_TREE)
8119 return true;
8120 if (TREE_THIS_VOLATILE (t))
8122 if (flags & tf_error)
8123 error ("expression %qE has side-effects", t);
8124 return false;
8126 if (CONSTANT_CLASS_P (t))
8128 if (TREE_OVERFLOW (t))
8130 if (flags & tf_error)
8132 permerror (EXPR_LOC_OR_HERE (t),
8133 "overflow in constant expression");
8134 if (flag_permissive)
8135 return true;
8137 return false;
8139 return true;
8142 switch (TREE_CODE (t))
8144 case FUNCTION_DECL:
8145 case BASELINK:
8146 case TEMPLATE_DECL:
8147 case OVERLOAD:
8148 case TEMPLATE_ID_EXPR:
8149 case LABEL_DECL:
8150 case CONST_DECL:
8151 case SIZEOF_EXPR:
8152 case ALIGNOF_EXPR:
8153 case OFFSETOF_EXPR:
8154 case NOEXCEPT_EXPR:
8155 case TEMPLATE_PARM_INDEX:
8156 case TRAIT_EXPR:
8157 case IDENTIFIER_NODE:
8158 case USERDEF_LITERAL:
8159 /* We can see a FIELD_DECL in a pointer-to-member expression. */
8160 case FIELD_DECL:
8161 case PARM_DECL:
8162 case USING_DECL:
8163 return true;
8165 case AGGR_INIT_EXPR:
8166 case CALL_EXPR:
8167 /* -- an invocation of a function other than a constexpr function
8168 or a constexpr constructor. */
8170 tree fun = get_function_named_in_call (t);
8171 const int nargs = call_expr_nargs (t);
8172 i = 0;
8174 if (is_overloaded_fn (fun))
8176 if (TREE_CODE (fun) == FUNCTION_DECL)
8178 if (builtin_valid_in_constant_expr_p (fun))
8179 return true;
8180 if (!DECL_DECLARED_CONSTEXPR_P (fun)
8181 /* Allow any built-in function; if the expansion
8182 isn't constant, we'll deal with that then. */
8183 && !is_builtin_fn (fun))
8185 if (flags & tf_error)
8187 error_at (EXPR_LOC_OR_HERE (t),
8188 "call to non-constexpr function %qD", fun);
8189 explain_invalid_constexpr_fn (fun);
8191 return false;
8193 /* A call to a non-static member function takes the address
8194 of the object as the first argument. But in a constant
8195 expression the address will be folded away, so look
8196 through it now. */
8197 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fun)
8198 && !DECL_CONSTRUCTOR_P (fun))
8200 tree x = get_nth_callarg (t, 0);
8201 if (is_this_parameter (x))
8203 if (DECL_CONSTRUCTOR_P (DECL_CONTEXT (x)))
8205 if (flags & tf_error)
8206 sorry ("calling a member function of the "
8207 "object being constructed in a constant "
8208 "expression");
8209 return false;
8211 /* Otherwise OK. */;
8213 else if (!potential_constant_expression_1 (x, rval, flags))
8214 return false;
8215 i = 1;
8218 else
8219 fun = get_first_fn (fun);
8220 /* Skip initial arguments to base constructors. */
8221 if (DECL_BASE_CONSTRUCTOR_P (fun))
8222 i = num_artificial_parms_for (fun);
8223 fun = DECL_ORIGIN (fun);
8225 else
8227 if (potential_constant_expression_1 (fun, rval, flags))
8228 /* Might end up being a constant function pointer. */;
8229 else
8230 return false;
8232 for (; i < nargs; ++i)
8234 tree x = get_nth_callarg (t, i);
8235 if (!potential_constant_expression_1 (x, rval, flags))
8236 return false;
8238 return true;
8241 case NON_LVALUE_EXPR:
8242 /* -- an lvalue-to-rvalue conversion (4.1) unless it is applied to
8243 -- an lvalue of integral type that refers to a non-volatile
8244 const variable or static data member initialized with
8245 constant expressions, or
8247 -- an lvalue of literal type that refers to non-volatile
8248 object defined with constexpr, or that refers to a
8249 sub-object of such an object; */
8250 return potential_constant_expression_1 (TREE_OPERAND (t, 0), rval, flags);
8252 case VAR_DECL:
8253 if (want_rval && !decl_constant_var_p (t)
8254 && !dependent_type_p (TREE_TYPE (t)))
8256 if (flags & tf_error)
8257 non_const_var_error (t);
8258 return false;
8260 return true;
8262 case NOP_EXPR:
8263 case CONVERT_EXPR:
8264 case VIEW_CONVERT_EXPR:
8265 /* -- a reinterpret_cast. FIXME not implemented, and this rule
8266 may change to something more specific to type-punning (DR 1312). */
8268 tree from = TREE_OPERAND (t, 0);
8269 return (potential_constant_expression_1
8270 (from, TREE_CODE (t) != VIEW_CONVERT_EXPR, flags));
8273 case ADDR_EXPR:
8274 /* -- a unary operator & that is applied to an lvalue that
8275 designates an object with thread or automatic storage
8276 duration; */
8277 t = TREE_OPERAND (t, 0);
8278 #if 0
8279 /* FIXME adjust when issue 1197 is fully resolved. For now don't do
8280 any checking here, as we might dereference the pointer later. If
8281 we remove this code, also remove check_automatic_or_tls. */
8282 i = check_automatic_or_tls (t);
8283 if (i == ck_ok)
8284 return true;
8285 if (i == ck_bad)
8287 if (flags & tf_error)
8288 error ("address-of an object %qE with thread local or "
8289 "automatic storage is not a constant expression", t);
8290 return false;
8292 #endif
8293 return potential_constant_expression_1 (t, any, flags);
8295 case COMPONENT_REF:
8296 case BIT_FIELD_REF:
8297 case ARROW_EXPR:
8298 case OFFSET_REF:
8299 /* -- a class member access unless its postfix-expression is
8300 of literal type or of pointer to literal type. */
8301 /* This test would be redundant, as it follows from the
8302 postfix-expression being a potential constant expression. */
8303 return potential_constant_expression_1 (TREE_OPERAND (t, 0),
8304 want_rval, flags);
8306 case EXPR_PACK_EXPANSION:
8307 return potential_constant_expression_1 (PACK_EXPANSION_PATTERN (t),
8308 want_rval, flags);
8310 case INDIRECT_REF:
8312 tree x = TREE_OPERAND (t, 0);
8313 STRIP_NOPS (x);
8314 if (is_this_parameter (x))
8316 if (want_rval && DECL_CONTEXT (x)
8317 && DECL_CONSTRUCTOR_P (DECL_CONTEXT (x)))
8319 if (flags & tf_error)
8320 sorry ("use of the value of the object being constructed "
8321 "in a constant expression");
8322 return false;
8324 return true;
8326 return potential_constant_expression_1 (x, rval, flags);
8329 case LAMBDA_EXPR:
8330 case DYNAMIC_CAST_EXPR:
8331 case PSEUDO_DTOR_EXPR:
8332 case PREINCREMENT_EXPR:
8333 case POSTINCREMENT_EXPR:
8334 case PREDECREMENT_EXPR:
8335 case POSTDECREMENT_EXPR:
8336 case NEW_EXPR:
8337 case VEC_NEW_EXPR:
8338 case DELETE_EXPR:
8339 case VEC_DELETE_EXPR:
8340 case THROW_EXPR:
8341 case MODIFY_EXPR:
8342 case MODOP_EXPR:
8343 /* GCC internal stuff. */
8344 case VA_ARG_EXPR:
8345 case OBJ_TYPE_REF:
8346 case WITH_CLEANUP_EXPR:
8347 case CLEANUP_POINT_EXPR:
8348 case MUST_NOT_THROW_EXPR:
8349 case TRY_CATCH_EXPR:
8350 case STATEMENT_LIST:
8351 /* Don't bother trying to define a subset of statement-expressions to
8352 be constant-expressions, at least for now. */
8353 case STMT_EXPR:
8354 case EXPR_STMT:
8355 case BIND_EXPR:
8356 case TRANSACTION_EXPR:
8357 case IF_STMT:
8358 case DO_STMT:
8359 case FOR_STMT:
8360 case WHILE_STMT:
8361 if (flags & tf_error)
8362 error ("expression %qE is not a constant-expression", t);
8363 return false;
8365 case TYPEID_EXPR:
8366 /* -- a typeid expression whose operand is of polymorphic
8367 class type; */
8369 tree e = TREE_OPERAND (t, 0);
8370 if (!TYPE_P (e) && !type_dependent_expression_p (e)
8371 && TYPE_POLYMORPHIC_P (TREE_TYPE (e)))
8373 if (flags & tf_error)
8374 error ("typeid-expression is not a constant expression "
8375 "because %qE is of polymorphic type", e);
8376 return false;
8378 return true;
8381 case MINUS_EXPR:
8382 /* -- a subtraction where both operands are pointers. */
8383 if (TYPE_PTR_P (TREE_OPERAND (t, 0))
8384 && TYPE_PTR_P (TREE_OPERAND (t, 1)))
8386 if (flags & tf_error)
8387 error ("difference of two pointer expressions is not "
8388 "a constant expression");
8389 return false;
8391 want_rval = true;
8392 goto binary;
8394 case LT_EXPR:
8395 case LE_EXPR:
8396 case GT_EXPR:
8397 case GE_EXPR:
8398 case EQ_EXPR:
8399 case NE_EXPR:
8400 /* -- a relational or equality operator where at least
8401 one of the operands is a pointer. */
8402 if (TYPE_PTR_P (TREE_OPERAND (t, 0))
8403 || TYPE_PTR_P (TREE_OPERAND (t, 1)))
8405 if (flags & tf_error)
8406 error ("pointer comparison expression is not a "
8407 "constant expression");
8408 return false;
8410 want_rval = true;
8411 goto binary;
8413 case BIT_NOT_EXPR:
8414 /* A destructor. */
8415 if (TYPE_P (TREE_OPERAND (t, 0)))
8416 return true;
8417 /* else fall through. */
8419 case REALPART_EXPR:
8420 case IMAGPART_EXPR:
8421 case CONJ_EXPR:
8422 case SAVE_EXPR:
8423 case FIX_TRUNC_EXPR:
8424 case FLOAT_EXPR:
8425 case NEGATE_EXPR:
8426 case ABS_EXPR:
8427 case TRUTH_NOT_EXPR:
8428 case FIXED_CONVERT_EXPR:
8429 case UNARY_PLUS_EXPR:
8430 return potential_constant_expression_1 (TREE_OPERAND (t, 0), rval,
8431 flags);
8433 case CAST_EXPR:
8434 case CONST_CAST_EXPR:
8435 case STATIC_CAST_EXPR:
8436 case REINTERPRET_CAST_EXPR:
8437 case IMPLICIT_CONV_EXPR:
8438 return (potential_constant_expression_1
8439 (TREE_OPERAND (t, 0),
8440 TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE, flags));
8442 case PAREN_EXPR:
8443 case NON_DEPENDENT_EXPR:
8444 /* For convenience. */
8445 case RETURN_EXPR:
8446 return potential_constant_expression_1 (TREE_OPERAND (t, 0),
8447 want_rval, flags);
8449 case SCOPE_REF:
8450 return potential_constant_expression_1 (TREE_OPERAND (t, 1),
8451 want_rval, flags);
8453 case TARGET_EXPR:
8454 if (!literal_type_p (TREE_TYPE (t)))
8456 if (flags & tf_error)
8458 error ("temporary of non-literal type %qT in a "
8459 "constant expression", TREE_TYPE (t));
8460 explain_non_literal_class (TREE_TYPE (t));
8462 return false;
8464 case INIT_EXPR:
8465 return potential_constant_expression_1 (TREE_OPERAND (t, 1),
8466 rval, flags);
8468 case CONSTRUCTOR:
8470 VEC(constructor_elt, gc) *v = CONSTRUCTOR_ELTS (t);
8471 constructor_elt *ce;
8472 for (i = 0; VEC_iterate (constructor_elt, v, i, ce); ++i)
8473 if (!potential_constant_expression_1 (ce->value, want_rval, flags))
8474 return false;
8475 return true;
8478 case TREE_LIST:
8480 gcc_assert (TREE_PURPOSE (t) == NULL_TREE
8481 || DECL_P (TREE_PURPOSE (t)));
8482 if (!potential_constant_expression_1 (TREE_VALUE (t), want_rval,
8483 flags))
8484 return false;
8485 if (TREE_CHAIN (t) == NULL_TREE)
8486 return true;
8487 return potential_constant_expression_1 (TREE_CHAIN (t), want_rval,
8488 flags);
8491 case TRUNC_DIV_EXPR:
8492 case CEIL_DIV_EXPR:
8493 case FLOOR_DIV_EXPR:
8494 case ROUND_DIV_EXPR:
8495 case TRUNC_MOD_EXPR:
8496 case CEIL_MOD_EXPR:
8497 case ROUND_MOD_EXPR:
8499 tree denom = TREE_OPERAND (t, 1);
8500 /* We can't call maybe_constant_value on an expression
8501 that hasn't been through fold_non_dependent_expr yet. */
8502 if (!processing_template_decl)
8503 denom = maybe_constant_value (denom);
8504 if (integer_zerop (denom))
8506 if (flags & tf_error)
8507 error ("division by zero is not a constant-expression");
8508 return false;
8510 else
8512 want_rval = true;
8513 goto binary;
8517 case COMPOUND_EXPR:
8519 /* check_return_expr sometimes wraps a TARGET_EXPR in a
8520 COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR
8521 introduced by build_call_a. */
8522 tree op0 = TREE_OPERAND (t, 0);
8523 tree op1 = TREE_OPERAND (t, 1);
8524 STRIP_NOPS (op1);
8525 if ((TREE_CODE (op0) == TARGET_EXPR && op1 == TARGET_EXPR_SLOT (op0))
8526 || TREE_CODE (op1) == EMPTY_CLASS_EXPR)
8527 return potential_constant_expression_1 (op0, want_rval, flags);
8528 else
8529 goto binary;
8532 /* If the first operand is the non-short-circuit constant, look at
8533 the second operand; otherwise we only care about the first one for
8534 potentiality. */
8535 case TRUTH_AND_EXPR:
8536 case TRUTH_ANDIF_EXPR:
8537 tmp = boolean_true_node;
8538 goto truth;
8539 case TRUTH_OR_EXPR:
8540 case TRUTH_ORIF_EXPR:
8541 tmp = boolean_false_node;
8542 truth:
8544 tree op = TREE_OPERAND (t, 0);
8545 if (!potential_constant_expression_1 (op, rval, flags))
8546 return false;
8547 if (!processing_template_decl)
8548 op = maybe_constant_value (op);
8549 if (tree_int_cst_equal (op, tmp))
8550 return potential_constant_expression_1 (TREE_OPERAND (t, 1), rval, flags);
8551 else
8552 return true;
8555 case PLUS_EXPR:
8556 case MULT_EXPR:
8557 case POINTER_PLUS_EXPR:
8558 case RDIV_EXPR:
8559 case EXACT_DIV_EXPR:
8560 case MIN_EXPR:
8561 case MAX_EXPR:
8562 case LSHIFT_EXPR:
8563 case RSHIFT_EXPR:
8564 case LROTATE_EXPR:
8565 case RROTATE_EXPR:
8566 case BIT_IOR_EXPR:
8567 case BIT_XOR_EXPR:
8568 case BIT_AND_EXPR:
8569 case TRUTH_XOR_EXPR:
8570 case UNORDERED_EXPR:
8571 case ORDERED_EXPR:
8572 case UNLT_EXPR:
8573 case UNLE_EXPR:
8574 case UNGT_EXPR:
8575 case UNGE_EXPR:
8576 case UNEQ_EXPR:
8577 case LTGT_EXPR:
8578 case RANGE_EXPR:
8579 case COMPLEX_EXPR:
8580 want_rval = true;
8581 /* Fall through. */
8582 case ARRAY_REF:
8583 case ARRAY_RANGE_REF:
8584 case MEMBER_REF:
8585 case DOTSTAR_EXPR:
8586 binary:
8587 for (i = 0; i < 2; ++i)
8588 if (!potential_constant_expression_1 (TREE_OPERAND (t, i),
8589 want_rval, flags))
8590 return false;
8591 return true;
8593 case FMA_EXPR:
8594 case VEC_PERM_EXPR:
8595 for (i = 0; i < 3; ++i)
8596 if (!potential_constant_expression_1 (TREE_OPERAND (t, i),
8597 true, flags))
8598 return false;
8599 return true;
8601 case COND_EXPR:
8602 case VEC_COND_EXPR:
8603 /* If the condition is a known constant, we know which of the legs we
8604 care about; otherwise we only require that the condition and
8605 either of the legs be potentially constant. */
8606 tmp = TREE_OPERAND (t, 0);
8607 if (!potential_constant_expression_1 (tmp, rval, flags))
8608 return false;
8609 if (!processing_template_decl)
8610 tmp = maybe_constant_value (tmp);
8611 if (integer_zerop (tmp))
8612 return potential_constant_expression_1 (TREE_OPERAND (t, 2),
8613 want_rval, flags);
8614 else if (TREE_CODE (tmp) == INTEGER_CST)
8615 return potential_constant_expression_1 (TREE_OPERAND (t, 1),
8616 want_rval, flags);
8617 for (i = 1; i < 3; ++i)
8618 if (potential_constant_expression_1 (TREE_OPERAND (t, i),
8619 want_rval, tf_none))
8620 return true;
8621 if (flags & tf_error)
8622 error ("expression %qE is not a constant-expression", t);
8623 return false;
8625 case VEC_INIT_EXPR:
8626 if (VEC_INIT_EXPR_IS_CONSTEXPR (t))
8627 return true;
8628 if (flags & tf_error)
8630 error ("non-constant array initialization");
8631 diagnose_non_constexpr_vec_init (t);
8633 return false;
8635 default:
8636 sorry ("unexpected AST of kind %s", tree_code_name[TREE_CODE (t)]);
8637 gcc_unreachable();
8638 return false;
8642 /* The main entry point to the above. */
8644 bool
8645 potential_constant_expression (tree t)
8647 return potential_constant_expression_1 (t, false, tf_none);
8650 /* As above, but require a constant rvalue. */
8652 bool
8653 potential_rvalue_constant_expression (tree t)
8655 return potential_constant_expression_1 (t, true, tf_none);
8658 /* Like above, but complain about non-constant expressions. */
8660 bool
8661 require_potential_constant_expression (tree t)
8663 return potential_constant_expression_1 (t, false, tf_warning_or_error);
8666 /* Cross product of the above. */
8668 bool
8669 require_potential_rvalue_constant_expression (tree t)
8671 return potential_constant_expression_1 (t, true, tf_warning_or_error);
8674 /* Constructor for a lambda expression. */
8676 tree
8677 build_lambda_expr (void)
8679 tree lambda = make_node (LAMBDA_EXPR);
8680 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda) = CPLD_NONE;
8681 LAMBDA_EXPR_CAPTURE_LIST (lambda) = NULL_TREE;
8682 LAMBDA_EXPR_THIS_CAPTURE (lambda) = NULL_TREE;
8683 LAMBDA_EXPR_PENDING_PROXIES (lambda) = NULL;
8684 LAMBDA_EXPR_RETURN_TYPE (lambda) = NULL_TREE;
8685 LAMBDA_EXPR_MUTABLE_P (lambda) = false;
8686 return lambda;
8689 /* Create the closure object for a LAMBDA_EXPR. */
8691 tree
8692 build_lambda_object (tree lambda_expr)
8694 /* Build aggregate constructor call.
8695 - cp_parser_braced_list
8696 - cp_parser_functional_cast */
8697 VEC(constructor_elt,gc) *elts = NULL;
8698 tree node, expr, type;
8699 location_t saved_loc;
8701 if (processing_template_decl)
8702 return lambda_expr;
8704 /* Make sure any error messages refer to the lambda-introducer. */
8705 saved_loc = input_location;
8706 input_location = LAMBDA_EXPR_LOCATION (lambda_expr);
8708 for (node = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr);
8709 node;
8710 node = TREE_CHAIN (node))
8712 tree field = TREE_PURPOSE (node);
8713 tree val = TREE_VALUE (node);
8715 if (field == error_mark_node)
8717 expr = error_mark_node;
8718 goto out;
8721 if (DECL_P (val))
8722 mark_used (val);
8724 /* Mere mortals can't copy arrays with aggregate initialization, so
8725 do some magic to make it work here. */
8726 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE)
8727 val = build_array_copy (val);
8728 else if (DECL_NORMAL_CAPTURE_P (field)
8729 && TREE_CODE (TREE_TYPE (field)) != REFERENCE_TYPE)
8731 /* "the entities that are captured by copy are used to
8732 direct-initialize each corresponding non-static data
8733 member of the resulting closure object."
8735 There's normally no way to express direct-initialization
8736 from an element of a CONSTRUCTOR, so we build up a special
8737 TARGET_EXPR to bypass the usual copy-initialization. */
8738 val = force_rvalue (val, tf_warning_or_error);
8739 if (TREE_CODE (val) == TARGET_EXPR)
8740 TARGET_EXPR_DIRECT_INIT_P (val) = true;
8743 CONSTRUCTOR_APPEND_ELT (elts, DECL_NAME (field), val);
8746 expr = build_constructor (init_list_type_node, elts);
8747 CONSTRUCTOR_IS_DIRECT_INIT (expr) = 1;
8749 /* N2927: "[The closure] class type is not an aggregate."
8750 But we briefly treat it as an aggregate to make this simpler. */
8751 type = LAMBDA_EXPR_CLOSURE (lambda_expr);
8752 CLASSTYPE_NON_AGGREGATE (type) = 0;
8753 expr = finish_compound_literal (type, expr, tf_warning_or_error);
8754 CLASSTYPE_NON_AGGREGATE (type) = 1;
8756 out:
8757 input_location = saved_loc;
8758 return expr;
8761 /* Return an initialized RECORD_TYPE for LAMBDA.
8762 LAMBDA must have its explicit captures already. */
8764 tree
8765 begin_lambda_type (tree lambda)
8767 tree type;
8770 /* Unique name. This is just like an unnamed class, but we cannot use
8771 make_anon_name because of certain checks against TYPE_ANONYMOUS_P. */
8772 tree name;
8773 name = make_lambda_name ();
8775 /* Create the new RECORD_TYPE for this lambda. */
8776 type = xref_tag (/*tag_code=*/record_type,
8777 name,
8778 /*scope=*/ts_within_enclosing_non_class,
8779 /*template_header_p=*/false);
8782 /* Designate it as a struct so that we can use aggregate initialization. */
8783 CLASSTYPE_DECLARED_CLASS (type) = false;
8785 /* Cross-reference the expression and the type. */
8786 LAMBDA_EXPR_CLOSURE (lambda) = type;
8787 CLASSTYPE_LAMBDA_EXPR (type) = lambda;
8789 /* Clear base types. */
8790 xref_basetypes (type, /*bases=*/NULL_TREE);
8792 /* Start the class. */
8793 type = begin_class_definition (type);
8794 if (type == error_mark_node)
8795 return error_mark_node;
8797 return type;
8800 /* Returns the type to use for the return type of the operator() of a
8801 closure class. */
8803 tree
8804 lambda_return_type (tree expr)
8806 if (expr == NULL_TREE)
8807 return void_type_node;
8808 if (type_unknown_p (expr)
8809 || BRACE_ENCLOSED_INITIALIZER_P (expr))
8811 cxx_incomplete_type_error (expr, TREE_TYPE (expr));
8812 return void_type_node;
8814 gcc_checking_assert (!type_dependent_expression_p (expr));
8815 return cv_unqualified (type_decays_to (unlowered_expr_type (expr)));
8818 /* Given a LAMBDA_EXPR or closure type LAMBDA, return the op() of the
8819 closure type. */
8821 tree
8822 lambda_function (tree lambda)
8824 tree type;
8825 if (TREE_CODE (lambda) == LAMBDA_EXPR)
8826 type = LAMBDA_EXPR_CLOSURE (lambda);
8827 else
8828 type = lambda;
8829 gcc_assert (LAMBDA_TYPE_P (type));
8830 /* Don't let debug_tree cause instantiation. */
8831 if (CLASSTYPE_TEMPLATE_INSTANTIATION (type)
8832 && !COMPLETE_OR_OPEN_TYPE_P (type))
8833 return NULL_TREE;
8834 lambda = lookup_member (type, ansi_opname (CALL_EXPR),
8835 /*protect=*/0, /*want_type=*/false,
8836 tf_warning_or_error);
8837 if (lambda)
8838 lambda = BASELINK_FUNCTIONS (lambda);
8839 return lambda;
8842 /* Returns the type to use for the FIELD_DECL corresponding to the
8843 capture of EXPR.
8844 The caller should add REFERENCE_TYPE for capture by reference. */
8846 tree
8847 lambda_capture_field_type (tree expr)
8849 tree type;
8850 if (type_dependent_expression_p (expr))
8852 type = cxx_make_type (DECLTYPE_TYPE);
8853 DECLTYPE_TYPE_EXPR (type) = expr;
8854 DECLTYPE_FOR_LAMBDA_CAPTURE (type) = true;
8855 SET_TYPE_STRUCTURAL_EQUALITY (type);
8857 else
8858 type = non_reference (unlowered_expr_type (expr));
8859 return type;
8862 /* Insert the deduced return type for an auto function. */
8864 void
8865 apply_deduced_return_type (tree fco, tree return_type)
8867 tree result;
8869 if (return_type == error_mark_node)
8870 return;
8872 if (LAMBDA_FUNCTION_P (fco))
8874 tree lambda = CLASSTYPE_LAMBDA_EXPR (current_class_type);
8875 LAMBDA_EXPR_RETURN_TYPE (lambda) = return_type;
8878 if (DECL_CONV_FN_P (fco))
8879 DECL_NAME (fco) = mangle_conv_op_name_for_type (return_type);
8881 TREE_TYPE (fco) = change_return_type (return_type, TREE_TYPE (fco));
8883 result = DECL_RESULT (fco);
8884 if (result == NULL_TREE)
8885 return;
8886 if (TREE_TYPE (result) == return_type)
8887 return;
8889 /* We already have a DECL_RESULT from start_preparsed_function.
8890 Now we need to redo the work it and allocate_struct_function
8891 did to reflect the new type. */
8892 gcc_assert (current_function_decl == fco);
8893 result = build_decl (input_location, RESULT_DECL, NULL_TREE,
8894 TYPE_MAIN_VARIANT (return_type));
8895 DECL_ARTIFICIAL (result) = 1;
8896 DECL_IGNORED_P (result) = 1;
8897 cp_apply_type_quals_to_decl (cp_type_quals (return_type),
8898 result);
8900 DECL_RESULT (fco) = result;
8902 if (!processing_template_decl)
8904 bool aggr = aggregate_value_p (result, fco);
8905 #ifdef PCC_STATIC_STRUCT_RETURN
8906 cfun->returns_pcc_struct = aggr;
8907 #endif
8908 cfun->returns_struct = aggr;
8913 /* DECL is a local variable or parameter from the surrounding scope of a
8914 lambda-expression. Returns the decltype for a use of the capture field
8915 for DECL even if it hasn't been captured yet. */
8917 static tree
8918 capture_decltype (tree decl)
8920 tree lam = CLASSTYPE_LAMBDA_EXPR (DECL_CONTEXT (current_function_decl));
8921 /* FIXME do lookup instead of list walk? */
8922 tree cap = value_member (decl, LAMBDA_EXPR_CAPTURE_LIST (lam));
8923 tree type;
8925 if (cap)
8926 type = TREE_TYPE (TREE_PURPOSE (cap));
8927 else
8928 switch (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lam))
8930 case CPLD_NONE:
8931 error ("%qD is not captured", decl);
8932 return error_mark_node;
8934 case CPLD_COPY:
8935 type = TREE_TYPE (decl);
8936 if (TREE_CODE (type) == REFERENCE_TYPE
8937 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
8938 type = TREE_TYPE (type);
8939 break;
8941 case CPLD_REFERENCE:
8942 type = TREE_TYPE (decl);
8943 if (TREE_CODE (type) != REFERENCE_TYPE)
8944 type = build_reference_type (TREE_TYPE (decl));
8945 break;
8947 default:
8948 gcc_unreachable ();
8951 if (TREE_CODE (type) != REFERENCE_TYPE)
8953 if (!LAMBDA_EXPR_MUTABLE_P (lam))
8954 type = cp_build_qualified_type (type, (cp_type_quals (type)
8955 |TYPE_QUAL_CONST));
8956 type = build_reference_type (type);
8958 return type;
8961 /* Returns true iff DECL is a lambda capture proxy variable created by
8962 build_capture_proxy. */
8964 bool
8965 is_capture_proxy (tree decl)
8967 return (TREE_CODE (decl) == VAR_DECL
8968 && DECL_HAS_VALUE_EXPR_P (decl)
8969 && !DECL_ANON_UNION_VAR_P (decl)
8970 && LAMBDA_FUNCTION_P (DECL_CONTEXT (decl)));
8973 /* Returns true iff DECL is a capture proxy for a normal capture
8974 (i.e. without explicit initializer). */
8976 bool
8977 is_normal_capture_proxy (tree decl)
8979 tree val;
8981 if (!is_capture_proxy (decl))
8982 /* It's not a capture proxy. */
8983 return false;
8985 /* It is a capture proxy, is it a normal capture? */
8986 val = DECL_VALUE_EXPR (decl);
8987 gcc_assert (TREE_CODE (val) == COMPONENT_REF);
8988 val = TREE_OPERAND (val, 1);
8989 return DECL_NORMAL_CAPTURE_P (val);
8992 /* VAR is a capture proxy created by build_capture_proxy; add it to the
8993 current function, which is the operator() for the appropriate lambda. */
8995 void
8996 insert_capture_proxy (tree var)
8998 cp_binding_level *b;
8999 int skip;
9000 tree stmt_list;
9002 /* Put the capture proxy in the extra body block so that it won't clash
9003 with a later local variable. */
9004 b = current_binding_level;
9005 for (skip = 0; ; ++skip)
9007 cp_binding_level *n = b->level_chain;
9008 if (n->kind == sk_function_parms)
9009 break;
9010 b = n;
9012 pushdecl_with_scope (var, b, false);
9014 /* And put a DECL_EXPR in the STATEMENT_LIST for the same block. */
9015 var = build_stmt (DECL_SOURCE_LOCATION (var), DECL_EXPR, var);
9016 stmt_list = VEC_index (tree, stmt_list_stack,
9017 VEC_length (tree, stmt_list_stack) - 1 - skip);
9018 gcc_assert (stmt_list);
9019 append_to_statement_list_force (var, &stmt_list);
9022 /* We've just finished processing a lambda; if the containing scope is also
9023 a lambda, insert any capture proxies that were created while processing
9024 the nested lambda. */
9026 void
9027 insert_pending_capture_proxies (void)
9029 tree lam;
9030 VEC(tree,gc) *proxies;
9031 unsigned i;
9033 if (!current_function_decl || !LAMBDA_FUNCTION_P (current_function_decl))
9034 return;
9036 lam = CLASSTYPE_LAMBDA_EXPR (DECL_CONTEXT (current_function_decl));
9037 proxies = LAMBDA_EXPR_PENDING_PROXIES (lam);
9038 for (i = 0; i < VEC_length (tree, proxies); ++i)
9040 tree var = VEC_index (tree, proxies, i);
9041 insert_capture_proxy (var);
9043 release_tree_vector (LAMBDA_EXPR_PENDING_PROXIES (lam));
9044 LAMBDA_EXPR_PENDING_PROXIES (lam) = NULL;
9047 /* Given REF, a COMPONENT_REF designating a field in the lambda closure,
9048 return the type we want the proxy to have: the type of the field itself,
9049 with added const-qualification if the lambda isn't mutable and the
9050 capture is by value. */
9052 tree
9053 lambda_proxy_type (tree ref)
9055 tree type;
9056 if (REFERENCE_REF_P (ref))
9057 ref = TREE_OPERAND (ref, 0);
9058 type = TREE_TYPE (ref);
9059 if (!dependent_type_p (type))
9060 return type;
9061 type = cxx_make_type (DECLTYPE_TYPE);
9062 DECLTYPE_TYPE_EXPR (type) = ref;
9063 DECLTYPE_FOR_LAMBDA_PROXY (type) = true;
9064 SET_TYPE_STRUCTURAL_EQUALITY (type);
9065 return type;
9068 /* MEMBER is a capture field in a lambda closure class. Now that we're
9069 inside the operator(), build a placeholder var for future lookups and
9070 debugging. */
9072 tree
9073 build_capture_proxy (tree member)
9075 tree var, object, fn, closure, name, lam, type;
9077 closure = DECL_CONTEXT (member);
9078 fn = lambda_function (closure);
9079 lam = CLASSTYPE_LAMBDA_EXPR (closure);
9081 /* The proxy variable forwards to the capture field. */
9082 object = build_fold_indirect_ref (DECL_ARGUMENTS (fn));
9083 object = finish_non_static_data_member (member, object, NULL_TREE);
9084 if (REFERENCE_REF_P (object))
9085 object = TREE_OPERAND (object, 0);
9087 /* Remove the __ inserted by add_capture. */
9088 name = get_identifier (IDENTIFIER_POINTER (DECL_NAME (member)) + 2);
9090 type = lambda_proxy_type (object);
9091 var = build_decl (input_location, VAR_DECL, name, type);
9092 SET_DECL_VALUE_EXPR (var, object);
9093 DECL_HAS_VALUE_EXPR_P (var) = 1;
9094 DECL_ARTIFICIAL (var) = 1;
9095 TREE_USED (var) = 1;
9096 DECL_CONTEXT (var) = fn;
9098 if (name == this_identifier)
9100 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (lam) == member);
9101 LAMBDA_EXPR_THIS_CAPTURE (lam) = var;
9104 if (fn == current_function_decl)
9105 insert_capture_proxy (var);
9106 else
9107 VEC_safe_push (tree, gc, LAMBDA_EXPR_PENDING_PROXIES (lam), var);
9109 return var;
9112 /* From an ID and INITIALIZER, create a capture (by reference if
9113 BY_REFERENCE_P is true), add it to the capture-list for LAMBDA,
9114 and return it. */
9116 tree
9117 add_capture (tree lambda, tree id, tree initializer, bool by_reference_p,
9118 bool explicit_init_p)
9120 char *buf;
9121 tree type, member, name;
9123 type = lambda_capture_field_type (initializer);
9124 if (by_reference_p)
9126 type = build_reference_type (type);
9127 if (!real_lvalue_p (initializer))
9128 error ("cannot capture %qE by reference", initializer);
9130 else
9131 /* Capture by copy requires a complete type. */
9132 type = complete_type (type);
9134 /* Add __ to the beginning of the field name so that user code
9135 won't find the field with name lookup. We can't just leave the name
9136 unset because template instantiation uses the name to find
9137 instantiated fields. */
9138 buf = (char *) alloca (IDENTIFIER_LENGTH (id) + 3);
9139 buf[1] = buf[0] = '_';
9140 memcpy (buf + 2, IDENTIFIER_POINTER (id),
9141 IDENTIFIER_LENGTH (id) + 1);
9142 name = get_identifier (buf);
9144 /* If TREE_TYPE isn't set, we're still in the introducer, so check
9145 for duplicates. */
9146 if (!LAMBDA_EXPR_CLOSURE (lambda))
9148 if (IDENTIFIER_MARKED (name))
9150 pedwarn (input_location, 0,
9151 "already captured %qD in lambda expression", id);
9152 return NULL_TREE;
9154 IDENTIFIER_MARKED (name) = true;
9157 /* Make member variable. */
9158 member = build_lang_decl (FIELD_DECL, name, type);
9160 if (!explicit_init_p)
9161 /* Normal captures are invisible to name lookup but uses are replaced
9162 with references to the capture field; we implement this by only
9163 really making them invisible in unevaluated context; see
9164 qualify_lookup. For now, let's make explicitly initialized captures
9165 always visible. */
9166 DECL_NORMAL_CAPTURE_P (member) = true;
9168 if (id == this_identifier)
9169 LAMBDA_EXPR_THIS_CAPTURE (lambda) = member;
9171 /* Add it to the appropriate closure class if we've started it. */
9172 if (current_class_type
9173 && current_class_type == LAMBDA_EXPR_CLOSURE (lambda))
9174 finish_member_declaration (member);
9176 LAMBDA_EXPR_CAPTURE_LIST (lambda)
9177 = tree_cons (member, initializer, LAMBDA_EXPR_CAPTURE_LIST (lambda));
9179 if (LAMBDA_EXPR_CLOSURE (lambda))
9180 return build_capture_proxy (member);
9181 /* For explicit captures we haven't started the function yet, so we wait
9182 and build the proxy from cp_parser_lambda_body. */
9183 return NULL_TREE;
9186 /* Register all the capture members on the list CAPTURES, which is the
9187 LAMBDA_EXPR_CAPTURE_LIST for the lambda after the introducer. */
9189 void
9190 register_capture_members (tree captures)
9192 if (captures == NULL_TREE)
9193 return;
9195 register_capture_members (TREE_CHAIN (captures));
9196 /* We set this in add_capture to avoid duplicates. */
9197 IDENTIFIER_MARKED (DECL_NAME (TREE_PURPOSE (captures))) = false;
9198 finish_member_declaration (TREE_PURPOSE (captures));
9201 /* Similar to add_capture, except this works on a stack of nested lambdas.
9202 BY_REFERENCE_P in this case is derived from the default capture mode.
9203 Returns the capture for the lambda at the bottom of the stack. */
9205 tree
9206 add_default_capture (tree lambda_stack, tree id, tree initializer)
9208 bool this_capture_p = (id == this_identifier);
9210 tree var = NULL_TREE;
9212 tree saved_class_type = current_class_type;
9214 tree node;
9216 for (node = lambda_stack;
9217 node;
9218 node = TREE_CHAIN (node))
9220 tree lambda = TREE_VALUE (node);
9222 current_class_type = LAMBDA_EXPR_CLOSURE (lambda);
9223 var = add_capture (lambda,
9225 initializer,
9226 /*by_reference_p=*/
9227 (!this_capture_p
9228 && (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda)
9229 == CPLD_REFERENCE)),
9230 /*explicit_init_p=*/false);
9231 initializer = convert_from_reference (var);
9234 current_class_type = saved_class_type;
9236 return var;
9239 /* Return the capture pertaining to a use of 'this' in LAMBDA, in the form of an
9240 INDIRECT_REF, possibly adding it through default capturing. */
9242 tree
9243 lambda_expr_this_capture (tree lambda)
9245 tree result;
9247 tree this_capture = LAMBDA_EXPR_THIS_CAPTURE (lambda);
9249 /* Try to default capture 'this' if we can. */
9250 if (!this_capture
9251 && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda) != CPLD_NONE)
9253 tree containing_function = TYPE_CONTEXT (LAMBDA_EXPR_CLOSURE (lambda));
9254 tree lambda_stack = tree_cons (NULL_TREE, lambda, NULL_TREE);
9255 tree init = NULL_TREE;
9257 /* If we are in a lambda function, we can move out until we hit:
9258 1. a non-lambda function,
9259 2. a lambda function capturing 'this', or
9260 3. a non-default capturing lambda function. */
9261 while (LAMBDA_FUNCTION_P (containing_function))
9263 tree lambda
9264 = CLASSTYPE_LAMBDA_EXPR (DECL_CONTEXT (containing_function));
9266 if (LAMBDA_EXPR_THIS_CAPTURE (lambda))
9268 /* An outer lambda has already captured 'this'. */
9269 init = LAMBDA_EXPR_THIS_CAPTURE (lambda);
9270 break;
9273 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda) == CPLD_NONE)
9274 /* An outer lambda won't let us capture 'this'. */
9275 break;
9277 lambda_stack = tree_cons (NULL_TREE,
9278 lambda,
9279 lambda_stack);
9281 containing_function = decl_function_context (containing_function);
9284 if (!init && DECL_NONSTATIC_MEMBER_FUNCTION_P (containing_function)
9285 && !LAMBDA_FUNCTION_P (containing_function))
9286 /* First parameter is 'this'. */
9287 init = DECL_ARGUMENTS (containing_function);
9289 if (init)
9290 this_capture = add_default_capture (lambda_stack,
9291 /*id=*/this_identifier,
9292 init);
9295 if (!this_capture)
9297 error ("%<this%> was not captured for this lambda function");
9298 result = error_mark_node;
9300 else
9302 /* To make sure that current_class_ref is for the lambda. */
9303 gcc_assert (TYPE_MAIN_VARIANT (TREE_TYPE (current_class_ref))
9304 == LAMBDA_EXPR_CLOSURE (lambda));
9306 result = this_capture;
9308 /* If 'this' is captured, each use of 'this' is transformed into an
9309 access to the corresponding unnamed data member of the closure
9310 type cast (_expr.cast_ 5.4) to the type of 'this'. [ The cast
9311 ensures that the transformed expression is an rvalue. ] */
9312 result = rvalue (result);
9315 return result;
9318 /* Returns the method basetype of the innermost non-lambda function, or
9319 NULL_TREE if none. */
9321 tree
9322 nonlambda_method_basetype (void)
9324 tree fn, type;
9325 if (!current_class_ref)
9326 return NULL_TREE;
9328 type = current_class_type;
9329 if (!LAMBDA_TYPE_P (type))
9330 return type;
9332 /* Find the nearest enclosing non-lambda function. */
9333 fn = TYPE_NAME (type);
9335 fn = decl_function_context (fn);
9336 while (fn && LAMBDA_FUNCTION_P (fn));
9338 if (!fn || !DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
9339 return NULL_TREE;
9341 return TYPE_METHOD_BASETYPE (TREE_TYPE (fn));
9344 /* If the closure TYPE has a static op(), also add a conversion to function
9345 pointer. */
9347 void
9348 maybe_add_lambda_conv_op (tree type)
9350 bool nested = (current_function_decl != NULL_TREE);
9351 tree callop = lambda_function (type);
9352 tree rettype, name, fntype, fn, body, compound_stmt;
9353 tree thistype, stattype, statfn, convfn, call, arg;
9354 VEC (tree, gc) *argvec;
9356 if (LAMBDA_EXPR_CAPTURE_LIST (CLASSTYPE_LAMBDA_EXPR (type)) != NULL_TREE)
9357 return;
9359 if (processing_template_decl)
9360 return;
9362 stattype = build_function_type (TREE_TYPE (TREE_TYPE (callop)),
9363 FUNCTION_ARG_CHAIN (callop));
9365 /* First build up the conversion op. */
9367 rettype = build_pointer_type (stattype);
9368 name = mangle_conv_op_name_for_type (rettype);
9369 thistype = cp_build_qualified_type (type, TYPE_QUAL_CONST);
9370 fntype = build_method_type_directly (thistype, rettype, void_list_node);
9371 fn = convfn = build_lang_decl (FUNCTION_DECL, name, fntype);
9372 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (callop);
9374 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_pfn
9375 && DECL_ALIGN (fn) < 2 * BITS_PER_UNIT)
9376 DECL_ALIGN (fn) = 2 * BITS_PER_UNIT;
9378 SET_OVERLOADED_OPERATOR_CODE (fn, TYPE_EXPR);
9379 grokclassfn (type, fn, NO_SPECIAL);
9380 set_linkage_according_to_type (type, fn);
9381 rest_of_decl_compilation (fn, toplevel_bindings_p (), at_eof);
9382 DECL_IN_AGGR_P (fn) = 1;
9383 DECL_ARTIFICIAL (fn) = 1;
9384 DECL_NOT_REALLY_EXTERN (fn) = 1;
9385 DECL_DECLARED_INLINE_P (fn) = 1;
9386 DECL_ARGUMENTS (fn) = build_this_parm (fntype, TYPE_QUAL_CONST);
9388 add_method (type, fn, NULL_TREE);
9390 /* Generic thunk code fails for varargs; we'll complain in mark_used if
9391 the conversion op is used. */
9392 if (varargs_function_p (callop))
9394 DECL_DELETED_FN (fn) = 1;
9395 return;
9398 /* Now build up the thunk to be returned. */
9400 name = get_identifier ("_FUN");
9401 fn = statfn = build_lang_decl (FUNCTION_DECL, name, stattype);
9402 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (callop);
9403 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_pfn
9404 && DECL_ALIGN (fn) < 2 * BITS_PER_UNIT)
9405 DECL_ALIGN (fn) = 2 * BITS_PER_UNIT;
9406 grokclassfn (type, fn, NO_SPECIAL);
9407 set_linkage_according_to_type (type, fn);
9408 rest_of_decl_compilation (fn, toplevel_bindings_p (), at_eof);
9409 DECL_IN_AGGR_P (fn) = 1;
9410 DECL_ARTIFICIAL (fn) = 1;
9411 DECL_NOT_REALLY_EXTERN (fn) = 1;
9412 DECL_DECLARED_INLINE_P (fn) = 1;
9413 DECL_STATIC_FUNCTION_P (fn) = 1;
9414 DECL_ARGUMENTS (fn) = copy_list (DECL_CHAIN (DECL_ARGUMENTS (callop)));
9415 for (arg = DECL_ARGUMENTS (fn); arg; arg = DECL_CHAIN (arg))
9416 DECL_CONTEXT (arg) = fn;
9418 add_method (type, fn, NULL_TREE);
9420 if (nested)
9421 push_function_context ();
9422 else
9423 /* Still increment function_depth so that we don't GC in the
9424 middle of an expression. */
9425 ++function_depth;
9427 /* Generate the body of the thunk. */
9429 start_preparsed_function (statfn, NULL_TREE,
9430 SF_PRE_PARSED | SF_INCLASS_INLINE);
9431 if (DECL_ONE_ONLY (statfn))
9433 /* Put the thunk in the same comdat group as the call op. */
9434 symtab_add_to_same_comdat_group
9435 ((symtab_node) cgraph_get_create_node (statfn),
9436 (symtab_node) cgraph_get_create_node (callop));
9438 body = begin_function_body ();
9439 compound_stmt = begin_compound_stmt (0);
9441 arg = build1 (NOP_EXPR, TREE_TYPE (DECL_ARGUMENTS (callop)),
9442 null_pointer_node);
9443 argvec = make_tree_vector ();
9444 VEC_quick_push (tree, argvec, arg);
9445 for (arg = DECL_ARGUMENTS (statfn); arg; arg = DECL_CHAIN (arg))
9447 mark_exp_read (arg);
9448 VEC_safe_push (tree, gc, argvec, arg);
9450 call = build_call_a (callop, VEC_length (tree, argvec),
9451 VEC_address (tree, argvec));
9452 CALL_FROM_THUNK_P (call) = 1;
9453 if (MAYBE_CLASS_TYPE_P (TREE_TYPE (call)))
9454 call = build_cplus_new (TREE_TYPE (call), call, tf_warning_or_error);
9455 call = convert_from_reference (call);
9456 finish_return_stmt (call);
9458 finish_compound_stmt (compound_stmt);
9459 finish_function_body (body);
9461 expand_or_defer_fn (finish_function (2));
9463 /* Generate the body of the conversion op. */
9465 start_preparsed_function (convfn, NULL_TREE,
9466 SF_PRE_PARSED | SF_INCLASS_INLINE);
9467 body = begin_function_body ();
9468 compound_stmt = begin_compound_stmt (0);
9470 finish_return_stmt (decay_conversion (statfn, tf_warning_or_error));
9472 finish_compound_stmt (compound_stmt);
9473 finish_function_body (body);
9475 expand_or_defer_fn (finish_function (2));
9477 if (nested)
9478 pop_function_context ();
9479 else
9480 --function_depth;
9483 /* Returns true iff VAL is a lambda-related declaration which should
9484 be ignored by unqualified lookup. */
9486 bool
9487 is_lambda_ignored_entity (tree val)
9489 /* In unevaluated context, look past normal capture proxies. */
9490 if (cp_unevaluated_operand && is_normal_capture_proxy (val))
9491 return true;
9493 /* Always ignore lambda fields, their names are only for debugging. */
9494 if (TREE_CODE (val) == FIELD_DECL
9495 && CLASSTYPE_LAMBDA_EXPR (DECL_CONTEXT (val)))
9496 return true;
9498 /* None of the lookups that use qualify_lookup want the op() from the
9499 lambda; they want the one from the enclosing class. */
9500 if (TREE_CODE (val) == FUNCTION_DECL && LAMBDA_FUNCTION_P (val))
9501 return true;
9503 return false;
9506 #include "gt-cp-semantics.h"