* semantics.c (finish_omp_threadprivate): Call complete_type.
[official-gcc.git] / gcc / cp / semantics.c
blob073ee97be9c5d5269c724cdfafb2a9bff59c2cc0
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-2012 Free Software Foundation, Inc.
7 Written by Mark Mitchell (mmitchell@usa.net) based on code found
8 formerly in parse.y and pt.c.
10 This file is part of GCC.
12 GCC is free software; you can redistribute it and/or modify it
13 under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 3, or (at your option)
15 any later version.
17 GCC is distributed in the hope that it will be useful, but
18 WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with GCC; see the file COPYING3. If not see
24 <http://www.gnu.org/licenses/>. */
26 #include "config.h"
27 #include "system.h"
28 #include "coretypes.h"
29 #include "tm.h"
30 #include "tree.h"
31 #include "cp-tree.h"
32 #include "c-family/c-common.h"
33 #include "c-family/c-objc.h"
34 #include "tree-inline.h"
35 #include "intl.h"
36 #include "toplev.h"
37 #include "flags.h"
38 #include "timevar.h"
39 #include "diagnostic.h"
40 #include "cgraph.h"
41 #include "tree-iterator.h"
42 #include "vec.h"
43 #include "target.h"
44 #include "gimple.h"
45 #include "bitmap.h"
46 #include "hash-table.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)
2174 if (warn_sizeof_pointer_memaccess
2175 && !VEC_empty(tree, *args)
2176 && !processing_template_decl)
2178 location_t sizeof_arg_loc[3];
2179 tree sizeof_arg[3];
2180 unsigned int i;
2181 for (i = 0; i < 3; i++)
2183 tree t;
2185 sizeof_arg_loc[i] = UNKNOWN_LOCATION;
2186 sizeof_arg[i] = NULL_TREE;
2187 if (i >= VEC_length (tree, *args))
2188 continue;
2189 t = VEC_index (tree, *args, i);
2190 if (TREE_CODE (t) != SIZEOF_EXPR)
2191 continue;
2192 if (SIZEOF_EXPR_TYPE_P (t))
2193 sizeof_arg[i] = TREE_TYPE (TREE_OPERAND (t, 0));
2194 else
2195 sizeof_arg[i] = TREE_OPERAND (t, 0);
2196 sizeof_arg_loc[i] = EXPR_LOCATION (t);
2198 sizeof_pointer_memaccess_warning
2199 (sizeof_arg_loc, fn, *args,
2200 sizeof_arg, same_type_ignoring_top_level_qualifiers_p);
2203 /* A call to a namespace-scope function. */
2204 result = build_new_function_call (fn, args, koenig_p, complain);
2207 else if (TREE_CODE (fn) == PSEUDO_DTOR_EXPR)
2209 if (!VEC_empty (tree, *args))
2210 error ("arguments to destructor are not allowed");
2211 /* Mark the pseudo-destructor call as having side-effects so
2212 that we do not issue warnings about its use. */
2213 result = build1 (NOP_EXPR,
2214 void_type_node,
2215 TREE_OPERAND (fn, 0));
2216 TREE_SIDE_EFFECTS (result) = 1;
2218 else if (CLASS_TYPE_P (TREE_TYPE (fn)))
2219 /* If the "function" is really an object of class type, it might
2220 have an overloaded `operator ()'. */
2221 result = build_op_call (fn, args, complain);
2223 if (!result)
2224 /* A call where the function is unknown. */
2225 result = cp_build_function_call_vec (fn, args, complain);
2227 if (processing_template_decl && result != error_mark_node)
2229 if (TREE_CODE (result) == INDIRECT_REF)
2230 result = TREE_OPERAND (result, 0);
2231 result = build_call_vec (TREE_TYPE (result), orig_fn, orig_args);
2232 SET_EXPR_LOCATION (result, input_location);
2233 KOENIG_LOOKUP_P (result) = koenig_p;
2234 release_tree_vector (orig_args);
2235 result = convert_from_reference (result);
2238 if (koenig_p)
2240 /* Free garbage OVERLOADs from arg-dependent lookup. */
2241 tree next = NULL_TREE;
2242 for (fn = orig_fn;
2243 fn && TREE_CODE (fn) == OVERLOAD && OVL_ARG_DEPENDENT (fn);
2244 fn = next)
2246 if (processing_template_decl)
2247 /* In a template, we'll re-use them at instantiation time. */
2248 OVL_ARG_DEPENDENT (fn) = false;
2249 else
2251 next = OVL_CHAIN (fn);
2252 ggc_free (fn);
2257 return result;
2260 /* Finish a call to a postfix increment or decrement or EXPR. (Which
2261 is indicated by CODE, which should be POSTINCREMENT_EXPR or
2262 POSTDECREMENT_EXPR.) */
2264 tree
2265 finish_increment_expr (tree expr, enum tree_code code)
2267 return build_x_unary_op (input_location, code, expr, tf_warning_or_error);
2270 /* Finish a use of `this'. Returns an expression for `this'. */
2272 tree
2273 finish_this_expr (void)
2275 tree result;
2277 if (current_class_ptr)
2279 tree type = TREE_TYPE (current_class_ref);
2281 /* In a lambda expression, 'this' refers to the captured 'this'. */
2282 if (LAMBDA_TYPE_P (type))
2283 result = lambda_expr_this_capture (CLASSTYPE_LAMBDA_EXPR (type));
2284 else
2285 result = current_class_ptr;
2288 else if (current_function_decl
2289 && DECL_STATIC_FUNCTION_P (current_function_decl))
2291 error ("%<this%> is unavailable for static member functions");
2292 result = error_mark_node;
2294 else
2296 if (current_function_decl)
2297 error ("invalid use of %<this%> in non-member function");
2298 else
2299 error ("invalid use of %<this%> at top level");
2300 result = error_mark_node;
2303 return result;
2306 /* Finish a pseudo-destructor expression. If SCOPE is NULL, the
2307 expression was of the form `OBJECT.~DESTRUCTOR' where DESTRUCTOR is
2308 the TYPE for the type given. If SCOPE is non-NULL, the expression
2309 was of the form `OBJECT.SCOPE::~DESTRUCTOR'. */
2311 tree
2312 finish_pseudo_destructor_expr (tree object, tree scope, tree destructor)
2314 if (object == error_mark_node || destructor == error_mark_node)
2315 return error_mark_node;
2317 gcc_assert (TYPE_P (destructor));
2319 if (!processing_template_decl)
2321 if (scope == error_mark_node)
2323 error ("invalid qualifying scope in pseudo-destructor name");
2324 return error_mark_node;
2326 if (scope && TYPE_P (scope) && !check_dtor_name (scope, destructor))
2328 error ("qualified type %qT does not match destructor name ~%qT",
2329 scope, destructor);
2330 return error_mark_node;
2334 /* [expr.pseudo] says both:
2336 The type designated by the pseudo-destructor-name shall be
2337 the same as the object type.
2339 and:
2341 The cv-unqualified versions of the object type and of the
2342 type designated by the pseudo-destructor-name shall be the
2343 same type.
2345 We implement the more generous second sentence, since that is
2346 what most other compilers do. */
2347 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (object),
2348 destructor))
2350 error ("%qE is not of type %qT", object, destructor);
2351 return error_mark_node;
2355 return build3 (PSEUDO_DTOR_EXPR, void_type_node, object, scope, destructor);
2358 /* Finish an expression of the form CODE EXPR. */
2360 tree
2361 finish_unary_op_expr (location_t loc, enum tree_code code, tree expr)
2363 tree result = build_x_unary_op (loc, code, expr, tf_warning_or_error);
2364 if (TREE_OVERFLOW_P (result) && !TREE_OVERFLOW_P (expr))
2365 overflow_warning (input_location, result);
2367 return result;
2370 /* Finish a compound-literal expression. TYPE is the type to which
2371 the CONSTRUCTOR in COMPOUND_LITERAL is being cast. */
2373 tree
2374 finish_compound_literal (tree type, tree compound_literal,
2375 tsubst_flags_t complain)
2377 if (type == error_mark_node)
2378 return error_mark_node;
2380 if (TREE_CODE (type) == REFERENCE_TYPE)
2382 compound_literal
2383 = finish_compound_literal (TREE_TYPE (type), compound_literal,
2384 complain);
2385 return cp_build_c_cast (type, compound_literal, complain);
2388 if (!TYPE_OBJ_P (type))
2390 if (complain & tf_error)
2391 error ("compound literal of non-object type %qT", type);
2392 return error_mark_node;
2395 if (processing_template_decl)
2397 TREE_TYPE (compound_literal) = type;
2398 /* Mark the expression as a compound literal. */
2399 TREE_HAS_CONSTRUCTOR (compound_literal) = 1;
2400 return compound_literal;
2403 type = complete_type (type);
2405 if (TYPE_NON_AGGREGATE_CLASS (type))
2407 /* Trying to deal with a CONSTRUCTOR instead of a TREE_LIST
2408 everywhere that deals with function arguments would be a pain, so
2409 just wrap it in a TREE_LIST. The parser set a flag so we know
2410 that it came from T{} rather than T({}). */
2411 CONSTRUCTOR_IS_DIRECT_INIT (compound_literal) = 1;
2412 compound_literal = build_tree_list (NULL_TREE, compound_literal);
2413 return build_functional_cast (type, compound_literal, complain);
2416 if (TREE_CODE (type) == ARRAY_TYPE
2417 && check_array_initializer (NULL_TREE, type, compound_literal))
2418 return error_mark_node;
2419 compound_literal = reshape_init (type, compound_literal, complain);
2420 if (SCALAR_TYPE_P (type)
2421 && !BRACE_ENCLOSED_INITIALIZER_P (compound_literal)
2422 && (complain & tf_warning_or_error))
2423 check_narrowing (type, compound_literal);
2424 if (TREE_CODE (type) == ARRAY_TYPE
2425 && TYPE_DOMAIN (type) == NULL_TREE)
2427 cp_complete_array_type_or_error (&type, compound_literal,
2428 false, complain);
2429 if (type == error_mark_node)
2430 return error_mark_node;
2432 compound_literal = digest_init (type, compound_literal, complain);
2433 if (TREE_CODE (compound_literal) == CONSTRUCTOR)
2434 TREE_HAS_CONSTRUCTOR (compound_literal) = true;
2435 /* Put static/constant array temporaries in static variables, but always
2436 represent class temporaries with TARGET_EXPR so we elide copies. */
2437 if ((!at_function_scope_p () || CP_TYPE_CONST_P (type))
2438 && TREE_CODE (type) == ARRAY_TYPE
2439 && !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
2440 && initializer_constant_valid_p (compound_literal, type))
2442 tree decl = create_temporary_var (type);
2443 DECL_INITIAL (decl) = compound_literal;
2444 TREE_STATIC (decl) = 1;
2445 if (literal_type_p (type) && CP_TYPE_CONST_NON_VOLATILE_P (type))
2447 /* 5.19 says that a constant expression can include an
2448 lvalue-rvalue conversion applied to "a glvalue of literal type
2449 that refers to a non-volatile temporary object initialized
2450 with a constant expression". Rather than try to communicate
2451 that this VAR_DECL is a temporary, just mark it constexpr. */
2452 DECL_DECLARED_CONSTEXPR_P (decl) = true;
2453 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
2454 TREE_CONSTANT (decl) = true;
2456 cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
2457 decl = pushdecl_top_level (decl);
2458 DECL_NAME (decl) = make_anon_name ();
2459 SET_DECL_ASSEMBLER_NAME (decl, DECL_NAME (decl));
2460 return decl;
2462 else
2463 return get_target_expr_sfinae (compound_literal, complain);
2466 /* Return the declaration for the function-name variable indicated by
2467 ID. */
2469 tree
2470 finish_fname (tree id)
2472 tree decl;
2474 decl = fname_decl (input_location, C_RID_CODE (id), id);
2475 if (processing_template_decl && current_function_decl)
2476 decl = DECL_NAME (decl);
2477 return decl;
2480 /* Finish a translation unit. */
2482 void
2483 finish_translation_unit (void)
2485 /* In case there were missing closebraces,
2486 get us back to the global binding level. */
2487 pop_everything ();
2488 while (current_namespace != global_namespace)
2489 pop_namespace ();
2491 /* Do file scope __FUNCTION__ et al. */
2492 finish_fname_decls ();
2495 /* Finish a template type parameter, specified as AGGR IDENTIFIER.
2496 Returns the parameter. */
2498 tree
2499 finish_template_type_parm (tree aggr, tree identifier)
2501 if (aggr != class_type_node)
2503 permerror (input_location, "template type parameters must use the keyword %<class%> or %<typename%>");
2504 aggr = class_type_node;
2507 return build_tree_list (aggr, identifier);
2510 /* Finish a template template parameter, specified as AGGR IDENTIFIER.
2511 Returns the parameter. */
2513 tree
2514 finish_template_template_parm (tree aggr, tree identifier)
2516 tree decl = build_decl (input_location,
2517 TYPE_DECL, identifier, NULL_TREE);
2518 tree tmpl = build_lang_decl (TEMPLATE_DECL, identifier, NULL_TREE);
2519 DECL_TEMPLATE_PARMS (tmpl) = current_template_parms;
2520 DECL_TEMPLATE_RESULT (tmpl) = decl;
2521 DECL_ARTIFICIAL (decl) = 1;
2522 end_template_decl ();
2524 gcc_assert (DECL_TEMPLATE_PARMS (tmpl));
2526 check_default_tmpl_args (decl, DECL_TEMPLATE_PARMS (tmpl),
2527 /*is_primary=*/true, /*is_partial=*/false,
2528 /*is_friend=*/0);
2530 return finish_template_type_parm (aggr, tmpl);
2533 /* ARGUMENT is the default-argument value for a template template
2534 parameter. If ARGUMENT is invalid, issue error messages and return
2535 the ERROR_MARK_NODE. Otherwise, ARGUMENT itself is returned. */
2537 tree
2538 check_template_template_default_arg (tree argument)
2540 if (TREE_CODE (argument) != TEMPLATE_DECL
2541 && TREE_CODE (argument) != TEMPLATE_TEMPLATE_PARM
2542 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
2544 if (TREE_CODE (argument) == TYPE_DECL)
2545 error ("invalid use of type %qT as a default value for a template "
2546 "template-parameter", TREE_TYPE (argument));
2547 else
2548 error ("invalid default argument for a template template parameter");
2549 return error_mark_node;
2552 return argument;
2555 /* Begin a class definition, as indicated by T. */
2557 tree
2558 begin_class_definition (tree t)
2560 if (error_operand_p (t) || error_operand_p (TYPE_MAIN_DECL (t)))
2561 return error_mark_node;
2563 if (processing_template_parmlist)
2565 error ("definition of %q#T inside template parameter list", t);
2566 return error_mark_node;
2569 /* According to the C++ ABI, decimal classes defined in ISO/IEC TR 24733
2570 are passed the same as decimal scalar types. */
2571 if (TREE_CODE (t) == RECORD_TYPE
2572 && !processing_template_decl)
2574 tree ns = TYPE_CONTEXT (t);
2575 if (ns && TREE_CODE (ns) == NAMESPACE_DECL
2576 && DECL_CONTEXT (ns) == std_node
2577 && DECL_NAME (ns)
2578 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (ns)), "decimal"))
2580 const char *n = TYPE_NAME_STRING (t);
2581 if ((strcmp (n, "decimal32") == 0)
2582 || (strcmp (n, "decimal64") == 0)
2583 || (strcmp (n, "decimal128") == 0))
2584 TYPE_TRANSPARENT_AGGR (t) = 1;
2588 /* A non-implicit typename comes from code like:
2590 template <typename T> struct A {
2591 template <typename U> struct A<T>::B ...
2593 This is erroneous. */
2594 else if (TREE_CODE (t) == TYPENAME_TYPE)
2596 error ("invalid definition of qualified type %qT", t);
2597 t = error_mark_node;
2600 if (t == error_mark_node || ! MAYBE_CLASS_TYPE_P (t))
2602 t = make_class_type (RECORD_TYPE);
2603 pushtag (make_anon_name (), t, /*tag_scope=*/ts_current);
2606 if (TYPE_BEING_DEFINED (t))
2608 t = make_class_type (TREE_CODE (t));
2609 pushtag (TYPE_IDENTIFIER (t), t, /*tag_scope=*/ts_current);
2611 maybe_process_partial_specialization (t);
2612 pushclass (t);
2613 TYPE_BEING_DEFINED (t) = 1;
2615 if (flag_pack_struct)
2617 tree v;
2618 TYPE_PACKED (t) = 1;
2619 /* Even though the type is being defined for the first time
2620 here, there might have been a forward declaration, so there
2621 might be cv-qualified variants of T. */
2622 for (v = TYPE_NEXT_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v))
2623 TYPE_PACKED (v) = 1;
2625 /* Reset the interface data, at the earliest possible
2626 moment, as it might have been set via a class foo;
2627 before. */
2628 if (! TYPE_ANONYMOUS_P (t))
2630 struct c_fileinfo *finfo = get_fileinfo (input_filename);
2631 CLASSTYPE_INTERFACE_ONLY (t) = finfo->interface_only;
2632 SET_CLASSTYPE_INTERFACE_UNKNOWN_X
2633 (t, finfo->interface_unknown);
2635 reset_specialization();
2637 /* Make a declaration for this class in its own scope. */
2638 build_self_reference ();
2640 return t;
2643 /* Finish the member declaration given by DECL. */
2645 void
2646 finish_member_declaration (tree decl)
2648 if (decl == error_mark_node || decl == NULL_TREE)
2649 return;
2651 if (decl == void_type_node)
2652 /* The COMPONENT was a friend, not a member, and so there's
2653 nothing for us to do. */
2654 return;
2656 /* We should see only one DECL at a time. */
2657 gcc_assert (DECL_CHAIN (decl) == NULL_TREE);
2659 /* Set up access control for DECL. */
2660 TREE_PRIVATE (decl)
2661 = (current_access_specifier == access_private_node);
2662 TREE_PROTECTED (decl)
2663 = (current_access_specifier == access_protected_node);
2664 if (TREE_CODE (decl) == TEMPLATE_DECL)
2666 TREE_PRIVATE (DECL_TEMPLATE_RESULT (decl)) = TREE_PRIVATE (decl);
2667 TREE_PROTECTED (DECL_TEMPLATE_RESULT (decl)) = TREE_PROTECTED (decl);
2670 /* Mark the DECL as a member of the current class, unless it's
2671 a member of an enumeration. */
2672 if (TREE_CODE (decl) != CONST_DECL)
2673 DECL_CONTEXT (decl) = current_class_type;
2675 /* Check for bare parameter packs in the member variable declaration. */
2676 if (TREE_CODE (decl) == FIELD_DECL)
2678 if (check_for_bare_parameter_packs (TREE_TYPE (decl)))
2679 TREE_TYPE (decl) = error_mark_node;
2680 if (check_for_bare_parameter_packs (DECL_ATTRIBUTES (decl)))
2681 DECL_ATTRIBUTES (decl) = NULL_TREE;
2684 /* [dcl.link]
2686 A C language linkage is ignored for the names of class members
2687 and the member function type of class member functions. */
2688 if (DECL_LANG_SPECIFIC (decl) && DECL_LANGUAGE (decl) == lang_c)
2689 SET_DECL_LANGUAGE (decl, lang_cplusplus);
2691 /* Put functions on the TYPE_METHODS list and everything else on the
2692 TYPE_FIELDS list. Note that these are built up in reverse order.
2693 We reverse them (to obtain declaration order) in finish_struct. */
2694 if (TREE_CODE (decl) == FUNCTION_DECL
2695 || DECL_FUNCTION_TEMPLATE_P (decl))
2697 /* We also need to add this function to the
2698 CLASSTYPE_METHOD_VEC. */
2699 if (add_method (current_class_type, decl, NULL_TREE))
2701 DECL_CHAIN (decl) = TYPE_METHODS (current_class_type);
2702 TYPE_METHODS (current_class_type) = decl;
2704 maybe_add_class_template_decl_list (current_class_type, decl,
2705 /*friend_p=*/0);
2708 /* Enter the DECL into the scope of the class. */
2709 else if (pushdecl_class_level (decl))
2711 if (TREE_CODE (decl) == USING_DECL)
2713 /* For now, ignore class-scope USING_DECLS, so that
2714 debugging backends do not see them. */
2715 DECL_IGNORED_P (decl) = 1;
2718 /* All TYPE_DECLs go at the end of TYPE_FIELDS. Ordinary fields
2719 go at the beginning. The reason is that lookup_field_1
2720 searches the list in order, and we want a field name to
2721 override a type name so that the "struct stat hack" will
2722 work. In particular:
2724 struct S { enum E { }; int E } s;
2725 s.E = 3;
2727 is valid. In addition, the FIELD_DECLs must be maintained in
2728 declaration order so that class layout works as expected.
2729 However, we don't need that order until class layout, so we
2730 save a little time by putting FIELD_DECLs on in reverse order
2731 here, and then reversing them in finish_struct_1. (We could
2732 also keep a pointer to the correct insertion points in the
2733 list.) */
2735 if (TREE_CODE (decl) == TYPE_DECL)
2736 TYPE_FIELDS (current_class_type)
2737 = chainon (TYPE_FIELDS (current_class_type), decl);
2738 else
2740 DECL_CHAIN (decl) = TYPE_FIELDS (current_class_type);
2741 TYPE_FIELDS (current_class_type) = decl;
2744 maybe_add_class_template_decl_list (current_class_type, decl,
2745 /*friend_p=*/0);
2748 if (pch_file)
2749 note_decl_for_pch (decl);
2752 /* DECL has been declared while we are building a PCH file. Perform
2753 actions that we might normally undertake lazily, but which can be
2754 performed now so that they do not have to be performed in
2755 translation units which include the PCH file. */
2757 void
2758 note_decl_for_pch (tree decl)
2760 gcc_assert (pch_file);
2762 /* There's a good chance that we'll have to mangle names at some
2763 point, even if only for emission in debugging information. */
2764 if ((TREE_CODE (decl) == VAR_DECL
2765 || TREE_CODE (decl) == FUNCTION_DECL)
2766 && !processing_template_decl)
2767 mangle_decl (decl);
2770 /* Finish processing a complete template declaration. The PARMS are
2771 the template parameters. */
2773 void
2774 finish_template_decl (tree parms)
2776 if (parms)
2777 end_template_decl ();
2778 else
2779 end_specialization ();
2782 /* Finish processing a template-id (which names a type) of the form
2783 NAME < ARGS >. Return the TYPE_DECL for the type named by the
2784 template-id. If ENTERING_SCOPE is nonzero we are about to enter
2785 the scope of template-id indicated. */
2787 tree
2788 finish_template_type (tree name, tree args, int entering_scope)
2790 tree type;
2792 type = lookup_template_class (name, args,
2793 NULL_TREE, NULL_TREE, entering_scope,
2794 tf_warning_or_error | tf_user);
2795 if (type == error_mark_node)
2796 return type;
2797 else if (CLASS_TYPE_P (type) && !alias_type_or_template_p (type))
2798 return TYPE_STUB_DECL (type);
2799 else
2800 return TYPE_NAME (type);
2803 /* Finish processing a BASE_CLASS with the indicated ACCESS_SPECIFIER.
2804 Return a TREE_LIST containing the ACCESS_SPECIFIER and the
2805 BASE_CLASS, or NULL_TREE if an error occurred. The
2806 ACCESS_SPECIFIER is one of
2807 access_{default,public,protected_private}_node. For a virtual base
2808 we set TREE_TYPE. */
2810 tree
2811 finish_base_specifier (tree base, tree access, bool virtual_p)
2813 tree result;
2815 if (base == error_mark_node)
2817 error ("invalid base-class specification");
2818 result = NULL_TREE;
2820 else if (! MAYBE_CLASS_TYPE_P (base))
2822 error ("%qT is not a class type", base);
2823 result = NULL_TREE;
2825 else
2827 if (cp_type_quals (base) != 0)
2829 /* DR 484: Can a base-specifier name a cv-qualified
2830 class type? */
2831 base = TYPE_MAIN_VARIANT (base);
2833 result = build_tree_list (access, base);
2834 if (virtual_p)
2835 TREE_TYPE (result) = integer_type_node;
2838 return result;
2841 /* If FNS is a member function, a set of member functions, or a
2842 template-id referring to one or more member functions, return a
2843 BASELINK for FNS, incorporating the current access context.
2844 Otherwise, return FNS unchanged. */
2846 tree
2847 baselink_for_fns (tree fns)
2849 tree scope;
2850 tree cl;
2852 if (BASELINK_P (fns)
2853 || error_operand_p (fns))
2854 return fns;
2856 scope = ovl_scope (fns);
2857 if (!CLASS_TYPE_P (scope))
2858 return fns;
2860 cl = currently_open_derived_class (scope);
2861 if (!cl)
2862 cl = scope;
2863 cl = TYPE_BINFO (cl);
2864 return build_baselink (cl, cl, fns, /*optype=*/NULL_TREE);
2867 /* Returns true iff DECL is an automatic variable from a function outside
2868 the current one. */
2870 static bool
2871 outer_automatic_var_p (tree decl)
2873 return ((TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL)
2874 && DECL_FUNCTION_SCOPE_P (decl)
2875 && !TREE_STATIC (decl)
2876 && DECL_CONTEXT (decl) != current_function_decl);
2879 /* ID_EXPRESSION is a representation of parsed, but unprocessed,
2880 id-expression. (See cp_parser_id_expression for details.) SCOPE,
2881 if non-NULL, is the type or namespace used to explicitly qualify
2882 ID_EXPRESSION. DECL is the entity to which that name has been
2883 resolved.
2885 *CONSTANT_EXPRESSION_P is true if we are presently parsing a
2886 constant-expression. In that case, *NON_CONSTANT_EXPRESSION_P will
2887 be set to true if this expression isn't permitted in a
2888 constant-expression, but it is otherwise not set by this function.
2889 *ALLOW_NON_CONSTANT_EXPRESSION_P is true if we are parsing a
2890 constant-expression, but a non-constant expression is also
2891 permissible.
2893 DONE is true if this expression is a complete postfix-expression;
2894 it is false if this expression is followed by '->', '[', '(', etc.
2895 ADDRESS_P is true iff this expression is the operand of '&'.
2896 TEMPLATE_P is true iff the qualified-id was of the form
2897 "A::template B". TEMPLATE_ARG_P is true iff this qualified name
2898 appears as a template argument.
2900 If an error occurs, and it is the kind of error that might cause
2901 the parser to abort a tentative parse, *ERROR_MSG is filled in. It
2902 is the caller's responsibility to issue the message. *ERROR_MSG
2903 will be a string with static storage duration, so the caller need
2904 not "free" it.
2906 Return an expression for the entity, after issuing appropriate
2907 diagnostics. This function is also responsible for transforming a
2908 reference to a non-static member into a COMPONENT_REF that makes
2909 the use of "this" explicit.
2911 Upon return, *IDK will be filled in appropriately. */
2912 tree
2913 finish_id_expression (tree id_expression,
2914 tree decl,
2915 tree scope,
2916 cp_id_kind *idk,
2917 bool integral_constant_expression_p,
2918 bool allow_non_integral_constant_expression_p,
2919 bool *non_integral_constant_expression_p,
2920 bool template_p,
2921 bool done,
2922 bool address_p,
2923 bool template_arg_p,
2924 const char **error_msg,
2925 location_t location)
2927 decl = strip_using_decl (decl);
2929 /* Initialize the output parameters. */
2930 *idk = CP_ID_KIND_NONE;
2931 *error_msg = NULL;
2933 if (id_expression == error_mark_node)
2934 return error_mark_node;
2935 /* If we have a template-id, then no further lookup is
2936 required. If the template-id was for a template-class, we
2937 will sometimes have a TYPE_DECL at this point. */
2938 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
2939 || TREE_CODE (decl) == TYPE_DECL)
2941 /* Look up the name. */
2942 else
2944 if (decl == error_mark_node)
2946 /* Name lookup failed. */
2947 if (scope
2948 && (!TYPE_P (scope)
2949 || (!dependent_type_p (scope)
2950 && !(TREE_CODE (id_expression) == IDENTIFIER_NODE
2951 && IDENTIFIER_TYPENAME_P (id_expression)
2952 && dependent_type_p (TREE_TYPE (id_expression))))))
2954 /* If the qualifying type is non-dependent (and the name
2955 does not name a conversion operator to a dependent
2956 type), issue an error. */
2957 qualified_name_lookup_error (scope, id_expression, decl, location);
2958 return error_mark_node;
2960 else if (!scope)
2962 /* It may be resolved via Koenig lookup. */
2963 *idk = CP_ID_KIND_UNQUALIFIED;
2964 return id_expression;
2966 else
2967 decl = id_expression;
2969 /* If DECL is a variable that would be out of scope under
2970 ANSI/ISO rules, but in scope in the ARM, name lookup
2971 will succeed. Issue a diagnostic here. */
2972 else
2973 decl = check_for_out_of_scope_variable (decl);
2975 /* Remember that the name was used in the definition of
2976 the current class so that we can check later to see if
2977 the meaning would have been different after the class
2978 was entirely defined. */
2979 if (!scope && decl != error_mark_node
2980 && TREE_CODE (id_expression) == IDENTIFIER_NODE)
2981 maybe_note_name_used_in_class (id_expression, decl);
2983 /* Disallow uses of local variables from containing functions, except
2984 within lambda-expressions. */
2985 if (outer_automatic_var_p (decl)
2986 /* It's not a use (3.2) if we're in an unevaluated context. */
2987 && !cp_unevaluated_operand)
2989 tree context = DECL_CONTEXT (decl);
2990 tree containing_function = current_function_decl;
2991 tree lambda_stack = NULL_TREE;
2992 tree lambda_expr = NULL_TREE;
2993 tree initializer = convert_from_reference (decl);
2995 /* Mark it as used now even if the use is ill-formed. */
2996 mark_used (decl);
2998 /* Core issue 696: "[At the July 2009 meeting] the CWG expressed
2999 support for an approach in which a reference to a local
3000 [constant] automatic variable in a nested class or lambda body
3001 would enter the expression as an rvalue, which would reduce
3002 the complexity of the problem"
3004 FIXME update for final resolution of core issue 696. */
3005 if (decl_constant_var_p (decl))
3006 return integral_constant_value (decl);
3008 /* If we are in a lambda function, we can move out until we hit
3009 1. the context,
3010 2. a non-lambda function, or
3011 3. a non-default capturing lambda function. */
3012 while (context != containing_function
3013 && LAMBDA_FUNCTION_P (containing_function))
3015 lambda_expr = CLASSTYPE_LAMBDA_EXPR
3016 (DECL_CONTEXT (containing_function));
3018 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr)
3019 == CPLD_NONE)
3020 break;
3022 lambda_stack = tree_cons (NULL_TREE,
3023 lambda_expr,
3024 lambda_stack);
3026 containing_function
3027 = decl_function_context (containing_function);
3030 if (context == containing_function)
3032 decl = add_default_capture (lambda_stack,
3033 /*id=*/DECL_NAME (decl),
3034 initializer);
3036 else if (lambda_expr)
3038 error ("%qD is not captured", decl);
3039 return error_mark_node;
3041 else
3043 error (TREE_CODE (decl) == VAR_DECL
3044 ? G_("use of %<auto%> variable from containing function")
3045 : G_("use of parameter from containing function"));
3046 error (" %q+#D declared here", decl);
3047 return error_mark_node;
3051 /* Also disallow uses of function parameters outside the function
3052 body, except inside an unevaluated context (i.e. decltype). */
3053 if (TREE_CODE (decl) == PARM_DECL
3054 && DECL_CONTEXT (decl) == NULL_TREE
3055 && !cp_unevaluated_operand)
3057 error ("use of parameter %qD outside function body", decl);
3058 return error_mark_node;
3062 /* If we didn't find anything, or what we found was a type,
3063 then this wasn't really an id-expression. */
3064 if (TREE_CODE (decl) == TEMPLATE_DECL
3065 && !DECL_FUNCTION_TEMPLATE_P (decl))
3067 *error_msg = "missing template arguments";
3068 return error_mark_node;
3070 else if (TREE_CODE (decl) == TYPE_DECL
3071 || TREE_CODE (decl) == NAMESPACE_DECL)
3073 *error_msg = "expected primary-expression";
3074 return error_mark_node;
3077 /* If the name resolved to a template parameter, there is no
3078 need to look it up again later. */
3079 if ((TREE_CODE (decl) == CONST_DECL && DECL_TEMPLATE_PARM_P (decl))
3080 || TREE_CODE (decl) == TEMPLATE_PARM_INDEX)
3082 tree r;
3084 *idk = CP_ID_KIND_NONE;
3085 if (TREE_CODE (decl) == TEMPLATE_PARM_INDEX)
3086 decl = TEMPLATE_PARM_DECL (decl);
3087 r = convert_from_reference (DECL_INITIAL (decl));
3089 if (integral_constant_expression_p
3090 && !dependent_type_p (TREE_TYPE (decl))
3091 && !(INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (r))))
3093 if (!allow_non_integral_constant_expression_p)
3094 error ("template parameter %qD of type %qT is not allowed in "
3095 "an integral constant expression because it is not of "
3096 "integral or enumeration type", decl, TREE_TYPE (decl));
3097 *non_integral_constant_expression_p = true;
3099 return r;
3101 else
3103 bool dependent_p;
3105 /* If the declaration was explicitly qualified indicate
3106 that. The semantics of `A::f(3)' are different than
3107 `f(3)' if `f' is virtual. */
3108 *idk = (scope
3109 ? CP_ID_KIND_QUALIFIED
3110 : (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3111 ? CP_ID_KIND_TEMPLATE_ID
3112 : CP_ID_KIND_UNQUALIFIED));
3115 /* [temp.dep.expr]
3117 An id-expression is type-dependent if it contains an
3118 identifier that was declared with a dependent type.
3120 The standard is not very specific about an id-expression that
3121 names a set of overloaded functions. What if some of them
3122 have dependent types and some of them do not? Presumably,
3123 such a name should be treated as a dependent name. */
3124 /* Assume the name is not dependent. */
3125 dependent_p = false;
3126 if (!processing_template_decl)
3127 /* No names are dependent outside a template. */
3129 else if (TREE_CODE (decl) == CONST_DECL)
3130 /* We don't want to treat enumerators as dependent. */
3132 /* A template-id where the name of the template was not resolved
3133 is definitely dependent. */
3134 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3135 && (TREE_CODE (TREE_OPERAND (decl, 0))
3136 == IDENTIFIER_NODE))
3137 dependent_p = true;
3138 /* For anything except an overloaded function, just check its
3139 type. */
3140 else if (!is_overloaded_fn (decl))
3141 dependent_p
3142 = dependent_type_p (TREE_TYPE (decl));
3143 /* For a set of overloaded functions, check each of the
3144 functions. */
3145 else
3147 tree fns = decl;
3149 if (BASELINK_P (fns))
3150 fns = BASELINK_FUNCTIONS (fns);
3152 /* For a template-id, check to see if the template
3153 arguments are dependent. */
3154 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
3156 tree args = TREE_OPERAND (fns, 1);
3157 dependent_p = any_dependent_template_arguments_p (args);
3158 /* The functions are those referred to by the
3159 template-id. */
3160 fns = TREE_OPERAND (fns, 0);
3163 /* If there are no dependent template arguments, go through
3164 the overloaded functions. */
3165 while (fns && !dependent_p)
3167 tree fn = OVL_CURRENT (fns);
3169 /* Member functions of dependent classes are
3170 dependent. */
3171 if (TREE_CODE (fn) == FUNCTION_DECL
3172 && type_dependent_expression_p (fn))
3173 dependent_p = true;
3174 else if (TREE_CODE (fn) == TEMPLATE_DECL
3175 && dependent_template_p (fn))
3176 dependent_p = true;
3178 fns = OVL_NEXT (fns);
3182 /* If the name was dependent on a template parameter, we will
3183 resolve the name at instantiation time. */
3184 if (dependent_p)
3186 /* Create a SCOPE_REF for qualified names, if the scope is
3187 dependent. */
3188 if (scope)
3190 if (TYPE_P (scope))
3192 if (address_p && done)
3193 decl = finish_qualified_id_expr (scope, decl,
3194 done, address_p,
3195 template_p,
3196 template_arg_p);
3197 else
3199 tree type = NULL_TREE;
3200 if (DECL_P (decl) && !dependent_scope_p (scope))
3201 type = TREE_TYPE (decl);
3202 decl = build_qualified_name (type,
3203 scope,
3204 id_expression,
3205 template_p);
3208 if (TREE_TYPE (decl))
3209 decl = convert_from_reference (decl);
3210 return decl;
3212 /* A TEMPLATE_ID already contains all the information we
3213 need. */
3214 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR)
3215 return id_expression;
3216 *idk = CP_ID_KIND_UNQUALIFIED_DEPENDENT;
3217 /* If we found a variable, then name lookup during the
3218 instantiation will always resolve to the same VAR_DECL
3219 (or an instantiation thereof). */
3220 if (TREE_CODE (decl) == VAR_DECL
3221 || TREE_CODE (decl) == PARM_DECL)
3223 mark_used (decl);
3224 return convert_from_reference (decl);
3226 /* The same is true for FIELD_DECL, but we also need to
3227 make sure that the syntax is correct. */
3228 else if (TREE_CODE (decl) == FIELD_DECL)
3230 /* Since SCOPE is NULL here, this is an unqualified name.
3231 Access checking has been performed during name lookup
3232 already. Turn off checking to avoid duplicate errors. */
3233 push_deferring_access_checks (dk_no_check);
3234 decl = finish_non_static_data_member
3235 (decl, NULL_TREE,
3236 /*qualifying_scope=*/NULL_TREE);
3237 pop_deferring_access_checks ();
3238 return decl;
3240 return id_expression;
3243 if (TREE_CODE (decl) == NAMESPACE_DECL)
3245 error ("use of namespace %qD as expression", decl);
3246 return error_mark_node;
3248 else if (DECL_CLASS_TEMPLATE_P (decl))
3250 error ("use of class template %qT as expression", decl);
3251 return error_mark_node;
3253 else if (TREE_CODE (decl) == TREE_LIST)
3255 /* Ambiguous reference to base members. */
3256 error ("request for member %qD is ambiguous in "
3257 "multiple inheritance lattice", id_expression);
3258 print_candidates (decl);
3259 return error_mark_node;
3262 /* Mark variable-like entities as used. Functions are similarly
3263 marked either below or after overload resolution. */
3264 if ((TREE_CODE (decl) == VAR_DECL
3265 || TREE_CODE (decl) == PARM_DECL
3266 || TREE_CODE (decl) == CONST_DECL
3267 || TREE_CODE (decl) == RESULT_DECL)
3268 && !mark_used (decl))
3269 return error_mark_node;
3271 /* Only certain kinds of names are allowed in constant
3272 expression. Template parameters have already
3273 been handled above. */
3274 if (! error_operand_p (decl)
3275 && integral_constant_expression_p
3276 && ! decl_constant_var_p (decl)
3277 && TREE_CODE (decl) != CONST_DECL
3278 && ! builtin_valid_in_constant_expr_p (decl))
3280 if (!allow_non_integral_constant_expression_p)
3282 error ("%qD cannot appear in a constant-expression", decl);
3283 return error_mark_node;
3285 *non_integral_constant_expression_p = true;
3288 tree wrap;
3289 if (TREE_CODE (decl) == VAR_DECL
3290 && !cp_unevaluated_operand
3291 && DECL_THREAD_LOCAL_P (decl)
3292 && (wrap = get_tls_wrapper_fn (decl)))
3294 /* Replace an evaluated use of the thread_local variable with
3295 a call to its wrapper. */
3296 decl = build_cxx_call (wrap, 0, NULL, tf_warning_or_error);
3298 else if (scope)
3300 decl = (adjust_result_of_qualified_name_lookup
3301 (decl, scope, current_nonlambda_class_type()));
3303 if (TREE_CODE (decl) == FUNCTION_DECL)
3304 mark_used (decl);
3306 if (TYPE_P (scope))
3307 decl = finish_qualified_id_expr (scope,
3308 decl,
3309 done,
3310 address_p,
3311 template_p,
3312 template_arg_p);
3313 else
3314 decl = convert_from_reference (decl);
3316 else if (TREE_CODE (decl) == FIELD_DECL)
3318 /* Since SCOPE is NULL here, this is an unqualified name.
3319 Access checking has been performed during name lookup
3320 already. Turn off checking to avoid duplicate errors. */
3321 push_deferring_access_checks (dk_no_check);
3322 decl = finish_non_static_data_member (decl, NULL_TREE,
3323 /*qualifying_scope=*/NULL_TREE);
3324 pop_deferring_access_checks ();
3326 else if (is_overloaded_fn (decl))
3328 tree first_fn;
3330 first_fn = get_first_fn (decl);
3331 if (TREE_CODE (first_fn) == TEMPLATE_DECL)
3332 first_fn = DECL_TEMPLATE_RESULT (first_fn);
3334 if (!really_overloaded_fn (decl)
3335 && !mark_used (first_fn))
3336 return error_mark_node;
3338 if (!template_arg_p
3339 && TREE_CODE (first_fn) == FUNCTION_DECL
3340 && DECL_FUNCTION_MEMBER_P (first_fn)
3341 && !shared_member_p (decl))
3343 /* A set of member functions. */
3344 decl = maybe_dummy_object (DECL_CONTEXT (first_fn), 0);
3345 return finish_class_member_access_expr (decl, id_expression,
3346 /*template_p=*/false,
3347 tf_warning_or_error);
3350 decl = baselink_for_fns (decl);
3352 else
3354 if (DECL_P (decl) && DECL_NONLOCAL (decl)
3355 && DECL_CLASS_SCOPE_P (decl))
3357 tree context = context_for_name_lookup (decl);
3358 if (context != current_class_type)
3360 tree path = currently_open_derived_class (context);
3361 perform_or_defer_access_check (TYPE_BINFO (path),
3362 decl, decl,
3363 tf_warning_or_error);
3367 decl = convert_from_reference (decl);
3371 if (TREE_DEPRECATED (decl))
3372 warn_deprecated_use (decl, NULL_TREE);
3374 return decl;
3377 /* Implement the __typeof keyword: Return the type of EXPR, suitable for
3378 use as a type-specifier. */
3380 tree
3381 finish_typeof (tree expr)
3383 tree type;
3385 if (type_dependent_expression_p (expr))
3387 type = cxx_make_type (TYPEOF_TYPE);
3388 TYPEOF_TYPE_EXPR (type) = expr;
3389 SET_TYPE_STRUCTURAL_EQUALITY (type);
3391 return type;
3394 expr = mark_type_use (expr);
3396 type = unlowered_expr_type (expr);
3398 if (!type || type == unknown_type_node)
3400 error ("type of %qE is unknown", expr);
3401 return error_mark_node;
3404 return type;
3407 /* Implement the __underlying_type keyword: Return the underlying
3408 type of TYPE, suitable for use as a type-specifier. */
3410 tree
3411 finish_underlying_type (tree type)
3413 tree underlying_type;
3415 if (processing_template_decl)
3417 underlying_type = cxx_make_type (UNDERLYING_TYPE);
3418 UNDERLYING_TYPE_TYPE (underlying_type) = type;
3419 SET_TYPE_STRUCTURAL_EQUALITY (underlying_type);
3421 return underlying_type;
3424 complete_type (type);
3426 if (TREE_CODE (type) != ENUMERAL_TYPE)
3428 error ("%qT is not an enumeration type", type);
3429 return error_mark_node;
3432 underlying_type = ENUM_UNDERLYING_TYPE (type);
3434 /* Fixup necessary in this case because ENUM_UNDERLYING_TYPE
3435 includes TYPE_MIN_VALUE and TYPE_MAX_VALUE information.
3436 See finish_enum_value_list for details. */
3437 if (!ENUM_FIXED_UNDERLYING_TYPE_P (type))
3438 underlying_type
3439 = c_common_type_for_mode (TYPE_MODE (underlying_type),
3440 TYPE_UNSIGNED (underlying_type));
3442 return underlying_type;
3445 /* Implement the __direct_bases keyword: Return the direct base classes
3446 of type */
3448 tree
3449 calculate_direct_bases (tree type)
3451 VEC(tree, gc) *vector = make_tree_vector();
3452 tree bases_vec = NULL_TREE;
3453 VEC(tree, none) *base_binfos;
3454 tree binfo;
3455 unsigned i;
3457 complete_type (type);
3459 if (!NON_UNION_CLASS_TYPE_P (type))
3460 return make_tree_vec (0);
3462 base_binfos = BINFO_BASE_BINFOS (TYPE_BINFO (type));
3464 /* Virtual bases are initialized first */
3465 for (i = 0; VEC_iterate (tree, base_binfos, i, binfo); i++)
3467 if (BINFO_VIRTUAL_P (binfo))
3469 VEC_safe_push (tree, gc, vector, binfo);
3473 /* Now non-virtuals */
3474 for (i = 0; VEC_iterate (tree, base_binfos, i, binfo); i++)
3476 if (!BINFO_VIRTUAL_P (binfo))
3478 VEC_safe_push (tree, gc, vector, binfo);
3483 bases_vec = make_tree_vec (VEC_length (tree, vector));
3485 for (i = 0; i < VEC_length (tree, vector); ++i)
3487 TREE_VEC_ELT (bases_vec, i) = BINFO_TYPE (VEC_index (tree, vector, i));
3489 return bases_vec;
3492 /* Implement the __bases keyword: Return the base classes
3493 of type */
3495 /* Find morally non-virtual base classes by walking binfo hierarchy */
3496 /* Virtual base classes are handled separately in finish_bases */
3498 static tree
3499 dfs_calculate_bases_pre (tree binfo, void * /*data_*/)
3501 /* Don't walk bases of virtual bases */
3502 return BINFO_VIRTUAL_P (binfo) ? dfs_skip_bases : NULL_TREE;
3505 static tree
3506 dfs_calculate_bases_post (tree binfo, void *data_)
3508 VEC(tree, gc) **data = (VEC(tree, gc) **) data_;
3509 if (!BINFO_VIRTUAL_P (binfo))
3511 VEC_safe_push (tree, gc, *data, BINFO_TYPE (binfo));
3513 return NULL_TREE;
3516 /* Calculates the morally non-virtual base classes of a class */
3517 static VEC(tree, gc) *
3518 calculate_bases_helper (tree type)
3520 VEC(tree, gc) *vector = make_tree_vector();
3522 /* Now add non-virtual base classes in order of construction */
3523 dfs_walk_all (TYPE_BINFO (type),
3524 dfs_calculate_bases_pre, dfs_calculate_bases_post, &vector);
3525 return vector;
3528 tree
3529 calculate_bases (tree type)
3531 VEC(tree, gc) *vector = make_tree_vector();
3532 tree bases_vec = NULL_TREE;
3533 unsigned i;
3534 VEC(tree, gc) *vbases;
3535 VEC(tree, gc) *nonvbases;
3536 tree binfo;
3538 complete_type (type);
3540 if (!NON_UNION_CLASS_TYPE_P (type))
3541 return make_tree_vec (0);
3543 /* First go through virtual base classes */
3544 for (vbases = CLASSTYPE_VBASECLASSES (type), i = 0;
3545 VEC_iterate (tree, vbases, i, binfo); i++)
3547 VEC(tree, gc) *vbase_bases = calculate_bases_helper (BINFO_TYPE (binfo));
3548 VEC_safe_splice (tree, gc, vector, vbase_bases);
3549 release_tree_vector (vbase_bases);
3552 /* Now for the non-virtual bases */
3553 nonvbases = calculate_bases_helper (type);
3554 VEC_safe_splice (tree, gc, vector, nonvbases);
3555 release_tree_vector (nonvbases);
3557 /* Last element is entire class, so don't copy */
3558 bases_vec = make_tree_vec (VEC_length (tree, vector) - 1);
3560 for (i = 0; i < VEC_length (tree, vector) - 1; ++i)
3562 TREE_VEC_ELT (bases_vec, i) = VEC_index (tree, vector, i);
3564 release_tree_vector (vector);
3565 return bases_vec;
3568 tree
3569 finish_bases (tree type, bool direct)
3571 tree bases = NULL_TREE;
3573 if (!processing_template_decl)
3575 /* Parameter packs can only be used in templates */
3576 error ("Parameter pack __bases only valid in template declaration");
3577 return error_mark_node;
3580 bases = cxx_make_type (BASES);
3581 BASES_TYPE (bases) = type;
3582 BASES_DIRECT (bases) = direct;
3583 SET_TYPE_STRUCTURAL_EQUALITY (bases);
3585 return bases;
3588 /* Perform C++-specific checks for __builtin_offsetof before calling
3589 fold_offsetof. */
3591 tree
3592 finish_offsetof (tree expr)
3594 if (TREE_CODE (expr) == PSEUDO_DTOR_EXPR)
3596 error ("cannot apply %<offsetof%> to destructor %<~%T%>",
3597 TREE_OPERAND (expr, 2));
3598 return error_mark_node;
3600 if (TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE
3601 || TREE_CODE (TREE_TYPE (expr)) == METHOD_TYPE
3602 || TREE_TYPE (expr) == unknown_type_node)
3604 if (TREE_CODE (expr) == COMPONENT_REF
3605 || TREE_CODE (expr) == COMPOUND_EXPR)
3606 expr = TREE_OPERAND (expr, 1);
3607 error ("cannot apply %<offsetof%> to member function %qD", expr);
3608 return error_mark_node;
3610 if (REFERENCE_REF_P (expr))
3611 expr = TREE_OPERAND (expr, 0);
3612 if (TREE_CODE (expr) == COMPONENT_REF)
3614 tree object = TREE_OPERAND (expr, 0);
3615 if (!complete_type_or_else (TREE_TYPE (object), object))
3616 return error_mark_node;
3618 return fold_offsetof (expr);
3621 /* Replace the AGGR_INIT_EXPR at *TP with an equivalent CALL_EXPR. This
3622 function is broken out from the above for the benefit of the tree-ssa
3623 project. */
3625 void
3626 simplify_aggr_init_expr (tree *tp)
3628 tree aggr_init_expr = *tp;
3630 /* Form an appropriate CALL_EXPR. */
3631 tree fn = AGGR_INIT_EXPR_FN (aggr_init_expr);
3632 tree slot = AGGR_INIT_EXPR_SLOT (aggr_init_expr);
3633 tree type = TREE_TYPE (slot);
3635 tree call_expr;
3636 enum style_t { ctor, arg, pcc } style;
3638 if (AGGR_INIT_VIA_CTOR_P (aggr_init_expr))
3639 style = ctor;
3640 #ifdef PCC_STATIC_STRUCT_RETURN
3641 else if (1)
3642 style = pcc;
3643 #endif
3644 else
3646 gcc_assert (TREE_ADDRESSABLE (type));
3647 style = arg;
3650 call_expr = build_call_array_loc (input_location,
3651 TREE_TYPE (TREE_TYPE (TREE_TYPE (fn))),
3653 aggr_init_expr_nargs (aggr_init_expr),
3654 AGGR_INIT_EXPR_ARGP (aggr_init_expr));
3655 TREE_NOTHROW (call_expr) = TREE_NOTHROW (aggr_init_expr);
3657 if (style == ctor)
3659 /* Replace the first argument to the ctor with the address of the
3660 slot. */
3661 cxx_mark_addressable (slot);
3662 CALL_EXPR_ARG (call_expr, 0) =
3663 build1 (ADDR_EXPR, build_pointer_type (type), slot);
3665 else if (style == arg)
3667 /* Just mark it addressable here, and leave the rest to
3668 expand_call{,_inline}. */
3669 cxx_mark_addressable (slot);
3670 CALL_EXPR_RETURN_SLOT_OPT (call_expr) = true;
3671 call_expr = build2 (INIT_EXPR, TREE_TYPE (call_expr), slot, call_expr);
3673 else if (style == pcc)
3675 /* If we're using the non-reentrant PCC calling convention, then we
3676 need to copy the returned value out of the static buffer into the
3677 SLOT. */
3678 push_deferring_access_checks (dk_no_check);
3679 call_expr = build_aggr_init (slot, call_expr,
3680 DIRECT_BIND | LOOKUP_ONLYCONVERTING,
3681 tf_warning_or_error);
3682 pop_deferring_access_checks ();
3683 call_expr = build2 (COMPOUND_EXPR, TREE_TYPE (slot), call_expr, slot);
3686 if (AGGR_INIT_ZERO_FIRST (aggr_init_expr))
3688 tree init = build_zero_init (type, NULL_TREE,
3689 /*static_storage_p=*/false);
3690 init = build2 (INIT_EXPR, void_type_node, slot, init);
3691 call_expr = build2 (COMPOUND_EXPR, TREE_TYPE (call_expr),
3692 init, call_expr);
3695 *tp = call_expr;
3698 /* Emit all thunks to FN that should be emitted when FN is emitted. */
3700 void
3701 emit_associated_thunks (tree fn)
3703 /* When we use vcall offsets, we emit thunks with the virtual
3704 functions to which they thunk. The whole point of vcall offsets
3705 is so that you can know statically the entire set of thunks that
3706 will ever be needed for a given virtual function, thereby
3707 enabling you to output all the thunks with the function itself. */
3708 if (DECL_VIRTUAL_P (fn)
3709 /* Do not emit thunks for extern template instantiations. */
3710 && ! DECL_REALLY_EXTERN (fn))
3712 tree thunk;
3714 for (thunk = DECL_THUNKS (fn); thunk; thunk = DECL_CHAIN (thunk))
3716 if (!THUNK_ALIAS (thunk))
3718 use_thunk (thunk, /*emit_p=*/1);
3719 if (DECL_RESULT_THUNK_P (thunk))
3721 tree probe;
3723 for (probe = DECL_THUNKS (thunk);
3724 probe; probe = DECL_CHAIN (probe))
3725 use_thunk (probe, /*emit_p=*/1);
3728 else
3729 gcc_assert (!DECL_THUNKS (thunk));
3734 /* Returns true iff FUN is an instantiation of a constexpr function
3735 template. */
3737 static inline bool
3738 is_instantiation_of_constexpr (tree fun)
3740 return (DECL_TEMPLOID_INSTANTIATION (fun)
3741 && DECL_DECLARED_CONSTEXPR_P (DECL_TEMPLATE_RESULT
3742 (DECL_TI_TEMPLATE (fun))));
3745 /* Generate RTL for FN. */
3747 bool
3748 expand_or_defer_fn_1 (tree fn)
3750 /* When the parser calls us after finishing the body of a template
3751 function, we don't really want to expand the body. */
3752 if (processing_template_decl)
3754 /* Normally, collection only occurs in rest_of_compilation. So,
3755 if we don't collect here, we never collect junk generated
3756 during the processing of templates until we hit a
3757 non-template function. It's not safe to do this inside a
3758 nested class, though, as the parser may have local state that
3759 is not a GC root. */
3760 if (!function_depth)
3761 ggc_collect ();
3762 return false;
3765 gcc_assert (DECL_SAVED_TREE (fn));
3767 /* If this is a constructor or destructor body, we have to clone
3768 it. */
3769 if (maybe_clone_body (fn))
3771 /* We don't want to process FN again, so pretend we've written
3772 it out, even though we haven't. */
3773 TREE_ASM_WRITTEN (fn) = 1;
3774 /* If this is an instantiation of a constexpr function, keep
3775 DECL_SAVED_TREE for explain_invalid_constexpr_fn. */
3776 if (!is_instantiation_of_constexpr (fn))
3777 DECL_SAVED_TREE (fn) = NULL_TREE;
3778 return false;
3781 /* We make a decision about linkage for these functions at the end
3782 of the compilation. Until that point, we do not want the back
3783 end to output them -- but we do want it to see the bodies of
3784 these functions so that it can inline them as appropriate. */
3785 if (DECL_DECLARED_INLINE_P (fn) || DECL_IMPLICIT_INSTANTIATION (fn))
3787 if (DECL_INTERFACE_KNOWN (fn))
3788 /* We've already made a decision as to how this function will
3789 be handled. */;
3790 else if (!at_eof)
3792 DECL_EXTERNAL (fn) = 1;
3793 DECL_NOT_REALLY_EXTERN (fn) = 1;
3794 note_vague_linkage_fn (fn);
3795 /* A non-template inline function with external linkage will
3796 always be COMDAT. As we must eventually determine the
3797 linkage of all functions, and as that causes writes to
3798 the data mapped in from the PCH file, it's advantageous
3799 to mark the functions at this point. */
3800 if (!DECL_IMPLICIT_INSTANTIATION (fn))
3802 /* This function must have external linkage, as
3803 otherwise DECL_INTERFACE_KNOWN would have been
3804 set. */
3805 gcc_assert (TREE_PUBLIC (fn));
3806 comdat_linkage (fn);
3807 DECL_INTERFACE_KNOWN (fn) = 1;
3810 else
3811 import_export_decl (fn);
3813 /* If the user wants us to keep all inline functions, then mark
3814 this function as needed so that finish_file will make sure to
3815 output it later. Similarly, all dllexport'd functions must
3816 be emitted; there may be callers in other DLLs. */
3817 if ((flag_keep_inline_functions
3818 && DECL_DECLARED_INLINE_P (fn)
3819 && !DECL_REALLY_EXTERN (fn))
3820 || (flag_keep_inline_dllexport
3821 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (fn))))
3823 mark_needed (fn);
3824 DECL_EXTERNAL (fn) = 0;
3828 /* There's no reason to do any of the work here if we're only doing
3829 semantic analysis; this code just generates RTL. */
3830 if (flag_syntax_only)
3831 return false;
3833 return true;
3836 void
3837 expand_or_defer_fn (tree fn)
3839 if (expand_or_defer_fn_1 (fn))
3841 function_depth++;
3843 /* Expand or defer, at the whim of the compilation unit manager. */
3844 cgraph_finalize_function (fn, function_depth > 1);
3845 emit_associated_thunks (fn);
3847 function_depth--;
3851 struct nrv_data
3853 tree var;
3854 tree result;
3855 hash_table <pointer_hash <tree_node> > visited;
3858 /* Helper function for walk_tree, used by finalize_nrv below. */
3860 static tree
3861 finalize_nrv_r (tree* tp, int* walk_subtrees, void* data)
3863 struct nrv_data *dp = (struct nrv_data *)data;
3864 tree_node **slot;
3866 /* No need to walk into types. There wouldn't be any need to walk into
3867 non-statements, except that we have to consider STMT_EXPRs. */
3868 if (TYPE_P (*tp))
3869 *walk_subtrees = 0;
3870 /* Change all returns to just refer to the RESULT_DECL; this is a nop,
3871 but differs from using NULL_TREE in that it indicates that we care
3872 about the value of the RESULT_DECL. */
3873 else if (TREE_CODE (*tp) == RETURN_EXPR)
3874 TREE_OPERAND (*tp, 0) = dp->result;
3875 /* Change all cleanups for the NRV to only run when an exception is
3876 thrown. */
3877 else if (TREE_CODE (*tp) == CLEANUP_STMT
3878 && CLEANUP_DECL (*tp) == dp->var)
3879 CLEANUP_EH_ONLY (*tp) = 1;
3880 /* Replace the DECL_EXPR for the NRV with an initialization of the
3881 RESULT_DECL, if needed. */
3882 else if (TREE_CODE (*tp) == DECL_EXPR
3883 && DECL_EXPR_DECL (*tp) == dp->var)
3885 tree init;
3886 if (DECL_INITIAL (dp->var)
3887 && DECL_INITIAL (dp->var) != error_mark_node)
3888 init = build2 (INIT_EXPR, void_type_node, dp->result,
3889 DECL_INITIAL (dp->var));
3890 else
3891 init = build_empty_stmt (EXPR_LOCATION (*tp));
3892 DECL_INITIAL (dp->var) = NULL_TREE;
3893 SET_EXPR_LOCATION (init, EXPR_LOCATION (*tp));
3894 *tp = init;
3896 /* And replace all uses of the NRV with the RESULT_DECL. */
3897 else if (*tp == dp->var)
3898 *tp = dp->result;
3900 /* Avoid walking into the same tree more than once. Unfortunately, we
3901 can't just use walk_tree_without duplicates because it would only call
3902 us for the first occurrence of dp->var in the function body. */
3903 slot = dp->visited.find_slot (*tp, INSERT);
3904 if (*slot)
3905 *walk_subtrees = 0;
3906 else
3907 *slot = *tp;
3909 /* Keep iterating. */
3910 return NULL_TREE;
3913 /* Called from finish_function to implement the named return value
3914 optimization by overriding all the RETURN_EXPRs and pertinent
3915 CLEANUP_STMTs and replacing all occurrences of VAR with RESULT, the
3916 RESULT_DECL for the function. */
3918 void
3919 finalize_nrv (tree *tp, tree var, tree result)
3921 struct nrv_data data;
3923 /* Copy name from VAR to RESULT. */
3924 DECL_NAME (result) = DECL_NAME (var);
3925 /* Don't forget that we take its address. */
3926 TREE_ADDRESSABLE (result) = TREE_ADDRESSABLE (var);
3927 /* Finally set DECL_VALUE_EXPR to avoid assigning
3928 a stack slot at -O0 for the original var and debug info
3929 uses RESULT location for VAR. */
3930 SET_DECL_VALUE_EXPR (var, result);
3931 DECL_HAS_VALUE_EXPR_P (var) = 1;
3933 data.var = var;
3934 data.result = result;
3935 data.visited.create (37);
3936 cp_walk_tree (tp, finalize_nrv_r, &data, 0);
3937 data.visited.dispose ();
3940 /* Create CP_OMP_CLAUSE_INFO for clause C. Returns true if it is invalid. */
3942 bool
3943 cxx_omp_create_clause_info (tree c, tree type, bool need_default_ctor,
3944 bool need_copy_ctor, bool need_copy_assignment)
3946 int save_errorcount = errorcount;
3947 tree info, t;
3949 /* Always allocate 3 elements for simplicity. These are the
3950 function decls for the ctor, dtor, and assignment op.
3951 This layout is known to the three lang hooks,
3952 cxx_omp_clause_default_init, cxx_omp_clause_copy_init,
3953 and cxx_omp_clause_assign_op. */
3954 info = make_tree_vec (3);
3955 CP_OMP_CLAUSE_INFO (c) = info;
3957 if (need_default_ctor || need_copy_ctor)
3959 if (need_default_ctor)
3960 t = get_default_ctor (type);
3961 else
3962 t = get_copy_ctor (type, tf_warning_or_error);
3964 if (t && !trivial_fn_p (t))
3965 TREE_VEC_ELT (info, 0) = t;
3968 if ((need_default_ctor || need_copy_ctor)
3969 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
3970 TREE_VEC_ELT (info, 1) = get_dtor (type, tf_warning_or_error);
3972 if (need_copy_assignment)
3974 t = get_copy_assign (type);
3976 if (t && !trivial_fn_p (t))
3977 TREE_VEC_ELT (info, 2) = t;
3980 return errorcount != save_errorcount;
3983 /* For all elements of CLAUSES, validate them vs OpenMP constraints.
3984 Remove any elements from the list that are invalid. */
3986 tree
3987 finish_omp_clauses (tree clauses)
3989 bitmap_head generic_head, firstprivate_head, lastprivate_head;
3990 tree c, t, *pc = &clauses;
3991 const char *name;
3993 bitmap_obstack_initialize (NULL);
3994 bitmap_initialize (&generic_head, &bitmap_default_obstack);
3995 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
3996 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
3998 for (pc = &clauses, c = clauses; c ; c = *pc)
4000 bool remove = false;
4002 switch (OMP_CLAUSE_CODE (c))
4004 case OMP_CLAUSE_SHARED:
4005 name = "shared";
4006 goto check_dup_generic;
4007 case OMP_CLAUSE_PRIVATE:
4008 name = "private";
4009 goto check_dup_generic;
4010 case OMP_CLAUSE_REDUCTION:
4011 name = "reduction";
4012 goto check_dup_generic;
4013 case OMP_CLAUSE_COPYPRIVATE:
4014 name = "copyprivate";
4015 goto check_dup_generic;
4016 case OMP_CLAUSE_COPYIN:
4017 name = "copyin";
4018 goto check_dup_generic;
4019 check_dup_generic:
4020 t = OMP_CLAUSE_DECL (c);
4021 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
4023 if (processing_template_decl)
4024 break;
4025 if (DECL_P (t))
4026 error ("%qD is not a variable in clause %qs", t, name);
4027 else
4028 error ("%qE is not a variable in clause %qs", t, name);
4029 remove = true;
4031 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
4032 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
4033 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
4035 error ("%qD appears more than once in data clauses", t);
4036 remove = true;
4038 else
4039 bitmap_set_bit (&generic_head, DECL_UID (t));
4040 break;
4042 case OMP_CLAUSE_FIRSTPRIVATE:
4043 t = OMP_CLAUSE_DECL (c);
4044 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
4046 if (processing_template_decl)
4047 break;
4048 if (DECL_P (t))
4049 error ("%qD is not a variable in clause %<firstprivate%>", t);
4050 else
4051 error ("%qE is not a variable in clause %<firstprivate%>", t);
4052 remove = true;
4054 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
4055 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
4057 error ("%qD appears more than once in data clauses", t);
4058 remove = true;
4060 else
4061 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
4062 break;
4064 case OMP_CLAUSE_LASTPRIVATE:
4065 t = OMP_CLAUSE_DECL (c);
4066 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
4068 if (processing_template_decl)
4069 break;
4070 if (DECL_P (t))
4071 error ("%qD is not a variable in clause %<lastprivate%>", t);
4072 else
4073 error ("%qE is not a variable in clause %<lastprivate%>", t);
4074 remove = true;
4076 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
4077 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
4079 error ("%qD appears more than once in data clauses", t);
4080 remove = true;
4082 else
4083 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
4084 break;
4086 case OMP_CLAUSE_IF:
4087 t = OMP_CLAUSE_IF_EXPR (c);
4088 t = maybe_convert_cond (t);
4089 if (t == error_mark_node)
4090 remove = true;
4091 else if (!processing_template_decl)
4092 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
4093 OMP_CLAUSE_IF_EXPR (c) = t;
4094 break;
4096 case OMP_CLAUSE_FINAL:
4097 t = OMP_CLAUSE_FINAL_EXPR (c);
4098 t = maybe_convert_cond (t);
4099 if (t == error_mark_node)
4100 remove = true;
4101 else if (!processing_template_decl)
4102 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
4103 OMP_CLAUSE_FINAL_EXPR (c) = t;
4104 break;
4106 case OMP_CLAUSE_NUM_THREADS:
4107 t = OMP_CLAUSE_NUM_THREADS_EXPR (c);
4108 if (t == error_mark_node)
4109 remove = true;
4110 else if (!type_dependent_expression_p (t)
4111 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
4113 error ("num_threads expression must be integral");
4114 remove = true;
4116 else
4118 t = mark_rvalue_use (t);
4119 if (!processing_template_decl)
4120 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
4121 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
4123 break;
4125 case OMP_CLAUSE_SCHEDULE:
4126 t = OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c);
4127 if (t == NULL)
4129 else if (t == error_mark_node)
4130 remove = true;
4131 else if (!type_dependent_expression_p (t)
4132 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
4134 error ("schedule chunk size expression must be integral");
4135 remove = true;
4137 else
4139 t = mark_rvalue_use (t);
4140 if (!processing_template_decl)
4141 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
4142 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
4144 break;
4146 case OMP_CLAUSE_NOWAIT:
4147 case OMP_CLAUSE_ORDERED:
4148 case OMP_CLAUSE_DEFAULT:
4149 case OMP_CLAUSE_UNTIED:
4150 case OMP_CLAUSE_COLLAPSE:
4151 case OMP_CLAUSE_MERGEABLE:
4152 break;
4154 default:
4155 gcc_unreachable ();
4158 if (remove)
4159 *pc = OMP_CLAUSE_CHAIN (c);
4160 else
4161 pc = &OMP_CLAUSE_CHAIN (c);
4164 for (pc = &clauses, c = clauses; c ; c = *pc)
4166 enum omp_clause_code c_kind = OMP_CLAUSE_CODE (c);
4167 bool remove = false;
4168 bool need_complete_non_reference = false;
4169 bool need_default_ctor = false;
4170 bool need_copy_ctor = false;
4171 bool need_copy_assignment = false;
4172 bool need_implicitly_determined = false;
4173 tree type, inner_type;
4175 switch (c_kind)
4177 case OMP_CLAUSE_SHARED:
4178 name = "shared";
4179 need_implicitly_determined = true;
4180 break;
4181 case OMP_CLAUSE_PRIVATE:
4182 name = "private";
4183 need_complete_non_reference = true;
4184 need_default_ctor = true;
4185 need_implicitly_determined = true;
4186 break;
4187 case OMP_CLAUSE_FIRSTPRIVATE:
4188 name = "firstprivate";
4189 need_complete_non_reference = true;
4190 need_copy_ctor = true;
4191 need_implicitly_determined = true;
4192 break;
4193 case OMP_CLAUSE_LASTPRIVATE:
4194 name = "lastprivate";
4195 need_complete_non_reference = true;
4196 need_copy_assignment = true;
4197 need_implicitly_determined = true;
4198 break;
4199 case OMP_CLAUSE_REDUCTION:
4200 name = "reduction";
4201 need_implicitly_determined = true;
4202 break;
4203 case OMP_CLAUSE_COPYPRIVATE:
4204 name = "copyprivate";
4205 need_copy_assignment = true;
4206 break;
4207 case OMP_CLAUSE_COPYIN:
4208 name = "copyin";
4209 need_copy_assignment = true;
4210 break;
4211 default:
4212 pc = &OMP_CLAUSE_CHAIN (c);
4213 continue;
4216 t = OMP_CLAUSE_DECL (c);
4217 if (processing_template_decl
4218 && TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
4220 pc = &OMP_CLAUSE_CHAIN (c);
4221 continue;
4224 switch (c_kind)
4226 case OMP_CLAUSE_LASTPRIVATE:
4227 if (!bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
4228 need_default_ctor = true;
4229 break;
4231 case OMP_CLAUSE_REDUCTION:
4232 if (AGGREGATE_TYPE_P (TREE_TYPE (t))
4233 || POINTER_TYPE_P (TREE_TYPE (t)))
4235 error ("%qE has invalid type for %<reduction%>", t);
4236 remove = true;
4238 else if (FLOAT_TYPE_P (TREE_TYPE (t)))
4240 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
4241 switch (r_code)
4243 case PLUS_EXPR:
4244 case MULT_EXPR:
4245 case MINUS_EXPR:
4246 case MIN_EXPR:
4247 case MAX_EXPR:
4248 break;
4249 default:
4250 error ("%qE has invalid type for %<reduction(%s)%>",
4251 t, operator_name_info[r_code].name);
4252 remove = true;
4255 break;
4257 case OMP_CLAUSE_COPYIN:
4258 if (TREE_CODE (t) != VAR_DECL || !DECL_THREAD_LOCAL_P (t))
4260 error ("%qE must be %<threadprivate%> for %<copyin%>", t);
4261 remove = true;
4263 break;
4265 default:
4266 break;
4269 if (need_complete_non_reference || need_copy_assignment)
4271 t = require_complete_type (t);
4272 if (t == error_mark_node)
4273 remove = true;
4274 else if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE
4275 && need_complete_non_reference)
4277 error ("%qE has reference type for %qs", t, name);
4278 remove = true;
4281 if (need_implicitly_determined)
4283 const char *share_name = NULL;
4285 if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
4286 share_name = "threadprivate";
4287 else switch (cxx_omp_predetermined_sharing (t))
4289 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
4290 break;
4291 case OMP_CLAUSE_DEFAULT_SHARED:
4292 /* const vars may be specified in firstprivate clause. */
4293 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
4294 && cxx_omp_const_qual_no_mutable (t))
4295 break;
4296 share_name = "shared";
4297 break;
4298 case OMP_CLAUSE_DEFAULT_PRIVATE:
4299 share_name = "private";
4300 break;
4301 default:
4302 gcc_unreachable ();
4304 if (share_name)
4306 error ("%qE is predetermined %qs for %qs",
4307 t, share_name, name);
4308 remove = true;
4312 /* We're interested in the base element, not arrays. */
4313 inner_type = type = TREE_TYPE (t);
4314 while (TREE_CODE (inner_type) == ARRAY_TYPE)
4315 inner_type = TREE_TYPE (inner_type);
4317 /* Check for special function availability by building a call to one.
4318 Save the results, because later we won't be in the right context
4319 for making these queries. */
4320 if (CLASS_TYPE_P (inner_type)
4321 && COMPLETE_TYPE_P (inner_type)
4322 && (need_default_ctor || need_copy_ctor || need_copy_assignment)
4323 && !type_dependent_expression_p (t)
4324 && cxx_omp_create_clause_info (c, inner_type, need_default_ctor,
4325 need_copy_ctor, need_copy_assignment))
4326 remove = true;
4328 if (remove)
4329 *pc = OMP_CLAUSE_CHAIN (c);
4330 else
4331 pc = &OMP_CLAUSE_CHAIN (c);
4334 bitmap_obstack_release (NULL);
4335 return clauses;
4338 /* For all variables in the tree_list VARS, mark them as thread local. */
4340 void
4341 finish_omp_threadprivate (tree vars)
4343 tree t;
4345 /* Mark every variable in VARS to be assigned thread local storage. */
4346 for (t = vars; t; t = TREE_CHAIN (t))
4348 tree v = TREE_PURPOSE (t);
4350 if (error_operand_p (v))
4352 else if (TREE_CODE (v) != VAR_DECL)
4353 error ("%<threadprivate%> %qD is not file, namespace "
4354 "or block scope variable", v);
4355 /* If V had already been marked threadprivate, it doesn't matter
4356 whether it had been used prior to this point. */
4357 else if (TREE_USED (v)
4358 && (DECL_LANG_SPECIFIC (v) == NULL
4359 || !CP_DECL_THREADPRIVATE_P (v)))
4360 error ("%qE declared %<threadprivate%> after first use", v);
4361 else if (! TREE_STATIC (v) && ! DECL_EXTERNAL (v))
4362 error ("automatic variable %qE cannot be %<threadprivate%>", v);
4363 else if (! COMPLETE_TYPE_P (complete_type (TREE_TYPE (v))))
4364 error ("%<threadprivate%> %qE has incomplete type", v);
4365 else if (TREE_STATIC (v) && TYPE_P (CP_DECL_CONTEXT (v))
4366 && CP_DECL_CONTEXT (v) != current_class_type)
4367 error ("%<threadprivate%> %qE directive not "
4368 "in %qT definition", v, CP_DECL_CONTEXT (v));
4369 else
4371 /* Allocate a LANG_SPECIFIC structure for V, if needed. */
4372 if (DECL_LANG_SPECIFIC (v) == NULL)
4374 retrofit_lang_decl (v);
4376 /* Make sure that DECL_DISCRIMINATOR_P continues to be true
4377 after the allocation of the lang_decl structure. */
4378 if (DECL_DISCRIMINATOR_P (v))
4379 DECL_LANG_SPECIFIC (v)->u.base.u2sel = 1;
4382 if (! DECL_THREAD_LOCAL_P (v))
4384 DECL_TLS_MODEL (v) = decl_default_tls_model (v);
4385 /* If rtl has been already set for this var, call
4386 make_decl_rtl once again, so that encode_section_info
4387 has a chance to look at the new decl flags. */
4388 if (DECL_RTL_SET_P (v))
4389 make_decl_rtl (v);
4391 CP_DECL_THREADPRIVATE_P (v) = 1;
4396 /* Build an OpenMP structured block. */
4398 tree
4399 begin_omp_structured_block (void)
4401 return do_pushlevel (sk_omp);
4404 tree
4405 finish_omp_structured_block (tree block)
4407 return do_poplevel (block);
4410 /* Similarly, except force the retention of the BLOCK. */
4412 tree
4413 begin_omp_parallel (void)
4415 keep_next_level (true);
4416 return begin_omp_structured_block ();
4419 tree
4420 finish_omp_parallel (tree clauses, tree body)
4422 tree stmt;
4424 body = finish_omp_structured_block (body);
4426 stmt = make_node (OMP_PARALLEL);
4427 TREE_TYPE (stmt) = void_type_node;
4428 OMP_PARALLEL_CLAUSES (stmt) = clauses;
4429 OMP_PARALLEL_BODY (stmt) = body;
4431 return add_stmt (stmt);
4434 tree
4435 begin_omp_task (void)
4437 keep_next_level (true);
4438 return begin_omp_structured_block ();
4441 tree
4442 finish_omp_task (tree clauses, tree body)
4444 tree stmt;
4446 body = finish_omp_structured_block (body);
4448 stmt = make_node (OMP_TASK);
4449 TREE_TYPE (stmt) = void_type_node;
4450 OMP_TASK_CLAUSES (stmt) = clauses;
4451 OMP_TASK_BODY (stmt) = body;
4453 return add_stmt (stmt);
4456 /* Helper function for finish_omp_for. Convert Ith random access iterator
4457 into integral iterator. Return FALSE if successful. */
4459 static bool
4460 handle_omp_for_class_iterator (int i, location_t locus, tree declv, tree initv,
4461 tree condv, tree incrv, tree *body,
4462 tree *pre_body, tree clauses)
4464 tree diff, iter_init, iter_incr = NULL, last;
4465 tree incr_var = NULL, orig_pre_body, orig_body, c;
4466 tree decl = TREE_VEC_ELT (declv, i);
4467 tree init = TREE_VEC_ELT (initv, i);
4468 tree cond = TREE_VEC_ELT (condv, i);
4469 tree incr = TREE_VEC_ELT (incrv, i);
4470 tree iter = decl;
4471 location_t elocus = locus;
4473 if (init && EXPR_HAS_LOCATION (init))
4474 elocus = EXPR_LOCATION (init);
4476 switch (TREE_CODE (cond))
4478 case GT_EXPR:
4479 case GE_EXPR:
4480 case LT_EXPR:
4481 case LE_EXPR:
4482 if (TREE_OPERAND (cond, 1) == iter)
4483 cond = build2 (swap_tree_comparison (TREE_CODE (cond)),
4484 TREE_TYPE (cond), iter, TREE_OPERAND (cond, 0));
4485 if (TREE_OPERAND (cond, 0) != iter)
4486 cond = error_mark_node;
4487 else
4489 tree tem = build_x_binary_op (EXPR_LOCATION (cond),
4490 TREE_CODE (cond),
4491 iter, ERROR_MARK,
4492 TREE_OPERAND (cond, 1), ERROR_MARK,
4493 NULL, tf_warning_or_error);
4494 if (error_operand_p (tem))
4495 return true;
4497 break;
4498 default:
4499 cond = error_mark_node;
4500 break;
4502 if (cond == error_mark_node)
4504 error_at (elocus, "invalid controlling predicate");
4505 return true;
4507 diff = build_x_binary_op (elocus, MINUS_EXPR, TREE_OPERAND (cond, 1),
4508 ERROR_MARK, iter, ERROR_MARK, NULL,
4509 tf_warning_or_error);
4510 if (error_operand_p (diff))
4511 return true;
4512 if (TREE_CODE (TREE_TYPE (diff)) != INTEGER_TYPE)
4514 error_at (elocus, "difference between %qE and %qD does not have integer type",
4515 TREE_OPERAND (cond, 1), iter);
4516 return true;
4519 switch (TREE_CODE (incr))
4521 case PREINCREMENT_EXPR:
4522 case PREDECREMENT_EXPR:
4523 case POSTINCREMENT_EXPR:
4524 case POSTDECREMENT_EXPR:
4525 if (TREE_OPERAND (incr, 0) != iter)
4527 incr = error_mark_node;
4528 break;
4530 iter_incr = build_x_unary_op (EXPR_LOCATION (incr),
4531 TREE_CODE (incr), iter,
4532 tf_warning_or_error);
4533 if (error_operand_p (iter_incr))
4534 return true;
4535 else if (TREE_CODE (incr) == PREINCREMENT_EXPR
4536 || TREE_CODE (incr) == POSTINCREMENT_EXPR)
4537 incr = integer_one_node;
4538 else
4539 incr = integer_minus_one_node;
4540 break;
4541 case MODIFY_EXPR:
4542 if (TREE_OPERAND (incr, 0) != iter)
4543 incr = error_mark_node;
4544 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
4545 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
4547 tree rhs = TREE_OPERAND (incr, 1);
4548 if (TREE_OPERAND (rhs, 0) == iter)
4550 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (rhs, 1)))
4551 != INTEGER_TYPE)
4552 incr = error_mark_node;
4553 else
4555 iter_incr = build_x_modify_expr (EXPR_LOCATION (rhs),
4556 iter, TREE_CODE (rhs),
4557 TREE_OPERAND (rhs, 1),
4558 tf_warning_or_error);
4559 if (error_operand_p (iter_incr))
4560 return true;
4561 incr = TREE_OPERAND (rhs, 1);
4562 incr = cp_convert (TREE_TYPE (diff), incr,
4563 tf_warning_or_error);
4564 if (TREE_CODE (rhs) == MINUS_EXPR)
4566 incr = build1 (NEGATE_EXPR, TREE_TYPE (diff), incr);
4567 incr = fold_if_not_in_template (incr);
4569 if (TREE_CODE (incr) != INTEGER_CST
4570 && (TREE_CODE (incr) != NOP_EXPR
4571 || (TREE_CODE (TREE_OPERAND (incr, 0))
4572 != INTEGER_CST)))
4573 iter_incr = NULL;
4576 else if (TREE_OPERAND (rhs, 1) == iter)
4578 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (rhs, 0))) != INTEGER_TYPE
4579 || TREE_CODE (rhs) != PLUS_EXPR)
4580 incr = error_mark_node;
4581 else
4583 iter_incr = build_x_binary_op (EXPR_LOCATION (rhs),
4584 PLUS_EXPR,
4585 TREE_OPERAND (rhs, 0),
4586 ERROR_MARK, iter,
4587 ERROR_MARK, NULL,
4588 tf_warning_or_error);
4589 if (error_operand_p (iter_incr))
4590 return true;
4591 iter_incr = build_x_modify_expr (EXPR_LOCATION (rhs),
4592 iter, NOP_EXPR,
4593 iter_incr,
4594 tf_warning_or_error);
4595 if (error_operand_p (iter_incr))
4596 return true;
4597 incr = TREE_OPERAND (rhs, 0);
4598 iter_incr = NULL;
4601 else
4602 incr = error_mark_node;
4604 else
4605 incr = error_mark_node;
4606 break;
4607 default:
4608 incr = error_mark_node;
4609 break;
4612 if (incr == error_mark_node)
4614 error_at (elocus, "invalid increment expression");
4615 return true;
4618 incr = cp_convert (TREE_TYPE (diff), incr, tf_warning_or_error);
4619 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
4620 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
4621 && OMP_CLAUSE_DECL (c) == iter)
4622 break;
4624 decl = create_temporary_var (TREE_TYPE (diff));
4625 pushdecl (decl);
4626 add_decl_expr (decl);
4627 last = create_temporary_var (TREE_TYPE (diff));
4628 pushdecl (last);
4629 add_decl_expr (last);
4630 if (c && iter_incr == NULL)
4632 incr_var = create_temporary_var (TREE_TYPE (diff));
4633 pushdecl (incr_var);
4634 add_decl_expr (incr_var);
4636 gcc_assert (stmts_are_full_exprs_p ());
4638 orig_pre_body = *pre_body;
4639 *pre_body = push_stmt_list ();
4640 if (orig_pre_body)
4641 add_stmt (orig_pre_body);
4642 if (init != NULL)
4643 finish_expr_stmt (build_x_modify_expr (elocus,
4644 iter, NOP_EXPR, init,
4645 tf_warning_or_error));
4646 init = build_int_cst (TREE_TYPE (diff), 0);
4647 if (c && iter_incr == NULL)
4649 finish_expr_stmt (build_x_modify_expr (elocus,
4650 incr_var, NOP_EXPR,
4651 incr, tf_warning_or_error));
4652 incr = incr_var;
4653 iter_incr = build_x_modify_expr (elocus,
4654 iter, PLUS_EXPR, incr,
4655 tf_warning_or_error);
4657 finish_expr_stmt (build_x_modify_expr (elocus,
4658 last, NOP_EXPR, init,
4659 tf_warning_or_error));
4660 *pre_body = pop_stmt_list (*pre_body);
4662 cond = cp_build_binary_op (elocus,
4663 TREE_CODE (cond), decl, diff,
4664 tf_warning_or_error);
4665 incr = build_modify_expr (elocus, decl, NULL_TREE, PLUS_EXPR,
4666 elocus, incr, NULL_TREE);
4668 orig_body = *body;
4669 *body = push_stmt_list ();
4670 iter_init = build2 (MINUS_EXPR, TREE_TYPE (diff), decl, last);
4671 iter_init = build_x_modify_expr (elocus,
4672 iter, PLUS_EXPR, iter_init,
4673 tf_warning_or_error);
4674 iter_init = build1 (NOP_EXPR, void_type_node, iter_init);
4675 finish_expr_stmt (iter_init);
4676 finish_expr_stmt (build_x_modify_expr (elocus,
4677 last, NOP_EXPR, decl,
4678 tf_warning_or_error));
4679 add_stmt (orig_body);
4680 *body = pop_stmt_list (*body);
4682 if (c)
4684 OMP_CLAUSE_LASTPRIVATE_STMT (c) = push_stmt_list ();
4685 finish_expr_stmt (iter_incr);
4686 OMP_CLAUSE_LASTPRIVATE_STMT (c)
4687 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (c));
4690 TREE_VEC_ELT (declv, i) = decl;
4691 TREE_VEC_ELT (initv, i) = init;
4692 TREE_VEC_ELT (condv, i) = cond;
4693 TREE_VEC_ELT (incrv, i) = incr;
4695 return false;
4698 /* Build and validate an OMP_FOR statement. CLAUSES, BODY, COND, INCR
4699 are directly for their associated operands in the statement. DECL
4700 and INIT are a combo; if DECL is NULL then INIT ought to be a
4701 MODIFY_EXPR, and the DECL should be extracted. PRE_BODY are
4702 optional statements that need to go before the loop into its
4703 sk_omp scope. */
4705 tree
4706 finish_omp_for (location_t locus, tree declv, tree initv, tree condv,
4707 tree incrv, tree body, tree pre_body, tree clauses)
4709 tree omp_for = NULL, orig_incr = NULL;
4710 tree decl, init, cond, incr;
4711 location_t elocus;
4712 int i;
4714 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (initv));
4715 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (condv));
4716 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (incrv));
4717 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
4719 decl = TREE_VEC_ELT (declv, i);
4720 init = TREE_VEC_ELT (initv, i);
4721 cond = TREE_VEC_ELT (condv, i);
4722 incr = TREE_VEC_ELT (incrv, i);
4723 elocus = locus;
4725 if (decl == NULL)
4727 if (init != NULL)
4728 switch (TREE_CODE (init))
4730 case MODIFY_EXPR:
4731 decl = TREE_OPERAND (init, 0);
4732 init = TREE_OPERAND (init, 1);
4733 break;
4734 case MODOP_EXPR:
4735 if (TREE_CODE (TREE_OPERAND (init, 1)) == NOP_EXPR)
4737 decl = TREE_OPERAND (init, 0);
4738 init = TREE_OPERAND (init, 2);
4740 break;
4741 default:
4742 break;
4745 if (decl == NULL)
4747 error_at (locus,
4748 "expected iteration declaration or initialization");
4749 return NULL;
4753 if (init && EXPR_HAS_LOCATION (init))
4754 elocus = EXPR_LOCATION (init);
4756 if (cond == NULL)
4758 error_at (elocus, "missing controlling predicate");
4759 return NULL;
4762 if (incr == NULL)
4764 error_at (elocus, "missing increment expression");
4765 return NULL;
4768 TREE_VEC_ELT (declv, i) = decl;
4769 TREE_VEC_ELT (initv, i) = init;
4772 if (dependent_omp_for_p (declv, initv, condv, incrv))
4774 tree stmt;
4776 stmt = make_node (OMP_FOR);
4778 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
4780 /* This is really just a place-holder. We'll be decomposing this
4781 again and going through the cp_build_modify_expr path below when
4782 we instantiate the thing. */
4783 TREE_VEC_ELT (initv, i)
4784 = build2 (MODIFY_EXPR, void_type_node, TREE_VEC_ELT (declv, i),
4785 TREE_VEC_ELT (initv, i));
4788 TREE_TYPE (stmt) = void_type_node;
4789 OMP_FOR_INIT (stmt) = initv;
4790 OMP_FOR_COND (stmt) = condv;
4791 OMP_FOR_INCR (stmt) = incrv;
4792 OMP_FOR_BODY (stmt) = body;
4793 OMP_FOR_PRE_BODY (stmt) = pre_body;
4794 OMP_FOR_CLAUSES (stmt) = clauses;
4796 SET_EXPR_LOCATION (stmt, locus);
4797 return add_stmt (stmt);
4800 if (processing_template_decl)
4801 orig_incr = make_tree_vec (TREE_VEC_LENGTH (incrv));
4803 for (i = 0; i < TREE_VEC_LENGTH (declv); )
4805 decl = TREE_VEC_ELT (declv, i);
4806 init = TREE_VEC_ELT (initv, i);
4807 cond = TREE_VEC_ELT (condv, i);
4808 incr = TREE_VEC_ELT (incrv, i);
4809 if (orig_incr)
4810 TREE_VEC_ELT (orig_incr, i) = incr;
4811 elocus = locus;
4813 if (init && EXPR_HAS_LOCATION (init))
4814 elocus = EXPR_LOCATION (init);
4816 if (!DECL_P (decl))
4818 error_at (elocus, "expected iteration declaration or initialization");
4819 return NULL;
4822 if (incr && TREE_CODE (incr) == MODOP_EXPR)
4824 if (orig_incr)
4825 TREE_VEC_ELT (orig_incr, i) = incr;
4826 incr = cp_build_modify_expr (TREE_OPERAND (incr, 0),
4827 TREE_CODE (TREE_OPERAND (incr, 1)),
4828 TREE_OPERAND (incr, 2),
4829 tf_warning_or_error);
4832 if (CLASS_TYPE_P (TREE_TYPE (decl)))
4834 if (handle_omp_for_class_iterator (i, locus, declv, initv, condv,
4835 incrv, &body, &pre_body, clauses))
4836 return NULL;
4837 continue;
4840 if (!INTEGRAL_TYPE_P (TREE_TYPE (decl))
4841 && TREE_CODE (TREE_TYPE (decl)) != POINTER_TYPE)
4843 error_at (elocus, "invalid type for iteration variable %qE", decl);
4844 return NULL;
4847 if (!processing_template_decl)
4849 init = fold_build_cleanup_point_expr (TREE_TYPE (init), init);
4850 init = cp_build_modify_expr (decl, NOP_EXPR, init, tf_warning_or_error);
4852 else
4853 init = build2 (MODIFY_EXPR, void_type_node, decl, init);
4854 if (cond
4855 && TREE_SIDE_EFFECTS (cond)
4856 && COMPARISON_CLASS_P (cond)
4857 && !processing_template_decl)
4859 tree t = TREE_OPERAND (cond, 0);
4860 if (TREE_SIDE_EFFECTS (t)
4861 && t != decl
4862 && (TREE_CODE (t) != NOP_EXPR
4863 || TREE_OPERAND (t, 0) != decl))
4864 TREE_OPERAND (cond, 0)
4865 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
4867 t = TREE_OPERAND (cond, 1);
4868 if (TREE_SIDE_EFFECTS (t)
4869 && t != decl
4870 && (TREE_CODE (t) != NOP_EXPR
4871 || TREE_OPERAND (t, 0) != decl))
4872 TREE_OPERAND (cond, 1)
4873 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
4875 if (decl == error_mark_node || init == error_mark_node)
4876 return NULL;
4878 TREE_VEC_ELT (declv, i) = decl;
4879 TREE_VEC_ELT (initv, i) = init;
4880 TREE_VEC_ELT (condv, i) = cond;
4881 TREE_VEC_ELT (incrv, i) = incr;
4882 i++;
4885 if (IS_EMPTY_STMT (pre_body))
4886 pre_body = NULL;
4888 omp_for = c_finish_omp_for (locus, declv, initv, condv, incrv,
4889 body, pre_body);
4891 if (omp_for == NULL)
4892 return NULL;
4894 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INCR (omp_for)); i++)
4896 decl = TREE_OPERAND (TREE_VEC_ELT (OMP_FOR_INIT (omp_for), i), 0);
4897 incr = TREE_VEC_ELT (OMP_FOR_INCR (omp_for), i);
4899 if (TREE_CODE (incr) != MODIFY_EXPR)
4900 continue;
4902 if (TREE_SIDE_EFFECTS (TREE_OPERAND (incr, 1))
4903 && BINARY_CLASS_P (TREE_OPERAND (incr, 1))
4904 && !processing_template_decl)
4906 tree t = TREE_OPERAND (TREE_OPERAND (incr, 1), 0);
4907 if (TREE_SIDE_EFFECTS (t)
4908 && t != decl
4909 && (TREE_CODE (t) != NOP_EXPR
4910 || TREE_OPERAND (t, 0) != decl))
4911 TREE_OPERAND (TREE_OPERAND (incr, 1), 0)
4912 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
4914 t = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
4915 if (TREE_SIDE_EFFECTS (t)
4916 && t != decl
4917 && (TREE_CODE (t) != NOP_EXPR
4918 || TREE_OPERAND (t, 0) != decl))
4919 TREE_OPERAND (TREE_OPERAND (incr, 1), 1)
4920 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
4923 if (orig_incr)
4924 TREE_VEC_ELT (OMP_FOR_INCR (omp_for), i) = TREE_VEC_ELT (orig_incr, i);
4926 if (omp_for != NULL)
4927 OMP_FOR_CLAUSES (omp_for) = clauses;
4928 return omp_for;
4931 void
4932 finish_omp_atomic (enum tree_code code, enum tree_code opcode, tree lhs,
4933 tree rhs, tree v, tree lhs1, tree rhs1)
4935 tree orig_lhs;
4936 tree orig_rhs;
4937 tree orig_v;
4938 tree orig_lhs1;
4939 tree orig_rhs1;
4940 bool dependent_p;
4941 tree stmt;
4943 orig_lhs = lhs;
4944 orig_rhs = rhs;
4945 orig_v = v;
4946 orig_lhs1 = lhs1;
4947 orig_rhs1 = rhs1;
4948 dependent_p = false;
4949 stmt = NULL_TREE;
4951 /* Even in a template, we can detect invalid uses of the atomic
4952 pragma if neither LHS nor RHS is type-dependent. */
4953 if (processing_template_decl)
4955 dependent_p = (type_dependent_expression_p (lhs)
4956 || (rhs && type_dependent_expression_p (rhs))
4957 || (v && type_dependent_expression_p (v))
4958 || (lhs1 && type_dependent_expression_p (lhs1))
4959 || (rhs1 && type_dependent_expression_p (rhs1)));
4960 if (!dependent_p)
4962 lhs = build_non_dependent_expr (lhs);
4963 if (rhs)
4964 rhs = build_non_dependent_expr (rhs);
4965 if (v)
4966 v = build_non_dependent_expr (v);
4967 if (lhs1)
4968 lhs1 = build_non_dependent_expr (lhs1);
4969 if (rhs1)
4970 rhs1 = build_non_dependent_expr (rhs1);
4973 if (!dependent_p)
4975 stmt = c_finish_omp_atomic (input_location, code, opcode, lhs, rhs,
4976 v, lhs1, rhs1);
4977 if (stmt == error_mark_node)
4978 return;
4980 if (processing_template_decl)
4982 if (code == OMP_ATOMIC_READ)
4984 stmt = build_min_nt_loc (EXPR_LOCATION (orig_lhs),
4985 OMP_ATOMIC_READ, orig_lhs);
4986 stmt = build2 (MODIFY_EXPR, void_type_node, orig_v, stmt);
4988 else
4990 if (opcode == NOP_EXPR)
4991 stmt = build2 (MODIFY_EXPR, void_type_node, orig_lhs, orig_rhs);
4992 else
4993 stmt = build2 (opcode, void_type_node, orig_lhs, orig_rhs);
4994 if (orig_rhs1)
4995 stmt = build_min_nt_loc (EXPR_LOCATION (orig_rhs1),
4996 COMPOUND_EXPR, orig_rhs1, stmt);
4997 if (code != OMP_ATOMIC)
4999 stmt = build_min_nt_loc (EXPR_LOCATION (orig_lhs1),
5000 code, orig_lhs1, stmt);
5001 stmt = build2 (MODIFY_EXPR, void_type_node, orig_v, stmt);
5004 stmt = build2 (OMP_ATOMIC, void_type_node, integer_zero_node, stmt);
5006 add_stmt (stmt);
5009 void
5010 finish_omp_barrier (void)
5012 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_BARRIER);
5013 VEC(tree,gc) *vec = make_tree_vector ();
5014 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
5015 release_tree_vector (vec);
5016 finish_expr_stmt (stmt);
5019 void
5020 finish_omp_flush (void)
5022 tree fn = builtin_decl_explicit (BUILT_IN_SYNC_SYNCHRONIZE);
5023 VEC(tree,gc) *vec = make_tree_vector ();
5024 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
5025 release_tree_vector (vec);
5026 finish_expr_stmt (stmt);
5029 void
5030 finish_omp_taskwait (void)
5032 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_TASKWAIT);
5033 VEC(tree,gc) *vec = make_tree_vector ();
5034 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
5035 release_tree_vector (vec);
5036 finish_expr_stmt (stmt);
5039 void
5040 finish_omp_taskyield (void)
5042 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_TASKYIELD);
5043 VEC(tree,gc) *vec = make_tree_vector ();
5044 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
5045 release_tree_vector (vec);
5046 finish_expr_stmt (stmt);
5049 /* Begin a __transaction_atomic or __transaction_relaxed statement.
5050 If PCOMPOUND is non-null, this is for a function-transaction-block, and we
5051 should create an extra compound stmt. */
5053 tree
5054 begin_transaction_stmt (location_t loc, tree *pcompound, int flags)
5056 tree r;
5058 if (pcompound)
5059 *pcompound = begin_compound_stmt (0);
5061 r = build_stmt (loc, TRANSACTION_EXPR, NULL_TREE);
5063 /* Only add the statement to the function if support enabled. */
5064 if (flag_tm)
5065 add_stmt (r);
5066 else
5067 error_at (loc, ((flags & TM_STMT_ATTR_RELAXED) != 0
5068 ? G_("%<__transaction_relaxed%> without "
5069 "transactional memory support enabled")
5070 : G_("%<__transaction_atomic%> without "
5071 "transactional memory support enabled")));
5073 TRANSACTION_EXPR_BODY (r) = push_stmt_list ();
5074 return r;
5077 /* End a __transaction_atomic or __transaction_relaxed statement.
5078 If COMPOUND_STMT is non-null, this is for a function-transaction-block,
5079 and we should end the compound. If NOEX is non-NULL, we wrap the body in
5080 a MUST_NOT_THROW_EXPR with NOEX as condition. */
5082 void
5083 finish_transaction_stmt (tree stmt, tree compound_stmt, int flags, tree noex)
5085 TRANSACTION_EXPR_BODY (stmt) = pop_stmt_list (TRANSACTION_EXPR_BODY (stmt));
5086 TRANSACTION_EXPR_OUTER (stmt) = (flags & TM_STMT_ATTR_OUTER) != 0;
5087 TRANSACTION_EXPR_RELAXED (stmt) = (flags & TM_STMT_ATTR_RELAXED) != 0;
5088 TRANSACTION_EXPR_IS_STMT (stmt) = 1;
5090 /* noexcept specifications are not allowed for function transactions. */
5091 gcc_assert (!(noex && compound_stmt));
5092 if (noex)
5094 tree body = build_must_not_throw_expr (TRANSACTION_EXPR_BODY (stmt),
5095 noex);
5096 SET_EXPR_LOCATION (body, EXPR_LOCATION (TRANSACTION_EXPR_BODY (stmt)));
5097 TREE_SIDE_EFFECTS (body) = 1;
5098 TRANSACTION_EXPR_BODY (stmt) = body;
5101 if (compound_stmt)
5102 finish_compound_stmt (compound_stmt);
5103 finish_stmt ();
5106 /* Build a __transaction_atomic or __transaction_relaxed expression. If
5107 NOEX is non-NULL, we wrap the body in a MUST_NOT_THROW_EXPR with NOEX as
5108 condition. */
5110 tree
5111 build_transaction_expr (location_t loc, tree expr, int flags, tree noex)
5113 tree ret;
5114 if (noex)
5116 expr = build_must_not_throw_expr (expr, noex);
5117 SET_EXPR_LOCATION (expr, loc);
5118 TREE_SIDE_EFFECTS (expr) = 1;
5120 ret = build1 (TRANSACTION_EXPR, TREE_TYPE (expr), expr);
5121 if (flags & TM_STMT_ATTR_RELAXED)
5122 TRANSACTION_EXPR_RELAXED (ret) = 1;
5123 SET_EXPR_LOCATION (ret, loc);
5124 return ret;
5127 void
5128 init_cp_semantics (void)
5132 /* Build a STATIC_ASSERT for a static assertion with the condition
5133 CONDITION and the message text MESSAGE. LOCATION is the location
5134 of the static assertion in the source code. When MEMBER_P, this
5135 static assertion is a member of a class. */
5136 void
5137 finish_static_assert (tree condition, tree message, location_t location,
5138 bool member_p)
5140 if (message == NULL_TREE
5141 || message == error_mark_node
5142 || condition == NULL_TREE
5143 || condition == error_mark_node)
5144 return;
5146 if (check_for_bare_parameter_packs (condition))
5147 condition = error_mark_node;
5149 if (type_dependent_expression_p (condition)
5150 || value_dependent_expression_p (condition))
5152 /* We're in a template; build a STATIC_ASSERT and put it in
5153 the right place. */
5154 tree assertion;
5156 assertion = make_node (STATIC_ASSERT);
5157 STATIC_ASSERT_CONDITION (assertion) = condition;
5158 STATIC_ASSERT_MESSAGE (assertion) = message;
5159 STATIC_ASSERT_SOURCE_LOCATION (assertion) = location;
5161 if (member_p)
5162 maybe_add_class_template_decl_list (current_class_type,
5163 assertion,
5164 /*friend_p=*/0);
5165 else
5166 add_stmt (assertion);
5168 return;
5171 /* Fold the expression and convert it to a boolean value. */
5172 condition = fold_non_dependent_expr (condition);
5173 condition = cp_convert (boolean_type_node, condition, tf_warning_or_error);
5174 condition = maybe_constant_value (condition);
5176 if (TREE_CODE (condition) == INTEGER_CST && !integer_zerop (condition))
5177 /* Do nothing; the condition is satisfied. */
5179 else
5181 location_t saved_loc = input_location;
5183 input_location = location;
5184 if (TREE_CODE (condition) == INTEGER_CST
5185 && integer_zerop (condition))
5186 /* Report the error. */
5187 error ("static assertion failed: %s", TREE_STRING_POINTER (message));
5188 else if (condition && condition != error_mark_node)
5190 error ("non-constant condition for static assertion");
5191 cxx_constant_value (condition);
5193 input_location = saved_loc;
5197 /* Implements the C++0x decltype keyword. Returns the type of EXPR,
5198 suitable for use as a type-specifier.
5200 ID_EXPRESSION_OR_MEMBER_ACCESS_P is true when EXPR was parsed as an
5201 id-expression or a class member access, FALSE when it was parsed as
5202 a full expression. */
5204 tree
5205 finish_decltype_type (tree expr, bool id_expression_or_member_access_p,
5206 tsubst_flags_t complain)
5208 tree type = NULL_TREE;
5210 if (!expr || error_operand_p (expr))
5211 return error_mark_node;
5213 if (TYPE_P (expr)
5214 || TREE_CODE (expr) == TYPE_DECL
5215 || (TREE_CODE (expr) == BIT_NOT_EXPR
5216 && TYPE_P (TREE_OPERAND (expr, 0))))
5218 if (complain & tf_error)
5219 error ("argument to decltype must be an expression");
5220 return error_mark_node;
5223 /* Depending on the resolution of DR 1172, we may later need to distinguish
5224 instantiation-dependent but not type-dependent expressions so that, say,
5225 A<decltype(sizeof(T))>::U doesn't require 'typename'. */
5226 if (instantiation_dependent_expression_p (expr))
5228 type = cxx_make_type (DECLTYPE_TYPE);
5229 DECLTYPE_TYPE_EXPR (type) = expr;
5230 DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (type)
5231 = id_expression_or_member_access_p;
5232 SET_TYPE_STRUCTURAL_EQUALITY (type);
5234 return type;
5237 /* The type denoted by decltype(e) is defined as follows: */
5239 expr = resolve_nondeduced_context (expr);
5241 if (type_unknown_p (expr))
5243 if (complain & tf_error)
5244 error ("decltype cannot resolve address of overloaded function");
5245 return error_mark_node;
5248 if (invalid_nonstatic_memfn_p (expr, complain))
5249 return error_mark_node;
5251 /* To get the size of a static data member declared as an array of
5252 unknown bound, we need to instantiate it. */
5253 if (TREE_CODE (expr) == VAR_DECL
5254 && VAR_HAD_UNKNOWN_BOUND (expr)
5255 && DECL_TEMPLATE_INSTANTIATION (expr))
5256 instantiate_decl (expr, /*defer_ok*/true, /*expl_inst_mem*/false);
5258 if (id_expression_or_member_access_p)
5260 /* If e is an id-expression or a class member access (5.2.5
5261 [expr.ref]), decltype(e) is defined as the type of the entity
5262 named by e. If there is no such entity, or e names a set of
5263 overloaded functions, the program is ill-formed. */
5264 if (TREE_CODE (expr) == IDENTIFIER_NODE)
5265 expr = lookup_name (expr);
5267 if (TREE_CODE (expr) == INDIRECT_REF)
5268 /* This can happen when the expression is, e.g., "a.b". Just
5269 look at the underlying operand. */
5270 expr = TREE_OPERAND (expr, 0);
5272 if (TREE_CODE (expr) == OFFSET_REF
5273 || TREE_CODE (expr) == MEMBER_REF)
5274 /* We're only interested in the field itself. If it is a
5275 BASELINK, we will need to see through it in the next
5276 step. */
5277 expr = TREE_OPERAND (expr, 1);
5279 if (BASELINK_P (expr))
5280 /* See through BASELINK nodes to the underlying function. */
5281 expr = BASELINK_FUNCTIONS (expr);
5283 switch (TREE_CODE (expr))
5285 case FIELD_DECL:
5286 if (DECL_BIT_FIELD_TYPE (expr))
5288 type = DECL_BIT_FIELD_TYPE (expr);
5289 break;
5291 /* Fall through for fields that aren't bitfields. */
5293 case FUNCTION_DECL:
5294 case VAR_DECL:
5295 case CONST_DECL:
5296 case PARM_DECL:
5297 case RESULT_DECL:
5298 case TEMPLATE_PARM_INDEX:
5299 expr = mark_type_use (expr);
5300 type = TREE_TYPE (expr);
5301 break;
5303 case ERROR_MARK:
5304 type = error_mark_node;
5305 break;
5307 case COMPONENT_REF:
5308 mark_type_use (expr);
5309 type = is_bitfield_expr_with_lowered_type (expr);
5310 if (!type)
5311 type = TREE_TYPE (TREE_OPERAND (expr, 1));
5312 break;
5314 case BIT_FIELD_REF:
5315 gcc_unreachable ();
5317 case INTEGER_CST:
5318 case PTRMEM_CST:
5319 /* We can get here when the id-expression refers to an
5320 enumerator or non-type template parameter. */
5321 type = TREE_TYPE (expr);
5322 break;
5324 default:
5325 gcc_unreachable ();
5326 return error_mark_node;
5329 else
5331 /* Within a lambda-expression:
5333 Every occurrence of decltype((x)) where x is a possibly
5334 parenthesized id-expression that names an entity of
5335 automatic storage duration is treated as if x were
5336 transformed into an access to a corresponding data member
5337 of the closure type that would have been declared if x
5338 were a use of the denoted entity. */
5339 if (outer_automatic_var_p (expr)
5340 && current_function_decl
5341 && LAMBDA_FUNCTION_P (current_function_decl))
5342 type = capture_decltype (expr);
5343 else if (error_operand_p (expr))
5344 type = error_mark_node;
5345 else if (expr == current_class_ptr)
5346 /* If the expression is just "this", we want the
5347 cv-unqualified pointer for the "this" type. */
5348 type = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
5349 else
5351 /* Otherwise, where T is the type of e, if e is an lvalue,
5352 decltype(e) is defined as T&; if an xvalue, T&&; otherwise, T. */
5353 cp_lvalue_kind clk = lvalue_kind (expr);
5354 type = unlowered_expr_type (expr);
5355 gcc_assert (TREE_CODE (type) != REFERENCE_TYPE);
5357 /* For vector types, pick a non-opaque variant. */
5358 if (TREE_CODE (type) == VECTOR_TYPE)
5359 type = strip_typedefs (type);
5361 if (clk != clk_none && !(clk & clk_class))
5362 type = cp_build_reference_type (type, (clk & clk_rvalueref));
5366 return type;
5369 /* Called from trait_expr_value to evaluate either __has_nothrow_assign or
5370 __has_nothrow_copy, depending on assign_p. */
5372 static bool
5373 classtype_has_nothrow_assign_or_copy_p (tree type, bool assign_p)
5375 tree fns;
5377 if (assign_p)
5379 int ix;
5380 ix = lookup_fnfields_1 (type, ansi_assopname (NOP_EXPR));
5381 if (ix < 0)
5382 return false;
5383 fns = VEC_index (tree, CLASSTYPE_METHOD_VEC (type), ix);
5385 else if (TYPE_HAS_COPY_CTOR (type))
5387 /* If construction of the copy constructor was postponed, create
5388 it now. */
5389 if (CLASSTYPE_LAZY_COPY_CTOR (type))
5390 lazily_declare_fn (sfk_copy_constructor, type);
5391 if (CLASSTYPE_LAZY_MOVE_CTOR (type))
5392 lazily_declare_fn (sfk_move_constructor, type);
5393 fns = CLASSTYPE_CONSTRUCTORS (type);
5395 else
5396 return false;
5398 for (; fns; fns = OVL_NEXT (fns))
5400 tree fn = OVL_CURRENT (fns);
5402 if (assign_p)
5404 if (copy_fn_p (fn) == 0)
5405 continue;
5407 else if (copy_fn_p (fn) <= 0)
5408 continue;
5410 if (!TYPE_NOTHROW_P (TREE_TYPE (fn)))
5411 return false;
5414 return true;
5417 /* Actually evaluates the trait. */
5419 static bool
5420 trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
5422 enum tree_code type_code1;
5423 tree t;
5425 type_code1 = TREE_CODE (type1);
5427 switch (kind)
5429 case CPTK_HAS_NOTHROW_ASSIGN:
5430 type1 = strip_array_types (type1);
5431 return (!CP_TYPE_CONST_P (type1) && type_code1 != REFERENCE_TYPE
5432 && (trait_expr_value (CPTK_HAS_TRIVIAL_ASSIGN, type1, type2)
5433 || (CLASS_TYPE_P (type1)
5434 && classtype_has_nothrow_assign_or_copy_p (type1,
5435 true))));
5437 case CPTK_HAS_TRIVIAL_ASSIGN:
5438 /* ??? The standard seems to be missing the "or array of such a class
5439 type" wording for this trait. */
5440 type1 = strip_array_types (type1);
5441 return (!CP_TYPE_CONST_P (type1) && type_code1 != REFERENCE_TYPE
5442 && (trivial_type_p (type1)
5443 || (CLASS_TYPE_P (type1)
5444 && TYPE_HAS_TRIVIAL_COPY_ASSIGN (type1))));
5446 case CPTK_HAS_NOTHROW_CONSTRUCTOR:
5447 type1 = strip_array_types (type1);
5448 return (trait_expr_value (CPTK_HAS_TRIVIAL_CONSTRUCTOR, type1, type2)
5449 || (CLASS_TYPE_P (type1)
5450 && (t = locate_ctor (type1))
5451 && TYPE_NOTHROW_P (TREE_TYPE (t))));
5453 case CPTK_HAS_TRIVIAL_CONSTRUCTOR:
5454 type1 = strip_array_types (type1);
5455 return (trivial_type_p (type1)
5456 || (CLASS_TYPE_P (type1) && TYPE_HAS_TRIVIAL_DFLT (type1)));
5458 case CPTK_HAS_NOTHROW_COPY:
5459 type1 = strip_array_types (type1);
5460 return (trait_expr_value (CPTK_HAS_TRIVIAL_COPY, type1, type2)
5461 || (CLASS_TYPE_P (type1)
5462 && classtype_has_nothrow_assign_or_copy_p (type1, false)));
5464 case CPTK_HAS_TRIVIAL_COPY:
5465 /* ??? The standard seems to be missing the "or array of such a class
5466 type" wording for this trait. */
5467 type1 = strip_array_types (type1);
5468 return (trivial_type_p (type1) || type_code1 == REFERENCE_TYPE
5469 || (CLASS_TYPE_P (type1) && TYPE_HAS_TRIVIAL_COPY_CTOR (type1)));
5471 case CPTK_HAS_TRIVIAL_DESTRUCTOR:
5472 type1 = strip_array_types (type1);
5473 return (trivial_type_p (type1) || type_code1 == REFERENCE_TYPE
5474 || (CLASS_TYPE_P (type1)
5475 && TYPE_HAS_TRIVIAL_DESTRUCTOR (type1)));
5477 case CPTK_HAS_VIRTUAL_DESTRUCTOR:
5478 return type_has_virtual_destructor (type1);
5480 case CPTK_IS_ABSTRACT:
5481 return (ABSTRACT_CLASS_TYPE_P (type1));
5483 case CPTK_IS_BASE_OF:
5484 return (NON_UNION_CLASS_TYPE_P (type1) && NON_UNION_CLASS_TYPE_P (type2)
5485 && DERIVED_FROM_P (type1, type2));
5487 case CPTK_IS_CLASS:
5488 return (NON_UNION_CLASS_TYPE_P (type1));
5490 case CPTK_IS_CONVERTIBLE_TO:
5491 /* TODO */
5492 return false;
5494 case CPTK_IS_EMPTY:
5495 return (NON_UNION_CLASS_TYPE_P (type1) && CLASSTYPE_EMPTY_P (type1));
5497 case CPTK_IS_ENUM:
5498 return (type_code1 == ENUMERAL_TYPE);
5500 case CPTK_IS_FINAL:
5501 return (CLASS_TYPE_P (type1) && CLASSTYPE_FINAL (type1));
5503 case CPTK_IS_LITERAL_TYPE:
5504 return (literal_type_p (type1));
5506 case CPTK_IS_POD:
5507 return (pod_type_p (type1));
5509 case CPTK_IS_POLYMORPHIC:
5510 return (CLASS_TYPE_P (type1) && TYPE_POLYMORPHIC_P (type1));
5512 case CPTK_IS_STD_LAYOUT:
5513 return (std_layout_type_p (type1));
5515 case CPTK_IS_TRIVIAL:
5516 return (trivial_type_p (type1));
5518 case CPTK_IS_UNION:
5519 return (type_code1 == UNION_TYPE);
5521 default:
5522 gcc_unreachable ();
5523 return false;
5527 /* If TYPE is an array of unknown bound, or (possibly cv-qualified)
5528 void, or a complete type, returns it, otherwise NULL_TREE. */
5530 static tree
5531 check_trait_type (tree type)
5533 if (TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type)
5534 && COMPLETE_TYPE_P (TREE_TYPE (type)))
5535 return type;
5537 if (VOID_TYPE_P (type))
5538 return type;
5540 return complete_type_or_else (strip_array_types (type), NULL_TREE);
5543 /* Process a trait expression. */
5545 tree
5546 finish_trait_expr (cp_trait_kind kind, tree type1, tree type2)
5548 gcc_assert (kind == CPTK_HAS_NOTHROW_ASSIGN
5549 || kind == CPTK_HAS_NOTHROW_CONSTRUCTOR
5550 || kind == CPTK_HAS_NOTHROW_COPY
5551 || kind == CPTK_HAS_TRIVIAL_ASSIGN
5552 || kind == CPTK_HAS_TRIVIAL_CONSTRUCTOR
5553 || kind == CPTK_HAS_TRIVIAL_COPY
5554 || kind == CPTK_HAS_TRIVIAL_DESTRUCTOR
5555 || kind == CPTK_HAS_VIRTUAL_DESTRUCTOR
5556 || kind == CPTK_IS_ABSTRACT
5557 || kind == CPTK_IS_BASE_OF
5558 || kind == CPTK_IS_CLASS
5559 || kind == CPTK_IS_CONVERTIBLE_TO
5560 || kind == CPTK_IS_EMPTY
5561 || kind == CPTK_IS_ENUM
5562 || kind == CPTK_IS_FINAL
5563 || kind == CPTK_IS_LITERAL_TYPE
5564 || kind == CPTK_IS_POD
5565 || kind == CPTK_IS_POLYMORPHIC
5566 || kind == CPTK_IS_STD_LAYOUT
5567 || kind == CPTK_IS_TRIVIAL
5568 || kind == CPTK_IS_UNION);
5570 if (kind == CPTK_IS_CONVERTIBLE_TO)
5572 sorry ("__is_convertible_to");
5573 return error_mark_node;
5576 if (type1 == error_mark_node
5577 || ((kind == CPTK_IS_BASE_OF || kind == CPTK_IS_CONVERTIBLE_TO)
5578 && type2 == error_mark_node))
5579 return error_mark_node;
5581 if (processing_template_decl)
5583 tree trait_expr = make_node (TRAIT_EXPR);
5584 TREE_TYPE (trait_expr) = boolean_type_node;
5585 TRAIT_EXPR_TYPE1 (trait_expr) = type1;
5586 TRAIT_EXPR_TYPE2 (trait_expr) = type2;
5587 TRAIT_EXPR_KIND (trait_expr) = kind;
5588 return trait_expr;
5591 switch (kind)
5593 case CPTK_HAS_NOTHROW_ASSIGN:
5594 case CPTK_HAS_TRIVIAL_ASSIGN:
5595 case CPTK_HAS_NOTHROW_CONSTRUCTOR:
5596 case CPTK_HAS_TRIVIAL_CONSTRUCTOR:
5597 case CPTK_HAS_NOTHROW_COPY:
5598 case CPTK_HAS_TRIVIAL_COPY:
5599 case CPTK_HAS_TRIVIAL_DESTRUCTOR:
5600 case CPTK_HAS_VIRTUAL_DESTRUCTOR:
5601 case CPTK_IS_ABSTRACT:
5602 case CPTK_IS_EMPTY:
5603 case CPTK_IS_FINAL:
5604 case CPTK_IS_LITERAL_TYPE:
5605 case CPTK_IS_POD:
5606 case CPTK_IS_POLYMORPHIC:
5607 case CPTK_IS_STD_LAYOUT:
5608 case CPTK_IS_TRIVIAL:
5609 if (!check_trait_type (type1))
5610 return error_mark_node;
5611 break;
5613 case CPTK_IS_BASE_OF:
5614 if (NON_UNION_CLASS_TYPE_P (type1) && NON_UNION_CLASS_TYPE_P (type2)
5615 && !same_type_ignoring_top_level_qualifiers_p (type1, type2)
5616 && !complete_type_or_else (type2, NULL_TREE))
5617 /* We already issued an error. */
5618 return error_mark_node;
5619 break;
5621 case CPTK_IS_CLASS:
5622 case CPTK_IS_ENUM:
5623 case CPTK_IS_UNION:
5624 break;
5626 case CPTK_IS_CONVERTIBLE_TO:
5627 default:
5628 gcc_unreachable ();
5631 return (trait_expr_value (kind, type1, type2)
5632 ? boolean_true_node : boolean_false_node);
5635 /* Do-nothing variants of functions to handle pragma FLOAT_CONST_DECIMAL64,
5636 which is ignored for C++. */
5638 void
5639 set_float_const_decimal64 (void)
5643 void
5644 clear_float_const_decimal64 (void)
5648 bool
5649 float_const_decimal64_p (void)
5651 return 0;
5655 /* Return true if T is a literal type. */
5657 bool
5658 literal_type_p (tree t)
5660 if (SCALAR_TYPE_P (t)
5661 || TREE_CODE (t) == VECTOR_TYPE
5662 || TREE_CODE (t) == REFERENCE_TYPE)
5663 return true;
5664 if (CLASS_TYPE_P (t))
5666 t = complete_type (t);
5667 gcc_assert (COMPLETE_TYPE_P (t) || errorcount);
5668 return CLASSTYPE_LITERAL_P (t);
5670 if (TREE_CODE (t) == ARRAY_TYPE)
5671 return literal_type_p (strip_array_types (t));
5672 return false;
5675 /* If DECL is a variable declared `constexpr', require its type
5676 be literal. Return the DECL if OK, otherwise NULL. */
5678 tree
5679 ensure_literal_type_for_constexpr_object (tree decl)
5681 tree type = TREE_TYPE (decl);
5682 if (TREE_CODE (decl) == VAR_DECL && DECL_DECLARED_CONSTEXPR_P (decl)
5683 && !processing_template_decl)
5685 if (CLASS_TYPE_P (type) && !COMPLETE_TYPE_P (complete_type (type)))
5686 /* Don't complain here, we'll complain about incompleteness
5687 when we try to initialize the variable. */;
5688 else if (!literal_type_p (type))
5690 error ("the type %qT of constexpr variable %qD is not literal",
5691 type, decl);
5692 explain_non_literal_class (type);
5693 return NULL;
5696 return decl;
5699 /* Representation of entries in the constexpr function definition table. */
5701 typedef struct GTY(()) constexpr_fundef {
5702 tree decl;
5703 tree body;
5704 } constexpr_fundef;
5706 /* This table holds all constexpr function definitions seen in
5707 the current translation unit. */
5709 static GTY ((param_is (constexpr_fundef))) htab_t constexpr_fundef_table;
5711 /* Utility function used for managing the constexpr function table.
5712 Return true if the entries pointed to by P and Q are for the
5713 same constexpr function. */
5715 static inline int
5716 constexpr_fundef_equal (const void *p, const void *q)
5718 const constexpr_fundef *lhs = (const constexpr_fundef *) p;
5719 const constexpr_fundef *rhs = (const constexpr_fundef *) q;
5720 return lhs->decl == rhs->decl;
5723 /* Utility function used for managing the constexpr function table.
5724 Return a hash value for the entry pointed to by Q. */
5726 static inline hashval_t
5727 constexpr_fundef_hash (const void *p)
5729 const constexpr_fundef *fundef = (const constexpr_fundef *) p;
5730 return DECL_UID (fundef->decl);
5733 /* Return a previously saved definition of function FUN. */
5735 static constexpr_fundef *
5736 retrieve_constexpr_fundef (tree fun)
5738 constexpr_fundef fundef = { NULL, NULL };
5739 if (constexpr_fundef_table == NULL)
5740 return NULL;
5742 fundef.decl = fun;
5743 return (constexpr_fundef *) htab_find (constexpr_fundef_table, &fundef);
5746 /* Check whether the parameter and return types of FUN are valid for a
5747 constexpr function, and complain if COMPLAIN. */
5749 static bool
5750 is_valid_constexpr_fn (tree fun, bool complain)
5752 tree parm = FUNCTION_FIRST_USER_PARM (fun);
5753 bool ret = true;
5754 for (; parm != NULL; parm = TREE_CHAIN (parm))
5755 if (!literal_type_p (TREE_TYPE (parm)))
5757 ret = false;
5758 if (complain)
5760 error ("invalid type for parameter %d of constexpr "
5761 "function %q+#D", DECL_PARM_INDEX (parm), fun);
5762 explain_non_literal_class (TREE_TYPE (parm));
5766 if (!DECL_CONSTRUCTOR_P (fun))
5768 tree rettype = TREE_TYPE (TREE_TYPE (fun));
5769 if (!literal_type_p (rettype))
5771 ret = false;
5772 if (complain)
5774 error ("invalid return type %qT of constexpr function %q+D",
5775 rettype, fun);
5776 explain_non_literal_class (rettype);
5780 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fun)
5781 && !CLASSTYPE_LITERAL_P (DECL_CONTEXT (fun)))
5783 ret = false;
5784 if (complain)
5786 error ("enclosing class of constexpr non-static member "
5787 "function %q+#D is not a literal type", fun);
5788 explain_non_literal_class (DECL_CONTEXT (fun));
5792 else if (CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fun)))
5794 ret = false;
5795 if (complain)
5796 error ("%q#T has virtual base classes", DECL_CONTEXT (fun));
5799 return ret;
5802 /* Subroutine of build_constexpr_constructor_member_initializers.
5803 The expression tree T represents a data member initialization
5804 in a (constexpr) constructor definition. Build a pairing of
5805 the data member with its initializer, and prepend that pair
5806 to the existing initialization pair INITS. */
5808 static bool
5809 build_data_member_initialization (tree t, VEC(constructor_elt,gc) **vec)
5811 tree member, init;
5812 if (TREE_CODE (t) == CLEANUP_POINT_EXPR)
5813 t = TREE_OPERAND (t, 0);
5814 if (TREE_CODE (t) == EXPR_STMT)
5815 t = TREE_OPERAND (t, 0);
5816 if (t == error_mark_node)
5817 return false;
5818 if (TREE_CODE (t) == STATEMENT_LIST)
5820 tree_stmt_iterator i;
5821 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
5823 if (! build_data_member_initialization (tsi_stmt (i), vec))
5824 return false;
5826 return true;
5828 if (TREE_CODE (t) == CLEANUP_STMT)
5830 /* We can't see a CLEANUP_STMT in a constructor for a literal class,
5831 but we can in a constexpr constructor for a non-literal class. Just
5832 ignore it; either all the initialization will be constant, in which
5833 case the cleanup can't run, or it can't be constexpr.
5834 Still recurse into CLEANUP_BODY. */
5835 return build_data_member_initialization (CLEANUP_BODY (t), vec);
5837 if (TREE_CODE (t) == CONVERT_EXPR)
5838 t = TREE_OPERAND (t, 0);
5839 if (TREE_CODE (t) == INIT_EXPR
5840 || TREE_CODE (t) == MODIFY_EXPR)
5842 member = TREE_OPERAND (t, 0);
5843 init = unshare_expr (TREE_OPERAND (t, 1));
5845 else
5847 gcc_assert (TREE_CODE (t) == CALL_EXPR);
5848 member = CALL_EXPR_ARG (t, 0);
5849 /* We don't use build_cplus_new here because it complains about
5850 abstract bases. Leaving the call unwrapped means that it has the
5851 wrong type, but cxx_eval_constant_expression doesn't care. */
5852 init = unshare_expr (t);
5854 if (TREE_CODE (member) == INDIRECT_REF)
5855 member = TREE_OPERAND (member, 0);
5856 if (TREE_CODE (member) == NOP_EXPR)
5858 tree op = member;
5859 STRIP_NOPS (op);
5860 if (TREE_CODE (op) == ADDR_EXPR)
5862 gcc_assert (same_type_ignoring_top_level_qualifiers_p
5863 (TREE_TYPE (TREE_TYPE (op)),
5864 TREE_TYPE (TREE_TYPE (member))));
5865 /* Initializing a cv-qualified member; we need to look through
5866 the const_cast. */
5867 member = op;
5869 else if (op == current_class_ptr
5870 && (same_type_ignoring_top_level_qualifiers_p
5871 (TREE_TYPE (TREE_TYPE (member)),
5872 current_class_type)))
5873 /* Delegating constructor. */
5874 member = op;
5875 else
5877 /* This is an initializer for an empty base; keep it for now so
5878 we can check it in cxx_eval_bare_aggregate. */
5879 gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (member))));
5882 if (TREE_CODE (member) == ADDR_EXPR)
5883 member = TREE_OPERAND (member, 0);
5884 if (TREE_CODE (member) == COMPONENT_REF
5885 /* If we're initializing a member of a subaggregate, it's a vtable
5886 pointer. Leave it as COMPONENT_REF so we remember the path to get
5887 to the vfield. */
5888 && TREE_CODE (TREE_OPERAND (member, 0)) != COMPONENT_REF)
5889 member = TREE_OPERAND (member, 1);
5890 CONSTRUCTOR_APPEND_ELT (*vec, member, init);
5891 return true;
5894 /* Make sure that there are no statements after LAST in the constructor
5895 body represented by LIST. */
5897 bool
5898 check_constexpr_ctor_body (tree last, tree list)
5900 bool ok = true;
5901 if (TREE_CODE (list) == STATEMENT_LIST)
5903 tree_stmt_iterator i = tsi_last (list);
5904 for (; !tsi_end_p (i); tsi_prev (&i))
5906 tree t = tsi_stmt (i);
5907 if (t == last)
5908 break;
5909 if (TREE_CODE (t) == BIND_EXPR)
5911 if (!check_constexpr_ctor_body (last, BIND_EXPR_BODY (t)))
5912 return false;
5913 else
5914 continue;
5916 /* We currently allow typedefs and static_assert.
5917 FIXME allow them in the standard, too. */
5918 if (TREE_CODE (t) != STATIC_ASSERT)
5920 ok = false;
5921 break;
5925 else if (list != last
5926 && TREE_CODE (list) != STATIC_ASSERT)
5927 ok = false;
5928 if (!ok)
5930 error ("constexpr constructor does not have empty body");
5931 DECL_DECLARED_CONSTEXPR_P (current_function_decl) = false;
5933 return ok;
5936 /* VEC is a vector of constructor elements built up for the base and member
5937 initializers of a constructor for TYPE. They need to be in increasing
5938 offset order, which they might not be yet if TYPE has a primary base
5939 which is not first in the base-clause. */
5941 static VEC(constructor_elt,gc) *
5942 sort_constexpr_mem_initializers (tree type, VEC(constructor_elt,gc) *vec)
5944 tree pri = CLASSTYPE_PRIMARY_BINFO (type);
5945 constructor_elt elt;
5946 int i;
5948 if (pri == NULL_TREE
5949 || pri == BINFO_BASE_BINFO (TYPE_BINFO (type), 0))
5950 return vec;
5952 /* Find the element for the primary base and move it to the beginning of
5953 the vec. */
5954 VEC(constructor_elt,gc) &v = *vec;
5955 pri = BINFO_TYPE (pri);
5956 for (i = 1; ; ++i)
5957 if (TREE_TYPE (v[i].index) == pri)
5958 break;
5960 elt = v[i];
5961 for (; i > 0; --i)
5962 v[i] = v[i-1];
5963 v[0] = elt;
5964 return vec;
5967 /* Build compile-time evalable representations of member-initializer list
5968 for a constexpr constructor. */
5970 static tree
5971 build_constexpr_constructor_member_initializers (tree type, tree body)
5973 VEC(constructor_elt,gc) *vec = NULL;
5974 bool ok = true;
5975 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR
5976 || TREE_CODE (body) == EH_SPEC_BLOCK)
5977 body = TREE_OPERAND (body, 0);
5978 if (TREE_CODE (body) == STATEMENT_LIST)
5979 body = STATEMENT_LIST_HEAD (body)->stmt;
5980 body = BIND_EXPR_BODY (body);
5981 if (TREE_CODE (body) == CLEANUP_POINT_EXPR)
5983 body = TREE_OPERAND (body, 0);
5984 if (TREE_CODE (body) == EXPR_STMT)
5985 body = TREE_OPERAND (body, 0);
5986 if (TREE_CODE (body) == INIT_EXPR
5987 && (same_type_ignoring_top_level_qualifiers_p
5988 (TREE_TYPE (TREE_OPERAND (body, 0)),
5989 current_class_type)))
5991 /* Trivial copy. */
5992 return TREE_OPERAND (body, 1);
5994 ok = build_data_member_initialization (body, &vec);
5996 else if (TREE_CODE (body) == STATEMENT_LIST)
5998 tree_stmt_iterator i;
5999 for (i = tsi_start (body); !tsi_end_p (i); tsi_next (&i))
6001 ok = build_data_member_initialization (tsi_stmt (i), &vec);
6002 if (!ok)
6003 break;
6006 else if (TREE_CODE (body) == TRY_BLOCK)
6008 error ("body of %<constexpr%> constructor cannot be "
6009 "a function-try-block");
6010 return error_mark_node;
6012 else if (EXPR_P (body))
6013 ok = build_data_member_initialization (body, &vec);
6014 else
6015 gcc_assert (errorcount > 0);
6016 if (ok)
6018 if (VEC_length (constructor_elt, vec) > 0)
6020 /* In a delegating constructor, return the target. */
6021 constructor_elt *ce = &VEC_index (constructor_elt, vec, 0);
6022 if (ce->index == current_class_ptr)
6024 body = ce->value;
6025 VEC_free (constructor_elt, gc, vec);
6026 return body;
6029 vec = sort_constexpr_mem_initializers (type, vec);
6030 return build_constructor (type, vec);
6032 else
6033 return error_mark_node;
6036 /* Subroutine of register_constexpr_fundef. BODY is the body of a function
6037 declared to be constexpr, or a sub-statement thereof. Returns the
6038 return value if suitable, error_mark_node for a statement not allowed in
6039 a constexpr function, or NULL_TREE if no return value was found. */
6041 static tree
6042 constexpr_fn_retval (tree body)
6044 switch (TREE_CODE (body))
6046 case STATEMENT_LIST:
6048 tree_stmt_iterator i;
6049 tree expr = NULL_TREE;
6050 for (i = tsi_start (body); !tsi_end_p (i); tsi_next (&i))
6052 tree s = constexpr_fn_retval (tsi_stmt (i));
6053 if (s == error_mark_node)
6054 return error_mark_node;
6055 else if (s == NULL_TREE)
6056 /* Keep iterating. */;
6057 else if (expr)
6058 /* Multiple return statements. */
6059 return error_mark_node;
6060 else
6061 expr = s;
6063 return expr;
6066 case RETURN_EXPR:
6067 return unshare_expr (TREE_OPERAND (body, 0));
6069 case DECL_EXPR:
6070 if (TREE_CODE (DECL_EXPR_DECL (body)) == USING_DECL)
6071 return NULL_TREE;
6072 return error_mark_node;
6074 case CLEANUP_POINT_EXPR:
6075 return constexpr_fn_retval (TREE_OPERAND (body, 0));
6077 case USING_STMT:
6078 return NULL_TREE;
6080 default:
6081 return error_mark_node;
6085 /* Subroutine of register_constexpr_fundef. BODY is the DECL_SAVED_TREE of
6086 FUN; do the necessary transformations to turn it into a single expression
6087 that we can store in the hash table. */
6089 static tree
6090 massage_constexpr_body (tree fun, tree body)
6092 if (DECL_CONSTRUCTOR_P (fun))
6093 body = build_constexpr_constructor_member_initializers
6094 (DECL_CONTEXT (fun), body);
6095 else
6097 if (TREE_CODE (body) == EH_SPEC_BLOCK)
6098 body = EH_SPEC_STMTS (body);
6099 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
6100 body = TREE_OPERAND (body, 0);
6101 if (TREE_CODE (body) == BIND_EXPR)
6102 body = BIND_EXPR_BODY (body);
6103 body = constexpr_fn_retval (body);
6105 return body;
6108 /* FUN is a constexpr constructor with massaged body BODY. Return true
6109 if some bases/fields are uninitialized, and complain if COMPLAIN. */
6111 static bool
6112 cx_check_missing_mem_inits (tree fun, tree body, bool complain)
6114 bool bad;
6115 tree field;
6116 unsigned i, nelts;
6117 tree ctype;
6119 if (TREE_CODE (body) != CONSTRUCTOR)
6120 return false;
6122 nelts = CONSTRUCTOR_NELTS (body);
6123 ctype = DECL_CONTEXT (fun);
6124 field = TYPE_FIELDS (ctype);
6126 if (TREE_CODE (ctype) == UNION_TYPE)
6128 if (nelts == 0 && next_initializable_field (field))
6130 if (complain)
6131 error ("%<constexpr%> constructor for union %qT must "
6132 "initialize exactly one non-static data member", ctype);
6133 return true;
6135 return false;
6138 bad = false;
6139 for (i = 0; i <= nelts; ++i)
6141 tree index;
6142 tree anon_union_init_type = NULL_TREE;
6143 if (i == nelts)
6144 index = NULL_TREE;
6145 else
6147 index = CONSTRUCTOR_ELT (body, i)->index;
6148 /* Handle anonymous union members. */
6149 if (TREE_CODE (index) == COMPONENT_REF
6150 && ANON_UNION_TYPE_P (TREE_TYPE (TREE_OPERAND (index, 0))))
6151 anon_union_init_type = TREE_TYPE (TREE_OPERAND (index, 0));
6152 /* Skip base and vtable inits. */
6153 else if (TREE_CODE (index) != FIELD_DECL
6154 || DECL_ARTIFICIAL (index))
6155 continue;
6157 for (; field != index && TREE_TYPE (field) != anon_union_init_type;
6158 field = DECL_CHAIN (field))
6160 tree ftype;
6161 if (TREE_CODE (field) != FIELD_DECL
6162 || (DECL_C_BIT_FIELD (field) && !DECL_NAME (field))
6163 || DECL_ARTIFICIAL (field))
6164 continue;
6165 ftype = strip_array_types (TREE_TYPE (field));
6166 if (type_has_constexpr_default_constructor (ftype))
6168 /* It's OK to skip a member with a trivial constexpr ctor.
6169 A constexpr ctor that isn't trivial should have been
6170 added in by now. */
6171 gcc_checking_assert (!TYPE_HAS_COMPLEX_DFLT (ftype)
6172 || errorcount != 0);
6173 continue;
6175 if (!complain)
6176 return true;
6177 error ("uninitialized member %qD in %<constexpr%> constructor",
6178 field);
6179 bad = true;
6181 if (field == NULL_TREE)
6182 break;
6183 field = DECL_CHAIN (field);
6186 return bad;
6189 /* We are processing the definition of the constexpr function FUN.
6190 Check that its BODY fulfills the propriate requirements and
6191 enter it in the constexpr function definition table.
6192 For constructor BODY is actually the TREE_LIST of the
6193 member-initializer list. */
6195 tree
6196 register_constexpr_fundef (tree fun, tree body)
6198 constexpr_fundef entry;
6199 constexpr_fundef **slot;
6201 if (!is_valid_constexpr_fn (fun, !DECL_GENERATED_P (fun)))
6202 return NULL;
6204 body = massage_constexpr_body (fun, body);
6205 if (body == NULL_TREE || body == error_mark_node)
6207 if (!DECL_CONSTRUCTOR_P (fun))
6208 error ("body of constexpr function %qD not a return-statement", fun);
6209 return NULL;
6212 if (!potential_rvalue_constant_expression (body))
6214 if (!DECL_GENERATED_P (fun))
6215 require_potential_rvalue_constant_expression (body);
6216 return NULL;
6219 if (DECL_CONSTRUCTOR_P (fun)
6220 && cx_check_missing_mem_inits (fun, body, !DECL_GENERATED_P (fun)))
6221 return NULL;
6223 /* Create the constexpr function table if necessary. */
6224 if (constexpr_fundef_table == NULL)
6225 constexpr_fundef_table = htab_create_ggc (101,
6226 constexpr_fundef_hash,
6227 constexpr_fundef_equal,
6228 ggc_free);
6229 entry.decl = fun;
6230 entry.body = body;
6231 slot = (constexpr_fundef **)
6232 htab_find_slot (constexpr_fundef_table, &entry, INSERT);
6234 gcc_assert (*slot == NULL);
6235 *slot = ggc_alloc_constexpr_fundef ();
6236 **slot = entry;
6238 return fun;
6241 /* FUN is a non-constexpr function called in a context that requires a
6242 constant expression. If it comes from a constexpr template, explain why
6243 the instantiation isn't constexpr. */
6245 void
6246 explain_invalid_constexpr_fn (tree fun)
6248 static struct pointer_set_t *diagnosed;
6249 tree body;
6250 location_t save_loc;
6251 /* Only diagnose defaulted functions or instantiations. */
6252 if (!DECL_DEFAULTED_FN (fun)
6253 && !is_instantiation_of_constexpr (fun))
6254 return;
6255 if (diagnosed == NULL)
6256 diagnosed = pointer_set_create ();
6257 if (pointer_set_insert (diagnosed, fun) != 0)
6258 /* Already explained. */
6259 return;
6261 save_loc = input_location;
6262 input_location = DECL_SOURCE_LOCATION (fun);
6263 inform (0, "%q+D is not usable as a constexpr function because:", fun);
6264 /* First check the declaration. */
6265 if (is_valid_constexpr_fn (fun, true))
6267 /* Then if it's OK, the body. */
6268 if (DECL_DEFAULTED_FN (fun))
6269 explain_implicit_non_constexpr (fun);
6270 else
6272 body = massage_constexpr_body (fun, DECL_SAVED_TREE (fun));
6273 require_potential_rvalue_constant_expression (body);
6274 if (DECL_CONSTRUCTOR_P (fun))
6275 cx_check_missing_mem_inits (fun, body, true);
6278 input_location = save_loc;
6281 /* Objects of this type represent calls to constexpr functions
6282 along with the bindings of parameters to their arguments, for
6283 the purpose of compile time evaluation. */
6285 typedef struct GTY(()) constexpr_call {
6286 /* Description of the constexpr function definition. */
6287 constexpr_fundef *fundef;
6288 /* Parameter bindings environment. A TREE_LIST where each TREE_PURPOSE
6289 is a parameter _DECL and the TREE_VALUE is the value of the parameter.
6290 Note: This arrangement is made to accomodate the use of
6291 iterative_hash_template_arg (see pt.c). If you change this
6292 representation, also change the hash calculation in
6293 cxx_eval_call_expression. */
6294 tree bindings;
6295 /* Result of the call.
6296 NULL means the call is being evaluated.
6297 error_mark_node means that the evaluation was erroneous;
6298 otherwise, the actuall value of the call. */
6299 tree result;
6300 /* The hash of this call; we remember it here to avoid having to
6301 recalculate it when expanding the hash table. */
6302 hashval_t hash;
6303 } constexpr_call;
6305 /* A table of all constexpr calls that have been evaluated by the
6306 compiler in this translation unit. */
6308 static GTY ((param_is (constexpr_call))) htab_t constexpr_call_table;
6310 static tree cxx_eval_constant_expression (const constexpr_call *, tree,
6311 bool, bool, bool *);
6312 static tree cxx_eval_vec_perm_expr (const constexpr_call *, tree, bool, bool,
6313 bool *);
6316 /* Compute a hash value for a constexpr call representation. */
6318 static hashval_t
6319 constexpr_call_hash (const void *p)
6321 const constexpr_call *info = (const constexpr_call *) p;
6322 return info->hash;
6325 /* Return 1 if the objects pointed to by P and Q represent calls
6326 to the same constexpr function with the same arguments.
6327 Otherwise, return 0. */
6329 static int
6330 constexpr_call_equal (const void *p, const void *q)
6332 const constexpr_call *lhs = (const constexpr_call *) p;
6333 const constexpr_call *rhs = (const constexpr_call *) q;
6334 tree lhs_bindings;
6335 tree rhs_bindings;
6336 if (lhs == rhs)
6337 return 1;
6338 if (!constexpr_fundef_equal (lhs->fundef, rhs->fundef))
6339 return 0;
6340 lhs_bindings = lhs->bindings;
6341 rhs_bindings = rhs->bindings;
6342 while (lhs_bindings != NULL && rhs_bindings != NULL)
6344 tree lhs_arg = TREE_VALUE (lhs_bindings);
6345 tree rhs_arg = TREE_VALUE (rhs_bindings);
6346 gcc_assert (TREE_TYPE (lhs_arg) == TREE_TYPE (rhs_arg));
6347 if (!cp_tree_equal (lhs_arg, rhs_arg))
6348 return 0;
6349 lhs_bindings = TREE_CHAIN (lhs_bindings);
6350 rhs_bindings = TREE_CHAIN (rhs_bindings);
6352 return lhs_bindings == rhs_bindings;
6355 /* Initialize the constexpr call table, if needed. */
6357 static void
6358 maybe_initialize_constexpr_call_table (void)
6360 if (constexpr_call_table == NULL)
6361 constexpr_call_table = htab_create_ggc (101,
6362 constexpr_call_hash,
6363 constexpr_call_equal,
6364 ggc_free);
6367 /* Return true if T designates the implied `this' parameter. */
6369 static inline bool
6370 is_this_parameter (tree t)
6372 return t == current_class_ptr;
6375 /* We have an expression tree T that represents a call, either CALL_EXPR
6376 or AGGR_INIT_EXPR. If the call is lexically to a named function,
6377 retrun the _DECL for that function. */
6379 static tree
6380 get_function_named_in_call (tree t)
6382 tree fun = NULL;
6383 switch (TREE_CODE (t))
6385 case CALL_EXPR:
6386 fun = CALL_EXPR_FN (t);
6387 break;
6389 case AGGR_INIT_EXPR:
6390 fun = AGGR_INIT_EXPR_FN (t);
6391 break;
6393 default:
6394 gcc_unreachable();
6395 break;
6397 if (TREE_CODE (fun) == ADDR_EXPR
6398 && TREE_CODE (TREE_OPERAND (fun, 0)) == FUNCTION_DECL)
6399 fun = TREE_OPERAND (fun, 0);
6400 return fun;
6403 /* We have an expression tree T that represents a call, either CALL_EXPR
6404 or AGGR_INIT_EXPR. Return the Nth argument. */
6406 static inline tree
6407 get_nth_callarg (tree t, int n)
6409 switch (TREE_CODE (t))
6411 case CALL_EXPR:
6412 return CALL_EXPR_ARG (t, n);
6414 case AGGR_INIT_EXPR:
6415 return AGGR_INIT_EXPR_ARG (t, n);
6417 default:
6418 gcc_unreachable ();
6419 return NULL;
6423 /* Look up the binding of the function parameter T in a constexpr
6424 function call context CALL. */
6426 static tree
6427 lookup_parameter_binding (const constexpr_call *call, tree t)
6429 tree b = purpose_member (t, call->bindings);
6430 return TREE_VALUE (b);
6433 /* Attempt to evaluate T which represents a call to a builtin function.
6434 We assume here that all builtin functions evaluate to scalar types
6435 represented by _CST nodes. */
6437 static tree
6438 cxx_eval_builtin_function_call (const constexpr_call *call, tree t,
6439 bool allow_non_constant, bool addr,
6440 bool *non_constant_p)
6442 const int nargs = call_expr_nargs (t);
6443 tree *args = (tree *) alloca (nargs * sizeof (tree));
6444 tree new_call;
6445 int i;
6446 for (i = 0; i < nargs; ++i)
6448 args[i] = cxx_eval_constant_expression (call, CALL_EXPR_ARG (t, i),
6449 allow_non_constant, addr,
6450 non_constant_p);
6451 if (allow_non_constant && *non_constant_p)
6452 return t;
6454 if (*non_constant_p)
6455 return t;
6456 new_call = build_call_array_loc (EXPR_LOCATION (t), TREE_TYPE (t),
6457 CALL_EXPR_FN (t), nargs, args);
6458 return fold (new_call);
6461 /* TEMP is the constant value of a temporary object of type TYPE. Adjust
6462 the type of the value to match. */
6464 static tree
6465 adjust_temp_type (tree type, tree temp)
6467 if (TREE_TYPE (temp) == type)
6468 return temp;
6469 /* Avoid wrapping an aggregate value in a NOP_EXPR. */
6470 if (TREE_CODE (temp) == CONSTRUCTOR)
6471 return build_constructor (type, CONSTRUCTOR_ELTS (temp));
6472 gcc_assert (SCALAR_TYPE_P (type));
6473 return cp_fold_convert (type, temp);
6476 /* Subroutine of cxx_eval_call_expression.
6477 We are processing a call expression (either CALL_EXPR or
6478 AGGR_INIT_EXPR) in the call context of OLD_CALL. Evaluate
6479 all arguments and bind their values to correspondings
6480 parameters, making up the NEW_CALL context. */
6482 static void
6483 cxx_bind_parameters_in_call (const constexpr_call *old_call, tree t,
6484 constexpr_call *new_call,
6485 bool allow_non_constant,
6486 bool *non_constant_p)
6488 const int nargs = call_expr_nargs (t);
6489 tree fun = new_call->fundef->decl;
6490 tree parms = DECL_ARGUMENTS (fun);
6491 int i;
6492 for (i = 0; i < nargs; ++i)
6494 tree x, arg;
6495 tree type = parms ? TREE_TYPE (parms) : void_type_node;
6496 /* For member function, the first argument is a pointer to the implied
6497 object. And for an object contruction, don't bind `this' before
6498 it is fully constructed. */
6499 if (i == 0 && DECL_CONSTRUCTOR_P (fun))
6500 goto next;
6501 x = get_nth_callarg (t, i);
6502 arg = cxx_eval_constant_expression (old_call, x, allow_non_constant,
6503 TREE_CODE (type) == REFERENCE_TYPE,
6504 non_constant_p);
6505 /* Don't VERIFY_CONSTANT here. */
6506 if (*non_constant_p && allow_non_constant)
6507 return;
6508 /* Just discard ellipsis args after checking their constantitude. */
6509 if (!parms)
6510 continue;
6511 if (*non_constant_p)
6512 /* Don't try to adjust the type of non-constant args. */
6513 goto next;
6515 /* Make sure the binding has the same type as the parm. */
6516 if (TREE_CODE (type) != REFERENCE_TYPE)
6517 arg = adjust_temp_type (type, arg);
6518 new_call->bindings = tree_cons (parms, arg, new_call->bindings);
6519 next:
6520 parms = TREE_CHAIN (parms);
6524 /* Variables and functions to manage constexpr call expansion context.
6525 These do not need to be marked for PCH or GC. */
6527 /* FIXME remember and print actual constant arguments. */
6528 static VEC(tree,heap) *call_stack = NULL;
6529 static int call_stack_tick;
6530 static int last_cx_error_tick;
6532 static bool
6533 push_cx_call_context (tree call)
6535 ++call_stack_tick;
6536 if (!EXPR_HAS_LOCATION (call))
6537 SET_EXPR_LOCATION (call, input_location);
6538 VEC_safe_push (tree, heap, call_stack, call);
6539 if (VEC_length (tree, call_stack) > (unsigned) max_constexpr_depth)
6540 return false;
6541 return true;
6544 static void
6545 pop_cx_call_context (void)
6547 ++call_stack_tick;
6548 VEC_pop (tree, call_stack);
6551 VEC(tree,heap) *
6552 cx_error_context (void)
6554 VEC(tree,heap) *r = NULL;
6555 if (call_stack_tick != last_cx_error_tick
6556 && !VEC_empty (tree, call_stack))
6557 r = call_stack;
6558 last_cx_error_tick = call_stack_tick;
6559 return r;
6562 /* Subroutine of cxx_eval_constant_expression.
6563 Evaluate the call expression tree T in the context of OLD_CALL expression
6564 evaluation. */
6566 static tree
6567 cxx_eval_call_expression (const constexpr_call *old_call, tree t,
6568 bool allow_non_constant, bool addr,
6569 bool *non_constant_p)
6571 location_t loc = EXPR_LOC_OR_HERE (t);
6572 tree fun = get_function_named_in_call (t);
6573 tree result;
6574 constexpr_call new_call = { NULL, NULL, NULL, 0 };
6575 constexpr_call **slot;
6576 constexpr_call *entry;
6577 bool depth_ok;
6579 if (TREE_CODE (fun) != FUNCTION_DECL)
6581 /* Might be a constexpr function pointer. */
6582 fun = cxx_eval_constant_expression (old_call, fun, allow_non_constant,
6583 /*addr*/false, non_constant_p);
6584 if (TREE_CODE (fun) == ADDR_EXPR)
6585 fun = TREE_OPERAND (fun, 0);
6587 if (TREE_CODE (fun) != FUNCTION_DECL)
6589 if (!allow_non_constant && !*non_constant_p)
6590 error_at (loc, "expression %qE does not designate a constexpr "
6591 "function", fun);
6592 *non_constant_p = true;
6593 return t;
6595 if (DECL_CLONED_FUNCTION_P (fun))
6596 fun = DECL_CLONED_FUNCTION (fun);
6597 if (is_builtin_fn (fun))
6598 return cxx_eval_builtin_function_call (old_call, t, allow_non_constant,
6599 addr, non_constant_p);
6600 if (!DECL_DECLARED_CONSTEXPR_P (fun))
6602 if (!allow_non_constant)
6604 error_at (loc, "call to non-constexpr function %qD", fun);
6605 explain_invalid_constexpr_fn (fun);
6607 *non_constant_p = true;
6608 return t;
6611 /* Shortcut trivial copy constructor/op=. */
6612 if (call_expr_nargs (t) == 2 && trivial_fn_p (fun))
6614 tree arg = convert_from_reference (get_nth_callarg (t, 1));
6615 return cxx_eval_constant_expression (old_call, arg, allow_non_constant,
6616 addr, non_constant_p);
6619 /* If in direct recursive call, optimize definition search. */
6620 if (old_call != NULL && old_call->fundef->decl == fun)
6621 new_call.fundef = old_call->fundef;
6622 else
6624 new_call.fundef = retrieve_constexpr_fundef (fun);
6625 if (new_call.fundef == NULL || new_call.fundef->body == NULL)
6627 if (!allow_non_constant)
6629 if (DECL_INITIAL (fun))
6631 /* The definition of fun was somehow unsuitable. */
6632 error_at (loc, "%qD called in a constant expression", fun);
6633 explain_invalid_constexpr_fn (fun);
6635 else
6636 error_at (loc, "%qD used before its definition", fun);
6638 *non_constant_p = true;
6639 return t;
6642 cxx_bind_parameters_in_call (old_call, t, &new_call,
6643 allow_non_constant, non_constant_p);
6644 if (*non_constant_p)
6645 return t;
6647 depth_ok = push_cx_call_context (t);
6649 new_call.hash
6650 = iterative_hash_template_arg (new_call.bindings,
6651 constexpr_fundef_hash (new_call.fundef));
6653 /* If we have seen this call before, we are done. */
6654 maybe_initialize_constexpr_call_table ();
6655 slot = (constexpr_call **)
6656 htab_find_slot (constexpr_call_table, &new_call, INSERT);
6657 entry = *slot;
6658 if (entry == NULL)
6660 /* We need to keep a pointer to the entry, not just the slot, as the
6661 slot can move in the call to cxx_eval_builtin_function_call. */
6662 *slot = entry = ggc_alloc_constexpr_call ();
6663 *entry = new_call;
6665 /* Calls which are in progress have their result set to NULL
6666 so that we can detect circular dependencies. */
6667 else if (entry->result == NULL)
6669 if (!allow_non_constant)
6670 error ("call has circular dependency");
6671 *non_constant_p = true;
6672 entry->result = result = error_mark_node;
6675 if (!depth_ok)
6677 if (!allow_non_constant)
6678 error ("constexpr evaluation depth exceeds maximum of %d (use "
6679 "-fconstexpr-depth= to increase the maximum)",
6680 max_constexpr_depth);
6681 *non_constant_p = true;
6682 entry->result = result = error_mark_node;
6684 else
6686 result = entry->result;
6687 if (!result || result == error_mark_node)
6688 result = (cxx_eval_constant_expression
6689 (&new_call, new_call.fundef->body,
6690 allow_non_constant, addr,
6691 non_constant_p));
6692 if (result == error_mark_node)
6693 *non_constant_p = true;
6694 if (*non_constant_p)
6695 entry->result = result = error_mark_node;
6696 else
6698 /* If this was a call to initialize an object, set the type of
6699 the CONSTRUCTOR to the type of that object. */
6700 if (DECL_CONSTRUCTOR_P (fun))
6702 tree ob_arg = get_nth_callarg (t, 0);
6703 STRIP_NOPS (ob_arg);
6704 gcc_assert (TREE_CODE (TREE_TYPE (ob_arg)) == POINTER_TYPE
6705 && CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (ob_arg))));
6706 result = adjust_temp_type (TREE_TYPE (TREE_TYPE (ob_arg)),
6707 result);
6709 entry->result = result;
6713 pop_cx_call_context ();
6714 return unshare_expr (result);
6717 /* FIXME speed this up, it's taking 16% of compile time on sieve testcase. */
6719 bool
6720 reduced_constant_expression_p (tree t)
6722 if (TREE_OVERFLOW_P (t))
6723 /* Integer overflow makes this not a constant expression. */
6724 return false;
6725 /* FIXME are we calling this too much? */
6726 return initializer_constant_valid_p (t, TREE_TYPE (t)) != NULL_TREE;
6729 /* Some expressions may have constant operands but are not constant
6730 themselves, such as 1/0. Call this function (or rather, the macro
6731 following it) to check for that condition.
6733 We only call this in places that require an arithmetic constant, not in
6734 places where we might have a non-constant expression that can be a
6735 component of a constant expression, such as the address of a constexpr
6736 variable that might be dereferenced later. */
6738 static bool
6739 verify_constant (tree t, bool allow_non_constant, bool *non_constant_p)
6741 if (!*non_constant_p && !reduced_constant_expression_p (t))
6743 if (!allow_non_constant)
6745 /* If T was already folded to a _CST with TREE_OVERFLOW set,
6746 printing the folded constant isn't helpful. */
6747 if (TREE_OVERFLOW_P (t))
6749 permerror (input_location, "overflow in constant expression");
6750 /* If we're being permissive (and are in an enforcing
6751 context), consider this constant. */
6752 if (flag_permissive)
6753 return false;
6755 else
6756 error ("%q+E is not a constant expression", t);
6758 *non_constant_p = true;
6760 return *non_constant_p;
6762 #define VERIFY_CONSTANT(X) \
6763 do { \
6764 if (verify_constant ((X), allow_non_constant, non_constant_p)) \
6765 return t; \
6766 } while (0)
6768 /* Subroutine of cxx_eval_constant_expression.
6769 Attempt to reduce the unary expression tree T to a compile time value.
6770 If successful, return the value. Otherwise issue a diagnostic
6771 and return error_mark_node. */
6773 static tree
6774 cxx_eval_unary_expression (const constexpr_call *call, tree t,
6775 bool allow_non_constant, bool addr,
6776 bool *non_constant_p)
6778 tree r;
6779 tree orig_arg = TREE_OPERAND (t, 0);
6780 tree arg = cxx_eval_constant_expression (call, orig_arg, allow_non_constant,
6781 addr, non_constant_p);
6782 VERIFY_CONSTANT (arg);
6783 if (arg == orig_arg)
6784 return t;
6785 r = fold_build1 (TREE_CODE (t), TREE_TYPE (t), arg);
6786 VERIFY_CONSTANT (r);
6787 return r;
6790 /* Subroutine of cxx_eval_constant_expression.
6791 Like cxx_eval_unary_expression, except for binary expressions. */
6793 static tree
6794 cxx_eval_binary_expression (const constexpr_call *call, tree t,
6795 bool allow_non_constant, bool addr,
6796 bool *non_constant_p)
6798 tree r;
6799 tree orig_lhs = TREE_OPERAND (t, 0);
6800 tree orig_rhs = TREE_OPERAND (t, 1);
6801 tree lhs, rhs;
6802 lhs = cxx_eval_constant_expression (call, orig_lhs,
6803 allow_non_constant, addr,
6804 non_constant_p);
6805 VERIFY_CONSTANT (lhs);
6806 rhs = cxx_eval_constant_expression (call, orig_rhs,
6807 allow_non_constant, addr,
6808 non_constant_p);
6809 VERIFY_CONSTANT (rhs);
6810 if (lhs == orig_lhs && rhs == orig_rhs)
6811 return t;
6812 r = fold_build2 (TREE_CODE (t), TREE_TYPE (t), lhs, rhs);
6813 VERIFY_CONSTANT (r);
6814 return r;
6817 /* Subroutine of cxx_eval_constant_expression.
6818 Attempt to evaluate condition expressions. Dead branches are not
6819 looked into. */
6821 static tree
6822 cxx_eval_conditional_expression (const constexpr_call *call, tree t,
6823 bool allow_non_constant, bool addr,
6824 bool *non_constant_p)
6826 tree val = cxx_eval_constant_expression (call, TREE_OPERAND (t, 0),
6827 allow_non_constant, addr,
6828 non_constant_p);
6829 VERIFY_CONSTANT (val);
6830 /* Don't VERIFY_CONSTANT the other operands. */
6831 if (integer_zerop (val))
6832 return cxx_eval_constant_expression (call, TREE_OPERAND (t, 2),
6833 allow_non_constant, addr,
6834 non_constant_p);
6835 return cxx_eval_constant_expression (call, TREE_OPERAND (t, 1),
6836 allow_non_constant, addr,
6837 non_constant_p);
6840 /* Subroutine of cxx_eval_constant_expression.
6841 Attempt to reduce a reference to an array slot. */
6843 static tree
6844 cxx_eval_array_reference (const constexpr_call *call, tree t,
6845 bool allow_non_constant, bool addr,
6846 bool *non_constant_p)
6848 tree oldary = TREE_OPERAND (t, 0);
6849 tree ary = cxx_eval_constant_expression (call, oldary,
6850 allow_non_constant, addr,
6851 non_constant_p);
6852 tree index, oldidx;
6853 HOST_WIDE_INT i;
6854 tree elem_type;
6855 unsigned len, elem_nchars = 1;
6856 if (*non_constant_p)
6857 return t;
6858 oldidx = TREE_OPERAND (t, 1);
6859 index = cxx_eval_constant_expression (call, oldidx,
6860 allow_non_constant, false,
6861 non_constant_p);
6862 VERIFY_CONSTANT (index);
6863 if (addr && ary == oldary && index == oldidx)
6864 return t;
6865 else if (addr)
6866 return build4 (ARRAY_REF, TREE_TYPE (t), ary, index, NULL, NULL);
6867 elem_type = TREE_TYPE (TREE_TYPE (ary));
6868 if (TREE_CODE (ary) == CONSTRUCTOR)
6869 len = CONSTRUCTOR_NELTS (ary);
6870 else if (TREE_CODE (ary) == STRING_CST)
6872 elem_nchars = (TYPE_PRECISION (elem_type)
6873 / TYPE_PRECISION (char_type_node));
6874 len = (unsigned) TREE_STRING_LENGTH (ary) / elem_nchars;
6876 else
6878 /* We can't do anything with other tree codes, so use
6879 VERIFY_CONSTANT to complain and fail. */
6880 VERIFY_CONSTANT (ary);
6881 gcc_unreachable ();
6883 if (compare_tree_int (index, len) >= 0)
6885 if (tree_int_cst_lt (index, array_type_nelts_top (TREE_TYPE (ary))))
6887 /* If it's within the array bounds but doesn't have an explicit
6888 initializer, it's value-initialized. */
6889 tree val = build_value_init (elem_type, tf_warning_or_error);
6890 return cxx_eval_constant_expression (call, val,
6891 allow_non_constant, addr,
6892 non_constant_p);
6895 if (!allow_non_constant)
6896 error ("array subscript out of bound");
6897 *non_constant_p = true;
6898 return t;
6900 i = tree_low_cst (index, 0);
6901 if (TREE_CODE (ary) == CONSTRUCTOR)
6902 return VEC_index (constructor_elt, CONSTRUCTOR_ELTS (ary), i).value;
6903 else if (elem_nchars == 1)
6904 return build_int_cst (cv_unqualified (TREE_TYPE (TREE_TYPE (ary))),
6905 TREE_STRING_POINTER (ary)[i]);
6906 else
6908 tree type = cv_unqualified (TREE_TYPE (TREE_TYPE (ary)));
6909 return native_interpret_expr (type, (const unsigned char *)
6910 TREE_STRING_POINTER (ary)
6911 + i * elem_nchars, elem_nchars);
6913 /* Don't VERIFY_CONSTANT here. */
6916 /* Subroutine of cxx_eval_constant_expression.
6917 Attempt to reduce a field access of a value of class type. */
6919 static tree
6920 cxx_eval_component_reference (const constexpr_call *call, tree t,
6921 bool allow_non_constant, bool addr,
6922 bool *non_constant_p)
6924 unsigned HOST_WIDE_INT i;
6925 tree field;
6926 tree value;
6927 tree part = TREE_OPERAND (t, 1);
6928 tree orig_whole = TREE_OPERAND (t, 0);
6929 tree whole = cxx_eval_constant_expression (call, orig_whole,
6930 allow_non_constant, addr,
6931 non_constant_p);
6932 if (whole == orig_whole)
6933 return t;
6934 if (addr)
6935 return fold_build3 (COMPONENT_REF, TREE_TYPE (t),
6936 whole, part, NULL_TREE);
6937 /* Don't VERIFY_CONSTANT here; we only want to check that we got a
6938 CONSTRUCTOR. */
6939 if (!*non_constant_p && TREE_CODE (whole) != CONSTRUCTOR)
6941 if (!allow_non_constant)
6942 error ("%qE is not a constant expression", orig_whole);
6943 *non_constant_p = true;
6945 if (DECL_MUTABLE_P (part))
6947 if (!allow_non_constant)
6948 error ("mutable %qD is not usable in a constant expression", part);
6949 *non_constant_p = true;
6951 if (*non_constant_p)
6952 return t;
6953 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole), i, field, value)
6955 if (field == part)
6956 return value;
6958 if (TREE_CODE (TREE_TYPE (whole)) == UNION_TYPE
6959 && CONSTRUCTOR_NELTS (whole) > 0)
6961 /* DR 1188 says we don't have to deal with this. */
6962 if (!allow_non_constant)
6963 error ("accessing %qD member instead of initialized %qD member in "
6964 "constant expression", part, CONSTRUCTOR_ELT (whole, 0)->index);
6965 *non_constant_p = true;
6966 return t;
6969 /* If there's no explicit init for this field, it's value-initialized. */
6970 value = build_value_init (TREE_TYPE (t), tf_warning_or_error);
6971 return cxx_eval_constant_expression (call, value,
6972 allow_non_constant, addr,
6973 non_constant_p);
6976 /* Subroutine of cxx_eval_constant_expression.
6977 Attempt to reduce a field access of a value of class type that is
6978 expressed as a BIT_FIELD_REF. */
6980 static tree
6981 cxx_eval_bit_field_ref (const constexpr_call *call, tree t,
6982 bool allow_non_constant, bool addr,
6983 bool *non_constant_p)
6985 tree orig_whole = TREE_OPERAND (t, 0);
6986 tree retval, fldval, utype, mask;
6987 bool fld_seen = false;
6988 HOST_WIDE_INT istart, isize;
6989 tree whole = cxx_eval_constant_expression (call, orig_whole,
6990 allow_non_constant, addr,
6991 non_constant_p);
6992 tree start, field, value;
6993 unsigned HOST_WIDE_INT i;
6995 if (whole == orig_whole)
6996 return t;
6997 /* Don't VERIFY_CONSTANT here; we only want to check that we got a
6998 CONSTRUCTOR. */
6999 if (!*non_constant_p && TREE_CODE (whole) != CONSTRUCTOR)
7001 if (!allow_non_constant)
7002 error ("%qE is not a constant expression", orig_whole);
7003 *non_constant_p = true;
7005 if (*non_constant_p)
7006 return t;
7008 start = TREE_OPERAND (t, 2);
7009 istart = tree_low_cst (start, 0);
7010 isize = tree_low_cst (TREE_OPERAND (t, 1), 0);
7011 utype = TREE_TYPE (t);
7012 if (!TYPE_UNSIGNED (utype))
7013 utype = build_nonstandard_integer_type (TYPE_PRECISION (utype), 1);
7014 retval = build_int_cst (utype, 0);
7015 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole), i, field, value)
7017 tree bitpos = bit_position (field);
7018 if (bitpos == start && DECL_SIZE (field) == TREE_OPERAND (t, 1))
7019 return value;
7020 if (TREE_CODE (TREE_TYPE (field)) == INTEGER_TYPE
7021 && TREE_CODE (value) == INTEGER_CST
7022 && host_integerp (bitpos, 0)
7023 && host_integerp (DECL_SIZE (field), 0))
7025 HOST_WIDE_INT bit = tree_low_cst (bitpos, 0);
7026 HOST_WIDE_INT sz = tree_low_cst (DECL_SIZE (field), 0);
7027 HOST_WIDE_INT shift;
7028 if (bit >= istart && bit + sz <= istart + isize)
7030 fldval = fold_convert (utype, value);
7031 mask = build_int_cst_type (utype, -1);
7032 mask = fold_build2 (LSHIFT_EXPR, utype, mask,
7033 size_int (TYPE_PRECISION (utype) - sz));
7034 mask = fold_build2 (RSHIFT_EXPR, utype, mask,
7035 size_int (TYPE_PRECISION (utype) - sz));
7036 fldval = fold_build2 (BIT_AND_EXPR, utype, fldval, mask);
7037 shift = bit - istart;
7038 if (BYTES_BIG_ENDIAN)
7039 shift = TYPE_PRECISION (utype) - shift - sz;
7040 fldval = fold_build2 (LSHIFT_EXPR, utype, fldval,
7041 size_int (shift));
7042 retval = fold_build2 (BIT_IOR_EXPR, utype, retval, fldval);
7043 fld_seen = true;
7047 if (fld_seen)
7048 return fold_convert (TREE_TYPE (t), retval);
7049 gcc_unreachable ();
7050 return error_mark_node;
7053 /* Subroutine of cxx_eval_constant_expression.
7054 Evaluate a short-circuited logical expression T in the context
7055 of a given constexpr CALL. BAILOUT_VALUE is the value for
7056 early return. CONTINUE_VALUE is used here purely for
7057 sanity check purposes. */
7059 static tree
7060 cxx_eval_logical_expression (const constexpr_call *call, tree t,
7061 tree bailout_value, tree continue_value,
7062 bool allow_non_constant, bool addr,
7063 bool *non_constant_p)
7065 tree r;
7066 tree lhs = cxx_eval_constant_expression (call, TREE_OPERAND (t, 0),
7067 allow_non_constant, addr,
7068 non_constant_p);
7069 VERIFY_CONSTANT (lhs);
7070 if (tree_int_cst_equal (lhs, bailout_value))
7071 return lhs;
7072 gcc_assert (tree_int_cst_equal (lhs, continue_value));
7073 r = cxx_eval_constant_expression (call, TREE_OPERAND (t, 1),
7074 allow_non_constant, addr, non_constant_p);
7075 VERIFY_CONSTANT (r);
7076 return r;
7079 /* REF is a COMPONENT_REF designating a particular field. V is a vector of
7080 CONSTRUCTOR elements to initialize (part of) an object containing that
7081 field. Return a pointer to the constructor_elt corresponding to the
7082 initialization of the field. */
7084 static constructor_elt *
7085 base_field_constructor_elt (VEC(constructor_elt,gc) *v, tree ref)
7087 tree aggr = TREE_OPERAND (ref, 0);
7088 tree field = TREE_OPERAND (ref, 1);
7089 HOST_WIDE_INT i;
7090 constructor_elt *ce;
7092 gcc_assert (TREE_CODE (ref) == COMPONENT_REF);
7094 if (TREE_CODE (aggr) == COMPONENT_REF)
7096 constructor_elt *base_ce
7097 = base_field_constructor_elt (v, aggr);
7098 v = CONSTRUCTOR_ELTS (base_ce->value);
7101 for (i = 0; VEC_iterate (constructor_elt, v, i, ce); ++i)
7102 if (ce->index == field)
7103 return ce;
7105 gcc_unreachable ();
7106 return NULL;
7109 /* Subroutine of cxx_eval_constant_expression.
7110 The expression tree T denotes a C-style array or a C-style
7111 aggregate. Reduce it to a constant expression. */
7113 static tree
7114 cxx_eval_bare_aggregate (const constexpr_call *call, tree t,
7115 bool allow_non_constant, bool addr,
7116 bool *non_constant_p)
7118 VEC(constructor_elt,gc) *v = CONSTRUCTOR_ELTS (t);
7119 VEC(constructor_elt,gc) *n = VEC_alloc (constructor_elt, gc,
7120 VEC_length (constructor_elt, v));
7121 constructor_elt *ce;
7122 HOST_WIDE_INT i;
7123 bool changed = false;
7124 gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (t));
7125 for (i = 0; VEC_iterate (constructor_elt, v, i, ce); ++i)
7127 tree elt = cxx_eval_constant_expression (call, ce->value,
7128 allow_non_constant, addr,
7129 non_constant_p);
7130 /* Don't VERIFY_CONSTANT here. */
7131 if (allow_non_constant && *non_constant_p)
7132 goto fail;
7133 if (elt != ce->value)
7134 changed = true;
7135 if (TREE_CODE (ce->index) == COMPONENT_REF)
7137 /* This is an initialization of a vfield inside a base
7138 subaggregate that we already initialized; push this
7139 initialization into the previous initialization. */
7140 constructor_elt *inner = base_field_constructor_elt (n, ce->index);
7141 inner->value = elt;
7143 else if (TREE_CODE (ce->index) == NOP_EXPR)
7145 /* This is an initializer for an empty base; now that we've
7146 checked that it's constant, we can ignore it. */
7147 gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (ce->index))));
7149 else
7150 CONSTRUCTOR_APPEND_ELT (n, ce->index, elt);
7152 if (*non_constant_p || !changed)
7154 fail:
7155 VEC_free (constructor_elt, gc, n);
7156 return t;
7158 t = build_constructor (TREE_TYPE (t), n);
7159 TREE_CONSTANT (t) = true;
7160 return t;
7163 /* Subroutine of cxx_eval_constant_expression.
7164 The expression tree T is a VEC_INIT_EXPR which denotes the desired
7165 initialization of a non-static data member of array type. Reduce it to a
7166 CONSTRUCTOR.
7168 Note that apart from value-initialization (when VALUE_INIT is true),
7169 this is only intended to support value-initialization and the
7170 initializations done by defaulted constructors for classes with
7171 non-static data members of array type. In this case, VEC_INIT_EXPR_INIT
7172 will either be NULL_TREE for the default constructor, or a COMPONENT_REF
7173 for the copy/move constructor. */
7175 static tree
7176 cxx_eval_vec_init_1 (const constexpr_call *call, tree atype, tree init,
7177 bool value_init, bool allow_non_constant, bool addr,
7178 bool *non_constant_p)
7180 tree elttype = TREE_TYPE (atype);
7181 int max = tree_low_cst (array_type_nelts (atype), 0);
7182 VEC(constructor_elt,gc) *n = VEC_alloc (constructor_elt, gc, max + 1);
7183 bool pre_init = false;
7184 int i;
7186 /* For the default constructor, build up a call to the default
7187 constructor of the element type. We only need to handle class types
7188 here, as for a constructor to be constexpr, all members must be
7189 initialized, which for a defaulted default constructor means they must
7190 be of a class type with a constexpr default constructor. */
7191 if (TREE_CODE (elttype) == ARRAY_TYPE)
7192 /* We only do this at the lowest level. */;
7193 else if (value_init)
7195 init = build_value_init (elttype, tf_warning_or_error);
7196 init = cxx_eval_constant_expression
7197 (call, init, allow_non_constant, addr, non_constant_p);
7198 pre_init = true;
7200 else if (!init)
7202 VEC(tree,gc) *argvec = make_tree_vector ();
7203 init = build_special_member_call (NULL_TREE, complete_ctor_identifier,
7204 &argvec, elttype, LOOKUP_NORMAL,
7205 tf_warning_or_error);
7206 release_tree_vector (argvec);
7207 init = cxx_eval_constant_expression (call, init, allow_non_constant,
7208 addr, non_constant_p);
7209 pre_init = true;
7212 if (*non_constant_p && !allow_non_constant)
7213 goto fail;
7215 for (i = 0; i <= max; ++i)
7217 tree idx = build_int_cst (size_type_node, i);
7218 tree eltinit;
7219 if (TREE_CODE (elttype) == ARRAY_TYPE)
7221 /* A multidimensional array; recurse. */
7222 if (value_init || init == NULL_TREE)
7223 eltinit = NULL_TREE;
7224 else
7225 eltinit = cp_build_array_ref (input_location, init, idx,
7226 tf_warning_or_error);
7227 eltinit = cxx_eval_vec_init_1 (call, elttype, eltinit, value_init,
7228 allow_non_constant, addr,
7229 non_constant_p);
7231 else if (pre_init)
7233 /* Initializing an element using value or default initialization
7234 we just pre-built above. */
7235 if (i == 0)
7236 eltinit = init;
7237 else
7238 eltinit = unshare_expr (init);
7240 else
7242 /* Copying an element. */
7243 VEC(tree,gc) *argvec;
7244 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7245 (atype, TREE_TYPE (init)));
7246 eltinit = cp_build_array_ref (input_location, init, idx,
7247 tf_warning_or_error);
7248 if (!real_lvalue_p (init))
7249 eltinit = move (eltinit);
7250 argvec = make_tree_vector ();
7251 VEC_quick_push (tree, argvec, eltinit);
7252 eltinit = (build_special_member_call
7253 (NULL_TREE, complete_ctor_identifier, &argvec,
7254 elttype, LOOKUP_NORMAL, tf_warning_or_error));
7255 release_tree_vector (argvec);
7256 eltinit = cxx_eval_constant_expression
7257 (call, eltinit, allow_non_constant, addr, non_constant_p);
7259 if (*non_constant_p && !allow_non_constant)
7260 goto fail;
7261 CONSTRUCTOR_APPEND_ELT (n, idx, eltinit);
7264 if (!*non_constant_p)
7266 init = build_constructor (atype, n);
7267 TREE_CONSTANT (init) = true;
7268 return init;
7271 fail:
7272 VEC_free (constructor_elt, gc, n);
7273 return init;
7276 static tree
7277 cxx_eval_vec_init (const constexpr_call *call, tree t,
7278 bool allow_non_constant, bool addr,
7279 bool *non_constant_p)
7281 tree atype = TREE_TYPE (t);
7282 tree init = VEC_INIT_EXPR_INIT (t);
7283 tree r = cxx_eval_vec_init_1 (call, atype, init,
7284 VEC_INIT_EXPR_VALUE_INIT (t),
7285 allow_non_constant, addr, non_constant_p);
7286 if (*non_constant_p)
7287 return t;
7288 else
7289 return r;
7292 /* A less strict version of fold_indirect_ref_1, which requires cv-quals to
7293 match. We want to be less strict for simple *& folding; if we have a
7294 non-const temporary that we access through a const pointer, that should
7295 work. We handle this here rather than change fold_indirect_ref_1
7296 because we're dealing with things like ADDR_EXPR of INTEGER_CST which
7297 don't really make sense outside of constant expression evaluation. Also
7298 we want to allow folding to COMPONENT_REF, which could cause trouble
7299 with TBAA in fold_indirect_ref_1.
7301 Try to keep this function synced with fold_indirect_ref_1. */
7303 static tree
7304 cxx_fold_indirect_ref (location_t loc, tree type, tree op0, bool *empty_base)
7306 tree sub, subtype;
7308 sub = op0;
7309 STRIP_NOPS (sub);
7310 subtype = TREE_TYPE (sub);
7311 if (!POINTER_TYPE_P (subtype))
7312 return NULL_TREE;
7314 if (TREE_CODE (sub) == ADDR_EXPR)
7316 tree op = TREE_OPERAND (sub, 0);
7317 tree optype = TREE_TYPE (op);
7319 /* *&CONST_DECL -> to the value of the const decl. */
7320 if (TREE_CODE (op) == CONST_DECL)
7321 return DECL_INITIAL (op);
7322 /* *&p => p; make sure to handle *&"str"[cst] here. */
7323 if (same_type_ignoring_top_level_qualifiers_p (optype, type))
7325 tree fop = fold_read_from_constant_string (op);
7326 if (fop)
7327 return fop;
7328 else
7329 return op;
7331 /* *(foo *)&fooarray => fooarray[0] */
7332 else if (TREE_CODE (optype) == ARRAY_TYPE
7333 && (same_type_ignoring_top_level_qualifiers_p
7334 (type, TREE_TYPE (optype))))
7336 tree type_domain = TYPE_DOMAIN (optype);
7337 tree min_val = size_zero_node;
7338 if (type_domain && TYPE_MIN_VALUE (type_domain))
7339 min_val = TYPE_MIN_VALUE (type_domain);
7340 return build4_loc (loc, ARRAY_REF, type, op, min_val,
7341 NULL_TREE, NULL_TREE);
7343 /* *(foo *)&complexfoo => __real__ complexfoo */
7344 else if (TREE_CODE (optype) == COMPLEX_TYPE
7345 && (same_type_ignoring_top_level_qualifiers_p
7346 (type, TREE_TYPE (optype))))
7347 return fold_build1_loc (loc, REALPART_EXPR, type, op);
7348 /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
7349 else if (TREE_CODE (optype) == VECTOR_TYPE
7350 && (same_type_ignoring_top_level_qualifiers_p
7351 (type, TREE_TYPE (optype))))
7353 tree part_width = TYPE_SIZE (type);
7354 tree index = bitsize_int (0);
7355 return fold_build3_loc (loc, BIT_FIELD_REF, type, op, part_width, index);
7357 /* Also handle conversion to an empty base class, which
7358 is represented with a NOP_EXPR. */
7359 else if (is_empty_class (type)
7360 && CLASS_TYPE_P (optype)
7361 && DERIVED_FROM_P (type, optype))
7363 *empty_base = true;
7364 return op;
7366 /* *(foo *)&struct_with_foo_field => COMPONENT_REF */
7367 else if (RECORD_OR_UNION_TYPE_P (optype))
7369 tree field = TYPE_FIELDS (optype);
7370 for (; field; field = DECL_CHAIN (field))
7371 if (TREE_CODE (field) == FIELD_DECL
7372 && integer_zerop (byte_position (field))
7373 && (same_type_ignoring_top_level_qualifiers_p
7374 (TREE_TYPE (field), type)))
7376 return fold_build3 (COMPONENT_REF, type, op, field, NULL_TREE);
7377 break;
7381 else if (TREE_CODE (sub) == POINTER_PLUS_EXPR
7382 && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST)
7384 tree op00 = TREE_OPERAND (sub, 0);
7385 tree op01 = TREE_OPERAND (sub, 1);
7387 STRIP_NOPS (op00);
7388 if (TREE_CODE (op00) == ADDR_EXPR)
7390 tree op00type;
7391 op00 = TREE_OPERAND (op00, 0);
7392 op00type = TREE_TYPE (op00);
7394 /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
7395 if (TREE_CODE (op00type) == VECTOR_TYPE
7396 && (same_type_ignoring_top_level_qualifiers_p
7397 (type, TREE_TYPE (op00type))))
7399 HOST_WIDE_INT offset = tree_low_cst (op01, 0);
7400 tree part_width = TYPE_SIZE (type);
7401 unsigned HOST_WIDE_INT part_widthi = tree_low_cst (part_width, 0)/BITS_PER_UNIT;
7402 unsigned HOST_WIDE_INT indexi = offset * BITS_PER_UNIT;
7403 tree index = bitsize_int (indexi);
7405 if (offset/part_widthi <= TYPE_VECTOR_SUBPARTS (op00type))
7406 return fold_build3_loc (loc,
7407 BIT_FIELD_REF, type, op00,
7408 part_width, index);
7411 /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
7412 else if (TREE_CODE (op00type) == COMPLEX_TYPE
7413 && (same_type_ignoring_top_level_qualifiers_p
7414 (type, TREE_TYPE (op00type))))
7416 tree size = TYPE_SIZE_UNIT (type);
7417 if (tree_int_cst_equal (size, op01))
7418 return fold_build1_loc (loc, IMAGPART_EXPR, type, op00);
7420 /* ((foo *)&fooarray)[1] => fooarray[1] */
7421 else if (TREE_CODE (op00type) == ARRAY_TYPE
7422 && (same_type_ignoring_top_level_qualifiers_p
7423 (type, TREE_TYPE (op00type))))
7425 tree type_domain = TYPE_DOMAIN (op00type);
7426 tree min_val = size_zero_node;
7427 if (type_domain && TYPE_MIN_VALUE (type_domain))
7428 min_val = TYPE_MIN_VALUE (type_domain);
7429 op01 = size_binop_loc (loc, EXACT_DIV_EXPR, op01,
7430 TYPE_SIZE_UNIT (type));
7431 op01 = size_binop_loc (loc, PLUS_EXPR, op01, min_val);
7432 return build4_loc (loc, ARRAY_REF, type, op00, op01,
7433 NULL_TREE, NULL_TREE);
7435 /* ((foo *)&struct_with_foo_field)[1] => COMPONENT_REF */
7436 else if (RECORD_OR_UNION_TYPE_P (op00type))
7438 tree field = TYPE_FIELDS (op00type);
7439 for (; field; field = DECL_CHAIN (field))
7440 if (TREE_CODE (field) == FIELD_DECL
7441 && tree_int_cst_equal (byte_position (field), op01)
7442 && (same_type_ignoring_top_level_qualifiers_p
7443 (TREE_TYPE (field), type)))
7445 return fold_build3 (COMPONENT_REF, type, op00,
7446 field, NULL_TREE);
7447 break;
7452 /* *(foo *)fooarrptreturn> (*fooarrptr)[0] */
7453 else if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
7454 && (same_type_ignoring_top_level_qualifiers_p
7455 (type, TREE_TYPE (TREE_TYPE (subtype)))))
7457 tree type_domain;
7458 tree min_val = size_zero_node;
7459 sub = cxx_fold_indirect_ref (loc, TREE_TYPE (subtype), sub, NULL);
7460 if (!sub)
7461 sub = build1_loc (loc, INDIRECT_REF, TREE_TYPE (subtype), sub);
7462 type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
7463 if (type_domain && TYPE_MIN_VALUE (type_domain))
7464 min_val = TYPE_MIN_VALUE (type_domain);
7465 return build4_loc (loc, ARRAY_REF, type, sub, min_val, NULL_TREE,
7466 NULL_TREE);
7469 return NULL_TREE;
7472 static tree
7473 cxx_eval_indirect_ref (const constexpr_call *call, tree t,
7474 bool allow_non_constant, bool addr,
7475 bool *non_constant_p)
7477 tree orig_op0 = TREE_OPERAND (t, 0);
7478 tree op0 = cxx_eval_constant_expression (call, orig_op0, allow_non_constant,
7479 /*addr*/false, non_constant_p);
7480 bool empty_base = false;
7481 tree r;
7483 /* Don't VERIFY_CONSTANT here. */
7484 if (*non_constant_p)
7485 return t;
7487 r = cxx_fold_indirect_ref (EXPR_LOCATION (t), TREE_TYPE (t), op0,
7488 &empty_base);
7490 if (r)
7491 r = cxx_eval_constant_expression (call, r, allow_non_constant,
7492 addr, non_constant_p);
7493 else
7495 tree sub = op0;
7496 STRIP_NOPS (sub);
7497 if (TREE_CODE (sub) == POINTER_PLUS_EXPR)
7499 sub = TREE_OPERAND (sub, 0);
7500 STRIP_NOPS (sub);
7502 if (TREE_CODE (sub) == ADDR_EXPR)
7504 /* We couldn't fold to a constant value. Make sure it's not
7505 something we should have been able to fold. */
7506 gcc_assert (!same_type_ignoring_top_level_qualifiers_p
7507 (TREE_TYPE (TREE_TYPE (sub)), TREE_TYPE (t)));
7508 /* DR 1188 says we don't have to deal with this. */
7509 if (!allow_non_constant)
7510 error ("accessing value of %qE through a %qT glvalue in a "
7511 "constant expression", build_fold_indirect_ref (sub),
7512 TREE_TYPE (t));
7513 *non_constant_p = true;
7514 return t;
7518 /* If we're pulling out the value of an empty base, make sure
7519 that the whole object is constant and then return an empty
7520 CONSTRUCTOR. */
7521 if (empty_base)
7523 VERIFY_CONSTANT (r);
7524 r = build_constructor (TREE_TYPE (t), NULL);
7525 TREE_CONSTANT (r) = true;
7528 if (r == NULL_TREE)
7530 if (!addr)
7531 VERIFY_CONSTANT (t);
7532 return t;
7534 return r;
7537 /* Complain about R, a VAR_DECL, not being usable in a constant expression.
7538 Shared between potential_constant_expression and
7539 cxx_eval_constant_expression. */
7541 static void
7542 non_const_var_error (tree r)
7544 tree type = TREE_TYPE (r);
7545 error ("the value of %qD is not usable in a constant "
7546 "expression", r);
7547 /* Avoid error cascade. */
7548 if (DECL_INITIAL (r) == error_mark_node)
7549 return;
7550 if (DECL_DECLARED_CONSTEXPR_P (r))
7551 inform (DECL_SOURCE_LOCATION (r),
7552 "%qD used in its own initializer", r);
7553 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
7555 if (!CP_TYPE_CONST_P (type))
7556 inform (DECL_SOURCE_LOCATION (r),
7557 "%q#D is not const", r);
7558 else if (CP_TYPE_VOLATILE_P (type))
7559 inform (DECL_SOURCE_LOCATION (r),
7560 "%q#D is volatile", r);
7561 else if (!DECL_INITIAL (r)
7562 || !TREE_CONSTANT (DECL_INITIAL (r)))
7563 inform (DECL_SOURCE_LOCATION (r),
7564 "%qD was not initialized with a constant "
7565 "expression", r);
7566 else
7567 gcc_unreachable ();
7569 else
7571 if (cxx_dialect >= cxx0x && !DECL_DECLARED_CONSTEXPR_P (r))
7572 inform (DECL_SOURCE_LOCATION (r),
7573 "%qD was not declared %<constexpr%>", r);
7574 else
7575 inform (DECL_SOURCE_LOCATION (r),
7576 "%qD does not have integral or enumeration type",
7581 /* Evaluate VEC_PERM_EXPR (v1, v2, mask). */
7582 static tree
7583 cxx_eval_vec_perm_expr (const constexpr_call *call, tree t,
7584 bool allow_non_constant, bool addr,
7585 bool * non_constant_p)
7587 int i;
7588 tree args[3];
7589 tree val;
7590 tree elttype = TREE_TYPE (t);
7592 for (i = 0; i < 3; i++)
7594 args[i] = cxx_eval_constant_expression (call, TREE_OPERAND (t, i),
7595 allow_non_constant, addr,
7596 non_constant_p);
7597 if (*non_constant_p)
7598 goto fail;
7601 gcc_assert (TREE_CODE (TREE_TYPE (args[0])) == VECTOR_TYPE);
7602 gcc_assert (TREE_CODE (TREE_TYPE (args[1])) == VECTOR_TYPE);
7603 gcc_assert (TREE_CODE (TREE_TYPE (args[2])) == VECTOR_TYPE);
7605 val = fold_ternary_loc (EXPR_LOCATION (t), VEC_PERM_EXPR, elttype,
7606 args[0], args[1], args[2]);
7607 if (val != NULL_TREE)
7608 return val;
7610 fail:
7611 return t;
7614 /* Attempt to reduce the expression T to a constant value.
7615 On failure, issue diagnostic and return error_mark_node. */
7616 /* FIXME unify with c_fully_fold */
7618 static tree
7619 cxx_eval_constant_expression (const constexpr_call *call, tree t,
7620 bool allow_non_constant, bool addr,
7621 bool *non_constant_p)
7623 tree r = t;
7625 if (t == error_mark_node)
7627 *non_constant_p = true;
7628 return t;
7630 if (CONSTANT_CLASS_P (t))
7632 if (TREE_CODE (t) == PTRMEM_CST)
7633 t = cplus_expand_constant (t);
7634 return t;
7636 if (TREE_CODE (t) != NOP_EXPR
7637 && reduced_constant_expression_p (t))
7638 return fold (t);
7640 switch (TREE_CODE (t))
7642 case VAR_DECL:
7643 if (addr)
7644 return t;
7645 /* else fall through. */
7646 case CONST_DECL:
7647 r = integral_constant_value (t);
7648 if (TREE_CODE (r) == TARGET_EXPR
7649 && TREE_CODE (TARGET_EXPR_INITIAL (r)) == CONSTRUCTOR)
7650 r = TARGET_EXPR_INITIAL (r);
7651 if (DECL_P (r))
7653 if (!allow_non_constant)
7654 non_const_var_error (r);
7655 *non_constant_p = true;
7657 break;
7659 case FUNCTION_DECL:
7660 case TEMPLATE_DECL:
7661 case LABEL_DECL:
7662 return t;
7664 case PARM_DECL:
7665 if (call && DECL_CONTEXT (t) == call->fundef->decl)
7667 if (DECL_ARTIFICIAL (t) && DECL_CONSTRUCTOR_P (DECL_CONTEXT (t)))
7669 if (!allow_non_constant)
7670 sorry ("use of the value of the object being constructed "
7671 "in a constant expression");
7672 *non_constant_p = true;
7674 else
7675 r = lookup_parameter_binding (call, t);
7677 else if (addr)
7678 /* Defer in case this is only used for its type. */;
7679 else
7681 if (!allow_non_constant)
7682 error ("%qE is not a constant expression", t);
7683 *non_constant_p = true;
7685 break;
7687 case CALL_EXPR:
7688 case AGGR_INIT_EXPR:
7689 r = cxx_eval_call_expression (call, t, allow_non_constant, addr,
7690 non_constant_p);
7691 break;
7693 case TARGET_EXPR:
7694 if (!literal_type_p (TREE_TYPE (t)))
7696 if (!allow_non_constant)
7698 error ("temporary of non-literal type %qT in a "
7699 "constant expression", TREE_TYPE (t));
7700 explain_non_literal_class (TREE_TYPE (t));
7702 *non_constant_p = true;
7703 break;
7705 /* else fall through. */
7706 case INIT_EXPR:
7707 /* Pass false for 'addr' because these codes indicate
7708 initialization of a temporary. */
7709 r = cxx_eval_constant_expression (call, TREE_OPERAND (t, 1),
7710 allow_non_constant, false,
7711 non_constant_p);
7712 if (!*non_constant_p)
7713 /* Adjust the type of the result to the type of the temporary. */
7714 r = adjust_temp_type (TREE_TYPE (t), r);
7715 break;
7717 case SCOPE_REF:
7718 r = cxx_eval_constant_expression (call, TREE_OPERAND (t, 1),
7719 allow_non_constant, addr,
7720 non_constant_p);
7721 break;
7723 case RETURN_EXPR:
7724 case NON_LVALUE_EXPR:
7725 case TRY_CATCH_EXPR:
7726 case CLEANUP_POINT_EXPR:
7727 case MUST_NOT_THROW_EXPR:
7728 case SAVE_EXPR:
7729 r = cxx_eval_constant_expression (call, TREE_OPERAND (t, 0),
7730 allow_non_constant, addr,
7731 non_constant_p);
7732 break;
7734 /* These differ from cxx_eval_unary_expression in that this doesn't
7735 check for a constant operand or result; an address can be
7736 constant without its operand being, and vice versa. */
7737 case INDIRECT_REF:
7738 r = cxx_eval_indirect_ref (call, t, allow_non_constant, addr,
7739 non_constant_p);
7740 break;
7742 case ADDR_EXPR:
7744 tree oldop = TREE_OPERAND (t, 0);
7745 tree op = cxx_eval_constant_expression (call, oldop,
7746 allow_non_constant,
7747 /*addr*/true,
7748 non_constant_p);
7749 /* Don't VERIFY_CONSTANT here. */
7750 if (*non_constant_p)
7751 return t;
7752 /* This function does more aggressive folding than fold itself. */
7753 r = build_fold_addr_expr_with_type (op, TREE_TYPE (t));
7754 if (TREE_CODE (r) == ADDR_EXPR && TREE_OPERAND (r, 0) == oldop)
7755 return t;
7756 break;
7759 case REALPART_EXPR:
7760 case IMAGPART_EXPR:
7761 case CONJ_EXPR:
7762 case FIX_TRUNC_EXPR:
7763 case FLOAT_EXPR:
7764 case NEGATE_EXPR:
7765 case ABS_EXPR:
7766 case BIT_NOT_EXPR:
7767 case TRUTH_NOT_EXPR:
7768 case FIXED_CONVERT_EXPR:
7769 r = cxx_eval_unary_expression (call, t, allow_non_constant, addr,
7770 non_constant_p);
7771 break;
7773 case SIZEOF_EXPR:
7774 if (SIZEOF_EXPR_TYPE_P (t))
7775 r = cxx_sizeof_or_alignof_type (TREE_TYPE (TREE_OPERAND (t, 0)),
7776 SIZEOF_EXPR, false);
7777 else if (TYPE_P (TREE_OPERAND (t, 0)))
7778 r = cxx_sizeof_or_alignof_type (TREE_OPERAND (t, 0), SIZEOF_EXPR,
7779 false);
7780 else
7781 r = cxx_sizeof_or_alignof_expr (TREE_OPERAND (t, 0), SIZEOF_EXPR,
7782 false);
7783 if (r == error_mark_node)
7784 r = size_one_node;
7785 VERIFY_CONSTANT (r);
7786 break;
7788 case COMPOUND_EXPR:
7790 /* check_return_expr sometimes wraps a TARGET_EXPR in a
7791 COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR
7792 introduced by build_call_a. */
7793 tree op0 = TREE_OPERAND (t, 0);
7794 tree op1 = TREE_OPERAND (t, 1);
7795 STRIP_NOPS (op1);
7796 if ((TREE_CODE (op0) == TARGET_EXPR && op1 == TARGET_EXPR_SLOT (op0))
7797 || TREE_CODE (op1) == EMPTY_CLASS_EXPR)
7798 r = cxx_eval_constant_expression (call, op0, allow_non_constant,
7799 addr, non_constant_p);
7800 else
7802 /* Check that the LHS is constant and then discard it. */
7803 cxx_eval_constant_expression (call, op0, allow_non_constant,
7804 false, non_constant_p);
7805 op1 = TREE_OPERAND (t, 1);
7806 r = cxx_eval_constant_expression (call, op1, allow_non_constant,
7807 addr, non_constant_p);
7810 break;
7812 case POINTER_PLUS_EXPR:
7813 case PLUS_EXPR:
7814 case MINUS_EXPR:
7815 case MULT_EXPR:
7816 case TRUNC_DIV_EXPR:
7817 case CEIL_DIV_EXPR:
7818 case FLOOR_DIV_EXPR:
7819 case ROUND_DIV_EXPR:
7820 case TRUNC_MOD_EXPR:
7821 case CEIL_MOD_EXPR:
7822 case ROUND_MOD_EXPR:
7823 case RDIV_EXPR:
7824 case EXACT_DIV_EXPR:
7825 case MIN_EXPR:
7826 case MAX_EXPR:
7827 case LSHIFT_EXPR:
7828 case RSHIFT_EXPR:
7829 case LROTATE_EXPR:
7830 case RROTATE_EXPR:
7831 case BIT_IOR_EXPR:
7832 case BIT_XOR_EXPR:
7833 case BIT_AND_EXPR:
7834 case TRUTH_XOR_EXPR:
7835 case LT_EXPR:
7836 case LE_EXPR:
7837 case GT_EXPR:
7838 case GE_EXPR:
7839 case EQ_EXPR:
7840 case NE_EXPR:
7841 case UNORDERED_EXPR:
7842 case ORDERED_EXPR:
7843 case UNLT_EXPR:
7844 case UNLE_EXPR:
7845 case UNGT_EXPR:
7846 case UNGE_EXPR:
7847 case UNEQ_EXPR:
7848 case RANGE_EXPR:
7849 case COMPLEX_EXPR:
7850 r = cxx_eval_binary_expression (call, t, allow_non_constant, addr,
7851 non_constant_p);
7852 break;
7854 /* fold can introduce non-IF versions of these; still treat them as
7855 short-circuiting. */
7856 case TRUTH_AND_EXPR:
7857 case TRUTH_ANDIF_EXPR:
7858 r = cxx_eval_logical_expression (call, t, boolean_false_node,
7859 boolean_true_node,
7860 allow_non_constant, addr,
7861 non_constant_p);
7862 break;
7864 case TRUTH_OR_EXPR:
7865 case TRUTH_ORIF_EXPR:
7866 r = cxx_eval_logical_expression (call, t, boolean_true_node,
7867 boolean_false_node,
7868 allow_non_constant, addr,
7869 non_constant_p);
7870 break;
7872 case ARRAY_REF:
7873 r = cxx_eval_array_reference (call, t, allow_non_constant, addr,
7874 non_constant_p);
7875 break;
7877 case COMPONENT_REF:
7878 r = cxx_eval_component_reference (call, t, allow_non_constant, addr,
7879 non_constant_p);
7880 break;
7882 case BIT_FIELD_REF:
7883 r = cxx_eval_bit_field_ref (call, t, allow_non_constant, addr,
7884 non_constant_p);
7885 break;
7887 case COND_EXPR:
7888 case VEC_COND_EXPR:
7889 r = cxx_eval_conditional_expression (call, t, allow_non_constant, addr,
7890 non_constant_p);
7891 break;
7893 case CONSTRUCTOR:
7894 r = cxx_eval_bare_aggregate (call, t, allow_non_constant, addr,
7895 non_constant_p);
7896 break;
7898 case VEC_INIT_EXPR:
7899 /* We can get this in a defaulted constructor for a class with a
7900 non-static data member of array type. Either the initializer will
7901 be NULL, meaning default-initialization, or it will be an lvalue
7902 or xvalue of the same type, meaning direct-initialization from the
7903 corresponding member. */
7904 r = cxx_eval_vec_init (call, t, allow_non_constant, addr,
7905 non_constant_p);
7906 break;
7908 case VEC_PERM_EXPR:
7909 r = cxx_eval_vec_perm_expr (call, t, allow_non_constant, addr,
7910 non_constant_p);
7911 break;
7913 case CONVERT_EXPR:
7914 case VIEW_CONVERT_EXPR:
7915 case NOP_EXPR:
7917 tree oldop = TREE_OPERAND (t, 0);
7918 tree op = cxx_eval_constant_expression (call, oldop,
7919 allow_non_constant, addr,
7920 non_constant_p);
7921 if (*non_constant_p)
7922 return t;
7923 if (op == oldop)
7924 /* We didn't fold at the top so we could check for ptr-int
7925 conversion. */
7926 return fold (t);
7927 r = fold_build1 (TREE_CODE (t), TREE_TYPE (t), op);
7928 /* Conversion of an out-of-range value has implementation-defined
7929 behavior; the language considers it different from arithmetic
7930 overflow, which is undefined. */
7931 if (TREE_OVERFLOW_P (r) && !TREE_OVERFLOW_P (op))
7932 TREE_OVERFLOW (r) = false;
7934 break;
7936 case EMPTY_CLASS_EXPR:
7937 /* This is good enough for a function argument that might not get
7938 used, and they can't do anything with it, so just return it. */
7939 return t;
7941 case LAMBDA_EXPR:
7942 case PREINCREMENT_EXPR:
7943 case POSTINCREMENT_EXPR:
7944 case PREDECREMENT_EXPR:
7945 case POSTDECREMENT_EXPR:
7946 case NEW_EXPR:
7947 case VEC_NEW_EXPR:
7948 case DELETE_EXPR:
7949 case VEC_DELETE_EXPR:
7950 case THROW_EXPR:
7951 case MODIFY_EXPR:
7952 case MODOP_EXPR:
7953 /* GCC internal stuff. */
7954 case VA_ARG_EXPR:
7955 case OBJ_TYPE_REF:
7956 case WITH_CLEANUP_EXPR:
7957 case STATEMENT_LIST:
7958 case BIND_EXPR:
7959 case NON_DEPENDENT_EXPR:
7960 case BASELINK:
7961 case EXPR_STMT:
7962 case OFFSET_REF:
7963 if (!allow_non_constant)
7964 error_at (EXPR_LOC_OR_HERE (t),
7965 "expression %qE is not a constant-expression", t);
7966 *non_constant_p = true;
7967 break;
7969 default:
7970 internal_error ("unexpected expression %qE of kind %s", t,
7971 tree_code_name[TREE_CODE (t)]);
7972 *non_constant_p = true;
7973 break;
7976 if (r == error_mark_node)
7977 *non_constant_p = true;
7979 if (*non_constant_p)
7980 return t;
7981 else
7982 return r;
7985 static tree
7986 cxx_eval_outermost_constant_expr (tree t, bool allow_non_constant)
7988 bool non_constant_p = false;
7989 tree r = cxx_eval_constant_expression (NULL, t, allow_non_constant,
7990 false, &non_constant_p);
7992 verify_constant (r, allow_non_constant, &non_constant_p);
7994 if (TREE_CODE (t) != CONSTRUCTOR
7995 && cp_has_mutable_p (TREE_TYPE (t)))
7997 /* We allow a mutable type if the original expression was a
7998 CONSTRUCTOR so that we can do aggregate initialization of
7999 constexpr variables. */
8000 if (!allow_non_constant)
8001 error ("%qT cannot be the type of a complete constant expression "
8002 "because it has mutable sub-objects", TREE_TYPE (t));
8003 non_constant_p = true;
8006 /* Technically we should check this for all subexpressions, but that
8007 runs into problems with our internal representation of pointer
8008 subtraction and the 5.19 rules are still in flux. */
8009 if (CONVERT_EXPR_CODE_P (TREE_CODE (r))
8010 && ARITHMETIC_TYPE_P (TREE_TYPE (r))
8011 && TREE_CODE (TREE_OPERAND (r, 0)) == ADDR_EXPR)
8013 if (!allow_non_constant)
8014 error ("conversion from pointer type %qT "
8015 "to arithmetic type %qT in a constant-expression",
8016 TREE_TYPE (TREE_OPERAND (r, 0)), TREE_TYPE (r));
8017 non_constant_p = true;
8020 if (non_constant_p && !allow_non_constant)
8021 return error_mark_node;
8022 else if (non_constant_p && TREE_CONSTANT (t))
8024 /* This isn't actually constant, so unset TREE_CONSTANT. */
8025 if (EXPR_P (t) || TREE_CODE (t) == CONSTRUCTOR)
8026 r = copy_node (t);
8027 else
8028 r = build_nop (TREE_TYPE (t), t);
8029 TREE_CONSTANT (r) = false;
8030 return r;
8032 else if (non_constant_p || r == t)
8033 return t;
8034 else if (TREE_CODE (r) == CONSTRUCTOR && CLASS_TYPE_P (TREE_TYPE (r)))
8036 if (TREE_CODE (t) == TARGET_EXPR
8037 && TARGET_EXPR_INITIAL (t) == r)
8038 return t;
8039 else
8041 r = get_target_expr (r);
8042 TREE_CONSTANT (r) = true;
8043 return r;
8046 else
8047 return r;
8050 /* Returns true if T is a valid subexpression of a constant expression,
8051 even if it isn't itself a constant expression. */
8053 bool
8054 is_sub_constant_expr (tree t)
8056 bool non_constant_p = false;
8057 cxx_eval_constant_expression (NULL, t, true, false, &non_constant_p);
8058 return !non_constant_p;
8061 /* If T represents a constant expression returns its reduced value.
8062 Otherwise return error_mark_node. If T is dependent, then
8063 return NULL. */
8065 tree
8066 cxx_constant_value (tree t)
8068 return cxx_eval_outermost_constant_expr (t, false);
8071 /* If T is a constant expression, returns its reduced value.
8072 Otherwise, if T does not have TREE_CONSTANT set, returns T.
8073 Otherwise, returns a version of T without TREE_CONSTANT. */
8075 tree
8076 maybe_constant_value (tree t)
8078 tree r;
8080 if (type_dependent_expression_p (t)
8081 || type_unknown_p (t)
8082 || BRACE_ENCLOSED_INITIALIZER_P (t)
8083 || !potential_constant_expression (t)
8084 || value_dependent_expression_p (t))
8086 if (TREE_OVERFLOW_P (t))
8088 t = build_nop (TREE_TYPE (t), t);
8089 TREE_CONSTANT (t) = false;
8091 return t;
8094 r = cxx_eval_outermost_constant_expr (t, true);
8095 #ifdef ENABLE_CHECKING
8096 /* cp_tree_equal looks through NOPs, so allow them. */
8097 gcc_assert (r == t
8098 || CONVERT_EXPR_P (t)
8099 || (TREE_CONSTANT (t) && !TREE_CONSTANT (r))
8100 || !cp_tree_equal (r, t));
8101 #endif
8102 return r;
8105 /* Like maybe_constant_value, but returns a CONSTRUCTOR directly, rather
8106 than wrapped in a TARGET_EXPR. */
8108 tree
8109 maybe_constant_init (tree t)
8111 t = maybe_constant_value (t);
8112 if (TREE_CODE (t) == TARGET_EXPR)
8114 tree init = TARGET_EXPR_INITIAL (t);
8115 if (TREE_CODE (init) == CONSTRUCTOR
8116 && TREE_CONSTANT (init))
8117 t = init;
8119 return t;
8122 #if 0
8123 /* FIXME see ADDR_EXPR section in potential_constant_expression_1. */
8124 /* Return true if the object referred to by REF has automatic or thread
8125 local storage. */
8127 enum { ck_ok, ck_bad, ck_unknown };
8128 static int
8129 check_automatic_or_tls (tree ref)
8131 enum machine_mode mode;
8132 HOST_WIDE_INT bitsize, bitpos;
8133 tree offset;
8134 int volatilep = 0, unsignedp = 0;
8135 tree decl = get_inner_reference (ref, &bitsize, &bitpos, &offset,
8136 &mode, &unsignedp, &volatilep, false);
8137 duration_kind dk;
8139 /* If there isn't a decl in the middle, we don't know the linkage here,
8140 and this isn't a constant expression anyway. */
8141 if (!DECL_P (decl))
8142 return ck_unknown;
8143 dk = decl_storage_duration (decl);
8144 return (dk == dk_auto || dk == dk_thread) ? ck_bad : ck_ok;
8146 #endif
8148 /* Return true if T denotes a potentially constant expression. Issue
8149 diagnostic as appropriate under control of FLAGS. If WANT_RVAL is true,
8150 an lvalue-rvalue conversion is implied.
8152 C++0x [expr.const] used to say
8154 6 An expression is a potential constant expression if it is
8155 a constant expression where all occurences of function
8156 parameters are replaced by arbitrary constant expressions
8157 of the appropriate type.
8159 2 A conditional expression is a constant expression unless it
8160 involves one of the following as a potentially evaluated
8161 subexpression (3.2), but subexpressions of logical AND (5.14),
8162 logical OR (5.15), and conditional (5.16) operations that are
8163 not evaluated are not considered. */
8165 static bool
8166 potential_constant_expression_1 (tree t, bool want_rval, tsubst_flags_t flags)
8168 enum { any = false, rval = true };
8169 int i;
8170 tree tmp;
8172 if (t == error_mark_node)
8173 return false;
8174 if (t == NULL_TREE)
8175 return true;
8176 if (TREE_THIS_VOLATILE (t))
8178 if (flags & tf_error)
8179 error ("expression %qE has side-effects", t);
8180 return false;
8182 if (CONSTANT_CLASS_P (t))
8184 if (TREE_OVERFLOW (t))
8186 if (flags & tf_error)
8188 permerror (EXPR_LOC_OR_HERE (t),
8189 "overflow in constant expression");
8190 if (flag_permissive)
8191 return true;
8193 return false;
8195 return true;
8198 switch (TREE_CODE (t))
8200 case FUNCTION_DECL:
8201 case BASELINK:
8202 case TEMPLATE_DECL:
8203 case OVERLOAD:
8204 case TEMPLATE_ID_EXPR:
8205 case LABEL_DECL:
8206 case CONST_DECL:
8207 case SIZEOF_EXPR:
8208 case ALIGNOF_EXPR:
8209 case OFFSETOF_EXPR:
8210 case NOEXCEPT_EXPR:
8211 case TEMPLATE_PARM_INDEX:
8212 case TRAIT_EXPR:
8213 case IDENTIFIER_NODE:
8214 case USERDEF_LITERAL:
8215 /* We can see a FIELD_DECL in a pointer-to-member expression. */
8216 case FIELD_DECL:
8217 case PARM_DECL:
8218 case USING_DECL:
8219 return true;
8221 case AGGR_INIT_EXPR:
8222 case CALL_EXPR:
8223 /* -- an invocation of a function other than a constexpr function
8224 or a constexpr constructor. */
8226 tree fun = get_function_named_in_call (t);
8227 const int nargs = call_expr_nargs (t);
8228 i = 0;
8230 if (is_overloaded_fn (fun))
8232 if (TREE_CODE (fun) == FUNCTION_DECL)
8234 if (builtin_valid_in_constant_expr_p (fun))
8235 return true;
8236 if (!DECL_DECLARED_CONSTEXPR_P (fun)
8237 /* Allow any built-in function; if the expansion
8238 isn't constant, we'll deal with that then. */
8239 && !is_builtin_fn (fun))
8241 if (flags & tf_error)
8243 error_at (EXPR_LOC_OR_HERE (t),
8244 "call to non-constexpr function %qD", fun);
8245 explain_invalid_constexpr_fn (fun);
8247 return false;
8249 /* A call to a non-static member function takes the address
8250 of the object as the first argument. But in a constant
8251 expression the address will be folded away, so look
8252 through it now. */
8253 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fun)
8254 && !DECL_CONSTRUCTOR_P (fun))
8256 tree x = get_nth_callarg (t, 0);
8257 if (is_this_parameter (x))
8259 if (DECL_CONSTRUCTOR_P (DECL_CONTEXT (x)))
8261 if (flags & tf_error)
8262 sorry ("calling a member function of the "
8263 "object being constructed in a constant "
8264 "expression");
8265 return false;
8267 /* Otherwise OK. */;
8269 else if (!potential_constant_expression_1 (x, rval, flags))
8270 return false;
8271 i = 1;
8274 else
8275 fun = get_first_fn (fun);
8276 /* Skip initial arguments to base constructors. */
8277 if (DECL_BASE_CONSTRUCTOR_P (fun))
8278 i = num_artificial_parms_for (fun);
8279 fun = DECL_ORIGIN (fun);
8281 else
8283 if (potential_constant_expression_1 (fun, rval, flags))
8284 /* Might end up being a constant function pointer. */;
8285 else
8286 return false;
8288 for (; i < nargs; ++i)
8290 tree x = get_nth_callarg (t, i);
8291 if (!potential_constant_expression_1 (x, rval, flags))
8292 return false;
8294 return true;
8297 case NON_LVALUE_EXPR:
8298 /* -- an lvalue-to-rvalue conversion (4.1) unless it is applied to
8299 -- an lvalue of integral type that refers to a non-volatile
8300 const variable or static data member initialized with
8301 constant expressions, or
8303 -- an lvalue of literal type that refers to non-volatile
8304 object defined with constexpr, or that refers to a
8305 sub-object of such an object; */
8306 return potential_constant_expression_1 (TREE_OPERAND (t, 0), rval, flags);
8308 case VAR_DECL:
8309 if (want_rval && !decl_constant_var_p (t)
8310 && !dependent_type_p (TREE_TYPE (t)))
8312 if (flags & tf_error)
8313 non_const_var_error (t);
8314 return false;
8316 return true;
8318 case NOP_EXPR:
8319 case CONVERT_EXPR:
8320 case VIEW_CONVERT_EXPR:
8321 /* -- a reinterpret_cast. FIXME not implemented, and this rule
8322 may change to something more specific to type-punning (DR 1312). */
8324 tree from = TREE_OPERAND (t, 0);
8325 return (potential_constant_expression_1
8326 (from, TREE_CODE (t) != VIEW_CONVERT_EXPR, flags));
8329 case ADDR_EXPR:
8330 /* -- a unary operator & that is applied to an lvalue that
8331 designates an object with thread or automatic storage
8332 duration; */
8333 t = TREE_OPERAND (t, 0);
8334 #if 0
8335 /* FIXME adjust when issue 1197 is fully resolved. For now don't do
8336 any checking here, as we might dereference the pointer later. If
8337 we remove this code, also remove check_automatic_or_tls. */
8338 i = check_automatic_or_tls (t);
8339 if (i == ck_ok)
8340 return true;
8341 if (i == ck_bad)
8343 if (flags & tf_error)
8344 error ("address-of an object %qE with thread local or "
8345 "automatic storage is not a constant expression", t);
8346 return false;
8348 #endif
8349 return potential_constant_expression_1 (t, any, flags);
8351 case COMPONENT_REF:
8352 case BIT_FIELD_REF:
8353 case ARROW_EXPR:
8354 case OFFSET_REF:
8355 /* -- a class member access unless its postfix-expression is
8356 of literal type or of pointer to literal type. */
8357 /* This test would be redundant, as it follows from the
8358 postfix-expression being a potential constant expression. */
8359 return potential_constant_expression_1 (TREE_OPERAND (t, 0),
8360 want_rval, flags);
8362 case EXPR_PACK_EXPANSION:
8363 return potential_constant_expression_1 (PACK_EXPANSION_PATTERN (t),
8364 want_rval, flags);
8366 case INDIRECT_REF:
8368 tree x = TREE_OPERAND (t, 0);
8369 STRIP_NOPS (x);
8370 if (is_this_parameter (x))
8372 if (want_rval && DECL_CONTEXT (x)
8373 && DECL_CONSTRUCTOR_P (DECL_CONTEXT (x)))
8375 if (flags & tf_error)
8376 sorry ("use of the value of the object being constructed "
8377 "in a constant expression");
8378 return false;
8380 return true;
8382 return potential_constant_expression_1 (x, rval, flags);
8385 case LAMBDA_EXPR:
8386 case DYNAMIC_CAST_EXPR:
8387 case PSEUDO_DTOR_EXPR:
8388 case PREINCREMENT_EXPR:
8389 case POSTINCREMENT_EXPR:
8390 case PREDECREMENT_EXPR:
8391 case POSTDECREMENT_EXPR:
8392 case NEW_EXPR:
8393 case VEC_NEW_EXPR:
8394 case DELETE_EXPR:
8395 case VEC_DELETE_EXPR:
8396 case THROW_EXPR:
8397 case MODIFY_EXPR:
8398 case MODOP_EXPR:
8399 /* GCC internal stuff. */
8400 case VA_ARG_EXPR:
8401 case OBJ_TYPE_REF:
8402 case WITH_CLEANUP_EXPR:
8403 case CLEANUP_POINT_EXPR:
8404 case MUST_NOT_THROW_EXPR:
8405 case TRY_CATCH_EXPR:
8406 case STATEMENT_LIST:
8407 /* Don't bother trying to define a subset of statement-expressions to
8408 be constant-expressions, at least for now. */
8409 case STMT_EXPR:
8410 case EXPR_STMT:
8411 case BIND_EXPR:
8412 case TRANSACTION_EXPR:
8413 case IF_STMT:
8414 case DO_STMT:
8415 case FOR_STMT:
8416 case WHILE_STMT:
8417 if (flags & tf_error)
8418 error ("expression %qE is not a constant-expression", t);
8419 return false;
8421 case TYPEID_EXPR:
8422 /* -- a typeid expression whose operand is of polymorphic
8423 class type; */
8425 tree e = TREE_OPERAND (t, 0);
8426 if (!TYPE_P (e) && !type_dependent_expression_p (e)
8427 && TYPE_POLYMORPHIC_P (TREE_TYPE (e)))
8429 if (flags & tf_error)
8430 error ("typeid-expression is not a constant expression "
8431 "because %qE is of polymorphic type", e);
8432 return false;
8434 return true;
8437 case MINUS_EXPR:
8438 /* -- a subtraction where both operands are pointers. */
8439 if (TYPE_PTR_P (TREE_OPERAND (t, 0))
8440 && TYPE_PTR_P (TREE_OPERAND (t, 1)))
8442 if (flags & tf_error)
8443 error ("difference of two pointer expressions is not "
8444 "a constant expression");
8445 return false;
8447 want_rval = true;
8448 goto binary;
8450 case LT_EXPR:
8451 case LE_EXPR:
8452 case GT_EXPR:
8453 case GE_EXPR:
8454 case EQ_EXPR:
8455 case NE_EXPR:
8456 /* -- a relational or equality operator where at least
8457 one of the operands is a pointer. */
8458 if (TYPE_PTR_P (TREE_OPERAND (t, 0))
8459 || TYPE_PTR_P (TREE_OPERAND (t, 1)))
8461 if (flags & tf_error)
8462 error ("pointer comparison expression is not a "
8463 "constant expression");
8464 return false;
8466 want_rval = true;
8467 goto binary;
8469 case BIT_NOT_EXPR:
8470 /* A destructor. */
8471 if (TYPE_P (TREE_OPERAND (t, 0)))
8472 return true;
8473 /* else fall through. */
8475 case REALPART_EXPR:
8476 case IMAGPART_EXPR:
8477 case CONJ_EXPR:
8478 case SAVE_EXPR:
8479 case FIX_TRUNC_EXPR:
8480 case FLOAT_EXPR:
8481 case NEGATE_EXPR:
8482 case ABS_EXPR:
8483 case TRUTH_NOT_EXPR:
8484 case FIXED_CONVERT_EXPR:
8485 case UNARY_PLUS_EXPR:
8486 return potential_constant_expression_1 (TREE_OPERAND (t, 0), rval,
8487 flags);
8489 case CAST_EXPR:
8490 case CONST_CAST_EXPR:
8491 case STATIC_CAST_EXPR:
8492 case REINTERPRET_CAST_EXPR:
8493 case IMPLICIT_CONV_EXPR:
8494 return (potential_constant_expression_1
8495 (TREE_OPERAND (t, 0),
8496 TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE, flags));
8498 case PAREN_EXPR:
8499 case NON_DEPENDENT_EXPR:
8500 /* For convenience. */
8501 case RETURN_EXPR:
8502 return potential_constant_expression_1 (TREE_OPERAND (t, 0),
8503 want_rval, flags);
8505 case SCOPE_REF:
8506 return potential_constant_expression_1 (TREE_OPERAND (t, 1),
8507 want_rval, flags);
8509 case TARGET_EXPR:
8510 if (!literal_type_p (TREE_TYPE (t)))
8512 if (flags & tf_error)
8514 error ("temporary of non-literal type %qT in a "
8515 "constant expression", TREE_TYPE (t));
8516 explain_non_literal_class (TREE_TYPE (t));
8518 return false;
8520 case INIT_EXPR:
8521 return potential_constant_expression_1 (TREE_OPERAND (t, 1),
8522 rval, flags);
8524 case CONSTRUCTOR:
8526 VEC(constructor_elt, gc) *v = CONSTRUCTOR_ELTS (t);
8527 constructor_elt *ce;
8528 for (i = 0; VEC_iterate (constructor_elt, v, i, ce); ++i)
8529 if (!potential_constant_expression_1 (ce->value, want_rval, flags))
8530 return false;
8531 return true;
8534 case TREE_LIST:
8536 gcc_assert (TREE_PURPOSE (t) == NULL_TREE
8537 || DECL_P (TREE_PURPOSE (t)));
8538 if (!potential_constant_expression_1 (TREE_VALUE (t), want_rval,
8539 flags))
8540 return false;
8541 if (TREE_CHAIN (t) == NULL_TREE)
8542 return true;
8543 return potential_constant_expression_1 (TREE_CHAIN (t), want_rval,
8544 flags);
8547 case TRUNC_DIV_EXPR:
8548 case CEIL_DIV_EXPR:
8549 case FLOOR_DIV_EXPR:
8550 case ROUND_DIV_EXPR:
8551 case TRUNC_MOD_EXPR:
8552 case CEIL_MOD_EXPR:
8553 case ROUND_MOD_EXPR:
8555 tree denom = TREE_OPERAND (t, 1);
8556 /* We can't call maybe_constant_value on an expression
8557 that hasn't been through fold_non_dependent_expr yet. */
8558 if (!processing_template_decl)
8559 denom = maybe_constant_value (denom);
8560 if (integer_zerop (denom))
8562 if (flags & tf_error)
8563 error ("division by zero is not a constant-expression");
8564 return false;
8566 else
8568 want_rval = true;
8569 goto binary;
8573 case COMPOUND_EXPR:
8575 /* check_return_expr sometimes wraps a TARGET_EXPR in a
8576 COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR
8577 introduced by build_call_a. */
8578 tree op0 = TREE_OPERAND (t, 0);
8579 tree op1 = TREE_OPERAND (t, 1);
8580 STRIP_NOPS (op1);
8581 if ((TREE_CODE (op0) == TARGET_EXPR && op1 == TARGET_EXPR_SLOT (op0))
8582 || TREE_CODE (op1) == EMPTY_CLASS_EXPR)
8583 return potential_constant_expression_1 (op0, want_rval, flags);
8584 else
8585 goto binary;
8588 /* If the first operand is the non-short-circuit constant, look at
8589 the second operand; otherwise we only care about the first one for
8590 potentiality. */
8591 case TRUTH_AND_EXPR:
8592 case TRUTH_ANDIF_EXPR:
8593 tmp = boolean_true_node;
8594 goto truth;
8595 case TRUTH_OR_EXPR:
8596 case TRUTH_ORIF_EXPR:
8597 tmp = boolean_false_node;
8598 truth:
8600 tree op = TREE_OPERAND (t, 0);
8601 if (!potential_constant_expression_1 (op, rval, flags))
8602 return false;
8603 if (!processing_template_decl)
8604 op = maybe_constant_value (op);
8605 if (tree_int_cst_equal (op, tmp))
8606 return potential_constant_expression_1 (TREE_OPERAND (t, 1), rval, flags);
8607 else
8608 return true;
8611 case PLUS_EXPR:
8612 case MULT_EXPR:
8613 case POINTER_PLUS_EXPR:
8614 case RDIV_EXPR:
8615 case EXACT_DIV_EXPR:
8616 case MIN_EXPR:
8617 case MAX_EXPR:
8618 case LSHIFT_EXPR:
8619 case RSHIFT_EXPR:
8620 case LROTATE_EXPR:
8621 case RROTATE_EXPR:
8622 case BIT_IOR_EXPR:
8623 case BIT_XOR_EXPR:
8624 case BIT_AND_EXPR:
8625 case TRUTH_XOR_EXPR:
8626 case UNORDERED_EXPR:
8627 case ORDERED_EXPR:
8628 case UNLT_EXPR:
8629 case UNLE_EXPR:
8630 case UNGT_EXPR:
8631 case UNGE_EXPR:
8632 case UNEQ_EXPR:
8633 case LTGT_EXPR:
8634 case RANGE_EXPR:
8635 case COMPLEX_EXPR:
8636 want_rval = true;
8637 /* Fall through. */
8638 case ARRAY_REF:
8639 case ARRAY_RANGE_REF:
8640 case MEMBER_REF:
8641 case DOTSTAR_EXPR:
8642 binary:
8643 for (i = 0; i < 2; ++i)
8644 if (!potential_constant_expression_1 (TREE_OPERAND (t, i),
8645 want_rval, flags))
8646 return false;
8647 return true;
8649 case FMA_EXPR:
8650 case VEC_PERM_EXPR:
8651 for (i = 0; i < 3; ++i)
8652 if (!potential_constant_expression_1 (TREE_OPERAND (t, i),
8653 true, flags))
8654 return false;
8655 return true;
8657 case COND_EXPR:
8658 case VEC_COND_EXPR:
8659 /* If the condition is a known constant, we know which of the legs we
8660 care about; otherwise we only require that the condition and
8661 either of the legs be potentially constant. */
8662 tmp = TREE_OPERAND (t, 0);
8663 if (!potential_constant_expression_1 (tmp, rval, flags))
8664 return false;
8665 if (!processing_template_decl)
8666 tmp = maybe_constant_value (tmp);
8667 if (integer_zerop (tmp))
8668 return potential_constant_expression_1 (TREE_OPERAND (t, 2),
8669 want_rval, flags);
8670 else if (TREE_CODE (tmp) == INTEGER_CST)
8671 return potential_constant_expression_1 (TREE_OPERAND (t, 1),
8672 want_rval, flags);
8673 for (i = 1; i < 3; ++i)
8674 if (potential_constant_expression_1 (TREE_OPERAND (t, i),
8675 want_rval, tf_none))
8676 return true;
8677 if (flags & tf_error)
8678 error ("expression %qE is not a constant-expression", t);
8679 return false;
8681 case VEC_INIT_EXPR:
8682 if (VEC_INIT_EXPR_IS_CONSTEXPR (t))
8683 return true;
8684 if (flags & tf_error)
8686 error ("non-constant array initialization");
8687 diagnose_non_constexpr_vec_init (t);
8689 return false;
8691 default:
8692 if (objc_is_property_ref (t))
8693 return false;
8695 sorry ("unexpected AST of kind %s", tree_code_name[TREE_CODE (t)]);
8696 gcc_unreachable();
8697 return false;
8701 /* The main entry point to the above. */
8703 bool
8704 potential_constant_expression (tree t)
8706 return potential_constant_expression_1 (t, false, tf_none);
8709 /* As above, but require a constant rvalue. */
8711 bool
8712 potential_rvalue_constant_expression (tree t)
8714 return potential_constant_expression_1 (t, true, tf_none);
8717 /* Like above, but complain about non-constant expressions. */
8719 bool
8720 require_potential_constant_expression (tree t)
8722 return potential_constant_expression_1 (t, false, tf_warning_or_error);
8725 /* Cross product of the above. */
8727 bool
8728 require_potential_rvalue_constant_expression (tree t)
8730 return potential_constant_expression_1 (t, true, tf_warning_or_error);
8733 /* Constructor for a lambda expression. */
8735 tree
8736 build_lambda_expr (void)
8738 tree lambda = make_node (LAMBDA_EXPR);
8739 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda) = CPLD_NONE;
8740 LAMBDA_EXPR_CAPTURE_LIST (lambda) = NULL_TREE;
8741 LAMBDA_EXPR_THIS_CAPTURE (lambda) = NULL_TREE;
8742 LAMBDA_EXPR_PENDING_PROXIES (lambda) = NULL;
8743 LAMBDA_EXPR_RETURN_TYPE (lambda) = NULL_TREE;
8744 LAMBDA_EXPR_MUTABLE_P (lambda) = false;
8745 return lambda;
8748 /* Create the closure object for a LAMBDA_EXPR. */
8750 tree
8751 build_lambda_object (tree lambda_expr)
8753 /* Build aggregate constructor call.
8754 - cp_parser_braced_list
8755 - cp_parser_functional_cast */
8756 VEC(constructor_elt,gc) *elts = NULL;
8757 tree node, expr, type;
8758 location_t saved_loc;
8760 if (processing_template_decl)
8761 return lambda_expr;
8763 /* Make sure any error messages refer to the lambda-introducer. */
8764 saved_loc = input_location;
8765 input_location = LAMBDA_EXPR_LOCATION (lambda_expr);
8767 for (node = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr);
8768 node;
8769 node = TREE_CHAIN (node))
8771 tree field = TREE_PURPOSE (node);
8772 tree val = TREE_VALUE (node);
8774 if (field == error_mark_node)
8776 expr = error_mark_node;
8777 goto out;
8780 if (DECL_P (val))
8781 mark_used (val);
8783 /* Mere mortals can't copy arrays with aggregate initialization, so
8784 do some magic to make it work here. */
8785 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE)
8786 val = build_array_copy (val);
8787 else if (DECL_NORMAL_CAPTURE_P (field)
8788 && TREE_CODE (TREE_TYPE (field)) != REFERENCE_TYPE)
8790 /* "the entities that are captured by copy are used to
8791 direct-initialize each corresponding non-static data
8792 member of the resulting closure object."
8794 There's normally no way to express direct-initialization
8795 from an element of a CONSTRUCTOR, so we build up a special
8796 TARGET_EXPR to bypass the usual copy-initialization. */
8797 val = force_rvalue (val, tf_warning_or_error);
8798 if (TREE_CODE (val) == TARGET_EXPR)
8799 TARGET_EXPR_DIRECT_INIT_P (val) = true;
8802 CONSTRUCTOR_APPEND_ELT (elts, DECL_NAME (field), val);
8805 expr = build_constructor (init_list_type_node, elts);
8806 CONSTRUCTOR_IS_DIRECT_INIT (expr) = 1;
8808 /* N2927: "[The closure] class type is not an aggregate."
8809 But we briefly treat it as an aggregate to make this simpler. */
8810 type = LAMBDA_EXPR_CLOSURE (lambda_expr);
8811 CLASSTYPE_NON_AGGREGATE (type) = 0;
8812 expr = finish_compound_literal (type, expr, tf_warning_or_error);
8813 CLASSTYPE_NON_AGGREGATE (type) = 1;
8815 out:
8816 input_location = saved_loc;
8817 return expr;
8820 /* Return an initialized RECORD_TYPE for LAMBDA.
8821 LAMBDA must have its explicit captures already. */
8823 tree
8824 begin_lambda_type (tree lambda)
8826 tree type;
8829 /* Unique name. This is just like an unnamed class, but we cannot use
8830 make_anon_name because of certain checks against TYPE_ANONYMOUS_P. */
8831 tree name;
8832 name = make_lambda_name ();
8834 /* Create the new RECORD_TYPE for this lambda. */
8835 type = xref_tag (/*tag_code=*/record_type,
8836 name,
8837 /*scope=*/ts_within_enclosing_non_class,
8838 /*template_header_p=*/false);
8841 /* Designate it as a struct so that we can use aggregate initialization. */
8842 CLASSTYPE_DECLARED_CLASS (type) = false;
8844 /* Cross-reference the expression and the type. */
8845 LAMBDA_EXPR_CLOSURE (lambda) = type;
8846 CLASSTYPE_LAMBDA_EXPR (type) = lambda;
8848 /* Clear base types. */
8849 xref_basetypes (type, /*bases=*/NULL_TREE);
8851 /* Start the class. */
8852 type = begin_class_definition (type);
8853 if (type == error_mark_node)
8854 return error_mark_node;
8856 return type;
8859 /* Returns the type to use for the return type of the operator() of a
8860 closure class. */
8862 tree
8863 lambda_return_type (tree expr)
8865 if (expr == NULL_TREE)
8866 return void_type_node;
8867 if (type_unknown_p (expr)
8868 || BRACE_ENCLOSED_INITIALIZER_P (expr))
8870 cxx_incomplete_type_error (expr, TREE_TYPE (expr));
8871 return void_type_node;
8873 gcc_checking_assert (!type_dependent_expression_p (expr));
8874 return cv_unqualified (type_decays_to (unlowered_expr_type (expr)));
8877 /* Given a LAMBDA_EXPR or closure type LAMBDA, return the op() of the
8878 closure type. */
8880 tree
8881 lambda_function (tree lambda)
8883 tree type;
8884 if (TREE_CODE (lambda) == LAMBDA_EXPR)
8885 type = LAMBDA_EXPR_CLOSURE (lambda);
8886 else
8887 type = lambda;
8888 gcc_assert (LAMBDA_TYPE_P (type));
8889 /* Don't let debug_tree cause instantiation. */
8890 if (CLASSTYPE_TEMPLATE_INSTANTIATION (type)
8891 && !COMPLETE_OR_OPEN_TYPE_P (type))
8892 return NULL_TREE;
8893 lambda = lookup_member (type, ansi_opname (CALL_EXPR),
8894 /*protect=*/0, /*want_type=*/false,
8895 tf_warning_or_error);
8896 if (lambda)
8897 lambda = BASELINK_FUNCTIONS (lambda);
8898 return lambda;
8901 /* Returns the type to use for the FIELD_DECL corresponding to the
8902 capture of EXPR.
8903 The caller should add REFERENCE_TYPE for capture by reference. */
8905 tree
8906 lambda_capture_field_type (tree expr)
8908 tree type;
8909 if (type_dependent_expression_p (expr))
8911 type = cxx_make_type (DECLTYPE_TYPE);
8912 DECLTYPE_TYPE_EXPR (type) = expr;
8913 DECLTYPE_FOR_LAMBDA_CAPTURE (type) = true;
8914 SET_TYPE_STRUCTURAL_EQUALITY (type);
8916 else
8917 type = non_reference (unlowered_expr_type (expr));
8918 return type;
8921 /* Insert the deduced return type for an auto function. */
8923 void
8924 apply_deduced_return_type (tree fco, tree return_type)
8926 tree result;
8928 if (return_type == error_mark_node)
8929 return;
8931 if (LAMBDA_FUNCTION_P (fco))
8933 tree lambda = CLASSTYPE_LAMBDA_EXPR (current_class_type);
8934 LAMBDA_EXPR_RETURN_TYPE (lambda) = return_type;
8937 if (DECL_CONV_FN_P (fco))
8938 DECL_NAME (fco) = mangle_conv_op_name_for_type (return_type);
8940 TREE_TYPE (fco) = change_return_type (return_type, TREE_TYPE (fco));
8942 result = DECL_RESULT (fco);
8943 if (result == NULL_TREE)
8944 return;
8945 if (TREE_TYPE (result) == return_type)
8946 return;
8948 /* We already have a DECL_RESULT from start_preparsed_function.
8949 Now we need to redo the work it and allocate_struct_function
8950 did to reflect the new type. */
8951 gcc_assert (current_function_decl == fco);
8952 result = build_decl (input_location, RESULT_DECL, NULL_TREE,
8953 TYPE_MAIN_VARIANT (return_type));
8954 DECL_ARTIFICIAL (result) = 1;
8955 DECL_IGNORED_P (result) = 1;
8956 cp_apply_type_quals_to_decl (cp_type_quals (return_type),
8957 result);
8959 DECL_RESULT (fco) = result;
8961 if (!processing_template_decl)
8963 bool aggr = aggregate_value_p (result, fco);
8964 #ifdef PCC_STATIC_STRUCT_RETURN
8965 cfun->returns_pcc_struct = aggr;
8966 #endif
8967 cfun->returns_struct = aggr;
8972 /* DECL is a local variable or parameter from the surrounding scope of a
8973 lambda-expression. Returns the decltype for a use of the capture field
8974 for DECL even if it hasn't been captured yet. */
8976 static tree
8977 capture_decltype (tree decl)
8979 tree lam = CLASSTYPE_LAMBDA_EXPR (DECL_CONTEXT (current_function_decl));
8980 /* FIXME do lookup instead of list walk? */
8981 tree cap = value_member (decl, LAMBDA_EXPR_CAPTURE_LIST (lam));
8982 tree type;
8984 if (cap)
8985 type = TREE_TYPE (TREE_PURPOSE (cap));
8986 else
8987 switch (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lam))
8989 case CPLD_NONE:
8990 error ("%qD is not captured", decl);
8991 return error_mark_node;
8993 case CPLD_COPY:
8994 type = TREE_TYPE (decl);
8995 if (TREE_CODE (type) == REFERENCE_TYPE
8996 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
8997 type = TREE_TYPE (type);
8998 break;
9000 case CPLD_REFERENCE:
9001 type = TREE_TYPE (decl);
9002 if (TREE_CODE (type) != REFERENCE_TYPE)
9003 type = build_reference_type (TREE_TYPE (decl));
9004 break;
9006 default:
9007 gcc_unreachable ();
9010 if (TREE_CODE (type) != REFERENCE_TYPE)
9012 if (!LAMBDA_EXPR_MUTABLE_P (lam))
9013 type = cp_build_qualified_type (type, (cp_type_quals (type)
9014 |TYPE_QUAL_CONST));
9015 type = build_reference_type (type);
9017 return type;
9020 /* Returns true iff DECL is a lambda capture proxy variable created by
9021 build_capture_proxy. */
9023 bool
9024 is_capture_proxy (tree decl)
9026 return (TREE_CODE (decl) == VAR_DECL
9027 && DECL_HAS_VALUE_EXPR_P (decl)
9028 && !DECL_ANON_UNION_VAR_P (decl)
9029 && LAMBDA_FUNCTION_P (DECL_CONTEXT (decl)));
9032 /* Returns true iff DECL is a capture proxy for a normal capture
9033 (i.e. without explicit initializer). */
9035 bool
9036 is_normal_capture_proxy (tree decl)
9038 if (!is_capture_proxy (decl))
9039 /* It's not a capture proxy. */
9040 return false;
9042 /* It is a capture proxy, is it a normal capture? */
9043 tree val = DECL_VALUE_EXPR (decl);
9044 if (val == error_mark_node)
9045 return true;
9047 gcc_assert (TREE_CODE (val) == COMPONENT_REF);
9048 val = TREE_OPERAND (val, 1);
9049 return DECL_NORMAL_CAPTURE_P (val);
9052 /* VAR is a capture proxy created by build_capture_proxy; add it to the
9053 current function, which is the operator() for the appropriate lambda. */
9055 void
9056 insert_capture_proxy (tree var)
9058 cp_binding_level *b;
9059 int skip;
9060 tree stmt_list;
9062 /* Put the capture proxy in the extra body block so that it won't clash
9063 with a later local variable. */
9064 b = current_binding_level;
9065 for (skip = 0; ; ++skip)
9067 cp_binding_level *n = b->level_chain;
9068 if (n->kind == sk_function_parms)
9069 break;
9070 b = n;
9072 pushdecl_with_scope (var, b, false);
9074 /* And put a DECL_EXPR in the STATEMENT_LIST for the same block. */
9075 var = build_stmt (DECL_SOURCE_LOCATION (var), DECL_EXPR, var);
9076 stmt_list = VEC_index (tree, stmt_list_stack,
9077 VEC_length (tree, stmt_list_stack) - 1 - skip);
9078 gcc_assert (stmt_list);
9079 append_to_statement_list_force (var, &stmt_list);
9082 /* We've just finished processing a lambda; if the containing scope is also
9083 a lambda, insert any capture proxies that were created while processing
9084 the nested lambda. */
9086 void
9087 insert_pending_capture_proxies (void)
9089 tree lam;
9090 VEC(tree,gc) *proxies;
9091 unsigned i;
9093 if (!current_function_decl || !LAMBDA_FUNCTION_P (current_function_decl))
9094 return;
9096 lam = CLASSTYPE_LAMBDA_EXPR (DECL_CONTEXT (current_function_decl));
9097 proxies = LAMBDA_EXPR_PENDING_PROXIES (lam);
9098 for (i = 0; i < VEC_length (tree, proxies); ++i)
9100 tree var = VEC_index (tree, proxies, i);
9101 insert_capture_proxy (var);
9103 release_tree_vector (LAMBDA_EXPR_PENDING_PROXIES (lam));
9104 LAMBDA_EXPR_PENDING_PROXIES (lam) = NULL;
9107 /* Given REF, a COMPONENT_REF designating a field in the lambda closure,
9108 return the type we want the proxy to have: the type of the field itself,
9109 with added const-qualification if the lambda isn't mutable and the
9110 capture is by value. */
9112 tree
9113 lambda_proxy_type (tree ref)
9115 tree type;
9116 if (REFERENCE_REF_P (ref))
9117 ref = TREE_OPERAND (ref, 0);
9118 type = TREE_TYPE (ref);
9119 if (!dependent_type_p (type))
9120 return type;
9121 type = cxx_make_type (DECLTYPE_TYPE);
9122 DECLTYPE_TYPE_EXPR (type) = ref;
9123 DECLTYPE_FOR_LAMBDA_PROXY (type) = true;
9124 SET_TYPE_STRUCTURAL_EQUALITY (type);
9125 return type;
9128 /* MEMBER is a capture field in a lambda closure class. Now that we're
9129 inside the operator(), build a placeholder var for future lookups and
9130 debugging. */
9132 tree
9133 build_capture_proxy (tree member)
9135 tree var, object, fn, closure, name, lam, type;
9137 closure = DECL_CONTEXT (member);
9138 fn = lambda_function (closure);
9139 lam = CLASSTYPE_LAMBDA_EXPR (closure);
9141 /* The proxy variable forwards to the capture field. */
9142 object = build_fold_indirect_ref (DECL_ARGUMENTS (fn));
9143 object = finish_non_static_data_member (member, object, NULL_TREE);
9144 if (REFERENCE_REF_P (object))
9145 object = TREE_OPERAND (object, 0);
9147 /* Remove the __ inserted by add_capture. */
9148 name = get_identifier (IDENTIFIER_POINTER (DECL_NAME (member)) + 2);
9150 type = lambda_proxy_type (object);
9151 var = build_decl (input_location, VAR_DECL, name, type);
9152 SET_DECL_VALUE_EXPR (var, object);
9153 DECL_HAS_VALUE_EXPR_P (var) = 1;
9154 DECL_ARTIFICIAL (var) = 1;
9155 TREE_USED (var) = 1;
9156 DECL_CONTEXT (var) = fn;
9158 if (name == this_identifier)
9160 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (lam) == member);
9161 LAMBDA_EXPR_THIS_CAPTURE (lam) = var;
9164 if (fn == current_function_decl)
9165 insert_capture_proxy (var);
9166 else
9167 VEC_safe_push (tree, gc, LAMBDA_EXPR_PENDING_PROXIES (lam), var);
9169 return var;
9172 /* From an ID and INITIALIZER, create a capture (by reference if
9173 BY_REFERENCE_P is true), add it to the capture-list for LAMBDA,
9174 and return it. */
9176 tree
9177 add_capture (tree lambda, tree id, tree initializer, bool by_reference_p,
9178 bool explicit_init_p)
9180 char *buf;
9181 tree type, member, name;
9183 type = lambda_capture_field_type (initializer);
9184 if (by_reference_p)
9186 type = build_reference_type (type);
9187 if (!real_lvalue_p (initializer))
9188 error ("cannot capture %qE by reference", initializer);
9190 else
9191 /* Capture by copy requires a complete type. */
9192 type = complete_type (type);
9194 /* Add __ to the beginning of the field name so that user code
9195 won't find the field with name lookup. We can't just leave the name
9196 unset because template instantiation uses the name to find
9197 instantiated fields. */
9198 buf = (char *) alloca (IDENTIFIER_LENGTH (id) + 3);
9199 buf[1] = buf[0] = '_';
9200 memcpy (buf + 2, IDENTIFIER_POINTER (id),
9201 IDENTIFIER_LENGTH (id) + 1);
9202 name = get_identifier (buf);
9204 /* If TREE_TYPE isn't set, we're still in the introducer, so check
9205 for duplicates. */
9206 if (!LAMBDA_EXPR_CLOSURE (lambda))
9208 if (IDENTIFIER_MARKED (name))
9210 pedwarn (input_location, 0,
9211 "already captured %qD in lambda expression", id);
9212 return NULL_TREE;
9214 IDENTIFIER_MARKED (name) = true;
9217 /* Make member variable. */
9218 member = build_lang_decl (FIELD_DECL, name, type);
9220 if (!explicit_init_p)
9221 /* Normal captures are invisible to name lookup but uses are replaced
9222 with references to the capture field; we implement this by only
9223 really making them invisible in unevaluated context; see
9224 qualify_lookup. For now, let's make explicitly initialized captures
9225 always visible. */
9226 DECL_NORMAL_CAPTURE_P (member) = true;
9228 if (id == this_identifier)
9229 LAMBDA_EXPR_THIS_CAPTURE (lambda) = member;
9231 /* Add it to the appropriate closure class if we've started it. */
9232 if (current_class_type
9233 && current_class_type == LAMBDA_EXPR_CLOSURE (lambda))
9234 finish_member_declaration (member);
9236 LAMBDA_EXPR_CAPTURE_LIST (lambda)
9237 = tree_cons (member, initializer, LAMBDA_EXPR_CAPTURE_LIST (lambda));
9239 if (LAMBDA_EXPR_CLOSURE (lambda))
9240 return build_capture_proxy (member);
9241 /* For explicit captures we haven't started the function yet, so we wait
9242 and build the proxy from cp_parser_lambda_body. */
9243 return NULL_TREE;
9246 /* Register all the capture members on the list CAPTURES, which is the
9247 LAMBDA_EXPR_CAPTURE_LIST for the lambda after the introducer. */
9249 void
9250 register_capture_members (tree captures)
9252 if (captures == NULL_TREE)
9253 return;
9255 register_capture_members (TREE_CHAIN (captures));
9256 /* We set this in add_capture to avoid duplicates. */
9257 IDENTIFIER_MARKED (DECL_NAME (TREE_PURPOSE (captures))) = false;
9258 finish_member_declaration (TREE_PURPOSE (captures));
9261 /* Similar to add_capture, except this works on a stack of nested lambdas.
9262 BY_REFERENCE_P in this case is derived from the default capture mode.
9263 Returns the capture for the lambda at the bottom of the stack. */
9265 tree
9266 add_default_capture (tree lambda_stack, tree id, tree initializer)
9268 bool this_capture_p = (id == this_identifier);
9270 tree var = NULL_TREE;
9272 tree saved_class_type = current_class_type;
9274 tree node;
9276 for (node = lambda_stack;
9277 node;
9278 node = TREE_CHAIN (node))
9280 tree lambda = TREE_VALUE (node);
9282 current_class_type = LAMBDA_EXPR_CLOSURE (lambda);
9283 var = add_capture (lambda,
9285 initializer,
9286 /*by_reference_p=*/
9287 (!this_capture_p
9288 && (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda)
9289 == CPLD_REFERENCE)),
9290 /*explicit_init_p=*/false);
9291 initializer = convert_from_reference (var);
9294 current_class_type = saved_class_type;
9296 return var;
9299 /* Return the capture pertaining to a use of 'this' in LAMBDA, in the form of an
9300 INDIRECT_REF, possibly adding it through default capturing. */
9302 tree
9303 lambda_expr_this_capture (tree lambda)
9305 tree result;
9307 tree this_capture = LAMBDA_EXPR_THIS_CAPTURE (lambda);
9309 /* Try to default capture 'this' if we can. */
9310 if (!this_capture
9311 && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda) != CPLD_NONE)
9313 tree containing_function = TYPE_CONTEXT (LAMBDA_EXPR_CLOSURE (lambda));
9314 tree lambda_stack = tree_cons (NULL_TREE, lambda, NULL_TREE);
9315 tree init = NULL_TREE;
9317 /* If we are in a lambda function, we can move out until we hit:
9318 1. a non-lambda function,
9319 2. a lambda function capturing 'this', or
9320 3. a non-default capturing lambda function. */
9321 while (LAMBDA_FUNCTION_P (containing_function))
9323 tree lambda
9324 = CLASSTYPE_LAMBDA_EXPR (DECL_CONTEXT (containing_function));
9326 if (LAMBDA_EXPR_THIS_CAPTURE (lambda))
9328 /* An outer lambda has already captured 'this'. */
9329 init = LAMBDA_EXPR_THIS_CAPTURE (lambda);
9330 break;
9333 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda) == CPLD_NONE)
9334 /* An outer lambda won't let us capture 'this'. */
9335 break;
9337 lambda_stack = tree_cons (NULL_TREE,
9338 lambda,
9339 lambda_stack);
9341 containing_function = decl_function_context (containing_function);
9344 if (!init && DECL_NONSTATIC_MEMBER_FUNCTION_P (containing_function)
9345 && !LAMBDA_FUNCTION_P (containing_function))
9346 /* First parameter is 'this'. */
9347 init = DECL_ARGUMENTS (containing_function);
9349 if (init)
9350 this_capture = add_default_capture (lambda_stack,
9351 /*id=*/this_identifier,
9352 init);
9355 if (!this_capture)
9357 error ("%<this%> was not captured for this lambda function");
9358 result = error_mark_node;
9360 else
9362 /* To make sure that current_class_ref is for the lambda. */
9363 gcc_assert (TYPE_MAIN_VARIANT (TREE_TYPE (current_class_ref))
9364 == LAMBDA_EXPR_CLOSURE (lambda));
9366 result = this_capture;
9368 /* If 'this' is captured, each use of 'this' is transformed into an
9369 access to the corresponding unnamed data member of the closure
9370 type cast (_expr.cast_ 5.4) to the type of 'this'. [ The cast
9371 ensures that the transformed expression is an rvalue. ] */
9372 result = rvalue (result);
9375 return result;
9378 /* Returns the method basetype of the innermost non-lambda function, or
9379 NULL_TREE if none. */
9381 tree
9382 nonlambda_method_basetype (void)
9384 tree fn, type;
9385 if (!current_class_ref)
9386 return NULL_TREE;
9388 type = current_class_type;
9389 if (!LAMBDA_TYPE_P (type))
9390 return type;
9392 /* Find the nearest enclosing non-lambda function. */
9393 fn = TYPE_NAME (type);
9395 fn = decl_function_context (fn);
9396 while (fn && LAMBDA_FUNCTION_P (fn));
9398 if (!fn || !DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
9399 return NULL_TREE;
9401 return TYPE_METHOD_BASETYPE (TREE_TYPE (fn));
9404 /* If the closure TYPE has a static op(), also add a conversion to function
9405 pointer. */
9407 void
9408 maybe_add_lambda_conv_op (tree type)
9410 bool nested = (current_function_decl != NULL_TREE);
9411 tree callop = lambda_function (type);
9412 tree rettype, name, fntype, fn, body, compound_stmt;
9413 tree thistype, stattype, statfn, convfn, call, arg;
9414 VEC (tree, gc) *argvec;
9416 if (LAMBDA_EXPR_CAPTURE_LIST (CLASSTYPE_LAMBDA_EXPR (type)) != NULL_TREE)
9417 return;
9419 if (processing_template_decl)
9420 return;
9422 stattype = build_function_type (TREE_TYPE (TREE_TYPE (callop)),
9423 FUNCTION_ARG_CHAIN (callop));
9425 /* First build up the conversion op. */
9427 rettype = build_pointer_type (stattype);
9428 name = mangle_conv_op_name_for_type (rettype);
9429 thistype = cp_build_qualified_type (type, TYPE_QUAL_CONST);
9430 fntype = build_method_type_directly (thistype, rettype, void_list_node);
9431 fn = convfn = build_lang_decl (FUNCTION_DECL, name, fntype);
9432 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (callop);
9434 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_pfn
9435 && DECL_ALIGN (fn) < 2 * BITS_PER_UNIT)
9436 DECL_ALIGN (fn) = 2 * BITS_PER_UNIT;
9438 SET_OVERLOADED_OPERATOR_CODE (fn, TYPE_EXPR);
9439 grokclassfn (type, fn, NO_SPECIAL);
9440 set_linkage_according_to_type (type, fn);
9441 rest_of_decl_compilation (fn, toplevel_bindings_p (), at_eof);
9442 DECL_IN_AGGR_P (fn) = 1;
9443 DECL_ARTIFICIAL (fn) = 1;
9444 DECL_NOT_REALLY_EXTERN (fn) = 1;
9445 DECL_DECLARED_INLINE_P (fn) = 1;
9446 DECL_ARGUMENTS (fn) = build_this_parm (fntype, TYPE_QUAL_CONST);
9448 add_method (type, fn, NULL_TREE);
9450 /* Generic thunk code fails for varargs; we'll complain in mark_used if
9451 the conversion op is used. */
9452 if (varargs_function_p (callop))
9454 DECL_DELETED_FN (fn) = 1;
9455 return;
9458 /* Now build up the thunk to be returned. */
9460 name = get_identifier ("_FUN");
9461 fn = statfn = build_lang_decl (FUNCTION_DECL, name, stattype);
9462 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (callop);
9463 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_pfn
9464 && DECL_ALIGN (fn) < 2 * BITS_PER_UNIT)
9465 DECL_ALIGN (fn) = 2 * BITS_PER_UNIT;
9466 grokclassfn (type, fn, NO_SPECIAL);
9467 set_linkage_according_to_type (type, fn);
9468 rest_of_decl_compilation (fn, toplevel_bindings_p (), at_eof);
9469 DECL_IN_AGGR_P (fn) = 1;
9470 DECL_ARTIFICIAL (fn) = 1;
9471 DECL_NOT_REALLY_EXTERN (fn) = 1;
9472 DECL_DECLARED_INLINE_P (fn) = 1;
9473 DECL_STATIC_FUNCTION_P (fn) = 1;
9474 DECL_ARGUMENTS (fn) = copy_list (DECL_CHAIN (DECL_ARGUMENTS (callop)));
9475 for (arg = DECL_ARGUMENTS (fn); arg; arg = DECL_CHAIN (arg))
9476 DECL_CONTEXT (arg) = fn;
9478 add_method (type, fn, NULL_TREE);
9480 if (nested)
9481 push_function_context ();
9482 else
9483 /* Still increment function_depth so that we don't GC in the
9484 middle of an expression. */
9485 ++function_depth;
9487 /* Generate the body of the thunk. */
9489 start_preparsed_function (statfn, NULL_TREE,
9490 SF_PRE_PARSED | SF_INCLASS_INLINE);
9491 if (DECL_ONE_ONLY (statfn))
9493 /* Put the thunk in the same comdat group as the call op. */
9494 symtab_add_to_same_comdat_group
9495 ((symtab_node) cgraph_get_create_node (statfn),
9496 (symtab_node) cgraph_get_create_node (callop));
9498 body = begin_function_body ();
9499 compound_stmt = begin_compound_stmt (0);
9501 arg = build1 (NOP_EXPR, TREE_TYPE (DECL_ARGUMENTS (callop)),
9502 null_pointer_node);
9503 argvec = make_tree_vector ();
9504 VEC_quick_push (tree, argvec, arg);
9505 for (arg = DECL_ARGUMENTS (statfn); arg; arg = DECL_CHAIN (arg))
9507 mark_exp_read (arg);
9508 VEC_safe_push (tree, gc, argvec, arg);
9510 call = build_call_a (callop, VEC_length (tree, argvec),
9511 VEC_address (tree, argvec));
9512 CALL_FROM_THUNK_P (call) = 1;
9513 if (MAYBE_CLASS_TYPE_P (TREE_TYPE (call)))
9514 call = build_cplus_new (TREE_TYPE (call), call, tf_warning_or_error);
9515 call = convert_from_reference (call);
9516 finish_return_stmt (call);
9518 finish_compound_stmt (compound_stmt);
9519 finish_function_body (body);
9521 expand_or_defer_fn (finish_function (2));
9523 /* Generate the body of the conversion op. */
9525 start_preparsed_function (convfn, NULL_TREE,
9526 SF_PRE_PARSED | SF_INCLASS_INLINE);
9527 body = begin_function_body ();
9528 compound_stmt = begin_compound_stmt (0);
9530 finish_return_stmt (decay_conversion (statfn, tf_warning_or_error));
9532 finish_compound_stmt (compound_stmt);
9533 finish_function_body (body);
9535 expand_or_defer_fn (finish_function (2));
9537 if (nested)
9538 pop_function_context ();
9539 else
9540 --function_depth;
9543 /* Returns true iff VAL is a lambda-related declaration which should
9544 be ignored by unqualified lookup. */
9546 bool
9547 is_lambda_ignored_entity (tree val)
9549 /* In unevaluated context, look past normal capture proxies. */
9550 if (cp_unevaluated_operand && is_normal_capture_proxy (val))
9551 return true;
9553 /* Always ignore lambda fields, their names are only for debugging. */
9554 if (TREE_CODE (val) == FIELD_DECL
9555 && CLASSTYPE_LAMBDA_EXPR (DECL_CONTEXT (val)))
9556 return true;
9558 /* None of the lookups that use qualify_lookup want the op() from the
9559 lambda; they want the one from the enclosing class. */
9560 if (TREE_CODE (val) == FUNCTION_DECL && LAMBDA_FUNCTION_P (val))
9561 return true;
9563 return false;
9566 #include "gt-cp-semantics.h"